blob: 5ab21f7dc3fcd7d6e44d659deda4c888fa0a27c7 [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 Smartacd68592012-01-18 16:25:09 -05004 * Copyright (C) 2004-2012 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>
dea31012005-04-17 16:05:31 -050027
28#include <scsi/scsi.h>
29#include <scsi/scsi_device.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050030#include <scsi/scsi_eh.h>
dea31012005-04-17 16:05:31 -050031#include <scsi/scsi_host.h>
32#include <scsi/scsi_tcq.h>
33#include <scsi/scsi_transport_fc.h>
34
35#include "lpfc_version.h"
James Smartda0436e2009-05-22 14:51:39 -040036#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050037#include "lpfc_hw.h"
38#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040039#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040040#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050041#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050042#include "lpfc.h"
James Smart9a6b09c2012-03-01 22:37:42 -050043#include "lpfc_scsi.h"
dea31012005-04-17 16:05:31 -050044#include "lpfc_logmsg.h"
45#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050046#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050047
48#define LPFC_RESET_WAIT 2
49#define LPFC_ABORT_WAIT 2
50
James Smarte2a0a9d2008-12-04 22:40:02 -050051int _dump_buf_done;
52
53static char *dif_op_str[] = {
James Smart9a6b09c2012-03-01 22:37:42 -050054 "PROT_NORMAL",
55 "PROT_READ_INSERT",
56 "PROT_WRITE_STRIP",
57 "PROT_READ_STRIP",
58 "PROT_WRITE_INSERT",
59 "PROT_READ_PASS",
60 "PROT_WRITE_PASS",
61};
62
James Smartf9bb2da2011-10-10 21:34:11 -040063struct scsi_dif_tuple {
64 __be16 guard_tag; /* Checksum */
65 __be16 app_tag; /* Opaque storage */
66 __be32 ref_tag; /* Target LBA or indirect LBA */
67};
68
James Smartda0436e2009-05-22 14:51:39 -040069static void
70lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smart1c6f4ef52009-11-18 15:40:49 -050071static void
72lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smarte2a0a9d2008-12-04 22:40:02 -050073
74static void
James Smart6a9c52c2009-10-02 15:16:51 -040075lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -050076{
77 void *src, *dst;
78 struct scatterlist *sgde = scsi_sglist(cmnd);
79
80 if (!_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040081 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
82 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -050083 __func__);
84 return;
85 }
86
87
88 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -040089 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
90 "9051 BLKGRD: ERROR: data scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -050091 return;
92 }
93
94 dst = (void *) _dump_buf_data;
95 while (sgde) {
96 src = sg_virt(sgde);
97 memcpy(dst, src, sgde->length);
98 dst += sgde->length;
99 sgde = sg_next(sgde);
100 }
101}
102
103static void
James Smart6a9c52c2009-10-02 15:16:51 -0400104lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -0500105{
106 void *src, *dst;
107 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
108
109 if (!_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -0400110 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
111 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -0500112 __func__);
113 return;
114 }
115
116 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -0400117 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
118 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -0500119 return;
120 }
121
122 dst = _dump_buf_dif;
123 while (sgde) {
124 src = sg_virt(sgde);
125 memcpy(dst, src, sgde->length);
126 dst += sgde->length;
127 sgde = sg_next(sgde);
128 }
129}
130
James Smartea2151b2008-09-07 11:52:10 -0400131/**
James Smartf1126682009-06-10 17:22:44 -0400132 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
133 * @phba: Pointer to HBA object.
134 * @lpfc_cmd: lpfc scsi command object pointer.
135 *
136 * This function is called from the lpfc_prep_task_mgmt_cmd function to
137 * set the last bit in the response sge entry.
138 **/
139static void
140lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
141 struct lpfc_scsi_buf *lpfc_cmd)
142{
143 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
144 if (sgl) {
145 sgl += 1;
146 sgl->word2 = le32_to_cpu(sgl->word2);
147 bf_set(lpfc_sli4_sge_last, sgl, 1);
148 sgl->word2 = cpu_to_le32(sgl->word2);
149 }
150}
151
152/**
James Smart3621a712009-04-06 18:47:14 -0400153 * lpfc_update_stats - Update statistical data for the command completion
James Smartea2151b2008-09-07 11:52:10 -0400154 * @phba: Pointer to HBA object.
155 * @lpfc_cmd: lpfc scsi command object pointer.
156 *
157 * This function is called when there is a command completion and this
158 * function updates the statistical data for the command completion.
159 **/
160static void
161lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
162{
163 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
164 struct lpfc_nodelist *pnode = rdata->pnode;
165 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
166 unsigned long flags;
167 struct Scsi_Host *shost = cmd->device->host;
168 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
169 unsigned long latency;
170 int i;
171
172 if (cmd->result)
173 return;
174
James Smart9f1e1b52008-12-04 22:39:40 -0500175 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
176
James Smartea2151b2008-09-07 11:52:10 -0400177 spin_lock_irqsave(shost->host_lock, flags);
178 if (!vport->stat_data_enabled ||
179 vport->stat_data_blocked ||
James Smart5989b8d2010-10-22 11:06:56 -0400180 !pnode ||
James Smartea2151b2008-09-07 11:52:10 -0400181 !pnode->lat_data ||
182 (phba->bucket_type == LPFC_NO_BUCKET)) {
183 spin_unlock_irqrestore(shost->host_lock, flags);
184 return;
185 }
James Smartea2151b2008-09-07 11:52:10 -0400186
187 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
188 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
189 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -0500190 /* check array subscript bounds */
191 if (i < 0)
192 i = 0;
193 else if (i >= LPFC_MAX_BUCKET_COUNT)
194 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -0400195 } else {
196 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
197 if (latency <= (phba->bucket_base +
198 ((1<<i)*phba->bucket_step)))
199 break;
200 }
201
202 pnode->lat_data[i].cmd_count++;
203 spin_unlock_irqrestore(shost->host_lock, flags);
204}
205
James Smartea2151b2008-09-07 11:52:10 -0400206/**
James Smart3621a712009-04-06 18:47:14 -0400207 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
James Smartea2151b2008-09-07 11:52:10 -0400208 * @phba: Pointer to HBA context object.
209 * @vport: Pointer to vport object.
210 * @ndlp: Pointer to FC node associated with the target.
211 * @lun: Lun number of the scsi device.
212 * @old_val: Old value of the queue depth.
213 * @new_val: New value of the queue depth.
214 *
215 * This function sends an event to the mgmt application indicating
216 * there is a change in the scsi device queue depth.
217 **/
218static void
219lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
220 struct lpfc_vport *vport,
221 struct lpfc_nodelist *ndlp,
222 uint32_t lun,
223 uint32_t old_val,
224 uint32_t new_val)
225{
226 struct lpfc_fast_path_event *fast_path_evt;
227 unsigned long flags;
228
229 fast_path_evt = lpfc_alloc_fast_evt(phba);
230 if (!fast_path_evt)
231 return;
232
233 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
234 FC_REG_SCSI_EVENT;
235 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
236 LPFC_EVENT_VARQUEDEPTH;
237
238 /* Report all luns with change in queue depth */
239 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
240 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
241 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
242 &ndlp->nlp_portname, sizeof(struct lpfc_name));
243 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
244 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
245 }
246
247 fast_path_evt->un.queue_depth_evt.oldval = old_val;
248 fast_path_evt->un.queue_depth_evt.newval = new_val;
249 fast_path_evt->vport = vport;
250
251 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
252 spin_lock_irqsave(&phba->hbalock, flags);
253 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
254 spin_unlock_irqrestore(&phba->hbalock, flags);
255 lpfc_worker_wake_up(phba);
256
257 return;
258}
259
James Smart9bad7672008-12-04 22:39:02 -0500260/**
James Smart5ffc2662009-11-18 15:39:44 -0500261 * lpfc_change_queue_depth - Alter scsi device queue depth
262 * @sdev: Pointer the scsi device on which to change the queue depth.
263 * @qdepth: New queue depth to set the sdev to.
264 * @reason: The reason for the queue depth change.
265 *
266 * This function is called by the midlayer and the LLD to alter the queue
267 * depth for a scsi device. This function sets the queue depth to the new
268 * value and sends an event out to log the queue depth change.
269 **/
270int
271lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
272{
273 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
274 struct lpfc_hba *phba = vport->phba;
275 struct lpfc_rport_data *rdata;
276 unsigned long new_queue_depth, old_queue_depth;
277
278 old_queue_depth = sdev->queue_depth;
279 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
280 new_queue_depth = sdev->queue_depth;
281 rdata = sdev->hostdata;
282 if (rdata)
283 lpfc_send_sdev_queuedepth_change_event(phba, vport,
284 rdata->pnode, sdev->lun,
285 old_queue_depth,
286 new_queue_depth);
287 return sdev->queue_depth;
288}
289
290/**
James Smart3621a712009-04-06 18:47:14 -0400291 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500292 * @phba: The Hba for which this call is being executed.
293 *
294 * This routine is called when there is resource error in driver or firmware.
295 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
296 * posts at most 1 event each second. This routine wakes up worker thread of
297 * @phba to process WORKER_RAM_DOWN_EVENT event.
298 *
299 * This routine should be called with no lock held.
300 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500301void
James Smarteaf15d52008-12-04 22:39:29 -0500302lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500303{
304 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400305 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500306
307 spin_lock_irqsave(&phba->hbalock, flags);
308 atomic_inc(&phba->num_rsrc_err);
309 phba->last_rsrc_error_time = jiffies;
310
311 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
312 spin_unlock_irqrestore(&phba->hbalock, flags);
313 return;
314 }
315
316 phba->last_ramp_down_time = jiffies;
317
318 spin_unlock_irqrestore(&phba->hbalock, flags);
319
320 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400321 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
322 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500323 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500324 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
325
James Smart5e9d9b82008-06-14 22:52:53 -0400326 if (!evt_posted)
327 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500328 return;
329}
330
James Smart9bad7672008-12-04 22:39:02 -0500331/**
James Smart3621a712009-04-06 18:47:14 -0400332 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
James Smart9bad7672008-12-04 22:39:02 -0500333 * @phba: The Hba for which this call is being executed.
334 *
335 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
336 * post at most 1 event every 5 minute after last_ramp_up_time or
337 * last_rsrc_error_time. This routine wakes up worker thread of @phba
338 * to process WORKER_RAM_DOWN_EVENT event.
339 *
340 * This routine should be called with no lock held.
341 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500342static inline void
James Smart3de2a652007-08-02 11:09:59 -0400343lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smarta257bf92009-04-06 18:48:10 -0400344 uint32_t queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500345{
346 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -0400347 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400348 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500349 atomic_inc(&phba->num_cmd_success);
350
James Smarta257bf92009-04-06 18:48:10 -0400351 if (vport->cfg_lun_queue_depth <= queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500352 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500353 spin_lock_irqsave(&phba->hbalock, flags);
James Smart5ffc2662009-11-18 15:39:44 -0500354 if (time_before(jiffies,
355 phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
356 time_before(jiffies,
357 phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500358 spin_unlock_irqrestore(&phba->hbalock, flags);
359 return;
360 }
James Smart92d7f7b2007-06-17 19:56:38 -0500361 phba->last_ramp_up_time = jiffies;
362 spin_unlock_irqrestore(&phba->hbalock, flags);
363
364 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400365 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
366 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500367 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500368 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
369
James Smart5e9d9b82008-06-14 22:52:53 -0400370 if (!evt_posted)
371 lpfc_worker_wake_up(phba);
372 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500373}
374
James Smart9bad7672008-12-04 22:39:02 -0500375/**
James Smart3621a712009-04-06 18:47:14 -0400376 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500377 * @phba: The Hba for which this call is being executed.
378 *
379 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
380 * thread.This routine reduces queue depth for all scsi device on each vport
381 * associated with @phba.
382 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500383void
384lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
385{
James Smart549e55c2007-08-02 11:09:51 -0400386 struct lpfc_vport **vports;
387 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500388 struct scsi_device *sdev;
James Smart5ffc2662009-11-18 15:39:44 -0500389 unsigned long new_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500390 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400391 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500392
393 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
394 num_cmd_success = atomic_read(&phba->num_cmd_success);
395
James Smart75ad83a2012-05-09 21:18:40 -0400396 /*
397 * The error and success command counters are global per
398 * driver instance. If another handler has already
399 * operated on this error event, just exit.
400 */
401 if (num_rsrc_err == 0)
402 return;
403
James Smart549e55c2007-08-02 11:09:51 -0400404 vports = lpfc_create_vport_work_array(phba);
405 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400406 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400407 shost = lpfc_shost_from_vport(vports[i]);
408 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500409 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400410 sdev->queue_depth * num_rsrc_err /
411 (num_rsrc_err + num_cmd_success);
412 if (!new_queue_depth)
413 new_queue_depth = sdev->queue_depth - 1;
414 else
415 new_queue_depth = sdev->queue_depth -
416 new_queue_depth;
James Smart5ffc2662009-11-18 15:39:44 -0500417 lpfc_change_queue_depth(sdev, new_queue_depth,
418 SCSI_QDEPTH_DEFAULT);
James Smart549e55c2007-08-02 11:09:51 -0400419 }
James Smart92d7f7b2007-06-17 19:56:38 -0500420 }
James Smart09372822008-01-11 01:52:54 -0500421 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500422 atomic_set(&phba->num_rsrc_err, 0);
423 atomic_set(&phba->num_cmd_success, 0);
424}
425
James Smart9bad7672008-12-04 22:39:02 -0500426/**
James Smart3621a712009-04-06 18:47:14 -0400427 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500428 * @phba: The Hba for which this call is being executed.
429 *
430 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
431 * thread.This routine increases queue depth for all scsi device on each vport
432 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
433 * num_cmd_success to zero.
434 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500435void
436lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
437{
James Smart549e55c2007-08-02 11:09:51 -0400438 struct lpfc_vport **vports;
439 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500440 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400441 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500442
James Smart549e55c2007-08-02 11:09:51 -0400443 vports = lpfc_create_vport_work_array(phba);
444 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400445 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400446 shost = lpfc_shost_from_vport(vports[i]);
447 shost_for_each_device(sdev, shost) {
James Smart97eab632008-04-07 10:16:05 -0400448 if (vports[i]->cfg_lun_queue_depth <=
449 sdev->queue_depth)
450 continue;
James Smart5ffc2662009-11-18 15:39:44 -0500451 lpfc_change_queue_depth(sdev,
452 sdev->queue_depth+1,
453 SCSI_QDEPTH_RAMP_UP);
James Smart549e55c2007-08-02 11:09:51 -0400454 }
James Smart92d7f7b2007-06-17 19:56:38 -0500455 }
James Smart09372822008-01-11 01:52:54 -0500456 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500457 atomic_set(&phba->num_rsrc_err, 0);
458 atomic_set(&phba->num_cmd_success, 0);
459}
460
James Smarta8e497d2008-08-24 21:50:11 -0400461/**
James Smart3621a712009-04-06 18:47:14 -0400462 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400463 * @phba: Pointer to HBA context object.
464 *
465 * This function walks vport list and set each SCSI host to block state
466 * by invoking fc_remote_port_delete() routine. This function is invoked
467 * with EEH when device's PCI slot has been permanently disabled.
468 **/
469void
470lpfc_scsi_dev_block(struct lpfc_hba *phba)
471{
472 struct lpfc_vport **vports;
473 struct Scsi_Host *shost;
474 struct scsi_device *sdev;
475 struct fc_rport *rport;
476 int i;
477
478 vports = lpfc_create_vport_work_array(phba);
479 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400480 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400481 shost = lpfc_shost_from_vport(vports[i]);
482 shost_for_each_device(sdev, shost) {
483 rport = starget_to_rport(scsi_target(sdev));
484 fc_remote_port_delete(rport);
485 }
486 }
487 lpfc_destroy_vport_work_array(phba, vports);
488}
489
James Smart9bad7672008-12-04 22:39:02 -0500490/**
James Smart3772a992009-05-22 14:50:54 -0400491 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500492 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400493 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500494 *
James Smart3772a992009-05-22 14:50:54 -0400495 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
496 * the scsi buffer contains all the necessary information needed to initiate
497 * a SCSI I/O. The non-DMAable buffer region contains information to build
498 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
499 * and the initial BPL. In addition to allocating memory, the FCP CMND and
500 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500501 *
502 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400503 * int - number of scsi buffers that were allocated.
504 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500505 **/
James Smart3772a992009-05-22 14:50:54 -0400506static int
507lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500508{
James Smart2e0fef82007-06-17 19:56:36 -0500509 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500510 struct lpfc_scsi_buf *psb;
511 struct ulp_bde64 *bpl;
512 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400513 dma_addr_t pdma_phys_fcp_cmd;
514 dma_addr_t pdma_phys_fcp_rsp;
515 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500516 uint16_t iotag;
James Smart3772a992009-05-22 14:50:54 -0400517 int bcnt;
dea31012005-04-17 16:05:31 -0500518
James Smart3772a992009-05-22 14:50:54 -0400519 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
520 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
521 if (!psb)
522 break;
dea31012005-04-17 16:05:31 -0500523
James Smart3772a992009-05-22 14:50:54 -0400524 /*
525 * Get memory from the pci pool to map the virt space to pci
526 * bus space for an I/O. The DMA buffer includes space for the
527 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
528 * necessary to support the sg_tablesize.
529 */
530 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
531 GFP_KERNEL, &psb->dma_handle);
532 if (!psb->data) {
533 kfree(psb);
534 break;
535 }
dea31012005-04-17 16:05:31 -0500536
James Smart3772a992009-05-22 14:50:54 -0400537 /* Initialize virtual ptrs to dma_buf region. */
538 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
dea31012005-04-17 16:05:31 -0500539
James Smart3772a992009-05-22 14:50:54 -0400540 /* Allocate iotag for psb->cur_iocbq. */
541 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
542 if (iotag == 0) {
543 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
544 psb->data, psb->dma_handle);
545 kfree(psb);
546 break;
547 }
548 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500549
James Smart3772a992009-05-22 14:50:54 -0400550 psb->fcp_cmnd = psb->data;
551 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
552 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400553 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500554
James Smart3772a992009-05-22 14:50:54 -0400555 /* Initialize local short-hand pointers. */
556 bpl = psb->fcp_bpl;
557 pdma_phys_fcp_cmd = psb->dma_handle;
558 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
559 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
560 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500561
James Smart3772a992009-05-22 14:50:54 -0400562 /*
563 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
564 * are sg list bdes. Initialize the first two and leave the
565 * rest for queuecommand.
566 */
567 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
568 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
569 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
570 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
571 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500572
James Smart3772a992009-05-22 14:50:54 -0400573 /* Setup the physical region for the FCP RSP */
574 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
575 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
576 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
577 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
578 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
579
580 /*
581 * Since the IOCB for the FCP I/O is built into this
582 * lpfc_scsi_buf, initialize it with all known data now.
583 */
584 iocb = &psb->cur_iocbq.iocb;
585 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
586 if ((phba->sli_rev == 3) &&
587 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
588 /* fill in immediate fcp command BDE */
589 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
590 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
591 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
592 unsli3.fcp_ext.icd);
593 iocb->un.fcpi64.bdl.addrHigh = 0;
594 iocb->ulpBdeCount = 0;
595 iocb->ulpLe = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300596 /* fill in response BDE */
James Smart3772a992009-05-22 14:50:54 -0400597 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
598 BUFF_TYPE_BDE_64;
599 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
600 sizeof(struct fcp_rsp);
601 iocb->unsli3.fcp_ext.rbde.addrLow =
602 putPaddrLow(pdma_phys_fcp_rsp);
603 iocb->unsli3.fcp_ext.rbde.addrHigh =
604 putPaddrHigh(pdma_phys_fcp_rsp);
605 } else {
606 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
607 iocb->un.fcpi64.bdl.bdeSize =
608 (2 * sizeof(struct ulp_bde64));
609 iocb->un.fcpi64.bdl.addrLow =
610 putPaddrLow(pdma_phys_bpl);
611 iocb->un.fcpi64.bdl.addrHigh =
612 putPaddrHigh(pdma_phys_bpl);
613 iocb->ulpBdeCount = 1;
614 iocb->ulpLe = 1;
615 }
616 iocb->ulpClass = CLASS3;
617 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400618 /* Put it back into the SCSI buffer list */
James Smarteee88772010-09-29 11:19:08 -0400619 psb->cur_iocbq.context1 = psb;
James Smart1c6f4ef52009-11-18 15:40:49 -0500620 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400621
James Smart34b02dc2008-08-24 21:49:55 -0400622 }
dea31012005-04-17 16:05:31 -0500623
James Smart3772a992009-05-22 14:50:54 -0400624 return bcnt;
dea31012005-04-17 16:05:31 -0500625}
626
James Smart9bad7672008-12-04 22:39:02 -0500627/**
James Smart1151e3e2011-02-16 12:39:35 -0500628 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
629 * @vport: pointer to lpfc vport data structure.
630 *
631 * This routine is invoked by the vport cleanup for deletions and the cleanup
632 * for an ndlp on removal.
633 **/
634void
635lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
636{
637 struct lpfc_hba *phba = vport->phba;
638 struct lpfc_scsi_buf *psb, *next_psb;
639 unsigned long iflag = 0;
640
641 spin_lock_irqsave(&phba->hbalock, iflag);
642 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
643 list_for_each_entry_safe(psb, next_psb,
644 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
645 if (psb->rdata && psb->rdata->pnode
646 && psb->rdata->pnode->vport == vport)
647 psb->rdata = NULL;
648 }
649 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
650 spin_unlock_irqrestore(&phba->hbalock, iflag);
651}
652
653/**
James Smartda0436e2009-05-22 14:51:39 -0400654 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
655 * @phba: pointer to lpfc hba data structure.
656 * @axri: pointer to the fcp xri abort wcqe structure.
657 *
658 * This routine is invoked by the worker thread to process a SLI4 fast-path
659 * FCP aborted xri.
660 **/
661void
662lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
663 struct sli4_wcqe_xri_aborted *axri)
664{
665 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500666 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartda0436e2009-05-22 14:51:39 -0400667 struct lpfc_scsi_buf *psb, *next_psb;
668 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500669 struct lpfc_iocbq *iocbq;
670 int i;
James Smart19ca7602010-11-20 23:11:55 -0500671 struct lpfc_nodelist *ndlp;
672 int rrq_empty = 0;
James Smart589a52d2010-07-14 15:30:54 -0400673 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smartda0436e2009-05-22 14:51:39 -0400674
James Smart0f65ff62010-02-26 14:14:23 -0500675 spin_lock_irqsave(&phba->hbalock, iflag);
676 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400677 list_for_each_entry_safe(psb, next_psb,
678 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
679 if (psb->cur_iocbq.sli4_xritag == xri) {
680 list_del(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500681 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400682 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500683 spin_unlock(
684 &phba->sli4_hba.abts_scsi_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500685 if (psb->rdata && psb->rdata->pnode)
686 ndlp = psb->rdata->pnode;
687 else
688 ndlp = NULL;
689
James Smart19ca7602010-11-20 23:11:55 -0500690 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500691 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartcb69f7d2011-12-13 13:21:57 -0500692 if (ndlp) {
James Smartee0f4fe2012-05-09 21:19:14 -0400693 lpfc_set_rrq_active(phba, ndlp,
694 psb->cur_iocbq.sli4_lxritag, rxid, 1);
James Smartcb69f7d2011-12-13 13:21:57 -0500695 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
696 }
James Smartda0436e2009-05-22 14:51:39 -0400697 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500698 if (rrq_empty)
699 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400700 return;
701 }
702 }
James Smart0f65ff62010-02-26 14:14:23 -0500703 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
704 for (i = 1; i <= phba->sli.last_iotag; i++) {
705 iocbq = phba->sli.iocbq_lookup[i];
706
707 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
708 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
709 continue;
710 if (iocbq->sli4_xritag != xri)
711 continue;
712 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
713 psb->exch_busy = 0;
714 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart589a52d2010-07-14 15:30:54 -0400715 if (pring->txq_cnt)
716 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500717 return;
718
719 }
720 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400721}
722
723/**
James Smart8a9d2e82012-05-09 21:16:12 -0400724 * lpfc_sli4_post_scsi_sgl_list - Psot blocks of scsi buffer sgls from a list
725 * @phba: pointer to lpfc hba data structure.
726 * @post_sblist: pointer to the scsi buffer list.
727 *
728 * This routine walks a list of scsi buffers that was passed in. It attempts
729 * to construct blocks of scsi buffer sgls which contains contiguous xris and
730 * uses the non-embedded SGL block post mailbox commands to post to the port.
731 * For single SCSI buffer sgl with non-contiguous xri, if any, it shall use
732 * embedded SGL post mailbox command for posting. The @post_sblist passed in
733 * must be local list, thus no lock is needed when manipulate the list.
734 *
735 * Returns: 0 = failure, non-zero number of successfully posted buffers.
736 **/
737int
738lpfc_sli4_post_scsi_sgl_list(struct lpfc_hba *phba,
739 struct list_head *post_sblist, int sb_count)
740{
741 struct lpfc_scsi_buf *psb, *psb_next;
742 int status;
743 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
744 dma_addr_t pdma_phys_bpl1;
745 int last_xritag = NO_XRI;
746 LIST_HEAD(prep_sblist);
747 LIST_HEAD(blck_sblist);
748 LIST_HEAD(scsi_sblist);
749
750 /* sanity check */
751 if (sb_count <= 0)
752 return -EINVAL;
753
754 list_for_each_entry_safe(psb, psb_next, post_sblist, list) {
755 list_del_init(&psb->list);
756 block_cnt++;
757 if ((last_xritag != NO_XRI) &&
758 (psb->cur_iocbq.sli4_xritag != last_xritag + 1)) {
759 /* a hole in xri block, form a sgl posting block */
760 list_splice_init(&prep_sblist, &blck_sblist);
761 post_cnt = block_cnt - 1;
762 /* prepare list for next posting block */
763 list_add_tail(&psb->list, &prep_sblist);
764 block_cnt = 1;
765 } else {
766 /* prepare list for next posting block */
767 list_add_tail(&psb->list, &prep_sblist);
768 /* enough sgls for non-embed sgl mbox command */
769 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
770 list_splice_init(&prep_sblist, &blck_sblist);
771 post_cnt = block_cnt;
772 block_cnt = 0;
773 }
774 }
775 num_posting++;
776 last_xritag = psb->cur_iocbq.sli4_xritag;
777
778 /* end of repost sgl list condition for SCSI buffers */
779 if (num_posting == sb_count) {
780 if (post_cnt == 0) {
781 /* last sgl posting block */
782 list_splice_init(&prep_sblist, &blck_sblist);
783 post_cnt = block_cnt;
784 } else if (block_cnt == 1) {
785 /* last single sgl with non-contiguous xri */
786 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
787 pdma_phys_bpl1 = psb->dma_phys_bpl +
788 SGL_PAGE_SIZE;
789 else
790 pdma_phys_bpl1 = 0;
791 status = lpfc_sli4_post_sgl(phba,
792 psb->dma_phys_bpl,
793 pdma_phys_bpl1,
794 psb->cur_iocbq.sli4_xritag);
795 if (status) {
796 /* failure, put on abort scsi list */
797 psb->exch_busy = 1;
798 } else {
799 /* success, put on SCSI buffer list */
800 psb->exch_busy = 0;
801 psb->status = IOSTAT_SUCCESS;
802 num_posted++;
803 }
804 /* success, put on SCSI buffer sgl list */
805 list_add_tail(&psb->list, &scsi_sblist);
806 }
807 }
808
809 /* continue until a nembed page worth of sgls */
810 if (post_cnt == 0)
811 continue;
812
813 /* post block of SCSI buffer list sgls */
814 status = lpfc_sli4_post_scsi_sgl_block(phba, &blck_sblist,
815 post_cnt);
816
817 /* don't reset xirtag due to hole in xri block */
818 if (block_cnt == 0)
819 last_xritag = NO_XRI;
820
821 /* reset SCSI buffer post count for next round of posting */
822 post_cnt = 0;
823
824 /* put posted SCSI buffer-sgl posted on SCSI buffer sgl list */
825 while (!list_empty(&blck_sblist)) {
826 list_remove_head(&blck_sblist, psb,
827 struct lpfc_scsi_buf, list);
828 if (status) {
829 /* failure, put on abort scsi list */
830 psb->exch_busy = 1;
831 } else {
832 /* success, put on SCSI buffer list */
833 psb->exch_busy = 0;
834 psb->status = IOSTAT_SUCCESS;
835 num_posted++;
836 }
837 list_add_tail(&psb->list, &scsi_sblist);
838 }
839 }
840 /* Push SCSI buffers with sgl posted to the availble list */
841 while (!list_empty(&scsi_sblist)) {
842 list_remove_head(&scsi_sblist, psb,
843 struct lpfc_scsi_buf, list);
844 lpfc_release_scsi_buf_s4(phba, psb);
845 }
846 return num_posted;
847}
848
849/**
850 * lpfc_sli4_repost_scsi_sgl_list - Repsot all the allocated scsi buffer sgls
James Smartda0436e2009-05-22 14:51:39 -0400851 * @phba: pointer to lpfc hba data structure.
852 *
853 * This routine walks the list of scsi buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -0400854 * repost them to the port by using SGL block post. This is needed after a
James Smartda0436e2009-05-22 14:51:39 -0400855 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
856 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
857 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
858 *
859 * Returns: 0 = success, non-zero failure.
860 **/
861int
862lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
863{
James Smart8a9d2e82012-05-09 21:16:12 -0400864 LIST_HEAD(post_sblist);
865 int num_posted, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -0400866
James Smart8a9d2e82012-05-09 21:16:12 -0400867 /* get all SCSI buffers need to repost to a local list */
868 spin_lock(&phba->scsi_buf_list_lock);
869 list_splice_init(&phba->lpfc_scsi_buf_list, &post_sblist);
870 spin_unlock(&phba->scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400871
James Smart8a9d2e82012-05-09 21:16:12 -0400872 /* post the list of scsi buffer sgls to port if available */
873 if (!list_empty(&post_sblist)) {
874 num_posted = lpfc_sli4_post_scsi_sgl_list(phba, &post_sblist,
875 phba->sli4_hba.scsi_xri_cnt);
876 /* failed to post any scsi buffer, return error */
877 if (num_posted == 0)
878 rc = -EIO;
James Smartda0436e2009-05-22 14:51:39 -0400879 }
880 return rc;
881}
882
883/**
884 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
885 * @vport: The virtual port for which this call being executed.
886 * @num_to_allocate: The requested number of buffers to allocate.
887 *
James Smart8a9d2e82012-05-09 21:16:12 -0400888 * This routine allocates scsi buffers for device with SLI-4 interface spec,
James Smartda0436e2009-05-22 14:51:39 -0400889 * the scsi buffer contains all the necessary information needed to initiate
James Smart8a9d2e82012-05-09 21:16:12 -0400890 * a SCSI I/O. After allocating up to @num_to_allocate SCSI buffers and put
891 * them on a list, it post them to the port by using SGL block post.
James Smartda0436e2009-05-22 14:51:39 -0400892 *
893 * Return codes:
James Smart8a9d2e82012-05-09 21:16:12 -0400894 * int - number of scsi buffers that were allocated and posted.
James Smartda0436e2009-05-22 14:51:39 -0400895 * 0 = failure, less than num_to_alloc is a partial failure.
896 **/
897static int
898lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
899{
900 struct lpfc_hba *phba = vport->phba;
901 struct lpfc_scsi_buf *psb;
902 struct sli4_sge *sgl;
903 IOCB_t *iocb;
904 dma_addr_t pdma_phys_fcp_cmd;
905 dma_addr_t pdma_phys_fcp_rsp;
906 dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
James Smart8a9d2e82012-05-09 21:16:12 -0400907 uint16_t iotag, lxri = 0;
908 int bcnt, num_posted;
909 LIST_HEAD(prep_sblist);
910 LIST_HEAD(post_sblist);
911 LIST_HEAD(scsi_sblist);
James Smartda0436e2009-05-22 14:51:39 -0400912
913 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
914 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
915 if (!psb)
916 break;
James Smartda0436e2009-05-22 14:51:39 -0400917 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400918 * Get memory from the pci pool to map the virt space to
919 * pci bus space for an I/O. The DMA buffer includes space
920 * for the struct fcp_cmnd, struct fcp_rsp and the number
921 * of bde's necessary to support the sg_tablesize.
James Smartda0436e2009-05-22 14:51:39 -0400922 */
923 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
924 GFP_KERNEL, &psb->dma_handle);
925 if (!psb->data) {
926 kfree(psb);
927 break;
928 }
James Smartda0436e2009-05-22 14:51:39 -0400929 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
930
931 /* Allocate iotag for psb->cur_iocbq. */
932 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
933 if (iotag == 0) {
James Smartb92938b2010-06-07 15:24:12 -0400934 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
935 psb->data, psb->dma_handle);
James Smartda0436e2009-05-22 14:51:39 -0400936 kfree(psb);
937 break;
938 }
939
James Smart6d368e52011-05-24 11:44:12 -0400940 lxri = lpfc_sli4_next_xritag(phba);
941 if (lxri == NO_XRI) {
James Smartda0436e2009-05-22 14:51:39 -0400942 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
943 psb->data, psb->dma_handle);
944 kfree(psb);
945 break;
946 }
James Smart6d368e52011-05-24 11:44:12 -0400947 psb->cur_iocbq.sli4_lxritag = lxri;
948 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smartda0436e2009-05-22 14:51:39 -0400949 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Smartda0436e2009-05-22 14:51:39 -0400950 psb->fcp_bpl = psb->data;
951 psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
952 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
953 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
954 sizeof(struct fcp_cmnd));
955
956 /* Initialize local short-hand pointers. */
957 sgl = (struct sli4_sge *)psb->fcp_bpl;
958 pdma_phys_bpl = psb->dma_handle;
959 pdma_phys_fcp_cmd =
960 (psb->dma_handle + phba->cfg_sg_dma_buf_size)
961 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
962 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
963
964 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400965 * The first two bdes are the FCP_CMD and FCP_RSP.
966 * The balance are sg list bdes. Initialize the
967 * first two and leave the rest for queuecommand.
James Smartda0436e2009-05-22 14:51:39 -0400968 */
969 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
970 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
James Smart05580562011-05-24 11:40:48 -0400971 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400972 bf_set(lpfc_sli4_sge_last, sgl, 0);
973 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500974 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
James Smartda0436e2009-05-22 14:51:39 -0400975 sgl++;
976
977 /* Setup the physical region for the FCP RSP */
978 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
979 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smart05580562011-05-24 11:40:48 -0400980 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400981 bf_set(lpfc_sli4_sge_last, sgl, 1);
982 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500983 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -0400984
985 /*
986 * Since the IOCB for the FCP I/O is built into this
987 * lpfc_scsi_buf, initialize it with all known data now.
988 */
989 iocb = &psb->cur_iocbq.iocb;
990 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
991 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
992 /* setting the BLP size to 2 * sizeof BDE may not be correct.
993 * We are setting the bpl to point to out sgl. An sgl's
994 * entries are 16 bytes, a bpl entries are 12 bytes.
995 */
996 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
997 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
998 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
999 iocb->ulpBdeCount = 1;
1000 iocb->ulpLe = 1;
1001 iocb->ulpClass = CLASS3;
James Smart8a9d2e82012-05-09 21:16:12 -04001002 psb->cur_iocbq.context1 = psb;
James Smartda0436e2009-05-22 14:51:39 -04001003 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
1004 pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
1005 else
1006 pdma_phys_bpl1 = 0;
1007 psb->dma_phys_bpl = pdma_phys_bpl;
James Smart6d368e52011-05-24 11:44:12 -04001008
James Smart8a9d2e82012-05-09 21:16:12 -04001009 /* add the scsi buffer to a post list */
1010 list_add_tail(&psb->list, &post_sblist);
1011 spin_lock_irq(&phba->scsi_buf_list_lock);
1012 phba->sli4_hba.scsi_xri_cnt++;
1013 spin_unlock_irq(&phba->scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04001014 }
James Smart8a9d2e82012-05-09 21:16:12 -04001015 lpfc_printf_log(phba, KERN_INFO, LOG_BG,
1016 "3021 Allocate %d out of %d requested new SCSI "
1017 "buffers\n", bcnt, num_to_alloc);
James Smartda0436e2009-05-22 14:51:39 -04001018
James Smart8a9d2e82012-05-09 21:16:12 -04001019 /* post the list of scsi buffer sgls to port if available */
1020 if (!list_empty(&post_sblist))
1021 num_posted = lpfc_sli4_post_scsi_sgl_list(phba,
1022 &post_sblist, bcnt);
1023 else
1024 num_posted = 0;
1025
1026 return num_posted;
James Smartda0436e2009-05-22 14:51:39 -04001027}
1028
1029/**
James Smart3772a992009-05-22 14:50:54 -04001030 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
1031 * @vport: The virtual port for which this call being executed.
1032 * @num_to_allocate: The requested number of buffers to allocate.
1033 *
1034 * This routine wraps the actual SCSI buffer allocator function pointer from
1035 * the lpfc_hba struct.
1036 *
1037 * Return codes:
1038 * int - number of scsi buffers that were allocated.
1039 * 0 = failure, less than num_to_alloc is a partial failure.
1040 **/
1041static inline int
1042lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
1043{
1044 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
1045}
1046
1047/**
James Smart19ca7602010-11-20 23:11:55 -05001048 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -04001049 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05001050 *
1051 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1052 * and returns to caller.
1053 *
1054 * Return codes:
1055 * NULL - Error
1056 * Pointer to lpfc_scsi_buf - Success
1057 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +01001058static struct lpfc_scsi_buf*
James Smart19ca7602010-11-20 23:11:55 -05001059lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001060{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001061 struct lpfc_scsi_buf * lpfc_cmd = NULL;
1062 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001063 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001064
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001065 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001066 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -04001067 if (lpfc_cmd) {
1068 lpfc_cmd->seg_cnt = 0;
1069 lpfc_cmd->nonsg_phys = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001070 lpfc_cmd->prot_seg_cnt = 0;
James Smart1dcb58e2007-04-25 09:51:30 -04001071 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001072 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001073 return lpfc_cmd;
1074}
James Smart19ca7602010-11-20 23:11:55 -05001075/**
1076 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1077 * @phba: The HBA for which this call is being executed.
1078 *
1079 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1080 * and returns to caller.
1081 *
1082 * Return codes:
1083 * NULL - Error
1084 * Pointer to lpfc_scsi_buf - Success
1085 **/
1086static struct lpfc_scsi_buf*
1087lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1088{
James Smart1151e3e2011-02-16 12:39:35 -05001089 struct lpfc_scsi_buf *lpfc_cmd ;
James Smart19ca7602010-11-20 23:11:55 -05001090 unsigned long iflag = 0;
1091 int found = 0;
1092
1093 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -05001094 list_for_each_entry(lpfc_cmd, &phba->lpfc_scsi_buf_list,
1095 list) {
James Smart19ca7602010-11-20 23:11:55 -05001096 if (lpfc_test_rrq_active(phba, ndlp,
James Smartee0f4fe2012-05-09 21:19:14 -04001097 lpfc_cmd->cur_iocbq.sli4_lxritag))
James Smart1151e3e2011-02-16 12:39:35 -05001098 continue;
1099 list_del(&lpfc_cmd->list);
James Smart19ca7602010-11-20 23:11:55 -05001100 found = 1;
1101 lpfc_cmd->seg_cnt = 0;
1102 lpfc_cmd->nonsg_phys = 0;
1103 lpfc_cmd->prot_seg_cnt = 0;
James Smart1151e3e2011-02-16 12:39:35 -05001104 break;
James Smart19ca7602010-11-20 23:11:55 -05001105 }
James Smart1151e3e2011-02-16 12:39:35 -05001106 spin_unlock_irqrestore(&phba->scsi_buf_list_lock,
1107 iflag);
1108 if (!found)
1109 return NULL;
1110 else
1111 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001112}
1113/**
1114 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1115 * @phba: The HBA for which this call is being executed.
1116 *
1117 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1118 * and returns to caller.
1119 *
1120 * Return codes:
1121 * NULL - Error
1122 * Pointer to lpfc_scsi_buf - Success
1123 **/
1124static struct lpfc_scsi_buf*
1125lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1126{
1127 return phba->lpfc_get_scsi_buf(phba, ndlp);
1128}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001129
James Smart9bad7672008-12-04 22:39:02 -05001130/**
James Smart3772a992009-05-22 14:50:54 -04001131 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -05001132 * @phba: The Hba for which this call is being executed.
1133 * @psb: The scsi buffer which is being released.
1134 *
1135 * This routine releases @psb scsi buffer by adding it to tail of @phba
1136 * lpfc_scsi_buf_list list.
1137 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001138static void
James Smart3772a992009-05-22 14:50:54 -04001139lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001140{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001141 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001142
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001143 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001144 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -05001145 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001146 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -05001147}
1148
James Smart9bad7672008-12-04 22:39:02 -05001149/**
James Smartda0436e2009-05-22 14:51:39 -04001150 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1151 * @phba: The Hba for which this call is being executed.
1152 * @psb: The scsi buffer which is being released.
1153 *
1154 * This routine releases @psb scsi buffer by adding it to tail of @phba
1155 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1156 * and cannot be reused for at least RA_TOV amount of time if it was
1157 * aborted.
1158 **/
1159static void
1160lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1161{
1162 unsigned long iflag = 0;
1163
James Smart341af102010-01-26 23:07:37 -05001164 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -04001165 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1166 iflag);
1167 psb->pCmd = NULL;
1168 list_add_tail(&psb->list,
1169 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1170 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1171 iflag);
1172 } else {
1173
1174 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1175 psb->pCmd = NULL;
1176 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
1177 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
1178 }
1179}
1180
1181/**
James Smart3772a992009-05-22 14:50:54 -04001182 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1183 * @phba: The Hba for which this call is being executed.
1184 * @psb: The scsi buffer which is being released.
1185 *
1186 * This routine releases @psb scsi buffer by adding it to tail of @phba
1187 * lpfc_scsi_buf_list list.
1188 **/
1189static void
1190lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1191{
1192
1193 phba->lpfc_release_scsi_buf(phba, psb);
1194}
1195
1196/**
1197 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001198 * @phba: The Hba for which this call is being executed.
1199 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1200 *
1201 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001202 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1203 * through sg elements and format the bdea. This routine also initializes all
1204 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001205 *
1206 * Return codes:
1207 * 1 - Error
1208 * 0 - Success
1209 **/
dea31012005-04-17 16:05:31 -05001210static int
James Smart3772a992009-05-22 14:50:54 -04001211lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001212{
1213 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1214 struct scatterlist *sgel = NULL;
1215 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1216 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001217 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001218 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001219 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001220 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001221 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001222 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001223
1224 /*
1225 * There are three possibilities here - use scatter-gather segment, use
1226 * the single mapping, or neither. Start the lpfc command prep by
1227 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1228 * data bde entry.
1229 */
1230 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001231 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001232 /*
1233 * The driver stores the segment count returned from pci_map_sg
1234 * because this a count of dma-mappings used to map the use_sg
1235 * pages. They are not guaranteed to be the same for those
1236 * architectures that implement an IOMMU.
1237 */
dea31012005-04-17 16:05:31 -05001238
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001239 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1240 scsi_sg_count(scsi_cmnd), datadir);
1241 if (unlikely(!nseg))
1242 return 1;
1243
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001244 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001245 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001246 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1247 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001248 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001249 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001250 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001251 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001252 return 1;
1253 }
1254
1255 /*
1256 * The driver established a maximum scatter-gather segment count
1257 * during probe that limits the number of sg elements in any
1258 * single scsi command. Just run through the seg_cnt and format
1259 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001260 * When using SLI-3 the driver will try to fit all the BDEs into
1261 * the IOCB. If it can't then the BDEs get added to a BPL as it
1262 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001263 */
James Smart34b02dc2008-08-24 21:49:55 -04001264 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001265 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001266 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001267 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001268 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001269 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1270 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1271 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1272 data_bde->addrLow = putPaddrLow(physaddr);
1273 data_bde->addrHigh = putPaddrHigh(physaddr);
1274 data_bde++;
1275 } else {
1276 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1277 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1278 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1279 bpl->addrLow =
1280 le32_to_cpu(putPaddrLow(physaddr));
1281 bpl->addrHigh =
1282 le32_to_cpu(putPaddrHigh(physaddr));
1283 bpl++;
1284 }
dea31012005-04-17 16:05:31 -05001285 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001286 }
dea31012005-04-17 16:05:31 -05001287
1288 /*
1289 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001290 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1291 * explicitly reinitialized and for SLI-3 the extended bde count is
1292 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001293 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001294 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001295 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1296 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001297 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1298 /*
1299 * The extended IOCB format can only fit 3 BDE or a BPL.
1300 * This I/O has more than 3 BDE so the 1st data bde will
1301 * be a BPL that is filled in here.
1302 */
1303 physaddr = lpfc_cmd->dma_handle;
1304 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1305 data_bde->tus.f.bdeSize = (num_bde *
1306 sizeof(struct ulp_bde64));
1307 physaddr += (sizeof(struct fcp_cmnd) +
1308 sizeof(struct fcp_rsp) +
1309 (2 * sizeof(struct ulp_bde64)));
1310 data_bde->addrHigh = putPaddrHigh(physaddr);
1311 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001312 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -04001313 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1314 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001315 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -04001316 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1317 }
1318 } else {
1319 iocb_cmd->un.fcpi64.bdl.bdeSize =
1320 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001321 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001322 }
James Smart09372822008-01-11 01:52:54 -05001323 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001324
1325 /*
1326 * Due to difference in data length between DIF/non-DIF paths,
1327 * we need to set word 4 of IOCB here
1328 */
James Smarta257bf92009-04-06 18:48:10 -04001329 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001330 return 0;
1331}
1332
James Smartf9bb2da2011-10-10 21:34:11 -04001333static inline unsigned
1334lpfc_cmd_blksize(struct scsi_cmnd *sc)
1335{
1336 return sc->device->sector_size;
1337}
1338
1339#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001340
James Smart9a6b09c2012-03-01 22:37:42 -05001341/* Return if if error injection is detected by Initiator */
1342#define BG_ERR_INIT 0x1
1343/* Return if if error injection is detected by Target */
1344#define BG_ERR_TGT 0x2
1345/* Return if if swapping CSUM<-->CRC is required for error injection */
1346#define BG_ERR_SWAP 0x10
1347/* Return if disabling Guard/Ref/App checking is required for error injection */
1348#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -05001349
1350/**
1351 * lpfc_bg_err_inject - Determine if we should inject an error
1352 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -04001353 * @sc: The SCSI command to examine
1354 * @reftag: (out) BlockGuard reference tag for transmitted data
1355 * @apptag: (out) BlockGuard application tag for transmitted data
1356 * @new_guard (in) Value to replace CRC with if needed
1357 *
James Smart9a6b09c2012-03-01 22:37:42 -05001358 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -05001359 **/
James Smartf9bb2da2011-10-10 21:34:11 -04001360static int
1361lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1362 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1363{
1364 struct scatterlist *sgpe; /* s/g prot entry */
1365 struct scatterlist *sgde; /* s/g data entry */
James Smart9a6b09c2012-03-01 22:37:42 -05001366 struct lpfc_scsi_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -05001367 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -05001368 struct lpfc_nodelist *ndlp;
1369 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -04001370 uint32_t op = scsi_get_prot_op(sc);
1371 uint32_t blksize;
1372 uint32_t numblks;
1373 sector_t lba;
1374 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001375 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001376
1377 if (op == SCSI_PROT_NORMAL)
1378 return 0;
1379
James Smartacd68592012-01-18 16:25:09 -05001380 sgpe = scsi_prot_sglist(sc);
1381 sgde = scsi_sglist(sc);
James Smartf9bb2da2011-10-10 21:34:11 -04001382 lba = scsi_get_lba(sc);
James Smart4ac9b222012-03-01 22:38:29 -05001383
1384 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001385 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1386 blksize = lpfc_cmd_blksize(sc);
1387 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1388
1389 /* Make sure we have the right LBA if one is specified */
1390 if ((phba->lpfc_injerr_lba < lba) ||
1391 (phba->lpfc_injerr_lba >= (lba + numblks)))
1392 return 0;
James Smartacd68592012-01-18 16:25:09 -05001393 if (sgpe) {
1394 blockoff = phba->lpfc_injerr_lba - lba;
1395 numblks = sg_dma_len(sgpe) /
1396 sizeof(struct scsi_dif_tuple);
1397 if (numblks < blockoff)
1398 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001399 }
James Smartf9bb2da2011-10-10 21:34:11 -04001400 }
1401
James Smart4ac9b222012-03-01 22:38:29 -05001402 /* Next check if we need to match the remote NPortID or WWPN */
1403 rdata = sc->device->hostdata;
1404 if (rdata && rdata->pnode) {
1405 ndlp = rdata->pnode;
1406
1407 /* Make sure we have the right NPortID if one is specified */
1408 if (phba->lpfc_injerr_nportid &&
1409 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1410 return 0;
1411
1412 /*
1413 * Make sure we have the right WWPN if one is specified.
1414 * wwn[0] should be a non-zero NAA in a good WWPN.
1415 */
1416 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1417 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1418 sizeof(struct lpfc_name)) != 0))
1419 return 0;
1420 }
1421
1422 /* Setup a ptr to the protection data if the SCSI host provides it */
1423 if (sgpe) {
1424 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1425 src += blockoff;
1426 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1427 }
1428
James Smartf9bb2da2011-10-10 21:34:11 -04001429 /* Should we change the Reference Tag */
1430 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001431 if (phba->lpfc_injerr_wref_cnt) {
1432 switch (op) {
1433 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001434 if (src) {
1435 /*
1436 * For WRITE_PASS, force the error
1437 * to be sent on the wire. It should
1438 * be detected by the Target.
1439 * If blockoff != 0 error will be
1440 * inserted in middle of the IO.
1441 */
James Smartf9bb2da2011-10-10 21:34:11 -04001442
James Smartacd68592012-01-18 16:25:09 -05001443 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1444 "9076 BLKGRD: Injecting reftag error: "
1445 "write lba x%lx + x%x oldrefTag x%x\n",
1446 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001447 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001448
1449 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001450 * Save the old ref_tag so we can
1451 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001452 */
James Smart9a6b09c2012-03-01 22:37:42 -05001453 if (lpfc_cmd) {
1454 lpfc_cmd->prot_data_type =
1455 LPFC_INJERR_REFTAG;
1456 lpfc_cmd->prot_data_segment =
1457 src;
1458 lpfc_cmd->prot_data =
1459 src->ref_tag;
1460 }
1461 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001462 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001463 if (phba->lpfc_injerr_wref_cnt == 0) {
1464 phba->lpfc_injerr_nportid = 0;
1465 phba->lpfc_injerr_lba =
1466 LPFC_INJERR_LBA_OFF;
1467 memset(&phba->lpfc_injerr_wwpn,
1468 0, sizeof(struct lpfc_name));
1469 }
James Smart9a6b09c2012-03-01 22:37:42 -05001470 rc = BG_ERR_TGT | BG_ERR_CHECK;
1471
James Smartacd68592012-01-18 16:25:09 -05001472 break;
1473 }
1474 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001475 case SCSI_PROT_WRITE_INSERT:
1476 /*
1477 * For WRITE_INSERT, force the error
1478 * to be sent on the wire. It should be
1479 * detected by the Target.
1480 */
1481 /* DEADBEEF will be the reftag on the wire */
1482 *reftag = 0xDEADBEEF;
1483 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001484 if (phba->lpfc_injerr_wref_cnt == 0) {
1485 phba->lpfc_injerr_nportid = 0;
1486 phba->lpfc_injerr_lba =
1487 LPFC_INJERR_LBA_OFF;
1488 memset(&phba->lpfc_injerr_wwpn,
1489 0, sizeof(struct lpfc_name));
1490 }
James Smart9a6b09c2012-03-01 22:37:42 -05001491 rc = BG_ERR_TGT | BG_ERR_CHECK;
1492
1493 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1494 "9078 BLKGRD: Injecting reftag error: "
1495 "write lba x%lx\n", (unsigned long)lba);
1496 break;
James Smartacd68592012-01-18 16:25:09 -05001497 case SCSI_PROT_WRITE_STRIP:
1498 /*
1499 * For WRITE_STRIP and WRITE_PASS,
1500 * force the error on data
1501 * being copied from SLI-Host to SLI-Port.
1502 */
1503 *reftag = 0xDEADBEEF;
1504 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001505 if (phba->lpfc_injerr_wref_cnt == 0) {
1506 phba->lpfc_injerr_nportid = 0;
1507 phba->lpfc_injerr_lba =
1508 LPFC_INJERR_LBA_OFF;
1509 memset(&phba->lpfc_injerr_wwpn,
1510 0, sizeof(struct lpfc_name));
1511 }
James Smartacd68592012-01-18 16:25:09 -05001512 rc = BG_ERR_INIT;
1513
1514 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1515 "9077 BLKGRD: Injecting reftag error: "
1516 "write lba x%lx\n", (unsigned long)lba);
1517 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001518 }
James Smartacd68592012-01-18 16:25:09 -05001519 }
1520 if (phba->lpfc_injerr_rref_cnt) {
1521 switch (op) {
1522 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001523 case SCSI_PROT_READ_STRIP:
1524 case SCSI_PROT_READ_PASS:
1525 /*
1526 * For READ_STRIP and READ_PASS, force the
1527 * error on data being read off the wire. It
1528 * should force an IO error to the driver.
1529 */
James Smartf9bb2da2011-10-10 21:34:11 -04001530 *reftag = 0xDEADBEEF;
1531 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001532 if (phba->lpfc_injerr_rref_cnt == 0) {
1533 phba->lpfc_injerr_nportid = 0;
1534 phba->lpfc_injerr_lba =
1535 LPFC_INJERR_LBA_OFF;
1536 memset(&phba->lpfc_injerr_wwpn,
1537 0, sizeof(struct lpfc_name));
1538 }
James Smartacd68592012-01-18 16:25:09 -05001539 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001540
1541 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001542 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001543 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001544 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001545 }
1546 }
1547 }
1548
1549 /* Should we change the Application Tag */
1550 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001551 if (phba->lpfc_injerr_wapp_cnt) {
1552 switch (op) {
1553 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001554 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001555 /*
1556 * For WRITE_PASS, force the error
1557 * to be sent on the wire. It should
1558 * be detected by the Target.
1559 * If blockoff != 0 error will be
1560 * inserted in middle of the IO.
1561 */
1562
James Smartacd68592012-01-18 16:25:09 -05001563 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1564 "9080 BLKGRD: Injecting apptag error: "
1565 "write lba x%lx + x%x oldappTag x%x\n",
1566 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001567 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001568
1569 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001570 * Save the old app_tag so we can
1571 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001572 */
James Smart9a6b09c2012-03-01 22:37:42 -05001573 if (lpfc_cmd) {
1574 lpfc_cmd->prot_data_type =
1575 LPFC_INJERR_APPTAG;
1576 lpfc_cmd->prot_data_segment =
1577 src;
1578 lpfc_cmd->prot_data =
1579 src->app_tag;
1580 }
1581 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001582 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001583 if (phba->lpfc_injerr_wapp_cnt == 0) {
1584 phba->lpfc_injerr_nportid = 0;
1585 phba->lpfc_injerr_lba =
1586 LPFC_INJERR_LBA_OFF;
1587 memset(&phba->lpfc_injerr_wwpn,
1588 0, sizeof(struct lpfc_name));
1589 }
James Smart9a6b09c2012-03-01 22:37:42 -05001590 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001591 break;
1592 }
1593 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001594 case SCSI_PROT_WRITE_INSERT:
1595 /*
1596 * For WRITE_INSERT, force the
1597 * error to be sent on the wire. It should be
1598 * detected by the Target.
1599 */
1600 /* DEAD will be the apptag on the wire */
1601 *apptag = 0xDEAD;
1602 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001603 if (phba->lpfc_injerr_wapp_cnt == 0) {
1604 phba->lpfc_injerr_nportid = 0;
1605 phba->lpfc_injerr_lba =
1606 LPFC_INJERR_LBA_OFF;
1607 memset(&phba->lpfc_injerr_wwpn,
1608 0, sizeof(struct lpfc_name));
1609 }
James Smart9a6b09c2012-03-01 22:37:42 -05001610 rc = BG_ERR_TGT | BG_ERR_CHECK;
1611
1612 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1613 "0813 BLKGRD: Injecting apptag error: "
1614 "write lba x%lx\n", (unsigned long)lba);
1615 break;
James Smartacd68592012-01-18 16:25:09 -05001616 case SCSI_PROT_WRITE_STRIP:
1617 /*
1618 * For WRITE_STRIP and WRITE_PASS,
1619 * force the error on data
1620 * being copied from SLI-Host to SLI-Port.
1621 */
1622 *apptag = 0xDEAD;
1623 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001624 if (phba->lpfc_injerr_wapp_cnt == 0) {
1625 phba->lpfc_injerr_nportid = 0;
1626 phba->lpfc_injerr_lba =
1627 LPFC_INJERR_LBA_OFF;
1628 memset(&phba->lpfc_injerr_wwpn,
1629 0, sizeof(struct lpfc_name));
1630 }
James Smartacd68592012-01-18 16:25:09 -05001631 rc = BG_ERR_INIT;
1632
1633 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1634 "0812 BLKGRD: Injecting apptag error: "
1635 "write lba x%lx\n", (unsigned long)lba);
1636 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001637 }
James Smartacd68592012-01-18 16:25:09 -05001638 }
1639 if (phba->lpfc_injerr_rapp_cnt) {
1640 switch (op) {
1641 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001642 case SCSI_PROT_READ_STRIP:
1643 case SCSI_PROT_READ_PASS:
1644 /*
1645 * For READ_STRIP and READ_PASS, force the
1646 * error on data being read off the wire. It
1647 * should force an IO error to the driver.
1648 */
James Smartf9bb2da2011-10-10 21:34:11 -04001649 *apptag = 0xDEAD;
1650 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001651 if (phba->lpfc_injerr_rapp_cnt == 0) {
1652 phba->lpfc_injerr_nportid = 0;
1653 phba->lpfc_injerr_lba =
1654 LPFC_INJERR_LBA_OFF;
1655 memset(&phba->lpfc_injerr_wwpn,
1656 0, sizeof(struct lpfc_name));
1657 }
James Smartacd68592012-01-18 16:25:09 -05001658 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001659
1660 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001661 "0814 BLKGRD: Injecting apptag error: "
1662 "read lba x%lx\n", (unsigned long)lba);
1663 break;
1664 }
1665 }
1666 }
1667
1668
1669 /* Should we change the Guard Tag */
1670 if (new_guard) {
1671 if (phba->lpfc_injerr_wgrd_cnt) {
1672 switch (op) {
1673 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001674 rc = BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001675 /* Drop thru */
James Smartacd68592012-01-18 16:25:09 -05001676
James Smartacd68592012-01-18 16:25:09 -05001677 case SCSI_PROT_WRITE_INSERT:
1678 /*
1679 * For WRITE_INSERT, force the
1680 * error to be sent on the wire. It should be
1681 * detected by the Target.
1682 */
1683 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001684 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1685 phba->lpfc_injerr_nportid = 0;
1686 phba->lpfc_injerr_lba =
1687 LPFC_INJERR_LBA_OFF;
1688 memset(&phba->lpfc_injerr_wwpn,
1689 0, sizeof(struct lpfc_name));
1690 }
James Smartacd68592012-01-18 16:25:09 -05001691
James Smart9a6b09c2012-03-01 22:37:42 -05001692 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001693 /* Signals the caller to swap CRC->CSUM */
1694
1695 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1696 "0817 BLKGRD: Injecting guard error: "
1697 "write lba x%lx\n", (unsigned long)lba);
1698 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001699 case SCSI_PROT_WRITE_STRIP:
1700 /*
1701 * For WRITE_STRIP and WRITE_PASS,
1702 * force the error on data
1703 * being copied from SLI-Host to SLI-Port.
1704 */
1705 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001706 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1707 phba->lpfc_injerr_nportid = 0;
1708 phba->lpfc_injerr_lba =
1709 LPFC_INJERR_LBA_OFF;
1710 memset(&phba->lpfc_injerr_wwpn,
1711 0, sizeof(struct lpfc_name));
1712 }
James Smart9a6b09c2012-03-01 22:37:42 -05001713
1714 rc = BG_ERR_INIT | BG_ERR_SWAP;
1715 /* Signals the caller to swap CRC->CSUM */
1716
1717 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1718 "0816 BLKGRD: Injecting guard error: "
1719 "write lba x%lx\n", (unsigned long)lba);
1720 break;
James Smartacd68592012-01-18 16:25:09 -05001721 }
1722 }
1723 if (phba->lpfc_injerr_rgrd_cnt) {
1724 switch (op) {
1725 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001726 case SCSI_PROT_READ_STRIP:
1727 case SCSI_PROT_READ_PASS:
1728 /*
1729 * For READ_STRIP and READ_PASS, force the
1730 * error on data being read off the wire. It
1731 * should force an IO error to the driver.
1732 */
James Smartacd68592012-01-18 16:25:09 -05001733 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001734 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1735 phba->lpfc_injerr_nportid = 0;
1736 phba->lpfc_injerr_lba =
1737 LPFC_INJERR_LBA_OFF;
1738 memset(&phba->lpfc_injerr_wwpn,
1739 0, sizeof(struct lpfc_name));
1740 }
James Smartacd68592012-01-18 16:25:09 -05001741
James Smart9a6b09c2012-03-01 22:37:42 -05001742 rc = BG_ERR_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001743 /* Signals the caller to swap CRC->CSUM */
1744
1745 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1746 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001747 "read lba x%lx\n", (unsigned long)lba);
1748 }
1749 }
1750 }
1751
James Smartf9bb2da2011-10-10 21:34:11 -04001752 return rc;
1753}
1754#endif
1755
James Smartacd68592012-01-18 16:25:09 -05001756/**
1757 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1758 * the specified SCSI command.
1759 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001760 * @sc: The SCSI command to examine
1761 * @txopt: (out) BlockGuard operation for transmitted data
1762 * @rxopt: (out) BlockGuard operation for received data
1763 *
1764 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1765 *
James Smartacd68592012-01-18 16:25:09 -05001766 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001767static int
James Smart6c8eea52010-04-06 14:49:53 -04001768lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1769 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001770{
1771 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
James Smart6c8eea52010-04-06 14:49:53 -04001772 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001773
1774 if (guard_type == SHOST_DIX_GUARD_IP) {
1775 switch (scsi_get_prot_op(sc)) {
1776 case SCSI_PROT_READ_INSERT:
1777 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001778 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001779 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001780 break;
1781
1782 case SCSI_PROT_READ_STRIP:
1783 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001784 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001785 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001786 break;
1787
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001788 case SCSI_PROT_READ_PASS:
1789 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001790 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001791 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001792 break;
1793
James Smarte2a0a9d2008-12-04 22:40:02 -05001794 case SCSI_PROT_NORMAL:
1795 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001796 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001797 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1798 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001799 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001800 break;
1801
1802 }
James Smart7c56b9f2011-07-22 18:36:25 -04001803 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001804 switch (scsi_get_prot_op(sc)) {
1805 case SCSI_PROT_READ_STRIP:
1806 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001807 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001808 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001809 break;
1810
1811 case SCSI_PROT_READ_PASS:
1812 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001813 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001814 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001815 break;
1816
James Smarte2a0a9d2008-12-04 22:40:02 -05001817 case SCSI_PROT_READ_INSERT:
1818 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001819 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001820 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001821 break;
1822
James Smarte2a0a9d2008-12-04 22:40:02 -05001823 case SCSI_PROT_NORMAL:
1824 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001825 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001826 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1827 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001828 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001829 break;
1830 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001831 }
1832
James Smart6c8eea52010-04-06 14:49:53 -04001833 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001834}
1835
James Smartacd68592012-01-18 16:25:09 -05001836#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1837/**
1838 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1839 * the specified SCSI command in order to force a guard tag error.
1840 * @phba: The Hba for which this call is being executed.
1841 * @sc: The SCSI command to examine
1842 * @txopt: (out) BlockGuard operation for transmitted data
1843 * @rxopt: (out) BlockGuard operation for received data
1844 *
1845 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1846 *
1847 **/
1848static int
1849lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1850 uint8_t *txop, uint8_t *rxop)
1851{
1852 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
1853 uint8_t ret = 0;
1854
1855 if (guard_type == SHOST_DIX_GUARD_IP) {
1856 switch (scsi_get_prot_op(sc)) {
1857 case SCSI_PROT_READ_INSERT:
1858 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001859 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001860 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001861 break;
1862
1863 case SCSI_PROT_READ_STRIP:
1864 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001865 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001866 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001867 break;
1868
1869 case SCSI_PROT_READ_PASS:
1870 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001871 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001872 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001873 break;
1874
1875 case SCSI_PROT_NORMAL:
1876 default:
1877 break;
1878
1879 }
1880 } else {
1881 switch (scsi_get_prot_op(sc)) {
1882 case SCSI_PROT_READ_STRIP:
1883 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001884 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001885 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001886 break;
1887
1888 case SCSI_PROT_READ_PASS:
1889 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001890 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05001891 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001892 break;
1893
1894 case SCSI_PROT_READ_INSERT:
1895 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001896 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001897 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001898 break;
1899
1900 case SCSI_PROT_NORMAL:
1901 default:
1902 break;
1903 }
1904 }
1905
1906 return ret;
1907}
1908#endif
1909
1910/**
1911 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1912 * @phba: The Hba for which this call is being executed.
1913 * @sc: pointer to scsi command we're working on
1914 * @bpl: pointer to buffer list for protection groups
1915 * @datacnt: number of segments of data that have been dma mapped
1916 *
1917 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05001918 * type LPFC_PG_TYPE_NO_DIF
1919 *
1920 * This is usually used when the HBA is instructed to generate
1921 * DIFs and insert them into data stream (or strip DIF from
1922 * incoming data stream)
1923 *
1924 * The buffer list consists of just one protection group described
1925 * below:
1926 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001927 * start of prot group --> | PDE_5 |
1928 * +-------------------------+
1929 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001930 * +-------------------------+
1931 * | Data BDE |
1932 * +-------------------------+
1933 * |more Data BDE's ... (opt)|
1934 * +-------------------------+
1935 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001936 *
1937 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05001938 *
1939 * Returns the number of BDEs added to the BPL.
1940 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001941static int
1942lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1943 struct ulp_bde64 *bpl, int datasegcnt)
1944{
1945 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001946 struct lpfc_pde5 *pde5 = NULL;
1947 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001948 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001949 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001950 int datadir = sc->sc_data_direction;
James Smart0829a192012-05-09 21:18:12 -04001951#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001952 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001953#endif
James Smartacd68592012-01-18 16:25:09 -05001954 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001955 uint32_t reftag;
James Smart7c56b9f2011-07-22 18:36:25 -04001956 unsigned blksize;
James Smart6c8eea52010-04-06 14:49:53 -04001957 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001958
James Smart6c8eea52010-04-06 14:49:53 -04001959 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1960 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001961 goto out;
1962
James Smart6c8eea52010-04-06 14:49:53 -04001963 /* extract some info from the scsi command for pde*/
James Smarte2a0a9d2008-12-04 22:40:02 -05001964 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05001965 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05001966
James Smartf9bb2da2011-10-10 21:34:11 -04001967#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001968 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001969 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001970 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001971 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001972 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001973 checking = 0;
1974 }
James Smartf9bb2da2011-10-10 21:34:11 -04001975#endif
1976
James Smart6c8eea52010-04-06 14:49:53 -04001977 /* setup PDE5 with what we have */
1978 pde5 = (struct lpfc_pde5 *) bpl;
1979 memset(pde5, 0, sizeof(struct lpfc_pde5));
1980 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001981
James Smartbc739052010-08-04 16:11:18 -04001982 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001983 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001984 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001985
James Smart6c8eea52010-04-06 14:49:53 -04001986 /* advance bpl and increment bde count */
1987 num_bde++;
1988 bpl++;
1989 pde6 = (struct lpfc_pde6 *) bpl;
1990
1991 /* setup PDE6 with the rest of the info */
1992 memset(pde6, 0, sizeof(struct lpfc_pde6));
1993 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1994 bf_set(pde6_optx, pde6, txop);
1995 bf_set(pde6_oprx, pde6, rxop);
1996 if (datadir == DMA_FROM_DEVICE) {
James Smartacd68592012-01-18 16:25:09 -05001997 bf_set(pde6_ce, pde6, checking);
1998 bf_set(pde6_re, pde6, checking);
James Smart6c8eea52010-04-06 14:49:53 -04001999 }
2000 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002001 bf_set(pde6_ae, pde6, 0);
2002 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002003
James Smartbc739052010-08-04 16:11:18 -04002004 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002005 pde6->word0 = cpu_to_le32(pde6->word0);
2006 pde6->word1 = cpu_to_le32(pde6->word1);
2007 pde6->word2 = cpu_to_le32(pde6->word2);
2008
James Smart6c8eea52010-04-06 14:49:53 -04002009 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002010 num_bde++;
2011 bpl++;
2012
2013 /* assumption: caller has already run dma_map_sg on command data */
2014 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2015 physaddr = sg_dma_address(sgde);
2016 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
2017 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2018 bpl->tus.f.bdeSize = sg_dma_len(sgde);
2019 if (datadir == DMA_TO_DEVICE)
2020 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2021 else
2022 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2023 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2024 bpl++;
2025 num_bde++;
2026 }
2027
2028out:
2029 return num_bde;
2030}
2031
James Smartacd68592012-01-18 16:25:09 -05002032/**
2033 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
2034 * @phba: The Hba for which this call is being executed.
2035 * @sc: pointer to scsi command we're working on
2036 * @bpl: pointer to buffer list for protection groups
2037 * @datacnt: number of segments of data that have been dma mapped
2038 * @protcnt: number of segment of protection data that have been dma mapped
2039 *
2040 * This function sets up BPL buffer list for protection groups of
2041 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05002042 *
2043 * This is usually used when DIFs are in their own buffers,
2044 * separate from the data. The HBA can then by instructed
2045 * to place the DIFs in the outgoing stream. For read operations,
2046 * The HBA could extract the DIFs and place it in DIF buffers.
2047 *
2048 * The buffer list for this type consists of one or more of the
2049 * protection groups described below:
2050 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002051 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002052 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002053 * | PDE_6 |
2054 * +-------------------------+
2055 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05002056 * +-------------------------+
2057 * | Data BDE |
2058 * +-------------------------+
2059 * |more Data BDE's ... (opt)|
2060 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002061 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002062 * +-------------------------+
2063 * | ... |
2064 * +-------------------------+
2065 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002066 * Note: It is assumed that both data and protection s/g buffers have been
2067 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05002068 *
2069 * Returns the number of BDEs added to the BPL.
2070 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002071static int
2072lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2073 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2074{
2075 struct scatterlist *sgde = NULL; /* s/g data entry */
2076 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04002077 struct lpfc_pde5 *pde5 = NULL;
2078 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05002079 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002080 dma_addr_t dataphysaddr, protphysaddr;
2081 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05002082 unsigned int split_offset;
2083 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05002084 unsigned int protgrp_blks, protgrp_bytes;
2085 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04002086 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002087 int datadir = sc->sc_data_direction;
2088 unsigned char pgdone = 0, alldone = 0;
2089 unsigned blksize;
James Smart0829a192012-05-09 21:18:12 -04002090#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002091 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002092#endif
James Smartacd68592012-01-18 16:25:09 -05002093 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002094 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04002095 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002096 int num_bde = 0;
2097
2098 sgpe = scsi_prot_sglist(sc);
2099 sgde = scsi_sglist(sc);
2100
2101 if (!sgpe || !sgde) {
2102 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2103 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2104 sgpe, sgde);
2105 return 0;
2106 }
2107
James Smart6c8eea52010-04-06 14:49:53 -04002108 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2109 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002110 goto out;
2111
James Smart6c8eea52010-04-06 14:49:53 -04002112 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05002113 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002114 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002115
James Smartf9bb2da2011-10-10 21:34:11 -04002116#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002117 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002118 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002119 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002120 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002121 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002122 checking = 0;
2123 }
James Smartf9bb2da2011-10-10 21:34:11 -04002124#endif
2125
James Smarte2a0a9d2008-12-04 22:40:02 -05002126 split_offset = 0;
2127 do {
James Smart6c8eea52010-04-06 14:49:53 -04002128 /* setup PDE5 with what we have */
2129 pde5 = (struct lpfc_pde5 *) bpl;
2130 memset(pde5, 0, sizeof(struct lpfc_pde5));
2131 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002132
James Smartbc739052010-08-04 16:11:18 -04002133 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002134 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002135 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002136
James Smart6c8eea52010-04-06 14:49:53 -04002137 /* advance bpl and increment bde count */
2138 num_bde++;
2139 bpl++;
2140 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002141
James Smart6c8eea52010-04-06 14:49:53 -04002142 /* setup PDE6 with the rest of the info */
2143 memset(pde6, 0, sizeof(struct lpfc_pde6));
2144 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2145 bf_set(pde6_optx, pde6, txop);
2146 bf_set(pde6_oprx, pde6, rxop);
James Smartacd68592012-01-18 16:25:09 -05002147 bf_set(pde6_ce, pde6, checking);
2148 bf_set(pde6_re, pde6, checking);
James Smart6c8eea52010-04-06 14:49:53 -04002149 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002150 bf_set(pde6_ae, pde6, 0);
2151 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002152
James Smartbc739052010-08-04 16:11:18 -04002153 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002154 pde6->word0 = cpu_to_le32(pde6->word0);
2155 pde6->word1 = cpu_to_le32(pde6->word1);
2156 pde6->word2 = cpu_to_le32(pde6->word2);
2157
James Smart6c8eea52010-04-06 14:49:53 -04002158 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002159 num_bde++;
2160 bpl++;
2161
2162 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05002163 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2164 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05002165
James Smarte2a0a9d2008-12-04 22:40:02 -05002166 /* must be integer multiple of the DIF block length */
2167 BUG_ON(protgroup_len % 8);
2168
James Smart7f860592011-03-11 16:05:52 -05002169 pde7 = (struct lpfc_pde7 *) bpl;
2170 memset(pde7, 0, sizeof(struct lpfc_pde7));
2171 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2172
James Smart7c56b9f2011-07-22 18:36:25 -04002173 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2174 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05002175
James Smarte2a0a9d2008-12-04 22:40:02 -05002176 protgrp_blks = protgroup_len / 8;
2177 protgrp_bytes = protgrp_blks * blksize;
2178
James Smart7f860592011-03-11 16:05:52 -05002179 /* check if this pde is crossing the 4K boundary; if so split */
2180 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2181 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2182 protgroup_offset += protgroup_remainder;
2183 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04002184 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05002185 } else {
2186 protgroup_offset = 0;
2187 curr_prot++;
2188 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002189
James Smarte2a0a9d2008-12-04 22:40:02 -05002190 num_bde++;
2191
2192 /* setup BDE's for data blocks associated with DIF data */
2193 pgdone = 0;
2194 subtotal = 0; /* total bytes processed for current prot grp */
2195 while (!pgdone) {
2196 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04002197 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2198 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002199 __func__);
2200 return 0;
2201 }
2202 bpl++;
2203 dataphysaddr = sg_dma_address(sgde) + split_offset;
2204 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2205 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2206
2207 remainder = sg_dma_len(sgde) - split_offset;
2208
2209 if ((subtotal + remainder) <= protgrp_bytes) {
2210 /* we can use this whole buffer */
2211 bpl->tus.f.bdeSize = remainder;
2212 split_offset = 0;
2213
2214 if ((subtotal + remainder) == protgrp_bytes)
2215 pgdone = 1;
2216 } else {
2217 /* must split this buffer with next prot grp */
2218 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2219 split_offset += bpl->tus.f.bdeSize;
2220 }
2221
2222 subtotal += bpl->tus.f.bdeSize;
2223
2224 if (datadir == DMA_TO_DEVICE)
2225 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2226 else
2227 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2228 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2229
2230 num_bde++;
2231 curr_data++;
2232
2233 if (split_offset)
2234 break;
2235
2236 /* Move to the next s/g segment if possible */
2237 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04002238
James Smarte2a0a9d2008-12-04 22:40:02 -05002239 }
2240
James Smart7f860592011-03-11 16:05:52 -05002241 if (protgroup_offset) {
2242 /* update the reference tag */
2243 reftag += protgrp_blks;
2244 bpl++;
2245 continue;
2246 }
2247
James Smarte2a0a9d2008-12-04 22:40:02 -05002248 /* are we done ? */
2249 if (curr_prot == protcnt) {
2250 alldone = 1;
2251 } else if (curr_prot < protcnt) {
2252 /* advance to next prot buffer */
2253 sgpe = sg_next(sgpe);
2254 bpl++;
2255
2256 /* update the reference tag */
2257 reftag += protgrp_blks;
2258 } else {
2259 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04002260 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2261 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05002262 }
2263
2264 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05002265out:
2266
James Smarte2a0a9d2008-12-04 22:40:02 -05002267 return num_bde;
2268}
James Smart7f860592011-03-11 16:05:52 -05002269
James Smartacd68592012-01-18 16:25:09 -05002270/**
2271 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2272 * @phba: The Hba for which this call is being executed.
2273 * @sc: pointer to scsi command we're working on
2274 * @sgl: pointer to buffer list for protection groups
2275 * @datacnt: number of segments of data that have been dma mapped
2276 *
2277 * This function sets up SGL buffer list for protection groups of
2278 * type LPFC_PG_TYPE_NO_DIF
2279 *
2280 * This is usually used when the HBA is instructed to generate
2281 * DIFs and insert them into data stream (or strip DIF from
2282 * incoming data stream)
2283 *
2284 * The buffer list consists of just one protection group described
2285 * below:
2286 * +-------------------------+
2287 * start of prot group --> | DI_SEED |
2288 * +-------------------------+
2289 * | Data SGE |
2290 * +-------------------------+
2291 * |more Data SGE's ... (opt)|
2292 * +-------------------------+
2293 *
2294 *
2295 * Note: Data s/g buffers have been dma mapped
2296 *
2297 * Returns the number of SGEs added to the SGL.
2298 **/
2299static int
2300lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2301 struct sli4_sge *sgl, int datasegcnt)
2302{
2303 struct scatterlist *sgde = NULL; /* s/g data entry */
2304 struct sli4_sge_diseed *diseed = NULL;
2305 dma_addr_t physaddr;
2306 int i = 0, num_sge = 0, status;
2307 int datadir = sc->sc_data_direction;
2308 uint32_t reftag;
2309 unsigned blksize;
2310 uint8_t txop, rxop;
James Smart0829a192012-05-09 21:18:12 -04002311#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002312 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002313#endif
James Smartacd68592012-01-18 16:25:09 -05002314 uint32_t checking = 1;
2315 uint32_t dma_len;
2316 uint32_t dma_offset = 0;
2317
2318 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2319 if (status)
2320 goto out;
2321
2322 /* extract some info from the scsi command for pde*/
2323 blksize = lpfc_cmd_blksize(sc);
2324 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2325
2326#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002327 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002328 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002329 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002330 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002331 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002332 checking = 0;
2333 }
2334#endif
2335
2336 /* setup DISEED with what we have */
2337 diseed = (struct sli4_sge_diseed *) sgl;
2338 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2339 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2340
2341 /* Endianness conversion if necessary */
2342 diseed->ref_tag = cpu_to_le32(reftag);
2343 diseed->ref_tag_tran = diseed->ref_tag;
2344
2345 /* setup DISEED with the rest of the info */
2346 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2347 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2348 if (datadir == DMA_FROM_DEVICE) {
2349 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2350 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2351 }
2352 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2353 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2354
2355 /* Endianness conversion if necessary for DISEED */
2356 diseed->word2 = cpu_to_le32(diseed->word2);
2357 diseed->word3 = cpu_to_le32(diseed->word3);
2358
2359 /* advance bpl and increment sge count */
2360 num_sge++;
2361 sgl++;
2362
2363 /* assumption: caller has already run dma_map_sg on command data */
2364 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2365 physaddr = sg_dma_address(sgde);
2366 dma_len = sg_dma_len(sgde);
2367 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2368 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2369 if ((i + 1) == datasegcnt)
2370 bf_set(lpfc_sli4_sge_last, sgl, 1);
2371 else
2372 bf_set(lpfc_sli4_sge_last, sgl, 0);
2373 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2374 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2375
2376 sgl->sge_len = cpu_to_le32(dma_len);
2377 dma_offset += dma_len;
2378
2379 sgl++;
2380 num_sge++;
2381 }
2382
2383out:
2384 return num_sge;
2385}
2386
2387/**
2388 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2389 * @phba: The Hba for which this call is being executed.
2390 * @sc: pointer to scsi command we're working on
2391 * @sgl: pointer to buffer list for protection groups
2392 * @datacnt: number of segments of data that have been dma mapped
2393 * @protcnt: number of segment of protection data that have been dma mapped
2394 *
2395 * This function sets up SGL buffer list for protection groups of
2396 * type LPFC_PG_TYPE_DIF
2397 *
2398 * This is usually used when DIFs are in their own buffers,
2399 * separate from the data. The HBA can then by instructed
2400 * to place the DIFs in the outgoing stream. For read operations,
2401 * The HBA could extract the DIFs and place it in DIF buffers.
2402 *
2403 * The buffer list for this type consists of one or more of the
2404 * protection groups described below:
2405 * +-------------------------+
2406 * start of first prot group --> | DISEED |
2407 * +-------------------------+
2408 * | DIF (Prot SGE) |
2409 * +-------------------------+
2410 * | Data SGE |
2411 * +-------------------------+
2412 * |more Data SGE's ... (opt)|
2413 * +-------------------------+
2414 * start of new prot group --> | DISEED |
2415 * +-------------------------+
2416 * | ... |
2417 * +-------------------------+
2418 *
2419 * Note: It is assumed that both data and protection s/g buffers have been
2420 * mapped for DMA
2421 *
2422 * Returns the number of SGEs added to the SGL.
2423 **/
2424static int
2425lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2426 struct sli4_sge *sgl, int datacnt, int protcnt)
2427{
2428 struct scatterlist *sgde = NULL; /* s/g data entry */
2429 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2430 struct sli4_sge_diseed *diseed = NULL;
2431 dma_addr_t dataphysaddr, protphysaddr;
2432 unsigned short curr_data = 0, curr_prot = 0;
2433 unsigned int split_offset;
2434 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2435 unsigned int protgrp_blks, protgrp_bytes;
2436 unsigned int remainder, subtotal;
2437 int status;
2438 unsigned char pgdone = 0, alldone = 0;
2439 unsigned blksize;
2440 uint32_t reftag;
2441 uint8_t txop, rxop;
2442 uint32_t dma_len;
James Smart0829a192012-05-09 21:18:12 -04002443#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002444 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002445#endif
James Smartacd68592012-01-18 16:25:09 -05002446 uint32_t checking = 1;
2447 uint32_t dma_offset = 0;
2448 int num_sge = 0;
2449
2450 sgpe = scsi_prot_sglist(sc);
2451 sgde = scsi_sglist(sc);
2452
2453 if (!sgpe || !sgde) {
2454 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2455 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2456 sgpe, sgde);
2457 return 0;
2458 }
2459
2460 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2461 if (status)
2462 goto out;
2463
2464 /* extract some info from the scsi command */
2465 blksize = lpfc_cmd_blksize(sc);
2466 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2467
2468#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002469 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002470 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002471 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002472 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002473 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002474 checking = 0;
2475 }
2476#endif
2477
2478 split_offset = 0;
2479 do {
2480 /* setup DISEED with what we have */
2481 diseed = (struct sli4_sge_diseed *) sgl;
2482 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2483 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2484
2485 /* Endianness conversion if necessary */
2486 diseed->ref_tag = cpu_to_le32(reftag);
2487 diseed->ref_tag_tran = diseed->ref_tag;
2488
2489 /* setup DISEED with the rest of the info */
2490 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2491 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2492 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2493 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2494 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2495 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2496
2497 /* Endianness conversion if necessary for DISEED */
2498 diseed->word2 = cpu_to_le32(diseed->word2);
2499 diseed->word3 = cpu_to_le32(diseed->word3);
2500
2501 /* advance sgl and increment bde count */
2502 num_sge++;
2503 sgl++;
2504
2505 /* setup the first BDE that points to protection buffer */
2506 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2507 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2508
2509 /* must be integer multiple of the DIF block length */
2510 BUG_ON(protgroup_len % 8);
2511
2512 /* Now setup DIF SGE */
2513 sgl->word2 = 0;
2514 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2515 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2516 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2517 sgl->word2 = cpu_to_le32(sgl->word2);
2518
2519 protgrp_blks = protgroup_len / 8;
2520 protgrp_bytes = protgrp_blks * blksize;
2521
2522 /* check if DIF SGE is crossing the 4K boundary; if so split */
2523 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2524 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2525 protgroup_offset += protgroup_remainder;
2526 protgrp_blks = protgroup_remainder / 8;
2527 protgrp_bytes = protgrp_blks * blksize;
2528 } else {
2529 protgroup_offset = 0;
2530 curr_prot++;
2531 }
2532
2533 num_sge++;
2534
2535 /* setup SGE's for data blocks associated with DIF data */
2536 pgdone = 0;
2537 subtotal = 0; /* total bytes processed for current prot grp */
2538 while (!pgdone) {
2539 if (!sgde) {
2540 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2541 "9086 BLKGRD:%s Invalid data segment\n",
2542 __func__);
2543 return 0;
2544 }
2545 sgl++;
2546 dataphysaddr = sg_dma_address(sgde) + split_offset;
2547
2548 remainder = sg_dma_len(sgde) - split_offset;
2549
2550 if ((subtotal + remainder) <= protgrp_bytes) {
2551 /* we can use this whole buffer */
2552 dma_len = remainder;
2553 split_offset = 0;
2554
2555 if ((subtotal + remainder) == protgrp_bytes)
2556 pgdone = 1;
2557 } else {
2558 /* must split this buffer with next prot grp */
2559 dma_len = protgrp_bytes - subtotal;
2560 split_offset += dma_len;
2561 }
2562
2563 subtotal += dma_len;
2564
2565 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2566 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2567 bf_set(lpfc_sli4_sge_last, sgl, 0);
2568 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2569 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2570
2571 sgl->sge_len = cpu_to_le32(dma_len);
2572 dma_offset += dma_len;
2573
2574 num_sge++;
2575 curr_data++;
2576
2577 if (split_offset)
2578 break;
2579
2580 /* Move to the next s/g segment if possible */
2581 sgde = sg_next(sgde);
2582 }
2583
2584 if (protgroup_offset) {
2585 /* update the reference tag */
2586 reftag += protgrp_blks;
2587 sgl++;
2588 continue;
2589 }
2590
2591 /* are we done ? */
2592 if (curr_prot == protcnt) {
2593 bf_set(lpfc_sli4_sge_last, sgl, 1);
2594 alldone = 1;
2595 } else if (curr_prot < protcnt) {
2596 /* advance to next prot buffer */
2597 sgpe = sg_next(sgpe);
2598 sgl++;
2599
2600 /* update the reference tag */
2601 reftag += protgrp_blks;
2602 } else {
2603 /* if we're here, we have a bug */
2604 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2605 "9085 BLKGRD: bug in %s\n", __func__);
2606 }
2607
2608 } while (!alldone);
2609
2610out:
2611
2612 return num_sge;
2613}
2614
2615/**
2616 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2617 * @phba: The Hba for which this call is being executed.
2618 * @sc: pointer to scsi command we're working on
2619 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002620 * Given a SCSI command that supports DIF, determine composition of protection
2621 * groups involved in setting up buffer lists
2622 *
James Smartacd68592012-01-18 16:25:09 -05002623 * Returns: Protection group type (with or without DIF)
2624 *
2625 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002626static int
2627lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2628{
2629 int ret = LPFC_PG_TYPE_INVALID;
2630 unsigned char op = scsi_get_prot_op(sc);
2631
2632 switch (op) {
2633 case SCSI_PROT_READ_STRIP:
2634 case SCSI_PROT_WRITE_INSERT:
2635 ret = LPFC_PG_TYPE_NO_DIF;
2636 break;
2637 case SCSI_PROT_READ_INSERT:
2638 case SCSI_PROT_WRITE_STRIP:
2639 case SCSI_PROT_READ_PASS:
2640 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002641 ret = LPFC_PG_TYPE_DIF_BUF;
2642 break;
2643 default:
2644 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2645 "9021 Unsupported protection op:%d\n", op);
2646 break;
2647 }
2648
2649 return ret;
2650}
2651
James Smartacd68592012-01-18 16:25:09 -05002652/**
2653 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2654 * @phba: The Hba for which this call is being executed.
2655 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2656 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002657 * This is the protection/DIF aware version of
2658 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2659 * two functions eventually, but for now, it's here
James Smartacd68592012-01-18 16:25:09 -05002660 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002661static int
James Smartacd68592012-01-18 16:25:09 -05002662lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smarte2a0a9d2008-12-04 22:40:02 -05002663 struct lpfc_scsi_buf *lpfc_cmd)
2664{
2665 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2666 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2667 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2668 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2669 uint32_t num_bde = 0;
2670 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2671 int prot_group_type = 0;
2672 int diflen, fcpdl;
2673 unsigned blksize;
2674
2675 /*
2676 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2677 * fcp_rsp regions to the first data bde entry
2678 */
2679 bpl += 2;
2680 if (scsi_sg_count(scsi_cmnd)) {
2681 /*
2682 * The driver stores the segment count returned from pci_map_sg
2683 * because this a count of dma-mappings used to map the use_sg
2684 * pages. They are not guaranteed to be the same for those
2685 * architectures that implement an IOMMU.
2686 */
2687 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2688 scsi_sglist(scsi_cmnd),
2689 scsi_sg_count(scsi_cmnd), datadir);
2690 if (unlikely(!datasegcnt))
2691 return 1;
2692
2693 lpfc_cmd->seg_cnt = datasegcnt;
2694 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04002695 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2696 "9067 BLKGRD: %s: Too many sg segments"
2697 " from dma_map_sg. Config %d, seg_cnt"
2698 " %d\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002699 __func__, phba->cfg_sg_seg_cnt,
2700 lpfc_cmd->seg_cnt);
2701 scsi_dma_unmap(scsi_cmnd);
2702 return 1;
2703 }
2704
2705 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2706
2707 switch (prot_group_type) {
2708 case LPFC_PG_TYPE_NO_DIF:
2709 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2710 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002711 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002712 if (num_bde < 2)
2713 goto err;
2714 break;
2715 case LPFC_PG_TYPE_DIF_BUF:{
2716 /*
2717 * This type indicates that protection buffers are
2718 * passed to the driver, so that needs to be prepared
2719 * for DMA
2720 */
2721 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2722 scsi_prot_sglist(scsi_cmnd),
2723 scsi_prot_sg_count(scsi_cmnd), datadir);
2724 if (unlikely(!protsegcnt)) {
2725 scsi_dma_unmap(scsi_cmnd);
2726 return 1;
2727 }
2728
2729 lpfc_cmd->prot_seg_cnt = protsegcnt;
2730 if (lpfc_cmd->prot_seg_cnt
2731 > phba->cfg_prot_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04002732 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2733 "9068 BLKGRD: %s: Too many prot sg "
2734 "segments from dma_map_sg. Config %d,"
James Smarte2a0a9d2008-12-04 22:40:02 -05002735 "prot_seg_cnt %d\n", __func__,
2736 phba->cfg_prot_sg_seg_cnt,
2737 lpfc_cmd->prot_seg_cnt);
2738 dma_unmap_sg(&phba->pcidev->dev,
2739 scsi_prot_sglist(scsi_cmnd),
2740 scsi_prot_sg_count(scsi_cmnd),
2741 datadir);
2742 scsi_dma_unmap(scsi_cmnd);
2743 return 1;
2744 }
2745
2746 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2747 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002748 /* we should have 3 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002749 if (num_bde < 3)
2750 goto err;
2751 break;
2752 }
2753 case LPFC_PG_TYPE_INVALID:
2754 default:
2755 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2756 "9022 Unexpected protection group %i\n",
2757 prot_group_type);
2758 return 1;
2759 }
2760 }
2761
2762 /*
2763 * Finish initializing those IOCB fields that are dependent on the
2764 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2765 * reinitialized since all iocb memory resources are used many times
2766 * for transmit, receive, and continuation bpl's.
2767 */
2768 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2769 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2770 iocb_cmd->ulpBdeCount = 1;
2771 iocb_cmd->ulpLe = 1;
2772
2773 fcpdl = scsi_bufflen(scsi_cmnd);
2774
2775 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
2776 /*
2777 * We are in DIF Type 1 mode
2778 * Every data block has a 8 byte DIF (trailer)
2779 * attached to it. Must ajust FCP data length
2780 */
2781 blksize = lpfc_cmd_blksize(scsi_cmnd);
2782 diflen = (fcpdl / blksize) * 8;
2783 fcpdl += diflen;
2784 }
2785 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2786
2787 /*
2788 * Due to difference in data length between DIF/non-DIF paths,
2789 * we need to set word 4 of IOCB here
2790 */
2791 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2792
2793 return 0;
2794err:
2795 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2796 "9023 Could not setup all needed BDE's"
2797 "prot_group_type=%d, num_bde=%d\n",
2798 prot_group_type, num_bde);
2799 return 1;
2800}
2801
2802/*
2803 * This function checks for BlockGuard errors detected by
2804 * the HBA. In case of errors, the ASC/ASCQ fields in the
2805 * sense buffer will be set accordingly, paired with
2806 * ILLEGAL_REQUEST to signal to the kernel that the HBA
2807 * detected corruption.
2808 *
2809 * Returns:
2810 * 0 - No error found
2811 * 1 - BlockGuard error found
2812 * -1 - Internal error (bad profile, ...etc)
2813 */
2814static int
2815lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
2816 struct lpfc_iocbq *pIocbOut)
2817{
2818 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2819 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
2820 int ret = 0;
2821 uint32_t bghm = bgf->bghm;
2822 uint32_t bgstat = bgf->bgstat;
2823 uint64_t failing_sector = 0;
2824
James Smart6a9c52c2009-10-02 15:16:51 -04002825 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
2826 " 0x%x lba 0x%llx blk cnt 0x%x "
James Smarte2a0a9d2008-12-04 22:40:02 -05002827 "bgstat=0x%x bghm=0x%x\n",
James Smart87b5c322008-12-16 10:34:09 -05002828 cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
Tejun Heo83096eb2009-05-07 22:24:39 +09002829 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002830
2831 spin_lock(&_dump_buf_lock);
2832 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04002833 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
2834 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002835 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04002836 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002837
2838 /* If we have a prot sgl, save the DIF buffer */
2839 if (lpfc_prot_group_type(phba, cmd) ==
2840 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04002841 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
2842 "Saving DIF for %u blocks to debugfs\n",
2843 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
2844 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002845 }
2846
2847 _dump_buf_done = 1;
2848 }
2849 spin_unlock(&_dump_buf_lock);
2850
2851 if (lpfc_bgs_get_invalid_prof(bgstat)) {
2852 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002853 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
2854 " BlockGuard profile. bgstat:0x%x\n",
2855 bgstat);
James Smarte2a0a9d2008-12-04 22:40:02 -05002856 ret = (-1);
2857 goto out;
2858 }
2859
2860 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
2861 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002862 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
2863 "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002864 bgstat);
2865 ret = (-1);
2866 goto out;
2867 }
2868
2869 if (lpfc_bgs_get_guard_err(bgstat)) {
2870 ret = 1;
2871
2872 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2873 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002874 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002875 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2876 phba->bg_guard_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002877 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2878 "9055 BLKGRD: guard_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002879 }
2880
2881 if (lpfc_bgs_get_reftag_err(bgstat)) {
2882 ret = 1;
2883
2884 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2885 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002886 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002887 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2888
2889 phba->bg_reftag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002890 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2891 "9056 BLKGRD: ref_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002892 }
2893
2894 if (lpfc_bgs_get_apptag_err(bgstat)) {
2895 ret = 1;
2896
2897 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2898 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002899 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002900 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2901
2902 phba->bg_apptag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002903 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2904 "9061 BLKGRD: app_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002905 }
2906
2907 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
2908 /*
2909 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04002910 * field, and put the failing LBA in it.
2911 * This code assumes there was also a guard/app/ref tag error
2912 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05002913 */
James Smart7c56b9f2011-07-22 18:36:25 -04002914 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
2915 cmd->sense_buffer[8] = 0; /* Information descriptor type */
2916 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
2917 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05002918
2919 /* bghm is a "on the wire" FC frame based count */
2920 switch (scsi_get_prot_op(cmd)) {
2921 case SCSI_PROT_READ_INSERT:
2922 case SCSI_PROT_WRITE_STRIP:
2923 bghm /= cmd->device->sector_size;
2924 break;
2925 case SCSI_PROT_READ_STRIP:
2926 case SCSI_PROT_WRITE_INSERT:
2927 case SCSI_PROT_READ_PASS:
2928 case SCSI_PROT_WRITE_PASS:
2929 bghm /= (cmd->device->sector_size +
2930 sizeof(struct scsi_dif_tuple));
2931 break;
2932 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002933
2934 failing_sector = scsi_get_lba(cmd);
2935 failing_sector += bghm;
2936
James Smart7c56b9f2011-07-22 18:36:25 -04002937 /* Descriptor Information */
2938 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05002939 }
2940
2941 if (!ret) {
2942 /* No error was reported - problem in FW? */
2943 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002944 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart4ac9b222012-03-01 22:38:29 -05002945 "9057 BLKGRD: Unknown error reported!\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002946 }
2947
2948out:
2949 return ret;
2950}
2951
James Smartea2151b2008-09-07 11:52:10 -04002952/**
James Smartda0436e2009-05-22 14:51:39 -04002953 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
2954 * @phba: The Hba for which this call is being executed.
2955 * @lpfc_cmd: The scsi buffer which is going to be mapped.
2956 *
2957 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
2958 * field of @lpfc_cmd for device with SLI-4 interface spec.
2959 *
2960 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04002961 * 1 - Error
2962 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04002963 **/
2964static int
2965lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2966{
2967 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2968 struct scatterlist *sgel = NULL;
2969 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2970 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
James Smartfedd3b72011-02-16 12:39:24 -05002971 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04002972 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2973 dma_addr_t physaddr;
2974 uint32_t num_bde = 0;
2975 uint32_t dma_len;
2976 uint32_t dma_offset = 0;
2977 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05002978 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04002979
2980 /*
2981 * There are three possibilities here - use scatter-gather segment, use
2982 * the single mapping, or neither. Start the lpfc command prep by
2983 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2984 * data bde entry.
2985 */
2986 if (scsi_sg_count(scsi_cmnd)) {
2987 /*
2988 * The driver stores the segment count returned from pci_map_sg
2989 * because this a count of dma-mappings used to map the use_sg
2990 * pages. They are not guaranteed to be the same for those
2991 * architectures that implement an IOMMU.
2992 */
2993
2994 nseg = scsi_dma_map(scsi_cmnd);
2995 if (unlikely(!nseg))
2996 return 1;
2997 sgl += 1;
2998 /* clear the last flag in the fcp_rsp map entry */
2999 sgl->word2 = le32_to_cpu(sgl->word2);
3000 bf_set(lpfc_sli4_sge_last, sgl, 0);
3001 sgl->word2 = cpu_to_le32(sgl->word2);
3002 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05003003 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04003004 lpfc_cmd->seg_cnt = nseg;
3005 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04003006 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3007 " %s: Too many sg segments from "
3008 "dma_map_sg. Config %d, seg_cnt %d\n",
3009 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04003010 lpfc_cmd->seg_cnt);
3011 scsi_dma_unmap(scsi_cmnd);
3012 return 1;
3013 }
3014
3015 /*
3016 * The driver established a maximum scatter-gather segment count
3017 * during probe that limits the number of sg elements in any
3018 * single scsi command. Just run through the seg_cnt and format
3019 * the sge's.
3020 * When using SLI-3 the driver will try to fit all the BDEs into
3021 * the IOCB. If it can't then the BDEs get added to a BPL as it
3022 * does for SLI-2 mode.
3023 */
3024 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3025 physaddr = sg_dma_address(sgel);
3026 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04003027 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3028 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04003029 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04003030 if ((num_bde + 1) == nseg)
3031 bf_set(lpfc_sli4_sge_last, sgl, 1);
3032 else
3033 bf_set(lpfc_sli4_sge_last, sgl, 0);
3034 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04003035 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04003036 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05003037 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04003038 dma_offset += dma_len;
3039 sgl++;
3040 }
James Smartfedd3b72011-02-16 12:39:24 -05003041 /* setup the performance hint (first data BDE) if enabled */
3042 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
3043 bde = (struct ulp_bde64 *)
3044 &(iocb_cmd->unsli3.sli3Words[5]);
3045 bde->addrLow = first_data_sgl->addr_lo;
3046 bde->addrHigh = first_data_sgl->addr_hi;
3047 bde->tus.f.bdeSize =
3048 le32_to_cpu(first_data_sgl->sge_len);
3049 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3050 bde->tus.w = cpu_to_le32(bde->tus.w);
3051 }
James Smartda0436e2009-05-22 14:51:39 -04003052 } else {
3053 sgl += 1;
3054 /* clear the last flag in the fcp_rsp map entry */
3055 sgl->word2 = le32_to_cpu(sgl->word2);
3056 bf_set(lpfc_sli4_sge_last, sgl, 1);
3057 sgl->word2 = cpu_to_le32(sgl->word2);
3058 }
3059
3060 /*
3061 * Finish initializing those IOCB fields that are dependent on the
3062 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3063 * explicitly reinitialized.
3064 * all iocb memory resources are reused.
3065 */
3066 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3067
3068 /*
3069 * Due to difference in data length between DIF/non-DIF paths,
3070 * we need to set word 4 of IOCB here
3071 */
3072 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
3073 return 0;
3074}
3075
3076/**
James Smartacd68592012-01-18 16:25:09 -05003077 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
3078 * @phba: The Hba for which this call is being executed.
3079 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
3080 *
3081 * Adjust the data length to account for how much data
3082 * is actually on the wire.
3083 *
3084 * returns the adjusted data length
3085 **/
3086static int
3087lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
3088 struct lpfc_scsi_buf *lpfc_cmd)
3089{
3090 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
3091 int diflen, fcpdl;
3092 unsigned blksize;
3093
3094 fcpdl = scsi_bufflen(sc);
3095
3096 /* Check if there is protection data on the wire */
3097 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
3098 /* Read */
3099 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
3100 return fcpdl;
3101
3102 } else {
3103 /* Write */
3104 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
3105 return fcpdl;
3106 }
3107
3108 /* If protection data on the wire, adjust the count accordingly */
3109 blksize = lpfc_cmd_blksize(sc);
3110 diflen = (fcpdl / blksize) * 8;
3111 fcpdl += diflen;
3112 return fcpdl;
3113}
3114
3115/**
3116 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3117 * @phba: The Hba for which this call is being executed.
3118 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3119 *
3120 * This is the protection/DIF aware version of
3121 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3122 * two functions eventually, but for now, it's here
3123 **/
3124static int
3125lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3126 struct lpfc_scsi_buf *lpfc_cmd)
3127{
3128 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3129 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3130 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3131 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3132 uint32_t num_bde = 0;
3133 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3134 int prot_group_type = 0;
3135 int fcpdl;
3136
3137 /*
3138 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
3139 * fcp_rsp regions to the first data bde entry
3140 */
3141 if (scsi_sg_count(scsi_cmnd)) {
3142 /*
3143 * The driver stores the segment count returned from pci_map_sg
3144 * because this a count of dma-mappings used to map the use_sg
3145 * pages. They are not guaranteed to be the same for those
3146 * architectures that implement an IOMMU.
3147 */
3148 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3149 scsi_sglist(scsi_cmnd),
3150 scsi_sg_count(scsi_cmnd), datadir);
3151 if (unlikely(!datasegcnt))
3152 return 1;
3153
3154 sgl += 1;
3155 /* clear the last flag in the fcp_rsp map entry */
3156 sgl->word2 = le32_to_cpu(sgl->word2);
3157 bf_set(lpfc_sli4_sge_last, sgl, 0);
3158 sgl->word2 = cpu_to_le32(sgl->word2);
3159
3160 sgl += 1;
3161 lpfc_cmd->seg_cnt = datasegcnt;
3162 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
3163 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3164 "9087 BLKGRD: %s: Too many sg segments"
3165 " from dma_map_sg. Config %d, seg_cnt"
3166 " %d\n",
3167 __func__, phba->cfg_sg_seg_cnt,
3168 lpfc_cmd->seg_cnt);
3169 scsi_dma_unmap(scsi_cmnd);
3170 return 1;
3171 }
3172
3173 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3174
3175 switch (prot_group_type) {
3176 case LPFC_PG_TYPE_NO_DIF:
3177 num_bde = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
3178 datasegcnt);
3179 /* we should have 2 or more entries in buffer list */
3180 if (num_bde < 2)
3181 goto err;
3182 break;
3183 case LPFC_PG_TYPE_DIF_BUF:{
3184 /*
3185 * This type indicates that protection buffers are
3186 * passed to the driver, so that needs to be prepared
3187 * for DMA
3188 */
3189 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3190 scsi_prot_sglist(scsi_cmnd),
3191 scsi_prot_sg_count(scsi_cmnd), datadir);
3192 if (unlikely(!protsegcnt)) {
3193 scsi_dma_unmap(scsi_cmnd);
3194 return 1;
3195 }
3196
3197 lpfc_cmd->prot_seg_cnt = protsegcnt;
3198 if (lpfc_cmd->prot_seg_cnt
3199 > phba->cfg_prot_sg_seg_cnt) {
3200 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3201 "9088 BLKGRD: %s: Too many prot sg "
3202 "segments from dma_map_sg. Config %d,"
3203 "prot_seg_cnt %d\n", __func__,
3204 phba->cfg_prot_sg_seg_cnt,
3205 lpfc_cmd->prot_seg_cnt);
3206 dma_unmap_sg(&phba->pcidev->dev,
3207 scsi_prot_sglist(scsi_cmnd),
3208 scsi_prot_sg_count(scsi_cmnd),
3209 datadir);
3210 scsi_dma_unmap(scsi_cmnd);
3211 return 1;
3212 }
3213
3214 num_bde = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
3215 datasegcnt, protsegcnt);
3216 /* we should have 3 or more entries in buffer list */
3217 if (num_bde < 3)
3218 goto err;
3219 break;
3220 }
3221 case LPFC_PG_TYPE_INVALID:
3222 default:
3223 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3224 "9083 Unexpected protection group %i\n",
3225 prot_group_type);
3226 return 1;
3227 }
3228 }
3229
3230 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
3231
3232 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3233
3234 /*
3235 * Due to difference in data length between DIF/non-DIF paths,
3236 * we need to set word 4 of IOCB here
3237 */
3238 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
3239 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF;
3240
3241 return 0;
3242err:
3243 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3244 "9084 Could not setup all needed BDE's"
3245 "prot_group_type=%d, num_bde=%d\n",
3246 prot_group_type, num_bde);
3247 return 1;
3248}
3249
3250/**
James Smart3772a992009-05-22 14:50:54 -04003251 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3252 * @phba: The Hba for which this call is being executed.
3253 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3254 *
3255 * This routine wraps the actual DMA mapping function pointer from the
3256 * lpfc_hba struct.
3257 *
3258 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003259 * 1 - Error
3260 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003261 **/
3262static inline int
3263lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3264{
3265 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3266}
3267
3268/**
James Smartacd68592012-01-18 16:25:09 -05003269 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3270 * using BlockGuard.
3271 * @phba: The Hba for which this call is being executed.
3272 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3273 *
3274 * This routine wraps the actual DMA mapping function pointer from the
3275 * lpfc_hba struct.
3276 *
3277 * Return codes:
3278 * 1 - Error
3279 * 0 - Success
3280 **/
3281static inline int
3282lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3283{
3284 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3285}
3286
3287/**
James Smart3621a712009-04-06 18:47:14 -04003288 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003289 * @phba: Pointer to hba context object.
3290 * @vport: Pointer to vport object.
3291 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3292 * @rsp_iocb: Pointer to response iocb object which reported error.
3293 *
3294 * This function posts an event when there is a SCSI command reporting
3295 * error from the scsi device.
3296 **/
3297static void
3298lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3299 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3300 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3301 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3302 uint32_t resp_info = fcprsp->rspStatus2;
3303 uint32_t scsi_status = fcprsp->rspStatus3;
3304 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3305 struct lpfc_fast_path_event *fast_path_evt = NULL;
3306 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3307 unsigned long flags;
3308
James Smart5989b8d2010-10-22 11:06:56 -04003309 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3310 return;
3311
James Smartea2151b2008-09-07 11:52:10 -04003312 /* If there is queuefull or busy condition send a scsi event */
3313 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3314 (cmnd->result == SAM_STAT_BUSY)) {
3315 fast_path_evt = lpfc_alloc_fast_evt(phba);
3316 if (!fast_path_evt)
3317 return;
3318 fast_path_evt->un.scsi_evt.event_type =
3319 FC_REG_SCSI_EVENT;
3320 fast_path_evt->un.scsi_evt.subcategory =
3321 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3322 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3323 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3324 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3325 &pnode->nlp_portname, sizeof(struct lpfc_name));
3326 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3327 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3328 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3329 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3330 fast_path_evt = lpfc_alloc_fast_evt(phba);
3331 if (!fast_path_evt)
3332 return;
3333 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3334 FC_REG_SCSI_EVENT;
3335 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3336 LPFC_EVENT_CHECK_COND;
3337 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3338 cmnd->device->lun;
3339 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3340 &pnode->nlp_portname, sizeof(struct lpfc_name));
3341 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3342 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3343 fast_path_evt->un.check_cond_evt.sense_key =
3344 cmnd->sense_buffer[2] & 0xf;
3345 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3346 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3347 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3348 fcpi_parm &&
3349 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3350 ((scsi_status == SAM_STAT_GOOD) &&
3351 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3352 /*
3353 * If status is good or resid does not match with fcp_param and
3354 * there is valid fcpi_parm, then there is a read_check error
3355 */
3356 fast_path_evt = lpfc_alloc_fast_evt(phba);
3357 if (!fast_path_evt)
3358 return;
3359 fast_path_evt->un.read_check_error.header.event_type =
3360 FC_REG_FABRIC_EVENT;
3361 fast_path_evt->un.read_check_error.header.subcategory =
3362 LPFC_EVENT_FCPRDCHKERR;
3363 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3364 &pnode->nlp_portname, sizeof(struct lpfc_name));
3365 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3366 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3367 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3368 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3369 fast_path_evt->un.read_check_error.fcpiparam =
3370 fcpi_parm;
3371 } else
3372 return;
3373
3374 fast_path_evt->vport = vport;
3375 spin_lock_irqsave(&phba->hbalock, flags);
3376 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3377 spin_unlock_irqrestore(&phba->hbalock, flags);
3378 lpfc_worker_wake_up(phba);
3379 return;
3380}
James Smart9bad7672008-12-04 22:39:02 -05003381
3382/**
James Smartf1126682009-06-10 17:22:44 -04003383 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003384 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003385 * @psb: The scsi buffer which is going to be un-mapped.
3386 *
3387 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003388 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003389 **/
dea31012005-04-17 16:05:31 -05003390static void
James Smartf1126682009-06-10 17:22:44 -04003391lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003392{
3393 /*
3394 * There are only two special cases to consider. (1) the scsi command
3395 * requested scatter-gather usage or (2) the scsi command allocated
3396 * a request buffer, but did not request use_sg. There is a third
3397 * case, but it does not require resource deallocation.
3398 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003399 if (psb->seg_cnt > 0)
3400 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003401 if (psb->prot_seg_cnt > 0)
3402 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3403 scsi_prot_sg_count(psb->pCmd),
3404 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003405}
3406
James Smart9bad7672008-12-04 22:39:02 -05003407/**
James Smart3621a712009-04-06 18:47:14 -04003408 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003409 * @vport: The virtual port for which this call is being executed.
3410 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3411 * @rsp_iocb: The response IOCB which contains FCP error.
3412 *
3413 * This routine is called to process response IOCB with status field
3414 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3415 * based upon SCSI and FCP error.
3416 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003417static void
James Smart2e0fef82007-06-17 19:56:36 -05003418lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3419 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003420{
3421 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3422 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3423 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003424 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003425 uint32_t resp_info = fcprsp->rspStatus2;
3426 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003427 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003428 uint32_t host_status = DID_OK;
3429 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05003430 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003431
James Smartea2151b2008-09-07 11:52:10 -04003432
dea31012005-04-17 16:05:31 -05003433 /*
3434 * If this is a task management command, there is no
3435 * scsi packet associated with this lpfc_cmd. The driver
3436 * consumes it.
3437 */
3438 if (fcpcmd->fcpCntl2) {
3439 scsi_status = 0;
3440 goto out;
3441 }
3442
James Smart6a9c52c2009-10-02 15:16:51 -04003443 if (resp_info & RSP_LEN_VALID) {
3444 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003445 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04003446 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3447 "2719 Invalid response length: "
3448 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
3449 cmnd->device->id,
3450 cmnd->device->lun, cmnd->cmnd[0],
3451 rsplen);
3452 host_status = DID_ERROR;
3453 goto out;
3454 }
James Smarte40a02c2010-02-26 14:13:54 -05003455 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3456 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3457 "2757 Protocol failure detected during "
3458 "processing of FCP I/O op: "
3459 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
3460 cmnd->device->id,
3461 cmnd->device->lun, cmnd->cmnd[0],
3462 fcprsp->rspInfo3);
3463 host_status = DID_ERROR;
3464 goto out;
3465 }
James Smart6a9c52c2009-10-02 15:16:51 -04003466 }
3467
James Smartc7743952006-12-02 13:34:42 -05003468 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3469 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3470 if (snslen > SCSI_SENSE_BUFFERSIZE)
3471 snslen = SCSI_SENSE_BUFFERSIZE;
3472
3473 if (resp_info & RSP_LEN_VALID)
3474 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3475 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3476 }
3477 lp = (uint32_t *)cmnd->sense_buffer;
3478
James Smartaa1c7ee2012-08-14 14:26:06 -04003479 /* special handling for under run conditions */
3480 if (!scsi_status && (resp_info & RESID_UNDER)) {
3481 /* don't log under runs if fcp set... */
3482 if (vport->cfg_log_verbose & LOG_FCP)
3483 logit = LOG_FCP_ERROR;
3484 /* unless operator says so */
3485 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3486 logit = LOG_FCP_UNDER;
3487 }
James Smartc7743952006-12-02 13:34:42 -05003488
James Smarte8b62012007-08-02 11:10:09 -04003489 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003490 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003491 "Data: x%x x%x x%x x%x x%x\n",
3492 cmnd->cmnd[0], scsi_status,
3493 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3494 be32_to_cpu(fcprsp->rspResId),
3495 be32_to_cpu(fcprsp->rspSnsLen),
3496 be32_to_cpu(fcprsp->rspRspLen),
3497 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003498
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003499 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05003500 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003501 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003502
James Smart73d91e52011-10-10 21:32:10 -04003503 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smarte2a0a9d2008-12-04 22:40:02 -05003504 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003505 "residual %d Data: x%x x%x x%x\n",
3506 be32_to_cpu(fcpcmd->fcpDl),
3507 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3508 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003509
3510 /*
James Smart7054a602007-04-25 09:52:34 -04003511 * If there is an under run check if under run reported by
3512 * storage array is same as the under run reported by HBA.
3513 * If this is not same, there is a dropped frame.
3514 */
3515 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3516 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003517 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003518 lpfc_printf_vlog(vport, KERN_WARNING,
3519 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003520 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003521 "and Underrun Data: x%x x%x x%x x%x\n",
3522 be32_to_cpu(fcpcmd->fcpDl),
3523 scsi_get_resid(cmnd), fcpi_parm,
3524 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003525 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003526 host_status = DID_ERROR;
3527 }
3528 /*
dea31012005-04-17 16:05:31 -05003529 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003530 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003531 * is not present, make sure the actual amount transferred is at
3532 * least the underflow value or fail.
3533 */
3534 if (!(resp_info & SNS_LEN_VALID) &&
3535 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003536 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3537 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003538 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003539 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003540 "underrun converted to error "
3541 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003542 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003543 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003544 host_status = DID_ERROR;
3545 }
3546 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003547 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003548 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003549 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003550 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003551 host_status = DID_ERROR;
3552
3553 /*
3554 * Check SLI validation that all the transfer was actually done
James Smart582dd792012-08-03 12:34:44 -04003555 * (fcpi_parm should be zero).
dea31012005-04-17 16:05:31 -05003556 */
James Smart582dd792012-08-03 12:34:44 -04003557 } else if (fcpi_parm) {
James Smarte8b62012007-08-02 11:10:09 -04003558 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart582dd792012-08-03 12:34:44 -04003559 "9029 FCP Data Transfer Check Error: "
James Smarteee88772010-09-29 11:19:08 -04003560 "x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003561 be32_to_cpu(fcpcmd->fcpDl),
3562 be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04003563 fcpi_parm, cmnd->cmnd[0], scsi_status);
3564 switch (scsi_status) {
3565 case SAM_STAT_GOOD:
3566 case SAM_STAT_CHECK_CONDITION:
3567 /* Fabric dropped a data frame. Fail any successful
3568 * command in which we detected dropped frames.
3569 * A status of good or some check conditions could
3570 * be considered a successful command.
3571 */
3572 host_status = DID_ERROR;
3573 break;
3574 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003575 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05003576 }
3577
3578 out:
3579 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04003580 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05003581}
3582
James Smart9bad7672008-12-04 22:39:02 -05003583/**
James Smart3621a712009-04-06 18:47:14 -04003584 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05003585 * @phba: The Hba for which this call is being executed.
3586 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04003587 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05003588 *
3589 * This routine assigns scsi command result by looking into response IOCB
3590 * status field appropriately. This routine handles QUEUE FULL condition as
3591 * well by ramping down device queue depth.
3592 **/
dea31012005-04-17 16:05:31 -05003593static void
3594lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3595 struct lpfc_iocbq *pIocbOut)
3596{
3597 struct lpfc_scsi_buf *lpfc_cmd =
3598 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003599 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05003600 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3601 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04003602 struct scsi_cmnd *cmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003603 int result;
James Smarta257bf92009-04-06 18:48:10 -04003604 struct scsi_device *tmp_sdev;
James Smart5ffc2662009-11-18 15:39:44 -05003605 int depth;
James Smartfa61a542008-01-11 01:52:42 -05003606 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04003607 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04003608 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04003609 uint32_t queue_depth, scsi_id;
James Smart73d91e52011-10-10 21:32:10 -04003610 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05003611
James Smart75baf692010-06-08 18:31:21 -04003612 /* Sanity check on return of outstanding command */
3613 if (!(lpfc_cmd->pCmd))
3614 return;
3615 cmd = lpfc_cmd->pCmd;
3616 shost = cmd->device->host;
3617
James Smarte3d2b802012-08-14 14:25:43 -04003618 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
dea31012005-04-17 16:05:31 -05003619 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05003620 /* pick up SLI4 exhange busy status from HBA */
3621 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
3622
James Smart9a6b09c2012-03-01 22:37:42 -05003623#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3624 if (lpfc_cmd->prot_data_type) {
3625 struct scsi_dif_tuple *src = NULL;
3626
3627 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
3628 /*
3629 * Used to restore any changes to protection
3630 * data for error injection.
3631 */
3632 switch (lpfc_cmd->prot_data_type) {
3633 case LPFC_INJERR_REFTAG:
3634 src->ref_tag =
3635 lpfc_cmd->prot_data;
3636 break;
3637 case LPFC_INJERR_APPTAG:
3638 src->app_tag =
3639 (uint16_t)lpfc_cmd->prot_data;
3640 break;
3641 case LPFC_INJERR_GUARD:
3642 src->guard_tag =
3643 (uint16_t)lpfc_cmd->prot_data;
3644 break;
3645 default:
3646 break;
3647 }
3648
3649 lpfc_cmd->prot_data = 0;
3650 lpfc_cmd->prot_data_type = 0;
3651 lpfc_cmd->prot_data_segment = NULL;
3652 }
3653#endif
James Smart109f6ed2008-12-04 22:39:08 -05003654 if (pnode && NLP_CHK_NODE_ACT(pnode))
3655 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05003656
3657 if (lpfc_cmd->status) {
3658 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
3659 (lpfc_cmd->result & IOERR_DRVR_MASK))
3660 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3661 else if (lpfc_cmd->status >= IOSTAT_CNT)
3662 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smartaa1c7ee2012-08-14 14:26:06 -04003663 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
3664 !lpfc_cmd->fcp_rsp->rspStatus3 &&
3665 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
3666 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
James Smart73d91e52011-10-10 21:32:10 -04003667 logit = 0;
3668 else
3669 logit = LOG_FCP | LOG_FCP_UNDER;
3670 lpfc_printf_vlog(vport, KERN_WARNING, logit,
3671 "9030 FCP cmd x%x failed <%d/%d> "
James Smart5a0d80f2012-05-09 21:18:20 -04003672 "status: x%x result: x%x "
3673 "sid: x%x did: x%x oxid: x%x "
3674 "Data: x%x x%x\n",
James Smart73d91e52011-10-10 21:32:10 -04003675 cmd->cmnd[0],
3676 cmd->device ? cmd->device->id : 0xffff,
3677 cmd->device ? cmd->device->lun : 0xffff,
3678 lpfc_cmd->status, lpfc_cmd->result,
James Smart5a0d80f2012-05-09 21:18:20 -04003679 vport->fc_myDID, pnode->nlp_DID,
3680 phba->sli_rev == LPFC_SLI_REV4 ?
3681 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
James Smart73d91e52011-10-10 21:32:10 -04003682 pIocbOut->iocb.ulpContext,
3683 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05003684
3685 switch (lpfc_cmd->status) {
3686 case IOSTAT_FCP_RSP_ERROR:
3687 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05003688 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05003689 break;
3690 case IOSTAT_NPORT_BSY:
3691 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04003692 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04003693 fast_path_evt = lpfc_alloc_fast_evt(phba);
3694 if (!fast_path_evt)
3695 break;
3696 fast_path_evt->un.fabric_evt.event_type =
3697 FC_REG_FABRIC_EVENT;
3698 fast_path_evt->un.fabric_evt.subcategory =
3699 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
3700 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
3701 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3702 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
3703 &pnode->nlp_portname,
3704 sizeof(struct lpfc_name));
3705 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
3706 &pnode->nlp_nodename,
3707 sizeof(struct lpfc_name));
3708 }
3709 fast_path_evt->vport = vport;
3710 fast_path_evt->work_evt.evt =
3711 LPFC_EVT_FASTPATH_MGMT_EVT;
3712 spin_lock_irqsave(&phba->hbalock, flags);
3713 list_add_tail(&fast_path_evt->work_evt.evt_listp,
3714 &phba->work_list);
3715 spin_unlock_irqrestore(&phba->hbalock, flags);
3716 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05003717 break;
James Smart92d7f7b2007-06-17 19:56:38 -05003718 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05003719 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05003720 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
3721 lpfc_cmd->result ==
3722 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
3723 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
3724 lpfc_cmd->result ==
3725 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
3726 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
3727 break;
3728 }
James Smartd7c255b2008-08-24 21:50:00 -04003729 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05003730 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04003731 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
3732 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
James Smart92d7f7b2007-06-17 19:56:38 -05003733 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04003734 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05003735 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003736 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
3737 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
3738 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
3739 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
3740 /*
3741 * This is a response for a BG enabled
3742 * cmd. Parse BG error
3743 */
3744 lpfc_parse_bg_err(phba, lpfc_cmd,
3745 pIocbOut);
3746 break;
3747 } else {
3748 lpfc_printf_vlog(vport, KERN_WARNING,
3749 LOG_BG,
3750 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04003751 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05003752 }
3753 }
James Smart1151e3e2011-02-16 12:39:35 -05003754 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
3755 && (phba->sli_rev == LPFC_SLI_REV4)
3756 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
3757 /* This IO was aborted by the target, we don't
3758 * know the rxid and because we did not send the
3759 * ABTS we cannot generate and RRQ.
3760 */
3761 lpfc_set_rrq_active(phba, pnode,
James Smartee0f4fe2012-05-09 21:19:14 -04003762 lpfc_cmd->cur_iocbq.sli4_lxritag,
3763 0, 0);
James Smart1151e3e2011-02-16 12:39:35 -05003764 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003765 /* else: fall through */
dea31012005-04-17 16:05:31 -05003766 default:
3767 cmd->result = ScsiResult(DID_ERROR, 0);
3768 break;
3769 }
3770
James Smart58da1ff2008-04-07 10:15:56 -04003771 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003772 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04003773 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
3774 SAM_STAT_BUSY);
James Smartab56dc22011-02-16 12:39:57 -05003775 } else
dea31012005-04-17 16:05:31 -05003776 cmd->result = ScsiResult(DID_OK, 0);
dea31012005-04-17 16:05:31 -05003777
3778 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
3779 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
3780
James Smarte8b62012007-08-02 11:10:09 -04003781 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3782 "0710 Iodone <%d/%d> cmd %p, error "
3783 "x%x SNS x%x x%x Data: x%x x%x\n",
3784 cmd->device->id, cmd->device->lun, cmd,
3785 cmd->result, *lp, *(lp + 3), cmd->retries,
3786 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05003787 }
3788
James Smartea2151b2008-09-07 11:52:10 -04003789 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003790 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04003791 if (vport->cfg_max_scsicmpl_time &&
3792 time_after(jiffies, lpfc_cmd->start_time +
3793 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04003794 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05003795 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3796 if (pnode->cmd_qdepth >
3797 atomic_read(&pnode->cmd_pending) &&
3798 (atomic_read(&pnode->cmd_pending) >
3799 LPFC_MIN_TGT_QDEPTH) &&
3800 ((cmd->cmnd[0] == READ_10) ||
3801 (cmd->cmnd[0] == WRITE_10)))
3802 pnode->cmd_qdepth =
3803 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04003804
James Smart109f6ed2008-12-04 22:39:08 -05003805 pnode->last_change_time = jiffies;
3806 }
James Smarta257bf92009-04-06 18:48:10 -04003807 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05003808 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smart7dc517d2010-07-14 15:32:10 -04003809 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
James Smart977b5a02008-09-07 11:52:04 -04003810 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05003811 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04003812 spin_lock_irqsave(shost->host_lock, flags);
James Smart7dc517d2010-07-14 15:32:10 -04003813 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
3814 / 100;
3815 depth = depth ? depth : 1;
3816 pnode->cmd_qdepth += depth;
3817 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
3818 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05003819 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04003820 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05003821 }
James Smart977b5a02008-09-07 11:52:04 -04003822 }
3823
James Smart1dcb58e2007-04-25 09:51:30 -04003824 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04003825
3826 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
3827 queue_depth = cmd->device->queue_depth;
3828 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05003829 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003830
Jamie Wellnitzb8086082006-02-28 22:33:12 -05003831 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smartfa61a542008-01-11 01:52:42 -05003832 /*
3833 * If there is a thread waiting for command completion
3834 * wake up the thread.
3835 */
James Smarta257bf92009-04-06 18:48:10 -04003836 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04003837 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05003838 if (lpfc_cmd->waitq)
3839 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04003840 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05003841 lpfc_release_scsi_buf(phba, lpfc_cmd);
3842 return;
3843 }
3844
James Smart92d7f7b2007-06-17 19:56:38 -05003845 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04003846 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05003847
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003848 /*
3849 * Check for queue full. If the lun is reporting queue full, then
3850 * back off the lun queue depth to prevent target overloads.
3851 */
James Smart58da1ff2008-04-07 10:15:56 -04003852 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
3853 NLP_CHK_NODE_ACT(pnode)) {
James Smarta257bf92009-04-06 18:48:10 -04003854 shost_for_each_device(tmp_sdev, shost) {
3855 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003856 continue;
3857 depth = scsi_track_queue_full(tmp_sdev,
James Smart5ffc2662009-11-18 15:39:44 -05003858 tmp_sdev->queue_depth-1);
3859 if (depth <= 0)
3860 continue;
James Smarte8b62012007-08-02 11:10:09 -04003861 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3862 "0711 detected queue full - lun queue "
3863 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04003864 lpfc_send_sdev_queuedepth_change_event(phba, vport,
James Smart5ffc2662009-11-18 15:39:44 -05003865 pnode,
3866 tmp_sdev->lun,
3867 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003868 }
3869 }
3870
James Smartfa61a542008-01-11 01:52:42 -05003871 /*
3872 * If there is a thread waiting for command completion
3873 * wake up the thread.
3874 */
James Smarta257bf92009-04-06 18:48:10 -04003875 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04003876 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05003877 if (lpfc_cmd->waitq)
3878 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04003879 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05003880
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003881 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05003882}
3883
James Smart34b02dc2008-08-24 21:49:55 -04003884/**
James Smart3621a712009-04-06 18:47:14 -04003885 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04003886 * @data: A pointer to the immediate command data portion of the IOCB.
3887 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
3888 *
3889 * The routine copies the entire FCP command from @fcp_cmnd to @data while
3890 * byte swapping the data to big endian format for transmission on the wire.
3891 **/
3892static void
3893lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
3894{
3895 int i, j;
3896 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
3897 i += sizeof(uint32_t), j++) {
3898 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
3899 }
3900}
3901
James Smart9bad7672008-12-04 22:39:02 -05003902/**
James Smartf1126682009-06-10 17:22:44 -04003903 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05003904 * @vport: The virtual port for which this call is being executed.
3905 * @lpfc_cmd: The scsi command which needs to send.
3906 * @pnode: Pointer to lpfc_nodelist.
3907 *
3908 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04003909 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003910 **/
dea31012005-04-17 16:05:31 -05003911static void
James Smartf1126682009-06-10 17:22:44 -04003912lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05003913 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05003914{
James Smart2e0fef82007-06-17 19:56:36 -05003915 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003916 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3917 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3918 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3919 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
3920 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04003921 char tag[2];
James Smart027140e2012-08-03 12:35:44 -04003922 uint8_t *ptr;
3923 bool sli4;
dea31012005-04-17 16:05:31 -05003924
James Smart58da1ff2008-04-07 10:15:56 -04003925 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3926 return;
3927
dea31012005-04-17 16:05:31 -05003928 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04003929 /* clear task management bits */
3930 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05003931
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04003932 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
3933 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05003934
James Smart027140e2012-08-03 12:35:44 -04003935 ptr = &fcp_cmnd->fcpCdb[0];
3936 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
3937 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
3938 ptr += scsi_cmnd->cmd_len;
3939 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
3940 }
3941
James Smart7e2b19f2007-10-29 11:00:39 -04003942 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
3943 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05003944 case HEAD_OF_QUEUE_TAG:
3945 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
3946 break;
3947 case ORDERED_QUEUE_TAG:
3948 fcp_cmnd->fcpCntl1 = ORDERED_Q;
3949 break;
3950 default:
3951 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
3952 break;
3953 }
3954 } else
3955 fcp_cmnd->fcpCntl1 = 0;
3956
James Smart027140e2012-08-03 12:35:44 -04003957 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
3958
dea31012005-04-17 16:05:31 -05003959 /*
3960 * There are three possibilities here - use scatter-gather segment, use
3961 * the single mapping, or neither. Start the lpfc command prep by
3962 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3963 * data bde entry.
3964 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003965 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05003966 if (datadir == DMA_TO_DEVICE) {
3967 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart027140e2012-08-03 12:35:44 -04003968 if (sli4)
3969 iocb_cmd->ulpPU = PARM_READ_CHECK;
3970 else {
James Smart3772a992009-05-22 14:50:54 -04003971 iocb_cmd->un.fcpi.fcpi_parm = 0;
3972 iocb_cmd->ulpPU = 0;
James Smart027140e2012-08-03 12:35:44 -04003973 }
dea31012005-04-17 16:05:31 -05003974 fcp_cmnd->fcpCntl3 = WRITE_DATA;
3975 phba->fc4OutputRequests++;
3976 } else {
3977 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
3978 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05003979 fcp_cmnd->fcpCntl3 = READ_DATA;
3980 phba->fc4InputRequests++;
3981 }
3982 } else {
3983 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
3984 iocb_cmd->un.fcpi.fcpi_parm = 0;
3985 iocb_cmd->ulpPU = 0;
3986 fcp_cmnd->fcpCntl3 = 0;
3987 phba->fc4ControlRequests++;
3988 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003989 if (phba->sli_rev == 3 &&
3990 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04003991 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05003992 /*
3993 * Finish initializing those IOCB fields that are independent
3994 * of the scsi_cmnd request_buffer
3995 */
3996 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart027140e2012-08-03 12:35:44 -04003997 if (sli4)
James Smart6d368e52011-05-24 11:44:12 -04003998 piocbq->iocb.ulpContext =
3999 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05004000 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4001 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05004002 else
4003 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05004004
4005 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4006 piocbq->context1 = lpfc_cmd;
4007 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4008 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05004009 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05004010}
4011
James Smart9bad7672008-12-04 22:39:02 -05004012/**
James Smart6d368e52011-05-24 11:44:12 -04004013 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004014 * @vport: The virtual port for which this call is being executed.
4015 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4016 * @lun: Logical unit number.
4017 * @task_mgmt_cmd: SCSI task management command.
4018 *
James Smart3772a992009-05-22 14:50:54 -04004019 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4020 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004021 *
4022 * Return codes:
4023 * 0 - Error
4024 * 1 - Success
4025 **/
dea31012005-04-17 16:05:31 -05004026static int
James Smartf1126682009-06-10 17:22:44 -04004027lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05004028 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04004029 unsigned int lun,
dea31012005-04-17 16:05:31 -05004030 uint8_t task_mgmt_cmd)
4031{
dea31012005-04-17 16:05:31 -05004032 struct lpfc_iocbq *piocbq;
4033 IOCB_t *piocb;
4034 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04004035 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05004036 struct lpfc_nodelist *ndlp = rdata->pnode;
4037
James Smart58da1ff2008-04-07 10:15:56 -04004038 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4039 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05004040 return 0;
dea31012005-04-17 16:05:31 -05004041
dea31012005-04-17 16:05:31 -05004042 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004043 piocbq->vport = vport;
4044
dea31012005-04-17 16:05:31 -05004045 piocb = &piocbq->iocb;
4046
4047 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04004048 /* Clear out any old data in the FCP command area */
4049 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4050 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004051 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05004052 if (vport->phba->sli_rev == 3 &&
4053 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004054 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004055 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05004056 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04004057 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4058 piocb->ulpContext =
4059 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4060 }
dea31012005-04-17 16:05:31 -05004061 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
4062 piocb->ulpFCP2Rcvy = 1;
4063 }
4064 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
4065
4066 /* ulpTimeout is only one byte */
4067 if (lpfc_cmd->timeout > 0xff) {
4068 /*
4069 * Do not timeout the command at the firmware level.
4070 * The driver will provide the timeout mechanism.
4071 */
4072 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04004073 } else
dea31012005-04-17 16:05:31 -05004074 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04004075
4076 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4077 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004078
James Smart2e0fef82007-06-17 19:56:36 -05004079 return 1;
dea31012005-04-17 16:05:31 -05004080}
4081
James Smart9bad7672008-12-04 22:39:02 -05004082/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004083 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04004084 * @phba: The hba struct for which this call is being executed.
4085 * @dev_grp: The HBA PCI-Device group number.
4086 *
4087 * This routine sets up the SCSI interface API function jump table in @phba
4088 * struct.
4089 * Returns: 0 - success, -ENODEV - failure.
4090 **/
4091int
4092lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4093{
4094
James Smartf1126682009-06-10 17:22:44 -04004095 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4096 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04004097
James Smart3772a992009-05-22 14:50:54 -04004098 switch (dev_grp) {
4099 case LPFC_PCI_DEV_LP:
4100 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4101 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05004102 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004103 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05004104 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004105 break;
James Smartda0436e2009-05-22 14:51:39 -04004106 case LPFC_PCI_DEV_OC:
4107 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4108 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05004109 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004110 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05004111 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004112 break;
James Smart3772a992009-05-22 14:50:54 -04004113 default:
4114 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4115 "1418 Invalid HBA PCI-device group: 0x%x\n",
4116 dev_grp);
4117 return -ENODEV;
4118 break;
4119 }
James Smart3772a992009-05-22 14:50:54 -04004120 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05004121 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04004122 return 0;
4123}
4124
4125/**
James Smart3621a712009-04-06 18:47:14 -04004126 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05004127 * @phba: The Hba for which this call is being executed.
4128 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4129 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4130 *
4131 * This routine is IOCB completion routine for device reset and target reset
4132 * routine. This routine release scsi buffer associated with lpfc_cmd.
4133 **/
James Smart7054a602007-04-25 09:52:34 -04004134static void
4135lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4136 struct lpfc_iocbq *cmdiocbq,
4137 struct lpfc_iocbq *rspiocbq)
4138{
4139 struct lpfc_scsi_buf *lpfc_cmd =
4140 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4141 if (lpfc_cmd)
4142 lpfc_release_scsi_buf(phba, lpfc_cmd);
4143 return;
4144}
4145
James Smart9bad7672008-12-04 22:39:02 -05004146/**
James Smart3621a712009-04-06 18:47:14 -04004147 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05004148 * @host: The scsi host for which this call is being executed.
4149 *
4150 * This routine provides module information about hba.
4151 *
4152 * Reutrn code:
4153 * Pointer to char - Success.
4154 **/
dea31012005-04-17 16:05:31 -05004155const char *
4156lpfc_info(struct Scsi_Host *host)
4157{
James Smart2e0fef82007-06-17 19:56:36 -05004158 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4159 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004160 int len;
4161 static char lpfcinfobuf[384];
4162
4163 memset(lpfcinfobuf,0,384);
4164 if (phba && phba->pcidev){
4165 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4166 len = strlen(lpfcinfobuf);
4167 snprintf(lpfcinfobuf + len,
4168 384-len,
4169 " on PCI bus %02x device %02x irq %d",
4170 phba->pcidev->bus->number,
4171 phba->pcidev->devfn,
4172 phba->pcidev->irq);
4173 len = strlen(lpfcinfobuf);
4174 if (phba->Port[0]) {
4175 snprintf(lpfcinfobuf + len,
4176 384-len,
4177 " port %s",
4178 phba->Port);
4179 }
James Smart65467b62010-01-26 23:08:29 -05004180 len = strlen(lpfcinfobuf);
4181 if (phba->sli4_hba.link_state.logical_speed) {
4182 snprintf(lpfcinfobuf + len,
4183 384-len,
4184 " Logical Link Speed: %d Mbps",
4185 phba->sli4_hba.link_state.logical_speed * 10);
4186 }
dea31012005-04-17 16:05:31 -05004187 }
4188 return lpfcinfobuf;
4189}
4190
James Smart9bad7672008-12-04 22:39:02 -05004191/**
James Smart3621a712009-04-06 18:47:14 -04004192 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05004193 * @phba: The Hba for which this call is being executed.
4194 *
4195 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4196 * The default value of cfg_poll_tmo is 10 milliseconds.
4197 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004198static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4199{
4200 unsigned long poll_tmo_expires =
4201 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4202
4203 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
4204 mod_timer(&phba->fcp_poll_timer,
4205 poll_tmo_expires);
4206}
4207
James Smart9bad7672008-12-04 22:39:02 -05004208/**
James Smart3621a712009-04-06 18:47:14 -04004209 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004210 * @phba: The Hba for which this call is being executed.
4211 *
4212 * This routine starts the fcp_poll_timer of @phba.
4213 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004214void lpfc_poll_start_timer(struct lpfc_hba * phba)
4215{
4216 lpfc_poll_rearm_timer(phba);
4217}
4218
James Smart9bad7672008-12-04 22:39:02 -05004219/**
James Smart3621a712009-04-06 18:47:14 -04004220 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05004221 * @ptr: Map to lpfc_hba data structure pointer.
4222 *
4223 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4224 * and FCP Ring interrupt is disable.
4225 **/
4226
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004227void lpfc_poll_timeout(unsigned long ptr)
4228{
James Smart2e0fef82007-06-17 19:56:36 -05004229 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004230
4231 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004232 lpfc_sli_handle_fast_ring_event(phba,
4233 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4234
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004235 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4236 lpfc_poll_rearm_timer(phba);
4237 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004238}
4239
James Smart9bad7672008-12-04 22:39:02 -05004240/**
James Smart3621a712009-04-06 18:47:14 -04004241 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05004242 * @cmnd: Pointer to scsi_cmnd data structure.
4243 * @done: Pointer to done routine.
4244 *
4245 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4246 * This routine prepares an IOCB from scsi command and provides to firmware.
4247 * The @done callback is invoked after driver finished processing the command.
4248 *
4249 * Return value :
4250 * 0 - Success
4251 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4252 **/
dea31012005-04-17 16:05:31 -05004253static int
James Smartb9a7c632012-08-03 12:35:24 -04004254lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004255{
James Smart2e0fef82007-06-17 19:56:36 -05004256 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4257 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004258 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004259 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004260 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004261 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004262 int err;
dea31012005-04-17 16:05:31 -05004263
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004264 err = fc_remote_port_chkready(rport);
4265 if (err) {
4266 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004267 goto out_fail_command;
4268 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004269 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004270
James Smartbf086112011-08-21 21:48:13 -04004271 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004272 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004273
James Smart6a9c52c2009-10-02 15:16:51 -04004274 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4275 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4276 " op:%02x str=%s without registering for"
4277 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004278 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4279 dif_op_str[scsi_get_prot_op(cmnd)]);
4280 goto out_fail_command;
4281 }
4282
dea31012005-04-17 16:05:31 -05004283 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004284 * Catch race where our node has transitioned, but the
4285 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004286 */
James Smart6b415f52012-06-12 13:54:59 -04004287 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4288 goto out_tgt_busy;
James Smart7dc517d2010-07-14 15:32:10 -04004289 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
Mike Christie34963432011-02-25 14:04:28 -06004290 goto out_tgt_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05004291
James Smart19ca7602010-11-20 23:11:55 -05004292 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05004293 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004294 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004295
James Smarte8b62012007-08-02 11:10:09 -04004296 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4297 "0707 driver's buffer pool is empty, "
4298 "IO busied\n");
dea31012005-04-17 16:05:31 -05004299 goto out_host_busy;
4300 }
4301
4302 /*
4303 * Store the midlayer's command structure for the completion phase
4304 * and complete the command initialization.
4305 */
4306 lpfc_cmd->pCmd = cmnd;
4307 lpfc_cmd->rdata = rdata;
4308 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04004309 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05004310 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
dea31012005-04-17 16:05:31 -05004311
James Smarte2a0a9d2008-12-04 22:40:02 -05004312 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004313 if (vport->phba->cfg_enable_bg) {
James Smart26134702012-08-14 14:25:50 -04004314 lpfc_printf_vlog(vport, KERN_INFO, LOG_BG,
4315 "9033 BLKGRD: rcvd %s cmd:x%x "
4316 "sector x%llx cnt %u pt %x\n",
4317 dif_op_str[scsi_get_prot_op(cmnd)],
4318 cmnd->cmnd[0],
4319 (unsigned long long)scsi_get_lba(cmnd),
4320 blk_rq_sectors(cmnd->request),
4321 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004322 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004323 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4324 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004325 if (vport->phba->cfg_enable_bg) {
James Smart26134702012-08-14 14:25:50 -04004326 lpfc_printf_vlog(vport, KERN_INFO, LOG_BG,
4327 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4328 "x%x sector x%llx cnt %u pt %x\n",
4329 cmnd->cmnd[0],
4330 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004331 blk_rq_sectors(cmnd->request),
James Smart26134702012-08-14 14:25:50 -04004332 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004333 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004334 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4335 }
4336
dea31012005-04-17 16:05:31 -05004337 if (err)
4338 goto out_host_busy_free_buf;
4339
James Smart2e0fef82007-06-17 19:56:36 -05004340 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004341
James Smart977b5a02008-09-07 11:52:04 -04004342 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04004343 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004344 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05004345 if (err) {
4346 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05004347 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004348 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004349 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004350 lpfc_sli_handle_fast_ring_event(phba,
4351 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4352
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004353 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4354 lpfc_poll_rearm_timer(phba);
4355 }
4356
dea31012005-04-17 16:05:31 -05004357 return 0;
4358
4359 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04004360 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004361 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004362 out_host_busy:
4363 return SCSI_MLQUEUE_HOST_BUSY;
4364
Mike Christie34963432011-02-25 14:04:28 -06004365 out_tgt_busy:
4366 return SCSI_MLQUEUE_TARGET_BUSY;
4367
dea31012005-04-17 16:05:31 -05004368 out_fail_command:
James Smartb9a7c632012-08-03 12:35:24 -04004369 cmnd->scsi_done(cmnd);
dea31012005-04-17 16:05:31 -05004370 return 0;
4371}
4372
Jeff Garzikf2812332010-11-16 02:10:29 -05004373
James Smart9bad7672008-12-04 22:39:02 -05004374/**
James Smart3621a712009-04-06 18:47:14 -04004375 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004376 * @cmnd: Pointer to scsi_cmnd data structure.
4377 *
4378 * This routine aborts @cmnd pending in base driver.
4379 *
4380 * Return code :
4381 * 0x2003 - Error
4382 * 0x2002 - Success
4383 **/
dea31012005-04-17 16:05:31 -05004384static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004385lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004386{
James Smart2e0fef82007-06-17 19:56:36 -05004387 struct Scsi_Host *shost = cmnd->device->host;
4388 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4389 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004390 struct lpfc_iocbq *iocb;
4391 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004392 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004393 IOCB_t *cmd, *icmd;
James Smart3a707302012-06-12 13:54:42 -04004394 int ret = SUCCESS, status = 0;
James Smartfa61a542008-01-11 01:52:42 -05004395 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004396
James Smart3a707302012-06-12 13:54:42 -04004397 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004398 if (status != 0 && status != SUCCESS)
James Smart3a707302012-06-12 13:54:42 -04004399 return status;
James Smart4f2e66c2012-05-09 21:17:07 -04004400
4401 spin_lock_irq(&phba->hbalock);
4402 /* driver queued commands are in process of being flushed */
4403 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
4404 spin_unlock_irq(&phba->hbalock);
4405 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4406 "3168 SCSI Layer abort requested I/O has been "
4407 "flushed by LLD.\n");
4408 return FAILED;
4409 }
4410
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004411 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smarteee88772010-09-29 11:19:08 -04004412 if (!lpfc_cmd) {
James Smart4f2e66c2012-05-09 21:17:07 -04004413 spin_unlock_irq(&phba->hbalock);
James Smarteee88772010-09-29 11:19:08 -04004414 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4415 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004416 "x%x ID %d LUN %d\n",
James Smart3a707302012-06-12 13:54:42 -04004417 SUCCESS, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04004418 return SUCCESS;
4419 }
dea31012005-04-17 16:05:31 -05004420
James Smart4f2e66c2012-05-09 21:17:07 -04004421 iocb = &lpfc_cmd->cur_iocbq;
4422 /* the command is in process of being cancelled */
4423 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
4424 spin_unlock_irq(&phba->hbalock);
4425 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4426 "3169 SCSI Layer abort requested I/O has been "
4427 "cancelled by LLD.\n");
4428 return FAILED;
4429 }
dea31012005-04-17 16:05:31 -05004430 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004431 * If pCmd field of the corresponding lpfc_scsi_buf structure
4432 * points to a different SCSI command, then the driver has
4433 * already completed this command, but the midlayer did not
James Smart4f2e66c2012-05-09 21:17:07 -04004434 * see the completion before the eh fired. Just return SUCCESS.
dea31012005-04-17 16:05:31 -05004435 */
James Smart4f2e66c2012-05-09 21:17:07 -04004436 if (lpfc_cmd->pCmd != cmnd) {
4437 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4438 "3170 SCSI Layer abort requested I/O has been "
4439 "completed by LLD.\n");
4440 goto out_unlock;
4441 }
dea31012005-04-17 16:05:31 -05004442
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004443 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004444
James Smart4f2e66c2012-05-09 21:17:07 -04004445 abtsiocb = __lpfc_sli_get_iocbq(phba);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004446 if (abtsiocb == NULL) {
4447 ret = FAILED;
James Smart4f2e66c2012-05-09 21:17:07 -04004448 goto out_unlock;
dea31012005-04-17 16:05:31 -05004449 }
4450
dea31012005-04-17 16:05:31 -05004451 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004452 * The scsi command can not be in txq and it is in flight because the
4453 * pCmd is still pointig at the SCSI command we have to abort. There
4454 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004455 */
dea31012005-04-17 16:05:31 -05004456
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004457 cmd = &iocb->iocb;
4458 icmd = &abtsiocb->iocb;
4459 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4460 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004461 if (phba->sli_rev == LPFC_SLI_REV4)
4462 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4463 else
4464 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004465
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004466 icmd->ulpLe = 1;
4467 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004468
4469 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4470 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05004471 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05004472
James Smart2e0fef82007-06-17 19:56:36 -05004473 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004474 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4475 else
4476 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004477
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004478 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004479 abtsiocb->vport = vport;
James Smart4f2e66c2012-05-09 21:17:07 -04004480 /* no longer need the lock after this point */
4481 spin_unlock_irq(&phba->hbalock);
4482
James Smart3772a992009-05-22 14:50:54 -04004483 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
4484 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004485 lpfc_sli_release_iocbq(phba, abtsiocb);
4486 ret = FAILED;
4487 goto out;
4488 }
4489
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004490 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004491 lpfc_sli_handle_fast_ring_event(phba,
4492 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004493
James Smartfa61a542008-01-11 01:52:42 -05004494 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004495 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004496 wait_event_timeout(waitq,
4497 (lpfc_cmd->pCmd != cmnd),
4498 (2*vport->cfg_devloss_tmo*HZ));
James Smartfa61a542008-01-11 01:52:42 -05004499 lpfc_cmd->waitq = NULL;
dea31012005-04-17 16:05:31 -05004500
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004501 if (lpfc_cmd->pCmd == cmnd) {
4502 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04004503 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4504 "0748 abort handler timed out waiting "
James Smart247ca942012-08-14 14:26:13 -04004505 "for abortng I/O (xri:x%x) to complete: "
4506 "ret %#x, ID %d, LUN %d\n",
4507 iocb->sli4_xritag, ret,
4508 cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004509 }
James Smart4f2e66c2012-05-09 21:17:07 -04004510 goto out;
dea31012005-04-17 16:05:31 -05004511
James Smart4f2e66c2012-05-09 21:17:07 -04004512out_unlock:
4513 spin_unlock_irq(&phba->hbalock);
4514out:
James Smarte8b62012007-08-02 11:10:09 -04004515 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4516 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004517 "LUN %d\n", ret, cmnd->device->id,
4518 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004519 return ret;
dea31012005-04-17 16:05:31 -05004520}
4521
James Smartbbb9d182009-06-10 17:23:16 -04004522static char *
4523lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4524{
4525 switch (task_mgmt_cmd) {
4526 case FCP_ABORT_TASK_SET:
4527 return "ABORT_TASK_SET";
4528 case FCP_CLEAR_TASK_SET:
4529 return "FCP_CLEAR_TASK_SET";
4530 case FCP_BUS_RESET:
4531 return "FCP_BUS_RESET";
4532 case FCP_LUN_RESET:
4533 return "FCP_LUN_RESET";
4534 case FCP_TARGET_RESET:
4535 return "FCP_TARGET_RESET";
4536 case FCP_CLEAR_ACA:
4537 return "FCP_CLEAR_ACA";
4538 case FCP_TERMINATE_TASK:
4539 return "FCP_TERMINATE_TASK";
4540 default:
4541 return "unknown";
4542 }
4543}
4544
4545/**
4546 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
4547 * @vport: The virtual port for which this call is being executed.
4548 * @rdata: Pointer to remote port local data
4549 * @tgt_id: Target ID of remote device.
4550 * @lun_id: Lun number for the TMF
4551 * @task_mgmt_cmd: type of TMF to send
4552 *
4553 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
4554 * a remote port.
4555 *
4556 * Return Code:
4557 * 0x2003 - Error
4558 * 0x2002 - Success.
4559 **/
4560static int
4561lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
4562 unsigned tgt_id, unsigned int lun_id,
4563 uint8_t task_mgmt_cmd)
4564{
4565 struct lpfc_hba *phba = vport->phba;
4566 struct lpfc_scsi_buf *lpfc_cmd;
4567 struct lpfc_iocbq *iocbq;
4568 struct lpfc_iocbq *iocbqrsp;
James Smart5989b8d2010-10-22 11:06:56 -04004569 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004570 int ret;
4571 int status;
4572
James Smart5989b8d2010-10-22 11:06:56 -04004573 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smartbbb9d182009-06-10 17:23:16 -04004574 return FAILED;
4575
James Smart19ca7602010-11-20 23:11:55 -05004576 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
James Smartbbb9d182009-06-10 17:23:16 -04004577 if (lpfc_cmd == NULL)
4578 return FAILED;
4579 lpfc_cmd->timeout = 60;
4580 lpfc_cmd->rdata = rdata;
4581
4582 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
4583 task_mgmt_cmd);
4584 if (!status) {
4585 lpfc_release_scsi_buf(phba, lpfc_cmd);
4586 return FAILED;
4587 }
4588
4589 iocbq = &lpfc_cmd->cur_iocbq;
4590 iocbqrsp = lpfc_sli_get_iocbq(phba);
4591 if (iocbqrsp == NULL) {
4592 lpfc_release_scsi_buf(phba, lpfc_cmd);
4593 return FAILED;
4594 }
4595
4596 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4597 "0702 Issue %s to TGT %d LUN %d "
James Smart6d368e52011-05-24 11:44:12 -04004598 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04004599 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04004600 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
4601 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04004602
4603 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
4604 iocbq, iocbqrsp, lpfc_cmd->timeout);
4605 if (status != IOCB_SUCCESS) {
4606 if (status == IOCB_TIMEDOUT) {
4607 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
4608 ret = TIMEOUT_ERROR;
4609 } else
4610 ret = FAILED;
4611 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4612 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart6d368e52011-05-24 11:44:12 -04004613 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
4614 "iocb_flag x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04004615 lpfc_taskmgmt_name(task_mgmt_cmd),
4616 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
James Smart6d368e52011-05-24 11:44:12 -04004617 iocbqrsp->iocb.un.ulpWord[4],
4618 iocbq->iocb_flag);
James Smart2a9bf3d2010-06-07 15:24:45 -04004619 } else if (status == IOCB_BUSY)
4620 ret = FAILED;
4621 else
James Smartbbb9d182009-06-10 17:23:16 -04004622 ret = SUCCESS;
4623
4624 lpfc_sli_release_iocbq(phba, iocbqrsp);
4625
4626 if (ret != TIMEOUT_ERROR)
4627 lpfc_release_scsi_buf(phba, lpfc_cmd);
4628
4629 return ret;
4630}
4631
4632/**
4633 * lpfc_chk_tgt_mapped -
4634 * @vport: The virtual port to check on
4635 * @cmnd: Pointer to scsi_cmnd data structure.
4636 *
4637 * This routine delays until the scsi target (aka rport) for the
4638 * command exists (is present and logged in) or we declare it non-existent.
4639 *
4640 * Return code :
4641 * 0x2003 - Error
4642 * 0x2002 - Success
4643 **/
4644static int
4645lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
4646{
4647 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004648 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004649 unsigned long later;
4650
James Smart1c6f4ef52009-11-18 15:40:49 -05004651 if (!rdata) {
4652 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4653 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
4654 return FAILED;
4655 }
4656 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004657 /*
4658 * If target is not in a MAPPED state, delay until
4659 * target is rediscovered or devloss timeout expires.
4660 */
4661 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
4662 while (time_after(later, jiffies)) {
4663 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4664 return FAILED;
4665 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
4666 return SUCCESS;
4667 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
4668 rdata = cmnd->device->hostdata;
4669 if (!rdata)
4670 return FAILED;
4671 pnode = rdata->pnode;
4672 }
4673 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
4674 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
4675 return FAILED;
4676 return SUCCESS;
4677}
4678
4679/**
4680 * lpfc_reset_flush_io_context -
4681 * @vport: The virtual port (scsi_host) for the flush context
4682 * @tgt_id: If aborting by Target contect - specifies the target id
4683 * @lun_id: If aborting by Lun context - specifies the lun id
4684 * @context: specifies the context level to flush at.
4685 *
4686 * After a reset condition via TMF, we need to flush orphaned i/o
4687 * contexts from the adapter. This routine aborts any contexts
4688 * outstanding, then waits for their completions. The wait is
4689 * bounded by devloss_tmo though.
4690 *
4691 * Return code :
4692 * 0x2003 - Error
4693 * 0x2002 - Success
4694 **/
4695static int
4696lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
4697 uint64_t lun_id, lpfc_ctx_cmd context)
4698{
4699 struct lpfc_hba *phba = vport->phba;
4700 unsigned long later;
4701 int cnt;
4702
4703 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
4704 if (cnt)
4705 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
4706 tgt_id, lun_id, context);
4707 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
4708 while (time_after(later, jiffies) && cnt) {
4709 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
4710 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
4711 }
4712 if (cnt) {
4713 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4714 "0724 I/O flush failure for context %s : cnt x%x\n",
4715 ((context == LPFC_CTX_LUN) ? "LUN" :
4716 ((context == LPFC_CTX_TGT) ? "TGT" :
4717 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
4718 cnt);
4719 return FAILED;
4720 }
4721 return SUCCESS;
4722}
4723
James Smart9bad7672008-12-04 22:39:02 -05004724/**
James Smart3621a712009-04-06 18:47:14 -04004725 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05004726 * @cmnd: Pointer to scsi_cmnd data structure.
4727 *
James Smartbbb9d182009-06-10 17:23:16 -04004728 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05004729 * command.
4730 *
4731 * Return code :
4732 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04004733 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05004734 **/
dea31012005-04-17 16:05:31 -05004735static int
James Smart7054a602007-04-25 09:52:34 -04004736lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004737{
James Smart2e0fef82007-06-17 19:56:36 -05004738 struct Scsi_Host *shost = cmnd->device->host;
4739 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05004740 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004741 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004742 unsigned tgt_id = cmnd->device->id;
4743 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04004744 struct lpfc_scsi_event_header scsi_event;
James Smart3a707302012-06-12 13:54:42 -04004745 int status, ret = SUCCESS;
dea31012005-04-17 16:05:31 -05004746
James Smart1c6f4ef52009-11-18 15:40:49 -05004747 if (!rdata) {
4748 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4749 "0798 Device Reset rport failure: rdata x%p\n", rdata);
4750 return FAILED;
4751 }
4752 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04004753 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004754 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04004755 return status;
James Smartbbb9d182009-06-10 17:23:16 -04004756
4757 status = lpfc_chk_tgt_mapped(vport, cmnd);
4758 if (status == FAILED) {
4759 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4760 "0721 Device Reset rport failure: rdata x%p\n", rdata);
4761 return FAILED;
4762 }
4763
4764 scsi_event.event_type = FC_REG_SCSI_EVENT;
4765 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
4766 scsi_event.lun = lun_id;
4767 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
4768 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
4769
4770 fc_host_post_vendor_event(shost, fc_get_event_number(),
4771 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
4772
4773 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
4774 FCP_LUN_RESET);
4775
4776 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4777 "0713 SCSI layer issued Device Reset (%d, %d) "
4778 "return x%x\n", tgt_id, lun_id, status);
4779
dea31012005-04-17 16:05:31 -05004780 /*
James Smartbbb9d182009-06-10 17:23:16 -04004781 * We have to clean up i/o as : they may be orphaned by the TMF;
4782 * or if the TMF failed, they may be in an indeterminate state.
4783 * So, continue on.
4784 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05004785 */
James Smart3a707302012-06-12 13:54:42 -04004786 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04004787 LPFC_CTX_LUN);
James Smart3a707302012-06-12 13:54:42 -04004788 return ret;
James Smartbbb9d182009-06-10 17:23:16 -04004789}
4790
4791/**
4792 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
4793 * @cmnd: Pointer to scsi_cmnd data structure.
4794 *
4795 * This routine does a target reset by sending a TARGET_RESET task management
4796 * command.
4797 *
4798 * Return code :
4799 * 0x2003 - Error
4800 * 0x2002 - Success
4801 **/
4802static int
4803lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
4804{
4805 struct Scsi_Host *shost = cmnd->device->host;
4806 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4807 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004808 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004809 unsigned tgt_id = cmnd->device->id;
4810 unsigned int lun_id = cmnd->device->lun;
4811 struct lpfc_scsi_event_header scsi_event;
James Smart3a707302012-06-12 13:54:42 -04004812 int status, ret = SUCCESS;
James Smartbbb9d182009-06-10 17:23:16 -04004813
James Smart1c6f4ef52009-11-18 15:40:49 -05004814 if (!rdata) {
4815 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4816 "0799 Target Reset rport failure: rdata x%p\n", rdata);
4817 return FAILED;
4818 }
4819 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04004820 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004821 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04004822 return status;
James Smartbbb9d182009-06-10 17:23:16 -04004823
4824 status = lpfc_chk_tgt_mapped(vport, cmnd);
4825 if (status == FAILED) {
4826 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4827 "0722 Target Reset rport failure: rdata x%p\n", rdata);
4828 return FAILED;
dea31012005-04-17 16:05:31 -05004829 }
James Smartea2151b2008-09-07 11:52:10 -04004830
4831 scsi_event.event_type = FC_REG_SCSI_EVENT;
4832 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
4833 scsi_event.lun = 0;
4834 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
4835 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
4836
James Smartbbb9d182009-06-10 17:23:16 -04004837 fc_host_post_vendor_event(shost, fc_get_event_number(),
4838 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04004839
James Smartbbb9d182009-06-10 17:23:16 -04004840 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
4841 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05004842
James Smarte8b62012007-08-02 11:10:09 -04004843 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04004844 "0723 SCSI layer issued Target Reset (%d, %d) "
4845 "return x%x\n", tgt_id, lun_id, status);
4846
4847 /*
4848 * We have to clean up i/o as : they may be orphaned by the TMF;
4849 * or if the TMF failed, they may be in an indeterminate state.
4850 * So, continue on.
4851 * We will report success if all the i/o aborts successfully.
4852 */
James Smart3a707302012-06-12 13:54:42 -04004853 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
4854 LPFC_CTX_TGT);
4855 return ret;
dea31012005-04-17 16:05:31 -05004856}
4857
James Smart9bad7672008-12-04 22:39:02 -05004858/**
James Smart3621a712009-04-06 18:47:14 -04004859 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004860 * @cmnd: Pointer to scsi_cmnd data structure.
4861 *
James Smartbbb9d182009-06-10 17:23:16 -04004862 * This routine does target reset to all targets on @cmnd->device->host.
4863 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05004864 *
James Smartbbb9d182009-06-10 17:23:16 -04004865 * Return code :
4866 * 0x2003 - Error
4867 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05004868 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04004869static int
James Smart7054a602007-04-25 09:52:34 -04004870lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004871{
James Smart2e0fef82007-06-17 19:56:36 -05004872 struct Scsi_Host *shost = cmnd->device->host;
4873 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05004874 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04004875 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04004876 int match;
4877 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04004878
4879 scsi_event.event_type = FC_REG_SCSI_EVENT;
4880 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
4881 scsi_event.lun = 0;
4882 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
4883 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
4884
James Smartbbb9d182009-06-10 17:23:16 -04004885 fc_host_post_vendor_event(shost, fc_get_event_number(),
4886 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05004887
James Smartbf086112011-08-21 21:48:13 -04004888 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004889 if (status != 0 && status != SUCCESS)
James Smartbf086112011-08-21 21:48:13 -04004890 return status;
James Smartbbb9d182009-06-10 17:23:16 -04004891
dea31012005-04-17 16:05:31 -05004892 /*
4893 * Since the driver manages a single bus device, reset all
4894 * targets known to the driver. Should any target reset
4895 * fail, this routine returns failure to the midlayer.
4896 */
James Smarte17da182006-07-06 15:49:25 -04004897 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04004898 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05004899 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004900 spin_lock_irq(shost->host_lock);
4901 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004902 if (!NLP_CHK_NODE_ACT(ndlp))
4903 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004904 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04004905 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04004906 ndlp->rport) {
dea31012005-04-17 16:05:31 -05004907 match = 1;
4908 break;
4909 }
4910 }
James Smart2e0fef82007-06-17 19:56:36 -05004911 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004912 if (!match)
4913 continue;
James Smartbbb9d182009-06-10 17:23:16 -04004914
4915 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
4916 i, 0, FCP_TARGET_RESET);
4917
4918 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04004919 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4920 "0700 Bus Reset on target %d failed\n",
4921 i);
James Smart915caaa2008-06-14 22:52:38 -04004922 ret = FAILED;
dea31012005-04-17 16:05:31 -05004923 }
4924 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05004925 /*
James Smartbbb9d182009-06-10 17:23:16 -04004926 * We have to clean up i/o as : they may be orphaned by the TMFs
4927 * above; or if any of the TMFs failed, they may be in an
4928 * indeterminate state.
4929 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05004930 */
James Smartbbb9d182009-06-10 17:23:16 -04004931
4932 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
4933 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05004934 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04004935
James Smarte8b62012007-08-02 11:10:09 -04004936 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4937 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05004938 return ret;
4939}
4940
James Smart9bad7672008-12-04 22:39:02 -05004941/**
James Smart27b01b82012-05-09 21:19:44 -04004942 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
4943 * @cmnd: Pointer to scsi_cmnd data structure.
4944 *
4945 * This routine does host reset to the adaptor port. It brings the HBA
4946 * offline, performs a board restart, and then brings the board back online.
4947 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
4948 * reject all outstanding SCSI commands to the host and error returned
4949 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
4950 * of error handling, it will only return error if resetting of the adapter
4951 * is not successful; in all other cases, will return success.
4952 *
4953 * Return code :
4954 * 0x2003 - Error
4955 * 0x2002 - Success
4956 **/
4957static int
4958lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
4959{
4960 struct Scsi_Host *shost = cmnd->device->host;
4961 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4962 struct lpfc_hba *phba = vport->phba;
4963 int rc, ret = SUCCESS;
4964
James Smart618a5232012-06-12 13:54:36 -04004965 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart27b01b82012-05-09 21:19:44 -04004966 lpfc_offline(phba);
4967 rc = lpfc_sli_brdrestart(phba);
4968 if (rc)
4969 ret = FAILED;
4970 lpfc_online(phba);
4971 lpfc_unblock_mgmt_io(phba);
4972
4973 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
4974 "3172 SCSI layer issued Host Reset Data: x%x\n", ret);
4975 return ret;
4976}
4977
4978/**
James Smart3621a712009-04-06 18:47:14 -04004979 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05004980 * @sdev: Pointer to scsi_device.
4981 *
4982 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
4983 * globally available list of scsi buffers. This routine also makes sure scsi
4984 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
4985 * of scsi buffer exists for the lifetime of the driver.
4986 *
4987 * Return codes:
4988 * non-0 - Error
4989 * 0 - Success
4990 **/
dea31012005-04-17 16:05:31 -05004991static int
dea31012005-04-17 16:05:31 -05004992lpfc_slave_alloc(struct scsi_device *sdev)
4993{
James Smart2e0fef82007-06-17 19:56:36 -05004994 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
4995 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004996 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04004997 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05004998 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04004999 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04005000 uint32_t sdev_cnt;
dea31012005-04-17 16:05:31 -05005001
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005002 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05005003 return -ENXIO;
5004
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005005 sdev->hostdata = rport->dd_data;
James Smartd7c47992010-06-08 18:31:54 -04005006 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005007
5008 /*
5009 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5010 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04005011 * HBA limit conveyed to the midlayer via the host structure. The
5012 * formula accounts for the lun_queue_depth + error handlers + 1
5013 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05005014 */
5015 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04005016 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05005017
James Smartd7c47992010-06-08 18:31:54 -04005018 /* If allocated buffers are enough do nothing */
5019 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5020 return 0;
5021
James Smart92d7f7b2007-06-17 19:56:38 -05005022 /* Allow some exchanges to be available always to complete discovery */
5023 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005024 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5025 "0704 At limitation of %d preallocated "
5026 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05005027 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05005028 /* Allow some exchanges to be available always to complete discovery */
5029 } else if (total + num_to_alloc >
5030 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005031 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5032 "0705 Allocation request of %d "
5033 "command buffers will exceed max of %d. "
5034 "Reducing allocation request to %d.\n",
5035 num_to_alloc, phba->cfg_hba_queue_depth,
5036 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05005037 num_to_alloc = phba->cfg_hba_queue_depth - total;
5038 }
James Smart3772a992009-05-22 14:50:54 -04005039 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
5040 if (num_to_alloc != num_allocated) {
5041 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5042 "0708 Allocation request of %d "
5043 "command buffers did not succeed. "
5044 "Allocated %d buffers.\n",
5045 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05005046 }
James Smart1c6f4ef52009-11-18 15:40:49 -05005047 if (num_allocated > 0)
5048 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05005049 return 0;
5050}
5051
James Smart9bad7672008-12-04 22:39:02 -05005052/**
James Smart3621a712009-04-06 18:47:14 -04005053 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05005054 * @sdev: Pointer to scsi_device.
5055 *
5056 * This routine configures following items
5057 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05005058 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5059 *
5060 * Return codes:
5061 * 0 - Success
5062 **/
dea31012005-04-17 16:05:31 -05005063static int
5064lpfc_slave_configure(struct scsi_device *sdev)
5065{
James Smart2e0fef82007-06-17 19:56:36 -05005066 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5067 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005068
5069 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04005070 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005071 else
James Smart3de2a652007-08-02 11:09:59 -04005072 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005073
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005074 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005075 lpfc_sli_handle_fast_ring_event(phba,
5076 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005077 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5078 lpfc_poll_rearm_timer(phba);
5079 }
5080
dea31012005-04-17 16:05:31 -05005081 return 0;
5082}
5083
James Smart9bad7672008-12-04 22:39:02 -05005084/**
James Smart3621a712009-04-06 18:47:14 -04005085 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05005086 * @sdev: Pointer to scsi_device.
5087 *
5088 * This routine sets @sdev hostatdata filed to null.
5089 **/
dea31012005-04-17 16:05:31 -05005090static void
5091lpfc_slave_destroy(struct scsi_device *sdev)
5092{
James Smartd7c47992010-06-08 18:31:54 -04005093 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5094 struct lpfc_hba *phba = vport->phba;
5095 atomic_dec(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005096 sdev->hostdata = NULL;
5097 return;
5098}
5099
James Smart92d7f7b2007-06-17 19:56:38 -05005100
dea31012005-04-17 16:05:31 -05005101struct scsi_host_template lpfc_template = {
5102 .module = THIS_MODULE,
5103 .name = LPFC_DRIVER_NAME,
5104 .info = lpfc_info,
5105 .queuecommand = lpfc_queuecommand,
5106 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005107 .eh_device_reset_handler = lpfc_device_reset_handler,
5108 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04005109 .eh_bus_reset_handler = lpfc_bus_reset_handler,
James Smart27b01b82012-05-09 21:19:44 -04005110 .eh_host_reset_handler = lpfc_host_reset_handler,
dea31012005-04-17 16:05:31 -05005111 .slave_alloc = lpfc_slave_alloc,
5112 .slave_configure = lpfc_slave_configure,
5113 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04005114 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05005115 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005116 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05005117 .cmd_per_lun = LPFC_CMD_PER_LUN,
5118 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05005119 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04005120 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04005121 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05005122 .change_queue_depth = lpfc_change_queue_depth,
dea31012005-04-17 16:05:31 -05005123};
James Smart3de2a652007-08-02 11:09:59 -04005124
5125struct scsi_host_template lpfc_vport_template = {
5126 .module = THIS_MODULE,
5127 .name = LPFC_DRIVER_NAME,
5128 .info = lpfc_info,
5129 .queuecommand = lpfc_queuecommand,
5130 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005131 .eh_device_reset_handler = lpfc_device_reset_handler,
5132 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04005133 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5134 .slave_alloc = lpfc_slave_alloc,
5135 .slave_configure = lpfc_slave_configure,
5136 .slave_destroy = lpfc_slave_destroy,
5137 .scan_finished = lpfc_scan_finished,
5138 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005139 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04005140 .cmd_per_lun = LPFC_CMD_PER_LUN,
5141 .use_clustering = ENABLE_CLUSTERING,
5142 .shost_attrs = lpfc_vport_attrs,
5143 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05005144 .change_queue_depth = lpfc_change_queue_depth,
James Smart3de2a652007-08-02 11:09:59 -04005145};