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