1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include <linux/types.h>
30#include <linux/module.h>
31#include <linux/sched.h>
32#include <linux/wait.h>
33#include <linux/pci.h>
34#include <linux/string.h>
35#include <linux/dma-mapping.h>
36#include <linux/delay.h>
37#include <linux/module.h>
38#include <linux/interrupt.h>
39#include <linux/crc-itu-t.h>
40
41#include "card_base.h"
42#include "card_ddcb.h"
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91static int queue_empty(struct ddcb_queue *queue)
92{
93 return queue->ddcb_next == queue->ddcb_act;
94}
95
96static int queue_enqueued_ddcbs(struct ddcb_queue *queue)
97{
98 if (queue->ddcb_next >= queue->ddcb_act)
99 return queue->ddcb_next - queue->ddcb_act;
100
101 return queue->ddcb_max - (queue->ddcb_act - queue->ddcb_next);
102}
103
104static int queue_free_ddcbs(struct ddcb_queue *queue)
105{
106 int free_ddcbs = queue->ddcb_max - queue_enqueued_ddcbs(queue) - 1;
107
108 if (WARN_ON_ONCE(free_ddcbs < 0)) {
109 return 0;
110 }
111 return free_ddcbs;
112}
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130static inline void ddcb_mark_tapped(struct ddcb *pddcb)
131{
132 pddcb->priv[7] = 0xbb;
133}
134
135static inline void ddcb_mark_appended(struct ddcb *pddcb)
136{
137 pddcb->priv[7] = 0xaa;
138}
139
140static inline void ddcb_mark_cleared(struct ddcb *pddcb)
141{
142 pddcb->priv[6] = 0xcc;
143}
144
145static inline void ddcb_mark_finished(struct ddcb *pddcb)
146{
147 pddcb->priv[6] = 0xff;
148}
149
150static inline void ddcb_mark_unused(struct ddcb *pddcb)
151{
152 pddcb->priv_64 = cpu_to_be64(0);
153}
154
155
156
157
158
159
160
161
162
163
164
165
166
167static inline u16 genwqe_crc16(const u8 *buff, size_t len, u16 init)
168{
169 return crc_itu_t(init, buff, len);
170}
171
172static void print_ddcb_info(struct genwqe_dev *cd, struct ddcb_queue *queue)
173{
174 int i;
175 struct ddcb *pddcb;
176 unsigned long flags;
177 struct pci_dev *pci_dev = cd->pci_dev;
178
179 spin_lock_irqsave(&cd->print_lock, flags);
180
181 dev_info(&pci_dev->dev,
182 "DDCB list for card #%d (ddcb_act=%d / ddcb_next=%d):\n",
183 cd->card_idx, queue->ddcb_act, queue->ddcb_next);
184
185 pddcb = queue->ddcb_vaddr;
186 for (i = 0; i < queue->ddcb_max; i++) {
187 dev_err(&pci_dev->dev,
188 " %c %-3d: RETC=%03x SEQ=%04x "
189 "HSI=%02X SHI=%02x PRIV=%06llx CMD=%03x\n",
190 i == queue->ddcb_act ? '>' : ' ',
191 i,
192 be16_to_cpu(pddcb->retc_16),
193 be16_to_cpu(pddcb->seqnum_16),
194 pddcb->hsi,
195 pddcb->shi,
196 be64_to_cpu(pddcb->priv_64),
197 pddcb->cmd);
198 pddcb++;
199 }
200 spin_unlock_irqrestore(&cd->print_lock, flags);
201}
202
203struct genwqe_ddcb_cmd *ddcb_requ_alloc(void)
204{
205 struct ddcb_requ *req;
206
207 req = kzalloc(sizeof(*req), GFP_ATOMIC);
208 if (!req)
209 return NULL;
210
211 return &req->cmd;
212}
213
214void ddcb_requ_free(struct genwqe_ddcb_cmd *cmd)
215{
216 struct ddcb_requ *req = container_of(cmd, struct ddcb_requ, cmd);
217 kfree(req);
218}
219
220static inline enum genwqe_requ_state ddcb_requ_get_state(struct ddcb_requ *req)
221{
222 return req->req_state;
223}
224
225static inline void ddcb_requ_set_state(struct ddcb_requ *req,
226 enum genwqe_requ_state new_state)
227{
228 req->req_state = new_state;
229}
230
231static inline int ddcb_requ_collect_debug_data(struct ddcb_requ *req)
232{
233 return req->cmd.ddata_addr != 0x0;
234}
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250static int ddcb_requ_finished(struct genwqe_dev *cd, struct ddcb_requ *req)
251{
252 return (ddcb_requ_get_state(req) == GENWQE_REQU_FINISHED) ||
253 (cd->card_state != GENWQE_CARD_USED);
254}
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271#define RET_DDCB_APPENDED 1
272#define RET_DDCB_TAPPED 2
273
274static int enqueue_ddcb(struct genwqe_dev *cd, struct ddcb_queue *queue,
275 struct ddcb *pddcb, int ddcb_no)
276{
277 unsigned int try;
278 int prev_no;
279 struct ddcb *prev_ddcb;
280 __be32 old, new, icrc_hsi_shi;
281 u64 num;
282
283
284
285
286
287
288 ddcb_mark_unused(pddcb);
289
290
291 prev_no = (ddcb_no == 0) ? queue->ddcb_max - 1 : ddcb_no - 1;
292 prev_ddcb = &queue->ddcb_vaddr[prev_no];
293
294
295
296
297
298
299 ddcb_mark_appended(pddcb);
300 for (try = 0; try < 2; try++) {
301 old = prev_ddcb->icrc_hsi_shi_32;
302
303
304 if ((old & DDCB_COMPLETED_BE32) != 0x00000000)
305 break;
306
307 new = (old | DDCB_NEXT_BE32);
308
309 wmb();
310 icrc_hsi_shi = cmpxchg(&prev_ddcb->icrc_hsi_shi_32, old, new);
311
312 if (icrc_hsi_shi == old)
313 return RET_DDCB_APPENDED;
314 }
315
316
317 ddcb_mark_tapped(pddcb);
318 num = (u64)ddcb_no << 8;
319
320 wmb();
321 __genwqe_writeq(cd, queue->IO_QUEUE_OFFSET, num);
322
323 return RET_DDCB_TAPPED;
324}
325
326
327
328
329
330
331
332
333
334
335
336
337static void copy_ddcb_results(struct ddcb_requ *req, int ddcb_no)
338{
339 struct ddcb_queue *queue = req->queue;
340 struct ddcb *pddcb = &queue->ddcb_vaddr[req->num];
341
342 memcpy(&req->cmd.asv[0], &pddcb->asv[0], DDCB_ASV_LENGTH);
343
344
345 req->cmd.vcrc = be16_to_cpu(pddcb->vcrc_16);
346 req->cmd.deque_ts = be64_to_cpu(pddcb->deque_ts_64);
347 req->cmd.cmplt_ts = be64_to_cpu(pddcb->cmplt_ts_64);
348
349 req->cmd.attn = be16_to_cpu(pddcb->attn_16);
350 req->cmd.progress = be32_to_cpu(pddcb->progress_32);
351 req->cmd.retc = be16_to_cpu(pddcb->retc_16);
352
353 if (ddcb_requ_collect_debug_data(req)) {
354 int prev_no = (ddcb_no == 0) ?
355 queue->ddcb_max - 1 : ddcb_no - 1;
356 struct ddcb *prev_pddcb = &queue->ddcb_vaddr[prev_no];
357
358 memcpy(&req->debug_data.ddcb_finished, pddcb,
359 sizeof(req->debug_data.ddcb_finished));
360 memcpy(&req->debug_data.ddcb_prev, prev_pddcb,
361 sizeof(req->debug_data.ddcb_prev));
362 }
363}
364
365
366
367
368
369
370
371static int genwqe_check_ddcb_queue(struct genwqe_dev *cd,
372 struct ddcb_queue *queue)
373{
374 unsigned long flags;
375 int ddcbs_finished = 0;
376 struct pci_dev *pci_dev = cd->pci_dev;
377
378 spin_lock_irqsave(&queue->ddcb_lock, flags);
379
380
381 while (!queue_empty(queue) && (ddcbs_finished < queue->ddcb_max)) {
382
383 struct ddcb *pddcb;
384 struct ddcb_requ *req;
385 u16 vcrc, vcrc_16, retc_16;
386
387 pddcb = &queue->ddcb_vaddr[queue->ddcb_act];
388
389 if ((pddcb->icrc_hsi_shi_32 & DDCB_COMPLETED_BE32) ==
390 0x00000000)
391 goto go_home;
392
393
394
395 req = queue->ddcb_req[queue->ddcb_act];
396 if (req == NULL) {
397
398
399 goto pick_next_one;
400 }
401
402
403
404
405
406
407
408
409
410 retc_16 = be16_to_cpu(pddcb->retc_16);
411 if ((pddcb->hsi == 0x44) && (retc_16 <= 0x101)) {
412 u64 errcnts, status;
413 u64 ddcb_offs = (u64)pddcb - (u64)queue->ddcb_vaddr;
414
415 errcnts = __genwqe_readq(cd, queue->IO_QUEUE_ERRCNTS);
416 status = __genwqe_readq(cd, queue->IO_QUEUE_STATUS);
417
418 dev_err(&pci_dev->dev,
419 "[%s] SEQN=%04x HSI=%02x RETC=%03x "
420 " Q_ERRCNTS=%016llx Q_STATUS=%016llx\n"
421 " DDCB_DMA_ADDR=%016llx\n",
422 __func__, be16_to_cpu(pddcb->seqnum_16),
423 pddcb->hsi, retc_16, errcnts, status,
424 queue->ddcb_daddr + ddcb_offs);
425 }
426
427 copy_ddcb_results(req, queue->ddcb_act);
428 queue->ddcb_req[queue->ddcb_act] = NULL;
429
430 dev_dbg(&pci_dev->dev, "FINISHED DDCB#%d\n", req->num);
431 genwqe_hexdump(pci_dev, pddcb, sizeof(*pddcb));
432
433 ddcb_mark_finished(pddcb);
434
435
436 vcrc = genwqe_crc16(pddcb->asv,
437 VCRC_LENGTH(req->cmd.asv_length),
438 0xffff);
439 vcrc_16 = be16_to_cpu(pddcb->vcrc_16);
440 if (vcrc != vcrc_16) {
441 printk_ratelimited(KERN_ERR
442 "%s %s: err: wrong VCRC pre=%02x vcrc_len=%d "
443 "bytes vcrc_data=%04x is not vcrc_card=%04x\n",
444 GENWQE_DEVNAME, dev_name(&pci_dev->dev),
445 pddcb->pre, VCRC_LENGTH(req->cmd.asv_length),
446 vcrc, vcrc_16);
447 }
448
449 ddcb_requ_set_state(req, GENWQE_REQU_FINISHED);
450 queue->ddcbs_completed++;
451 queue->ddcbs_in_flight--;
452
453
454 wake_up_interruptible(&queue->ddcb_waitqs[queue->ddcb_act]);
455
456pick_next_one:
457 queue->ddcb_act = (queue->ddcb_act + 1) % queue->ddcb_max;
458 ddcbs_finished++;
459 }
460
461 go_home:
462 spin_unlock_irqrestore(&queue->ddcb_lock, flags);
463 return ddcbs_finished;
464}
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483int __genwqe_wait_ddcb(struct genwqe_dev *cd, struct ddcb_requ *req)
484{
485 int rc;
486 unsigned int ddcb_no;
487 struct ddcb_queue *queue;
488 struct pci_dev *pci_dev = cd->pci_dev;
489
490 if (req == NULL)
491 return -EINVAL;
492
493 queue = req->queue;
494 if (queue == NULL)
495 return -EINVAL;
496
497 ddcb_no = req->num;
498 if (ddcb_no >= queue->ddcb_max)
499 return -EINVAL;
500
501 rc = wait_event_interruptible_timeout(queue->ddcb_waitqs[ddcb_no],
502 ddcb_requ_finished(cd, req),
503 genwqe_ddcb_software_timeout * HZ);
504
505
506
507
508
509
510
511 if (rc == 0) {
512 struct ddcb_queue *queue = req->queue;
513 struct ddcb *pddcb;
514
515
516
517
518
519
520 genwqe_check_ddcb_queue(cd, req->queue);
521 if (ddcb_requ_finished(cd, req))
522 return rc;
523
524 dev_err(&pci_dev->dev,
525 "[%s] err: DDCB#%d timeout rc=%d state=%d req @ %p\n",
526 __func__, req->num, rc, ddcb_requ_get_state(req),
527 req);
528 dev_err(&pci_dev->dev,
529 "[%s] IO_QUEUE_STATUS=0x%016llx\n", __func__,
530 __genwqe_readq(cd, queue->IO_QUEUE_STATUS));
531
532 pddcb = &queue->ddcb_vaddr[req->num];
533 genwqe_hexdump(pci_dev, pddcb, sizeof(*pddcb));
534
535 print_ddcb_info(cd, req->queue);
536 return -ETIMEDOUT;
537
538 } else if (rc == -ERESTARTSYS) {
539 return rc;
540
541
542
543
544
545 } else if (rc < 0) {
546 dev_err(&pci_dev->dev,
547 "[%s] err: DDCB#%d unknown result (rc=%d) %d!\n",
548 __func__, req->num, rc, ddcb_requ_get_state(req));
549 return -EINVAL;
550 }
551
552
553 if (cd->card_state != GENWQE_CARD_USED) {
554 dev_err(&pci_dev->dev,
555 "[%s] err: DDCB#%d forced to stop (rc=%d)\n",
556 __func__, req->num, rc);
557 return -EIO;
558 }
559 return rc;
560}
561
562
563
564
565
566
567
568
569
570
571static struct ddcb *get_next_ddcb(struct genwqe_dev *cd,
572 struct ddcb_queue *queue,
573 int *num)
574{
575 u64 *pu64;
576 struct ddcb *pddcb;
577
578 if (queue_free_ddcbs(queue) == 0)
579 return NULL;
580
581
582 pddcb = &queue->ddcb_vaddr[queue->ddcb_next];
583
584
585
586 if ((pddcb->icrc_hsi_shi_32 & DDCB_COMPLETED_BE32) == 0x00000000)
587 return NULL;
588
589 *num = queue->ddcb_next;
590 queue->ddcb_next = (queue->ddcb_next + 1) % queue->ddcb_max;
591
592
593 pu64 = (u64 *)pddcb;
594 pu64[0] = 0ULL;
595 pu64[1] = 0ULL;
596
597
598 pu64[0x80/8] = 0ULL;
599 pu64[0x88/8] = 0ULL;
600 pu64[0x90/8] = 0ULL;
601 pu64[0x98/8] = 0ULL;
602 pu64[0xd0/8] = 0ULL;
603
604 pddcb->pre = DDCB_PRESET_PRE;
605 pddcb->seqnum_16 = cpu_to_be16(queue->ddcb_seq++);
606 return pddcb;
607}
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624int __genwqe_purge_ddcb(struct genwqe_dev *cd, struct ddcb_requ *req)
625{
626 struct ddcb *pddcb = NULL;
627 unsigned int t;
628 unsigned long flags;
629 struct ddcb_queue *queue = req->queue;
630 struct pci_dev *pci_dev = cd->pci_dev;
631 u64 queue_status;
632 __be32 icrc_hsi_shi = 0x0000;
633 __be32 old, new;
634
635
636 if (genwqe_ddcb_software_timeout <= 0) {
637 dev_err(&pci_dev->dev,
638 "[%s] err: software timeout is not set!\n", __func__);
639 return -EFAULT;
640 }
641
642 pddcb = &queue->ddcb_vaddr[req->num];
643
644 for (t = 0; t < genwqe_ddcb_software_timeout * 10; t++) {
645
646 spin_lock_irqsave(&queue->ddcb_lock, flags);
647
648
649 if (ddcb_requ_get_state(req) == GENWQE_REQU_FINISHED)
650 goto go_home;
651
652
653 old = pddcb->icrc_hsi_shi_32;
654 if ((old & DDCB_FETCHED_BE32) == 0x00000000) {
655
656 new = (old | DDCB_PURGE_BE32);
657 icrc_hsi_shi = cmpxchg(&pddcb->icrc_hsi_shi_32,
658 old, new);
659 if (icrc_hsi_shi == old)
660 goto finish_ddcb;
661 }
662
663
664 barrier();
665 icrc_hsi_shi = pddcb->icrc_hsi_shi_32;
666 if (icrc_hsi_shi & DDCB_COMPLETED_BE32)
667 goto finish_ddcb;
668
669 spin_unlock_irqrestore(&queue->ddcb_lock, flags);
670
671
672
673
674
675
676
677
678 copy_ddcb_results(req, req->num);
679 msleep(100);
680 continue;
681
682finish_ddcb:
683 copy_ddcb_results(req, req->num);
684 ddcb_requ_set_state(req, GENWQE_REQU_FINISHED);
685 queue->ddcbs_in_flight--;
686 queue->ddcb_req[req->num] = NULL;
687 ddcb_mark_cleared(pddcb);
688
689
690
691
692
693
694
695
696
697
698
699 icrc_hsi_shi = pddcb->icrc_hsi_shi_32;
700 if ((icrc_hsi_shi & DDCB_COMPLETED_BE32) &&
701 (queue->ddcb_act == req->num)) {
702 queue->ddcb_act = ((queue->ddcb_act + 1) %
703 queue->ddcb_max);
704 }
705go_home:
706 spin_unlock_irqrestore(&queue->ddcb_lock, flags);
707 return 0;
708 }
709
710
711
712
713
714 queue_status = __genwqe_readq(cd, queue->IO_QUEUE_STATUS);
715
716 dev_dbg(&pci_dev->dev, "UN/FINISHED DDCB#%d\n", req->num);
717 genwqe_hexdump(pci_dev, pddcb, sizeof(*pddcb));
718
719 dev_err(&pci_dev->dev,
720 "[%s] err: DDCB#%d not purged and not completed "
721 "after %d seconds QSTAT=%016llx!!\n",
722 __func__, req->num, genwqe_ddcb_software_timeout,
723 queue_status);
724
725 print_ddcb_info(cd, req->queue);
726
727 return -EFAULT;
728}
729
730int genwqe_init_debug_data(struct genwqe_dev *cd, struct genwqe_debug_data *d)
731{
732 int len;
733 struct pci_dev *pci_dev = cd->pci_dev;
734
735 if (d == NULL) {
736 dev_err(&pci_dev->dev,
737 "[%s] err: invalid memory for debug data!\n",
738 __func__);
739 return -EFAULT;
740 }
741
742 len = sizeof(d->driver_version);
743 snprintf(d->driver_version, len, "%s", DRV_VERS_STRING);
744 d->slu_unitcfg = cd->slu_unitcfg;
745 d->app_unitcfg = cd->app_unitcfg;
746 return 0;
747}
748
749
750
751
752
753
754
755
756
757
758int __genwqe_enqueue_ddcb(struct genwqe_dev *cd, struct ddcb_requ *req)
759{
760 struct ddcb *pddcb;
761 unsigned long flags;
762 struct ddcb_queue *queue;
763 struct pci_dev *pci_dev = cd->pci_dev;
764 u16 icrc;
765
766 if (cd->card_state != GENWQE_CARD_USED) {
767 printk_ratelimited(KERN_ERR
768 "%s %s: [%s] Card is unusable/PCIe problem Req#%d\n",
769 GENWQE_DEVNAME, dev_name(&pci_dev->dev),
770 __func__, req->num);
771 return -EIO;
772 }
773
774 queue = req->queue = &cd->queue;
775
776
777
778
779 if (genwqe_polling_enabled)
780 genwqe_check_ddcb_queue(cd, queue);
781
782
783
784
785
786
787 spin_lock_irqsave(&queue->ddcb_lock, flags);
788
789 pddcb = get_next_ddcb(cd, queue, &req->num);
790 if (pddcb == NULL) {
791 spin_unlock_irqrestore(&queue->ddcb_lock, flags);
792 queue->busy++;
793 return -EBUSY;
794 }
795
796 if (queue->ddcb_req[req->num] != NULL) {
797 spin_unlock_irqrestore(&queue->ddcb_lock, flags);
798
799 dev_err(&pci_dev->dev,
800 "[%s] picked DDCB %d with req=%p still in use!!\n",
801 __func__, req->num, req);
802 return -EFAULT;
803 }
804 ddcb_requ_set_state(req, GENWQE_REQU_ENQUEUED);
805 queue->ddcb_req[req->num] = req;
806
807 pddcb->cmdopts_16 = cpu_to_be16(req->cmd.cmdopts);
808 pddcb->cmd = req->cmd.cmd;
809 pddcb->acfunc = req->cmd.acfunc;
810
811
812
813
814
815
816
817
818
819 if ((cd->slu_unitcfg & 0xFFFF0ull) > 0x34199ull)
820 pddcb->xdir = 0x1;
821 else
822 pddcb->xdir = 0x0;
823
824
825 pddcb->psp = (((req->cmd.asiv_length / 8) << 4) |
826 ((req->cmd.asv_length / 8)));
827 pddcb->disp_ts_64 = cpu_to_be64(req->cmd.disp_ts);
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842 if (genwqe_get_slu_id(cd) <= 0x2) {
843 memcpy(&pddcb->__asiv[0],
844 &req->cmd.__asiv[0],
845 DDCB_ASIV_LENGTH);
846 } else {
847 pddcb->n.ats_64 = cpu_to_be64(req->cmd.ats);
848 memcpy(&pddcb->n.asiv[0],
849 &req->cmd.asiv[0],
850 DDCB_ASIV_LENGTH_ATS);
851 }
852
853 pddcb->icrc_hsi_shi_32 = cpu_to_be32(0x00000000);
854
855
856
857
858
859 icrc = genwqe_crc16((const u8 *)pddcb,
860 ICRC_LENGTH(req->cmd.asiv_length), 0xffff);
861 pddcb->icrc_hsi_shi_32 = cpu_to_be32((u32)icrc << 16);
862
863
864 if (!genwqe_polling_enabled)
865 pddcb->icrc_hsi_shi_32 |= DDCB_INTR_BE32;
866
867 dev_dbg(&pci_dev->dev, "INPUT DDCB#%d\n", req->num);
868 genwqe_hexdump(pci_dev, pddcb, sizeof(*pddcb));
869
870 if (ddcb_requ_collect_debug_data(req)) {
871
872
873
874 genwqe_init_debug_data(cd, &req->debug_data);
875 memcpy(&req->debug_data.ddcb_before, pddcb,
876 sizeof(req->debug_data.ddcb_before));
877 }
878
879 enqueue_ddcb(cd, queue, pddcb, req->num);
880 queue->ddcbs_in_flight++;
881
882 if (queue->ddcbs_in_flight > queue->ddcbs_max_in_flight)
883 queue->ddcbs_max_in_flight = queue->ddcbs_in_flight;
884
885 ddcb_requ_set_state(req, GENWQE_REQU_TAPPED);
886 spin_unlock_irqrestore(&queue->ddcb_lock, flags);
887 wake_up_interruptible(&cd->queue_waitq);
888
889 return 0;
890}
891
892
893
894
895
896
897int __genwqe_execute_raw_ddcb(struct genwqe_dev *cd,
898 struct genwqe_ddcb_cmd *cmd)
899{
900 int rc = 0;
901 struct pci_dev *pci_dev = cd->pci_dev;
902 struct ddcb_requ *req = container_of(cmd, struct ddcb_requ, cmd);
903
904 if (cmd->asiv_length > DDCB_ASIV_LENGTH) {
905 dev_err(&pci_dev->dev, "[%s] err: wrong asiv_length of %d\n",
906 __func__, cmd->asiv_length);
907 return -EINVAL;
908 }
909 if (cmd->asv_length > DDCB_ASV_LENGTH) {
910 dev_err(&pci_dev->dev, "[%s] err: wrong asv_length of %d\n",
911 __func__, cmd->asiv_length);
912 return -EINVAL;
913 }
914 rc = __genwqe_enqueue_ddcb(cd, req);
915 if (rc != 0)
916 return rc;
917
918 rc = __genwqe_wait_ddcb(cd, req);
919 if (rc < 0)
920 goto err_exit;
921
922 if (ddcb_requ_collect_debug_data(req)) {
923 if (copy_to_user((struct genwqe_debug_data __user *)
924 (unsigned long)cmd->ddata_addr,
925 &req->debug_data,
926 sizeof(struct genwqe_debug_data)))
927 return -EFAULT;
928 }
929
930
931
932
933
934
935 if (cmd->retc != DDCB_RETC_COMPLETE) {
936
937
938 rc = -EBADMSG;
939 }
940
941 return rc;
942
943 err_exit:
944 __genwqe_purge_ddcb(cd, req);
945
946 if (ddcb_requ_collect_debug_data(req)) {
947 if (copy_to_user((struct genwqe_debug_data __user *)
948 (unsigned long)cmd->ddata_addr,
949 &req->debug_data,
950 sizeof(struct genwqe_debug_data)))
951 return -EFAULT;
952 }
953 return rc;
954}
955
956
957
958
959
960
961static int genwqe_next_ddcb_ready(struct genwqe_dev *cd)
962{
963 unsigned long flags;
964 struct ddcb *pddcb;
965 struct ddcb_queue *queue = &cd->queue;
966
967 spin_lock_irqsave(&queue->ddcb_lock, flags);
968
969 if (queue_empty(queue)) {
970 spin_unlock_irqrestore(&queue->ddcb_lock, flags);
971 return 0;
972 }
973
974 pddcb = &queue->ddcb_vaddr[queue->ddcb_act];
975 if (pddcb->icrc_hsi_shi_32 & DDCB_COMPLETED_BE32) {
976 spin_unlock_irqrestore(&queue->ddcb_lock, flags);
977 return 1;
978 }
979
980 spin_unlock_irqrestore(&queue->ddcb_lock, flags);
981 return 0;
982}
983
984
985
986
987
988
989
990
991int genwqe_ddcbs_in_flight(struct genwqe_dev *cd)
992{
993 unsigned long flags;
994 int ddcbs_in_flight = 0;
995 struct ddcb_queue *queue = &cd->queue;
996
997 spin_lock_irqsave(&queue->ddcb_lock, flags);
998 ddcbs_in_flight += queue->ddcbs_in_flight;
999 spin_unlock_irqrestore(&queue->ddcb_lock, flags);
1000
1001 return ddcbs_in_flight;
1002}
1003
1004static int setup_ddcb_queue(struct genwqe_dev *cd, struct ddcb_queue *queue)
1005{
1006 int rc, i;
1007 struct ddcb *pddcb;
1008 u64 val64;
1009 unsigned int queue_size;
1010 struct pci_dev *pci_dev = cd->pci_dev;
1011
1012 if (genwqe_ddcb_max < 2)
1013 return -EINVAL;
1014
1015 queue_size = roundup(genwqe_ddcb_max * sizeof(struct ddcb), PAGE_SIZE);
1016
1017 queue->ddcbs_in_flight = 0;
1018 queue->ddcbs_max_in_flight = 0;
1019 queue->ddcbs_completed = 0;
1020 queue->busy = 0;
1021
1022 queue->ddcb_seq = 0x100;
1023 queue->ddcb_max = genwqe_ddcb_max;
1024 queue->ddcb_vaddr = __genwqe_alloc_consistent(cd, queue_size,
1025 &queue->ddcb_daddr);
1026 if (queue->ddcb_vaddr == NULL) {
1027 dev_err(&pci_dev->dev,
1028 "[%s] **err: could not allocate DDCB **\n", __func__);
1029 return -ENOMEM;
1030 }
1031 memset(queue->ddcb_vaddr, 0, queue_size);
1032
1033 queue->ddcb_req = kzalloc(sizeof(struct ddcb_requ *) *
1034 queue->ddcb_max, GFP_KERNEL);
1035 if (!queue->ddcb_req) {
1036 rc = -ENOMEM;
1037 goto free_ddcbs;
1038 }
1039
1040 queue->ddcb_waitqs = kzalloc(sizeof(wait_queue_head_t) *
1041 queue->ddcb_max, GFP_KERNEL);
1042 if (!queue->ddcb_waitqs) {
1043 rc = -ENOMEM;
1044 goto free_requs;
1045 }
1046
1047 for (i = 0; i < queue->ddcb_max; i++) {
1048 pddcb = &queue->ddcb_vaddr[i];
1049 pddcb->icrc_hsi_shi_32 = DDCB_COMPLETED_BE32;
1050 pddcb->retc_16 = cpu_to_be16(0xfff);
1051
1052 queue->ddcb_req[i] = NULL;
1053 init_waitqueue_head(&queue->ddcb_waitqs[i]);
1054 }
1055
1056 queue->ddcb_act = 0;
1057 queue->ddcb_next = 0;
1058
1059 spin_lock_init(&queue->ddcb_lock);
1060 init_waitqueue_head(&queue->ddcb_waitq);
1061
1062 val64 = ((u64)(queue->ddcb_max - 1) << 8);
1063 __genwqe_writeq(cd, queue->IO_QUEUE_CONFIG, 0x07);
1064 __genwqe_writeq(cd, queue->IO_QUEUE_SEGMENT, queue->ddcb_daddr);
1065 __genwqe_writeq(cd, queue->IO_QUEUE_INITSQN, queue->ddcb_seq);
1066 __genwqe_writeq(cd, queue->IO_QUEUE_WRAP, val64);
1067 return 0;
1068
1069 free_requs:
1070 kfree(queue->ddcb_req);
1071 queue->ddcb_req = NULL;
1072 free_ddcbs:
1073 __genwqe_free_consistent(cd, queue_size, queue->ddcb_vaddr,
1074 queue->ddcb_daddr);
1075 queue->ddcb_vaddr = NULL;
1076 queue->ddcb_daddr = 0ull;
1077 return -ENODEV;
1078
1079}
1080
1081static int ddcb_queue_initialized(struct ddcb_queue *queue)
1082{
1083 return queue->ddcb_vaddr != NULL;
1084}
1085
1086static void free_ddcb_queue(struct genwqe_dev *cd, struct ddcb_queue *queue)
1087{
1088 unsigned int queue_size;
1089
1090 queue_size = roundup(queue->ddcb_max * sizeof(struct ddcb), PAGE_SIZE);
1091
1092 kfree(queue->ddcb_req);
1093 queue->ddcb_req = NULL;
1094
1095 if (queue->ddcb_vaddr) {
1096 __genwqe_free_consistent(cd, queue_size, queue->ddcb_vaddr,
1097 queue->ddcb_daddr);
1098 queue->ddcb_vaddr = NULL;
1099 queue->ddcb_daddr = 0ull;
1100 }
1101}
1102
1103static irqreturn_t genwqe_pf_isr(int irq, void *dev_id)
1104{
1105 u64 gfir;
1106 struct genwqe_dev *cd = (struct genwqe_dev *)dev_id;
1107 struct pci_dev *pci_dev = cd->pci_dev;
1108
1109
1110
1111
1112
1113 cd->irqs_processed++;
1114 wake_up_interruptible(&cd->queue_waitq);
1115
1116
1117
1118
1119
1120 gfir = __genwqe_readq(cd, IO_SLC_CFGREG_GFIR);
1121 if (((gfir & GFIR_ERR_TRIGGER) != 0x0) &&
1122 !pci_channel_offline(pci_dev)) {
1123
1124 if (cd->use_platform_recovery) {
1125
1126
1127
1128
1129
1130 readq(cd->mmio + IO_SLC_CFGREG_GFIR);
1131
1132
1133 if (pci_channel_offline(pci_dev))
1134 goto exit;
1135 }
1136
1137 wake_up_interruptible(&cd->health_waitq);
1138
1139
1140
1141
1142
1143 dev_err_ratelimited(&pci_dev->dev,
1144 "[%s] GFIR=%016llx\n",
1145 __func__, gfir);
1146 }
1147
1148 exit:
1149 return IRQ_HANDLED;
1150}
1151
1152static irqreturn_t genwqe_vf_isr(int irq, void *dev_id)
1153{
1154 struct genwqe_dev *cd = (struct genwqe_dev *)dev_id;
1155
1156 cd->irqs_processed++;
1157 wake_up_interruptible(&cd->queue_waitq);
1158
1159 return IRQ_HANDLED;
1160}
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170static int genwqe_card_thread(void *data)
1171{
1172 int should_stop = 0, rc = 0;
1173 struct genwqe_dev *cd = (struct genwqe_dev *)data;
1174
1175 while (!kthread_should_stop()) {
1176
1177 genwqe_check_ddcb_queue(cd, &cd->queue);
1178
1179 if (genwqe_polling_enabled) {
1180 rc = wait_event_interruptible_timeout(
1181 cd->queue_waitq,
1182 genwqe_ddcbs_in_flight(cd) ||
1183 (should_stop = kthread_should_stop()), 1);
1184 } else {
1185 rc = wait_event_interruptible_timeout(
1186 cd->queue_waitq,
1187 genwqe_next_ddcb_ready(cd) ||
1188 (should_stop = kthread_should_stop()), HZ);
1189 }
1190 if (should_stop)
1191 break;
1192
1193
1194
1195
1196
1197 cond_resched();
1198 }
1199 return 0;
1200}
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210int genwqe_setup_service_layer(struct genwqe_dev *cd)
1211{
1212 int rc;
1213 struct ddcb_queue *queue;
1214 struct pci_dev *pci_dev = cd->pci_dev;
1215
1216 if (genwqe_is_privileged(cd)) {
1217 rc = genwqe_card_reset(cd);
1218 if (rc < 0) {
1219 dev_err(&pci_dev->dev,
1220 "[%s] err: reset failed.\n", __func__);
1221 return rc;
1222 }
1223 genwqe_read_softreset(cd);
1224 }
1225
1226 queue = &cd->queue;
1227 queue->IO_QUEUE_CONFIG = IO_SLC_QUEUE_CONFIG;
1228 queue->IO_QUEUE_STATUS = IO_SLC_QUEUE_STATUS;
1229 queue->IO_QUEUE_SEGMENT = IO_SLC_QUEUE_SEGMENT;
1230 queue->IO_QUEUE_INITSQN = IO_SLC_QUEUE_INITSQN;
1231 queue->IO_QUEUE_OFFSET = IO_SLC_QUEUE_OFFSET;
1232 queue->IO_QUEUE_WRAP = IO_SLC_QUEUE_WRAP;
1233 queue->IO_QUEUE_WTIME = IO_SLC_QUEUE_WTIME;
1234 queue->IO_QUEUE_ERRCNTS = IO_SLC_QUEUE_ERRCNTS;
1235 queue->IO_QUEUE_LRW = IO_SLC_QUEUE_LRW;
1236
1237 rc = setup_ddcb_queue(cd, queue);
1238 if (rc != 0) {
1239 rc = -ENODEV;
1240 goto err_out;
1241 }
1242
1243 init_waitqueue_head(&cd->queue_waitq);
1244 cd->card_thread = kthread_run(genwqe_card_thread, cd,
1245 GENWQE_DEVNAME "%d_thread",
1246 cd->card_idx);
1247 if (IS_ERR(cd->card_thread)) {
1248 rc = PTR_ERR(cd->card_thread);
1249 cd->card_thread = NULL;
1250 goto stop_free_queue;
1251 }
1252
1253 rc = genwqe_set_interrupt_capability(cd, GENWQE_MSI_IRQS);
1254 if (rc) {
1255 rc = -ENODEV;
1256 goto stop_kthread;
1257 }
1258
1259
1260
1261
1262
1263
1264 init_waitqueue_head(&cd->health_waitq);
1265
1266 if (genwqe_is_privileged(cd)) {
1267 rc = request_irq(pci_dev->irq, genwqe_pf_isr, IRQF_SHARED,
1268 GENWQE_DEVNAME, cd);
1269 } else {
1270 rc = request_irq(pci_dev->irq, genwqe_vf_isr, IRQF_SHARED,
1271 GENWQE_DEVNAME, cd);
1272 }
1273 if (rc < 0) {
1274 dev_err(&pci_dev->dev, "irq %d not free.\n", pci_dev->irq);
1275 goto stop_irq_cap;
1276 }
1277
1278 cd->card_state = GENWQE_CARD_USED;
1279 return 0;
1280
1281 stop_irq_cap:
1282 genwqe_reset_interrupt_capability(cd);
1283 stop_kthread:
1284 kthread_stop(cd->card_thread);
1285 cd->card_thread = NULL;
1286 stop_free_queue:
1287 free_ddcb_queue(cd, queue);
1288 err_out:
1289 return rc;
1290}
1291
1292
1293
1294
1295
1296
1297
1298
1299static int queue_wake_up_all(struct genwqe_dev *cd)
1300{
1301 unsigned int i;
1302 unsigned long flags;
1303 struct ddcb_queue *queue = &cd->queue;
1304
1305 spin_lock_irqsave(&queue->ddcb_lock, flags);
1306
1307 for (i = 0; i < queue->ddcb_max; i++)
1308 wake_up_interruptible(&queue->ddcb_waitqs[queue->ddcb_act]);
1309
1310 spin_unlock_irqrestore(&queue->ddcb_lock, flags);
1311
1312 return 0;
1313}
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323int genwqe_finish_queue(struct genwqe_dev *cd)
1324{
1325 int i, rc = 0, in_flight;
1326 int waitmax = genwqe_ddcb_software_timeout;
1327 struct pci_dev *pci_dev = cd->pci_dev;
1328 struct ddcb_queue *queue = &cd->queue;
1329
1330 if (!ddcb_queue_initialized(queue))
1331 return 0;
1332
1333
1334 if (cd->card_state == GENWQE_CARD_USED)
1335 cd->card_state = GENWQE_CARD_UNUSED;
1336
1337
1338
1339 queue_wake_up_all(cd);
1340
1341
1342 for (i = 0; i < waitmax; i++) {
1343 in_flight = genwqe_ddcbs_in_flight(cd);
1344
1345 if (in_flight == 0)
1346 break;
1347
1348 dev_dbg(&pci_dev->dev,
1349 " DEBUG [%d/%d] waiting for queue to get empty: "
1350 "%d requests!\n", i, waitmax, in_flight);
1351
1352
1353
1354
1355
1356
1357
1358
1359 msleep(1000);
1360 }
1361 if (i == waitmax) {
1362 dev_err(&pci_dev->dev, " [%s] err: queue is not empty!!\n",
1363 __func__);
1364 rc = -EIO;
1365 }
1366 return rc;
1367}
1368
1369
1370
1371
1372
1373
1374
1375int genwqe_release_service_layer(struct genwqe_dev *cd)
1376{
1377 struct pci_dev *pci_dev = cd->pci_dev;
1378
1379 if (!ddcb_queue_initialized(&cd->queue))
1380 return 1;
1381
1382 free_irq(pci_dev->irq, cd);
1383 genwqe_reset_interrupt_capability(cd);
1384
1385 if (cd->card_thread != NULL) {
1386 kthread_stop(cd->card_thread);
1387 cd->card_thread = NULL;
1388 }
1389
1390 free_ddcb_queue(cd, &cd->queue);
1391 return 0;
1392}
1393