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