2 * This file is part of the Chelsio FCoE driver for Linux.
4 * Copyright (c) 2008-2012 Chelsio Communications, Inc. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
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>
46 #include <scsi/scsi.h>
47 #include <scsi/scsi_device.h>
48 #include <scsi/scsi_transport_fc.h>
51 #include "csio_lnode.h"
52 #include "csio_rnode.h"
53 #include "csio_scsi.h"
54 #include "csio_init.h"
56 int csio_scsi_eqsize = 65536;
57 int csio_scsi_iqlen = 128;
58 int csio_scsi_ioreqs = 2048;
59 uint32_t csio_max_scan_tmo;
60 uint32_t csio_delta_scan_tmo = 5;
61 int csio_lun_qdepth = 32;
63 static int csio_ddp_descs = 128;
65 static int csio_do_abrt_cls(struct csio_hw *,
66 struct csio_ioreq *, bool);
68 static void csio_scsis_uninit(struct csio_ioreq *, enum csio_scsi_ev);
69 static void csio_scsis_io_active(struct csio_ioreq *, enum csio_scsi_ev);
70 static void csio_scsis_tm_active(struct csio_ioreq *, enum csio_scsi_ev);
71 static void csio_scsis_aborting(struct csio_ioreq *, enum csio_scsi_ev);
72 static void csio_scsis_closing(struct csio_ioreq *, enum csio_scsi_ev);
73 static void csio_scsis_shost_cmpl_await(struct csio_ioreq *, enum csio_scsi_ev);
76 * csio_scsi_match_io - Match an ioreq with the given SCSI level data.
77 * @ioreq: The I/O request
78 * @sld: Level information
80 * Should be called with lock held.
84 csio_scsi_match_io(struct csio_ioreq *ioreq, struct csio_scsi_level_data *sld)
86 struct scsi_cmnd *scmnd = csio_scsi_cmnd(ioreq);
93 return ((ioreq->lnode == sld->lnode) &&
94 (ioreq->rnode == sld->rnode) &&
95 ((uint64_t)scmnd->device->lun == sld->oslun));
98 return ((ioreq->lnode == sld->lnode) &&
99 (ioreq->rnode == sld->rnode));
101 return (ioreq->lnode == sld->lnode);
110 * csio_scsi_gather_active_ios - Gather active I/Os based on level
112 * @sld: Level information
113 * @dest: The queue where these I/Os have to be gathered.
115 * Should be called with lock held.
118 csio_scsi_gather_active_ios(struct csio_scsim *scm,
119 struct csio_scsi_level_data *sld,
120 struct list_head *dest)
122 struct list_head *tmp, *next;
124 if (list_empty(&scm->active_q))
127 /* Just splice the entire active_q into dest */
128 if (sld->level == CSIO_LEV_ALL) {
129 list_splice_tail_init(&scm->active_q, dest);
133 list_for_each_safe(tmp, next, &scm->active_q) {
134 if (csio_scsi_match_io((struct csio_ioreq *)tmp, sld)) {
136 list_add_tail(tmp, dest);
142 csio_scsi_itnexus_loss_error(uint16_t 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:
156 * csio_scsi_fcp_cmnd - Frame the SCSI FCP command paylod.
157 * @req: IO req structure.
158 * @addr: DMA location to place the payload.
160 * This routine is shared between FCP_WRITE, FCP_READ and FCP_CMD requests.
163 csio_scsi_fcp_cmnd(struct csio_ioreq *req, void *addr)
165 struct fcp_cmnd *fcp_cmnd = (struct fcp_cmnd *)addr;
166 struct scsi_cmnd *scmnd = csio_scsi_cmnd(req);
168 /* Check for Task Management */
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;
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));
179 if (req->datadir == DMA_TO_DEVICE)
180 fcp_cmnd->fc_flags = FCP_CFL_WRDATA;
182 fcp_cmnd->fc_flags = FCP_CFL_RDDATA;
184 fcp_cmnd->fc_flags = 0;
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;
193 * csio_scsi_init_cmd_wr - Initialize the SCSI CMD WR.
194 * @req: IO req structure.
195 * @addr: DMA location to place the payload.
196 * @size: Size of WR (including FW WR + immed data + rsp SG entry
198 * Wrapper for populating fw_scsi_cmd_wr.
201 csio_scsi_init_cmd_wr(struct csio_ioreq *req, void *addr, uint32_t size)
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;
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) |
213 DIV_ROUND_UP(size, 16)));
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;
219 memset(&wr->r5, 0, 8);
221 /* Get RSP DMA buffer */
222 dma_buf = &req->dma_buf;
224 /* Prepare RSP SGL */
225 wr->rsp_dmalen = cpu_to_be32(dma_buf->len);
226 wr->rsp_dmaaddr = cpu_to_be64(dma_buf->paddr);
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;
235 /* Frame a FCP command */
236 csio_scsi_fcp_cmnd(req, (void *)((uintptr_t)addr +
237 sizeof(struct fw_scsi_cmd_wr)));
240 #define CSIO_SCSI_CMD_WR_SZ(_imm) \
241 (sizeof(struct fw_scsi_cmd_wr) + /* WR size */ \
242 ALIGN((_imm), 16)) /* Immed data */
244 #define CSIO_SCSI_CMD_WR_SZ_16(_imm) \
245 (ALIGN(CSIO_SCSI_CMD_WR_SZ((_imm)), 16))
248 * csio_scsi_cmd - Create a SCSI CMD WR.
249 * @req: IO req structure.
251 * Gets a WR slot in the ingress queue and initializes it with SCSI CMD WR.
255 csio_scsi_cmd(struct csio_ioreq *req)
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);
262 req->drv_status = csio_wr_get(hw, req->eq_idx, size, &wrp);
263 if (unlikely(req->drv_status != 0))
266 if (wrp.size1 >= size) {
267 /* Initialize WR in one shot */
268 csio_scsi_init_cmd_wr(req, wrp.addr1, size);
270 uint8_t *tmpwr = csio_q_eq_wrap(hw, req->eq_idx);
273 * Make a temporary copy of the WR and write back
274 * the copy into the WR pair.
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);
283 * csio_scsi_init_ulptx_dsgl - Fill in a ULP_TX_SC_DSGL
286 * @sgl: ULP TX SGL pointer.
290 csio_scsi_init_ultptx_dsgl(struct csio_hw *hw, struct csio_ioreq *req,
291 struct ulptx_sgl *sgl)
293 struct ulptx_sge_pair *sge_pair = NULL;
294 struct scatterlist *sgel;
297 struct list_head *tmp;
298 struct csio_dma_buf *dma_buf;
299 struct scsi_cmnd *scmnd = csio_scsi_cmnd(req);
301 sgl->cmd_nsge = htonl(ULPTX_CMD_V(ULP_TX_SC_DSGL) | ULPTX_MORE_F |
302 ULPTX_NSGE_V(req->nsge));
303 /* Now add the data SGLs */
304 if (likely(!req->dcopy)) {
305 scsi_for_each_sg(scmnd, sgel, req->nsge, i) {
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);
313 sge_pair->addr[1] = cpu_to_be64(
314 sg_dma_address(sgel));
315 sge_pair->len[1] = cpu_to_be32(
319 sge_pair->addr[0] = cpu_to_be64(
320 sg_dma_address(sgel));
321 sge_pair->len[0] = cpu_to_be32(
326 /* Program sg elements with driver's DDP buffer */
327 xfer_len = scsi_bufflen(scmnd);
328 list_for_each(tmp, &req->gen_list) {
329 dma_buf = (struct csio_dma_buf *)tmp;
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));
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));
345 xfer_len -= min(xfer_len, dma_buf->len);
352 * csio_scsi_init_read_wr - Initialize the READ SCSI WR.
353 * @req: IO req structure.
354 * @wrp: DMA location to place the payload.
355 * @size: Size of WR (including FW WR + immed data + rsp SG entry + data SGL
357 * Wrapper for populating fw_scsi_read_wr.
360 csio_scsi_init_read_wr(struct csio_ioreq *req, void *wrp, uint32_t size)
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);
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 /* Get RSP DMA buffer */
381 dma_buf = &req->dma_buf;
383 /* Prepare RSP SGL */
384 wr->rsp_dmalen = cpu_to_be32(dma_buf->len);
385 wr->rsp_dmaaddr = cpu_to_be64(dma_buf->paddr);
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)));
396 /* Move WR pointer past command and immediate data */
397 sgl = (struct ulptx_sgl *)((uintptr_t)wrp +
398 sizeof(struct fw_scsi_read_wr) + ALIGN(imm, 16));
400 /* Fill in the DSGL */
401 csio_scsi_init_ultptx_dsgl(hw, req, sgl);
405 * csio_scsi_init_write_wr - Initialize the WRITE SCSI WR.
406 * @req: IO req structure.
407 * @wrp: DMA location to place the payload.
408 * @size: Size of WR (including FW WR + immed data + rsp SG entry + data SGL
410 * Wrapper for populating fw_scsi_write_wr.
413 csio_scsi_init_write_wr(struct csio_ioreq *req, void *wrp, uint32_t size)
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);
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 /* Get RSP DMA buffer */
434 dma_buf = &req->dma_buf;
436 /* Prepare RSP SGL */
437 wr->rsp_dmalen = cpu_to_be32(dma_buf->len);
438 wr->rsp_dmaaddr = cpu_to_be64(dma_buf->paddr);
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)));
449 /* Move WR pointer past command and immediate data */
450 sgl = (struct ulptx_sgl *)((uintptr_t)wrp +
451 sizeof(struct fw_scsi_write_wr) + ALIGN(imm, 16));
453 /* Fill in the DSGL */
454 csio_scsi_init_ultptx_dsgl(hw, req, sgl);
457 /* Calculate WR size needed for fw_scsi_read_wr/fw_scsi_write_wr */
458 #define CSIO_SCSI_DATA_WRSZ(req, oper, sz, imm) \
460 (sz) = sizeof(struct fw_scsi_##oper##_wr) + /* WR size */ \
461 ALIGN((imm), 16) + /* Immed data */ \
462 sizeof(struct ulptx_sgl); /* ulptx_sgl */ \
464 if (unlikely((req)->nsge > 1)) \
465 (sz) += (sizeof(struct ulptx_sge_pair) * \
466 (ALIGN(((req)->nsge - 1), 2) / 2)); \
471 * csio_scsi_read - Create a SCSI READ WR.
472 * @req: IO req structure.
474 * Gets a WR slot in the ingress queue and initializes it with
479 csio_scsi_read(struct csio_ioreq *req)
481 struct csio_wr_pair wrp;
483 struct csio_hw *hw = req->lnode->hwp;
484 struct csio_scsim *scsim = csio_hw_to_scsim(hw);
486 CSIO_SCSI_DATA_WRSZ(req, read, size, scsim->proto_cmd_len);
487 size = ALIGN(size, 16);
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 /* Initialize WR in one shot */
493 csio_scsi_init_read_wr(req, wrp.addr1, size);
495 uint8_t *tmpwr = csio_q_eq_wrap(hw, req->eq_idx);
497 * Make a temporary copy of the WR and write back
498 * the copy into the WR pair.
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);
508 * csio_scsi_write - Create a SCSI WRITE WR.
509 * @req: IO req structure.
511 * Gets a WR slot in the ingress queue and initializes it with
516 csio_scsi_write(struct csio_ioreq *req)
518 struct csio_wr_pair wrp;
520 struct csio_hw *hw = req->lnode->hwp;
521 struct csio_scsim *scsim = csio_hw_to_scsim(hw);
523 CSIO_SCSI_DATA_WRSZ(req, write, size, scsim->proto_cmd_len);
524 size = ALIGN(size, 16);
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 /* Initialize WR in one shot */
530 csio_scsi_init_write_wr(req, wrp.addr1, size);
532 uint8_t *tmpwr = csio_q_eq_wrap(hw, req->eq_idx);
534 * Make a temporary copy of the WR and write back
535 * the copy into the WR pair.
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);
545 * csio_setup_ddp - Setup DDP buffers for Read request.
546 * @req: IO req structure.
548 * Checks SGLs/Data buffers are virtually contiguous required for DDP.
549 * If contiguous,driver posts SGLs in the WR otherwise post internal
550 * buffers for such request for DDP.
553 csio_setup_ddp(struct csio_scsim *scsim, struct csio_ioreq *req)
555 #ifdef __CSIO_DEBUG__
556 struct csio_hw *hw = req->lnode->hwp;
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;
563 struct csio_dma_buf *dma_buf = NULL;
564 uint32_t alloc_len = 0;
565 uint32_t xfer_len = 0;
569 scsi_for_each_sg(scmnd, sgel, req->nsge, i) {
570 sg_addr = sg_dma_address(sgel);
571 sg_len = sg_dma_len(sgel);
573 buf_off = sg_addr & (ddp_pagesz - 1);
575 /* Except 1st buffer,all buffer addr have to be Page aligned */
576 if (i != 0 && buf_off) {
577 csio_dbg(hw, "SGL addr not DDP aligned (%llx:%d)\n",
582 /* Except last buffer,all buffer must end on page boundary */
583 if ((i != (req->nsge - 1)) &&
584 ((buf_off + sg_len) & (ddp_pagesz - 1))) {
586 "SGL addr not ending on page boundary"
587 "(%llx:%d)\n", sg_addr, sg_len);
592 /* SGL's are virtually contiguous. HW will DDP to SGLs */
599 CSIO_INC_STATS(scsim, n_unaligned);
601 * For unaligned SGLs, driver will allocate internal DDP buffer.
602 * Once command is completed data from DDP buffer copied to SGLs
606 /* Use gen_list to store the DDP buffers */
607 INIT_LIST_HEAD(&req->gen_list);
608 xfer_len = scsi_bufflen(scmnd);
611 /* Allocate ddp buffers for this request */
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;
618 alloc_len += dma_buf->len;
619 /* Added to IO req */
620 list_add_tail(&dma_buf->list, &req->gen_list);
624 if (!req->drv_status) {
625 /* set number of ddp bufs used */
631 /* release dma descs */
633 csio_put_scsi_ddp_list(scsim, &req->gen_list, i);
637 * csio_scsi_init_abrt_cls_wr - Initialize an ABORT/CLOSE WR.
638 * @req: IO req structure.
639 * @addr: DMA location to place the payload.
641 * @abort: abort OR close
643 * Wrapper for populating fw_scsi_cmd_wr.
646 csio_scsi_init_abrt_cls_wr(struct csio_ioreq *req, void *addr, uint32_t size,
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;
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) |
656 DIV_ROUND_UP(size, 16)));
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 /* 0 for CHK_ALL_IO tells FW to look up t_cookie */
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));
669 /* Since we re-use the same ioreq for abort as well */
670 wr->t_cookie = (uintptr_t) req;
674 csio_scsi_abrt_cls(struct csio_ioreq *req, bool abort)
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);
680 req->drv_status = csio_wr_get(hw, req->eq_idx, size, &wrp);
681 if (req->drv_status != 0)
684 if (wrp.size1 >= size) {
685 /* Initialize WR in one shot */
686 csio_scsi_init_abrt_cls_wr(req, wrp.addr1, size, abort);
688 uint8_t *tmpwr = csio_q_eq_wrap(hw, req->eq_idx);
690 * Make a temporary copy of the WR and write back
691 * the copy into the WR pair.
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);
699 /*****************************************************************************/
701 /*****************************************************************************/
703 csio_scsis_uninit(struct csio_ioreq *req, enum csio_scsi_ev evt)
705 struct csio_hw *hw = req->lnode->hwp;
706 struct csio_scsim *scsim = csio_hw_to_scsim(hw);
709 case CSIO_SCSIE_START_IO:
712 if (req->datadir == DMA_TO_DEVICE) {
714 csio_scsi_write(req);
716 csio_setup_ddp(scsim, req);
721 if (likely(req->drv_status == 0)) {
722 /* change state and enqueue on active_q */
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);
732 case CSIO_SCSIE_START_TM:
734 if (req->drv_status == 0) {
736 * NOTE: We collect the affected I/Os prior to issuing
737 * LUN reset, and not after it. This is to prevent
738 * aborting I/Os that get issued after the LUN reset,
739 * but prior to LUN reset completion (in the event that
740 * the host stack has not blocked I/Os to a LUN that is
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);
750 case CSIO_SCSIE_ABORT:
751 case CSIO_SCSIE_CLOSE:
754 * We could get here due to :
755 * - a window in the cleanup path of the SCSI module
756 * (csio_scsi_abort_io()). Please see NOTE in this function.
757 * - a window in the time we tried to issue an abort/close
758 * of a request to FW, and the FW completed the request
760 * Print a message for now, and return INVAL either way.
762 req->drv_status = -EINVAL;
763 csio_warn(hw, "Trying to abort/close completed IO:%p!\n", req);
767 csio_dbg(hw, "Unhandled event:%d sent to req:%p\n", evt, req);
773 csio_scsis_io_active(struct csio_ioreq *req, enum csio_scsi_ev evt)
775 struct csio_hw *hw = req->lnode->hwp;
776 struct csio_scsim *scm = csio_hw_to_scsim(hw);
777 struct csio_rnode *rn;
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);
785 * In MSIX mode, with multiple queues, the SCSI compeltions
786 * could reach us sooner than the FW events sent to indicate
787 * I-T nexus loss (link down, remote device logo etc). We
788 * dont want to be returning such I/Os to the upper layer
789 * immediately, since we wouldnt have reported the I-T nexus
790 * loss itself. This forces us to serialize such completions
791 * with the reporting of the I-T nexus loss. Therefore, we
792 * internally queue up such up such completions in the rnode.
793 * The reporting of I-T nexus loss to the upper layer is then
794 * followed by the returning of I/Os in this internal queue.
795 * Having another state alongwith another queue helps us take
796 * actions for events such as ABORT received while we are
797 * in this rnode queue.
799 if (unlikely(req->wr_status != FW_SUCCESS)) {
802 * FW says remote device is lost, but rnode
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,
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);
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);
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);
839 csio_dbg(hw, "Unhandled event:%d sent to req:%p\n", evt, req);
845 csio_scsis_tm_active(struct csio_ioreq *req, enum csio_scsi_ev evt)
847 struct csio_hw *hw = req->lnode->hwp;
848 struct csio_scsim *scm = csio_hw_to_scsim(hw);
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);
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);
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);
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);
882 csio_dbg(hw, "Unhandled event:%d sent to req:%p\n", evt, req);
888 csio_scsis_aborting(struct csio_ioreq *req, enum csio_scsi_ev evt)
890 struct csio_hw *hw = req->lnode->hwp;
891 struct csio_scsim *scm = csio_hw_to_scsim(hw);
894 case CSIO_SCSIE_COMPLETED:
896 "ioreq %p recvd cmpltd (wr_status:%d) "
897 "in aborting st\n", req, req->wr_status);
899 * Use -ECANCELED to explicitly tell the ABORTED event that
900 * the original I/O was returned to driver by FW.
901 * We dont really care if the I/O was returned with success by
902 * FW (because the ABORT and completion of the I/O crossed each
903 * other), or any other return value. Once we are in aborting
904 * state, the success or failure of the I/O is unimportant to
907 req->drv_status = -ECANCELED;
910 case CSIO_SCSIE_ABORT:
911 CSIO_INC_STATS(scm, n_abrt_dups);
914 case CSIO_SCSIE_ABORTED:
916 csio_dbg(hw, "abort of %p return status:0x%x drv_status:%x\n",
917 req, req->wr_status, req->drv_status);
919 * Check if original I/O WR completed before the Abort
922 if (req->drv_status != -ECANCELED) {
924 "Abort completed before original I/O,"
930 * There are the following possible scenarios:
931 * 1. The abort completed successfully, FW returned FW_SUCCESS.
932 * 2. The completion of an I/O and the receipt of
933 * abort for that I/O by the FW crossed each other.
934 * The FW returned FW_EINVAL. The original I/O would have
935 * returned with FW_SUCCESS or any other SCSI error.
936 * 3. The FW couldnt sent the abort out on the wire, as there
937 * was an I-T nexus loss (link down, remote device logged
938 * out etc). FW sent back an appropriate IT nexus loss status
940 * 4. FW sent an abort, but abort timed out (remote device
941 * didnt respond). FW replied back with
942 * FW_SCSI_ABORT_TIMEDOUT.
943 * 5. FW couldnt genuinely abort the request for some reason,
944 * and sent us an error.
946 * The first 3 scenarios are treated as succesful abort
947 * operations by the host, while the last 2 are failed attempts
948 * to abort. Manipulate the return value of the request
949 * appropriately, so that host can convey these results
950 * back to the upper layer.
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;
957 CSIO_DEC_STATS(scm, n_active);
958 list_del_init(&req->sm.sm_list);
959 csio_set_state(&req->sm, csio_scsis_uninit);
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);
968 case CSIO_SCSIE_CLOSE:
970 * We can receive this event from the module
971 * cleanup paths, if the FW forgot to reply to the ABORT WR
972 * and left this ioreq in this state. For now, just ignore
973 * the event. The CLOSE event is sent to this state, as
974 * the LINK may have already gone down.
979 csio_dbg(hw, "Unhandled event:%d sent to req:%p\n", evt, req);
985 csio_scsis_closing(struct csio_ioreq *req, enum csio_scsi_ev evt)
987 struct csio_hw *hw = req->lnode->hwp;
988 struct csio_scsim *scm = csio_hw_to_scsim(hw);
991 case CSIO_SCSIE_COMPLETED:
993 "ioreq %p recvd cmpltd (wr_status:%d) "
994 "in closing st\n", req, req->wr_status);
996 * Use -ECANCELED to explicitly tell the CLOSED event that
997 * the original I/O was returned to driver by FW.
998 * We dont really care if the I/O was returned with success by
999 * FW (because the CLOSE and completion of the I/O crossed each
1000 * other), or any other return value. Once we are in aborting
1001 * state, the success or failure of the I/O is unimportant to
1004 req->drv_status = -ECANCELED;
1007 case CSIO_SCSIE_CLOSED:
1009 * Check if original I/O WR completed before the Close
1012 if (req->drv_status != -ECANCELED) {
1014 "Close completed before original I/O,"
1020 * Either close succeeded, or we issued close to FW at the
1021 * same time FW compelted it to us. Either way, the I/O
1024 CSIO_DB_ASSERT((req->wr_status == FW_SUCCESS) ||
1025 (req->wr_status == FW_EINVAL));
1026 req->wr_status = FW_SCSI_CLOSE_REQUESTED;
1028 CSIO_DEC_STATS(scm, n_active);
1029 list_del_init(&req->sm.sm_list);
1030 csio_set_state(&req->sm, csio_scsis_uninit);
1033 case CSIO_SCSIE_CLOSE:
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);
1043 csio_dbg(hw, "Unhandled event:%d sent to req:%p\n", evt, req);
1049 csio_scsis_shost_cmpl_await(struct csio_ioreq *req, enum csio_scsi_ev evt)
1052 case CSIO_SCSIE_ABORT:
1053 case CSIO_SCSIE_CLOSE:
1055 * Just succeed the abort request, and hope that
1056 * the remote device unregister path will cleanup
1057 * this I/O to the upper layer within a sane
1061 * A close can come in during a LINK DOWN. The FW would have
1062 * returned us the I/O back, but not the remote device lost
1063 * FW event. In this interval, if the I/O times out at the upper
1064 * layer, a close can come in. Take the same action as abort:
1065 * return success, and hope that the remote device unregister
1066 * path will cleanup this I/O. If the FW still doesnt send
1067 * the msg, the close times out, and the upper layer resorts
1068 * to the next level of error recovery.
1070 req->drv_status = 0;
1072 case CSIO_SCSIE_DRVCLEANUP:
1073 csio_set_state(&req->sm, csio_scsis_uninit);
1076 csio_dbg(req->lnode->hwp, "Unhandled event:%d sent to req:%p\n",
1083 * csio_scsi_cmpl_handler - WR completion handler for SCSI.
1085 * @wr: The completed WR from the ingress queue.
1086 * @len: Length of the WR.
1087 * @flb: Freelist buffer array.
1088 * @priv: Private object
1089 * @scsiwr: Pointer to SCSI WR.
1091 * This is the WR completion handler called per completion from the
1092 * ISR. It is called with lock held. It walks past the RSS and CPL message
1093 * header where the actual WR is present.
1094 * It then gets the status, WR handle (ioreq pointer) and the len of
1095 * the WR, based on WR opcode. Only on a non-good status is the entire
1096 * WR copied into the WR cache (ioreq->fw_wr).
1097 * The ioreq corresponding to the WR is returned to the caller.
1098 * NOTE: The SCSI queue doesnt allocate a freelist today, hence
1099 * no freelist buffer is expected.
1102 csio_scsi_cmpl_handler(struct csio_hw *hw, void *wr, uint32_t len,
1103 struct csio_fl_dma_buf *flb, void *priv, uint8_t **scsiwr)
1105 struct csio_ioreq *ioreq = NULL;
1106 struct cpl_fw6_msg *cpl;
1109 struct csio_scsim *scm = csio_hw_to_scsim(hw);
1111 /* skip RSS header */
1112 cpl = (struct cpl_fw6_msg *)((uintptr_t)wr + sizeof(__be64));
1114 if (unlikely(cpl->opcode != CPL_FW6_MSG)) {
1115 csio_warn(hw, "Error: Invalid CPL msg %x recvd on SCSI q\n",
1117 CSIO_INC_STATS(scm, n_inval_cplop);
1121 tempwr = (uint8_t *)(cpl->data);
1122 status = csio_wr_status(tempwr);
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));
1132 ioreq->wr_status = status;
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));
1142 ioreq->wr_status = status;
1146 csio_warn(hw, "WR with invalid opcode in SCSI IQ: %x\n", *tempwr);
1147 CSIO_INC_STATS(scm, n_inval_scsiop);
1152 * csio_scsi_cleanup_io_q - Cleanup the given queue.
1153 * @scm: SCSI module.
1154 * @q: Queue to be cleaned up.
1156 * Called with lock held. Has to exit with lock held.
1159 csio_scsi_cleanup_io_q(struct csio_scsim *scm, struct list_head *q)
1161 struct csio_hw *hw = scm->hw;
1162 struct csio_ioreq *ioreq;
1163 struct list_head *tmp, *next;
1164 struct scsi_cmnd *scmnd;
1166 /* Call back the completion routines of the active_q */
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);
1175 * Upper layers may have cleared this command, hence this
1176 * check to avoid accessing stale references.
1179 ioreq->io_cbfn(hw, ioreq);
1181 spin_lock_irq(&scm->freelist_lock);
1182 csio_put_scsi_ioreq(scm, ioreq);
1183 spin_unlock_irq(&scm->freelist_lock);
1185 spin_lock_irq(&hw->lock);
1189 #define CSIO_SCSI_ABORT_Q_POLL_MS 2000
1192 csio_abrt_cls(struct csio_ioreq *ioreq, struct scsi_cmnd *scmnd)
1194 struct csio_lnode *ln = ioreq->lnode;
1195 struct csio_hw *hw = ln->hwp;
1197 struct csio_scsim *scsim = csio_hw_to_scsim(hw);
1200 if (csio_scsi_cmnd(ioreq) != scmnd) {
1201 CSIO_INC_STATS(scsim, n_abrt_race_comp);
1205 ready = csio_is_lnode_ready(ln);
1207 rv = csio_do_abrt_cls(hw, ioreq, (ready ? SCSI_ABORT : SCSI_CLOSE));
1210 CSIO_INC_STATS(scsim, n_abrt_busy_error);
1212 CSIO_INC_STATS(scsim, n_cls_busy_error);
1217 * csio_scsi_abort_io_q - Abort all I/Os on given queue
1218 * @scm: SCSI module.
1219 * @q: Queue to abort.
1220 * @tmo: Timeout in ms
1222 * Attempt to abort all I/Os on given queue, and wait for a max
1223 * of tmo milliseconds for them to complete. Returns success
1224 * if all I/Os are aborted. Else returns -ETIMEDOUT.
1225 * Should be entered with lock held. Exits with lock held.
1227 * Lock has to be held across the loop that aborts I/Os, since dropping the lock
1228 * in between can cause the list to be corrupted. As a result, the caller
1229 * of this function has to ensure that the number of I/os to be aborted
1230 * is finite enough to not cause lock-held-for-too-long issues.
1233 csio_scsi_abort_io_q(struct csio_scsim *scm, struct list_head *q, uint32_t tmo)
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;
1243 csio_dbg(hw, "Aborting SCSI I/Os\n");
1245 /* Now abort/close I/Os in the queue passed */
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);
1251 /* Wait till all active I/Os are completed/aborted/closed */
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);
1258 /* all aborts completed */
1266 * csio_scsim_cleanup_io - Cleanup all I/Os in SCSI module.
1267 * @scm: SCSI module.
1268 * @abort: abort required.
1269 * Called with lock held, should exit with lock held.
1270 * Can sleep when waiting for I/Os to complete.
1273 csio_scsim_cleanup_io(struct csio_scsim *scm, bool abort)
1275 struct csio_hw *hw = scm->hw;
1277 int count = DIV_ROUND_UP(60 * 1000, CSIO_SCSI_ABORT_Q_POLL_MS);
1279 /* No I/Os pending */
1280 if (list_empty(&scm->active_q))
1283 /* Wait until all active I/Os are completed */
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);
1290 /* all I/Os completed */
1291 if (list_empty(&scm->active_q))
1296 rv = csio_scsi_abort_io_q(scm, &scm->active_q, 30000);
1299 csio_dbg(hw, "Some I/O aborts timed out, cleaning up..\n");
1302 csio_scsi_cleanup_io_q(scm, &scm->active_q);
1304 CSIO_DB_ASSERT(list_empty(&scm->active_q));
1310 * csio_scsim_cleanup_io_lnode - Cleanup all I/Os of given lnode.
1311 * @scm: SCSI module.
1314 * Called with lock held, should exit with lock held.
1315 * Can sleep (with dropped lock) when waiting for I/Os to complete.
1318 csio_scsim_cleanup_io_lnode(struct csio_scsim *scm, struct csio_lnode *ln)
1320 struct csio_hw *hw = scm->hw;
1321 struct csio_scsi_level_data sld;
1323 int count = DIV_ROUND_UP(60 * 1000, CSIO_SCSI_ABORT_Q_POLL_MS);
1325 csio_dbg(hw, "Gathering all SCSI I/Os on lnode %p\n", ln);
1327 sld.level = CSIO_LEV_LNODE;
1329 INIT_LIST_HEAD(&ln->cmpl_q);
1330 csio_scsi_gather_active_ios(scm, &sld, &ln->cmpl_q);
1332 /* No I/Os pending on this lnode */
1333 if (list_empty(&ln->cmpl_q))
1336 /* Wait until all active I/Os on this lnode are completed */
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);
1343 /* all I/Os completed */
1344 if (list_empty(&ln->cmpl_q))
1347 csio_dbg(hw, "Some I/Os pending on ln:%p, aborting them..\n", ln);
1349 /* I/Os are pending, abort them */
1350 rv = csio_scsi_abort_io_q(scm, &ln->cmpl_q, 30000);
1352 csio_dbg(hw, "Some I/O aborts timed out, cleaning up..\n");
1353 csio_scsi_cleanup_io_q(scm, &ln->cmpl_q);
1356 CSIO_DB_ASSERT(list_empty(&ln->cmpl_q));
1362 csio_show_hw_state(struct device *dev,
1363 struct device_attribute *attr, char *buf)
1365 struct csio_lnode *ln = shost_priv(class_to_shost(dev));
1366 struct csio_hw *hw = csio_lnode_to_hw(ln);
1368 if (csio_is_hw_ready(hw))
1369 return snprintf(buf, PAGE_SIZE, "ready\n");
1371 return snprintf(buf, PAGE_SIZE, "not ready\n");
1376 csio_device_reset(struct device *dev,
1377 struct device_attribute *attr, const char *buf, size_t count)
1379 struct csio_lnode *ln = shost_priv(class_to_shost(dev));
1380 struct csio_hw *hw = csio_lnode_to_hw(ln);
1385 /* Delete NPIV lnodes */
1386 csio_lnodes_exit(hw, 1);
1388 /* Block upper IOs */
1389 csio_lnodes_block_request(hw);
1391 spin_lock_irq(&hw->lock);
1393 spin_unlock_irq(&hw->lock);
1395 /* Unblock upper IOs */
1396 csio_lnodes_unblock_request(hw);
1402 csio_disable_port(struct device *dev,
1403 struct device_attribute *attr, const char *buf, size_t count)
1405 struct csio_lnode *ln = shost_priv(class_to_shost(dev));
1406 struct csio_hw *hw = csio_lnode_to_hw(ln);
1409 if (*buf == '1' || *buf == '0')
1410 disable = (*buf == '1') ? true : false;
1414 /* Block upper IOs */
1415 csio_lnodes_block_by_port(hw, ln->portid);
1417 spin_lock_irq(&hw->lock);
1418 csio_disable_lnodes(hw, ln->portid, disable);
1419 spin_unlock_irq(&hw->lock);
1421 /* Unblock upper IOs */
1422 csio_lnodes_unblock_by_port(hw, ln->portid);
1426 /* Show debug level */
1428 csio_show_dbg_level(struct device *dev,
1429 struct device_attribute *attr, char *buf)
1431 struct csio_lnode *ln = shost_priv(class_to_shost(dev));
1433 return snprintf(buf, PAGE_SIZE, "%x\n", ln->params.log_level);
1436 /* Store debug level */
1438 csio_store_dbg_level(struct device *dev,
1439 struct device_attribute *attr, const char *buf, size_t count)
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;
1445 if (!isdigit(buf[0]))
1448 if (sscanf(buf, "%i", &dbg_level))
1451 ln->params.log_level = dbg_level;
1452 hw->params.log_level = dbg_level;
1457 static DEVICE_ATTR(hw_state, S_IRUGO, csio_show_hw_state, NULL);
1458 static DEVICE_ATTR(device_reset, S_IWUSR, NULL, csio_device_reset);
1459 static DEVICE_ATTR(disable_port, S_IWUSR, NULL, csio_disable_port);
1460 static DEVICE_ATTR(dbg_level, S_IRUGO | S_IWUSR, csio_show_dbg_level,
1461 csio_store_dbg_level);
1463 static struct device_attribute *csio_fcoe_lport_attrs[] = {
1465 &dev_attr_device_reset,
1466 &dev_attr_disable_port,
1467 &dev_attr_dbg_level,
1472 csio_show_num_reg_rnodes(struct device *dev,
1473 struct device_attribute *attr, char *buf)
1475 struct csio_lnode *ln = shost_priv(class_to_shost(dev));
1477 return snprintf(buf, PAGE_SIZE, "%d\n", ln->num_reg_rnodes);
1480 static DEVICE_ATTR(num_reg_rnodes, S_IRUGO, csio_show_num_reg_rnodes, NULL);
1482 static struct device_attribute *csio_fcoe_vport_attrs[] = {
1483 &dev_attr_num_reg_rnodes,
1484 &dev_attr_dbg_level,
1488 static inline uint32_t
1489 csio_scsi_copy_to_sgl(struct csio_hw *hw, struct csio_ioreq *req)
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;
1500 struct csio_dma_buf *dma_buf;
1502 bytes_left = scsi_bufflen(scmnd);
1503 sg = scsi_sglist(scmnd);
1504 dma_buf = (struct csio_dma_buf *)csio_list_next(&req->gen_list);
1506 /* Copy data from driver buffer to SGs of SCSI CMD */
1507 while (bytes_left > 0 && sg && dma_buf) {
1508 if (buf_off >= dma_buf->len) {
1510 dma_buf = (struct csio_dma_buf *)
1511 csio_list_next(dma_buf);
1515 if (start_off >= sg->length) {
1516 start_off -= sg->length;
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)),
1528 sg_addr = kmap_atomic(sg_page(sg) + (sg_off >> PAGE_SHIFT));
1530 csio_err(hw, "failed to kmap sg:%p of ioreq:%p\n",
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);
1540 start_off += bytes_copy;
1541 buf_off += bytes_copy;
1542 bytes_left -= bytes_copy;
1552 * csio_scsi_err_handler - SCSI error handler.
1558 csio_scsi_err_handler(struct csio_hw *hw, struct csio_ioreq *req)
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);
1571 switch (req->wr_status) {
1573 if (unlikely(!csio_is_hw_ready(hw)))
1576 host_status = DID_ERROR;
1577 CSIO_INC_STATS(scm, n_hosterror);
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;
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;
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;
1601 memcpy(cmnd->sense_buffer,
1602 &rsp_info->_fr_resvd[0] + rsp_len, sns_len);
1603 CSIO_INC_STATS(scm, n_autosense);
1606 scsi_set_resid(cmnd, 0);
1609 if (flags & FCP_RESID_UNDER) {
1610 scsi_set_resid(cmnd,
1611 be32_to_cpu(fcp_resp->ext.fr_resid));
1613 if (!(flags & FCP_SNS_LEN_VAL) &&
1614 (scsi_status == SAM_STAT_GOOD) &&
1615 ((scsi_bufflen(cmnd) - scsi_get_resid(cmnd))
1617 host_status = DID_ERROR;
1618 } else if (flags & FCP_RESID_OVER)
1619 host_status = DID_ERROR;
1621 CSIO_INC_STATS(scm, n_rsperror);
1624 case FW_SCSI_OVER_FLOW_ERR:
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);
1633 case FW_SCSI_UNDER_FLOW_ERR:
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,
1640 host_status = DID_ERROR;
1641 CSIO_INC_STATS(scm, n_unflerror);
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,
1649 (req->wr_status == FW_SCSI_CLOSE_REQUESTED) ?
1650 "closed" : "aborted");
1652 * csio_eh_abort_handler checks this value to
1653 * succeed or fail the abort request.
1655 host_status = DID_REQUEUE;
1656 if (req->wr_status == FW_SCSI_CLOSE_REQUESTED)
1657 CSIO_INC_STATS(scm, n_closed);
1659 CSIO_INC_STATS(scm, n_aborted);
1662 case FW_SCSI_ABORT_TIMEDOUT:
1663 /* FW timed out the abort itself */
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);
1670 case FW_RDEV_NOT_READY:
1672 * In firmware, a RDEV can get into this state
1673 * temporarily, before moving into dissapeared/lost
1674 * state. So, the driver should complete the request equivalent
1675 * to device-disappeared!
1677 CSIO_INC_STATS(scm, n_rdev_nr_error);
1678 host_status = DID_ERROR;
1681 case FW_ERR_RDEV_LOST:
1682 CSIO_INC_STATS(scm, n_rdev_lost_error);
1683 host_status = DID_ERROR;
1686 case FW_ERR_RDEV_LOGO:
1687 CSIO_INC_STATS(scm, n_rdev_logo_error);
1688 host_status = DID_ERROR;
1691 case FW_ERR_RDEV_IMPL_LOGO:
1692 host_status = DID_ERROR;
1695 case FW_ERR_LINK_DOWN:
1696 CSIO_INC_STATS(scm, n_link_down_error);
1697 host_status = DID_ERROR;
1700 case FW_FCOE_NO_XCHG:
1701 CSIO_INC_STATS(scm, n_no_xchg_error);
1702 host_status = DID_ERROR;
1706 csio_err(hw, "Unknown SCSI FW WR status:%d req:%p cmnd:%p\n",
1707 req->wr_status, req, cmnd);
1710 CSIO_INC_STATS(scm, n_unknown_error);
1711 host_status = DID_ERROR;
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);
1722 cmnd->result = (((host_status) << 16) | scsi_status);
1723 cmnd->scsi_done(cmnd);
1725 /* Wake up waiting threads */
1726 csio_scsi_cmnd(req) = NULL;
1727 complete(&req->cmplobj);
1731 * csio_scsi_cbfn - SCSI callback function.
1737 csio_scsi_cbfn(struct csio_hw *hw, struct csio_ioreq *req)
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;
1743 if (likely(req->wr_status == FW_SUCCESS)) {
1744 if (req->nsge > 0) {
1745 scsi_dma_unmap(cmnd);
1747 host_status = csio_scsi_copy_to_sgl(hw, req);
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);
1755 /* Error handling */
1756 csio_scsi_err_handler(hw, req);
1761 * csio_queuecommand - Entry point to kickstart an I/O request.
1762 * @host: The scsi_host pointer.
1763 * @cmnd: The I/O request from ML.
1765 * This routine does the following:
1766 * - Checks for HW and Rnode module readiness.
1767 * - Gets a free ioreq structure (which is already initialized
1768 * to uninit during its allocation).
1769 * - Maps SG elements.
1770 * - Initializes ioreq members.
1771 * - Kicks off the SCSI state machine for this IO.
1772 * - Returns busy status on error.
1775 csio_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmnd)
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;
1784 int rv = SCSI_MLQUEUE_HOST_BUSY, nr;
1786 struct csio_scsi_qset *sqset;
1787 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1789 sqset = &hw->sqset[ln->portid][blk_mq_rq_cpu(cmnd->request)];
1791 nr = fc_remote_port_chkready(rport);
1794 CSIO_INC_STATS(scsim, n_rn_nr_error);
1798 if (unlikely(!csio_is_hw_ready(hw))) {
1799 cmnd->result = (DID_REQUEUE << 16);
1800 CSIO_INC_STATS(scsim, n_hw_nr_error);
1804 /* Get req->nsge, if there are SG elements to be mapped */
1805 nsge = scsi_dma_map(cmnd);
1806 if (unlikely(nsge < 0)) {
1807 CSIO_INC_STATS(scsim, n_dmamap_error);
1811 /* Do we support so many mappings? */
1812 if (unlikely(nsge > scsim->max_sge)) {
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);
1820 /* Get a free ioreq structure - SM is already set to uninit */
1821 ioreq = csio_get_scsi_ioreq_lock(hw, scsim);
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);
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;
1838 ioreq->datadir = cmnd->sc_data_direction;
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);
1847 CSIO_INC_STATS(ln, n_control_requests);
1850 ioreq->io_cbfn = csio_scsi_cbfn;
1852 /* Needed during abort */
1853 cmnd->host_scribble = (unsigned char *)ioreq;
1854 cmnd->SCp.Message = 0;
1856 /* Kick off SCSI IO SM on the ioreq */
1857 spin_lock_irqsave(&hw->lock, flags);
1858 retval = csio_scsi_start_io(ioreq);
1859 spin_unlock_irqrestore(&hw->lock, flags);
1862 csio_err(hw, "ioreq: %p couldnt be started, status:%d\n",
1864 CSIO_INC_STATS(scsim, n_busy_error);
1871 csio_put_scsi_ioreq_lock(hw, scsim, ioreq);
1874 scsi_dma_unmap(cmnd);
1879 cmnd->scsi_done(cmnd);
1884 csio_do_abrt_cls(struct csio_hw *hw, struct csio_ioreq *ioreq, bool abort)
1887 int cpu = smp_processor_id();
1888 struct csio_lnode *ln = ioreq->lnode;
1889 struct csio_scsi_qset *sqset = &hw->sqset[ln->portid][cpu];
1891 ioreq->tmo = CSIO_SCSI_ABRT_TMO_MS;
1893 * Use current processor queue for posting the abort/close, but retain
1894 * the ingress queue ID of the original I/O being aborted/closed - we
1895 * need the abort/close completion to be received on the same queue
1896 * as the original I/O.
1898 ioreq->eq_idx = sqset->eq_idx;
1900 if (abort == SCSI_ABORT)
1901 rv = csio_scsi_abort(ioreq);
1903 rv = csio_scsi_close(ioreq);
1909 csio_eh_abort_handler(struct scsi_cmnd *cmnd)
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);
1916 unsigned long tmo = 0;
1918 struct csio_rnode *rn = (struct csio_rnode *)(cmnd->device->hostdata);
1920 ret = fc_block_scsi_eh(cmnd);
1924 ioreq = (struct csio_ioreq *)cmnd->host_scribble;
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));
1937 if (((struct scsi_cmnd *)csio_scsi_cmnd(ioreq)) != cmnd) {
1938 CSIO_INC_STATS(scsim, n_abrt_race_comp);
1942 ready = csio_is_lnode_ready(ln);
1943 tmo = CSIO_SCSI_ABRT_TMO_MS;
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);
1951 if (rv == -EINVAL) {
1952 /* Return success, if abort/close request issued on
1953 * already completed IO
1958 CSIO_INC_STATS(scsim, n_abrt_busy_error);
1960 CSIO_INC_STATS(scsim, n_cls_busy_error);
1965 wait_for_completion_timeout(&ioreq->cmplobj, msecs_to_jiffies(tmo));
1967 /* FW didnt respond to abort within our timeout */
1968 if (((struct scsi_cmnd *)csio_scsi_cmnd(ioreq)) == cmnd) {
1970 csio_err(hw, "Abort timed out -- req: %p\n", ioreq);
1971 CSIO_INC_STATS(scsim, n_abrt_timedout);
1974 if (ioreq->nsge > 0)
1975 scsi_dma_unmap(cmnd);
1977 spin_lock_irq(&hw->lock);
1978 csio_scsi_cmnd(ioreq) = NULL;
1979 spin_unlock_irq(&hw->lock);
1981 cmnd->result = (DID_ERROR << 16);
1982 cmnd->scsi_done(cmnd);
1987 /* FW successfully aborted the request */
1988 if (host_byte(cmnd->result) == DID_REQUEUE) {
1990 "Aborted SCSI command to (%d:%llu) tag %u\n",
1991 cmnd->device->id, cmnd->device->lun,
1992 cmnd->request->tag);
1996 "Failed to abort SCSI command, (%d:%llu) tag %u\n",
1997 cmnd->device->id, cmnd->device->lun,
1998 cmnd->request->tag);
2004 * csio_tm_cbfn - TM callback function.
2008 * Cache the result in 'cmnd', since ioreq will be freed soon
2009 * after we return from here, and the waiting thread shouldnt trust
2010 * the ioreq contents.
2013 csio_tm_cbfn(struct csio_hw *hw, struct csio_ioreq *req)
2015 struct scsi_cmnd *cmnd = (struct scsi_cmnd *)csio_scsi_cmnd(req);
2016 struct csio_dma_buf *dma_buf;
2018 struct fcp_resp_with_ext *fcp_resp;
2019 struct fcp_resp_rsp_info *rsp_info;
2021 csio_dbg(hw, "req: %p in csio_tm_cbfn status: %d\n",
2022 req, req->wr_status);
2024 /* Cache FW return status */
2025 cmnd->SCp.Status = req->wr_status;
2027 /* Special handling based on FCP response */
2030 * FW returns us this error, if flags were set. FCP4 says
2031 * FCP_RSP_LEN_VAL in flags shall be set for TM completions.
2032 * So if a target were to set this bit, we expect that the
2033 * rsp_code is set to FCP_TMF_CMPL for a successful TM
2034 * completion. Any other rsp_code means TM operation failed.
2035 * If a target were to just ignore setting flags, we treat
2036 * the TM operation as success, and FW returns FW_SUCCESS.
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);
2043 flags = fcp_resp->resp.fr_flags;
2045 /* Modify return status if flags indicate success */
2046 if (flags & FCP_RSP_LEN_VAL)
2047 if (rsp_info->rsp_code == FCP_TMF_CMPL)
2048 cmnd->SCp.Status = FW_SUCCESS;
2050 csio_dbg(hw, "TM FCP rsp code: %d\n", rsp_info->rsp_code);
2053 /* Wake up the TM handler thread */
2054 csio_scsi_cmnd(req) = NULL;
2058 csio_eh_lun_reset_handler(struct scsi_cmnd *cmnd)
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;
2070 struct csio_scsi_level_data sld;
2075 csio_dbg(hw, "Request to reset LUN:%llu (ssni:0x%x tgtid:%d)\n",
2076 cmnd->device->lun, rn->flowid, rn->scsi_id);
2078 if (!csio_is_lnode_ready(ln)) {
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);
2086 /* Lnode is ready, now wait on rport node readiness */
2087 ret = fc_block_scsi_eh(cmnd);
2092 * If we have blocked in the previous call, at this point, either the
2093 * remote node has come back online, or device loss timer has fired
2094 * and the remote node is destroyed. Allow the LUN reset only for
2095 * the former case, since LUN reset is a TMF I/O on the wire, and we
2096 * need a valid session to issue it.
2098 if (fc_remote_port_chkready(rn->rport)) {
2100 "LUN reset cannot be issued on non-ready"
2101 " remote node ssni:0x%x (LUN:%llu)\n",
2102 rn->flowid, cmnd->device->lun);
2106 /* Get a free ioreq structure - SM is already set to uninit */
2107 ioreq = csio_get_scsi_ioreq_lock(hw, scsim);
2110 csio_err(hw, "Out of IO request elements. Active # :%d\n",
2111 scsim->stats.n_active);
2115 sqset = &hw->sqset[ln->portid][smp_processor_id()];
2119 ioreq->iq_idx = sqset->iq_idx;
2120 ioreq->eq_idx = sqset->eq_idx;
2122 csio_scsi_cmnd(ioreq) = cmnd;
2123 cmnd->host_scribble = (unsigned char *)ioreq;
2124 cmnd->SCp.Status = 0;
2126 cmnd->SCp.Message = FCP_TMF_LUN_RESET;
2127 ioreq->tmo = CSIO_SCSI_LUNRST_TMO_MS / 1000;
2130 * FW times the LUN reset for ioreq->tmo, so we got to wait a little
2131 * longer (10s for now) than that to allow FW to return the timed
2134 count = DIV_ROUND_UP((ioreq->tmo + 10) * 1000, CSIO_SCSI_TM_POLL_MS);
2137 ioreq->io_cbfn = csio_tm_cbfn;
2139 /* Save of the ioreq info for later use */
2140 sld.level = CSIO_LEV_LUN;
2141 sld.lnode = ioreq->lnode;
2142 sld.rnode = ioreq->rnode;
2143 sld.oslun = cmnd->device->lun;
2145 spin_lock_irqsave(&hw->lock, flags);
2146 /* Kick off TM SM on the ioreq */
2147 retval = csio_scsi_start_tm(ioreq);
2148 spin_unlock_irqrestore(&hw->lock, flags);
2151 csio_err(hw, "Failed to issue LUN reset, req:%p, status:%d\n",
2153 goto fail_ret_ioreq;
2156 csio_dbg(hw, "Waiting max %d secs for LUN reset completion\n",
2157 count * (CSIO_SCSI_TM_POLL_MS / 1000));
2158 /* Wait for completion */
2159 while ((((struct scsi_cmnd *)csio_scsi_cmnd(ioreq)) == cmnd)
2161 msleep(CSIO_SCSI_TM_POLL_MS);
2163 /* LUN reset timed-out */
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);
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);
2173 goto fail_ret_ioreq;
2176 /* LUN reset returned, check cached status */
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);
2183 /* LUN reset succeeded, Start aborting affected I/Os */
2185 * Since the host guarantees during LUN reset that there
2186 * will not be any more I/Os to that LUN, until the LUN reset
2187 * completes, we gather pending I/Os after the LUN reset.
2189 spin_lock_irq(&hw->lock);
2190 csio_scsi_gather_active_ios(scsim, &sld, &local_q);
2192 retval = csio_scsi_abort_io_q(scsim, &local_q, 30000);
2193 spin_unlock_irq(&hw->lock);
2195 /* Aborts may have timed out */
2198 "Attempt to abort I/Os during LUN reset of %llu"
2199 " returned %d\n", cmnd->device->lun, retval);
2200 /* Return I/Os back to active_q */
2201 spin_lock_irq(&hw->lock);
2202 list_splice_tail_init(&local_q, &scsim->active_q);
2203 spin_unlock_irq(&hw->lock);
2207 CSIO_INC_STATS(rn, n_lun_rst);
2209 csio_info(hw, "LUN reset occurred (%d:%llu)\n",
2210 cmnd->device->id, cmnd->device->lun);
2215 csio_put_scsi_ioreq_lock(hw, scsim, ioreq);
2217 CSIO_INC_STATS(rn, n_lun_rst_fail);
2222 csio_slave_alloc(struct scsi_device *sdev)
2224 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2226 if (!rport || fc_remote_port_chkready(rport))
2229 sdev->hostdata = *((struct csio_lnode **)(rport->dd_data));
2235 csio_slave_configure(struct scsi_device *sdev)
2237 scsi_change_queue_depth(sdev, csio_lun_qdepth);
2242 csio_slave_destroy(struct scsi_device *sdev)
2244 sdev->hostdata = NULL;
2248 csio_scan_finished(struct Scsi_Host *shost, unsigned long time)
2250 struct csio_lnode *ln = shost_priv(shost);
2253 spin_lock_irq(shost->host_lock);
2254 if (!ln->hwp || csio_list_deleted(&ln->sm.sm_list))
2257 rv = csio_scan_done(ln, jiffies, time, csio_max_scan_tmo * HZ,
2258 csio_delta_scan_tmo * HZ);
2260 spin_unlock_irq(shost->host_lock);
2265 struct 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,
2278 .sg_tablesize = CSIO_SCSI_MAX_SGE,
2279 .cmd_per_lun = CSIO_MAX_CMD_PER_LUN,
2280 .shost_attrs = csio_fcoe_lport_attrs,
2281 .max_sectors = CSIO_MAX_SECTOR_SIZE,
2284 struct scsi_host_template csio_fcoe_shost_vport_template = {
2285 .module = THIS_MODULE,
2286 .name = CSIO_DRV_DESC,
2287 .proc_name = KBUILD_MODNAME,
2288 .queuecommand = csio_queuecommand,
2289 .eh_timed_out = fc_eh_timed_out,
2290 .eh_abort_handler = csio_eh_abort_handler,
2291 .eh_device_reset_handler = csio_eh_lun_reset_handler,
2292 .slave_alloc = csio_slave_alloc,
2293 .slave_configure = csio_slave_configure,
2294 .slave_destroy = csio_slave_destroy,
2295 .scan_finished = csio_scan_finished,
2297 .sg_tablesize = CSIO_SCSI_MAX_SGE,
2298 .cmd_per_lun = CSIO_MAX_CMD_PER_LUN,
2299 .shost_attrs = csio_fcoe_vport_attrs,
2300 .max_sectors = CSIO_MAX_SECTOR_SIZE,
2304 * csio_scsi_alloc_ddp_bufs - Allocate buffers for DDP of unaligned SGLs.
2307 * @buf_size: buffer size
2308 * @num_buf : Number of buffers.
2310 * This routine allocates DMA buffers required for SCSI Data xfer, if
2311 * each SGL buffer for a SCSI Read request posted by SCSI midlayer are
2312 * not virtually contiguous.
2315 csio_scsi_alloc_ddp_bufs(struct csio_scsim *scm, struct csio_hw *hw,
2316 int buf_size, int num_buf)
2319 struct list_head *tmp;
2320 struct csio_dma_buf *ddp_desc = NULL;
2321 uint32_t unit_size = 0;
2329 INIT_LIST_HEAD(&scm->ddp_freelist);
2331 /* Align buf size to page size */
2332 buf_size = (buf_size + PAGE_SIZE - 1) & PAGE_MASK;
2333 /* Initialize dma descriptors */
2334 for (n = 0; n < num_buf; n++) {
2335 /* Set unit size to request size */
2336 unit_size = buf_size;
2337 ddp_desc = kzalloc(sizeof(struct csio_dma_buf), GFP_KERNEL);
2340 "Failed to allocate ddp descriptors,"
2341 " Num allocated = %d.\n",
2342 scm->stats.n_free_ddp);
2346 /* Allocate Dma buffers for DDP */
2347 ddp_desc->vaddr = dma_alloc_coherent(&hw->pdev->dev, unit_size,
2348 &ddp_desc->paddr, GFP_KERNEL);
2349 if (!ddp_desc->vaddr) {
2351 "SCSI response DMA buffer (ddp) allocation"
2357 ddp_desc->len = unit_size;
2359 /* Added it to scsi ddp freelist */
2360 list_add_tail(&ddp_desc->list, &scm->ddp_freelist);
2361 CSIO_INC_STATS(scm, n_free_ddp);
2366 /* release dma descs back to freelist and free dma memory */
2367 list_for_each(tmp, &scm->ddp_freelist) {
2368 ddp_desc = (struct csio_dma_buf *) tmp;
2369 tmp = csio_list_prev(tmp);
2370 dma_free_coherent(&hw->pdev->dev, ddp_desc->len,
2371 ddp_desc->vaddr, ddp_desc->paddr);
2372 list_del_init(&ddp_desc->list);
2375 scm->stats.n_free_ddp = 0;
2381 * csio_scsi_free_ddp_bufs - free DDP buffers of unaligned SGLs.
2385 * This routine frees ddp buffers.
2388 csio_scsi_free_ddp_bufs(struct csio_scsim *scm, struct csio_hw *hw)
2390 struct list_head *tmp;
2391 struct csio_dma_buf *ddp_desc;
2393 /* release dma descs back to freelist and free dma memory */
2394 list_for_each(tmp, &scm->ddp_freelist) {
2395 ddp_desc = (struct csio_dma_buf *) tmp;
2396 tmp = csio_list_prev(tmp);
2397 dma_free_coherent(&hw->pdev->dev, ddp_desc->len,
2398 ddp_desc->vaddr, ddp_desc->paddr);
2399 list_del_init(&ddp_desc->list);
2402 scm->stats.n_free_ddp = 0;
2406 * csio_scsim_init - Initialize SCSI Module
2412 csio_scsim_init(struct csio_scsim *scm, struct csio_hw *hw)
2415 struct csio_ioreq *ioreq;
2416 struct csio_dma_buf *dma_buf;
2418 INIT_LIST_HEAD(&scm->active_q);
2421 scm->proto_cmd_len = sizeof(struct fcp_cmnd);
2422 scm->proto_rsp_len = CSIO_SCSI_RSP_LEN;
2423 scm->max_sge = CSIO_SCSI_MAX_SGE;
2425 spin_lock_init(&scm->freelist_lock);
2427 /* Pre-allocate ioreqs and initialize them */
2428 INIT_LIST_HEAD(&scm->ioreq_freelist);
2429 for (i = 0; i < csio_scsi_ioreqs; i++) {
2431 ioreq = kzalloc(sizeof(struct csio_ioreq), GFP_KERNEL);
2434 "I/O request element allocation failed, "
2435 " Num allocated = %d.\n",
2436 scm->stats.n_free_ioreq);
2441 /* Allocate Dma buffers for Response Payload */
2442 dma_buf = &ioreq->dma_buf;
2443 dma_buf->vaddr = dma_pool_alloc(hw->scsi_dma_pool, GFP_KERNEL,
2445 if (!dma_buf->vaddr) {
2447 "SCSI response DMA buffer allocation"
2453 dma_buf->len = scm->proto_rsp_len;
2455 /* Set state to uninit */
2456 csio_init_state(&ioreq->sm, csio_scsis_uninit);
2457 INIT_LIST_HEAD(&ioreq->gen_list);
2458 init_completion(&ioreq->cmplobj);
2460 list_add_tail(&ioreq->sm.sm_list, &scm->ioreq_freelist);
2461 CSIO_INC_STATS(scm, n_free_ioreq);
2464 if (csio_scsi_alloc_ddp_bufs(scm, hw, PAGE_SIZE, csio_ddp_descs))
2471 * Free up existing allocations, since an error
2472 * from here means we are returning for good
2474 while (!list_empty(&scm->ioreq_freelist)) {
2475 struct csio_sm *tmp;
2477 tmp = list_first_entry(&scm->ioreq_freelist,
2478 struct csio_sm, sm_list);
2479 list_del_init(&tmp->sm_list);
2480 ioreq = (struct csio_ioreq *)tmp;
2482 dma_buf = &ioreq->dma_buf;
2483 dma_pool_free(hw->scsi_dma_pool, dma_buf->vaddr,
2489 scm->stats.n_free_ioreq = 0;
2495 * csio_scsim_exit: Uninitialize SCSI Module
2500 csio_scsim_exit(struct csio_scsim *scm)
2502 struct csio_ioreq *ioreq;
2503 struct csio_dma_buf *dma_buf;
2505 while (!list_empty(&scm->ioreq_freelist)) {
2506 struct csio_sm *tmp;
2508 tmp = list_first_entry(&scm->ioreq_freelist,
2509 struct csio_sm, sm_list);
2510 list_del_init(&tmp->sm_list);
2511 ioreq = (struct csio_ioreq *)tmp;
2513 dma_buf = &ioreq->dma_buf;
2514 dma_pool_free(scm->hw->scsi_dma_pool, dma_buf->vaddr,
2520 scm->stats.n_free_ioreq = 0;
2522 csio_scsi_free_ddp_bufs(scm, scm->hw);