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