blob: 9434b0379abe45363eaf5148c809e0716ff307ef [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
63static char *dif_grd_str[] = {
64 "NO_GUARD",
65 "DIF_CRC",
66 "DIX_IP",
James Smarte2a0a9d2008-12-04 22:40:02 -050067};
James Smartf9bb2da2011-10-10 21:34:11 -040068
69struct scsi_dif_tuple {
70 __be16 guard_tag; /* Checksum */
71 __be16 app_tag; /* Opaque storage */
72 __be32 ref_tag; /* Target LBA or indirect LBA */
73};
74
James Smartda0436e2009-05-22 14:51:39 -040075static void
76lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smart1c6f4ef52009-11-18 15:40:49 -050077static void
78lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smarte2a0a9d2008-12-04 22:40:02 -050079
80static void
James Smart6a9c52c2009-10-02 15:16:51 -040081lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -050082{
83 void *src, *dst;
84 struct scatterlist *sgde = scsi_sglist(cmnd);
85
86 if (!_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040087 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
88 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -050089 __func__);
90 return;
91 }
92
93
94 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -040095 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
96 "9051 BLKGRD: ERROR: data scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -050097 return;
98 }
99
100 dst = (void *) _dump_buf_data;
101 while (sgde) {
102 src = sg_virt(sgde);
103 memcpy(dst, src, sgde->length);
104 dst += sgde->length;
105 sgde = sg_next(sgde);
106 }
107}
108
109static void
James Smart6a9c52c2009-10-02 15:16:51 -0400110lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -0500111{
112 void *src, *dst;
113 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
114
115 if (!_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -0400116 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
117 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -0500118 __func__);
119 return;
120 }
121
122 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -0400123 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
124 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -0500125 return;
126 }
127
128 dst = _dump_buf_dif;
129 while (sgde) {
130 src = sg_virt(sgde);
131 memcpy(dst, src, sgde->length);
132 dst += sgde->length;
133 sgde = sg_next(sgde);
134 }
135}
136
James Smartea2151b2008-09-07 11:52:10 -0400137/**
James Smartf1126682009-06-10 17:22:44 -0400138 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
139 * @phba: Pointer to HBA object.
140 * @lpfc_cmd: lpfc scsi command object pointer.
141 *
142 * This function is called from the lpfc_prep_task_mgmt_cmd function to
143 * set the last bit in the response sge entry.
144 **/
145static void
146lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
147 struct lpfc_scsi_buf *lpfc_cmd)
148{
149 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
150 if (sgl) {
151 sgl += 1;
152 sgl->word2 = le32_to_cpu(sgl->word2);
153 bf_set(lpfc_sli4_sge_last, sgl, 1);
154 sgl->word2 = cpu_to_le32(sgl->word2);
155 }
156}
157
158/**
James Smart3621a712009-04-06 18:47:14 -0400159 * lpfc_update_stats - Update statistical data for the command completion
James Smartea2151b2008-09-07 11:52:10 -0400160 * @phba: Pointer to HBA object.
161 * @lpfc_cmd: lpfc scsi command object pointer.
162 *
163 * This function is called when there is a command completion and this
164 * function updates the statistical data for the command completion.
165 **/
166static void
167lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
168{
169 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
170 struct lpfc_nodelist *pnode = rdata->pnode;
171 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
172 unsigned long flags;
173 struct Scsi_Host *shost = cmd->device->host;
174 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
175 unsigned long latency;
176 int i;
177
178 if (cmd->result)
179 return;
180
James Smart9f1e1b52008-12-04 22:39:40 -0500181 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
182
James Smartea2151b2008-09-07 11:52:10 -0400183 spin_lock_irqsave(shost->host_lock, flags);
184 if (!vport->stat_data_enabled ||
185 vport->stat_data_blocked ||
James Smart5989b8d2010-10-22 11:06:56 -0400186 !pnode ||
James Smartea2151b2008-09-07 11:52:10 -0400187 !pnode->lat_data ||
188 (phba->bucket_type == LPFC_NO_BUCKET)) {
189 spin_unlock_irqrestore(shost->host_lock, flags);
190 return;
191 }
James Smartea2151b2008-09-07 11:52:10 -0400192
193 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
194 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
195 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -0500196 /* check array subscript bounds */
197 if (i < 0)
198 i = 0;
199 else if (i >= LPFC_MAX_BUCKET_COUNT)
200 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -0400201 } else {
202 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
203 if (latency <= (phba->bucket_base +
204 ((1<<i)*phba->bucket_step)))
205 break;
206 }
207
208 pnode->lat_data[i].cmd_count++;
209 spin_unlock_irqrestore(shost->host_lock, flags);
210}
211
James Smartea2151b2008-09-07 11:52:10 -0400212/**
James Smart3621a712009-04-06 18:47:14 -0400213 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
James Smartea2151b2008-09-07 11:52:10 -0400214 * @phba: Pointer to HBA context object.
215 * @vport: Pointer to vport object.
216 * @ndlp: Pointer to FC node associated with the target.
217 * @lun: Lun number of the scsi device.
218 * @old_val: Old value of the queue depth.
219 * @new_val: New value of the queue depth.
220 *
221 * This function sends an event to the mgmt application indicating
222 * there is a change in the scsi device queue depth.
223 **/
224static void
225lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
226 struct lpfc_vport *vport,
227 struct lpfc_nodelist *ndlp,
228 uint32_t lun,
229 uint32_t old_val,
230 uint32_t new_val)
231{
232 struct lpfc_fast_path_event *fast_path_evt;
233 unsigned long flags;
234
235 fast_path_evt = lpfc_alloc_fast_evt(phba);
236 if (!fast_path_evt)
237 return;
238
239 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
240 FC_REG_SCSI_EVENT;
241 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
242 LPFC_EVENT_VARQUEDEPTH;
243
244 /* Report all luns with change in queue depth */
245 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
246 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
247 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
248 &ndlp->nlp_portname, sizeof(struct lpfc_name));
249 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
250 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
251 }
252
253 fast_path_evt->un.queue_depth_evt.oldval = old_val;
254 fast_path_evt->un.queue_depth_evt.newval = new_val;
255 fast_path_evt->vport = vport;
256
257 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
258 spin_lock_irqsave(&phba->hbalock, flags);
259 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
260 spin_unlock_irqrestore(&phba->hbalock, flags);
261 lpfc_worker_wake_up(phba);
262
263 return;
264}
265
James Smart9bad7672008-12-04 22:39:02 -0500266/**
James Smart5ffc2662009-11-18 15:39:44 -0500267 * lpfc_change_queue_depth - Alter scsi device queue depth
268 * @sdev: Pointer the scsi device on which to change the queue depth.
269 * @qdepth: New queue depth to set the sdev to.
270 * @reason: The reason for the queue depth change.
271 *
272 * This function is called by the midlayer and the LLD to alter the queue
273 * depth for a scsi device. This function sets the queue depth to the new
274 * value and sends an event out to log the queue depth change.
275 **/
276int
277lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
278{
279 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
280 struct lpfc_hba *phba = vport->phba;
281 struct lpfc_rport_data *rdata;
282 unsigned long new_queue_depth, old_queue_depth;
283
284 old_queue_depth = sdev->queue_depth;
285 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
286 new_queue_depth = sdev->queue_depth;
287 rdata = sdev->hostdata;
288 if (rdata)
289 lpfc_send_sdev_queuedepth_change_event(phba, vport,
290 rdata->pnode, sdev->lun,
291 old_queue_depth,
292 new_queue_depth);
293 return sdev->queue_depth;
294}
295
296/**
James Smart3621a712009-04-06 18:47:14 -0400297 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500298 * @phba: The Hba for which this call is being executed.
299 *
300 * This routine is called when there is resource error in driver or firmware.
301 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
302 * posts at most 1 event each second. This routine wakes up worker thread of
303 * @phba to process WORKER_RAM_DOWN_EVENT event.
304 *
305 * This routine should be called with no lock held.
306 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500307void
James Smarteaf15d52008-12-04 22:39:29 -0500308lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500309{
310 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400311 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500312
313 spin_lock_irqsave(&phba->hbalock, flags);
314 atomic_inc(&phba->num_rsrc_err);
315 phba->last_rsrc_error_time = jiffies;
316
317 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
318 spin_unlock_irqrestore(&phba->hbalock, flags);
319 return;
320 }
321
322 phba->last_ramp_down_time = jiffies;
323
324 spin_unlock_irqrestore(&phba->hbalock, flags);
325
326 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400327 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
328 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500329 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500330 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
331
James Smart5e9d9b82008-06-14 22:52:53 -0400332 if (!evt_posted)
333 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500334 return;
335}
336
James Smart9bad7672008-12-04 22:39:02 -0500337/**
James Smart3621a712009-04-06 18:47:14 -0400338 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
James Smart9bad7672008-12-04 22:39:02 -0500339 * @phba: The Hba for which this call is being executed.
340 *
341 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
342 * post at most 1 event every 5 minute after last_ramp_up_time or
343 * last_rsrc_error_time. This routine wakes up worker thread of @phba
344 * to process WORKER_RAM_DOWN_EVENT event.
345 *
346 * This routine should be called with no lock held.
347 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500348static inline void
James Smart3de2a652007-08-02 11:09:59 -0400349lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smarta257bf92009-04-06 18:48:10 -0400350 uint32_t queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500351{
352 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -0400353 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400354 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500355 atomic_inc(&phba->num_cmd_success);
356
James Smarta257bf92009-04-06 18:48:10 -0400357 if (vport->cfg_lun_queue_depth <= queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500358 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500359 spin_lock_irqsave(&phba->hbalock, flags);
James Smart5ffc2662009-11-18 15:39:44 -0500360 if (time_before(jiffies,
361 phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
362 time_before(jiffies,
363 phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500364 spin_unlock_irqrestore(&phba->hbalock, flags);
365 return;
366 }
James Smart92d7f7b2007-06-17 19:56:38 -0500367 phba->last_ramp_up_time = jiffies;
368 spin_unlock_irqrestore(&phba->hbalock, flags);
369
370 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400371 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
372 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500373 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500374 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
375
James Smart5e9d9b82008-06-14 22:52:53 -0400376 if (!evt_posted)
377 lpfc_worker_wake_up(phba);
378 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500379}
380
James Smart9bad7672008-12-04 22:39:02 -0500381/**
James Smart3621a712009-04-06 18:47:14 -0400382 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500383 * @phba: The Hba for which this call is being executed.
384 *
385 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
386 * thread.This routine reduces queue depth for all scsi device on each vport
387 * associated with @phba.
388 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500389void
390lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
391{
James Smart549e55c2007-08-02 11:09:51 -0400392 struct lpfc_vport **vports;
393 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500394 struct scsi_device *sdev;
James Smart5ffc2662009-11-18 15:39:44 -0500395 unsigned long new_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500396 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400397 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500398
399 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
400 num_cmd_success = atomic_read(&phba->num_cmd_success);
401
James Smart75ad83a2012-05-09 21:18:40 -0400402 /*
403 * The error and success command counters are global per
404 * driver instance. If another handler has already
405 * operated on this error event, just exit.
406 */
407 if (num_rsrc_err == 0)
408 return;
409
James Smart549e55c2007-08-02 11:09:51 -0400410 vports = lpfc_create_vport_work_array(phba);
411 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400412 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400413 shost = lpfc_shost_from_vport(vports[i]);
414 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500415 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400416 sdev->queue_depth * num_rsrc_err /
417 (num_rsrc_err + num_cmd_success);
418 if (!new_queue_depth)
419 new_queue_depth = sdev->queue_depth - 1;
420 else
421 new_queue_depth = sdev->queue_depth -
422 new_queue_depth;
James Smart5ffc2662009-11-18 15:39:44 -0500423 lpfc_change_queue_depth(sdev, new_queue_depth,
424 SCSI_QDEPTH_DEFAULT);
James Smart549e55c2007-08-02 11:09:51 -0400425 }
James Smart92d7f7b2007-06-17 19:56:38 -0500426 }
James Smart09372822008-01-11 01:52:54 -0500427 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500428 atomic_set(&phba->num_rsrc_err, 0);
429 atomic_set(&phba->num_cmd_success, 0);
430}
431
James Smart9bad7672008-12-04 22:39:02 -0500432/**
James Smart3621a712009-04-06 18:47:14 -0400433 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500434 * @phba: The Hba for which this call is being executed.
435 *
436 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
437 * thread.This routine increases queue depth for all scsi device on each vport
438 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
439 * num_cmd_success to zero.
440 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500441void
442lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
443{
James Smart549e55c2007-08-02 11:09:51 -0400444 struct lpfc_vport **vports;
445 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500446 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400447 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500448
James Smart549e55c2007-08-02 11:09:51 -0400449 vports = lpfc_create_vport_work_array(phba);
450 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400451 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400452 shost = lpfc_shost_from_vport(vports[i]);
453 shost_for_each_device(sdev, shost) {
James Smart97eab632008-04-07 10:16:05 -0400454 if (vports[i]->cfg_lun_queue_depth <=
455 sdev->queue_depth)
456 continue;
James Smart5ffc2662009-11-18 15:39:44 -0500457 lpfc_change_queue_depth(sdev,
458 sdev->queue_depth+1,
459 SCSI_QDEPTH_RAMP_UP);
James Smart549e55c2007-08-02 11:09:51 -0400460 }
James Smart92d7f7b2007-06-17 19:56:38 -0500461 }
James Smart09372822008-01-11 01:52:54 -0500462 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500463 atomic_set(&phba->num_rsrc_err, 0);
464 atomic_set(&phba->num_cmd_success, 0);
465}
466
James Smarta8e497d2008-08-24 21:50:11 -0400467/**
James Smart3621a712009-04-06 18:47:14 -0400468 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400469 * @phba: Pointer to HBA context object.
470 *
471 * This function walks vport list and set each SCSI host to block state
472 * by invoking fc_remote_port_delete() routine. This function is invoked
473 * with EEH when device's PCI slot has been permanently disabled.
474 **/
475void
476lpfc_scsi_dev_block(struct lpfc_hba *phba)
477{
478 struct lpfc_vport **vports;
479 struct Scsi_Host *shost;
480 struct scsi_device *sdev;
481 struct fc_rport *rport;
482 int i;
483
484 vports = lpfc_create_vport_work_array(phba);
485 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400486 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400487 shost = lpfc_shost_from_vport(vports[i]);
488 shost_for_each_device(sdev, shost) {
489 rport = starget_to_rport(scsi_target(sdev));
490 fc_remote_port_delete(rport);
491 }
492 }
493 lpfc_destroy_vport_work_array(phba, vports);
494}
495
James Smart9bad7672008-12-04 22:39:02 -0500496/**
James Smart3772a992009-05-22 14:50:54 -0400497 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500498 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400499 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500500 *
James Smart3772a992009-05-22 14:50:54 -0400501 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
502 * the scsi buffer contains all the necessary information needed to initiate
503 * a SCSI I/O. The non-DMAable buffer region contains information to build
504 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
505 * and the initial BPL. In addition to allocating memory, the FCP CMND and
506 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500507 *
508 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400509 * int - number of scsi buffers that were allocated.
510 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500511 **/
James Smart3772a992009-05-22 14:50:54 -0400512static int
513lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500514{
James Smart2e0fef82007-06-17 19:56:36 -0500515 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500516 struct lpfc_scsi_buf *psb;
517 struct ulp_bde64 *bpl;
518 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400519 dma_addr_t pdma_phys_fcp_cmd;
520 dma_addr_t pdma_phys_fcp_rsp;
521 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500522 uint16_t iotag;
James Smart3772a992009-05-22 14:50:54 -0400523 int bcnt;
dea31012005-04-17 16:05:31 -0500524
James Smart3772a992009-05-22 14:50:54 -0400525 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
526 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
527 if (!psb)
528 break;
dea31012005-04-17 16:05:31 -0500529
James Smart3772a992009-05-22 14:50:54 -0400530 /*
531 * Get memory from the pci pool to map the virt space to pci
532 * bus space for an I/O. The DMA buffer includes space for the
533 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
534 * necessary to support the sg_tablesize.
535 */
536 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
537 GFP_KERNEL, &psb->dma_handle);
538 if (!psb->data) {
539 kfree(psb);
540 break;
541 }
dea31012005-04-17 16:05:31 -0500542
James Smart3772a992009-05-22 14:50:54 -0400543 /* Initialize virtual ptrs to dma_buf region. */
544 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
dea31012005-04-17 16:05:31 -0500545
James Smart3772a992009-05-22 14:50:54 -0400546 /* Allocate iotag for psb->cur_iocbq. */
547 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
548 if (iotag == 0) {
549 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
550 psb->data, psb->dma_handle);
551 kfree(psb);
552 break;
553 }
554 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500555
James Smart3772a992009-05-22 14:50:54 -0400556 psb->fcp_cmnd = psb->data;
557 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
558 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400559 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500560
James Smart3772a992009-05-22 14:50:54 -0400561 /* Initialize local short-hand pointers. */
562 bpl = psb->fcp_bpl;
563 pdma_phys_fcp_cmd = psb->dma_handle;
564 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
565 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
566 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500567
James Smart3772a992009-05-22 14:50:54 -0400568 /*
569 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
570 * are sg list bdes. Initialize the first two and leave the
571 * rest for queuecommand.
572 */
573 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
574 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
575 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
576 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
577 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500578
James Smart3772a992009-05-22 14:50:54 -0400579 /* Setup the physical region for the FCP RSP */
580 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
581 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
582 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
583 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
584 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
585
586 /*
587 * Since the IOCB for the FCP I/O is built into this
588 * lpfc_scsi_buf, initialize it with all known data now.
589 */
590 iocb = &psb->cur_iocbq.iocb;
591 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
592 if ((phba->sli_rev == 3) &&
593 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
594 /* fill in immediate fcp command BDE */
595 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
596 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
597 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
598 unsli3.fcp_ext.icd);
599 iocb->un.fcpi64.bdl.addrHigh = 0;
600 iocb->ulpBdeCount = 0;
601 iocb->ulpLe = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300602 /* fill in response BDE */
James Smart3772a992009-05-22 14:50:54 -0400603 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
604 BUFF_TYPE_BDE_64;
605 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
606 sizeof(struct fcp_rsp);
607 iocb->unsli3.fcp_ext.rbde.addrLow =
608 putPaddrLow(pdma_phys_fcp_rsp);
609 iocb->unsli3.fcp_ext.rbde.addrHigh =
610 putPaddrHigh(pdma_phys_fcp_rsp);
611 } else {
612 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
613 iocb->un.fcpi64.bdl.bdeSize =
614 (2 * sizeof(struct ulp_bde64));
615 iocb->un.fcpi64.bdl.addrLow =
616 putPaddrLow(pdma_phys_bpl);
617 iocb->un.fcpi64.bdl.addrHigh =
618 putPaddrHigh(pdma_phys_bpl);
619 iocb->ulpBdeCount = 1;
620 iocb->ulpLe = 1;
621 }
622 iocb->ulpClass = CLASS3;
623 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400624 /* Put it back into the SCSI buffer list */
James Smarteee88772010-09-29 11:19:08 -0400625 psb->cur_iocbq.context1 = psb;
James Smart1c6f4ef52009-11-18 15:40:49 -0500626 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400627
James Smart34b02dc2008-08-24 21:49:55 -0400628 }
dea31012005-04-17 16:05:31 -0500629
James Smart3772a992009-05-22 14:50:54 -0400630 return bcnt;
dea31012005-04-17 16:05:31 -0500631}
632
James Smart9bad7672008-12-04 22:39:02 -0500633/**
James Smart1151e3e2011-02-16 12:39:35 -0500634 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
635 * @vport: pointer to lpfc vport data structure.
636 *
637 * This routine is invoked by the vport cleanup for deletions and the cleanup
638 * for an ndlp on removal.
639 **/
640void
641lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
642{
643 struct lpfc_hba *phba = vport->phba;
644 struct lpfc_scsi_buf *psb, *next_psb;
645 unsigned long iflag = 0;
646
647 spin_lock_irqsave(&phba->hbalock, iflag);
648 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
649 list_for_each_entry_safe(psb, next_psb,
650 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
651 if (psb->rdata && psb->rdata->pnode
652 && psb->rdata->pnode->vport == vport)
653 psb->rdata = NULL;
654 }
655 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
656 spin_unlock_irqrestore(&phba->hbalock, iflag);
657}
658
659/**
James Smartda0436e2009-05-22 14:51:39 -0400660 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
661 * @phba: pointer to lpfc hba data structure.
662 * @axri: pointer to the fcp xri abort wcqe structure.
663 *
664 * This routine is invoked by the worker thread to process a SLI4 fast-path
665 * FCP aborted xri.
666 **/
667void
668lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
669 struct sli4_wcqe_xri_aborted *axri)
670{
671 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500672 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartda0436e2009-05-22 14:51:39 -0400673 struct lpfc_scsi_buf *psb, *next_psb;
674 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500675 struct lpfc_iocbq *iocbq;
676 int i;
James Smart19ca7602010-11-20 23:11:55 -0500677 struct lpfc_nodelist *ndlp;
678 int rrq_empty = 0;
James Smart589a52d2010-07-14 15:30:54 -0400679 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smartda0436e2009-05-22 14:51:39 -0400680
James Smart0f65ff62010-02-26 14:14:23 -0500681 spin_lock_irqsave(&phba->hbalock, iflag);
682 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400683 list_for_each_entry_safe(psb, next_psb,
684 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
685 if (psb->cur_iocbq.sli4_xritag == xri) {
686 list_del(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500687 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400688 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500689 spin_unlock(
690 &phba->sli4_hba.abts_scsi_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500691 if (psb->rdata && psb->rdata->pnode)
692 ndlp = psb->rdata->pnode;
693 else
694 ndlp = NULL;
695
James Smart19ca7602010-11-20 23:11:55 -0500696 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500697 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartcb69f7d2011-12-13 13:21:57 -0500698 if (ndlp) {
James Smart19ca7602010-11-20 23:11:55 -0500699 lpfc_set_rrq_active(phba, ndlp, xri, rxid, 1);
James Smartcb69f7d2011-12-13 13:21:57 -0500700 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
701 }
James Smartda0436e2009-05-22 14:51:39 -0400702 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500703 if (rrq_empty)
704 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400705 return;
706 }
707 }
James Smart0f65ff62010-02-26 14:14:23 -0500708 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
709 for (i = 1; i <= phba->sli.last_iotag; i++) {
710 iocbq = phba->sli.iocbq_lookup[i];
711
712 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
713 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
714 continue;
715 if (iocbq->sli4_xritag != xri)
716 continue;
717 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
718 psb->exch_busy = 0;
719 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart589a52d2010-07-14 15:30:54 -0400720 if (pring->txq_cnt)
721 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500722 return;
723
724 }
725 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400726}
727
728/**
James Smart8a9d2e82012-05-09 21:16:12 -0400729 * lpfc_sli4_post_scsi_sgl_list - Psot blocks of scsi buffer sgls from a list
730 * @phba: pointer to lpfc hba data structure.
731 * @post_sblist: pointer to the scsi buffer list.
732 *
733 * This routine walks a list of scsi buffers that was passed in. It attempts
734 * to construct blocks of scsi buffer sgls which contains contiguous xris and
735 * uses the non-embedded SGL block post mailbox commands to post to the port.
736 * For single SCSI buffer sgl with non-contiguous xri, if any, it shall use
737 * embedded SGL post mailbox command for posting. The @post_sblist passed in
738 * must be local list, thus no lock is needed when manipulate the list.
739 *
740 * Returns: 0 = failure, non-zero number of successfully posted buffers.
741 **/
742int
743lpfc_sli4_post_scsi_sgl_list(struct lpfc_hba *phba,
744 struct list_head *post_sblist, int sb_count)
745{
746 struct lpfc_scsi_buf *psb, *psb_next;
747 int status;
748 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
749 dma_addr_t pdma_phys_bpl1;
750 int last_xritag = NO_XRI;
751 LIST_HEAD(prep_sblist);
752 LIST_HEAD(blck_sblist);
753 LIST_HEAD(scsi_sblist);
754
755 /* sanity check */
756 if (sb_count <= 0)
757 return -EINVAL;
758
759 list_for_each_entry_safe(psb, psb_next, post_sblist, list) {
760 list_del_init(&psb->list);
761 block_cnt++;
762 if ((last_xritag != NO_XRI) &&
763 (psb->cur_iocbq.sli4_xritag != last_xritag + 1)) {
764 /* a hole in xri block, form a sgl posting block */
765 list_splice_init(&prep_sblist, &blck_sblist);
766 post_cnt = block_cnt - 1;
767 /* prepare list for next posting block */
768 list_add_tail(&psb->list, &prep_sblist);
769 block_cnt = 1;
770 } else {
771 /* prepare list for next posting block */
772 list_add_tail(&psb->list, &prep_sblist);
773 /* enough sgls for non-embed sgl mbox command */
774 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
775 list_splice_init(&prep_sblist, &blck_sblist);
776 post_cnt = block_cnt;
777 block_cnt = 0;
778 }
779 }
780 num_posting++;
781 last_xritag = psb->cur_iocbq.sli4_xritag;
782
783 /* end of repost sgl list condition for SCSI buffers */
784 if (num_posting == sb_count) {
785 if (post_cnt == 0) {
786 /* last sgl posting block */
787 list_splice_init(&prep_sblist, &blck_sblist);
788 post_cnt = block_cnt;
789 } else if (block_cnt == 1) {
790 /* last single sgl with non-contiguous xri */
791 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
792 pdma_phys_bpl1 = psb->dma_phys_bpl +
793 SGL_PAGE_SIZE;
794 else
795 pdma_phys_bpl1 = 0;
796 status = lpfc_sli4_post_sgl(phba,
797 psb->dma_phys_bpl,
798 pdma_phys_bpl1,
799 psb->cur_iocbq.sli4_xritag);
800 if (status) {
801 /* failure, put on abort scsi list */
802 psb->exch_busy = 1;
803 } else {
804 /* success, put on SCSI buffer list */
805 psb->exch_busy = 0;
806 psb->status = IOSTAT_SUCCESS;
807 num_posted++;
808 }
809 /* success, put on SCSI buffer sgl list */
810 list_add_tail(&psb->list, &scsi_sblist);
811 }
812 }
813
814 /* continue until a nembed page worth of sgls */
815 if (post_cnt == 0)
816 continue;
817
818 /* post block of SCSI buffer list sgls */
819 status = lpfc_sli4_post_scsi_sgl_block(phba, &blck_sblist,
820 post_cnt);
821
822 /* don't reset xirtag due to hole in xri block */
823 if (block_cnt == 0)
824 last_xritag = NO_XRI;
825
826 /* reset SCSI buffer post count for next round of posting */
827 post_cnt = 0;
828
829 /* put posted SCSI buffer-sgl posted on SCSI buffer sgl list */
830 while (!list_empty(&blck_sblist)) {
831 list_remove_head(&blck_sblist, psb,
832 struct lpfc_scsi_buf, list);
833 if (status) {
834 /* failure, put on abort scsi list */
835 psb->exch_busy = 1;
836 } else {
837 /* success, put on SCSI buffer list */
838 psb->exch_busy = 0;
839 psb->status = IOSTAT_SUCCESS;
840 num_posted++;
841 }
842 list_add_tail(&psb->list, &scsi_sblist);
843 }
844 }
845 /* Push SCSI buffers with sgl posted to the availble list */
846 while (!list_empty(&scsi_sblist)) {
847 list_remove_head(&scsi_sblist, psb,
848 struct lpfc_scsi_buf, list);
849 lpfc_release_scsi_buf_s4(phba, psb);
850 }
851 return num_posted;
852}
853
854/**
855 * lpfc_sli4_repost_scsi_sgl_list - Repsot all the allocated scsi buffer sgls
James Smartda0436e2009-05-22 14:51:39 -0400856 * @phba: pointer to lpfc hba data structure.
857 *
858 * This routine walks the list of scsi buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -0400859 * repost them to the port by using SGL block post. This is needed after a
James Smartda0436e2009-05-22 14:51:39 -0400860 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
861 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
862 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
863 *
864 * Returns: 0 = success, non-zero failure.
865 **/
866int
867lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
868{
James Smart8a9d2e82012-05-09 21:16:12 -0400869 LIST_HEAD(post_sblist);
870 int num_posted, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -0400871
James Smart8a9d2e82012-05-09 21:16:12 -0400872 /* get all SCSI buffers need to repost to a local list */
873 spin_lock(&phba->scsi_buf_list_lock);
874 list_splice_init(&phba->lpfc_scsi_buf_list, &post_sblist);
875 spin_unlock(&phba->scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400876
James Smart8a9d2e82012-05-09 21:16:12 -0400877 /* post the list of scsi buffer sgls to port if available */
878 if (!list_empty(&post_sblist)) {
879 num_posted = lpfc_sli4_post_scsi_sgl_list(phba, &post_sblist,
880 phba->sli4_hba.scsi_xri_cnt);
881 /* failed to post any scsi buffer, return error */
882 if (num_posted == 0)
883 rc = -EIO;
James Smartda0436e2009-05-22 14:51:39 -0400884 }
885 return rc;
886}
887
888/**
889 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
890 * @vport: The virtual port for which this call being executed.
891 * @num_to_allocate: The requested number of buffers to allocate.
892 *
James Smart8a9d2e82012-05-09 21:16:12 -0400893 * This routine allocates scsi buffers for device with SLI-4 interface spec,
James Smartda0436e2009-05-22 14:51:39 -0400894 * the scsi buffer contains all the necessary information needed to initiate
James Smart8a9d2e82012-05-09 21:16:12 -0400895 * a SCSI I/O. After allocating up to @num_to_allocate SCSI buffers and put
896 * them on a list, it post them to the port by using SGL block post.
James Smartda0436e2009-05-22 14:51:39 -0400897 *
898 * Return codes:
James Smart8a9d2e82012-05-09 21:16:12 -0400899 * int - number of scsi buffers that were allocated and posted.
James Smartda0436e2009-05-22 14:51:39 -0400900 * 0 = failure, less than num_to_alloc is a partial failure.
901 **/
902static int
903lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
904{
905 struct lpfc_hba *phba = vport->phba;
906 struct lpfc_scsi_buf *psb;
907 struct sli4_sge *sgl;
908 IOCB_t *iocb;
909 dma_addr_t pdma_phys_fcp_cmd;
910 dma_addr_t pdma_phys_fcp_rsp;
911 dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
James Smart8a9d2e82012-05-09 21:16:12 -0400912 uint16_t iotag, lxri = 0;
913 int bcnt, num_posted;
914 LIST_HEAD(prep_sblist);
915 LIST_HEAD(post_sblist);
916 LIST_HEAD(scsi_sblist);
James Smartda0436e2009-05-22 14:51:39 -0400917
918 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
919 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
920 if (!psb)
921 break;
James Smartda0436e2009-05-22 14:51:39 -0400922 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400923 * Get memory from the pci pool to map the virt space to
924 * pci bus space for an I/O. The DMA buffer includes space
925 * for the struct fcp_cmnd, struct fcp_rsp and the number
926 * of bde's necessary to support the sg_tablesize.
James Smartda0436e2009-05-22 14:51:39 -0400927 */
928 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
929 GFP_KERNEL, &psb->dma_handle);
930 if (!psb->data) {
931 kfree(psb);
932 break;
933 }
James Smartda0436e2009-05-22 14:51:39 -0400934 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
935
936 /* Allocate iotag for psb->cur_iocbq. */
937 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
938 if (iotag == 0) {
James Smartb92938b2010-06-07 15:24:12 -0400939 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
940 psb->data, psb->dma_handle);
James Smartda0436e2009-05-22 14:51:39 -0400941 kfree(psb);
942 break;
943 }
944
James Smart6d368e52011-05-24 11:44:12 -0400945 lxri = lpfc_sli4_next_xritag(phba);
946 if (lxri == NO_XRI) {
James Smartda0436e2009-05-22 14:51:39 -0400947 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
948 psb->data, psb->dma_handle);
949 kfree(psb);
950 break;
951 }
James Smart6d368e52011-05-24 11:44:12 -0400952 psb->cur_iocbq.sli4_lxritag = lxri;
953 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smartda0436e2009-05-22 14:51:39 -0400954 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Smartda0436e2009-05-22 14:51:39 -0400955 psb->fcp_bpl = psb->data;
956 psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
957 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
958 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
959 sizeof(struct fcp_cmnd));
960
961 /* Initialize local short-hand pointers. */
962 sgl = (struct sli4_sge *)psb->fcp_bpl;
963 pdma_phys_bpl = psb->dma_handle;
964 pdma_phys_fcp_cmd =
965 (psb->dma_handle + phba->cfg_sg_dma_buf_size)
966 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
967 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
968
969 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400970 * The first two bdes are the FCP_CMD and FCP_RSP.
971 * The balance are sg list bdes. Initialize the
972 * first two and leave the rest for queuecommand.
James Smartda0436e2009-05-22 14:51:39 -0400973 */
974 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
975 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
James Smart05580562011-05-24 11:40:48 -0400976 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400977 bf_set(lpfc_sli4_sge_last, sgl, 0);
978 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500979 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
James Smartda0436e2009-05-22 14:51:39 -0400980 sgl++;
981
982 /* Setup the physical region for the FCP RSP */
983 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
984 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smart05580562011-05-24 11:40:48 -0400985 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400986 bf_set(lpfc_sli4_sge_last, sgl, 1);
987 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500988 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -0400989
990 /*
991 * Since the IOCB for the FCP I/O is built into this
992 * lpfc_scsi_buf, initialize it with all known data now.
993 */
994 iocb = &psb->cur_iocbq.iocb;
995 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
996 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
997 /* setting the BLP size to 2 * sizeof BDE may not be correct.
998 * We are setting the bpl to point to out sgl. An sgl's
999 * entries are 16 bytes, a bpl entries are 12 bytes.
1000 */
1001 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
1002 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
1003 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
1004 iocb->ulpBdeCount = 1;
1005 iocb->ulpLe = 1;
1006 iocb->ulpClass = CLASS3;
James Smart8a9d2e82012-05-09 21:16:12 -04001007 psb->cur_iocbq.context1 = psb;
James Smartda0436e2009-05-22 14:51:39 -04001008 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
1009 pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
1010 else
1011 pdma_phys_bpl1 = 0;
1012 psb->dma_phys_bpl = pdma_phys_bpl;
James Smart6d368e52011-05-24 11:44:12 -04001013
James Smart8a9d2e82012-05-09 21:16:12 -04001014 /* add the scsi buffer to a post list */
1015 list_add_tail(&psb->list, &post_sblist);
1016 spin_lock_irq(&phba->scsi_buf_list_lock);
1017 phba->sli4_hba.scsi_xri_cnt++;
1018 spin_unlock_irq(&phba->scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04001019 }
James Smart8a9d2e82012-05-09 21:16:12 -04001020 lpfc_printf_log(phba, KERN_INFO, LOG_BG,
1021 "3021 Allocate %d out of %d requested new SCSI "
1022 "buffers\n", bcnt, num_to_alloc);
James Smartda0436e2009-05-22 14:51:39 -04001023
James Smart8a9d2e82012-05-09 21:16:12 -04001024 /* post the list of scsi buffer sgls to port if available */
1025 if (!list_empty(&post_sblist))
1026 num_posted = lpfc_sli4_post_scsi_sgl_list(phba,
1027 &post_sblist, bcnt);
1028 else
1029 num_posted = 0;
1030
1031 return num_posted;
James Smartda0436e2009-05-22 14:51:39 -04001032}
1033
1034/**
James Smart3772a992009-05-22 14:50:54 -04001035 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
1036 * @vport: The virtual port for which this call being executed.
1037 * @num_to_allocate: The requested number of buffers to allocate.
1038 *
1039 * This routine wraps the actual SCSI buffer allocator function pointer from
1040 * the lpfc_hba struct.
1041 *
1042 * Return codes:
1043 * int - number of scsi buffers that were allocated.
1044 * 0 = failure, less than num_to_alloc is a partial failure.
1045 **/
1046static inline int
1047lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
1048{
1049 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
1050}
1051
1052/**
James Smart19ca7602010-11-20 23:11:55 -05001053 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -04001054 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05001055 *
1056 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1057 * and returns to caller.
1058 *
1059 * Return codes:
1060 * NULL - Error
1061 * Pointer to lpfc_scsi_buf - Success
1062 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +01001063static struct lpfc_scsi_buf*
James Smart19ca7602010-11-20 23:11:55 -05001064lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001065{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001066 struct lpfc_scsi_buf * lpfc_cmd = NULL;
1067 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001068 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001069
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001070 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001071 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -04001072 if (lpfc_cmd) {
1073 lpfc_cmd->seg_cnt = 0;
1074 lpfc_cmd->nonsg_phys = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001075 lpfc_cmd->prot_seg_cnt = 0;
James Smart1dcb58e2007-04-25 09:51:30 -04001076 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001077 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001078 return lpfc_cmd;
1079}
James Smart19ca7602010-11-20 23:11:55 -05001080/**
1081 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1082 * @phba: The HBA for which this call is being executed.
1083 *
1084 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1085 * and returns to caller.
1086 *
1087 * Return codes:
1088 * NULL - Error
1089 * Pointer to lpfc_scsi_buf - Success
1090 **/
1091static struct lpfc_scsi_buf*
1092lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1093{
James Smart1151e3e2011-02-16 12:39:35 -05001094 struct lpfc_scsi_buf *lpfc_cmd ;
James Smart19ca7602010-11-20 23:11:55 -05001095 unsigned long iflag = 0;
1096 int found = 0;
1097
1098 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -05001099 list_for_each_entry(lpfc_cmd, &phba->lpfc_scsi_buf_list,
1100 list) {
James Smart19ca7602010-11-20 23:11:55 -05001101 if (lpfc_test_rrq_active(phba, ndlp,
James Smart1151e3e2011-02-16 12:39:35 -05001102 lpfc_cmd->cur_iocbq.sli4_xritag))
1103 continue;
1104 list_del(&lpfc_cmd->list);
James Smart19ca7602010-11-20 23:11:55 -05001105 found = 1;
1106 lpfc_cmd->seg_cnt = 0;
1107 lpfc_cmd->nonsg_phys = 0;
1108 lpfc_cmd->prot_seg_cnt = 0;
James Smart1151e3e2011-02-16 12:39:35 -05001109 break;
James Smart19ca7602010-11-20 23:11:55 -05001110 }
James Smart1151e3e2011-02-16 12:39:35 -05001111 spin_unlock_irqrestore(&phba->scsi_buf_list_lock,
1112 iflag);
1113 if (!found)
1114 return NULL;
1115 else
1116 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001117}
1118/**
1119 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1120 * @phba: The HBA for which this call is being executed.
1121 *
1122 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1123 * and returns to caller.
1124 *
1125 * Return codes:
1126 * NULL - Error
1127 * Pointer to lpfc_scsi_buf - Success
1128 **/
1129static struct lpfc_scsi_buf*
1130lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1131{
1132 return phba->lpfc_get_scsi_buf(phba, ndlp);
1133}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001134
James Smart9bad7672008-12-04 22:39:02 -05001135/**
James Smart3772a992009-05-22 14:50:54 -04001136 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -05001137 * @phba: The Hba for which this call is being executed.
1138 * @psb: The scsi buffer which is being released.
1139 *
1140 * This routine releases @psb scsi buffer by adding it to tail of @phba
1141 * lpfc_scsi_buf_list list.
1142 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001143static void
James Smart3772a992009-05-22 14:50:54 -04001144lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001145{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001146 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001147
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001148 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001149 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -05001150 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001151 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -05001152}
1153
James Smart9bad7672008-12-04 22:39:02 -05001154/**
James Smartda0436e2009-05-22 14:51:39 -04001155 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1156 * @phba: The Hba for which this call is being executed.
1157 * @psb: The scsi buffer which is being released.
1158 *
1159 * This routine releases @psb scsi buffer by adding it to tail of @phba
1160 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1161 * and cannot be reused for at least RA_TOV amount of time if it was
1162 * aborted.
1163 **/
1164static void
1165lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1166{
1167 unsigned long iflag = 0;
1168
James Smart341af102010-01-26 23:07:37 -05001169 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -04001170 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1171 iflag);
1172 psb->pCmd = NULL;
1173 list_add_tail(&psb->list,
1174 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1175 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1176 iflag);
1177 } else {
1178
1179 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1180 psb->pCmd = NULL;
1181 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
1182 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
1183 }
1184}
1185
1186/**
James Smart3772a992009-05-22 14:50:54 -04001187 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1188 * @phba: The Hba for which this call is being executed.
1189 * @psb: The scsi buffer which is being released.
1190 *
1191 * This routine releases @psb scsi buffer by adding it to tail of @phba
1192 * lpfc_scsi_buf_list list.
1193 **/
1194static void
1195lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1196{
1197
1198 phba->lpfc_release_scsi_buf(phba, psb);
1199}
1200
1201/**
1202 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001203 * @phba: The Hba for which this call is being executed.
1204 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1205 *
1206 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001207 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1208 * through sg elements and format the bdea. This routine also initializes all
1209 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001210 *
1211 * Return codes:
1212 * 1 - Error
1213 * 0 - Success
1214 **/
dea31012005-04-17 16:05:31 -05001215static int
James Smart3772a992009-05-22 14:50:54 -04001216lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001217{
1218 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1219 struct scatterlist *sgel = NULL;
1220 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1221 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001222 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001223 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001224 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001225 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001226 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001227 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001228
1229 /*
1230 * There are three possibilities here - use scatter-gather segment, use
1231 * the single mapping, or neither. Start the lpfc command prep by
1232 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1233 * data bde entry.
1234 */
1235 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001236 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001237 /*
1238 * The driver stores the segment count returned from pci_map_sg
1239 * because this a count of dma-mappings used to map the use_sg
1240 * pages. They are not guaranteed to be the same for those
1241 * architectures that implement an IOMMU.
1242 */
dea31012005-04-17 16:05:31 -05001243
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001244 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1245 scsi_sg_count(scsi_cmnd), datadir);
1246 if (unlikely(!nseg))
1247 return 1;
1248
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001249 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001250 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001251 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1252 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001253 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001254 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001255 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001256 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001257 return 1;
1258 }
1259
1260 /*
1261 * The driver established a maximum scatter-gather segment count
1262 * during probe that limits the number of sg elements in any
1263 * single scsi command. Just run through the seg_cnt and format
1264 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001265 * When using SLI-3 the driver will try to fit all the BDEs into
1266 * the IOCB. If it can't then the BDEs get added to a BPL as it
1267 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001268 */
James Smart34b02dc2008-08-24 21:49:55 -04001269 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001270 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001271 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001272 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001273 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001274 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1275 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1276 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1277 data_bde->addrLow = putPaddrLow(physaddr);
1278 data_bde->addrHigh = putPaddrHigh(physaddr);
1279 data_bde++;
1280 } else {
1281 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1282 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1283 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1284 bpl->addrLow =
1285 le32_to_cpu(putPaddrLow(physaddr));
1286 bpl->addrHigh =
1287 le32_to_cpu(putPaddrHigh(physaddr));
1288 bpl++;
1289 }
dea31012005-04-17 16:05:31 -05001290 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001291 }
dea31012005-04-17 16:05:31 -05001292
1293 /*
1294 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001295 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1296 * explicitly reinitialized and for SLI-3 the extended bde count is
1297 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001298 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001299 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001300 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1301 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001302 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1303 /*
1304 * The extended IOCB format can only fit 3 BDE or a BPL.
1305 * This I/O has more than 3 BDE so the 1st data bde will
1306 * be a BPL that is filled in here.
1307 */
1308 physaddr = lpfc_cmd->dma_handle;
1309 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1310 data_bde->tus.f.bdeSize = (num_bde *
1311 sizeof(struct ulp_bde64));
1312 physaddr += (sizeof(struct fcp_cmnd) +
1313 sizeof(struct fcp_rsp) +
1314 (2 * sizeof(struct ulp_bde64)));
1315 data_bde->addrHigh = putPaddrHigh(physaddr);
1316 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001317 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -04001318 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1319 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001320 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -04001321 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1322 }
1323 } else {
1324 iocb_cmd->un.fcpi64.bdl.bdeSize =
1325 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001326 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001327 }
James Smart09372822008-01-11 01:52:54 -05001328 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001329
1330 /*
1331 * Due to difference in data length between DIF/non-DIF paths,
1332 * we need to set word 4 of IOCB here
1333 */
James Smarta257bf92009-04-06 18:48:10 -04001334 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001335 return 0;
1336}
1337
James Smartf9bb2da2011-10-10 21:34:11 -04001338static inline unsigned
1339lpfc_cmd_blksize(struct scsi_cmnd *sc)
1340{
1341 return sc->device->sector_size;
1342}
1343
1344#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001345
James Smart9a6b09c2012-03-01 22:37:42 -05001346/* Return if if error injection is detected by Initiator */
1347#define BG_ERR_INIT 0x1
1348/* Return if if error injection is detected by Target */
1349#define BG_ERR_TGT 0x2
1350/* Return if if swapping CSUM<-->CRC is required for error injection */
1351#define BG_ERR_SWAP 0x10
1352/* Return if disabling Guard/Ref/App checking is required for error injection */
1353#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -05001354
1355/**
1356 * lpfc_bg_err_inject - Determine if we should inject an error
1357 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -04001358 * @sc: The SCSI command to examine
1359 * @reftag: (out) BlockGuard reference tag for transmitted data
1360 * @apptag: (out) BlockGuard application tag for transmitted data
1361 * @new_guard (in) Value to replace CRC with if needed
1362 *
James Smart9a6b09c2012-03-01 22:37:42 -05001363 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -05001364 **/
James Smartf9bb2da2011-10-10 21:34:11 -04001365static int
1366lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1367 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1368{
1369 struct scatterlist *sgpe; /* s/g prot entry */
1370 struct scatterlist *sgde; /* s/g data entry */
James Smart9a6b09c2012-03-01 22:37:42 -05001371 struct lpfc_scsi_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -05001372 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -05001373 struct lpfc_nodelist *ndlp;
1374 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -04001375 uint32_t op = scsi_get_prot_op(sc);
1376 uint32_t blksize;
1377 uint32_t numblks;
1378 sector_t lba;
1379 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001380 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001381
1382 if (op == SCSI_PROT_NORMAL)
1383 return 0;
1384
James Smartacd68592012-01-18 16:25:09 -05001385 sgpe = scsi_prot_sglist(sc);
1386 sgde = scsi_sglist(sc);
James Smartf9bb2da2011-10-10 21:34:11 -04001387 lba = scsi_get_lba(sc);
James Smart4ac9b222012-03-01 22:38:29 -05001388
1389 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001390 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1391 blksize = lpfc_cmd_blksize(sc);
1392 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1393
1394 /* Make sure we have the right LBA if one is specified */
1395 if ((phba->lpfc_injerr_lba < lba) ||
1396 (phba->lpfc_injerr_lba >= (lba + numblks)))
1397 return 0;
James Smartacd68592012-01-18 16:25:09 -05001398 if (sgpe) {
1399 blockoff = phba->lpfc_injerr_lba - lba;
1400 numblks = sg_dma_len(sgpe) /
1401 sizeof(struct scsi_dif_tuple);
1402 if (numblks < blockoff)
1403 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001404 }
James Smartf9bb2da2011-10-10 21:34:11 -04001405 }
1406
James Smart4ac9b222012-03-01 22:38:29 -05001407 /* Next check if we need to match the remote NPortID or WWPN */
1408 rdata = sc->device->hostdata;
1409 if (rdata && rdata->pnode) {
1410 ndlp = rdata->pnode;
1411
1412 /* Make sure we have the right NPortID if one is specified */
1413 if (phba->lpfc_injerr_nportid &&
1414 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1415 return 0;
1416
1417 /*
1418 * Make sure we have the right WWPN if one is specified.
1419 * wwn[0] should be a non-zero NAA in a good WWPN.
1420 */
1421 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1422 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1423 sizeof(struct lpfc_name)) != 0))
1424 return 0;
1425 }
1426
1427 /* Setup a ptr to the protection data if the SCSI host provides it */
1428 if (sgpe) {
1429 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1430 src += blockoff;
1431 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1432 }
1433
James Smartf9bb2da2011-10-10 21:34:11 -04001434 /* Should we change the Reference Tag */
1435 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001436 if (phba->lpfc_injerr_wref_cnt) {
1437 switch (op) {
1438 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001439 if (src) {
1440 /*
1441 * For WRITE_PASS, force the error
1442 * to be sent on the wire. It should
1443 * be detected by the Target.
1444 * If blockoff != 0 error will be
1445 * inserted in middle of the IO.
1446 */
James Smartf9bb2da2011-10-10 21:34:11 -04001447
James Smartacd68592012-01-18 16:25:09 -05001448 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1449 "9076 BLKGRD: Injecting reftag error: "
1450 "write lba x%lx + x%x oldrefTag x%x\n",
1451 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001452 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001453
1454 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001455 * Save the old ref_tag so we can
1456 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001457 */
James Smart9a6b09c2012-03-01 22:37:42 -05001458 if (lpfc_cmd) {
1459 lpfc_cmd->prot_data_type =
1460 LPFC_INJERR_REFTAG;
1461 lpfc_cmd->prot_data_segment =
1462 src;
1463 lpfc_cmd->prot_data =
1464 src->ref_tag;
1465 }
1466 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001467 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001468 if (phba->lpfc_injerr_wref_cnt == 0) {
1469 phba->lpfc_injerr_nportid = 0;
1470 phba->lpfc_injerr_lba =
1471 LPFC_INJERR_LBA_OFF;
1472 memset(&phba->lpfc_injerr_wwpn,
1473 0, sizeof(struct lpfc_name));
1474 }
James Smart9a6b09c2012-03-01 22:37:42 -05001475 rc = BG_ERR_TGT | BG_ERR_CHECK;
1476
James Smartacd68592012-01-18 16:25:09 -05001477 break;
1478 }
1479 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001480 case SCSI_PROT_WRITE_INSERT:
1481 /*
1482 * For WRITE_INSERT, force the error
1483 * to be sent on the wire. It should be
1484 * detected by the Target.
1485 */
1486 /* DEADBEEF will be the reftag on the wire */
1487 *reftag = 0xDEADBEEF;
1488 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001489 if (phba->lpfc_injerr_wref_cnt == 0) {
1490 phba->lpfc_injerr_nportid = 0;
1491 phba->lpfc_injerr_lba =
1492 LPFC_INJERR_LBA_OFF;
1493 memset(&phba->lpfc_injerr_wwpn,
1494 0, sizeof(struct lpfc_name));
1495 }
James Smart9a6b09c2012-03-01 22:37:42 -05001496 rc = BG_ERR_TGT | BG_ERR_CHECK;
1497
1498 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1499 "9078 BLKGRD: Injecting reftag error: "
1500 "write lba x%lx\n", (unsigned long)lba);
1501 break;
James Smartacd68592012-01-18 16:25:09 -05001502 case SCSI_PROT_WRITE_STRIP:
1503 /*
1504 * For WRITE_STRIP and WRITE_PASS,
1505 * force the error on data
1506 * being copied from SLI-Host to SLI-Port.
1507 */
1508 *reftag = 0xDEADBEEF;
1509 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001510 if (phba->lpfc_injerr_wref_cnt == 0) {
1511 phba->lpfc_injerr_nportid = 0;
1512 phba->lpfc_injerr_lba =
1513 LPFC_INJERR_LBA_OFF;
1514 memset(&phba->lpfc_injerr_wwpn,
1515 0, sizeof(struct lpfc_name));
1516 }
James Smartacd68592012-01-18 16:25:09 -05001517 rc = BG_ERR_INIT;
1518
1519 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1520 "9077 BLKGRD: Injecting reftag error: "
1521 "write lba x%lx\n", (unsigned long)lba);
1522 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001523 }
James Smartacd68592012-01-18 16:25:09 -05001524 }
1525 if (phba->lpfc_injerr_rref_cnt) {
1526 switch (op) {
1527 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001528 case SCSI_PROT_READ_STRIP:
1529 case SCSI_PROT_READ_PASS:
1530 /*
1531 * For READ_STRIP and READ_PASS, force the
1532 * error on data being read off the wire. It
1533 * should force an IO error to the driver.
1534 */
James Smartf9bb2da2011-10-10 21:34:11 -04001535 *reftag = 0xDEADBEEF;
1536 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001537 if (phba->lpfc_injerr_rref_cnt == 0) {
1538 phba->lpfc_injerr_nportid = 0;
1539 phba->lpfc_injerr_lba =
1540 LPFC_INJERR_LBA_OFF;
1541 memset(&phba->lpfc_injerr_wwpn,
1542 0, sizeof(struct lpfc_name));
1543 }
James Smartacd68592012-01-18 16:25:09 -05001544 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001545
1546 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001547 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001548 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001549 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001550 }
1551 }
1552 }
1553
1554 /* Should we change the Application Tag */
1555 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001556 if (phba->lpfc_injerr_wapp_cnt) {
1557 switch (op) {
1558 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001559 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001560 /*
1561 * For WRITE_PASS, force the error
1562 * to be sent on the wire. It should
1563 * be detected by the Target.
1564 * If blockoff != 0 error will be
1565 * inserted in middle of the IO.
1566 */
1567
James Smartacd68592012-01-18 16:25:09 -05001568 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1569 "9080 BLKGRD: Injecting apptag error: "
1570 "write lba x%lx + x%x oldappTag x%x\n",
1571 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001572 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001573
1574 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001575 * Save the old app_tag so we can
1576 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001577 */
James Smart9a6b09c2012-03-01 22:37:42 -05001578 if (lpfc_cmd) {
1579 lpfc_cmd->prot_data_type =
1580 LPFC_INJERR_APPTAG;
1581 lpfc_cmd->prot_data_segment =
1582 src;
1583 lpfc_cmd->prot_data =
1584 src->app_tag;
1585 }
1586 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001587 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001588 if (phba->lpfc_injerr_wapp_cnt == 0) {
1589 phba->lpfc_injerr_nportid = 0;
1590 phba->lpfc_injerr_lba =
1591 LPFC_INJERR_LBA_OFF;
1592 memset(&phba->lpfc_injerr_wwpn,
1593 0, sizeof(struct lpfc_name));
1594 }
James Smart9a6b09c2012-03-01 22:37:42 -05001595 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001596 break;
1597 }
1598 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001599 case SCSI_PROT_WRITE_INSERT:
1600 /*
1601 * For WRITE_INSERT, force the
1602 * error to be sent on the wire. It should be
1603 * detected by the Target.
1604 */
1605 /* DEAD will be the apptag on the wire */
1606 *apptag = 0xDEAD;
1607 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001608 if (phba->lpfc_injerr_wapp_cnt == 0) {
1609 phba->lpfc_injerr_nportid = 0;
1610 phba->lpfc_injerr_lba =
1611 LPFC_INJERR_LBA_OFF;
1612 memset(&phba->lpfc_injerr_wwpn,
1613 0, sizeof(struct lpfc_name));
1614 }
James Smart9a6b09c2012-03-01 22:37:42 -05001615 rc = BG_ERR_TGT | BG_ERR_CHECK;
1616
1617 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1618 "0813 BLKGRD: Injecting apptag error: "
1619 "write lba x%lx\n", (unsigned long)lba);
1620 break;
James Smartacd68592012-01-18 16:25:09 -05001621 case SCSI_PROT_WRITE_STRIP:
1622 /*
1623 * For WRITE_STRIP and WRITE_PASS,
1624 * force the error on data
1625 * being copied from SLI-Host to SLI-Port.
1626 */
1627 *apptag = 0xDEAD;
1628 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001629 if (phba->lpfc_injerr_wapp_cnt == 0) {
1630 phba->lpfc_injerr_nportid = 0;
1631 phba->lpfc_injerr_lba =
1632 LPFC_INJERR_LBA_OFF;
1633 memset(&phba->lpfc_injerr_wwpn,
1634 0, sizeof(struct lpfc_name));
1635 }
James Smartacd68592012-01-18 16:25:09 -05001636 rc = BG_ERR_INIT;
1637
1638 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1639 "0812 BLKGRD: Injecting apptag error: "
1640 "write lba x%lx\n", (unsigned long)lba);
1641 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001642 }
James Smartacd68592012-01-18 16:25:09 -05001643 }
1644 if (phba->lpfc_injerr_rapp_cnt) {
1645 switch (op) {
1646 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001647 case SCSI_PROT_READ_STRIP:
1648 case SCSI_PROT_READ_PASS:
1649 /*
1650 * For READ_STRIP and READ_PASS, force the
1651 * error on data being read off the wire. It
1652 * should force an IO error to the driver.
1653 */
James Smartf9bb2da2011-10-10 21:34:11 -04001654 *apptag = 0xDEAD;
1655 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001656 if (phba->lpfc_injerr_rapp_cnt == 0) {
1657 phba->lpfc_injerr_nportid = 0;
1658 phba->lpfc_injerr_lba =
1659 LPFC_INJERR_LBA_OFF;
1660 memset(&phba->lpfc_injerr_wwpn,
1661 0, sizeof(struct lpfc_name));
1662 }
James Smartacd68592012-01-18 16:25:09 -05001663 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001664
1665 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001666 "0814 BLKGRD: Injecting apptag error: "
1667 "read lba x%lx\n", (unsigned long)lba);
1668 break;
1669 }
1670 }
1671 }
1672
1673
1674 /* Should we change the Guard Tag */
1675 if (new_guard) {
1676 if (phba->lpfc_injerr_wgrd_cnt) {
1677 switch (op) {
1678 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001679 rc = BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001680 /* Drop thru */
James Smartacd68592012-01-18 16:25:09 -05001681
James Smartacd68592012-01-18 16:25:09 -05001682 case SCSI_PROT_WRITE_INSERT:
1683 /*
1684 * For WRITE_INSERT, force the
1685 * error to be sent on the wire. It should be
1686 * detected by the Target.
1687 */
1688 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001689 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1690 phba->lpfc_injerr_nportid = 0;
1691 phba->lpfc_injerr_lba =
1692 LPFC_INJERR_LBA_OFF;
1693 memset(&phba->lpfc_injerr_wwpn,
1694 0, sizeof(struct lpfc_name));
1695 }
James Smartacd68592012-01-18 16:25:09 -05001696
James Smart9a6b09c2012-03-01 22:37:42 -05001697 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001698 /* Signals the caller to swap CRC->CSUM */
1699
1700 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1701 "0817 BLKGRD: Injecting guard error: "
1702 "write lba x%lx\n", (unsigned long)lba);
1703 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001704 case SCSI_PROT_WRITE_STRIP:
1705 /*
1706 * For WRITE_STRIP and WRITE_PASS,
1707 * force the error on data
1708 * being copied from SLI-Host to SLI-Port.
1709 */
1710 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001711 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1712 phba->lpfc_injerr_nportid = 0;
1713 phba->lpfc_injerr_lba =
1714 LPFC_INJERR_LBA_OFF;
1715 memset(&phba->lpfc_injerr_wwpn,
1716 0, sizeof(struct lpfc_name));
1717 }
James Smart9a6b09c2012-03-01 22:37:42 -05001718
1719 rc = BG_ERR_INIT | BG_ERR_SWAP;
1720 /* Signals the caller to swap CRC->CSUM */
1721
1722 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1723 "0816 BLKGRD: Injecting guard error: "
1724 "write lba x%lx\n", (unsigned long)lba);
1725 break;
James Smartacd68592012-01-18 16:25:09 -05001726 }
1727 }
1728 if (phba->lpfc_injerr_rgrd_cnt) {
1729 switch (op) {
1730 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001731 case SCSI_PROT_READ_STRIP:
1732 case SCSI_PROT_READ_PASS:
1733 /*
1734 * For READ_STRIP and READ_PASS, force the
1735 * error on data being read off the wire. It
1736 * should force an IO error to the driver.
1737 */
James Smartacd68592012-01-18 16:25:09 -05001738 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001739 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1740 phba->lpfc_injerr_nportid = 0;
1741 phba->lpfc_injerr_lba =
1742 LPFC_INJERR_LBA_OFF;
1743 memset(&phba->lpfc_injerr_wwpn,
1744 0, sizeof(struct lpfc_name));
1745 }
James Smartacd68592012-01-18 16:25:09 -05001746
James Smart9a6b09c2012-03-01 22:37:42 -05001747 rc = BG_ERR_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001748 /* Signals the caller to swap CRC->CSUM */
1749
1750 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1751 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001752 "read lba x%lx\n", (unsigned long)lba);
1753 }
1754 }
1755 }
1756
James Smartf9bb2da2011-10-10 21:34:11 -04001757 return rc;
1758}
1759#endif
1760
James Smartacd68592012-01-18 16:25:09 -05001761/**
1762 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1763 * the specified SCSI command.
1764 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001765 * @sc: The SCSI command to examine
1766 * @txopt: (out) BlockGuard operation for transmitted data
1767 * @rxopt: (out) BlockGuard operation for received data
1768 *
1769 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1770 *
James Smartacd68592012-01-18 16:25:09 -05001771 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001772static int
James Smart6c8eea52010-04-06 14:49:53 -04001773lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1774 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001775{
1776 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
James Smart6c8eea52010-04-06 14:49:53 -04001777 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001778
1779 if (guard_type == SHOST_DIX_GUARD_IP) {
1780 switch (scsi_get_prot_op(sc)) {
1781 case SCSI_PROT_READ_INSERT:
1782 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001783 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001784 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001785 break;
1786
1787 case SCSI_PROT_READ_STRIP:
1788 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001789 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001790 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001791 break;
1792
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001793 case SCSI_PROT_READ_PASS:
1794 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001795 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001796 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001797 break;
1798
James Smarte2a0a9d2008-12-04 22:40:02 -05001799 case SCSI_PROT_NORMAL:
1800 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001801 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001802 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1803 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001804 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001805 break;
1806
1807 }
James Smart7c56b9f2011-07-22 18:36:25 -04001808 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001809 switch (scsi_get_prot_op(sc)) {
1810 case SCSI_PROT_READ_STRIP:
1811 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001812 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001813 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001814 break;
1815
1816 case SCSI_PROT_READ_PASS:
1817 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001818 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001819 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001820 break;
1821
James Smarte2a0a9d2008-12-04 22:40:02 -05001822 case SCSI_PROT_READ_INSERT:
1823 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001824 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001825 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001826 break;
1827
James Smarte2a0a9d2008-12-04 22:40:02 -05001828 case SCSI_PROT_NORMAL:
1829 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001830 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001831 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1832 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001833 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001834 break;
1835 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001836 }
1837
James Smart6c8eea52010-04-06 14:49:53 -04001838 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001839}
1840
James Smartacd68592012-01-18 16:25:09 -05001841#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1842/**
1843 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1844 * the specified SCSI command in order to force a guard tag error.
1845 * @phba: The Hba for which this call is being executed.
1846 * @sc: The SCSI command to examine
1847 * @txopt: (out) BlockGuard operation for transmitted data
1848 * @rxopt: (out) BlockGuard operation for received data
1849 *
1850 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1851 *
1852 **/
1853static int
1854lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1855 uint8_t *txop, uint8_t *rxop)
1856{
1857 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
1858 uint8_t ret = 0;
1859
1860 if (guard_type == SHOST_DIX_GUARD_IP) {
1861 switch (scsi_get_prot_op(sc)) {
1862 case SCSI_PROT_READ_INSERT:
1863 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001864 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001865 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001866 break;
1867
1868 case SCSI_PROT_READ_STRIP:
1869 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001870 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001871 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001872 break;
1873
1874 case SCSI_PROT_READ_PASS:
1875 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001876 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001877 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001878 break;
1879
1880 case SCSI_PROT_NORMAL:
1881 default:
1882 break;
1883
1884 }
1885 } else {
1886 switch (scsi_get_prot_op(sc)) {
1887 case SCSI_PROT_READ_STRIP:
1888 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001889 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001890 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001891 break;
1892
1893 case SCSI_PROT_READ_PASS:
1894 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001895 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05001896 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001897 break;
1898
1899 case SCSI_PROT_READ_INSERT:
1900 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001901 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001902 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001903 break;
1904
1905 case SCSI_PROT_NORMAL:
1906 default:
1907 break;
1908 }
1909 }
1910
1911 return ret;
1912}
1913#endif
1914
1915/**
1916 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1917 * @phba: The Hba for which this call is being executed.
1918 * @sc: pointer to scsi command we're working on
1919 * @bpl: pointer to buffer list for protection groups
1920 * @datacnt: number of segments of data that have been dma mapped
1921 *
1922 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05001923 * type LPFC_PG_TYPE_NO_DIF
1924 *
1925 * This is usually used when the HBA is instructed to generate
1926 * DIFs and insert them into data stream (or strip DIF from
1927 * incoming data stream)
1928 *
1929 * The buffer list consists of just one protection group described
1930 * below:
1931 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001932 * start of prot group --> | PDE_5 |
1933 * +-------------------------+
1934 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001935 * +-------------------------+
1936 * | Data BDE |
1937 * +-------------------------+
1938 * |more Data BDE's ... (opt)|
1939 * +-------------------------+
1940 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001941 *
1942 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05001943 *
1944 * Returns the number of BDEs added to the BPL.
1945 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001946static int
1947lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1948 struct ulp_bde64 *bpl, int datasegcnt)
1949{
1950 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001951 struct lpfc_pde5 *pde5 = NULL;
1952 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001953 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001954 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001955 int datadir = sc->sc_data_direction;
James Smart0829a192012-05-09 21:18:12 -04001956#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001957 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001958#endif
James Smartacd68592012-01-18 16:25:09 -05001959 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001960 uint32_t reftag;
James Smart7c56b9f2011-07-22 18:36:25 -04001961 unsigned blksize;
James Smart6c8eea52010-04-06 14:49:53 -04001962 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001963
James Smart6c8eea52010-04-06 14:49:53 -04001964 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1965 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001966 goto out;
1967
James Smart6c8eea52010-04-06 14:49:53 -04001968 /* extract some info from the scsi command for pde*/
James Smarte2a0a9d2008-12-04 22:40:02 -05001969 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05001970 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05001971
James Smartf9bb2da2011-10-10 21:34:11 -04001972#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001973 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001974 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001975 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001976 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001977 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001978 checking = 0;
1979 }
James Smartf9bb2da2011-10-10 21:34:11 -04001980#endif
1981
James Smart6c8eea52010-04-06 14:49:53 -04001982 /* setup PDE5 with what we have */
1983 pde5 = (struct lpfc_pde5 *) bpl;
1984 memset(pde5, 0, sizeof(struct lpfc_pde5));
1985 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001986
James Smartbc739052010-08-04 16:11:18 -04001987 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001988 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001989 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001990
James Smart6c8eea52010-04-06 14:49:53 -04001991 /* advance bpl and increment bde count */
1992 num_bde++;
1993 bpl++;
1994 pde6 = (struct lpfc_pde6 *) bpl;
1995
1996 /* setup PDE6 with the rest of the info */
1997 memset(pde6, 0, sizeof(struct lpfc_pde6));
1998 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1999 bf_set(pde6_optx, pde6, txop);
2000 bf_set(pde6_oprx, pde6, rxop);
2001 if (datadir == DMA_FROM_DEVICE) {
James Smartacd68592012-01-18 16:25:09 -05002002 bf_set(pde6_ce, pde6, checking);
2003 bf_set(pde6_re, pde6, checking);
James Smart6c8eea52010-04-06 14:49:53 -04002004 }
2005 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002006 bf_set(pde6_ae, pde6, 0);
2007 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002008
James Smartbc739052010-08-04 16:11:18 -04002009 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002010 pde6->word0 = cpu_to_le32(pde6->word0);
2011 pde6->word1 = cpu_to_le32(pde6->word1);
2012 pde6->word2 = cpu_to_le32(pde6->word2);
2013
James Smart6c8eea52010-04-06 14:49:53 -04002014 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002015 num_bde++;
2016 bpl++;
2017
2018 /* assumption: caller has already run dma_map_sg on command data */
2019 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2020 physaddr = sg_dma_address(sgde);
2021 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
2022 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2023 bpl->tus.f.bdeSize = sg_dma_len(sgde);
2024 if (datadir == DMA_TO_DEVICE)
2025 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2026 else
2027 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2028 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2029 bpl++;
2030 num_bde++;
2031 }
2032
2033out:
2034 return num_bde;
2035}
2036
James Smartacd68592012-01-18 16:25:09 -05002037/**
2038 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
2039 * @phba: The Hba for which this call is being executed.
2040 * @sc: pointer to scsi command we're working on
2041 * @bpl: pointer to buffer list for protection groups
2042 * @datacnt: number of segments of data that have been dma mapped
2043 * @protcnt: number of segment of protection data that have been dma mapped
2044 *
2045 * This function sets up BPL buffer list for protection groups of
2046 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05002047 *
2048 * This is usually used when DIFs are in their own buffers,
2049 * separate from the data. The HBA can then by instructed
2050 * to place the DIFs in the outgoing stream. For read operations,
2051 * The HBA could extract the DIFs and place it in DIF buffers.
2052 *
2053 * The buffer list for this type consists of one or more of the
2054 * protection groups described below:
2055 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002056 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002057 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002058 * | PDE_6 |
2059 * +-------------------------+
2060 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05002061 * +-------------------------+
2062 * | Data BDE |
2063 * +-------------------------+
2064 * |more Data BDE's ... (opt)|
2065 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002066 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002067 * +-------------------------+
2068 * | ... |
2069 * +-------------------------+
2070 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002071 * Note: It is assumed that both data and protection s/g buffers have been
2072 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05002073 *
2074 * Returns the number of BDEs added to the BPL.
2075 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002076static int
2077lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2078 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2079{
2080 struct scatterlist *sgde = NULL; /* s/g data entry */
2081 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04002082 struct lpfc_pde5 *pde5 = NULL;
2083 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05002084 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002085 dma_addr_t dataphysaddr, protphysaddr;
2086 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05002087 unsigned int split_offset;
2088 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05002089 unsigned int protgrp_blks, protgrp_bytes;
2090 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04002091 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002092 int datadir = sc->sc_data_direction;
2093 unsigned char pgdone = 0, alldone = 0;
2094 unsigned blksize;
James Smart0829a192012-05-09 21:18:12 -04002095#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002096 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002097#endif
James Smartacd68592012-01-18 16:25:09 -05002098 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002099 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04002100 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002101 int num_bde = 0;
2102
2103 sgpe = scsi_prot_sglist(sc);
2104 sgde = scsi_sglist(sc);
2105
2106 if (!sgpe || !sgde) {
2107 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2108 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2109 sgpe, sgde);
2110 return 0;
2111 }
2112
James Smart6c8eea52010-04-06 14:49:53 -04002113 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2114 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002115 goto out;
2116
James Smart6c8eea52010-04-06 14:49:53 -04002117 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05002118 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002119 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002120
James Smartf9bb2da2011-10-10 21:34:11 -04002121#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002122 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002123 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002124 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002125 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002126 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002127 checking = 0;
2128 }
James Smartf9bb2da2011-10-10 21:34:11 -04002129#endif
2130
James Smarte2a0a9d2008-12-04 22:40:02 -05002131 split_offset = 0;
2132 do {
James Smart6c8eea52010-04-06 14:49:53 -04002133 /* setup PDE5 with what we have */
2134 pde5 = (struct lpfc_pde5 *) bpl;
2135 memset(pde5, 0, sizeof(struct lpfc_pde5));
2136 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002137
James Smartbc739052010-08-04 16:11:18 -04002138 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002139 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002140 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002141
James Smart6c8eea52010-04-06 14:49:53 -04002142 /* advance bpl and increment bde count */
2143 num_bde++;
2144 bpl++;
2145 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002146
James Smart6c8eea52010-04-06 14:49:53 -04002147 /* setup PDE6 with the rest of the info */
2148 memset(pde6, 0, sizeof(struct lpfc_pde6));
2149 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2150 bf_set(pde6_optx, pde6, txop);
2151 bf_set(pde6_oprx, pde6, rxop);
James Smartacd68592012-01-18 16:25:09 -05002152 bf_set(pde6_ce, pde6, checking);
2153 bf_set(pde6_re, pde6, checking);
James Smart6c8eea52010-04-06 14:49:53 -04002154 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002155 bf_set(pde6_ae, pde6, 0);
2156 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002157
James Smartbc739052010-08-04 16:11:18 -04002158 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002159 pde6->word0 = cpu_to_le32(pde6->word0);
2160 pde6->word1 = cpu_to_le32(pde6->word1);
2161 pde6->word2 = cpu_to_le32(pde6->word2);
2162
James Smart6c8eea52010-04-06 14:49:53 -04002163 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002164 num_bde++;
2165 bpl++;
2166
2167 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05002168 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2169 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05002170
James Smarte2a0a9d2008-12-04 22:40:02 -05002171 /* must be integer multiple of the DIF block length */
2172 BUG_ON(protgroup_len % 8);
2173
James Smart7f860592011-03-11 16:05:52 -05002174 pde7 = (struct lpfc_pde7 *) bpl;
2175 memset(pde7, 0, sizeof(struct lpfc_pde7));
2176 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2177
James Smart7c56b9f2011-07-22 18:36:25 -04002178 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2179 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05002180
James Smarte2a0a9d2008-12-04 22:40:02 -05002181 protgrp_blks = protgroup_len / 8;
2182 protgrp_bytes = protgrp_blks * blksize;
2183
James Smart7f860592011-03-11 16:05:52 -05002184 /* check if this pde is crossing the 4K boundary; if so split */
2185 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2186 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2187 protgroup_offset += protgroup_remainder;
2188 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04002189 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05002190 } else {
2191 protgroup_offset = 0;
2192 curr_prot++;
2193 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002194
James Smarte2a0a9d2008-12-04 22:40:02 -05002195 num_bde++;
2196
2197 /* setup BDE's for data blocks associated with DIF data */
2198 pgdone = 0;
2199 subtotal = 0; /* total bytes processed for current prot grp */
2200 while (!pgdone) {
2201 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04002202 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2203 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002204 __func__);
2205 return 0;
2206 }
2207 bpl++;
2208 dataphysaddr = sg_dma_address(sgde) + split_offset;
2209 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2210 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2211
2212 remainder = sg_dma_len(sgde) - split_offset;
2213
2214 if ((subtotal + remainder) <= protgrp_bytes) {
2215 /* we can use this whole buffer */
2216 bpl->tus.f.bdeSize = remainder;
2217 split_offset = 0;
2218
2219 if ((subtotal + remainder) == protgrp_bytes)
2220 pgdone = 1;
2221 } else {
2222 /* must split this buffer with next prot grp */
2223 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2224 split_offset += bpl->tus.f.bdeSize;
2225 }
2226
2227 subtotal += bpl->tus.f.bdeSize;
2228
2229 if (datadir == DMA_TO_DEVICE)
2230 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2231 else
2232 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2233 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2234
2235 num_bde++;
2236 curr_data++;
2237
2238 if (split_offset)
2239 break;
2240
2241 /* Move to the next s/g segment if possible */
2242 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04002243
James Smarte2a0a9d2008-12-04 22:40:02 -05002244 }
2245
James Smart7f860592011-03-11 16:05:52 -05002246 if (protgroup_offset) {
2247 /* update the reference tag */
2248 reftag += protgrp_blks;
2249 bpl++;
2250 continue;
2251 }
2252
James Smarte2a0a9d2008-12-04 22:40:02 -05002253 /* are we done ? */
2254 if (curr_prot == protcnt) {
2255 alldone = 1;
2256 } else if (curr_prot < protcnt) {
2257 /* advance to next prot buffer */
2258 sgpe = sg_next(sgpe);
2259 bpl++;
2260
2261 /* update the reference tag */
2262 reftag += protgrp_blks;
2263 } else {
2264 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04002265 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2266 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05002267 }
2268
2269 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05002270out:
2271
James Smarte2a0a9d2008-12-04 22:40:02 -05002272 return num_bde;
2273}
James Smart7f860592011-03-11 16:05:52 -05002274
James Smartacd68592012-01-18 16:25:09 -05002275/**
2276 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2277 * @phba: The Hba for which this call is being executed.
2278 * @sc: pointer to scsi command we're working on
2279 * @sgl: pointer to buffer list for protection groups
2280 * @datacnt: number of segments of data that have been dma mapped
2281 *
2282 * This function sets up SGL buffer list for protection groups of
2283 * type LPFC_PG_TYPE_NO_DIF
2284 *
2285 * This is usually used when the HBA is instructed to generate
2286 * DIFs and insert them into data stream (or strip DIF from
2287 * incoming data stream)
2288 *
2289 * The buffer list consists of just one protection group described
2290 * below:
2291 * +-------------------------+
2292 * start of prot group --> | DI_SEED |
2293 * +-------------------------+
2294 * | Data SGE |
2295 * +-------------------------+
2296 * |more Data SGE's ... (opt)|
2297 * +-------------------------+
2298 *
2299 *
2300 * Note: Data s/g buffers have been dma mapped
2301 *
2302 * Returns the number of SGEs added to the SGL.
2303 **/
2304static int
2305lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2306 struct sli4_sge *sgl, int datasegcnt)
2307{
2308 struct scatterlist *sgde = NULL; /* s/g data entry */
2309 struct sli4_sge_diseed *diseed = NULL;
2310 dma_addr_t physaddr;
2311 int i = 0, num_sge = 0, status;
2312 int datadir = sc->sc_data_direction;
2313 uint32_t reftag;
2314 unsigned blksize;
2315 uint8_t txop, rxop;
James Smart0829a192012-05-09 21:18:12 -04002316#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002317 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002318#endif
James Smartacd68592012-01-18 16:25:09 -05002319 uint32_t checking = 1;
2320 uint32_t dma_len;
2321 uint32_t dma_offset = 0;
2322
2323 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2324 if (status)
2325 goto out;
2326
2327 /* extract some info from the scsi command for pde*/
2328 blksize = lpfc_cmd_blksize(sc);
2329 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2330
2331#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002332 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002333 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002334 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002335 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002336 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002337 checking = 0;
2338 }
2339#endif
2340
2341 /* setup DISEED with what we have */
2342 diseed = (struct sli4_sge_diseed *) sgl;
2343 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2344 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2345
2346 /* Endianness conversion if necessary */
2347 diseed->ref_tag = cpu_to_le32(reftag);
2348 diseed->ref_tag_tran = diseed->ref_tag;
2349
2350 /* setup DISEED with the rest of the info */
2351 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2352 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2353 if (datadir == DMA_FROM_DEVICE) {
2354 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2355 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2356 }
2357 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2358 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2359
2360 /* Endianness conversion if necessary for DISEED */
2361 diseed->word2 = cpu_to_le32(diseed->word2);
2362 diseed->word3 = cpu_to_le32(diseed->word3);
2363
2364 /* advance bpl and increment sge count */
2365 num_sge++;
2366 sgl++;
2367
2368 /* assumption: caller has already run dma_map_sg on command data */
2369 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2370 physaddr = sg_dma_address(sgde);
2371 dma_len = sg_dma_len(sgde);
2372 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2373 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2374 if ((i + 1) == datasegcnt)
2375 bf_set(lpfc_sli4_sge_last, sgl, 1);
2376 else
2377 bf_set(lpfc_sli4_sge_last, sgl, 0);
2378 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2379 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2380
2381 sgl->sge_len = cpu_to_le32(dma_len);
2382 dma_offset += dma_len;
2383
2384 sgl++;
2385 num_sge++;
2386 }
2387
2388out:
2389 return num_sge;
2390}
2391
2392/**
2393 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2394 * @phba: The Hba for which this call is being executed.
2395 * @sc: pointer to scsi command we're working on
2396 * @sgl: pointer to buffer list for protection groups
2397 * @datacnt: number of segments of data that have been dma mapped
2398 * @protcnt: number of segment of protection data that have been dma mapped
2399 *
2400 * This function sets up SGL buffer list for protection groups of
2401 * type LPFC_PG_TYPE_DIF
2402 *
2403 * This is usually used when DIFs are in their own buffers,
2404 * separate from the data. The HBA can then by instructed
2405 * to place the DIFs in the outgoing stream. For read operations,
2406 * The HBA could extract the DIFs and place it in DIF buffers.
2407 *
2408 * The buffer list for this type consists of one or more of the
2409 * protection groups described below:
2410 * +-------------------------+
2411 * start of first prot group --> | DISEED |
2412 * +-------------------------+
2413 * | DIF (Prot SGE) |
2414 * +-------------------------+
2415 * | Data SGE |
2416 * +-------------------------+
2417 * |more Data SGE's ... (opt)|
2418 * +-------------------------+
2419 * start of new prot group --> | DISEED |
2420 * +-------------------------+
2421 * | ... |
2422 * +-------------------------+
2423 *
2424 * Note: It is assumed that both data and protection s/g buffers have been
2425 * mapped for DMA
2426 *
2427 * Returns the number of SGEs added to the SGL.
2428 **/
2429static int
2430lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2431 struct sli4_sge *sgl, int datacnt, int protcnt)
2432{
2433 struct scatterlist *sgde = NULL; /* s/g data entry */
2434 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2435 struct sli4_sge_diseed *diseed = NULL;
2436 dma_addr_t dataphysaddr, protphysaddr;
2437 unsigned short curr_data = 0, curr_prot = 0;
2438 unsigned int split_offset;
2439 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2440 unsigned int protgrp_blks, protgrp_bytes;
2441 unsigned int remainder, subtotal;
2442 int status;
2443 unsigned char pgdone = 0, alldone = 0;
2444 unsigned blksize;
2445 uint32_t reftag;
2446 uint8_t txop, rxop;
2447 uint32_t dma_len;
James Smart0829a192012-05-09 21:18:12 -04002448#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002449 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002450#endif
James Smartacd68592012-01-18 16:25:09 -05002451 uint32_t checking = 1;
2452 uint32_t dma_offset = 0;
2453 int num_sge = 0;
2454
2455 sgpe = scsi_prot_sglist(sc);
2456 sgde = scsi_sglist(sc);
2457
2458 if (!sgpe || !sgde) {
2459 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2460 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2461 sgpe, sgde);
2462 return 0;
2463 }
2464
2465 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2466 if (status)
2467 goto out;
2468
2469 /* extract some info from the scsi command */
2470 blksize = lpfc_cmd_blksize(sc);
2471 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2472
2473#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002474 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002475 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002476 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002477 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002478 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002479 checking = 0;
2480 }
2481#endif
2482
2483 split_offset = 0;
2484 do {
2485 /* setup DISEED with what we have */
2486 diseed = (struct sli4_sge_diseed *) sgl;
2487 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2488 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2489
2490 /* Endianness conversion if necessary */
2491 diseed->ref_tag = cpu_to_le32(reftag);
2492 diseed->ref_tag_tran = diseed->ref_tag;
2493
2494 /* setup DISEED with the rest of the info */
2495 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2496 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2497 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2498 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2499 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2500 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2501
2502 /* Endianness conversion if necessary for DISEED */
2503 diseed->word2 = cpu_to_le32(diseed->word2);
2504 diseed->word3 = cpu_to_le32(diseed->word3);
2505
2506 /* advance sgl and increment bde count */
2507 num_sge++;
2508 sgl++;
2509
2510 /* setup the first BDE that points to protection buffer */
2511 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2512 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2513
2514 /* must be integer multiple of the DIF block length */
2515 BUG_ON(protgroup_len % 8);
2516
2517 /* Now setup DIF SGE */
2518 sgl->word2 = 0;
2519 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2520 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2521 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2522 sgl->word2 = cpu_to_le32(sgl->word2);
2523
2524 protgrp_blks = protgroup_len / 8;
2525 protgrp_bytes = protgrp_blks * blksize;
2526
2527 /* check if DIF SGE is crossing the 4K boundary; if so split */
2528 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2529 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2530 protgroup_offset += protgroup_remainder;
2531 protgrp_blks = protgroup_remainder / 8;
2532 protgrp_bytes = protgrp_blks * blksize;
2533 } else {
2534 protgroup_offset = 0;
2535 curr_prot++;
2536 }
2537
2538 num_sge++;
2539
2540 /* setup SGE's for data blocks associated with DIF data */
2541 pgdone = 0;
2542 subtotal = 0; /* total bytes processed for current prot grp */
2543 while (!pgdone) {
2544 if (!sgde) {
2545 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2546 "9086 BLKGRD:%s Invalid data segment\n",
2547 __func__);
2548 return 0;
2549 }
2550 sgl++;
2551 dataphysaddr = sg_dma_address(sgde) + split_offset;
2552
2553 remainder = sg_dma_len(sgde) - split_offset;
2554
2555 if ((subtotal + remainder) <= protgrp_bytes) {
2556 /* we can use this whole buffer */
2557 dma_len = remainder;
2558 split_offset = 0;
2559
2560 if ((subtotal + remainder) == protgrp_bytes)
2561 pgdone = 1;
2562 } else {
2563 /* must split this buffer with next prot grp */
2564 dma_len = protgrp_bytes - subtotal;
2565 split_offset += dma_len;
2566 }
2567
2568 subtotal += dma_len;
2569
2570 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2571 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2572 bf_set(lpfc_sli4_sge_last, sgl, 0);
2573 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2574 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2575
2576 sgl->sge_len = cpu_to_le32(dma_len);
2577 dma_offset += dma_len;
2578
2579 num_sge++;
2580 curr_data++;
2581
2582 if (split_offset)
2583 break;
2584
2585 /* Move to the next s/g segment if possible */
2586 sgde = sg_next(sgde);
2587 }
2588
2589 if (protgroup_offset) {
2590 /* update the reference tag */
2591 reftag += protgrp_blks;
2592 sgl++;
2593 continue;
2594 }
2595
2596 /* are we done ? */
2597 if (curr_prot == protcnt) {
2598 bf_set(lpfc_sli4_sge_last, sgl, 1);
2599 alldone = 1;
2600 } else if (curr_prot < protcnt) {
2601 /* advance to next prot buffer */
2602 sgpe = sg_next(sgpe);
2603 sgl++;
2604
2605 /* update the reference tag */
2606 reftag += protgrp_blks;
2607 } else {
2608 /* if we're here, we have a bug */
2609 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2610 "9085 BLKGRD: bug in %s\n", __func__);
2611 }
2612
2613 } while (!alldone);
2614
2615out:
2616
2617 return num_sge;
2618}
2619
2620/**
2621 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2622 * @phba: The Hba for which this call is being executed.
2623 * @sc: pointer to scsi command we're working on
2624 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002625 * Given a SCSI command that supports DIF, determine composition of protection
2626 * groups involved in setting up buffer lists
2627 *
James Smartacd68592012-01-18 16:25:09 -05002628 * Returns: Protection group type (with or without DIF)
2629 *
2630 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002631static int
2632lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2633{
2634 int ret = LPFC_PG_TYPE_INVALID;
2635 unsigned char op = scsi_get_prot_op(sc);
2636
2637 switch (op) {
2638 case SCSI_PROT_READ_STRIP:
2639 case SCSI_PROT_WRITE_INSERT:
2640 ret = LPFC_PG_TYPE_NO_DIF;
2641 break;
2642 case SCSI_PROT_READ_INSERT:
2643 case SCSI_PROT_WRITE_STRIP:
2644 case SCSI_PROT_READ_PASS:
2645 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002646 ret = LPFC_PG_TYPE_DIF_BUF;
2647 break;
2648 default:
2649 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2650 "9021 Unsupported protection op:%d\n", op);
2651 break;
2652 }
2653
2654 return ret;
2655}
2656
James Smartacd68592012-01-18 16:25:09 -05002657/**
2658 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2659 * @phba: The Hba for which this call is being executed.
2660 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2661 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002662 * This is the protection/DIF aware version of
2663 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2664 * two functions eventually, but for now, it's here
James Smartacd68592012-01-18 16:25:09 -05002665 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002666static int
James Smartacd68592012-01-18 16:25:09 -05002667lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smarte2a0a9d2008-12-04 22:40:02 -05002668 struct lpfc_scsi_buf *lpfc_cmd)
2669{
2670 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2671 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2672 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2673 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2674 uint32_t num_bde = 0;
2675 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2676 int prot_group_type = 0;
2677 int diflen, fcpdl;
2678 unsigned blksize;
2679
2680 /*
2681 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2682 * fcp_rsp regions to the first data bde entry
2683 */
2684 bpl += 2;
2685 if (scsi_sg_count(scsi_cmnd)) {
2686 /*
2687 * The driver stores the segment count returned from pci_map_sg
2688 * because this a count of dma-mappings used to map the use_sg
2689 * pages. They are not guaranteed to be the same for those
2690 * architectures that implement an IOMMU.
2691 */
2692 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2693 scsi_sglist(scsi_cmnd),
2694 scsi_sg_count(scsi_cmnd), datadir);
2695 if (unlikely(!datasegcnt))
2696 return 1;
2697
2698 lpfc_cmd->seg_cnt = datasegcnt;
2699 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04002700 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2701 "9067 BLKGRD: %s: Too many sg segments"
2702 " from dma_map_sg. Config %d, seg_cnt"
2703 " %d\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002704 __func__, phba->cfg_sg_seg_cnt,
2705 lpfc_cmd->seg_cnt);
2706 scsi_dma_unmap(scsi_cmnd);
2707 return 1;
2708 }
2709
2710 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2711
2712 switch (prot_group_type) {
2713 case LPFC_PG_TYPE_NO_DIF:
2714 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2715 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002716 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002717 if (num_bde < 2)
2718 goto err;
2719 break;
2720 case LPFC_PG_TYPE_DIF_BUF:{
2721 /*
2722 * This type indicates that protection buffers are
2723 * passed to the driver, so that needs to be prepared
2724 * for DMA
2725 */
2726 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2727 scsi_prot_sglist(scsi_cmnd),
2728 scsi_prot_sg_count(scsi_cmnd), datadir);
2729 if (unlikely(!protsegcnt)) {
2730 scsi_dma_unmap(scsi_cmnd);
2731 return 1;
2732 }
2733
2734 lpfc_cmd->prot_seg_cnt = protsegcnt;
2735 if (lpfc_cmd->prot_seg_cnt
2736 > phba->cfg_prot_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04002737 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2738 "9068 BLKGRD: %s: Too many prot sg "
2739 "segments from dma_map_sg. Config %d,"
James Smarte2a0a9d2008-12-04 22:40:02 -05002740 "prot_seg_cnt %d\n", __func__,
2741 phba->cfg_prot_sg_seg_cnt,
2742 lpfc_cmd->prot_seg_cnt);
2743 dma_unmap_sg(&phba->pcidev->dev,
2744 scsi_prot_sglist(scsi_cmnd),
2745 scsi_prot_sg_count(scsi_cmnd),
2746 datadir);
2747 scsi_dma_unmap(scsi_cmnd);
2748 return 1;
2749 }
2750
2751 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2752 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002753 /* we should have 3 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002754 if (num_bde < 3)
2755 goto err;
2756 break;
2757 }
2758 case LPFC_PG_TYPE_INVALID:
2759 default:
2760 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2761 "9022 Unexpected protection group %i\n",
2762 prot_group_type);
2763 return 1;
2764 }
2765 }
2766
2767 /*
2768 * Finish initializing those IOCB fields that are dependent on the
2769 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2770 * reinitialized since all iocb memory resources are used many times
2771 * for transmit, receive, and continuation bpl's.
2772 */
2773 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2774 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2775 iocb_cmd->ulpBdeCount = 1;
2776 iocb_cmd->ulpLe = 1;
2777
2778 fcpdl = scsi_bufflen(scsi_cmnd);
2779
2780 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
2781 /*
2782 * We are in DIF Type 1 mode
2783 * Every data block has a 8 byte DIF (trailer)
2784 * attached to it. Must ajust FCP data length
2785 */
2786 blksize = lpfc_cmd_blksize(scsi_cmnd);
2787 diflen = (fcpdl / blksize) * 8;
2788 fcpdl += diflen;
2789 }
2790 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2791
2792 /*
2793 * Due to difference in data length between DIF/non-DIF paths,
2794 * we need to set word 4 of IOCB here
2795 */
2796 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2797
2798 return 0;
2799err:
2800 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2801 "9023 Could not setup all needed BDE's"
2802 "prot_group_type=%d, num_bde=%d\n",
2803 prot_group_type, num_bde);
2804 return 1;
2805}
2806
2807/*
2808 * This function checks for BlockGuard errors detected by
2809 * the HBA. In case of errors, the ASC/ASCQ fields in the
2810 * sense buffer will be set accordingly, paired with
2811 * ILLEGAL_REQUEST to signal to the kernel that the HBA
2812 * detected corruption.
2813 *
2814 * Returns:
2815 * 0 - No error found
2816 * 1 - BlockGuard error found
2817 * -1 - Internal error (bad profile, ...etc)
2818 */
2819static int
2820lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
2821 struct lpfc_iocbq *pIocbOut)
2822{
2823 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2824 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
2825 int ret = 0;
2826 uint32_t bghm = bgf->bghm;
2827 uint32_t bgstat = bgf->bgstat;
2828 uint64_t failing_sector = 0;
2829
James Smart6a9c52c2009-10-02 15:16:51 -04002830 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
2831 " 0x%x lba 0x%llx blk cnt 0x%x "
James Smarte2a0a9d2008-12-04 22:40:02 -05002832 "bgstat=0x%x bghm=0x%x\n",
James Smart87b5c322008-12-16 10:34:09 -05002833 cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
Tejun Heo83096eb2009-05-07 22:24:39 +09002834 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002835
2836 spin_lock(&_dump_buf_lock);
2837 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04002838 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
2839 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002840 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04002841 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002842
2843 /* If we have a prot sgl, save the DIF buffer */
2844 if (lpfc_prot_group_type(phba, cmd) ==
2845 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04002846 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
2847 "Saving DIF for %u blocks to debugfs\n",
2848 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
2849 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002850 }
2851
2852 _dump_buf_done = 1;
2853 }
2854 spin_unlock(&_dump_buf_lock);
2855
2856 if (lpfc_bgs_get_invalid_prof(bgstat)) {
2857 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002858 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
2859 " BlockGuard profile. bgstat:0x%x\n",
2860 bgstat);
James Smarte2a0a9d2008-12-04 22:40:02 -05002861 ret = (-1);
2862 goto out;
2863 }
2864
2865 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
2866 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002867 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
2868 "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002869 bgstat);
2870 ret = (-1);
2871 goto out;
2872 }
2873
2874 if (lpfc_bgs_get_guard_err(bgstat)) {
2875 ret = 1;
2876
2877 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2878 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002879 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002880 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2881 phba->bg_guard_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002882 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2883 "9055 BLKGRD: guard_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002884 }
2885
2886 if (lpfc_bgs_get_reftag_err(bgstat)) {
2887 ret = 1;
2888
2889 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2890 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002891 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002892 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2893
2894 phba->bg_reftag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002895 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2896 "9056 BLKGRD: ref_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002897 }
2898
2899 if (lpfc_bgs_get_apptag_err(bgstat)) {
2900 ret = 1;
2901
2902 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2903 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002904 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002905 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2906
2907 phba->bg_apptag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002908 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2909 "9061 BLKGRD: app_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002910 }
2911
2912 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
2913 /*
2914 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04002915 * field, and put the failing LBA in it.
2916 * This code assumes there was also a guard/app/ref tag error
2917 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05002918 */
James Smart7c56b9f2011-07-22 18:36:25 -04002919 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
2920 cmd->sense_buffer[8] = 0; /* Information descriptor type */
2921 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
2922 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05002923
2924 /* bghm is a "on the wire" FC frame based count */
2925 switch (scsi_get_prot_op(cmd)) {
2926 case SCSI_PROT_READ_INSERT:
2927 case SCSI_PROT_WRITE_STRIP:
2928 bghm /= cmd->device->sector_size;
2929 break;
2930 case SCSI_PROT_READ_STRIP:
2931 case SCSI_PROT_WRITE_INSERT:
2932 case SCSI_PROT_READ_PASS:
2933 case SCSI_PROT_WRITE_PASS:
2934 bghm /= (cmd->device->sector_size +
2935 sizeof(struct scsi_dif_tuple));
2936 break;
2937 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002938
2939 failing_sector = scsi_get_lba(cmd);
2940 failing_sector += bghm;
2941
James Smart7c56b9f2011-07-22 18:36:25 -04002942 /* Descriptor Information */
2943 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05002944 }
2945
2946 if (!ret) {
2947 /* No error was reported - problem in FW? */
2948 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002949 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart4ac9b222012-03-01 22:38:29 -05002950 "9057 BLKGRD: Unknown error reported!\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002951 }
2952
2953out:
2954 return ret;
2955}
2956
James Smartea2151b2008-09-07 11:52:10 -04002957/**
James Smartda0436e2009-05-22 14:51:39 -04002958 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
2959 * @phba: The Hba for which this call is being executed.
2960 * @lpfc_cmd: The scsi buffer which is going to be mapped.
2961 *
2962 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
2963 * field of @lpfc_cmd for device with SLI-4 interface spec.
2964 *
2965 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04002966 * 1 - Error
2967 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04002968 **/
2969static int
2970lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2971{
2972 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2973 struct scatterlist *sgel = NULL;
2974 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2975 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
James Smartfedd3b72011-02-16 12:39:24 -05002976 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04002977 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2978 dma_addr_t physaddr;
2979 uint32_t num_bde = 0;
2980 uint32_t dma_len;
2981 uint32_t dma_offset = 0;
2982 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05002983 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04002984
2985 /*
2986 * There are three possibilities here - use scatter-gather segment, use
2987 * the single mapping, or neither. Start the lpfc command prep by
2988 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2989 * data bde entry.
2990 */
2991 if (scsi_sg_count(scsi_cmnd)) {
2992 /*
2993 * The driver stores the segment count returned from pci_map_sg
2994 * because this a count of dma-mappings used to map the use_sg
2995 * pages. They are not guaranteed to be the same for those
2996 * architectures that implement an IOMMU.
2997 */
2998
2999 nseg = scsi_dma_map(scsi_cmnd);
3000 if (unlikely(!nseg))
3001 return 1;
3002 sgl += 1;
3003 /* clear the last flag in the fcp_rsp map entry */
3004 sgl->word2 = le32_to_cpu(sgl->word2);
3005 bf_set(lpfc_sli4_sge_last, sgl, 0);
3006 sgl->word2 = cpu_to_le32(sgl->word2);
3007 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05003008 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04003009 lpfc_cmd->seg_cnt = nseg;
3010 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04003011 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3012 " %s: Too many sg segments from "
3013 "dma_map_sg. Config %d, seg_cnt %d\n",
3014 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04003015 lpfc_cmd->seg_cnt);
3016 scsi_dma_unmap(scsi_cmnd);
3017 return 1;
3018 }
3019
3020 /*
3021 * The driver established a maximum scatter-gather segment count
3022 * during probe that limits the number of sg elements in any
3023 * single scsi command. Just run through the seg_cnt and format
3024 * the sge's.
3025 * When using SLI-3 the driver will try to fit all the BDEs into
3026 * the IOCB. If it can't then the BDEs get added to a BPL as it
3027 * does for SLI-2 mode.
3028 */
3029 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3030 physaddr = sg_dma_address(sgel);
3031 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04003032 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3033 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04003034 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04003035 if ((num_bde + 1) == nseg)
3036 bf_set(lpfc_sli4_sge_last, sgl, 1);
3037 else
3038 bf_set(lpfc_sli4_sge_last, sgl, 0);
3039 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04003040 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04003041 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05003042 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04003043 dma_offset += dma_len;
3044 sgl++;
3045 }
James Smartfedd3b72011-02-16 12:39:24 -05003046 /* setup the performance hint (first data BDE) if enabled */
3047 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
3048 bde = (struct ulp_bde64 *)
3049 &(iocb_cmd->unsli3.sli3Words[5]);
3050 bde->addrLow = first_data_sgl->addr_lo;
3051 bde->addrHigh = first_data_sgl->addr_hi;
3052 bde->tus.f.bdeSize =
3053 le32_to_cpu(first_data_sgl->sge_len);
3054 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3055 bde->tus.w = cpu_to_le32(bde->tus.w);
3056 }
James Smartda0436e2009-05-22 14:51:39 -04003057 } else {
3058 sgl += 1;
3059 /* clear the last flag in the fcp_rsp map entry */
3060 sgl->word2 = le32_to_cpu(sgl->word2);
3061 bf_set(lpfc_sli4_sge_last, sgl, 1);
3062 sgl->word2 = cpu_to_le32(sgl->word2);
3063 }
3064
3065 /*
3066 * Finish initializing those IOCB fields that are dependent on the
3067 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3068 * explicitly reinitialized.
3069 * all iocb memory resources are reused.
3070 */
3071 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3072
3073 /*
3074 * Due to difference in data length between DIF/non-DIF paths,
3075 * we need to set word 4 of IOCB here
3076 */
3077 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
3078 return 0;
3079}
3080
3081/**
James Smartacd68592012-01-18 16:25:09 -05003082 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
3083 * @phba: The Hba for which this call is being executed.
3084 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
3085 *
3086 * Adjust the data length to account for how much data
3087 * is actually on the wire.
3088 *
3089 * returns the adjusted data length
3090 **/
3091static int
3092lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
3093 struct lpfc_scsi_buf *lpfc_cmd)
3094{
3095 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
3096 int diflen, fcpdl;
3097 unsigned blksize;
3098
3099 fcpdl = scsi_bufflen(sc);
3100
3101 /* Check if there is protection data on the wire */
3102 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
3103 /* Read */
3104 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
3105 return fcpdl;
3106
3107 } else {
3108 /* Write */
3109 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
3110 return fcpdl;
3111 }
3112
3113 /* If protection data on the wire, adjust the count accordingly */
3114 blksize = lpfc_cmd_blksize(sc);
3115 diflen = (fcpdl / blksize) * 8;
3116 fcpdl += diflen;
3117 return fcpdl;
3118}
3119
3120/**
3121 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3122 * @phba: The Hba for which this call is being executed.
3123 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3124 *
3125 * This is the protection/DIF aware version of
3126 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3127 * two functions eventually, but for now, it's here
3128 **/
3129static int
3130lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3131 struct lpfc_scsi_buf *lpfc_cmd)
3132{
3133 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3134 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3135 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3136 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3137 uint32_t num_bde = 0;
3138 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3139 int prot_group_type = 0;
3140 int fcpdl;
3141
3142 /*
3143 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
3144 * fcp_rsp regions to the first data bde entry
3145 */
3146 if (scsi_sg_count(scsi_cmnd)) {
3147 /*
3148 * The driver stores the segment count returned from pci_map_sg
3149 * because this a count of dma-mappings used to map the use_sg
3150 * pages. They are not guaranteed to be the same for those
3151 * architectures that implement an IOMMU.
3152 */
3153 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3154 scsi_sglist(scsi_cmnd),
3155 scsi_sg_count(scsi_cmnd), datadir);
3156 if (unlikely(!datasegcnt))
3157 return 1;
3158
3159 sgl += 1;
3160 /* clear the last flag in the fcp_rsp map entry */
3161 sgl->word2 = le32_to_cpu(sgl->word2);
3162 bf_set(lpfc_sli4_sge_last, sgl, 0);
3163 sgl->word2 = cpu_to_le32(sgl->word2);
3164
3165 sgl += 1;
3166 lpfc_cmd->seg_cnt = datasegcnt;
3167 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
3168 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3169 "9087 BLKGRD: %s: Too many sg segments"
3170 " from dma_map_sg. Config %d, seg_cnt"
3171 " %d\n",
3172 __func__, phba->cfg_sg_seg_cnt,
3173 lpfc_cmd->seg_cnt);
3174 scsi_dma_unmap(scsi_cmnd);
3175 return 1;
3176 }
3177
3178 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3179
3180 switch (prot_group_type) {
3181 case LPFC_PG_TYPE_NO_DIF:
3182 num_bde = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
3183 datasegcnt);
3184 /* we should have 2 or more entries in buffer list */
3185 if (num_bde < 2)
3186 goto err;
3187 break;
3188 case LPFC_PG_TYPE_DIF_BUF:{
3189 /*
3190 * This type indicates that protection buffers are
3191 * passed to the driver, so that needs to be prepared
3192 * for DMA
3193 */
3194 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3195 scsi_prot_sglist(scsi_cmnd),
3196 scsi_prot_sg_count(scsi_cmnd), datadir);
3197 if (unlikely(!protsegcnt)) {
3198 scsi_dma_unmap(scsi_cmnd);
3199 return 1;
3200 }
3201
3202 lpfc_cmd->prot_seg_cnt = protsegcnt;
3203 if (lpfc_cmd->prot_seg_cnt
3204 > phba->cfg_prot_sg_seg_cnt) {
3205 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3206 "9088 BLKGRD: %s: Too many prot sg "
3207 "segments from dma_map_sg. Config %d,"
3208 "prot_seg_cnt %d\n", __func__,
3209 phba->cfg_prot_sg_seg_cnt,
3210 lpfc_cmd->prot_seg_cnt);
3211 dma_unmap_sg(&phba->pcidev->dev,
3212 scsi_prot_sglist(scsi_cmnd),
3213 scsi_prot_sg_count(scsi_cmnd),
3214 datadir);
3215 scsi_dma_unmap(scsi_cmnd);
3216 return 1;
3217 }
3218
3219 num_bde = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
3220 datasegcnt, protsegcnt);
3221 /* we should have 3 or more entries in buffer list */
3222 if (num_bde < 3)
3223 goto err;
3224 break;
3225 }
3226 case LPFC_PG_TYPE_INVALID:
3227 default:
3228 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3229 "9083 Unexpected protection group %i\n",
3230 prot_group_type);
3231 return 1;
3232 }
3233 }
3234
3235 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
3236
3237 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3238
3239 /*
3240 * Due to difference in data length between DIF/non-DIF paths,
3241 * we need to set word 4 of IOCB here
3242 */
3243 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
3244 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF;
3245
3246 return 0;
3247err:
3248 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3249 "9084 Could not setup all needed BDE's"
3250 "prot_group_type=%d, num_bde=%d\n",
3251 prot_group_type, num_bde);
3252 return 1;
3253}
3254
3255/**
James Smart3772a992009-05-22 14:50:54 -04003256 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3257 * @phba: The Hba for which this call is being executed.
3258 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3259 *
3260 * This routine wraps the actual DMA mapping function pointer from the
3261 * lpfc_hba struct.
3262 *
3263 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003264 * 1 - Error
3265 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003266 **/
3267static inline int
3268lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3269{
3270 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3271}
3272
3273/**
James Smartacd68592012-01-18 16:25:09 -05003274 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3275 * using BlockGuard.
3276 * @phba: The Hba for which this call is being executed.
3277 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3278 *
3279 * This routine wraps the actual DMA mapping function pointer from the
3280 * lpfc_hba struct.
3281 *
3282 * Return codes:
3283 * 1 - Error
3284 * 0 - Success
3285 **/
3286static inline int
3287lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3288{
3289 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3290}
3291
3292/**
James Smart3621a712009-04-06 18:47:14 -04003293 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003294 * @phba: Pointer to hba context object.
3295 * @vport: Pointer to vport object.
3296 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3297 * @rsp_iocb: Pointer to response iocb object which reported error.
3298 *
3299 * This function posts an event when there is a SCSI command reporting
3300 * error from the scsi device.
3301 **/
3302static void
3303lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3304 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3305 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3306 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3307 uint32_t resp_info = fcprsp->rspStatus2;
3308 uint32_t scsi_status = fcprsp->rspStatus3;
3309 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3310 struct lpfc_fast_path_event *fast_path_evt = NULL;
3311 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3312 unsigned long flags;
3313
James Smart5989b8d2010-10-22 11:06:56 -04003314 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3315 return;
3316
James Smartea2151b2008-09-07 11:52:10 -04003317 /* If there is queuefull or busy condition send a scsi event */
3318 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3319 (cmnd->result == SAM_STAT_BUSY)) {
3320 fast_path_evt = lpfc_alloc_fast_evt(phba);
3321 if (!fast_path_evt)
3322 return;
3323 fast_path_evt->un.scsi_evt.event_type =
3324 FC_REG_SCSI_EVENT;
3325 fast_path_evt->un.scsi_evt.subcategory =
3326 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3327 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3328 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3329 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3330 &pnode->nlp_portname, sizeof(struct lpfc_name));
3331 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3332 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3333 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3334 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3335 fast_path_evt = lpfc_alloc_fast_evt(phba);
3336 if (!fast_path_evt)
3337 return;
3338 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3339 FC_REG_SCSI_EVENT;
3340 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3341 LPFC_EVENT_CHECK_COND;
3342 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3343 cmnd->device->lun;
3344 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3345 &pnode->nlp_portname, sizeof(struct lpfc_name));
3346 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3347 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3348 fast_path_evt->un.check_cond_evt.sense_key =
3349 cmnd->sense_buffer[2] & 0xf;
3350 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3351 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3352 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3353 fcpi_parm &&
3354 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3355 ((scsi_status == SAM_STAT_GOOD) &&
3356 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3357 /*
3358 * If status is good or resid does not match with fcp_param and
3359 * there is valid fcpi_parm, then there is a read_check error
3360 */
3361 fast_path_evt = lpfc_alloc_fast_evt(phba);
3362 if (!fast_path_evt)
3363 return;
3364 fast_path_evt->un.read_check_error.header.event_type =
3365 FC_REG_FABRIC_EVENT;
3366 fast_path_evt->un.read_check_error.header.subcategory =
3367 LPFC_EVENT_FCPRDCHKERR;
3368 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3369 &pnode->nlp_portname, sizeof(struct lpfc_name));
3370 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3371 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3372 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3373 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3374 fast_path_evt->un.read_check_error.fcpiparam =
3375 fcpi_parm;
3376 } else
3377 return;
3378
3379 fast_path_evt->vport = vport;
3380 spin_lock_irqsave(&phba->hbalock, flags);
3381 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3382 spin_unlock_irqrestore(&phba->hbalock, flags);
3383 lpfc_worker_wake_up(phba);
3384 return;
3385}
James Smart9bad7672008-12-04 22:39:02 -05003386
3387/**
James Smartf1126682009-06-10 17:22:44 -04003388 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003389 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003390 * @psb: The scsi buffer which is going to be un-mapped.
3391 *
3392 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003393 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003394 **/
dea31012005-04-17 16:05:31 -05003395static void
James Smartf1126682009-06-10 17:22:44 -04003396lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003397{
3398 /*
3399 * There are only two special cases to consider. (1) the scsi command
3400 * requested scatter-gather usage or (2) the scsi command allocated
3401 * a request buffer, but did not request use_sg. There is a third
3402 * case, but it does not require resource deallocation.
3403 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003404 if (psb->seg_cnt > 0)
3405 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003406 if (psb->prot_seg_cnt > 0)
3407 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3408 scsi_prot_sg_count(psb->pCmd),
3409 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003410}
3411
James Smart9bad7672008-12-04 22:39:02 -05003412/**
James Smart3621a712009-04-06 18:47:14 -04003413 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003414 * @vport: The virtual port for which this call is being executed.
3415 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3416 * @rsp_iocb: The response IOCB which contains FCP error.
3417 *
3418 * This routine is called to process response IOCB with status field
3419 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3420 * based upon SCSI and FCP error.
3421 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003422static void
James Smart2e0fef82007-06-17 19:56:36 -05003423lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3424 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003425{
3426 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3427 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3428 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003429 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003430 uint32_t resp_info = fcprsp->rspStatus2;
3431 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003432 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003433 uint32_t host_status = DID_OK;
3434 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05003435 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003436
James Smartea2151b2008-09-07 11:52:10 -04003437
dea31012005-04-17 16:05:31 -05003438 /*
3439 * If this is a task management command, there is no
3440 * scsi packet associated with this lpfc_cmd. The driver
3441 * consumes it.
3442 */
3443 if (fcpcmd->fcpCntl2) {
3444 scsi_status = 0;
3445 goto out;
3446 }
3447
James Smart6a9c52c2009-10-02 15:16:51 -04003448 if (resp_info & RSP_LEN_VALID) {
3449 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003450 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04003451 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3452 "2719 Invalid response length: "
3453 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
3454 cmnd->device->id,
3455 cmnd->device->lun, cmnd->cmnd[0],
3456 rsplen);
3457 host_status = DID_ERROR;
3458 goto out;
3459 }
James Smarte40a02c2010-02-26 14:13:54 -05003460 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3461 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3462 "2757 Protocol failure detected during "
3463 "processing of FCP I/O op: "
3464 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
3465 cmnd->device->id,
3466 cmnd->device->lun, cmnd->cmnd[0],
3467 fcprsp->rspInfo3);
3468 host_status = DID_ERROR;
3469 goto out;
3470 }
James Smart6a9c52c2009-10-02 15:16:51 -04003471 }
3472
James Smartc7743952006-12-02 13:34:42 -05003473 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3474 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3475 if (snslen > SCSI_SENSE_BUFFERSIZE)
3476 snslen = SCSI_SENSE_BUFFERSIZE;
3477
3478 if (resp_info & RSP_LEN_VALID)
3479 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3480 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3481 }
3482 lp = (uint32_t *)cmnd->sense_buffer;
3483
James Smart73d91e52011-10-10 21:32:10 -04003484 if (!scsi_status && (resp_info & RESID_UNDER) &&
3485 vport->cfg_log_verbose & LOG_FCP_UNDER)
3486 logit = LOG_FCP_UNDER;
James Smartc7743952006-12-02 13:34:42 -05003487
James Smarte8b62012007-08-02 11:10:09 -04003488 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003489 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003490 "Data: x%x x%x x%x x%x x%x\n",
3491 cmnd->cmnd[0], scsi_status,
3492 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3493 be32_to_cpu(fcprsp->rspResId),
3494 be32_to_cpu(fcprsp->rspSnsLen),
3495 be32_to_cpu(fcprsp->rspRspLen),
3496 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003497
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003498 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05003499 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003500 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003501
James Smart73d91e52011-10-10 21:32:10 -04003502 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smarte2a0a9d2008-12-04 22:40:02 -05003503 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003504 "residual %d Data: x%x x%x x%x\n",
3505 be32_to_cpu(fcpcmd->fcpDl),
3506 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3507 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003508
3509 /*
James Smart7054a602007-04-25 09:52:34 -04003510 * If there is an under run check if under run reported by
3511 * storage array is same as the under run reported by HBA.
3512 * If this is not same, there is a dropped frame.
3513 */
3514 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3515 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003516 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003517 lpfc_printf_vlog(vport, KERN_WARNING,
3518 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003519 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003520 "and Underrun Data: x%x x%x x%x x%x\n",
3521 be32_to_cpu(fcpcmd->fcpDl),
3522 scsi_get_resid(cmnd), fcpi_parm,
3523 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003524 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003525 host_status = DID_ERROR;
3526 }
3527 /*
dea31012005-04-17 16:05:31 -05003528 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003529 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003530 * is not present, make sure the actual amount transferred is at
3531 * least the underflow value or fail.
3532 */
3533 if (!(resp_info & SNS_LEN_VALID) &&
3534 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003535 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3536 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003537 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003538 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003539 "underrun converted to error "
3540 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003541 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003542 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003543 host_status = DID_ERROR;
3544 }
3545 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003546 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003547 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003548 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003549 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003550 host_status = DID_ERROR;
3551
3552 /*
3553 * Check SLI validation that all the transfer was actually done
3554 * (fcpi_parm should be zero). Apply check only to reads.
3555 */
James Smarteee88772010-09-29 11:19:08 -04003556 } else if (fcpi_parm && (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -04003557 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003558 "9029 FCP Read Check Error Data: "
James Smarteee88772010-09-29 11:19:08 -04003559 "x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003560 be32_to_cpu(fcpcmd->fcpDl),
3561 be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04003562 fcpi_parm, cmnd->cmnd[0], scsi_status);
3563 switch (scsi_status) {
3564 case SAM_STAT_GOOD:
3565 case SAM_STAT_CHECK_CONDITION:
3566 /* Fabric dropped a data frame. Fail any successful
3567 * command in which we detected dropped frames.
3568 * A status of good or some check conditions could
3569 * be considered a successful command.
3570 */
3571 host_status = DID_ERROR;
3572 break;
3573 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003574 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05003575 }
3576
3577 out:
3578 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04003579 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05003580}
3581
James Smart9bad7672008-12-04 22:39:02 -05003582/**
James Smart3621a712009-04-06 18:47:14 -04003583 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05003584 * @phba: The Hba for which this call is being executed.
3585 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04003586 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05003587 *
3588 * This routine assigns scsi command result by looking into response IOCB
3589 * status field appropriately. This routine handles QUEUE FULL condition as
3590 * well by ramping down device queue depth.
3591 **/
dea31012005-04-17 16:05:31 -05003592static void
3593lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3594 struct lpfc_iocbq *pIocbOut)
3595{
3596 struct lpfc_scsi_buf *lpfc_cmd =
3597 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003598 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05003599 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3600 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04003601 struct scsi_cmnd *cmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003602 int result;
James Smarta257bf92009-04-06 18:48:10 -04003603 struct scsi_device *tmp_sdev;
James Smart5ffc2662009-11-18 15:39:44 -05003604 int depth;
James Smartfa61a542008-01-11 01:52:42 -05003605 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04003606 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04003607 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04003608 uint32_t queue_depth, scsi_id;
James Smart73d91e52011-10-10 21:32:10 -04003609 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05003610
James Smart75baf692010-06-08 18:31:21 -04003611 /* Sanity check on return of outstanding command */
3612 if (!(lpfc_cmd->pCmd))
3613 return;
3614 cmd = lpfc_cmd->pCmd;
3615 shost = cmd->device->host;
3616
dea31012005-04-17 16:05:31 -05003617 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
3618 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05003619 /* pick up SLI4 exhange busy status from HBA */
3620 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
3621
James Smart9a6b09c2012-03-01 22:37:42 -05003622#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3623 if (lpfc_cmd->prot_data_type) {
3624 struct scsi_dif_tuple *src = NULL;
3625
3626 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
3627 /*
3628 * Used to restore any changes to protection
3629 * data for error injection.
3630 */
3631 switch (lpfc_cmd->prot_data_type) {
3632 case LPFC_INJERR_REFTAG:
3633 src->ref_tag =
3634 lpfc_cmd->prot_data;
3635 break;
3636 case LPFC_INJERR_APPTAG:
3637 src->app_tag =
3638 (uint16_t)lpfc_cmd->prot_data;
3639 break;
3640 case LPFC_INJERR_GUARD:
3641 src->guard_tag =
3642 (uint16_t)lpfc_cmd->prot_data;
3643 break;
3644 default:
3645 break;
3646 }
3647
3648 lpfc_cmd->prot_data = 0;
3649 lpfc_cmd->prot_data_type = 0;
3650 lpfc_cmd->prot_data_segment = NULL;
3651 }
3652#endif
James Smart109f6ed2008-12-04 22:39:08 -05003653 if (pnode && NLP_CHK_NODE_ACT(pnode))
3654 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05003655
3656 if (lpfc_cmd->status) {
3657 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
3658 (lpfc_cmd->result & IOERR_DRVR_MASK))
3659 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3660 else if (lpfc_cmd->status >= IOSTAT_CNT)
3661 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smart73d91e52011-10-10 21:32:10 -04003662 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR
3663 && !lpfc_cmd->fcp_rsp->rspStatus3
3664 && (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER)
3665 && !(phba->cfg_log_verbose & LOG_FCP_UNDER))
3666 logit = 0;
3667 else
3668 logit = LOG_FCP | LOG_FCP_UNDER;
3669 lpfc_printf_vlog(vport, KERN_WARNING, logit,
3670 "9030 FCP cmd x%x failed <%d/%d> "
James Smart5a0d80f2012-05-09 21:18:20 -04003671 "status: x%x result: x%x "
3672 "sid: x%x did: x%x oxid: x%x "
3673 "Data: x%x x%x\n",
James Smart73d91e52011-10-10 21:32:10 -04003674 cmd->cmnd[0],
3675 cmd->device ? cmd->device->id : 0xffff,
3676 cmd->device ? cmd->device->lun : 0xffff,
3677 lpfc_cmd->status, lpfc_cmd->result,
James Smart5a0d80f2012-05-09 21:18:20 -04003678 vport->fc_myDID, pnode->nlp_DID,
3679 phba->sli_rev == LPFC_SLI_REV4 ?
3680 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
James Smart73d91e52011-10-10 21:32:10 -04003681 pIocbOut->iocb.ulpContext,
3682 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05003683
3684 switch (lpfc_cmd->status) {
3685 case IOSTAT_FCP_RSP_ERROR:
3686 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05003687 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05003688 break;
3689 case IOSTAT_NPORT_BSY:
3690 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04003691 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04003692 fast_path_evt = lpfc_alloc_fast_evt(phba);
3693 if (!fast_path_evt)
3694 break;
3695 fast_path_evt->un.fabric_evt.event_type =
3696 FC_REG_FABRIC_EVENT;
3697 fast_path_evt->un.fabric_evt.subcategory =
3698 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
3699 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
3700 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3701 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
3702 &pnode->nlp_portname,
3703 sizeof(struct lpfc_name));
3704 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
3705 &pnode->nlp_nodename,
3706 sizeof(struct lpfc_name));
3707 }
3708 fast_path_evt->vport = vport;
3709 fast_path_evt->work_evt.evt =
3710 LPFC_EVT_FASTPATH_MGMT_EVT;
3711 spin_lock_irqsave(&phba->hbalock, flags);
3712 list_add_tail(&fast_path_evt->work_evt.evt_listp,
3713 &phba->work_list);
3714 spin_unlock_irqrestore(&phba->hbalock, flags);
3715 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05003716 break;
James Smart92d7f7b2007-06-17 19:56:38 -05003717 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05003718 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05003719 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
3720 lpfc_cmd->result ==
3721 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
3722 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
3723 lpfc_cmd->result ==
3724 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
3725 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
3726 break;
3727 }
James Smartd7c255b2008-08-24 21:50:00 -04003728 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05003729 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04003730 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
3731 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
James Smart92d7f7b2007-06-17 19:56:38 -05003732 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04003733 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05003734 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003735 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
3736 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
3737 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
3738 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
3739 /*
3740 * This is a response for a BG enabled
3741 * cmd. Parse BG error
3742 */
3743 lpfc_parse_bg_err(phba, lpfc_cmd,
3744 pIocbOut);
3745 break;
3746 } else {
3747 lpfc_printf_vlog(vport, KERN_WARNING,
3748 LOG_BG,
3749 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04003750 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05003751 }
3752 }
James Smart1151e3e2011-02-16 12:39:35 -05003753 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
3754 && (phba->sli_rev == LPFC_SLI_REV4)
3755 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
3756 /* This IO was aborted by the target, we don't
3757 * know the rxid and because we did not send the
3758 * ABTS we cannot generate and RRQ.
3759 */
3760 lpfc_set_rrq_active(phba, pnode,
3761 lpfc_cmd->cur_iocbq.sli4_xritag,
3762 0, 0);
3763 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003764 /* else: fall through */
dea31012005-04-17 16:05:31 -05003765 default:
3766 cmd->result = ScsiResult(DID_ERROR, 0);
3767 break;
3768 }
3769
James Smart58da1ff2008-04-07 10:15:56 -04003770 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003771 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04003772 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
3773 SAM_STAT_BUSY);
James Smartab56dc22011-02-16 12:39:57 -05003774 } else
dea31012005-04-17 16:05:31 -05003775 cmd->result = ScsiResult(DID_OK, 0);
dea31012005-04-17 16:05:31 -05003776
3777 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
3778 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
3779
James Smarte8b62012007-08-02 11:10:09 -04003780 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3781 "0710 Iodone <%d/%d> cmd %p, error "
3782 "x%x SNS x%x x%x Data: x%x x%x\n",
3783 cmd->device->id, cmd->device->lun, cmd,
3784 cmd->result, *lp, *(lp + 3), cmd->retries,
3785 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05003786 }
3787
James Smartea2151b2008-09-07 11:52:10 -04003788 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003789 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04003790 if (vport->cfg_max_scsicmpl_time &&
3791 time_after(jiffies, lpfc_cmd->start_time +
3792 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04003793 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05003794 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3795 if (pnode->cmd_qdepth >
3796 atomic_read(&pnode->cmd_pending) &&
3797 (atomic_read(&pnode->cmd_pending) >
3798 LPFC_MIN_TGT_QDEPTH) &&
3799 ((cmd->cmnd[0] == READ_10) ||
3800 (cmd->cmnd[0] == WRITE_10)))
3801 pnode->cmd_qdepth =
3802 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04003803
James Smart109f6ed2008-12-04 22:39:08 -05003804 pnode->last_change_time = jiffies;
3805 }
James Smarta257bf92009-04-06 18:48:10 -04003806 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05003807 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smart7dc517d2010-07-14 15:32:10 -04003808 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
James Smart977b5a02008-09-07 11:52:04 -04003809 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05003810 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04003811 spin_lock_irqsave(shost->host_lock, flags);
James Smart7dc517d2010-07-14 15:32:10 -04003812 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
3813 / 100;
3814 depth = depth ? depth : 1;
3815 pnode->cmd_qdepth += depth;
3816 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
3817 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05003818 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04003819 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05003820 }
James Smart977b5a02008-09-07 11:52:04 -04003821 }
3822
James Smart1dcb58e2007-04-25 09:51:30 -04003823 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04003824
3825 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
3826 queue_depth = cmd->device->queue_depth;
3827 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05003828 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003829
Jamie Wellnitzb8086082006-02-28 22:33:12 -05003830 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smartfa61a542008-01-11 01:52:42 -05003831 /*
3832 * If there is a thread waiting for command completion
3833 * wake up the thread.
3834 */
James Smarta257bf92009-04-06 18:48:10 -04003835 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04003836 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05003837 if (lpfc_cmd->waitq)
3838 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04003839 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05003840 lpfc_release_scsi_buf(phba, lpfc_cmd);
3841 return;
3842 }
3843
James Smart92d7f7b2007-06-17 19:56:38 -05003844 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04003845 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05003846
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003847 /*
3848 * Check for queue full. If the lun is reporting queue full, then
3849 * back off the lun queue depth to prevent target overloads.
3850 */
James Smart58da1ff2008-04-07 10:15:56 -04003851 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
3852 NLP_CHK_NODE_ACT(pnode)) {
James Smarta257bf92009-04-06 18:48:10 -04003853 shost_for_each_device(tmp_sdev, shost) {
3854 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003855 continue;
3856 depth = scsi_track_queue_full(tmp_sdev,
James Smart5ffc2662009-11-18 15:39:44 -05003857 tmp_sdev->queue_depth-1);
3858 if (depth <= 0)
3859 continue;
James Smarte8b62012007-08-02 11:10:09 -04003860 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3861 "0711 detected queue full - lun queue "
3862 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04003863 lpfc_send_sdev_queuedepth_change_event(phba, vport,
James Smart5ffc2662009-11-18 15:39:44 -05003864 pnode,
3865 tmp_sdev->lun,
3866 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003867 }
3868 }
3869
James Smartfa61a542008-01-11 01:52:42 -05003870 /*
3871 * If there is a thread waiting for command completion
3872 * wake up the thread.
3873 */
James Smarta257bf92009-04-06 18:48:10 -04003874 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04003875 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05003876 if (lpfc_cmd->waitq)
3877 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04003878 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05003879
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003880 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05003881}
3882
James Smart34b02dc2008-08-24 21:49:55 -04003883/**
James Smart3621a712009-04-06 18:47:14 -04003884 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04003885 * @data: A pointer to the immediate command data portion of the IOCB.
3886 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
3887 *
3888 * The routine copies the entire FCP command from @fcp_cmnd to @data while
3889 * byte swapping the data to big endian format for transmission on the wire.
3890 **/
3891static void
3892lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
3893{
3894 int i, j;
3895 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
3896 i += sizeof(uint32_t), j++) {
3897 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
3898 }
3899}
3900
James Smart9bad7672008-12-04 22:39:02 -05003901/**
James Smartf1126682009-06-10 17:22:44 -04003902 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05003903 * @vport: The virtual port for which this call is being executed.
3904 * @lpfc_cmd: The scsi command which needs to send.
3905 * @pnode: Pointer to lpfc_nodelist.
3906 *
3907 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04003908 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003909 **/
dea31012005-04-17 16:05:31 -05003910static void
James Smartf1126682009-06-10 17:22:44 -04003911lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05003912 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05003913{
James Smart2e0fef82007-06-17 19:56:36 -05003914 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003915 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3916 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3917 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3918 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
3919 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04003920 char tag[2];
dea31012005-04-17 16:05:31 -05003921
James Smart58da1ff2008-04-07 10:15:56 -04003922 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3923 return;
3924
dea31012005-04-17 16:05:31 -05003925 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04003926 /* clear task management bits */
3927 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05003928
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04003929 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
3930 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05003931
James Smartdf9e1b52011-12-13 13:22:17 -05003932 memset(&fcp_cmnd->fcpCdb[0], 0, LPFC_FCP_CDB_LEN);
3933 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
James Smart7e2b19f2007-10-29 11:00:39 -04003934 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
3935 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05003936 case HEAD_OF_QUEUE_TAG:
3937 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
3938 break;
3939 case ORDERED_QUEUE_TAG:
3940 fcp_cmnd->fcpCntl1 = ORDERED_Q;
3941 break;
3942 default:
3943 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
3944 break;
3945 }
3946 } else
3947 fcp_cmnd->fcpCntl1 = 0;
3948
3949 /*
3950 * There are three possibilities here - use scatter-gather segment, use
3951 * the single mapping, or neither. Start the lpfc command prep by
3952 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3953 * data bde entry.
3954 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003955 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05003956 if (datadir == DMA_TO_DEVICE) {
3957 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart3772a992009-05-22 14:50:54 -04003958 if (phba->sli_rev < LPFC_SLI_REV4) {
3959 iocb_cmd->un.fcpi.fcpi_parm = 0;
3960 iocb_cmd->ulpPU = 0;
3961 } else
3962 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05003963 fcp_cmnd->fcpCntl3 = WRITE_DATA;
3964 phba->fc4OutputRequests++;
3965 } else {
3966 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
3967 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05003968 fcp_cmnd->fcpCntl3 = READ_DATA;
3969 phba->fc4InputRequests++;
3970 }
3971 } else {
3972 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
3973 iocb_cmd->un.fcpi.fcpi_parm = 0;
3974 iocb_cmd->ulpPU = 0;
3975 fcp_cmnd->fcpCntl3 = 0;
3976 phba->fc4ControlRequests++;
3977 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003978 if (phba->sli_rev == 3 &&
3979 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04003980 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05003981 /*
3982 * Finish initializing those IOCB fields that are independent
3983 * of the scsi_cmnd request_buffer
3984 */
3985 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04003986 if (phba->sli_rev == LPFC_SLI_REV4)
3987 piocbq->iocb.ulpContext =
3988 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05003989 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
3990 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05003991 else
3992 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05003993
3994 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
3995 piocbq->context1 = lpfc_cmd;
3996 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
3997 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05003998 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05003999}
4000
James Smart9bad7672008-12-04 22:39:02 -05004001/**
James Smart6d368e52011-05-24 11:44:12 -04004002 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004003 * @vport: The virtual port for which this call is being executed.
4004 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4005 * @lun: Logical unit number.
4006 * @task_mgmt_cmd: SCSI task management command.
4007 *
James Smart3772a992009-05-22 14:50:54 -04004008 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4009 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004010 *
4011 * Return codes:
4012 * 0 - Error
4013 * 1 - Success
4014 **/
dea31012005-04-17 16:05:31 -05004015static int
James Smartf1126682009-06-10 17:22:44 -04004016lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05004017 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04004018 unsigned int lun,
dea31012005-04-17 16:05:31 -05004019 uint8_t task_mgmt_cmd)
4020{
dea31012005-04-17 16:05:31 -05004021 struct lpfc_iocbq *piocbq;
4022 IOCB_t *piocb;
4023 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04004024 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05004025 struct lpfc_nodelist *ndlp = rdata->pnode;
4026
James Smart58da1ff2008-04-07 10:15:56 -04004027 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4028 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05004029 return 0;
dea31012005-04-17 16:05:31 -05004030
dea31012005-04-17 16:05:31 -05004031 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004032 piocbq->vport = vport;
4033
dea31012005-04-17 16:05:31 -05004034 piocb = &piocbq->iocb;
4035
4036 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04004037 /* Clear out any old data in the FCP command area */
4038 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4039 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004040 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05004041 if (vport->phba->sli_rev == 3 &&
4042 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004043 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004044 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05004045 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04004046 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4047 piocb->ulpContext =
4048 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4049 }
dea31012005-04-17 16:05:31 -05004050 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
4051 piocb->ulpFCP2Rcvy = 1;
4052 }
4053 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
4054
4055 /* ulpTimeout is only one byte */
4056 if (lpfc_cmd->timeout > 0xff) {
4057 /*
4058 * Do not timeout the command at the firmware level.
4059 * The driver will provide the timeout mechanism.
4060 */
4061 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04004062 } else
dea31012005-04-17 16:05:31 -05004063 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04004064
4065 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4066 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004067
James Smart2e0fef82007-06-17 19:56:36 -05004068 return 1;
dea31012005-04-17 16:05:31 -05004069}
4070
James Smart9bad7672008-12-04 22:39:02 -05004071/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004072 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04004073 * @phba: The hba struct for which this call is being executed.
4074 * @dev_grp: The HBA PCI-Device group number.
4075 *
4076 * This routine sets up the SCSI interface API function jump table in @phba
4077 * struct.
4078 * Returns: 0 - success, -ENODEV - failure.
4079 **/
4080int
4081lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4082{
4083
James Smartf1126682009-06-10 17:22:44 -04004084 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4085 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04004086
James Smart3772a992009-05-22 14:50:54 -04004087 switch (dev_grp) {
4088 case LPFC_PCI_DEV_LP:
4089 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4090 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05004091 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004092 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05004093 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004094 break;
James Smartda0436e2009-05-22 14:51:39 -04004095 case LPFC_PCI_DEV_OC:
4096 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4097 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05004098 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004099 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05004100 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004101 break;
James Smart3772a992009-05-22 14:50:54 -04004102 default:
4103 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4104 "1418 Invalid HBA PCI-device group: 0x%x\n",
4105 dev_grp);
4106 return -ENODEV;
4107 break;
4108 }
James Smart3772a992009-05-22 14:50:54 -04004109 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05004110 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04004111 return 0;
4112}
4113
4114/**
James Smart3621a712009-04-06 18:47:14 -04004115 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05004116 * @phba: The Hba for which this call is being executed.
4117 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4118 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4119 *
4120 * This routine is IOCB completion routine for device reset and target reset
4121 * routine. This routine release scsi buffer associated with lpfc_cmd.
4122 **/
James Smart7054a602007-04-25 09:52:34 -04004123static void
4124lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4125 struct lpfc_iocbq *cmdiocbq,
4126 struct lpfc_iocbq *rspiocbq)
4127{
4128 struct lpfc_scsi_buf *lpfc_cmd =
4129 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4130 if (lpfc_cmd)
4131 lpfc_release_scsi_buf(phba, lpfc_cmd);
4132 return;
4133}
4134
James Smart9bad7672008-12-04 22:39:02 -05004135/**
James Smart3621a712009-04-06 18:47:14 -04004136 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05004137 * @host: The scsi host for which this call is being executed.
4138 *
4139 * This routine provides module information about hba.
4140 *
4141 * Reutrn code:
4142 * Pointer to char - Success.
4143 **/
dea31012005-04-17 16:05:31 -05004144const char *
4145lpfc_info(struct Scsi_Host *host)
4146{
James Smart2e0fef82007-06-17 19:56:36 -05004147 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4148 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004149 int len;
4150 static char lpfcinfobuf[384];
4151
4152 memset(lpfcinfobuf,0,384);
4153 if (phba && phba->pcidev){
4154 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4155 len = strlen(lpfcinfobuf);
4156 snprintf(lpfcinfobuf + len,
4157 384-len,
4158 " on PCI bus %02x device %02x irq %d",
4159 phba->pcidev->bus->number,
4160 phba->pcidev->devfn,
4161 phba->pcidev->irq);
4162 len = strlen(lpfcinfobuf);
4163 if (phba->Port[0]) {
4164 snprintf(lpfcinfobuf + len,
4165 384-len,
4166 " port %s",
4167 phba->Port);
4168 }
James Smart65467b62010-01-26 23:08:29 -05004169 len = strlen(lpfcinfobuf);
4170 if (phba->sli4_hba.link_state.logical_speed) {
4171 snprintf(lpfcinfobuf + len,
4172 384-len,
4173 " Logical Link Speed: %d Mbps",
4174 phba->sli4_hba.link_state.logical_speed * 10);
4175 }
dea31012005-04-17 16:05:31 -05004176 }
4177 return lpfcinfobuf;
4178}
4179
James Smart9bad7672008-12-04 22:39:02 -05004180/**
James Smart3621a712009-04-06 18:47:14 -04004181 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05004182 * @phba: The Hba for which this call is being executed.
4183 *
4184 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4185 * The default value of cfg_poll_tmo is 10 milliseconds.
4186 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004187static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4188{
4189 unsigned long poll_tmo_expires =
4190 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4191
4192 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
4193 mod_timer(&phba->fcp_poll_timer,
4194 poll_tmo_expires);
4195}
4196
James Smart9bad7672008-12-04 22:39:02 -05004197/**
James Smart3621a712009-04-06 18:47:14 -04004198 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004199 * @phba: The Hba for which this call is being executed.
4200 *
4201 * This routine starts the fcp_poll_timer of @phba.
4202 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004203void lpfc_poll_start_timer(struct lpfc_hba * phba)
4204{
4205 lpfc_poll_rearm_timer(phba);
4206}
4207
James Smart9bad7672008-12-04 22:39:02 -05004208/**
James Smart3621a712009-04-06 18:47:14 -04004209 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05004210 * @ptr: Map to lpfc_hba data structure pointer.
4211 *
4212 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4213 * and FCP Ring interrupt is disable.
4214 **/
4215
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004216void lpfc_poll_timeout(unsigned long ptr)
4217{
James Smart2e0fef82007-06-17 19:56:36 -05004218 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004219
4220 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004221 lpfc_sli_handle_fast_ring_event(phba,
4222 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4223
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004224 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4225 lpfc_poll_rearm_timer(phba);
4226 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004227}
4228
James Smart9bad7672008-12-04 22:39:02 -05004229/**
James Smart3621a712009-04-06 18:47:14 -04004230 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05004231 * @cmnd: Pointer to scsi_cmnd data structure.
4232 * @done: Pointer to done routine.
4233 *
4234 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4235 * This routine prepares an IOCB from scsi command and provides to firmware.
4236 * The @done callback is invoked after driver finished processing the command.
4237 *
4238 * Return value :
4239 * 0 - Success
4240 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4241 **/
dea31012005-04-17 16:05:31 -05004242static int
Jeff Garzikf2812332010-11-16 02:10:29 -05004243lpfc_queuecommand_lck(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
dea31012005-04-17 16:05:31 -05004244{
James Smart2e0fef82007-06-17 19:56:36 -05004245 struct Scsi_Host *shost = cmnd->device->host;
4246 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4247 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004248 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004249 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004250 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004251 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004252 int err;
dea31012005-04-17 16:05:31 -05004253
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004254 err = fc_remote_port_chkready(rport);
4255 if (err) {
4256 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004257 goto out_fail_command;
4258 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004259 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004260
James Smartbf086112011-08-21 21:48:13 -04004261 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004262 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004263
James Smart6a9c52c2009-10-02 15:16:51 -04004264 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4265 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4266 " op:%02x str=%s without registering for"
4267 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004268 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4269 dif_op_str[scsi_get_prot_op(cmnd)]);
4270 goto out_fail_command;
4271 }
4272
dea31012005-04-17 16:05:31 -05004273 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004274 * Catch race where our node has transitioned, but the
4275 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004276 */
James Smartb522d7d2008-09-07 11:51:56 -04004277 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
Mike Christief55ca84d2011-02-25 14:04:27 -06004278 cmnd->result = ScsiResult(DID_IMM_RETRY, 0);
James Smartb522d7d2008-09-07 11:51:56 -04004279 goto out_fail_command;
4280 }
James Smart7dc517d2010-07-14 15:32:10 -04004281 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
Mike Christie34963432011-02-25 14:04:28 -06004282 goto out_tgt_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05004283
James Smart19ca7602010-11-20 23:11:55 -05004284 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05004285 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004286 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004287
James Smarte8b62012007-08-02 11:10:09 -04004288 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4289 "0707 driver's buffer pool is empty, "
4290 "IO busied\n");
dea31012005-04-17 16:05:31 -05004291 goto out_host_busy;
4292 }
4293
4294 /*
4295 * Store the midlayer's command structure for the completion phase
4296 * and complete the command initialization.
4297 */
4298 lpfc_cmd->pCmd = cmnd;
4299 lpfc_cmd->rdata = rdata;
4300 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04004301 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05004302 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
4303 cmnd->scsi_done = done;
4304
James Smarte2a0a9d2008-12-04 22:40:02 -05004305 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004306 if (vport->phba->cfg_enable_bg) {
4307 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smart9a6b09c2012-03-01 22:37:42 -05004308 "9033 BLKGRD: rcvd protected cmd:%02x op=%s "
4309 "guard=%s\n", cmnd->cmnd[0],
4310 dif_op_str[scsi_get_prot_op(cmnd)],
4311 dif_grd_str[scsi_host_get_guard(shost)]);
James Smart6a9c52c2009-10-02 15:16:51 -04004312 if (cmnd->cmnd[0] == READ_10)
4313 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05004314 "9035 BLKGRD: READ @ sector %llu, "
James Smart9a6b09c2012-03-01 22:37:42 -05004315 "cnt %u, rpt %d\n",
Tejun Heo83096eb2009-05-07 22:24:39 +09004316 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004317 blk_rq_sectors(cmnd->request),
4318 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004319 else if (cmnd->cmnd[0] == WRITE_10)
4320 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05004321 "9036 BLKGRD: WRITE @ sector %llu, "
James Smart9a6b09c2012-03-01 22:37:42 -05004322 "cnt %u, wpt %d\n",
James Smart87b5c322008-12-16 10:34:09 -05004323 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09004324 blk_rq_sectors(cmnd->request),
James Smart9a6b09c2012-03-01 22:37:42 -05004325 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004326 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004327
4328 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4329 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004330 if (vport->phba->cfg_enable_bg) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004331 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smart9a6b09c2012-03-01 22:37:42 -05004332 "9038 BLKGRD: rcvd unprotected cmd:"
4333 "%02x op=%s guard=%s\n", cmnd->cmnd[0],
4334 dif_op_str[scsi_get_prot_op(cmnd)],
4335 dif_grd_str[scsi_host_get_guard(shost)]);
James Smart6a9c52c2009-10-02 15:16:51 -04004336 if (cmnd->cmnd[0] == READ_10)
4337 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
4338 "9040 dbg: READ @ sector %llu, "
James Smart9a6b09c2012-03-01 22:37:42 -05004339 "cnt %u, rpt %d\n",
James Smart6a9c52c2009-10-02 15:16:51 -04004340 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004341 blk_rq_sectors(cmnd->request),
4342 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004343 else if (cmnd->cmnd[0] == WRITE_10)
4344 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smart9a6b09c2012-03-01 22:37:42 -05004345 "9041 dbg: WRITE @ sector %llu, "
4346 "cnt %u, wpt %d\n",
4347 (unsigned long long)scsi_get_lba(cmnd),
4348 blk_rq_sectors(cmnd->request),
4349 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004350 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004351 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4352 }
4353
dea31012005-04-17 16:05:31 -05004354 if (err)
4355 goto out_host_busy_free_buf;
4356
James Smart2e0fef82007-06-17 19:56:36 -05004357 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004358
James Smart977b5a02008-09-07 11:52:04 -04004359 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04004360 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004361 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05004362 if (err) {
4363 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05004364 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004365 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004366 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004367 spin_unlock(shost->host_lock);
4368 lpfc_sli_handle_fast_ring_event(phba,
4369 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4370
4371 spin_lock(shost->host_lock);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004372 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4373 lpfc_poll_rearm_timer(phba);
4374 }
4375
dea31012005-04-17 16:05:31 -05004376 return 0;
4377
4378 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04004379 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004380 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004381 out_host_busy:
4382 return SCSI_MLQUEUE_HOST_BUSY;
4383
Mike Christie34963432011-02-25 14:04:28 -06004384 out_tgt_busy:
4385 return SCSI_MLQUEUE_TARGET_BUSY;
4386
dea31012005-04-17 16:05:31 -05004387 out_fail_command:
4388 done(cmnd);
4389 return 0;
4390}
4391
Jeff Garzikf2812332010-11-16 02:10:29 -05004392static DEF_SCSI_QCMD(lpfc_queuecommand)
4393
James Smart9bad7672008-12-04 22:39:02 -05004394/**
James Smart3621a712009-04-06 18:47:14 -04004395 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004396 * @cmnd: Pointer to scsi_cmnd data structure.
4397 *
4398 * This routine aborts @cmnd pending in base driver.
4399 *
4400 * Return code :
4401 * 0x2003 - Error
4402 * 0x2002 - Success
4403 **/
dea31012005-04-17 16:05:31 -05004404static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004405lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004406{
James Smart2e0fef82007-06-17 19:56:36 -05004407 struct Scsi_Host *shost = cmnd->device->host;
4408 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4409 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004410 struct lpfc_iocbq *iocb;
4411 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004412 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004413 IOCB_t *cmd, *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004414 int ret = SUCCESS;
James Smartfa61a542008-01-11 01:52:42 -05004415 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004416
James Smart589a52d2010-07-14 15:30:54 -04004417 ret = fc_block_scsi_eh(cmnd);
4418 if (ret)
4419 return ret;
James Smart4f2e66c2012-05-09 21:17:07 -04004420
4421 spin_lock_irq(&phba->hbalock);
4422 /* driver queued commands are in process of being flushed */
4423 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
4424 spin_unlock_irq(&phba->hbalock);
4425 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4426 "3168 SCSI Layer abort requested I/O has been "
4427 "flushed by LLD.\n");
4428 return FAILED;
4429 }
4430
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004431 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smarteee88772010-09-29 11:19:08 -04004432 if (!lpfc_cmd) {
James Smart4f2e66c2012-05-09 21:17:07 -04004433 spin_unlock_irq(&phba->hbalock);
James Smarteee88772010-09-29 11:19:08 -04004434 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4435 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004436 "x%x ID %d LUN %d\n",
4437 ret, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04004438 return SUCCESS;
4439 }
dea31012005-04-17 16:05:31 -05004440
James Smart4f2e66c2012-05-09 21:17:07 -04004441 iocb = &lpfc_cmd->cur_iocbq;
4442 /* the command is in process of being cancelled */
4443 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
4444 spin_unlock_irq(&phba->hbalock);
4445 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4446 "3169 SCSI Layer abort requested I/O has been "
4447 "cancelled by LLD.\n");
4448 return FAILED;
4449 }
dea31012005-04-17 16:05:31 -05004450 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004451 * If pCmd field of the corresponding lpfc_scsi_buf structure
4452 * points to a different SCSI command, then the driver has
4453 * already completed this command, but the midlayer did not
James Smart4f2e66c2012-05-09 21:17:07 -04004454 * see the completion before the eh fired. Just return SUCCESS.
dea31012005-04-17 16:05:31 -05004455 */
James Smart4f2e66c2012-05-09 21:17:07 -04004456 if (lpfc_cmd->pCmd != cmnd) {
4457 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4458 "3170 SCSI Layer abort requested I/O has been "
4459 "completed by LLD.\n");
4460 goto out_unlock;
4461 }
dea31012005-04-17 16:05:31 -05004462
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004463 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004464
James Smart4f2e66c2012-05-09 21:17:07 -04004465 abtsiocb = __lpfc_sli_get_iocbq(phba);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004466 if (abtsiocb == NULL) {
4467 ret = FAILED;
James Smart4f2e66c2012-05-09 21:17:07 -04004468 goto out_unlock;
dea31012005-04-17 16:05:31 -05004469 }
4470
dea31012005-04-17 16:05:31 -05004471 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004472 * The scsi command can not be in txq and it is in flight because the
4473 * pCmd is still pointig at the SCSI command we have to abort. There
4474 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004475 */
dea31012005-04-17 16:05:31 -05004476
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004477 cmd = &iocb->iocb;
4478 icmd = &abtsiocb->iocb;
4479 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4480 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004481 if (phba->sli_rev == LPFC_SLI_REV4)
4482 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4483 else
4484 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004485
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004486 icmd->ulpLe = 1;
4487 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004488
4489 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4490 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05004491 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05004492
James Smart2e0fef82007-06-17 19:56:36 -05004493 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004494 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4495 else
4496 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004497
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004498 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004499 abtsiocb->vport = vport;
James Smart4f2e66c2012-05-09 21:17:07 -04004500 /* no longer need the lock after this point */
4501 spin_unlock_irq(&phba->hbalock);
4502
James Smart3772a992009-05-22 14:50:54 -04004503 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
4504 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004505 lpfc_sli_release_iocbq(phba, abtsiocb);
4506 ret = FAILED;
4507 goto out;
4508 }
4509
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004510 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004511 lpfc_sli_handle_fast_ring_event(phba,
4512 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004513
James Smartfa61a542008-01-11 01:52:42 -05004514 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004515 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004516 wait_event_timeout(waitq,
4517 (lpfc_cmd->pCmd != cmnd),
4518 (2*vport->cfg_devloss_tmo*HZ));
James Smartfa61a542008-01-11 01:52:42 -05004519 lpfc_cmd->waitq = NULL;
dea31012005-04-17 16:05:31 -05004520
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004521 if (lpfc_cmd->pCmd == cmnd) {
4522 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04004523 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4524 "0748 abort handler timed out waiting "
4525 "for abort to complete: ret %#x, ID %d, "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004526 "LUN %d\n",
4527 ret, cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004528 }
James Smart4f2e66c2012-05-09 21:17:07 -04004529 goto out;
dea31012005-04-17 16:05:31 -05004530
James Smart4f2e66c2012-05-09 21:17:07 -04004531out_unlock:
4532 spin_unlock_irq(&phba->hbalock);
4533out:
James Smarte8b62012007-08-02 11:10:09 -04004534 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4535 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004536 "LUN %d\n", ret, cmnd->device->id,
4537 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004538 return ret;
dea31012005-04-17 16:05:31 -05004539}
4540
James Smartbbb9d182009-06-10 17:23:16 -04004541static char *
4542lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4543{
4544 switch (task_mgmt_cmd) {
4545 case FCP_ABORT_TASK_SET:
4546 return "ABORT_TASK_SET";
4547 case FCP_CLEAR_TASK_SET:
4548 return "FCP_CLEAR_TASK_SET";
4549 case FCP_BUS_RESET:
4550 return "FCP_BUS_RESET";
4551 case FCP_LUN_RESET:
4552 return "FCP_LUN_RESET";
4553 case FCP_TARGET_RESET:
4554 return "FCP_TARGET_RESET";
4555 case FCP_CLEAR_ACA:
4556 return "FCP_CLEAR_ACA";
4557 case FCP_TERMINATE_TASK:
4558 return "FCP_TERMINATE_TASK";
4559 default:
4560 return "unknown";
4561 }
4562}
4563
4564/**
4565 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
4566 * @vport: The virtual port for which this call is being executed.
4567 * @rdata: Pointer to remote port local data
4568 * @tgt_id: Target ID of remote device.
4569 * @lun_id: Lun number for the TMF
4570 * @task_mgmt_cmd: type of TMF to send
4571 *
4572 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
4573 * a remote port.
4574 *
4575 * Return Code:
4576 * 0x2003 - Error
4577 * 0x2002 - Success.
4578 **/
4579static int
4580lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
4581 unsigned tgt_id, unsigned int lun_id,
4582 uint8_t task_mgmt_cmd)
4583{
4584 struct lpfc_hba *phba = vport->phba;
4585 struct lpfc_scsi_buf *lpfc_cmd;
4586 struct lpfc_iocbq *iocbq;
4587 struct lpfc_iocbq *iocbqrsp;
James Smart5989b8d2010-10-22 11:06:56 -04004588 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004589 int ret;
4590 int status;
4591
James Smart5989b8d2010-10-22 11:06:56 -04004592 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smartbbb9d182009-06-10 17:23:16 -04004593 return FAILED;
4594
James Smart19ca7602010-11-20 23:11:55 -05004595 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
James Smartbbb9d182009-06-10 17:23:16 -04004596 if (lpfc_cmd == NULL)
4597 return FAILED;
4598 lpfc_cmd->timeout = 60;
4599 lpfc_cmd->rdata = rdata;
4600
4601 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
4602 task_mgmt_cmd);
4603 if (!status) {
4604 lpfc_release_scsi_buf(phba, lpfc_cmd);
4605 return FAILED;
4606 }
4607
4608 iocbq = &lpfc_cmd->cur_iocbq;
4609 iocbqrsp = lpfc_sli_get_iocbq(phba);
4610 if (iocbqrsp == NULL) {
4611 lpfc_release_scsi_buf(phba, lpfc_cmd);
4612 return FAILED;
4613 }
4614
4615 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4616 "0702 Issue %s to TGT %d LUN %d "
James Smart6d368e52011-05-24 11:44:12 -04004617 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04004618 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04004619 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
4620 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04004621
4622 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
4623 iocbq, iocbqrsp, lpfc_cmd->timeout);
4624 if (status != IOCB_SUCCESS) {
4625 if (status == IOCB_TIMEDOUT) {
4626 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
4627 ret = TIMEOUT_ERROR;
4628 } else
4629 ret = FAILED;
4630 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4631 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart6d368e52011-05-24 11:44:12 -04004632 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
4633 "iocb_flag x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04004634 lpfc_taskmgmt_name(task_mgmt_cmd),
4635 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
James Smart6d368e52011-05-24 11:44:12 -04004636 iocbqrsp->iocb.un.ulpWord[4],
4637 iocbq->iocb_flag);
James Smart2a9bf3d2010-06-07 15:24:45 -04004638 } else if (status == IOCB_BUSY)
4639 ret = FAILED;
4640 else
James Smartbbb9d182009-06-10 17:23:16 -04004641 ret = SUCCESS;
4642
4643 lpfc_sli_release_iocbq(phba, iocbqrsp);
4644
4645 if (ret != TIMEOUT_ERROR)
4646 lpfc_release_scsi_buf(phba, lpfc_cmd);
4647
4648 return ret;
4649}
4650
4651/**
4652 * lpfc_chk_tgt_mapped -
4653 * @vport: The virtual port to check on
4654 * @cmnd: Pointer to scsi_cmnd data structure.
4655 *
4656 * This routine delays until the scsi target (aka rport) for the
4657 * command exists (is present and logged in) or we declare it non-existent.
4658 *
4659 * Return code :
4660 * 0x2003 - Error
4661 * 0x2002 - Success
4662 **/
4663static int
4664lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
4665{
4666 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004667 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004668 unsigned long later;
4669
James Smart1c6f4ef52009-11-18 15:40:49 -05004670 if (!rdata) {
4671 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4672 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
4673 return FAILED;
4674 }
4675 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004676 /*
4677 * If target is not in a MAPPED state, delay until
4678 * target is rediscovered or devloss timeout expires.
4679 */
4680 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
4681 while (time_after(later, jiffies)) {
4682 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4683 return FAILED;
4684 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
4685 return SUCCESS;
4686 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
4687 rdata = cmnd->device->hostdata;
4688 if (!rdata)
4689 return FAILED;
4690 pnode = rdata->pnode;
4691 }
4692 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
4693 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
4694 return FAILED;
4695 return SUCCESS;
4696}
4697
4698/**
4699 * lpfc_reset_flush_io_context -
4700 * @vport: The virtual port (scsi_host) for the flush context
4701 * @tgt_id: If aborting by Target contect - specifies the target id
4702 * @lun_id: If aborting by Lun context - specifies the lun id
4703 * @context: specifies the context level to flush at.
4704 *
4705 * After a reset condition via TMF, we need to flush orphaned i/o
4706 * contexts from the adapter. This routine aborts any contexts
4707 * outstanding, then waits for their completions. The wait is
4708 * bounded by devloss_tmo though.
4709 *
4710 * Return code :
4711 * 0x2003 - Error
4712 * 0x2002 - Success
4713 **/
4714static int
4715lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
4716 uint64_t lun_id, lpfc_ctx_cmd context)
4717{
4718 struct lpfc_hba *phba = vport->phba;
4719 unsigned long later;
4720 int cnt;
4721
4722 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
4723 if (cnt)
4724 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
4725 tgt_id, lun_id, context);
4726 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
4727 while (time_after(later, jiffies) && cnt) {
4728 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
4729 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
4730 }
4731 if (cnt) {
4732 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4733 "0724 I/O flush failure for context %s : cnt x%x\n",
4734 ((context == LPFC_CTX_LUN) ? "LUN" :
4735 ((context == LPFC_CTX_TGT) ? "TGT" :
4736 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
4737 cnt);
4738 return FAILED;
4739 }
4740 return SUCCESS;
4741}
4742
James Smart9bad7672008-12-04 22:39:02 -05004743/**
James Smart3621a712009-04-06 18:47:14 -04004744 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05004745 * @cmnd: Pointer to scsi_cmnd data structure.
4746 *
James Smartbbb9d182009-06-10 17:23:16 -04004747 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05004748 * command.
4749 *
4750 * Return code :
4751 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04004752 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05004753 **/
dea31012005-04-17 16:05:31 -05004754static int
James Smart7054a602007-04-25 09:52:34 -04004755lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004756{
James Smart2e0fef82007-06-17 19:56:36 -05004757 struct Scsi_Host *shost = cmnd->device->host;
4758 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05004759 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004760 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004761 unsigned tgt_id = cmnd->device->id;
4762 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04004763 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04004764 int status;
dea31012005-04-17 16:05:31 -05004765
James Smart1c6f4ef52009-11-18 15:40:49 -05004766 if (!rdata) {
4767 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4768 "0798 Device Reset rport failure: rdata x%p\n", rdata);
4769 return FAILED;
4770 }
4771 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04004772 status = fc_block_scsi_eh(cmnd);
4773 if (status)
4774 return status;
James Smartbbb9d182009-06-10 17:23:16 -04004775
4776 status = lpfc_chk_tgt_mapped(vport, cmnd);
4777 if (status == FAILED) {
4778 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4779 "0721 Device Reset rport failure: rdata x%p\n", rdata);
4780 return FAILED;
4781 }
4782
4783 scsi_event.event_type = FC_REG_SCSI_EVENT;
4784 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
4785 scsi_event.lun = lun_id;
4786 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
4787 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
4788
4789 fc_host_post_vendor_event(shost, fc_get_event_number(),
4790 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
4791
4792 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
4793 FCP_LUN_RESET);
4794
4795 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4796 "0713 SCSI layer issued Device Reset (%d, %d) "
4797 "return x%x\n", tgt_id, lun_id, status);
4798
dea31012005-04-17 16:05:31 -05004799 /*
James Smartbbb9d182009-06-10 17:23:16 -04004800 * We have to clean up i/o as : they may be orphaned by the TMF;
4801 * or if the TMF failed, they may be in an indeterminate state.
4802 * So, continue on.
4803 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05004804 */
James Smartbbb9d182009-06-10 17:23:16 -04004805 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
4806 LPFC_CTX_LUN);
4807 return status;
4808}
4809
4810/**
4811 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
4812 * @cmnd: Pointer to scsi_cmnd data structure.
4813 *
4814 * This routine does a target reset by sending a TARGET_RESET task management
4815 * command.
4816 *
4817 * Return code :
4818 * 0x2003 - Error
4819 * 0x2002 - Success
4820 **/
4821static int
4822lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
4823{
4824 struct Scsi_Host *shost = cmnd->device->host;
4825 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4826 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004827 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004828 unsigned tgt_id = cmnd->device->id;
4829 unsigned int lun_id = cmnd->device->lun;
4830 struct lpfc_scsi_event_header scsi_event;
4831 int status;
4832
James Smart1c6f4ef52009-11-18 15:40:49 -05004833 if (!rdata) {
4834 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4835 "0799 Target Reset rport failure: rdata x%p\n", rdata);
4836 return FAILED;
4837 }
4838 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04004839 status = fc_block_scsi_eh(cmnd);
4840 if (status)
4841 return status;
James Smartbbb9d182009-06-10 17:23:16 -04004842
4843 status = lpfc_chk_tgt_mapped(vport, cmnd);
4844 if (status == FAILED) {
4845 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4846 "0722 Target Reset rport failure: rdata x%p\n", rdata);
4847 return FAILED;
dea31012005-04-17 16:05:31 -05004848 }
James Smartea2151b2008-09-07 11:52:10 -04004849
4850 scsi_event.event_type = FC_REG_SCSI_EVENT;
4851 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
4852 scsi_event.lun = 0;
4853 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
4854 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
4855
James Smartbbb9d182009-06-10 17:23:16 -04004856 fc_host_post_vendor_event(shost, fc_get_event_number(),
4857 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04004858
James Smartbbb9d182009-06-10 17:23:16 -04004859 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
4860 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05004861
James Smarte8b62012007-08-02 11:10:09 -04004862 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04004863 "0723 SCSI layer issued Target Reset (%d, %d) "
4864 "return x%x\n", tgt_id, lun_id, status);
4865
4866 /*
4867 * We have to clean up i/o as : they may be orphaned by the TMF;
4868 * or if the TMF failed, they may be in an indeterminate state.
4869 * So, continue on.
4870 * We will report success if all the i/o aborts successfully.
4871 */
4872 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
4873 LPFC_CTX_TGT);
4874 return status;
dea31012005-04-17 16:05:31 -05004875}
4876
James Smart9bad7672008-12-04 22:39:02 -05004877/**
James Smart3621a712009-04-06 18:47:14 -04004878 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004879 * @cmnd: Pointer to scsi_cmnd data structure.
4880 *
James Smartbbb9d182009-06-10 17:23:16 -04004881 * This routine does target reset to all targets on @cmnd->device->host.
4882 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05004883 *
James Smartbbb9d182009-06-10 17:23:16 -04004884 * Return code :
4885 * 0x2003 - Error
4886 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05004887 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04004888static int
James Smart7054a602007-04-25 09:52:34 -04004889lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004890{
James Smart2e0fef82007-06-17 19:56:36 -05004891 struct Scsi_Host *shost = cmnd->device->host;
4892 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05004893 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04004894 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04004895 int match;
4896 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04004897
4898 scsi_event.event_type = FC_REG_SCSI_EVENT;
4899 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
4900 scsi_event.lun = 0;
4901 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
4902 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
4903
James Smartbbb9d182009-06-10 17:23:16 -04004904 fc_host_post_vendor_event(shost, fc_get_event_number(),
4905 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05004906
James Smartbf086112011-08-21 21:48:13 -04004907 status = fc_block_scsi_eh(cmnd);
4908 if (status)
4909 return status;
James Smartbbb9d182009-06-10 17:23:16 -04004910
dea31012005-04-17 16:05:31 -05004911 /*
4912 * Since the driver manages a single bus device, reset all
4913 * targets known to the driver. Should any target reset
4914 * fail, this routine returns failure to the midlayer.
4915 */
James Smarte17da182006-07-06 15:49:25 -04004916 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04004917 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05004918 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004919 spin_lock_irq(shost->host_lock);
4920 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004921 if (!NLP_CHK_NODE_ACT(ndlp))
4922 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004923 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04004924 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04004925 ndlp->rport) {
dea31012005-04-17 16:05:31 -05004926 match = 1;
4927 break;
4928 }
4929 }
James Smart2e0fef82007-06-17 19:56:36 -05004930 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004931 if (!match)
4932 continue;
James Smartbbb9d182009-06-10 17:23:16 -04004933
4934 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
4935 i, 0, FCP_TARGET_RESET);
4936
4937 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04004938 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4939 "0700 Bus Reset on target %d failed\n",
4940 i);
James Smart915caaa2008-06-14 22:52:38 -04004941 ret = FAILED;
dea31012005-04-17 16:05:31 -05004942 }
4943 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05004944 /*
James Smartbbb9d182009-06-10 17:23:16 -04004945 * We have to clean up i/o as : they may be orphaned by the TMFs
4946 * above; or if any of the TMFs failed, they may be in an
4947 * indeterminate state.
4948 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05004949 */
James Smartbbb9d182009-06-10 17:23:16 -04004950
4951 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
4952 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05004953 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04004954
James Smarte8b62012007-08-02 11:10:09 -04004955 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4956 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05004957 return ret;
4958}
4959
James Smart9bad7672008-12-04 22:39:02 -05004960/**
James Smart3621a712009-04-06 18:47:14 -04004961 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05004962 * @sdev: Pointer to scsi_device.
4963 *
4964 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
4965 * globally available list of scsi buffers. This routine also makes sure scsi
4966 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
4967 * of scsi buffer exists for the lifetime of the driver.
4968 *
4969 * Return codes:
4970 * non-0 - Error
4971 * 0 - Success
4972 **/
dea31012005-04-17 16:05:31 -05004973static int
dea31012005-04-17 16:05:31 -05004974lpfc_slave_alloc(struct scsi_device *sdev)
4975{
James Smart2e0fef82007-06-17 19:56:36 -05004976 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
4977 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004978 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04004979 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05004980 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04004981 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04004982 uint32_t sdev_cnt;
dea31012005-04-17 16:05:31 -05004983
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004984 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05004985 return -ENXIO;
4986
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004987 sdev->hostdata = rport->dd_data;
James Smartd7c47992010-06-08 18:31:54 -04004988 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05004989
4990 /*
4991 * Populate the cmds_per_lun count scsi_bufs into this host's globally
4992 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04004993 * HBA limit conveyed to the midlayer via the host structure. The
4994 * formula accounts for the lun_queue_depth + error handlers + 1
4995 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05004996 */
4997 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04004998 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05004999
James Smartd7c47992010-06-08 18:31:54 -04005000 /* If allocated buffers are enough do nothing */
5001 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5002 return 0;
5003
James Smart92d7f7b2007-06-17 19:56:38 -05005004 /* Allow some exchanges to be available always to complete discovery */
5005 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005006 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5007 "0704 At limitation of %d preallocated "
5008 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05005009 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05005010 /* Allow some exchanges to be available always to complete discovery */
5011 } else if (total + num_to_alloc >
5012 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005013 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5014 "0705 Allocation request of %d "
5015 "command buffers will exceed max of %d. "
5016 "Reducing allocation request to %d.\n",
5017 num_to_alloc, phba->cfg_hba_queue_depth,
5018 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05005019 num_to_alloc = phba->cfg_hba_queue_depth - total;
5020 }
James Smart3772a992009-05-22 14:50:54 -04005021 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
5022 if (num_to_alloc != num_allocated) {
5023 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5024 "0708 Allocation request of %d "
5025 "command buffers did not succeed. "
5026 "Allocated %d buffers.\n",
5027 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05005028 }
James Smart1c6f4ef52009-11-18 15:40:49 -05005029 if (num_allocated > 0)
5030 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05005031 return 0;
5032}
5033
James Smart9bad7672008-12-04 22:39:02 -05005034/**
James Smart3621a712009-04-06 18:47:14 -04005035 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05005036 * @sdev: Pointer to scsi_device.
5037 *
5038 * This routine configures following items
5039 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05005040 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5041 *
5042 * Return codes:
5043 * 0 - Success
5044 **/
dea31012005-04-17 16:05:31 -05005045static int
5046lpfc_slave_configure(struct scsi_device *sdev)
5047{
James Smart2e0fef82007-06-17 19:56:36 -05005048 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5049 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005050
5051 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04005052 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005053 else
James Smart3de2a652007-08-02 11:09:59 -04005054 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005055
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005056 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005057 lpfc_sli_handle_fast_ring_event(phba,
5058 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005059 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5060 lpfc_poll_rearm_timer(phba);
5061 }
5062
dea31012005-04-17 16:05:31 -05005063 return 0;
5064}
5065
James Smart9bad7672008-12-04 22:39:02 -05005066/**
James Smart3621a712009-04-06 18:47:14 -04005067 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05005068 * @sdev: Pointer to scsi_device.
5069 *
5070 * This routine sets @sdev hostatdata filed to null.
5071 **/
dea31012005-04-17 16:05:31 -05005072static void
5073lpfc_slave_destroy(struct scsi_device *sdev)
5074{
James Smartd7c47992010-06-08 18:31:54 -04005075 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5076 struct lpfc_hba *phba = vport->phba;
5077 atomic_dec(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005078 sdev->hostdata = NULL;
5079 return;
5080}
5081
James Smart92d7f7b2007-06-17 19:56:38 -05005082
dea31012005-04-17 16:05:31 -05005083struct scsi_host_template lpfc_template = {
5084 .module = THIS_MODULE,
5085 .name = LPFC_DRIVER_NAME,
5086 .info = lpfc_info,
5087 .queuecommand = lpfc_queuecommand,
5088 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005089 .eh_device_reset_handler = lpfc_device_reset_handler,
5090 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04005091 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05005092 .slave_alloc = lpfc_slave_alloc,
5093 .slave_configure = lpfc_slave_configure,
5094 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04005095 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05005096 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005097 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05005098 .cmd_per_lun = LPFC_CMD_PER_LUN,
5099 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05005100 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04005101 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04005102 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05005103 .change_queue_depth = lpfc_change_queue_depth,
dea31012005-04-17 16:05:31 -05005104};
James Smart3de2a652007-08-02 11:09:59 -04005105
5106struct scsi_host_template lpfc_vport_template = {
5107 .module = THIS_MODULE,
5108 .name = LPFC_DRIVER_NAME,
5109 .info = lpfc_info,
5110 .queuecommand = lpfc_queuecommand,
5111 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005112 .eh_device_reset_handler = lpfc_device_reset_handler,
5113 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04005114 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5115 .slave_alloc = lpfc_slave_alloc,
5116 .slave_configure = lpfc_slave_configure,
5117 .slave_destroy = lpfc_slave_destroy,
5118 .scan_finished = lpfc_scan_finished,
5119 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005120 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04005121 .cmd_per_lun = LPFC_CMD_PER_LUN,
5122 .use_clustering = ENABLE_CLUSTERING,
5123 .shost_attrs = lpfc_vport_attrs,
5124 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05005125 .change_queue_depth = lpfc_change_queue_depth,
James Smart3de2a652007-08-02 11:09:59 -04005126};