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/mfd/core.h>
12 #include <linux/mfd/cros_ec.h>
13 #include <linux/mfd/cros_ec_commands.h>
14 #include <linux/module.h>
15 #include <linux/pci.h>
16 #include <linux/intel-ish-client-if.h>
19 * ISH TX/RX ring buffer pool size
21 * The AP->ISH messages and corresponding ISH->AP responses are
22 * serialized. We need 1 TX and 1 RX buffer for these.
24 * The MKBP ISH->AP events are serialized. We need one additional RX
27 #define CROS_ISH_CL_TX_RING_SIZE 8
28 #define CROS_ISH_CL_RX_RING_SIZE 8
30 /* ISH CrOS EC Host Commands */
31 enum cros_ec_ish_channel {
32 CROS_EC_COMMAND = 1, /* AP->ISH message */
33 CROS_MKBP_EVENT = 2, /* ISH->AP events */
37 * ISH firmware timeout for 1 message send failure is 1Hz, and the
38 * firmware will retry 2 times, so 3Hz is used for timeout.
40 #define ISHTP_SEND_TIMEOUT (3 * HZ)
42 /* ISH Transport CrOS EC ISH client unique GUID */
43 static const guid_t cros_ish_guid =
44 GUID_INIT(0x7b7154d0, 0x56f4, 0x4bdc,
45 0xb0, 0xd8, 0x9e, 0x7c, 0xda, 0xe0, 0xd6, 0xa0);
53 struct cros_ish_out_msg {
55 struct ec_host_request ec_request;
58 struct cros_ish_in_msg {
60 struct ec_host_response ec_response;
63 #define IN_MSG_EC_RESPONSE_PREAMBLE \
64 offsetof(struct cros_ish_in_msg, ec_response)
66 #define OUT_MSG_EC_REQUEST_PREAMBLE \
67 offsetof(struct cros_ish_out_msg, ec_request)
69 #define cl_data_to_dev(client_data) ishtp_device((client_data)->cl_device)
72 * The Read-Write Semaphore is used to prevent message TX or RX while
73 * the ishtp client is being initialized or undergoing reset.
75 * The readers are the kernel function calls responsible for IA->ISH
76 * and ISH->AP messaging.
78 * The writers are .reset() and .probe() function.
80 DECLARE_RWSEM(init_lock);
83 * struct response_info - Encapsulate firmware response related
84 * information for passing between function ish_send() and
85 * process_recv() callback.
87 * @data: Copy the data received from firmware here.
88 * @max_size: Max size allocated for the @data buffer. If the received
89 * data exceeds this value, we log an error.
90 * @size: Actual size of data received from firmware.
91 * @error: 0 for success, negative error code for a failure in process_recv().
92 * @received: Set to true on receiving a valid firmware response to host command
93 * @wait_queue: Wait queue for host to wait for firmware response.
95 struct response_info {
101 wait_queue_head_t wait_queue;
105 * struct ishtp_cl_data - Encapsulate per ISH TP Client.
107 * @cros_ish_cl: ISHTP firmware client instance.
108 * @cl_device: ISHTP client device instance.
109 * @response: Response info passing between ish_send() and process_recv().
110 * @work_ishtp_reset: Work queue reset handling.
111 * @work_ec_evt: Work queue for EC events.
112 * @ec_dev: CrOS EC MFD device.
114 * This structure is used to store per client data.
116 struct ishtp_cl_data {
117 struct ishtp_cl *cros_ish_cl;
118 struct ishtp_cl_device *cl_device;
121 * Used for passing firmware response information between
122 * ish_send() and process_recv() callback.
124 struct response_info response;
126 struct work_struct work_ishtp_reset;
127 struct work_struct work_ec_evt;
128 struct cros_ec_device *ec_dev;
132 * ish_evt_handler - ISH to AP event handler
135 static void ish_evt_handler(struct work_struct *work)
137 struct ishtp_cl_data *client_data =
138 container_of(work, struct ishtp_cl_data, work_ec_evt);
139 struct cros_ec_device *ec_dev = client_data->ec_dev;
141 if (cros_ec_get_next_event(ec_dev, NULL) > 0) {
142 blocking_notifier_call_chain(&ec_dev->event_notifier,
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)
165 struct header *out_hdr = (struct header *)out_msg;
166 struct ishtp_cl *cros_ish_cl = client_data->cros_ish_cl;
168 dev_dbg(cl_data_to_dev(client_data),
169 "%s: channel=%02u status=%02u\n",
170 __func__, out_hdr->channel, out_hdr->status);
172 /* Setup for incoming response */
173 client_data->response.data = in_msg;
174 client_data->response.max_size = in_size;
175 client_data->response.error = 0;
176 client_data->response.received = false;
178 rv = ishtp_cl_send(cros_ish_cl, out_msg, out_size);
180 dev_err(cl_data_to_dev(client_data),
181 "ishtp_cl_send error %d\n", rv);
185 wait_event_interruptible_timeout(client_data->response.wait_queue,
186 client_data->response.received,
188 if (!client_data->response.received) {
189 dev_err(cl_data_to_dev(client_data),
190 "Timed out for response to host message\n");
194 if (client_data->response.error < 0)
195 return client_data->response.error;
197 return client_data->response.size;
201 * process_recv() - Received and parse incoming packet
202 * @cros_ish_cl: Client instance to get stats
203 * @rb_in_proc: Host interface message buffer
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)
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:
299 /* The event system doesn't send any data in buffer */
300 schedule_work(&client_data->work_ec_evt);
305 dev_err(dev, "Invalid channel=%02d\n", in_msg->hdr.channel);
309 /* Free the buffer */
310 ishtp_cl_io_rb_recycle(rb_in_proc);
316 * ish_event_cb() - bus driver callback for incoming message
317 * @cl_device: ISHTP client device for which this message is targeted.
319 * Remove the packet from the list and process the message by calling
322 static void ish_event_cb(struct ishtp_cl_device *cl_device)
324 struct ishtp_cl_rb *rb_in_proc;
325 struct ishtp_cl *cros_ish_cl = ishtp_get_drvdata(cl_device);
327 while ((rb_in_proc = ishtp_cl_rx_get_rb(cros_ish_cl)) != NULL) {
328 /* Decide what to do with received data */
329 process_recv(cros_ish_cl, rb_in_proc);
334 * cros_ish_init() - Init function for ISHTP client
335 * @cros_ish_cl: ISHTP client instance
337 * This function complete the initializtion of the client.
339 * Return: 0 for success, negative error code for failure.
341 static int cros_ish_init(struct ishtp_cl *cros_ish_cl)
344 struct ishtp_device *dev;
345 struct ishtp_fw_client *fw_client;
346 struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
348 rv = ishtp_cl_link(cros_ish_cl);
350 dev_err(cl_data_to_dev(client_data),
351 "ishtp_cl_link failed\n");
355 dev = ishtp_get_ishtp_device(cros_ish_cl);
357 /* Connect to firmware client */
358 ishtp_set_tx_ring_size(cros_ish_cl, CROS_ISH_CL_TX_RING_SIZE);
359 ishtp_set_rx_ring_size(cros_ish_cl, CROS_ISH_CL_RX_RING_SIZE);
361 fw_client = ishtp_fw_cl_get_client(dev, &cros_ish_guid);
363 dev_err(cl_data_to_dev(client_data),
364 "ish client uuid not found\n");
369 ishtp_cl_set_fw_client_id(cros_ish_cl,
370 ishtp_get_fw_client_id(fw_client));
371 ishtp_set_connection_state(cros_ish_cl, ISHTP_CL_CONNECTING);
373 rv = ishtp_cl_connect(cros_ish_cl);
375 dev_err(cl_data_to_dev(client_data),
376 "client connect fail\n");
380 ishtp_register_event_cb(client_data->cl_device, ish_event_cb);
384 ishtp_cl_unlink(cros_ish_cl);
389 * cros_ish_deinit() - Deinit function for ISHTP client
390 * @cros_ish_cl: ISHTP client instance
392 * Unlink and free cros_ec client
394 static void cros_ish_deinit(struct ishtp_cl *cros_ish_cl)
396 ishtp_set_connection_state(cros_ish_cl, ISHTP_CL_DISCONNECTING);
397 ishtp_cl_disconnect(cros_ish_cl);
398 ishtp_cl_unlink(cros_ish_cl);
399 ishtp_cl_flush_queues(cros_ish_cl);
401 /* Disband and free all Tx and Rx client-level rings */
402 ishtp_cl_free(cros_ish_cl);
406 * prepare_cros_ec_rx() - Check & prepare receive buffer
407 * @ec_dev: CrOS EC MFD device.
408 * @in_msg: Incoming message buffer
409 * @msg: cros_ec command used to send & receive data
411 * Return: 0 for success, negative error code for failure.
413 * Check the received buffer. Convert to cros_ec_command format.
415 static int prepare_cros_ec_rx(struct cros_ec_device *ec_dev,
416 const struct cros_ish_in_msg *in_msg,
417 struct cros_ec_command *msg)
422 /* Check response error code */
423 msg->result = in_msg->ec_response.result;
424 rv = cros_ec_check_result(ec_dev, msg);
428 if (in_msg->ec_response.data_len > msg->insize) {
429 dev_err(ec_dev->dev, "Packet too long (%d bytes, expected %d)",
430 in_msg->ec_response.data_len, msg->insize);
434 /* Copy response packet payload and compute checksum */
435 for (i = 0; i < sizeof(struct ec_host_response); i++)
436 sum += ((u8 *)in_msg)[IN_MSG_EC_RESPONSE_PREAMBLE + i];
438 offset = sizeof(struct cros_ish_in_msg);
439 for (i = 0; i < in_msg->ec_response.data_len; i++)
440 sum += msg->data[i] = ((u8 *)in_msg)[offset + i];
443 dev_dbg(ec_dev->dev, "Bad received packet checksum %d\n", sum);
450 static int cros_ec_pkt_xfer_ish(struct cros_ec_device *ec_dev,
451 struct cros_ec_command *msg)
454 struct ishtp_cl *cros_ish_cl = ec_dev->priv;
455 struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
456 struct device *dev = cl_data_to_dev(client_data);
457 struct cros_ish_in_msg *in_msg = (struct cros_ish_in_msg *)ec_dev->din;
458 struct cros_ish_out_msg *out_msg =
459 (struct cros_ish_out_msg *)ec_dev->dout;
460 size_t in_size = sizeof(struct cros_ish_in_msg) + msg->insize;
461 size_t out_size = sizeof(struct cros_ish_out_msg) + msg->outsize;
464 if (in_size > ec_dev->din_size) {
466 "Incoming payload size %zu is too large for ec_dev->din_size %d\n",
467 in_size, ec_dev->din_size);
471 if (out_size > ec_dev->dout_size) {
473 "Outgoing payload size %zu is too large for ec_dev->dout_size %d\n",
474 out_size, ec_dev->dout_size);
478 /* Proceed only if reset-init is not in progress */
479 if (!down_read_trylock(&init_lock)) {
481 "Host is not ready to send messages to ISH. Try again\n");
485 /* Prepare the package to be sent over ISH TP */
486 out_msg->hdr.channel = CROS_EC_COMMAND;
487 out_msg->hdr.status = 0;
489 ec_dev->dout += OUT_MSG_EC_REQUEST_PREAMBLE;
490 cros_ec_prepare_tx(ec_dev, msg);
491 ec_dev->dout -= OUT_MSG_EC_REQUEST_PREAMBLE;
494 "out_msg: struct_ver=0x%x checksum=0x%x command=0x%x command_ver=0x%x data_len=0x%x\n",
495 out_msg->ec_request.struct_version,
496 out_msg->ec_request.checksum,
497 out_msg->ec_request.command,
498 out_msg->ec_request.command_version,
499 out_msg->ec_request.data_len);
501 /* Send command to ISH EC firmware and read response */
502 rv = ish_send(client_data,
503 (u8 *)out_msg, out_size,
504 (u8 *)in_msg, in_size);
508 rv = prepare_cros_ec_rx(ec_dev, in_msg, msg);
512 rv = in_msg->ec_response.data_len;
515 "in_msg: struct_ver=0x%x checksum=0x%x result=0x%x data_len=0x%x\n",
516 in_msg->ec_response.struct_version,
517 in_msg->ec_response.checksum,
518 in_msg->ec_response.result,
519 in_msg->ec_response.data_len);
522 if (msg->command == EC_CMD_REBOOT_EC)
523 msleep(EC_REBOOT_DELAY_MS);
530 static int cros_ec_dev_init(struct ishtp_cl_data *client_data)
532 struct cros_ec_device *ec_dev;
533 struct device *dev = cl_data_to_dev(client_data);
535 ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
539 client_data->ec_dev = ec_dev;
540 dev->driver_data = ec_dev;
543 ec_dev->priv = client_data->cros_ish_cl;
544 ec_dev->cmd_xfer = NULL;
545 ec_dev->pkt_xfer = cros_ec_pkt_xfer_ish;
546 ec_dev->phys_name = dev_name(dev);
547 ec_dev->din_size = sizeof(struct cros_ish_in_msg) +
548 sizeof(struct ec_response_get_protocol_info);
549 ec_dev->dout_size = sizeof(struct cros_ish_out_msg);
551 return cros_ec_register(ec_dev);
554 static void reset_handler(struct work_struct *work)
558 struct ishtp_cl *cros_ish_cl;
559 struct ishtp_cl_device *cl_device;
560 struct ishtp_cl_data *client_data =
561 container_of(work, struct ishtp_cl_data, work_ishtp_reset);
563 /* Lock for reset to complete */
564 down_write(&init_lock);
566 cros_ish_cl = client_data->cros_ish_cl;
567 cl_device = client_data->cl_device;
569 /* Unlink, flush queues & start again */
570 ishtp_cl_unlink(cros_ish_cl);
571 ishtp_cl_flush_queues(cros_ish_cl);
572 ishtp_cl_free(cros_ish_cl);
574 cros_ish_cl = ishtp_cl_allocate(cl_device);
576 up_write(&init_lock);
580 ishtp_set_drvdata(cl_device, cros_ish_cl);
581 ishtp_set_client_data(cros_ish_cl, client_data);
582 client_data->cros_ish_cl = cros_ish_cl;
584 rv = cros_ish_init(cros_ish_cl);
586 ishtp_cl_free(cros_ish_cl);
587 dev_err(cl_data_to_dev(client_data), "Reset Failed\n");
588 up_write(&init_lock);
592 /* Refresh ec_dev device pointers */
593 client_data->ec_dev->priv = client_data->cros_ish_cl;
594 dev = cl_data_to_dev(client_data);
595 dev->driver_data = client_data->ec_dev;
597 dev_info(cl_data_to_dev(client_data), "Chrome EC ISH reset done\n");
599 up_write(&init_lock);
603 * cros_ec_ishtp_probe() - ISHTP client driver probe callback
604 * @cl_device: ISHTP client device instance
606 * Return: 0 for success, negative error code for failure.
608 static int cros_ec_ishtp_probe(struct ishtp_cl_device *cl_device)
611 struct ishtp_cl *cros_ish_cl;
612 struct ishtp_cl_data *client_data =
613 devm_kzalloc(ishtp_device(cl_device),
614 sizeof(*client_data), GFP_KERNEL);
618 /* Lock for initialization to complete */
619 down_write(&init_lock);
621 cros_ish_cl = ishtp_cl_allocate(cl_device);
624 goto end_ishtp_cl_alloc_error;
627 ishtp_set_drvdata(cl_device, cros_ish_cl);
628 ishtp_set_client_data(cros_ish_cl, client_data);
629 client_data->cros_ish_cl = cros_ish_cl;
630 client_data->cl_device = cl_device;
632 init_waitqueue_head(&client_data->response.wait_queue);
634 INIT_WORK(&client_data->work_ishtp_reset,
636 INIT_WORK(&client_data->work_ec_evt,
639 rv = cros_ish_init(cros_ish_cl);
641 goto end_ishtp_cl_init_error;
643 ishtp_get_device(cl_device);
645 up_write(&init_lock);
647 /* Register croc_ec_dev mfd */
648 rv = cros_ec_dev_init(client_data);
650 goto end_cros_ec_dev_init_error;
654 end_cros_ec_dev_init_error:
655 ishtp_set_connection_state(cros_ish_cl, ISHTP_CL_DISCONNECTING);
656 ishtp_cl_disconnect(cros_ish_cl);
657 ishtp_cl_unlink(cros_ish_cl);
658 ishtp_cl_flush_queues(cros_ish_cl);
659 ishtp_put_device(cl_device);
660 end_ishtp_cl_init_error:
661 ishtp_cl_free(cros_ish_cl);
662 end_ishtp_cl_alloc_error:
663 up_write(&init_lock);
668 * cros_ec_ishtp_remove() - ISHTP client driver remove callback
669 * @cl_device: ISHTP client device instance
673 static int cros_ec_ishtp_remove(struct ishtp_cl_device *cl_device)
675 struct ishtp_cl *cros_ish_cl = ishtp_get_drvdata(cl_device);
676 struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
678 cancel_work_sync(&client_data->work_ishtp_reset);
679 cancel_work_sync(&client_data->work_ec_evt);
680 cros_ish_deinit(cros_ish_cl);
681 ishtp_put_device(cl_device);
687 * cros_ec_ishtp_reset() - ISHTP client driver reset callback
688 * @cl_device: ISHTP client device instance
692 static int cros_ec_ishtp_reset(struct ishtp_cl_device *cl_device)
694 struct ishtp_cl *cros_ish_cl = ishtp_get_drvdata(cl_device);
695 struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
697 schedule_work(&client_data->work_ishtp_reset);
703 * cros_ec_ishtp_suspend() - ISHTP client driver suspend callback
704 * @device: device instance
706 * Return: 0 for success, negative error code for failure.
708 static int __maybe_unused cros_ec_ishtp_suspend(struct device *device)
710 struct ishtp_cl_device *cl_device = dev_get_drvdata(device);
711 struct ishtp_cl *cros_ish_cl = ishtp_get_drvdata(cl_device);
712 struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
714 return cros_ec_suspend(client_data->ec_dev);
718 * cros_ec_ishtp_resume() - ISHTP client driver resume callback
719 * @device: device instance
721 * Return: 0 for success, negative error code for failure.
723 static int __maybe_unused cros_ec_ishtp_resume(struct device *device)
725 struct ishtp_cl_device *cl_device = dev_get_drvdata(device);
726 struct ishtp_cl *cros_ish_cl = ishtp_get_drvdata(cl_device);
727 struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
729 return cros_ec_resume(client_data->ec_dev);
732 static SIMPLE_DEV_PM_OPS(cros_ec_ishtp_pm_ops, cros_ec_ishtp_suspend,
733 cros_ec_ishtp_resume);
735 static struct ishtp_cl_driver cros_ec_ishtp_driver = {
736 .name = "cros_ec_ishtp",
737 .guid = &cros_ish_guid,
738 .probe = cros_ec_ishtp_probe,
739 .remove = cros_ec_ishtp_remove,
740 .reset = cros_ec_ishtp_reset,
742 .pm = &cros_ec_ishtp_pm_ops,
746 static int __init cros_ec_ishtp_mod_init(void)
748 return ishtp_cl_driver_register(&cros_ec_ishtp_driver, THIS_MODULE);
751 static void __exit cros_ec_ishtp_mod_exit(void)
753 ishtp_cl_driver_unregister(&cros_ec_ishtp_driver);
756 module_init(cros_ec_ishtp_mod_init);
757 module_exit(cros_ec_ishtp_mod_exit);
759 MODULE_DESCRIPTION("ChromeOS EC ISHTP Client Driver");
762 MODULE_LICENSE("GPL v2");
763 MODULE_ALIAS("ishtp:*");