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