2 * Copyright 2008 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
5 * This program is free software; you may redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
10 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
13 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
14 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
15 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18 #include <linux/mempool.h>
19 #include <linux/errno.h>
20 #include <linux/init.h>
21 #include <linux/workqueue.h>
22 #include <linux/pci.h>
23 #include <linux/scatterlist.h>
24 #include <linux/skbuff.h>
25 #include <linux/spinlock.h>
26 #include <linux/if_ether.h>
27 #include <linux/if_vlan.h>
28 #include <linux/delay.h>
29 #include <scsi/scsi.h>
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <scsi/scsi_tcq.h>
34 #include <scsi/fc/fc_els.h>
35 #include <scsi/fc/fc_fcoe.h>
36 #include <scsi/libfc.h>
37 #include <scsi/fc_frame.h>
41 const char *fnic_state_str[] = {
42 [FNIC_IN_FC_MODE] = "FNIC_IN_FC_MODE",
43 [FNIC_IN_FC_TRANS_ETH_MODE] = "FNIC_IN_FC_TRANS_ETH_MODE",
44 [FNIC_IN_ETH_MODE] = "FNIC_IN_ETH_MODE",
45 [FNIC_IN_ETH_TRANS_FC_MODE] = "FNIC_IN_ETH_TRANS_FC_MODE",
48 static const char *fnic_ioreq_state_str[] = {
49 [FNIC_IOREQ_CMD_PENDING] = "FNIC_IOREQ_CMD_PENDING",
50 [FNIC_IOREQ_ABTS_PENDING] = "FNIC_IOREQ_ABTS_PENDING",
51 [FNIC_IOREQ_ABTS_COMPLETE] = "FNIC_IOREQ_ABTS_COMPLETE",
52 [FNIC_IOREQ_CMD_COMPLETE] = "FNIC_IOREQ_CMD_COMPLETE",
55 static const char *fcpio_status_str[] = {
56 [FCPIO_SUCCESS] = "FCPIO_SUCCESS", /*0x0*/
57 [FCPIO_INVALID_HEADER] = "FCPIO_INVALID_HEADER",
58 [FCPIO_OUT_OF_RESOURCE] = "FCPIO_OUT_OF_RESOURCE",
59 [FCPIO_INVALID_PARAM] = "FCPIO_INVALID_PARAM]",
60 [FCPIO_REQ_NOT_SUPPORTED] = "FCPIO_REQ_NOT_SUPPORTED",
61 [FCPIO_IO_NOT_FOUND] = "FCPIO_IO_NOT_FOUND",
62 [FCPIO_ABORTED] = "FCPIO_ABORTED", /*0x41*/
63 [FCPIO_TIMEOUT] = "FCPIO_TIMEOUT",
64 [FCPIO_SGL_INVALID] = "FCPIO_SGL_INVALID",
65 [FCPIO_MSS_INVALID] = "FCPIO_MSS_INVALID",
66 [FCPIO_DATA_CNT_MISMATCH] = "FCPIO_DATA_CNT_MISMATCH",
67 [FCPIO_FW_ERR] = "FCPIO_FW_ERR",
68 [FCPIO_ITMF_REJECTED] = "FCPIO_ITMF_REJECTED",
69 [FCPIO_ITMF_FAILED] = "FCPIO_ITMF_FAILED",
70 [FCPIO_ITMF_INCORRECT_LUN] = "FCPIO_ITMF_INCORRECT_LUN",
71 [FCPIO_CMND_REJECTED] = "FCPIO_CMND_REJECTED",
72 [FCPIO_NO_PATH_AVAIL] = "FCPIO_NO_PATH_AVAIL",
73 [FCPIO_PATH_FAILED] = "FCPIO_PATH_FAILED",
74 [FCPIO_LUNMAP_CHNG_PEND] = "FCPIO_LUNHMAP_CHNG_PEND",
77 const char *fnic_state_to_str(unsigned int state)
79 if (state >= ARRAY_SIZE(fnic_state_str) || !fnic_state_str[state])
82 return fnic_state_str[state];
85 static const char *fnic_ioreq_state_to_str(unsigned int state)
87 if (state >= ARRAY_SIZE(fnic_ioreq_state_str) ||
88 !fnic_ioreq_state_str[state])
91 return fnic_ioreq_state_str[state];
94 static const char *fnic_fcpio_status_to_str(unsigned int status)
96 if (status >= ARRAY_SIZE(fcpio_status_str) || !fcpio_status_str[status])
99 return fcpio_status_str[status];
102 static void fnic_cleanup_io(struct fnic *fnic, int exclude_id);
104 static inline spinlock_t *fnic_io_lock_hash(struct fnic *fnic,
105 struct scsi_cmnd *sc)
107 u32 hash = sc->request->tag & (FNIC_IO_LOCKS - 1);
109 return &fnic->io_req_lock[hash];
113 * Unmap the data buffer and sense buffer for an io_req,
114 * also unmap and free the device-private scatter/gather list.
116 static void fnic_release_ioreq_buf(struct fnic *fnic,
117 struct fnic_io_req *io_req,
118 struct scsi_cmnd *sc)
120 if (io_req->sgl_list_pa)
121 pci_unmap_single(fnic->pdev, io_req->sgl_list_pa,
122 sizeof(io_req->sgl_list[0]) * io_req->sgl_cnt,
127 mempool_free(io_req->sgl_list_alloc,
128 fnic->io_sgl_pool[io_req->sgl_type]);
129 if (io_req->sense_buf_pa)
130 pci_unmap_single(fnic->pdev, io_req->sense_buf_pa,
131 SCSI_SENSE_BUFFERSIZE, PCI_DMA_FROMDEVICE);
134 /* Free up Copy Wq descriptors. Called with copy_wq lock held */
135 static int free_wq_copy_descs(struct fnic *fnic, struct vnic_wq_copy *wq)
137 /* if no Ack received from firmware, then nothing to clean */
138 if (!fnic->fw_ack_recd[0])
142 * Update desc_available count based on number of freed descriptors
143 * Account for wraparound
145 if (wq->to_clean_index <= fnic->fw_ack_index[0])
146 wq->ring.desc_avail += (fnic->fw_ack_index[0]
147 - wq->to_clean_index + 1);
149 wq->ring.desc_avail += (wq->ring.desc_count
151 + fnic->fw_ack_index[0] + 1);
154 * just bump clean index to ack_index+1 accounting for wraparound
155 * this will essentially free up all descriptors between
156 * to_clean_index and fw_ack_index, both inclusive
159 (fnic->fw_ack_index[0] + 1) % wq->ring.desc_count;
161 /* we have processed the acks received so far */
162 fnic->fw_ack_recd[0] = 0;
168 * fnic_fw_reset_handler
169 * Routine to send reset msg to fw
171 int fnic_fw_reset_handler(struct fnic *fnic)
173 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
177 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
179 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
180 free_wq_copy_descs(fnic, wq);
182 if (!vnic_wq_copy_desc_avail(wq))
185 fnic_queue_wq_copy_desc_fw_reset(wq, SCSI_NO_TAG);
187 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
190 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
191 "Issued fw reset\n");
193 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
194 "Failed to issue fw reset\n");
200 * fnic_flogi_reg_handler
201 * Routine to send flogi register msg to fw
203 int fnic_flogi_reg_handler(struct fnic *fnic)
205 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
210 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
212 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
213 free_wq_copy_descs(fnic, wq);
215 if (!vnic_wq_copy_desc_avail(wq)) {
217 goto flogi_reg_ioreq_end;
220 if (fnic->fcoui_mode)
221 memset(gw_mac, 0xff, ETH_ALEN);
223 memcpy(gw_mac, fnic->dest_addr, ETH_ALEN);
225 fnic_queue_wq_copy_desc_flogi_reg(wq, SCSI_NO_TAG,
226 FCPIO_FLOGI_REG_GW_DEST,
231 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
234 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
235 "flog reg issued\n");
241 * fnic_queue_wq_copy_desc
242 * Routine to enqueue a wq copy desc
244 static inline int fnic_queue_wq_copy_desc(struct fnic *fnic,
245 struct vnic_wq_copy *wq,
246 struct fnic_io_req *io_req,
247 struct scsi_cmnd *sc,
250 struct scatterlist *sg;
251 struct fc_rport *rport = starget_to_rport(scsi_target(sc->device));
252 struct fc_rport_libfc_priv *rp = rport->dd_data;
253 struct host_sg_desc *desc;
256 unsigned long intr_flags;
259 struct scsi_lun fc_lun;
263 /* For each SGE, create a device desc entry */
264 desc = io_req->sgl_list;
265 for_each_sg(scsi_sglist(sc), sg, sg_count, i) {
266 desc->addr = cpu_to_le64(sg_dma_address(sg));
267 desc->len = cpu_to_le32(sg_dma_len(sg));
272 io_req->sgl_list_pa = pci_map_single
275 sizeof(io_req->sgl_list[0]) * sg_count,
279 io_req->sense_buf_pa = pci_map_single(fnic->pdev,
281 SCSI_SENSE_BUFFERSIZE,
284 int_to_scsilun(sc->device->lun, &fc_lun);
286 pri_tag = FCPIO_ICMND_PTA_SIMPLE;
287 msg[0] = MSG_SIMPLE_TAG;
288 scsi_populate_tag_msg(sc, msg);
289 if (msg[0] == MSG_ORDERED_TAG)
290 pri_tag = FCPIO_ICMND_PTA_ORDERED;
292 /* Enqueue the descriptor in the Copy WQ */
293 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
295 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
296 free_wq_copy_descs(fnic, wq);
298 if (unlikely(!vnic_wq_copy_desc_avail(wq))) {
299 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
300 return SCSI_MLQUEUE_HOST_BUSY;
304 if (sc->sc_data_direction == DMA_FROM_DEVICE)
305 flags = FCPIO_ICMND_RDDATA;
306 else if (sc->sc_data_direction == DMA_TO_DEVICE)
307 flags = FCPIO_ICMND_WRDATA;
310 if ((fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR) &&
311 (rp->flags & FC_RP_FLAGS_RETRY))
312 exch_flags |= FCPIO_ICMND_SRFLAG_RETRY;
314 fnic_queue_wq_copy_desc_icmnd_16(wq, sc->request->tag,
315 0, exch_flags, io_req->sgl_cnt,
316 SCSI_SENSE_BUFFERSIZE,
318 io_req->sense_buf_pa,
319 0, /* scsi cmd ref, always 0 */
320 pri_tag, /* scsi pri and tag */
321 flags, /* command flags */
322 sc->cmnd, sc->cmd_len,
324 fc_lun.scsi_lun, io_req->port_id,
325 rport->maxframe_size, rp->r_a_tov,
328 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
334 * Routine to send a scsi cdb
335 * Called with host_lock held and interrupts disabled.
337 int fnic_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
340 struct fc_rport *rport;
341 struct fnic_io_req *io_req;
343 struct vnic_wq_copy *wq;
349 rport = starget_to_rport(scsi_target(sc->device));
350 ret = fc_remote_port_chkready(rport);
357 lp = shost_priv(sc->device->host);
358 if (lp->state != LPORT_ST_READY || !(lp->link_up))
359 return SCSI_MLQUEUE_HOST_BUSY;
362 * Release host lock, use driver resource specific locks from here.
363 * Don't re-enable interrupts in case they were disabled prior to the
364 * caller disabling them.
366 spin_unlock(lp->host->host_lock);
368 /* Get a new io_req for this SCSI IO */
369 fnic = lport_priv(lp);
371 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
373 ret = SCSI_MLQUEUE_HOST_BUSY;
376 memset(io_req, 0, sizeof(*io_req));
378 /* Map the data buffer */
379 sg_count = scsi_dma_map(sc);
381 mempool_free(io_req, fnic->io_req_pool);
385 /* Determine the type of scatter/gather list we need */
386 io_req->sgl_cnt = sg_count;
387 io_req->sgl_type = FNIC_SGL_CACHE_DFLT;
388 if (sg_count > FNIC_DFLT_SG_DESC_CNT)
389 io_req->sgl_type = FNIC_SGL_CACHE_MAX;
393 mempool_alloc(fnic->io_sgl_pool[io_req->sgl_type],
394 GFP_ATOMIC | GFP_DMA);
395 if (!io_req->sgl_list) {
396 ret = SCSI_MLQUEUE_HOST_BUSY;
398 mempool_free(io_req, fnic->io_req_pool);
402 /* Cache sgl list allocated address before alignment */
403 io_req->sgl_list_alloc = io_req->sgl_list;
404 ptr = (unsigned long) io_req->sgl_list;
405 if (ptr % FNIC_SG_DESC_ALIGN) {
406 io_req->sgl_list = (struct host_sg_desc *)
407 (((unsigned long) ptr
408 + FNIC_SG_DESC_ALIGN - 1)
409 & ~(FNIC_SG_DESC_ALIGN - 1));
413 /* initialize rest of io_req */
414 io_req->port_id = rport->port_id;
415 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
416 CMD_SP(sc) = (char *)io_req;
417 sc->scsi_done = done;
419 /* create copy wq desc and enqueue it */
420 wq = &fnic->wq_copy[0];
421 ret = fnic_queue_wq_copy_desc(fnic, wq, io_req, sc, sg_count);
424 * In case another thread cancelled the request,
425 * refetch the pointer under the lock.
427 spinlock_t *io_lock = fnic_io_lock_hash(fnic, sc);
429 spin_lock_irqsave(io_lock, flags);
430 io_req = (struct fnic_io_req *)CMD_SP(sc);
432 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
433 spin_unlock_irqrestore(io_lock, flags);
435 fnic_release_ioreq_buf(fnic, io_req, sc);
436 mempool_free(io_req, fnic->io_req_pool);
440 /* acquire host lock before returning to SCSI */
441 spin_lock(lp->host->host_lock);
446 * fnic_fcpio_fw_reset_cmpl_handler
447 * Routine to handle fw reset completion
449 static int fnic_fcpio_fw_reset_cmpl_handler(struct fnic *fnic,
450 struct fcpio_fw_req *desc)
454 struct fcpio_tag tag;
456 struct fc_frame *flogi;
459 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
461 /* Clean up all outstanding io requests */
462 fnic_cleanup_io(fnic, SCSI_NO_TAG);
464 spin_lock_irqsave(&fnic->fnic_lock, flags);
469 /* fnic should be in FC_TRANS_ETH_MODE */
470 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE) {
471 /* Check status of reset completion */
473 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
474 "reset cmpl success\n");
475 /* Ready to send flogi out */
476 fnic->state = FNIC_IN_ETH_MODE;
478 FNIC_SCSI_DBG(KERN_DEBUG,
480 "fnic fw_reset : failed %s\n",
481 fnic_fcpio_status_to_str(hdr_status));
484 * Unable to change to eth mode, cannot send out flogi
485 * Change state to fc mode, so that subsequent Flogi
486 * requests from libFC will cause more attempts to
487 * reset the firmware. Free the cached flogi
489 fnic->state = FNIC_IN_FC_MODE;
493 FNIC_SCSI_DBG(KERN_DEBUG,
495 "Unexpected state %s while processing"
496 " reset cmpl\n", fnic_state_to_str(fnic->state));
500 /* Thread removing device blocks till firmware reset is complete */
501 if (fnic->remove_wait)
502 complete(fnic->remove_wait);
505 * If fnic is being removed, or fw reset failed
506 * free the flogi frame. Else, send it out
508 if (fnic->remove_wait || ret) {
509 fnic->flogi_oxid = FC_XID_UNKNOWN;
510 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
512 dev_kfree_skb_irq(fp_skb(flogi));
513 goto reset_cmpl_handler_end;
516 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
519 ret = fnic_send_frame(fnic, flogi);
521 reset_cmpl_handler_end:
526 * fnic_fcpio_flogi_reg_cmpl_handler
527 * Routine to handle flogi register completion
529 static int fnic_fcpio_flogi_reg_cmpl_handler(struct fnic *fnic,
530 struct fcpio_fw_req *desc)
534 struct fcpio_tag tag;
536 struct fc_frame *flogi_resp = NULL;
540 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
542 /* Update fnic state based on status of flogi reg completion */
543 spin_lock_irqsave(&fnic->fnic_lock, flags);
545 flogi_resp = fnic->flogi_resp;
546 fnic->flogi_resp = NULL;
548 if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE) {
550 /* Check flogi registration completion status */
552 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
553 "flog reg succeeded\n");
554 fnic->state = FNIC_IN_FC_MODE;
556 FNIC_SCSI_DBG(KERN_DEBUG,
558 "fnic flogi reg :failed %s\n",
559 fnic_fcpio_status_to_str(hdr_status));
560 fnic->state = FNIC_IN_ETH_MODE;
564 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
565 "Unexpected fnic state %s while"
566 " processing flogi reg completion\n",
567 fnic_state_to_str(fnic->state));
571 /* Successful flogi reg cmpl, pass frame to LibFC */
572 if (!ret && flogi_resp) {
573 if (fnic->stop_rx_link_events) {
574 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
575 goto reg_cmpl_handler_end;
577 skb = (struct sk_buff *)flogi_resp;
578 /* Use fr_flags to indicate whether flogi resp or not */
579 fr_flags(flogi_resp) = 1;
580 fr_dev(flogi_resp) = fnic->lport;
581 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
583 skb_queue_tail(&fnic->frame_queue, skb);
584 queue_work(fnic_event_queue, &fnic->frame_work);
587 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
589 dev_kfree_skb_irq(fp_skb(flogi_resp));
592 reg_cmpl_handler_end:
596 static inline int is_ack_index_in_range(struct vnic_wq_copy *wq,
599 if (wq->to_clean_index <= wq->to_use_index) {
600 /* out of range, stale request_out index */
601 if (request_out < wq->to_clean_index ||
602 request_out >= wq->to_use_index)
605 /* out of range, stale request_out index */
606 if (request_out < wq->to_clean_index &&
607 request_out >= wq->to_use_index)
610 /* request_out index is in range */
616 * Mark that ack received and store the Ack index. If there are multiple
617 * acks received before Tx thread cleans it up, the latest value will be
618 * used which is correct behavior. This state should be in the copy Wq
619 * instead of in the fnic
621 static inline void fnic_fcpio_ack_handler(struct fnic *fnic,
622 unsigned int cq_index,
623 struct fcpio_fw_req *desc)
625 struct vnic_wq_copy *wq;
626 u16 request_out = desc->u.ack.request_out;
629 /* mark the ack state */
630 wq = &fnic->wq_copy[cq_index - fnic->raw_wq_count - fnic->rq_count];
631 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
633 if (is_ack_index_in_range(wq, request_out)) {
634 fnic->fw_ack_index[0] = request_out;
635 fnic->fw_ack_recd[0] = 1;
637 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
641 * fnic_fcpio_icmnd_cmpl_handler
642 * Routine to handle icmnd completions
644 static void fnic_fcpio_icmnd_cmpl_handler(struct fnic *fnic,
645 struct fcpio_fw_req *desc)
649 struct fcpio_tag tag;
652 struct fcpio_icmnd_cmpl *icmnd_cmpl;
653 struct fnic_io_req *io_req;
654 struct scsi_cmnd *sc;
658 /* Decode the cmpl description to get the io_req id */
659 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
660 fcpio_tag_id_dec(&tag, &id);
662 if (id >= FNIC_MAX_IO_REQ)
665 sc = scsi_host_find_tag(fnic->lport->host, id);
670 io_lock = fnic_io_lock_hash(fnic, sc);
671 spin_lock_irqsave(io_lock, flags);
672 io_req = (struct fnic_io_req *)CMD_SP(sc);
673 WARN_ON_ONCE(!io_req);
675 spin_unlock_irqrestore(io_lock, flags);
679 /* firmware completed the io */
680 io_req->io_completed = 1;
683 * if SCSI-ML has already issued abort on this command,
684 * ignore completion of the IO. The abts path will clean it up
686 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
687 spin_unlock_irqrestore(io_lock, flags);
691 /* Mark the IO as complete */
692 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
694 icmnd_cmpl = &desc->u.icmnd_cmpl;
696 switch (hdr_status) {
698 sc->result = (DID_OK << 16) | icmnd_cmpl->scsi_status;
699 xfer_len = scsi_bufflen(sc);
700 scsi_set_resid(sc, icmnd_cmpl->residual);
702 if (icmnd_cmpl->flags & FCPIO_ICMND_CMPL_RESID_UNDER)
703 xfer_len -= icmnd_cmpl->residual;
706 * If queue_full, then try to reduce queue depth for all
707 * LUNS on the target. Todo: this should be accompanied
708 * by a periodic queue_depth rampup based on successful
711 if (icmnd_cmpl->scsi_status == QUEUE_FULL) {
712 struct scsi_device *t_sdev;
715 shost_for_each_device(t_sdev, sc->device->host) {
716 if (t_sdev->id != sc->device->id)
719 if (t_sdev->queue_depth > 1) {
720 qd = scsi_track_queue_full
722 t_sdev->queue_depth - 1);
724 qd = t_sdev->host->cmd_per_lun;
725 shost_printk(KERN_INFO,
728 "] queue full detected,"
730 t_sdev->host->host_no,
732 t_sdev->id, t_sdev->lun,
733 t_sdev->queue_depth);
739 case FCPIO_TIMEOUT: /* request was timed out */
740 sc->result = (DID_TIME_OUT << 16) | icmnd_cmpl->scsi_status;
743 case FCPIO_ABORTED: /* request was aborted */
744 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
747 case FCPIO_DATA_CNT_MISMATCH: /* recv/sent more/less data than exp. */
748 scsi_set_resid(sc, icmnd_cmpl->residual);
749 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
752 case FCPIO_OUT_OF_RESOURCE: /* out of resources to complete request */
753 sc->result = (DID_REQUEUE << 16) | icmnd_cmpl->scsi_status;
755 case FCPIO_INVALID_HEADER: /* header contains invalid data */
756 case FCPIO_INVALID_PARAM: /* some parameter in request invalid */
757 case FCPIO_REQ_NOT_SUPPORTED:/* request type is not supported */
758 case FCPIO_IO_NOT_FOUND: /* requested I/O was not found */
759 case FCPIO_SGL_INVALID: /* request was aborted due to sgl error */
760 case FCPIO_MSS_INVALID: /* request was aborted due to mss error */
761 case FCPIO_FW_ERR: /* request was terminated due fw error */
763 shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
764 fnic_fcpio_status_to_str(hdr_status));
765 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
769 /* Break link with the SCSI command */
772 spin_unlock_irqrestore(io_lock, flags);
774 fnic_release_ioreq_buf(fnic, io_req, sc);
776 mempool_free(io_req, fnic->io_req_pool);
778 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
779 fnic->lport->host_stats.fcp_input_requests++;
780 fnic->fcp_input_bytes += xfer_len;
781 } else if (sc->sc_data_direction == DMA_TO_DEVICE) {
782 fnic->lport->host_stats.fcp_output_requests++;
783 fnic->fcp_output_bytes += xfer_len;
785 fnic->lport->host_stats.fcp_control_requests++;
787 /* Call SCSI completion function to complete the IO */
793 /* fnic_fcpio_itmf_cmpl_handler
794 * Routine to handle itmf completions
796 static void fnic_fcpio_itmf_cmpl_handler(struct fnic *fnic,
797 struct fcpio_fw_req *desc)
801 struct fcpio_tag tag;
803 struct scsi_cmnd *sc;
804 struct fnic_io_req *io_req;
808 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
809 fcpio_tag_id_dec(&tag, &id);
811 if ((id & FNIC_TAG_MASK) >= FNIC_MAX_IO_REQ)
814 sc = scsi_host_find_tag(fnic->lport->host, id & FNIC_TAG_MASK);
819 io_lock = fnic_io_lock_hash(fnic, sc);
820 spin_lock_irqsave(io_lock, flags);
821 io_req = (struct fnic_io_req *)CMD_SP(sc);
822 WARN_ON_ONCE(!io_req);
824 spin_unlock_irqrestore(io_lock, flags);
828 if (id & FNIC_TAG_ABORT) {
829 /* Completion of abort cmd */
830 if (CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING) {
831 /* This is a late completion. Ignore it */
832 spin_unlock_irqrestore(io_lock, flags);
835 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
836 CMD_ABTS_STATUS(sc) = hdr_status;
838 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
839 "abts cmpl recd. id %d status %s\n",
840 (int)(id & FNIC_TAG_MASK),
841 fnic_fcpio_status_to_str(hdr_status));
844 * If scsi_eh thread is blocked waiting for abts to complete,
845 * signal completion to it. IO will be cleaned in the thread
846 * else clean it in this context
848 if (io_req->abts_done) {
849 complete(io_req->abts_done);
850 spin_unlock_irqrestore(io_lock, flags);
852 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
853 "abts cmpl, completing IO\n");
855 sc->result = (DID_ERROR << 16);
857 spin_unlock_irqrestore(io_lock, flags);
859 fnic_release_ioreq_buf(fnic, io_req, sc);
860 mempool_free(io_req, fnic->io_req_pool);
865 } else if (id & FNIC_TAG_DEV_RST) {
866 /* Completion of device reset */
867 CMD_LR_STATUS(sc) = hdr_status;
868 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
869 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
870 "dev reset cmpl recd. id %d status %s\n",
871 (int)(id & FNIC_TAG_MASK),
872 fnic_fcpio_status_to_str(hdr_status));
874 complete(io_req->dr_done);
875 spin_unlock_irqrestore(io_lock, flags);
878 shost_printk(KERN_ERR, fnic->lport->host,
879 "Unexpected itmf io state %s tag %x\n",
880 fnic_ioreq_state_to_str(CMD_STATE(sc)), id);
881 spin_unlock_irqrestore(io_lock, flags);
887 * fnic_fcpio_cmpl_handler
888 * Routine to service the cq for wq_copy
890 static int fnic_fcpio_cmpl_handler(struct vnic_dev *vdev,
891 unsigned int cq_index,
892 struct fcpio_fw_req *desc)
894 struct fnic *fnic = vnic_dev_priv(vdev);
897 switch (desc->hdr.type) {
898 case FCPIO_ACK: /* fw copied copy wq desc to its queue */
899 fnic_fcpio_ack_handler(fnic, cq_index, desc);
902 case FCPIO_ICMND_CMPL: /* fw completed a command */
903 fnic_fcpio_icmnd_cmpl_handler(fnic, desc);
906 case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
907 fnic_fcpio_itmf_cmpl_handler(fnic, desc);
910 case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
911 ret = fnic_fcpio_flogi_reg_cmpl_handler(fnic, desc);
914 case FCPIO_RESET_CMPL: /* fw completed reset */
915 ret = fnic_fcpio_fw_reset_cmpl_handler(fnic, desc);
919 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
920 "firmware completion type %d\n",
929 * fnic_wq_copy_cmpl_handler
930 * Routine to process wq copy
932 int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int copy_work_to_do)
934 unsigned int wq_work_done = 0;
935 unsigned int i, cq_index;
936 unsigned int cur_work_done;
938 for (i = 0; i < fnic->wq_copy_count; i++) {
939 cq_index = i + fnic->raw_wq_count + fnic->rq_count;
940 cur_work_done = vnic_cq_copy_service(&fnic->cq[cq_index],
941 fnic_fcpio_cmpl_handler,
943 wq_work_done += cur_work_done;
948 static void fnic_cleanup_io(struct fnic *fnic, int exclude_id)
951 struct fnic_io_req *io_req;
952 unsigned long flags = 0;
953 struct scsi_cmnd *sc;
956 for (i = 0; i < FNIC_MAX_IO_REQ; i++) {
960 sc = scsi_host_find_tag(fnic->lport->host, i);
964 io_lock = fnic_io_lock_hash(fnic, sc);
965 spin_lock_irqsave(io_lock, flags);
966 io_req = (struct fnic_io_req *)CMD_SP(sc);
968 spin_unlock_irqrestore(io_lock, flags);
969 goto cleanup_scsi_cmd;
974 spin_unlock_irqrestore(io_lock, flags);
977 * If there is a scsi_cmnd associated with this io_req, then
978 * free the corresponding state
980 fnic_release_ioreq_buf(fnic, io_req, sc);
981 mempool_free(io_req, fnic->io_req_pool);
984 sc->result = DID_TRANSPORT_DISRUPTED << 16;
985 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "fnic_cleanup_io:"
986 " DID_TRANSPORT_DISRUPTED\n");
988 /* Complete the command to SCSI */
994 void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
995 struct fcpio_host_req *desc)
998 struct fnic *fnic = vnic_dev_priv(wq->vdev);
999 struct fnic_io_req *io_req;
1000 struct scsi_cmnd *sc;
1001 unsigned long flags;
1002 spinlock_t *io_lock;
1004 /* get the tag reference */
1005 fcpio_tag_id_dec(&desc->hdr.tag, &id);
1006 id &= FNIC_TAG_MASK;
1008 if (id >= FNIC_MAX_IO_REQ)
1011 sc = scsi_host_find_tag(fnic->lport->host, id);
1015 io_lock = fnic_io_lock_hash(fnic, sc);
1016 spin_lock_irqsave(io_lock, flags);
1018 /* Get the IO context which this desc refers to */
1019 io_req = (struct fnic_io_req *)CMD_SP(sc);
1021 /* fnic interrupts are turned off by now */
1024 spin_unlock_irqrestore(io_lock, flags);
1025 goto wq_copy_cleanup_scsi_cmd;
1030 spin_unlock_irqrestore(io_lock, flags);
1032 fnic_release_ioreq_buf(fnic, io_req, sc);
1033 mempool_free(io_req, fnic->io_req_pool);
1035 wq_copy_cleanup_scsi_cmd:
1036 sc->result = DID_NO_CONNECT << 16;
1037 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "wq_copy_cleanup_handler:"
1038 " DID_NO_CONNECT\n");
1044 static inline int fnic_queue_abort_io_req(struct fnic *fnic, int tag,
1045 u32 task_req, u8 *fc_lun,
1046 struct fnic_io_req *io_req)
1048 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
1049 unsigned long flags;
1051 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
1053 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1054 free_wq_copy_descs(fnic, wq);
1056 if (!vnic_wq_copy_desc_avail(wq)) {
1057 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
1060 fnic_queue_wq_copy_desc_itmf(wq, tag | FNIC_TAG_ABORT,
1061 0, task_req, tag, fc_lun, io_req->port_id,
1062 fnic->config.ra_tov, fnic->config.ed_tov);
1064 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
1068 void fnic_rport_exch_reset(struct fnic *fnic, u32 port_id)
1071 struct fnic_io_req *io_req;
1072 spinlock_t *io_lock;
1073 unsigned long flags;
1074 struct scsi_cmnd *sc;
1075 struct scsi_lun fc_lun;
1076 enum fnic_ioreq_state old_ioreq_state;
1078 FNIC_SCSI_DBG(KERN_DEBUG,
1080 "fnic_rport_reset_exch called portid 0x%06x\n",
1083 if (fnic->in_remove)
1086 for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
1087 sc = scsi_host_find_tag(fnic->lport->host, tag);
1091 io_lock = fnic_io_lock_hash(fnic, sc);
1092 spin_lock_irqsave(io_lock, flags);
1094 io_req = (struct fnic_io_req *)CMD_SP(sc);
1096 if (!io_req || io_req->port_id != port_id) {
1097 spin_unlock_irqrestore(io_lock, flags);
1102 * Found IO that is still pending with firmware and
1103 * belongs to rport that went away
1105 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1106 spin_unlock_irqrestore(io_lock, flags);
1109 old_ioreq_state = CMD_STATE(sc);
1110 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1111 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1113 BUG_ON(io_req->abts_done);
1115 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1116 "fnic_rport_reset_exch: Issuing abts\n");
1118 spin_unlock_irqrestore(io_lock, flags);
1120 /* Now queue the abort command to firmware */
1121 int_to_scsilun(sc->device->lun, &fc_lun);
1123 if (fnic_queue_abort_io_req(fnic, tag,
1124 FCPIO_ITMF_ABT_TASK_TERM,
1125 fc_lun.scsi_lun, io_req)) {
1127 * Revert the cmd state back to old state, if
1128 * it hasnt changed in between. This cmd will get
1129 * aborted later by scsi_eh, or cleaned up during
1132 io_lock = fnic_io_lock_hash(fnic, sc);
1134 spin_lock_irqsave(io_lock, flags);
1135 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1136 CMD_STATE(sc) = old_ioreq_state;
1137 spin_unlock_irqrestore(io_lock, flags);
1143 void fnic_terminate_rport_io(struct fc_rport *rport)
1146 struct fnic_io_req *io_req;
1147 spinlock_t *io_lock;
1148 unsigned long flags;
1149 struct scsi_cmnd *sc;
1150 struct scsi_lun fc_lun;
1151 struct fc_rport_libfc_priv *rdata = rport->dd_data;
1152 struct fc_lport *lport = rdata->local_port;
1153 struct fnic *fnic = lport_priv(lport);
1154 struct fc_rport *cmd_rport;
1155 enum fnic_ioreq_state old_ioreq_state;
1157 FNIC_SCSI_DBG(KERN_DEBUG,
1158 fnic->lport->host, "fnic_terminate_rport_io called"
1159 " wwpn 0x%llx, wwnn0x%llx, portid 0x%06x\n",
1160 rport->port_name, rport->node_name,
1163 if (fnic->in_remove)
1166 for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
1167 sc = scsi_host_find_tag(fnic->lport->host, tag);
1171 cmd_rport = starget_to_rport(scsi_target(sc->device));
1172 if (rport != cmd_rport)
1175 io_lock = fnic_io_lock_hash(fnic, sc);
1176 spin_lock_irqsave(io_lock, flags);
1178 io_req = (struct fnic_io_req *)CMD_SP(sc);
1180 if (!io_req || rport != cmd_rport) {
1181 spin_unlock_irqrestore(io_lock, flags);
1186 * Found IO that is still pending with firmware and
1187 * belongs to rport that went away
1189 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1190 spin_unlock_irqrestore(io_lock, flags);
1193 old_ioreq_state = CMD_STATE(sc);
1194 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1195 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1197 BUG_ON(io_req->abts_done);
1199 FNIC_SCSI_DBG(KERN_DEBUG,
1201 "fnic_terminate_rport_io: Issuing abts\n");
1203 spin_unlock_irqrestore(io_lock, flags);
1205 /* Now queue the abort command to firmware */
1206 int_to_scsilun(sc->device->lun, &fc_lun);
1208 if (fnic_queue_abort_io_req(fnic, tag,
1209 FCPIO_ITMF_ABT_TASK_TERM,
1210 fc_lun.scsi_lun, io_req)) {
1212 * Revert the cmd state back to old state, if
1213 * it hasnt changed in between. This cmd will get
1214 * aborted later by scsi_eh, or cleaned up during
1217 io_lock = fnic_io_lock_hash(fnic, sc);
1219 spin_lock_irqsave(io_lock, flags);
1220 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1221 CMD_STATE(sc) = old_ioreq_state;
1222 spin_unlock_irqrestore(io_lock, flags);
1228 static void fnic_block_error_handler(struct scsi_cmnd *sc)
1230 struct Scsi_Host *shost = sc->device->host;
1231 struct fc_rport *rport = starget_to_rport(scsi_target(sc->device));
1232 unsigned long flags;
1234 spin_lock_irqsave(shost->host_lock, flags);
1235 while (rport->port_state == FC_PORTSTATE_BLOCKED) {
1236 spin_unlock_irqrestore(shost->host_lock, flags);
1238 spin_lock_irqsave(shost->host_lock, flags);
1240 spin_unlock_irqrestore(shost->host_lock, flags);
1245 * This function is exported to SCSI for sending abort cmnds.
1246 * A SCSI IO is represented by a io_req in the driver.
1247 * The ioreq is linked to the SCSI Cmd, thus a link with the ULP's IO.
1249 int fnic_abort_cmd(struct scsi_cmnd *sc)
1251 struct fc_lport *lp;
1253 struct fnic_io_req *io_req;
1254 struct fc_rport *rport;
1255 spinlock_t *io_lock;
1256 unsigned long flags;
1259 struct scsi_lun fc_lun;
1260 DECLARE_COMPLETION_ONSTACK(tm_done);
1262 /* Wait for rport to unblock */
1263 fnic_block_error_handler(sc);
1265 /* Get local-port, check ready and link up */
1266 lp = shost_priv(sc->device->host);
1268 fnic = lport_priv(lp);
1269 FNIC_SCSI_DBG(KERN_DEBUG,
1271 "Abort Cmd called FCID 0x%x, LUN 0x%x TAG %d\n",
1272 (starget_to_rport(scsi_target(sc->device)))->port_id,
1273 sc->device->lun, sc->request->tag);
1275 if (lp->state != LPORT_ST_READY || !(lp->link_up)) {
1277 goto fnic_abort_cmd_end;
1281 * Avoid a race between SCSI issuing the abort and the device
1282 * completing the command.
1284 * If the command is already completed by the fw cmpl code,
1285 * we just return SUCCESS from here. This means that the abort
1286 * succeeded. In the SCSI ML, since the timeout for command has
1287 * happened, the completion wont actually complete the command
1288 * and it will be considered as an aborted command
1290 * The CMD_SP will not be cleared except while holding io_req_lock.
1292 io_lock = fnic_io_lock_hash(fnic, sc);
1293 spin_lock_irqsave(io_lock, flags);
1294 io_req = (struct fnic_io_req *)CMD_SP(sc);
1296 spin_unlock_irqrestore(io_lock, flags);
1297 goto fnic_abort_cmd_end;
1300 io_req->abts_done = &tm_done;
1302 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1303 spin_unlock_irqrestore(io_lock, flags);
1307 * Command is still pending, need to abort it
1308 * If the firmware completes the command after this point,
1309 * the completion wont be done till mid-layer, since abort
1310 * has already started.
1312 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1313 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1315 spin_unlock_irqrestore(io_lock, flags);
1318 * Check readiness of the remote port. If the path to remote
1319 * port is up, then send abts to the remote port to terminate
1320 * the IO. Else, just locally terminate the IO in the firmware
1322 rport = starget_to_rport(scsi_target(sc->device));
1323 if (fc_remote_port_chkready(rport) == 0)
1324 task_req = FCPIO_ITMF_ABT_TASK;
1326 task_req = FCPIO_ITMF_ABT_TASK_TERM;
1328 /* Now queue the abort command to firmware */
1329 int_to_scsilun(sc->device->lun, &fc_lun);
1331 if (fnic_queue_abort_io_req(fnic, sc->request->tag, task_req,
1332 fc_lun.scsi_lun, io_req)) {
1333 spin_lock_irqsave(io_lock, flags);
1334 io_req = (struct fnic_io_req *)CMD_SP(sc);
1336 io_req->abts_done = NULL;
1337 spin_unlock_irqrestore(io_lock, flags);
1339 goto fnic_abort_cmd_end;
1343 * We queued an abort IO, wait for its completion.
1344 * Once the firmware completes the abort command, it will
1345 * wake up this thread.
1348 wait_for_completion_timeout(&tm_done,
1350 (2 * fnic->config.ra_tov +
1351 fnic->config.ed_tov));
1353 /* Check the abort status */
1354 spin_lock_irqsave(io_lock, flags);
1356 io_req = (struct fnic_io_req *)CMD_SP(sc);
1358 spin_unlock_irqrestore(io_lock, flags);
1360 goto fnic_abort_cmd_end;
1362 io_req->abts_done = NULL;
1364 /* fw did not complete abort, timed out */
1365 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1366 spin_unlock_irqrestore(io_lock, flags);
1368 goto fnic_abort_cmd_end;
1372 * firmware completed the abort, check the status,
1373 * free the io_req irrespective of failure or success
1375 if (CMD_ABTS_STATUS(sc) != FCPIO_SUCCESS)
1380 spin_unlock_irqrestore(io_lock, flags);
1382 fnic_release_ioreq_buf(fnic, io_req, sc);
1383 mempool_free(io_req, fnic->io_req_pool);
1386 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1387 "Returning from abort cmd %s\n",
1389 "SUCCESS" : "FAILED");
1393 static inline int fnic_queue_dr_io_req(struct fnic *fnic,
1394 struct scsi_cmnd *sc,
1395 struct fnic_io_req *io_req)
1397 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
1398 struct scsi_lun fc_lun;
1400 unsigned long intr_flags;
1402 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
1404 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1405 free_wq_copy_descs(fnic, wq);
1407 if (!vnic_wq_copy_desc_avail(wq)) {
1412 /* fill in the lun info */
1413 int_to_scsilun(sc->device->lun, &fc_lun);
1415 fnic_queue_wq_copy_desc_itmf(wq, sc->request->tag | FNIC_TAG_DEV_RST,
1416 0, FCPIO_ITMF_LUN_RESET, SCSI_NO_TAG,
1417 fc_lun.scsi_lun, io_req->port_id,
1418 fnic->config.ra_tov, fnic->config.ed_tov);
1421 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
1427 * Clean up any pending aborts on the lun
1428 * For each outstanding IO on this lun, whose abort is not completed by fw,
1429 * issue a local abort. Wait for abort to complete. Return 0 if all commands
1430 * successfully aborted, 1 otherwise
1432 static int fnic_clean_pending_aborts(struct fnic *fnic,
1433 struct scsi_cmnd *lr_sc)
1436 struct fnic_io_req *io_req;
1437 spinlock_t *io_lock;
1438 unsigned long flags;
1440 struct scsi_cmnd *sc;
1441 struct fc_rport *rport;
1442 struct scsi_lun fc_lun;
1443 struct scsi_device *lun_dev = lr_sc->device;
1444 DECLARE_COMPLETION_ONSTACK(tm_done);
1446 for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
1447 sc = scsi_host_find_tag(fnic->lport->host, tag);
1449 * ignore this lun reset cmd or cmds that do not belong to
1452 if (!sc || sc == lr_sc || sc->device != lun_dev)
1455 io_lock = fnic_io_lock_hash(fnic, sc);
1456 spin_lock_irqsave(io_lock, flags);
1458 io_req = (struct fnic_io_req *)CMD_SP(sc);
1460 if (!io_req || sc->device != lun_dev) {
1461 spin_unlock_irqrestore(io_lock, flags);
1466 * Found IO that is still pending with firmware and
1467 * belongs to the LUN that we are resetting
1469 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1470 "Found IO in %s on lun\n",
1471 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1473 BUG_ON(CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING);
1475 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1476 io_req->abts_done = &tm_done;
1477 spin_unlock_irqrestore(io_lock, flags);
1479 /* Now queue the abort command to firmware */
1480 int_to_scsilun(sc->device->lun, &fc_lun);
1481 rport = starget_to_rport(scsi_target(sc->device));
1483 if (fnic_queue_abort_io_req(fnic, tag,
1484 FCPIO_ITMF_ABT_TASK_TERM,
1485 fc_lun.scsi_lun, io_req)) {
1486 spin_lock_irqsave(io_lock, flags);
1487 io_req = (struct fnic_io_req *)CMD_SP(sc);
1489 io_req->abts_done = NULL;
1490 spin_unlock_irqrestore(io_lock, flags);
1492 goto clean_pending_aborts_end;
1495 wait_for_completion_timeout(&tm_done,
1497 (fnic->config.ed_tov));
1499 /* Recheck cmd state to check if it is now aborted */
1500 spin_lock_irqsave(io_lock, flags);
1501 io_req = (struct fnic_io_req *)CMD_SP(sc);
1503 spin_unlock_irqrestore(io_lock, flags);
1505 goto clean_pending_aborts_end;
1508 io_req->abts_done = NULL;
1510 /* if abort is still pending with fw, fail */
1511 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1512 spin_unlock_irqrestore(io_lock, flags);
1514 goto clean_pending_aborts_end;
1517 spin_unlock_irqrestore(io_lock, flags);
1519 fnic_release_ioreq_buf(fnic, io_req, sc);
1520 mempool_free(io_req, fnic->io_req_pool);
1523 clean_pending_aborts_end:
1528 * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
1529 * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
1532 int fnic_device_reset(struct scsi_cmnd *sc)
1534 struct fc_lport *lp;
1536 struct fnic_io_req *io_req;
1537 struct fc_rport *rport;
1540 spinlock_t *io_lock;
1541 unsigned long flags;
1542 DECLARE_COMPLETION_ONSTACK(tm_done);
1544 /* Wait for rport to unblock */
1545 fnic_block_error_handler(sc);
1547 /* Get local-port, check ready and link up */
1548 lp = shost_priv(sc->device->host);
1550 fnic = lport_priv(lp);
1551 FNIC_SCSI_DBG(KERN_DEBUG,
1553 "Device reset called FCID 0x%x, LUN 0x%x\n",
1554 (starget_to_rport(scsi_target(sc->device)))->port_id,
1558 if (lp->state != LPORT_ST_READY || !(lp->link_up))
1559 goto fnic_device_reset_end;
1561 /* Check if remote port up */
1562 rport = starget_to_rport(scsi_target(sc->device));
1563 if (fc_remote_port_chkready(rport))
1564 goto fnic_device_reset_end;
1566 io_lock = fnic_io_lock_hash(fnic, sc);
1567 spin_lock_irqsave(io_lock, flags);
1568 io_req = (struct fnic_io_req *)CMD_SP(sc);
1571 * If there is a io_req attached to this command, then use it,
1572 * else allocate a new one.
1575 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
1577 spin_unlock_irqrestore(io_lock, flags);
1578 goto fnic_device_reset_end;
1580 memset(io_req, 0, sizeof(*io_req));
1581 io_req->port_id = rport->port_id;
1582 CMD_SP(sc) = (char *)io_req;
1584 io_req->dr_done = &tm_done;
1585 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
1586 CMD_LR_STATUS(sc) = FCPIO_INVALID_CODE;
1587 spin_unlock_irqrestore(io_lock, flags);
1589 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %d\n",
1593 * issue the device reset, if enqueue failed, clean up the ioreq
1594 * and break assoc with scsi cmd
1596 if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
1597 spin_lock_irqsave(io_lock, flags);
1598 io_req = (struct fnic_io_req *)CMD_SP(sc);
1600 io_req->dr_done = NULL;
1601 goto fnic_device_reset_clean;
1605 * Wait on the local completion for LUN reset. The io_req may be
1606 * freed while we wait since we hold no lock.
1608 wait_for_completion_timeout(&tm_done,
1609 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
1611 spin_lock_irqsave(io_lock, flags);
1612 io_req = (struct fnic_io_req *)CMD_SP(sc);
1614 spin_unlock_irqrestore(io_lock, flags);
1615 goto fnic_device_reset_end;
1617 io_req->dr_done = NULL;
1619 status = CMD_LR_STATUS(sc);
1620 spin_unlock_irqrestore(io_lock, flags);
1623 * If lun reset not completed, bail out with failed. io_req
1624 * gets cleaned up during higher levels of EH
1626 if (status == FCPIO_INVALID_CODE) {
1627 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1628 "Device reset timed out\n");
1629 goto fnic_device_reset_end;
1632 /* Completed, but not successful, clean up the io_req, return fail */
1633 if (status != FCPIO_SUCCESS) {
1634 spin_lock_irqsave(io_lock, flags);
1635 FNIC_SCSI_DBG(KERN_DEBUG,
1637 "Device reset completed - failed\n");
1638 io_req = (struct fnic_io_req *)CMD_SP(sc);
1639 goto fnic_device_reset_clean;
1643 * Clean up any aborts on this lun that have still not
1644 * completed. If any of these fail, then LUN reset fails.
1645 * clean_pending_aborts cleans all cmds on this lun except
1646 * the lun reset cmd. If all cmds get cleaned, the lun reset
1649 if (fnic_clean_pending_aborts(fnic, sc)) {
1650 spin_lock_irqsave(io_lock, flags);
1651 io_req = (struct fnic_io_req *)CMD_SP(sc);
1652 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1653 "Device reset failed"
1654 " since could not abort all IOs\n");
1655 goto fnic_device_reset_clean;
1658 /* Clean lun reset command */
1659 spin_lock_irqsave(io_lock, flags);
1660 io_req = (struct fnic_io_req *)CMD_SP(sc);
1662 /* Completed, and successful */
1665 fnic_device_reset_clean:
1669 spin_unlock_irqrestore(io_lock, flags);
1672 fnic_release_ioreq_buf(fnic, io_req, sc);
1673 mempool_free(io_req, fnic->io_req_pool);
1676 fnic_device_reset_end:
1677 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1678 "Returning from device reset %s\n",
1680 "SUCCESS" : "FAILED");
1684 /* Clean up all IOs, clean up libFC local port */
1685 int fnic_reset(struct Scsi_Host *shost)
1687 struct fc_lport *lp;
1691 lp = shost_priv(shost);
1692 fnic = lport_priv(lp);
1694 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1695 "fnic_reset called\n");
1698 * Reset local port, this will clean up libFC exchanges,
1699 * reset remote port sessions, and if link is up, begin flogi
1701 if (lp->tt.lport_reset(lp))
1704 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1705 "Returning from fnic reset %s\n",
1707 "SUCCESS" : "FAILED");
1713 * SCSI Error handling calls driver's eh_host_reset if all prior
1714 * error handling levels return FAILED. If host reset completes
1715 * successfully, and if link is up, then Fabric login begins.
1717 * Host Reset is the highest level of error recovery. If this fails, then
1718 * host is offlined by SCSI.
1721 int fnic_host_reset(struct scsi_cmnd *sc)
1724 unsigned long wait_host_tmo;
1725 struct Scsi_Host *shost = sc->device->host;
1726 struct fc_lport *lp = shost_priv(shost);
1729 * If fnic_reset is successful, wait for fabric login to complete
1730 * scsi-ml tries to send a TUR to every device if host reset is
1731 * successful, so before returning to scsi, fabric should be up
1733 ret = fnic_reset(shost);
1734 if (ret == SUCCESS) {
1735 wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
1737 while (time_before(jiffies, wait_host_tmo)) {
1738 if ((lp->state == LPORT_ST_READY) &&
1751 * This fxn is called from libFC when host is removed
1753 void fnic_scsi_abort_io(struct fc_lport *lp)
1756 unsigned long flags;
1757 enum fnic_state old_state;
1758 struct fnic *fnic = lport_priv(lp);
1759 DECLARE_COMPLETION_ONSTACK(remove_wait);
1761 /* Issue firmware reset for fnic, wait for reset to complete */
1762 spin_lock_irqsave(&fnic->fnic_lock, flags);
1763 fnic->remove_wait = &remove_wait;
1764 old_state = fnic->state;
1765 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
1766 vnic_dev_del_addr(fnic->vdev, fnic->data_src_addr);
1767 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1769 err = fnic_fw_reset_handler(fnic);
1771 spin_lock_irqsave(&fnic->fnic_lock, flags);
1772 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
1773 fnic->state = old_state;
1774 fnic->remove_wait = NULL;
1775 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1779 /* Wait for firmware reset to complete */
1780 wait_for_completion_timeout(&remove_wait,
1781 msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
1783 spin_lock_irqsave(&fnic->fnic_lock, flags);
1784 fnic->remove_wait = NULL;
1785 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1786 "fnic_scsi_abort_io %s\n",
1787 (fnic->state == FNIC_IN_ETH_MODE) ?
1788 "SUCCESS" : "FAILED");
1789 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1794 * This fxn called from libFC to clean up driver IO state on link down
1796 void fnic_scsi_cleanup(struct fc_lport *lp)
1798 unsigned long flags;
1799 enum fnic_state old_state;
1800 struct fnic *fnic = lport_priv(lp);
1802 /* issue fw reset */
1803 spin_lock_irqsave(&fnic->fnic_lock, flags);
1804 old_state = fnic->state;
1805 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
1806 vnic_dev_del_addr(fnic->vdev, fnic->data_src_addr);
1807 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1809 if (fnic_fw_reset_handler(fnic)) {
1810 spin_lock_irqsave(&fnic->fnic_lock, flags);
1811 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
1812 fnic->state = old_state;
1813 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1818 void fnic_empty_scsi_cleanup(struct fc_lport *lp)
1822 void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
1824 struct fnic *fnic = lport_priv(lp);
1826 /* Non-zero sid, nothing to do */
1828 goto call_fc_exch_mgr_reset;
1831 fnic_rport_exch_reset(fnic, did);
1832 goto call_fc_exch_mgr_reset;
1837 * link down or device being removed
1839 if (!fnic->in_remove)
1840 fnic_scsi_cleanup(lp);
1842 fnic_scsi_abort_io(lp);
1844 /* call libFC exch mgr reset to reset its exchanges */
1845 call_fc_exch_mgr_reset:
1846 fc_exch_mgr_reset(lp, sid, did);