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