blob: 8570486013f32675b70cc5474b5c5deb0c85757b [file] [log] [blame]
James Smartd85296c2012-03-01 22:38:13 -05001/*******************************************************************
dea31012005-04-17 16:05:31 -05002 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smart128bdda2018-01-30 15:59:03 -08004 * Copyright (C) 2017-2018 Broadcom. All Rights Reserved. The term *
James Smartd080abe2017-02-12 13:52:39 -08005 * “Broadcom” refers to Broadcom Limited and/or its subsidiaries. *
James Smart51f4ca32016-07-06 12:36:13 -07006 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * EMULEX and SLI are trademarks of Emulex. *
James Smartd080abe2017-02-12 13:52:39 -08008 * www.broadcom.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04009 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -050010 * *
11 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040012 * 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. *
dea31012005-04-17 16:05:31 -050022 *******************************************************************/
dea31012005-04-17 16:05:31 -050023#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050025#include <linux/interrupt.h>
Paul Gortmaker09703662011-05-27 09:37:25 -040026#include <linux/export.h>
James Smarta90f5682006-08-17 11:58:04 -040027#include <linux/delay.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050028#include <asm/unaligned.h>
Dmitry Monakhov128b6f92017-06-29 11:31:12 -070029#include <linux/t10-pi.h>
James Smart737d4242013-04-17 20:14:49 -040030#include <linux/crc-t10dif.h>
31#include <net/checksum.h>
dea31012005-04-17 16:05:31 -050032
33#include <scsi/scsi.h>
34#include <scsi/scsi_device.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050035#include <scsi/scsi_eh.h>
dea31012005-04-17 16:05:31 -050036#include <scsi/scsi_host.h>
37#include <scsi/scsi_tcq.h>
38#include <scsi/scsi_transport_fc.h>
39
40#include "lpfc_version.h"
James Smartda0436e2009-05-22 14:51:39 -040041#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050042#include "lpfc_hw.h"
43#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040044#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040045#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050046#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050047#include "lpfc.h"
James Smart9a6b09c2012-03-01 22:37:42 -050048#include "lpfc_scsi.h"
dea31012005-04-17 16:05:31 -050049#include "lpfc_logmsg.h"
50#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050051#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050052
53#define LPFC_RESET_WAIT 2
54#define LPFC_ABORT_WAIT 2
55
James Smart737d4242013-04-17 20:14:49 -040056int _dump_buf_done = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -050057
58static char *dif_op_str[] = {
James Smart9a6b09c2012-03-01 22:37:42 -050059 "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
James Smartf9bb2da2011-10-10 21:34:11 -040068struct 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
James Smart1ba981f2014-02-20 09:56:45 -050074static struct lpfc_rport_data *
75lpfc_rport_data_from_scsi_device(struct scsi_device *sdev)
76{
77 struct lpfc_vport *vport = (struct lpfc_vport *)sdev->host->hostdata;
78
James Smartf38fa0b2014-04-04 13:52:21 -040079 if (vport->phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -050080 return ((struct lpfc_device_data *)sdev->hostdata)->rport_data;
81 else
82 return (struct lpfc_rport_data *)sdev->hostdata;
83}
84
James Smartda0436e2009-05-22 14:51:39 -040085static void
86lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smart1c6f4ef52009-11-18 15:40:49 -050087static void
88lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smart9c6aa9d2013-05-31 17:03:39 -040089static int
90lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc);
James Smarte2a0a9d2008-12-04 22:40:02 -050091
92static void
James Smart6a9c52c2009-10-02 15:16:51 -040093lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -050094{
95 void *src, *dst;
96 struct scatterlist *sgde = scsi_sglist(cmnd);
97
98 if (!_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040099 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
100 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -0500101 __func__);
102 return;
103 }
104
105
106 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -0400107 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
108 "9051 BLKGRD: ERROR: data scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -0500109 return;
110 }
111
112 dst = (void *) _dump_buf_data;
113 while (sgde) {
114 src = sg_virt(sgde);
115 memcpy(dst, src, sgde->length);
116 dst += sgde->length;
117 sgde = sg_next(sgde);
118 }
119}
120
121static void
James Smart6a9c52c2009-10-02 15:16:51 -0400122lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -0500123{
124 void *src, *dst;
125 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
126
127 if (!_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -0400128 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
129 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -0500130 __func__);
131 return;
132 }
133
134 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -0400135 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
136 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -0500137 return;
138 }
139
140 dst = _dump_buf_dif;
141 while (sgde) {
142 src = sg_virt(sgde);
143 memcpy(dst, src, sgde->length);
144 dst += sgde->length;
145 sgde = sg_next(sgde);
146 }
147}
148
James Smart9c6aa9d2013-05-31 17:03:39 -0400149static inline unsigned
150lpfc_cmd_blksize(struct scsi_cmnd *sc)
151{
152 return sc->device->sector_size;
153}
154
155#define LPFC_CHECK_PROTECT_GUARD 1
156#define LPFC_CHECK_PROTECT_REF 2
157static inline unsigned
158lpfc_cmd_protect(struct scsi_cmnd *sc, int flag)
159{
160 return 1;
161}
162
163static inline unsigned
164lpfc_cmd_guard_csum(struct scsi_cmnd *sc)
165{
166 if (lpfc_prot_group_type(NULL, sc) == LPFC_PG_TYPE_NO_DIF)
167 return 0;
168 if (scsi_host_get_guard(sc->device->host) == SHOST_DIX_GUARD_IP)
169 return 1;
170 return 0;
171}
172
James Smartea2151b2008-09-07 11:52:10 -0400173/**
James Smartf1126682009-06-10 17:22:44 -0400174 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
175 * @phba: Pointer to HBA object.
176 * @lpfc_cmd: lpfc scsi command object pointer.
177 *
178 * This function is called from the lpfc_prep_task_mgmt_cmd function to
179 * set the last bit in the response sge entry.
180 **/
181static void
182lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
183 struct lpfc_scsi_buf *lpfc_cmd)
184{
185 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
186 if (sgl) {
187 sgl += 1;
188 sgl->word2 = le32_to_cpu(sgl->word2);
189 bf_set(lpfc_sli4_sge_last, sgl, 1);
190 sgl->word2 = cpu_to_le32(sgl->word2);
191 }
192}
193
194/**
James Smart3621a712009-04-06 18:47:14 -0400195 * lpfc_update_stats - Update statistical data for the command completion
James Smartea2151b2008-09-07 11:52:10 -0400196 * @phba: Pointer to HBA object.
197 * @lpfc_cmd: lpfc scsi command object pointer.
198 *
199 * This function is called when there is a command completion and this
200 * function updates the statistical data for the command completion.
201 **/
202static void
203lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
204{
205 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
206 struct lpfc_nodelist *pnode = rdata->pnode;
207 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
208 unsigned long flags;
209 struct Scsi_Host *shost = cmd->device->host;
210 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
211 unsigned long latency;
212 int i;
213
214 if (cmd->result)
215 return;
216
James Smart9f1e1b52008-12-04 22:39:40 -0500217 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
218
James Smartea2151b2008-09-07 11:52:10 -0400219 spin_lock_irqsave(shost->host_lock, flags);
220 if (!vport->stat_data_enabled ||
221 vport->stat_data_blocked ||
James Smart5989b8d2010-10-22 11:06:56 -0400222 !pnode ||
James Smartea2151b2008-09-07 11:52:10 -0400223 !pnode->lat_data ||
224 (phba->bucket_type == LPFC_NO_BUCKET)) {
225 spin_unlock_irqrestore(shost->host_lock, flags);
226 return;
227 }
James Smartea2151b2008-09-07 11:52:10 -0400228
229 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
230 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
231 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -0500232 /* check array subscript bounds */
233 if (i < 0)
234 i = 0;
235 else if (i >= LPFC_MAX_BUCKET_COUNT)
236 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -0400237 } else {
238 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
239 if (latency <= (phba->bucket_base +
240 ((1<<i)*phba->bucket_step)))
241 break;
242 }
243
244 pnode->lat_data[i].cmd_count++;
245 spin_unlock_irqrestore(shost->host_lock, flags);
246}
247
James Smartea2151b2008-09-07 11:52:10 -0400248/**
James Smart3621a712009-04-06 18:47:14 -0400249 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500250 * @phba: The Hba for which this call is being executed.
251 *
252 * This routine is called when there is resource error in driver or firmware.
253 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
254 * posts at most 1 event each second. This routine wakes up worker thread of
255 * @phba to process WORKER_RAM_DOWN_EVENT event.
256 *
257 * This routine should be called with no lock held.
258 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500259void
James Smarteaf15d52008-12-04 22:39:29 -0500260lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500261{
262 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400263 uint32_t evt_posted;
Manuel Schölling0d4aec12014-09-03 12:55:58 -0400264 unsigned long expires;
James Smart92d7f7b2007-06-17 19:56:38 -0500265
266 spin_lock_irqsave(&phba->hbalock, flags);
267 atomic_inc(&phba->num_rsrc_err);
268 phba->last_rsrc_error_time = jiffies;
269
Manuel Schölling0d4aec12014-09-03 12:55:58 -0400270 expires = phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL;
271 if (time_after(expires, jiffies)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500272 spin_unlock_irqrestore(&phba->hbalock, flags);
273 return;
274 }
275
276 phba->last_ramp_down_time = jiffies;
277
278 spin_unlock_irqrestore(&phba->hbalock, flags);
279
280 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400281 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
282 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500283 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500284 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
285
James Smart5e9d9b82008-06-14 22:52:53 -0400286 if (!evt_posted)
287 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500288 return;
289}
290
James Smart9bad7672008-12-04 22:39:02 -0500291/**
James Smart3621a712009-04-06 18:47:14 -0400292 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500293 * @phba: The Hba for which this call is being executed.
294 *
295 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
296 * thread.This routine reduces queue depth for all scsi device on each vport
297 * associated with @phba.
298 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500299void
300lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
301{
James Smart549e55c2007-08-02 11:09:51 -0400302 struct lpfc_vport **vports;
303 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500304 struct scsi_device *sdev;
James Smart5ffc2662009-11-18 15:39:44 -0500305 unsigned long new_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500306 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400307 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500308
309 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
310 num_cmd_success = atomic_read(&phba->num_cmd_success);
311
James Smart75ad83a2012-05-09 21:18:40 -0400312 /*
313 * The error and success command counters are global per
314 * driver instance. If another handler has already
315 * operated on this error event, just exit.
316 */
317 if (num_rsrc_err == 0)
318 return;
319
James Smart549e55c2007-08-02 11:09:51 -0400320 vports = lpfc_create_vport_work_array(phba);
321 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400322 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400323 shost = lpfc_shost_from_vport(vports[i]);
324 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500325 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400326 sdev->queue_depth * num_rsrc_err /
327 (num_rsrc_err + num_cmd_success);
328 if (!new_queue_depth)
329 new_queue_depth = sdev->queue_depth - 1;
330 else
331 new_queue_depth = sdev->queue_depth -
332 new_queue_depth;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100333 scsi_change_queue_depth(sdev, new_queue_depth);
James Smart549e55c2007-08-02 11:09:51 -0400334 }
James Smart92d7f7b2007-06-17 19:56:38 -0500335 }
James Smart09372822008-01-11 01:52:54 -0500336 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500337 atomic_set(&phba->num_rsrc_err, 0);
338 atomic_set(&phba->num_cmd_success, 0);
339}
340
James Smart9bad7672008-12-04 22:39:02 -0500341/**
James Smart3621a712009-04-06 18:47:14 -0400342 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400343 * @phba: Pointer to HBA context object.
344 *
345 * This function walks vport list and set each SCSI host to block state
346 * by invoking fc_remote_port_delete() routine. This function is invoked
347 * with EEH when device's PCI slot has been permanently disabled.
348 **/
349void
350lpfc_scsi_dev_block(struct lpfc_hba *phba)
351{
352 struct lpfc_vport **vports;
353 struct Scsi_Host *shost;
354 struct scsi_device *sdev;
355 struct fc_rport *rport;
356 int i;
357
358 vports = lpfc_create_vport_work_array(phba);
359 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400360 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400361 shost = lpfc_shost_from_vport(vports[i]);
362 shost_for_each_device(sdev, shost) {
363 rport = starget_to_rport(scsi_target(sdev));
364 fc_remote_port_delete(rport);
365 }
366 }
367 lpfc_destroy_vport_work_array(phba, vports);
368}
369
James Smart9bad7672008-12-04 22:39:02 -0500370/**
James Smart3772a992009-05-22 14:50:54 -0400371 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500372 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400373 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500374 *
James Smart3772a992009-05-22 14:50:54 -0400375 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
376 * the scsi buffer contains all the necessary information needed to initiate
377 * a SCSI I/O. The non-DMAable buffer region contains information to build
378 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
379 * and the initial BPL. In addition to allocating memory, the FCP CMND and
380 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500381 *
382 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400383 * int - number of scsi buffers that were allocated.
384 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500385 **/
James Smart3772a992009-05-22 14:50:54 -0400386static int
387lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500388{
James Smart2e0fef82007-06-17 19:56:36 -0500389 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500390 struct lpfc_scsi_buf *psb;
391 struct ulp_bde64 *bpl;
392 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400393 dma_addr_t pdma_phys_fcp_cmd;
394 dma_addr_t pdma_phys_fcp_rsp;
395 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500396 uint16_t iotag;
James Smart96f70772013-04-17 20:16:15 -0400397 int bcnt, bpl_size;
398
399 bpl_size = phba->cfg_sg_dma_buf_size -
400 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
401
402 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
403 "9067 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
404 num_to_alloc, phba->cfg_sg_dma_buf_size,
405 (int)sizeof(struct fcp_cmnd),
406 (int)sizeof(struct fcp_rsp), bpl_size);
dea31012005-04-17 16:05:31 -0500407
James Smart3772a992009-05-22 14:50:54 -0400408 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
409 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
410 if (!psb)
411 break;
dea31012005-04-17 16:05:31 -0500412
James Smart3772a992009-05-22 14:50:54 -0400413 /*
414 * Get memory from the pci pool to map the virt space to pci
415 * bus space for an I/O. The DMA buffer includes space for the
416 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
417 * necessary to support the sg_tablesize.
418 */
Romain Perier771db5c2017-07-06 10:13:05 +0200419 psb->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
James Smart3772a992009-05-22 14:50:54 -0400420 GFP_KERNEL, &psb->dma_handle);
421 if (!psb->data) {
422 kfree(psb);
423 break;
424 }
dea31012005-04-17 16:05:31 -0500425
dea31012005-04-17 16:05:31 -0500426
James Smart3772a992009-05-22 14:50:54 -0400427 /* Allocate iotag for psb->cur_iocbq. */
428 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
429 if (iotag == 0) {
Romain Perier771db5c2017-07-06 10:13:05 +0200430 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smart895427b2017-02-12 13:52:30 -0800431 psb->data, psb->dma_handle);
James Smart3772a992009-05-22 14:50:54 -0400432 kfree(psb);
433 break;
434 }
435 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500436
James Smart3772a992009-05-22 14:50:54 -0400437 psb->fcp_cmnd = psb->data;
438 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
439 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400440 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500441
James Smart3772a992009-05-22 14:50:54 -0400442 /* Initialize local short-hand pointers. */
443 bpl = psb->fcp_bpl;
444 pdma_phys_fcp_cmd = psb->dma_handle;
445 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
446 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
447 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500448
James Smart3772a992009-05-22 14:50:54 -0400449 /*
450 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
451 * are sg list bdes. Initialize the first two and leave the
452 * rest for queuecommand.
453 */
454 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
455 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
456 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
457 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
458 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500459
James Smart3772a992009-05-22 14:50:54 -0400460 /* Setup the physical region for the FCP RSP */
461 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
462 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
463 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
464 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
465 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
466
467 /*
468 * Since the IOCB for the FCP I/O is built into this
469 * lpfc_scsi_buf, initialize it with all known data now.
470 */
471 iocb = &psb->cur_iocbq.iocb;
472 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
473 if ((phba->sli_rev == 3) &&
474 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
475 /* fill in immediate fcp command BDE */
476 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
477 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
478 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
479 unsli3.fcp_ext.icd);
480 iocb->un.fcpi64.bdl.addrHigh = 0;
481 iocb->ulpBdeCount = 0;
482 iocb->ulpLe = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300483 /* fill in response BDE */
James Smart3772a992009-05-22 14:50:54 -0400484 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
485 BUFF_TYPE_BDE_64;
486 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
487 sizeof(struct fcp_rsp);
488 iocb->unsli3.fcp_ext.rbde.addrLow =
489 putPaddrLow(pdma_phys_fcp_rsp);
490 iocb->unsli3.fcp_ext.rbde.addrHigh =
491 putPaddrHigh(pdma_phys_fcp_rsp);
492 } else {
493 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
494 iocb->un.fcpi64.bdl.bdeSize =
495 (2 * sizeof(struct ulp_bde64));
496 iocb->un.fcpi64.bdl.addrLow =
497 putPaddrLow(pdma_phys_bpl);
498 iocb->un.fcpi64.bdl.addrHigh =
499 putPaddrHigh(pdma_phys_bpl);
500 iocb->ulpBdeCount = 1;
501 iocb->ulpLe = 1;
502 }
503 iocb->ulpClass = CLASS3;
504 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400505 /* Put it back into the SCSI buffer list */
James Smarteee88772010-09-29 11:19:08 -0400506 psb->cur_iocbq.context1 = psb;
James Smart1c6f4ef52009-11-18 15:40:49 -0500507 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400508
James Smart34b02dc2008-08-24 21:49:55 -0400509 }
dea31012005-04-17 16:05:31 -0500510
James Smart3772a992009-05-22 14:50:54 -0400511 return bcnt;
dea31012005-04-17 16:05:31 -0500512}
513
James Smart9bad7672008-12-04 22:39:02 -0500514/**
James Smart1151e3e2011-02-16 12:39:35 -0500515 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
516 * @vport: pointer to lpfc vport data structure.
517 *
518 * This routine is invoked by the vport cleanup for deletions and the cleanup
519 * for an ndlp on removal.
520 **/
521void
522lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
523{
524 struct lpfc_hba *phba = vport->phba;
525 struct lpfc_scsi_buf *psb, *next_psb;
526 unsigned long iflag = 0;
527
James Smart895427b2017-02-12 13:52:30 -0800528 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
529 return;
James Smart1151e3e2011-02-16 12:39:35 -0500530 spin_lock_irqsave(&phba->hbalock, iflag);
531 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
532 list_for_each_entry_safe(psb, next_psb,
533 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
534 if (psb->rdata && psb->rdata->pnode
535 && psb->rdata->pnode->vport == vport)
536 psb->rdata = NULL;
537 }
538 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
539 spin_unlock_irqrestore(&phba->hbalock, iflag);
540}
541
542/**
James Smartda0436e2009-05-22 14:51:39 -0400543 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
544 * @phba: pointer to lpfc hba data structure.
545 * @axri: pointer to the fcp xri abort wcqe structure.
546 *
547 * This routine is invoked by the worker thread to process a SLI4 fast-path
548 * FCP aborted xri.
549 **/
550void
551lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
552 struct sli4_wcqe_xri_aborted *axri)
553{
554 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500555 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartda0436e2009-05-22 14:51:39 -0400556 struct lpfc_scsi_buf *psb, *next_psb;
557 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500558 struct lpfc_iocbq *iocbq;
559 int i;
James Smart19ca7602010-11-20 23:11:55 -0500560 struct lpfc_nodelist *ndlp;
561 int rrq_empty = 0;
James Smart895427b2017-02-12 13:52:30 -0800562 struct lpfc_sli_ring *pring = phba->sli4_hba.els_wq->pring;
James Smartda0436e2009-05-22 14:51:39 -0400563
James Smart895427b2017-02-12 13:52:30 -0800564 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
565 return;
James Smart0f65ff62010-02-26 14:14:23 -0500566 spin_lock_irqsave(&phba->hbalock, iflag);
567 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400568 list_for_each_entry_safe(psb, next_psb,
569 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
570 if (psb->cur_iocbq.sli4_xritag == xri) {
571 list_del(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500572 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400573 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500574 spin_unlock(
575 &phba->sli4_hba.abts_scsi_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500576 if (psb->rdata && psb->rdata->pnode)
577 ndlp = psb->rdata->pnode;
578 else
579 ndlp = NULL;
580
James Smart19ca7602010-11-20 23:11:55 -0500581 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500582 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartcb69f7d2011-12-13 13:21:57 -0500583 if (ndlp) {
James Smartee0f4fe2012-05-09 21:19:14 -0400584 lpfc_set_rrq_active(phba, ndlp,
585 psb->cur_iocbq.sli4_lxritag, rxid, 1);
James Smartcb69f7d2011-12-13 13:21:57 -0500586 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
587 }
James Smartda0436e2009-05-22 14:51:39 -0400588 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500589 if (rrq_empty)
590 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400591 return;
592 }
593 }
James Smart0f65ff62010-02-26 14:14:23 -0500594 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
595 for (i = 1; i <= phba->sli.last_iotag; i++) {
596 iocbq = phba->sli.iocbq_lookup[i];
597
598 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
599 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
600 continue;
601 if (iocbq->sli4_xritag != xri)
602 continue;
603 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
604 psb->exch_busy = 0;
605 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart0e9bb8d2013-03-01 16:35:12 -0500606 if (!list_empty(&pring->txq))
James Smart589a52d2010-07-14 15:30:54 -0400607 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500608 return;
609
610 }
611 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400612}
613
614/**
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -0700615 * lpfc_sli4_post_scsi_sgl_list - Post blocks of scsi buffer sgls from a list
James Smart8a9d2e82012-05-09 21:16:12 -0400616 * @phba: pointer to lpfc hba data structure.
617 * @post_sblist: pointer to the scsi buffer list.
618 *
619 * This routine walks a list of scsi buffers that was passed in. It attempts
620 * to construct blocks of scsi buffer sgls which contains contiguous xris and
621 * uses the non-embedded SGL block post mailbox commands to post to the port.
622 * For single SCSI buffer sgl with non-contiguous xri, if any, it shall use
623 * embedded SGL post mailbox command for posting. The @post_sblist passed in
624 * must be local list, thus no lock is needed when manipulate the list.
625 *
626 * Returns: 0 = failure, non-zero number of successfully posted buffers.
627 **/
Rashika Kheria7bfe7812014-09-03 12:55:36 -0400628static int
James Smart8a9d2e82012-05-09 21:16:12 -0400629lpfc_sli4_post_scsi_sgl_list(struct lpfc_hba *phba,
630 struct list_head *post_sblist, int sb_count)
631{
632 struct lpfc_scsi_buf *psb, *psb_next;
James Smart96f70772013-04-17 20:16:15 -0400633 int status, sgl_size;
James Smart8a9d2e82012-05-09 21:16:12 -0400634 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
635 dma_addr_t pdma_phys_bpl1;
636 int last_xritag = NO_XRI;
637 LIST_HEAD(prep_sblist);
638 LIST_HEAD(blck_sblist);
639 LIST_HEAD(scsi_sblist);
640
641 /* sanity check */
642 if (sb_count <= 0)
643 return -EINVAL;
644
James Smart96f70772013-04-17 20:16:15 -0400645 sgl_size = phba->cfg_sg_dma_buf_size -
646 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
647
James Smart8a9d2e82012-05-09 21:16:12 -0400648 list_for_each_entry_safe(psb, psb_next, post_sblist, list) {
649 list_del_init(&psb->list);
650 block_cnt++;
651 if ((last_xritag != NO_XRI) &&
652 (psb->cur_iocbq.sli4_xritag != last_xritag + 1)) {
653 /* a hole in xri block, form a sgl posting block */
654 list_splice_init(&prep_sblist, &blck_sblist);
655 post_cnt = block_cnt - 1;
656 /* prepare list for next posting block */
657 list_add_tail(&psb->list, &prep_sblist);
658 block_cnt = 1;
659 } else {
660 /* prepare list for next posting block */
661 list_add_tail(&psb->list, &prep_sblist);
662 /* enough sgls for non-embed sgl mbox command */
663 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
664 list_splice_init(&prep_sblist, &blck_sblist);
665 post_cnt = block_cnt;
666 block_cnt = 0;
667 }
668 }
669 num_posting++;
670 last_xritag = psb->cur_iocbq.sli4_xritag;
671
672 /* end of repost sgl list condition for SCSI buffers */
673 if (num_posting == sb_count) {
674 if (post_cnt == 0) {
675 /* last sgl posting block */
676 list_splice_init(&prep_sblist, &blck_sblist);
677 post_cnt = block_cnt;
678 } else if (block_cnt == 1) {
679 /* last single sgl with non-contiguous xri */
James Smart96f70772013-04-17 20:16:15 -0400680 if (sgl_size > SGL_PAGE_SIZE)
James Smart8a9d2e82012-05-09 21:16:12 -0400681 pdma_phys_bpl1 = psb->dma_phys_bpl +
682 SGL_PAGE_SIZE;
683 else
684 pdma_phys_bpl1 = 0;
685 status = lpfc_sli4_post_sgl(phba,
686 psb->dma_phys_bpl,
687 pdma_phys_bpl1,
688 psb->cur_iocbq.sli4_xritag);
689 if (status) {
690 /* failure, put on abort scsi list */
691 psb->exch_busy = 1;
692 } else {
693 /* success, put on SCSI buffer list */
694 psb->exch_busy = 0;
695 psb->status = IOSTAT_SUCCESS;
696 num_posted++;
697 }
698 /* success, put on SCSI buffer sgl list */
699 list_add_tail(&psb->list, &scsi_sblist);
700 }
701 }
702
703 /* continue until a nembed page worth of sgls */
704 if (post_cnt == 0)
705 continue;
706
707 /* post block of SCSI buffer list sgls */
708 status = lpfc_sli4_post_scsi_sgl_block(phba, &blck_sblist,
709 post_cnt);
710
711 /* don't reset xirtag due to hole in xri block */
712 if (block_cnt == 0)
713 last_xritag = NO_XRI;
714
715 /* reset SCSI buffer post count for next round of posting */
716 post_cnt = 0;
717
718 /* put posted SCSI buffer-sgl posted on SCSI buffer sgl list */
719 while (!list_empty(&blck_sblist)) {
720 list_remove_head(&blck_sblist, psb,
721 struct lpfc_scsi_buf, list);
722 if (status) {
723 /* failure, put on abort scsi list */
724 psb->exch_busy = 1;
725 } else {
726 /* success, put on SCSI buffer list */
727 psb->exch_busy = 0;
728 psb->status = IOSTAT_SUCCESS;
729 num_posted++;
730 }
731 list_add_tail(&psb->list, &scsi_sblist);
732 }
733 }
734 /* Push SCSI buffers with sgl posted to the availble list */
735 while (!list_empty(&scsi_sblist)) {
736 list_remove_head(&scsi_sblist, psb,
737 struct lpfc_scsi_buf, list);
738 lpfc_release_scsi_buf_s4(phba, psb);
739 }
740 return num_posted;
741}
742
743/**
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -0700744 * lpfc_sli4_repost_scsi_sgl_list - Repost all the allocated scsi buffer sgls
James Smartda0436e2009-05-22 14:51:39 -0400745 * @phba: pointer to lpfc hba data structure.
746 *
747 * This routine walks the list of scsi buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -0400748 * repost them to the port by using SGL block post. This is needed after a
James Smartda0436e2009-05-22 14:51:39 -0400749 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
750 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
751 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
752 *
753 * Returns: 0 = success, non-zero failure.
754 **/
755int
756lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
757{
James Smart8a9d2e82012-05-09 21:16:12 -0400758 LIST_HEAD(post_sblist);
759 int num_posted, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -0400760
James Smart8a9d2e82012-05-09 21:16:12 -0400761 /* get all SCSI buffers need to repost to a local list */
James Smarta40fc5f2013-04-17 20:17:40 -0400762 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -0400763 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400764 list_splice_init(&phba->lpfc_scsi_buf_list_get, &post_sblist);
765 list_splice(&phba->lpfc_scsi_buf_list_put, &post_sblist);
James Smart164cecd2013-09-06 12:22:38 -0400766 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400767 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smartda0436e2009-05-22 14:51:39 -0400768
James Smart8a9d2e82012-05-09 21:16:12 -0400769 /* post the list of scsi buffer sgls to port if available */
770 if (!list_empty(&post_sblist)) {
771 num_posted = lpfc_sli4_post_scsi_sgl_list(phba, &post_sblist,
772 phba->sli4_hba.scsi_xri_cnt);
773 /* failed to post any scsi buffer, return error */
774 if (num_posted == 0)
775 rc = -EIO;
James Smartda0436e2009-05-22 14:51:39 -0400776 }
777 return rc;
778}
779
780/**
781 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
782 * @vport: The virtual port for which this call being executed.
783 * @num_to_allocate: The requested number of buffers to allocate.
784 *
James Smart8a9d2e82012-05-09 21:16:12 -0400785 * This routine allocates scsi buffers for device with SLI-4 interface spec,
James Smartda0436e2009-05-22 14:51:39 -0400786 * the scsi buffer contains all the necessary information needed to initiate
James Smart8a9d2e82012-05-09 21:16:12 -0400787 * a SCSI I/O. After allocating up to @num_to_allocate SCSI buffers and put
788 * them on a list, it post them to the port by using SGL block post.
James Smartda0436e2009-05-22 14:51:39 -0400789 *
790 * Return codes:
James Smart8a9d2e82012-05-09 21:16:12 -0400791 * int - number of scsi buffers that were allocated and posted.
James Smartda0436e2009-05-22 14:51:39 -0400792 * 0 = failure, less than num_to_alloc is a partial failure.
793 **/
794static int
795lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
796{
797 struct lpfc_hba *phba = vport->phba;
798 struct lpfc_scsi_buf *psb;
799 struct sli4_sge *sgl;
800 IOCB_t *iocb;
801 dma_addr_t pdma_phys_fcp_cmd;
802 dma_addr_t pdma_phys_fcp_rsp;
James Smart96f70772013-04-17 20:16:15 -0400803 dma_addr_t pdma_phys_bpl;
James Smart8a9d2e82012-05-09 21:16:12 -0400804 uint16_t iotag, lxri = 0;
James Smart96f70772013-04-17 20:16:15 -0400805 int bcnt, num_posted, sgl_size;
James Smart8a9d2e82012-05-09 21:16:12 -0400806 LIST_HEAD(prep_sblist);
807 LIST_HEAD(post_sblist);
808 LIST_HEAD(scsi_sblist);
James Smartda0436e2009-05-22 14:51:39 -0400809
James Smart96f70772013-04-17 20:16:15 -0400810 sgl_size = phba->cfg_sg_dma_buf_size -
811 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
812
813 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
814 "9068 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
815 num_to_alloc, phba->cfg_sg_dma_buf_size, sgl_size,
816 (int)sizeof(struct fcp_cmnd),
817 (int)sizeof(struct fcp_rsp));
818
James Smartda0436e2009-05-22 14:51:39 -0400819 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
820 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
821 if (!psb)
822 break;
James Smartda0436e2009-05-22 14:51:39 -0400823 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400824 * Get memory from the pci pool to map the virt space to
825 * pci bus space for an I/O. The DMA buffer includes space
826 * for the struct fcp_cmnd, struct fcp_rsp and the number
827 * of bde's necessary to support the sg_tablesize.
James Smartda0436e2009-05-22 14:51:39 -0400828 */
Romain Perier771db5c2017-07-06 10:13:05 +0200829 psb->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
James Smartda0436e2009-05-22 14:51:39 -0400830 GFP_KERNEL, &psb->dma_handle);
831 if (!psb->data) {
832 kfree(psb);
833 break;
834 }
James Smartda0436e2009-05-22 14:51:39 -0400835
James Smart092cb032013-09-06 12:21:50 -0400836 /*
837 * 4K Page alignment is CRITICAL to BlockGuard, double check
838 * to be sure.
839 */
James Smartf44ac122018-03-05 12:04:08 -0800840 if ((phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
841 (((unsigned long)(psb->data) &
James Smart092cb032013-09-06 12:21:50 -0400842 (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0)) {
James Smartf44ac122018-03-05 12:04:08 -0800843 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
844 "3369 Memory alignment error "
845 "addr=%lx\n",
846 (unsigned long)psb->data);
Romain Perier771db5c2017-07-06 10:13:05 +0200847 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smart96f70772013-04-17 20:16:15 -0400848 psb->data, psb->dma_handle);
849 kfree(psb);
850 break;
851 }
852
James Smartda0436e2009-05-22 14:51:39 -0400853
James Smart6d368e52011-05-24 11:44:12 -0400854 lxri = lpfc_sli4_next_xritag(phba);
855 if (lxri == NO_XRI) {
Romain Perier771db5c2017-07-06 10:13:05 +0200856 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smart895427b2017-02-12 13:52:30 -0800857 psb->data, psb->dma_handle);
James Smartda0436e2009-05-22 14:51:39 -0400858 kfree(psb);
859 break;
860 }
James Smartf7bc6432013-10-10 12:19:53 -0400861
862 /* Allocate iotag for psb->cur_iocbq. */
863 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
864 if (iotag == 0) {
Romain Perier771db5c2017-07-06 10:13:05 +0200865 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
James Smart895427b2017-02-12 13:52:30 -0800866 psb->data, psb->dma_handle);
James Smartf7bc6432013-10-10 12:19:53 -0400867 kfree(psb);
868 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -0700869 "3368 Failed to allocate IOTAG for"
James Smartf7bc6432013-10-10 12:19:53 -0400870 " XRI:0x%x\n", lxri);
871 lpfc_sli4_free_xri(phba, lxri);
872 break;
873 }
James Smart6d368e52011-05-24 11:44:12 -0400874 psb->cur_iocbq.sli4_lxritag = lxri;
875 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smartda0436e2009-05-22 14:51:39 -0400876 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Smartda0436e2009-05-22 14:51:39 -0400877 psb->fcp_bpl = psb->data;
James Smart96f70772013-04-17 20:16:15 -0400878 psb->fcp_cmnd = (psb->data + sgl_size);
James Smartda0436e2009-05-22 14:51:39 -0400879 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
880 sizeof(struct fcp_cmnd));
881
882 /* Initialize local short-hand pointers. */
883 sgl = (struct sli4_sge *)psb->fcp_bpl;
884 pdma_phys_bpl = psb->dma_handle;
James Smart96f70772013-04-17 20:16:15 -0400885 pdma_phys_fcp_cmd = (psb->dma_handle + sgl_size);
James Smartda0436e2009-05-22 14:51:39 -0400886 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
887
888 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400889 * The first two bdes are the FCP_CMD and FCP_RSP.
890 * The balance are sg list bdes. Initialize the
891 * first two and leave the rest for queuecommand.
James Smartda0436e2009-05-22 14:51:39 -0400892 */
893 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
894 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
James Smart05580562011-05-24 11:40:48 -0400895 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400896 bf_set(lpfc_sli4_sge_last, sgl, 0);
897 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500898 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
James Smartda0436e2009-05-22 14:51:39 -0400899 sgl++;
900
901 /* Setup the physical region for the FCP RSP */
902 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
903 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smart05580562011-05-24 11:40:48 -0400904 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400905 bf_set(lpfc_sli4_sge_last, sgl, 1);
906 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500907 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -0400908
909 /*
910 * Since the IOCB for the FCP I/O is built into this
911 * lpfc_scsi_buf, initialize it with all known data now.
912 */
913 iocb = &psb->cur_iocbq.iocb;
914 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
915 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
916 /* setting the BLP size to 2 * sizeof BDE may not be correct.
917 * We are setting the bpl to point to out sgl. An sgl's
918 * entries are 16 bytes, a bpl entries are 12 bytes.
919 */
920 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
921 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
922 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
923 iocb->ulpBdeCount = 1;
924 iocb->ulpLe = 1;
925 iocb->ulpClass = CLASS3;
James Smart8a9d2e82012-05-09 21:16:12 -0400926 psb->cur_iocbq.context1 = psb;
James Smartda0436e2009-05-22 14:51:39 -0400927 psb->dma_phys_bpl = pdma_phys_bpl;
James Smart6d368e52011-05-24 11:44:12 -0400928
James Smart8a9d2e82012-05-09 21:16:12 -0400929 /* add the scsi buffer to a post list */
930 list_add_tail(&psb->list, &post_sblist);
James Smarta40fc5f2013-04-17 20:17:40 -0400931 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -0400932 phba->sli4_hba.scsi_xri_cnt++;
James Smarta40fc5f2013-04-17 20:17:40 -0400933 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smartda0436e2009-05-22 14:51:39 -0400934 }
James Smart895427b2017-02-12 13:52:30 -0800935 lpfc_printf_log(phba, KERN_INFO, LOG_BG | LOG_FCP,
James Smart8a9d2e82012-05-09 21:16:12 -0400936 "3021 Allocate %d out of %d requested new SCSI "
937 "buffers\n", bcnt, num_to_alloc);
James Smartda0436e2009-05-22 14:51:39 -0400938
James Smart8a9d2e82012-05-09 21:16:12 -0400939 /* post the list of scsi buffer sgls to port if available */
940 if (!list_empty(&post_sblist))
941 num_posted = lpfc_sli4_post_scsi_sgl_list(phba,
942 &post_sblist, bcnt);
943 else
944 num_posted = 0;
945
946 return num_posted;
James Smartda0436e2009-05-22 14:51:39 -0400947}
948
949/**
James Smart3772a992009-05-22 14:50:54 -0400950 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
951 * @vport: The virtual port for which this call being executed.
952 * @num_to_allocate: The requested number of buffers to allocate.
953 *
954 * This routine wraps the actual SCSI buffer allocator function pointer from
955 * the lpfc_hba struct.
956 *
957 * Return codes:
958 * int - number of scsi buffers that were allocated.
959 * 0 = failure, less than num_to_alloc is a partial failure.
960 **/
961static inline int
962lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
963{
964 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
965}
966
967/**
James Smart19ca7602010-11-20 23:11:55 -0500968 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -0400969 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -0500970 *
971 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
972 * and returns to caller.
973 *
974 * Return codes:
975 * NULL - Error
976 * Pointer to lpfc_scsi_buf - Success
977 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +0100978static struct lpfc_scsi_buf*
James Smart19ca7602010-11-20 23:11:55 -0500979lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -0500980{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400981 struct lpfc_scsi_buf * lpfc_cmd = NULL;
James Smarta40fc5f2013-04-17 20:17:40 -0400982 struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
James Smart164cecd2013-09-06 12:22:38 -0400983 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500984
James Smart164cecd2013-09-06 12:22:38 -0400985 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
James Smarta40fc5f2013-04-17 20:17:40 -0400986 list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_scsi_buf,
987 list);
988 if (!lpfc_cmd) {
James Smart164cecd2013-09-06 12:22:38 -0400989 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400990 list_splice(&phba->lpfc_scsi_buf_list_put,
991 &phba->lpfc_scsi_buf_list_get);
992 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
993 list_remove_head(scsi_buf_list_get, lpfc_cmd,
994 struct lpfc_scsi_buf, list);
James Smart164cecd2013-09-06 12:22:38 -0400995 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smart1dcb58e2007-04-25 09:51:30 -0400996 }
James Smart164cecd2013-09-06 12:22:38 -0400997 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400998 return lpfc_cmd;
999}
James Smart19ca7602010-11-20 23:11:55 -05001000/**
1001 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1002 * @phba: The HBA for which this call is being executed.
1003 *
1004 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1005 * and returns to caller.
1006 *
1007 * Return codes:
1008 * NULL - Error
1009 * Pointer to lpfc_scsi_buf - Success
1010 **/
1011static struct lpfc_scsi_buf*
1012lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1013{
James Smart3be30e02013-05-31 17:05:17 -04001014 struct lpfc_scsi_buf *lpfc_cmd, *lpfc_cmd_next;
James Smart164cecd2013-09-06 12:22:38 -04001015 unsigned long iflag = 0;
James Smart19ca7602010-11-20 23:11:55 -05001016 int found = 0;
1017
James Smart164cecd2013-09-06 12:22:38 -04001018 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
James Smart3be30e02013-05-31 17:05:17 -04001019 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1020 &phba->lpfc_scsi_buf_list_get, list) {
James Smart19ca7602010-11-20 23:11:55 -05001021 if (lpfc_test_rrq_active(phba, ndlp,
James Smartee0f4fe2012-05-09 21:19:14 -04001022 lpfc_cmd->cur_iocbq.sli4_lxritag))
James Smart1151e3e2011-02-16 12:39:35 -05001023 continue;
1024 list_del(&lpfc_cmd->list);
James Smart19ca7602010-11-20 23:11:55 -05001025 found = 1;
James Smart1151e3e2011-02-16 12:39:35 -05001026 break;
James Smart19ca7602010-11-20 23:11:55 -05001027 }
James Smarta40fc5f2013-04-17 20:17:40 -04001028 if (!found) {
James Smart164cecd2013-09-06 12:22:38 -04001029 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04001030 list_splice(&phba->lpfc_scsi_buf_list_put,
1031 &phba->lpfc_scsi_buf_list_get);
1032 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart164cecd2013-09-06 12:22:38 -04001033 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smart3be30e02013-05-31 17:05:17 -04001034 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1035 &phba->lpfc_scsi_buf_list_get, list) {
James Smarta40fc5f2013-04-17 20:17:40 -04001036 if (lpfc_test_rrq_active(
1037 phba, ndlp, lpfc_cmd->cur_iocbq.sli4_lxritag))
1038 continue;
1039 list_del(&lpfc_cmd->list);
1040 found = 1;
1041 break;
1042 }
1043 }
James Smart164cecd2013-09-06 12:22:38 -04001044 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -05001045 if (!found)
1046 return NULL;
James Smarta40fc5f2013-04-17 20:17:40 -04001047 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001048}
1049/**
1050 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1051 * @phba: The HBA for which this call is being executed.
1052 *
1053 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1054 * and returns to caller.
1055 *
1056 * Return codes:
1057 * NULL - Error
1058 * Pointer to lpfc_scsi_buf - Success
1059 **/
1060static struct lpfc_scsi_buf*
1061lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1062{
1063 return phba->lpfc_get_scsi_buf(phba, ndlp);
1064}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001065
James Smart9bad7672008-12-04 22:39:02 -05001066/**
James Smart3772a992009-05-22 14:50:54 -04001067 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -05001068 * @phba: The Hba for which this call is being executed.
1069 * @psb: The scsi buffer which is being released.
1070 *
1071 * This routine releases @psb scsi buffer by adding it to tail of @phba
1072 * lpfc_scsi_buf_list list.
1073 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001074static void
James Smart3772a992009-05-22 14:50:54 -04001075lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001076{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001077 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001078
James Smarta40fc5f2013-04-17 20:17:40 -04001079 psb->seg_cnt = 0;
1080 psb->nonsg_phys = 0;
1081 psb->prot_seg_cnt = 0;
1082
1083 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001084 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -04001085 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -04001086 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1087 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
dea31012005-04-17 16:05:31 -05001088}
1089
James Smart9bad7672008-12-04 22:39:02 -05001090/**
James Smartda0436e2009-05-22 14:51:39 -04001091 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1092 * @phba: The Hba for which this call is being executed.
1093 * @psb: The scsi buffer which is being released.
1094 *
1095 * This routine releases @psb scsi buffer by adding it to tail of @phba
1096 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1097 * and cannot be reused for at least RA_TOV amount of time if it was
1098 * aborted.
1099 **/
1100static void
1101lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1102{
1103 unsigned long iflag = 0;
1104
James Smarta40fc5f2013-04-17 20:17:40 -04001105 psb->seg_cnt = 0;
1106 psb->nonsg_phys = 0;
1107 psb->prot_seg_cnt = 0;
1108
James Smart341af102010-01-26 23:07:37 -05001109 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -04001110 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1111 iflag);
1112 psb->pCmd = NULL;
1113 list_add_tail(&psb->list,
1114 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1115 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1116 iflag);
1117 } else {
James Smartda0436e2009-05-22 14:51:39 -04001118 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -04001119 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -04001120 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1121 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1122 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -04001123 }
1124}
1125
1126/**
James Smart3772a992009-05-22 14:50:54 -04001127 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1128 * @phba: The Hba for which this call is being executed.
1129 * @psb: The scsi buffer which is being released.
1130 *
1131 * This routine releases @psb scsi buffer by adding it to tail of @phba
1132 * lpfc_scsi_buf_list list.
1133 **/
1134static void
1135lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1136{
1137
1138 phba->lpfc_release_scsi_buf(phba, psb);
1139}
1140
1141/**
1142 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001143 * @phba: The Hba for which this call is being executed.
1144 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1145 *
1146 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001147 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001148 * through sg elements and format the bde. This routine also initializes all
James Smart3772a992009-05-22 14:50:54 -04001149 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001150 *
1151 * Return codes:
1152 * 1 - Error
1153 * 0 - Success
1154 **/
dea31012005-04-17 16:05:31 -05001155static int
James Smart3772a992009-05-22 14:50:54 -04001156lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001157{
1158 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1159 struct scatterlist *sgel = NULL;
1160 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1161 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001162 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001163 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001164 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001165 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001166 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001167 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001168
1169 /*
1170 * There are three possibilities here - use scatter-gather segment, use
1171 * the single mapping, or neither. Start the lpfc command prep by
1172 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1173 * data bde entry.
1174 */
1175 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001176 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001177 /*
1178 * The driver stores the segment count returned from pci_map_sg
1179 * because this a count of dma-mappings used to map the use_sg
1180 * pages. They are not guaranteed to be the same for those
1181 * architectures that implement an IOMMU.
1182 */
dea31012005-04-17 16:05:31 -05001183
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001184 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1185 scsi_sg_count(scsi_cmnd), datadir);
1186 if (unlikely(!nseg))
1187 return 1;
1188
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001189 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001190 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001191 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1192 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001193 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001194 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001195 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04001196 lpfc_cmd->seg_cnt = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001197 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001198 return 1;
1199 }
1200
1201 /*
1202 * The driver established a maximum scatter-gather segment count
1203 * during probe that limits the number of sg elements in any
1204 * single scsi command. Just run through the seg_cnt and format
1205 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001206 * When using SLI-3 the driver will try to fit all the BDEs into
1207 * the IOCB. If it can't then the BDEs get added to a BPL as it
1208 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001209 */
James Smart34b02dc2008-08-24 21:49:55 -04001210 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001211 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001212 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001213 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001214 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001215 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1216 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1217 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1218 data_bde->addrLow = putPaddrLow(physaddr);
1219 data_bde->addrHigh = putPaddrHigh(physaddr);
1220 data_bde++;
1221 } else {
1222 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1223 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1224 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1225 bpl->addrLow =
1226 le32_to_cpu(putPaddrLow(physaddr));
1227 bpl->addrHigh =
1228 le32_to_cpu(putPaddrHigh(physaddr));
1229 bpl++;
1230 }
dea31012005-04-17 16:05:31 -05001231 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001232 }
dea31012005-04-17 16:05:31 -05001233
1234 /*
1235 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001236 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1237 * explicitly reinitialized and for SLI-3 the extended bde count is
1238 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001239 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001240 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001241 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1242 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001243 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1244 /*
1245 * The extended IOCB format can only fit 3 BDE or a BPL.
1246 * This I/O has more than 3 BDE so the 1st data bde will
1247 * be a BPL that is filled in here.
1248 */
1249 physaddr = lpfc_cmd->dma_handle;
1250 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1251 data_bde->tus.f.bdeSize = (num_bde *
1252 sizeof(struct ulp_bde64));
1253 physaddr += (sizeof(struct fcp_cmnd) +
1254 sizeof(struct fcp_rsp) +
1255 (2 * sizeof(struct ulp_bde64)));
1256 data_bde->addrHigh = putPaddrHigh(physaddr);
1257 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001258 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -04001259 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1260 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001261 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -04001262 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1263 }
1264 } else {
1265 iocb_cmd->un.fcpi64.bdl.bdeSize =
1266 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001267 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001268 }
James Smart09372822008-01-11 01:52:54 -05001269 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001270
1271 /*
1272 * Due to difference in data length between DIF/non-DIF paths,
1273 * we need to set word 4 of IOCB here
1274 */
James Smarta257bf92009-04-06 18:48:10 -04001275 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001276 return 0;
1277}
1278
James Smartf9bb2da2011-10-10 21:34:11 -04001279#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001280
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001281/* Return BG_ERR_INIT if error injection is detected by Initiator */
James Smart9a6b09c2012-03-01 22:37:42 -05001282#define BG_ERR_INIT 0x1
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001283/* Return BG_ERR_TGT if error injection is detected by Target */
James Smart9a6b09c2012-03-01 22:37:42 -05001284#define BG_ERR_TGT 0x2
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001285/* Return BG_ERR_SWAP if swapping CSUM<-->CRC is required for error injection */
James Smart9a6b09c2012-03-01 22:37:42 -05001286#define BG_ERR_SWAP 0x10
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07001287/**
1288 * Return BG_ERR_CHECK if disabling Guard/Ref/App checking is required for
1289 * error injection
1290 **/
James Smart9a6b09c2012-03-01 22:37:42 -05001291#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -05001292
1293/**
1294 * lpfc_bg_err_inject - Determine if we should inject an error
1295 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -04001296 * @sc: The SCSI command to examine
1297 * @reftag: (out) BlockGuard reference tag for transmitted data
1298 * @apptag: (out) BlockGuard application tag for transmitted data
1299 * @new_guard (in) Value to replace CRC with if needed
1300 *
James Smart9a6b09c2012-03-01 22:37:42 -05001301 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -05001302 **/
James Smartf9bb2da2011-10-10 21:34:11 -04001303static int
1304lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1305 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1306{
1307 struct scatterlist *sgpe; /* s/g prot entry */
James Smart9a6b09c2012-03-01 22:37:42 -05001308 struct lpfc_scsi_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -05001309 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -05001310 struct lpfc_nodelist *ndlp;
1311 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -04001312 uint32_t op = scsi_get_prot_op(sc);
1313 uint32_t blksize;
1314 uint32_t numblks;
1315 sector_t lba;
1316 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001317 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001318
1319 if (op == SCSI_PROT_NORMAL)
1320 return 0;
1321
James Smartacd68592012-01-18 16:25:09 -05001322 sgpe = scsi_prot_sglist(sc);
James Smartf9bb2da2011-10-10 21:34:11 -04001323 lba = scsi_get_lba(sc);
James Smart4ac9b222012-03-01 22:38:29 -05001324
1325 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001326 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1327 blksize = lpfc_cmd_blksize(sc);
1328 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1329
1330 /* Make sure we have the right LBA if one is specified */
1331 if ((phba->lpfc_injerr_lba < lba) ||
1332 (phba->lpfc_injerr_lba >= (lba + numblks)))
1333 return 0;
James Smartacd68592012-01-18 16:25:09 -05001334 if (sgpe) {
1335 blockoff = phba->lpfc_injerr_lba - lba;
1336 numblks = sg_dma_len(sgpe) /
1337 sizeof(struct scsi_dif_tuple);
1338 if (numblks < blockoff)
1339 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001340 }
James Smartf9bb2da2011-10-10 21:34:11 -04001341 }
1342
James Smart4ac9b222012-03-01 22:38:29 -05001343 /* Next check if we need to match the remote NPortID or WWPN */
James Smart1ba981f2014-02-20 09:56:45 -05001344 rdata = lpfc_rport_data_from_scsi_device(sc->device);
James Smart4ac9b222012-03-01 22:38:29 -05001345 if (rdata && rdata->pnode) {
1346 ndlp = rdata->pnode;
1347
1348 /* Make sure we have the right NPortID if one is specified */
1349 if (phba->lpfc_injerr_nportid &&
1350 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1351 return 0;
1352
1353 /*
1354 * Make sure we have the right WWPN if one is specified.
1355 * wwn[0] should be a non-zero NAA in a good WWPN.
1356 */
1357 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1358 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1359 sizeof(struct lpfc_name)) != 0))
1360 return 0;
1361 }
1362
1363 /* Setup a ptr to the protection data if the SCSI host provides it */
1364 if (sgpe) {
1365 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1366 src += blockoff;
1367 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1368 }
1369
James Smartf9bb2da2011-10-10 21:34:11 -04001370 /* Should we change the Reference Tag */
1371 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001372 if (phba->lpfc_injerr_wref_cnt) {
1373 switch (op) {
1374 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001375 if (src) {
1376 /*
1377 * For WRITE_PASS, force the error
1378 * to be sent on the wire. It should
1379 * be detected by the Target.
1380 * If blockoff != 0 error will be
1381 * inserted in middle of the IO.
1382 */
James Smartf9bb2da2011-10-10 21:34:11 -04001383
James Smartacd68592012-01-18 16:25:09 -05001384 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1385 "9076 BLKGRD: Injecting reftag error: "
1386 "write lba x%lx + x%x oldrefTag x%x\n",
1387 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001388 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001389
1390 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001391 * Save the old ref_tag so we can
1392 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001393 */
James Smart9a6b09c2012-03-01 22:37:42 -05001394 if (lpfc_cmd) {
1395 lpfc_cmd->prot_data_type =
1396 LPFC_INJERR_REFTAG;
1397 lpfc_cmd->prot_data_segment =
1398 src;
1399 lpfc_cmd->prot_data =
1400 src->ref_tag;
1401 }
1402 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001403 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001404 if (phba->lpfc_injerr_wref_cnt == 0) {
1405 phba->lpfc_injerr_nportid = 0;
1406 phba->lpfc_injerr_lba =
1407 LPFC_INJERR_LBA_OFF;
1408 memset(&phba->lpfc_injerr_wwpn,
1409 0, sizeof(struct lpfc_name));
1410 }
James Smart9a6b09c2012-03-01 22:37:42 -05001411 rc = BG_ERR_TGT | BG_ERR_CHECK;
1412
James Smartacd68592012-01-18 16:25:09 -05001413 break;
1414 }
1415 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001416 case SCSI_PROT_WRITE_INSERT:
1417 /*
1418 * For WRITE_INSERT, force the error
1419 * to be sent on the wire. It should be
1420 * detected by the Target.
1421 */
1422 /* DEADBEEF will be the reftag on the wire */
1423 *reftag = 0xDEADBEEF;
1424 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001425 if (phba->lpfc_injerr_wref_cnt == 0) {
1426 phba->lpfc_injerr_nportid = 0;
1427 phba->lpfc_injerr_lba =
1428 LPFC_INJERR_LBA_OFF;
1429 memset(&phba->lpfc_injerr_wwpn,
1430 0, sizeof(struct lpfc_name));
1431 }
James Smart9a6b09c2012-03-01 22:37:42 -05001432 rc = BG_ERR_TGT | BG_ERR_CHECK;
1433
1434 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1435 "9078 BLKGRD: Injecting reftag error: "
1436 "write lba x%lx\n", (unsigned long)lba);
1437 break;
James Smartacd68592012-01-18 16:25:09 -05001438 case SCSI_PROT_WRITE_STRIP:
1439 /*
1440 * For WRITE_STRIP and WRITE_PASS,
1441 * force the error on data
1442 * being copied from SLI-Host to SLI-Port.
1443 */
1444 *reftag = 0xDEADBEEF;
1445 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001446 if (phba->lpfc_injerr_wref_cnt == 0) {
1447 phba->lpfc_injerr_nportid = 0;
1448 phba->lpfc_injerr_lba =
1449 LPFC_INJERR_LBA_OFF;
1450 memset(&phba->lpfc_injerr_wwpn,
1451 0, sizeof(struct lpfc_name));
1452 }
James Smartacd68592012-01-18 16:25:09 -05001453 rc = BG_ERR_INIT;
1454
1455 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1456 "9077 BLKGRD: Injecting reftag error: "
1457 "write lba x%lx\n", (unsigned long)lba);
1458 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001459 }
James Smartacd68592012-01-18 16:25:09 -05001460 }
1461 if (phba->lpfc_injerr_rref_cnt) {
1462 switch (op) {
1463 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001464 case SCSI_PROT_READ_STRIP:
1465 case SCSI_PROT_READ_PASS:
1466 /*
1467 * For READ_STRIP and READ_PASS, force the
1468 * error on data being read off the wire. It
1469 * should force an IO error to the driver.
1470 */
James Smartf9bb2da2011-10-10 21:34:11 -04001471 *reftag = 0xDEADBEEF;
1472 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001473 if (phba->lpfc_injerr_rref_cnt == 0) {
1474 phba->lpfc_injerr_nportid = 0;
1475 phba->lpfc_injerr_lba =
1476 LPFC_INJERR_LBA_OFF;
1477 memset(&phba->lpfc_injerr_wwpn,
1478 0, sizeof(struct lpfc_name));
1479 }
James Smartacd68592012-01-18 16:25:09 -05001480 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001481
1482 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001483 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001484 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001485 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001486 }
1487 }
1488 }
1489
1490 /* Should we change the Application Tag */
1491 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001492 if (phba->lpfc_injerr_wapp_cnt) {
1493 switch (op) {
1494 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001495 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001496 /*
1497 * For WRITE_PASS, force the error
1498 * to be sent on the wire. It should
1499 * be detected by the Target.
1500 * If blockoff != 0 error will be
1501 * inserted in middle of the IO.
1502 */
1503
James Smartacd68592012-01-18 16:25:09 -05001504 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1505 "9080 BLKGRD: Injecting apptag error: "
1506 "write lba x%lx + x%x oldappTag x%x\n",
1507 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001508 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001509
1510 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001511 * Save the old app_tag so we can
1512 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001513 */
James Smart9a6b09c2012-03-01 22:37:42 -05001514 if (lpfc_cmd) {
1515 lpfc_cmd->prot_data_type =
1516 LPFC_INJERR_APPTAG;
1517 lpfc_cmd->prot_data_segment =
1518 src;
1519 lpfc_cmd->prot_data =
1520 src->app_tag;
1521 }
1522 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001523 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001524 if (phba->lpfc_injerr_wapp_cnt == 0) {
1525 phba->lpfc_injerr_nportid = 0;
1526 phba->lpfc_injerr_lba =
1527 LPFC_INJERR_LBA_OFF;
1528 memset(&phba->lpfc_injerr_wwpn,
1529 0, sizeof(struct lpfc_name));
1530 }
James Smart9a6b09c2012-03-01 22:37:42 -05001531 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001532 break;
1533 }
1534 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001535 case SCSI_PROT_WRITE_INSERT:
1536 /*
1537 * For WRITE_INSERT, force the
1538 * error to be sent on the wire. It should be
1539 * detected by the Target.
1540 */
1541 /* DEAD will be the apptag on the wire */
1542 *apptag = 0xDEAD;
1543 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001544 if (phba->lpfc_injerr_wapp_cnt == 0) {
1545 phba->lpfc_injerr_nportid = 0;
1546 phba->lpfc_injerr_lba =
1547 LPFC_INJERR_LBA_OFF;
1548 memset(&phba->lpfc_injerr_wwpn,
1549 0, sizeof(struct lpfc_name));
1550 }
James Smart9a6b09c2012-03-01 22:37:42 -05001551 rc = BG_ERR_TGT | BG_ERR_CHECK;
1552
1553 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1554 "0813 BLKGRD: Injecting apptag error: "
1555 "write lba x%lx\n", (unsigned long)lba);
1556 break;
James Smartacd68592012-01-18 16:25:09 -05001557 case SCSI_PROT_WRITE_STRIP:
1558 /*
1559 * For WRITE_STRIP and WRITE_PASS,
1560 * force the error on data
1561 * being copied from SLI-Host to SLI-Port.
1562 */
1563 *apptag = 0xDEAD;
1564 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001565 if (phba->lpfc_injerr_wapp_cnt == 0) {
1566 phba->lpfc_injerr_nportid = 0;
1567 phba->lpfc_injerr_lba =
1568 LPFC_INJERR_LBA_OFF;
1569 memset(&phba->lpfc_injerr_wwpn,
1570 0, sizeof(struct lpfc_name));
1571 }
James Smartacd68592012-01-18 16:25:09 -05001572 rc = BG_ERR_INIT;
1573
1574 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1575 "0812 BLKGRD: Injecting apptag error: "
1576 "write lba x%lx\n", (unsigned long)lba);
1577 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001578 }
James Smartacd68592012-01-18 16:25:09 -05001579 }
1580 if (phba->lpfc_injerr_rapp_cnt) {
1581 switch (op) {
1582 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001583 case SCSI_PROT_READ_STRIP:
1584 case SCSI_PROT_READ_PASS:
1585 /*
1586 * For READ_STRIP and READ_PASS, force the
1587 * error on data being read off the wire. It
1588 * should force an IO error to the driver.
1589 */
James Smartf9bb2da2011-10-10 21:34:11 -04001590 *apptag = 0xDEAD;
1591 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001592 if (phba->lpfc_injerr_rapp_cnt == 0) {
1593 phba->lpfc_injerr_nportid = 0;
1594 phba->lpfc_injerr_lba =
1595 LPFC_INJERR_LBA_OFF;
1596 memset(&phba->lpfc_injerr_wwpn,
1597 0, sizeof(struct lpfc_name));
1598 }
James Smartacd68592012-01-18 16:25:09 -05001599 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001600
1601 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001602 "0814 BLKGRD: Injecting apptag error: "
1603 "read lba x%lx\n", (unsigned long)lba);
1604 break;
1605 }
1606 }
1607 }
1608
1609
1610 /* Should we change the Guard Tag */
1611 if (new_guard) {
1612 if (phba->lpfc_injerr_wgrd_cnt) {
1613 switch (op) {
1614 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001615 rc = BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001616 /* Drop thru */
James Smartacd68592012-01-18 16:25:09 -05001617
James Smartacd68592012-01-18 16:25:09 -05001618 case SCSI_PROT_WRITE_INSERT:
1619 /*
1620 * For WRITE_INSERT, force the
1621 * error to be sent on the wire. It should be
1622 * detected by the Target.
1623 */
1624 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001625 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1626 phba->lpfc_injerr_nportid = 0;
1627 phba->lpfc_injerr_lba =
1628 LPFC_INJERR_LBA_OFF;
1629 memset(&phba->lpfc_injerr_wwpn,
1630 0, sizeof(struct lpfc_name));
1631 }
James Smartacd68592012-01-18 16:25:09 -05001632
James Smart9a6b09c2012-03-01 22:37:42 -05001633 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001634 /* Signals the caller to swap CRC->CSUM */
1635
1636 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1637 "0817 BLKGRD: Injecting guard error: "
1638 "write lba x%lx\n", (unsigned long)lba);
1639 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001640 case SCSI_PROT_WRITE_STRIP:
1641 /*
1642 * For WRITE_STRIP and WRITE_PASS,
1643 * force the error on data
1644 * being copied from SLI-Host to SLI-Port.
1645 */
1646 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001647 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1648 phba->lpfc_injerr_nportid = 0;
1649 phba->lpfc_injerr_lba =
1650 LPFC_INJERR_LBA_OFF;
1651 memset(&phba->lpfc_injerr_wwpn,
1652 0, sizeof(struct lpfc_name));
1653 }
James Smart9a6b09c2012-03-01 22:37:42 -05001654
1655 rc = BG_ERR_INIT | BG_ERR_SWAP;
1656 /* Signals the caller to swap CRC->CSUM */
1657
1658 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1659 "0816 BLKGRD: Injecting guard error: "
1660 "write lba x%lx\n", (unsigned long)lba);
1661 break;
James Smartacd68592012-01-18 16:25:09 -05001662 }
1663 }
1664 if (phba->lpfc_injerr_rgrd_cnt) {
1665 switch (op) {
1666 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001667 case SCSI_PROT_READ_STRIP:
1668 case SCSI_PROT_READ_PASS:
1669 /*
1670 * For READ_STRIP and READ_PASS, force the
1671 * error on data being read off the wire. It
1672 * should force an IO error to the driver.
1673 */
James Smartacd68592012-01-18 16:25:09 -05001674 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001675 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1676 phba->lpfc_injerr_nportid = 0;
1677 phba->lpfc_injerr_lba =
1678 LPFC_INJERR_LBA_OFF;
1679 memset(&phba->lpfc_injerr_wwpn,
1680 0, sizeof(struct lpfc_name));
1681 }
James Smartacd68592012-01-18 16:25:09 -05001682
James Smart9a6b09c2012-03-01 22:37:42 -05001683 rc = BG_ERR_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001684 /* Signals the caller to swap CRC->CSUM */
1685
1686 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1687 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001688 "read lba x%lx\n", (unsigned long)lba);
1689 }
1690 }
1691 }
1692
James Smartf9bb2da2011-10-10 21:34:11 -04001693 return rc;
1694}
1695#endif
1696
James Smartacd68592012-01-18 16:25:09 -05001697/**
1698 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1699 * the specified SCSI command.
1700 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001701 * @sc: The SCSI command to examine
1702 * @txopt: (out) BlockGuard operation for transmitted data
1703 * @rxopt: (out) BlockGuard operation for received data
1704 *
1705 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1706 *
James Smartacd68592012-01-18 16:25:09 -05001707 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001708static int
James Smart6c8eea52010-04-06 14:49:53 -04001709lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1710 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001711{
James Smart6c8eea52010-04-06 14:49:53 -04001712 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001713
James Smart9c6aa9d2013-05-31 17:03:39 -04001714 if (lpfc_cmd_guard_csum(sc)) {
James Smarte2a0a9d2008-12-04 22:40:02 -05001715 switch (scsi_get_prot_op(sc)) {
1716 case SCSI_PROT_READ_INSERT:
1717 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001718 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001719 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001720 break;
1721
1722 case SCSI_PROT_READ_STRIP:
1723 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001724 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001725 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001726 break;
1727
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001728 case SCSI_PROT_READ_PASS:
1729 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001730 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001731 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001732 break;
1733
James Smarte2a0a9d2008-12-04 22:40:02 -05001734 case SCSI_PROT_NORMAL:
1735 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001736 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001737 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1738 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001739 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001740 break;
1741
1742 }
James Smart7c56b9f2011-07-22 18:36:25 -04001743 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001744 switch (scsi_get_prot_op(sc)) {
1745 case SCSI_PROT_READ_STRIP:
1746 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001747 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001748 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001749 break;
1750
1751 case SCSI_PROT_READ_PASS:
1752 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001753 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001754 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001755 break;
1756
James Smarte2a0a9d2008-12-04 22:40:02 -05001757 case SCSI_PROT_READ_INSERT:
1758 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001759 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001760 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001761 break;
1762
James Smarte2a0a9d2008-12-04 22:40:02 -05001763 case SCSI_PROT_NORMAL:
1764 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001765 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001766 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1767 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001768 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001769 break;
1770 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001771 }
1772
James Smart6c8eea52010-04-06 14:49:53 -04001773 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001774}
1775
James Smartacd68592012-01-18 16:25:09 -05001776#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1777/**
1778 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1779 * the specified SCSI command in order to force a guard tag error.
1780 * @phba: The Hba for which this call is being executed.
1781 * @sc: The SCSI command to examine
1782 * @txopt: (out) BlockGuard operation for transmitted data
1783 * @rxopt: (out) BlockGuard operation for received data
1784 *
1785 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1786 *
1787 **/
1788static int
1789lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1790 uint8_t *txop, uint8_t *rxop)
1791{
James Smartacd68592012-01-18 16:25:09 -05001792 uint8_t ret = 0;
1793
James Smart9c6aa9d2013-05-31 17:03:39 -04001794 if (lpfc_cmd_guard_csum(sc)) {
James Smartacd68592012-01-18 16:25:09 -05001795 switch (scsi_get_prot_op(sc)) {
1796 case SCSI_PROT_READ_INSERT:
1797 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001798 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001799 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001800 break;
1801
1802 case SCSI_PROT_READ_STRIP:
1803 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001804 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001805 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001806 break;
1807
1808 case SCSI_PROT_READ_PASS:
1809 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001810 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001811 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001812 break;
1813
1814 case SCSI_PROT_NORMAL:
1815 default:
1816 break;
1817
1818 }
1819 } else {
1820 switch (scsi_get_prot_op(sc)) {
1821 case SCSI_PROT_READ_STRIP:
1822 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001823 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001824 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001825 break;
1826
1827 case SCSI_PROT_READ_PASS:
1828 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001829 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05001830 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001831 break;
1832
1833 case SCSI_PROT_READ_INSERT:
1834 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001835 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001836 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001837 break;
1838
1839 case SCSI_PROT_NORMAL:
1840 default:
1841 break;
1842 }
1843 }
1844
1845 return ret;
1846}
1847#endif
1848
1849/**
1850 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1851 * @phba: The Hba for which this call is being executed.
1852 * @sc: pointer to scsi command we're working on
1853 * @bpl: pointer to buffer list for protection groups
1854 * @datacnt: number of segments of data that have been dma mapped
1855 *
1856 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05001857 * type LPFC_PG_TYPE_NO_DIF
1858 *
1859 * This is usually used when the HBA is instructed to generate
1860 * DIFs and insert them into data stream (or strip DIF from
1861 * incoming data stream)
1862 *
1863 * The buffer list consists of just one protection group described
1864 * below:
1865 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001866 * start of prot group --> | PDE_5 |
1867 * +-------------------------+
1868 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001869 * +-------------------------+
1870 * | Data BDE |
1871 * +-------------------------+
1872 * |more Data BDE's ... (opt)|
1873 * +-------------------------+
1874 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001875 *
1876 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05001877 *
1878 * Returns the number of BDEs added to the BPL.
1879 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001880static int
1881lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1882 struct ulp_bde64 *bpl, int datasegcnt)
1883{
1884 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001885 struct lpfc_pde5 *pde5 = NULL;
1886 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001887 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001888 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001889 int datadir = sc->sc_data_direction;
James Smart0829a192012-05-09 21:18:12 -04001890#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001891 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001892#endif
James Smartacd68592012-01-18 16:25:09 -05001893 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001894 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04001895 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001896
James Smart6c8eea52010-04-06 14:49:53 -04001897 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1898 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001899 goto out;
1900
James Smart6c8eea52010-04-06 14:49:53 -04001901 /* extract some info from the scsi command for pde*/
James Smartacd68592012-01-18 16:25:09 -05001902 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05001903
James Smartf9bb2da2011-10-10 21:34:11 -04001904#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001905 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001906 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001907 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001908 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001909 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001910 checking = 0;
1911 }
James Smartf9bb2da2011-10-10 21:34:11 -04001912#endif
1913
James Smart6c8eea52010-04-06 14:49:53 -04001914 /* setup PDE5 with what we have */
1915 pde5 = (struct lpfc_pde5 *) bpl;
1916 memset(pde5, 0, sizeof(struct lpfc_pde5));
1917 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001918
James Smartbc739052010-08-04 16:11:18 -04001919 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001920 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001921 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001922
James Smart6c8eea52010-04-06 14:49:53 -04001923 /* advance bpl and increment bde count */
1924 num_bde++;
1925 bpl++;
1926 pde6 = (struct lpfc_pde6 *) bpl;
1927
1928 /* setup PDE6 with the rest of the info */
1929 memset(pde6, 0, sizeof(struct lpfc_pde6));
1930 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1931 bf_set(pde6_optx, pde6, txop);
1932 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04001933
1934 /*
1935 * We only need to check the data on READs, for WRITEs
1936 * protection data is automatically generated, not checked.
1937 */
James Smart6c8eea52010-04-06 14:49:53 -04001938 if (datadir == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04001939 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04001940 bf_set(pde6_ce, pde6, checking);
1941 else
1942 bf_set(pde6_ce, pde6, 0);
1943
James Smart9c6aa9d2013-05-31 17:03:39 -04001944 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04001945 bf_set(pde6_re, pde6, checking);
1946 else
1947 bf_set(pde6_re, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001948 }
1949 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001950 bf_set(pde6_ae, pde6, 0);
1951 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001952
James Smartbc739052010-08-04 16:11:18 -04001953 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001954 pde6->word0 = cpu_to_le32(pde6->word0);
1955 pde6->word1 = cpu_to_le32(pde6->word1);
1956 pde6->word2 = cpu_to_le32(pde6->word2);
1957
James Smart6c8eea52010-04-06 14:49:53 -04001958 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001959 num_bde++;
1960 bpl++;
1961
1962 /* assumption: caller has already run dma_map_sg on command data */
1963 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1964 physaddr = sg_dma_address(sgde);
1965 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1966 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1967 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1968 if (datadir == DMA_TO_DEVICE)
1969 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1970 else
1971 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1972 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1973 bpl++;
1974 num_bde++;
1975 }
1976
1977out:
1978 return num_bde;
1979}
1980
James Smartacd68592012-01-18 16:25:09 -05001981/**
1982 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
1983 * @phba: The Hba for which this call is being executed.
1984 * @sc: pointer to scsi command we're working on
1985 * @bpl: pointer to buffer list for protection groups
1986 * @datacnt: number of segments of data that have been dma mapped
1987 * @protcnt: number of segment of protection data that have been dma mapped
1988 *
1989 * This function sets up BPL buffer list for protection groups of
1990 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05001991 *
1992 * This is usually used when DIFs are in their own buffers,
1993 * separate from the data. The HBA can then by instructed
1994 * to place the DIFs in the outgoing stream. For read operations,
1995 * The HBA could extract the DIFs and place it in DIF buffers.
1996 *
1997 * The buffer list for this type consists of one or more of the
1998 * protection groups described below:
1999 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002000 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002001 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002002 * | PDE_6 |
2003 * +-------------------------+
2004 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05002005 * +-------------------------+
2006 * | Data BDE |
2007 * +-------------------------+
2008 * |more Data BDE's ... (opt)|
2009 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002010 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002011 * +-------------------------+
2012 * | ... |
2013 * +-------------------------+
2014 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002015 * Note: It is assumed that both data and protection s/g buffers have been
2016 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05002017 *
2018 * Returns the number of BDEs added to the BPL.
2019 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002020static int
2021lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2022 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2023{
2024 struct scatterlist *sgde = NULL; /* s/g data entry */
2025 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04002026 struct lpfc_pde5 *pde5 = NULL;
2027 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05002028 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002029 dma_addr_t dataphysaddr, protphysaddr;
2030 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05002031 unsigned int split_offset;
2032 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05002033 unsigned int protgrp_blks, protgrp_bytes;
2034 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04002035 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002036 int datadir = sc->sc_data_direction;
2037 unsigned char pgdone = 0, alldone = 0;
2038 unsigned blksize;
James Smart0829a192012-05-09 21:18:12 -04002039#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002040 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002041#endif
James Smartacd68592012-01-18 16:25:09 -05002042 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002043 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04002044 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002045 int num_bde = 0;
2046
2047 sgpe = scsi_prot_sglist(sc);
2048 sgde = scsi_sglist(sc);
2049
2050 if (!sgpe || !sgde) {
2051 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2052 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2053 sgpe, sgde);
2054 return 0;
2055 }
2056
James Smart6c8eea52010-04-06 14:49:53 -04002057 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2058 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002059 goto out;
2060
James Smart6c8eea52010-04-06 14:49:53 -04002061 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05002062 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002063 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002064
James Smartf9bb2da2011-10-10 21:34:11 -04002065#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002066 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002067 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002068 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002069 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002070 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002071 checking = 0;
2072 }
James Smartf9bb2da2011-10-10 21:34:11 -04002073#endif
2074
James Smarte2a0a9d2008-12-04 22:40:02 -05002075 split_offset = 0;
2076 do {
James Smart96f70772013-04-17 20:16:15 -04002077 /* Check to see if we ran out of space */
2078 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
2079 return num_bde + 3;
2080
James Smart6c8eea52010-04-06 14:49:53 -04002081 /* setup PDE5 with what we have */
2082 pde5 = (struct lpfc_pde5 *) bpl;
2083 memset(pde5, 0, sizeof(struct lpfc_pde5));
2084 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002085
James Smartbc739052010-08-04 16:11:18 -04002086 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002087 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002088 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002089
James Smart6c8eea52010-04-06 14:49:53 -04002090 /* advance bpl and increment bde count */
2091 num_bde++;
2092 bpl++;
2093 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002094
James Smart6c8eea52010-04-06 14:49:53 -04002095 /* setup PDE6 with the rest of the info */
2096 memset(pde6, 0, sizeof(struct lpfc_pde6));
2097 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2098 bf_set(pde6_optx, pde6, txop);
2099 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002100
James Smart9c6aa9d2013-05-31 17:03:39 -04002101 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002102 bf_set(pde6_ce, pde6, checking);
2103 else
2104 bf_set(pde6_ce, pde6, 0);
2105
James Smart9c6aa9d2013-05-31 17:03:39 -04002106 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002107 bf_set(pde6_re, pde6, checking);
2108 else
2109 bf_set(pde6_re, pde6, 0);
2110
James Smart6c8eea52010-04-06 14:49:53 -04002111 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002112 bf_set(pde6_ae, pde6, 0);
2113 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002114
James Smartbc739052010-08-04 16:11:18 -04002115 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002116 pde6->word0 = cpu_to_le32(pde6->word0);
2117 pde6->word1 = cpu_to_le32(pde6->word1);
2118 pde6->word2 = cpu_to_le32(pde6->word2);
2119
James Smart6c8eea52010-04-06 14:49:53 -04002120 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002121 num_bde++;
2122 bpl++;
2123
2124 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05002125 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2126 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05002127
James Smarte2a0a9d2008-12-04 22:40:02 -05002128 /* must be integer multiple of the DIF block length */
2129 BUG_ON(protgroup_len % 8);
2130
James Smart7f860592011-03-11 16:05:52 -05002131 pde7 = (struct lpfc_pde7 *) bpl;
2132 memset(pde7, 0, sizeof(struct lpfc_pde7));
2133 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2134
James Smart7c56b9f2011-07-22 18:36:25 -04002135 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2136 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05002137
James Smarte2a0a9d2008-12-04 22:40:02 -05002138 protgrp_blks = protgroup_len / 8;
2139 protgrp_bytes = protgrp_blks * blksize;
2140
James Smart7f860592011-03-11 16:05:52 -05002141 /* check if this pde is crossing the 4K boundary; if so split */
2142 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2143 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2144 protgroup_offset += protgroup_remainder;
2145 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04002146 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05002147 } else {
2148 protgroup_offset = 0;
2149 curr_prot++;
2150 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002151
James Smarte2a0a9d2008-12-04 22:40:02 -05002152 num_bde++;
2153
2154 /* setup BDE's for data blocks associated with DIF data */
2155 pgdone = 0;
2156 subtotal = 0; /* total bytes processed for current prot grp */
2157 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002158 /* Check to see if we ran out of space */
2159 if (num_bde >= phba->cfg_total_seg_cnt)
2160 return num_bde + 1;
2161
James Smarte2a0a9d2008-12-04 22:40:02 -05002162 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04002163 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2164 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002165 __func__);
2166 return 0;
2167 }
2168 bpl++;
2169 dataphysaddr = sg_dma_address(sgde) + split_offset;
2170 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2171 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2172
2173 remainder = sg_dma_len(sgde) - split_offset;
2174
2175 if ((subtotal + remainder) <= protgrp_bytes) {
2176 /* we can use this whole buffer */
2177 bpl->tus.f.bdeSize = remainder;
2178 split_offset = 0;
2179
2180 if ((subtotal + remainder) == protgrp_bytes)
2181 pgdone = 1;
2182 } else {
2183 /* must split this buffer with next prot grp */
2184 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2185 split_offset += bpl->tus.f.bdeSize;
2186 }
2187
2188 subtotal += bpl->tus.f.bdeSize;
2189
2190 if (datadir == DMA_TO_DEVICE)
2191 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2192 else
2193 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2194 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2195
2196 num_bde++;
2197 curr_data++;
2198
2199 if (split_offset)
2200 break;
2201
2202 /* Move to the next s/g segment if possible */
2203 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04002204
James Smarte2a0a9d2008-12-04 22:40:02 -05002205 }
2206
James Smart7f860592011-03-11 16:05:52 -05002207 if (protgroup_offset) {
2208 /* update the reference tag */
2209 reftag += protgrp_blks;
2210 bpl++;
2211 continue;
2212 }
2213
James Smarte2a0a9d2008-12-04 22:40:02 -05002214 /* are we done ? */
2215 if (curr_prot == protcnt) {
2216 alldone = 1;
2217 } else if (curr_prot < protcnt) {
2218 /* advance to next prot buffer */
2219 sgpe = sg_next(sgpe);
2220 bpl++;
2221
2222 /* update the reference tag */
2223 reftag += protgrp_blks;
2224 } else {
2225 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04002226 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2227 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05002228 }
2229
2230 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05002231out:
2232
James Smarte2a0a9d2008-12-04 22:40:02 -05002233 return num_bde;
2234}
James Smart7f860592011-03-11 16:05:52 -05002235
James Smartacd68592012-01-18 16:25:09 -05002236/**
2237 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2238 * @phba: The Hba for which this call is being executed.
2239 * @sc: pointer to scsi command we're working on
2240 * @sgl: pointer to buffer list for protection groups
2241 * @datacnt: number of segments of data that have been dma mapped
2242 *
2243 * This function sets up SGL buffer list for protection groups of
2244 * type LPFC_PG_TYPE_NO_DIF
2245 *
2246 * This is usually used when the HBA is instructed to generate
2247 * DIFs and insert them into data stream (or strip DIF from
2248 * incoming data stream)
2249 *
2250 * The buffer list consists of just one protection group described
2251 * below:
2252 * +-------------------------+
2253 * start of prot group --> | DI_SEED |
2254 * +-------------------------+
2255 * | Data SGE |
2256 * +-------------------------+
2257 * |more Data SGE's ... (opt)|
2258 * +-------------------------+
2259 *
2260 *
2261 * Note: Data s/g buffers have been dma mapped
2262 *
2263 * Returns the number of SGEs added to the SGL.
2264 **/
2265static int
2266lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2267 struct sli4_sge *sgl, int datasegcnt)
2268{
2269 struct scatterlist *sgde = NULL; /* s/g data entry */
2270 struct sli4_sge_diseed *diseed = NULL;
2271 dma_addr_t physaddr;
2272 int i = 0, num_sge = 0, status;
James Smartacd68592012-01-18 16:25:09 -05002273 uint32_t reftag;
James Smartacd68592012-01-18 16:25:09 -05002274 uint8_t txop, rxop;
James Smart0829a192012-05-09 21:18:12 -04002275#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002276 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002277#endif
James Smartacd68592012-01-18 16:25:09 -05002278 uint32_t checking = 1;
2279 uint32_t dma_len;
2280 uint32_t dma_offset = 0;
2281
2282 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2283 if (status)
2284 goto out;
2285
2286 /* extract some info from the scsi command for pde*/
James Smartacd68592012-01-18 16:25:09 -05002287 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2288
2289#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002290 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002291 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002292 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002293 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002294 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002295 checking = 0;
2296 }
2297#endif
2298
2299 /* setup DISEED with what we have */
2300 diseed = (struct sli4_sge_diseed *) sgl;
2301 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2302 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2303
2304 /* Endianness conversion if necessary */
2305 diseed->ref_tag = cpu_to_le32(reftag);
2306 diseed->ref_tag_tran = diseed->ref_tag;
2307
James Smarta6887e22013-04-17 20:18:07 -04002308 /*
2309 * We only need to check the data on READs, for WRITEs
2310 * protection data is automatically generated, not checked.
2311 */
2312 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002313 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002314 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2315 else
2316 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2317
James Smart9c6aa9d2013-05-31 17:03:39 -04002318 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002319 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2320 else
2321 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2322 }
2323
James Smartacd68592012-01-18 16:25:09 -05002324 /* setup DISEED with the rest of the info */
2325 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2326 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002327
James Smartacd68592012-01-18 16:25:09 -05002328 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2329 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2330
2331 /* Endianness conversion if necessary for DISEED */
2332 diseed->word2 = cpu_to_le32(diseed->word2);
2333 diseed->word3 = cpu_to_le32(diseed->word3);
2334
2335 /* advance bpl and increment sge count */
2336 num_sge++;
2337 sgl++;
2338
2339 /* assumption: caller has already run dma_map_sg on command data */
2340 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2341 physaddr = sg_dma_address(sgde);
2342 dma_len = sg_dma_len(sgde);
2343 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2344 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2345 if ((i + 1) == datasegcnt)
2346 bf_set(lpfc_sli4_sge_last, sgl, 1);
2347 else
2348 bf_set(lpfc_sli4_sge_last, sgl, 0);
2349 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2350 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2351
2352 sgl->sge_len = cpu_to_le32(dma_len);
2353 dma_offset += dma_len;
2354
2355 sgl++;
2356 num_sge++;
2357 }
2358
2359out:
2360 return num_sge;
2361}
2362
2363/**
2364 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2365 * @phba: The Hba for which this call is being executed.
2366 * @sc: pointer to scsi command we're working on
2367 * @sgl: pointer to buffer list for protection groups
2368 * @datacnt: number of segments of data that have been dma mapped
2369 * @protcnt: number of segment of protection data that have been dma mapped
2370 *
2371 * This function sets up SGL buffer list for protection groups of
2372 * type LPFC_PG_TYPE_DIF
2373 *
2374 * This is usually used when DIFs are in their own buffers,
2375 * separate from the data. The HBA can then by instructed
2376 * to place the DIFs in the outgoing stream. For read operations,
2377 * The HBA could extract the DIFs and place it in DIF buffers.
2378 *
2379 * The buffer list for this type consists of one or more of the
2380 * protection groups described below:
2381 * +-------------------------+
2382 * start of first prot group --> | DISEED |
2383 * +-------------------------+
2384 * | DIF (Prot SGE) |
2385 * +-------------------------+
2386 * | Data SGE |
2387 * +-------------------------+
2388 * |more Data SGE's ... (opt)|
2389 * +-------------------------+
2390 * start of new prot group --> | DISEED |
2391 * +-------------------------+
2392 * | ... |
2393 * +-------------------------+
2394 *
2395 * Note: It is assumed that both data and protection s/g buffers have been
2396 * mapped for DMA
2397 *
2398 * Returns the number of SGEs added to the SGL.
2399 **/
2400static int
2401lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2402 struct sli4_sge *sgl, int datacnt, int protcnt)
2403{
2404 struct scatterlist *sgde = NULL; /* s/g data entry */
2405 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2406 struct sli4_sge_diseed *diseed = NULL;
2407 dma_addr_t dataphysaddr, protphysaddr;
2408 unsigned short curr_data = 0, curr_prot = 0;
2409 unsigned int split_offset;
2410 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2411 unsigned int protgrp_blks, protgrp_bytes;
2412 unsigned int remainder, subtotal;
2413 int status;
2414 unsigned char pgdone = 0, alldone = 0;
2415 unsigned blksize;
2416 uint32_t reftag;
2417 uint8_t txop, rxop;
2418 uint32_t dma_len;
James Smart0829a192012-05-09 21:18:12 -04002419#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002420 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002421#endif
James Smartacd68592012-01-18 16:25:09 -05002422 uint32_t checking = 1;
2423 uint32_t dma_offset = 0;
2424 int num_sge = 0;
2425
2426 sgpe = scsi_prot_sglist(sc);
2427 sgde = scsi_sglist(sc);
2428
2429 if (!sgpe || !sgde) {
2430 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2431 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2432 sgpe, sgde);
2433 return 0;
2434 }
2435
2436 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2437 if (status)
2438 goto out;
2439
2440 /* extract some info from the scsi command */
2441 blksize = lpfc_cmd_blksize(sc);
2442 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2443
2444#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002445 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002446 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002447 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002448 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002449 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002450 checking = 0;
2451 }
2452#endif
2453
2454 split_offset = 0;
2455 do {
James Smart96f70772013-04-17 20:16:15 -04002456 /* Check to see if we ran out of space */
2457 if (num_sge >= (phba->cfg_total_seg_cnt - 2))
2458 return num_sge + 3;
2459
James Smartacd68592012-01-18 16:25:09 -05002460 /* setup DISEED with what we have */
2461 diseed = (struct sli4_sge_diseed *) sgl;
2462 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2463 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2464
2465 /* Endianness conversion if necessary */
2466 diseed->ref_tag = cpu_to_le32(reftag);
2467 diseed->ref_tag_tran = diseed->ref_tag;
2468
James Smart9c6aa9d2013-05-31 17:03:39 -04002469 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD)) {
James Smarta6887e22013-04-17 20:18:07 -04002470 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2471
2472 } else {
2473 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2474 /*
2475 * When in this mode, the hardware will replace
2476 * the guard tag from the host with a
2477 * newly generated good CRC for the wire.
2478 * Switch to raw mode here to avoid this
2479 * behavior. What the host sends gets put on the wire.
2480 */
2481 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2482 txop = BG_OP_RAW_MODE;
2483 rxop = BG_OP_RAW_MODE;
2484 }
2485 }
2486
2487
James Smart9c6aa9d2013-05-31 17:03:39 -04002488 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002489 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2490 else
2491 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2492
James Smartacd68592012-01-18 16:25:09 -05002493 /* setup DISEED with the rest of the info */
2494 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2495 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002496
James Smartacd68592012-01-18 16:25:09 -05002497 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2498 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2499
2500 /* Endianness conversion if necessary for DISEED */
2501 diseed->word2 = cpu_to_le32(diseed->word2);
2502 diseed->word3 = cpu_to_le32(diseed->word3);
2503
2504 /* advance sgl and increment bde count */
2505 num_sge++;
2506 sgl++;
2507
2508 /* setup the first BDE that points to protection buffer */
2509 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2510 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2511
2512 /* must be integer multiple of the DIF block length */
2513 BUG_ON(protgroup_len % 8);
2514
2515 /* Now setup DIF SGE */
2516 sgl->word2 = 0;
2517 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2518 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2519 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2520 sgl->word2 = cpu_to_le32(sgl->word2);
2521
2522 protgrp_blks = protgroup_len / 8;
2523 protgrp_bytes = protgrp_blks * blksize;
2524
2525 /* check if DIF SGE is crossing the 4K boundary; if so split */
2526 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2527 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2528 protgroup_offset += protgroup_remainder;
2529 protgrp_blks = protgroup_remainder / 8;
2530 protgrp_bytes = protgrp_blks * blksize;
2531 } else {
2532 protgroup_offset = 0;
2533 curr_prot++;
2534 }
2535
2536 num_sge++;
2537
2538 /* setup SGE's for data blocks associated with DIF data */
2539 pgdone = 0;
2540 subtotal = 0; /* total bytes processed for current prot grp */
2541 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002542 /* Check to see if we ran out of space */
2543 if (num_sge >= phba->cfg_total_seg_cnt)
2544 return num_sge + 1;
2545
James Smartacd68592012-01-18 16:25:09 -05002546 if (!sgde) {
2547 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2548 "9086 BLKGRD:%s Invalid data segment\n",
2549 __func__);
2550 return 0;
2551 }
2552 sgl++;
2553 dataphysaddr = sg_dma_address(sgde) + split_offset;
2554
2555 remainder = sg_dma_len(sgde) - split_offset;
2556
2557 if ((subtotal + remainder) <= protgrp_bytes) {
2558 /* we can use this whole buffer */
2559 dma_len = remainder;
2560 split_offset = 0;
2561
2562 if ((subtotal + remainder) == protgrp_bytes)
2563 pgdone = 1;
2564 } else {
2565 /* must split this buffer with next prot grp */
2566 dma_len = protgrp_bytes - subtotal;
2567 split_offset += dma_len;
2568 }
2569
2570 subtotal += dma_len;
2571
2572 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2573 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2574 bf_set(lpfc_sli4_sge_last, sgl, 0);
2575 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2576 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2577
2578 sgl->sge_len = cpu_to_le32(dma_len);
2579 dma_offset += dma_len;
2580
2581 num_sge++;
2582 curr_data++;
2583
2584 if (split_offset)
2585 break;
2586
2587 /* Move to the next s/g segment if possible */
2588 sgde = sg_next(sgde);
2589 }
2590
2591 if (protgroup_offset) {
2592 /* update the reference tag */
2593 reftag += protgrp_blks;
2594 sgl++;
2595 continue;
2596 }
2597
2598 /* are we done ? */
2599 if (curr_prot == protcnt) {
2600 bf_set(lpfc_sli4_sge_last, sgl, 1);
2601 alldone = 1;
2602 } else if (curr_prot < protcnt) {
2603 /* advance to next prot buffer */
2604 sgpe = sg_next(sgpe);
2605 sgl++;
2606
2607 /* update the reference tag */
2608 reftag += protgrp_blks;
2609 } else {
2610 /* if we're here, we have a bug */
2611 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2612 "9085 BLKGRD: bug in %s\n", __func__);
2613 }
2614
2615 } while (!alldone);
2616
2617out:
2618
2619 return num_sge;
2620}
2621
2622/**
2623 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2624 * @phba: The Hba for which this call is being executed.
2625 * @sc: pointer to scsi command we're working on
2626 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002627 * Given a SCSI command that supports DIF, determine composition of protection
2628 * groups involved in setting up buffer lists
2629 *
James Smartacd68592012-01-18 16:25:09 -05002630 * Returns: Protection group type (with or without DIF)
2631 *
2632 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002633static int
2634lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2635{
2636 int ret = LPFC_PG_TYPE_INVALID;
2637 unsigned char op = scsi_get_prot_op(sc);
2638
2639 switch (op) {
2640 case SCSI_PROT_READ_STRIP:
2641 case SCSI_PROT_WRITE_INSERT:
2642 ret = LPFC_PG_TYPE_NO_DIF;
2643 break;
2644 case SCSI_PROT_READ_INSERT:
2645 case SCSI_PROT_WRITE_STRIP:
2646 case SCSI_PROT_READ_PASS:
2647 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002648 ret = LPFC_PG_TYPE_DIF_BUF;
2649 break;
2650 default:
James Smart9c6aa9d2013-05-31 17:03:39 -04002651 if (phba)
2652 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2653 "9021 Unsupported protection op:%d\n",
2654 op);
James Smarte2a0a9d2008-12-04 22:40:02 -05002655 break;
2656 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002657 return ret;
2658}
2659
James Smartacd68592012-01-18 16:25:09 -05002660/**
James Smarta6887e22013-04-17 20:18:07 -04002661 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2662 * @phba: The Hba for which this call is being executed.
2663 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2664 *
2665 * Adjust the data length to account for how much data
2666 * is actually on the wire.
2667 *
2668 * returns the adjusted data length
2669 **/
2670static int
2671lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
2672 struct lpfc_scsi_buf *lpfc_cmd)
2673{
2674 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2675 int fcpdl;
2676
2677 fcpdl = scsi_bufflen(sc);
2678
2679 /* Check if there is protection data on the wire */
2680 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002681 /* Read check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002682 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2683 return fcpdl;
2684
2685 } else {
James Smart9c6aa9d2013-05-31 17:03:39 -04002686 /* Write check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002687 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2688 return fcpdl;
2689 }
2690
2691 /*
2692 * If we are in DIF Type 1 mode every data block has a 8 byte
James Smart9c6aa9d2013-05-31 17:03:39 -04002693 * DIF (trailer) attached to it. Must ajust FCP data length
2694 * to account for the protection data.
James Smarta6887e22013-04-17 20:18:07 -04002695 */
James Smart9c6aa9d2013-05-31 17:03:39 -04002696 fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
James Smarta6887e22013-04-17 20:18:07 -04002697
2698 return fcpdl;
2699}
2700
2701/**
James Smartacd68592012-01-18 16:25:09 -05002702 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2703 * @phba: The Hba for which this call is being executed.
2704 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2705 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002706 * This is the protection/DIF aware version of
2707 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2708 * two functions eventually, but for now, it's here
James Smartacd68592012-01-18 16:25:09 -05002709 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002710static int
James Smartacd68592012-01-18 16:25:09 -05002711lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smarte2a0a9d2008-12-04 22:40:02 -05002712 struct lpfc_scsi_buf *lpfc_cmd)
2713{
2714 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2715 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2716 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2717 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2718 uint32_t num_bde = 0;
2719 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2720 int prot_group_type = 0;
James Smarta6887e22013-04-17 20:18:07 -04002721 int fcpdl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002722
2723 /*
2724 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2725 * fcp_rsp regions to the first data bde entry
2726 */
2727 bpl += 2;
2728 if (scsi_sg_count(scsi_cmnd)) {
2729 /*
2730 * The driver stores the segment count returned from pci_map_sg
2731 * because this a count of dma-mappings used to map the use_sg
2732 * pages. They are not guaranteed to be the same for those
2733 * architectures that implement an IOMMU.
2734 */
2735 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2736 scsi_sglist(scsi_cmnd),
2737 scsi_sg_count(scsi_cmnd), datadir);
2738 if (unlikely(!datasegcnt))
2739 return 1;
2740
2741 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04002742
2743 /* First check if data segment count from SCSI Layer is good */
2744 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
2745 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002746
2747 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2748
2749 switch (prot_group_type) {
2750 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04002751
2752 /* Here we need to add a PDE5 and PDE6 to the count */
2753 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt)
2754 goto err;
2755
James Smarte2a0a9d2008-12-04 22:40:02 -05002756 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2757 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002758 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002759 if (num_bde < 2)
2760 goto err;
2761 break;
James Smart96f70772013-04-17 20:16:15 -04002762
2763 case LPFC_PG_TYPE_DIF_BUF:
James Smarte2a0a9d2008-12-04 22:40:02 -05002764 /*
2765 * This type indicates that protection buffers are
2766 * passed to the driver, so that needs to be prepared
2767 * for DMA
2768 */
2769 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2770 scsi_prot_sglist(scsi_cmnd),
2771 scsi_prot_sg_count(scsi_cmnd), datadir);
2772 if (unlikely(!protsegcnt)) {
2773 scsi_dma_unmap(scsi_cmnd);
2774 return 1;
2775 }
2776
2777 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04002778
2779 /*
2780 * There is a minimun of 4 BPLs used for every
2781 * protection data segment.
2782 */
2783 if ((lpfc_cmd->prot_seg_cnt * 4) >
2784 (phba->cfg_total_seg_cnt - 2))
2785 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002786
2787 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2788 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002789 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04002790 if ((num_bde < 3) ||
2791 (num_bde > phba->cfg_total_seg_cnt))
James Smarte2a0a9d2008-12-04 22:40:02 -05002792 goto err;
2793 break;
James Smart96f70772013-04-17 20:16:15 -04002794
James Smarte2a0a9d2008-12-04 22:40:02 -05002795 case LPFC_PG_TYPE_INVALID:
2796 default:
James Smart96f70772013-04-17 20:16:15 -04002797 scsi_dma_unmap(scsi_cmnd);
2798 lpfc_cmd->seg_cnt = 0;
2799
James Smarte2a0a9d2008-12-04 22:40:02 -05002800 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2801 "9022 Unexpected protection group %i\n",
2802 prot_group_type);
2803 return 1;
2804 }
2805 }
2806
2807 /*
2808 * Finish initializing those IOCB fields that are dependent on the
2809 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2810 * reinitialized since all iocb memory resources are used many times
2811 * for transmit, receive, and continuation bpl's.
2812 */
2813 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2814 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2815 iocb_cmd->ulpBdeCount = 1;
2816 iocb_cmd->ulpLe = 1;
2817
James Smarta6887e22013-04-17 20:18:07 -04002818 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002819 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2820
2821 /*
2822 * Due to difference in data length between DIF/non-DIF paths,
2823 * we need to set word 4 of IOCB here
2824 */
2825 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2826
2827 return 0;
2828err:
James Smart96f70772013-04-17 20:16:15 -04002829 if (lpfc_cmd->seg_cnt)
2830 scsi_dma_unmap(scsi_cmnd);
2831 if (lpfc_cmd->prot_seg_cnt)
2832 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2833 scsi_prot_sg_count(scsi_cmnd),
2834 scsi_cmnd->sc_data_direction);
2835
James Smarte2a0a9d2008-12-04 22:40:02 -05002836 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04002837 "9023 Cannot setup S/G List for HBA"
2838 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
2839 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
2840 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
James Smarte2a0a9d2008-12-04 22:40:02 -05002841 prot_group_type, num_bde);
James Smart96f70772013-04-17 20:16:15 -04002842
2843 lpfc_cmd->seg_cnt = 0;
2844 lpfc_cmd->prot_seg_cnt = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05002845 return 1;
2846}
2847
2848/*
James Smart737d4242013-04-17 20:14:49 -04002849 * This function calcuates the T10 DIF guard tag
2850 * on the specified data using a CRC algorithmn
2851 * using crc_t10dif.
2852 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002853static uint16_t
James Smart737d4242013-04-17 20:14:49 -04002854lpfc_bg_crc(uint8_t *data, int count)
2855{
2856 uint16_t crc = 0;
2857 uint16_t x;
2858
2859 crc = crc_t10dif(data, count);
2860 x = cpu_to_be16(crc);
2861 return x;
2862}
2863
2864/*
2865 * This function calcuates the T10 DIF guard tag
2866 * on the specified data using a CSUM algorithmn
2867 * using ip_compute_csum.
2868 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002869static uint16_t
James Smart737d4242013-04-17 20:14:49 -04002870lpfc_bg_csum(uint8_t *data, int count)
2871{
2872 uint16_t ret;
2873
2874 ret = ip_compute_csum(data, count);
2875 return ret;
2876}
2877
2878/*
2879 * This function examines the protection data to try to determine
2880 * what type of T10-DIF error occurred.
2881 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002882static void
James Smart737d4242013-04-17 20:14:49 -04002883lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2884{
2885 struct scatterlist *sgpe; /* s/g prot entry */
2886 struct scatterlist *sgde; /* s/g data entry */
2887 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2888 struct scsi_dif_tuple *src = NULL;
2889 uint8_t *data_src = NULL;
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -04002890 uint16_t guard_tag;
James Smart737d4242013-04-17 20:14:49 -04002891 uint16_t start_app_tag, app_tag;
2892 uint32_t start_ref_tag, ref_tag;
2893 int prot, protsegcnt;
2894 int err_type, len, data_len;
2895 int chk_ref, chk_app, chk_guard;
2896 uint16_t sum;
2897 unsigned blksize;
2898
2899 err_type = BGS_GUARD_ERR_MASK;
2900 sum = 0;
2901 guard_tag = 0;
2902
2903 /* First check to see if there is protection data to examine */
2904 prot = scsi_get_prot_op(cmd);
2905 if ((prot == SCSI_PROT_READ_STRIP) ||
2906 (prot == SCSI_PROT_WRITE_INSERT) ||
2907 (prot == SCSI_PROT_NORMAL))
2908 goto out;
2909
2910 /* Currently the driver just supports ref_tag and guard_tag checking */
2911 chk_ref = 1;
2912 chk_app = 0;
2913 chk_guard = 0;
2914
2915 /* Setup a ptr to the protection data provided by the SCSI host */
2916 sgpe = scsi_prot_sglist(cmd);
2917 protsegcnt = lpfc_cmd->prot_seg_cnt;
2918
2919 if (sgpe && protsegcnt) {
2920
2921 /*
2922 * We will only try to verify guard tag if the segment
2923 * data length is a multiple of the blksize.
2924 */
2925 sgde = scsi_sglist(cmd);
2926 blksize = lpfc_cmd_blksize(cmd);
2927 data_src = (uint8_t *)sg_virt(sgde);
2928 data_len = sgde->length;
2929 if ((data_len & (blksize - 1)) == 0)
2930 chk_guard = 1;
James Smart737d4242013-04-17 20:14:49 -04002931
James Smarte85d8f92013-05-31 17:04:10 -04002932 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
James Smarta6887e22013-04-17 20:18:07 -04002933 start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
James Smart737d4242013-04-17 20:14:49 -04002934 start_app_tag = src->app_tag;
James Smart737d4242013-04-17 20:14:49 -04002935 len = sgpe->length;
2936 while (src && protsegcnt) {
2937 while (len) {
2938
2939 /*
2940 * First check to see if a protection data
2941 * check is valid
2942 */
Dmitry Monakhov128b6f92017-06-29 11:31:12 -07002943 if ((src->ref_tag == T10_PI_REF_ESCAPE) ||
2944 (src->app_tag == T10_PI_APP_ESCAPE)) {
James Smart737d4242013-04-17 20:14:49 -04002945 start_ref_tag++;
2946 goto skipit;
2947 }
2948
James Smart9c6aa9d2013-05-31 17:03:39 -04002949 /* First Guard Tag checking */
2950 if (chk_guard) {
2951 guard_tag = src->guard_tag;
2952 if (lpfc_cmd_guard_csum(cmd))
2953 sum = lpfc_bg_csum(data_src,
2954 blksize);
2955 else
2956 sum = lpfc_bg_crc(data_src,
2957 blksize);
2958 if ((guard_tag != sum)) {
2959 err_type = BGS_GUARD_ERR_MASK;
2960 goto out;
2961 }
James Smart737d4242013-04-17 20:14:49 -04002962 }
2963
2964 /* Reference Tag checking */
2965 ref_tag = be32_to_cpu(src->ref_tag);
2966 if (chk_ref && (ref_tag != start_ref_tag)) {
2967 err_type = BGS_REFTAG_ERR_MASK;
2968 goto out;
2969 }
2970 start_ref_tag++;
2971
James Smart9c6aa9d2013-05-31 17:03:39 -04002972 /* App Tag checking */
2973 app_tag = src->app_tag;
2974 if (chk_app && (app_tag != start_app_tag)) {
2975 err_type = BGS_APPTAG_ERR_MASK;
2976 goto out;
James Smart737d4242013-04-17 20:14:49 -04002977 }
2978skipit:
2979 len -= sizeof(struct scsi_dif_tuple);
2980 if (len < 0)
2981 len = 0;
2982 src++;
2983
2984 data_src += blksize;
2985 data_len -= blksize;
2986
2987 /*
2988 * Are we at the end of the Data segment?
2989 * The data segment is only used for Guard
2990 * tag checking.
2991 */
2992 if (chk_guard && (data_len == 0)) {
2993 chk_guard = 0;
2994 sgde = sg_next(sgde);
2995 if (!sgde)
2996 goto out;
2997
2998 data_src = (uint8_t *)sg_virt(sgde);
2999 data_len = sgde->length;
3000 if ((data_len & (blksize - 1)) == 0)
3001 chk_guard = 1;
3002 }
3003 }
3004
3005 /* Goto the next Protection data segment */
3006 sgpe = sg_next(sgpe);
3007 if (sgpe) {
3008 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
3009 len = sgpe->length;
3010 } else {
3011 src = NULL;
3012 }
3013 protsegcnt--;
3014 }
3015 }
3016out:
3017 if (err_type == BGS_GUARD_ERR_MASK) {
3018 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3019 0x10, 0x1);
3020 cmd->result = DRIVER_SENSE << 24
3021 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3022 phba->bg_guard_err_cnt++;
3023 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3024 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
3025 (unsigned long)scsi_get_lba(cmd),
3026 sum, guard_tag);
3027
3028 } else if (err_type == BGS_REFTAG_ERR_MASK) {
3029 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3030 0x10, 0x3);
3031 cmd->result = DRIVER_SENSE << 24
3032 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3033
3034 phba->bg_reftag_err_cnt++;
3035 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3036 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
3037 (unsigned long)scsi_get_lba(cmd),
3038 ref_tag, start_ref_tag);
3039
3040 } else if (err_type == BGS_APPTAG_ERR_MASK) {
3041 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3042 0x10, 0x2);
3043 cmd->result = DRIVER_SENSE << 24
3044 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3045
3046 phba->bg_apptag_err_cnt++;
3047 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3048 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
3049 (unsigned long)scsi_get_lba(cmd),
3050 app_tag, start_app_tag);
3051 }
3052}
3053
3054
3055/*
James Smarte2a0a9d2008-12-04 22:40:02 -05003056 * This function checks for BlockGuard errors detected by
3057 * the HBA. In case of errors, the ASC/ASCQ fields in the
3058 * sense buffer will be set accordingly, paired with
3059 * ILLEGAL_REQUEST to signal to the kernel that the HBA
3060 * detected corruption.
3061 *
3062 * Returns:
3063 * 0 - No error found
3064 * 1 - BlockGuard error found
3065 * -1 - Internal error (bad profile, ...etc)
3066 */
3067static int
3068lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
3069 struct lpfc_iocbq *pIocbOut)
3070{
3071 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3072 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
3073 int ret = 0;
3074 uint32_t bghm = bgf->bghm;
3075 uint32_t bgstat = bgf->bgstat;
3076 uint64_t failing_sector = 0;
3077
James Smarte2a0a9d2008-12-04 22:40:02 -05003078 spin_lock(&_dump_buf_lock);
3079 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04003080 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
3081 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05003082 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04003083 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003084
3085 /* If we have a prot sgl, save the DIF buffer */
3086 if (lpfc_prot_group_type(phba, cmd) ==
3087 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04003088 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
3089 "Saving DIF for %u blocks to debugfs\n",
3090 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
3091 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003092 }
3093
3094 _dump_buf_done = 1;
3095 }
3096 spin_unlock(&_dump_buf_lock);
3097
3098 if (lpfc_bgs_get_invalid_prof(bgstat)) {
3099 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart737d4242013-04-17 20:14:49 -04003100 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3101 "9072 BLKGRD: Invalid BG Profile in cmd"
3102 " 0x%x lba 0x%llx blk cnt 0x%x "
3103 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3104 (unsigned long long)scsi_get_lba(cmd),
3105 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003106 ret = (-1);
3107 goto out;
3108 }
3109
3110 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
3111 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart737d4242013-04-17 20:14:49 -04003112 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3113 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
3114 " 0x%x lba 0x%llx blk cnt 0x%x "
3115 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3116 (unsigned long long)scsi_get_lba(cmd),
3117 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003118 ret = (-1);
3119 goto out;
3120 }
3121
3122 if (lpfc_bgs_get_guard_err(bgstat)) {
3123 ret = 1;
3124
3125 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3126 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003127 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003128 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3129 phba->bg_guard_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003130 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3131 "9055 BLKGRD: Guard Tag error in cmd"
3132 " 0x%x lba 0x%llx blk cnt 0x%x "
3133 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3134 (unsigned long long)scsi_get_lba(cmd),
3135 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003136 }
3137
3138 if (lpfc_bgs_get_reftag_err(bgstat)) {
3139 ret = 1;
3140
3141 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3142 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003143 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003144 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3145
3146 phba->bg_reftag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003147 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3148 "9056 BLKGRD: Ref Tag error in cmd"
3149 " 0x%x lba 0x%llx blk cnt 0x%x "
3150 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3151 (unsigned long long)scsi_get_lba(cmd),
3152 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003153 }
3154
3155 if (lpfc_bgs_get_apptag_err(bgstat)) {
3156 ret = 1;
3157
3158 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3159 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003160 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003161 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3162
3163 phba->bg_apptag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003164 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3165 "9061 BLKGRD: App Tag error in cmd"
3166 " 0x%x lba 0x%llx blk cnt 0x%x "
3167 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3168 (unsigned long long)scsi_get_lba(cmd),
3169 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003170 }
3171
3172 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
3173 /*
3174 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04003175 * field, and put the failing LBA in it.
3176 * This code assumes there was also a guard/app/ref tag error
3177 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05003178 */
James Smart7c56b9f2011-07-22 18:36:25 -04003179 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
3180 cmd->sense_buffer[8] = 0; /* Information descriptor type */
3181 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
3182 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05003183
3184 /* bghm is a "on the wire" FC frame based count */
3185 switch (scsi_get_prot_op(cmd)) {
3186 case SCSI_PROT_READ_INSERT:
3187 case SCSI_PROT_WRITE_STRIP:
3188 bghm /= cmd->device->sector_size;
3189 break;
3190 case SCSI_PROT_READ_STRIP:
3191 case SCSI_PROT_WRITE_INSERT:
3192 case SCSI_PROT_READ_PASS:
3193 case SCSI_PROT_WRITE_PASS:
3194 bghm /= (cmd->device->sector_size +
3195 sizeof(struct scsi_dif_tuple));
3196 break;
3197 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003198
3199 failing_sector = scsi_get_lba(cmd);
3200 failing_sector += bghm;
3201
James Smart7c56b9f2011-07-22 18:36:25 -04003202 /* Descriptor Information */
3203 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05003204 }
3205
3206 if (!ret) {
3207 /* No error was reported - problem in FW? */
James Smart737d4242013-04-17 20:14:49 -04003208 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3209 "9057 BLKGRD: Unknown error in cmd"
3210 " 0x%x lba 0x%llx blk cnt 0x%x "
3211 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3212 (unsigned long long)scsi_get_lba(cmd),
3213 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003214
James Smart737d4242013-04-17 20:14:49 -04003215 /* Calcuate what type of error it was */
3216 lpfc_calc_bg_err(phba, lpfc_cmd);
3217 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003218out:
3219 return ret;
3220}
3221
James Smartea2151b2008-09-07 11:52:10 -04003222/**
James Smartda0436e2009-05-22 14:51:39 -04003223 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3224 * @phba: The Hba for which this call is being executed.
3225 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3226 *
3227 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3228 * field of @lpfc_cmd for device with SLI-4 interface spec.
3229 *
3230 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003231 * 1 - Error
3232 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04003233 **/
3234static int
3235lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3236{
3237 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3238 struct scatterlist *sgel = NULL;
3239 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3240 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
James Smartfedd3b72011-02-16 12:39:24 -05003241 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04003242 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3243 dma_addr_t physaddr;
3244 uint32_t num_bde = 0;
3245 uint32_t dma_len;
3246 uint32_t dma_offset = 0;
3247 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05003248 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04003249
3250 /*
3251 * There are three possibilities here - use scatter-gather segment, use
3252 * the single mapping, or neither. Start the lpfc command prep by
3253 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3254 * data bde entry.
3255 */
3256 if (scsi_sg_count(scsi_cmnd)) {
3257 /*
3258 * The driver stores the segment count returned from pci_map_sg
3259 * because this a count of dma-mappings used to map the use_sg
3260 * pages. They are not guaranteed to be the same for those
3261 * architectures that implement an IOMMU.
3262 */
3263
3264 nseg = scsi_dma_map(scsi_cmnd);
James Smart5116fbf2015-05-22 10:42:39 -04003265 if (unlikely(nseg <= 0))
James Smartda0436e2009-05-22 14:51:39 -04003266 return 1;
3267 sgl += 1;
3268 /* clear the last flag in the fcp_rsp map entry */
3269 sgl->word2 = le32_to_cpu(sgl->word2);
3270 bf_set(lpfc_sli4_sge_last, sgl, 0);
3271 sgl->word2 = cpu_to_le32(sgl->word2);
3272 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05003273 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04003274 lpfc_cmd->seg_cnt = nseg;
3275 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04003276 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3277 " %s: Too many sg segments from "
3278 "dma_map_sg. Config %d, seg_cnt %d\n",
3279 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04003280 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04003281 lpfc_cmd->seg_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04003282 scsi_dma_unmap(scsi_cmnd);
3283 return 1;
3284 }
3285
3286 /*
3287 * The driver established a maximum scatter-gather segment count
3288 * during probe that limits the number of sg elements in any
3289 * single scsi command. Just run through the seg_cnt and format
3290 * the sge's.
3291 * When using SLI-3 the driver will try to fit all the BDEs into
3292 * the IOCB. If it can't then the BDEs get added to a BPL as it
3293 * does for SLI-2 mode.
3294 */
3295 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3296 physaddr = sg_dma_address(sgel);
3297 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04003298 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3299 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04003300 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04003301 if ((num_bde + 1) == nseg)
3302 bf_set(lpfc_sli4_sge_last, sgl, 1);
3303 else
3304 bf_set(lpfc_sli4_sge_last, sgl, 0);
3305 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04003306 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04003307 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05003308 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04003309 dma_offset += dma_len;
3310 sgl++;
3311 }
James Smart0bc2b7c2018-02-22 08:18:48 -08003312 /*
3313 * Setup the first Payload BDE. For FCoE we just key off
3314 * Performance Hints, for FC we utilize fcp_embed_pbde.
3315 */
3316 if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
3317 phba->fcp_embed_pbde) {
James Smartfedd3b72011-02-16 12:39:24 -05003318 bde = (struct ulp_bde64 *)
3319 &(iocb_cmd->unsli3.sli3Words[5]);
3320 bde->addrLow = first_data_sgl->addr_lo;
3321 bde->addrHigh = first_data_sgl->addr_hi;
3322 bde->tus.f.bdeSize =
3323 le32_to_cpu(first_data_sgl->sge_len);
3324 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3325 bde->tus.w = cpu_to_le32(bde->tus.w);
3326 }
James Smartda0436e2009-05-22 14:51:39 -04003327 } else {
3328 sgl += 1;
3329 /* clear the last flag in the fcp_rsp map entry */
3330 sgl->word2 = le32_to_cpu(sgl->word2);
3331 bf_set(lpfc_sli4_sge_last, sgl, 1);
3332 sgl->word2 = cpu_to_le32(sgl->word2);
3333 }
3334
3335 /*
3336 * Finish initializing those IOCB fields that are dependent on the
3337 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3338 * explicitly reinitialized.
3339 * all iocb memory resources are reused.
3340 */
3341 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3342
3343 /*
3344 * Due to difference in data length between DIF/non-DIF paths,
3345 * we need to set word 4 of IOCB here
3346 */
3347 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
James Smart1ba981f2014-02-20 09:56:45 -05003348
3349 /*
3350 * If the OAS driver feature is enabled and the lun is enabled for
3351 * OAS, set the oas iocb related flags.
3352 */
James Smartf38fa0b2014-04-04 13:52:21 -04003353 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
James Smartc92c8412016-07-06 12:36:05 -07003354 scsi_cmnd->device->hostdata)->oas_enabled) {
James Smart9bd2bff52014-09-03 12:57:30 -04003355 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
James Smartc92c8412016-07-06 12:36:05 -07003356 lpfc_cmd->cur_iocbq.priority = ((struct lpfc_device_data *)
3357 scsi_cmnd->device->hostdata)->priority;
3358 }
James Smartda0436e2009-05-22 14:51:39 -04003359 return 0;
3360}
3361
3362/**
James Smartacd68592012-01-18 16:25:09 -05003363 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3364 * @phba: The Hba for which this call is being executed.
3365 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3366 *
3367 * This is the protection/DIF aware version of
3368 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3369 * two functions eventually, but for now, it's here
3370 **/
3371static int
3372lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3373 struct lpfc_scsi_buf *lpfc_cmd)
3374{
3375 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3376 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3377 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3378 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart96f70772013-04-17 20:16:15 -04003379 uint32_t num_sge = 0;
James Smartacd68592012-01-18 16:25:09 -05003380 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3381 int prot_group_type = 0;
3382 int fcpdl;
3383
3384 /*
3385 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
James Smart96f70772013-04-17 20:16:15 -04003386 * fcp_rsp regions to the first data sge entry
James Smartacd68592012-01-18 16:25:09 -05003387 */
3388 if (scsi_sg_count(scsi_cmnd)) {
3389 /*
3390 * The driver stores the segment count returned from pci_map_sg
3391 * because this a count of dma-mappings used to map the use_sg
3392 * pages. They are not guaranteed to be the same for those
3393 * architectures that implement an IOMMU.
3394 */
3395 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3396 scsi_sglist(scsi_cmnd),
3397 scsi_sg_count(scsi_cmnd), datadir);
3398 if (unlikely(!datasegcnt))
3399 return 1;
3400
3401 sgl += 1;
3402 /* clear the last flag in the fcp_rsp map entry */
3403 sgl->word2 = le32_to_cpu(sgl->word2);
3404 bf_set(lpfc_sli4_sge_last, sgl, 0);
3405 sgl->word2 = cpu_to_le32(sgl->word2);
3406
3407 sgl += 1;
3408 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04003409
3410 /* First check if data segment count from SCSI Layer is good */
3411 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
3412 goto err;
James Smartacd68592012-01-18 16:25:09 -05003413
3414 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3415
3416 switch (prot_group_type) {
3417 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04003418 /* Here we need to add a DISEED to the count */
3419 if ((lpfc_cmd->seg_cnt + 1) > phba->cfg_total_seg_cnt)
3420 goto err;
3421
3422 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003423 datasegcnt);
James Smart96f70772013-04-17 20:16:15 -04003424
James Smartacd68592012-01-18 16:25:09 -05003425 /* we should have 2 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003426 if (num_sge < 2)
James Smartacd68592012-01-18 16:25:09 -05003427 goto err;
3428 break;
James Smart96f70772013-04-17 20:16:15 -04003429
3430 case LPFC_PG_TYPE_DIF_BUF:
James Smartacd68592012-01-18 16:25:09 -05003431 /*
3432 * This type indicates that protection buffers are
3433 * passed to the driver, so that needs to be prepared
3434 * for DMA
3435 */
3436 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3437 scsi_prot_sglist(scsi_cmnd),
3438 scsi_prot_sg_count(scsi_cmnd), datadir);
3439 if (unlikely(!protsegcnt)) {
3440 scsi_dma_unmap(scsi_cmnd);
3441 return 1;
3442 }
3443
3444 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04003445 /*
3446 * There is a minimun of 3 SGEs used for every
3447 * protection data segment.
3448 */
3449 if ((lpfc_cmd->prot_seg_cnt * 3) >
3450 (phba->cfg_total_seg_cnt - 2))
3451 goto err;
James Smartacd68592012-01-18 16:25:09 -05003452
James Smart96f70772013-04-17 20:16:15 -04003453 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003454 datasegcnt, protsegcnt);
James Smart96f70772013-04-17 20:16:15 -04003455
James Smartacd68592012-01-18 16:25:09 -05003456 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003457 if ((num_sge < 3) ||
3458 (num_sge > phba->cfg_total_seg_cnt))
James Smartacd68592012-01-18 16:25:09 -05003459 goto err;
3460 break;
James Smart96f70772013-04-17 20:16:15 -04003461
James Smartacd68592012-01-18 16:25:09 -05003462 case LPFC_PG_TYPE_INVALID:
3463 default:
James Smart96f70772013-04-17 20:16:15 -04003464 scsi_dma_unmap(scsi_cmnd);
3465 lpfc_cmd->seg_cnt = 0;
3466
James Smartacd68592012-01-18 16:25:09 -05003467 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3468 "9083 Unexpected protection group %i\n",
3469 prot_group_type);
3470 return 1;
3471 }
3472 }
3473
James Smart8012cc32012-10-31 14:44:49 -04003474 switch (scsi_get_prot_op(scsi_cmnd)) {
3475 case SCSI_PROT_WRITE_STRIP:
3476 case SCSI_PROT_READ_STRIP:
3477 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3478 break;
3479 case SCSI_PROT_WRITE_INSERT:
3480 case SCSI_PROT_READ_INSERT:
3481 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3482 break;
3483 case SCSI_PROT_WRITE_PASS:
3484 case SCSI_PROT_READ_PASS:
3485 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3486 break;
3487 }
3488
James Smartacd68592012-01-18 16:25:09 -05003489 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smartacd68592012-01-18 16:25:09 -05003490 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3491
3492 /*
3493 * Due to difference in data length between DIF/non-DIF paths,
3494 * we need to set word 4 of IOCB here
3495 */
3496 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
James Smartacd68592012-01-18 16:25:09 -05003497
James Smart9bd2bff52014-09-03 12:57:30 -04003498 /*
3499 * If the OAS driver feature is enabled and the lun is enabled for
3500 * OAS, set the oas iocb related flags.
3501 */
3502 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
3503 scsi_cmnd->device->hostdata)->oas_enabled)
3504 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
3505
James Smartacd68592012-01-18 16:25:09 -05003506 return 0;
3507err:
James Smart96f70772013-04-17 20:16:15 -04003508 if (lpfc_cmd->seg_cnt)
3509 scsi_dma_unmap(scsi_cmnd);
3510 if (lpfc_cmd->prot_seg_cnt)
3511 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3512 scsi_prot_sg_count(scsi_cmnd),
3513 scsi_cmnd->sc_data_direction);
3514
James Smartacd68592012-01-18 16:25:09 -05003515 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04003516 "9084 Cannot setup S/G List for HBA"
3517 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3518 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3519 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3520 prot_group_type, num_sge);
3521
3522 lpfc_cmd->seg_cnt = 0;
3523 lpfc_cmd->prot_seg_cnt = 0;
James Smartacd68592012-01-18 16:25:09 -05003524 return 1;
3525}
3526
3527/**
James Smart3772a992009-05-22 14:50:54 -04003528 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3529 * @phba: The Hba for which this call is being executed.
3530 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3531 *
3532 * This routine wraps the actual DMA mapping function pointer from the
3533 * lpfc_hba struct.
3534 *
3535 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003536 * 1 - Error
3537 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003538 **/
3539static inline int
3540lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3541{
3542 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3543}
3544
3545/**
James Smartacd68592012-01-18 16:25:09 -05003546 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3547 * using BlockGuard.
3548 * @phba: The Hba for which this call is being executed.
3549 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3550 *
3551 * This routine wraps the actual DMA mapping function pointer from the
3552 * lpfc_hba struct.
3553 *
3554 * Return codes:
3555 * 1 - Error
3556 * 0 - Success
3557 **/
3558static inline int
3559lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3560{
3561 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3562}
3563
3564/**
James Smart3621a712009-04-06 18:47:14 -04003565 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003566 * @phba: Pointer to hba context object.
3567 * @vport: Pointer to vport object.
3568 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3569 * @rsp_iocb: Pointer to response iocb object which reported error.
3570 *
3571 * This function posts an event when there is a SCSI command reporting
3572 * error from the scsi device.
3573 **/
3574static void
3575lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3576 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3577 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3578 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3579 uint32_t resp_info = fcprsp->rspStatus2;
3580 uint32_t scsi_status = fcprsp->rspStatus3;
3581 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3582 struct lpfc_fast_path_event *fast_path_evt = NULL;
3583 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3584 unsigned long flags;
3585
James Smart5989b8d2010-10-22 11:06:56 -04003586 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3587 return;
3588
James Smartea2151b2008-09-07 11:52:10 -04003589 /* If there is queuefull or busy condition send a scsi event */
3590 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3591 (cmnd->result == SAM_STAT_BUSY)) {
3592 fast_path_evt = lpfc_alloc_fast_evt(phba);
3593 if (!fast_path_evt)
3594 return;
3595 fast_path_evt->un.scsi_evt.event_type =
3596 FC_REG_SCSI_EVENT;
3597 fast_path_evt->un.scsi_evt.subcategory =
3598 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3599 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3600 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3601 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3602 &pnode->nlp_portname, sizeof(struct lpfc_name));
3603 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3604 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3605 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3606 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3607 fast_path_evt = lpfc_alloc_fast_evt(phba);
3608 if (!fast_path_evt)
3609 return;
3610 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3611 FC_REG_SCSI_EVENT;
3612 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3613 LPFC_EVENT_CHECK_COND;
3614 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3615 cmnd->device->lun;
3616 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3617 &pnode->nlp_portname, sizeof(struct lpfc_name));
3618 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3619 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3620 fast_path_evt->un.check_cond_evt.sense_key =
3621 cmnd->sense_buffer[2] & 0xf;
3622 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3623 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3624 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3625 fcpi_parm &&
3626 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3627 ((scsi_status == SAM_STAT_GOOD) &&
3628 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3629 /*
3630 * If status is good or resid does not match with fcp_param and
3631 * there is valid fcpi_parm, then there is a read_check error
3632 */
3633 fast_path_evt = lpfc_alloc_fast_evt(phba);
3634 if (!fast_path_evt)
3635 return;
3636 fast_path_evt->un.read_check_error.header.event_type =
3637 FC_REG_FABRIC_EVENT;
3638 fast_path_evt->un.read_check_error.header.subcategory =
3639 LPFC_EVENT_FCPRDCHKERR;
3640 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3641 &pnode->nlp_portname, sizeof(struct lpfc_name));
3642 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3643 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3644 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3645 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3646 fast_path_evt->un.read_check_error.fcpiparam =
3647 fcpi_parm;
3648 } else
3649 return;
3650
3651 fast_path_evt->vport = vport;
3652 spin_lock_irqsave(&phba->hbalock, flags);
3653 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3654 spin_unlock_irqrestore(&phba->hbalock, flags);
3655 lpfc_worker_wake_up(phba);
3656 return;
3657}
James Smart9bad7672008-12-04 22:39:02 -05003658
3659/**
James Smartf1126682009-06-10 17:22:44 -04003660 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003661 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003662 * @psb: The scsi buffer which is going to be un-mapped.
3663 *
3664 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003665 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003666 **/
dea31012005-04-17 16:05:31 -05003667static void
James Smartf1126682009-06-10 17:22:44 -04003668lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003669{
3670 /*
3671 * There are only two special cases to consider. (1) the scsi command
3672 * requested scatter-gather usage or (2) the scsi command allocated
3673 * a request buffer, but did not request use_sg. There is a third
3674 * case, but it does not require resource deallocation.
3675 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003676 if (psb->seg_cnt > 0)
3677 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003678 if (psb->prot_seg_cnt > 0)
3679 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3680 scsi_prot_sg_count(psb->pCmd),
3681 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003682}
3683
James Smart9bad7672008-12-04 22:39:02 -05003684/**
James Smart3621a712009-04-06 18:47:14 -04003685 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003686 * @vport: The virtual port for which this call is being executed.
3687 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3688 * @rsp_iocb: The response IOCB which contains FCP error.
3689 *
3690 * This routine is called to process response IOCB with status field
3691 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3692 * based upon SCSI and FCP error.
3693 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003694static void
James Smart2e0fef82007-06-17 19:56:36 -05003695lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3696 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003697{
James Smart5afab6b2015-12-16 18:12:01 -05003698 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003699 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3700 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3701 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003702 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003703 uint32_t resp_info = fcprsp->rspStatus2;
3704 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003705 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003706 uint32_t host_status = DID_OK;
3707 uint32_t rsplen = 0;
James Smart5afab6b2015-12-16 18:12:01 -05003708 uint32_t fcpDl;
James Smartc7743952006-12-02 13:34:42 -05003709 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003710
James Smartea2151b2008-09-07 11:52:10 -04003711
dea31012005-04-17 16:05:31 -05003712 /*
3713 * If this is a task management command, there is no
3714 * scsi packet associated with this lpfc_cmd. The driver
3715 * consumes it.
3716 */
3717 if (fcpcmd->fcpCntl2) {
3718 scsi_status = 0;
3719 goto out;
3720 }
3721
James Smart6a9c52c2009-10-02 15:16:51 -04003722 if (resp_info & RSP_LEN_VALID) {
3723 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003724 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04003725 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3726 "2719 Invalid response length: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003727 "tgt x%x lun x%llx cmnd x%x rsplen x%x\n",
James Smart6a9c52c2009-10-02 15:16:51 -04003728 cmnd->device->id,
3729 cmnd->device->lun, cmnd->cmnd[0],
3730 rsplen);
3731 host_status = DID_ERROR;
3732 goto out;
3733 }
James Smarte40a02c2010-02-26 14:13:54 -05003734 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3735 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3736 "2757 Protocol failure detected during "
3737 "processing of FCP I/O op: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003738 "tgt x%x lun x%llx cmnd x%x rspInfo3 x%x\n",
James Smarte40a02c2010-02-26 14:13:54 -05003739 cmnd->device->id,
3740 cmnd->device->lun, cmnd->cmnd[0],
3741 fcprsp->rspInfo3);
3742 host_status = DID_ERROR;
3743 goto out;
3744 }
James Smart6a9c52c2009-10-02 15:16:51 -04003745 }
3746
James Smartc7743952006-12-02 13:34:42 -05003747 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3748 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3749 if (snslen > SCSI_SENSE_BUFFERSIZE)
3750 snslen = SCSI_SENSE_BUFFERSIZE;
3751
3752 if (resp_info & RSP_LEN_VALID)
3753 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3754 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3755 }
3756 lp = (uint32_t *)cmnd->sense_buffer;
3757
James Smartaa1c7ee2012-08-14 14:26:06 -04003758 /* special handling for under run conditions */
3759 if (!scsi_status && (resp_info & RESID_UNDER)) {
3760 /* don't log under runs if fcp set... */
3761 if (vport->cfg_log_verbose & LOG_FCP)
3762 logit = LOG_FCP_ERROR;
3763 /* unless operator says so */
3764 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3765 logit = LOG_FCP_UNDER;
3766 }
James Smartc7743952006-12-02 13:34:42 -05003767
James Smarte8b62012007-08-02 11:10:09 -04003768 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003769 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003770 "Data: x%x x%x x%x x%x x%x\n",
3771 cmnd->cmnd[0], scsi_status,
3772 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3773 be32_to_cpu(fcprsp->rspResId),
3774 be32_to_cpu(fcprsp->rspSnsLen),
3775 be32_to_cpu(fcprsp->rspRspLen),
3776 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003777
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003778 scsi_set_resid(cmnd, 0);
James Smart5afab6b2015-12-16 18:12:01 -05003779 fcpDl = be32_to_cpu(fcpcmd->fcpDl);
dea31012005-04-17 16:05:31 -05003780 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003781 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003782
James Smart73d91e52011-10-10 21:32:10 -04003783 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smart45634a862018-01-30 15:59:00 -08003784 "9025 FCP Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003785 "residual %d Data: x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003786 fcpDl,
James Smarte8b62012007-08-02 11:10:09 -04003787 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3788 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003789
3790 /*
James Smart45634a862018-01-30 15:59:00 -08003791 * If there is an under run, check if under run reported by
James Smart7054a602007-04-25 09:52:34 -04003792 * storage array is same as the under run reported by HBA.
3793 * If this is not same, there is a dropped frame.
3794 */
James Smart45634a862018-01-30 15:59:00 -08003795 if (fcpi_parm && (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003796 lpfc_printf_vlog(vport, KERN_WARNING,
3797 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003798 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003799 "and Underrun Data: x%x x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003800 fcpDl,
James Smarte8b62012007-08-02 11:10:09 -04003801 scsi_get_resid(cmnd), fcpi_parm,
3802 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003803 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003804 host_status = DID_ERROR;
3805 }
3806 /*
dea31012005-04-17 16:05:31 -05003807 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003808 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003809 * is not present, make sure the actual amount transferred is at
3810 * least the underflow value or fail.
3811 */
3812 if (!(resp_info & SNS_LEN_VALID) &&
3813 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003814 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3815 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003816 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003817 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003818 "underrun converted to error "
3819 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003820 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003821 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003822 host_status = DID_ERROR;
3823 }
3824 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003825 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003826 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003827 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003828 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003829 host_status = DID_ERROR;
3830
3831 /*
3832 * Check SLI validation that all the transfer was actually done
James Smart26373d22013-09-06 12:19:17 -04003833 * (fcpi_parm should be zero). Apply check only to reads.
dea31012005-04-17 16:05:31 -05003834 */
James Smart5afab6b2015-12-16 18:12:01 -05003835 } else if (fcpi_parm) {
James Smarte8b62012007-08-02 11:10:09 -04003836 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart5afab6b2015-12-16 18:12:01 -05003837 "9029 FCP %s Check Error xri x%x Data: "
James Smarteee88772010-09-29 11:19:08 -04003838 "x%x x%x x%x x%x x%x\n",
James Smart5afab6b2015-12-16 18:12:01 -05003839 ((cmnd->sc_data_direction == DMA_FROM_DEVICE) ?
3840 "Read" : "Write"),
3841 ((phba->sli_rev == LPFC_SLI_REV4) ?
3842 lpfc_cmd->cur_iocbq.sli4_xritag :
3843 rsp_iocb->iocb.ulpContext),
3844 fcpDl, be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04003845 fcpi_parm, cmnd->cmnd[0], scsi_status);
James Smart5afab6b2015-12-16 18:12:01 -05003846
3847 /* There is some issue with the LPe12000 that causes it
3848 * to miscalculate the fcpi_parm and falsely trip this
3849 * recovery logic. Detect this case and don't error when true.
3850 */
3851 if (fcpi_parm > fcpDl)
3852 goto out;
3853
James Smarteee88772010-09-29 11:19:08 -04003854 switch (scsi_status) {
3855 case SAM_STAT_GOOD:
3856 case SAM_STAT_CHECK_CONDITION:
3857 /* Fabric dropped a data frame. Fail any successful
3858 * command in which we detected dropped frames.
3859 * A status of good or some check conditions could
3860 * be considered a successful command.
3861 */
3862 host_status = DID_ERROR;
3863 break;
3864 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003865 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05003866 }
3867
3868 out:
3869 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04003870 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05003871}
3872
James Smart9bad7672008-12-04 22:39:02 -05003873/**
James Smart8b0dff12015-05-22 10:42:38 -04003874 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
3875 * @phba: Pointer to HBA context object.
3876 *
3877 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
3878 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
3879 * held.
3880 * If scsi-mq is enabled, get the default block layer mapping of software queues
3881 * to hardware queues. This information is saved in request tag.
3882 *
3883 * Return: index into SLI4 fast-path FCP queue index.
3884 **/
3885int lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba,
3886 struct lpfc_scsi_buf *lpfc_cmd)
3887{
3888 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3889 struct lpfc_vector_map_info *cpup;
3890 int chann, cpu;
3891 uint32_t tag;
3892 uint16_t hwq;
3893
Mauricio Faria de Oliveira05a05872016-06-07 20:13:08 -03003894 if (cmnd && shost_use_blk_mq(cmnd->device->host)) {
James Smart8b0dff12015-05-22 10:42:38 -04003895 tag = blk_mq_unique_tag(cmnd->request);
3896 hwq = blk_mq_unique_tag_to_hwq(tag);
3897
3898 return hwq;
3899 }
3900
3901 if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_CPU
3902 && phba->cfg_fcp_io_channel > 1) {
3903 cpu = smp_processor_id();
3904 if (cpu < phba->sli4_hba.num_present_cpu) {
3905 cpup = phba->sli4_hba.cpu_map;
3906 cpup += cpu;
3907 return cpup->channel_id;
3908 }
3909 }
3910 chann = atomic_add_return(1, &phba->fcp_qidx);
James Smart2ea259e2017-02-12 13:52:27 -08003911 chann = chann % phba->cfg_fcp_io_channel;
James Smart8b0dff12015-05-22 10:42:38 -04003912 return chann;
3913}
3914
3915
3916/**
James Smart3621a712009-04-06 18:47:14 -04003917 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05003918 * @phba: The Hba for which this call is being executed.
3919 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04003920 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05003921 *
3922 * This routine assigns scsi command result by looking into response IOCB
3923 * status field appropriately. This routine handles QUEUE FULL condition as
3924 * well by ramping down device queue depth.
3925 **/
dea31012005-04-17 16:05:31 -05003926static void
3927lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3928 struct lpfc_iocbq *pIocbOut)
3929{
3930 struct lpfc_scsi_buf *lpfc_cmd =
3931 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003932 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05003933 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3934 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04003935 struct scsi_cmnd *cmd;
James Smartfa61a542008-01-11 01:52:42 -05003936 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04003937 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04003938 struct Scsi_Host *shost;
James Smart73d91e52011-10-10 21:32:10 -04003939 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05003940
James Smart2cee7802017-06-01 21:07:02 -07003941 atomic_inc(&phba->fc4ScsiIoCmpls);
James Smart895427b2017-02-12 13:52:30 -08003942
James Smart75baf692010-06-08 18:31:21 -04003943 /* Sanity check on return of outstanding command */
James Smart75baf692010-06-08 18:31:21 -04003944 cmd = lpfc_cmd->pCmd;
James Smartc90261d2015-12-16 18:11:57 -05003945 if (!cmd)
3946 return;
James Smart75baf692010-06-08 18:31:21 -04003947 shost = cmd->device->host;
3948
James Smarte3d2b802012-08-14 14:25:43 -04003949 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
dea31012005-04-17 16:05:31 -05003950 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05003951 /* pick up SLI4 exhange busy status from HBA */
3952 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
3953
James Smart9a6b09c2012-03-01 22:37:42 -05003954#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3955 if (lpfc_cmd->prot_data_type) {
3956 struct scsi_dif_tuple *src = NULL;
3957
3958 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
3959 /*
3960 * Used to restore any changes to protection
3961 * data for error injection.
3962 */
3963 switch (lpfc_cmd->prot_data_type) {
3964 case LPFC_INJERR_REFTAG:
3965 src->ref_tag =
3966 lpfc_cmd->prot_data;
3967 break;
3968 case LPFC_INJERR_APPTAG:
3969 src->app_tag =
3970 (uint16_t)lpfc_cmd->prot_data;
3971 break;
3972 case LPFC_INJERR_GUARD:
3973 src->guard_tag =
3974 (uint16_t)lpfc_cmd->prot_data;
3975 break;
3976 default:
3977 break;
3978 }
3979
3980 lpfc_cmd->prot_data = 0;
3981 lpfc_cmd->prot_data_type = 0;
3982 lpfc_cmd->prot_data_segment = NULL;
3983 }
3984#endif
James Smart2ea259e2017-02-12 13:52:27 -08003985
dea31012005-04-17 16:05:31 -05003986 if (lpfc_cmd->status) {
3987 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
3988 (lpfc_cmd->result & IOERR_DRVR_MASK))
3989 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3990 else if (lpfc_cmd->status >= IOSTAT_CNT)
3991 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smartaa1c7ee2012-08-14 14:26:06 -04003992 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
3993 !lpfc_cmd->fcp_rsp->rspStatus3 &&
3994 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
3995 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
James Smart73d91e52011-10-10 21:32:10 -04003996 logit = 0;
3997 else
3998 logit = LOG_FCP | LOG_FCP_UNDER;
3999 lpfc_printf_vlog(vport, KERN_WARNING, logit,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004000 "9030 FCP cmd x%x failed <%d/%lld> "
James Smart5a0d80f2012-05-09 21:18:20 -04004001 "status: x%x result: x%x "
4002 "sid: x%x did: x%x oxid: x%x "
4003 "Data: x%x x%x\n",
James Smart73d91e52011-10-10 21:32:10 -04004004 cmd->cmnd[0],
4005 cmd->device ? cmd->device->id : 0xffff,
4006 cmd->device ? cmd->device->lun : 0xffff,
4007 lpfc_cmd->status, lpfc_cmd->result,
James Smart3bf41ba2013-05-31 17:03:18 -04004008 vport->fc_myDID,
4009 (pnode) ? pnode->nlp_DID : 0,
James Smart5a0d80f2012-05-09 21:18:20 -04004010 phba->sli_rev == LPFC_SLI_REV4 ?
4011 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
James Smart73d91e52011-10-10 21:32:10 -04004012 pIocbOut->iocb.ulpContext,
4013 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05004014
4015 switch (lpfc_cmd->status) {
4016 case IOSTAT_FCP_RSP_ERROR:
4017 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05004018 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05004019 break;
4020 case IOSTAT_NPORT_BSY:
4021 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04004022 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04004023 fast_path_evt = lpfc_alloc_fast_evt(phba);
4024 if (!fast_path_evt)
4025 break;
4026 fast_path_evt->un.fabric_evt.event_type =
4027 FC_REG_FABRIC_EVENT;
4028 fast_path_evt->un.fabric_evt.subcategory =
4029 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
4030 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
4031 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4032 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
4033 &pnode->nlp_portname,
4034 sizeof(struct lpfc_name));
4035 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
4036 &pnode->nlp_nodename,
4037 sizeof(struct lpfc_name));
4038 }
4039 fast_path_evt->vport = vport;
4040 fast_path_evt->work_evt.evt =
4041 LPFC_EVT_FASTPATH_MGMT_EVT;
4042 spin_lock_irqsave(&phba->hbalock, flags);
4043 list_add_tail(&fast_path_evt->work_evt.evt_listp,
4044 &phba->work_list);
4045 spin_unlock_irqrestore(&phba->hbalock, flags);
4046 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004047 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004048 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05004049 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05004050 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
4051 lpfc_cmd->result ==
4052 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
4053 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
4054 lpfc_cmd->result ==
4055 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
4056 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
4057 break;
4058 }
James Smartd7c255b2008-08-24 21:50:00 -04004059 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05004060 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04004061 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
4062 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
James Smart92d7f7b2007-06-17 19:56:38 -05004063 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04004064 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05004065 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004066 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
4067 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
4068 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
4069 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
4070 /*
4071 * This is a response for a BG enabled
4072 * cmd. Parse BG error
4073 */
4074 lpfc_parse_bg_err(phba, lpfc_cmd,
4075 pIocbOut);
4076 break;
4077 } else {
4078 lpfc_printf_vlog(vport, KERN_WARNING,
4079 LOG_BG,
4080 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04004081 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05004082 }
4083 }
James Smart1151e3e2011-02-16 12:39:35 -05004084 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
4085 && (phba->sli_rev == LPFC_SLI_REV4)
4086 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
4087 /* This IO was aborted by the target, we don't
4088 * know the rxid and because we did not send the
4089 * ABTS we cannot generate and RRQ.
4090 */
4091 lpfc_set_rrq_active(phba, pnode,
James Smartee0f4fe2012-05-09 21:19:14 -04004092 lpfc_cmd->cur_iocbq.sli4_lxritag,
4093 0, 0);
James Smart1151e3e2011-02-16 12:39:35 -05004094 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004095 /* else: fall through */
dea31012005-04-17 16:05:31 -05004096 default:
4097 cmd->result = ScsiResult(DID_ERROR, 0);
4098 break;
4099 }
4100
James Smart58da1ff2008-04-07 10:15:56 -04004101 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004102 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04004103 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
4104 SAM_STAT_BUSY);
James Smartab56dc22011-02-16 12:39:57 -05004105 } else
dea31012005-04-17 16:05:31 -05004106 cmd->result = ScsiResult(DID_OK, 0);
dea31012005-04-17 16:05:31 -05004107
4108 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4109 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
4110
James Smarte8b62012007-08-02 11:10:09 -04004111 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004112 "0710 Iodone <%d/%llu> cmd %p, error "
James Smarte8b62012007-08-02 11:10:09 -04004113 "x%x SNS x%x x%x Data: x%x x%x\n",
4114 cmd->device->id, cmd->device->lun, cmd,
4115 cmd->result, *lp, *(lp + 3), cmd->retries,
4116 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05004117 }
4118
James Smartea2151b2008-09-07 11:52:10 -04004119 lpfc_update_stats(phba, lpfc_cmd);
James Smart977b5a02008-09-07 11:52:04 -04004120 if (vport->cfg_max_scsicmpl_time &&
4121 time_after(jiffies, lpfc_cmd->start_time +
4122 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04004123 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004124 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smartf91bc592018-04-09 14:24:22 -07004125 atomic_dec(&pnode->cmd_pending);
James Smart109f6ed2008-12-04 22:39:08 -05004126 if (pnode->cmd_qdepth >
4127 atomic_read(&pnode->cmd_pending) &&
4128 (atomic_read(&pnode->cmd_pending) >
4129 LPFC_MIN_TGT_QDEPTH) &&
4130 ((cmd->cmnd[0] == READ_10) ||
4131 (cmd->cmnd[0] == WRITE_10)))
4132 pnode->cmd_qdepth =
4133 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04004134
James Smart109f6ed2008-12-04 22:39:08 -05004135 pnode->last_change_time = jiffies;
4136 }
James Smarta257bf92009-04-06 18:48:10 -04004137 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004138 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smartf91bc592018-04-09 14:24:22 -07004139 atomic_dec(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04004140 }
James Smart1dcb58e2007-04-25 09:51:30 -04004141 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04004142
James Smart876dd7d2012-09-29 11:31:28 -04004143 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004144 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04004145 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004146
James Smart89533e92016-10-13 15:06:15 -07004147 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
4148 cmd->scsi_done(cmd);
4149
James Smartfa61a542008-01-11 01:52:42 -05004150 /*
4151 * If there is a thread waiting for command completion
4152 * wake up the thread.
4153 */
James Smarta257bf92009-04-06 18:48:10 -04004154 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004155 if (lpfc_cmd->waitq)
4156 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04004157 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004158
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004159 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004160}
4161
James Smart34b02dc2008-08-24 21:49:55 -04004162/**
Alexey Kardashevskiy8b2564e2015-04-28 18:26:22 +10004163 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
4164 * @data: A pointer to the immediate command data portion of the IOCB.
4165 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
4166 *
4167 * The routine copies the entire FCP command from @fcp_cmnd to @data while
4168 * byte swapping the data to big endian format for transmission on the wire.
4169 **/
4170static void
4171lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
4172{
4173 int i, j;
4174 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
4175 i += sizeof(uint32_t), j++) {
4176 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
4177 }
4178}
4179
4180/**
James Smartf1126682009-06-10 17:22:44 -04004181 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004182 * @vport: The virtual port for which this call is being executed.
4183 * @lpfc_cmd: The scsi command which needs to send.
4184 * @pnode: Pointer to lpfc_nodelist.
4185 *
4186 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04004187 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004188 **/
dea31012005-04-17 16:05:31 -05004189static void
James Smartf1126682009-06-10 17:22:44 -04004190lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05004191 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05004192{
James Smart2e0fef82007-06-17 19:56:36 -05004193 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004194 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4195 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4196 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4197 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
4198 int datadir = scsi_cmnd->sc_data_direction;
James Smart027140e2012-08-03 12:35:44 -04004199 uint8_t *ptr;
4200 bool sli4;
James Smart98bbf5f2013-09-06 12:18:45 -04004201 uint32_t fcpdl;
dea31012005-04-17 16:05:31 -05004202
James Smart58da1ff2008-04-07 10:15:56 -04004203 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4204 return;
4205
dea31012005-04-17 16:05:31 -05004206 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04004207 /* clear task management bits */
4208 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05004209
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04004210 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4211 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004212
James Smart027140e2012-08-03 12:35:44 -04004213 ptr = &fcp_cmnd->fcpCdb[0];
4214 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4215 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4216 ptr += scsi_cmnd->cmd_len;
4217 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4218 }
4219
Christoph Hellwig50668632014-10-30 14:30:06 +01004220 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
dea31012005-04-17 16:05:31 -05004221
James Smart027140e2012-08-03 12:35:44 -04004222 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
James Smart6acb3482014-04-04 13:51:25 -04004223 piocbq->iocb.un.fcpi.fcpi_XRdy = 0;
James Smart027140e2012-08-03 12:35:44 -04004224
dea31012005-04-17 16:05:31 -05004225 /*
4226 * There are three possibilities here - use scatter-gather segment, use
4227 * the single mapping, or neither. Start the lpfc command prep by
4228 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4229 * data bde entry.
4230 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05004231 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05004232 if (datadir == DMA_TO_DEVICE) {
4233 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart182ba752013-07-15 18:34:05 -04004234 iocb_cmd->ulpPU = PARM_READ_CHECK;
James Smart3cb01c52013-07-15 18:35:04 -04004235 if (vport->cfg_first_burst_size &&
4236 (pnode->nlp_flag & NLP_FIRSTBURST)) {
James Smart98bbf5f2013-09-06 12:18:45 -04004237 fcpdl = scsi_bufflen(scsi_cmnd);
4238 if (fcpdl < vport->cfg_first_burst_size)
4239 piocbq->iocb.un.fcpi.fcpi_XRdy = fcpdl;
4240 else
4241 piocbq->iocb.un.fcpi.fcpi_XRdy =
4242 vport->cfg_first_burst_size;
James Smart3cb01c52013-07-15 18:35:04 -04004243 }
dea31012005-04-17 16:05:31 -05004244 fcp_cmnd->fcpCntl3 = WRITE_DATA;
James Smart2cee7802017-06-01 21:07:02 -07004245 atomic_inc(&phba->fc4ScsiOutputRequests);
dea31012005-04-17 16:05:31 -05004246 } else {
4247 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4248 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05004249 fcp_cmnd->fcpCntl3 = READ_DATA;
James Smart2cee7802017-06-01 21:07:02 -07004250 atomic_inc(&phba->fc4ScsiInputRequests);
dea31012005-04-17 16:05:31 -05004251 }
4252 } else {
4253 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4254 iocb_cmd->un.fcpi.fcpi_parm = 0;
4255 iocb_cmd->ulpPU = 0;
4256 fcp_cmnd->fcpCntl3 = 0;
James Smart2cee7802017-06-01 21:07:02 -07004257 atomic_inc(&phba->fc4ScsiControlRequests);
dea31012005-04-17 16:05:31 -05004258 }
Alexey Kardashevskiy8b2564e2015-04-28 18:26:22 +10004259 if (phba->sli_rev == 3 &&
4260 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
4261 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004262 /*
4263 * Finish initializing those IOCB fields that are independent
4264 * of the scsi_cmnd request_buffer
4265 */
4266 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart027140e2012-08-03 12:35:44 -04004267 if (sli4)
James Smart6d368e52011-05-24 11:44:12 -04004268 piocbq->iocb.ulpContext =
4269 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05004270 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4271 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05004272 else
4273 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05004274
4275 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4276 piocbq->context1 = lpfc_cmd;
4277 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4278 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05004279 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05004280}
4281
James Smart9bad7672008-12-04 22:39:02 -05004282/**
James Smart6d368e52011-05-24 11:44:12 -04004283 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004284 * @vport: The virtual port for which this call is being executed.
4285 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4286 * @lun: Logical unit number.
4287 * @task_mgmt_cmd: SCSI task management command.
4288 *
James Smart3772a992009-05-22 14:50:54 -04004289 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4290 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004291 *
4292 * Return codes:
4293 * 0 - Error
4294 * 1 - Success
4295 **/
dea31012005-04-17 16:05:31 -05004296static int
James Smartf1126682009-06-10 17:22:44 -04004297lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05004298 struct lpfc_scsi_buf *lpfc_cmd,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004299 uint64_t lun,
dea31012005-04-17 16:05:31 -05004300 uint8_t task_mgmt_cmd)
4301{
dea31012005-04-17 16:05:31 -05004302 struct lpfc_iocbq *piocbq;
4303 IOCB_t *piocb;
4304 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04004305 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05004306 struct lpfc_nodelist *ndlp = rdata->pnode;
4307
James Smart58da1ff2008-04-07 10:15:56 -04004308 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4309 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05004310 return 0;
dea31012005-04-17 16:05:31 -05004311
dea31012005-04-17 16:05:31 -05004312 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004313 piocbq->vport = vport;
4314
dea31012005-04-17 16:05:31 -05004315 piocb = &piocbq->iocb;
4316
4317 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04004318 /* Clear out any old data in the FCP command area */
4319 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4320 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004321 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05004322 if (vport->phba->sli_rev == 3 &&
4323 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004324 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004325 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05004326 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04004327 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4328 piocb->ulpContext =
4329 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4330 }
James Smart53151bb2013-10-10 12:24:07 -04004331 piocb->ulpFCP2Rcvy = (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) ? 1 : 0;
dea31012005-04-17 16:05:31 -05004332 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
James Smartf9226c22014-02-20 09:57:28 -05004333 piocb->ulpPU = 0;
4334 piocb->un.fcpi.fcpi_parm = 0;
dea31012005-04-17 16:05:31 -05004335
4336 /* ulpTimeout is only one byte */
4337 if (lpfc_cmd->timeout > 0xff) {
4338 /*
4339 * Do not timeout the command at the firmware level.
4340 * The driver will provide the timeout mechanism.
4341 */
4342 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04004343 } else
dea31012005-04-17 16:05:31 -05004344 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04004345
4346 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4347 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004348
James Smart2e0fef82007-06-17 19:56:36 -05004349 return 1;
dea31012005-04-17 16:05:31 -05004350}
4351
James Smart9bad7672008-12-04 22:39:02 -05004352/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004353 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04004354 * @phba: The hba struct for which this call is being executed.
4355 * @dev_grp: The HBA PCI-Device group number.
4356 *
4357 * This routine sets up the SCSI interface API function jump table in @phba
4358 * struct.
4359 * Returns: 0 - success, -ENODEV - failure.
4360 **/
4361int
4362lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4363{
4364
James Smartf1126682009-06-10 17:22:44 -04004365 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4366 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04004367
James Smart3772a992009-05-22 14:50:54 -04004368 switch (dev_grp) {
4369 case LPFC_PCI_DEV_LP:
4370 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4371 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05004372 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004373 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05004374 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004375 break;
James Smartda0436e2009-05-22 14:51:39 -04004376 case LPFC_PCI_DEV_OC:
4377 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4378 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05004379 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004380 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05004381 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004382 break;
James Smart3772a992009-05-22 14:50:54 -04004383 default:
4384 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4385 "1418 Invalid HBA PCI-device group: 0x%x\n",
4386 dev_grp);
4387 return -ENODEV;
4388 break;
4389 }
James Smart3772a992009-05-22 14:50:54 -04004390 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05004391 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04004392 return 0;
4393}
4394
4395/**
James Smart3621a712009-04-06 18:47:14 -04004396 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05004397 * @phba: The Hba for which this call is being executed.
4398 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4399 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4400 *
4401 * This routine is IOCB completion routine for device reset and target reset
4402 * routine. This routine release scsi buffer associated with lpfc_cmd.
4403 **/
James Smart7054a602007-04-25 09:52:34 -04004404static void
4405lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4406 struct lpfc_iocbq *cmdiocbq,
4407 struct lpfc_iocbq *rspiocbq)
4408{
4409 struct lpfc_scsi_buf *lpfc_cmd =
4410 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4411 if (lpfc_cmd)
4412 lpfc_release_scsi_buf(phba, lpfc_cmd);
4413 return;
4414}
4415
James Smart9bad7672008-12-04 22:39:02 -05004416/**
James Smart3621a712009-04-06 18:47:14 -04004417 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05004418 * @host: The scsi host for which this call is being executed.
4419 *
4420 * This routine provides module information about hba.
4421 *
4422 * Reutrn code:
4423 * Pointer to char - Success.
4424 **/
dea31012005-04-17 16:05:31 -05004425const char *
4426lpfc_info(struct Scsi_Host *host)
4427{
James Smart2e0fef82007-06-17 19:56:36 -05004428 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4429 struct lpfc_hba *phba = vport->phba;
James Smart8b68cd52012-09-29 11:32:37 -04004430 int len, link_speed = 0;
dea31012005-04-17 16:05:31 -05004431 static char lpfcinfobuf[384];
4432
4433 memset(lpfcinfobuf,0,384);
4434 if (phba && phba->pcidev){
4435 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4436 len = strlen(lpfcinfobuf);
4437 snprintf(lpfcinfobuf + len,
4438 384-len,
4439 " on PCI bus %02x device %02x irq %d",
4440 phba->pcidev->bus->number,
4441 phba->pcidev->devfn,
4442 phba->pcidev->irq);
4443 len = strlen(lpfcinfobuf);
4444 if (phba->Port[0]) {
4445 snprintf(lpfcinfobuf + len,
4446 384-len,
4447 " port %s",
4448 phba->Port);
4449 }
James Smart65467b62010-01-26 23:08:29 -05004450 len = strlen(lpfcinfobuf);
James Smarta085e872015-12-16 18:12:02 -05004451 link_speed = lpfc_sli_port_speed_get(phba);
James Smart8b68cd52012-09-29 11:32:37 -04004452 if (link_speed != 0)
4453 snprintf(lpfcinfobuf + len, 384-len,
4454 " Logical Link Speed: %d Mbps", link_speed);
dea31012005-04-17 16:05:31 -05004455 }
4456 return lpfcinfobuf;
4457}
4458
James Smart9bad7672008-12-04 22:39:02 -05004459/**
James Smart3621a712009-04-06 18:47:14 -04004460 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05004461 * @phba: The Hba for which this call is being executed.
4462 *
4463 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4464 * The default value of cfg_poll_tmo is 10 milliseconds.
4465 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004466static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4467{
4468 unsigned long poll_tmo_expires =
4469 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4470
James Smart895427b2017-02-12 13:52:30 -08004471 if (!list_empty(&phba->sli.sli3_ring[LPFC_FCP_RING].txcmplq))
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004472 mod_timer(&phba->fcp_poll_timer,
4473 poll_tmo_expires);
4474}
4475
James Smart9bad7672008-12-04 22:39:02 -05004476/**
James Smart3621a712009-04-06 18:47:14 -04004477 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004478 * @phba: The Hba for which this call is being executed.
4479 *
4480 * This routine starts the fcp_poll_timer of @phba.
4481 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004482void lpfc_poll_start_timer(struct lpfc_hba * phba)
4483{
4484 lpfc_poll_rearm_timer(phba);
4485}
4486
James Smart9bad7672008-12-04 22:39:02 -05004487/**
James Smart3621a712009-04-06 18:47:14 -04004488 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05004489 * @ptr: Map to lpfc_hba data structure pointer.
4490 *
4491 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4492 * and FCP Ring interrupt is disable.
4493 **/
4494
Kees Cookf22eb4d2017-09-06 20:24:26 -07004495void lpfc_poll_timeout(struct timer_list *t)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004496{
Kees Cookf22eb4d2017-09-06 20:24:26 -07004497 struct lpfc_hba *phba = from_timer(phba, t, fcp_poll_timer);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004498
4499 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004500 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08004501 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James Smart45ed1192009-10-02 15:17:02 -04004502
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004503 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4504 lpfc_poll_rearm_timer(phba);
4505 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004506}
4507
James Smart9bad7672008-12-04 22:39:02 -05004508/**
James Smart3621a712009-04-06 18:47:14 -04004509 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05004510 * @cmnd: Pointer to scsi_cmnd data structure.
4511 * @done: Pointer to done routine.
4512 *
4513 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4514 * This routine prepares an IOCB from scsi command and provides to firmware.
4515 * The @done callback is invoked after driver finished processing the command.
4516 *
4517 * Return value :
4518 * 0 - Success
4519 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4520 **/
dea31012005-04-17 16:05:31 -05004521static int
James Smartb9a7c632012-08-03 12:35:24 -04004522lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004523{
James Smart2e0fef82007-06-17 19:56:36 -05004524 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4525 struct lpfc_hba *phba = vport->phba;
James Smart1ba981f2014-02-20 09:56:45 -05004526 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004527 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004528 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004529 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004530 int err;
dea31012005-04-17 16:05:31 -05004531
James Smart1ba981f2014-02-20 09:56:45 -05004532 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004533 err = fc_remote_port_chkready(rport);
4534 if (err) {
4535 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004536 goto out_fail_command;
4537 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004538 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004539
James Smartbf086112011-08-21 21:48:13 -04004540 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004541 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004542
James Smart6a9c52c2009-10-02 15:16:51 -04004543 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4544 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4545 " op:%02x str=%s without registering for"
4546 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004547 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4548 dif_op_str[scsi_get_prot_op(cmnd)]);
4549 goto out_fail_command;
4550 }
4551
dea31012005-04-17 16:05:31 -05004552 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004553 * Catch race where our node has transitioned, but the
4554 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004555 */
James Smart6b415f52012-06-12 13:54:59 -04004556 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4557 goto out_tgt_busy;
James Smart64bf0092018-01-30 15:58:53 -08004558 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) {
4559 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
4560 "3377 Target Queue Full, scsi Id:%d Qdepth:%d"
4561 " Pending command:%d"
4562 " WWNN:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x, "
4563 " WWPN:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
4564 ndlp->nlp_sid, ndlp->cmd_qdepth,
4565 atomic_read(&ndlp->cmd_pending),
4566 ndlp->nlp_nodename.u.wwn[0],
4567 ndlp->nlp_nodename.u.wwn[1],
4568 ndlp->nlp_nodename.u.wwn[2],
4569 ndlp->nlp_nodename.u.wwn[3],
4570 ndlp->nlp_nodename.u.wwn[4],
4571 ndlp->nlp_nodename.u.wwn[5],
4572 ndlp->nlp_nodename.u.wwn[6],
4573 ndlp->nlp_nodename.u.wwn[7],
4574 ndlp->nlp_portname.u.wwn[0],
4575 ndlp->nlp_portname.u.wwn[1],
4576 ndlp->nlp_portname.u.wwn[2],
4577 ndlp->nlp_portname.u.wwn[3],
4578 ndlp->nlp_portname.u.wwn[4],
4579 ndlp->nlp_portname.u.wwn[5],
4580 ndlp->nlp_portname.u.wwn[6],
4581 ndlp->nlp_portname.u.wwn[7]);
Mike Christie34963432011-02-25 14:04:28 -06004582 goto out_tgt_busy;
James Smart64bf0092018-01-30 15:58:53 -08004583 }
James Smartf91bc592018-04-09 14:24:22 -07004584 atomic_inc(&ndlp->cmd_pending);
4585
James Smart19ca7602010-11-20 23:11:55 -05004586 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05004587 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004588 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004589
James Smart895427b2017-02-12 13:52:30 -08004590 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
James Smarte8b62012007-08-02 11:10:09 -04004591 "0707 driver's buffer pool is empty, "
4592 "IO busied\n");
dea31012005-04-17 16:05:31 -05004593 goto out_host_busy;
4594 }
4595
4596 /*
4597 * Store the midlayer's command structure for the completion phase
4598 * and complete the command initialization.
4599 */
4600 lpfc_cmd->pCmd = cmnd;
4601 lpfc_cmd->rdata = rdata;
4602 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04004603 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05004604 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
dea31012005-04-17 16:05:31 -05004605
James Smarte2a0a9d2008-12-04 22:40:02 -05004606 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004607 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004608 lpfc_printf_vlog(vport,
4609 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004610 "9033 BLKGRD: rcvd %s cmd:x%x "
4611 "sector x%llx cnt %u pt %x\n",
4612 dif_op_str[scsi_get_prot_op(cmnd)],
4613 cmnd->cmnd[0],
4614 (unsigned long long)scsi_get_lba(cmnd),
4615 blk_rq_sectors(cmnd->request),
4616 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004617 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004618 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4619 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004620 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004621 lpfc_printf_vlog(vport,
4622 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004623 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4624 "x%x sector x%llx cnt %u pt %x\n",
4625 cmnd->cmnd[0],
4626 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004627 blk_rq_sectors(cmnd->request),
James Smart26134702012-08-14 14:25:50 -04004628 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004629 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004630 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4631 }
4632
dea31012005-04-17 16:05:31 -05004633 if (err)
4634 goto out_host_busy_free_buf;
4635
James Smart2e0fef82007-06-17 19:56:36 -05004636 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004637
James Smart3772a992009-05-22 14:50:54 -04004638 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004639 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05004640 if (err) {
James Smart76f96b62013-12-17 20:29:01 -05004641 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4642 "3376 FCP could not issue IOCB err %x"
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004643 "FCP cmd x%x <%d/%llu> "
James Smart76f96b62013-12-17 20:29:01 -05004644 "sid: x%x did: x%x oxid: x%x "
4645 "Data: x%x x%x x%x x%x\n",
4646 err, cmnd->cmnd[0],
4647 cmnd->device ? cmnd->device->id : 0xffff,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004648 cmnd->device ? cmnd->device->lun : (u64) -1,
James Smart76f96b62013-12-17 20:29:01 -05004649 vport->fc_myDID, ndlp->nlp_DID,
4650 phba->sli_rev == LPFC_SLI_REV4 ?
4651 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
4652 lpfc_cmd->cur_iocbq.iocb.ulpContext,
4653 lpfc_cmd->cur_iocbq.iocb.ulpIoTag,
4654 lpfc_cmd->cur_iocbq.iocb.ulpTimeout,
4655 (uint32_t)
4656 (cmnd->request->timeout / 1000));
4657
James Smart2cee7802017-06-01 21:07:02 -07004658 switch (lpfc_cmd->fcp_cmnd->fcpCntl3) {
4659 case WRITE_DATA:
4660 atomic_dec(&phba->fc4ScsiOutputRequests);
4661 break;
4662 case READ_DATA:
4663 atomic_dec(&phba->fc4ScsiInputRequests);
4664 break;
4665 default:
4666 atomic_dec(&phba->fc4ScsiControlRequests);
4667 }
dea31012005-04-17 16:05:31 -05004668 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004669 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004670 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004671 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08004672 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James Smart45ed1192009-10-02 15:17:02 -04004673
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004674 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4675 lpfc_poll_rearm_timer(phba);
4676 }
4677
dea31012005-04-17 16:05:31 -05004678 return 0;
4679
4680 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04004681 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004682 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004683 out_host_busy:
James Smartf91bc592018-04-09 14:24:22 -07004684 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05004685 return SCSI_MLQUEUE_HOST_BUSY;
4686
Mike Christie34963432011-02-25 14:04:28 -06004687 out_tgt_busy:
4688 return SCSI_MLQUEUE_TARGET_BUSY;
4689
dea31012005-04-17 16:05:31 -05004690 out_fail_command:
James Smartb9a7c632012-08-03 12:35:24 -04004691 cmnd->scsi_done(cmnd);
dea31012005-04-17 16:05:31 -05004692 return 0;
4693}
4694
Jeff Garzikf2812332010-11-16 02:10:29 -05004695
James Smart9bad7672008-12-04 22:39:02 -05004696/**
James Smart3621a712009-04-06 18:47:14 -04004697 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004698 * @cmnd: Pointer to scsi_cmnd data structure.
4699 *
4700 * This routine aborts @cmnd pending in base driver.
4701 *
4702 * Return code :
4703 * 0x2003 - Error
4704 * 0x2002 - Success
4705 **/
dea31012005-04-17 16:05:31 -05004706static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004707lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004708{
James Smart2e0fef82007-06-17 19:56:36 -05004709 struct Scsi_Host *shost = cmnd->device->host;
4710 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4711 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004712 struct lpfc_iocbq *iocb;
4713 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004714 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004715 IOCB_t *cmd, *icmd;
James Smart3a707302012-06-12 13:54:42 -04004716 int ret = SUCCESS, status = 0;
James Smart98912dda2014-04-04 13:52:31 -04004717 struct lpfc_sli_ring *pring_s4;
James Smart895427b2017-02-12 13:52:30 -08004718 int ret_val;
James Smart98912dda2014-04-04 13:52:31 -04004719 unsigned long flags, iflags;
James Smartfa61a542008-01-11 01:52:42 -05004720 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004721
James Smart3a707302012-06-12 13:54:42 -04004722 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004723 if (status != 0 && status != SUCCESS)
James Smart3a707302012-06-12 13:54:42 -04004724 return status;
James Smart4f2e66c2012-05-09 21:17:07 -04004725
James Smart876dd7d2012-09-29 11:31:28 -04004726 spin_lock_irqsave(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004727 /* driver queued commands are in process of being flushed */
4728 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
James Smart876dd7d2012-09-29 11:31:28 -04004729 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004730 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4731 "3168 SCSI Layer abort requested I/O has been "
4732 "flushed by LLD.\n");
4733 return FAILED;
4734 }
4735
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004736 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smart92e3af62012-08-14 14:26:28 -04004737 if (!lpfc_cmd || !lpfc_cmd->pCmd) {
James Smart876dd7d2012-09-29 11:31:28 -04004738 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarteee88772010-09-29 11:19:08 -04004739 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4740 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004741 "x%x ID %d LUN %llu\n",
James Smart3a707302012-06-12 13:54:42 -04004742 SUCCESS, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04004743 return SUCCESS;
4744 }
dea31012005-04-17 16:05:31 -05004745
James Smart4f2e66c2012-05-09 21:17:07 -04004746 iocb = &lpfc_cmd->cur_iocbq;
4747 /* the command is in process of being cancelled */
4748 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
James Smart876dd7d2012-09-29 11:31:28 -04004749 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004750 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4751 "3169 SCSI Layer abort requested I/O has been "
4752 "cancelled by LLD.\n");
4753 return FAILED;
4754 }
dea31012005-04-17 16:05:31 -05004755 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004756 * If pCmd field of the corresponding lpfc_scsi_buf structure
4757 * points to a different SCSI command, then the driver has
4758 * already completed this command, but the midlayer did not
James Smart4f2e66c2012-05-09 21:17:07 -04004759 * see the completion before the eh fired. Just return SUCCESS.
dea31012005-04-17 16:05:31 -05004760 */
James Smart4f2e66c2012-05-09 21:17:07 -04004761 if (lpfc_cmd->pCmd != cmnd) {
4762 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4763 "3170 SCSI Layer abort requested I/O has been "
4764 "completed by LLD.\n");
4765 goto out_unlock;
4766 }
dea31012005-04-17 16:05:31 -05004767
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004768 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004769
James Smartee620212014-04-04 13:51:53 -04004770 /* abort issued in recovery is still in progress */
4771 if (iocb->iocb_flag & LPFC_DRIVER_ABORTED) {
4772 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4773 "3389 SCSI Layer I/O Abort Request is pending\n");
4774 spin_unlock_irqrestore(&phba->hbalock, flags);
4775 goto wait_for_cmpl;
4776 }
4777
James Smart4f2e66c2012-05-09 21:17:07 -04004778 abtsiocb = __lpfc_sli_get_iocbq(phba);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004779 if (abtsiocb == NULL) {
4780 ret = FAILED;
James Smart4f2e66c2012-05-09 21:17:07 -04004781 goto out_unlock;
dea31012005-04-17 16:05:31 -05004782 }
4783
James Smartafbd8d82013-09-06 12:22:13 -04004784 /* Indicate the IO is being aborted by the driver. */
4785 iocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4786
dea31012005-04-17 16:05:31 -05004787 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004788 * The scsi command can not be in txq and it is in flight because the
4789 * pCmd is still pointig at the SCSI command we have to abort. There
4790 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004791 */
dea31012005-04-17 16:05:31 -05004792
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004793 cmd = &iocb->iocb;
4794 icmd = &abtsiocb->iocb;
4795 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4796 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004797 if (phba->sli_rev == LPFC_SLI_REV4)
4798 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4799 else
4800 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004801
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004802 icmd->ulpLe = 1;
4803 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004804
4805 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -08004806 abtsiocb->hba_wqidx = iocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -05004807 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -04004808 if (iocb->iocb_flag & LPFC_IO_FOF)
4809 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -05004810
James Smart2e0fef82007-06-17 19:56:36 -05004811 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004812 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4813 else
4814 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004815
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004816 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004817 abtsiocb->vport = vport;
James Smart98912dda2014-04-04 13:52:31 -04004818 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -08004819 pring_s4 = lpfc_sli4_calc_ring(phba, iocb);
4820 if (pring_s4 == NULL) {
4821 ret = FAILED;
4822 goto out_unlock;
4823 }
James Smart98912dda2014-04-04 13:52:31 -04004824 /* Note: both hbalock and ring_lock must be set here */
4825 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
4826 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
4827 abtsiocb, 0);
4828 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
4829 } else {
4830 ret_val = __lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
4831 abtsiocb, 0);
4832 }
James Smart4f2e66c2012-05-09 21:17:07 -04004833 /* no longer need the lock after this point */
James Smart876dd7d2012-09-29 11:31:28 -04004834 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004835
James Smart98912dda2014-04-04 13:52:31 -04004836
4837 if (ret_val == IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004838 lpfc_sli_release_iocbq(phba, abtsiocb);
4839 ret = FAILED;
4840 goto out;
4841 }
4842
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004843 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004844 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08004845 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004846
James Smartee620212014-04-04 13:51:53 -04004847wait_for_cmpl:
James Smartfa61a542008-01-11 01:52:42 -05004848 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004849 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004850 wait_event_timeout(waitq,
4851 (lpfc_cmd->pCmd != cmnd),
James Smart256ec0d2013-04-17 20:14:58 -04004852 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
James Smartee620212014-04-04 13:51:53 -04004853
4854 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004855 lpfc_cmd->waitq = NULL;
James Smartee620212014-04-04 13:51:53 -04004856 spin_unlock_irqrestore(shost->host_lock, flags);
dea31012005-04-17 16:05:31 -05004857
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004858 if (lpfc_cmd->pCmd == cmnd) {
4859 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04004860 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4861 "0748 abort handler timed out waiting "
Milan P. Gandhi4b160ae2016-10-13 15:06:02 -07004862 "for aborting I/O (xri:x%x) to complete: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004863 "ret %#x, ID %d, LUN %llu\n",
James Smart247ca942012-08-14 14:26:13 -04004864 iocb->sli4_xritag, ret,
4865 cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004866 }
James Smart4f2e66c2012-05-09 21:17:07 -04004867 goto out;
dea31012005-04-17 16:05:31 -05004868
James Smart4f2e66c2012-05-09 21:17:07 -04004869out_unlock:
James Smart876dd7d2012-09-29 11:31:28 -04004870 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004871out:
James Smarte8b62012007-08-02 11:10:09 -04004872 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4873 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004874 "LUN %llu\n", ret, cmnd->device->id,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004875 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004876 return ret;
dea31012005-04-17 16:05:31 -05004877}
4878
James Smartbbb9d182009-06-10 17:23:16 -04004879static char *
4880lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4881{
4882 switch (task_mgmt_cmd) {
4883 case FCP_ABORT_TASK_SET:
4884 return "ABORT_TASK_SET";
4885 case FCP_CLEAR_TASK_SET:
4886 return "FCP_CLEAR_TASK_SET";
4887 case FCP_BUS_RESET:
4888 return "FCP_BUS_RESET";
4889 case FCP_LUN_RESET:
4890 return "FCP_LUN_RESET";
4891 case FCP_TARGET_RESET:
4892 return "FCP_TARGET_RESET";
4893 case FCP_CLEAR_ACA:
4894 return "FCP_CLEAR_ACA";
4895 case FCP_TERMINATE_TASK:
4896 return "FCP_TERMINATE_TASK";
4897 default:
4898 return "unknown";
4899 }
4900}
4901
James Smart53151bb2013-10-10 12:24:07 -04004902
4903/**
4904 * lpfc_check_fcp_rsp - check the returned fcp_rsp to see if task failed
4905 * @vport: The virtual port for which this call is being executed.
4906 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4907 *
4908 * This routine checks the FCP RSP INFO to see if the tsk mgmt command succeded
4909 *
4910 * Return code :
4911 * 0x2003 - Error
4912 * 0x2002 - Success
4913 **/
4914static int
4915lpfc_check_fcp_rsp(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd)
4916{
4917 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
4918 uint32_t rsp_info;
4919 uint32_t rsp_len;
4920 uint8_t rsp_info_code;
4921 int ret = FAILED;
4922
4923
4924 if (fcprsp == NULL)
4925 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4926 "0703 fcp_rsp is missing\n");
4927 else {
4928 rsp_info = fcprsp->rspStatus2;
4929 rsp_len = be32_to_cpu(fcprsp->rspRspLen);
4930 rsp_info_code = fcprsp->rspInfo3;
4931
4932
4933 lpfc_printf_vlog(vport, KERN_INFO,
4934 LOG_FCP,
4935 "0706 fcp_rsp valid 0x%x,"
4936 " rsp len=%d code 0x%x\n",
4937 rsp_info,
4938 rsp_len, rsp_info_code);
4939
4940 if ((fcprsp->rspStatus2&RSP_LEN_VALID) && (rsp_len == 8)) {
4941 switch (rsp_info_code) {
4942 case RSP_NO_FAILURE:
4943 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4944 "0715 Task Mgmt No Failure\n");
4945 ret = SUCCESS;
4946 break;
4947 case RSP_TM_NOT_SUPPORTED: /* TM rejected */
4948 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4949 "0716 Task Mgmt Target "
4950 "reject\n");
4951 break;
4952 case RSP_TM_NOT_COMPLETED: /* TM failed */
4953 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4954 "0717 Task Mgmt Target "
4955 "failed TM\n");
4956 break;
4957 case RSP_TM_INVALID_LU: /* TM to invalid LU! */
4958 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4959 "0718 Task Mgmt to invalid "
4960 "LUN\n");
4961 break;
4962 }
4963 }
4964 }
4965 return ret;
4966}
4967
4968
James Smartbbb9d182009-06-10 17:23:16 -04004969/**
4970 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
4971 * @vport: The virtual port for which this call is being executed.
4972 * @rdata: Pointer to remote port local data
4973 * @tgt_id: Target ID of remote device.
4974 * @lun_id: Lun number for the TMF
4975 * @task_mgmt_cmd: type of TMF to send
4976 *
4977 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
4978 * a remote port.
4979 *
4980 * Return Code:
4981 * 0x2003 - Error
4982 * 0x2002 - Success.
4983 **/
4984static int
James Smarteed695d2016-10-13 15:06:04 -07004985lpfc_send_taskmgmt(struct lpfc_vport *vport, struct scsi_cmnd *cmnd,
4986 unsigned int tgt_id, uint64_t lun_id,
4987 uint8_t task_mgmt_cmd)
James Smartbbb9d182009-06-10 17:23:16 -04004988{
4989 struct lpfc_hba *phba = vport->phba;
4990 struct lpfc_scsi_buf *lpfc_cmd;
4991 struct lpfc_iocbq *iocbq;
4992 struct lpfc_iocbq *iocbqrsp;
James Smarteed695d2016-10-13 15:06:04 -07004993 struct lpfc_rport_data *rdata;
4994 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004995 int ret;
4996 int status;
4997
James Smarteed695d2016-10-13 15:06:04 -07004998 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
4999 if (!rdata || !rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
James Smartbbb9d182009-06-10 17:23:16 -04005000 return FAILED;
James Smarteed695d2016-10-13 15:06:04 -07005001 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005002
James Smarteed695d2016-10-13 15:06:04 -07005003 lpfc_cmd = lpfc_get_scsi_buf(phba, pnode);
James Smartbbb9d182009-06-10 17:23:16 -04005004 if (lpfc_cmd == NULL)
5005 return FAILED;
James Smart0c411222013-09-06 12:22:46 -04005006 lpfc_cmd->timeout = phba->cfg_task_mgmt_tmo;
James Smartbbb9d182009-06-10 17:23:16 -04005007 lpfc_cmd->rdata = rdata;
James Smarteed695d2016-10-13 15:06:04 -07005008 lpfc_cmd->pCmd = cmnd;
James Smartbbb9d182009-06-10 17:23:16 -04005009
5010 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
5011 task_mgmt_cmd);
5012 if (!status) {
5013 lpfc_release_scsi_buf(phba, lpfc_cmd);
5014 return FAILED;
5015 }
5016
5017 iocbq = &lpfc_cmd->cur_iocbq;
5018 iocbqrsp = lpfc_sli_get_iocbq(phba);
5019 if (iocbqrsp == NULL) {
5020 lpfc_release_scsi_buf(phba, lpfc_cmd);
5021 return FAILED;
5022 }
James Smart5a0916b2013-07-15 18:31:42 -04005023 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
James Smartbbb9d182009-06-10 17:23:16 -04005024
5025 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005026 "0702 Issue %s to TGT %d LUN %llu "
James Smart6d368e52011-05-24 11:44:12 -04005027 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04005028 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04005029 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
5030 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04005031
5032 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
5033 iocbq, iocbqrsp, lpfc_cmd->timeout);
James Smart53151bb2013-10-10 12:24:07 -04005034 if ((status != IOCB_SUCCESS) ||
5035 (iocbqrsp->iocb.ulpStatus != IOSTAT_SUCCESS)) {
James Smartae374a32015-05-22 10:42:41 -04005036 if (status != IOCB_SUCCESS ||
5037 iocbqrsp->iocb.ulpStatus != IOSTAT_FCP_RSP_ERROR)
5038 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5039 "0727 TMF %s to TGT %d LUN %llu "
5040 "failed (%d, %d) iocb_flag x%x\n",
5041 lpfc_taskmgmt_name(task_mgmt_cmd),
5042 tgt_id, lun_id,
5043 iocbqrsp->iocb.ulpStatus,
5044 iocbqrsp->iocb.un.ulpWord[4],
5045 iocbq->iocb_flag);
James Smart53151bb2013-10-10 12:24:07 -04005046 /* if ulpStatus != IOCB_SUCCESS, then status == IOCB_SUCCESS */
5047 if (status == IOCB_SUCCESS) {
5048 if (iocbqrsp->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
5049 /* Something in the FCP_RSP was invalid.
5050 * Check conditions */
5051 ret = lpfc_check_fcp_rsp(vport, lpfc_cmd);
5052 else
5053 ret = FAILED;
5054 } else if (status == IOCB_TIMEDOUT) {
5055 ret = TIMEOUT_ERROR;
5056 } else {
5057 ret = FAILED;
5058 }
James Smart53151bb2013-10-10 12:24:07 -04005059 } else
James Smartbbb9d182009-06-10 17:23:16 -04005060 ret = SUCCESS;
5061
5062 lpfc_sli_release_iocbq(phba, iocbqrsp);
5063
5064 if (ret != TIMEOUT_ERROR)
5065 lpfc_release_scsi_buf(phba, lpfc_cmd);
5066
5067 return ret;
5068}
5069
5070/**
5071 * lpfc_chk_tgt_mapped -
5072 * @vport: The virtual port to check on
5073 * @cmnd: Pointer to scsi_cmnd data structure.
5074 *
5075 * This routine delays until the scsi target (aka rport) for the
5076 * command exists (is present and logged in) or we declare it non-existent.
5077 *
5078 * Return code :
5079 * 0x2003 - Error
5080 * 0x2002 - Success
5081 **/
5082static int
5083lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
5084{
James Smart1ba981f2014-02-20 09:56:45 -05005085 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005086 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005087 unsigned long later;
5088
James Smart1ba981f2014-02-20 09:56:45 -05005089 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smart1c6f4ef52009-11-18 15:40:49 -05005090 if (!rdata) {
5091 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5092 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
5093 return FAILED;
5094 }
5095 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005096 /*
5097 * If target is not in a MAPPED state, delay until
5098 * target is rediscovered or devloss timeout expires.
5099 */
5100 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5101 while (time_after(later, jiffies)) {
5102 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
5103 return FAILED;
5104 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
5105 return SUCCESS;
5106 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
James Smart1ba981f2014-02-20 09:56:45 -05005107 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartbbb9d182009-06-10 17:23:16 -04005108 if (!rdata)
5109 return FAILED;
5110 pnode = rdata->pnode;
5111 }
5112 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
5113 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
5114 return FAILED;
5115 return SUCCESS;
5116}
5117
5118/**
5119 * lpfc_reset_flush_io_context -
5120 * @vport: The virtual port (scsi_host) for the flush context
5121 * @tgt_id: If aborting by Target contect - specifies the target id
5122 * @lun_id: If aborting by Lun context - specifies the lun id
5123 * @context: specifies the context level to flush at.
5124 *
5125 * After a reset condition via TMF, we need to flush orphaned i/o
5126 * contexts from the adapter. This routine aborts any contexts
5127 * outstanding, then waits for their completions. The wait is
5128 * bounded by devloss_tmo though.
5129 *
5130 * Return code :
5131 * 0x2003 - Error
5132 * 0x2002 - Success
5133 **/
5134static int
5135lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5136 uint64_t lun_id, lpfc_ctx_cmd context)
5137{
5138 struct lpfc_hba *phba = vport->phba;
5139 unsigned long later;
5140 int cnt;
5141
5142 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5143 if (cnt)
James Smart98912dda2014-04-04 13:52:31 -04005144 lpfc_sli_abort_taskmgmt(vport,
James Smart895427b2017-02-12 13:52:30 -08005145 &phba->sli.sli3_ring[LPFC_FCP_RING],
James Smart98912dda2014-04-04 13:52:31 -04005146 tgt_id, lun_id, context);
James Smartbbb9d182009-06-10 17:23:16 -04005147 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5148 while (time_after(later, jiffies) && cnt) {
5149 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5150 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5151 }
5152 if (cnt) {
5153 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5154 "0724 I/O flush failure for context %s : cnt x%x\n",
5155 ((context == LPFC_CTX_LUN) ? "LUN" :
5156 ((context == LPFC_CTX_TGT) ? "TGT" :
5157 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5158 cnt);
5159 return FAILED;
5160 }
5161 return SUCCESS;
5162}
5163
James Smart9bad7672008-12-04 22:39:02 -05005164/**
James Smart3621a712009-04-06 18:47:14 -04005165 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05005166 * @cmnd: Pointer to scsi_cmnd data structure.
5167 *
James Smartbbb9d182009-06-10 17:23:16 -04005168 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05005169 * command.
5170 *
5171 * Return code :
5172 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04005173 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005174 **/
dea31012005-04-17 16:05:31 -05005175static int
James Smart7054a602007-04-25 09:52:34 -04005176lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005177{
James Smart2e0fef82007-06-17 19:56:36 -05005178 struct Scsi_Host *shost = cmnd->device->host;
5179 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smart1ba981f2014-02-20 09:56:45 -05005180 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005181 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005182 unsigned tgt_id = cmnd->device->id;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005183 uint64_t lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04005184 struct lpfc_scsi_event_header scsi_event;
James Smart53151bb2013-10-10 12:24:07 -04005185 int status;
dea31012005-04-17 16:05:31 -05005186
James Smart1ba981f2014-02-20 09:56:45 -05005187 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartad490b62015-04-07 15:07:26 -04005188 if (!rdata || !rdata->pnode) {
James Smart1c6f4ef52009-11-18 15:40:49 -05005189 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartad490b62015-04-07 15:07:26 -04005190 "0798 Device Reset rport failure: rdata x%p\n",
5191 rdata);
James Smart1c6f4ef52009-11-18 15:40:49 -05005192 return FAILED;
5193 }
5194 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005195 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005196 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005197 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005198
5199 status = lpfc_chk_tgt_mapped(vport, cmnd);
5200 if (status == FAILED) {
5201 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5202 "0721 Device Reset rport failure: rdata x%p\n", rdata);
5203 return FAILED;
5204 }
5205
5206 scsi_event.event_type = FC_REG_SCSI_EVENT;
5207 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5208 scsi_event.lun = lun_id;
5209 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5210 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5211
5212 fc_host_post_vendor_event(shost, fc_get_event_number(),
5213 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5214
James Smarteed695d2016-10-13 15:06:04 -07005215 status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005216 FCP_LUN_RESET);
5217
5218 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005219 "0713 SCSI layer issued Device Reset (%d, %llu) "
James Smartbbb9d182009-06-10 17:23:16 -04005220 "return x%x\n", tgt_id, lun_id, status);
5221
dea31012005-04-17 16:05:31 -05005222 /*
James Smartbbb9d182009-06-10 17:23:16 -04005223 * We have to clean up i/o as : they may be orphaned by the TMF;
5224 * or if the TMF failed, they may be in an indeterminate state.
5225 * So, continue on.
5226 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05005227 */
James Smart53151bb2013-10-10 12:24:07 -04005228 if (status == SUCCESS)
5229 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005230 LPFC_CTX_LUN);
James Smart53151bb2013-10-10 12:24:07 -04005231
5232 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005233}
5234
5235/**
5236 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5237 * @cmnd: Pointer to scsi_cmnd data structure.
5238 *
5239 * This routine does a target reset by sending a TARGET_RESET task management
5240 * command.
5241 *
5242 * Return code :
5243 * 0x2003 - Error
5244 * 0x2002 - Success
5245 **/
5246static int
5247lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5248{
5249 struct Scsi_Host *shost = cmnd->device->host;
5250 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smart1ba981f2014-02-20 09:56:45 -05005251 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005252 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005253 unsigned tgt_id = cmnd->device->id;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005254 uint64_t lun_id = cmnd->device->lun;
James Smartbbb9d182009-06-10 17:23:16 -04005255 struct lpfc_scsi_event_header scsi_event;
James Smart53151bb2013-10-10 12:24:07 -04005256 int status;
James Smartbbb9d182009-06-10 17:23:16 -04005257
James Smart1ba981f2014-02-20 09:56:45 -05005258 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smart1c6f4ef52009-11-18 15:40:49 -05005259 if (!rdata) {
5260 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5261 "0799 Target Reset rport failure: rdata x%p\n", rdata);
5262 return FAILED;
5263 }
5264 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005265 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005266 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005267 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005268
5269 status = lpfc_chk_tgt_mapped(vport, cmnd);
5270 if (status == FAILED) {
5271 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5272 "0722 Target Reset rport failure: rdata x%p\n", rdata);
James Smart63e480f2015-04-07 15:07:20 -04005273 if (pnode) {
5274 spin_lock_irq(shost->host_lock);
5275 pnode->nlp_flag &= ~NLP_NPR_ADISC;
5276 pnode->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
5277 spin_unlock_irq(shost->host_lock);
5278 }
James Smart8c50d252014-09-03 12:58:16 -04005279 lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5280 LPFC_CTX_TGT);
5281 return FAST_IO_FAIL;
dea31012005-04-17 16:05:31 -05005282 }
James Smartea2151b2008-09-07 11:52:10 -04005283
5284 scsi_event.event_type = FC_REG_SCSI_EVENT;
5285 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5286 scsi_event.lun = 0;
5287 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5288 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5289
James Smartbbb9d182009-06-10 17:23:16 -04005290 fc_host_post_vendor_event(shost, fc_get_event_number(),
5291 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04005292
James Smarteed695d2016-10-13 15:06:04 -07005293 status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005294 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05005295
James Smarte8b62012007-08-02 11:10:09 -04005296 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005297 "0723 SCSI layer issued Target Reset (%d, %llu) "
James Smartbbb9d182009-06-10 17:23:16 -04005298 "return x%x\n", tgt_id, lun_id, status);
5299
5300 /*
5301 * We have to clean up i/o as : they may be orphaned by the TMF;
5302 * or if the TMF failed, they may be in an indeterminate state.
5303 * So, continue on.
5304 * We will report success if all the i/o aborts successfully.
5305 */
James Smart53151bb2013-10-10 12:24:07 -04005306 if (status == SUCCESS)
5307 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smart3a707302012-06-12 13:54:42 -04005308 LPFC_CTX_TGT);
James Smart53151bb2013-10-10 12:24:07 -04005309 return status;
dea31012005-04-17 16:05:31 -05005310}
5311
James Smart9bad7672008-12-04 22:39:02 -05005312/**
James Smart3621a712009-04-06 18:47:14 -04005313 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05005314 * @cmnd: Pointer to scsi_cmnd data structure.
5315 *
James Smartbbb9d182009-06-10 17:23:16 -04005316 * This routine does target reset to all targets on @cmnd->device->host.
5317 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05005318 *
James Smartbbb9d182009-06-10 17:23:16 -04005319 * Return code :
5320 * 0x2003 - Error
5321 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005322 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04005323static int
James Smart7054a602007-04-25 09:52:34 -04005324lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005325{
James Smart2e0fef82007-06-17 19:56:36 -05005326 struct Scsi_Host *shost = cmnd->device->host;
5327 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005328 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04005329 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04005330 int match;
5331 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04005332
5333 scsi_event.event_type = FC_REG_SCSI_EVENT;
5334 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5335 scsi_event.lun = 0;
5336 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5337 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5338
James Smartbbb9d182009-06-10 17:23:16 -04005339 fc_host_post_vendor_event(shost, fc_get_event_number(),
5340 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05005341
James Smartbf086112011-08-21 21:48:13 -04005342 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005343 if (status != 0 && status != SUCCESS)
James Smartbf086112011-08-21 21:48:13 -04005344 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005345
dea31012005-04-17 16:05:31 -05005346 /*
5347 * Since the driver manages a single bus device, reset all
5348 * targets known to the driver. Should any target reset
5349 * fail, this routine returns failure to the midlayer.
5350 */
James Smarte17da182006-07-06 15:49:25 -04005351 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04005352 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05005353 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05005354 spin_lock_irq(shost->host_lock);
5355 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005356 if (!NLP_CHK_NODE_ACT(ndlp))
5357 continue;
James Smarta6571c62012-10-31 14:44:42 -04005358 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5359 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5360 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005361 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04005362 ndlp->nlp_sid == i &&
James Smarta0f2d3e2017-02-12 13:52:31 -08005363 ndlp->rport &&
5364 ndlp->nlp_type & NLP_FCP_TARGET) {
dea31012005-04-17 16:05:31 -05005365 match = 1;
5366 break;
5367 }
5368 }
James Smart2e0fef82007-06-17 19:56:36 -05005369 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005370 if (!match)
5371 continue;
James Smartbbb9d182009-06-10 17:23:16 -04005372
James Smarteed695d2016-10-13 15:06:04 -07005373 status = lpfc_send_taskmgmt(vport, cmnd,
James Smartbbb9d182009-06-10 17:23:16 -04005374 i, 0, FCP_TARGET_RESET);
5375
5376 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04005377 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5378 "0700 Bus Reset on target %d failed\n",
5379 i);
James Smart915caaa2008-06-14 22:52:38 -04005380 ret = FAILED;
dea31012005-04-17 16:05:31 -05005381 }
5382 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005383 /*
James Smartbbb9d182009-06-10 17:23:16 -04005384 * We have to clean up i/o as : they may be orphaned by the TMFs
5385 * above; or if any of the TMFs failed, they may be in an
5386 * indeterminate state.
5387 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005388 */
James Smartbbb9d182009-06-10 17:23:16 -04005389
5390 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5391 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005392 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04005393
James Smarte8b62012007-08-02 11:10:09 -04005394 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5395 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05005396 return ret;
5397}
5398
James Smart9bad7672008-12-04 22:39:02 -05005399/**
James Smart27b01b82012-05-09 21:19:44 -04005400 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
5401 * @cmnd: Pointer to scsi_cmnd data structure.
5402 *
5403 * This routine does host reset to the adaptor port. It brings the HBA
5404 * offline, performs a board restart, and then brings the board back online.
5405 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
5406 * reject all outstanding SCSI commands to the host and error returned
5407 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5408 * of error handling, it will only return error if resetting of the adapter
5409 * is not successful; in all other cases, will return success.
5410 *
5411 * Return code :
5412 * 0x2003 - Error
5413 * 0x2002 - Success
5414 **/
5415static int
5416lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5417{
5418 struct Scsi_Host *shost = cmnd->device->host;
5419 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5420 struct lpfc_hba *phba = vport->phba;
5421 int rc, ret = SUCCESS;
5422
James Smarta88dbb62013-04-17 20:18:39 -04005423 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5424 "3172 SCSI layer issued Host Reset Data:\n");
5425
James Smart618a5232012-06-12 13:54:36 -04005426 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart27b01b82012-05-09 21:19:44 -04005427 lpfc_offline(phba);
5428 rc = lpfc_sli_brdrestart(phba);
5429 if (rc)
5430 ret = FAILED;
James Smarta88dbb62013-04-17 20:18:39 -04005431 rc = lpfc_online(phba);
5432 if (rc)
5433 ret = FAILED;
James Smart27b01b82012-05-09 21:19:44 -04005434 lpfc_unblock_mgmt_io(phba);
5435
James Smarta88dbb62013-04-17 20:18:39 -04005436 if (ret == FAILED) {
5437 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5438 "3323 Failed host reset, bring it offline\n");
5439 lpfc_sli4_offline_eratt(phba);
5440 }
James Smart27b01b82012-05-09 21:19:44 -04005441 return ret;
5442}
5443
5444/**
James Smart3621a712009-04-06 18:47:14 -04005445 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05005446 * @sdev: Pointer to scsi_device.
5447 *
5448 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5449 * globally available list of scsi buffers. This routine also makes sure scsi
5450 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5451 * of scsi buffer exists for the lifetime of the driver.
5452 *
5453 * Return codes:
5454 * non-0 - Error
5455 * 0 - Success
5456 **/
dea31012005-04-17 16:05:31 -05005457static int
dea31012005-04-17 16:05:31 -05005458lpfc_slave_alloc(struct scsi_device *sdev)
5459{
James Smart2e0fef82007-06-17 19:56:36 -05005460 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5461 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005462 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04005463 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05005464 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04005465 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04005466 uint32_t sdev_cnt;
James Smart1ba981f2014-02-20 09:56:45 -05005467 struct lpfc_device_data *device_data;
5468 unsigned long flags;
5469 struct lpfc_name target_wwpn;
dea31012005-04-17 16:05:31 -05005470
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005471 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05005472 return -ENXIO;
5473
James Smartf38fa0b2014-04-04 13:52:21 -04005474 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -05005475
5476 /*
5477 * Check to see if the device data structure for the lun
5478 * exists. If not, create one.
5479 */
5480
5481 u64_to_wwn(rport->port_name, target_wwpn.u.wwn);
5482 spin_lock_irqsave(&phba->devicelock, flags);
5483 device_data = __lpfc_get_device_data(phba,
5484 &phba->luns,
5485 &vport->fc_portname,
5486 &target_wwpn,
5487 sdev->lun);
5488 if (!device_data) {
5489 spin_unlock_irqrestore(&phba->devicelock, flags);
5490 device_data = lpfc_create_device_data(phba,
5491 &vport->fc_portname,
5492 &target_wwpn,
James Smartb5749fe2016-12-19 15:07:26 -08005493 sdev->lun,
5494 phba->cfg_XLanePriority,
5495 true);
James Smart1ba981f2014-02-20 09:56:45 -05005496 if (!device_data)
5497 return -ENOMEM;
5498 spin_lock_irqsave(&phba->devicelock, flags);
5499 list_add_tail(&device_data->listentry, &phba->luns);
5500 }
5501 device_data->rport_data = rport->dd_data;
5502 device_data->available = true;
5503 spin_unlock_irqrestore(&phba->devicelock, flags);
5504 sdev->hostdata = device_data;
5505 } else {
5506 sdev->hostdata = rport->dd_data;
5507 }
James Smartd7c47992010-06-08 18:31:54 -04005508 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005509
5510 /*
5511 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5512 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04005513 * HBA limit conveyed to the midlayer via the host structure. The
5514 * formula accounts for the lun_queue_depth + error handlers + 1
5515 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05005516 */
5517 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04005518 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05005519
James Smartd7c47992010-06-08 18:31:54 -04005520 /* If allocated buffers are enough do nothing */
5521 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5522 return 0;
5523
James Smart92d7f7b2007-06-17 19:56:38 -05005524 /* Allow some exchanges to be available always to complete discovery */
5525 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005526 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5527 "0704 At limitation of %d preallocated "
5528 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05005529 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05005530 /* Allow some exchanges to be available always to complete discovery */
5531 } else if (total + num_to_alloc >
5532 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005533 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5534 "0705 Allocation request of %d "
5535 "command buffers will exceed max of %d. "
5536 "Reducing allocation request to %d.\n",
5537 num_to_alloc, phba->cfg_hba_queue_depth,
5538 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05005539 num_to_alloc = phba->cfg_hba_queue_depth - total;
5540 }
James Smart3772a992009-05-22 14:50:54 -04005541 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
5542 if (num_to_alloc != num_allocated) {
James Smart96f70772013-04-17 20:16:15 -04005543 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5544 "0708 Allocation request of %d "
5545 "command buffers did not succeed. "
5546 "Allocated %d buffers.\n",
5547 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05005548 }
James Smart1c6f4ef52009-11-18 15:40:49 -05005549 if (num_allocated > 0)
5550 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05005551 return 0;
5552}
5553
James Smart9bad7672008-12-04 22:39:02 -05005554/**
James Smart3621a712009-04-06 18:47:14 -04005555 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05005556 * @sdev: Pointer to scsi_device.
5557 *
5558 * This routine configures following items
5559 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05005560 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5561 *
5562 * Return codes:
5563 * 0 - Success
5564 **/
dea31012005-04-17 16:05:31 -05005565static int
5566lpfc_slave_configure(struct scsi_device *sdev)
5567{
James Smart2e0fef82007-06-17 19:56:36 -05005568 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5569 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005570
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01005571 scsi_change_queue_depth(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005572
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005573 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005574 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -08005575 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005576 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5577 lpfc_poll_rearm_timer(phba);
5578 }
5579
dea31012005-04-17 16:05:31 -05005580 return 0;
5581}
5582
James Smart9bad7672008-12-04 22:39:02 -05005583/**
James Smart3621a712009-04-06 18:47:14 -04005584 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05005585 * @sdev: Pointer to scsi_device.
5586 *
5587 * This routine sets @sdev hostatdata filed to null.
5588 **/
dea31012005-04-17 16:05:31 -05005589static void
5590lpfc_slave_destroy(struct scsi_device *sdev)
5591{
James Smartd7c47992010-06-08 18:31:54 -04005592 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5593 struct lpfc_hba *phba = vport->phba;
James Smart1ba981f2014-02-20 09:56:45 -05005594 unsigned long flags;
5595 struct lpfc_device_data *device_data = sdev->hostdata;
5596
James Smartd7c47992010-06-08 18:31:54 -04005597 atomic_dec(&phba->sdev_cnt);
James Smartf38fa0b2014-04-04 13:52:21 -04005598 if ((phba->cfg_fof) && (device_data)) {
James Smart1ba981f2014-02-20 09:56:45 -05005599 spin_lock_irqsave(&phba->devicelock, flags);
5600 device_data->available = false;
5601 if (!device_data->oas_enabled)
5602 lpfc_delete_device_data(phba, device_data);
5603 spin_unlock_irqrestore(&phba->devicelock, flags);
5604 }
dea31012005-04-17 16:05:31 -05005605 sdev->hostdata = NULL;
5606 return;
5607}
5608
James Smart1ba981f2014-02-20 09:56:45 -05005609/**
5610 * lpfc_create_device_data - creates and initializes device data structure for OAS
5611 * @pha: Pointer to host bus adapter structure.
5612 * @vport_wwpn: Pointer to vport's wwpn information
5613 * @target_wwpn: Pointer to target's wwpn information
5614 * @lun: Lun on target
5615 * @atomic_create: Flag to indicate if memory should be allocated using the
5616 * GFP_ATOMIC flag or not.
5617 *
5618 * This routine creates a device data structure which will contain identifying
5619 * information for the device (host wwpn, target wwpn, lun), state of OAS,
5620 * whether or not the corresponding lun is available by the system,
5621 * and pointer to the rport data.
5622 *
5623 * Return codes:
5624 * NULL - Error
5625 * Pointer to lpfc_device_data - Success
5626 **/
5627struct lpfc_device_data*
5628lpfc_create_device_data(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5629 struct lpfc_name *target_wwpn, uint64_t lun,
James Smartb5749fe2016-12-19 15:07:26 -08005630 uint32_t pri, bool atomic_create)
James Smart1ba981f2014-02-20 09:56:45 -05005631{
5632
5633 struct lpfc_device_data *lun_info;
5634 int memory_flags;
5635
5636 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005637 !(phba->cfg_fof))
James Smart1ba981f2014-02-20 09:56:45 -05005638 return NULL;
5639
5640 /* Attempt to create the device data to contain lun info */
5641
5642 if (atomic_create)
5643 memory_flags = GFP_ATOMIC;
5644 else
5645 memory_flags = GFP_KERNEL;
5646 lun_info = mempool_alloc(phba->device_data_mem_pool, memory_flags);
5647 if (!lun_info)
5648 return NULL;
5649 INIT_LIST_HEAD(&lun_info->listentry);
5650 lun_info->rport_data = NULL;
5651 memcpy(&lun_info->device_id.vport_wwpn, vport_wwpn,
5652 sizeof(struct lpfc_name));
5653 memcpy(&lun_info->device_id.target_wwpn, target_wwpn,
5654 sizeof(struct lpfc_name));
5655 lun_info->device_id.lun = lun;
5656 lun_info->oas_enabled = false;
James Smartb5749fe2016-12-19 15:07:26 -08005657 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005658 lun_info->available = false;
5659 return lun_info;
5660}
5661
5662/**
5663 * lpfc_delete_device_data - frees a device data structure for OAS
5664 * @pha: Pointer to host bus adapter structure.
5665 * @lun_info: Pointer to device data structure to free.
5666 *
5667 * This routine frees the previously allocated device data structure passed.
5668 *
5669 **/
5670void
5671lpfc_delete_device_data(struct lpfc_hba *phba,
5672 struct lpfc_device_data *lun_info)
5673{
5674
5675 if (unlikely(!phba) || !lun_info ||
James Smartf38fa0b2014-04-04 13:52:21 -04005676 !(phba->cfg_fof))
James Smart1ba981f2014-02-20 09:56:45 -05005677 return;
5678
5679 if (!list_empty(&lun_info->listentry))
5680 list_del(&lun_info->listentry);
5681 mempool_free(lun_info, phba->device_data_mem_pool);
5682 return;
5683}
5684
5685/**
5686 * __lpfc_get_device_data - returns the device data for the specified lun
5687 * @pha: Pointer to host bus adapter structure.
5688 * @list: Point to list to search.
5689 * @vport_wwpn: Pointer to vport's wwpn information
5690 * @target_wwpn: Pointer to target's wwpn information
5691 * @lun: Lun on target
5692 *
5693 * This routine searches the list passed for the specified lun's device data.
5694 * This function does not hold locks, it is the responsibility of the caller
5695 * to ensure the proper lock is held before calling the function.
5696 *
5697 * Return codes:
5698 * NULL - Error
5699 * Pointer to lpfc_device_data - Success
5700 **/
5701struct lpfc_device_data*
5702__lpfc_get_device_data(struct lpfc_hba *phba, struct list_head *list,
5703 struct lpfc_name *vport_wwpn,
5704 struct lpfc_name *target_wwpn, uint64_t lun)
5705{
5706
5707 struct lpfc_device_data *lun_info;
5708
5709 if (unlikely(!phba) || !list || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005710 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005711 return NULL;
5712
5713 /* Check to see if the lun is already enabled for OAS. */
5714
5715 list_for_each_entry(lun_info, list, listentry) {
5716 if ((memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5717 sizeof(struct lpfc_name)) == 0) &&
5718 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5719 sizeof(struct lpfc_name)) == 0) &&
5720 (lun_info->device_id.lun == lun))
5721 return lun_info;
5722 }
5723
5724 return NULL;
5725}
5726
5727/**
5728 * lpfc_find_next_oas_lun - searches for the next oas lun
5729 * @pha: Pointer to host bus adapter structure.
5730 * @vport_wwpn: Pointer to vport's wwpn information
5731 * @target_wwpn: Pointer to target's wwpn information
5732 * @starting_lun: Pointer to the lun to start searching for
5733 * @found_vport_wwpn: Pointer to the found lun's vport wwpn information
5734 * @found_target_wwpn: Pointer to the found lun's target wwpn information
5735 * @found_lun: Pointer to the found lun.
5736 * @found_lun_status: Pointer to status of the found lun.
5737 *
5738 * This routine searches the luns list for the specified lun
5739 * or the first lun for the vport/target. If the vport wwpn contains
5740 * a zero value then a specific vport is not specified. In this case
5741 * any vport which contains the lun will be considered a match. If the
5742 * target wwpn contains a zero value then a specific target is not specified.
5743 * In this case any target which contains the lun will be considered a
5744 * match. If the lun is found, the lun, vport wwpn, target wwpn and lun status
5745 * are returned. The function will also return the next lun if available.
5746 * If the next lun is not found, starting_lun parameter will be set to
5747 * NO_MORE_OAS_LUN.
5748 *
5749 * Return codes:
5750 * non-0 - Error
5751 * 0 - Success
5752 **/
5753bool
5754lpfc_find_next_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5755 struct lpfc_name *target_wwpn, uint64_t *starting_lun,
5756 struct lpfc_name *found_vport_wwpn,
5757 struct lpfc_name *found_target_wwpn,
5758 uint64_t *found_lun,
James Smartb5749fe2016-12-19 15:07:26 -08005759 uint32_t *found_lun_status,
5760 uint32_t *found_lun_pri)
James Smart1ba981f2014-02-20 09:56:45 -05005761{
5762
5763 unsigned long flags;
5764 struct lpfc_device_data *lun_info;
5765 struct lpfc_device_id *device_id;
5766 uint64_t lun;
5767 bool found = false;
5768
5769 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5770 !starting_lun || !found_vport_wwpn ||
5771 !found_target_wwpn || !found_lun || !found_lun_status ||
5772 (*starting_lun == NO_MORE_OAS_LUN) ||
James Smartf38fa0b2014-04-04 13:52:21 -04005773 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005774 return false;
5775
5776 lun = *starting_lun;
5777 *found_lun = NO_MORE_OAS_LUN;
5778 *starting_lun = NO_MORE_OAS_LUN;
5779
5780 /* Search for lun or the lun closet in value */
5781
5782 spin_lock_irqsave(&phba->devicelock, flags);
5783 list_for_each_entry(lun_info, &phba->luns, listentry) {
5784 if (((wwn_to_u64(vport_wwpn->u.wwn) == 0) ||
5785 (memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5786 sizeof(struct lpfc_name)) == 0)) &&
5787 ((wwn_to_u64(target_wwpn->u.wwn) == 0) ||
5788 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5789 sizeof(struct lpfc_name)) == 0)) &&
5790 (lun_info->oas_enabled)) {
5791 device_id = &lun_info->device_id;
5792 if ((!found) &&
5793 ((lun == FIND_FIRST_OAS_LUN) ||
5794 (device_id->lun == lun))) {
5795 *found_lun = device_id->lun;
5796 memcpy(found_vport_wwpn,
5797 &device_id->vport_wwpn,
5798 sizeof(struct lpfc_name));
5799 memcpy(found_target_wwpn,
5800 &device_id->target_wwpn,
5801 sizeof(struct lpfc_name));
5802 if (lun_info->available)
5803 *found_lun_status =
5804 OAS_LUN_STATUS_EXISTS;
5805 else
5806 *found_lun_status = 0;
James Smartb5749fe2016-12-19 15:07:26 -08005807 *found_lun_pri = lun_info->priority;
James Smart1ba981f2014-02-20 09:56:45 -05005808 if (phba->cfg_oas_flags & OAS_FIND_ANY_VPORT)
5809 memset(vport_wwpn, 0x0,
5810 sizeof(struct lpfc_name));
5811 if (phba->cfg_oas_flags & OAS_FIND_ANY_TARGET)
5812 memset(target_wwpn, 0x0,
5813 sizeof(struct lpfc_name));
5814 found = true;
5815 } else if (found) {
5816 *starting_lun = device_id->lun;
5817 memcpy(vport_wwpn, &device_id->vport_wwpn,
5818 sizeof(struct lpfc_name));
5819 memcpy(target_wwpn, &device_id->target_wwpn,
5820 sizeof(struct lpfc_name));
5821 break;
5822 }
5823 }
5824 }
5825 spin_unlock_irqrestore(&phba->devicelock, flags);
5826 return found;
5827}
5828
5829/**
5830 * lpfc_enable_oas_lun - enables a lun for OAS operations
5831 * @pha: Pointer to host bus adapter structure.
5832 * @vport_wwpn: Pointer to vport's wwpn information
5833 * @target_wwpn: Pointer to target's wwpn information
5834 * @lun: Lun
5835 *
5836 * This routine enables a lun for oas operations. The routines does so by
5837 * doing the following :
5838 *
5839 * 1) Checks to see if the device data for the lun has been created.
5840 * 2) If found, sets the OAS enabled flag if not set and returns.
5841 * 3) Otherwise, creates a device data structure.
5842 * 4) If successfully created, indicates the device data is for an OAS lun,
5843 * indicates the lun is not available and add to the list of luns.
5844 *
5845 * Return codes:
5846 * false - Error
5847 * true - Success
5848 **/
5849bool
5850lpfc_enable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
James Smartc92c8412016-07-06 12:36:05 -07005851 struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
James Smart1ba981f2014-02-20 09:56:45 -05005852{
5853
5854 struct lpfc_device_data *lun_info;
5855 unsigned long flags;
5856
5857 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005858 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005859 return false;
5860
5861 spin_lock_irqsave(&phba->devicelock, flags);
5862
5863 /* Check to see if the device data for the lun has been created */
5864 lun_info = __lpfc_get_device_data(phba, &phba->luns, vport_wwpn,
5865 target_wwpn, lun);
5866 if (lun_info) {
5867 if (!lun_info->oas_enabled)
5868 lun_info->oas_enabled = true;
James Smartb5749fe2016-12-19 15:07:26 -08005869 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005870 spin_unlock_irqrestore(&phba->devicelock, flags);
5871 return true;
5872 }
5873
5874 /* Create an lun info structure and add to list of luns */
5875 lun_info = lpfc_create_device_data(phba, vport_wwpn, target_wwpn, lun,
James Smartb5749fe2016-12-19 15:07:26 -08005876 pri, false);
James Smart1ba981f2014-02-20 09:56:45 -05005877 if (lun_info) {
5878 lun_info->oas_enabled = true;
James Smartc92c8412016-07-06 12:36:05 -07005879 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005880 lun_info->available = false;
5881 list_add_tail(&lun_info->listentry, &phba->luns);
5882 spin_unlock_irqrestore(&phba->devicelock, flags);
5883 return true;
5884 }
5885 spin_unlock_irqrestore(&phba->devicelock, flags);
5886 return false;
5887}
5888
5889/**
5890 * lpfc_disable_oas_lun - disables a lun for OAS operations
5891 * @pha: Pointer to host bus adapter structure.
5892 * @vport_wwpn: Pointer to vport's wwpn information
5893 * @target_wwpn: Pointer to target's wwpn information
5894 * @lun: Lun
5895 *
5896 * This routine disables a lun for oas operations. The routines does so by
5897 * doing the following :
5898 *
5899 * 1) Checks to see if the device data for the lun is created.
5900 * 2) If present, clears the flag indicating this lun is for OAS.
5901 * 3) If the lun is not available by the system, the device data is
5902 * freed.
5903 *
5904 * Return codes:
5905 * false - Error
5906 * true - Success
5907 **/
5908bool
5909lpfc_disable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
James Smartb5749fe2016-12-19 15:07:26 -08005910 struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
James Smart1ba981f2014-02-20 09:56:45 -05005911{
5912
5913 struct lpfc_device_data *lun_info;
5914 unsigned long flags;
5915
5916 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005917 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005918 return false;
5919
5920 spin_lock_irqsave(&phba->devicelock, flags);
5921
5922 /* Check to see if the lun is available. */
5923 lun_info = __lpfc_get_device_data(phba,
5924 &phba->luns, vport_wwpn,
5925 target_wwpn, lun);
5926 if (lun_info) {
5927 lun_info->oas_enabled = false;
James Smartb5749fe2016-12-19 15:07:26 -08005928 lun_info->priority = pri;
James Smart1ba981f2014-02-20 09:56:45 -05005929 if (!lun_info->available)
5930 lpfc_delete_device_data(phba, lun_info);
5931 spin_unlock_irqrestore(&phba->devicelock, flags);
5932 return true;
5933 }
5934
5935 spin_unlock_irqrestore(&phba->devicelock, flags);
5936 return false;
5937}
James Smart92d7f7b2007-06-17 19:56:38 -05005938
James Smart895427b2017-02-12 13:52:30 -08005939static int
5940lpfc_no_command(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
5941{
5942 return SCSI_MLQUEUE_HOST_BUSY;
5943}
5944
5945static int
5946lpfc_no_handler(struct scsi_cmnd *cmnd)
5947{
5948 return FAILED;
5949}
5950
5951static int
5952lpfc_no_slave(struct scsi_device *sdev)
5953{
5954 return -ENODEV;
5955}
5956
5957struct scsi_host_template lpfc_template_nvme = {
5958 .module = THIS_MODULE,
5959 .name = LPFC_DRIVER_NAME,
5960 .proc_name = LPFC_DRIVER_NAME,
5961 .info = lpfc_info,
5962 .queuecommand = lpfc_no_command,
5963 .eh_abort_handler = lpfc_no_handler,
5964 .eh_device_reset_handler = lpfc_no_handler,
5965 .eh_target_reset_handler = lpfc_no_handler,
5966 .eh_bus_reset_handler = lpfc_no_handler,
5967 .eh_host_reset_handler = lpfc_no_handler,
5968 .slave_alloc = lpfc_no_slave,
5969 .slave_configure = lpfc_no_slave,
5970 .scan_finished = lpfc_scan_finished,
5971 .this_id = -1,
5972 .sg_tablesize = 1,
5973 .cmd_per_lun = 1,
5974 .use_clustering = ENABLE_CLUSTERING,
5975 .shost_attrs = lpfc_hba_attrs,
5976 .max_sectors = 0xFFFF,
5977 .vendor_id = LPFC_NL_VENDOR_ID,
5978 .track_queue_depth = 0,
5979};
5980
James Smart96418b52017-03-04 09:30:31 -08005981struct scsi_host_template lpfc_template_no_hr = {
James Smartea4142f2015-04-07 15:07:13 -04005982 .module = THIS_MODULE,
5983 .name = LPFC_DRIVER_NAME,
James Smart08dcd4c2016-07-06 12:35:59 -07005984 .proc_name = LPFC_DRIVER_NAME,
James Smartea4142f2015-04-07 15:07:13 -04005985 .info = lpfc_info,
5986 .queuecommand = lpfc_queuecommand,
James Smart856984b2017-03-04 09:30:32 -08005987 .eh_timed_out = fc_eh_timed_out,
James Smartea4142f2015-04-07 15:07:13 -04005988 .eh_abort_handler = lpfc_abort_handler,
5989 .eh_device_reset_handler = lpfc_device_reset_handler,
5990 .eh_target_reset_handler = lpfc_target_reset_handler,
5991 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5992 .slave_alloc = lpfc_slave_alloc,
5993 .slave_configure = lpfc_slave_configure,
5994 .slave_destroy = lpfc_slave_destroy,
5995 .scan_finished = lpfc_scan_finished,
5996 .this_id = -1,
5997 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
5998 .cmd_per_lun = LPFC_CMD_PER_LUN,
5999 .use_clustering = ENABLE_CLUSTERING,
6000 .shost_attrs = lpfc_hba_attrs,
6001 .max_sectors = 0xFFFF,
6002 .vendor_id = LPFC_NL_VENDOR_ID,
6003 .change_queue_depth = scsi_change_queue_depth,
James Smartea4142f2015-04-07 15:07:13 -04006004 .track_queue_depth = 1,
6005};
6006
dea31012005-04-17 16:05:31 -05006007struct scsi_host_template lpfc_template = {
6008 .module = THIS_MODULE,
6009 .name = LPFC_DRIVER_NAME,
James Smart08dcd4c2016-07-06 12:35:59 -07006010 .proc_name = LPFC_DRIVER_NAME,
dea31012005-04-17 16:05:31 -05006011 .info = lpfc_info,
6012 .queuecommand = lpfc_queuecommand,
Christoph Hellwigb6a05c82017-01-30 13:18:58 +01006013 .eh_timed_out = fc_eh_timed_out,
dea31012005-04-17 16:05:31 -05006014 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04006015 .eh_device_reset_handler = lpfc_device_reset_handler,
6016 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04006017 .eh_bus_reset_handler = lpfc_bus_reset_handler,
James Smart27b01b82012-05-09 21:19:44 -04006018 .eh_host_reset_handler = lpfc_host_reset_handler,
dea31012005-04-17 16:05:31 -05006019 .slave_alloc = lpfc_slave_alloc,
6020 .slave_configure = lpfc_slave_configure,
6021 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04006022 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05006023 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05006024 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05006025 .cmd_per_lun = LPFC_CMD_PER_LUN,
6026 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05006027 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04006028 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04006029 .vendor_id = LPFC_NL_VENDOR_ID,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01006030 .change_queue_depth = scsi_change_queue_depth,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01006031 .track_queue_depth = 1,
dea31012005-04-17 16:05:31 -05006032};
James Smart3de2a652007-08-02 11:09:59 -04006033
6034struct scsi_host_template lpfc_vport_template = {
6035 .module = THIS_MODULE,
6036 .name = LPFC_DRIVER_NAME,
James Smart08dcd4c2016-07-06 12:35:59 -07006037 .proc_name = LPFC_DRIVER_NAME,
James Smart3de2a652007-08-02 11:09:59 -04006038 .info = lpfc_info,
6039 .queuecommand = lpfc_queuecommand,
Christoph Hellwigb6a05c82017-01-30 13:18:58 +01006040 .eh_timed_out = fc_eh_timed_out,
James Smart3de2a652007-08-02 11:09:59 -04006041 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04006042 .eh_device_reset_handler = lpfc_device_reset_handler,
6043 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04006044 .slave_alloc = lpfc_slave_alloc,
6045 .slave_configure = lpfc_slave_configure,
6046 .slave_destroy = lpfc_slave_destroy,
6047 .scan_finished = lpfc_scan_finished,
6048 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05006049 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04006050 .cmd_per_lun = LPFC_CMD_PER_LUN,
6051 .use_clustering = ENABLE_CLUSTERING,
6052 .shost_attrs = lpfc_vport_attrs,
6053 .max_sectors = 0xFFFF,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01006054 .change_queue_depth = scsi_change_queue_depth,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01006055 .track_queue_depth = 1,
James Smart3de2a652007-08-02 11:09:59 -04006056};