blob: 522854920369fde0364027da36462fc0949eb911 [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 Smart16a59fb2014-04-04 13:52:41 -04004 * Copyright (C) 2004-2014 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
dea31012005-04-17 16:05:31 -050021#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050023#include <linux/interrupt.h>
Paul Gortmaker09703662011-05-27 09:37:25 -040024#include <linux/export.h>
James Smarta90f5682006-08-17 11:58:04 -040025#include <linux/delay.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050026#include <asm/unaligned.h>
James Smart737d4242013-04-17 20:14:49 -040027#include <linux/crc-t10dif.h>
28#include <net/checksum.h>
dea31012005-04-17 16:05:31 -050029
30#include <scsi/scsi.h>
31#include <scsi/scsi_device.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050032#include <scsi/scsi_eh.h>
dea31012005-04-17 16:05:31 -050033#include <scsi/scsi_host.h>
34#include <scsi/scsi_tcq.h>
35#include <scsi/scsi_transport_fc.h>
36
37#include "lpfc_version.h"
James Smartda0436e2009-05-22 14:51:39 -040038#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050039#include "lpfc_hw.h"
40#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040041#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040042#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050043#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050044#include "lpfc.h"
James Smart9a6b09c2012-03-01 22:37:42 -050045#include "lpfc_scsi.h"
dea31012005-04-17 16:05:31 -050046#include "lpfc_logmsg.h"
47#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050048#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050049
50#define LPFC_RESET_WAIT 2
51#define LPFC_ABORT_WAIT 2
52
James Smart737d4242013-04-17 20:14:49 -040053int _dump_buf_done = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -050054
55static char *dif_op_str[] = {
James Smart9a6b09c2012-03-01 22:37:42 -050056 "PROT_NORMAL",
57 "PROT_READ_INSERT",
58 "PROT_WRITE_STRIP",
59 "PROT_READ_STRIP",
60 "PROT_WRITE_INSERT",
61 "PROT_READ_PASS",
62 "PROT_WRITE_PASS",
63};
64
James Smartf9bb2da2011-10-10 21:34:11 -040065struct scsi_dif_tuple {
66 __be16 guard_tag; /* Checksum */
67 __be16 app_tag; /* Opaque storage */
68 __be32 ref_tag; /* Target LBA or indirect LBA */
69};
70
James Smart1ba981f2014-02-20 09:56:45 -050071static struct lpfc_rport_data *
72lpfc_rport_data_from_scsi_device(struct scsi_device *sdev)
73{
74 struct lpfc_vport *vport = (struct lpfc_vport *)sdev->host->hostdata;
75
James Smartf38fa0b2014-04-04 13:52:21 -040076 if (vport->phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -050077 return ((struct lpfc_device_data *)sdev->hostdata)->rport_data;
78 else
79 return (struct lpfc_rport_data *)sdev->hostdata;
80}
81
James Smartda0436e2009-05-22 14:51:39 -040082static void
83lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smart1c6f4ef52009-11-18 15:40:49 -050084static void
85lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smart9c6aa9d2013-05-31 17:03:39 -040086static int
87lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc);
James Smarte2a0a9d2008-12-04 22:40:02 -050088
89static void
James Smart6a9c52c2009-10-02 15:16:51 -040090lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -050091{
92 void *src, *dst;
93 struct scatterlist *sgde = scsi_sglist(cmnd);
94
95 if (!_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040096 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
97 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -050098 __func__);
99 return;
100 }
101
102
103 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -0400104 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
105 "9051 BLKGRD: ERROR: data scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -0500106 return;
107 }
108
109 dst = (void *) _dump_buf_data;
110 while (sgde) {
111 src = sg_virt(sgde);
112 memcpy(dst, src, sgde->length);
113 dst += sgde->length;
114 sgde = sg_next(sgde);
115 }
116}
117
118static void
James Smart6a9c52c2009-10-02 15:16:51 -0400119lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -0500120{
121 void *src, *dst;
122 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
123
124 if (!_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -0400125 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
126 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -0500127 __func__);
128 return;
129 }
130
131 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -0400132 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
133 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -0500134 return;
135 }
136
137 dst = _dump_buf_dif;
138 while (sgde) {
139 src = sg_virt(sgde);
140 memcpy(dst, src, sgde->length);
141 dst += sgde->length;
142 sgde = sg_next(sgde);
143 }
144}
145
James Smart9c6aa9d2013-05-31 17:03:39 -0400146static inline unsigned
147lpfc_cmd_blksize(struct scsi_cmnd *sc)
148{
149 return sc->device->sector_size;
150}
151
152#define LPFC_CHECK_PROTECT_GUARD 1
153#define LPFC_CHECK_PROTECT_REF 2
154static inline unsigned
155lpfc_cmd_protect(struct scsi_cmnd *sc, int flag)
156{
157 return 1;
158}
159
160static inline unsigned
161lpfc_cmd_guard_csum(struct scsi_cmnd *sc)
162{
163 if (lpfc_prot_group_type(NULL, sc) == LPFC_PG_TYPE_NO_DIF)
164 return 0;
165 if (scsi_host_get_guard(sc->device->host) == SHOST_DIX_GUARD_IP)
166 return 1;
167 return 0;
168}
169
James Smartea2151b2008-09-07 11:52:10 -0400170/**
James Smartf1126682009-06-10 17:22:44 -0400171 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
172 * @phba: Pointer to HBA object.
173 * @lpfc_cmd: lpfc scsi command object pointer.
174 *
175 * This function is called from the lpfc_prep_task_mgmt_cmd function to
176 * set the last bit in the response sge entry.
177 **/
178static void
179lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
180 struct lpfc_scsi_buf *lpfc_cmd)
181{
182 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
183 if (sgl) {
184 sgl += 1;
185 sgl->word2 = le32_to_cpu(sgl->word2);
186 bf_set(lpfc_sli4_sge_last, sgl, 1);
187 sgl->word2 = cpu_to_le32(sgl->word2);
188 }
189}
190
191/**
James Smart3621a712009-04-06 18:47:14 -0400192 * lpfc_update_stats - Update statistical data for the command completion
James Smartea2151b2008-09-07 11:52:10 -0400193 * @phba: Pointer to HBA object.
194 * @lpfc_cmd: lpfc scsi command object pointer.
195 *
196 * This function is called when there is a command completion and this
197 * function updates the statistical data for the command completion.
198 **/
199static void
200lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
201{
202 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
203 struct lpfc_nodelist *pnode = rdata->pnode;
204 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
205 unsigned long flags;
206 struct Scsi_Host *shost = cmd->device->host;
207 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
208 unsigned long latency;
209 int i;
210
211 if (cmd->result)
212 return;
213
James Smart9f1e1b52008-12-04 22:39:40 -0500214 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
215
James Smartea2151b2008-09-07 11:52:10 -0400216 spin_lock_irqsave(shost->host_lock, flags);
217 if (!vport->stat_data_enabled ||
218 vport->stat_data_blocked ||
James Smart5989b8d2010-10-22 11:06:56 -0400219 !pnode ||
James Smartea2151b2008-09-07 11:52:10 -0400220 !pnode->lat_data ||
221 (phba->bucket_type == LPFC_NO_BUCKET)) {
222 spin_unlock_irqrestore(shost->host_lock, flags);
223 return;
224 }
James Smartea2151b2008-09-07 11:52:10 -0400225
226 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
227 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
228 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -0500229 /* check array subscript bounds */
230 if (i < 0)
231 i = 0;
232 else if (i >= LPFC_MAX_BUCKET_COUNT)
233 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -0400234 } else {
235 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
236 if (latency <= (phba->bucket_base +
237 ((1<<i)*phba->bucket_step)))
238 break;
239 }
240
241 pnode->lat_data[i].cmd_count++;
242 spin_unlock_irqrestore(shost->host_lock, flags);
243}
244
James Smartea2151b2008-09-07 11:52:10 -0400245/**
James Smart5ffc2662009-11-18 15:39:44 -0500246 * lpfc_change_queue_depth - Alter scsi device queue depth
247 * @sdev: Pointer the scsi device on which to change the queue depth.
248 * @qdepth: New queue depth to set the sdev to.
249 * @reason: The reason for the queue depth change.
250 *
251 * This function is called by the midlayer and the LLD to alter the queue
252 * depth for a scsi device. This function sets the queue depth to the new
253 * value and sends an event out to log the queue depth change.
254 **/
Rashika Kheria7bfe7812014-09-03 12:55:36 -0400255static int
James Smart5ffc2662009-11-18 15:39:44 -0500256lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
257{
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100258 scsi_adjust_queue_depth(sdev, qdepth);
James Smart5ffc2662009-11-18 15:39:44 -0500259 return sdev->queue_depth;
260}
261
262/**
James Smart3621a712009-04-06 18:47:14 -0400263 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500264 * @phba: The Hba for which this call is being executed.
265 *
266 * This routine is called when there is resource error in driver or firmware.
267 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
268 * posts at most 1 event each second. This routine wakes up worker thread of
269 * @phba to process WORKER_RAM_DOWN_EVENT event.
270 *
271 * This routine should be called with no lock held.
272 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500273void
James Smarteaf15d52008-12-04 22:39:29 -0500274lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500275{
276 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400277 uint32_t evt_posted;
Manuel Schölling0d4aec12014-09-03 12:55:58 -0400278 unsigned long expires;
James Smart92d7f7b2007-06-17 19:56:38 -0500279
280 spin_lock_irqsave(&phba->hbalock, flags);
281 atomic_inc(&phba->num_rsrc_err);
282 phba->last_rsrc_error_time = jiffies;
283
Manuel Schölling0d4aec12014-09-03 12:55:58 -0400284 expires = phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL;
285 if (time_after(expires, jiffies)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500286 spin_unlock_irqrestore(&phba->hbalock, flags);
287 return;
288 }
289
290 phba->last_ramp_down_time = jiffies;
291
292 spin_unlock_irqrestore(&phba->hbalock, flags);
293
294 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400295 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
296 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500297 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500298 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
299
James Smart5e9d9b82008-06-14 22:52:53 -0400300 if (!evt_posted)
301 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500302 return;
303}
304
James Smart9bad7672008-12-04 22:39:02 -0500305/**
James Smart3621a712009-04-06 18:47:14 -0400306 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500307 * @phba: The Hba for which this call is being executed.
308 *
309 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
310 * thread.This routine reduces queue depth for all scsi device on each vport
311 * associated with @phba.
312 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500313void
314lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
315{
James Smart549e55c2007-08-02 11:09:51 -0400316 struct lpfc_vport **vports;
317 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500318 struct scsi_device *sdev;
James Smart5ffc2662009-11-18 15:39:44 -0500319 unsigned long new_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500320 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400321 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500322
323 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
324 num_cmd_success = atomic_read(&phba->num_cmd_success);
325
James Smart75ad83a2012-05-09 21:18:40 -0400326 /*
327 * The error and success command counters are global per
328 * driver instance. If another handler has already
329 * operated on this error event, just exit.
330 */
331 if (num_rsrc_err == 0)
332 return;
333
James Smart549e55c2007-08-02 11:09:51 -0400334 vports = lpfc_create_vport_work_array(phba);
335 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400336 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400337 shost = lpfc_shost_from_vport(vports[i]);
338 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500339 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400340 sdev->queue_depth * num_rsrc_err /
341 (num_rsrc_err + num_cmd_success);
342 if (!new_queue_depth)
343 new_queue_depth = sdev->queue_depth - 1;
344 else
345 new_queue_depth = sdev->queue_depth -
346 new_queue_depth;
James Smart5ffc2662009-11-18 15:39:44 -0500347 lpfc_change_queue_depth(sdev, new_queue_depth,
348 SCSI_QDEPTH_DEFAULT);
James Smart549e55c2007-08-02 11:09:51 -0400349 }
James Smart92d7f7b2007-06-17 19:56:38 -0500350 }
James Smart09372822008-01-11 01:52:54 -0500351 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500352 atomic_set(&phba->num_rsrc_err, 0);
353 atomic_set(&phba->num_cmd_success, 0);
354}
355
James Smart9bad7672008-12-04 22:39:02 -0500356/**
James Smart3621a712009-04-06 18:47:14 -0400357 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400358 * @phba: Pointer to HBA context object.
359 *
360 * This function walks vport list and set each SCSI host to block state
361 * by invoking fc_remote_port_delete() routine. This function is invoked
362 * with EEH when device's PCI slot has been permanently disabled.
363 **/
364void
365lpfc_scsi_dev_block(struct lpfc_hba *phba)
366{
367 struct lpfc_vport **vports;
368 struct Scsi_Host *shost;
369 struct scsi_device *sdev;
370 struct fc_rport *rport;
371 int i;
372
373 vports = lpfc_create_vport_work_array(phba);
374 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400375 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400376 shost = lpfc_shost_from_vport(vports[i]);
377 shost_for_each_device(sdev, shost) {
378 rport = starget_to_rport(scsi_target(sdev));
379 fc_remote_port_delete(rport);
380 }
381 }
382 lpfc_destroy_vport_work_array(phba, vports);
383}
384
James Smart9bad7672008-12-04 22:39:02 -0500385/**
James Smart3772a992009-05-22 14:50:54 -0400386 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500387 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400388 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500389 *
James Smart3772a992009-05-22 14:50:54 -0400390 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
391 * the scsi buffer contains all the necessary information needed to initiate
392 * a SCSI I/O. The non-DMAable buffer region contains information to build
393 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
394 * and the initial BPL. In addition to allocating memory, the FCP CMND and
395 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500396 *
397 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400398 * int - number of scsi buffers that were allocated.
399 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500400 **/
James Smart3772a992009-05-22 14:50:54 -0400401static int
402lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500403{
James Smart2e0fef82007-06-17 19:56:36 -0500404 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500405 struct lpfc_scsi_buf *psb;
406 struct ulp_bde64 *bpl;
407 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400408 dma_addr_t pdma_phys_fcp_cmd;
409 dma_addr_t pdma_phys_fcp_rsp;
410 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500411 uint16_t iotag;
James Smart96f70772013-04-17 20:16:15 -0400412 int bcnt, bpl_size;
413
414 bpl_size = phba->cfg_sg_dma_buf_size -
415 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
416
417 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
418 "9067 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
419 num_to_alloc, phba->cfg_sg_dma_buf_size,
420 (int)sizeof(struct fcp_cmnd),
421 (int)sizeof(struct fcp_rsp), bpl_size);
dea31012005-04-17 16:05:31 -0500422
James Smart3772a992009-05-22 14:50:54 -0400423 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
424 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
425 if (!psb)
426 break;
dea31012005-04-17 16:05:31 -0500427
James Smart3772a992009-05-22 14:50:54 -0400428 /*
429 * Get memory from the pci pool to map the virt space to pci
430 * bus space for an I/O. The DMA buffer includes space for the
431 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
432 * necessary to support the sg_tablesize.
433 */
434 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
435 GFP_KERNEL, &psb->dma_handle);
436 if (!psb->data) {
437 kfree(psb);
438 break;
439 }
dea31012005-04-17 16:05:31 -0500440
James Smart3772a992009-05-22 14:50:54 -0400441 /* Initialize virtual ptrs to dma_buf region. */
442 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
dea31012005-04-17 16:05:31 -0500443
James Smart3772a992009-05-22 14:50:54 -0400444 /* Allocate iotag for psb->cur_iocbq. */
445 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
446 if (iotag == 0) {
447 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
448 psb->data, psb->dma_handle);
449 kfree(psb);
450 break;
451 }
452 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500453
James Smart3772a992009-05-22 14:50:54 -0400454 psb->fcp_cmnd = psb->data;
455 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
456 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400457 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500458
James Smart3772a992009-05-22 14:50:54 -0400459 /* Initialize local short-hand pointers. */
460 bpl = psb->fcp_bpl;
461 pdma_phys_fcp_cmd = psb->dma_handle;
462 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
463 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
464 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500465
James Smart3772a992009-05-22 14:50:54 -0400466 /*
467 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
468 * are sg list bdes. Initialize the first two and leave the
469 * rest for queuecommand.
470 */
471 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
472 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
473 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
474 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
475 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500476
James Smart3772a992009-05-22 14:50:54 -0400477 /* Setup the physical region for the FCP RSP */
478 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
479 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
480 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
481 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
482 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
483
484 /*
485 * Since the IOCB for the FCP I/O is built into this
486 * lpfc_scsi_buf, initialize it with all known data now.
487 */
488 iocb = &psb->cur_iocbq.iocb;
489 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
490 if ((phba->sli_rev == 3) &&
491 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
492 /* fill in immediate fcp command BDE */
493 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
494 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
495 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
496 unsli3.fcp_ext.icd);
497 iocb->un.fcpi64.bdl.addrHigh = 0;
498 iocb->ulpBdeCount = 0;
499 iocb->ulpLe = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300500 /* fill in response BDE */
James Smart3772a992009-05-22 14:50:54 -0400501 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
502 BUFF_TYPE_BDE_64;
503 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
504 sizeof(struct fcp_rsp);
505 iocb->unsli3.fcp_ext.rbde.addrLow =
506 putPaddrLow(pdma_phys_fcp_rsp);
507 iocb->unsli3.fcp_ext.rbde.addrHigh =
508 putPaddrHigh(pdma_phys_fcp_rsp);
509 } else {
510 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
511 iocb->un.fcpi64.bdl.bdeSize =
512 (2 * sizeof(struct ulp_bde64));
513 iocb->un.fcpi64.bdl.addrLow =
514 putPaddrLow(pdma_phys_bpl);
515 iocb->un.fcpi64.bdl.addrHigh =
516 putPaddrHigh(pdma_phys_bpl);
517 iocb->ulpBdeCount = 1;
518 iocb->ulpLe = 1;
519 }
520 iocb->ulpClass = CLASS3;
521 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400522 /* Put it back into the SCSI buffer list */
James Smarteee88772010-09-29 11:19:08 -0400523 psb->cur_iocbq.context1 = psb;
James Smart1c6f4ef52009-11-18 15:40:49 -0500524 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400525
James Smart34b02dc2008-08-24 21:49:55 -0400526 }
dea31012005-04-17 16:05:31 -0500527
James Smart3772a992009-05-22 14:50:54 -0400528 return bcnt;
dea31012005-04-17 16:05:31 -0500529}
530
James Smart9bad7672008-12-04 22:39:02 -0500531/**
James Smart1151e3e2011-02-16 12:39:35 -0500532 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
533 * @vport: pointer to lpfc vport data structure.
534 *
535 * This routine is invoked by the vport cleanup for deletions and the cleanup
536 * for an ndlp on removal.
537 **/
538void
539lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
540{
541 struct lpfc_hba *phba = vport->phba;
542 struct lpfc_scsi_buf *psb, *next_psb;
543 unsigned long iflag = 0;
544
545 spin_lock_irqsave(&phba->hbalock, iflag);
546 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
547 list_for_each_entry_safe(psb, next_psb,
548 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
549 if (psb->rdata && psb->rdata->pnode
550 && psb->rdata->pnode->vport == vport)
551 psb->rdata = NULL;
552 }
553 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
554 spin_unlock_irqrestore(&phba->hbalock, iflag);
555}
556
557/**
James Smartda0436e2009-05-22 14:51:39 -0400558 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
559 * @phba: pointer to lpfc hba data structure.
560 * @axri: pointer to the fcp xri abort wcqe structure.
561 *
562 * This routine is invoked by the worker thread to process a SLI4 fast-path
563 * FCP aborted xri.
564 **/
565void
566lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
567 struct sli4_wcqe_xri_aborted *axri)
568{
569 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500570 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartda0436e2009-05-22 14:51:39 -0400571 struct lpfc_scsi_buf *psb, *next_psb;
572 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500573 struct lpfc_iocbq *iocbq;
574 int i;
James Smart19ca7602010-11-20 23:11:55 -0500575 struct lpfc_nodelist *ndlp;
576 int rrq_empty = 0;
James Smart589a52d2010-07-14 15:30:54 -0400577 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smartda0436e2009-05-22 14:51:39 -0400578
James Smart0f65ff62010-02-26 14:14:23 -0500579 spin_lock_irqsave(&phba->hbalock, iflag);
580 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400581 list_for_each_entry_safe(psb, next_psb,
582 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
583 if (psb->cur_iocbq.sli4_xritag == xri) {
584 list_del(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500585 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400586 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500587 spin_unlock(
588 &phba->sli4_hba.abts_scsi_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500589 if (psb->rdata && psb->rdata->pnode)
590 ndlp = psb->rdata->pnode;
591 else
592 ndlp = NULL;
593
James Smart19ca7602010-11-20 23:11:55 -0500594 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500595 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartcb69f7d2011-12-13 13:21:57 -0500596 if (ndlp) {
James Smartee0f4fe2012-05-09 21:19:14 -0400597 lpfc_set_rrq_active(phba, ndlp,
598 psb->cur_iocbq.sli4_lxritag, rxid, 1);
James Smartcb69f7d2011-12-13 13:21:57 -0500599 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
600 }
James Smartda0436e2009-05-22 14:51:39 -0400601 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500602 if (rrq_empty)
603 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400604 return;
605 }
606 }
James Smart0f65ff62010-02-26 14:14:23 -0500607 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
608 for (i = 1; i <= phba->sli.last_iotag; i++) {
609 iocbq = phba->sli.iocbq_lookup[i];
610
611 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
612 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
613 continue;
614 if (iocbq->sli4_xritag != xri)
615 continue;
616 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
617 psb->exch_busy = 0;
618 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart0e9bb8d2013-03-01 16:35:12 -0500619 if (!list_empty(&pring->txq))
James Smart589a52d2010-07-14 15:30:54 -0400620 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500621 return;
622
623 }
624 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400625}
626
627/**
James Smart8a9d2e82012-05-09 21:16:12 -0400628 * lpfc_sli4_post_scsi_sgl_list - Psot blocks of scsi buffer sgls from a list
629 * @phba: pointer to lpfc hba data structure.
630 * @post_sblist: pointer to the scsi buffer list.
631 *
632 * This routine walks a list of scsi buffers that was passed in. It attempts
633 * to construct blocks of scsi buffer sgls which contains contiguous xris and
634 * uses the non-embedded SGL block post mailbox commands to post to the port.
635 * For single SCSI buffer sgl with non-contiguous xri, if any, it shall use
636 * embedded SGL post mailbox command for posting. The @post_sblist passed in
637 * must be local list, thus no lock is needed when manipulate the list.
638 *
639 * Returns: 0 = failure, non-zero number of successfully posted buffers.
640 **/
Rashika Kheria7bfe7812014-09-03 12:55:36 -0400641static int
James Smart8a9d2e82012-05-09 21:16:12 -0400642lpfc_sli4_post_scsi_sgl_list(struct lpfc_hba *phba,
643 struct list_head *post_sblist, int sb_count)
644{
645 struct lpfc_scsi_buf *psb, *psb_next;
James Smart96f70772013-04-17 20:16:15 -0400646 int status, sgl_size;
James Smart8a9d2e82012-05-09 21:16:12 -0400647 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
648 dma_addr_t pdma_phys_bpl1;
649 int last_xritag = NO_XRI;
650 LIST_HEAD(prep_sblist);
651 LIST_HEAD(blck_sblist);
652 LIST_HEAD(scsi_sblist);
653
654 /* sanity check */
655 if (sb_count <= 0)
656 return -EINVAL;
657
James Smart96f70772013-04-17 20:16:15 -0400658 sgl_size = phba->cfg_sg_dma_buf_size -
659 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
660
James Smart8a9d2e82012-05-09 21:16:12 -0400661 list_for_each_entry_safe(psb, psb_next, post_sblist, list) {
662 list_del_init(&psb->list);
663 block_cnt++;
664 if ((last_xritag != NO_XRI) &&
665 (psb->cur_iocbq.sli4_xritag != last_xritag + 1)) {
666 /* a hole in xri block, form a sgl posting block */
667 list_splice_init(&prep_sblist, &blck_sblist);
668 post_cnt = block_cnt - 1;
669 /* prepare list for next posting block */
670 list_add_tail(&psb->list, &prep_sblist);
671 block_cnt = 1;
672 } else {
673 /* prepare list for next posting block */
674 list_add_tail(&psb->list, &prep_sblist);
675 /* enough sgls for non-embed sgl mbox command */
676 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
677 list_splice_init(&prep_sblist, &blck_sblist);
678 post_cnt = block_cnt;
679 block_cnt = 0;
680 }
681 }
682 num_posting++;
683 last_xritag = psb->cur_iocbq.sli4_xritag;
684
685 /* end of repost sgl list condition for SCSI buffers */
686 if (num_posting == sb_count) {
687 if (post_cnt == 0) {
688 /* last sgl posting block */
689 list_splice_init(&prep_sblist, &blck_sblist);
690 post_cnt = block_cnt;
691 } else if (block_cnt == 1) {
692 /* last single sgl with non-contiguous xri */
James Smart96f70772013-04-17 20:16:15 -0400693 if (sgl_size > SGL_PAGE_SIZE)
James Smart8a9d2e82012-05-09 21:16:12 -0400694 pdma_phys_bpl1 = psb->dma_phys_bpl +
695 SGL_PAGE_SIZE;
696 else
697 pdma_phys_bpl1 = 0;
698 status = lpfc_sli4_post_sgl(phba,
699 psb->dma_phys_bpl,
700 pdma_phys_bpl1,
701 psb->cur_iocbq.sli4_xritag);
702 if (status) {
703 /* failure, put on abort scsi list */
704 psb->exch_busy = 1;
705 } else {
706 /* success, put on SCSI buffer list */
707 psb->exch_busy = 0;
708 psb->status = IOSTAT_SUCCESS;
709 num_posted++;
710 }
711 /* success, put on SCSI buffer sgl list */
712 list_add_tail(&psb->list, &scsi_sblist);
713 }
714 }
715
716 /* continue until a nembed page worth of sgls */
717 if (post_cnt == 0)
718 continue;
719
720 /* post block of SCSI buffer list sgls */
721 status = lpfc_sli4_post_scsi_sgl_block(phba, &blck_sblist,
722 post_cnt);
723
724 /* don't reset xirtag due to hole in xri block */
725 if (block_cnt == 0)
726 last_xritag = NO_XRI;
727
728 /* reset SCSI buffer post count for next round of posting */
729 post_cnt = 0;
730
731 /* put posted SCSI buffer-sgl posted on SCSI buffer sgl list */
732 while (!list_empty(&blck_sblist)) {
733 list_remove_head(&blck_sblist, psb,
734 struct lpfc_scsi_buf, list);
735 if (status) {
736 /* failure, put on abort scsi list */
737 psb->exch_busy = 1;
738 } else {
739 /* success, put on SCSI buffer list */
740 psb->exch_busy = 0;
741 psb->status = IOSTAT_SUCCESS;
742 num_posted++;
743 }
744 list_add_tail(&psb->list, &scsi_sblist);
745 }
746 }
747 /* Push SCSI buffers with sgl posted to the availble list */
748 while (!list_empty(&scsi_sblist)) {
749 list_remove_head(&scsi_sblist, psb,
750 struct lpfc_scsi_buf, list);
751 lpfc_release_scsi_buf_s4(phba, psb);
752 }
753 return num_posted;
754}
755
756/**
757 * lpfc_sli4_repost_scsi_sgl_list - Repsot all the allocated scsi buffer sgls
James Smartda0436e2009-05-22 14:51:39 -0400758 * @phba: pointer to lpfc hba data structure.
759 *
760 * This routine walks the list of scsi buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -0400761 * repost them to the port by using SGL block post. This is needed after a
James Smartda0436e2009-05-22 14:51:39 -0400762 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
763 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
764 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
765 *
766 * Returns: 0 = success, non-zero failure.
767 **/
768int
769lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
770{
James Smart8a9d2e82012-05-09 21:16:12 -0400771 LIST_HEAD(post_sblist);
772 int num_posted, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -0400773
James Smart8a9d2e82012-05-09 21:16:12 -0400774 /* get all SCSI buffers need to repost to a local list */
James Smarta40fc5f2013-04-17 20:17:40 -0400775 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -0400776 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400777 list_splice_init(&phba->lpfc_scsi_buf_list_get, &post_sblist);
778 list_splice(&phba->lpfc_scsi_buf_list_put, &post_sblist);
James Smart164cecd2013-09-06 12:22:38 -0400779 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400780 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smartda0436e2009-05-22 14:51:39 -0400781
James Smart8a9d2e82012-05-09 21:16:12 -0400782 /* post the list of scsi buffer sgls to port if available */
783 if (!list_empty(&post_sblist)) {
784 num_posted = lpfc_sli4_post_scsi_sgl_list(phba, &post_sblist,
785 phba->sli4_hba.scsi_xri_cnt);
786 /* failed to post any scsi buffer, return error */
787 if (num_posted == 0)
788 rc = -EIO;
James Smartda0436e2009-05-22 14:51:39 -0400789 }
790 return rc;
791}
792
793/**
794 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
795 * @vport: The virtual port for which this call being executed.
796 * @num_to_allocate: The requested number of buffers to allocate.
797 *
James Smart8a9d2e82012-05-09 21:16:12 -0400798 * This routine allocates scsi buffers for device with SLI-4 interface spec,
James Smartda0436e2009-05-22 14:51:39 -0400799 * the scsi buffer contains all the necessary information needed to initiate
James Smart8a9d2e82012-05-09 21:16:12 -0400800 * a SCSI I/O. After allocating up to @num_to_allocate SCSI buffers and put
801 * them on a list, it post them to the port by using SGL block post.
James Smartda0436e2009-05-22 14:51:39 -0400802 *
803 * Return codes:
James Smart8a9d2e82012-05-09 21:16:12 -0400804 * int - number of scsi buffers that were allocated and posted.
James Smartda0436e2009-05-22 14:51:39 -0400805 * 0 = failure, less than num_to_alloc is a partial failure.
806 **/
807static int
808lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
809{
810 struct lpfc_hba *phba = vport->phba;
811 struct lpfc_scsi_buf *psb;
812 struct sli4_sge *sgl;
813 IOCB_t *iocb;
814 dma_addr_t pdma_phys_fcp_cmd;
815 dma_addr_t pdma_phys_fcp_rsp;
James Smart96f70772013-04-17 20:16:15 -0400816 dma_addr_t pdma_phys_bpl;
James Smart8a9d2e82012-05-09 21:16:12 -0400817 uint16_t iotag, lxri = 0;
James Smart96f70772013-04-17 20:16:15 -0400818 int bcnt, num_posted, sgl_size;
James Smart8a9d2e82012-05-09 21:16:12 -0400819 LIST_HEAD(prep_sblist);
820 LIST_HEAD(post_sblist);
821 LIST_HEAD(scsi_sblist);
James Smartda0436e2009-05-22 14:51:39 -0400822
James Smart96f70772013-04-17 20:16:15 -0400823 sgl_size = phba->cfg_sg_dma_buf_size -
824 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
825
826 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
827 "9068 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
828 num_to_alloc, phba->cfg_sg_dma_buf_size, sgl_size,
829 (int)sizeof(struct fcp_cmnd),
830 (int)sizeof(struct fcp_rsp));
831
James Smartda0436e2009-05-22 14:51:39 -0400832 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
833 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
834 if (!psb)
835 break;
James Smartda0436e2009-05-22 14:51:39 -0400836 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400837 * Get memory from the pci pool to map the virt space to
838 * pci bus space for an I/O. The DMA buffer includes space
839 * for the struct fcp_cmnd, struct fcp_rsp and the number
840 * of bde's necessary to support the sg_tablesize.
James Smartda0436e2009-05-22 14:51:39 -0400841 */
842 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
843 GFP_KERNEL, &psb->dma_handle);
844 if (!psb->data) {
845 kfree(psb);
846 break;
847 }
James Smartda0436e2009-05-22 14:51:39 -0400848 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
849
James Smart092cb032013-09-06 12:21:50 -0400850 /*
851 * 4K Page alignment is CRITICAL to BlockGuard, double check
852 * to be sure.
853 */
854 if (phba->cfg_enable_bg && (((unsigned long)(psb->data) &
855 (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0)) {
James Smart96f70772013-04-17 20:16:15 -0400856 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
857 psb->data, psb->dma_handle);
858 kfree(psb);
859 break;
860 }
861
James Smartda0436e2009-05-22 14:51:39 -0400862
James Smart6d368e52011-05-24 11:44:12 -0400863 lxri = lpfc_sli4_next_xritag(phba);
864 if (lxri == NO_XRI) {
James Smartda0436e2009-05-22 14:51:39 -0400865 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
866 psb->data, psb->dma_handle);
867 kfree(psb);
868 break;
869 }
James Smartf7bc6432013-10-10 12:19:53 -0400870
871 /* Allocate iotag for psb->cur_iocbq. */
872 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
873 if (iotag == 0) {
874 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
875 psb->data, psb->dma_handle);
876 kfree(psb);
877 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
878 "3368 Failed to allocated IOTAG for"
879 " XRI:0x%x\n", lxri);
880 lpfc_sli4_free_xri(phba, lxri);
881 break;
882 }
James Smart6d368e52011-05-24 11:44:12 -0400883 psb->cur_iocbq.sli4_lxritag = lxri;
884 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smartda0436e2009-05-22 14:51:39 -0400885 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Smartda0436e2009-05-22 14:51:39 -0400886 psb->fcp_bpl = psb->data;
James Smart96f70772013-04-17 20:16:15 -0400887 psb->fcp_cmnd = (psb->data + sgl_size);
James Smartda0436e2009-05-22 14:51:39 -0400888 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
889 sizeof(struct fcp_cmnd));
890
891 /* Initialize local short-hand pointers. */
892 sgl = (struct sli4_sge *)psb->fcp_bpl;
893 pdma_phys_bpl = psb->dma_handle;
James Smart96f70772013-04-17 20:16:15 -0400894 pdma_phys_fcp_cmd = (psb->dma_handle + sgl_size);
James Smartda0436e2009-05-22 14:51:39 -0400895 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
896
897 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400898 * The first two bdes are the FCP_CMD and FCP_RSP.
899 * The balance are sg list bdes. Initialize the
900 * first two and leave the rest for queuecommand.
James Smartda0436e2009-05-22 14:51:39 -0400901 */
902 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
903 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
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, 0);
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_cmnd));
James Smartda0436e2009-05-22 14:51:39 -0400908 sgl++;
909
910 /* Setup the physical region for the FCP RSP */
911 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
912 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smart05580562011-05-24 11:40:48 -0400913 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400914 bf_set(lpfc_sli4_sge_last, sgl, 1);
915 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500916 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -0400917
918 /*
919 * Since the IOCB for the FCP I/O is built into this
920 * lpfc_scsi_buf, initialize it with all known data now.
921 */
922 iocb = &psb->cur_iocbq.iocb;
923 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
924 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
925 /* setting the BLP size to 2 * sizeof BDE may not be correct.
926 * We are setting the bpl to point to out sgl. An sgl's
927 * entries are 16 bytes, a bpl entries are 12 bytes.
928 */
929 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
930 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
931 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
932 iocb->ulpBdeCount = 1;
933 iocb->ulpLe = 1;
934 iocb->ulpClass = CLASS3;
James Smart8a9d2e82012-05-09 21:16:12 -0400935 psb->cur_iocbq.context1 = psb;
James Smartda0436e2009-05-22 14:51:39 -0400936 psb->dma_phys_bpl = pdma_phys_bpl;
James Smart6d368e52011-05-24 11:44:12 -0400937
James Smart8a9d2e82012-05-09 21:16:12 -0400938 /* add the scsi buffer to a post list */
939 list_add_tail(&psb->list, &post_sblist);
James Smarta40fc5f2013-04-17 20:17:40 -0400940 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -0400941 phba->sli4_hba.scsi_xri_cnt++;
James Smarta40fc5f2013-04-17 20:17:40 -0400942 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smartda0436e2009-05-22 14:51:39 -0400943 }
James Smart8a9d2e82012-05-09 21:16:12 -0400944 lpfc_printf_log(phba, KERN_INFO, LOG_BG,
945 "3021 Allocate %d out of %d requested new SCSI "
946 "buffers\n", bcnt, num_to_alloc);
James Smartda0436e2009-05-22 14:51:39 -0400947
James Smart8a9d2e82012-05-09 21:16:12 -0400948 /* post the list of scsi buffer sgls to port if available */
949 if (!list_empty(&post_sblist))
950 num_posted = lpfc_sli4_post_scsi_sgl_list(phba,
951 &post_sblist, bcnt);
952 else
953 num_posted = 0;
954
955 return num_posted;
James Smartda0436e2009-05-22 14:51:39 -0400956}
957
958/**
James Smart3772a992009-05-22 14:50:54 -0400959 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
960 * @vport: The virtual port for which this call being executed.
961 * @num_to_allocate: The requested number of buffers to allocate.
962 *
963 * This routine wraps the actual SCSI buffer allocator function pointer from
964 * the lpfc_hba struct.
965 *
966 * Return codes:
967 * int - number of scsi buffers that were allocated.
968 * 0 = failure, less than num_to_alloc is a partial failure.
969 **/
970static inline int
971lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
972{
973 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
974}
975
976/**
James Smart19ca7602010-11-20 23:11:55 -0500977 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -0400978 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -0500979 *
980 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
981 * and returns to caller.
982 *
983 * Return codes:
984 * NULL - Error
985 * Pointer to lpfc_scsi_buf - Success
986 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +0100987static struct lpfc_scsi_buf*
James Smart19ca7602010-11-20 23:11:55 -0500988lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -0500989{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400990 struct lpfc_scsi_buf * lpfc_cmd = NULL;
James Smarta40fc5f2013-04-17 20:17:40 -0400991 struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
James Smart164cecd2013-09-06 12:22:38 -0400992 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500993
James Smart164cecd2013-09-06 12:22:38 -0400994 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
James Smarta40fc5f2013-04-17 20:17:40 -0400995 list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_scsi_buf,
996 list);
997 if (!lpfc_cmd) {
James Smart164cecd2013-09-06 12:22:38 -0400998 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400999 list_splice(&phba->lpfc_scsi_buf_list_put,
1000 &phba->lpfc_scsi_buf_list_get);
1001 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
1002 list_remove_head(scsi_buf_list_get, lpfc_cmd,
1003 struct lpfc_scsi_buf, list);
James Smart164cecd2013-09-06 12:22:38 -04001004 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smart1dcb58e2007-04-25 09:51:30 -04001005 }
James Smart164cecd2013-09-06 12:22:38 -04001006 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001007 return lpfc_cmd;
1008}
James Smart19ca7602010-11-20 23:11:55 -05001009/**
1010 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1011 * @phba: The HBA for which this call is being executed.
1012 *
1013 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1014 * and returns to caller.
1015 *
1016 * Return codes:
1017 * NULL - Error
1018 * Pointer to lpfc_scsi_buf - Success
1019 **/
1020static struct lpfc_scsi_buf*
1021lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1022{
James Smart3be30e02013-05-31 17:05:17 -04001023 struct lpfc_scsi_buf *lpfc_cmd, *lpfc_cmd_next;
James Smart164cecd2013-09-06 12:22:38 -04001024 unsigned long iflag = 0;
James Smart19ca7602010-11-20 23:11:55 -05001025 int found = 0;
1026
James Smart164cecd2013-09-06 12:22:38 -04001027 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
James Smart3be30e02013-05-31 17:05:17 -04001028 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1029 &phba->lpfc_scsi_buf_list_get, list) {
James Smart19ca7602010-11-20 23:11:55 -05001030 if (lpfc_test_rrq_active(phba, ndlp,
James Smartee0f4fe2012-05-09 21:19:14 -04001031 lpfc_cmd->cur_iocbq.sli4_lxritag))
James Smart1151e3e2011-02-16 12:39:35 -05001032 continue;
1033 list_del(&lpfc_cmd->list);
James Smart19ca7602010-11-20 23:11:55 -05001034 found = 1;
James Smart1151e3e2011-02-16 12:39:35 -05001035 break;
James Smart19ca7602010-11-20 23:11:55 -05001036 }
James Smarta40fc5f2013-04-17 20:17:40 -04001037 if (!found) {
James Smart164cecd2013-09-06 12:22:38 -04001038 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04001039 list_splice(&phba->lpfc_scsi_buf_list_put,
1040 &phba->lpfc_scsi_buf_list_get);
1041 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart164cecd2013-09-06 12:22:38 -04001042 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smart3be30e02013-05-31 17:05:17 -04001043 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1044 &phba->lpfc_scsi_buf_list_get, list) {
James Smarta40fc5f2013-04-17 20:17:40 -04001045 if (lpfc_test_rrq_active(
1046 phba, ndlp, lpfc_cmd->cur_iocbq.sli4_lxritag))
1047 continue;
1048 list_del(&lpfc_cmd->list);
1049 found = 1;
1050 break;
1051 }
1052 }
James Smart164cecd2013-09-06 12:22:38 -04001053 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -05001054 if (!found)
1055 return NULL;
James Smarta40fc5f2013-04-17 20:17:40 -04001056 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001057}
1058/**
1059 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1060 * @phba: The HBA for which this call is being executed.
1061 *
1062 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1063 * and returns to caller.
1064 *
1065 * Return codes:
1066 * NULL - Error
1067 * Pointer to lpfc_scsi_buf - Success
1068 **/
1069static struct lpfc_scsi_buf*
1070lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1071{
1072 return phba->lpfc_get_scsi_buf(phba, ndlp);
1073}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001074
James Smart9bad7672008-12-04 22:39:02 -05001075/**
James Smart3772a992009-05-22 14:50:54 -04001076 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -05001077 * @phba: The Hba for which this call is being executed.
1078 * @psb: The scsi buffer which is being released.
1079 *
1080 * This routine releases @psb scsi buffer by adding it to tail of @phba
1081 * lpfc_scsi_buf_list list.
1082 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001083static void
James Smart3772a992009-05-22 14:50:54 -04001084lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001085{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001086 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001087
James Smarta40fc5f2013-04-17 20:17:40 -04001088 psb->seg_cnt = 0;
1089 psb->nonsg_phys = 0;
1090 psb->prot_seg_cnt = 0;
1091
1092 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001093 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -04001094 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -04001095 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1096 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
dea31012005-04-17 16:05:31 -05001097}
1098
James Smart9bad7672008-12-04 22:39:02 -05001099/**
James Smartda0436e2009-05-22 14:51:39 -04001100 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1101 * @phba: The Hba for which this call is being executed.
1102 * @psb: The scsi buffer which is being released.
1103 *
1104 * This routine releases @psb scsi buffer by adding it to tail of @phba
1105 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1106 * and cannot be reused for at least RA_TOV amount of time if it was
1107 * aborted.
1108 **/
1109static void
1110lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1111{
1112 unsigned long iflag = 0;
1113
James Smarta40fc5f2013-04-17 20:17:40 -04001114 psb->seg_cnt = 0;
1115 psb->nonsg_phys = 0;
1116 psb->prot_seg_cnt = 0;
1117
James Smart341af102010-01-26 23:07:37 -05001118 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -04001119 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1120 iflag);
1121 psb->pCmd = NULL;
1122 list_add_tail(&psb->list,
1123 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1124 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1125 iflag);
1126 } else {
James Smartda0436e2009-05-22 14:51:39 -04001127 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -04001128 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -04001129 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1130 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1131 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -04001132 }
1133}
1134
1135/**
James Smart3772a992009-05-22 14:50:54 -04001136 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1137 * @phba: The Hba for which this call is being executed.
1138 * @psb: The scsi buffer which is being released.
1139 *
1140 * This routine releases @psb scsi buffer by adding it to tail of @phba
1141 * lpfc_scsi_buf_list list.
1142 **/
1143static void
1144lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1145{
1146
1147 phba->lpfc_release_scsi_buf(phba, psb);
1148}
1149
1150/**
1151 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001152 * @phba: The Hba for which this call is being executed.
1153 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1154 *
1155 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001156 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1157 * through sg elements and format the bdea. This routine also initializes all
1158 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001159 *
1160 * Return codes:
1161 * 1 - Error
1162 * 0 - Success
1163 **/
dea31012005-04-17 16:05:31 -05001164static int
James Smart3772a992009-05-22 14:50:54 -04001165lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001166{
1167 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1168 struct scatterlist *sgel = NULL;
1169 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1170 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001171 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001172 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001173 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001174 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001175 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001176 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001177
1178 /*
1179 * There are three possibilities here - use scatter-gather segment, use
1180 * the single mapping, or neither. Start the lpfc command prep by
1181 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1182 * data bde entry.
1183 */
1184 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001185 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001186 /*
1187 * The driver stores the segment count returned from pci_map_sg
1188 * because this a count of dma-mappings used to map the use_sg
1189 * pages. They are not guaranteed to be the same for those
1190 * architectures that implement an IOMMU.
1191 */
dea31012005-04-17 16:05:31 -05001192
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001193 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1194 scsi_sg_count(scsi_cmnd), datadir);
1195 if (unlikely(!nseg))
1196 return 1;
1197
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001198 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001199 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001200 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1201 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001202 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001203 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001204 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04001205 lpfc_cmd->seg_cnt = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001206 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001207 return 1;
1208 }
1209
1210 /*
1211 * The driver established a maximum scatter-gather segment count
1212 * during probe that limits the number of sg elements in any
1213 * single scsi command. Just run through the seg_cnt and format
1214 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001215 * When using SLI-3 the driver will try to fit all the BDEs into
1216 * the IOCB. If it can't then the BDEs get added to a BPL as it
1217 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001218 */
James Smart34b02dc2008-08-24 21:49:55 -04001219 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001220 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001221 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001222 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001223 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001224 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1225 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1226 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1227 data_bde->addrLow = putPaddrLow(physaddr);
1228 data_bde->addrHigh = putPaddrHigh(physaddr);
1229 data_bde++;
1230 } else {
1231 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1232 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1233 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1234 bpl->addrLow =
1235 le32_to_cpu(putPaddrLow(physaddr));
1236 bpl->addrHigh =
1237 le32_to_cpu(putPaddrHigh(physaddr));
1238 bpl++;
1239 }
dea31012005-04-17 16:05:31 -05001240 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001241 }
dea31012005-04-17 16:05:31 -05001242
1243 /*
1244 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001245 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1246 * explicitly reinitialized and for SLI-3 the extended bde count is
1247 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001248 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001249 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001250 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1251 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001252 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1253 /*
1254 * The extended IOCB format can only fit 3 BDE or a BPL.
1255 * This I/O has more than 3 BDE so the 1st data bde will
1256 * be a BPL that is filled in here.
1257 */
1258 physaddr = lpfc_cmd->dma_handle;
1259 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1260 data_bde->tus.f.bdeSize = (num_bde *
1261 sizeof(struct ulp_bde64));
1262 physaddr += (sizeof(struct fcp_cmnd) +
1263 sizeof(struct fcp_rsp) +
1264 (2 * sizeof(struct ulp_bde64)));
1265 data_bde->addrHigh = putPaddrHigh(physaddr);
1266 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001267 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -04001268 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1269 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001270 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -04001271 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1272 }
1273 } else {
1274 iocb_cmd->un.fcpi64.bdl.bdeSize =
1275 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001276 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001277 }
James Smart09372822008-01-11 01:52:54 -05001278 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001279
1280 /*
1281 * Due to difference in data length between DIF/non-DIF paths,
1282 * we need to set word 4 of IOCB here
1283 */
James Smarta257bf92009-04-06 18:48:10 -04001284 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001285 return 0;
1286}
1287
James Smartf9bb2da2011-10-10 21:34:11 -04001288#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001289
James Smart9a6b09c2012-03-01 22:37:42 -05001290/* Return if if error injection is detected by Initiator */
1291#define BG_ERR_INIT 0x1
1292/* Return if if error injection is detected by Target */
1293#define BG_ERR_TGT 0x2
1294/* Return if if swapping CSUM<-->CRC is required for error injection */
1295#define BG_ERR_SWAP 0x10
1296/* Return if disabling Guard/Ref/App checking is required for error injection */
1297#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -05001298
1299/**
1300 * lpfc_bg_err_inject - Determine if we should inject an error
1301 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -04001302 * @sc: The SCSI command to examine
1303 * @reftag: (out) BlockGuard reference tag for transmitted data
1304 * @apptag: (out) BlockGuard application tag for transmitted data
1305 * @new_guard (in) Value to replace CRC with if needed
1306 *
James Smart9a6b09c2012-03-01 22:37:42 -05001307 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -05001308 **/
James Smartf9bb2da2011-10-10 21:34:11 -04001309static int
1310lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1311 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1312{
1313 struct scatterlist *sgpe; /* s/g prot entry */
1314 struct scatterlist *sgde; /* s/g data entry */
James Smart9a6b09c2012-03-01 22:37:42 -05001315 struct lpfc_scsi_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -05001316 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -05001317 struct lpfc_nodelist *ndlp;
1318 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -04001319 uint32_t op = scsi_get_prot_op(sc);
1320 uint32_t blksize;
1321 uint32_t numblks;
1322 sector_t lba;
1323 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001324 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001325
1326 if (op == SCSI_PROT_NORMAL)
1327 return 0;
1328
James Smartacd68592012-01-18 16:25:09 -05001329 sgpe = scsi_prot_sglist(sc);
1330 sgde = scsi_sglist(sc);
James Smartf9bb2da2011-10-10 21:34:11 -04001331 lba = scsi_get_lba(sc);
James Smart4ac9b222012-03-01 22:38:29 -05001332
1333 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001334 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1335 blksize = lpfc_cmd_blksize(sc);
1336 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1337
1338 /* Make sure we have the right LBA if one is specified */
1339 if ((phba->lpfc_injerr_lba < lba) ||
1340 (phba->lpfc_injerr_lba >= (lba + numblks)))
1341 return 0;
James Smartacd68592012-01-18 16:25:09 -05001342 if (sgpe) {
1343 blockoff = phba->lpfc_injerr_lba - lba;
1344 numblks = sg_dma_len(sgpe) /
1345 sizeof(struct scsi_dif_tuple);
1346 if (numblks < blockoff)
1347 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001348 }
James Smartf9bb2da2011-10-10 21:34:11 -04001349 }
1350
James Smart4ac9b222012-03-01 22:38:29 -05001351 /* Next check if we need to match the remote NPortID or WWPN */
James Smart1ba981f2014-02-20 09:56:45 -05001352 rdata = lpfc_rport_data_from_scsi_device(sc->device);
James Smart4ac9b222012-03-01 22:38:29 -05001353 if (rdata && rdata->pnode) {
1354 ndlp = rdata->pnode;
1355
1356 /* Make sure we have the right NPortID if one is specified */
1357 if (phba->lpfc_injerr_nportid &&
1358 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1359 return 0;
1360
1361 /*
1362 * Make sure we have the right WWPN if one is specified.
1363 * wwn[0] should be a non-zero NAA in a good WWPN.
1364 */
1365 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1366 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1367 sizeof(struct lpfc_name)) != 0))
1368 return 0;
1369 }
1370
1371 /* Setup a ptr to the protection data if the SCSI host provides it */
1372 if (sgpe) {
1373 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1374 src += blockoff;
1375 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1376 }
1377
James Smartf9bb2da2011-10-10 21:34:11 -04001378 /* Should we change the Reference Tag */
1379 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001380 if (phba->lpfc_injerr_wref_cnt) {
1381 switch (op) {
1382 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001383 if (src) {
1384 /*
1385 * For WRITE_PASS, force the error
1386 * to be sent on the wire. It should
1387 * be detected by the Target.
1388 * If blockoff != 0 error will be
1389 * inserted in middle of the IO.
1390 */
James Smartf9bb2da2011-10-10 21:34:11 -04001391
James Smartacd68592012-01-18 16:25:09 -05001392 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1393 "9076 BLKGRD: Injecting reftag error: "
1394 "write lba x%lx + x%x oldrefTag x%x\n",
1395 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001396 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001397
1398 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001399 * Save the old ref_tag so we can
1400 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001401 */
James Smart9a6b09c2012-03-01 22:37:42 -05001402 if (lpfc_cmd) {
1403 lpfc_cmd->prot_data_type =
1404 LPFC_INJERR_REFTAG;
1405 lpfc_cmd->prot_data_segment =
1406 src;
1407 lpfc_cmd->prot_data =
1408 src->ref_tag;
1409 }
1410 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001411 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001412 if (phba->lpfc_injerr_wref_cnt == 0) {
1413 phba->lpfc_injerr_nportid = 0;
1414 phba->lpfc_injerr_lba =
1415 LPFC_INJERR_LBA_OFF;
1416 memset(&phba->lpfc_injerr_wwpn,
1417 0, sizeof(struct lpfc_name));
1418 }
James Smart9a6b09c2012-03-01 22:37:42 -05001419 rc = BG_ERR_TGT | BG_ERR_CHECK;
1420
James Smartacd68592012-01-18 16:25:09 -05001421 break;
1422 }
1423 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001424 case SCSI_PROT_WRITE_INSERT:
1425 /*
1426 * For WRITE_INSERT, force the error
1427 * to be sent on the wire. It should be
1428 * detected by the Target.
1429 */
1430 /* DEADBEEF will be the reftag on the wire */
1431 *reftag = 0xDEADBEEF;
1432 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001433 if (phba->lpfc_injerr_wref_cnt == 0) {
1434 phba->lpfc_injerr_nportid = 0;
1435 phba->lpfc_injerr_lba =
1436 LPFC_INJERR_LBA_OFF;
1437 memset(&phba->lpfc_injerr_wwpn,
1438 0, sizeof(struct lpfc_name));
1439 }
James Smart9a6b09c2012-03-01 22:37:42 -05001440 rc = BG_ERR_TGT | BG_ERR_CHECK;
1441
1442 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1443 "9078 BLKGRD: Injecting reftag error: "
1444 "write lba x%lx\n", (unsigned long)lba);
1445 break;
James Smartacd68592012-01-18 16:25:09 -05001446 case SCSI_PROT_WRITE_STRIP:
1447 /*
1448 * For WRITE_STRIP and WRITE_PASS,
1449 * force the error on data
1450 * being copied from SLI-Host to SLI-Port.
1451 */
1452 *reftag = 0xDEADBEEF;
1453 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001454 if (phba->lpfc_injerr_wref_cnt == 0) {
1455 phba->lpfc_injerr_nportid = 0;
1456 phba->lpfc_injerr_lba =
1457 LPFC_INJERR_LBA_OFF;
1458 memset(&phba->lpfc_injerr_wwpn,
1459 0, sizeof(struct lpfc_name));
1460 }
James Smartacd68592012-01-18 16:25:09 -05001461 rc = BG_ERR_INIT;
1462
1463 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1464 "9077 BLKGRD: Injecting reftag error: "
1465 "write lba x%lx\n", (unsigned long)lba);
1466 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001467 }
James Smartacd68592012-01-18 16:25:09 -05001468 }
1469 if (phba->lpfc_injerr_rref_cnt) {
1470 switch (op) {
1471 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001472 case SCSI_PROT_READ_STRIP:
1473 case SCSI_PROT_READ_PASS:
1474 /*
1475 * For READ_STRIP and READ_PASS, force the
1476 * error on data being read off the wire. It
1477 * should force an IO error to the driver.
1478 */
James Smartf9bb2da2011-10-10 21:34:11 -04001479 *reftag = 0xDEADBEEF;
1480 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001481 if (phba->lpfc_injerr_rref_cnt == 0) {
1482 phba->lpfc_injerr_nportid = 0;
1483 phba->lpfc_injerr_lba =
1484 LPFC_INJERR_LBA_OFF;
1485 memset(&phba->lpfc_injerr_wwpn,
1486 0, sizeof(struct lpfc_name));
1487 }
James Smartacd68592012-01-18 16:25:09 -05001488 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001489
1490 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001491 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001492 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001493 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001494 }
1495 }
1496 }
1497
1498 /* Should we change the Application Tag */
1499 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001500 if (phba->lpfc_injerr_wapp_cnt) {
1501 switch (op) {
1502 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001503 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001504 /*
1505 * For WRITE_PASS, force the error
1506 * to be sent on the wire. It should
1507 * be detected by the Target.
1508 * If blockoff != 0 error will be
1509 * inserted in middle of the IO.
1510 */
1511
James Smartacd68592012-01-18 16:25:09 -05001512 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1513 "9080 BLKGRD: Injecting apptag error: "
1514 "write lba x%lx + x%x oldappTag x%x\n",
1515 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001516 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001517
1518 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001519 * Save the old app_tag so we can
1520 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001521 */
James Smart9a6b09c2012-03-01 22:37:42 -05001522 if (lpfc_cmd) {
1523 lpfc_cmd->prot_data_type =
1524 LPFC_INJERR_APPTAG;
1525 lpfc_cmd->prot_data_segment =
1526 src;
1527 lpfc_cmd->prot_data =
1528 src->app_tag;
1529 }
1530 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001531 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001532 if (phba->lpfc_injerr_wapp_cnt == 0) {
1533 phba->lpfc_injerr_nportid = 0;
1534 phba->lpfc_injerr_lba =
1535 LPFC_INJERR_LBA_OFF;
1536 memset(&phba->lpfc_injerr_wwpn,
1537 0, sizeof(struct lpfc_name));
1538 }
James Smart9a6b09c2012-03-01 22:37:42 -05001539 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001540 break;
1541 }
1542 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001543 case SCSI_PROT_WRITE_INSERT:
1544 /*
1545 * For WRITE_INSERT, force the
1546 * error to be sent on the wire. It should be
1547 * detected by the Target.
1548 */
1549 /* DEAD will be the apptag on the wire */
1550 *apptag = 0xDEAD;
1551 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001552 if (phba->lpfc_injerr_wapp_cnt == 0) {
1553 phba->lpfc_injerr_nportid = 0;
1554 phba->lpfc_injerr_lba =
1555 LPFC_INJERR_LBA_OFF;
1556 memset(&phba->lpfc_injerr_wwpn,
1557 0, sizeof(struct lpfc_name));
1558 }
James Smart9a6b09c2012-03-01 22:37:42 -05001559 rc = BG_ERR_TGT | BG_ERR_CHECK;
1560
1561 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1562 "0813 BLKGRD: Injecting apptag error: "
1563 "write lba x%lx\n", (unsigned long)lba);
1564 break;
James Smartacd68592012-01-18 16:25:09 -05001565 case SCSI_PROT_WRITE_STRIP:
1566 /*
1567 * For WRITE_STRIP and WRITE_PASS,
1568 * force the error on data
1569 * being copied from SLI-Host to SLI-Port.
1570 */
1571 *apptag = 0xDEAD;
1572 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001573 if (phba->lpfc_injerr_wapp_cnt == 0) {
1574 phba->lpfc_injerr_nportid = 0;
1575 phba->lpfc_injerr_lba =
1576 LPFC_INJERR_LBA_OFF;
1577 memset(&phba->lpfc_injerr_wwpn,
1578 0, sizeof(struct lpfc_name));
1579 }
James Smartacd68592012-01-18 16:25:09 -05001580 rc = BG_ERR_INIT;
1581
1582 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1583 "0812 BLKGRD: Injecting apptag error: "
1584 "write lba x%lx\n", (unsigned long)lba);
1585 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001586 }
James Smartacd68592012-01-18 16:25:09 -05001587 }
1588 if (phba->lpfc_injerr_rapp_cnt) {
1589 switch (op) {
1590 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001591 case SCSI_PROT_READ_STRIP:
1592 case SCSI_PROT_READ_PASS:
1593 /*
1594 * For READ_STRIP and READ_PASS, force the
1595 * error on data being read off the wire. It
1596 * should force an IO error to the driver.
1597 */
James Smartf9bb2da2011-10-10 21:34:11 -04001598 *apptag = 0xDEAD;
1599 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001600 if (phba->lpfc_injerr_rapp_cnt == 0) {
1601 phba->lpfc_injerr_nportid = 0;
1602 phba->lpfc_injerr_lba =
1603 LPFC_INJERR_LBA_OFF;
1604 memset(&phba->lpfc_injerr_wwpn,
1605 0, sizeof(struct lpfc_name));
1606 }
James Smartacd68592012-01-18 16:25:09 -05001607 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001608
1609 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001610 "0814 BLKGRD: Injecting apptag error: "
1611 "read lba x%lx\n", (unsigned long)lba);
1612 break;
1613 }
1614 }
1615 }
1616
1617
1618 /* Should we change the Guard Tag */
1619 if (new_guard) {
1620 if (phba->lpfc_injerr_wgrd_cnt) {
1621 switch (op) {
1622 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001623 rc = BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001624 /* Drop thru */
James Smartacd68592012-01-18 16:25:09 -05001625
James Smartacd68592012-01-18 16:25:09 -05001626 case SCSI_PROT_WRITE_INSERT:
1627 /*
1628 * For WRITE_INSERT, force the
1629 * error to be sent on the wire. It should be
1630 * detected by the Target.
1631 */
1632 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001633 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1634 phba->lpfc_injerr_nportid = 0;
1635 phba->lpfc_injerr_lba =
1636 LPFC_INJERR_LBA_OFF;
1637 memset(&phba->lpfc_injerr_wwpn,
1638 0, sizeof(struct lpfc_name));
1639 }
James Smartacd68592012-01-18 16:25:09 -05001640
James Smart9a6b09c2012-03-01 22:37:42 -05001641 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001642 /* Signals the caller to swap CRC->CSUM */
1643
1644 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1645 "0817 BLKGRD: Injecting guard error: "
1646 "write lba x%lx\n", (unsigned long)lba);
1647 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001648 case SCSI_PROT_WRITE_STRIP:
1649 /*
1650 * For WRITE_STRIP and WRITE_PASS,
1651 * force the error on data
1652 * being copied from SLI-Host to SLI-Port.
1653 */
1654 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001655 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1656 phba->lpfc_injerr_nportid = 0;
1657 phba->lpfc_injerr_lba =
1658 LPFC_INJERR_LBA_OFF;
1659 memset(&phba->lpfc_injerr_wwpn,
1660 0, sizeof(struct lpfc_name));
1661 }
James Smart9a6b09c2012-03-01 22:37:42 -05001662
1663 rc = BG_ERR_INIT | BG_ERR_SWAP;
1664 /* Signals the caller to swap CRC->CSUM */
1665
1666 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1667 "0816 BLKGRD: Injecting guard error: "
1668 "write lba x%lx\n", (unsigned long)lba);
1669 break;
James Smartacd68592012-01-18 16:25:09 -05001670 }
1671 }
1672 if (phba->lpfc_injerr_rgrd_cnt) {
1673 switch (op) {
1674 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001675 case SCSI_PROT_READ_STRIP:
1676 case SCSI_PROT_READ_PASS:
1677 /*
1678 * For READ_STRIP and READ_PASS, force the
1679 * error on data being read off the wire. It
1680 * should force an IO error to the driver.
1681 */
James Smartacd68592012-01-18 16:25:09 -05001682 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001683 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1684 phba->lpfc_injerr_nportid = 0;
1685 phba->lpfc_injerr_lba =
1686 LPFC_INJERR_LBA_OFF;
1687 memset(&phba->lpfc_injerr_wwpn,
1688 0, sizeof(struct lpfc_name));
1689 }
James Smartacd68592012-01-18 16:25:09 -05001690
James Smart9a6b09c2012-03-01 22:37:42 -05001691 rc = BG_ERR_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001692 /* Signals the caller to swap CRC->CSUM */
1693
1694 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1695 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001696 "read lba x%lx\n", (unsigned long)lba);
1697 }
1698 }
1699 }
1700
James Smartf9bb2da2011-10-10 21:34:11 -04001701 return rc;
1702}
1703#endif
1704
James Smartacd68592012-01-18 16:25:09 -05001705/**
1706 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1707 * the specified SCSI command.
1708 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001709 * @sc: The SCSI command to examine
1710 * @txopt: (out) BlockGuard operation for transmitted data
1711 * @rxopt: (out) BlockGuard operation for received data
1712 *
1713 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1714 *
James Smartacd68592012-01-18 16:25:09 -05001715 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001716static int
James Smart6c8eea52010-04-06 14:49:53 -04001717lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1718 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001719{
James Smart6c8eea52010-04-06 14:49:53 -04001720 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001721
James Smart9c6aa9d2013-05-31 17:03:39 -04001722 if (lpfc_cmd_guard_csum(sc)) {
James Smarte2a0a9d2008-12-04 22:40:02 -05001723 switch (scsi_get_prot_op(sc)) {
1724 case SCSI_PROT_READ_INSERT:
1725 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001726 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001727 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001728 break;
1729
1730 case SCSI_PROT_READ_STRIP:
1731 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001732 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001733 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001734 break;
1735
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001736 case SCSI_PROT_READ_PASS:
1737 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001738 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001739 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001740 break;
1741
James Smarte2a0a9d2008-12-04 22:40:02 -05001742 case SCSI_PROT_NORMAL:
1743 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001744 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001745 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1746 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001747 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001748 break;
1749
1750 }
James Smart7c56b9f2011-07-22 18:36:25 -04001751 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001752 switch (scsi_get_prot_op(sc)) {
1753 case SCSI_PROT_READ_STRIP:
1754 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001755 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001756 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001757 break;
1758
1759 case SCSI_PROT_READ_PASS:
1760 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001761 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001762 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001763 break;
1764
James Smarte2a0a9d2008-12-04 22:40:02 -05001765 case SCSI_PROT_READ_INSERT:
1766 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001767 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001768 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001769 break;
1770
James Smarte2a0a9d2008-12-04 22:40:02 -05001771 case SCSI_PROT_NORMAL:
1772 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001773 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001774 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1775 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001776 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001777 break;
1778 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001779 }
1780
James Smart6c8eea52010-04-06 14:49:53 -04001781 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001782}
1783
James Smartacd68592012-01-18 16:25:09 -05001784#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1785/**
1786 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1787 * the specified SCSI command in order to force a guard tag error.
1788 * @phba: The Hba for which this call is being executed.
1789 * @sc: The SCSI command to examine
1790 * @txopt: (out) BlockGuard operation for transmitted data
1791 * @rxopt: (out) BlockGuard operation for received data
1792 *
1793 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1794 *
1795 **/
1796static int
1797lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1798 uint8_t *txop, uint8_t *rxop)
1799{
James Smartacd68592012-01-18 16:25:09 -05001800 uint8_t ret = 0;
1801
James Smart9c6aa9d2013-05-31 17:03:39 -04001802 if (lpfc_cmd_guard_csum(sc)) {
James Smartacd68592012-01-18 16:25:09 -05001803 switch (scsi_get_prot_op(sc)) {
1804 case SCSI_PROT_READ_INSERT:
1805 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001806 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001807 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001808 break;
1809
1810 case SCSI_PROT_READ_STRIP:
1811 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001812 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001813 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001814 break;
1815
1816 case SCSI_PROT_READ_PASS:
1817 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001818 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001819 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001820 break;
1821
1822 case SCSI_PROT_NORMAL:
1823 default:
1824 break;
1825
1826 }
1827 } else {
1828 switch (scsi_get_prot_op(sc)) {
1829 case SCSI_PROT_READ_STRIP:
1830 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001831 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001832 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001833 break;
1834
1835 case SCSI_PROT_READ_PASS:
1836 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001837 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05001838 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001839 break;
1840
1841 case SCSI_PROT_READ_INSERT:
1842 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001843 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001844 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001845 break;
1846
1847 case SCSI_PROT_NORMAL:
1848 default:
1849 break;
1850 }
1851 }
1852
1853 return ret;
1854}
1855#endif
1856
1857/**
1858 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1859 * @phba: The Hba for which this call is being executed.
1860 * @sc: pointer to scsi command we're working on
1861 * @bpl: pointer to buffer list for protection groups
1862 * @datacnt: number of segments of data that have been dma mapped
1863 *
1864 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05001865 * type LPFC_PG_TYPE_NO_DIF
1866 *
1867 * This is usually used when the HBA is instructed to generate
1868 * DIFs and insert them into data stream (or strip DIF from
1869 * incoming data stream)
1870 *
1871 * The buffer list consists of just one protection group described
1872 * below:
1873 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001874 * start of prot group --> | PDE_5 |
1875 * +-------------------------+
1876 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001877 * +-------------------------+
1878 * | Data BDE |
1879 * +-------------------------+
1880 * |more Data BDE's ... (opt)|
1881 * +-------------------------+
1882 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001883 *
1884 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05001885 *
1886 * Returns the number of BDEs added to the BPL.
1887 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001888static int
1889lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1890 struct ulp_bde64 *bpl, int datasegcnt)
1891{
1892 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001893 struct lpfc_pde5 *pde5 = NULL;
1894 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001895 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001896 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001897 int datadir = sc->sc_data_direction;
James Smart0829a192012-05-09 21:18:12 -04001898#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001899 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001900#endif
James Smartacd68592012-01-18 16:25:09 -05001901 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001902 uint32_t reftag;
James Smart7c56b9f2011-07-22 18:36:25 -04001903 unsigned blksize;
James Smart6c8eea52010-04-06 14:49:53 -04001904 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001905
James Smart6c8eea52010-04-06 14:49:53 -04001906 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1907 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001908 goto out;
1909
James Smart6c8eea52010-04-06 14:49:53 -04001910 /* extract some info from the scsi command for pde*/
James Smarte2a0a9d2008-12-04 22:40:02 -05001911 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05001912 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05001913
James Smartf9bb2da2011-10-10 21:34:11 -04001914#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001915 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001916 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001917 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001918 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001919 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001920 checking = 0;
1921 }
James Smartf9bb2da2011-10-10 21:34:11 -04001922#endif
1923
James Smart6c8eea52010-04-06 14:49:53 -04001924 /* setup PDE5 with what we have */
1925 pde5 = (struct lpfc_pde5 *) bpl;
1926 memset(pde5, 0, sizeof(struct lpfc_pde5));
1927 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001928
James Smartbc739052010-08-04 16:11:18 -04001929 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001930 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001931 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001932
James Smart6c8eea52010-04-06 14:49:53 -04001933 /* advance bpl and increment bde count */
1934 num_bde++;
1935 bpl++;
1936 pde6 = (struct lpfc_pde6 *) bpl;
1937
1938 /* setup PDE6 with the rest of the info */
1939 memset(pde6, 0, sizeof(struct lpfc_pde6));
1940 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1941 bf_set(pde6_optx, pde6, txop);
1942 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04001943
1944 /*
1945 * We only need to check the data on READs, for WRITEs
1946 * protection data is automatically generated, not checked.
1947 */
James Smart6c8eea52010-04-06 14:49:53 -04001948 if (datadir == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04001949 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04001950 bf_set(pde6_ce, pde6, checking);
1951 else
1952 bf_set(pde6_ce, pde6, 0);
1953
James Smart9c6aa9d2013-05-31 17:03:39 -04001954 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04001955 bf_set(pde6_re, pde6, checking);
1956 else
1957 bf_set(pde6_re, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001958 }
1959 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001960 bf_set(pde6_ae, pde6, 0);
1961 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001962
James Smartbc739052010-08-04 16:11:18 -04001963 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001964 pde6->word0 = cpu_to_le32(pde6->word0);
1965 pde6->word1 = cpu_to_le32(pde6->word1);
1966 pde6->word2 = cpu_to_le32(pde6->word2);
1967
James Smart6c8eea52010-04-06 14:49:53 -04001968 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001969 num_bde++;
1970 bpl++;
1971
1972 /* assumption: caller has already run dma_map_sg on command data */
1973 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1974 physaddr = sg_dma_address(sgde);
1975 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1976 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1977 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1978 if (datadir == DMA_TO_DEVICE)
1979 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1980 else
1981 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1982 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1983 bpl++;
1984 num_bde++;
1985 }
1986
1987out:
1988 return num_bde;
1989}
1990
James Smartacd68592012-01-18 16:25:09 -05001991/**
1992 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
1993 * @phba: The Hba for which this call is being executed.
1994 * @sc: pointer to scsi command we're working on
1995 * @bpl: pointer to buffer list for protection groups
1996 * @datacnt: number of segments of data that have been dma mapped
1997 * @protcnt: number of segment of protection data that have been dma mapped
1998 *
1999 * This function sets up BPL buffer list for protection groups of
2000 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05002001 *
2002 * This is usually used when DIFs are in their own buffers,
2003 * separate from the data. The HBA can then by instructed
2004 * to place the DIFs in the outgoing stream. For read operations,
2005 * The HBA could extract the DIFs and place it in DIF buffers.
2006 *
2007 * The buffer list for this type consists of one or more of the
2008 * protection groups described below:
2009 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002010 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002011 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002012 * | PDE_6 |
2013 * +-------------------------+
2014 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05002015 * +-------------------------+
2016 * | Data BDE |
2017 * +-------------------------+
2018 * |more Data BDE's ... (opt)|
2019 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002020 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002021 * +-------------------------+
2022 * | ... |
2023 * +-------------------------+
2024 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002025 * Note: It is assumed that both data and protection s/g buffers have been
2026 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05002027 *
2028 * Returns the number of BDEs added to the BPL.
2029 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002030static int
2031lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2032 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2033{
2034 struct scatterlist *sgde = NULL; /* s/g data entry */
2035 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04002036 struct lpfc_pde5 *pde5 = NULL;
2037 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05002038 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002039 dma_addr_t dataphysaddr, protphysaddr;
2040 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05002041 unsigned int split_offset;
2042 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05002043 unsigned int protgrp_blks, protgrp_bytes;
2044 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04002045 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002046 int datadir = sc->sc_data_direction;
2047 unsigned char pgdone = 0, alldone = 0;
2048 unsigned blksize;
James Smart0829a192012-05-09 21:18:12 -04002049#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002050 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002051#endif
James Smartacd68592012-01-18 16:25:09 -05002052 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002053 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04002054 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002055 int num_bde = 0;
2056
2057 sgpe = scsi_prot_sglist(sc);
2058 sgde = scsi_sglist(sc);
2059
2060 if (!sgpe || !sgde) {
2061 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2062 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2063 sgpe, sgde);
2064 return 0;
2065 }
2066
James Smart6c8eea52010-04-06 14:49:53 -04002067 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2068 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002069 goto out;
2070
James Smart6c8eea52010-04-06 14:49:53 -04002071 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05002072 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002073 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002074
James Smartf9bb2da2011-10-10 21:34:11 -04002075#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002076 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002077 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002078 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002079 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002080 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002081 checking = 0;
2082 }
James Smartf9bb2da2011-10-10 21:34:11 -04002083#endif
2084
James Smarte2a0a9d2008-12-04 22:40:02 -05002085 split_offset = 0;
2086 do {
James Smart96f70772013-04-17 20:16:15 -04002087 /* Check to see if we ran out of space */
2088 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
2089 return num_bde + 3;
2090
James Smart6c8eea52010-04-06 14:49:53 -04002091 /* setup PDE5 with what we have */
2092 pde5 = (struct lpfc_pde5 *) bpl;
2093 memset(pde5, 0, sizeof(struct lpfc_pde5));
2094 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002095
James Smartbc739052010-08-04 16:11:18 -04002096 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002097 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002098 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002099
James Smart6c8eea52010-04-06 14:49:53 -04002100 /* advance bpl and increment bde count */
2101 num_bde++;
2102 bpl++;
2103 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002104
James Smart6c8eea52010-04-06 14:49:53 -04002105 /* setup PDE6 with the rest of the info */
2106 memset(pde6, 0, sizeof(struct lpfc_pde6));
2107 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2108 bf_set(pde6_optx, pde6, txop);
2109 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002110
James Smart9c6aa9d2013-05-31 17:03:39 -04002111 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002112 bf_set(pde6_ce, pde6, checking);
2113 else
2114 bf_set(pde6_ce, pde6, 0);
2115
James Smart9c6aa9d2013-05-31 17:03:39 -04002116 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002117 bf_set(pde6_re, pde6, checking);
2118 else
2119 bf_set(pde6_re, pde6, 0);
2120
James Smart6c8eea52010-04-06 14:49:53 -04002121 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002122 bf_set(pde6_ae, pde6, 0);
2123 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002124
James Smartbc739052010-08-04 16:11:18 -04002125 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002126 pde6->word0 = cpu_to_le32(pde6->word0);
2127 pde6->word1 = cpu_to_le32(pde6->word1);
2128 pde6->word2 = cpu_to_le32(pde6->word2);
2129
James Smart6c8eea52010-04-06 14:49:53 -04002130 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002131 num_bde++;
2132 bpl++;
2133
2134 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05002135 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2136 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05002137
James Smarte2a0a9d2008-12-04 22:40:02 -05002138 /* must be integer multiple of the DIF block length */
2139 BUG_ON(protgroup_len % 8);
2140
James Smart7f860592011-03-11 16:05:52 -05002141 pde7 = (struct lpfc_pde7 *) bpl;
2142 memset(pde7, 0, sizeof(struct lpfc_pde7));
2143 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2144
James Smart7c56b9f2011-07-22 18:36:25 -04002145 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2146 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05002147
James Smarte2a0a9d2008-12-04 22:40:02 -05002148 protgrp_blks = protgroup_len / 8;
2149 protgrp_bytes = protgrp_blks * blksize;
2150
James Smart7f860592011-03-11 16:05:52 -05002151 /* check if this pde is crossing the 4K boundary; if so split */
2152 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2153 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2154 protgroup_offset += protgroup_remainder;
2155 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04002156 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05002157 } else {
2158 protgroup_offset = 0;
2159 curr_prot++;
2160 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002161
James Smarte2a0a9d2008-12-04 22:40:02 -05002162 num_bde++;
2163
2164 /* setup BDE's for data blocks associated with DIF data */
2165 pgdone = 0;
2166 subtotal = 0; /* total bytes processed for current prot grp */
2167 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002168 /* Check to see if we ran out of space */
2169 if (num_bde >= phba->cfg_total_seg_cnt)
2170 return num_bde + 1;
2171
James Smarte2a0a9d2008-12-04 22:40:02 -05002172 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04002173 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2174 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002175 __func__);
2176 return 0;
2177 }
2178 bpl++;
2179 dataphysaddr = sg_dma_address(sgde) + split_offset;
2180 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2181 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2182
2183 remainder = sg_dma_len(sgde) - split_offset;
2184
2185 if ((subtotal + remainder) <= protgrp_bytes) {
2186 /* we can use this whole buffer */
2187 bpl->tus.f.bdeSize = remainder;
2188 split_offset = 0;
2189
2190 if ((subtotal + remainder) == protgrp_bytes)
2191 pgdone = 1;
2192 } else {
2193 /* must split this buffer with next prot grp */
2194 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2195 split_offset += bpl->tus.f.bdeSize;
2196 }
2197
2198 subtotal += bpl->tus.f.bdeSize;
2199
2200 if (datadir == DMA_TO_DEVICE)
2201 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2202 else
2203 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2204 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2205
2206 num_bde++;
2207 curr_data++;
2208
2209 if (split_offset)
2210 break;
2211
2212 /* Move to the next s/g segment if possible */
2213 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04002214
James Smarte2a0a9d2008-12-04 22:40:02 -05002215 }
2216
James Smart7f860592011-03-11 16:05:52 -05002217 if (protgroup_offset) {
2218 /* update the reference tag */
2219 reftag += protgrp_blks;
2220 bpl++;
2221 continue;
2222 }
2223
James Smarte2a0a9d2008-12-04 22:40:02 -05002224 /* are we done ? */
2225 if (curr_prot == protcnt) {
2226 alldone = 1;
2227 } else if (curr_prot < protcnt) {
2228 /* advance to next prot buffer */
2229 sgpe = sg_next(sgpe);
2230 bpl++;
2231
2232 /* update the reference tag */
2233 reftag += protgrp_blks;
2234 } else {
2235 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04002236 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2237 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05002238 }
2239
2240 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05002241out:
2242
James Smarte2a0a9d2008-12-04 22:40:02 -05002243 return num_bde;
2244}
James Smart7f860592011-03-11 16:05:52 -05002245
James Smartacd68592012-01-18 16:25:09 -05002246/**
2247 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2248 * @phba: The Hba for which this call is being executed.
2249 * @sc: pointer to scsi command we're working on
2250 * @sgl: pointer to buffer list for protection groups
2251 * @datacnt: number of segments of data that have been dma mapped
2252 *
2253 * This function sets up SGL buffer list for protection groups of
2254 * type LPFC_PG_TYPE_NO_DIF
2255 *
2256 * This is usually used when the HBA is instructed to generate
2257 * DIFs and insert them into data stream (or strip DIF from
2258 * incoming data stream)
2259 *
2260 * The buffer list consists of just one protection group described
2261 * below:
2262 * +-------------------------+
2263 * start of prot group --> | DI_SEED |
2264 * +-------------------------+
2265 * | Data SGE |
2266 * +-------------------------+
2267 * |more Data SGE's ... (opt)|
2268 * +-------------------------+
2269 *
2270 *
2271 * Note: Data s/g buffers have been dma mapped
2272 *
2273 * Returns the number of SGEs added to the SGL.
2274 **/
2275static int
2276lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2277 struct sli4_sge *sgl, int datasegcnt)
2278{
2279 struct scatterlist *sgde = NULL; /* s/g data entry */
2280 struct sli4_sge_diseed *diseed = NULL;
2281 dma_addr_t physaddr;
2282 int i = 0, num_sge = 0, status;
James Smartacd68592012-01-18 16:25:09 -05002283 uint32_t reftag;
2284 unsigned blksize;
2285 uint8_t txop, rxop;
James Smart0829a192012-05-09 21:18:12 -04002286#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002287 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002288#endif
James Smartacd68592012-01-18 16:25:09 -05002289 uint32_t checking = 1;
2290 uint32_t dma_len;
2291 uint32_t dma_offset = 0;
2292
2293 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2294 if (status)
2295 goto out;
2296
2297 /* extract some info from the scsi command for pde*/
2298 blksize = lpfc_cmd_blksize(sc);
2299 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2300
2301#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002302 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002303 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002304 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002305 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002306 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002307 checking = 0;
2308 }
2309#endif
2310
2311 /* setup DISEED with what we have */
2312 diseed = (struct sli4_sge_diseed *) sgl;
2313 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2314 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2315
2316 /* Endianness conversion if necessary */
2317 diseed->ref_tag = cpu_to_le32(reftag);
2318 diseed->ref_tag_tran = diseed->ref_tag;
2319
James Smarta6887e22013-04-17 20:18:07 -04002320 /*
2321 * We only need to check the data on READs, for WRITEs
2322 * protection data is automatically generated, not checked.
2323 */
2324 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002325 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002326 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2327 else
2328 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2329
James Smart9c6aa9d2013-05-31 17:03:39 -04002330 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002331 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2332 else
2333 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2334 }
2335
James Smartacd68592012-01-18 16:25:09 -05002336 /* setup DISEED with the rest of the info */
2337 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2338 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002339
James Smartacd68592012-01-18 16:25:09 -05002340 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2341 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2342
2343 /* Endianness conversion if necessary for DISEED */
2344 diseed->word2 = cpu_to_le32(diseed->word2);
2345 diseed->word3 = cpu_to_le32(diseed->word3);
2346
2347 /* advance bpl and increment sge count */
2348 num_sge++;
2349 sgl++;
2350
2351 /* assumption: caller has already run dma_map_sg on command data */
2352 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2353 physaddr = sg_dma_address(sgde);
2354 dma_len = sg_dma_len(sgde);
2355 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2356 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2357 if ((i + 1) == datasegcnt)
2358 bf_set(lpfc_sli4_sge_last, sgl, 1);
2359 else
2360 bf_set(lpfc_sli4_sge_last, sgl, 0);
2361 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2362 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2363
2364 sgl->sge_len = cpu_to_le32(dma_len);
2365 dma_offset += dma_len;
2366
2367 sgl++;
2368 num_sge++;
2369 }
2370
2371out:
2372 return num_sge;
2373}
2374
2375/**
2376 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2377 * @phba: The Hba for which this call is being executed.
2378 * @sc: pointer to scsi command we're working on
2379 * @sgl: pointer to buffer list for protection groups
2380 * @datacnt: number of segments of data that have been dma mapped
2381 * @protcnt: number of segment of protection data that have been dma mapped
2382 *
2383 * This function sets up SGL buffer list for protection groups of
2384 * type LPFC_PG_TYPE_DIF
2385 *
2386 * This is usually used when DIFs are in their own buffers,
2387 * separate from the data. The HBA can then by instructed
2388 * to place the DIFs in the outgoing stream. For read operations,
2389 * The HBA could extract the DIFs and place it in DIF buffers.
2390 *
2391 * The buffer list for this type consists of one or more of the
2392 * protection groups described below:
2393 * +-------------------------+
2394 * start of first prot group --> | DISEED |
2395 * +-------------------------+
2396 * | DIF (Prot SGE) |
2397 * +-------------------------+
2398 * | Data SGE |
2399 * +-------------------------+
2400 * |more Data SGE's ... (opt)|
2401 * +-------------------------+
2402 * start of new prot group --> | DISEED |
2403 * +-------------------------+
2404 * | ... |
2405 * +-------------------------+
2406 *
2407 * Note: It is assumed that both data and protection s/g buffers have been
2408 * mapped for DMA
2409 *
2410 * Returns the number of SGEs added to the SGL.
2411 **/
2412static int
2413lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2414 struct sli4_sge *sgl, int datacnt, int protcnt)
2415{
2416 struct scatterlist *sgde = NULL; /* s/g data entry */
2417 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2418 struct sli4_sge_diseed *diseed = NULL;
2419 dma_addr_t dataphysaddr, protphysaddr;
2420 unsigned short curr_data = 0, curr_prot = 0;
2421 unsigned int split_offset;
2422 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2423 unsigned int protgrp_blks, protgrp_bytes;
2424 unsigned int remainder, subtotal;
2425 int status;
2426 unsigned char pgdone = 0, alldone = 0;
2427 unsigned blksize;
2428 uint32_t reftag;
2429 uint8_t txop, rxop;
2430 uint32_t dma_len;
James Smart0829a192012-05-09 21:18:12 -04002431#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002432 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002433#endif
James Smartacd68592012-01-18 16:25:09 -05002434 uint32_t checking = 1;
2435 uint32_t dma_offset = 0;
2436 int num_sge = 0;
2437
2438 sgpe = scsi_prot_sglist(sc);
2439 sgde = scsi_sglist(sc);
2440
2441 if (!sgpe || !sgde) {
2442 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2443 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2444 sgpe, sgde);
2445 return 0;
2446 }
2447
2448 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2449 if (status)
2450 goto out;
2451
2452 /* extract some info from the scsi command */
2453 blksize = lpfc_cmd_blksize(sc);
2454 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2455
2456#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002457 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002458 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002459 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002460 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002461 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002462 checking = 0;
2463 }
2464#endif
2465
2466 split_offset = 0;
2467 do {
James Smart96f70772013-04-17 20:16:15 -04002468 /* Check to see if we ran out of space */
2469 if (num_sge >= (phba->cfg_total_seg_cnt - 2))
2470 return num_sge + 3;
2471
James Smartacd68592012-01-18 16:25:09 -05002472 /* setup DISEED with what we have */
2473 diseed = (struct sli4_sge_diseed *) sgl;
2474 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2475 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2476
2477 /* Endianness conversion if necessary */
2478 diseed->ref_tag = cpu_to_le32(reftag);
2479 diseed->ref_tag_tran = diseed->ref_tag;
2480
James Smart9c6aa9d2013-05-31 17:03:39 -04002481 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD)) {
James Smarta6887e22013-04-17 20:18:07 -04002482 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2483
2484 } else {
2485 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2486 /*
2487 * When in this mode, the hardware will replace
2488 * the guard tag from the host with a
2489 * newly generated good CRC for the wire.
2490 * Switch to raw mode here to avoid this
2491 * behavior. What the host sends gets put on the wire.
2492 */
2493 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2494 txop = BG_OP_RAW_MODE;
2495 rxop = BG_OP_RAW_MODE;
2496 }
2497 }
2498
2499
James Smart9c6aa9d2013-05-31 17:03:39 -04002500 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002501 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2502 else
2503 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2504
James Smartacd68592012-01-18 16:25:09 -05002505 /* setup DISEED with the rest of the info */
2506 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2507 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002508
James Smartacd68592012-01-18 16:25:09 -05002509 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2510 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2511
2512 /* Endianness conversion if necessary for DISEED */
2513 diseed->word2 = cpu_to_le32(diseed->word2);
2514 diseed->word3 = cpu_to_le32(diseed->word3);
2515
2516 /* advance sgl and increment bde count */
2517 num_sge++;
2518 sgl++;
2519
2520 /* setup the first BDE that points to protection buffer */
2521 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2522 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2523
2524 /* must be integer multiple of the DIF block length */
2525 BUG_ON(protgroup_len % 8);
2526
2527 /* Now setup DIF SGE */
2528 sgl->word2 = 0;
2529 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2530 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2531 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2532 sgl->word2 = cpu_to_le32(sgl->word2);
2533
2534 protgrp_blks = protgroup_len / 8;
2535 protgrp_bytes = protgrp_blks * blksize;
2536
2537 /* check if DIF SGE is crossing the 4K boundary; if so split */
2538 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2539 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2540 protgroup_offset += protgroup_remainder;
2541 protgrp_blks = protgroup_remainder / 8;
2542 protgrp_bytes = protgrp_blks * blksize;
2543 } else {
2544 protgroup_offset = 0;
2545 curr_prot++;
2546 }
2547
2548 num_sge++;
2549
2550 /* setup SGE's for data blocks associated with DIF data */
2551 pgdone = 0;
2552 subtotal = 0; /* total bytes processed for current prot grp */
2553 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002554 /* Check to see if we ran out of space */
2555 if (num_sge >= phba->cfg_total_seg_cnt)
2556 return num_sge + 1;
2557
James Smartacd68592012-01-18 16:25:09 -05002558 if (!sgde) {
2559 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2560 "9086 BLKGRD:%s Invalid data segment\n",
2561 __func__);
2562 return 0;
2563 }
2564 sgl++;
2565 dataphysaddr = sg_dma_address(sgde) + split_offset;
2566
2567 remainder = sg_dma_len(sgde) - split_offset;
2568
2569 if ((subtotal + remainder) <= protgrp_bytes) {
2570 /* we can use this whole buffer */
2571 dma_len = remainder;
2572 split_offset = 0;
2573
2574 if ((subtotal + remainder) == protgrp_bytes)
2575 pgdone = 1;
2576 } else {
2577 /* must split this buffer with next prot grp */
2578 dma_len = protgrp_bytes - subtotal;
2579 split_offset += dma_len;
2580 }
2581
2582 subtotal += dma_len;
2583
2584 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2585 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2586 bf_set(lpfc_sli4_sge_last, sgl, 0);
2587 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2588 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2589
2590 sgl->sge_len = cpu_to_le32(dma_len);
2591 dma_offset += dma_len;
2592
2593 num_sge++;
2594 curr_data++;
2595
2596 if (split_offset)
2597 break;
2598
2599 /* Move to the next s/g segment if possible */
2600 sgde = sg_next(sgde);
2601 }
2602
2603 if (protgroup_offset) {
2604 /* update the reference tag */
2605 reftag += protgrp_blks;
2606 sgl++;
2607 continue;
2608 }
2609
2610 /* are we done ? */
2611 if (curr_prot == protcnt) {
2612 bf_set(lpfc_sli4_sge_last, sgl, 1);
2613 alldone = 1;
2614 } else if (curr_prot < protcnt) {
2615 /* advance to next prot buffer */
2616 sgpe = sg_next(sgpe);
2617 sgl++;
2618
2619 /* update the reference tag */
2620 reftag += protgrp_blks;
2621 } else {
2622 /* if we're here, we have a bug */
2623 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2624 "9085 BLKGRD: bug in %s\n", __func__);
2625 }
2626
2627 } while (!alldone);
2628
2629out:
2630
2631 return num_sge;
2632}
2633
2634/**
2635 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2636 * @phba: The Hba for which this call is being executed.
2637 * @sc: pointer to scsi command we're working on
2638 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002639 * Given a SCSI command that supports DIF, determine composition of protection
2640 * groups involved in setting up buffer lists
2641 *
James Smartacd68592012-01-18 16:25:09 -05002642 * Returns: Protection group type (with or without DIF)
2643 *
2644 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002645static int
2646lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2647{
2648 int ret = LPFC_PG_TYPE_INVALID;
2649 unsigned char op = scsi_get_prot_op(sc);
2650
2651 switch (op) {
2652 case SCSI_PROT_READ_STRIP:
2653 case SCSI_PROT_WRITE_INSERT:
2654 ret = LPFC_PG_TYPE_NO_DIF;
2655 break;
2656 case SCSI_PROT_READ_INSERT:
2657 case SCSI_PROT_WRITE_STRIP:
2658 case SCSI_PROT_READ_PASS:
2659 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002660 ret = LPFC_PG_TYPE_DIF_BUF;
2661 break;
2662 default:
James Smart9c6aa9d2013-05-31 17:03:39 -04002663 if (phba)
2664 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2665 "9021 Unsupported protection op:%d\n",
2666 op);
James Smarte2a0a9d2008-12-04 22:40:02 -05002667 break;
2668 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002669 return ret;
2670}
2671
James Smartacd68592012-01-18 16:25:09 -05002672/**
James Smarta6887e22013-04-17 20:18:07 -04002673 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2674 * @phba: The Hba for which this call is being executed.
2675 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2676 *
2677 * Adjust the data length to account for how much data
2678 * is actually on the wire.
2679 *
2680 * returns the adjusted data length
2681 **/
2682static int
2683lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
2684 struct lpfc_scsi_buf *lpfc_cmd)
2685{
2686 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2687 int fcpdl;
2688
2689 fcpdl = scsi_bufflen(sc);
2690
2691 /* Check if there is protection data on the wire */
2692 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002693 /* Read check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002694 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2695 return fcpdl;
2696
2697 } else {
James Smart9c6aa9d2013-05-31 17:03:39 -04002698 /* Write check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002699 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2700 return fcpdl;
2701 }
2702
2703 /*
2704 * If we are in DIF Type 1 mode every data block has a 8 byte
James Smart9c6aa9d2013-05-31 17:03:39 -04002705 * DIF (trailer) attached to it. Must ajust FCP data length
2706 * to account for the protection data.
James Smarta6887e22013-04-17 20:18:07 -04002707 */
James Smart9c6aa9d2013-05-31 17:03:39 -04002708 fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
James Smarta6887e22013-04-17 20:18:07 -04002709
2710 return fcpdl;
2711}
2712
2713/**
James Smartacd68592012-01-18 16:25:09 -05002714 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2715 * @phba: The Hba for which this call is being executed.
2716 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2717 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002718 * This is the protection/DIF aware version of
2719 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2720 * two functions eventually, but for now, it's here
James Smartacd68592012-01-18 16:25:09 -05002721 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002722static int
James Smartacd68592012-01-18 16:25:09 -05002723lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smarte2a0a9d2008-12-04 22:40:02 -05002724 struct lpfc_scsi_buf *lpfc_cmd)
2725{
2726 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2727 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2728 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2729 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2730 uint32_t num_bde = 0;
2731 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2732 int prot_group_type = 0;
James Smarta6887e22013-04-17 20:18:07 -04002733 int fcpdl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002734
2735 /*
2736 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2737 * fcp_rsp regions to the first data bde entry
2738 */
2739 bpl += 2;
2740 if (scsi_sg_count(scsi_cmnd)) {
2741 /*
2742 * The driver stores the segment count returned from pci_map_sg
2743 * because this a count of dma-mappings used to map the use_sg
2744 * pages. They are not guaranteed to be the same for those
2745 * architectures that implement an IOMMU.
2746 */
2747 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2748 scsi_sglist(scsi_cmnd),
2749 scsi_sg_count(scsi_cmnd), datadir);
2750 if (unlikely(!datasegcnt))
2751 return 1;
2752
2753 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04002754
2755 /* First check if data segment count from SCSI Layer is good */
2756 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
2757 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002758
2759 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2760
2761 switch (prot_group_type) {
2762 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04002763
2764 /* Here we need to add a PDE5 and PDE6 to the count */
2765 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt)
2766 goto err;
2767
James Smarte2a0a9d2008-12-04 22:40:02 -05002768 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2769 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002770 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002771 if (num_bde < 2)
2772 goto err;
2773 break;
James Smart96f70772013-04-17 20:16:15 -04002774
2775 case LPFC_PG_TYPE_DIF_BUF:
James Smarte2a0a9d2008-12-04 22:40:02 -05002776 /*
2777 * This type indicates that protection buffers are
2778 * passed to the driver, so that needs to be prepared
2779 * for DMA
2780 */
2781 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2782 scsi_prot_sglist(scsi_cmnd),
2783 scsi_prot_sg_count(scsi_cmnd), datadir);
2784 if (unlikely(!protsegcnt)) {
2785 scsi_dma_unmap(scsi_cmnd);
2786 return 1;
2787 }
2788
2789 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04002790
2791 /*
2792 * There is a minimun of 4 BPLs used for every
2793 * protection data segment.
2794 */
2795 if ((lpfc_cmd->prot_seg_cnt * 4) >
2796 (phba->cfg_total_seg_cnt - 2))
2797 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002798
2799 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2800 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002801 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04002802 if ((num_bde < 3) ||
2803 (num_bde > phba->cfg_total_seg_cnt))
James Smarte2a0a9d2008-12-04 22:40:02 -05002804 goto err;
2805 break;
James Smart96f70772013-04-17 20:16:15 -04002806
James Smarte2a0a9d2008-12-04 22:40:02 -05002807 case LPFC_PG_TYPE_INVALID:
2808 default:
James Smart96f70772013-04-17 20:16:15 -04002809 scsi_dma_unmap(scsi_cmnd);
2810 lpfc_cmd->seg_cnt = 0;
2811
James Smarte2a0a9d2008-12-04 22:40:02 -05002812 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2813 "9022 Unexpected protection group %i\n",
2814 prot_group_type);
2815 return 1;
2816 }
2817 }
2818
2819 /*
2820 * Finish initializing those IOCB fields that are dependent on the
2821 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2822 * reinitialized since all iocb memory resources are used many times
2823 * for transmit, receive, and continuation bpl's.
2824 */
2825 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2826 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2827 iocb_cmd->ulpBdeCount = 1;
2828 iocb_cmd->ulpLe = 1;
2829
James Smarta6887e22013-04-17 20:18:07 -04002830 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002831 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2832
2833 /*
2834 * Due to difference in data length between DIF/non-DIF paths,
2835 * we need to set word 4 of IOCB here
2836 */
2837 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2838
2839 return 0;
2840err:
James Smart96f70772013-04-17 20:16:15 -04002841 if (lpfc_cmd->seg_cnt)
2842 scsi_dma_unmap(scsi_cmnd);
2843 if (lpfc_cmd->prot_seg_cnt)
2844 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2845 scsi_prot_sg_count(scsi_cmnd),
2846 scsi_cmnd->sc_data_direction);
2847
James Smarte2a0a9d2008-12-04 22:40:02 -05002848 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04002849 "9023 Cannot setup S/G List for HBA"
2850 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
2851 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
2852 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
James Smarte2a0a9d2008-12-04 22:40:02 -05002853 prot_group_type, num_bde);
James Smart96f70772013-04-17 20:16:15 -04002854
2855 lpfc_cmd->seg_cnt = 0;
2856 lpfc_cmd->prot_seg_cnt = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05002857 return 1;
2858}
2859
2860/*
James Smart737d4242013-04-17 20:14:49 -04002861 * This function calcuates the T10 DIF guard tag
2862 * on the specified data using a CRC algorithmn
2863 * using crc_t10dif.
2864 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002865static uint16_t
James Smart737d4242013-04-17 20:14:49 -04002866lpfc_bg_crc(uint8_t *data, int count)
2867{
2868 uint16_t crc = 0;
2869 uint16_t x;
2870
2871 crc = crc_t10dif(data, count);
2872 x = cpu_to_be16(crc);
2873 return x;
2874}
2875
2876/*
2877 * This function calcuates the T10 DIF guard tag
2878 * on the specified data using a CSUM algorithmn
2879 * using ip_compute_csum.
2880 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002881static uint16_t
James Smart737d4242013-04-17 20:14:49 -04002882lpfc_bg_csum(uint8_t *data, int count)
2883{
2884 uint16_t ret;
2885
2886 ret = ip_compute_csum(data, count);
2887 return ret;
2888}
2889
2890/*
2891 * This function examines the protection data to try to determine
2892 * what type of T10-DIF error occurred.
2893 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002894static void
James Smart737d4242013-04-17 20:14:49 -04002895lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2896{
2897 struct scatterlist *sgpe; /* s/g prot entry */
2898 struct scatterlist *sgde; /* s/g data entry */
2899 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2900 struct scsi_dif_tuple *src = NULL;
2901 uint8_t *data_src = NULL;
2902 uint16_t guard_tag, guard_type;
2903 uint16_t start_app_tag, app_tag;
2904 uint32_t start_ref_tag, ref_tag;
2905 int prot, protsegcnt;
2906 int err_type, len, data_len;
2907 int chk_ref, chk_app, chk_guard;
2908 uint16_t sum;
2909 unsigned blksize;
2910
2911 err_type = BGS_GUARD_ERR_MASK;
2912 sum = 0;
2913 guard_tag = 0;
2914
2915 /* First check to see if there is protection data to examine */
2916 prot = scsi_get_prot_op(cmd);
2917 if ((prot == SCSI_PROT_READ_STRIP) ||
2918 (prot == SCSI_PROT_WRITE_INSERT) ||
2919 (prot == SCSI_PROT_NORMAL))
2920 goto out;
2921
2922 /* Currently the driver just supports ref_tag and guard_tag checking */
2923 chk_ref = 1;
2924 chk_app = 0;
2925 chk_guard = 0;
2926
2927 /* Setup a ptr to the protection data provided by the SCSI host */
2928 sgpe = scsi_prot_sglist(cmd);
2929 protsegcnt = lpfc_cmd->prot_seg_cnt;
2930
2931 if (sgpe && protsegcnt) {
2932
2933 /*
2934 * We will only try to verify guard tag if the segment
2935 * data length is a multiple of the blksize.
2936 */
2937 sgde = scsi_sglist(cmd);
2938 blksize = lpfc_cmd_blksize(cmd);
2939 data_src = (uint8_t *)sg_virt(sgde);
2940 data_len = sgde->length;
2941 if ((data_len & (blksize - 1)) == 0)
2942 chk_guard = 1;
2943 guard_type = scsi_host_get_guard(cmd->device->host);
2944
James Smarte85d8f92013-05-31 17:04:10 -04002945 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
James Smarta6887e22013-04-17 20:18:07 -04002946 start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
James Smart737d4242013-04-17 20:14:49 -04002947 start_app_tag = src->app_tag;
James Smart737d4242013-04-17 20:14:49 -04002948 len = sgpe->length;
2949 while (src && protsegcnt) {
2950 while (len) {
2951
2952 /*
2953 * First check to see if a protection data
2954 * check is valid
2955 */
2956 if ((src->ref_tag == 0xffffffff) ||
2957 (src->app_tag == 0xffff)) {
2958 start_ref_tag++;
2959 goto skipit;
2960 }
2961
James Smart9c6aa9d2013-05-31 17:03:39 -04002962 /* First Guard Tag checking */
2963 if (chk_guard) {
2964 guard_tag = src->guard_tag;
2965 if (lpfc_cmd_guard_csum(cmd))
2966 sum = lpfc_bg_csum(data_src,
2967 blksize);
2968 else
2969 sum = lpfc_bg_crc(data_src,
2970 blksize);
2971 if ((guard_tag != sum)) {
2972 err_type = BGS_GUARD_ERR_MASK;
2973 goto out;
2974 }
James Smart737d4242013-04-17 20:14:49 -04002975 }
2976
2977 /* Reference Tag checking */
2978 ref_tag = be32_to_cpu(src->ref_tag);
2979 if (chk_ref && (ref_tag != start_ref_tag)) {
2980 err_type = BGS_REFTAG_ERR_MASK;
2981 goto out;
2982 }
2983 start_ref_tag++;
2984
James Smart9c6aa9d2013-05-31 17:03:39 -04002985 /* App Tag checking */
2986 app_tag = src->app_tag;
2987 if (chk_app && (app_tag != start_app_tag)) {
2988 err_type = BGS_APPTAG_ERR_MASK;
2989 goto out;
James Smart737d4242013-04-17 20:14:49 -04002990 }
2991skipit:
2992 len -= sizeof(struct scsi_dif_tuple);
2993 if (len < 0)
2994 len = 0;
2995 src++;
2996
2997 data_src += blksize;
2998 data_len -= blksize;
2999
3000 /*
3001 * Are we at the end of the Data segment?
3002 * The data segment is only used for Guard
3003 * tag checking.
3004 */
3005 if (chk_guard && (data_len == 0)) {
3006 chk_guard = 0;
3007 sgde = sg_next(sgde);
3008 if (!sgde)
3009 goto out;
3010
3011 data_src = (uint8_t *)sg_virt(sgde);
3012 data_len = sgde->length;
3013 if ((data_len & (blksize - 1)) == 0)
3014 chk_guard = 1;
3015 }
3016 }
3017
3018 /* Goto the next Protection data segment */
3019 sgpe = sg_next(sgpe);
3020 if (sgpe) {
3021 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
3022 len = sgpe->length;
3023 } else {
3024 src = NULL;
3025 }
3026 protsegcnt--;
3027 }
3028 }
3029out:
3030 if (err_type == BGS_GUARD_ERR_MASK) {
3031 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3032 0x10, 0x1);
3033 cmd->result = DRIVER_SENSE << 24
3034 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3035 phba->bg_guard_err_cnt++;
3036 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3037 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
3038 (unsigned long)scsi_get_lba(cmd),
3039 sum, guard_tag);
3040
3041 } else if (err_type == BGS_REFTAG_ERR_MASK) {
3042 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3043 0x10, 0x3);
3044 cmd->result = DRIVER_SENSE << 24
3045 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3046
3047 phba->bg_reftag_err_cnt++;
3048 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3049 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
3050 (unsigned long)scsi_get_lba(cmd),
3051 ref_tag, start_ref_tag);
3052
3053 } else if (err_type == BGS_APPTAG_ERR_MASK) {
3054 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3055 0x10, 0x2);
3056 cmd->result = DRIVER_SENSE << 24
3057 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3058
3059 phba->bg_apptag_err_cnt++;
3060 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3061 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
3062 (unsigned long)scsi_get_lba(cmd),
3063 app_tag, start_app_tag);
3064 }
3065}
3066
3067
3068/*
James Smarte2a0a9d2008-12-04 22:40:02 -05003069 * This function checks for BlockGuard errors detected by
3070 * the HBA. In case of errors, the ASC/ASCQ fields in the
3071 * sense buffer will be set accordingly, paired with
3072 * ILLEGAL_REQUEST to signal to the kernel that the HBA
3073 * detected corruption.
3074 *
3075 * Returns:
3076 * 0 - No error found
3077 * 1 - BlockGuard error found
3078 * -1 - Internal error (bad profile, ...etc)
3079 */
3080static int
3081lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
3082 struct lpfc_iocbq *pIocbOut)
3083{
3084 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3085 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
3086 int ret = 0;
3087 uint32_t bghm = bgf->bghm;
3088 uint32_t bgstat = bgf->bgstat;
3089 uint64_t failing_sector = 0;
3090
James Smarte2a0a9d2008-12-04 22:40:02 -05003091 spin_lock(&_dump_buf_lock);
3092 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04003093 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
3094 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05003095 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04003096 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003097
3098 /* If we have a prot sgl, save the DIF buffer */
3099 if (lpfc_prot_group_type(phba, cmd) ==
3100 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04003101 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
3102 "Saving DIF for %u blocks to debugfs\n",
3103 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
3104 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003105 }
3106
3107 _dump_buf_done = 1;
3108 }
3109 spin_unlock(&_dump_buf_lock);
3110
3111 if (lpfc_bgs_get_invalid_prof(bgstat)) {
3112 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart737d4242013-04-17 20:14:49 -04003113 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3114 "9072 BLKGRD: Invalid BG Profile in cmd"
3115 " 0x%x lba 0x%llx blk cnt 0x%x "
3116 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3117 (unsigned long long)scsi_get_lba(cmd),
3118 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003119 ret = (-1);
3120 goto out;
3121 }
3122
3123 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
3124 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart737d4242013-04-17 20:14:49 -04003125 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3126 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
3127 " 0x%x lba 0x%llx blk cnt 0x%x "
3128 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3129 (unsigned long long)scsi_get_lba(cmd),
3130 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003131 ret = (-1);
3132 goto out;
3133 }
3134
3135 if (lpfc_bgs_get_guard_err(bgstat)) {
3136 ret = 1;
3137
3138 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3139 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003140 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003141 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3142 phba->bg_guard_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003143 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3144 "9055 BLKGRD: Guard Tag error in cmd"
3145 " 0x%x lba 0x%llx blk cnt 0x%x "
3146 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3147 (unsigned long long)scsi_get_lba(cmd),
3148 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003149 }
3150
3151 if (lpfc_bgs_get_reftag_err(bgstat)) {
3152 ret = 1;
3153
3154 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3155 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003156 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003157 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3158
3159 phba->bg_reftag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003160 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3161 "9056 BLKGRD: Ref Tag error in cmd"
3162 " 0x%x lba 0x%llx blk cnt 0x%x "
3163 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3164 (unsigned long long)scsi_get_lba(cmd),
3165 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003166 }
3167
3168 if (lpfc_bgs_get_apptag_err(bgstat)) {
3169 ret = 1;
3170
3171 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3172 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003173 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003174 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3175
3176 phba->bg_apptag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003177 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3178 "9061 BLKGRD: App Tag error in cmd"
3179 " 0x%x lba 0x%llx blk cnt 0x%x "
3180 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3181 (unsigned long long)scsi_get_lba(cmd),
3182 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003183 }
3184
3185 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
3186 /*
3187 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04003188 * field, and put the failing LBA in it.
3189 * This code assumes there was also a guard/app/ref tag error
3190 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05003191 */
James Smart7c56b9f2011-07-22 18:36:25 -04003192 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
3193 cmd->sense_buffer[8] = 0; /* Information descriptor type */
3194 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
3195 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05003196
3197 /* bghm is a "on the wire" FC frame based count */
3198 switch (scsi_get_prot_op(cmd)) {
3199 case SCSI_PROT_READ_INSERT:
3200 case SCSI_PROT_WRITE_STRIP:
3201 bghm /= cmd->device->sector_size;
3202 break;
3203 case SCSI_PROT_READ_STRIP:
3204 case SCSI_PROT_WRITE_INSERT:
3205 case SCSI_PROT_READ_PASS:
3206 case SCSI_PROT_WRITE_PASS:
3207 bghm /= (cmd->device->sector_size +
3208 sizeof(struct scsi_dif_tuple));
3209 break;
3210 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003211
3212 failing_sector = scsi_get_lba(cmd);
3213 failing_sector += bghm;
3214
James Smart7c56b9f2011-07-22 18:36:25 -04003215 /* Descriptor Information */
3216 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05003217 }
3218
3219 if (!ret) {
3220 /* No error was reported - problem in FW? */
James Smart737d4242013-04-17 20:14:49 -04003221 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3222 "9057 BLKGRD: Unknown error in cmd"
3223 " 0x%x lba 0x%llx blk cnt 0x%x "
3224 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3225 (unsigned long long)scsi_get_lba(cmd),
3226 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003227
James Smart737d4242013-04-17 20:14:49 -04003228 /* Calcuate what type of error it was */
3229 lpfc_calc_bg_err(phba, lpfc_cmd);
3230 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003231out:
3232 return ret;
3233}
3234
James Smartea2151b2008-09-07 11:52:10 -04003235/**
James Smartda0436e2009-05-22 14:51:39 -04003236 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3237 * @phba: The Hba for which this call is being executed.
3238 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3239 *
3240 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3241 * field of @lpfc_cmd for device with SLI-4 interface spec.
3242 *
3243 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003244 * 1 - Error
3245 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04003246 **/
3247static int
3248lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3249{
3250 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3251 struct scatterlist *sgel = NULL;
3252 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3253 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
James Smartfedd3b72011-02-16 12:39:24 -05003254 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04003255 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3256 dma_addr_t physaddr;
3257 uint32_t num_bde = 0;
3258 uint32_t dma_len;
3259 uint32_t dma_offset = 0;
3260 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05003261 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04003262
3263 /*
3264 * There are three possibilities here - use scatter-gather segment, use
3265 * the single mapping, or neither. Start the lpfc command prep by
3266 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3267 * data bde entry.
3268 */
3269 if (scsi_sg_count(scsi_cmnd)) {
3270 /*
3271 * The driver stores the segment count returned from pci_map_sg
3272 * because this a count of dma-mappings used to map the use_sg
3273 * pages. They are not guaranteed to be the same for those
3274 * architectures that implement an IOMMU.
3275 */
3276
3277 nseg = scsi_dma_map(scsi_cmnd);
3278 if (unlikely(!nseg))
3279 return 1;
3280 sgl += 1;
3281 /* clear the last flag in the fcp_rsp map entry */
3282 sgl->word2 = le32_to_cpu(sgl->word2);
3283 bf_set(lpfc_sli4_sge_last, sgl, 0);
3284 sgl->word2 = cpu_to_le32(sgl->word2);
3285 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05003286 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04003287 lpfc_cmd->seg_cnt = nseg;
3288 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04003289 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3290 " %s: Too many sg segments from "
3291 "dma_map_sg. Config %d, seg_cnt %d\n",
3292 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04003293 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04003294 lpfc_cmd->seg_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04003295 scsi_dma_unmap(scsi_cmnd);
3296 return 1;
3297 }
3298
3299 /*
3300 * The driver established a maximum scatter-gather segment count
3301 * during probe that limits the number of sg elements in any
3302 * single scsi command. Just run through the seg_cnt and format
3303 * the sge's.
3304 * When using SLI-3 the driver will try to fit all the BDEs into
3305 * the IOCB. If it can't then the BDEs get added to a BPL as it
3306 * does for SLI-2 mode.
3307 */
3308 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3309 physaddr = sg_dma_address(sgel);
3310 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04003311 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3312 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04003313 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04003314 if ((num_bde + 1) == nseg)
3315 bf_set(lpfc_sli4_sge_last, sgl, 1);
3316 else
3317 bf_set(lpfc_sli4_sge_last, sgl, 0);
3318 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04003319 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04003320 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05003321 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04003322 dma_offset += dma_len;
3323 sgl++;
3324 }
James Smartfedd3b72011-02-16 12:39:24 -05003325 /* setup the performance hint (first data BDE) if enabled */
3326 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
3327 bde = (struct ulp_bde64 *)
3328 &(iocb_cmd->unsli3.sli3Words[5]);
3329 bde->addrLow = first_data_sgl->addr_lo;
3330 bde->addrHigh = first_data_sgl->addr_hi;
3331 bde->tus.f.bdeSize =
3332 le32_to_cpu(first_data_sgl->sge_len);
3333 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3334 bde->tus.w = cpu_to_le32(bde->tus.w);
3335 }
James Smartda0436e2009-05-22 14:51:39 -04003336 } else {
3337 sgl += 1;
3338 /* clear the last flag in the fcp_rsp map entry */
3339 sgl->word2 = le32_to_cpu(sgl->word2);
3340 bf_set(lpfc_sli4_sge_last, sgl, 1);
3341 sgl->word2 = cpu_to_le32(sgl->word2);
3342 }
3343
3344 /*
3345 * Finish initializing those IOCB fields that are dependent on the
3346 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3347 * explicitly reinitialized.
3348 * all iocb memory resources are reused.
3349 */
3350 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3351
3352 /*
3353 * Due to difference in data length between DIF/non-DIF paths,
3354 * we need to set word 4 of IOCB here
3355 */
3356 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
James Smart1ba981f2014-02-20 09:56:45 -05003357
3358 /*
3359 * If the OAS driver feature is enabled and the lun is enabled for
3360 * OAS, set the oas iocb related flags.
3361 */
James Smartf38fa0b2014-04-04 13:52:21 -04003362 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
James Smart1ba981f2014-02-20 09:56:45 -05003363 scsi_cmnd->device->hostdata)->oas_enabled)
James Smart9bd2bff52014-09-03 12:57:30 -04003364 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
James Smartda0436e2009-05-22 14:51:39 -04003365 return 0;
3366}
3367
3368/**
James Smartacd68592012-01-18 16:25:09 -05003369 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3370 * @phba: The Hba for which this call is being executed.
3371 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3372 *
3373 * This is the protection/DIF aware version of
3374 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3375 * two functions eventually, but for now, it's here
3376 **/
3377static int
3378lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3379 struct lpfc_scsi_buf *lpfc_cmd)
3380{
3381 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3382 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3383 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3384 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart96f70772013-04-17 20:16:15 -04003385 uint32_t num_sge = 0;
James Smartacd68592012-01-18 16:25:09 -05003386 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3387 int prot_group_type = 0;
3388 int fcpdl;
3389
3390 /*
3391 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
James Smart96f70772013-04-17 20:16:15 -04003392 * fcp_rsp regions to the first data sge entry
James Smartacd68592012-01-18 16:25:09 -05003393 */
3394 if (scsi_sg_count(scsi_cmnd)) {
3395 /*
3396 * The driver stores the segment count returned from pci_map_sg
3397 * because this a count of dma-mappings used to map the use_sg
3398 * pages. They are not guaranteed to be the same for those
3399 * architectures that implement an IOMMU.
3400 */
3401 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3402 scsi_sglist(scsi_cmnd),
3403 scsi_sg_count(scsi_cmnd), datadir);
3404 if (unlikely(!datasegcnt))
3405 return 1;
3406
3407 sgl += 1;
3408 /* clear the last flag in the fcp_rsp map entry */
3409 sgl->word2 = le32_to_cpu(sgl->word2);
3410 bf_set(lpfc_sli4_sge_last, sgl, 0);
3411 sgl->word2 = cpu_to_le32(sgl->word2);
3412
3413 sgl += 1;
3414 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04003415
3416 /* First check if data segment count from SCSI Layer is good */
3417 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
3418 goto err;
James Smartacd68592012-01-18 16:25:09 -05003419
3420 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3421
3422 switch (prot_group_type) {
3423 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04003424 /* Here we need to add a DISEED to the count */
3425 if ((lpfc_cmd->seg_cnt + 1) > phba->cfg_total_seg_cnt)
3426 goto err;
3427
3428 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003429 datasegcnt);
James Smart96f70772013-04-17 20:16:15 -04003430
James Smartacd68592012-01-18 16:25:09 -05003431 /* we should have 2 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003432 if (num_sge < 2)
James Smartacd68592012-01-18 16:25:09 -05003433 goto err;
3434 break;
James Smart96f70772013-04-17 20:16:15 -04003435
3436 case LPFC_PG_TYPE_DIF_BUF:
James Smartacd68592012-01-18 16:25:09 -05003437 /*
3438 * This type indicates that protection buffers are
3439 * passed to the driver, so that needs to be prepared
3440 * for DMA
3441 */
3442 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3443 scsi_prot_sglist(scsi_cmnd),
3444 scsi_prot_sg_count(scsi_cmnd), datadir);
3445 if (unlikely(!protsegcnt)) {
3446 scsi_dma_unmap(scsi_cmnd);
3447 return 1;
3448 }
3449
3450 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04003451 /*
3452 * There is a minimun of 3 SGEs used for every
3453 * protection data segment.
3454 */
3455 if ((lpfc_cmd->prot_seg_cnt * 3) >
3456 (phba->cfg_total_seg_cnt - 2))
3457 goto err;
James Smartacd68592012-01-18 16:25:09 -05003458
James Smart96f70772013-04-17 20:16:15 -04003459 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003460 datasegcnt, protsegcnt);
James Smart96f70772013-04-17 20:16:15 -04003461
James Smartacd68592012-01-18 16:25:09 -05003462 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003463 if ((num_sge < 3) ||
3464 (num_sge > phba->cfg_total_seg_cnt))
James Smartacd68592012-01-18 16:25:09 -05003465 goto err;
3466 break;
James Smart96f70772013-04-17 20:16:15 -04003467
James Smartacd68592012-01-18 16:25:09 -05003468 case LPFC_PG_TYPE_INVALID:
3469 default:
James Smart96f70772013-04-17 20:16:15 -04003470 scsi_dma_unmap(scsi_cmnd);
3471 lpfc_cmd->seg_cnt = 0;
3472
James Smartacd68592012-01-18 16:25:09 -05003473 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3474 "9083 Unexpected protection group %i\n",
3475 prot_group_type);
3476 return 1;
3477 }
3478 }
3479
James Smart8012cc32012-10-31 14:44:49 -04003480 switch (scsi_get_prot_op(scsi_cmnd)) {
3481 case SCSI_PROT_WRITE_STRIP:
3482 case SCSI_PROT_READ_STRIP:
3483 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3484 break;
3485 case SCSI_PROT_WRITE_INSERT:
3486 case SCSI_PROT_READ_INSERT:
3487 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3488 break;
3489 case SCSI_PROT_WRITE_PASS:
3490 case SCSI_PROT_READ_PASS:
3491 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3492 break;
3493 }
3494
James Smartacd68592012-01-18 16:25:09 -05003495 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smartacd68592012-01-18 16:25:09 -05003496 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3497
3498 /*
3499 * Due to difference in data length between DIF/non-DIF paths,
3500 * we need to set word 4 of IOCB here
3501 */
3502 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
James Smartacd68592012-01-18 16:25:09 -05003503
James Smart9bd2bff52014-09-03 12:57:30 -04003504 /*
3505 * If the OAS driver feature is enabled and the lun is enabled for
3506 * OAS, set the oas iocb related flags.
3507 */
3508 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
3509 scsi_cmnd->device->hostdata)->oas_enabled)
3510 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
3511
James Smartacd68592012-01-18 16:25:09 -05003512 return 0;
3513err:
James Smart96f70772013-04-17 20:16:15 -04003514 if (lpfc_cmd->seg_cnt)
3515 scsi_dma_unmap(scsi_cmnd);
3516 if (lpfc_cmd->prot_seg_cnt)
3517 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3518 scsi_prot_sg_count(scsi_cmnd),
3519 scsi_cmnd->sc_data_direction);
3520
James Smartacd68592012-01-18 16:25:09 -05003521 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04003522 "9084 Cannot setup S/G List for HBA"
3523 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3524 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3525 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3526 prot_group_type, num_sge);
3527
3528 lpfc_cmd->seg_cnt = 0;
3529 lpfc_cmd->prot_seg_cnt = 0;
James Smartacd68592012-01-18 16:25:09 -05003530 return 1;
3531}
3532
3533/**
James Smart3772a992009-05-22 14:50:54 -04003534 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3535 * @phba: The Hba for which this call is being executed.
3536 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3537 *
3538 * This routine wraps the actual DMA mapping function pointer from the
3539 * lpfc_hba struct.
3540 *
3541 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003542 * 1 - Error
3543 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003544 **/
3545static inline int
3546lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3547{
3548 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3549}
3550
3551/**
James Smartacd68592012-01-18 16:25:09 -05003552 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3553 * using BlockGuard.
3554 * @phba: The Hba for which this call is being executed.
3555 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3556 *
3557 * This routine wraps the actual DMA mapping function pointer from the
3558 * lpfc_hba struct.
3559 *
3560 * Return codes:
3561 * 1 - Error
3562 * 0 - Success
3563 **/
3564static inline int
3565lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3566{
3567 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3568}
3569
3570/**
James Smart3621a712009-04-06 18:47:14 -04003571 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003572 * @phba: Pointer to hba context object.
3573 * @vport: Pointer to vport object.
3574 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3575 * @rsp_iocb: Pointer to response iocb object which reported error.
3576 *
3577 * This function posts an event when there is a SCSI command reporting
3578 * error from the scsi device.
3579 **/
3580static void
3581lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3582 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3583 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3584 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3585 uint32_t resp_info = fcprsp->rspStatus2;
3586 uint32_t scsi_status = fcprsp->rspStatus3;
3587 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3588 struct lpfc_fast_path_event *fast_path_evt = NULL;
3589 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3590 unsigned long flags;
3591
James Smart5989b8d2010-10-22 11:06:56 -04003592 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3593 return;
3594
James Smartea2151b2008-09-07 11:52:10 -04003595 /* If there is queuefull or busy condition send a scsi event */
3596 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3597 (cmnd->result == SAM_STAT_BUSY)) {
3598 fast_path_evt = lpfc_alloc_fast_evt(phba);
3599 if (!fast_path_evt)
3600 return;
3601 fast_path_evt->un.scsi_evt.event_type =
3602 FC_REG_SCSI_EVENT;
3603 fast_path_evt->un.scsi_evt.subcategory =
3604 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3605 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3606 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3607 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3608 &pnode->nlp_portname, sizeof(struct lpfc_name));
3609 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3610 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3611 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3612 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3613 fast_path_evt = lpfc_alloc_fast_evt(phba);
3614 if (!fast_path_evt)
3615 return;
3616 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3617 FC_REG_SCSI_EVENT;
3618 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3619 LPFC_EVENT_CHECK_COND;
3620 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3621 cmnd->device->lun;
3622 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3623 &pnode->nlp_portname, sizeof(struct lpfc_name));
3624 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3625 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3626 fast_path_evt->un.check_cond_evt.sense_key =
3627 cmnd->sense_buffer[2] & 0xf;
3628 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3629 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3630 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3631 fcpi_parm &&
3632 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3633 ((scsi_status == SAM_STAT_GOOD) &&
3634 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3635 /*
3636 * If status is good or resid does not match with fcp_param and
3637 * there is valid fcpi_parm, then there is a read_check error
3638 */
3639 fast_path_evt = lpfc_alloc_fast_evt(phba);
3640 if (!fast_path_evt)
3641 return;
3642 fast_path_evt->un.read_check_error.header.event_type =
3643 FC_REG_FABRIC_EVENT;
3644 fast_path_evt->un.read_check_error.header.subcategory =
3645 LPFC_EVENT_FCPRDCHKERR;
3646 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3647 &pnode->nlp_portname, sizeof(struct lpfc_name));
3648 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3649 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3650 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3651 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3652 fast_path_evt->un.read_check_error.fcpiparam =
3653 fcpi_parm;
3654 } else
3655 return;
3656
3657 fast_path_evt->vport = vport;
3658 spin_lock_irqsave(&phba->hbalock, flags);
3659 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3660 spin_unlock_irqrestore(&phba->hbalock, flags);
3661 lpfc_worker_wake_up(phba);
3662 return;
3663}
James Smart9bad7672008-12-04 22:39:02 -05003664
3665/**
James Smartf1126682009-06-10 17:22:44 -04003666 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003667 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003668 * @psb: The scsi buffer which is going to be un-mapped.
3669 *
3670 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003671 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003672 **/
dea31012005-04-17 16:05:31 -05003673static void
James Smartf1126682009-06-10 17:22:44 -04003674lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003675{
3676 /*
3677 * There are only two special cases to consider. (1) the scsi command
3678 * requested scatter-gather usage or (2) the scsi command allocated
3679 * a request buffer, but did not request use_sg. There is a third
3680 * case, but it does not require resource deallocation.
3681 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003682 if (psb->seg_cnt > 0)
3683 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003684 if (psb->prot_seg_cnt > 0)
3685 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3686 scsi_prot_sg_count(psb->pCmd),
3687 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003688}
3689
James Smart9bad7672008-12-04 22:39:02 -05003690/**
James Smart3621a712009-04-06 18:47:14 -04003691 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003692 * @vport: The virtual port for which this call is being executed.
3693 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3694 * @rsp_iocb: The response IOCB which contains FCP error.
3695 *
3696 * This routine is called to process response IOCB with status field
3697 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3698 * based upon SCSI and FCP error.
3699 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003700static void
James Smart2e0fef82007-06-17 19:56:36 -05003701lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3702 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003703{
3704 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3705 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3706 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003707 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003708 uint32_t resp_info = fcprsp->rspStatus2;
3709 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003710 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003711 uint32_t host_status = DID_OK;
3712 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05003713 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003714
James Smartea2151b2008-09-07 11:52:10 -04003715
dea31012005-04-17 16:05:31 -05003716 /*
3717 * If this is a task management command, there is no
3718 * scsi packet associated with this lpfc_cmd. The driver
3719 * consumes it.
3720 */
3721 if (fcpcmd->fcpCntl2) {
3722 scsi_status = 0;
3723 goto out;
3724 }
3725
James Smart6a9c52c2009-10-02 15:16:51 -04003726 if (resp_info & RSP_LEN_VALID) {
3727 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003728 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04003729 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3730 "2719 Invalid response length: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003731 "tgt x%x lun x%llx cmnd x%x rsplen x%x\n",
James Smart6a9c52c2009-10-02 15:16:51 -04003732 cmnd->device->id,
3733 cmnd->device->lun, cmnd->cmnd[0],
3734 rsplen);
3735 host_status = DID_ERROR;
3736 goto out;
3737 }
James Smarte40a02c2010-02-26 14:13:54 -05003738 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3739 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3740 "2757 Protocol failure detected during "
3741 "processing of FCP I/O op: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003742 "tgt x%x lun x%llx cmnd x%x rspInfo3 x%x\n",
James Smarte40a02c2010-02-26 14:13:54 -05003743 cmnd->device->id,
3744 cmnd->device->lun, cmnd->cmnd[0],
3745 fcprsp->rspInfo3);
3746 host_status = DID_ERROR;
3747 goto out;
3748 }
James Smart6a9c52c2009-10-02 15:16:51 -04003749 }
3750
James Smartc7743952006-12-02 13:34:42 -05003751 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3752 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3753 if (snslen > SCSI_SENSE_BUFFERSIZE)
3754 snslen = SCSI_SENSE_BUFFERSIZE;
3755
3756 if (resp_info & RSP_LEN_VALID)
3757 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3758 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3759 }
3760 lp = (uint32_t *)cmnd->sense_buffer;
3761
James Smartaa1c7ee2012-08-14 14:26:06 -04003762 /* special handling for under run conditions */
3763 if (!scsi_status && (resp_info & RESID_UNDER)) {
3764 /* don't log under runs if fcp set... */
3765 if (vport->cfg_log_verbose & LOG_FCP)
3766 logit = LOG_FCP_ERROR;
3767 /* unless operator says so */
3768 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3769 logit = LOG_FCP_UNDER;
3770 }
James Smartc7743952006-12-02 13:34:42 -05003771
James Smarte8b62012007-08-02 11:10:09 -04003772 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003773 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003774 "Data: x%x x%x x%x x%x x%x\n",
3775 cmnd->cmnd[0], scsi_status,
3776 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3777 be32_to_cpu(fcprsp->rspResId),
3778 be32_to_cpu(fcprsp->rspSnsLen),
3779 be32_to_cpu(fcprsp->rspRspLen),
3780 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003781
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003782 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05003783 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003784 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003785
James Smart73d91e52011-10-10 21:32:10 -04003786 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smarte2a0a9d2008-12-04 22:40:02 -05003787 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003788 "residual %d Data: x%x x%x x%x\n",
3789 be32_to_cpu(fcpcmd->fcpDl),
3790 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3791 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003792
3793 /*
James Smart7054a602007-04-25 09:52:34 -04003794 * If there is an under run check if under run reported by
3795 * storage array is same as the under run reported by HBA.
3796 * If this is not same, there is a dropped frame.
3797 */
3798 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3799 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003800 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003801 lpfc_printf_vlog(vport, KERN_WARNING,
3802 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003803 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003804 "and Underrun Data: x%x x%x x%x x%x\n",
3805 be32_to_cpu(fcpcmd->fcpDl),
3806 scsi_get_resid(cmnd), fcpi_parm,
3807 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003808 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003809 host_status = DID_ERROR;
3810 }
3811 /*
dea31012005-04-17 16:05:31 -05003812 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003813 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003814 * is not present, make sure the actual amount transferred is at
3815 * least the underflow value or fail.
3816 */
3817 if (!(resp_info & SNS_LEN_VALID) &&
3818 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003819 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3820 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003821 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003822 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003823 "underrun converted to error "
3824 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003825 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003826 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003827 host_status = DID_ERROR;
3828 }
3829 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003830 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003831 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003832 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003833 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003834 host_status = DID_ERROR;
3835
3836 /*
3837 * Check SLI validation that all the transfer was actually done
James Smart26373d22013-09-06 12:19:17 -04003838 * (fcpi_parm should be zero). Apply check only to reads.
dea31012005-04-17 16:05:31 -05003839 */
James Smart26373d22013-09-06 12:19:17 -04003840 } else if (fcpi_parm && (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -04003841 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart26373d22013-09-06 12:19:17 -04003842 "9029 FCP Read Check Error Data: "
James Smarteee88772010-09-29 11:19:08 -04003843 "x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003844 be32_to_cpu(fcpcmd->fcpDl),
3845 be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04003846 fcpi_parm, cmnd->cmnd[0], scsi_status);
3847 switch (scsi_status) {
3848 case SAM_STAT_GOOD:
3849 case SAM_STAT_CHECK_CONDITION:
3850 /* Fabric dropped a data frame. Fail any successful
3851 * command in which we detected dropped frames.
3852 * A status of good or some check conditions could
3853 * be considered a successful command.
3854 */
3855 host_status = DID_ERROR;
3856 break;
3857 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003858 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05003859 }
3860
3861 out:
3862 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04003863 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05003864}
3865
James Smart9bad7672008-12-04 22:39:02 -05003866/**
James Smart3621a712009-04-06 18:47:14 -04003867 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05003868 * @phba: The Hba for which this call is being executed.
3869 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04003870 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05003871 *
3872 * This routine assigns scsi command result by looking into response IOCB
3873 * status field appropriately. This routine handles QUEUE FULL condition as
3874 * well by ramping down device queue depth.
3875 **/
dea31012005-04-17 16:05:31 -05003876static void
3877lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3878 struct lpfc_iocbq *pIocbOut)
3879{
3880 struct lpfc_scsi_buf *lpfc_cmd =
3881 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003882 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05003883 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3884 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04003885 struct scsi_cmnd *cmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003886 int result;
James Smart5ffc2662009-11-18 15:39:44 -05003887 int depth;
James Smartfa61a542008-01-11 01:52:42 -05003888 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04003889 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04003890 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04003891 uint32_t queue_depth, scsi_id;
James Smart73d91e52011-10-10 21:32:10 -04003892 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05003893
James Smart75baf692010-06-08 18:31:21 -04003894 /* Sanity check on return of outstanding command */
3895 if (!(lpfc_cmd->pCmd))
3896 return;
3897 cmd = lpfc_cmd->pCmd;
3898 shost = cmd->device->host;
3899
James Smarte3d2b802012-08-14 14:25:43 -04003900 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
dea31012005-04-17 16:05:31 -05003901 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05003902 /* pick up SLI4 exhange busy status from HBA */
3903 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
3904
James Smart9a6b09c2012-03-01 22:37:42 -05003905#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3906 if (lpfc_cmd->prot_data_type) {
3907 struct scsi_dif_tuple *src = NULL;
3908
3909 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
3910 /*
3911 * Used to restore any changes to protection
3912 * data for error injection.
3913 */
3914 switch (lpfc_cmd->prot_data_type) {
3915 case LPFC_INJERR_REFTAG:
3916 src->ref_tag =
3917 lpfc_cmd->prot_data;
3918 break;
3919 case LPFC_INJERR_APPTAG:
3920 src->app_tag =
3921 (uint16_t)lpfc_cmd->prot_data;
3922 break;
3923 case LPFC_INJERR_GUARD:
3924 src->guard_tag =
3925 (uint16_t)lpfc_cmd->prot_data;
3926 break;
3927 default:
3928 break;
3929 }
3930
3931 lpfc_cmd->prot_data = 0;
3932 lpfc_cmd->prot_data_type = 0;
3933 lpfc_cmd->prot_data_segment = NULL;
3934 }
3935#endif
James Smart109f6ed2008-12-04 22:39:08 -05003936 if (pnode && NLP_CHK_NODE_ACT(pnode))
3937 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05003938
3939 if (lpfc_cmd->status) {
3940 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
3941 (lpfc_cmd->result & IOERR_DRVR_MASK))
3942 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3943 else if (lpfc_cmd->status >= IOSTAT_CNT)
3944 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smartaa1c7ee2012-08-14 14:26:06 -04003945 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
3946 !lpfc_cmd->fcp_rsp->rspStatus3 &&
3947 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
3948 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
James Smart73d91e52011-10-10 21:32:10 -04003949 logit = 0;
3950 else
3951 logit = LOG_FCP | LOG_FCP_UNDER;
3952 lpfc_printf_vlog(vport, KERN_WARNING, logit,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003953 "9030 FCP cmd x%x failed <%d/%lld> "
James Smart5a0d80f2012-05-09 21:18:20 -04003954 "status: x%x result: x%x "
3955 "sid: x%x did: x%x oxid: x%x "
3956 "Data: x%x x%x\n",
James Smart73d91e52011-10-10 21:32:10 -04003957 cmd->cmnd[0],
3958 cmd->device ? cmd->device->id : 0xffff,
3959 cmd->device ? cmd->device->lun : 0xffff,
3960 lpfc_cmd->status, lpfc_cmd->result,
James Smart3bf41ba2013-05-31 17:03:18 -04003961 vport->fc_myDID,
3962 (pnode) ? pnode->nlp_DID : 0,
James Smart5a0d80f2012-05-09 21:18:20 -04003963 phba->sli_rev == LPFC_SLI_REV4 ?
3964 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
James Smart73d91e52011-10-10 21:32:10 -04003965 pIocbOut->iocb.ulpContext,
3966 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05003967
3968 switch (lpfc_cmd->status) {
3969 case IOSTAT_FCP_RSP_ERROR:
3970 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05003971 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05003972 break;
3973 case IOSTAT_NPORT_BSY:
3974 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04003975 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04003976 fast_path_evt = lpfc_alloc_fast_evt(phba);
3977 if (!fast_path_evt)
3978 break;
3979 fast_path_evt->un.fabric_evt.event_type =
3980 FC_REG_FABRIC_EVENT;
3981 fast_path_evt->un.fabric_evt.subcategory =
3982 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
3983 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
3984 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3985 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
3986 &pnode->nlp_portname,
3987 sizeof(struct lpfc_name));
3988 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
3989 &pnode->nlp_nodename,
3990 sizeof(struct lpfc_name));
3991 }
3992 fast_path_evt->vport = vport;
3993 fast_path_evt->work_evt.evt =
3994 LPFC_EVT_FASTPATH_MGMT_EVT;
3995 spin_lock_irqsave(&phba->hbalock, flags);
3996 list_add_tail(&fast_path_evt->work_evt.evt_listp,
3997 &phba->work_list);
3998 spin_unlock_irqrestore(&phba->hbalock, flags);
3999 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004000 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004001 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05004002 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05004003 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
4004 lpfc_cmd->result ==
4005 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
4006 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
4007 lpfc_cmd->result ==
4008 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
4009 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
4010 break;
4011 }
James Smartd7c255b2008-08-24 21:50:00 -04004012 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05004013 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04004014 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
4015 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
James Smart92d7f7b2007-06-17 19:56:38 -05004016 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04004017 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05004018 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004019 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
4020 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
4021 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
4022 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
4023 /*
4024 * This is a response for a BG enabled
4025 * cmd. Parse BG error
4026 */
4027 lpfc_parse_bg_err(phba, lpfc_cmd,
4028 pIocbOut);
4029 break;
4030 } else {
4031 lpfc_printf_vlog(vport, KERN_WARNING,
4032 LOG_BG,
4033 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04004034 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05004035 }
4036 }
James Smart1151e3e2011-02-16 12:39:35 -05004037 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
4038 && (phba->sli_rev == LPFC_SLI_REV4)
4039 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
4040 /* This IO was aborted by the target, we don't
4041 * know the rxid and because we did not send the
4042 * ABTS we cannot generate and RRQ.
4043 */
4044 lpfc_set_rrq_active(phba, pnode,
James Smartee0f4fe2012-05-09 21:19:14 -04004045 lpfc_cmd->cur_iocbq.sli4_lxritag,
4046 0, 0);
James Smart1151e3e2011-02-16 12:39:35 -05004047 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004048 /* else: fall through */
dea31012005-04-17 16:05:31 -05004049 default:
4050 cmd->result = ScsiResult(DID_ERROR, 0);
4051 break;
4052 }
4053
James Smart58da1ff2008-04-07 10:15:56 -04004054 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004055 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04004056 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
4057 SAM_STAT_BUSY);
James Smartab56dc22011-02-16 12:39:57 -05004058 } else
dea31012005-04-17 16:05:31 -05004059 cmd->result = ScsiResult(DID_OK, 0);
dea31012005-04-17 16:05:31 -05004060
4061 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4062 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
4063
James Smarte8b62012007-08-02 11:10:09 -04004064 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004065 "0710 Iodone <%d/%llu> cmd %p, error "
James Smarte8b62012007-08-02 11:10:09 -04004066 "x%x SNS x%x x%x Data: x%x x%x\n",
4067 cmd->device->id, cmd->device->lun, cmd,
4068 cmd->result, *lp, *(lp + 3), cmd->retries,
4069 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05004070 }
4071
James Smartea2151b2008-09-07 11:52:10 -04004072 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004073 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04004074 if (vport->cfg_max_scsicmpl_time &&
4075 time_after(jiffies, lpfc_cmd->start_time +
4076 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04004077 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004078 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4079 if (pnode->cmd_qdepth >
4080 atomic_read(&pnode->cmd_pending) &&
4081 (atomic_read(&pnode->cmd_pending) >
4082 LPFC_MIN_TGT_QDEPTH) &&
4083 ((cmd->cmnd[0] == READ_10) ||
4084 (cmd->cmnd[0] == WRITE_10)))
4085 pnode->cmd_qdepth =
4086 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04004087
James Smart109f6ed2008-12-04 22:39:08 -05004088 pnode->last_change_time = jiffies;
4089 }
James Smarta257bf92009-04-06 18:48:10 -04004090 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004091 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smart7dc517d2010-07-14 15:32:10 -04004092 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
James Smart977b5a02008-09-07 11:52:04 -04004093 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05004094 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04004095 spin_lock_irqsave(shost->host_lock, flags);
James Smart7dc517d2010-07-14 15:32:10 -04004096 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
4097 / 100;
4098 depth = depth ? depth : 1;
4099 pnode->cmd_qdepth += depth;
4100 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
4101 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05004102 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04004103 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004104 }
James Smart977b5a02008-09-07 11:52:04 -04004105 }
4106
James Smart1dcb58e2007-04-25 09:51:30 -04004107 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04004108
4109 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
4110 queue_depth = cmd->device->queue_depth;
4111 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05004112 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004113
Jamie Wellnitzb8086082006-02-28 22:33:12 -05004114 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart876dd7d2012-09-29 11:31:28 -04004115 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004116 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04004117 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004118
James Smartfa61a542008-01-11 01:52:42 -05004119 /*
4120 * If there is a thread waiting for command completion
4121 * wake up the thread.
4122 */
James Smarta257bf92009-04-06 18:48:10 -04004123 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004124 if (lpfc_cmd->waitq)
4125 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04004126 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05004127 lpfc_release_scsi_buf(phba, lpfc_cmd);
4128 return;
4129 }
4130
James Smart876dd7d2012-09-29 11:31:28 -04004131 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004132 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04004133 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004134
James Smartfa61a542008-01-11 01:52:42 -05004135 /*
4136 * If there is a thread waiting for command completion
4137 * wake up the thread.
4138 */
James Smarta257bf92009-04-06 18:48:10 -04004139 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004140 if (lpfc_cmd->waitq)
4141 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04004142 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004143
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004144 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004145}
4146
James Smart34b02dc2008-08-24 21:49:55 -04004147/**
James Smart3621a712009-04-06 18:47:14 -04004148 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04004149 * @data: A pointer to the immediate command data portion of the IOCB.
4150 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
4151 *
4152 * The routine copies the entire FCP command from @fcp_cmnd to @data while
4153 * byte swapping the data to big endian format for transmission on the wire.
4154 **/
4155static void
4156lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
4157{
4158 int i, j;
4159 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
4160 i += sizeof(uint32_t), j++) {
4161 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
4162 }
4163}
4164
James Smart9bad7672008-12-04 22:39:02 -05004165/**
James Smartf1126682009-06-10 17:22:44 -04004166 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004167 * @vport: The virtual port for which this call is being executed.
4168 * @lpfc_cmd: The scsi command which needs to send.
4169 * @pnode: Pointer to lpfc_nodelist.
4170 *
4171 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04004172 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004173 **/
dea31012005-04-17 16:05:31 -05004174static void
James Smartf1126682009-06-10 17:22:44 -04004175lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05004176 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05004177{
James Smart2e0fef82007-06-17 19:56:36 -05004178 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004179 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4180 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4181 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4182 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
4183 int datadir = scsi_cmnd->sc_data_direction;
James Smart027140e2012-08-03 12:35:44 -04004184 uint8_t *ptr;
4185 bool sli4;
James Smart98bbf5f2013-09-06 12:18:45 -04004186 uint32_t fcpdl;
dea31012005-04-17 16:05:31 -05004187
James Smart58da1ff2008-04-07 10:15:56 -04004188 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4189 return;
4190
dea31012005-04-17 16:05:31 -05004191 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04004192 /* clear task management bits */
4193 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05004194
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04004195 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4196 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004197
James Smart027140e2012-08-03 12:35:44 -04004198 ptr = &fcp_cmnd->fcpCdb[0];
4199 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4200 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4201 ptr += scsi_cmnd->cmd_len;
4202 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4203 }
4204
Christoph Hellwig50668632014-10-30 14:30:06 +01004205 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
dea31012005-04-17 16:05:31 -05004206
James Smart027140e2012-08-03 12:35:44 -04004207 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
James Smart6acb3482014-04-04 13:51:25 -04004208 piocbq->iocb.un.fcpi.fcpi_XRdy = 0;
James Smart027140e2012-08-03 12:35:44 -04004209
dea31012005-04-17 16:05:31 -05004210 /*
4211 * There are three possibilities here - use scatter-gather segment, use
4212 * the single mapping, or neither. Start the lpfc command prep by
4213 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4214 * data bde entry.
4215 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05004216 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05004217 if (datadir == DMA_TO_DEVICE) {
4218 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart182ba752013-07-15 18:34:05 -04004219 iocb_cmd->ulpPU = PARM_READ_CHECK;
James Smart3cb01c52013-07-15 18:35:04 -04004220 if (vport->cfg_first_burst_size &&
4221 (pnode->nlp_flag & NLP_FIRSTBURST)) {
James Smart98bbf5f2013-09-06 12:18:45 -04004222 fcpdl = scsi_bufflen(scsi_cmnd);
4223 if (fcpdl < vport->cfg_first_burst_size)
4224 piocbq->iocb.un.fcpi.fcpi_XRdy = fcpdl;
4225 else
4226 piocbq->iocb.un.fcpi.fcpi_XRdy =
4227 vport->cfg_first_burst_size;
James Smart3cb01c52013-07-15 18:35:04 -04004228 }
dea31012005-04-17 16:05:31 -05004229 fcp_cmnd->fcpCntl3 = WRITE_DATA;
4230 phba->fc4OutputRequests++;
4231 } else {
4232 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4233 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05004234 fcp_cmnd->fcpCntl3 = READ_DATA;
4235 phba->fc4InputRequests++;
4236 }
4237 } else {
4238 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4239 iocb_cmd->un.fcpi.fcpi_parm = 0;
4240 iocb_cmd->ulpPU = 0;
4241 fcp_cmnd->fcpCntl3 = 0;
4242 phba->fc4ControlRequests++;
4243 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004244 if (phba->sli_rev == 3 &&
4245 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004246 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004247 /*
4248 * Finish initializing those IOCB fields that are independent
4249 * of the scsi_cmnd request_buffer
4250 */
4251 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart027140e2012-08-03 12:35:44 -04004252 if (sli4)
James Smart6d368e52011-05-24 11:44:12 -04004253 piocbq->iocb.ulpContext =
4254 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05004255 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4256 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05004257 else
4258 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05004259
4260 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4261 piocbq->context1 = lpfc_cmd;
4262 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4263 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05004264 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05004265}
4266
James Smart9bad7672008-12-04 22:39:02 -05004267/**
James Smart6d368e52011-05-24 11:44:12 -04004268 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004269 * @vport: The virtual port for which this call is being executed.
4270 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4271 * @lun: Logical unit number.
4272 * @task_mgmt_cmd: SCSI task management command.
4273 *
James Smart3772a992009-05-22 14:50:54 -04004274 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4275 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004276 *
4277 * Return codes:
4278 * 0 - Error
4279 * 1 - Success
4280 **/
dea31012005-04-17 16:05:31 -05004281static int
James Smartf1126682009-06-10 17:22:44 -04004282lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05004283 struct lpfc_scsi_buf *lpfc_cmd,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004284 uint64_t lun,
dea31012005-04-17 16:05:31 -05004285 uint8_t task_mgmt_cmd)
4286{
dea31012005-04-17 16:05:31 -05004287 struct lpfc_iocbq *piocbq;
4288 IOCB_t *piocb;
4289 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04004290 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05004291 struct lpfc_nodelist *ndlp = rdata->pnode;
4292
James Smart58da1ff2008-04-07 10:15:56 -04004293 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4294 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05004295 return 0;
dea31012005-04-17 16:05:31 -05004296
dea31012005-04-17 16:05:31 -05004297 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004298 piocbq->vport = vport;
4299
dea31012005-04-17 16:05:31 -05004300 piocb = &piocbq->iocb;
4301
4302 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04004303 /* Clear out any old data in the FCP command area */
4304 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4305 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004306 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05004307 if (vport->phba->sli_rev == 3 &&
4308 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004309 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004310 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05004311 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04004312 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4313 piocb->ulpContext =
4314 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4315 }
James Smart53151bb2013-10-10 12:24:07 -04004316 piocb->ulpFCP2Rcvy = (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) ? 1 : 0;
dea31012005-04-17 16:05:31 -05004317 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
James Smartf9226c22014-02-20 09:57:28 -05004318 piocb->ulpPU = 0;
4319 piocb->un.fcpi.fcpi_parm = 0;
dea31012005-04-17 16:05:31 -05004320
4321 /* ulpTimeout is only one byte */
4322 if (lpfc_cmd->timeout > 0xff) {
4323 /*
4324 * Do not timeout the command at the firmware level.
4325 * The driver will provide the timeout mechanism.
4326 */
4327 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04004328 } else
dea31012005-04-17 16:05:31 -05004329 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04004330
4331 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4332 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004333
James Smart2e0fef82007-06-17 19:56:36 -05004334 return 1;
dea31012005-04-17 16:05:31 -05004335}
4336
James Smart9bad7672008-12-04 22:39:02 -05004337/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004338 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04004339 * @phba: The hba struct for which this call is being executed.
4340 * @dev_grp: The HBA PCI-Device group number.
4341 *
4342 * This routine sets up the SCSI interface API function jump table in @phba
4343 * struct.
4344 * Returns: 0 - success, -ENODEV - failure.
4345 **/
4346int
4347lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4348{
4349
James Smartf1126682009-06-10 17:22:44 -04004350 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4351 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04004352
James Smart3772a992009-05-22 14:50:54 -04004353 switch (dev_grp) {
4354 case LPFC_PCI_DEV_LP:
4355 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4356 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05004357 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004358 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05004359 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004360 break;
James Smartda0436e2009-05-22 14:51:39 -04004361 case LPFC_PCI_DEV_OC:
4362 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4363 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05004364 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004365 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05004366 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004367 break;
James Smart3772a992009-05-22 14:50:54 -04004368 default:
4369 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4370 "1418 Invalid HBA PCI-device group: 0x%x\n",
4371 dev_grp);
4372 return -ENODEV;
4373 break;
4374 }
James Smart3772a992009-05-22 14:50:54 -04004375 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05004376 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04004377 return 0;
4378}
4379
4380/**
James Smart3621a712009-04-06 18:47:14 -04004381 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05004382 * @phba: The Hba for which this call is being executed.
4383 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4384 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4385 *
4386 * This routine is IOCB completion routine for device reset and target reset
4387 * routine. This routine release scsi buffer associated with lpfc_cmd.
4388 **/
James Smart7054a602007-04-25 09:52:34 -04004389static void
4390lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4391 struct lpfc_iocbq *cmdiocbq,
4392 struct lpfc_iocbq *rspiocbq)
4393{
4394 struct lpfc_scsi_buf *lpfc_cmd =
4395 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4396 if (lpfc_cmd)
4397 lpfc_release_scsi_buf(phba, lpfc_cmd);
4398 return;
4399}
4400
James Smart9bad7672008-12-04 22:39:02 -05004401/**
James Smart3621a712009-04-06 18:47:14 -04004402 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05004403 * @host: The scsi host for which this call is being executed.
4404 *
4405 * This routine provides module information about hba.
4406 *
4407 * Reutrn code:
4408 * Pointer to char - Success.
4409 **/
dea31012005-04-17 16:05:31 -05004410const char *
4411lpfc_info(struct Scsi_Host *host)
4412{
James Smart2e0fef82007-06-17 19:56:36 -05004413 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4414 struct lpfc_hba *phba = vport->phba;
James Smart8b68cd52012-09-29 11:32:37 -04004415 int len, link_speed = 0;
dea31012005-04-17 16:05:31 -05004416 static char lpfcinfobuf[384];
4417
4418 memset(lpfcinfobuf,0,384);
4419 if (phba && phba->pcidev){
4420 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4421 len = strlen(lpfcinfobuf);
4422 snprintf(lpfcinfobuf + len,
4423 384-len,
4424 " on PCI bus %02x device %02x irq %d",
4425 phba->pcidev->bus->number,
4426 phba->pcidev->devfn,
4427 phba->pcidev->irq);
4428 len = strlen(lpfcinfobuf);
4429 if (phba->Port[0]) {
4430 snprintf(lpfcinfobuf + len,
4431 384-len,
4432 " port %s",
4433 phba->Port);
4434 }
James Smart65467b62010-01-26 23:08:29 -05004435 len = strlen(lpfcinfobuf);
James Smart8b68cd52012-09-29 11:32:37 -04004436 if (phba->sli_rev <= LPFC_SLI_REV3) {
4437 link_speed = lpfc_sli_port_speed_get(phba);
4438 } else {
4439 if (phba->sli4_hba.link_state.logical_speed)
4440 link_speed =
4441 phba->sli4_hba.link_state.logical_speed;
4442 else
4443 link_speed = phba->sli4_hba.link_state.speed;
James Smart65467b62010-01-26 23:08:29 -05004444 }
James Smart8b68cd52012-09-29 11:32:37 -04004445 if (link_speed != 0)
4446 snprintf(lpfcinfobuf + len, 384-len,
4447 " Logical Link Speed: %d Mbps", link_speed);
dea31012005-04-17 16:05:31 -05004448 }
4449 return lpfcinfobuf;
4450}
4451
James Smart9bad7672008-12-04 22:39:02 -05004452/**
James Smart3621a712009-04-06 18:47:14 -04004453 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05004454 * @phba: The Hba for which this call is being executed.
4455 *
4456 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4457 * The default value of cfg_poll_tmo is 10 milliseconds.
4458 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004459static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4460{
4461 unsigned long poll_tmo_expires =
4462 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4463
James Smart0e9bb8d2013-03-01 16:35:12 -05004464 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004465 mod_timer(&phba->fcp_poll_timer,
4466 poll_tmo_expires);
4467}
4468
James Smart9bad7672008-12-04 22:39:02 -05004469/**
James Smart3621a712009-04-06 18:47:14 -04004470 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004471 * @phba: The Hba for which this call is being executed.
4472 *
4473 * This routine starts the fcp_poll_timer of @phba.
4474 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004475void lpfc_poll_start_timer(struct lpfc_hba * phba)
4476{
4477 lpfc_poll_rearm_timer(phba);
4478}
4479
James Smart9bad7672008-12-04 22:39:02 -05004480/**
James Smart3621a712009-04-06 18:47:14 -04004481 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05004482 * @ptr: Map to lpfc_hba data structure pointer.
4483 *
4484 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4485 * and FCP Ring interrupt is disable.
4486 **/
4487
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004488void lpfc_poll_timeout(unsigned long ptr)
4489{
James Smart2e0fef82007-06-17 19:56:36 -05004490 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004491
4492 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004493 lpfc_sli_handle_fast_ring_event(phba,
4494 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4495
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004496 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4497 lpfc_poll_rearm_timer(phba);
4498 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004499}
4500
James Smart9bad7672008-12-04 22:39:02 -05004501/**
James Smart3621a712009-04-06 18:47:14 -04004502 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05004503 * @cmnd: Pointer to scsi_cmnd data structure.
4504 * @done: Pointer to done routine.
4505 *
4506 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4507 * This routine prepares an IOCB from scsi command and provides to firmware.
4508 * The @done callback is invoked after driver finished processing the command.
4509 *
4510 * Return value :
4511 * 0 - Success
4512 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4513 **/
dea31012005-04-17 16:05:31 -05004514static int
James Smartb9a7c632012-08-03 12:35:24 -04004515lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004516{
James Smart2e0fef82007-06-17 19:56:36 -05004517 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4518 struct lpfc_hba *phba = vport->phba;
James Smart1ba981f2014-02-20 09:56:45 -05004519 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004520 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004521 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004522 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004523 int err;
dea31012005-04-17 16:05:31 -05004524
James Smart1ba981f2014-02-20 09:56:45 -05004525 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004526 err = fc_remote_port_chkready(rport);
4527 if (err) {
4528 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004529 goto out_fail_command;
4530 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004531 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004532
James Smartbf086112011-08-21 21:48:13 -04004533 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004534 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004535
James Smart6a9c52c2009-10-02 15:16:51 -04004536 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4537 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4538 " op:%02x str=%s without registering for"
4539 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004540 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4541 dif_op_str[scsi_get_prot_op(cmnd)]);
4542 goto out_fail_command;
4543 }
4544
dea31012005-04-17 16:05:31 -05004545 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004546 * Catch race where our node has transitioned, but the
4547 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004548 */
James Smart6b415f52012-06-12 13:54:59 -04004549 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4550 goto out_tgt_busy;
James Smart7dc517d2010-07-14 15:32:10 -04004551 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
Mike Christie34963432011-02-25 14:04:28 -06004552 goto out_tgt_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05004553
James Smart19ca7602010-11-20 23:11:55 -05004554 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05004555 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004556 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004557
James Smarte8b62012007-08-02 11:10:09 -04004558 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4559 "0707 driver's buffer pool is empty, "
4560 "IO busied\n");
dea31012005-04-17 16:05:31 -05004561 goto out_host_busy;
4562 }
4563
4564 /*
4565 * Store the midlayer's command structure for the completion phase
4566 * and complete the command initialization.
4567 */
4568 lpfc_cmd->pCmd = cmnd;
4569 lpfc_cmd->rdata = rdata;
4570 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04004571 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05004572 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
dea31012005-04-17 16:05:31 -05004573
James Smarte2a0a9d2008-12-04 22:40:02 -05004574 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004575 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004576 lpfc_printf_vlog(vport,
4577 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004578 "9033 BLKGRD: rcvd %s cmd:x%x "
4579 "sector x%llx cnt %u pt %x\n",
4580 dif_op_str[scsi_get_prot_op(cmnd)],
4581 cmnd->cmnd[0],
4582 (unsigned long long)scsi_get_lba(cmnd),
4583 blk_rq_sectors(cmnd->request),
4584 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004585 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004586 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4587 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004588 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004589 lpfc_printf_vlog(vport,
4590 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004591 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4592 "x%x sector x%llx cnt %u pt %x\n",
4593 cmnd->cmnd[0],
4594 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004595 blk_rq_sectors(cmnd->request),
James Smart26134702012-08-14 14:25:50 -04004596 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004597 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004598 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4599 }
4600
dea31012005-04-17 16:05:31 -05004601 if (err)
4602 goto out_host_busy_free_buf;
4603
James Smart2e0fef82007-06-17 19:56:36 -05004604 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004605
James Smart977b5a02008-09-07 11:52:04 -04004606 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04004607 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004608 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05004609 if (err) {
4610 atomic_dec(&ndlp->cmd_pending);
James Smart76f96b62013-12-17 20:29:01 -05004611 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4612 "3376 FCP could not issue IOCB err %x"
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004613 "FCP cmd x%x <%d/%llu> "
James Smart76f96b62013-12-17 20:29:01 -05004614 "sid: x%x did: x%x oxid: x%x "
4615 "Data: x%x x%x x%x x%x\n",
4616 err, cmnd->cmnd[0],
4617 cmnd->device ? cmnd->device->id : 0xffff,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004618 cmnd->device ? cmnd->device->lun : (u64) -1,
James Smart76f96b62013-12-17 20:29:01 -05004619 vport->fc_myDID, ndlp->nlp_DID,
4620 phba->sli_rev == LPFC_SLI_REV4 ?
4621 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
4622 lpfc_cmd->cur_iocbq.iocb.ulpContext,
4623 lpfc_cmd->cur_iocbq.iocb.ulpIoTag,
4624 lpfc_cmd->cur_iocbq.iocb.ulpTimeout,
4625 (uint32_t)
4626 (cmnd->request->timeout / 1000));
4627
4628
dea31012005-04-17 16:05:31 -05004629 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004630 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004631 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004632 lpfc_sli_handle_fast_ring_event(phba,
4633 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4634
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004635 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4636 lpfc_poll_rearm_timer(phba);
4637 }
4638
dea31012005-04-17 16:05:31 -05004639 return 0;
4640
4641 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04004642 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004643 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004644 out_host_busy:
4645 return SCSI_MLQUEUE_HOST_BUSY;
4646
Mike Christie34963432011-02-25 14:04:28 -06004647 out_tgt_busy:
4648 return SCSI_MLQUEUE_TARGET_BUSY;
4649
dea31012005-04-17 16:05:31 -05004650 out_fail_command:
James Smartb9a7c632012-08-03 12:35:24 -04004651 cmnd->scsi_done(cmnd);
dea31012005-04-17 16:05:31 -05004652 return 0;
4653}
4654
Jeff Garzikf2812332010-11-16 02:10:29 -05004655
James Smart9bad7672008-12-04 22:39:02 -05004656/**
James Smart3621a712009-04-06 18:47:14 -04004657 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004658 * @cmnd: Pointer to scsi_cmnd data structure.
4659 *
4660 * This routine aborts @cmnd pending in base driver.
4661 *
4662 * Return code :
4663 * 0x2003 - Error
4664 * 0x2002 - Success
4665 **/
dea31012005-04-17 16:05:31 -05004666static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004667lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004668{
James Smart2e0fef82007-06-17 19:56:36 -05004669 struct Scsi_Host *shost = cmnd->device->host;
4670 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4671 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004672 struct lpfc_iocbq *iocb;
4673 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004674 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004675 IOCB_t *cmd, *icmd;
James Smart3a707302012-06-12 13:54:42 -04004676 int ret = SUCCESS, status = 0;
James Smart98912dd2014-04-04 13:52:31 -04004677 struct lpfc_sli_ring *pring_s4;
4678 int ring_number, ret_val;
4679 unsigned long flags, iflags;
James Smartfa61a542008-01-11 01:52:42 -05004680 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004681
James Smart3a707302012-06-12 13:54:42 -04004682 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004683 if (status != 0 && status != SUCCESS)
James Smart3a707302012-06-12 13:54:42 -04004684 return status;
James Smart4f2e66c2012-05-09 21:17:07 -04004685
James Smart876dd7d2012-09-29 11:31:28 -04004686 spin_lock_irqsave(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004687 /* driver queued commands are in process of being flushed */
4688 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
James Smart876dd7d2012-09-29 11:31:28 -04004689 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004690 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4691 "3168 SCSI Layer abort requested I/O has been "
4692 "flushed by LLD.\n");
4693 return FAILED;
4694 }
4695
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004696 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smart92e3af62012-08-14 14:26:28 -04004697 if (!lpfc_cmd || !lpfc_cmd->pCmd) {
James Smart876dd7d2012-09-29 11:31:28 -04004698 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarteee88772010-09-29 11:19:08 -04004699 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4700 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004701 "x%x ID %d LUN %llu\n",
James Smart3a707302012-06-12 13:54:42 -04004702 SUCCESS, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04004703 return SUCCESS;
4704 }
dea31012005-04-17 16:05:31 -05004705
James Smart4f2e66c2012-05-09 21:17:07 -04004706 iocb = &lpfc_cmd->cur_iocbq;
4707 /* the command is in process of being cancelled */
4708 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
James Smart876dd7d2012-09-29 11:31:28 -04004709 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004710 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4711 "3169 SCSI Layer abort requested I/O has been "
4712 "cancelled by LLD.\n");
4713 return FAILED;
4714 }
dea31012005-04-17 16:05:31 -05004715 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004716 * If pCmd field of the corresponding lpfc_scsi_buf structure
4717 * points to a different SCSI command, then the driver has
4718 * already completed this command, but the midlayer did not
James Smart4f2e66c2012-05-09 21:17:07 -04004719 * see the completion before the eh fired. Just return SUCCESS.
dea31012005-04-17 16:05:31 -05004720 */
James Smart4f2e66c2012-05-09 21:17:07 -04004721 if (lpfc_cmd->pCmd != cmnd) {
4722 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4723 "3170 SCSI Layer abort requested I/O has been "
4724 "completed by LLD.\n");
4725 goto out_unlock;
4726 }
dea31012005-04-17 16:05:31 -05004727
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004728 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004729
James Smartee620212014-04-04 13:51:53 -04004730 /* abort issued in recovery is still in progress */
4731 if (iocb->iocb_flag & LPFC_DRIVER_ABORTED) {
4732 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4733 "3389 SCSI Layer I/O Abort Request is pending\n");
4734 spin_unlock_irqrestore(&phba->hbalock, flags);
4735 goto wait_for_cmpl;
4736 }
4737
James Smart4f2e66c2012-05-09 21:17:07 -04004738 abtsiocb = __lpfc_sli_get_iocbq(phba);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004739 if (abtsiocb == NULL) {
4740 ret = FAILED;
James Smart4f2e66c2012-05-09 21:17:07 -04004741 goto out_unlock;
dea31012005-04-17 16:05:31 -05004742 }
4743
James Smartafbd8d82013-09-06 12:22:13 -04004744 /* Indicate the IO is being aborted by the driver. */
4745 iocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4746
dea31012005-04-17 16:05:31 -05004747 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004748 * The scsi command can not be in txq and it is in flight because the
4749 * pCmd is still pointig at the SCSI command we have to abort. There
4750 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004751 */
dea31012005-04-17 16:05:31 -05004752
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004753 cmd = &iocb->iocb;
4754 icmd = &abtsiocb->iocb;
4755 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4756 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004757 if (phba->sli_rev == LPFC_SLI_REV4)
4758 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4759 else
4760 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004761
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004762 icmd->ulpLe = 1;
4763 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004764
4765 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4766 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05004767 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -04004768 if (iocb->iocb_flag & LPFC_IO_FOF)
4769 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -05004770
James Smart2e0fef82007-06-17 19:56:36 -05004771 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004772 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4773 else
4774 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004775
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004776 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004777 abtsiocb->vport = vport;
James Smart98912dd2014-04-04 13:52:31 -04004778 if (phba->sli_rev == LPFC_SLI_REV4) {
4779 ring_number = MAX_SLI3_CONFIGURED_RINGS + iocb->fcp_wqidx;
4780 pring_s4 = &phba->sli.ring[ring_number];
4781 /* Note: both hbalock and ring_lock must be set here */
4782 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
4783 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
4784 abtsiocb, 0);
4785 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
4786 } else {
4787 ret_val = __lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
4788 abtsiocb, 0);
4789 }
James Smart4f2e66c2012-05-09 21:17:07 -04004790 /* no longer need the lock after this point */
James Smart876dd7d2012-09-29 11:31:28 -04004791 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004792
James Smart98912dd2014-04-04 13:52:31 -04004793
4794 if (ret_val == IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004795 lpfc_sli_release_iocbq(phba, abtsiocb);
4796 ret = FAILED;
4797 goto out;
4798 }
4799
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004800 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004801 lpfc_sli_handle_fast_ring_event(phba,
4802 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004803
James Smartee620212014-04-04 13:51:53 -04004804wait_for_cmpl:
James Smartfa61a542008-01-11 01:52:42 -05004805 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004806 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004807 wait_event_timeout(waitq,
4808 (lpfc_cmd->pCmd != cmnd),
James Smart256ec0d2013-04-17 20:14:58 -04004809 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
James Smartee620212014-04-04 13:51:53 -04004810
4811 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004812 lpfc_cmd->waitq = NULL;
James Smartee620212014-04-04 13:51:53 -04004813 spin_unlock_irqrestore(shost->host_lock, flags);
dea31012005-04-17 16:05:31 -05004814
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004815 if (lpfc_cmd->pCmd == cmnd) {
4816 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04004817 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4818 "0748 abort handler timed out waiting "
James Smart247ca942012-08-14 14:26:13 -04004819 "for abortng I/O (xri:x%x) to complete: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004820 "ret %#x, ID %d, LUN %llu\n",
James Smart247ca942012-08-14 14:26:13 -04004821 iocb->sli4_xritag, ret,
4822 cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004823 }
James Smart4f2e66c2012-05-09 21:17:07 -04004824 goto out;
dea31012005-04-17 16:05:31 -05004825
James Smart4f2e66c2012-05-09 21:17:07 -04004826out_unlock:
James Smart876dd7d2012-09-29 11:31:28 -04004827 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004828out:
James Smarte8b62012007-08-02 11:10:09 -04004829 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4830 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004831 "LUN %llu\n", ret, cmnd->device->id,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004832 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004833 return ret;
dea31012005-04-17 16:05:31 -05004834}
4835
James Smartbbb9d182009-06-10 17:23:16 -04004836static char *
4837lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4838{
4839 switch (task_mgmt_cmd) {
4840 case FCP_ABORT_TASK_SET:
4841 return "ABORT_TASK_SET";
4842 case FCP_CLEAR_TASK_SET:
4843 return "FCP_CLEAR_TASK_SET";
4844 case FCP_BUS_RESET:
4845 return "FCP_BUS_RESET";
4846 case FCP_LUN_RESET:
4847 return "FCP_LUN_RESET";
4848 case FCP_TARGET_RESET:
4849 return "FCP_TARGET_RESET";
4850 case FCP_CLEAR_ACA:
4851 return "FCP_CLEAR_ACA";
4852 case FCP_TERMINATE_TASK:
4853 return "FCP_TERMINATE_TASK";
4854 default:
4855 return "unknown";
4856 }
4857}
4858
James Smart53151bb2013-10-10 12:24:07 -04004859
4860/**
4861 * lpfc_check_fcp_rsp - check the returned fcp_rsp to see if task failed
4862 * @vport: The virtual port for which this call is being executed.
4863 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4864 *
4865 * This routine checks the FCP RSP INFO to see if the tsk mgmt command succeded
4866 *
4867 * Return code :
4868 * 0x2003 - Error
4869 * 0x2002 - Success
4870 **/
4871static int
4872lpfc_check_fcp_rsp(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd)
4873{
4874 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
4875 uint32_t rsp_info;
4876 uint32_t rsp_len;
4877 uint8_t rsp_info_code;
4878 int ret = FAILED;
4879
4880
4881 if (fcprsp == NULL)
4882 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4883 "0703 fcp_rsp is missing\n");
4884 else {
4885 rsp_info = fcprsp->rspStatus2;
4886 rsp_len = be32_to_cpu(fcprsp->rspRspLen);
4887 rsp_info_code = fcprsp->rspInfo3;
4888
4889
4890 lpfc_printf_vlog(vport, KERN_INFO,
4891 LOG_FCP,
4892 "0706 fcp_rsp valid 0x%x,"
4893 " rsp len=%d code 0x%x\n",
4894 rsp_info,
4895 rsp_len, rsp_info_code);
4896
4897 if ((fcprsp->rspStatus2&RSP_LEN_VALID) && (rsp_len == 8)) {
4898 switch (rsp_info_code) {
4899 case RSP_NO_FAILURE:
4900 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4901 "0715 Task Mgmt No Failure\n");
4902 ret = SUCCESS;
4903 break;
4904 case RSP_TM_NOT_SUPPORTED: /* TM rejected */
4905 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4906 "0716 Task Mgmt Target "
4907 "reject\n");
4908 break;
4909 case RSP_TM_NOT_COMPLETED: /* TM failed */
4910 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4911 "0717 Task Mgmt Target "
4912 "failed TM\n");
4913 break;
4914 case RSP_TM_INVALID_LU: /* TM to invalid LU! */
4915 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4916 "0718 Task Mgmt to invalid "
4917 "LUN\n");
4918 break;
4919 }
4920 }
4921 }
4922 return ret;
4923}
4924
4925
James Smartbbb9d182009-06-10 17:23:16 -04004926/**
4927 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
4928 * @vport: The virtual port for which this call is being executed.
4929 * @rdata: Pointer to remote port local data
4930 * @tgt_id: Target ID of remote device.
4931 * @lun_id: Lun number for the TMF
4932 * @task_mgmt_cmd: type of TMF to send
4933 *
4934 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
4935 * a remote port.
4936 *
4937 * Return Code:
4938 * 0x2003 - Error
4939 * 0x2002 - Success.
4940 **/
4941static int
4942lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004943 unsigned tgt_id, uint64_t lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04004944 uint8_t task_mgmt_cmd)
4945{
4946 struct lpfc_hba *phba = vport->phba;
4947 struct lpfc_scsi_buf *lpfc_cmd;
4948 struct lpfc_iocbq *iocbq;
4949 struct lpfc_iocbq *iocbqrsp;
James Smart5989b8d2010-10-22 11:06:56 -04004950 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004951 int ret;
4952 int status;
4953
James Smart5989b8d2010-10-22 11:06:56 -04004954 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smartbbb9d182009-06-10 17:23:16 -04004955 return FAILED;
4956
James Smart19ca7602010-11-20 23:11:55 -05004957 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
James Smartbbb9d182009-06-10 17:23:16 -04004958 if (lpfc_cmd == NULL)
4959 return FAILED;
James Smart0c411222013-09-06 12:22:46 -04004960 lpfc_cmd->timeout = phba->cfg_task_mgmt_tmo;
James Smartbbb9d182009-06-10 17:23:16 -04004961 lpfc_cmd->rdata = rdata;
4962
4963 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
4964 task_mgmt_cmd);
4965 if (!status) {
4966 lpfc_release_scsi_buf(phba, lpfc_cmd);
4967 return FAILED;
4968 }
4969
4970 iocbq = &lpfc_cmd->cur_iocbq;
4971 iocbqrsp = lpfc_sli_get_iocbq(phba);
4972 if (iocbqrsp == NULL) {
4973 lpfc_release_scsi_buf(phba, lpfc_cmd);
4974 return FAILED;
4975 }
James Smart5a0916b2013-07-15 18:31:42 -04004976 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
James Smartbbb9d182009-06-10 17:23:16 -04004977
4978 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004979 "0702 Issue %s to TGT %d LUN %llu "
James Smart6d368e52011-05-24 11:44:12 -04004980 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04004981 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04004982 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
4983 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04004984
4985 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
4986 iocbq, iocbqrsp, lpfc_cmd->timeout);
James Smart53151bb2013-10-10 12:24:07 -04004987 if ((status != IOCB_SUCCESS) ||
4988 (iocbqrsp->iocb.ulpStatus != IOSTAT_SUCCESS)) {
James Smartbbb9d182009-06-10 17:23:16 -04004989 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004990 "0727 TMF %s to TGT %d LUN %llu failed (%d, %d) "
James Smart6d368e52011-05-24 11:44:12 -04004991 "iocb_flag x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04004992 lpfc_taskmgmt_name(task_mgmt_cmd),
4993 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
James Smart6d368e52011-05-24 11:44:12 -04004994 iocbqrsp->iocb.un.ulpWord[4],
4995 iocbq->iocb_flag);
James Smart53151bb2013-10-10 12:24:07 -04004996 /* if ulpStatus != IOCB_SUCCESS, then status == IOCB_SUCCESS */
4997 if (status == IOCB_SUCCESS) {
4998 if (iocbqrsp->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
4999 /* Something in the FCP_RSP was invalid.
5000 * Check conditions */
5001 ret = lpfc_check_fcp_rsp(vport, lpfc_cmd);
5002 else
5003 ret = FAILED;
5004 } else if (status == IOCB_TIMEDOUT) {
5005 ret = TIMEOUT_ERROR;
5006 } else {
5007 ret = FAILED;
5008 }
5009 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
5010 } else
James Smartbbb9d182009-06-10 17:23:16 -04005011 ret = SUCCESS;
5012
5013 lpfc_sli_release_iocbq(phba, iocbqrsp);
5014
5015 if (ret != TIMEOUT_ERROR)
5016 lpfc_release_scsi_buf(phba, lpfc_cmd);
5017
5018 return ret;
5019}
5020
5021/**
5022 * lpfc_chk_tgt_mapped -
5023 * @vport: The virtual port to check on
5024 * @cmnd: Pointer to scsi_cmnd data structure.
5025 *
5026 * This routine delays until the scsi target (aka rport) for the
5027 * command exists (is present and logged in) or we declare it non-existent.
5028 *
5029 * Return code :
5030 * 0x2003 - Error
5031 * 0x2002 - Success
5032 **/
5033static int
5034lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
5035{
James Smart1ba981f2014-02-20 09:56:45 -05005036 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005037 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005038 unsigned long later;
5039
James Smart1ba981f2014-02-20 09:56:45 -05005040 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smart1c6f4ef52009-11-18 15:40:49 -05005041 if (!rdata) {
5042 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5043 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
5044 return FAILED;
5045 }
5046 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005047 /*
5048 * If target is not in a MAPPED state, delay until
5049 * target is rediscovered or devloss timeout expires.
5050 */
5051 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5052 while (time_after(later, jiffies)) {
5053 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
5054 return FAILED;
5055 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
5056 return SUCCESS;
5057 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
James Smart1ba981f2014-02-20 09:56:45 -05005058 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartbbb9d182009-06-10 17:23:16 -04005059 if (!rdata)
5060 return FAILED;
5061 pnode = rdata->pnode;
5062 }
5063 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
5064 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
5065 return FAILED;
5066 return SUCCESS;
5067}
5068
5069/**
5070 * lpfc_reset_flush_io_context -
5071 * @vport: The virtual port (scsi_host) for the flush context
5072 * @tgt_id: If aborting by Target contect - specifies the target id
5073 * @lun_id: If aborting by Lun context - specifies the lun id
5074 * @context: specifies the context level to flush at.
5075 *
5076 * After a reset condition via TMF, we need to flush orphaned i/o
5077 * contexts from the adapter. This routine aborts any contexts
5078 * outstanding, then waits for their completions. The wait is
5079 * bounded by devloss_tmo though.
5080 *
5081 * Return code :
5082 * 0x2003 - Error
5083 * 0x2002 - Success
5084 **/
5085static int
5086lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5087 uint64_t lun_id, lpfc_ctx_cmd context)
5088{
5089 struct lpfc_hba *phba = vport->phba;
5090 unsigned long later;
5091 int cnt;
5092
5093 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5094 if (cnt)
James Smart98912dd2014-04-04 13:52:31 -04005095 lpfc_sli_abort_taskmgmt(vport,
5096 &phba->sli.ring[phba->sli.fcp_ring],
5097 tgt_id, lun_id, context);
James Smartbbb9d182009-06-10 17:23:16 -04005098 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5099 while (time_after(later, jiffies) && cnt) {
5100 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5101 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5102 }
5103 if (cnt) {
5104 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5105 "0724 I/O flush failure for context %s : cnt x%x\n",
5106 ((context == LPFC_CTX_LUN) ? "LUN" :
5107 ((context == LPFC_CTX_TGT) ? "TGT" :
5108 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5109 cnt);
5110 return FAILED;
5111 }
5112 return SUCCESS;
5113}
5114
James Smart9bad7672008-12-04 22:39:02 -05005115/**
James Smart3621a712009-04-06 18:47:14 -04005116 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05005117 * @cmnd: Pointer to scsi_cmnd data structure.
5118 *
James Smartbbb9d182009-06-10 17:23:16 -04005119 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05005120 * command.
5121 *
5122 * Return code :
5123 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04005124 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005125 **/
dea31012005-04-17 16:05:31 -05005126static int
James Smart7054a602007-04-25 09:52:34 -04005127lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005128{
James Smart2e0fef82007-06-17 19:56:36 -05005129 struct Scsi_Host *shost = cmnd->device->host;
5130 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smart1ba981f2014-02-20 09:56:45 -05005131 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005132 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005133 unsigned tgt_id = cmnd->device->id;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005134 uint64_t lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04005135 struct lpfc_scsi_event_header scsi_event;
James Smart53151bb2013-10-10 12:24:07 -04005136 int status;
dea31012005-04-17 16:05:31 -05005137
James Smart1ba981f2014-02-20 09:56:45 -05005138 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smart1c6f4ef52009-11-18 15:40:49 -05005139 if (!rdata) {
5140 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5141 "0798 Device Reset rport failure: rdata x%p\n", rdata);
5142 return FAILED;
5143 }
5144 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005145 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005146 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005147 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005148
5149 status = lpfc_chk_tgt_mapped(vport, cmnd);
5150 if (status == FAILED) {
5151 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5152 "0721 Device Reset rport failure: rdata x%p\n", rdata);
5153 return FAILED;
5154 }
5155
5156 scsi_event.event_type = FC_REG_SCSI_EVENT;
5157 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5158 scsi_event.lun = lun_id;
5159 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5160 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5161
5162 fc_host_post_vendor_event(shost, fc_get_event_number(),
5163 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5164
5165 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5166 FCP_LUN_RESET);
5167
5168 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005169 "0713 SCSI layer issued Device Reset (%d, %llu) "
James Smartbbb9d182009-06-10 17:23:16 -04005170 "return x%x\n", tgt_id, lun_id, status);
5171
dea31012005-04-17 16:05:31 -05005172 /*
James Smartbbb9d182009-06-10 17:23:16 -04005173 * We have to clean up i/o as : they may be orphaned by the TMF;
5174 * or if the TMF failed, they may be in an indeterminate state.
5175 * So, continue on.
5176 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05005177 */
James Smart53151bb2013-10-10 12:24:07 -04005178 if (status == SUCCESS)
5179 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005180 LPFC_CTX_LUN);
James Smart53151bb2013-10-10 12:24:07 -04005181
5182 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005183}
5184
5185/**
5186 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5187 * @cmnd: Pointer to scsi_cmnd data structure.
5188 *
5189 * This routine does a target reset by sending a TARGET_RESET task management
5190 * command.
5191 *
5192 * Return code :
5193 * 0x2003 - Error
5194 * 0x2002 - Success
5195 **/
5196static int
5197lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5198{
5199 struct Scsi_Host *shost = cmnd->device->host;
5200 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smart1ba981f2014-02-20 09:56:45 -05005201 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005202 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005203 unsigned tgt_id = cmnd->device->id;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005204 uint64_t lun_id = cmnd->device->lun;
James Smartbbb9d182009-06-10 17:23:16 -04005205 struct lpfc_scsi_event_header scsi_event;
James Smart53151bb2013-10-10 12:24:07 -04005206 int status;
James Smartbbb9d182009-06-10 17:23:16 -04005207
James Smart1ba981f2014-02-20 09:56:45 -05005208 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smart1c6f4ef52009-11-18 15:40:49 -05005209 if (!rdata) {
5210 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5211 "0799 Target Reset rport failure: rdata x%p\n", rdata);
5212 return FAILED;
5213 }
5214 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005215 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005216 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005217 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005218
5219 status = lpfc_chk_tgt_mapped(vport, cmnd);
5220 if (status == FAILED) {
5221 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5222 "0722 Target Reset rport failure: rdata x%p\n", rdata);
James Smart8c50d252014-09-03 12:58:16 -04005223 spin_lock_irq(shost->host_lock);
5224 pnode->nlp_flag &= ~NLP_NPR_ADISC;
5225 pnode->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
5226 spin_unlock_irq(shost->host_lock);
5227 lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5228 LPFC_CTX_TGT);
5229 return FAST_IO_FAIL;
dea31012005-04-17 16:05:31 -05005230 }
James Smartea2151b2008-09-07 11:52:10 -04005231
5232 scsi_event.event_type = FC_REG_SCSI_EVENT;
5233 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5234 scsi_event.lun = 0;
5235 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5236 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5237
James Smartbbb9d182009-06-10 17:23:16 -04005238 fc_host_post_vendor_event(shost, fc_get_event_number(),
5239 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04005240
James Smartbbb9d182009-06-10 17:23:16 -04005241 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5242 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05005243
James Smarte8b62012007-08-02 11:10:09 -04005244 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005245 "0723 SCSI layer issued Target Reset (%d, %llu) "
James Smartbbb9d182009-06-10 17:23:16 -04005246 "return x%x\n", tgt_id, lun_id, status);
5247
5248 /*
5249 * We have to clean up i/o as : they may be orphaned by the TMF;
5250 * or if the TMF failed, they may be in an indeterminate state.
5251 * So, continue on.
5252 * We will report success if all the i/o aborts successfully.
5253 */
James Smart53151bb2013-10-10 12:24:07 -04005254 if (status == SUCCESS)
5255 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smart3a707302012-06-12 13:54:42 -04005256 LPFC_CTX_TGT);
James Smart53151bb2013-10-10 12:24:07 -04005257 return status;
dea31012005-04-17 16:05:31 -05005258}
5259
James Smart9bad7672008-12-04 22:39:02 -05005260/**
James Smart3621a712009-04-06 18:47:14 -04005261 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05005262 * @cmnd: Pointer to scsi_cmnd data structure.
5263 *
James Smartbbb9d182009-06-10 17:23:16 -04005264 * This routine does target reset to all targets on @cmnd->device->host.
5265 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05005266 *
James Smartbbb9d182009-06-10 17:23:16 -04005267 * Return code :
5268 * 0x2003 - Error
5269 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005270 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04005271static int
James Smart7054a602007-04-25 09:52:34 -04005272lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005273{
James Smart2e0fef82007-06-17 19:56:36 -05005274 struct Scsi_Host *shost = cmnd->device->host;
5275 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005276 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04005277 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04005278 int match;
5279 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04005280
5281 scsi_event.event_type = FC_REG_SCSI_EVENT;
5282 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5283 scsi_event.lun = 0;
5284 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5285 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5286
James Smartbbb9d182009-06-10 17:23:16 -04005287 fc_host_post_vendor_event(shost, fc_get_event_number(),
5288 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05005289
James Smartbf086112011-08-21 21:48:13 -04005290 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005291 if (status != 0 && status != SUCCESS)
James Smartbf086112011-08-21 21:48:13 -04005292 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005293
dea31012005-04-17 16:05:31 -05005294 /*
5295 * Since the driver manages a single bus device, reset all
5296 * targets known to the driver. Should any target reset
5297 * fail, this routine returns failure to the midlayer.
5298 */
James Smarte17da182006-07-06 15:49:25 -04005299 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04005300 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05005301 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05005302 spin_lock_irq(shost->host_lock);
5303 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005304 if (!NLP_CHK_NODE_ACT(ndlp))
5305 continue;
James Smarta6571c62012-10-31 14:44:42 -04005306 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5307 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5308 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005309 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04005310 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04005311 ndlp->rport) {
dea31012005-04-17 16:05:31 -05005312 match = 1;
5313 break;
5314 }
5315 }
James Smart2e0fef82007-06-17 19:56:36 -05005316 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005317 if (!match)
5318 continue;
James Smartbbb9d182009-06-10 17:23:16 -04005319
5320 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
5321 i, 0, FCP_TARGET_RESET);
5322
5323 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04005324 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5325 "0700 Bus Reset on target %d failed\n",
5326 i);
James Smart915caaa2008-06-14 22:52:38 -04005327 ret = FAILED;
dea31012005-04-17 16:05:31 -05005328 }
5329 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005330 /*
James Smartbbb9d182009-06-10 17:23:16 -04005331 * We have to clean up i/o as : they may be orphaned by the TMFs
5332 * above; or if any of the TMFs failed, they may be in an
5333 * indeterminate state.
5334 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005335 */
James Smartbbb9d182009-06-10 17:23:16 -04005336
5337 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5338 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005339 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04005340
James Smarte8b62012007-08-02 11:10:09 -04005341 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5342 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05005343 return ret;
5344}
5345
James Smart9bad7672008-12-04 22:39:02 -05005346/**
James Smart27b01b82012-05-09 21:19:44 -04005347 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
5348 * @cmnd: Pointer to scsi_cmnd data structure.
5349 *
5350 * This routine does host reset to the adaptor port. It brings the HBA
5351 * offline, performs a board restart, and then brings the board back online.
5352 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
5353 * reject all outstanding SCSI commands to the host and error returned
5354 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5355 * of error handling, it will only return error if resetting of the adapter
5356 * is not successful; in all other cases, will return success.
5357 *
5358 * Return code :
5359 * 0x2003 - Error
5360 * 0x2002 - Success
5361 **/
5362static int
5363lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5364{
5365 struct Scsi_Host *shost = cmnd->device->host;
5366 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5367 struct lpfc_hba *phba = vport->phba;
5368 int rc, ret = SUCCESS;
5369
James Smarta88dbb62013-04-17 20:18:39 -04005370 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5371 "3172 SCSI layer issued Host Reset Data:\n");
5372
James Smart618a5232012-06-12 13:54:36 -04005373 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart27b01b82012-05-09 21:19:44 -04005374 lpfc_offline(phba);
5375 rc = lpfc_sli_brdrestart(phba);
5376 if (rc)
5377 ret = FAILED;
James Smarta88dbb62013-04-17 20:18:39 -04005378 rc = lpfc_online(phba);
5379 if (rc)
5380 ret = FAILED;
James Smart27b01b82012-05-09 21:19:44 -04005381 lpfc_unblock_mgmt_io(phba);
5382
James Smarta88dbb62013-04-17 20:18:39 -04005383 if (ret == FAILED) {
5384 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5385 "3323 Failed host reset, bring it offline\n");
5386 lpfc_sli4_offline_eratt(phba);
5387 }
James Smart27b01b82012-05-09 21:19:44 -04005388 return ret;
5389}
5390
5391/**
James Smart3621a712009-04-06 18:47:14 -04005392 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05005393 * @sdev: Pointer to scsi_device.
5394 *
5395 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5396 * globally available list of scsi buffers. This routine also makes sure scsi
5397 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5398 * of scsi buffer exists for the lifetime of the driver.
5399 *
5400 * Return codes:
5401 * non-0 - Error
5402 * 0 - Success
5403 **/
dea31012005-04-17 16:05:31 -05005404static int
dea31012005-04-17 16:05:31 -05005405lpfc_slave_alloc(struct scsi_device *sdev)
5406{
James Smart2e0fef82007-06-17 19:56:36 -05005407 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5408 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005409 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04005410 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05005411 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04005412 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04005413 uint32_t sdev_cnt;
James Smart1ba981f2014-02-20 09:56:45 -05005414 struct lpfc_device_data *device_data;
5415 unsigned long flags;
5416 struct lpfc_name target_wwpn;
dea31012005-04-17 16:05:31 -05005417
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005418 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05005419 return -ENXIO;
5420
James Smartf38fa0b2014-04-04 13:52:21 -04005421 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -05005422
5423 /*
5424 * Check to see if the device data structure for the lun
5425 * exists. If not, create one.
5426 */
5427
5428 u64_to_wwn(rport->port_name, target_wwpn.u.wwn);
5429 spin_lock_irqsave(&phba->devicelock, flags);
5430 device_data = __lpfc_get_device_data(phba,
5431 &phba->luns,
5432 &vport->fc_portname,
5433 &target_wwpn,
5434 sdev->lun);
5435 if (!device_data) {
5436 spin_unlock_irqrestore(&phba->devicelock, flags);
5437 device_data = lpfc_create_device_data(phba,
5438 &vport->fc_portname,
5439 &target_wwpn,
5440 sdev->lun, true);
5441 if (!device_data)
5442 return -ENOMEM;
5443 spin_lock_irqsave(&phba->devicelock, flags);
5444 list_add_tail(&device_data->listentry, &phba->luns);
5445 }
5446 device_data->rport_data = rport->dd_data;
5447 device_data->available = true;
5448 spin_unlock_irqrestore(&phba->devicelock, flags);
5449 sdev->hostdata = device_data;
5450 } else {
5451 sdev->hostdata = rport->dd_data;
5452 }
James Smartd7c47992010-06-08 18:31:54 -04005453 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005454
5455 /*
5456 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5457 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04005458 * HBA limit conveyed to the midlayer via the host structure. The
5459 * formula accounts for the lun_queue_depth + error handlers + 1
5460 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05005461 */
5462 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04005463 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05005464
James Smartd7c47992010-06-08 18:31:54 -04005465 /* If allocated buffers are enough do nothing */
5466 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5467 return 0;
5468
James Smart92d7f7b2007-06-17 19:56:38 -05005469 /* Allow some exchanges to be available always to complete discovery */
5470 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005471 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5472 "0704 At limitation of %d preallocated "
5473 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05005474 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05005475 /* Allow some exchanges to be available always to complete discovery */
5476 } else if (total + num_to_alloc >
5477 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005478 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5479 "0705 Allocation request of %d "
5480 "command buffers will exceed max of %d. "
5481 "Reducing allocation request to %d.\n",
5482 num_to_alloc, phba->cfg_hba_queue_depth,
5483 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05005484 num_to_alloc = phba->cfg_hba_queue_depth - total;
5485 }
James Smart3772a992009-05-22 14:50:54 -04005486 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
5487 if (num_to_alloc != num_allocated) {
James Smart96f70772013-04-17 20:16:15 -04005488 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5489 "0708 Allocation request of %d "
5490 "command buffers did not succeed. "
5491 "Allocated %d buffers.\n",
5492 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05005493 }
James Smart1c6f4ef52009-11-18 15:40:49 -05005494 if (num_allocated > 0)
5495 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05005496 return 0;
5497}
5498
James Smart9bad7672008-12-04 22:39:02 -05005499/**
James Smart3621a712009-04-06 18:47:14 -04005500 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05005501 * @sdev: Pointer to scsi_device.
5502 *
5503 * This routine configures following items
5504 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05005505 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5506 *
5507 * Return codes:
5508 * 0 - Success
5509 **/
dea31012005-04-17 16:05:31 -05005510static int
5511lpfc_slave_configure(struct scsi_device *sdev)
5512{
James Smart2e0fef82007-06-17 19:56:36 -05005513 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5514 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005515
Christoph Hellwigc8b09f62014-11-03 20:15:14 +01005516 scsi_adjust_queue_depth(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005517
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005518 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005519 lpfc_sli_handle_fast_ring_event(phba,
5520 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005521 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5522 lpfc_poll_rearm_timer(phba);
5523 }
5524
dea31012005-04-17 16:05:31 -05005525 return 0;
5526}
5527
James Smart9bad7672008-12-04 22:39:02 -05005528/**
James Smart3621a712009-04-06 18:47:14 -04005529 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05005530 * @sdev: Pointer to scsi_device.
5531 *
5532 * This routine sets @sdev hostatdata filed to null.
5533 **/
dea31012005-04-17 16:05:31 -05005534static void
5535lpfc_slave_destroy(struct scsi_device *sdev)
5536{
James Smartd7c47992010-06-08 18:31:54 -04005537 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5538 struct lpfc_hba *phba = vport->phba;
James Smart1ba981f2014-02-20 09:56:45 -05005539 unsigned long flags;
5540 struct lpfc_device_data *device_data = sdev->hostdata;
5541
James Smartd7c47992010-06-08 18:31:54 -04005542 atomic_dec(&phba->sdev_cnt);
James Smartf38fa0b2014-04-04 13:52:21 -04005543 if ((phba->cfg_fof) && (device_data)) {
James Smart1ba981f2014-02-20 09:56:45 -05005544 spin_lock_irqsave(&phba->devicelock, flags);
5545 device_data->available = false;
5546 if (!device_data->oas_enabled)
5547 lpfc_delete_device_data(phba, device_data);
5548 spin_unlock_irqrestore(&phba->devicelock, flags);
5549 }
dea31012005-04-17 16:05:31 -05005550 sdev->hostdata = NULL;
5551 return;
5552}
5553
James Smart1ba981f2014-02-20 09:56:45 -05005554/**
5555 * lpfc_create_device_data - creates and initializes device data structure for OAS
5556 * @pha: Pointer to host bus adapter structure.
5557 * @vport_wwpn: Pointer to vport's wwpn information
5558 * @target_wwpn: Pointer to target's wwpn information
5559 * @lun: Lun on target
5560 * @atomic_create: Flag to indicate if memory should be allocated using the
5561 * GFP_ATOMIC flag or not.
5562 *
5563 * This routine creates a device data structure which will contain identifying
5564 * information for the device (host wwpn, target wwpn, lun), state of OAS,
5565 * whether or not the corresponding lun is available by the system,
5566 * and pointer to the rport data.
5567 *
5568 * Return codes:
5569 * NULL - Error
5570 * Pointer to lpfc_device_data - Success
5571 **/
5572struct lpfc_device_data*
5573lpfc_create_device_data(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5574 struct lpfc_name *target_wwpn, uint64_t lun,
5575 bool atomic_create)
5576{
5577
5578 struct lpfc_device_data *lun_info;
5579 int memory_flags;
5580
5581 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005582 !(phba->cfg_fof))
James Smart1ba981f2014-02-20 09:56:45 -05005583 return NULL;
5584
5585 /* Attempt to create the device data to contain lun info */
5586
5587 if (atomic_create)
5588 memory_flags = GFP_ATOMIC;
5589 else
5590 memory_flags = GFP_KERNEL;
5591 lun_info = mempool_alloc(phba->device_data_mem_pool, memory_flags);
5592 if (!lun_info)
5593 return NULL;
5594 INIT_LIST_HEAD(&lun_info->listentry);
5595 lun_info->rport_data = NULL;
5596 memcpy(&lun_info->device_id.vport_wwpn, vport_wwpn,
5597 sizeof(struct lpfc_name));
5598 memcpy(&lun_info->device_id.target_wwpn, target_wwpn,
5599 sizeof(struct lpfc_name));
5600 lun_info->device_id.lun = lun;
5601 lun_info->oas_enabled = false;
5602 lun_info->available = false;
5603 return lun_info;
5604}
5605
5606/**
5607 * lpfc_delete_device_data - frees a device data structure for OAS
5608 * @pha: Pointer to host bus adapter structure.
5609 * @lun_info: Pointer to device data structure to free.
5610 *
5611 * This routine frees the previously allocated device data structure passed.
5612 *
5613 **/
5614void
5615lpfc_delete_device_data(struct lpfc_hba *phba,
5616 struct lpfc_device_data *lun_info)
5617{
5618
5619 if (unlikely(!phba) || !lun_info ||
James Smartf38fa0b2014-04-04 13:52:21 -04005620 !(phba->cfg_fof))
James Smart1ba981f2014-02-20 09:56:45 -05005621 return;
5622
5623 if (!list_empty(&lun_info->listentry))
5624 list_del(&lun_info->listentry);
5625 mempool_free(lun_info, phba->device_data_mem_pool);
5626 return;
5627}
5628
5629/**
5630 * __lpfc_get_device_data - returns the device data for the specified lun
5631 * @pha: Pointer to host bus adapter structure.
5632 * @list: Point to list to search.
5633 * @vport_wwpn: Pointer to vport's wwpn information
5634 * @target_wwpn: Pointer to target's wwpn information
5635 * @lun: Lun on target
5636 *
5637 * This routine searches the list passed for the specified lun's device data.
5638 * This function does not hold locks, it is the responsibility of the caller
5639 * to ensure the proper lock is held before calling the function.
5640 *
5641 * Return codes:
5642 * NULL - Error
5643 * Pointer to lpfc_device_data - Success
5644 **/
5645struct lpfc_device_data*
5646__lpfc_get_device_data(struct lpfc_hba *phba, struct list_head *list,
5647 struct lpfc_name *vport_wwpn,
5648 struct lpfc_name *target_wwpn, uint64_t lun)
5649{
5650
5651 struct lpfc_device_data *lun_info;
5652
5653 if (unlikely(!phba) || !list || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005654 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005655 return NULL;
5656
5657 /* Check to see if the lun is already enabled for OAS. */
5658
5659 list_for_each_entry(lun_info, list, listentry) {
5660 if ((memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5661 sizeof(struct lpfc_name)) == 0) &&
5662 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5663 sizeof(struct lpfc_name)) == 0) &&
5664 (lun_info->device_id.lun == lun))
5665 return lun_info;
5666 }
5667
5668 return NULL;
5669}
5670
5671/**
5672 * lpfc_find_next_oas_lun - searches for the next oas lun
5673 * @pha: Pointer to host bus adapter structure.
5674 * @vport_wwpn: Pointer to vport's wwpn information
5675 * @target_wwpn: Pointer to target's wwpn information
5676 * @starting_lun: Pointer to the lun to start searching for
5677 * @found_vport_wwpn: Pointer to the found lun's vport wwpn information
5678 * @found_target_wwpn: Pointer to the found lun's target wwpn information
5679 * @found_lun: Pointer to the found lun.
5680 * @found_lun_status: Pointer to status of the found lun.
5681 *
5682 * This routine searches the luns list for the specified lun
5683 * or the first lun for the vport/target. If the vport wwpn contains
5684 * a zero value then a specific vport is not specified. In this case
5685 * any vport which contains the lun will be considered a match. If the
5686 * target wwpn contains a zero value then a specific target is not specified.
5687 * In this case any target which contains the lun will be considered a
5688 * match. If the lun is found, the lun, vport wwpn, target wwpn and lun status
5689 * are returned. The function will also return the next lun if available.
5690 * If the next lun is not found, starting_lun parameter will be set to
5691 * NO_MORE_OAS_LUN.
5692 *
5693 * Return codes:
5694 * non-0 - Error
5695 * 0 - Success
5696 **/
5697bool
5698lpfc_find_next_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5699 struct lpfc_name *target_wwpn, uint64_t *starting_lun,
5700 struct lpfc_name *found_vport_wwpn,
5701 struct lpfc_name *found_target_wwpn,
5702 uint64_t *found_lun,
5703 uint32_t *found_lun_status)
5704{
5705
5706 unsigned long flags;
5707 struct lpfc_device_data *lun_info;
5708 struct lpfc_device_id *device_id;
5709 uint64_t lun;
5710 bool found = false;
5711
5712 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5713 !starting_lun || !found_vport_wwpn ||
5714 !found_target_wwpn || !found_lun || !found_lun_status ||
5715 (*starting_lun == NO_MORE_OAS_LUN) ||
James Smartf38fa0b2014-04-04 13:52:21 -04005716 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005717 return false;
5718
5719 lun = *starting_lun;
5720 *found_lun = NO_MORE_OAS_LUN;
5721 *starting_lun = NO_MORE_OAS_LUN;
5722
5723 /* Search for lun or the lun closet in value */
5724
5725 spin_lock_irqsave(&phba->devicelock, flags);
5726 list_for_each_entry(lun_info, &phba->luns, listentry) {
5727 if (((wwn_to_u64(vport_wwpn->u.wwn) == 0) ||
5728 (memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5729 sizeof(struct lpfc_name)) == 0)) &&
5730 ((wwn_to_u64(target_wwpn->u.wwn) == 0) ||
5731 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5732 sizeof(struct lpfc_name)) == 0)) &&
5733 (lun_info->oas_enabled)) {
5734 device_id = &lun_info->device_id;
5735 if ((!found) &&
5736 ((lun == FIND_FIRST_OAS_LUN) ||
5737 (device_id->lun == lun))) {
5738 *found_lun = device_id->lun;
5739 memcpy(found_vport_wwpn,
5740 &device_id->vport_wwpn,
5741 sizeof(struct lpfc_name));
5742 memcpy(found_target_wwpn,
5743 &device_id->target_wwpn,
5744 sizeof(struct lpfc_name));
5745 if (lun_info->available)
5746 *found_lun_status =
5747 OAS_LUN_STATUS_EXISTS;
5748 else
5749 *found_lun_status = 0;
5750 if (phba->cfg_oas_flags & OAS_FIND_ANY_VPORT)
5751 memset(vport_wwpn, 0x0,
5752 sizeof(struct lpfc_name));
5753 if (phba->cfg_oas_flags & OAS_FIND_ANY_TARGET)
5754 memset(target_wwpn, 0x0,
5755 sizeof(struct lpfc_name));
5756 found = true;
5757 } else if (found) {
5758 *starting_lun = device_id->lun;
5759 memcpy(vport_wwpn, &device_id->vport_wwpn,
5760 sizeof(struct lpfc_name));
5761 memcpy(target_wwpn, &device_id->target_wwpn,
5762 sizeof(struct lpfc_name));
5763 break;
5764 }
5765 }
5766 }
5767 spin_unlock_irqrestore(&phba->devicelock, flags);
5768 return found;
5769}
5770
5771/**
5772 * lpfc_enable_oas_lun - enables a lun for OAS operations
5773 * @pha: Pointer to host bus adapter structure.
5774 * @vport_wwpn: Pointer to vport's wwpn information
5775 * @target_wwpn: Pointer to target's wwpn information
5776 * @lun: Lun
5777 *
5778 * This routine enables a lun for oas operations. The routines does so by
5779 * doing the following :
5780 *
5781 * 1) Checks to see if the device data for the lun has been created.
5782 * 2) If found, sets the OAS enabled flag if not set and returns.
5783 * 3) Otherwise, creates a device data structure.
5784 * 4) If successfully created, indicates the device data is for an OAS lun,
5785 * indicates the lun is not available and add to the list of luns.
5786 *
5787 * Return codes:
5788 * false - Error
5789 * true - Success
5790 **/
5791bool
5792lpfc_enable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5793 struct lpfc_name *target_wwpn, uint64_t lun)
5794{
5795
5796 struct lpfc_device_data *lun_info;
5797 unsigned long flags;
5798
5799 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005800 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005801 return false;
5802
5803 spin_lock_irqsave(&phba->devicelock, flags);
5804
5805 /* Check to see if the device data for the lun has been created */
5806 lun_info = __lpfc_get_device_data(phba, &phba->luns, vport_wwpn,
5807 target_wwpn, lun);
5808 if (lun_info) {
5809 if (!lun_info->oas_enabled)
5810 lun_info->oas_enabled = true;
5811 spin_unlock_irqrestore(&phba->devicelock, flags);
5812 return true;
5813 }
5814
5815 /* Create an lun info structure and add to list of luns */
5816 lun_info = lpfc_create_device_data(phba, vport_wwpn, target_wwpn, lun,
5817 false);
5818 if (lun_info) {
5819 lun_info->oas_enabled = true;
5820 lun_info->available = false;
5821 list_add_tail(&lun_info->listentry, &phba->luns);
5822 spin_unlock_irqrestore(&phba->devicelock, flags);
5823 return true;
5824 }
5825 spin_unlock_irqrestore(&phba->devicelock, flags);
5826 return false;
5827}
5828
5829/**
5830 * lpfc_disable_oas_lun - disables 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 disables 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 is created.
5840 * 2) If present, clears the flag indicating this lun is for OAS.
5841 * 3) If the lun is not available by the system, the device data is
5842 * freed.
5843 *
5844 * Return codes:
5845 * false - Error
5846 * true - Success
5847 **/
5848bool
5849lpfc_disable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5850 struct lpfc_name *target_wwpn, uint64_t lun)
5851{
5852
5853 struct lpfc_device_data *lun_info;
5854 unsigned long flags;
5855
5856 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005857 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005858 return false;
5859
5860 spin_lock_irqsave(&phba->devicelock, flags);
5861
5862 /* Check to see if the lun is available. */
5863 lun_info = __lpfc_get_device_data(phba,
5864 &phba->luns, vport_wwpn,
5865 target_wwpn, lun);
5866 if (lun_info) {
5867 lun_info->oas_enabled = false;
5868 if (!lun_info->available)
5869 lpfc_delete_device_data(phba, lun_info);
5870 spin_unlock_irqrestore(&phba->devicelock, flags);
5871 return true;
5872 }
5873
5874 spin_unlock_irqrestore(&phba->devicelock, flags);
5875 return false;
5876}
James Smart92d7f7b2007-06-17 19:56:38 -05005877
dea31012005-04-17 16:05:31 -05005878struct scsi_host_template lpfc_template = {
5879 .module = THIS_MODULE,
5880 .name = LPFC_DRIVER_NAME,
5881 .info = lpfc_info,
5882 .queuecommand = lpfc_queuecommand,
5883 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005884 .eh_device_reset_handler = lpfc_device_reset_handler,
5885 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04005886 .eh_bus_reset_handler = lpfc_bus_reset_handler,
James Smart27b01b82012-05-09 21:19:44 -04005887 .eh_host_reset_handler = lpfc_host_reset_handler,
dea31012005-04-17 16:05:31 -05005888 .slave_alloc = lpfc_slave_alloc,
5889 .slave_configure = lpfc_slave_configure,
5890 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04005891 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05005892 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005893 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05005894 .cmd_per_lun = LPFC_CMD_PER_LUN,
5895 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05005896 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04005897 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04005898 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05005899 .change_queue_depth = lpfc_change_queue_depth,
Christoph Hellwiga62182f2014-10-02 14:39:55 +02005900 .change_queue_type = scsi_change_queue_type,
Christoph Hellwig2ecb2042014-11-03 14:09:02 +01005901 .use_blk_tags = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01005902 .track_queue_depth = 1,
dea31012005-04-17 16:05:31 -05005903};
James Smart3de2a652007-08-02 11:09:59 -04005904
5905struct scsi_host_template lpfc_vport_template = {
5906 .module = THIS_MODULE,
5907 .name = LPFC_DRIVER_NAME,
5908 .info = lpfc_info,
5909 .queuecommand = lpfc_queuecommand,
5910 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005911 .eh_device_reset_handler = lpfc_device_reset_handler,
5912 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04005913 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5914 .slave_alloc = lpfc_slave_alloc,
5915 .slave_configure = lpfc_slave_configure,
5916 .slave_destroy = lpfc_slave_destroy,
5917 .scan_finished = lpfc_scan_finished,
5918 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005919 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04005920 .cmd_per_lun = LPFC_CMD_PER_LUN,
5921 .use_clustering = ENABLE_CLUSTERING,
5922 .shost_attrs = lpfc_vport_attrs,
5923 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05005924 .change_queue_depth = lpfc_change_queue_depth,
Christoph Hellwiga62182f2014-10-02 14:39:55 +02005925 .change_queue_type = scsi_change_queue_type,
Christoph Hellwig2ecb2042014-11-03 14:09:02 +01005926 .use_blk_tags = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01005927 .track_queue_depth = 1,
James Smart3de2a652007-08-02 11:09:59 -04005928};