blob: b3e8f33460095ccba66038d0fd370d778c57f823 [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 Smart549e55c2007-08-02 11:09:51 -0400402 vports = lpfc_create_vport_work_array(phba);
403 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400404 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400405 shost = lpfc_shost_from_vport(vports[i]);
406 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500407 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400408 sdev->queue_depth * num_rsrc_err /
409 (num_rsrc_err + num_cmd_success);
410 if (!new_queue_depth)
411 new_queue_depth = sdev->queue_depth - 1;
412 else
413 new_queue_depth = sdev->queue_depth -
414 new_queue_depth;
James Smart5ffc2662009-11-18 15:39:44 -0500415 lpfc_change_queue_depth(sdev, new_queue_depth,
416 SCSI_QDEPTH_DEFAULT);
James Smart549e55c2007-08-02 11:09:51 -0400417 }
James Smart92d7f7b2007-06-17 19:56:38 -0500418 }
James Smart09372822008-01-11 01:52:54 -0500419 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500420 atomic_set(&phba->num_rsrc_err, 0);
421 atomic_set(&phba->num_cmd_success, 0);
422}
423
James Smart9bad7672008-12-04 22:39:02 -0500424/**
James Smart3621a712009-04-06 18:47:14 -0400425 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500426 * @phba: The Hba for which this call is being executed.
427 *
428 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
429 * thread.This routine increases queue depth for all scsi device on each vport
430 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
431 * num_cmd_success to zero.
432 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500433void
434lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
435{
James Smart549e55c2007-08-02 11:09:51 -0400436 struct lpfc_vport **vports;
437 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500438 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400439 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500440
James Smart549e55c2007-08-02 11:09:51 -0400441 vports = lpfc_create_vport_work_array(phba);
442 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400443 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400444 shost = lpfc_shost_from_vport(vports[i]);
445 shost_for_each_device(sdev, shost) {
James Smart97eab632008-04-07 10:16:05 -0400446 if (vports[i]->cfg_lun_queue_depth <=
447 sdev->queue_depth)
448 continue;
James Smart5ffc2662009-11-18 15:39:44 -0500449 lpfc_change_queue_depth(sdev,
450 sdev->queue_depth+1,
451 SCSI_QDEPTH_RAMP_UP);
James Smart549e55c2007-08-02 11:09:51 -0400452 }
James Smart92d7f7b2007-06-17 19:56:38 -0500453 }
James Smart09372822008-01-11 01:52:54 -0500454 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500455 atomic_set(&phba->num_rsrc_err, 0);
456 atomic_set(&phba->num_cmd_success, 0);
457}
458
James Smarta8e497d2008-08-24 21:50:11 -0400459/**
James Smart3621a712009-04-06 18:47:14 -0400460 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400461 * @phba: Pointer to HBA context object.
462 *
463 * This function walks vport list and set each SCSI host to block state
464 * by invoking fc_remote_port_delete() routine. This function is invoked
465 * with EEH when device's PCI slot has been permanently disabled.
466 **/
467void
468lpfc_scsi_dev_block(struct lpfc_hba *phba)
469{
470 struct lpfc_vport **vports;
471 struct Scsi_Host *shost;
472 struct scsi_device *sdev;
473 struct fc_rport *rport;
474 int i;
475
476 vports = lpfc_create_vport_work_array(phba);
477 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400478 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400479 shost = lpfc_shost_from_vport(vports[i]);
480 shost_for_each_device(sdev, shost) {
481 rport = starget_to_rport(scsi_target(sdev));
482 fc_remote_port_delete(rport);
483 }
484 }
485 lpfc_destroy_vport_work_array(phba, vports);
486}
487
James Smart9bad7672008-12-04 22:39:02 -0500488/**
James Smart3772a992009-05-22 14:50:54 -0400489 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500490 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400491 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500492 *
James Smart3772a992009-05-22 14:50:54 -0400493 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
494 * the scsi buffer contains all the necessary information needed to initiate
495 * a SCSI I/O. The non-DMAable buffer region contains information to build
496 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
497 * and the initial BPL. In addition to allocating memory, the FCP CMND and
498 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500499 *
500 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400501 * int - number of scsi buffers that were allocated.
502 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500503 **/
James Smart3772a992009-05-22 14:50:54 -0400504static int
505lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500506{
James Smart2e0fef82007-06-17 19:56:36 -0500507 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500508 struct lpfc_scsi_buf *psb;
509 struct ulp_bde64 *bpl;
510 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400511 dma_addr_t pdma_phys_fcp_cmd;
512 dma_addr_t pdma_phys_fcp_rsp;
513 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500514 uint16_t iotag;
James Smart3772a992009-05-22 14:50:54 -0400515 int bcnt;
dea31012005-04-17 16:05:31 -0500516
James Smart3772a992009-05-22 14:50:54 -0400517 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
518 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
519 if (!psb)
520 break;
dea31012005-04-17 16:05:31 -0500521
James Smart3772a992009-05-22 14:50:54 -0400522 /*
523 * Get memory from the pci pool to map the virt space to pci
524 * bus space for an I/O. The DMA buffer includes space for the
525 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
526 * necessary to support the sg_tablesize.
527 */
528 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
529 GFP_KERNEL, &psb->dma_handle);
530 if (!psb->data) {
531 kfree(psb);
532 break;
533 }
dea31012005-04-17 16:05:31 -0500534
James Smart3772a992009-05-22 14:50:54 -0400535 /* Initialize virtual ptrs to dma_buf region. */
536 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
dea31012005-04-17 16:05:31 -0500537
James Smart3772a992009-05-22 14:50:54 -0400538 /* Allocate iotag for psb->cur_iocbq. */
539 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
540 if (iotag == 0) {
541 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
542 psb->data, psb->dma_handle);
543 kfree(psb);
544 break;
545 }
546 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500547
James Smart3772a992009-05-22 14:50:54 -0400548 psb->fcp_cmnd = psb->data;
549 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
550 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400551 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500552
James Smart3772a992009-05-22 14:50:54 -0400553 /* Initialize local short-hand pointers. */
554 bpl = psb->fcp_bpl;
555 pdma_phys_fcp_cmd = psb->dma_handle;
556 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
557 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
558 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500559
James Smart3772a992009-05-22 14:50:54 -0400560 /*
561 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
562 * are sg list bdes. Initialize the first two and leave the
563 * rest for queuecommand.
564 */
565 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
566 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
567 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
568 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
569 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500570
James Smart3772a992009-05-22 14:50:54 -0400571 /* Setup the physical region for the FCP RSP */
572 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
573 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
574 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
575 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
576 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
577
578 /*
579 * Since the IOCB for the FCP I/O is built into this
580 * lpfc_scsi_buf, initialize it with all known data now.
581 */
582 iocb = &psb->cur_iocbq.iocb;
583 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
584 if ((phba->sli_rev == 3) &&
585 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
586 /* fill in immediate fcp command BDE */
587 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
588 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
589 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
590 unsli3.fcp_ext.icd);
591 iocb->un.fcpi64.bdl.addrHigh = 0;
592 iocb->ulpBdeCount = 0;
593 iocb->ulpLe = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300594 /* fill in response BDE */
James Smart3772a992009-05-22 14:50:54 -0400595 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
596 BUFF_TYPE_BDE_64;
597 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
598 sizeof(struct fcp_rsp);
599 iocb->unsli3.fcp_ext.rbde.addrLow =
600 putPaddrLow(pdma_phys_fcp_rsp);
601 iocb->unsli3.fcp_ext.rbde.addrHigh =
602 putPaddrHigh(pdma_phys_fcp_rsp);
603 } else {
604 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
605 iocb->un.fcpi64.bdl.bdeSize =
606 (2 * sizeof(struct ulp_bde64));
607 iocb->un.fcpi64.bdl.addrLow =
608 putPaddrLow(pdma_phys_bpl);
609 iocb->un.fcpi64.bdl.addrHigh =
610 putPaddrHigh(pdma_phys_bpl);
611 iocb->ulpBdeCount = 1;
612 iocb->ulpLe = 1;
613 }
614 iocb->ulpClass = CLASS3;
615 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400616 /* Put it back into the SCSI buffer list */
James Smarteee88772010-09-29 11:19:08 -0400617 psb->cur_iocbq.context1 = psb;
James Smart1c6f4ef52009-11-18 15:40:49 -0500618 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400619
James Smart34b02dc2008-08-24 21:49:55 -0400620 }
dea31012005-04-17 16:05:31 -0500621
James Smart3772a992009-05-22 14:50:54 -0400622 return bcnt;
dea31012005-04-17 16:05:31 -0500623}
624
James Smart9bad7672008-12-04 22:39:02 -0500625/**
James Smart1151e3e2011-02-16 12:39:35 -0500626 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
627 * @vport: pointer to lpfc vport data structure.
628 *
629 * This routine is invoked by the vport cleanup for deletions and the cleanup
630 * for an ndlp on removal.
631 **/
632void
633lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
634{
635 struct lpfc_hba *phba = vport->phba;
636 struct lpfc_scsi_buf *psb, *next_psb;
637 unsigned long iflag = 0;
638
639 spin_lock_irqsave(&phba->hbalock, iflag);
640 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
641 list_for_each_entry_safe(psb, next_psb,
642 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
643 if (psb->rdata && psb->rdata->pnode
644 && psb->rdata->pnode->vport == vport)
645 psb->rdata = NULL;
646 }
647 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
648 spin_unlock_irqrestore(&phba->hbalock, iflag);
649}
650
651/**
James Smartda0436e2009-05-22 14:51:39 -0400652 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
653 * @phba: pointer to lpfc hba data structure.
654 * @axri: pointer to the fcp xri abort wcqe structure.
655 *
656 * This routine is invoked by the worker thread to process a SLI4 fast-path
657 * FCP aborted xri.
658 **/
659void
660lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
661 struct sli4_wcqe_xri_aborted *axri)
662{
663 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500664 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartda0436e2009-05-22 14:51:39 -0400665 struct lpfc_scsi_buf *psb, *next_psb;
666 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500667 struct lpfc_iocbq *iocbq;
668 int i;
James Smart19ca7602010-11-20 23:11:55 -0500669 struct lpfc_nodelist *ndlp;
670 int rrq_empty = 0;
James Smart589a52d2010-07-14 15:30:54 -0400671 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smartda0436e2009-05-22 14:51:39 -0400672
James Smart0f65ff62010-02-26 14:14:23 -0500673 spin_lock_irqsave(&phba->hbalock, iflag);
674 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400675 list_for_each_entry_safe(psb, next_psb,
676 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
677 if (psb->cur_iocbq.sli4_xritag == xri) {
678 list_del(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500679 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400680 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500681 spin_unlock(
682 &phba->sli4_hba.abts_scsi_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500683 if (psb->rdata && psb->rdata->pnode)
684 ndlp = psb->rdata->pnode;
685 else
686 ndlp = NULL;
687
James Smart19ca7602010-11-20 23:11:55 -0500688 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500689 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartcb69f7d2011-12-13 13:21:57 -0500690 if (ndlp) {
James Smart19ca7602010-11-20 23:11:55 -0500691 lpfc_set_rrq_active(phba, ndlp, xri, rxid, 1);
James Smartcb69f7d2011-12-13 13:21:57 -0500692 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
693 }
James Smartda0436e2009-05-22 14:51:39 -0400694 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500695 if (rrq_empty)
696 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400697 return;
698 }
699 }
James Smart0f65ff62010-02-26 14:14:23 -0500700 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
701 for (i = 1; i <= phba->sli.last_iotag; i++) {
702 iocbq = phba->sli.iocbq_lookup[i];
703
704 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
705 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
706 continue;
707 if (iocbq->sli4_xritag != xri)
708 continue;
709 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
710 psb->exch_busy = 0;
711 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart589a52d2010-07-14 15:30:54 -0400712 if (pring->txq_cnt)
713 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500714 return;
715
716 }
717 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400718}
719
720/**
James Smart8a9d2e82012-05-09 21:16:12 -0400721 * lpfc_sli4_post_scsi_sgl_list - Psot blocks of scsi buffer sgls from a list
722 * @phba: pointer to lpfc hba data structure.
723 * @post_sblist: pointer to the scsi buffer list.
724 *
725 * This routine walks a list of scsi buffers that was passed in. It attempts
726 * to construct blocks of scsi buffer sgls which contains contiguous xris and
727 * uses the non-embedded SGL block post mailbox commands to post to the port.
728 * For single SCSI buffer sgl with non-contiguous xri, if any, it shall use
729 * embedded SGL post mailbox command for posting. The @post_sblist passed in
730 * must be local list, thus no lock is needed when manipulate the list.
731 *
732 * Returns: 0 = failure, non-zero number of successfully posted buffers.
733 **/
734int
735lpfc_sli4_post_scsi_sgl_list(struct lpfc_hba *phba,
736 struct list_head *post_sblist, int sb_count)
737{
738 struct lpfc_scsi_buf *psb, *psb_next;
739 int status;
740 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
741 dma_addr_t pdma_phys_bpl1;
742 int last_xritag = NO_XRI;
743 LIST_HEAD(prep_sblist);
744 LIST_HEAD(blck_sblist);
745 LIST_HEAD(scsi_sblist);
746
747 /* sanity check */
748 if (sb_count <= 0)
749 return -EINVAL;
750
751 list_for_each_entry_safe(psb, psb_next, post_sblist, list) {
752 list_del_init(&psb->list);
753 block_cnt++;
754 if ((last_xritag != NO_XRI) &&
755 (psb->cur_iocbq.sli4_xritag != last_xritag + 1)) {
756 /* a hole in xri block, form a sgl posting block */
757 list_splice_init(&prep_sblist, &blck_sblist);
758 post_cnt = block_cnt - 1;
759 /* prepare list for next posting block */
760 list_add_tail(&psb->list, &prep_sblist);
761 block_cnt = 1;
762 } else {
763 /* prepare list for next posting block */
764 list_add_tail(&psb->list, &prep_sblist);
765 /* enough sgls for non-embed sgl mbox command */
766 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
767 list_splice_init(&prep_sblist, &blck_sblist);
768 post_cnt = block_cnt;
769 block_cnt = 0;
770 }
771 }
772 num_posting++;
773 last_xritag = psb->cur_iocbq.sli4_xritag;
774
775 /* end of repost sgl list condition for SCSI buffers */
776 if (num_posting == sb_count) {
777 if (post_cnt == 0) {
778 /* last sgl posting block */
779 list_splice_init(&prep_sblist, &blck_sblist);
780 post_cnt = block_cnt;
781 } else if (block_cnt == 1) {
782 /* last single sgl with non-contiguous xri */
783 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
784 pdma_phys_bpl1 = psb->dma_phys_bpl +
785 SGL_PAGE_SIZE;
786 else
787 pdma_phys_bpl1 = 0;
788 status = lpfc_sli4_post_sgl(phba,
789 psb->dma_phys_bpl,
790 pdma_phys_bpl1,
791 psb->cur_iocbq.sli4_xritag);
792 if (status) {
793 /* failure, put on abort scsi list */
794 psb->exch_busy = 1;
795 } else {
796 /* success, put on SCSI buffer list */
797 psb->exch_busy = 0;
798 psb->status = IOSTAT_SUCCESS;
799 num_posted++;
800 }
801 /* success, put on SCSI buffer sgl list */
802 list_add_tail(&psb->list, &scsi_sblist);
803 }
804 }
805
806 /* continue until a nembed page worth of sgls */
807 if (post_cnt == 0)
808 continue;
809
810 /* post block of SCSI buffer list sgls */
811 status = lpfc_sli4_post_scsi_sgl_block(phba, &blck_sblist,
812 post_cnt);
813
814 /* don't reset xirtag due to hole in xri block */
815 if (block_cnt == 0)
816 last_xritag = NO_XRI;
817
818 /* reset SCSI buffer post count for next round of posting */
819 post_cnt = 0;
820
821 /* put posted SCSI buffer-sgl posted on SCSI buffer sgl list */
822 while (!list_empty(&blck_sblist)) {
823 list_remove_head(&blck_sblist, psb,
824 struct lpfc_scsi_buf, list);
825 if (status) {
826 /* failure, put on abort scsi list */
827 psb->exch_busy = 1;
828 } else {
829 /* success, put on SCSI buffer list */
830 psb->exch_busy = 0;
831 psb->status = IOSTAT_SUCCESS;
832 num_posted++;
833 }
834 list_add_tail(&psb->list, &scsi_sblist);
835 }
836 }
837 /* Push SCSI buffers with sgl posted to the availble list */
838 while (!list_empty(&scsi_sblist)) {
839 list_remove_head(&scsi_sblist, psb,
840 struct lpfc_scsi_buf, list);
841 lpfc_release_scsi_buf_s4(phba, psb);
842 }
843 return num_posted;
844}
845
846/**
847 * lpfc_sli4_repost_scsi_sgl_list - Repsot all the allocated scsi buffer sgls
James Smartda0436e2009-05-22 14:51:39 -0400848 * @phba: pointer to lpfc hba data structure.
849 *
850 * This routine walks the list of scsi buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -0400851 * repost them to the port by using SGL block post. This is needed after a
James Smartda0436e2009-05-22 14:51:39 -0400852 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
853 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
854 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
855 *
856 * Returns: 0 = success, non-zero failure.
857 **/
858int
859lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
860{
James Smart8a9d2e82012-05-09 21:16:12 -0400861 LIST_HEAD(post_sblist);
862 int num_posted, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -0400863
James Smart8a9d2e82012-05-09 21:16:12 -0400864 /* get all SCSI buffers need to repost to a local list */
865 spin_lock(&phba->scsi_buf_list_lock);
866 list_splice_init(&phba->lpfc_scsi_buf_list, &post_sblist);
867 spin_unlock(&phba->scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400868
James Smart8a9d2e82012-05-09 21:16:12 -0400869 /* post the list of scsi buffer sgls to port if available */
870 if (!list_empty(&post_sblist)) {
871 num_posted = lpfc_sli4_post_scsi_sgl_list(phba, &post_sblist,
872 phba->sli4_hba.scsi_xri_cnt);
873 /* failed to post any scsi buffer, return error */
874 if (num_posted == 0)
875 rc = -EIO;
James Smartda0436e2009-05-22 14:51:39 -0400876 }
877 return rc;
878}
879
880/**
881 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
882 * @vport: The virtual port for which this call being executed.
883 * @num_to_allocate: The requested number of buffers to allocate.
884 *
James Smart8a9d2e82012-05-09 21:16:12 -0400885 * This routine allocates scsi buffers for device with SLI-4 interface spec,
James Smartda0436e2009-05-22 14:51:39 -0400886 * the scsi buffer contains all the necessary information needed to initiate
James Smart8a9d2e82012-05-09 21:16:12 -0400887 * a SCSI I/O. After allocating up to @num_to_allocate SCSI buffers and put
888 * them on a list, it post them to the port by using SGL block post.
James Smartda0436e2009-05-22 14:51:39 -0400889 *
890 * Return codes:
James Smart8a9d2e82012-05-09 21:16:12 -0400891 * int - number of scsi buffers that were allocated and posted.
James Smartda0436e2009-05-22 14:51:39 -0400892 * 0 = failure, less than num_to_alloc is a partial failure.
893 **/
894static int
895lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
896{
897 struct lpfc_hba *phba = vport->phba;
898 struct lpfc_scsi_buf *psb;
899 struct sli4_sge *sgl;
900 IOCB_t *iocb;
901 dma_addr_t pdma_phys_fcp_cmd;
902 dma_addr_t pdma_phys_fcp_rsp;
903 dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
James Smart8a9d2e82012-05-09 21:16:12 -0400904 uint16_t iotag, lxri = 0;
905 int bcnt, num_posted;
906 LIST_HEAD(prep_sblist);
907 LIST_HEAD(post_sblist);
908 LIST_HEAD(scsi_sblist);
James Smartda0436e2009-05-22 14:51:39 -0400909
910 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
911 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
912 if (!psb)
913 break;
James Smartda0436e2009-05-22 14:51:39 -0400914 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400915 * Get memory from the pci pool to map the virt space to
916 * pci bus space for an I/O. The DMA buffer includes space
917 * for the struct fcp_cmnd, struct fcp_rsp and the number
918 * of bde's necessary to support the sg_tablesize.
James Smartda0436e2009-05-22 14:51:39 -0400919 */
920 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
921 GFP_KERNEL, &psb->dma_handle);
922 if (!psb->data) {
923 kfree(psb);
924 break;
925 }
James Smartda0436e2009-05-22 14:51:39 -0400926 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
927
928 /* Allocate iotag for psb->cur_iocbq. */
929 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
930 if (iotag == 0) {
James Smartb92938b2010-06-07 15:24:12 -0400931 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
932 psb->data, psb->dma_handle);
James Smartda0436e2009-05-22 14:51:39 -0400933 kfree(psb);
934 break;
935 }
936
James Smart6d368e52011-05-24 11:44:12 -0400937 lxri = lpfc_sli4_next_xritag(phba);
938 if (lxri == NO_XRI) {
James Smartda0436e2009-05-22 14:51:39 -0400939 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
940 psb->data, psb->dma_handle);
941 kfree(psb);
942 break;
943 }
James Smart6d368e52011-05-24 11:44:12 -0400944 psb->cur_iocbq.sli4_lxritag = lxri;
945 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smartda0436e2009-05-22 14:51:39 -0400946 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Smartda0436e2009-05-22 14:51:39 -0400947 psb->fcp_bpl = psb->data;
948 psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
949 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
950 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
951 sizeof(struct fcp_cmnd));
952
953 /* Initialize local short-hand pointers. */
954 sgl = (struct sli4_sge *)psb->fcp_bpl;
955 pdma_phys_bpl = psb->dma_handle;
956 pdma_phys_fcp_cmd =
957 (psb->dma_handle + phba->cfg_sg_dma_buf_size)
958 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
959 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
960
961 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400962 * The first two bdes are the FCP_CMD and FCP_RSP.
963 * The balance are sg list bdes. Initialize the
964 * first two and leave the rest for queuecommand.
James Smartda0436e2009-05-22 14:51:39 -0400965 */
966 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
967 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
James Smart05580562011-05-24 11:40:48 -0400968 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400969 bf_set(lpfc_sli4_sge_last, sgl, 0);
970 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500971 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
James Smartda0436e2009-05-22 14:51:39 -0400972 sgl++;
973
974 /* Setup the physical region for the FCP RSP */
975 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
976 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smart05580562011-05-24 11:40:48 -0400977 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400978 bf_set(lpfc_sli4_sge_last, sgl, 1);
979 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500980 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -0400981
982 /*
983 * Since the IOCB for the FCP I/O is built into this
984 * lpfc_scsi_buf, initialize it with all known data now.
985 */
986 iocb = &psb->cur_iocbq.iocb;
987 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
988 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
989 /* setting the BLP size to 2 * sizeof BDE may not be correct.
990 * We are setting the bpl to point to out sgl. An sgl's
991 * entries are 16 bytes, a bpl entries are 12 bytes.
992 */
993 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
994 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
995 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
996 iocb->ulpBdeCount = 1;
997 iocb->ulpLe = 1;
998 iocb->ulpClass = CLASS3;
James Smart8a9d2e82012-05-09 21:16:12 -0400999 psb->cur_iocbq.context1 = psb;
James Smartda0436e2009-05-22 14:51:39 -04001000 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
1001 pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
1002 else
1003 pdma_phys_bpl1 = 0;
1004 psb->dma_phys_bpl = pdma_phys_bpl;
James Smart6d368e52011-05-24 11:44:12 -04001005
James Smart8a9d2e82012-05-09 21:16:12 -04001006 /* add the scsi buffer to a post list */
1007 list_add_tail(&psb->list, &post_sblist);
1008 spin_lock_irq(&phba->scsi_buf_list_lock);
1009 phba->sli4_hba.scsi_xri_cnt++;
1010 spin_unlock_irq(&phba->scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04001011 }
James Smart8a9d2e82012-05-09 21:16:12 -04001012 lpfc_printf_log(phba, KERN_INFO, LOG_BG,
1013 "3021 Allocate %d out of %d requested new SCSI "
1014 "buffers\n", bcnt, num_to_alloc);
James Smartda0436e2009-05-22 14:51:39 -04001015
James Smart8a9d2e82012-05-09 21:16:12 -04001016 /* post the list of scsi buffer sgls to port if available */
1017 if (!list_empty(&post_sblist))
1018 num_posted = lpfc_sli4_post_scsi_sgl_list(phba,
1019 &post_sblist, bcnt);
1020 else
1021 num_posted = 0;
1022
1023 return num_posted;
James Smartda0436e2009-05-22 14:51:39 -04001024}
1025
1026/**
James Smart3772a992009-05-22 14:50:54 -04001027 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
1028 * @vport: The virtual port for which this call being executed.
1029 * @num_to_allocate: The requested number of buffers to allocate.
1030 *
1031 * This routine wraps the actual SCSI buffer allocator function pointer from
1032 * the lpfc_hba struct.
1033 *
1034 * Return codes:
1035 * int - number of scsi buffers that were allocated.
1036 * 0 = failure, less than num_to_alloc is a partial failure.
1037 **/
1038static inline int
1039lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
1040{
1041 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
1042}
1043
1044/**
James Smart19ca7602010-11-20 23:11:55 -05001045 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -04001046 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05001047 *
1048 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1049 * and returns to caller.
1050 *
1051 * Return codes:
1052 * NULL - Error
1053 * Pointer to lpfc_scsi_buf - Success
1054 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +01001055static struct lpfc_scsi_buf*
James Smart19ca7602010-11-20 23:11:55 -05001056lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001057{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001058 struct lpfc_scsi_buf * lpfc_cmd = NULL;
1059 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001060 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001061
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001062 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001063 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -04001064 if (lpfc_cmd) {
1065 lpfc_cmd->seg_cnt = 0;
1066 lpfc_cmd->nonsg_phys = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001067 lpfc_cmd->prot_seg_cnt = 0;
James Smart1dcb58e2007-04-25 09:51:30 -04001068 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001069 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001070 return lpfc_cmd;
1071}
James Smart19ca7602010-11-20 23:11:55 -05001072/**
1073 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1074 * @phba: The HBA for which this call is being executed.
1075 *
1076 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1077 * and returns to caller.
1078 *
1079 * Return codes:
1080 * NULL - Error
1081 * Pointer to lpfc_scsi_buf - Success
1082 **/
1083static struct lpfc_scsi_buf*
1084lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1085{
James Smart1151e3e2011-02-16 12:39:35 -05001086 struct lpfc_scsi_buf *lpfc_cmd ;
James Smart19ca7602010-11-20 23:11:55 -05001087 unsigned long iflag = 0;
1088 int found = 0;
1089
1090 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -05001091 list_for_each_entry(lpfc_cmd, &phba->lpfc_scsi_buf_list,
1092 list) {
James Smart19ca7602010-11-20 23:11:55 -05001093 if (lpfc_test_rrq_active(phba, ndlp,
James Smart1151e3e2011-02-16 12:39:35 -05001094 lpfc_cmd->cur_iocbq.sli4_xritag))
1095 continue;
1096 list_del(&lpfc_cmd->list);
James Smart19ca7602010-11-20 23:11:55 -05001097 found = 1;
1098 lpfc_cmd->seg_cnt = 0;
1099 lpfc_cmd->nonsg_phys = 0;
1100 lpfc_cmd->prot_seg_cnt = 0;
James Smart1151e3e2011-02-16 12:39:35 -05001101 break;
James Smart19ca7602010-11-20 23:11:55 -05001102 }
James Smart1151e3e2011-02-16 12:39:35 -05001103 spin_unlock_irqrestore(&phba->scsi_buf_list_lock,
1104 iflag);
1105 if (!found)
1106 return NULL;
1107 else
1108 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001109}
1110/**
1111 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1112 * @phba: The HBA for which this call is being executed.
1113 *
1114 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1115 * and returns to caller.
1116 *
1117 * Return codes:
1118 * NULL - Error
1119 * Pointer to lpfc_scsi_buf - Success
1120 **/
1121static struct lpfc_scsi_buf*
1122lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1123{
1124 return phba->lpfc_get_scsi_buf(phba, ndlp);
1125}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001126
James Smart9bad7672008-12-04 22:39:02 -05001127/**
James Smart3772a992009-05-22 14:50:54 -04001128 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -05001129 * @phba: The Hba for which this call is being executed.
1130 * @psb: The scsi buffer which is being released.
1131 *
1132 * This routine releases @psb scsi buffer by adding it to tail of @phba
1133 * lpfc_scsi_buf_list list.
1134 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001135static void
James Smart3772a992009-05-22 14:50:54 -04001136lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001137{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001138 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001139
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001140 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001141 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -05001142 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001143 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -05001144}
1145
James Smart9bad7672008-12-04 22:39:02 -05001146/**
James Smartda0436e2009-05-22 14:51:39 -04001147 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1148 * @phba: The Hba for which this call is being executed.
1149 * @psb: The scsi buffer which is being released.
1150 *
1151 * This routine releases @psb scsi buffer by adding it to tail of @phba
1152 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1153 * and cannot be reused for at least RA_TOV amount of time if it was
1154 * aborted.
1155 **/
1156static void
1157lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1158{
1159 unsigned long iflag = 0;
1160
James Smart341af102010-01-26 23:07:37 -05001161 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -04001162 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1163 iflag);
1164 psb->pCmd = NULL;
1165 list_add_tail(&psb->list,
1166 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1167 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1168 iflag);
1169 } else {
1170
1171 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1172 psb->pCmd = NULL;
1173 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
1174 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
1175 }
1176}
1177
1178/**
James Smart3772a992009-05-22 14:50:54 -04001179 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1180 * @phba: The Hba for which this call is being executed.
1181 * @psb: The scsi buffer which is being released.
1182 *
1183 * This routine releases @psb scsi buffer by adding it to tail of @phba
1184 * lpfc_scsi_buf_list list.
1185 **/
1186static void
1187lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1188{
1189
1190 phba->lpfc_release_scsi_buf(phba, psb);
1191}
1192
1193/**
1194 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001195 * @phba: The Hba for which this call is being executed.
1196 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1197 *
1198 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001199 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1200 * through sg elements and format the bdea. This routine also initializes all
1201 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001202 *
1203 * Return codes:
1204 * 1 - Error
1205 * 0 - Success
1206 **/
dea31012005-04-17 16:05:31 -05001207static int
James Smart3772a992009-05-22 14:50:54 -04001208lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001209{
1210 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1211 struct scatterlist *sgel = NULL;
1212 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1213 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001214 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001215 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001216 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001217 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001218 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001219 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001220
1221 /*
1222 * There are three possibilities here - use scatter-gather segment, use
1223 * the single mapping, or neither. Start the lpfc command prep by
1224 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1225 * data bde entry.
1226 */
1227 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001228 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001229 /*
1230 * The driver stores the segment count returned from pci_map_sg
1231 * because this a count of dma-mappings used to map the use_sg
1232 * pages. They are not guaranteed to be the same for those
1233 * architectures that implement an IOMMU.
1234 */
dea31012005-04-17 16:05:31 -05001235
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001236 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1237 scsi_sg_count(scsi_cmnd), datadir);
1238 if (unlikely(!nseg))
1239 return 1;
1240
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001241 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001242 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001243 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1244 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001245 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001246 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001247 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001248 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001249 return 1;
1250 }
1251
1252 /*
1253 * The driver established a maximum scatter-gather segment count
1254 * during probe that limits the number of sg elements in any
1255 * single scsi command. Just run through the seg_cnt and format
1256 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001257 * When using SLI-3 the driver will try to fit all the BDEs into
1258 * the IOCB. If it can't then the BDEs get added to a BPL as it
1259 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001260 */
James Smart34b02dc2008-08-24 21:49:55 -04001261 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001262 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001263 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001264 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001265 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001266 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1267 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1268 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1269 data_bde->addrLow = putPaddrLow(physaddr);
1270 data_bde->addrHigh = putPaddrHigh(physaddr);
1271 data_bde++;
1272 } else {
1273 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1274 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1275 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1276 bpl->addrLow =
1277 le32_to_cpu(putPaddrLow(physaddr));
1278 bpl->addrHigh =
1279 le32_to_cpu(putPaddrHigh(physaddr));
1280 bpl++;
1281 }
dea31012005-04-17 16:05:31 -05001282 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001283 }
dea31012005-04-17 16:05:31 -05001284
1285 /*
1286 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001287 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1288 * explicitly reinitialized and for SLI-3 the extended bde count is
1289 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001290 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001291 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001292 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1293 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001294 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1295 /*
1296 * The extended IOCB format can only fit 3 BDE or a BPL.
1297 * This I/O has more than 3 BDE so the 1st data bde will
1298 * be a BPL that is filled in here.
1299 */
1300 physaddr = lpfc_cmd->dma_handle;
1301 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1302 data_bde->tus.f.bdeSize = (num_bde *
1303 sizeof(struct ulp_bde64));
1304 physaddr += (sizeof(struct fcp_cmnd) +
1305 sizeof(struct fcp_rsp) +
1306 (2 * sizeof(struct ulp_bde64)));
1307 data_bde->addrHigh = putPaddrHigh(physaddr);
1308 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001309 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -04001310 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1311 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001312 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -04001313 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1314 }
1315 } else {
1316 iocb_cmd->un.fcpi64.bdl.bdeSize =
1317 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001318 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001319 }
James Smart09372822008-01-11 01:52:54 -05001320 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001321
1322 /*
1323 * Due to difference in data length between DIF/non-DIF paths,
1324 * we need to set word 4 of IOCB here
1325 */
James Smarta257bf92009-04-06 18:48:10 -04001326 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001327 return 0;
1328}
1329
James Smartf9bb2da2011-10-10 21:34:11 -04001330static inline unsigned
1331lpfc_cmd_blksize(struct scsi_cmnd *sc)
1332{
1333 return sc->device->sector_size;
1334}
1335
1336#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001337
James Smart9a6b09c2012-03-01 22:37:42 -05001338/* Return if if error injection is detected by Initiator */
1339#define BG_ERR_INIT 0x1
1340/* Return if if error injection is detected by Target */
1341#define BG_ERR_TGT 0x2
1342/* Return if if swapping CSUM<-->CRC is required for error injection */
1343#define BG_ERR_SWAP 0x10
1344/* Return if disabling Guard/Ref/App checking is required for error injection */
1345#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -05001346
1347/**
1348 * lpfc_bg_err_inject - Determine if we should inject an error
1349 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -04001350 * @sc: The SCSI command to examine
1351 * @reftag: (out) BlockGuard reference tag for transmitted data
1352 * @apptag: (out) BlockGuard application tag for transmitted data
1353 * @new_guard (in) Value to replace CRC with if needed
1354 *
James Smart9a6b09c2012-03-01 22:37:42 -05001355 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -05001356 **/
James Smartf9bb2da2011-10-10 21:34:11 -04001357static int
1358lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1359 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1360{
1361 struct scatterlist *sgpe; /* s/g prot entry */
1362 struct scatterlist *sgde; /* s/g data entry */
James Smart9a6b09c2012-03-01 22:37:42 -05001363 struct lpfc_scsi_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -05001364 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -05001365 struct lpfc_nodelist *ndlp;
1366 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -04001367 uint32_t op = scsi_get_prot_op(sc);
1368 uint32_t blksize;
1369 uint32_t numblks;
1370 sector_t lba;
1371 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001372 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001373
1374 if (op == SCSI_PROT_NORMAL)
1375 return 0;
1376
James Smartacd68592012-01-18 16:25:09 -05001377 sgpe = scsi_prot_sglist(sc);
1378 sgde = scsi_sglist(sc);
James Smartf9bb2da2011-10-10 21:34:11 -04001379 lba = scsi_get_lba(sc);
James Smart4ac9b222012-03-01 22:38:29 -05001380
1381 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001382 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1383 blksize = lpfc_cmd_blksize(sc);
1384 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1385
1386 /* Make sure we have the right LBA if one is specified */
1387 if ((phba->lpfc_injerr_lba < lba) ||
1388 (phba->lpfc_injerr_lba >= (lba + numblks)))
1389 return 0;
James Smartacd68592012-01-18 16:25:09 -05001390 if (sgpe) {
1391 blockoff = phba->lpfc_injerr_lba - lba;
1392 numblks = sg_dma_len(sgpe) /
1393 sizeof(struct scsi_dif_tuple);
1394 if (numblks < blockoff)
1395 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001396 }
James Smartf9bb2da2011-10-10 21:34:11 -04001397 }
1398
James Smart4ac9b222012-03-01 22:38:29 -05001399 /* Next check if we need to match the remote NPortID or WWPN */
1400 rdata = sc->device->hostdata;
1401 if (rdata && rdata->pnode) {
1402 ndlp = rdata->pnode;
1403
1404 /* Make sure we have the right NPortID if one is specified */
1405 if (phba->lpfc_injerr_nportid &&
1406 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1407 return 0;
1408
1409 /*
1410 * Make sure we have the right WWPN if one is specified.
1411 * wwn[0] should be a non-zero NAA in a good WWPN.
1412 */
1413 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1414 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1415 sizeof(struct lpfc_name)) != 0))
1416 return 0;
1417 }
1418
1419 /* Setup a ptr to the protection data if the SCSI host provides it */
1420 if (sgpe) {
1421 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1422 src += blockoff;
1423 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1424 }
1425
James Smartf9bb2da2011-10-10 21:34:11 -04001426 /* Should we change the Reference Tag */
1427 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001428 if (phba->lpfc_injerr_wref_cnt) {
1429 switch (op) {
1430 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001431 if (src) {
1432 /*
1433 * For WRITE_PASS, force the error
1434 * to be sent on the wire. It should
1435 * be detected by the Target.
1436 * If blockoff != 0 error will be
1437 * inserted in middle of the IO.
1438 */
James Smartf9bb2da2011-10-10 21:34:11 -04001439
James Smartacd68592012-01-18 16:25:09 -05001440 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1441 "9076 BLKGRD: Injecting reftag error: "
1442 "write lba x%lx + x%x oldrefTag x%x\n",
1443 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001444 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001445
1446 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001447 * Save the old ref_tag so we can
1448 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001449 */
James Smart9a6b09c2012-03-01 22:37:42 -05001450 if (lpfc_cmd) {
1451 lpfc_cmd->prot_data_type =
1452 LPFC_INJERR_REFTAG;
1453 lpfc_cmd->prot_data_segment =
1454 src;
1455 lpfc_cmd->prot_data =
1456 src->ref_tag;
1457 }
1458 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001459 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001460 if (phba->lpfc_injerr_wref_cnt == 0) {
1461 phba->lpfc_injerr_nportid = 0;
1462 phba->lpfc_injerr_lba =
1463 LPFC_INJERR_LBA_OFF;
1464 memset(&phba->lpfc_injerr_wwpn,
1465 0, sizeof(struct lpfc_name));
1466 }
James Smart9a6b09c2012-03-01 22:37:42 -05001467 rc = BG_ERR_TGT | BG_ERR_CHECK;
1468
James Smartacd68592012-01-18 16:25:09 -05001469 break;
1470 }
1471 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001472 case SCSI_PROT_WRITE_INSERT:
1473 /*
1474 * For WRITE_INSERT, force the error
1475 * to be sent on the wire. It should be
1476 * detected by the Target.
1477 */
1478 /* DEADBEEF will be the reftag on the wire */
1479 *reftag = 0xDEADBEEF;
1480 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001481 if (phba->lpfc_injerr_wref_cnt == 0) {
1482 phba->lpfc_injerr_nportid = 0;
1483 phba->lpfc_injerr_lba =
1484 LPFC_INJERR_LBA_OFF;
1485 memset(&phba->lpfc_injerr_wwpn,
1486 0, sizeof(struct lpfc_name));
1487 }
James Smart9a6b09c2012-03-01 22:37:42 -05001488 rc = BG_ERR_TGT | BG_ERR_CHECK;
1489
1490 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1491 "9078 BLKGRD: Injecting reftag error: "
1492 "write lba x%lx\n", (unsigned long)lba);
1493 break;
James Smartacd68592012-01-18 16:25:09 -05001494 case SCSI_PROT_WRITE_STRIP:
1495 /*
1496 * For WRITE_STRIP and WRITE_PASS,
1497 * force the error on data
1498 * being copied from SLI-Host to SLI-Port.
1499 */
1500 *reftag = 0xDEADBEEF;
1501 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001502 if (phba->lpfc_injerr_wref_cnt == 0) {
1503 phba->lpfc_injerr_nportid = 0;
1504 phba->lpfc_injerr_lba =
1505 LPFC_INJERR_LBA_OFF;
1506 memset(&phba->lpfc_injerr_wwpn,
1507 0, sizeof(struct lpfc_name));
1508 }
James Smartacd68592012-01-18 16:25:09 -05001509 rc = BG_ERR_INIT;
1510
1511 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1512 "9077 BLKGRD: Injecting reftag error: "
1513 "write lba x%lx\n", (unsigned long)lba);
1514 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001515 }
James Smartacd68592012-01-18 16:25:09 -05001516 }
1517 if (phba->lpfc_injerr_rref_cnt) {
1518 switch (op) {
1519 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001520 case SCSI_PROT_READ_STRIP:
1521 case SCSI_PROT_READ_PASS:
1522 /*
1523 * For READ_STRIP and READ_PASS, force the
1524 * error on data being read off the wire. It
1525 * should force an IO error to the driver.
1526 */
James Smartf9bb2da2011-10-10 21:34:11 -04001527 *reftag = 0xDEADBEEF;
1528 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001529 if (phba->lpfc_injerr_rref_cnt == 0) {
1530 phba->lpfc_injerr_nportid = 0;
1531 phba->lpfc_injerr_lba =
1532 LPFC_INJERR_LBA_OFF;
1533 memset(&phba->lpfc_injerr_wwpn,
1534 0, sizeof(struct lpfc_name));
1535 }
James Smartacd68592012-01-18 16:25:09 -05001536 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001537
1538 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001539 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001540 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001541 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001542 }
1543 }
1544 }
1545
1546 /* Should we change the Application Tag */
1547 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001548 if (phba->lpfc_injerr_wapp_cnt) {
1549 switch (op) {
1550 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001551 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001552 /*
1553 * For WRITE_PASS, force the error
1554 * to be sent on the wire. It should
1555 * be detected by the Target.
1556 * If blockoff != 0 error will be
1557 * inserted in middle of the IO.
1558 */
1559
James Smartacd68592012-01-18 16:25:09 -05001560 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1561 "9080 BLKGRD: Injecting apptag error: "
1562 "write lba x%lx + x%x oldappTag x%x\n",
1563 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001564 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001565
1566 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001567 * Save the old app_tag so we can
1568 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001569 */
James Smart9a6b09c2012-03-01 22:37:42 -05001570 if (lpfc_cmd) {
1571 lpfc_cmd->prot_data_type =
1572 LPFC_INJERR_APPTAG;
1573 lpfc_cmd->prot_data_segment =
1574 src;
1575 lpfc_cmd->prot_data =
1576 src->app_tag;
1577 }
1578 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001579 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001580 if (phba->lpfc_injerr_wapp_cnt == 0) {
1581 phba->lpfc_injerr_nportid = 0;
1582 phba->lpfc_injerr_lba =
1583 LPFC_INJERR_LBA_OFF;
1584 memset(&phba->lpfc_injerr_wwpn,
1585 0, sizeof(struct lpfc_name));
1586 }
James Smart9a6b09c2012-03-01 22:37:42 -05001587 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001588 break;
1589 }
1590 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001591 case SCSI_PROT_WRITE_INSERT:
1592 /*
1593 * For WRITE_INSERT, force the
1594 * error to be sent on the wire. It should be
1595 * detected by the Target.
1596 */
1597 /* DEAD will be the apptag on the wire */
1598 *apptag = 0xDEAD;
1599 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001600 if (phba->lpfc_injerr_wapp_cnt == 0) {
1601 phba->lpfc_injerr_nportid = 0;
1602 phba->lpfc_injerr_lba =
1603 LPFC_INJERR_LBA_OFF;
1604 memset(&phba->lpfc_injerr_wwpn,
1605 0, sizeof(struct lpfc_name));
1606 }
James Smart9a6b09c2012-03-01 22:37:42 -05001607 rc = BG_ERR_TGT | BG_ERR_CHECK;
1608
1609 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1610 "0813 BLKGRD: Injecting apptag error: "
1611 "write lba x%lx\n", (unsigned long)lba);
1612 break;
James Smartacd68592012-01-18 16:25:09 -05001613 case SCSI_PROT_WRITE_STRIP:
1614 /*
1615 * For WRITE_STRIP and WRITE_PASS,
1616 * force the error on data
1617 * being copied from SLI-Host to SLI-Port.
1618 */
1619 *apptag = 0xDEAD;
1620 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001621 if (phba->lpfc_injerr_wapp_cnt == 0) {
1622 phba->lpfc_injerr_nportid = 0;
1623 phba->lpfc_injerr_lba =
1624 LPFC_INJERR_LBA_OFF;
1625 memset(&phba->lpfc_injerr_wwpn,
1626 0, sizeof(struct lpfc_name));
1627 }
James Smartacd68592012-01-18 16:25:09 -05001628 rc = BG_ERR_INIT;
1629
1630 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1631 "0812 BLKGRD: Injecting apptag error: "
1632 "write lba x%lx\n", (unsigned long)lba);
1633 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001634 }
James Smartacd68592012-01-18 16:25:09 -05001635 }
1636 if (phba->lpfc_injerr_rapp_cnt) {
1637 switch (op) {
1638 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001639 case SCSI_PROT_READ_STRIP:
1640 case SCSI_PROT_READ_PASS:
1641 /*
1642 * For READ_STRIP and READ_PASS, force the
1643 * error on data being read off the wire. It
1644 * should force an IO error to the driver.
1645 */
James Smartf9bb2da2011-10-10 21:34:11 -04001646 *apptag = 0xDEAD;
1647 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001648 if (phba->lpfc_injerr_rapp_cnt == 0) {
1649 phba->lpfc_injerr_nportid = 0;
1650 phba->lpfc_injerr_lba =
1651 LPFC_INJERR_LBA_OFF;
1652 memset(&phba->lpfc_injerr_wwpn,
1653 0, sizeof(struct lpfc_name));
1654 }
James Smartacd68592012-01-18 16:25:09 -05001655 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001656
1657 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001658 "0814 BLKGRD: Injecting apptag error: "
1659 "read lba x%lx\n", (unsigned long)lba);
1660 break;
1661 }
1662 }
1663 }
1664
1665
1666 /* Should we change the Guard Tag */
1667 if (new_guard) {
1668 if (phba->lpfc_injerr_wgrd_cnt) {
1669 switch (op) {
1670 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001671 rc = BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001672 /* Drop thru */
James Smartacd68592012-01-18 16:25:09 -05001673
James Smartacd68592012-01-18 16:25:09 -05001674 case SCSI_PROT_WRITE_INSERT:
1675 /*
1676 * For WRITE_INSERT, force the
1677 * error to be sent on the wire. It should be
1678 * detected by the Target.
1679 */
1680 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001681 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1682 phba->lpfc_injerr_nportid = 0;
1683 phba->lpfc_injerr_lba =
1684 LPFC_INJERR_LBA_OFF;
1685 memset(&phba->lpfc_injerr_wwpn,
1686 0, sizeof(struct lpfc_name));
1687 }
James Smartacd68592012-01-18 16:25:09 -05001688
James Smart9a6b09c2012-03-01 22:37:42 -05001689 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001690 /* Signals the caller to swap CRC->CSUM */
1691
1692 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1693 "0817 BLKGRD: Injecting guard error: "
1694 "write lba x%lx\n", (unsigned long)lba);
1695 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001696 case SCSI_PROT_WRITE_STRIP:
1697 /*
1698 * For WRITE_STRIP and WRITE_PASS,
1699 * force the error on data
1700 * being copied from SLI-Host to SLI-Port.
1701 */
1702 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001703 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1704 phba->lpfc_injerr_nportid = 0;
1705 phba->lpfc_injerr_lba =
1706 LPFC_INJERR_LBA_OFF;
1707 memset(&phba->lpfc_injerr_wwpn,
1708 0, sizeof(struct lpfc_name));
1709 }
James Smart9a6b09c2012-03-01 22:37:42 -05001710
1711 rc = BG_ERR_INIT | BG_ERR_SWAP;
1712 /* Signals the caller to swap CRC->CSUM */
1713
1714 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1715 "0816 BLKGRD: Injecting guard error: "
1716 "write lba x%lx\n", (unsigned long)lba);
1717 break;
James Smartacd68592012-01-18 16:25:09 -05001718 }
1719 }
1720 if (phba->lpfc_injerr_rgrd_cnt) {
1721 switch (op) {
1722 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001723 case SCSI_PROT_READ_STRIP:
1724 case SCSI_PROT_READ_PASS:
1725 /*
1726 * For READ_STRIP and READ_PASS, force the
1727 * error on data being read off the wire. It
1728 * should force an IO error to the driver.
1729 */
James Smartacd68592012-01-18 16:25:09 -05001730 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001731 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1732 phba->lpfc_injerr_nportid = 0;
1733 phba->lpfc_injerr_lba =
1734 LPFC_INJERR_LBA_OFF;
1735 memset(&phba->lpfc_injerr_wwpn,
1736 0, sizeof(struct lpfc_name));
1737 }
James Smartacd68592012-01-18 16:25:09 -05001738
James Smart9a6b09c2012-03-01 22:37:42 -05001739 rc = BG_ERR_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001740 /* Signals the caller to swap CRC->CSUM */
1741
1742 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1743 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001744 "read lba x%lx\n", (unsigned long)lba);
1745 }
1746 }
1747 }
1748
James Smartf9bb2da2011-10-10 21:34:11 -04001749 return rc;
1750}
1751#endif
1752
James Smartacd68592012-01-18 16:25:09 -05001753/**
1754 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1755 * the specified SCSI command.
1756 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001757 * @sc: The SCSI command to examine
1758 * @txopt: (out) BlockGuard operation for transmitted data
1759 * @rxopt: (out) BlockGuard operation for received data
1760 *
1761 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1762 *
James Smartacd68592012-01-18 16:25:09 -05001763 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001764static int
James Smart6c8eea52010-04-06 14:49:53 -04001765lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1766 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001767{
1768 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
James Smart6c8eea52010-04-06 14:49:53 -04001769 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001770
1771 if (guard_type == SHOST_DIX_GUARD_IP) {
1772 switch (scsi_get_prot_op(sc)) {
1773 case SCSI_PROT_READ_INSERT:
1774 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001775 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001776 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001777 break;
1778
1779 case SCSI_PROT_READ_STRIP:
1780 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001781 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001782 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001783 break;
1784
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001785 case SCSI_PROT_READ_PASS:
1786 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001787 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001788 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001789 break;
1790
James Smarte2a0a9d2008-12-04 22:40:02 -05001791 case SCSI_PROT_NORMAL:
1792 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001793 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001794 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1795 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001796 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001797 break;
1798
1799 }
James Smart7c56b9f2011-07-22 18:36:25 -04001800 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001801 switch (scsi_get_prot_op(sc)) {
1802 case SCSI_PROT_READ_STRIP:
1803 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001804 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001805 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001806 break;
1807
1808 case SCSI_PROT_READ_PASS:
1809 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001810 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001811 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001812 break;
1813
James Smarte2a0a9d2008-12-04 22:40:02 -05001814 case SCSI_PROT_READ_INSERT:
1815 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001816 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001817 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001818 break;
1819
James Smarte2a0a9d2008-12-04 22:40:02 -05001820 case SCSI_PROT_NORMAL:
1821 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001822 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001823 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1824 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001825 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001826 break;
1827 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001828 }
1829
James Smart6c8eea52010-04-06 14:49:53 -04001830 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001831}
1832
James Smartacd68592012-01-18 16:25:09 -05001833#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1834/**
1835 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1836 * the specified SCSI command in order to force a guard tag error.
1837 * @phba: The Hba for which this call is being executed.
1838 * @sc: The SCSI command to examine
1839 * @txopt: (out) BlockGuard operation for transmitted data
1840 * @rxopt: (out) BlockGuard operation for received data
1841 *
1842 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1843 *
1844 **/
1845static int
1846lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1847 uint8_t *txop, uint8_t *rxop)
1848{
1849 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
1850 uint8_t ret = 0;
1851
1852 if (guard_type == SHOST_DIX_GUARD_IP) {
1853 switch (scsi_get_prot_op(sc)) {
1854 case SCSI_PROT_READ_INSERT:
1855 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001856 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001857 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001858 break;
1859
1860 case SCSI_PROT_READ_STRIP:
1861 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001862 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001863 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001864 break;
1865
1866 case SCSI_PROT_READ_PASS:
1867 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001868 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001869 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001870 break;
1871
1872 case SCSI_PROT_NORMAL:
1873 default:
1874 break;
1875
1876 }
1877 } else {
1878 switch (scsi_get_prot_op(sc)) {
1879 case SCSI_PROT_READ_STRIP:
1880 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001881 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001882 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001883 break;
1884
1885 case SCSI_PROT_READ_PASS:
1886 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001887 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05001888 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001889 break;
1890
1891 case SCSI_PROT_READ_INSERT:
1892 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001893 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001894 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001895 break;
1896
1897 case SCSI_PROT_NORMAL:
1898 default:
1899 break;
1900 }
1901 }
1902
1903 return ret;
1904}
1905#endif
1906
1907/**
1908 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1909 * @phba: The Hba for which this call is being executed.
1910 * @sc: pointer to scsi command we're working on
1911 * @bpl: pointer to buffer list for protection groups
1912 * @datacnt: number of segments of data that have been dma mapped
1913 *
1914 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05001915 * type LPFC_PG_TYPE_NO_DIF
1916 *
1917 * This is usually used when the HBA is instructed to generate
1918 * DIFs and insert them into data stream (or strip DIF from
1919 * incoming data stream)
1920 *
1921 * The buffer list consists of just one protection group described
1922 * below:
1923 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001924 * start of prot group --> | PDE_5 |
1925 * +-------------------------+
1926 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001927 * +-------------------------+
1928 * | Data BDE |
1929 * +-------------------------+
1930 * |more Data BDE's ... (opt)|
1931 * +-------------------------+
1932 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001933 *
1934 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05001935 *
1936 * Returns the number of BDEs added to the BPL.
1937 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001938static int
1939lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1940 struct ulp_bde64 *bpl, int datasegcnt)
1941{
1942 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001943 struct lpfc_pde5 *pde5 = NULL;
1944 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001945 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001946 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001947 int datadir = sc->sc_data_direction;
James Smart0829a192012-05-09 21:18:12 -04001948#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001949 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001950#endif
James Smartacd68592012-01-18 16:25:09 -05001951 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001952 uint32_t reftag;
James Smart7c56b9f2011-07-22 18:36:25 -04001953 unsigned blksize;
James Smart6c8eea52010-04-06 14:49:53 -04001954 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001955
James Smart6c8eea52010-04-06 14:49:53 -04001956 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1957 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001958 goto out;
1959
James Smart6c8eea52010-04-06 14:49:53 -04001960 /* extract some info from the scsi command for pde*/
James Smarte2a0a9d2008-12-04 22:40:02 -05001961 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05001962 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05001963
James Smartf9bb2da2011-10-10 21:34:11 -04001964#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001965 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001966 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001967 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001968 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001969 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001970 checking = 0;
1971 }
James Smartf9bb2da2011-10-10 21:34:11 -04001972#endif
1973
James Smart6c8eea52010-04-06 14:49:53 -04001974 /* setup PDE5 with what we have */
1975 pde5 = (struct lpfc_pde5 *) bpl;
1976 memset(pde5, 0, sizeof(struct lpfc_pde5));
1977 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001978
James Smartbc739052010-08-04 16:11:18 -04001979 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001980 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001981 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001982
James Smart6c8eea52010-04-06 14:49:53 -04001983 /* advance bpl and increment bde count */
1984 num_bde++;
1985 bpl++;
1986 pde6 = (struct lpfc_pde6 *) bpl;
1987
1988 /* setup PDE6 with the rest of the info */
1989 memset(pde6, 0, sizeof(struct lpfc_pde6));
1990 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1991 bf_set(pde6_optx, pde6, txop);
1992 bf_set(pde6_oprx, pde6, rxop);
1993 if (datadir == DMA_FROM_DEVICE) {
James Smartacd68592012-01-18 16:25:09 -05001994 bf_set(pde6_ce, pde6, checking);
1995 bf_set(pde6_re, pde6, checking);
James Smart6c8eea52010-04-06 14:49:53 -04001996 }
1997 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001998 bf_set(pde6_ae, pde6, 0);
1999 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002000
James Smartbc739052010-08-04 16:11:18 -04002001 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002002 pde6->word0 = cpu_to_le32(pde6->word0);
2003 pde6->word1 = cpu_to_le32(pde6->word1);
2004 pde6->word2 = cpu_to_le32(pde6->word2);
2005
James Smart6c8eea52010-04-06 14:49:53 -04002006 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002007 num_bde++;
2008 bpl++;
2009
2010 /* assumption: caller has already run dma_map_sg on command data */
2011 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2012 physaddr = sg_dma_address(sgde);
2013 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
2014 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2015 bpl->tus.f.bdeSize = sg_dma_len(sgde);
2016 if (datadir == DMA_TO_DEVICE)
2017 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2018 else
2019 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2020 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2021 bpl++;
2022 num_bde++;
2023 }
2024
2025out:
2026 return num_bde;
2027}
2028
James Smartacd68592012-01-18 16:25:09 -05002029/**
2030 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
2031 * @phba: The Hba for which this call is being executed.
2032 * @sc: pointer to scsi command we're working on
2033 * @bpl: pointer to buffer list for protection groups
2034 * @datacnt: number of segments of data that have been dma mapped
2035 * @protcnt: number of segment of protection data that have been dma mapped
2036 *
2037 * This function sets up BPL buffer list for protection groups of
2038 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05002039 *
2040 * This is usually used when DIFs are in their own buffers,
2041 * separate from the data. The HBA can then by instructed
2042 * to place the DIFs in the outgoing stream. For read operations,
2043 * The HBA could extract the DIFs and place it in DIF buffers.
2044 *
2045 * The buffer list for this type consists of one or more of the
2046 * protection groups described below:
2047 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002048 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002049 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002050 * | PDE_6 |
2051 * +-------------------------+
2052 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05002053 * +-------------------------+
2054 * | Data BDE |
2055 * +-------------------------+
2056 * |more Data BDE's ... (opt)|
2057 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002058 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002059 * +-------------------------+
2060 * | ... |
2061 * +-------------------------+
2062 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002063 * Note: It is assumed that both data and protection s/g buffers have been
2064 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05002065 *
2066 * Returns the number of BDEs added to the BPL.
2067 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002068static int
2069lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2070 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2071{
2072 struct scatterlist *sgde = NULL; /* s/g data entry */
2073 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04002074 struct lpfc_pde5 *pde5 = NULL;
2075 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05002076 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002077 dma_addr_t dataphysaddr, protphysaddr;
2078 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05002079 unsigned int split_offset;
2080 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05002081 unsigned int protgrp_blks, protgrp_bytes;
2082 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04002083 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002084 int datadir = sc->sc_data_direction;
2085 unsigned char pgdone = 0, alldone = 0;
2086 unsigned blksize;
James Smart0829a192012-05-09 21:18:12 -04002087#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002088 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002089#endif
James Smartacd68592012-01-18 16:25:09 -05002090 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002091 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04002092 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002093 int num_bde = 0;
2094
2095 sgpe = scsi_prot_sglist(sc);
2096 sgde = scsi_sglist(sc);
2097
2098 if (!sgpe || !sgde) {
2099 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2100 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2101 sgpe, sgde);
2102 return 0;
2103 }
2104
James Smart6c8eea52010-04-06 14:49:53 -04002105 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2106 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002107 goto out;
2108
James Smart6c8eea52010-04-06 14:49:53 -04002109 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05002110 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002111 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002112
James Smartf9bb2da2011-10-10 21:34:11 -04002113#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002114 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002115 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002116 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002117 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002118 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002119 checking = 0;
2120 }
James Smartf9bb2da2011-10-10 21:34:11 -04002121#endif
2122
James Smarte2a0a9d2008-12-04 22:40:02 -05002123 split_offset = 0;
2124 do {
James Smart6c8eea52010-04-06 14:49:53 -04002125 /* setup PDE5 with what we have */
2126 pde5 = (struct lpfc_pde5 *) bpl;
2127 memset(pde5, 0, sizeof(struct lpfc_pde5));
2128 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002129
James Smartbc739052010-08-04 16:11:18 -04002130 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002131 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002132 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002133
James Smart6c8eea52010-04-06 14:49:53 -04002134 /* advance bpl and increment bde count */
2135 num_bde++;
2136 bpl++;
2137 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002138
James Smart6c8eea52010-04-06 14:49:53 -04002139 /* setup PDE6 with the rest of the info */
2140 memset(pde6, 0, sizeof(struct lpfc_pde6));
2141 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2142 bf_set(pde6_optx, pde6, txop);
2143 bf_set(pde6_oprx, pde6, rxop);
James Smartacd68592012-01-18 16:25:09 -05002144 bf_set(pde6_ce, pde6, checking);
2145 bf_set(pde6_re, pde6, checking);
James Smart6c8eea52010-04-06 14:49:53 -04002146 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002147 bf_set(pde6_ae, pde6, 0);
2148 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002149
James Smartbc739052010-08-04 16:11:18 -04002150 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002151 pde6->word0 = cpu_to_le32(pde6->word0);
2152 pde6->word1 = cpu_to_le32(pde6->word1);
2153 pde6->word2 = cpu_to_le32(pde6->word2);
2154
James Smart6c8eea52010-04-06 14:49:53 -04002155 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002156 num_bde++;
2157 bpl++;
2158
2159 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05002160 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2161 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05002162
James Smarte2a0a9d2008-12-04 22:40:02 -05002163 /* must be integer multiple of the DIF block length */
2164 BUG_ON(protgroup_len % 8);
2165
James Smart7f860592011-03-11 16:05:52 -05002166 pde7 = (struct lpfc_pde7 *) bpl;
2167 memset(pde7, 0, sizeof(struct lpfc_pde7));
2168 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2169
James Smart7c56b9f2011-07-22 18:36:25 -04002170 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2171 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05002172
James Smarte2a0a9d2008-12-04 22:40:02 -05002173 protgrp_blks = protgroup_len / 8;
2174 protgrp_bytes = protgrp_blks * blksize;
2175
James Smart7f860592011-03-11 16:05:52 -05002176 /* check if this pde is crossing the 4K boundary; if so split */
2177 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2178 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2179 protgroup_offset += protgroup_remainder;
2180 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04002181 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05002182 } else {
2183 protgroup_offset = 0;
2184 curr_prot++;
2185 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002186
James Smarte2a0a9d2008-12-04 22:40:02 -05002187 num_bde++;
2188
2189 /* setup BDE's for data blocks associated with DIF data */
2190 pgdone = 0;
2191 subtotal = 0; /* total bytes processed for current prot grp */
2192 while (!pgdone) {
2193 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04002194 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2195 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002196 __func__);
2197 return 0;
2198 }
2199 bpl++;
2200 dataphysaddr = sg_dma_address(sgde) + split_offset;
2201 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2202 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2203
2204 remainder = sg_dma_len(sgde) - split_offset;
2205
2206 if ((subtotal + remainder) <= protgrp_bytes) {
2207 /* we can use this whole buffer */
2208 bpl->tus.f.bdeSize = remainder;
2209 split_offset = 0;
2210
2211 if ((subtotal + remainder) == protgrp_bytes)
2212 pgdone = 1;
2213 } else {
2214 /* must split this buffer with next prot grp */
2215 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2216 split_offset += bpl->tus.f.bdeSize;
2217 }
2218
2219 subtotal += bpl->tus.f.bdeSize;
2220
2221 if (datadir == DMA_TO_DEVICE)
2222 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2223 else
2224 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2225 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2226
2227 num_bde++;
2228 curr_data++;
2229
2230 if (split_offset)
2231 break;
2232
2233 /* Move to the next s/g segment if possible */
2234 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04002235
James Smarte2a0a9d2008-12-04 22:40:02 -05002236 }
2237
James Smart7f860592011-03-11 16:05:52 -05002238 if (protgroup_offset) {
2239 /* update the reference tag */
2240 reftag += protgrp_blks;
2241 bpl++;
2242 continue;
2243 }
2244
James Smarte2a0a9d2008-12-04 22:40:02 -05002245 /* are we done ? */
2246 if (curr_prot == protcnt) {
2247 alldone = 1;
2248 } else if (curr_prot < protcnt) {
2249 /* advance to next prot buffer */
2250 sgpe = sg_next(sgpe);
2251 bpl++;
2252
2253 /* update the reference tag */
2254 reftag += protgrp_blks;
2255 } else {
2256 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04002257 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2258 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05002259 }
2260
2261 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05002262out:
2263
James Smarte2a0a9d2008-12-04 22:40:02 -05002264 return num_bde;
2265}
James Smart7f860592011-03-11 16:05:52 -05002266
James Smartacd68592012-01-18 16:25:09 -05002267/**
2268 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2269 * @phba: The Hba for which this call is being executed.
2270 * @sc: pointer to scsi command we're working on
2271 * @sgl: pointer to buffer list for protection groups
2272 * @datacnt: number of segments of data that have been dma mapped
2273 *
2274 * This function sets up SGL buffer list for protection groups of
2275 * type LPFC_PG_TYPE_NO_DIF
2276 *
2277 * This is usually used when the HBA is instructed to generate
2278 * DIFs and insert them into data stream (or strip DIF from
2279 * incoming data stream)
2280 *
2281 * The buffer list consists of just one protection group described
2282 * below:
2283 * +-------------------------+
2284 * start of prot group --> | DI_SEED |
2285 * +-------------------------+
2286 * | Data SGE |
2287 * +-------------------------+
2288 * |more Data SGE's ... (opt)|
2289 * +-------------------------+
2290 *
2291 *
2292 * Note: Data s/g buffers have been dma mapped
2293 *
2294 * Returns the number of SGEs added to the SGL.
2295 **/
2296static int
2297lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2298 struct sli4_sge *sgl, int datasegcnt)
2299{
2300 struct scatterlist *sgde = NULL; /* s/g data entry */
2301 struct sli4_sge_diseed *diseed = NULL;
2302 dma_addr_t physaddr;
2303 int i = 0, num_sge = 0, status;
2304 int datadir = sc->sc_data_direction;
2305 uint32_t reftag;
2306 unsigned blksize;
2307 uint8_t txop, rxop;
James Smart0829a192012-05-09 21:18:12 -04002308#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002309 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002310#endif
James Smartacd68592012-01-18 16:25:09 -05002311 uint32_t checking = 1;
2312 uint32_t dma_len;
2313 uint32_t dma_offset = 0;
2314
2315 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2316 if (status)
2317 goto out;
2318
2319 /* extract some info from the scsi command for pde*/
2320 blksize = lpfc_cmd_blksize(sc);
2321 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2322
2323#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002324 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002325 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002326 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002327 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002328 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002329 checking = 0;
2330 }
2331#endif
2332
2333 /* setup DISEED with what we have */
2334 diseed = (struct sli4_sge_diseed *) sgl;
2335 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2336 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2337
2338 /* Endianness conversion if necessary */
2339 diseed->ref_tag = cpu_to_le32(reftag);
2340 diseed->ref_tag_tran = diseed->ref_tag;
2341
2342 /* setup DISEED with the rest of the info */
2343 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2344 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2345 if (datadir == DMA_FROM_DEVICE) {
2346 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2347 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2348 }
2349 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2350 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2351
2352 /* Endianness conversion if necessary for DISEED */
2353 diseed->word2 = cpu_to_le32(diseed->word2);
2354 diseed->word3 = cpu_to_le32(diseed->word3);
2355
2356 /* advance bpl and increment sge count */
2357 num_sge++;
2358 sgl++;
2359
2360 /* assumption: caller has already run dma_map_sg on command data */
2361 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2362 physaddr = sg_dma_address(sgde);
2363 dma_len = sg_dma_len(sgde);
2364 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2365 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2366 if ((i + 1) == datasegcnt)
2367 bf_set(lpfc_sli4_sge_last, sgl, 1);
2368 else
2369 bf_set(lpfc_sli4_sge_last, sgl, 0);
2370 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2371 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2372
2373 sgl->sge_len = cpu_to_le32(dma_len);
2374 dma_offset += dma_len;
2375
2376 sgl++;
2377 num_sge++;
2378 }
2379
2380out:
2381 return num_sge;
2382}
2383
2384/**
2385 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2386 * @phba: The Hba for which this call is being executed.
2387 * @sc: pointer to scsi command we're working on
2388 * @sgl: pointer to buffer list for protection groups
2389 * @datacnt: number of segments of data that have been dma mapped
2390 * @protcnt: number of segment of protection data that have been dma mapped
2391 *
2392 * This function sets up SGL buffer list for protection groups of
2393 * type LPFC_PG_TYPE_DIF
2394 *
2395 * This is usually used when DIFs are in their own buffers,
2396 * separate from the data. The HBA can then by instructed
2397 * to place the DIFs in the outgoing stream. For read operations,
2398 * The HBA could extract the DIFs and place it in DIF buffers.
2399 *
2400 * The buffer list for this type consists of one or more of the
2401 * protection groups described below:
2402 * +-------------------------+
2403 * start of first prot group --> | DISEED |
2404 * +-------------------------+
2405 * | DIF (Prot SGE) |
2406 * +-------------------------+
2407 * | Data SGE |
2408 * +-------------------------+
2409 * |more Data SGE's ... (opt)|
2410 * +-------------------------+
2411 * start of new prot group --> | DISEED |
2412 * +-------------------------+
2413 * | ... |
2414 * +-------------------------+
2415 *
2416 * Note: It is assumed that both data and protection s/g buffers have been
2417 * mapped for DMA
2418 *
2419 * Returns the number of SGEs added to the SGL.
2420 **/
2421static int
2422lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2423 struct sli4_sge *sgl, int datacnt, int protcnt)
2424{
2425 struct scatterlist *sgde = NULL; /* s/g data entry */
2426 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2427 struct sli4_sge_diseed *diseed = NULL;
2428 dma_addr_t dataphysaddr, protphysaddr;
2429 unsigned short curr_data = 0, curr_prot = 0;
2430 unsigned int split_offset;
2431 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2432 unsigned int protgrp_blks, protgrp_bytes;
2433 unsigned int remainder, subtotal;
2434 int status;
2435 unsigned char pgdone = 0, alldone = 0;
2436 unsigned blksize;
2437 uint32_t reftag;
2438 uint8_t txop, rxop;
2439 uint32_t dma_len;
James Smart0829a192012-05-09 21:18:12 -04002440#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002441 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002442#endif
James Smartacd68592012-01-18 16:25:09 -05002443 uint32_t checking = 1;
2444 uint32_t dma_offset = 0;
2445 int num_sge = 0;
2446
2447 sgpe = scsi_prot_sglist(sc);
2448 sgde = scsi_sglist(sc);
2449
2450 if (!sgpe || !sgde) {
2451 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2452 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2453 sgpe, sgde);
2454 return 0;
2455 }
2456
2457 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2458 if (status)
2459 goto out;
2460
2461 /* extract some info from the scsi command */
2462 blksize = lpfc_cmd_blksize(sc);
2463 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2464
2465#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002466 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002467 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002468 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002469 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002470 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002471 checking = 0;
2472 }
2473#endif
2474
2475 split_offset = 0;
2476 do {
2477 /* setup DISEED with what we have */
2478 diseed = (struct sli4_sge_diseed *) sgl;
2479 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2480 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2481
2482 /* Endianness conversion if necessary */
2483 diseed->ref_tag = cpu_to_le32(reftag);
2484 diseed->ref_tag_tran = diseed->ref_tag;
2485
2486 /* setup DISEED with the rest of the info */
2487 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2488 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2489 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2490 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2491 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2492 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2493
2494 /* Endianness conversion if necessary for DISEED */
2495 diseed->word2 = cpu_to_le32(diseed->word2);
2496 diseed->word3 = cpu_to_le32(diseed->word3);
2497
2498 /* advance sgl and increment bde count */
2499 num_sge++;
2500 sgl++;
2501
2502 /* setup the first BDE that points to protection buffer */
2503 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2504 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2505
2506 /* must be integer multiple of the DIF block length */
2507 BUG_ON(protgroup_len % 8);
2508
2509 /* Now setup DIF SGE */
2510 sgl->word2 = 0;
2511 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2512 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2513 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2514 sgl->word2 = cpu_to_le32(sgl->word2);
2515
2516 protgrp_blks = protgroup_len / 8;
2517 protgrp_bytes = protgrp_blks * blksize;
2518
2519 /* check if DIF SGE is crossing the 4K boundary; if so split */
2520 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2521 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2522 protgroup_offset += protgroup_remainder;
2523 protgrp_blks = protgroup_remainder / 8;
2524 protgrp_bytes = protgrp_blks * blksize;
2525 } else {
2526 protgroup_offset = 0;
2527 curr_prot++;
2528 }
2529
2530 num_sge++;
2531
2532 /* setup SGE's for data blocks associated with DIF data */
2533 pgdone = 0;
2534 subtotal = 0; /* total bytes processed for current prot grp */
2535 while (!pgdone) {
2536 if (!sgde) {
2537 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2538 "9086 BLKGRD:%s Invalid data segment\n",
2539 __func__);
2540 return 0;
2541 }
2542 sgl++;
2543 dataphysaddr = sg_dma_address(sgde) + split_offset;
2544
2545 remainder = sg_dma_len(sgde) - split_offset;
2546
2547 if ((subtotal + remainder) <= protgrp_bytes) {
2548 /* we can use this whole buffer */
2549 dma_len = remainder;
2550 split_offset = 0;
2551
2552 if ((subtotal + remainder) == protgrp_bytes)
2553 pgdone = 1;
2554 } else {
2555 /* must split this buffer with next prot grp */
2556 dma_len = protgrp_bytes - subtotal;
2557 split_offset += dma_len;
2558 }
2559
2560 subtotal += dma_len;
2561
2562 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2563 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2564 bf_set(lpfc_sli4_sge_last, sgl, 0);
2565 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2566 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2567
2568 sgl->sge_len = cpu_to_le32(dma_len);
2569 dma_offset += dma_len;
2570
2571 num_sge++;
2572 curr_data++;
2573
2574 if (split_offset)
2575 break;
2576
2577 /* Move to the next s/g segment if possible */
2578 sgde = sg_next(sgde);
2579 }
2580
2581 if (protgroup_offset) {
2582 /* update the reference tag */
2583 reftag += protgrp_blks;
2584 sgl++;
2585 continue;
2586 }
2587
2588 /* are we done ? */
2589 if (curr_prot == protcnt) {
2590 bf_set(lpfc_sli4_sge_last, sgl, 1);
2591 alldone = 1;
2592 } else if (curr_prot < protcnt) {
2593 /* advance to next prot buffer */
2594 sgpe = sg_next(sgpe);
2595 sgl++;
2596
2597 /* update the reference tag */
2598 reftag += protgrp_blks;
2599 } else {
2600 /* if we're here, we have a bug */
2601 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2602 "9085 BLKGRD: bug in %s\n", __func__);
2603 }
2604
2605 } while (!alldone);
2606
2607out:
2608
2609 return num_sge;
2610}
2611
2612/**
2613 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2614 * @phba: The Hba for which this call is being executed.
2615 * @sc: pointer to scsi command we're working on
2616 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002617 * Given a SCSI command that supports DIF, determine composition of protection
2618 * groups involved in setting up buffer lists
2619 *
James Smartacd68592012-01-18 16:25:09 -05002620 * Returns: Protection group type (with or without DIF)
2621 *
2622 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002623static int
2624lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2625{
2626 int ret = LPFC_PG_TYPE_INVALID;
2627 unsigned char op = scsi_get_prot_op(sc);
2628
2629 switch (op) {
2630 case SCSI_PROT_READ_STRIP:
2631 case SCSI_PROT_WRITE_INSERT:
2632 ret = LPFC_PG_TYPE_NO_DIF;
2633 break;
2634 case SCSI_PROT_READ_INSERT:
2635 case SCSI_PROT_WRITE_STRIP:
2636 case SCSI_PROT_READ_PASS:
2637 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002638 ret = LPFC_PG_TYPE_DIF_BUF;
2639 break;
2640 default:
2641 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2642 "9021 Unsupported protection op:%d\n", op);
2643 break;
2644 }
2645
2646 return ret;
2647}
2648
James Smartacd68592012-01-18 16:25:09 -05002649/**
2650 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2651 * @phba: The Hba for which this call is being executed.
2652 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2653 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002654 * This is the protection/DIF aware version of
2655 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2656 * two functions eventually, but for now, it's here
James Smartacd68592012-01-18 16:25:09 -05002657 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002658static int
James Smartacd68592012-01-18 16:25:09 -05002659lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smarte2a0a9d2008-12-04 22:40:02 -05002660 struct lpfc_scsi_buf *lpfc_cmd)
2661{
2662 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2663 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2664 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2665 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2666 uint32_t num_bde = 0;
2667 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2668 int prot_group_type = 0;
2669 int diflen, fcpdl;
2670 unsigned blksize;
2671
2672 /*
2673 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2674 * fcp_rsp regions to the first data bde entry
2675 */
2676 bpl += 2;
2677 if (scsi_sg_count(scsi_cmnd)) {
2678 /*
2679 * The driver stores the segment count returned from pci_map_sg
2680 * because this a count of dma-mappings used to map the use_sg
2681 * pages. They are not guaranteed to be the same for those
2682 * architectures that implement an IOMMU.
2683 */
2684 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2685 scsi_sglist(scsi_cmnd),
2686 scsi_sg_count(scsi_cmnd), datadir);
2687 if (unlikely(!datasegcnt))
2688 return 1;
2689
2690 lpfc_cmd->seg_cnt = datasegcnt;
2691 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04002692 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2693 "9067 BLKGRD: %s: Too many sg segments"
2694 " from dma_map_sg. Config %d, seg_cnt"
2695 " %d\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002696 __func__, phba->cfg_sg_seg_cnt,
2697 lpfc_cmd->seg_cnt);
2698 scsi_dma_unmap(scsi_cmnd);
2699 return 1;
2700 }
2701
2702 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2703
2704 switch (prot_group_type) {
2705 case LPFC_PG_TYPE_NO_DIF:
2706 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2707 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002708 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002709 if (num_bde < 2)
2710 goto err;
2711 break;
2712 case LPFC_PG_TYPE_DIF_BUF:{
2713 /*
2714 * This type indicates that protection buffers are
2715 * passed to the driver, so that needs to be prepared
2716 * for DMA
2717 */
2718 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2719 scsi_prot_sglist(scsi_cmnd),
2720 scsi_prot_sg_count(scsi_cmnd), datadir);
2721 if (unlikely(!protsegcnt)) {
2722 scsi_dma_unmap(scsi_cmnd);
2723 return 1;
2724 }
2725
2726 lpfc_cmd->prot_seg_cnt = protsegcnt;
2727 if (lpfc_cmd->prot_seg_cnt
2728 > phba->cfg_prot_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04002729 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2730 "9068 BLKGRD: %s: Too many prot sg "
2731 "segments from dma_map_sg. Config %d,"
James Smarte2a0a9d2008-12-04 22:40:02 -05002732 "prot_seg_cnt %d\n", __func__,
2733 phba->cfg_prot_sg_seg_cnt,
2734 lpfc_cmd->prot_seg_cnt);
2735 dma_unmap_sg(&phba->pcidev->dev,
2736 scsi_prot_sglist(scsi_cmnd),
2737 scsi_prot_sg_count(scsi_cmnd),
2738 datadir);
2739 scsi_dma_unmap(scsi_cmnd);
2740 return 1;
2741 }
2742
2743 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2744 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002745 /* we should have 3 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002746 if (num_bde < 3)
2747 goto err;
2748 break;
2749 }
2750 case LPFC_PG_TYPE_INVALID:
2751 default:
2752 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2753 "9022 Unexpected protection group %i\n",
2754 prot_group_type);
2755 return 1;
2756 }
2757 }
2758
2759 /*
2760 * Finish initializing those IOCB fields that are dependent on the
2761 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2762 * reinitialized since all iocb memory resources are used many times
2763 * for transmit, receive, and continuation bpl's.
2764 */
2765 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2766 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2767 iocb_cmd->ulpBdeCount = 1;
2768 iocb_cmd->ulpLe = 1;
2769
2770 fcpdl = scsi_bufflen(scsi_cmnd);
2771
2772 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
2773 /*
2774 * We are in DIF Type 1 mode
2775 * Every data block has a 8 byte DIF (trailer)
2776 * attached to it. Must ajust FCP data length
2777 */
2778 blksize = lpfc_cmd_blksize(scsi_cmnd);
2779 diflen = (fcpdl / blksize) * 8;
2780 fcpdl += diflen;
2781 }
2782 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2783
2784 /*
2785 * Due to difference in data length between DIF/non-DIF paths,
2786 * we need to set word 4 of IOCB here
2787 */
2788 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2789
2790 return 0;
2791err:
2792 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2793 "9023 Could not setup all needed BDE's"
2794 "prot_group_type=%d, num_bde=%d\n",
2795 prot_group_type, num_bde);
2796 return 1;
2797}
2798
2799/*
2800 * This function checks for BlockGuard errors detected by
2801 * the HBA. In case of errors, the ASC/ASCQ fields in the
2802 * sense buffer will be set accordingly, paired with
2803 * ILLEGAL_REQUEST to signal to the kernel that the HBA
2804 * detected corruption.
2805 *
2806 * Returns:
2807 * 0 - No error found
2808 * 1 - BlockGuard error found
2809 * -1 - Internal error (bad profile, ...etc)
2810 */
2811static int
2812lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
2813 struct lpfc_iocbq *pIocbOut)
2814{
2815 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2816 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
2817 int ret = 0;
2818 uint32_t bghm = bgf->bghm;
2819 uint32_t bgstat = bgf->bgstat;
2820 uint64_t failing_sector = 0;
2821
James Smart6a9c52c2009-10-02 15:16:51 -04002822 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
2823 " 0x%x lba 0x%llx blk cnt 0x%x "
James Smarte2a0a9d2008-12-04 22:40:02 -05002824 "bgstat=0x%x bghm=0x%x\n",
James Smart87b5c322008-12-16 10:34:09 -05002825 cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
Tejun Heo83096eb2009-05-07 22:24:39 +09002826 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002827
2828 spin_lock(&_dump_buf_lock);
2829 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04002830 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
2831 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002832 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04002833 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002834
2835 /* If we have a prot sgl, save the DIF buffer */
2836 if (lpfc_prot_group_type(phba, cmd) ==
2837 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04002838 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
2839 "Saving DIF for %u blocks to debugfs\n",
2840 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
2841 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002842 }
2843
2844 _dump_buf_done = 1;
2845 }
2846 spin_unlock(&_dump_buf_lock);
2847
2848 if (lpfc_bgs_get_invalid_prof(bgstat)) {
2849 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002850 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
2851 " BlockGuard profile. bgstat:0x%x\n",
2852 bgstat);
James Smarte2a0a9d2008-12-04 22:40:02 -05002853 ret = (-1);
2854 goto out;
2855 }
2856
2857 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
2858 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002859 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
2860 "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002861 bgstat);
2862 ret = (-1);
2863 goto out;
2864 }
2865
2866 if (lpfc_bgs_get_guard_err(bgstat)) {
2867 ret = 1;
2868
2869 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2870 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002871 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002872 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2873 phba->bg_guard_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002874 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2875 "9055 BLKGRD: guard_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002876 }
2877
2878 if (lpfc_bgs_get_reftag_err(bgstat)) {
2879 ret = 1;
2880
2881 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2882 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002883 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002884 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2885
2886 phba->bg_reftag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002887 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2888 "9056 BLKGRD: ref_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002889 }
2890
2891 if (lpfc_bgs_get_apptag_err(bgstat)) {
2892 ret = 1;
2893
2894 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2895 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002896 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002897 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2898
2899 phba->bg_apptag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002900 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2901 "9061 BLKGRD: app_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002902 }
2903
2904 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
2905 /*
2906 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04002907 * field, and put the failing LBA in it.
2908 * This code assumes there was also a guard/app/ref tag error
2909 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05002910 */
James Smart7c56b9f2011-07-22 18:36:25 -04002911 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
2912 cmd->sense_buffer[8] = 0; /* Information descriptor type */
2913 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
2914 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05002915
2916 /* bghm is a "on the wire" FC frame based count */
2917 switch (scsi_get_prot_op(cmd)) {
2918 case SCSI_PROT_READ_INSERT:
2919 case SCSI_PROT_WRITE_STRIP:
2920 bghm /= cmd->device->sector_size;
2921 break;
2922 case SCSI_PROT_READ_STRIP:
2923 case SCSI_PROT_WRITE_INSERT:
2924 case SCSI_PROT_READ_PASS:
2925 case SCSI_PROT_WRITE_PASS:
2926 bghm /= (cmd->device->sector_size +
2927 sizeof(struct scsi_dif_tuple));
2928 break;
2929 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002930
2931 failing_sector = scsi_get_lba(cmd);
2932 failing_sector += bghm;
2933
James Smart7c56b9f2011-07-22 18:36:25 -04002934 /* Descriptor Information */
2935 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05002936 }
2937
2938 if (!ret) {
2939 /* No error was reported - problem in FW? */
2940 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002941 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart4ac9b222012-03-01 22:38:29 -05002942 "9057 BLKGRD: Unknown error reported!\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002943 }
2944
2945out:
2946 return ret;
2947}
2948
James Smartea2151b2008-09-07 11:52:10 -04002949/**
James Smartda0436e2009-05-22 14:51:39 -04002950 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
2951 * @phba: The Hba for which this call is being executed.
2952 * @lpfc_cmd: The scsi buffer which is going to be mapped.
2953 *
2954 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
2955 * field of @lpfc_cmd for device with SLI-4 interface spec.
2956 *
2957 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04002958 * 1 - Error
2959 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04002960 **/
2961static int
2962lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2963{
2964 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2965 struct scatterlist *sgel = NULL;
2966 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2967 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
James Smartfedd3b72011-02-16 12:39:24 -05002968 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04002969 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2970 dma_addr_t physaddr;
2971 uint32_t num_bde = 0;
2972 uint32_t dma_len;
2973 uint32_t dma_offset = 0;
2974 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05002975 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04002976
2977 /*
2978 * There are three possibilities here - use scatter-gather segment, use
2979 * the single mapping, or neither. Start the lpfc command prep by
2980 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2981 * data bde entry.
2982 */
2983 if (scsi_sg_count(scsi_cmnd)) {
2984 /*
2985 * The driver stores the segment count returned from pci_map_sg
2986 * because this a count of dma-mappings used to map the use_sg
2987 * pages. They are not guaranteed to be the same for those
2988 * architectures that implement an IOMMU.
2989 */
2990
2991 nseg = scsi_dma_map(scsi_cmnd);
2992 if (unlikely(!nseg))
2993 return 1;
2994 sgl += 1;
2995 /* clear the last flag in the fcp_rsp map entry */
2996 sgl->word2 = le32_to_cpu(sgl->word2);
2997 bf_set(lpfc_sli4_sge_last, sgl, 0);
2998 sgl->word2 = cpu_to_le32(sgl->word2);
2999 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05003000 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04003001 lpfc_cmd->seg_cnt = nseg;
3002 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04003003 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3004 " %s: Too many sg segments from "
3005 "dma_map_sg. Config %d, seg_cnt %d\n",
3006 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04003007 lpfc_cmd->seg_cnt);
3008 scsi_dma_unmap(scsi_cmnd);
3009 return 1;
3010 }
3011
3012 /*
3013 * The driver established a maximum scatter-gather segment count
3014 * during probe that limits the number of sg elements in any
3015 * single scsi command. Just run through the seg_cnt and format
3016 * the sge's.
3017 * When using SLI-3 the driver will try to fit all the BDEs into
3018 * the IOCB. If it can't then the BDEs get added to a BPL as it
3019 * does for SLI-2 mode.
3020 */
3021 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3022 physaddr = sg_dma_address(sgel);
3023 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04003024 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3025 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04003026 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04003027 if ((num_bde + 1) == nseg)
3028 bf_set(lpfc_sli4_sge_last, sgl, 1);
3029 else
3030 bf_set(lpfc_sli4_sge_last, sgl, 0);
3031 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04003032 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04003033 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05003034 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04003035 dma_offset += dma_len;
3036 sgl++;
3037 }
James Smartfedd3b72011-02-16 12:39:24 -05003038 /* setup the performance hint (first data BDE) if enabled */
3039 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
3040 bde = (struct ulp_bde64 *)
3041 &(iocb_cmd->unsli3.sli3Words[5]);
3042 bde->addrLow = first_data_sgl->addr_lo;
3043 bde->addrHigh = first_data_sgl->addr_hi;
3044 bde->tus.f.bdeSize =
3045 le32_to_cpu(first_data_sgl->sge_len);
3046 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3047 bde->tus.w = cpu_to_le32(bde->tus.w);
3048 }
James Smartda0436e2009-05-22 14:51:39 -04003049 } else {
3050 sgl += 1;
3051 /* clear the last flag in the fcp_rsp map entry */
3052 sgl->word2 = le32_to_cpu(sgl->word2);
3053 bf_set(lpfc_sli4_sge_last, sgl, 1);
3054 sgl->word2 = cpu_to_le32(sgl->word2);
3055 }
3056
3057 /*
3058 * Finish initializing those IOCB fields that are dependent on the
3059 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3060 * explicitly reinitialized.
3061 * all iocb memory resources are reused.
3062 */
3063 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3064
3065 /*
3066 * Due to difference in data length between DIF/non-DIF paths,
3067 * we need to set word 4 of IOCB here
3068 */
3069 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
3070 return 0;
3071}
3072
3073/**
James Smartacd68592012-01-18 16:25:09 -05003074 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
3075 * @phba: The Hba for which this call is being executed.
3076 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
3077 *
3078 * Adjust the data length to account for how much data
3079 * is actually on the wire.
3080 *
3081 * returns the adjusted data length
3082 **/
3083static int
3084lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
3085 struct lpfc_scsi_buf *lpfc_cmd)
3086{
3087 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
3088 int diflen, fcpdl;
3089 unsigned blksize;
3090
3091 fcpdl = scsi_bufflen(sc);
3092
3093 /* Check if there is protection data on the wire */
3094 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
3095 /* Read */
3096 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
3097 return fcpdl;
3098
3099 } else {
3100 /* Write */
3101 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
3102 return fcpdl;
3103 }
3104
3105 /* If protection data on the wire, adjust the count accordingly */
3106 blksize = lpfc_cmd_blksize(sc);
3107 diflen = (fcpdl / blksize) * 8;
3108 fcpdl += diflen;
3109 return fcpdl;
3110}
3111
3112/**
3113 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3114 * @phba: The Hba for which this call is being executed.
3115 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3116 *
3117 * This is the protection/DIF aware version of
3118 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3119 * two functions eventually, but for now, it's here
3120 **/
3121static int
3122lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3123 struct lpfc_scsi_buf *lpfc_cmd)
3124{
3125 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3126 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3127 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3128 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3129 uint32_t num_bde = 0;
3130 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3131 int prot_group_type = 0;
3132 int fcpdl;
3133
3134 /*
3135 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
3136 * fcp_rsp regions to the first data bde entry
3137 */
3138 if (scsi_sg_count(scsi_cmnd)) {
3139 /*
3140 * The driver stores the segment count returned from pci_map_sg
3141 * because this a count of dma-mappings used to map the use_sg
3142 * pages. They are not guaranteed to be the same for those
3143 * architectures that implement an IOMMU.
3144 */
3145 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3146 scsi_sglist(scsi_cmnd),
3147 scsi_sg_count(scsi_cmnd), datadir);
3148 if (unlikely(!datasegcnt))
3149 return 1;
3150
3151 sgl += 1;
3152 /* clear the last flag in the fcp_rsp map entry */
3153 sgl->word2 = le32_to_cpu(sgl->word2);
3154 bf_set(lpfc_sli4_sge_last, sgl, 0);
3155 sgl->word2 = cpu_to_le32(sgl->word2);
3156
3157 sgl += 1;
3158 lpfc_cmd->seg_cnt = datasegcnt;
3159 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
3160 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3161 "9087 BLKGRD: %s: Too many sg segments"
3162 " from dma_map_sg. Config %d, seg_cnt"
3163 " %d\n",
3164 __func__, phba->cfg_sg_seg_cnt,
3165 lpfc_cmd->seg_cnt);
3166 scsi_dma_unmap(scsi_cmnd);
3167 return 1;
3168 }
3169
3170 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3171
3172 switch (prot_group_type) {
3173 case LPFC_PG_TYPE_NO_DIF:
3174 num_bde = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
3175 datasegcnt);
3176 /* we should have 2 or more entries in buffer list */
3177 if (num_bde < 2)
3178 goto err;
3179 break;
3180 case LPFC_PG_TYPE_DIF_BUF:{
3181 /*
3182 * This type indicates that protection buffers are
3183 * passed to the driver, so that needs to be prepared
3184 * for DMA
3185 */
3186 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3187 scsi_prot_sglist(scsi_cmnd),
3188 scsi_prot_sg_count(scsi_cmnd), datadir);
3189 if (unlikely(!protsegcnt)) {
3190 scsi_dma_unmap(scsi_cmnd);
3191 return 1;
3192 }
3193
3194 lpfc_cmd->prot_seg_cnt = protsegcnt;
3195 if (lpfc_cmd->prot_seg_cnt
3196 > phba->cfg_prot_sg_seg_cnt) {
3197 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3198 "9088 BLKGRD: %s: Too many prot sg "
3199 "segments from dma_map_sg. Config %d,"
3200 "prot_seg_cnt %d\n", __func__,
3201 phba->cfg_prot_sg_seg_cnt,
3202 lpfc_cmd->prot_seg_cnt);
3203 dma_unmap_sg(&phba->pcidev->dev,
3204 scsi_prot_sglist(scsi_cmnd),
3205 scsi_prot_sg_count(scsi_cmnd),
3206 datadir);
3207 scsi_dma_unmap(scsi_cmnd);
3208 return 1;
3209 }
3210
3211 num_bde = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
3212 datasegcnt, protsegcnt);
3213 /* we should have 3 or more entries in buffer list */
3214 if (num_bde < 3)
3215 goto err;
3216 break;
3217 }
3218 case LPFC_PG_TYPE_INVALID:
3219 default:
3220 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3221 "9083 Unexpected protection group %i\n",
3222 prot_group_type);
3223 return 1;
3224 }
3225 }
3226
3227 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
3228
3229 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3230
3231 /*
3232 * Due to difference in data length between DIF/non-DIF paths,
3233 * we need to set word 4 of IOCB here
3234 */
3235 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
3236 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF;
3237
3238 return 0;
3239err:
3240 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3241 "9084 Could not setup all needed BDE's"
3242 "prot_group_type=%d, num_bde=%d\n",
3243 prot_group_type, num_bde);
3244 return 1;
3245}
3246
3247/**
James Smart3772a992009-05-22 14:50:54 -04003248 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3249 * @phba: The Hba for which this call is being executed.
3250 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3251 *
3252 * This routine wraps the actual DMA mapping function pointer from the
3253 * lpfc_hba struct.
3254 *
3255 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003256 * 1 - Error
3257 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003258 **/
3259static inline int
3260lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3261{
3262 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3263}
3264
3265/**
James Smartacd68592012-01-18 16:25:09 -05003266 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3267 * using BlockGuard.
3268 * @phba: The Hba for which this call is being executed.
3269 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3270 *
3271 * This routine wraps the actual DMA mapping function pointer from the
3272 * lpfc_hba struct.
3273 *
3274 * Return codes:
3275 * 1 - Error
3276 * 0 - Success
3277 **/
3278static inline int
3279lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3280{
3281 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3282}
3283
3284/**
James Smart3621a712009-04-06 18:47:14 -04003285 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003286 * @phba: Pointer to hba context object.
3287 * @vport: Pointer to vport object.
3288 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3289 * @rsp_iocb: Pointer to response iocb object which reported error.
3290 *
3291 * This function posts an event when there is a SCSI command reporting
3292 * error from the scsi device.
3293 **/
3294static void
3295lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3296 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3297 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3298 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3299 uint32_t resp_info = fcprsp->rspStatus2;
3300 uint32_t scsi_status = fcprsp->rspStatus3;
3301 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3302 struct lpfc_fast_path_event *fast_path_evt = NULL;
3303 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3304 unsigned long flags;
3305
James Smart5989b8d2010-10-22 11:06:56 -04003306 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3307 return;
3308
James Smartea2151b2008-09-07 11:52:10 -04003309 /* If there is queuefull or busy condition send a scsi event */
3310 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3311 (cmnd->result == SAM_STAT_BUSY)) {
3312 fast_path_evt = lpfc_alloc_fast_evt(phba);
3313 if (!fast_path_evt)
3314 return;
3315 fast_path_evt->un.scsi_evt.event_type =
3316 FC_REG_SCSI_EVENT;
3317 fast_path_evt->un.scsi_evt.subcategory =
3318 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3319 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3320 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3321 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3322 &pnode->nlp_portname, sizeof(struct lpfc_name));
3323 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3324 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3325 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3326 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3327 fast_path_evt = lpfc_alloc_fast_evt(phba);
3328 if (!fast_path_evt)
3329 return;
3330 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3331 FC_REG_SCSI_EVENT;
3332 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3333 LPFC_EVENT_CHECK_COND;
3334 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3335 cmnd->device->lun;
3336 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3337 &pnode->nlp_portname, sizeof(struct lpfc_name));
3338 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3339 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3340 fast_path_evt->un.check_cond_evt.sense_key =
3341 cmnd->sense_buffer[2] & 0xf;
3342 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3343 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3344 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3345 fcpi_parm &&
3346 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3347 ((scsi_status == SAM_STAT_GOOD) &&
3348 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3349 /*
3350 * If status is good or resid does not match with fcp_param and
3351 * there is valid fcpi_parm, then there is a read_check error
3352 */
3353 fast_path_evt = lpfc_alloc_fast_evt(phba);
3354 if (!fast_path_evt)
3355 return;
3356 fast_path_evt->un.read_check_error.header.event_type =
3357 FC_REG_FABRIC_EVENT;
3358 fast_path_evt->un.read_check_error.header.subcategory =
3359 LPFC_EVENT_FCPRDCHKERR;
3360 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3361 &pnode->nlp_portname, sizeof(struct lpfc_name));
3362 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3363 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3364 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3365 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3366 fast_path_evt->un.read_check_error.fcpiparam =
3367 fcpi_parm;
3368 } else
3369 return;
3370
3371 fast_path_evt->vport = vport;
3372 spin_lock_irqsave(&phba->hbalock, flags);
3373 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3374 spin_unlock_irqrestore(&phba->hbalock, flags);
3375 lpfc_worker_wake_up(phba);
3376 return;
3377}
James Smart9bad7672008-12-04 22:39:02 -05003378
3379/**
James Smartf1126682009-06-10 17:22:44 -04003380 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003381 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003382 * @psb: The scsi buffer which is going to be un-mapped.
3383 *
3384 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003385 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003386 **/
dea31012005-04-17 16:05:31 -05003387static void
James Smartf1126682009-06-10 17:22:44 -04003388lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003389{
3390 /*
3391 * There are only two special cases to consider. (1) the scsi command
3392 * requested scatter-gather usage or (2) the scsi command allocated
3393 * a request buffer, but did not request use_sg. There is a third
3394 * case, but it does not require resource deallocation.
3395 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003396 if (psb->seg_cnt > 0)
3397 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003398 if (psb->prot_seg_cnt > 0)
3399 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3400 scsi_prot_sg_count(psb->pCmd),
3401 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003402}
3403
James Smart9bad7672008-12-04 22:39:02 -05003404/**
James Smart3621a712009-04-06 18:47:14 -04003405 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003406 * @vport: The virtual port for which this call is being executed.
3407 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3408 * @rsp_iocb: The response IOCB which contains FCP error.
3409 *
3410 * This routine is called to process response IOCB with status field
3411 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3412 * based upon SCSI and FCP error.
3413 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003414static void
James Smart2e0fef82007-06-17 19:56:36 -05003415lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3416 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003417{
3418 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3419 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3420 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003421 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003422 uint32_t resp_info = fcprsp->rspStatus2;
3423 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003424 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003425 uint32_t host_status = DID_OK;
3426 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05003427 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003428
James Smartea2151b2008-09-07 11:52:10 -04003429
dea31012005-04-17 16:05:31 -05003430 /*
3431 * If this is a task management command, there is no
3432 * scsi packet associated with this lpfc_cmd. The driver
3433 * consumes it.
3434 */
3435 if (fcpcmd->fcpCntl2) {
3436 scsi_status = 0;
3437 goto out;
3438 }
3439
James Smart6a9c52c2009-10-02 15:16:51 -04003440 if (resp_info & RSP_LEN_VALID) {
3441 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003442 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04003443 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3444 "2719 Invalid response length: "
3445 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
3446 cmnd->device->id,
3447 cmnd->device->lun, cmnd->cmnd[0],
3448 rsplen);
3449 host_status = DID_ERROR;
3450 goto out;
3451 }
James Smarte40a02c2010-02-26 14:13:54 -05003452 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3453 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3454 "2757 Protocol failure detected during "
3455 "processing of FCP I/O op: "
3456 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
3457 cmnd->device->id,
3458 cmnd->device->lun, cmnd->cmnd[0],
3459 fcprsp->rspInfo3);
3460 host_status = DID_ERROR;
3461 goto out;
3462 }
James Smart6a9c52c2009-10-02 15:16:51 -04003463 }
3464
James Smartc7743952006-12-02 13:34:42 -05003465 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3466 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3467 if (snslen > SCSI_SENSE_BUFFERSIZE)
3468 snslen = SCSI_SENSE_BUFFERSIZE;
3469
3470 if (resp_info & RSP_LEN_VALID)
3471 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3472 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3473 }
3474 lp = (uint32_t *)cmnd->sense_buffer;
3475
James Smart73d91e52011-10-10 21:32:10 -04003476 if (!scsi_status && (resp_info & RESID_UNDER) &&
3477 vport->cfg_log_verbose & LOG_FCP_UNDER)
3478 logit = LOG_FCP_UNDER;
James Smartc7743952006-12-02 13:34:42 -05003479
James Smarte8b62012007-08-02 11:10:09 -04003480 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003481 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003482 "Data: x%x x%x x%x x%x x%x\n",
3483 cmnd->cmnd[0], scsi_status,
3484 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3485 be32_to_cpu(fcprsp->rspResId),
3486 be32_to_cpu(fcprsp->rspSnsLen),
3487 be32_to_cpu(fcprsp->rspRspLen),
3488 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003489
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003490 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05003491 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003492 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003493
James Smart73d91e52011-10-10 21:32:10 -04003494 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smarte2a0a9d2008-12-04 22:40:02 -05003495 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003496 "residual %d Data: x%x x%x x%x\n",
3497 be32_to_cpu(fcpcmd->fcpDl),
3498 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3499 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003500
3501 /*
James Smart7054a602007-04-25 09:52:34 -04003502 * If there is an under run check if under run reported by
3503 * storage array is same as the under run reported by HBA.
3504 * If this is not same, there is a dropped frame.
3505 */
3506 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3507 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003508 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003509 lpfc_printf_vlog(vport, KERN_WARNING,
3510 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003511 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003512 "and Underrun Data: x%x x%x x%x x%x\n",
3513 be32_to_cpu(fcpcmd->fcpDl),
3514 scsi_get_resid(cmnd), fcpi_parm,
3515 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003516 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003517 host_status = DID_ERROR;
3518 }
3519 /*
dea31012005-04-17 16:05:31 -05003520 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003521 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003522 * is not present, make sure the actual amount transferred is at
3523 * least the underflow value or fail.
3524 */
3525 if (!(resp_info & SNS_LEN_VALID) &&
3526 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003527 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3528 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003529 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003530 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003531 "underrun converted to error "
3532 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003533 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003534 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003535 host_status = DID_ERROR;
3536 }
3537 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003538 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003539 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003540 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003541 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003542 host_status = DID_ERROR;
3543
3544 /*
3545 * Check SLI validation that all the transfer was actually done
3546 * (fcpi_parm should be zero). Apply check only to reads.
3547 */
James Smarteee88772010-09-29 11:19:08 -04003548 } else if (fcpi_parm && (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -04003549 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003550 "9029 FCP Read Check Error Data: "
James Smarteee88772010-09-29 11:19:08 -04003551 "x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003552 be32_to_cpu(fcpcmd->fcpDl),
3553 be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04003554 fcpi_parm, cmnd->cmnd[0], scsi_status);
3555 switch (scsi_status) {
3556 case SAM_STAT_GOOD:
3557 case SAM_STAT_CHECK_CONDITION:
3558 /* Fabric dropped a data frame. Fail any successful
3559 * command in which we detected dropped frames.
3560 * A status of good or some check conditions could
3561 * be considered a successful command.
3562 */
3563 host_status = DID_ERROR;
3564 break;
3565 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003566 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05003567 }
3568
3569 out:
3570 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04003571 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05003572}
3573
James Smart9bad7672008-12-04 22:39:02 -05003574/**
James Smart3621a712009-04-06 18:47:14 -04003575 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05003576 * @phba: The Hba for which this call is being executed.
3577 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04003578 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05003579 *
3580 * This routine assigns scsi command result by looking into response IOCB
3581 * status field appropriately. This routine handles QUEUE FULL condition as
3582 * well by ramping down device queue depth.
3583 **/
dea31012005-04-17 16:05:31 -05003584static void
3585lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3586 struct lpfc_iocbq *pIocbOut)
3587{
3588 struct lpfc_scsi_buf *lpfc_cmd =
3589 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003590 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05003591 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3592 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04003593 struct scsi_cmnd *cmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003594 int result;
James Smarta257bf92009-04-06 18:48:10 -04003595 struct scsi_device *tmp_sdev;
James Smart5ffc2662009-11-18 15:39:44 -05003596 int depth;
James Smartfa61a542008-01-11 01:52:42 -05003597 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04003598 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04003599 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04003600 uint32_t queue_depth, scsi_id;
James Smart73d91e52011-10-10 21:32:10 -04003601 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05003602
James Smart75baf692010-06-08 18:31:21 -04003603 /* Sanity check on return of outstanding command */
3604 if (!(lpfc_cmd->pCmd))
3605 return;
3606 cmd = lpfc_cmd->pCmd;
3607 shost = cmd->device->host;
3608
dea31012005-04-17 16:05:31 -05003609 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
3610 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05003611 /* pick up SLI4 exhange busy status from HBA */
3612 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
3613
James Smart9a6b09c2012-03-01 22:37:42 -05003614#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3615 if (lpfc_cmd->prot_data_type) {
3616 struct scsi_dif_tuple *src = NULL;
3617
3618 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
3619 /*
3620 * Used to restore any changes to protection
3621 * data for error injection.
3622 */
3623 switch (lpfc_cmd->prot_data_type) {
3624 case LPFC_INJERR_REFTAG:
3625 src->ref_tag =
3626 lpfc_cmd->prot_data;
3627 break;
3628 case LPFC_INJERR_APPTAG:
3629 src->app_tag =
3630 (uint16_t)lpfc_cmd->prot_data;
3631 break;
3632 case LPFC_INJERR_GUARD:
3633 src->guard_tag =
3634 (uint16_t)lpfc_cmd->prot_data;
3635 break;
3636 default:
3637 break;
3638 }
3639
3640 lpfc_cmd->prot_data = 0;
3641 lpfc_cmd->prot_data_type = 0;
3642 lpfc_cmd->prot_data_segment = NULL;
3643 }
3644#endif
James Smart109f6ed2008-12-04 22:39:08 -05003645 if (pnode && NLP_CHK_NODE_ACT(pnode))
3646 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05003647
3648 if (lpfc_cmd->status) {
3649 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
3650 (lpfc_cmd->result & IOERR_DRVR_MASK))
3651 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3652 else if (lpfc_cmd->status >= IOSTAT_CNT)
3653 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smart73d91e52011-10-10 21:32:10 -04003654 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR
3655 && !lpfc_cmd->fcp_rsp->rspStatus3
3656 && (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER)
3657 && !(phba->cfg_log_verbose & LOG_FCP_UNDER))
3658 logit = 0;
3659 else
3660 logit = LOG_FCP | LOG_FCP_UNDER;
3661 lpfc_printf_vlog(vport, KERN_WARNING, logit,
3662 "9030 FCP cmd x%x failed <%d/%d> "
3663 "status: x%x result: x%x Data: x%x x%x\n",
3664 cmd->cmnd[0],
3665 cmd->device ? cmd->device->id : 0xffff,
3666 cmd->device ? cmd->device->lun : 0xffff,
3667 lpfc_cmd->status, lpfc_cmd->result,
3668 pIocbOut->iocb.ulpContext,
3669 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05003670
3671 switch (lpfc_cmd->status) {
3672 case IOSTAT_FCP_RSP_ERROR:
3673 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05003674 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05003675 break;
3676 case IOSTAT_NPORT_BSY:
3677 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04003678 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04003679 fast_path_evt = lpfc_alloc_fast_evt(phba);
3680 if (!fast_path_evt)
3681 break;
3682 fast_path_evt->un.fabric_evt.event_type =
3683 FC_REG_FABRIC_EVENT;
3684 fast_path_evt->un.fabric_evt.subcategory =
3685 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
3686 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
3687 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3688 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
3689 &pnode->nlp_portname,
3690 sizeof(struct lpfc_name));
3691 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
3692 &pnode->nlp_nodename,
3693 sizeof(struct lpfc_name));
3694 }
3695 fast_path_evt->vport = vport;
3696 fast_path_evt->work_evt.evt =
3697 LPFC_EVT_FASTPATH_MGMT_EVT;
3698 spin_lock_irqsave(&phba->hbalock, flags);
3699 list_add_tail(&fast_path_evt->work_evt.evt_listp,
3700 &phba->work_list);
3701 spin_unlock_irqrestore(&phba->hbalock, flags);
3702 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05003703 break;
James Smart92d7f7b2007-06-17 19:56:38 -05003704 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05003705 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05003706 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
3707 lpfc_cmd->result ==
3708 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
3709 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
3710 lpfc_cmd->result ==
3711 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
3712 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
3713 break;
3714 }
James Smartd7c255b2008-08-24 21:50:00 -04003715 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05003716 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04003717 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
3718 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
James Smart92d7f7b2007-06-17 19:56:38 -05003719 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04003720 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05003721 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003722 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
3723 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
3724 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
3725 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
3726 /*
3727 * This is a response for a BG enabled
3728 * cmd. Parse BG error
3729 */
3730 lpfc_parse_bg_err(phba, lpfc_cmd,
3731 pIocbOut);
3732 break;
3733 } else {
3734 lpfc_printf_vlog(vport, KERN_WARNING,
3735 LOG_BG,
3736 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04003737 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05003738 }
3739 }
James Smart1151e3e2011-02-16 12:39:35 -05003740 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
3741 && (phba->sli_rev == LPFC_SLI_REV4)
3742 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
3743 /* This IO was aborted by the target, we don't
3744 * know the rxid and because we did not send the
3745 * ABTS we cannot generate and RRQ.
3746 */
3747 lpfc_set_rrq_active(phba, pnode,
3748 lpfc_cmd->cur_iocbq.sli4_xritag,
3749 0, 0);
3750 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003751 /* else: fall through */
dea31012005-04-17 16:05:31 -05003752 default:
3753 cmd->result = ScsiResult(DID_ERROR, 0);
3754 break;
3755 }
3756
James Smart58da1ff2008-04-07 10:15:56 -04003757 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003758 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04003759 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
3760 SAM_STAT_BUSY);
James Smartab56dc22011-02-16 12:39:57 -05003761 } else
dea31012005-04-17 16:05:31 -05003762 cmd->result = ScsiResult(DID_OK, 0);
dea31012005-04-17 16:05:31 -05003763
3764 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
3765 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
3766
James Smarte8b62012007-08-02 11:10:09 -04003767 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3768 "0710 Iodone <%d/%d> cmd %p, error "
3769 "x%x SNS x%x x%x Data: x%x x%x\n",
3770 cmd->device->id, cmd->device->lun, cmd,
3771 cmd->result, *lp, *(lp + 3), cmd->retries,
3772 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05003773 }
3774
James Smartea2151b2008-09-07 11:52:10 -04003775 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003776 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04003777 if (vport->cfg_max_scsicmpl_time &&
3778 time_after(jiffies, lpfc_cmd->start_time +
3779 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04003780 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05003781 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3782 if (pnode->cmd_qdepth >
3783 atomic_read(&pnode->cmd_pending) &&
3784 (atomic_read(&pnode->cmd_pending) >
3785 LPFC_MIN_TGT_QDEPTH) &&
3786 ((cmd->cmnd[0] == READ_10) ||
3787 (cmd->cmnd[0] == WRITE_10)))
3788 pnode->cmd_qdepth =
3789 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04003790
James Smart109f6ed2008-12-04 22:39:08 -05003791 pnode->last_change_time = jiffies;
3792 }
James Smarta257bf92009-04-06 18:48:10 -04003793 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05003794 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smart7dc517d2010-07-14 15:32:10 -04003795 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
James Smart977b5a02008-09-07 11:52:04 -04003796 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05003797 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04003798 spin_lock_irqsave(shost->host_lock, flags);
James Smart7dc517d2010-07-14 15:32:10 -04003799 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
3800 / 100;
3801 depth = depth ? depth : 1;
3802 pnode->cmd_qdepth += depth;
3803 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
3804 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05003805 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04003806 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05003807 }
James Smart977b5a02008-09-07 11:52:04 -04003808 }
3809
James Smart1dcb58e2007-04-25 09:51:30 -04003810 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04003811
3812 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
3813 queue_depth = cmd->device->queue_depth;
3814 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05003815 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003816
Jamie Wellnitzb8086082006-02-28 22:33:12 -05003817 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smartfa61a542008-01-11 01:52:42 -05003818 /*
3819 * If there is a thread waiting for command completion
3820 * wake up the thread.
3821 */
James Smarta257bf92009-04-06 18:48:10 -04003822 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04003823 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05003824 if (lpfc_cmd->waitq)
3825 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04003826 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05003827 lpfc_release_scsi_buf(phba, lpfc_cmd);
3828 return;
3829 }
3830
James Smart92d7f7b2007-06-17 19:56:38 -05003831 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04003832 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05003833
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003834 /*
3835 * Check for queue full. If the lun is reporting queue full, then
3836 * back off the lun queue depth to prevent target overloads.
3837 */
James Smart58da1ff2008-04-07 10:15:56 -04003838 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
3839 NLP_CHK_NODE_ACT(pnode)) {
James Smarta257bf92009-04-06 18:48:10 -04003840 shost_for_each_device(tmp_sdev, shost) {
3841 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003842 continue;
3843 depth = scsi_track_queue_full(tmp_sdev,
James Smart5ffc2662009-11-18 15:39:44 -05003844 tmp_sdev->queue_depth-1);
3845 if (depth <= 0)
3846 continue;
James Smarte8b62012007-08-02 11:10:09 -04003847 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3848 "0711 detected queue full - lun queue "
3849 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04003850 lpfc_send_sdev_queuedepth_change_event(phba, vport,
James Smart5ffc2662009-11-18 15:39:44 -05003851 pnode,
3852 tmp_sdev->lun,
3853 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003854 }
3855 }
3856
James Smartfa61a542008-01-11 01:52:42 -05003857 /*
3858 * If there is a thread waiting for command completion
3859 * wake up the thread.
3860 */
James Smarta257bf92009-04-06 18:48:10 -04003861 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04003862 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05003863 if (lpfc_cmd->waitq)
3864 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04003865 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05003866
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003867 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05003868}
3869
James Smart34b02dc2008-08-24 21:49:55 -04003870/**
James Smart3621a712009-04-06 18:47:14 -04003871 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04003872 * @data: A pointer to the immediate command data portion of the IOCB.
3873 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
3874 *
3875 * The routine copies the entire FCP command from @fcp_cmnd to @data while
3876 * byte swapping the data to big endian format for transmission on the wire.
3877 **/
3878static void
3879lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
3880{
3881 int i, j;
3882 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
3883 i += sizeof(uint32_t), j++) {
3884 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
3885 }
3886}
3887
James Smart9bad7672008-12-04 22:39:02 -05003888/**
James Smartf1126682009-06-10 17:22:44 -04003889 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05003890 * @vport: The virtual port for which this call is being executed.
3891 * @lpfc_cmd: The scsi command which needs to send.
3892 * @pnode: Pointer to lpfc_nodelist.
3893 *
3894 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04003895 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003896 **/
dea31012005-04-17 16:05:31 -05003897static void
James Smartf1126682009-06-10 17:22:44 -04003898lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05003899 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05003900{
James Smart2e0fef82007-06-17 19:56:36 -05003901 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003902 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3903 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3904 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3905 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
3906 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04003907 char tag[2];
dea31012005-04-17 16:05:31 -05003908
James Smart58da1ff2008-04-07 10:15:56 -04003909 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3910 return;
3911
dea31012005-04-17 16:05:31 -05003912 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04003913 /* clear task management bits */
3914 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05003915
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04003916 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
3917 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05003918
James Smartdf9e1b52011-12-13 13:22:17 -05003919 memset(&fcp_cmnd->fcpCdb[0], 0, LPFC_FCP_CDB_LEN);
3920 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
James Smart7e2b19f2007-10-29 11:00:39 -04003921 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
3922 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05003923 case HEAD_OF_QUEUE_TAG:
3924 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
3925 break;
3926 case ORDERED_QUEUE_TAG:
3927 fcp_cmnd->fcpCntl1 = ORDERED_Q;
3928 break;
3929 default:
3930 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
3931 break;
3932 }
3933 } else
3934 fcp_cmnd->fcpCntl1 = 0;
3935
3936 /*
3937 * There are three possibilities here - use scatter-gather segment, use
3938 * the single mapping, or neither. Start the lpfc command prep by
3939 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3940 * data bde entry.
3941 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003942 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05003943 if (datadir == DMA_TO_DEVICE) {
3944 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart3772a992009-05-22 14:50:54 -04003945 if (phba->sli_rev < LPFC_SLI_REV4) {
3946 iocb_cmd->un.fcpi.fcpi_parm = 0;
3947 iocb_cmd->ulpPU = 0;
3948 } else
3949 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05003950 fcp_cmnd->fcpCntl3 = WRITE_DATA;
3951 phba->fc4OutputRequests++;
3952 } else {
3953 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
3954 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05003955 fcp_cmnd->fcpCntl3 = READ_DATA;
3956 phba->fc4InputRequests++;
3957 }
3958 } else {
3959 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
3960 iocb_cmd->un.fcpi.fcpi_parm = 0;
3961 iocb_cmd->ulpPU = 0;
3962 fcp_cmnd->fcpCntl3 = 0;
3963 phba->fc4ControlRequests++;
3964 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003965 if (phba->sli_rev == 3 &&
3966 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04003967 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05003968 /*
3969 * Finish initializing those IOCB fields that are independent
3970 * of the scsi_cmnd request_buffer
3971 */
3972 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04003973 if (phba->sli_rev == LPFC_SLI_REV4)
3974 piocbq->iocb.ulpContext =
3975 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05003976 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
3977 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05003978 else
3979 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05003980
3981 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
3982 piocbq->context1 = lpfc_cmd;
3983 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
3984 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05003985 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05003986}
3987
James Smart9bad7672008-12-04 22:39:02 -05003988/**
James Smart6d368e52011-05-24 11:44:12 -04003989 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05003990 * @vport: The virtual port for which this call is being executed.
3991 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3992 * @lun: Logical unit number.
3993 * @task_mgmt_cmd: SCSI task management command.
3994 *
James Smart3772a992009-05-22 14:50:54 -04003995 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
3996 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003997 *
3998 * Return codes:
3999 * 0 - Error
4000 * 1 - Success
4001 **/
dea31012005-04-17 16:05:31 -05004002static int
James Smartf1126682009-06-10 17:22:44 -04004003lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05004004 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04004005 unsigned int lun,
dea31012005-04-17 16:05:31 -05004006 uint8_t task_mgmt_cmd)
4007{
dea31012005-04-17 16:05:31 -05004008 struct lpfc_iocbq *piocbq;
4009 IOCB_t *piocb;
4010 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04004011 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05004012 struct lpfc_nodelist *ndlp = rdata->pnode;
4013
James Smart58da1ff2008-04-07 10:15:56 -04004014 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4015 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05004016 return 0;
dea31012005-04-17 16:05:31 -05004017
dea31012005-04-17 16:05:31 -05004018 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004019 piocbq->vport = vport;
4020
dea31012005-04-17 16:05:31 -05004021 piocb = &piocbq->iocb;
4022
4023 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04004024 /* Clear out any old data in the FCP command area */
4025 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4026 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004027 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05004028 if (vport->phba->sli_rev == 3 &&
4029 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004030 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004031 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05004032 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04004033 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4034 piocb->ulpContext =
4035 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4036 }
dea31012005-04-17 16:05:31 -05004037 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
4038 piocb->ulpFCP2Rcvy = 1;
4039 }
4040 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
4041
4042 /* ulpTimeout is only one byte */
4043 if (lpfc_cmd->timeout > 0xff) {
4044 /*
4045 * Do not timeout the command at the firmware level.
4046 * The driver will provide the timeout mechanism.
4047 */
4048 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04004049 } else
dea31012005-04-17 16:05:31 -05004050 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04004051
4052 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4053 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004054
James Smart2e0fef82007-06-17 19:56:36 -05004055 return 1;
dea31012005-04-17 16:05:31 -05004056}
4057
James Smart9bad7672008-12-04 22:39:02 -05004058/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004059 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04004060 * @phba: The hba struct for which this call is being executed.
4061 * @dev_grp: The HBA PCI-Device group number.
4062 *
4063 * This routine sets up the SCSI interface API function jump table in @phba
4064 * struct.
4065 * Returns: 0 - success, -ENODEV - failure.
4066 **/
4067int
4068lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4069{
4070
James Smartf1126682009-06-10 17:22:44 -04004071 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4072 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04004073
James Smart3772a992009-05-22 14:50:54 -04004074 switch (dev_grp) {
4075 case LPFC_PCI_DEV_LP:
4076 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4077 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05004078 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004079 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05004080 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004081 break;
James Smartda0436e2009-05-22 14:51:39 -04004082 case LPFC_PCI_DEV_OC:
4083 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4084 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05004085 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004086 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05004087 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004088 break;
James Smart3772a992009-05-22 14:50:54 -04004089 default:
4090 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4091 "1418 Invalid HBA PCI-device group: 0x%x\n",
4092 dev_grp);
4093 return -ENODEV;
4094 break;
4095 }
James Smart3772a992009-05-22 14:50:54 -04004096 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05004097 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04004098 return 0;
4099}
4100
4101/**
James Smart3621a712009-04-06 18:47:14 -04004102 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05004103 * @phba: The Hba for which this call is being executed.
4104 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4105 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4106 *
4107 * This routine is IOCB completion routine for device reset and target reset
4108 * routine. This routine release scsi buffer associated with lpfc_cmd.
4109 **/
James Smart7054a602007-04-25 09:52:34 -04004110static void
4111lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4112 struct lpfc_iocbq *cmdiocbq,
4113 struct lpfc_iocbq *rspiocbq)
4114{
4115 struct lpfc_scsi_buf *lpfc_cmd =
4116 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4117 if (lpfc_cmd)
4118 lpfc_release_scsi_buf(phba, lpfc_cmd);
4119 return;
4120}
4121
James Smart9bad7672008-12-04 22:39:02 -05004122/**
James Smart3621a712009-04-06 18:47:14 -04004123 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05004124 * @host: The scsi host for which this call is being executed.
4125 *
4126 * This routine provides module information about hba.
4127 *
4128 * Reutrn code:
4129 * Pointer to char - Success.
4130 **/
dea31012005-04-17 16:05:31 -05004131const char *
4132lpfc_info(struct Scsi_Host *host)
4133{
James Smart2e0fef82007-06-17 19:56:36 -05004134 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4135 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004136 int len;
4137 static char lpfcinfobuf[384];
4138
4139 memset(lpfcinfobuf,0,384);
4140 if (phba && phba->pcidev){
4141 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4142 len = strlen(lpfcinfobuf);
4143 snprintf(lpfcinfobuf + len,
4144 384-len,
4145 " on PCI bus %02x device %02x irq %d",
4146 phba->pcidev->bus->number,
4147 phba->pcidev->devfn,
4148 phba->pcidev->irq);
4149 len = strlen(lpfcinfobuf);
4150 if (phba->Port[0]) {
4151 snprintf(lpfcinfobuf + len,
4152 384-len,
4153 " port %s",
4154 phba->Port);
4155 }
James Smart65467b62010-01-26 23:08:29 -05004156 len = strlen(lpfcinfobuf);
4157 if (phba->sli4_hba.link_state.logical_speed) {
4158 snprintf(lpfcinfobuf + len,
4159 384-len,
4160 " Logical Link Speed: %d Mbps",
4161 phba->sli4_hba.link_state.logical_speed * 10);
4162 }
dea31012005-04-17 16:05:31 -05004163 }
4164 return lpfcinfobuf;
4165}
4166
James Smart9bad7672008-12-04 22:39:02 -05004167/**
James Smart3621a712009-04-06 18:47:14 -04004168 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05004169 * @phba: The Hba for which this call is being executed.
4170 *
4171 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4172 * The default value of cfg_poll_tmo is 10 milliseconds.
4173 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004174static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4175{
4176 unsigned long poll_tmo_expires =
4177 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4178
4179 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
4180 mod_timer(&phba->fcp_poll_timer,
4181 poll_tmo_expires);
4182}
4183
James Smart9bad7672008-12-04 22:39:02 -05004184/**
James Smart3621a712009-04-06 18:47:14 -04004185 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004186 * @phba: The Hba for which this call is being executed.
4187 *
4188 * This routine starts the fcp_poll_timer of @phba.
4189 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004190void lpfc_poll_start_timer(struct lpfc_hba * phba)
4191{
4192 lpfc_poll_rearm_timer(phba);
4193}
4194
James Smart9bad7672008-12-04 22:39:02 -05004195/**
James Smart3621a712009-04-06 18:47:14 -04004196 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05004197 * @ptr: Map to lpfc_hba data structure pointer.
4198 *
4199 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4200 * and FCP Ring interrupt is disable.
4201 **/
4202
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004203void lpfc_poll_timeout(unsigned long ptr)
4204{
James Smart2e0fef82007-06-17 19:56:36 -05004205 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004206
4207 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004208 lpfc_sli_handle_fast_ring_event(phba,
4209 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4210
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004211 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4212 lpfc_poll_rearm_timer(phba);
4213 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004214}
4215
James Smart9bad7672008-12-04 22:39:02 -05004216/**
James Smart3621a712009-04-06 18:47:14 -04004217 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05004218 * @cmnd: Pointer to scsi_cmnd data structure.
4219 * @done: Pointer to done routine.
4220 *
4221 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4222 * This routine prepares an IOCB from scsi command and provides to firmware.
4223 * The @done callback is invoked after driver finished processing the command.
4224 *
4225 * Return value :
4226 * 0 - Success
4227 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4228 **/
dea31012005-04-17 16:05:31 -05004229static int
Jeff Garzikf2812332010-11-16 02:10:29 -05004230lpfc_queuecommand_lck(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
dea31012005-04-17 16:05:31 -05004231{
James Smart2e0fef82007-06-17 19:56:36 -05004232 struct Scsi_Host *shost = cmnd->device->host;
4233 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4234 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004235 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004236 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004237 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004238 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004239 int err;
dea31012005-04-17 16:05:31 -05004240
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004241 err = fc_remote_port_chkready(rport);
4242 if (err) {
4243 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004244 goto out_fail_command;
4245 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004246 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004247
James Smartbf086112011-08-21 21:48:13 -04004248 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004249 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004250
James Smart6a9c52c2009-10-02 15:16:51 -04004251 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4252 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4253 " op:%02x str=%s without registering for"
4254 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004255 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4256 dif_op_str[scsi_get_prot_op(cmnd)]);
4257 goto out_fail_command;
4258 }
4259
dea31012005-04-17 16:05:31 -05004260 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004261 * Catch race where our node has transitioned, but the
4262 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004263 */
James Smartb522d7d2008-09-07 11:51:56 -04004264 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
Mike Christief55ca84d2011-02-25 14:04:27 -06004265 cmnd->result = ScsiResult(DID_IMM_RETRY, 0);
James Smartb522d7d2008-09-07 11:51:56 -04004266 goto out_fail_command;
4267 }
James Smart7dc517d2010-07-14 15:32:10 -04004268 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
Mike Christie34963432011-02-25 14:04:28 -06004269 goto out_tgt_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05004270
James Smart19ca7602010-11-20 23:11:55 -05004271 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05004272 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004273 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004274
James Smarte8b62012007-08-02 11:10:09 -04004275 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4276 "0707 driver's buffer pool is empty, "
4277 "IO busied\n");
dea31012005-04-17 16:05:31 -05004278 goto out_host_busy;
4279 }
4280
4281 /*
4282 * Store the midlayer's command structure for the completion phase
4283 * and complete the command initialization.
4284 */
4285 lpfc_cmd->pCmd = cmnd;
4286 lpfc_cmd->rdata = rdata;
4287 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04004288 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05004289 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
4290 cmnd->scsi_done = done;
4291
James Smarte2a0a9d2008-12-04 22:40:02 -05004292 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004293 if (vport->phba->cfg_enable_bg) {
4294 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smart9a6b09c2012-03-01 22:37:42 -05004295 "9033 BLKGRD: rcvd protected cmd:%02x op=%s "
4296 "guard=%s\n", cmnd->cmnd[0],
4297 dif_op_str[scsi_get_prot_op(cmnd)],
4298 dif_grd_str[scsi_host_get_guard(shost)]);
James Smart6a9c52c2009-10-02 15:16:51 -04004299 if (cmnd->cmnd[0] == READ_10)
4300 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05004301 "9035 BLKGRD: READ @ sector %llu, "
James Smart9a6b09c2012-03-01 22:37:42 -05004302 "cnt %u, rpt %d\n",
Tejun Heo83096eb2009-05-07 22:24:39 +09004303 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004304 blk_rq_sectors(cmnd->request),
4305 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004306 else if (cmnd->cmnd[0] == WRITE_10)
4307 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05004308 "9036 BLKGRD: WRITE @ sector %llu, "
James Smart9a6b09c2012-03-01 22:37:42 -05004309 "cnt %u, wpt %d\n",
James Smart87b5c322008-12-16 10:34:09 -05004310 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09004311 blk_rq_sectors(cmnd->request),
James Smart9a6b09c2012-03-01 22:37:42 -05004312 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004313 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004314
4315 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4316 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004317 if (vport->phba->cfg_enable_bg) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004318 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smart9a6b09c2012-03-01 22:37:42 -05004319 "9038 BLKGRD: rcvd unprotected cmd:"
4320 "%02x op=%s guard=%s\n", cmnd->cmnd[0],
4321 dif_op_str[scsi_get_prot_op(cmnd)],
4322 dif_grd_str[scsi_host_get_guard(shost)]);
James Smart6a9c52c2009-10-02 15:16:51 -04004323 if (cmnd->cmnd[0] == READ_10)
4324 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
4325 "9040 dbg: READ @ sector %llu, "
James Smart9a6b09c2012-03-01 22:37:42 -05004326 "cnt %u, rpt %d\n",
James Smart6a9c52c2009-10-02 15:16:51 -04004327 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004328 blk_rq_sectors(cmnd->request),
4329 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004330 else if (cmnd->cmnd[0] == WRITE_10)
4331 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smart9a6b09c2012-03-01 22:37:42 -05004332 "9041 dbg: WRITE @ sector %llu, "
4333 "cnt %u, wpt %d\n",
4334 (unsigned long long)scsi_get_lba(cmnd),
4335 blk_rq_sectors(cmnd->request),
4336 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004337 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004338 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4339 }
4340
dea31012005-04-17 16:05:31 -05004341 if (err)
4342 goto out_host_busy_free_buf;
4343
James Smart2e0fef82007-06-17 19:56:36 -05004344 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004345
James Smart977b5a02008-09-07 11:52:04 -04004346 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04004347 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004348 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05004349 if (err) {
4350 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05004351 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004352 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004353 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004354 spin_unlock(shost->host_lock);
4355 lpfc_sli_handle_fast_ring_event(phba,
4356 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4357
4358 spin_lock(shost->host_lock);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004359 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4360 lpfc_poll_rearm_timer(phba);
4361 }
4362
dea31012005-04-17 16:05:31 -05004363 return 0;
4364
4365 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04004366 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004367 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004368 out_host_busy:
4369 return SCSI_MLQUEUE_HOST_BUSY;
4370
Mike Christie34963432011-02-25 14:04:28 -06004371 out_tgt_busy:
4372 return SCSI_MLQUEUE_TARGET_BUSY;
4373
dea31012005-04-17 16:05:31 -05004374 out_fail_command:
4375 done(cmnd);
4376 return 0;
4377}
4378
Jeff Garzikf2812332010-11-16 02:10:29 -05004379static DEF_SCSI_QCMD(lpfc_queuecommand)
4380
James Smart9bad7672008-12-04 22:39:02 -05004381/**
James Smart3621a712009-04-06 18:47:14 -04004382 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004383 * @cmnd: Pointer to scsi_cmnd data structure.
4384 *
4385 * This routine aborts @cmnd pending in base driver.
4386 *
4387 * Return code :
4388 * 0x2003 - Error
4389 * 0x2002 - Success
4390 **/
dea31012005-04-17 16:05:31 -05004391static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004392lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004393{
James Smart2e0fef82007-06-17 19:56:36 -05004394 struct Scsi_Host *shost = cmnd->device->host;
4395 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4396 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004397 struct lpfc_iocbq *iocb;
4398 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004399 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004400 IOCB_t *cmd, *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004401 int ret = SUCCESS;
James Smartfa61a542008-01-11 01:52:42 -05004402 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004403
James Smart589a52d2010-07-14 15:30:54 -04004404 ret = fc_block_scsi_eh(cmnd);
4405 if (ret)
4406 return ret;
James Smart4f2e66c2012-05-09 21:17:07 -04004407
4408 spin_lock_irq(&phba->hbalock);
4409 /* driver queued commands are in process of being flushed */
4410 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
4411 spin_unlock_irq(&phba->hbalock);
4412 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4413 "3168 SCSI Layer abort requested I/O has been "
4414 "flushed by LLD.\n");
4415 return FAILED;
4416 }
4417
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004418 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smarteee88772010-09-29 11:19:08 -04004419 if (!lpfc_cmd) {
James Smart4f2e66c2012-05-09 21:17:07 -04004420 spin_unlock_irq(&phba->hbalock);
James Smarteee88772010-09-29 11:19:08 -04004421 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4422 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004423 "x%x ID %d LUN %d\n",
4424 ret, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04004425 return SUCCESS;
4426 }
dea31012005-04-17 16:05:31 -05004427
James Smart4f2e66c2012-05-09 21:17:07 -04004428 iocb = &lpfc_cmd->cur_iocbq;
4429 /* the command is in process of being cancelled */
4430 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
4431 spin_unlock_irq(&phba->hbalock);
4432 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4433 "3169 SCSI Layer abort requested I/O has been "
4434 "cancelled by LLD.\n");
4435 return FAILED;
4436 }
dea31012005-04-17 16:05:31 -05004437 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004438 * If pCmd field of the corresponding lpfc_scsi_buf structure
4439 * points to a different SCSI command, then the driver has
4440 * already completed this command, but the midlayer did not
James Smart4f2e66c2012-05-09 21:17:07 -04004441 * see the completion before the eh fired. Just return SUCCESS.
dea31012005-04-17 16:05:31 -05004442 */
James Smart4f2e66c2012-05-09 21:17:07 -04004443 if (lpfc_cmd->pCmd != cmnd) {
4444 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4445 "3170 SCSI Layer abort requested I/O has been "
4446 "completed by LLD.\n");
4447 goto out_unlock;
4448 }
dea31012005-04-17 16:05:31 -05004449
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004450 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004451
James Smart4f2e66c2012-05-09 21:17:07 -04004452 abtsiocb = __lpfc_sli_get_iocbq(phba);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004453 if (abtsiocb == NULL) {
4454 ret = FAILED;
James Smart4f2e66c2012-05-09 21:17:07 -04004455 goto out_unlock;
dea31012005-04-17 16:05:31 -05004456 }
4457
dea31012005-04-17 16:05:31 -05004458 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004459 * The scsi command can not be in txq and it is in flight because the
4460 * pCmd is still pointig at the SCSI command we have to abort. There
4461 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004462 */
dea31012005-04-17 16:05:31 -05004463
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004464 cmd = &iocb->iocb;
4465 icmd = &abtsiocb->iocb;
4466 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4467 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004468 if (phba->sli_rev == LPFC_SLI_REV4)
4469 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4470 else
4471 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004472
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004473 icmd->ulpLe = 1;
4474 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004475
4476 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4477 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05004478 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05004479
James Smart2e0fef82007-06-17 19:56:36 -05004480 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004481 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4482 else
4483 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004484
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004485 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004486 abtsiocb->vport = vport;
James Smart4f2e66c2012-05-09 21:17:07 -04004487 /* no longer need the lock after this point */
4488 spin_unlock_irq(&phba->hbalock);
4489
James Smart3772a992009-05-22 14:50:54 -04004490 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
4491 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004492 lpfc_sli_release_iocbq(phba, abtsiocb);
4493 ret = FAILED;
4494 goto out;
4495 }
4496
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004497 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004498 lpfc_sli_handle_fast_ring_event(phba,
4499 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004500
James Smartfa61a542008-01-11 01:52:42 -05004501 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004502 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004503 wait_event_timeout(waitq,
4504 (lpfc_cmd->pCmd != cmnd),
4505 (2*vport->cfg_devloss_tmo*HZ));
James Smartfa61a542008-01-11 01:52:42 -05004506 lpfc_cmd->waitq = NULL;
dea31012005-04-17 16:05:31 -05004507
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004508 if (lpfc_cmd->pCmd == cmnd) {
4509 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04004510 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4511 "0748 abort handler timed out waiting "
4512 "for abort to complete: ret %#x, ID %d, "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004513 "LUN %d\n",
4514 ret, cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004515 }
James Smart4f2e66c2012-05-09 21:17:07 -04004516 goto out;
dea31012005-04-17 16:05:31 -05004517
James Smart4f2e66c2012-05-09 21:17:07 -04004518out_unlock:
4519 spin_unlock_irq(&phba->hbalock);
4520out:
James Smarte8b62012007-08-02 11:10:09 -04004521 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4522 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004523 "LUN %d\n", ret, cmnd->device->id,
4524 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004525 return ret;
dea31012005-04-17 16:05:31 -05004526}
4527
James Smartbbb9d182009-06-10 17:23:16 -04004528static char *
4529lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4530{
4531 switch (task_mgmt_cmd) {
4532 case FCP_ABORT_TASK_SET:
4533 return "ABORT_TASK_SET";
4534 case FCP_CLEAR_TASK_SET:
4535 return "FCP_CLEAR_TASK_SET";
4536 case FCP_BUS_RESET:
4537 return "FCP_BUS_RESET";
4538 case FCP_LUN_RESET:
4539 return "FCP_LUN_RESET";
4540 case FCP_TARGET_RESET:
4541 return "FCP_TARGET_RESET";
4542 case FCP_CLEAR_ACA:
4543 return "FCP_CLEAR_ACA";
4544 case FCP_TERMINATE_TASK:
4545 return "FCP_TERMINATE_TASK";
4546 default:
4547 return "unknown";
4548 }
4549}
4550
4551/**
4552 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
4553 * @vport: The virtual port for which this call is being executed.
4554 * @rdata: Pointer to remote port local data
4555 * @tgt_id: Target ID of remote device.
4556 * @lun_id: Lun number for the TMF
4557 * @task_mgmt_cmd: type of TMF to send
4558 *
4559 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
4560 * a remote port.
4561 *
4562 * Return Code:
4563 * 0x2003 - Error
4564 * 0x2002 - Success.
4565 **/
4566static int
4567lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
4568 unsigned tgt_id, unsigned int lun_id,
4569 uint8_t task_mgmt_cmd)
4570{
4571 struct lpfc_hba *phba = vport->phba;
4572 struct lpfc_scsi_buf *lpfc_cmd;
4573 struct lpfc_iocbq *iocbq;
4574 struct lpfc_iocbq *iocbqrsp;
James Smart5989b8d2010-10-22 11:06:56 -04004575 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004576 int ret;
4577 int status;
4578
James Smart5989b8d2010-10-22 11:06:56 -04004579 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smartbbb9d182009-06-10 17:23:16 -04004580 return FAILED;
4581
James Smart19ca7602010-11-20 23:11:55 -05004582 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
James Smartbbb9d182009-06-10 17:23:16 -04004583 if (lpfc_cmd == NULL)
4584 return FAILED;
4585 lpfc_cmd->timeout = 60;
4586 lpfc_cmd->rdata = rdata;
4587
4588 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
4589 task_mgmt_cmd);
4590 if (!status) {
4591 lpfc_release_scsi_buf(phba, lpfc_cmd);
4592 return FAILED;
4593 }
4594
4595 iocbq = &lpfc_cmd->cur_iocbq;
4596 iocbqrsp = lpfc_sli_get_iocbq(phba);
4597 if (iocbqrsp == NULL) {
4598 lpfc_release_scsi_buf(phba, lpfc_cmd);
4599 return FAILED;
4600 }
4601
4602 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4603 "0702 Issue %s to TGT %d LUN %d "
James Smart6d368e52011-05-24 11:44:12 -04004604 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04004605 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04004606 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
4607 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04004608
4609 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
4610 iocbq, iocbqrsp, lpfc_cmd->timeout);
4611 if (status != IOCB_SUCCESS) {
4612 if (status == IOCB_TIMEDOUT) {
4613 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
4614 ret = TIMEOUT_ERROR;
4615 } else
4616 ret = FAILED;
4617 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4618 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart6d368e52011-05-24 11:44:12 -04004619 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
4620 "iocb_flag x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04004621 lpfc_taskmgmt_name(task_mgmt_cmd),
4622 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
James Smart6d368e52011-05-24 11:44:12 -04004623 iocbqrsp->iocb.un.ulpWord[4],
4624 iocbq->iocb_flag);
James Smart2a9bf3d2010-06-07 15:24:45 -04004625 } else if (status == IOCB_BUSY)
4626 ret = FAILED;
4627 else
James Smartbbb9d182009-06-10 17:23:16 -04004628 ret = SUCCESS;
4629
4630 lpfc_sli_release_iocbq(phba, iocbqrsp);
4631
4632 if (ret != TIMEOUT_ERROR)
4633 lpfc_release_scsi_buf(phba, lpfc_cmd);
4634
4635 return ret;
4636}
4637
4638/**
4639 * lpfc_chk_tgt_mapped -
4640 * @vport: The virtual port to check on
4641 * @cmnd: Pointer to scsi_cmnd data structure.
4642 *
4643 * This routine delays until the scsi target (aka rport) for the
4644 * command exists (is present and logged in) or we declare it non-existent.
4645 *
4646 * Return code :
4647 * 0x2003 - Error
4648 * 0x2002 - Success
4649 **/
4650static int
4651lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
4652{
4653 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004654 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004655 unsigned long later;
4656
James Smart1c6f4ef52009-11-18 15:40:49 -05004657 if (!rdata) {
4658 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4659 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
4660 return FAILED;
4661 }
4662 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004663 /*
4664 * If target is not in a MAPPED state, delay until
4665 * target is rediscovered or devloss timeout expires.
4666 */
4667 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
4668 while (time_after(later, jiffies)) {
4669 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4670 return FAILED;
4671 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
4672 return SUCCESS;
4673 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
4674 rdata = cmnd->device->hostdata;
4675 if (!rdata)
4676 return FAILED;
4677 pnode = rdata->pnode;
4678 }
4679 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
4680 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
4681 return FAILED;
4682 return SUCCESS;
4683}
4684
4685/**
4686 * lpfc_reset_flush_io_context -
4687 * @vport: The virtual port (scsi_host) for the flush context
4688 * @tgt_id: If aborting by Target contect - specifies the target id
4689 * @lun_id: If aborting by Lun context - specifies the lun id
4690 * @context: specifies the context level to flush at.
4691 *
4692 * After a reset condition via TMF, we need to flush orphaned i/o
4693 * contexts from the adapter. This routine aborts any contexts
4694 * outstanding, then waits for their completions. The wait is
4695 * bounded by devloss_tmo though.
4696 *
4697 * Return code :
4698 * 0x2003 - Error
4699 * 0x2002 - Success
4700 **/
4701static int
4702lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
4703 uint64_t lun_id, lpfc_ctx_cmd context)
4704{
4705 struct lpfc_hba *phba = vport->phba;
4706 unsigned long later;
4707 int cnt;
4708
4709 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
4710 if (cnt)
4711 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
4712 tgt_id, lun_id, context);
4713 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
4714 while (time_after(later, jiffies) && cnt) {
4715 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
4716 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
4717 }
4718 if (cnt) {
4719 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4720 "0724 I/O flush failure for context %s : cnt x%x\n",
4721 ((context == LPFC_CTX_LUN) ? "LUN" :
4722 ((context == LPFC_CTX_TGT) ? "TGT" :
4723 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
4724 cnt);
4725 return FAILED;
4726 }
4727 return SUCCESS;
4728}
4729
James Smart9bad7672008-12-04 22:39:02 -05004730/**
James Smart3621a712009-04-06 18:47:14 -04004731 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05004732 * @cmnd: Pointer to scsi_cmnd data structure.
4733 *
James Smartbbb9d182009-06-10 17:23:16 -04004734 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05004735 * command.
4736 *
4737 * Return code :
4738 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04004739 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05004740 **/
dea31012005-04-17 16:05:31 -05004741static int
James Smart7054a602007-04-25 09:52:34 -04004742lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004743{
James Smart2e0fef82007-06-17 19:56:36 -05004744 struct Scsi_Host *shost = cmnd->device->host;
4745 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05004746 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004747 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004748 unsigned tgt_id = cmnd->device->id;
4749 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04004750 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04004751 int status;
dea31012005-04-17 16:05:31 -05004752
James Smart1c6f4ef52009-11-18 15:40:49 -05004753 if (!rdata) {
4754 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4755 "0798 Device Reset rport failure: rdata x%p\n", rdata);
4756 return FAILED;
4757 }
4758 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04004759 status = fc_block_scsi_eh(cmnd);
4760 if (status)
4761 return status;
James Smartbbb9d182009-06-10 17:23:16 -04004762
4763 status = lpfc_chk_tgt_mapped(vport, cmnd);
4764 if (status == FAILED) {
4765 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4766 "0721 Device Reset rport failure: rdata x%p\n", rdata);
4767 return FAILED;
4768 }
4769
4770 scsi_event.event_type = FC_REG_SCSI_EVENT;
4771 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
4772 scsi_event.lun = lun_id;
4773 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
4774 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
4775
4776 fc_host_post_vendor_event(shost, fc_get_event_number(),
4777 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
4778
4779 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
4780 FCP_LUN_RESET);
4781
4782 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4783 "0713 SCSI layer issued Device Reset (%d, %d) "
4784 "return x%x\n", tgt_id, lun_id, status);
4785
dea31012005-04-17 16:05:31 -05004786 /*
James Smartbbb9d182009-06-10 17:23:16 -04004787 * We have to clean up i/o as : they may be orphaned by the TMF;
4788 * or if the TMF failed, they may be in an indeterminate state.
4789 * So, continue on.
4790 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05004791 */
James Smartbbb9d182009-06-10 17:23:16 -04004792 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
4793 LPFC_CTX_LUN);
4794 return status;
4795}
4796
4797/**
4798 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
4799 * @cmnd: Pointer to scsi_cmnd data structure.
4800 *
4801 * This routine does a target reset by sending a TARGET_RESET task management
4802 * command.
4803 *
4804 * Return code :
4805 * 0x2003 - Error
4806 * 0x2002 - Success
4807 **/
4808static int
4809lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
4810{
4811 struct Scsi_Host *shost = cmnd->device->host;
4812 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4813 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004814 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004815 unsigned tgt_id = cmnd->device->id;
4816 unsigned int lun_id = cmnd->device->lun;
4817 struct lpfc_scsi_event_header scsi_event;
4818 int status;
4819
James Smart1c6f4ef52009-11-18 15:40:49 -05004820 if (!rdata) {
4821 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4822 "0799 Target Reset rport failure: rdata x%p\n", rdata);
4823 return FAILED;
4824 }
4825 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04004826 status = fc_block_scsi_eh(cmnd);
4827 if (status)
4828 return status;
James Smartbbb9d182009-06-10 17:23:16 -04004829
4830 status = lpfc_chk_tgt_mapped(vport, cmnd);
4831 if (status == FAILED) {
4832 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4833 "0722 Target Reset rport failure: rdata x%p\n", rdata);
4834 return FAILED;
dea31012005-04-17 16:05:31 -05004835 }
James Smartea2151b2008-09-07 11:52:10 -04004836
4837 scsi_event.event_type = FC_REG_SCSI_EVENT;
4838 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
4839 scsi_event.lun = 0;
4840 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
4841 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
4842
James Smartbbb9d182009-06-10 17:23:16 -04004843 fc_host_post_vendor_event(shost, fc_get_event_number(),
4844 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04004845
James Smartbbb9d182009-06-10 17:23:16 -04004846 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
4847 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05004848
James Smarte8b62012007-08-02 11:10:09 -04004849 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04004850 "0723 SCSI layer issued Target Reset (%d, %d) "
4851 "return x%x\n", tgt_id, lun_id, status);
4852
4853 /*
4854 * We have to clean up i/o as : they may be orphaned by the TMF;
4855 * or if the TMF failed, they may be in an indeterminate state.
4856 * So, continue on.
4857 * We will report success if all the i/o aborts successfully.
4858 */
4859 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
4860 LPFC_CTX_TGT);
4861 return status;
dea31012005-04-17 16:05:31 -05004862}
4863
James Smart9bad7672008-12-04 22:39:02 -05004864/**
James Smart3621a712009-04-06 18:47:14 -04004865 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004866 * @cmnd: Pointer to scsi_cmnd data structure.
4867 *
James Smartbbb9d182009-06-10 17:23:16 -04004868 * This routine does target reset to all targets on @cmnd->device->host.
4869 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05004870 *
James Smartbbb9d182009-06-10 17:23:16 -04004871 * Return code :
4872 * 0x2003 - Error
4873 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05004874 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04004875static int
James Smart7054a602007-04-25 09:52:34 -04004876lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004877{
James Smart2e0fef82007-06-17 19:56:36 -05004878 struct Scsi_Host *shost = cmnd->device->host;
4879 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05004880 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04004881 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04004882 int match;
4883 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04004884
4885 scsi_event.event_type = FC_REG_SCSI_EVENT;
4886 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
4887 scsi_event.lun = 0;
4888 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
4889 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
4890
James Smartbbb9d182009-06-10 17:23:16 -04004891 fc_host_post_vendor_event(shost, fc_get_event_number(),
4892 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05004893
James Smartbf086112011-08-21 21:48:13 -04004894 status = fc_block_scsi_eh(cmnd);
4895 if (status)
4896 return status;
James Smartbbb9d182009-06-10 17:23:16 -04004897
dea31012005-04-17 16:05:31 -05004898 /*
4899 * Since the driver manages a single bus device, reset all
4900 * targets known to the driver. Should any target reset
4901 * fail, this routine returns failure to the midlayer.
4902 */
James Smarte17da182006-07-06 15:49:25 -04004903 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04004904 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05004905 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004906 spin_lock_irq(shost->host_lock);
4907 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004908 if (!NLP_CHK_NODE_ACT(ndlp))
4909 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004910 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04004911 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04004912 ndlp->rport) {
dea31012005-04-17 16:05:31 -05004913 match = 1;
4914 break;
4915 }
4916 }
James Smart2e0fef82007-06-17 19:56:36 -05004917 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004918 if (!match)
4919 continue;
James Smartbbb9d182009-06-10 17:23:16 -04004920
4921 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
4922 i, 0, FCP_TARGET_RESET);
4923
4924 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04004925 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4926 "0700 Bus Reset on target %d failed\n",
4927 i);
James Smart915caaa2008-06-14 22:52:38 -04004928 ret = FAILED;
dea31012005-04-17 16:05:31 -05004929 }
4930 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05004931 /*
James Smartbbb9d182009-06-10 17:23:16 -04004932 * We have to clean up i/o as : they may be orphaned by the TMFs
4933 * above; or if any of the TMFs failed, they may be in an
4934 * indeterminate state.
4935 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05004936 */
James Smartbbb9d182009-06-10 17:23:16 -04004937
4938 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
4939 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05004940 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04004941
James Smarte8b62012007-08-02 11:10:09 -04004942 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4943 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05004944 return ret;
4945}
4946
James Smart9bad7672008-12-04 22:39:02 -05004947/**
James Smart3621a712009-04-06 18:47:14 -04004948 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05004949 * @sdev: Pointer to scsi_device.
4950 *
4951 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
4952 * globally available list of scsi buffers. This routine also makes sure scsi
4953 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
4954 * of scsi buffer exists for the lifetime of the driver.
4955 *
4956 * Return codes:
4957 * non-0 - Error
4958 * 0 - Success
4959 **/
dea31012005-04-17 16:05:31 -05004960static int
dea31012005-04-17 16:05:31 -05004961lpfc_slave_alloc(struct scsi_device *sdev)
4962{
James Smart2e0fef82007-06-17 19:56:36 -05004963 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
4964 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004965 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04004966 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05004967 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04004968 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04004969 uint32_t sdev_cnt;
dea31012005-04-17 16:05:31 -05004970
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004971 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05004972 return -ENXIO;
4973
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004974 sdev->hostdata = rport->dd_data;
James Smartd7c47992010-06-08 18:31:54 -04004975 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05004976
4977 /*
4978 * Populate the cmds_per_lun count scsi_bufs into this host's globally
4979 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04004980 * HBA limit conveyed to the midlayer via the host structure. The
4981 * formula accounts for the lun_queue_depth + error handlers + 1
4982 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05004983 */
4984 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04004985 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05004986
James Smartd7c47992010-06-08 18:31:54 -04004987 /* If allocated buffers are enough do nothing */
4988 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
4989 return 0;
4990
James Smart92d7f7b2007-06-17 19:56:38 -05004991 /* Allow some exchanges to be available always to complete discovery */
4992 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04004993 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4994 "0704 At limitation of %d preallocated "
4995 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05004996 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05004997 /* Allow some exchanges to be available always to complete discovery */
4998 } else if (total + num_to_alloc >
4999 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005000 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5001 "0705 Allocation request of %d "
5002 "command buffers will exceed max of %d. "
5003 "Reducing allocation request to %d.\n",
5004 num_to_alloc, phba->cfg_hba_queue_depth,
5005 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05005006 num_to_alloc = phba->cfg_hba_queue_depth - total;
5007 }
James Smart3772a992009-05-22 14:50:54 -04005008 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
5009 if (num_to_alloc != num_allocated) {
5010 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5011 "0708 Allocation request of %d "
5012 "command buffers did not succeed. "
5013 "Allocated %d buffers.\n",
5014 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05005015 }
James Smart1c6f4ef52009-11-18 15:40:49 -05005016 if (num_allocated > 0)
5017 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05005018 return 0;
5019}
5020
James Smart9bad7672008-12-04 22:39:02 -05005021/**
James Smart3621a712009-04-06 18:47:14 -04005022 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05005023 * @sdev: Pointer to scsi_device.
5024 *
5025 * This routine configures following items
5026 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05005027 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5028 *
5029 * Return codes:
5030 * 0 - Success
5031 **/
dea31012005-04-17 16:05:31 -05005032static int
5033lpfc_slave_configure(struct scsi_device *sdev)
5034{
James Smart2e0fef82007-06-17 19:56:36 -05005035 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5036 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005037
5038 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04005039 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005040 else
James Smart3de2a652007-08-02 11:09:59 -04005041 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005042
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005043 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005044 lpfc_sli_handle_fast_ring_event(phba,
5045 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005046 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5047 lpfc_poll_rearm_timer(phba);
5048 }
5049
dea31012005-04-17 16:05:31 -05005050 return 0;
5051}
5052
James Smart9bad7672008-12-04 22:39:02 -05005053/**
James Smart3621a712009-04-06 18:47:14 -04005054 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05005055 * @sdev: Pointer to scsi_device.
5056 *
5057 * This routine sets @sdev hostatdata filed to null.
5058 **/
dea31012005-04-17 16:05:31 -05005059static void
5060lpfc_slave_destroy(struct scsi_device *sdev)
5061{
James Smartd7c47992010-06-08 18:31:54 -04005062 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5063 struct lpfc_hba *phba = vport->phba;
5064 atomic_dec(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005065 sdev->hostdata = NULL;
5066 return;
5067}
5068
James Smart92d7f7b2007-06-17 19:56:38 -05005069
dea31012005-04-17 16:05:31 -05005070struct scsi_host_template lpfc_template = {
5071 .module = THIS_MODULE,
5072 .name = LPFC_DRIVER_NAME,
5073 .info = lpfc_info,
5074 .queuecommand = lpfc_queuecommand,
5075 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005076 .eh_device_reset_handler = lpfc_device_reset_handler,
5077 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04005078 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05005079 .slave_alloc = lpfc_slave_alloc,
5080 .slave_configure = lpfc_slave_configure,
5081 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04005082 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05005083 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005084 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05005085 .cmd_per_lun = LPFC_CMD_PER_LUN,
5086 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05005087 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04005088 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04005089 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05005090 .change_queue_depth = lpfc_change_queue_depth,
dea31012005-04-17 16:05:31 -05005091};
James Smart3de2a652007-08-02 11:09:59 -04005092
5093struct scsi_host_template lpfc_vport_template = {
5094 .module = THIS_MODULE,
5095 .name = LPFC_DRIVER_NAME,
5096 .info = lpfc_info,
5097 .queuecommand = lpfc_queuecommand,
5098 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005099 .eh_device_reset_handler = lpfc_device_reset_handler,
5100 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04005101 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5102 .slave_alloc = lpfc_slave_alloc,
5103 .slave_configure = lpfc_slave_configure,
5104 .slave_destroy = lpfc_slave_destroy,
5105 .scan_finished = lpfc_scan_finished,
5106 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005107 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04005108 .cmd_per_lun = LPFC_CMD_PER_LUN,
5109 .use_clustering = ENABLE_CLUSTERING,
5110 .shost_attrs = lpfc_vport_attrs,
5111 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05005112 .change_queue_depth = lpfc_change_queue_depth,
James Smart3de2a652007-08-02 11:09:59 -04005113};