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 struct ishtp_device_id cros_ec_ishtp_id_table
[] = {
45 { .guid
= GUID_INIT(0x7b7154d0, 0x56f4, 0x4bdc,
46 0xb0, 0xd8, 0x9e, 0x7c, 0xda, 0xe0, 0xd6, 0xa0), },
49 MODULE_DEVICE_TABLE(ishtp
, cros_ec_ishtp_id_table
);
58 struct cros_ish_out_msg
{
60 struct ec_host_request ec_request
;
63 struct cros_ish_in_msg
{
65 struct ec_host_response ec_response
;
68 #define IN_MSG_EC_RESPONSE_PREAMBLE \
69 offsetof(struct cros_ish_in_msg, ec_response)
71 #define OUT_MSG_EC_REQUEST_PREAMBLE \
72 offsetof(struct cros_ish_out_msg, ec_request)
74 #define cl_data_to_dev(client_data) ishtp_device((client_data)->cl_device)
77 * The Read-Write Semaphore is used to prevent message TX or RX while
78 * the ishtp client is being initialized or undergoing reset.
80 * The readers are the kernel function calls responsible for IA->ISH
81 * and ISH->AP messaging.
83 * The writers are .reset() and .probe() function.
85 static DECLARE_RWSEM(init_lock
);
88 * struct response_info - Encapsulate firmware response related
89 * information for passing between function ish_send() and
90 * process_recv() callback.
92 * @data: Copy the data received from firmware here.
93 * @max_size: Max size allocated for the @data buffer. If the received
94 * data exceeds this value, we log an error.
95 * @size: Actual size of data received from firmware.
96 * @error: 0 for success, negative error code for a failure in process_recv().
97 * @token: Expected token for response that we are waiting on.
98 * @received: Set to true on receiving a valid firmware response to host command
99 * @wait_queue: Wait queue for host to wait for firmware response.
101 struct response_info
{
108 wait_queue_head_t wait_queue
;
112 * struct ishtp_cl_data - Encapsulate per ISH TP Client.
114 * @cros_ish_cl: ISHTP firmware client instance.
115 * @cl_device: ISHTP client device instance.
116 * @response: Response info passing between ish_send() and process_recv().
117 * @work_ishtp_reset: Work queue reset handling.
118 * @work_ec_evt: Work queue for EC events.
119 * @ec_dev: CrOS EC MFD device.
121 * This structure is used to store per client data.
123 struct ishtp_cl_data
{
124 struct ishtp_cl
*cros_ish_cl
;
125 struct ishtp_cl_device
*cl_device
;
128 * Used for passing firmware response information between
129 * ish_send() and process_recv() callback.
131 struct response_info response
;
133 struct work_struct work_ishtp_reset
;
134 struct work_struct work_ec_evt
;
135 struct cros_ec_device
*ec_dev
;
139 * ish_evt_handler - ISH to AP event handler
142 static void ish_evt_handler(struct work_struct
*work
)
144 struct ishtp_cl_data
*client_data
=
145 container_of(work
, struct ishtp_cl_data
, work_ec_evt
);
147 cros_ec_irq_thread(0, client_data
->ec_dev
);
151 * ish_send() - Send message from host to firmware
153 * @client_data: Client data instance
154 * @out_msg: Message buffer to be sent to firmware
155 * @out_size: Size of out going message
156 * @in_msg: Message buffer where the incoming data is copied. This buffer
157 * is allocated by calling
158 * @in_size: Max size of incoming message
160 * Return: Number of bytes copied in the in_msg on success, negative
161 * error code on failure.
163 static int ish_send(struct ishtp_cl_data
*client_data
,
164 u8
*out_msg
, size_t out_size
,
165 u8
*in_msg
, size_t in_size
)
167 static u8 next_token
;
169 struct header
*out_hdr
= (struct header
*)out_msg
;
170 struct ishtp_cl
*cros_ish_cl
= client_data
->cros_ish_cl
;
172 dev_dbg(cl_data_to_dev(client_data
),
173 "%s: channel=%02u status=%02u\n",
174 __func__
, out_hdr
->channel
, out_hdr
->status
);
176 /* Setup for incoming response */
177 client_data
->response
.data
= in_msg
;
178 client_data
->response
.max_size
= in_size
;
179 client_data
->response
.error
= 0;
180 client_data
->response
.token
= next_token
++;
181 client_data
->response
.received
= false;
183 out_hdr
->token
= client_data
->response
.token
;
185 rv
= ishtp_cl_send(cros_ish_cl
, out_msg
, out_size
);
187 dev_err(cl_data_to_dev(client_data
),
188 "ishtp_cl_send error %d\n", rv
);
192 wait_event_interruptible_timeout(client_data
->response
.wait_queue
,
193 client_data
->response
.received
,
195 if (!client_data
->response
.received
) {
196 dev_err(cl_data_to_dev(client_data
),
197 "Timed out for response to host message\n");
201 if (client_data
->response
.error
< 0)
202 return client_data
->response
.error
;
204 return client_data
->response
.size
;
208 * process_recv() - Received and parse incoming packet
209 * @cros_ish_cl: Client instance to get stats
210 * @rb_in_proc: Host interface message buffer
211 * @timestamp: Timestamp of when parent callback started
213 * Parse the incoming packet. If it is a response packet then it will
214 * update per instance flags and wake up the caller waiting to for the
215 * response. If it is an event packet then it will schedule event work.
217 static void process_recv(struct ishtp_cl
*cros_ish_cl
,
218 struct ishtp_cl_rb
*rb_in_proc
, ktime_t timestamp
)
220 size_t data_len
= rb_in_proc
->buf_idx
;
221 struct ishtp_cl_data
*client_data
=
222 ishtp_get_client_data(cros_ish_cl
);
223 struct device
*dev
= cl_data_to_dev(client_data
);
224 struct cros_ish_in_msg
*in_msg
=
225 (struct cros_ish_in_msg
*)rb_in_proc
->buffer
.data
;
227 /* Proceed only if reset or init is not in progress */
228 if (!down_read_trylock(&init_lock
)) {
229 /* Free the buffer */
230 ishtp_cl_io_rb_recycle(rb_in_proc
);
232 "Host is not ready to receive incoming messages\n");
237 * All firmware messages contain a header. Check the buffer size
238 * before accessing elements inside.
240 if (!rb_in_proc
->buffer
.data
) {
241 dev_warn(dev
, "rb_in_proc->buffer.data returned null");
242 client_data
->response
.error
= -EBADMSG
;
246 if (data_len
< sizeof(struct header
)) {
247 dev_err(dev
, "data size %zu is less than header %zu\n",
248 data_len
, sizeof(struct header
));
249 client_data
->response
.error
= -EMSGSIZE
;
253 dev_dbg(dev
, "channel=%02u status=%02u\n",
254 in_msg
->hdr
.channel
, in_msg
->hdr
.status
);
256 switch (in_msg
->hdr
.channel
) {
257 case CROS_EC_COMMAND
:
258 if (client_data
->response
.received
) {
260 "Previous firmware message not yet processed\n");
264 if (client_data
->response
.token
!= in_msg
->hdr
.token
) {
265 dev_err_ratelimited(dev
,
266 "Dropping old response token %d\n",
272 if (!client_data
->response
.data
) {
274 "Receiving buffer is null. Should be allocated by calling function\n");
275 client_data
->response
.error
= -EINVAL
;
279 if (data_len
> client_data
->response
.max_size
) {
281 "Received buffer size %zu is larger than allocated buffer %zu\n",
282 data_len
, client_data
->response
.max_size
);
283 client_data
->response
.error
= -EMSGSIZE
;
287 if (in_msg
->hdr
.status
) {
288 dev_err(dev
, "firmware returned status %d\n",
290 client_data
->response
.error
= -EIO
;
294 /* Update the actual received buffer size */
295 client_data
->response
.size
= data_len
;
298 * Copy the buffer received in firmware response for the
301 memcpy(client_data
->response
.data
,
302 rb_in_proc
->buffer
.data
, data_len
);
305 /* Free the buffer since we copied data or didn't need it */
306 ishtp_cl_io_rb_recycle(rb_in_proc
);
309 /* Set flag before waking up the caller */
310 client_data
->response
.received
= true;
312 /* Wake the calling thread */
313 wake_up_interruptible(&client_data
->response
.wait_queue
);
317 case CROS_MKBP_EVENT
:
318 /* Free the buffer. This is just an event without data */
319 ishtp_cl_io_rb_recycle(rb_in_proc
);
322 * Set timestamp from beginning of function since we actually
323 * got an incoming MKBP event
325 client_data
->ec_dev
->last_event_time
= timestamp
;
326 schedule_work(&client_data
->work_ec_evt
);
331 dev_err(dev
, "Invalid channel=%02d\n", in_msg
->hdr
.channel
);
335 /* Free the buffer if we already haven't */
337 ishtp_cl_io_rb_recycle(rb_in_proc
);
343 * ish_event_cb() - bus driver callback for incoming message
344 * @cl_device: ISHTP client device for which this message is targeted.
346 * Remove the packet from the list and process the message by calling
349 static void ish_event_cb(struct ishtp_cl_device
*cl_device
)
351 struct ishtp_cl_rb
*rb_in_proc
;
352 struct ishtp_cl
*cros_ish_cl
= ishtp_get_drvdata(cl_device
);
356 * Take timestamp as close to hardware interrupt as possible for sensor
359 timestamp
= cros_ec_get_time_ns();
361 while ((rb_in_proc
= ishtp_cl_rx_get_rb(cros_ish_cl
)) != NULL
) {
362 /* Decide what to do with received data */
363 process_recv(cros_ish_cl
, rb_in_proc
, timestamp
);
368 * cros_ish_init() - Init function for ISHTP client
369 * @cros_ish_cl: ISHTP client instance
370 * @reset: true if called from reset handler
372 * This function complete the initializtion of the client.
374 * Return: 0 for success, negative error code for failure.
376 static int cros_ish_init(struct ishtp_cl
*cros_ish_cl
, bool reset
)
379 struct ishtp_cl_data
*client_data
= ishtp_get_client_data(cros_ish_cl
);
381 rv
= ishtp_cl_establish_connection(cros_ish_cl
,
382 &cros_ec_ishtp_id_table
[0].guid
,
383 CROS_ISH_CL_TX_RING_SIZE
,
384 CROS_ISH_CL_RX_RING_SIZE
,
387 dev_err(cl_data_to_dev(client_data
),
388 "client connect fail\n");
389 goto err_cl_disconnect
;
392 ishtp_register_event_cb(client_data
->cl_device
, ish_event_cb
);
396 ishtp_cl_destroy_connection(cros_ish_cl
, reset
);
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_cl_destroy_connection(cros_ish_cl
, false);
410 /* Disband and free all Tx and Rx client-level rings */
411 ishtp_cl_free(cros_ish_cl
);
415 * prepare_cros_ec_rx() - Check & prepare receive buffer
416 * @ec_dev: CrOS EC MFD device.
417 * @in_msg: Incoming message buffer
418 * @msg: cros_ec command used to send & receive data
420 * Return: 0 for success, negative error code for failure.
422 * Check the received buffer. Convert to cros_ec_command format.
424 static int prepare_cros_ec_rx(struct cros_ec_device
*ec_dev
,
425 const struct cros_ish_in_msg
*in_msg
,
426 struct cros_ec_command
*msg
)
431 /* Check response error code */
432 msg
->result
= in_msg
->ec_response
.result
;
433 rv
= cros_ec_check_result(ec_dev
, msg
);
437 if (in_msg
->ec_response
.data_len
> msg
->insize
) {
438 dev_err(ec_dev
->dev
, "Packet too long (%d bytes, expected %d)",
439 in_msg
->ec_response
.data_len
, msg
->insize
);
443 /* Copy response packet payload and compute checksum */
444 for (i
= 0; i
< sizeof(struct ec_host_response
); i
++)
445 sum
+= ((u8
*)in_msg
)[IN_MSG_EC_RESPONSE_PREAMBLE
+ i
];
447 offset
= sizeof(struct cros_ish_in_msg
);
448 for (i
= 0; i
< in_msg
->ec_response
.data_len
; i
++)
449 sum
+= msg
->data
[i
] = ((u8
*)in_msg
)[offset
+ i
];
452 dev_dbg(ec_dev
->dev
, "Bad received packet checksum %d\n", sum
);
459 static int cros_ec_pkt_xfer_ish(struct cros_ec_device
*ec_dev
,
460 struct cros_ec_command
*msg
)
463 struct ishtp_cl
*cros_ish_cl
= ec_dev
->priv
;
464 struct ishtp_cl_data
*client_data
= ishtp_get_client_data(cros_ish_cl
);
465 struct device
*dev
= cl_data_to_dev(client_data
);
466 struct cros_ish_in_msg
*in_msg
= (struct cros_ish_in_msg
*)ec_dev
->din
;
467 struct cros_ish_out_msg
*out_msg
=
468 (struct cros_ish_out_msg
*)ec_dev
->dout
;
469 size_t in_size
= sizeof(struct cros_ish_in_msg
) + msg
->insize
;
470 size_t out_size
= sizeof(struct cros_ish_out_msg
) + msg
->outsize
;
473 if (in_size
> ec_dev
->din_size
) {
475 "Incoming payload size %zu is too large for ec_dev->din_size %d\n",
476 in_size
, ec_dev
->din_size
);
480 if (out_size
> ec_dev
->dout_size
) {
482 "Outgoing payload size %zu is too large for ec_dev->dout_size %d\n",
483 out_size
, ec_dev
->dout_size
);
487 /* Proceed only if reset-init is not in progress */
488 if (!down_read_trylock(&init_lock
)) {
490 "Host is not ready to send messages to ISH. Try again\n");
494 /* Prepare the package to be sent over ISH TP */
495 out_msg
->hdr
.channel
= CROS_EC_COMMAND
;
496 out_msg
->hdr
.status
= 0;
498 ec_dev
->dout
+= OUT_MSG_EC_REQUEST_PREAMBLE
;
499 rv
= cros_ec_prepare_tx(ec_dev
, msg
);
502 ec_dev
->dout
-= OUT_MSG_EC_REQUEST_PREAMBLE
;
505 "out_msg: struct_ver=0x%x checksum=0x%x command=0x%x command_ver=0x%x data_len=0x%x\n",
506 out_msg
->ec_request
.struct_version
,
507 out_msg
->ec_request
.checksum
,
508 out_msg
->ec_request
.command
,
509 out_msg
->ec_request
.command_version
,
510 out_msg
->ec_request
.data_len
);
512 /* Send command to ISH EC firmware and read response */
513 rv
= ish_send(client_data
,
514 (u8
*)out_msg
, out_size
,
515 (u8
*)in_msg
, in_size
);
519 rv
= prepare_cros_ec_rx(ec_dev
, in_msg
, msg
);
523 rv
= in_msg
->ec_response
.data_len
;
526 "in_msg: struct_ver=0x%x checksum=0x%x result=0x%x data_len=0x%x\n",
527 in_msg
->ec_response
.struct_version
,
528 in_msg
->ec_response
.checksum
,
529 in_msg
->ec_response
.result
,
530 in_msg
->ec_response
.data_len
);
533 if (msg
->command
== EC_CMD_REBOOT_EC
)
534 msleep(EC_REBOOT_DELAY_MS
);
541 static int cros_ec_dev_init(struct ishtp_cl_data
*client_data
)
543 struct cros_ec_device
*ec_dev
;
544 struct device
*dev
= cl_data_to_dev(client_data
);
546 ec_dev
= devm_kzalloc(dev
, sizeof(*ec_dev
), GFP_KERNEL
);
550 client_data
->ec_dev
= ec_dev
;
551 dev
->driver_data
= ec_dev
;
554 ec_dev
->priv
= client_data
->cros_ish_cl
;
555 ec_dev
->cmd_xfer
= NULL
;
556 ec_dev
->pkt_xfer
= cros_ec_pkt_xfer_ish
;
557 ec_dev
->phys_name
= dev_name(dev
);
558 ec_dev
->din_size
= sizeof(struct cros_ish_in_msg
) +
559 sizeof(struct ec_response_get_protocol_info
);
560 ec_dev
->dout_size
= sizeof(struct cros_ish_out_msg
);
562 return cros_ec_register(ec_dev
);
565 static void reset_handler(struct work_struct
*work
)
569 struct ishtp_cl
*cros_ish_cl
;
570 struct ishtp_cl_data
*client_data
=
571 container_of(work
, struct ishtp_cl_data
, work_ishtp_reset
);
573 /* Lock for reset to complete */
574 down_write(&init_lock
);
576 cros_ish_cl
= client_data
->cros_ish_cl
;
578 ishtp_cl_destroy_connection(cros_ish_cl
, true);
580 rv
= cros_ish_init(cros_ish_cl
, true);
582 dev_err(cl_data_to_dev(client_data
), "Reset Failed\n");
583 up_write(&init_lock
);
587 /* Refresh ec_dev device pointers */
588 client_data
->ec_dev
->priv
= client_data
->cros_ish_cl
;
589 dev
= cl_data_to_dev(client_data
);
590 dev
->driver_data
= client_data
->ec_dev
;
592 dev_info(cl_data_to_dev(client_data
), "Chrome EC ISH reset done\n");
594 up_write(&init_lock
);
598 * cros_ec_ishtp_probe() - ISHTP client driver probe callback
599 * @cl_device: ISHTP client device instance
601 * Return: 0 for success, negative error code for failure.
603 static int cros_ec_ishtp_probe(struct ishtp_cl_device
*cl_device
)
606 struct ishtp_cl
*cros_ish_cl
;
607 struct ishtp_cl_data
*client_data
=
608 devm_kzalloc(ishtp_device(cl_device
),
609 sizeof(*client_data
), GFP_KERNEL
);
613 /* Lock for initialization to complete */
614 down_write(&init_lock
);
616 cros_ish_cl
= ishtp_cl_allocate(cl_device
);
619 goto end_ishtp_cl_alloc_error
;
622 ishtp_set_drvdata(cl_device
, cros_ish_cl
);
623 ishtp_set_client_data(cros_ish_cl
, client_data
);
624 client_data
->cros_ish_cl
= cros_ish_cl
;
625 client_data
->cl_device
= cl_device
;
627 init_waitqueue_head(&client_data
->response
.wait_queue
);
629 INIT_WORK(&client_data
->work_ishtp_reset
,
631 INIT_WORK(&client_data
->work_ec_evt
,
634 rv
= cros_ish_init(cros_ish_cl
, false);
636 goto end_ishtp_cl_init_error
;
638 ishtp_get_device(cl_device
);
640 up_write(&init_lock
);
642 /* Register croc_ec_dev mfd */
643 rv
= cros_ec_dev_init(client_data
);
645 down_write(&init_lock
);
646 goto end_cros_ec_dev_init_error
;
651 end_cros_ec_dev_init_error
:
652 ishtp_cl_destroy_connection(cros_ish_cl
, false);
653 ishtp_put_device(cl_device
);
654 end_ishtp_cl_init_error
:
655 ishtp_cl_free(cros_ish_cl
);
656 end_ishtp_cl_alloc_error
:
657 up_write(&init_lock
);
662 * cros_ec_ishtp_remove() - ISHTP client driver remove callback
663 * @cl_device: ISHTP client device instance
667 static void cros_ec_ishtp_remove(struct ishtp_cl_device
*cl_device
)
669 struct ishtp_cl
*cros_ish_cl
= ishtp_get_drvdata(cl_device
);
670 struct ishtp_cl_data
*client_data
= ishtp_get_client_data(cros_ish_cl
);
672 cancel_work_sync(&client_data
->work_ishtp_reset
);
673 cancel_work_sync(&client_data
->work_ec_evt
);
674 cros_ish_deinit(cros_ish_cl
);
675 ishtp_put_device(cl_device
);
679 * cros_ec_ishtp_reset() - ISHTP client driver reset callback
680 * @cl_device: ISHTP client device instance
684 static int cros_ec_ishtp_reset(struct ishtp_cl_device
*cl_device
)
686 struct ishtp_cl
*cros_ish_cl
= ishtp_get_drvdata(cl_device
);
687 struct ishtp_cl_data
*client_data
= ishtp_get_client_data(cros_ish_cl
);
689 schedule_work(&client_data
->work_ishtp_reset
);
695 * cros_ec_ishtp_suspend() - ISHTP client driver suspend callback
696 * @device: device instance
698 * Return: 0 for success, negative error code for failure.
700 static int __maybe_unused
cros_ec_ishtp_suspend(struct device
*device
)
702 struct ishtp_cl_device
*cl_device
= ishtp_dev_to_cl_device(device
);
703 struct ishtp_cl
*cros_ish_cl
= ishtp_get_drvdata(cl_device
);
704 struct ishtp_cl_data
*client_data
= ishtp_get_client_data(cros_ish_cl
);
706 return cros_ec_suspend(client_data
->ec_dev
);
710 * cros_ec_ishtp_resume() - ISHTP client driver resume callback
711 * @device: device instance
713 * Return: 0 for success, negative error code for failure.
715 static int __maybe_unused
cros_ec_ishtp_resume(struct device
*device
)
717 struct ishtp_cl_device
*cl_device
= ishtp_dev_to_cl_device(device
);
718 struct ishtp_cl
*cros_ish_cl
= ishtp_get_drvdata(cl_device
);
719 struct ishtp_cl_data
*client_data
= ishtp_get_client_data(cros_ish_cl
);
721 return cros_ec_resume(client_data
->ec_dev
);
724 static SIMPLE_DEV_PM_OPS(cros_ec_ishtp_pm_ops
, cros_ec_ishtp_suspend
,
725 cros_ec_ishtp_resume
);
727 static struct ishtp_cl_driver cros_ec_ishtp_driver
= {
728 .name
= "cros_ec_ishtp",
729 .id
= cros_ec_ishtp_id_table
,
730 .probe
= cros_ec_ishtp_probe
,
731 .remove
= cros_ec_ishtp_remove
,
732 .reset
= cros_ec_ishtp_reset
,
734 .pm
= &cros_ec_ishtp_pm_ops
,
738 static int __init
cros_ec_ishtp_mod_init(void)
740 return ishtp_cl_driver_register(&cros_ec_ishtp_driver
, THIS_MODULE
);
743 static void __exit
cros_ec_ishtp_mod_exit(void)
745 ishtp_cl_driver_unregister(&cros_ec_ishtp_driver
);
748 module_init(cros_ec_ishtp_mod_init
);
749 module_exit(cros_ec_ishtp_mod_exit
);
751 MODULE_DESCRIPTION("ChromeOS EC ISHTP Client Driver");
752 MODULE_AUTHOR("Rushikesh S Kadam <rushikesh.s.kadam@intel.com>");
754 MODULE_LICENSE("GPL v2");