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#include <linux/device.h>
36#include <linux/delay.h>
37#include <linux/ctype.h>
38#include <linux/kernel.h>
39#include <linux/slab.h>
40#include <linux/string.h>
41#include <linux/compiler.h>
42#include <linux/export.h>
43#include <linux/module.h>
44#include <asm/unaligned.h>
45#include <asm/page.h>
46#include <scsi/scsi.h>
47#include <scsi/scsi_device.h>
48#include <scsi/scsi_transport_fc.h>
49
50#include "csio_hw.h"
51#include "csio_lnode.h"
52#include "csio_rnode.h"
53#include "csio_scsi.h"
54#include "csio_init.h"
55
56int csio_scsi_eqsize = 65536;
57int csio_scsi_iqlen = 128;
58int csio_scsi_ioreqs = 2048;
59uint32_t csio_max_scan_tmo;
60uint32_t csio_delta_scan_tmo = 5;
61int csio_lun_qdepth = 32;
62
63static int csio_ddp_descs = 128;
64
65static int csio_do_abrt_cls(struct csio_hw *,
66 struct csio_ioreq *, bool);
67
68static void csio_scsis_uninit(struct csio_ioreq *, enum csio_scsi_ev);
69static void csio_scsis_io_active(struct csio_ioreq *, enum csio_scsi_ev);
70static void csio_scsis_tm_active(struct csio_ioreq *, enum csio_scsi_ev);
71static void csio_scsis_aborting(struct csio_ioreq *, enum csio_scsi_ev);
72static void csio_scsis_closing(struct csio_ioreq *, enum csio_scsi_ev);
73static void csio_scsis_shost_cmpl_await(struct csio_ioreq *, enum csio_scsi_ev);
74
75
76
77
78
79
80
81
82
83static bool
84csio_scsi_match_io(struct csio_ioreq *ioreq, struct csio_scsi_level_data *sld)
85{
86 struct scsi_cmnd *scmnd = csio_scsi_cmnd(ioreq);
87
88 switch (sld->level) {
89 case CSIO_LEV_LUN:
90 if (scmnd == NULL)
91 return false;
92
93 return ((ioreq->lnode == sld->lnode) &&
94 (ioreq->rnode == sld->rnode) &&
95 ((uint64_t)scmnd->device->lun == sld->oslun));
96
97 case CSIO_LEV_RNODE:
98 return ((ioreq->lnode == sld->lnode) &&
99 (ioreq->rnode == sld->rnode));
100 case CSIO_LEV_LNODE:
101 return (ioreq->lnode == sld->lnode);
102 case CSIO_LEV_ALL:
103 return true;
104 default:
105 return false;
106 }
107}
108
109
110
111
112
113
114
115
116
117static void
118csio_scsi_gather_active_ios(struct csio_scsim *scm,
119 struct csio_scsi_level_data *sld,
120 struct list_head *dest)
121{
122 struct list_head *tmp, *next;
123
124 if (list_empty(&scm->active_q))
125 return;
126
127
128 if (sld->level == CSIO_LEV_ALL) {
129 list_splice_tail_init(&scm->active_q, dest);
130 return;
131 }
132
133 list_for_each_safe(tmp, next, &scm->active_q) {
134 if (csio_scsi_match_io((struct csio_ioreq *)tmp, sld)) {
135 list_del_init(tmp);
136 list_add_tail(tmp, dest);
137 }
138 }
139}
140
141static inline bool
142csio_scsi_itnexus_loss_error(uint16_t error)
143{
144 switch (error) {
145 case FW_ERR_LINK_DOWN:
146 case FW_RDEV_NOT_READY:
147 case FW_ERR_RDEV_LOST:
148 case FW_ERR_RDEV_LOGO:
149 case FW_ERR_RDEV_IMPL_LOGO:
150 return 1;
151 }
152 return 0;
153}
154
155
156
157
158
159
160
161
162static inline void
163csio_scsi_fcp_cmnd(struct csio_ioreq *req, void *addr)
164{
165 struct fcp_cmnd *fcp_cmnd = (struct fcp_cmnd *)addr;
166 struct scsi_cmnd *scmnd = csio_scsi_cmnd(req);
167
168
169 if (likely(scmnd->SCp.Message == 0)) {
170 int_to_scsilun(scmnd->device->lun, &fcp_cmnd->fc_lun);
171 fcp_cmnd->fc_tm_flags = 0;
172 fcp_cmnd->fc_cmdref = 0;
173
174 memcpy(fcp_cmnd->fc_cdb, scmnd->cmnd, 16);
175 fcp_cmnd->fc_pri_ta = FCP_PTA_SIMPLE;
176 fcp_cmnd->fc_dl = cpu_to_be32(scsi_bufflen(scmnd));
177
178 if (req->nsge)
179 if (req->datadir == DMA_TO_DEVICE)
180 fcp_cmnd->fc_flags = FCP_CFL_WRDATA;
181 else
182 fcp_cmnd->fc_flags = FCP_CFL_RDDATA;
183 else
184 fcp_cmnd->fc_flags = 0;
185 } else {
186 memset(fcp_cmnd, 0, sizeof(*fcp_cmnd));
187 int_to_scsilun(scmnd->device->lun, &fcp_cmnd->fc_lun);
188 fcp_cmnd->fc_tm_flags = (uint8_t)scmnd->SCp.Message;
189 }
190}
191
192
193
194
195
196
197
198
199
200static inline void
201csio_scsi_init_cmd_wr(struct csio_ioreq *req, void *addr, uint32_t size)
202{
203 struct csio_hw *hw = req->lnode->hwp;
204 struct csio_rnode *rn = req->rnode;
205 struct fw_scsi_cmd_wr *wr = (struct fw_scsi_cmd_wr *)addr;
206 struct csio_dma_buf *dma_buf;
207 uint8_t imm = csio_hw_to_scsim(hw)->proto_cmd_len;
208
209 wr->op_immdlen = cpu_to_be32(FW_WR_OP_V(FW_SCSI_CMD_WR) |
210 FW_SCSI_CMD_WR_IMMDLEN(imm));
211 wr->flowid_len16 = cpu_to_be32(FW_WR_FLOWID_V(rn->flowid) |
212 FW_WR_LEN16_V(
213 DIV_ROUND_UP(size, 16)));
214
215 wr->cookie = (uintptr_t) req;
216 wr->iqid = cpu_to_be16(csio_q_physiqid(hw, req->iq_idx));
217 wr->tmo_val = (uint8_t) req->tmo;
218 wr->r3 = 0;
219 memset(&wr->r5, 0, 8);
220
221
222 dma_buf = &req->dma_buf;
223
224
225 wr->rsp_dmalen = cpu_to_be32(dma_buf->len);
226 wr->rsp_dmaaddr = cpu_to_be64(dma_buf->paddr);
227
228 wr->r6 = 0;
229
230 wr->u.fcoe.ctl_pri = 0;
231 wr->u.fcoe.cp_en_class = 0;
232 wr->u.fcoe.r4_lo[0] = 0;
233 wr->u.fcoe.r4_lo[1] = 0;
234
235
236 csio_scsi_fcp_cmnd(req, (void *)((uintptr_t)addr +
237 sizeof(struct fw_scsi_cmd_wr)));
238}
239
240#define CSIO_SCSI_CMD_WR_SZ(_imm) \
241 (sizeof(struct fw_scsi_cmd_wr) + \
242 ALIGN((_imm), 16))
243
244#define CSIO_SCSI_CMD_WR_SZ_16(_imm) \
245 (ALIGN(CSIO_SCSI_CMD_WR_SZ((_imm)), 16))
246
247
248
249
250
251
252
253
254static inline void
255csio_scsi_cmd(struct csio_ioreq *req)
256{
257 struct csio_wr_pair wrp;
258 struct csio_hw *hw = req->lnode->hwp;
259 struct csio_scsim *scsim = csio_hw_to_scsim(hw);
260 uint32_t size = CSIO_SCSI_CMD_WR_SZ_16(scsim->proto_cmd_len);
261
262 req->drv_status = csio_wr_get(hw, req->eq_idx, size, &wrp);
263 if (unlikely(req->drv_status != 0))
264 return;
265
266 if (wrp.size1 >= size) {
267
268 csio_scsi_init_cmd_wr(req, wrp.addr1, size);
269 } else {
270 uint8_t *tmpwr = csio_q_eq_wrap(hw, req->eq_idx);
271
272
273
274
275
276 csio_scsi_init_cmd_wr(req, (void *)tmpwr, size);
277 memcpy(wrp.addr1, tmpwr, wrp.size1);
278 memcpy(wrp.addr2, tmpwr + wrp.size1, size - wrp.size1);
279 }
280}
281
282
283
284
285
286
287
288
289static inline void
290csio_scsi_init_ultptx_dsgl(struct csio_hw *hw, struct csio_ioreq *req,
291 struct ulptx_sgl *sgl)
292{
293 struct ulptx_sge_pair *sge_pair = NULL;
294 struct scatterlist *sgel;
295 uint32_t i = 0;
296 uint32_t xfer_len;
297 struct list_head *tmp;
298 struct csio_dma_buf *dma_buf;
299 struct scsi_cmnd *scmnd = csio_scsi_cmnd(req);
300
301 sgl->cmd_nsge = htonl(ULPTX_CMD_V(ULP_TX_SC_DSGL) | ULPTX_MORE_F |
302 ULPTX_NSGE_V(req->nsge));
303
304 if (likely(!req->dcopy)) {
305 scsi_for_each_sg(scmnd, sgel, req->nsge, i) {
306 if (i == 0) {
307 sgl->addr0 = cpu_to_be64(sg_dma_address(sgel));
308 sgl->len0 = cpu_to_be32(sg_dma_len(sgel));
309 sge_pair = (struct ulptx_sge_pair *)(sgl + 1);
310 continue;
311 }
312 if ((i - 1) & 0x1) {
313 sge_pair->addr[1] = cpu_to_be64(
314 sg_dma_address(sgel));
315 sge_pair->len[1] = cpu_to_be32(
316 sg_dma_len(sgel));
317 sge_pair++;
318 } else {
319 sge_pair->addr[0] = cpu_to_be64(
320 sg_dma_address(sgel));
321 sge_pair->len[0] = cpu_to_be32(
322 sg_dma_len(sgel));
323 }
324 }
325 } else {
326
327 xfer_len = scsi_bufflen(scmnd);
328 list_for_each(tmp, &req->gen_list) {
329 dma_buf = (struct csio_dma_buf *)tmp;
330 if (i == 0) {
331 sgl->addr0 = cpu_to_be64(dma_buf->paddr);
332 sgl->len0 = cpu_to_be32(
333 min(xfer_len, dma_buf->len));
334 sge_pair = (struct ulptx_sge_pair *)(sgl + 1);
335 } else if ((i - 1) & 0x1) {
336 sge_pair->addr[1] = cpu_to_be64(dma_buf->paddr);
337 sge_pair->len[1] = cpu_to_be32(
338 min(xfer_len, dma_buf->len));
339 sge_pair++;
340 } else {
341 sge_pair->addr[0] = cpu_to_be64(dma_buf->paddr);
342 sge_pair->len[0] = cpu_to_be32(
343 min(xfer_len, dma_buf->len));
344 }
345 xfer_len -= min(xfer_len, dma_buf->len);
346 i++;
347 }
348 }
349}
350
351
352
353
354
355
356
357
358
359static inline void
360csio_scsi_init_read_wr(struct csio_ioreq *req, void *wrp, uint32_t size)
361{
362 struct csio_hw *hw = req->lnode->hwp;
363 struct csio_rnode *rn = req->rnode;
364 struct fw_scsi_read_wr *wr = (struct fw_scsi_read_wr *)wrp;
365 struct ulptx_sgl *sgl;
366 struct csio_dma_buf *dma_buf;
367 uint8_t imm = csio_hw_to_scsim(hw)->proto_cmd_len;
368 struct scsi_cmnd *scmnd = csio_scsi_cmnd(req);
369
370 wr->op_immdlen = cpu_to_be32(FW_WR_OP_V(FW_SCSI_READ_WR) |
371 FW_SCSI_READ_WR_IMMDLEN(imm));
372 wr->flowid_len16 = cpu_to_be32(FW_WR_FLOWID_V(rn->flowid) |
373 FW_WR_LEN16_V(DIV_ROUND_UP(size, 16)));
374 wr->cookie = (uintptr_t)req;
375 wr->iqid = cpu_to_be16(csio_q_physiqid(hw, req->iq_idx));
376 wr->tmo_val = (uint8_t)(req->tmo);
377 wr->use_xfer_cnt = 1;
378 wr->xfer_cnt = cpu_to_be32(scsi_bufflen(scmnd));
379 wr->ini_xfer_cnt = cpu_to_be32(scsi_bufflen(scmnd));
380
381 dma_buf = &req->dma_buf;
382
383
384 wr->rsp_dmalen = cpu_to_be32(dma_buf->len);
385 wr->rsp_dmaaddr = cpu_to_be64(dma_buf->paddr);
386
387 wr->r4 = 0;
388
389 wr->u.fcoe.ctl_pri = 0;
390 wr->u.fcoe.cp_en_class = 0;
391 wr->u.fcoe.r3_lo[0] = 0;
392 wr->u.fcoe.r3_lo[1] = 0;
393 csio_scsi_fcp_cmnd(req, (void *)((uintptr_t)wrp +
394 sizeof(struct fw_scsi_read_wr)));
395
396
397 sgl = (struct ulptx_sgl *)((uintptr_t)wrp +
398 sizeof(struct fw_scsi_read_wr) + ALIGN(imm, 16));
399
400
401 csio_scsi_init_ultptx_dsgl(hw, req, sgl);
402}
403
404
405
406
407
408
409
410
411
412static inline void
413csio_scsi_init_write_wr(struct csio_ioreq *req, void *wrp, uint32_t size)
414{
415 struct csio_hw *hw = req->lnode->hwp;
416 struct csio_rnode *rn = req->rnode;
417 struct fw_scsi_write_wr *wr = (struct fw_scsi_write_wr *)wrp;
418 struct ulptx_sgl *sgl;
419 struct csio_dma_buf *dma_buf;
420 uint8_t imm = csio_hw_to_scsim(hw)->proto_cmd_len;
421 struct scsi_cmnd *scmnd = csio_scsi_cmnd(req);
422
423 wr->op_immdlen = cpu_to_be32(FW_WR_OP_V(FW_SCSI_WRITE_WR) |
424 FW_SCSI_WRITE_WR_IMMDLEN(imm));
425 wr->flowid_len16 = cpu_to_be32(FW_WR_FLOWID_V(rn->flowid) |
426 FW_WR_LEN16_V(DIV_ROUND_UP(size, 16)));
427 wr->cookie = (uintptr_t)req;
428 wr->iqid = cpu_to_be16(csio_q_physiqid(hw, req->iq_idx));
429 wr->tmo_val = (uint8_t)(req->tmo);
430 wr->use_xfer_cnt = 1;
431 wr->xfer_cnt = cpu_to_be32(scsi_bufflen(scmnd));
432 wr->ini_xfer_cnt = cpu_to_be32(scsi_bufflen(scmnd));
433
434 dma_buf = &req->dma_buf;
435
436
437 wr->rsp_dmalen = cpu_to_be32(dma_buf->len);
438 wr->rsp_dmaaddr = cpu_to_be64(dma_buf->paddr);
439
440 wr->r4 = 0;
441
442 wr->u.fcoe.ctl_pri = 0;
443 wr->u.fcoe.cp_en_class = 0;
444 wr->u.fcoe.r3_lo[0] = 0;
445 wr->u.fcoe.r3_lo[1] = 0;
446 csio_scsi_fcp_cmnd(req, (void *)((uintptr_t)wrp +
447 sizeof(struct fw_scsi_write_wr)));
448
449
450 sgl = (struct ulptx_sgl *)((uintptr_t)wrp +
451 sizeof(struct fw_scsi_write_wr) + ALIGN(imm, 16));
452
453
454 csio_scsi_init_ultptx_dsgl(hw, req, sgl);
455}
456
457
458#define CSIO_SCSI_DATA_WRSZ(req, oper, sz, imm) \
459do { \
460 (sz) = sizeof(struct fw_scsi_##oper##_wr) + \
461 ALIGN((imm), 16) + \
462 sizeof(struct ulptx_sgl); \
463 \
464 if (unlikely((req)->nsge > 1)) \
465 (sz) += (sizeof(struct ulptx_sge_pair) * \
466 (ALIGN(((req)->nsge - 1), 2) / 2)); \
467 \
468} while (0)
469
470
471
472
473
474
475
476
477
478static inline void
479csio_scsi_read(struct csio_ioreq *req)
480{
481 struct csio_wr_pair wrp;
482 uint32_t size;
483 struct csio_hw *hw = req->lnode->hwp;
484 struct csio_scsim *scsim = csio_hw_to_scsim(hw);
485
486 CSIO_SCSI_DATA_WRSZ(req, read, size, scsim->proto_cmd_len);
487 size = ALIGN(size, 16);
488
489 req->drv_status = csio_wr_get(hw, req->eq_idx, size, &wrp);
490 if (likely(req->drv_status == 0)) {
491 if (likely(wrp.size1 >= size)) {
492
493 csio_scsi_init_read_wr(req, wrp.addr1, size);
494 } else {
495 uint8_t *tmpwr = csio_q_eq_wrap(hw, req->eq_idx);
496
497
498
499
500 csio_scsi_init_read_wr(req, (void *)tmpwr, size);
501 memcpy(wrp.addr1, tmpwr, wrp.size1);
502 memcpy(wrp.addr2, tmpwr + wrp.size1, size - wrp.size1);
503 }
504 }
505}
506
507
508
509
510
511
512
513
514
515static inline void
516csio_scsi_write(struct csio_ioreq *req)
517{
518 struct csio_wr_pair wrp;
519 uint32_t size;
520 struct csio_hw *hw = req->lnode->hwp;
521 struct csio_scsim *scsim = csio_hw_to_scsim(hw);
522
523 CSIO_SCSI_DATA_WRSZ(req, write, size, scsim->proto_cmd_len);
524 size = ALIGN(size, 16);
525
526 req->drv_status = csio_wr_get(hw, req->eq_idx, size, &wrp);
527 if (likely(req->drv_status == 0)) {
528 if (likely(wrp.size1 >= size)) {
529
530 csio_scsi_init_write_wr(req, wrp.addr1, size);
531 } else {
532 uint8_t *tmpwr = csio_q_eq_wrap(hw, req->eq_idx);
533
534
535
536
537 csio_scsi_init_write_wr(req, (void *)tmpwr, size);
538 memcpy(wrp.addr1, tmpwr, wrp.size1);
539 memcpy(wrp.addr2, tmpwr + wrp.size1, size - wrp.size1);
540 }
541 }
542}
543
544
545
546
547
548
549
550
551
552static inline void
553csio_setup_ddp(struct csio_scsim *scsim, struct csio_ioreq *req)
554{
555#ifdef __CSIO_DEBUG__
556 struct csio_hw *hw = req->lnode->hwp;
557#endif
558 struct scatterlist *sgel = NULL;
559 struct scsi_cmnd *scmnd = csio_scsi_cmnd(req);
560 uint64_t sg_addr = 0;
561 uint32_t ddp_pagesz = 4096;
562 uint32_t buf_off;
563 struct csio_dma_buf *dma_buf = NULL;
564 uint32_t alloc_len = 0;
565 uint32_t xfer_len = 0;
566 uint32_t sg_len = 0;
567 uint32_t i;
568
569 scsi_for_each_sg(scmnd, sgel, req->nsge, i) {
570 sg_addr = sg_dma_address(sgel);
571 sg_len = sg_dma_len(sgel);
572
573 buf_off = sg_addr & (ddp_pagesz - 1);
574
575
576 if (i != 0 && buf_off) {
577 csio_dbg(hw, "SGL addr not DDP aligned (%llx:%d)\n",
578 sg_addr, sg_len);
579 goto unaligned;
580 }
581
582
583 if ((i != (req->nsge - 1)) &&
584 ((buf_off + sg_len) & (ddp_pagesz - 1))) {
585 csio_dbg(hw,
586 "SGL addr not ending on page boundary"
587 "(%llx:%d)\n", sg_addr, sg_len);
588 goto unaligned;
589 }
590 }
591
592
593 req->dcopy = 0;
594 csio_scsi_read(req);
595
596 return;
597
598unaligned:
599 CSIO_INC_STATS(scsim, n_unaligned);
600
601
602
603
604 req->dcopy = 1;
605
606
607 INIT_LIST_HEAD(&req->gen_list);
608 xfer_len = scsi_bufflen(scmnd);
609
610 i = 0;
611
612 while (alloc_len < xfer_len) {
613 dma_buf = csio_get_scsi_ddp(scsim);
614 if (dma_buf == NULL || i > scsim->max_sge) {
615 req->drv_status = -EBUSY;
616 break;
617 }
618 alloc_len += dma_buf->len;
619
620 list_add_tail(&dma_buf->list, &req->gen_list);
621 i++;
622 }
623
624 if (!req->drv_status) {
625
626 req->nsge = i;
627 csio_scsi_read(req);
628 return;
629 }
630
631
632 if (i > 0)
633 csio_put_scsi_ddp_list(scsim, &req->gen_list, i);
634}
635
636
637
638
639
640
641
642
643
644
645static inline void
646csio_scsi_init_abrt_cls_wr(struct csio_ioreq *req, void *addr, uint32_t size,
647 bool abort)
648{
649 struct csio_hw *hw = req->lnode->hwp;
650 struct csio_rnode *rn = req->rnode;
651 struct fw_scsi_abrt_cls_wr *wr = (struct fw_scsi_abrt_cls_wr *)addr;
652
653 wr->op_immdlen = cpu_to_be32(FW_WR_OP_V(FW_SCSI_ABRT_CLS_WR));
654 wr->flowid_len16 = cpu_to_be32(FW_WR_FLOWID_V(rn->flowid) |
655 FW_WR_LEN16_V(
656 DIV_ROUND_UP(size, 16)));
657
658 wr->cookie = (uintptr_t) req;
659 wr->iqid = cpu_to_be16(csio_q_physiqid(hw, req->iq_idx));
660 wr->tmo_val = (uint8_t) req->tmo;
661
662 wr->sub_opcode_to_chk_all_io =
663 (FW_SCSI_ABRT_CLS_WR_SUB_OPCODE(abort) |
664 FW_SCSI_ABRT_CLS_WR_CHK_ALL_IO(0));
665 wr->r3[0] = 0;
666 wr->r3[1] = 0;
667 wr->r3[2] = 0;
668 wr->r3[3] = 0;
669
670 wr->t_cookie = (uintptr_t) req;
671}
672
673static inline void
674csio_scsi_abrt_cls(struct csio_ioreq *req, bool abort)
675{
676 struct csio_wr_pair wrp;
677 struct csio_hw *hw = req->lnode->hwp;
678 uint32_t size = ALIGN(sizeof(struct fw_scsi_abrt_cls_wr), 16);
679
680 req->drv_status = csio_wr_get(hw, req->eq_idx, size, &wrp);
681 if (req->drv_status != 0)
682 return;
683
684 if (wrp.size1 >= size) {
685
686 csio_scsi_init_abrt_cls_wr(req, wrp.addr1, size, abort);
687 } else {
688 uint8_t *tmpwr = csio_q_eq_wrap(hw, req->eq_idx);
689
690
691
692
693 csio_scsi_init_abrt_cls_wr(req, (void *)tmpwr, size, abort);
694 memcpy(wrp.addr1, tmpwr, wrp.size1);
695 memcpy(wrp.addr2, tmpwr + wrp.size1, size - wrp.size1);
696 }
697}
698
699
700
701
702static void
703csio_scsis_uninit(struct csio_ioreq *req, enum csio_scsi_ev evt)
704{
705 struct csio_hw *hw = req->lnode->hwp;
706 struct csio_scsim *scsim = csio_hw_to_scsim(hw);
707
708 switch (evt) {
709 case CSIO_SCSIE_START_IO:
710
711 if (req->nsge) {
712 if (req->datadir == DMA_TO_DEVICE) {
713 req->dcopy = 0;
714 csio_scsi_write(req);
715 } else
716 csio_setup_ddp(scsim, req);
717 } else {
718 csio_scsi_cmd(req);
719 }
720
721 if (likely(req->drv_status == 0)) {
722
723 csio_set_state(&req->sm, csio_scsis_io_active);
724 list_add_tail(&req->sm.sm_list, &scsim->active_q);
725 csio_wr_issue(hw, req->eq_idx, false);
726 CSIO_INC_STATS(scsim, n_active);
727
728 return;
729 }
730 break;
731
732 case CSIO_SCSIE_START_TM:
733 csio_scsi_cmd(req);
734 if (req->drv_status == 0) {
735
736
737
738
739
740
741
742
743 csio_set_state(&req->sm, csio_scsis_tm_active);
744 list_add_tail(&req->sm.sm_list, &scsim->active_q);
745 csio_wr_issue(hw, req->eq_idx, false);
746 CSIO_INC_STATS(scsim, n_tm_active);
747 }
748 return;
749
750 case CSIO_SCSIE_ABORT:
751 case CSIO_SCSIE_CLOSE:
752
753
754
755
756
757
758
759
760
761
762 req->drv_status = -EINVAL;
763 csio_warn(hw, "Trying to abort/close completed IO:%p!\n", req);
764 break;
765
766 default:
767 csio_dbg(hw, "Unhandled event:%d sent to req:%p\n", evt, req);
768 CSIO_DB_ASSERT(0);
769 }
770}
771
772static void
773csio_scsis_io_active(struct csio_ioreq *req, enum csio_scsi_ev evt)
774{
775 struct csio_hw *hw = req->lnode->hwp;
776 struct csio_scsim *scm = csio_hw_to_scsim(hw);
777 struct csio_rnode *rn;
778
779 switch (evt) {
780 case CSIO_SCSIE_COMPLETED:
781 CSIO_DEC_STATS(scm, n_active);
782 list_del_init(&req->sm.sm_list);
783 csio_set_state(&req->sm, csio_scsis_uninit);
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799 if (unlikely(req->wr_status != FW_SUCCESS)) {
800 rn = req->rnode;
801
802
803
804
805 if (csio_scsi_itnexus_loss_error(req->wr_status) &&
806 csio_is_rnode_ready(rn)) {
807 csio_set_state(&req->sm,
808 csio_scsis_shost_cmpl_await);
809 list_add_tail(&req->sm.sm_list,
810 &rn->host_cmpl_q);
811 }
812 }
813
814 break;
815
816 case CSIO_SCSIE_ABORT:
817 csio_scsi_abrt_cls(req, SCSI_ABORT);
818 if (req->drv_status == 0) {
819 csio_wr_issue(hw, req->eq_idx, false);
820 csio_set_state(&req->sm, csio_scsis_aborting);
821 }
822 break;
823
824 case CSIO_SCSIE_CLOSE:
825 csio_scsi_abrt_cls(req, SCSI_CLOSE);
826 if (req->drv_status == 0) {
827 csio_wr_issue(hw, req->eq_idx, false);
828 csio_set_state(&req->sm, csio_scsis_closing);
829 }
830 break;
831
832 case CSIO_SCSIE_DRVCLEANUP:
833 req->wr_status = FW_HOSTERROR;
834 CSIO_DEC_STATS(scm, n_active);
835 csio_set_state(&req->sm, csio_scsis_uninit);
836 break;
837
838 default:
839 csio_dbg(hw, "Unhandled event:%d sent to req:%p\n", evt, req);
840 CSIO_DB_ASSERT(0);
841 }
842}
843
844static void
845csio_scsis_tm_active(struct csio_ioreq *req, enum csio_scsi_ev evt)
846{
847 struct csio_hw *hw = req->lnode->hwp;
848 struct csio_scsim *scm = csio_hw_to_scsim(hw);
849
850 switch (evt) {
851 case CSIO_SCSIE_COMPLETED:
852 CSIO_DEC_STATS(scm, n_tm_active);
853 list_del_init(&req->sm.sm_list);
854 csio_set_state(&req->sm, csio_scsis_uninit);
855
856 break;
857
858 case CSIO_SCSIE_ABORT:
859 csio_scsi_abrt_cls(req, SCSI_ABORT);
860 if (req->drv_status == 0) {
861 csio_wr_issue(hw, req->eq_idx, false);
862 csio_set_state(&req->sm, csio_scsis_aborting);
863 }
864 break;
865
866
867 case CSIO_SCSIE_CLOSE:
868 csio_scsi_abrt_cls(req, SCSI_CLOSE);
869 if (req->drv_status == 0) {
870 csio_wr_issue(hw, req->eq_idx, false);
871 csio_set_state(&req->sm, csio_scsis_closing);
872 }
873 break;
874
875 case CSIO_SCSIE_DRVCLEANUP:
876 req->wr_status = FW_HOSTERROR;
877 CSIO_DEC_STATS(scm, n_tm_active);
878 csio_set_state(&req->sm, csio_scsis_uninit);
879 break;
880
881 default:
882 csio_dbg(hw, "Unhandled event:%d sent to req:%p\n", evt, req);
883 CSIO_DB_ASSERT(0);
884 }
885}
886
887static void
888csio_scsis_aborting(struct csio_ioreq *req, enum csio_scsi_ev evt)
889{
890 struct csio_hw *hw = req->lnode->hwp;
891 struct csio_scsim *scm = csio_hw_to_scsim(hw);
892
893 switch (evt) {
894 case CSIO_SCSIE_COMPLETED:
895 csio_dbg(hw,
896 "ioreq %p recvd cmpltd (wr_status:%d) "
897 "in aborting st\n", req, req->wr_status);
898
899
900
901
902
903
904
905
906
907 req->drv_status = -ECANCELED;
908 break;
909
910 case CSIO_SCSIE_ABORT:
911 CSIO_INC_STATS(scm, n_abrt_dups);
912 break;
913
914 case CSIO_SCSIE_ABORTED:
915
916 csio_dbg(hw, "abort of %p return status:0x%x drv_status:%x\n",
917 req, req->wr_status, req->drv_status);
918
919
920
921
922 if (req->drv_status != -ECANCELED) {
923 csio_warn(hw,
924 "Abort completed before original I/O,"
925 " req:%p\n", req);
926 CSIO_DB_ASSERT(0);
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 if ((req->wr_status == FW_SUCCESS) ||
953 (req->wr_status == FW_EINVAL) ||
954 csio_scsi_itnexus_loss_error(req->wr_status))
955 req->wr_status = FW_SCSI_ABORT_REQUESTED;
956
957 CSIO_DEC_STATS(scm, n_active);
958 list_del_init(&req->sm.sm_list);
959 csio_set_state(&req->sm, csio_scsis_uninit);
960 break;
961
962 case CSIO_SCSIE_DRVCLEANUP:
963 req->wr_status = FW_HOSTERROR;
964 CSIO_DEC_STATS(scm, n_active);
965 csio_set_state(&req->sm, csio_scsis_uninit);
966 break;
967
968 case CSIO_SCSIE_CLOSE:
969
970
971
972
973
974
975
976 break;
977
978 default:
979 csio_dbg(hw, "Unhandled event:%d sent to req:%p\n", evt, req);
980 CSIO_DB_ASSERT(0);
981 }
982}
983
984static void
985csio_scsis_closing(struct csio_ioreq *req, enum csio_scsi_ev evt)
986{
987 struct csio_hw *hw = req->lnode->hwp;
988 struct csio_scsim *scm = csio_hw_to_scsim(hw);
989
990 switch (evt) {
991 case CSIO_SCSIE_COMPLETED:
992 csio_dbg(hw,
993 "ioreq %p recvd cmpltd (wr_status:%d) "
994 "in closing st\n", req, req->wr_status);
995
996
997
998
999
1000
1001
1002
1003
1004 req->drv_status = -ECANCELED;
1005 break;
1006
1007 case CSIO_SCSIE_CLOSED:
1008
1009
1010
1011
1012 if (req->drv_status != -ECANCELED) {
1013 csio_fatal(hw,
1014 "Close completed before original I/O,"
1015 " req:%p\n", req);
1016 CSIO_DB_ASSERT(0);
1017 }
1018
1019
1020
1021
1022
1023
1024 CSIO_DB_ASSERT((req->wr_status == FW_SUCCESS) ||
1025 (req->wr_status == FW_EINVAL));
1026 req->wr_status = FW_SCSI_CLOSE_REQUESTED;
1027
1028 CSIO_DEC_STATS(scm, n_active);
1029 list_del_init(&req->sm.sm_list);
1030 csio_set_state(&req->sm, csio_scsis_uninit);
1031 break;
1032
1033 case CSIO_SCSIE_CLOSE:
1034 break;
1035
1036 case CSIO_SCSIE_DRVCLEANUP:
1037 req->wr_status = FW_HOSTERROR;
1038 CSIO_DEC_STATS(scm, n_active);
1039 csio_set_state(&req->sm, csio_scsis_uninit);
1040 break;
1041
1042 default:
1043 csio_dbg(hw, "Unhandled event:%d sent to req:%p\n", evt, req);
1044 CSIO_DB_ASSERT(0);
1045 }
1046}
1047
1048static void
1049csio_scsis_shost_cmpl_await(struct csio_ioreq *req, enum csio_scsi_ev evt)
1050{
1051 switch (evt) {
1052 case CSIO_SCSIE_ABORT:
1053 case CSIO_SCSIE_CLOSE:
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070 req->drv_status = 0;
1071 break;
1072 case CSIO_SCSIE_DRVCLEANUP:
1073 csio_set_state(&req->sm, csio_scsis_uninit);
1074 break;
1075 default:
1076 csio_dbg(req->lnode->hwp, "Unhandled event:%d sent to req:%p\n",
1077 evt, req);
1078 CSIO_DB_ASSERT(0);
1079 }
1080}
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101struct csio_ioreq *
1102csio_scsi_cmpl_handler(struct csio_hw *hw, void *wr, uint32_t len,
1103 struct csio_fl_dma_buf *flb, void *priv, uint8_t **scsiwr)
1104{
1105 struct csio_ioreq *ioreq = NULL;
1106 struct cpl_fw6_msg *cpl;
1107 uint8_t *tempwr;
1108 uint8_t status;
1109 struct csio_scsim *scm = csio_hw_to_scsim(hw);
1110
1111
1112 cpl = (struct cpl_fw6_msg *)((uintptr_t)wr + sizeof(__be64));
1113
1114 if (unlikely(cpl->opcode != CPL_FW6_MSG)) {
1115 csio_warn(hw, "Error: Invalid CPL msg %x recvd on SCSI q\n",
1116 cpl->opcode);
1117 CSIO_INC_STATS(scm, n_inval_cplop);
1118 return NULL;
1119 }
1120
1121 tempwr = (uint8_t *)(cpl->data);
1122 status = csio_wr_status(tempwr);
1123 *scsiwr = tempwr;
1124
1125 if (likely((*tempwr == FW_SCSI_READ_WR) ||
1126 (*tempwr == FW_SCSI_WRITE_WR) ||
1127 (*tempwr == FW_SCSI_CMD_WR))) {
1128 ioreq = (struct csio_ioreq *)((uintptr_t)
1129 (((struct fw_scsi_read_wr *)tempwr)->cookie));
1130 CSIO_DB_ASSERT(virt_addr_valid(ioreq));
1131
1132 ioreq->wr_status = status;
1133
1134 return ioreq;
1135 }
1136
1137 if (*tempwr == FW_SCSI_ABRT_CLS_WR) {
1138 ioreq = (struct csio_ioreq *)((uintptr_t)
1139 (((struct fw_scsi_abrt_cls_wr *)tempwr)->cookie));
1140 CSIO_DB_ASSERT(virt_addr_valid(ioreq));
1141
1142 ioreq->wr_status = status;
1143 return ioreq;
1144 }
1145
1146 csio_warn(hw, "WR with invalid opcode in SCSI IQ: %x\n", *tempwr);
1147 CSIO_INC_STATS(scm, n_inval_scsiop);
1148 return NULL;
1149}
1150
1151
1152
1153
1154
1155
1156
1157
1158void
1159csio_scsi_cleanup_io_q(struct csio_scsim *scm, struct list_head *q)
1160{
1161 struct csio_hw *hw = scm->hw;
1162 struct csio_ioreq *ioreq;
1163 struct list_head *tmp, *next;
1164 struct scsi_cmnd *scmnd;
1165
1166
1167 list_for_each_safe(tmp, next, q) {
1168 ioreq = (struct csio_ioreq *)tmp;
1169 csio_scsi_drvcleanup(ioreq);
1170 list_del_init(&ioreq->sm.sm_list);
1171 scmnd = csio_scsi_cmnd(ioreq);
1172 spin_unlock_irq(&hw->lock);
1173
1174
1175
1176
1177
1178 if (scmnd != NULL)
1179 ioreq->io_cbfn(hw, ioreq);
1180
1181 spin_lock_irq(&scm->freelist_lock);
1182 csio_put_scsi_ioreq(scm, ioreq);
1183 spin_unlock_irq(&scm->freelist_lock);
1184
1185 spin_lock_irq(&hw->lock);
1186 }
1187}
1188
1189#define CSIO_SCSI_ABORT_Q_POLL_MS 2000
1190
1191static void
1192csio_abrt_cls(struct csio_ioreq *ioreq, struct scsi_cmnd *scmnd)
1193{
1194 struct csio_lnode *ln = ioreq->lnode;
1195 struct csio_hw *hw = ln->hwp;
1196 int ready = 0;
1197 struct csio_scsim *scsim = csio_hw_to_scsim(hw);
1198 int rv;
1199
1200 if (csio_scsi_cmnd(ioreq) != scmnd) {
1201 CSIO_INC_STATS(scsim, n_abrt_race_comp);
1202 return;
1203 }
1204
1205 ready = csio_is_lnode_ready(ln);
1206
1207 rv = csio_do_abrt_cls(hw, ioreq, (ready ? SCSI_ABORT : SCSI_CLOSE));
1208 if (rv != 0) {
1209 if (ready)
1210 CSIO_INC_STATS(scsim, n_abrt_busy_error);
1211 else
1212 CSIO_INC_STATS(scsim, n_cls_busy_error);
1213 }
1214}
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232static int
1233csio_scsi_abort_io_q(struct csio_scsim *scm, struct list_head *q, uint32_t tmo)
1234{
1235 struct csio_hw *hw = scm->hw;
1236 struct list_head *tmp, *next;
1237 int count = DIV_ROUND_UP(tmo, CSIO_SCSI_ABORT_Q_POLL_MS);
1238 struct scsi_cmnd *scmnd;
1239
1240 if (list_empty(q))
1241 return 0;
1242
1243 csio_dbg(hw, "Aborting SCSI I/Os\n");
1244
1245
1246 list_for_each_safe(tmp, next, q) {
1247 scmnd = csio_scsi_cmnd((struct csio_ioreq *)tmp);
1248 csio_abrt_cls((struct csio_ioreq *)tmp, scmnd);
1249 }
1250
1251
1252 while (!list_empty(q) && count--) {
1253 spin_unlock_irq(&hw->lock);
1254 msleep(CSIO_SCSI_ABORT_Q_POLL_MS);
1255 spin_lock_irq(&hw->lock);
1256 }
1257
1258
1259 if (list_empty(q))
1260 return 0;
1261
1262 return -ETIMEDOUT;
1263}
1264
1265
1266
1267
1268
1269
1270
1271
1272int
1273csio_scsim_cleanup_io(struct csio_scsim *scm, bool abort)
1274{
1275 struct csio_hw *hw = scm->hw;
1276 int rv = 0;
1277 int count = DIV_ROUND_UP(60 * 1000, CSIO_SCSI_ABORT_Q_POLL_MS);
1278
1279
1280 if (list_empty(&scm->active_q))
1281 return 0;
1282
1283
1284 while (!list_empty(&scm->active_q) && count--) {
1285 spin_unlock_irq(&hw->lock);
1286 msleep(CSIO_SCSI_ABORT_Q_POLL_MS);
1287 spin_lock_irq(&hw->lock);
1288 }
1289
1290
1291 if (list_empty(&scm->active_q))
1292 return 0;
1293
1294
1295 if (abort) {
1296 rv = csio_scsi_abort_io_q(scm, &scm->active_q, 30000);
1297 if (rv == 0)
1298 return rv;
1299 csio_dbg(hw, "Some I/O aborts timed out, cleaning up..\n");
1300 }
1301
1302 csio_scsi_cleanup_io_q(scm, &scm->active_q);
1303
1304 CSIO_DB_ASSERT(list_empty(&scm->active_q));
1305
1306 return rv;
1307}
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317int
1318csio_scsim_cleanup_io_lnode(struct csio_scsim *scm, struct csio_lnode *ln)
1319{
1320 struct csio_hw *hw = scm->hw;
1321 struct csio_scsi_level_data sld;
1322 int rv;
1323 int count = DIV_ROUND_UP(60 * 1000, CSIO_SCSI_ABORT_Q_POLL_MS);
1324
1325 csio_dbg(hw, "Gathering all SCSI I/Os on lnode %p\n", ln);
1326
1327 sld.level = CSIO_LEV_LNODE;
1328 sld.lnode = ln;
1329 INIT_LIST_HEAD(&ln->cmpl_q);
1330 csio_scsi_gather_active_ios(scm, &sld, &ln->cmpl_q);
1331
1332
1333 if (list_empty(&ln->cmpl_q))
1334 return 0;
1335
1336
1337 while (!list_empty(&ln->cmpl_q) && count--) {
1338 spin_unlock_irq(&hw->lock);
1339 msleep(CSIO_SCSI_ABORT_Q_POLL_MS);
1340 spin_lock_irq(&hw->lock);
1341 }
1342
1343
1344 if (list_empty(&ln->cmpl_q))
1345 return 0;
1346
1347 csio_dbg(hw, "Some I/Os pending on ln:%p, aborting them..\n", ln);
1348
1349
1350 rv = csio_scsi_abort_io_q(scm, &ln->cmpl_q, 30000);
1351 if (rv != 0) {
1352 csio_dbg(hw, "Some I/O aborts timed out, cleaning up..\n");
1353 csio_scsi_cleanup_io_q(scm, &ln->cmpl_q);
1354 }
1355
1356 CSIO_DB_ASSERT(list_empty(&ln->cmpl_q));
1357
1358 return rv;
1359}
1360
1361static ssize_t
1362csio_show_hw_state(struct device *dev,
1363 struct device_attribute *attr, char *buf)
1364{
1365 struct csio_lnode *ln = shost_priv(class_to_shost(dev));
1366 struct csio_hw *hw = csio_lnode_to_hw(ln);
1367
1368 if (csio_is_hw_ready(hw))
1369 return snprintf(buf, PAGE_SIZE, "ready\n");
1370 else
1371 return snprintf(buf, PAGE_SIZE, "not ready\n");
1372}
1373
1374
1375static ssize_t
1376csio_device_reset(struct device *dev,
1377 struct device_attribute *attr, const char *buf, size_t count)
1378{
1379 struct csio_lnode *ln = shost_priv(class_to_shost(dev));
1380 struct csio_hw *hw = csio_lnode_to_hw(ln);
1381
1382 if (*buf != '1')
1383 return -EINVAL;
1384
1385
1386 csio_lnodes_exit(hw, 1);
1387
1388
1389 csio_lnodes_block_request(hw);
1390
1391 spin_lock_irq(&hw->lock);
1392 csio_hw_reset(hw);
1393 spin_unlock_irq(&hw->lock);
1394
1395
1396 csio_lnodes_unblock_request(hw);
1397 return count;
1398}
1399
1400
1401static ssize_t
1402csio_disable_port(struct device *dev,
1403 struct device_attribute *attr, const char *buf, size_t count)
1404{
1405 struct csio_lnode *ln = shost_priv(class_to_shost(dev));
1406 struct csio_hw *hw = csio_lnode_to_hw(ln);
1407 bool disable;
1408
1409 if (*buf == '1' || *buf == '0')
1410 disable = (*buf == '1') ? true : false;
1411 else
1412 return -EINVAL;
1413
1414
1415 csio_lnodes_block_by_port(hw, ln->portid);
1416
1417 spin_lock_irq(&hw->lock);
1418 csio_disable_lnodes(hw, ln->portid, disable);
1419 spin_unlock_irq(&hw->lock);
1420
1421
1422 csio_lnodes_unblock_by_port(hw, ln->portid);
1423 return count;
1424}
1425
1426
1427static ssize_t
1428csio_show_dbg_level(struct device *dev,
1429 struct device_attribute *attr, char *buf)
1430{
1431 struct csio_lnode *ln = shost_priv(class_to_shost(dev));
1432
1433 return snprintf(buf, PAGE_SIZE, "%x\n", ln->params.log_level);
1434}
1435
1436
1437static ssize_t
1438csio_store_dbg_level(struct device *dev,
1439 struct device_attribute *attr, const char *buf, size_t count)
1440{
1441 struct csio_lnode *ln = shost_priv(class_to_shost(dev));
1442 struct csio_hw *hw = csio_lnode_to_hw(ln);
1443 uint32_t dbg_level = 0;
1444
1445 if (!isdigit(buf[0]))
1446 return -EINVAL;
1447
1448 if (sscanf(buf, "%i", &dbg_level))
1449 return -EINVAL;
1450
1451 ln->params.log_level = dbg_level;
1452 hw->params.log_level = dbg_level;
1453
1454 return 0;
1455}
1456
1457static DEVICE_ATTR(hw_state, S_IRUGO, csio_show_hw_state, NULL);
1458static DEVICE_ATTR(device_reset, S_IWUSR, NULL, csio_device_reset);
1459static DEVICE_ATTR(disable_port, S_IWUSR, NULL, csio_disable_port);
1460static DEVICE_ATTR(dbg_level, S_IRUGO | S_IWUSR, csio_show_dbg_level,
1461 csio_store_dbg_level);
1462
1463static struct device_attribute *csio_fcoe_lport_attrs[] = {
1464 &dev_attr_hw_state,
1465 &dev_attr_device_reset,
1466 &dev_attr_disable_port,
1467 &dev_attr_dbg_level,
1468 NULL,
1469};
1470
1471static ssize_t
1472csio_show_num_reg_rnodes(struct device *dev,
1473 struct device_attribute *attr, char *buf)
1474{
1475 struct csio_lnode *ln = shost_priv(class_to_shost(dev));
1476
1477 return snprintf(buf, PAGE_SIZE, "%d\n", ln->num_reg_rnodes);
1478}
1479
1480static DEVICE_ATTR(num_reg_rnodes, S_IRUGO, csio_show_num_reg_rnodes, NULL);
1481
1482static struct device_attribute *csio_fcoe_vport_attrs[] = {
1483 &dev_attr_num_reg_rnodes,
1484 &dev_attr_dbg_level,
1485 NULL,
1486};
1487
1488static inline uint32_t
1489csio_scsi_copy_to_sgl(struct csio_hw *hw, struct csio_ioreq *req)
1490{
1491 struct scsi_cmnd *scmnd = (struct scsi_cmnd *)csio_scsi_cmnd(req);
1492 struct scatterlist *sg;
1493 uint32_t bytes_left;
1494 uint32_t bytes_copy;
1495 uint32_t buf_off = 0;
1496 uint32_t start_off = 0;
1497 uint32_t sg_off = 0;
1498 void *sg_addr;
1499 void *buf_addr;
1500 struct csio_dma_buf *dma_buf;
1501
1502 bytes_left = scsi_bufflen(scmnd);
1503 sg = scsi_sglist(scmnd);
1504 dma_buf = (struct csio_dma_buf *)csio_list_next(&req->gen_list);
1505
1506
1507 while (bytes_left > 0 && sg && dma_buf) {
1508 if (buf_off >= dma_buf->len) {
1509 buf_off = 0;
1510 dma_buf = (struct csio_dma_buf *)
1511 csio_list_next(dma_buf);
1512 continue;
1513 }
1514
1515 if (start_off >= sg->length) {
1516 start_off -= sg->length;
1517 sg = sg_next(sg);
1518 continue;
1519 }
1520
1521 buf_addr = dma_buf->vaddr + buf_off;
1522 sg_off = sg->offset + start_off;
1523 bytes_copy = min((dma_buf->len - buf_off),
1524 sg->length - start_off);
1525 bytes_copy = min((uint32_t)(PAGE_SIZE - (sg_off & ~PAGE_MASK)),
1526 bytes_copy);
1527
1528 sg_addr = kmap_atomic(sg_page(sg) + (sg_off >> PAGE_SHIFT));
1529 if (!sg_addr) {
1530 csio_err(hw, "failed to kmap sg:%p of ioreq:%p\n",
1531 sg, req);
1532 break;
1533 }
1534
1535 csio_dbg(hw, "copy_to_sgl:sg_addr %p sg_off %d buf %p len %d\n",
1536 sg_addr, sg_off, buf_addr, bytes_copy);
1537 memcpy(sg_addr + (sg_off & ~PAGE_MASK), buf_addr, bytes_copy);
1538 kunmap_atomic(sg_addr);
1539
1540 start_off += bytes_copy;
1541 buf_off += bytes_copy;
1542 bytes_left -= bytes_copy;
1543 }
1544
1545 if (bytes_left > 0)
1546 return DID_ERROR;
1547 else
1548 return DID_OK;
1549}
1550
1551
1552
1553
1554
1555
1556
1557static inline void
1558csio_scsi_err_handler(struct csio_hw *hw, struct csio_ioreq *req)
1559{
1560 struct scsi_cmnd *cmnd = (struct scsi_cmnd *)csio_scsi_cmnd(req);
1561 struct csio_scsim *scm = csio_hw_to_scsim(hw);
1562 struct fcp_resp_with_ext *fcp_resp;
1563 struct fcp_resp_rsp_info *rsp_info;
1564 struct csio_dma_buf *dma_buf;
1565 uint8_t flags, scsi_status = 0;
1566 uint32_t host_status = DID_OK;
1567 uint32_t rsp_len = 0, sns_len = 0;
1568 struct csio_rnode *rn = (struct csio_rnode *)(cmnd->device->hostdata);
1569
1570
1571 switch (req->wr_status) {
1572 case FW_HOSTERROR:
1573 if (unlikely(!csio_is_hw_ready(hw)))
1574 return;
1575
1576 host_status = DID_ERROR;
1577 CSIO_INC_STATS(scm, n_hosterror);
1578
1579 break;
1580 case FW_SCSI_RSP_ERR:
1581 dma_buf = &req->dma_buf;
1582 fcp_resp = (struct fcp_resp_with_ext *)dma_buf->vaddr;
1583 rsp_info = (struct fcp_resp_rsp_info *)(fcp_resp + 1);
1584 flags = fcp_resp->resp.fr_flags;
1585 scsi_status = fcp_resp->resp.fr_status;
1586
1587 if (flags & FCP_RSP_LEN_VAL) {
1588 rsp_len = be32_to_cpu(fcp_resp->ext.fr_rsp_len);
1589 if ((rsp_len != 0 && rsp_len != 4 && rsp_len != 8) ||
1590 (rsp_info->rsp_code != FCP_TMF_CMPL)) {
1591 host_status = DID_ERROR;
1592 goto out;
1593 }
1594 }
1595
1596 if ((flags & FCP_SNS_LEN_VAL) && fcp_resp->ext.fr_sns_len) {
1597 sns_len = be32_to_cpu(fcp_resp->ext.fr_sns_len);
1598 if (sns_len > SCSI_SENSE_BUFFERSIZE)
1599 sns_len = SCSI_SENSE_BUFFERSIZE;
1600
1601 memcpy(cmnd->sense_buffer,
1602 &rsp_info->_fr_resvd[0] + rsp_len, sns_len);
1603 CSIO_INC_STATS(scm, n_autosense);
1604 }
1605
1606 scsi_set_resid(cmnd, 0);
1607
1608
1609 if (flags & FCP_RESID_UNDER) {
1610 scsi_set_resid(cmnd,
1611 be32_to_cpu(fcp_resp->ext.fr_resid));
1612
1613 if (!(flags & FCP_SNS_LEN_VAL) &&
1614 (scsi_status == SAM_STAT_GOOD) &&
1615 ((scsi_bufflen(cmnd) - scsi_get_resid(cmnd))
1616 < cmnd->underflow))
1617 host_status = DID_ERROR;
1618 } else if (flags & FCP_RESID_OVER)
1619 host_status = DID_ERROR;
1620
1621 CSIO_INC_STATS(scm, n_rsperror);
1622 break;
1623
1624 case FW_SCSI_OVER_FLOW_ERR:
1625 csio_warn(hw,
1626 "Over-flow error,cmnd:0x%x expected len:0x%x"
1627 " resid:0x%x\n", cmnd->cmnd[0],
1628 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
1629 host_status = DID_ERROR;
1630 CSIO_INC_STATS(scm, n_ovflerror);
1631 break;
1632
1633 case FW_SCSI_UNDER_FLOW_ERR:
1634 csio_warn(hw,
1635 "Under-flow error,cmnd:0x%x expected"
1636 " len:0x%x resid:0x%x lun:0x%llx ssn:0x%x\n",
1637 cmnd->cmnd[0], scsi_bufflen(cmnd),
1638 scsi_get_resid(cmnd), cmnd->device->lun,
1639 rn->flowid);
1640 host_status = DID_ERROR;
1641 CSIO_INC_STATS(scm, n_unflerror);
1642 break;
1643
1644 case FW_SCSI_ABORT_REQUESTED:
1645 case FW_SCSI_ABORTED:
1646 case FW_SCSI_CLOSE_REQUESTED:
1647 csio_dbg(hw, "Req %p cmd:%p op:%x %s\n", req, cmnd,
1648 cmnd->cmnd[0],
1649 (req->wr_status == FW_SCSI_CLOSE_REQUESTED) ?
1650 "closed" : "aborted");
1651
1652
1653
1654
1655 host_status = DID_REQUEUE;
1656 if (req->wr_status == FW_SCSI_CLOSE_REQUESTED)
1657 CSIO_INC_STATS(scm, n_closed);
1658 else
1659 CSIO_INC_STATS(scm, n_aborted);
1660 break;
1661
1662 case FW_SCSI_ABORT_TIMEDOUT:
1663
1664 csio_dbg(hw, "FW timed out abort req:%p cmnd:%p status:%x\n",
1665 req, cmnd, req->wr_status);
1666 host_status = DID_ERROR;
1667 CSIO_INC_STATS(scm, n_abrt_timedout);
1668 break;
1669
1670 case FW_RDEV_NOT_READY:
1671
1672
1673
1674
1675
1676
1677 CSIO_INC_STATS(scm, n_rdev_nr_error);
1678 host_status = DID_ERROR;
1679 break;
1680
1681 case FW_ERR_RDEV_LOST:
1682 CSIO_INC_STATS(scm, n_rdev_lost_error);
1683 host_status = DID_ERROR;
1684 break;
1685
1686 case FW_ERR_RDEV_LOGO:
1687 CSIO_INC_STATS(scm, n_rdev_logo_error);
1688 host_status = DID_ERROR;
1689 break;
1690
1691 case FW_ERR_RDEV_IMPL_LOGO:
1692 host_status = DID_ERROR;
1693 break;
1694
1695 case FW_ERR_LINK_DOWN:
1696 CSIO_INC_STATS(scm, n_link_down_error);
1697 host_status = DID_ERROR;
1698 break;
1699
1700 case FW_FCOE_NO_XCHG:
1701 CSIO_INC_STATS(scm, n_no_xchg_error);
1702 host_status = DID_ERROR;
1703 break;
1704
1705 default:
1706 csio_err(hw, "Unknown SCSI FW WR status:%d req:%p cmnd:%p\n",
1707 req->wr_status, req, cmnd);
1708 CSIO_DB_ASSERT(0);
1709
1710 CSIO_INC_STATS(scm, n_unknown_error);
1711 host_status = DID_ERROR;
1712 break;
1713 }
1714
1715out:
1716 if (req->nsge > 0) {
1717 scsi_dma_unmap(cmnd);
1718 if (req->dcopy && (host_status == DID_OK))
1719 host_status = csio_scsi_copy_to_sgl(hw, req);
1720 }
1721
1722 cmnd->result = (((host_status) << 16) | scsi_status);
1723 cmnd->scsi_done(cmnd);
1724
1725
1726 csio_scsi_cmnd(req) = NULL;
1727 complete(&req->cmplobj);
1728}
1729
1730
1731
1732
1733
1734
1735
1736static void
1737csio_scsi_cbfn(struct csio_hw *hw, struct csio_ioreq *req)
1738{
1739 struct scsi_cmnd *cmnd = (struct scsi_cmnd *)csio_scsi_cmnd(req);
1740 uint8_t scsi_status = SAM_STAT_GOOD;
1741 uint32_t host_status = DID_OK;
1742
1743 if (likely(req->wr_status == FW_SUCCESS)) {
1744 if (req->nsge > 0) {
1745 scsi_dma_unmap(cmnd);
1746 if (req->dcopy)
1747 host_status = csio_scsi_copy_to_sgl(hw, req);
1748 }
1749
1750 cmnd->result = (((host_status) << 16) | scsi_status);
1751 cmnd->scsi_done(cmnd);
1752 csio_scsi_cmnd(req) = NULL;
1753 CSIO_INC_STATS(csio_hw_to_scsim(hw), n_tot_success);
1754 } else {
1755
1756 csio_scsi_err_handler(hw, req);
1757 }
1758}
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774static int
1775csio_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmnd)
1776{
1777 struct csio_lnode *ln = shost_priv(host);
1778 struct csio_hw *hw = csio_lnode_to_hw(ln);
1779 struct csio_scsim *scsim = csio_hw_to_scsim(hw);
1780 struct csio_rnode *rn = (struct csio_rnode *)(cmnd->device->hostdata);
1781 struct csio_ioreq *ioreq = NULL;
1782 unsigned long flags;
1783 int nsge = 0;
1784 int rv = SCSI_MLQUEUE_HOST_BUSY, nr;
1785 int retval;
1786 struct csio_scsi_qset *sqset;
1787 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1788
1789 sqset = &hw->sqset[ln->portid][blk_mq_rq_cpu(cmnd->request)];
1790
1791 nr = fc_remote_port_chkready(rport);
1792 if (nr) {
1793 cmnd->result = nr;
1794 CSIO_INC_STATS(scsim, n_rn_nr_error);
1795 goto err_done;
1796 }
1797
1798 if (unlikely(!csio_is_hw_ready(hw))) {
1799 cmnd->result = (DID_REQUEUE << 16);
1800 CSIO_INC_STATS(scsim, n_hw_nr_error);
1801 goto err_done;
1802 }
1803
1804
1805 nsge = scsi_dma_map(cmnd);
1806 if (unlikely(nsge < 0)) {
1807 CSIO_INC_STATS(scsim, n_dmamap_error);
1808 goto err;
1809 }
1810
1811
1812 if (unlikely(nsge > scsim->max_sge)) {
1813 csio_warn(hw,
1814 "More SGEs than can be supported."
1815 " SGEs: %d, Max SGEs: %d\n", nsge, scsim->max_sge);
1816 CSIO_INC_STATS(scsim, n_unsupp_sge_error);
1817 goto err_dma_unmap;
1818 }
1819
1820
1821 ioreq = csio_get_scsi_ioreq_lock(hw, scsim);
1822 if (!ioreq) {
1823 csio_err(hw, "Out of I/O request elements. Active #:%d\n",
1824 scsim->stats.n_active);
1825 CSIO_INC_STATS(scsim, n_no_req_error);
1826 goto err_dma_unmap;
1827 }
1828
1829 ioreq->nsge = nsge;
1830 ioreq->lnode = ln;
1831 ioreq->rnode = rn;
1832 ioreq->iq_idx = sqset->iq_idx;
1833 ioreq->eq_idx = sqset->eq_idx;
1834 ioreq->wr_status = 0;
1835 ioreq->drv_status = 0;
1836 csio_scsi_cmnd(ioreq) = (void *)cmnd;
1837 ioreq->tmo = 0;
1838 ioreq->datadir = cmnd->sc_data_direction;
1839
1840 if (cmnd->sc_data_direction == DMA_TO_DEVICE) {
1841 CSIO_INC_STATS(ln, n_output_requests);
1842 ln->stats.n_output_bytes += scsi_bufflen(cmnd);
1843 } else if (cmnd->sc_data_direction == DMA_FROM_DEVICE) {
1844 CSIO_INC_STATS(ln, n_input_requests);
1845 ln->stats.n_input_bytes += scsi_bufflen(cmnd);
1846 } else
1847 CSIO_INC_STATS(ln, n_control_requests);
1848
1849
1850 ioreq->io_cbfn = csio_scsi_cbfn;
1851
1852
1853 cmnd->host_scribble = (unsigned char *)ioreq;
1854 cmnd->SCp.Message = 0;
1855
1856
1857 spin_lock_irqsave(&hw->lock, flags);
1858 retval = csio_scsi_start_io(ioreq);
1859 spin_unlock_irqrestore(&hw->lock, flags);
1860
1861 if (retval != 0) {
1862 csio_err(hw, "ioreq: %p couldnt be started, status:%d\n",
1863 ioreq, retval);
1864 CSIO_INC_STATS(scsim, n_busy_error);
1865 goto err_put_req;
1866 }
1867
1868 return 0;
1869
1870err_put_req:
1871 csio_put_scsi_ioreq_lock(hw, scsim, ioreq);
1872err_dma_unmap:
1873 if (nsge > 0)
1874 scsi_dma_unmap(cmnd);
1875err:
1876 return rv;
1877
1878err_done:
1879 cmnd->scsi_done(cmnd);
1880 return 0;
1881}
1882
1883static int
1884csio_do_abrt_cls(struct csio_hw *hw, struct csio_ioreq *ioreq, bool abort)
1885{
1886 int rv;
1887 int cpu = smp_processor_id();
1888 struct csio_lnode *ln = ioreq->lnode;
1889 struct csio_scsi_qset *sqset = &hw->sqset[ln->portid][cpu];
1890
1891 ioreq->tmo = CSIO_SCSI_ABRT_TMO_MS;
1892
1893
1894
1895
1896
1897
1898 ioreq->eq_idx = sqset->eq_idx;
1899
1900 if (abort == SCSI_ABORT)
1901 rv = csio_scsi_abort(ioreq);
1902 else
1903 rv = csio_scsi_close(ioreq);
1904
1905 return rv;
1906}
1907
1908static int
1909csio_eh_abort_handler(struct scsi_cmnd *cmnd)
1910{
1911 struct csio_ioreq *ioreq;
1912 struct csio_lnode *ln = shost_priv(cmnd->device->host);
1913 struct csio_hw *hw = csio_lnode_to_hw(ln);
1914 struct csio_scsim *scsim = csio_hw_to_scsim(hw);
1915 int ready = 0, ret;
1916 unsigned long tmo = 0;
1917 int rv;
1918 struct csio_rnode *rn = (struct csio_rnode *)(cmnd->device->hostdata);
1919
1920 ret = fc_block_scsi_eh(cmnd);
1921 if (ret)
1922 return ret;
1923
1924 ioreq = (struct csio_ioreq *)cmnd->host_scribble;
1925 if (!ioreq)
1926 return SUCCESS;
1927
1928 if (!rn)
1929 return FAILED;
1930
1931 csio_dbg(hw,
1932 "Request to abort ioreq:%p cmd:%p cdb:%08llx"
1933 " ssni:0x%x lun:%llu iq:0x%x\n",
1934 ioreq, cmnd, *((uint64_t *)cmnd->cmnd), rn->flowid,
1935 cmnd->device->lun, csio_q_physiqid(hw, ioreq->iq_idx));
1936
1937 if (((struct scsi_cmnd *)csio_scsi_cmnd(ioreq)) != cmnd) {
1938 CSIO_INC_STATS(scsim, n_abrt_race_comp);
1939 return SUCCESS;
1940 }
1941
1942 ready = csio_is_lnode_ready(ln);
1943 tmo = CSIO_SCSI_ABRT_TMO_MS;
1944
1945 reinit_completion(&ioreq->cmplobj);
1946 spin_lock_irq(&hw->lock);
1947 rv = csio_do_abrt_cls(hw, ioreq, (ready ? SCSI_ABORT : SCSI_CLOSE));
1948 spin_unlock_irq(&hw->lock);
1949
1950 if (rv != 0) {
1951 if (rv == -EINVAL) {
1952
1953
1954
1955 return SUCCESS;
1956 }
1957 if (ready)
1958 CSIO_INC_STATS(scsim, n_abrt_busy_error);
1959 else
1960 CSIO_INC_STATS(scsim, n_cls_busy_error);
1961
1962 goto inval_scmnd;
1963 }
1964
1965 wait_for_completion_timeout(&ioreq->cmplobj, msecs_to_jiffies(tmo));
1966
1967
1968 if (((struct scsi_cmnd *)csio_scsi_cmnd(ioreq)) == cmnd) {
1969
1970 csio_err(hw, "Abort timed out -- req: %p\n", ioreq);
1971 CSIO_INC_STATS(scsim, n_abrt_timedout);
1972
1973inval_scmnd:
1974 if (ioreq->nsge > 0)
1975 scsi_dma_unmap(cmnd);
1976
1977 spin_lock_irq(&hw->lock);
1978 csio_scsi_cmnd(ioreq) = NULL;
1979 spin_unlock_irq(&hw->lock);
1980
1981 cmnd->result = (DID_ERROR << 16);
1982 cmnd->scsi_done(cmnd);
1983
1984 return FAILED;
1985 }
1986
1987
1988 if (host_byte(cmnd->result) == DID_REQUEUE) {
1989 csio_info(hw,
1990 "Aborted SCSI command to (%d:%llu) tag %u\n",
1991 cmnd->device->id, cmnd->device->lun,
1992 cmnd->request->tag);
1993 return SUCCESS;
1994 } else {
1995 csio_info(hw,
1996 "Failed to abort SCSI command, (%d:%llu) tag %u\n",
1997 cmnd->device->id, cmnd->device->lun,
1998 cmnd->request->tag);
1999 return FAILED;
2000 }
2001}
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012static void
2013csio_tm_cbfn(struct csio_hw *hw, struct csio_ioreq *req)
2014{
2015 struct scsi_cmnd *cmnd = (struct scsi_cmnd *)csio_scsi_cmnd(req);
2016 struct csio_dma_buf *dma_buf;
2017 uint8_t flags = 0;
2018 struct fcp_resp_with_ext *fcp_resp;
2019 struct fcp_resp_rsp_info *rsp_info;
2020
2021 csio_dbg(hw, "req: %p in csio_tm_cbfn status: %d\n",
2022 req, req->wr_status);
2023
2024
2025 cmnd->SCp.Status = req->wr_status;
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038 if (req->wr_status == FW_SCSI_RSP_ERR) {
2039 dma_buf = &req->dma_buf;
2040 fcp_resp = (struct fcp_resp_with_ext *)dma_buf->vaddr;
2041 rsp_info = (struct fcp_resp_rsp_info *)(fcp_resp + 1);
2042
2043 flags = fcp_resp->resp.fr_flags;
2044
2045
2046 if (flags & FCP_RSP_LEN_VAL)
2047 if (rsp_info->rsp_code == FCP_TMF_CMPL)
2048 cmnd->SCp.Status = FW_SUCCESS;
2049
2050 csio_dbg(hw, "TM FCP rsp code: %d\n", rsp_info->rsp_code);
2051 }
2052
2053
2054 csio_scsi_cmnd(req) = NULL;
2055}
2056
2057static int
2058csio_eh_lun_reset_handler(struct scsi_cmnd *cmnd)
2059{
2060 struct csio_lnode *ln = shost_priv(cmnd->device->host);
2061 struct csio_hw *hw = csio_lnode_to_hw(ln);
2062 struct csio_scsim *scsim = csio_hw_to_scsim(hw);
2063 struct csio_rnode *rn = (struct csio_rnode *)(cmnd->device->hostdata);
2064 struct csio_ioreq *ioreq = NULL;
2065 struct csio_scsi_qset *sqset;
2066 unsigned long flags;
2067 int retval;
2068 int count, ret;
2069 LIST_HEAD(local_q);
2070 struct csio_scsi_level_data sld;
2071
2072 if (!rn)
2073 goto fail;
2074
2075 csio_dbg(hw, "Request to reset LUN:%llu (ssni:0x%x tgtid:%d)\n",
2076 cmnd->device->lun, rn->flowid, rn->scsi_id);
2077
2078 if (!csio_is_lnode_ready(ln)) {
2079 csio_err(hw,
2080 "LUN reset cannot be issued on non-ready"
2081 " local node vnpi:0x%x (LUN:%llu)\n",
2082 ln->vnp_flowid, cmnd->device->lun);
2083 goto fail;
2084 }
2085
2086
2087 ret = fc_block_scsi_eh(cmnd);
2088 if (ret)
2089 return ret;
2090
2091
2092
2093
2094
2095
2096
2097
2098 if (fc_remote_port_chkready(rn->rport)) {
2099 csio_err(hw,
2100 "LUN reset cannot be issued on non-ready"
2101 " remote node ssni:0x%x (LUN:%llu)\n",
2102 rn->flowid, cmnd->device->lun);
2103 goto fail;
2104 }
2105
2106
2107 ioreq = csio_get_scsi_ioreq_lock(hw, scsim);
2108
2109 if (!ioreq) {
2110 csio_err(hw, "Out of IO request elements. Active # :%d\n",
2111 scsim->stats.n_active);
2112 goto fail;
2113 }
2114
2115 sqset = &hw->sqset[ln->portid][smp_processor_id()];
2116 ioreq->nsge = 0;
2117 ioreq->lnode = ln;
2118 ioreq->rnode = rn;
2119 ioreq->iq_idx = sqset->iq_idx;
2120 ioreq->eq_idx = sqset->eq_idx;
2121
2122 csio_scsi_cmnd(ioreq) = cmnd;
2123 cmnd->host_scribble = (unsigned char *)ioreq;
2124 cmnd->SCp.Status = 0;
2125
2126 cmnd->SCp.Message = FCP_TMF_LUN_RESET;
2127 ioreq->tmo = CSIO_SCSI_LUNRST_TMO_MS / 1000;
2128
2129
2130
2131
2132
2133
2134 count = DIV_ROUND_UP((ioreq->tmo + 10) * 1000, CSIO_SCSI_TM_POLL_MS);
2135
2136
2137 ioreq->io_cbfn = csio_tm_cbfn;
2138
2139
2140 sld.level = CSIO_LEV_LUN;
2141 sld.lnode = ioreq->lnode;
2142 sld.rnode = ioreq->rnode;
2143 sld.oslun = cmnd->device->lun;
2144
2145 spin_lock_irqsave(&hw->lock, flags);
2146
2147 retval = csio_scsi_start_tm(ioreq);
2148 spin_unlock_irqrestore(&hw->lock, flags);
2149
2150 if (retval != 0) {
2151 csio_err(hw, "Failed to issue LUN reset, req:%p, status:%d\n",
2152 ioreq, retval);
2153 goto fail_ret_ioreq;
2154 }
2155
2156 csio_dbg(hw, "Waiting max %d secs for LUN reset completion\n",
2157 count * (CSIO_SCSI_TM_POLL_MS / 1000));
2158
2159 while ((((struct scsi_cmnd *)csio_scsi_cmnd(ioreq)) == cmnd)
2160 && count--)
2161 msleep(CSIO_SCSI_TM_POLL_MS);
2162
2163
2164 if (((struct scsi_cmnd *)csio_scsi_cmnd(ioreq)) == cmnd) {
2165 csio_err(hw, "LUN reset (%d:%llu) timed out\n",
2166 cmnd->device->id, cmnd->device->lun);
2167
2168 spin_lock_irq(&hw->lock);
2169 csio_scsi_drvcleanup(ioreq);
2170 list_del_init(&ioreq->sm.sm_list);
2171 spin_unlock_irq(&hw->lock);
2172
2173 goto fail_ret_ioreq;
2174 }
2175
2176
2177 if (cmnd->SCp.Status != FW_SUCCESS) {
2178 csio_err(hw, "LUN reset failed (%d:%llu), status: %d\n",
2179 cmnd->device->id, cmnd->device->lun, cmnd->SCp.Status);
2180 goto fail;
2181 }
2182
2183
2184
2185
2186
2187
2188
2189 spin_lock_irq(&hw->lock);
2190 csio_scsi_gather_active_ios(scsim, &sld, &local_q);
2191
2192 retval = csio_scsi_abort_io_q(scsim, &local_q, 30000);
2193 spin_unlock_irq(&hw->lock);
2194
2195
2196 if (retval != 0) {
2197 csio_err(hw,
2198 "Attempt to abort I/Os during LUN reset of %llu"
2199 " returned %d\n", cmnd->device->lun, retval);
2200
2201 spin_lock_irq(&hw->lock);
2202 list_splice_tail_init(&local_q, &scsim->active_q);
2203 spin_unlock_irq(&hw->lock);
2204 goto fail;
2205 }
2206
2207 CSIO_INC_STATS(rn, n_lun_rst);
2208
2209 csio_info(hw, "LUN reset occurred (%d:%llu)\n",
2210 cmnd->device->id, cmnd->device->lun);
2211
2212 return SUCCESS;
2213
2214fail_ret_ioreq:
2215 csio_put_scsi_ioreq_lock(hw, scsim, ioreq);
2216fail:
2217 CSIO_INC_STATS(rn, n_lun_rst_fail);
2218 return FAILED;
2219}
2220
2221static int
2222csio_slave_alloc(struct scsi_device *sdev)
2223{
2224 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2225
2226 if (!rport || fc_remote_port_chkready(rport))
2227 return -ENXIO;
2228
2229 sdev->hostdata = *((struct csio_lnode **)(rport->dd_data));
2230
2231 return 0;
2232}
2233
2234static int
2235csio_slave_configure(struct scsi_device *sdev)
2236{
2237 scsi_change_queue_depth(sdev, csio_lun_qdepth);
2238 return 0;
2239}
2240
2241static void
2242csio_slave_destroy(struct scsi_device *sdev)
2243{
2244 sdev->hostdata = NULL;
2245}
2246
2247static int
2248csio_scan_finished(struct Scsi_Host *shost, unsigned long time)
2249{
2250 struct csio_lnode *ln = shost_priv(shost);
2251 int rv = 1;
2252
2253 spin_lock_irq(shost->host_lock);
2254 if (!ln->hwp || csio_list_deleted(&ln->sm.sm_list))
2255 goto out;
2256
2257 rv = csio_scan_done(ln, jiffies, time, csio_max_scan_tmo * HZ,
2258 csio_delta_scan_tmo * HZ);
2259out:
2260 spin_unlock_irq(shost->host_lock);
2261
2262 return rv;
2263}
2264
2265struct scsi_host_template csio_fcoe_shost_template = {
2266 .module = THIS_MODULE,
2267 .name = CSIO_DRV_DESC,
2268 .proc_name = KBUILD_MODNAME,
2269 .queuecommand = csio_queuecommand,
2270 .eh_timed_out = fc_eh_timed_out,
2271 .eh_abort_handler = csio_eh_abort_handler,
2272 .eh_device_reset_handler = csio_eh_lun_reset_handler,
2273 .slave_alloc = csio_slave_alloc,
2274 .slave_configure = csio_slave_configure,
2275 .slave_destroy = csio_slave_destroy,
2276 .scan_finished = csio_scan_finished,
2277 .this_id = -1,
2278 .sg_tablesize = CSIO_SCSI_MAX_SGE,
2279 .cmd_per_lun = CSIO_MAX_CMD_PER_LUN,
2280 .use_clustering = ENABLE_CLUSTERING,
2281 .shost_attrs = csio_fcoe_lport_attrs,
2282 .max_sectors = CSIO_MAX_SECTOR_SIZE,
2283};
2284
2285struct scsi_host_template csio_fcoe_shost_vport_template = {
2286 .module = THIS_MODULE,
2287 .name = CSIO_DRV_DESC,
2288 .proc_name = KBUILD_MODNAME,
2289 .queuecommand = csio_queuecommand,
2290 .eh_timed_out = fc_eh_timed_out,
2291 .eh_abort_handler = csio_eh_abort_handler,
2292 .eh_device_reset_handler = csio_eh_lun_reset_handler,
2293 .slave_alloc = csio_slave_alloc,
2294 .slave_configure = csio_slave_configure,
2295 .slave_destroy = csio_slave_destroy,
2296 .scan_finished = csio_scan_finished,
2297 .this_id = -1,
2298 .sg_tablesize = CSIO_SCSI_MAX_SGE,
2299 .cmd_per_lun = CSIO_MAX_CMD_PER_LUN,
2300 .use_clustering = ENABLE_CLUSTERING,
2301 .shost_attrs = csio_fcoe_vport_attrs,
2302 .max_sectors = CSIO_MAX_SECTOR_SIZE,
2303};
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316static int
2317csio_scsi_alloc_ddp_bufs(struct csio_scsim *scm, struct csio_hw *hw,
2318 int buf_size, int num_buf)
2319{
2320 int n = 0;
2321 struct list_head *tmp;
2322 struct csio_dma_buf *ddp_desc = NULL;
2323 uint32_t unit_size = 0;
2324
2325 if (!num_buf)
2326 return 0;
2327
2328 if (!buf_size)
2329 return -EINVAL;
2330
2331 INIT_LIST_HEAD(&scm->ddp_freelist);
2332
2333
2334 buf_size = (buf_size + PAGE_SIZE - 1) & PAGE_MASK;
2335
2336 for (n = 0; n < num_buf; n++) {
2337
2338 unit_size = buf_size;
2339 ddp_desc = kzalloc(sizeof(struct csio_dma_buf), GFP_KERNEL);
2340 if (!ddp_desc) {
2341 csio_err(hw,
2342 "Failed to allocate ddp descriptors,"
2343 " Num allocated = %d.\n",
2344 scm->stats.n_free_ddp);
2345 goto no_mem;
2346 }
2347
2348
2349 ddp_desc->vaddr = dma_alloc_coherent(&hw->pdev->dev, unit_size,
2350 &ddp_desc->paddr, GFP_KERNEL);
2351 if (!ddp_desc->vaddr) {
2352 csio_err(hw,
2353 "SCSI response DMA buffer (ddp) allocation"
2354 " failed!\n");
2355 kfree(ddp_desc);
2356 goto no_mem;
2357 }
2358
2359 ddp_desc->len = unit_size;
2360
2361
2362 list_add_tail(&ddp_desc->list, &scm->ddp_freelist);
2363 CSIO_INC_STATS(scm, n_free_ddp);
2364 }
2365
2366 return 0;
2367no_mem:
2368
2369 list_for_each(tmp, &scm->ddp_freelist) {
2370 ddp_desc = (struct csio_dma_buf *) tmp;
2371 tmp = csio_list_prev(tmp);
2372 dma_free_coherent(&hw->pdev->dev, ddp_desc->len,
2373 ddp_desc->vaddr, ddp_desc->paddr);
2374 list_del_init(&ddp_desc->list);
2375 kfree(ddp_desc);
2376 }
2377 scm->stats.n_free_ddp = 0;
2378
2379 return -ENOMEM;
2380}
2381
2382
2383
2384
2385
2386
2387
2388
2389static void
2390csio_scsi_free_ddp_bufs(struct csio_scsim *scm, struct csio_hw *hw)
2391{
2392 struct list_head *tmp;
2393 struct csio_dma_buf *ddp_desc;
2394
2395
2396 list_for_each(tmp, &scm->ddp_freelist) {
2397 ddp_desc = (struct csio_dma_buf *) tmp;
2398 tmp = csio_list_prev(tmp);
2399 dma_free_coherent(&hw->pdev->dev, ddp_desc->len,
2400 ddp_desc->vaddr, ddp_desc->paddr);
2401 list_del_init(&ddp_desc->list);
2402 kfree(ddp_desc);
2403 }
2404 scm->stats.n_free_ddp = 0;
2405}
2406
2407
2408
2409
2410
2411
2412
2413int
2414csio_scsim_init(struct csio_scsim *scm, struct csio_hw *hw)
2415{
2416 int i;
2417 struct csio_ioreq *ioreq;
2418 struct csio_dma_buf *dma_buf;
2419
2420 INIT_LIST_HEAD(&scm->active_q);
2421 scm->hw = hw;
2422
2423 scm->proto_cmd_len = sizeof(struct fcp_cmnd);
2424 scm->proto_rsp_len = CSIO_SCSI_RSP_LEN;
2425 scm->max_sge = CSIO_SCSI_MAX_SGE;
2426
2427 spin_lock_init(&scm->freelist_lock);
2428
2429
2430 INIT_LIST_HEAD(&scm->ioreq_freelist);
2431 for (i = 0; i < csio_scsi_ioreqs; i++) {
2432
2433 ioreq = kzalloc(sizeof(struct csio_ioreq), GFP_KERNEL);
2434 if (!ioreq) {
2435 csio_err(hw,
2436 "I/O request element allocation failed, "
2437 " Num allocated = %d.\n",
2438 scm->stats.n_free_ioreq);
2439
2440 goto free_ioreq;
2441 }
2442
2443
2444 dma_buf = &ioreq->dma_buf;
2445 dma_buf->vaddr = dma_pool_alloc(hw->scsi_dma_pool, GFP_KERNEL,
2446 &dma_buf->paddr);
2447 if (!dma_buf->vaddr) {
2448 csio_err(hw,
2449 "SCSI response DMA buffer allocation"
2450 " failed!\n");
2451 kfree(ioreq);
2452 goto free_ioreq;
2453 }
2454
2455 dma_buf->len = scm->proto_rsp_len;
2456
2457
2458 csio_init_state(&ioreq->sm, csio_scsis_uninit);
2459 INIT_LIST_HEAD(&ioreq->gen_list);
2460 init_completion(&ioreq->cmplobj);
2461
2462 list_add_tail(&ioreq->sm.sm_list, &scm->ioreq_freelist);
2463 CSIO_INC_STATS(scm, n_free_ioreq);
2464 }
2465
2466 if (csio_scsi_alloc_ddp_bufs(scm, hw, PAGE_SIZE, csio_ddp_descs))
2467 goto free_ioreq;
2468
2469 return 0;
2470
2471free_ioreq:
2472
2473
2474
2475
2476 while (!list_empty(&scm->ioreq_freelist)) {
2477 struct csio_sm *tmp;
2478
2479 tmp = list_first_entry(&scm->ioreq_freelist,
2480 struct csio_sm, sm_list);
2481 list_del_init(&tmp->sm_list);
2482 ioreq = (struct csio_ioreq *)tmp;
2483
2484 dma_buf = &ioreq->dma_buf;
2485 dma_pool_free(hw->scsi_dma_pool, dma_buf->vaddr,
2486 dma_buf->paddr);
2487
2488 kfree(ioreq);
2489 }
2490
2491 scm->stats.n_free_ioreq = 0;
2492
2493 return -ENOMEM;
2494}
2495
2496
2497
2498
2499
2500
2501void
2502csio_scsim_exit(struct csio_scsim *scm)
2503{
2504 struct csio_ioreq *ioreq;
2505 struct csio_dma_buf *dma_buf;
2506
2507 while (!list_empty(&scm->ioreq_freelist)) {
2508 struct csio_sm *tmp;
2509
2510 tmp = list_first_entry(&scm->ioreq_freelist,
2511 struct csio_sm, sm_list);
2512 list_del_init(&tmp->sm_list);
2513 ioreq = (struct csio_ioreq *)tmp;
2514
2515 dma_buf = &ioreq->dma_buf;
2516 dma_pool_free(scm->hw->scsi_dma_pool, dma_buf->vaddr,
2517 dma_buf->paddr);
2518
2519 kfree(ioreq);
2520 }
2521
2522 scm->stats.n_free_ioreq = 0;
2523
2524 csio_scsi_free_ddp_bufs(scm, scm->hw);
2525}
2526