blob: 4a150063fb4d1b568e6c0a01ddda0579f91507b0 [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 Smart3621a712009-04-06 18:47:14 -0400246 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
James Smartea2151b2008-09-07 11:52:10 -0400247 * @phba: Pointer to HBA context object.
248 * @vport: Pointer to vport object.
249 * @ndlp: Pointer to FC node associated with the target.
250 * @lun: Lun number of the scsi device.
251 * @old_val: Old value of the queue depth.
252 * @new_val: New value of the queue depth.
253 *
254 * This function sends an event to the mgmt application indicating
255 * there is a change in the scsi device queue depth.
256 **/
257static void
258lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
259 struct lpfc_vport *vport,
260 struct lpfc_nodelist *ndlp,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +0200261 uint64_t lun,
James Smartea2151b2008-09-07 11:52:10 -0400262 uint32_t old_val,
263 uint32_t new_val)
264{
265 struct lpfc_fast_path_event *fast_path_evt;
266 unsigned long flags;
267
268 fast_path_evt = lpfc_alloc_fast_evt(phba);
269 if (!fast_path_evt)
270 return;
271
272 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
273 FC_REG_SCSI_EVENT;
274 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
275 LPFC_EVENT_VARQUEDEPTH;
276
277 /* Report all luns with change in queue depth */
278 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
279 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
280 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
281 &ndlp->nlp_portname, sizeof(struct lpfc_name));
282 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
283 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
284 }
285
286 fast_path_evt->un.queue_depth_evt.oldval = old_val;
287 fast_path_evt->un.queue_depth_evt.newval = new_val;
288 fast_path_evt->vport = vport;
289
290 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
291 spin_lock_irqsave(&phba->hbalock, flags);
292 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
293 spin_unlock_irqrestore(&phba->hbalock, flags);
294 lpfc_worker_wake_up(phba);
295
296 return;
297}
298
James Smart9bad7672008-12-04 22:39:02 -0500299/**
James Smart5ffc2662009-11-18 15:39:44 -0500300 * lpfc_change_queue_depth - Alter scsi device queue depth
301 * @sdev: Pointer the scsi device on which to change the queue depth.
302 * @qdepth: New queue depth to set the sdev to.
303 * @reason: The reason for the queue depth change.
304 *
305 * This function is called by the midlayer and the LLD to alter the queue
306 * depth for a scsi device. This function sets the queue depth to the new
307 * value and sends an event out to log the queue depth change.
308 **/
Rashika Kheria7bfe7812014-09-03 12:55:36 -0400309static int
James Smart5ffc2662009-11-18 15:39:44 -0500310lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
311{
312 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
313 struct lpfc_hba *phba = vport->phba;
314 struct lpfc_rport_data *rdata;
315 unsigned long new_queue_depth, old_queue_depth;
316
317 old_queue_depth = sdev->queue_depth;
James Smart6ff85562014-02-20 09:57:08 -0500318
319 switch (reason) {
320 case SCSI_QDEPTH_DEFAULT:
321 /* change request from sysfs, fall through */
322 case SCSI_QDEPTH_RAMP_UP:
323 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
324 break;
325 case SCSI_QDEPTH_QFULL:
326 if (scsi_track_queue_full(sdev, qdepth) == 0)
327 return sdev->queue_depth;
328
329 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
330 "0711 detected queue full - lun queue "
331 "depth adjusted to %d.\n", sdev->queue_depth);
332 break;
333 default:
334 return -EOPNOTSUPP;
335 }
336
James Smart5ffc2662009-11-18 15:39:44 -0500337 new_queue_depth = sdev->queue_depth;
James Smart1ba981f2014-02-20 09:56:45 -0500338 rdata = lpfc_rport_data_from_scsi_device(sdev);
James Smart5ffc2662009-11-18 15:39:44 -0500339 if (rdata)
340 lpfc_send_sdev_queuedepth_change_event(phba, vport,
341 rdata->pnode, sdev->lun,
342 old_queue_depth,
343 new_queue_depth);
344 return sdev->queue_depth;
345}
346
347/**
James Smart3621a712009-04-06 18:47:14 -0400348 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500349 * @phba: The Hba for which this call is being executed.
350 *
351 * This routine is called when there is resource error in driver or firmware.
352 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
353 * posts at most 1 event each second. This routine wakes up worker thread of
354 * @phba to process WORKER_RAM_DOWN_EVENT event.
355 *
356 * This routine should be called with no lock held.
357 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500358void
James Smarteaf15d52008-12-04 22:39:29 -0500359lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500360{
361 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400362 uint32_t evt_posted;
Manuel Schölling0d4aec12014-09-03 12:55:58 -0400363 unsigned long expires;
James Smart92d7f7b2007-06-17 19:56:38 -0500364
365 spin_lock_irqsave(&phba->hbalock, flags);
366 atomic_inc(&phba->num_rsrc_err);
367 phba->last_rsrc_error_time = jiffies;
368
Manuel Schölling0d4aec12014-09-03 12:55:58 -0400369 expires = phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL;
370 if (time_after(expires, jiffies)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500371 spin_unlock_irqrestore(&phba->hbalock, flags);
372 return;
373 }
374
375 phba->last_ramp_down_time = jiffies;
376
377 spin_unlock_irqrestore(&phba->hbalock, flags);
378
379 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400380 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
381 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500382 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500383 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
384
James Smart5e9d9b82008-06-14 22:52:53 -0400385 if (!evt_posted)
386 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500387 return;
388}
389
James Smart9bad7672008-12-04 22:39:02 -0500390/**
James Smart3621a712009-04-06 18:47:14 -0400391 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500392 * @phba: The Hba for which this call is being executed.
393 *
394 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
395 * thread.This routine reduces queue depth for all scsi device on each vport
396 * associated with @phba.
397 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500398void
399lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
400{
James Smart549e55c2007-08-02 11:09:51 -0400401 struct lpfc_vport **vports;
402 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500403 struct scsi_device *sdev;
James Smart5ffc2662009-11-18 15:39:44 -0500404 unsigned long new_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500405 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400406 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500407
408 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
409 num_cmd_success = atomic_read(&phba->num_cmd_success);
410
James Smart75ad83a2012-05-09 21:18:40 -0400411 /*
412 * The error and success command counters are global per
413 * driver instance. If another handler has already
414 * operated on this error event, just exit.
415 */
416 if (num_rsrc_err == 0)
417 return;
418
James Smart549e55c2007-08-02 11:09:51 -0400419 vports = lpfc_create_vport_work_array(phba);
420 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400421 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400422 shost = lpfc_shost_from_vport(vports[i]);
423 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500424 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400425 sdev->queue_depth * num_rsrc_err /
426 (num_rsrc_err + num_cmd_success);
427 if (!new_queue_depth)
428 new_queue_depth = sdev->queue_depth - 1;
429 else
430 new_queue_depth = sdev->queue_depth -
431 new_queue_depth;
James Smart5ffc2662009-11-18 15:39:44 -0500432 lpfc_change_queue_depth(sdev, new_queue_depth,
433 SCSI_QDEPTH_DEFAULT);
James Smart549e55c2007-08-02 11:09:51 -0400434 }
James Smart92d7f7b2007-06-17 19:56:38 -0500435 }
James Smart09372822008-01-11 01:52:54 -0500436 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500437 atomic_set(&phba->num_rsrc_err, 0);
438 atomic_set(&phba->num_cmd_success, 0);
439}
440
James Smart9bad7672008-12-04 22:39:02 -0500441/**
James Smart3621a712009-04-06 18:47:14 -0400442 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400443 * @phba: Pointer to HBA context object.
444 *
445 * This function walks vport list and set each SCSI host to block state
446 * by invoking fc_remote_port_delete() routine. This function is invoked
447 * with EEH when device's PCI slot has been permanently disabled.
448 **/
449void
450lpfc_scsi_dev_block(struct lpfc_hba *phba)
451{
452 struct lpfc_vport **vports;
453 struct Scsi_Host *shost;
454 struct scsi_device *sdev;
455 struct fc_rport *rport;
456 int i;
457
458 vports = lpfc_create_vport_work_array(phba);
459 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400460 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400461 shost = lpfc_shost_from_vport(vports[i]);
462 shost_for_each_device(sdev, shost) {
463 rport = starget_to_rport(scsi_target(sdev));
464 fc_remote_port_delete(rport);
465 }
466 }
467 lpfc_destroy_vport_work_array(phba, vports);
468}
469
James Smart9bad7672008-12-04 22:39:02 -0500470/**
James Smart3772a992009-05-22 14:50:54 -0400471 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500472 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400473 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500474 *
James Smart3772a992009-05-22 14:50:54 -0400475 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
476 * the scsi buffer contains all the necessary information needed to initiate
477 * a SCSI I/O. The non-DMAable buffer region contains information to build
478 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
479 * and the initial BPL. In addition to allocating memory, the FCP CMND and
480 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500481 *
482 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400483 * int - number of scsi buffers that were allocated.
484 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500485 **/
James Smart3772a992009-05-22 14:50:54 -0400486static int
487lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500488{
James Smart2e0fef82007-06-17 19:56:36 -0500489 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500490 struct lpfc_scsi_buf *psb;
491 struct ulp_bde64 *bpl;
492 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400493 dma_addr_t pdma_phys_fcp_cmd;
494 dma_addr_t pdma_phys_fcp_rsp;
495 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500496 uint16_t iotag;
James Smart96f70772013-04-17 20:16:15 -0400497 int bcnt, bpl_size;
498
499 bpl_size = phba->cfg_sg_dma_buf_size -
500 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
501
502 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
503 "9067 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
504 num_to_alloc, phba->cfg_sg_dma_buf_size,
505 (int)sizeof(struct fcp_cmnd),
506 (int)sizeof(struct fcp_rsp), bpl_size);
dea31012005-04-17 16:05:31 -0500507
James Smart3772a992009-05-22 14:50:54 -0400508 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
509 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
510 if (!psb)
511 break;
dea31012005-04-17 16:05:31 -0500512
James Smart3772a992009-05-22 14:50:54 -0400513 /*
514 * Get memory from the pci pool to map the virt space to pci
515 * bus space for an I/O. The DMA buffer includes space for the
516 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
517 * necessary to support the sg_tablesize.
518 */
519 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
520 GFP_KERNEL, &psb->dma_handle);
521 if (!psb->data) {
522 kfree(psb);
523 break;
524 }
dea31012005-04-17 16:05:31 -0500525
James Smart3772a992009-05-22 14:50:54 -0400526 /* Initialize virtual ptrs to dma_buf region. */
527 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
dea31012005-04-17 16:05:31 -0500528
James Smart3772a992009-05-22 14:50:54 -0400529 /* Allocate iotag for psb->cur_iocbq. */
530 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
531 if (iotag == 0) {
532 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
533 psb->data, psb->dma_handle);
534 kfree(psb);
535 break;
536 }
537 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500538
James Smart3772a992009-05-22 14:50:54 -0400539 psb->fcp_cmnd = psb->data;
540 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
541 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400542 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500543
James Smart3772a992009-05-22 14:50:54 -0400544 /* Initialize local short-hand pointers. */
545 bpl = psb->fcp_bpl;
546 pdma_phys_fcp_cmd = psb->dma_handle;
547 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
548 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
549 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500550
James Smart3772a992009-05-22 14:50:54 -0400551 /*
552 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
553 * are sg list bdes. Initialize the first two and leave the
554 * rest for queuecommand.
555 */
556 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
557 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
558 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
559 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
560 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500561
James Smart3772a992009-05-22 14:50:54 -0400562 /* Setup the physical region for the FCP RSP */
563 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
564 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
565 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
566 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
567 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
568
569 /*
570 * Since the IOCB for the FCP I/O is built into this
571 * lpfc_scsi_buf, initialize it with all known data now.
572 */
573 iocb = &psb->cur_iocbq.iocb;
574 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
575 if ((phba->sli_rev == 3) &&
576 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
577 /* fill in immediate fcp command BDE */
578 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
579 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
580 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
581 unsli3.fcp_ext.icd);
582 iocb->un.fcpi64.bdl.addrHigh = 0;
583 iocb->ulpBdeCount = 0;
584 iocb->ulpLe = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300585 /* fill in response BDE */
James Smart3772a992009-05-22 14:50:54 -0400586 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
587 BUFF_TYPE_BDE_64;
588 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
589 sizeof(struct fcp_rsp);
590 iocb->unsli3.fcp_ext.rbde.addrLow =
591 putPaddrLow(pdma_phys_fcp_rsp);
592 iocb->unsli3.fcp_ext.rbde.addrHigh =
593 putPaddrHigh(pdma_phys_fcp_rsp);
594 } else {
595 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
596 iocb->un.fcpi64.bdl.bdeSize =
597 (2 * sizeof(struct ulp_bde64));
598 iocb->un.fcpi64.bdl.addrLow =
599 putPaddrLow(pdma_phys_bpl);
600 iocb->un.fcpi64.bdl.addrHigh =
601 putPaddrHigh(pdma_phys_bpl);
602 iocb->ulpBdeCount = 1;
603 iocb->ulpLe = 1;
604 }
605 iocb->ulpClass = CLASS3;
606 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400607 /* Put it back into the SCSI buffer list */
James Smarteee88772010-09-29 11:19:08 -0400608 psb->cur_iocbq.context1 = psb;
James Smart1c6f4ef52009-11-18 15:40:49 -0500609 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400610
James Smart34b02dc2008-08-24 21:49:55 -0400611 }
dea31012005-04-17 16:05:31 -0500612
James Smart3772a992009-05-22 14:50:54 -0400613 return bcnt;
dea31012005-04-17 16:05:31 -0500614}
615
James Smart9bad7672008-12-04 22:39:02 -0500616/**
James Smart1151e3e2011-02-16 12:39:35 -0500617 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
618 * @vport: pointer to lpfc vport data structure.
619 *
620 * This routine is invoked by the vport cleanup for deletions and the cleanup
621 * for an ndlp on removal.
622 **/
623void
624lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
625{
626 struct lpfc_hba *phba = vport->phba;
627 struct lpfc_scsi_buf *psb, *next_psb;
628 unsigned long iflag = 0;
629
630 spin_lock_irqsave(&phba->hbalock, iflag);
631 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
632 list_for_each_entry_safe(psb, next_psb,
633 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
634 if (psb->rdata && psb->rdata->pnode
635 && psb->rdata->pnode->vport == vport)
636 psb->rdata = NULL;
637 }
638 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
639 spin_unlock_irqrestore(&phba->hbalock, iflag);
640}
641
642/**
James Smartda0436e2009-05-22 14:51:39 -0400643 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
644 * @phba: pointer to lpfc hba data structure.
645 * @axri: pointer to the fcp xri abort wcqe structure.
646 *
647 * This routine is invoked by the worker thread to process a SLI4 fast-path
648 * FCP aborted xri.
649 **/
650void
651lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
652 struct sli4_wcqe_xri_aborted *axri)
653{
654 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500655 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartda0436e2009-05-22 14:51:39 -0400656 struct lpfc_scsi_buf *psb, *next_psb;
657 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500658 struct lpfc_iocbq *iocbq;
659 int i;
James Smart19ca7602010-11-20 23:11:55 -0500660 struct lpfc_nodelist *ndlp;
661 int rrq_empty = 0;
James Smart589a52d2010-07-14 15:30:54 -0400662 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smartda0436e2009-05-22 14:51:39 -0400663
James Smart0f65ff62010-02-26 14:14:23 -0500664 spin_lock_irqsave(&phba->hbalock, iflag);
665 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400666 list_for_each_entry_safe(psb, next_psb,
667 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
668 if (psb->cur_iocbq.sli4_xritag == xri) {
669 list_del(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500670 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400671 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500672 spin_unlock(
673 &phba->sli4_hba.abts_scsi_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500674 if (psb->rdata && psb->rdata->pnode)
675 ndlp = psb->rdata->pnode;
676 else
677 ndlp = NULL;
678
James Smart19ca7602010-11-20 23:11:55 -0500679 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500680 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartcb69f7d2011-12-13 13:21:57 -0500681 if (ndlp) {
James Smartee0f4fe2012-05-09 21:19:14 -0400682 lpfc_set_rrq_active(phba, ndlp,
683 psb->cur_iocbq.sli4_lxritag, rxid, 1);
James Smartcb69f7d2011-12-13 13:21:57 -0500684 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
685 }
James Smartda0436e2009-05-22 14:51:39 -0400686 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500687 if (rrq_empty)
688 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400689 return;
690 }
691 }
James Smart0f65ff62010-02-26 14:14:23 -0500692 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
693 for (i = 1; i <= phba->sli.last_iotag; i++) {
694 iocbq = phba->sli.iocbq_lookup[i];
695
696 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
697 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
698 continue;
699 if (iocbq->sli4_xritag != xri)
700 continue;
701 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
702 psb->exch_busy = 0;
703 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart0e9bb8d2013-03-01 16:35:12 -0500704 if (!list_empty(&pring->txq))
James Smart589a52d2010-07-14 15:30:54 -0400705 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500706 return;
707
708 }
709 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400710}
711
712/**
James Smart8a9d2e82012-05-09 21:16:12 -0400713 * lpfc_sli4_post_scsi_sgl_list - Psot blocks of scsi buffer sgls from a list
714 * @phba: pointer to lpfc hba data structure.
715 * @post_sblist: pointer to the scsi buffer list.
716 *
717 * This routine walks a list of scsi buffers that was passed in. It attempts
718 * to construct blocks of scsi buffer sgls which contains contiguous xris and
719 * uses the non-embedded SGL block post mailbox commands to post to the port.
720 * For single SCSI buffer sgl with non-contiguous xri, if any, it shall use
721 * embedded SGL post mailbox command for posting. The @post_sblist passed in
722 * must be local list, thus no lock is needed when manipulate the list.
723 *
724 * Returns: 0 = failure, non-zero number of successfully posted buffers.
725 **/
Rashika Kheria7bfe7812014-09-03 12:55:36 -0400726static int
James Smart8a9d2e82012-05-09 21:16:12 -0400727lpfc_sli4_post_scsi_sgl_list(struct lpfc_hba *phba,
728 struct list_head *post_sblist, int sb_count)
729{
730 struct lpfc_scsi_buf *psb, *psb_next;
James Smart96f70772013-04-17 20:16:15 -0400731 int status, sgl_size;
James Smart8a9d2e82012-05-09 21:16:12 -0400732 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
733 dma_addr_t pdma_phys_bpl1;
734 int last_xritag = NO_XRI;
735 LIST_HEAD(prep_sblist);
736 LIST_HEAD(blck_sblist);
737 LIST_HEAD(scsi_sblist);
738
739 /* sanity check */
740 if (sb_count <= 0)
741 return -EINVAL;
742
James Smart96f70772013-04-17 20:16:15 -0400743 sgl_size = phba->cfg_sg_dma_buf_size -
744 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
745
James Smart8a9d2e82012-05-09 21:16:12 -0400746 list_for_each_entry_safe(psb, psb_next, post_sblist, list) {
747 list_del_init(&psb->list);
748 block_cnt++;
749 if ((last_xritag != NO_XRI) &&
750 (psb->cur_iocbq.sli4_xritag != last_xritag + 1)) {
751 /* a hole in xri block, form a sgl posting block */
752 list_splice_init(&prep_sblist, &blck_sblist);
753 post_cnt = block_cnt - 1;
754 /* prepare list for next posting block */
755 list_add_tail(&psb->list, &prep_sblist);
756 block_cnt = 1;
757 } else {
758 /* prepare list for next posting block */
759 list_add_tail(&psb->list, &prep_sblist);
760 /* enough sgls for non-embed sgl mbox command */
761 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
762 list_splice_init(&prep_sblist, &blck_sblist);
763 post_cnt = block_cnt;
764 block_cnt = 0;
765 }
766 }
767 num_posting++;
768 last_xritag = psb->cur_iocbq.sli4_xritag;
769
770 /* end of repost sgl list condition for SCSI buffers */
771 if (num_posting == sb_count) {
772 if (post_cnt == 0) {
773 /* last sgl posting block */
774 list_splice_init(&prep_sblist, &blck_sblist);
775 post_cnt = block_cnt;
776 } else if (block_cnt == 1) {
777 /* last single sgl with non-contiguous xri */
James Smart96f70772013-04-17 20:16:15 -0400778 if (sgl_size > SGL_PAGE_SIZE)
James Smart8a9d2e82012-05-09 21:16:12 -0400779 pdma_phys_bpl1 = psb->dma_phys_bpl +
780 SGL_PAGE_SIZE;
781 else
782 pdma_phys_bpl1 = 0;
783 status = lpfc_sli4_post_sgl(phba,
784 psb->dma_phys_bpl,
785 pdma_phys_bpl1,
786 psb->cur_iocbq.sli4_xritag);
787 if (status) {
788 /* failure, put on abort scsi list */
789 psb->exch_busy = 1;
790 } else {
791 /* success, put on SCSI buffer list */
792 psb->exch_busy = 0;
793 psb->status = IOSTAT_SUCCESS;
794 num_posted++;
795 }
796 /* success, put on SCSI buffer sgl list */
797 list_add_tail(&psb->list, &scsi_sblist);
798 }
799 }
800
801 /* continue until a nembed page worth of sgls */
802 if (post_cnt == 0)
803 continue;
804
805 /* post block of SCSI buffer list sgls */
806 status = lpfc_sli4_post_scsi_sgl_block(phba, &blck_sblist,
807 post_cnt);
808
809 /* don't reset xirtag due to hole in xri block */
810 if (block_cnt == 0)
811 last_xritag = NO_XRI;
812
813 /* reset SCSI buffer post count for next round of posting */
814 post_cnt = 0;
815
816 /* put posted SCSI buffer-sgl posted on SCSI buffer sgl list */
817 while (!list_empty(&blck_sblist)) {
818 list_remove_head(&blck_sblist, psb,
819 struct lpfc_scsi_buf, list);
820 if (status) {
821 /* failure, put on abort scsi list */
822 psb->exch_busy = 1;
823 } else {
824 /* success, put on SCSI buffer list */
825 psb->exch_busy = 0;
826 psb->status = IOSTAT_SUCCESS;
827 num_posted++;
828 }
829 list_add_tail(&psb->list, &scsi_sblist);
830 }
831 }
832 /* Push SCSI buffers with sgl posted to the availble list */
833 while (!list_empty(&scsi_sblist)) {
834 list_remove_head(&scsi_sblist, psb,
835 struct lpfc_scsi_buf, list);
836 lpfc_release_scsi_buf_s4(phba, psb);
837 }
838 return num_posted;
839}
840
841/**
842 * lpfc_sli4_repost_scsi_sgl_list - Repsot all the allocated scsi buffer sgls
James Smartda0436e2009-05-22 14:51:39 -0400843 * @phba: pointer to lpfc hba data structure.
844 *
845 * This routine walks the list of scsi buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -0400846 * repost them to the port by using SGL block post. This is needed after a
James Smartda0436e2009-05-22 14:51:39 -0400847 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
848 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
849 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
850 *
851 * Returns: 0 = success, non-zero failure.
852 **/
853int
854lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
855{
James Smart8a9d2e82012-05-09 21:16:12 -0400856 LIST_HEAD(post_sblist);
857 int num_posted, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -0400858
James Smart8a9d2e82012-05-09 21:16:12 -0400859 /* get all SCSI buffers need to repost to a local list */
James Smarta40fc5f2013-04-17 20:17:40 -0400860 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -0400861 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400862 list_splice_init(&phba->lpfc_scsi_buf_list_get, &post_sblist);
863 list_splice(&phba->lpfc_scsi_buf_list_put, &post_sblist);
James Smart164cecd2013-09-06 12:22:38 -0400864 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400865 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smartda0436e2009-05-22 14:51:39 -0400866
James Smart8a9d2e82012-05-09 21:16:12 -0400867 /* post the list of scsi buffer sgls to port if available */
868 if (!list_empty(&post_sblist)) {
869 num_posted = lpfc_sli4_post_scsi_sgl_list(phba, &post_sblist,
870 phba->sli4_hba.scsi_xri_cnt);
871 /* failed to post any scsi buffer, return error */
872 if (num_posted == 0)
873 rc = -EIO;
James Smartda0436e2009-05-22 14:51:39 -0400874 }
875 return rc;
876}
877
878/**
879 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
880 * @vport: The virtual port for which this call being executed.
881 * @num_to_allocate: The requested number of buffers to allocate.
882 *
James Smart8a9d2e82012-05-09 21:16:12 -0400883 * This routine allocates scsi buffers for device with SLI-4 interface spec,
James Smartda0436e2009-05-22 14:51:39 -0400884 * the scsi buffer contains all the necessary information needed to initiate
James Smart8a9d2e82012-05-09 21:16:12 -0400885 * a SCSI I/O. After allocating up to @num_to_allocate SCSI buffers and put
886 * them on a list, it post them to the port by using SGL block post.
James Smartda0436e2009-05-22 14:51:39 -0400887 *
888 * Return codes:
James Smart8a9d2e82012-05-09 21:16:12 -0400889 * int - number of scsi buffers that were allocated and posted.
James Smartda0436e2009-05-22 14:51:39 -0400890 * 0 = failure, less than num_to_alloc is a partial failure.
891 **/
892static int
893lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
894{
895 struct lpfc_hba *phba = vport->phba;
896 struct lpfc_scsi_buf *psb;
897 struct sli4_sge *sgl;
898 IOCB_t *iocb;
899 dma_addr_t pdma_phys_fcp_cmd;
900 dma_addr_t pdma_phys_fcp_rsp;
James Smart96f70772013-04-17 20:16:15 -0400901 dma_addr_t pdma_phys_bpl;
James Smart8a9d2e82012-05-09 21:16:12 -0400902 uint16_t iotag, lxri = 0;
James Smart96f70772013-04-17 20:16:15 -0400903 int bcnt, num_posted, sgl_size;
James Smart8a9d2e82012-05-09 21:16:12 -0400904 LIST_HEAD(prep_sblist);
905 LIST_HEAD(post_sblist);
906 LIST_HEAD(scsi_sblist);
James Smartda0436e2009-05-22 14:51:39 -0400907
James Smart96f70772013-04-17 20:16:15 -0400908 sgl_size = phba->cfg_sg_dma_buf_size -
909 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
910
911 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
912 "9068 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
913 num_to_alloc, phba->cfg_sg_dma_buf_size, sgl_size,
914 (int)sizeof(struct fcp_cmnd),
915 (int)sizeof(struct fcp_rsp));
916
James Smartda0436e2009-05-22 14:51:39 -0400917 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
918 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
919 if (!psb)
920 break;
James Smartda0436e2009-05-22 14:51:39 -0400921 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400922 * Get memory from the pci pool to map the virt space to
923 * pci bus space for an I/O. The DMA buffer includes space
924 * for the struct fcp_cmnd, struct fcp_rsp and the number
925 * of bde's necessary to support the sg_tablesize.
James Smartda0436e2009-05-22 14:51:39 -0400926 */
927 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
928 GFP_KERNEL, &psb->dma_handle);
929 if (!psb->data) {
930 kfree(psb);
931 break;
932 }
James Smartda0436e2009-05-22 14:51:39 -0400933 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
934
James Smart092cb032013-09-06 12:21:50 -0400935 /*
936 * 4K Page alignment is CRITICAL to BlockGuard, double check
937 * to be sure.
938 */
939 if (phba->cfg_enable_bg && (((unsigned long)(psb->data) &
940 (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0)) {
James Smart96f70772013-04-17 20:16:15 -0400941 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
942 psb->data, psb->dma_handle);
943 kfree(psb);
944 break;
945 }
946
James Smartda0436e2009-05-22 14:51:39 -0400947
James Smart6d368e52011-05-24 11:44:12 -0400948 lxri = lpfc_sli4_next_xritag(phba);
949 if (lxri == NO_XRI) {
James Smartda0436e2009-05-22 14:51:39 -0400950 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
951 psb->data, psb->dma_handle);
952 kfree(psb);
953 break;
954 }
James Smartf7bc6432013-10-10 12:19:53 -0400955
956 /* Allocate iotag for psb->cur_iocbq. */
957 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
958 if (iotag == 0) {
959 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
960 psb->data, psb->dma_handle);
961 kfree(psb);
962 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
963 "3368 Failed to allocated IOTAG for"
964 " XRI:0x%x\n", lxri);
965 lpfc_sli4_free_xri(phba, lxri);
966 break;
967 }
James Smart6d368e52011-05-24 11:44:12 -0400968 psb->cur_iocbq.sli4_lxritag = lxri;
969 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smartda0436e2009-05-22 14:51:39 -0400970 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Smartda0436e2009-05-22 14:51:39 -0400971 psb->fcp_bpl = psb->data;
James Smart96f70772013-04-17 20:16:15 -0400972 psb->fcp_cmnd = (psb->data + sgl_size);
James Smartda0436e2009-05-22 14:51:39 -0400973 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
974 sizeof(struct fcp_cmnd));
975
976 /* Initialize local short-hand pointers. */
977 sgl = (struct sli4_sge *)psb->fcp_bpl;
978 pdma_phys_bpl = psb->dma_handle;
James Smart96f70772013-04-17 20:16:15 -0400979 pdma_phys_fcp_cmd = (psb->dma_handle + sgl_size);
James Smartda0436e2009-05-22 14:51:39 -0400980 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
981
982 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400983 * The first two bdes are the FCP_CMD and FCP_RSP.
984 * The balance are sg list bdes. Initialize the
985 * first two and leave the rest for queuecommand.
James Smartda0436e2009-05-22 14:51:39 -0400986 */
987 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
988 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
James Smart05580562011-05-24 11:40:48 -0400989 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400990 bf_set(lpfc_sli4_sge_last, sgl, 0);
991 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500992 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
James Smartda0436e2009-05-22 14:51:39 -0400993 sgl++;
994
995 /* Setup the physical region for the FCP RSP */
996 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
997 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smart05580562011-05-24 11:40:48 -0400998 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400999 bf_set(lpfc_sli4_sge_last, sgl, 1);
1000 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05001001 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -04001002
1003 /*
1004 * Since the IOCB for the FCP I/O is built into this
1005 * lpfc_scsi_buf, initialize it with all known data now.
1006 */
1007 iocb = &psb->cur_iocbq.iocb;
1008 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
1009 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
1010 /* setting the BLP size to 2 * sizeof BDE may not be correct.
1011 * We are setting the bpl to point to out sgl. An sgl's
1012 * entries are 16 bytes, a bpl entries are 12 bytes.
1013 */
1014 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
1015 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
1016 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
1017 iocb->ulpBdeCount = 1;
1018 iocb->ulpLe = 1;
1019 iocb->ulpClass = CLASS3;
James Smart8a9d2e82012-05-09 21:16:12 -04001020 psb->cur_iocbq.context1 = psb;
James Smartda0436e2009-05-22 14:51:39 -04001021 psb->dma_phys_bpl = pdma_phys_bpl;
James Smart6d368e52011-05-24 11:44:12 -04001022
James Smart8a9d2e82012-05-09 21:16:12 -04001023 /* add the scsi buffer to a post list */
1024 list_add_tail(&psb->list, &post_sblist);
James Smarta40fc5f2013-04-17 20:17:40 -04001025 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04001026 phba->sli4_hba.scsi_xri_cnt++;
James Smarta40fc5f2013-04-17 20:17:40 -04001027 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smartda0436e2009-05-22 14:51:39 -04001028 }
James Smart8a9d2e82012-05-09 21:16:12 -04001029 lpfc_printf_log(phba, KERN_INFO, LOG_BG,
1030 "3021 Allocate %d out of %d requested new SCSI "
1031 "buffers\n", bcnt, num_to_alloc);
James Smartda0436e2009-05-22 14:51:39 -04001032
James Smart8a9d2e82012-05-09 21:16:12 -04001033 /* post the list of scsi buffer sgls to port if available */
1034 if (!list_empty(&post_sblist))
1035 num_posted = lpfc_sli4_post_scsi_sgl_list(phba,
1036 &post_sblist, bcnt);
1037 else
1038 num_posted = 0;
1039
1040 return num_posted;
James Smartda0436e2009-05-22 14:51:39 -04001041}
1042
1043/**
James Smart3772a992009-05-22 14:50:54 -04001044 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
1045 * @vport: The virtual port for which this call being executed.
1046 * @num_to_allocate: The requested number of buffers to allocate.
1047 *
1048 * This routine wraps the actual SCSI buffer allocator function pointer from
1049 * the lpfc_hba struct.
1050 *
1051 * Return codes:
1052 * int - number of scsi buffers that were allocated.
1053 * 0 = failure, less than num_to_alloc is a partial failure.
1054 **/
1055static inline int
1056lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
1057{
1058 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
1059}
1060
1061/**
James Smart19ca7602010-11-20 23:11:55 -05001062 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -04001063 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05001064 *
1065 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1066 * and returns to caller.
1067 *
1068 * Return codes:
1069 * NULL - Error
1070 * Pointer to lpfc_scsi_buf - Success
1071 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +01001072static struct lpfc_scsi_buf*
James Smart19ca7602010-11-20 23:11:55 -05001073lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001074{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001075 struct lpfc_scsi_buf * lpfc_cmd = NULL;
James Smarta40fc5f2013-04-17 20:17:40 -04001076 struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
James Smart164cecd2013-09-06 12:22:38 -04001077 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001078
James Smart164cecd2013-09-06 12:22:38 -04001079 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
James Smarta40fc5f2013-04-17 20:17:40 -04001080 list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_scsi_buf,
1081 list);
1082 if (!lpfc_cmd) {
James Smart164cecd2013-09-06 12:22:38 -04001083 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04001084 list_splice(&phba->lpfc_scsi_buf_list_put,
1085 &phba->lpfc_scsi_buf_list_get);
1086 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
1087 list_remove_head(scsi_buf_list_get, lpfc_cmd,
1088 struct lpfc_scsi_buf, list);
James Smart164cecd2013-09-06 12:22:38 -04001089 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smart1dcb58e2007-04-25 09:51:30 -04001090 }
James Smart164cecd2013-09-06 12:22:38 -04001091 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001092 return lpfc_cmd;
1093}
James Smart19ca7602010-11-20 23:11:55 -05001094/**
1095 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1096 * @phba: The HBA for which this call is being executed.
1097 *
1098 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1099 * and returns to caller.
1100 *
1101 * Return codes:
1102 * NULL - Error
1103 * Pointer to lpfc_scsi_buf - Success
1104 **/
1105static struct lpfc_scsi_buf*
1106lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1107{
James Smart3be30e02013-05-31 17:05:17 -04001108 struct lpfc_scsi_buf *lpfc_cmd, *lpfc_cmd_next;
James Smart164cecd2013-09-06 12:22:38 -04001109 unsigned long iflag = 0;
James Smart19ca7602010-11-20 23:11:55 -05001110 int found = 0;
1111
James Smart164cecd2013-09-06 12:22:38 -04001112 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
James Smart3be30e02013-05-31 17:05:17 -04001113 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1114 &phba->lpfc_scsi_buf_list_get, list) {
James Smart19ca7602010-11-20 23:11:55 -05001115 if (lpfc_test_rrq_active(phba, ndlp,
James Smartee0f4fe2012-05-09 21:19:14 -04001116 lpfc_cmd->cur_iocbq.sli4_lxritag))
James Smart1151e3e2011-02-16 12:39:35 -05001117 continue;
1118 list_del(&lpfc_cmd->list);
James Smart19ca7602010-11-20 23:11:55 -05001119 found = 1;
James Smart1151e3e2011-02-16 12:39:35 -05001120 break;
James Smart19ca7602010-11-20 23:11:55 -05001121 }
James Smarta40fc5f2013-04-17 20:17:40 -04001122 if (!found) {
James Smart164cecd2013-09-06 12:22:38 -04001123 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04001124 list_splice(&phba->lpfc_scsi_buf_list_put,
1125 &phba->lpfc_scsi_buf_list_get);
1126 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart164cecd2013-09-06 12:22:38 -04001127 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smart3be30e02013-05-31 17:05:17 -04001128 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1129 &phba->lpfc_scsi_buf_list_get, list) {
James Smarta40fc5f2013-04-17 20:17:40 -04001130 if (lpfc_test_rrq_active(
1131 phba, ndlp, lpfc_cmd->cur_iocbq.sli4_lxritag))
1132 continue;
1133 list_del(&lpfc_cmd->list);
1134 found = 1;
1135 break;
1136 }
1137 }
James Smart164cecd2013-09-06 12:22:38 -04001138 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -05001139 if (!found)
1140 return NULL;
James Smarta40fc5f2013-04-17 20:17:40 -04001141 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001142}
1143/**
1144 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1145 * @phba: The HBA for which this call is being executed.
1146 *
1147 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1148 * and returns to caller.
1149 *
1150 * Return codes:
1151 * NULL - Error
1152 * Pointer to lpfc_scsi_buf - Success
1153 **/
1154static struct lpfc_scsi_buf*
1155lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1156{
1157 return phba->lpfc_get_scsi_buf(phba, ndlp);
1158}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001159
James Smart9bad7672008-12-04 22:39:02 -05001160/**
James Smart3772a992009-05-22 14:50:54 -04001161 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -05001162 * @phba: The Hba for which this call is being executed.
1163 * @psb: The scsi buffer which is being released.
1164 *
1165 * This routine releases @psb scsi buffer by adding it to tail of @phba
1166 * lpfc_scsi_buf_list list.
1167 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001168static void
James Smart3772a992009-05-22 14:50:54 -04001169lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001170{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001171 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001172
James Smarta40fc5f2013-04-17 20:17:40 -04001173 psb->seg_cnt = 0;
1174 psb->nonsg_phys = 0;
1175 psb->prot_seg_cnt = 0;
1176
1177 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001178 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -04001179 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -04001180 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1181 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
dea31012005-04-17 16:05:31 -05001182}
1183
James Smart9bad7672008-12-04 22:39:02 -05001184/**
James Smartda0436e2009-05-22 14:51:39 -04001185 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1186 * @phba: The Hba for which this call is being executed.
1187 * @psb: The scsi buffer which is being released.
1188 *
1189 * This routine releases @psb scsi buffer by adding it to tail of @phba
1190 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1191 * and cannot be reused for at least RA_TOV amount of time if it was
1192 * aborted.
1193 **/
1194static void
1195lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1196{
1197 unsigned long iflag = 0;
1198
James Smarta40fc5f2013-04-17 20:17:40 -04001199 psb->seg_cnt = 0;
1200 psb->nonsg_phys = 0;
1201 psb->prot_seg_cnt = 0;
1202
James Smart341af102010-01-26 23:07:37 -05001203 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -04001204 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1205 iflag);
1206 psb->pCmd = NULL;
1207 list_add_tail(&psb->list,
1208 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1209 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1210 iflag);
1211 } else {
James Smartda0436e2009-05-22 14:51:39 -04001212 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -04001213 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -04001214 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1215 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1216 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -04001217 }
1218}
1219
1220/**
James Smart3772a992009-05-22 14:50:54 -04001221 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1222 * @phba: The Hba for which this call is being executed.
1223 * @psb: The scsi buffer which is being released.
1224 *
1225 * This routine releases @psb scsi buffer by adding it to tail of @phba
1226 * lpfc_scsi_buf_list list.
1227 **/
1228static void
1229lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1230{
1231
1232 phba->lpfc_release_scsi_buf(phba, psb);
1233}
1234
1235/**
1236 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001237 * @phba: The Hba for which this call is being executed.
1238 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1239 *
1240 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001241 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1242 * through sg elements and format the bdea. This routine also initializes all
1243 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001244 *
1245 * Return codes:
1246 * 1 - Error
1247 * 0 - Success
1248 **/
dea31012005-04-17 16:05:31 -05001249static int
James Smart3772a992009-05-22 14:50:54 -04001250lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001251{
1252 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1253 struct scatterlist *sgel = NULL;
1254 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1255 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001256 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001257 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001258 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001259 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001260 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001261 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001262
1263 /*
1264 * There are three possibilities here - use scatter-gather segment, use
1265 * the single mapping, or neither. Start the lpfc command prep by
1266 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1267 * data bde entry.
1268 */
1269 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001270 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001271 /*
1272 * The driver stores the segment count returned from pci_map_sg
1273 * because this a count of dma-mappings used to map the use_sg
1274 * pages. They are not guaranteed to be the same for those
1275 * architectures that implement an IOMMU.
1276 */
dea31012005-04-17 16:05:31 -05001277
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001278 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1279 scsi_sg_count(scsi_cmnd), datadir);
1280 if (unlikely(!nseg))
1281 return 1;
1282
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001283 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001284 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001285 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1286 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001287 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001288 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001289 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04001290 lpfc_cmd->seg_cnt = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001291 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001292 return 1;
1293 }
1294
1295 /*
1296 * The driver established a maximum scatter-gather segment count
1297 * during probe that limits the number of sg elements in any
1298 * single scsi command. Just run through the seg_cnt and format
1299 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001300 * When using SLI-3 the driver will try to fit all the BDEs into
1301 * the IOCB. If it can't then the BDEs get added to a BPL as it
1302 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001303 */
James Smart34b02dc2008-08-24 21:49:55 -04001304 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001305 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001306 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001307 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001308 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001309 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1310 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1311 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1312 data_bde->addrLow = putPaddrLow(physaddr);
1313 data_bde->addrHigh = putPaddrHigh(physaddr);
1314 data_bde++;
1315 } else {
1316 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1317 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1318 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1319 bpl->addrLow =
1320 le32_to_cpu(putPaddrLow(physaddr));
1321 bpl->addrHigh =
1322 le32_to_cpu(putPaddrHigh(physaddr));
1323 bpl++;
1324 }
dea31012005-04-17 16:05:31 -05001325 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001326 }
dea31012005-04-17 16:05:31 -05001327
1328 /*
1329 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001330 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1331 * explicitly reinitialized and for SLI-3 the extended bde count is
1332 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001333 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001334 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001335 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1336 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001337 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1338 /*
1339 * The extended IOCB format can only fit 3 BDE or a BPL.
1340 * This I/O has more than 3 BDE so the 1st data bde will
1341 * be a BPL that is filled in here.
1342 */
1343 physaddr = lpfc_cmd->dma_handle;
1344 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1345 data_bde->tus.f.bdeSize = (num_bde *
1346 sizeof(struct ulp_bde64));
1347 physaddr += (sizeof(struct fcp_cmnd) +
1348 sizeof(struct fcp_rsp) +
1349 (2 * sizeof(struct ulp_bde64)));
1350 data_bde->addrHigh = putPaddrHigh(physaddr);
1351 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001352 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -04001353 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1354 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001355 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -04001356 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1357 }
1358 } else {
1359 iocb_cmd->un.fcpi64.bdl.bdeSize =
1360 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001361 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001362 }
James Smart09372822008-01-11 01:52:54 -05001363 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001364
1365 /*
1366 * Due to difference in data length between DIF/non-DIF paths,
1367 * we need to set word 4 of IOCB here
1368 */
James Smarta257bf92009-04-06 18:48:10 -04001369 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001370 return 0;
1371}
1372
James Smartf9bb2da2011-10-10 21:34:11 -04001373#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001374
James Smart9a6b09c2012-03-01 22:37:42 -05001375/* Return if if error injection is detected by Initiator */
1376#define BG_ERR_INIT 0x1
1377/* Return if if error injection is detected by Target */
1378#define BG_ERR_TGT 0x2
1379/* Return if if swapping CSUM<-->CRC is required for error injection */
1380#define BG_ERR_SWAP 0x10
1381/* Return if disabling Guard/Ref/App checking is required for error injection */
1382#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -05001383
1384/**
1385 * lpfc_bg_err_inject - Determine if we should inject an error
1386 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -04001387 * @sc: The SCSI command to examine
1388 * @reftag: (out) BlockGuard reference tag for transmitted data
1389 * @apptag: (out) BlockGuard application tag for transmitted data
1390 * @new_guard (in) Value to replace CRC with if needed
1391 *
James Smart9a6b09c2012-03-01 22:37:42 -05001392 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -05001393 **/
James Smartf9bb2da2011-10-10 21:34:11 -04001394static int
1395lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1396 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1397{
1398 struct scatterlist *sgpe; /* s/g prot entry */
1399 struct scatterlist *sgde; /* s/g data entry */
James Smart9a6b09c2012-03-01 22:37:42 -05001400 struct lpfc_scsi_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -05001401 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -05001402 struct lpfc_nodelist *ndlp;
1403 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -04001404 uint32_t op = scsi_get_prot_op(sc);
1405 uint32_t blksize;
1406 uint32_t numblks;
1407 sector_t lba;
1408 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001409 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001410
1411 if (op == SCSI_PROT_NORMAL)
1412 return 0;
1413
James Smartacd68592012-01-18 16:25:09 -05001414 sgpe = scsi_prot_sglist(sc);
1415 sgde = scsi_sglist(sc);
James Smartf9bb2da2011-10-10 21:34:11 -04001416 lba = scsi_get_lba(sc);
James Smart4ac9b222012-03-01 22:38:29 -05001417
1418 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001419 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1420 blksize = lpfc_cmd_blksize(sc);
1421 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1422
1423 /* Make sure we have the right LBA if one is specified */
1424 if ((phba->lpfc_injerr_lba < lba) ||
1425 (phba->lpfc_injerr_lba >= (lba + numblks)))
1426 return 0;
James Smartacd68592012-01-18 16:25:09 -05001427 if (sgpe) {
1428 blockoff = phba->lpfc_injerr_lba - lba;
1429 numblks = sg_dma_len(sgpe) /
1430 sizeof(struct scsi_dif_tuple);
1431 if (numblks < blockoff)
1432 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001433 }
James Smartf9bb2da2011-10-10 21:34:11 -04001434 }
1435
James Smart4ac9b222012-03-01 22:38:29 -05001436 /* Next check if we need to match the remote NPortID or WWPN */
James Smart1ba981f2014-02-20 09:56:45 -05001437 rdata = lpfc_rport_data_from_scsi_device(sc->device);
James Smart4ac9b222012-03-01 22:38:29 -05001438 if (rdata && rdata->pnode) {
1439 ndlp = rdata->pnode;
1440
1441 /* Make sure we have the right NPortID if one is specified */
1442 if (phba->lpfc_injerr_nportid &&
1443 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1444 return 0;
1445
1446 /*
1447 * Make sure we have the right WWPN if one is specified.
1448 * wwn[0] should be a non-zero NAA in a good WWPN.
1449 */
1450 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1451 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1452 sizeof(struct lpfc_name)) != 0))
1453 return 0;
1454 }
1455
1456 /* Setup a ptr to the protection data if the SCSI host provides it */
1457 if (sgpe) {
1458 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1459 src += blockoff;
1460 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1461 }
1462
James Smartf9bb2da2011-10-10 21:34:11 -04001463 /* Should we change the Reference Tag */
1464 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001465 if (phba->lpfc_injerr_wref_cnt) {
1466 switch (op) {
1467 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001468 if (src) {
1469 /*
1470 * For WRITE_PASS, force the error
1471 * to be sent on the wire. It should
1472 * be detected by the Target.
1473 * If blockoff != 0 error will be
1474 * inserted in middle of the IO.
1475 */
James Smartf9bb2da2011-10-10 21:34:11 -04001476
James Smartacd68592012-01-18 16:25:09 -05001477 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1478 "9076 BLKGRD: Injecting reftag error: "
1479 "write lba x%lx + x%x oldrefTag x%x\n",
1480 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001481 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001482
1483 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001484 * Save the old ref_tag so we can
1485 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001486 */
James Smart9a6b09c2012-03-01 22:37:42 -05001487 if (lpfc_cmd) {
1488 lpfc_cmd->prot_data_type =
1489 LPFC_INJERR_REFTAG;
1490 lpfc_cmd->prot_data_segment =
1491 src;
1492 lpfc_cmd->prot_data =
1493 src->ref_tag;
1494 }
1495 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001496 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001497 if (phba->lpfc_injerr_wref_cnt == 0) {
1498 phba->lpfc_injerr_nportid = 0;
1499 phba->lpfc_injerr_lba =
1500 LPFC_INJERR_LBA_OFF;
1501 memset(&phba->lpfc_injerr_wwpn,
1502 0, sizeof(struct lpfc_name));
1503 }
James Smart9a6b09c2012-03-01 22:37:42 -05001504 rc = BG_ERR_TGT | BG_ERR_CHECK;
1505
James Smartacd68592012-01-18 16:25:09 -05001506 break;
1507 }
1508 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001509 case SCSI_PROT_WRITE_INSERT:
1510 /*
1511 * For WRITE_INSERT, force the error
1512 * to be sent on the wire. It should be
1513 * detected by the Target.
1514 */
1515 /* DEADBEEF will be the reftag on the wire */
1516 *reftag = 0xDEADBEEF;
1517 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001518 if (phba->lpfc_injerr_wref_cnt == 0) {
1519 phba->lpfc_injerr_nportid = 0;
1520 phba->lpfc_injerr_lba =
1521 LPFC_INJERR_LBA_OFF;
1522 memset(&phba->lpfc_injerr_wwpn,
1523 0, sizeof(struct lpfc_name));
1524 }
James Smart9a6b09c2012-03-01 22:37:42 -05001525 rc = BG_ERR_TGT | BG_ERR_CHECK;
1526
1527 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1528 "9078 BLKGRD: Injecting reftag error: "
1529 "write lba x%lx\n", (unsigned long)lba);
1530 break;
James Smartacd68592012-01-18 16:25:09 -05001531 case SCSI_PROT_WRITE_STRIP:
1532 /*
1533 * For WRITE_STRIP and WRITE_PASS,
1534 * force the error on data
1535 * being copied from SLI-Host to SLI-Port.
1536 */
1537 *reftag = 0xDEADBEEF;
1538 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001539 if (phba->lpfc_injerr_wref_cnt == 0) {
1540 phba->lpfc_injerr_nportid = 0;
1541 phba->lpfc_injerr_lba =
1542 LPFC_INJERR_LBA_OFF;
1543 memset(&phba->lpfc_injerr_wwpn,
1544 0, sizeof(struct lpfc_name));
1545 }
James Smartacd68592012-01-18 16:25:09 -05001546 rc = BG_ERR_INIT;
1547
1548 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1549 "9077 BLKGRD: Injecting reftag error: "
1550 "write lba x%lx\n", (unsigned long)lba);
1551 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001552 }
James Smartacd68592012-01-18 16:25:09 -05001553 }
1554 if (phba->lpfc_injerr_rref_cnt) {
1555 switch (op) {
1556 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001557 case SCSI_PROT_READ_STRIP:
1558 case SCSI_PROT_READ_PASS:
1559 /*
1560 * For READ_STRIP and READ_PASS, force the
1561 * error on data being read off the wire. It
1562 * should force an IO error to the driver.
1563 */
James Smartf9bb2da2011-10-10 21:34:11 -04001564 *reftag = 0xDEADBEEF;
1565 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001566 if (phba->lpfc_injerr_rref_cnt == 0) {
1567 phba->lpfc_injerr_nportid = 0;
1568 phba->lpfc_injerr_lba =
1569 LPFC_INJERR_LBA_OFF;
1570 memset(&phba->lpfc_injerr_wwpn,
1571 0, sizeof(struct lpfc_name));
1572 }
James Smartacd68592012-01-18 16:25:09 -05001573 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001574
1575 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001576 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001577 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001578 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001579 }
1580 }
1581 }
1582
1583 /* Should we change the Application Tag */
1584 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001585 if (phba->lpfc_injerr_wapp_cnt) {
1586 switch (op) {
1587 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001588 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001589 /*
1590 * For WRITE_PASS, force the error
1591 * to be sent on the wire. It should
1592 * be detected by the Target.
1593 * If blockoff != 0 error will be
1594 * inserted in middle of the IO.
1595 */
1596
James Smartacd68592012-01-18 16:25:09 -05001597 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1598 "9080 BLKGRD: Injecting apptag error: "
1599 "write lba x%lx + x%x oldappTag x%x\n",
1600 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001601 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001602
1603 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001604 * Save the old app_tag so we can
1605 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001606 */
James Smart9a6b09c2012-03-01 22:37:42 -05001607 if (lpfc_cmd) {
1608 lpfc_cmd->prot_data_type =
1609 LPFC_INJERR_APPTAG;
1610 lpfc_cmd->prot_data_segment =
1611 src;
1612 lpfc_cmd->prot_data =
1613 src->app_tag;
1614 }
1615 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001616 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001617 if (phba->lpfc_injerr_wapp_cnt == 0) {
1618 phba->lpfc_injerr_nportid = 0;
1619 phba->lpfc_injerr_lba =
1620 LPFC_INJERR_LBA_OFF;
1621 memset(&phba->lpfc_injerr_wwpn,
1622 0, sizeof(struct lpfc_name));
1623 }
James Smart9a6b09c2012-03-01 22:37:42 -05001624 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001625 break;
1626 }
1627 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001628 case SCSI_PROT_WRITE_INSERT:
1629 /*
1630 * For WRITE_INSERT, force the
1631 * error to be sent on the wire. It should be
1632 * detected by the Target.
1633 */
1634 /* DEAD will be the apptag on the wire */
1635 *apptag = 0xDEAD;
1636 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001637 if (phba->lpfc_injerr_wapp_cnt == 0) {
1638 phba->lpfc_injerr_nportid = 0;
1639 phba->lpfc_injerr_lba =
1640 LPFC_INJERR_LBA_OFF;
1641 memset(&phba->lpfc_injerr_wwpn,
1642 0, sizeof(struct lpfc_name));
1643 }
James Smart9a6b09c2012-03-01 22:37:42 -05001644 rc = BG_ERR_TGT | BG_ERR_CHECK;
1645
1646 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1647 "0813 BLKGRD: Injecting apptag error: "
1648 "write lba x%lx\n", (unsigned long)lba);
1649 break;
James Smartacd68592012-01-18 16:25:09 -05001650 case SCSI_PROT_WRITE_STRIP:
1651 /*
1652 * For WRITE_STRIP and WRITE_PASS,
1653 * force the error on data
1654 * being copied from SLI-Host to SLI-Port.
1655 */
1656 *apptag = 0xDEAD;
1657 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001658 if (phba->lpfc_injerr_wapp_cnt == 0) {
1659 phba->lpfc_injerr_nportid = 0;
1660 phba->lpfc_injerr_lba =
1661 LPFC_INJERR_LBA_OFF;
1662 memset(&phba->lpfc_injerr_wwpn,
1663 0, sizeof(struct lpfc_name));
1664 }
James Smartacd68592012-01-18 16:25:09 -05001665 rc = BG_ERR_INIT;
1666
1667 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1668 "0812 BLKGRD: Injecting apptag error: "
1669 "write lba x%lx\n", (unsigned long)lba);
1670 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001671 }
James Smartacd68592012-01-18 16:25:09 -05001672 }
1673 if (phba->lpfc_injerr_rapp_cnt) {
1674 switch (op) {
1675 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001676 case SCSI_PROT_READ_STRIP:
1677 case SCSI_PROT_READ_PASS:
1678 /*
1679 * For READ_STRIP and READ_PASS, force the
1680 * error on data being read off the wire. It
1681 * should force an IO error to the driver.
1682 */
James Smartf9bb2da2011-10-10 21:34:11 -04001683 *apptag = 0xDEAD;
1684 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001685 if (phba->lpfc_injerr_rapp_cnt == 0) {
1686 phba->lpfc_injerr_nportid = 0;
1687 phba->lpfc_injerr_lba =
1688 LPFC_INJERR_LBA_OFF;
1689 memset(&phba->lpfc_injerr_wwpn,
1690 0, sizeof(struct lpfc_name));
1691 }
James Smartacd68592012-01-18 16:25:09 -05001692 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001693
1694 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001695 "0814 BLKGRD: Injecting apptag error: "
1696 "read lba x%lx\n", (unsigned long)lba);
1697 break;
1698 }
1699 }
1700 }
1701
1702
1703 /* Should we change the Guard Tag */
1704 if (new_guard) {
1705 if (phba->lpfc_injerr_wgrd_cnt) {
1706 switch (op) {
1707 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001708 rc = BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001709 /* Drop thru */
James Smartacd68592012-01-18 16:25:09 -05001710
James Smartacd68592012-01-18 16:25:09 -05001711 case SCSI_PROT_WRITE_INSERT:
1712 /*
1713 * For WRITE_INSERT, force the
1714 * error to be sent on the wire. It should be
1715 * detected by the Target.
1716 */
1717 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001718 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1719 phba->lpfc_injerr_nportid = 0;
1720 phba->lpfc_injerr_lba =
1721 LPFC_INJERR_LBA_OFF;
1722 memset(&phba->lpfc_injerr_wwpn,
1723 0, sizeof(struct lpfc_name));
1724 }
James Smartacd68592012-01-18 16:25:09 -05001725
James Smart9a6b09c2012-03-01 22:37:42 -05001726 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001727 /* Signals the caller to swap CRC->CSUM */
1728
1729 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1730 "0817 BLKGRD: Injecting guard error: "
1731 "write lba x%lx\n", (unsigned long)lba);
1732 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001733 case SCSI_PROT_WRITE_STRIP:
1734 /*
1735 * For WRITE_STRIP and WRITE_PASS,
1736 * force the error on data
1737 * being copied from SLI-Host to SLI-Port.
1738 */
1739 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001740 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1741 phba->lpfc_injerr_nportid = 0;
1742 phba->lpfc_injerr_lba =
1743 LPFC_INJERR_LBA_OFF;
1744 memset(&phba->lpfc_injerr_wwpn,
1745 0, sizeof(struct lpfc_name));
1746 }
James Smart9a6b09c2012-03-01 22:37:42 -05001747
1748 rc = BG_ERR_INIT | BG_ERR_SWAP;
1749 /* Signals the caller to swap CRC->CSUM */
1750
1751 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1752 "0816 BLKGRD: Injecting guard error: "
1753 "write lba x%lx\n", (unsigned long)lba);
1754 break;
James Smartacd68592012-01-18 16:25:09 -05001755 }
1756 }
1757 if (phba->lpfc_injerr_rgrd_cnt) {
1758 switch (op) {
1759 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001760 case SCSI_PROT_READ_STRIP:
1761 case SCSI_PROT_READ_PASS:
1762 /*
1763 * For READ_STRIP and READ_PASS, force the
1764 * error on data being read off the wire. It
1765 * should force an IO error to the driver.
1766 */
James Smartacd68592012-01-18 16:25:09 -05001767 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001768 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1769 phba->lpfc_injerr_nportid = 0;
1770 phba->lpfc_injerr_lba =
1771 LPFC_INJERR_LBA_OFF;
1772 memset(&phba->lpfc_injerr_wwpn,
1773 0, sizeof(struct lpfc_name));
1774 }
James Smartacd68592012-01-18 16:25:09 -05001775
James Smart9a6b09c2012-03-01 22:37:42 -05001776 rc = BG_ERR_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001777 /* Signals the caller to swap CRC->CSUM */
1778
1779 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1780 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001781 "read lba x%lx\n", (unsigned long)lba);
1782 }
1783 }
1784 }
1785
James Smartf9bb2da2011-10-10 21:34:11 -04001786 return rc;
1787}
1788#endif
1789
James Smartacd68592012-01-18 16:25:09 -05001790/**
1791 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1792 * the specified SCSI command.
1793 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001794 * @sc: The SCSI command to examine
1795 * @txopt: (out) BlockGuard operation for transmitted data
1796 * @rxopt: (out) BlockGuard operation for received data
1797 *
1798 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1799 *
James Smartacd68592012-01-18 16:25:09 -05001800 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001801static int
James Smart6c8eea52010-04-06 14:49:53 -04001802lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1803 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001804{
James Smart6c8eea52010-04-06 14:49:53 -04001805 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001806
James Smart9c6aa9d2013-05-31 17:03:39 -04001807 if (lpfc_cmd_guard_csum(sc)) {
James Smarte2a0a9d2008-12-04 22:40:02 -05001808 switch (scsi_get_prot_op(sc)) {
1809 case SCSI_PROT_READ_INSERT:
1810 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001811 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001812 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001813 break;
1814
1815 case SCSI_PROT_READ_STRIP:
1816 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001817 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001818 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001819 break;
1820
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001821 case SCSI_PROT_READ_PASS:
1822 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001823 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001824 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001825 break;
1826
James Smarte2a0a9d2008-12-04 22:40:02 -05001827 case SCSI_PROT_NORMAL:
1828 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001829 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001830 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1831 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001832 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001833 break;
1834
1835 }
James Smart7c56b9f2011-07-22 18:36:25 -04001836 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001837 switch (scsi_get_prot_op(sc)) {
1838 case SCSI_PROT_READ_STRIP:
1839 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001840 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001841 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001842 break;
1843
1844 case SCSI_PROT_READ_PASS:
1845 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001846 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001847 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001848 break;
1849
James Smarte2a0a9d2008-12-04 22:40:02 -05001850 case SCSI_PROT_READ_INSERT:
1851 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001852 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001853 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001854 break;
1855
James Smarte2a0a9d2008-12-04 22:40:02 -05001856 case SCSI_PROT_NORMAL:
1857 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001858 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001859 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1860 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001861 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001862 break;
1863 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001864 }
1865
James Smart6c8eea52010-04-06 14:49:53 -04001866 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001867}
1868
James Smartacd68592012-01-18 16:25:09 -05001869#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1870/**
1871 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1872 * the specified SCSI command in order to force a guard tag error.
1873 * @phba: The Hba for which this call is being executed.
1874 * @sc: The SCSI command to examine
1875 * @txopt: (out) BlockGuard operation for transmitted data
1876 * @rxopt: (out) BlockGuard operation for received data
1877 *
1878 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1879 *
1880 **/
1881static int
1882lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1883 uint8_t *txop, uint8_t *rxop)
1884{
James Smartacd68592012-01-18 16:25:09 -05001885 uint8_t ret = 0;
1886
James Smart9c6aa9d2013-05-31 17:03:39 -04001887 if (lpfc_cmd_guard_csum(sc)) {
James Smartacd68592012-01-18 16:25:09 -05001888 switch (scsi_get_prot_op(sc)) {
1889 case SCSI_PROT_READ_INSERT:
1890 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001891 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001892 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001893 break;
1894
1895 case SCSI_PROT_READ_STRIP:
1896 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001897 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001898 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001899 break;
1900
1901 case SCSI_PROT_READ_PASS:
1902 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001903 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001904 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001905 break;
1906
1907 case SCSI_PROT_NORMAL:
1908 default:
1909 break;
1910
1911 }
1912 } else {
1913 switch (scsi_get_prot_op(sc)) {
1914 case SCSI_PROT_READ_STRIP:
1915 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001916 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001917 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001918 break;
1919
1920 case SCSI_PROT_READ_PASS:
1921 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001922 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05001923 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001924 break;
1925
1926 case SCSI_PROT_READ_INSERT:
1927 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001928 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001929 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001930 break;
1931
1932 case SCSI_PROT_NORMAL:
1933 default:
1934 break;
1935 }
1936 }
1937
1938 return ret;
1939}
1940#endif
1941
1942/**
1943 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1944 * @phba: The Hba for which this call is being executed.
1945 * @sc: pointer to scsi command we're working on
1946 * @bpl: pointer to buffer list for protection groups
1947 * @datacnt: number of segments of data that have been dma mapped
1948 *
1949 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05001950 * type LPFC_PG_TYPE_NO_DIF
1951 *
1952 * This is usually used when the HBA is instructed to generate
1953 * DIFs and insert them into data stream (or strip DIF from
1954 * incoming data stream)
1955 *
1956 * The buffer list consists of just one protection group described
1957 * below:
1958 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001959 * start of prot group --> | PDE_5 |
1960 * +-------------------------+
1961 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001962 * +-------------------------+
1963 * | Data BDE |
1964 * +-------------------------+
1965 * |more Data BDE's ... (opt)|
1966 * +-------------------------+
1967 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001968 *
1969 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05001970 *
1971 * Returns the number of BDEs added to the BPL.
1972 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001973static int
1974lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1975 struct ulp_bde64 *bpl, int datasegcnt)
1976{
1977 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001978 struct lpfc_pde5 *pde5 = NULL;
1979 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001980 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001981 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001982 int datadir = sc->sc_data_direction;
James Smart0829a192012-05-09 21:18:12 -04001983#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001984 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001985#endif
James Smartacd68592012-01-18 16:25:09 -05001986 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001987 uint32_t reftag;
James Smart7c56b9f2011-07-22 18:36:25 -04001988 unsigned blksize;
James Smart6c8eea52010-04-06 14:49:53 -04001989 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001990
James Smart6c8eea52010-04-06 14:49:53 -04001991 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1992 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001993 goto out;
1994
James Smart6c8eea52010-04-06 14:49:53 -04001995 /* extract some info from the scsi command for pde*/
James Smarte2a0a9d2008-12-04 22:40:02 -05001996 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05001997 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05001998
James Smartf9bb2da2011-10-10 21:34:11 -04001999#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002000 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002001 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002002 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002003 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002004 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002005 checking = 0;
2006 }
James Smartf9bb2da2011-10-10 21:34:11 -04002007#endif
2008
James Smart6c8eea52010-04-06 14:49:53 -04002009 /* setup PDE5 with what we have */
2010 pde5 = (struct lpfc_pde5 *) bpl;
2011 memset(pde5, 0, sizeof(struct lpfc_pde5));
2012 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002013
James Smartbc739052010-08-04 16:11:18 -04002014 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002015 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002016 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002017
James Smart6c8eea52010-04-06 14:49:53 -04002018 /* advance bpl and increment bde count */
2019 num_bde++;
2020 bpl++;
2021 pde6 = (struct lpfc_pde6 *) bpl;
2022
2023 /* setup PDE6 with the rest of the info */
2024 memset(pde6, 0, sizeof(struct lpfc_pde6));
2025 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2026 bf_set(pde6_optx, pde6, txop);
2027 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002028
2029 /*
2030 * We only need to check the data on READs, for WRITEs
2031 * protection data is automatically generated, not checked.
2032 */
James Smart6c8eea52010-04-06 14:49:53 -04002033 if (datadir == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002034 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002035 bf_set(pde6_ce, pde6, checking);
2036 else
2037 bf_set(pde6_ce, pde6, 0);
2038
James Smart9c6aa9d2013-05-31 17:03:39 -04002039 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002040 bf_set(pde6_re, pde6, checking);
2041 else
2042 bf_set(pde6_re, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002043 }
2044 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002045 bf_set(pde6_ae, pde6, 0);
2046 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002047
James Smartbc739052010-08-04 16:11:18 -04002048 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002049 pde6->word0 = cpu_to_le32(pde6->word0);
2050 pde6->word1 = cpu_to_le32(pde6->word1);
2051 pde6->word2 = cpu_to_le32(pde6->word2);
2052
James Smart6c8eea52010-04-06 14:49:53 -04002053 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002054 num_bde++;
2055 bpl++;
2056
2057 /* assumption: caller has already run dma_map_sg on command data */
2058 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2059 physaddr = sg_dma_address(sgde);
2060 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
2061 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2062 bpl->tus.f.bdeSize = sg_dma_len(sgde);
2063 if (datadir == DMA_TO_DEVICE)
2064 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2065 else
2066 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2067 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2068 bpl++;
2069 num_bde++;
2070 }
2071
2072out:
2073 return num_bde;
2074}
2075
James Smartacd68592012-01-18 16:25:09 -05002076/**
2077 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
2078 * @phba: The Hba for which this call is being executed.
2079 * @sc: pointer to scsi command we're working on
2080 * @bpl: pointer to buffer list for protection groups
2081 * @datacnt: number of segments of data that have been dma mapped
2082 * @protcnt: number of segment of protection data that have been dma mapped
2083 *
2084 * This function sets up BPL buffer list for protection groups of
2085 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05002086 *
2087 * This is usually used when DIFs are in their own buffers,
2088 * separate from the data. The HBA can then by instructed
2089 * to place the DIFs in the outgoing stream. For read operations,
2090 * The HBA could extract the DIFs and place it in DIF buffers.
2091 *
2092 * The buffer list for this type consists of one or more of the
2093 * protection groups described below:
2094 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002095 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002096 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002097 * | PDE_6 |
2098 * +-------------------------+
2099 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05002100 * +-------------------------+
2101 * | Data BDE |
2102 * +-------------------------+
2103 * |more Data BDE's ... (opt)|
2104 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002105 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002106 * +-------------------------+
2107 * | ... |
2108 * +-------------------------+
2109 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002110 * Note: It is assumed that both data and protection s/g buffers have been
2111 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05002112 *
2113 * Returns the number of BDEs added to the BPL.
2114 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002115static int
2116lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2117 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2118{
2119 struct scatterlist *sgde = NULL; /* s/g data entry */
2120 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04002121 struct lpfc_pde5 *pde5 = NULL;
2122 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05002123 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002124 dma_addr_t dataphysaddr, protphysaddr;
2125 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05002126 unsigned int split_offset;
2127 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05002128 unsigned int protgrp_blks, protgrp_bytes;
2129 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04002130 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002131 int datadir = sc->sc_data_direction;
2132 unsigned char pgdone = 0, alldone = 0;
2133 unsigned blksize;
James Smart0829a192012-05-09 21:18:12 -04002134#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002135 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002136#endif
James Smartacd68592012-01-18 16:25:09 -05002137 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002138 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04002139 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002140 int num_bde = 0;
2141
2142 sgpe = scsi_prot_sglist(sc);
2143 sgde = scsi_sglist(sc);
2144
2145 if (!sgpe || !sgde) {
2146 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2147 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2148 sgpe, sgde);
2149 return 0;
2150 }
2151
James Smart6c8eea52010-04-06 14:49:53 -04002152 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2153 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002154 goto out;
2155
James Smart6c8eea52010-04-06 14:49:53 -04002156 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05002157 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002158 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002159
James Smartf9bb2da2011-10-10 21:34:11 -04002160#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002161 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002162 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002163 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002164 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002165 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002166 checking = 0;
2167 }
James Smartf9bb2da2011-10-10 21:34:11 -04002168#endif
2169
James Smarte2a0a9d2008-12-04 22:40:02 -05002170 split_offset = 0;
2171 do {
James Smart96f70772013-04-17 20:16:15 -04002172 /* Check to see if we ran out of space */
2173 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
2174 return num_bde + 3;
2175
James Smart6c8eea52010-04-06 14:49:53 -04002176 /* setup PDE5 with what we have */
2177 pde5 = (struct lpfc_pde5 *) bpl;
2178 memset(pde5, 0, sizeof(struct lpfc_pde5));
2179 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002180
James Smartbc739052010-08-04 16:11:18 -04002181 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002182 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002183 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002184
James Smart6c8eea52010-04-06 14:49:53 -04002185 /* advance bpl and increment bde count */
2186 num_bde++;
2187 bpl++;
2188 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002189
James Smart6c8eea52010-04-06 14:49:53 -04002190 /* setup PDE6 with the rest of the info */
2191 memset(pde6, 0, sizeof(struct lpfc_pde6));
2192 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2193 bf_set(pde6_optx, pde6, txop);
2194 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002195
James Smart9c6aa9d2013-05-31 17:03:39 -04002196 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002197 bf_set(pde6_ce, pde6, checking);
2198 else
2199 bf_set(pde6_ce, pde6, 0);
2200
James Smart9c6aa9d2013-05-31 17:03:39 -04002201 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002202 bf_set(pde6_re, pde6, checking);
2203 else
2204 bf_set(pde6_re, pde6, 0);
2205
James Smart6c8eea52010-04-06 14:49:53 -04002206 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002207 bf_set(pde6_ae, pde6, 0);
2208 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002209
James Smartbc739052010-08-04 16:11:18 -04002210 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002211 pde6->word0 = cpu_to_le32(pde6->word0);
2212 pde6->word1 = cpu_to_le32(pde6->word1);
2213 pde6->word2 = cpu_to_le32(pde6->word2);
2214
James Smart6c8eea52010-04-06 14:49:53 -04002215 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002216 num_bde++;
2217 bpl++;
2218
2219 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05002220 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2221 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05002222
James Smarte2a0a9d2008-12-04 22:40:02 -05002223 /* must be integer multiple of the DIF block length */
2224 BUG_ON(protgroup_len % 8);
2225
James Smart7f860592011-03-11 16:05:52 -05002226 pde7 = (struct lpfc_pde7 *) bpl;
2227 memset(pde7, 0, sizeof(struct lpfc_pde7));
2228 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2229
James Smart7c56b9f2011-07-22 18:36:25 -04002230 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2231 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05002232
James Smarte2a0a9d2008-12-04 22:40:02 -05002233 protgrp_blks = protgroup_len / 8;
2234 protgrp_bytes = protgrp_blks * blksize;
2235
James Smart7f860592011-03-11 16:05:52 -05002236 /* check if this pde is crossing the 4K boundary; if so split */
2237 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2238 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2239 protgroup_offset += protgroup_remainder;
2240 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04002241 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05002242 } else {
2243 protgroup_offset = 0;
2244 curr_prot++;
2245 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002246
James Smarte2a0a9d2008-12-04 22:40:02 -05002247 num_bde++;
2248
2249 /* setup BDE's for data blocks associated with DIF data */
2250 pgdone = 0;
2251 subtotal = 0; /* total bytes processed for current prot grp */
2252 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002253 /* Check to see if we ran out of space */
2254 if (num_bde >= phba->cfg_total_seg_cnt)
2255 return num_bde + 1;
2256
James Smarte2a0a9d2008-12-04 22:40:02 -05002257 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04002258 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2259 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002260 __func__);
2261 return 0;
2262 }
2263 bpl++;
2264 dataphysaddr = sg_dma_address(sgde) + split_offset;
2265 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2266 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2267
2268 remainder = sg_dma_len(sgde) - split_offset;
2269
2270 if ((subtotal + remainder) <= protgrp_bytes) {
2271 /* we can use this whole buffer */
2272 bpl->tus.f.bdeSize = remainder;
2273 split_offset = 0;
2274
2275 if ((subtotal + remainder) == protgrp_bytes)
2276 pgdone = 1;
2277 } else {
2278 /* must split this buffer with next prot grp */
2279 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2280 split_offset += bpl->tus.f.bdeSize;
2281 }
2282
2283 subtotal += bpl->tus.f.bdeSize;
2284
2285 if (datadir == DMA_TO_DEVICE)
2286 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2287 else
2288 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2289 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2290
2291 num_bde++;
2292 curr_data++;
2293
2294 if (split_offset)
2295 break;
2296
2297 /* Move to the next s/g segment if possible */
2298 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04002299
James Smarte2a0a9d2008-12-04 22:40:02 -05002300 }
2301
James Smart7f860592011-03-11 16:05:52 -05002302 if (protgroup_offset) {
2303 /* update the reference tag */
2304 reftag += protgrp_blks;
2305 bpl++;
2306 continue;
2307 }
2308
James Smarte2a0a9d2008-12-04 22:40:02 -05002309 /* are we done ? */
2310 if (curr_prot == protcnt) {
2311 alldone = 1;
2312 } else if (curr_prot < protcnt) {
2313 /* advance to next prot buffer */
2314 sgpe = sg_next(sgpe);
2315 bpl++;
2316
2317 /* update the reference tag */
2318 reftag += protgrp_blks;
2319 } else {
2320 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04002321 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2322 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05002323 }
2324
2325 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05002326out:
2327
James Smarte2a0a9d2008-12-04 22:40:02 -05002328 return num_bde;
2329}
James Smart7f860592011-03-11 16:05:52 -05002330
James Smartacd68592012-01-18 16:25:09 -05002331/**
2332 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2333 * @phba: The Hba for which this call is being executed.
2334 * @sc: pointer to scsi command we're working on
2335 * @sgl: pointer to buffer list for protection groups
2336 * @datacnt: number of segments of data that have been dma mapped
2337 *
2338 * This function sets up SGL buffer list for protection groups of
2339 * type LPFC_PG_TYPE_NO_DIF
2340 *
2341 * This is usually used when the HBA is instructed to generate
2342 * DIFs and insert them into data stream (or strip DIF from
2343 * incoming data stream)
2344 *
2345 * The buffer list consists of just one protection group described
2346 * below:
2347 * +-------------------------+
2348 * start of prot group --> | DI_SEED |
2349 * +-------------------------+
2350 * | Data SGE |
2351 * +-------------------------+
2352 * |more Data SGE's ... (opt)|
2353 * +-------------------------+
2354 *
2355 *
2356 * Note: Data s/g buffers have been dma mapped
2357 *
2358 * Returns the number of SGEs added to the SGL.
2359 **/
2360static int
2361lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2362 struct sli4_sge *sgl, int datasegcnt)
2363{
2364 struct scatterlist *sgde = NULL; /* s/g data entry */
2365 struct sli4_sge_diseed *diseed = NULL;
2366 dma_addr_t physaddr;
2367 int i = 0, num_sge = 0, status;
James Smartacd68592012-01-18 16:25:09 -05002368 uint32_t reftag;
2369 unsigned blksize;
2370 uint8_t txop, rxop;
James Smart0829a192012-05-09 21:18:12 -04002371#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002372 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002373#endif
James Smartacd68592012-01-18 16:25:09 -05002374 uint32_t checking = 1;
2375 uint32_t dma_len;
2376 uint32_t dma_offset = 0;
2377
2378 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2379 if (status)
2380 goto out;
2381
2382 /* extract some info from the scsi command for pde*/
2383 blksize = lpfc_cmd_blksize(sc);
2384 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2385
2386#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002387 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002388 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002389 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002390 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002391 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002392 checking = 0;
2393 }
2394#endif
2395
2396 /* setup DISEED with what we have */
2397 diseed = (struct sli4_sge_diseed *) sgl;
2398 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2399 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2400
2401 /* Endianness conversion if necessary */
2402 diseed->ref_tag = cpu_to_le32(reftag);
2403 diseed->ref_tag_tran = diseed->ref_tag;
2404
James Smarta6887e22013-04-17 20:18:07 -04002405 /*
2406 * We only need to check the data on READs, for WRITEs
2407 * protection data is automatically generated, not checked.
2408 */
2409 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002410 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002411 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2412 else
2413 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2414
James Smart9c6aa9d2013-05-31 17:03:39 -04002415 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002416 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2417 else
2418 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2419 }
2420
James Smartacd68592012-01-18 16:25:09 -05002421 /* setup DISEED with the rest of the info */
2422 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2423 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002424
James Smartacd68592012-01-18 16:25:09 -05002425 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2426 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2427
2428 /* Endianness conversion if necessary for DISEED */
2429 diseed->word2 = cpu_to_le32(diseed->word2);
2430 diseed->word3 = cpu_to_le32(diseed->word3);
2431
2432 /* advance bpl and increment sge count */
2433 num_sge++;
2434 sgl++;
2435
2436 /* assumption: caller has already run dma_map_sg on command data */
2437 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2438 physaddr = sg_dma_address(sgde);
2439 dma_len = sg_dma_len(sgde);
2440 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2441 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2442 if ((i + 1) == datasegcnt)
2443 bf_set(lpfc_sli4_sge_last, sgl, 1);
2444 else
2445 bf_set(lpfc_sli4_sge_last, sgl, 0);
2446 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2447 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2448
2449 sgl->sge_len = cpu_to_le32(dma_len);
2450 dma_offset += dma_len;
2451
2452 sgl++;
2453 num_sge++;
2454 }
2455
2456out:
2457 return num_sge;
2458}
2459
2460/**
2461 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2462 * @phba: The Hba for which this call is being executed.
2463 * @sc: pointer to scsi command we're working on
2464 * @sgl: pointer to buffer list for protection groups
2465 * @datacnt: number of segments of data that have been dma mapped
2466 * @protcnt: number of segment of protection data that have been dma mapped
2467 *
2468 * This function sets up SGL buffer list for protection groups of
2469 * type LPFC_PG_TYPE_DIF
2470 *
2471 * This is usually used when DIFs are in their own buffers,
2472 * separate from the data. The HBA can then by instructed
2473 * to place the DIFs in the outgoing stream. For read operations,
2474 * The HBA could extract the DIFs and place it in DIF buffers.
2475 *
2476 * The buffer list for this type consists of one or more of the
2477 * protection groups described below:
2478 * +-------------------------+
2479 * start of first prot group --> | DISEED |
2480 * +-------------------------+
2481 * | DIF (Prot SGE) |
2482 * +-------------------------+
2483 * | Data SGE |
2484 * +-------------------------+
2485 * |more Data SGE's ... (opt)|
2486 * +-------------------------+
2487 * start of new prot group --> | DISEED |
2488 * +-------------------------+
2489 * | ... |
2490 * +-------------------------+
2491 *
2492 * Note: It is assumed that both data and protection s/g buffers have been
2493 * mapped for DMA
2494 *
2495 * Returns the number of SGEs added to the SGL.
2496 **/
2497static int
2498lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2499 struct sli4_sge *sgl, int datacnt, int protcnt)
2500{
2501 struct scatterlist *sgde = NULL; /* s/g data entry */
2502 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2503 struct sli4_sge_diseed *diseed = NULL;
2504 dma_addr_t dataphysaddr, protphysaddr;
2505 unsigned short curr_data = 0, curr_prot = 0;
2506 unsigned int split_offset;
2507 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2508 unsigned int protgrp_blks, protgrp_bytes;
2509 unsigned int remainder, subtotal;
2510 int status;
2511 unsigned char pgdone = 0, alldone = 0;
2512 unsigned blksize;
2513 uint32_t reftag;
2514 uint8_t txop, rxop;
2515 uint32_t dma_len;
James Smart0829a192012-05-09 21:18:12 -04002516#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002517 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002518#endif
James Smartacd68592012-01-18 16:25:09 -05002519 uint32_t checking = 1;
2520 uint32_t dma_offset = 0;
2521 int num_sge = 0;
2522
2523 sgpe = scsi_prot_sglist(sc);
2524 sgde = scsi_sglist(sc);
2525
2526 if (!sgpe || !sgde) {
2527 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2528 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2529 sgpe, sgde);
2530 return 0;
2531 }
2532
2533 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2534 if (status)
2535 goto out;
2536
2537 /* extract some info from the scsi command */
2538 blksize = lpfc_cmd_blksize(sc);
2539 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2540
2541#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002542 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002543 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002544 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002545 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002546 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002547 checking = 0;
2548 }
2549#endif
2550
2551 split_offset = 0;
2552 do {
James Smart96f70772013-04-17 20:16:15 -04002553 /* Check to see if we ran out of space */
2554 if (num_sge >= (phba->cfg_total_seg_cnt - 2))
2555 return num_sge + 3;
2556
James Smartacd68592012-01-18 16:25:09 -05002557 /* setup DISEED with what we have */
2558 diseed = (struct sli4_sge_diseed *) sgl;
2559 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2560 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2561
2562 /* Endianness conversion if necessary */
2563 diseed->ref_tag = cpu_to_le32(reftag);
2564 diseed->ref_tag_tran = diseed->ref_tag;
2565
James Smart9c6aa9d2013-05-31 17:03:39 -04002566 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD)) {
James Smarta6887e22013-04-17 20:18:07 -04002567 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2568
2569 } else {
2570 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2571 /*
2572 * When in this mode, the hardware will replace
2573 * the guard tag from the host with a
2574 * newly generated good CRC for the wire.
2575 * Switch to raw mode here to avoid this
2576 * behavior. What the host sends gets put on the wire.
2577 */
2578 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2579 txop = BG_OP_RAW_MODE;
2580 rxop = BG_OP_RAW_MODE;
2581 }
2582 }
2583
2584
James Smart9c6aa9d2013-05-31 17:03:39 -04002585 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002586 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2587 else
2588 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2589
James Smartacd68592012-01-18 16:25:09 -05002590 /* setup DISEED with the rest of the info */
2591 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2592 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002593
James Smartacd68592012-01-18 16:25:09 -05002594 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2595 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2596
2597 /* Endianness conversion if necessary for DISEED */
2598 diseed->word2 = cpu_to_le32(diseed->word2);
2599 diseed->word3 = cpu_to_le32(diseed->word3);
2600
2601 /* advance sgl and increment bde count */
2602 num_sge++;
2603 sgl++;
2604
2605 /* setup the first BDE that points to protection buffer */
2606 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2607 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2608
2609 /* must be integer multiple of the DIF block length */
2610 BUG_ON(protgroup_len % 8);
2611
2612 /* Now setup DIF SGE */
2613 sgl->word2 = 0;
2614 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2615 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2616 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2617 sgl->word2 = cpu_to_le32(sgl->word2);
2618
2619 protgrp_blks = protgroup_len / 8;
2620 protgrp_bytes = protgrp_blks * blksize;
2621
2622 /* check if DIF SGE is crossing the 4K boundary; if so split */
2623 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2624 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2625 protgroup_offset += protgroup_remainder;
2626 protgrp_blks = protgroup_remainder / 8;
2627 protgrp_bytes = protgrp_blks * blksize;
2628 } else {
2629 protgroup_offset = 0;
2630 curr_prot++;
2631 }
2632
2633 num_sge++;
2634
2635 /* setup SGE's for data blocks associated with DIF data */
2636 pgdone = 0;
2637 subtotal = 0; /* total bytes processed for current prot grp */
2638 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002639 /* Check to see if we ran out of space */
2640 if (num_sge >= phba->cfg_total_seg_cnt)
2641 return num_sge + 1;
2642
James Smartacd68592012-01-18 16:25:09 -05002643 if (!sgde) {
2644 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2645 "9086 BLKGRD:%s Invalid data segment\n",
2646 __func__);
2647 return 0;
2648 }
2649 sgl++;
2650 dataphysaddr = sg_dma_address(sgde) + split_offset;
2651
2652 remainder = sg_dma_len(sgde) - split_offset;
2653
2654 if ((subtotal + remainder) <= protgrp_bytes) {
2655 /* we can use this whole buffer */
2656 dma_len = remainder;
2657 split_offset = 0;
2658
2659 if ((subtotal + remainder) == protgrp_bytes)
2660 pgdone = 1;
2661 } else {
2662 /* must split this buffer with next prot grp */
2663 dma_len = protgrp_bytes - subtotal;
2664 split_offset += dma_len;
2665 }
2666
2667 subtotal += dma_len;
2668
2669 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2670 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2671 bf_set(lpfc_sli4_sge_last, sgl, 0);
2672 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2673 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2674
2675 sgl->sge_len = cpu_to_le32(dma_len);
2676 dma_offset += dma_len;
2677
2678 num_sge++;
2679 curr_data++;
2680
2681 if (split_offset)
2682 break;
2683
2684 /* Move to the next s/g segment if possible */
2685 sgde = sg_next(sgde);
2686 }
2687
2688 if (protgroup_offset) {
2689 /* update the reference tag */
2690 reftag += protgrp_blks;
2691 sgl++;
2692 continue;
2693 }
2694
2695 /* are we done ? */
2696 if (curr_prot == protcnt) {
2697 bf_set(lpfc_sli4_sge_last, sgl, 1);
2698 alldone = 1;
2699 } else if (curr_prot < protcnt) {
2700 /* advance to next prot buffer */
2701 sgpe = sg_next(sgpe);
2702 sgl++;
2703
2704 /* update the reference tag */
2705 reftag += protgrp_blks;
2706 } else {
2707 /* if we're here, we have a bug */
2708 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2709 "9085 BLKGRD: bug in %s\n", __func__);
2710 }
2711
2712 } while (!alldone);
2713
2714out:
2715
2716 return num_sge;
2717}
2718
2719/**
2720 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2721 * @phba: The Hba for which this call is being executed.
2722 * @sc: pointer to scsi command we're working on
2723 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002724 * Given a SCSI command that supports DIF, determine composition of protection
2725 * groups involved in setting up buffer lists
2726 *
James Smartacd68592012-01-18 16:25:09 -05002727 * Returns: Protection group type (with or without DIF)
2728 *
2729 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002730static int
2731lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2732{
2733 int ret = LPFC_PG_TYPE_INVALID;
2734 unsigned char op = scsi_get_prot_op(sc);
2735
2736 switch (op) {
2737 case SCSI_PROT_READ_STRIP:
2738 case SCSI_PROT_WRITE_INSERT:
2739 ret = LPFC_PG_TYPE_NO_DIF;
2740 break;
2741 case SCSI_PROT_READ_INSERT:
2742 case SCSI_PROT_WRITE_STRIP:
2743 case SCSI_PROT_READ_PASS:
2744 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002745 ret = LPFC_PG_TYPE_DIF_BUF;
2746 break;
2747 default:
James Smart9c6aa9d2013-05-31 17:03:39 -04002748 if (phba)
2749 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2750 "9021 Unsupported protection op:%d\n",
2751 op);
James Smarte2a0a9d2008-12-04 22:40:02 -05002752 break;
2753 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002754 return ret;
2755}
2756
James Smartacd68592012-01-18 16:25:09 -05002757/**
James Smarta6887e22013-04-17 20:18:07 -04002758 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2759 * @phba: The Hba for which this call is being executed.
2760 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2761 *
2762 * Adjust the data length to account for how much data
2763 * is actually on the wire.
2764 *
2765 * returns the adjusted data length
2766 **/
2767static int
2768lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
2769 struct lpfc_scsi_buf *lpfc_cmd)
2770{
2771 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2772 int fcpdl;
2773
2774 fcpdl = scsi_bufflen(sc);
2775
2776 /* Check if there is protection data on the wire */
2777 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002778 /* Read check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002779 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2780 return fcpdl;
2781
2782 } else {
James Smart9c6aa9d2013-05-31 17:03:39 -04002783 /* Write check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002784 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2785 return fcpdl;
2786 }
2787
2788 /*
2789 * If we are in DIF Type 1 mode every data block has a 8 byte
James Smart9c6aa9d2013-05-31 17:03:39 -04002790 * DIF (trailer) attached to it. Must ajust FCP data length
2791 * to account for the protection data.
James Smarta6887e22013-04-17 20:18:07 -04002792 */
James Smart9c6aa9d2013-05-31 17:03:39 -04002793 fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
James Smarta6887e22013-04-17 20:18:07 -04002794
2795 return fcpdl;
2796}
2797
2798/**
James Smartacd68592012-01-18 16:25:09 -05002799 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2800 * @phba: The Hba for which this call is being executed.
2801 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2802 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002803 * This is the protection/DIF aware version of
2804 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2805 * two functions eventually, but for now, it's here
James Smartacd68592012-01-18 16:25:09 -05002806 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002807static int
James Smartacd68592012-01-18 16:25:09 -05002808lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smarte2a0a9d2008-12-04 22:40:02 -05002809 struct lpfc_scsi_buf *lpfc_cmd)
2810{
2811 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2812 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2813 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2814 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2815 uint32_t num_bde = 0;
2816 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2817 int prot_group_type = 0;
James Smarta6887e22013-04-17 20:18:07 -04002818 int fcpdl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002819
2820 /*
2821 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2822 * fcp_rsp regions to the first data bde entry
2823 */
2824 bpl += 2;
2825 if (scsi_sg_count(scsi_cmnd)) {
2826 /*
2827 * The driver stores the segment count returned from pci_map_sg
2828 * because this a count of dma-mappings used to map the use_sg
2829 * pages. They are not guaranteed to be the same for those
2830 * architectures that implement an IOMMU.
2831 */
2832 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2833 scsi_sglist(scsi_cmnd),
2834 scsi_sg_count(scsi_cmnd), datadir);
2835 if (unlikely(!datasegcnt))
2836 return 1;
2837
2838 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04002839
2840 /* First check if data segment count from SCSI Layer is good */
2841 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
2842 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002843
2844 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2845
2846 switch (prot_group_type) {
2847 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04002848
2849 /* Here we need to add a PDE5 and PDE6 to the count */
2850 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt)
2851 goto err;
2852
James Smarte2a0a9d2008-12-04 22:40:02 -05002853 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2854 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002855 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002856 if (num_bde < 2)
2857 goto err;
2858 break;
James Smart96f70772013-04-17 20:16:15 -04002859
2860 case LPFC_PG_TYPE_DIF_BUF:
James Smarte2a0a9d2008-12-04 22:40:02 -05002861 /*
2862 * This type indicates that protection buffers are
2863 * passed to the driver, so that needs to be prepared
2864 * for DMA
2865 */
2866 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2867 scsi_prot_sglist(scsi_cmnd),
2868 scsi_prot_sg_count(scsi_cmnd), datadir);
2869 if (unlikely(!protsegcnt)) {
2870 scsi_dma_unmap(scsi_cmnd);
2871 return 1;
2872 }
2873
2874 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04002875
2876 /*
2877 * There is a minimun of 4 BPLs used for every
2878 * protection data segment.
2879 */
2880 if ((lpfc_cmd->prot_seg_cnt * 4) >
2881 (phba->cfg_total_seg_cnt - 2))
2882 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002883
2884 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2885 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002886 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04002887 if ((num_bde < 3) ||
2888 (num_bde > phba->cfg_total_seg_cnt))
James Smarte2a0a9d2008-12-04 22:40:02 -05002889 goto err;
2890 break;
James Smart96f70772013-04-17 20:16:15 -04002891
James Smarte2a0a9d2008-12-04 22:40:02 -05002892 case LPFC_PG_TYPE_INVALID:
2893 default:
James Smart96f70772013-04-17 20:16:15 -04002894 scsi_dma_unmap(scsi_cmnd);
2895 lpfc_cmd->seg_cnt = 0;
2896
James Smarte2a0a9d2008-12-04 22:40:02 -05002897 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2898 "9022 Unexpected protection group %i\n",
2899 prot_group_type);
2900 return 1;
2901 }
2902 }
2903
2904 /*
2905 * Finish initializing those IOCB fields that are dependent on the
2906 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2907 * reinitialized since all iocb memory resources are used many times
2908 * for transmit, receive, and continuation bpl's.
2909 */
2910 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2911 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2912 iocb_cmd->ulpBdeCount = 1;
2913 iocb_cmd->ulpLe = 1;
2914
James Smarta6887e22013-04-17 20:18:07 -04002915 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002916 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2917
2918 /*
2919 * Due to difference in data length between DIF/non-DIF paths,
2920 * we need to set word 4 of IOCB here
2921 */
2922 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2923
2924 return 0;
2925err:
James Smart96f70772013-04-17 20:16:15 -04002926 if (lpfc_cmd->seg_cnt)
2927 scsi_dma_unmap(scsi_cmnd);
2928 if (lpfc_cmd->prot_seg_cnt)
2929 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2930 scsi_prot_sg_count(scsi_cmnd),
2931 scsi_cmnd->sc_data_direction);
2932
James Smarte2a0a9d2008-12-04 22:40:02 -05002933 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04002934 "9023 Cannot setup S/G List for HBA"
2935 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
2936 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
2937 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
James Smarte2a0a9d2008-12-04 22:40:02 -05002938 prot_group_type, num_bde);
James Smart96f70772013-04-17 20:16:15 -04002939
2940 lpfc_cmd->seg_cnt = 0;
2941 lpfc_cmd->prot_seg_cnt = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05002942 return 1;
2943}
2944
2945/*
James Smart737d4242013-04-17 20:14:49 -04002946 * This function calcuates the T10 DIF guard tag
2947 * on the specified data using a CRC algorithmn
2948 * using crc_t10dif.
2949 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002950static uint16_t
James Smart737d4242013-04-17 20:14:49 -04002951lpfc_bg_crc(uint8_t *data, int count)
2952{
2953 uint16_t crc = 0;
2954 uint16_t x;
2955
2956 crc = crc_t10dif(data, count);
2957 x = cpu_to_be16(crc);
2958 return x;
2959}
2960
2961/*
2962 * This function calcuates the T10 DIF guard tag
2963 * on the specified data using a CSUM algorithmn
2964 * using ip_compute_csum.
2965 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002966static uint16_t
James Smart737d4242013-04-17 20:14:49 -04002967lpfc_bg_csum(uint8_t *data, int count)
2968{
2969 uint16_t ret;
2970
2971 ret = ip_compute_csum(data, count);
2972 return ret;
2973}
2974
2975/*
2976 * This function examines the protection data to try to determine
2977 * what type of T10-DIF error occurred.
2978 */
Rashika Kheria7bfe7812014-09-03 12:55:36 -04002979static void
James Smart737d4242013-04-17 20:14:49 -04002980lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2981{
2982 struct scatterlist *sgpe; /* s/g prot entry */
2983 struct scatterlist *sgde; /* s/g data entry */
2984 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2985 struct scsi_dif_tuple *src = NULL;
2986 uint8_t *data_src = NULL;
2987 uint16_t guard_tag, guard_type;
2988 uint16_t start_app_tag, app_tag;
2989 uint32_t start_ref_tag, ref_tag;
2990 int prot, protsegcnt;
2991 int err_type, len, data_len;
2992 int chk_ref, chk_app, chk_guard;
2993 uint16_t sum;
2994 unsigned blksize;
2995
2996 err_type = BGS_GUARD_ERR_MASK;
2997 sum = 0;
2998 guard_tag = 0;
2999
3000 /* First check to see if there is protection data to examine */
3001 prot = scsi_get_prot_op(cmd);
3002 if ((prot == SCSI_PROT_READ_STRIP) ||
3003 (prot == SCSI_PROT_WRITE_INSERT) ||
3004 (prot == SCSI_PROT_NORMAL))
3005 goto out;
3006
3007 /* Currently the driver just supports ref_tag and guard_tag checking */
3008 chk_ref = 1;
3009 chk_app = 0;
3010 chk_guard = 0;
3011
3012 /* Setup a ptr to the protection data provided by the SCSI host */
3013 sgpe = scsi_prot_sglist(cmd);
3014 protsegcnt = lpfc_cmd->prot_seg_cnt;
3015
3016 if (sgpe && protsegcnt) {
3017
3018 /*
3019 * We will only try to verify guard tag if the segment
3020 * data length is a multiple of the blksize.
3021 */
3022 sgde = scsi_sglist(cmd);
3023 blksize = lpfc_cmd_blksize(cmd);
3024 data_src = (uint8_t *)sg_virt(sgde);
3025 data_len = sgde->length;
3026 if ((data_len & (blksize - 1)) == 0)
3027 chk_guard = 1;
3028 guard_type = scsi_host_get_guard(cmd->device->host);
3029
James Smarte85d8f92013-05-31 17:04:10 -04003030 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
James Smarta6887e22013-04-17 20:18:07 -04003031 start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
James Smart737d4242013-04-17 20:14:49 -04003032 start_app_tag = src->app_tag;
James Smart737d4242013-04-17 20:14:49 -04003033 len = sgpe->length;
3034 while (src && protsegcnt) {
3035 while (len) {
3036
3037 /*
3038 * First check to see if a protection data
3039 * check is valid
3040 */
3041 if ((src->ref_tag == 0xffffffff) ||
3042 (src->app_tag == 0xffff)) {
3043 start_ref_tag++;
3044 goto skipit;
3045 }
3046
James Smart9c6aa9d2013-05-31 17:03:39 -04003047 /* First Guard Tag checking */
3048 if (chk_guard) {
3049 guard_tag = src->guard_tag;
3050 if (lpfc_cmd_guard_csum(cmd))
3051 sum = lpfc_bg_csum(data_src,
3052 blksize);
3053 else
3054 sum = lpfc_bg_crc(data_src,
3055 blksize);
3056 if ((guard_tag != sum)) {
3057 err_type = BGS_GUARD_ERR_MASK;
3058 goto out;
3059 }
James Smart737d4242013-04-17 20:14:49 -04003060 }
3061
3062 /* Reference Tag checking */
3063 ref_tag = be32_to_cpu(src->ref_tag);
3064 if (chk_ref && (ref_tag != start_ref_tag)) {
3065 err_type = BGS_REFTAG_ERR_MASK;
3066 goto out;
3067 }
3068 start_ref_tag++;
3069
James Smart9c6aa9d2013-05-31 17:03:39 -04003070 /* App Tag checking */
3071 app_tag = src->app_tag;
3072 if (chk_app && (app_tag != start_app_tag)) {
3073 err_type = BGS_APPTAG_ERR_MASK;
3074 goto out;
James Smart737d4242013-04-17 20:14:49 -04003075 }
3076skipit:
3077 len -= sizeof(struct scsi_dif_tuple);
3078 if (len < 0)
3079 len = 0;
3080 src++;
3081
3082 data_src += blksize;
3083 data_len -= blksize;
3084
3085 /*
3086 * Are we at the end of the Data segment?
3087 * The data segment is only used for Guard
3088 * tag checking.
3089 */
3090 if (chk_guard && (data_len == 0)) {
3091 chk_guard = 0;
3092 sgde = sg_next(sgde);
3093 if (!sgde)
3094 goto out;
3095
3096 data_src = (uint8_t *)sg_virt(sgde);
3097 data_len = sgde->length;
3098 if ((data_len & (blksize - 1)) == 0)
3099 chk_guard = 1;
3100 }
3101 }
3102
3103 /* Goto the next Protection data segment */
3104 sgpe = sg_next(sgpe);
3105 if (sgpe) {
3106 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
3107 len = sgpe->length;
3108 } else {
3109 src = NULL;
3110 }
3111 protsegcnt--;
3112 }
3113 }
3114out:
3115 if (err_type == BGS_GUARD_ERR_MASK) {
3116 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3117 0x10, 0x1);
3118 cmd->result = DRIVER_SENSE << 24
3119 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3120 phba->bg_guard_err_cnt++;
3121 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3122 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
3123 (unsigned long)scsi_get_lba(cmd),
3124 sum, guard_tag);
3125
3126 } else if (err_type == BGS_REFTAG_ERR_MASK) {
3127 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3128 0x10, 0x3);
3129 cmd->result = DRIVER_SENSE << 24
3130 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3131
3132 phba->bg_reftag_err_cnt++;
3133 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3134 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
3135 (unsigned long)scsi_get_lba(cmd),
3136 ref_tag, start_ref_tag);
3137
3138 } else if (err_type == BGS_APPTAG_ERR_MASK) {
3139 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3140 0x10, 0x2);
3141 cmd->result = DRIVER_SENSE << 24
3142 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3143
3144 phba->bg_apptag_err_cnt++;
3145 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3146 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
3147 (unsigned long)scsi_get_lba(cmd),
3148 app_tag, start_app_tag);
3149 }
3150}
3151
3152
3153/*
James Smarte2a0a9d2008-12-04 22:40:02 -05003154 * This function checks for BlockGuard errors detected by
3155 * the HBA. In case of errors, the ASC/ASCQ fields in the
3156 * sense buffer will be set accordingly, paired with
3157 * ILLEGAL_REQUEST to signal to the kernel that the HBA
3158 * detected corruption.
3159 *
3160 * Returns:
3161 * 0 - No error found
3162 * 1 - BlockGuard error found
3163 * -1 - Internal error (bad profile, ...etc)
3164 */
3165static int
3166lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
3167 struct lpfc_iocbq *pIocbOut)
3168{
3169 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3170 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
3171 int ret = 0;
3172 uint32_t bghm = bgf->bghm;
3173 uint32_t bgstat = bgf->bgstat;
3174 uint64_t failing_sector = 0;
3175
James Smarte2a0a9d2008-12-04 22:40:02 -05003176 spin_lock(&_dump_buf_lock);
3177 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04003178 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
3179 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05003180 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04003181 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003182
3183 /* If we have a prot sgl, save the DIF buffer */
3184 if (lpfc_prot_group_type(phba, cmd) ==
3185 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04003186 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
3187 "Saving DIF for %u blocks to debugfs\n",
3188 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
3189 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003190 }
3191
3192 _dump_buf_done = 1;
3193 }
3194 spin_unlock(&_dump_buf_lock);
3195
3196 if (lpfc_bgs_get_invalid_prof(bgstat)) {
3197 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart737d4242013-04-17 20:14:49 -04003198 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3199 "9072 BLKGRD: Invalid BG Profile in cmd"
3200 " 0x%x lba 0x%llx blk cnt 0x%x "
3201 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3202 (unsigned long long)scsi_get_lba(cmd),
3203 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003204 ret = (-1);
3205 goto out;
3206 }
3207
3208 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
3209 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart737d4242013-04-17 20:14:49 -04003210 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3211 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
3212 " 0x%x lba 0x%llx blk cnt 0x%x "
3213 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3214 (unsigned long long)scsi_get_lba(cmd),
3215 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003216 ret = (-1);
3217 goto out;
3218 }
3219
3220 if (lpfc_bgs_get_guard_err(bgstat)) {
3221 ret = 1;
3222
3223 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3224 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003225 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003226 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3227 phba->bg_guard_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003228 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3229 "9055 BLKGRD: Guard Tag error in cmd"
3230 " 0x%x lba 0x%llx blk cnt 0x%x "
3231 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3232 (unsigned long long)scsi_get_lba(cmd),
3233 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003234 }
3235
3236 if (lpfc_bgs_get_reftag_err(bgstat)) {
3237 ret = 1;
3238
3239 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3240 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003241 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003242 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3243
3244 phba->bg_reftag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003245 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3246 "9056 BLKGRD: Ref Tag error in cmd"
3247 " 0x%x lba 0x%llx blk cnt 0x%x "
3248 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3249 (unsigned long long)scsi_get_lba(cmd),
3250 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003251 }
3252
3253 if (lpfc_bgs_get_apptag_err(bgstat)) {
3254 ret = 1;
3255
3256 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3257 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003258 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003259 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3260
3261 phba->bg_apptag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003262 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3263 "9061 BLKGRD: App Tag error in cmd"
3264 " 0x%x lba 0x%llx blk cnt 0x%x "
3265 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3266 (unsigned long long)scsi_get_lba(cmd),
3267 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003268 }
3269
3270 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
3271 /*
3272 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04003273 * field, and put the failing LBA in it.
3274 * This code assumes there was also a guard/app/ref tag error
3275 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05003276 */
James Smart7c56b9f2011-07-22 18:36:25 -04003277 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
3278 cmd->sense_buffer[8] = 0; /* Information descriptor type */
3279 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
3280 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05003281
3282 /* bghm is a "on the wire" FC frame based count */
3283 switch (scsi_get_prot_op(cmd)) {
3284 case SCSI_PROT_READ_INSERT:
3285 case SCSI_PROT_WRITE_STRIP:
3286 bghm /= cmd->device->sector_size;
3287 break;
3288 case SCSI_PROT_READ_STRIP:
3289 case SCSI_PROT_WRITE_INSERT:
3290 case SCSI_PROT_READ_PASS:
3291 case SCSI_PROT_WRITE_PASS:
3292 bghm /= (cmd->device->sector_size +
3293 sizeof(struct scsi_dif_tuple));
3294 break;
3295 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003296
3297 failing_sector = scsi_get_lba(cmd);
3298 failing_sector += bghm;
3299
James Smart7c56b9f2011-07-22 18:36:25 -04003300 /* Descriptor Information */
3301 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05003302 }
3303
3304 if (!ret) {
3305 /* No error was reported - problem in FW? */
James Smart737d4242013-04-17 20:14:49 -04003306 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3307 "9057 BLKGRD: Unknown error in cmd"
3308 " 0x%x lba 0x%llx blk cnt 0x%x "
3309 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3310 (unsigned long long)scsi_get_lba(cmd),
3311 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003312
James Smart737d4242013-04-17 20:14:49 -04003313 /* Calcuate what type of error it was */
3314 lpfc_calc_bg_err(phba, lpfc_cmd);
3315 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003316out:
3317 return ret;
3318}
3319
James Smartea2151b2008-09-07 11:52:10 -04003320/**
James Smartda0436e2009-05-22 14:51:39 -04003321 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3322 * @phba: The Hba for which this call is being executed.
3323 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3324 *
3325 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3326 * field of @lpfc_cmd for device with SLI-4 interface spec.
3327 *
3328 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003329 * 1 - Error
3330 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04003331 **/
3332static int
3333lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3334{
3335 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3336 struct scatterlist *sgel = NULL;
3337 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3338 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
James Smartfedd3b72011-02-16 12:39:24 -05003339 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04003340 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3341 dma_addr_t physaddr;
3342 uint32_t num_bde = 0;
3343 uint32_t dma_len;
3344 uint32_t dma_offset = 0;
3345 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05003346 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04003347
3348 /*
3349 * There are three possibilities here - use scatter-gather segment, use
3350 * the single mapping, or neither. Start the lpfc command prep by
3351 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3352 * data bde entry.
3353 */
3354 if (scsi_sg_count(scsi_cmnd)) {
3355 /*
3356 * The driver stores the segment count returned from pci_map_sg
3357 * because this a count of dma-mappings used to map the use_sg
3358 * pages. They are not guaranteed to be the same for those
3359 * architectures that implement an IOMMU.
3360 */
3361
3362 nseg = scsi_dma_map(scsi_cmnd);
3363 if (unlikely(!nseg))
3364 return 1;
3365 sgl += 1;
3366 /* clear the last flag in the fcp_rsp map entry */
3367 sgl->word2 = le32_to_cpu(sgl->word2);
3368 bf_set(lpfc_sli4_sge_last, sgl, 0);
3369 sgl->word2 = cpu_to_le32(sgl->word2);
3370 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05003371 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04003372 lpfc_cmd->seg_cnt = nseg;
3373 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04003374 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3375 " %s: Too many sg segments from "
3376 "dma_map_sg. Config %d, seg_cnt %d\n",
3377 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04003378 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04003379 lpfc_cmd->seg_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04003380 scsi_dma_unmap(scsi_cmnd);
3381 return 1;
3382 }
3383
3384 /*
3385 * The driver established a maximum scatter-gather segment count
3386 * during probe that limits the number of sg elements in any
3387 * single scsi command. Just run through the seg_cnt and format
3388 * the sge's.
3389 * When using SLI-3 the driver will try to fit all the BDEs into
3390 * the IOCB. If it can't then the BDEs get added to a BPL as it
3391 * does for SLI-2 mode.
3392 */
3393 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3394 physaddr = sg_dma_address(sgel);
3395 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04003396 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3397 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04003398 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04003399 if ((num_bde + 1) == nseg)
3400 bf_set(lpfc_sli4_sge_last, sgl, 1);
3401 else
3402 bf_set(lpfc_sli4_sge_last, sgl, 0);
3403 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04003404 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04003405 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05003406 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04003407 dma_offset += dma_len;
3408 sgl++;
3409 }
James Smartfedd3b72011-02-16 12:39:24 -05003410 /* setup the performance hint (first data BDE) if enabled */
3411 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
3412 bde = (struct ulp_bde64 *)
3413 &(iocb_cmd->unsli3.sli3Words[5]);
3414 bde->addrLow = first_data_sgl->addr_lo;
3415 bde->addrHigh = first_data_sgl->addr_hi;
3416 bde->tus.f.bdeSize =
3417 le32_to_cpu(first_data_sgl->sge_len);
3418 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3419 bde->tus.w = cpu_to_le32(bde->tus.w);
3420 }
James Smartda0436e2009-05-22 14:51:39 -04003421 } else {
3422 sgl += 1;
3423 /* clear the last flag in the fcp_rsp map entry */
3424 sgl->word2 = le32_to_cpu(sgl->word2);
3425 bf_set(lpfc_sli4_sge_last, sgl, 1);
3426 sgl->word2 = cpu_to_le32(sgl->word2);
3427 }
3428
3429 /*
3430 * Finish initializing those IOCB fields that are dependent on the
3431 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3432 * explicitly reinitialized.
3433 * all iocb memory resources are reused.
3434 */
3435 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3436
3437 /*
3438 * Due to difference in data length between DIF/non-DIF paths,
3439 * we need to set word 4 of IOCB here
3440 */
3441 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
James Smart1ba981f2014-02-20 09:56:45 -05003442
3443 /*
3444 * If the OAS driver feature is enabled and the lun is enabled for
3445 * OAS, set the oas iocb related flags.
3446 */
James Smartf38fa0b2014-04-04 13:52:21 -04003447 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
James Smart1ba981f2014-02-20 09:56:45 -05003448 scsi_cmnd->device->hostdata)->oas_enabled)
James Smart9bd2bff52014-09-03 12:57:30 -04003449 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
James Smartda0436e2009-05-22 14:51:39 -04003450 return 0;
3451}
3452
3453/**
James Smartacd68592012-01-18 16:25:09 -05003454 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3455 * @phba: The Hba for which this call is being executed.
3456 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3457 *
3458 * This is the protection/DIF aware version of
3459 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3460 * two functions eventually, but for now, it's here
3461 **/
3462static int
3463lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3464 struct lpfc_scsi_buf *lpfc_cmd)
3465{
3466 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3467 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3468 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3469 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart96f70772013-04-17 20:16:15 -04003470 uint32_t num_sge = 0;
James Smartacd68592012-01-18 16:25:09 -05003471 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3472 int prot_group_type = 0;
3473 int fcpdl;
3474
3475 /*
3476 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
James Smart96f70772013-04-17 20:16:15 -04003477 * fcp_rsp regions to the first data sge entry
James Smartacd68592012-01-18 16:25:09 -05003478 */
3479 if (scsi_sg_count(scsi_cmnd)) {
3480 /*
3481 * The driver stores the segment count returned from pci_map_sg
3482 * because this a count of dma-mappings used to map the use_sg
3483 * pages. They are not guaranteed to be the same for those
3484 * architectures that implement an IOMMU.
3485 */
3486 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3487 scsi_sglist(scsi_cmnd),
3488 scsi_sg_count(scsi_cmnd), datadir);
3489 if (unlikely(!datasegcnt))
3490 return 1;
3491
3492 sgl += 1;
3493 /* clear the last flag in the fcp_rsp map entry */
3494 sgl->word2 = le32_to_cpu(sgl->word2);
3495 bf_set(lpfc_sli4_sge_last, sgl, 0);
3496 sgl->word2 = cpu_to_le32(sgl->word2);
3497
3498 sgl += 1;
3499 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04003500
3501 /* First check if data segment count from SCSI Layer is good */
3502 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
3503 goto err;
James Smartacd68592012-01-18 16:25:09 -05003504
3505 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3506
3507 switch (prot_group_type) {
3508 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04003509 /* Here we need to add a DISEED to the count */
3510 if ((lpfc_cmd->seg_cnt + 1) > phba->cfg_total_seg_cnt)
3511 goto err;
3512
3513 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003514 datasegcnt);
James Smart96f70772013-04-17 20:16:15 -04003515
James Smartacd68592012-01-18 16:25:09 -05003516 /* we should have 2 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003517 if (num_sge < 2)
James Smartacd68592012-01-18 16:25:09 -05003518 goto err;
3519 break;
James Smart96f70772013-04-17 20:16:15 -04003520
3521 case LPFC_PG_TYPE_DIF_BUF:
James Smartacd68592012-01-18 16:25:09 -05003522 /*
3523 * This type indicates that protection buffers are
3524 * passed to the driver, so that needs to be prepared
3525 * for DMA
3526 */
3527 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3528 scsi_prot_sglist(scsi_cmnd),
3529 scsi_prot_sg_count(scsi_cmnd), datadir);
3530 if (unlikely(!protsegcnt)) {
3531 scsi_dma_unmap(scsi_cmnd);
3532 return 1;
3533 }
3534
3535 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04003536 /*
3537 * There is a minimun of 3 SGEs used for every
3538 * protection data segment.
3539 */
3540 if ((lpfc_cmd->prot_seg_cnt * 3) >
3541 (phba->cfg_total_seg_cnt - 2))
3542 goto err;
James Smartacd68592012-01-18 16:25:09 -05003543
James Smart96f70772013-04-17 20:16:15 -04003544 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003545 datasegcnt, protsegcnt);
James Smart96f70772013-04-17 20:16:15 -04003546
James Smartacd68592012-01-18 16:25:09 -05003547 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003548 if ((num_sge < 3) ||
3549 (num_sge > phba->cfg_total_seg_cnt))
James Smartacd68592012-01-18 16:25:09 -05003550 goto err;
3551 break;
James Smart96f70772013-04-17 20:16:15 -04003552
James Smartacd68592012-01-18 16:25:09 -05003553 case LPFC_PG_TYPE_INVALID:
3554 default:
James Smart96f70772013-04-17 20:16:15 -04003555 scsi_dma_unmap(scsi_cmnd);
3556 lpfc_cmd->seg_cnt = 0;
3557
James Smartacd68592012-01-18 16:25:09 -05003558 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3559 "9083 Unexpected protection group %i\n",
3560 prot_group_type);
3561 return 1;
3562 }
3563 }
3564
James Smart8012cc32012-10-31 14:44:49 -04003565 switch (scsi_get_prot_op(scsi_cmnd)) {
3566 case SCSI_PROT_WRITE_STRIP:
3567 case SCSI_PROT_READ_STRIP:
3568 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3569 break;
3570 case SCSI_PROT_WRITE_INSERT:
3571 case SCSI_PROT_READ_INSERT:
3572 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3573 break;
3574 case SCSI_PROT_WRITE_PASS:
3575 case SCSI_PROT_READ_PASS:
3576 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3577 break;
3578 }
3579
James Smartacd68592012-01-18 16:25:09 -05003580 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smartacd68592012-01-18 16:25:09 -05003581 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3582
3583 /*
3584 * Due to difference in data length between DIF/non-DIF paths,
3585 * we need to set word 4 of IOCB here
3586 */
3587 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
James Smartacd68592012-01-18 16:25:09 -05003588
James Smart9bd2bff52014-09-03 12:57:30 -04003589 /*
3590 * If the OAS driver feature is enabled and the lun is enabled for
3591 * OAS, set the oas iocb related flags.
3592 */
3593 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
3594 scsi_cmnd->device->hostdata)->oas_enabled)
3595 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
3596
James Smartacd68592012-01-18 16:25:09 -05003597 return 0;
3598err:
James Smart96f70772013-04-17 20:16:15 -04003599 if (lpfc_cmd->seg_cnt)
3600 scsi_dma_unmap(scsi_cmnd);
3601 if (lpfc_cmd->prot_seg_cnt)
3602 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3603 scsi_prot_sg_count(scsi_cmnd),
3604 scsi_cmnd->sc_data_direction);
3605
James Smartacd68592012-01-18 16:25:09 -05003606 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04003607 "9084 Cannot setup S/G List for HBA"
3608 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3609 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3610 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3611 prot_group_type, num_sge);
3612
3613 lpfc_cmd->seg_cnt = 0;
3614 lpfc_cmd->prot_seg_cnt = 0;
James Smartacd68592012-01-18 16:25:09 -05003615 return 1;
3616}
3617
3618/**
James Smart3772a992009-05-22 14:50:54 -04003619 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3620 * @phba: The Hba for which this call is being executed.
3621 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3622 *
3623 * This routine wraps the actual DMA mapping function pointer from the
3624 * lpfc_hba struct.
3625 *
3626 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003627 * 1 - Error
3628 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003629 **/
3630static inline int
3631lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3632{
3633 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3634}
3635
3636/**
James Smartacd68592012-01-18 16:25:09 -05003637 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3638 * using BlockGuard.
3639 * @phba: The Hba for which this call is being executed.
3640 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3641 *
3642 * This routine wraps the actual DMA mapping function pointer from the
3643 * lpfc_hba struct.
3644 *
3645 * Return codes:
3646 * 1 - Error
3647 * 0 - Success
3648 **/
3649static inline int
3650lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3651{
3652 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3653}
3654
3655/**
James Smart3621a712009-04-06 18:47:14 -04003656 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003657 * @phba: Pointer to hba context object.
3658 * @vport: Pointer to vport object.
3659 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3660 * @rsp_iocb: Pointer to response iocb object which reported error.
3661 *
3662 * This function posts an event when there is a SCSI command reporting
3663 * error from the scsi device.
3664 **/
3665static void
3666lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3667 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3668 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3669 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3670 uint32_t resp_info = fcprsp->rspStatus2;
3671 uint32_t scsi_status = fcprsp->rspStatus3;
3672 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3673 struct lpfc_fast_path_event *fast_path_evt = NULL;
3674 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3675 unsigned long flags;
3676
James Smart5989b8d2010-10-22 11:06:56 -04003677 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3678 return;
3679
James Smartea2151b2008-09-07 11:52:10 -04003680 /* If there is queuefull or busy condition send a scsi event */
3681 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3682 (cmnd->result == SAM_STAT_BUSY)) {
3683 fast_path_evt = lpfc_alloc_fast_evt(phba);
3684 if (!fast_path_evt)
3685 return;
3686 fast_path_evt->un.scsi_evt.event_type =
3687 FC_REG_SCSI_EVENT;
3688 fast_path_evt->un.scsi_evt.subcategory =
3689 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3690 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3691 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3692 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3693 &pnode->nlp_portname, sizeof(struct lpfc_name));
3694 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3695 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3696 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3697 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3698 fast_path_evt = lpfc_alloc_fast_evt(phba);
3699 if (!fast_path_evt)
3700 return;
3701 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3702 FC_REG_SCSI_EVENT;
3703 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3704 LPFC_EVENT_CHECK_COND;
3705 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3706 cmnd->device->lun;
3707 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3708 &pnode->nlp_portname, sizeof(struct lpfc_name));
3709 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3710 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3711 fast_path_evt->un.check_cond_evt.sense_key =
3712 cmnd->sense_buffer[2] & 0xf;
3713 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3714 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3715 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3716 fcpi_parm &&
3717 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3718 ((scsi_status == SAM_STAT_GOOD) &&
3719 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3720 /*
3721 * If status is good or resid does not match with fcp_param and
3722 * there is valid fcpi_parm, then there is a read_check error
3723 */
3724 fast_path_evt = lpfc_alloc_fast_evt(phba);
3725 if (!fast_path_evt)
3726 return;
3727 fast_path_evt->un.read_check_error.header.event_type =
3728 FC_REG_FABRIC_EVENT;
3729 fast_path_evt->un.read_check_error.header.subcategory =
3730 LPFC_EVENT_FCPRDCHKERR;
3731 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3732 &pnode->nlp_portname, sizeof(struct lpfc_name));
3733 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3734 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3735 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3736 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3737 fast_path_evt->un.read_check_error.fcpiparam =
3738 fcpi_parm;
3739 } else
3740 return;
3741
3742 fast_path_evt->vport = vport;
3743 spin_lock_irqsave(&phba->hbalock, flags);
3744 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3745 spin_unlock_irqrestore(&phba->hbalock, flags);
3746 lpfc_worker_wake_up(phba);
3747 return;
3748}
James Smart9bad7672008-12-04 22:39:02 -05003749
3750/**
James Smartf1126682009-06-10 17:22:44 -04003751 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003752 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003753 * @psb: The scsi buffer which is going to be un-mapped.
3754 *
3755 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003756 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003757 **/
dea31012005-04-17 16:05:31 -05003758static void
James Smartf1126682009-06-10 17:22:44 -04003759lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003760{
3761 /*
3762 * There are only two special cases to consider. (1) the scsi command
3763 * requested scatter-gather usage or (2) the scsi command allocated
3764 * a request buffer, but did not request use_sg. There is a third
3765 * case, but it does not require resource deallocation.
3766 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003767 if (psb->seg_cnt > 0)
3768 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003769 if (psb->prot_seg_cnt > 0)
3770 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3771 scsi_prot_sg_count(psb->pCmd),
3772 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003773}
3774
James Smart9bad7672008-12-04 22:39:02 -05003775/**
James Smart3621a712009-04-06 18:47:14 -04003776 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003777 * @vport: The virtual port for which this call is being executed.
3778 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3779 * @rsp_iocb: The response IOCB which contains FCP error.
3780 *
3781 * This routine is called to process response IOCB with status field
3782 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3783 * based upon SCSI and FCP error.
3784 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003785static void
James Smart2e0fef82007-06-17 19:56:36 -05003786lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3787 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003788{
3789 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3790 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3791 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003792 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003793 uint32_t resp_info = fcprsp->rspStatus2;
3794 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003795 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003796 uint32_t host_status = DID_OK;
3797 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05003798 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003799
James Smartea2151b2008-09-07 11:52:10 -04003800
dea31012005-04-17 16:05:31 -05003801 /*
3802 * If this is a task management command, there is no
3803 * scsi packet associated with this lpfc_cmd. The driver
3804 * consumes it.
3805 */
3806 if (fcpcmd->fcpCntl2) {
3807 scsi_status = 0;
3808 goto out;
3809 }
3810
James Smart6a9c52c2009-10-02 15:16:51 -04003811 if (resp_info & RSP_LEN_VALID) {
3812 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003813 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04003814 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3815 "2719 Invalid response length: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003816 "tgt x%x lun x%llx cmnd x%x rsplen x%x\n",
James Smart6a9c52c2009-10-02 15:16:51 -04003817 cmnd->device->id,
3818 cmnd->device->lun, cmnd->cmnd[0],
3819 rsplen);
3820 host_status = DID_ERROR;
3821 goto out;
3822 }
James Smarte40a02c2010-02-26 14:13:54 -05003823 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3824 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3825 "2757 Protocol failure detected during "
3826 "processing of FCP I/O op: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003827 "tgt x%x lun x%llx cmnd x%x rspInfo3 x%x\n",
James Smarte40a02c2010-02-26 14:13:54 -05003828 cmnd->device->id,
3829 cmnd->device->lun, cmnd->cmnd[0],
3830 fcprsp->rspInfo3);
3831 host_status = DID_ERROR;
3832 goto out;
3833 }
James Smart6a9c52c2009-10-02 15:16:51 -04003834 }
3835
James Smartc7743952006-12-02 13:34:42 -05003836 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3837 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3838 if (snslen > SCSI_SENSE_BUFFERSIZE)
3839 snslen = SCSI_SENSE_BUFFERSIZE;
3840
3841 if (resp_info & RSP_LEN_VALID)
3842 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3843 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3844 }
3845 lp = (uint32_t *)cmnd->sense_buffer;
3846
James Smartaa1c7ee2012-08-14 14:26:06 -04003847 /* special handling for under run conditions */
3848 if (!scsi_status && (resp_info & RESID_UNDER)) {
3849 /* don't log under runs if fcp set... */
3850 if (vport->cfg_log_verbose & LOG_FCP)
3851 logit = LOG_FCP_ERROR;
3852 /* unless operator says so */
3853 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3854 logit = LOG_FCP_UNDER;
3855 }
James Smartc7743952006-12-02 13:34:42 -05003856
James Smarte8b62012007-08-02 11:10:09 -04003857 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003858 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003859 "Data: x%x x%x x%x x%x x%x\n",
3860 cmnd->cmnd[0], scsi_status,
3861 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3862 be32_to_cpu(fcprsp->rspResId),
3863 be32_to_cpu(fcprsp->rspSnsLen),
3864 be32_to_cpu(fcprsp->rspRspLen),
3865 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003866
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003867 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05003868 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003869 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003870
James Smart73d91e52011-10-10 21:32:10 -04003871 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smarte2a0a9d2008-12-04 22:40:02 -05003872 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003873 "residual %d Data: x%x x%x x%x\n",
3874 be32_to_cpu(fcpcmd->fcpDl),
3875 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3876 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003877
3878 /*
James Smart7054a602007-04-25 09:52:34 -04003879 * If there is an under run check if under run reported by
3880 * storage array is same as the under run reported by HBA.
3881 * If this is not same, there is a dropped frame.
3882 */
3883 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3884 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003885 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003886 lpfc_printf_vlog(vport, KERN_WARNING,
3887 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003888 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003889 "and Underrun Data: x%x x%x x%x x%x\n",
3890 be32_to_cpu(fcpcmd->fcpDl),
3891 scsi_get_resid(cmnd), fcpi_parm,
3892 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003893 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003894 host_status = DID_ERROR;
3895 }
3896 /*
dea31012005-04-17 16:05:31 -05003897 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003898 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003899 * is not present, make sure the actual amount transferred is at
3900 * least the underflow value or fail.
3901 */
3902 if (!(resp_info & SNS_LEN_VALID) &&
3903 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003904 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3905 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003906 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003907 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003908 "underrun converted to error "
3909 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003910 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003911 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003912 host_status = DID_ERROR;
3913 }
3914 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003915 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003916 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003917 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003918 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003919 host_status = DID_ERROR;
3920
3921 /*
3922 * Check SLI validation that all the transfer was actually done
James Smart26373d22013-09-06 12:19:17 -04003923 * (fcpi_parm should be zero). Apply check only to reads.
dea31012005-04-17 16:05:31 -05003924 */
James Smart26373d22013-09-06 12:19:17 -04003925 } else if (fcpi_parm && (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -04003926 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart26373d22013-09-06 12:19:17 -04003927 "9029 FCP Read Check Error Data: "
James Smarteee88772010-09-29 11:19:08 -04003928 "x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003929 be32_to_cpu(fcpcmd->fcpDl),
3930 be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04003931 fcpi_parm, cmnd->cmnd[0], scsi_status);
3932 switch (scsi_status) {
3933 case SAM_STAT_GOOD:
3934 case SAM_STAT_CHECK_CONDITION:
3935 /* Fabric dropped a data frame. Fail any successful
3936 * command in which we detected dropped frames.
3937 * A status of good or some check conditions could
3938 * be considered a successful command.
3939 */
3940 host_status = DID_ERROR;
3941 break;
3942 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003943 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05003944 }
3945
3946 out:
3947 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04003948 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05003949}
3950
James Smart9bad7672008-12-04 22:39:02 -05003951/**
James Smart3621a712009-04-06 18:47:14 -04003952 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05003953 * @phba: The Hba for which this call is being executed.
3954 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04003955 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05003956 *
3957 * This routine assigns scsi command result by looking into response IOCB
3958 * status field appropriately. This routine handles QUEUE FULL condition as
3959 * well by ramping down device queue depth.
3960 **/
dea31012005-04-17 16:05:31 -05003961static void
3962lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3963 struct lpfc_iocbq *pIocbOut)
3964{
3965 struct lpfc_scsi_buf *lpfc_cmd =
3966 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003967 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05003968 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3969 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04003970 struct scsi_cmnd *cmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003971 int result;
James Smart5ffc2662009-11-18 15:39:44 -05003972 int depth;
James Smartfa61a542008-01-11 01:52:42 -05003973 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04003974 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04003975 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04003976 uint32_t queue_depth, scsi_id;
James Smart73d91e52011-10-10 21:32:10 -04003977 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05003978
James Smart75baf692010-06-08 18:31:21 -04003979 /* Sanity check on return of outstanding command */
3980 if (!(lpfc_cmd->pCmd))
3981 return;
3982 cmd = lpfc_cmd->pCmd;
3983 shost = cmd->device->host;
3984
James Smarte3d2b802012-08-14 14:25:43 -04003985 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
dea31012005-04-17 16:05:31 -05003986 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05003987 /* pick up SLI4 exhange busy status from HBA */
3988 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
3989
James Smart9a6b09c2012-03-01 22:37:42 -05003990#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3991 if (lpfc_cmd->prot_data_type) {
3992 struct scsi_dif_tuple *src = NULL;
3993
3994 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
3995 /*
3996 * Used to restore any changes to protection
3997 * data for error injection.
3998 */
3999 switch (lpfc_cmd->prot_data_type) {
4000 case LPFC_INJERR_REFTAG:
4001 src->ref_tag =
4002 lpfc_cmd->prot_data;
4003 break;
4004 case LPFC_INJERR_APPTAG:
4005 src->app_tag =
4006 (uint16_t)lpfc_cmd->prot_data;
4007 break;
4008 case LPFC_INJERR_GUARD:
4009 src->guard_tag =
4010 (uint16_t)lpfc_cmd->prot_data;
4011 break;
4012 default:
4013 break;
4014 }
4015
4016 lpfc_cmd->prot_data = 0;
4017 lpfc_cmd->prot_data_type = 0;
4018 lpfc_cmd->prot_data_segment = NULL;
4019 }
4020#endif
James Smart109f6ed2008-12-04 22:39:08 -05004021 if (pnode && NLP_CHK_NODE_ACT(pnode))
4022 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05004023
4024 if (lpfc_cmd->status) {
4025 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
4026 (lpfc_cmd->result & IOERR_DRVR_MASK))
4027 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4028 else if (lpfc_cmd->status >= IOSTAT_CNT)
4029 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smartaa1c7ee2012-08-14 14:26:06 -04004030 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
4031 !lpfc_cmd->fcp_rsp->rspStatus3 &&
4032 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
4033 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
James Smart73d91e52011-10-10 21:32:10 -04004034 logit = 0;
4035 else
4036 logit = LOG_FCP | LOG_FCP_UNDER;
4037 lpfc_printf_vlog(vport, KERN_WARNING, logit,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004038 "9030 FCP cmd x%x failed <%d/%lld> "
James Smart5a0d80f2012-05-09 21:18:20 -04004039 "status: x%x result: x%x "
4040 "sid: x%x did: x%x oxid: x%x "
4041 "Data: x%x x%x\n",
James Smart73d91e52011-10-10 21:32:10 -04004042 cmd->cmnd[0],
4043 cmd->device ? cmd->device->id : 0xffff,
4044 cmd->device ? cmd->device->lun : 0xffff,
4045 lpfc_cmd->status, lpfc_cmd->result,
James Smart3bf41ba2013-05-31 17:03:18 -04004046 vport->fc_myDID,
4047 (pnode) ? pnode->nlp_DID : 0,
James Smart5a0d80f2012-05-09 21:18:20 -04004048 phba->sli_rev == LPFC_SLI_REV4 ?
4049 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
James Smart73d91e52011-10-10 21:32:10 -04004050 pIocbOut->iocb.ulpContext,
4051 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05004052
4053 switch (lpfc_cmd->status) {
4054 case IOSTAT_FCP_RSP_ERROR:
4055 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05004056 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05004057 break;
4058 case IOSTAT_NPORT_BSY:
4059 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04004060 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04004061 fast_path_evt = lpfc_alloc_fast_evt(phba);
4062 if (!fast_path_evt)
4063 break;
4064 fast_path_evt->un.fabric_evt.event_type =
4065 FC_REG_FABRIC_EVENT;
4066 fast_path_evt->un.fabric_evt.subcategory =
4067 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
4068 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
4069 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4070 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
4071 &pnode->nlp_portname,
4072 sizeof(struct lpfc_name));
4073 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
4074 &pnode->nlp_nodename,
4075 sizeof(struct lpfc_name));
4076 }
4077 fast_path_evt->vport = vport;
4078 fast_path_evt->work_evt.evt =
4079 LPFC_EVT_FASTPATH_MGMT_EVT;
4080 spin_lock_irqsave(&phba->hbalock, flags);
4081 list_add_tail(&fast_path_evt->work_evt.evt_listp,
4082 &phba->work_list);
4083 spin_unlock_irqrestore(&phba->hbalock, flags);
4084 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004085 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004086 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05004087 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05004088 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
4089 lpfc_cmd->result ==
4090 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
4091 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
4092 lpfc_cmd->result ==
4093 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
4094 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
4095 break;
4096 }
James Smartd7c255b2008-08-24 21:50:00 -04004097 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05004098 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04004099 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
4100 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
James Smart92d7f7b2007-06-17 19:56:38 -05004101 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04004102 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05004103 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004104 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
4105 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
4106 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
4107 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
4108 /*
4109 * This is a response for a BG enabled
4110 * cmd. Parse BG error
4111 */
4112 lpfc_parse_bg_err(phba, lpfc_cmd,
4113 pIocbOut);
4114 break;
4115 } else {
4116 lpfc_printf_vlog(vport, KERN_WARNING,
4117 LOG_BG,
4118 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04004119 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05004120 }
4121 }
James Smart1151e3e2011-02-16 12:39:35 -05004122 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
4123 && (phba->sli_rev == LPFC_SLI_REV4)
4124 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
4125 /* This IO was aborted by the target, we don't
4126 * know the rxid and because we did not send the
4127 * ABTS we cannot generate and RRQ.
4128 */
4129 lpfc_set_rrq_active(phba, pnode,
James Smartee0f4fe2012-05-09 21:19:14 -04004130 lpfc_cmd->cur_iocbq.sli4_lxritag,
4131 0, 0);
James Smart1151e3e2011-02-16 12:39:35 -05004132 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004133 /* else: fall through */
dea31012005-04-17 16:05:31 -05004134 default:
4135 cmd->result = ScsiResult(DID_ERROR, 0);
4136 break;
4137 }
4138
James Smart58da1ff2008-04-07 10:15:56 -04004139 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004140 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04004141 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
4142 SAM_STAT_BUSY);
James Smartab56dc22011-02-16 12:39:57 -05004143 } else
dea31012005-04-17 16:05:31 -05004144 cmd->result = ScsiResult(DID_OK, 0);
dea31012005-04-17 16:05:31 -05004145
4146 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4147 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
4148
James Smarte8b62012007-08-02 11:10:09 -04004149 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004150 "0710 Iodone <%d/%llu> cmd %p, error "
James Smarte8b62012007-08-02 11:10:09 -04004151 "x%x SNS x%x x%x Data: x%x x%x\n",
4152 cmd->device->id, cmd->device->lun, cmd,
4153 cmd->result, *lp, *(lp + 3), cmd->retries,
4154 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05004155 }
4156
James Smartea2151b2008-09-07 11:52:10 -04004157 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004158 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04004159 if (vport->cfg_max_scsicmpl_time &&
4160 time_after(jiffies, lpfc_cmd->start_time +
4161 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04004162 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004163 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4164 if (pnode->cmd_qdepth >
4165 atomic_read(&pnode->cmd_pending) &&
4166 (atomic_read(&pnode->cmd_pending) >
4167 LPFC_MIN_TGT_QDEPTH) &&
4168 ((cmd->cmnd[0] == READ_10) ||
4169 (cmd->cmnd[0] == WRITE_10)))
4170 pnode->cmd_qdepth =
4171 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04004172
James Smart109f6ed2008-12-04 22:39:08 -05004173 pnode->last_change_time = jiffies;
4174 }
James Smarta257bf92009-04-06 18:48:10 -04004175 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004176 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smart7dc517d2010-07-14 15:32:10 -04004177 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
James Smart977b5a02008-09-07 11:52:04 -04004178 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05004179 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04004180 spin_lock_irqsave(shost->host_lock, flags);
James Smart7dc517d2010-07-14 15:32:10 -04004181 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
4182 / 100;
4183 depth = depth ? depth : 1;
4184 pnode->cmd_qdepth += depth;
4185 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
4186 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05004187 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04004188 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004189 }
James Smart977b5a02008-09-07 11:52:04 -04004190 }
4191
James Smart1dcb58e2007-04-25 09:51:30 -04004192 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04004193
4194 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
4195 queue_depth = cmd->device->queue_depth;
4196 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05004197 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004198
Jamie Wellnitzb8086082006-02-28 22:33:12 -05004199 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart876dd7d2012-09-29 11:31:28 -04004200 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004201 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04004202 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004203
James Smartfa61a542008-01-11 01:52:42 -05004204 /*
4205 * If there is a thread waiting for command completion
4206 * wake up the thread.
4207 */
James Smarta257bf92009-04-06 18:48:10 -04004208 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004209 if (lpfc_cmd->waitq)
4210 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04004211 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05004212 lpfc_release_scsi_buf(phba, lpfc_cmd);
4213 return;
4214 }
4215
James Smart876dd7d2012-09-29 11:31:28 -04004216 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004217 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04004218 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004219
James Smartfa61a542008-01-11 01:52:42 -05004220 /*
4221 * If there is a thread waiting for command completion
4222 * wake up the thread.
4223 */
James Smarta257bf92009-04-06 18:48:10 -04004224 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004225 if (lpfc_cmd->waitq)
4226 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04004227 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004228
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004229 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004230}
4231
James Smart34b02dc2008-08-24 21:49:55 -04004232/**
James Smart3621a712009-04-06 18:47:14 -04004233 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04004234 * @data: A pointer to the immediate command data portion of the IOCB.
4235 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
4236 *
4237 * The routine copies the entire FCP command from @fcp_cmnd to @data while
4238 * byte swapping the data to big endian format for transmission on the wire.
4239 **/
4240static void
4241lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
4242{
4243 int i, j;
4244 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
4245 i += sizeof(uint32_t), j++) {
4246 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
4247 }
4248}
4249
James Smart9bad7672008-12-04 22:39:02 -05004250/**
James Smartf1126682009-06-10 17:22:44 -04004251 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004252 * @vport: The virtual port for which this call is being executed.
4253 * @lpfc_cmd: The scsi command which needs to send.
4254 * @pnode: Pointer to lpfc_nodelist.
4255 *
4256 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04004257 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004258 **/
dea31012005-04-17 16:05:31 -05004259static void
James Smartf1126682009-06-10 17:22:44 -04004260lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05004261 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05004262{
James Smart2e0fef82007-06-17 19:56:36 -05004263 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004264 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4265 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4266 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4267 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
4268 int datadir = scsi_cmnd->sc_data_direction;
James Smart027140e2012-08-03 12:35:44 -04004269 uint8_t *ptr;
4270 bool sli4;
James Smart98bbf5f2013-09-06 12:18:45 -04004271 uint32_t fcpdl;
dea31012005-04-17 16:05:31 -05004272
James Smart58da1ff2008-04-07 10:15:56 -04004273 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4274 return;
4275
dea31012005-04-17 16:05:31 -05004276 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04004277 /* clear task management bits */
4278 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05004279
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04004280 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4281 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004282
James Smart027140e2012-08-03 12:35:44 -04004283 ptr = &fcp_cmnd->fcpCdb[0];
4284 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4285 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4286 ptr += scsi_cmnd->cmd_len;
4287 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4288 }
4289
Christoph Hellwig50668632014-10-30 14:30:06 +01004290 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
dea31012005-04-17 16:05:31 -05004291
James Smart027140e2012-08-03 12:35:44 -04004292 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
James Smart6acb3482014-04-04 13:51:25 -04004293 piocbq->iocb.un.fcpi.fcpi_XRdy = 0;
James Smart027140e2012-08-03 12:35:44 -04004294
dea31012005-04-17 16:05:31 -05004295 /*
4296 * There are three possibilities here - use scatter-gather segment, use
4297 * the single mapping, or neither. Start the lpfc command prep by
4298 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4299 * data bde entry.
4300 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05004301 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05004302 if (datadir == DMA_TO_DEVICE) {
4303 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart182ba752013-07-15 18:34:05 -04004304 iocb_cmd->ulpPU = PARM_READ_CHECK;
James Smart3cb01c52013-07-15 18:35:04 -04004305 if (vport->cfg_first_burst_size &&
4306 (pnode->nlp_flag & NLP_FIRSTBURST)) {
James Smart98bbf5f2013-09-06 12:18:45 -04004307 fcpdl = scsi_bufflen(scsi_cmnd);
4308 if (fcpdl < vport->cfg_first_burst_size)
4309 piocbq->iocb.un.fcpi.fcpi_XRdy = fcpdl;
4310 else
4311 piocbq->iocb.un.fcpi.fcpi_XRdy =
4312 vport->cfg_first_burst_size;
James Smart3cb01c52013-07-15 18:35:04 -04004313 }
dea31012005-04-17 16:05:31 -05004314 fcp_cmnd->fcpCntl3 = WRITE_DATA;
4315 phba->fc4OutputRequests++;
4316 } else {
4317 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4318 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05004319 fcp_cmnd->fcpCntl3 = READ_DATA;
4320 phba->fc4InputRequests++;
4321 }
4322 } else {
4323 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4324 iocb_cmd->un.fcpi.fcpi_parm = 0;
4325 iocb_cmd->ulpPU = 0;
4326 fcp_cmnd->fcpCntl3 = 0;
4327 phba->fc4ControlRequests++;
4328 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004329 if (phba->sli_rev == 3 &&
4330 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004331 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004332 /*
4333 * Finish initializing those IOCB fields that are independent
4334 * of the scsi_cmnd request_buffer
4335 */
4336 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart027140e2012-08-03 12:35:44 -04004337 if (sli4)
James Smart6d368e52011-05-24 11:44:12 -04004338 piocbq->iocb.ulpContext =
4339 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05004340 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4341 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05004342 else
4343 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05004344
4345 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4346 piocbq->context1 = lpfc_cmd;
4347 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4348 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05004349 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05004350}
4351
James Smart9bad7672008-12-04 22:39:02 -05004352/**
James Smart6d368e52011-05-24 11:44:12 -04004353 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004354 * @vport: The virtual port for which this call is being executed.
4355 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4356 * @lun: Logical unit number.
4357 * @task_mgmt_cmd: SCSI task management command.
4358 *
James Smart3772a992009-05-22 14:50:54 -04004359 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4360 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004361 *
4362 * Return codes:
4363 * 0 - Error
4364 * 1 - Success
4365 **/
dea31012005-04-17 16:05:31 -05004366static int
James Smartf1126682009-06-10 17:22:44 -04004367lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05004368 struct lpfc_scsi_buf *lpfc_cmd,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004369 uint64_t lun,
dea31012005-04-17 16:05:31 -05004370 uint8_t task_mgmt_cmd)
4371{
dea31012005-04-17 16:05:31 -05004372 struct lpfc_iocbq *piocbq;
4373 IOCB_t *piocb;
4374 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04004375 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05004376 struct lpfc_nodelist *ndlp = rdata->pnode;
4377
James Smart58da1ff2008-04-07 10:15:56 -04004378 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4379 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05004380 return 0;
dea31012005-04-17 16:05:31 -05004381
dea31012005-04-17 16:05:31 -05004382 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004383 piocbq->vport = vport;
4384
dea31012005-04-17 16:05:31 -05004385 piocb = &piocbq->iocb;
4386
4387 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04004388 /* Clear out any old data in the FCP command area */
4389 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4390 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004391 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05004392 if (vport->phba->sli_rev == 3 &&
4393 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004394 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004395 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05004396 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04004397 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4398 piocb->ulpContext =
4399 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4400 }
James Smart53151bb2013-10-10 12:24:07 -04004401 piocb->ulpFCP2Rcvy = (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) ? 1 : 0;
dea31012005-04-17 16:05:31 -05004402 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
James Smartf9226c22014-02-20 09:57:28 -05004403 piocb->ulpPU = 0;
4404 piocb->un.fcpi.fcpi_parm = 0;
dea31012005-04-17 16:05:31 -05004405
4406 /* ulpTimeout is only one byte */
4407 if (lpfc_cmd->timeout > 0xff) {
4408 /*
4409 * Do not timeout the command at the firmware level.
4410 * The driver will provide the timeout mechanism.
4411 */
4412 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04004413 } else
dea31012005-04-17 16:05:31 -05004414 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04004415
4416 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4417 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004418
James Smart2e0fef82007-06-17 19:56:36 -05004419 return 1;
dea31012005-04-17 16:05:31 -05004420}
4421
James Smart9bad7672008-12-04 22:39:02 -05004422/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004423 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04004424 * @phba: The hba struct for which this call is being executed.
4425 * @dev_grp: The HBA PCI-Device group number.
4426 *
4427 * This routine sets up the SCSI interface API function jump table in @phba
4428 * struct.
4429 * Returns: 0 - success, -ENODEV - failure.
4430 **/
4431int
4432lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4433{
4434
James Smartf1126682009-06-10 17:22:44 -04004435 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4436 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04004437
James Smart3772a992009-05-22 14:50:54 -04004438 switch (dev_grp) {
4439 case LPFC_PCI_DEV_LP:
4440 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4441 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05004442 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004443 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05004444 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004445 break;
James Smartda0436e2009-05-22 14:51:39 -04004446 case LPFC_PCI_DEV_OC:
4447 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4448 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05004449 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004450 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05004451 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004452 break;
James Smart3772a992009-05-22 14:50:54 -04004453 default:
4454 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4455 "1418 Invalid HBA PCI-device group: 0x%x\n",
4456 dev_grp);
4457 return -ENODEV;
4458 break;
4459 }
James Smart3772a992009-05-22 14:50:54 -04004460 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05004461 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04004462 return 0;
4463}
4464
4465/**
James Smart3621a712009-04-06 18:47:14 -04004466 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05004467 * @phba: The Hba for which this call is being executed.
4468 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4469 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4470 *
4471 * This routine is IOCB completion routine for device reset and target reset
4472 * routine. This routine release scsi buffer associated with lpfc_cmd.
4473 **/
James Smart7054a602007-04-25 09:52:34 -04004474static void
4475lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4476 struct lpfc_iocbq *cmdiocbq,
4477 struct lpfc_iocbq *rspiocbq)
4478{
4479 struct lpfc_scsi_buf *lpfc_cmd =
4480 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4481 if (lpfc_cmd)
4482 lpfc_release_scsi_buf(phba, lpfc_cmd);
4483 return;
4484}
4485
James Smart9bad7672008-12-04 22:39:02 -05004486/**
James Smart3621a712009-04-06 18:47:14 -04004487 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05004488 * @host: The scsi host for which this call is being executed.
4489 *
4490 * This routine provides module information about hba.
4491 *
4492 * Reutrn code:
4493 * Pointer to char - Success.
4494 **/
dea31012005-04-17 16:05:31 -05004495const char *
4496lpfc_info(struct Scsi_Host *host)
4497{
James Smart2e0fef82007-06-17 19:56:36 -05004498 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4499 struct lpfc_hba *phba = vport->phba;
James Smart8b68cd52012-09-29 11:32:37 -04004500 int len, link_speed = 0;
dea31012005-04-17 16:05:31 -05004501 static char lpfcinfobuf[384];
4502
4503 memset(lpfcinfobuf,0,384);
4504 if (phba && phba->pcidev){
4505 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4506 len = strlen(lpfcinfobuf);
4507 snprintf(lpfcinfobuf + len,
4508 384-len,
4509 " on PCI bus %02x device %02x irq %d",
4510 phba->pcidev->bus->number,
4511 phba->pcidev->devfn,
4512 phba->pcidev->irq);
4513 len = strlen(lpfcinfobuf);
4514 if (phba->Port[0]) {
4515 snprintf(lpfcinfobuf + len,
4516 384-len,
4517 " port %s",
4518 phba->Port);
4519 }
James Smart65467b62010-01-26 23:08:29 -05004520 len = strlen(lpfcinfobuf);
James Smart8b68cd52012-09-29 11:32:37 -04004521 if (phba->sli_rev <= LPFC_SLI_REV3) {
4522 link_speed = lpfc_sli_port_speed_get(phba);
4523 } else {
4524 if (phba->sli4_hba.link_state.logical_speed)
4525 link_speed =
4526 phba->sli4_hba.link_state.logical_speed;
4527 else
4528 link_speed = phba->sli4_hba.link_state.speed;
James Smart65467b62010-01-26 23:08:29 -05004529 }
James Smart8b68cd52012-09-29 11:32:37 -04004530 if (link_speed != 0)
4531 snprintf(lpfcinfobuf + len, 384-len,
4532 " Logical Link Speed: %d Mbps", link_speed);
dea31012005-04-17 16:05:31 -05004533 }
4534 return lpfcinfobuf;
4535}
4536
James Smart9bad7672008-12-04 22:39:02 -05004537/**
James Smart3621a712009-04-06 18:47:14 -04004538 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05004539 * @phba: The Hba for which this call is being executed.
4540 *
4541 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4542 * The default value of cfg_poll_tmo is 10 milliseconds.
4543 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004544static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4545{
4546 unsigned long poll_tmo_expires =
4547 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4548
James Smart0e9bb8d2013-03-01 16:35:12 -05004549 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004550 mod_timer(&phba->fcp_poll_timer,
4551 poll_tmo_expires);
4552}
4553
James Smart9bad7672008-12-04 22:39:02 -05004554/**
James Smart3621a712009-04-06 18:47:14 -04004555 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004556 * @phba: The Hba for which this call is being executed.
4557 *
4558 * This routine starts the fcp_poll_timer of @phba.
4559 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004560void lpfc_poll_start_timer(struct lpfc_hba * phba)
4561{
4562 lpfc_poll_rearm_timer(phba);
4563}
4564
James Smart9bad7672008-12-04 22:39:02 -05004565/**
James Smart3621a712009-04-06 18:47:14 -04004566 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05004567 * @ptr: Map to lpfc_hba data structure pointer.
4568 *
4569 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4570 * and FCP Ring interrupt is disable.
4571 **/
4572
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004573void lpfc_poll_timeout(unsigned long ptr)
4574{
James Smart2e0fef82007-06-17 19:56:36 -05004575 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004576
4577 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004578 lpfc_sli_handle_fast_ring_event(phba,
4579 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4580
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004581 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4582 lpfc_poll_rearm_timer(phba);
4583 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004584}
4585
James Smart9bad7672008-12-04 22:39:02 -05004586/**
James Smart3621a712009-04-06 18:47:14 -04004587 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05004588 * @cmnd: Pointer to scsi_cmnd data structure.
4589 * @done: Pointer to done routine.
4590 *
4591 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4592 * This routine prepares an IOCB from scsi command and provides to firmware.
4593 * The @done callback is invoked after driver finished processing the command.
4594 *
4595 * Return value :
4596 * 0 - Success
4597 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4598 **/
dea31012005-04-17 16:05:31 -05004599static int
James Smartb9a7c632012-08-03 12:35:24 -04004600lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004601{
James Smart2e0fef82007-06-17 19:56:36 -05004602 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4603 struct lpfc_hba *phba = vport->phba;
James Smart1ba981f2014-02-20 09:56:45 -05004604 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004605 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004606 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004607 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004608 int err;
dea31012005-04-17 16:05:31 -05004609
James Smart1ba981f2014-02-20 09:56:45 -05004610 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004611 err = fc_remote_port_chkready(rport);
4612 if (err) {
4613 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004614 goto out_fail_command;
4615 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004616 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004617
James Smartbf086112011-08-21 21:48:13 -04004618 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004619 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004620
James Smart6a9c52c2009-10-02 15:16:51 -04004621 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4622 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4623 " op:%02x str=%s without registering for"
4624 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004625 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4626 dif_op_str[scsi_get_prot_op(cmnd)]);
4627 goto out_fail_command;
4628 }
4629
dea31012005-04-17 16:05:31 -05004630 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004631 * Catch race where our node has transitioned, but the
4632 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004633 */
James Smart6b415f52012-06-12 13:54:59 -04004634 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4635 goto out_tgt_busy;
James Smart7dc517d2010-07-14 15:32:10 -04004636 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
Mike Christie34963432011-02-25 14:04:28 -06004637 goto out_tgt_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05004638
James Smart19ca7602010-11-20 23:11:55 -05004639 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05004640 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004641 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004642
James Smarte8b62012007-08-02 11:10:09 -04004643 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4644 "0707 driver's buffer pool is empty, "
4645 "IO busied\n");
dea31012005-04-17 16:05:31 -05004646 goto out_host_busy;
4647 }
4648
4649 /*
4650 * Store the midlayer's command structure for the completion phase
4651 * and complete the command initialization.
4652 */
4653 lpfc_cmd->pCmd = cmnd;
4654 lpfc_cmd->rdata = rdata;
4655 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04004656 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05004657 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
dea31012005-04-17 16:05:31 -05004658
James Smarte2a0a9d2008-12-04 22:40:02 -05004659 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004660 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004661 lpfc_printf_vlog(vport,
4662 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004663 "9033 BLKGRD: rcvd %s cmd:x%x "
4664 "sector x%llx cnt %u pt %x\n",
4665 dif_op_str[scsi_get_prot_op(cmnd)],
4666 cmnd->cmnd[0],
4667 (unsigned long long)scsi_get_lba(cmnd),
4668 blk_rq_sectors(cmnd->request),
4669 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004670 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004671 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4672 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004673 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004674 lpfc_printf_vlog(vport,
4675 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004676 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4677 "x%x sector x%llx cnt %u pt %x\n",
4678 cmnd->cmnd[0],
4679 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004680 blk_rq_sectors(cmnd->request),
James Smart26134702012-08-14 14:25:50 -04004681 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004682 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004683 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4684 }
4685
dea31012005-04-17 16:05:31 -05004686 if (err)
4687 goto out_host_busy_free_buf;
4688
James Smart2e0fef82007-06-17 19:56:36 -05004689 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004690
James Smart977b5a02008-09-07 11:52:04 -04004691 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04004692 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004693 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05004694 if (err) {
4695 atomic_dec(&ndlp->cmd_pending);
James Smart76f96b62013-12-17 20:29:01 -05004696 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4697 "3376 FCP could not issue IOCB err %x"
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004698 "FCP cmd x%x <%d/%llu> "
James Smart76f96b62013-12-17 20:29:01 -05004699 "sid: x%x did: x%x oxid: x%x "
4700 "Data: x%x x%x x%x x%x\n",
4701 err, cmnd->cmnd[0],
4702 cmnd->device ? cmnd->device->id : 0xffff,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004703 cmnd->device ? cmnd->device->lun : (u64) -1,
James Smart76f96b62013-12-17 20:29:01 -05004704 vport->fc_myDID, ndlp->nlp_DID,
4705 phba->sli_rev == LPFC_SLI_REV4 ?
4706 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
4707 lpfc_cmd->cur_iocbq.iocb.ulpContext,
4708 lpfc_cmd->cur_iocbq.iocb.ulpIoTag,
4709 lpfc_cmd->cur_iocbq.iocb.ulpTimeout,
4710 (uint32_t)
4711 (cmnd->request->timeout / 1000));
4712
4713
dea31012005-04-17 16:05:31 -05004714 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004715 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004716 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004717 lpfc_sli_handle_fast_ring_event(phba,
4718 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4719
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004720 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4721 lpfc_poll_rearm_timer(phba);
4722 }
4723
dea31012005-04-17 16:05:31 -05004724 return 0;
4725
4726 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04004727 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004728 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004729 out_host_busy:
4730 return SCSI_MLQUEUE_HOST_BUSY;
4731
Mike Christie34963432011-02-25 14:04:28 -06004732 out_tgt_busy:
4733 return SCSI_MLQUEUE_TARGET_BUSY;
4734
dea31012005-04-17 16:05:31 -05004735 out_fail_command:
James Smartb9a7c632012-08-03 12:35:24 -04004736 cmnd->scsi_done(cmnd);
dea31012005-04-17 16:05:31 -05004737 return 0;
4738}
4739
Jeff Garzikf2812332010-11-16 02:10:29 -05004740
James Smart9bad7672008-12-04 22:39:02 -05004741/**
James Smart3621a712009-04-06 18:47:14 -04004742 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004743 * @cmnd: Pointer to scsi_cmnd data structure.
4744 *
4745 * This routine aborts @cmnd pending in base driver.
4746 *
4747 * Return code :
4748 * 0x2003 - Error
4749 * 0x2002 - Success
4750 **/
dea31012005-04-17 16:05:31 -05004751static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004752lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004753{
James Smart2e0fef82007-06-17 19:56:36 -05004754 struct Scsi_Host *shost = cmnd->device->host;
4755 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4756 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004757 struct lpfc_iocbq *iocb;
4758 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004759 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004760 IOCB_t *cmd, *icmd;
James Smart3a707302012-06-12 13:54:42 -04004761 int ret = SUCCESS, status = 0;
James Smart98912dd2014-04-04 13:52:31 -04004762 struct lpfc_sli_ring *pring_s4;
4763 int ring_number, ret_val;
4764 unsigned long flags, iflags;
James Smartfa61a542008-01-11 01:52:42 -05004765 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004766
James Smart3a707302012-06-12 13:54:42 -04004767 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004768 if (status != 0 && status != SUCCESS)
James Smart3a707302012-06-12 13:54:42 -04004769 return status;
James Smart4f2e66c2012-05-09 21:17:07 -04004770
James Smart876dd7d2012-09-29 11:31:28 -04004771 spin_lock_irqsave(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004772 /* driver queued commands are in process of being flushed */
4773 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
James Smart876dd7d2012-09-29 11:31:28 -04004774 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004775 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4776 "3168 SCSI Layer abort requested I/O has been "
4777 "flushed by LLD.\n");
4778 return FAILED;
4779 }
4780
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004781 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smart92e3af62012-08-14 14:26:28 -04004782 if (!lpfc_cmd || !lpfc_cmd->pCmd) {
James Smart876dd7d2012-09-29 11:31:28 -04004783 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarteee88772010-09-29 11:19:08 -04004784 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4785 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004786 "x%x ID %d LUN %llu\n",
James Smart3a707302012-06-12 13:54:42 -04004787 SUCCESS, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04004788 return SUCCESS;
4789 }
dea31012005-04-17 16:05:31 -05004790
James Smart4f2e66c2012-05-09 21:17:07 -04004791 iocb = &lpfc_cmd->cur_iocbq;
4792 /* the command is in process of being cancelled */
4793 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
James Smart876dd7d2012-09-29 11:31:28 -04004794 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004795 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4796 "3169 SCSI Layer abort requested I/O has been "
4797 "cancelled by LLD.\n");
4798 return FAILED;
4799 }
dea31012005-04-17 16:05:31 -05004800 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004801 * If pCmd field of the corresponding lpfc_scsi_buf structure
4802 * points to a different SCSI command, then the driver has
4803 * already completed this command, but the midlayer did not
James Smart4f2e66c2012-05-09 21:17:07 -04004804 * see the completion before the eh fired. Just return SUCCESS.
dea31012005-04-17 16:05:31 -05004805 */
James Smart4f2e66c2012-05-09 21:17:07 -04004806 if (lpfc_cmd->pCmd != cmnd) {
4807 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4808 "3170 SCSI Layer abort requested I/O has been "
4809 "completed by LLD.\n");
4810 goto out_unlock;
4811 }
dea31012005-04-17 16:05:31 -05004812
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004813 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004814
James Smartee620212014-04-04 13:51:53 -04004815 /* abort issued in recovery is still in progress */
4816 if (iocb->iocb_flag & LPFC_DRIVER_ABORTED) {
4817 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4818 "3389 SCSI Layer I/O Abort Request is pending\n");
4819 spin_unlock_irqrestore(&phba->hbalock, flags);
4820 goto wait_for_cmpl;
4821 }
4822
James Smart4f2e66c2012-05-09 21:17:07 -04004823 abtsiocb = __lpfc_sli_get_iocbq(phba);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004824 if (abtsiocb == NULL) {
4825 ret = FAILED;
James Smart4f2e66c2012-05-09 21:17:07 -04004826 goto out_unlock;
dea31012005-04-17 16:05:31 -05004827 }
4828
James Smartafbd8d82013-09-06 12:22:13 -04004829 /* Indicate the IO is being aborted by the driver. */
4830 iocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4831
dea31012005-04-17 16:05:31 -05004832 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004833 * The scsi command can not be in txq and it is in flight because the
4834 * pCmd is still pointig at the SCSI command we have to abort. There
4835 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004836 */
dea31012005-04-17 16:05:31 -05004837
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004838 cmd = &iocb->iocb;
4839 icmd = &abtsiocb->iocb;
4840 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4841 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004842 if (phba->sli_rev == LPFC_SLI_REV4)
4843 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4844 else
4845 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004846
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004847 icmd->ulpLe = 1;
4848 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004849
4850 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4851 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05004852 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -04004853 if (iocb->iocb_flag & LPFC_IO_FOF)
4854 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -05004855
James Smart2e0fef82007-06-17 19:56:36 -05004856 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004857 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4858 else
4859 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004860
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004861 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004862 abtsiocb->vport = vport;
James Smart98912dd2014-04-04 13:52:31 -04004863 if (phba->sli_rev == LPFC_SLI_REV4) {
4864 ring_number = MAX_SLI3_CONFIGURED_RINGS + iocb->fcp_wqidx;
4865 pring_s4 = &phba->sli.ring[ring_number];
4866 /* Note: both hbalock and ring_lock must be set here */
4867 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
4868 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
4869 abtsiocb, 0);
4870 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
4871 } else {
4872 ret_val = __lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
4873 abtsiocb, 0);
4874 }
James Smart4f2e66c2012-05-09 21:17:07 -04004875 /* no longer need the lock after this point */
James Smart876dd7d2012-09-29 11:31:28 -04004876 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004877
James Smart98912dd2014-04-04 13:52:31 -04004878
4879 if (ret_val == IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004880 lpfc_sli_release_iocbq(phba, abtsiocb);
4881 ret = FAILED;
4882 goto out;
4883 }
4884
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004885 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004886 lpfc_sli_handle_fast_ring_event(phba,
4887 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004888
James Smartee620212014-04-04 13:51:53 -04004889wait_for_cmpl:
James Smartfa61a542008-01-11 01:52:42 -05004890 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004891 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004892 wait_event_timeout(waitq,
4893 (lpfc_cmd->pCmd != cmnd),
James Smart256ec0d2013-04-17 20:14:58 -04004894 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
James Smartee620212014-04-04 13:51:53 -04004895
4896 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004897 lpfc_cmd->waitq = NULL;
James Smartee620212014-04-04 13:51:53 -04004898 spin_unlock_irqrestore(shost->host_lock, flags);
dea31012005-04-17 16:05:31 -05004899
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004900 if (lpfc_cmd->pCmd == cmnd) {
4901 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04004902 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4903 "0748 abort handler timed out waiting "
James Smart247ca942012-08-14 14:26:13 -04004904 "for abortng I/O (xri:x%x) to complete: "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004905 "ret %#x, ID %d, LUN %llu\n",
James Smart247ca942012-08-14 14:26:13 -04004906 iocb->sli4_xritag, ret,
4907 cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004908 }
James Smart4f2e66c2012-05-09 21:17:07 -04004909 goto out;
dea31012005-04-17 16:05:31 -05004910
James Smart4f2e66c2012-05-09 21:17:07 -04004911out_unlock:
James Smart876dd7d2012-09-29 11:31:28 -04004912 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004913out:
James Smarte8b62012007-08-02 11:10:09 -04004914 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4915 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004916 "LUN %llu\n", ret, cmnd->device->id,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004917 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004918 return ret;
dea31012005-04-17 16:05:31 -05004919}
4920
James Smartbbb9d182009-06-10 17:23:16 -04004921static char *
4922lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4923{
4924 switch (task_mgmt_cmd) {
4925 case FCP_ABORT_TASK_SET:
4926 return "ABORT_TASK_SET";
4927 case FCP_CLEAR_TASK_SET:
4928 return "FCP_CLEAR_TASK_SET";
4929 case FCP_BUS_RESET:
4930 return "FCP_BUS_RESET";
4931 case FCP_LUN_RESET:
4932 return "FCP_LUN_RESET";
4933 case FCP_TARGET_RESET:
4934 return "FCP_TARGET_RESET";
4935 case FCP_CLEAR_ACA:
4936 return "FCP_CLEAR_ACA";
4937 case FCP_TERMINATE_TASK:
4938 return "FCP_TERMINATE_TASK";
4939 default:
4940 return "unknown";
4941 }
4942}
4943
James Smart53151bb2013-10-10 12:24:07 -04004944
4945/**
4946 * lpfc_check_fcp_rsp - check the returned fcp_rsp to see if task failed
4947 * @vport: The virtual port for which this call is being executed.
4948 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4949 *
4950 * This routine checks the FCP RSP INFO to see if the tsk mgmt command succeded
4951 *
4952 * Return code :
4953 * 0x2003 - Error
4954 * 0x2002 - Success
4955 **/
4956static int
4957lpfc_check_fcp_rsp(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd)
4958{
4959 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
4960 uint32_t rsp_info;
4961 uint32_t rsp_len;
4962 uint8_t rsp_info_code;
4963 int ret = FAILED;
4964
4965
4966 if (fcprsp == NULL)
4967 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4968 "0703 fcp_rsp is missing\n");
4969 else {
4970 rsp_info = fcprsp->rspStatus2;
4971 rsp_len = be32_to_cpu(fcprsp->rspRspLen);
4972 rsp_info_code = fcprsp->rspInfo3;
4973
4974
4975 lpfc_printf_vlog(vport, KERN_INFO,
4976 LOG_FCP,
4977 "0706 fcp_rsp valid 0x%x,"
4978 " rsp len=%d code 0x%x\n",
4979 rsp_info,
4980 rsp_len, rsp_info_code);
4981
4982 if ((fcprsp->rspStatus2&RSP_LEN_VALID) && (rsp_len == 8)) {
4983 switch (rsp_info_code) {
4984 case RSP_NO_FAILURE:
4985 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4986 "0715 Task Mgmt No Failure\n");
4987 ret = SUCCESS;
4988 break;
4989 case RSP_TM_NOT_SUPPORTED: /* TM rejected */
4990 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4991 "0716 Task Mgmt Target "
4992 "reject\n");
4993 break;
4994 case RSP_TM_NOT_COMPLETED: /* TM failed */
4995 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4996 "0717 Task Mgmt Target "
4997 "failed TM\n");
4998 break;
4999 case RSP_TM_INVALID_LU: /* TM to invalid LU! */
5000 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5001 "0718 Task Mgmt to invalid "
5002 "LUN\n");
5003 break;
5004 }
5005 }
5006 }
5007 return ret;
5008}
5009
5010
James Smartbbb9d182009-06-10 17:23:16 -04005011/**
5012 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
5013 * @vport: The virtual port for which this call is being executed.
5014 * @rdata: Pointer to remote port local data
5015 * @tgt_id: Target ID of remote device.
5016 * @lun_id: Lun number for the TMF
5017 * @task_mgmt_cmd: type of TMF to send
5018 *
5019 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
5020 * a remote port.
5021 *
5022 * Return Code:
5023 * 0x2003 - Error
5024 * 0x2002 - Success.
5025 **/
5026static int
5027lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005028 unsigned tgt_id, uint64_t lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005029 uint8_t task_mgmt_cmd)
5030{
5031 struct lpfc_hba *phba = vport->phba;
5032 struct lpfc_scsi_buf *lpfc_cmd;
5033 struct lpfc_iocbq *iocbq;
5034 struct lpfc_iocbq *iocbqrsp;
James Smart5989b8d2010-10-22 11:06:56 -04005035 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005036 int ret;
5037 int status;
5038
James Smart5989b8d2010-10-22 11:06:56 -04005039 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smartbbb9d182009-06-10 17:23:16 -04005040 return FAILED;
5041
James Smart19ca7602010-11-20 23:11:55 -05005042 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
James Smartbbb9d182009-06-10 17:23:16 -04005043 if (lpfc_cmd == NULL)
5044 return FAILED;
James Smart0c411222013-09-06 12:22:46 -04005045 lpfc_cmd->timeout = phba->cfg_task_mgmt_tmo;
James Smartbbb9d182009-06-10 17:23:16 -04005046 lpfc_cmd->rdata = rdata;
5047
5048 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
5049 task_mgmt_cmd);
5050 if (!status) {
5051 lpfc_release_scsi_buf(phba, lpfc_cmd);
5052 return FAILED;
5053 }
5054
5055 iocbq = &lpfc_cmd->cur_iocbq;
5056 iocbqrsp = lpfc_sli_get_iocbq(phba);
5057 if (iocbqrsp == NULL) {
5058 lpfc_release_scsi_buf(phba, lpfc_cmd);
5059 return FAILED;
5060 }
James Smart5a0916b2013-07-15 18:31:42 -04005061 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
James Smartbbb9d182009-06-10 17:23:16 -04005062
5063 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005064 "0702 Issue %s to TGT %d LUN %llu "
James Smart6d368e52011-05-24 11:44:12 -04005065 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04005066 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04005067 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
5068 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04005069
5070 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
5071 iocbq, iocbqrsp, lpfc_cmd->timeout);
James Smart53151bb2013-10-10 12:24:07 -04005072 if ((status != IOCB_SUCCESS) ||
5073 (iocbqrsp->iocb.ulpStatus != IOSTAT_SUCCESS)) {
James Smartbbb9d182009-06-10 17:23:16 -04005074 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005075 "0727 TMF %s to TGT %d LUN %llu failed (%d, %d) "
James Smart6d368e52011-05-24 11:44:12 -04005076 "iocb_flag x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04005077 lpfc_taskmgmt_name(task_mgmt_cmd),
5078 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
James Smart6d368e52011-05-24 11:44:12 -04005079 iocbqrsp->iocb.un.ulpWord[4],
5080 iocbq->iocb_flag);
James Smart53151bb2013-10-10 12:24:07 -04005081 /* if ulpStatus != IOCB_SUCCESS, then status == IOCB_SUCCESS */
5082 if (status == IOCB_SUCCESS) {
5083 if (iocbqrsp->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
5084 /* Something in the FCP_RSP was invalid.
5085 * Check conditions */
5086 ret = lpfc_check_fcp_rsp(vport, lpfc_cmd);
5087 else
5088 ret = FAILED;
5089 } else if (status == IOCB_TIMEDOUT) {
5090 ret = TIMEOUT_ERROR;
5091 } else {
5092 ret = FAILED;
5093 }
5094 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
5095 } else
James Smartbbb9d182009-06-10 17:23:16 -04005096 ret = SUCCESS;
5097
5098 lpfc_sli_release_iocbq(phba, iocbqrsp);
5099
5100 if (ret != TIMEOUT_ERROR)
5101 lpfc_release_scsi_buf(phba, lpfc_cmd);
5102
5103 return ret;
5104}
5105
5106/**
5107 * lpfc_chk_tgt_mapped -
5108 * @vport: The virtual port to check on
5109 * @cmnd: Pointer to scsi_cmnd data structure.
5110 *
5111 * This routine delays until the scsi target (aka rport) for the
5112 * command exists (is present and logged in) or we declare it non-existent.
5113 *
5114 * Return code :
5115 * 0x2003 - Error
5116 * 0x2002 - Success
5117 **/
5118static int
5119lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
5120{
James Smart1ba981f2014-02-20 09:56:45 -05005121 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005122 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005123 unsigned long later;
5124
James Smart1ba981f2014-02-20 09:56:45 -05005125 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smart1c6f4ef52009-11-18 15:40:49 -05005126 if (!rdata) {
5127 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5128 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
5129 return FAILED;
5130 }
5131 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005132 /*
5133 * If target is not in a MAPPED state, delay until
5134 * target is rediscovered or devloss timeout expires.
5135 */
5136 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5137 while (time_after(later, jiffies)) {
5138 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
5139 return FAILED;
5140 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
5141 return SUCCESS;
5142 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
James Smart1ba981f2014-02-20 09:56:45 -05005143 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartbbb9d182009-06-10 17:23:16 -04005144 if (!rdata)
5145 return FAILED;
5146 pnode = rdata->pnode;
5147 }
5148 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
5149 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
5150 return FAILED;
5151 return SUCCESS;
5152}
5153
5154/**
5155 * lpfc_reset_flush_io_context -
5156 * @vport: The virtual port (scsi_host) for the flush context
5157 * @tgt_id: If aborting by Target contect - specifies the target id
5158 * @lun_id: If aborting by Lun context - specifies the lun id
5159 * @context: specifies the context level to flush at.
5160 *
5161 * After a reset condition via TMF, we need to flush orphaned i/o
5162 * contexts from the adapter. This routine aborts any contexts
5163 * outstanding, then waits for their completions. The wait is
5164 * bounded by devloss_tmo though.
5165 *
5166 * Return code :
5167 * 0x2003 - Error
5168 * 0x2002 - Success
5169 **/
5170static int
5171lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5172 uint64_t lun_id, lpfc_ctx_cmd context)
5173{
5174 struct lpfc_hba *phba = vport->phba;
5175 unsigned long later;
5176 int cnt;
5177
5178 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5179 if (cnt)
James Smart98912dd2014-04-04 13:52:31 -04005180 lpfc_sli_abort_taskmgmt(vport,
5181 &phba->sli.ring[phba->sli.fcp_ring],
5182 tgt_id, lun_id, context);
James Smartbbb9d182009-06-10 17:23:16 -04005183 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5184 while (time_after(later, jiffies) && cnt) {
5185 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5186 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5187 }
5188 if (cnt) {
5189 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5190 "0724 I/O flush failure for context %s : cnt x%x\n",
5191 ((context == LPFC_CTX_LUN) ? "LUN" :
5192 ((context == LPFC_CTX_TGT) ? "TGT" :
5193 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5194 cnt);
5195 return FAILED;
5196 }
5197 return SUCCESS;
5198}
5199
James Smart9bad7672008-12-04 22:39:02 -05005200/**
James Smart3621a712009-04-06 18:47:14 -04005201 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05005202 * @cmnd: Pointer to scsi_cmnd data structure.
5203 *
James Smartbbb9d182009-06-10 17:23:16 -04005204 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05005205 * command.
5206 *
5207 * Return code :
5208 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04005209 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005210 **/
dea31012005-04-17 16:05:31 -05005211static int
James Smart7054a602007-04-25 09:52:34 -04005212lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005213{
James Smart2e0fef82007-06-17 19:56:36 -05005214 struct Scsi_Host *shost = cmnd->device->host;
5215 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smart1ba981f2014-02-20 09:56:45 -05005216 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005217 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005218 unsigned tgt_id = cmnd->device->id;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005219 uint64_t lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04005220 struct lpfc_scsi_event_header scsi_event;
James Smart53151bb2013-10-10 12:24:07 -04005221 int status;
dea31012005-04-17 16:05:31 -05005222
James Smart1ba981f2014-02-20 09:56:45 -05005223 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smart1c6f4ef52009-11-18 15:40:49 -05005224 if (!rdata) {
5225 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5226 "0798 Device Reset rport failure: rdata x%p\n", rdata);
5227 return FAILED;
5228 }
5229 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005230 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005231 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005232 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005233
5234 status = lpfc_chk_tgt_mapped(vport, cmnd);
5235 if (status == FAILED) {
5236 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5237 "0721 Device Reset rport failure: rdata x%p\n", rdata);
5238 return FAILED;
5239 }
5240
5241 scsi_event.event_type = FC_REG_SCSI_EVENT;
5242 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5243 scsi_event.lun = lun_id;
5244 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5245 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5246
5247 fc_host_post_vendor_event(shost, fc_get_event_number(),
5248 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5249
5250 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5251 FCP_LUN_RESET);
5252
5253 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005254 "0713 SCSI layer issued Device Reset (%d, %llu) "
James Smartbbb9d182009-06-10 17:23:16 -04005255 "return x%x\n", tgt_id, lun_id, status);
5256
dea31012005-04-17 16:05:31 -05005257 /*
James Smartbbb9d182009-06-10 17:23:16 -04005258 * We have to clean up i/o as : they may be orphaned by the TMF;
5259 * or if the TMF failed, they may be in an indeterminate state.
5260 * So, continue on.
5261 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05005262 */
James Smart53151bb2013-10-10 12:24:07 -04005263 if (status == SUCCESS)
5264 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005265 LPFC_CTX_LUN);
James Smart53151bb2013-10-10 12:24:07 -04005266
5267 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005268}
5269
5270/**
5271 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5272 * @cmnd: Pointer to scsi_cmnd data structure.
5273 *
5274 * This routine does a target reset by sending a TARGET_RESET task management
5275 * command.
5276 *
5277 * Return code :
5278 * 0x2003 - Error
5279 * 0x2002 - Success
5280 **/
5281static int
5282lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5283{
5284 struct Scsi_Host *shost = cmnd->device->host;
5285 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smart1ba981f2014-02-20 09:56:45 -05005286 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005287 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005288 unsigned tgt_id = cmnd->device->id;
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005289 uint64_t lun_id = cmnd->device->lun;
James Smartbbb9d182009-06-10 17:23:16 -04005290 struct lpfc_scsi_event_header scsi_event;
James Smart53151bb2013-10-10 12:24:07 -04005291 int status;
James Smartbbb9d182009-06-10 17:23:16 -04005292
James Smart1ba981f2014-02-20 09:56:45 -05005293 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smart1c6f4ef52009-11-18 15:40:49 -05005294 if (!rdata) {
5295 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5296 "0799 Target Reset rport failure: rdata x%p\n", rdata);
5297 return FAILED;
5298 }
5299 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005300 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005301 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005302 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005303
5304 status = lpfc_chk_tgt_mapped(vport, cmnd);
5305 if (status == FAILED) {
5306 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5307 "0722 Target Reset rport failure: rdata x%p\n", rdata);
James Smart8c50d252014-09-03 12:58:16 -04005308 spin_lock_irq(shost->host_lock);
5309 pnode->nlp_flag &= ~NLP_NPR_ADISC;
5310 pnode->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
5311 spin_unlock_irq(shost->host_lock);
5312 lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5313 LPFC_CTX_TGT);
5314 return FAST_IO_FAIL;
dea31012005-04-17 16:05:31 -05005315 }
James Smartea2151b2008-09-07 11:52:10 -04005316
5317 scsi_event.event_type = FC_REG_SCSI_EVENT;
5318 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5319 scsi_event.lun = 0;
5320 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5321 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5322
James Smartbbb9d182009-06-10 17:23:16 -04005323 fc_host_post_vendor_event(shost, fc_get_event_number(),
5324 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04005325
James Smartbbb9d182009-06-10 17:23:16 -04005326 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5327 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05005328
James Smarte8b62012007-08-02 11:10:09 -04005329 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02005330 "0723 SCSI layer issued Target Reset (%d, %llu) "
James Smartbbb9d182009-06-10 17:23:16 -04005331 "return x%x\n", tgt_id, lun_id, status);
5332
5333 /*
5334 * We have to clean up i/o as : they may be orphaned by the TMF;
5335 * or if the TMF failed, they may be in an indeterminate state.
5336 * So, continue on.
5337 * We will report success if all the i/o aborts successfully.
5338 */
James Smart53151bb2013-10-10 12:24:07 -04005339 if (status == SUCCESS)
5340 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smart3a707302012-06-12 13:54:42 -04005341 LPFC_CTX_TGT);
James Smart53151bb2013-10-10 12:24:07 -04005342 return status;
dea31012005-04-17 16:05:31 -05005343}
5344
James Smart9bad7672008-12-04 22:39:02 -05005345/**
James Smart3621a712009-04-06 18:47:14 -04005346 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05005347 * @cmnd: Pointer to scsi_cmnd data structure.
5348 *
James Smartbbb9d182009-06-10 17:23:16 -04005349 * This routine does target reset to all targets on @cmnd->device->host.
5350 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05005351 *
James Smartbbb9d182009-06-10 17:23:16 -04005352 * Return code :
5353 * 0x2003 - Error
5354 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005355 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04005356static int
James Smart7054a602007-04-25 09:52:34 -04005357lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005358{
James Smart2e0fef82007-06-17 19:56:36 -05005359 struct Scsi_Host *shost = cmnd->device->host;
5360 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005361 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04005362 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04005363 int match;
5364 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04005365
5366 scsi_event.event_type = FC_REG_SCSI_EVENT;
5367 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5368 scsi_event.lun = 0;
5369 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5370 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5371
James Smartbbb9d182009-06-10 17:23:16 -04005372 fc_host_post_vendor_event(shost, fc_get_event_number(),
5373 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05005374
James Smartbf086112011-08-21 21:48:13 -04005375 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005376 if (status != 0 && status != SUCCESS)
James Smartbf086112011-08-21 21:48:13 -04005377 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005378
dea31012005-04-17 16:05:31 -05005379 /*
5380 * Since the driver manages a single bus device, reset all
5381 * targets known to the driver. Should any target reset
5382 * fail, this routine returns failure to the midlayer.
5383 */
James Smarte17da182006-07-06 15:49:25 -04005384 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04005385 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05005386 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05005387 spin_lock_irq(shost->host_lock);
5388 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005389 if (!NLP_CHK_NODE_ACT(ndlp))
5390 continue;
James Smarta6571c62012-10-31 14:44:42 -04005391 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5392 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5393 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005394 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04005395 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04005396 ndlp->rport) {
dea31012005-04-17 16:05:31 -05005397 match = 1;
5398 break;
5399 }
5400 }
James Smart2e0fef82007-06-17 19:56:36 -05005401 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005402 if (!match)
5403 continue;
James Smartbbb9d182009-06-10 17:23:16 -04005404
5405 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
5406 i, 0, FCP_TARGET_RESET);
5407
5408 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04005409 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5410 "0700 Bus Reset on target %d failed\n",
5411 i);
James Smart915caaa2008-06-14 22:52:38 -04005412 ret = FAILED;
dea31012005-04-17 16:05:31 -05005413 }
5414 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005415 /*
James Smartbbb9d182009-06-10 17:23:16 -04005416 * We have to clean up i/o as : they may be orphaned by the TMFs
5417 * above; or if any of the TMFs failed, they may be in an
5418 * indeterminate state.
5419 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005420 */
James Smartbbb9d182009-06-10 17:23:16 -04005421
5422 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5423 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005424 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04005425
James Smarte8b62012007-08-02 11:10:09 -04005426 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5427 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05005428 return ret;
5429}
5430
James Smart9bad7672008-12-04 22:39:02 -05005431/**
James Smart27b01b82012-05-09 21:19:44 -04005432 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
5433 * @cmnd: Pointer to scsi_cmnd data structure.
5434 *
5435 * This routine does host reset to the adaptor port. It brings the HBA
5436 * offline, performs a board restart, and then brings the board back online.
5437 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
5438 * reject all outstanding SCSI commands to the host and error returned
5439 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5440 * of error handling, it will only return error if resetting of the adapter
5441 * is not successful; in all other cases, will return success.
5442 *
5443 * Return code :
5444 * 0x2003 - Error
5445 * 0x2002 - Success
5446 **/
5447static int
5448lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5449{
5450 struct Scsi_Host *shost = cmnd->device->host;
5451 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5452 struct lpfc_hba *phba = vport->phba;
5453 int rc, ret = SUCCESS;
5454
James Smarta88dbb62013-04-17 20:18:39 -04005455 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5456 "3172 SCSI layer issued Host Reset Data:\n");
5457
James Smart618a5232012-06-12 13:54:36 -04005458 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart27b01b82012-05-09 21:19:44 -04005459 lpfc_offline(phba);
5460 rc = lpfc_sli_brdrestart(phba);
5461 if (rc)
5462 ret = FAILED;
James Smarta88dbb62013-04-17 20:18:39 -04005463 rc = lpfc_online(phba);
5464 if (rc)
5465 ret = FAILED;
James Smart27b01b82012-05-09 21:19:44 -04005466 lpfc_unblock_mgmt_io(phba);
5467
James Smarta88dbb62013-04-17 20:18:39 -04005468 if (ret == FAILED) {
5469 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5470 "3323 Failed host reset, bring it offline\n");
5471 lpfc_sli4_offline_eratt(phba);
5472 }
James Smart27b01b82012-05-09 21:19:44 -04005473 return ret;
5474}
5475
5476/**
James Smart3621a712009-04-06 18:47:14 -04005477 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05005478 * @sdev: Pointer to scsi_device.
5479 *
5480 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5481 * globally available list of scsi buffers. This routine also makes sure scsi
5482 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5483 * of scsi buffer exists for the lifetime of the driver.
5484 *
5485 * Return codes:
5486 * non-0 - Error
5487 * 0 - Success
5488 **/
dea31012005-04-17 16:05:31 -05005489static int
dea31012005-04-17 16:05:31 -05005490lpfc_slave_alloc(struct scsi_device *sdev)
5491{
James Smart2e0fef82007-06-17 19:56:36 -05005492 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5493 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005494 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04005495 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05005496 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04005497 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04005498 uint32_t sdev_cnt;
James Smart1ba981f2014-02-20 09:56:45 -05005499 struct lpfc_device_data *device_data;
5500 unsigned long flags;
5501 struct lpfc_name target_wwpn;
dea31012005-04-17 16:05:31 -05005502
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005503 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05005504 return -ENXIO;
5505
James Smartf38fa0b2014-04-04 13:52:21 -04005506 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -05005507
5508 /*
5509 * Check to see if the device data structure for the lun
5510 * exists. If not, create one.
5511 */
5512
5513 u64_to_wwn(rport->port_name, target_wwpn.u.wwn);
5514 spin_lock_irqsave(&phba->devicelock, flags);
5515 device_data = __lpfc_get_device_data(phba,
5516 &phba->luns,
5517 &vport->fc_portname,
5518 &target_wwpn,
5519 sdev->lun);
5520 if (!device_data) {
5521 spin_unlock_irqrestore(&phba->devicelock, flags);
5522 device_data = lpfc_create_device_data(phba,
5523 &vport->fc_portname,
5524 &target_wwpn,
5525 sdev->lun, true);
5526 if (!device_data)
5527 return -ENOMEM;
5528 spin_lock_irqsave(&phba->devicelock, flags);
5529 list_add_tail(&device_data->listentry, &phba->luns);
5530 }
5531 device_data->rport_data = rport->dd_data;
5532 device_data->available = true;
5533 spin_unlock_irqrestore(&phba->devicelock, flags);
5534 sdev->hostdata = device_data;
5535 } else {
5536 sdev->hostdata = rport->dd_data;
5537 }
James Smartd7c47992010-06-08 18:31:54 -04005538 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005539
5540 /*
5541 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5542 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04005543 * HBA limit conveyed to the midlayer via the host structure. The
5544 * formula accounts for the lun_queue_depth + error handlers + 1
5545 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05005546 */
5547 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04005548 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05005549
James Smartd7c47992010-06-08 18:31:54 -04005550 /* If allocated buffers are enough do nothing */
5551 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5552 return 0;
5553
James Smart92d7f7b2007-06-17 19:56:38 -05005554 /* Allow some exchanges to be available always to complete discovery */
5555 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005556 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5557 "0704 At limitation of %d preallocated "
5558 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05005559 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05005560 /* Allow some exchanges to be available always to complete discovery */
5561 } else if (total + num_to_alloc >
5562 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005563 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5564 "0705 Allocation request of %d "
5565 "command buffers will exceed max of %d. "
5566 "Reducing allocation request to %d.\n",
5567 num_to_alloc, phba->cfg_hba_queue_depth,
5568 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05005569 num_to_alloc = phba->cfg_hba_queue_depth - total;
5570 }
James Smart3772a992009-05-22 14:50:54 -04005571 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
5572 if (num_to_alloc != num_allocated) {
James Smart96f70772013-04-17 20:16:15 -04005573 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5574 "0708 Allocation request of %d "
5575 "command buffers did not succeed. "
5576 "Allocated %d buffers.\n",
5577 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05005578 }
James Smart1c6f4ef52009-11-18 15:40:49 -05005579 if (num_allocated > 0)
5580 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05005581 return 0;
5582}
5583
James Smart9bad7672008-12-04 22:39:02 -05005584/**
James Smart3621a712009-04-06 18:47:14 -04005585 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05005586 * @sdev: Pointer to scsi_device.
5587 *
5588 * This routine configures following items
5589 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05005590 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5591 *
5592 * Return codes:
5593 * 0 - Success
5594 **/
dea31012005-04-17 16:05:31 -05005595static int
5596lpfc_slave_configure(struct scsi_device *sdev)
5597{
James Smart2e0fef82007-06-17 19:56:36 -05005598 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5599 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005600
5601 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04005602 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005603 else
James Smart3de2a652007-08-02 11:09:59 -04005604 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005605
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005606 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005607 lpfc_sli_handle_fast_ring_event(phba,
5608 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005609 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5610 lpfc_poll_rearm_timer(phba);
5611 }
5612
dea31012005-04-17 16:05:31 -05005613 return 0;
5614}
5615
James Smart9bad7672008-12-04 22:39:02 -05005616/**
James Smart3621a712009-04-06 18:47:14 -04005617 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05005618 * @sdev: Pointer to scsi_device.
5619 *
5620 * This routine sets @sdev hostatdata filed to null.
5621 **/
dea31012005-04-17 16:05:31 -05005622static void
5623lpfc_slave_destroy(struct scsi_device *sdev)
5624{
James Smartd7c47992010-06-08 18:31:54 -04005625 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5626 struct lpfc_hba *phba = vport->phba;
James Smart1ba981f2014-02-20 09:56:45 -05005627 unsigned long flags;
5628 struct lpfc_device_data *device_data = sdev->hostdata;
5629
James Smartd7c47992010-06-08 18:31:54 -04005630 atomic_dec(&phba->sdev_cnt);
James Smartf38fa0b2014-04-04 13:52:21 -04005631 if ((phba->cfg_fof) && (device_data)) {
James Smart1ba981f2014-02-20 09:56:45 -05005632 spin_lock_irqsave(&phba->devicelock, flags);
5633 device_data->available = false;
5634 if (!device_data->oas_enabled)
5635 lpfc_delete_device_data(phba, device_data);
5636 spin_unlock_irqrestore(&phba->devicelock, flags);
5637 }
dea31012005-04-17 16:05:31 -05005638 sdev->hostdata = NULL;
5639 return;
5640}
5641
James Smart1ba981f2014-02-20 09:56:45 -05005642/**
5643 * lpfc_create_device_data - creates and initializes device data structure for OAS
5644 * @pha: Pointer to host bus adapter structure.
5645 * @vport_wwpn: Pointer to vport's wwpn information
5646 * @target_wwpn: Pointer to target's wwpn information
5647 * @lun: Lun on target
5648 * @atomic_create: Flag to indicate if memory should be allocated using the
5649 * GFP_ATOMIC flag or not.
5650 *
5651 * This routine creates a device data structure which will contain identifying
5652 * information for the device (host wwpn, target wwpn, lun), state of OAS,
5653 * whether or not the corresponding lun is available by the system,
5654 * and pointer to the rport data.
5655 *
5656 * Return codes:
5657 * NULL - Error
5658 * Pointer to lpfc_device_data - Success
5659 **/
5660struct lpfc_device_data*
5661lpfc_create_device_data(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5662 struct lpfc_name *target_wwpn, uint64_t lun,
5663 bool atomic_create)
5664{
5665
5666 struct lpfc_device_data *lun_info;
5667 int memory_flags;
5668
5669 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005670 !(phba->cfg_fof))
James Smart1ba981f2014-02-20 09:56:45 -05005671 return NULL;
5672
5673 /* Attempt to create the device data to contain lun info */
5674
5675 if (atomic_create)
5676 memory_flags = GFP_ATOMIC;
5677 else
5678 memory_flags = GFP_KERNEL;
5679 lun_info = mempool_alloc(phba->device_data_mem_pool, memory_flags);
5680 if (!lun_info)
5681 return NULL;
5682 INIT_LIST_HEAD(&lun_info->listentry);
5683 lun_info->rport_data = NULL;
5684 memcpy(&lun_info->device_id.vport_wwpn, vport_wwpn,
5685 sizeof(struct lpfc_name));
5686 memcpy(&lun_info->device_id.target_wwpn, target_wwpn,
5687 sizeof(struct lpfc_name));
5688 lun_info->device_id.lun = lun;
5689 lun_info->oas_enabled = false;
5690 lun_info->available = false;
5691 return lun_info;
5692}
5693
5694/**
5695 * lpfc_delete_device_data - frees a device data structure for OAS
5696 * @pha: Pointer to host bus adapter structure.
5697 * @lun_info: Pointer to device data structure to free.
5698 *
5699 * This routine frees the previously allocated device data structure passed.
5700 *
5701 **/
5702void
5703lpfc_delete_device_data(struct lpfc_hba *phba,
5704 struct lpfc_device_data *lun_info)
5705{
5706
5707 if (unlikely(!phba) || !lun_info ||
James Smartf38fa0b2014-04-04 13:52:21 -04005708 !(phba->cfg_fof))
James Smart1ba981f2014-02-20 09:56:45 -05005709 return;
5710
5711 if (!list_empty(&lun_info->listentry))
5712 list_del(&lun_info->listentry);
5713 mempool_free(lun_info, phba->device_data_mem_pool);
5714 return;
5715}
5716
5717/**
5718 * __lpfc_get_device_data - returns the device data for the specified lun
5719 * @pha: Pointer to host bus adapter structure.
5720 * @list: Point to list to search.
5721 * @vport_wwpn: Pointer to vport's wwpn information
5722 * @target_wwpn: Pointer to target's wwpn information
5723 * @lun: Lun on target
5724 *
5725 * This routine searches the list passed for the specified lun's device data.
5726 * This function does not hold locks, it is the responsibility of the caller
5727 * to ensure the proper lock is held before calling the function.
5728 *
5729 * Return codes:
5730 * NULL - Error
5731 * Pointer to lpfc_device_data - Success
5732 **/
5733struct lpfc_device_data*
5734__lpfc_get_device_data(struct lpfc_hba *phba, struct list_head *list,
5735 struct lpfc_name *vport_wwpn,
5736 struct lpfc_name *target_wwpn, uint64_t lun)
5737{
5738
5739 struct lpfc_device_data *lun_info;
5740
5741 if (unlikely(!phba) || !list || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005742 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005743 return NULL;
5744
5745 /* Check to see if the lun is already enabled for OAS. */
5746
5747 list_for_each_entry(lun_info, list, listentry) {
5748 if ((memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5749 sizeof(struct lpfc_name)) == 0) &&
5750 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5751 sizeof(struct lpfc_name)) == 0) &&
5752 (lun_info->device_id.lun == lun))
5753 return lun_info;
5754 }
5755
5756 return NULL;
5757}
5758
5759/**
5760 * lpfc_find_next_oas_lun - searches for the next oas lun
5761 * @pha: Pointer to host bus adapter structure.
5762 * @vport_wwpn: Pointer to vport's wwpn information
5763 * @target_wwpn: Pointer to target's wwpn information
5764 * @starting_lun: Pointer to the lun to start searching for
5765 * @found_vport_wwpn: Pointer to the found lun's vport wwpn information
5766 * @found_target_wwpn: Pointer to the found lun's target wwpn information
5767 * @found_lun: Pointer to the found lun.
5768 * @found_lun_status: Pointer to status of the found lun.
5769 *
5770 * This routine searches the luns list for the specified lun
5771 * or the first lun for the vport/target. If the vport wwpn contains
5772 * a zero value then a specific vport is not specified. In this case
5773 * any vport which contains the lun will be considered a match. If the
5774 * target wwpn contains a zero value then a specific target is not specified.
5775 * In this case any target which contains the lun will be considered a
5776 * match. If the lun is found, the lun, vport wwpn, target wwpn and lun status
5777 * are returned. The function will also return the next lun if available.
5778 * If the next lun is not found, starting_lun parameter will be set to
5779 * NO_MORE_OAS_LUN.
5780 *
5781 * Return codes:
5782 * non-0 - Error
5783 * 0 - Success
5784 **/
5785bool
5786lpfc_find_next_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5787 struct lpfc_name *target_wwpn, uint64_t *starting_lun,
5788 struct lpfc_name *found_vport_wwpn,
5789 struct lpfc_name *found_target_wwpn,
5790 uint64_t *found_lun,
5791 uint32_t *found_lun_status)
5792{
5793
5794 unsigned long flags;
5795 struct lpfc_device_data *lun_info;
5796 struct lpfc_device_id *device_id;
5797 uint64_t lun;
5798 bool found = false;
5799
5800 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5801 !starting_lun || !found_vport_wwpn ||
5802 !found_target_wwpn || !found_lun || !found_lun_status ||
5803 (*starting_lun == NO_MORE_OAS_LUN) ||
James Smartf38fa0b2014-04-04 13:52:21 -04005804 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005805 return false;
5806
5807 lun = *starting_lun;
5808 *found_lun = NO_MORE_OAS_LUN;
5809 *starting_lun = NO_MORE_OAS_LUN;
5810
5811 /* Search for lun or the lun closet in value */
5812
5813 spin_lock_irqsave(&phba->devicelock, flags);
5814 list_for_each_entry(lun_info, &phba->luns, listentry) {
5815 if (((wwn_to_u64(vport_wwpn->u.wwn) == 0) ||
5816 (memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5817 sizeof(struct lpfc_name)) == 0)) &&
5818 ((wwn_to_u64(target_wwpn->u.wwn) == 0) ||
5819 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5820 sizeof(struct lpfc_name)) == 0)) &&
5821 (lun_info->oas_enabled)) {
5822 device_id = &lun_info->device_id;
5823 if ((!found) &&
5824 ((lun == FIND_FIRST_OAS_LUN) ||
5825 (device_id->lun == lun))) {
5826 *found_lun = device_id->lun;
5827 memcpy(found_vport_wwpn,
5828 &device_id->vport_wwpn,
5829 sizeof(struct lpfc_name));
5830 memcpy(found_target_wwpn,
5831 &device_id->target_wwpn,
5832 sizeof(struct lpfc_name));
5833 if (lun_info->available)
5834 *found_lun_status =
5835 OAS_LUN_STATUS_EXISTS;
5836 else
5837 *found_lun_status = 0;
5838 if (phba->cfg_oas_flags & OAS_FIND_ANY_VPORT)
5839 memset(vport_wwpn, 0x0,
5840 sizeof(struct lpfc_name));
5841 if (phba->cfg_oas_flags & OAS_FIND_ANY_TARGET)
5842 memset(target_wwpn, 0x0,
5843 sizeof(struct lpfc_name));
5844 found = true;
5845 } else if (found) {
5846 *starting_lun = device_id->lun;
5847 memcpy(vport_wwpn, &device_id->vport_wwpn,
5848 sizeof(struct lpfc_name));
5849 memcpy(target_wwpn, &device_id->target_wwpn,
5850 sizeof(struct lpfc_name));
5851 break;
5852 }
5853 }
5854 }
5855 spin_unlock_irqrestore(&phba->devicelock, flags);
5856 return found;
5857}
5858
5859/**
5860 * lpfc_enable_oas_lun - enables a lun for OAS operations
5861 * @pha: Pointer to host bus adapter structure.
5862 * @vport_wwpn: Pointer to vport's wwpn information
5863 * @target_wwpn: Pointer to target's wwpn information
5864 * @lun: Lun
5865 *
5866 * This routine enables a lun for oas operations. The routines does so by
5867 * doing the following :
5868 *
5869 * 1) Checks to see if the device data for the lun has been created.
5870 * 2) If found, sets the OAS enabled flag if not set and returns.
5871 * 3) Otherwise, creates a device data structure.
5872 * 4) If successfully created, indicates the device data is for an OAS lun,
5873 * indicates the lun is not available and add to the list of luns.
5874 *
5875 * Return codes:
5876 * false - Error
5877 * true - Success
5878 **/
5879bool
5880lpfc_enable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5881 struct lpfc_name *target_wwpn, uint64_t lun)
5882{
5883
5884 struct lpfc_device_data *lun_info;
5885 unsigned long flags;
5886
5887 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005888 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005889 return false;
5890
5891 spin_lock_irqsave(&phba->devicelock, flags);
5892
5893 /* Check to see if the device data for the lun has been created */
5894 lun_info = __lpfc_get_device_data(phba, &phba->luns, vport_wwpn,
5895 target_wwpn, lun);
5896 if (lun_info) {
5897 if (!lun_info->oas_enabled)
5898 lun_info->oas_enabled = true;
5899 spin_unlock_irqrestore(&phba->devicelock, flags);
5900 return true;
5901 }
5902
5903 /* Create an lun info structure and add to list of luns */
5904 lun_info = lpfc_create_device_data(phba, vport_wwpn, target_wwpn, lun,
5905 false);
5906 if (lun_info) {
5907 lun_info->oas_enabled = true;
5908 lun_info->available = false;
5909 list_add_tail(&lun_info->listentry, &phba->luns);
5910 spin_unlock_irqrestore(&phba->devicelock, flags);
5911 return true;
5912 }
5913 spin_unlock_irqrestore(&phba->devicelock, flags);
5914 return false;
5915}
5916
5917/**
5918 * lpfc_disable_oas_lun - disables a lun for OAS operations
5919 * @pha: Pointer to host bus adapter structure.
5920 * @vport_wwpn: Pointer to vport's wwpn information
5921 * @target_wwpn: Pointer to target's wwpn information
5922 * @lun: Lun
5923 *
5924 * This routine disables a lun for oas operations. The routines does so by
5925 * doing the following :
5926 *
5927 * 1) Checks to see if the device data for the lun is created.
5928 * 2) If present, clears the flag indicating this lun is for OAS.
5929 * 3) If the lun is not available by the system, the device data is
5930 * freed.
5931 *
5932 * Return codes:
5933 * false - Error
5934 * true - Success
5935 **/
5936bool
5937lpfc_disable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5938 struct lpfc_name *target_wwpn, uint64_t lun)
5939{
5940
5941 struct lpfc_device_data *lun_info;
5942 unsigned long flags;
5943
5944 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
James Smartf38fa0b2014-04-04 13:52:21 -04005945 !phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005946 return false;
5947
5948 spin_lock_irqsave(&phba->devicelock, flags);
5949
5950 /* Check to see if the lun is available. */
5951 lun_info = __lpfc_get_device_data(phba,
5952 &phba->luns, vport_wwpn,
5953 target_wwpn, lun);
5954 if (lun_info) {
5955 lun_info->oas_enabled = false;
5956 if (!lun_info->available)
5957 lpfc_delete_device_data(phba, lun_info);
5958 spin_unlock_irqrestore(&phba->devicelock, flags);
5959 return true;
5960 }
5961
5962 spin_unlock_irqrestore(&phba->devicelock, flags);
5963 return false;
5964}
James Smart92d7f7b2007-06-17 19:56:38 -05005965
dea31012005-04-17 16:05:31 -05005966struct scsi_host_template lpfc_template = {
5967 .module = THIS_MODULE,
5968 .name = LPFC_DRIVER_NAME,
5969 .info = lpfc_info,
5970 .queuecommand = lpfc_queuecommand,
5971 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005972 .eh_device_reset_handler = lpfc_device_reset_handler,
5973 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04005974 .eh_bus_reset_handler = lpfc_bus_reset_handler,
James Smart27b01b82012-05-09 21:19:44 -04005975 .eh_host_reset_handler = lpfc_host_reset_handler,
dea31012005-04-17 16:05:31 -05005976 .slave_alloc = lpfc_slave_alloc,
5977 .slave_configure = lpfc_slave_configure,
5978 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04005979 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05005980 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005981 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05005982 .cmd_per_lun = LPFC_CMD_PER_LUN,
5983 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05005984 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04005985 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04005986 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05005987 .change_queue_depth = lpfc_change_queue_depth,
Christoph Hellwiga62182f2014-10-02 14:39:55 +02005988 .change_queue_type = scsi_change_queue_type,
dea31012005-04-17 16:05:31 -05005989};
James Smart3de2a652007-08-02 11:09:59 -04005990
5991struct scsi_host_template lpfc_vport_template = {
5992 .module = THIS_MODULE,
5993 .name = LPFC_DRIVER_NAME,
5994 .info = lpfc_info,
5995 .queuecommand = lpfc_queuecommand,
5996 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005997 .eh_device_reset_handler = lpfc_device_reset_handler,
5998 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04005999 .eh_bus_reset_handler = lpfc_bus_reset_handler,
6000 .slave_alloc = lpfc_slave_alloc,
6001 .slave_configure = lpfc_slave_configure,
6002 .slave_destroy = lpfc_slave_destroy,
6003 .scan_finished = lpfc_scan_finished,
6004 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05006005 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04006006 .cmd_per_lun = LPFC_CMD_PER_LUN,
6007 .use_clustering = ENABLE_CLUSTERING,
6008 .shost_attrs = lpfc_vport_attrs,
6009 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05006010 .change_queue_depth = lpfc_change_queue_depth,
Christoph Hellwiga62182f2014-10-02 14:39:55 +02006011 .change_queue_type = scsi_change_queue_type,
James Smart3de2a652007-08-02 11:09:59 -04006012};