Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * ISH-TP client driver for ISH firmware loading
0004  *
0005  * Copyright (c) 2019, Intel Corporation.
0006  */
0007 
0008 #include <linux/firmware.h>
0009 #include <linux/module.h>
0010 #include <linux/pci.h>
0011 #include <linux/intel-ish-client-if.h>
0012 #include <linux/property.h>
0013 #include <asm/cacheflush.h>
0014 
0015 /* Number of times we attempt to load the firmware before giving up */
0016 #define MAX_LOAD_ATTEMPTS           3
0017 
0018 /* ISH TX/RX ring buffer pool size */
0019 #define LOADER_CL_RX_RING_SIZE          1
0020 #define LOADER_CL_TX_RING_SIZE          1
0021 
0022 /*
0023  * ISH Shim firmware loader reserves 4 Kb buffer in SRAM. The buffer is
0024  * used to temporarily hold the data transferred from host to Shim
0025  * firmware loader. Reason for the odd size of 3968 bytes? Each IPC
0026  * transfer is 128 bytes (= 4 bytes header + 124 bytes payload). So the
0027  * 4 Kb buffer can hold maximum of 32 IPC transfers, which means we can
0028  * have a max payload of 3968 bytes (= 32 x 124 payload).
0029  */
0030 #define LOADER_SHIM_IPC_BUF_SIZE        3968
0031 
0032 /**
0033  * enum ish_loader_commands -   ISH loader host commands.
0034  * @LOADER_CMD_XFER_QUERY:  Query the Shim firmware loader for
0035  *              capabilities
0036  * @LOADER_CMD_XFER_FRAGMENT:   Transfer one firmware image fragment at a
0037  *              time. The command may be executed
0038  *              multiple times until the entire firmware
0039  *              image is downloaded to SRAM.
0040  * @LOADER_CMD_START:       Start executing the main firmware.
0041  */
0042 enum ish_loader_commands {
0043     LOADER_CMD_XFER_QUERY = 0,
0044     LOADER_CMD_XFER_FRAGMENT,
0045     LOADER_CMD_START,
0046 };
0047 
0048 /* Command bit mask */
0049 #define CMD_MASK                GENMASK(6, 0)
0050 #define IS_RESPONSE             BIT(7)
0051 
0052 /*
0053  * ISH firmware max delay for one transmit failure is 1 Hz,
0054  * and firmware will retry 2 times, so 3 Hz is used for timeout.
0055  */
0056 #define ISHTP_SEND_TIMEOUT          (3 * HZ)
0057 
0058 /*
0059  * Loader transfer modes:
0060  *
0061  * LOADER_XFER_MODE_ISHTP mode uses the existing ISH-TP mechanism to
0062  * transfer data. This may use IPC or DMA if supported in firmware.
0063  * The buffer size is limited to 4 Kb by the IPC/ISH-TP protocol for
0064  * both IPC & DMA (legacy).
0065  *
0066  * LOADER_XFER_MODE_DIRECT_DMA - firmware loading is a bit different
0067  * from the sensor data streaming. Here we download a large (300+ Kb)
0068  * image directly to ISH SRAM memory. There is limited benefit of
0069  * DMA'ing 300 Kb image in 4 Kb chucks limit. Hence, we introduce
0070  * this "direct dma" mode, where we do not use ISH-TP for DMA, but
0071  * instead manage the DMA directly in kernel driver and Shim firmware
0072  * loader (allocate buffer, break in chucks and transfer). This allows
0073  * to overcome 4 Kb limit, and optimize the data flow path in firmware.
0074  */
0075 #define LOADER_XFER_MODE_DIRECT_DMA     BIT(0)
0076 #define LOADER_XFER_MODE_ISHTP          BIT(1)
0077 
0078 /* ISH Transport Loader client unique GUID */
0079 static const struct ishtp_device_id loader_ishtp_id_table[] = {
0080     { .guid = GUID_INIT(0xc804d06a, 0x55bd, 0x4ea7,
0081           0xad, 0xed, 0x1e, 0x31, 0x22, 0x8c, 0x76, 0xdc) },
0082     { }
0083 };
0084 MODULE_DEVICE_TABLE(ishtp, loader_ishtp_id_table);
0085 
0086 #define FILENAME_SIZE               256
0087 
0088 /*
0089  * The firmware loading latency will be minimum if we can DMA the
0090  * entire ISH firmware image in one go. This requires that we allocate
0091  * a large DMA buffer in kernel, which could be problematic on some
0092  * platforms. So here we limit the DMA buffer size via a module_param.
0093  * We default to 4 pages, but a customer can set it to higher limit if
0094  * deemed appropriate for his platform.
0095  */
0096 static int dma_buf_size_limit = 4 * PAGE_SIZE;
0097 
0098 /**
0099  * struct loader_msg_hdr - Header for ISH Loader commands.
0100  * @command:        LOADER_CMD* commands. Bit 7 is the response.
0101  * @reserved:       Reserved space
0102  * @status:     Command response status. Non 0, is error
0103  *          condition.
0104  *
0105  * This structure is used as header for every command/data sent/received
0106  * between Host driver and ISH Shim firmware loader.
0107  */
0108 struct loader_msg_hdr {
0109     u8 command;
0110     u8 reserved[2];
0111     u8 status;
0112 } __packed;
0113 
0114 struct loader_xfer_query {
0115     struct loader_msg_hdr hdr;
0116     u32 image_size;
0117 } __packed;
0118 
0119 struct ish_fw_version {
0120     u16 major;
0121     u16 minor;
0122     u16 hotfix;
0123     u16 build;
0124 } __packed;
0125 
0126 union loader_version {
0127     u32 value;
0128     struct {
0129         u8 major;
0130         u8 minor;
0131         u8 hotfix;
0132         u8 build;
0133     };
0134 } __packed;
0135 
0136 struct loader_capability {
0137     u32 max_fw_image_size;
0138     u32 xfer_mode;
0139     u32 max_dma_buf_size; /* only for dma mode, multiples of cacheline */
0140 } __packed;
0141 
0142 struct shim_fw_info {
0143     struct ish_fw_version ish_fw_version;
0144     u32 protocol_version;
0145     union loader_version ldr_version;
0146     struct loader_capability ldr_capability;
0147 } __packed;
0148 
0149 struct loader_xfer_query_response {
0150     struct loader_msg_hdr hdr;
0151     struct shim_fw_info fw_info;
0152 } __packed;
0153 
0154 struct loader_xfer_fragment {
0155     struct loader_msg_hdr hdr;
0156     u32 xfer_mode;
0157     u32 offset;
0158     u32 size;
0159     u32 is_last;
0160 } __packed;
0161 
0162 struct loader_xfer_ipc_fragment {
0163     struct loader_xfer_fragment fragment;
0164     u8 data[] ____cacheline_aligned; /* variable length payload here */
0165 } __packed;
0166 
0167 struct loader_xfer_dma_fragment {
0168     struct loader_xfer_fragment fragment;
0169     u64 ddr_phys_addr;
0170 } __packed;
0171 
0172 struct loader_start {
0173     struct loader_msg_hdr hdr;
0174 } __packed;
0175 
0176 /**
0177  * struct response_info - Encapsulate firmware response related
0178  *          information for passing between function
0179  *          loader_cl_send() and process_recv() callback.
0180  * @data:       Copy the data received from firmware here.
0181  * @max_size:       Max size allocated for the @data buffer. If the
0182  *          received data exceeds this value, we log an
0183  *          error.
0184  * @size:       Actual size of data received from firmware.
0185  * @error:      Returns 0 for success, negative error code for a
0186  *          failure in function process_recv().
0187  * @received:       Set to true on receiving a valid firmware
0188  *          response to host command
0189  * @wait_queue:     Wait queue for Host firmware loading where the
0190  *          client sends message to ISH firmware and waits
0191  *          for response
0192  */
0193 struct response_info {
0194     void *data;
0195     size_t max_size;
0196     size_t size;
0197     int error;
0198     bool received;
0199     wait_queue_head_t wait_queue;
0200 };
0201 
0202 /*
0203  * struct ishtp_cl_data - Encapsulate per ISH-TP Client Data.
0204  * @work_ishtp_reset:   Work queue for reset handling.
0205  * @work_fw_load:   Work queue for host firmware loading.
0206  * @flag_retry:     Flag for indicating host firmware loading should
0207  *          be retried.
0208  * @retry_count:    Count the number of retries.
0209  *
0210  * This structure is used to store data per client.
0211  */
0212 struct ishtp_cl_data {
0213     struct ishtp_cl *loader_ishtp_cl;
0214     struct ishtp_cl_device *cl_device;
0215 
0216     /*
0217      * Used for passing firmware response information between
0218      * loader_cl_send() and process_recv() callback.
0219      */
0220     struct response_info response;
0221 
0222     struct work_struct work_ishtp_reset;
0223     struct work_struct work_fw_load;
0224 
0225     /*
0226      * In certain failure scenrios, it makes sense to reset the ISH
0227      * subsystem and retry Host firmware loading (e.g. bad message
0228      * packet, ENOMEM, etc.). On the other hand, failures due to
0229      * protocol mismatch, etc., are not recoverable. We do not
0230      * retry them.
0231      *
0232      * If set, the flag indicates that we should re-try the
0233      * particular failure.
0234      */
0235     bool flag_retry;
0236     int retry_count;
0237 };
0238 
0239 #define IPC_FRAGMENT_DATA_PREAMBLE              \
0240     offsetof(struct loader_xfer_ipc_fragment, data)
0241 
0242 #define cl_data_to_dev(client_data) ishtp_device((client_data)->cl_device)
0243 
0244 /**
0245  * get_firmware_variant() - Gets the filename of firmware image to be
0246  *          loaded based on platform variant.
0247  * @client_data:    Client data instance.
0248  * @filename:       Returns firmware filename.
0249  *
0250  * Queries the firmware-name device property string.
0251  *
0252  * Return: 0 for success, negative error code for failure.
0253  */
0254 static int get_firmware_variant(struct ishtp_cl_data *client_data,
0255                 char *filename)
0256 {
0257     int rv;
0258     const char *val;
0259     struct device *devc = ishtp_get_pci_device(client_data->cl_device);
0260 
0261     rv = device_property_read_string(devc, "firmware-name", &val);
0262     if (rv < 0) {
0263         dev_err(devc,
0264             "Error: ISH firmware-name device property required\n");
0265         return rv;
0266     }
0267     return snprintf(filename, FILENAME_SIZE, "intel/%s", val);
0268 }
0269 
0270 /**
0271  * loader_cl_send() - Send message from host to firmware
0272  *
0273  * @client_data:    Client data instance
0274  * @out_msg:        Message buffer to be sent to firmware
0275  * @out_size:       Size of out going message
0276  * @in_msg:     Message buffer where the incoming data copied.
0277  *          This buffer is allocated by calling
0278  * @in_size:        Max size of incoming message
0279  *
0280  * Return: Number of bytes copied in the in_msg on success, negative
0281  * error code on failure.
0282  */
0283 static int loader_cl_send(struct ishtp_cl_data *client_data,
0284               u8 *out_msg, size_t out_size,
0285               u8 *in_msg, size_t in_size)
0286 {
0287     int rv;
0288     struct loader_msg_hdr *out_hdr = (struct loader_msg_hdr *)out_msg;
0289     struct ishtp_cl *loader_ishtp_cl = client_data->loader_ishtp_cl;
0290 
0291     dev_dbg(cl_data_to_dev(client_data),
0292         "%s: command=%02lx is_response=%u status=%02x\n",
0293         __func__,
0294         out_hdr->command & CMD_MASK,
0295         out_hdr->command & IS_RESPONSE ? 1 : 0,
0296         out_hdr->status);
0297 
0298     /* Setup in coming buffer & size */
0299     client_data->response.data = in_msg;
0300     client_data->response.max_size = in_size;
0301     client_data->response.error = 0;
0302     client_data->response.received = false;
0303 
0304     rv = ishtp_cl_send(loader_ishtp_cl, out_msg, out_size);
0305     if (rv < 0) {
0306         dev_err(cl_data_to_dev(client_data),
0307             "ishtp_cl_send error %d\n", rv);
0308         return rv;
0309     }
0310 
0311     wait_event_interruptible_timeout(client_data->response.wait_queue,
0312                      client_data->response.received,
0313                      ISHTP_SEND_TIMEOUT);
0314     if (!client_data->response.received) {
0315         dev_err(cl_data_to_dev(client_data),
0316             "Timed out for response to command=%02lx",
0317             out_hdr->command & CMD_MASK);
0318         return -ETIMEDOUT;
0319     }
0320 
0321     if (client_data->response.error < 0)
0322         return client_data->response.error;
0323 
0324     return client_data->response.size;
0325 }
0326 
0327 /**
0328  * process_recv() - Receive and parse incoming packet
0329  * @loader_ishtp_cl:    Client instance to get stats
0330  * @rb_in_proc:     ISH received message buffer
0331  *
0332  * Parse the incoming packet. If it is a response packet then it will
0333  * update received and wake up the caller waiting to for the response.
0334  */
0335 static void process_recv(struct ishtp_cl *loader_ishtp_cl,
0336              struct ishtp_cl_rb *rb_in_proc)
0337 {
0338     struct loader_msg_hdr *hdr;
0339     size_t data_len = rb_in_proc->buf_idx;
0340     struct ishtp_cl_data *client_data =
0341         ishtp_get_client_data(loader_ishtp_cl);
0342 
0343     /* Sanity check */
0344     if (!client_data->response.data) {
0345         dev_err(cl_data_to_dev(client_data),
0346             "Receiving buffer is null. Should be allocated by calling function\n");
0347         client_data->response.error = -EINVAL;
0348         goto end;
0349     }
0350 
0351     if (client_data->response.received) {
0352         dev_err(cl_data_to_dev(client_data),
0353             "Previous firmware message not yet processed\n");
0354         client_data->response.error = -EINVAL;
0355         goto end;
0356     }
0357     /*
0358      * All firmware messages have a header. Check buffer size
0359      * before accessing elements inside.
0360      */
0361     if (!rb_in_proc->buffer.data) {
0362         dev_warn(cl_data_to_dev(client_data),
0363              "rb_in_proc->buffer.data returned null");
0364         client_data->response.error = -EBADMSG;
0365         goto end;
0366     }
0367 
0368     if (data_len < sizeof(struct loader_msg_hdr)) {
0369         dev_err(cl_data_to_dev(client_data),
0370             "data size %zu is less than header %zu\n",
0371             data_len, sizeof(struct loader_msg_hdr));
0372         client_data->response.error = -EMSGSIZE;
0373         goto end;
0374     }
0375 
0376     hdr = (struct loader_msg_hdr *)rb_in_proc->buffer.data;
0377 
0378     dev_dbg(cl_data_to_dev(client_data),
0379         "%s: command=%02lx is_response=%u status=%02x\n",
0380         __func__,
0381         hdr->command & CMD_MASK,
0382         hdr->command & IS_RESPONSE ? 1 : 0,
0383         hdr->status);
0384 
0385     if (((hdr->command & CMD_MASK) != LOADER_CMD_XFER_QUERY) &&
0386         ((hdr->command & CMD_MASK) != LOADER_CMD_XFER_FRAGMENT) &&
0387         ((hdr->command & CMD_MASK) != LOADER_CMD_START)) {
0388         dev_err(cl_data_to_dev(client_data),
0389             "Invalid command=%02lx\n",
0390             hdr->command & CMD_MASK);
0391         client_data->response.error = -EPROTO;
0392         goto end;
0393     }
0394 
0395     if (data_len > client_data->response.max_size) {
0396         dev_err(cl_data_to_dev(client_data),
0397             "Received buffer size %zu is larger than allocated buffer %zu\n",
0398             data_len, client_data->response.max_size);
0399         client_data->response.error = -EMSGSIZE;
0400         goto end;
0401     }
0402 
0403     /* We expect only "response" messages from firmware */
0404     if (!(hdr->command & IS_RESPONSE)) {
0405         dev_err(cl_data_to_dev(client_data),
0406             "Invalid response to command\n");
0407         client_data->response.error = -EIO;
0408         goto end;
0409     }
0410 
0411     if (hdr->status) {
0412         dev_err(cl_data_to_dev(client_data),
0413             "Loader returned status %d\n",
0414             hdr->status);
0415         client_data->response.error = -EIO;
0416         goto end;
0417     }
0418 
0419     /* Update the actual received buffer size */
0420     client_data->response.size = data_len;
0421 
0422     /*
0423      * Copy the buffer received in firmware response for the
0424      * calling thread.
0425      */
0426     memcpy(client_data->response.data,
0427            rb_in_proc->buffer.data, data_len);
0428 
0429     /* Set flag before waking up the caller */
0430     client_data->response.received = true;
0431 
0432 end:
0433     /* Free the buffer */
0434     ishtp_cl_io_rb_recycle(rb_in_proc);
0435     rb_in_proc = NULL;
0436 
0437     /* Wake the calling thread */
0438     wake_up_interruptible(&client_data->response.wait_queue);
0439 }
0440 
0441 /**
0442  * loader_cl_event_cb() - bus driver callback for incoming message
0443  * @cl_device:      Pointer to the ishtp client device for which this
0444  *          message is targeted
0445  *
0446  * Remove the packet from the list and process the message by calling
0447  * process_recv
0448  */
0449 static void loader_cl_event_cb(struct ishtp_cl_device *cl_device)
0450 {
0451     struct ishtp_cl_rb *rb_in_proc;
0452     struct ishtp_cl *loader_ishtp_cl = ishtp_get_drvdata(cl_device);
0453 
0454     while ((rb_in_proc = ishtp_cl_rx_get_rb(loader_ishtp_cl)) != NULL) {
0455         /* Process the data packet from firmware */
0456         process_recv(loader_ishtp_cl, rb_in_proc);
0457     }
0458 }
0459 
0460 /**
0461  * ish_query_loader_prop() -  Query ISH Shim firmware loader
0462  * @client_data:    Client data instance
0463  * @fw:         Pointer to firmware data struct in host memory
0464  * @fw_info:        Loader firmware properties
0465  *
0466  * This function queries the ISH Shim firmware loader for capabilities.
0467  *
0468  * Return: 0 for success, negative error code for failure.
0469  */
0470 static int ish_query_loader_prop(struct ishtp_cl_data *client_data,
0471                  const struct firmware *fw,
0472                  struct shim_fw_info *fw_info)
0473 {
0474     int rv;
0475     struct loader_xfer_query ldr_xfer_query;
0476     struct loader_xfer_query_response ldr_xfer_query_resp;
0477 
0478     memset(&ldr_xfer_query, 0, sizeof(ldr_xfer_query));
0479     ldr_xfer_query.hdr.command = LOADER_CMD_XFER_QUERY;
0480     ldr_xfer_query.image_size = fw->size;
0481     rv = loader_cl_send(client_data,
0482                 (u8 *)&ldr_xfer_query,
0483                 sizeof(ldr_xfer_query),
0484                 (u8 *)&ldr_xfer_query_resp,
0485                 sizeof(ldr_xfer_query_resp));
0486     if (rv < 0) {
0487         client_data->flag_retry = true;
0488         *fw_info = (struct shim_fw_info){};
0489         return rv;
0490     }
0491 
0492     /* On success, the return value is the received buffer size */
0493     if (rv != sizeof(struct loader_xfer_query_response)) {
0494         dev_err(cl_data_to_dev(client_data),
0495             "data size %d is not equal to size of loader_xfer_query_response %zu\n",
0496             rv, sizeof(struct loader_xfer_query_response));
0497         client_data->flag_retry = true;
0498         *fw_info = (struct shim_fw_info){};
0499         return -EMSGSIZE;
0500     }
0501 
0502     /* Save fw_info for use outside this function */
0503     *fw_info = ldr_xfer_query_resp.fw_info;
0504 
0505     /* Loader firmware properties */
0506     dev_dbg(cl_data_to_dev(client_data),
0507         "ish_fw_version: major=%d minor=%d hotfix=%d build=%d protocol_version=0x%x loader_version=%d\n",
0508         fw_info->ish_fw_version.major,
0509         fw_info->ish_fw_version.minor,
0510         fw_info->ish_fw_version.hotfix,
0511         fw_info->ish_fw_version.build,
0512         fw_info->protocol_version,
0513         fw_info->ldr_version.value);
0514 
0515     dev_dbg(cl_data_to_dev(client_data),
0516         "loader_capability: max_fw_image_size=0x%x xfer_mode=%d max_dma_buf_size=0x%x dma_buf_size_limit=0x%x\n",
0517         fw_info->ldr_capability.max_fw_image_size,
0518         fw_info->ldr_capability.xfer_mode,
0519         fw_info->ldr_capability.max_dma_buf_size,
0520         dma_buf_size_limit);
0521 
0522     /* Sanity checks */
0523     if (fw_info->ldr_capability.max_fw_image_size < fw->size) {
0524         dev_err(cl_data_to_dev(client_data),
0525             "ISH firmware size %zu is greater than Shim firmware loader max supported %d\n",
0526             fw->size,
0527             fw_info->ldr_capability.max_fw_image_size);
0528         return -ENOSPC;
0529     }
0530 
0531     /* For DMA the buffer size should be multiple of cacheline size */
0532     if ((fw_info->ldr_capability.xfer_mode & LOADER_XFER_MODE_DIRECT_DMA) &&
0533         (fw_info->ldr_capability.max_dma_buf_size % L1_CACHE_BYTES)) {
0534         dev_err(cl_data_to_dev(client_data),
0535             "Shim firmware loader buffer size %d should be multiple of cacheline\n",
0536             fw_info->ldr_capability.max_dma_buf_size);
0537         return -EINVAL;
0538     }
0539 
0540     return 0;
0541 }
0542 
0543 /**
0544  * ish_fw_xfer_ishtp() - Loads ISH firmware using ishtp interface
0545  * @client_data:    Client data instance
0546  * @fw:         Pointer to firmware data struct in host memory
0547  *
0548  * This function uses ISH-TP to transfer ISH firmware from host to
0549  * ISH SRAM. Lower layers may use IPC or DMA depending on firmware
0550  * support.
0551  *
0552  * Return: 0 for success, negative error code for failure.
0553  */
0554 static int ish_fw_xfer_ishtp(struct ishtp_cl_data *client_data,
0555                  const struct firmware *fw)
0556 {
0557     int rv;
0558     u32 fragment_offset, fragment_size, payload_max_size;
0559     struct loader_xfer_ipc_fragment *ldr_xfer_ipc_frag;
0560     struct loader_msg_hdr ldr_xfer_ipc_ack;
0561 
0562     payload_max_size =
0563         LOADER_SHIM_IPC_BUF_SIZE - IPC_FRAGMENT_DATA_PREAMBLE;
0564 
0565     ldr_xfer_ipc_frag = kzalloc(LOADER_SHIM_IPC_BUF_SIZE, GFP_KERNEL);
0566     if (!ldr_xfer_ipc_frag) {
0567         client_data->flag_retry = true;
0568         return -ENOMEM;
0569     }
0570 
0571     ldr_xfer_ipc_frag->fragment.hdr.command = LOADER_CMD_XFER_FRAGMENT;
0572     ldr_xfer_ipc_frag->fragment.xfer_mode = LOADER_XFER_MODE_ISHTP;
0573 
0574     /* Break the firmware image into fragments and send as ISH-TP payload */
0575     fragment_offset = 0;
0576     while (fragment_offset < fw->size) {
0577         if (fragment_offset + payload_max_size < fw->size) {
0578             fragment_size = payload_max_size;
0579             ldr_xfer_ipc_frag->fragment.is_last = 0;
0580         } else {
0581             fragment_size = fw->size - fragment_offset;
0582             ldr_xfer_ipc_frag->fragment.is_last = 1;
0583         }
0584 
0585         ldr_xfer_ipc_frag->fragment.offset = fragment_offset;
0586         ldr_xfer_ipc_frag->fragment.size = fragment_size;
0587         memcpy(ldr_xfer_ipc_frag->data,
0588                &fw->data[fragment_offset],
0589                fragment_size);
0590 
0591         dev_dbg(cl_data_to_dev(client_data),
0592             "xfer_mode=ipc offset=0x%08x size=0x%08x is_last=%d\n",
0593             ldr_xfer_ipc_frag->fragment.offset,
0594             ldr_xfer_ipc_frag->fragment.size,
0595             ldr_xfer_ipc_frag->fragment.is_last);
0596 
0597         rv = loader_cl_send(client_data,
0598                     (u8 *)ldr_xfer_ipc_frag,
0599                     IPC_FRAGMENT_DATA_PREAMBLE + fragment_size,
0600                     (u8 *)&ldr_xfer_ipc_ack,
0601                     sizeof(ldr_xfer_ipc_ack));
0602         if (rv < 0) {
0603             client_data->flag_retry = true;
0604             goto end_err_resp_buf_release;
0605         }
0606 
0607         fragment_offset += fragment_size;
0608     }
0609 
0610     kfree(ldr_xfer_ipc_frag);
0611     return 0;
0612 
0613 end_err_resp_buf_release:
0614     /* Free ISH buffer if not done already, in error case */
0615     kfree(ldr_xfer_ipc_frag);
0616     return rv;
0617 }
0618 
0619 /**
0620  * ish_fw_xfer_direct_dma() - Loads ISH firmware using direct dma
0621  * @client_data:    Client data instance
0622  * @fw:         Pointer to firmware data struct in host memory
0623  * @fw_info:        Loader firmware properties
0624  *
0625  * Host firmware load is a unique case where we need to download
0626  * a large firmware image (200+ Kb). This function implements
0627  * direct DMA transfer in kernel and ISH firmware. This allows
0628  * us to overcome the ISH-TP 4 Kb limit, and allows us to DMA
0629  * directly to ISH UMA at location of choice.
0630  * Function depends on corresponding support in ISH firmware.
0631  *
0632  * Return: 0 for success, negative error code for failure.
0633  */
0634 static int ish_fw_xfer_direct_dma(struct ishtp_cl_data *client_data,
0635                   const struct firmware *fw,
0636                   const struct shim_fw_info fw_info)
0637 {
0638     int rv;
0639     void *dma_buf;
0640     dma_addr_t dma_buf_phy;
0641     u32 fragment_offset, fragment_size, payload_max_size;
0642     struct loader_msg_hdr ldr_xfer_dma_frag_ack;
0643     struct loader_xfer_dma_fragment ldr_xfer_dma_frag;
0644     struct device *devc = ishtp_get_pci_device(client_data->cl_device);
0645     u32 shim_fw_buf_size =
0646         fw_info.ldr_capability.max_dma_buf_size;
0647 
0648     /*
0649      * payload_max_size should be set to minimum of
0650      *  (1) Size of firmware to be loaded,
0651      *  (2) Max DMA buffer size supported by Shim firmware,
0652      *  (3) DMA buffer size limit set by boot_param dma_buf_size_limit.
0653      */
0654     payload_max_size = min3(fw->size,
0655                 (size_t)shim_fw_buf_size,
0656                 (size_t)dma_buf_size_limit);
0657 
0658     /*
0659      * Buffer size should be multiple of cacheline size
0660      * if it's not, select the previous cacheline boundary.
0661      */
0662     payload_max_size &= ~(L1_CACHE_BYTES - 1);
0663 
0664     dma_buf = dma_alloc_coherent(devc, payload_max_size, &dma_buf_phy, GFP_KERNEL);
0665     if (!dma_buf) {
0666         client_data->flag_retry = true;
0667         return -ENOMEM;
0668     }
0669 
0670     ldr_xfer_dma_frag.fragment.hdr.command = LOADER_CMD_XFER_FRAGMENT;
0671     ldr_xfer_dma_frag.fragment.xfer_mode = LOADER_XFER_MODE_DIRECT_DMA;
0672     ldr_xfer_dma_frag.ddr_phys_addr = (u64)dma_buf_phy;
0673 
0674     /* Send the firmware image in chucks of payload_max_size */
0675     fragment_offset = 0;
0676     while (fragment_offset < fw->size) {
0677         if (fragment_offset + payload_max_size < fw->size) {
0678             fragment_size = payload_max_size;
0679             ldr_xfer_dma_frag.fragment.is_last = 0;
0680         } else {
0681             fragment_size = fw->size - fragment_offset;
0682             ldr_xfer_dma_frag.fragment.is_last = 1;
0683         }
0684 
0685         ldr_xfer_dma_frag.fragment.offset = fragment_offset;
0686         ldr_xfer_dma_frag.fragment.size = fragment_size;
0687         memcpy(dma_buf, &fw->data[fragment_offset], fragment_size);
0688 
0689         /* Flush cache to be sure the data is in main memory. */
0690         clflush_cache_range(dma_buf, payload_max_size);
0691 
0692         dev_dbg(cl_data_to_dev(client_data),
0693             "xfer_mode=dma offset=0x%08x size=0x%x is_last=%d ddr_phys_addr=0x%016llx\n",
0694             ldr_xfer_dma_frag.fragment.offset,
0695             ldr_xfer_dma_frag.fragment.size,
0696             ldr_xfer_dma_frag.fragment.is_last,
0697             ldr_xfer_dma_frag.ddr_phys_addr);
0698 
0699         rv = loader_cl_send(client_data,
0700                     (u8 *)&ldr_xfer_dma_frag,
0701                     sizeof(ldr_xfer_dma_frag),
0702                     (u8 *)&ldr_xfer_dma_frag_ack,
0703                     sizeof(ldr_xfer_dma_frag_ack));
0704         if (rv < 0) {
0705             client_data->flag_retry = true;
0706             goto end_err_resp_buf_release;
0707         }
0708 
0709         fragment_offset += fragment_size;
0710     }
0711 
0712 end_err_resp_buf_release:
0713     dma_free_coherent(devc, payload_max_size, dma_buf, dma_buf_phy);
0714     return rv;
0715 }
0716 
0717 /**
0718  * ish_fw_start() - Start executing ISH main firmware
0719  * @client_data:    client data instance
0720  *
0721  * This function sends message to Shim firmware loader to start
0722  * the execution of ISH main firmware.
0723  *
0724  * Return: 0 for success, negative error code for failure.
0725  */
0726 static int ish_fw_start(struct ishtp_cl_data *client_data)
0727 {
0728     struct loader_start ldr_start;
0729     struct loader_msg_hdr ldr_start_ack;
0730 
0731     memset(&ldr_start, 0, sizeof(ldr_start));
0732     ldr_start.hdr.command = LOADER_CMD_START;
0733     return loader_cl_send(client_data,
0734                 (u8 *)&ldr_start,
0735                 sizeof(ldr_start),
0736                 (u8 *)&ldr_start_ack,
0737                 sizeof(ldr_start_ack));
0738 }
0739 
0740 /**
0741  * load_fw_from_host() - Loads ISH firmware from host
0742  * @client_data:    Client data instance
0743  *
0744  * This function loads the ISH firmware to ISH SRAM and starts execution
0745  *
0746  * Return: 0 for success, negative error code for failure.
0747  */
0748 static int load_fw_from_host(struct ishtp_cl_data *client_data)
0749 {
0750     int rv;
0751     u32 xfer_mode;
0752     char *filename;
0753     const struct firmware *fw;
0754     struct shim_fw_info fw_info;
0755     struct ishtp_cl *loader_ishtp_cl = client_data->loader_ishtp_cl;
0756 
0757     client_data->flag_retry = false;
0758 
0759     filename = kzalloc(FILENAME_SIZE, GFP_KERNEL);
0760     if (!filename) {
0761         client_data->flag_retry = true;
0762         rv = -ENOMEM;
0763         goto end_error;
0764     }
0765 
0766     /* Get filename of the ISH firmware to be loaded */
0767     rv = get_firmware_variant(client_data, filename);
0768     if (rv < 0)
0769         goto end_err_filename_buf_release;
0770 
0771     rv = request_firmware(&fw, filename, cl_data_to_dev(client_data));
0772     if (rv < 0)
0773         goto end_err_filename_buf_release;
0774 
0775     /* Step 1: Query Shim firmware loader properties */
0776 
0777     rv = ish_query_loader_prop(client_data, fw, &fw_info);
0778     if (rv < 0)
0779         goto end_err_fw_release;
0780 
0781     /* Step 2: Send the main firmware image to be loaded, to ISH SRAM */
0782 
0783     xfer_mode = fw_info.ldr_capability.xfer_mode;
0784     if (xfer_mode & LOADER_XFER_MODE_DIRECT_DMA) {
0785         rv = ish_fw_xfer_direct_dma(client_data, fw, fw_info);
0786     } else if (xfer_mode & LOADER_XFER_MODE_ISHTP) {
0787         rv = ish_fw_xfer_ishtp(client_data, fw);
0788     } else {
0789         dev_err(cl_data_to_dev(client_data),
0790             "No transfer mode selected in firmware\n");
0791         rv = -EINVAL;
0792     }
0793     if (rv < 0)
0794         goto end_err_fw_release;
0795 
0796     /* Step 3: Start ISH main firmware exeuction */
0797 
0798     rv = ish_fw_start(client_data);
0799     if (rv < 0)
0800         goto end_err_fw_release;
0801 
0802     release_firmware(fw);
0803     dev_info(cl_data_to_dev(client_data), "ISH firmware %s loaded\n",
0804          filename);
0805     kfree(filename);
0806     return 0;
0807 
0808 end_err_fw_release:
0809     release_firmware(fw);
0810 end_err_filename_buf_release:
0811     kfree(filename);
0812 end_error:
0813     /* Keep a count of retries, and give up after 3 attempts */
0814     if (client_data->flag_retry &&
0815         client_data->retry_count++ < MAX_LOAD_ATTEMPTS) {
0816         dev_warn(cl_data_to_dev(client_data),
0817              "ISH host firmware load failed %d. Resetting ISH, and trying again..\n",
0818              rv);
0819         ish_hw_reset(ishtp_get_ishtp_device(loader_ishtp_cl));
0820     } else {
0821         dev_err(cl_data_to_dev(client_data),
0822             "ISH host firmware load failed %d\n", rv);
0823     }
0824     return rv;
0825 }
0826 
0827 static void load_fw_from_host_handler(struct work_struct *work)
0828 {
0829     struct ishtp_cl_data *client_data;
0830 
0831     client_data = container_of(work, struct ishtp_cl_data,
0832                    work_fw_load);
0833     load_fw_from_host(client_data);
0834 }
0835 
0836 /**
0837  * loader_init() -  Init function for ISH-TP client
0838  * @loader_ishtp_cl:    ISH-TP client instance
0839  * @reset:      true if called for init after reset
0840  *
0841  * Return: 0 for success, negative error code for failure
0842  */
0843 static int loader_init(struct ishtp_cl *loader_ishtp_cl, int reset)
0844 {
0845     int rv;
0846     struct ishtp_fw_client *fw_client;
0847     struct ishtp_cl_data *client_data =
0848         ishtp_get_client_data(loader_ishtp_cl);
0849 
0850     dev_dbg(cl_data_to_dev(client_data), "reset flag: %d\n", reset);
0851 
0852     rv = ishtp_cl_link(loader_ishtp_cl);
0853     if (rv < 0) {
0854         dev_err(cl_data_to_dev(client_data), "ishtp_cl_link failed\n");
0855         return rv;
0856     }
0857 
0858     /* Connect to firmware client */
0859     ishtp_set_tx_ring_size(loader_ishtp_cl, LOADER_CL_TX_RING_SIZE);
0860     ishtp_set_rx_ring_size(loader_ishtp_cl, LOADER_CL_RX_RING_SIZE);
0861 
0862     fw_client =
0863         ishtp_fw_cl_get_client(ishtp_get_ishtp_device(loader_ishtp_cl),
0864                        &loader_ishtp_id_table[0].guid);
0865     if (!fw_client) {
0866         dev_err(cl_data_to_dev(client_data),
0867             "ISH client uuid not found\n");
0868         rv = -ENOENT;
0869         goto err_cl_unlink;
0870     }
0871 
0872     ishtp_cl_set_fw_client_id(loader_ishtp_cl,
0873                   ishtp_get_fw_client_id(fw_client));
0874     ishtp_set_connection_state(loader_ishtp_cl, ISHTP_CL_CONNECTING);
0875 
0876     rv = ishtp_cl_connect(loader_ishtp_cl);
0877     if (rv < 0) {
0878         dev_err(cl_data_to_dev(client_data), "Client connect fail\n");
0879         goto err_cl_unlink;
0880     }
0881 
0882     dev_dbg(cl_data_to_dev(client_data), "Client connected\n");
0883 
0884     ishtp_register_event_cb(client_data->cl_device, loader_cl_event_cb);
0885 
0886     return 0;
0887 
0888 err_cl_unlink:
0889     ishtp_cl_unlink(loader_ishtp_cl);
0890     return rv;
0891 }
0892 
0893 static void loader_deinit(struct ishtp_cl *loader_ishtp_cl)
0894 {
0895     ishtp_set_connection_state(loader_ishtp_cl, ISHTP_CL_DISCONNECTING);
0896     ishtp_cl_disconnect(loader_ishtp_cl);
0897     ishtp_cl_unlink(loader_ishtp_cl);
0898     ishtp_cl_flush_queues(loader_ishtp_cl);
0899 
0900     /* Disband and free all Tx and Rx client-level rings */
0901     ishtp_cl_free(loader_ishtp_cl);
0902 }
0903 
0904 static void reset_handler(struct work_struct *work)
0905 {
0906     int rv;
0907     struct ishtp_cl_data *client_data;
0908     struct ishtp_cl *loader_ishtp_cl;
0909     struct ishtp_cl_device *cl_device;
0910 
0911     client_data = container_of(work, struct ishtp_cl_data,
0912                    work_ishtp_reset);
0913 
0914     loader_ishtp_cl = client_data->loader_ishtp_cl;
0915     cl_device = client_data->cl_device;
0916 
0917     /* Unlink, flush queues & start again */
0918     ishtp_cl_unlink(loader_ishtp_cl);
0919     ishtp_cl_flush_queues(loader_ishtp_cl);
0920     ishtp_cl_free(loader_ishtp_cl);
0921 
0922     loader_ishtp_cl = ishtp_cl_allocate(cl_device);
0923     if (!loader_ishtp_cl)
0924         return;
0925 
0926     ishtp_set_drvdata(cl_device, loader_ishtp_cl);
0927     ishtp_set_client_data(loader_ishtp_cl, client_data);
0928     client_data->loader_ishtp_cl = loader_ishtp_cl;
0929     client_data->cl_device = cl_device;
0930 
0931     rv = loader_init(loader_ishtp_cl, 1);
0932     if (rv < 0) {
0933         dev_err(ishtp_device(cl_device), "Reset Failed\n");
0934         return;
0935     }
0936 
0937     /* ISH firmware loading from host */
0938     load_fw_from_host(client_data);
0939 }
0940 
0941 /**
0942  * loader_ishtp_cl_probe() - ISH-TP client driver probe
0943  * @cl_device:      ISH-TP client device instance
0944  *
0945  * This function gets called on device create on ISH-TP bus
0946  *
0947  * Return: 0 for success, negative error code for failure
0948  */
0949 static int loader_ishtp_cl_probe(struct ishtp_cl_device *cl_device)
0950 {
0951     struct ishtp_cl *loader_ishtp_cl;
0952     struct ishtp_cl_data *client_data;
0953     int rv;
0954 
0955     client_data = devm_kzalloc(ishtp_device(cl_device),
0956                    sizeof(*client_data),
0957                    GFP_KERNEL);
0958     if (!client_data)
0959         return -ENOMEM;
0960 
0961     loader_ishtp_cl = ishtp_cl_allocate(cl_device);
0962     if (!loader_ishtp_cl)
0963         return -ENOMEM;
0964 
0965     ishtp_set_drvdata(cl_device, loader_ishtp_cl);
0966     ishtp_set_client_data(loader_ishtp_cl, client_data);
0967     client_data->loader_ishtp_cl = loader_ishtp_cl;
0968     client_data->cl_device = cl_device;
0969 
0970     init_waitqueue_head(&client_data->response.wait_queue);
0971 
0972     INIT_WORK(&client_data->work_ishtp_reset,
0973           reset_handler);
0974     INIT_WORK(&client_data->work_fw_load,
0975           load_fw_from_host_handler);
0976 
0977     rv = loader_init(loader_ishtp_cl, 0);
0978     if (rv < 0) {
0979         ishtp_cl_free(loader_ishtp_cl);
0980         return rv;
0981     }
0982     ishtp_get_device(cl_device);
0983 
0984     client_data->retry_count = 0;
0985 
0986     /* ISH firmware loading from host */
0987     schedule_work(&client_data->work_fw_load);
0988 
0989     return 0;
0990 }
0991 
0992 /**
0993  * loader_ishtp_cl_remove() - ISH-TP client driver remove
0994  * @cl_device:      ISH-TP client device instance
0995  *
0996  * This function gets called on device remove on ISH-TP bus
0997  *
0998  * Return: 0
0999  */
1000 static void loader_ishtp_cl_remove(struct ishtp_cl_device *cl_device)
1001 {
1002     struct ishtp_cl_data *client_data;
1003     struct ishtp_cl *loader_ishtp_cl = ishtp_get_drvdata(cl_device);
1004 
1005     client_data = ishtp_get_client_data(loader_ishtp_cl);
1006 
1007     /*
1008      * The sequence of the following two cancel_work_sync() is
1009      * important. The work_fw_load can in turn schedue
1010      * work_ishtp_reset, so first cancel work_fw_load then
1011      * cancel work_ishtp_reset.
1012      */
1013     cancel_work_sync(&client_data->work_fw_load);
1014     cancel_work_sync(&client_data->work_ishtp_reset);
1015     loader_deinit(loader_ishtp_cl);
1016     ishtp_put_device(cl_device);
1017 }
1018 
1019 /**
1020  * loader_ishtp_cl_reset() - ISH-TP client driver reset
1021  * @cl_device:      ISH-TP client device instance
1022  *
1023  * This function gets called on device reset on ISH-TP bus
1024  *
1025  * Return: 0
1026  */
1027 static int loader_ishtp_cl_reset(struct ishtp_cl_device *cl_device)
1028 {
1029     struct ishtp_cl_data *client_data;
1030     struct ishtp_cl *loader_ishtp_cl = ishtp_get_drvdata(cl_device);
1031 
1032     client_data = ishtp_get_client_data(loader_ishtp_cl);
1033 
1034     schedule_work(&client_data->work_ishtp_reset);
1035 
1036     return 0;
1037 }
1038 
1039 static struct ishtp_cl_driver   loader_ishtp_cl_driver = {
1040     .name = "ish-loader",
1041     .id = loader_ishtp_id_table,
1042     .probe = loader_ishtp_cl_probe,
1043     .remove = loader_ishtp_cl_remove,
1044     .reset = loader_ishtp_cl_reset,
1045 };
1046 
1047 static int __init ish_loader_init(void)
1048 {
1049     return ishtp_cl_driver_register(&loader_ishtp_cl_driver, THIS_MODULE);
1050 }
1051 
1052 static void __exit ish_loader_exit(void)
1053 {
1054     ishtp_cl_driver_unregister(&loader_ishtp_cl_driver);
1055 }
1056 
1057 late_initcall(ish_loader_init);
1058 module_exit(ish_loader_exit);
1059 
1060 module_param(dma_buf_size_limit, int, 0644);
1061 MODULE_PARM_DESC(dma_buf_size_limit, "Limit the DMA buf size to this value in bytes");
1062 
1063 MODULE_DESCRIPTION("ISH ISH-TP Host firmware Loader Client Driver");
1064 MODULE_AUTHOR("Rushikesh S Kadam <rushikesh.s.kadam@intel.com>");
1065 
1066 MODULE_LICENSE("GPL v2");