xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_scsi.c (revision 76a4f7cc)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2021 Broadcom. All Rights Reserved. The term *
5  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
6  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
7  * EMULEX and SLI are trademarks of Emulex.                        *
8  * www.broadcom.com                                                *
9  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
10  *                                                                 *
11  * This program is free software; you can redistribute it and/or   *
12  * modify it under the terms of version 2 of the GNU General       *
13  * Public License as published by the Free Software Foundation.    *
14  * This program is distributed in the hope that it will be useful. *
15  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
16  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
17  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
18  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
20  * more details, a copy of which can be found in the file COPYING  *
21  * included with this package.                                     *
22  *******************************************************************/
23 #include <linux/pci.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.h>
26 #include <linux/export.h>
27 #include <linux/delay.h>
28 #include <asm/unaligned.h>
29 #include <linux/t10-pi.h>
30 #include <linux/crc-t10dif.h>
31 #include <linux/blk-cgroup.h>
32 #include <net/checksum.h>
33 
34 #include <scsi/scsi.h>
35 #include <scsi/scsi_device.h>
36 #include <scsi/scsi_eh.h>
37 #include <scsi/scsi_host.h>
38 #include <scsi/scsi_tcq.h>
39 #include <scsi/scsi_transport_fc.h>
40 
41 #include "lpfc_version.h"
42 #include "lpfc_hw4.h"
43 #include "lpfc_hw.h"
44 #include "lpfc_sli.h"
45 #include "lpfc_sli4.h"
46 #include "lpfc_nl.h"
47 #include "lpfc_disc.h"
48 #include "lpfc.h"
49 #include "lpfc_scsi.h"
50 #include "lpfc_logmsg.h"
51 #include "lpfc_crtn.h"
52 #include "lpfc_vport.h"
53 
54 #define LPFC_RESET_WAIT  2
55 #define LPFC_ABORT_WAIT  2
56 
57 static char *dif_op_str[] = {
58 	"PROT_NORMAL",
59 	"PROT_READ_INSERT",
60 	"PROT_WRITE_STRIP",
61 	"PROT_READ_STRIP",
62 	"PROT_WRITE_INSERT",
63 	"PROT_READ_PASS",
64 	"PROT_WRITE_PASS",
65 };
66 
67 struct scsi_dif_tuple {
68 	__be16 guard_tag;       /* Checksum */
69 	__be16 app_tag;         /* Opaque storage */
70 	__be32 ref_tag;         /* Target LBA or indirect LBA */
71 };
72 
73 static struct lpfc_rport_data *
74 lpfc_rport_data_from_scsi_device(struct scsi_device *sdev)
75 {
76 	struct lpfc_vport *vport = (struct lpfc_vport *)sdev->host->hostdata;
77 
78 	if (vport->phba->cfg_fof)
79 		return ((struct lpfc_device_data *)sdev->hostdata)->rport_data;
80 	else
81 		return (struct lpfc_rport_data *)sdev->hostdata;
82 }
83 
84 static void
85 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *psb);
86 static void
87 lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *psb);
88 static int
89 lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc);
90 static void
91 lpfc_put_vmid_in_hashtable(struct lpfc_vport *vport, u32 hash,
92 			   struct lpfc_vmid *vmp);
93 static void lpfc_vmid_update_entry(struct lpfc_vport *vport, struct scsi_cmnd
94 				   *cmd, struct lpfc_vmid *vmp,
95 				   union lpfc_vmid_io_tag *tag);
96 static void lpfc_vmid_assign_cs_ctl(struct lpfc_vport *vport,
97 				    struct lpfc_vmid *vmid);
98 
99 /**
100  * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
101  * @phba: Pointer to HBA object.
102  * @lpfc_cmd: lpfc scsi command object pointer.
103  *
104  * This function is called from the lpfc_prep_task_mgmt_cmd function to
105  * set the last bit in the response sge entry.
106  **/
107 static void
108 lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
109 				struct lpfc_io_buf *lpfc_cmd)
110 {
111 	struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
112 	if (sgl) {
113 		sgl += 1;
114 		sgl->word2 = le32_to_cpu(sgl->word2);
115 		bf_set(lpfc_sli4_sge_last, sgl, 1);
116 		sgl->word2 = cpu_to_le32(sgl->word2);
117 	}
118 }
119 
120 #define LPFC_INVALID_REFTAG ((u32)-1)
121 
122 /**
123  * lpfc_update_stats - Update statistical data for the command completion
124  * @vport: The virtual port on which this call is executing.
125  * @lpfc_cmd: lpfc scsi command object pointer.
126  *
127  * This function is called when there is a command completion and this
128  * function updates the statistical data for the command completion.
129  **/
130 static void
131 lpfc_update_stats(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd)
132 {
133 	struct lpfc_hba *phba = vport->phba;
134 	struct lpfc_rport_data *rdata;
135 	struct lpfc_nodelist *pnode;
136 	struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
137 	unsigned long flags;
138 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
139 	unsigned long latency;
140 	int i;
141 
142 	if (!vport->stat_data_enabled ||
143 	    vport->stat_data_blocked ||
144 	    (cmd->result))
145 		return;
146 
147 	latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
148 	rdata = lpfc_cmd->rdata;
149 	pnode = rdata->pnode;
150 
151 	spin_lock_irqsave(shost->host_lock, flags);
152 	if (!pnode ||
153 	    !pnode->lat_data ||
154 	    (phba->bucket_type == LPFC_NO_BUCKET)) {
155 		spin_unlock_irqrestore(shost->host_lock, flags);
156 		return;
157 	}
158 
159 	if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
160 		i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
161 			phba->bucket_step;
162 		/* check array subscript bounds */
163 		if (i < 0)
164 			i = 0;
165 		else if (i >= LPFC_MAX_BUCKET_COUNT)
166 			i = LPFC_MAX_BUCKET_COUNT - 1;
167 	} else {
168 		for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
169 			if (latency <= (phba->bucket_base +
170 				((1<<i)*phba->bucket_step)))
171 				break;
172 	}
173 
174 	pnode->lat_data[i].cmd_count++;
175 	spin_unlock_irqrestore(shost->host_lock, flags);
176 }
177 
178 /**
179  * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
180  * @phba: The Hba for which this call is being executed.
181  *
182  * This routine is called when there is resource error in driver or firmware.
183  * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
184  * posts at most 1 event each second. This routine wakes up worker thread of
185  * @phba to process WORKER_RAM_DOWN_EVENT event.
186  *
187  * This routine should be called with no lock held.
188  **/
189 void
190 lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
191 {
192 	unsigned long flags;
193 	uint32_t evt_posted;
194 	unsigned long expires;
195 
196 	spin_lock_irqsave(&phba->hbalock, flags);
197 	atomic_inc(&phba->num_rsrc_err);
198 	phba->last_rsrc_error_time = jiffies;
199 
200 	expires = phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL;
201 	if (time_after(expires, jiffies)) {
202 		spin_unlock_irqrestore(&phba->hbalock, flags);
203 		return;
204 	}
205 
206 	phba->last_ramp_down_time = jiffies;
207 
208 	spin_unlock_irqrestore(&phba->hbalock, flags);
209 
210 	spin_lock_irqsave(&phba->pport->work_port_lock, flags);
211 	evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
212 	if (!evt_posted)
213 		phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
214 	spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
215 
216 	if (!evt_posted)
217 		lpfc_worker_wake_up(phba);
218 	return;
219 }
220 
221 /**
222  * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
223  * @phba: The Hba for which this call is being executed.
224  *
225  * This routine is called to  process WORKER_RAMP_DOWN_QUEUE event for worker
226  * thread.This routine reduces queue depth for all scsi device on each vport
227  * associated with @phba.
228  **/
229 void
230 lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
231 {
232 	struct lpfc_vport **vports;
233 	struct Scsi_Host  *shost;
234 	struct scsi_device *sdev;
235 	unsigned long new_queue_depth;
236 	unsigned long num_rsrc_err, num_cmd_success;
237 	int i;
238 
239 	num_rsrc_err = atomic_read(&phba->num_rsrc_err);
240 	num_cmd_success = atomic_read(&phba->num_cmd_success);
241 
242 	/*
243 	 * The error and success command counters are global per
244 	 * driver instance.  If another handler has already
245 	 * operated on this error event, just exit.
246 	 */
247 	if (num_rsrc_err == 0)
248 		return;
249 
250 	vports = lpfc_create_vport_work_array(phba);
251 	if (vports != NULL)
252 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
253 			shost = lpfc_shost_from_vport(vports[i]);
254 			shost_for_each_device(sdev, shost) {
255 				new_queue_depth =
256 					sdev->queue_depth * num_rsrc_err /
257 					(num_rsrc_err + num_cmd_success);
258 				if (!new_queue_depth)
259 					new_queue_depth = sdev->queue_depth - 1;
260 				else
261 					new_queue_depth = sdev->queue_depth -
262 								new_queue_depth;
263 				scsi_change_queue_depth(sdev, new_queue_depth);
264 			}
265 		}
266 	lpfc_destroy_vport_work_array(phba, vports);
267 	atomic_set(&phba->num_rsrc_err, 0);
268 	atomic_set(&phba->num_cmd_success, 0);
269 }
270 
271 /**
272  * lpfc_scsi_dev_block - set all scsi hosts to block state
273  * @phba: Pointer to HBA context object.
274  *
275  * This function walks vport list and set each SCSI host to block state
276  * by invoking fc_remote_port_delete() routine. This function is invoked
277  * with EEH when device's PCI slot has been permanently disabled.
278  **/
279 void
280 lpfc_scsi_dev_block(struct lpfc_hba *phba)
281 {
282 	struct lpfc_vport **vports;
283 	struct Scsi_Host  *shost;
284 	struct scsi_device *sdev;
285 	struct fc_rport *rport;
286 	int i;
287 
288 	vports = lpfc_create_vport_work_array(phba);
289 	if (vports != NULL)
290 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
291 			shost = lpfc_shost_from_vport(vports[i]);
292 			shost_for_each_device(sdev, shost) {
293 				rport = starget_to_rport(scsi_target(sdev));
294 				fc_remote_port_delete(rport);
295 			}
296 		}
297 	lpfc_destroy_vport_work_array(phba, vports);
298 }
299 
300 /**
301  * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
302  * @vport: The virtual port for which this call being executed.
303  * @num_to_alloc: The requested number of buffers to allocate.
304  *
305  * This routine allocates a scsi buffer for device with SLI-3 interface spec,
306  * the scsi buffer contains all the necessary information needed to initiate
307  * a SCSI I/O. The non-DMAable buffer region contains information to build
308  * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
309  * and the initial BPL. In addition to allocating memory, the FCP CMND and
310  * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
311  *
312  * Return codes:
313  *   int - number of scsi buffers that were allocated.
314  *   0 = failure, less than num_to_alloc is a partial failure.
315  **/
316 static int
317 lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
318 {
319 	struct lpfc_hba *phba = vport->phba;
320 	struct lpfc_io_buf *psb;
321 	struct ulp_bde64 *bpl;
322 	IOCB_t *iocb;
323 	dma_addr_t pdma_phys_fcp_cmd;
324 	dma_addr_t pdma_phys_fcp_rsp;
325 	dma_addr_t pdma_phys_sgl;
326 	uint16_t iotag;
327 	int bcnt, bpl_size;
328 
329 	bpl_size = phba->cfg_sg_dma_buf_size -
330 		(sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
331 
332 	lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
333 			 "9067 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
334 			 num_to_alloc, phba->cfg_sg_dma_buf_size,
335 			 (int)sizeof(struct fcp_cmnd),
336 			 (int)sizeof(struct fcp_rsp), bpl_size);
337 
338 	for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
339 		psb = kzalloc(sizeof(struct lpfc_io_buf), GFP_KERNEL);
340 		if (!psb)
341 			break;
342 
343 		/*
344 		 * Get memory from the pci pool to map the virt space to pci
345 		 * bus space for an I/O.  The DMA buffer includes space for the
346 		 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
347 		 * necessary to support the sg_tablesize.
348 		 */
349 		psb->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
350 					GFP_KERNEL, &psb->dma_handle);
351 		if (!psb->data) {
352 			kfree(psb);
353 			break;
354 		}
355 
356 
357 		/* Allocate iotag for psb->cur_iocbq. */
358 		iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
359 		if (iotag == 0) {
360 			dma_pool_free(phba->lpfc_sg_dma_buf_pool,
361 				      psb->data, psb->dma_handle);
362 			kfree(psb);
363 			break;
364 		}
365 		psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
366 
367 		psb->fcp_cmnd = psb->data;
368 		psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
369 		psb->dma_sgl = psb->data + sizeof(struct fcp_cmnd) +
370 			sizeof(struct fcp_rsp);
371 
372 		/* Initialize local short-hand pointers. */
373 		bpl = (struct ulp_bde64 *)psb->dma_sgl;
374 		pdma_phys_fcp_cmd = psb->dma_handle;
375 		pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
376 		pdma_phys_sgl = psb->dma_handle + sizeof(struct fcp_cmnd) +
377 			sizeof(struct fcp_rsp);
378 
379 		/*
380 		 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
381 		 * are sg list bdes.  Initialize the first two and leave the
382 		 * rest for queuecommand.
383 		 */
384 		bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
385 		bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
386 		bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
387 		bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
388 		bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
389 
390 		/* Setup the physical region for the FCP RSP */
391 		bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
392 		bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
393 		bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
394 		bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
395 		bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
396 
397 		/*
398 		 * Since the IOCB for the FCP I/O is built into this
399 		 * lpfc_scsi_buf, initialize it with all known data now.
400 		 */
401 		iocb = &psb->cur_iocbq.iocb;
402 		iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
403 		if ((phba->sli_rev == 3) &&
404 				!(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
405 			/* fill in immediate fcp command BDE */
406 			iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
407 			iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
408 			iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
409 					unsli3.fcp_ext.icd);
410 			iocb->un.fcpi64.bdl.addrHigh = 0;
411 			iocb->ulpBdeCount = 0;
412 			iocb->ulpLe = 0;
413 			/* fill in response BDE */
414 			iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
415 							BUFF_TYPE_BDE_64;
416 			iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
417 				sizeof(struct fcp_rsp);
418 			iocb->unsli3.fcp_ext.rbde.addrLow =
419 				putPaddrLow(pdma_phys_fcp_rsp);
420 			iocb->unsli3.fcp_ext.rbde.addrHigh =
421 				putPaddrHigh(pdma_phys_fcp_rsp);
422 		} else {
423 			iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
424 			iocb->un.fcpi64.bdl.bdeSize =
425 					(2 * sizeof(struct ulp_bde64));
426 			iocb->un.fcpi64.bdl.addrLow =
427 					putPaddrLow(pdma_phys_sgl);
428 			iocb->un.fcpi64.bdl.addrHigh =
429 					putPaddrHigh(pdma_phys_sgl);
430 			iocb->ulpBdeCount = 1;
431 			iocb->ulpLe = 1;
432 		}
433 		iocb->ulpClass = CLASS3;
434 		psb->status = IOSTAT_SUCCESS;
435 		/* Put it back into the SCSI buffer list */
436 		psb->cur_iocbq.context1  = psb;
437 		spin_lock_init(&psb->buf_lock);
438 		lpfc_release_scsi_buf_s3(phba, psb);
439 
440 	}
441 
442 	return bcnt;
443 }
444 
445 /**
446  * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
447  * @vport: pointer to lpfc vport data structure.
448  *
449  * This routine is invoked by the vport cleanup for deletions and the cleanup
450  * for an ndlp on removal.
451  **/
452 void
453 lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
454 {
455 	struct lpfc_hba *phba = vport->phba;
456 	struct lpfc_io_buf *psb, *next_psb;
457 	struct lpfc_sli4_hdw_queue *qp;
458 	unsigned long iflag = 0;
459 	int idx;
460 
461 	if (!(vport->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
462 		return;
463 
464 	spin_lock_irqsave(&phba->hbalock, iflag);
465 	for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
466 		qp = &phba->sli4_hba.hdwq[idx];
467 
468 		spin_lock(&qp->abts_io_buf_list_lock);
469 		list_for_each_entry_safe(psb, next_psb,
470 					 &qp->lpfc_abts_io_buf_list, list) {
471 			if (psb->cur_iocbq.iocb_flag & LPFC_IO_NVME)
472 				continue;
473 
474 			if (psb->rdata && psb->rdata->pnode &&
475 			    psb->rdata->pnode->vport == vport)
476 				psb->rdata = NULL;
477 		}
478 		spin_unlock(&qp->abts_io_buf_list_lock);
479 	}
480 	spin_unlock_irqrestore(&phba->hbalock, iflag);
481 }
482 
483 /**
484  * lpfc_sli4_io_xri_aborted - Fast-path process of fcp xri abort
485  * @phba: pointer to lpfc hba data structure.
486  * @axri: pointer to the fcp xri abort wcqe structure.
487  * @idx: index into hdwq
488  *
489  * This routine is invoked by the worker thread to process a SLI4 fast-path
490  * FCP or NVME aborted xri.
491  **/
492 void
493 lpfc_sli4_io_xri_aborted(struct lpfc_hba *phba,
494 			 struct sli4_wcqe_xri_aborted *axri, int idx)
495 {
496 	u16 xri = 0;
497 	u16 rxid = 0;
498 	struct lpfc_io_buf *psb, *next_psb;
499 	struct lpfc_sli4_hdw_queue *qp;
500 	unsigned long iflag = 0;
501 	struct lpfc_iocbq *iocbq;
502 	int i;
503 	struct lpfc_nodelist *ndlp;
504 	int rrq_empty = 0;
505 	struct lpfc_sli_ring *pring = phba->sli4_hba.els_wq->pring;
506 	struct scsi_cmnd *cmd;
507 	int offline = 0;
508 
509 	if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
510 		return;
511 	offline = pci_channel_offline(phba->pcidev);
512 	if (!offline) {
513 		xri = bf_get(lpfc_wcqe_xa_xri, axri);
514 		rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
515 	}
516 	qp = &phba->sli4_hba.hdwq[idx];
517 	spin_lock_irqsave(&phba->hbalock, iflag);
518 	spin_lock(&qp->abts_io_buf_list_lock);
519 	list_for_each_entry_safe(psb, next_psb,
520 		&qp->lpfc_abts_io_buf_list, list) {
521 		if (offline)
522 			xri = psb->cur_iocbq.sli4_xritag;
523 		if (psb->cur_iocbq.sli4_xritag == xri) {
524 			list_del_init(&psb->list);
525 			psb->flags &= ~LPFC_SBUF_XBUSY;
526 			psb->status = IOSTAT_SUCCESS;
527 			if (psb->cur_iocbq.iocb_flag & LPFC_IO_NVME) {
528 				qp->abts_nvme_io_bufs--;
529 				spin_unlock(&qp->abts_io_buf_list_lock);
530 				spin_unlock_irqrestore(&phba->hbalock, iflag);
531 				if (!offline) {
532 					lpfc_sli4_nvme_xri_aborted(phba, axri,
533 								   psb);
534 					return;
535 				}
536 				lpfc_sli4_nvme_pci_offline_aborted(phba, psb);
537 				spin_lock_irqsave(&phba->hbalock, iflag);
538 				spin_lock(&qp->abts_io_buf_list_lock);
539 				continue;
540 			}
541 			qp->abts_scsi_io_bufs--;
542 			spin_unlock(&qp->abts_io_buf_list_lock);
543 
544 			if (psb->rdata && psb->rdata->pnode)
545 				ndlp = psb->rdata->pnode;
546 			else
547 				ndlp = NULL;
548 
549 			rrq_empty = list_empty(&phba->active_rrq_list);
550 			spin_unlock_irqrestore(&phba->hbalock, iflag);
551 			if (ndlp && !offline) {
552 				lpfc_set_rrq_active(phba, ndlp,
553 					psb->cur_iocbq.sli4_lxritag, rxid, 1);
554 				lpfc_sli4_abts_err_handler(phba, ndlp, axri);
555 			}
556 
557 			if (phba->cfg_fcp_wait_abts_rsp || offline) {
558 				spin_lock_irqsave(&psb->buf_lock, iflag);
559 				cmd = psb->pCmd;
560 				psb->pCmd = NULL;
561 				spin_unlock_irqrestore(&psb->buf_lock, iflag);
562 
563 				/* The sdev is not guaranteed to be valid post
564 				 * scsi_done upcall.
565 				 */
566 				if (cmd)
567 					cmd->scsi_done(cmd);
568 
569 				/*
570 				 * We expect there is an abort thread waiting
571 				 * for command completion wake up the thread.
572 				 */
573 				spin_lock_irqsave(&psb->buf_lock, iflag);
574 				psb->cur_iocbq.iocb_flag &=
575 					~LPFC_DRIVER_ABORTED;
576 				if (psb->waitq)
577 					wake_up(psb->waitq);
578 				spin_unlock_irqrestore(&psb->buf_lock, iflag);
579 			}
580 
581 			lpfc_release_scsi_buf_s4(phba, psb);
582 			if (rrq_empty)
583 				lpfc_worker_wake_up(phba);
584 			if (!offline)
585 				return;
586 			spin_lock_irqsave(&phba->hbalock, iflag);
587 			spin_lock(&qp->abts_io_buf_list_lock);
588 			continue;
589 		}
590 	}
591 	spin_unlock(&qp->abts_io_buf_list_lock);
592 	if (!offline) {
593 		for (i = 1; i <= phba->sli.last_iotag; i++) {
594 			iocbq = phba->sli.iocbq_lookup[i];
595 
596 			if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
597 			    (iocbq->iocb_flag & LPFC_IO_LIBDFC))
598 				continue;
599 			if (iocbq->sli4_xritag != xri)
600 				continue;
601 			psb = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
602 			psb->flags &= ~LPFC_SBUF_XBUSY;
603 			spin_unlock_irqrestore(&phba->hbalock, iflag);
604 			if (!list_empty(&pring->txq))
605 				lpfc_worker_wake_up(phba);
606 			return;
607 		}
608 	}
609 	spin_unlock_irqrestore(&phba->hbalock, iflag);
610 }
611 
612 /**
613  * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
614  * @phba: The HBA for which this call is being executed.
615  * @ndlp: pointer to a node-list data structure.
616  * @cmnd: Pointer to scsi_cmnd data structure.
617  *
618  * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
619  * and returns to caller.
620  *
621  * Return codes:
622  *   NULL - Error
623  *   Pointer to lpfc_scsi_buf - Success
624  **/
625 static struct lpfc_io_buf *
626 lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
627 		     struct scsi_cmnd *cmnd)
628 {
629 	struct lpfc_io_buf *lpfc_cmd = NULL;
630 	struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
631 	unsigned long iflag = 0;
632 
633 	spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
634 	list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_io_buf,
635 			 list);
636 	if (!lpfc_cmd) {
637 		spin_lock(&phba->scsi_buf_list_put_lock);
638 		list_splice(&phba->lpfc_scsi_buf_list_put,
639 			    &phba->lpfc_scsi_buf_list_get);
640 		INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
641 		list_remove_head(scsi_buf_list_get, lpfc_cmd,
642 				 struct lpfc_io_buf, list);
643 		spin_unlock(&phba->scsi_buf_list_put_lock);
644 	}
645 	spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
646 
647 	if (lpfc_ndlp_check_qdepth(phba, ndlp) && lpfc_cmd) {
648 		atomic_inc(&ndlp->cmd_pending);
649 		lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
650 	}
651 	return  lpfc_cmd;
652 }
653 /**
654  * lpfc_get_scsi_buf_s4 - Get a scsi buffer from io_buf_list of the HBA
655  * @phba: The HBA for which this call is being executed.
656  * @ndlp: pointer to a node-list data structure.
657  * @cmnd: Pointer to scsi_cmnd data structure.
658  *
659  * This routine removes a scsi buffer from head of @hdwq io_buf_list
660  * and returns to caller.
661  *
662  * Return codes:
663  *   NULL - Error
664  *   Pointer to lpfc_scsi_buf - Success
665  **/
666 static struct lpfc_io_buf *
667 lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
668 		     struct scsi_cmnd *cmnd)
669 {
670 	struct lpfc_io_buf *lpfc_cmd;
671 	struct lpfc_sli4_hdw_queue *qp;
672 	struct sli4_sge *sgl;
673 	dma_addr_t pdma_phys_fcp_rsp;
674 	dma_addr_t pdma_phys_fcp_cmd;
675 	uint32_t cpu, idx;
676 	int tag;
677 	struct fcp_cmd_rsp_buf *tmp = NULL;
678 
679 	cpu = raw_smp_processor_id();
680 	if (cmnd && phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_HDWQ) {
681 		tag = blk_mq_unique_tag(scsi_cmd_to_rq(cmnd));
682 		idx = blk_mq_unique_tag_to_hwq(tag);
683 	} else {
684 		idx = phba->sli4_hba.cpu_map[cpu].hdwq;
685 	}
686 
687 	lpfc_cmd = lpfc_get_io_buf(phba, ndlp, idx,
688 				   !phba->cfg_xri_rebalancing);
689 	if (!lpfc_cmd) {
690 		qp = &phba->sli4_hba.hdwq[idx];
691 		qp->empty_io_bufs++;
692 		return NULL;
693 	}
694 
695 	/* Setup key fields in buffer that may have been changed
696 	 * if other protocols used this buffer.
697 	 */
698 	lpfc_cmd->cur_iocbq.iocb_flag = LPFC_IO_FCP;
699 	lpfc_cmd->prot_seg_cnt = 0;
700 	lpfc_cmd->seg_cnt = 0;
701 	lpfc_cmd->timeout = 0;
702 	lpfc_cmd->flags = 0;
703 	lpfc_cmd->start_time = jiffies;
704 	lpfc_cmd->waitq = NULL;
705 	lpfc_cmd->cpu = cpu;
706 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
707 	lpfc_cmd->prot_data_type = 0;
708 #endif
709 	tmp = lpfc_get_cmd_rsp_buf_per_hdwq(phba, lpfc_cmd);
710 	if (!tmp) {
711 		lpfc_release_io_buf(phba, lpfc_cmd, lpfc_cmd->hdwq);
712 		return NULL;
713 	}
714 
715 	lpfc_cmd->fcp_cmnd = tmp->fcp_cmnd;
716 	lpfc_cmd->fcp_rsp = tmp->fcp_rsp;
717 
718 	/*
719 	 * The first two SGEs are the FCP_CMD and FCP_RSP.
720 	 * The balance are sg list bdes. Initialize the
721 	 * first two and leave the rest for queuecommand.
722 	 */
723 	sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
724 	pdma_phys_fcp_cmd = tmp->fcp_cmd_rsp_dma_handle;
725 	sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
726 	sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
727 	sgl->word2 = le32_to_cpu(sgl->word2);
728 	bf_set(lpfc_sli4_sge_last, sgl, 0);
729 	sgl->word2 = cpu_to_le32(sgl->word2);
730 	sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
731 	sgl++;
732 
733 	/* Setup the physical region for the FCP RSP */
734 	pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
735 	sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
736 	sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
737 	sgl->word2 = le32_to_cpu(sgl->word2);
738 	bf_set(lpfc_sli4_sge_last, sgl, 1);
739 	sgl->word2 = cpu_to_le32(sgl->word2);
740 	sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
741 
742 	if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
743 		atomic_inc(&ndlp->cmd_pending);
744 		lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
745 	}
746 	return  lpfc_cmd;
747 }
748 /**
749  * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
750  * @phba: The HBA for which this call is being executed.
751  * @ndlp: pointer to a node-list data structure.
752  * @cmnd: Pointer to scsi_cmnd data structure.
753  *
754  * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
755  * and returns to caller.
756  *
757  * Return codes:
758  *   NULL - Error
759  *   Pointer to lpfc_scsi_buf - Success
760  **/
761 static struct lpfc_io_buf*
762 lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
763 		  struct scsi_cmnd *cmnd)
764 {
765 	return  phba->lpfc_get_scsi_buf(phba, ndlp, cmnd);
766 }
767 
768 /**
769  * lpfc_release_scsi_buf_s3 - Return a scsi buffer back to hba scsi buf list
770  * @phba: The Hba for which this call is being executed.
771  * @psb: The scsi buffer which is being released.
772  *
773  * This routine releases @psb scsi buffer by adding it to tail of @phba
774  * lpfc_scsi_buf_list list.
775  **/
776 static void
777 lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
778 {
779 	unsigned long iflag = 0;
780 
781 	psb->seg_cnt = 0;
782 	psb->prot_seg_cnt = 0;
783 
784 	spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
785 	psb->pCmd = NULL;
786 	psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
787 	list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
788 	spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
789 }
790 
791 /**
792  * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
793  * @phba: The Hba for which this call is being executed.
794  * @psb: The scsi buffer which is being released.
795  *
796  * This routine releases @psb scsi buffer by adding it to tail of @hdwq
797  * io_buf_list list. For SLI4 XRI's are tied to the scsi buffer
798  * and cannot be reused for at least RA_TOV amount of time if it was
799  * aborted.
800  **/
801 static void
802 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
803 {
804 	struct lpfc_sli4_hdw_queue *qp;
805 	unsigned long iflag = 0;
806 
807 	psb->seg_cnt = 0;
808 	psb->prot_seg_cnt = 0;
809 
810 	qp = psb->hdwq;
811 	if (psb->flags & LPFC_SBUF_XBUSY) {
812 		spin_lock_irqsave(&qp->abts_io_buf_list_lock, iflag);
813 		if (!phba->cfg_fcp_wait_abts_rsp)
814 			psb->pCmd = NULL;
815 		list_add_tail(&psb->list, &qp->lpfc_abts_io_buf_list);
816 		qp->abts_scsi_io_bufs++;
817 		spin_unlock_irqrestore(&qp->abts_io_buf_list_lock, iflag);
818 	} else {
819 		lpfc_release_io_buf(phba, (struct lpfc_io_buf *)psb, qp);
820 	}
821 }
822 
823 /**
824  * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
825  * @phba: The Hba for which this call is being executed.
826  * @psb: The scsi buffer which is being released.
827  *
828  * This routine releases @psb scsi buffer by adding it to tail of @phba
829  * lpfc_scsi_buf_list list.
830  **/
831 static void
832 lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
833 {
834 	if ((psb->flags & LPFC_SBUF_BUMP_QDEPTH) && psb->ndlp)
835 		atomic_dec(&psb->ndlp->cmd_pending);
836 
837 	psb->flags &= ~LPFC_SBUF_BUMP_QDEPTH;
838 	phba->lpfc_release_scsi_buf(phba, psb);
839 }
840 
841 /**
842  * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
843  * @data: A pointer to the immediate command data portion of the IOCB.
844  * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
845  *
846  * The routine copies the entire FCP command from @fcp_cmnd to @data while
847  * byte swapping the data to big endian format for transmission on the wire.
848  **/
849 static void
850 lpfc_fcpcmd_to_iocb(u8 *data, struct fcp_cmnd *fcp_cmnd)
851 {
852 	int i, j;
853 
854 	for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
855 	     i += sizeof(uint32_t), j++) {
856 		((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
857 	}
858 }
859 
860 /**
861  * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
862  * @phba: The Hba for which this call is being executed.
863  * @lpfc_cmd: The scsi buffer which is going to be mapped.
864  *
865  * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
866  * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
867  * through sg elements and format the bde. This routine also initializes all
868  * IOCB fields which are dependent on scsi command request buffer.
869  *
870  * Return codes:
871  *   1 - Error
872  *   0 - Success
873  **/
874 static int
875 lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
876 {
877 	struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
878 	struct scatterlist *sgel = NULL;
879 	struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
880 	struct ulp_bde64 *bpl = (struct ulp_bde64 *)lpfc_cmd->dma_sgl;
881 	struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
882 	IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
883 	struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
884 	dma_addr_t physaddr;
885 	uint32_t num_bde = 0;
886 	int nseg, datadir = scsi_cmnd->sc_data_direction;
887 
888 	/*
889 	 * There are three possibilities here - use scatter-gather segment, use
890 	 * the single mapping, or neither.  Start the lpfc command prep by
891 	 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
892 	 * data bde entry.
893 	 */
894 	bpl += 2;
895 	if (scsi_sg_count(scsi_cmnd)) {
896 		/*
897 		 * The driver stores the segment count returned from dma_map_sg
898 		 * because this a count of dma-mappings used to map the use_sg
899 		 * pages.  They are not guaranteed to be the same for those
900 		 * architectures that implement an IOMMU.
901 		 */
902 
903 		nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
904 				  scsi_sg_count(scsi_cmnd), datadir);
905 		if (unlikely(!nseg))
906 			return 1;
907 
908 		lpfc_cmd->seg_cnt = nseg;
909 		if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
910 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
911 					"9064 BLKGRD: %s: Too many sg segments"
912 					" from dma_map_sg.  Config %d, seg_cnt"
913 					" %d\n", __func__, phba->cfg_sg_seg_cnt,
914 					lpfc_cmd->seg_cnt);
915 			WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
916 			lpfc_cmd->seg_cnt = 0;
917 			scsi_dma_unmap(scsi_cmnd);
918 			return 2;
919 		}
920 
921 		/*
922 		 * The driver established a maximum scatter-gather segment count
923 		 * during probe that limits the number of sg elements in any
924 		 * single scsi command.  Just run through the seg_cnt and format
925 		 * the bde's.
926 		 * When using SLI-3 the driver will try to fit all the BDEs into
927 		 * the IOCB. If it can't then the BDEs get added to a BPL as it
928 		 * does for SLI-2 mode.
929 		 */
930 		scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
931 			physaddr = sg_dma_address(sgel);
932 			if (phba->sli_rev == 3 &&
933 			    !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
934 			    !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
935 			    nseg <= LPFC_EXT_DATA_BDE_COUNT) {
936 				data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
937 				data_bde->tus.f.bdeSize = sg_dma_len(sgel);
938 				data_bde->addrLow = putPaddrLow(physaddr);
939 				data_bde->addrHigh = putPaddrHigh(physaddr);
940 				data_bde++;
941 			} else {
942 				bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
943 				bpl->tus.f.bdeSize = sg_dma_len(sgel);
944 				bpl->tus.w = le32_to_cpu(bpl->tus.w);
945 				bpl->addrLow =
946 					le32_to_cpu(putPaddrLow(physaddr));
947 				bpl->addrHigh =
948 					le32_to_cpu(putPaddrHigh(physaddr));
949 				bpl++;
950 			}
951 		}
952 	}
953 
954 	/*
955 	 * Finish initializing those IOCB fields that are dependent on the
956 	 * scsi_cmnd request_buffer.  Note that for SLI-2 the bdeSize is
957 	 * explicitly reinitialized and for SLI-3 the extended bde count is
958 	 * explicitly reinitialized since all iocb memory resources are reused.
959 	 */
960 	if (phba->sli_rev == 3 &&
961 	    !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
962 	    !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
963 		if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
964 			/*
965 			 * The extended IOCB format can only fit 3 BDE or a BPL.
966 			 * This I/O has more than 3 BDE so the 1st data bde will
967 			 * be a BPL that is filled in here.
968 			 */
969 			physaddr = lpfc_cmd->dma_handle;
970 			data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
971 			data_bde->tus.f.bdeSize = (num_bde *
972 						   sizeof(struct ulp_bde64));
973 			physaddr += (sizeof(struct fcp_cmnd) +
974 				     sizeof(struct fcp_rsp) +
975 				     (2 * sizeof(struct ulp_bde64)));
976 			data_bde->addrHigh = putPaddrHigh(physaddr);
977 			data_bde->addrLow = putPaddrLow(physaddr);
978 			/* ebde count includes the response bde and data bpl */
979 			iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
980 		} else {
981 			/* ebde count includes the response bde and data bdes */
982 			iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
983 		}
984 	} else {
985 		iocb_cmd->un.fcpi64.bdl.bdeSize =
986 			((num_bde + 2) * sizeof(struct ulp_bde64));
987 		iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
988 	}
989 	fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
990 
991 	/*
992 	 * Due to difference in data length between DIF/non-DIF paths,
993 	 * we need to set word 4 of IOCB here
994 	 */
995 	iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
996 	lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
997 	return 0;
998 }
999 
1000 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1001 
1002 /* Return BG_ERR_INIT if error injection is detected by Initiator */
1003 #define BG_ERR_INIT	0x1
1004 /* Return BG_ERR_TGT if error injection is detected by Target */
1005 #define BG_ERR_TGT	0x2
1006 /* Return BG_ERR_SWAP if swapping CSUM<-->CRC is required for error injection */
1007 #define BG_ERR_SWAP	0x10
1008 /*
1009  * Return BG_ERR_CHECK if disabling Guard/Ref/App checking is required for
1010  * error injection
1011  */
1012 #define BG_ERR_CHECK	0x20
1013 
1014 /**
1015  * lpfc_bg_err_inject - Determine if we should inject an error
1016  * @phba: The Hba for which this call is being executed.
1017  * @sc: The SCSI command to examine
1018  * @reftag: (out) BlockGuard reference tag for transmitted data
1019  * @apptag: (out) BlockGuard application tag for transmitted data
1020  * @new_guard: (in) Value to replace CRC with if needed
1021  *
1022  * Returns BG_ERR_* bit mask or 0 if request ignored
1023  **/
1024 static int
1025 lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1026 		uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1027 {
1028 	struct scatterlist *sgpe; /* s/g prot entry */
1029 	struct lpfc_io_buf *lpfc_cmd = NULL;
1030 	struct scsi_dif_tuple *src = NULL;
1031 	struct lpfc_nodelist *ndlp;
1032 	struct lpfc_rport_data *rdata;
1033 	uint32_t op = scsi_get_prot_op(sc);
1034 	uint32_t blksize;
1035 	uint32_t numblks;
1036 	u32 lba;
1037 	int rc = 0;
1038 	int blockoff = 0;
1039 
1040 	if (op == SCSI_PROT_NORMAL)
1041 		return 0;
1042 
1043 	sgpe = scsi_prot_sglist(sc);
1044 	lba = scsi_prot_ref_tag(sc);
1045 	if (lba == LPFC_INVALID_REFTAG)
1046 		return 0;
1047 
1048 	/* First check if we need to match the LBA */
1049 	if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1050 		blksize = scsi_prot_interval(sc);
1051 		numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1052 
1053 		/* Make sure we have the right LBA if one is specified */
1054 		if (phba->lpfc_injerr_lba < (u64)lba ||
1055 		    (phba->lpfc_injerr_lba >= (u64)(lba + numblks)))
1056 			return 0;
1057 		if (sgpe) {
1058 			blockoff = phba->lpfc_injerr_lba - (u64)lba;
1059 			numblks = sg_dma_len(sgpe) /
1060 				sizeof(struct scsi_dif_tuple);
1061 			if (numblks < blockoff)
1062 				blockoff = numblks;
1063 		}
1064 	}
1065 
1066 	/* Next check if we need to match the remote NPortID or WWPN */
1067 	rdata = lpfc_rport_data_from_scsi_device(sc->device);
1068 	if (rdata && rdata->pnode) {
1069 		ndlp = rdata->pnode;
1070 
1071 		/* Make sure we have the right NPortID if one is specified */
1072 		if (phba->lpfc_injerr_nportid  &&
1073 			(phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1074 			return 0;
1075 
1076 		/*
1077 		 * Make sure we have the right WWPN if one is specified.
1078 		 * wwn[0] should be a non-zero NAA in a good WWPN.
1079 		 */
1080 		if (phba->lpfc_injerr_wwpn.u.wwn[0]  &&
1081 			(memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1082 				sizeof(struct lpfc_name)) != 0))
1083 			return 0;
1084 	}
1085 
1086 	/* Setup a ptr to the protection data if the SCSI host provides it */
1087 	if (sgpe) {
1088 		src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1089 		src += blockoff;
1090 		lpfc_cmd = (struct lpfc_io_buf *)sc->host_scribble;
1091 	}
1092 
1093 	/* Should we change the Reference Tag */
1094 	if (reftag) {
1095 		if (phba->lpfc_injerr_wref_cnt) {
1096 			switch (op) {
1097 			case SCSI_PROT_WRITE_PASS:
1098 				if (src) {
1099 					/*
1100 					 * For WRITE_PASS, force the error
1101 					 * to be sent on the wire. It should
1102 					 * be detected by the Target.
1103 					 * If blockoff != 0 error will be
1104 					 * inserted in middle of the IO.
1105 					 */
1106 
1107 					lpfc_printf_log(phba, KERN_ERR,
1108 							LOG_TRACE_EVENT,
1109 					"9076 BLKGRD: Injecting reftag error: "
1110 					"write lba x%lx + x%x oldrefTag x%x\n",
1111 					(unsigned long)lba, blockoff,
1112 					be32_to_cpu(src->ref_tag));
1113 
1114 					/*
1115 					 * Save the old ref_tag so we can
1116 					 * restore it on completion.
1117 					 */
1118 					if (lpfc_cmd) {
1119 						lpfc_cmd->prot_data_type =
1120 							LPFC_INJERR_REFTAG;
1121 						lpfc_cmd->prot_data_segment =
1122 							src;
1123 						lpfc_cmd->prot_data =
1124 							src->ref_tag;
1125 					}
1126 					src->ref_tag = cpu_to_be32(0xDEADBEEF);
1127 					phba->lpfc_injerr_wref_cnt--;
1128 					if (phba->lpfc_injerr_wref_cnt == 0) {
1129 						phba->lpfc_injerr_nportid = 0;
1130 						phba->lpfc_injerr_lba =
1131 							LPFC_INJERR_LBA_OFF;
1132 						memset(&phba->lpfc_injerr_wwpn,
1133 						  0, sizeof(struct lpfc_name));
1134 					}
1135 					rc = BG_ERR_TGT | BG_ERR_CHECK;
1136 
1137 					break;
1138 				}
1139 				fallthrough;
1140 			case SCSI_PROT_WRITE_INSERT:
1141 				/*
1142 				 * For WRITE_INSERT, force the error
1143 				 * to be sent on the wire. It should be
1144 				 * detected by the Target.
1145 				 */
1146 				/* DEADBEEF will be the reftag on the wire */
1147 				*reftag = 0xDEADBEEF;
1148 				phba->lpfc_injerr_wref_cnt--;
1149 				if (phba->lpfc_injerr_wref_cnt == 0) {
1150 					phba->lpfc_injerr_nportid = 0;
1151 					phba->lpfc_injerr_lba =
1152 					LPFC_INJERR_LBA_OFF;
1153 					memset(&phba->lpfc_injerr_wwpn,
1154 						0, sizeof(struct lpfc_name));
1155 				}
1156 				rc = BG_ERR_TGT | BG_ERR_CHECK;
1157 
1158 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1159 					"9078 BLKGRD: Injecting reftag error: "
1160 					"write lba x%lx\n", (unsigned long)lba);
1161 				break;
1162 			case SCSI_PROT_WRITE_STRIP:
1163 				/*
1164 				 * For WRITE_STRIP and WRITE_PASS,
1165 				 * force the error on data
1166 				 * being copied from SLI-Host to SLI-Port.
1167 				 */
1168 				*reftag = 0xDEADBEEF;
1169 				phba->lpfc_injerr_wref_cnt--;
1170 				if (phba->lpfc_injerr_wref_cnt == 0) {
1171 					phba->lpfc_injerr_nportid = 0;
1172 					phba->lpfc_injerr_lba =
1173 						LPFC_INJERR_LBA_OFF;
1174 					memset(&phba->lpfc_injerr_wwpn,
1175 						0, sizeof(struct lpfc_name));
1176 				}
1177 				rc = BG_ERR_INIT;
1178 
1179 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1180 					"9077 BLKGRD: Injecting reftag error: "
1181 					"write lba x%lx\n", (unsigned long)lba);
1182 				break;
1183 			}
1184 		}
1185 		if (phba->lpfc_injerr_rref_cnt) {
1186 			switch (op) {
1187 			case SCSI_PROT_READ_INSERT:
1188 			case SCSI_PROT_READ_STRIP:
1189 			case SCSI_PROT_READ_PASS:
1190 				/*
1191 				 * For READ_STRIP and READ_PASS, force the
1192 				 * error on data being read off the wire. It
1193 				 * should force an IO error to the driver.
1194 				 */
1195 				*reftag = 0xDEADBEEF;
1196 				phba->lpfc_injerr_rref_cnt--;
1197 				if (phba->lpfc_injerr_rref_cnt == 0) {
1198 					phba->lpfc_injerr_nportid = 0;
1199 					phba->lpfc_injerr_lba =
1200 						LPFC_INJERR_LBA_OFF;
1201 					memset(&phba->lpfc_injerr_wwpn,
1202 						0, sizeof(struct lpfc_name));
1203 				}
1204 				rc = BG_ERR_INIT;
1205 
1206 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1207 					"9079 BLKGRD: Injecting reftag error: "
1208 					"read lba x%lx\n", (unsigned long)lba);
1209 				break;
1210 			}
1211 		}
1212 	}
1213 
1214 	/* Should we change the Application Tag */
1215 	if (apptag) {
1216 		if (phba->lpfc_injerr_wapp_cnt) {
1217 			switch (op) {
1218 			case SCSI_PROT_WRITE_PASS:
1219 				if (src) {
1220 					/*
1221 					 * For WRITE_PASS, force the error
1222 					 * to be sent on the wire. It should
1223 					 * be detected by the Target.
1224 					 * If blockoff != 0 error will be
1225 					 * inserted in middle of the IO.
1226 					 */
1227 
1228 					lpfc_printf_log(phba, KERN_ERR,
1229 							LOG_TRACE_EVENT,
1230 					"9080 BLKGRD: Injecting apptag error: "
1231 					"write lba x%lx + x%x oldappTag x%x\n",
1232 					(unsigned long)lba, blockoff,
1233 					be16_to_cpu(src->app_tag));
1234 
1235 					/*
1236 					 * Save the old app_tag so we can
1237 					 * restore it on completion.
1238 					 */
1239 					if (lpfc_cmd) {
1240 						lpfc_cmd->prot_data_type =
1241 							LPFC_INJERR_APPTAG;
1242 						lpfc_cmd->prot_data_segment =
1243 							src;
1244 						lpfc_cmd->prot_data =
1245 							src->app_tag;
1246 					}
1247 					src->app_tag = cpu_to_be16(0xDEAD);
1248 					phba->lpfc_injerr_wapp_cnt--;
1249 					if (phba->lpfc_injerr_wapp_cnt == 0) {
1250 						phba->lpfc_injerr_nportid = 0;
1251 						phba->lpfc_injerr_lba =
1252 							LPFC_INJERR_LBA_OFF;
1253 						memset(&phba->lpfc_injerr_wwpn,
1254 						  0, sizeof(struct lpfc_name));
1255 					}
1256 					rc = BG_ERR_TGT | BG_ERR_CHECK;
1257 					break;
1258 				}
1259 				fallthrough;
1260 			case SCSI_PROT_WRITE_INSERT:
1261 				/*
1262 				 * For WRITE_INSERT, force the
1263 				 * error to be sent on the wire. It should be
1264 				 * detected by the Target.
1265 				 */
1266 				/* DEAD will be the apptag on the wire */
1267 				*apptag = 0xDEAD;
1268 				phba->lpfc_injerr_wapp_cnt--;
1269 				if (phba->lpfc_injerr_wapp_cnt == 0) {
1270 					phba->lpfc_injerr_nportid = 0;
1271 					phba->lpfc_injerr_lba =
1272 						LPFC_INJERR_LBA_OFF;
1273 					memset(&phba->lpfc_injerr_wwpn,
1274 						0, sizeof(struct lpfc_name));
1275 				}
1276 				rc = BG_ERR_TGT | BG_ERR_CHECK;
1277 
1278 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1279 					"0813 BLKGRD: Injecting apptag error: "
1280 					"write lba x%lx\n", (unsigned long)lba);
1281 				break;
1282 			case SCSI_PROT_WRITE_STRIP:
1283 				/*
1284 				 * For WRITE_STRIP and WRITE_PASS,
1285 				 * force the error on data
1286 				 * being copied from SLI-Host to SLI-Port.
1287 				 */
1288 				*apptag = 0xDEAD;
1289 				phba->lpfc_injerr_wapp_cnt--;
1290 				if (phba->lpfc_injerr_wapp_cnt == 0) {
1291 					phba->lpfc_injerr_nportid = 0;
1292 					phba->lpfc_injerr_lba =
1293 						LPFC_INJERR_LBA_OFF;
1294 					memset(&phba->lpfc_injerr_wwpn,
1295 						0, sizeof(struct lpfc_name));
1296 				}
1297 				rc = BG_ERR_INIT;
1298 
1299 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1300 					"0812 BLKGRD: Injecting apptag error: "
1301 					"write lba x%lx\n", (unsigned long)lba);
1302 				break;
1303 			}
1304 		}
1305 		if (phba->lpfc_injerr_rapp_cnt) {
1306 			switch (op) {
1307 			case SCSI_PROT_READ_INSERT:
1308 			case SCSI_PROT_READ_STRIP:
1309 			case SCSI_PROT_READ_PASS:
1310 				/*
1311 				 * For READ_STRIP and READ_PASS, force the
1312 				 * error on data being read off the wire. It
1313 				 * should force an IO error to the driver.
1314 				 */
1315 				*apptag = 0xDEAD;
1316 				phba->lpfc_injerr_rapp_cnt--;
1317 				if (phba->lpfc_injerr_rapp_cnt == 0) {
1318 					phba->lpfc_injerr_nportid = 0;
1319 					phba->lpfc_injerr_lba =
1320 						LPFC_INJERR_LBA_OFF;
1321 					memset(&phba->lpfc_injerr_wwpn,
1322 						0, sizeof(struct lpfc_name));
1323 				}
1324 				rc = BG_ERR_INIT;
1325 
1326 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1327 					"0814 BLKGRD: Injecting apptag error: "
1328 					"read lba x%lx\n", (unsigned long)lba);
1329 				break;
1330 			}
1331 		}
1332 	}
1333 
1334 
1335 	/* Should we change the Guard Tag */
1336 	if (new_guard) {
1337 		if (phba->lpfc_injerr_wgrd_cnt) {
1338 			switch (op) {
1339 			case SCSI_PROT_WRITE_PASS:
1340 				rc = BG_ERR_CHECK;
1341 				fallthrough;
1342 
1343 			case SCSI_PROT_WRITE_INSERT:
1344 				/*
1345 				 * For WRITE_INSERT, force the
1346 				 * error to be sent on the wire. It should be
1347 				 * detected by the Target.
1348 				 */
1349 				phba->lpfc_injerr_wgrd_cnt--;
1350 				if (phba->lpfc_injerr_wgrd_cnt == 0) {
1351 					phba->lpfc_injerr_nportid = 0;
1352 					phba->lpfc_injerr_lba =
1353 						LPFC_INJERR_LBA_OFF;
1354 					memset(&phba->lpfc_injerr_wwpn,
1355 						0, sizeof(struct lpfc_name));
1356 				}
1357 
1358 				rc |= BG_ERR_TGT | BG_ERR_SWAP;
1359 				/* Signals the caller to swap CRC->CSUM */
1360 
1361 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1362 					"0817 BLKGRD: Injecting guard error: "
1363 					"write lba x%lx\n", (unsigned long)lba);
1364 				break;
1365 			case SCSI_PROT_WRITE_STRIP:
1366 				/*
1367 				 * For WRITE_STRIP and WRITE_PASS,
1368 				 * force the error on data
1369 				 * being copied from SLI-Host to SLI-Port.
1370 				 */
1371 				phba->lpfc_injerr_wgrd_cnt--;
1372 				if (phba->lpfc_injerr_wgrd_cnt == 0) {
1373 					phba->lpfc_injerr_nportid = 0;
1374 					phba->lpfc_injerr_lba =
1375 						LPFC_INJERR_LBA_OFF;
1376 					memset(&phba->lpfc_injerr_wwpn,
1377 						0, sizeof(struct lpfc_name));
1378 				}
1379 
1380 				rc = BG_ERR_INIT | BG_ERR_SWAP;
1381 				/* Signals the caller to swap CRC->CSUM */
1382 
1383 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1384 					"0816 BLKGRD: Injecting guard error: "
1385 					"write lba x%lx\n", (unsigned long)lba);
1386 				break;
1387 			}
1388 		}
1389 		if (phba->lpfc_injerr_rgrd_cnt) {
1390 			switch (op) {
1391 			case SCSI_PROT_READ_INSERT:
1392 			case SCSI_PROT_READ_STRIP:
1393 			case SCSI_PROT_READ_PASS:
1394 				/*
1395 				 * For READ_STRIP and READ_PASS, force the
1396 				 * error on data being read off the wire. It
1397 				 * should force an IO error to the driver.
1398 				 */
1399 				phba->lpfc_injerr_rgrd_cnt--;
1400 				if (phba->lpfc_injerr_rgrd_cnt == 0) {
1401 					phba->lpfc_injerr_nportid = 0;
1402 					phba->lpfc_injerr_lba =
1403 						LPFC_INJERR_LBA_OFF;
1404 					memset(&phba->lpfc_injerr_wwpn,
1405 						0, sizeof(struct lpfc_name));
1406 				}
1407 
1408 				rc = BG_ERR_INIT | BG_ERR_SWAP;
1409 				/* Signals the caller to swap CRC->CSUM */
1410 
1411 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1412 					"0818 BLKGRD: Injecting guard error: "
1413 					"read lba x%lx\n", (unsigned long)lba);
1414 			}
1415 		}
1416 	}
1417 
1418 	return rc;
1419 }
1420 #endif
1421 
1422 /**
1423  * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1424  * the specified SCSI command.
1425  * @phba: The Hba for which this call is being executed.
1426  * @sc: The SCSI command to examine
1427  * @txop: (out) BlockGuard operation for transmitted data
1428  * @rxop: (out) BlockGuard operation for received data
1429  *
1430  * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1431  *
1432  **/
1433 static int
1434 lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1435 		uint8_t *txop, uint8_t *rxop)
1436 {
1437 	uint8_t ret = 0;
1438 
1439 	if (sc->prot_flags & SCSI_PROT_IP_CHECKSUM) {
1440 		switch (scsi_get_prot_op(sc)) {
1441 		case SCSI_PROT_READ_INSERT:
1442 		case SCSI_PROT_WRITE_STRIP:
1443 			*rxop = BG_OP_IN_NODIF_OUT_CSUM;
1444 			*txop = BG_OP_IN_CSUM_OUT_NODIF;
1445 			break;
1446 
1447 		case SCSI_PROT_READ_STRIP:
1448 		case SCSI_PROT_WRITE_INSERT:
1449 			*rxop = BG_OP_IN_CRC_OUT_NODIF;
1450 			*txop = BG_OP_IN_NODIF_OUT_CRC;
1451 			break;
1452 
1453 		case SCSI_PROT_READ_PASS:
1454 		case SCSI_PROT_WRITE_PASS:
1455 			*rxop = BG_OP_IN_CRC_OUT_CSUM;
1456 			*txop = BG_OP_IN_CSUM_OUT_CRC;
1457 			break;
1458 
1459 		case SCSI_PROT_NORMAL:
1460 		default:
1461 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1462 				"9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1463 					scsi_get_prot_op(sc));
1464 			ret = 1;
1465 			break;
1466 
1467 		}
1468 	} else {
1469 		switch (scsi_get_prot_op(sc)) {
1470 		case SCSI_PROT_READ_STRIP:
1471 		case SCSI_PROT_WRITE_INSERT:
1472 			*rxop = BG_OP_IN_CRC_OUT_NODIF;
1473 			*txop = BG_OP_IN_NODIF_OUT_CRC;
1474 			break;
1475 
1476 		case SCSI_PROT_READ_PASS:
1477 		case SCSI_PROT_WRITE_PASS:
1478 			*rxop = BG_OP_IN_CRC_OUT_CRC;
1479 			*txop = BG_OP_IN_CRC_OUT_CRC;
1480 			break;
1481 
1482 		case SCSI_PROT_READ_INSERT:
1483 		case SCSI_PROT_WRITE_STRIP:
1484 			*rxop = BG_OP_IN_NODIF_OUT_CRC;
1485 			*txop = BG_OP_IN_CRC_OUT_NODIF;
1486 			break;
1487 
1488 		case SCSI_PROT_NORMAL:
1489 		default:
1490 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1491 				"9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1492 					scsi_get_prot_op(sc));
1493 			ret = 1;
1494 			break;
1495 		}
1496 	}
1497 
1498 	return ret;
1499 }
1500 
1501 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1502 /**
1503  * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1504  * the specified SCSI command in order to force a guard tag error.
1505  * @phba: The Hba for which this call is being executed.
1506  * @sc: The SCSI command to examine
1507  * @txop: (out) BlockGuard operation for transmitted data
1508  * @rxop: (out) BlockGuard operation for received data
1509  *
1510  * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1511  *
1512  **/
1513 static int
1514 lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1515 		uint8_t *txop, uint8_t *rxop)
1516 {
1517 	uint8_t ret = 0;
1518 
1519 	if (sc->prot_flags & SCSI_PROT_IP_CHECKSUM) {
1520 		switch (scsi_get_prot_op(sc)) {
1521 		case SCSI_PROT_READ_INSERT:
1522 		case SCSI_PROT_WRITE_STRIP:
1523 			*rxop = BG_OP_IN_NODIF_OUT_CRC;
1524 			*txop = BG_OP_IN_CRC_OUT_NODIF;
1525 			break;
1526 
1527 		case SCSI_PROT_READ_STRIP:
1528 		case SCSI_PROT_WRITE_INSERT:
1529 			*rxop = BG_OP_IN_CSUM_OUT_NODIF;
1530 			*txop = BG_OP_IN_NODIF_OUT_CSUM;
1531 			break;
1532 
1533 		case SCSI_PROT_READ_PASS:
1534 		case SCSI_PROT_WRITE_PASS:
1535 			*rxop = BG_OP_IN_CSUM_OUT_CRC;
1536 			*txop = BG_OP_IN_CRC_OUT_CSUM;
1537 			break;
1538 
1539 		case SCSI_PROT_NORMAL:
1540 		default:
1541 			break;
1542 
1543 		}
1544 	} else {
1545 		switch (scsi_get_prot_op(sc)) {
1546 		case SCSI_PROT_READ_STRIP:
1547 		case SCSI_PROT_WRITE_INSERT:
1548 			*rxop = BG_OP_IN_CSUM_OUT_NODIF;
1549 			*txop = BG_OP_IN_NODIF_OUT_CSUM;
1550 			break;
1551 
1552 		case SCSI_PROT_READ_PASS:
1553 		case SCSI_PROT_WRITE_PASS:
1554 			*rxop = BG_OP_IN_CSUM_OUT_CSUM;
1555 			*txop = BG_OP_IN_CSUM_OUT_CSUM;
1556 			break;
1557 
1558 		case SCSI_PROT_READ_INSERT:
1559 		case SCSI_PROT_WRITE_STRIP:
1560 			*rxop = BG_OP_IN_NODIF_OUT_CSUM;
1561 			*txop = BG_OP_IN_CSUM_OUT_NODIF;
1562 			break;
1563 
1564 		case SCSI_PROT_NORMAL:
1565 		default:
1566 			break;
1567 		}
1568 	}
1569 
1570 	return ret;
1571 }
1572 #endif
1573 
1574 /**
1575  * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1576  * @phba: The Hba for which this call is being executed.
1577  * @sc: pointer to scsi command we're working on
1578  * @bpl: pointer to buffer list for protection groups
1579  * @datasegcnt: number of segments of data that have been dma mapped
1580  *
1581  * This function sets up BPL buffer list for protection groups of
1582  * type LPFC_PG_TYPE_NO_DIF
1583  *
1584  * This is usually used when the HBA is instructed to generate
1585  * DIFs and insert them into data stream (or strip DIF from
1586  * incoming data stream)
1587  *
1588  * The buffer list consists of just one protection group described
1589  * below:
1590  *                                +-------------------------+
1591  *   start of prot group  -->     |          PDE_5          |
1592  *                                +-------------------------+
1593  *                                |          PDE_6          |
1594  *                                +-------------------------+
1595  *                                |         Data BDE        |
1596  *                                +-------------------------+
1597  *                                |more Data BDE's ... (opt)|
1598  *                                +-------------------------+
1599  *
1600  *
1601  * Note: Data s/g buffers have been dma mapped
1602  *
1603  * Returns the number of BDEs added to the BPL.
1604  **/
1605 static int
1606 lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1607 		struct ulp_bde64 *bpl, int datasegcnt)
1608 {
1609 	struct scatterlist *sgde = NULL; /* s/g data entry */
1610 	struct lpfc_pde5 *pde5 = NULL;
1611 	struct lpfc_pde6 *pde6 = NULL;
1612 	dma_addr_t physaddr;
1613 	int i = 0, num_bde = 0, status;
1614 	int datadir = sc->sc_data_direction;
1615 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1616 	uint32_t rc;
1617 #endif
1618 	uint32_t checking = 1;
1619 	uint32_t reftag;
1620 	uint8_t txop, rxop;
1621 
1622 	status  = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1623 	if (status)
1624 		goto out;
1625 
1626 	/* extract some info from the scsi command for pde*/
1627 	reftag = scsi_prot_ref_tag(sc);
1628 	if (reftag == LPFC_INVALID_REFTAG)
1629 		goto out;
1630 
1631 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1632 	rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
1633 	if (rc) {
1634 		if (rc & BG_ERR_SWAP)
1635 			lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
1636 		if (rc & BG_ERR_CHECK)
1637 			checking = 0;
1638 	}
1639 #endif
1640 
1641 	/* setup PDE5 with what we have */
1642 	pde5 = (struct lpfc_pde5 *) bpl;
1643 	memset(pde5, 0, sizeof(struct lpfc_pde5));
1644 	bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
1645 
1646 	/* Endianness conversion if necessary for PDE5 */
1647 	pde5->word0 = cpu_to_le32(pde5->word0);
1648 	pde5->reftag = cpu_to_le32(reftag);
1649 
1650 	/* advance bpl and increment bde count */
1651 	num_bde++;
1652 	bpl++;
1653 	pde6 = (struct lpfc_pde6 *) bpl;
1654 
1655 	/* setup PDE6 with the rest of the info */
1656 	memset(pde6, 0, sizeof(struct lpfc_pde6));
1657 	bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1658 	bf_set(pde6_optx, pde6, txop);
1659 	bf_set(pde6_oprx, pde6, rxop);
1660 
1661 	/*
1662 	 * We only need to check the data on READs, for WRITEs
1663 	 * protection data is automatically generated, not checked.
1664 	 */
1665 	if (datadir == DMA_FROM_DEVICE) {
1666 		if (sc->prot_flags & SCSI_PROT_GUARD_CHECK)
1667 			bf_set(pde6_ce, pde6, checking);
1668 		else
1669 			bf_set(pde6_ce, pde6, 0);
1670 
1671 		if (sc->prot_flags & SCSI_PROT_REF_CHECK)
1672 			bf_set(pde6_re, pde6, checking);
1673 		else
1674 			bf_set(pde6_re, pde6, 0);
1675 	}
1676 	bf_set(pde6_ai, pde6, 1);
1677 	bf_set(pde6_ae, pde6, 0);
1678 	bf_set(pde6_apptagval, pde6, 0);
1679 
1680 	/* Endianness conversion if necessary for PDE6 */
1681 	pde6->word0 = cpu_to_le32(pde6->word0);
1682 	pde6->word1 = cpu_to_le32(pde6->word1);
1683 	pde6->word2 = cpu_to_le32(pde6->word2);
1684 
1685 	/* advance bpl and increment bde count */
1686 	num_bde++;
1687 	bpl++;
1688 
1689 	/* assumption: caller has already run dma_map_sg on command data */
1690 	scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1691 		physaddr = sg_dma_address(sgde);
1692 		bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1693 		bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1694 		bpl->tus.f.bdeSize = sg_dma_len(sgde);
1695 		if (datadir == DMA_TO_DEVICE)
1696 			bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1697 		else
1698 			bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1699 		bpl->tus.w = le32_to_cpu(bpl->tus.w);
1700 		bpl++;
1701 		num_bde++;
1702 	}
1703 
1704 out:
1705 	return num_bde;
1706 }
1707 
1708 /**
1709  * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
1710  * @phba: The Hba for which this call is being executed.
1711  * @sc: pointer to scsi command we're working on
1712  * @bpl: pointer to buffer list for protection groups
1713  * @datacnt: number of segments of data that have been dma mapped
1714  * @protcnt: number of segment of protection data that have been dma mapped
1715  *
1716  * This function sets up BPL buffer list for protection groups of
1717  * type LPFC_PG_TYPE_DIF
1718  *
1719  * This is usually used when DIFs are in their own buffers,
1720  * separate from the data. The HBA can then by instructed
1721  * to place the DIFs in the outgoing stream.  For read operations,
1722  * The HBA could extract the DIFs and place it in DIF buffers.
1723  *
1724  * The buffer list for this type consists of one or more of the
1725  * protection groups described below:
1726  *                                    +-------------------------+
1727  *   start of first prot group  -->   |          PDE_5          |
1728  *                                    +-------------------------+
1729  *                                    |          PDE_6          |
1730  *                                    +-------------------------+
1731  *                                    |      PDE_7 (Prot BDE)   |
1732  *                                    +-------------------------+
1733  *                                    |        Data BDE         |
1734  *                                    +-------------------------+
1735  *                                    |more Data BDE's ... (opt)|
1736  *                                    +-------------------------+
1737  *   start of new  prot group  -->    |          PDE_5          |
1738  *                                    +-------------------------+
1739  *                                    |          ...            |
1740  *                                    +-------------------------+
1741  *
1742  * Note: It is assumed that both data and protection s/g buffers have been
1743  *       mapped for DMA
1744  *
1745  * Returns the number of BDEs added to the BPL.
1746  **/
1747 static int
1748 lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1749 		struct ulp_bde64 *bpl, int datacnt, int protcnt)
1750 {
1751 	struct scatterlist *sgde = NULL; /* s/g data entry */
1752 	struct scatterlist *sgpe = NULL; /* s/g prot entry */
1753 	struct lpfc_pde5 *pde5 = NULL;
1754 	struct lpfc_pde6 *pde6 = NULL;
1755 	struct lpfc_pde7 *pde7 = NULL;
1756 	dma_addr_t dataphysaddr, protphysaddr;
1757 	unsigned short curr_data = 0, curr_prot = 0;
1758 	unsigned int split_offset;
1759 	unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
1760 	unsigned int protgrp_blks, protgrp_bytes;
1761 	unsigned int remainder, subtotal;
1762 	int status;
1763 	int datadir = sc->sc_data_direction;
1764 	unsigned char pgdone = 0, alldone = 0;
1765 	unsigned blksize;
1766 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1767 	uint32_t rc;
1768 #endif
1769 	uint32_t checking = 1;
1770 	uint32_t reftag;
1771 	uint8_t txop, rxop;
1772 	int num_bde = 0;
1773 
1774 	sgpe = scsi_prot_sglist(sc);
1775 	sgde = scsi_sglist(sc);
1776 
1777 	if (!sgpe || !sgde) {
1778 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1779 				"9020 Invalid s/g entry: data=x%px prot=x%px\n",
1780 				sgpe, sgde);
1781 		return 0;
1782 	}
1783 
1784 	status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1785 	if (status)
1786 		goto out;
1787 
1788 	/* extract some info from the scsi command */
1789 	blksize = scsi_prot_interval(sc);
1790 	reftag = scsi_prot_ref_tag(sc);
1791 	if (reftag == LPFC_INVALID_REFTAG)
1792 		goto out;
1793 
1794 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1795 	rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
1796 	if (rc) {
1797 		if (rc & BG_ERR_SWAP)
1798 			lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
1799 		if (rc & BG_ERR_CHECK)
1800 			checking = 0;
1801 	}
1802 #endif
1803 
1804 	split_offset = 0;
1805 	do {
1806 		/* Check to see if we ran out of space */
1807 		if (num_bde >= (phba->cfg_total_seg_cnt - 2))
1808 			return num_bde + 3;
1809 
1810 		/* setup PDE5 with what we have */
1811 		pde5 = (struct lpfc_pde5 *) bpl;
1812 		memset(pde5, 0, sizeof(struct lpfc_pde5));
1813 		bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
1814 
1815 		/* Endianness conversion if necessary for PDE5 */
1816 		pde5->word0 = cpu_to_le32(pde5->word0);
1817 		pde5->reftag = cpu_to_le32(reftag);
1818 
1819 		/* advance bpl and increment bde count */
1820 		num_bde++;
1821 		bpl++;
1822 		pde6 = (struct lpfc_pde6 *) bpl;
1823 
1824 		/* setup PDE6 with the rest of the info */
1825 		memset(pde6, 0, sizeof(struct lpfc_pde6));
1826 		bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1827 		bf_set(pde6_optx, pde6, txop);
1828 		bf_set(pde6_oprx, pde6, rxop);
1829 
1830 		if (sc->prot_flags & SCSI_PROT_GUARD_CHECK)
1831 			bf_set(pde6_ce, pde6, checking);
1832 		else
1833 			bf_set(pde6_ce, pde6, 0);
1834 
1835 		if (sc->prot_flags & SCSI_PROT_REF_CHECK)
1836 			bf_set(pde6_re, pde6, checking);
1837 		else
1838 			bf_set(pde6_re, pde6, 0);
1839 
1840 		bf_set(pde6_ai, pde6, 1);
1841 		bf_set(pde6_ae, pde6, 0);
1842 		bf_set(pde6_apptagval, pde6, 0);
1843 
1844 		/* Endianness conversion if necessary for PDE6 */
1845 		pde6->word0 = cpu_to_le32(pde6->word0);
1846 		pde6->word1 = cpu_to_le32(pde6->word1);
1847 		pde6->word2 = cpu_to_le32(pde6->word2);
1848 
1849 		/* advance bpl and increment bde count */
1850 		num_bde++;
1851 		bpl++;
1852 
1853 		/* setup the first BDE that points to protection buffer */
1854 		protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
1855 		protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
1856 
1857 		/* must be integer multiple of the DIF block length */
1858 		BUG_ON(protgroup_len % 8);
1859 
1860 		pde7 = (struct lpfc_pde7 *) bpl;
1861 		memset(pde7, 0, sizeof(struct lpfc_pde7));
1862 		bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
1863 
1864 		pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1865 		pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
1866 
1867 		protgrp_blks = protgroup_len / 8;
1868 		protgrp_bytes = protgrp_blks * blksize;
1869 
1870 		/* check if this pde is crossing the 4K boundary; if so split */
1871 		if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
1872 			protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
1873 			protgroup_offset += protgroup_remainder;
1874 			protgrp_blks = protgroup_remainder / 8;
1875 			protgrp_bytes = protgrp_blks * blksize;
1876 		} else {
1877 			protgroup_offset = 0;
1878 			curr_prot++;
1879 		}
1880 
1881 		num_bde++;
1882 
1883 		/* setup BDE's for data blocks associated with DIF data */
1884 		pgdone = 0;
1885 		subtotal = 0; /* total bytes processed for current prot grp */
1886 		while (!pgdone) {
1887 			/* Check to see if we ran out of space */
1888 			if (num_bde >= phba->cfg_total_seg_cnt)
1889 				return num_bde + 1;
1890 
1891 			if (!sgde) {
1892 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1893 					"9065 BLKGRD:%s Invalid data segment\n",
1894 						__func__);
1895 				return 0;
1896 			}
1897 			bpl++;
1898 			dataphysaddr = sg_dma_address(sgde) + split_offset;
1899 			bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1900 			bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1901 
1902 			remainder = sg_dma_len(sgde) - split_offset;
1903 
1904 			if ((subtotal + remainder) <= protgrp_bytes) {
1905 				/* we can use this whole buffer */
1906 				bpl->tus.f.bdeSize = remainder;
1907 				split_offset = 0;
1908 
1909 				if ((subtotal + remainder) == protgrp_bytes)
1910 					pgdone = 1;
1911 			} else {
1912 				/* must split this buffer with next prot grp */
1913 				bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1914 				split_offset += bpl->tus.f.bdeSize;
1915 			}
1916 
1917 			subtotal += bpl->tus.f.bdeSize;
1918 
1919 			if (datadir == DMA_TO_DEVICE)
1920 				bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1921 			else
1922 				bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1923 			bpl->tus.w = le32_to_cpu(bpl->tus.w);
1924 
1925 			num_bde++;
1926 			curr_data++;
1927 
1928 			if (split_offset)
1929 				break;
1930 
1931 			/* Move to the next s/g segment if possible */
1932 			sgde = sg_next(sgde);
1933 
1934 		}
1935 
1936 		if (protgroup_offset) {
1937 			/* update the reference tag */
1938 			reftag += protgrp_blks;
1939 			bpl++;
1940 			continue;
1941 		}
1942 
1943 		/* are we done ? */
1944 		if (curr_prot == protcnt) {
1945 			alldone = 1;
1946 		} else if (curr_prot < protcnt) {
1947 			/* advance to next prot buffer */
1948 			sgpe = sg_next(sgpe);
1949 			bpl++;
1950 
1951 			/* update the reference tag */
1952 			reftag += protgrp_blks;
1953 		} else {
1954 			/* if we're here, we have a bug */
1955 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1956 					"9054 BLKGRD: bug in %s\n", __func__);
1957 		}
1958 
1959 	} while (!alldone);
1960 out:
1961 
1962 	return num_bde;
1963 }
1964 
1965 /**
1966  * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
1967  * @phba: The Hba for which this call is being executed.
1968  * @sc: pointer to scsi command we're working on
1969  * @sgl: pointer to buffer list for protection groups
1970  * @datasegcnt: number of segments of data that have been dma mapped
1971  * @lpfc_cmd: lpfc scsi command object pointer.
1972  *
1973  * This function sets up SGL buffer list for protection groups of
1974  * type LPFC_PG_TYPE_NO_DIF
1975  *
1976  * This is usually used when the HBA is instructed to generate
1977  * DIFs and insert them into data stream (or strip DIF from
1978  * incoming data stream)
1979  *
1980  * The buffer list consists of just one protection group described
1981  * below:
1982  *                                +-------------------------+
1983  *   start of prot group  -->     |         DI_SEED         |
1984  *                                +-------------------------+
1985  *                                |         Data SGE        |
1986  *                                +-------------------------+
1987  *                                |more Data SGE's ... (opt)|
1988  *                                +-------------------------+
1989  *
1990  *
1991  * Note: Data s/g buffers have been dma mapped
1992  *
1993  * Returns the number of SGEs added to the SGL.
1994  **/
1995 static int
1996 lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1997 		struct sli4_sge *sgl, int datasegcnt,
1998 		struct lpfc_io_buf *lpfc_cmd)
1999 {
2000 	struct scatterlist *sgde = NULL; /* s/g data entry */
2001 	struct sli4_sge_diseed *diseed = NULL;
2002 	dma_addr_t physaddr;
2003 	int i = 0, num_sge = 0, status;
2004 	uint32_t reftag;
2005 	uint8_t txop, rxop;
2006 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2007 	uint32_t rc;
2008 #endif
2009 	uint32_t checking = 1;
2010 	uint32_t dma_len;
2011 	uint32_t dma_offset = 0;
2012 	struct sli4_hybrid_sgl *sgl_xtra = NULL;
2013 	int j;
2014 	bool lsp_just_set = false;
2015 
2016 	status  = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2017 	if (status)
2018 		goto out;
2019 
2020 	/* extract some info from the scsi command for pde*/
2021 	reftag = scsi_prot_ref_tag(sc);
2022 	if (reftag == LPFC_INVALID_REFTAG)
2023 		goto out;
2024 
2025 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2026 	rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
2027 	if (rc) {
2028 		if (rc & BG_ERR_SWAP)
2029 			lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2030 		if (rc & BG_ERR_CHECK)
2031 			checking = 0;
2032 	}
2033 #endif
2034 
2035 	/* setup DISEED with what we have */
2036 	diseed = (struct sli4_sge_diseed *) sgl;
2037 	memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2038 	bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2039 
2040 	/* Endianness conversion if necessary */
2041 	diseed->ref_tag = cpu_to_le32(reftag);
2042 	diseed->ref_tag_tran = diseed->ref_tag;
2043 
2044 	/*
2045 	 * We only need to check the data on READs, for WRITEs
2046 	 * protection data is automatically generated, not checked.
2047 	 */
2048 	if (sc->sc_data_direction == DMA_FROM_DEVICE) {
2049 		if (sc->prot_flags & SCSI_PROT_GUARD_CHECK)
2050 			bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2051 		else
2052 			bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2053 
2054 		if (sc->prot_flags & SCSI_PROT_REF_CHECK)
2055 			bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2056 		else
2057 			bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2058 	}
2059 
2060 	/* setup DISEED with the rest of the info */
2061 	bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2062 	bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2063 
2064 	bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2065 	bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2066 
2067 	/* Endianness conversion if necessary for DISEED */
2068 	diseed->word2 = cpu_to_le32(diseed->word2);
2069 	diseed->word3 = cpu_to_le32(diseed->word3);
2070 
2071 	/* advance bpl and increment sge count */
2072 	num_sge++;
2073 	sgl++;
2074 
2075 	/* assumption: caller has already run dma_map_sg on command data */
2076 	sgde = scsi_sglist(sc);
2077 	j = 3;
2078 	for (i = 0; i < datasegcnt; i++) {
2079 		/* clear it */
2080 		sgl->word2 = 0;
2081 
2082 		/* do we need to expand the segment */
2083 		if (!lsp_just_set && !((j + 1) % phba->border_sge_num) &&
2084 		    ((datasegcnt - 1) != i)) {
2085 			/* set LSP type */
2086 			bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_LSP);
2087 
2088 			sgl_xtra = lpfc_get_sgl_per_hdwq(phba, lpfc_cmd);
2089 
2090 			if (unlikely(!sgl_xtra)) {
2091 				lpfc_cmd->seg_cnt = 0;
2092 				return 0;
2093 			}
2094 			sgl->addr_lo = cpu_to_le32(putPaddrLow(
2095 						sgl_xtra->dma_phys_sgl));
2096 			sgl->addr_hi = cpu_to_le32(putPaddrHigh(
2097 						sgl_xtra->dma_phys_sgl));
2098 
2099 		} else {
2100 			bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2101 		}
2102 
2103 		if (!(bf_get(lpfc_sli4_sge_type, sgl) & LPFC_SGE_TYPE_LSP)) {
2104 			if ((datasegcnt - 1) == i)
2105 				bf_set(lpfc_sli4_sge_last, sgl, 1);
2106 			physaddr = sg_dma_address(sgde);
2107 			dma_len = sg_dma_len(sgde);
2108 			sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2109 			sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2110 
2111 			bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2112 			sgl->word2 = cpu_to_le32(sgl->word2);
2113 			sgl->sge_len = cpu_to_le32(dma_len);
2114 
2115 			dma_offset += dma_len;
2116 			sgde = sg_next(sgde);
2117 
2118 			sgl++;
2119 			num_sge++;
2120 			lsp_just_set = false;
2121 
2122 		} else {
2123 			sgl->word2 = cpu_to_le32(sgl->word2);
2124 			sgl->sge_len = cpu_to_le32(phba->cfg_sg_dma_buf_size);
2125 
2126 			sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
2127 			i = i - 1;
2128 
2129 			lsp_just_set = true;
2130 		}
2131 
2132 		j++;
2133 
2134 	}
2135 
2136 out:
2137 	return num_sge;
2138 }
2139 
2140 /**
2141  * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2142  * @phba: The Hba for which this call is being executed.
2143  * @sc: pointer to scsi command we're working on
2144  * @sgl: pointer to buffer list for protection groups
2145  * @datacnt: number of segments of data that have been dma mapped
2146  * @protcnt: number of segment of protection data that have been dma mapped
2147  * @lpfc_cmd: lpfc scsi command object pointer.
2148  *
2149  * This function sets up SGL buffer list for protection groups of
2150  * type LPFC_PG_TYPE_DIF
2151  *
2152  * This is usually used when DIFs are in their own buffers,
2153  * separate from the data. The HBA can then by instructed
2154  * to place the DIFs in the outgoing stream.  For read operations,
2155  * The HBA could extract the DIFs and place it in DIF buffers.
2156  *
2157  * The buffer list for this type consists of one or more of the
2158  * protection groups described below:
2159  *                                    +-------------------------+
2160  *   start of first prot group  -->   |         DISEED          |
2161  *                                    +-------------------------+
2162  *                                    |      DIF (Prot SGE)     |
2163  *                                    +-------------------------+
2164  *                                    |        Data SGE         |
2165  *                                    +-------------------------+
2166  *                                    |more Data SGE's ... (opt)|
2167  *                                    +-------------------------+
2168  *   start of new  prot group  -->    |         DISEED          |
2169  *                                    +-------------------------+
2170  *                                    |          ...            |
2171  *                                    +-------------------------+
2172  *
2173  * Note: It is assumed that both data and protection s/g buffers have been
2174  *       mapped for DMA
2175  *
2176  * Returns the number of SGEs added to the SGL.
2177  **/
2178 static int
2179 lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2180 		struct sli4_sge *sgl, int datacnt, int protcnt,
2181 		struct lpfc_io_buf *lpfc_cmd)
2182 {
2183 	struct scatterlist *sgde = NULL; /* s/g data entry */
2184 	struct scatterlist *sgpe = NULL; /* s/g prot entry */
2185 	struct sli4_sge_diseed *diseed = NULL;
2186 	dma_addr_t dataphysaddr, protphysaddr;
2187 	unsigned short curr_data = 0, curr_prot = 0;
2188 	unsigned int split_offset;
2189 	unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2190 	unsigned int protgrp_blks, protgrp_bytes;
2191 	unsigned int remainder, subtotal;
2192 	int status;
2193 	unsigned char pgdone = 0, alldone = 0;
2194 	unsigned blksize;
2195 	uint32_t reftag;
2196 	uint8_t txop, rxop;
2197 	uint32_t dma_len;
2198 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2199 	uint32_t rc;
2200 #endif
2201 	uint32_t checking = 1;
2202 	uint32_t dma_offset = 0;
2203 	int num_sge = 0, j = 2;
2204 	struct sli4_hybrid_sgl *sgl_xtra = NULL;
2205 
2206 	sgpe = scsi_prot_sglist(sc);
2207 	sgde = scsi_sglist(sc);
2208 
2209 	if (!sgpe || !sgde) {
2210 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2211 				"9082 Invalid s/g entry: data=x%px prot=x%px\n",
2212 				sgpe, sgde);
2213 		return 0;
2214 	}
2215 
2216 	status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2217 	if (status)
2218 		goto out;
2219 
2220 	/* extract some info from the scsi command */
2221 	blksize = scsi_prot_interval(sc);
2222 	reftag = scsi_prot_ref_tag(sc);
2223 	if (reftag == LPFC_INVALID_REFTAG)
2224 		goto out;
2225 
2226 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2227 	rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
2228 	if (rc) {
2229 		if (rc & BG_ERR_SWAP)
2230 			lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2231 		if (rc & BG_ERR_CHECK)
2232 			checking = 0;
2233 	}
2234 #endif
2235 
2236 	split_offset = 0;
2237 	do {
2238 		/* Check to see if we ran out of space */
2239 		if ((num_sge >= (phba->cfg_total_seg_cnt - 2)) &&
2240 		    !(phba->cfg_xpsgl))
2241 			return num_sge + 3;
2242 
2243 		/* DISEED and DIF have to be together */
2244 		if (!((j + 1) % phba->border_sge_num) ||
2245 		    !((j + 2) % phba->border_sge_num) ||
2246 		    !((j + 3) % phba->border_sge_num)) {
2247 			sgl->word2 = 0;
2248 
2249 			/* set LSP type */
2250 			bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_LSP);
2251 
2252 			sgl_xtra = lpfc_get_sgl_per_hdwq(phba, lpfc_cmd);
2253 
2254 			if (unlikely(!sgl_xtra)) {
2255 				goto out;
2256 			} else {
2257 				sgl->addr_lo = cpu_to_le32(putPaddrLow(
2258 						sgl_xtra->dma_phys_sgl));
2259 				sgl->addr_hi = cpu_to_le32(putPaddrHigh(
2260 						       sgl_xtra->dma_phys_sgl));
2261 			}
2262 
2263 			sgl->word2 = cpu_to_le32(sgl->word2);
2264 			sgl->sge_len = cpu_to_le32(phba->cfg_sg_dma_buf_size);
2265 
2266 			sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
2267 			j = 0;
2268 		}
2269 
2270 		/* setup DISEED with what we have */
2271 		diseed = (struct sli4_sge_diseed *) sgl;
2272 		memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2273 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2274 
2275 		/* Endianness conversion if necessary */
2276 		diseed->ref_tag = cpu_to_le32(reftag);
2277 		diseed->ref_tag_tran = diseed->ref_tag;
2278 
2279 		if (sc->prot_flags & SCSI_PROT_GUARD_CHECK) {
2280 			bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2281 		} else {
2282 			bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2283 			/*
2284 			 * When in this mode, the hardware will replace
2285 			 * the guard tag from the host with a
2286 			 * newly generated good CRC for the wire.
2287 			 * Switch to raw mode here to avoid this
2288 			 * behavior. What the host sends gets put on the wire.
2289 			 */
2290 			if (txop == BG_OP_IN_CRC_OUT_CRC) {
2291 				txop = BG_OP_RAW_MODE;
2292 				rxop = BG_OP_RAW_MODE;
2293 			}
2294 		}
2295 
2296 
2297 		if (sc->prot_flags & SCSI_PROT_REF_CHECK)
2298 			bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2299 		else
2300 			bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2301 
2302 		/* setup DISEED with the rest of the info */
2303 		bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2304 		bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2305 
2306 		bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2307 		bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2308 
2309 		/* Endianness conversion if necessary for DISEED */
2310 		diseed->word2 = cpu_to_le32(diseed->word2);
2311 		diseed->word3 = cpu_to_le32(diseed->word3);
2312 
2313 		/* advance sgl and increment bde count */
2314 		num_sge++;
2315 
2316 		sgl++;
2317 		j++;
2318 
2319 		/* setup the first BDE that points to protection buffer */
2320 		protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2321 		protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2322 
2323 		/* must be integer multiple of the DIF block length */
2324 		BUG_ON(protgroup_len % 8);
2325 
2326 		/* Now setup DIF SGE */
2327 		sgl->word2 = 0;
2328 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2329 		sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2330 		sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2331 		sgl->word2 = cpu_to_le32(sgl->word2);
2332 		sgl->sge_len = 0;
2333 
2334 		protgrp_blks = protgroup_len / 8;
2335 		protgrp_bytes = protgrp_blks * blksize;
2336 
2337 		/* check if DIF SGE is crossing the 4K boundary; if so split */
2338 		if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2339 			protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2340 			protgroup_offset += protgroup_remainder;
2341 			protgrp_blks = protgroup_remainder / 8;
2342 			protgrp_bytes = protgrp_blks * blksize;
2343 		} else {
2344 			protgroup_offset = 0;
2345 			curr_prot++;
2346 		}
2347 
2348 		num_sge++;
2349 
2350 		/* setup SGE's for data blocks associated with DIF data */
2351 		pgdone = 0;
2352 		subtotal = 0; /* total bytes processed for current prot grp */
2353 
2354 		sgl++;
2355 		j++;
2356 
2357 		while (!pgdone) {
2358 			/* Check to see if we ran out of space */
2359 			if ((num_sge >= phba->cfg_total_seg_cnt) &&
2360 			    !phba->cfg_xpsgl)
2361 				return num_sge + 1;
2362 
2363 			if (!sgde) {
2364 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2365 					"9086 BLKGRD:%s Invalid data segment\n",
2366 						__func__);
2367 				return 0;
2368 			}
2369 
2370 			if (!((j + 1) % phba->border_sge_num)) {
2371 				sgl->word2 = 0;
2372 
2373 				/* set LSP type */
2374 				bf_set(lpfc_sli4_sge_type, sgl,
2375 				       LPFC_SGE_TYPE_LSP);
2376 
2377 				sgl_xtra = lpfc_get_sgl_per_hdwq(phba,
2378 								 lpfc_cmd);
2379 
2380 				if (unlikely(!sgl_xtra)) {
2381 					goto out;
2382 				} else {
2383 					sgl->addr_lo = cpu_to_le32(
2384 					  putPaddrLow(sgl_xtra->dma_phys_sgl));
2385 					sgl->addr_hi = cpu_to_le32(
2386 					  putPaddrHigh(sgl_xtra->dma_phys_sgl));
2387 				}
2388 
2389 				sgl->word2 = cpu_to_le32(sgl->word2);
2390 				sgl->sge_len = cpu_to_le32(
2391 						     phba->cfg_sg_dma_buf_size);
2392 
2393 				sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
2394 			} else {
2395 				dataphysaddr = sg_dma_address(sgde) +
2396 								   split_offset;
2397 
2398 				remainder = sg_dma_len(sgde) - split_offset;
2399 
2400 				if ((subtotal + remainder) <= protgrp_bytes) {
2401 					/* we can use this whole buffer */
2402 					dma_len = remainder;
2403 					split_offset = 0;
2404 
2405 					if ((subtotal + remainder) ==
2406 								  protgrp_bytes)
2407 						pgdone = 1;
2408 				} else {
2409 					/* must split this buffer with next
2410 					 * prot grp
2411 					 */
2412 					dma_len = protgrp_bytes - subtotal;
2413 					split_offset += dma_len;
2414 				}
2415 
2416 				subtotal += dma_len;
2417 
2418 				sgl->word2 = 0;
2419 				sgl->addr_lo = cpu_to_le32(putPaddrLow(
2420 								 dataphysaddr));
2421 				sgl->addr_hi = cpu_to_le32(putPaddrHigh(
2422 								 dataphysaddr));
2423 				bf_set(lpfc_sli4_sge_last, sgl, 0);
2424 				bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2425 				bf_set(lpfc_sli4_sge_type, sgl,
2426 				       LPFC_SGE_TYPE_DATA);
2427 
2428 				sgl->sge_len = cpu_to_le32(dma_len);
2429 				dma_offset += dma_len;
2430 
2431 				num_sge++;
2432 				curr_data++;
2433 
2434 				if (split_offset) {
2435 					sgl++;
2436 					j++;
2437 					break;
2438 				}
2439 
2440 				/* Move to the next s/g segment if possible */
2441 				sgde = sg_next(sgde);
2442 
2443 				sgl++;
2444 			}
2445 
2446 			j++;
2447 		}
2448 
2449 		if (protgroup_offset) {
2450 			/* update the reference tag */
2451 			reftag += protgrp_blks;
2452 			continue;
2453 		}
2454 
2455 		/* are we done ? */
2456 		if (curr_prot == protcnt) {
2457 			/* mark the last SGL */
2458 			sgl--;
2459 			bf_set(lpfc_sli4_sge_last, sgl, 1);
2460 			alldone = 1;
2461 		} else if (curr_prot < protcnt) {
2462 			/* advance to next prot buffer */
2463 			sgpe = sg_next(sgpe);
2464 
2465 			/* update the reference tag */
2466 			reftag += protgrp_blks;
2467 		} else {
2468 			/* if we're here, we have a bug */
2469 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2470 					"9085 BLKGRD: bug in %s\n", __func__);
2471 		}
2472 
2473 	} while (!alldone);
2474 
2475 out:
2476 
2477 	return num_sge;
2478 }
2479 
2480 /**
2481  * lpfc_prot_group_type - Get prtotection group type of SCSI command
2482  * @phba: The Hba for which this call is being executed.
2483  * @sc: pointer to scsi command we're working on
2484  *
2485  * Given a SCSI command that supports DIF, determine composition of protection
2486  * groups involved in setting up buffer lists
2487  *
2488  * Returns: Protection group type (with or without DIF)
2489  *
2490  **/
2491 static int
2492 lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2493 {
2494 	int ret = LPFC_PG_TYPE_INVALID;
2495 	unsigned char op = scsi_get_prot_op(sc);
2496 
2497 	switch (op) {
2498 	case SCSI_PROT_READ_STRIP:
2499 	case SCSI_PROT_WRITE_INSERT:
2500 		ret = LPFC_PG_TYPE_NO_DIF;
2501 		break;
2502 	case SCSI_PROT_READ_INSERT:
2503 	case SCSI_PROT_WRITE_STRIP:
2504 	case SCSI_PROT_READ_PASS:
2505 	case SCSI_PROT_WRITE_PASS:
2506 		ret = LPFC_PG_TYPE_DIF_BUF;
2507 		break;
2508 	default:
2509 		if (phba)
2510 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2511 					"9021 Unsupported protection op:%d\n",
2512 					op);
2513 		break;
2514 	}
2515 	return ret;
2516 }
2517 
2518 /**
2519  * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2520  * @phba: The Hba for which this call is being executed.
2521  * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2522  *
2523  * Adjust the data length to account for how much data
2524  * is actually on the wire.
2525  *
2526  * returns the adjusted data length
2527  **/
2528 static int
2529 lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
2530 		       struct lpfc_io_buf *lpfc_cmd)
2531 {
2532 	struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2533 	int fcpdl;
2534 
2535 	fcpdl = scsi_bufflen(sc);
2536 
2537 	/* Check if there is protection data on the wire */
2538 	if (sc->sc_data_direction == DMA_FROM_DEVICE) {
2539 		/* Read check for protection data */
2540 		if (scsi_get_prot_op(sc) ==  SCSI_PROT_READ_INSERT)
2541 			return fcpdl;
2542 
2543 	} else {
2544 		/* Write check for protection data */
2545 		if (scsi_get_prot_op(sc) ==  SCSI_PROT_WRITE_STRIP)
2546 			return fcpdl;
2547 	}
2548 
2549 	/*
2550 	 * If we are in DIF Type 1 mode every data block has a 8 byte
2551 	 * DIF (trailer) attached to it. Must ajust FCP data length
2552 	 * to account for the protection data.
2553 	 */
2554 	fcpdl += (fcpdl / scsi_prot_interval(sc)) * 8;
2555 
2556 	return fcpdl;
2557 }
2558 
2559 /**
2560  * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2561  * @phba: The Hba for which this call is being executed.
2562  * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2563  *
2564  * This is the protection/DIF aware version of
2565  * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2566  * two functions eventually, but for now, it's here.
2567  * RETURNS 0 - SUCCESS,
2568  *         1 - Failed DMA map, retry.
2569  *         2 - Invalid scsi cmd or prot-type. Do not rety.
2570  **/
2571 static int
2572 lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
2573 		struct lpfc_io_buf *lpfc_cmd)
2574 {
2575 	struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2576 	struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2577 	struct ulp_bde64 *bpl = (struct ulp_bde64 *)lpfc_cmd->dma_sgl;
2578 	IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2579 	uint32_t num_bde = 0;
2580 	int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2581 	int prot_group_type = 0;
2582 	int fcpdl;
2583 	int ret = 1;
2584 	struct lpfc_vport *vport = phba->pport;
2585 
2586 	/*
2587 	 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2588 	 *  fcp_rsp regions to the first data bde entry
2589 	 */
2590 	bpl += 2;
2591 	if (scsi_sg_count(scsi_cmnd)) {
2592 		/*
2593 		 * The driver stores the segment count returned from dma_map_sg
2594 		 * because this a count of dma-mappings used to map the use_sg
2595 		 * pages.  They are not guaranteed to be the same for those
2596 		 * architectures that implement an IOMMU.
2597 		 */
2598 		datasegcnt = dma_map_sg(&phba->pcidev->dev,
2599 					scsi_sglist(scsi_cmnd),
2600 					scsi_sg_count(scsi_cmnd), datadir);
2601 		if (unlikely(!datasegcnt))
2602 			return 1;
2603 
2604 		lpfc_cmd->seg_cnt = datasegcnt;
2605 
2606 		/* First check if data segment count from SCSI Layer is good */
2607 		if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
2608 			WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
2609 			ret = 2;
2610 			goto err;
2611 		}
2612 
2613 		prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2614 
2615 		switch (prot_group_type) {
2616 		case LPFC_PG_TYPE_NO_DIF:
2617 
2618 			/* Here we need to add a PDE5 and PDE6 to the count */
2619 			if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt) {
2620 				ret = 2;
2621 				goto err;
2622 			}
2623 
2624 			num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2625 					datasegcnt);
2626 			/* we should have 2 or more entries in buffer list */
2627 			if (num_bde < 2) {
2628 				ret = 2;
2629 				goto err;
2630 			}
2631 			break;
2632 
2633 		case LPFC_PG_TYPE_DIF_BUF:
2634 			/*
2635 			 * This type indicates that protection buffers are
2636 			 * passed to the driver, so that needs to be prepared
2637 			 * for DMA
2638 			 */
2639 			protsegcnt = dma_map_sg(&phba->pcidev->dev,
2640 					scsi_prot_sglist(scsi_cmnd),
2641 					scsi_prot_sg_count(scsi_cmnd), datadir);
2642 			if (unlikely(!protsegcnt)) {
2643 				scsi_dma_unmap(scsi_cmnd);
2644 				return 1;
2645 			}
2646 
2647 			lpfc_cmd->prot_seg_cnt = protsegcnt;
2648 
2649 			/*
2650 			 * There is a minimun of 4 BPLs used for every
2651 			 * protection data segment.
2652 			 */
2653 			if ((lpfc_cmd->prot_seg_cnt * 4) >
2654 			    (phba->cfg_total_seg_cnt - 2)) {
2655 				ret = 2;
2656 				goto err;
2657 			}
2658 
2659 			num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2660 					datasegcnt, protsegcnt);
2661 			/* we should have 3 or more entries in buffer list */
2662 			if ((num_bde < 3) ||
2663 			    (num_bde > phba->cfg_total_seg_cnt)) {
2664 				ret = 2;
2665 				goto err;
2666 			}
2667 			break;
2668 
2669 		case LPFC_PG_TYPE_INVALID:
2670 		default:
2671 			scsi_dma_unmap(scsi_cmnd);
2672 			lpfc_cmd->seg_cnt = 0;
2673 
2674 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2675 					"9022 Unexpected protection group %i\n",
2676 					prot_group_type);
2677 			return 2;
2678 		}
2679 	}
2680 
2681 	/*
2682 	 * Finish initializing those IOCB fields that are dependent on the
2683 	 * scsi_cmnd request_buffer.  Note that the bdeSize is explicitly
2684 	 * reinitialized since all iocb memory resources are used many times
2685 	 * for transmit, receive, and continuation bpl's.
2686 	 */
2687 	iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2688 	iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2689 	iocb_cmd->ulpBdeCount = 1;
2690 	iocb_cmd->ulpLe = 1;
2691 
2692 	fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
2693 	fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2694 
2695 	/*
2696 	 * Due to difference in data length between DIF/non-DIF paths,
2697 	 * we need to set word 4 of IOCB here
2698 	 */
2699 	iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2700 
2701 	/*
2702 	 * For First burst, we may need to adjust the initial transfer
2703 	 * length for DIF
2704 	 */
2705 	if (iocb_cmd->un.fcpi.fcpi_XRdy &&
2706 	    (fcpdl < vport->cfg_first_burst_size))
2707 		iocb_cmd->un.fcpi.fcpi_XRdy = fcpdl;
2708 
2709 	return 0;
2710 err:
2711 	if (lpfc_cmd->seg_cnt)
2712 		scsi_dma_unmap(scsi_cmnd);
2713 	if (lpfc_cmd->prot_seg_cnt)
2714 		dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2715 			     scsi_prot_sg_count(scsi_cmnd),
2716 			     scsi_cmnd->sc_data_direction);
2717 
2718 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2719 			"9023 Cannot setup S/G List for HBA"
2720 			"IO segs %d/%d BPL %d SCSI %d: %d %d\n",
2721 			lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
2722 			phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
2723 			prot_group_type, num_bde);
2724 
2725 	lpfc_cmd->seg_cnt = 0;
2726 	lpfc_cmd->prot_seg_cnt = 0;
2727 	return ret;
2728 }
2729 
2730 /*
2731  * This function calcuates the T10 DIF guard tag
2732  * on the specified data using a CRC algorithmn
2733  * using crc_t10dif.
2734  */
2735 static uint16_t
2736 lpfc_bg_crc(uint8_t *data, int count)
2737 {
2738 	uint16_t crc = 0;
2739 	uint16_t x;
2740 
2741 	crc = crc_t10dif(data, count);
2742 	x = cpu_to_be16(crc);
2743 	return x;
2744 }
2745 
2746 /*
2747  * This function calcuates the T10 DIF guard tag
2748  * on the specified data using a CSUM algorithmn
2749  * using ip_compute_csum.
2750  */
2751 static uint16_t
2752 lpfc_bg_csum(uint8_t *data, int count)
2753 {
2754 	uint16_t ret;
2755 
2756 	ret = ip_compute_csum(data, count);
2757 	return ret;
2758 }
2759 
2760 /*
2761  * This function examines the protection data to try to determine
2762  * what type of T10-DIF error occurred.
2763  */
2764 static void
2765 lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
2766 {
2767 	struct scatterlist *sgpe; /* s/g prot entry */
2768 	struct scatterlist *sgde; /* s/g data entry */
2769 	struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2770 	struct scsi_dif_tuple *src = NULL;
2771 	uint8_t *data_src = NULL;
2772 	uint16_t guard_tag;
2773 	uint16_t start_app_tag, app_tag;
2774 	uint32_t start_ref_tag, ref_tag;
2775 	int prot, protsegcnt;
2776 	int err_type, len, data_len;
2777 	int chk_ref, chk_app, chk_guard;
2778 	uint16_t sum;
2779 	unsigned blksize;
2780 
2781 	err_type = BGS_GUARD_ERR_MASK;
2782 	sum = 0;
2783 	guard_tag = 0;
2784 
2785 	/* First check to see if there is protection data to examine */
2786 	prot = scsi_get_prot_op(cmd);
2787 	if ((prot == SCSI_PROT_READ_STRIP) ||
2788 	    (prot == SCSI_PROT_WRITE_INSERT) ||
2789 	    (prot == SCSI_PROT_NORMAL))
2790 		goto out;
2791 
2792 	/* Currently the driver just supports ref_tag and guard_tag checking */
2793 	chk_ref = 1;
2794 	chk_app = 0;
2795 	chk_guard = 0;
2796 
2797 	/* Setup a ptr to the protection data provided by the SCSI host */
2798 	sgpe = scsi_prot_sglist(cmd);
2799 	protsegcnt = lpfc_cmd->prot_seg_cnt;
2800 
2801 	if (sgpe && protsegcnt) {
2802 
2803 		/*
2804 		 * We will only try to verify guard tag if the segment
2805 		 * data length is a multiple of the blksize.
2806 		 */
2807 		sgde = scsi_sglist(cmd);
2808 		blksize = scsi_prot_interval(cmd);
2809 		data_src = (uint8_t *)sg_virt(sgde);
2810 		data_len = sgde->length;
2811 		if ((data_len & (blksize - 1)) == 0)
2812 			chk_guard = 1;
2813 
2814 		src = (struct scsi_dif_tuple *)sg_virt(sgpe);
2815 		start_ref_tag = scsi_prot_ref_tag(cmd);
2816 		if (start_ref_tag == LPFC_INVALID_REFTAG)
2817 			goto out;
2818 		start_app_tag = src->app_tag;
2819 		len = sgpe->length;
2820 		while (src && protsegcnt) {
2821 			while (len) {
2822 
2823 				/*
2824 				 * First check to see if a protection data
2825 				 * check is valid
2826 				 */
2827 				if ((src->ref_tag == T10_PI_REF_ESCAPE) ||
2828 				    (src->app_tag == T10_PI_APP_ESCAPE)) {
2829 					start_ref_tag++;
2830 					goto skipit;
2831 				}
2832 
2833 				/* First Guard Tag checking */
2834 				if (chk_guard) {
2835 					guard_tag = src->guard_tag;
2836 					if (cmd->prot_flags
2837 					    & SCSI_PROT_IP_CHECKSUM)
2838 						sum = lpfc_bg_csum(data_src,
2839 								   blksize);
2840 					else
2841 						sum = lpfc_bg_crc(data_src,
2842 								  blksize);
2843 					if ((guard_tag != sum)) {
2844 						err_type = BGS_GUARD_ERR_MASK;
2845 						goto out;
2846 					}
2847 				}
2848 
2849 				/* Reference Tag checking */
2850 				ref_tag = be32_to_cpu(src->ref_tag);
2851 				if (chk_ref && (ref_tag != start_ref_tag)) {
2852 					err_type = BGS_REFTAG_ERR_MASK;
2853 					goto out;
2854 				}
2855 				start_ref_tag++;
2856 
2857 				/* App Tag checking */
2858 				app_tag = src->app_tag;
2859 				if (chk_app && (app_tag != start_app_tag)) {
2860 					err_type = BGS_APPTAG_ERR_MASK;
2861 					goto out;
2862 				}
2863 skipit:
2864 				len -= sizeof(struct scsi_dif_tuple);
2865 				if (len < 0)
2866 					len = 0;
2867 				src++;
2868 
2869 				data_src += blksize;
2870 				data_len -= blksize;
2871 
2872 				/*
2873 				 * Are we at the end of the Data segment?
2874 				 * The data segment is only used for Guard
2875 				 * tag checking.
2876 				 */
2877 				if (chk_guard && (data_len == 0)) {
2878 					chk_guard = 0;
2879 					sgde = sg_next(sgde);
2880 					if (!sgde)
2881 						goto out;
2882 
2883 					data_src = (uint8_t *)sg_virt(sgde);
2884 					data_len = sgde->length;
2885 					if ((data_len & (blksize - 1)) == 0)
2886 						chk_guard = 1;
2887 				}
2888 			}
2889 
2890 			/* Goto the next Protection data segment */
2891 			sgpe = sg_next(sgpe);
2892 			if (sgpe) {
2893 				src = (struct scsi_dif_tuple *)sg_virt(sgpe);
2894 				len = sgpe->length;
2895 			} else {
2896 				src = NULL;
2897 			}
2898 			protsegcnt--;
2899 		}
2900 	}
2901 out:
2902 	if (err_type == BGS_GUARD_ERR_MASK) {
2903 		scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x1);
2904 		set_host_byte(cmd, DID_ABORT);
2905 		phba->bg_guard_err_cnt++;
2906 		lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2907 				"9069 BLKGRD: reftag %x grd_tag err %x != %x\n",
2908 				scsi_prot_ref_tag(cmd),
2909 				sum, guard_tag);
2910 
2911 	} else if (err_type == BGS_REFTAG_ERR_MASK) {
2912 		scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x3);
2913 		set_host_byte(cmd, DID_ABORT);
2914 
2915 		phba->bg_reftag_err_cnt++;
2916 		lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2917 				"9066 BLKGRD: reftag %x ref_tag err %x != %x\n",
2918 				scsi_prot_ref_tag(cmd),
2919 				ref_tag, start_ref_tag);
2920 
2921 	} else if (err_type == BGS_APPTAG_ERR_MASK) {
2922 		scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x2);
2923 		set_host_byte(cmd, DID_ABORT);
2924 
2925 		phba->bg_apptag_err_cnt++;
2926 		lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2927 				"9041 BLKGRD: reftag %x app_tag err %x != %x\n",
2928 				scsi_prot_ref_tag(cmd),
2929 				app_tag, start_app_tag);
2930 	}
2931 }
2932 
2933 /*
2934  * This function checks for BlockGuard errors detected by
2935  * the HBA.  In case of errors, the ASC/ASCQ fields in the
2936  * sense buffer will be set accordingly, paired with
2937  * ILLEGAL_REQUEST to signal to the kernel that the HBA
2938  * detected corruption.
2939  *
2940  * Returns:
2941  *  0 - No error found
2942  *  1 - BlockGuard error found
2943  * -1 - Internal error (bad profile, ...etc)
2944  */
2945 static int
2946 lpfc_sli4_parse_bg_err(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd,
2947 		       struct lpfc_wcqe_complete *wcqe)
2948 {
2949 	struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2950 	int ret = 0;
2951 	u32 status = bf_get(lpfc_wcqe_c_status, wcqe);
2952 	u32 bghm = 0;
2953 	u32 bgstat = 0;
2954 	u64 failing_sector = 0;
2955 
2956 	if (status == CQE_STATUS_DI_ERROR) {
2957 		if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
2958 			bgstat |= BGS_GUARD_ERR_MASK;
2959 		if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* AppTag Check failed */
2960 			bgstat |= BGS_APPTAG_ERR_MASK;
2961 		if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* RefTag Check failed */
2962 			bgstat |= BGS_REFTAG_ERR_MASK;
2963 
2964 		/* Check to see if there was any good data before the error */
2965 		if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
2966 			bgstat |= BGS_HI_WATER_MARK_PRESENT_MASK;
2967 			bghm = wcqe->total_data_placed;
2968 		}
2969 
2970 		/*
2971 		 * Set ALL the error bits to indicate we don't know what
2972 		 * type of error it is.
2973 		 */
2974 		if (!bgstat)
2975 			bgstat |= (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
2976 				BGS_GUARD_ERR_MASK);
2977 	}
2978 
2979 	if (lpfc_bgs_get_guard_err(bgstat)) {
2980 		ret = 1;
2981 
2982 		scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x1);
2983 		set_host_byte(cmd, DID_ABORT);
2984 		phba->bg_guard_err_cnt++;
2985 		lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2986 				"9059 BLKGRD: Guard Tag error in cmd"
2987 				" 0x%x lba 0x%llx blk cnt 0x%x "
2988 				"bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2989 				(unsigned long long)scsi_get_lba(cmd),
2990 				scsi_logical_block_count(cmd), bgstat, bghm);
2991 	}
2992 
2993 	if (lpfc_bgs_get_reftag_err(bgstat)) {
2994 		ret = 1;
2995 
2996 		scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x3);
2997 		set_host_byte(cmd, DID_ABORT);
2998 
2999 		phba->bg_reftag_err_cnt++;
3000 		lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3001 				"9060 BLKGRD: Ref Tag error in cmd"
3002 				" 0x%x lba 0x%llx blk cnt 0x%x "
3003 				"bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3004 				(unsigned long long)scsi_get_lba(cmd),
3005 				scsi_logical_block_count(cmd), bgstat, bghm);
3006 	}
3007 
3008 	if (lpfc_bgs_get_apptag_err(bgstat)) {
3009 		ret = 1;
3010 
3011 		scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x2);
3012 		set_host_byte(cmd, DID_ABORT);
3013 
3014 		phba->bg_apptag_err_cnt++;
3015 		lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3016 				"9062 BLKGRD: App Tag error in cmd"
3017 				" 0x%x lba 0x%llx blk cnt 0x%x "
3018 				"bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3019 				(unsigned long long)scsi_get_lba(cmd),
3020 				scsi_logical_block_count(cmd), bgstat, bghm);
3021 	}
3022 
3023 	if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
3024 		/*
3025 		 * setup sense data descriptor 0 per SPC-4 as an information
3026 		 * field, and put the failing LBA in it.
3027 		 * This code assumes there was also a guard/app/ref tag error
3028 		 * indication.
3029 		 */
3030 		cmd->sense_buffer[7] = 0xc;   /* Additional sense length */
3031 		cmd->sense_buffer[8] = 0;     /* Information descriptor type */
3032 		cmd->sense_buffer[9] = 0xa;   /* Additional descriptor length */
3033 		cmd->sense_buffer[10] = 0x80; /* Validity bit */
3034 
3035 		/* bghm is a "on the wire" FC frame based count */
3036 		switch (scsi_get_prot_op(cmd)) {
3037 		case SCSI_PROT_READ_INSERT:
3038 		case SCSI_PROT_WRITE_STRIP:
3039 			bghm /= cmd->device->sector_size;
3040 			break;
3041 		case SCSI_PROT_READ_STRIP:
3042 		case SCSI_PROT_WRITE_INSERT:
3043 		case SCSI_PROT_READ_PASS:
3044 		case SCSI_PROT_WRITE_PASS:
3045 			bghm /= (cmd->device->sector_size +
3046 				sizeof(struct scsi_dif_tuple));
3047 			break;
3048 		}
3049 
3050 		failing_sector = scsi_get_lba(cmd);
3051 		failing_sector += bghm;
3052 
3053 		/* Descriptor Information */
3054 		put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
3055 	}
3056 
3057 	if (!ret) {
3058 		/* No error was reported - problem in FW? */
3059 		lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3060 				"9068 BLKGRD: Unknown error in cmd"
3061 				" 0x%x lba 0x%llx blk cnt 0x%x "
3062 				"bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3063 				(unsigned long long)scsi_get_lba(cmd),
3064 				scsi_logical_block_count(cmd), bgstat, bghm);
3065 
3066 		/* Calculate what type of error it was */
3067 		lpfc_calc_bg_err(phba, lpfc_cmd);
3068 	}
3069 	return ret;
3070 }
3071 
3072 /*
3073  * This function checks for BlockGuard errors detected by
3074  * the HBA.  In case of errors, the ASC/ASCQ fields in the
3075  * sense buffer will be set accordingly, paired with
3076  * ILLEGAL_REQUEST to signal to the kernel that the HBA
3077  * detected corruption.
3078  *
3079  * Returns:
3080  *  0 - No error found
3081  *  1 - BlockGuard error found
3082  * -1 - Internal error (bad profile, ...etc)
3083  */
3084 static int
3085 lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd,
3086 		  struct lpfc_iocbq *pIocbOut)
3087 {
3088 	struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3089 	struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
3090 	int ret = 0;
3091 	uint32_t bghm = bgf->bghm;
3092 	uint32_t bgstat = bgf->bgstat;
3093 	uint64_t failing_sector = 0;
3094 
3095 	if (lpfc_bgs_get_invalid_prof(bgstat)) {
3096 		cmd->result = DID_ERROR << 16;
3097 		lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3098 				"9072 BLKGRD: Invalid BG Profile in cmd "
3099 				"0x%x reftag 0x%x blk cnt 0x%x "
3100 				"bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3101 				scsi_prot_ref_tag(cmd),
3102 				scsi_logical_block_count(cmd), bgstat, bghm);
3103 		ret = (-1);
3104 		goto out;
3105 	}
3106 
3107 	if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
3108 		cmd->result = DID_ERROR << 16;
3109 		lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3110 				"9073 BLKGRD: Invalid BG PDIF Block in cmd "
3111 				"0x%x reftag 0x%x blk cnt 0x%x "
3112 				"bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3113 				scsi_prot_ref_tag(cmd),
3114 				scsi_logical_block_count(cmd), bgstat, bghm);
3115 		ret = (-1);
3116 		goto out;
3117 	}
3118 
3119 	if (lpfc_bgs_get_guard_err(bgstat)) {
3120 		ret = 1;
3121 
3122 		scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x1);
3123 		set_host_byte(cmd, DID_ABORT);
3124 		phba->bg_guard_err_cnt++;
3125 		lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3126 				"9055 BLKGRD: Guard Tag error in cmd "
3127 				"0x%x reftag 0x%x blk cnt 0x%x "
3128 				"bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3129 				scsi_prot_ref_tag(cmd),
3130 				scsi_logical_block_count(cmd), bgstat, bghm);
3131 	}
3132 
3133 	if (lpfc_bgs_get_reftag_err(bgstat)) {
3134 		ret = 1;
3135 
3136 		scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x3);
3137 		set_host_byte(cmd, DID_ABORT);
3138 
3139 		phba->bg_reftag_err_cnt++;
3140 		lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3141 				"9056 BLKGRD: Ref Tag error in cmd "
3142 				"0x%x reftag 0x%x blk cnt 0x%x "
3143 				"bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3144 				scsi_prot_ref_tag(cmd),
3145 				scsi_logical_block_count(cmd), bgstat, bghm);
3146 	}
3147 
3148 	if (lpfc_bgs_get_apptag_err(bgstat)) {
3149 		ret = 1;
3150 
3151 		scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x2);
3152 		set_host_byte(cmd, DID_ABORT);
3153 
3154 		phba->bg_apptag_err_cnt++;
3155 		lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3156 				"9061 BLKGRD: App Tag error in cmd "
3157 				"0x%x reftag 0x%x blk cnt 0x%x "
3158 				"bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3159 				scsi_prot_ref_tag(cmd),
3160 				scsi_logical_block_count(cmd), bgstat, bghm);
3161 	}
3162 
3163 	if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
3164 		/*
3165 		 * setup sense data descriptor 0 per SPC-4 as an information
3166 		 * field, and put the failing LBA in it.
3167 		 * This code assumes there was also a guard/app/ref tag error
3168 		 * indication.
3169 		 */
3170 		cmd->sense_buffer[7] = 0xc;   /* Additional sense length */
3171 		cmd->sense_buffer[8] = 0;     /* Information descriptor type */
3172 		cmd->sense_buffer[9] = 0xa;   /* Additional descriptor length */
3173 		cmd->sense_buffer[10] = 0x80; /* Validity bit */
3174 
3175 		/* bghm is a "on the wire" FC frame based count */
3176 		switch (scsi_get_prot_op(cmd)) {
3177 		case SCSI_PROT_READ_INSERT:
3178 		case SCSI_PROT_WRITE_STRIP:
3179 			bghm /= cmd->device->sector_size;
3180 			break;
3181 		case SCSI_PROT_READ_STRIP:
3182 		case SCSI_PROT_WRITE_INSERT:
3183 		case SCSI_PROT_READ_PASS:
3184 		case SCSI_PROT_WRITE_PASS:
3185 			bghm /= (cmd->device->sector_size +
3186 				sizeof(struct scsi_dif_tuple));
3187 			break;
3188 		}
3189 
3190 		failing_sector = scsi_get_lba(cmd);
3191 		failing_sector += bghm;
3192 
3193 		/* Descriptor Information */
3194 		put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
3195 	}
3196 
3197 	if (!ret) {
3198 		/* No error was reported - problem in FW? */
3199 		lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3200 				"9057 BLKGRD: Unknown error in cmd "
3201 				"0x%x reftag 0x%x blk cnt 0x%x "
3202 				"bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3203 				scsi_prot_ref_tag(cmd),
3204 				scsi_logical_block_count(cmd), bgstat, bghm);
3205 
3206 		/* Calculate what type of error it was */
3207 		lpfc_calc_bg_err(phba, lpfc_cmd);
3208 	}
3209 out:
3210 	return ret;
3211 }
3212 
3213 /**
3214  * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3215  * @phba: The Hba for which this call is being executed.
3216  * @lpfc_cmd: The scsi buffer which is going to be mapped.
3217  *
3218  * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3219  * field of @lpfc_cmd for device with SLI-4 interface spec.
3220  *
3221  * Return codes:
3222  *	2 - Error - Do not retry
3223  *	1 - Error - Retry
3224  *	0 - Success
3225  **/
3226 static int
3227 lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
3228 {
3229 	struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3230 	struct scatterlist *sgel = NULL;
3231 	struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3232 	struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
3233 	struct sli4_sge *first_data_sgl;
3234 	struct lpfc_iocbq *pwqeq = &lpfc_cmd->cur_iocbq;
3235 	struct lpfc_vport *vport = phba->pport;
3236 	union lpfc_wqe128 *wqe = &pwqeq->wqe;
3237 	dma_addr_t physaddr;
3238 	uint32_t dma_len;
3239 	uint32_t dma_offset = 0;
3240 	int nseg, i, j;
3241 	struct ulp_bde64 *bde;
3242 	bool lsp_just_set = false;
3243 	struct sli4_hybrid_sgl *sgl_xtra = NULL;
3244 
3245 	/*
3246 	 * There are three possibilities here - use scatter-gather segment, use
3247 	 * the single mapping, or neither.  Start the lpfc command prep by
3248 	 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3249 	 * data bde entry.
3250 	 */
3251 	if (scsi_sg_count(scsi_cmnd)) {
3252 		/*
3253 		 * The driver stores the segment count returned from dma_map_sg
3254 		 * because this a count of dma-mappings used to map the use_sg
3255 		 * pages.  They are not guaranteed to be the same for those
3256 		 * architectures that implement an IOMMU.
3257 		 */
3258 
3259 		nseg = scsi_dma_map(scsi_cmnd);
3260 		if (unlikely(nseg <= 0))
3261 			return 1;
3262 		sgl += 1;
3263 		/* clear the last flag in the fcp_rsp map entry */
3264 		sgl->word2 = le32_to_cpu(sgl->word2);
3265 		bf_set(lpfc_sli4_sge_last, sgl, 0);
3266 		sgl->word2 = cpu_to_le32(sgl->word2);
3267 		sgl += 1;
3268 		first_data_sgl = sgl;
3269 		lpfc_cmd->seg_cnt = nseg;
3270 		if (!phba->cfg_xpsgl &&
3271 		    lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
3272 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3273 					"9074 BLKGRD:"
3274 					" %s: Too many sg segments from "
3275 					"dma_map_sg.  Config %d, seg_cnt %d\n",
3276 					__func__, phba->cfg_sg_seg_cnt,
3277 					lpfc_cmd->seg_cnt);
3278 			WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
3279 			lpfc_cmd->seg_cnt = 0;
3280 			scsi_dma_unmap(scsi_cmnd);
3281 			return 2;
3282 		}
3283 
3284 		/*
3285 		 * The driver established a maximum scatter-gather segment count
3286 		 * during probe that limits the number of sg elements in any
3287 		 * single scsi command.  Just run through the seg_cnt and format
3288 		 * the sge's.
3289 		 * When using SLI-3 the driver will try to fit all the BDEs into
3290 		 * the IOCB. If it can't then the BDEs get added to a BPL as it
3291 		 * does for SLI-2 mode.
3292 		 */
3293 
3294 		/* for tracking segment boundaries */
3295 		sgel = scsi_sglist(scsi_cmnd);
3296 		j = 2;
3297 		for (i = 0; i < nseg; i++) {
3298 			sgl->word2 = 0;
3299 			if (nseg == 1) {
3300 				bf_set(lpfc_sli4_sge_last, sgl, 1);
3301 				bf_set(lpfc_sli4_sge_type, sgl,
3302 				       LPFC_SGE_TYPE_DATA);
3303 			} else {
3304 				bf_set(lpfc_sli4_sge_last, sgl, 0);
3305 
3306 				/* do we need to expand the segment */
3307 				if (!lsp_just_set &&
3308 				    !((j + 1) % phba->border_sge_num) &&
3309 				    ((nseg - 1) != i)) {
3310 					/* set LSP type */
3311 					bf_set(lpfc_sli4_sge_type, sgl,
3312 					       LPFC_SGE_TYPE_LSP);
3313 
3314 					sgl_xtra = lpfc_get_sgl_per_hdwq(
3315 							phba, lpfc_cmd);
3316 
3317 					if (unlikely(!sgl_xtra)) {
3318 						lpfc_cmd->seg_cnt = 0;
3319 						scsi_dma_unmap(scsi_cmnd);
3320 						return 1;
3321 					}
3322 					sgl->addr_lo = cpu_to_le32(putPaddrLow(
3323 						       sgl_xtra->dma_phys_sgl));
3324 					sgl->addr_hi = cpu_to_le32(putPaddrHigh(
3325 						       sgl_xtra->dma_phys_sgl));
3326 
3327 				} else {
3328 					bf_set(lpfc_sli4_sge_type, sgl,
3329 					       LPFC_SGE_TYPE_DATA);
3330 				}
3331 			}
3332 
3333 			if (!(bf_get(lpfc_sli4_sge_type, sgl) &
3334 				     LPFC_SGE_TYPE_LSP)) {
3335 				if ((nseg - 1) == i)
3336 					bf_set(lpfc_sli4_sge_last, sgl, 1);
3337 
3338 				physaddr = sg_dma_address(sgel);
3339 				dma_len = sg_dma_len(sgel);
3340 				sgl->addr_lo = cpu_to_le32(putPaddrLow(
3341 							   physaddr));
3342 				sgl->addr_hi = cpu_to_le32(putPaddrHigh(
3343 							   physaddr));
3344 
3345 				bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
3346 				sgl->word2 = cpu_to_le32(sgl->word2);
3347 				sgl->sge_len = cpu_to_le32(dma_len);
3348 
3349 				dma_offset += dma_len;
3350 				sgel = sg_next(sgel);
3351 
3352 				sgl++;
3353 				lsp_just_set = false;
3354 
3355 			} else {
3356 				sgl->word2 = cpu_to_le32(sgl->word2);
3357 				sgl->sge_len = cpu_to_le32(
3358 						     phba->cfg_sg_dma_buf_size);
3359 
3360 				sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
3361 				i = i - 1;
3362 
3363 				lsp_just_set = true;
3364 			}
3365 
3366 			j++;
3367 		}
3368 
3369 		/* PBDE support for first data SGE only.
3370 		 * For FCoE, we key off Performance Hints.
3371 		 * For FC, we key off lpfc_enable_pbde.
3372 		 */
3373 		if (nseg == 1 &&
3374 		    ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
3375 		     phba->cfg_enable_pbde)) {
3376 			/* Words 13-15 */
3377 			bde = (struct ulp_bde64 *)
3378 				&wqe->words[13];
3379 			bde->addrLow = first_data_sgl->addr_lo;
3380 			bde->addrHigh = first_data_sgl->addr_hi;
3381 			bde->tus.f.bdeSize =
3382 					le32_to_cpu(first_data_sgl->sge_len);
3383 			bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3384 			bde->tus.w = cpu_to_le32(bde->tus.w);
3385 
3386 			/* Word 11 - set PBDE bit */
3387 			bf_set(wqe_pbde, &wqe->generic.wqe_com, 1);
3388 		} else {
3389 			memset(&wqe->words[13], 0, (sizeof(uint32_t) * 3));
3390 			/* Word 11 - PBDE bit disabled by default template */
3391 		}
3392 	} else {
3393 		sgl += 1;
3394 		/* set the last flag in the fcp_rsp map entry */
3395 		sgl->word2 = le32_to_cpu(sgl->word2);
3396 		bf_set(lpfc_sli4_sge_last, sgl, 1);
3397 		sgl->word2 = cpu_to_le32(sgl->word2);
3398 
3399 		if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
3400 		    phba->cfg_enable_pbde) {
3401 			bde = (struct ulp_bde64 *)
3402 				&wqe->words[13];
3403 			memset(bde, 0, (sizeof(uint32_t) * 3));
3404 		}
3405 	}
3406 
3407 	/*
3408 	 * Finish initializing those IOCB fields that are dependent on the
3409 	 * scsi_cmnd request_buffer.  Note that for SLI-2 the bdeSize is
3410 	 * explicitly reinitialized.
3411 	 * all iocb memory resources are reused.
3412 	 */
3413 	fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3414 	/* Set first-burst provided it was successfully negotiated */
3415 	if (!(phba->hba_flag & HBA_FCOE_MODE) &&
3416 	    vport->cfg_first_burst_size &&
3417 	    scsi_cmnd->sc_data_direction == DMA_TO_DEVICE) {
3418 		u32 init_len, total_len;
3419 
3420 		total_len = be32_to_cpu(fcp_cmnd->fcpDl);
3421 		init_len = min(total_len, vport->cfg_first_burst_size);
3422 
3423 		/* Word 4 & 5 */
3424 		wqe->fcp_iwrite.initial_xfer_len = init_len;
3425 		wqe->fcp_iwrite.total_xfer_len = total_len;
3426 	} else {
3427 		/* Word 4 */
3428 		wqe->fcp_iwrite.total_xfer_len =
3429 			be32_to_cpu(fcp_cmnd->fcpDl);
3430 	}
3431 
3432 	/*
3433 	 * If the OAS driver feature is enabled and the lun is enabled for
3434 	 * OAS, set the oas iocb related flags.
3435 	 */
3436 	if ((phba->cfg_fof) && ((struct lpfc_device_data *)
3437 		scsi_cmnd->device->hostdata)->oas_enabled) {
3438 		lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
3439 		lpfc_cmd->cur_iocbq.priority = ((struct lpfc_device_data *)
3440 			scsi_cmnd->device->hostdata)->priority;
3441 
3442 		/* Word 10 */
3443 		bf_set(wqe_oas, &wqe->generic.wqe_com, 1);
3444 		bf_set(wqe_ccpe, &wqe->generic.wqe_com, 1);
3445 
3446 		if (lpfc_cmd->cur_iocbq.priority)
3447 			bf_set(wqe_ccp, &wqe->generic.wqe_com,
3448 			       (lpfc_cmd->cur_iocbq.priority << 1));
3449 		else
3450 			bf_set(wqe_ccp, &wqe->generic.wqe_com,
3451 			       (phba->cfg_XLanePriority << 1));
3452 	}
3453 
3454 	return 0;
3455 }
3456 
3457 /**
3458  * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3459  * @phba: The Hba for which this call is being executed.
3460  * @lpfc_cmd: The scsi buffer which is going to be mapped.
3461  *
3462  * This is the protection/DIF aware version of
3463  * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3464  * two functions eventually, but for now, it's here
3465  * Return codes:
3466  *	2 - Error - Do not retry
3467  *	1 - Error - Retry
3468  *	0 - Success
3469  **/
3470 static int
3471 lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3472 		struct lpfc_io_buf *lpfc_cmd)
3473 {
3474 	struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3475 	struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3476 	struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->dma_sgl);
3477 	struct lpfc_iocbq *pwqeq = &lpfc_cmd->cur_iocbq;
3478 	union lpfc_wqe128 *wqe = &pwqeq->wqe;
3479 	uint32_t num_sge = 0;
3480 	int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3481 	int prot_group_type = 0;
3482 	int fcpdl;
3483 	int ret = 1;
3484 	struct lpfc_vport *vport = phba->pport;
3485 
3486 	/*
3487 	 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
3488 	 *  fcp_rsp regions to the first data sge entry
3489 	 */
3490 	if (scsi_sg_count(scsi_cmnd)) {
3491 		/*
3492 		 * The driver stores the segment count returned from dma_map_sg
3493 		 * because this a count of dma-mappings used to map the use_sg
3494 		 * pages.  They are not guaranteed to be the same for those
3495 		 * architectures that implement an IOMMU.
3496 		 */
3497 		datasegcnt = dma_map_sg(&phba->pcidev->dev,
3498 					scsi_sglist(scsi_cmnd),
3499 					scsi_sg_count(scsi_cmnd), datadir);
3500 		if (unlikely(!datasegcnt))
3501 			return 1;
3502 
3503 		sgl += 1;
3504 		/* clear the last flag in the fcp_rsp map entry */
3505 		sgl->word2 = le32_to_cpu(sgl->word2);
3506 		bf_set(lpfc_sli4_sge_last, sgl, 0);
3507 		sgl->word2 = cpu_to_le32(sgl->word2);
3508 
3509 		sgl += 1;
3510 		lpfc_cmd->seg_cnt = datasegcnt;
3511 
3512 		/* First check if data segment count from SCSI Layer is good */
3513 		if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt &&
3514 		    !phba->cfg_xpsgl) {
3515 			WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
3516 			ret = 2;
3517 			goto err;
3518 		}
3519 
3520 		prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3521 
3522 		switch (prot_group_type) {
3523 		case LPFC_PG_TYPE_NO_DIF:
3524 			/* Here we need to add a DISEED to the count */
3525 			if (((lpfc_cmd->seg_cnt + 1) >
3526 					phba->cfg_total_seg_cnt) &&
3527 			    !phba->cfg_xpsgl) {
3528 				ret = 2;
3529 				goto err;
3530 			}
3531 
3532 			num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
3533 					datasegcnt, lpfc_cmd);
3534 
3535 			/* we should have 2 or more entries in buffer list */
3536 			if (num_sge < 2) {
3537 				ret = 2;
3538 				goto err;
3539 			}
3540 			break;
3541 
3542 		case LPFC_PG_TYPE_DIF_BUF:
3543 			/*
3544 			 * This type indicates that protection buffers are
3545 			 * passed to the driver, so that needs to be prepared
3546 			 * for DMA
3547 			 */
3548 			protsegcnt = dma_map_sg(&phba->pcidev->dev,
3549 					scsi_prot_sglist(scsi_cmnd),
3550 					scsi_prot_sg_count(scsi_cmnd), datadir);
3551 			if (unlikely(!protsegcnt)) {
3552 				scsi_dma_unmap(scsi_cmnd);
3553 				return 1;
3554 			}
3555 
3556 			lpfc_cmd->prot_seg_cnt = protsegcnt;
3557 			/*
3558 			 * There is a minimun of 3 SGEs used for every
3559 			 * protection data segment.
3560 			 */
3561 			if (((lpfc_cmd->prot_seg_cnt * 3) >
3562 					(phba->cfg_total_seg_cnt - 2)) &&
3563 			    !phba->cfg_xpsgl) {
3564 				ret = 2;
3565 				goto err;
3566 			}
3567 
3568 			num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
3569 					datasegcnt, protsegcnt, lpfc_cmd);
3570 
3571 			/* we should have 3 or more entries in buffer list */
3572 			if (num_sge < 3 ||
3573 			    (num_sge > phba->cfg_total_seg_cnt &&
3574 			     !phba->cfg_xpsgl)) {
3575 				ret = 2;
3576 				goto err;
3577 			}
3578 			break;
3579 
3580 		case LPFC_PG_TYPE_INVALID:
3581 		default:
3582 			scsi_dma_unmap(scsi_cmnd);
3583 			lpfc_cmd->seg_cnt = 0;
3584 
3585 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3586 					"9083 Unexpected protection group %i\n",
3587 					prot_group_type);
3588 			return 2;
3589 		}
3590 	}
3591 
3592 	switch (scsi_get_prot_op(scsi_cmnd)) {
3593 	case SCSI_PROT_WRITE_STRIP:
3594 	case SCSI_PROT_READ_STRIP:
3595 		lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3596 		break;
3597 	case SCSI_PROT_WRITE_INSERT:
3598 	case SCSI_PROT_READ_INSERT:
3599 		lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3600 		break;
3601 	case SCSI_PROT_WRITE_PASS:
3602 	case SCSI_PROT_READ_PASS:
3603 		lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3604 		break;
3605 	}
3606 
3607 	fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
3608 	fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3609 
3610 	/* Set first-burst provided it was successfully negotiated */
3611 	if (!(phba->hba_flag & HBA_FCOE_MODE) &&
3612 	    vport->cfg_first_burst_size &&
3613 	    scsi_cmnd->sc_data_direction == DMA_TO_DEVICE) {
3614 		u32 init_len, total_len;
3615 
3616 		total_len = be32_to_cpu(fcp_cmnd->fcpDl);
3617 		init_len = min(total_len, vport->cfg_first_burst_size);
3618 
3619 		/* Word 4 & 5 */
3620 		wqe->fcp_iwrite.initial_xfer_len = init_len;
3621 		wqe->fcp_iwrite.total_xfer_len = total_len;
3622 	} else {
3623 		/* Word 4 */
3624 		wqe->fcp_iwrite.total_xfer_len =
3625 			be32_to_cpu(fcp_cmnd->fcpDl);
3626 	}
3627 
3628 	/*
3629 	 * If the OAS driver feature is enabled and the lun is enabled for
3630 	 * OAS, set the oas iocb related flags.
3631 	 */
3632 	if ((phba->cfg_fof) && ((struct lpfc_device_data *)
3633 		scsi_cmnd->device->hostdata)->oas_enabled) {
3634 		lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
3635 
3636 		/* Word 10 */
3637 		bf_set(wqe_oas, &wqe->generic.wqe_com, 1);
3638 		bf_set(wqe_ccpe, &wqe->generic.wqe_com, 1);
3639 		bf_set(wqe_ccp, &wqe->generic.wqe_com,
3640 		       (phba->cfg_XLanePriority << 1));
3641 	}
3642 
3643 	/* Word 7. DIF Flags */
3644 	if (lpfc_cmd->cur_iocbq.iocb_flag & LPFC_IO_DIF_PASS)
3645 		bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
3646 	else if (lpfc_cmd->cur_iocbq.iocb_flag & LPFC_IO_DIF_STRIP)
3647 		bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
3648 	else if (lpfc_cmd->cur_iocbq.iocb_flag & LPFC_IO_DIF_INSERT)
3649 		bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
3650 
3651 	lpfc_cmd->cur_iocbq.iocb_flag &= ~(LPFC_IO_DIF_PASS |
3652 				 LPFC_IO_DIF_STRIP | LPFC_IO_DIF_INSERT);
3653 
3654 	return 0;
3655 err:
3656 	if (lpfc_cmd->seg_cnt)
3657 		scsi_dma_unmap(scsi_cmnd);
3658 	if (lpfc_cmd->prot_seg_cnt)
3659 		dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3660 			     scsi_prot_sg_count(scsi_cmnd),
3661 			     scsi_cmnd->sc_data_direction);
3662 
3663 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3664 			"9084 Cannot setup S/G List for HBA"
3665 			"IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3666 			lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3667 			phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3668 			prot_group_type, num_sge);
3669 
3670 	lpfc_cmd->seg_cnt = 0;
3671 	lpfc_cmd->prot_seg_cnt = 0;
3672 	return ret;
3673 }
3674 
3675 /**
3676  * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3677  * @phba: The Hba for which this call is being executed.
3678  * @lpfc_cmd: The scsi buffer which is going to be mapped.
3679  *
3680  * This routine wraps the actual DMA mapping function pointer from the
3681  * lpfc_hba struct.
3682  *
3683  * Return codes:
3684  *	1 - Error
3685  *	0 - Success
3686  **/
3687 static inline int
3688 lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
3689 {
3690 	return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3691 }
3692 
3693 /**
3694  * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3695  * using BlockGuard.
3696  * @phba: The Hba for which this call is being executed.
3697  * @lpfc_cmd: The scsi buffer which is going to be mapped.
3698  *
3699  * This routine wraps the actual DMA mapping function pointer from the
3700  * lpfc_hba struct.
3701  *
3702  * Return codes:
3703  *	1 - Error
3704  *	0 - Success
3705  **/
3706 static inline int
3707 lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
3708 {
3709 	return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3710 }
3711 
3712 /**
3713  * lpfc_scsi_prep_cmnd_buf - Wrapper function for IOCB/WQE mapping of scsi
3714  * buffer
3715  * @vport: Pointer to vport object.
3716  * @lpfc_cmd: The scsi buffer which is going to be mapped.
3717  * @tmo: Timeout value for IO
3718  *
3719  * This routine initializes IOCB/WQE data structure from scsi command
3720  *
3721  * Return codes:
3722  *	1 - Error
3723  *	0 - Success
3724  **/
3725 static inline int
3726 lpfc_scsi_prep_cmnd_buf(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd,
3727 			uint8_t tmo)
3728 {
3729 	return vport->phba->lpfc_scsi_prep_cmnd_buf(vport, lpfc_cmd, tmo);
3730 }
3731 
3732 /**
3733  * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
3734  * @phba: Pointer to hba context object.
3735  * @vport: Pointer to vport object.
3736  * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3737  * @fcpi_parm: FCP Initiator parameter.
3738  *
3739  * This function posts an event when there is a SCSI command reporting
3740  * error from the scsi device.
3741  **/
3742 static void
3743 lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3744 		struct lpfc_io_buf *lpfc_cmd, uint32_t fcpi_parm) {
3745 	struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3746 	struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3747 	uint32_t resp_info = fcprsp->rspStatus2;
3748 	uint32_t scsi_status = fcprsp->rspStatus3;
3749 	struct lpfc_fast_path_event *fast_path_evt = NULL;
3750 	struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3751 	unsigned long flags;
3752 
3753 	if (!pnode)
3754 		return;
3755 
3756 	/* If there is queuefull or busy condition send a scsi event */
3757 	if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3758 		(cmnd->result == SAM_STAT_BUSY)) {
3759 		fast_path_evt = lpfc_alloc_fast_evt(phba);
3760 		if (!fast_path_evt)
3761 			return;
3762 		fast_path_evt->un.scsi_evt.event_type =
3763 			FC_REG_SCSI_EVENT;
3764 		fast_path_evt->un.scsi_evt.subcategory =
3765 		(cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3766 		LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3767 		fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3768 		memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3769 			&pnode->nlp_portname, sizeof(struct lpfc_name));
3770 		memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3771 			&pnode->nlp_nodename, sizeof(struct lpfc_name));
3772 	} else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3773 		((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3774 		fast_path_evt = lpfc_alloc_fast_evt(phba);
3775 		if (!fast_path_evt)
3776 			return;
3777 		fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3778 			FC_REG_SCSI_EVENT;
3779 		fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3780 			LPFC_EVENT_CHECK_COND;
3781 		fast_path_evt->un.check_cond_evt.scsi_event.lun =
3782 			cmnd->device->lun;
3783 		memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3784 			&pnode->nlp_portname, sizeof(struct lpfc_name));
3785 		memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3786 			&pnode->nlp_nodename, sizeof(struct lpfc_name));
3787 		fast_path_evt->un.check_cond_evt.sense_key =
3788 			cmnd->sense_buffer[2] & 0xf;
3789 		fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3790 		fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3791 	} else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3792 		     fcpi_parm &&
3793 		     ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3794 			((scsi_status == SAM_STAT_GOOD) &&
3795 			!(resp_info & (RESID_UNDER | RESID_OVER))))) {
3796 		/*
3797 		 * If status is good or resid does not match with fcp_param and
3798 		 * there is valid fcpi_parm, then there is a read_check error
3799 		 */
3800 		fast_path_evt = lpfc_alloc_fast_evt(phba);
3801 		if (!fast_path_evt)
3802 			return;
3803 		fast_path_evt->un.read_check_error.header.event_type =
3804 			FC_REG_FABRIC_EVENT;
3805 		fast_path_evt->un.read_check_error.header.subcategory =
3806 			LPFC_EVENT_FCPRDCHKERR;
3807 		memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3808 			&pnode->nlp_portname, sizeof(struct lpfc_name));
3809 		memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3810 			&pnode->nlp_nodename, sizeof(struct lpfc_name));
3811 		fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3812 		fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3813 		fast_path_evt->un.read_check_error.fcpiparam =
3814 			fcpi_parm;
3815 	} else
3816 		return;
3817 
3818 	fast_path_evt->vport = vport;
3819 	spin_lock_irqsave(&phba->hbalock, flags);
3820 	list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3821 	spin_unlock_irqrestore(&phba->hbalock, flags);
3822 	lpfc_worker_wake_up(phba);
3823 	return;
3824 }
3825 
3826 /**
3827  * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
3828  * @phba: The HBA for which this call is being executed.
3829  * @psb: The scsi buffer which is going to be un-mapped.
3830  *
3831  * This routine does DMA un-mapping of scatter gather list of scsi command
3832  * field of @lpfc_cmd for device with SLI-3 interface spec.
3833  **/
3834 static void
3835 lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
3836 {
3837 	/*
3838 	 * There are only two special cases to consider.  (1) the scsi command
3839 	 * requested scatter-gather usage or (2) the scsi command allocated
3840 	 * a request buffer, but did not request use_sg.  There is a third
3841 	 * case, but it does not require resource deallocation.
3842 	 */
3843 	if (psb->seg_cnt > 0)
3844 		scsi_dma_unmap(psb->pCmd);
3845 	if (psb->prot_seg_cnt > 0)
3846 		dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3847 				scsi_prot_sg_count(psb->pCmd),
3848 				psb->pCmd->sc_data_direction);
3849 }
3850 
3851 /**
3852  * lpfc_unblock_requests - allow further commands to be queued.
3853  * @phba: pointer to phba object
3854  *
3855  * For single vport, just call scsi_unblock_requests on physical port.
3856  * For multiple vports, send scsi_unblock_requests for all the vports.
3857  */
3858 void
3859 lpfc_unblock_requests(struct lpfc_hba *phba)
3860 {
3861 	struct lpfc_vport **vports;
3862 	struct Scsi_Host  *shost;
3863 	int i;
3864 
3865 	if (phba->sli_rev == LPFC_SLI_REV4 &&
3866 	    !phba->sli4_hba.max_cfg_param.vpi_used) {
3867 		shost = lpfc_shost_from_vport(phba->pport);
3868 		scsi_unblock_requests(shost);
3869 		return;
3870 	}
3871 
3872 	vports = lpfc_create_vport_work_array(phba);
3873 	if (vports != NULL)
3874 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3875 			shost = lpfc_shost_from_vport(vports[i]);
3876 			scsi_unblock_requests(shost);
3877 		}
3878 	lpfc_destroy_vport_work_array(phba, vports);
3879 }
3880 
3881 /**
3882  * lpfc_block_requests - prevent further commands from being queued.
3883  * @phba: pointer to phba object
3884  *
3885  * For single vport, just call scsi_block_requests on physical port.
3886  * For multiple vports, send scsi_block_requests for all the vports.
3887  */
3888 void
3889 lpfc_block_requests(struct lpfc_hba *phba)
3890 {
3891 	struct lpfc_vport **vports;
3892 	struct Scsi_Host  *shost;
3893 	int i;
3894 
3895 	if (atomic_read(&phba->cmf_stop_io))
3896 		return;
3897 
3898 	if (phba->sli_rev == LPFC_SLI_REV4 &&
3899 	    !phba->sli4_hba.max_cfg_param.vpi_used) {
3900 		shost = lpfc_shost_from_vport(phba->pport);
3901 		scsi_block_requests(shost);
3902 		return;
3903 	}
3904 
3905 	vports = lpfc_create_vport_work_array(phba);
3906 	if (vports != NULL)
3907 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3908 			shost = lpfc_shost_from_vport(vports[i]);
3909 			scsi_block_requests(shost);
3910 		}
3911 	lpfc_destroy_vport_work_array(phba, vports);
3912 }
3913 
3914 /**
3915  * lpfc_update_cmf_cmpl - Adjust CMF counters for IO completion
3916  * @phba: The HBA for which this call is being executed.
3917  * @time: The latency of the IO that completed (in ns)
3918  * @size: The size of the IO that completed
3919  * @shost: SCSI host the IO completed on (NULL for a NVME IO)
3920  *
3921  * The routine adjusts the various Burst and Bandwidth counters used in
3922  * Congestion management and E2E. If time is set to LPFC_CGN_NOT_SENT,
3923  * that means the IO was never issued to the HBA, so this routine is
3924  * just being called to cleanup the counter from a previous
3925  * lpfc_update_cmf_cmd call.
3926  */
3927 int
3928 lpfc_update_cmf_cmpl(struct lpfc_hba *phba,
3929 		     uint64_t time, uint32_t size, struct Scsi_Host *shost)
3930 {
3931 	struct lpfc_cgn_stat *cgs;
3932 
3933 	if (time != LPFC_CGN_NOT_SENT) {
3934 		/* lat is ns coming in, save latency in us */
3935 		if (time < 1000)
3936 			time = 1;
3937 		else
3938 			time = div_u64(time + 500, 1000); /* round it */
3939 
3940 		cgs = this_cpu_ptr(phba->cmf_stat);
3941 		atomic64_add(size, &cgs->rcv_bytes);
3942 		atomic64_add(time, &cgs->rx_latency);
3943 		atomic_inc(&cgs->rx_io_cnt);
3944 	}
3945 	return 0;
3946 }
3947 
3948 /**
3949  * lpfc_update_cmf_cmd - Adjust CMF counters for IO submission
3950  * @phba: The HBA for which this call is being executed.
3951  * @size: The size of the IO that will be issued
3952  *
3953  * The routine adjusts the various Burst and Bandwidth counters used in
3954  * Congestion management and E2E.
3955  */
3956 int
3957 lpfc_update_cmf_cmd(struct lpfc_hba *phba, uint32_t size)
3958 {
3959 	uint64_t total;
3960 	struct lpfc_cgn_stat *cgs;
3961 	int cpu;
3962 
3963 	/* At this point we are either LPFC_CFG_MANAGED or LPFC_CFG_MONITOR */
3964 	if (phba->cmf_active_mode == LPFC_CFG_MANAGED &&
3965 	    phba->cmf_max_bytes_per_interval) {
3966 		total = 0;
3967 		for_each_present_cpu(cpu) {
3968 			cgs = per_cpu_ptr(phba->cmf_stat, cpu);
3969 			total += atomic64_read(&cgs->total_bytes);
3970 		}
3971 		if (total >= phba->cmf_max_bytes_per_interval) {
3972 			if (!atomic_xchg(&phba->cmf_bw_wait, 1)) {
3973 				lpfc_block_requests(phba);
3974 				phba->cmf_last_ts =
3975 					lpfc_calc_cmf_latency(phba);
3976 			}
3977 			atomic_inc(&phba->cmf_busy);
3978 			return -EBUSY;
3979 		}
3980 		if (size > atomic_read(&phba->rx_max_read_cnt))
3981 			atomic_set(&phba->rx_max_read_cnt, size);
3982 	}
3983 
3984 	cgs = this_cpu_ptr(phba->cmf_stat);
3985 	atomic64_add(size, &cgs->total_bytes);
3986 	return 0;
3987 }
3988 
3989 /**
3990  * lpfc_handle_fcp_err - FCP response handler
3991  * @vport: The virtual port for which this call is being executed.
3992  * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
3993  * @fcpi_parm: FCP Initiator parameter.
3994  *
3995  * This routine is called to process response IOCB with status field
3996  * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3997  * based upon SCSI and FCP error.
3998  **/
3999 static void
4000 lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd,
4001 		    uint32_t fcpi_parm)
4002 {
4003 	struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
4004 	struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
4005 	struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
4006 	uint32_t resp_info = fcprsp->rspStatus2;
4007 	uint32_t scsi_status = fcprsp->rspStatus3;
4008 	uint32_t *lp;
4009 	uint32_t host_status = DID_OK;
4010 	uint32_t rsplen = 0;
4011 	uint32_t fcpDl;
4012 	uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
4013 
4014 
4015 	/*
4016 	 *  If this is a task management command, there is no
4017 	 *  scsi packet associated with this lpfc_cmd.  The driver
4018 	 *  consumes it.
4019 	 */
4020 	if (fcpcmd->fcpCntl2) {
4021 		scsi_status = 0;
4022 		goto out;
4023 	}
4024 
4025 	if (resp_info & RSP_LEN_VALID) {
4026 		rsplen = be32_to_cpu(fcprsp->rspRspLen);
4027 		if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
4028 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4029 					 "2719 Invalid response length: "
4030 					 "tgt x%x lun x%llx cmnd x%x rsplen "
4031 					 "x%x\n", cmnd->device->id,
4032 					 cmnd->device->lun, cmnd->cmnd[0],
4033 					 rsplen);
4034 			host_status = DID_ERROR;
4035 			goto out;
4036 		}
4037 		if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
4038 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4039 				 "2757 Protocol failure detected during "
4040 				 "processing of FCP I/O op: "
4041 				 "tgt x%x lun x%llx cmnd x%x rspInfo3 x%x\n",
4042 				 cmnd->device->id,
4043 				 cmnd->device->lun, cmnd->cmnd[0],
4044 				 fcprsp->rspInfo3);
4045 			host_status = DID_ERROR;
4046 			goto out;
4047 		}
4048 	}
4049 
4050 	if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
4051 		uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
4052 		if (snslen > SCSI_SENSE_BUFFERSIZE)
4053 			snslen = SCSI_SENSE_BUFFERSIZE;
4054 
4055 		if (resp_info & RSP_LEN_VALID)
4056 		  rsplen = be32_to_cpu(fcprsp->rspRspLen);
4057 		memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
4058 	}
4059 	lp = (uint32_t *)cmnd->sense_buffer;
4060 
4061 	/* special handling for under run conditions */
4062 	if (!scsi_status && (resp_info & RESID_UNDER)) {
4063 		/* don't log under runs if fcp set... */
4064 		if (vport->cfg_log_verbose & LOG_FCP)
4065 			logit = LOG_FCP_ERROR;
4066 		/* unless operator says so */
4067 		if (vport->cfg_log_verbose & LOG_FCP_UNDER)
4068 			logit = LOG_FCP_UNDER;
4069 	}
4070 
4071 	lpfc_printf_vlog(vport, KERN_WARNING, logit,
4072 			 "9024 FCP command x%x failed: x%x SNS x%x x%x "
4073 			 "Data: x%x x%x x%x x%x x%x\n",
4074 			 cmnd->cmnd[0], scsi_status,
4075 			 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
4076 			 be32_to_cpu(fcprsp->rspResId),
4077 			 be32_to_cpu(fcprsp->rspSnsLen),
4078 			 be32_to_cpu(fcprsp->rspRspLen),
4079 			 fcprsp->rspInfo3);
4080 
4081 	scsi_set_resid(cmnd, 0);
4082 	fcpDl = be32_to_cpu(fcpcmd->fcpDl);
4083 	if (resp_info & RESID_UNDER) {
4084 		scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
4085 
4086 		lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
4087 				 "9025 FCP Underrun, expected %d, "
4088 				 "residual %d Data: x%x x%x x%x\n",
4089 				 fcpDl,
4090 				 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
4091 				 cmnd->underflow);
4092 
4093 		/*
4094 		 * If there is an under run, check if under run reported by
4095 		 * storage array is same as the under run reported by HBA.
4096 		 * If this is not same, there is a dropped frame.
4097 		 */
4098 		if (fcpi_parm && (scsi_get_resid(cmnd) != fcpi_parm)) {
4099 			lpfc_printf_vlog(vport, KERN_WARNING,
4100 					 LOG_FCP | LOG_FCP_ERROR,
4101 					 "9026 FCP Read Check Error "
4102 					 "and Underrun Data: x%x x%x x%x x%x\n",
4103 					 fcpDl,
4104 					 scsi_get_resid(cmnd), fcpi_parm,
4105 					 cmnd->cmnd[0]);
4106 			scsi_set_resid(cmnd, scsi_bufflen(cmnd));
4107 			host_status = DID_ERROR;
4108 		}
4109 		/*
4110 		 * The cmnd->underflow is the minimum number of bytes that must
4111 		 * be transferred for this command.  Provided a sense condition
4112 		 * is not present, make sure the actual amount transferred is at
4113 		 * least the underflow value or fail.
4114 		 */
4115 		if (!(resp_info & SNS_LEN_VALID) &&
4116 		    (scsi_status == SAM_STAT_GOOD) &&
4117 		    (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
4118 		     < cmnd->underflow)) {
4119 			lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4120 					 "9027 FCP command x%x residual "
4121 					 "underrun converted to error "
4122 					 "Data: x%x x%x x%x\n",
4123 					 cmnd->cmnd[0], scsi_bufflen(cmnd),
4124 					 scsi_get_resid(cmnd), cmnd->underflow);
4125 			host_status = DID_ERROR;
4126 		}
4127 	} else if (resp_info & RESID_OVER) {
4128 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4129 				 "9028 FCP command x%x residual overrun error. "
4130 				 "Data: x%x x%x\n", cmnd->cmnd[0],
4131 				 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
4132 		host_status = DID_ERROR;
4133 
4134 	/*
4135 	 * Check SLI validation that all the transfer was actually done
4136 	 * (fcpi_parm should be zero). Apply check only to reads.
4137 	 */
4138 	} else if (fcpi_parm) {
4139 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
4140 				 "9029 FCP %s Check Error Data: "
4141 				 "x%x x%x x%x x%x x%x\n",
4142 				 ((cmnd->sc_data_direction == DMA_FROM_DEVICE) ?
4143 				 "Read" : "Write"),
4144 				 fcpDl, be32_to_cpu(fcprsp->rspResId),
4145 				 fcpi_parm, cmnd->cmnd[0], scsi_status);
4146 
4147 		/* There is some issue with the LPe12000 that causes it
4148 		 * to miscalculate the fcpi_parm and falsely trip this
4149 		 * recovery logic.  Detect this case and don't error when true.
4150 		 */
4151 		if (fcpi_parm > fcpDl)
4152 			goto out;
4153 
4154 		switch (scsi_status) {
4155 		case SAM_STAT_GOOD:
4156 		case SAM_STAT_CHECK_CONDITION:
4157 			/* Fabric dropped a data frame. Fail any successful
4158 			 * command in which we detected dropped frames.
4159 			 * A status of good or some check conditions could
4160 			 * be considered a successful command.
4161 			 */
4162 			host_status = DID_ERROR;
4163 			break;
4164 		}
4165 		scsi_set_resid(cmnd, scsi_bufflen(cmnd));
4166 	}
4167 
4168  out:
4169 	cmnd->result = host_status << 16 | scsi_status;
4170 	lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, fcpi_parm);
4171 }
4172 
4173 /**
4174  * lpfc_fcp_io_cmd_wqe_cmpl - Complete a FCP IO
4175  * @phba: The hba for which this call is being executed.
4176  * @pwqeIn: The command WQE for the scsi cmnd.
4177  * @wcqe: Pointer to driver response CQE object.
4178  *
4179  * This routine assigns scsi command result by looking into response WQE
4180  * status field appropriately. This routine handles QUEUE FULL condition as
4181  * well by ramping down device queue depth.
4182  **/
4183 static void
4184 lpfc_fcp_io_cmd_wqe_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeIn,
4185 			 struct lpfc_wcqe_complete *wcqe)
4186 {
4187 	struct lpfc_io_buf *lpfc_cmd =
4188 		(struct lpfc_io_buf *)pwqeIn->context1;
4189 	struct lpfc_vport *vport = pwqeIn->vport;
4190 	struct lpfc_rport_data *rdata;
4191 	struct lpfc_nodelist *ndlp;
4192 	struct scsi_cmnd *cmd;
4193 	unsigned long flags;
4194 	struct lpfc_fast_path_event *fast_path_evt;
4195 	struct Scsi_Host *shost;
4196 	u32 logit = LOG_FCP;
4197 	u32 status, idx;
4198 	unsigned long iflags = 0;
4199 	u32 lat;
4200 	u8 wait_xb_clr = 0;
4201 
4202 	/* Sanity check on return of outstanding command */
4203 	if (!lpfc_cmd) {
4204 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4205 				 "9032 Null lpfc_cmd pointer. No "
4206 				 "release, skip completion\n");
4207 		return;
4208 	}
4209 
4210 	rdata = lpfc_cmd->rdata;
4211 	ndlp = rdata->pnode;
4212 
4213 	if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
4214 		/* TOREMOVE - currently this flag is checked during
4215 		 * the release of lpfc_iocbq. Remove once we move
4216 		 * to lpfc_wqe_job construct.
4217 		 *
4218 		 * This needs to be done outside buf_lock
4219 		 */
4220 		spin_lock_irqsave(&phba->hbalock, iflags);
4221 		lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_EXCHANGE_BUSY;
4222 		spin_unlock_irqrestore(&phba->hbalock, iflags);
4223 	}
4224 
4225 	/* Guard against abort handler being called at same time */
4226 	spin_lock(&lpfc_cmd->buf_lock);
4227 
4228 	/* Sanity check on return of outstanding command */
4229 	cmd = lpfc_cmd->pCmd;
4230 	if (!cmd) {
4231 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4232 				 "9042 I/O completion: Not an active IO\n");
4233 		spin_unlock(&lpfc_cmd->buf_lock);
4234 		lpfc_release_scsi_buf(phba, lpfc_cmd);
4235 		return;
4236 	}
4237 	idx = lpfc_cmd->cur_iocbq.hba_wqidx;
4238 	if (phba->sli4_hba.hdwq)
4239 		phba->sli4_hba.hdwq[idx].scsi_cstat.io_cmpls++;
4240 
4241 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4242 	if (unlikely(phba->hdwqstat_on & LPFC_CHECK_SCSI_IO))
4243 		this_cpu_inc(phba->sli4_hba.c_stat->cmpl_io);
4244 #endif
4245 	shost = cmd->device->host;
4246 
4247 	status = bf_get(lpfc_wcqe_c_status, wcqe);
4248 	lpfc_cmd->status = (status & LPFC_IOCB_STATUS_MASK);
4249 	lpfc_cmd->result = (wcqe->parameter & IOERR_PARAM_MASK);
4250 
4251 	lpfc_cmd->flags &= ~LPFC_SBUF_XBUSY;
4252 	if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
4253 		lpfc_cmd->flags |= LPFC_SBUF_XBUSY;
4254 		if (phba->cfg_fcp_wait_abts_rsp)
4255 			wait_xb_clr = 1;
4256 	}
4257 
4258 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4259 	if (lpfc_cmd->prot_data_type) {
4260 		struct scsi_dif_tuple *src = NULL;
4261 
4262 		src =  (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
4263 		/*
4264 		 * Used to restore any changes to protection
4265 		 * data for error injection.
4266 		 */
4267 		switch (lpfc_cmd->prot_data_type) {
4268 		case LPFC_INJERR_REFTAG:
4269 			src->ref_tag =
4270 				lpfc_cmd->prot_data;
4271 			break;
4272 		case LPFC_INJERR_APPTAG:
4273 			src->app_tag =
4274 				(uint16_t)lpfc_cmd->prot_data;
4275 			break;
4276 		case LPFC_INJERR_GUARD:
4277 			src->guard_tag =
4278 				(uint16_t)lpfc_cmd->prot_data;
4279 			break;
4280 		default:
4281 			break;
4282 		}
4283 
4284 		lpfc_cmd->prot_data = 0;
4285 		lpfc_cmd->prot_data_type = 0;
4286 		lpfc_cmd->prot_data_segment = NULL;
4287 	}
4288 #endif
4289 	if (unlikely(lpfc_cmd->status)) {
4290 		if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
4291 		    (lpfc_cmd->result & IOERR_DRVR_MASK))
4292 			lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4293 		else if (lpfc_cmd->status >= IOSTAT_CNT)
4294 			lpfc_cmd->status = IOSTAT_DEFAULT;
4295 		if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
4296 		    !lpfc_cmd->fcp_rsp->rspStatus3 &&
4297 		    (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
4298 		    !(vport->cfg_log_verbose & LOG_FCP_UNDER))
4299 			logit = 0;
4300 		else
4301 			logit = LOG_FCP | LOG_FCP_UNDER;
4302 		lpfc_printf_vlog(vport, KERN_WARNING, logit,
4303 				 "9034 FCP cmd x%x failed <%d/%lld> "
4304 				 "status: x%x result: x%x "
4305 				 "sid: x%x did: x%x oxid: x%x "
4306 				 "Data: x%x x%x x%x\n",
4307 				 cmd->cmnd[0],
4308 				 cmd->device ? cmd->device->id : 0xffff,
4309 				 cmd->device ? cmd->device->lun : 0xffff,
4310 				 lpfc_cmd->status, lpfc_cmd->result,
4311 				 vport->fc_myDID,
4312 				 (ndlp) ? ndlp->nlp_DID : 0,
4313 				 lpfc_cmd->cur_iocbq.sli4_xritag,
4314 				 wcqe->parameter, wcqe->total_data_placed,
4315 				 lpfc_cmd->cur_iocbq.iotag);
4316 	}
4317 
4318 	switch (lpfc_cmd->status) {
4319 	case IOSTAT_SUCCESS:
4320 		cmd->result = DID_OK << 16;
4321 		break;
4322 	case IOSTAT_FCP_RSP_ERROR:
4323 		lpfc_handle_fcp_err(vport, lpfc_cmd,
4324 				    pwqeIn->wqe.fcp_iread.total_xfer_len -
4325 				    wcqe->total_data_placed);
4326 		break;
4327 	case IOSTAT_NPORT_BSY:
4328 	case IOSTAT_FABRIC_BSY:
4329 		cmd->result = DID_TRANSPORT_DISRUPTED << 16;
4330 		fast_path_evt = lpfc_alloc_fast_evt(phba);
4331 		if (!fast_path_evt)
4332 			break;
4333 		fast_path_evt->un.fabric_evt.event_type =
4334 			FC_REG_FABRIC_EVENT;
4335 		fast_path_evt->un.fabric_evt.subcategory =
4336 			(lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
4337 			LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
4338 		if (ndlp) {
4339 			memcpy(&fast_path_evt->un.fabric_evt.wwpn,
4340 			       &ndlp->nlp_portname,
4341 				sizeof(struct lpfc_name));
4342 			memcpy(&fast_path_evt->un.fabric_evt.wwnn,
4343 			       &ndlp->nlp_nodename,
4344 				sizeof(struct lpfc_name));
4345 		}
4346 		fast_path_evt->vport = vport;
4347 		fast_path_evt->work_evt.evt =
4348 			LPFC_EVT_FASTPATH_MGMT_EVT;
4349 		spin_lock_irqsave(&phba->hbalock, flags);
4350 		list_add_tail(&fast_path_evt->work_evt.evt_listp,
4351 			      &phba->work_list);
4352 		spin_unlock_irqrestore(&phba->hbalock, flags);
4353 		lpfc_worker_wake_up(phba);
4354 		lpfc_printf_vlog(vport, KERN_WARNING, logit,
4355 				 "9035 Fabric/Node busy FCP cmd x%x failed"
4356 				 " <%d/%lld> "
4357 				 "status: x%x result: x%x "
4358 				 "sid: x%x did: x%x oxid: x%x "
4359 				 "Data: x%x x%x x%x\n",
4360 				 cmd->cmnd[0],
4361 				 cmd->device ? cmd->device->id : 0xffff,
4362 				 cmd->device ? cmd->device->lun : 0xffff,
4363 				 lpfc_cmd->status, lpfc_cmd->result,
4364 				 vport->fc_myDID,
4365 				 (ndlp) ? ndlp->nlp_DID : 0,
4366 				 lpfc_cmd->cur_iocbq.sli4_xritag,
4367 				 wcqe->parameter,
4368 				 wcqe->total_data_placed,
4369 				 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
4370 		break;
4371 	case IOSTAT_REMOTE_STOP:
4372 		if (ndlp) {
4373 			/* This I/O was aborted by the target, we don't
4374 			 * know the rxid and because we did not send the
4375 			 * ABTS we cannot generate and RRQ.
4376 			 */
4377 			lpfc_set_rrq_active(phba, ndlp,
4378 					    lpfc_cmd->cur_iocbq.sli4_lxritag,
4379 					    0, 0);
4380 		}
4381 		fallthrough;
4382 	case IOSTAT_LOCAL_REJECT:
4383 		if (lpfc_cmd->result & IOERR_DRVR_MASK)
4384 			lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4385 		if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
4386 		    lpfc_cmd->result ==
4387 		    IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
4388 		    lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
4389 		    lpfc_cmd->result ==
4390 		    IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
4391 			cmd->result = DID_NO_CONNECT << 16;
4392 			break;
4393 		}
4394 		if (lpfc_cmd->result == IOERR_INVALID_RPI ||
4395 		    lpfc_cmd->result == IOERR_NO_RESOURCES ||
4396 		    lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
4397 		    lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
4398 			cmd->result = DID_REQUEUE << 16;
4399 			break;
4400 		}
4401 		if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
4402 		     lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
4403 		     status == CQE_STATUS_DI_ERROR) {
4404 			if (scsi_get_prot_op(cmd) !=
4405 			    SCSI_PROT_NORMAL) {
4406 				/*
4407 				 * This is a response for a BG enabled
4408 				 * cmd. Parse BG error
4409 				 */
4410 				lpfc_sli4_parse_bg_err(phba, lpfc_cmd,
4411 						       wcqe);
4412 				break;
4413 			}
4414 			lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
4415 				 "9040 non-zero BGSTAT on unprotected cmd\n");
4416 		}
4417 		lpfc_printf_vlog(vport, KERN_WARNING, logit,
4418 				 "9036 Local Reject FCP cmd x%x failed"
4419 				 " <%d/%lld> "
4420 				 "status: x%x result: x%x "
4421 				 "sid: x%x did: x%x oxid: x%x "
4422 				 "Data: x%x x%x x%x\n",
4423 				 cmd->cmnd[0],
4424 				 cmd->device ? cmd->device->id : 0xffff,
4425 				 cmd->device ? cmd->device->lun : 0xffff,
4426 				 lpfc_cmd->status, lpfc_cmd->result,
4427 				 vport->fc_myDID,
4428 				 (ndlp) ? ndlp->nlp_DID : 0,
4429 				 lpfc_cmd->cur_iocbq.sli4_xritag,
4430 				 wcqe->parameter,
4431 				 wcqe->total_data_placed,
4432 				 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
4433 		fallthrough;
4434 	default:
4435 		if (lpfc_cmd->status >= IOSTAT_CNT)
4436 			lpfc_cmd->status = IOSTAT_DEFAULT;
4437 		cmd->result = DID_ERROR << 16;
4438 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
4439 				 "9037 FCP Completion Error: xri %x "
4440 				 "status x%x result x%x [x%x] "
4441 				 "placed x%x\n",
4442 				 lpfc_cmd->cur_iocbq.sli4_xritag,
4443 				 lpfc_cmd->status, lpfc_cmd->result,
4444 				 wcqe->parameter,
4445 				 wcqe->total_data_placed);
4446 	}
4447 	if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4448 		u32 *lp = (u32 *)cmd->sense_buffer;
4449 
4450 		lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4451 				 "9039 Iodone <%d/%llu> cmd x%px, error "
4452 				 "x%x SNS x%x x%x Data: x%x x%x\n",
4453 				 cmd->device->id, cmd->device->lun, cmd,
4454 				 cmd->result, *lp, *(lp + 3), cmd->retries,
4455 				 scsi_get_resid(cmd));
4456 	}
4457 
4458 	lpfc_update_stats(vport, lpfc_cmd);
4459 
4460 	if (vport->cfg_max_scsicmpl_time &&
4461 	    time_after(jiffies, lpfc_cmd->start_time +
4462 	    msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
4463 		spin_lock_irqsave(shost->host_lock, flags);
4464 		if (ndlp) {
4465 			if (ndlp->cmd_qdepth >
4466 				atomic_read(&ndlp->cmd_pending) &&
4467 				(atomic_read(&ndlp->cmd_pending) >
4468 				LPFC_MIN_TGT_QDEPTH) &&
4469 				(cmd->cmnd[0] == READ_10 ||
4470 				cmd->cmnd[0] == WRITE_10))
4471 				ndlp->cmd_qdepth =
4472 					atomic_read(&ndlp->cmd_pending);
4473 
4474 			ndlp->last_change_time = jiffies;
4475 		}
4476 		spin_unlock_irqrestore(shost->host_lock, flags);
4477 	}
4478 	lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
4479 
4480 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4481 	if (lpfc_cmd->ts_cmd_start) {
4482 		lpfc_cmd->ts_isr_cmpl = lpfc_cmd->cur_iocbq.isr_timestamp;
4483 		lpfc_cmd->ts_data_io = ktime_get_ns();
4484 		phba->ktime_last_cmd = lpfc_cmd->ts_data_io;
4485 		lpfc_io_ktime(phba, lpfc_cmd);
4486 	}
4487 #endif
4488 	if (likely(!wait_xb_clr))
4489 		lpfc_cmd->pCmd = NULL;
4490 	spin_unlock(&lpfc_cmd->buf_lock);
4491 
4492 	/* Check if IO qualified for CMF */
4493 	if (phba->cmf_active_mode != LPFC_CFG_OFF &&
4494 	    cmd->sc_data_direction == DMA_FROM_DEVICE &&
4495 	    (scsi_sg_count(cmd))) {
4496 		/* Used when calculating average latency */
4497 		lat = ktime_get_ns() - lpfc_cmd->rx_cmd_start;
4498 		lpfc_update_cmf_cmpl(phba, lat, scsi_bufflen(cmd), shost);
4499 	}
4500 
4501 	if (wait_xb_clr)
4502 		goto out;
4503 
4504 	/* The sdev is not guaranteed to be valid post scsi_done upcall. */
4505 	cmd->scsi_done(cmd);
4506 
4507 	/*
4508 	 * If there is an abort thread waiting for command completion
4509 	 * wake up the thread.
4510 	 */
4511 	spin_lock(&lpfc_cmd->buf_lock);
4512 	lpfc_cmd->cur_iocbq.iocb_flag &= ~LPFC_DRIVER_ABORTED;
4513 	if (lpfc_cmd->waitq)
4514 		wake_up(lpfc_cmd->waitq);
4515 	spin_unlock(&lpfc_cmd->buf_lock);
4516 out:
4517 	lpfc_release_scsi_buf(phba, lpfc_cmd);
4518 }
4519 
4520 /**
4521  * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
4522  * @phba: The Hba for which this call is being executed.
4523  * @pIocbIn: The command IOCBQ for the scsi cmnd.
4524  * @pIocbOut: The response IOCBQ for the scsi cmnd.
4525  *
4526  * This routine assigns scsi command result by looking into response IOCB
4527  * status field appropriately. This routine handles QUEUE FULL condition as
4528  * well by ramping down device queue depth.
4529  **/
4530 static void
4531 lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
4532 			struct lpfc_iocbq *pIocbOut)
4533 {
4534 	struct lpfc_io_buf *lpfc_cmd =
4535 		(struct lpfc_io_buf *) pIocbIn->context1;
4536 	struct lpfc_vport      *vport = pIocbIn->vport;
4537 	struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
4538 	struct lpfc_nodelist *pnode = rdata->pnode;
4539 	struct scsi_cmnd *cmd;
4540 	unsigned long flags;
4541 	struct lpfc_fast_path_event *fast_path_evt;
4542 	struct Scsi_Host *shost;
4543 	int idx;
4544 	uint32_t logit = LOG_FCP;
4545 
4546 	/* Guard against abort handler being called at same time */
4547 	spin_lock(&lpfc_cmd->buf_lock);
4548 
4549 	/* Sanity check on return of outstanding command */
4550 	cmd = lpfc_cmd->pCmd;
4551 	if (!cmd || !phba) {
4552 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4553 				 "2621 IO completion: Not an active IO\n");
4554 		spin_unlock(&lpfc_cmd->buf_lock);
4555 		return;
4556 	}
4557 
4558 	idx = lpfc_cmd->cur_iocbq.hba_wqidx;
4559 	if (phba->sli4_hba.hdwq)
4560 		phba->sli4_hba.hdwq[idx].scsi_cstat.io_cmpls++;
4561 
4562 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4563 	if (unlikely(phba->hdwqstat_on & LPFC_CHECK_SCSI_IO))
4564 		this_cpu_inc(phba->sli4_hba.c_stat->cmpl_io);
4565 #endif
4566 	shost = cmd->device->host;
4567 
4568 	lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
4569 	lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
4570 	/* pick up SLI4 exchange busy status from HBA */
4571 	lpfc_cmd->flags &= ~LPFC_SBUF_XBUSY;
4572 	if (pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY)
4573 		lpfc_cmd->flags |= LPFC_SBUF_XBUSY;
4574 
4575 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4576 	if (lpfc_cmd->prot_data_type) {
4577 		struct scsi_dif_tuple *src = NULL;
4578 
4579 		src =  (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
4580 		/*
4581 		 * Used to restore any changes to protection
4582 		 * data for error injection.
4583 		 */
4584 		switch (lpfc_cmd->prot_data_type) {
4585 		case LPFC_INJERR_REFTAG:
4586 			src->ref_tag =
4587 				lpfc_cmd->prot_data;
4588 			break;
4589 		case LPFC_INJERR_APPTAG:
4590 			src->app_tag =
4591 				(uint16_t)lpfc_cmd->prot_data;
4592 			break;
4593 		case LPFC_INJERR_GUARD:
4594 			src->guard_tag =
4595 				(uint16_t)lpfc_cmd->prot_data;
4596 			break;
4597 		default:
4598 			break;
4599 		}
4600 
4601 		lpfc_cmd->prot_data = 0;
4602 		lpfc_cmd->prot_data_type = 0;
4603 		lpfc_cmd->prot_data_segment = NULL;
4604 	}
4605 #endif
4606 
4607 	if (unlikely(lpfc_cmd->status)) {
4608 		if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
4609 		    (lpfc_cmd->result & IOERR_DRVR_MASK))
4610 			lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4611 		else if (lpfc_cmd->status >= IOSTAT_CNT)
4612 			lpfc_cmd->status = IOSTAT_DEFAULT;
4613 		if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
4614 		    !lpfc_cmd->fcp_rsp->rspStatus3 &&
4615 		    (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
4616 		    !(vport->cfg_log_verbose & LOG_FCP_UNDER))
4617 			logit = 0;
4618 		else
4619 			logit = LOG_FCP | LOG_FCP_UNDER;
4620 		lpfc_printf_vlog(vport, KERN_WARNING, logit,
4621 			 "9030 FCP cmd x%x failed <%d/%lld> "
4622 			 "status: x%x result: x%x "
4623 			 "sid: x%x did: x%x oxid: x%x "
4624 			 "Data: x%x x%x\n",
4625 			 cmd->cmnd[0],
4626 			 cmd->device ? cmd->device->id : 0xffff,
4627 			 cmd->device ? cmd->device->lun : 0xffff,
4628 			 lpfc_cmd->status, lpfc_cmd->result,
4629 			 vport->fc_myDID,
4630 			 (pnode) ? pnode->nlp_DID : 0,
4631 			 phba->sli_rev == LPFC_SLI_REV4 ?
4632 			     lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
4633 			 pIocbOut->iocb.ulpContext,
4634 			 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
4635 
4636 		switch (lpfc_cmd->status) {
4637 		case IOSTAT_FCP_RSP_ERROR:
4638 			/* Call FCP RSP handler to determine result */
4639 			lpfc_handle_fcp_err(vport, lpfc_cmd,
4640 					    pIocbOut->iocb.un.fcpi.fcpi_parm);
4641 			break;
4642 		case IOSTAT_NPORT_BSY:
4643 		case IOSTAT_FABRIC_BSY:
4644 			cmd->result = DID_TRANSPORT_DISRUPTED << 16;
4645 			fast_path_evt = lpfc_alloc_fast_evt(phba);
4646 			if (!fast_path_evt)
4647 				break;
4648 			fast_path_evt->un.fabric_evt.event_type =
4649 				FC_REG_FABRIC_EVENT;
4650 			fast_path_evt->un.fabric_evt.subcategory =
4651 				(lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
4652 				LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
4653 			if (pnode) {
4654 				memcpy(&fast_path_evt->un.fabric_evt.wwpn,
4655 					&pnode->nlp_portname,
4656 					sizeof(struct lpfc_name));
4657 				memcpy(&fast_path_evt->un.fabric_evt.wwnn,
4658 					&pnode->nlp_nodename,
4659 					sizeof(struct lpfc_name));
4660 			}
4661 			fast_path_evt->vport = vport;
4662 			fast_path_evt->work_evt.evt =
4663 				LPFC_EVT_FASTPATH_MGMT_EVT;
4664 			spin_lock_irqsave(&phba->hbalock, flags);
4665 			list_add_tail(&fast_path_evt->work_evt.evt_listp,
4666 				&phba->work_list);
4667 			spin_unlock_irqrestore(&phba->hbalock, flags);
4668 			lpfc_worker_wake_up(phba);
4669 			break;
4670 		case IOSTAT_LOCAL_REJECT:
4671 		case IOSTAT_REMOTE_STOP:
4672 			if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
4673 			    lpfc_cmd->result ==
4674 					IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
4675 			    lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
4676 			    lpfc_cmd->result ==
4677 					IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
4678 				cmd->result = DID_NO_CONNECT << 16;
4679 				break;
4680 			}
4681 			if (lpfc_cmd->result == IOERR_INVALID_RPI ||
4682 			    lpfc_cmd->result == IOERR_NO_RESOURCES ||
4683 			    lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
4684 			    lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
4685 				cmd->result = DID_REQUEUE << 16;
4686 				break;
4687 			}
4688 			if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
4689 			     lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
4690 			     pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
4691 				if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
4692 					/*
4693 					 * This is a response for a BG enabled
4694 					 * cmd. Parse BG error
4695 					 */
4696 					lpfc_parse_bg_err(phba, lpfc_cmd,
4697 							pIocbOut);
4698 					break;
4699 				} else {
4700 					lpfc_printf_vlog(vport, KERN_WARNING,
4701 							LOG_BG,
4702 							"9031 non-zero BGSTAT "
4703 							"on unprotected cmd\n");
4704 				}
4705 			}
4706 			if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
4707 				&& (phba->sli_rev == LPFC_SLI_REV4)
4708 				&& pnode) {
4709 				/* This IO was aborted by the target, we don't
4710 				 * know the rxid and because we did not send the
4711 				 * ABTS we cannot generate and RRQ.
4712 				 */
4713 				lpfc_set_rrq_active(phba, pnode,
4714 					lpfc_cmd->cur_iocbq.sli4_lxritag,
4715 					0, 0);
4716 			}
4717 			fallthrough;
4718 		default:
4719 			cmd->result = DID_ERROR << 16;
4720 			break;
4721 		}
4722 
4723 		if (!pnode || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
4724 			cmd->result = DID_TRANSPORT_DISRUPTED << 16 |
4725 				      SAM_STAT_BUSY;
4726 	} else
4727 		cmd->result = DID_OK << 16;
4728 
4729 	if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4730 		uint32_t *lp = (uint32_t *)cmd->sense_buffer;
4731 
4732 		lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4733 				 "0710 Iodone <%d/%llu> cmd x%px, error "
4734 				 "x%x SNS x%x x%x Data: x%x x%x\n",
4735 				 cmd->device->id, cmd->device->lun, cmd,
4736 				 cmd->result, *lp, *(lp + 3), cmd->retries,
4737 				 scsi_get_resid(cmd));
4738 	}
4739 
4740 	lpfc_update_stats(vport, lpfc_cmd);
4741 	if (vport->cfg_max_scsicmpl_time &&
4742 	   time_after(jiffies, lpfc_cmd->start_time +
4743 		msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
4744 		spin_lock_irqsave(shost->host_lock, flags);
4745 		if (pnode) {
4746 			if (pnode->cmd_qdepth >
4747 				atomic_read(&pnode->cmd_pending) &&
4748 				(atomic_read(&pnode->cmd_pending) >
4749 				LPFC_MIN_TGT_QDEPTH) &&
4750 				((cmd->cmnd[0] == READ_10) ||
4751 				(cmd->cmnd[0] == WRITE_10)))
4752 				pnode->cmd_qdepth =
4753 					atomic_read(&pnode->cmd_pending);
4754 
4755 			pnode->last_change_time = jiffies;
4756 		}
4757 		spin_unlock_irqrestore(shost->host_lock, flags);
4758 	}
4759 	lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
4760 
4761 	lpfc_cmd->pCmd = NULL;
4762 	spin_unlock(&lpfc_cmd->buf_lock);
4763 
4764 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4765 	if (lpfc_cmd->ts_cmd_start) {
4766 		lpfc_cmd->ts_isr_cmpl = pIocbIn->isr_timestamp;
4767 		lpfc_cmd->ts_data_io = ktime_get_ns();
4768 		phba->ktime_last_cmd = lpfc_cmd->ts_data_io;
4769 		lpfc_io_ktime(phba, lpfc_cmd);
4770 	}
4771 #endif
4772 
4773 	/* The sdev is not guaranteed to be valid post scsi_done upcall. */
4774 	cmd->scsi_done(cmd);
4775 
4776 	/*
4777 	 * If there is an abort thread waiting for command completion
4778 	 * wake up the thread.
4779 	 */
4780 	spin_lock(&lpfc_cmd->buf_lock);
4781 	lpfc_cmd->cur_iocbq.iocb_flag &= ~LPFC_DRIVER_ABORTED;
4782 	if (lpfc_cmd->waitq)
4783 		wake_up(lpfc_cmd->waitq);
4784 	spin_unlock(&lpfc_cmd->buf_lock);
4785 
4786 	lpfc_release_scsi_buf(phba, lpfc_cmd);
4787 }
4788 
4789 /**
4790  * lpfc_scsi_prep_cmnd_buf_s3 - SLI-3 IOCB init for the IO
4791  * @vport: Pointer to vport object.
4792  * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
4793  * @tmo: timeout value for the IO
4794  *
4795  * Based on the data-direction of the command, initialize IOCB
4796  * in the I/O buffer. Fill in the IOCB fields which are independent
4797  * of the scsi buffer
4798  *
4799  * RETURNS 0 - SUCCESS,
4800  **/
4801 static int lpfc_scsi_prep_cmnd_buf_s3(struct lpfc_vport *vport,
4802 				      struct lpfc_io_buf *lpfc_cmd,
4803 				      uint8_t tmo)
4804 {
4805 	IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4806 	struct lpfc_iocbq *piocbq = &lpfc_cmd->cur_iocbq;
4807 	struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4808 	struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4809 	struct lpfc_nodelist *pnode = lpfc_cmd->ndlp;
4810 	int datadir = scsi_cmnd->sc_data_direction;
4811 	u32 fcpdl;
4812 
4813 	piocbq->iocb.un.fcpi.fcpi_XRdy = 0;
4814 
4815 	/*
4816 	 * There are three possibilities here - use scatter-gather segment, use
4817 	 * the single mapping, or neither.  Start the lpfc command prep by
4818 	 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4819 	 * data bde entry.
4820 	 */
4821 	if (scsi_sg_count(scsi_cmnd)) {
4822 		if (datadir == DMA_TO_DEVICE) {
4823 			iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
4824 			iocb_cmd->ulpPU = PARM_READ_CHECK;
4825 			if (vport->cfg_first_burst_size &&
4826 			    (pnode->nlp_flag & NLP_FIRSTBURST)) {
4827 				u32 xrdy_len;
4828 
4829 				fcpdl = scsi_bufflen(scsi_cmnd);
4830 				xrdy_len = min(fcpdl,
4831 					       vport->cfg_first_burst_size);
4832 				piocbq->iocb.un.fcpi.fcpi_XRdy = xrdy_len;
4833 			}
4834 			fcp_cmnd->fcpCntl3 = WRITE_DATA;
4835 		} else {
4836 			iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4837 			iocb_cmd->ulpPU = PARM_READ_CHECK;
4838 			fcp_cmnd->fcpCntl3 = READ_DATA;
4839 		}
4840 	} else {
4841 		iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4842 		iocb_cmd->un.fcpi.fcpi_parm = 0;
4843 		iocb_cmd->ulpPU = 0;
4844 		fcp_cmnd->fcpCntl3 = 0;
4845 	}
4846 
4847 	/*
4848 	 * Finish initializing those IOCB fields that are independent
4849 	 * of the scsi_cmnd request_buffer
4850 	 */
4851 	piocbq->iocb.ulpContext = pnode->nlp_rpi;
4852 	if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4853 		piocbq->iocb.ulpFCP2Rcvy = 1;
4854 	else
4855 		piocbq->iocb.ulpFCP2Rcvy = 0;
4856 
4857 	piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4858 	piocbq->context1  = lpfc_cmd;
4859 	if (!piocbq->iocb_cmpl)
4860 		piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4861 	piocbq->iocb.ulpTimeout = tmo;
4862 	piocbq->vport = vport;
4863 	return 0;
4864 }
4865 
4866 /**
4867  * lpfc_scsi_prep_cmnd_buf_s4 - SLI-4 WQE init for the IO
4868  * @vport: Pointer to vport object.
4869  * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
4870  * @tmo: timeout value for the IO
4871  *
4872  * Based on the data-direction of the command copy WQE template
4873  * to I/O buffer WQE. Fill in the WQE fields which are independent
4874  * of the scsi buffer
4875  *
4876  * RETURNS 0 - SUCCESS,
4877  **/
4878 static int lpfc_scsi_prep_cmnd_buf_s4(struct lpfc_vport *vport,
4879 				      struct lpfc_io_buf *lpfc_cmd,
4880 				      uint8_t tmo)
4881 {
4882 	struct lpfc_hba *phba = vport->phba;
4883 	struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4884 	struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4885 	struct lpfc_sli4_hdw_queue *hdwq = NULL;
4886 	struct lpfc_iocbq *pwqeq = &lpfc_cmd->cur_iocbq;
4887 	struct lpfc_nodelist *pnode = lpfc_cmd->ndlp;
4888 	union lpfc_wqe128 *wqe = &pwqeq->wqe;
4889 	u16 idx = lpfc_cmd->hdwq_no;
4890 	int datadir = scsi_cmnd->sc_data_direction;
4891 
4892 	hdwq = &phba->sli4_hba.hdwq[idx];
4893 
4894 	/* Initialize 64 bytes only */
4895 	memset(wqe, 0, sizeof(union lpfc_wqe128));
4896 
4897 	/*
4898 	 * There are three possibilities here - use scatter-gather segment, use
4899 	 * the single mapping, or neither.
4900 	 */
4901 	if (scsi_sg_count(scsi_cmnd)) {
4902 		if (datadir == DMA_TO_DEVICE) {
4903 			/* From the iwrite template, initialize words 7 -  11 */
4904 			memcpy(&wqe->words[7],
4905 			       &lpfc_iwrite_cmd_template.words[7],
4906 			       sizeof(uint32_t) * 5);
4907 
4908 			fcp_cmnd->fcpCntl3 = WRITE_DATA;
4909 			if (hdwq)
4910 				hdwq->scsi_cstat.output_requests++;
4911 		} else {
4912 			/* From the iread template, initialize words 7 - 11 */
4913 			memcpy(&wqe->words[7],
4914 			       &lpfc_iread_cmd_template.words[7],
4915 			       sizeof(uint32_t) * 5);
4916 
4917 			/* Word 7 */
4918 			bf_set(wqe_tmo, &wqe->fcp_iread.wqe_com, tmo);
4919 
4920 			fcp_cmnd->fcpCntl3 = READ_DATA;
4921 			if (hdwq)
4922 				hdwq->scsi_cstat.input_requests++;
4923 
4924 			/* For a CMF Managed port, iod must be zero'ed */
4925 			if (phba->cmf_active_mode == LPFC_CFG_MANAGED)
4926 				bf_set(wqe_iod, &wqe->fcp_iread.wqe_com,
4927 				       LPFC_WQE_IOD_NONE);
4928 		}
4929 	} else {
4930 		/* From the icmnd template, initialize words 4 - 11 */
4931 		memcpy(&wqe->words[4], &lpfc_icmnd_cmd_template.words[4],
4932 		       sizeof(uint32_t) * 8);
4933 
4934 		/* Word 7 */
4935 		bf_set(wqe_tmo, &wqe->fcp_icmd.wqe_com, tmo);
4936 
4937 		fcp_cmnd->fcpCntl3 = 0;
4938 		if (hdwq)
4939 			hdwq->scsi_cstat.control_requests++;
4940 	}
4941 
4942 	/*
4943 	 * Finish initializing those WQE fields that are independent
4944 	 * of the request_buffer
4945 	 */
4946 
4947 	 /* Word 3 */
4948 	bf_set(payload_offset_len, &wqe->fcp_icmd,
4949 	       sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
4950 
4951 	/* Word 6 */
4952 	bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
4953 	       phba->sli4_hba.rpi_ids[pnode->nlp_rpi]);
4954 	bf_set(wqe_xri_tag, &wqe->generic.wqe_com, pwqeq->sli4_xritag);
4955 
4956 	/* Word 7*/
4957 	if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4958 		bf_set(wqe_erp, &wqe->generic.wqe_com, 1);
4959 
4960 	bf_set(wqe_class, &wqe->generic.wqe_com,
4961 	       (pnode->nlp_fcp_info & 0x0f));
4962 
4963 	 /* Word 8 */
4964 	wqe->generic.wqe_com.abort_tag = pwqeq->iotag;
4965 
4966 	/* Word 9 */
4967 	bf_set(wqe_reqtag, &wqe->generic.wqe_com, pwqeq->iotag);
4968 
4969 	pwqeq->vport = vport;
4970 	pwqeq->vport = vport;
4971 	pwqeq->context1 = lpfc_cmd;
4972 	pwqeq->hba_wqidx = lpfc_cmd->hdwq_no;
4973 	pwqeq->wqe_cmpl = lpfc_fcp_io_cmd_wqe_cmpl;
4974 
4975 	return 0;
4976 }
4977 
4978 /**
4979  * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
4980  * @vport: The virtual port for which this call is being executed.
4981  * @lpfc_cmd: The scsi command which needs to send.
4982  * @pnode: Pointer to lpfc_nodelist.
4983  *
4984  * This routine initializes fcp_cmnd and iocb data structure from scsi command
4985  * to transfer for device with SLI3 interface spec.
4986  **/
4987 static int
4988 lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd,
4989 		    struct lpfc_nodelist *pnode)
4990 {
4991 	struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4992 	struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4993 	u8 *ptr;
4994 
4995 	if (!pnode)
4996 		return 0;
4997 
4998 	lpfc_cmd->fcp_rsp->rspSnsLen = 0;
4999 	/* clear task management bits */
5000 	lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
5001 
5002 	int_to_scsilun(lpfc_cmd->pCmd->device->lun,
5003 		       &lpfc_cmd->fcp_cmnd->fcp_lun);
5004 
5005 	ptr = &fcp_cmnd->fcpCdb[0];
5006 	memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
5007 	if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
5008 		ptr += scsi_cmnd->cmd_len;
5009 		memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
5010 	}
5011 
5012 	fcp_cmnd->fcpCntl1 = SIMPLE_Q;
5013 
5014 	lpfc_scsi_prep_cmnd_buf(vport, lpfc_cmd, lpfc_cmd->timeout);
5015 
5016 	return 0;
5017 }
5018 
5019 /**
5020  * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
5021  * @vport: The virtual port for which this call is being executed.
5022  * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
5023  * @lun: Logical unit number.
5024  * @task_mgmt_cmd: SCSI task management command.
5025  *
5026  * This routine creates FCP information unit corresponding to @task_mgmt_cmd
5027  * for device with SLI-3 interface spec.
5028  *
5029  * Return codes:
5030  *   0 - Error
5031  *   1 - Success
5032  **/
5033 static int
5034 lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
5035 			     struct lpfc_io_buf *lpfc_cmd,
5036 			     uint64_t lun,
5037 			     uint8_t task_mgmt_cmd)
5038 {
5039 	struct lpfc_iocbq *piocbq;
5040 	IOCB_t *piocb;
5041 	struct fcp_cmnd *fcp_cmnd;
5042 	struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
5043 	struct lpfc_nodelist *ndlp = rdata->pnode;
5044 
5045 	if (!ndlp || ndlp->nlp_state != NLP_STE_MAPPED_NODE)
5046 		return 0;
5047 
5048 	piocbq = &(lpfc_cmd->cur_iocbq);
5049 	piocbq->vport = vport;
5050 
5051 	piocb = &piocbq->iocb;
5052 
5053 	fcp_cmnd = lpfc_cmd->fcp_cmnd;
5054 	/* Clear out any old data in the FCP command area */
5055 	memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
5056 	int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
5057 	fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
5058 	if (vport->phba->sli_rev == 3 &&
5059 	    !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
5060 		lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
5061 	piocb->ulpCommand = CMD_FCP_ICMND64_CR;
5062 	piocb->ulpContext = ndlp->nlp_rpi;
5063 	if (vport->phba->sli_rev == LPFC_SLI_REV4) {
5064 		piocb->ulpContext =
5065 		  vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
5066 	}
5067 	piocb->ulpFCP2Rcvy = (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) ? 1 : 0;
5068 	piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
5069 	piocb->ulpPU = 0;
5070 	piocb->un.fcpi.fcpi_parm = 0;
5071 
5072 	/* ulpTimeout is only one byte */
5073 	if (lpfc_cmd->timeout > 0xff) {
5074 		/*
5075 		 * Do not timeout the command at the firmware level.
5076 		 * The driver will provide the timeout mechanism.
5077 		 */
5078 		piocb->ulpTimeout = 0;
5079 	} else
5080 		piocb->ulpTimeout = lpfc_cmd->timeout;
5081 
5082 	if (vport->phba->sli_rev == LPFC_SLI_REV4)
5083 		lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
5084 
5085 	return 1;
5086 }
5087 
5088 /**
5089  * lpfc_scsi_api_table_setup - Set up scsi api function jump table
5090  * @phba: The hba struct for which this call is being executed.
5091  * @dev_grp: The HBA PCI-Device group number.
5092  *
5093  * This routine sets up the SCSI interface API function jump table in @phba
5094  * struct.
5095  * Returns: 0 - success, -ENODEV - failure.
5096  **/
5097 int
5098 lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5099 {
5100 
5101 	phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
5102 
5103 	switch (dev_grp) {
5104 	case LPFC_PCI_DEV_LP:
5105 		phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
5106 		phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
5107 		phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
5108 		phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
5109 		phba->lpfc_scsi_prep_cmnd_buf = lpfc_scsi_prep_cmnd_buf_s3;
5110 		break;
5111 	case LPFC_PCI_DEV_OC:
5112 		phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
5113 		phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
5114 		phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
5115 		phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
5116 		phba->lpfc_scsi_prep_cmnd_buf = lpfc_scsi_prep_cmnd_buf_s4;
5117 		break;
5118 	default:
5119 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5120 				"1418 Invalid HBA PCI-device group: 0x%x\n",
5121 				dev_grp);
5122 		return -ENODEV;
5123 	}
5124 	phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
5125 	phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
5126 	return 0;
5127 }
5128 
5129 /**
5130  * lpfc_tskmgmt_def_cmpl - IOCB completion routine for task management command
5131  * @phba: The Hba for which this call is being executed.
5132  * @cmdiocbq: Pointer to lpfc_iocbq data structure.
5133  * @rspiocbq: Pointer to lpfc_iocbq data structure.
5134  *
5135  * This routine is IOCB completion routine for device reset and target reset
5136  * routine. This routine release scsi buffer associated with lpfc_cmd.
5137  **/
5138 static void
5139 lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
5140 			struct lpfc_iocbq *cmdiocbq,
5141 			struct lpfc_iocbq *rspiocbq)
5142 {
5143 	struct lpfc_io_buf *lpfc_cmd =
5144 		(struct lpfc_io_buf *) cmdiocbq->context1;
5145 	if (lpfc_cmd)
5146 		lpfc_release_scsi_buf(phba, lpfc_cmd);
5147 	return;
5148 }
5149 
5150 /**
5151  * lpfc_check_pci_resettable - Walks list of devices on pci_dev's bus to check
5152  *                             if issuing a pci_bus_reset is possibly unsafe
5153  * @phba: lpfc_hba pointer.
5154  *
5155  * Description:
5156  * Walks the bus_list to ensure only PCI devices with Emulex
5157  * vendor id, device ids that support hot reset, and only one occurrence
5158  * of function 0.
5159  *
5160  * Returns:
5161  * -EBADSLT,  detected invalid device
5162  *      0,    successful
5163  */
5164 int
5165 lpfc_check_pci_resettable(struct lpfc_hba *phba)
5166 {
5167 	const struct pci_dev *pdev = phba->pcidev;
5168 	struct pci_dev *ptr = NULL;
5169 	u8 counter = 0;
5170 
5171 	/* Walk the list of devices on the pci_dev's bus */
5172 	list_for_each_entry(ptr, &pdev->bus->devices, bus_list) {
5173 		/* Check for Emulex Vendor ID */
5174 		if (ptr->vendor != PCI_VENDOR_ID_EMULEX) {
5175 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5176 					"8346 Non-Emulex vendor found: "
5177 					"0x%04x\n", ptr->vendor);
5178 			return -EBADSLT;
5179 		}
5180 
5181 		/* Check for valid Emulex Device ID */
5182 		if (phba->sli_rev != LPFC_SLI_REV4 ||
5183 		    phba->hba_flag & HBA_FCOE_MODE) {
5184 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5185 					"8347 Incapable PCI reset device: "
5186 					"0x%04x\n", ptr->device);
5187 			return -EBADSLT;
5188 		}
5189 
5190 		/* Check for only one function 0 ID to ensure only one HBA on
5191 		 * secondary bus
5192 		 */
5193 		if (ptr->devfn == 0) {
5194 			if (++counter > 1) {
5195 				lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5196 						"8348 More than one device on "
5197 						"secondary bus found\n");
5198 				return -EBADSLT;
5199 			}
5200 		}
5201 	}
5202 
5203 	return 0;
5204 }
5205 
5206 /**
5207  * lpfc_info - Info entry point of scsi_host_template data structure
5208  * @host: The scsi host for which this call is being executed.
5209  *
5210  * This routine provides module information about hba.
5211  *
5212  * Reutrn code:
5213  *   Pointer to char - Success.
5214  **/
5215 const char *
5216 lpfc_info(struct Scsi_Host *host)
5217 {
5218 	struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
5219 	struct lpfc_hba   *phba = vport->phba;
5220 	int link_speed = 0;
5221 	static char lpfcinfobuf[384];
5222 	char tmp[384] = {0};
5223 
5224 	memset(lpfcinfobuf, 0, sizeof(lpfcinfobuf));
5225 	if (phba && phba->pcidev){
5226 		/* Model Description */
5227 		scnprintf(tmp, sizeof(tmp), phba->ModelDesc);
5228 		if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
5229 		    sizeof(lpfcinfobuf))
5230 			goto buffer_done;
5231 
5232 		/* PCI Info */
5233 		scnprintf(tmp, sizeof(tmp),
5234 			  " on PCI bus %02x device %02x irq %d",
5235 			  phba->pcidev->bus->number, phba->pcidev->devfn,
5236 			  phba->pcidev->irq);
5237 		if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
5238 		    sizeof(lpfcinfobuf))
5239 			goto buffer_done;
5240 
5241 		/* Port Number */
5242 		if (phba->Port[0]) {
5243 			scnprintf(tmp, sizeof(tmp), " port %s", phba->Port);
5244 			if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
5245 			    sizeof(lpfcinfobuf))
5246 				goto buffer_done;
5247 		}
5248 
5249 		/* Link Speed */
5250 		link_speed = lpfc_sli_port_speed_get(phba);
5251 		if (link_speed != 0) {
5252 			scnprintf(tmp, sizeof(tmp),
5253 				  " Logical Link Speed: %d Mbps", link_speed);
5254 			if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
5255 			    sizeof(lpfcinfobuf))
5256 				goto buffer_done;
5257 		}
5258 
5259 		/* PCI resettable */
5260 		if (!lpfc_check_pci_resettable(phba)) {
5261 			scnprintf(tmp, sizeof(tmp), " PCI resettable");
5262 			strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf));
5263 		}
5264 	}
5265 
5266 buffer_done:
5267 	return lpfcinfobuf;
5268 }
5269 
5270 /**
5271  * lpfc_poll_rearm_timer - Routine to modify fcp_poll timer of hba
5272  * @phba: The Hba for which this call is being executed.
5273  *
5274  * This routine modifies fcp_poll_timer  field of @phba by cfg_poll_tmo.
5275  * The default value of cfg_poll_tmo is 10 milliseconds.
5276  **/
5277 static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
5278 {
5279 	unsigned long  poll_tmo_expires =
5280 		(jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
5281 
5282 	if (!list_empty(&phba->sli.sli3_ring[LPFC_FCP_RING].txcmplq))
5283 		mod_timer(&phba->fcp_poll_timer,
5284 			  poll_tmo_expires);
5285 }
5286 
5287 /**
5288  * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
5289  * @phba: The Hba for which this call is being executed.
5290  *
5291  * This routine starts the fcp_poll_timer of @phba.
5292  **/
5293 void lpfc_poll_start_timer(struct lpfc_hba * phba)
5294 {
5295 	lpfc_poll_rearm_timer(phba);
5296 }
5297 
5298 /**
5299  * lpfc_poll_timeout - Restart polling timer
5300  * @t: Timer construct where lpfc_hba data structure pointer is obtained.
5301  *
5302  * This routine restarts fcp_poll timer, when FCP ring  polling is enable
5303  * and FCP Ring interrupt is disable.
5304  **/
5305 void lpfc_poll_timeout(struct timer_list *t)
5306 {
5307 	struct lpfc_hba *phba = from_timer(phba, t, fcp_poll_timer);
5308 
5309 	if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
5310 		lpfc_sli_handle_fast_ring_event(phba,
5311 			&phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
5312 
5313 		if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5314 			lpfc_poll_rearm_timer(phba);
5315 	}
5316 }
5317 
5318 /*
5319  * lpfc_get_vmid_from_hashtable - search the UUID in the hash table
5320  * @vport: The virtual port for which this call is being executed.
5321  * @hash: calculated hash value
5322  * @buf: uuid associated with the VE
5323  * Return the VMID entry associated with the UUID
5324  * Make sure to acquire the appropriate lock before invoking this routine.
5325  */
5326 struct lpfc_vmid *lpfc_get_vmid_from_hashtable(struct lpfc_vport *vport,
5327 					      u32 hash, u8 *buf)
5328 {
5329 	struct lpfc_vmid *vmp;
5330 
5331 	hash_for_each_possible(vport->hash_table, vmp, hnode, hash) {
5332 		if (memcmp(&vmp->host_vmid[0], buf, 16) == 0)
5333 			return vmp;
5334 	}
5335 	return NULL;
5336 }
5337 
5338 /*
5339  * lpfc_put_vmid_in_hashtable - put the VMID in the hash table
5340  * @vport: The virtual port for which this call is being executed.
5341  * @hash - calculated hash value
5342  * @vmp: Pointer to a VMID entry representing a VM sending I/O
5343  *
5344  * This routine will insert the newly acquired VMID entity in the hash table.
5345  * Make sure to acquire the appropriate lock before invoking this routine.
5346  */
5347 static void
5348 lpfc_put_vmid_in_hashtable(struct lpfc_vport *vport, u32 hash,
5349 			   struct lpfc_vmid *vmp)
5350 {
5351 	hash_add(vport->hash_table, &vmp->hnode, hash);
5352 }
5353 
5354 /*
5355  * lpfc_vmid_hash_fn - create a hash value of the UUID
5356  * @vmid: uuid associated with the VE
5357  * @len: length of the VMID string
5358  * Returns the calculated hash value
5359  */
5360 int lpfc_vmid_hash_fn(const char *vmid, int len)
5361 {
5362 	int c;
5363 	int hash = 0;
5364 
5365 	if (len == 0)
5366 		return 0;
5367 	while (len--) {
5368 		c = *vmid++;
5369 		if (c >= 'A' && c <= 'Z')
5370 			c += 'a' - 'A';
5371 
5372 		hash = (hash + (c << LPFC_VMID_HASH_SHIFT) +
5373 			(c >> LPFC_VMID_HASH_SHIFT)) * 19;
5374 	}
5375 
5376 	return hash & LPFC_VMID_HASH_MASK;
5377 }
5378 
5379 /*
5380  * lpfc_vmid_update_entry - update the vmid entry in the hash table
5381  * @vport: The virtual port for which this call is being executed.
5382  * @cmd: address of scsi cmd descriptor
5383  * @vmp: Pointer to a VMID entry representing a VM sending I/O
5384  * @tag: VMID tag
5385  */
5386 static void lpfc_vmid_update_entry(struct lpfc_vport *vport, struct scsi_cmnd
5387 				   *cmd, struct lpfc_vmid *vmp,
5388 				   union lpfc_vmid_io_tag *tag)
5389 {
5390 	u64 *lta;
5391 
5392 	if (vport->vmid_priority_tagging)
5393 		tag->cs_ctl_vmid = vmp->un.cs_ctl_vmid;
5394 	else
5395 		tag->app_id = vmp->un.app_id;
5396 
5397 	if (cmd->sc_data_direction == DMA_TO_DEVICE)
5398 		vmp->io_wr_cnt++;
5399 	else
5400 		vmp->io_rd_cnt++;
5401 
5402 	/* update the last access timestamp in the table */
5403 	lta = per_cpu_ptr(vmp->last_io_time, raw_smp_processor_id());
5404 	*lta = jiffies;
5405 }
5406 
5407 static void lpfc_vmid_assign_cs_ctl(struct lpfc_vport *vport,
5408 				    struct lpfc_vmid *vmid)
5409 {
5410 	u32 hash;
5411 	struct lpfc_vmid *pvmid;
5412 
5413 	if (vport->port_type == LPFC_PHYSICAL_PORT) {
5414 		vmid->un.cs_ctl_vmid = lpfc_vmid_get_cs_ctl(vport);
5415 	} else {
5416 		hash = lpfc_vmid_hash_fn(vmid->host_vmid, vmid->vmid_len);
5417 		pvmid =
5418 		    lpfc_get_vmid_from_hashtable(vport->phba->pport, hash,
5419 						vmid->host_vmid);
5420 		if (pvmid)
5421 			vmid->un.cs_ctl_vmid = pvmid->un.cs_ctl_vmid;
5422 		else
5423 			vmid->un.cs_ctl_vmid = lpfc_vmid_get_cs_ctl(vport);
5424 	}
5425 }
5426 
5427 /*
5428  * lpfc_vmid_get_appid - get the VMID associated with the UUID
5429  * @vport: The virtual port for which this call is being executed.
5430  * @uuid: UUID associated with the VE
5431  * @cmd: address of scsi_cmd descriptor
5432  * @tag: VMID tag
5433  * Returns status of the function
5434  */
5435 static int lpfc_vmid_get_appid(struct lpfc_vport *vport, char *uuid, struct
5436 			       scsi_cmnd * cmd, union lpfc_vmid_io_tag *tag)
5437 {
5438 	struct lpfc_vmid *vmp = NULL;
5439 	int hash, len, rc, i;
5440 
5441 	/* check if QFPA is complete */
5442 	if (lpfc_vmid_is_type_priority_tag(vport) && !(vport->vmid_flag &
5443 	      LPFC_VMID_QFPA_CMPL)) {
5444 		vport->work_port_events |= WORKER_CHECK_VMID_ISSUE_QFPA;
5445 		return -EAGAIN;
5446 	}
5447 
5448 	/* search if the UUID has already been mapped to the VMID */
5449 	len = strlen(uuid);
5450 	hash = lpfc_vmid_hash_fn(uuid, len);
5451 
5452 	/* search for the VMID in the table */
5453 	read_lock(&vport->vmid_lock);
5454 	vmp = lpfc_get_vmid_from_hashtable(vport, hash, uuid);
5455 
5456 	/* if found, check if its already registered  */
5457 	if (vmp  && vmp->flag & LPFC_VMID_REGISTERED) {
5458 		read_unlock(&vport->vmid_lock);
5459 		lpfc_vmid_update_entry(vport, cmd, vmp, tag);
5460 		rc = 0;
5461 	} else if (vmp && (vmp->flag & LPFC_VMID_REQ_REGISTER ||
5462 			   vmp->flag & LPFC_VMID_DE_REGISTER)) {
5463 		/* else if register or dereg request has already been sent */
5464 		/* Hence VMID tag will not be added for this I/O */
5465 		read_unlock(&vport->vmid_lock);
5466 		rc = -EBUSY;
5467 	} else {
5468 		/* The VMID was not found in the hashtable. At this point, */
5469 		/* drop the read lock first before proceeding further */
5470 		read_unlock(&vport->vmid_lock);
5471 		/* start the process to obtain one as per the */
5472 		/* type of the VMID indicated */
5473 		write_lock(&vport->vmid_lock);
5474 		vmp = lpfc_get_vmid_from_hashtable(vport, hash, uuid);
5475 
5476 		/* while the read lock was released, in case the entry was */
5477 		/* added by other context or is in process of being added */
5478 		if (vmp && vmp->flag & LPFC_VMID_REGISTERED) {
5479 			lpfc_vmid_update_entry(vport, cmd, vmp, tag);
5480 			write_unlock(&vport->vmid_lock);
5481 			return 0;
5482 		} else if (vmp && vmp->flag & LPFC_VMID_REQ_REGISTER) {
5483 			write_unlock(&vport->vmid_lock);
5484 			return -EBUSY;
5485 		}
5486 
5487 		/* else search and allocate a free slot in the hash table */
5488 		if (vport->cur_vmid_cnt < vport->max_vmid) {
5489 			for (i = 0; i < vport->max_vmid; i++) {
5490 				vmp = vport->vmid + i;
5491 				if (vmp->flag == LPFC_VMID_SLOT_FREE)
5492 					break;
5493 			}
5494 			if (i == vport->max_vmid)
5495 				vmp = NULL;
5496 		} else {
5497 			vmp = NULL;
5498 		}
5499 
5500 		if (!vmp) {
5501 			write_unlock(&vport->vmid_lock);
5502 			return -ENOMEM;
5503 		}
5504 
5505 		/* Add the vmid and register */
5506 		lpfc_put_vmid_in_hashtable(vport, hash, vmp);
5507 		vmp->vmid_len = len;
5508 		memcpy(vmp->host_vmid, uuid, vmp->vmid_len);
5509 		vmp->io_rd_cnt = 0;
5510 		vmp->io_wr_cnt = 0;
5511 		vmp->flag = LPFC_VMID_SLOT_USED;
5512 
5513 		vmp->delete_inactive =
5514 			vport->vmid_inactivity_timeout ? 1 : 0;
5515 
5516 		/* if type priority tag, get next available VMID */
5517 		if (lpfc_vmid_is_type_priority_tag(vport))
5518 			lpfc_vmid_assign_cs_ctl(vport, vmp);
5519 
5520 		/* allocate the per cpu variable for holding */
5521 		/* the last access time stamp only if VMID is enabled */
5522 		if (!vmp->last_io_time)
5523 			vmp->last_io_time = __alloc_percpu(sizeof(u64),
5524 							   __alignof__(struct
5525 							   lpfc_vmid));
5526 		if (!vmp->last_io_time) {
5527 			hash_del(&vmp->hnode);
5528 			vmp->flag = LPFC_VMID_SLOT_FREE;
5529 			write_unlock(&vport->vmid_lock);
5530 			return -EIO;
5531 		}
5532 
5533 		write_unlock(&vport->vmid_lock);
5534 
5535 		/* complete transaction with switch */
5536 		if (lpfc_vmid_is_type_priority_tag(vport))
5537 			rc = lpfc_vmid_uvem(vport, vmp, true);
5538 		else
5539 			rc = lpfc_vmid_cmd(vport, SLI_CTAS_RAPP_IDENT, vmp);
5540 		if (!rc) {
5541 			write_lock(&vport->vmid_lock);
5542 			vport->cur_vmid_cnt++;
5543 			vmp->flag |= LPFC_VMID_REQ_REGISTER;
5544 			write_unlock(&vport->vmid_lock);
5545 		} else {
5546 			write_lock(&vport->vmid_lock);
5547 			hash_del(&vmp->hnode);
5548 			vmp->flag = LPFC_VMID_SLOT_FREE;
5549 			free_percpu(vmp->last_io_time);
5550 			write_unlock(&vport->vmid_lock);
5551 			return -EIO;
5552 		}
5553 
5554 		/* finally, enable the idle timer once */
5555 		if (!(vport->phba->pport->vmid_flag & LPFC_VMID_TIMER_ENBLD)) {
5556 			mod_timer(&vport->phba->inactive_vmid_poll,
5557 				  jiffies +
5558 				  msecs_to_jiffies(1000 * LPFC_VMID_TIMER));
5559 			vport->phba->pport->vmid_flag |= LPFC_VMID_TIMER_ENBLD;
5560 		}
5561 	}
5562 	return rc;
5563 }
5564 
5565 /*
5566  * lpfc_is_command_vm_io - get the UUID from blk cgroup
5567  * @cmd: Pointer to scsi_cmnd data structure
5568  * Returns UUID if present, otherwise NULL
5569  */
5570 static char *lpfc_is_command_vm_io(struct scsi_cmnd *cmd)
5571 {
5572 	struct bio *bio = scsi_cmd_to_rq(cmd)->bio;
5573 
5574 	return bio ? blkcg_get_fc_appid(bio) : NULL;
5575 }
5576 
5577 /**
5578  * lpfc_queuecommand - scsi_host_template queuecommand entry point
5579  * @shost: kernel scsi host pointer.
5580  * @cmnd: Pointer to scsi_cmnd data structure.
5581  *
5582  * Driver registers this routine to scsi midlayer to submit a @cmd to process.
5583  * This routine prepares an IOCB from scsi command and provides to firmware.
5584  * The @done callback is invoked after driver finished processing the command.
5585  *
5586  * Return value :
5587  *   0 - Success
5588  *   SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
5589  **/
5590 static int
5591 lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
5592 {
5593 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5594 	struct lpfc_hba   *phba = vport->phba;
5595 	struct lpfc_rport_data *rdata;
5596 	struct lpfc_nodelist *ndlp;
5597 	struct lpfc_io_buf *lpfc_cmd;
5598 	struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
5599 	int err, idx;
5600 	u8 *uuid = NULL;
5601 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5602 	uint64_t start = 0L;
5603 
5604 	if (phba->ktime_on)
5605 		start = ktime_get_ns();
5606 #endif
5607 	start = ktime_get_ns();
5608 	rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
5609 
5610 	/* sanity check on references */
5611 	if (unlikely(!rdata) || unlikely(!rport))
5612 		goto out_fail_command;
5613 
5614 	err = fc_remote_port_chkready(rport);
5615 	if (err) {
5616 		cmnd->result = err;
5617 		goto out_fail_command;
5618 	}
5619 	ndlp = rdata->pnode;
5620 
5621 	if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
5622 		(!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
5623 
5624 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5625 				"9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
5626 				" op:%02x str=%s without registering for"
5627 				" BlockGuard - Rejecting command\n",
5628 				cmnd->cmnd[0], scsi_get_prot_op(cmnd),
5629 				dif_op_str[scsi_get_prot_op(cmnd)]);
5630 		goto out_fail_command;
5631 	}
5632 
5633 	/*
5634 	 * Catch race where our node has transitioned, but the
5635 	 * transport is still transitioning.
5636 	 */
5637 	if (!ndlp)
5638 		goto out_tgt_busy1;
5639 
5640 	/* Check if IO qualifies for CMF */
5641 	if (phba->cmf_active_mode != LPFC_CFG_OFF &&
5642 	    cmnd->sc_data_direction == DMA_FROM_DEVICE &&
5643 	    (scsi_sg_count(cmnd))) {
5644 		/* Latency start time saved in rx_cmd_start later in routine */
5645 		err = lpfc_update_cmf_cmd(phba, scsi_bufflen(cmnd));
5646 		if (err)
5647 			goto out_tgt_busy1;
5648 	}
5649 
5650 	if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
5651 		if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) {
5652 			lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
5653 					 "3377 Target Queue Full, scsi Id:%d "
5654 					 "Qdepth:%d Pending command:%d"
5655 					 " WWNN:%02x:%02x:%02x:%02x:"
5656 					 "%02x:%02x:%02x:%02x, "
5657 					 " WWPN:%02x:%02x:%02x:%02x:"
5658 					 "%02x:%02x:%02x:%02x",
5659 					 ndlp->nlp_sid, ndlp->cmd_qdepth,
5660 					 atomic_read(&ndlp->cmd_pending),
5661 					 ndlp->nlp_nodename.u.wwn[0],
5662 					 ndlp->nlp_nodename.u.wwn[1],
5663 					 ndlp->nlp_nodename.u.wwn[2],
5664 					 ndlp->nlp_nodename.u.wwn[3],
5665 					 ndlp->nlp_nodename.u.wwn[4],
5666 					 ndlp->nlp_nodename.u.wwn[5],
5667 					 ndlp->nlp_nodename.u.wwn[6],
5668 					 ndlp->nlp_nodename.u.wwn[7],
5669 					 ndlp->nlp_portname.u.wwn[0],
5670 					 ndlp->nlp_portname.u.wwn[1],
5671 					 ndlp->nlp_portname.u.wwn[2],
5672 					 ndlp->nlp_portname.u.wwn[3],
5673 					 ndlp->nlp_portname.u.wwn[4],
5674 					 ndlp->nlp_portname.u.wwn[5],
5675 					 ndlp->nlp_portname.u.wwn[6],
5676 					 ndlp->nlp_portname.u.wwn[7]);
5677 			goto out_tgt_busy2;
5678 		}
5679 	}
5680 
5681 	lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp, cmnd);
5682 	if (lpfc_cmd == NULL) {
5683 		lpfc_rampdown_queue_depth(phba);
5684 
5685 		lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
5686 				 "0707 driver's buffer pool is empty, "
5687 				 "IO busied\n");
5688 		goto out_host_busy;
5689 	}
5690 	lpfc_cmd->rx_cmd_start = start;
5691 
5692 	/*
5693 	 * Store the midlayer's command structure for the completion phase
5694 	 * and complete the command initialization.
5695 	 */
5696 	lpfc_cmd->pCmd  = cmnd;
5697 	lpfc_cmd->rdata = rdata;
5698 	lpfc_cmd->ndlp = ndlp;
5699 	lpfc_cmd->cur_iocbq.iocb_cmpl = NULL;
5700 	cmnd->host_scribble = (unsigned char *)lpfc_cmd;
5701 
5702 	err = lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
5703 	if (err)
5704 		goto out_host_busy_release_buf;
5705 
5706 	if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
5707 		if (vport->phba->cfg_enable_bg) {
5708 			lpfc_printf_vlog(vport,
5709 					 KERN_INFO, LOG_SCSI_CMD,
5710 					 "9033 BLKGRD: rcvd %s cmd:x%x "
5711 					 "reftag x%x cnt %u pt %x\n",
5712 					 dif_op_str[scsi_get_prot_op(cmnd)],
5713 					 cmnd->cmnd[0],
5714 					 scsi_prot_ref_tag(cmnd),
5715 					 scsi_logical_block_count(cmnd),
5716 					 (cmnd->cmnd[1]>>5));
5717 		}
5718 		err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
5719 	} else {
5720 		if (vport->phba->cfg_enable_bg) {
5721 			lpfc_printf_vlog(vport,
5722 					 KERN_INFO, LOG_SCSI_CMD,
5723 					 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
5724 					 "x%x reftag x%x cnt %u pt %x\n",
5725 					 cmnd->cmnd[0],
5726 					 scsi_prot_ref_tag(cmnd),
5727 					 scsi_logical_block_count(cmnd),
5728 					 (cmnd->cmnd[1]>>5));
5729 		}
5730 		err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
5731 	}
5732 
5733 	if (unlikely(err)) {
5734 		if (err == 2) {
5735 			cmnd->result = DID_ERROR << 16;
5736 			goto out_fail_command_release_buf;
5737 		}
5738 		goto out_host_busy_free_buf;
5739 	}
5740 
5741 
5742 	/* check the necessary and sufficient condition to support VMID */
5743 	if (lpfc_is_vmid_enabled(phba) &&
5744 	    (ndlp->vmid_support ||
5745 	     phba->pport->vmid_priority_tagging ==
5746 	     LPFC_VMID_PRIO_TAG_ALL_TARGETS)) {
5747 		/* is the I/O generated by a VM, get the associated virtual */
5748 		/* entity id */
5749 		uuid = lpfc_is_command_vm_io(cmnd);
5750 
5751 		if (uuid) {
5752 			err = lpfc_vmid_get_appid(vport, uuid, cmnd,
5753 				(union lpfc_vmid_io_tag *)
5754 					&lpfc_cmd->cur_iocbq.vmid_tag);
5755 			if (!err)
5756 				lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_VMID;
5757 		}
5758 	}
5759 
5760 	atomic_inc(&ndlp->cmd_pending);
5761 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5762 	if (unlikely(phba->hdwqstat_on & LPFC_CHECK_SCSI_IO))
5763 		this_cpu_inc(phba->sli4_hba.c_stat->xmt_io);
5764 #endif
5765 	/* Issue I/O to adapter */
5766 	err = lpfc_sli_issue_fcp_io(phba, LPFC_FCP_RING,
5767 				    &lpfc_cmd->cur_iocbq,
5768 				    SLI_IOCB_RET_IOCB);
5769 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5770 	if (start) {
5771 		lpfc_cmd->ts_cmd_start = start;
5772 		lpfc_cmd->ts_last_cmd = phba->ktime_last_cmd;
5773 		lpfc_cmd->ts_cmd_wqput = ktime_get_ns();
5774 	} else {
5775 		lpfc_cmd->ts_cmd_start = 0;
5776 	}
5777 #endif
5778 	if (err) {
5779 		lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5780 				   "3376 FCP could not issue IOCB err %x "
5781 				   "FCP cmd x%x <%d/%llu> "
5782 				   "sid: x%x did: x%x oxid: x%x "
5783 				   "Data: x%x x%x x%x x%x\n",
5784 				   err, cmnd->cmnd[0],
5785 				   cmnd->device ? cmnd->device->id : 0xffff,
5786 				   cmnd->device ? cmnd->device->lun : (u64)-1,
5787 				   vport->fc_myDID, ndlp->nlp_DID,
5788 				   phba->sli_rev == LPFC_SLI_REV4 ?
5789 				   lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
5790 				   phba->sli_rev == LPFC_SLI_REV4 ?
5791 				   phba->sli4_hba.rpi_ids[ndlp->nlp_rpi] :
5792 				   lpfc_cmd->cur_iocbq.iocb.ulpContext,
5793 				   lpfc_cmd->cur_iocbq.iotag,
5794 				   phba->sli_rev == LPFC_SLI_REV4 ?
5795 				   bf_get(wqe_tmo,
5796 				   &lpfc_cmd->cur_iocbq.wqe.generic.wqe_com) :
5797 				   lpfc_cmd->cur_iocbq.iocb.ulpTimeout,
5798 				   (uint32_t)(scsi_cmd_to_rq(cmnd)->timeout / 1000));
5799 
5800 		goto out_host_busy_free_buf;
5801 	}
5802 
5803 	if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
5804 		lpfc_sli_handle_fast_ring_event(phba,
5805 			&phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
5806 
5807 		if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5808 			lpfc_poll_rearm_timer(phba);
5809 	}
5810 
5811 	if (phba->cfg_xri_rebalancing)
5812 		lpfc_keep_pvt_pool_above_lowwm(phba, lpfc_cmd->hdwq_no);
5813 
5814 	return 0;
5815 
5816  out_host_busy_free_buf:
5817 	idx = lpfc_cmd->hdwq_no;
5818 	lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
5819 	if (phba->sli4_hba.hdwq) {
5820 		switch (lpfc_cmd->fcp_cmnd->fcpCntl3) {
5821 		case WRITE_DATA:
5822 			phba->sli4_hba.hdwq[idx].scsi_cstat.output_requests--;
5823 			break;
5824 		case READ_DATA:
5825 			phba->sli4_hba.hdwq[idx].scsi_cstat.input_requests--;
5826 			break;
5827 		default:
5828 			phba->sli4_hba.hdwq[idx].scsi_cstat.control_requests--;
5829 		}
5830 	}
5831  out_host_busy_release_buf:
5832 	lpfc_release_scsi_buf(phba, lpfc_cmd);
5833  out_host_busy:
5834 	lpfc_update_cmf_cmpl(phba, LPFC_CGN_NOT_SENT, scsi_bufflen(cmnd),
5835 			     shost);
5836 	return SCSI_MLQUEUE_HOST_BUSY;
5837 
5838  out_tgt_busy2:
5839 	lpfc_update_cmf_cmpl(phba, LPFC_CGN_NOT_SENT, scsi_bufflen(cmnd),
5840 			     shost);
5841  out_tgt_busy1:
5842 	return SCSI_MLQUEUE_TARGET_BUSY;
5843 
5844  out_fail_command_release_buf:
5845 	lpfc_release_scsi_buf(phba, lpfc_cmd);
5846 	lpfc_update_cmf_cmpl(phba, LPFC_CGN_NOT_SENT, scsi_bufflen(cmnd),
5847 			     shost);
5848 
5849  out_fail_command:
5850 	cmnd->scsi_done(cmnd);
5851 	return 0;
5852 }
5853 
5854 /*
5855  * lpfc_vmid_vport_cleanup - cleans up the resources associated with a vport
5856  * @vport: The virtual port for which this call is being executed.
5857  */
5858 void lpfc_vmid_vport_cleanup(struct lpfc_vport *vport)
5859 {
5860 	u32 bucket;
5861 	struct lpfc_vmid *cur;
5862 
5863 	if (vport->port_type == LPFC_PHYSICAL_PORT)
5864 		del_timer_sync(&vport->phba->inactive_vmid_poll);
5865 
5866 	kfree(vport->qfpa_res);
5867 	kfree(vport->vmid_priority.vmid_range);
5868 	kfree(vport->vmid);
5869 
5870 	if (!hash_empty(vport->hash_table))
5871 		hash_for_each(vport->hash_table, bucket, cur, hnode)
5872 			hash_del(&cur->hnode);
5873 
5874 	vport->qfpa_res = NULL;
5875 	vport->vmid_priority.vmid_range = NULL;
5876 	vport->vmid = NULL;
5877 	vport->cur_vmid_cnt = 0;
5878 }
5879 
5880 /**
5881  * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
5882  * @cmnd: Pointer to scsi_cmnd data structure.
5883  *
5884  * This routine aborts @cmnd pending in base driver.
5885  *
5886  * Return code :
5887  *   0x2003 - Error
5888  *   0x2002 - Success
5889  **/
5890 static int
5891 lpfc_abort_handler(struct scsi_cmnd *cmnd)
5892 {
5893 	struct Scsi_Host  *shost = cmnd->device->host;
5894 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5895 	struct lpfc_hba   *phba = vport->phba;
5896 	struct lpfc_iocbq *iocb;
5897 	struct lpfc_io_buf *lpfc_cmd;
5898 	int ret = SUCCESS, status = 0;
5899 	struct lpfc_sli_ring *pring_s4 = NULL;
5900 	struct lpfc_sli_ring *pring = NULL;
5901 	int ret_val;
5902 	unsigned long flags;
5903 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
5904 
5905 	status = fc_block_scsi_eh(cmnd);
5906 	if (status != 0 && status != SUCCESS)
5907 		return status;
5908 
5909 	lpfc_cmd = (struct lpfc_io_buf *)cmnd->host_scribble;
5910 	if (!lpfc_cmd)
5911 		return ret;
5912 
5913 	spin_lock_irqsave(&phba->hbalock, flags);
5914 	/* driver queued commands are in process of being flushed */
5915 	if (phba->hba_flag & HBA_IOQ_FLUSH) {
5916 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5917 			"3168 SCSI Layer abort requested I/O has been "
5918 			"flushed by LLD.\n");
5919 		ret = FAILED;
5920 		goto out_unlock;
5921 	}
5922 
5923 	/* Guard against IO completion being called at same time */
5924 	spin_lock(&lpfc_cmd->buf_lock);
5925 
5926 	if (!lpfc_cmd->pCmd) {
5927 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5928 			 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
5929 			 "x%x ID %d LUN %llu\n",
5930 			 SUCCESS, cmnd->device->id, cmnd->device->lun);
5931 		goto out_unlock_buf;
5932 	}
5933 
5934 	iocb = &lpfc_cmd->cur_iocbq;
5935 	if (phba->sli_rev == LPFC_SLI_REV4) {
5936 		pring_s4 = phba->sli4_hba.hdwq[iocb->hba_wqidx].io_wq->pring;
5937 		if (!pring_s4) {
5938 			ret = FAILED;
5939 			goto out_unlock_buf;
5940 		}
5941 		spin_lock(&pring_s4->ring_lock);
5942 	}
5943 	/* the command is in process of being cancelled */
5944 	if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
5945 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5946 			"3169 SCSI Layer abort requested I/O has been "
5947 			"cancelled by LLD.\n");
5948 		ret = FAILED;
5949 		goto out_unlock_ring;
5950 	}
5951 	/*
5952 	 * If pCmd field of the corresponding lpfc_io_buf structure
5953 	 * points to a different SCSI command, then the driver has
5954 	 * already completed this command, but the midlayer did not
5955 	 * see the completion before the eh fired. Just return SUCCESS.
5956 	 */
5957 	if (lpfc_cmd->pCmd != cmnd) {
5958 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5959 			"3170 SCSI Layer abort requested I/O has been "
5960 			"completed by LLD.\n");
5961 		goto out_unlock_ring;
5962 	}
5963 
5964 	BUG_ON(iocb->context1 != lpfc_cmd);
5965 
5966 	/* abort issued in recovery is still in progress */
5967 	if (iocb->iocb_flag & LPFC_DRIVER_ABORTED) {
5968 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5969 			 "3389 SCSI Layer I/O Abort Request is pending\n");
5970 		if (phba->sli_rev == LPFC_SLI_REV4)
5971 			spin_unlock(&pring_s4->ring_lock);
5972 		spin_unlock(&lpfc_cmd->buf_lock);
5973 		spin_unlock_irqrestore(&phba->hbalock, flags);
5974 		goto wait_for_cmpl;
5975 	}
5976 
5977 	lpfc_cmd->waitq = &waitq;
5978 	if (phba->sli_rev == LPFC_SLI_REV4) {
5979 		spin_unlock(&pring_s4->ring_lock);
5980 		ret_val = lpfc_sli4_issue_abort_iotag(phba, iocb,
5981 						      lpfc_sli4_abort_fcp_cmpl);
5982 	} else {
5983 		pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
5984 		ret_val = lpfc_sli_issue_abort_iotag(phba, pring, iocb,
5985 						     lpfc_sli_abort_fcp_cmpl);
5986 	}
5987 
5988 	/* Make sure HBA is alive */
5989 	lpfc_issue_hb_tmo(phba);
5990 
5991 	if (ret_val != IOCB_SUCCESS) {
5992 		/* Indicate the IO is not being aborted by the driver. */
5993 		lpfc_cmd->waitq = NULL;
5994 		spin_unlock(&lpfc_cmd->buf_lock);
5995 		spin_unlock_irqrestore(&phba->hbalock, flags);
5996 		ret = FAILED;
5997 		goto out;
5998 	}
5999 
6000 	/* no longer need the lock after this point */
6001 	spin_unlock(&lpfc_cmd->buf_lock);
6002 	spin_unlock_irqrestore(&phba->hbalock, flags);
6003 
6004 	if (phba->cfg_poll & DISABLE_FCP_RING_INT)
6005 		lpfc_sli_handle_fast_ring_event(phba,
6006 			&phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
6007 
6008 wait_for_cmpl:
6009 	/*
6010 	 * iocb_flag is set to LPFC_DRIVER_ABORTED before we wait
6011 	 * for abort to complete.
6012 	 */
6013 	wait_event_timeout(waitq,
6014 			  (lpfc_cmd->pCmd != cmnd),
6015 			   msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
6016 
6017 	spin_lock(&lpfc_cmd->buf_lock);
6018 
6019 	if (lpfc_cmd->pCmd == cmnd) {
6020 		ret = FAILED;
6021 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
6022 				 "0748 abort handler timed out waiting "
6023 				 "for aborting I/O (xri:x%x) to complete: "
6024 				 "ret %#x, ID %d, LUN %llu\n",
6025 				 iocb->sli4_xritag, ret,
6026 				 cmnd->device->id, cmnd->device->lun);
6027 	}
6028 
6029 	lpfc_cmd->waitq = NULL;
6030 
6031 	spin_unlock(&lpfc_cmd->buf_lock);
6032 	goto out;
6033 
6034 out_unlock_ring:
6035 	if (phba->sli_rev == LPFC_SLI_REV4)
6036 		spin_unlock(&pring_s4->ring_lock);
6037 out_unlock_buf:
6038 	spin_unlock(&lpfc_cmd->buf_lock);
6039 out_unlock:
6040 	spin_unlock_irqrestore(&phba->hbalock, flags);
6041 out:
6042 	lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
6043 			 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
6044 			 "LUN %llu\n", ret, cmnd->device->id,
6045 			 cmnd->device->lun);
6046 	return ret;
6047 }
6048 
6049 static char *
6050 lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
6051 {
6052 	switch (task_mgmt_cmd) {
6053 	case FCP_ABORT_TASK_SET:
6054 		return "ABORT_TASK_SET";
6055 	case FCP_CLEAR_TASK_SET:
6056 		return "FCP_CLEAR_TASK_SET";
6057 	case FCP_BUS_RESET:
6058 		return "FCP_BUS_RESET";
6059 	case FCP_LUN_RESET:
6060 		return "FCP_LUN_RESET";
6061 	case FCP_TARGET_RESET:
6062 		return "FCP_TARGET_RESET";
6063 	case FCP_CLEAR_ACA:
6064 		return "FCP_CLEAR_ACA";
6065 	case FCP_TERMINATE_TASK:
6066 		return "FCP_TERMINATE_TASK";
6067 	default:
6068 		return "unknown";
6069 	}
6070 }
6071 
6072 
6073 /**
6074  * lpfc_check_fcp_rsp - check the returned fcp_rsp to see if task failed
6075  * @vport: The virtual port for which this call is being executed.
6076  * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
6077  *
6078  * This routine checks the FCP RSP INFO to see if the tsk mgmt command succeded
6079  *
6080  * Return code :
6081  *   0x2003 - Error
6082  *   0x2002 - Success
6083  **/
6084 static int
6085 lpfc_check_fcp_rsp(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd)
6086 {
6087 	struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
6088 	uint32_t rsp_info;
6089 	uint32_t rsp_len;
6090 	uint8_t  rsp_info_code;
6091 	int ret = FAILED;
6092 
6093 
6094 	if (fcprsp == NULL)
6095 		lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
6096 				 "0703 fcp_rsp is missing\n");
6097 	else {
6098 		rsp_info = fcprsp->rspStatus2;
6099 		rsp_len = be32_to_cpu(fcprsp->rspRspLen);
6100 		rsp_info_code = fcprsp->rspInfo3;
6101 
6102 
6103 		lpfc_printf_vlog(vport, KERN_INFO,
6104 				 LOG_FCP,
6105 				 "0706 fcp_rsp valid 0x%x,"
6106 				 " rsp len=%d code 0x%x\n",
6107 				 rsp_info,
6108 				 rsp_len, rsp_info_code);
6109 
6110 		/* If FCP_RSP_LEN_VALID bit is one, then the FCP_RSP_LEN
6111 		 * field specifies the number of valid bytes of FCP_RSP_INFO.
6112 		 * The FCP_RSP_LEN field shall be set to 0x04 or 0x08
6113 		 */
6114 		if ((fcprsp->rspStatus2 & RSP_LEN_VALID) &&
6115 		    ((rsp_len == 8) || (rsp_len == 4))) {
6116 			switch (rsp_info_code) {
6117 			case RSP_NO_FAILURE:
6118 				lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
6119 						 "0715 Task Mgmt No Failure\n");
6120 				ret = SUCCESS;
6121 				break;
6122 			case RSP_TM_NOT_SUPPORTED: /* TM rejected */
6123 				lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
6124 						 "0716 Task Mgmt Target "
6125 						"reject\n");
6126 				break;
6127 			case RSP_TM_NOT_COMPLETED: /* TM failed */
6128 				lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
6129 						 "0717 Task Mgmt Target "
6130 						"failed TM\n");
6131 				break;
6132 			case RSP_TM_INVALID_LU: /* TM to invalid LU! */
6133 				lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
6134 						 "0718 Task Mgmt to invalid "
6135 						"LUN\n");
6136 				break;
6137 			}
6138 		}
6139 	}
6140 	return ret;
6141 }
6142 
6143 
6144 /**
6145  * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
6146  * @vport: The virtual port for which this call is being executed.
6147  * @cmnd: Pointer to scsi_cmnd data structure.
6148  * @tgt_id: Target ID of remote device.
6149  * @lun_id: Lun number for the TMF
6150  * @task_mgmt_cmd: type of TMF to send
6151  *
6152  * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
6153  * a remote port.
6154  *
6155  * Return Code:
6156  *   0x2003 - Error
6157  *   0x2002 - Success.
6158  **/
6159 static int
6160 lpfc_send_taskmgmt(struct lpfc_vport *vport, struct scsi_cmnd *cmnd,
6161 		   unsigned int tgt_id, uint64_t lun_id,
6162 		   uint8_t task_mgmt_cmd)
6163 {
6164 	struct lpfc_hba   *phba = vport->phba;
6165 	struct lpfc_io_buf *lpfc_cmd;
6166 	struct lpfc_iocbq *iocbq;
6167 	struct lpfc_iocbq *iocbqrsp;
6168 	struct lpfc_rport_data *rdata;
6169 	struct lpfc_nodelist *pnode;
6170 	int ret;
6171 	int status;
6172 
6173 	rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
6174 	if (!rdata || !rdata->pnode)
6175 		return FAILED;
6176 	pnode = rdata->pnode;
6177 
6178 	lpfc_cmd = lpfc_get_scsi_buf(phba, pnode, NULL);
6179 	if (lpfc_cmd == NULL)
6180 		return FAILED;
6181 	lpfc_cmd->timeout = phba->cfg_task_mgmt_tmo;
6182 	lpfc_cmd->rdata = rdata;
6183 	lpfc_cmd->pCmd = cmnd;
6184 	lpfc_cmd->ndlp = pnode;
6185 
6186 	status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
6187 					   task_mgmt_cmd);
6188 	if (!status) {
6189 		lpfc_release_scsi_buf(phba, lpfc_cmd);
6190 		return FAILED;
6191 	}
6192 
6193 	iocbq = &lpfc_cmd->cur_iocbq;
6194 	iocbqrsp = lpfc_sli_get_iocbq(phba);
6195 	if (iocbqrsp == NULL) {
6196 		lpfc_release_scsi_buf(phba, lpfc_cmd);
6197 		return FAILED;
6198 	}
6199 	iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
6200 
6201 	lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
6202 			 "0702 Issue %s to TGT %d LUN %llu "
6203 			 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
6204 			 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
6205 			 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
6206 			 iocbq->iocb_flag);
6207 
6208 	status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
6209 					  iocbq, iocbqrsp, lpfc_cmd->timeout);
6210 	if ((status != IOCB_SUCCESS) ||
6211 	    (iocbqrsp->iocb.ulpStatus != IOSTAT_SUCCESS)) {
6212 		if (status != IOCB_SUCCESS ||
6213 		    iocbqrsp->iocb.ulpStatus != IOSTAT_FCP_RSP_ERROR)
6214 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
6215 					 "0727 TMF %s to TGT %d LUN %llu "
6216 					 "failed (%d, %d) iocb_flag x%x\n",
6217 					 lpfc_taskmgmt_name(task_mgmt_cmd),
6218 					 tgt_id, lun_id,
6219 					 iocbqrsp->iocb.ulpStatus,
6220 					 iocbqrsp->iocb.un.ulpWord[4],
6221 					 iocbq->iocb_flag);
6222 		/* if ulpStatus != IOCB_SUCCESS, then status == IOCB_SUCCESS */
6223 		if (status == IOCB_SUCCESS) {
6224 			if (iocbqrsp->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
6225 				/* Something in the FCP_RSP was invalid.
6226 				 * Check conditions */
6227 				ret = lpfc_check_fcp_rsp(vport, lpfc_cmd);
6228 			else
6229 				ret = FAILED;
6230 		} else if (status == IOCB_TIMEDOUT) {
6231 			ret = TIMEOUT_ERROR;
6232 		} else {
6233 			ret = FAILED;
6234 		}
6235 	} else
6236 		ret = SUCCESS;
6237 
6238 	lpfc_sli_release_iocbq(phba, iocbqrsp);
6239 
6240 	if (ret != TIMEOUT_ERROR)
6241 		lpfc_release_scsi_buf(phba, lpfc_cmd);
6242 
6243 	return ret;
6244 }
6245 
6246 /**
6247  * lpfc_chk_tgt_mapped -
6248  * @vport: The virtual port to check on
6249  * @cmnd: Pointer to scsi_cmnd data structure.
6250  *
6251  * This routine delays until the scsi target (aka rport) for the
6252  * command exists (is present and logged in) or we declare it non-existent.
6253  *
6254  * Return code :
6255  *  0x2003 - Error
6256  *  0x2002 - Success
6257  **/
6258 static int
6259 lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
6260 {
6261 	struct lpfc_rport_data *rdata;
6262 	struct lpfc_nodelist *pnode;
6263 	unsigned long later;
6264 
6265 	rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
6266 	if (!rdata) {
6267 		lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
6268 			"0797 Tgt Map rport failure: rdata x%px\n", rdata);
6269 		return FAILED;
6270 	}
6271 	pnode = rdata->pnode;
6272 	/*
6273 	 * If target is not in a MAPPED state, delay until
6274 	 * target is rediscovered or devloss timeout expires.
6275 	 */
6276 	later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
6277 	while (time_after(later, jiffies)) {
6278 		if (!pnode)
6279 			return FAILED;
6280 		if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
6281 			return SUCCESS;
6282 		schedule_timeout_uninterruptible(msecs_to_jiffies(500));
6283 		rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
6284 		if (!rdata)
6285 			return FAILED;
6286 		pnode = rdata->pnode;
6287 	}
6288 	if (!pnode || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
6289 		return FAILED;
6290 	return SUCCESS;
6291 }
6292 
6293 /**
6294  * lpfc_reset_flush_io_context -
6295  * @vport: The virtual port (scsi_host) for the flush context
6296  * @tgt_id: If aborting by Target contect - specifies the target id
6297  * @lun_id: If aborting by Lun context - specifies the lun id
6298  * @context: specifies the context level to flush at.
6299  *
6300  * After a reset condition via TMF, we need to flush orphaned i/o
6301  * contexts from the adapter. This routine aborts any contexts
6302  * outstanding, then waits for their completions. The wait is
6303  * bounded by devloss_tmo though.
6304  *
6305  * Return code :
6306  *  0x2003 - Error
6307  *  0x2002 - Success
6308  **/
6309 static int
6310 lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
6311 			uint64_t lun_id, lpfc_ctx_cmd context)
6312 {
6313 	struct lpfc_hba   *phba = vport->phba;
6314 	unsigned long later;
6315 	int cnt;
6316 
6317 	cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
6318 	if (cnt)
6319 		lpfc_sli_abort_taskmgmt(vport,
6320 					&phba->sli.sli3_ring[LPFC_FCP_RING],
6321 					tgt_id, lun_id, context);
6322 	later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
6323 	while (time_after(later, jiffies) && cnt) {
6324 		schedule_timeout_uninterruptible(msecs_to_jiffies(20));
6325 		cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
6326 	}
6327 	if (cnt) {
6328 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
6329 			"0724 I/O flush failure for context %s : cnt x%x\n",
6330 			((context == LPFC_CTX_LUN) ? "LUN" :
6331 			 ((context == LPFC_CTX_TGT) ? "TGT" :
6332 			  ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
6333 			cnt);
6334 		return FAILED;
6335 	}
6336 	return SUCCESS;
6337 }
6338 
6339 /**
6340  * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
6341  * @cmnd: Pointer to scsi_cmnd data structure.
6342  *
6343  * This routine does a device reset by sending a LUN_RESET task management
6344  * command.
6345  *
6346  * Return code :
6347  *  0x2003 - Error
6348  *  0x2002 - Success
6349  **/
6350 static int
6351 lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
6352 {
6353 	struct Scsi_Host  *shost = cmnd->device->host;
6354 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
6355 	struct lpfc_rport_data *rdata;
6356 	struct lpfc_nodelist *pnode;
6357 	unsigned tgt_id = cmnd->device->id;
6358 	uint64_t lun_id = cmnd->device->lun;
6359 	struct lpfc_scsi_event_header scsi_event;
6360 	int status;
6361 	u32 logit = LOG_FCP;
6362 
6363 	rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
6364 	if (!rdata || !rdata->pnode) {
6365 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
6366 				 "0798 Device Reset rdata failure: rdata x%px\n",
6367 				 rdata);
6368 		return FAILED;
6369 	}
6370 	pnode = rdata->pnode;
6371 	status = fc_block_scsi_eh(cmnd);
6372 	if (status != 0 && status != SUCCESS)
6373 		return status;
6374 
6375 	status = lpfc_chk_tgt_mapped(vport, cmnd);
6376 	if (status == FAILED) {
6377 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
6378 			"0721 Device Reset rport failure: rdata x%px\n", rdata);
6379 		return FAILED;
6380 	}
6381 
6382 	scsi_event.event_type = FC_REG_SCSI_EVENT;
6383 	scsi_event.subcategory = LPFC_EVENT_LUNRESET;
6384 	scsi_event.lun = lun_id;
6385 	memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
6386 	memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
6387 
6388 	fc_host_post_vendor_event(shost, fc_get_event_number(),
6389 		sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
6390 
6391 	status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
6392 						FCP_LUN_RESET);
6393 	if (status != SUCCESS)
6394 		logit =  LOG_TRACE_EVENT;
6395 
6396 	lpfc_printf_vlog(vport, KERN_ERR, logit,
6397 			 "0713 SCSI layer issued Device Reset (%d, %llu) "
6398 			 "return x%x\n", tgt_id, lun_id, status);
6399 
6400 	/*
6401 	 * We have to clean up i/o as : they may be orphaned by the TMF;
6402 	 * or if the TMF failed, they may be in an indeterminate state.
6403 	 * So, continue on.
6404 	 * We will report success if all the i/o aborts successfully.
6405 	 */
6406 	if (status == SUCCESS)
6407 		status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
6408 						LPFC_CTX_LUN);
6409 
6410 	return status;
6411 }
6412 
6413 /**
6414  * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
6415  * @cmnd: Pointer to scsi_cmnd data structure.
6416  *
6417  * This routine does a target reset by sending a TARGET_RESET task management
6418  * command.
6419  *
6420  * Return code :
6421  *  0x2003 - Error
6422  *  0x2002 - Success
6423  **/
6424 static int
6425 lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
6426 {
6427 	struct Scsi_Host  *shost = cmnd->device->host;
6428 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
6429 	struct lpfc_rport_data *rdata;
6430 	struct lpfc_nodelist *pnode;
6431 	unsigned tgt_id = cmnd->device->id;
6432 	uint64_t lun_id = cmnd->device->lun;
6433 	struct lpfc_scsi_event_header scsi_event;
6434 	int status;
6435 	u32 logit = LOG_FCP;
6436 	u32 dev_loss_tmo = vport->cfg_devloss_tmo;
6437 	unsigned long flags;
6438 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
6439 
6440 	rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
6441 	if (!rdata || !rdata->pnode) {
6442 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
6443 				 "0799 Target Reset rdata failure: rdata x%px\n",
6444 				 rdata);
6445 		return FAILED;
6446 	}
6447 	pnode = rdata->pnode;
6448 	status = fc_block_scsi_eh(cmnd);
6449 	if (status != 0 && status != SUCCESS)
6450 		return status;
6451 
6452 	status = lpfc_chk_tgt_mapped(vport, cmnd);
6453 	if (status == FAILED) {
6454 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
6455 			"0722 Target Reset rport failure: rdata x%px\n", rdata);
6456 		if (pnode) {
6457 			spin_lock_irqsave(&pnode->lock, flags);
6458 			pnode->nlp_flag &= ~NLP_NPR_ADISC;
6459 			pnode->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
6460 			spin_unlock_irqrestore(&pnode->lock, flags);
6461 		}
6462 		lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
6463 					  LPFC_CTX_TGT);
6464 		return FAST_IO_FAIL;
6465 	}
6466 
6467 	scsi_event.event_type = FC_REG_SCSI_EVENT;
6468 	scsi_event.subcategory = LPFC_EVENT_TGTRESET;
6469 	scsi_event.lun = 0;
6470 	memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
6471 	memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
6472 
6473 	fc_host_post_vendor_event(shost, fc_get_event_number(),
6474 		sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
6475 
6476 	status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
6477 					FCP_TARGET_RESET);
6478 	if (status != SUCCESS) {
6479 		logit = LOG_TRACE_EVENT;
6480 
6481 		/* Issue LOGO, if no LOGO is outstanding */
6482 		spin_lock_irqsave(&pnode->lock, flags);
6483 		if (!(pnode->upcall_flags & NLP_WAIT_FOR_LOGO) &&
6484 		    !pnode->logo_waitq) {
6485 			pnode->logo_waitq = &waitq;
6486 			pnode->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
6487 			pnode->nlp_flag |= NLP_ISSUE_LOGO;
6488 			pnode->upcall_flags |= NLP_WAIT_FOR_LOGO;
6489 			spin_unlock_irqrestore(&pnode->lock, flags);
6490 			lpfc_unreg_rpi(vport, pnode);
6491 			wait_event_timeout(waitq,
6492 					   (!(pnode->upcall_flags &
6493 					      NLP_WAIT_FOR_LOGO)),
6494 					   msecs_to_jiffies(dev_loss_tmo *
6495 							    1000));
6496 
6497 			if (pnode->upcall_flags & NLP_WAIT_FOR_LOGO) {
6498 				lpfc_printf_vlog(vport, KERN_ERR, logit,
6499 						 "0725 SCSI layer TGTRST "
6500 						 "failed & LOGO TMO (%d, %llu) "
6501 						 "return x%x\n",
6502 						 tgt_id, lun_id, status);
6503 				spin_lock_irqsave(&pnode->lock, flags);
6504 				pnode->upcall_flags &= ~NLP_WAIT_FOR_LOGO;
6505 			} else {
6506 				spin_lock_irqsave(&pnode->lock, flags);
6507 			}
6508 			pnode->logo_waitq = NULL;
6509 			spin_unlock_irqrestore(&pnode->lock, flags);
6510 			status = SUCCESS;
6511 
6512 		} else {
6513 			spin_unlock_irqrestore(&pnode->lock, flags);
6514 			status = FAILED;
6515 		}
6516 	}
6517 
6518 	lpfc_printf_vlog(vport, KERN_ERR, logit,
6519 			 "0723 SCSI layer issued Target Reset (%d, %llu) "
6520 			 "return x%x\n", tgt_id, lun_id, status);
6521 
6522 	/*
6523 	 * We have to clean up i/o as : they may be orphaned by the TMF;
6524 	 * or if the TMF failed, they may be in an indeterminate state.
6525 	 * So, continue on.
6526 	 * We will report success if all the i/o aborts successfully.
6527 	 */
6528 	if (status == SUCCESS)
6529 		status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
6530 					  LPFC_CTX_TGT);
6531 	return status;
6532 }
6533 
6534 /**
6535  * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
6536  * @cmnd: Pointer to scsi_cmnd data structure.
6537  *
6538  * This routine does target reset to all targets on @cmnd->device->host.
6539  * This emulates Parallel SCSI Bus Reset Semantics.
6540  *
6541  * Return code :
6542  *  0x2003 - Error
6543  *  0x2002 - Success
6544  **/
6545 static int
6546 lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
6547 {
6548 	struct Scsi_Host  *shost = cmnd->device->host;
6549 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
6550 	struct lpfc_nodelist *ndlp = NULL;
6551 	struct lpfc_scsi_event_header scsi_event;
6552 	int match;
6553 	int ret = SUCCESS, status, i;
6554 	u32 logit = LOG_FCP;
6555 
6556 	scsi_event.event_type = FC_REG_SCSI_EVENT;
6557 	scsi_event.subcategory = LPFC_EVENT_BUSRESET;
6558 	scsi_event.lun = 0;
6559 	memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
6560 	memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
6561 
6562 	fc_host_post_vendor_event(shost, fc_get_event_number(),
6563 		sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
6564 
6565 	status = fc_block_scsi_eh(cmnd);
6566 	if (status != 0 && status != SUCCESS)
6567 		return status;
6568 
6569 	/*
6570 	 * Since the driver manages a single bus device, reset all
6571 	 * targets known to the driver.  Should any target reset
6572 	 * fail, this routine returns failure to the midlayer.
6573 	 */
6574 	for (i = 0; i < LPFC_MAX_TARGET; i++) {
6575 		/* Search for mapped node by target ID */
6576 		match = 0;
6577 		spin_lock_irq(shost->host_lock);
6578 		list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
6579 
6580 			if (vport->phba->cfg_fcp2_no_tgt_reset &&
6581 			    (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
6582 				continue;
6583 			if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
6584 			    ndlp->nlp_sid == i &&
6585 			    ndlp->rport &&
6586 			    ndlp->nlp_type & NLP_FCP_TARGET) {
6587 				match = 1;
6588 				break;
6589 			}
6590 		}
6591 		spin_unlock_irq(shost->host_lock);
6592 		if (!match)
6593 			continue;
6594 
6595 		status = lpfc_send_taskmgmt(vport, cmnd,
6596 					i, 0, FCP_TARGET_RESET);
6597 
6598 		if (status != SUCCESS) {
6599 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
6600 					 "0700 Bus Reset on target %d failed\n",
6601 					 i);
6602 			ret = FAILED;
6603 		}
6604 	}
6605 	/*
6606 	 * We have to clean up i/o as : they may be orphaned by the TMFs
6607 	 * above; or if any of the TMFs failed, they may be in an
6608 	 * indeterminate state.
6609 	 * We will report success if all the i/o aborts successfully.
6610 	 */
6611 
6612 	status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
6613 	if (status != SUCCESS)
6614 		ret = FAILED;
6615 	if (ret == FAILED)
6616 		logit =  LOG_TRACE_EVENT;
6617 
6618 	lpfc_printf_vlog(vport, KERN_ERR, logit,
6619 			 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
6620 	return ret;
6621 }
6622 
6623 /**
6624  * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
6625  * @cmnd: Pointer to scsi_cmnd data structure.
6626  *
6627  * This routine does host reset to the adaptor port. It brings the HBA
6628  * offline, performs a board restart, and then brings the board back online.
6629  * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
6630  * reject all outstanding SCSI commands to the host and error returned
6631  * back to SCSI mid-level. As this will be SCSI mid-level's last resort
6632  * of error handling, it will only return error if resetting of the adapter
6633  * is not successful; in all other cases, will return success.
6634  *
6635  * Return code :
6636  *  0x2003 - Error
6637  *  0x2002 - Success
6638  **/
6639 static int
6640 lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
6641 {
6642 	struct Scsi_Host *shost = cmnd->device->host;
6643 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
6644 	struct lpfc_hba *phba = vport->phba;
6645 	int rc, ret = SUCCESS;
6646 
6647 	lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
6648 			 "3172 SCSI layer issued Host Reset Data:\n");
6649 
6650 	lpfc_offline_prep(phba, LPFC_MBX_WAIT);
6651 	lpfc_offline(phba);
6652 	rc = lpfc_sli_brdrestart(phba);
6653 	if (rc)
6654 		goto error;
6655 
6656 	rc = lpfc_online(phba);
6657 	if (rc)
6658 		goto error;
6659 
6660 	lpfc_unblock_mgmt_io(phba);
6661 
6662 	return ret;
6663 error:
6664 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
6665 			 "3323 Failed host reset\n");
6666 	lpfc_unblock_mgmt_io(phba);
6667 	return FAILED;
6668 }
6669 
6670 /**
6671  * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
6672  * @sdev: Pointer to scsi_device.
6673  *
6674  * This routine populates the cmds_per_lun count + 2 scsi_bufs into  this host's
6675  * globally available list of scsi buffers. This routine also makes sure scsi
6676  * buffer is not allocated more than HBA limit conveyed to midlayer. This list
6677  * of scsi buffer exists for the lifetime of the driver.
6678  *
6679  * Return codes:
6680  *   non-0 - Error
6681  *   0 - Success
6682  **/
6683 static int
6684 lpfc_slave_alloc(struct scsi_device *sdev)
6685 {
6686 	struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
6687 	struct lpfc_hba   *phba = vport->phba;
6688 	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
6689 	uint32_t total = 0;
6690 	uint32_t num_to_alloc = 0;
6691 	int num_allocated = 0;
6692 	uint32_t sdev_cnt;
6693 	struct lpfc_device_data *device_data;
6694 	unsigned long flags;
6695 	struct lpfc_name target_wwpn;
6696 
6697 	if (!rport || fc_remote_port_chkready(rport))
6698 		return -ENXIO;
6699 
6700 	if (phba->cfg_fof) {
6701 
6702 		/*
6703 		 * Check to see if the device data structure for the lun
6704 		 * exists.  If not, create one.
6705 		 */
6706 
6707 		u64_to_wwn(rport->port_name, target_wwpn.u.wwn);
6708 		spin_lock_irqsave(&phba->devicelock, flags);
6709 		device_data = __lpfc_get_device_data(phba,
6710 						     &phba->luns,
6711 						     &vport->fc_portname,
6712 						     &target_wwpn,
6713 						     sdev->lun);
6714 		if (!device_data) {
6715 			spin_unlock_irqrestore(&phba->devicelock, flags);
6716 			device_data = lpfc_create_device_data(phba,
6717 							&vport->fc_portname,
6718 							&target_wwpn,
6719 							sdev->lun,
6720 							phba->cfg_XLanePriority,
6721 							true);
6722 			if (!device_data)
6723 				return -ENOMEM;
6724 			spin_lock_irqsave(&phba->devicelock, flags);
6725 			list_add_tail(&device_data->listentry, &phba->luns);
6726 		}
6727 		device_data->rport_data = rport->dd_data;
6728 		device_data->available = true;
6729 		spin_unlock_irqrestore(&phba->devicelock, flags);
6730 		sdev->hostdata = device_data;
6731 	} else {
6732 		sdev->hostdata = rport->dd_data;
6733 	}
6734 	sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
6735 
6736 	/* For SLI4, all IO buffers are pre-allocated */
6737 	if (phba->sli_rev == LPFC_SLI_REV4)
6738 		return 0;
6739 
6740 	/* This code path is now ONLY for SLI3 adapters */
6741 
6742 	/*
6743 	 * Populate the cmds_per_lun count scsi_bufs into this host's globally
6744 	 * available list of scsi buffers.  Don't allocate more than the
6745 	 * HBA limit conveyed to the midlayer via the host structure.  The
6746 	 * formula accounts for the lun_queue_depth + error handlers + 1
6747 	 * extra.  This list of scsi bufs exists for the lifetime of the driver.
6748 	 */
6749 	total = phba->total_scsi_bufs;
6750 	num_to_alloc = vport->cfg_lun_queue_depth + 2;
6751 
6752 	/* If allocated buffers are enough do nothing */
6753 	if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
6754 		return 0;
6755 
6756 	/* Allow some exchanges to be available always to complete discovery */
6757 	if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
6758 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
6759 				 "0704 At limitation of %d preallocated "
6760 				 "command buffers\n", total);
6761 		return 0;
6762 	/* Allow some exchanges to be available always to complete discovery */
6763 	} else if (total + num_to_alloc >
6764 		phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
6765 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
6766 				 "0705 Allocation request of %d "
6767 				 "command buffers will exceed max of %d.  "
6768 				 "Reducing allocation request to %d.\n",
6769 				 num_to_alloc, phba->cfg_hba_queue_depth,
6770 				 (phba->cfg_hba_queue_depth - total));
6771 		num_to_alloc = phba->cfg_hba_queue_depth - total;
6772 	}
6773 	num_allocated = lpfc_new_scsi_buf_s3(vport, num_to_alloc);
6774 	if (num_to_alloc != num_allocated) {
6775 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
6776 					 "0708 Allocation request of %d "
6777 					 "command buffers did not succeed.  "
6778 					 "Allocated %d buffers.\n",
6779 					 num_to_alloc, num_allocated);
6780 	}
6781 	if (num_allocated > 0)
6782 		phba->total_scsi_bufs += num_allocated;
6783 	return 0;
6784 }
6785 
6786 /**
6787  * lpfc_slave_configure - scsi_host_template slave_configure entry point
6788  * @sdev: Pointer to scsi_device.
6789  *
6790  * This routine configures following items
6791  *   - Tag command queuing support for @sdev if supported.
6792  *   - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
6793  *
6794  * Return codes:
6795  *   0 - Success
6796  **/
6797 static int
6798 lpfc_slave_configure(struct scsi_device *sdev)
6799 {
6800 	struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
6801 	struct lpfc_hba   *phba = vport->phba;
6802 
6803 	scsi_change_queue_depth(sdev, vport->cfg_lun_queue_depth);
6804 
6805 	if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
6806 		lpfc_sli_handle_fast_ring_event(phba,
6807 			&phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
6808 		if (phba->cfg_poll & DISABLE_FCP_RING_INT)
6809 			lpfc_poll_rearm_timer(phba);
6810 	}
6811 
6812 	return 0;
6813 }
6814 
6815 /**
6816  * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
6817  * @sdev: Pointer to scsi_device.
6818  *
6819  * This routine sets @sdev hostatdata filed to null.
6820  **/
6821 static void
6822 lpfc_slave_destroy(struct scsi_device *sdev)
6823 {
6824 	struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
6825 	struct lpfc_hba   *phba = vport->phba;
6826 	unsigned long flags;
6827 	struct lpfc_device_data *device_data = sdev->hostdata;
6828 
6829 	atomic_dec(&phba->sdev_cnt);
6830 	if ((phba->cfg_fof) && (device_data)) {
6831 		spin_lock_irqsave(&phba->devicelock, flags);
6832 		device_data->available = false;
6833 		if (!device_data->oas_enabled)
6834 			lpfc_delete_device_data(phba, device_data);
6835 		spin_unlock_irqrestore(&phba->devicelock, flags);
6836 	}
6837 	sdev->hostdata = NULL;
6838 	return;
6839 }
6840 
6841 /**
6842  * lpfc_create_device_data - creates and initializes device data structure for OAS
6843  * @phba: Pointer to host bus adapter structure.
6844  * @vport_wwpn: Pointer to vport's wwpn information
6845  * @target_wwpn: Pointer to target's wwpn information
6846  * @lun: Lun on target
6847  * @pri: Priority
6848  * @atomic_create: Flag to indicate if memory should be allocated using the
6849  *		  GFP_ATOMIC flag or not.
6850  *
6851  * This routine creates a device data structure which will contain identifying
6852  * information for the device (host wwpn, target wwpn, lun), state of OAS,
6853  * whether or not the corresponding lun is available by the system,
6854  * and pointer to the rport data.
6855  *
6856  * Return codes:
6857  *   NULL - Error
6858  *   Pointer to lpfc_device_data - Success
6859  **/
6860 struct lpfc_device_data*
6861 lpfc_create_device_data(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
6862 			struct lpfc_name *target_wwpn, uint64_t lun,
6863 			uint32_t pri, bool atomic_create)
6864 {
6865 
6866 	struct lpfc_device_data *lun_info;
6867 	int memory_flags;
6868 
6869 	if (unlikely(!phba) || !vport_wwpn || !target_wwpn  ||
6870 	    !(phba->cfg_fof))
6871 		return NULL;
6872 
6873 	/* Attempt to create the device data to contain lun info */
6874 
6875 	if (atomic_create)
6876 		memory_flags = GFP_ATOMIC;
6877 	else
6878 		memory_flags = GFP_KERNEL;
6879 	lun_info = mempool_alloc(phba->device_data_mem_pool, memory_flags);
6880 	if (!lun_info)
6881 		return NULL;
6882 	INIT_LIST_HEAD(&lun_info->listentry);
6883 	lun_info->rport_data  = NULL;
6884 	memcpy(&lun_info->device_id.vport_wwpn, vport_wwpn,
6885 	       sizeof(struct lpfc_name));
6886 	memcpy(&lun_info->device_id.target_wwpn, target_wwpn,
6887 	       sizeof(struct lpfc_name));
6888 	lun_info->device_id.lun = lun;
6889 	lun_info->oas_enabled = false;
6890 	lun_info->priority = pri;
6891 	lun_info->available = false;
6892 	return lun_info;
6893 }
6894 
6895 /**
6896  * lpfc_delete_device_data - frees a device data structure for OAS
6897  * @phba: Pointer to host bus adapter structure.
6898  * @lun_info: Pointer to device data structure to free.
6899  *
6900  * This routine frees the previously allocated device data structure passed.
6901  *
6902  **/
6903 void
6904 lpfc_delete_device_data(struct lpfc_hba *phba,
6905 			struct lpfc_device_data *lun_info)
6906 {
6907 
6908 	if (unlikely(!phba) || !lun_info  ||
6909 	    !(phba->cfg_fof))
6910 		return;
6911 
6912 	if (!list_empty(&lun_info->listentry))
6913 		list_del(&lun_info->listentry);
6914 	mempool_free(lun_info, phba->device_data_mem_pool);
6915 	return;
6916 }
6917 
6918 /**
6919  * __lpfc_get_device_data - returns the device data for the specified lun
6920  * @phba: Pointer to host bus adapter structure.
6921  * @list: Point to list to search.
6922  * @vport_wwpn: Pointer to vport's wwpn information
6923  * @target_wwpn: Pointer to target's wwpn information
6924  * @lun: Lun on target
6925  *
6926  * This routine searches the list passed for the specified lun's device data.
6927  * This function does not hold locks, it is the responsibility of the caller
6928  * to ensure the proper lock is held before calling the function.
6929  *
6930  * Return codes:
6931  *   NULL - Error
6932  *   Pointer to lpfc_device_data - Success
6933  **/
6934 struct lpfc_device_data*
6935 __lpfc_get_device_data(struct lpfc_hba *phba, struct list_head *list,
6936 		       struct lpfc_name *vport_wwpn,
6937 		       struct lpfc_name *target_wwpn, uint64_t lun)
6938 {
6939 
6940 	struct lpfc_device_data *lun_info;
6941 
6942 	if (unlikely(!phba) || !list || !vport_wwpn || !target_wwpn ||
6943 	    !phba->cfg_fof)
6944 		return NULL;
6945 
6946 	/* Check to see if the lun is already enabled for OAS. */
6947 
6948 	list_for_each_entry(lun_info, list, listentry) {
6949 		if ((memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
6950 			    sizeof(struct lpfc_name)) == 0) &&
6951 		    (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
6952 			    sizeof(struct lpfc_name)) == 0) &&
6953 		    (lun_info->device_id.lun == lun))
6954 			return lun_info;
6955 	}
6956 
6957 	return NULL;
6958 }
6959 
6960 /**
6961  * lpfc_find_next_oas_lun - searches for the next oas lun
6962  * @phba: Pointer to host bus adapter structure.
6963  * @vport_wwpn: Pointer to vport's wwpn information
6964  * @target_wwpn: Pointer to target's wwpn information
6965  * @starting_lun: Pointer to the lun to start searching for
6966  * @found_vport_wwpn: Pointer to the found lun's vport wwpn information
6967  * @found_target_wwpn: Pointer to the found lun's target wwpn information
6968  * @found_lun: Pointer to the found lun.
6969  * @found_lun_status: Pointer to status of the found lun.
6970  * @found_lun_pri: Pointer to priority of the found lun.
6971  *
6972  * This routine searches the luns list for the specified lun
6973  * or the first lun for the vport/target.  If the vport wwpn contains
6974  * a zero value then a specific vport is not specified. In this case
6975  * any vport which contains the lun will be considered a match.  If the
6976  * target wwpn contains a zero value then a specific target is not specified.
6977  * In this case any target which contains the lun will be considered a
6978  * match.  If the lun is found, the lun, vport wwpn, target wwpn and lun status
6979  * are returned.  The function will also return the next lun if available.
6980  * If the next lun is not found, starting_lun parameter will be set to
6981  * NO_MORE_OAS_LUN.
6982  *
6983  * Return codes:
6984  *   non-0 - Error
6985  *   0 - Success
6986  **/
6987 bool
6988 lpfc_find_next_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
6989 		       struct lpfc_name *target_wwpn, uint64_t *starting_lun,
6990 		       struct lpfc_name *found_vport_wwpn,
6991 		       struct lpfc_name *found_target_wwpn,
6992 		       uint64_t *found_lun,
6993 		       uint32_t *found_lun_status,
6994 		       uint32_t *found_lun_pri)
6995 {
6996 
6997 	unsigned long flags;
6998 	struct lpfc_device_data *lun_info;
6999 	struct lpfc_device_id *device_id;
7000 	uint64_t lun;
7001 	bool found = false;
7002 
7003 	if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
7004 	    !starting_lun || !found_vport_wwpn ||
7005 	    !found_target_wwpn || !found_lun || !found_lun_status ||
7006 	    (*starting_lun == NO_MORE_OAS_LUN) ||
7007 	    !phba->cfg_fof)
7008 		return false;
7009 
7010 	lun = *starting_lun;
7011 	*found_lun = NO_MORE_OAS_LUN;
7012 	*starting_lun = NO_MORE_OAS_LUN;
7013 
7014 	/* Search for lun or the lun closet in value */
7015 
7016 	spin_lock_irqsave(&phba->devicelock, flags);
7017 	list_for_each_entry(lun_info, &phba->luns, listentry) {
7018 		if (((wwn_to_u64(vport_wwpn->u.wwn) == 0) ||
7019 		     (memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
7020 			    sizeof(struct lpfc_name)) == 0)) &&
7021 		    ((wwn_to_u64(target_wwpn->u.wwn) == 0) ||
7022 		     (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
7023 			    sizeof(struct lpfc_name)) == 0)) &&
7024 		    (lun_info->oas_enabled)) {
7025 			device_id = &lun_info->device_id;
7026 			if ((!found) &&
7027 			    ((lun == FIND_FIRST_OAS_LUN) ||
7028 			     (device_id->lun == lun))) {
7029 				*found_lun = device_id->lun;
7030 				memcpy(found_vport_wwpn,
7031 				       &device_id->vport_wwpn,
7032 				       sizeof(struct lpfc_name));
7033 				memcpy(found_target_wwpn,
7034 				       &device_id->target_wwpn,
7035 				       sizeof(struct lpfc_name));
7036 				if (lun_info->available)
7037 					*found_lun_status =
7038 						OAS_LUN_STATUS_EXISTS;
7039 				else
7040 					*found_lun_status = 0;
7041 				*found_lun_pri = lun_info->priority;
7042 				if (phba->cfg_oas_flags & OAS_FIND_ANY_VPORT)
7043 					memset(vport_wwpn, 0x0,
7044 					       sizeof(struct lpfc_name));
7045 				if (phba->cfg_oas_flags & OAS_FIND_ANY_TARGET)
7046 					memset(target_wwpn, 0x0,
7047 					       sizeof(struct lpfc_name));
7048 				found = true;
7049 			} else if (found) {
7050 				*starting_lun = device_id->lun;
7051 				memcpy(vport_wwpn, &device_id->vport_wwpn,
7052 				       sizeof(struct lpfc_name));
7053 				memcpy(target_wwpn, &device_id->target_wwpn,
7054 				       sizeof(struct lpfc_name));
7055 				break;
7056 			}
7057 		}
7058 	}
7059 	spin_unlock_irqrestore(&phba->devicelock, flags);
7060 	return found;
7061 }
7062 
7063 /**
7064  * lpfc_enable_oas_lun - enables a lun for OAS operations
7065  * @phba: Pointer to host bus adapter structure.
7066  * @vport_wwpn: Pointer to vport's wwpn information
7067  * @target_wwpn: Pointer to target's wwpn information
7068  * @lun: Lun
7069  * @pri: Priority
7070  *
7071  * This routine enables a lun for oas operations.  The routines does so by
7072  * doing the following :
7073  *
7074  *   1) Checks to see if the device data for the lun has been created.
7075  *   2) If found, sets the OAS enabled flag if not set and returns.
7076  *   3) Otherwise, creates a device data structure.
7077  *   4) If successfully created, indicates the device data is for an OAS lun,
7078  *   indicates the lun is not available and add to the list of luns.
7079  *
7080  * Return codes:
7081  *   false - Error
7082  *   true - Success
7083  **/
7084 bool
7085 lpfc_enable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
7086 		    struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
7087 {
7088 
7089 	struct lpfc_device_data *lun_info;
7090 	unsigned long flags;
7091 
7092 	if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
7093 	    !phba->cfg_fof)
7094 		return false;
7095 
7096 	spin_lock_irqsave(&phba->devicelock, flags);
7097 
7098 	/* Check to see if the device data for the lun has been created */
7099 	lun_info = __lpfc_get_device_data(phba, &phba->luns, vport_wwpn,
7100 					  target_wwpn, lun);
7101 	if (lun_info) {
7102 		if (!lun_info->oas_enabled)
7103 			lun_info->oas_enabled = true;
7104 		lun_info->priority = pri;
7105 		spin_unlock_irqrestore(&phba->devicelock, flags);
7106 		return true;
7107 	}
7108 
7109 	/* Create an lun info structure and add to list of luns */
7110 	lun_info = lpfc_create_device_data(phba, vport_wwpn, target_wwpn, lun,
7111 					   pri, true);
7112 	if (lun_info) {
7113 		lun_info->oas_enabled = true;
7114 		lun_info->priority = pri;
7115 		lun_info->available = false;
7116 		list_add_tail(&lun_info->listentry, &phba->luns);
7117 		spin_unlock_irqrestore(&phba->devicelock, flags);
7118 		return true;
7119 	}
7120 	spin_unlock_irqrestore(&phba->devicelock, flags);
7121 	return false;
7122 }
7123 
7124 /**
7125  * lpfc_disable_oas_lun - disables a lun for OAS operations
7126  * @phba: Pointer to host bus adapter structure.
7127  * @vport_wwpn: Pointer to vport's wwpn information
7128  * @target_wwpn: Pointer to target's wwpn information
7129  * @lun: Lun
7130  * @pri: Priority
7131  *
7132  * This routine disables a lun for oas operations.  The routines does so by
7133  * doing the following :
7134  *
7135  *   1) Checks to see if the device data for the lun is created.
7136  *   2) If present, clears the flag indicating this lun is for OAS.
7137  *   3) If the lun is not available by the system, the device data is
7138  *   freed.
7139  *
7140  * Return codes:
7141  *   false - Error
7142  *   true - Success
7143  **/
7144 bool
7145 lpfc_disable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
7146 		     struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
7147 {
7148 
7149 	struct lpfc_device_data *lun_info;
7150 	unsigned long flags;
7151 
7152 	if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
7153 	    !phba->cfg_fof)
7154 		return false;
7155 
7156 	spin_lock_irqsave(&phba->devicelock, flags);
7157 
7158 	/* Check to see if the lun is available. */
7159 	lun_info = __lpfc_get_device_data(phba,
7160 					  &phba->luns, vport_wwpn,
7161 					  target_wwpn, lun);
7162 	if (lun_info) {
7163 		lun_info->oas_enabled = false;
7164 		lun_info->priority = pri;
7165 		if (!lun_info->available)
7166 			lpfc_delete_device_data(phba, lun_info);
7167 		spin_unlock_irqrestore(&phba->devicelock, flags);
7168 		return true;
7169 	}
7170 
7171 	spin_unlock_irqrestore(&phba->devicelock, flags);
7172 	return false;
7173 }
7174 
7175 static int
7176 lpfc_no_command(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
7177 {
7178 	return SCSI_MLQUEUE_HOST_BUSY;
7179 }
7180 
7181 static int
7182 lpfc_no_handler(struct scsi_cmnd *cmnd)
7183 {
7184 	return FAILED;
7185 }
7186 
7187 static int
7188 lpfc_no_slave(struct scsi_device *sdev)
7189 {
7190 	return -ENODEV;
7191 }
7192 
7193 struct scsi_host_template lpfc_template_nvme = {
7194 	.module			= THIS_MODULE,
7195 	.name			= LPFC_DRIVER_NAME,
7196 	.proc_name		= LPFC_DRIVER_NAME,
7197 	.info			= lpfc_info,
7198 	.queuecommand		= lpfc_no_command,
7199 	.eh_abort_handler	= lpfc_no_handler,
7200 	.eh_device_reset_handler = lpfc_no_handler,
7201 	.eh_target_reset_handler = lpfc_no_handler,
7202 	.eh_bus_reset_handler	= lpfc_no_handler,
7203 	.eh_host_reset_handler  = lpfc_no_handler,
7204 	.slave_alloc		= lpfc_no_slave,
7205 	.slave_configure	= lpfc_no_slave,
7206 	.scan_finished		= lpfc_scan_finished,
7207 	.this_id		= -1,
7208 	.sg_tablesize		= 1,
7209 	.cmd_per_lun		= 1,
7210 	.shost_attrs		= lpfc_hba_attrs,
7211 	.max_sectors		= 0xFFFFFFFF,
7212 	.vendor_id		= LPFC_NL_VENDOR_ID,
7213 	.track_queue_depth	= 0,
7214 };
7215 
7216 struct scsi_host_template lpfc_template = {
7217 	.module			= THIS_MODULE,
7218 	.name			= LPFC_DRIVER_NAME,
7219 	.proc_name		= LPFC_DRIVER_NAME,
7220 	.info			= lpfc_info,
7221 	.queuecommand		= lpfc_queuecommand,
7222 	.eh_timed_out		= fc_eh_timed_out,
7223 	.eh_should_retry_cmd    = fc_eh_should_retry_cmd,
7224 	.eh_abort_handler	= lpfc_abort_handler,
7225 	.eh_device_reset_handler = lpfc_device_reset_handler,
7226 	.eh_target_reset_handler = lpfc_target_reset_handler,
7227 	.eh_bus_reset_handler	= lpfc_bus_reset_handler,
7228 	.eh_host_reset_handler  = lpfc_host_reset_handler,
7229 	.slave_alloc		= lpfc_slave_alloc,
7230 	.slave_configure	= lpfc_slave_configure,
7231 	.slave_destroy		= lpfc_slave_destroy,
7232 	.scan_finished		= lpfc_scan_finished,
7233 	.this_id		= -1,
7234 	.sg_tablesize		= LPFC_DEFAULT_SG_SEG_CNT,
7235 	.cmd_per_lun		= LPFC_CMD_PER_LUN,
7236 	.shost_attrs		= lpfc_hba_attrs,
7237 	.max_sectors		= 0xFFFFFFFF,
7238 	.vendor_id		= LPFC_NL_VENDOR_ID,
7239 	.change_queue_depth	= scsi_change_queue_depth,
7240 	.track_queue_depth	= 1,
7241 };
7242