xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_scsi.c (revision 81d67439)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2011 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/delay.h>
25 #include <asm/unaligned.h>
26 
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/scsi_eh.h>
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_tcq.h>
32 #include <scsi/scsi_transport_fc.h>
33 
34 #include "lpfc_version.h"
35 #include "lpfc_hw4.h"
36 #include "lpfc_hw.h"
37 #include "lpfc_sli.h"
38 #include "lpfc_sli4.h"
39 #include "lpfc_nl.h"
40 #include "lpfc_disc.h"
41 #include "lpfc_scsi.h"
42 #include "lpfc.h"
43 #include "lpfc_logmsg.h"
44 #include "lpfc_crtn.h"
45 #include "lpfc_vport.h"
46 
47 #define LPFC_RESET_WAIT  2
48 #define LPFC_ABORT_WAIT  2
49 
50 int _dump_buf_done;
51 
52 static char *dif_op_str[] = {
53 	"SCSI_PROT_NORMAL",
54 	"SCSI_PROT_READ_INSERT",
55 	"SCSI_PROT_WRITE_STRIP",
56 	"SCSI_PROT_READ_STRIP",
57 	"SCSI_PROT_WRITE_INSERT",
58 	"SCSI_PROT_READ_PASS",
59 	"SCSI_PROT_WRITE_PASS",
60 };
61 static void
62 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
63 static void
64 lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
65 
66 static void
67 lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
68 {
69 	void *src, *dst;
70 	struct scatterlist *sgde = scsi_sglist(cmnd);
71 
72 	if (!_dump_buf_data) {
73 		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
74 			"9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
75 				__func__);
76 		return;
77 	}
78 
79 
80 	if (!sgde) {
81 		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
82 			"9051 BLKGRD: ERROR: data scatterlist is null\n");
83 		return;
84 	}
85 
86 	dst = (void *) _dump_buf_data;
87 	while (sgde) {
88 		src = sg_virt(sgde);
89 		memcpy(dst, src, sgde->length);
90 		dst += sgde->length;
91 		sgde = sg_next(sgde);
92 	}
93 }
94 
95 static void
96 lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
97 {
98 	void *src, *dst;
99 	struct scatterlist *sgde = scsi_prot_sglist(cmnd);
100 
101 	if (!_dump_buf_dif) {
102 		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
103 			"9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
104 				__func__);
105 		return;
106 	}
107 
108 	if (!sgde) {
109 		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
110 			"9053 BLKGRD: ERROR: prot scatterlist is null\n");
111 		return;
112 	}
113 
114 	dst = _dump_buf_dif;
115 	while (sgde) {
116 		src = sg_virt(sgde);
117 		memcpy(dst, src, sgde->length);
118 		dst += sgde->length;
119 		sgde = sg_next(sgde);
120 	}
121 }
122 
123 /**
124  * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
125  * @phba: Pointer to HBA object.
126  * @lpfc_cmd: lpfc scsi command object pointer.
127  *
128  * This function is called from the lpfc_prep_task_mgmt_cmd function to
129  * set the last bit in the response sge entry.
130  **/
131 static void
132 lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
133 				struct lpfc_scsi_buf *lpfc_cmd)
134 {
135 	struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
136 	if (sgl) {
137 		sgl += 1;
138 		sgl->word2 = le32_to_cpu(sgl->word2);
139 		bf_set(lpfc_sli4_sge_last, sgl, 1);
140 		sgl->word2 = cpu_to_le32(sgl->word2);
141 	}
142 }
143 
144 /**
145  * lpfc_update_stats - Update statistical data for the command completion
146  * @phba: Pointer to HBA object.
147  * @lpfc_cmd: lpfc scsi command object pointer.
148  *
149  * This function is called when there is a command completion and this
150  * function updates the statistical data for the command completion.
151  **/
152 static void
153 lpfc_update_stats(struct lpfc_hba *phba, struct  lpfc_scsi_buf *lpfc_cmd)
154 {
155 	struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
156 	struct lpfc_nodelist *pnode = rdata->pnode;
157 	struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
158 	unsigned long flags;
159 	struct Scsi_Host  *shost = cmd->device->host;
160 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
161 	unsigned long latency;
162 	int i;
163 
164 	if (cmd->result)
165 		return;
166 
167 	latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
168 
169 	spin_lock_irqsave(shost->host_lock, flags);
170 	if (!vport->stat_data_enabled ||
171 		vport->stat_data_blocked ||
172 		!pnode ||
173 		!pnode->lat_data ||
174 		(phba->bucket_type == LPFC_NO_BUCKET)) {
175 		spin_unlock_irqrestore(shost->host_lock, flags);
176 		return;
177 	}
178 
179 	if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
180 		i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
181 			phba->bucket_step;
182 		/* check array subscript bounds */
183 		if (i < 0)
184 			i = 0;
185 		else if (i >= LPFC_MAX_BUCKET_COUNT)
186 			i = LPFC_MAX_BUCKET_COUNT - 1;
187 	} else {
188 		for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
189 			if (latency <= (phba->bucket_base +
190 				((1<<i)*phba->bucket_step)))
191 				break;
192 	}
193 
194 	pnode->lat_data[i].cmd_count++;
195 	spin_unlock_irqrestore(shost->host_lock, flags);
196 }
197 
198 /**
199  * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
200  * @phba: Pointer to HBA context object.
201  * @vport: Pointer to vport object.
202  * @ndlp: Pointer to FC node associated with the target.
203  * @lun: Lun number of the scsi device.
204  * @old_val: Old value of the queue depth.
205  * @new_val: New value of the queue depth.
206  *
207  * This function sends an event to the mgmt application indicating
208  * there is a change in the scsi device queue depth.
209  **/
210 static void
211 lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
212 		struct lpfc_vport  *vport,
213 		struct lpfc_nodelist *ndlp,
214 		uint32_t lun,
215 		uint32_t old_val,
216 		uint32_t new_val)
217 {
218 	struct lpfc_fast_path_event *fast_path_evt;
219 	unsigned long flags;
220 
221 	fast_path_evt = lpfc_alloc_fast_evt(phba);
222 	if (!fast_path_evt)
223 		return;
224 
225 	fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
226 		FC_REG_SCSI_EVENT;
227 	fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
228 		LPFC_EVENT_VARQUEDEPTH;
229 
230 	/* Report all luns with change in queue depth */
231 	fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
232 	if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
233 		memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
234 			&ndlp->nlp_portname, sizeof(struct lpfc_name));
235 		memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
236 			&ndlp->nlp_nodename, sizeof(struct lpfc_name));
237 	}
238 
239 	fast_path_evt->un.queue_depth_evt.oldval = old_val;
240 	fast_path_evt->un.queue_depth_evt.newval = new_val;
241 	fast_path_evt->vport = vport;
242 
243 	fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
244 	spin_lock_irqsave(&phba->hbalock, flags);
245 	list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
246 	spin_unlock_irqrestore(&phba->hbalock, flags);
247 	lpfc_worker_wake_up(phba);
248 
249 	return;
250 }
251 
252 /**
253  * lpfc_change_queue_depth - Alter scsi device queue depth
254  * @sdev: Pointer the scsi device on which to change the queue depth.
255  * @qdepth: New queue depth to set the sdev to.
256  * @reason: The reason for the queue depth change.
257  *
258  * This function is called by the midlayer and the LLD to alter the queue
259  * depth for a scsi device. This function sets the queue depth to the new
260  * value and sends an event out to log the queue depth change.
261  **/
262 int
263 lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
264 {
265 	struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
266 	struct lpfc_hba   *phba = vport->phba;
267 	struct lpfc_rport_data *rdata;
268 	unsigned long new_queue_depth, old_queue_depth;
269 
270 	old_queue_depth = sdev->queue_depth;
271 	scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
272 	new_queue_depth = sdev->queue_depth;
273 	rdata = sdev->hostdata;
274 	if (rdata)
275 		lpfc_send_sdev_queuedepth_change_event(phba, vport,
276 						       rdata->pnode, sdev->lun,
277 						       old_queue_depth,
278 						       new_queue_depth);
279 	return sdev->queue_depth;
280 }
281 
282 /**
283  * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
284  * @phba: The Hba for which this call is being executed.
285  *
286  * This routine is called when there is resource error in driver or firmware.
287  * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
288  * posts at most 1 event each second. This routine wakes up worker thread of
289  * @phba to process WORKER_RAM_DOWN_EVENT event.
290  *
291  * This routine should be called with no lock held.
292  **/
293 void
294 lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
295 {
296 	unsigned long flags;
297 	uint32_t evt_posted;
298 
299 	spin_lock_irqsave(&phba->hbalock, flags);
300 	atomic_inc(&phba->num_rsrc_err);
301 	phba->last_rsrc_error_time = jiffies;
302 
303 	if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
304 		spin_unlock_irqrestore(&phba->hbalock, flags);
305 		return;
306 	}
307 
308 	phba->last_ramp_down_time = jiffies;
309 
310 	spin_unlock_irqrestore(&phba->hbalock, flags);
311 
312 	spin_lock_irqsave(&phba->pport->work_port_lock, flags);
313 	evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
314 	if (!evt_posted)
315 		phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
316 	spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
317 
318 	if (!evt_posted)
319 		lpfc_worker_wake_up(phba);
320 	return;
321 }
322 
323 /**
324  * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
325  * @phba: The Hba for which this call is being executed.
326  *
327  * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
328  * post at most 1 event every 5 minute after last_ramp_up_time or
329  * last_rsrc_error_time.  This routine wakes up worker thread of @phba
330  * to process WORKER_RAM_DOWN_EVENT event.
331  *
332  * This routine should be called with no lock held.
333  **/
334 static inline void
335 lpfc_rampup_queue_depth(struct lpfc_vport  *vport,
336 			uint32_t queue_depth)
337 {
338 	unsigned long flags;
339 	struct lpfc_hba *phba = vport->phba;
340 	uint32_t evt_posted;
341 	atomic_inc(&phba->num_cmd_success);
342 
343 	if (vport->cfg_lun_queue_depth <= queue_depth)
344 		return;
345 	spin_lock_irqsave(&phba->hbalock, flags);
346 	if (time_before(jiffies,
347 			phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
348 	    time_before(jiffies,
349 			phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
350 		spin_unlock_irqrestore(&phba->hbalock, flags);
351 		return;
352 	}
353 	phba->last_ramp_up_time = jiffies;
354 	spin_unlock_irqrestore(&phba->hbalock, flags);
355 
356 	spin_lock_irqsave(&phba->pport->work_port_lock, flags);
357 	evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
358 	if (!evt_posted)
359 		phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
360 	spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
361 
362 	if (!evt_posted)
363 		lpfc_worker_wake_up(phba);
364 	return;
365 }
366 
367 /**
368  * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
369  * @phba: The Hba for which this call is being executed.
370  *
371  * This routine is called to  process WORKER_RAMP_DOWN_QUEUE event for worker
372  * thread.This routine reduces queue depth for all scsi device on each vport
373  * associated with @phba.
374  **/
375 void
376 lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
377 {
378 	struct lpfc_vport **vports;
379 	struct Scsi_Host  *shost;
380 	struct scsi_device *sdev;
381 	unsigned long new_queue_depth;
382 	unsigned long num_rsrc_err, num_cmd_success;
383 	int i;
384 
385 	num_rsrc_err = atomic_read(&phba->num_rsrc_err);
386 	num_cmd_success = atomic_read(&phba->num_cmd_success);
387 
388 	vports = lpfc_create_vport_work_array(phba);
389 	if (vports != NULL)
390 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
391 			shost = lpfc_shost_from_vport(vports[i]);
392 			shost_for_each_device(sdev, shost) {
393 				new_queue_depth =
394 					sdev->queue_depth * num_rsrc_err /
395 					(num_rsrc_err + num_cmd_success);
396 				if (!new_queue_depth)
397 					new_queue_depth = sdev->queue_depth - 1;
398 				else
399 					new_queue_depth = sdev->queue_depth -
400 								new_queue_depth;
401 				lpfc_change_queue_depth(sdev, new_queue_depth,
402 							SCSI_QDEPTH_DEFAULT);
403 			}
404 		}
405 	lpfc_destroy_vport_work_array(phba, vports);
406 	atomic_set(&phba->num_rsrc_err, 0);
407 	atomic_set(&phba->num_cmd_success, 0);
408 }
409 
410 /**
411  * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
412  * @phba: The Hba for which this call is being executed.
413  *
414  * This routine is called to  process WORKER_RAMP_UP_QUEUE event for worker
415  * thread.This routine increases queue depth for all scsi device on each vport
416  * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
417  * num_cmd_success to zero.
418  **/
419 void
420 lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
421 {
422 	struct lpfc_vport **vports;
423 	struct Scsi_Host  *shost;
424 	struct scsi_device *sdev;
425 	int i;
426 
427 	vports = lpfc_create_vport_work_array(phba);
428 	if (vports != NULL)
429 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
430 			shost = lpfc_shost_from_vport(vports[i]);
431 			shost_for_each_device(sdev, shost) {
432 				if (vports[i]->cfg_lun_queue_depth <=
433 				    sdev->queue_depth)
434 					continue;
435 				lpfc_change_queue_depth(sdev,
436 							sdev->queue_depth+1,
437 							SCSI_QDEPTH_RAMP_UP);
438 			}
439 		}
440 	lpfc_destroy_vport_work_array(phba, vports);
441 	atomic_set(&phba->num_rsrc_err, 0);
442 	atomic_set(&phba->num_cmd_success, 0);
443 }
444 
445 /**
446  * lpfc_scsi_dev_block - set all scsi hosts to block state
447  * @phba: Pointer to HBA context object.
448  *
449  * This function walks vport list and set each SCSI host to block state
450  * by invoking fc_remote_port_delete() routine. This function is invoked
451  * with EEH when device's PCI slot has been permanently disabled.
452  **/
453 void
454 lpfc_scsi_dev_block(struct lpfc_hba *phba)
455 {
456 	struct lpfc_vport **vports;
457 	struct Scsi_Host  *shost;
458 	struct scsi_device *sdev;
459 	struct fc_rport *rport;
460 	int i;
461 
462 	vports = lpfc_create_vport_work_array(phba);
463 	if (vports != NULL)
464 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
465 			shost = lpfc_shost_from_vport(vports[i]);
466 			shost_for_each_device(sdev, shost) {
467 				rport = starget_to_rport(scsi_target(sdev));
468 				fc_remote_port_delete(rport);
469 			}
470 		}
471 	lpfc_destroy_vport_work_array(phba, vports);
472 }
473 
474 /**
475  * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
476  * @vport: The virtual port for which this call being executed.
477  * @num_to_allocate: The requested number of buffers to allocate.
478  *
479  * This routine allocates a scsi buffer for device with SLI-3 interface spec,
480  * the scsi buffer contains all the necessary information needed to initiate
481  * a SCSI I/O. The non-DMAable buffer region contains information to build
482  * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
483  * and the initial BPL. In addition to allocating memory, the FCP CMND and
484  * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
485  *
486  * Return codes:
487  *   int - number of scsi buffers that were allocated.
488  *   0 = failure, less than num_to_alloc is a partial failure.
489  **/
490 static int
491 lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
492 {
493 	struct lpfc_hba *phba = vport->phba;
494 	struct lpfc_scsi_buf *psb;
495 	struct ulp_bde64 *bpl;
496 	IOCB_t *iocb;
497 	dma_addr_t pdma_phys_fcp_cmd;
498 	dma_addr_t pdma_phys_fcp_rsp;
499 	dma_addr_t pdma_phys_bpl;
500 	uint16_t iotag;
501 	int bcnt;
502 
503 	for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
504 		psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
505 		if (!psb)
506 			break;
507 
508 		/*
509 		 * Get memory from the pci pool to map the virt space to pci
510 		 * bus space for an I/O.  The DMA buffer includes space for the
511 		 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
512 		 * necessary to support the sg_tablesize.
513 		 */
514 		psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
515 					GFP_KERNEL, &psb->dma_handle);
516 		if (!psb->data) {
517 			kfree(psb);
518 			break;
519 		}
520 
521 		/* Initialize virtual ptrs to dma_buf region. */
522 		memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
523 
524 		/* Allocate iotag for psb->cur_iocbq. */
525 		iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
526 		if (iotag == 0) {
527 			pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
528 					psb->data, psb->dma_handle);
529 			kfree(psb);
530 			break;
531 		}
532 		psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
533 
534 		psb->fcp_cmnd = psb->data;
535 		psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
536 		psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
537 			sizeof(struct fcp_rsp);
538 
539 		/* Initialize local short-hand pointers. */
540 		bpl = psb->fcp_bpl;
541 		pdma_phys_fcp_cmd = psb->dma_handle;
542 		pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
543 		pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
544 			sizeof(struct fcp_rsp);
545 
546 		/*
547 		 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
548 		 * are sg list bdes.  Initialize the first two and leave the
549 		 * rest for queuecommand.
550 		 */
551 		bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
552 		bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
553 		bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
554 		bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
555 		bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
556 
557 		/* Setup the physical region for the FCP RSP */
558 		bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
559 		bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
560 		bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
561 		bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
562 		bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
563 
564 		/*
565 		 * Since the IOCB for the FCP I/O is built into this
566 		 * lpfc_scsi_buf, initialize it with all known data now.
567 		 */
568 		iocb = &psb->cur_iocbq.iocb;
569 		iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
570 		if ((phba->sli_rev == 3) &&
571 				!(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
572 			/* fill in immediate fcp command BDE */
573 			iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
574 			iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
575 			iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
576 					unsli3.fcp_ext.icd);
577 			iocb->un.fcpi64.bdl.addrHigh = 0;
578 			iocb->ulpBdeCount = 0;
579 			iocb->ulpLe = 0;
580 			/* fill in response BDE */
581 			iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
582 							BUFF_TYPE_BDE_64;
583 			iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
584 				sizeof(struct fcp_rsp);
585 			iocb->unsli3.fcp_ext.rbde.addrLow =
586 				putPaddrLow(pdma_phys_fcp_rsp);
587 			iocb->unsli3.fcp_ext.rbde.addrHigh =
588 				putPaddrHigh(pdma_phys_fcp_rsp);
589 		} else {
590 			iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
591 			iocb->un.fcpi64.bdl.bdeSize =
592 					(2 * sizeof(struct ulp_bde64));
593 			iocb->un.fcpi64.bdl.addrLow =
594 					putPaddrLow(pdma_phys_bpl);
595 			iocb->un.fcpi64.bdl.addrHigh =
596 					putPaddrHigh(pdma_phys_bpl);
597 			iocb->ulpBdeCount = 1;
598 			iocb->ulpLe = 1;
599 		}
600 		iocb->ulpClass = CLASS3;
601 		psb->status = IOSTAT_SUCCESS;
602 		/* Put it back into the SCSI buffer list */
603 		psb->cur_iocbq.context1  = psb;
604 		lpfc_release_scsi_buf_s3(phba, psb);
605 
606 	}
607 
608 	return bcnt;
609 }
610 
611 /**
612  * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
613  * @vport: pointer to lpfc vport data structure.
614  *
615  * This routine is invoked by the vport cleanup for deletions and the cleanup
616  * for an ndlp on removal.
617  **/
618 void
619 lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
620 {
621 	struct lpfc_hba *phba = vport->phba;
622 	struct lpfc_scsi_buf *psb, *next_psb;
623 	unsigned long iflag = 0;
624 
625 	spin_lock_irqsave(&phba->hbalock, iflag);
626 	spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
627 	list_for_each_entry_safe(psb, next_psb,
628 				&phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
629 		if (psb->rdata && psb->rdata->pnode
630 			&& psb->rdata->pnode->vport == vport)
631 			psb->rdata = NULL;
632 	}
633 	spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
634 	spin_unlock_irqrestore(&phba->hbalock, iflag);
635 }
636 
637 /**
638  * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
639  * @phba: pointer to lpfc hba data structure.
640  * @axri: pointer to the fcp xri abort wcqe structure.
641  *
642  * This routine is invoked by the worker thread to process a SLI4 fast-path
643  * FCP aborted xri.
644  **/
645 void
646 lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
647 			  struct sli4_wcqe_xri_aborted *axri)
648 {
649 	uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
650 	uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
651 	struct lpfc_scsi_buf *psb, *next_psb;
652 	unsigned long iflag = 0;
653 	struct lpfc_iocbq *iocbq;
654 	int i;
655 	struct lpfc_nodelist *ndlp;
656 	int rrq_empty = 0;
657 	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
658 
659 	spin_lock_irqsave(&phba->hbalock, iflag);
660 	spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
661 	list_for_each_entry_safe(psb, next_psb,
662 		&phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
663 		if (psb->cur_iocbq.sli4_xritag == xri) {
664 			list_del(&psb->list);
665 			psb->exch_busy = 0;
666 			psb->status = IOSTAT_SUCCESS;
667 			spin_unlock(
668 				&phba->sli4_hba.abts_scsi_buf_list_lock);
669 			if (psb->rdata && psb->rdata->pnode)
670 				ndlp = psb->rdata->pnode;
671 			else
672 				ndlp = NULL;
673 
674 			rrq_empty = list_empty(&phba->active_rrq_list);
675 			spin_unlock_irqrestore(&phba->hbalock, iflag);
676 			if (ndlp)
677 				lpfc_set_rrq_active(phba, ndlp, xri, rxid, 1);
678 			lpfc_release_scsi_buf_s4(phba, psb);
679 			if (rrq_empty)
680 				lpfc_worker_wake_up(phba);
681 			return;
682 		}
683 	}
684 	spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
685 	for (i = 1; i <= phba->sli.last_iotag; i++) {
686 		iocbq = phba->sli.iocbq_lookup[i];
687 
688 		if (!(iocbq->iocb_flag &  LPFC_IO_FCP) ||
689 			(iocbq->iocb_flag & LPFC_IO_LIBDFC))
690 			continue;
691 		if (iocbq->sli4_xritag != xri)
692 			continue;
693 		psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
694 		psb->exch_busy = 0;
695 		spin_unlock_irqrestore(&phba->hbalock, iflag);
696 		if (pring->txq_cnt)
697 			lpfc_worker_wake_up(phba);
698 		return;
699 
700 	}
701 	spin_unlock_irqrestore(&phba->hbalock, iflag);
702 }
703 
704 /**
705  * lpfc_sli4_repost_scsi_sgl_list - Repsot the Scsi buffers sgl pages as block
706  * @phba: pointer to lpfc hba data structure.
707  *
708  * This routine walks the list of scsi buffers that have been allocated and
709  * repost them to the HBA by using SGL block post. This is needed after a
710  * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
711  * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
712  * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
713  *
714  * Returns: 0 = success, non-zero failure.
715  **/
716 int
717 lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
718 {
719 	struct lpfc_scsi_buf *psb;
720 	int index, status, bcnt = 0, rcnt = 0, rc = 0;
721 	LIST_HEAD(sblist);
722 
723 	for (index = 0; index < phba->sli4_hba.scsi_xri_cnt; index++) {
724 		psb = phba->sli4_hba.lpfc_scsi_psb_array[index];
725 		if (psb) {
726 			/* Remove from SCSI buffer list */
727 			list_del(&psb->list);
728 			/* Add it to a local SCSI buffer list */
729 			list_add_tail(&psb->list, &sblist);
730 			if (++rcnt == LPFC_NEMBED_MBOX_SGL_CNT) {
731 				bcnt = rcnt;
732 				rcnt = 0;
733 			}
734 		} else
735 			/* A hole present in the XRI array, need to skip */
736 			bcnt = rcnt;
737 
738 		if (index == phba->sli4_hba.scsi_xri_cnt - 1)
739 			/* End of XRI array for SCSI buffer, complete */
740 			bcnt = rcnt;
741 
742 		/* Continue until collect up to a nembed page worth of sgls */
743 		if (bcnt == 0)
744 			continue;
745 		/* Now, post the SCSI buffer list sgls as a block */
746 		if (!phba->sli4_hba.extents_in_use)
747 			status = lpfc_sli4_post_scsi_sgl_block(phba,
748 							&sblist,
749 							bcnt);
750 		else
751 			status = lpfc_sli4_post_scsi_sgl_blk_ext(phba,
752 							&sblist,
753 							bcnt);
754 		/* Reset SCSI buffer count for next round of posting */
755 		bcnt = 0;
756 		while (!list_empty(&sblist)) {
757 			list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
758 					 list);
759 			if (status) {
760 				/* Put this back on the abort scsi list */
761 				psb->exch_busy = 1;
762 				rc++;
763 			} else {
764 				psb->exch_busy = 0;
765 				psb->status = IOSTAT_SUCCESS;
766 			}
767 			/* Put it back into the SCSI buffer list */
768 			lpfc_release_scsi_buf_s4(phba, psb);
769 		}
770 	}
771 	return rc;
772 }
773 
774 /**
775  * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
776  * @vport: The virtual port for which this call being executed.
777  * @num_to_allocate: The requested number of buffers to allocate.
778  *
779  * This routine allocates a scsi buffer for device with SLI-4 interface spec,
780  * the scsi buffer contains all the necessary information needed to initiate
781  * a SCSI I/O.
782  *
783  * Return codes:
784  *   int - number of scsi buffers that were allocated.
785  *   0 = failure, less than num_to_alloc is a partial failure.
786  **/
787 static int
788 lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
789 {
790 	struct lpfc_hba *phba = vport->phba;
791 	struct lpfc_scsi_buf *psb;
792 	struct sli4_sge *sgl;
793 	IOCB_t *iocb;
794 	dma_addr_t pdma_phys_fcp_cmd;
795 	dma_addr_t pdma_phys_fcp_rsp;
796 	dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
797 	uint16_t iotag, last_xritag = NO_XRI, lxri = 0;
798 	int status = 0, index;
799 	int bcnt;
800 	int non_sequential_xri = 0;
801 	LIST_HEAD(sblist);
802 
803 	for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
804 		psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
805 		if (!psb)
806 			break;
807 
808 		/*
809 		 * Get memory from the pci pool to map the virt space to pci bus
810 		 * space for an I/O.  The DMA buffer includes space for the
811 		 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
812 		 * necessary to support the sg_tablesize.
813 		 */
814 		psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
815 						GFP_KERNEL, &psb->dma_handle);
816 		if (!psb->data) {
817 			kfree(psb);
818 			break;
819 		}
820 
821 		/* Initialize virtual ptrs to dma_buf region. */
822 		memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
823 
824 		/* Allocate iotag for psb->cur_iocbq. */
825 		iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
826 		if (iotag == 0) {
827 			pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
828 				psb->data, psb->dma_handle);
829 			kfree(psb);
830 			break;
831 		}
832 
833 		lxri = lpfc_sli4_next_xritag(phba);
834 		if (lxri == NO_XRI) {
835 			pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
836 			      psb->data, psb->dma_handle);
837 			kfree(psb);
838 			break;
839 		}
840 		psb->cur_iocbq.sli4_lxritag = lxri;
841 		psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
842 		if (last_xritag != NO_XRI
843 			&& psb->cur_iocbq.sli4_xritag != (last_xritag+1)) {
844 			non_sequential_xri = 1;
845 		} else
846 			list_add_tail(&psb->list, &sblist);
847 		last_xritag = psb->cur_iocbq.sli4_xritag;
848 
849 		index = phba->sli4_hba.scsi_xri_cnt++;
850 		psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
851 
852 		psb->fcp_bpl = psb->data;
853 		psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
854 			- (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
855 		psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
856 					sizeof(struct fcp_cmnd));
857 
858 		/* Initialize local short-hand pointers. */
859 		sgl = (struct sli4_sge *)psb->fcp_bpl;
860 		pdma_phys_bpl = psb->dma_handle;
861 		pdma_phys_fcp_cmd =
862 			(psb->dma_handle + phba->cfg_sg_dma_buf_size)
863 			 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
864 		pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
865 
866 		/*
867 		 * The first two bdes are the FCP_CMD and FCP_RSP.  The balance
868 		 * are sg list bdes.  Initialize the first two and leave the
869 		 * rest for queuecommand.
870 		 */
871 		sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
872 		sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
873 		sgl->word2 = le32_to_cpu(sgl->word2);
874 		bf_set(lpfc_sli4_sge_last, sgl, 0);
875 		sgl->word2 = cpu_to_le32(sgl->word2);
876 		sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
877 		sgl++;
878 
879 		/* Setup the physical region for the FCP RSP */
880 		sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
881 		sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
882 		sgl->word2 = le32_to_cpu(sgl->word2);
883 		bf_set(lpfc_sli4_sge_last, sgl, 1);
884 		sgl->word2 = cpu_to_le32(sgl->word2);
885 		sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
886 
887 		/*
888 		 * Since the IOCB for the FCP I/O is built into this
889 		 * lpfc_scsi_buf, initialize it with all known data now.
890 		 */
891 		iocb = &psb->cur_iocbq.iocb;
892 		iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
893 		iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
894 		/* setting the BLP size to 2 * sizeof BDE may not be correct.
895 		 * We are setting the bpl to point to out sgl. An sgl's
896 		 * entries are 16 bytes, a bpl entries are 12 bytes.
897 		 */
898 		iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
899 		iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
900 		iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
901 		iocb->ulpBdeCount = 1;
902 		iocb->ulpLe = 1;
903 		iocb->ulpClass = CLASS3;
904 		psb->cur_iocbq.context1  = psb;
905 		if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
906 			pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
907 		else
908 			pdma_phys_bpl1 = 0;
909 		psb->dma_phys_bpl = pdma_phys_bpl;
910 		phba->sli4_hba.lpfc_scsi_psb_array[index] = psb;
911 		if (non_sequential_xri) {
912 			status = lpfc_sli4_post_sgl(phba, pdma_phys_bpl,
913 						pdma_phys_bpl1,
914 						psb->cur_iocbq.sli4_xritag);
915 			if (status) {
916 				/* Put this back on the abort scsi list */
917 				psb->exch_busy = 1;
918 			} else {
919 				psb->exch_busy = 0;
920 				psb->status = IOSTAT_SUCCESS;
921 			}
922 			/* Put it back into the SCSI buffer list */
923 			lpfc_release_scsi_buf_s4(phba, psb);
924 			break;
925 		}
926 	}
927 	if (bcnt) {
928 		if (!phba->sli4_hba.extents_in_use)
929 			status = lpfc_sli4_post_scsi_sgl_block(phba,
930 								&sblist,
931 								bcnt);
932 		else
933 			status = lpfc_sli4_post_scsi_sgl_blk_ext(phba,
934 								&sblist,
935 								bcnt);
936 
937 		if (status) {
938 			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
939 					"3021 SCSI SGL post error %d\n",
940 					status);
941 			bcnt = 0;
942 		}
943 		/* Reset SCSI buffer count for next round of posting */
944 		while (!list_empty(&sblist)) {
945 			list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
946 				 list);
947 			if (status) {
948 				/* Put this back on the abort scsi list */
949 				psb->exch_busy = 1;
950 			} else {
951 				psb->exch_busy = 0;
952 				psb->status = IOSTAT_SUCCESS;
953 			}
954 			/* Put it back into the SCSI buffer list */
955 			lpfc_release_scsi_buf_s4(phba, psb);
956 		}
957 	}
958 
959 	return bcnt + non_sequential_xri;
960 }
961 
962 /**
963  * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
964  * @vport: The virtual port for which this call being executed.
965  * @num_to_allocate: The requested number of buffers to allocate.
966  *
967  * This routine wraps the actual SCSI buffer allocator function pointer from
968  * the lpfc_hba struct.
969  *
970  * Return codes:
971  *   int - number of scsi buffers that were allocated.
972  *   0 = failure, less than num_to_alloc is a partial failure.
973  **/
974 static inline int
975 lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
976 {
977 	return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
978 }
979 
980 /**
981  * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
982  * @phba: The HBA for which this call is being executed.
983  *
984  * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
985  * and returns to caller.
986  *
987  * Return codes:
988  *   NULL - Error
989  *   Pointer to lpfc_scsi_buf - Success
990  **/
991 static struct lpfc_scsi_buf*
992 lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
993 {
994 	struct  lpfc_scsi_buf * lpfc_cmd = NULL;
995 	struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
996 	unsigned long iflag = 0;
997 
998 	spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
999 	list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
1000 	if (lpfc_cmd) {
1001 		lpfc_cmd->seg_cnt = 0;
1002 		lpfc_cmd->nonsg_phys = 0;
1003 		lpfc_cmd->prot_seg_cnt = 0;
1004 	}
1005 	spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
1006 	return  lpfc_cmd;
1007 }
1008 /**
1009  * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1010  * @phba: The HBA for which this call is being executed.
1011  *
1012  * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1013  * and returns to caller.
1014  *
1015  * Return codes:
1016  *   NULL - Error
1017  *   Pointer to lpfc_scsi_buf - Success
1018  **/
1019 static struct lpfc_scsi_buf*
1020 lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1021 {
1022 	struct lpfc_scsi_buf *lpfc_cmd ;
1023 	unsigned long iflag = 0;
1024 	int found = 0;
1025 
1026 	spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1027 	list_for_each_entry(lpfc_cmd, &phba->lpfc_scsi_buf_list,
1028 							list) {
1029 		if (lpfc_test_rrq_active(phba, ndlp,
1030 					 lpfc_cmd->cur_iocbq.sli4_xritag))
1031 			continue;
1032 		list_del(&lpfc_cmd->list);
1033 		found = 1;
1034 		lpfc_cmd->seg_cnt = 0;
1035 		lpfc_cmd->nonsg_phys = 0;
1036 		lpfc_cmd->prot_seg_cnt = 0;
1037 		break;
1038 	}
1039 	spin_unlock_irqrestore(&phba->scsi_buf_list_lock,
1040 						 iflag);
1041 	if (!found)
1042 		return NULL;
1043 	else
1044 		return  lpfc_cmd;
1045 }
1046 /**
1047  * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1048  * @phba: The HBA for which this call is being executed.
1049  *
1050  * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1051  * and returns to caller.
1052  *
1053  * Return codes:
1054  *   NULL - Error
1055  *   Pointer to lpfc_scsi_buf - Success
1056  **/
1057 static struct lpfc_scsi_buf*
1058 lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1059 {
1060 	return  phba->lpfc_get_scsi_buf(phba, ndlp);
1061 }
1062 
1063 /**
1064  * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
1065  * @phba: The Hba for which this call is being executed.
1066  * @psb: The scsi buffer which is being released.
1067  *
1068  * This routine releases @psb scsi buffer by adding it to tail of @phba
1069  * lpfc_scsi_buf_list list.
1070  **/
1071 static void
1072 lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1073 {
1074 	unsigned long iflag = 0;
1075 
1076 	spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1077 	psb->pCmd = NULL;
1078 	list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
1079 	spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
1080 }
1081 
1082 /**
1083  * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1084  * @phba: The Hba for which this call is being executed.
1085  * @psb: The scsi buffer which is being released.
1086  *
1087  * This routine releases @psb scsi buffer by adding it to tail of @phba
1088  * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1089  * and cannot be reused for at least RA_TOV amount of time if it was
1090  * aborted.
1091  **/
1092 static void
1093 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1094 {
1095 	unsigned long iflag = 0;
1096 
1097 	if (psb->exch_busy) {
1098 		spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1099 					iflag);
1100 		psb->pCmd = NULL;
1101 		list_add_tail(&psb->list,
1102 			&phba->sli4_hba.lpfc_abts_scsi_buf_list);
1103 		spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1104 					iflag);
1105 	} else {
1106 
1107 		spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1108 		psb->pCmd = NULL;
1109 		list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
1110 		spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
1111 	}
1112 }
1113 
1114 /**
1115  * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1116  * @phba: The Hba for which this call is being executed.
1117  * @psb: The scsi buffer which is being released.
1118  *
1119  * This routine releases @psb scsi buffer by adding it to tail of @phba
1120  * lpfc_scsi_buf_list list.
1121  **/
1122 static void
1123 lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1124 {
1125 
1126 	phba->lpfc_release_scsi_buf(phba, psb);
1127 }
1128 
1129 /**
1130  * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
1131  * @phba: The Hba for which this call is being executed.
1132  * @lpfc_cmd: The scsi buffer which is going to be mapped.
1133  *
1134  * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
1135  * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1136  * through sg elements and format the bdea. This routine also initializes all
1137  * IOCB fields which are dependent on scsi command request buffer.
1138  *
1139  * Return codes:
1140  *   1 - Error
1141  *   0 - Success
1142  **/
1143 static int
1144 lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1145 {
1146 	struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1147 	struct scatterlist *sgel = NULL;
1148 	struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1149 	struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1150 	struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
1151 	IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1152 	struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
1153 	dma_addr_t physaddr;
1154 	uint32_t num_bde = 0;
1155 	int nseg, datadir = scsi_cmnd->sc_data_direction;
1156 
1157 	/*
1158 	 * There are three possibilities here - use scatter-gather segment, use
1159 	 * the single mapping, or neither.  Start the lpfc command prep by
1160 	 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1161 	 * data bde entry.
1162 	 */
1163 	bpl += 2;
1164 	if (scsi_sg_count(scsi_cmnd)) {
1165 		/*
1166 		 * The driver stores the segment count returned from pci_map_sg
1167 		 * because this a count of dma-mappings used to map the use_sg
1168 		 * pages.  They are not guaranteed to be the same for those
1169 		 * architectures that implement an IOMMU.
1170 		 */
1171 
1172 		nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1173 				  scsi_sg_count(scsi_cmnd), datadir);
1174 		if (unlikely(!nseg))
1175 			return 1;
1176 
1177 		lpfc_cmd->seg_cnt = nseg;
1178 		if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
1179 			lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1180 				"9064 BLKGRD: %s: Too many sg segments from "
1181 			       "dma_map_sg.  Config %d, seg_cnt %d\n",
1182 			       __func__, phba->cfg_sg_seg_cnt,
1183 			       lpfc_cmd->seg_cnt);
1184 			scsi_dma_unmap(scsi_cmnd);
1185 			return 1;
1186 		}
1187 
1188 		/*
1189 		 * The driver established a maximum scatter-gather segment count
1190 		 * during probe that limits the number of sg elements in any
1191 		 * single scsi command.  Just run through the seg_cnt and format
1192 		 * the bde's.
1193 		 * When using SLI-3 the driver will try to fit all the BDEs into
1194 		 * the IOCB. If it can't then the BDEs get added to a BPL as it
1195 		 * does for SLI-2 mode.
1196 		 */
1197 		scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
1198 			physaddr = sg_dma_address(sgel);
1199 			if (phba->sli_rev == 3 &&
1200 			    !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1201 			    !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
1202 			    nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1203 				data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1204 				data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1205 				data_bde->addrLow = putPaddrLow(physaddr);
1206 				data_bde->addrHigh = putPaddrHigh(physaddr);
1207 				data_bde++;
1208 			} else {
1209 				bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1210 				bpl->tus.f.bdeSize = sg_dma_len(sgel);
1211 				bpl->tus.w = le32_to_cpu(bpl->tus.w);
1212 				bpl->addrLow =
1213 					le32_to_cpu(putPaddrLow(physaddr));
1214 				bpl->addrHigh =
1215 					le32_to_cpu(putPaddrHigh(physaddr));
1216 				bpl++;
1217 			}
1218 		}
1219 	}
1220 
1221 	/*
1222 	 * Finish initializing those IOCB fields that are dependent on the
1223 	 * scsi_cmnd request_buffer.  Note that for SLI-2 the bdeSize is
1224 	 * explicitly reinitialized and for SLI-3 the extended bde count is
1225 	 * explicitly reinitialized since all iocb memory resources are reused.
1226 	 */
1227 	if (phba->sli_rev == 3 &&
1228 	    !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1229 	    !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
1230 		if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1231 			/*
1232 			 * The extended IOCB format can only fit 3 BDE or a BPL.
1233 			 * This I/O has more than 3 BDE so the 1st data bde will
1234 			 * be a BPL that is filled in here.
1235 			 */
1236 			physaddr = lpfc_cmd->dma_handle;
1237 			data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1238 			data_bde->tus.f.bdeSize = (num_bde *
1239 						   sizeof(struct ulp_bde64));
1240 			physaddr += (sizeof(struct fcp_cmnd) +
1241 				     sizeof(struct fcp_rsp) +
1242 				     (2 * sizeof(struct ulp_bde64)));
1243 			data_bde->addrHigh = putPaddrHigh(physaddr);
1244 			data_bde->addrLow = putPaddrLow(physaddr);
1245 			/* ebde count includes the response bde and data bpl */
1246 			iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1247 		} else {
1248 			/* ebde count includes the response bde and data bdes */
1249 			iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1250 		}
1251 	} else {
1252 		iocb_cmd->un.fcpi64.bdl.bdeSize =
1253 			((num_bde + 2) * sizeof(struct ulp_bde64));
1254 		iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1255 	}
1256 	fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
1257 
1258 	/*
1259 	 * Due to difference in data length between DIF/non-DIF paths,
1260 	 * we need to set word 4 of IOCB here
1261 	 */
1262 	iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
1263 	return 0;
1264 }
1265 
1266 /*
1267  * Given a scsi cmnd, determine the BlockGuard opcodes to be used with it
1268  * @sc: The SCSI command to examine
1269  * @txopt: (out) BlockGuard operation for transmitted data
1270  * @rxopt: (out) BlockGuard operation for received data
1271  *
1272  * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1273  *
1274  */
1275 static int
1276 lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1277 		uint8_t *txop, uint8_t *rxop)
1278 {
1279 	uint8_t guard_type = scsi_host_get_guard(sc->device->host);
1280 	uint8_t ret = 0;
1281 
1282 	if (guard_type == SHOST_DIX_GUARD_IP) {
1283 		switch (scsi_get_prot_op(sc)) {
1284 		case SCSI_PROT_READ_INSERT:
1285 		case SCSI_PROT_WRITE_STRIP:
1286 			*txop = BG_OP_IN_CSUM_OUT_NODIF;
1287 			*rxop = BG_OP_IN_NODIF_OUT_CSUM;
1288 			break;
1289 
1290 		case SCSI_PROT_READ_STRIP:
1291 		case SCSI_PROT_WRITE_INSERT:
1292 			*txop = BG_OP_IN_NODIF_OUT_CRC;
1293 			*rxop = BG_OP_IN_CRC_OUT_NODIF;
1294 			break;
1295 
1296 		case SCSI_PROT_READ_PASS:
1297 		case SCSI_PROT_WRITE_PASS:
1298 			*txop = BG_OP_IN_CSUM_OUT_CRC;
1299 			*rxop = BG_OP_IN_CRC_OUT_CSUM;
1300 			break;
1301 
1302 		case SCSI_PROT_NORMAL:
1303 		default:
1304 			lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1305 				"9063 BLKGRD: Bad op/guard:%d/%d combination\n",
1306 					scsi_get_prot_op(sc), guard_type);
1307 			ret = 1;
1308 			break;
1309 
1310 		}
1311 	} else if (guard_type == SHOST_DIX_GUARD_CRC) {
1312 		switch (scsi_get_prot_op(sc)) {
1313 		case SCSI_PROT_READ_STRIP:
1314 		case SCSI_PROT_WRITE_INSERT:
1315 			*txop = BG_OP_IN_NODIF_OUT_CRC;
1316 			*rxop = BG_OP_IN_CRC_OUT_NODIF;
1317 			break;
1318 
1319 		case SCSI_PROT_READ_PASS:
1320 		case SCSI_PROT_WRITE_PASS:
1321 			*txop = BG_OP_IN_CRC_OUT_CRC;
1322 			*rxop = BG_OP_IN_CRC_OUT_CRC;
1323 			break;
1324 
1325 		case SCSI_PROT_READ_INSERT:
1326 		case SCSI_PROT_WRITE_STRIP:
1327 		case SCSI_PROT_NORMAL:
1328 		default:
1329 			lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1330 				"9075 BLKGRD: Bad op/guard:%d/%d combination\n",
1331 					scsi_get_prot_op(sc), guard_type);
1332 			ret = 1;
1333 			break;
1334 		}
1335 	} else {
1336 		/* unsupported format */
1337 		BUG();
1338 	}
1339 
1340 	return ret;
1341 }
1342 
1343 struct scsi_dif_tuple {
1344 	__be16 guard_tag;       /* Checksum */
1345 	__be16 app_tag;         /* Opaque storage */
1346 	__be32 ref_tag;         /* Target LBA or indirect LBA */
1347 };
1348 
1349 static inline unsigned
1350 lpfc_cmd_blksize(struct scsi_cmnd *sc)
1351 {
1352 	return sc->device->sector_size;
1353 }
1354 
1355 /**
1356  * lpfc_get_cmd_dif_parms - Extract DIF parameters from SCSI command
1357  * @sc:             in: SCSI command
1358  * @apptagmask:     out: app tag mask
1359  * @apptagval:      out: app tag value
1360  * @reftag:         out: ref tag (reference tag)
1361  *
1362  * Description:
1363  *   Extract DIF parameters from the command if possible.  Otherwise,
1364  *   use default parameters.
1365  *
1366  **/
1367 static inline void
1368 lpfc_get_cmd_dif_parms(struct scsi_cmnd *sc, uint16_t *apptagmask,
1369 		uint16_t *apptagval, uint32_t *reftag)
1370 {
1371 	struct  scsi_dif_tuple *spt;
1372 	unsigned char op = scsi_get_prot_op(sc);
1373 	unsigned int protcnt = scsi_prot_sg_count(sc);
1374 	static int cnt;
1375 
1376 	if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
1377 				op == SCSI_PROT_WRITE_PASS)) {
1378 
1379 		cnt++;
1380 		spt = page_address(sg_page(scsi_prot_sglist(sc))) +
1381 			scsi_prot_sglist(sc)[0].offset;
1382 		*apptagmask = 0;
1383 		*apptagval = 0;
1384 		*reftag = cpu_to_be32(spt->ref_tag);
1385 
1386 	} else {
1387 		/* SBC defines ref tag to be lower 32bits of LBA */
1388 		*reftag = (uint32_t) (0xffffffff & scsi_get_lba(sc));
1389 		*apptagmask = 0;
1390 		*apptagval = 0;
1391 	}
1392 }
1393 
1394 /*
1395  * This function sets up buffer list for protection groups of
1396  * type LPFC_PG_TYPE_NO_DIF
1397  *
1398  * This is usually used when the HBA is instructed to generate
1399  * DIFs and insert them into data stream (or strip DIF from
1400  * incoming data stream)
1401  *
1402  * The buffer list consists of just one protection group described
1403  * below:
1404  *                                +-------------------------+
1405  *   start of prot group  -->     |          PDE_5          |
1406  *                                +-------------------------+
1407  *                                |          PDE_6          |
1408  *                                +-------------------------+
1409  *                                |         Data BDE        |
1410  *                                +-------------------------+
1411  *                                |more Data BDE's ... (opt)|
1412  *                                +-------------------------+
1413  *
1414  * @sc: pointer to scsi command we're working on
1415  * @bpl: pointer to buffer list for protection groups
1416  * @datacnt: number of segments of data that have been dma mapped
1417  *
1418  * Note: Data s/g buffers have been dma mapped
1419  */
1420 static int
1421 lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1422 		struct ulp_bde64 *bpl, int datasegcnt)
1423 {
1424 	struct scatterlist *sgde = NULL; /* s/g data entry */
1425 	struct lpfc_pde5 *pde5 = NULL;
1426 	struct lpfc_pde6 *pde6 = NULL;
1427 	dma_addr_t physaddr;
1428 	int i = 0, num_bde = 0, status;
1429 	int datadir = sc->sc_data_direction;
1430 	unsigned blksize;
1431 	uint32_t reftag;
1432 	uint16_t apptagmask, apptagval;
1433 	uint8_t txop, rxop;
1434 
1435 	status  = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1436 	if (status)
1437 		goto out;
1438 
1439 	/* extract some info from the scsi command for pde*/
1440 	blksize = lpfc_cmd_blksize(sc);
1441 	lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1442 
1443 	/* setup PDE5 with what we have */
1444 	pde5 = (struct lpfc_pde5 *) bpl;
1445 	memset(pde5, 0, sizeof(struct lpfc_pde5));
1446 	bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
1447 	pde5->reftag = reftag;
1448 
1449 	/* Endianness conversion if necessary for PDE5 */
1450 	pde5->word0 = cpu_to_le32(pde5->word0);
1451 	pde5->reftag = cpu_to_le32(pde5->reftag);
1452 
1453 	/* advance bpl and increment bde count */
1454 	num_bde++;
1455 	bpl++;
1456 	pde6 = (struct lpfc_pde6 *) bpl;
1457 
1458 	/* setup PDE6 with the rest of the info */
1459 	memset(pde6, 0, sizeof(struct lpfc_pde6));
1460 	bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1461 	bf_set(pde6_optx, pde6, txop);
1462 	bf_set(pde6_oprx, pde6, rxop);
1463 	if (datadir == DMA_FROM_DEVICE) {
1464 		bf_set(pde6_ce, pde6, 1);
1465 		bf_set(pde6_re, pde6, 1);
1466 		bf_set(pde6_ae, pde6, 1);
1467 	}
1468 	bf_set(pde6_ai, pde6, 1);
1469 	bf_set(pde6_apptagval, pde6, apptagval);
1470 
1471 	/* Endianness conversion if necessary for PDE6 */
1472 	pde6->word0 = cpu_to_le32(pde6->word0);
1473 	pde6->word1 = cpu_to_le32(pde6->word1);
1474 	pde6->word2 = cpu_to_le32(pde6->word2);
1475 
1476 	/* advance bpl and increment bde count */
1477 	num_bde++;
1478 	bpl++;
1479 
1480 	/* assumption: caller has already run dma_map_sg on command data */
1481 	scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1482 		physaddr = sg_dma_address(sgde);
1483 		bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1484 		bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1485 		bpl->tus.f.bdeSize = sg_dma_len(sgde);
1486 		if (datadir == DMA_TO_DEVICE)
1487 			bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1488 		else
1489 			bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1490 		bpl->tus.w = le32_to_cpu(bpl->tus.w);
1491 		bpl++;
1492 		num_bde++;
1493 	}
1494 
1495 out:
1496 	return num_bde;
1497 }
1498 
1499 /*
1500  * This function sets up buffer list for protection groups of
1501  * type LPFC_PG_TYPE_DIF_BUF
1502  *
1503  * This is usually used when DIFs are in their own buffers,
1504  * separate from the data. The HBA can then by instructed
1505  * to place the DIFs in the outgoing stream.  For read operations,
1506  * The HBA could extract the DIFs and place it in DIF buffers.
1507  *
1508  * The buffer list for this type consists of one or more of the
1509  * protection groups described below:
1510  *                                    +-------------------------+
1511  *   start of first prot group  -->   |          PDE_5          |
1512  *                                    +-------------------------+
1513  *                                    |          PDE_6          |
1514  *                                    +-------------------------+
1515  *                                    |      PDE_7 (Prot BDE)   |
1516  *                                    +-------------------------+
1517  *                                    |        Data BDE         |
1518  *                                    +-------------------------+
1519  *                                    |more Data BDE's ... (opt)|
1520  *                                    +-------------------------+
1521  *   start of new  prot group  -->    |          PDE_5          |
1522  *                                    +-------------------------+
1523  *                                    |          ...            |
1524  *                                    +-------------------------+
1525  *
1526  * @sc: pointer to scsi command we're working on
1527  * @bpl: pointer to buffer list for protection groups
1528  * @datacnt: number of segments of data that have been dma mapped
1529  * @protcnt: number of segment of protection data that have been dma mapped
1530  *
1531  * Note: It is assumed that both data and protection s/g buffers have been
1532  *       mapped for DMA
1533  */
1534 static int
1535 lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1536 		struct ulp_bde64 *bpl, int datacnt, int protcnt)
1537 {
1538 	struct scatterlist *sgde = NULL; /* s/g data entry */
1539 	struct scatterlist *sgpe = NULL; /* s/g prot entry */
1540 	struct lpfc_pde5 *pde5 = NULL;
1541 	struct lpfc_pde6 *pde6 = NULL;
1542 	struct lpfc_pde7 *pde7 = NULL;
1543 	dma_addr_t dataphysaddr, protphysaddr;
1544 	unsigned short curr_data = 0, curr_prot = 0;
1545 	unsigned int split_offset;
1546 	unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
1547 	unsigned int protgrp_blks, protgrp_bytes;
1548 	unsigned int remainder, subtotal;
1549 	int status;
1550 	int datadir = sc->sc_data_direction;
1551 	unsigned char pgdone = 0, alldone = 0;
1552 	unsigned blksize;
1553 	uint32_t reftag;
1554 	uint16_t apptagmask, apptagval;
1555 	uint8_t txop, rxop;
1556 	int num_bde = 0;
1557 
1558 	sgpe = scsi_prot_sglist(sc);
1559 	sgde = scsi_sglist(sc);
1560 
1561 	if (!sgpe || !sgde) {
1562 		lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1563 				"9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
1564 				sgpe, sgde);
1565 		return 0;
1566 	}
1567 
1568 	status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1569 	if (status)
1570 		goto out;
1571 
1572 	/* extract some info from the scsi command */
1573 	blksize = lpfc_cmd_blksize(sc);
1574 	lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1575 
1576 	split_offset = 0;
1577 	do {
1578 		/* setup PDE5 with what we have */
1579 		pde5 = (struct lpfc_pde5 *) bpl;
1580 		memset(pde5, 0, sizeof(struct lpfc_pde5));
1581 		bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
1582 		pde5->reftag = reftag;
1583 
1584 		/* Endianness conversion if necessary for PDE5 */
1585 		pde5->word0 = cpu_to_le32(pde5->word0);
1586 		pde5->reftag = cpu_to_le32(pde5->reftag);
1587 
1588 		/* advance bpl and increment bde count */
1589 		num_bde++;
1590 		bpl++;
1591 		pde6 = (struct lpfc_pde6 *) bpl;
1592 
1593 		/* setup PDE6 with the rest of the info */
1594 		memset(pde6, 0, sizeof(struct lpfc_pde6));
1595 		bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1596 		bf_set(pde6_optx, pde6, txop);
1597 		bf_set(pde6_oprx, pde6, rxop);
1598 		bf_set(pde6_ce, pde6, 1);
1599 		bf_set(pde6_re, pde6, 1);
1600 		bf_set(pde6_ae, pde6, 1);
1601 		bf_set(pde6_ai, pde6, 1);
1602 		bf_set(pde6_apptagval, pde6, apptagval);
1603 
1604 		/* Endianness conversion if necessary for PDE6 */
1605 		pde6->word0 = cpu_to_le32(pde6->word0);
1606 		pde6->word1 = cpu_to_le32(pde6->word1);
1607 		pde6->word2 = cpu_to_le32(pde6->word2);
1608 
1609 		/* advance bpl and increment bde count */
1610 		num_bde++;
1611 		bpl++;
1612 
1613 		/* setup the first BDE that points to protection buffer */
1614 		protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
1615 		protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
1616 
1617 		/* must be integer multiple of the DIF block length */
1618 		BUG_ON(protgroup_len % 8);
1619 
1620 		pde7 = (struct lpfc_pde7 *) bpl;
1621 		memset(pde7, 0, sizeof(struct lpfc_pde7));
1622 		bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
1623 
1624 		pde7->addrHigh = le32_to_cpu(putPaddrLow(protphysaddr));
1625 		pde7->addrLow = le32_to_cpu(putPaddrHigh(protphysaddr));
1626 
1627 		protgrp_blks = protgroup_len / 8;
1628 		protgrp_bytes = protgrp_blks * blksize;
1629 
1630 		/* check if this pde is crossing the 4K boundary; if so split */
1631 		if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
1632 			protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
1633 			protgroup_offset += protgroup_remainder;
1634 			protgrp_blks = protgroup_remainder / 8;
1635 			protgrp_bytes = protgroup_remainder * blksize;
1636 		} else {
1637 			protgroup_offset = 0;
1638 			curr_prot++;
1639 		}
1640 
1641 		num_bde++;
1642 
1643 		/* setup BDE's for data blocks associated with DIF data */
1644 		pgdone = 0;
1645 		subtotal = 0; /* total bytes processed for current prot grp */
1646 		while (!pgdone) {
1647 			if (!sgde) {
1648 				lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1649 					"9065 BLKGRD:%s Invalid data segment\n",
1650 						__func__);
1651 				return 0;
1652 			}
1653 			bpl++;
1654 			dataphysaddr = sg_dma_address(sgde) + split_offset;
1655 			bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1656 			bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1657 
1658 			remainder = sg_dma_len(sgde) - split_offset;
1659 
1660 			if ((subtotal + remainder) <= protgrp_bytes) {
1661 				/* we can use this whole buffer */
1662 				bpl->tus.f.bdeSize = remainder;
1663 				split_offset = 0;
1664 
1665 				if ((subtotal + remainder) == protgrp_bytes)
1666 					pgdone = 1;
1667 			} else {
1668 				/* must split this buffer with next prot grp */
1669 				bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1670 				split_offset += bpl->tus.f.bdeSize;
1671 			}
1672 
1673 			subtotal += bpl->tus.f.bdeSize;
1674 
1675 			if (datadir == DMA_TO_DEVICE)
1676 				bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1677 			else
1678 				bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1679 			bpl->tus.w = le32_to_cpu(bpl->tus.w);
1680 
1681 			num_bde++;
1682 			curr_data++;
1683 
1684 			if (split_offset)
1685 				break;
1686 
1687 			/* Move to the next s/g segment if possible */
1688 			sgde = sg_next(sgde);
1689 
1690 		}
1691 
1692 		if (protgroup_offset) {
1693 			/* update the reference tag */
1694 			reftag += protgrp_blks;
1695 			bpl++;
1696 			continue;
1697 		}
1698 
1699 		/* are we done ? */
1700 		if (curr_prot == protcnt) {
1701 			alldone = 1;
1702 		} else if (curr_prot < protcnt) {
1703 			/* advance to next prot buffer */
1704 			sgpe = sg_next(sgpe);
1705 			bpl++;
1706 
1707 			/* update the reference tag */
1708 			reftag += protgrp_blks;
1709 		} else {
1710 			/* if we're here, we have a bug */
1711 			lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1712 				"9054 BLKGRD: bug in %s\n", __func__);
1713 		}
1714 
1715 	} while (!alldone);
1716 
1717 out:
1718 
1719 	return num_bde;
1720 }
1721 
1722 /*
1723  * Given a SCSI command that supports DIF, determine composition of protection
1724  * groups involved in setting up buffer lists
1725  *
1726  * Returns:
1727  *			      for DIF (for both read and write)
1728  * */
1729 static int
1730 lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
1731 {
1732 	int ret = LPFC_PG_TYPE_INVALID;
1733 	unsigned char op = scsi_get_prot_op(sc);
1734 
1735 	switch (op) {
1736 	case SCSI_PROT_READ_STRIP:
1737 	case SCSI_PROT_WRITE_INSERT:
1738 		ret = LPFC_PG_TYPE_NO_DIF;
1739 		break;
1740 	case SCSI_PROT_READ_INSERT:
1741 	case SCSI_PROT_WRITE_STRIP:
1742 	case SCSI_PROT_READ_PASS:
1743 	case SCSI_PROT_WRITE_PASS:
1744 		ret = LPFC_PG_TYPE_DIF_BUF;
1745 		break;
1746 	default:
1747 		lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1748 				"9021 Unsupported protection op:%d\n", op);
1749 		break;
1750 	}
1751 
1752 	return ret;
1753 }
1754 
1755 /*
1756  * This is the protection/DIF aware version of
1757  * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
1758  * two functions eventually, but for now, it's here
1759  */
1760 static int
1761 lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba,
1762 		struct lpfc_scsi_buf *lpfc_cmd)
1763 {
1764 	struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1765 	struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1766 	struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1767 	IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1768 	uint32_t num_bde = 0;
1769 	int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
1770 	int prot_group_type = 0;
1771 	int diflen, fcpdl;
1772 	unsigned blksize;
1773 
1774 	/*
1775 	 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
1776 	 *  fcp_rsp regions to the first data bde entry
1777 	 */
1778 	bpl += 2;
1779 	if (scsi_sg_count(scsi_cmnd)) {
1780 		/*
1781 		 * The driver stores the segment count returned from pci_map_sg
1782 		 * because this a count of dma-mappings used to map the use_sg
1783 		 * pages.  They are not guaranteed to be the same for those
1784 		 * architectures that implement an IOMMU.
1785 		 */
1786 		datasegcnt = dma_map_sg(&phba->pcidev->dev,
1787 					scsi_sglist(scsi_cmnd),
1788 					scsi_sg_count(scsi_cmnd), datadir);
1789 		if (unlikely(!datasegcnt))
1790 			return 1;
1791 
1792 		lpfc_cmd->seg_cnt = datasegcnt;
1793 		if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
1794 			lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1795 					"9067 BLKGRD: %s: Too many sg segments"
1796 					" from dma_map_sg.  Config %d, seg_cnt"
1797 					" %d\n",
1798 					__func__, phba->cfg_sg_seg_cnt,
1799 					lpfc_cmd->seg_cnt);
1800 			scsi_dma_unmap(scsi_cmnd);
1801 			return 1;
1802 		}
1803 
1804 		prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
1805 
1806 		switch (prot_group_type) {
1807 		case LPFC_PG_TYPE_NO_DIF:
1808 			num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
1809 					datasegcnt);
1810 			/* we should have 2 or more entries in buffer list */
1811 			if (num_bde < 2)
1812 				goto err;
1813 			break;
1814 		case LPFC_PG_TYPE_DIF_BUF:{
1815 			/*
1816 			 * This type indicates that protection buffers are
1817 			 * passed to the driver, so that needs to be prepared
1818 			 * for DMA
1819 			 */
1820 			protsegcnt = dma_map_sg(&phba->pcidev->dev,
1821 					scsi_prot_sglist(scsi_cmnd),
1822 					scsi_prot_sg_count(scsi_cmnd), datadir);
1823 			if (unlikely(!protsegcnt)) {
1824 				scsi_dma_unmap(scsi_cmnd);
1825 				return 1;
1826 			}
1827 
1828 			lpfc_cmd->prot_seg_cnt = protsegcnt;
1829 			if (lpfc_cmd->prot_seg_cnt
1830 			    > phba->cfg_prot_sg_seg_cnt) {
1831 				lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1832 					"9068 BLKGRD: %s: Too many prot sg "
1833 					"segments from dma_map_sg.  Config %d,"
1834 						"prot_seg_cnt %d\n", __func__,
1835 						phba->cfg_prot_sg_seg_cnt,
1836 						lpfc_cmd->prot_seg_cnt);
1837 				dma_unmap_sg(&phba->pcidev->dev,
1838 					     scsi_prot_sglist(scsi_cmnd),
1839 					     scsi_prot_sg_count(scsi_cmnd),
1840 					     datadir);
1841 				scsi_dma_unmap(scsi_cmnd);
1842 				return 1;
1843 			}
1844 
1845 			num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
1846 					datasegcnt, protsegcnt);
1847 			/* we should have 3 or more entries in buffer list */
1848 			if (num_bde < 3)
1849 				goto err;
1850 			break;
1851 		}
1852 		case LPFC_PG_TYPE_INVALID:
1853 		default:
1854 			lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1855 					"9022 Unexpected protection group %i\n",
1856 					prot_group_type);
1857 			return 1;
1858 		}
1859 	}
1860 
1861 	/*
1862 	 * Finish initializing those IOCB fields that are dependent on the
1863 	 * scsi_cmnd request_buffer.  Note that the bdeSize is explicitly
1864 	 * reinitialized since all iocb memory resources are used many times
1865 	 * for transmit, receive, and continuation bpl's.
1866 	 */
1867 	iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
1868 	iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
1869 	iocb_cmd->ulpBdeCount = 1;
1870 	iocb_cmd->ulpLe = 1;
1871 
1872 	fcpdl = scsi_bufflen(scsi_cmnd);
1873 
1874 	if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
1875 		/*
1876 		 * We are in DIF Type 1 mode
1877 		 * Every data block has a 8 byte DIF (trailer)
1878 		 * attached to it.  Must ajust FCP data length
1879 		 */
1880 		blksize = lpfc_cmd_blksize(scsi_cmnd);
1881 		diflen = (fcpdl / blksize) * 8;
1882 		fcpdl += diflen;
1883 	}
1884 	fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
1885 
1886 	/*
1887 	 * Due to difference in data length between DIF/non-DIF paths,
1888 	 * we need to set word 4 of IOCB here
1889 	 */
1890 	iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
1891 
1892 	return 0;
1893 err:
1894 	lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1895 			"9023 Could not setup all needed BDE's"
1896 			"prot_group_type=%d, num_bde=%d\n",
1897 			prot_group_type, num_bde);
1898 	return 1;
1899 }
1900 
1901 /*
1902  * This function checks for BlockGuard errors detected by
1903  * the HBA.  In case of errors, the ASC/ASCQ fields in the
1904  * sense buffer will be set accordingly, paired with
1905  * ILLEGAL_REQUEST to signal to the kernel that the HBA
1906  * detected corruption.
1907  *
1908  * Returns:
1909  *  0 - No error found
1910  *  1 - BlockGuard error found
1911  * -1 - Internal error (bad profile, ...etc)
1912  */
1913 static int
1914 lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
1915 			struct lpfc_iocbq *pIocbOut)
1916 {
1917 	struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
1918 	struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
1919 	int ret = 0;
1920 	uint32_t bghm = bgf->bghm;
1921 	uint32_t bgstat = bgf->bgstat;
1922 	uint64_t failing_sector = 0;
1923 
1924 	lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
1925 			" 0x%x lba 0x%llx blk cnt 0x%x "
1926 			"bgstat=0x%x bghm=0x%x\n",
1927 			cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
1928 			blk_rq_sectors(cmd->request), bgstat, bghm);
1929 
1930 	spin_lock(&_dump_buf_lock);
1931 	if (!_dump_buf_done) {
1932 		lpfc_printf_log(phba, KERN_ERR, LOG_BG,  "9070 BLKGRD: Saving"
1933 			" Data for %u blocks to debugfs\n",
1934 				(cmd->cmnd[7] << 8 | cmd->cmnd[8]));
1935 		lpfc_debug_save_data(phba, cmd);
1936 
1937 		/* If we have a prot sgl, save the DIF buffer */
1938 		if (lpfc_prot_group_type(phba, cmd) ==
1939 				LPFC_PG_TYPE_DIF_BUF) {
1940 			lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
1941 				"Saving DIF for %u blocks to debugfs\n",
1942 				(cmd->cmnd[7] << 8 | cmd->cmnd[8]));
1943 			lpfc_debug_save_dif(phba, cmd);
1944 		}
1945 
1946 		_dump_buf_done = 1;
1947 	}
1948 	spin_unlock(&_dump_buf_lock);
1949 
1950 	if (lpfc_bgs_get_invalid_prof(bgstat)) {
1951 		cmd->result = ScsiResult(DID_ERROR, 0);
1952 		lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
1953 			" BlockGuard profile. bgstat:0x%x\n",
1954 			bgstat);
1955 		ret = (-1);
1956 		goto out;
1957 	}
1958 
1959 	if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
1960 		cmd->result = ScsiResult(DID_ERROR, 0);
1961 		lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
1962 				"Invalid BlockGuard DIF Block. bgstat:0x%x\n",
1963 				bgstat);
1964 		ret = (-1);
1965 		goto out;
1966 	}
1967 
1968 	if (lpfc_bgs_get_guard_err(bgstat)) {
1969 		ret = 1;
1970 
1971 		scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1972 				0x10, 0x1);
1973 		cmd->result = DRIVER_SENSE << 24
1974 			| ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1975 		phba->bg_guard_err_cnt++;
1976 		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1977 			"9055 BLKGRD: guard_tag error\n");
1978 	}
1979 
1980 	if (lpfc_bgs_get_reftag_err(bgstat)) {
1981 		ret = 1;
1982 
1983 		scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1984 				0x10, 0x3);
1985 		cmd->result = DRIVER_SENSE << 24
1986 			| ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1987 
1988 		phba->bg_reftag_err_cnt++;
1989 		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1990 			"9056 BLKGRD: ref_tag error\n");
1991 	}
1992 
1993 	if (lpfc_bgs_get_apptag_err(bgstat)) {
1994 		ret = 1;
1995 
1996 		scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1997 				0x10, 0x2);
1998 		cmd->result = DRIVER_SENSE << 24
1999 			| ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2000 
2001 		phba->bg_apptag_err_cnt++;
2002 		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2003 			"9061 BLKGRD: app_tag error\n");
2004 	}
2005 
2006 	if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
2007 		/*
2008 		 * setup sense data descriptor 0 per SPC-4 as an information
2009 		 * field, and put the failing LBA in it
2010 		 */
2011 		cmd->sense_buffer[8] = 0;     /* Information */
2012 		cmd->sense_buffer[9] = 0xa;   /* Add. length */
2013 		bghm /= cmd->device->sector_size;
2014 
2015 		failing_sector = scsi_get_lba(cmd);
2016 		failing_sector += bghm;
2017 
2018 		put_unaligned_be64(failing_sector, &cmd->sense_buffer[10]);
2019 	}
2020 
2021 	if (!ret) {
2022 		/* No error was reported - problem in FW? */
2023 		cmd->result = ScsiResult(DID_ERROR, 0);
2024 		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2025 			"9057 BLKGRD: no errors reported!\n");
2026 	}
2027 
2028 out:
2029 	return ret;
2030 }
2031 
2032 /**
2033  * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
2034  * @phba: The Hba for which this call is being executed.
2035  * @lpfc_cmd: The scsi buffer which is going to be mapped.
2036  *
2037  * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
2038  * field of @lpfc_cmd for device with SLI-4 interface spec.
2039  *
2040  * Return codes:
2041  *	1 - Error
2042  *	0 - Success
2043  **/
2044 static int
2045 lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2046 {
2047 	struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2048 	struct scatterlist *sgel = NULL;
2049 	struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2050 	struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
2051 	struct sli4_sge *first_data_sgl;
2052 	IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2053 	dma_addr_t physaddr;
2054 	uint32_t num_bde = 0;
2055 	uint32_t dma_len;
2056 	uint32_t dma_offset = 0;
2057 	int nseg;
2058 	struct ulp_bde64 *bde;
2059 
2060 	/*
2061 	 * There are three possibilities here - use scatter-gather segment, use
2062 	 * the single mapping, or neither.  Start the lpfc command prep by
2063 	 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2064 	 * data bde entry.
2065 	 */
2066 	if (scsi_sg_count(scsi_cmnd)) {
2067 		/*
2068 		 * The driver stores the segment count returned from pci_map_sg
2069 		 * because this a count of dma-mappings used to map the use_sg
2070 		 * pages.  They are not guaranteed to be the same for those
2071 		 * architectures that implement an IOMMU.
2072 		 */
2073 
2074 		nseg = scsi_dma_map(scsi_cmnd);
2075 		if (unlikely(!nseg))
2076 			return 1;
2077 		sgl += 1;
2078 		/* clear the last flag in the fcp_rsp map entry */
2079 		sgl->word2 = le32_to_cpu(sgl->word2);
2080 		bf_set(lpfc_sli4_sge_last, sgl, 0);
2081 		sgl->word2 = cpu_to_le32(sgl->word2);
2082 		sgl += 1;
2083 		first_data_sgl = sgl;
2084 		lpfc_cmd->seg_cnt = nseg;
2085 		if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
2086 			lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
2087 				" %s: Too many sg segments from "
2088 				"dma_map_sg.  Config %d, seg_cnt %d\n",
2089 				__func__, phba->cfg_sg_seg_cnt,
2090 			       lpfc_cmd->seg_cnt);
2091 			scsi_dma_unmap(scsi_cmnd);
2092 			return 1;
2093 		}
2094 
2095 		/*
2096 		 * The driver established a maximum scatter-gather segment count
2097 		 * during probe that limits the number of sg elements in any
2098 		 * single scsi command.  Just run through the seg_cnt and format
2099 		 * the sge's.
2100 		 * When using SLI-3 the driver will try to fit all the BDEs into
2101 		 * the IOCB. If it can't then the BDEs get added to a BPL as it
2102 		 * does for SLI-2 mode.
2103 		 */
2104 		scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
2105 			physaddr = sg_dma_address(sgel);
2106 			dma_len = sg_dma_len(sgel);
2107 			sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2108 			sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2109 			sgl->word2 = le32_to_cpu(sgl->word2);
2110 			if ((num_bde + 1) == nseg)
2111 				bf_set(lpfc_sli4_sge_last, sgl, 1);
2112 			else
2113 				bf_set(lpfc_sli4_sge_last, sgl, 0);
2114 			bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2115 			sgl->word2 = cpu_to_le32(sgl->word2);
2116 			sgl->sge_len = cpu_to_le32(dma_len);
2117 			dma_offset += dma_len;
2118 			sgl++;
2119 		}
2120 		/* setup the performance hint (first data BDE) if enabled */
2121 		if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
2122 			bde = (struct ulp_bde64 *)
2123 					&(iocb_cmd->unsli3.sli3Words[5]);
2124 			bde->addrLow = first_data_sgl->addr_lo;
2125 			bde->addrHigh = first_data_sgl->addr_hi;
2126 			bde->tus.f.bdeSize =
2127 					le32_to_cpu(first_data_sgl->sge_len);
2128 			bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2129 			bde->tus.w = cpu_to_le32(bde->tus.w);
2130 		}
2131 	} else {
2132 		sgl += 1;
2133 		/* clear the last flag in the fcp_rsp map entry */
2134 		sgl->word2 = le32_to_cpu(sgl->word2);
2135 		bf_set(lpfc_sli4_sge_last, sgl, 1);
2136 		sgl->word2 = cpu_to_le32(sgl->word2);
2137 	}
2138 
2139 	/*
2140 	 * Finish initializing those IOCB fields that are dependent on the
2141 	 * scsi_cmnd request_buffer.  Note that for SLI-2 the bdeSize is
2142 	 * explicitly reinitialized.
2143 	 * all iocb memory resources are reused.
2144 	 */
2145 	fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
2146 
2147 	/*
2148 	 * Due to difference in data length between DIF/non-DIF paths,
2149 	 * we need to set word 4 of IOCB here
2150 	 */
2151 	iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
2152 	return 0;
2153 }
2154 
2155 /**
2156  * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
2157  * @phba: The Hba for which this call is being executed.
2158  * @lpfc_cmd: The scsi buffer which is going to be mapped.
2159  *
2160  * This routine wraps the actual DMA mapping function pointer from the
2161  * lpfc_hba struct.
2162  *
2163  * Return codes:
2164  *	1 - Error
2165  *	0 - Success
2166  **/
2167 static inline int
2168 lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2169 {
2170 	return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
2171 }
2172 
2173 /**
2174  * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
2175  * @phba: Pointer to hba context object.
2176  * @vport: Pointer to vport object.
2177  * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
2178  * @rsp_iocb: Pointer to response iocb object which reported error.
2179  *
2180  * This function posts an event when there is a SCSI command reporting
2181  * error from the scsi device.
2182  **/
2183 static void
2184 lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
2185 		struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
2186 	struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2187 	struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
2188 	uint32_t resp_info = fcprsp->rspStatus2;
2189 	uint32_t scsi_status = fcprsp->rspStatus3;
2190 	uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
2191 	struct lpfc_fast_path_event *fast_path_evt = NULL;
2192 	struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
2193 	unsigned long flags;
2194 
2195 	if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2196 		return;
2197 
2198 	/* If there is queuefull or busy condition send a scsi event */
2199 	if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
2200 		(cmnd->result == SAM_STAT_BUSY)) {
2201 		fast_path_evt = lpfc_alloc_fast_evt(phba);
2202 		if (!fast_path_evt)
2203 			return;
2204 		fast_path_evt->un.scsi_evt.event_type =
2205 			FC_REG_SCSI_EVENT;
2206 		fast_path_evt->un.scsi_evt.subcategory =
2207 		(cmnd->result == SAM_STAT_TASK_SET_FULL) ?
2208 		LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
2209 		fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
2210 		memcpy(&fast_path_evt->un.scsi_evt.wwpn,
2211 			&pnode->nlp_portname, sizeof(struct lpfc_name));
2212 		memcpy(&fast_path_evt->un.scsi_evt.wwnn,
2213 			&pnode->nlp_nodename, sizeof(struct lpfc_name));
2214 	} else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
2215 		((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
2216 		fast_path_evt = lpfc_alloc_fast_evt(phba);
2217 		if (!fast_path_evt)
2218 			return;
2219 		fast_path_evt->un.check_cond_evt.scsi_event.event_type =
2220 			FC_REG_SCSI_EVENT;
2221 		fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
2222 			LPFC_EVENT_CHECK_COND;
2223 		fast_path_evt->un.check_cond_evt.scsi_event.lun =
2224 			cmnd->device->lun;
2225 		memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
2226 			&pnode->nlp_portname, sizeof(struct lpfc_name));
2227 		memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
2228 			&pnode->nlp_nodename, sizeof(struct lpfc_name));
2229 		fast_path_evt->un.check_cond_evt.sense_key =
2230 			cmnd->sense_buffer[2] & 0xf;
2231 		fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
2232 		fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
2233 	} else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2234 		     fcpi_parm &&
2235 		     ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
2236 			((scsi_status == SAM_STAT_GOOD) &&
2237 			!(resp_info & (RESID_UNDER | RESID_OVER))))) {
2238 		/*
2239 		 * If status is good or resid does not match with fcp_param and
2240 		 * there is valid fcpi_parm, then there is a read_check error
2241 		 */
2242 		fast_path_evt = lpfc_alloc_fast_evt(phba);
2243 		if (!fast_path_evt)
2244 			return;
2245 		fast_path_evt->un.read_check_error.header.event_type =
2246 			FC_REG_FABRIC_EVENT;
2247 		fast_path_evt->un.read_check_error.header.subcategory =
2248 			LPFC_EVENT_FCPRDCHKERR;
2249 		memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
2250 			&pnode->nlp_portname, sizeof(struct lpfc_name));
2251 		memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
2252 			&pnode->nlp_nodename, sizeof(struct lpfc_name));
2253 		fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
2254 		fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
2255 		fast_path_evt->un.read_check_error.fcpiparam =
2256 			fcpi_parm;
2257 	} else
2258 		return;
2259 
2260 	fast_path_evt->vport = vport;
2261 	spin_lock_irqsave(&phba->hbalock, flags);
2262 	list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
2263 	spin_unlock_irqrestore(&phba->hbalock, flags);
2264 	lpfc_worker_wake_up(phba);
2265 	return;
2266 }
2267 
2268 /**
2269  * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
2270  * @phba: The HBA for which this call is being executed.
2271  * @psb: The scsi buffer which is going to be un-mapped.
2272  *
2273  * This routine does DMA un-mapping of scatter gather list of scsi command
2274  * field of @lpfc_cmd for device with SLI-3 interface spec.
2275  **/
2276 static void
2277 lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
2278 {
2279 	/*
2280 	 * There are only two special cases to consider.  (1) the scsi command
2281 	 * requested scatter-gather usage or (2) the scsi command allocated
2282 	 * a request buffer, but did not request use_sg.  There is a third
2283 	 * case, but it does not require resource deallocation.
2284 	 */
2285 	if (psb->seg_cnt > 0)
2286 		scsi_dma_unmap(psb->pCmd);
2287 	if (psb->prot_seg_cnt > 0)
2288 		dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
2289 				scsi_prot_sg_count(psb->pCmd),
2290 				psb->pCmd->sc_data_direction);
2291 }
2292 
2293 /**
2294  * lpfc_handler_fcp_err - FCP response handler
2295  * @vport: The virtual port for which this call is being executed.
2296  * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2297  * @rsp_iocb: The response IOCB which contains FCP error.
2298  *
2299  * This routine is called to process response IOCB with status field
2300  * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
2301  * based upon SCSI and FCP error.
2302  **/
2303 static void
2304 lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2305 		    struct lpfc_iocbq *rsp_iocb)
2306 {
2307 	struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2308 	struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
2309 	struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
2310 	uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
2311 	uint32_t resp_info = fcprsp->rspStatus2;
2312 	uint32_t scsi_status = fcprsp->rspStatus3;
2313 	uint32_t *lp;
2314 	uint32_t host_status = DID_OK;
2315 	uint32_t rsplen = 0;
2316 	uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
2317 
2318 
2319 	/*
2320 	 *  If this is a task management command, there is no
2321 	 *  scsi packet associated with this lpfc_cmd.  The driver
2322 	 *  consumes it.
2323 	 */
2324 	if (fcpcmd->fcpCntl2) {
2325 		scsi_status = 0;
2326 		goto out;
2327 	}
2328 
2329 	if (resp_info & RSP_LEN_VALID) {
2330 		rsplen = be32_to_cpu(fcprsp->rspRspLen);
2331 		if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
2332 			lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2333 				 "2719 Invalid response length: "
2334 				 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
2335 				 cmnd->device->id,
2336 				 cmnd->device->lun, cmnd->cmnd[0],
2337 				 rsplen);
2338 			host_status = DID_ERROR;
2339 			goto out;
2340 		}
2341 		if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
2342 			lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2343 				 "2757 Protocol failure detected during "
2344 				 "processing of FCP I/O op: "
2345 				 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
2346 				 cmnd->device->id,
2347 				 cmnd->device->lun, cmnd->cmnd[0],
2348 				 fcprsp->rspInfo3);
2349 			host_status = DID_ERROR;
2350 			goto out;
2351 		}
2352 	}
2353 
2354 	if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
2355 		uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
2356 		if (snslen > SCSI_SENSE_BUFFERSIZE)
2357 			snslen = SCSI_SENSE_BUFFERSIZE;
2358 
2359 		if (resp_info & RSP_LEN_VALID)
2360 		  rsplen = be32_to_cpu(fcprsp->rspRspLen);
2361 		memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
2362 	}
2363 	lp = (uint32_t *)cmnd->sense_buffer;
2364 
2365 	if (!scsi_status && (resp_info & RESID_UNDER))
2366 		logit = LOG_FCP;
2367 
2368 	lpfc_printf_vlog(vport, KERN_WARNING, logit,
2369 			 "9024 FCP command x%x failed: x%x SNS x%x x%x "
2370 			 "Data: x%x x%x x%x x%x x%x\n",
2371 			 cmnd->cmnd[0], scsi_status,
2372 			 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
2373 			 be32_to_cpu(fcprsp->rspResId),
2374 			 be32_to_cpu(fcprsp->rspSnsLen),
2375 			 be32_to_cpu(fcprsp->rspRspLen),
2376 			 fcprsp->rspInfo3);
2377 
2378 	scsi_set_resid(cmnd, 0);
2379 	if (resp_info & RESID_UNDER) {
2380 		scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
2381 
2382 		lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2383 				 "9025 FCP Read Underrun, expected %d, "
2384 				 "residual %d Data: x%x x%x x%x\n",
2385 				 be32_to_cpu(fcpcmd->fcpDl),
2386 				 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
2387 				 cmnd->underflow);
2388 
2389 		/*
2390 		 * If there is an under run check if under run reported by
2391 		 * storage array is same as the under run reported by HBA.
2392 		 * If this is not same, there is a dropped frame.
2393 		 */
2394 		if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2395 			fcpi_parm &&
2396 			(scsi_get_resid(cmnd) != fcpi_parm)) {
2397 			lpfc_printf_vlog(vport, KERN_WARNING,
2398 					 LOG_FCP | LOG_FCP_ERROR,
2399 					 "9026 FCP Read Check Error "
2400 					 "and Underrun Data: x%x x%x x%x x%x\n",
2401 					 be32_to_cpu(fcpcmd->fcpDl),
2402 					 scsi_get_resid(cmnd), fcpi_parm,
2403 					 cmnd->cmnd[0]);
2404 			scsi_set_resid(cmnd, scsi_bufflen(cmnd));
2405 			host_status = DID_ERROR;
2406 		}
2407 		/*
2408 		 * The cmnd->underflow is the minimum number of bytes that must
2409 		 * be transferred for this command.  Provided a sense condition
2410 		 * is not present, make sure the actual amount transferred is at
2411 		 * least the underflow value or fail.
2412 		 */
2413 		if (!(resp_info & SNS_LEN_VALID) &&
2414 		    (scsi_status == SAM_STAT_GOOD) &&
2415 		    (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
2416 		     < cmnd->underflow)) {
2417 			lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2418 					 "9027 FCP command x%x residual "
2419 					 "underrun converted to error "
2420 					 "Data: x%x x%x x%x\n",
2421 					 cmnd->cmnd[0], scsi_bufflen(cmnd),
2422 					 scsi_get_resid(cmnd), cmnd->underflow);
2423 			host_status = DID_ERROR;
2424 		}
2425 	} else if (resp_info & RESID_OVER) {
2426 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2427 				 "9028 FCP command x%x residual overrun error. "
2428 				 "Data: x%x x%x\n", cmnd->cmnd[0],
2429 				 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
2430 		host_status = DID_ERROR;
2431 
2432 	/*
2433 	 * Check SLI validation that all the transfer was actually done
2434 	 * (fcpi_parm should be zero). Apply check only to reads.
2435 	 */
2436 	} else if (fcpi_parm && (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
2437 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
2438 				 "9029 FCP Read Check Error Data: "
2439 				 "x%x x%x x%x x%x x%x\n",
2440 				 be32_to_cpu(fcpcmd->fcpDl),
2441 				 be32_to_cpu(fcprsp->rspResId),
2442 				 fcpi_parm, cmnd->cmnd[0], scsi_status);
2443 		switch (scsi_status) {
2444 		case SAM_STAT_GOOD:
2445 		case SAM_STAT_CHECK_CONDITION:
2446 			/* Fabric dropped a data frame. Fail any successful
2447 			 * command in which we detected dropped frames.
2448 			 * A status of good or some check conditions could
2449 			 * be considered a successful command.
2450 			 */
2451 			host_status = DID_ERROR;
2452 			break;
2453 		}
2454 		scsi_set_resid(cmnd, scsi_bufflen(cmnd));
2455 	}
2456 
2457  out:
2458 	cmnd->result = ScsiResult(host_status, scsi_status);
2459 	lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
2460 }
2461 
2462 /**
2463  * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
2464  * @phba: The Hba for which this call is being executed.
2465  * @pIocbIn: The command IOCBQ for the scsi cmnd.
2466  * @pIocbOut: The response IOCBQ for the scsi cmnd.
2467  *
2468  * This routine assigns scsi command result by looking into response IOCB
2469  * status field appropriately. This routine handles QUEUE FULL condition as
2470  * well by ramping down device queue depth.
2471  **/
2472 static void
2473 lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
2474 			struct lpfc_iocbq *pIocbOut)
2475 {
2476 	struct lpfc_scsi_buf *lpfc_cmd =
2477 		(struct lpfc_scsi_buf *) pIocbIn->context1;
2478 	struct lpfc_vport      *vport = pIocbIn->vport;
2479 	struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
2480 	struct lpfc_nodelist *pnode = rdata->pnode;
2481 	struct scsi_cmnd *cmd;
2482 	int result;
2483 	struct scsi_device *tmp_sdev;
2484 	int depth;
2485 	unsigned long flags;
2486 	struct lpfc_fast_path_event *fast_path_evt;
2487 	struct Scsi_Host *shost;
2488 	uint32_t queue_depth, scsi_id;
2489 
2490 	/* Sanity check on return of outstanding command */
2491 	if (!(lpfc_cmd->pCmd))
2492 		return;
2493 	cmd = lpfc_cmd->pCmd;
2494 	shost = cmd->device->host;
2495 
2496 	lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
2497 	lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
2498 	/* pick up SLI4 exhange busy status from HBA */
2499 	lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
2500 
2501 	if (pnode && NLP_CHK_NODE_ACT(pnode))
2502 		atomic_dec(&pnode->cmd_pending);
2503 
2504 	if (lpfc_cmd->status) {
2505 		if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
2506 		    (lpfc_cmd->result & IOERR_DRVR_MASK))
2507 			lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
2508 		else if (lpfc_cmd->status >= IOSTAT_CNT)
2509 			lpfc_cmd->status = IOSTAT_DEFAULT;
2510 
2511 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2512 				 "9030 FCP cmd x%x failed <%d/%d> "
2513 				 "status: x%x result: x%x Data: x%x x%x\n",
2514 				 cmd->cmnd[0],
2515 				 cmd->device ? cmd->device->id : 0xffff,
2516 				 cmd->device ? cmd->device->lun : 0xffff,
2517 				 lpfc_cmd->status, lpfc_cmd->result,
2518 				 pIocbOut->iocb.ulpContext,
2519 				 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
2520 
2521 		switch (lpfc_cmd->status) {
2522 		case IOSTAT_FCP_RSP_ERROR:
2523 			/* Call FCP RSP handler to determine result */
2524 			lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
2525 			break;
2526 		case IOSTAT_NPORT_BSY:
2527 		case IOSTAT_FABRIC_BSY:
2528 			cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
2529 			fast_path_evt = lpfc_alloc_fast_evt(phba);
2530 			if (!fast_path_evt)
2531 				break;
2532 			fast_path_evt->un.fabric_evt.event_type =
2533 				FC_REG_FABRIC_EVENT;
2534 			fast_path_evt->un.fabric_evt.subcategory =
2535 				(lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
2536 				LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
2537 			if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2538 				memcpy(&fast_path_evt->un.fabric_evt.wwpn,
2539 					&pnode->nlp_portname,
2540 					sizeof(struct lpfc_name));
2541 				memcpy(&fast_path_evt->un.fabric_evt.wwnn,
2542 					&pnode->nlp_nodename,
2543 					sizeof(struct lpfc_name));
2544 			}
2545 			fast_path_evt->vport = vport;
2546 			fast_path_evt->work_evt.evt =
2547 				LPFC_EVT_FASTPATH_MGMT_EVT;
2548 			spin_lock_irqsave(&phba->hbalock, flags);
2549 			list_add_tail(&fast_path_evt->work_evt.evt_listp,
2550 				&phba->work_list);
2551 			spin_unlock_irqrestore(&phba->hbalock, flags);
2552 			lpfc_worker_wake_up(phba);
2553 			break;
2554 		case IOSTAT_LOCAL_REJECT:
2555 		case IOSTAT_REMOTE_STOP:
2556 			if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
2557 			    lpfc_cmd->result ==
2558 					IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
2559 			    lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
2560 			    lpfc_cmd->result ==
2561 					IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
2562 				cmd->result = ScsiResult(DID_NO_CONNECT, 0);
2563 				break;
2564 			}
2565 			if (lpfc_cmd->result == IOERR_INVALID_RPI ||
2566 			    lpfc_cmd->result == IOERR_NO_RESOURCES ||
2567 			    lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
2568 			    lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
2569 				cmd->result = ScsiResult(DID_REQUEUE, 0);
2570 				break;
2571 			}
2572 			if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
2573 			     lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
2574 			     pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
2575 				if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
2576 					/*
2577 					 * This is a response for a BG enabled
2578 					 * cmd. Parse BG error
2579 					 */
2580 					lpfc_parse_bg_err(phba, lpfc_cmd,
2581 							pIocbOut);
2582 					break;
2583 				} else {
2584 					lpfc_printf_vlog(vport, KERN_WARNING,
2585 							LOG_BG,
2586 							"9031 non-zero BGSTAT "
2587 							"on unprotected cmd\n");
2588 				}
2589 			}
2590 			if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
2591 				&& (phba->sli_rev == LPFC_SLI_REV4)
2592 				&& (pnode && NLP_CHK_NODE_ACT(pnode))) {
2593 				/* This IO was aborted by the target, we don't
2594 				 * know the rxid and because we did not send the
2595 				 * ABTS we cannot generate and RRQ.
2596 				 */
2597 				lpfc_set_rrq_active(phba, pnode,
2598 						lpfc_cmd->cur_iocbq.sli4_xritag,
2599 						0, 0);
2600 			}
2601 		/* else: fall through */
2602 		default:
2603 			cmd->result = ScsiResult(DID_ERROR, 0);
2604 			break;
2605 		}
2606 
2607 		if (!pnode || !NLP_CHK_NODE_ACT(pnode)
2608 		    || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
2609 			cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
2610 						 SAM_STAT_BUSY);
2611 	} else
2612 		cmd->result = ScsiResult(DID_OK, 0);
2613 
2614 	if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
2615 		uint32_t *lp = (uint32_t *)cmd->sense_buffer;
2616 
2617 		lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2618 				 "0710 Iodone <%d/%d> cmd %p, error "
2619 				 "x%x SNS x%x x%x Data: x%x x%x\n",
2620 				 cmd->device->id, cmd->device->lun, cmd,
2621 				 cmd->result, *lp, *(lp + 3), cmd->retries,
2622 				 scsi_get_resid(cmd));
2623 	}
2624 
2625 	lpfc_update_stats(phba, lpfc_cmd);
2626 	result = cmd->result;
2627 	if (vport->cfg_max_scsicmpl_time &&
2628 	   time_after(jiffies, lpfc_cmd->start_time +
2629 		msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
2630 		spin_lock_irqsave(shost->host_lock, flags);
2631 		if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2632 			if (pnode->cmd_qdepth >
2633 				atomic_read(&pnode->cmd_pending) &&
2634 				(atomic_read(&pnode->cmd_pending) >
2635 				LPFC_MIN_TGT_QDEPTH) &&
2636 				((cmd->cmnd[0] == READ_10) ||
2637 				(cmd->cmnd[0] == WRITE_10)))
2638 				pnode->cmd_qdepth =
2639 					atomic_read(&pnode->cmd_pending);
2640 
2641 			pnode->last_change_time = jiffies;
2642 		}
2643 		spin_unlock_irqrestore(shost->host_lock, flags);
2644 	} else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2645 		if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
2646 		   time_after(jiffies, pnode->last_change_time +
2647 			      msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
2648 			spin_lock_irqsave(shost->host_lock, flags);
2649 			depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
2650 				/ 100;
2651 			depth = depth ? depth : 1;
2652 			pnode->cmd_qdepth += depth;
2653 			if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
2654 				pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
2655 			pnode->last_change_time = jiffies;
2656 			spin_unlock_irqrestore(shost->host_lock, flags);
2657 		}
2658 	}
2659 
2660 	lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
2661 
2662 	/* The sdev is not guaranteed to be valid post scsi_done upcall. */
2663 	queue_depth = cmd->device->queue_depth;
2664 	scsi_id = cmd->device->id;
2665 	cmd->scsi_done(cmd);
2666 
2667 	if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2668 		/*
2669 		 * If there is a thread waiting for command completion
2670 		 * wake up the thread.
2671 		 */
2672 		spin_lock_irqsave(shost->host_lock, flags);
2673 		lpfc_cmd->pCmd = NULL;
2674 		if (lpfc_cmd->waitq)
2675 			wake_up(lpfc_cmd->waitq);
2676 		spin_unlock_irqrestore(shost->host_lock, flags);
2677 		lpfc_release_scsi_buf(phba, lpfc_cmd);
2678 		return;
2679 	}
2680 
2681 	if (!result)
2682 		lpfc_rampup_queue_depth(vport, queue_depth);
2683 
2684 	/*
2685 	 * Check for queue full.  If the lun is reporting queue full, then
2686 	 * back off the lun queue depth to prevent target overloads.
2687 	 */
2688 	if (result == SAM_STAT_TASK_SET_FULL && pnode &&
2689 	    NLP_CHK_NODE_ACT(pnode)) {
2690 		shost_for_each_device(tmp_sdev, shost) {
2691 			if (tmp_sdev->id != scsi_id)
2692 				continue;
2693 			depth = scsi_track_queue_full(tmp_sdev,
2694 						      tmp_sdev->queue_depth-1);
2695 			if (depth <= 0)
2696 				continue;
2697 			lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2698 					 "0711 detected queue full - lun queue "
2699 					 "depth adjusted to %d.\n", depth);
2700 			lpfc_send_sdev_queuedepth_change_event(phba, vport,
2701 							       pnode,
2702 							       tmp_sdev->lun,
2703 							       depth+1, depth);
2704 		}
2705 	}
2706 
2707 	/*
2708 	 * If there is a thread waiting for command completion
2709 	 * wake up the thread.
2710 	 */
2711 	spin_lock_irqsave(shost->host_lock, flags);
2712 	lpfc_cmd->pCmd = NULL;
2713 	if (lpfc_cmd->waitq)
2714 		wake_up(lpfc_cmd->waitq);
2715 	spin_unlock_irqrestore(shost->host_lock, flags);
2716 
2717 	lpfc_release_scsi_buf(phba, lpfc_cmd);
2718 }
2719 
2720 /**
2721  * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
2722  * @data: A pointer to the immediate command data portion of the IOCB.
2723  * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
2724  *
2725  * The routine copies the entire FCP command from @fcp_cmnd to @data while
2726  * byte swapping the data to big endian format for transmission on the wire.
2727  **/
2728 static void
2729 lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
2730 {
2731 	int i, j;
2732 	for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
2733 	     i += sizeof(uint32_t), j++) {
2734 		((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
2735 	}
2736 }
2737 
2738 /**
2739  * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
2740  * @vport: The virtual port for which this call is being executed.
2741  * @lpfc_cmd: The scsi command which needs to send.
2742  * @pnode: Pointer to lpfc_nodelist.
2743  *
2744  * This routine initializes fcp_cmnd and iocb data structure from scsi command
2745  * to transfer for device with SLI3 interface spec.
2746  **/
2747 static void
2748 lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2749 		    struct lpfc_nodelist *pnode)
2750 {
2751 	struct lpfc_hba *phba = vport->phba;
2752 	struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2753 	struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2754 	IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2755 	struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
2756 	int datadir = scsi_cmnd->sc_data_direction;
2757 	char tag[2];
2758 
2759 	if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2760 		return;
2761 
2762 	lpfc_cmd->fcp_rsp->rspSnsLen = 0;
2763 	/* clear task management bits */
2764 	lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
2765 
2766 	int_to_scsilun(lpfc_cmd->pCmd->device->lun,
2767 			&lpfc_cmd->fcp_cmnd->fcp_lun);
2768 
2769 	memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
2770 
2771 	if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
2772 		switch (tag[0]) {
2773 		case HEAD_OF_QUEUE_TAG:
2774 			fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
2775 			break;
2776 		case ORDERED_QUEUE_TAG:
2777 			fcp_cmnd->fcpCntl1 = ORDERED_Q;
2778 			break;
2779 		default:
2780 			fcp_cmnd->fcpCntl1 = SIMPLE_Q;
2781 			break;
2782 		}
2783 	} else
2784 		fcp_cmnd->fcpCntl1 = 0;
2785 
2786 	/*
2787 	 * There are three possibilities here - use scatter-gather segment, use
2788 	 * the single mapping, or neither.  Start the lpfc command prep by
2789 	 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2790 	 * data bde entry.
2791 	 */
2792 	if (scsi_sg_count(scsi_cmnd)) {
2793 		if (datadir == DMA_TO_DEVICE) {
2794 			iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
2795 			if (phba->sli_rev < LPFC_SLI_REV4) {
2796 				iocb_cmd->un.fcpi.fcpi_parm = 0;
2797 				iocb_cmd->ulpPU = 0;
2798 			} else
2799 				iocb_cmd->ulpPU = PARM_READ_CHECK;
2800 			fcp_cmnd->fcpCntl3 = WRITE_DATA;
2801 			phba->fc4OutputRequests++;
2802 		} else {
2803 			iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
2804 			iocb_cmd->ulpPU = PARM_READ_CHECK;
2805 			fcp_cmnd->fcpCntl3 = READ_DATA;
2806 			phba->fc4InputRequests++;
2807 		}
2808 	} else {
2809 		iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
2810 		iocb_cmd->un.fcpi.fcpi_parm = 0;
2811 		iocb_cmd->ulpPU = 0;
2812 		fcp_cmnd->fcpCntl3 = 0;
2813 		phba->fc4ControlRequests++;
2814 	}
2815 	if (phba->sli_rev == 3 &&
2816 	    !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
2817 		lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
2818 	/*
2819 	 * Finish initializing those IOCB fields that are independent
2820 	 * of the scsi_cmnd request_buffer
2821 	 */
2822 	piocbq->iocb.ulpContext = pnode->nlp_rpi;
2823 	if (phba->sli_rev == LPFC_SLI_REV4)
2824 		piocbq->iocb.ulpContext =
2825 		  phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
2826 	if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
2827 		piocbq->iocb.ulpFCP2Rcvy = 1;
2828 	else
2829 		piocbq->iocb.ulpFCP2Rcvy = 0;
2830 
2831 	piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
2832 	piocbq->context1  = lpfc_cmd;
2833 	piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
2834 	piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
2835 	piocbq->vport = vport;
2836 }
2837 
2838 /**
2839  * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
2840  * @vport: The virtual port for which this call is being executed.
2841  * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2842  * @lun: Logical unit number.
2843  * @task_mgmt_cmd: SCSI task management command.
2844  *
2845  * This routine creates FCP information unit corresponding to @task_mgmt_cmd
2846  * for device with SLI-3 interface spec.
2847  *
2848  * Return codes:
2849  *   0 - Error
2850  *   1 - Success
2851  **/
2852 static int
2853 lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
2854 			     struct lpfc_scsi_buf *lpfc_cmd,
2855 			     unsigned int lun,
2856 			     uint8_t task_mgmt_cmd)
2857 {
2858 	struct lpfc_iocbq *piocbq;
2859 	IOCB_t *piocb;
2860 	struct fcp_cmnd *fcp_cmnd;
2861 	struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
2862 	struct lpfc_nodelist *ndlp = rdata->pnode;
2863 
2864 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2865 	    ndlp->nlp_state != NLP_STE_MAPPED_NODE)
2866 		return 0;
2867 
2868 	piocbq = &(lpfc_cmd->cur_iocbq);
2869 	piocbq->vport = vport;
2870 
2871 	piocb = &piocbq->iocb;
2872 
2873 	fcp_cmnd = lpfc_cmd->fcp_cmnd;
2874 	/* Clear out any old data in the FCP command area */
2875 	memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
2876 	int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
2877 	fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
2878 	if (vport->phba->sli_rev == 3 &&
2879 	    !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
2880 		lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
2881 	piocb->ulpCommand = CMD_FCP_ICMND64_CR;
2882 	piocb->ulpContext = ndlp->nlp_rpi;
2883 	if (vport->phba->sli_rev == LPFC_SLI_REV4) {
2884 		piocb->ulpContext =
2885 		  vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
2886 	}
2887 	if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
2888 		piocb->ulpFCP2Rcvy = 1;
2889 	}
2890 	piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
2891 
2892 	/* ulpTimeout is only one byte */
2893 	if (lpfc_cmd->timeout > 0xff) {
2894 		/*
2895 		 * Do not timeout the command at the firmware level.
2896 		 * The driver will provide the timeout mechanism.
2897 		 */
2898 		piocb->ulpTimeout = 0;
2899 	} else
2900 		piocb->ulpTimeout = lpfc_cmd->timeout;
2901 
2902 	if (vport->phba->sli_rev == LPFC_SLI_REV4)
2903 		lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
2904 
2905 	return 1;
2906 }
2907 
2908 /**
2909  * lpfc_scsi_api_table_setup - Set up scsi api function jump table
2910  * @phba: The hba struct for which this call is being executed.
2911  * @dev_grp: The HBA PCI-Device group number.
2912  *
2913  * This routine sets up the SCSI interface API function jump table in @phba
2914  * struct.
2915  * Returns: 0 - success, -ENODEV - failure.
2916  **/
2917 int
2918 lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
2919 {
2920 
2921 	phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
2922 	phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
2923 
2924 	switch (dev_grp) {
2925 	case LPFC_PCI_DEV_LP:
2926 		phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
2927 		phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
2928 		phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
2929 		phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
2930 		break;
2931 	case LPFC_PCI_DEV_OC:
2932 		phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
2933 		phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
2934 		phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
2935 		phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
2936 		break;
2937 	default:
2938 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2939 				"1418 Invalid HBA PCI-device group: 0x%x\n",
2940 				dev_grp);
2941 		return -ENODEV;
2942 		break;
2943 	}
2944 	phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
2945 	phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
2946 	return 0;
2947 }
2948 
2949 /**
2950  * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
2951  * @phba: The Hba for which this call is being executed.
2952  * @cmdiocbq: Pointer to lpfc_iocbq data structure.
2953  * @rspiocbq: Pointer to lpfc_iocbq data structure.
2954  *
2955  * This routine is IOCB completion routine for device reset and target reset
2956  * routine. This routine release scsi buffer associated with lpfc_cmd.
2957  **/
2958 static void
2959 lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
2960 			struct lpfc_iocbq *cmdiocbq,
2961 			struct lpfc_iocbq *rspiocbq)
2962 {
2963 	struct lpfc_scsi_buf *lpfc_cmd =
2964 		(struct lpfc_scsi_buf *) cmdiocbq->context1;
2965 	if (lpfc_cmd)
2966 		lpfc_release_scsi_buf(phba, lpfc_cmd);
2967 	return;
2968 }
2969 
2970 /**
2971  * lpfc_info - Info entry point of scsi_host_template data structure
2972  * @host: The scsi host for which this call is being executed.
2973  *
2974  * This routine provides module information about hba.
2975  *
2976  * Reutrn code:
2977  *   Pointer to char - Success.
2978  **/
2979 const char *
2980 lpfc_info(struct Scsi_Host *host)
2981 {
2982 	struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
2983 	struct lpfc_hba   *phba = vport->phba;
2984 	int len;
2985 	static char  lpfcinfobuf[384];
2986 
2987 	memset(lpfcinfobuf,0,384);
2988 	if (phba && phba->pcidev){
2989 		strncpy(lpfcinfobuf, phba->ModelDesc, 256);
2990 		len = strlen(lpfcinfobuf);
2991 		snprintf(lpfcinfobuf + len,
2992 			384-len,
2993 			" on PCI bus %02x device %02x irq %d",
2994 			phba->pcidev->bus->number,
2995 			phba->pcidev->devfn,
2996 			phba->pcidev->irq);
2997 		len = strlen(lpfcinfobuf);
2998 		if (phba->Port[0]) {
2999 			snprintf(lpfcinfobuf + len,
3000 				 384-len,
3001 				 " port %s",
3002 				 phba->Port);
3003 		}
3004 		len = strlen(lpfcinfobuf);
3005 		if (phba->sli4_hba.link_state.logical_speed) {
3006 			snprintf(lpfcinfobuf + len,
3007 				 384-len,
3008 				 " Logical Link Speed: %d Mbps",
3009 				 phba->sli4_hba.link_state.logical_speed * 10);
3010 		}
3011 	}
3012 	return lpfcinfobuf;
3013 }
3014 
3015 /**
3016  * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
3017  * @phba: The Hba for which this call is being executed.
3018  *
3019  * This routine modifies fcp_poll_timer  field of @phba by cfg_poll_tmo.
3020  * The default value of cfg_poll_tmo is 10 milliseconds.
3021  **/
3022 static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
3023 {
3024 	unsigned long  poll_tmo_expires =
3025 		(jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
3026 
3027 	if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
3028 		mod_timer(&phba->fcp_poll_timer,
3029 			  poll_tmo_expires);
3030 }
3031 
3032 /**
3033  * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
3034  * @phba: The Hba for which this call is being executed.
3035  *
3036  * This routine starts the fcp_poll_timer of @phba.
3037  **/
3038 void lpfc_poll_start_timer(struct lpfc_hba * phba)
3039 {
3040 	lpfc_poll_rearm_timer(phba);
3041 }
3042 
3043 /**
3044  * lpfc_poll_timeout - Restart polling timer
3045  * @ptr: Map to lpfc_hba data structure pointer.
3046  *
3047  * This routine restarts fcp_poll timer, when FCP ring  polling is enable
3048  * and FCP Ring interrupt is disable.
3049  **/
3050 
3051 void lpfc_poll_timeout(unsigned long ptr)
3052 {
3053 	struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
3054 
3055 	if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
3056 		lpfc_sli_handle_fast_ring_event(phba,
3057 			&phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
3058 
3059 		if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3060 			lpfc_poll_rearm_timer(phba);
3061 	}
3062 }
3063 
3064 /**
3065  * lpfc_queuecommand - scsi_host_template queuecommand entry point
3066  * @cmnd: Pointer to scsi_cmnd data structure.
3067  * @done: Pointer to done routine.
3068  *
3069  * Driver registers this routine to scsi midlayer to submit a @cmd to process.
3070  * This routine prepares an IOCB from scsi command and provides to firmware.
3071  * The @done callback is invoked after driver finished processing the command.
3072  *
3073  * Return value :
3074  *   0 - Success
3075  *   SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
3076  **/
3077 static int
3078 lpfc_queuecommand_lck(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
3079 {
3080 	struct Scsi_Host  *shost = cmnd->device->host;
3081 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3082 	struct lpfc_hba   *phba = vport->phba;
3083 	struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3084 	struct lpfc_nodelist *ndlp;
3085 	struct lpfc_scsi_buf *lpfc_cmd;
3086 	struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
3087 	int err;
3088 
3089 	err = fc_remote_port_chkready(rport);
3090 	if (err) {
3091 		cmnd->result = err;
3092 		goto out_fail_command;
3093 	}
3094 	ndlp = rdata->pnode;
3095 
3096 	if (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
3097 		scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
3098 
3099 		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3100 				"9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
3101 				" op:%02x str=%s without registering for"
3102 				" BlockGuard - Rejecting command\n",
3103 				cmnd->cmnd[0], scsi_get_prot_op(cmnd),
3104 				dif_op_str[scsi_get_prot_op(cmnd)]);
3105 		goto out_fail_command;
3106 	}
3107 
3108 	/*
3109 	 * Catch race where our node has transitioned, but the
3110 	 * transport is still transitioning.
3111 	 */
3112 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
3113 		cmnd->result = ScsiResult(DID_IMM_RETRY, 0);
3114 		goto out_fail_command;
3115 	}
3116 	if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
3117 		goto out_tgt_busy;
3118 
3119 	lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
3120 	if (lpfc_cmd == NULL) {
3121 		lpfc_rampdown_queue_depth(phba);
3122 
3123 		lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3124 				 "0707 driver's buffer pool is empty, "
3125 				 "IO busied\n");
3126 		goto out_host_busy;
3127 	}
3128 
3129 	/*
3130 	 * Store the midlayer's command structure for the completion phase
3131 	 * and complete the command initialization.
3132 	 */
3133 	lpfc_cmd->pCmd  = cmnd;
3134 	lpfc_cmd->rdata = rdata;
3135 	lpfc_cmd->timeout = 0;
3136 	lpfc_cmd->start_time = jiffies;
3137 	cmnd->host_scribble = (unsigned char *)lpfc_cmd;
3138 	cmnd->scsi_done = done;
3139 
3140 	if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
3141 		if (vport->phba->cfg_enable_bg) {
3142 			lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3143 				"9033 BLKGRD: rcvd protected cmd:%02x op:%02x "
3144 				"str=%s\n",
3145 				cmnd->cmnd[0], scsi_get_prot_op(cmnd),
3146 				dif_op_str[scsi_get_prot_op(cmnd)]);
3147 			lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3148 				"9034 BLKGRD: CDB: %02x %02x %02x %02x %02x "
3149 				"%02x %02x %02x %02x %02x\n",
3150 				cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
3151 				cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
3152 				cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
3153 				cmnd->cmnd[9]);
3154 			if (cmnd->cmnd[0] == READ_10)
3155 				lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3156 					"9035 BLKGRD: READ @ sector %llu, "
3157 					"count %u\n",
3158 					(unsigned long long)scsi_get_lba(cmnd),
3159 					blk_rq_sectors(cmnd->request));
3160 			else if (cmnd->cmnd[0] == WRITE_10)
3161 				lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3162 					"9036 BLKGRD: WRITE @ sector %llu, "
3163 					"count %u cmd=%p\n",
3164 					(unsigned long long)scsi_get_lba(cmnd),
3165 					blk_rq_sectors(cmnd->request),
3166 					cmnd);
3167 		}
3168 
3169 		err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3170 	} else {
3171 		if (vport->phba->cfg_enable_bg) {
3172 			lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3173 					"9038 BLKGRD: rcvd unprotected cmd:"
3174 					"%02x op:%02x str=%s\n",
3175 					cmnd->cmnd[0], scsi_get_prot_op(cmnd),
3176 					dif_op_str[scsi_get_prot_op(cmnd)]);
3177 				lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3178 					"9039 BLKGRD: CDB: %02x %02x %02x "
3179 					"%02x %02x %02x %02x %02x %02x %02x\n",
3180 					cmnd->cmnd[0], cmnd->cmnd[1],
3181 					cmnd->cmnd[2], cmnd->cmnd[3],
3182 					cmnd->cmnd[4], cmnd->cmnd[5],
3183 					cmnd->cmnd[6], cmnd->cmnd[7],
3184 					cmnd->cmnd[8], cmnd->cmnd[9]);
3185 			if (cmnd->cmnd[0] == READ_10)
3186 				lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3187 					"9040 dbg: READ @ sector %llu, "
3188 					"count %u\n",
3189 					(unsigned long long)scsi_get_lba(cmnd),
3190 					 blk_rq_sectors(cmnd->request));
3191 			else if (cmnd->cmnd[0] == WRITE_10)
3192 				lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3193 					 "9041 dbg: WRITE @ sector %llu, "
3194 					 "count %u cmd=%p\n",
3195 					 (unsigned long long)scsi_get_lba(cmnd),
3196 					 blk_rq_sectors(cmnd->request), cmnd);
3197 			else
3198 				lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3199 					 "9042 dbg: parser not implemented\n");
3200 		}
3201 		err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3202 	}
3203 
3204 	if (err)
3205 		goto out_host_busy_free_buf;
3206 
3207 	lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
3208 
3209 	atomic_inc(&ndlp->cmd_pending);
3210 	err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
3211 				  &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
3212 	if (err) {
3213 		atomic_dec(&ndlp->cmd_pending);
3214 		goto out_host_busy_free_buf;
3215 	}
3216 	if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
3217 		spin_unlock(shost->host_lock);
3218 		lpfc_sli_handle_fast_ring_event(phba,
3219 			&phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
3220 
3221 		spin_lock(shost->host_lock);
3222 		if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3223 			lpfc_poll_rearm_timer(phba);
3224 	}
3225 
3226 	return 0;
3227 
3228  out_host_busy_free_buf:
3229 	lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
3230 	lpfc_release_scsi_buf(phba, lpfc_cmd);
3231  out_host_busy:
3232 	return SCSI_MLQUEUE_HOST_BUSY;
3233 
3234  out_tgt_busy:
3235 	return SCSI_MLQUEUE_TARGET_BUSY;
3236 
3237  out_fail_command:
3238 	done(cmnd);
3239 	return 0;
3240 }
3241 
3242 static DEF_SCSI_QCMD(lpfc_queuecommand)
3243 
3244 /**
3245  * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
3246  * @cmnd: Pointer to scsi_cmnd data structure.
3247  *
3248  * This routine aborts @cmnd pending in base driver.
3249  *
3250  * Return code :
3251  *   0x2003 - Error
3252  *   0x2002 - Success
3253  **/
3254 static int
3255 lpfc_abort_handler(struct scsi_cmnd *cmnd)
3256 {
3257 	struct Scsi_Host  *shost = cmnd->device->host;
3258 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3259 	struct lpfc_hba   *phba = vport->phba;
3260 	struct lpfc_iocbq *iocb;
3261 	struct lpfc_iocbq *abtsiocb;
3262 	struct lpfc_scsi_buf *lpfc_cmd;
3263 	IOCB_t *cmd, *icmd;
3264 	int ret = SUCCESS;
3265 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
3266 
3267 	ret = fc_block_scsi_eh(cmnd);
3268 	if (ret)
3269 		return ret;
3270 	lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
3271 	if (!lpfc_cmd) {
3272 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3273 			 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
3274 			 "x%x ID %d LUN %d\n",
3275 			 ret, cmnd->device->id, cmnd->device->lun);
3276 		return SUCCESS;
3277 	}
3278 
3279 	/*
3280 	 * If pCmd field of the corresponding lpfc_scsi_buf structure
3281 	 * points to a different SCSI command, then the driver has
3282 	 * already completed this command, but the midlayer did not
3283 	 * see the completion before the eh fired.  Just return
3284 	 * SUCCESS.
3285 	 */
3286 	iocb = &lpfc_cmd->cur_iocbq;
3287 	if (lpfc_cmd->pCmd != cmnd)
3288 		goto out;
3289 
3290 	BUG_ON(iocb->context1 != lpfc_cmd);
3291 
3292 	abtsiocb = lpfc_sli_get_iocbq(phba);
3293 	if (abtsiocb == NULL) {
3294 		ret = FAILED;
3295 		goto out;
3296 	}
3297 
3298 	/*
3299 	 * The scsi command can not be in txq and it is in flight because the
3300 	 * pCmd is still pointig at the SCSI command we have to abort. There
3301 	 * is no need to search the txcmplq. Just send an abort to the FW.
3302 	 */
3303 
3304 	cmd = &iocb->iocb;
3305 	icmd = &abtsiocb->iocb;
3306 	icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
3307 	icmd->un.acxri.abortContextTag = cmd->ulpContext;
3308 	if (phba->sli_rev == LPFC_SLI_REV4)
3309 		icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
3310 	else
3311 		icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
3312 
3313 	icmd->ulpLe = 1;
3314 	icmd->ulpClass = cmd->ulpClass;
3315 
3316 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
3317 	abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
3318 	abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
3319 
3320 	if (lpfc_is_link_up(phba))
3321 		icmd->ulpCommand = CMD_ABORT_XRI_CN;
3322 	else
3323 		icmd->ulpCommand = CMD_CLOSE_XRI_CN;
3324 
3325 	abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
3326 	abtsiocb->vport = vport;
3327 	if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
3328 	    IOCB_ERROR) {
3329 		lpfc_sli_release_iocbq(phba, abtsiocb);
3330 		ret = FAILED;
3331 		goto out;
3332 	}
3333 
3334 	if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3335 		lpfc_sli_handle_fast_ring_event(phba,
3336 			&phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
3337 
3338 	lpfc_cmd->waitq = &waitq;
3339 	/* Wait for abort to complete */
3340 	wait_event_timeout(waitq,
3341 			  (lpfc_cmd->pCmd != cmnd),
3342 			   (2*vport->cfg_devloss_tmo*HZ));
3343 
3344 	spin_lock_irq(shost->host_lock);
3345 	lpfc_cmd->waitq = NULL;
3346 	spin_unlock_irq(shost->host_lock);
3347 
3348 	if (lpfc_cmd->pCmd == cmnd) {
3349 		ret = FAILED;
3350 		lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3351 				 "0748 abort handler timed out waiting "
3352 				 "for abort to complete: ret %#x, ID %d, "
3353 				 "LUN %d\n",
3354 				 ret, cmnd->device->id, cmnd->device->lun);
3355 	}
3356 
3357  out:
3358 	lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3359 			 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
3360 			 "LUN %d\n", ret, cmnd->device->id,
3361 			 cmnd->device->lun);
3362 	return ret;
3363 }
3364 
3365 static char *
3366 lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
3367 {
3368 	switch (task_mgmt_cmd) {
3369 	case FCP_ABORT_TASK_SET:
3370 		return "ABORT_TASK_SET";
3371 	case FCP_CLEAR_TASK_SET:
3372 		return "FCP_CLEAR_TASK_SET";
3373 	case FCP_BUS_RESET:
3374 		return "FCP_BUS_RESET";
3375 	case FCP_LUN_RESET:
3376 		return "FCP_LUN_RESET";
3377 	case FCP_TARGET_RESET:
3378 		return "FCP_TARGET_RESET";
3379 	case FCP_CLEAR_ACA:
3380 		return "FCP_CLEAR_ACA";
3381 	case FCP_TERMINATE_TASK:
3382 		return "FCP_TERMINATE_TASK";
3383 	default:
3384 		return "unknown";
3385 	}
3386 }
3387 
3388 /**
3389  * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
3390  * @vport: The virtual port for which this call is being executed.
3391  * @rdata: Pointer to remote port local data
3392  * @tgt_id: Target ID of remote device.
3393  * @lun_id: Lun number for the TMF
3394  * @task_mgmt_cmd: type of TMF to send
3395  *
3396  * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
3397  * a remote port.
3398  *
3399  * Return Code:
3400  *   0x2003 - Error
3401  *   0x2002 - Success.
3402  **/
3403 static int
3404 lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
3405 		    unsigned  tgt_id, unsigned int lun_id,
3406 		    uint8_t task_mgmt_cmd)
3407 {
3408 	struct lpfc_hba   *phba = vport->phba;
3409 	struct lpfc_scsi_buf *lpfc_cmd;
3410 	struct lpfc_iocbq *iocbq;
3411 	struct lpfc_iocbq *iocbqrsp;
3412 	struct lpfc_nodelist *pnode = rdata->pnode;
3413 	int ret;
3414 	int status;
3415 
3416 	if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3417 		return FAILED;
3418 
3419 	lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
3420 	if (lpfc_cmd == NULL)
3421 		return FAILED;
3422 	lpfc_cmd->timeout = 60;
3423 	lpfc_cmd->rdata = rdata;
3424 
3425 	status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
3426 					   task_mgmt_cmd);
3427 	if (!status) {
3428 		lpfc_release_scsi_buf(phba, lpfc_cmd);
3429 		return FAILED;
3430 	}
3431 
3432 	iocbq = &lpfc_cmd->cur_iocbq;
3433 	iocbqrsp = lpfc_sli_get_iocbq(phba);
3434 	if (iocbqrsp == NULL) {
3435 		lpfc_release_scsi_buf(phba, lpfc_cmd);
3436 		return FAILED;
3437 	}
3438 
3439 	lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3440 			 "0702 Issue %s to TGT %d LUN %d "
3441 			 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
3442 			 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
3443 			 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
3444 			 iocbq->iocb_flag);
3445 
3446 	status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
3447 					  iocbq, iocbqrsp, lpfc_cmd->timeout);
3448 	if (status != IOCB_SUCCESS) {
3449 		if (status == IOCB_TIMEDOUT) {
3450 			iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
3451 			ret = TIMEOUT_ERROR;
3452 		} else
3453 			ret = FAILED;
3454 		lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3455 		lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3456 			 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
3457 			 "iocb_flag x%x\n",
3458 			 lpfc_taskmgmt_name(task_mgmt_cmd),
3459 			 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
3460 			 iocbqrsp->iocb.un.ulpWord[4],
3461 			 iocbq->iocb_flag);
3462 	} else if (status == IOCB_BUSY)
3463 		ret = FAILED;
3464 	else
3465 		ret = SUCCESS;
3466 
3467 	lpfc_sli_release_iocbq(phba, iocbqrsp);
3468 
3469 	if (ret != TIMEOUT_ERROR)
3470 		lpfc_release_scsi_buf(phba, lpfc_cmd);
3471 
3472 	return ret;
3473 }
3474 
3475 /**
3476  * lpfc_chk_tgt_mapped -
3477  * @vport: The virtual port to check on
3478  * @cmnd: Pointer to scsi_cmnd data structure.
3479  *
3480  * This routine delays until the scsi target (aka rport) for the
3481  * command exists (is present and logged in) or we declare it non-existent.
3482  *
3483  * Return code :
3484  *  0x2003 - Error
3485  *  0x2002 - Success
3486  **/
3487 static int
3488 lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
3489 {
3490 	struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3491 	struct lpfc_nodelist *pnode;
3492 	unsigned long later;
3493 
3494 	if (!rdata) {
3495 		lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3496 			"0797 Tgt Map rport failure: rdata x%p\n", rdata);
3497 		return FAILED;
3498 	}
3499 	pnode = rdata->pnode;
3500 	/*
3501 	 * If target is not in a MAPPED state, delay until
3502 	 * target is rediscovered or devloss timeout expires.
3503 	 */
3504 	later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3505 	while (time_after(later, jiffies)) {
3506 		if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3507 			return FAILED;
3508 		if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
3509 			return SUCCESS;
3510 		schedule_timeout_uninterruptible(msecs_to_jiffies(500));
3511 		rdata = cmnd->device->hostdata;
3512 		if (!rdata)
3513 			return FAILED;
3514 		pnode = rdata->pnode;
3515 	}
3516 	if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
3517 	    (pnode->nlp_state != NLP_STE_MAPPED_NODE))
3518 		return FAILED;
3519 	return SUCCESS;
3520 }
3521 
3522 /**
3523  * lpfc_reset_flush_io_context -
3524  * @vport: The virtual port (scsi_host) for the flush context
3525  * @tgt_id: If aborting by Target contect - specifies the target id
3526  * @lun_id: If aborting by Lun context - specifies the lun id
3527  * @context: specifies the context level to flush at.
3528  *
3529  * After a reset condition via TMF, we need to flush orphaned i/o
3530  * contexts from the adapter. This routine aborts any contexts
3531  * outstanding, then waits for their completions. The wait is
3532  * bounded by devloss_tmo though.
3533  *
3534  * Return code :
3535  *  0x2003 - Error
3536  *  0x2002 - Success
3537  **/
3538 static int
3539 lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
3540 			uint64_t lun_id, lpfc_ctx_cmd context)
3541 {
3542 	struct lpfc_hba   *phba = vport->phba;
3543 	unsigned long later;
3544 	int cnt;
3545 
3546 	cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3547 	if (cnt)
3548 		lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
3549 				    tgt_id, lun_id, context);
3550 	later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3551 	while (time_after(later, jiffies) && cnt) {
3552 		schedule_timeout_uninterruptible(msecs_to_jiffies(20));
3553 		cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3554 	}
3555 	if (cnt) {
3556 		lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3557 			"0724 I/O flush failure for context %s : cnt x%x\n",
3558 			((context == LPFC_CTX_LUN) ? "LUN" :
3559 			 ((context == LPFC_CTX_TGT) ? "TGT" :
3560 			  ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
3561 			cnt);
3562 		return FAILED;
3563 	}
3564 	return SUCCESS;
3565 }
3566 
3567 /**
3568  * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
3569  * @cmnd: Pointer to scsi_cmnd data structure.
3570  *
3571  * This routine does a device reset by sending a LUN_RESET task management
3572  * command.
3573  *
3574  * Return code :
3575  *  0x2003 - Error
3576  *  0x2002 - Success
3577  **/
3578 static int
3579 lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
3580 {
3581 	struct Scsi_Host  *shost = cmnd->device->host;
3582 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3583 	struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3584 	struct lpfc_nodelist *pnode;
3585 	unsigned tgt_id = cmnd->device->id;
3586 	unsigned int lun_id = cmnd->device->lun;
3587 	struct lpfc_scsi_event_header scsi_event;
3588 	int status;
3589 
3590 	if (!rdata) {
3591 		lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3592 			"0798 Device Reset rport failure: rdata x%p\n", rdata);
3593 		return FAILED;
3594 	}
3595 	pnode = rdata->pnode;
3596 	status = fc_block_scsi_eh(cmnd);
3597 	if (status)
3598 		return status;
3599 
3600 	status = lpfc_chk_tgt_mapped(vport, cmnd);
3601 	if (status == FAILED) {
3602 		lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3603 			"0721 Device Reset rport failure: rdata x%p\n", rdata);
3604 		return FAILED;
3605 	}
3606 
3607 	scsi_event.event_type = FC_REG_SCSI_EVENT;
3608 	scsi_event.subcategory = LPFC_EVENT_LUNRESET;
3609 	scsi_event.lun = lun_id;
3610 	memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3611 	memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3612 
3613 	fc_host_post_vendor_event(shost, fc_get_event_number(),
3614 		sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3615 
3616 	status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3617 						FCP_LUN_RESET);
3618 
3619 	lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3620 			 "0713 SCSI layer issued Device Reset (%d, %d) "
3621 			 "return x%x\n", tgt_id, lun_id, status);
3622 
3623 	/*
3624 	 * We have to clean up i/o as : they may be orphaned by the TMF;
3625 	 * or if the TMF failed, they may be in an indeterminate state.
3626 	 * So, continue on.
3627 	 * We will report success if all the i/o aborts successfully.
3628 	 */
3629 	status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3630 						LPFC_CTX_LUN);
3631 	return status;
3632 }
3633 
3634 /**
3635  * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
3636  * @cmnd: Pointer to scsi_cmnd data structure.
3637  *
3638  * This routine does a target reset by sending a TARGET_RESET task management
3639  * command.
3640  *
3641  * Return code :
3642  *  0x2003 - Error
3643  *  0x2002 - Success
3644  **/
3645 static int
3646 lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
3647 {
3648 	struct Scsi_Host  *shost = cmnd->device->host;
3649 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3650 	struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3651 	struct lpfc_nodelist *pnode;
3652 	unsigned tgt_id = cmnd->device->id;
3653 	unsigned int lun_id = cmnd->device->lun;
3654 	struct lpfc_scsi_event_header scsi_event;
3655 	int status;
3656 
3657 	if (!rdata) {
3658 		lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3659 			"0799 Target Reset rport failure: rdata x%p\n", rdata);
3660 		return FAILED;
3661 	}
3662 	pnode = rdata->pnode;
3663 	status = fc_block_scsi_eh(cmnd);
3664 	if (status)
3665 		return status;
3666 
3667 	status = lpfc_chk_tgt_mapped(vport, cmnd);
3668 	if (status == FAILED) {
3669 		lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3670 			"0722 Target Reset rport failure: rdata x%p\n", rdata);
3671 		return FAILED;
3672 	}
3673 
3674 	scsi_event.event_type = FC_REG_SCSI_EVENT;
3675 	scsi_event.subcategory = LPFC_EVENT_TGTRESET;
3676 	scsi_event.lun = 0;
3677 	memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3678 	memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3679 
3680 	fc_host_post_vendor_event(shost, fc_get_event_number(),
3681 		sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3682 
3683 	status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3684 					FCP_TARGET_RESET);
3685 
3686 	lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3687 			 "0723 SCSI layer issued Target Reset (%d, %d) "
3688 			 "return x%x\n", tgt_id, lun_id, status);
3689 
3690 	/*
3691 	 * We have to clean up i/o as : they may be orphaned by the TMF;
3692 	 * or if the TMF failed, they may be in an indeterminate state.
3693 	 * So, continue on.
3694 	 * We will report success if all the i/o aborts successfully.
3695 	 */
3696 	status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3697 					LPFC_CTX_TGT);
3698 	return status;
3699 }
3700 
3701 /**
3702  * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
3703  * @cmnd: Pointer to scsi_cmnd data structure.
3704  *
3705  * This routine does target reset to all targets on @cmnd->device->host.
3706  * This emulates Parallel SCSI Bus Reset Semantics.
3707  *
3708  * Return code :
3709  *  0x2003 - Error
3710  *  0x2002 - Success
3711  **/
3712 static int
3713 lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
3714 {
3715 	struct Scsi_Host  *shost = cmnd->device->host;
3716 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3717 	struct lpfc_nodelist *ndlp = NULL;
3718 	struct lpfc_scsi_event_header scsi_event;
3719 	int match;
3720 	int ret = SUCCESS, status, i;
3721 
3722 	scsi_event.event_type = FC_REG_SCSI_EVENT;
3723 	scsi_event.subcategory = LPFC_EVENT_BUSRESET;
3724 	scsi_event.lun = 0;
3725 	memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
3726 	memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
3727 
3728 	fc_host_post_vendor_event(shost, fc_get_event_number(),
3729 		sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3730 
3731 	ret = fc_block_scsi_eh(cmnd);
3732 	if (ret)
3733 		return ret;
3734 
3735 	/*
3736 	 * Since the driver manages a single bus device, reset all
3737 	 * targets known to the driver.  Should any target reset
3738 	 * fail, this routine returns failure to the midlayer.
3739 	 */
3740 	for (i = 0; i < LPFC_MAX_TARGET; i++) {
3741 		/* Search for mapped node by target ID */
3742 		match = 0;
3743 		spin_lock_irq(shost->host_lock);
3744 		list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3745 			if (!NLP_CHK_NODE_ACT(ndlp))
3746 				continue;
3747 			if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
3748 			    ndlp->nlp_sid == i &&
3749 			    ndlp->rport) {
3750 				match = 1;
3751 				break;
3752 			}
3753 		}
3754 		spin_unlock_irq(shost->host_lock);
3755 		if (!match)
3756 			continue;
3757 
3758 		status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
3759 					i, 0, FCP_TARGET_RESET);
3760 
3761 		if (status != SUCCESS) {
3762 			lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3763 					 "0700 Bus Reset on target %d failed\n",
3764 					 i);
3765 			ret = FAILED;
3766 		}
3767 	}
3768 	/*
3769 	 * We have to clean up i/o as : they may be orphaned by the TMFs
3770 	 * above; or if any of the TMFs failed, they may be in an
3771 	 * indeterminate state.
3772 	 * We will report success if all the i/o aborts successfully.
3773 	 */
3774 
3775 	status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
3776 	if (status != SUCCESS)
3777 		ret = FAILED;
3778 
3779 	lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3780 			 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
3781 	return ret;
3782 }
3783 
3784 /**
3785  * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
3786  * @sdev: Pointer to scsi_device.
3787  *
3788  * This routine populates the cmds_per_lun count + 2 scsi_bufs into  this host's
3789  * globally available list of scsi buffers. This routine also makes sure scsi
3790  * buffer is not allocated more than HBA limit conveyed to midlayer. This list
3791  * of scsi buffer exists for the lifetime of the driver.
3792  *
3793  * Return codes:
3794  *   non-0 - Error
3795  *   0 - Success
3796  **/
3797 static int
3798 lpfc_slave_alloc(struct scsi_device *sdev)
3799 {
3800 	struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3801 	struct lpfc_hba   *phba = vport->phba;
3802 	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
3803 	uint32_t total = 0;
3804 	uint32_t num_to_alloc = 0;
3805 	int num_allocated = 0;
3806 	uint32_t sdev_cnt;
3807 
3808 	if (!rport || fc_remote_port_chkready(rport))
3809 		return -ENXIO;
3810 
3811 	sdev->hostdata = rport->dd_data;
3812 	sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
3813 
3814 	/*
3815 	 * Populate the cmds_per_lun count scsi_bufs into this host's globally
3816 	 * available list of scsi buffers.  Don't allocate more than the
3817 	 * HBA limit conveyed to the midlayer via the host structure.  The
3818 	 * formula accounts for the lun_queue_depth + error handlers + 1
3819 	 * extra.  This list of scsi bufs exists for the lifetime of the driver.
3820 	 */
3821 	total = phba->total_scsi_bufs;
3822 	num_to_alloc = vport->cfg_lun_queue_depth + 2;
3823 
3824 	/* If allocated buffers are enough do nothing */
3825 	if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
3826 		return 0;
3827 
3828 	/* Allow some exchanges to be available always to complete discovery */
3829 	if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
3830 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3831 				 "0704 At limitation of %d preallocated "
3832 				 "command buffers\n", total);
3833 		return 0;
3834 	/* Allow some exchanges to be available always to complete discovery */
3835 	} else if (total + num_to_alloc >
3836 		phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
3837 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3838 				 "0705 Allocation request of %d "
3839 				 "command buffers will exceed max of %d.  "
3840 				 "Reducing allocation request to %d.\n",
3841 				 num_to_alloc, phba->cfg_hba_queue_depth,
3842 				 (phba->cfg_hba_queue_depth - total));
3843 		num_to_alloc = phba->cfg_hba_queue_depth - total;
3844 	}
3845 	num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
3846 	if (num_to_alloc != num_allocated) {
3847 			lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3848 				 "0708 Allocation request of %d "
3849 				 "command buffers did not succeed.  "
3850 				 "Allocated %d buffers.\n",
3851 				 num_to_alloc, num_allocated);
3852 	}
3853 	if (num_allocated > 0)
3854 		phba->total_scsi_bufs += num_allocated;
3855 	return 0;
3856 }
3857 
3858 /**
3859  * lpfc_slave_configure - scsi_host_template slave_configure entry point
3860  * @sdev: Pointer to scsi_device.
3861  *
3862  * This routine configures following items
3863  *   - Tag command queuing support for @sdev if supported.
3864  *   - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
3865  *
3866  * Return codes:
3867  *   0 - Success
3868  **/
3869 static int
3870 lpfc_slave_configure(struct scsi_device *sdev)
3871 {
3872 	struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3873 	struct lpfc_hba   *phba = vport->phba;
3874 
3875 	if (sdev->tagged_supported)
3876 		scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
3877 	else
3878 		scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
3879 
3880 	if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
3881 		lpfc_sli_handle_fast_ring_event(phba,
3882 			&phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
3883 		if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3884 			lpfc_poll_rearm_timer(phba);
3885 	}
3886 
3887 	return 0;
3888 }
3889 
3890 /**
3891  * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
3892  * @sdev: Pointer to scsi_device.
3893  *
3894  * This routine sets @sdev hostatdata filed to null.
3895  **/
3896 static void
3897 lpfc_slave_destroy(struct scsi_device *sdev)
3898 {
3899 	struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3900 	struct lpfc_hba   *phba = vport->phba;
3901 	atomic_dec(&phba->sdev_cnt);
3902 	sdev->hostdata = NULL;
3903 	return;
3904 }
3905 
3906 
3907 struct scsi_host_template lpfc_template = {
3908 	.module			= THIS_MODULE,
3909 	.name			= LPFC_DRIVER_NAME,
3910 	.info			= lpfc_info,
3911 	.queuecommand		= lpfc_queuecommand,
3912 	.eh_abort_handler	= lpfc_abort_handler,
3913 	.eh_device_reset_handler = lpfc_device_reset_handler,
3914 	.eh_target_reset_handler = lpfc_target_reset_handler,
3915 	.eh_bus_reset_handler	= lpfc_bus_reset_handler,
3916 	.slave_alloc		= lpfc_slave_alloc,
3917 	.slave_configure	= lpfc_slave_configure,
3918 	.slave_destroy		= lpfc_slave_destroy,
3919 	.scan_finished		= lpfc_scan_finished,
3920 	.this_id		= -1,
3921 	.sg_tablesize		= LPFC_DEFAULT_SG_SEG_CNT,
3922 	.cmd_per_lun		= LPFC_CMD_PER_LUN,
3923 	.use_clustering		= ENABLE_CLUSTERING,
3924 	.shost_attrs		= lpfc_hba_attrs,
3925 	.max_sectors		= 0xFFFF,
3926 	.vendor_id		= LPFC_NL_VENDOR_ID,
3927 	.change_queue_depth	= lpfc_change_queue_depth,
3928 };
3929 
3930 struct scsi_host_template lpfc_vport_template = {
3931 	.module			= THIS_MODULE,
3932 	.name			= LPFC_DRIVER_NAME,
3933 	.info			= lpfc_info,
3934 	.queuecommand		= lpfc_queuecommand,
3935 	.eh_abort_handler	= lpfc_abort_handler,
3936 	.eh_device_reset_handler = lpfc_device_reset_handler,
3937 	.eh_target_reset_handler = lpfc_target_reset_handler,
3938 	.eh_bus_reset_handler	= lpfc_bus_reset_handler,
3939 	.slave_alloc		= lpfc_slave_alloc,
3940 	.slave_configure	= lpfc_slave_configure,
3941 	.slave_destroy		= lpfc_slave_destroy,
3942 	.scan_finished		= lpfc_scan_finished,
3943 	.this_id		= -1,
3944 	.sg_tablesize		= LPFC_DEFAULT_SG_SEG_CNT,
3945 	.cmd_per_lun		= LPFC_CMD_PER_LUN,
3946 	.use_clustering		= ENABLE_CLUSTERING,
3947 	.shost_attrs		= lpfc_vport_attrs,
3948 	.max_sectors		= 0xFFFF,
3949 	.change_queue_depth	= lpfc_change_queue_depth,
3950 };
3951