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