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