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