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