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