16bd067c4SBogdan Purcareata // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
26bd067c4SBogdan Purcareata /*
36bd067c4SBogdan Purcareata * Copyright 2013-2016 Freescale Semiconductor Inc.
46bd067c4SBogdan Purcareata *
56bd067c4SBogdan Purcareata * I/O services to send MC commands to the MC hardware
66bd067c4SBogdan Purcareata *
76bd067c4SBogdan Purcareata */
86bd067c4SBogdan Purcareata
96bd067c4SBogdan Purcareata #include <linux/delay.h>
106bd067c4SBogdan Purcareata #include <linux/slab.h>
116bd067c4SBogdan Purcareata #include <linux/ioport.h>
126bd067c4SBogdan Purcareata #include <linux/device.h>
136bd067c4SBogdan Purcareata #include <linux/io.h>
146bd067c4SBogdan Purcareata #include <linux/io-64-nonatomic-hi-lo.h>
156bd067c4SBogdan Purcareata #include <linux/fsl/mc.h>
166bd067c4SBogdan Purcareata
176bd067c4SBogdan Purcareata #include "fsl-mc-private.h"
186bd067c4SBogdan Purcareata
19*33c6527aSLee Jones /*
206bd067c4SBogdan Purcareata * Timeout in milliseconds to wait for the completion of an MC command
216bd067c4SBogdan Purcareata */
226bd067c4SBogdan Purcareata #define MC_CMD_COMPLETION_TIMEOUT_MS 500
236bd067c4SBogdan Purcareata
246bd067c4SBogdan Purcareata /*
256bd067c4SBogdan Purcareata * usleep_range() min and max values used to throttle down polling
266bd067c4SBogdan Purcareata * iterations while waiting for MC command completion
276bd067c4SBogdan Purcareata */
286bd067c4SBogdan Purcareata #define MC_CMD_COMPLETION_POLLING_MIN_SLEEP_USECS 10
296bd067c4SBogdan Purcareata #define MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS 500
306bd067c4SBogdan Purcareata
mc_cmd_hdr_read_status(struct fsl_mc_command * cmd)315b04cedeSIoana Ciornei static enum mc_cmd_status mc_cmd_hdr_read_status(struct fsl_mc_command *cmd)
326bd067c4SBogdan Purcareata {
336bd067c4SBogdan Purcareata struct mc_cmd_header *hdr = (struct mc_cmd_header *)&cmd->header;
346bd067c4SBogdan Purcareata
356bd067c4SBogdan Purcareata return (enum mc_cmd_status)hdr->status;
366bd067c4SBogdan Purcareata }
376bd067c4SBogdan Purcareata
mc_cmd_hdr_read_cmdid(struct fsl_mc_command * cmd)3846707989SIoana Ciornei u16 mc_cmd_hdr_read_cmdid(struct fsl_mc_command *cmd)
396bd067c4SBogdan Purcareata {
406bd067c4SBogdan Purcareata struct mc_cmd_header *hdr = (struct mc_cmd_header *)&cmd->header;
416bd067c4SBogdan Purcareata u16 cmd_id = le16_to_cpu(hdr->cmd_id);
426bd067c4SBogdan Purcareata
436bd067c4SBogdan Purcareata return cmd_id;
446bd067c4SBogdan Purcareata }
456bd067c4SBogdan Purcareata
mc_status_to_error(enum mc_cmd_status status)466bd067c4SBogdan Purcareata static int mc_status_to_error(enum mc_cmd_status status)
476bd067c4SBogdan Purcareata {
486bd067c4SBogdan Purcareata static const int mc_status_to_error_map[] = {
496bd067c4SBogdan Purcareata [MC_CMD_STATUS_OK] = 0,
506bd067c4SBogdan Purcareata [MC_CMD_STATUS_AUTH_ERR] = -EACCES,
516bd067c4SBogdan Purcareata [MC_CMD_STATUS_NO_PRIVILEGE] = -EPERM,
526bd067c4SBogdan Purcareata [MC_CMD_STATUS_DMA_ERR] = -EIO,
536bd067c4SBogdan Purcareata [MC_CMD_STATUS_CONFIG_ERR] = -ENXIO,
546bd067c4SBogdan Purcareata [MC_CMD_STATUS_TIMEOUT] = -ETIMEDOUT,
556bd067c4SBogdan Purcareata [MC_CMD_STATUS_NO_RESOURCE] = -ENAVAIL,
566bd067c4SBogdan Purcareata [MC_CMD_STATUS_NO_MEMORY] = -ENOMEM,
576bd067c4SBogdan Purcareata [MC_CMD_STATUS_BUSY] = -EBUSY,
586bd067c4SBogdan Purcareata [MC_CMD_STATUS_UNSUPPORTED_OP] = -ENOTSUPP,
596bd067c4SBogdan Purcareata [MC_CMD_STATUS_INVALID_STATE] = -ENODEV,
606bd067c4SBogdan Purcareata };
616bd067c4SBogdan Purcareata
626bd067c4SBogdan Purcareata if ((u32)status >= ARRAY_SIZE(mc_status_to_error_map))
636bd067c4SBogdan Purcareata return -EINVAL;
646bd067c4SBogdan Purcareata
656bd067c4SBogdan Purcareata return mc_status_to_error_map[status];
666bd067c4SBogdan Purcareata }
676bd067c4SBogdan Purcareata
mc_status_to_string(enum mc_cmd_status status)686bd067c4SBogdan Purcareata static const char *mc_status_to_string(enum mc_cmd_status status)
696bd067c4SBogdan Purcareata {
706bd067c4SBogdan Purcareata static const char *const status_strings[] = {
716bd067c4SBogdan Purcareata [MC_CMD_STATUS_OK] = "Command completed successfully",
726bd067c4SBogdan Purcareata [MC_CMD_STATUS_READY] = "Command ready to be processed",
736bd067c4SBogdan Purcareata [MC_CMD_STATUS_AUTH_ERR] = "Authentication error",
746bd067c4SBogdan Purcareata [MC_CMD_STATUS_NO_PRIVILEGE] = "No privilege",
756bd067c4SBogdan Purcareata [MC_CMD_STATUS_DMA_ERR] = "DMA or I/O error",
766bd067c4SBogdan Purcareata [MC_CMD_STATUS_CONFIG_ERR] = "Configuration error",
776bd067c4SBogdan Purcareata [MC_CMD_STATUS_TIMEOUT] = "Operation timed out",
786bd067c4SBogdan Purcareata [MC_CMD_STATUS_NO_RESOURCE] = "No resources",
796bd067c4SBogdan Purcareata [MC_CMD_STATUS_NO_MEMORY] = "No memory available",
806bd067c4SBogdan Purcareata [MC_CMD_STATUS_BUSY] = "Device is busy",
816bd067c4SBogdan Purcareata [MC_CMD_STATUS_UNSUPPORTED_OP] = "Unsupported operation",
826bd067c4SBogdan Purcareata [MC_CMD_STATUS_INVALID_STATE] = "Invalid state"
836bd067c4SBogdan Purcareata };
846bd067c4SBogdan Purcareata
856bd067c4SBogdan Purcareata if ((unsigned int)status >= ARRAY_SIZE(status_strings))
866bd067c4SBogdan Purcareata return "Unknown MC error";
876bd067c4SBogdan Purcareata
886bd067c4SBogdan Purcareata return status_strings[status];
896bd067c4SBogdan Purcareata }
906bd067c4SBogdan Purcareata
916bd067c4SBogdan Purcareata /**
926bd067c4SBogdan Purcareata * mc_write_command - writes a command to a Management Complex (MC) portal
936bd067c4SBogdan Purcareata *
946bd067c4SBogdan Purcareata * @portal: pointer to an MC portal
956bd067c4SBogdan Purcareata * @cmd: pointer to a filled command
966bd067c4SBogdan Purcareata */
mc_write_command(struct fsl_mc_command __iomem * portal,struct fsl_mc_command * cmd)975b04cedeSIoana Ciornei static inline void mc_write_command(struct fsl_mc_command __iomem *portal,
985b04cedeSIoana Ciornei struct fsl_mc_command *cmd)
996bd067c4SBogdan Purcareata {
1006bd067c4SBogdan Purcareata int i;
1016bd067c4SBogdan Purcareata
1026bd067c4SBogdan Purcareata /* copy command parameters into the portal */
1036bd067c4SBogdan Purcareata for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
1046bd067c4SBogdan Purcareata /*
1056bd067c4SBogdan Purcareata * Data is already in the expected LE byte-order. Do an
1066bd067c4SBogdan Purcareata * extra LE -> CPU conversion so that the CPU -> LE done in
1076bd067c4SBogdan Purcareata * the device io write api puts it back in the right order.
1086bd067c4SBogdan Purcareata */
1096bd067c4SBogdan Purcareata writeq_relaxed(le64_to_cpu(cmd->params[i]), &portal->params[i]);
1106bd067c4SBogdan Purcareata
1116bd067c4SBogdan Purcareata /* submit the command by writing the header */
1126bd067c4SBogdan Purcareata writeq(le64_to_cpu(cmd->header), &portal->header);
1136bd067c4SBogdan Purcareata }
1146bd067c4SBogdan Purcareata
1156bd067c4SBogdan Purcareata /**
1166bd067c4SBogdan Purcareata * mc_read_response - reads the response for the last MC command from a
1176bd067c4SBogdan Purcareata * Management Complex (MC) portal
1186bd067c4SBogdan Purcareata *
1196bd067c4SBogdan Purcareata * @portal: pointer to an MC portal
1206bd067c4SBogdan Purcareata * @resp: pointer to command response buffer
1216bd067c4SBogdan Purcareata *
1226bd067c4SBogdan Purcareata * Returns MC_CMD_STATUS_OK on Success; Error code otherwise.
1236bd067c4SBogdan Purcareata */
mc_read_response(struct fsl_mc_command __iomem * portal,struct fsl_mc_command * resp)1245b04cedeSIoana Ciornei static inline enum mc_cmd_status mc_read_response(struct fsl_mc_command __iomem
1255b04cedeSIoana Ciornei *portal,
1265b04cedeSIoana Ciornei struct fsl_mc_command *resp)
1276bd067c4SBogdan Purcareata {
1286bd067c4SBogdan Purcareata int i;
1296bd067c4SBogdan Purcareata enum mc_cmd_status status;
1306bd067c4SBogdan Purcareata
1316bd067c4SBogdan Purcareata /* Copy command response header from MC portal: */
1326bd067c4SBogdan Purcareata resp->header = cpu_to_le64(readq_relaxed(&portal->header));
1336bd067c4SBogdan Purcareata status = mc_cmd_hdr_read_status(resp);
1346bd067c4SBogdan Purcareata if (status != MC_CMD_STATUS_OK)
1356bd067c4SBogdan Purcareata return status;
1366bd067c4SBogdan Purcareata
1376bd067c4SBogdan Purcareata /* Copy command response data from MC portal: */
1386bd067c4SBogdan Purcareata for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
1396bd067c4SBogdan Purcareata /*
1406bd067c4SBogdan Purcareata * Data is expected to be in LE byte-order. Do an
1416bd067c4SBogdan Purcareata * extra CPU -> LE to revert the LE -> CPU done in
1426bd067c4SBogdan Purcareata * the device io read api.
1436bd067c4SBogdan Purcareata */
1446bd067c4SBogdan Purcareata resp->params[i] =
1456bd067c4SBogdan Purcareata cpu_to_le64(readq_relaxed(&portal->params[i]));
1466bd067c4SBogdan Purcareata
1476bd067c4SBogdan Purcareata return status;
1486bd067c4SBogdan Purcareata }
1496bd067c4SBogdan Purcareata
1506bd067c4SBogdan Purcareata /**
151*33c6527aSLee Jones * mc_polling_wait_preemptible() - Waits for the completion of an MC
152*33c6527aSLee Jones * command doing preemptible polling.
153*33c6527aSLee Jones * uslepp_range() is called between
154*33c6527aSLee Jones * polling iterations.
1556bd067c4SBogdan Purcareata * @mc_io: MC I/O object to be used
1566bd067c4SBogdan Purcareata * @cmd: command buffer to receive MC response
1576bd067c4SBogdan Purcareata * @mc_status: MC command completion status
1586bd067c4SBogdan Purcareata */
mc_polling_wait_preemptible(struct fsl_mc_io * mc_io,struct fsl_mc_command * cmd,enum mc_cmd_status * mc_status)1596bd067c4SBogdan Purcareata static int mc_polling_wait_preemptible(struct fsl_mc_io *mc_io,
1605b04cedeSIoana Ciornei struct fsl_mc_command *cmd,
1616bd067c4SBogdan Purcareata enum mc_cmd_status *mc_status)
1626bd067c4SBogdan Purcareata {
1636bd067c4SBogdan Purcareata enum mc_cmd_status status;
1646bd067c4SBogdan Purcareata unsigned long jiffies_until_timeout =
1656bd067c4SBogdan Purcareata jiffies + msecs_to_jiffies(MC_CMD_COMPLETION_TIMEOUT_MS);
1666bd067c4SBogdan Purcareata
1676bd067c4SBogdan Purcareata /*
1686bd067c4SBogdan Purcareata * Wait for response from the MC hardware:
1696bd067c4SBogdan Purcareata */
1706bd067c4SBogdan Purcareata for (;;) {
1716bd067c4SBogdan Purcareata status = mc_read_response(mc_io->portal_virt_addr, cmd);
1726bd067c4SBogdan Purcareata if (status != MC_CMD_STATUS_READY)
1736bd067c4SBogdan Purcareata break;
1746bd067c4SBogdan Purcareata
1756bd067c4SBogdan Purcareata /*
1766bd067c4SBogdan Purcareata * TODO: When MC command completion interrupts are supported
1776bd067c4SBogdan Purcareata * call wait function here instead of usleep_range()
1786bd067c4SBogdan Purcareata */
1796bd067c4SBogdan Purcareata usleep_range(MC_CMD_COMPLETION_POLLING_MIN_SLEEP_USECS,
1806bd067c4SBogdan Purcareata MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS);
1816bd067c4SBogdan Purcareata
1826bd067c4SBogdan Purcareata if (time_after_eq(jiffies, jiffies_until_timeout)) {
1836bd067c4SBogdan Purcareata dev_dbg(mc_io->dev,
1846bd067c4SBogdan Purcareata "MC command timed out (portal: %pa, dprc handle: %#x, command: %#x)\n",
1856bd067c4SBogdan Purcareata &mc_io->portal_phys_addr,
1866bd067c4SBogdan Purcareata (unsigned int)mc_cmd_hdr_read_token(cmd),
1876bd067c4SBogdan Purcareata (unsigned int)mc_cmd_hdr_read_cmdid(cmd));
1886bd067c4SBogdan Purcareata
1896bd067c4SBogdan Purcareata return -ETIMEDOUT;
1906bd067c4SBogdan Purcareata }
1916bd067c4SBogdan Purcareata }
1926bd067c4SBogdan Purcareata
1936bd067c4SBogdan Purcareata *mc_status = status;
1946bd067c4SBogdan Purcareata return 0;
1956bd067c4SBogdan Purcareata }
1966bd067c4SBogdan Purcareata
1976bd067c4SBogdan Purcareata /**
198*33c6527aSLee Jones * mc_polling_wait_atomic() - Waits for the completion of an MC command
199*33c6527aSLee Jones * doing atomic polling. udelay() is called
200*33c6527aSLee Jones * between polling iterations.
2016bd067c4SBogdan Purcareata * @mc_io: MC I/O object to be used
2026bd067c4SBogdan Purcareata * @cmd: command buffer to receive MC response
2036bd067c4SBogdan Purcareata * @mc_status: MC command completion status
2046bd067c4SBogdan Purcareata */
mc_polling_wait_atomic(struct fsl_mc_io * mc_io,struct fsl_mc_command * cmd,enum mc_cmd_status * mc_status)2056bd067c4SBogdan Purcareata static int mc_polling_wait_atomic(struct fsl_mc_io *mc_io,
2065b04cedeSIoana Ciornei struct fsl_mc_command *cmd,
2076bd067c4SBogdan Purcareata enum mc_cmd_status *mc_status)
2086bd067c4SBogdan Purcareata {
2096bd067c4SBogdan Purcareata enum mc_cmd_status status;
2106bd067c4SBogdan Purcareata unsigned long timeout_usecs = MC_CMD_COMPLETION_TIMEOUT_MS * 1000;
2116bd067c4SBogdan Purcareata
2126bd067c4SBogdan Purcareata BUILD_BUG_ON((MC_CMD_COMPLETION_TIMEOUT_MS * 1000) %
2136bd067c4SBogdan Purcareata MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS != 0);
2146bd067c4SBogdan Purcareata
2156bd067c4SBogdan Purcareata for (;;) {
2166bd067c4SBogdan Purcareata status = mc_read_response(mc_io->portal_virt_addr, cmd);
2176bd067c4SBogdan Purcareata if (status != MC_CMD_STATUS_READY)
2186bd067c4SBogdan Purcareata break;
2196bd067c4SBogdan Purcareata
2206bd067c4SBogdan Purcareata udelay(MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS);
2216bd067c4SBogdan Purcareata timeout_usecs -= MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS;
2226bd067c4SBogdan Purcareata if (timeout_usecs == 0) {
2236bd067c4SBogdan Purcareata dev_dbg(mc_io->dev,
2246bd067c4SBogdan Purcareata "MC command timed out (portal: %pa, dprc handle: %#x, command: %#x)\n",
2256bd067c4SBogdan Purcareata &mc_io->portal_phys_addr,
2266bd067c4SBogdan Purcareata (unsigned int)mc_cmd_hdr_read_token(cmd),
2276bd067c4SBogdan Purcareata (unsigned int)mc_cmd_hdr_read_cmdid(cmd));
2286bd067c4SBogdan Purcareata
2296bd067c4SBogdan Purcareata return -ETIMEDOUT;
2306bd067c4SBogdan Purcareata }
2316bd067c4SBogdan Purcareata }
2326bd067c4SBogdan Purcareata
2336bd067c4SBogdan Purcareata *mc_status = status;
2346bd067c4SBogdan Purcareata return 0;
2356bd067c4SBogdan Purcareata }
2366bd067c4SBogdan Purcareata
2376bd067c4SBogdan Purcareata /**
238*33c6527aSLee Jones * mc_send_command() - Sends a command to the MC device using the given
239*33c6527aSLee Jones * MC I/O object
2406bd067c4SBogdan Purcareata * @mc_io: MC I/O object to be used
2416bd067c4SBogdan Purcareata * @cmd: command to be sent
2426bd067c4SBogdan Purcareata *
2436bd067c4SBogdan Purcareata * Returns '0' on Success; Error code otherwise.
2446bd067c4SBogdan Purcareata */
mc_send_command(struct fsl_mc_io * mc_io,struct fsl_mc_command * cmd)2455b04cedeSIoana Ciornei int mc_send_command(struct fsl_mc_io *mc_io, struct fsl_mc_command *cmd)
2466bd067c4SBogdan Purcareata {
2476bd067c4SBogdan Purcareata int error;
2486bd067c4SBogdan Purcareata enum mc_cmd_status status;
2496bd067c4SBogdan Purcareata unsigned long irq_flags = 0;
2506bd067c4SBogdan Purcareata
2516bd067c4SBogdan Purcareata if (in_irq() && !(mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL))
2526bd067c4SBogdan Purcareata return -EINVAL;
2536bd067c4SBogdan Purcareata
2546bd067c4SBogdan Purcareata if (mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL)
2559a872defSLaurentiu Tudor raw_spin_lock_irqsave(&mc_io->spinlock, irq_flags);
2566bd067c4SBogdan Purcareata else
2576bd067c4SBogdan Purcareata mutex_lock(&mc_io->mutex);
2586bd067c4SBogdan Purcareata
2596bd067c4SBogdan Purcareata /*
2606bd067c4SBogdan Purcareata * Send command to the MC hardware:
2616bd067c4SBogdan Purcareata */
2626bd067c4SBogdan Purcareata mc_write_command(mc_io->portal_virt_addr, cmd);
2636bd067c4SBogdan Purcareata
2646bd067c4SBogdan Purcareata /*
2656bd067c4SBogdan Purcareata * Wait for response from the MC hardware:
2666bd067c4SBogdan Purcareata */
2676bd067c4SBogdan Purcareata if (!(mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL))
2686bd067c4SBogdan Purcareata error = mc_polling_wait_preemptible(mc_io, cmd, &status);
2696bd067c4SBogdan Purcareata else
2706bd067c4SBogdan Purcareata error = mc_polling_wait_atomic(mc_io, cmd, &status);
2716bd067c4SBogdan Purcareata
2726bd067c4SBogdan Purcareata if (error < 0)
2736bd067c4SBogdan Purcareata goto common_exit;
2746bd067c4SBogdan Purcareata
2756bd067c4SBogdan Purcareata if (status != MC_CMD_STATUS_OK) {
2766bd067c4SBogdan Purcareata dev_dbg(mc_io->dev,
2776bd067c4SBogdan Purcareata "MC command failed: portal: %pa, dprc handle: %#x, command: %#x, status: %s (%#x)\n",
2786bd067c4SBogdan Purcareata &mc_io->portal_phys_addr,
2796bd067c4SBogdan Purcareata (unsigned int)mc_cmd_hdr_read_token(cmd),
2806bd067c4SBogdan Purcareata (unsigned int)mc_cmd_hdr_read_cmdid(cmd),
2816bd067c4SBogdan Purcareata mc_status_to_string(status),
2826bd067c4SBogdan Purcareata (unsigned int)status);
2836bd067c4SBogdan Purcareata
2846bd067c4SBogdan Purcareata error = mc_status_to_error(status);
2856bd067c4SBogdan Purcareata goto common_exit;
2866bd067c4SBogdan Purcareata }
2876bd067c4SBogdan Purcareata
2886bd067c4SBogdan Purcareata error = 0;
2896bd067c4SBogdan Purcareata common_exit:
2906bd067c4SBogdan Purcareata if (mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL)
2919a872defSLaurentiu Tudor raw_spin_unlock_irqrestore(&mc_io->spinlock, irq_flags);
2926bd067c4SBogdan Purcareata else
2936bd067c4SBogdan Purcareata mutex_unlock(&mc_io->mutex);
2946bd067c4SBogdan Purcareata
2956bd067c4SBogdan Purcareata return error;
2966bd067c4SBogdan Purcareata }
2976bd067c4SBogdan Purcareata EXPORT_SYMBOL_GPL(mc_send_command);
298