New upstream version 17.11-rc3
[deb_dpdk.git] / drivers / bus / fslmc / mc / mc_sys.c
1 /*-
2  * This file is provided under a dual BSD/GPLv2 license. When using or
3  * redistributing this file, you may do so under either license.
4  *
5  *   BSD LICENSE
6  *
7  * Copyright 2013-2015 Freescale Semiconductor Inc.
8  * Copyright 2017 NXP.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  * * Neither the name of the above-listed copyright holders nor the
18  * names of any contributors may be used to endorse or promote products
19  * derived from this software without specific prior written permission.
20  *
21  *   GPL LICENSE SUMMARY
22  *
23  * ALTERNATIVELY, this software may be distributed under the terms of the
24  * GNU General Public License ("GPL") as published by the Free Software
25  * Foundation, either version 2 of that License or (at your option) any
26  * later version.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
32  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40 #include <fsl_mc_sys.h>
41 #include <fsl_mc_cmd.h>
42
43 #include <rte_spinlock.h>
44
45 /** User space framework uses MC Portal in shared mode. Following change
46  * introduces lock in MC FLIB
47  */
48
49 /**
50  * A static spinlock initializer.
51  */
52 static rte_spinlock_t mc_portal_lock = RTE_SPINLOCK_INITIALIZER;
53
54 static int mc_status_to_error(enum mc_cmd_status status)
55 {
56         switch (status) {
57         case MC_CMD_STATUS_OK:
58                 return 0;
59         case MC_CMD_STATUS_AUTH_ERR:
60                 return -EACCES; /* Token error */
61         case MC_CMD_STATUS_NO_PRIVILEGE:
62                 return -EPERM; /* Permission denied */
63         case MC_CMD_STATUS_DMA_ERR:
64                 return -EIO; /* Input/Output error */
65         case MC_CMD_STATUS_CONFIG_ERR:
66                 return -EINVAL; /* Device not configured */
67         case MC_CMD_STATUS_TIMEOUT:
68                 return -ETIMEDOUT; /* Operation timed out */
69         case MC_CMD_STATUS_NO_RESOURCE:
70                 return -ENAVAIL; /* Resource temporarily unavailable */
71         case MC_CMD_STATUS_NO_MEMORY:
72                 return -ENOMEM; /* Cannot allocate memory */
73         case MC_CMD_STATUS_BUSY:
74                 return -EBUSY; /* Device busy */
75         case MC_CMD_STATUS_UNSUPPORTED_OP:
76                 return -ENOTSUP; /* Operation not supported by device */
77         case MC_CMD_STATUS_INVALID_STATE:
78                 return -ENODEV; /* Invalid device state */
79         default:
80                 break;
81         }
82
83         /* Not expected to reach here */
84         return -EINVAL;
85 }
86
87 int mc_send_command(struct fsl_mc_io *mc_io, struct mc_command *cmd)
88 {
89         enum mc_cmd_status status;
90         uint64_t response;
91
92         if (!mc_io || !mc_io->regs)
93                 return -EACCES;
94
95         /* --- Call lock function here in case portal is shared --- */
96         rte_spinlock_lock(&mc_portal_lock);
97
98         mc_write_command(mc_io->regs, cmd);
99
100         /* Spin until status changes */
101         do {
102                 response = ioread64(mc_io->regs);
103                 status = mc_cmd_read_status((struct mc_command *)&response);
104
105                 /* --- Call wait function here to prevent blocking ---
106                  * Change the loop condition accordingly to exit on timeout.
107                  */
108         } while (status == MC_CMD_STATUS_READY);
109
110         /* Read the response back into the command buffer */
111         mc_read_response(mc_io->regs, cmd);
112
113         /* --- Call unlock function here in case portal is shared --- */
114         rte_spinlock_unlock(&mc_portal_lock);
115
116         return mc_status_to_error(status);
117 }