1
2
3
4
5
6
7
8#include <linux/firmware.h>
9#include <linux/module.h>
10#include <linux/pci.h>
11#include <linux/intel-ish-client-if.h>
12#include <linux/property.h>
13#include <asm/cacheflush.h>
14
15
16#define MAX_LOAD_ATTEMPTS 3
17
18
19#define LOADER_CL_RX_RING_SIZE 1
20#define LOADER_CL_TX_RING_SIZE 1
21
22
23
24
25
26
27
28
29
30#define LOADER_SHIM_IPC_BUF_SIZE 3968
31
32
33
34
35
36
37
38
39
40
41
42enum ish_loader_commands {
43 LOADER_CMD_XFER_QUERY = 0,
44 LOADER_CMD_XFER_FRAGMENT,
45 LOADER_CMD_START,
46};
47
48
49#define CMD_MASK GENMASK(6, 0)
50#define IS_RESPONSE BIT(7)
51
52
53
54
55
56#define ISHTP_SEND_TIMEOUT (3 * HZ)
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75#define LOADER_XFER_MODE_DIRECT_DMA BIT(0)
76#define LOADER_XFER_MODE_ISHTP BIT(1)
77
78
79static const guid_t loader_ishtp_guid =
80 GUID_INIT(0xc804d06a, 0x55bd, 0x4ea7,
81 0xad, 0xed, 0x1e, 0x31, 0x22, 0x8c, 0x76, 0xdc);
82
83#define FILENAME_SIZE 256
84
85
86
87
88
89
90
91
92
93static int dma_buf_size_limit = 4 * PAGE_SIZE;
94
95
96
97
98
99
100
101
102
103
104
105struct loader_msg_hdr {
106 u8 command;
107 u8 reserved[2];
108 u8 status;
109} __packed;
110
111struct loader_xfer_query {
112 struct loader_msg_hdr hdr;
113 u32 image_size;
114} __packed;
115
116struct ish_fw_version {
117 u16 major;
118 u16 minor;
119 u16 hotfix;
120 u16 build;
121} __packed;
122
123union loader_version {
124 u32 value;
125 struct {
126 u8 major;
127 u8 minor;
128 u8 hotfix;
129 u8 build;
130 };
131} __packed;
132
133struct loader_capability {
134 u32 max_fw_image_size;
135 u32 xfer_mode;
136 u32 max_dma_buf_size;
137} __packed;
138
139struct shim_fw_info {
140 struct ish_fw_version ish_fw_version;
141 u32 protocol_version;
142 union loader_version ldr_version;
143 struct loader_capability ldr_capability;
144} __packed;
145
146struct loader_xfer_query_response {
147 struct loader_msg_hdr hdr;
148 struct shim_fw_info fw_info;
149} __packed;
150
151struct loader_xfer_fragment {
152 struct loader_msg_hdr hdr;
153 u32 xfer_mode;
154 u32 offset;
155 u32 size;
156 u32 is_last;
157} __packed;
158
159struct loader_xfer_ipc_fragment {
160 struct loader_xfer_fragment fragment;
161 u8 data[] ____cacheline_aligned;
162} __packed;
163
164struct loader_xfer_dma_fragment {
165 struct loader_xfer_fragment fragment;
166 u64 ddr_phys_addr;
167} __packed;
168
169struct loader_start {
170 struct loader_msg_hdr hdr;
171} __packed;
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190struct response_info {
191 void *data;
192 size_t max_size;
193 size_t size;
194 int error;
195 bool received;
196 wait_queue_head_t wait_queue;
197};
198
199
200
201
202
203
204
205
206
207
208
209struct ishtp_cl_data {
210 struct ishtp_cl *loader_ishtp_cl;
211 struct ishtp_cl_device *cl_device;
212
213
214
215
216
217 struct response_info response;
218
219 struct work_struct work_ishtp_reset;
220 struct work_struct work_fw_load;
221
222
223
224
225
226
227
228
229
230
231
232 bool flag_retry;
233 int retry_count;
234};
235
236#define IPC_FRAGMENT_DATA_PREAMBLE \
237 offsetof(struct loader_xfer_ipc_fragment, data)
238
239#define cl_data_to_dev(client_data) ishtp_device((client_data)->cl_device)
240
241
242
243
244
245
246
247
248
249
250
251static int get_firmware_variant(struct ishtp_cl_data *client_data,
252 char *filename)
253{
254 int rv;
255 const char *val;
256 struct device *devc = ishtp_get_pci_device(client_data->cl_device);
257
258 rv = device_property_read_string(devc, "firmware-name", &val);
259 if (rv < 0) {
260 dev_err(devc,
261 "Error: ISH firmware-name device property required\n");
262 return rv;
263 }
264 return snprintf(filename, FILENAME_SIZE, "intel/%s", val);
265}
266
267
268
269
270
271
272
273
274
275
276
277
278
279static int loader_cl_send(struct ishtp_cl_data *client_data,
280 u8 *out_msg, size_t out_size,
281 u8 *in_msg, size_t in_size)
282{
283 int rv;
284 struct loader_msg_hdr *out_hdr = (struct loader_msg_hdr *)out_msg;
285 struct ishtp_cl *loader_ishtp_cl = client_data->loader_ishtp_cl;
286
287 dev_dbg(cl_data_to_dev(client_data),
288 "%s: command=%02lx is_response=%u status=%02x\n",
289 __func__,
290 out_hdr->command & CMD_MASK,
291 out_hdr->command & IS_RESPONSE ? 1 : 0,
292 out_hdr->status);
293
294
295 client_data->response.data = in_msg;
296 client_data->response.max_size = in_size;
297 client_data->response.error = 0;
298 client_data->response.received = false;
299
300 rv = ishtp_cl_send(loader_ishtp_cl, out_msg, out_size);
301 if (rv < 0) {
302 dev_err(cl_data_to_dev(client_data),
303 "ishtp_cl_send error %d\n", rv);
304 return rv;
305 }
306
307 wait_event_interruptible_timeout(client_data->response.wait_queue,
308 client_data->response.received,
309 ISHTP_SEND_TIMEOUT);
310 if (!client_data->response.received) {
311 dev_err(cl_data_to_dev(client_data),
312 "Timed out for response to command=%02lx",
313 out_hdr->command & CMD_MASK);
314 return -ETIMEDOUT;
315 }
316
317 if (client_data->response.error < 0)
318 return client_data->response.error;
319
320 return client_data->response.size;
321}
322
323
324
325
326
327
328
329
330
331static void process_recv(struct ishtp_cl *loader_ishtp_cl,
332 struct ishtp_cl_rb *rb_in_proc)
333{
334 struct loader_msg_hdr *hdr;
335 size_t data_len = rb_in_proc->buf_idx;
336 struct ishtp_cl_data *client_data =
337 ishtp_get_client_data(loader_ishtp_cl);
338
339
340 if (!client_data->response.data) {
341 dev_err(cl_data_to_dev(client_data),
342 "Receiving buffer is null. Should be allocated by calling function\n");
343 client_data->response.error = -EINVAL;
344 goto end;
345 }
346
347 if (client_data->response.received) {
348 dev_err(cl_data_to_dev(client_data),
349 "Previous firmware message not yet processed\n");
350 client_data->response.error = -EINVAL;
351 goto end;
352 }
353
354
355
356
357 if (!rb_in_proc->buffer.data) {
358 dev_warn(cl_data_to_dev(client_data),
359 "rb_in_proc->buffer.data returned null");
360 client_data->response.error = -EBADMSG;
361 goto end;
362 }
363
364 if (data_len < sizeof(struct loader_msg_hdr)) {
365 dev_err(cl_data_to_dev(client_data),
366 "data size %zu is less than header %zu\n",
367 data_len, sizeof(struct loader_msg_hdr));
368 client_data->response.error = -EMSGSIZE;
369 goto end;
370 }
371
372 hdr = (struct loader_msg_hdr *)rb_in_proc->buffer.data;
373
374 dev_dbg(cl_data_to_dev(client_data),
375 "%s: command=%02lx is_response=%u status=%02x\n",
376 __func__,
377 hdr->command & CMD_MASK,
378 hdr->command & IS_RESPONSE ? 1 : 0,
379 hdr->status);
380
381 if (((hdr->command & CMD_MASK) != LOADER_CMD_XFER_QUERY) &&
382 ((hdr->command & CMD_MASK) != LOADER_CMD_XFER_FRAGMENT) &&
383 ((hdr->command & CMD_MASK) != LOADER_CMD_START)) {
384 dev_err(cl_data_to_dev(client_data),
385 "Invalid command=%02lx\n",
386 hdr->command & CMD_MASK);
387 client_data->response.error = -EPROTO;
388 goto end;
389 }
390
391 if (data_len > client_data->response.max_size) {
392 dev_err(cl_data_to_dev(client_data),
393 "Received buffer size %zu is larger than allocated buffer %zu\n",
394 data_len, client_data->response.max_size);
395 client_data->response.error = -EMSGSIZE;
396 goto end;
397 }
398
399
400 if (!(hdr->command & IS_RESPONSE)) {
401 dev_err(cl_data_to_dev(client_data),
402 "Invalid response to command\n");
403 client_data->response.error = -EIO;
404 goto end;
405 }
406
407 if (hdr->status) {
408 dev_err(cl_data_to_dev(client_data),
409 "Loader returned status %d\n",
410 hdr->status);
411 client_data->response.error = -EIO;
412 goto end;
413 }
414
415
416 client_data->response.size = data_len;
417
418
419
420
421
422 memcpy(client_data->response.data,
423 rb_in_proc->buffer.data, data_len);
424
425
426 client_data->response.received = true;
427
428end:
429
430 ishtp_cl_io_rb_recycle(rb_in_proc);
431 rb_in_proc = NULL;
432
433
434 wake_up_interruptible(&client_data->response.wait_queue);
435}
436
437
438
439
440
441
442
443
444
445static void loader_cl_event_cb(struct ishtp_cl_device *cl_device)
446{
447 struct ishtp_cl_rb *rb_in_proc;
448 struct ishtp_cl *loader_ishtp_cl = ishtp_get_drvdata(cl_device);
449
450 while ((rb_in_proc = ishtp_cl_rx_get_rb(loader_ishtp_cl)) != NULL) {
451
452 process_recv(loader_ishtp_cl, rb_in_proc);
453 }
454}
455
456
457
458
459
460
461
462
463
464
465
466static int ish_query_loader_prop(struct ishtp_cl_data *client_data,
467 const struct firmware *fw,
468 struct shim_fw_info *fw_info)
469{
470 int rv;
471 struct loader_xfer_query ldr_xfer_query;
472 struct loader_xfer_query_response ldr_xfer_query_resp;
473
474 memset(&ldr_xfer_query, 0, sizeof(ldr_xfer_query));
475 ldr_xfer_query.hdr.command = LOADER_CMD_XFER_QUERY;
476 ldr_xfer_query.image_size = fw->size;
477 rv = loader_cl_send(client_data,
478 (u8 *)&ldr_xfer_query,
479 sizeof(ldr_xfer_query),
480 (u8 *)&ldr_xfer_query_resp,
481 sizeof(ldr_xfer_query_resp));
482 if (rv < 0) {
483 client_data->flag_retry = true;
484 *fw_info = (struct shim_fw_info){};
485 return rv;
486 }
487
488
489 if (rv != sizeof(struct loader_xfer_query_response)) {
490 dev_err(cl_data_to_dev(client_data),
491 "data size %d is not equal to size of loader_xfer_query_response %zu\n",
492 rv, sizeof(struct loader_xfer_query_response));
493 client_data->flag_retry = true;
494 *fw_info = (struct shim_fw_info){};
495 return -EMSGSIZE;
496 }
497
498
499 *fw_info = ldr_xfer_query_resp.fw_info;
500
501
502 dev_dbg(cl_data_to_dev(client_data),
503 "ish_fw_version: major=%d minor=%d hotfix=%d build=%d protocol_version=0x%x loader_version=%d\n",
504 fw_info->ish_fw_version.major,
505 fw_info->ish_fw_version.minor,
506 fw_info->ish_fw_version.hotfix,
507 fw_info->ish_fw_version.build,
508 fw_info->protocol_version,
509 fw_info->ldr_version.value);
510
511 dev_dbg(cl_data_to_dev(client_data),
512 "loader_capability: max_fw_image_size=0x%x xfer_mode=%d max_dma_buf_size=0x%x dma_buf_size_limit=0x%x\n",
513 fw_info->ldr_capability.max_fw_image_size,
514 fw_info->ldr_capability.xfer_mode,
515 fw_info->ldr_capability.max_dma_buf_size,
516 dma_buf_size_limit);
517
518
519 if (fw_info->ldr_capability.max_fw_image_size < fw->size) {
520 dev_err(cl_data_to_dev(client_data),
521 "ISH firmware size %zu is greater than Shim firmware loader max supported %d\n",
522 fw->size,
523 fw_info->ldr_capability.max_fw_image_size);
524 return -ENOSPC;
525 }
526
527
528 if ((fw_info->ldr_capability.xfer_mode & LOADER_XFER_MODE_DIRECT_DMA) &&
529 (fw_info->ldr_capability.max_dma_buf_size % L1_CACHE_BYTES)) {
530 dev_err(cl_data_to_dev(client_data),
531 "Shim firmware loader buffer size %d should be multiple of cacheline\n",
532 fw_info->ldr_capability.max_dma_buf_size);
533 return -EINVAL;
534 }
535
536 return 0;
537}
538
539
540
541
542
543
544
545
546
547
548
549
550static int ish_fw_xfer_ishtp(struct ishtp_cl_data *client_data,
551 const struct firmware *fw)
552{
553 int rv;
554 u32 fragment_offset, fragment_size, payload_max_size;
555 struct loader_xfer_ipc_fragment *ldr_xfer_ipc_frag;
556 struct loader_msg_hdr ldr_xfer_ipc_ack;
557
558 payload_max_size =
559 LOADER_SHIM_IPC_BUF_SIZE - IPC_FRAGMENT_DATA_PREAMBLE;
560
561 ldr_xfer_ipc_frag = kzalloc(LOADER_SHIM_IPC_BUF_SIZE, GFP_KERNEL);
562 if (!ldr_xfer_ipc_frag) {
563 client_data->flag_retry = true;
564 return -ENOMEM;
565 }
566
567 ldr_xfer_ipc_frag->fragment.hdr.command = LOADER_CMD_XFER_FRAGMENT;
568 ldr_xfer_ipc_frag->fragment.xfer_mode = LOADER_XFER_MODE_ISHTP;
569
570
571 fragment_offset = 0;
572 while (fragment_offset < fw->size) {
573 if (fragment_offset + payload_max_size < fw->size) {
574 fragment_size = payload_max_size;
575 ldr_xfer_ipc_frag->fragment.is_last = 0;
576 } else {
577 fragment_size = fw->size - fragment_offset;
578 ldr_xfer_ipc_frag->fragment.is_last = 1;
579 }
580
581 ldr_xfer_ipc_frag->fragment.offset = fragment_offset;
582 ldr_xfer_ipc_frag->fragment.size = fragment_size;
583 memcpy(ldr_xfer_ipc_frag->data,
584 &fw->data[fragment_offset],
585 fragment_size);
586
587 dev_dbg(cl_data_to_dev(client_data),
588 "xfer_mode=ipc offset=0x%08x size=0x%08x is_last=%d\n",
589 ldr_xfer_ipc_frag->fragment.offset,
590 ldr_xfer_ipc_frag->fragment.size,
591 ldr_xfer_ipc_frag->fragment.is_last);
592
593 rv = loader_cl_send(client_data,
594 (u8 *)ldr_xfer_ipc_frag,
595 IPC_FRAGMENT_DATA_PREAMBLE + fragment_size,
596 (u8 *)&ldr_xfer_ipc_ack,
597 sizeof(ldr_xfer_ipc_ack));
598 if (rv < 0) {
599 client_data->flag_retry = true;
600 goto end_err_resp_buf_release;
601 }
602
603 fragment_offset += fragment_size;
604 }
605
606 kfree(ldr_xfer_ipc_frag);
607 return 0;
608
609end_err_resp_buf_release:
610
611 kfree(ldr_xfer_ipc_frag);
612 return rv;
613}
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630static int ish_fw_xfer_direct_dma(struct ishtp_cl_data *client_data,
631 const struct firmware *fw,
632 const struct shim_fw_info fw_info)
633{
634 int rv;
635 void *dma_buf;
636 dma_addr_t dma_buf_phy;
637 u32 fragment_offset, fragment_size, payload_max_size;
638 struct loader_msg_hdr ldr_xfer_dma_frag_ack;
639 struct loader_xfer_dma_fragment ldr_xfer_dma_frag;
640 struct device *devc = ishtp_get_pci_device(client_data->cl_device);
641 u32 shim_fw_buf_size =
642 fw_info.ldr_capability.max_dma_buf_size;
643
644
645
646
647
648
649
650 payload_max_size = min3(fw->size,
651 (size_t)shim_fw_buf_size,
652 (size_t)dma_buf_size_limit);
653
654
655
656
657
658 payload_max_size &= ~(L1_CACHE_BYTES - 1);
659
660 dma_buf = kmalloc(payload_max_size, GFP_KERNEL | GFP_DMA32);
661 if (!dma_buf) {
662 client_data->flag_retry = true;
663 return -ENOMEM;
664 }
665
666 dma_buf_phy = dma_map_single(devc, dma_buf, payload_max_size,
667 DMA_TO_DEVICE);
668 if (dma_mapping_error(devc, dma_buf_phy)) {
669 dev_err(cl_data_to_dev(client_data), "DMA map failed\n");
670 client_data->flag_retry = true;
671 rv = -ENOMEM;
672 goto end_err_dma_buf_release;
673 }
674
675 ldr_xfer_dma_frag.fragment.hdr.command = LOADER_CMD_XFER_FRAGMENT;
676 ldr_xfer_dma_frag.fragment.xfer_mode = LOADER_XFER_MODE_DIRECT_DMA;
677 ldr_xfer_dma_frag.ddr_phys_addr = (u64)dma_buf_phy;
678
679
680 fragment_offset = 0;
681 while (fragment_offset < fw->size) {
682 if (fragment_offset + payload_max_size < fw->size) {
683 fragment_size = payload_max_size;
684 ldr_xfer_dma_frag.fragment.is_last = 0;
685 } else {
686 fragment_size = fw->size - fragment_offset;
687 ldr_xfer_dma_frag.fragment.is_last = 1;
688 }
689
690 ldr_xfer_dma_frag.fragment.offset = fragment_offset;
691 ldr_xfer_dma_frag.fragment.size = fragment_size;
692 memcpy(dma_buf, &fw->data[fragment_offset], fragment_size);
693
694 dma_sync_single_for_device(devc, dma_buf_phy,
695 payload_max_size,
696 DMA_TO_DEVICE);
697
698
699
700
701
702 clflush_cache_range(dma_buf, payload_max_size);
703
704 dev_dbg(cl_data_to_dev(client_data),
705 "xfer_mode=dma offset=0x%08x size=0x%x is_last=%d ddr_phys_addr=0x%016llx\n",
706 ldr_xfer_dma_frag.fragment.offset,
707 ldr_xfer_dma_frag.fragment.size,
708 ldr_xfer_dma_frag.fragment.is_last,
709 ldr_xfer_dma_frag.ddr_phys_addr);
710
711 rv = loader_cl_send(client_data,
712 (u8 *)&ldr_xfer_dma_frag,
713 sizeof(ldr_xfer_dma_frag),
714 (u8 *)&ldr_xfer_dma_frag_ack,
715 sizeof(ldr_xfer_dma_frag_ack));
716 if (rv < 0) {
717 client_data->flag_retry = true;
718 goto end_err_resp_buf_release;
719 }
720
721 fragment_offset += fragment_size;
722 }
723
724 dma_unmap_single(devc, dma_buf_phy, payload_max_size, DMA_TO_DEVICE);
725 kfree(dma_buf);
726 return 0;
727
728end_err_resp_buf_release:
729
730 dma_unmap_single(devc, dma_buf_phy, payload_max_size, DMA_TO_DEVICE);
731end_err_dma_buf_release:
732 kfree(dma_buf);
733 return rv;
734}
735
736
737
738
739
740
741
742
743
744
745static int ish_fw_start(struct ishtp_cl_data *client_data)
746{
747 struct loader_start ldr_start;
748 struct loader_msg_hdr ldr_start_ack;
749
750 memset(&ldr_start, 0, sizeof(ldr_start));
751 ldr_start.hdr.command = LOADER_CMD_START;
752 return loader_cl_send(client_data,
753 (u8 *)&ldr_start,
754 sizeof(ldr_start),
755 (u8 *)&ldr_start_ack,
756 sizeof(ldr_start_ack));
757}
758
759
760
761
762
763
764
765
766
767static int load_fw_from_host(struct ishtp_cl_data *client_data)
768{
769 int rv;
770 u32 xfer_mode;
771 char *filename;
772 const struct firmware *fw;
773 struct shim_fw_info fw_info;
774 struct ishtp_cl *loader_ishtp_cl = client_data->loader_ishtp_cl;
775
776 client_data->flag_retry = false;
777
778 filename = kzalloc(FILENAME_SIZE, GFP_KERNEL);
779 if (!filename) {
780 client_data->flag_retry = true;
781 rv = -ENOMEM;
782 goto end_error;
783 }
784
785
786 rv = get_firmware_variant(client_data, filename);
787 if (rv < 0)
788 goto end_err_filename_buf_release;
789
790 rv = request_firmware(&fw, filename, cl_data_to_dev(client_data));
791 if (rv < 0)
792 goto end_err_filename_buf_release;
793
794
795
796 rv = ish_query_loader_prop(client_data, fw, &fw_info);
797 if (rv < 0)
798 goto end_err_fw_release;
799
800
801
802 xfer_mode = fw_info.ldr_capability.xfer_mode;
803 if (xfer_mode & LOADER_XFER_MODE_DIRECT_DMA) {
804 rv = ish_fw_xfer_direct_dma(client_data, fw, fw_info);
805 } else if (xfer_mode & LOADER_XFER_MODE_ISHTP) {
806 rv = ish_fw_xfer_ishtp(client_data, fw);
807 } else {
808 dev_err(cl_data_to_dev(client_data),
809 "No transfer mode selected in firmware\n");
810 rv = -EINVAL;
811 }
812 if (rv < 0)
813 goto end_err_fw_release;
814
815
816
817 rv = ish_fw_start(client_data);
818 if (rv < 0)
819 goto end_err_fw_release;
820
821 release_firmware(fw);
822 dev_info(cl_data_to_dev(client_data), "ISH firmware %s loaded\n",
823 filename);
824 kfree(filename);
825 return 0;
826
827end_err_fw_release:
828 release_firmware(fw);
829end_err_filename_buf_release:
830 kfree(filename);
831end_error:
832
833 if (client_data->flag_retry &&
834 client_data->retry_count++ < MAX_LOAD_ATTEMPTS) {
835 dev_warn(cl_data_to_dev(client_data),
836 "ISH host firmware load failed %d. Resetting ISH, and trying again..\n",
837 rv);
838 ish_hw_reset(ishtp_get_ishtp_device(loader_ishtp_cl));
839 } else {
840 dev_err(cl_data_to_dev(client_data),
841 "ISH host firmware load failed %d\n", rv);
842 }
843 return rv;
844}
845
846static void load_fw_from_host_handler(struct work_struct *work)
847{
848 struct ishtp_cl_data *client_data;
849
850 client_data = container_of(work, struct ishtp_cl_data,
851 work_fw_load);
852 load_fw_from_host(client_data);
853}
854
855
856
857
858
859
860
861
862static int loader_init(struct ishtp_cl *loader_ishtp_cl, int reset)
863{
864 int rv;
865 struct ishtp_fw_client *fw_client;
866 struct ishtp_cl_data *client_data =
867 ishtp_get_client_data(loader_ishtp_cl);
868
869 dev_dbg(cl_data_to_dev(client_data), "reset flag: %d\n", reset);
870
871 rv = ishtp_cl_link(loader_ishtp_cl);
872 if (rv < 0) {
873 dev_err(cl_data_to_dev(client_data), "ishtp_cl_link failed\n");
874 return rv;
875 }
876
877
878 ishtp_set_tx_ring_size(loader_ishtp_cl, LOADER_CL_TX_RING_SIZE);
879 ishtp_set_rx_ring_size(loader_ishtp_cl, LOADER_CL_RX_RING_SIZE);
880
881 fw_client =
882 ishtp_fw_cl_get_client(ishtp_get_ishtp_device(loader_ishtp_cl),
883 &loader_ishtp_guid);
884 if (!fw_client) {
885 dev_err(cl_data_to_dev(client_data),
886 "ISH client uuid not found\n");
887 rv = -ENOENT;
888 goto err_cl_unlink;
889 }
890
891 ishtp_cl_set_fw_client_id(loader_ishtp_cl,
892 ishtp_get_fw_client_id(fw_client));
893 ishtp_set_connection_state(loader_ishtp_cl, ISHTP_CL_CONNECTING);
894
895 rv = ishtp_cl_connect(loader_ishtp_cl);
896 if (rv < 0) {
897 dev_err(cl_data_to_dev(client_data), "Client connect fail\n");
898 goto err_cl_unlink;
899 }
900
901 dev_dbg(cl_data_to_dev(client_data), "Client connected\n");
902
903 ishtp_register_event_cb(client_data->cl_device, loader_cl_event_cb);
904
905 return 0;
906
907err_cl_unlink:
908 ishtp_cl_unlink(loader_ishtp_cl);
909 return rv;
910}
911
912static void loader_deinit(struct ishtp_cl *loader_ishtp_cl)
913{
914 ishtp_set_connection_state(loader_ishtp_cl, ISHTP_CL_DISCONNECTING);
915 ishtp_cl_disconnect(loader_ishtp_cl);
916 ishtp_cl_unlink(loader_ishtp_cl);
917 ishtp_cl_flush_queues(loader_ishtp_cl);
918
919
920 ishtp_cl_free(loader_ishtp_cl);
921}
922
923static void reset_handler(struct work_struct *work)
924{
925 int rv;
926 struct ishtp_cl_data *client_data;
927 struct ishtp_cl *loader_ishtp_cl;
928 struct ishtp_cl_device *cl_device;
929
930 client_data = container_of(work, struct ishtp_cl_data,
931 work_ishtp_reset);
932
933 loader_ishtp_cl = client_data->loader_ishtp_cl;
934 cl_device = client_data->cl_device;
935
936
937 ishtp_cl_unlink(loader_ishtp_cl);
938 ishtp_cl_flush_queues(loader_ishtp_cl);
939 ishtp_cl_free(loader_ishtp_cl);
940
941 loader_ishtp_cl = ishtp_cl_allocate(cl_device);
942 if (!loader_ishtp_cl)
943 return;
944
945 ishtp_set_drvdata(cl_device, loader_ishtp_cl);
946 ishtp_set_client_data(loader_ishtp_cl, client_data);
947 client_data->loader_ishtp_cl = loader_ishtp_cl;
948 client_data->cl_device = cl_device;
949
950 rv = loader_init(loader_ishtp_cl, 1);
951 if (rv < 0) {
952 dev_err(ishtp_device(cl_device), "Reset Failed\n");
953 return;
954 }
955
956
957 load_fw_from_host(client_data);
958}
959
960
961
962
963
964
965
966
967
968static int loader_ishtp_cl_probe(struct ishtp_cl_device *cl_device)
969{
970 struct ishtp_cl *loader_ishtp_cl;
971 struct ishtp_cl_data *client_data;
972 int rv;
973
974 client_data = devm_kzalloc(ishtp_device(cl_device),
975 sizeof(*client_data),
976 GFP_KERNEL);
977 if (!client_data)
978 return -ENOMEM;
979
980 loader_ishtp_cl = ishtp_cl_allocate(cl_device);
981 if (!loader_ishtp_cl)
982 return -ENOMEM;
983
984 ishtp_set_drvdata(cl_device, loader_ishtp_cl);
985 ishtp_set_client_data(loader_ishtp_cl, client_data);
986 client_data->loader_ishtp_cl = loader_ishtp_cl;
987 client_data->cl_device = cl_device;
988
989 init_waitqueue_head(&client_data->response.wait_queue);
990
991 INIT_WORK(&client_data->work_ishtp_reset,
992 reset_handler);
993 INIT_WORK(&client_data->work_fw_load,
994 load_fw_from_host_handler);
995
996 rv = loader_init(loader_ishtp_cl, 0);
997 if (rv < 0) {
998 ishtp_cl_free(loader_ishtp_cl);
999 return rv;
1000 }
1001 ishtp_get_device(cl_device);
1002
1003 client_data->retry_count = 0;
1004
1005
1006 schedule_work(&client_data->work_fw_load);
1007
1008 return 0;
1009}
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019static void loader_ishtp_cl_remove(struct ishtp_cl_device *cl_device)
1020{
1021 struct ishtp_cl_data *client_data;
1022 struct ishtp_cl *loader_ishtp_cl = ishtp_get_drvdata(cl_device);
1023
1024 client_data = ishtp_get_client_data(loader_ishtp_cl);
1025
1026
1027
1028
1029
1030
1031
1032 cancel_work_sync(&client_data->work_fw_load);
1033 cancel_work_sync(&client_data->work_ishtp_reset);
1034 loader_deinit(loader_ishtp_cl);
1035 ishtp_put_device(cl_device);
1036}
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046static int loader_ishtp_cl_reset(struct ishtp_cl_device *cl_device)
1047{
1048 struct ishtp_cl_data *client_data;
1049 struct ishtp_cl *loader_ishtp_cl = ishtp_get_drvdata(cl_device);
1050
1051 client_data = ishtp_get_client_data(loader_ishtp_cl);
1052
1053 schedule_work(&client_data->work_ishtp_reset);
1054
1055 return 0;
1056}
1057
1058static struct ishtp_cl_driver loader_ishtp_cl_driver = {
1059 .name = "ish-loader",
1060 .guid = &loader_ishtp_guid,
1061 .probe = loader_ishtp_cl_probe,
1062 .remove = loader_ishtp_cl_remove,
1063 .reset = loader_ishtp_cl_reset,
1064};
1065
1066static int __init ish_loader_init(void)
1067{
1068 return ishtp_cl_driver_register(&loader_ishtp_cl_driver, THIS_MODULE);
1069}
1070
1071static void __exit ish_loader_exit(void)
1072{
1073 ishtp_cl_driver_unregister(&loader_ishtp_cl_driver);
1074}
1075
1076late_initcall(ish_loader_init);
1077module_exit(ish_loader_exit);
1078
1079module_param(dma_buf_size_limit, int, 0644);
1080MODULE_PARM_DESC(dma_buf_size_limit, "Limit the DMA buf size to this value in bytes");
1081
1082MODULE_DESCRIPTION("ISH ISH-TP Host firmware Loader Client Driver");
1083MODULE_AUTHOR("Rushikesh S Kadam <rushikesh.s.kadam@intel.com>");
1084
1085MODULE_LICENSE("GPL v2");
1086MODULE_ALIAS("ishtp:*");
1087