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