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