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