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