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