1 // SPDX-License-Identifier: GPL-2.0
2 // ISHTP interface for ChromeOS Embedded Controller
4 // Copyright (c) 2019, Intel Corporation.
6 // ISHTP client driver for talking to the Chrome OS EC firmware running
7 // on Intel Integrated Sensor Hub (ISH) using the ISH Transport protocol
10 #include <linux/delay.h>
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/platform_data/cros_ec_commands.h>
14 #include <linux/platform_data/cros_ec_proto.h>
15 #include <linux/intel-ish-client-if.h>
18 * ISH TX/RX ring buffer pool size
20 * The AP->ISH messages and corresponding ISH->AP responses are
21 * serialized. We need 1 TX and 1 RX buffer for these.
23 * The MKBP ISH->AP events are serialized. We need one additional RX
26 #define CROS_ISH_CL_TX_RING_SIZE 8
27 #define CROS_ISH_CL_RX_RING_SIZE 8
29 /* ISH CrOS EC Host Commands */
30 enum cros_ec_ish_channel {
31 CROS_EC_COMMAND = 1, /* AP->ISH message */
32 CROS_MKBP_EVENT = 2, /* ISH->AP events */
36 * ISH firmware timeout for 1 message send failure is 1Hz, and the
37 * firmware will retry 2 times, so 3Hz is used for timeout.
39 #define ISHTP_SEND_TIMEOUT (3 * HZ)
41 /* ISH Transport CrOS EC ISH client unique GUID */
42 static const guid_t cros_ish_guid =
43 GUID_INIT(0x7b7154d0, 0x56f4, 0x4bdc,
44 0xb0, 0xd8, 0x9e, 0x7c, 0xda, 0xe0, 0xd6, 0xa0);
52 struct cros_ish_out_msg {
54 struct ec_host_request ec_request;
57 struct cros_ish_in_msg {
59 struct ec_host_response ec_response;
62 #define IN_MSG_EC_RESPONSE_PREAMBLE \
63 offsetof(struct cros_ish_in_msg, ec_response)
65 #define OUT_MSG_EC_REQUEST_PREAMBLE \
66 offsetof(struct cros_ish_out_msg, ec_request)
68 #define cl_data_to_dev(client_data) ishtp_device((client_data)->cl_device)
71 * The Read-Write Semaphore is used to prevent message TX or RX while
72 * the ishtp client is being initialized or undergoing reset.
74 * The readers are the kernel function calls responsible for IA->ISH
75 * and ISH->AP messaging.
77 * The writers are .reset() and .probe() function.
79 DECLARE_RWSEM(init_lock);
82 * struct response_info - Encapsulate firmware response related
83 * information for passing between function ish_send() and
84 * process_recv() callback.
86 * @data: Copy the data received from firmware here.
87 * @max_size: Max size allocated for the @data buffer. If the received
88 * data exceeds this value, we log an error.
89 * @size: Actual size of data received from firmware.
90 * @error: 0 for success, negative error code for a failure in process_recv().
91 * @received: Set to true on receiving a valid firmware response to host command
92 * @wait_queue: Wait queue for host to wait for firmware response.
94 struct response_info {
100 wait_queue_head_t wait_queue;
104 * struct ishtp_cl_data - Encapsulate per ISH TP Client.
106 * @cros_ish_cl: ISHTP firmware client instance.
107 * @cl_device: ISHTP client device instance.
108 * @response: Response info passing between ish_send() and process_recv().
109 * @work_ishtp_reset: Work queue reset handling.
110 * @work_ec_evt: Work queue for EC events.
111 * @ec_dev: CrOS EC MFD device.
113 * This structure is used to store per client data.
115 struct ishtp_cl_data {
116 struct ishtp_cl *cros_ish_cl;
117 struct ishtp_cl_device *cl_device;
120 * Used for passing firmware response information between
121 * ish_send() and process_recv() callback.
123 struct response_info response;
125 struct work_struct work_ishtp_reset;
126 struct work_struct work_ec_evt;
127 struct cros_ec_device *ec_dev;
131 * ish_evt_handler - ISH to AP event handler
134 static void ish_evt_handler(struct work_struct *work)
136 struct ishtp_cl_data *client_data =
137 container_of(work, struct ishtp_cl_data, work_ec_evt);
138 struct cros_ec_device *ec_dev = client_data->ec_dev;
139 bool ec_has_more_events;
142 ec_has_more_events = cros_ec_handle_event(ec_dev);
143 } while (ec_has_more_events);
147 * ish_send() - Send message from host to firmware
149 * @client_data: Client data instance
150 * @out_msg: Message buffer to be sent to firmware
151 * @out_size: Size of out going message
152 * @in_msg: Message buffer where the incoming data is copied. This buffer
153 * is allocated by calling
154 * @in_size: Max size of incoming message
156 * Return: Number of bytes copied in the in_msg on success, negative
157 * error code on failure.
159 static int ish_send(struct ishtp_cl_data *client_data,
160 u8 *out_msg, size_t out_size,
161 u8 *in_msg, size_t in_size)
164 struct header *out_hdr = (struct header *)out_msg;
165 struct ishtp_cl *cros_ish_cl = client_data->cros_ish_cl;
167 dev_dbg(cl_data_to_dev(client_data),
168 "%s: channel=%02u status=%02u\n",
169 __func__, out_hdr->channel, out_hdr->status);
171 /* Setup for incoming response */
172 client_data->response.data = in_msg;
173 client_data->response.max_size = in_size;
174 client_data->response.error = 0;
175 client_data->response.received = false;
177 rv = ishtp_cl_send(cros_ish_cl, out_msg, out_size);
179 dev_err(cl_data_to_dev(client_data),
180 "ishtp_cl_send error %d\n", rv);
184 wait_event_interruptible_timeout(client_data->response.wait_queue,
185 client_data->response.received,
187 if (!client_data->response.received) {
188 dev_err(cl_data_to_dev(client_data),
189 "Timed out for response to host message\n");
193 if (client_data->response.error < 0)
194 return client_data->response.error;
196 return client_data->response.size;
200 * process_recv() - Received and parse incoming packet
201 * @cros_ish_cl: Client instance to get stats
202 * @rb_in_proc: Host interface message buffer
203 * @timestamp: Timestamp of when parent callback started
205 * Parse the incoming packet. If it is a response packet then it will
206 * update per instance flags and wake up the caller waiting to for the
207 * response. If it is an event packet then it will schedule event work.
209 static void process_recv(struct ishtp_cl *cros_ish_cl,
210 struct ishtp_cl_rb *rb_in_proc, ktime_t timestamp)
212 size_t data_len = rb_in_proc->buf_idx;
213 struct ishtp_cl_data *client_data =
214 ishtp_get_client_data(cros_ish_cl);
215 struct device *dev = cl_data_to_dev(client_data);
216 struct cros_ish_in_msg *in_msg =
217 (struct cros_ish_in_msg *)rb_in_proc->buffer.data;
219 /* Proceed only if reset or init is not in progress */
220 if (!down_read_trylock(&init_lock)) {
221 /* Free the buffer */
222 ishtp_cl_io_rb_recycle(rb_in_proc);
224 "Host is not ready to receive incoming messages\n");
229 * All firmware messages contain a header. Check the buffer size
230 * before accessing elements inside.
232 if (!rb_in_proc->buffer.data) {
233 dev_warn(dev, "rb_in_proc->buffer.data returned null");
234 client_data->response.error = -EBADMSG;
238 if (data_len < sizeof(struct header)) {
239 dev_err(dev, "data size %zu is less than header %zu\n",
240 data_len, sizeof(struct header));
241 client_data->response.error = -EMSGSIZE;
245 dev_dbg(dev, "channel=%02u status=%02u\n",
246 in_msg->hdr.channel, in_msg->hdr.status);
248 switch (in_msg->hdr.channel) {
249 case CROS_EC_COMMAND:
251 if (!client_data->response.data) {
253 "Receiving buffer is null. Should be allocated by calling function\n");
254 client_data->response.error = -EINVAL;
258 if (client_data->response.received) {
260 "Previous firmware message not yet processed\n");
261 client_data->response.error = -EINVAL;
265 if (data_len > client_data->response.max_size) {
267 "Received buffer size %zu is larger than allocated buffer %zu\n",
268 data_len, client_data->response.max_size);
269 client_data->response.error = -EMSGSIZE;
273 if (in_msg->hdr.status) {
274 dev_err(dev, "firmware returned status %d\n",
276 client_data->response.error = -EIO;
280 /* Update the actual received buffer size */
281 client_data->response.size = data_len;
284 * Copy the buffer received in firmware response for the
287 memcpy(client_data->response.data,
288 rb_in_proc->buffer.data, data_len);
290 /* Set flag before waking up the caller */
291 client_data->response.received = true;
293 /* Wake the calling thread */
294 wake_up_interruptible(&client_data->response.wait_queue);
298 case CROS_MKBP_EVENT:
300 * Set timestamp from beginning of function since we actually
301 * got an incoming MKBP event
303 client_data->ec_dev->last_event_time = timestamp;
304 /* The event system doesn't send any data in buffer */
305 schedule_work(&client_data->work_ec_evt);
310 dev_err(dev, "Invalid channel=%02d\n", in_msg->hdr.channel);
314 /* Free the buffer */
315 ishtp_cl_io_rb_recycle(rb_in_proc);
321 * ish_event_cb() - bus driver callback for incoming message
322 * @cl_device: ISHTP client device for which this message is targeted.
324 * Remove the packet from the list and process the message by calling
327 static void ish_event_cb(struct ishtp_cl_device *cl_device)
329 struct ishtp_cl_rb *rb_in_proc;
330 struct ishtp_cl *cros_ish_cl = ishtp_get_drvdata(cl_device);
334 * Take timestamp as close to hardware interrupt as possible for sensor
337 timestamp = cros_ec_get_time_ns();
339 while ((rb_in_proc = ishtp_cl_rx_get_rb(cros_ish_cl)) != NULL) {
340 /* Decide what to do with received data */
341 process_recv(cros_ish_cl, rb_in_proc, timestamp);
346 * cros_ish_init() - Init function for ISHTP client
347 * @cros_ish_cl: ISHTP client instance
349 * This function complete the initializtion of the client.
351 * Return: 0 for success, negative error code for failure.
353 static int cros_ish_init(struct ishtp_cl *cros_ish_cl)
356 struct ishtp_device *dev;
357 struct ishtp_fw_client *fw_client;
358 struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
360 rv = ishtp_cl_link(cros_ish_cl);
362 dev_err(cl_data_to_dev(client_data),
363 "ishtp_cl_link failed\n");
367 dev = ishtp_get_ishtp_device(cros_ish_cl);
369 /* Connect to firmware client */
370 ishtp_set_tx_ring_size(cros_ish_cl, CROS_ISH_CL_TX_RING_SIZE);
371 ishtp_set_rx_ring_size(cros_ish_cl, CROS_ISH_CL_RX_RING_SIZE);
373 fw_client = ishtp_fw_cl_get_client(dev, &cros_ish_guid);
375 dev_err(cl_data_to_dev(client_data),
376 "ish client uuid not found\n");
381 ishtp_cl_set_fw_client_id(cros_ish_cl,
382 ishtp_get_fw_client_id(fw_client));
383 ishtp_set_connection_state(cros_ish_cl, ISHTP_CL_CONNECTING);
385 rv = ishtp_cl_connect(cros_ish_cl);
387 dev_err(cl_data_to_dev(client_data),
388 "client connect fail\n");
392 ishtp_register_event_cb(client_data->cl_device, ish_event_cb);
396 ishtp_cl_unlink(cros_ish_cl);
401 * cros_ish_deinit() - Deinit function for ISHTP client
402 * @cros_ish_cl: ISHTP client instance
404 * Unlink and free cros_ec client
406 static void cros_ish_deinit(struct ishtp_cl *cros_ish_cl)
408 ishtp_set_connection_state(cros_ish_cl, ISHTP_CL_DISCONNECTING);
409 ishtp_cl_disconnect(cros_ish_cl);
410 ishtp_cl_unlink(cros_ish_cl);
411 ishtp_cl_flush_queues(cros_ish_cl);
413 /* Disband and free all Tx and Rx client-level rings */
414 ishtp_cl_free(cros_ish_cl);
418 * prepare_cros_ec_rx() - Check & prepare receive buffer
419 * @ec_dev: CrOS EC MFD device.
420 * @in_msg: Incoming message buffer
421 * @msg: cros_ec command used to send & receive data
423 * Return: 0 for success, negative error code for failure.
425 * Check the received buffer. Convert to cros_ec_command format.
427 static int prepare_cros_ec_rx(struct cros_ec_device *ec_dev,
428 const struct cros_ish_in_msg *in_msg,
429 struct cros_ec_command *msg)
434 /* Check response error code */
435 msg->result = in_msg->ec_response.result;
436 rv = cros_ec_check_result(ec_dev, msg);
440 if (in_msg->ec_response.data_len > msg->insize) {
441 dev_err(ec_dev->dev, "Packet too long (%d bytes, expected %d)",
442 in_msg->ec_response.data_len, msg->insize);
446 /* Copy response packet payload and compute checksum */
447 for (i = 0; i < sizeof(struct ec_host_response); i++)
448 sum += ((u8 *)in_msg)[IN_MSG_EC_RESPONSE_PREAMBLE + i];
450 offset = sizeof(struct cros_ish_in_msg);
451 for (i = 0; i < in_msg->ec_response.data_len; i++)
452 sum += msg->data[i] = ((u8 *)in_msg)[offset + i];
455 dev_dbg(ec_dev->dev, "Bad received packet checksum %d\n", sum);
462 static int cros_ec_pkt_xfer_ish(struct cros_ec_device *ec_dev,
463 struct cros_ec_command *msg)
466 struct ishtp_cl *cros_ish_cl = ec_dev->priv;
467 struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
468 struct device *dev = cl_data_to_dev(client_data);
469 struct cros_ish_in_msg *in_msg = (struct cros_ish_in_msg *)ec_dev->din;
470 struct cros_ish_out_msg *out_msg =
471 (struct cros_ish_out_msg *)ec_dev->dout;
472 size_t in_size = sizeof(struct cros_ish_in_msg) + msg->insize;
473 size_t out_size = sizeof(struct cros_ish_out_msg) + msg->outsize;
476 if (in_size > ec_dev->din_size) {
478 "Incoming payload size %zu is too large for ec_dev->din_size %d\n",
479 in_size, ec_dev->din_size);
483 if (out_size > ec_dev->dout_size) {
485 "Outgoing payload size %zu is too large for ec_dev->dout_size %d\n",
486 out_size, ec_dev->dout_size);
490 /* Proceed only if reset-init is not in progress */
491 if (!down_read_trylock(&init_lock)) {
493 "Host is not ready to send messages to ISH. Try again\n");
497 /* Prepare the package to be sent over ISH TP */
498 out_msg->hdr.channel = CROS_EC_COMMAND;
499 out_msg->hdr.status = 0;
501 ec_dev->dout += OUT_MSG_EC_REQUEST_PREAMBLE;
502 cros_ec_prepare_tx(ec_dev, msg);
503 ec_dev->dout -= OUT_MSG_EC_REQUEST_PREAMBLE;
506 "out_msg: struct_ver=0x%x checksum=0x%x command=0x%x command_ver=0x%x data_len=0x%x\n",
507 out_msg->ec_request.struct_version,
508 out_msg->ec_request.checksum,
509 out_msg->ec_request.command,
510 out_msg->ec_request.command_version,
511 out_msg->ec_request.data_len);
513 /* Send command to ISH EC firmware and read response */
514 rv = ish_send(client_data,
515 (u8 *)out_msg, out_size,
516 (u8 *)in_msg, in_size);
520 rv = prepare_cros_ec_rx(ec_dev, in_msg, msg);
524 rv = in_msg->ec_response.data_len;
527 "in_msg: struct_ver=0x%x checksum=0x%x result=0x%x data_len=0x%x\n",
528 in_msg->ec_response.struct_version,
529 in_msg->ec_response.checksum,
530 in_msg->ec_response.result,
531 in_msg->ec_response.data_len);
534 if (msg->command == EC_CMD_REBOOT_EC)
535 msleep(EC_REBOOT_DELAY_MS);
542 static int cros_ec_dev_init(struct ishtp_cl_data *client_data)
544 struct cros_ec_device *ec_dev;
545 struct device *dev = cl_data_to_dev(client_data);
547 ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
551 client_data->ec_dev = ec_dev;
552 dev->driver_data = ec_dev;
555 ec_dev->priv = client_data->cros_ish_cl;
556 ec_dev->cmd_xfer = NULL;
557 ec_dev->pkt_xfer = cros_ec_pkt_xfer_ish;
558 ec_dev->phys_name = dev_name(dev);
559 ec_dev->din_size = sizeof(struct cros_ish_in_msg) +
560 sizeof(struct ec_response_get_protocol_info);
561 ec_dev->dout_size = sizeof(struct cros_ish_out_msg);
563 return cros_ec_register(ec_dev);
566 static void reset_handler(struct work_struct *work)
570 struct ishtp_cl *cros_ish_cl;
571 struct ishtp_cl_device *cl_device;
572 struct ishtp_cl_data *client_data =
573 container_of(work, struct ishtp_cl_data, work_ishtp_reset);
575 /* Lock for reset to complete */
576 down_write(&init_lock);
578 cros_ish_cl = client_data->cros_ish_cl;
579 cl_device = client_data->cl_device;
581 /* Unlink, flush queues & start again */
582 ishtp_cl_unlink(cros_ish_cl);
583 ishtp_cl_flush_queues(cros_ish_cl);
584 ishtp_cl_free(cros_ish_cl);
586 cros_ish_cl = ishtp_cl_allocate(cl_device);
588 up_write(&init_lock);
592 ishtp_set_drvdata(cl_device, cros_ish_cl);
593 ishtp_set_client_data(cros_ish_cl, client_data);
594 client_data->cros_ish_cl = cros_ish_cl;
596 rv = cros_ish_init(cros_ish_cl);
598 ishtp_cl_free(cros_ish_cl);
599 dev_err(cl_data_to_dev(client_data), "Reset Failed\n");
600 up_write(&init_lock);
604 /* Refresh ec_dev device pointers */
605 client_data->ec_dev->priv = client_data->cros_ish_cl;
606 dev = cl_data_to_dev(client_data);
607 dev->driver_data = client_data->ec_dev;
609 dev_info(cl_data_to_dev(client_data), "Chrome EC ISH reset done\n");
611 up_write(&init_lock);
615 * cros_ec_ishtp_probe() - ISHTP client driver probe callback
616 * @cl_device: ISHTP client device instance
618 * Return: 0 for success, negative error code for failure.
620 static int cros_ec_ishtp_probe(struct ishtp_cl_device *cl_device)
623 struct ishtp_cl *cros_ish_cl;
624 struct ishtp_cl_data *client_data =
625 devm_kzalloc(ishtp_device(cl_device),
626 sizeof(*client_data), GFP_KERNEL);
630 /* Lock for initialization to complete */
631 down_write(&init_lock);
633 cros_ish_cl = ishtp_cl_allocate(cl_device);
636 goto end_ishtp_cl_alloc_error;
639 ishtp_set_drvdata(cl_device, cros_ish_cl);
640 ishtp_set_client_data(cros_ish_cl, client_data);
641 client_data->cros_ish_cl = cros_ish_cl;
642 client_data->cl_device = cl_device;
644 init_waitqueue_head(&client_data->response.wait_queue);
646 INIT_WORK(&client_data->work_ishtp_reset,
648 INIT_WORK(&client_data->work_ec_evt,
651 rv = cros_ish_init(cros_ish_cl);
653 goto end_ishtp_cl_init_error;
655 ishtp_get_device(cl_device);
657 up_write(&init_lock);
659 /* Register croc_ec_dev mfd */
660 rv = cros_ec_dev_init(client_data);
662 goto end_cros_ec_dev_init_error;
666 end_cros_ec_dev_init_error:
667 ishtp_set_connection_state(cros_ish_cl, ISHTP_CL_DISCONNECTING);
668 ishtp_cl_disconnect(cros_ish_cl);
669 ishtp_cl_unlink(cros_ish_cl);
670 ishtp_cl_flush_queues(cros_ish_cl);
671 ishtp_put_device(cl_device);
672 end_ishtp_cl_init_error:
673 ishtp_cl_free(cros_ish_cl);
674 end_ishtp_cl_alloc_error:
675 up_write(&init_lock);
680 * cros_ec_ishtp_remove() - ISHTP client driver remove callback
681 * @cl_device: ISHTP client device instance
685 static int cros_ec_ishtp_remove(struct ishtp_cl_device *cl_device)
687 struct ishtp_cl *cros_ish_cl = ishtp_get_drvdata(cl_device);
688 struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
690 cancel_work_sync(&client_data->work_ishtp_reset);
691 cancel_work_sync(&client_data->work_ec_evt);
692 cros_ish_deinit(cros_ish_cl);
693 ishtp_put_device(cl_device);
699 * cros_ec_ishtp_reset() - ISHTP client driver reset callback
700 * @cl_device: ISHTP client device instance
704 static int cros_ec_ishtp_reset(struct ishtp_cl_device *cl_device)
706 struct ishtp_cl *cros_ish_cl = ishtp_get_drvdata(cl_device);
707 struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
709 schedule_work(&client_data->work_ishtp_reset);
715 * cros_ec_ishtp_suspend() - ISHTP client driver suspend callback
716 * @device: device instance
718 * Return: 0 for success, negative error code for failure.
720 static int __maybe_unused cros_ec_ishtp_suspend(struct device *device)
722 struct ishtp_cl_device *cl_device = ishtp_dev_to_cl_device(device);
723 struct ishtp_cl *cros_ish_cl = ishtp_get_drvdata(cl_device);
724 struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
726 return cros_ec_suspend(client_data->ec_dev);
730 * cros_ec_ishtp_resume() - ISHTP client driver resume callback
731 * @device: device instance
733 * Return: 0 for success, negative error code for failure.
735 static int __maybe_unused cros_ec_ishtp_resume(struct device *device)
737 struct ishtp_cl_device *cl_device = ishtp_dev_to_cl_device(device);
738 struct ishtp_cl *cros_ish_cl = ishtp_get_drvdata(cl_device);
739 struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
741 return cros_ec_resume(client_data->ec_dev);
744 static SIMPLE_DEV_PM_OPS(cros_ec_ishtp_pm_ops, cros_ec_ishtp_suspend,
745 cros_ec_ishtp_resume);
747 static struct ishtp_cl_driver cros_ec_ishtp_driver = {
748 .name = "cros_ec_ishtp",
749 .guid = &cros_ish_guid,
750 .probe = cros_ec_ishtp_probe,
751 .remove = cros_ec_ishtp_remove,
752 .reset = cros_ec_ishtp_reset,
754 .pm = &cros_ec_ishtp_pm_ops,
758 static int __init cros_ec_ishtp_mod_init(void)
760 return ishtp_cl_driver_register(&cros_ec_ishtp_driver, THIS_MODULE);
763 static void __exit cros_ec_ishtp_mod_exit(void)
765 ishtp_cl_driver_unregister(&cros_ec_ishtp_driver);
768 module_init(cros_ec_ishtp_mod_init);
769 module_exit(cros_ec_ishtp_mod_exit);
771 MODULE_DESCRIPTION("ChromeOS EC ISHTP Client Driver");
772 MODULE_AUTHOR("Rushikesh S Kadam <rushikesh.s.kadam@intel.com>");
774 MODULE_LICENSE("GPL v2");
775 MODULE_ALIAS("ishtp:*");