Branch data Line data Source code
1 : : /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0) 2 : : * 3 : : * Copyright 2013-2015 Freescale Semiconductor Inc. 4 : : * Copyright 2017,2022 NXP 5 : : * 6 : : */ 7 : : #include <fsl_mc_sys.h> 8 : : #include <fsl_mc_cmd.h> 9 : : 10 : : #include <eal_export.h> 11 : : #include <rte_spinlock.h> 12 : : #include <rte_cycles.h> 13 : : 14 : : /** User space framework uses MC Portal in shared mode. Following change 15 : : * introduces lock in MC FLIB 16 : : */ 17 : : 18 : : /** 19 : : * A static spinlock initializer. 20 : : */ 21 : : static rte_spinlock_t mc_portal_lock = RTE_SPINLOCK_INITIALIZER; 22 : : 23 : : static int mc_status_to_error(enum mc_cmd_status status) 24 : : { 25 : : switch (status) { 26 : : case MC_CMD_STATUS_OK: 27 : : return 0; 28 : : case MC_CMD_STATUS_AUTH_ERR: 29 : : return -EACCES; /* Token error */ 30 : : case MC_CMD_STATUS_NO_PRIVILEGE: 31 : : return -EPERM; /* Permission denied */ 32 : : case MC_CMD_STATUS_DMA_ERR: 33 : : return -EIO; /* Input/Output error */ 34 : : case MC_CMD_STATUS_CONFIG_ERR: 35 : : return -EINVAL; /* Device not configured */ 36 : : case MC_CMD_STATUS_TIMEOUT: 37 : : return -ETIMEDOUT; /* Operation timed out */ 38 : : case MC_CMD_STATUS_NO_RESOURCE: 39 : : return -ENAVAIL; /* Resource temporarily unavailable */ 40 : : case MC_CMD_STATUS_NO_MEMORY: 41 : : return -ENOMEM; /* Cannot allocate memory */ 42 : : case MC_CMD_STATUS_BUSY: 43 : : return -EBUSY; /* Device busy */ 44 : : case MC_CMD_STATUS_UNSUPPORTED_OP: 45 : : return -ENOTSUP; /* Operation not supported by device */ 46 : : case MC_CMD_STATUS_INVALID_STATE: 47 : : return -ENODEV; /* Invalid device state */ 48 : : default: 49 : : break; 50 : : } 51 : : 52 : : /* Not expected to reach here */ 53 : : return -EINVAL; 54 : : } 55 : : 56 : : RTE_EXPORT_INTERNAL_SYMBOL(mc_send_command) 57 : 0 : int mc_send_command(struct fsl_mc_io *mc_io, struct mc_command *cmd) 58 : : { 59 : : enum mc_cmd_status status; 60 : : uint64_t response, start_time, total_time, time_to_wait; 61 : : 62 [ # # # # ]: 0 : if (!mc_io || !mc_io->regs) 63 : : return -EACCES; 64 : : 65 : : /* --- Call lock function here in case portal is shared --- */ 66 : : rte_spinlock_lock(&mc_portal_lock); 67 : : 68 : 0 : mc_write_command(mc_io->regs, cmd); 69 : : 70 : : /* Wait for one second. rte_get_timer_hz() returns frequency of CPU */ 71 : : time_to_wait = rte_get_timer_hz(); 72 : : total_time = 0; 73 : : start_time = rte_get_timer_cycles(); 74 : : 75 : : /* Spin until status changes */ 76 : : do { 77 : 0 : response = ioread64(mc_io->regs); 78 : : status = mc_cmd_read_status((struct mc_command *)&response); 79 : 0 : total_time = rte_get_timer_cycles() - start_time; 80 [ # # ]: 0 : } while (status == MC_CMD_STATUS_READY && total_time <= time_to_wait); 81 : : 82 [ # # ]: 0 : if (status == MC_CMD_STATUS_READY) { 83 : : rte_spinlock_unlock(&mc_portal_lock); 84 : : 85 : 0 : return mc_status_to_error(MC_CMD_STATUS_TIMEOUT); 86 : : } 87 : : 88 : : /* Read the response back into the command buffer */ 89 : 0 : mc_read_response(mc_io->regs, cmd); 90 : : 91 : : /* --- Call unlock function here in case portal is shared --- */ 92 : : rte_spinlock_unlock(&mc_portal_lock); 93 : : 94 : : return mc_status_to_error(status); 95 : : }