• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2008 Cisco Systems, Inc.  All rights reserved.
3  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
4  *
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.
8  *
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
16  * SOFTWARE.
17  */
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 <linux/gfp.h>
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_device.h>
33 #include <scsi/scsi_cmnd.h>
34 #include <scsi/scsi_tcq.h>
35 #include <scsi/fc/fc_els.h>
36 #include <scsi/fc/fc_fcoe.h>
37 #include <scsi/libfc.h>
38 #include <scsi/fc_frame.h>
39 #include "fnic_io.h"
40 #include "fnic.h"
41 
42 const char *fnic_state_str[] = {
43 	[FNIC_IN_FC_MODE] =           "FNIC_IN_FC_MODE",
44 	[FNIC_IN_FC_TRANS_ETH_MODE] = "FNIC_IN_FC_TRANS_ETH_MODE",
45 	[FNIC_IN_ETH_MODE] =          "FNIC_IN_ETH_MODE",
46 	[FNIC_IN_ETH_TRANS_FC_MODE] = "FNIC_IN_ETH_TRANS_FC_MODE",
47 };
48 
49 static const char *fnic_ioreq_state_str[] = {
50 	[FNIC_IOREQ_NOT_INITED] = "FNIC_IOREQ_NOT_INITED",
51 	[FNIC_IOREQ_CMD_PENDING] = "FNIC_IOREQ_CMD_PENDING",
52 	[FNIC_IOREQ_ABTS_PENDING] = "FNIC_IOREQ_ABTS_PENDING",
53 	[FNIC_IOREQ_ABTS_COMPLETE] = "FNIC_IOREQ_ABTS_COMPLETE",
54 	[FNIC_IOREQ_CMD_COMPLETE] = "FNIC_IOREQ_CMD_COMPLETE",
55 };
56 
57 static const char *fcpio_status_str[] =  {
58 	[FCPIO_SUCCESS] = "FCPIO_SUCCESS", /*0x0*/
59 	[FCPIO_INVALID_HEADER] = "FCPIO_INVALID_HEADER",
60 	[FCPIO_OUT_OF_RESOURCE] = "FCPIO_OUT_OF_RESOURCE",
61 	[FCPIO_INVALID_PARAM] = "FCPIO_INVALID_PARAM]",
62 	[FCPIO_REQ_NOT_SUPPORTED] = "FCPIO_REQ_NOT_SUPPORTED",
63 	[FCPIO_IO_NOT_FOUND] = "FCPIO_IO_NOT_FOUND",
64 	[FCPIO_ABORTED] = "FCPIO_ABORTED", /*0x41*/
65 	[FCPIO_TIMEOUT] = "FCPIO_TIMEOUT",
66 	[FCPIO_SGL_INVALID] = "FCPIO_SGL_INVALID",
67 	[FCPIO_MSS_INVALID] = "FCPIO_MSS_INVALID",
68 	[FCPIO_DATA_CNT_MISMATCH] = "FCPIO_DATA_CNT_MISMATCH",
69 	[FCPIO_FW_ERR] = "FCPIO_FW_ERR",
70 	[FCPIO_ITMF_REJECTED] = "FCPIO_ITMF_REJECTED",
71 	[FCPIO_ITMF_FAILED] = "FCPIO_ITMF_FAILED",
72 	[FCPIO_ITMF_INCORRECT_LUN] = "FCPIO_ITMF_INCORRECT_LUN",
73 	[FCPIO_CMND_REJECTED] = "FCPIO_CMND_REJECTED",
74 	[FCPIO_NO_PATH_AVAIL] = "FCPIO_NO_PATH_AVAIL",
75 	[FCPIO_PATH_FAILED] = "FCPIO_PATH_FAILED",
76 	[FCPIO_LUNMAP_CHNG_PEND] = "FCPIO_LUNHMAP_CHNG_PEND",
77 };
78 
fnic_state_to_str(unsigned int state)79 const char *fnic_state_to_str(unsigned int state)
80 {
81 	if (state >= ARRAY_SIZE(fnic_state_str) || !fnic_state_str[state])
82 		return "unknown";
83 
84 	return fnic_state_str[state];
85 }
86 
fnic_ioreq_state_to_str(unsigned int state)87 static const char *fnic_ioreq_state_to_str(unsigned int state)
88 {
89 	if (state >= ARRAY_SIZE(fnic_ioreq_state_str) ||
90 	    !fnic_ioreq_state_str[state])
91 		return "unknown";
92 
93 	return fnic_ioreq_state_str[state];
94 }
95 
fnic_fcpio_status_to_str(unsigned int status)96 static const char *fnic_fcpio_status_to_str(unsigned int status)
97 {
98 	if (status >= ARRAY_SIZE(fcpio_status_str) || !fcpio_status_str[status])
99 		return "unknown";
100 
101 	return fcpio_status_str[status];
102 }
103 
104 static void fnic_cleanup_io(struct fnic *fnic, int exclude_id);
105 
fnic_io_lock_hash(struct fnic * fnic,struct scsi_cmnd * sc)106 static inline spinlock_t *fnic_io_lock_hash(struct fnic *fnic,
107 					    struct scsi_cmnd *sc)
108 {
109 	u32 hash = sc->request->tag & (FNIC_IO_LOCKS - 1);
110 
111 	return &fnic->io_req_lock[hash];
112 }
113 
fnic_io_lock_tag(struct fnic * fnic,int tag)114 static inline spinlock_t *fnic_io_lock_tag(struct fnic *fnic,
115 					    int tag)
116 {
117 	return &fnic->io_req_lock[tag & (FNIC_IO_LOCKS - 1)];
118 }
119 
120 /*
121  * Unmap the data buffer and sense buffer for an io_req,
122  * also unmap and free the device-private scatter/gather list.
123  */
fnic_release_ioreq_buf(struct fnic * fnic,struct fnic_io_req * io_req,struct scsi_cmnd * sc)124 static void fnic_release_ioreq_buf(struct fnic *fnic,
125 				   struct fnic_io_req *io_req,
126 				   struct scsi_cmnd *sc)
127 {
128 	if (io_req->sgl_list_pa)
129 		pci_unmap_single(fnic->pdev, io_req->sgl_list_pa,
130 				 sizeof(io_req->sgl_list[0]) * io_req->sgl_cnt,
131 				 PCI_DMA_TODEVICE);
132 	scsi_dma_unmap(sc);
133 
134 	if (io_req->sgl_cnt)
135 		mempool_free(io_req->sgl_list_alloc,
136 			     fnic->io_sgl_pool[io_req->sgl_type]);
137 	if (io_req->sense_buf_pa)
138 		pci_unmap_single(fnic->pdev, io_req->sense_buf_pa,
139 				 SCSI_SENSE_BUFFERSIZE, PCI_DMA_FROMDEVICE);
140 }
141 
142 /* Free up Copy Wq descriptors. Called with copy_wq lock held */
free_wq_copy_descs(struct fnic * fnic,struct vnic_wq_copy * wq)143 static int free_wq_copy_descs(struct fnic *fnic, struct vnic_wq_copy *wq)
144 {
145 	/* if no Ack received from firmware, then nothing to clean */
146 	if (!fnic->fw_ack_recd[0])
147 		return 1;
148 
149 	/*
150 	 * Update desc_available count based on number of freed descriptors
151 	 * Account for wraparound
152 	 */
153 	if (wq->to_clean_index <= fnic->fw_ack_index[0])
154 		wq->ring.desc_avail += (fnic->fw_ack_index[0]
155 					- wq->to_clean_index + 1);
156 	else
157 		wq->ring.desc_avail += (wq->ring.desc_count
158 					- wq->to_clean_index
159 					+ fnic->fw_ack_index[0] + 1);
160 
161 	/*
162 	 * just bump clean index to ack_index+1 accounting for wraparound
163 	 * this will essentially free up all descriptors between
164 	 * to_clean_index and fw_ack_index, both inclusive
165 	 */
166 	wq->to_clean_index =
167 		(fnic->fw_ack_index[0] + 1) % wq->ring.desc_count;
168 
169 	/* we have processed the acks received so far */
170 	fnic->fw_ack_recd[0] = 0;
171 	return 0;
172 }
173 
174 
175 /**
176  * __fnic_set_state_flags
177  * Sets/Clears bits in fnic's state_flags
178  **/
179 void
__fnic_set_state_flags(struct fnic * fnic,unsigned long st_flags,unsigned long clearbits)180 __fnic_set_state_flags(struct fnic *fnic, unsigned long st_flags,
181 			unsigned long clearbits)
182 {
183 	struct Scsi_Host *host = fnic->lport->host;
184 	int sh_locked = spin_is_locked(host->host_lock);
185 	unsigned long flags = 0;
186 
187 	if (!sh_locked)
188 		spin_lock_irqsave(host->host_lock, flags);
189 
190 	if (clearbits)
191 		fnic->state_flags &= ~st_flags;
192 	else
193 		fnic->state_flags |= st_flags;
194 
195 	if (!sh_locked)
196 		spin_unlock_irqrestore(host->host_lock, flags);
197 
198 	return;
199 }
200 
201 
202 /*
203  * fnic_fw_reset_handler
204  * Routine to send reset msg to fw
205  */
fnic_fw_reset_handler(struct fnic * fnic)206 int fnic_fw_reset_handler(struct fnic *fnic)
207 {
208 	struct vnic_wq_copy *wq = &fnic->wq_copy[0];
209 	int ret = 0;
210 	unsigned long flags;
211 
212 	/* indicate fwreset to io path */
213 	fnic_set_state_flags(fnic, FNIC_FLAGS_FWRESET);
214 
215 	skb_queue_purge(&fnic->frame_queue);
216 	skb_queue_purge(&fnic->tx_queue);
217 
218 	/* wait for io cmpl */
219 	while (atomic_read(&fnic->in_flight))
220 		schedule_timeout(msecs_to_jiffies(1));
221 
222 	spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
223 
224 	if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
225 		free_wq_copy_descs(fnic, wq);
226 
227 	if (!vnic_wq_copy_desc_avail(wq))
228 		ret = -EAGAIN;
229 	else {
230 		fnic_queue_wq_copy_desc_fw_reset(wq, SCSI_NO_TAG);
231 		atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
232 		if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
233 			  atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
234 			atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
235 				atomic64_read(
236 				  &fnic->fnic_stats.fw_stats.active_fw_reqs));
237 	}
238 
239 	spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
240 
241 	if (!ret) {
242 		atomic64_inc(&fnic->fnic_stats.reset_stats.fw_resets);
243 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
244 			      "Issued fw reset\n");
245 	} else {
246 		fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
247 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
248 			      "Failed to issue fw reset\n");
249 	}
250 
251 	return ret;
252 }
253 
254 
255 /*
256  * fnic_flogi_reg_handler
257  * Routine to send flogi register msg to fw
258  */
fnic_flogi_reg_handler(struct fnic * fnic,u32 fc_id)259 int fnic_flogi_reg_handler(struct fnic *fnic, u32 fc_id)
260 {
261 	struct vnic_wq_copy *wq = &fnic->wq_copy[0];
262 	enum fcpio_flogi_reg_format_type format;
263 	struct fc_lport *lp = fnic->lport;
264 	u8 gw_mac[ETH_ALEN];
265 	int ret = 0;
266 	unsigned long flags;
267 
268 	spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
269 
270 	if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
271 		free_wq_copy_descs(fnic, wq);
272 
273 	if (!vnic_wq_copy_desc_avail(wq)) {
274 		ret = -EAGAIN;
275 		goto flogi_reg_ioreq_end;
276 	}
277 
278 	if (fnic->ctlr.map_dest) {
279 		memset(gw_mac, 0xff, ETH_ALEN);
280 		format = FCPIO_FLOGI_REG_DEF_DEST;
281 	} else {
282 		memcpy(gw_mac, fnic->ctlr.dest_addr, ETH_ALEN);
283 		format = FCPIO_FLOGI_REG_GW_DEST;
284 	}
285 
286 	if ((fnic->config.flags & VFCF_FIP_CAPABLE) && !fnic->ctlr.map_dest) {
287 		fnic_queue_wq_copy_desc_fip_reg(wq, SCSI_NO_TAG,
288 						fc_id, gw_mac,
289 						fnic->data_src_addr,
290 						lp->r_a_tov, lp->e_d_tov);
291 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
292 			      "FLOGI FIP reg issued fcid %x src %pM dest %pM\n",
293 			      fc_id, fnic->data_src_addr, gw_mac);
294 	} else {
295 		fnic_queue_wq_copy_desc_flogi_reg(wq, SCSI_NO_TAG,
296 						  format, fc_id, gw_mac);
297 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
298 			      "FLOGI reg issued fcid %x map %d dest %pM\n",
299 			      fc_id, fnic->ctlr.map_dest, gw_mac);
300 	}
301 
302 	atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
303 	if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
304 		  atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
305 		atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
306 		  atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
307 
308 flogi_reg_ioreq_end:
309 	spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
310 	return ret;
311 }
312 
313 /*
314  * fnic_queue_wq_copy_desc
315  * Routine to enqueue a wq copy desc
316  */
fnic_queue_wq_copy_desc(struct fnic * fnic,struct vnic_wq_copy * wq,struct fnic_io_req * io_req,struct scsi_cmnd * sc,int sg_count)317 static inline int fnic_queue_wq_copy_desc(struct fnic *fnic,
318 					  struct vnic_wq_copy *wq,
319 					  struct fnic_io_req *io_req,
320 					  struct scsi_cmnd *sc,
321 					  int sg_count)
322 {
323 	struct scatterlist *sg;
324 	struct fc_rport *rport = starget_to_rport(scsi_target(sc->device));
325 	struct fc_rport_libfc_priv *rp = rport->dd_data;
326 	struct host_sg_desc *desc;
327 	struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
328 	unsigned int i;
329 	unsigned long intr_flags;
330 	int flags;
331 	u8 exch_flags;
332 	struct scsi_lun fc_lun;
333 	int r;
334 
335 	if (sg_count) {
336 		/* For each SGE, create a device desc entry */
337 		desc = io_req->sgl_list;
338 		for_each_sg(scsi_sglist(sc), sg, sg_count, i) {
339 			desc->addr = cpu_to_le64(sg_dma_address(sg));
340 			desc->len = cpu_to_le32(sg_dma_len(sg));
341 			desc->_resvd = 0;
342 			desc++;
343 		}
344 
345 		io_req->sgl_list_pa = pci_map_single
346 			(fnic->pdev,
347 			 io_req->sgl_list,
348 			 sizeof(io_req->sgl_list[0]) * sg_count,
349 			 PCI_DMA_TODEVICE);
350 
351 		r = pci_dma_mapping_error(fnic->pdev, io_req->sgl_list_pa);
352 		if (r) {
353 			printk(KERN_ERR "PCI mapping failed with error %d\n", r);
354 			return SCSI_MLQUEUE_HOST_BUSY;
355 		}
356 	}
357 
358 	io_req->sense_buf_pa = pci_map_single(fnic->pdev,
359 					      sc->sense_buffer,
360 					      SCSI_SENSE_BUFFERSIZE,
361 					      PCI_DMA_FROMDEVICE);
362 
363 	r = pci_dma_mapping_error(fnic->pdev, io_req->sense_buf_pa);
364 	if (r) {
365 		pci_unmap_single(fnic->pdev, io_req->sgl_list_pa,
366 				sizeof(io_req->sgl_list[0]) * sg_count,
367 				PCI_DMA_TODEVICE);
368 		printk(KERN_ERR "PCI mapping failed with error %d\n", r);
369 		return SCSI_MLQUEUE_HOST_BUSY;
370 	}
371 
372 	int_to_scsilun(sc->device->lun, &fc_lun);
373 
374 	/* Enqueue the descriptor in the Copy WQ */
375 	spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
376 
377 	if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
378 		free_wq_copy_descs(fnic, wq);
379 
380 	if (unlikely(!vnic_wq_copy_desc_avail(wq))) {
381 		spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
382 		FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
383 			  "fnic_queue_wq_copy_desc failure - no descriptors\n");
384 		atomic64_inc(&misc_stats->io_cpwq_alloc_failures);
385 		return SCSI_MLQUEUE_HOST_BUSY;
386 	}
387 
388 	flags = 0;
389 	if (sc->sc_data_direction == DMA_FROM_DEVICE)
390 		flags = FCPIO_ICMND_RDDATA;
391 	else if (sc->sc_data_direction == DMA_TO_DEVICE)
392 		flags = FCPIO_ICMND_WRDATA;
393 
394 	exch_flags = 0;
395 	if ((fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR) &&
396 	    (rp->flags & FC_RP_FLAGS_RETRY))
397 		exch_flags |= FCPIO_ICMND_SRFLAG_RETRY;
398 
399 	fnic_queue_wq_copy_desc_icmnd_16(wq, sc->request->tag,
400 					 0, exch_flags, io_req->sgl_cnt,
401 					 SCSI_SENSE_BUFFERSIZE,
402 					 io_req->sgl_list_pa,
403 					 io_req->sense_buf_pa,
404 					 0, /* scsi cmd ref, always 0 */
405 					 FCPIO_ICMND_PTA_SIMPLE,
406 					 	/* scsi pri and tag */
407 					 flags,	/* command flags */
408 					 sc->cmnd, sc->cmd_len,
409 					 scsi_bufflen(sc),
410 					 fc_lun.scsi_lun, io_req->port_id,
411 					 rport->maxframe_size, rp->r_a_tov,
412 					 rp->e_d_tov);
413 
414 	atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
415 	if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
416 		  atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
417 		atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
418 		  atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
419 
420 	spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
421 	return 0;
422 }
423 
424 /*
425  * fnic_queuecommand
426  * Routine to send a scsi cdb
427  * Called with host_lock held and interrupts disabled.
428  */
fnic_queuecommand_lck(struct scsi_cmnd * sc,void (* done)(struct scsi_cmnd *))429 static int fnic_queuecommand_lck(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
430 {
431 	struct fc_lport *lp = shost_priv(sc->device->host);
432 	struct fc_rport *rport;
433 	struct fnic_io_req *io_req = NULL;
434 	struct fnic *fnic = lport_priv(lp);
435 	struct fnic_stats *fnic_stats = &fnic->fnic_stats;
436 	struct vnic_wq_copy *wq;
437 	int ret;
438 	u64 cmd_trace;
439 	int sg_count = 0;
440 	unsigned long flags = 0;
441 	unsigned long ptr;
442 	spinlock_t *io_lock = NULL;
443 	int io_lock_acquired = 0;
444 
445 	if (unlikely(fnic_chk_state_flags_locked(fnic, FNIC_FLAGS_IO_BLOCKED)))
446 		return SCSI_MLQUEUE_HOST_BUSY;
447 
448 	rport = starget_to_rport(scsi_target(sc->device));
449 	ret = fc_remote_port_chkready(rport);
450 	if (ret) {
451 		atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
452 		sc->result = ret;
453 		done(sc);
454 		return 0;
455 	}
456 
457 	if (rport) {
458 		struct fc_rport_libfc_priv *rp = rport->dd_data;
459 
460 		if (!rp || rp->rp_state != RPORT_ST_READY) {
461 			FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
462 				"returning DID_NO_CONNECT for IO as rport is removed\n");
463 			atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
464 			sc->result = DID_NO_CONNECT<<16;
465 			done(sc);
466 			return 0;
467 		}
468 	}
469 
470 	if (lp->state != LPORT_ST_READY || !(lp->link_up))
471 		return SCSI_MLQUEUE_HOST_BUSY;
472 
473 	atomic_inc(&fnic->in_flight);
474 
475 	/*
476 	 * Release host lock, use driver resource specific locks from here.
477 	 * Don't re-enable interrupts in case they were disabled prior to the
478 	 * caller disabling them.
479 	 */
480 	spin_unlock(lp->host->host_lock);
481 	CMD_STATE(sc) = FNIC_IOREQ_NOT_INITED;
482 	CMD_FLAGS(sc) = FNIC_NO_FLAGS;
483 
484 	/* Get a new io_req for this SCSI IO */
485 	io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
486 	if (!io_req) {
487 		atomic64_inc(&fnic_stats->io_stats.alloc_failures);
488 		ret = SCSI_MLQUEUE_HOST_BUSY;
489 		goto out;
490 	}
491 	memset(io_req, 0, sizeof(*io_req));
492 
493 	/* Map the data buffer */
494 	sg_count = scsi_dma_map(sc);
495 	if (sg_count < 0) {
496 		FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
497 			  sc->request->tag, sc, 0, sc->cmnd[0],
498 			  sg_count, CMD_STATE(sc));
499 		mempool_free(io_req, fnic->io_req_pool);
500 		goto out;
501 	}
502 
503 	/* Determine the type of scatter/gather list we need */
504 	io_req->sgl_cnt = sg_count;
505 	io_req->sgl_type = FNIC_SGL_CACHE_DFLT;
506 	if (sg_count > FNIC_DFLT_SG_DESC_CNT)
507 		io_req->sgl_type = FNIC_SGL_CACHE_MAX;
508 
509 	if (sg_count) {
510 		io_req->sgl_list =
511 			mempool_alloc(fnic->io_sgl_pool[io_req->sgl_type],
512 				      GFP_ATOMIC);
513 		if (!io_req->sgl_list) {
514 			atomic64_inc(&fnic_stats->io_stats.alloc_failures);
515 			ret = SCSI_MLQUEUE_HOST_BUSY;
516 			scsi_dma_unmap(sc);
517 			mempool_free(io_req, fnic->io_req_pool);
518 			goto out;
519 		}
520 
521 		/* Cache sgl list allocated address before alignment */
522 		io_req->sgl_list_alloc = io_req->sgl_list;
523 		ptr = (unsigned long) io_req->sgl_list;
524 		if (ptr % FNIC_SG_DESC_ALIGN) {
525 			io_req->sgl_list = (struct host_sg_desc *)
526 				(((unsigned long) ptr
527 				  + FNIC_SG_DESC_ALIGN - 1)
528 				 & ~(FNIC_SG_DESC_ALIGN - 1));
529 		}
530 	}
531 
532 	/*
533 	* Will acquire lock defore setting to IO initialized.
534 	*/
535 
536 	io_lock = fnic_io_lock_hash(fnic, sc);
537 	spin_lock_irqsave(io_lock, flags);
538 
539 	/* initialize rest of io_req */
540 	io_lock_acquired = 1;
541 	io_req->port_id = rport->port_id;
542 	io_req->start_time = jiffies;
543 	CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
544 	CMD_SP(sc) = (char *)io_req;
545 	CMD_FLAGS(sc) |= FNIC_IO_INITIALIZED;
546 	sc->scsi_done = done;
547 
548 	/* create copy wq desc and enqueue it */
549 	wq = &fnic->wq_copy[0];
550 	ret = fnic_queue_wq_copy_desc(fnic, wq, io_req, sc, sg_count);
551 	if (ret) {
552 		/*
553 		 * In case another thread cancelled the request,
554 		 * refetch the pointer under the lock.
555 		 */
556 		FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
557 			  sc->request->tag, sc, 0, 0, 0,
558 			  (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
559 		io_req = (struct fnic_io_req *)CMD_SP(sc);
560 		CMD_SP(sc) = NULL;
561 		CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
562 		spin_unlock_irqrestore(io_lock, flags);
563 		if (io_req) {
564 			fnic_release_ioreq_buf(fnic, io_req, sc);
565 			mempool_free(io_req, fnic->io_req_pool);
566 		}
567 		atomic_dec(&fnic->in_flight);
568 		/* acquire host lock before returning to SCSI */
569 		spin_lock(lp->host->host_lock);
570 		return ret;
571 	} else {
572 		atomic64_inc(&fnic_stats->io_stats.active_ios);
573 		atomic64_inc(&fnic_stats->io_stats.num_ios);
574 		if (atomic64_read(&fnic_stats->io_stats.active_ios) >
575 			  atomic64_read(&fnic_stats->io_stats.max_active_ios))
576 			atomic64_set(&fnic_stats->io_stats.max_active_ios,
577 			     atomic64_read(&fnic_stats->io_stats.active_ios));
578 
579 		/* REVISIT: Use per IO lock in the final code */
580 		CMD_FLAGS(sc) |= FNIC_IO_ISSUED;
581 	}
582 out:
583 	cmd_trace = ((u64)sc->cmnd[0] << 56 | (u64)sc->cmnd[7] << 40 |
584 			(u64)sc->cmnd[8] << 32 | (u64)sc->cmnd[2] << 24 |
585 			(u64)sc->cmnd[3] << 16 | (u64)sc->cmnd[4] << 8 |
586 			sc->cmnd[5]);
587 
588 	FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
589 		  sc->request->tag, sc, io_req,
590 		  sg_count, cmd_trace,
591 		  (((u64)CMD_FLAGS(sc) >> 32) | CMD_STATE(sc)));
592 
593 	/* if only we issued IO, will we have the io lock */
594 	if (io_lock_acquired)
595 		spin_unlock_irqrestore(io_lock, flags);
596 
597 	atomic_dec(&fnic->in_flight);
598 	/* acquire host lock before returning to SCSI */
599 	spin_lock(lp->host->host_lock);
600 	return ret;
601 }
602 
DEF_SCSI_QCMD(fnic_queuecommand)603 DEF_SCSI_QCMD(fnic_queuecommand)
604 
605 /*
606  * fnic_fcpio_fw_reset_cmpl_handler
607  * Routine to handle fw reset completion
608  */
609 static int fnic_fcpio_fw_reset_cmpl_handler(struct fnic *fnic,
610 					    struct fcpio_fw_req *desc)
611 {
612 	u8 type;
613 	u8 hdr_status;
614 	struct fcpio_tag tag;
615 	int ret = 0;
616 	unsigned long flags;
617 	struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
618 
619 	fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
620 
621 	atomic64_inc(&reset_stats->fw_reset_completions);
622 
623 	/* Clean up all outstanding io requests */
624 	fnic_cleanup_io(fnic, SCSI_NO_TAG);
625 
626 	atomic64_set(&fnic->fnic_stats.fw_stats.active_fw_reqs, 0);
627 	atomic64_set(&fnic->fnic_stats.io_stats.active_ios, 0);
628 
629 	spin_lock_irqsave(&fnic->fnic_lock, flags);
630 
631 	/* fnic should be in FC_TRANS_ETH_MODE */
632 	if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE) {
633 		/* Check status of reset completion */
634 		if (!hdr_status) {
635 			FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
636 				      "reset cmpl success\n");
637 			/* Ready to send flogi out */
638 			fnic->state = FNIC_IN_ETH_MODE;
639 		} else {
640 			FNIC_SCSI_DBG(KERN_DEBUG,
641 				      fnic->lport->host,
642 				      "fnic fw_reset : failed %s\n",
643 				      fnic_fcpio_status_to_str(hdr_status));
644 
645 			/*
646 			 * Unable to change to eth mode, cannot send out flogi
647 			 * Change state to fc mode, so that subsequent Flogi
648 			 * requests from libFC will cause more attempts to
649 			 * reset the firmware. Free the cached flogi
650 			 */
651 			fnic->state = FNIC_IN_FC_MODE;
652 			atomic64_inc(&reset_stats->fw_reset_failures);
653 			ret = -1;
654 		}
655 	} else {
656 		FNIC_SCSI_DBG(KERN_DEBUG,
657 			      fnic->lport->host,
658 			      "Unexpected state %s while processing"
659 			      " reset cmpl\n", fnic_state_to_str(fnic->state));
660 		atomic64_inc(&reset_stats->fw_reset_failures);
661 		ret = -1;
662 	}
663 
664 	/* Thread removing device blocks till firmware reset is complete */
665 	if (fnic->remove_wait)
666 		complete(fnic->remove_wait);
667 
668 	/*
669 	 * If fnic is being removed, or fw reset failed
670 	 * free the flogi frame. Else, send it out
671 	 */
672 	if (fnic->remove_wait || ret) {
673 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
674 		skb_queue_purge(&fnic->tx_queue);
675 		goto reset_cmpl_handler_end;
676 	}
677 
678 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
679 
680 	fnic_flush_tx(fnic);
681 
682  reset_cmpl_handler_end:
683 	fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
684 
685 	return ret;
686 }
687 
688 /*
689  * fnic_fcpio_flogi_reg_cmpl_handler
690  * Routine to handle flogi register completion
691  */
fnic_fcpio_flogi_reg_cmpl_handler(struct fnic * fnic,struct fcpio_fw_req * desc)692 static int fnic_fcpio_flogi_reg_cmpl_handler(struct fnic *fnic,
693 					     struct fcpio_fw_req *desc)
694 {
695 	u8 type;
696 	u8 hdr_status;
697 	struct fcpio_tag tag;
698 	int ret = 0;
699 	unsigned long flags;
700 
701 	fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
702 
703 	/* Update fnic state based on status of flogi reg completion */
704 	spin_lock_irqsave(&fnic->fnic_lock, flags);
705 
706 	if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE) {
707 
708 		/* Check flogi registration completion status */
709 		if (!hdr_status) {
710 			FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
711 				      "flog reg succeeded\n");
712 			fnic->state = FNIC_IN_FC_MODE;
713 		} else {
714 			FNIC_SCSI_DBG(KERN_DEBUG,
715 				      fnic->lport->host,
716 				      "fnic flogi reg :failed %s\n",
717 				      fnic_fcpio_status_to_str(hdr_status));
718 			fnic->state = FNIC_IN_ETH_MODE;
719 			ret = -1;
720 		}
721 	} else {
722 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
723 			      "Unexpected fnic state %s while"
724 			      " processing flogi reg completion\n",
725 			      fnic_state_to_str(fnic->state));
726 		ret = -1;
727 	}
728 
729 	if (!ret) {
730 		if (fnic->stop_rx_link_events) {
731 			spin_unlock_irqrestore(&fnic->fnic_lock, flags);
732 			goto reg_cmpl_handler_end;
733 		}
734 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
735 
736 		fnic_flush_tx(fnic);
737 		queue_work(fnic_event_queue, &fnic->frame_work);
738 	} else {
739 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
740 	}
741 
742 reg_cmpl_handler_end:
743 	return ret;
744 }
745 
is_ack_index_in_range(struct vnic_wq_copy * wq,u16 request_out)746 static inline int is_ack_index_in_range(struct vnic_wq_copy *wq,
747 					u16 request_out)
748 {
749 	if (wq->to_clean_index <= wq->to_use_index) {
750 		/* out of range, stale request_out index */
751 		if (request_out < wq->to_clean_index ||
752 		    request_out >= wq->to_use_index)
753 			return 0;
754 	} else {
755 		/* out of range, stale request_out index */
756 		if (request_out < wq->to_clean_index &&
757 		    request_out >= wq->to_use_index)
758 			return 0;
759 	}
760 	/* request_out index is in range */
761 	return 1;
762 }
763 
764 
765 /*
766  * Mark that ack received and store the Ack index. If there are multiple
767  * acks received before Tx thread cleans it up, the latest value will be
768  * used which is correct behavior. This state should be in the copy Wq
769  * instead of in the fnic
770  */
fnic_fcpio_ack_handler(struct fnic * fnic,unsigned int cq_index,struct fcpio_fw_req * desc)771 static inline void fnic_fcpio_ack_handler(struct fnic *fnic,
772 					  unsigned int cq_index,
773 					  struct fcpio_fw_req *desc)
774 {
775 	struct vnic_wq_copy *wq;
776 	u16 request_out = desc->u.ack.request_out;
777 	unsigned long flags;
778 	u64 *ox_id_tag = (u64 *)(void *)desc;
779 
780 	/* mark the ack state */
781 	wq = &fnic->wq_copy[cq_index - fnic->raw_wq_count - fnic->rq_count];
782 	spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
783 
784 	fnic->fnic_stats.misc_stats.last_ack_time = jiffies;
785 	if (is_ack_index_in_range(wq, request_out)) {
786 		fnic->fw_ack_index[0] = request_out;
787 		fnic->fw_ack_recd[0] = 1;
788 	} else
789 		atomic64_inc(
790 			&fnic->fnic_stats.misc_stats.ack_index_out_of_range);
791 
792 	spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
793 	FNIC_TRACE(fnic_fcpio_ack_handler,
794 		  fnic->lport->host->host_no, 0, 0, ox_id_tag[2], ox_id_tag[3],
795 		  ox_id_tag[4], ox_id_tag[5]);
796 }
797 
798 /*
799  * fnic_fcpio_icmnd_cmpl_handler
800  * Routine to handle icmnd completions
801  */
fnic_fcpio_icmnd_cmpl_handler(struct fnic * fnic,struct fcpio_fw_req * desc)802 static void fnic_fcpio_icmnd_cmpl_handler(struct fnic *fnic,
803 					 struct fcpio_fw_req *desc)
804 {
805 	u8 type;
806 	u8 hdr_status;
807 	struct fcpio_tag tag;
808 	u32 id;
809 	u64 xfer_len = 0;
810 	struct fcpio_icmnd_cmpl *icmnd_cmpl;
811 	struct fnic_io_req *io_req;
812 	struct scsi_cmnd *sc;
813 	struct fnic_stats *fnic_stats = &fnic->fnic_stats;
814 	unsigned long flags;
815 	spinlock_t *io_lock;
816 	u64 cmd_trace;
817 	unsigned long start_time;
818 
819 	/* Decode the cmpl description to get the io_req id */
820 	fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
821 	fcpio_tag_id_dec(&tag, &id);
822 	icmnd_cmpl = &desc->u.icmnd_cmpl;
823 
824 	if (id >= fnic->fnic_max_tag_id) {
825 		shost_printk(KERN_ERR, fnic->lport->host,
826 			"Tag out of range tag %x hdr status = %s\n",
827 			     id, fnic_fcpio_status_to_str(hdr_status));
828 		return;
829 	}
830 
831 	sc = scsi_host_find_tag(fnic->lport->host, id);
832 	WARN_ON_ONCE(!sc);
833 	if (!sc) {
834 		atomic64_inc(&fnic_stats->io_stats.sc_null);
835 		shost_printk(KERN_ERR, fnic->lport->host,
836 			  "icmnd_cmpl sc is null - "
837 			  "hdr status = %s tag = 0x%x desc = 0x%p\n",
838 			  fnic_fcpio_status_to_str(hdr_status), id, desc);
839 		FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
840 			  fnic->lport->host->host_no, id,
841 			  ((u64)icmnd_cmpl->_resvd0[1] << 16 |
842 			  (u64)icmnd_cmpl->_resvd0[0]),
843 			  ((u64)hdr_status << 16 |
844 			  (u64)icmnd_cmpl->scsi_status << 8 |
845 			  (u64)icmnd_cmpl->flags), desc,
846 			  (u64)icmnd_cmpl->residual, 0);
847 		return;
848 	}
849 
850 	io_lock = fnic_io_lock_hash(fnic, sc);
851 	spin_lock_irqsave(io_lock, flags);
852 	io_req = (struct fnic_io_req *)CMD_SP(sc);
853 	WARN_ON_ONCE(!io_req);
854 	if (!io_req) {
855 		atomic64_inc(&fnic_stats->io_stats.ioreq_null);
856 		CMD_FLAGS(sc) |= FNIC_IO_REQ_NULL;
857 		spin_unlock_irqrestore(io_lock, flags);
858 		shost_printk(KERN_ERR, fnic->lport->host,
859 			  "icmnd_cmpl io_req is null - "
860 			  "hdr status = %s tag = 0x%x sc 0x%p\n",
861 			  fnic_fcpio_status_to_str(hdr_status), id, sc);
862 		return;
863 	}
864 	start_time = io_req->start_time;
865 
866 	/* firmware completed the io */
867 	io_req->io_completed = 1;
868 
869 	/*
870 	 *  if SCSI-ML has already issued abort on this command,
871 	 * ignore completion of the IO. The abts path will clean it up
872 	 */
873 	if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
874 		spin_unlock_irqrestore(io_lock, flags);
875 		CMD_FLAGS(sc) |= FNIC_IO_ABTS_PENDING;
876 		switch (hdr_status) {
877 		case FCPIO_SUCCESS:
878 			CMD_FLAGS(sc) |= FNIC_IO_DONE;
879 			FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
880 				  "icmnd_cmpl ABTS pending hdr status = %s "
881 				  "sc  0x%p scsi_status %x  residual %d\n",
882 				  fnic_fcpio_status_to_str(hdr_status), sc,
883 				  icmnd_cmpl->scsi_status,
884 				  icmnd_cmpl->residual);
885 			break;
886 		case FCPIO_ABORTED:
887 			CMD_FLAGS(sc) |= FNIC_IO_ABORTED;
888 			break;
889 		default:
890 			FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
891 					  "icmnd_cmpl abts pending "
892 					  "hdr status = %s tag = 0x%x sc = 0x%p\n",
893 					  fnic_fcpio_status_to_str(hdr_status),
894 					  id, sc);
895 			break;
896 		}
897 		return;
898 	}
899 
900 	/* Mark the IO as complete */
901 	CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
902 
903 	icmnd_cmpl = &desc->u.icmnd_cmpl;
904 
905 	switch (hdr_status) {
906 	case FCPIO_SUCCESS:
907 		sc->result = (DID_OK << 16) | icmnd_cmpl->scsi_status;
908 		xfer_len = scsi_bufflen(sc);
909 		scsi_set_resid(sc, icmnd_cmpl->residual);
910 
911 		if (icmnd_cmpl->flags & FCPIO_ICMND_CMPL_RESID_UNDER)
912 			xfer_len -= icmnd_cmpl->residual;
913 
914 		if (icmnd_cmpl->scsi_status == SAM_STAT_TASK_SET_FULL)
915 			atomic64_inc(&fnic_stats->misc_stats.queue_fulls);
916 		break;
917 
918 	case FCPIO_TIMEOUT:          /* request was timed out */
919 		atomic64_inc(&fnic_stats->misc_stats.fcpio_timeout);
920 		sc->result = (DID_TIME_OUT << 16) | icmnd_cmpl->scsi_status;
921 		break;
922 
923 	case FCPIO_ABORTED:          /* request was aborted */
924 		atomic64_inc(&fnic_stats->misc_stats.fcpio_aborted);
925 		sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
926 		break;
927 
928 	case FCPIO_DATA_CNT_MISMATCH: /* recv/sent more/less data than exp. */
929 		atomic64_inc(&fnic_stats->misc_stats.data_count_mismatch);
930 		scsi_set_resid(sc, icmnd_cmpl->residual);
931 		sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
932 		break;
933 
934 	case FCPIO_OUT_OF_RESOURCE:  /* out of resources to complete request */
935 		atomic64_inc(&fnic_stats->fw_stats.fw_out_of_resources);
936 		sc->result = (DID_REQUEUE << 16) | icmnd_cmpl->scsi_status;
937 		break;
938 
939 	case FCPIO_IO_NOT_FOUND:     /* requested I/O was not found */
940 		atomic64_inc(&fnic_stats->io_stats.io_not_found);
941 		sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
942 		break;
943 
944 	case FCPIO_SGL_INVALID:      /* request was aborted due to sgl error */
945 		atomic64_inc(&fnic_stats->misc_stats.sgl_invalid);
946 		sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
947 		break;
948 
949 	case FCPIO_FW_ERR:           /* request was terminated due fw error */
950 		atomic64_inc(&fnic_stats->fw_stats.io_fw_errs);
951 		sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
952 		break;
953 
954 	case FCPIO_MSS_INVALID:      /* request was aborted due to mss error */
955 		atomic64_inc(&fnic_stats->misc_stats.mss_invalid);
956 		sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
957 		break;
958 
959 	case FCPIO_INVALID_HEADER:   /* header contains invalid data */
960 	case FCPIO_INVALID_PARAM:    /* some parameter in request invalid */
961 	case FCPIO_REQ_NOT_SUPPORTED:/* request type is not supported */
962 	default:
963 		sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
964 		break;
965 	}
966 
967 	/* Break link with the SCSI command */
968 	CMD_SP(sc) = NULL;
969 	CMD_FLAGS(sc) |= FNIC_IO_DONE;
970 
971 	spin_unlock_irqrestore(io_lock, flags);
972 
973 	if (hdr_status != FCPIO_SUCCESS) {
974 		atomic64_inc(&fnic_stats->io_stats.io_failures);
975 		shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
976 			     fnic_fcpio_status_to_str(hdr_status));
977 	}
978 
979 	fnic_release_ioreq_buf(fnic, io_req, sc);
980 
981 	mempool_free(io_req, fnic->io_req_pool);
982 
983 	cmd_trace = ((u64)hdr_status << 56) |
984 		  (u64)icmnd_cmpl->scsi_status << 48 |
985 		  (u64)icmnd_cmpl->flags << 40 | (u64)sc->cmnd[0] << 32 |
986 		  (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
987 		  (u64)sc->cmnd[4] << 8 | sc->cmnd[5];
988 
989 	FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
990 		  sc->device->host->host_no, id, sc,
991 		  ((u64)icmnd_cmpl->_resvd0[1] << 56 |
992 		  (u64)icmnd_cmpl->_resvd0[0] << 48 |
993 		  jiffies_to_msecs(jiffies - start_time)),
994 		  desc, cmd_trace,
995 		  (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
996 
997 	if (sc->sc_data_direction == DMA_FROM_DEVICE) {
998 		fnic->lport->host_stats.fcp_input_requests++;
999 		fnic->fcp_input_bytes += xfer_len;
1000 	} else if (sc->sc_data_direction == DMA_TO_DEVICE) {
1001 		fnic->lport->host_stats.fcp_output_requests++;
1002 		fnic->fcp_output_bytes += xfer_len;
1003 	} else
1004 		fnic->lport->host_stats.fcp_control_requests++;
1005 
1006 	atomic64_dec(&fnic_stats->io_stats.active_ios);
1007 	if (atomic64_read(&fnic->io_cmpl_skip))
1008 		atomic64_dec(&fnic->io_cmpl_skip);
1009 	else
1010 		atomic64_inc(&fnic_stats->io_stats.io_completions);
1011 
1012 	/* Call SCSI completion function to complete the IO */
1013 	if (sc->scsi_done)
1014 		sc->scsi_done(sc);
1015 }
1016 
1017 /* fnic_fcpio_itmf_cmpl_handler
1018  * Routine to handle itmf completions
1019  */
fnic_fcpio_itmf_cmpl_handler(struct fnic * fnic,struct fcpio_fw_req * desc)1020 static void fnic_fcpio_itmf_cmpl_handler(struct fnic *fnic,
1021 					struct fcpio_fw_req *desc)
1022 {
1023 	u8 type;
1024 	u8 hdr_status;
1025 	struct fcpio_tag tag;
1026 	u32 id;
1027 	struct scsi_cmnd *sc;
1028 	struct fnic_io_req *io_req;
1029 	struct fnic_stats *fnic_stats = &fnic->fnic_stats;
1030 	struct abort_stats *abts_stats = &fnic->fnic_stats.abts_stats;
1031 	struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
1032 	struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
1033 	unsigned long flags;
1034 	spinlock_t *io_lock;
1035 	unsigned long start_time;
1036 
1037 	fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
1038 	fcpio_tag_id_dec(&tag, &id);
1039 
1040 	if ((id & FNIC_TAG_MASK) >= fnic->fnic_max_tag_id) {
1041 		shost_printk(KERN_ERR, fnic->lport->host,
1042 		"Tag out of range tag %x hdr status = %s\n",
1043 		id, fnic_fcpio_status_to_str(hdr_status));
1044 		return;
1045 	}
1046 
1047 	sc = scsi_host_find_tag(fnic->lport->host, id & FNIC_TAG_MASK);
1048 	WARN_ON_ONCE(!sc);
1049 	if (!sc) {
1050 		atomic64_inc(&fnic_stats->io_stats.sc_null);
1051 		shost_printk(KERN_ERR, fnic->lport->host,
1052 			  "itmf_cmpl sc is null - hdr status = %s tag = 0x%x\n",
1053 			  fnic_fcpio_status_to_str(hdr_status), id);
1054 		return;
1055 	}
1056 	io_lock = fnic_io_lock_hash(fnic, sc);
1057 	spin_lock_irqsave(io_lock, flags);
1058 	io_req = (struct fnic_io_req *)CMD_SP(sc);
1059 	WARN_ON_ONCE(!io_req);
1060 	if (!io_req) {
1061 		atomic64_inc(&fnic_stats->io_stats.ioreq_null);
1062 		spin_unlock_irqrestore(io_lock, flags);
1063 		CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
1064 		shost_printk(KERN_ERR, fnic->lport->host,
1065 			  "itmf_cmpl io_req is null - "
1066 			  "hdr status = %s tag = 0x%x sc 0x%p\n",
1067 			  fnic_fcpio_status_to_str(hdr_status), id, sc);
1068 		return;
1069 	}
1070 	start_time = io_req->start_time;
1071 
1072 	if ((id & FNIC_TAG_ABORT) && (id & FNIC_TAG_DEV_RST)) {
1073 		/* Abort and terminate completion of device reset req */
1074 		/* REVISIT : Add asserts about various flags */
1075 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1076 			      "dev reset abts cmpl recd. id %x status %s\n",
1077 			      id, fnic_fcpio_status_to_str(hdr_status));
1078 		CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1079 		CMD_ABTS_STATUS(sc) = hdr_status;
1080 		CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1081 		if (io_req->abts_done)
1082 			complete(io_req->abts_done);
1083 		spin_unlock_irqrestore(io_lock, flags);
1084 	} else if (id & FNIC_TAG_ABORT) {
1085 		/* Completion of abort cmd */
1086 		switch (hdr_status) {
1087 		case FCPIO_SUCCESS:
1088 			break;
1089 		case FCPIO_TIMEOUT:
1090 			if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1091 				atomic64_inc(&abts_stats->abort_fw_timeouts);
1092 			else
1093 				atomic64_inc(
1094 					&term_stats->terminate_fw_timeouts);
1095 			break;
1096 		case FCPIO_ITMF_REJECTED:
1097 			FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1098 				"abort reject recd. id %d\n",
1099 				(int)(id & FNIC_TAG_MASK));
1100 			break;
1101 		case FCPIO_IO_NOT_FOUND:
1102 			if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1103 				atomic64_inc(&abts_stats->abort_io_not_found);
1104 			else
1105 				atomic64_inc(
1106 					&term_stats->terminate_io_not_found);
1107 			break;
1108 		default:
1109 			if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1110 				atomic64_inc(&abts_stats->abort_failures);
1111 			else
1112 				atomic64_inc(
1113 					&term_stats->terminate_failures);
1114 			break;
1115 		}
1116 		if (CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING) {
1117 			/* This is a late completion. Ignore it */
1118 			spin_unlock_irqrestore(io_lock, flags);
1119 			return;
1120 		}
1121 
1122 		CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
1123 
1124 		/* If the status is IO not found consider it as success */
1125 		if (hdr_status == FCPIO_IO_NOT_FOUND)
1126 			CMD_ABTS_STATUS(sc) = FCPIO_SUCCESS;
1127 		else
1128 			CMD_ABTS_STATUS(sc) = hdr_status;
1129 
1130 		if (!(CMD_FLAGS(sc) & (FNIC_IO_ABORTED | FNIC_IO_DONE)))
1131 			atomic64_inc(&misc_stats->no_icmnd_itmf_cmpls);
1132 
1133 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1134 			      "abts cmpl recd. id %d status %s\n",
1135 			      (int)(id & FNIC_TAG_MASK),
1136 			      fnic_fcpio_status_to_str(hdr_status));
1137 
1138 		/*
1139 		 * If scsi_eh thread is blocked waiting for abts to complete,
1140 		 * signal completion to it. IO will be cleaned in the thread
1141 		 * else clean it in this context
1142 		 */
1143 		if (io_req->abts_done) {
1144 			complete(io_req->abts_done);
1145 			spin_unlock_irqrestore(io_lock, flags);
1146 		} else {
1147 			FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1148 				      "abts cmpl, completing IO\n");
1149 			CMD_SP(sc) = NULL;
1150 			sc->result = (DID_ERROR << 16);
1151 
1152 			spin_unlock_irqrestore(io_lock, flags);
1153 
1154 			fnic_release_ioreq_buf(fnic, io_req, sc);
1155 			mempool_free(io_req, fnic->io_req_pool);
1156 			if (sc->scsi_done) {
1157 				FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1158 					sc->device->host->host_no, id,
1159 					sc,
1160 					jiffies_to_msecs(jiffies - start_time),
1161 					desc,
1162 					(((u64)hdr_status << 40) |
1163 					(u64)sc->cmnd[0] << 32 |
1164 					(u64)sc->cmnd[2] << 24 |
1165 					(u64)sc->cmnd[3] << 16 |
1166 					(u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1167 					(((u64)CMD_FLAGS(sc) << 32) |
1168 					CMD_STATE(sc)));
1169 				sc->scsi_done(sc);
1170 				atomic64_dec(&fnic_stats->io_stats.active_ios);
1171 				if (atomic64_read(&fnic->io_cmpl_skip))
1172 					atomic64_dec(&fnic->io_cmpl_skip);
1173 				else
1174 					atomic64_inc(&fnic_stats->io_stats.io_completions);
1175 			}
1176 		}
1177 
1178 	} else if (id & FNIC_TAG_DEV_RST) {
1179 		/* Completion of device reset */
1180 		CMD_LR_STATUS(sc) = hdr_status;
1181 		if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1182 			spin_unlock_irqrestore(io_lock, flags);
1183 			CMD_FLAGS(sc) |= FNIC_DEV_RST_ABTS_PENDING;
1184 			FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1185 				  sc->device->host->host_no, id, sc,
1186 				  jiffies_to_msecs(jiffies - start_time),
1187 				  desc, 0,
1188 				  (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1189 			FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1190 				"Terminate pending "
1191 				"dev reset cmpl recd. id %d status %s\n",
1192 				(int)(id & FNIC_TAG_MASK),
1193 				fnic_fcpio_status_to_str(hdr_status));
1194 			return;
1195 		}
1196 		if (CMD_FLAGS(sc) & FNIC_DEV_RST_TIMED_OUT) {
1197 			/* Need to wait for terminate completion */
1198 			spin_unlock_irqrestore(io_lock, flags);
1199 			FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1200 				  sc->device->host->host_no, id, sc,
1201 				  jiffies_to_msecs(jiffies - start_time),
1202 				  desc, 0,
1203 				  (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1204 			FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1205 				"dev reset cmpl recd after time out. "
1206 				"id %d status %s\n",
1207 				(int)(id & FNIC_TAG_MASK),
1208 				fnic_fcpio_status_to_str(hdr_status));
1209 			return;
1210 		}
1211 		CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
1212 		CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1213 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1214 			      "dev reset cmpl recd. id %d status %s\n",
1215 			      (int)(id & FNIC_TAG_MASK),
1216 			      fnic_fcpio_status_to_str(hdr_status));
1217 		if (io_req->dr_done)
1218 			complete(io_req->dr_done);
1219 		spin_unlock_irqrestore(io_lock, flags);
1220 
1221 	} else {
1222 		shost_printk(KERN_ERR, fnic->lport->host,
1223 			     "Unexpected itmf io state %s tag %x\n",
1224 			     fnic_ioreq_state_to_str(CMD_STATE(sc)), id);
1225 		spin_unlock_irqrestore(io_lock, flags);
1226 	}
1227 
1228 }
1229 
1230 /*
1231  * fnic_fcpio_cmpl_handler
1232  * Routine to service the cq for wq_copy
1233  */
fnic_fcpio_cmpl_handler(struct vnic_dev * vdev,unsigned int cq_index,struct fcpio_fw_req * desc)1234 static int fnic_fcpio_cmpl_handler(struct vnic_dev *vdev,
1235 				   unsigned int cq_index,
1236 				   struct fcpio_fw_req *desc)
1237 {
1238 	struct fnic *fnic = vnic_dev_priv(vdev);
1239 
1240 	switch (desc->hdr.type) {
1241 	case FCPIO_ICMND_CMPL: /* fw completed a command */
1242 	case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1243 	case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
1244 	case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
1245 	case FCPIO_RESET_CMPL: /* fw completed reset */
1246 		atomic64_dec(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1247 		break;
1248 	default:
1249 		break;
1250 	}
1251 
1252 	switch (desc->hdr.type) {
1253 	case FCPIO_ACK: /* fw copied copy wq desc to its queue */
1254 		fnic_fcpio_ack_handler(fnic, cq_index, desc);
1255 		break;
1256 
1257 	case FCPIO_ICMND_CMPL: /* fw completed a command */
1258 		fnic_fcpio_icmnd_cmpl_handler(fnic, desc);
1259 		break;
1260 
1261 	case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1262 		fnic_fcpio_itmf_cmpl_handler(fnic, desc);
1263 		break;
1264 
1265 	case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
1266 	case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
1267 		fnic_fcpio_flogi_reg_cmpl_handler(fnic, desc);
1268 		break;
1269 
1270 	case FCPIO_RESET_CMPL: /* fw completed reset */
1271 		fnic_fcpio_fw_reset_cmpl_handler(fnic, desc);
1272 		break;
1273 
1274 	default:
1275 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1276 			      "firmware completion type %d\n",
1277 			      desc->hdr.type);
1278 		break;
1279 	}
1280 
1281 	return 0;
1282 }
1283 
1284 /*
1285  * fnic_wq_copy_cmpl_handler
1286  * Routine to process wq copy
1287  */
fnic_wq_copy_cmpl_handler(struct fnic * fnic,int copy_work_to_do)1288 int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int copy_work_to_do)
1289 {
1290 	unsigned int wq_work_done = 0;
1291 	unsigned int i, cq_index;
1292 	unsigned int cur_work_done;
1293 
1294 	for (i = 0; i < fnic->wq_copy_count; i++) {
1295 		cq_index = i + fnic->raw_wq_count + fnic->rq_count;
1296 		cur_work_done = vnic_cq_copy_service(&fnic->cq[cq_index],
1297 						     fnic_fcpio_cmpl_handler,
1298 						     copy_work_to_do);
1299 		wq_work_done += cur_work_done;
1300 	}
1301 	return wq_work_done;
1302 }
1303 
fnic_cleanup_io(struct fnic * fnic,int exclude_id)1304 static void fnic_cleanup_io(struct fnic *fnic, int exclude_id)
1305 {
1306 	int i;
1307 	struct fnic_io_req *io_req;
1308 	unsigned long flags = 0;
1309 	struct scsi_cmnd *sc;
1310 	spinlock_t *io_lock;
1311 	unsigned long start_time = 0;
1312 	struct fnic_stats *fnic_stats = &fnic->fnic_stats;
1313 
1314 	for (i = 0; i < fnic->fnic_max_tag_id; i++) {
1315 		if (i == exclude_id)
1316 			continue;
1317 
1318 		io_lock = fnic_io_lock_tag(fnic, i);
1319 		spin_lock_irqsave(io_lock, flags);
1320 		sc = scsi_host_find_tag(fnic->lport->host, i);
1321 		if (!sc) {
1322 			spin_unlock_irqrestore(io_lock, flags);
1323 			continue;
1324 		}
1325 
1326 		io_req = (struct fnic_io_req *)CMD_SP(sc);
1327 		if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1328 			!(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
1329 			/*
1330 			 * We will be here only when FW completes reset
1331 			 * without sending completions for outstanding ios.
1332 			 */
1333 			CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1334 			if (io_req && io_req->dr_done)
1335 				complete(io_req->dr_done);
1336 			else if (io_req && io_req->abts_done)
1337 				complete(io_req->abts_done);
1338 			spin_unlock_irqrestore(io_lock, flags);
1339 			continue;
1340 		} else if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1341 			spin_unlock_irqrestore(io_lock, flags);
1342 			continue;
1343 		}
1344 		if (!io_req) {
1345 			spin_unlock_irqrestore(io_lock, flags);
1346 			goto cleanup_scsi_cmd;
1347 		}
1348 
1349 		CMD_SP(sc) = NULL;
1350 
1351 		spin_unlock_irqrestore(io_lock, flags);
1352 
1353 		/*
1354 		 * If there is a scsi_cmnd associated with this io_req, then
1355 		 * free the corresponding state
1356 		 */
1357 		start_time = io_req->start_time;
1358 		fnic_release_ioreq_buf(fnic, io_req, sc);
1359 		mempool_free(io_req, fnic->io_req_pool);
1360 
1361 cleanup_scsi_cmd:
1362 		sc->result = DID_TRANSPORT_DISRUPTED << 16;
1363 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1364 			      "%s: sc duration = %lu DID_TRANSPORT_DISRUPTED\n",
1365 			      __func__, (jiffies - start_time));
1366 
1367 		if (atomic64_read(&fnic->io_cmpl_skip))
1368 			atomic64_dec(&fnic->io_cmpl_skip);
1369 		else
1370 			atomic64_inc(&fnic_stats->io_stats.io_completions);
1371 
1372 		/* Complete the command to SCSI */
1373 		if (sc->scsi_done) {
1374 			FNIC_TRACE(fnic_cleanup_io,
1375 				  sc->device->host->host_no, i, sc,
1376 				  jiffies_to_msecs(jiffies - start_time),
1377 				  0, ((u64)sc->cmnd[0] << 32 |
1378 				  (u64)sc->cmnd[2] << 24 |
1379 				  (u64)sc->cmnd[3] << 16 |
1380 				  (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1381 				  (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1382 
1383 			sc->scsi_done(sc);
1384 		}
1385 	}
1386 }
1387 
fnic_wq_copy_cleanup_handler(struct vnic_wq_copy * wq,struct fcpio_host_req * desc)1388 void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
1389 				  struct fcpio_host_req *desc)
1390 {
1391 	u32 id;
1392 	struct fnic *fnic = vnic_dev_priv(wq->vdev);
1393 	struct fnic_io_req *io_req;
1394 	struct scsi_cmnd *sc;
1395 	unsigned long flags;
1396 	spinlock_t *io_lock;
1397 	unsigned long start_time = 0;
1398 
1399 	/* get the tag reference */
1400 	fcpio_tag_id_dec(&desc->hdr.tag, &id);
1401 	id &= FNIC_TAG_MASK;
1402 
1403 	if (id >= fnic->fnic_max_tag_id)
1404 		return;
1405 
1406 	sc = scsi_host_find_tag(fnic->lport->host, id);
1407 	if (!sc)
1408 		return;
1409 
1410 	io_lock = fnic_io_lock_hash(fnic, sc);
1411 	spin_lock_irqsave(io_lock, flags);
1412 
1413 	/* Get the IO context which this desc refers to */
1414 	io_req = (struct fnic_io_req *)CMD_SP(sc);
1415 
1416 	/* fnic interrupts are turned off by now */
1417 
1418 	if (!io_req) {
1419 		spin_unlock_irqrestore(io_lock, flags);
1420 		goto wq_copy_cleanup_scsi_cmd;
1421 	}
1422 
1423 	CMD_SP(sc) = NULL;
1424 
1425 	spin_unlock_irqrestore(io_lock, flags);
1426 
1427 	start_time = io_req->start_time;
1428 	fnic_release_ioreq_buf(fnic, io_req, sc);
1429 	mempool_free(io_req, fnic->io_req_pool);
1430 
1431 wq_copy_cleanup_scsi_cmd:
1432 	sc->result = DID_NO_CONNECT << 16;
1433 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "wq_copy_cleanup_handler:"
1434 		      " DID_NO_CONNECT\n");
1435 
1436 	if (sc->scsi_done) {
1437 		FNIC_TRACE(fnic_wq_copy_cleanup_handler,
1438 			  sc->device->host->host_no, id, sc,
1439 			  jiffies_to_msecs(jiffies - start_time),
1440 			  0, ((u64)sc->cmnd[0] << 32 |
1441 			  (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1442 			  (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1443 			  (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1444 
1445 		sc->scsi_done(sc);
1446 	}
1447 }
1448 
fnic_queue_abort_io_req(struct fnic * fnic,int tag,u32 task_req,u8 * fc_lun,struct fnic_io_req * io_req)1449 static inline int fnic_queue_abort_io_req(struct fnic *fnic, int tag,
1450 					  u32 task_req, u8 *fc_lun,
1451 					  struct fnic_io_req *io_req)
1452 {
1453 	struct vnic_wq_copy *wq = &fnic->wq_copy[0];
1454 	struct Scsi_Host *host = fnic->lport->host;
1455 	struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
1456 	unsigned long flags;
1457 
1458 	spin_lock_irqsave(host->host_lock, flags);
1459 	if (unlikely(fnic_chk_state_flags_locked(fnic,
1460 						FNIC_FLAGS_IO_BLOCKED))) {
1461 		spin_unlock_irqrestore(host->host_lock, flags);
1462 		return 1;
1463 	} else
1464 		atomic_inc(&fnic->in_flight);
1465 	spin_unlock_irqrestore(host->host_lock, flags);
1466 
1467 	spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
1468 
1469 	if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1470 		free_wq_copy_descs(fnic, wq);
1471 
1472 	if (!vnic_wq_copy_desc_avail(wq)) {
1473 		spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
1474 		atomic_dec(&fnic->in_flight);
1475 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1476 			"fnic_queue_abort_io_req: failure: no descriptors\n");
1477 		atomic64_inc(&misc_stats->abts_cpwq_alloc_failures);
1478 		return 1;
1479 	}
1480 	fnic_queue_wq_copy_desc_itmf(wq, tag | FNIC_TAG_ABORT,
1481 				     0, task_req, tag, fc_lun, io_req->port_id,
1482 				     fnic->config.ra_tov, fnic->config.ed_tov);
1483 
1484 	atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1485 	if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
1486 		  atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
1487 		atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
1488 		  atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
1489 
1490 	spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
1491 	atomic_dec(&fnic->in_flight);
1492 
1493 	return 0;
1494 }
1495 
fnic_rport_exch_reset(struct fnic * fnic,u32 port_id)1496 static void fnic_rport_exch_reset(struct fnic *fnic, u32 port_id)
1497 {
1498 	int tag;
1499 	int abt_tag;
1500 	int term_cnt = 0;
1501 	struct fnic_io_req *io_req;
1502 	spinlock_t *io_lock;
1503 	unsigned long flags;
1504 	struct scsi_cmnd *sc;
1505 	struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
1506 	struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
1507 	struct scsi_lun fc_lun;
1508 	enum fnic_ioreq_state old_ioreq_state;
1509 
1510 	FNIC_SCSI_DBG(KERN_DEBUG,
1511 		      fnic->lport->host,
1512 		      "fnic_rport_exch_reset called portid 0x%06x\n",
1513 		      port_id);
1514 
1515 	if (fnic->in_remove)
1516 		return;
1517 
1518 	for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
1519 		abt_tag = tag;
1520 		io_lock = fnic_io_lock_tag(fnic, tag);
1521 		spin_lock_irqsave(io_lock, flags);
1522 		sc = scsi_host_find_tag(fnic->lport->host, tag);
1523 		if (!sc) {
1524 			spin_unlock_irqrestore(io_lock, flags);
1525 			continue;
1526 		}
1527 
1528 		io_req = (struct fnic_io_req *)CMD_SP(sc);
1529 
1530 		if (!io_req || io_req->port_id != port_id) {
1531 			spin_unlock_irqrestore(io_lock, flags);
1532 			continue;
1533 		}
1534 
1535 		if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1536 			(!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
1537 			FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1538 			"fnic_rport_exch_reset dev rst not pending sc 0x%p\n",
1539 			sc);
1540 			spin_unlock_irqrestore(io_lock, flags);
1541 			continue;
1542 		}
1543 
1544 		/*
1545 		 * Found IO that is still pending with firmware and
1546 		 * belongs to rport that went away
1547 		 */
1548 		if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1549 			spin_unlock_irqrestore(io_lock, flags);
1550 			continue;
1551 		}
1552 		if (io_req->abts_done) {
1553 			shost_printk(KERN_ERR, fnic->lport->host,
1554 			"fnic_rport_exch_reset: io_req->abts_done is set "
1555 			"state is %s\n",
1556 			fnic_ioreq_state_to_str(CMD_STATE(sc)));
1557 		}
1558 
1559 		if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1560 			shost_printk(KERN_ERR, fnic->lport->host,
1561 				  "rport_exch_reset "
1562 				  "IO not yet issued %p tag 0x%x flags "
1563 				  "%x state %d\n",
1564 				  sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1565 		}
1566 		old_ioreq_state = CMD_STATE(sc);
1567 		CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1568 		CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1569 		if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1570 			atomic64_inc(&reset_stats->device_reset_terminates);
1571 			abt_tag = (tag | FNIC_TAG_DEV_RST);
1572 			FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1573 			"fnic_rport_exch_reset dev rst sc 0x%p\n",
1574 			sc);
1575 		}
1576 
1577 		BUG_ON(io_req->abts_done);
1578 
1579 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1580 			      "fnic_rport_reset_exch: Issuing abts\n");
1581 
1582 		spin_unlock_irqrestore(io_lock, flags);
1583 
1584 		/* Now queue the abort command to firmware */
1585 		int_to_scsilun(sc->device->lun, &fc_lun);
1586 
1587 		if (fnic_queue_abort_io_req(fnic, abt_tag,
1588 					    FCPIO_ITMF_ABT_TASK_TERM,
1589 					    fc_lun.scsi_lun, io_req)) {
1590 			/*
1591 			 * Revert the cmd state back to old state, if
1592 			 * it hasn't changed in between. This cmd will get
1593 			 * aborted later by scsi_eh, or cleaned up during
1594 			 * lun reset
1595 			 */
1596 			spin_lock_irqsave(io_lock, flags);
1597 			if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1598 				CMD_STATE(sc) = old_ioreq_state;
1599 			spin_unlock_irqrestore(io_lock, flags);
1600 		} else {
1601 			spin_lock_irqsave(io_lock, flags);
1602 			if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1603 				CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
1604 			else
1605 				CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
1606 			spin_unlock_irqrestore(io_lock, flags);
1607 			atomic64_inc(&term_stats->terminates);
1608 			term_cnt++;
1609 		}
1610 	}
1611 	if (term_cnt > atomic64_read(&term_stats->max_terminates))
1612 		atomic64_set(&term_stats->max_terminates, term_cnt);
1613 
1614 }
1615 
fnic_terminate_rport_io(struct fc_rport * rport)1616 void fnic_terminate_rport_io(struct fc_rport *rport)
1617 {
1618 	int tag;
1619 	int abt_tag;
1620 	int term_cnt = 0;
1621 	struct fnic_io_req *io_req;
1622 	spinlock_t *io_lock;
1623 	unsigned long flags;
1624 	struct scsi_cmnd *sc;
1625 	struct scsi_lun fc_lun;
1626 	struct fc_rport_libfc_priv *rdata;
1627 	struct fc_lport *lport;
1628 	struct fnic *fnic;
1629 	struct fc_rport *cmd_rport;
1630 	struct reset_stats *reset_stats;
1631 	struct terminate_stats *term_stats;
1632 	enum fnic_ioreq_state old_ioreq_state;
1633 
1634 	if (!rport) {
1635 		printk(KERN_ERR "fnic_terminate_rport_io: rport is NULL\n");
1636 		return;
1637 	}
1638 	rdata = rport->dd_data;
1639 
1640 	if (!rdata) {
1641 		printk(KERN_ERR "fnic_terminate_rport_io: rdata is NULL\n");
1642 		return;
1643 	}
1644 	lport = rdata->local_port;
1645 
1646 	if (!lport) {
1647 		printk(KERN_ERR "fnic_terminate_rport_io: lport is NULL\n");
1648 		return;
1649 	}
1650 	fnic = lport_priv(lport);
1651 	FNIC_SCSI_DBG(KERN_DEBUG,
1652 		      fnic->lport->host, "fnic_terminate_rport_io called"
1653 		      " wwpn 0x%llx, wwnn0x%llx, rport 0x%p, portid 0x%06x\n",
1654 		      rport->port_name, rport->node_name, rport,
1655 		      rport->port_id);
1656 
1657 	if (fnic->in_remove)
1658 		return;
1659 
1660 	reset_stats = &fnic->fnic_stats.reset_stats;
1661 	term_stats = &fnic->fnic_stats.term_stats;
1662 
1663 	for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
1664 		abt_tag = tag;
1665 		io_lock = fnic_io_lock_tag(fnic, tag);
1666 		spin_lock_irqsave(io_lock, flags);
1667 		sc = scsi_host_find_tag(fnic->lport->host, tag);
1668 		if (!sc) {
1669 			spin_unlock_irqrestore(io_lock, flags);
1670 			continue;
1671 		}
1672 
1673 		cmd_rport = starget_to_rport(scsi_target(sc->device));
1674 		if (rport != cmd_rport) {
1675 			spin_unlock_irqrestore(io_lock, flags);
1676 			continue;
1677 		}
1678 
1679 		io_req = (struct fnic_io_req *)CMD_SP(sc);
1680 
1681 		if (!io_req || rport != cmd_rport) {
1682 			spin_unlock_irqrestore(io_lock, flags);
1683 			continue;
1684 		}
1685 
1686 		if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1687 			(!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
1688 			FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1689 			"fnic_terminate_rport_io dev rst not pending sc 0x%p\n",
1690 			sc);
1691 			spin_unlock_irqrestore(io_lock, flags);
1692 			continue;
1693 		}
1694 		/*
1695 		 * Found IO that is still pending with firmware and
1696 		 * belongs to rport that went away
1697 		 */
1698 		if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1699 			spin_unlock_irqrestore(io_lock, flags);
1700 			continue;
1701 		}
1702 		if (io_req->abts_done) {
1703 			shost_printk(KERN_ERR, fnic->lport->host,
1704 			"fnic_terminate_rport_io: io_req->abts_done is set "
1705 			"state is %s\n",
1706 			fnic_ioreq_state_to_str(CMD_STATE(sc)));
1707 		}
1708 		if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1709 			FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1710 				  "fnic_terminate_rport_io "
1711 				  "IO not yet issued %p tag 0x%x flags "
1712 				  "%x state %d\n",
1713 				  sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1714 		}
1715 		old_ioreq_state = CMD_STATE(sc);
1716 		CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1717 		CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1718 		if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1719 			atomic64_inc(&reset_stats->device_reset_terminates);
1720 			abt_tag = (tag | FNIC_TAG_DEV_RST);
1721 			FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1722 			"fnic_terminate_rport_io dev rst sc 0x%p\n", sc);
1723 		}
1724 
1725 		BUG_ON(io_req->abts_done);
1726 
1727 		FNIC_SCSI_DBG(KERN_DEBUG,
1728 			      fnic->lport->host,
1729 			      "fnic_terminate_rport_io: Issuing abts\n");
1730 
1731 		spin_unlock_irqrestore(io_lock, flags);
1732 
1733 		/* Now queue the abort command to firmware */
1734 		int_to_scsilun(sc->device->lun, &fc_lun);
1735 
1736 		if (fnic_queue_abort_io_req(fnic, abt_tag,
1737 					    FCPIO_ITMF_ABT_TASK_TERM,
1738 					    fc_lun.scsi_lun, io_req)) {
1739 			/*
1740 			 * Revert the cmd state back to old state, if
1741 			 * it hasn't changed in between. This cmd will get
1742 			 * aborted later by scsi_eh, or cleaned up during
1743 			 * lun reset
1744 			 */
1745 			spin_lock_irqsave(io_lock, flags);
1746 			if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1747 				CMD_STATE(sc) = old_ioreq_state;
1748 			spin_unlock_irqrestore(io_lock, flags);
1749 		} else {
1750 			spin_lock_irqsave(io_lock, flags);
1751 			if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1752 				CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
1753 			else
1754 				CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
1755 			spin_unlock_irqrestore(io_lock, flags);
1756 			atomic64_inc(&term_stats->terminates);
1757 			term_cnt++;
1758 		}
1759 	}
1760 	if (term_cnt > atomic64_read(&term_stats->max_terminates))
1761 		atomic64_set(&term_stats->max_terminates, term_cnt);
1762 
1763 }
1764 
1765 /*
1766  * This function is exported to SCSI for sending abort cmnds.
1767  * A SCSI IO is represented by a io_req in the driver.
1768  * The ioreq is linked to the SCSI Cmd, thus a link with the ULP's IO.
1769  */
fnic_abort_cmd(struct scsi_cmnd * sc)1770 int fnic_abort_cmd(struct scsi_cmnd *sc)
1771 {
1772 	struct fc_lport *lp;
1773 	struct fnic *fnic;
1774 	struct fnic_io_req *io_req = NULL;
1775 	struct fc_rport *rport;
1776 	spinlock_t *io_lock;
1777 	unsigned long flags;
1778 	unsigned long start_time = 0;
1779 	int ret = SUCCESS;
1780 	u32 task_req = 0;
1781 	struct scsi_lun fc_lun;
1782 	struct fnic_stats *fnic_stats;
1783 	struct abort_stats *abts_stats;
1784 	struct terminate_stats *term_stats;
1785 	enum fnic_ioreq_state old_ioreq_state;
1786 	int tag;
1787 	DECLARE_COMPLETION_ONSTACK(tm_done);
1788 
1789 	/* Wait for rport to unblock */
1790 	fc_block_scsi_eh(sc);
1791 
1792 	/* Get local-port, check ready and link up */
1793 	lp = shost_priv(sc->device->host);
1794 
1795 	fnic = lport_priv(lp);
1796 	fnic_stats = &fnic->fnic_stats;
1797 	abts_stats = &fnic->fnic_stats.abts_stats;
1798 	term_stats = &fnic->fnic_stats.term_stats;
1799 
1800 	rport = starget_to_rport(scsi_target(sc->device));
1801 	tag = sc->request->tag;
1802 	FNIC_SCSI_DBG(KERN_DEBUG,
1803 		fnic->lport->host,
1804 		"Abort Cmd called FCID 0x%x, LUN 0x%llx TAG %x flags %x\n",
1805 		rport->port_id, sc->device->lun, tag, CMD_FLAGS(sc));
1806 
1807 	CMD_FLAGS(sc) = FNIC_NO_FLAGS;
1808 
1809 	if (lp->state != LPORT_ST_READY || !(lp->link_up)) {
1810 		ret = FAILED;
1811 		goto fnic_abort_cmd_end;
1812 	}
1813 
1814 	/*
1815 	 * Avoid a race between SCSI issuing the abort and the device
1816 	 * completing the command.
1817 	 *
1818 	 * If the command is already completed by the fw cmpl code,
1819 	 * we just return SUCCESS from here. This means that the abort
1820 	 * succeeded. In the SCSI ML, since the timeout for command has
1821 	 * happened, the completion wont actually complete the command
1822 	 * and it will be considered as an aborted command
1823 	 *
1824 	 * The CMD_SP will not be cleared except while holding io_req_lock.
1825 	 */
1826 	io_lock = fnic_io_lock_hash(fnic, sc);
1827 	spin_lock_irqsave(io_lock, flags);
1828 	io_req = (struct fnic_io_req *)CMD_SP(sc);
1829 	if (!io_req) {
1830 		spin_unlock_irqrestore(io_lock, flags);
1831 		goto fnic_abort_cmd_end;
1832 	}
1833 
1834 	io_req->abts_done = &tm_done;
1835 
1836 	if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1837 		spin_unlock_irqrestore(io_lock, flags);
1838 		goto wait_pending;
1839 	}
1840 	/*
1841 	 * Command is still pending, need to abort it
1842 	 * If the firmware completes the command after this point,
1843 	 * the completion wont be done till mid-layer, since abort
1844 	 * has already started.
1845 	 */
1846 	old_ioreq_state = CMD_STATE(sc);
1847 	CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1848 	CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1849 
1850 	spin_unlock_irqrestore(io_lock, flags);
1851 
1852 	/*
1853 	 * Check readiness of the remote port. If the path to remote
1854 	 * port is up, then send abts to the remote port to terminate
1855 	 * the IO. Else, just locally terminate the IO in the firmware
1856 	 */
1857 	if (fc_remote_port_chkready(rport) == 0)
1858 		task_req = FCPIO_ITMF_ABT_TASK;
1859 	else {
1860 		atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
1861 		task_req = FCPIO_ITMF_ABT_TASK_TERM;
1862 	}
1863 
1864 	/* Now queue the abort command to firmware */
1865 	int_to_scsilun(sc->device->lun, &fc_lun);
1866 
1867 	if (fnic_queue_abort_io_req(fnic, sc->request->tag, task_req,
1868 				    fc_lun.scsi_lun, io_req)) {
1869 		spin_lock_irqsave(io_lock, flags);
1870 		if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1871 			CMD_STATE(sc) = old_ioreq_state;
1872 		io_req = (struct fnic_io_req *)CMD_SP(sc);
1873 		if (io_req)
1874 			io_req->abts_done = NULL;
1875 		spin_unlock_irqrestore(io_lock, flags);
1876 		ret = FAILED;
1877 		goto fnic_abort_cmd_end;
1878 	}
1879 	if (task_req == FCPIO_ITMF_ABT_TASK) {
1880 		CMD_FLAGS(sc) |= FNIC_IO_ABTS_ISSUED;
1881 		atomic64_inc(&fnic_stats->abts_stats.aborts);
1882 	} else {
1883 		CMD_FLAGS(sc) |= FNIC_IO_TERM_ISSUED;
1884 		atomic64_inc(&fnic_stats->term_stats.terminates);
1885 	}
1886 
1887 	/*
1888 	 * We queued an abort IO, wait for its completion.
1889 	 * Once the firmware completes the abort command, it will
1890 	 * wake up this thread.
1891 	 */
1892  wait_pending:
1893 	wait_for_completion_timeout(&tm_done,
1894 				    msecs_to_jiffies
1895 				    (2 * fnic->config.ra_tov +
1896 				     fnic->config.ed_tov));
1897 
1898 	/* Check the abort status */
1899 	spin_lock_irqsave(io_lock, flags);
1900 
1901 	io_req = (struct fnic_io_req *)CMD_SP(sc);
1902 	if (!io_req) {
1903 		atomic64_inc(&fnic_stats->io_stats.ioreq_null);
1904 		spin_unlock_irqrestore(io_lock, flags);
1905 		CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
1906 		ret = FAILED;
1907 		goto fnic_abort_cmd_end;
1908 	}
1909 	io_req->abts_done = NULL;
1910 
1911 	/* fw did not complete abort, timed out */
1912 	if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
1913 		spin_unlock_irqrestore(io_lock, flags);
1914 		if (task_req == FCPIO_ITMF_ABT_TASK) {
1915 			atomic64_inc(&abts_stats->abort_drv_timeouts);
1916 		} else {
1917 			atomic64_inc(&term_stats->terminate_drv_timeouts);
1918 		}
1919 		CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_TIMED_OUT;
1920 		ret = FAILED;
1921 		goto fnic_abort_cmd_end;
1922 	}
1923 
1924 	/* IO out of order */
1925 
1926 	if (!(CMD_FLAGS(sc) & (FNIC_IO_ABORTED | FNIC_IO_DONE))) {
1927 		spin_unlock_irqrestore(io_lock, flags);
1928 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1929 			"Issuing Host reset due to out of order IO\n");
1930 
1931 		if (fnic_host_reset(sc) == FAILED) {
1932 			FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1933 				"fnic_host_reset failed.\n");
1934 		}
1935 		ret = FAILED;
1936 		goto fnic_abort_cmd_end;
1937 	}
1938 
1939 	CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1940 
1941 	start_time = io_req->start_time;
1942 	/*
1943 	 * firmware completed the abort, check the status,
1944 	 * free the io_req if successful. If abort fails,
1945 	 * Device reset will clean the I/O.
1946 	 */
1947 	if (CMD_ABTS_STATUS(sc) == FCPIO_SUCCESS)
1948 		CMD_SP(sc) = NULL;
1949 	else {
1950 		ret = FAILED;
1951 		spin_unlock_irqrestore(io_lock, flags);
1952 		goto fnic_abort_cmd_end;
1953 	}
1954 
1955 	spin_unlock_irqrestore(io_lock, flags);
1956 
1957 	fnic_release_ioreq_buf(fnic, io_req, sc);
1958 	mempool_free(io_req, fnic->io_req_pool);
1959 
1960 	if (sc->scsi_done) {
1961 	/* Call SCSI completion function to complete the IO */
1962 		sc->result = (DID_ABORT << 16);
1963 		sc->scsi_done(sc);
1964 		atomic64_dec(&fnic_stats->io_stats.active_ios);
1965 		if (atomic64_read(&fnic->io_cmpl_skip))
1966 			atomic64_dec(&fnic->io_cmpl_skip);
1967 		else
1968 			atomic64_inc(&fnic_stats->io_stats.io_completions);
1969 	}
1970 
1971 fnic_abort_cmd_end:
1972 	FNIC_TRACE(fnic_abort_cmd, sc->device->host->host_no,
1973 		  sc->request->tag, sc,
1974 		  jiffies_to_msecs(jiffies - start_time),
1975 		  0, ((u64)sc->cmnd[0] << 32 |
1976 		  (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1977 		  (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1978 		  (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1979 
1980 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1981 		      "Returning from abort cmd type %x %s\n", task_req,
1982 		      (ret == SUCCESS) ?
1983 		      "SUCCESS" : "FAILED");
1984 	return ret;
1985 }
1986 
fnic_queue_dr_io_req(struct fnic * fnic,struct scsi_cmnd * sc,struct fnic_io_req * io_req)1987 static inline int fnic_queue_dr_io_req(struct fnic *fnic,
1988 				       struct scsi_cmnd *sc,
1989 				       struct fnic_io_req *io_req)
1990 {
1991 	struct vnic_wq_copy *wq = &fnic->wq_copy[0];
1992 	struct Scsi_Host *host = fnic->lport->host;
1993 	struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
1994 	struct scsi_lun fc_lun;
1995 	int ret = 0;
1996 	unsigned long intr_flags;
1997 
1998 	spin_lock_irqsave(host->host_lock, intr_flags);
1999 	if (unlikely(fnic_chk_state_flags_locked(fnic,
2000 						FNIC_FLAGS_IO_BLOCKED))) {
2001 		spin_unlock_irqrestore(host->host_lock, intr_flags);
2002 		return FAILED;
2003 	} else
2004 		atomic_inc(&fnic->in_flight);
2005 	spin_unlock_irqrestore(host->host_lock, intr_flags);
2006 
2007 	spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
2008 
2009 	if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
2010 		free_wq_copy_descs(fnic, wq);
2011 
2012 	if (!vnic_wq_copy_desc_avail(wq)) {
2013 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2014 			  "queue_dr_io_req failure - no descriptors\n");
2015 		atomic64_inc(&misc_stats->devrst_cpwq_alloc_failures);
2016 		ret = -EAGAIN;
2017 		goto lr_io_req_end;
2018 	}
2019 
2020 	/* fill in the lun info */
2021 	int_to_scsilun(sc->device->lun, &fc_lun);
2022 
2023 	fnic_queue_wq_copy_desc_itmf(wq, sc->request->tag | FNIC_TAG_DEV_RST,
2024 				     0, FCPIO_ITMF_LUN_RESET, SCSI_NO_TAG,
2025 				     fc_lun.scsi_lun, io_req->port_id,
2026 				     fnic->config.ra_tov, fnic->config.ed_tov);
2027 
2028 	atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
2029 	if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
2030 		  atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
2031 		atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
2032 		  atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
2033 
2034 lr_io_req_end:
2035 	spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
2036 	atomic_dec(&fnic->in_flight);
2037 
2038 	return ret;
2039 }
2040 
2041 /*
2042  * Clean up any pending aborts on the lun
2043  * For each outstanding IO on this lun, whose abort is not completed by fw,
2044  * issue a local abort. Wait for abort to complete. Return 0 if all commands
2045  * successfully aborted, 1 otherwise
2046  */
fnic_clean_pending_aborts(struct fnic * fnic,struct scsi_cmnd * lr_sc,bool new_sc)2047 static int fnic_clean_pending_aborts(struct fnic *fnic,
2048 				     struct scsi_cmnd *lr_sc,
2049 					 bool new_sc)
2050 
2051 {
2052 	int tag, abt_tag;
2053 	struct fnic_io_req *io_req;
2054 	spinlock_t *io_lock;
2055 	unsigned long flags;
2056 	int ret = 0;
2057 	struct scsi_cmnd *sc;
2058 	struct scsi_lun fc_lun;
2059 	struct scsi_device *lun_dev = lr_sc->device;
2060 	DECLARE_COMPLETION_ONSTACK(tm_done);
2061 	enum fnic_ioreq_state old_ioreq_state;
2062 
2063 	for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
2064 		io_lock = fnic_io_lock_tag(fnic, tag);
2065 		spin_lock_irqsave(io_lock, flags);
2066 		sc = scsi_host_find_tag(fnic->lport->host, tag);
2067 		/*
2068 		 * ignore this lun reset cmd if issued using new SC
2069 		 * or cmds that do not belong to this lun
2070 		 */
2071 		if (!sc || ((sc == lr_sc) && new_sc) || sc->device != lun_dev) {
2072 			spin_unlock_irqrestore(io_lock, flags);
2073 			continue;
2074 		}
2075 
2076 		io_req = (struct fnic_io_req *)CMD_SP(sc);
2077 
2078 		if (!io_req || sc->device != lun_dev) {
2079 			spin_unlock_irqrestore(io_lock, flags);
2080 			continue;
2081 		}
2082 
2083 		/*
2084 		 * Found IO that is still pending with firmware and
2085 		 * belongs to the LUN that we are resetting
2086 		 */
2087 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2088 			      "Found IO in %s on lun\n",
2089 			      fnic_ioreq_state_to_str(CMD_STATE(sc)));
2090 
2091 		if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
2092 			spin_unlock_irqrestore(io_lock, flags);
2093 			continue;
2094 		}
2095 		if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
2096 			(!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
2097 			FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2098 				"%s dev rst not pending sc 0x%p\n", __func__,
2099 				sc);
2100 			spin_unlock_irqrestore(io_lock, flags);
2101 			continue;
2102 		}
2103 
2104 		if (io_req->abts_done)
2105 			shost_printk(KERN_ERR, fnic->lport->host,
2106 			  "%s: io_req->abts_done is set state is %s\n",
2107 			  __func__, fnic_ioreq_state_to_str(CMD_STATE(sc)));
2108 		old_ioreq_state = CMD_STATE(sc);
2109 		/*
2110 		 * Any pending IO issued prior to reset is expected to be
2111 		 * in abts pending state, if not we need to set
2112 		 * FNIC_IOREQ_ABTS_PENDING to indicate the IO is abort pending.
2113 		 * When IO is completed, the IO will be handed over and
2114 		 * handled in this function.
2115 		 */
2116 		CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2117 
2118 		BUG_ON(io_req->abts_done);
2119 
2120 		abt_tag = tag;
2121 		if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
2122 			abt_tag |= FNIC_TAG_DEV_RST;
2123 			FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2124 				  "%s: dev rst sc 0x%p\n", __func__, sc);
2125 		}
2126 
2127 		CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
2128 		io_req->abts_done = &tm_done;
2129 		spin_unlock_irqrestore(io_lock, flags);
2130 
2131 		/* Now queue the abort command to firmware */
2132 		int_to_scsilun(sc->device->lun, &fc_lun);
2133 
2134 		if (fnic_queue_abort_io_req(fnic, abt_tag,
2135 					    FCPIO_ITMF_ABT_TASK_TERM,
2136 					    fc_lun.scsi_lun, io_req)) {
2137 			spin_lock_irqsave(io_lock, flags);
2138 			io_req = (struct fnic_io_req *)CMD_SP(sc);
2139 			if (io_req)
2140 				io_req->abts_done = NULL;
2141 			if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
2142 				CMD_STATE(sc) = old_ioreq_state;
2143 			spin_unlock_irqrestore(io_lock, flags);
2144 			ret = 1;
2145 			goto clean_pending_aborts_end;
2146 		} else {
2147 			spin_lock_irqsave(io_lock, flags);
2148 			if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
2149 				CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2150 			spin_unlock_irqrestore(io_lock, flags);
2151 		}
2152 		CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
2153 
2154 		wait_for_completion_timeout(&tm_done,
2155 					    msecs_to_jiffies
2156 					    (fnic->config.ed_tov));
2157 
2158 		/* Recheck cmd state to check if it is now aborted */
2159 		spin_lock_irqsave(io_lock, flags);
2160 		io_req = (struct fnic_io_req *)CMD_SP(sc);
2161 		if (!io_req) {
2162 			spin_unlock_irqrestore(io_lock, flags);
2163 			CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
2164 			continue;
2165 		}
2166 
2167 		io_req->abts_done = NULL;
2168 
2169 		/* if abort is still pending with fw, fail */
2170 		if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
2171 			spin_unlock_irqrestore(io_lock, flags);
2172 			CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
2173 			ret = 1;
2174 			goto clean_pending_aborts_end;
2175 		}
2176 		CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
2177 
2178 		/* original sc used for lr is handled by dev reset code */
2179 		if (sc != lr_sc)
2180 			CMD_SP(sc) = NULL;
2181 		spin_unlock_irqrestore(io_lock, flags);
2182 
2183 		/* original sc used for lr is handled by dev reset code */
2184 		if (sc != lr_sc) {
2185 			fnic_release_ioreq_buf(fnic, io_req, sc);
2186 			mempool_free(io_req, fnic->io_req_pool);
2187 		}
2188 
2189 		/*
2190 		 * Any IO is returned during reset, it needs to call scsi_done
2191 		 * to return the scsi_cmnd to upper layer.
2192 		 */
2193 		if (sc->scsi_done) {
2194 			/* Set result to let upper SCSI layer retry */
2195 			sc->result = DID_RESET << 16;
2196 			sc->scsi_done(sc);
2197 		}
2198 	}
2199 
2200 	schedule_timeout(msecs_to_jiffies(2 * fnic->config.ed_tov));
2201 
2202 	/* walk again to check, if IOs are still pending in fw */
2203 	if (fnic_is_abts_pending(fnic, lr_sc))
2204 		ret = FAILED;
2205 
2206 clean_pending_aborts_end:
2207 	return ret;
2208 }
2209 
2210 /**
2211  * fnic_scsi_host_start_tag
2212  * Allocates tagid from host's tag list
2213  **/
2214 static inline int
fnic_scsi_host_start_tag(struct fnic * fnic,struct scsi_cmnd * sc)2215 fnic_scsi_host_start_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2216 {
2217 	struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2218 	int tag, ret = SCSI_NO_TAG;
2219 
2220 	BUG_ON(!bqt);
2221 	if (!bqt) {
2222 		pr_err("Tags are not supported\n");
2223 		goto end;
2224 	}
2225 
2226 	do {
2227 		tag = find_next_zero_bit(bqt->tag_map, bqt->max_depth, 1);
2228 		if (tag >= bqt->max_depth) {
2229 			pr_err("Tag allocation failure\n");
2230 			goto end;
2231 		}
2232 	} while (test_and_set_bit(tag, bqt->tag_map));
2233 
2234 	bqt->tag_index[tag] = sc->request;
2235 	sc->request->tag = tag;
2236 	sc->tag = tag;
2237 	if (!sc->request->special)
2238 		sc->request->special = sc;
2239 
2240 	ret = tag;
2241 
2242 end:
2243 	return ret;
2244 }
2245 
2246 /**
2247  * fnic_scsi_host_end_tag
2248  * frees tag allocated by fnic_scsi_host_start_tag.
2249  **/
2250 static inline void
fnic_scsi_host_end_tag(struct fnic * fnic,struct scsi_cmnd * sc)2251 fnic_scsi_host_end_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2252 {
2253 	struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2254 	int tag = sc->request->tag;
2255 
2256 	if (tag == SCSI_NO_TAG)
2257 		return;
2258 
2259 	BUG_ON(!bqt || !bqt->tag_index[tag]);
2260 	if (!bqt)
2261 		return;
2262 
2263 	bqt->tag_index[tag] = NULL;
2264 	clear_bit(tag, bqt->tag_map);
2265 
2266 	return;
2267 }
2268 
2269 /*
2270  * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
2271  * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
2272  * on the LUN.
2273  */
fnic_device_reset(struct scsi_cmnd * sc)2274 int fnic_device_reset(struct scsi_cmnd *sc)
2275 {
2276 	struct fc_lport *lp;
2277 	struct fnic *fnic;
2278 	struct fnic_io_req *io_req = NULL;
2279 	struct fc_rport *rport;
2280 	int status;
2281 	int ret = FAILED;
2282 	spinlock_t *io_lock;
2283 	unsigned long flags;
2284 	unsigned long start_time = 0;
2285 	struct scsi_lun fc_lun;
2286 	struct fnic_stats *fnic_stats;
2287 	struct reset_stats *reset_stats;
2288 	int tag = 0;
2289 	DECLARE_COMPLETION_ONSTACK(tm_done);
2290 	int tag_gen_flag = 0;   /*to track tags allocated by fnic driver*/
2291 	bool new_sc = 0;
2292 
2293 	/* Wait for rport to unblock */
2294 	fc_block_scsi_eh(sc);
2295 
2296 	/* Get local-port, check ready and link up */
2297 	lp = shost_priv(sc->device->host);
2298 
2299 	fnic = lport_priv(lp);
2300 	fnic_stats = &fnic->fnic_stats;
2301 	reset_stats = &fnic->fnic_stats.reset_stats;
2302 
2303 	atomic64_inc(&reset_stats->device_resets);
2304 
2305 	rport = starget_to_rport(scsi_target(sc->device));
2306 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2307 		      "Device reset called FCID 0x%x, LUN 0x%llx sc 0x%p\n",
2308 		      rport->port_id, sc->device->lun, sc);
2309 
2310 	if (lp->state != LPORT_ST_READY || !(lp->link_up))
2311 		goto fnic_device_reset_end;
2312 
2313 	/* Check if remote port up */
2314 	if (fc_remote_port_chkready(rport)) {
2315 		atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
2316 		goto fnic_device_reset_end;
2317 	}
2318 
2319 	CMD_FLAGS(sc) = FNIC_DEVICE_RESET;
2320 	/* Allocate tag if not present */
2321 
2322 	tag = sc->request->tag;
2323 	if (unlikely(tag < 0)) {
2324 		/*
2325 		 * XXX(hch): current the midlayer fakes up a struct
2326 		 * request for the explicit reset ioctls, and those
2327 		 * don't have a tag allocated to them.  The below
2328 		 * code pokes into midlayer structures to paper over
2329 		 * this design issue, but that won't work for blk-mq.
2330 		 *
2331 		 * Either someone who can actually test the hardware
2332 		 * will have to come up with a similar hack for the
2333 		 * blk-mq case, or we'll have to bite the bullet and
2334 		 * fix the way the EH ioctls work for real, but until
2335 		 * that happens we fail these explicit requests here.
2336 		 */
2337 
2338 		tag = fnic_scsi_host_start_tag(fnic, sc);
2339 		if (unlikely(tag == SCSI_NO_TAG))
2340 			goto fnic_device_reset_end;
2341 		tag_gen_flag = 1;
2342 		new_sc = 1;
2343 	}
2344 	io_lock = fnic_io_lock_hash(fnic, sc);
2345 	spin_lock_irqsave(io_lock, flags);
2346 	io_req = (struct fnic_io_req *)CMD_SP(sc);
2347 
2348 	/*
2349 	 * If there is a io_req attached to this command, then use it,
2350 	 * else allocate a new one.
2351 	 */
2352 	if (!io_req) {
2353 		io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
2354 		if (!io_req) {
2355 			spin_unlock_irqrestore(io_lock, flags);
2356 			goto fnic_device_reset_end;
2357 		}
2358 		memset(io_req, 0, sizeof(*io_req));
2359 		io_req->port_id = rport->port_id;
2360 		CMD_SP(sc) = (char *)io_req;
2361 	}
2362 	io_req->dr_done = &tm_done;
2363 	CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
2364 	CMD_LR_STATUS(sc) = FCPIO_INVALID_CODE;
2365 	spin_unlock_irqrestore(io_lock, flags);
2366 
2367 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %x\n", tag);
2368 
2369 	/*
2370 	 * issue the device reset, if enqueue failed, clean up the ioreq
2371 	 * and break assoc with scsi cmd
2372 	 */
2373 	if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
2374 		spin_lock_irqsave(io_lock, flags);
2375 		io_req = (struct fnic_io_req *)CMD_SP(sc);
2376 		if (io_req)
2377 			io_req->dr_done = NULL;
2378 		goto fnic_device_reset_clean;
2379 	}
2380 	spin_lock_irqsave(io_lock, flags);
2381 	CMD_FLAGS(sc) |= FNIC_DEV_RST_ISSUED;
2382 	spin_unlock_irqrestore(io_lock, flags);
2383 
2384 	/*
2385 	 * Wait on the local completion for LUN reset.  The io_req may be
2386 	 * freed while we wait since we hold no lock.
2387 	 */
2388 	wait_for_completion_timeout(&tm_done,
2389 				    msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2390 
2391 	spin_lock_irqsave(io_lock, flags);
2392 	io_req = (struct fnic_io_req *)CMD_SP(sc);
2393 	if (!io_req) {
2394 		spin_unlock_irqrestore(io_lock, flags);
2395 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2396 				"io_req is null tag 0x%x sc 0x%p\n", tag, sc);
2397 		goto fnic_device_reset_end;
2398 	}
2399 	io_req->dr_done = NULL;
2400 
2401 	status = CMD_LR_STATUS(sc);
2402 
2403 	/*
2404 	 * If lun reset not completed, bail out with failed. io_req
2405 	 * gets cleaned up during higher levels of EH
2406 	 */
2407 	if (status == FCPIO_INVALID_CODE) {
2408 		atomic64_inc(&reset_stats->device_reset_timeouts);
2409 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2410 			      "Device reset timed out\n");
2411 		CMD_FLAGS(sc) |= FNIC_DEV_RST_TIMED_OUT;
2412 		spin_unlock_irqrestore(io_lock, flags);
2413 		int_to_scsilun(sc->device->lun, &fc_lun);
2414 		/*
2415 		 * Issue abort and terminate on device reset request.
2416 		 * If q'ing of terminate fails, retry it after a delay.
2417 		 */
2418 		while (1) {
2419 			spin_lock_irqsave(io_lock, flags);
2420 			if (CMD_FLAGS(sc) & FNIC_DEV_RST_TERM_ISSUED) {
2421 				spin_unlock_irqrestore(io_lock, flags);
2422 				break;
2423 			}
2424 			spin_unlock_irqrestore(io_lock, flags);
2425 			if (fnic_queue_abort_io_req(fnic,
2426 				tag | FNIC_TAG_DEV_RST,
2427 				FCPIO_ITMF_ABT_TASK_TERM,
2428 				fc_lun.scsi_lun, io_req)) {
2429 				wait_for_completion_timeout(&tm_done,
2430 				msecs_to_jiffies(FNIC_ABT_TERM_DELAY_TIMEOUT));
2431 			} else {
2432 				spin_lock_irqsave(io_lock, flags);
2433 				CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2434 				CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2435 				io_req->abts_done = &tm_done;
2436 				spin_unlock_irqrestore(io_lock, flags);
2437 				FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2438 				"Abort and terminate issued on Device reset "
2439 				"tag 0x%x sc 0x%p\n", tag, sc);
2440 				break;
2441 			}
2442 		}
2443 		while (1) {
2444 			spin_lock_irqsave(io_lock, flags);
2445 			if (!(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
2446 				spin_unlock_irqrestore(io_lock, flags);
2447 				wait_for_completion_timeout(&tm_done,
2448 				msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2449 				break;
2450 			} else {
2451 				io_req = (struct fnic_io_req *)CMD_SP(sc);
2452 				io_req->abts_done = NULL;
2453 				goto fnic_device_reset_clean;
2454 			}
2455 		}
2456 	} else {
2457 		spin_unlock_irqrestore(io_lock, flags);
2458 	}
2459 
2460 	/* Completed, but not successful, clean up the io_req, return fail */
2461 	if (status != FCPIO_SUCCESS) {
2462 		spin_lock_irqsave(io_lock, flags);
2463 		FNIC_SCSI_DBG(KERN_DEBUG,
2464 			      fnic->lport->host,
2465 			      "Device reset completed - failed\n");
2466 		io_req = (struct fnic_io_req *)CMD_SP(sc);
2467 		goto fnic_device_reset_clean;
2468 	}
2469 
2470 	/*
2471 	 * Clean up any aborts on this lun that have still not
2472 	 * completed. If any of these fail, then LUN reset fails.
2473 	 * clean_pending_aborts cleans all cmds on this lun except
2474 	 * the lun reset cmd. If all cmds get cleaned, the lun reset
2475 	 * succeeds
2476 	 */
2477 	if (fnic_clean_pending_aborts(fnic, sc, new_sc)) {
2478 		spin_lock_irqsave(io_lock, flags);
2479 		io_req = (struct fnic_io_req *)CMD_SP(sc);
2480 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2481 			      "Device reset failed"
2482 			      " since could not abort all IOs\n");
2483 		goto fnic_device_reset_clean;
2484 	}
2485 
2486 	/* Clean lun reset command */
2487 	spin_lock_irqsave(io_lock, flags);
2488 	io_req = (struct fnic_io_req *)CMD_SP(sc);
2489 	if (io_req)
2490 		/* Completed, and successful */
2491 		ret = SUCCESS;
2492 
2493 fnic_device_reset_clean:
2494 	if (io_req)
2495 		CMD_SP(sc) = NULL;
2496 
2497 	spin_unlock_irqrestore(io_lock, flags);
2498 
2499 	if (io_req) {
2500 		start_time = io_req->start_time;
2501 		fnic_release_ioreq_buf(fnic, io_req, sc);
2502 		mempool_free(io_req, fnic->io_req_pool);
2503 	}
2504 
2505 fnic_device_reset_end:
2506 	FNIC_TRACE(fnic_device_reset, sc->device->host->host_no,
2507 		  sc->request->tag, sc,
2508 		  jiffies_to_msecs(jiffies - start_time),
2509 		  0, ((u64)sc->cmnd[0] << 32 |
2510 		  (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
2511 		  (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
2512 		  (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
2513 
2514 	/* free tag if it is allocated */
2515 	if (unlikely(tag_gen_flag))
2516 		fnic_scsi_host_end_tag(fnic, sc);
2517 
2518 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2519 		      "Returning from device reset %s\n",
2520 		      (ret == SUCCESS) ?
2521 		      "SUCCESS" : "FAILED");
2522 
2523 	if (ret == FAILED)
2524 		atomic64_inc(&reset_stats->device_reset_failures);
2525 
2526 	return ret;
2527 }
2528 
2529 /* Clean up all IOs, clean up libFC local port */
fnic_reset(struct Scsi_Host * shost)2530 int fnic_reset(struct Scsi_Host *shost)
2531 {
2532 	struct fc_lport *lp;
2533 	struct fnic *fnic;
2534 	int ret = 0;
2535 	struct reset_stats *reset_stats;
2536 
2537 	lp = shost_priv(shost);
2538 	fnic = lport_priv(lp);
2539 	reset_stats = &fnic->fnic_stats.reset_stats;
2540 
2541 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2542 		      "fnic_reset called\n");
2543 
2544 	atomic64_inc(&reset_stats->fnic_resets);
2545 
2546 	/*
2547 	 * Reset local port, this will clean up libFC exchanges,
2548 	 * reset remote port sessions, and if link is up, begin flogi
2549 	 */
2550 	ret = lp->tt.lport_reset(lp);
2551 
2552 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2553 		      "Returning from fnic reset %s\n",
2554 		      (ret == 0) ?
2555 		      "SUCCESS" : "FAILED");
2556 
2557 	if (ret == 0)
2558 		atomic64_inc(&reset_stats->fnic_reset_completions);
2559 	else
2560 		atomic64_inc(&reset_stats->fnic_reset_failures);
2561 
2562 	return ret;
2563 }
2564 
2565 /*
2566  * SCSI Error handling calls driver's eh_host_reset if all prior
2567  * error handling levels return FAILED. If host reset completes
2568  * successfully, and if link is up, then Fabric login begins.
2569  *
2570  * Host Reset is the highest level of error recovery. If this fails, then
2571  * host is offlined by SCSI.
2572  *
2573  */
fnic_host_reset(struct scsi_cmnd * sc)2574 int fnic_host_reset(struct scsi_cmnd *sc)
2575 {
2576 	int ret;
2577 	unsigned long wait_host_tmo;
2578 	struct Scsi_Host *shost = sc->device->host;
2579 	struct fc_lport *lp = shost_priv(shost);
2580 	struct fnic *fnic = lport_priv(lp);
2581 	unsigned long flags;
2582 
2583 	spin_lock_irqsave(&fnic->fnic_lock, flags);
2584 	if (fnic->internal_reset_inprogress == 0) {
2585 		fnic->internal_reset_inprogress = 1;
2586 	} else {
2587 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2588 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2589 			"host reset in progress skipping another host reset\n");
2590 		return SUCCESS;
2591 	}
2592 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2593 
2594 	/*
2595 	 * If fnic_reset is successful, wait for fabric login to complete
2596 	 * scsi-ml tries to send a TUR to every device if host reset is
2597 	 * successful, so before returning to scsi, fabric should be up
2598 	 */
2599 	ret = (fnic_reset(shost) == 0) ? SUCCESS : FAILED;
2600 	if (ret == SUCCESS) {
2601 		wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
2602 		ret = FAILED;
2603 		while (time_before(jiffies, wait_host_tmo)) {
2604 			if ((lp->state == LPORT_ST_READY) &&
2605 			    (lp->link_up)) {
2606 				ret = SUCCESS;
2607 				break;
2608 			}
2609 			ssleep(1);
2610 		}
2611 	}
2612 
2613 	spin_lock_irqsave(&fnic->fnic_lock, flags);
2614 	fnic->internal_reset_inprogress = 0;
2615 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2616 	return ret;
2617 }
2618 
2619 /*
2620  * This fxn is called from libFC when host is removed
2621  */
fnic_scsi_abort_io(struct fc_lport * lp)2622 void fnic_scsi_abort_io(struct fc_lport *lp)
2623 {
2624 	int err = 0;
2625 	unsigned long flags;
2626 	enum fnic_state old_state;
2627 	struct fnic *fnic = lport_priv(lp);
2628 	DECLARE_COMPLETION_ONSTACK(remove_wait);
2629 
2630 	/* Issue firmware reset for fnic, wait for reset to complete */
2631 retry_fw_reset:
2632 	spin_lock_irqsave(&fnic->fnic_lock, flags);
2633 	if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2634 		/* fw reset is in progress, poll for its completion */
2635 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2636 		schedule_timeout(msecs_to_jiffies(100));
2637 		goto retry_fw_reset;
2638 	}
2639 
2640 	fnic->remove_wait = &remove_wait;
2641 	old_state = fnic->state;
2642 	fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
2643 	fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
2644 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2645 
2646 	err = fnic_fw_reset_handler(fnic);
2647 	if (err) {
2648 		spin_lock_irqsave(&fnic->fnic_lock, flags);
2649 		if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2650 			fnic->state = old_state;
2651 		fnic->remove_wait = NULL;
2652 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2653 		return;
2654 	}
2655 
2656 	/* Wait for firmware reset to complete */
2657 	wait_for_completion_timeout(&remove_wait,
2658 				    msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
2659 
2660 	spin_lock_irqsave(&fnic->fnic_lock, flags);
2661 	fnic->remove_wait = NULL;
2662 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2663 		      "fnic_scsi_abort_io %s\n",
2664 		      (fnic->state == FNIC_IN_ETH_MODE) ?
2665 		      "SUCCESS" : "FAILED");
2666 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2667 
2668 }
2669 
2670 /*
2671  * This fxn called from libFC to clean up driver IO state on link down
2672  */
fnic_scsi_cleanup(struct fc_lport * lp)2673 void fnic_scsi_cleanup(struct fc_lport *lp)
2674 {
2675 	unsigned long flags;
2676 	enum fnic_state old_state;
2677 	struct fnic *fnic = lport_priv(lp);
2678 
2679 	/* issue fw reset */
2680 retry_fw_reset:
2681 	spin_lock_irqsave(&fnic->fnic_lock, flags);
2682 	if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2683 		/* fw reset is in progress, poll for its completion */
2684 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2685 		schedule_timeout(msecs_to_jiffies(100));
2686 		goto retry_fw_reset;
2687 	}
2688 	old_state = fnic->state;
2689 	fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
2690 	fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
2691 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2692 
2693 	if (fnic_fw_reset_handler(fnic)) {
2694 		spin_lock_irqsave(&fnic->fnic_lock, flags);
2695 		if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2696 			fnic->state = old_state;
2697 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2698 	}
2699 
2700 }
2701 
fnic_empty_scsi_cleanup(struct fc_lport * lp)2702 void fnic_empty_scsi_cleanup(struct fc_lport *lp)
2703 {
2704 }
2705 
fnic_exch_mgr_reset(struct fc_lport * lp,u32 sid,u32 did)2706 void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
2707 {
2708 	struct fnic *fnic = lport_priv(lp);
2709 
2710 	/* Non-zero sid, nothing to do */
2711 	if (sid)
2712 		goto call_fc_exch_mgr_reset;
2713 
2714 	if (did) {
2715 		fnic_rport_exch_reset(fnic, did);
2716 		goto call_fc_exch_mgr_reset;
2717 	}
2718 
2719 	/*
2720 	 * sid = 0, did = 0
2721 	 * link down or device being removed
2722 	 */
2723 	if (!fnic->in_remove)
2724 		fnic_scsi_cleanup(lp);
2725 	else
2726 		fnic_scsi_abort_io(lp);
2727 
2728 	/* call libFC exch mgr reset to reset its exchanges */
2729 call_fc_exch_mgr_reset:
2730 	fc_exch_mgr_reset(lp, sid, did);
2731 
2732 }
2733 
2734 /*
2735  * fnic_is_abts_pending() is a helper function that
2736  * walks through tag map to check if there is any IOs pending,if there is one,
2737  * then it returns 1 (true), otherwise 0 (false)
2738  * if @lr_sc is non NULL, then it checks IOs specific to particular LUN,
2739  * otherwise, it checks for all IOs.
2740  */
fnic_is_abts_pending(struct fnic * fnic,struct scsi_cmnd * lr_sc)2741 int fnic_is_abts_pending(struct fnic *fnic, struct scsi_cmnd *lr_sc)
2742 {
2743 	int tag;
2744 	struct fnic_io_req *io_req;
2745 	spinlock_t *io_lock;
2746 	unsigned long flags;
2747 	int ret = 0;
2748 	struct scsi_cmnd *sc;
2749 	struct scsi_device *lun_dev = NULL;
2750 
2751 	if (lr_sc)
2752 		lun_dev = lr_sc->device;
2753 
2754 	/* walk again to check, if IOs are still pending in fw */
2755 	for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
2756 		sc = scsi_host_find_tag(fnic->lport->host, tag);
2757 		/*
2758 		 * ignore this lun reset cmd or cmds that do not belong to
2759 		 * this lun
2760 		 */
2761 		if (!sc || (lr_sc && (sc->device != lun_dev || sc == lr_sc)))
2762 			continue;
2763 
2764 		io_lock = fnic_io_lock_hash(fnic, sc);
2765 		spin_lock_irqsave(io_lock, flags);
2766 
2767 		io_req = (struct fnic_io_req *)CMD_SP(sc);
2768 
2769 		if (!io_req || sc->device != lun_dev) {
2770 			spin_unlock_irqrestore(io_lock, flags);
2771 			continue;
2772 		}
2773 
2774 		/*
2775 		 * Found IO that is still pending with firmware and
2776 		 * belongs to the LUN that we are resetting
2777 		 */
2778 		FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2779 			      "Found IO in %s on lun\n",
2780 			      fnic_ioreq_state_to_str(CMD_STATE(sc)));
2781 
2782 		if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
2783 			ret = 1;
2784 		spin_unlock_irqrestore(io_lock, flags);
2785 	}
2786 
2787 	return ret;
2788 }
2789