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);
1229 * This function is exported to SCSI for sending abort cmnds.
1230 * A SCSI IO is represented by a io_req in the driver.
1231 * The ioreq is linked to the SCSI Cmd, thus a link with the ULP's IO.
1233 int fnic_abort_cmd(struct scsi_cmnd *sc)
1235 struct fc_lport *lp;
1237 struct fnic_io_req *io_req;
1238 struct fc_rport *rport;
1239 spinlock_t *io_lock;
1240 unsigned long flags;
1243 struct scsi_lun fc_lun;
1244 DECLARE_COMPLETION_ONSTACK(tm_done);
1246 /* Wait for rport to unblock */
1247 fc_block_scsi_eh(sc);
1249 /* Get local-port, check ready and link up */
1250 lp = shost_priv(sc->device->host);
1252 fnic = lport_priv(lp);
1253 FNIC_SCSI_DBG(KERN_DEBUG,
1255 "Abort Cmd called FCID 0x%x, LUN 0x%x TAG %d\n",
1256 (starget_to_rport(scsi_target(sc->device)))->port_id,
1257 sc->device->lun, sc->request->tag);
1259 if (lp->state != LPORT_ST_READY || !(lp->link_up)) {
1261 goto fnic_abort_cmd_end;
1265 * Avoid a race between SCSI issuing the abort and the device
1266 * completing the command.
1268 * If the command is already completed by the fw cmpl code,
1269 * we just return SUCCESS from here. This means that the abort
1270 * succeeded. In the SCSI ML, since the timeout for command has
1271 * happened, the completion wont actually complete the command
1272 * and it will be considered as an aborted command
1274 * The CMD_SP will not be cleared except while holding io_req_lock.
1276 io_lock = fnic_io_lock_hash(fnic, sc);
1277 spin_lock_irqsave(io_lock, flags);
1278 io_req = (struct fnic_io_req *)CMD_SP(sc);
1280 spin_unlock_irqrestore(io_lock, flags);
1281 goto fnic_abort_cmd_end;
1284 io_req->abts_done = &tm_done;
1286 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1287 spin_unlock_irqrestore(io_lock, flags);
1291 * Command is still pending, need to abort it
1292 * If the firmware completes the command after this point,
1293 * the completion wont be done till mid-layer, since abort
1294 * has already started.
1296 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1297 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1299 spin_unlock_irqrestore(io_lock, flags);
1302 * Check readiness of the remote port. If the path to remote
1303 * port is up, then send abts to the remote port to terminate
1304 * the IO. Else, just locally terminate the IO in the firmware
1306 rport = starget_to_rport(scsi_target(sc->device));
1307 if (fc_remote_port_chkready(rport) == 0)
1308 task_req = FCPIO_ITMF_ABT_TASK;
1310 task_req = FCPIO_ITMF_ABT_TASK_TERM;
1312 /* Now queue the abort command to firmware */
1313 int_to_scsilun(sc->device->lun, &fc_lun);
1315 if (fnic_queue_abort_io_req(fnic, sc->request->tag, task_req,
1316 fc_lun.scsi_lun, io_req)) {
1317 spin_lock_irqsave(io_lock, flags);
1318 io_req = (struct fnic_io_req *)CMD_SP(sc);
1320 io_req->abts_done = NULL;
1321 spin_unlock_irqrestore(io_lock, flags);
1323 goto fnic_abort_cmd_end;
1327 * We queued an abort IO, wait for its completion.
1328 * Once the firmware completes the abort command, it will
1329 * wake up this thread.
1332 wait_for_completion_timeout(&tm_done,
1334 (2 * fnic->config.ra_tov +
1335 fnic->config.ed_tov));
1337 /* Check the abort status */
1338 spin_lock_irqsave(io_lock, flags);
1340 io_req = (struct fnic_io_req *)CMD_SP(sc);
1342 spin_unlock_irqrestore(io_lock, flags);
1344 goto fnic_abort_cmd_end;
1346 io_req->abts_done = NULL;
1348 /* fw did not complete abort, timed out */
1349 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1350 spin_unlock_irqrestore(io_lock, flags);
1352 goto fnic_abort_cmd_end;
1356 * firmware completed the abort, check the status,
1357 * free the io_req irrespective of failure or success
1359 if (CMD_ABTS_STATUS(sc) != FCPIO_SUCCESS)
1364 spin_unlock_irqrestore(io_lock, flags);
1366 fnic_release_ioreq_buf(fnic, io_req, sc);
1367 mempool_free(io_req, fnic->io_req_pool);
1370 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1371 "Returning from abort cmd %s\n",
1373 "SUCCESS" : "FAILED");
1377 static inline int fnic_queue_dr_io_req(struct fnic *fnic,
1378 struct scsi_cmnd *sc,
1379 struct fnic_io_req *io_req)
1381 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
1382 struct scsi_lun fc_lun;
1384 unsigned long intr_flags;
1386 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
1388 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1389 free_wq_copy_descs(fnic, wq);
1391 if (!vnic_wq_copy_desc_avail(wq)) {
1396 /* fill in the lun info */
1397 int_to_scsilun(sc->device->lun, &fc_lun);
1399 fnic_queue_wq_copy_desc_itmf(wq, sc->request->tag | FNIC_TAG_DEV_RST,
1400 0, FCPIO_ITMF_LUN_RESET, SCSI_NO_TAG,
1401 fc_lun.scsi_lun, io_req->port_id,
1402 fnic->config.ra_tov, fnic->config.ed_tov);
1405 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
1411 * Clean up any pending aborts on the lun
1412 * For each outstanding IO on this lun, whose abort is not completed by fw,
1413 * issue a local abort. Wait for abort to complete. Return 0 if all commands
1414 * successfully aborted, 1 otherwise
1416 static int fnic_clean_pending_aborts(struct fnic *fnic,
1417 struct scsi_cmnd *lr_sc)
1420 struct fnic_io_req *io_req;
1421 spinlock_t *io_lock;
1422 unsigned long flags;
1424 struct scsi_cmnd *sc;
1425 struct fc_rport *rport;
1426 struct scsi_lun fc_lun;
1427 struct scsi_device *lun_dev = lr_sc->device;
1428 DECLARE_COMPLETION_ONSTACK(tm_done);
1430 for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
1431 sc = scsi_host_find_tag(fnic->lport->host, tag);
1433 * ignore this lun reset cmd or cmds that do not belong to
1436 if (!sc || sc == lr_sc || sc->device != lun_dev)
1439 io_lock = fnic_io_lock_hash(fnic, sc);
1440 spin_lock_irqsave(io_lock, flags);
1442 io_req = (struct fnic_io_req *)CMD_SP(sc);
1444 if (!io_req || sc->device != lun_dev) {
1445 spin_unlock_irqrestore(io_lock, flags);
1450 * Found IO that is still pending with firmware and
1451 * belongs to the LUN that we are resetting
1453 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1454 "Found IO in %s on lun\n",
1455 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1457 BUG_ON(CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING);
1459 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1460 io_req->abts_done = &tm_done;
1461 spin_unlock_irqrestore(io_lock, flags);
1463 /* Now queue the abort command to firmware */
1464 int_to_scsilun(sc->device->lun, &fc_lun);
1465 rport = starget_to_rport(scsi_target(sc->device));
1467 if (fnic_queue_abort_io_req(fnic, tag,
1468 FCPIO_ITMF_ABT_TASK_TERM,
1469 fc_lun.scsi_lun, io_req)) {
1470 spin_lock_irqsave(io_lock, flags);
1471 io_req = (struct fnic_io_req *)CMD_SP(sc);
1473 io_req->abts_done = NULL;
1474 spin_unlock_irqrestore(io_lock, flags);
1476 goto clean_pending_aborts_end;
1479 wait_for_completion_timeout(&tm_done,
1481 (fnic->config.ed_tov));
1483 /* Recheck cmd state to check if it is now aborted */
1484 spin_lock_irqsave(io_lock, flags);
1485 io_req = (struct fnic_io_req *)CMD_SP(sc);
1487 spin_unlock_irqrestore(io_lock, flags);
1489 goto clean_pending_aborts_end;
1492 io_req->abts_done = NULL;
1494 /* if abort is still pending with fw, fail */
1495 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1496 spin_unlock_irqrestore(io_lock, flags);
1498 goto clean_pending_aborts_end;
1501 spin_unlock_irqrestore(io_lock, flags);
1503 fnic_release_ioreq_buf(fnic, io_req, sc);
1504 mempool_free(io_req, fnic->io_req_pool);
1507 clean_pending_aborts_end:
1512 * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
1513 * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
1516 int fnic_device_reset(struct scsi_cmnd *sc)
1518 struct fc_lport *lp;
1520 struct fnic_io_req *io_req;
1521 struct fc_rport *rport;
1524 spinlock_t *io_lock;
1525 unsigned long flags;
1526 DECLARE_COMPLETION_ONSTACK(tm_done);
1528 /* Wait for rport to unblock */
1529 fc_block_scsi_eh(sc);
1531 /* Get local-port, check ready and link up */
1532 lp = shost_priv(sc->device->host);
1534 fnic = lport_priv(lp);
1535 FNIC_SCSI_DBG(KERN_DEBUG,
1537 "Device reset called FCID 0x%x, LUN 0x%x\n",
1538 (starget_to_rport(scsi_target(sc->device)))->port_id,
1542 if (lp->state != LPORT_ST_READY || !(lp->link_up))
1543 goto fnic_device_reset_end;
1545 /* Check if remote port up */
1546 rport = starget_to_rport(scsi_target(sc->device));
1547 if (fc_remote_port_chkready(rport))
1548 goto fnic_device_reset_end;
1550 io_lock = fnic_io_lock_hash(fnic, sc);
1551 spin_lock_irqsave(io_lock, flags);
1552 io_req = (struct fnic_io_req *)CMD_SP(sc);
1555 * If there is a io_req attached to this command, then use it,
1556 * else allocate a new one.
1559 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
1561 spin_unlock_irqrestore(io_lock, flags);
1562 goto fnic_device_reset_end;
1564 memset(io_req, 0, sizeof(*io_req));
1565 io_req->port_id = rport->port_id;
1566 CMD_SP(sc) = (char *)io_req;
1568 io_req->dr_done = &tm_done;
1569 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
1570 CMD_LR_STATUS(sc) = FCPIO_INVALID_CODE;
1571 spin_unlock_irqrestore(io_lock, flags);
1573 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %d\n",
1577 * issue the device reset, if enqueue failed, clean up the ioreq
1578 * and break assoc with scsi cmd
1580 if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
1581 spin_lock_irqsave(io_lock, flags);
1582 io_req = (struct fnic_io_req *)CMD_SP(sc);
1584 io_req->dr_done = NULL;
1585 goto fnic_device_reset_clean;
1589 * Wait on the local completion for LUN reset. The io_req may be
1590 * freed while we wait since we hold no lock.
1592 wait_for_completion_timeout(&tm_done,
1593 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
1595 spin_lock_irqsave(io_lock, flags);
1596 io_req = (struct fnic_io_req *)CMD_SP(sc);
1598 spin_unlock_irqrestore(io_lock, flags);
1599 goto fnic_device_reset_end;
1601 io_req->dr_done = NULL;
1603 status = CMD_LR_STATUS(sc);
1604 spin_unlock_irqrestore(io_lock, flags);
1607 * If lun reset not completed, bail out with failed. io_req
1608 * gets cleaned up during higher levels of EH
1610 if (status == FCPIO_INVALID_CODE) {
1611 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1612 "Device reset timed out\n");
1613 goto fnic_device_reset_end;
1616 /* Completed, but not successful, clean up the io_req, return fail */
1617 if (status != FCPIO_SUCCESS) {
1618 spin_lock_irqsave(io_lock, flags);
1619 FNIC_SCSI_DBG(KERN_DEBUG,
1621 "Device reset completed - failed\n");
1622 io_req = (struct fnic_io_req *)CMD_SP(sc);
1623 goto fnic_device_reset_clean;
1627 * Clean up any aborts on this lun that have still not
1628 * completed. If any of these fail, then LUN reset fails.
1629 * clean_pending_aborts cleans all cmds on this lun except
1630 * the lun reset cmd. If all cmds get cleaned, the lun reset
1633 if (fnic_clean_pending_aborts(fnic, sc)) {
1634 spin_lock_irqsave(io_lock, flags);
1635 io_req = (struct fnic_io_req *)CMD_SP(sc);
1636 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1637 "Device reset failed"
1638 " since could not abort all IOs\n");
1639 goto fnic_device_reset_clean;
1642 /* Clean lun reset command */
1643 spin_lock_irqsave(io_lock, flags);
1644 io_req = (struct fnic_io_req *)CMD_SP(sc);
1646 /* Completed, and successful */
1649 fnic_device_reset_clean:
1653 spin_unlock_irqrestore(io_lock, flags);
1656 fnic_release_ioreq_buf(fnic, io_req, sc);
1657 mempool_free(io_req, fnic->io_req_pool);
1660 fnic_device_reset_end:
1661 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1662 "Returning from device reset %s\n",
1664 "SUCCESS" : "FAILED");
1668 /* Clean up all IOs, clean up libFC local port */
1669 int fnic_reset(struct Scsi_Host *shost)
1671 struct fc_lport *lp;
1675 lp = shost_priv(shost);
1676 fnic = lport_priv(lp);
1678 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1679 "fnic_reset called\n");
1682 * Reset local port, this will clean up libFC exchanges,
1683 * reset remote port sessions, and if link is up, begin flogi
1685 if (lp->tt.lport_reset(lp))
1688 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1689 "Returning from fnic reset %s\n",
1691 "SUCCESS" : "FAILED");
1697 * SCSI Error handling calls driver's eh_host_reset if all prior
1698 * error handling levels return FAILED. If host reset completes
1699 * successfully, and if link is up, then Fabric login begins.
1701 * Host Reset is the highest level of error recovery. If this fails, then
1702 * host is offlined by SCSI.
1705 int fnic_host_reset(struct scsi_cmnd *sc)
1708 unsigned long wait_host_tmo;
1709 struct Scsi_Host *shost = sc->device->host;
1710 struct fc_lport *lp = shost_priv(shost);
1713 * If fnic_reset is successful, wait for fabric login to complete
1714 * scsi-ml tries to send a TUR to every device if host reset is
1715 * successful, so before returning to scsi, fabric should be up
1717 ret = fnic_reset(shost);
1718 if (ret == SUCCESS) {
1719 wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
1721 while (time_before(jiffies, wait_host_tmo)) {
1722 if ((lp->state == LPORT_ST_READY) &&
1735 * This fxn is called from libFC when host is removed
1737 void fnic_scsi_abort_io(struct fc_lport *lp)
1740 unsigned long flags;
1741 enum fnic_state old_state;
1742 struct fnic *fnic = lport_priv(lp);
1743 DECLARE_COMPLETION_ONSTACK(remove_wait);
1745 /* Issue firmware reset for fnic, wait for reset to complete */
1746 spin_lock_irqsave(&fnic->fnic_lock, flags);
1747 fnic->remove_wait = &remove_wait;
1748 old_state = fnic->state;
1749 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
1750 vnic_dev_del_addr(fnic->vdev, fnic->data_src_addr);
1751 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1753 err = fnic_fw_reset_handler(fnic);
1755 spin_lock_irqsave(&fnic->fnic_lock, flags);
1756 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
1757 fnic->state = old_state;
1758 fnic->remove_wait = NULL;
1759 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1763 /* Wait for firmware reset to complete */
1764 wait_for_completion_timeout(&remove_wait,
1765 msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
1767 spin_lock_irqsave(&fnic->fnic_lock, flags);
1768 fnic->remove_wait = NULL;
1769 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1770 "fnic_scsi_abort_io %s\n",
1771 (fnic->state == FNIC_IN_ETH_MODE) ?
1772 "SUCCESS" : "FAILED");
1773 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1778 * This fxn called from libFC to clean up driver IO state on link down
1780 void fnic_scsi_cleanup(struct fc_lport *lp)
1782 unsigned long flags;
1783 enum fnic_state old_state;
1784 struct fnic *fnic = lport_priv(lp);
1786 /* issue fw reset */
1787 spin_lock_irqsave(&fnic->fnic_lock, flags);
1788 old_state = fnic->state;
1789 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
1790 vnic_dev_del_addr(fnic->vdev, fnic->data_src_addr);
1791 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1793 if (fnic_fw_reset_handler(fnic)) {
1794 spin_lock_irqsave(&fnic->fnic_lock, flags);
1795 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
1796 fnic->state = old_state;
1797 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1802 void fnic_empty_scsi_cleanup(struct fc_lport *lp)
1806 void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
1808 struct fnic *fnic = lport_priv(lp);
1810 /* Non-zero sid, nothing to do */
1812 goto call_fc_exch_mgr_reset;
1815 fnic_rport_exch_reset(fnic, did);
1816 goto call_fc_exch_mgr_reset;
1821 * link down or device being removed
1823 if (!fnic->in_remove)
1824 fnic_scsi_cleanup(lp);
1826 fnic_scsi_abort_io(lp);
1828 /* call libFC exch mgr reset to reset its exchanges */
1829 call_fc_exch_mgr_reset:
1830 fc_exch_mgr_reset(lp, sid, did);