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