xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_scsi.c (revision 0c874100)
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 
2738 	/*
2739 	 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2740 	 *  fcp_rsp regions to the first data bde entry
2741 	 */
2742 	bpl += 2;
2743 	if (scsi_sg_count(scsi_cmnd)) {
2744 		/*
2745 		 * The driver stores the segment count returned from pci_map_sg
2746 		 * because this a count of dma-mappings used to map the use_sg
2747 		 * pages.  They are not guaranteed to be the same for those
2748 		 * architectures that implement an IOMMU.
2749 		 */
2750 		datasegcnt = dma_map_sg(&phba->pcidev->dev,
2751 					scsi_sglist(scsi_cmnd),
2752 					scsi_sg_count(scsi_cmnd), datadir);
2753 		if (unlikely(!datasegcnt))
2754 			return 1;
2755 
2756 		lpfc_cmd->seg_cnt = datasegcnt;
2757 
2758 		/* First check if data segment count from SCSI Layer is good */
2759 		if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
2760 			goto err;
2761 
2762 		prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2763 
2764 		switch (prot_group_type) {
2765 		case LPFC_PG_TYPE_NO_DIF:
2766 
2767 			/* Here we need to add a PDE5 and PDE6 to the count */
2768 			if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt)
2769 				goto err;
2770 
2771 			num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2772 					datasegcnt);
2773 			/* we should have 2 or more entries in buffer list */
2774 			if (num_bde < 2)
2775 				goto err;
2776 			break;
2777 
2778 		case LPFC_PG_TYPE_DIF_BUF:
2779 			/*
2780 			 * This type indicates that protection buffers are
2781 			 * passed to the driver, so that needs to be prepared
2782 			 * for DMA
2783 			 */
2784 			protsegcnt = dma_map_sg(&phba->pcidev->dev,
2785 					scsi_prot_sglist(scsi_cmnd),
2786 					scsi_prot_sg_count(scsi_cmnd), datadir);
2787 			if (unlikely(!protsegcnt)) {
2788 				scsi_dma_unmap(scsi_cmnd);
2789 				return 1;
2790 			}
2791 
2792 			lpfc_cmd->prot_seg_cnt = protsegcnt;
2793 
2794 			/*
2795 			 * There is a minimun of 4 BPLs used for every
2796 			 * protection data segment.
2797 			 */
2798 			if ((lpfc_cmd->prot_seg_cnt * 4) >
2799 			    (phba->cfg_total_seg_cnt - 2))
2800 				goto err;
2801 
2802 			num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2803 					datasegcnt, protsegcnt);
2804 			/* we should have 3 or more entries in buffer list */
2805 			if ((num_bde < 3) ||
2806 			    (num_bde > phba->cfg_total_seg_cnt))
2807 				goto err;
2808 			break;
2809 
2810 		case LPFC_PG_TYPE_INVALID:
2811 		default:
2812 			scsi_dma_unmap(scsi_cmnd);
2813 			lpfc_cmd->seg_cnt = 0;
2814 
2815 			lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2816 					"9022 Unexpected protection group %i\n",
2817 					prot_group_type);
2818 			return 1;
2819 		}
2820 	}
2821 
2822 	/*
2823 	 * Finish initializing those IOCB fields that are dependent on the
2824 	 * scsi_cmnd request_buffer.  Note that the bdeSize is explicitly
2825 	 * reinitialized since all iocb memory resources are used many times
2826 	 * for transmit, receive, and continuation bpl's.
2827 	 */
2828 	iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2829 	iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2830 	iocb_cmd->ulpBdeCount = 1;
2831 	iocb_cmd->ulpLe = 1;
2832 
2833 	fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
2834 	fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2835 
2836 	/*
2837 	 * Due to difference in data length between DIF/non-DIF paths,
2838 	 * we need to set word 4 of IOCB here
2839 	 */
2840 	iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2841 
2842 	return 0;
2843 err:
2844 	if (lpfc_cmd->seg_cnt)
2845 		scsi_dma_unmap(scsi_cmnd);
2846 	if (lpfc_cmd->prot_seg_cnt)
2847 		dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2848 			     scsi_prot_sg_count(scsi_cmnd),
2849 			     scsi_cmnd->sc_data_direction);
2850 
2851 	lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2852 			"9023 Cannot setup S/G List for HBA"
2853 			"IO segs %d/%d BPL %d SCSI %d: %d %d\n",
2854 			lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
2855 			phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
2856 			prot_group_type, num_bde);
2857 
2858 	lpfc_cmd->seg_cnt = 0;
2859 	lpfc_cmd->prot_seg_cnt = 0;
2860 	return 1;
2861 }
2862 
2863 /*
2864  * This function calcuates the T10 DIF guard tag
2865  * on the specified data using a CRC algorithmn
2866  * using crc_t10dif.
2867  */
2868 static uint16_t
2869 lpfc_bg_crc(uint8_t *data, int count)
2870 {
2871 	uint16_t crc = 0;
2872 	uint16_t x;
2873 
2874 	crc = crc_t10dif(data, count);
2875 	x = cpu_to_be16(crc);
2876 	return x;
2877 }
2878 
2879 /*
2880  * This function calcuates the T10 DIF guard tag
2881  * on the specified data using a CSUM algorithmn
2882  * using ip_compute_csum.
2883  */
2884 static uint16_t
2885 lpfc_bg_csum(uint8_t *data, int count)
2886 {
2887 	uint16_t ret;
2888 
2889 	ret = ip_compute_csum(data, count);
2890 	return ret;
2891 }
2892 
2893 /*
2894  * This function examines the protection data to try to determine
2895  * what type of T10-DIF error occurred.
2896  */
2897 static void
2898 lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2899 {
2900 	struct scatterlist *sgpe; /* s/g prot entry */
2901 	struct scatterlist *sgde; /* s/g data entry */
2902 	struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2903 	struct scsi_dif_tuple *src = NULL;
2904 	uint8_t *data_src = NULL;
2905 	uint16_t guard_tag;
2906 	uint16_t start_app_tag, app_tag;
2907 	uint32_t start_ref_tag, ref_tag;
2908 	int prot, protsegcnt;
2909 	int err_type, len, data_len;
2910 	int chk_ref, chk_app, chk_guard;
2911 	uint16_t sum;
2912 	unsigned blksize;
2913 
2914 	err_type = BGS_GUARD_ERR_MASK;
2915 	sum = 0;
2916 	guard_tag = 0;
2917 
2918 	/* First check to see if there is protection data to examine */
2919 	prot = scsi_get_prot_op(cmd);
2920 	if ((prot == SCSI_PROT_READ_STRIP) ||
2921 	    (prot == SCSI_PROT_WRITE_INSERT) ||
2922 	    (prot == SCSI_PROT_NORMAL))
2923 		goto out;
2924 
2925 	/* Currently the driver just supports ref_tag and guard_tag checking */
2926 	chk_ref = 1;
2927 	chk_app = 0;
2928 	chk_guard = 0;
2929 
2930 	/* Setup a ptr to the protection data provided by the SCSI host */
2931 	sgpe = scsi_prot_sglist(cmd);
2932 	protsegcnt = lpfc_cmd->prot_seg_cnt;
2933 
2934 	if (sgpe && protsegcnt) {
2935 
2936 		/*
2937 		 * We will only try to verify guard tag if the segment
2938 		 * data length is a multiple of the blksize.
2939 		 */
2940 		sgde = scsi_sglist(cmd);
2941 		blksize = lpfc_cmd_blksize(cmd);
2942 		data_src = (uint8_t *)sg_virt(sgde);
2943 		data_len = sgde->length;
2944 		if ((data_len & (blksize - 1)) == 0)
2945 			chk_guard = 1;
2946 
2947 		src = (struct scsi_dif_tuple *)sg_virt(sgpe);
2948 		start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
2949 		start_app_tag = src->app_tag;
2950 		len = sgpe->length;
2951 		while (src && protsegcnt) {
2952 			while (len) {
2953 
2954 				/*
2955 				 * First check to see if a protection data
2956 				 * check is valid
2957 				 */
2958 				if ((src->ref_tag == T10_PI_REF_ESCAPE) ||
2959 				    (src->app_tag == T10_PI_APP_ESCAPE)) {
2960 					start_ref_tag++;
2961 					goto skipit;
2962 				}
2963 
2964 				/* First Guard Tag checking */
2965 				if (chk_guard) {
2966 					guard_tag = src->guard_tag;
2967 					if (lpfc_cmd_guard_csum(cmd))
2968 						sum = lpfc_bg_csum(data_src,
2969 								   blksize);
2970 					else
2971 						sum = lpfc_bg_crc(data_src,
2972 								  blksize);
2973 					if ((guard_tag != sum)) {
2974 						err_type = BGS_GUARD_ERR_MASK;
2975 						goto out;
2976 					}
2977 				}
2978 
2979 				/* Reference Tag checking */
2980 				ref_tag = be32_to_cpu(src->ref_tag);
2981 				if (chk_ref && (ref_tag != start_ref_tag)) {
2982 					err_type = BGS_REFTAG_ERR_MASK;
2983 					goto out;
2984 				}
2985 				start_ref_tag++;
2986 
2987 				/* App Tag checking */
2988 				app_tag = src->app_tag;
2989 				if (chk_app && (app_tag != start_app_tag)) {
2990 					err_type = BGS_APPTAG_ERR_MASK;
2991 					goto out;
2992 				}
2993 skipit:
2994 				len -= sizeof(struct scsi_dif_tuple);
2995 				if (len < 0)
2996 					len = 0;
2997 				src++;
2998 
2999 				data_src += blksize;
3000 				data_len -= blksize;
3001 
3002 				/*
3003 				 * Are we at the end of the Data segment?
3004 				 * The data segment is only used for Guard
3005 				 * tag checking.
3006 				 */
3007 				if (chk_guard && (data_len == 0)) {
3008 					chk_guard = 0;
3009 					sgde = sg_next(sgde);
3010 					if (!sgde)
3011 						goto out;
3012 
3013 					data_src = (uint8_t *)sg_virt(sgde);
3014 					data_len = sgde->length;
3015 					if ((data_len & (blksize - 1)) == 0)
3016 						chk_guard = 1;
3017 				}
3018 			}
3019 
3020 			/* Goto the next Protection data segment */
3021 			sgpe = sg_next(sgpe);
3022 			if (sgpe) {
3023 				src = (struct scsi_dif_tuple *)sg_virt(sgpe);
3024 				len = sgpe->length;
3025 			} else {
3026 				src = NULL;
3027 			}
3028 			protsegcnt--;
3029 		}
3030 	}
3031 out:
3032 	if (err_type == BGS_GUARD_ERR_MASK) {
3033 		scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3034 					0x10, 0x1);
3035 		cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
3036 			      SAM_STAT_CHECK_CONDITION;
3037 		phba->bg_guard_err_cnt++;
3038 		lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3039 				"9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
3040 				(unsigned long)scsi_get_lba(cmd),
3041 				sum, guard_tag);
3042 
3043 	} else if (err_type == BGS_REFTAG_ERR_MASK) {
3044 		scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3045 					0x10, 0x3);
3046 		cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
3047 			      SAM_STAT_CHECK_CONDITION;
3048 
3049 		phba->bg_reftag_err_cnt++;
3050 		lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3051 				"9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
3052 				(unsigned long)scsi_get_lba(cmd),
3053 				ref_tag, start_ref_tag);
3054 
3055 	} else if (err_type == BGS_APPTAG_ERR_MASK) {
3056 		scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3057 					0x10, 0x2);
3058 		cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
3059 			      SAM_STAT_CHECK_CONDITION;
3060 
3061 		phba->bg_apptag_err_cnt++;
3062 		lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3063 				"9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
3064 				(unsigned long)scsi_get_lba(cmd),
3065 				app_tag, start_app_tag);
3066 	}
3067 }
3068 
3069 
3070 /*
3071  * This function checks for BlockGuard errors detected by
3072  * the HBA.  In case of errors, the ASC/ASCQ fields in the
3073  * sense buffer will be set accordingly, paired with
3074  * ILLEGAL_REQUEST to signal to the kernel that the HBA
3075  * detected corruption.
3076  *
3077  * Returns:
3078  *  0 - No error found
3079  *  1 - BlockGuard error found
3080  * -1 - Internal error (bad profile, ...etc)
3081  */
3082 static int
3083 lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
3084 			struct lpfc_iocbq *pIocbOut)
3085 {
3086 	struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3087 	struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
3088 	int ret = 0;
3089 	uint32_t bghm = bgf->bghm;
3090 	uint32_t bgstat = bgf->bgstat;
3091 	uint64_t failing_sector = 0;
3092 
3093 	spin_lock(&_dump_buf_lock);
3094 	if (!_dump_buf_done) {
3095 		lpfc_printf_log(phba, KERN_ERR, LOG_BG,  "9070 BLKGRD: Saving"
3096 			" Data for %u blocks to debugfs\n",
3097 				(cmd->cmnd[7] << 8 | cmd->cmnd[8]));
3098 		lpfc_debug_save_data(phba, cmd);
3099 
3100 		/* If we have a prot sgl, save the DIF buffer */
3101 		if (lpfc_prot_group_type(phba, cmd) ==
3102 				LPFC_PG_TYPE_DIF_BUF) {
3103 			lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
3104 				"Saving DIF for %u blocks to debugfs\n",
3105 				(cmd->cmnd[7] << 8 | cmd->cmnd[8]));
3106 			lpfc_debug_save_dif(phba, cmd);
3107 		}
3108 
3109 		_dump_buf_done = 1;
3110 	}
3111 	spin_unlock(&_dump_buf_lock);
3112 
3113 	if (lpfc_bgs_get_invalid_prof(bgstat)) {
3114 		cmd->result = DID_ERROR << 16;
3115 		lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3116 				"9072 BLKGRD: Invalid BG Profile in cmd"
3117 				" 0x%x lba 0x%llx blk cnt 0x%x "
3118 				"bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3119 				(unsigned long long)scsi_get_lba(cmd),
3120 				blk_rq_sectors(cmd->request), bgstat, bghm);
3121 		ret = (-1);
3122 		goto out;
3123 	}
3124 
3125 	if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
3126 		cmd->result = DID_ERROR << 16;
3127 		lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3128 				"9073 BLKGRD: Invalid BG PDIF Block in cmd"
3129 				" 0x%x lba 0x%llx blk cnt 0x%x "
3130 				"bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3131 				(unsigned long long)scsi_get_lba(cmd),
3132 				blk_rq_sectors(cmd->request), bgstat, bghm);
3133 		ret = (-1);
3134 		goto out;
3135 	}
3136 
3137 	if (lpfc_bgs_get_guard_err(bgstat)) {
3138 		ret = 1;
3139 
3140 		scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3141 				0x10, 0x1);
3142 		cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
3143 			      SAM_STAT_CHECK_CONDITION;
3144 		phba->bg_guard_err_cnt++;
3145 		lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3146 				"9055 BLKGRD: Guard Tag error in cmd"
3147 				" 0x%x lba 0x%llx blk cnt 0x%x "
3148 				"bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3149 				(unsigned long long)scsi_get_lba(cmd),
3150 				blk_rq_sectors(cmd->request), bgstat, bghm);
3151 	}
3152 
3153 	if (lpfc_bgs_get_reftag_err(bgstat)) {
3154 		ret = 1;
3155 
3156 		scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3157 				0x10, 0x3);
3158 		cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
3159 			      SAM_STAT_CHECK_CONDITION;
3160 
3161 		phba->bg_reftag_err_cnt++;
3162 		lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3163 				"9056 BLKGRD: Ref Tag error in cmd"
3164 				" 0x%x lba 0x%llx blk cnt 0x%x "
3165 				"bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3166 				(unsigned long long)scsi_get_lba(cmd),
3167 				blk_rq_sectors(cmd->request), bgstat, bghm);
3168 	}
3169 
3170 	if (lpfc_bgs_get_apptag_err(bgstat)) {
3171 		ret = 1;
3172 
3173 		scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3174 				0x10, 0x2);
3175 		cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
3176 			      SAM_STAT_CHECK_CONDITION;
3177 
3178 		phba->bg_apptag_err_cnt++;
3179 		lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3180 				"9061 BLKGRD: App Tag error in cmd"
3181 				" 0x%x lba 0x%llx blk cnt 0x%x "
3182 				"bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3183 				(unsigned long long)scsi_get_lba(cmd),
3184 				blk_rq_sectors(cmd->request), bgstat, bghm);
3185 	}
3186 
3187 	if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
3188 		/*
3189 		 * setup sense data descriptor 0 per SPC-4 as an information
3190 		 * field, and put the failing LBA in it.
3191 		 * This code assumes there was also a guard/app/ref tag error
3192 		 * indication.
3193 		 */
3194 		cmd->sense_buffer[7] = 0xc;   /* Additional sense length */
3195 		cmd->sense_buffer[8] = 0;     /* Information descriptor type */
3196 		cmd->sense_buffer[9] = 0xa;   /* Additional descriptor length */
3197 		cmd->sense_buffer[10] = 0x80; /* Validity bit */
3198 
3199 		/* bghm is a "on the wire" FC frame based count */
3200 		switch (scsi_get_prot_op(cmd)) {
3201 		case SCSI_PROT_READ_INSERT:
3202 		case SCSI_PROT_WRITE_STRIP:
3203 			bghm /= cmd->device->sector_size;
3204 			break;
3205 		case SCSI_PROT_READ_STRIP:
3206 		case SCSI_PROT_WRITE_INSERT:
3207 		case SCSI_PROT_READ_PASS:
3208 		case SCSI_PROT_WRITE_PASS:
3209 			bghm /= (cmd->device->sector_size +
3210 				sizeof(struct scsi_dif_tuple));
3211 			break;
3212 		}
3213 
3214 		failing_sector = scsi_get_lba(cmd);
3215 		failing_sector += bghm;
3216 
3217 		/* Descriptor Information */
3218 		put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
3219 	}
3220 
3221 	if (!ret) {
3222 		/* No error was reported - problem in FW? */
3223 		lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3224 				"9057 BLKGRD: Unknown error in cmd"
3225 				" 0x%x lba 0x%llx blk cnt 0x%x "
3226 				"bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3227 				(unsigned long long)scsi_get_lba(cmd),
3228 				blk_rq_sectors(cmd->request), bgstat, bghm);
3229 
3230 		/* Calcuate what type of error it was */
3231 		lpfc_calc_bg_err(phba, lpfc_cmd);
3232 	}
3233 out:
3234 	return ret;
3235 }
3236 
3237 /**
3238  * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3239  * @phba: The Hba for which this call is being executed.
3240  * @lpfc_cmd: The scsi buffer which is going to be mapped.
3241  *
3242  * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3243  * field of @lpfc_cmd for device with SLI-4 interface spec.
3244  *
3245  * Return codes:
3246  *	1 - Error
3247  *	0 - Success
3248  **/
3249 static int
3250 lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3251 {
3252 	struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3253 	struct scatterlist *sgel = NULL;
3254 	struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3255 	struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
3256 	struct sli4_sge *first_data_sgl;
3257 	IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3258 	dma_addr_t physaddr;
3259 	uint32_t num_bde = 0;
3260 	uint32_t dma_len;
3261 	uint32_t dma_offset = 0;
3262 	int nseg;
3263 	struct ulp_bde64 *bde;
3264 
3265 	/*
3266 	 * There are three possibilities here - use scatter-gather segment, use
3267 	 * the single mapping, or neither.  Start the lpfc command prep by
3268 	 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3269 	 * data bde entry.
3270 	 */
3271 	if (scsi_sg_count(scsi_cmnd)) {
3272 		/*
3273 		 * The driver stores the segment count returned from pci_map_sg
3274 		 * because this a count of dma-mappings used to map the use_sg
3275 		 * pages.  They are not guaranteed to be the same for those
3276 		 * architectures that implement an IOMMU.
3277 		 */
3278 
3279 		nseg = scsi_dma_map(scsi_cmnd);
3280 		if (unlikely(nseg <= 0))
3281 			return 1;
3282 		sgl += 1;
3283 		/* clear the last flag in the fcp_rsp map entry */
3284 		sgl->word2 = le32_to_cpu(sgl->word2);
3285 		bf_set(lpfc_sli4_sge_last, sgl, 0);
3286 		sgl->word2 = cpu_to_le32(sgl->word2);
3287 		sgl += 1;
3288 		first_data_sgl = sgl;
3289 		lpfc_cmd->seg_cnt = nseg;
3290 		if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
3291 			lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3292 				" %s: Too many sg segments from "
3293 				"dma_map_sg.  Config %d, seg_cnt %d\n",
3294 				__func__, phba->cfg_sg_seg_cnt,
3295 			       lpfc_cmd->seg_cnt);
3296 			lpfc_cmd->seg_cnt = 0;
3297 			scsi_dma_unmap(scsi_cmnd);
3298 			return 1;
3299 		}
3300 
3301 		/*
3302 		 * The driver established a maximum scatter-gather segment count
3303 		 * during probe that limits the number of sg elements in any
3304 		 * single scsi command.  Just run through the seg_cnt and format
3305 		 * the sge's.
3306 		 * When using SLI-3 the driver will try to fit all the BDEs into
3307 		 * the IOCB. If it can't then the BDEs get added to a BPL as it
3308 		 * does for SLI-2 mode.
3309 		 */
3310 		scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3311 			physaddr = sg_dma_address(sgel);
3312 			dma_len = sg_dma_len(sgel);
3313 			sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3314 			sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
3315 			sgl->word2 = le32_to_cpu(sgl->word2);
3316 			if ((num_bde + 1) == nseg)
3317 				bf_set(lpfc_sli4_sge_last, sgl, 1);
3318 			else
3319 				bf_set(lpfc_sli4_sge_last, sgl, 0);
3320 			bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
3321 			bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
3322 			sgl->word2 = cpu_to_le32(sgl->word2);
3323 			sgl->sge_len = cpu_to_le32(dma_len);
3324 			dma_offset += dma_len;
3325 			sgl++;
3326 		}
3327 		/*
3328 		 * Setup the first Payload BDE. For FCoE we just key off
3329 		 * Performance Hints, for FC we use lpfc_enable_pbde.
3330 		 * We populate words 13-15 of IOCB/WQE.
3331 		 */
3332 		if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
3333 		    phba->cfg_enable_pbde) {
3334 			bde = (struct ulp_bde64 *)
3335 				&(iocb_cmd->unsli3.sli3Words[5]);
3336 			bde->addrLow = first_data_sgl->addr_lo;
3337 			bde->addrHigh = first_data_sgl->addr_hi;
3338 			bde->tus.f.bdeSize =
3339 					le32_to_cpu(first_data_sgl->sge_len);
3340 			bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3341 			bde->tus.w = cpu_to_le32(bde->tus.w);
3342 		}
3343 	} else {
3344 		sgl += 1;
3345 		/* clear the last flag in the fcp_rsp map entry */
3346 		sgl->word2 = le32_to_cpu(sgl->word2);
3347 		bf_set(lpfc_sli4_sge_last, sgl, 1);
3348 		sgl->word2 = cpu_to_le32(sgl->word2);
3349 
3350 		if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
3351 		    phba->cfg_enable_pbde) {
3352 			bde = (struct ulp_bde64 *)
3353 				&(iocb_cmd->unsli3.sli3Words[5]);
3354 			memset(bde, 0, (sizeof(uint32_t) * 3));
3355 		}
3356 	}
3357 
3358 	/*
3359 	 * Finish initializing those IOCB fields that are dependent on the
3360 	 * scsi_cmnd request_buffer.  Note that for SLI-2 the bdeSize is
3361 	 * explicitly reinitialized.
3362 	 * all iocb memory resources are reused.
3363 	 */
3364 	fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3365 
3366 	/*
3367 	 * Due to difference in data length between DIF/non-DIF paths,
3368 	 * we need to set word 4 of IOCB here
3369 	 */
3370 	iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
3371 
3372 	/*
3373 	 * If the OAS driver feature is enabled and the lun is enabled for
3374 	 * OAS, set the oas iocb related flags.
3375 	 */
3376 	if ((phba->cfg_fof) && ((struct lpfc_device_data *)
3377 		scsi_cmnd->device->hostdata)->oas_enabled) {
3378 		lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
3379 		lpfc_cmd->cur_iocbq.priority = ((struct lpfc_device_data *)
3380 			scsi_cmnd->device->hostdata)->priority;
3381 	}
3382 	return 0;
3383 }
3384 
3385 /**
3386  * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3387  * @phba: The Hba for which this call is being executed.
3388  * @lpfc_cmd: The scsi buffer which is going to be mapped.
3389  *
3390  * This is the protection/DIF aware version of
3391  * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3392  * two functions eventually, but for now, it's here
3393  **/
3394 static int
3395 lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3396 		struct lpfc_scsi_buf *lpfc_cmd)
3397 {
3398 	struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3399 	struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3400 	struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3401 	IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3402 	uint32_t num_sge = 0;
3403 	int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3404 	int prot_group_type = 0;
3405 	int fcpdl;
3406 
3407 	/*
3408 	 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
3409 	 *  fcp_rsp regions to the first data sge entry
3410 	 */
3411 	if (scsi_sg_count(scsi_cmnd)) {
3412 		/*
3413 		 * The driver stores the segment count returned from pci_map_sg
3414 		 * because this a count of dma-mappings used to map the use_sg
3415 		 * pages.  They are not guaranteed to be the same for those
3416 		 * architectures that implement an IOMMU.
3417 		 */
3418 		datasegcnt = dma_map_sg(&phba->pcidev->dev,
3419 					scsi_sglist(scsi_cmnd),
3420 					scsi_sg_count(scsi_cmnd), datadir);
3421 		if (unlikely(!datasegcnt))
3422 			return 1;
3423 
3424 		sgl += 1;
3425 		/* clear the last flag in the fcp_rsp map entry */
3426 		sgl->word2 = le32_to_cpu(sgl->word2);
3427 		bf_set(lpfc_sli4_sge_last, sgl, 0);
3428 		sgl->word2 = cpu_to_le32(sgl->word2);
3429 
3430 		sgl += 1;
3431 		lpfc_cmd->seg_cnt = datasegcnt;
3432 
3433 		/* First check if data segment count from SCSI Layer is good */
3434 		if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
3435 			goto err;
3436 
3437 		prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3438 
3439 		switch (prot_group_type) {
3440 		case LPFC_PG_TYPE_NO_DIF:
3441 			/* Here we need to add a DISEED to the count */
3442 			if ((lpfc_cmd->seg_cnt + 1) > phba->cfg_total_seg_cnt)
3443 				goto err;
3444 
3445 			num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
3446 					datasegcnt);
3447 
3448 			/* we should have 2 or more entries in buffer list */
3449 			if (num_sge < 2)
3450 				goto err;
3451 			break;
3452 
3453 		case LPFC_PG_TYPE_DIF_BUF:
3454 			/*
3455 			 * This type indicates that protection buffers are
3456 			 * passed to the driver, so that needs to be prepared
3457 			 * for DMA
3458 			 */
3459 			protsegcnt = dma_map_sg(&phba->pcidev->dev,
3460 					scsi_prot_sglist(scsi_cmnd),
3461 					scsi_prot_sg_count(scsi_cmnd), datadir);
3462 			if (unlikely(!protsegcnt)) {
3463 				scsi_dma_unmap(scsi_cmnd);
3464 				return 1;
3465 			}
3466 
3467 			lpfc_cmd->prot_seg_cnt = protsegcnt;
3468 			/*
3469 			 * There is a minimun of 3 SGEs used for every
3470 			 * protection data segment.
3471 			 */
3472 			if ((lpfc_cmd->prot_seg_cnt * 3) >
3473 			    (phba->cfg_total_seg_cnt - 2))
3474 				goto err;
3475 
3476 			num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
3477 					datasegcnt, protsegcnt);
3478 
3479 			/* we should have 3 or more entries in buffer list */
3480 			if ((num_sge < 3) ||
3481 			    (num_sge > phba->cfg_total_seg_cnt))
3482 				goto err;
3483 			break;
3484 
3485 		case LPFC_PG_TYPE_INVALID:
3486 		default:
3487 			scsi_dma_unmap(scsi_cmnd);
3488 			lpfc_cmd->seg_cnt = 0;
3489 
3490 			lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3491 					"9083 Unexpected protection group %i\n",
3492 					prot_group_type);
3493 			return 1;
3494 		}
3495 	}
3496 
3497 	switch (scsi_get_prot_op(scsi_cmnd)) {
3498 	case SCSI_PROT_WRITE_STRIP:
3499 	case SCSI_PROT_READ_STRIP:
3500 		lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3501 		break;
3502 	case SCSI_PROT_WRITE_INSERT:
3503 	case SCSI_PROT_READ_INSERT:
3504 		lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3505 		break;
3506 	case SCSI_PROT_WRITE_PASS:
3507 	case SCSI_PROT_READ_PASS:
3508 		lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3509 		break;
3510 	}
3511 
3512 	fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
3513 	fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3514 
3515 	/*
3516 	 * Due to difference in data length between DIF/non-DIF paths,
3517 	 * we need to set word 4 of IOCB here
3518 	 */
3519 	iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
3520 
3521 	/*
3522 	 * If the OAS driver feature is enabled and the lun is enabled for
3523 	 * OAS, set the oas iocb related flags.
3524 	 */
3525 	if ((phba->cfg_fof) && ((struct lpfc_device_data *)
3526 		scsi_cmnd->device->hostdata)->oas_enabled)
3527 		lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
3528 
3529 	return 0;
3530 err:
3531 	if (lpfc_cmd->seg_cnt)
3532 		scsi_dma_unmap(scsi_cmnd);
3533 	if (lpfc_cmd->prot_seg_cnt)
3534 		dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3535 			     scsi_prot_sg_count(scsi_cmnd),
3536 			     scsi_cmnd->sc_data_direction);
3537 
3538 	lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3539 			"9084 Cannot setup S/G List for HBA"
3540 			"IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3541 			lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3542 			phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3543 			prot_group_type, num_sge);
3544 
3545 	lpfc_cmd->seg_cnt = 0;
3546 	lpfc_cmd->prot_seg_cnt = 0;
3547 	return 1;
3548 }
3549 
3550 /**
3551  * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3552  * @phba: The Hba for which this call is being executed.
3553  * @lpfc_cmd: The scsi buffer which is going to be mapped.
3554  *
3555  * This routine wraps the actual DMA mapping function pointer from the
3556  * lpfc_hba struct.
3557  *
3558  * Return codes:
3559  *	1 - Error
3560  *	0 - Success
3561  **/
3562 static inline int
3563 lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3564 {
3565 	return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3566 }
3567 
3568 /**
3569  * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3570  * using BlockGuard.
3571  * @phba: The Hba for which this call is being executed.
3572  * @lpfc_cmd: The scsi buffer which is going to be mapped.
3573  *
3574  * This routine wraps the actual DMA mapping function pointer from the
3575  * lpfc_hba struct.
3576  *
3577  * Return codes:
3578  *	1 - Error
3579  *	0 - Success
3580  **/
3581 static inline int
3582 lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3583 {
3584 	return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3585 }
3586 
3587 /**
3588  * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
3589  * @phba: Pointer to hba context object.
3590  * @vport: Pointer to vport object.
3591  * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3592  * @rsp_iocb: Pointer to response iocb object which reported error.
3593  *
3594  * This function posts an event when there is a SCSI command reporting
3595  * error from the scsi device.
3596  **/
3597 static void
3598 lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3599 		struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3600 	struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3601 	struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3602 	uint32_t resp_info = fcprsp->rspStatus2;
3603 	uint32_t scsi_status = fcprsp->rspStatus3;
3604 	uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3605 	struct lpfc_fast_path_event *fast_path_evt = NULL;
3606 	struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3607 	unsigned long flags;
3608 
3609 	if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3610 		return;
3611 
3612 	/* If there is queuefull or busy condition send a scsi event */
3613 	if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3614 		(cmnd->result == SAM_STAT_BUSY)) {
3615 		fast_path_evt = lpfc_alloc_fast_evt(phba);
3616 		if (!fast_path_evt)
3617 			return;
3618 		fast_path_evt->un.scsi_evt.event_type =
3619 			FC_REG_SCSI_EVENT;
3620 		fast_path_evt->un.scsi_evt.subcategory =
3621 		(cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3622 		LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3623 		fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3624 		memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3625 			&pnode->nlp_portname, sizeof(struct lpfc_name));
3626 		memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3627 			&pnode->nlp_nodename, sizeof(struct lpfc_name));
3628 	} else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3629 		((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3630 		fast_path_evt = lpfc_alloc_fast_evt(phba);
3631 		if (!fast_path_evt)
3632 			return;
3633 		fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3634 			FC_REG_SCSI_EVENT;
3635 		fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3636 			LPFC_EVENT_CHECK_COND;
3637 		fast_path_evt->un.check_cond_evt.scsi_event.lun =
3638 			cmnd->device->lun;
3639 		memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3640 			&pnode->nlp_portname, sizeof(struct lpfc_name));
3641 		memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3642 			&pnode->nlp_nodename, sizeof(struct lpfc_name));
3643 		fast_path_evt->un.check_cond_evt.sense_key =
3644 			cmnd->sense_buffer[2] & 0xf;
3645 		fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3646 		fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3647 	} else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3648 		     fcpi_parm &&
3649 		     ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3650 			((scsi_status == SAM_STAT_GOOD) &&
3651 			!(resp_info & (RESID_UNDER | RESID_OVER))))) {
3652 		/*
3653 		 * If status is good or resid does not match with fcp_param and
3654 		 * there is valid fcpi_parm, then there is a read_check error
3655 		 */
3656 		fast_path_evt = lpfc_alloc_fast_evt(phba);
3657 		if (!fast_path_evt)
3658 			return;
3659 		fast_path_evt->un.read_check_error.header.event_type =
3660 			FC_REG_FABRIC_EVENT;
3661 		fast_path_evt->un.read_check_error.header.subcategory =
3662 			LPFC_EVENT_FCPRDCHKERR;
3663 		memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3664 			&pnode->nlp_portname, sizeof(struct lpfc_name));
3665 		memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3666 			&pnode->nlp_nodename, sizeof(struct lpfc_name));
3667 		fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3668 		fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3669 		fast_path_evt->un.read_check_error.fcpiparam =
3670 			fcpi_parm;
3671 	} else
3672 		return;
3673 
3674 	fast_path_evt->vport = vport;
3675 	spin_lock_irqsave(&phba->hbalock, flags);
3676 	list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3677 	spin_unlock_irqrestore(&phba->hbalock, flags);
3678 	lpfc_worker_wake_up(phba);
3679 	return;
3680 }
3681 
3682 /**
3683  * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
3684  * @phba: The HBA for which this call is being executed.
3685  * @psb: The scsi buffer which is going to be un-mapped.
3686  *
3687  * This routine does DMA un-mapping of scatter gather list of scsi command
3688  * field of @lpfc_cmd for device with SLI-3 interface spec.
3689  **/
3690 static void
3691 lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
3692 {
3693 	/*
3694 	 * There are only two special cases to consider.  (1) the scsi command
3695 	 * requested scatter-gather usage or (2) the scsi command allocated
3696 	 * a request buffer, but did not request use_sg.  There is a third
3697 	 * case, but it does not require resource deallocation.
3698 	 */
3699 	if (psb->seg_cnt > 0)
3700 		scsi_dma_unmap(psb->pCmd);
3701 	if (psb->prot_seg_cnt > 0)
3702 		dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3703 				scsi_prot_sg_count(psb->pCmd),
3704 				psb->pCmd->sc_data_direction);
3705 }
3706 
3707 /**
3708  * lpfc_handler_fcp_err - FCP response handler
3709  * @vport: The virtual port for which this call is being executed.
3710  * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3711  * @rsp_iocb: The response IOCB which contains FCP error.
3712  *
3713  * This routine is called to process response IOCB with status field
3714  * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3715  * based upon SCSI and FCP error.
3716  **/
3717 static void
3718 lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3719 		    struct lpfc_iocbq *rsp_iocb)
3720 {
3721 	struct lpfc_hba *phba = vport->phba;
3722 	struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3723 	struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3724 	struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3725 	uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3726 	uint32_t resp_info = fcprsp->rspStatus2;
3727 	uint32_t scsi_status = fcprsp->rspStatus3;
3728 	uint32_t *lp;
3729 	uint32_t host_status = DID_OK;
3730 	uint32_t rsplen = 0;
3731 	uint32_t fcpDl;
3732 	uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
3733 
3734 
3735 	/*
3736 	 *  If this is a task management command, there is no
3737 	 *  scsi packet associated with this lpfc_cmd.  The driver
3738 	 *  consumes it.
3739 	 */
3740 	if (fcpcmd->fcpCntl2) {
3741 		scsi_status = 0;
3742 		goto out;
3743 	}
3744 
3745 	if (resp_info & RSP_LEN_VALID) {
3746 		rsplen = be32_to_cpu(fcprsp->rspRspLen);
3747 		if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
3748 			lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3749 				 "2719 Invalid response length: "
3750 				 "tgt x%x lun x%llx cmnd x%x rsplen x%x\n",
3751 				 cmnd->device->id,
3752 				 cmnd->device->lun, cmnd->cmnd[0],
3753 				 rsplen);
3754 			host_status = DID_ERROR;
3755 			goto out;
3756 		}
3757 		if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3758 			lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3759 				 "2757 Protocol failure detected during "
3760 				 "processing of FCP I/O op: "
3761 				 "tgt x%x lun x%llx cmnd x%x rspInfo3 x%x\n",
3762 				 cmnd->device->id,
3763 				 cmnd->device->lun, cmnd->cmnd[0],
3764 				 fcprsp->rspInfo3);
3765 			host_status = DID_ERROR;
3766 			goto out;
3767 		}
3768 	}
3769 
3770 	if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3771 		uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3772 		if (snslen > SCSI_SENSE_BUFFERSIZE)
3773 			snslen = SCSI_SENSE_BUFFERSIZE;
3774 
3775 		if (resp_info & RSP_LEN_VALID)
3776 		  rsplen = be32_to_cpu(fcprsp->rspRspLen);
3777 		memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3778 	}
3779 	lp = (uint32_t *)cmnd->sense_buffer;
3780 
3781 	/* special handling for under run conditions */
3782 	if (!scsi_status && (resp_info & RESID_UNDER)) {
3783 		/* don't log under runs if fcp set... */
3784 		if (vport->cfg_log_verbose & LOG_FCP)
3785 			logit = LOG_FCP_ERROR;
3786 		/* unless operator says so */
3787 		if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3788 			logit = LOG_FCP_UNDER;
3789 	}
3790 
3791 	lpfc_printf_vlog(vport, KERN_WARNING, logit,
3792 			 "9024 FCP command x%x failed: x%x SNS x%x x%x "
3793 			 "Data: x%x x%x x%x x%x x%x\n",
3794 			 cmnd->cmnd[0], scsi_status,
3795 			 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3796 			 be32_to_cpu(fcprsp->rspResId),
3797 			 be32_to_cpu(fcprsp->rspSnsLen),
3798 			 be32_to_cpu(fcprsp->rspRspLen),
3799 			 fcprsp->rspInfo3);
3800 
3801 	scsi_set_resid(cmnd, 0);
3802 	fcpDl = be32_to_cpu(fcpcmd->fcpDl);
3803 	if (resp_info & RESID_UNDER) {
3804 		scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
3805 
3806 		lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
3807 				 "9025 FCP Underrun, expected %d, "
3808 				 "residual %d Data: x%x x%x x%x\n",
3809 				 fcpDl,
3810 				 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3811 				 cmnd->underflow);
3812 
3813 		/*
3814 		 * If there is an under run, check if under run reported by
3815 		 * storage array is same as the under run reported by HBA.
3816 		 * If this is not same, there is a dropped frame.
3817 		 */
3818 		if (fcpi_parm && (scsi_get_resid(cmnd) != fcpi_parm)) {
3819 			lpfc_printf_vlog(vport, KERN_WARNING,
3820 					 LOG_FCP | LOG_FCP_ERROR,
3821 					 "9026 FCP Read Check Error "
3822 					 "and Underrun Data: x%x x%x x%x x%x\n",
3823 					 fcpDl,
3824 					 scsi_get_resid(cmnd), fcpi_parm,
3825 					 cmnd->cmnd[0]);
3826 			scsi_set_resid(cmnd, scsi_bufflen(cmnd));
3827 			host_status = DID_ERROR;
3828 		}
3829 		/*
3830 		 * The cmnd->underflow is the minimum number of bytes that must
3831 		 * be transferred for this command.  Provided a sense condition
3832 		 * is not present, make sure the actual amount transferred is at
3833 		 * least the underflow value or fail.
3834 		 */
3835 		if (!(resp_info & SNS_LEN_VALID) &&
3836 		    (scsi_status == SAM_STAT_GOOD) &&
3837 		    (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3838 		     < cmnd->underflow)) {
3839 			lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3840 					 "9027 FCP command x%x residual "
3841 					 "underrun converted to error "
3842 					 "Data: x%x x%x x%x\n",
3843 					 cmnd->cmnd[0], scsi_bufflen(cmnd),
3844 					 scsi_get_resid(cmnd), cmnd->underflow);
3845 			host_status = DID_ERROR;
3846 		}
3847 	} else if (resp_info & RESID_OVER) {
3848 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3849 				 "9028 FCP command x%x residual overrun error. "
3850 				 "Data: x%x x%x\n", cmnd->cmnd[0],
3851 				 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
3852 		host_status = DID_ERROR;
3853 
3854 	/*
3855 	 * Check SLI validation that all the transfer was actually done
3856 	 * (fcpi_parm should be zero). Apply check only to reads.
3857 	 */
3858 	} else if (fcpi_parm) {
3859 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
3860 				 "9029 FCP %s Check Error xri x%x  Data: "
3861 				 "x%x x%x x%x x%x x%x\n",
3862 				 ((cmnd->sc_data_direction == DMA_FROM_DEVICE) ?
3863 				 "Read" : "Write"),
3864 				 ((phba->sli_rev == LPFC_SLI_REV4) ?
3865 				 lpfc_cmd->cur_iocbq.sli4_xritag :
3866 				 rsp_iocb->iocb.ulpContext),
3867 				 fcpDl, be32_to_cpu(fcprsp->rspResId),
3868 				 fcpi_parm, cmnd->cmnd[0], scsi_status);
3869 
3870 		/* There is some issue with the LPe12000 that causes it
3871 		 * to miscalculate the fcpi_parm and falsely trip this
3872 		 * recovery logic.  Detect this case and don't error when true.
3873 		 */
3874 		if (fcpi_parm > fcpDl)
3875 			goto out;
3876 
3877 		switch (scsi_status) {
3878 		case SAM_STAT_GOOD:
3879 		case SAM_STAT_CHECK_CONDITION:
3880 			/* Fabric dropped a data frame. Fail any successful
3881 			 * command in which we detected dropped frames.
3882 			 * A status of good or some check conditions could
3883 			 * be considered a successful command.
3884 			 */
3885 			host_status = DID_ERROR;
3886 			break;
3887 		}
3888 		scsi_set_resid(cmnd, scsi_bufflen(cmnd));
3889 	}
3890 
3891  out:
3892 	cmnd->result = host_status << 16 | scsi_status;
3893 	lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
3894 }
3895 
3896 /**
3897  * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
3898  * @phba: Pointer to HBA context object.
3899  *
3900  * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
3901  * distribution.  This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
3902  * held.
3903  * If scsi-mq is enabled, get the default block layer mapping of software queues
3904  * to hardware queues. This information is saved in request tag.
3905  *
3906  * Return: index into SLI4 fast-path FCP queue index.
3907  **/
3908 int lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba,
3909 				  struct lpfc_scsi_buf *lpfc_cmd)
3910 {
3911 	struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3912 	struct lpfc_vector_map_info *cpup;
3913 	int chann, cpu;
3914 	uint32_t tag;
3915 	uint16_t hwq;
3916 
3917 	if (cmnd && shost_use_blk_mq(cmnd->device->host)) {
3918 		tag = blk_mq_unique_tag(cmnd->request);
3919 		hwq = blk_mq_unique_tag_to_hwq(tag);
3920 
3921 		return hwq;
3922 	}
3923 
3924 	if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_CPU
3925 	    && phba->cfg_fcp_io_channel > 1) {
3926 		cpu = smp_processor_id();
3927 		if (cpu < phba->sli4_hba.num_present_cpu) {
3928 			cpup = phba->sli4_hba.cpu_map;
3929 			cpup += cpu;
3930 			return cpup->channel_id;
3931 		}
3932 	}
3933 	chann = atomic_add_return(1, &phba->fcp_qidx);
3934 	chann = chann % phba->cfg_fcp_io_channel;
3935 	return chann;
3936 }
3937 
3938 
3939 /**
3940  * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
3941  * @phba: The Hba for which this call is being executed.
3942  * @pIocbIn: The command IOCBQ for the scsi cmnd.
3943  * @pIocbOut: The response IOCBQ for the scsi cmnd.
3944  *
3945  * This routine assigns scsi command result by looking into response IOCB
3946  * status field appropriately. This routine handles QUEUE FULL condition as
3947  * well by ramping down device queue depth.
3948  **/
3949 static void
3950 lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3951 			struct lpfc_iocbq *pIocbOut)
3952 {
3953 	struct lpfc_scsi_buf *lpfc_cmd =
3954 		(struct lpfc_scsi_buf *) pIocbIn->context1;
3955 	struct lpfc_vport      *vport = pIocbIn->vport;
3956 	struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3957 	struct lpfc_nodelist *pnode = rdata->pnode;
3958 	struct scsi_cmnd *cmd;
3959 	unsigned long flags;
3960 	struct lpfc_fast_path_event *fast_path_evt;
3961 	struct Scsi_Host *shost;
3962 	uint32_t logit = LOG_FCP;
3963 
3964 	atomic_inc(&phba->fc4ScsiIoCmpls);
3965 
3966 	/* Sanity check on return of outstanding command */
3967 	cmd = lpfc_cmd->pCmd;
3968 	if (!cmd)
3969 		return;
3970 	shost = cmd->device->host;
3971 
3972 	lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
3973 	lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
3974 	/* pick up SLI4 exhange busy status from HBA */
3975 	lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
3976 
3977 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3978 	if (lpfc_cmd->prot_data_type) {
3979 		struct scsi_dif_tuple *src = NULL;
3980 
3981 		src =  (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
3982 		/*
3983 		 * Used to restore any changes to protection
3984 		 * data for error injection.
3985 		 */
3986 		switch (lpfc_cmd->prot_data_type) {
3987 		case LPFC_INJERR_REFTAG:
3988 			src->ref_tag =
3989 				lpfc_cmd->prot_data;
3990 			break;
3991 		case LPFC_INJERR_APPTAG:
3992 			src->app_tag =
3993 				(uint16_t)lpfc_cmd->prot_data;
3994 			break;
3995 		case LPFC_INJERR_GUARD:
3996 			src->guard_tag =
3997 				(uint16_t)lpfc_cmd->prot_data;
3998 			break;
3999 		default:
4000 			break;
4001 		}
4002 
4003 		lpfc_cmd->prot_data = 0;
4004 		lpfc_cmd->prot_data_type = 0;
4005 		lpfc_cmd->prot_data_segment = NULL;
4006 	}
4007 #endif
4008 
4009 	if (lpfc_cmd->status) {
4010 		if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
4011 		    (lpfc_cmd->result & IOERR_DRVR_MASK))
4012 			lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4013 		else if (lpfc_cmd->status >= IOSTAT_CNT)
4014 			lpfc_cmd->status = IOSTAT_DEFAULT;
4015 		if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
4016 		    !lpfc_cmd->fcp_rsp->rspStatus3 &&
4017 		    (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
4018 		    !(vport->cfg_log_verbose & LOG_FCP_UNDER))
4019 			logit = 0;
4020 		else
4021 			logit = LOG_FCP | LOG_FCP_UNDER;
4022 		lpfc_printf_vlog(vport, KERN_WARNING, logit,
4023 			 "9030 FCP cmd x%x failed <%d/%lld> "
4024 			 "status: x%x result: x%x "
4025 			 "sid: x%x did: x%x oxid: x%x "
4026 			 "Data: x%x x%x\n",
4027 			 cmd->cmnd[0],
4028 			 cmd->device ? cmd->device->id : 0xffff,
4029 			 cmd->device ? cmd->device->lun : 0xffff,
4030 			 lpfc_cmd->status, lpfc_cmd->result,
4031 			 vport->fc_myDID,
4032 			 (pnode) ? pnode->nlp_DID : 0,
4033 			 phba->sli_rev == LPFC_SLI_REV4 ?
4034 			     lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
4035 			 pIocbOut->iocb.ulpContext,
4036 			 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
4037 
4038 		switch (lpfc_cmd->status) {
4039 		case IOSTAT_FCP_RSP_ERROR:
4040 			/* Call FCP RSP handler to determine result */
4041 			lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
4042 			break;
4043 		case IOSTAT_NPORT_BSY:
4044 		case IOSTAT_FABRIC_BSY:
4045 			cmd->result = DID_TRANSPORT_DISRUPTED << 16;
4046 			fast_path_evt = lpfc_alloc_fast_evt(phba);
4047 			if (!fast_path_evt)
4048 				break;
4049 			fast_path_evt->un.fabric_evt.event_type =
4050 				FC_REG_FABRIC_EVENT;
4051 			fast_path_evt->un.fabric_evt.subcategory =
4052 				(lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
4053 				LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
4054 			if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4055 				memcpy(&fast_path_evt->un.fabric_evt.wwpn,
4056 					&pnode->nlp_portname,
4057 					sizeof(struct lpfc_name));
4058 				memcpy(&fast_path_evt->un.fabric_evt.wwnn,
4059 					&pnode->nlp_nodename,
4060 					sizeof(struct lpfc_name));
4061 			}
4062 			fast_path_evt->vport = vport;
4063 			fast_path_evt->work_evt.evt =
4064 				LPFC_EVT_FASTPATH_MGMT_EVT;
4065 			spin_lock_irqsave(&phba->hbalock, flags);
4066 			list_add_tail(&fast_path_evt->work_evt.evt_listp,
4067 				&phba->work_list);
4068 			spin_unlock_irqrestore(&phba->hbalock, flags);
4069 			lpfc_worker_wake_up(phba);
4070 			break;
4071 		case IOSTAT_LOCAL_REJECT:
4072 		case IOSTAT_REMOTE_STOP:
4073 			if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
4074 			    lpfc_cmd->result ==
4075 					IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
4076 			    lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
4077 			    lpfc_cmd->result ==
4078 					IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
4079 				cmd->result = DID_NO_CONNECT << 16;
4080 				break;
4081 			}
4082 			if (lpfc_cmd->result == IOERR_INVALID_RPI ||
4083 			    lpfc_cmd->result == IOERR_NO_RESOURCES ||
4084 			    lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
4085 			    lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
4086 				cmd->result = DID_REQUEUE << 16;
4087 				break;
4088 			}
4089 			if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
4090 			     lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
4091 			     pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
4092 				if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
4093 					/*
4094 					 * This is a response for a BG enabled
4095 					 * cmd. Parse BG error
4096 					 */
4097 					lpfc_parse_bg_err(phba, lpfc_cmd,
4098 							pIocbOut);
4099 					break;
4100 				} else {
4101 					lpfc_printf_vlog(vport, KERN_WARNING,
4102 							LOG_BG,
4103 							"9031 non-zero BGSTAT "
4104 							"on unprotected cmd\n");
4105 				}
4106 			}
4107 			if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
4108 				&& (phba->sli_rev == LPFC_SLI_REV4)
4109 				&& (pnode && NLP_CHK_NODE_ACT(pnode))) {
4110 				/* This IO was aborted by the target, we don't
4111 				 * know the rxid and because we did not send the
4112 				 * ABTS we cannot generate and RRQ.
4113 				 */
4114 				lpfc_set_rrq_active(phba, pnode,
4115 					lpfc_cmd->cur_iocbq.sli4_lxritag,
4116 					0, 0);
4117 			}
4118 		/* else: fall through */
4119 		default:
4120 			cmd->result = DID_ERROR << 16;
4121 			break;
4122 		}
4123 
4124 		if (!pnode || !NLP_CHK_NODE_ACT(pnode)
4125 		    || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
4126 			cmd->result = DID_TRANSPORT_DISRUPTED << 16 |
4127 				      SAM_STAT_BUSY;
4128 	} else
4129 		cmd->result = DID_OK << 16;
4130 
4131 	if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4132 		uint32_t *lp = (uint32_t *)cmd->sense_buffer;
4133 
4134 		lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4135 				 "0710 Iodone <%d/%llu> cmd %p, error "
4136 				 "x%x SNS x%x x%x Data: x%x x%x\n",
4137 				 cmd->device->id, cmd->device->lun, cmd,
4138 				 cmd->result, *lp, *(lp + 3), cmd->retries,
4139 				 scsi_get_resid(cmd));
4140 	}
4141 
4142 	lpfc_update_stats(phba, lpfc_cmd);
4143 	if (vport->cfg_max_scsicmpl_time &&
4144 	   time_after(jiffies, lpfc_cmd->start_time +
4145 		msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
4146 		spin_lock_irqsave(shost->host_lock, flags);
4147 		if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4148 			if (pnode->cmd_qdepth >
4149 				atomic_read(&pnode->cmd_pending) &&
4150 				(atomic_read(&pnode->cmd_pending) >
4151 				LPFC_MIN_TGT_QDEPTH) &&
4152 				((cmd->cmnd[0] == READ_10) ||
4153 				(cmd->cmnd[0] == WRITE_10)))
4154 				pnode->cmd_qdepth =
4155 					atomic_read(&pnode->cmd_pending);
4156 
4157 			pnode->last_change_time = jiffies;
4158 		}
4159 		spin_unlock_irqrestore(shost->host_lock, flags);
4160 	}
4161 	lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
4162 
4163 	/* If pCmd was set to NULL from abort path, do not call scsi_done */
4164 	if (xchg(&lpfc_cmd->pCmd, NULL) == NULL) {
4165 		lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4166 				 "0711 FCP cmd already NULL, sid: 0x%06x, "
4167 				 "did: 0x%06x, oxid: 0x%04x\n",
4168 				 vport->fc_myDID,
4169 				 (pnode) ? pnode->nlp_DID : 0,
4170 				 phba->sli_rev == LPFC_SLI_REV4 ?
4171 				 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff);
4172 		return;
4173 	}
4174 
4175 	/* The sdev is not guaranteed to be valid post scsi_done upcall. */
4176 	cmd->scsi_done(cmd);
4177 
4178 	/*
4179 	 * If there is a thread waiting for command completion
4180 	 * wake up the thread.
4181 	 */
4182 	spin_lock_irqsave(shost->host_lock, flags);
4183 	if (lpfc_cmd->waitq)
4184 		wake_up(lpfc_cmd->waitq);
4185 	spin_unlock_irqrestore(shost->host_lock, flags);
4186 
4187 	lpfc_release_scsi_buf(phba, lpfc_cmd);
4188 }
4189 
4190 /**
4191  * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
4192  * @data: A pointer to the immediate command data portion of the IOCB.
4193  * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
4194  *
4195  * The routine copies the entire FCP command from @fcp_cmnd to @data while
4196  * byte swapping the data to big endian format for transmission on the wire.
4197  **/
4198 static void
4199 lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
4200 {
4201 	int i, j;
4202 	for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
4203 	     i += sizeof(uint32_t), j++) {
4204 		((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
4205 	}
4206 }
4207 
4208 /**
4209  * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
4210  * @vport: The virtual port for which this call is being executed.
4211  * @lpfc_cmd: The scsi command which needs to send.
4212  * @pnode: Pointer to lpfc_nodelist.
4213  *
4214  * This routine initializes fcp_cmnd and iocb data structure from scsi command
4215  * to transfer for device with SLI3 interface spec.
4216  **/
4217 static void
4218 lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
4219 		    struct lpfc_nodelist *pnode)
4220 {
4221 	struct lpfc_hba *phba = vport->phba;
4222 	struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4223 	struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4224 	IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4225 	struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
4226 	int datadir = scsi_cmnd->sc_data_direction;
4227 	uint8_t *ptr;
4228 	bool sli4;
4229 	uint32_t fcpdl;
4230 
4231 	if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4232 		return;
4233 
4234 	lpfc_cmd->fcp_rsp->rspSnsLen = 0;
4235 	/* clear task management bits */
4236 	lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
4237 
4238 	int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4239 			&lpfc_cmd->fcp_cmnd->fcp_lun);
4240 
4241 	ptr = &fcp_cmnd->fcpCdb[0];
4242 	memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4243 	if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4244 		ptr += scsi_cmnd->cmd_len;
4245 		memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4246 	}
4247 
4248 	fcp_cmnd->fcpCntl1 = SIMPLE_Q;
4249 
4250 	sli4 = (phba->sli_rev == LPFC_SLI_REV4);
4251 	piocbq->iocb.un.fcpi.fcpi_XRdy = 0;
4252 
4253 	/*
4254 	 * There are three possibilities here - use scatter-gather segment, use
4255 	 * the single mapping, or neither.  Start the lpfc command prep by
4256 	 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4257 	 * data bde entry.
4258 	 */
4259 	if (scsi_sg_count(scsi_cmnd)) {
4260 		if (datadir == DMA_TO_DEVICE) {
4261 			iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
4262 			iocb_cmd->ulpPU = PARM_READ_CHECK;
4263 			if (vport->cfg_first_burst_size &&
4264 			    (pnode->nlp_flag & NLP_FIRSTBURST)) {
4265 				fcpdl = scsi_bufflen(scsi_cmnd);
4266 				if (fcpdl < vport->cfg_first_burst_size)
4267 					piocbq->iocb.un.fcpi.fcpi_XRdy = fcpdl;
4268 				else
4269 					piocbq->iocb.un.fcpi.fcpi_XRdy =
4270 						vport->cfg_first_burst_size;
4271 			}
4272 			fcp_cmnd->fcpCntl3 = WRITE_DATA;
4273 			atomic_inc(&phba->fc4ScsiOutputRequests);
4274 		} else {
4275 			iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4276 			iocb_cmd->ulpPU = PARM_READ_CHECK;
4277 			fcp_cmnd->fcpCntl3 = READ_DATA;
4278 			atomic_inc(&phba->fc4ScsiInputRequests);
4279 		}
4280 	} else {
4281 		iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4282 		iocb_cmd->un.fcpi.fcpi_parm = 0;
4283 		iocb_cmd->ulpPU = 0;
4284 		fcp_cmnd->fcpCntl3 = 0;
4285 		atomic_inc(&phba->fc4ScsiControlRequests);
4286 	}
4287 	if (phba->sli_rev == 3 &&
4288 	    !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
4289 		lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
4290 	/*
4291 	 * Finish initializing those IOCB fields that are independent
4292 	 * of the scsi_cmnd request_buffer
4293 	 */
4294 	piocbq->iocb.ulpContext = pnode->nlp_rpi;
4295 	if (sli4)
4296 		piocbq->iocb.ulpContext =
4297 		  phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
4298 	if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4299 		piocbq->iocb.ulpFCP2Rcvy = 1;
4300 	else
4301 		piocbq->iocb.ulpFCP2Rcvy = 0;
4302 
4303 	piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4304 	piocbq->context1  = lpfc_cmd;
4305 	piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4306 	piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
4307 	piocbq->vport = vport;
4308 }
4309 
4310 /**
4311  * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
4312  * @vport: The virtual port for which this call is being executed.
4313  * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4314  * @lun: Logical unit number.
4315  * @task_mgmt_cmd: SCSI task management command.
4316  *
4317  * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4318  * for device with SLI-3 interface spec.
4319  *
4320  * Return codes:
4321  *   0 - Error
4322  *   1 - Success
4323  **/
4324 static int
4325 lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
4326 			     struct lpfc_scsi_buf *lpfc_cmd,
4327 			     uint64_t lun,
4328 			     uint8_t task_mgmt_cmd)
4329 {
4330 	struct lpfc_iocbq *piocbq;
4331 	IOCB_t *piocb;
4332 	struct fcp_cmnd *fcp_cmnd;
4333 	struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
4334 	struct lpfc_nodelist *ndlp = rdata->pnode;
4335 
4336 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4337 	    ndlp->nlp_state != NLP_STE_MAPPED_NODE)
4338 		return 0;
4339 
4340 	piocbq = &(lpfc_cmd->cur_iocbq);
4341 	piocbq->vport = vport;
4342 
4343 	piocb = &piocbq->iocb;
4344 
4345 	fcp_cmnd = lpfc_cmd->fcp_cmnd;
4346 	/* Clear out any old data in the FCP command area */
4347 	memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4348 	int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
4349 	fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
4350 	if (vport->phba->sli_rev == 3 &&
4351 	    !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
4352 		lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
4353 	piocb->ulpCommand = CMD_FCP_ICMND64_CR;
4354 	piocb->ulpContext = ndlp->nlp_rpi;
4355 	if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4356 		piocb->ulpContext =
4357 		  vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4358 	}
4359 	piocb->ulpFCP2Rcvy = (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) ? 1 : 0;
4360 	piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
4361 	piocb->ulpPU = 0;
4362 	piocb->un.fcpi.fcpi_parm = 0;
4363 
4364 	/* ulpTimeout is only one byte */
4365 	if (lpfc_cmd->timeout > 0xff) {
4366 		/*
4367 		 * Do not timeout the command at the firmware level.
4368 		 * The driver will provide the timeout mechanism.
4369 		 */
4370 		piocb->ulpTimeout = 0;
4371 	} else
4372 		piocb->ulpTimeout = lpfc_cmd->timeout;
4373 
4374 	if (vport->phba->sli_rev == LPFC_SLI_REV4)
4375 		lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
4376 
4377 	return 1;
4378 }
4379 
4380 /**
4381  * lpfc_scsi_api_table_setup - Set up scsi api function jump table
4382  * @phba: The hba struct for which this call is being executed.
4383  * @dev_grp: The HBA PCI-Device group number.
4384  *
4385  * This routine sets up the SCSI interface API function jump table in @phba
4386  * struct.
4387  * Returns: 0 - success, -ENODEV - failure.
4388  **/
4389 int
4390 lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4391 {
4392 
4393 	phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4394 	phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
4395 
4396 	switch (dev_grp) {
4397 	case LPFC_PCI_DEV_LP:
4398 		phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4399 		phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
4400 		phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
4401 		phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
4402 		phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
4403 		break;
4404 	case LPFC_PCI_DEV_OC:
4405 		phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4406 		phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
4407 		phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
4408 		phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
4409 		phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
4410 		break;
4411 	default:
4412 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4413 				"1418 Invalid HBA PCI-device group: 0x%x\n",
4414 				dev_grp);
4415 		return -ENODEV;
4416 		break;
4417 	}
4418 	phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
4419 	phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4420 	return 0;
4421 }
4422 
4423 /**
4424  * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
4425  * @phba: The Hba for which this call is being executed.
4426  * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4427  * @rspiocbq: Pointer to lpfc_iocbq data structure.
4428  *
4429  * This routine is IOCB completion routine for device reset and target reset
4430  * routine. This routine release scsi buffer associated with lpfc_cmd.
4431  **/
4432 static void
4433 lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4434 			struct lpfc_iocbq *cmdiocbq,
4435 			struct lpfc_iocbq *rspiocbq)
4436 {
4437 	struct lpfc_scsi_buf *lpfc_cmd =
4438 		(struct lpfc_scsi_buf *) cmdiocbq->context1;
4439 	if (lpfc_cmd)
4440 		lpfc_release_scsi_buf(phba, lpfc_cmd);
4441 	return;
4442 }
4443 
4444 /**
4445  * lpfc_info - Info entry point of scsi_host_template data structure
4446  * @host: The scsi host for which this call is being executed.
4447  *
4448  * This routine provides module information about hba.
4449  *
4450  * Reutrn code:
4451  *   Pointer to char - Success.
4452  **/
4453 const char *
4454 lpfc_info(struct Scsi_Host *host)
4455 {
4456 	struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4457 	struct lpfc_hba   *phba = vport->phba;
4458 	int len, link_speed = 0;
4459 	static char  lpfcinfobuf[384];
4460 
4461 	memset(lpfcinfobuf,0,384);
4462 	if (phba && phba->pcidev){
4463 		strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4464 		len = strlen(lpfcinfobuf);
4465 		snprintf(lpfcinfobuf + len,
4466 			384-len,
4467 			" on PCI bus %02x device %02x irq %d",
4468 			phba->pcidev->bus->number,
4469 			phba->pcidev->devfn,
4470 			phba->pcidev->irq);
4471 		len = strlen(lpfcinfobuf);
4472 		if (phba->Port[0]) {
4473 			snprintf(lpfcinfobuf + len,
4474 				 384-len,
4475 				 " port %s",
4476 				 phba->Port);
4477 		}
4478 		len = strlen(lpfcinfobuf);
4479 		link_speed = lpfc_sli_port_speed_get(phba);
4480 		if (link_speed != 0)
4481 			snprintf(lpfcinfobuf + len, 384-len,
4482 				 " Logical Link Speed: %d Mbps", link_speed);
4483 	}
4484 	return lpfcinfobuf;
4485 }
4486 
4487 /**
4488  * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
4489  * @phba: The Hba for which this call is being executed.
4490  *
4491  * This routine modifies fcp_poll_timer  field of @phba by cfg_poll_tmo.
4492  * The default value of cfg_poll_tmo is 10 milliseconds.
4493  **/
4494 static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4495 {
4496 	unsigned long  poll_tmo_expires =
4497 		(jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4498 
4499 	if (!list_empty(&phba->sli.sli3_ring[LPFC_FCP_RING].txcmplq))
4500 		mod_timer(&phba->fcp_poll_timer,
4501 			  poll_tmo_expires);
4502 }
4503 
4504 /**
4505  * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
4506  * @phba: The Hba for which this call is being executed.
4507  *
4508  * This routine starts the fcp_poll_timer of @phba.
4509  **/
4510 void lpfc_poll_start_timer(struct lpfc_hba * phba)
4511 {
4512 	lpfc_poll_rearm_timer(phba);
4513 }
4514 
4515 /**
4516  * lpfc_poll_timeout - Restart polling timer
4517  * @ptr: Map to lpfc_hba data structure pointer.
4518  *
4519  * This routine restarts fcp_poll timer, when FCP ring  polling is enable
4520  * and FCP Ring interrupt is disable.
4521  **/
4522 
4523 void lpfc_poll_timeout(struct timer_list *t)
4524 {
4525 	struct lpfc_hba *phba = from_timer(phba, t, fcp_poll_timer);
4526 
4527 	if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
4528 		lpfc_sli_handle_fast_ring_event(phba,
4529 			&phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
4530 
4531 		if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4532 			lpfc_poll_rearm_timer(phba);
4533 	}
4534 }
4535 
4536 /**
4537  * lpfc_queuecommand - scsi_host_template queuecommand entry point
4538  * @cmnd: Pointer to scsi_cmnd data structure.
4539  * @done: Pointer to done routine.
4540  *
4541  * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4542  * This routine prepares an IOCB from scsi command and provides to firmware.
4543  * The @done callback is invoked after driver finished processing the command.
4544  *
4545  * Return value :
4546  *   0 - Success
4547  *   SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4548  **/
4549 static int
4550 lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
4551 {
4552 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4553 	struct lpfc_hba   *phba = vport->phba;
4554 	struct lpfc_rport_data *rdata;
4555 	struct lpfc_nodelist *ndlp;
4556 	struct lpfc_scsi_buf *lpfc_cmd;
4557 	struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
4558 	int err;
4559 
4560 	rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
4561 
4562 	/* sanity check on references */
4563 	if (unlikely(!rdata) || unlikely(!rport))
4564 		goto out_fail_command;
4565 
4566 	err = fc_remote_port_chkready(rport);
4567 	if (err) {
4568 		cmnd->result = err;
4569 		goto out_fail_command;
4570 	}
4571 	ndlp = rdata->pnode;
4572 
4573 	if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
4574 		(!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
4575 
4576 		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4577 				"9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4578 				" op:%02x str=%s without registering for"
4579 				" BlockGuard - Rejecting command\n",
4580 				cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4581 				dif_op_str[scsi_get_prot_op(cmnd)]);
4582 		goto out_fail_command;
4583 	}
4584 
4585 	/*
4586 	 * Catch race where our node has transitioned, but the
4587 	 * transport is still transitioning.
4588 	 */
4589 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4590 		goto out_tgt_busy;
4591 	if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
4592 		if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) {
4593 			lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
4594 					 "3377 Target Queue Full, scsi Id:%d "
4595 					 "Qdepth:%d Pending command:%d"
4596 					 " WWNN:%02x:%02x:%02x:%02x:"
4597 					 "%02x:%02x:%02x:%02x, "
4598 					 " WWPN:%02x:%02x:%02x:%02x:"
4599 					 "%02x:%02x:%02x:%02x",
4600 					 ndlp->nlp_sid, ndlp->cmd_qdepth,
4601 					 atomic_read(&ndlp->cmd_pending),
4602 					 ndlp->nlp_nodename.u.wwn[0],
4603 					 ndlp->nlp_nodename.u.wwn[1],
4604 					 ndlp->nlp_nodename.u.wwn[2],
4605 					 ndlp->nlp_nodename.u.wwn[3],
4606 					 ndlp->nlp_nodename.u.wwn[4],
4607 					 ndlp->nlp_nodename.u.wwn[5],
4608 					 ndlp->nlp_nodename.u.wwn[6],
4609 					 ndlp->nlp_nodename.u.wwn[7],
4610 					 ndlp->nlp_portname.u.wwn[0],
4611 					 ndlp->nlp_portname.u.wwn[1],
4612 					 ndlp->nlp_portname.u.wwn[2],
4613 					 ndlp->nlp_portname.u.wwn[3],
4614 					 ndlp->nlp_portname.u.wwn[4],
4615 					 ndlp->nlp_portname.u.wwn[5],
4616 					 ndlp->nlp_portname.u.wwn[6],
4617 					 ndlp->nlp_portname.u.wwn[7]);
4618 			goto out_tgt_busy;
4619 		}
4620 	}
4621 
4622 	lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
4623 	if (lpfc_cmd == NULL) {
4624 		lpfc_rampdown_queue_depth(phba);
4625 
4626 		lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
4627 				 "0707 driver's buffer pool is empty, "
4628 				 "IO busied\n");
4629 		goto out_host_busy;
4630 	}
4631 
4632 	/*
4633 	 * Store the midlayer's command structure for the completion phase
4634 	 * and complete the command initialization.
4635 	 */
4636 	lpfc_cmd->pCmd  = cmnd;
4637 	lpfc_cmd->rdata = rdata;
4638 	lpfc_cmd->ndlp = ndlp;
4639 	lpfc_cmd->timeout = 0;
4640 	lpfc_cmd->start_time = jiffies;
4641 	cmnd->host_scribble = (unsigned char *)lpfc_cmd;
4642 
4643 	if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
4644 		if (vport->phba->cfg_enable_bg) {
4645 			lpfc_printf_vlog(vport,
4646 					 KERN_INFO, LOG_SCSI_CMD,
4647 					 "9033 BLKGRD: rcvd %s cmd:x%x "
4648 					 "sector x%llx cnt %u pt %x\n",
4649 					 dif_op_str[scsi_get_prot_op(cmnd)],
4650 					 cmnd->cmnd[0],
4651 					 (unsigned long long)scsi_get_lba(cmnd),
4652 					 blk_rq_sectors(cmnd->request),
4653 					 (cmnd->cmnd[1]>>5));
4654 		}
4655 		err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4656 	} else {
4657 		if (vport->phba->cfg_enable_bg) {
4658 			lpfc_printf_vlog(vport,
4659 					 KERN_INFO, LOG_SCSI_CMD,
4660 					 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4661 					 "x%x sector x%llx cnt %u pt %x\n",
4662 					 cmnd->cmnd[0],
4663 					 (unsigned long long)scsi_get_lba(cmnd),
4664 					 blk_rq_sectors(cmnd->request),
4665 					 (cmnd->cmnd[1]>>5));
4666 		}
4667 		err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4668 	}
4669 
4670 	if (err)
4671 		goto out_host_busy_free_buf;
4672 
4673 	lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
4674 
4675 	err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
4676 				  &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
4677 	if (err) {
4678 		lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4679 				 "3376 FCP could not issue IOCB err %x"
4680 				 "FCP cmd x%x <%d/%llu> "
4681 				 "sid: x%x did: x%x oxid: x%x "
4682 				 "Data: x%x x%x x%x x%x\n",
4683 				 err, cmnd->cmnd[0],
4684 				 cmnd->device ? cmnd->device->id : 0xffff,
4685 				 cmnd->device ? cmnd->device->lun : (u64) -1,
4686 				 vport->fc_myDID, ndlp->nlp_DID,
4687 				 phba->sli_rev == LPFC_SLI_REV4 ?
4688 				 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
4689 				 lpfc_cmd->cur_iocbq.iocb.ulpContext,
4690 				 lpfc_cmd->cur_iocbq.iocb.ulpIoTag,
4691 				 lpfc_cmd->cur_iocbq.iocb.ulpTimeout,
4692 				 (uint32_t)
4693 				 (cmnd->request->timeout / 1000));
4694 
4695 		switch (lpfc_cmd->fcp_cmnd->fcpCntl3) {
4696 		case WRITE_DATA:
4697 			atomic_dec(&phba->fc4ScsiOutputRequests);
4698 			break;
4699 		case READ_DATA:
4700 			atomic_dec(&phba->fc4ScsiInputRequests);
4701 			break;
4702 		default:
4703 			atomic_dec(&phba->fc4ScsiControlRequests);
4704 		}
4705 		goto out_host_busy_free_buf;
4706 	}
4707 	if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
4708 		lpfc_sli_handle_fast_ring_event(phba,
4709 			&phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
4710 
4711 		if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4712 			lpfc_poll_rearm_timer(phba);
4713 	}
4714 
4715 	return 0;
4716 
4717  out_host_busy_free_buf:
4718 	lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
4719 	lpfc_release_scsi_buf(phba, lpfc_cmd);
4720  out_host_busy:
4721 	return SCSI_MLQUEUE_HOST_BUSY;
4722 
4723  out_tgt_busy:
4724 	return SCSI_MLQUEUE_TARGET_BUSY;
4725 
4726  out_fail_command:
4727 	cmnd->scsi_done(cmnd);
4728 	return 0;
4729 }
4730 
4731 
4732 /**
4733  * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
4734  * @cmnd: Pointer to scsi_cmnd data structure.
4735  *
4736  * This routine aborts @cmnd pending in base driver.
4737  *
4738  * Return code :
4739  *   0x2003 - Error
4740  *   0x2002 - Success
4741  **/
4742 static int
4743 lpfc_abort_handler(struct scsi_cmnd *cmnd)
4744 {
4745 	struct Scsi_Host  *shost = cmnd->device->host;
4746 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4747 	struct lpfc_hba   *phba = vport->phba;
4748 	struct lpfc_iocbq *iocb;
4749 	struct lpfc_iocbq *abtsiocb;
4750 	struct lpfc_scsi_buf *lpfc_cmd;
4751 	IOCB_t *cmd, *icmd;
4752 	int ret = SUCCESS, status = 0;
4753 	struct lpfc_sli_ring *pring_s4 = NULL;
4754 	int ret_val;
4755 	unsigned long flags;
4756 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
4757 
4758 	status = fc_block_scsi_eh(cmnd);
4759 	if (status != 0 && status != SUCCESS)
4760 		return status;
4761 
4762 	spin_lock_irqsave(&phba->hbalock, flags);
4763 	/* driver queued commands are in process of being flushed */
4764 	if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
4765 		spin_unlock_irqrestore(&phba->hbalock, flags);
4766 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4767 			"3168 SCSI Layer abort requested I/O has been "
4768 			"flushed by LLD.\n");
4769 		return FAILED;
4770 	}
4771 
4772 	lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
4773 	if (!lpfc_cmd || !lpfc_cmd->pCmd) {
4774 		spin_unlock_irqrestore(&phba->hbalock, flags);
4775 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4776 			 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
4777 			 "x%x ID %d LUN %llu\n",
4778 			 SUCCESS, cmnd->device->id, cmnd->device->lun);
4779 		return SUCCESS;
4780 	}
4781 
4782 	iocb = &lpfc_cmd->cur_iocbq;
4783 	if (phba->sli_rev == LPFC_SLI_REV4) {
4784 		if (!(phba->cfg_fof) ||
4785 		    (!(iocb->iocb_flag & LPFC_IO_FOF))) {
4786 			pring_s4 =
4787 				phba->sli4_hba.fcp_wq[iocb->hba_wqidx]->pring;
4788 		} else {
4789 			iocb->hba_wqidx = 0;
4790 			pring_s4 = phba->sli4_hba.oas_wq->pring;
4791 		}
4792 		if (!pring_s4) {
4793 			ret = FAILED;
4794 			goto out_unlock;
4795 		}
4796 		spin_lock(&pring_s4->ring_lock);
4797 	}
4798 	/* the command is in process of being cancelled */
4799 	if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
4800 		if (phba->sli_rev == LPFC_SLI_REV4)
4801 			spin_unlock(&pring_s4->ring_lock);
4802 		spin_unlock_irqrestore(&phba->hbalock, flags);
4803 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4804 			"3169 SCSI Layer abort requested I/O has been "
4805 			"cancelled by LLD.\n");
4806 		return FAILED;
4807 	}
4808 	/*
4809 	 * If pCmd field of the corresponding lpfc_scsi_buf structure
4810 	 * points to a different SCSI command, then the driver has
4811 	 * already completed this command, but the midlayer did not
4812 	 * see the completion before the eh fired. Just return SUCCESS.
4813 	 */
4814 	if (lpfc_cmd->pCmd != cmnd) {
4815 		if (phba->sli_rev == LPFC_SLI_REV4)
4816 			spin_unlock(&pring_s4->ring_lock);
4817 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4818 			"3170 SCSI Layer abort requested I/O has been "
4819 			"completed by LLD.\n");
4820 		goto out_unlock;
4821 	}
4822 
4823 	BUG_ON(iocb->context1 != lpfc_cmd);
4824 
4825 	/* abort issued in recovery is still in progress */
4826 	if (iocb->iocb_flag & LPFC_DRIVER_ABORTED) {
4827 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4828 			 "3389 SCSI Layer I/O Abort Request is pending\n");
4829 		if (phba->sli_rev == LPFC_SLI_REV4)
4830 			spin_unlock(&pring_s4->ring_lock);
4831 		spin_unlock_irqrestore(&phba->hbalock, flags);
4832 		goto wait_for_cmpl;
4833 	}
4834 
4835 	abtsiocb = __lpfc_sli_get_iocbq(phba);
4836 	if (abtsiocb == NULL) {
4837 		ret = FAILED;
4838 		if (phba->sli_rev == LPFC_SLI_REV4)
4839 			spin_unlock(&pring_s4->ring_lock);
4840 		goto out_unlock;
4841 	}
4842 
4843 	/* Indicate the IO is being aborted by the driver. */
4844 	iocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4845 
4846 	/*
4847 	 * The scsi command can not be in txq and it is in flight because the
4848 	 * pCmd is still pointig at the SCSI command we have to abort. There
4849 	 * is no need to search the txcmplq. Just send an abort to the FW.
4850 	 */
4851 
4852 	cmd = &iocb->iocb;
4853 	icmd = &abtsiocb->iocb;
4854 	icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4855 	icmd->un.acxri.abortContextTag = cmd->ulpContext;
4856 	if (phba->sli_rev == LPFC_SLI_REV4)
4857 		icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4858 	else
4859 		icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
4860 
4861 	icmd->ulpLe = 1;
4862 	icmd->ulpClass = cmd->ulpClass;
4863 
4864 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
4865 	abtsiocb->hba_wqidx = iocb->hba_wqidx;
4866 	abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
4867 	if (iocb->iocb_flag & LPFC_IO_FOF)
4868 		abtsiocb->iocb_flag |= LPFC_IO_FOF;
4869 
4870 	if (lpfc_is_link_up(phba))
4871 		icmd->ulpCommand = CMD_ABORT_XRI_CN;
4872 	else
4873 		icmd->ulpCommand = CMD_CLOSE_XRI_CN;
4874 
4875 	abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
4876 	abtsiocb->vport = vport;
4877 	lpfc_cmd->waitq = &waitq;
4878 	if (phba->sli_rev == LPFC_SLI_REV4) {
4879 		/* Note: both hbalock and ring_lock must be set here */
4880 		ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
4881 						abtsiocb, 0);
4882 		spin_unlock(&pring_s4->ring_lock);
4883 	} else {
4884 		ret_val = __lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
4885 						abtsiocb, 0);
4886 	}
4887 	/* no longer need the lock after this point */
4888 	spin_unlock_irqrestore(&phba->hbalock, flags);
4889 
4890 
4891 	if (ret_val == IOCB_ERROR) {
4892 		if (phba->sli_rev == LPFC_SLI_REV4)
4893 			spin_lock_irqsave(&pring_s4->ring_lock, flags);
4894 		else
4895 			spin_lock_irqsave(&phba->hbalock, flags);
4896 		/* Indicate the IO is not being aborted by the driver. */
4897 		iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
4898 		lpfc_cmd->waitq = NULL;
4899 		if (phba->sli_rev == LPFC_SLI_REV4)
4900 			spin_unlock_irqrestore(&pring_s4->ring_lock, flags);
4901 		else
4902 			spin_unlock_irqrestore(&phba->hbalock, flags);
4903 		lpfc_sli_release_iocbq(phba, abtsiocb);
4904 		ret = FAILED;
4905 		goto out;
4906 	}
4907 
4908 	if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4909 		lpfc_sli_handle_fast_ring_event(phba,
4910 			&phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
4911 
4912 wait_for_cmpl:
4913 	/* Wait for abort to complete */
4914 	wait_event_timeout(waitq,
4915 			  (lpfc_cmd->pCmd != cmnd),
4916 			   msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
4917 
4918 	spin_lock_irqsave(shost->host_lock, flags);
4919 	lpfc_cmd->waitq = NULL;
4920 	spin_unlock_irqrestore(shost->host_lock, flags);
4921 
4922 	if (lpfc_cmd->pCmd == cmnd) {
4923 		ret = FAILED;
4924 		lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4925 				 "0748 abort handler timed out waiting "
4926 				 "for aborting I/O (xri:x%x) to complete: "
4927 				 "ret %#x, ID %d, LUN %llu\n",
4928 				 iocb->sli4_xritag, ret,
4929 				 cmnd->device->id, cmnd->device->lun);
4930 	}
4931 	goto out;
4932 
4933 out_unlock:
4934 	spin_unlock_irqrestore(&phba->hbalock, flags);
4935 out:
4936 	lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4937 			 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
4938 			 "LUN %llu\n", ret, cmnd->device->id,
4939 			 cmnd->device->lun);
4940 	return ret;
4941 }
4942 
4943 static char *
4944 lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4945 {
4946 	switch (task_mgmt_cmd) {
4947 	case FCP_ABORT_TASK_SET:
4948 		return "ABORT_TASK_SET";
4949 	case FCP_CLEAR_TASK_SET:
4950 		return "FCP_CLEAR_TASK_SET";
4951 	case FCP_BUS_RESET:
4952 		return "FCP_BUS_RESET";
4953 	case FCP_LUN_RESET:
4954 		return "FCP_LUN_RESET";
4955 	case FCP_TARGET_RESET:
4956 		return "FCP_TARGET_RESET";
4957 	case FCP_CLEAR_ACA:
4958 		return "FCP_CLEAR_ACA";
4959 	case FCP_TERMINATE_TASK:
4960 		return "FCP_TERMINATE_TASK";
4961 	default:
4962 		return "unknown";
4963 	}
4964 }
4965 
4966 
4967 /**
4968  * lpfc_check_fcp_rsp - check the returned fcp_rsp to see if task failed
4969  * @vport: The virtual port for which this call is being executed.
4970  * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4971  *
4972  * This routine checks the FCP RSP INFO to see if the tsk mgmt command succeded
4973  *
4974  * Return code :
4975  *   0x2003 - Error
4976  *   0x2002 - Success
4977  **/
4978 static int
4979 lpfc_check_fcp_rsp(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd)
4980 {
4981 	struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
4982 	uint32_t rsp_info;
4983 	uint32_t rsp_len;
4984 	uint8_t  rsp_info_code;
4985 	int ret = FAILED;
4986 
4987 
4988 	if (fcprsp == NULL)
4989 		lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4990 				 "0703 fcp_rsp is missing\n");
4991 	else {
4992 		rsp_info = fcprsp->rspStatus2;
4993 		rsp_len = be32_to_cpu(fcprsp->rspRspLen);
4994 		rsp_info_code = fcprsp->rspInfo3;
4995 
4996 
4997 		lpfc_printf_vlog(vport, KERN_INFO,
4998 				 LOG_FCP,
4999 				 "0706 fcp_rsp valid 0x%x,"
5000 				 " rsp len=%d code 0x%x\n",
5001 				 rsp_info,
5002 				 rsp_len, rsp_info_code);
5003 
5004 		if ((fcprsp->rspStatus2&RSP_LEN_VALID) && (rsp_len == 8)) {
5005 			switch (rsp_info_code) {
5006 			case RSP_NO_FAILURE:
5007 				lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5008 						 "0715 Task Mgmt No Failure\n");
5009 				ret = SUCCESS;
5010 				break;
5011 			case RSP_TM_NOT_SUPPORTED: /* TM rejected */
5012 				lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5013 						 "0716 Task Mgmt Target "
5014 						"reject\n");
5015 				break;
5016 			case RSP_TM_NOT_COMPLETED: /* TM failed */
5017 				lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5018 						 "0717 Task Mgmt Target "
5019 						"failed TM\n");
5020 				break;
5021 			case RSP_TM_INVALID_LU: /* TM to invalid LU! */
5022 				lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5023 						 "0718 Task Mgmt to invalid "
5024 						"LUN\n");
5025 				break;
5026 			}
5027 		}
5028 	}
5029 	return ret;
5030 }
5031 
5032 
5033 /**
5034  * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
5035  * @vport: The virtual port for which this call is being executed.
5036  * @rdata: Pointer to remote port local data
5037  * @tgt_id: Target ID of remote device.
5038  * @lun_id: Lun number for the TMF
5039  * @task_mgmt_cmd: type of TMF to send
5040  *
5041  * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
5042  * a remote port.
5043  *
5044  * Return Code:
5045  *   0x2003 - Error
5046  *   0x2002 - Success.
5047  **/
5048 static int
5049 lpfc_send_taskmgmt(struct lpfc_vport *vport, struct scsi_cmnd *cmnd,
5050 		   unsigned int tgt_id, uint64_t lun_id,
5051 		   uint8_t task_mgmt_cmd)
5052 {
5053 	struct lpfc_hba   *phba = vport->phba;
5054 	struct lpfc_scsi_buf *lpfc_cmd;
5055 	struct lpfc_iocbq *iocbq;
5056 	struct lpfc_iocbq *iocbqrsp;
5057 	struct lpfc_rport_data *rdata;
5058 	struct lpfc_nodelist *pnode;
5059 	int ret;
5060 	int status;
5061 
5062 	rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
5063 	if (!rdata || !rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
5064 		return FAILED;
5065 	pnode = rdata->pnode;
5066 
5067 	lpfc_cmd = lpfc_get_scsi_buf(phba, pnode);
5068 	if (lpfc_cmd == NULL)
5069 		return FAILED;
5070 	lpfc_cmd->timeout = phba->cfg_task_mgmt_tmo;
5071 	lpfc_cmd->rdata = rdata;
5072 	lpfc_cmd->pCmd = cmnd;
5073 	lpfc_cmd->ndlp = pnode;
5074 
5075 	status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
5076 					   task_mgmt_cmd);
5077 	if (!status) {
5078 		lpfc_release_scsi_buf(phba, lpfc_cmd);
5079 		return FAILED;
5080 	}
5081 
5082 	iocbq = &lpfc_cmd->cur_iocbq;
5083 	iocbqrsp = lpfc_sli_get_iocbq(phba);
5084 	if (iocbqrsp == NULL) {
5085 		lpfc_release_scsi_buf(phba, lpfc_cmd);
5086 		return FAILED;
5087 	}
5088 	iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
5089 
5090 	lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5091 			 "0702 Issue %s to TGT %d LUN %llu "
5092 			 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
5093 			 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
5094 			 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
5095 			 iocbq->iocb_flag);
5096 
5097 	status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
5098 					  iocbq, iocbqrsp, lpfc_cmd->timeout);
5099 	if ((status != IOCB_SUCCESS) ||
5100 	    (iocbqrsp->iocb.ulpStatus != IOSTAT_SUCCESS)) {
5101 		if (status != IOCB_SUCCESS ||
5102 		    iocbqrsp->iocb.ulpStatus != IOSTAT_FCP_RSP_ERROR)
5103 			lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5104 					 "0727 TMF %s to TGT %d LUN %llu "
5105 					 "failed (%d, %d) iocb_flag x%x\n",
5106 					 lpfc_taskmgmt_name(task_mgmt_cmd),
5107 					 tgt_id, lun_id,
5108 					 iocbqrsp->iocb.ulpStatus,
5109 					 iocbqrsp->iocb.un.ulpWord[4],
5110 					 iocbq->iocb_flag);
5111 		/* if ulpStatus != IOCB_SUCCESS, then status == IOCB_SUCCESS */
5112 		if (status == IOCB_SUCCESS) {
5113 			if (iocbqrsp->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
5114 				/* Something in the FCP_RSP was invalid.
5115 				 * Check conditions */
5116 				ret = lpfc_check_fcp_rsp(vport, lpfc_cmd);
5117 			else
5118 				ret = FAILED;
5119 		} else if (status == IOCB_TIMEDOUT) {
5120 			ret = TIMEOUT_ERROR;
5121 		} else {
5122 			ret = FAILED;
5123 		}
5124 	} else
5125 		ret = SUCCESS;
5126 
5127 	lpfc_sli_release_iocbq(phba, iocbqrsp);
5128 
5129 	if (ret != TIMEOUT_ERROR)
5130 		lpfc_release_scsi_buf(phba, lpfc_cmd);
5131 
5132 	return ret;
5133 }
5134 
5135 /**
5136  * lpfc_chk_tgt_mapped -
5137  * @vport: The virtual port to check on
5138  * @cmnd: Pointer to scsi_cmnd data structure.
5139  *
5140  * This routine delays until the scsi target (aka rport) for the
5141  * command exists (is present and logged in) or we declare it non-existent.
5142  *
5143  * Return code :
5144  *  0x2003 - Error
5145  *  0x2002 - Success
5146  **/
5147 static int
5148 lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
5149 {
5150 	struct lpfc_rport_data *rdata;
5151 	struct lpfc_nodelist *pnode;
5152 	unsigned long later;
5153 
5154 	rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
5155 	if (!rdata) {
5156 		lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5157 			"0797 Tgt Map rport failure: rdata x%p\n", rdata);
5158 		return FAILED;
5159 	}
5160 	pnode = rdata->pnode;
5161 	/*
5162 	 * If target is not in a MAPPED state, delay until
5163 	 * target is rediscovered or devloss timeout expires.
5164 	 */
5165 	later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5166 	while (time_after(later, jiffies)) {
5167 		if (!pnode || !NLP_CHK_NODE_ACT(pnode))
5168 			return FAILED;
5169 		if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
5170 			return SUCCESS;
5171 		schedule_timeout_uninterruptible(msecs_to_jiffies(500));
5172 		rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
5173 		if (!rdata)
5174 			return FAILED;
5175 		pnode = rdata->pnode;
5176 	}
5177 	if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
5178 	    (pnode->nlp_state != NLP_STE_MAPPED_NODE))
5179 		return FAILED;
5180 	return SUCCESS;
5181 }
5182 
5183 /**
5184  * lpfc_reset_flush_io_context -
5185  * @vport: The virtual port (scsi_host) for the flush context
5186  * @tgt_id: If aborting by Target contect - specifies the target id
5187  * @lun_id: If aborting by Lun context - specifies the lun id
5188  * @context: specifies the context level to flush at.
5189  *
5190  * After a reset condition via TMF, we need to flush orphaned i/o
5191  * contexts from the adapter. This routine aborts any contexts
5192  * outstanding, then waits for their completions. The wait is
5193  * bounded by devloss_tmo though.
5194  *
5195  * Return code :
5196  *  0x2003 - Error
5197  *  0x2002 - Success
5198  **/
5199 static int
5200 lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5201 			uint64_t lun_id, lpfc_ctx_cmd context)
5202 {
5203 	struct lpfc_hba   *phba = vport->phba;
5204 	unsigned long later;
5205 	int cnt;
5206 
5207 	cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5208 	if (cnt)
5209 		lpfc_sli_abort_taskmgmt(vport,
5210 					&phba->sli.sli3_ring[LPFC_FCP_RING],
5211 					tgt_id, lun_id, context);
5212 	later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5213 	while (time_after(later, jiffies) && cnt) {
5214 		schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5215 		cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5216 	}
5217 	if (cnt) {
5218 		lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5219 			"0724 I/O flush failure for context %s : cnt x%x\n",
5220 			((context == LPFC_CTX_LUN) ? "LUN" :
5221 			 ((context == LPFC_CTX_TGT) ? "TGT" :
5222 			  ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5223 			cnt);
5224 		return FAILED;
5225 	}
5226 	return SUCCESS;
5227 }
5228 
5229 /**
5230  * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
5231  * @cmnd: Pointer to scsi_cmnd data structure.
5232  *
5233  * This routine does a device reset by sending a LUN_RESET task management
5234  * command.
5235  *
5236  * Return code :
5237  *  0x2003 - Error
5238  *  0x2002 - Success
5239  **/
5240 static int
5241 lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
5242 {
5243 	struct Scsi_Host  *shost = cmnd->device->host;
5244 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5245 	struct lpfc_rport_data *rdata;
5246 	struct lpfc_nodelist *pnode;
5247 	unsigned tgt_id = cmnd->device->id;
5248 	uint64_t lun_id = cmnd->device->lun;
5249 	struct lpfc_scsi_event_header scsi_event;
5250 	int status;
5251 
5252 	rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
5253 	if (!rdata || !rdata->pnode) {
5254 		lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5255 				 "0798 Device Reset rport failure: rdata x%p\n",
5256 				 rdata);
5257 		return FAILED;
5258 	}
5259 	pnode = rdata->pnode;
5260 	status = fc_block_scsi_eh(cmnd);
5261 	if (status != 0 && status != SUCCESS)
5262 		return status;
5263 
5264 	status = lpfc_chk_tgt_mapped(vport, cmnd);
5265 	if (status == FAILED) {
5266 		lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5267 			"0721 Device Reset rport failure: rdata x%p\n", rdata);
5268 		return FAILED;
5269 	}
5270 
5271 	scsi_event.event_type = FC_REG_SCSI_EVENT;
5272 	scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5273 	scsi_event.lun = lun_id;
5274 	memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5275 	memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5276 
5277 	fc_host_post_vendor_event(shost, fc_get_event_number(),
5278 		sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5279 
5280 	status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
5281 						FCP_LUN_RESET);
5282 
5283 	lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5284 			 "0713 SCSI layer issued Device Reset (%d, %llu) "
5285 			 "return x%x\n", tgt_id, lun_id, status);
5286 
5287 	/*
5288 	 * We have to clean up i/o as : they may be orphaned by the TMF;
5289 	 * or if the TMF failed, they may be in an indeterminate state.
5290 	 * So, continue on.
5291 	 * We will report success if all the i/o aborts successfully.
5292 	 */
5293 	if (status == SUCCESS)
5294 		status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5295 						LPFC_CTX_LUN);
5296 
5297 	return status;
5298 }
5299 
5300 /**
5301  * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5302  * @cmnd: Pointer to scsi_cmnd data structure.
5303  *
5304  * This routine does a target reset by sending a TARGET_RESET task management
5305  * command.
5306  *
5307  * Return code :
5308  *  0x2003 - Error
5309  *  0x2002 - Success
5310  **/
5311 static int
5312 lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5313 {
5314 	struct Scsi_Host  *shost = cmnd->device->host;
5315 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5316 	struct lpfc_rport_data *rdata;
5317 	struct lpfc_nodelist *pnode;
5318 	unsigned tgt_id = cmnd->device->id;
5319 	uint64_t lun_id = cmnd->device->lun;
5320 	struct lpfc_scsi_event_header scsi_event;
5321 	int status;
5322 
5323 	rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
5324 	if (!rdata) {
5325 		lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5326 			"0799 Target Reset rport failure: rdata x%p\n", rdata);
5327 		return FAILED;
5328 	}
5329 	pnode = rdata->pnode;
5330 	status = fc_block_scsi_eh(cmnd);
5331 	if (status != 0 && status != SUCCESS)
5332 		return status;
5333 
5334 	status = lpfc_chk_tgt_mapped(vport, cmnd);
5335 	if (status == FAILED) {
5336 		lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5337 			"0722 Target Reset rport failure: rdata x%p\n", rdata);
5338 		if (pnode) {
5339 			spin_lock_irq(shost->host_lock);
5340 			pnode->nlp_flag &= ~NLP_NPR_ADISC;
5341 			pnode->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
5342 			spin_unlock_irq(shost->host_lock);
5343 		}
5344 		lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5345 					  LPFC_CTX_TGT);
5346 		return FAST_IO_FAIL;
5347 	}
5348 
5349 	scsi_event.event_type = FC_REG_SCSI_EVENT;
5350 	scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5351 	scsi_event.lun = 0;
5352 	memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5353 	memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5354 
5355 	fc_host_post_vendor_event(shost, fc_get_event_number(),
5356 		sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5357 
5358 	status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
5359 					FCP_TARGET_RESET);
5360 
5361 	lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5362 			 "0723 SCSI layer issued Target Reset (%d, %llu) "
5363 			 "return x%x\n", tgt_id, lun_id, status);
5364 
5365 	/*
5366 	 * We have to clean up i/o as : they may be orphaned by the TMF;
5367 	 * or if the TMF failed, they may be in an indeterminate state.
5368 	 * So, continue on.
5369 	 * We will report success if all the i/o aborts successfully.
5370 	 */
5371 	if (status == SUCCESS)
5372 		status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5373 					  LPFC_CTX_TGT);
5374 	return status;
5375 }
5376 
5377 /**
5378  * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
5379  * @cmnd: Pointer to scsi_cmnd data structure.
5380  *
5381  * This routine does target reset to all targets on @cmnd->device->host.
5382  * This emulates Parallel SCSI Bus Reset Semantics.
5383  *
5384  * Return code :
5385  *  0x2003 - Error
5386  *  0x2002 - Success
5387  **/
5388 static int
5389 lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
5390 {
5391 	struct Scsi_Host  *shost = cmnd->device->host;
5392 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5393 	struct lpfc_nodelist *ndlp = NULL;
5394 	struct lpfc_scsi_event_header scsi_event;
5395 	int match;
5396 	int ret = SUCCESS, status, i;
5397 
5398 	scsi_event.event_type = FC_REG_SCSI_EVENT;
5399 	scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5400 	scsi_event.lun = 0;
5401 	memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5402 	memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5403 
5404 	fc_host_post_vendor_event(shost, fc_get_event_number(),
5405 		sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5406 
5407 	status = fc_block_scsi_eh(cmnd);
5408 	if (status != 0 && status != SUCCESS)
5409 		return status;
5410 
5411 	/*
5412 	 * Since the driver manages a single bus device, reset all
5413 	 * targets known to the driver.  Should any target reset
5414 	 * fail, this routine returns failure to the midlayer.
5415 	 */
5416 	for (i = 0; i < LPFC_MAX_TARGET; i++) {
5417 		/* Search for mapped node by target ID */
5418 		match = 0;
5419 		spin_lock_irq(shost->host_lock);
5420 		list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5421 			if (!NLP_CHK_NODE_ACT(ndlp))
5422 				continue;
5423 			if (vport->phba->cfg_fcp2_no_tgt_reset &&
5424 			    (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5425 				continue;
5426 			if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
5427 			    ndlp->nlp_sid == i &&
5428 			    ndlp->rport &&
5429 			    ndlp->nlp_type & NLP_FCP_TARGET) {
5430 				match = 1;
5431 				break;
5432 			}
5433 		}
5434 		spin_unlock_irq(shost->host_lock);
5435 		if (!match)
5436 			continue;
5437 
5438 		status = lpfc_send_taskmgmt(vport, cmnd,
5439 					i, 0, FCP_TARGET_RESET);
5440 
5441 		if (status != SUCCESS) {
5442 			lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5443 					 "0700 Bus Reset on target %d failed\n",
5444 					 i);
5445 			ret = FAILED;
5446 		}
5447 	}
5448 	/*
5449 	 * We have to clean up i/o as : they may be orphaned by the TMFs
5450 	 * above; or if any of the TMFs failed, they may be in an
5451 	 * indeterminate state.
5452 	 * We will report success if all the i/o aborts successfully.
5453 	 */
5454 
5455 	status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5456 	if (status != SUCCESS)
5457 		ret = FAILED;
5458 
5459 	lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5460 			 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
5461 	return ret;
5462 }
5463 
5464 /**
5465  * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
5466  * @cmnd: Pointer to scsi_cmnd data structure.
5467  *
5468  * This routine does host reset to the adaptor port. It brings the HBA
5469  * offline, performs a board restart, and then brings the board back online.
5470  * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
5471  * reject all outstanding SCSI commands to the host and error returned
5472  * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5473  * of error handling, it will only return error if resetting of the adapter
5474  * is not successful; in all other cases, will return success.
5475  *
5476  * Return code :
5477  *  0x2003 - Error
5478  *  0x2002 - Success
5479  **/
5480 static int
5481 lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5482 {
5483 	struct Scsi_Host *shost = cmnd->device->host;
5484 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5485 	struct lpfc_hba *phba = vport->phba;
5486 	int rc, ret = SUCCESS;
5487 
5488 	lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5489 			 "3172 SCSI layer issued Host Reset Data:\n");
5490 
5491 	lpfc_offline_prep(phba, LPFC_MBX_WAIT);
5492 	lpfc_offline(phba);
5493 	rc = lpfc_sli_brdrestart(phba);
5494 	if (rc)
5495 		ret = FAILED;
5496 	rc = lpfc_online(phba);
5497 	if (rc)
5498 		ret = FAILED;
5499 	lpfc_unblock_mgmt_io(phba);
5500 
5501 	if (ret == FAILED) {
5502 		lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5503 				 "3323 Failed host reset, bring it offline\n");
5504 		lpfc_sli4_offline_eratt(phba);
5505 	}
5506 	return ret;
5507 }
5508 
5509 /**
5510  * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
5511  * @sdev: Pointer to scsi_device.
5512  *
5513  * This routine populates the cmds_per_lun count + 2 scsi_bufs into  this host's
5514  * globally available list of scsi buffers. This routine also makes sure scsi
5515  * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5516  * of scsi buffer exists for the lifetime of the driver.
5517  *
5518  * Return codes:
5519  *   non-0 - Error
5520  *   0 - Success
5521  **/
5522 static int
5523 lpfc_slave_alloc(struct scsi_device *sdev)
5524 {
5525 	struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5526 	struct lpfc_hba   *phba = vport->phba;
5527 	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
5528 	uint32_t total = 0;
5529 	uint32_t num_to_alloc = 0;
5530 	int num_allocated = 0;
5531 	uint32_t sdev_cnt;
5532 	struct lpfc_device_data *device_data;
5533 	unsigned long flags;
5534 	struct lpfc_name target_wwpn;
5535 
5536 	if (!rport || fc_remote_port_chkready(rport))
5537 		return -ENXIO;
5538 
5539 	if (phba->cfg_fof) {
5540 
5541 		/*
5542 		 * Check to see if the device data structure for the lun
5543 		 * exists.  If not, create one.
5544 		 */
5545 
5546 		u64_to_wwn(rport->port_name, target_wwpn.u.wwn);
5547 		spin_lock_irqsave(&phba->devicelock, flags);
5548 		device_data = __lpfc_get_device_data(phba,
5549 						     &phba->luns,
5550 						     &vport->fc_portname,
5551 						     &target_wwpn,
5552 						     sdev->lun);
5553 		if (!device_data) {
5554 			spin_unlock_irqrestore(&phba->devicelock, flags);
5555 			device_data = lpfc_create_device_data(phba,
5556 							&vport->fc_portname,
5557 							&target_wwpn,
5558 							sdev->lun,
5559 							phba->cfg_XLanePriority,
5560 							true);
5561 			if (!device_data)
5562 				return -ENOMEM;
5563 			spin_lock_irqsave(&phba->devicelock, flags);
5564 			list_add_tail(&device_data->listentry, &phba->luns);
5565 		}
5566 		device_data->rport_data = rport->dd_data;
5567 		device_data->available = true;
5568 		spin_unlock_irqrestore(&phba->devicelock, flags);
5569 		sdev->hostdata = device_data;
5570 	} else {
5571 		sdev->hostdata = rport->dd_data;
5572 	}
5573 	sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
5574 
5575 	/*
5576 	 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5577 	 * available list of scsi buffers.  Don't allocate more than the
5578 	 * HBA limit conveyed to the midlayer via the host structure.  The
5579 	 * formula accounts for the lun_queue_depth + error handlers + 1
5580 	 * extra.  This list of scsi bufs exists for the lifetime of the driver.
5581 	 */
5582 	total = phba->total_scsi_bufs;
5583 	num_to_alloc = vport->cfg_lun_queue_depth + 2;
5584 
5585 	/* If allocated buffers are enough do nothing */
5586 	if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5587 		return 0;
5588 
5589 	/* Allow some exchanges to be available always to complete discovery */
5590 	if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
5591 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5592 				 "0704 At limitation of %d preallocated "
5593 				 "command buffers\n", total);
5594 		return 0;
5595 	/* Allow some exchanges to be available always to complete discovery */
5596 	} else if (total + num_to_alloc >
5597 		phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
5598 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5599 				 "0705 Allocation request of %d "
5600 				 "command buffers will exceed max of %d.  "
5601 				 "Reducing allocation request to %d.\n",
5602 				 num_to_alloc, phba->cfg_hba_queue_depth,
5603 				 (phba->cfg_hba_queue_depth - total));
5604 		num_to_alloc = phba->cfg_hba_queue_depth - total;
5605 	}
5606 	num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
5607 	if (num_to_alloc != num_allocated) {
5608 			lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5609 					 "0708 Allocation request of %d "
5610 					 "command buffers did not succeed.  "
5611 					 "Allocated %d buffers.\n",
5612 					 num_to_alloc, num_allocated);
5613 	}
5614 	if (num_allocated > 0)
5615 		phba->total_scsi_bufs += num_allocated;
5616 	return 0;
5617 }
5618 
5619 /**
5620  * lpfc_slave_configure - scsi_host_template slave_configure entry point
5621  * @sdev: Pointer to scsi_device.
5622  *
5623  * This routine configures following items
5624  *   - Tag command queuing support for @sdev if supported.
5625  *   - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5626  *
5627  * Return codes:
5628  *   0 - Success
5629  **/
5630 static int
5631 lpfc_slave_configure(struct scsi_device *sdev)
5632 {
5633 	struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5634 	struct lpfc_hba   *phba = vport->phba;
5635 
5636 	scsi_change_queue_depth(sdev, vport->cfg_lun_queue_depth);
5637 
5638 	if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
5639 		lpfc_sli_handle_fast_ring_event(phba,
5640 			&phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
5641 		if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5642 			lpfc_poll_rearm_timer(phba);
5643 	}
5644 
5645 	return 0;
5646 }
5647 
5648 /**
5649  * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
5650  * @sdev: Pointer to scsi_device.
5651  *
5652  * This routine sets @sdev hostatdata filed to null.
5653  **/
5654 static void
5655 lpfc_slave_destroy(struct scsi_device *sdev)
5656 {
5657 	struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5658 	struct lpfc_hba   *phba = vport->phba;
5659 	unsigned long flags;
5660 	struct lpfc_device_data *device_data = sdev->hostdata;
5661 
5662 	atomic_dec(&phba->sdev_cnt);
5663 	if ((phba->cfg_fof) && (device_data)) {
5664 		spin_lock_irqsave(&phba->devicelock, flags);
5665 		device_data->available = false;
5666 		if (!device_data->oas_enabled)
5667 			lpfc_delete_device_data(phba, device_data);
5668 		spin_unlock_irqrestore(&phba->devicelock, flags);
5669 	}
5670 	sdev->hostdata = NULL;
5671 	return;
5672 }
5673 
5674 /**
5675  * lpfc_create_device_data - creates and initializes device data structure for OAS
5676  * @pha: Pointer to host bus adapter structure.
5677  * @vport_wwpn: Pointer to vport's wwpn information
5678  * @target_wwpn: Pointer to target's wwpn information
5679  * @lun: Lun on target
5680  * @atomic_create: Flag to indicate if memory should be allocated using the
5681  *		  GFP_ATOMIC flag or not.
5682  *
5683  * This routine creates a device data structure which will contain identifying
5684  * information for the device (host wwpn, target wwpn, lun), state of OAS,
5685  * whether or not the corresponding lun is available by the system,
5686  * and pointer to the rport data.
5687  *
5688  * Return codes:
5689  *   NULL - Error
5690  *   Pointer to lpfc_device_data - Success
5691  **/
5692 struct lpfc_device_data*
5693 lpfc_create_device_data(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5694 			struct lpfc_name *target_wwpn, uint64_t lun,
5695 			uint32_t pri, bool atomic_create)
5696 {
5697 
5698 	struct lpfc_device_data *lun_info;
5699 	int memory_flags;
5700 
5701 	if (unlikely(!phba) || !vport_wwpn || !target_wwpn  ||
5702 	    !(phba->cfg_fof))
5703 		return NULL;
5704 
5705 	/* Attempt to create the device data to contain lun info */
5706 
5707 	if (atomic_create)
5708 		memory_flags = GFP_ATOMIC;
5709 	else
5710 		memory_flags = GFP_KERNEL;
5711 	lun_info = mempool_alloc(phba->device_data_mem_pool, memory_flags);
5712 	if (!lun_info)
5713 		return NULL;
5714 	INIT_LIST_HEAD(&lun_info->listentry);
5715 	lun_info->rport_data  = NULL;
5716 	memcpy(&lun_info->device_id.vport_wwpn, vport_wwpn,
5717 	       sizeof(struct lpfc_name));
5718 	memcpy(&lun_info->device_id.target_wwpn, target_wwpn,
5719 	       sizeof(struct lpfc_name));
5720 	lun_info->device_id.lun = lun;
5721 	lun_info->oas_enabled = false;
5722 	lun_info->priority = pri;
5723 	lun_info->available = false;
5724 	return lun_info;
5725 }
5726 
5727 /**
5728  * lpfc_delete_device_data - frees a device data structure for OAS
5729  * @pha: Pointer to host bus adapter structure.
5730  * @lun_info: Pointer to device data structure to free.
5731  *
5732  * This routine frees the previously allocated device data structure passed.
5733  *
5734  **/
5735 void
5736 lpfc_delete_device_data(struct lpfc_hba *phba,
5737 			struct lpfc_device_data *lun_info)
5738 {
5739 
5740 	if (unlikely(!phba) || !lun_info  ||
5741 	    !(phba->cfg_fof))
5742 		return;
5743 
5744 	if (!list_empty(&lun_info->listentry))
5745 		list_del(&lun_info->listentry);
5746 	mempool_free(lun_info, phba->device_data_mem_pool);
5747 	return;
5748 }
5749 
5750 /**
5751  * __lpfc_get_device_data - returns the device data for the specified lun
5752  * @pha: Pointer to host bus adapter structure.
5753  * @list: Point to list to search.
5754  * @vport_wwpn: Pointer to vport's wwpn information
5755  * @target_wwpn: Pointer to target's wwpn information
5756  * @lun: Lun on target
5757  *
5758  * This routine searches the list passed for the specified lun's device data.
5759  * This function does not hold locks, it is the responsibility of the caller
5760  * to ensure the proper lock is held before calling the function.
5761  *
5762  * Return codes:
5763  *   NULL - Error
5764  *   Pointer to lpfc_device_data - Success
5765  **/
5766 struct lpfc_device_data*
5767 __lpfc_get_device_data(struct lpfc_hba *phba, struct list_head *list,
5768 		       struct lpfc_name *vport_wwpn,
5769 		       struct lpfc_name *target_wwpn, uint64_t lun)
5770 {
5771 
5772 	struct lpfc_device_data *lun_info;
5773 
5774 	if (unlikely(!phba) || !list || !vport_wwpn || !target_wwpn ||
5775 	    !phba->cfg_fof)
5776 		return NULL;
5777 
5778 	/* Check to see if the lun is already enabled for OAS. */
5779 
5780 	list_for_each_entry(lun_info, list, listentry) {
5781 		if ((memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5782 			    sizeof(struct lpfc_name)) == 0) &&
5783 		    (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5784 			    sizeof(struct lpfc_name)) == 0) &&
5785 		    (lun_info->device_id.lun == lun))
5786 			return lun_info;
5787 	}
5788 
5789 	return NULL;
5790 }
5791 
5792 /**
5793  * lpfc_find_next_oas_lun - searches for the next oas lun
5794  * @pha: Pointer to host bus adapter structure.
5795  * @vport_wwpn: Pointer to vport's wwpn information
5796  * @target_wwpn: Pointer to target's wwpn information
5797  * @starting_lun: Pointer to the lun to start searching for
5798  * @found_vport_wwpn: Pointer to the found lun's vport wwpn information
5799  * @found_target_wwpn: Pointer to the found lun's target wwpn information
5800  * @found_lun: Pointer to the found lun.
5801  * @found_lun_status: Pointer to status of the found lun.
5802  *
5803  * This routine searches the luns list for the specified lun
5804  * or the first lun for the vport/target.  If the vport wwpn contains
5805  * a zero value then a specific vport is not specified. In this case
5806  * any vport which contains the lun will be considered a match.  If the
5807  * target wwpn contains a zero value then a specific target is not specified.
5808  * In this case any target which contains the lun will be considered a
5809  * match.  If the lun is found, the lun, vport wwpn, target wwpn and lun status
5810  * are returned.  The function will also return the next lun if available.
5811  * If the next lun is not found, starting_lun parameter will be set to
5812  * NO_MORE_OAS_LUN.
5813  *
5814  * Return codes:
5815  *   non-0 - Error
5816  *   0 - Success
5817  **/
5818 bool
5819 lpfc_find_next_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5820 		       struct lpfc_name *target_wwpn, uint64_t *starting_lun,
5821 		       struct lpfc_name *found_vport_wwpn,
5822 		       struct lpfc_name *found_target_wwpn,
5823 		       uint64_t *found_lun,
5824 		       uint32_t *found_lun_status,
5825 		       uint32_t *found_lun_pri)
5826 {
5827 
5828 	unsigned long flags;
5829 	struct lpfc_device_data *lun_info;
5830 	struct lpfc_device_id *device_id;
5831 	uint64_t lun;
5832 	bool found = false;
5833 
5834 	if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5835 	    !starting_lun || !found_vport_wwpn ||
5836 	    !found_target_wwpn || !found_lun || !found_lun_status ||
5837 	    (*starting_lun == NO_MORE_OAS_LUN) ||
5838 	    !phba->cfg_fof)
5839 		return false;
5840 
5841 	lun = *starting_lun;
5842 	*found_lun = NO_MORE_OAS_LUN;
5843 	*starting_lun = NO_MORE_OAS_LUN;
5844 
5845 	/* Search for lun or the lun closet in value */
5846 
5847 	spin_lock_irqsave(&phba->devicelock, flags);
5848 	list_for_each_entry(lun_info, &phba->luns, listentry) {
5849 		if (((wwn_to_u64(vport_wwpn->u.wwn) == 0) ||
5850 		     (memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5851 			    sizeof(struct lpfc_name)) == 0)) &&
5852 		    ((wwn_to_u64(target_wwpn->u.wwn) == 0) ||
5853 		     (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5854 			    sizeof(struct lpfc_name)) == 0)) &&
5855 		    (lun_info->oas_enabled)) {
5856 			device_id = &lun_info->device_id;
5857 			if ((!found) &&
5858 			    ((lun == FIND_FIRST_OAS_LUN) ||
5859 			     (device_id->lun == lun))) {
5860 				*found_lun = device_id->lun;
5861 				memcpy(found_vport_wwpn,
5862 				       &device_id->vport_wwpn,
5863 				       sizeof(struct lpfc_name));
5864 				memcpy(found_target_wwpn,
5865 				       &device_id->target_wwpn,
5866 				       sizeof(struct lpfc_name));
5867 				if (lun_info->available)
5868 					*found_lun_status =
5869 						OAS_LUN_STATUS_EXISTS;
5870 				else
5871 					*found_lun_status = 0;
5872 				*found_lun_pri = lun_info->priority;
5873 				if (phba->cfg_oas_flags & OAS_FIND_ANY_VPORT)
5874 					memset(vport_wwpn, 0x0,
5875 					       sizeof(struct lpfc_name));
5876 				if (phba->cfg_oas_flags & OAS_FIND_ANY_TARGET)
5877 					memset(target_wwpn, 0x0,
5878 					       sizeof(struct lpfc_name));
5879 				found = true;
5880 			} else if (found) {
5881 				*starting_lun = device_id->lun;
5882 				memcpy(vport_wwpn, &device_id->vport_wwpn,
5883 				       sizeof(struct lpfc_name));
5884 				memcpy(target_wwpn, &device_id->target_wwpn,
5885 				       sizeof(struct lpfc_name));
5886 				break;
5887 			}
5888 		}
5889 	}
5890 	spin_unlock_irqrestore(&phba->devicelock, flags);
5891 	return found;
5892 }
5893 
5894 /**
5895  * lpfc_enable_oas_lun - enables a lun for OAS operations
5896  * @pha: Pointer to host bus adapter structure.
5897  * @vport_wwpn: Pointer to vport's wwpn information
5898  * @target_wwpn: Pointer to target's wwpn information
5899  * @lun: Lun
5900  *
5901  * This routine enables a lun for oas operations.  The routines does so by
5902  * doing the following :
5903  *
5904  *   1) Checks to see if the device data for the lun has been created.
5905  *   2) If found, sets the OAS enabled flag if not set and returns.
5906  *   3) Otherwise, creates a device data structure.
5907  *   4) If successfully created, indicates the device data is for an OAS lun,
5908  *   indicates the lun is not available and add to the list of luns.
5909  *
5910  * Return codes:
5911  *   false - Error
5912  *   true - Success
5913  **/
5914 bool
5915 lpfc_enable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5916 		    struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
5917 {
5918 
5919 	struct lpfc_device_data *lun_info;
5920 	unsigned long flags;
5921 
5922 	if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5923 	    !phba->cfg_fof)
5924 		return false;
5925 
5926 	spin_lock_irqsave(&phba->devicelock, flags);
5927 
5928 	/* Check to see if the device data for the lun has been created */
5929 	lun_info = __lpfc_get_device_data(phba, &phba->luns, vport_wwpn,
5930 					  target_wwpn, lun);
5931 	if (lun_info) {
5932 		if (!lun_info->oas_enabled)
5933 			lun_info->oas_enabled = true;
5934 		lun_info->priority = pri;
5935 		spin_unlock_irqrestore(&phba->devicelock, flags);
5936 		return true;
5937 	}
5938 
5939 	/* Create an lun info structure and add to list of luns */
5940 	lun_info = lpfc_create_device_data(phba, vport_wwpn, target_wwpn, lun,
5941 					   pri, false);
5942 	if (lun_info) {
5943 		lun_info->oas_enabled = true;
5944 		lun_info->priority = pri;
5945 		lun_info->available = false;
5946 		list_add_tail(&lun_info->listentry, &phba->luns);
5947 		spin_unlock_irqrestore(&phba->devicelock, flags);
5948 		return true;
5949 	}
5950 	spin_unlock_irqrestore(&phba->devicelock, flags);
5951 	return false;
5952 }
5953 
5954 /**
5955  * lpfc_disable_oas_lun - disables a lun for OAS operations
5956  * @pha: Pointer to host bus adapter structure.
5957  * @vport_wwpn: Pointer to vport's wwpn information
5958  * @target_wwpn: Pointer to target's wwpn information
5959  * @lun: Lun
5960  *
5961  * This routine disables a lun for oas operations.  The routines does so by
5962  * doing the following :
5963  *
5964  *   1) Checks to see if the device data for the lun is created.
5965  *   2) If present, clears the flag indicating this lun is for OAS.
5966  *   3) If the lun is not available by the system, the device data is
5967  *   freed.
5968  *
5969  * Return codes:
5970  *   false - Error
5971  *   true - Success
5972  **/
5973 bool
5974 lpfc_disable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5975 		     struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
5976 {
5977 
5978 	struct lpfc_device_data *lun_info;
5979 	unsigned long flags;
5980 
5981 	if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5982 	    !phba->cfg_fof)
5983 		return false;
5984 
5985 	spin_lock_irqsave(&phba->devicelock, flags);
5986 
5987 	/* Check to see if the lun is available. */
5988 	lun_info = __lpfc_get_device_data(phba,
5989 					  &phba->luns, vport_wwpn,
5990 					  target_wwpn, lun);
5991 	if (lun_info) {
5992 		lun_info->oas_enabled = false;
5993 		lun_info->priority = pri;
5994 		if (!lun_info->available)
5995 			lpfc_delete_device_data(phba, lun_info);
5996 		spin_unlock_irqrestore(&phba->devicelock, flags);
5997 		return true;
5998 	}
5999 
6000 	spin_unlock_irqrestore(&phba->devicelock, flags);
6001 	return false;
6002 }
6003 
6004 static int
6005 lpfc_no_command(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
6006 {
6007 	return SCSI_MLQUEUE_HOST_BUSY;
6008 }
6009 
6010 static int
6011 lpfc_no_handler(struct scsi_cmnd *cmnd)
6012 {
6013 	return FAILED;
6014 }
6015 
6016 static int
6017 lpfc_no_slave(struct scsi_device *sdev)
6018 {
6019 	return -ENODEV;
6020 }
6021 
6022 struct scsi_host_template lpfc_template_nvme = {
6023 	.module			= THIS_MODULE,
6024 	.name			= LPFC_DRIVER_NAME,
6025 	.proc_name		= LPFC_DRIVER_NAME,
6026 	.info			= lpfc_info,
6027 	.queuecommand		= lpfc_no_command,
6028 	.eh_abort_handler	= lpfc_no_handler,
6029 	.eh_device_reset_handler = lpfc_no_handler,
6030 	.eh_target_reset_handler = lpfc_no_handler,
6031 	.eh_bus_reset_handler	= lpfc_no_handler,
6032 	.eh_host_reset_handler  = lpfc_no_handler,
6033 	.slave_alloc		= lpfc_no_slave,
6034 	.slave_configure	= lpfc_no_slave,
6035 	.scan_finished		= lpfc_scan_finished,
6036 	.this_id		= -1,
6037 	.sg_tablesize		= 1,
6038 	.cmd_per_lun		= 1,
6039 	.use_clustering		= ENABLE_CLUSTERING,
6040 	.shost_attrs		= lpfc_hba_attrs,
6041 	.max_sectors		= 0xFFFF,
6042 	.vendor_id		= LPFC_NL_VENDOR_ID,
6043 	.track_queue_depth	= 0,
6044 };
6045 
6046 struct scsi_host_template lpfc_template_no_hr = {
6047 	.module			= THIS_MODULE,
6048 	.name			= LPFC_DRIVER_NAME,
6049 	.proc_name		= LPFC_DRIVER_NAME,
6050 	.info			= lpfc_info,
6051 	.queuecommand		= lpfc_queuecommand,
6052 	.eh_timed_out		= fc_eh_timed_out,
6053 	.eh_abort_handler	= lpfc_abort_handler,
6054 	.eh_device_reset_handler = lpfc_device_reset_handler,
6055 	.eh_target_reset_handler = lpfc_target_reset_handler,
6056 	.eh_bus_reset_handler	= lpfc_bus_reset_handler,
6057 	.slave_alloc		= lpfc_slave_alloc,
6058 	.slave_configure	= lpfc_slave_configure,
6059 	.slave_destroy		= lpfc_slave_destroy,
6060 	.scan_finished		= lpfc_scan_finished,
6061 	.this_id		= -1,
6062 	.sg_tablesize		= LPFC_DEFAULT_SG_SEG_CNT,
6063 	.cmd_per_lun		= LPFC_CMD_PER_LUN,
6064 	.use_clustering		= ENABLE_CLUSTERING,
6065 	.shost_attrs		= lpfc_hba_attrs,
6066 	.max_sectors		= 0xFFFF,
6067 	.vendor_id		= LPFC_NL_VENDOR_ID,
6068 	.change_queue_depth	= scsi_change_queue_depth,
6069 	.track_queue_depth	= 1,
6070 };
6071 
6072 struct scsi_host_template lpfc_template = {
6073 	.module			= THIS_MODULE,
6074 	.name			= LPFC_DRIVER_NAME,
6075 	.proc_name		= LPFC_DRIVER_NAME,
6076 	.info			= lpfc_info,
6077 	.queuecommand		= lpfc_queuecommand,
6078 	.eh_timed_out		= fc_eh_timed_out,
6079 	.eh_abort_handler	= lpfc_abort_handler,
6080 	.eh_device_reset_handler = lpfc_device_reset_handler,
6081 	.eh_target_reset_handler = lpfc_target_reset_handler,
6082 	.eh_bus_reset_handler	= lpfc_bus_reset_handler,
6083 	.eh_host_reset_handler  = lpfc_host_reset_handler,
6084 	.slave_alloc		= lpfc_slave_alloc,
6085 	.slave_configure	= lpfc_slave_configure,
6086 	.slave_destroy		= lpfc_slave_destroy,
6087 	.scan_finished		= lpfc_scan_finished,
6088 	.this_id		= -1,
6089 	.sg_tablesize		= LPFC_DEFAULT_SG_SEG_CNT,
6090 	.cmd_per_lun		= LPFC_CMD_PER_LUN,
6091 	.use_clustering		= ENABLE_CLUSTERING,
6092 	.shost_attrs		= lpfc_hba_attrs,
6093 	.max_sectors		= 0xFFFF,
6094 	.vendor_id		= LPFC_NL_VENDOR_ID,
6095 	.change_queue_depth	= scsi_change_queue_depth,
6096 	.track_queue_depth	= 1,
6097 };
6098 
6099 struct scsi_host_template lpfc_vport_template = {
6100 	.module			= THIS_MODULE,
6101 	.name			= LPFC_DRIVER_NAME,
6102 	.proc_name		= LPFC_DRIVER_NAME,
6103 	.info			= lpfc_info,
6104 	.queuecommand		= lpfc_queuecommand,
6105 	.eh_timed_out		= fc_eh_timed_out,
6106 	.eh_abort_handler	= lpfc_abort_handler,
6107 	.eh_device_reset_handler = lpfc_device_reset_handler,
6108 	.eh_target_reset_handler = lpfc_target_reset_handler,
6109 	.slave_alloc		= lpfc_slave_alloc,
6110 	.slave_configure	= lpfc_slave_configure,
6111 	.slave_destroy		= lpfc_slave_destroy,
6112 	.scan_finished		= lpfc_scan_finished,
6113 	.this_id		= -1,
6114 	.sg_tablesize		= LPFC_DEFAULT_SG_SEG_CNT,
6115 	.cmd_per_lun		= LPFC_CMD_PER_LUN,
6116 	.use_clustering		= ENABLE_CLUSTERING,
6117 	.shost_attrs		= lpfc_vport_attrs,
6118 	.max_sectors		= 0xFFFF,
6119 	.change_queue_depth	= scsi_change_queue_depth,
6120 	.track_queue_depth	= 1,
6121 };
6122