-
Notifications
You must be signed in to change notification settings - Fork 102
/
bnx2fc_io.c
2108 lines (1829 loc) · 56.8 KB
/
bnx2fc_io.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
/* bnx2fc_io.c: QLogic Linux FCoE offload driver.
* IO manager and SCSI IO processing.
*
* Copyright (c) 2008-2013 Broadcom Corporation
* Copyright (c) 2014-2016 QLogic Corporation
* Copyright (c) 2016-2017 Cavium Inc.
*
* 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.
*
* Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
*/
#include "bnx2fc.h"
#define RESERVE_FREE_LIST_INDEX num_possible_cpus()
static int bnx2fc_split_bd(struct bnx2fc_cmd *io_req, u64 addr, int sg_len,
int bd_index);
static int bnx2fc_map_sg(struct bnx2fc_cmd *io_req);
static int bnx2fc_build_bd_list_from_sg(struct bnx2fc_cmd *io_req);
static void bnx2fc_unmap_sg_list(struct bnx2fc_cmd *io_req);
static void bnx2fc_free_mp_resc(struct bnx2fc_cmd *io_req);
static void bnx2fc_parse_fcp_rsp(struct bnx2fc_cmd *io_req,
struct fcoe_fcp_rsp_payload *fcp_rsp,
u8 num_rq, unsigned char *rq_data);
void bnx2fc_cmd_timer_set(struct bnx2fc_cmd *io_req,
unsigned int timer_msec)
{
struct bnx2fc_interface *interface = io_req->port->priv;
if (queue_delayed_work(interface->timer_work_queue,
&io_req->timeout_work,
msecs_to_jiffies(timer_msec)))
kref_get(&io_req->refcount);
}
static void bnx2fc_cmd_timeout(struct work_struct *work)
{
struct bnx2fc_cmd *io_req = container_of(work, struct bnx2fc_cmd,
timeout_work.work);
u8 cmd_type = io_req->cmd_type;
struct bnx2fc_rport *tgt = io_req->tgt;
int rc;
BNX2FC_IO_DBG(io_req, "cmd_timeout, cmd_type = %d,"
"req_flags = %lx\n", cmd_type, io_req->req_flags);
spin_lock_bh(&tgt->tgt_lock);
if (test_and_clear_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags)) {
clear_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags);
/*
* ideally we should hold the io_req until RRQ complets,
* and release io_req from timeout hold.
*/
spin_unlock_bh(&tgt->tgt_lock);
bnx2fc_send_rrq(io_req);
return;
}
if (test_and_clear_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags)) {
BNX2FC_IO_DBG(io_req, "IO ready for reuse now\n");
goto done;
}
switch (cmd_type) {
case BNX2FC_SCSI_CMD:
if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
&io_req->req_flags)) {
/* Handle eh_abort timeout */
BNX2FC_IO_DBG(io_req, "eh_abort timed out\n");
complete(&io_req->abts_done);
} else if (test_bit(BNX2FC_FLAG_ISSUE_ABTS,
&io_req->req_flags)) {
/* Handle internally generated ABTS timeout */
BNX2FC_IO_DBG(io_req, "ABTS timed out refcnt = %d\n",
kref_read(&io_req->refcount));
if (!(test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
&io_req->req_flags))) {
/*
* Cleanup and return original command to
* mid-layer.
*/
bnx2fc_initiate_cleanup(io_req);
kref_put(&io_req->refcount, bnx2fc_cmd_release);
spin_unlock_bh(&tgt->tgt_lock);
return;
}
} else {
/* Hanlde IO timeout */
BNX2FC_IO_DBG(io_req, "IO timed out. issue ABTS\n");
if (test_and_set_bit(BNX2FC_FLAG_IO_COMPL,
&io_req->req_flags)) {
BNX2FC_IO_DBG(io_req, "IO completed before "
" timer expiry\n");
goto done;
}
if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
&io_req->req_flags)) {
rc = bnx2fc_initiate_abts(io_req);
if (rc == SUCCESS)
goto done;
kref_put(&io_req->refcount, bnx2fc_cmd_release);
spin_unlock_bh(&tgt->tgt_lock);
return;
} else {
BNX2FC_IO_DBG(io_req, "IO already in "
"ABTS processing\n");
}
}
break;
case BNX2FC_ELS:
if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags)) {
BNX2FC_IO_DBG(io_req, "ABTS for ELS timed out\n");
if (!test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
&io_req->req_flags)) {
kref_put(&io_req->refcount, bnx2fc_cmd_release);
spin_unlock_bh(&tgt->tgt_lock);
return;
}
} else {
/*
* Handle ELS timeout.
* tgt_lock is used to sync compl path and timeout
* path. If els compl path is processing this IO, we
* have nothing to do here, just release the timer hold
*/
BNX2FC_IO_DBG(io_req, "ELS timed out\n");
if (test_and_set_bit(BNX2FC_FLAG_ELS_DONE,
&io_req->req_flags))
goto done;
/* Indicate the cb_func that this ELS is timed out */
set_bit(BNX2FC_FLAG_ELS_TIMEOUT, &io_req->req_flags);
if ((io_req->cb_func) && (io_req->cb_arg)) {
io_req->cb_func(io_req->cb_arg);
io_req->cb_arg = NULL;
}
}
break;
default:
printk(KERN_ERR PFX "cmd_timeout: invalid cmd_type %d\n",
cmd_type);
break;
}
done:
/* release the cmd that was held when timer was set */
kref_put(&io_req->refcount, bnx2fc_cmd_release);
spin_unlock_bh(&tgt->tgt_lock);
}
static void bnx2fc_scsi_done(struct bnx2fc_cmd *io_req, int err_code)
{
/* Called with host lock held */
struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
/*
* active_cmd_queue may have other command types as well,
* and during flush operation, we want to error back only
* scsi commands.
*/
if (io_req->cmd_type != BNX2FC_SCSI_CMD)
return;
BNX2FC_IO_DBG(io_req, "scsi_done. err_code = 0x%x\n", err_code);
if (test_bit(BNX2FC_FLAG_CMD_LOST, &io_req->req_flags)) {
/* Do not call scsi done for this IO */
return;
}
bnx2fc_unmap_sg_list(io_req);
io_req->sc_cmd = NULL;
/* Sanity checks before returning command to mid-layer */
if (!sc_cmd) {
printk(KERN_ERR PFX "scsi_done - sc_cmd NULL. "
"IO(0x%x) already cleaned up\n",
io_req->xid);
return;
}
if (!sc_cmd->device) {
pr_err(PFX "0x%x: sc_cmd->device is NULL.\n", io_req->xid);
return;
}
if (!sc_cmd->device->host) {
pr_err(PFX "0x%x: sc_cmd->device->host is NULL.\n",
io_req->xid);
return;
}
sc_cmd->result = err_code << 16;
BNX2FC_IO_DBG(io_req, "sc=%p, result=0x%x, retries=%d, allowed=%d\n",
sc_cmd, host_byte(sc_cmd->result), sc_cmd->retries,
sc_cmd->allowed);
scsi_set_resid(sc_cmd, scsi_bufflen(sc_cmd));
sc_cmd->SCp.ptr = NULL;
sc_cmd->scsi_done(sc_cmd);
}
struct bnx2fc_cmd_mgr *bnx2fc_cmd_mgr_alloc(struct bnx2fc_hba *hba)
{
struct bnx2fc_cmd_mgr *cmgr;
struct io_bdt *bdt_info;
struct bnx2fc_cmd *io_req;
size_t len;
u32 mem_size;
u16 xid;
int i;
int num_ios, num_pri_ios;
size_t bd_tbl_sz;
int arr_sz = num_possible_cpus() + 1;
u16 min_xid = BNX2FC_MIN_XID;
u16 max_xid = hba->max_xid;
if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN) {
printk(KERN_ERR PFX "cmd_mgr_alloc: Invalid min_xid 0x%x \
and max_xid 0x%x\n", min_xid, max_xid);
return NULL;
}
BNX2FC_MISC_DBG("min xid 0x%x, max xid 0x%x\n", min_xid, max_xid);
num_ios = max_xid - min_xid + 1;
len = (num_ios * (sizeof(struct bnx2fc_cmd *)));
len += sizeof(struct bnx2fc_cmd_mgr);
cmgr = kzalloc(len, GFP_KERNEL);
if (!cmgr) {
printk(KERN_ERR PFX "failed to alloc cmgr\n");
return NULL;
}
cmgr->hba = hba;
cmgr->free_list = kcalloc(arr_sz, sizeof(*cmgr->free_list),
GFP_KERNEL);
if (!cmgr->free_list) {
printk(KERN_ERR PFX "failed to alloc free_list\n");
goto mem_err;
}
cmgr->free_list_lock = kcalloc(arr_sz, sizeof(*cmgr->free_list_lock),
GFP_KERNEL);
if (!cmgr->free_list_lock) {
printk(KERN_ERR PFX "failed to alloc free_list_lock\n");
kfree(cmgr->free_list);
cmgr->free_list = NULL;
goto mem_err;
}
cmgr->cmds = (struct bnx2fc_cmd **)(cmgr + 1);
for (i = 0; i < arr_sz; i++) {
INIT_LIST_HEAD(&cmgr->free_list[i]);
spin_lock_init(&cmgr->free_list_lock[i]);
}
/*
* Pre-allocated pool of bnx2fc_cmds.
* Last entry in the free list array is the free list
* of slow path requests.
*/
xid = BNX2FC_MIN_XID;
num_pri_ios = num_ios - hba->elstm_xids;
for (i = 0; i < num_ios; i++) {
io_req = kzalloc(sizeof(*io_req), GFP_KERNEL);
if (!io_req) {
printk(KERN_ERR PFX "failed to alloc io_req\n");
goto mem_err;
}
INIT_LIST_HEAD(&io_req->link);
INIT_DELAYED_WORK(&io_req->timeout_work, bnx2fc_cmd_timeout);
io_req->xid = xid++;
if (i < num_pri_ios)
list_add_tail(&io_req->link,
&cmgr->free_list[io_req->xid %
num_possible_cpus()]);
else
list_add_tail(&io_req->link,
&cmgr->free_list[num_possible_cpus()]);
io_req++;
}
/* Allocate pool of io_bdts - one for each bnx2fc_cmd */
mem_size = num_ios * sizeof(struct io_bdt *);
cmgr->io_bdt_pool = kzalloc(mem_size, GFP_KERNEL);
if (!cmgr->io_bdt_pool) {
printk(KERN_ERR PFX "failed to alloc io_bdt_pool\n");
goto mem_err;
}
mem_size = sizeof(struct io_bdt);
for (i = 0; i < num_ios; i++) {
cmgr->io_bdt_pool[i] = kmalloc(mem_size, GFP_KERNEL);
if (!cmgr->io_bdt_pool[i]) {
printk(KERN_ERR PFX "failed to alloc "
"io_bdt_pool[%d]\n", i);
goto mem_err;
}
}
/* Allocate an map fcoe_bdt_ctx structures */
bd_tbl_sz = BNX2FC_MAX_BDS_PER_CMD * sizeof(struct fcoe_bd_ctx);
for (i = 0; i < num_ios; i++) {
bdt_info = cmgr->io_bdt_pool[i];
bdt_info->bd_tbl = dma_alloc_coherent(&hba->pcidev->dev,
bd_tbl_sz,
&bdt_info->bd_tbl_dma,
GFP_KERNEL);
if (!bdt_info->bd_tbl) {
printk(KERN_ERR PFX "failed to alloc "
"bdt_tbl[%d]\n", i);
goto mem_err;
}
}
return cmgr;
mem_err:
bnx2fc_cmd_mgr_free(cmgr);
return NULL;
}
void bnx2fc_cmd_mgr_free(struct bnx2fc_cmd_mgr *cmgr)
{
struct io_bdt *bdt_info;
struct bnx2fc_hba *hba = cmgr->hba;
size_t bd_tbl_sz;
u16 min_xid = BNX2FC_MIN_XID;
u16 max_xid = hba->max_xid;
int num_ios;
int i;
num_ios = max_xid - min_xid + 1;
/* Free fcoe_bdt_ctx structures */
if (!cmgr->io_bdt_pool)
goto free_cmd_pool;
bd_tbl_sz = BNX2FC_MAX_BDS_PER_CMD * sizeof(struct fcoe_bd_ctx);
for (i = 0; i < num_ios; i++) {
bdt_info = cmgr->io_bdt_pool[i];
if (bdt_info->bd_tbl) {
dma_free_coherent(&hba->pcidev->dev, bd_tbl_sz,
bdt_info->bd_tbl,
bdt_info->bd_tbl_dma);
bdt_info->bd_tbl = NULL;
}
}
/* Destroy io_bdt pool */
for (i = 0; i < num_ios; i++) {
kfree(cmgr->io_bdt_pool[i]);
cmgr->io_bdt_pool[i] = NULL;
}
kfree(cmgr->io_bdt_pool);
cmgr->io_bdt_pool = NULL;
free_cmd_pool:
kfree(cmgr->free_list_lock);
/* Destroy cmd pool */
if (!cmgr->free_list)
goto free_cmgr;
for (i = 0; i < num_possible_cpus() + 1; i++) {
struct bnx2fc_cmd *tmp, *io_req;
list_for_each_entry_safe(io_req, tmp,
&cmgr->free_list[i], link) {
list_del(&io_req->link);
kfree(io_req);
}
}
kfree(cmgr->free_list);
free_cmgr:
/* Free command manager itself */
kfree(cmgr);
}
struct bnx2fc_cmd *bnx2fc_elstm_alloc(struct bnx2fc_rport *tgt, int type)
{
struct fcoe_port *port = tgt->port;
struct bnx2fc_interface *interface = port->priv;
struct bnx2fc_cmd_mgr *cmd_mgr = interface->hba->cmd_mgr;
struct bnx2fc_cmd *io_req;
struct list_head *listp;
struct io_bdt *bd_tbl;
int index = RESERVE_FREE_LIST_INDEX;
u32 free_sqes;
u32 max_sqes;
u16 xid;
max_sqes = tgt->max_sqes;
switch (type) {
case BNX2FC_TASK_MGMT_CMD:
max_sqes = BNX2FC_TM_MAX_SQES;
break;
case BNX2FC_ELS:
max_sqes = BNX2FC_ELS_MAX_SQES;
break;
default:
break;
}
/*
* NOTE: Free list insertions and deletions are protected with
* cmgr lock
*/
spin_lock_bh(&cmd_mgr->free_list_lock[index]);
free_sqes = atomic_read(&tgt->free_sqes);
if ((list_empty(&(cmd_mgr->free_list[index]))) ||
(tgt->num_active_ios.counter >= max_sqes) ||
(free_sqes + max_sqes <= BNX2FC_SQ_WQES_MAX)) {
BNX2FC_TGT_DBG(tgt, "No free els_tm cmds available "
"ios(%d):sqes(%d)\n",
tgt->num_active_ios.counter, tgt->max_sqes);
if (list_empty(&(cmd_mgr->free_list[index])))
printk(KERN_ERR PFX "elstm_alloc: list_empty\n");
spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
return NULL;
}
listp = (struct list_head *)
cmd_mgr->free_list[index].next;
list_del_init(listp);
io_req = (struct bnx2fc_cmd *) listp;
xid = io_req->xid;
cmd_mgr->cmds[xid] = io_req;
atomic_inc(&tgt->num_active_ios);
atomic_dec(&tgt->free_sqes);
spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
INIT_LIST_HEAD(&io_req->link);
io_req->port = port;
io_req->cmd_mgr = cmd_mgr;
io_req->req_flags = 0;
io_req->cmd_type = type;
/* Bind io_bdt for this io_req */
/* Have a static link between io_req and io_bdt_pool */
bd_tbl = io_req->bd_tbl = cmd_mgr->io_bdt_pool[xid];
bd_tbl->io_req = io_req;
/* Hold the io_req against deletion */
kref_init(&io_req->refcount);
return io_req;
}
struct bnx2fc_cmd *bnx2fc_cmd_alloc(struct bnx2fc_rport *tgt)
{
struct fcoe_port *port = tgt->port;
struct bnx2fc_interface *interface = port->priv;
struct bnx2fc_cmd_mgr *cmd_mgr = interface->hba->cmd_mgr;
struct bnx2fc_cmd *io_req;
struct list_head *listp;
struct io_bdt *bd_tbl;
u32 free_sqes;
u32 max_sqes;
u16 xid;
int index = get_cpu();
max_sqes = BNX2FC_SCSI_MAX_SQES;
/*
* NOTE: Free list insertions and deletions are protected with
* cmgr lock
*/
spin_lock_bh(&cmd_mgr->free_list_lock[index]);
free_sqes = atomic_read(&tgt->free_sqes);
if ((list_empty(&cmd_mgr->free_list[index])) ||
(tgt->num_active_ios.counter >= max_sqes) ||
(free_sqes + max_sqes <= BNX2FC_SQ_WQES_MAX)) {
spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
put_cpu();
return NULL;
}
listp = (struct list_head *)
cmd_mgr->free_list[index].next;
list_del_init(listp);
io_req = (struct bnx2fc_cmd *) listp;
xid = io_req->xid;
cmd_mgr->cmds[xid] = io_req;
atomic_inc(&tgt->num_active_ios);
atomic_dec(&tgt->free_sqes);
spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
put_cpu();
INIT_LIST_HEAD(&io_req->link);
io_req->port = port;
io_req->cmd_mgr = cmd_mgr;
io_req->req_flags = 0;
/* Bind io_bdt for this io_req */
/* Have a static link between io_req and io_bdt_pool */
bd_tbl = io_req->bd_tbl = cmd_mgr->io_bdt_pool[xid];
bd_tbl->io_req = io_req;
/* Hold the io_req against deletion */
kref_init(&io_req->refcount);
return io_req;
}
void bnx2fc_cmd_release(struct kref *ref)
{
struct bnx2fc_cmd *io_req = container_of(ref,
struct bnx2fc_cmd, refcount);
struct bnx2fc_cmd_mgr *cmd_mgr = io_req->cmd_mgr;
int index;
if (io_req->cmd_type == BNX2FC_SCSI_CMD)
index = io_req->xid % num_possible_cpus();
else
index = RESERVE_FREE_LIST_INDEX;
spin_lock_bh(&cmd_mgr->free_list_lock[index]);
if (io_req->cmd_type != BNX2FC_SCSI_CMD)
bnx2fc_free_mp_resc(io_req);
cmd_mgr->cmds[io_req->xid] = NULL;
/* Delete IO from retire queue */
list_del_init(&io_req->link);
/* Add it to the free list */
list_add(&io_req->link,
&cmd_mgr->free_list[index]);
atomic_dec(&io_req->tgt->num_active_ios);
spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
}
static void bnx2fc_free_mp_resc(struct bnx2fc_cmd *io_req)
{
struct bnx2fc_mp_req *mp_req = &(io_req->mp_req);
struct bnx2fc_interface *interface = io_req->port->priv;
struct bnx2fc_hba *hba = interface->hba;
size_t sz = sizeof(struct fcoe_bd_ctx);
/* clear tm flags */
mp_req->tm_flags = 0;
if (mp_req->mp_req_bd) {
dma_free_coherent(&hba->pcidev->dev, sz,
mp_req->mp_req_bd,
mp_req->mp_req_bd_dma);
mp_req->mp_req_bd = NULL;
}
if (mp_req->mp_resp_bd) {
dma_free_coherent(&hba->pcidev->dev, sz,
mp_req->mp_resp_bd,
mp_req->mp_resp_bd_dma);
mp_req->mp_resp_bd = NULL;
}
if (mp_req->req_buf) {
dma_free_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
mp_req->req_buf,
mp_req->req_buf_dma);
mp_req->req_buf = NULL;
}
if (mp_req->resp_buf) {
dma_free_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
mp_req->resp_buf,
mp_req->resp_buf_dma);
mp_req->resp_buf = NULL;
}
}
int bnx2fc_init_mp_req(struct bnx2fc_cmd *io_req)
{
struct bnx2fc_mp_req *mp_req;
struct fcoe_bd_ctx *mp_req_bd;
struct fcoe_bd_ctx *mp_resp_bd;
struct bnx2fc_interface *interface = io_req->port->priv;
struct bnx2fc_hba *hba = interface->hba;
dma_addr_t addr;
size_t sz;
mp_req = (struct bnx2fc_mp_req *)&(io_req->mp_req);
memset(mp_req, 0, sizeof(struct bnx2fc_mp_req));
if (io_req->cmd_type != BNX2FC_ELS) {
mp_req->req_len = sizeof(struct fcp_cmnd);
io_req->data_xfer_len = mp_req->req_len;
} else
mp_req->req_len = io_req->data_xfer_len;
mp_req->req_buf = dma_alloc_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
&mp_req->req_buf_dma,
GFP_ATOMIC);
if (!mp_req->req_buf) {
printk(KERN_ERR PFX "unable to alloc MP req buffer\n");
bnx2fc_free_mp_resc(io_req);
return FAILED;
}
mp_req->resp_buf = dma_alloc_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
&mp_req->resp_buf_dma,
GFP_ATOMIC);
if (!mp_req->resp_buf) {
printk(KERN_ERR PFX "unable to alloc TM resp buffer\n");
bnx2fc_free_mp_resc(io_req);
return FAILED;
}
memset(mp_req->req_buf, 0, CNIC_PAGE_SIZE);
memset(mp_req->resp_buf, 0, CNIC_PAGE_SIZE);
/* Allocate and map mp_req_bd and mp_resp_bd */
sz = sizeof(struct fcoe_bd_ctx);
mp_req->mp_req_bd = dma_alloc_coherent(&hba->pcidev->dev, sz,
&mp_req->mp_req_bd_dma,
GFP_ATOMIC);
if (!mp_req->mp_req_bd) {
printk(KERN_ERR PFX "unable to alloc MP req bd\n");
bnx2fc_free_mp_resc(io_req);
return FAILED;
}
mp_req->mp_resp_bd = dma_alloc_coherent(&hba->pcidev->dev, sz,
&mp_req->mp_resp_bd_dma,
GFP_ATOMIC);
if (!mp_req->mp_resp_bd) {
printk(KERN_ERR PFX "unable to alloc MP resp bd\n");
bnx2fc_free_mp_resc(io_req);
return FAILED;
}
/* Fill bd table */
addr = mp_req->req_buf_dma;
mp_req_bd = mp_req->mp_req_bd;
mp_req_bd->buf_addr_lo = (u32)addr & 0xffffffff;
mp_req_bd->buf_addr_hi = (u32)((u64)addr >> 32);
mp_req_bd->buf_len = CNIC_PAGE_SIZE;
mp_req_bd->flags = 0;
/*
* MP buffer is either a task mgmt command or an ELS.
* So the assumption is that it consumes a single bd
* entry in the bd table
*/
mp_resp_bd = mp_req->mp_resp_bd;
addr = mp_req->resp_buf_dma;
mp_resp_bd->buf_addr_lo = (u32)addr & 0xffffffff;
mp_resp_bd->buf_addr_hi = (u32)((u64)addr >> 32);
mp_resp_bd->buf_len = CNIC_PAGE_SIZE;
mp_resp_bd->flags = 0;
return SUCCESS;
}
static int bnx2fc_initiate_tmf(struct scsi_cmnd *sc_cmd, u8 tm_flags)
{
struct fc_lport *lport;
struct fc_rport *rport;
struct fc_rport_libfc_priv *rp;
struct fcoe_port *port;
struct bnx2fc_interface *interface;
struct bnx2fc_rport *tgt;
struct bnx2fc_cmd *io_req;
struct bnx2fc_mp_req *tm_req;
struct fcoe_task_ctx_entry *task;
struct fcoe_task_ctx_entry *task_page;
struct Scsi_Host *host = sc_cmd->device->host;
struct fc_frame_header *fc_hdr;
struct fcp_cmnd *fcp_cmnd;
int task_idx, index;
int rc = SUCCESS;
u16 xid;
u32 sid, did;
unsigned long start = jiffies;
lport = shost_priv(host);
rport = starget_to_rport(scsi_target(sc_cmd->device));
port = lport_priv(lport);
interface = port->priv;
if (rport == NULL) {
printk(KERN_ERR PFX "device_reset: rport is NULL\n");
rc = FAILED;
goto tmf_err;
}
rp = rport->dd_data;
rc = fc_block_scsi_eh(sc_cmd);
if (rc)
return rc;
if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
printk(KERN_ERR PFX "device_reset: link is not ready\n");
rc = FAILED;
goto tmf_err;
}
/* rport and tgt are allocated together, so tgt should be non-NULL */
tgt = (struct bnx2fc_rport *)&rp[1];
if (!(test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
printk(KERN_ERR PFX "device_reset: tgt not offloaded\n");
rc = FAILED;
goto tmf_err;
}
retry_tmf:
io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_TASK_MGMT_CMD);
if (!io_req) {
if (time_after(jiffies, start + HZ)) {
printk(KERN_ERR PFX "tmf: Failed TMF");
rc = FAILED;
goto tmf_err;
}
msleep(20);
goto retry_tmf;
}
/* Initialize rest of io_req fields */
io_req->sc_cmd = sc_cmd;
io_req->port = port;
io_req->tgt = tgt;
tm_req = (struct bnx2fc_mp_req *)&(io_req->mp_req);
rc = bnx2fc_init_mp_req(io_req);
if (rc == FAILED) {
printk(KERN_ERR PFX "Task mgmt MP request init failed\n");
spin_lock_bh(&tgt->tgt_lock);
kref_put(&io_req->refcount, bnx2fc_cmd_release);
spin_unlock_bh(&tgt->tgt_lock);
goto tmf_err;
}
/* Set TM flags */
io_req->io_req_flags = 0;
tm_req->tm_flags = tm_flags;
/* Fill FCP_CMND */
bnx2fc_build_fcp_cmnd(io_req, (struct fcp_cmnd *)tm_req->req_buf);
fcp_cmnd = (struct fcp_cmnd *)tm_req->req_buf;
memset(fcp_cmnd->fc_cdb, 0, sc_cmd->cmd_len);
fcp_cmnd->fc_dl = 0;
/* Fill FC header */
fc_hdr = &(tm_req->req_fc_hdr);
sid = tgt->sid;
did = rport->port_id;
__fc_fill_fc_hdr(fc_hdr, FC_RCTL_DD_UNSOL_CMD, did, sid,
FC_TYPE_FCP, FC_FC_FIRST_SEQ | FC_FC_END_SEQ |
FC_FC_SEQ_INIT, 0);
/* Obtain exchange id */
xid = io_req->xid;
BNX2FC_TGT_DBG(tgt, "Initiate TMF - xid = 0x%x\n", xid);
task_idx = xid/BNX2FC_TASKS_PER_PAGE;
index = xid % BNX2FC_TASKS_PER_PAGE;
/* Initialize task context for this IO request */
task_page = (struct fcoe_task_ctx_entry *)
interface->hba->task_ctx[task_idx];
task = &(task_page[index]);
bnx2fc_init_mp_task(io_req, task);
sc_cmd->SCp.ptr = (char *)io_req;
/* Obtain free SQ entry */
spin_lock_bh(&tgt->tgt_lock);
bnx2fc_add_2_sq(tgt, xid);
/* Enqueue the io_req to active_tm_queue */
io_req->on_tmf_queue = 1;
list_add_tail(&io_req->link, &tgt->active_tm_queue);
init_completion(&io_req->abts_done);
io_req->wait_for_abts_comp = 1;
/* Ring doorbell */
bnx2fc_ring_doorbell(tgt);
spin_unlock_bh(&tgt->tgt_lock);
rc = wait_for_completion_timeout(&io_req->abts_done,
interface->tm_timeout * HZ);
spin_lock_bh(&tgt->tgt_lock);
io_req->wait_for_abts_comp = 0;
if (!(test_bit(BNX2FC_FLAG_TM_COMPL, &io_req->req_flags))) {
set_bit(BNX2FC_FLAG_TM_TIMEOUT, &io_req->req_flags);
if (io_req->on_tmf_queue) {
list_del_init(&io_req->link);
io_req->on_tmf_queue = 0;
}
io_req->wait_for_cleanup_comp = 1;
init_completion(&io_req->cleanup_done);
bnx2fc_initiate_cleanup(io_req);
spin_unlock_bh(&tgt->tgt_lock);
rc = wait_for_completion_timeout(&io_req->cleanup_done,
BNX2FC_FW_TIMEOUT);
spin_lock_bh(&tgt->tgt_lock);
io_req->wait_for_cleanup_comp = 0;
if (!rc)
kref_put(&io_req->refcount, bnx2fc_cmd_release);
}
spin_unlock_bh(&tgt->tgt_lock);
if (!rc) {
BNX2FC_TGT_DBG(tgt, "task mgmt command failed...\n");
rc = FAILED;
} else {
BNX2FC_TGT_DBG(tgt, "task mgmt command success...\n");
rc = SUCCESS;
}
tmf_err:
return rc;
}
int bnx2fc_initiate_abts(struct bnx2fc_cmd *io_req)
{
struct fc_lport *lport;
struct bnx2fc_rport *tgt = io_req->tgt;
struct fc_rport *rport = tgt->rport;
struct fc_rport_priv *rdata = tgt->rdata;
struct bnx2fc_interface *interface;
struct fcoe_port *port;
struct bnx2fc_cmd *abts_io_req;
struct fcoe_task_ctx_entry *task;
struct fcoe_task_ctx_entry *task_page;
struct fc_frame_header *fc_hdr;
struct bnx2fc_mp_req *abts_req;
int task_idx, index;
u32 sid, did;
u16 xid;
int rc = SUCCESS;
u32 r_a_tov = rdata->r_a_tov;
/* called with tgt_lock held */
BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_abts\n");
port = io_req->port;
interface = port->priv;
lport = port->lport;
if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
printk(KERN_ERR PFX "initiate_abts: tgt not offloaded\n");
rc = FAILED;
goto abts_err;
}
if (rport == NULL) {
printk(KERN_ERR PFX "initiate_abts: rport is NULL\n");
rc = FAILED;
goto abts_err;
}
if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
printk(KERN_ERR PFX "initiate_abts: link is not ready\n");
rc = FAILED;
goto abts_err;
}
abts_io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_ABTS);
if (!abts_io_req) {
printk(KERN_ERR PFX "abts: couldn't allocate cmd\n");
rc = FAILED;
goto abts_err;
}
/* Initialize rest of io_req fields */
abts_io_req->sc_cmd = NULL;
abts_io_req->port = port;
abts_io_req->tgt = tgt;
abts_io_req->data_xfer_len = 0; /* No data transfer for ABTS */
abts_req = (struct bnx2fc_mp_req *)&(abts_io_req->mp_req);
memset(abts_req, 0, sizeof(struct bnx2fc_mp_req));
/* Fill FC header */
fc_hdr = &(abts_req->req_fc_hdr);
/* Obtain oxid and rxid for the original exchange to be aborted */
fc_hdr->fh_ox_id = htons(io_req->xid);
fc_hdr->fh_rx_id = htons(io_req->task->rxwr_txrd.var_ctx.rx_id);
sid = tgt->sid;
did = rport->port_id;
__fc_fill_fc_hdr(fc_hdr, FC_RCTL_BA_ABTS, did, sid,
FC_TYPE_BLS, FC_FC_FIRST_SEQ | FC_FC_END_SEQ |
FC_FC_SEQ_INIT, 0);
xid = abts_io_req->xid;
BNX2FC_IO_DBG(abts_io_req, "ABTS io_req\n");
task_idx = xid/BNX2FC_TASKS_PER_PAGE;
index = xid % BNX2FC_TASKS_PER_PAGE;
/* Initialize task context for this IO request */
task_page = (struct fcoe_task_ctx_entry *)
interface->hba->task_ctx[task_idx];
task = &(task_page[index]);
bnx2fc_init_mp_task(abts_io_req, task);
/*
* ABTS task is a temporary task that will be cleaned up
* irrespective of ABTS response. We need to start the timer
* for the original exchange, as the CQE is posted for the original
* IO request.
*
* Timer for ABTS is started only when it is originated by a
* TM request. For the ABTS issued as part of ULP timeout,
* scsi-ml maintains the timers.
*/
/* if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags))*/
bnx2fc_cmd_timer_set(io_req, 2 * r_a_tov);
/* Obtain free SQ entry */
bnx2fc_add_2_sq(tgt, xid);
/* Ring doorbell */
bnx2fc_ring_doorbell(tgt);
abts_err:
return rc;
}
int bnx2fc_initiate_seq_cleanup(struct bnx2fc_cmd *orig_io_req, u32 offset,
enum fc_rctl r_ctl)
{
struct bnx2fc_rport *tgt = orig_io_req->tgt;
struct bnx2fc_interface *interface;
struct fcoe_port *port;
struct bnx2fc_cmd *seq_clnp_req;
struct fcoe_task_ctx_entry *task;
struct fcoe_task_ctx_entry *task_page;
struct bnx2fc_els_cb_arg *cb_arg = NULL;
int task_idx, index;
u16 xid;
int rc = 0;
BNX2FC_IO_DBG(orig_io_req, "bnx2fc_initiate_seq_cleanup xid = 0x%x\n",
orig_io_req->xid);
kref_get(&orig_io_req->refcount);
port = orig_io_req->port;
interface = port->priv;
cb_arg = kzalloc(sizeof(struct bnx2fc_els_cb_arg), GFP_ATOMIC);
if (!cb_arg) {
printk(KERN_ERR PFX "Unable to alloc cb_arg for seq clnup\n");
rc = -ENOMEM;
goto cleanup_err;
}
seq_clnp_req = bnx2fc_elstm_alloc(tgt, BNX2FC_SEQ_CLEANUP);
if (!seq_clnp_req) {
printk(KERN_ERR PFX "cleanup: couldn't allocate cmd\n");
rc = -ENOMEM;
kfree(cb_arg);
goto cleanup_err;
}
/* Initialize rest of io_req fields */
seq_clnp_req->sc_cmd = NULL;
seq_clnp_req->port = port;
seq_clnp_req->tgt = tgt;
seq_clnp_req->data_xfer_len = 0; /* No data transfer for cleanup */
xid = seq_clnp_req->xid;
task_idx = xid/BNX2FC_TASKS_PER_PAGE;
index = xid % BNX2FC_TASKS_PER_PAGE;
/* Initialize task context for this IO request */
task_page = (struct fcoe_task_ctx_entry *)
interface->hba->task_ctx[task_idx];
task = &(task_page[index]);
cb_arg->aborted_io_req = orig_io_req;
cb_arg->io_req = seq_clnp_req;
cb_arg->r_ctl = r_ctl;
cb_arg->offset = offset;
seq_clnp_req->cb_arg = cb_arg;
printk(KERN_ERR PFX "call init_seq_cleanup_task\n");
bnx2fc_init_seq_cleanup_task(seq_clnp_req, task, orig_io_req, offset);
/* Obtain free SQ entry */
bnx2fc_add_2_sq(tgt, xid);
/* Ring doorbell */
bnx2fc_ring_doorbell(tgt);
cleanup_err:
return rc;
}
int bnx2fc_initiate_cleanup(struct bnx2fc_cmd *io_req)
{
struct bnx2fc_rport *tgt = io_req->tgt;