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>
20 * ISH TX/RX ring buffer pool size
22 * The AP->ISH messages and corresponding ISH->AP responses are
23 * serialized. We need 1 TX and 1 RX buffer for these.
25 * The MKBP ISH->AP events are serialized. We need one additional RX
28 #define CROS_ISH_CL_TX_RING_SIZE 8
29 #define CROS_ISH_CL_RX_RING_SIZE 8
31 /* ISH CrOS EC Host Commands */
32 enum cros_ec_ish_channel {
33 CROS_EC_COMMAND = 1, /* AP->ISH message */
34 CROS_MKBP_EVENT = 2, /* ISH->AP events */
38 * ISH firmware timeout for 1 message send failure is 1Hz, and the
39 * firmware will retry 2 times, so 3Hz is used for timeout.
41 #define ISHTP_SEND_TIMEOUT (3 * HZ)
43 /* ISH Transport CrOS EC ISH client unique GUID */
44 static const guid_t cros_ish_guid =
45 GUID_INIT(0x7b7154d0, 0x56f4, 0x4bdc,
46 0xb0, 0xd8, 0x9e, 0x7c, 0xda, 0xe0, 0xd6, 0xa0);
55 struct cros_ish_out_msg {
57 struct ec_host_request ec_request;
60 struct cros_ish_in_msg {
62 struct ec_host_response ec_response;
65 #define IN_MSG_EC_RESPONSE_PREAMBLE \
66 offsetof(struct cros_ish_in_msg, ec_response)
68 #define OUT_MSG_EC_REQUEST_PREAMBLE \
69 offsetof(struct cros_ish_out_msg, ec_request)
71 #define cl_data_to_dev(client_data) ishtp_device((client_data)->cl_device)
74 * The Read-Write Semaphore is used to prevent message TX or RX while
75 * the ishtp client is being initialized or undergoing reset.
77 * The readers are the kernel function calls responsible for IA->ISH
78 * and ISH->AP messaging.
80 * The writers are .reset() and .probe() function.
82 static DECLARE_RWSEM(init_lock);
85 * struct response_info - Encapsulate firmware response related
86 * information for passing between function ish_send() and
87 * process_recv() callback.
89 * @data: Copy the data received from firmware here.
90 * @max_size: Max size allocated for the @data buffer. If the received
91 * data exceeds this value, we log an error.
92 * @size: Actual size of data received from firmware.
93 * @error: 0 for success, negative error code for a failure in process_recv().
94 * @token: Expected token for response that we are waiting on.
95 * @received: Set to true on receiving a valid firmware response to host command
96 * @wait_queue: Wait queue for host to wait for firmware response.
98 struct response_info {
105 wait_queue_head_t wait_queue;
109 * struct ishtp_cl_data - Encapsulate per ISH TP Client.
111 * @cros_ish_cl: ISHTP firmware client instance.
112 * @cl_device: ISHTP client device instance.
113 * @response: Response info passing between ish_send() and process_recv().
114 * @work_ishtp_reset: Work queue reset handling.
115 * @work_ec_evt: Work queue for EC events.
116 * @ec_dev: CrOS EC MFD device.
118 * This structure is used to store per client data.
120 struct ishtp_cl_data {
121 struct ishtp_cl *cros_ish_cl;
122 struct ishtp_cl_device *cl_device;
125 * Used for passing firmware response information between
126 * ish_send() and process_recv() callback.
128 struct response_info response;
130 struct work_struct work_ishtp_reset;
131 struct work_struct work_ec_evt;
132 struct cros_ec_device *ec_dev;
136 * ish_evt_handler - ISH to AP event handler
139 static void ish_evt_handler(struct work_struct *work)
141 struct ishtp_cl_data *client_data =
142 container_of(work, struct ishtp_cl_data, work_ec_evt);
144 cros_ec_irq_thread(0, client_data->ec_dev);
148 * ish_send() - Send message from host to firmware
150 * @client_data: Client data instance
151 * @out_msg: Message buffer to be sent to firmware
152 * @out_size: Size of out going message
153 * @in_msg: Message buffer where the incoming data is copied. This buffer
154 * is allocated by calling
155 * @in_size: Max size of incoming message
157 * Return: Number of bytes copied in the in_msg on success, negative
158 * error code on failure.
160 static int ish_send(struct ishtp_cl_data *client_data,
161 u8 *out_msg, size_t out_size,
162 u8 *in_msg, size_t in_size)
164 static u8 next_token;
166 struct header *out_hdr = (struct header *)out_msg;
167 struct ishtp_cl *cros_ish_cl = client_data->cros_ish_cl;
169 dev_dbg(cl_data_to_dev(client_data),
170 "%s: channel=%02u status=%02u\n",
171 __func__, out_hdr->channel, out_hdr->status);
173 /* Setup for incoming response */
174 client_data->response.data = in_msg;
175 client_data->response.max_size = in_size;
176 client_data->response.error = 0;
177 client_data->response.token = next_token++;
178 client_data->response.received = false;
180 out_hdr->token = client_data->response.token;
182 rv = ishtp_cl_send(cros_ish_cl, out_msg, out_size);
184 dev_err(cl_data_to_dev(client_data),
185 "ishtp_cl_send error %d\n", rv);
189 wait_event_interruptible_timeout(client_data->response.wait_queue,
190 client_data->response.received,
192 if (!client_data->response.received) {
193 dev_err(cl_data_to_dev(client_data),
194 "Timed out for response to host message\n");
198 if (client_data->response.error < 0)
199 return client_data->response.error;
201 return client_data->response.size;
205 * process_recv() - Received and parse incoming packet
206 * @cros_ish_cl: Client instance to get stats
207 * @rb_in_proc: Host interface message buffer
208 * @timestamp: Timestamp of when parent callback started
210 * Parse the incoming packet. If it is a response packet then it will
211 * update per instance flags and wake up the caller waiting to for the
212 * response. If it is an event packet then it will schedule event work.
214 static void process_recv(struct ishtp_cl *cros_ish_cl,
215 struct ishtp_cl_rb *rb_in_proc, ktime_t timestamp)
217 size_t data_len = rb_in_proc->buf_idx;
218 struct ishtp_cl_data *client_data =
219 ishtp_get_client_data(cros_ish_cl);
220 struct device *dev = cl_data_to_dev(client_data);
221 struct cros_ish_in_msg *in_msg =
222 (struct cros_ish_in_msg *)rb_in_proc->buffer.data;
224 /* Proceed only if reset or init is not in progress */
225 if (!down_read_trylock(&init_lock)) {
226 /* Free the buffer */
227 ishtp_cl_io_rb_recycle(rb_in_proc);
229 "Host is not ready to receive incoming messages\n");
234 * All firmware messages contain a header. Check the buffer size
235 * before accessing elements inside.
237 if (!rb_in_proc->buffer.data) {
238 dev_warn(dev, "rb_in_proc->buffer.data returned null");
239 client_data->response.error = -EBADMSG;
243 if (data_len < sizeof(struct header)) {
244 dev_err(dev, "data size %zu is less than header %zu\n",
245 data_len, sizeof(struct header));
246 client_data->response.error = -EMSGSIZE;
250 dev_dbg(dev, "channel=%02u status=%02u\n",
251 in_msg->hdr.channel, in_msg->hdr.status);
253 switch (in_msg->hdr.channel) {
254 case CROS_EC_COMMAND:
255 if (client_data->response.received) {
257 "Previous firmware message not yet processed\n");
261 if (client_data->response.token != in_msg->hdr.token) {
262 dev_err_ratelimited(dev,
263 "Dropping old response token %d\n",
269 if (!client_data->response.data) {
271 "Receiving buffer is null. Should be allocated by calling function\n");
272 client_data->response.error = -EINVAL;
276 if (data_len > client_data->response.max_size) {
278 "Received buffer size %zu is larger than allocated buffer %zu\n",
279 data_len, client_data->response.max_size);
280 client_data->response.error = -EMSGSIZE;
284 if (in_msg->hdr.status) {
285 dev_err(dev, "firmware returned status %d\n",
287 client_data->response.error = -EIO;
291 /* Update the actual received buffer size */
292 client_data->response.size = data_len;
295 * Copy the buffer received in firmware response for the
298 memcpy(client_data->response.data,
299 rb_in_proc->buffer.data, data_len);
302 /* Free the buffer since we copied data or didn't need it */
303 ishtp_cl_io_rb_recycle(rb_in_proc);
306 /* Set flag before waking up the caller */
307 client_data->response.received = true;
309 /* Wake the calling thread */
310 wake_up_interruptible(&client_data->response.wait_queue);
314 case CROS_MKBP_EVENT:
315 /* Free the buffer. This is just an event without data */
316 ishtp_cl_io_rb_recycle(rb_in_proc);
319 * Set timestamp from beginning of function since we actually
320 * got an incoming MKBP event
322 client_data->ec_dev->last_event_time = timestamp;
323 schedule_work(&client_data->work_ec_evt);
328 dev_err(dev, "Invalid channel=%02d\n", in_msg->hdr.channel);
332 /* Free the buffer if we already haven't */
334 ishtp_cl_io_rb_recycle(rb_in_proc);
340 * ish_event_cb() - bus driver callback for incoming message
341 * @cl_device: ISHTP client device for which this message is targeted.
343 * Remove the packet from the list and process the message by calling
346 static void ish_event_cb(struct ishtp_cl_device *cl_device)
348 struct ishtp_cl_rb *rb_in_proc;
349 struct ishtp_cl *cros_ish_cl = ishtp_get_drvdata(cl_device);
353 * Take timestamp as close to hardware interrupt as possible for sensor
356 timestamp = cros_ec_get_time_ns();
358 while ((rb_in_proc = ishtp_cl_rx_get_rb(cros_ish_cl)) != NULL) {
359 /* Decide what to do with received data */
360 process_recv(cros_ish_cl, rb_in_proc, timestamp);
365 * cros_ish_init() - Init function for ISHTP client
366 * @cros_ish_cl: ISHTP client instance
368 * This function complete the initializtion of the client.
370 * Return: 0 for success, negative error code for failure.
372 static int cros_ish_init(struct ishtp_cl *cros_ish_cl)
375 struct ishtp_device *dev;
376 struct ishtp_fw_client *fw_client;
377 struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
379 rv = ishtp_cl_link(cros_ish_cl);
381 dev_err(cl_data_to_dev(client_data),
382 "ishtp_cl_link failed\n");
386 dev = ishtp_get_ishtp_device(cros_ish_cl);
388 /* Connect to firmware client */
389 ishtp_set_tx_ring_size(cros_ish_cl, CROS_ISH_CL_TX_RING_SIZE);
390 ishtp_set_rx_ring_size(cros_ish_cl, CROS_ISH_CL_RX_RING_SIZE);
392 fw_client = ishtp_fw_cl_get_client(dev, &cros_ish_guid);
394 dev_err(cl_data_to_dev(client_data),
395 "ish client uuid not found\n");
400 ishtp_cl_set_fw_client_id(cros_ish_cl,
401 ishtp_get_fw_client_id(fw_client));
402 ishtp_set_connection_state(cros_ish_cl, ISHTP_CL_CONNECTING);
404 rv = ishtp_cl_connect(cros_ish_cl);
406 dev_err(cl_data_to_dev(client_data),
407 "client connect fail\n");
411 ishtp_register_event_cb(client_data->cl_device, ish_event_cb);
415 ishtp_cl_unlink(cros_ish_cl);
420 * cros_ish_deinit() - Deinit function for ISHTP client
421 * @cros_ish_cl: ISHTP client instance
423 * Unlink and free cros_ec client
425 static void cros_ish_deinit(struct ishtp_cl *cros_ish_cl)
427 ishtp_set_connection_state(cros_ish_cl, ISHTP_CL_DISCONNECTING);
428 ishtp_cl_disconnect(cros_ish_cl);
429 ishtp_cl_unlink(cros_ish_cl);
430 ishtp_cl_flush_queues(cros_ish_cl);
432 /* Disband and free all Tx and Rx client-level rings */
433 ishtp_cl_free(cros_ish_cl);
437 * prepare_cros_ec_rx() - Check & prepare receive buffer
438 * @ec_dev: CrOS EC MFD device.
439 * @in_msg: Incoming message buffer
440 * @msg: cros_ec command used to send & receive data
442 * Return: 0 for success, negative error code for failure.
444 * Check the received buffer. Convert to cros_ec_command format.
446 static int prepare_cros_ec_rx(struct cros_ec_device *ec_dev,
447 const struct cros_ish_in_msg *in_msg,
448 struct cros_ec_command *msg)
453 /* Check response error code */
454 msg->result = in_msg->ec_response.result;
455 rv = cros_ec_check_result(ec_dev, msg);
459 if (in_msg->ec_response.data_len > msg->insize) {
460 dev_err(ec_dev->dev, "Packet too long (%d bytes, expected %d)",
461 in_msg->ec_response.data_len, msg->insize);
465 /* Copy response packet payload and compute checksum */
466 for (i = 0; i < sizeof(struct ec_host_response); i++)
467 sum += ((u8 *)in_msg)[IN_MSG_EC_RESPONSE_PREAMBLE + i];
469 offset = sizeof(struct cros_ish_in_msg);
470 for (i = 0; i < in_msg->ec_response.data_len; i++)
471 sum += msg->data[i] = ((u8 *)in_msg)[offset + i];
474 dev_dbg(ec_dev->dev, "Bad received packet checksum %d\n", sum);
481 static int cros_ec_pkt_xfer_ish(struct cros_ec_device *ec_dev,
482 struct cros_ec_command *msg)
485 struct ishtp_cl *cros_ish_cl = ec_dev->priv;
486 struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
487 struct device *dev = cl_data_to_dev(client_data);
488 struct cros_ish_in_msg *in_msg = (struct cros_ish_in_msg *)ec_dev->din;
489 struct cros_ish_out_msg *out_msg =
490 (struct cros_ish_out_msg *)ec_dev->dout;
491 size_t in_size = sizeof(struct cros_ish_in_msg) + msg->insize;
492 size_t out_size = sizeof(struct cros_ish_out_msg) + msg->outsize;
495 if (in_size > ec_dev->din_size) {
497 "Incoming payload size %zu is too large for ec_dev->din_size %d\n",
498 in_size, ec_dev->din_size);
502 if (out_size > ec_dev->dout_size) {
504 "Outgoing payload size %zu is too large for ec_dev->dout_size %d\n",
505 out_size, ec_dev->dout_size);
509 /* Proceed only if reset-init is not in progress */
510 if (!down_read_trylock(&init_lock)) {
512 "Host is not ready to send messages to ISH. Try again\n");
516 /* Prepare the package to be sent over ISH TP */
517 out_msg->hdr.channel = CROS_EC_COMMAND;
518 out_msg->hdr.status = 0;
520 ec_dev->dout += OUT_MSG_EC_REQUEST_PREAMBLE;
521 cros_ec_prepare_tx(ec_dev, msg);
522 ec_dev->dout -= OUT_MSG_EC_REQUEST_PREAMBLE;
525 "out_msg: struct_ver=0x%x checksum=0x%x command=0x%x command_ver=0x%x data_len=0x%x\n",
526 out_msg->ec_request.struct_version,
527 out_msg->ec_request.checksum,
528 out_msg->ec_request.command,
529 out_msg->ec_request.command_version,
530 out_msg->ec_request.data_len);
532 /* Send command to ISH EC firmware and read response */
533 rv = ish_send(client_data,
534 (u8 *)out_msg, out_size,
535 (u8 *)in_msg, in_size);
539 rv = prepare_cros_ec_rx(ec_dev, in_msg, msg);
543 rv = in_msg->ec_response.data_len;
546 "in_msg: struct_ver=0x%x checksum=0x%x result=0x%x data_len=0x%x\n",
547 in_msg->ec_response.struct_version,
548 in_msg->ec_response.checksum,
549 in_msg->ec_response.result,
550 in_msg->ec_response.data_len);
553 if (msg->command == EC_CMD_REBOOT_EC)
554 msleep(EC_REBOOT_DELAY_MS);
561 static int cros_ec_dev_init(struct ishtp_cl_data *client_data)
563 struct cros_ec_device *ec_dev;
564 struct device *dev = cl_data_to_dev(client_data);
566 ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
570 client_data->ec_dev = ec_dev;
571 dev->driver_data = ec_dev;
574 ec_dev->priv = client_data->cros_ish_cl;
575 ec_dev->cmd_xfer = NULL;
576 ec_dev->pkt_xfer = cros_ec_pkt_xfer_ish;
577 ec_dev->phys_name = dev_name(dev);
578 ec_dev->din_size = sizeof(struct cros_ish_in_msg) +
579 sizeof(struct ec_response_get_protocol_info);
580 ec_dev->dout_size = sizeof(struct cros_ish_out_msg);
582 return cros_ec_register(ec_dev);
585 static void reset_handler(struct work_struct *work)
589 struct ishtp_cl *cros_ish_cl;
590 struct ishtp_cl_device *cl_device;
591 struct ishtp_cl_data *client_data =
592 container_of(work, struct ishtp_cl_data, work_ishtp_reset);
594 /* Lock for reset to complete */
595 down_write(&init_lock);
597 cros_ish_cl = client_data->cros_ish_cl;
598 cl_device = client_data->cl_device;
600 /* Unlink, flush queues & start again */
601 ishtp_cl_unlink(cros_ish_cl);
602 ishtp_cl_flush_queues(cros_ish_cl);
603 ishtp_cl_free(cros_ish_cl);
605 cros_ish_cl = ishtp_cl_allocate(cl_device);
607 up_write(&init_lock);
611 ishtp_set_drvdata(cl_device, cros_ish_cl);
612 ishtp_set_client_data(cros_ish_cl, client_data);
613 client_data->cros_ish_cl = cros_ish_cl;
615 rv = cros_ish_init(cros_ish_cl);
617 ishtp_cl_free(cros_ish_cl);
618 dev_err(cl_data_to_dev(client_data), "Reset Failed\n");
619 up_write(&init_lock);
623 /* Refresh ec_dev device pointers */
624 client_data->ec_dev->priv = client_data->cros_ish_cl;
625 dev = cl_data_to_dev(client_data);
626 dev->driver_data = client_data->ec_dev;
628 dev_info(cl_data_to_dev(client_data), "Chrome EC ISH reset done\n");
630 up_write(&init_lock);
634 * cros_ec_ishtp_probe() - ISHTP client driver probe callback
635 * @cl_device: ISHTP client device instance
637 * Return: 0 for success, negative error code for failure.
639 static int cros_ec_ishtp_probe(struct ishtp_cl_device *cl_device)
642 struct ishtp_cl *cros_ish_cl;
643 struct ishtp_cl_data *client_data =
644 devm_kzalloc(ishtp_device(cl_device),
645 sizeof(*client_data), GFP_KERNEL);
649 /* Lock for initialization to complete */
650 down_write(&init_lock);
652 cros_ish_cl = ishtp_cl_allocate(cl_device);
655 goto end_ishtp_cl_alloc_error;
658 ishtp_set_drvdata(cl_device, cros_ish_cl);
659 ishtp_set_client_data(cros_ish_cl, client_data);
660 client_data->cros_ish_cl = cros_ish_cl;
661 client_data->cl_device = cl_device;
663 init_waitqueue_head(&client_data->response.wait_queue);
665 INIT_WORK(&client_data->work_ishtp_reset,
667 INIT_WORK(&client_data->work_ec_evt,
670 rv = cros_ish_init(cros_ish_cl);
672 goto end_ishtp_cl_init_error;
674 ishtp_get_device(cl_device);
676 up_write(&init_lock);
678 /* Register croc_ec_dev mfd */
679 rv = cros_ec_dev_init(client_data);
681 down_write(&init_lock);
682 goto end_cros_ec_dev_init_error;
687 end_cros_ec_dev_init_error:
688 ishtp_set_connection_state(cros_ish_cl, ISHTP_CL_DISCONNECTING);
689 ishtp_cl_disconnect(cros_ish_cl);
690 ishtp_cl_unlink(cros_ish_cl);
691 ishtp_cl_flush_queues(cros_ish_cl);
692 ishtp_put_device(cl_device);
693 end_ishtp_cl_init_error:
694 ishtp_cl_free(cros_ish_cl);
695 end_ishtp_cl_alloc_error:
696 up_write(&init_lock);
701 * cros_ec_ishtp_remove() - ISHTP client driver remove callback
702 * @cl_device: ISHTP client device instance
706 static void cros_ec_ishtp_remove(struct ishtp_cl_device *cl_device)
708 struct ishtp_cl *cros_ish_cl = ishtp_get_drvdata(cl_device);
709 struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
711 cancel_work_sync(&client_data->work_ishtp_reset);
712 cancel_work_sync(&client_data->work_ec_evt);
713 cros_ish_deinit(cros_ish_cl);
714 ishtp_put_device(cl_device);
718 * cros_ec_ishtp_reset() - ISHTP client driver reset callback
719 * @cl_device: ISHTP client device instance
723 static int cros_ec_ishtp_reset(struct ishtp_cl_device *cl_device)
725 struct ishtp_cl *cros_ish_cl = ishtp_get_drvdata(cl_device);
726 struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
728 schedule_work(&client_data->work_ishtp_reset);
734 * cros_ec_ishtp_suspend() - ISHTP client driver suspend callback
735 * @device: device instance
737 * Return: 0 for success, negative error code for failure.
739 static int __maybe_unused cros_ec_ishtp_suspend(struct device *device)
741 struct ishtp_cl_device *cl_device = ishtp_dev_to_cl_device(device);
742 struct ishtp_cl *cros_ish_cl = ishtp_get_drvdata(cl_device);
743 struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
745 return cros_ec_suspend(client_data->ec_dev);
749 * cros_ec_ishtp_resume() - ISHTP client driver resume callback
750 * @device: device instance
752 * Return: 0 for success, negative error code for failure.
754 static int __maybe_unused cros_ec_ishtp_resume(struct device *device)
756 struct ishtp_cl_device *cl_device = ishtp_dev_to_cl_device(device);
757 struct ishtp_cl *cros_ish_cl = ishtp_get_drvdata(cl_device);
758 struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
760 return cros_ec_resume(client_data->ec_dev);
763 static SIMPLE_DEV_PM_OPS(cros_ec_ishtp_pm_ops, cros_ec_ishtp_suspend,
764 cros_ec_ishtp_resume);
766 static struct ishtp_cl_driver cros_ec_ishtp_driver = {
767 .name = "cros_ec_ishtp",
768 .guid = &cros_ish_guid,
769 .probe = cros_ec_ishtp_probe,
770 .remove = cros_ec_ishtp_remove,
771 .reset = cros_ec_ishtp_reset,
773 .pm = &cros_ec_ishtp_pm_ops,
777 static int __init cros_ec_ishtp_mod_init(void)
779 return ishtp_cl_driver_register(&cros_ec_ishtp_driver, THIS_MODULE);
782 static void __exit cros_ec_ishtp_mod_exit(void)
784 ishtp_cl_driver_unregister(&cros_ec_ishtp_driver);
787 module_init(cros_ec_ishtp_mod_init);
788 module_exit(cros_ec_ishtp_mod_exit);
790 MODULE_DESCRIPTION("ChromeOS EC ISHTP Client Driver");
791 MODULE_AUTHOR("Rushikesh S Kadam <rushikesh.s.kadam@intel.com>");
793 MODULE_LICENSE("GPL v2");
794 MODULE_ALIAS("ishtp:*");