/
linda.c
2400 lines (2115 loc) · 67.5 KB
/
linda.c
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
30
31
32
33
34
35
36
37
38
39
40
41
42
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
FILE_LICENCE ( GPL2_OR_LATER );
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <assert.h>
#include <gpxe/io.h>
#include <gpxe/pci.h>
#include <gpxe/infiniband.h>
#include <gpxe/i2c.h>
#include <gpxe/bitbash.h>
#include <gpxe/malloc.h>
#include <gpxe/iobuf.h>
#include "linda.h"
/**
* @file
*
* QLogic Linda Infiniband HCA
*
*/
/** A Linda send work queue */
struct linda_send_work_queue {
/** Send buffer usage */
uint8_t *send_buf;
/** Producer index */
unsigned int prod;
/** Consumer index */
unsigned int cons;
};
/** A Linda receive work queue */
struct linda_recv_work_queue {
/** Receive header ring */
void *header;
/** Receive header producer offset (written by hardware) */
struct QIB_7220_scalar header_prod;
/** Receive header consumer offset */
unsigned int header_cons;
/** Offset within register space of the eager array */
unsigned long eager_array;
/** Number of entries in eager array */
unsigned int eager_entries;
/** Eager array producer index */
unsigned int eager_prod;
/** Eager array consumer index */
unsigned int eager_cons;
};
/** A Linda HCA */
struct linda {
/** Registers */
void *regs;
/** In-use contexts */
uint8_t used_ctx[LINDA_NUM_CONTEXTS];
/** Send work queues */
struct linda_send_work_queue send_wq[LINDA_NUM_CONTEXTS];
/** Receive work queues */
struct linda_recv_work_queue recv_wq[LINDA_NUM_CONTEXTS];
/** Offset within register space of the first send buffer */
unsigned long send_buffer_base;
/** Send buffer availability (reported by hardware) */
struct QIB_7220_SendBufAvail *sendbufavail;
/** Send buffer availability (maintained by software) */
uint8_t send_buf[LINDA_MAX_SEND_BUFS];
/** Send buffer availability producer counter */
unsigned int send_buf_prod;
/** Send buffer availability consumer counter */
unsigned int send_buf_cons;
/** Number of reserved send buffers (across all QPs) */
unsigned int reserved_send_bufs;
/** I2C bit-bashing interface */
struct i2c_bit_basher i2c;
/** I2C serial EEPROM */
struct i2c_device eeprom;
};
/***************************************************************************
*
* Linda register access
*
***************************************************************************
*
* This card requires atomic 64-bit accesses. Strange things happen
* if you try to use 32-bit accesses; sometimes they work, sometimes
* they don't, sometimes you get random data.
*
* These accessors use the "movq" MMX instruction, and so won't work
* on really old Pentiums (which won't have PCIe anyway, so this is
* something of a moot point).
*/
/**
* Read Linda qword register
*
* @v linda Linda device
* @v dwords Register buffer to read into
* @v offset Register offset
*/
static void linda_readq ( struct linda *linda, uint32_t *dwords,
unsigned long offset ) {
void *addr = ( linda->regs + offset );
__asm__ __volatile__ ( "movq (%1), %%mm0\n\t"
"movq %%mm0, (%0)\n\t"
: : "r" ( dwords ), "r" ( addr ) : "memory" );
DBGIO ( "[%08lx] => %08x%08x\n",
virt_to_phys ( addr ), dwords[1], dwords[0] );
}
#define linda_readq( _linda, _ptr, _offset ) \
linda_readq ( (_linda), (_ptr)->u.dwords, (_offset) )
#define linda_readq_array8b( _linda, _ptr, _offset, _idx ) \
linda_readq ( (_linda), (_ptr), ( (_offset) + ( (_idx) * 8 ) ) )
#define linda_readq_array64k( _linda, _ptr, _offset, _idx ) \
linda_readq ( (_linda), (_ptr), ( (_offset) + ( (_idx) * 65536 ) ) )
/**
* Write Linda qword register
*
* @v linda Linda device
* @v dwords Register buffer to write
* @v offset Register offset
*/
static void linda_writeq ( struct linda *linda, const uint32_t *dwords,
unsigned long offset ) {
void *addr = ( linda->regs + offset );
DBGIO ( "[%08lx] <= %08x%08x\n",
virt_to_phys ( addr ), dwords[1], dwords[0] );
__asm__ __volatile__ ( "movq (%0), %%mm0\n\t"
"movq %%mm0, (%1)\n\t"
: : "r" ( dwords ), "r" ( addr ) : "memory" );
}
#define linda_writeq( _linda, _ptr, _offset ) \
linda_writeq ( (_linda), (_ptr)->u.dwords, (_offset) )
#define linda_writeq_array8b( _linda, _ptr, _offset, _idx ) \
linda_writeq ( (_linda), (_ptr), ( (_offset) + ( (_idx) * 8 ) ) )
#define linda_writeq_array64k( _linda, _ptr, _offset, _idx ) \
linda_writeq ( (_linda), (_ptr), ( (_offset) + ( (_idx) * 65536 ) ) )
/**
* Write Linda dword register
*
* @v linda Linda device
* @v dword Value to write
* @v offset Register offset
*/
static void linda_writel ( struct linda *linda, uint32_t dword,
unsigned long offset ) {
writel ( dword, ( linda->regs + offset ) );
}
/***************************************************************************
*
* Link state management
*
***************************************************************************
*/
/**
* Textual representation of link state
*
* @v link_state Link state
* @ret link_text Link state text
*/
static const char * linda_link_state_text ( unsigned int link_state ) {
switch ( link_state ) {
case LINDA_LINK_STATE_DOWN: return "DOWN";
case LINDA_LINK_STATE_INIT: return "INIT";
case LINDA_LINK_STATE_ARM: return "ARM";
case LINDA_LINK_STATE_ACTIVE: return "ACTIVE";
case LINDA_LINK_STATE_ACT_DEFER:return "ACT_DEFER";
default: return "UNKNOWN";
}
}
/**
* Handle link state change
*
* @v linda Linda device
*/
static void linda_link_state_changed ( struct ib_device *ibdev ) {
struct linda *linda = ib_get_drvdata ( ibdev );
struct QIB_7220_IBCStatus ibcstatus;
struct QIB_7220_EXTCtrl extctrl;
unsigned int link_state;
unsigned int link_width;
unsigned int link_speed;
/* Read link state */
linda_readq ( linda, &ibcstatus, QIB_7220_IBCStatus_offset );
link_state = BIT_GET ( &ibcstatus, LinkState );
link_width = BIT_GET ( &ibcstatus, LinkWidthActive );
link_speed = BIT_GET ( &ibcstatus, LinkSpeedActive );
DBGC ( linda, "Linda %p link state %s (%s %s)\n", linda,
linda_link_state_text ( link_state ),
( link_speed ? "DDR" : "SDR" ), ( link_width ? "x4" : "x1" ) );
/* Set LEDs according to link state */
linda_readq ( linda, &extctrl, QIB_7220_EXTCtrl_offset );
BIT_SET ( &extctrl, LEDPriPortGreenOn,
( ( link_state >= LINDA_LINK_STATE_INIT ) ? 1 : 0 ) );
BIT_SET ( &extctrl, LEDPriPortYellowOn,
( ( link_state >= LINDA_LINK_STATE_ACTIVE ) ? 1 : 0 ) );
linda_writeq ( linda, &extctrl, QIB_7220_EXTCtrl_offset );
/* Notify Infiniband core of link state change */
ibdev->port_state = ( link_state + 1 );
ibdev->link_width_active =
( link_width ? IB_LINK_WIDTH_4X : IB_LINK_WIDTH_1X );
ibdev->link_speed_active =
( link_speed ? IB_LINK_SPEED_DDR : IB_LINK_SPEED_SDR );
ib_link_state_changed ( ibdev );
}
/**
* Set port information
*
* @v ibdev Infiniband device
* @v mad Set port information MAD
*/
static int linda_set_port_info ( struct ib_device *ibdev, union ib_mad *mad ) {
struct linda *linda = ib_get_drvdata ( ibdev );
struct ib_port_info *port_info = &mad->smp.smp_data.port_info;
struct QIB_7220_IBCCtrl ibcctrl;
unsigned int port_state;
unsigned int link_state;
/* Set new link state */
port_state = ( port_info->link_speed_supported__port_state & 0xf );
if ( port_state ) {
link_state = ( port_state - 1 );
DBGC ( linda, "Linda %p set link state to %s (%x)\n", linda,
linda_link_state_text ( link_state ), link_state );
linda_readq ( linda, &ibcctrl, QIB_7220_IBCCtrl_offset );
BIT_SET ( &ibcctrl, LinkCmd, link_state );
linda_writeq ( linda, &ibcctrl, QIB_7220_IBCCtrl_offset );
}
/* Detect and report link state change */
linda_link_state_changed ( ibdev );
return 0;
}
/**
* Set partition key table
*
* @v ibdev Infiniband device
* @v mad Set partition key table MAD
*/
static int linda_set_pkey_table ( struct ib_device *ibdev __unused,
union ib_mad *mad __unused ) {
/* Nothing to do */
return 0;
}
/***************************************************************************
*
* Context allocation
*
***************************************************************************
*/
/**
* Map context number to QPN
*
* @v ctx Context index
* @ret qpn Queue pair number
*/
static int linda_ctx_to_qpn ( unsigned int ctx ) {
/* This mapping is fixed by hardware */
return ( ctx * 2 );
}
/**
* Map QPN to context number
*
* @v qpn Queue pair number
* @ret ctx Context index
*/
static int linda_qpn_to_ctx ( unsigned int qpn ) {
/* This mapping is fixed by hardware */
return ( qpn / 2 );
}
/**
* Allocate a context
*
* @v linda Linda device
* @ret ctx Context index, or negative error
*/
static int linda_alloc_ctx ( struct linda *linda ) {
unsigned int ctx;
for ( ctx = 0 ; ctx < LINDA_NUM_CONTEXTS ; ctx++ ) {
if ( ! linda->used_ctx[ctx] ) {
linda->used_ctx[ctx ] = 1;
DBGC2 ( linda, "Linda %p CTX %d allocated\n",
linda, ctx );
return ctx;
}
}
DBGC ( linda, "Linda %p out of available contexts\n", linda );
return -ENOENT;
}
/**
* Free a context
*
* @v linda Linda device
* @v ctx Context index
*/
static void linda_free_ctx ( struct linda *linda, unsigned int ctx ) {
linda->used_ctx[ctx] = 0;
DBGC2 ( linda, "Linda %p CTX %d freed\n", linda, ctx );
}
/***************************************************************************
*
* Send datapath
*
***************************************************************************
*/
/** Send buffer toggle bit
*
* We encode send buffers as 7 bits of send buffer index plus a single
* bit which should match the "check" bit in the SendBufAvail array.
*/
#define LINDA_SEND_BUF_TOGGLE 0x80
/**
* Allocate a send buffer
*
* @v linda Linda device
* @ret send_buf Send buffer
*
* You must guarantee that a send buffer is available. This is done
* by refusing to allocate more TX WQEs in total than the number of
* available send buffers.
*/
static unsigned int linda_alloc_send_buf ( struct linda *linda ) {
unsigned int send_buf;
send_buf = linda->send_buf[linda->send_buf_cons];
send_buf ^= LINDA_SEND_BUF_TOGGLE;
linda->send_buf_cons = ( ( linda->send_buf_cons + 1 ) %
LINDA_MAX_SEND_BUFS );
return send_buf;
}
/**
* Free a send buffer
*
* @v linda Linda device
* @v send_buf Send buffer
*/
static void linda_free_send_buf ( struct linda *linda,
unsigned int send_buf ) {
linda->send_buf[linda->send_buf_prod] = send_buf;
linda->send_buf_prod = ( ( linda->send_buf_prod + 1 ) %
LINDA_MAX_SEND_BUFS );
}
/**
* Check to see if send buffer is in use
*
* @v linda Linda device
* @v send_buf Send buffer
* @ret in_use Send buffer is in use
*/
static int linda_send_buf_in_use ( struct linda *linda,
unsigned int send_buf ) {
unsigned int send_idx;
unsigned int send_check;
unsigned int inusecheck;
unsigned int inuse;
unsigned int check;
send_idx = ( send_buf & ~LINDA_SEND_BUF_TOGGLE );
send_check = ( !! ( send_buf & LINDA_SEND_BUF_TOGGLE ) );
inusecheck = BIT_GET ( linda->sendbufavail, InUseCheck[send_idx] );
inuse = ( !! ( inusecheck & 0x02 ) );
check = ( !! ( inusecheck & 0x01 ) );
return ( inuse || ( check != send_check ) );
}
/**
* Calculate starting offset for send buffer
*
* @v linda Linda device
* @v send_buf Send buffer
* @ret offset Starting offset
*/
static unsigned long linda_send_buffer_offset ( struct linda *linda,
unsigned int send_buf ) {
return ( linda->send_buffer_base +
( ( send_buf & ~LINDA_SEND_BUF_TOGGLE ) *
LINDA_SEND_BUF_SIZE ) );
}
/**
* Create send work queue
*
* @v linda Linda device
* @v qp Queue pair
*/
static int linda_create_send_wq ( struct linda *linda,
struct ib_queue_pair *qp ) {
struct ib_work_queue *wq = &qp->send;
struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
int rc;
/* Reserve send buffers */
if ( ( linda->reserved_send_bufs + qp->send.num_wqes ) >
LINDA_MAX_SEND_BUFS ) {
DBGC ( linda, "Linda %p out of send buffers (have %d, used "
"%d, need %d)\n", linda, LINDA_MAX_SEND_BUFS,
linda->reserved_send_bufs, qp->send.num_wqes );
rc = -ENOBUFS;
goto err_reserve_bufs;
}
linda->reserved_send_bufs += qp->send.num_wqes;
/* Reset work queue */
linda_wq->prod = 0;
linda_wq->cons = 0;
/* Allocate space for send buffer uasge list */
linda_wq->send_buf = zalloc ( qp->send.num_wqes *
sizeof ( linda_wq->send_buf[0] ) );
if ( ! linda_wq->send_buf ) {
rc = -ENOBUFS;
goto err_alloc_send_buf;
}
return 0;
free ( linda_wq->send_buf );
err_alloc_send_buf:
linda->reserved_send_bufs -= qp->send.num_wqes;
err_reserve_bufs:
return rc;
}
/**
* Destroy send work queue
*
* @v linda Linda device
* @v qp Queue pair
*/
static void linda_destroy_send_wq ( struct linda *linda,
struct ib_queue_pair *qp ) {
struct ib_work_queue *wq = &qp->send;
struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
free ( linda_wq->send_buf );
linda->reserved_send_bufs -= qp->send.num_wqes;
}
/**
* Initialise send datapath
*
* @v linda Linda device
* @ret rc Return status code
*/
static int linda_init_send ( struct linda *linda ) {
struct QIB_7220_SendBufBase sendbufbase;
struct QIB_7220_SendBufAvailAddr sendbufavailaddr;
struct QIB_7220_SendCtrl sendctrl;
unsigned int i;
int rc;
/* Retrieve SendBufBase */
linda_readq ( linda, &sendbufbase, QIB_7220_SendBufBase_offset );
linda->send_buffer_base = BIT_GET ( &sendbufbase,
BaseAddr_SmallPIO );
DBGC ( linda, "Linda %p send buffers at %lx\n",
linda, linda->send_buffer_base );
/* Initialise the send_buf[] array */
for ( i = 0 ; i < LINDA_MAX_SEND_BUFS ; i++ )
linda->send_buf[i] = i;
/* Allocate space for the SendBufAvail array */
linda->sendbufavail = malloc_dma ( sizeof ( *linda->sendbufavail ),
LINDA_SENDBUFAVAIL_ALIGN );
if ( ! linda->sendbufavail ) {
rc = -ENOMEM;
goto err_alloc_sendbufavail;
}
memset ( linda->sendbufavail, 0, sizeof ( linda->sendbufavail ) );
/* Program SendBufAvailAddr into the hardware */
memset ( &sendbufavailaddr, 0, sizeof ( sendbufavailaddr ) );
BIT_FILL_1 ( &sendbufavailaddr, SendBufAvailAddr,
( virt_to_bus ( linda->sendbufavail ) >> 6 ) );
linda_writeq ( linda, &sendbufavailaddr,
QIB_7220_SendBufAvailAddr_offset );
/* Enable sending and DMA of SendBufAvail */
memset ( &sendctrl, 0, sizeof ( sendctrl ) );
BIT_FILL_2 ( &sendctrl,
SendBufAvailUpd, 1,
SPioEnable, 1 );
linda_writeq ( linda, &sendctrl, QIB_7220_SendCtrl_offset );
return 0;
free_dma ( linda->sendbufavail, sizeof ( *linda->sendbufavail ) );
err_alloc_sendbufavail:
return rc;
}
/**
* Shut down send datapath
*
* @v linda Linda device
*/
static void linda_fini_send ( struct linda *linda ) {
struct QIB_7220_SendCtrl sendctrl;
/* Disable sending and DMA of SendBufAvail */
memset ( &sendctrl, 0, sizeof ( sendctrl ) );
linda_writeq ( linda, &sendctrl, QIB_7220_SendCtrl_offset );
mb();
/* Ensure hardware has seen this disable */
linda_readq ( linda, &sendctrl, QIB_7220_SendCtrl_offset );
free_dma ( linda->sendbufavail, sizeof ( *linda->sendbufavail ) );
}
/***************************************************************************
*
* Receive datapath
*
***************************************************************************
*/
/**
* Create receive work queue
*
* @v linda Linda device
* @v qp Queue pair
* @ret rc Return status code
*/
static int linda_create_recv_wq ( struct linda *linda,
struct ib_queue_pair *qp ) {
struct ib_work_queue *wq = &qp->recv;
struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
struct QIB_7220_RcvHdrAddr0 rcvhdraddr;
struct QIB_7220_RcvHdrTailAddr0 rcvhdrtailaddr;
struct QIB_7220_RcvHdrHead0 rcvhdrhead;
struct QIB_7220_scalar rcvegrindexhead;
struct QIB_7220_RcvCtrl rcvctrl;
unsigned int ctx = linda_qpn_to_ctx ( qp->qpn );
int rc;
/* Reset context information */
memset ( &linda_wq->header_prod, 0,
sizeof ( linda_wq->header_prod ) );
linda_wq->header_cons = 0;
linda_wq->eager_prod = 0;
linda_wq->eager_cons = 0;
/* Allocate receive header buffer */
linda_wq->header = malloc_dma ( LINDA_RECV_HEADERS_SIZE,
LINDA_RECV_HEADERS_ALIGN );
if ( ! linda_wq->header ) {
rc = -ENOMEM;
goto err_alloc_header;
}
/* Enable context in hardware */
memset ( &rcvhdraddr, 0, sizeof ( rcvhdraddr ) );
BIT_FILL_1 ( &rcvhdraddr, RcvHdrAddr0,
( virt_to_bus ( linda_wq->header ) >> 2 ) );
linda_writeq_array8b ( linda, &rcvhdraddr,
QIB_7220_RcvHdrAddr0_offset, ctx );
memset ( &rcvhdrtailaddr, 0, sizeof ( rcvhdrtailaddr ) );
BIT_FILL_1 ( &rcvhdrtailaddr, RcvHdrTailAddr0,
( virt_to_bus ( &linda_wq->header_prod ) >> 2 ) );
linda_writeq_array8b ( linda, &rcvhdrtailaddr,
QIB_7220_RcvHdrTailAddr0_offset, ctx );
memset ( &rcvhdrhead, 0, sizeof ( rcvhdrhead ) );
BIT_FILL_1 ( &rcvhdrhead, counter, 1 );
linda_writeq_array64k ( linda, &rcvhdrhead,
QIB_7220_RcvHdrHead0_offset, ctx );
memset ( &rcvegrindexhead, 0, sizeof ( rcvegrindexhead ) );
BIT_FILL_1 ( &rcvegrindexhead, Value, 1 );
linda_writeq_array64k ( linda, &rcvegrindexhead,
QIB_7220_RcvEgrIndexHead0_offset, ctx );
linda_readq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
BIT_SET ( &rcvctrl, PortEnable[ctx], 1 );
BIT_SET ( &rcvctrl, IntrAvail[ctx], 1 );
linda_writeq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
DBGC ( linda, "Linda %p QPN %ld CTX %d hdrs [%lx,%lx) prod %lx\n",
linda, qp->qpn, ctx, virt_to_bus ( linda_wq->header ),
( virt_to_bus ( linda_wq->header ) + LINDA_RECV_HEADERS_SIZE ),
virt_to_bus ( &linda_wq->header_prod ) );
return 0;
free_dma ( linda_wq->header, LINDA_RECV_HEADERS_SIZE );
err_alloc_header:
return rc;
}
/**
* Destroy receive work queue
*
* @v linda Linda device
* @v qp Queue pair
*/
static void linda_destroy_recv_wq ( struct linda *linda,
struct ib_queue_pair *qp ) {
struct ib_work_queue *wq = &qp->recv;
struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
struct QIB_7220_RcvCtrl rcvctrl;
unsigned int ctx = linda_qpn_to_ctx ( qp->qpn );
/* Disable context in hardware */
linda_readq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
BIT_SET ( &rcvctrl, PortEnable[ctx], 0 );
BIT_SET ( &rcvctrl, IntrAvail[ctx], 0 );
linda_writeq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
/* Make sure the hardware has seen that the context is disabled */
linda_readq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
mb();
/* Free headers ring */
free_dma ( linda_wq->header, LINDA_RECV_HEADERS_SIZE );
/* Free context */
linda_free_ctx ( linda, ctx );
}
/**
* Initialise receive datapath
*
* @v linda Linda device
* @ret rc Return status code
*/
static int linda_init_recv ( struct linda *linda ) {
struct QIB_7220_RcvCtrl rcvctrl;
struct QIB_7220_scalar rcvegrbase;
struct QIB_7220_scalar rcvhdrentsize;
struct QIB_7220_scalar rcvhdrcnt;
struct QIB_7220_RcvBTHQP rcvbthqp;
unsigned int portcfg;
unsigned long egrbase;
unsigned int eager_array_size_0;
unsigned int eager_array_size_other;
unsigned int ctx;
/* Select configuration based on number of contexts */
switch ( LINDA_NUM_CONTEXTS ) {
case 5:
portcfg = LINDA_PORTCFG_5CTX;
eager_array_size_0 = LINDA_EAGER_ARRAY_SIZE_5CTX_0;
eager_array_size_other = LINDA_EAGER_ARRAY_SIZE_5CTX_OTHER;
break;
case 9:
portcfg = LINDA_PORTCFG_9CTX;
eager_array_size_0 = LINDA_EAGER_ARRAY_SIZE_9CTX_0;
eager_array_size_other = LINDA_EAGER_ARRAY_SIZE_9CTX_OTHER;
break;
case 17:
portcfg = LINDA_PORTCFG_17CTX;
eager_array_size_0 = LINDA_EAGER_ARRAY_SIZE_17CTX_0;
eager_array_size_other = LINDA_EAGER_ARRAY_SIZE_17CTX_OTHER;
break;
default:
linker_assert ( 0, invalid_LINDA_NUM_CONTEXTS );
return -EINVAL;
}
/* Configure number of contexts */
memset ( &rcvctrl, 0, sizeof ( rcvctrl ) );
BIT_FILL_3 ( &rcvctrl,
TailUpd, 1,
PortCfg, portcfg,
RcvQPMapEnable, 1 );
linda_writeq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
/* Configure receive header buffer sizes */
memset ( &rcvhdrcnt, 0, sizeof ( rcvhdrcnt ) );
BIT_FILL_1 ( &rcvhdrcnt, Value, LINDA_RECV_HEADER_COUNT );
linda_writeq ( linda, &rcvhdrcnt, QIB_7220_RcvHdrCnt_offset );
memset ( &rcvhdrentsize, 0, sizeof ( rcvhdrentsize ) );
BIT_FILL_1 ( &rcvhdrentsize, Value, ( LINDA_RECV_HEADER_SIZE >> 2 ) );
linda_writeq ( linda, &rcvhdrentsize, QIB_7220_RcvHdrEntSize_offset );
/* Calculate eager array start addresses for each context */
linda_readq ( linda, &rcvegrbase, QIB_7220_RcvEgrBase_offset );
egrbase = BIT_GET ( &rcvegrbase, Value );
linda->recv_wq[0].eager_array = egrbase;
linda->recv_wq[0].eager_entries = eager_array_size_0;
egrbase += ( eager_array_size_0 * sizeof ( struct QIB_7220_RcvEgr ) );
for ( ctx = 1 ; ctx < LINDA_NUM_CONTEXTS ; ctx++ ) {
linda->recv_wq[ctx].eager_array = egrbase;
linda->recv_wq[ctx].eager_entries = eager_array_size_other;
egrbase += ( eager_array_size_other *
sizeof ( struct QIB_7220_RcvEgr ) );
}
for ( ctx = 0 ; ctx < LINDA_NUM_CONTEXTS ; ctx++ ) {
DBGC ( linda, "Linda %p CTX %d eager array at %lx (%d "
"entries)\n", linda, ctx,
linda->recv_wq[ctx].eager_array,
linda->recv_wq[ctx].eager_entries );
}
/* Set the BTH QP for Infinipath packets to an unused value */
memset ( &rcvbthqp, 0, sizeof ( rcvbthqp ) );
BIT_FILL_1 ( &rcvbthqp, RcvBTHQP, LINDA_QP_IDETH );
linda_writeq ( linda, &rcvbthqp, QIB_7220_RcvBTHQP_offset );
return 0;
}
/**
* Shut down receive datapath
*
* @v linda Linda device
*/
static void linda_fini_recv ( struct linda *linda __unused ) {
/* Nothing to do; all contexts were already disabled when the
* queue pairs were destroyed
*/
}
/***************************************************************************
*
* Completion queue operations
*
***************************************************************************
*/
/**
* Create completion queue
*
* @v ibdev Infiniband device
* @v cq Completion queue
* @ret rc Return status code
*/
static int linda_create_cq ( struct ib_device *ibdev,
struct ib_completion_queue *cq ) {
struct linda *linda = ib_get_drvdata ( ibdev );
static int cqn;
/* The hardware has no concept of completion queues. We
* simply use the association between CQs and WQs (already
* handled by the IB core) to decide which WQs to poll.
*
* We do set a CQN, just to avoid confusing debug messages
* from the IB core.
*/
cq->cqn = ++cqn;
DBGC ( linda, "Linda %p CQN %ld created\n", linda, cq->cqn );
return 0;
}
/**
* Destroy completion queue
*
* @v ibdev Infiniband device
* @v cq Completion queue
*/
static void linda_destroy_cq ( struct ib_device *ibdev,
struct ib_completion_queue *cq ) {
struct linda *linda = ib_get_drvdata ( ibdev );
/* Nothing to do */
DBGC ( linda, "Linda %p CQN %ld destroyed\n", linda, cq->cqn );
}
/***************************************************************************
*
* Queue pair operations
*
***************************************************************************
*/
/**
* Create queue pair
*
* @v ibdev Infiniband device
* @v qp Queue pair
* @ret rc Return status code
*/
static int linda_create_qp ( struct ib_device *ibdev,
struct ib_queue_pair *qp ) {
struct linda *linda = ib_get_drvdata ( ibdev );
int ctx;
int rc;
/* Locate an available context */
ctx = linda_alloc_ctx ( linda );
if ( ctx < 0 ) {
rc = ctx;
goto err_alloc_ctx;
}
/* Set queue pair number based on context index */
qp->qpn = linda_ctx_to_qpn ( ctx );
/* Set work-queue private data pointers */
ib_wq_set_drvdata ( &qp->send, &linda->send_wq[ctx] );
ib_wq_set_drvdata ( &qp->recv, &linda->recv_wq[ctx] );
/* Create receive work queue */
if ( ( rc = linda_create_recv_wq ( linda, qp ) ) != 0 )
goto err_create_recv_wq;
/* Create send work queue */
if ( ( rc = linda_create_send_wq ( linda, qp ) ) != 0 )
goto err_create_send_wq;
return 0;
linda_destroy_send_wq ( linda, qp );
err_create_send_wq:
linda_destroy_recv_wq ( linda, qp );
err_create_recv_wq:
linda_free_ctx ( linda, ctx );
err_alloc_ctx:
return rc;
}
/**
* Modify queue pair
*
* @v ibdev Infiniband device
* @v qp Queue pair
* @ret rc Return status code
*/
static int linda_modify_qp ( struct ib_device *ibdev,
struct ib_queue_pair *qp ) {
struct linda *linda = ib_get_drvdata ( ibdev );
/* Nothing to do; the hardware doesn't have a notion of queue
* keys
*/
DBGC ( linda, "Linda %p QPN %ld modified\n", linda, qp->qpn );
return 0;
}
/**
* Destroy queue pair
*
* @v ibdev Infiniband device
* @v qp Queue pair
*/
static void linda_destroy_qp ( struct ib_device *ibdev,
struct ib_queue_pair *qp ) {
struct linda *linda = ib_get_drvdata ( ibdev );
linda_destroy_send_wq ( linda, qp );
linda_destroy_recv_wq ( linda, qp );
}
/***************************************************************************
*
* Work request operations
*
***************************************************************************
*/
/**
* Post send work queue entry
*
* @v ibdev Infiniband device
* @v qp Queue pair
* @v av Address vector
* @v iobuf I/O buffer
* @ret rc Return status code
*/
static int linda_post_send ( struct ib_device *ibdev,
struct ib_queue_pair *qp,
struct ib_address_vector *av,
struct io_buffer *iobuf ) {
struct linda *linda = ib_get_drvdata ( ibdev );
struct ib_work_queue *wq = &qp->send;
struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
struct QIB_7220_SendPbc sendpbc;
uint8_t header_buf[IB_MAX_HEADER_SIZE];
struct io_buffer headers;
unsigned int send_buf;
unsigned long start_offset;
unsigned long offset;
size_t len;
ssize_t frag_len;
uint32_t *data;
/* Allocate send buffer and calculate offset */
send_buf = linda_alloc_send_buf ( linda );
start_offset = offset = linda_send_buffer_offset ( linda, send_buf );
/* Store I/O buffer and send buffer index */
assert ( wq->iobufs[linda_wq->prod] == NULL );
wq->iobufs[linda_wq->prod] = iobuf;
linda_wq->send_buf[linda_wq->prod] = send_buf;
/* Construct headers */
iob_populate ( &headers, header_buf, 0, sizeof ( header_buf ) );
iob_reserve ( &headers, sizeof ( header_buf ) );
ib_push ( ibdev, &headers, qp, iob_len ( iobuf ), av );
/* Calculate packet length */
len = ( ( sizeof ( sendpbc ) + iob_len ( &headers ) +
iob_len ( iobuf ) + 3 ) & ~3 );
/* Construct send per-buffer control word */
memset ( &sendpbc, 0, sizeof ( sendpbc ) );
BIT_FILL_2 ( &sendpbc,
LengthP1_toibc, ( ( len >> 2 ) - 1 ),
VL15, 1 );
/* Write SendPbc */
DBG_DISABLE ( DBGLVL_IO );
linda_writeq ( linda, &sendpbc, offset );
offset += sizeof ( sendpbc );
/* Write headers */
for ( data = headers.data, frag_len = iob_len ( &headers ) ;
frag_len > 0 ; data++, offset += 4, frag_len -= 4 ) {
linda_writel ( linda, *data, offset );
}
/* Write data */
for ( data = iobuf->data, frag_len = iob_len ( iobuf ) ;
frag_len > 0 ; data++, offset += 4, frag_len -= 4 ) {
linda_writel ( linda, *data, offset );
}
DBG_ENABLE ( DBGLVL_IO );
assert ( ( start_offset + len ) == offset );
DBGC2 ( linda, "Linda %p QPN %ld TX %d(%d) posted [%lx,%lx)\n",
linda, qp->qpn, send_buf, linda_wq->prod,
start_offset, offset );
/* Increment producer counter */
linda_wq->prod = ( ( linda_wq->prod + 1 ) & ( wq->num_wqes - 1 ) );
return 0;
}
/**
* Complete send work queue entry
*
* @v ibdev Infiniband device
* @v qp Queue pair
* @v wqe_idx Work queue entry index
*/
static void linda_complete_send ( struct ib_device *ibdev,
struct ib_queue_pair *qp,
unsigned int wqe_idx ) {
struct linda *linda = ib_get_drvdata ( ibdev );
struct ib_work_queue *wq = &qp->send;
struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
struct io_buffer *iobuf;
unsigned int send_buf;
/* Parse completion */
send_buf = linda_wq->send_buf[wqe_idx];
DBGC2 ( linda, "Linda %p QPN %ld TX %d(%d) complete\n",
linda, qp->qpn, send_buf, wqe_idx );