Merge tag 'ntb-4.4' of git://github.com/jonmason/ntb
[firefly-linux-kernel-4.4.55.git] / drivers / staging / fsl-mc / bus / mc-sys.c
index c4f5bdd3cb2946e392af0f675958dcfbf0dfee3e..6e1489246066d8d9092a933f391fd19e3ab62ff9 100644 (file)
@@ -88,6 +88,11 @@ int __must_check fsl_create_mc_io(struct device *dev,
        mc_io->flags = flags;
        mc_io->portal_phys_addr = mc_portal_phys_addr;
        mc_io->portal_size = mc_portal_size;
+       if (flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL)
+               spin_lock_init(&mc_io->spinlock);
+       else
+               mutex_init(&mc_io->mutex);
+
        res = devm_request_mem_region(dev,
                                      mc_portal_phys_addr,
                                      mc_portal_size,
@@ -338,6 +343,47 @@ static int mc_polling_wait_preemptible(struct fsl_mc_io *mc_io,
        return 0;
 }
 
+/**
+ * Waits for the completion of an MC command doing atomic polling.
+ * udelay() is called between polling iterations.
+ *
+ * @mc_io: MC I/O object to be used
+ * @cmd: command buffer to receive MC response
+ * @mc_status: MC command completion status
+ */
+static int mc_polling_wait_atomic(struct fsl_mc_io *mc_io,
+                                 struct mc_command *cmd,
+                                 enum mc_cmd_status *mc_status)
+{
+       enum mc_cmd_status status;
+       unsigned long timeout_usecs = MC_CMD_COMPLETION_TIMEOUT_MS * 1000;
+
+       BUILD_BUG_ON((MC_CMD_COMPLETION_TIMEOUT_MS * 1000) %
+                    MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS != 0);
+
+       for (;;) {
+               status = mc_read_response(mc_io->portal_virt_addr, cmd);
+               if (status != MC_CMD_STATUS_READY)
+                       break;
+
+               udelay(MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS);
+               timeout_usecs -= MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS;
+               if (timeout_usecs == 0) {
+                       pr_debug("MC command timed out (portal: %#llx, obj handle: %#x, command: %#x)\n",
+                                mc_io->portal_phys_addr,
+                                (unsigned int)
+                                       MC_CMD_HDR_READ_TOKEN(cmd->header),
+                                (unsigned int)
+                                       MC_CMD_HDR_READ_CMDID(cmd->header));
+
+                       return -ETIMEDOUT;
+               }
+       }
+
+       *mc_status = status;
+       return 0;
+}
+
 /**
  * Sends a command to the MC device using the given MC I/O object
  *
@@ -345,13 +391,21 @@ static int mc_polling_wait_preemptible(struct fsl_mc_io *mc_io,
  * @cmd: command to be sent
  *
  * Returns '0' on Success; Error code otherwise.
- *
- * NOTE: This function cannot be invoked from from atomic contexts.
  */
 int mc_send_command(struct fsl_mc_io *mc_io, struct mc_command *cmd)
 {
        int error;
        enum mc_cmd_status status;
+       unsigned long irq_flags = 0;
+
+       if (WARN_ON(in_irq() &&
+                   !(mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL)))
+               return -EINVAL;
+
+       if (mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL)
+               spin_lock_irqsave(&mc_io->spinlock, irq_flags);
+       else
+               mutex_lock(&mc_io->mutex);
 
        /*
         * Send command to the MC hardware:
@@ -361,9 +415,13 @@ int mc_send_command(struct fsl_mc_io *mc_io, struct mc_command *cmd)
        /*
         * Wait for response from the MC hardware:
         */
-       error = mc_polling_wait_preemptible(mc_io, cmd, &status);
+       if (!(mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL))
+               error = mc_polling_wait_preemptible(mc_io, cmd, &status);
+       else
+               error = mc_polling_wait_atomic(mc_io, cmd, &status);
+
        if (error < 0)
-               return error;
+               goto common_exit;
 
        if (status != MC_CMD_STATUS_OK) {
                pr_debug("MC command failed: portal: %#llx, obj handle: %#x, command: %#x, status: %s (%#x)\n",
@@ -373,9 +431,17 @@ int mc_send_command(struct fsl_mc_io *mc_io, struct mc_command *cmd)
                         mc_status_to_string(status),
                         (unsigned int)status);
 
-               return mc_status_to_error(status);
+               error = mc_status_to_error(status);
+               goto common_exit;
        }
 
-       return 0;
+       error = 0;
+common_exit:
+       if (mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL)
+               spin_unlock_irqrestore(&mc_io->spinlock, irq_flags);
+       else
+               mutex_unlock(&mc_io->mutex);
+
+       return error;
 }
 EXPORT_SYMBOL(mc_send_command);