1
2
3
4
5
6
7
8
9
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>
16
17#include "cros_ec.h"
18
19
20
21
22
23
24
25
26
27
28#define CROS_ISH_CL_TX_RING_SIZE 8
29#define CROS_ISH_CL_RX_RING_SIZE 8
30
31
32enum cros_ec_ish_channel {
33 CROS_EC_COMMAND = 1,
34 CROS_MKBP_EVENT = 2,
35};
36
37
38
39
40
41#define ISHTP_SEND_TIMEOUT (3 * HZ)
42
43
44static const guid_t cros_ish_guid =
45 GUID_INIT(0x7b7154d0, 0x56f4, 0x4bdc,
46 0xb0, 0xd8, 0x9e, 0x7c, 0xda, 0xe0, 0xd6, 0xa0);
47
48struct header {
49 u8 channel;
50 u8 status;
51 u8 token;
52 u8 reserved;
53} __packed;
54
55struct cros_ish_out_msg {
56 struct header hdr;
57 struct ec_host_request ec_request;
58} __packed;
59
60struct cros_ish_in_msg {
61 struct header hdr;
62 struct ec_host_response ec_response;
63} __packed;
64
65#define IN_MSG_EC_RESPONSE_PREAMBLE \
66 offsetof(struct cros_ish_in_msg, ec_response)
67
68#define OUT_MSG_EC_REQUEST_PREAMBLE \
69 offsetof(struct cros_ish_out_msg, ec_request)
70
71#define cl_data_to_dev(client_data) ishtp_device((client_data)->cl_device)
72
73
74
75
76
77
78
79
80
81
82static DECLARE_RWSEM(init_lock);
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98struct response_info {
99 void *data;
100 size_t max_size;
101 size_t size;
102 int error;
103 u8 token;
104 bool received;
105 wait_queue_head_t wait_queue;
106};
107
108
109
110
111
112
113
114
115
116
117
118
119
120struct ishtp_cl_data {
121 struct ishtp_cl *cros_ish_cl;
122 struct ishtp_cl_device *cl_device;
123
124
125
126
127
128 struct response_info response;
129
130 struct work_struct work_ishtp_reset;
131 struct work_struct work_ec_evt;
132 struct cros_ec_device *ec_dev;
133};
134
135
136
137
138
139static void ish_evt_handler(struct work_struct *work)
140{
141 struct ishtp_cl_data *client_data =
142 container_of(work, struct ishtp_cl_data, work_ec_evt);
143
144 cros_ec_irq_thread(0, client_data->ec_dev);
145}
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160static 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)
163{
164 static u8 next_token;
165 int rv;
166 struct header *out_hdr = (struct header *)out_msg;
167 struct ishtp_cl *cros_ish_cl = client_data->cros_ish_cl;
168
169 dev_dbg(cl_data_to_dev(client_data),
170 "%s: channel=%02u status=%02u\n",
171 __func__, out_hdr->channel, out_hdr->status);
172
173
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;
179
180 out_hdr->token = client_data->response.token;
181
182 rv = ishtp_cl_send(cros_ish_cl, out_msg, out_size);
183 if (rv) {
184 dev_err(cl_data_to_dev(client_data),
185 "ishtp_cl_send error %d\n", rv);
186 return rv;
187 }
188
189 wait_event_interruptible_timeout(client_data->response.wait_queue,
190 client_data->response.received,
191 ISHTP_SEND_TIMEOUT);
192 if (!client_data->response.received) {
193 dev_err(cl_data_to_dev(client_data),
194 "Timed out for response to host message\n");
195 return -ETIMEDOUT;
196 }
197
198 if (client_data->response.error < 0)
199 return client_data->response.error;
200
201 return client_data->response.size;
202}
203
204
205
206
207
208
209
210
211
212
213
214static void process_recv(struct ishtp_cl *cros_ish_cl,
215 struct ishtp_cl_rb *rb_in_proc, ktime_t timestamp)
216{
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;
223
224
225 if (!down_read_trylock(&init_lock)) {
226
227 ishtp_cl_io_rb_recycle(rb_in_proc);
228 dev_warn(dev,
229 "Host is not ready to receive incoming messages\n");
230 return;
231 }
232
233
234
235
236
237 if (!rb_in_proc->buffer.data) {
238 dev_warn(dev, "rb_in_proc->buffer.data returned null");
239 client_data->response.error = -EBADMSG;
240 goto end_error;
241 }
242
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;
247 goto end_error;
248 }
249
250 dev_dbg(dev, "channel=%02u status=%02u\n",
251 in_msg->hdr.channel, in_msg->hdr.status);
252
253 switch (in_msg->hdr.channel) {
254 case CROS_EC_COMMAND:
255 if (client_data->response.received) {
256 dev_err(dev,
257 "Previous firmware message not yet processed\n");
258 goto end_error;
259 }
260
261 if (client_data->response.token != in_msg->hdr.token) {
262 dev_err_ratelimited(dev,
263 "Dropping old response token %d\n",
264 in_msg->hdr.token);
265 goto end_error;
266 }
267
268
269 if (!client_data->response.data) {
270 dev_err(dev,
271 "Receiving buffer is null. Should be allocated by calling function\n");
272 client_data->response.error = -EINVAL;
273 goto error_wake_up;
274 }
275
276 if (data_len > client_data->response.max_size) {
277 dev_err(dev,
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;
281 goto error_wake_up;
282 }
283
284 if (in_msg->hdr.status) {
285 dev_err(dev, "firmware returned status %d\n",
286 in_msg->hdr.status);
287 client_data->response.error = -EIO;
288 goto error_wake_up;
289 }
290
291
292 client_data->response.size = data_len;
293
294
295
296
297
298 memcpy(client_data->response.data,
299 rb_in_proc->buffer.data, data_len);
300
301error_wake_up:
302
303 ishtp_cl_io_rb_recycle(rb_in_proc);
304 rb_in_proc = NULL;
305
306
307 client_data->response.received = true;
308
309
310 wake_up_interruptible(&client_data->response.wait_queue);
311
312 break;
313
314 case CROS_MKBP_EVENT:
315
316 ishtp_cl_io_rb_recycle(rb_in_proc);
317 rb_in_proc = NULL;
318
319
320
321
322 client_data->ec_dev->last_event_time = timestamp;
323 schedule_work(&client_data->work_ec_evt);
324
325 break;
326
327 default:
328 dev_err(dev, "Invalid channel=%02d\n", in_msg->hdr.channel);
329 }
330
331end_error:
332
333 if (rb_in_proc)
334 ishtp_cl_io_rb_recycle(rb_in_proc);
335
336 up_read(&init_lock);
337}
338
339
340
341
342
343
344
345
346static void ish_event_cb(struct ishtp_cl_device *cl_device)
347{
348 struct ishtp_cl_rb *rb_in_proc;
349 struct ishtp_cl *cros_ish_cl = ishtp_get_drvdata(cl_device);
350 ktime_t timestamp;
351
352
353
354
355
356 timestamp = cros_ec_get_time_ns();
357
358 while ((rb_in_proc = ishtp_cl_rx_get_rb(cros_ish_cl)) != NULL) {
359
360 process_recv(cros_ish_cl, rb_in_proc, timestamp);
361 }
362}
363
364
365
366
367
368
369
370
371
372static int cros_ish_init(struct ishtp_cl *cros_ish_cl)
373{
374 int rv;
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);
378
379 rv = ishtp_cl_link(cros_ish_cl);
380 if (rv) {
381 dev_err(cl_data_to_dev(client_data),
382 "ishtp_cl_link failed\n");
383 return rv;
384 }
385
386 dev = ishtp_get_ishtp_device(cros_ish_cl);
387
388
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);
391
392 fw_client = ishtp_fw_cl_get_client(dev, &cros_ish_guid);
393 if (!fw_client) {
394 dev_err(cl_data_to_dev(client_data),
395 "ish client uuid not found\n");
396 rv = -ENOENT;
397 goto err_cl_unlink;
398 }
399
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);
403
404 rv = ishtp_cl_connect(cros_ish_cl);
405 if (rv) {
406 dev_err(cl_data_to_dev(client_data),
407 "client connect fail\n");
408 goto err_cl_unlink;
409 }
410
411 ishtp_register_event_cb(client_data->cl_device, ish_event_cb);
412 return 0;
413
414err_cl_unlink:
415 ishtp_cl_unlink(cros_ish_cl);
416 return rv;
417}
418
419
420
421
422
423
424
425static void cros_ish_deinit(struct ishtp_cl *cros_ish_cl)
426{
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);
431
432
433 ishtp_cl_free(cros_ish_cl);
434}
435
436
437
438
439
440
441
442
443
444
445
446static 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)
449{
450 u8 sum = 0;
451 int i, rv, offset;
452
453
454 msg->result = in_msg->ec_response.result;
455 rv = cros_ec_check_result(ec_dev, msg);
456 if (rv < 0)
457 return rv;
458
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);
462 return -ENOSPC;
463 }
464
465
466 for (i = 0; i < sizeof(struct ec_host_response); i++)
467 sum += ((u8 *)in_msg)[IN_MSG_EC_RESPONSE_PREAMBLE + i];
468
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];
472
473 if (sum) {
474 dev_dbg(ec_dev->dev, "Bad received packet checksum %d\n", sum);
475 return -EBADMSG;
476 }
477
478 return 0;
479}
480
481static int cros_ec_pkt_xfer_ish(struct cros_ec_device *ec_dev,
482 struct cros_ec_command *msg)
483{
484 int rv;
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;
493
494
495 if (in_size > ec_dev->din_size) {
496 dev_err(dev,
497 "Incoming payload size %zu is too large for ec_dev->din_size %d\n",
498 in_size, ec_dev->din_size);
499 return -EMSGSIZE;
500 }
501
502 if (out_size > ec_dev->dout_size) {
503 dev_err(dev,
504 "Outgoing payload size %zu is too large for ec_dev->dout_size %d\n",
505 out_size, ec_dev->dout_size);
506 return -EMSGSIZE;
507 }
508
509
510 if (!down_read_trylock(&init_lock)) {
511 dev_warn(dev,
512 "Host is not ready to send messages to ISH. Try again\n");
513 return -EAGAIN;
514 }
515
516
517 out_msg->hdr.channel = CROS_EC_COMMAND;
518 out_msg->hdr.status = 0;
519
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;
523
524 dev_dbg(dev,
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);
531
532
533 rv = ish_send(client_data,
534 (u8 *)out_msg, out_size,
535 (u8 *)in_msg, in_size);
536 if (rv < 0)
537 goto end_error;
538
539 rv = prepare_cros_ec_rx(ec_dev, in_msg, msg);
540 if (rv)
541 goto end_error;
542
543 rv = in_msg->ec_response.data_len;
544
545 dev_dbg(dev,
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);
551
552end_error:
553 if (msg->command == EC_CMD_REBOOT_EC)
554 msleep(EC_REBOOT_DELAY_MS);
555
556 up_read(&init_lock);
557
558 return rv;
559}
560
561static int cros_ec_dev_init(struct ishtp_cl_data *client_data)
562{
563 struct cros_ec_device *ec_dev;
564 struct device *dev = cl_data_to_dev(client_data);
565
566 ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
567 if (!ec_dev)
568 return -ENOMEM;
569
570 client_data->ec_dev = ec_dev;
571 dev->driver_data = ec_dev;
572
573 ec_dev->dev = 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);
581
582 return cros_ec_register(ec_dev);
583}
584
585static void reset_handler(struct work_struct *work)
586{
587 int rv;
588 struct device *dev;
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);
593
594
595 down_write(&init_lock);
596
597 cros_ish_cl = client_data->cros_ish_cl;
598 cl_device = client_data->cl_device;
599
600
601 ishtp_cl_unlink(cros_ish_cl);
602 ishtp_cl_flush_queues(cros_ish_cl);
603 ishtp_cl_free(cros_ish_cl);
604
605 cros_ish_cl = ishtp_cl_allocate(cl_device);
606 if (!cros_ish_cl) {
607 up_write(&init_lock);
608 return;
609 }
610
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;
614
615 rv = cros_ish_init(cros_ish_cl);
616 if (rv) {
617 ishtp_cl_free(cros_ish_cl);
618 dev_err(cl_data_to_dev(client_data), "Reset Failed\n");
619 up_write(&init_lock);
620 return;
621 }
622
623
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;
627
628 dev_info(cl_data_to_dev(client_data), "Chrome EC ISH reset done\n");
629
630 up_write(&init_lock);
631}
632
633
634
635
636
637
638
639static int cros_ec_ishtp_probe(struct ishtp_cl_device *cl_device)
640{
641 int rv;
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);
646 if (!client_data)
647 return -ENOMEM;
648
649
650 down_write(&init_lock);
651
652 cros_ish_cl = ishtp_cl_allocate(cl_device);
653 if (!cros_ish_cl) {
654 rv = -ENOMEM;
655 goto end_ishtp_cl_alloc_error;
656 }
657
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;
662
663 init_waitqueue_head(&client_data->response.wait_queue);
664
665 INIT_WORK(&client_data->work_ishtp_reset,
666 reset_handler);
667 INIT_WORK(&client_data->work_ec_evt,
668 ish_evt_handler);
669
670 rv = cros_ish_init(cros_ish_cl);
671 if (rv)
672 goto end_ishtp_cl_init_error;
673
674 ishtp_get_device(cl_device);
675
676 up_write(&init_lock);
677
678
679 rv = cros_ec_dev_init(client_data);
680 if (rv) {
681 down_write(&init_lock);
682 goto end_cros_ec_dev_init_error;
683 }
684
685 return 0;
686
687end_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);
693end_ishtp_cl_init_error:
694 ishtp_cl_free(cros_ish_cl);
695end_ishtp_cl_alloc_error:
696 up_write(&init_lock);
697 return rv;
698}
699
700
701
702
703
704
705
706static void cros_ec_ishtp_remove(struct ishtp_cl_device *cl_device)
707{
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);
710
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);
715}
716
717
718
719
720
721
722
723static int cros_ec_ishtp_reset(struct ishtp_cl_device *cl_device)
724{
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);
727
728 schedule_work(&client_data->work_ishtp_reset);
729
730 return 0;
731}
732
733
734
735
736
737
738
739static int __maybe_unused cros_ec_ishtp_suspend(struct device *device)
740{
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);
744
745 return cros_ec_suspend(client_data->ec_dev);
746}
747
748
749
750
751
752
753
754static int __maybe_unused cros_ec_ishtp_resume(struct device *device)
755{
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);
759
760 return cros_ec_resume(client_data->ec_dev);
761}
762
763static SIMPLE_DEV_PM_OPS(cros_ec_ishtp_pm_ops, cros_ec_ishtp_suspend,
764 cros_ec_ishtp_resume);
765
766static 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,
772 .driver = {
773 .pm = &cros_ec_ishtp_pm_ops,
774 },
775};
776
777static int __init cros_ec_ishtp_mod_init(void)
778{
779 return ishtp_cl_driver_register(&cros_ec_ishtp_driver, THIS_MODULE);
780}
781
782static void __exit cros_ec_ishtp_mod_exit(void)
783{
784 ishtp_cl_driver_unregister(&cros_ec_ishtp_driver);
785}
786
787module_init(cros_ec_ishtp_mod_init);
788module_exit(cros_ec_ishtp_mod_exit);
789
790MODULE_DESCRIPTION("ChromeOS EC ISHTP Client Driver");
791MODULE_AUTHOR("Rushikesh S Kadam <rushikesh.s.kadam@intel.com>");
792
793MODULE_LICENSE("GPL v2");
794MODULE_ALIAS("ishtp:*");
795