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