blob: 88f3a83dbd2eaf45a36d08a0c92b8fe186158ea3 [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/**
721 * lpfc_sli4_repost_scsi_sgl_list - Repsot the Scsi buffers sgl pages as block
722 * @phba: pointer to lpfc hba data structure.
723 *
724 * This routine walks the list of scsi buffers that have been allocated and
725 * repost them to the HBA by using SGL block post. This is needed after a
726 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
727 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
728 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
729 *
730 * Returns: 0 = success, non-zero failure.
731 **/
732int
733lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
734{
735 struct lpfc_scsi_buf *psb;
736 int index, status, bcnt = 0, rcnt = 0, rc = 0;
737 LIST_HEAD(sblist);
738
739 for (index = 0; index < phba->sli4_hba.scsi_xri_cnt; index++) {
740 psb = phba->sli4_hba.lpfc_scsi_psb_array[index];
741 if (psb) {
742 /* Remove from SCSI buffer list */
743 list_del(&psb->list);
744 /* Add it to a local SCSI buffer list */
745 list_add_tail(&psb->list, &sblist);
746 if (++rcnt == LPFC_NEMBED_MBOX_SGL_CNT) {
747 bcnt = rcnt;
748 rcnt = 0;
749 }
750 } else
751 /* A hole present in the XRI array, need to skip */
752 bcnt = rcnt;
753
754 if (index == phba->sli4_hba.scsi_xri_cnt - 1)
755 /* End of XRI array for SCSI buffer, complete */
756 bcnt = rcnt;
757
758 /* Continue until collect up to a nembed page worth of sgls */
759 if (bcnt == 0)
760 continue;
761 /* Now, post the SCSI buffer list sgls as a block */
James Smart6d368e52011-05-24 11:44:12 -0400762 if (!phba->sli4_hba.extents_in_use)
763 status = lpfc_sli4_post_scsi_sgl_block(phba,
764 &sblist,
765 bcnt);
766 else
767 status = lpfc_sli4_post_scsi_sgl_blk_ext(phba,
768 &sblist,
769 bcnt);
James Smartda0436e2009-05-22 14:51:39 -0400770 /* Reset SCSI buffer count for next round of posting */
771 bcnt = 0;
772 while (!list_empty(&sblist)) {
773 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
774 list);
775 if (status) {
776 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500777 psb->exch_busy = 1;
James Smartda0436e2009-05-22 14:51:39 -0400778 rc++;
James Smart341af102010-01-26 23:07:37 -0500779 } else {
780 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400781 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500782 }
James Smartda0436e2009-05-22 14:51:39 -0400783 /* Put it back into the SCSI buffer list */
784 lpfc_release_scsi_buf_s4(phba, psb);
785 }
786 }
787 return rc;
788}
789
790/**
791 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
792 * @vport: The virtual port for which this call being executed.
793 * @num_to_allocate: The requested number of buffers to allocate.
794 *
795 * This routine allocates a scsi buffer for device with SLI-4 interface spec,
796 * the scsi buffer contains all the necessary information needed to initiate
797 * a SCSI I/O.
798 *
799 * Return codes:
800 * int - number of scsi buffers that were allocated.
801 * 0 = failure, less than num_to_alloc is a partial failure.
802 **/
803static int
804lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
805{
806 struct lpfc_hba *phba = vport->phba;
807 struct lpfc_scsi_buf *psb;
808 struct sli4_sge *sgl;
809 IOCB_t *iocb;
810 dma_addr_t pdma_phys_fcp_cmd;
811 dma_addr_t pdma_phys_fcp_rsp;
812 dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
James Smart6d368e52011-05-24 11:44:12 -0400813 uint16_t iotag, last_xritag = NO_XRI, lxri = 0;
James Smartda0436e2009-05-22 14:51:39 -0400814 int status = 0, index;
815 int bcnt;
816 int non_sequential_xri = 0;
James Smartda0436e2009-05-22 14:51:39 -0400817 LIST_HEAD(sblist);
818
819 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
820 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
821 if (!psb)
822 break;
823
824 /*
825 * Get memory from the pci pool to map the virt space to pci bus
826 * space for an I/O. The DMA buffer includes space for the
827 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
828 * necessary to support the sg_tablesize.
829 */
830 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
831 GFP_KERNEL, &psb->dma_handle);
832 if (!psb->data) {
833 kfree(psb);
834 break;
835 }
836
837 /* Initialize virtual ptrs to dma_buf region. */
838 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
839
840 /* Allocate iotag for psb->cur_iocbq. */
841 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
842 if (iotag == 0) {
James Smartb92938b2010-06-07 15:24:12 -0400843 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
844 psb->data, psb->dma_handle);
James Smartda0436e2009-05-22 14:51:39 -0400845 kfree(psb);
846 break;
847 }
848
James Smart6d368e52011-05-24 11:44:12 -0400849 lxri = lpfc_sli4_next_xritag(phba);
850 if (lxri == NO_XRI) {
James Smartda0436e2009-05-22 14:51:39 -0400851 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
852 psb->data, psb->dma_handle);
853 kfree(psb);
854 break;
855 }
James Smart6d368e52011-05-24 11:44:12 -0400856 psb->cur_iocbq.sli4_lxritag = lxri;
857 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smartda0436e2009-05-22 14:51:39 -0400858 if (last_xritag != NO_XRI
859 && psb->cur_iocbq.sli4_xritag != (last_xritag+1)) {
860 non_sequential_xri = 1;
861 } else
862 list_add_tail(&psb->list, &sblist);
863 last_xritag = psb->cur_iocbq.sli4_xritag;
864
865 index = phba->sli4_hba.scsi_xri_cnt++;
866 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
867
868 psb->fcp_bpl = psb->data;
869 psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
870 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
871 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
872 sizeof(struct fcp_cmnd));
873
874 /* Initialize local short-hand pointers. */
875 sgl = (struct sli4_sge *)psb->fcp_bpl;
876 pdma_phys_bpl = psb->dma_handle;
877 pdma_phys_fcp_cmd =
878 (psb->dma_handle + phba->cfg_sg_dma_buf_size)
879 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
880 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
881
882 /*
883 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
884 * are sg list bdes. Initialize the first two and leave the
885 * rest for queuecommand.
886 */
887 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
888 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
James Smart05580562011-05-24 11:40:48 -0400889 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400890 bf_set(lpfc_sli4_sge_last, sgl, 0);
891 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500892 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
James Smartda0436e2009-05-22 14:51:39 -0400893 sgl++;
894
895 /* Setup the physical region for the FCP RSP */
896 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
897 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smart05580562011-05-24 11:40:48 -0400898 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400899 bf_set(lpfc_sli4_sge_last, sgl, 1);
900 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500901 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -0400902
903 /*
904 * Since the IOCB for the FCP I/O is built into this
905 * lpfc_scsi_buf, initialize it with all known data now.
906 */
907 iocb = &psb->cur_iocbq.iocb;
908 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
909 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
910 /* setting the BLP size to 2 * sizeof BDE may not be correct.
911 * We are setting the bpl to point to out sgl. An sgl's
912 * entries are 16 bytes, a bpl entries are 12 bytes.
913 */
914 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
915 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
916 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
917 iocb->ulpBdeCount = 1;
918 iocb->ulpLe = 1;
919 iocb->ulpClass = CLASS3;
James Smarteee88772010-09-29 11:19:08 -0400920 psb->cur_iocbq.context1 = psb;
James Smartda0436e2009-05-22 14:51:39 -0400921 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
922 pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
923 else
924 pdma_phys_bpl1 = 0;
925 psb->dma_phys_bpl = pdma_phys_bpl;
926 phba->sli4_hba.lpfc_scsi_psb_array[index] = psb;
927 if (non_sequential_xri) {
928 status = lpfc_sli4_post_sgl(phba, pdma_phys_bpl,
929 pdma_phys_bpl1,
930 psb->cur_iocbq.sli4_xritag);
931 if (status) {
932 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500933 psb->exch_busy = 1;
James Smart341af102010-01-26 23:07:37 -0500934 } else {
935 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400936 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500937 }
James Smartda0436e2009-05-22 14:51:39 -0400938 /* Put it back into the SCSI buffer list */
939 lpfc_release_scsi_buf_s4(phba, psb);
940 break;
941 }
942 }
943 if (bcnt) {
James Smart6d368e52011-05-24 11:44:12 -0400944 if (!phba->sli4_hba.extents_in_use)
945 status = lpfc_sli4_post_scsi_sgl_block(phba,
946 &sblist,
947 bcnt);
948 else
949 status = lpfc_sli4_post_scsi_sgl_blk_ext(phba,
950 &sblist,
951 bcnt);
952
953 if (status) {
954 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
955 "3021 SCSI SGL post error %d\n",
956 status);
957 bcnt = 0;
958 }
James Smartda0436e2009-05-22 14:51:39 -0400959 /* Reset SCSI buffer count for next round of posting */
960 while (!list_empty(&sblist)) {
961 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
962 list);
963 if (status) {
964 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500965 psb->exch_busy = 1;
James Smart341af102010-01-26 23:07:37 -0500966 } else {
967 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400968 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500969 }
James Smartda0436e2009-05-22 14:51:39 -0400970 /* Put it back into the SCSI buffer list */
971 lpfc_release_scsi_buf_s4(phba, psb);
972 }
973 }
974
James Smartd7c47992010-06-08 18:31:54 -0400975 return bcnt + non_sequential_xri;
James Smartda0436e2009-05-22 14:51:39 -0400976}
977
978/**
James Smart3772a992009-05-22 14:50:54 -0400979 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
980 * @vport: The virtual port for which this call being executed.
981 * @num_to_allocate: The requested number of buffers to allocate.
982 *
983 * This routine wraps the actual SCSI buffer allocator function pointer from
984 * the lpfc_hba struct.
985 *
986 * Return codes:
987 * int - number of scsi buffers that were allocated.
988 * 0 = failure, less than num_to_alloc is a partial failure.
989 **/
990static inline int
991lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
992{
993 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
994}
995
996/**
James Smart19ca7602010-11-20 23:11:55 -0500997 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -0400998 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -0500999 *
1000 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1001 * and returns to caller.
1002 *
1003 * Return codes:
1004 * NULL - Error
1005 * Pointer to lpfc_scsi_buf - Success
1006 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +01001007static struct lpfc_scsi_buf*
James Smart19ca7602010-11-20 23:11:55 -05001008lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001009{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001010 struct lpfc_scsi_buf * lpfc_cmd = NULL;
1011 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001012 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001013
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001014 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001015 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -04001016 if (lpfc_cmd) {
1017 lpfc_cmd->seg_cnt = 0;
1018 lpfc_cmd->nonsg_phys = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001019 lpfc_cmd->prot_seg_cnt = 0;
James Smart1dcb58e2007-04-25 09:51:30 -04001020 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001021 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001022 return lpfc_cmd;
1023}
James Smart19ca7602010-11-20 23:11:55 -05001024/**
1025 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1026 * @phba: The HBA for which this call is being executed.
1027 *
1028 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1029 * and returns to caller.
1030 *
1031 * Return codes:
1032 * NULL - Error
1033 * Pointer to lpfc_scsi_buf - Success
1034 **/
1035static struct lpfc_scsi_buf*
1036lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1037{
James Smart1151e3e2011-02-16 12:39:35 -05001038 struct lpfc_scsi_buf *lpfc_cmd ;
James Smart19ca7602010-11-20 23:11:55 -05001039 unsigned long iflag = 0;
1040 int found = 0;
1041
1042 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -05001043 list_for_each_entry(lpfc_cmd, &phba->lpfc_scsi_buf_list,
1044 list) {
James Smart19ca7602010-11-20 23:11:55 -05001045 if (lpfc_test_rrq_active(phba, ndlp,
James Smart1151e3e2011-02-16 12:39:35 -05001046 lpfc_cmd->cur_iocbq.sli4_xritag))
1047 continue;
1048 list_del(&lpfc_cmd->list);
James Smart19ca7602010-11-20 23:11:55 -05001049 found = 1;
1050 lpfc_cmd->seg_cnt = 0;
1051 lpfc_cmd->nonsg_phys = 0;
1052 lpfc_cmd->prot_seg_cnt = 0;
James Smart1151e3e2011-02-16 12:39:35 -05001053 break;
James Smart19ca7602010-11-20 23:11:55 -05001054 }
James Smart1151e3e2011-02-16 12:39:35 -05001055 spin_unlock_irqrestore(&phba->scsi_buf_list_lock,
1056 iflag);
1057 if (!found)
1058 return NULL;
1059 else
1060 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001061}
1062/**
1063 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1064 * @phba: The HBA for which this call is being executed.
1065 *
1066 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1067 * and returns to caller.
1068 *
1069 * Return codes:
1070 * NULL - Error
1071 * Pointer to lpfc_scsi_buf - Success
1072 **/
1073static struct lpfc_scsi_buf*
1074lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1075{
1076 return phba->lpfc_get_scsi_buf(phba, ndlp);
1077}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001078
James Smart9bad7672008-12-04 22:39:02 -05001079/**
James Smart3772a992009-05-22 14:50:54 -04001080 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -05001081 * @phba: The Hba for which this call is being executed.
1082 * @psb: The scsi buffer which is being released.
1083 *
1084 * This routine releases @psb scsi buffer by adding it to tail of @phba
1085 * lpfc_scsi_buf_list list.
1086 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001087static void
James Smart3772a992009-05-22 14:50:54 -04001088lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001089{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001090 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001091
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001092 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001093 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -05001094 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001095 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -05001096}
1097
James Smart9bad7672008-12-04 22:39:02 -05001098/**
James Smartda0436e2009-05-22 14:51:39 -04001099 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1100 * @phba: The Hba for which this call is being executed.
1101 * @psb: The scsi buffer which is being released.
1102 *
1103 * This routine releases @psb scsi buffer by adding it to tail of @phba
1104 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1105 * and cannot be reused for at least RA_TOV amount of time if it was
1106 * aborted.
1107 **/
1108static void
1109lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1110{
1111 unsigned long iflag = 0;
1112
James Smart341af102010-01-26 23:07:37 -05001113 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -04001114 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1115 iflag);
1116 psb->pCmd = NULL;
1117 list_add_tail(&psb->list,
1118 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1119 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1120 iflag);
1121 } else {
1122
1123 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1124 psb->pCmd = NULL;
1125 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
1126 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
1127 }
1128}
1129
1130/**
James Smart3772a992009-05-22 14:50:54 -04001131 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1132 * @phba: The Hba for which this call is being executed.
1133 * @psb: The scsi buffer which is being released.
1134 *
1135 * This routine releases @psb scsi buffer by adding it to tail of @phba
1136 * lpfc_scsi_buf_list list.
1137 **/
1138static void
1139lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1140{
1141
1142 phba->lpfc_release_scsi_buf(phba, psb);
1143}
1144
1145/**
1146 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001147 * @phba: The Hba for which this call is being executed.
1148 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1149 *
1150 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001151 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1152 * through sg elements and format the bdea. This routine also initializes all
1153 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001154 *
1155 * Return codes:
1156 * 1 - Error
1157 * 0 - Success
1158 **/
dea31012005-04-17 16:05:31 -05001159static int
James Smart3772a992009-05-22 14:50:54 -04001160lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001161{
1162 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1163 struct scatterlist *sgel = NULL;
1164 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1165 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001166 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001167 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001168 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001169 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001170 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001171 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001172
1173 /*
1174 * There are three possibilities here - use scatter-gather segment, use
1175 * the single mapping, or neither. Start the lpfc command prep by
1176 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1177 * data bde entry.
1178 */
1179 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001180 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001181 /*
1182 * The driver stores the segment count returned from pci_map_sg
1183 * because this a count of dma-mappings used to map the use_sg
1184 * pages. They are not guaranteed to be the same for those
1185 * architectures that implement an IOMMU.
1186 */
dea31012005-04-17 16:05:31 -05001187
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001188 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1189 scsi_sg_count(scsi_cmnd), datadir);
1190 if (unlikely(!nseg))
1191 return 1;
1192
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001193 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001194 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001195 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1196 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001197 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001198 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001199 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001200 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001201 return 1;
1202 }
1203
1204 /*
1205 * The driver established a maximum scatter-gather segment count
1206 * during probe that limits the number of sg elements in any
1207 * single scsi command. Just run through the seg_cnt and format
1208 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001209 * When using SLI-3 the driver will try to fit all the BDEs into
1210 * the IOCB. If it can't then the BDEs get added to a BPL as it
1211 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001212 */
James Smart34b02dc2008-08-24 21:49:55 -04001213 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001214 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001215 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001216 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001217 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001218 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1219 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1220 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1221 data_bde->addrLow = putPaddrLow(physaddr);
1222 data_bde->addrHigh = putPaddrHigh(physaddr);
1223 data_bde++;
1224 } else {
1225 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1226 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1227 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1228 bpl->addrLow =
1229 le32_to_cpu(putPaddrLow(physaddr));
1230 bpl->addrHigh =
1231 le32_to_cpu(putPaddrHigh(physaddr));
1232 bpl++;
1233 }
dea31012005-04-17 16:05:31 -05001234 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001235 }
dea31012005-04-17 16:05:31 -05001236
1237 /*
1238 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001239 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1240 * explicitly reinitialized and for SLI-3 the extended bde count is
1241 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001242 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001243 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001244 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1245 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001246 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1247 /*
1248 * The extended IOCB format can only fit 3 BDE or a BPL.
1249 * This I/O has more than 3 BDE so the 1st data bde will
1250 * be a BPL that is filled in here.
1251 */
1252 physaddr = lpfc_cmd->dma_handle;
1253 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1254 data_bde->tus.f.bdeSize = (num_bde *
1255 sizeof(struct ulp_bde64));
1256 physaddr += (sizeof(struct fcp_cmnd) +
1257 sizeof(struct fcp_rsp) +
1258 (2 * sizeof(struct ulp_bde64)));
1259 data_bde->addrHigh = putPaddrHigh(physaddr);
1260 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001261 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -04001262 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1263 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001264 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -04001265 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1266 }
1267 } else {
1268 iocb_cmd->un.fcpi64.bdl.bdeSize =
1269 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001270 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001271 }
James Smart09372822008-01-11 01:52:54 -05001272 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001273
1274 /*
1275 * Due to difference in data length between DIF/non-DIF paths,
1276 * we need to set word 4 of IOCB here
1277 */
James Smarta257bf92009-04-06 18:48:10 -04001278 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001279 return 0;
1280}
1281
James Smartf9bb2da2011-10-10 21:34:11 -04001282static inline unsigned
1283lpfc_cmd_blksize(struct scsi_cmnd *sc)
1284{
1285 return sc->device->sector_size;
1286}
1287
1288#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001289
James Smart9a6b09c2012-03-01 22:37:42 -05001290/* Return if if error injection is detected by Initiator */
1291#define BG_ERR_INIT 0x1
1292/* Return if if error injection is detected by Target */
1293#define BG_ERR_TGT 0x2
1294/* Return if if swapping CSUM<-->CRC is required for error injection */
1295#define BG_ERR_SWAP 0x10
1296/* Return if disabling Guard/Ref/App checking is required for error injection */
1297#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -05001298
1299/**
1300 * lpfc_bg_err_inject - Determine if we should inject an error
1301 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -04001302 * @sc: The SCSI command to examine
1303 * @reftag: (out) BlockGuard reference tag for transmitted data
1304 * @apptag: (out) BlockGuard application tag for transmitted data
1305 * @new_guard (in) Value to replace CRC with if needed
1306 *
James Smart9a6b09c2012-03-01 22:37:42 -05001307 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -05001308 **/
James Smartf9bb2da2011-10-10 21:34:11 -04001309static int
1310lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1311 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1312{
1313 struct scatterlist *sgpe; /* s/g prot entry */
1314 struct scatterlist *sgde; /* s/g data entry */
James Smart9a6b09c2012-03-01 22:37:42 -05001315 struct lpfc_scsi_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -05001316 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -05001317 struct lpfc_nodelist *ndlp;
1318 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -04001319 uint32_t op = scsi_get_prot_op(sc);
1320 uint32_t blksize;
1321 uint32_t numblks;
1322 sector_t lba;
1323 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001324 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001325
1326 if (op == SCSI_PROT_NORMAL)
1327 return 0;
1328
James Smartacd68592012-01-18 16:25:09 -05001329 sgpe = scsi_prot_sglist(sc);
1330 sgde = scsi_sglist(sc);
James Smartf9bb2da2011-10-10 21:34:11 -04001331 lba = scsi_get_lba(sc);
James Smart4ac9b222012-03-01 22:38:29 -05001332
1333 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001334 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1335 blksize = lpfc_cmd_blksize(sc);
1336 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1337
1338 /* Make sure we have the right LBA if one is specified */
1339 if ((phba->lpfc_injerr_lba < lba) ||
1340 (phba->lpfc_injerr_lba >= (lba + numblks)))
1341 return 0;
James Smartacd68592012-01-18 16:25:09 -05001342 if (sgpe) {
1343 blockoff = phba->lpfc_injerr_lba - lba;
1344 numblks = sg_dma_len(sgpe) /
1345 sizeof(struct scsi_dif_tuple);
1346 if (numblks < blockoff)
1347 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001348 }
James Smartf9bb2da2011-10-10 21:34:11 -04001349 }
1350
James Smart4ac9b222012-03-01 22:38:29 -05001351 /* Next check if we need to match the remote NPortID or WWPN */
1352 rdata = sc->device->hostdata;
1353 if (rdata && rdata->pnode) {
1354 ndlp = rdata->pnode;
1355
1356 /* Make sure we have the right NPortID if one is specified */
1357 if (phba->lpfc_injerr_nportid &&
1358 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1359 return 0;
1360
1361 /*
1362 * Make sure we have the right WWPN if one is specified.
1363 * wwn[0] should be a non-zero NAA in a good WWPN.
1364 */
1365 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1366 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1367 sizeof(struct lpfc_name)) != 0))
1368 return 0;
1369 }
1370
1371 /* Setup a ptr to the protection data if the SCSI host provides it */
1372 if (sgpe) {
1373 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1374 src += blockoff;
1375 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1376 }
1377
James Smartf9bb2da2011-10-10 21:34:11 -04001378 /* Should we change the Reference Tag */
1379 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001380 if (phba->lpfc_injerr_wref_cnt) {
1381 switch (op) {
1382 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001383 if (src) {
1384 /*
1385 * For WRITE_PASS, force the error
1386 * to be sent on the wire. It should
1387 * be detected by the Target.
1388 * If blockoff != 0 error will be
1389 * inserted in middle of the IO.
1390 */
James Smartf9bb2da2011-10-10 21:34:11 -04001391
James Smartacd68592012-01-18 16:25:09 -05001392 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1393 "9076 BLKGRD: Injecting reftag error: "
1394 "write lba x%lx + x%x oldrefTag x%x\n",
1395 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001396 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001397
1398 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001399 * Save the old ref_tag so we can
1400 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001401 */
James Smart9a6b09c2012-03-01 22:37:42 -05001402 if (lpfc_cmd) {
1403 lpfc_cmd->prot_data_type =
1404 LPFC_INJERR_REFTAG;
1405 lpfc_cmd->prot_data_segment =
1406 src;
1407 lpfc_cmd->prot_data =
1408 src->ref_tag;
1409 }
1410 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001411 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001412 if (phba->lpfc_injerr_wref_cnt == 0) {
1413 phba->lpfc_injerr_nportid = 0;
1414 phba->lpfc_injerr_lba =
1415 LPFC_INJERR_LBA_OFF;
1416 memset(&phba->lpfc_injerr_wwpn,
1417 0, sizeof(struct lpfc_name));
1418 }
James Smart9a6b09c2012-03-01 22:37:42 -05001419 rc = BG_ERR_TGT | BG_ERR_CHECK;
1420
James Smartacd68592012-01-18 16:25:09 -05001421 break;
1422 }
1423 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001424 case SCSI_PROT_WRITE_INSERT:
1425 /*
1426 * For WRITE_INSERT, force the error
1427 * to be sent on the wire. It should be
1428 * detected by the Target.
1429 */
1430 /* DEADBEEF will be the reftag on the wire */
1431 *reftag = 0xDEADBEEF;
1432 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001433 if (phba->lpfc_injerr_wref_cnt == 0) {
1434 phba->lpfc_injerr_nportid = 0;
1435 phba->lpfc_injerr_lba =
1436 LPFC_INJERR_LBA_OFF;
1437 memset(&phba->lpfc_injerr_wwpn,
1438 0, sizeof(struct lpfc_name));
1439 }
James Smart9a6b09c2012-03-01 22:37:42 -05001440 rc = BG_ERR_TGT | BG_ERR_CHECK;
1441
1442 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1443 "9078 BLKGRD: Injecting reftag error: "
1444 "write lba x%lx\n", (unsigned long)lba);
1445 break;
James Smartacd68592012-01-18 16:25:09 -05001446 case SCSI_PROT_WRITE_STRIP:
1447 /*
1448 * For WRITE_STRIP and WRITE_PASS,
1449 * force the error on data
1450 * being copied from SLI-Host to SLI-Port.
1451 */
1452 *reftag = 0xDEADBEEF;
1453 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001454 if (phba->lpfc_injerr_wref_cnt == 0) {
1455 phba->lpfc_injerr_nportid = 0;
1456 phba->lpfc_injerr_lba =
1457 LPFC_INJERR_LBA_OFF;
1458 memset(&phba->lpfc_injerr_wwpn,
1459 0, sizeof(struct lpfc_name));
1460 }
James Smartacd68592012-01-18 16:25:09 -05001461 rc = BG_ERR_INIT;
1462
1463 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1464 "9077 BLKGRD: Injecting reftag error: "
1465 "write lba x%lx\n", (unsigned long)lba);
1466 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001467 }
James Smartacd68592012-01-18 16:25:09 -05001468 }
1469 if (phba->lpfc_injerr_rref_cnt) {
1470 switch (op) {
1471 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001472 case SCSI_PROT_READ_STRIP:
1473 case SCSI_PROT_READ_PASS:
1474 /*
1475 * For READ_STRIP and READ_PASS, force the
1476 * error on data being read off the wire. It
1477 * should force an IO error to the driver.
1478 */
James Smartf9bb2da2011-10-10 21:34:11 -04001479 *reftag = 0xDEADBEEF;
1480 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001481 if (phba->lpfc_injerr_rref_cnt == 0) {
1482 phba->lpfc_injerr_nportid = 0;
1483 phba->lpfc_injerr_lba =
1484 LPFC_INJERR_LBA_OFF;
1485 memset(&phba->lpfc_injerr_wwpn,
1486 0, sizeof(struct lpfc_name));
1487 }
James Smartacd68592012-01-18 16:25:09 -05001488 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001489
1490 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001491 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001492 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001493 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001494 }
1495 }
1496 }
1497
1498 /* Should we change the Application Tag */
1499 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001500 if (phba->lpfc_injerr_wapp_cnt) {
1501 switch (op) {
1502 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001503 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001504 /*
1505 * For WRITE_PASS, force the error
1506 * to be sent on the wire. It should
1507 * be detected by the Target.
1508 * If blockoff != 0 error will be
1509 * inserted in middle of the IO.
1510 */
1511
James Smartacd68592012-01-18 16:25:09 -05001512 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1513 "9080 BLKGRD: Injecting apptag error: "
1514 "write lba x%lx + x%x oldappTag x%x\n",
1515 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001516 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001517
1518 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001519 * Save the old app_tag so we can
1520 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001521 */
James Smart9a6b09c2012-03-01 22:37:42 -05001522 if (lpfc_cmd) {
1523 lpfc_cmd->prot_data_type =
1524 LPFC_INJERR_APPTAG;
1525 lpfc_cmd->prot_data_segment =
1526 src;
1527 lpfc_cmd->prot_data =
1528 src->app_tag;
1529 }
1530 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001531 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001532 if (phba->lpfc_injerr_wapp_cnt == 0) {
1533 phba->lpfc_injerr_nportid = 0;
1534 phba->lpfc_injerr_lba =
1535 LPFC_INJERR_LBA_OFF;
1536 memset(&phba->lpfc_injerr_wwpn,
1537 0, sizeof(struct lpfc_name));
1538 }
James Smart9a6b09c2012-03-01 22:37:42 -05001539 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001540 break;
1541 }
1542 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001543 case SCSI_PROT_WRITE_INSERT:
1544 /*
1545 * For WRITE_INSERT, force the
1546 * error to be sent on the wire. It should be
1547 * detected by the Target.
1548 */
1549 /* DEAD will be the apptag on the wire */
1550 *apptag = 0xDEAD;
1551 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001552 if (phba->lpfc_injerr_wapp_cnt == 0) {
1553 phba->lpfc_injerr_nportid = 0;
1554 phba->lpfc_injerr_lba =
1555 LPFC_INJERR_LBA_OFF;
1556 memset(&phba->lpfc_injerr_wwpn,
1557 0, sizeof(struct lpfc_name));
1558 }
James Smart9a6b09c2012-03-01 22:37:42 -05001559 rc = BG_ERR_TGT | BG_ERR_CHECK;
1560
1561 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1562 "0813 BLKGRD: Injecting apptag error: "
1563 "write lba x%lx\n", (unsigned long)lba);
1564 break;
James Smartacd68592012-01-18 16:25:09 -05001565 case SCSI_PROT_WRITE_STRIP:
1566 /*
1567 * For WRITE_STRIP and WRITE_PASS,
1568 * force the error on data
1569 * being copied from SLI-Host to SLI-Port.
1570 */
1571 *apptag = 0xDEAD;
1572 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001573 if (phba->lpfc_injerr_wapp_cnt == 0) {
1574 phba->lpfc_injerr_nportid = 0;
1575 phba->lpfc_injerr_lba =
1576 LPFC_INJERR_LBA_OFF;
1577 memset(&phba->lpfc_injerr_wwpn,
1578 0, sizeof(struct lpfc_name));
1579 }
James Smartacd68592012-01-18 16:25:09 -05001580 rc = BG_ERR_INIT;
1581
1582 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1583 "0812 BLKGRD: Injecting apptag error: "
1584 "write lba x%lx\n", (unsigned long)lba);
1585 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001586 }
James Smartacd68592012-01-18 16:25:09 -05001587 }
1588 if (phba->lpfc_injerr_rapp_cnt) {
1589 switch (op) {
1590 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001591 case SCSI_PROT_READ_STRIP:
1592 case SCSI_PROT_READ_PASS:
1593 /*
1594 * For READ_STRIP and READ_PASS, force the
1595 * error on data being read off the wire. It
1596 * should force an IO error to the driver.
1597 */
James Smartf9bb2da2011-10-10 21:34:11 -04001598 *apptag = 0xDEAD;
1599 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001600 if (phba->lpfc_injerr_rapp_cnt == 0) {
1601 phba->lpfc_injerr_nportid = 0;
1602 phba->lpfc_injerr_lba =
1603 LPFC_INJERR_LBA_OFF;
1604 memset(&phba->lpfc_injerr_wwpn,
1605 0, sizeof(struct lpfc_name));
1606 }
James Smartacd68592012-01-18 16:25:09 -05001607 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001608
1609 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001610 "0814 BLKGRD: Injecting apptag error: "
1611 "read lba x%lx\n", (unsigned long)lba);
1612 break;
1613 }
1614 }
1615 }
1616
1617
1618 /* Should we change the Guard Tag */
1619 if (new_guard) {
1620 if (phba->lpfc_injerr_wgrd_cnt) {
1621 switch (op) {
1622 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001623 rc = BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001624 /* Drop thru */
James Smartacd68592012-01-18 16:25:09 -05001625
James Smartacd68592012-01-18 16:25:09 -05001626 case SCSI_PROT_WRITE_INSERT:
1627 /*
1628 * For WRITE_INSERT, force the
1629 * error to be sent on the wire. It should be
1630 * detected by the Target.
1631 */
1632 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001633 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1634 phba->lpfc_injerr_nportid = 0;
1635 phba->lpfc_injerr_lba =
1636 LPFC_INJERR_LBA_OFF;
1637 memset(&phba->lpfc_injerr_wwpn,
1638 0, sizeof(struct lpfc_name));
1639 }
James Smartacd68592012-01-18 16:25:09 -05001640
James Smart9a6b09c2012-03-01 22:37:42 -05001641 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001642 /* Signals the caller to swap CRC->CSUM */
1643
1644 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1645 "0817 BLKGRD: Injecting guard error: "
1646 "write lba x%lx\n", (unsigned long)lba);
1647 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001648 case SCSI_PROT_WRITE_STRIP:
1649 /*
1650 * For WRITE_STRIP and WRITE_PASS,
1651 * force the error on data
1652 * being copied from SLI-Host to SLI-Port.
1653 */
1654 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001655 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1656 phba->lpfc_injerr_nportid = 0;
1657 phba->lpfc_injerr_lba =
1658 LPFC_INJERR_LBA_OFF;
1659 memset(&phba->lpfc_injerr_wwpn,
1660 0, sizeof(struct lpfc_name));
1661 }
James Smart9a6b09c2012-03-01 22:37:42 -05001662
1663 rc = BG_ERR_INIT | BG_ERR_SWAP;
1664 /* Signals the caller to swap CRC->CSUM */
1665
1666 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1667 "0816 BLKGRD: Injecting guard error: "
1668 "write lba x%lx\n", (unsigned long)lba);
1669 break;
James Smartacd68592012-01-18 16:25:09 -05001670 }
1671 }
1672 if (phba->lpfc_injerr_rgrd_cnt) {
1673 switch (op) {
1674 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001675 case SCSI_PROT_READ_STRIP:
1676 case SCSI_PROT_READ_PASS:
1677 /*
1678 * For READ_STRIP and READ_PASS, force the
1679 * error on data being read off the wire. It
1680 * should force an IO error to the driver.
1681 */
James Smartacd68592012-01-18 16:25:09 -05001682 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001683 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1684 phba->lpfc_injerr_nportid = 0;
1685 phba->lpfc_injerr_lba =
1686 LPFC_INJERR_LBA_OFF;
1687 memset(&phba->lpfc_injerr_wwpn,
1688 0, sizeof(struct lpfc_name));
1689 }
James Smartacd68592012-01-18 16:25:09 -05001690
James Smart9a6b09c2012-03-01 22:37:42 -05001691 rc = BG_ERR_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001692 /* Signals the caller to swap CRC->CSUM */
1693
1694 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1695 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001696 "read lba x%lx\n", (unsigned long)lba);
1697 }
1698 }
1699 }
1700
James Smartf9bb2da2011-10-10 21:34:11 -04001701 return rc;
1702}
1703#endif
1704
James Smartacd68592012-01-18 16:25:09 -05001705/**
1706 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1707 * the specified SCSI command.
1708 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001709 * @sc: The SCSI command to examine
1710 * @txopt: (out) BlockGuard operation for transmitted data
1711 * @rxopt: (out) BlockGuard operation for received data
1712 *
1713 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1714 *
James Smartacd68592012-01-18 16:25:09 -05001715 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001716static int
James Smart6c8eea52010-04-06 14:49:53 -04001717lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1718 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001719{
1720 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
James Smart6c8eea52010-04-06 14:49:53 -04001721 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001722
1723 if (guard_type == SHOST_DIX_GUARD_IP) {
1724 switch (scsi_get_prot_op(sc)) {
1725 case SCSI_PROT_READ_INSERT:
1726 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001727 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001728 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001729 break;
1730
1731 case SCSI_PROT_READ_STRIP:
1732 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001733 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001734 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001735 break;
1736
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001737 case SCSI_PROT_READ_PASS:
1738 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001739 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001740 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001741 break;
1742
James Smarte2a0a9d2008-12-04 22:40:02 -05001743 case SCSI_PROT_NORMAL:
1744 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001745 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001746 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1747 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001748 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001749 break;
1750
1751 }
James Smart7c56b9f2011-07-22 18:36:25 -04001752 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001753 switch (scsi_get_prot_op(sc)) {
1754 case SCSI_PROT_READ_STRIP:
1755 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001756 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001757 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001758 break;
1759
1760 case SCSI_PROT_READ_PASS:
1761 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001762 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001763 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001764 break;
1765
James Smarte2a0a9d2008-12-04 22:40:02 -05001766 case SCSI_PROT_READ_INSERT:
1767 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001768 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001769 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001770 break;
1771
James Smarte2a0a9d2008-12-04 22:40:02 -05001772 case SCSI_PROT_NORMAL:
1773 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001774 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001775 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1776 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001777 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001778 break;
1779 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001780 }
1781
James Smart6c8eea52010-04-06 14:49:53 -04001782 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001783}
1784
James Smartacd68592012-01-18 16:25:09 -05001785#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1786/**
1787 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1788 * the specified SCSI command in order to force a guard tag error.
1789 * @phba: The Hba for which this call is being executed.
1790 * @sc: The SCSI command to examine
1791 * @txopt: (out) BlockGuard operation for transmitted data
1792 * @rxopt: (out) BlockGuard operation for received data
1793 *
1794 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1795 *
1796 **/
1797static int
1798lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1799 uint8_t *txop, uint8_t *rxop)
1800{
1801 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
1802 uint8_t ret = 0;
1803
1804 if (guard_type == SHOST_DIX_GUARD_IP) {
1805 switch (scsi_get_prot_op(sc)) {
1806 case SCSI_PROT_READ_INSERT:
1807 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001808 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001809 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001810 break;
1811
1812 case SCSI_PROT_READ_STRIP:
1813 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001814 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001815 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001816 break;
1817
1818 case SCSI_PROT_READ_PASS:
1819 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001820 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001821 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001822 break;
1823
1824 case SCSI_PROT_NORMAL:
1825 default:
1826 break;
1827
1828 }
1829 } else {
1830 switch (scsi_get_prot_op(sc)) {
1831 case SCSI_PROT_READ_STRIP:
1832 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001833 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001834 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001835 break;
1836
1837 case SCSI_PROT_READ_PASS:
1838 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001839 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05001840 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001841 break;
1842
1843 case SCSI_PROT_READ_INSERT:
1844 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001845 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001846 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001847 break;
1848
1849 case SCSI_PROT_NORMAL:
1850 default:
1851 break;
1852 }
1853 }
1854
1855 return ret;
1856}
1857#endif
1858
1859/**
1860 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1861 * @phba: The Hba for which this call is being executed.
1862 * @sc: pointer to scsi command we're working on
1863 * @bpl: pointer to buffer list for protection groups
1864 * @datacnt: number of segments of data that have been dma mapped
1865 *
1866 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05001867 * type LPFC_PG_TYPE_NO_DIF
1868 *
1869 * This is usually used when the HBA is instructed to generate
1870 * DIFs and insert them into data stream (or strip DIF from
1871 * incoming data stream)
1872 *
1873 * The buffer list consists of just one protection group described
1874 * below:
1875 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001876 * start of prot group --> | PDE_5 |
1877 * +-------------------------+
1878 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001879 * +-------------------------+
1880 * | Data BDE |
1881 * +-------------------------+
1882 * |more Data BDE's ... (opt)|
1883 * +-------------------------+
1884 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001885 *
1886 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05001887 *
1888 * Returns the number of BDEs added to the BPL.
1889 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001890static int
1891lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1892 struct ulp_bde64 *bpl, int datasegcnt)
1893{
1894 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001895 struct lpfc_pde5 *pde5 = NULL;
1896 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001897 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001898 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001899 int datadir = sc->sc_data_direction;
James Smartacd68592012-01-18 16:25:09 -05001900 uint32_t rc;
1901 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001902 uint32_t reftag;
James Smart7c56b9f2011-07-22 18:36:25 -04001903 unsigned blksize;
James Smart6c8eea52010-04-06 14:49:53 -04001904 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001905
James Smart6c8eea52010-04-06 14:49:53 -04001906 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1907 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001908 goto out;
1909
James Smart6c8eea52010-04-06 14:49:53 -04001910 /* extract some info from the scsi command for pde*/
James Smarte2a0a9d2008-12-04 22:40:02 -05001911 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05001912 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05001913
James Smartf9bb2da2011-10-10 21:34:11 -04001914#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05001915 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05001916 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05001917 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05001918 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05001919 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05001920 checking = 0;
1921 }
James Smartf9bb2da2011-10-10 21:34:11 -04001922#endif
1923
James Smart6c8eea52010-04-06 14:49:53 -04001924 /* setup PDE5 with what we have */
1925 pde5 = (struct lpfc_pde5 *) bpl;
1926 memset(pde5, 0, sizeof(struct lpfc_pde5));
1927 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001928
James Smartbc739052010-08-04 16:11:18 -04001929 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001930 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001931 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001932
James Smart6c8eea52010-04-06 14:49:53 -04001933 /* advance bpl and increment bde count */
1934 num_bde++;
1935 bpl++;
1936 pde6 = (struct lpfc_pde6 *) bpl;
1937
1938 /* setup PDE6 with the rest of the info */
1939 memset(pde6, 0, sizeof(struct lpfc_pde6));
1940 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1941 bf_set(pde6_optx, pde6, txop);
1942 bf_set(pde6_oprx, pde6, rxop);
1943 if (datadir == DMA_FROM_DEVICE) {
James Smartacd68592012-01-18 16:25:09 -05001944 bf_set(pde6_ce, pde6, checking);
1945 bf_set(pde6_re, pde6, checking);
James Smart6c8eea52010-04-06 14:49:53 -04001946 }
1947 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001948 bf_set(pde6_ae, pde6, 0);
1949 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001950
James Smartbc739052010-08-04 16:11:18 -04001951 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001952 pde6->word0 = cpu_to_le32(pde6->word0);
1953 pde6->word1 = cpu_to_le32(pde6->word1);
1954 pde6->word2 = cpu_to_le32(pde6->word2);
1955
James Smart6c8eea52010-04-06 14:49:53 -04001956 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001957 num_bde++;
1958 bpl++;
1959
1960 /* assumption: caller has already run dma_map_sg on command data */
1961 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1962 physaddr = sg_dma_address(sgde);
1963 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1964 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1965 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1966 if (datadir == DMA_TO_DEVICE)
1967 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1968 else
1969 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1970 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1971 bpl++;
1972 num_bde++;
1973 }
1974
1975out:
1976 return num_bde;
1977}
1978
James Smartacd68592012-01-18 16:25:09 -05001979/**
1980 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
1981 * @phba: The Hba for which this call is being executed.
1982 * @sc: pointer to scsi command we're working on
1983 * @bpl: pointer to buffer list for protection groups
1984 * @datacnt: number of segments of data that have been dma mapped
1985 * @protcnt: number of segment of protection data that have been dma mapped
1986 *
1987 * This function sets up BPL buffer list for protection groups of
1988 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05001989 *
1990 * This is usually used when DIFs are in their own buffers,
1991 * separate from the data. The HBA can then by instructed
1992 * to place the DIFs in the outgoing stream. For read operations,
1993 * The HBA could extract the DIFs and place it in DIF buffers.
1994 *
1995 * The buffer list for this type consists of one or more of the
1996 * protection groups described below:
1997 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001998 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001999 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002000 * | PDE_6 |
2001 * +-------------------------+
2002 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05002003 * +-------------------------+
2004 * | Data BDE |
2005 * +-------------------------+
2006 * |more Data BDE's ... (opt)|
2007 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002008 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002009 * +-------------------------+
2010 * | ... |
2011 * +-------------------------+
2012 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002013 * Note: It is assumed that both data and protection s/g buffers have been
2014 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05002015 *
2016 * Returns the number of BDEs added to the BPL.
2017 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002018static int
2019lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2020 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2021{
2022 struct scatterlist *sgde = NULL; /* s/g data entry */
2023 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04002024 struct lpfc_pde5 *pde5 = NULL;
2025 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05002026 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002027 dma_addr_t dataphysaddr, protphysaddr;
2028 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05002029 unsigned int split_offset;
2030 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05002031 unsigned int protgrp_blks, protgrp_bytes;
2032 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04002033 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002034 int datadir = sc->sc_data_direction;
2035 unsigned char pgdone = 0, alldone = 0;
2036 unsigned blksize;
James Smartacd68592012-01-18 16:25:09 -05002037 uint32_t rc;
2038 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002039 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04002040 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002041 int num_bde = 0;
2042
2043 sgpe = scsi_prot_sglist(sc);
2044 sgde = scsi_sglist(sc);
2045
2046 if (!sgpe || !sgde) {
2047 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2048 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2049 sgpe, sgde);
2050 return 0;
2051 }
2052
James Smart6c8eea52010-04-06 14:49:53 -04002053 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2054 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002055 goto out;
2056
James Smart6c8eea52010-04-06 14:49:53 -04002057 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05002058 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002059 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002060
James Smartf9bb2da2011-10-10 21:34:11 -04002061#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002062 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002063 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002064 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002065 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002066 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002067 checking = 0;
2068 }
James Smartf9bb2da2011-10-10 21:34:11 -04002069#endif
2070
James Smarte2a0a9d2008-12-04 22:40:02 -05002071 split_offset = 0;
2072 do {
James Smart6c8eea52010-04-06 14:49:53 -04002073 /* setup PDE5 with what we have */
2074 pde5 = (struct lpfc_pde5 *) bpl;
2075 memset(pde5, 0, sizeof(struct lpfc_pde5));
2076 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002077
James Smartbc739052010-08-04 16:11:18 -04002078 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002079 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002080 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002081
James Smart6c8eea52010-04-06 14:49:53 -04002082 /* advance bpl and increment bde count */
2083 num_bde++;
2084 bpl++;
2085 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002086
James Smart6c8eea52010-04-06 14:49:53 -04002087 /* setup PDE6 with the rest of the info */
2088 memset(pde6, 0, sizeof(struct lpfc_pde6));
2089 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2090 bf_set(pde6_optx, pde6, txop);
2091 bf_set(pde6_oprx, pde6, rxop);
James Smartacd68592012-01-18 16:25:09 -05002092 bf_set(pde6_ce, pde6, checking);
2093 bf_set(pde6_re, pde6, checking);
James Smart6c8eea52010-04-06 14:49:53 -04002094 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002095 bf_set(pde6_ae, pde6, 0);
2096 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002097
James Smartbc739052010-08-04 16:11:18 -04002098 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002099 pde6->word0 = cpu_to_le32(pde6->word0);
2100 pde6->word1 = cpu_to_le32(pde6->word1);
2101 pde6->word2 = cpu_to_le32(pde6->word2);
2102
James Smart6c8eea52010-04-06 14:49:53 -04002103 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002104 num_bde++;
2105 bpl++;
2106
2107 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05002108 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2109 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05002110
James Smarte2a0a9d2008-12-04 22:40:02 -05002111 /* must be integer multiple of the DIF block length */
2112 BUG_ON(protgroup_len % 8);
2113
James Smart7f860592011-03-11 16:05:52 -05002114 pde7 = (struct lpfc_pde7 *) bpl;
2115 memset(pde7, 0, sizeof(struct lpfc_pde7));
2116 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2117
James Smart7c56b9f2011-07-22 18:36:25 -04002118 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2119 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05002120
James Smarte2a0a9d2008-12-04 22:40:02 -05002121 protgrp_blks = protgroup_len / 8;
2122 protgrp_bytes = protgrp_blks * blksize;
2123
James Smart7f860592011-03-11 16:05:52 -05002124 /* check if this pde is crossing the 4K boundary; if so split */
2125 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2126 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2127 protgroup_offset += protgroup_remainder;
2128 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04002129 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05002130 } else {
2131 protgroup_offset = 0;
2132 curr_prot++;
2133 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002134
James Smarte2a0a9d2008-12-04 22:40:02 -05002135 num_bde++;
2136
2137 /* setup BDE's for data blocks associated with DIF data */
2138 pgdone = 0;
2139 subtotal = 0; /* total bytes processed for current prot grp */
2140 while (!pgdone) {
2141 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04002142 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2143 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002144 __func__);
2145 return 0;
2146 }
2147 bpl++;
2148 dataphysaddr = sg_dma_address(sgde) + split_offset;
2149 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2150 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2151
2152 remainder = sg_dma_len(sgde) - split_offset;
2153
2154 if ((subtotal + remainder) <= protgrp_bytes) {
2155 /* we can use this whole buffer */
2156 bpl->tus.f.bdeSize = remainder;
2157 split_offset = 0;
2158
2159 if ((subtotal + remainder) == protgrp_bytes)
2160 pgdone = 1;
2161 } else {
2162 /* must split this buffer with next prot grp */
2163 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2164 split_offset += bpl->tus.f.bdeSize;
2165 }
2166
2167 subtotal += bpl->tus.f.bdeSize;
2168
2169 if (datadir == DMA_TO_DEVICE)
2170 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2171 else
2172 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2173 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2174
2175 num_bde++;
2176 curr_data++;
2177
2178 if (split_offset)
2179 break;
2180
2181 /* Move to the next s/g segment if possible */
2182 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04002183
James Smarte2a0a9d2008-12-04 22:40:02 -05002184 }
2185
James Smart7f860592011-03-11 16:05:52 -05002186 if (protgroup_offset) {
2187 /* update the reference tag */
2188 reftag += protgrp_blks;
2189 bpl++;
2190 continue;
2191 }
2192
James Smarte2a0a9d2008-12-04 22:40:02 -05002193 /* are we done ? */
2194 if (curr_prot == protcnt) {
2195 alldone = 1;
2196 } else if (curr_prot < protcnt) {
2197 /* advance to next prot buffer */
2198 sgpe = sg_next(sgpe);
2199 bpl++;
2200
2201 /* update the reference tag */
2202 reftag += protgrp_blks;
2203 } else {
2204 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04002205 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2206 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05002207 }
2208
2209 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05002210out:
2211
James Smarte2a0a9d2008-12-04 22:40:02 -05002212 return num_bde;
2213}
James Smart7f860592011-03-11 16:05:52 -05002214
James Smartacd68592012-01-18 16:25:09 -05002215/**
2216 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2217 * @phba: The Hba for which this call is being executed.
2218 * @sc: pointer to scsi command we're working on
2219 * @sgl: pointer to buffer list for protection groups
2220 * @datacnt: number of segments of data that have been dma mapped
2221 *
2222 * This function sets up SGL buffer list for protection groups of
2223 * type LPFC_PG_TYPE_NO_DIF
2224 *
2225 * This is usually used when the HBA is instructed to generate
2226 * DIFs and insert them into data stream (or strip DIF from
2227 * incoming data stream)
2228 *
2229 * The buffer list consists of just one protection group described
2230 * below:
2231 * +-------------------------+
2232 * start of prot group --> | DI_SEED |
2233 * +-------------------------+
2234 * | Data SGE |
2235 * +-------------------------+
2236 * |more Data SGE's ... (opt)|
2237 * +-------------------------+
2238 *
2239 *
2240 * Note: Data s/g buffers have been dma mapped
2241 *
2242 * Returns the number of SGEs added to the SGL.
2243 **/
2244static int
2245lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2246 struct sli4_sge *sgl, int datasegcnt)
2247{
2248 struct scatterlist *sgde = NULL; /* s/g data entry */
2249 struct sli4_sge_diseed *diseed = NULL;
2250 dma_addr_t physaddr;
2251 int i = 0, num_sge = 0, status;
2252 int datadir = sc->sc_data_direction;
2253 uint32_t reftag;
2254 unsigned blksize;
2255 uint8_t txop, rxop;
2256 uint32_t rc;
2257 uint32_t checking = 1;
2258 uint32_t dma_len;
2259 uint32_t dma_offset = 0;
2260
2261 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2262 if (status)
2263 goto out;
2264
2265 /* extract some info from the scsi command for pde*/
2266 blksize = lpfc_cmd_blksize(sc);
2267 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2268
2269#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002270 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002271 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002272 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002273 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002274 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002275 checking = 0;
2276 }
2277#endif
2278
2279 /* setup DISEED with what we have */
2280 diseed = (struct sli4_sge_diseed *) sgl;
2281 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2282 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2283
2284 /* Endianness conversion if necessary */
2285 diseed->ref_tag = cpu_to_le32(reftag);
2286 diseed->ref_tag_tran = diseed->ref_tag;
2287
2288 /* setup DISEED with the rest of the info */
2289 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2290 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2291 if (datadir == DMA_FROM_DEVICE) {
2292 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2293 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2294 }
2295 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2296 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2297
2298 /* Endianness conversion if necessary for DISEED */
2299 diseed->word2 = cpu_to_le32(diseed->word2);
2300 diseed->word3 = cpu_to_le32(diseed->word3);
2301
2302 /* advance bpl and increment sge count */
2303 num_sge++;
2304 sgl++;
2305
2306 /* assumption: caller has already run dma_map_sg on command data */
2307 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2308 physaddr = sg_dma_address(sgde);
2309 dma_len = sg_dma_len(sgde);
2310 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2311 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2312 if ((i + 1) == datasegcnt)
2313 bf_set(lpfc_sli4_sge_last, sgl, 1);
2314 else
2315 bf_set(lpfc_sli4_sge_last, sgl, 0);
2316 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2317 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2318
2319 sgl->sge_len = cpu_to_le32(dma_len);
2320 dma_offset += dma_len;
2321
2322 sgl++;
2323 num_sge++;
2324 }
2325
2326out:
2327 return num_sge;
2328}
2329
2330/**
2331 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2332 * @phba: The Hba for which this call is being executed.
2333 * @sc: pointer to scsi command we're working on
2334 * @sgl: pointer to buffer list for protection groups
2335 * @datacnt: number of segments of data that have been dma mapped
2336 * @protcnt: number of segment of protection data that have been dma mapped
2337 *
2338 * This function sets up SGL buffer list for protection groups of
2339 * type LPFC_PG_TYPE_DIF
2340 *
2341 * This is usually used when DIFs are in their own buffers,
2342 * separate from the data. The HBA can then by instructed
2343 * to place the DIFs in the outgoing stream. For read operations,
2344 * The HBA could extract the DIFs and place it in DIF buffers.
2345 *
2346 * The buffer list for this type consists of one or more of the
2347 * protection groups described below:
2348 * +-------------------------+
2349 * start of first prot group --> | DISEED |
2350 * +-------------------------+
2351 * | DIF (Prot SGE) |
2352 * +-------------------------+
2353 * | Data SGE |
2354 * +-------------------------+
2355 * |more Data SGE's ... (opt)|
2356 * +-------------------------+
2357 * start of new prot group --> | DISEED |
2358 * +-------------------------+
2359 * | ... |
2360 * +-------------------------+
2361 *
2362 * Note: It is assumed that both data and protection s/g buffers have been
2363 * mapped for DMA
2364 *
2365 * Returns the number of SGEs added to the SGL.
2366 **/
2367static int
2368lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2369 struct sli4_sge *sgl, int datacnt, int protcnt)
2370{
2371 struct scatterlist *sgde = NULL; /* s/g data entry */
2372 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2373 struct sli4_sge_diseed *diseed = NULL;
2374 dma_addr_t dataphysaddr, protphysaddr;
2375 unsigned short curr_data = 0, curr_prot = 0;
2376 unsigned int split_offset;
2377 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2378 unsigned int protgrp_blks, protgrp_bytes;
2379 unsigned int remainder, subtotal;
2380 int status;
2381 unsigned char pgdone = 0, alldone = 0;
2382 unsigned blksize;
2383 uint32_t reftag;
2384 uint8_t txop, rxop;
2385 uint32_t dma_len;
2386 uint32_t rc;
2387 uint32_t checking = 1;
2388 uint32_t dma_offset = 0;
2389 int num_sge = 0;
2390
2391 sgpe = scsi_prot_sglist(sc);
2392 sgde = scsi_sglist(sc);
2393
2394 if (!sgpe || !sgde) {
2395 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2396 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2397 sgpe, sgde);
2398 return 0;
2399 }
2400
2401 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2402 if (status)
2403 goto out;
2404
2405 /* extract some info from the scsi command */
2406 blksize = lpfc_cmd_blksize(sc);
2407 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2408
2409#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002410 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002411 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002412 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002413 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002414 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002415 checking = 0;
2416 }
2417#endif
2418
2419 split_offset = 0;
2420 do {
2421 /* setup DISEED with what we have */
2422 diseed = (struct sli4_sge_diseed *) sgl;
2423 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2424 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2425
2426 /* Endianness conversion if necessary */
2427 diseed->ref_tag = cpu_to_le32(reftag);
2428 diseed->ref_tag_tran = diseed->ref_tag;
2429
2430 /* setup DISEED with the rest of the info */
2431 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2432 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2433 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2434 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2435 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2436 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2437
2438 /* Endianness conversion if necessary for DISEED */
2439 diseed->word2 = cpu_to_le32(diseed->word2);
2440 diseed->word3 = cpu_to_le32(diseed->word3);
2441
2442 /* advance sgl and increment bde count */
2443 num_sge++;
2444 sgl++;
2445
2446 /* setup the first BDE that points to protection buffer */
2447 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2448 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2449
2450 /* must be integer multiple of the DIF block length */
2451 BUG_ON(protgroup_len % 8);
2452
2453 /* Now setup DIF SGE */
2454 sgl->word2 = 0;
2455 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2456 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2457 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2458 sgl->word2 = cpu_to_le32(sgl->word2);
2459
2460 protgrp_blks = protgroup_len / 8;
2461 protgrp_bytes = protgrp_blks * blksize;
2462
2463 /* check if DIF SGE is crossing the 4K boundary; if so split */
2464 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2465 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2466 protgroup_offset += protgroup_remainder;
2467 protgrp_blks = protgroup_remainder / 8;
2468 protgrp_bytes = protgrp_blks * blksize;
2469 } else {
2470 protgroup_offset = 0;
2471 curr_prot++;
2472 }
2473
2474 num_sge++;
2475
2476 /* setup SGE's for data blocks associated with DIF data */
2477 pgdone = 0;
2478 subtotal = 0; /* total bytes processed for current prot grp */
2479 while (!pgdone) {
2480 if (!sgde) {
2481 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2482 "9086 BLKGRD:%s Invalid data segment\n",
2483 __func__);
2484 return 0;
2485 }
2486 sgl++;
2487 dataphysaddr = sg_dma_address(sgde) + split_offset;
2488
2489 remainder = sg_dma_len(sgde) - split_offset;
2490
2491 if ((subtotal + remainder) <= protgrp_bytes) {
2492 /* we can use this whole buffer */
2493 dma_len = remainder;
2494 split_offset = 0;
2495
2496 if ((subtotal + remainder) == protgrp_bytes)
2497 pgdone = 1;
2498 } else {
2499 /* must split this buffer with next prot grp */
2500 dma_len = protgrp_bytes - subtotal;
2501 split_offset += dma_len;
2502 }
2503
2504 subtotal += dma_len;
2505
2506 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2507 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2508 bf_set(lpfc_sli4_sge_last, sgl, 0);
2509 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2510 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2511
2512 sgl->sge_len = cpu_to_le32(dma_len);
2513 dma_offset += dma_len;
2514
2515 num_sge++;
2516 curr_data++;
2517
2518 if (split_offset)
2519 break;
2520
2521 /* Move to the next s/g segment if possible */
2522 sgde = sg_next(sgde);
2523 }
2524
2525 if (protgroup_offset) {
2526 /* update the reference tag */
2527 reftag += protgrp_blks;
2528 sgl++;
2529 continue;
2530 }
2531
2532 /* are we done ? */
2533 if (curr_prot == protcnt) {
2534 bf_set(lpfc_sli4_sge_last, sgl, 1);
2535 alldone = 1;
2536 } else if (curr_prot < protcnt) {
2537 /* advance to next prot buffer */
2538 sgpe = sg_next(sgpe);
2539 sgl++;
2540
2541 /* update the reference tag */
2542 reftag += protgrp_blks;
2543 } else {
2544 /* if we're here, we have a bug */
2545 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2546 "9085 BLKGRD: bug in %s\n", __func__);
2547 }
2548
2549 } while (!alldone);
2550
2551out:
2552
2553 return num_sge;
2554}
2555
2556/**
2557 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2558 * @phba: The Hba for which this call is being executed.
2559 * @sc: pointer to scsi command we're working on
2560 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002561 * Given a SCSI command that supports DIF, determine composition of protection
2562 * groups involved in setting up buffer lists
2563 *
James Smartacd68592012-01-18 16:25:09 -05002564 * Returns: Protection group type (with or without DIF)
2565 *
2566 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002567static int
2568lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2569{
2570 int ret = LPFC_PG_TYPE_INVALID;
2571 unsigned char op = scsi_get_prot_op(sc);
2572
2573 switch (op) {
2574 case SCSI_PROT_READ_STRIP:
2575 case SCSI_PROT_WRITE_INSERT:
2576 ret = LPFC_PG_TYPE_NO_DIF;
2577 break;
2578 case SCSI_PROT_READ_INSERT:
2579 case SCSI_PROT_WRITE_STRIP:
2580 case SCSI_PROT_READ_PASS:
2581 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002582 ret = LPFC_PG_TYPE_DIF_BUF;
2583 break;
2584 default:
2585 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2586 "9021 Unsupported protection op:%d\n", op);
2587 break;
2588 }
2589
2590 return ret;
2591}
2592
James Smartacd68592012-01-18 16:25:09 -05002593/**
2594 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2595 * @phba: The Hba for which this call is being executed.
2596 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2597 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002598 * This is the protection/DIF aware version of
2599 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2600 * two functions eventually, but for now, it's here
James Smartacd68592012-01-18 16:25:09 -05002601 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002602static int
James Smartacd68592012-01-18 16:25:09 -05002603lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smarte2a0a9d2008-12-04 22:40:02 -05002604 struct lpfc_scsi_buf *lpfc_cmd)
2605{
2606 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2607 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2608 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2609 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2610 uint32_t num_bde = 0;
2611 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2612 int prot_group_type = 0;
2613 int diflen, fcpdl;
2614 unsigned blksize;
2615
2616 /*
2617 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2618 * fcp_rsp regions to the first data bde entry
2619 */
2620 bpl += 2;
2621 if (scsi_sg_count(scsi_cmnd)) {
2622 /*
2623 * The driver stores the segment count returned from pci_map_sg
2624 * because this a count of dma-mappings used to map the use_sg
2625 * pages. They are not guaranteed to be the same for those
2626 * architectures that implement an IOMMU.
2627 */
2628 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2629 scsi_sglist(scsi_cmnd),
2630 scsi_sg_count(scsi_cmnd), datadir);
2631 if (unlikely(!datasegcnt))
2632 return 1;
2633
2634 lpfc_cmd->seg_cnt = datasegcnt;
2635 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04002636 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2637 "9067 BLKGRD: %s: Too many sg segments"
2638 " from dma_map_sg. Config %d, seg_cnt"
2639 " %d\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002640 __func__, phba->cfg_sg_seg_cnt,
2641 lpfc_cmd->seg_cnt);
2642 scsi_dma_unmap(scsi_cmnd);
2643 return 1;
2644 }
2645
2646 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2647
2648 switch (prot_group_type) {
2649 case LPFC_PG_TYPE_NO_DIF:
2650 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2651 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002652 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002653 if (num_bde < 2)
2654 goto err;
2655 break;
2656 case LPFC_PG_TYPE_DIF_BUF:{
2657 /*
2658 * This type indicates that protection buffers are
2659 * passed to the driver, so that needs to be prepared
2660 * for DMA
2661 */
2662 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2663 scsi_prot_sglist(scsi_cmnd),
2664 scsi_prot_sg_count(scsi_cmnd), datadir);
2665 if (unlikely(!protsegcnt)) {
2666 scsi_dma_unmap(scsi_cmnd);
2667 return 1;
2668 }
2669
2670 lpfc_cmd->prot_seg_cnt = protsegcnt;
2671 if (lpfc_cmd->prot_seg_cnt
2672 > phba->cfg_prot_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04002673 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2674 "9068 BLKGRD: %s: Too many prot sg "
2675 "segments from dma_map_sg. Config %d,"
James Smarte2a0a9d2008-12-04 22:40:02 -05002676 "prot_seg_cnt %d\n", __func__,
2677 phba->cfg_prot_sg_seg_cnt,
2678 lpfc_cmd->prot_seg_cnt);
2679 dma_unmap_sg(&phba->pcidev->dev,
2680 scsi_prot_sglist(scsi_cmnd),
2681 scsi_prot_sg_count(scsi_cmnd),
2682 datadir);
2683 scsi_dma_unmap(scsi_cmnd);
2684 return 1;
2685 }
2686
2687 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2688 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002689 /* we should have 3 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002690 if (num_bde < 3)
2691 goto err;
2692 break;
2693 }
2694 case LPFC_PG_TYPE_INVALID:
2695 default:
2696 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2697 "9022 Unexpected protection group %i\n",
2698 prot_group_type);
2699 return 1;
2700 }
2701 }
2702
2703 /*
2704 * Finish initializing those IOCB fields that are dependent on the
2705 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2706 * reinitialized since all iocb memory resources are used many times
2707 * for transmit, receive, and continuation bpl's.
2708 */
2709 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2710 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2711 iocb_cmd->ulpBdeCount = 1;
2712 iocb_cmd->ulpLe = 1;
2713
2714 fcpdl = scsi_bufflen(scsi_cmnd);
2715
2716 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
2717 /*
2718 * We are in DIF Type 1 mode
2719 * Every data block has a 8 byte DIF (trailer)
2720 * attached to it. Must ajust FCP data length
2721 */
2722 blksize = lpfc_cmd_blksize(scsi_cmnd);
2723 diflen = (fcpdl / blksize) * 8;
2724 fcpdl += diflen;
2725 }
2726 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2727
2728 /*
2729 * Due to difference in data length between DIF/non-DIF paths,
2730 * we need to set word 4 of IOCB here
2731 */
2732 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2733
2734 return 0;
2735err:
2736 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2737 "9023 Could not setup all needed BDE's"
2738 "prot_group_type=%d, num_bde=%d\n",
2739 prot_group_type, num_bde);
2740 return 1;
2741}
2742
2743/*
2744 * This function checks for BlockGuard errors detected by
2745 * the HBA. In case of errors, the ASC/ASCQ fields in the
2746 * sense buffer will be set accordingly, paired with
2747 * ILLEGAL_REQUEST to signal to the kernel that the HBA
2748 * detected corruption.
2749 *
2750 * Returns:
2751 * 0 - No error found
2752 * 1 - BlockGuard error found
2753 * -1 - Internal error (bad profile, ...etc)
2754 */
2755static int
2756lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
2757 struct lpfc_iocbq *pIocbOut)
2758{
2759 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2760 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
2761 int ret = 0;
2762 uint32_t bghm = bgf->bghm;
2763 uint32_t bgstat = bgf->bgstat;
2764 uint64_t failing_sector = 0;
2765
James Smart6a9c52c2009-10-02 15:16:51 -04002766 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
2767 " 0x%x lba 0x%llx blk cnt 0x%x "
James Smarte2a0a9d2008-12-04 22:40:02 -05002768 "bgstat=0x%x bghm=0x%x\n",
James Smart87b5c322008-12-16 10:34:09 -05002769 cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
Tejun Heo83096eb2009-05-07 22:24:39 +09002770 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002771
2772 spin_lock(&_dump_buf_lock);
2773 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04002774 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
2775 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002776 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04002777 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002778
2779 /* If we have a prot sgl, save the DIF buffer */
2780 if (lpfc_prot_group_type(phba, cmd) ==
2781 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04002782 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
2783 "Saving DIF for %u blocks to debugfs\n",
2784 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
2785 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002786 }
2787
2788 _dump_buf_done = 1;
2789 }
2790 spin_unlock(&_dump_buf_lock);
2791
2792 if (lpfc_bgs_get_invalid_prof(bgstat)) {
2793 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002794 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
2795 " BlockGuard profile. bgstat:0x%x\n",
2796 bgstat);
James Smarte2a0a9d2008-12-04 22:40:02 -05002797 ret = (-1);
2798 goto out;
2799 }
2800
2801 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
2802 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002803 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
2804 "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002805 bgstat);
2806 ret = (-1);
2807 goto out;
2808 }
2809
2810 if (lpfc_bgs_get_guard_err(bgstat)) {
2811 ret = 1;
2812
2813 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2814 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002815 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002816 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2817 phba->bg_guard_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002818 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2819 "9055 BLKGRD: guard_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002820 }
2821
2822 if (lpfc_bgs_get_reftag_err(bgstat)) {
2823 ret = 1;
2824
2825 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2826 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002827 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002828 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2829
2830 phba->bg_reftag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002831 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2832 "9056 BLKGRD: ref_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002833 }
2834
2835 if (lpfc_bgs_get_apptag_err(bgstat)) {
2836 ret = 1;
2837
2838 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2839 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002840 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002841 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2842
2843 phba->bg_apptag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002844 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2845 "9061 BLKGRD: app_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002846 }
2847
2848 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
2849 /*
2850 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04002851 * field, and put the failing LBA in it.
2852 * This code assumes there was also a guard/app/ref tag error
2853 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05002854 */
James Smart7c56b9f2011-07-22 18:36:25 -04002855 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
2856 cmd->sense_buffer[8] = 0; /* Information descriptor type */
2857 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
2858 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05002859
2860 /* bghm is a "on the wire" FC frame based count */
2861 switch (scsi_get_prot_op(cmd)) {
2862 case SCSI_PROT_READ_INSERT:
2863 case SCSI_PROT_WRITE_STRIP:
2864 bghm /= cmd->device->sector_size;
2865 break;
2866 case SCSI_PROT_READ_STRIP:
2867 case SCSI_PROT_WRITE_INSERT:
2868 case SCSI_PROT_READ_PASS:
2869 case SCSI_PROT_WRITE_PASS:
2870 bghm /= (cmd->device->sector_size +
2871 sizeof(struct scsi_dif_tuple));
2872 break;
2873 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002874
2875 failing_sector = scsi_get_lba(cmd);
2876 failing_sector += bghm;
2877
James Smart7c56b9f2011-07-22 18:36:25 -04002878 /* Descriptor Information */
2879 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05002880 }
2881
2882 if (!ret) {
2883 /* No error was reported - problem in FW? */
2884 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002885 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart4ac9b222012-03-01 22:38:29 -05002886 "9057 BLKGRD: Unknown error reported!\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002887 }
2888
2889out:
2890 return ret;
2891}
2892
James Smartea2151b2008-09-07 11:52:10 -04002893/**
James Smartda0436e2009-05-22 14:51:39 -04002894 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
2895 * @phba: The Hba for which this call is being executed.
2896 * @lpfc_cmd: The scsi buffer which is going to be mapped.
2897 *
2898 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
2899 * field of @lpfc_cmd for device with SLI-4 interface spec.
2900 *
2901 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04002902 * 1 - Error
2903 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04002904 **/
2905static int
2906lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2907{
2908 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2909 struct scatterlist *sgel = NULL;
2910 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2911 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
James Smartfedd3b72011-02-16 12:39:24 -05002912 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04002913 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2914 dma_addr_t physaddr;
2915 uint32_t num_bde = 0;
2916 uint32_t dma_len;
2917 uint32_t dma_offset = 0;
2918 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05002919 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04002920
2921 /*
2922 * There are three possibilities here - use scatter-gather segment, use
2923 * the single mapping, or neither. Start the lpfc command prep by
2924 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2925 * data bde entry.
2926 */
2927 if (scsi_sg_count(scsi_cmnd)) {
2928 /*
2929 * The driver stores the segment count returned from pci_map_sg
2930 * because this a count of dma-mappings used to map the use_sg
2931 * pages. They are not guaranteed to be the same for those
2932 * architectures that implement an IOMMU.
2933 */
2934
2935 nseg = scsi_dma_map(scsi_cmnd);
2936 if (unlikely(!nseg))
2937 return 1;
2938 sgl += 1;
2939 /* clear the last flag in the fcp_rsp map entry */
2940 sgl->word2 = le32_to_cpu(sgl->word2);
2941 bf_set(lpfc_sli4_sge_last, sgl, 0);
2942 sgl->word2 = cpu_to_le32(sgl->word2);
2943 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05002944 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04002945 lpfc_cmd->seg_cnt = nseg;
2946 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04002947 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
2948 " %s: Too many sg segments from "
2949 "dma_map_sg. Config %d, seg_cnt %d\n",
2950 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04002951 lpfc_cmd->seg_cnt);
2952 scsi_dma_unmap(scsi_cmnd);
2953 return 1;
2954 }
2955
2956 /*
2957 * The driver established a maximum scatter-gather segment count
2958 * during probe that limits the number of sg elements in any
2959 * single scsi command. Just run through the seg_cnt and format
2960 * the sge's.
2961 * When using SLI-3 the driver will try to fit all the BDEs into
2962 * the IOCB. If it can't then the BDEs get added to a BPL as it
2963 * does for SLI-2 mode.
2964 */
2965 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
2966 physaddr = sg_dma_address(sgel);
2967 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04002968 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2969 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04002970 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04002971 if ((num_bde + 1) == nseg)
2972 bf_set(lpfc_sli4_sge_last, sgl, 1);
2973 else
2974 bf_set(lpfc_sli4_sge_last, sgl, 0);
2975 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04002976 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04002977 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05002978 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04002979 dma_offset += dma_len;
2980 sgl++;
2981 }
James Smartfedd3b72011-02-16 12:39:24 -05002982 /* setup the performance hint (first data BDE) if enabled */
2983 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
2984 bde = (struct ulp_bde64 *)
2985 &(iocb_cmd->unsli3.sli3Words[5]);
2986 bde->addrLow = first_data_sgl->addr_lo;
2987 bde->addrHigh = first_data_sgl->addr_hi;
2988 bde->tus.f.bdeSize =
2989 le32_to_cpu(first_data_sgl->sge_len);
2990 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2991 bde->tus.w = cpu_to_le32(bde->tus.w);
2992 }
James Smartda0436e2009-05-22 14:51:39 -04002993 } else {
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, 1);
2998 sgl->word2 = cpu_to_le32(sgl->word2);
2999 }
3000
3001 /*
3002 * Finish initializing those IOCB fields that are dependent on the
3003 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3004 * explicitly reinitialized.
3005 * all iocb memory resources are reused.
3006 */
3007 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3008
3009 /*
3010 * Due to difference in data length between DIF/non-DIF paths,
3011 * we need to set word 4 of IOCB here
3012 */
3013 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
3014 return 0;
3015}
3016
3017/**
James Smartacd68592012-01-18 16:25:09 -05003018 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
3019 * @phba: The Hba for which this call is being executed.
3020 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
3021 *
3022 * Adjust the data length to account for how much data
3023 * is actually on the wire.
3024 *
3025 * returns the adjusted data length
3026 **/
3027static int
3028lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
3029 struct lpfc_scsi_buf *lpfc_cmd)
3030{
3031 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
3032 int diflen, fcpdl;
3033 unsigned blksize;
3034
3035 fcpdl = scsi_bufflen(sc);
3036
3037 /* Check if there is protection data on the wire */
3038 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
3039 /* Read */
3040 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
3041 return fcpdl;
3042
3043 } else {
3044 /* Write */
3045 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
3046 return fcpdl;
3047 }
3048
3049 /* If protection data on the wire, adjust the count accordingly */
3050 blksize = lpfc_cmd_blksize(sc);
3051 diflen = (fcpdl / blksize) * 8;
3052 fcpdl += diflen;
3053 return fcpdl;
3054}
3055
3056/**
3057 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3058 * @phba: The Hba for which this call is being executed.
3059 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3060 *
3061 * This is the protection/DIF aware version of
3062 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3063 * two functions eventually, but for now, it's here
3064 **/
3065static int
3066lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3067 struct lpfc_scsi_buf *lpfc_cmd)
3068{
3069 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3070 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3071 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3072 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3073 uint32_t num_bde = 0;
3074 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3075 int prot_group_type = 0;
3076 int fcpdl;
3077
3078 /*
3079 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
3080 * fcp_rsp regions to the first data bde entry
3081 */
3082 if (scsi_sg_count(scsi_cmnd)) {
3083 /*
3084 * The driver stores the segment count returned from pci_map_sg
3085 * because this a count of dma-mappings used to map the use_sg
3086 * pages. They are not guaranteed to be the same for those
3087 * architectures that implement an IOMMU.
3088 */
3089 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3090 scsi_sglist(scsi_cmnd),
3091 scsi_sg_count(scsi_cmnd), datadir);
3092 if (unlikely(!datasegcnt))
3093 return 1;
3094
3095 sgl += 1;
3096 /* clear the last flag in the fcp_rsp map entry */
3097 sgl->word2 = le32_to_cpu(sgl->word2);
3098 bf_set(lpfc_sli4_sge_last, sgl, 0);
3099 sgl->word2 = cpu_to_le32(sgl->word2);
3100
3101 sgl += 1;
3102 lpfc_cmd->seg_cnt = datasegcnt;
3103 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
3104 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3105 "9087 BLKGRD: %s: Too many sg segments"
3106 " from dma_map_sg. Config %d, seg_cnt"
3107 " %d\n",
3108 __func__, phba->cfg_sg_seg_cnt,
3109 lpfc_cmd->seg_cnt);
3110 scsi_dma_unmap(scsi_cmnd);
3111 return 1;
3112 }
3113
3114 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3115
3116 switch (prot_group_type) {
3117 case LPFC_PG_TYPE_NO_DIF:
3118 num_bde = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
3119 datasegcnt);
3120 /* we should have 2 or more entries in buffer list */
3121 if (num_bde < 2)
3122 goto err;
3123 break;
3124 case LPFC_PG_TYPE_DIF_BUF:{
3125 /*
3126 * This type indicates that protection buffers are
3127 * passed to the driver, so that needs to be prepared
3128 * for DMA
3129 */
3130 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3131 scsi_prot_sglist(scsi_cmnd),
3132 scsi_prot_sg_count(scsi_cmnd), datadir);
3133 if (unlikely(!protsegcnt)) {
3134 scsi_dma_unmap(scsi_cmnd);
3135 return 1;
3136 }
3137
3138 lpfc_cmd->prot_seg_cnt = protsegcnt;
3139 if (lpfc_cmd->prot_seg_cnt
3140 > phba->cfg_prot_sg_seg_cnt) {
3141 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3142 "9088 BLKGRD: %s: Too many prot sg "
3143 "segments from dma_map_sg. Config %d,"
3144 "prot_seg_cnt %d\n", __func__,
3145 phba->cfg_prot_sg_seg_cnt,
3146 lpfc_cmd->prot_seg_cnt);
3147 dma_unmap_sg(&phba->pcidev->dev,
3148 scsi_prot_sglist(scsi_cmnd),
3149 scsi_prot_sg_count(scsi_cmnd),
3150 datadir);
3151 scsi_dma_unmap(scsi_cmnd);
3152 return 1;
3153 }
3154
3155 num_bde = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
3156 datasegcnt, protsegcnt);
3157 /* we should have 3 or more entries in buffer list */
3158 if (num_bde < 3)
3159 goto err;
3160 break;
3161 }
3162 case LPFC_PG_TYPE_INVALID:
3163 default:
3164 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3165 "9083 Unexpected protection group %i\n",
3166 prot_group_type);
3167 return 1;
3168 }
3169 }
3170
3171 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
3172
3173 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3174
3175 /*
3176 * Due to difference in data length between DIF/non-DIF paths,
3177 * we need to set word 4 of IOCB here
3178 */
3179 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
3180 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF;
3181
3182 return 0;
3183err:
3184 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3185 "9084 Could not setup all needed BDE's"
3186 "prot_group_type=%d, num_bde=%d\n",
3187 prot_group_type, num_bde);
3188 return 1;
3189}
3190
3191/**
James Smart3772a992009-05-22 14:50:54 -04003192 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3193 * @phba: The Hba for which this call is being executed.
3194 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3195 *
3196 * This routine wraps the actual DMA mapping function pointer from the
3197 * lpfc_hba struct.
3198 *
3199 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003200 * 1 - Error
3201 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003202 **/
3203static inline int
3204lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3205{
3206 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3207}
3208
3209/**
James Smartacd68592012-01-18 16:25:09 -05003210 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3211 * using BlockGuard.
3212 * @phba: The Hba for which this call is being executed.
3213 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3214 *
3215 * This routine wraps the actual DMA mapping function pointer from the
3216 * lpfc_hba struct.
3217 *
3218 * Return codes:
3219 * 1 - Error
3220 * 0 - Success
3221 **/
3222static inline int
3223lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3224{
3225 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3226}
3227
3228/**
James Smart3621a712009-04-06 18:47:14 -04003229 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003230 * @phba: Pointer to hba context object.
3231 * @vport: Pointer to vport object.
3232 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3233 * @rsp_iocb: Pointer to response iocb object which reported error.
3234 *
3235 * This function posts an event when there is a SCSI command reporting
3236 * error from the scsi device.
3237 **/
3238static void
3239lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3240 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3241 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3242 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3243 uint32_t resp_info = fcprsp->rspStatus2;
3244 uint32_t scsi_status = fcprsp->rspStatus3;
3245 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3246 struct lpfc_fast_path_event *fast_path_evt = NULL;
3247 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3248 unsigned long flags;
3249
James Smart5989b8d2010-10-22 11:06:56 -04003250 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3251 return;
3252
James Smartea2151b2008-09-07 11:52:10 -04003253 /* If there is queuefull or busy condition send a scsi event */
3254 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3255 (cmnd->result == SAM_STAT_BUSY)) {
3256 fast_path_evt = lpfc_alloc_fast_evt(phba);
3257 if (!fast_path_evt)
3258 return;
3259 fast_path_evt->un.scsi_evt.event_type =
3260 FC_REG_SCSI_EVENT;
3261 fast_path_evt->un.scsi_evt.subcategory =
3262 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3263 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3264 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3265 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3266 &pnode->nlp_portname, sizeof(struct lpfc_name));
3267 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3268 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3269 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3270 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3271 fast_path_evt = lpfc_alloc_fast_evt(phba);
3272 if (!fast_path_evt)
3273 return;
3274 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3275 FC_REG_SCSI_EVENT;
3276 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3277 LPFC_EVENT_CHECK_COND;
3278 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3279 cmnd->device->lun;
3280 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3281 &pnode->nlp_portname, sizeof(struct lpfc_name));
3282 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3283 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3284 fast_path_evt->un.check_cond_evt.sense_key =
3285 cmnd->sense_buffer[2] & 0xf;
3286 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3287 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3288 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3289 fcpi_parm &&
3290 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3291 ((scsi_status == SAM_STAT_GOOD) &&
3292 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3293 /*
3294 * If status is good or resid does not match with fcp_param and
3295 * there is valid fcpi_parm, then there is a read_check error
3296 */
3297 fast_path_evt = lpfc_alloc_fast_evt(phba);
3298 if (!fast_path_evt)
3299 return;
3300 fast_path_evt->un.read_check_error.header.event_type =
3301 FC_REG_FABRIC_EVENT;
3302 fast_path_evt->un.read_check_error.header.subcategory =
3303 LPFC_EVENT_FCPRDCHKERR;
3304 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3305 &pnode->nlp_portname, sizeof(struct lpfc_name));
3306 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3307 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3308 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3309 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3310 fast_path_evt->un.read_check_error.fcpiparam =
3311 fcpi_parm;
3312 } else
3313 return;
3314
3315 fast_path_evt->vport = vport;
3316 spin_lock_irqsave(&phba->hbalock, flags);
3317 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3318 spin_unlock_irqrestore(&phba->hbalock, flags);
3319 lpfc_worker_wake_up(phba);
3320 return;
3321}
James Smart9bad7672008-12-04 22:39:02 -05003322
3323/**
James Smartf1126682009-06-10 17:22:44 -04003324 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003325 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003326 * @psb: The scsi buffer which is going to be un-mapped.
3327 *
3328 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003329 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003330 **/
dea31012005-04-17 16:05:31 -05003331static void
James Smartf1126682009-06-10 17:22:44 -04003332lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003333{
3334 /*
3335 * There are only two special cases to consider. (1) the scsi command
3336 * requested scatter-gather usage or (2) the scsi command allocated
3337 * a request buffer, but did not request use_sg. There is a third
3338 * case, but it does not require resource deallocation.
3339 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003340 if (psb->seg_cnt > 0)
3341 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003342 if (psb->prot_seg_cnt > 0)
3343 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3344 scsi_prot_sg_count(psb->pCmd),
3345 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003346}
3347
James Smart9bad7672008-12-04 22:39:02 -05003348/**
James Smart3621a712009-04-06 18:47:14 -04003349 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003350 * @vport: The virtual port for which this call is being executed.
3351 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3352 * @rsp_iocb: The response IOCB which contains FCP error.
3353 *
3354 * This routine is called to process response IOCB with status field
3355 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3356 * based upon SCSI and FCP error.
3357 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003358static void
James Smart2e0fef82007-06-17 19:56:36 -05003359lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3360 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003361{
3362 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3363 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3364 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003365 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003366 uint32_t resp_info = fcprsp->rspStatus2;
3367 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003368 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003369 uint32_t host_status = DID_OK;
3370 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05003371 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003372
James Smartea2151b2008-09-07 11:52:10 -04003373
dea31012005-04-17 16:05:31 -05003374 /*
3375 * If this is a task management command, there is no
3376 * scsi packet associated with this lpfc_cmd. The driver
3377 * consumes it.
3378 */
3379 if (fcpcmd->fcpCntl2) {
3380 scsi_status = 0;
3381 goto out;
3382 }
3383
James Smart6a9c52c2009-10-02 15:16:51 -04003384 if (resp_info & RSP_LEN_VALID) {
3385 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003386 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04003387 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3388 "2719 Invalid response length: "
3389 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
3390 cmnd->device->id,
3391 cmnd->device->lun, cmnd->cmnd[0],
3392 rsplen);
3393 host_status = DID_ERROR;
3394 goto out;
3395 }
James Smarte40a02c2010-02-26 14:13:54 -05003396 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3397 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3398 "2757 Protocol failure detected during "
3399 "processing of FCP I/O op: "
3400 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
3401 cmnd->device->id,
3402 cmnd->device->lun, cmnd->cmnd[0],
3403 fcprsp->rspInfo3);
3404 host_status = DID_ERROR;
3405 goto out;
3406 }
James Smart6a9c52c2009-10-02 15:16:51 -04003407 }
3408
James Smartc7743952006-12-02 13:34:42 -05003409 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3410 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3411 if (snslen > SCSI_SENSE_BUFFERSIZE)
3412 snslen = SCSI_SENSE_BUFFERSIZE;
3413
3414 if (resp_info & RSP_LEN_VALID)
3415 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3416 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3417 }
3418 lp = (uint32_t *)cmnd->sense_buffer;
3419
James Smart73d91e52011-10-10 21:32:10 -04003420 if (!scsi_status && (resp_info & RESID_UNDER) &&
3421 vport->cfg_log_verbose & LOG_FCP_UNDER)
3422 logit = LOG_FCP_UNDER;
James Smartc7743952006-12-02 13:34:42 -05003423
James Smarte8b62012007-08-02 11:10:09 -04003424 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003425 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003426 "Data: x%x x%x x%x x%x x%x\n",
3427 cmnd->cmnd[0], scsi_status,
3428 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3429 be32_to_cpu(fcprsp->rspResId),
3430 be32_to_cpu(fcprsp->rspSnsLen),
3431 be32_to_cpu(fcprsp->rspRspLen),
3432 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003433
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003434 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05003435 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003436 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003437
James Smart73d91e52011-10-10 21:32:10 -04003438 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smarte2a0a9d2008-12-04 22:40:02 -05003439 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003440 "residual %d Data: x%x x%x x%x\n",
3441 be32_to_cpu(fcpcmd->fcpDl),
3442 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3443 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003444
3445 /*
James Smart7054a602007-04-25 09:52:34 -04003446 * If there is an under run check if under run reported by
3447 * storage array is same as the under run reported by HBA.
3448 * If this is not same, there is a dropped frame.
3449 */
3450 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3451 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003452 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003453 lpfc_printf_vlog(vport, KERN_WARNING,
3454 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003455 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003456 "and Underrun Data: x%x x%x x%x x%x\n",
3457 be32_to_cpu(fcpcmd->fcpDl),
3458 scsi_get_resid(cmnd), fcpi_parm,
3459 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003460 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003461 host_status = DID_ERROR;
3462 }
3463 /*
dea31012005-04-17 16:05:31 -05003464 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003465 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003466 * is not present, make sure the actual amount transferred is at
3467 * least the underflow value or fail.
3468 */
3469 if (!(resp_info & SNS_LEN_VALID) &&
3470 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003471 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3472 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003473 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003474 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003475 "underrun converted to error "
3476 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003477 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003478 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003479 host_status = DID_ERROR;
3480 }
3481 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003482 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003483 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003484 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003485 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003486 host_status = DID_ERROR;
3487
3488 /*
3489 * Check SLI validation that all the transfer was actually done
3490 * (fcpi_parm should be zero). Apply check only to reads.
3491 */
James Smarteee88772010-09-29 11:19:08 -04003492 } else if (fcpi_parm && (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -04003493 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003494 "9029 FCP Read Check Error Data: "
James Smarteee88772010-09-29 11:19:08 -04003495 "x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003496 be32_to_cpu(fcpcmd->fcpDl),
3497 be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04003498 fcpi_parm, cmnd->cmnd[0], scsi_status);
3499 switch (scsi_status) {
3500 case SAM_STAT_GOOD:
3501 case SAM_STAT_CHECK_CONDITION:
3502 /* Fabric dropped a data frame. Fail any successful
3503 * command in which we detected dropped frames.
3504 * A status of good or some check conditions could
3505 * be considered a successful command.
3506 */
3507 host_status = DID_ERROR;
3508 break;
3509 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003510 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05003511 }
3512
3513 out:
3514 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04003515 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05003516}
3517
James Smart9bad7672008-12-04 22:39:02 -05003518/**
James Smart3621a712009-04-06 18:47:14 -04003519 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05003520 * @phba: The Hba for which this call is being executed.
3521 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04003522 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05003523 *
3524 * This routine assigns scsi command result by looking into response IOCB
3525 * status field appropriately. This routine handles QUEUE FULL condition as
3526 * well by ramping down device queue depth.
3527 **/
dea31012005-04-17 16:05:31 -05003528static void
3529lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3530 struct lpfc_iocbq *pIocbOut)
3531{
3532 struct lpfc_scsi_buf *lpfc_cmd =
3533 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003534 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05003535 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3536 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04003537 struct scsi_cmnd *cmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003538 int result;
James Smarta257bf92009-04-06 18:48:10 -04003539 struct scsi_device *tmp_sdev;
James Smart5ffc2662009-11-18 15:39:44 -05003540 int depth;
James Smartfa61a542008-01-11 01:52:42 -05003541 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04003542 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04003543 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04003544 uint32_t queue_depth, scsi_id;
James Smart73d91e52011-10-10 21:32:10 -04003545 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05003546
James Smart75baf692010-06-08 18:31:21 -04003547 /* Sanity check on return of outstanding command */
3548 if (!(lpfc_cmd->pCmd))
3549 return;
3550 cmd = lpfc_cmd->pCmd;
3551 shost = cmd->device->host;
3552
dea31012005-04-17 16:05:31 -05003553 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
3554 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05003555 /* pick up SLI4 exhange busy status from HBA */
3556 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
3557
James Smart9a6b09c2012-03-01 22:37:42 -05003558#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3559 if (lpfc_cmd->prot_data_type) {
3560 struct scsi_dif_tuple *src = NULL;
3561
3562 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
3563 /*
3564 * Used to restore any changes to protection
3565 * data for error injection.
3566 */
3567 switch (lpfc_cmd->prot_data_type) {
3568 case LPFC_INJERR_REFTAG:
3569 src->ref_tag =
3570 lpfc_cmd->prot_data;
3571 break;
3572 case LPFC_INJERR_APPTAG:
3573 src->app_tag =
3574 (uint16_t)lpfc_cmd->prot_data;
3575 break;
3576 case LPFC_INJERR_GUARD:
3577 src->guard_tag =
3578 (uint16_t)lpfc_cmd->prot_data;
3579 break;
3580 default:
3581 break;
3582 }
3583
3584 lpfc_cmd->prot_data = 0;
3585 lpfc_cmd->prot_data_type = 0;
3586 lpfc_cmd->prot_data_segment = NULL;
3587 }
3588#endif
James Smart109f6ed2008-12-04 22:39:08 -05003589 if (pnode && NLP_CHK_NODE_ACT(pnode))
3590 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05003591
3592 if (lpfc_cmd->status) {
3593 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
3594 (lpfc_cmd->result & IOERR_DRVR_MASK))
3595 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3596 else if (lpfc_cmd->status >= IOSTAT_CNT)
3597 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smart73d91e52011-10-10 21:32:10 -04003598 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR
3599 && !lpfc_cmd->fcp_rsp->rspStatus3
3600 && (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER)
3601 && !(phba->cfg_log_verbose & LOG_FCP_UNDER))
3602 logit = 0;
3603 else
3604 logit = LOG_FCP | LOG_FCP_UNDER;
3605 lpfc_printf_vlog(vport, KERN_WARNING, logit,
3606 "9030 FCP cmd x%x failed <%d/%d> "
3607 "status: x%x result: x%x Data: x%x x%x\n",
3608 cmd->cmnd[0],
3609 cmd->device ? cmd->device->id : 0xffff,
3610 cmd->device ? cmd->device->lun : 0xffff,
3611 lpfc_cmd->status, lpfc_cmd->result,
3612 pIocbOut->iocb.ulpContext,
3613 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05003614
3615 switch (lpfc_cmd->status) {
3616 case IOSTAT_FCP_RSP_ERROR:
3617 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05003618 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05003619 break;
3620 case IOSTAT_NPORT_BSY:
3621 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04003622 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04003623 fast_path_evt = lpfc_alloc_fast_evt(phba);
3624 if (!fast_path_evt)
3625 break;
3626 fast_path_evt->un.fabric_evt.event_type =
3627 FC_REG_FABRIC_EVENT;
3628 fast_path_evt->un.fabric_evt.subcategory =
3629 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
3630 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
3631 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3632 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
3633 &pnode->nlp_portname,
3634 sizeof(struct lpfc_name));
3635 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
3636 &pnode->nlp_nodename,
3637 sizeof(struct lpfc_name));
3638 }
3639 fast_path_evt->vport = vport;
3640 fast_path_evt->work_evt.evt =
3641 LPFC_EVT_FASTPATH_MGMT_EVT;
3642 spin_lock_irqsave(&phba->hbalock, flags);
3643 list_add_tail(&fast_path_evt->work_evt.evt_listp,
3644 &phba->work_list);
3645 spin_unlock_irqrestore(&phba->hbalock, flags);
3646 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05003647 break;
James Smart92d7f7b2007-06-17 19:56:38 -05003648 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05003649 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05003650 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
3651 lpfc_cmd->result ==
3652 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
3653 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
3654 lpfc_cmd->result ==
3655 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
3656 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
3657 break;
3658 }
James Smartd7c255b2008-08-24 21:50:00 -04003659 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05003660 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04003661 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
3662 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
James Smart92d7f7b2007-06-17 19:56:38 -05003663 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04003664 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05003665 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003666 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
3667 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
3668 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
3669 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
3670 /*
3671 * This is a response for a BG enabled
3672 * cmd. Parse BG error
3673 */
3674 lpfc_parse_bg_err(phba, lpfc_cmd,
3675 pIocbOut);
3676 break;
3677 } else {
3678 lpfc_printf_vlog(vport, KERN_WARNING,
3679 LOG_BG,
3680 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04003681 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05003682 }
3683 }
James Smart1151e3e2011-02-16 12:39:35 -05003684 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
3685 && (phba->sli_rev == LPFC_SLI_REV4)
3686 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
3687 /* This IO was aborted by the target, we don't
3688 * know the rxid and because we did not send the
3689 * ABTS we cannot generate and RRQ.
3690 */
3691 lpfc_set_rrq_active(phba, pnode,
3692 lpfc_cmd->cur_iocbq.sli4_xritag,
3693 0, 0);
3694 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003695 /* else: fall through */
dea31012005-04-17 16:05:31 -05003696 default:
3697 cmd->result = ScsiResult(DID_ERROR, 0);
3698 break;
3699 }
3700
James Smart58da1ff2008-04-07 10:15:56 -04003701 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003702 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04003703 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
3704 SAM_STAT_BUSY);
James Smartab56dc22011-02-16 12:39:57 -05003705 } else
dea31012005-04-17 16:05:31 -05003706 cmd->result = ScsiResult(DID_OK, 0);
dea31012005-04-17 16:05:31 -05003707
3708 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
3709 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
3710
James Smarte8b62012007-08-02 11:10:09 -04003711 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3712 "0710 Iodone <%d/%d> cmd %p, error "
3713 "x%x SNS x%x x%x Data: x%x x%x\n",
3714 cmd->device->id, cmd->device->lun, cmd,
3715 cmd->result, *lp, *(lp + 3), cmd->retries,
3716 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05003717 }
3718
James Smartea2151b2008-09-07 11:52:10 -04003719 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003720 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04003721 if (vport->cfg_max_scsicmpl_time &&
3722 time_after(jiffies, lpfc_cmd->start_time +
3723 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04003724 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05003725 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3726 if (pnode->cmd_qdepth >
3727 atomic_read(&pnode->cmd_pending) &&
3728 (atomic_read(&pnode->cmd_pending) >
3729 LPFC_MIN_TGT_QDEPTH) &&
3730 ((cmd->cmnd[0] == READ_10) ||
3731 (cmd->cmnd[0] == WRITE_10)))
3732 pnode->cmd_qdepth =
3733 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04003734
James Smart109f6ed2008-12-04 22:39:08 -05003735 pnode->last_change_time = jiffies;
3736 }
James Smarta257bf92009-04-06 18:48:10 -04003737 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05003738 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smart7dc517d2010-07-14 15:32:10 -04003739 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
James Smart977b5a02008-09-07 11:52:04 -04003740 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05003741 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04003742 spin_lock_irqsave(shost->host_lock, flags);
James Smart7dc517d2010-07-14 15:32:10 -04003743 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
3744 / 100;
3745 depth = depth ? depth : 1;
3746 pnode->cmd_qdepth += depth;
3747 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
3748 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05003749 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04003750 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05003751 }
James Smart977b5a02008-09-07 11:52:04 -04003752 }
3753
James Smart1dcb58e2007-04-25 09:51:30 -04003754 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04003755
3756 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
3757 queue_depth = cmd->device->queue_depth;
3758 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05003759 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003760
Jamie Wellnitzb8086082006-02-28 22:33:12 -05003761 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smartfa61a542008-01-11 01:52:42 -05003762 /*
3763 * If there is a thread waiting for command completion
3764 * wake up the thread.
3765 */
James Smarta257bf92009-04-06 18:48:10 -04003766 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04003767 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05003768 if (lpfc_cmd->waitq)
3769 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04003770 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05003771 lpfc_release_scsi_buf(phba, lpfc_cmd);
3772 return;
3773 }
3774
James Smart92d7f7b2007-06-17 19:56:38 -05003775 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04003776 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05003777
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003778 /*
3779 * Check for queue full. If the lun is reporting queue full, then
3780 * back off the lun queue depth to prevent target overloads.
3781 */
James Smart58da1ff2008-04-07 10:15:56 -04003782 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
3783 NLP_CHK_NODE_ACT(pnode)) {
James Smarta257bf92009-04-06 18:48:10 -04003784 shost_for_each_device(tmp_sdev, shost) {
3785 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003786 continue;
3787 depth = scsi_track_queue_full(tmp_sdev,
James Smart5ffc2662009-11-18 15:39:44 -05003788 tmp_sdev->queue_depth-1);
3789 if (depth <= 0)
3790 continue;
James Smarte8b62012007-08-02 11:10:09 -04003791 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3792 "0711 detected queue full - lun queue "
3793 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04003794 lpfc_send_sdev_queuedepth_change_event(phba, vport,
James Smart5ffc2662009-11-18 15:39:44 -05003795 pnode,
3796 tmp_sdev->lun,
3797 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003798 }
3799 }
3800
James Smartfa61a542008-01-11 01:52:42 -05003801 /*
3802 * If there is a thread waiting for command completion
3803 * wake up the thread.
3804 */
James Smarta257bf92009-04-06 18:48:10 -04003805 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04003806 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05003807 if (lpfc_cmd->waitq)
3808 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04003809 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05003810
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003811 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05003812}
3813
James Smart34b02dc2008-08-24 21:49:55 -04003814/**
James Smart3621a712009-04-06 18:47:14 -04003815 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04003816 * @data: A pointer to the immediate command data portion of the IOCB.
3817 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
3818 *
3819 * The routine copies the entire FCP command from @fcp_cmnd to @data while
3820 * byte swapping the data to big endian format for transmission on the wire.
3821 **/
3822static void
3823lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
3824{
3825 int i, j;
3826 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
3827 i += sizeof(uint32_t), j++) {
3828 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
3829 }
3830}
3831
James Smart9bad7672008-12-04 22:39:02 -05003832/**
James Smartf1126682009-06-10 17:22:44 -04003833 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05003834 * @vport: The virtual port for which this call is being executed.
3835 * @lpfc_cmd: The scsi command which needs to send.
3836 * @pnode: Pointer to lpfc_nodelist.
3837 *
3838 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04003839 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003840 **/
dea31012005-04-17 16:05:31 -05003841static void
James Smartf1126682009-06-10 17:22:44 -04003842lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05003843 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05003844{
James Smart2e0fef82007-06-17 19:56:36 -05003845 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003846 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3847 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3848 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3849 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
3850 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04003851 char tag[2];
dea31012005-04-17 16:05:31 -05003852
James Smart58da1ff2008-04-07 10:15:56 -04003853 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3854 return;
3855
dea31012005-04-17 16:05:31 -05003856 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04003857 /* clear task management bits */
3858 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05003859
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04003860 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
3861 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05003862
James Smartdf9e1b52011-12-13 13:22:17 -05003863 memset(&fcp_cmnd->fcpCdb[0], 0, LPFC_FCP_CDB_LEN);
3864 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
James Smart7e2b19f2007-10-29 11:00:39 -04003865 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
3866 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05003867 case HEAD_OF_QUEUE_TAG:
3868 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
3869 break;
3870 case ORDERED_QUEUE_TAG:
3871 fcp_cmnd->fcpCntl1 = ORDERED_Q;
3872 break;
3873 default:
3874 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
3875 break;
3876 }
3877 } else
3878 fcp_cmnd->fcpCntl1 = 0;
3879
3880 /*
3881 * There are three possibilities here - use scatter-gather segment, use
3882 * the single mapping, or neither. Start the lpfc command prep by
3883 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3884 * data bde entry.
3885 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003886 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05003887 if (datadir == DMA_TO_DEVICE) {
3888 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart3772a992009-05-22 14:50:54 -04003889 if (phba->sli_rev < LPFC_SLI_REV4) {
3890 iocb_cmd->un.fcpi.fcpi_parm = 0;
3891 iocb_cmd->ulpPU = 0;
3892 } else
3893 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05003894 fcp_cmnd->fcpCntl3 = WRITE_DATA;
3895 phba->fc4OutputRequests++;
3896 } else {
3897 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
3898 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05003899 fcp_cmnd->fcpCntl3 = READ_DATA;
3900 phba->fc4InputRequests++;
3901 }
3902 } else {
3903 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
3904 iocb_cmd->un.fcpi.fcpi_parm = 0;
3905 iocb_cmd->ulpPU = 0;
3906 fcp_cmnd->fcpCntl3 = 0;
3907 phba->fc4ControlRequests++;
3908 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003909 if (phba->sli_rev == 3 &&
3910 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04003911 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05003912 /*
3913 * Finish initializing those IOCB fields that are independent
3914 * of the scsi_cmnd request_buffer
3915 */
3916 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04003917 if (phba->sli_rev == LPFC_SLI_REV4)
3918 piocbq->iocb.ulpContext =
3919 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05003920 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
3921 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05003922 else
3923 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05003924
3925 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
3926 piocbq->context1 = lpfc_cmd;
3927 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
3928 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05003929 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05003930}
3931
James Smart9bad7672008-12-04 22:39:02 -05003932/**
James Smart6d368e52011-05-24 11:44:12 -04003933 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05003934 * @vport: The virtual port for which this call is being executed.
3935 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3936 * @lun: Logical unit number.
3937 * @task_mgmt_cmd: SCSI task management command.
3938 *
James Smart3772a992009-05-22 14:50:54 -04003939 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
3940 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003941 *
3942 * Return codes:
3943 * 0 - Error
3944 * 1 - Success
3945 **/
dea31012005-04-17 16:05:31 -05003946static int
James Smartf1126682009-06-10 17:22:44 -04003947lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05003948 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04003949 unsigned int lun,
dea31012005-04-17 16:05:31 -05003950 uint8_t task_mgmt_cmd)
3951{
dea31012005-04-17 16:05:31 -05003952 struct lpfc_iocbq *piocbq;
3953 IOCB_t *piocb;
3954 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04003955 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05003956 struct lpfc_nodelist *ndlp = rdata->pnode;
3957
James Smart58da1ff2008-04-07 10:15:56 -04003958 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
3959 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05003960 return 0;
dea31012005-04-17 16:05:31 -05003961
dea31012005-04-17 16:05:31 -05003962 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003963 piocbq->vport = vport;
3964
dea31012005-04-17 16:05:31 -05003965 piocb = &piocbq->iocb;
3966
3967 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04003968 /* Clear out any old data in the FCP command area */
3969 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
3970 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05003971 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05003972 if (vport->phba->sli_rev == 3 &&
3973 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04003974 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05003975 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05003976 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04003977 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
3978 piocb->ulpContext =
3979 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
3980 }
dea31012005-04-17 16:05:31 -05003981 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
3982 piocb->ulpFCP2Rcvy = 1;
3983 }
3984 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
3985
3986 /* ulpTimeout is only one byte */
3987 if (lpfc_cmd->timeout > 0xff) {
3988 /*
3989 * Do not timeout the command at the firmware level.
3990 * The driver will provide the timeout mechanism.
3991 */
3992 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04003993 } else
dea31012005-04-17 16:05:31 -05003994 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04003995
3996 if (vport->phba->sli_rev == LPFC_SLI_REV4)
3997 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05003998
James Smart2e0fef82007-06-17 19:56:36 -05003999 return 1;
dea31012005-04-17 16:05:31 -05004000}
4001
James Smart9bad7672008-12-04 22:39:02 -05004002/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004003 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04004004 * @phba: The hba struct for which this call is being executed.
4005 * @dev_grp: The HBA PCI-Device group number.
4006 *
4007 * This routine sets up the SCSI interface API function jump table in @phba
4008 * struct.
4009 * Returns: 0 - success, -ENODEV - failure.
4010 **/
4011int
4012lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4013{
4014
James Smartf1126682009-06-10 17:22:44 -04004015 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4016 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04004017
James Smart3772a992009-05-22 14:50:54 -04004018 switch (dev_grp) {
4019 case LPFC_PCI_DEV_LP:
4020 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4021 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05004022 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004023 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05004024 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004025 break;
James Smartda0436e2009-05-22 14:51:39 -04004026 case LPFC_PCI_DEV_OC:
4027 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4028 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05004029 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004030 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05004031 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004032 break;
James Smart3772a992009-05-22 14:50:54 -04004033 default:
4034 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4035 "1418 Invalid HBA PCI-device group: 0x%x\n",
4036 dev_grp);
4037 return -ENODEV;
4038 break;
4039 }
James Smart3772a992009-05-22 14:50:54 -04004040 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05004041 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04004042 return 0;
4043}
4044
4045/**
James Smart3621a712009-04-06 18:47:14 -04004046 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05004047 * @phba: The Hba for which this call is being executed.
4048 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4049 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4050 *
4051 * This routine is IOCB completion routine for device reset and target reset
4052 * routine. This routine release scsi buffer associated with lpfc_cmd.
4053 **/
James Smart7054a602007-04-25 09:52:34 -04004054static void
4055lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4056 struct lpfc_iocbq *cmdiocbq,
4057 struct lpfc_iocbq *rspiocbq)
4058{
4059 struct lpfc_scsi_buf *lpfc_cmd =
4060 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4061 if (lpfc_cmd)
4062 lpfc_release_scsi_buf(phba, lpfc_cmd);
4063 return;
4064}
4065
James Smart9bad7672008-12-04 22:39:02 -05004066/**
James Smart3621a712009-04-06 18:47:14 -04004067 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05004068 * @host: The scsi host for which this call is being executed.
4069 *
4070 * This routine provides module information about hba.
4071 *
4072 * Reutrn code:
4073 * Pointer to char - Success.
4074 **/
dea31012005-04-17 16:05:31 -05004075const char *
4076lpfc_info(struct Scsi_Host *host)
4077{
James Smart2e0fef82007-06-17 19:56:36 -05004078 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4079 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004080 int len;
4081 static char lpfcinfobuf[384];
4082
4083 memset(lpfcinfobuf,0,384);
4084 if (phba && phba->pcidev){
4085 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4086 len = strlen(lpfcinfobuf);
4087 snprintf(lpfcinfobuf + len,
4088 384-len,
4089 " on PCI bus %02x device %02x irq %d",
4090 phba->pcidev->bus->number,
4091 phba->pcidev->devfn,
4092 phba->pcidev->irq);
4093 len = strlen(lpfcinfobuf);
4094 if (phba->Port[0]) {
4095 snprintf(lpfcinfobuf + len,
4096 384-len,
4097 " port %s",
4098 phba->Port);
4099 }
James Smart65467b62010-01-26 23:08:29 -05004100 len = strlen(lpfcinfobuf);
4101 if (phba->sli4_hba.link_state.logical_speed) {
4102 snprintf(lpfcinfobuf + len,
4103 384-len,
4104 " Logical Link Speed: %d Mbps",
4105 phba->sli4_hba.link_state.logical_speed * 10);
4106 }
dea31012005-04-17 16:05:31 -05004107 }
4108 return lpfcinfobuf;
4109}
4110
James Smart9bad7672008-12-04 22:39:02 -05004111/**
James Smart3621a712009-04-06 18:47:14 -04004112 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05004113 * @phba: The Hba for which this call is being executed.
4114 *
4115 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4116 * The default value of cfg_poll_tmo is 10 milliseconds.
4117 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004118static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4119{
4120 unsigned long poll_tmo_expires =
4121 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4122
4123 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
4124 mod_timer(&phba->fcp_poll_timer,
4125 poll_tmo_expires);
4126}
4127
James Smart9bad7672008-12-04 22:39:02 -05004128/**
James Smart3621a712009-04-06 18:47:14 -04004129 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004130 * @phba: The Hba for which this call is being executed.
4131 *
4132 * This routine starts the fcp_poll_timer of @phba.
4133 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004134void lpfc_poll_start_timer(struct lpfc_hba * phba)
4135{
4136 lpfc_poll_rearm_timer(phba);
4137}
4138
James Smart9bad7672008-12-04 22:39:02 -05004139/**
James Smart3621a712009-04-06 18:47:14 -04004140 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05004141 * @ptr: Map to lpfc_hba data structure pointer.
4142 *
4143 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4144 * and FCP Ring interrupt is disable.
4145 **/
4146
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004147void lpfc_poll_timeout(unsigned long ptr)
4148{
James Smart2e0fef82007-06-17 19:56:36 -05004149 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004150
4151 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004152 lpfc_sli_handle_fast_ring_event(phba,
4153 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4154
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004155 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4156 lpfc_poll_rearm_timer(phba);
4157 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004158}
4159
James Smart9bad7672008-12-04 22:39:02 -05004160/**
James Smart3621a712009-04-06 18:47:14 -04004161 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05004162 * @cmnd: Pointer to scsi_cmnd data structure.
4163 * @done: Pointer to done routine.
4164 *
4165 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4166 * This routine prepares an IOCB from scsi command and provides to firmware.
4167 * The @done callback is invoked after driver finished processing the command.
4168 *
4169 * Return value :
4170 * 0 - Success
4171 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4172 **/
dea31012005-04-17 16:05:31 -05004173static int
Jeff Garzikf2812332010-11-16 02:10:29 -05004174lpfc_queuecommand_lck(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
dea31012005-04-17 16:05:31 -05004175{
James Smart2e0fef82007-06-17 19:56:36 -05004176 struct Scsi_Host *shost = cmnd->device->host;
4177 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4178 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004179 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004180 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004181 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004182 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004183 int err;
dea31012005-04-17 16:05:31 -05004184
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004185 err = fc_remote_port_chkready(rport);
4186 if (err) {
4187 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004188 goto out_fail_command;
4189 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004190 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004191
James Smartbf086112011-08-21 21:48:13 -04004192 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004193 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004194
James Smart6a9c52c2009-10-02 15:16:51 -04004195 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4196 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4197 " op:%02x str=%s without registering for"
4198 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004199 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4200 dif_op_str[scsi_get_prot_op(cmnd)]);
4201 goto out_fail_command;
4202 }
4203
dea31012005-04-17 16:05:31 -05004204 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004205 * Catch race where our node has transitioned, but the
4206 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004207 */
James Smartb522d7d2008-09-07 11:51:56 -04004208 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
Mike Christief55ca842011-02-25 14:04:27 -06004209 cmnd->result = ScsiResult(DID_IMM_RETRY, 0);
James Smartb522d7d2008-09-07 11:51:56 -04004210 goto out_fail_command;
4211 }
James Smart7dc517d2010-07-14 15:32:10 -04004212 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
Mike Christie34963432011-02-25 14:04:28 -06004213 goto out_tgt_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05004214
James Smart19ca7602010-11-20 23:11:55 -05004215 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05004216 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004217 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004218
James Smarte8b62012007-08-02 11:10:09 -04004219 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4220 "0707 driver's buffer pool is empty, "
4221 "IO busied\n");
dea31012005-04-17 16:05:31 -05004222 goto out_host_busy;
4223 }
4224
4225 /*
4226 * Store the midlayer's command structure for the completion phase
4227 * and complete the command initialization.
4228 */
4229 lpfc_cmd->pCmd = cmnd;
4230 lpfc_cmd->rdata = rdata;
4231 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04004232 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05004233 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
4234 cmnd->scsi_done = done;
4235
James Smarte2a0a9d2008-12-04 22:40:02 -05004236 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004237 if (vport->phba->cfg_enable_bg) {
4238 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smart9a6b09c2012-03-01 22:37:42 -05004239 "9033 BLKGRD: rcvd protected cmd:%02x op=%s "
4240 "guard=%s\n", cmnd->cmnd[0],
4241 dif_op_str[scsi_get_prot_op(cmnd)],
4242 dif_grd_str[scsi_host_get_guard(shost)]);
James Smart6a9c52c2009-10-02 15:16:51 -04004243 if (cmnd->cmnd[0] == READ_10)
4244 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05004245 "9035 BLKGRD: READ @ sector %llu, "
James Smart9a6b09c2012-03-01 22:37:42 -05004246 "cnt %u, rpt %d\n",
Tejun Heo83096eb2009-05-07 22:24:39 +09004247 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004248 blk_rq_sectors(cmnd->request),
4249 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004250 else if (cmnd->cmnd[0] == WRITE_10)
4251 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05004252 "9036 BLKGRD: WRITE @ sector %llu, "
James Smart9a6b09c2012-03-01 22:37:42 -05004253 "cnt %u, wpt %d\n",
James Smart87b5c322008-12-16 10:34:09 -05004254 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09004255 blk_rq_sectors(cmnd->request),
James Smart9a6b09c2012-03-01 22:37:42 -05004256 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004257 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004258
4259 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4260 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004261 if (vport->phba->cfg_enable_bg) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004262 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smart9a6b09c2012-03-01 22:37:42 -05004263 "9038 BLKGRD: rcvd unprotected cmd:"
4264 "%02x op=%s guard=%s\n", cmnd->cmnd[0],
4265 dif_op_str[scsi_get_prot_op(cmnd)],
4266 dif_grd_str[scsi_host_get_guard(shost)]);
James Smart6a9c52c2009-10-02 15:16:51 -04004267 if (cmnd->cmnd[0] == READ_10)
4268 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
4269 "9040 dbg: READ @ sector %llu, "
James Smart9a6b09c2012-03-01 22:37:42 -05004270 "cnt %u, rpt %d\n",
James Smart6a9c52c2009-10-02 15:16:51 -04004271 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004272 blk_rq_sectors(cmnd->request),
4273 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004274 else if (cmnd->cmnd[0] == WRITE_10)
4275 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smart9a6b09c2012-03-01 22:37:42 -05004276 "9041 dbg: WRITE @ sector %llu, "
4277 "cnt %u, wpt %d\n",
4278 (unsigned long long)scsi_get_lba(cmnd),
4279 blk_rq_sectors(cmnd->request),
4280 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004281 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004282 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4283 }
4284
dea31012005-04-17 16:05:31 -05004285 if (err)
4286 goto out_host_busy_free_buf;
4287
James Smart2e0fef82007-06-17 19:56:36 -05004288 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004289
James Smart977b5a02008-09-07 11:52:04 -04004290 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04004291 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004292 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05004293 if (err) {
4294 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05004295 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004296 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004297 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004298 spin_unlock(shost->host_lock);
4299 lpfc_sli_handle_fast_ring_event(phba,
4300 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4301
4302 spin_lock(shost->host_lock);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004303 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4304 lpfc_poll_rearm_timer(phba);
4305 }
4306
dea31012005-04-17 16:05:31 -05004307 return 0;
4308
4309 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04004310 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004311 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004312 out_host_busy:
4313 return SCSI_MLQUEUE_HOST_BUSY;
4314
Mike Christie34963432011-02-25 14:04:28 -06004315 out_tgt_busy:
4316 return SCSI_MLQUEUE_TARGET_BUSY;
4317
dea31012005-04-17 16:05:31 -05004318 out_fail_command:
4319 done(cmnd);
4320 return 0;
4321}
4322
Jeff Garzikf2812332010-11-16 02:10:29 -05004323static DEF_SCSI_QCMD(lpfc_queuecommand)
4324
James Smart9bad7672008-12-04 22:39:02 -05004325/**
James Smart3621a712009-04-06 18:47:14 -04004326 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004327 * @cmnd: Pointer to scsi_cmnd data structure.
4328 *
4329 * This routine aborts @cmnd pending in base driver.
4330 *
4331 * Return code :
4332 * 0x2003 - Error
4333 * 0x2002 - Success
4334 **/
dea31012005-04-17 16:05:31 -05004335static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004336lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004337{
James Smart2e0fef82007-06-17 19:56:36 -05004338 struct Scsi_Host *shost = cmnd->device->host;
4339 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4340 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004341 struct lpfc_iocbq *iocb;
4342 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004343 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004344 IOCB_t *cmd, *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004345 int ret = SUCCESS;
James Smartfa61a542008-01-11 01:52:42 -05004346 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004347
James Smart589a52d2010-07-14 15:30:54 -04004348 ret = fc_block_scsi_eh(cmnd);
4349 if (ret)
4350 return ret;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004351 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smarteee88772010-09-29 11:19:08 -04004352 if (!lpfc_cmd) {
4353 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4354 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004355 "x%x ID %d LUN %d\n",
4356 ret, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04004357 return SUCCESS;
4358 }
dea31012005-04-17 16:05:31 -05004359
4360 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004361 * If pCmd field of the corresponding lpfc_scsi_buf structure
4362 * points to a different SCSI command, then the driver has
4363 * already completed this command, but the midlayer did not
4364 * see the completion before the eh fired. Just return
4365 * SUCCESS.
dea31012005-04-17 16:05:31 -05004366 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004367 iocb = &lpfc_cmd->cur_iocbq;
4368 if (lpfc_cmd->pCmd != cmnd)
4369 goto out;
dea31012005-04-17 16:05:31 -05004370
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004371 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004372
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004373 abtsiocb = lpfc_sli_get_iocbq(phba);
4374 if (abtsiocb == NULL) {
4375 ret = FAILED;
dea31012005-04-17 16:05:31 -05004376 goto out;
4377 }
4378
dea31012005-04-17 16:05:31 -05004379 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004380 * The scsi command can not be in txq and it is in flight because the
4381 * pCmd is still pointig at the SCSI command we have to abort. There
4382 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004383 */
dea31012005-04-17 16:05:31 -05004384
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004385 cmd = &iocb->iocb;
4386 icmd = &abtsiocb->iocb;
4387 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4388 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004389 if (phba->sli_rev == LPFC_SLI_REV4)
4390 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4391 else
4392 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004393
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004394 icmd->ulpLe = 1;
4395 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004396
4397 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4398 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05004399 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05004400
James Smart2e0fef82007-06-17 19:56:36 -05004401 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004402 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4403 else
4404 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004405
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004406 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004407 abtsiocb->vport = vport;
James Smart3772a992009-05-22 14:50:54 -04004408 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
4409 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004410 lpfc_sli_release_iocbq(phba, abtsiocb);
4411 ret = FAILED;
4412 goto out;
4413 }
4414
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004415 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004416 lpfc_sli_handle_fast_ring_event(phba,
4417 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004418
James Smartfa61a542008-01-11 01:52:42 -05004419 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004420 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004421 wait_event_timeout(waitq,
4422 (lpfc_cmd->pCmd != cmnd),
4423 (2*vport->cfg_devloss_tmo*HZ));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004424
James Smartfa61a542008-01-11 01:52:42 -05004425 spin_lock_irq(shost->host_lock);
4426 lpfc_cmd->waitq = NULL;
4427 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004428
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004429 if (lpfc_cmd->pCmd == cmnd) {
4430 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04004431 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4432 "0748 abort handler timed out waiting "
4433 "for abort to complete: ret %#x, ID %d, "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004434 "LUN %d\n",
4435 ret, cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004436 }
4437
4438 out:
James Smarte8b62012007-08-02 11:10:09 -04004439 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4440 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004441 "LUN %d\n", ret, cmnd->device->id,
4442 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004443 return ret;
dea31012005-04-17 16:05:31 -05004444}
4445
James Smartbbb9d182009-06-10 17:23:16 -04004446static char *
4447lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4448{
4449 switch (task_mgmt_cmd) {
4450 case FCP_ABORT_TASK_SET:
4451 return "ABORT_TASK_SET";
4452 case FCP_CLEAR_TASK_SET:
4453 return "FCP_CLEAR_TASK_SET";
4454 case FCP_BUS_RESET:
4455 return "FCP_BUS_RESET";
4456 case FCP_LUN_RESET:
4457 return "FCP_LUN_RESET";
4458 case FCP_TARGET_RESET:
4459 return "FCP_TARGET_RESET";
4460 case FCP_CLEAR_ACA:
4461 return "FCP_CLEAR_ACA";
4462 case FCP_TERMINATE_TASK:
4463 return "FCP_TERMINATE_TASK";
4464 default:
4465 return "unknown";
4466 }
4467}
4468
4469/**
4470 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
4471 * @vport: The virtual port for which this call is being executed.
4472 * @rdata: Pointer to remote port local data
4473 * @tgt_id: Target ID of remote device.
4474 * @lun_id: Lun number for the TMF
4475 * @task_mgmt_cmd: type of TMF to send
4476 *
4477 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
4478 * a remote port.
4479 *
4480 * Return Code:
4481 * 0x2003 - Error
4482 * 0x2002 - Success.
4483 **/
4484static int
4485lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
4486 unsigned tgt_id, unsigned int lun_id,
4487 uint8_t task_mgmt_cmd)
4488{
4489 struct lpfc_hba *phba = vport->phba;
4490 struct lpfc_scsi_buf *lpfc_cmd;
4491 struct lpfc_iocbq *iocbq;
4492 struct lpfc_iocbq *iocbqrsp;
James Smart5989b8d2010-10-22 11:06:56 -04004493 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004494 int ret;
4495 int status;
4496
James Smart5989b8d2010-10-22 11:06:56 -04004497 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smartbbb9d182009-06-10 17:23:16 -04004498 return FAILED;
4499
James Smart19ca7602010-11-20 23:11:55 -05004500 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
James Smartbbb9d182009-06-10 17:23:16 -04004501 if (lpfc_cmd == NULL)
4502 return FAILED;
4503 lpfc_cmd->timeout = 60;
4504 lpfc_cmd->rdata = rdata;
4505
4506 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
4507 task_mgmt_cmd);
4508 if (!status) {
4509 lpfc_release_scsi_buf(phba, lpfc_cmd);
4510 return FAILED;
4511 }
4512
4513 iocbq = &lpfc_cmd->cur_iocbq;
4514 iocbqrsp = lpfc_sli_get_iocbq(phba);
4515 if (iocbqrsp == NULL) {
4516 lpfc_release_scsi_buf(phba, lpfc_cmd);
4517 return FAILED;
4518 }
4519
4520 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4521 "0702 Issue %s to TGT %d LUN %d "
James Smart6d368e52011-05-24 11:44:12 -04004522 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04004523 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04004524 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
4525 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04004526
4527 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
4528 iocbq, iocbqrsp, lpfc_cmd->timeout);
4529 if (status != IOCB_SUCCESS) {
4530 if (status == IOCB_TIMEDOUT) {
4531 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
4532 ret = TIMEOUT_ERROR;
4533 } else
4534 ret = FAILED;
4535 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4536 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart6d368e52011-05-24 11:44:12 -04004537 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
4538 "iocb_flag x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04004539 lpfc_taskmgmt_name(task_mgmt_cmd),
4540 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
James Smart6d368e52011-05-24 11:44:12 -04004541 iocbqrsp->iocb.un.ulpWord[4],
4542 iocbq->iocb_flag);
James Smart2a9bf3d2010-06-07 15:24:45 -04004543 } else if (status == IOCB_BUSY)
4544 ret = FAILED;
4545 else
James Smartbbb9d182009-06-10 17:23:16 -04004546 ret = SUCCESS;
4547
4548 lpfc_sli_release_iocbq(phba, iocbqrsp);
4549
4550 if (ret != TIMEOUT_ERROR)
4551 lpfc_release_scsi_buf(phba, lpfc_cmd);
4552
4553 return ret;
4554}
4555
4556/**
4557 * lpfc_chk_tgt_mapped -
4558 * @vport: The virtual port to check on
4559 * @cmnd: Pointer to scsi_cmnd data structure.
4560 *
4561 * This routine delays until the scsi target (aka rport) for the
4562 * command exists (is present and logged in) or we declare it non-existent.
4563 *
4564 * Return code :
4565 * 0x2003 - Error
4566 * 0x2002 - Success
4567 **/
4568static int
4569lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
4570{
4571 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004572 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004573 unsigned long later;
4574
James Smart1c6f4ef52009-11-18 15:40:49 -05004575 if (!rdata) {
4576 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4577 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
4578 return FAILED;
4579 }
4580 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004581 /*
4582 * If target is not in a MAPPED state, delay until
4583 * target is rediscovered or devloss timeout expires.
4584 */
4585 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
4586 while (time_after(later, jiffies)) {
4587 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4588 return FAILED;
4589 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
4590 return SUCCESS;
4591 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
4592 rdata = cmnd->device->hostdata;
4593 if (!rdata)
4594 return FAILED;
4595 pnode = rdata->pnode;
4596 }
4597 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
4598 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
4599 return FAILED;
4600 return SUCCESS;
4601}
4602
4603/**
4604 * lpfc_reset_flush_io_context -
4605 * @vport: The virtual port (scsi_host) for the flush context
4606 * @tgt_id: If aborting by Target contect - specifies the target id
4607 * @lun_id: If aborting by Lun context - specifies the lun id
4608 * @context: specifies the context level to flush at.
4609 *
4610 * After a reset condition via TMF, we need to flush orphaned i/o
4611 * contexts from the adapter. This routine aborts any contexts
4612 * outstanding, then waits for their completions. The wait is
4613 * bounded by devloss_tmo though.
4614 *
4615 * Return code :
4616 * 0x2003 - Error
4617 * 0x2002 - Success
4618 **/
4619static int
4620lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
4621 uint64_t lun_id, lpfc_ctx_cmd context)
4622{
4623 struct lpfc_hba *phba = vport->phba;
4624 unsigned long later;
4625 int cnt;
4626
4627 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
4628 if (cnt)
4629 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
4630 tgt_id, lun_id, context);
4631 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
4632 while (time_after(later, jiffies) && cnt) {
4633 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
4634 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
4635 }
4636 if (cnt) {
4637 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4638 "0724 I/O flush failure for context %s : cnt x%x\n",
4639 ((context == LPFC_CTX_LUN) ? "LUN" :
4640 ((context == LPFC_CTX_TGT) ? "TGT" :
4641 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
4642 cnt);
4643 return FAILED;
4644 }
4645 return SUCCESS;
4646}
4647
James Smart9bad7672008-12-04 22:39:02 -05004648/**
James Smart3621a712009-04-06 18:47:14 -04004649 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05004650 * @cmnd: Pointer to scsi_cmnd data structure.
4651 *
James Smartbbb9d182009-06-10 17:23:16 -04004652 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05004653 * command.
4654 *
4655 * Return code :
4656 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04004657 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05004658 **/
dea31012005-04-17 16:05:31 -05004659static int
James Smart7054a602007-04-25 09:52:34 -04004660lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004661{
James Smart2e0fef82007-06-17 19:56:36 -05004662 struct Scsi_Host *shost = cmnd->device->host;
4663 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05004664 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004665 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004666 unsigned tgt_id = cmnd->device->id;
4667 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04004668 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04004669 int status;
dea31012005-04-17 16:05:31 -05004670
James Smart1c6f4ef52009-11-18 15:40:49 -05004671 if (!rdata) {
4672 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4673 "0798 Device Reset rport failure: rdata x%p\n", rdata);
4674 return FAILED;
4675 }
4676 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04004677 status = fc_block_scsi_eh(cmnd);
4678 if (status)
4679 return status;
James Smartbbb9d182009-06-10 17:23:16 -04004680
4681 status = lpfc_chk_tgt_mapped(vport, cmnd);
4682 if (status == FAILED) {
4683 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4684 "0721 Device Reset rport failure: rdata x%p\n", rdata);
4685 return FAILED;
4686 }
4687
4688 scsi_event.event_type = FC_REG_SCSI_EVENT;
4689 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
4690 scsi_event.lun = lun_id;
4691 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
4692 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
4693
4694 fc_host_post_vendor_event(shost, fc_get_event_number(),
4695 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
4696
4697 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
4698 FCP_LUN_RESET);
4699
4700 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4701 "0713 SCSI layer issued Device Reset (%d, %d) "
4702 "return x%x\n", tgt_id, lun_id, status);
4703
dea31012005-04-17 16:05:31 -05004704 /*
James Smartbbb9d182009-06-10 17:23:16 -04004705 * We have to clean up i/o as : they may be orphaned by the TMF;
4706 * or if the TMF failed, they may be in an indeterminate state.
4707 * So, continue on.
4708 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05004709 */
James Smartbbb9d182009-06-10 17:23:16 -04004710 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
4711 LPFC_CTX_LUN);
4712 return status;
4713}
4714
4715/**
4716 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
4717 * @cmnd: Pointer to scsi_cmnd data structure.
4718 *
4719 * This routine does a target reset by sending a TARGET_RESET task management
4720 * command.
4721 *
4722 * Return code :
4723 * 0x2003 - Error
4724 * 0x2002 - Success
4725 **/
4726static int
4727lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
4728{
4729 struct Scsi_Host *shost = cmnd->device->host;
4730 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4731 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004732 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004733 unsigned tgt_id = cmnd->device->id;
4734 unsigned int lun_id = cmnd->device->lun;
4735 struct lpfc_scsi_event_header scsi_event;
4736 int status;
4737
James Smart1c6f4ef52009-11-18 15:40:49 -05004738 if (!rdata) {
4739 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4740 "0799 Target Reset rport failure: rdata x%p\n", rdata);
4741 return FAILED;
4742 }
4743 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04004744 status = fc_block_scsi_eh(cmnd);
4745 if (status)
4746 return status;
James Smartbbb9d182009-06-10 17:23:16 -04004747
4748 status = lpfc_chk_tgt_mapped(vport, cmnd);
4749 if (status == FAILED) {
4750 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4751 "0722 Target Reset rport failure: rdata x%p\n", rdata);
4752 return FAILED;
dea31012005-04-17 16:05:31 -05004753 }
James Smartea2151b2008-09-07 11:52:10 -04004754
4755 scsi_event.event_type = FC_REG_SCSI_EVENT;
4756 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
4757 scsi_event.lun = 0;
4758 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
4759 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
4760
James Smartbbb9d182009-06-10 17:23:16 -04004761 fc_host_post_vendor_event(shost, fc_get_event_number(),
4762 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04004763
James Smartbbb9d182009-06-10 17:23:16 -04004764 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
4765 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05004766
James Smarte8b62012007-08-02 11:10:09 -04004767 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04004768 "0723 SCSI layer issued Target Reset (%d, %d) "
4769 "return x%x\n", tgt_id, lun_id, status);
4770
4771 /*
4772 * We have to clean up i/o as : they may be orphaned by the TMF;
4773 * or if the TMF failed, they may be in an indeterminate state.
4774 * So, continue on.
4775 * We will report success if all the i/o aborts successfully.
4776 */
4777 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
4778 LPFC_CTX_TGT);
4779 return status;
dea31012005-04-17 16:05:31 -05004780}
4781
James Smart9bad7672008-12-04 22:39:02 -05004782/**
James Smart3621a712009-04-06 18:47:14 -04004783 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004784 * @cmnd: Pointer to scsi_cmnd data structure.
4785 *
James Smartbbb9d182009-06-10 17:23:16 -04004786 * This routine does target reset to all targets on @cmnd->device->host.
4787 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05004788 *
James Smartbbb9d182009-06-10 17:23:16 -04004789 * Return code :
4790 * 0x2003 - Error
4791 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05004792 **/
Jeff Garzik 94d0e7b2005-05-28 07:55:48 -04004793static int
James Smart7054a602007-04-25 09:52:34 -04004794lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004795{
James Smart2e0fef82007-06-17 19:56:36 -05004796 struct Scsi_Host *shost = cmnd->device->host;
4797 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05004798 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04004799 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04004800 int match;
4801 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04004802
4803 scsi_event.event_type = FC_REG_SCSI_EVENT;
4804 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
4805 scsi_event.lun = 0;
4806 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
4807 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
4808
James Smartbbb9d182009-06-10 17:23:16 -04004809 fc_host_post_vendor_event(shost, fc_get_event_number(),
4810 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05004811
James Smartbf086112011-08-21 21:48:13 -04004812 status = fc_block_scsi_eh(cmnd);
4813 if (status)
4814 return status;
James Smartbbb9d182009-06-10 17:23:16 -04004815
dea31012005-04-17 16:05:31 -05004816 /*
4817 * Since the driver manages a single bus device, reset all
4818 * targets known to the driver. Should any target reset
4819 * fail, this routine returns failure to the midlayer.
4820 */
James Smarte17da182006-07-06 15:49:25 -04004821 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04004822 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05004823 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004824 spin_lock_irq(shost->host_lock);
4825 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004826 if (!NLP_CHK_NODE_ACT(ndlp))
4827 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004828 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04004829 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04004830 ndlp->rport) {
dea31012005-04-17 16:05:31 -05004831 match = 1;
4832 break;
4833 }
4834 }
James Smart2e0fef82007-06-17 19:56:36 -05004835 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004836 if (!match)
4837 continue;
James Smartbbb9d182009-06-10 17:23:16 -04004838
4839 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
4840 i, 0, FCP_TARGET_RESET);
4841
4842 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04004843 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4844 "0700 Bus Reset on target %d failed\n",
4845 i);
James Smart915caaa2008-06-14 22:52:38 -04004846 ret = FAILED;
dea31012005-04-17 16:05:31 -05004847 }
4848 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05004849 /*
James Smartbbb9d182009-06-10 17:23:16 -04004850 * We have to clean up i/o as : they may be orphaned by the TMFs
4851 * above; or if any of the TMFs failed, they may be in an
4852 * indeterminate state.
4853 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05004854 */
James Smartbbb9d182009-06-10 17:23:16 -04004855
4856 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
4857 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05004858 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04004859
James Smarte8b62012007-08-02 11:10:09 -04004860 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4861 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05004862 return ret;
4863}
4864
James Smart9bad7672008-12-04 22:39:02 -05004865/**
James Smart3621a712009-04-06 18:47:14 -04004866 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05004867 * @sdev: Pointer to scsi_device.
4868 *
4869 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
4870 * globally available list of scsi buffers. This routine also makes sure scsi
4871 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
4872 * of scsi buffer exists for the lifetime of the driver.
4873 *
4874 * Return codes:
4875 * non-0 - Error
4876 * 0 - Success
4877 **/
dea31012005-04-17 16:05:31 -05004878static int
dea31012005-04-17 16:05:31 -05004879lpfc_slave_alloc(struct scsi_device *sdev)
4880{
James Smart2e0fef82007-06-17 19:56:36 -05004881 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
4882 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004883 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04004884 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05004885 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04004886 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04004887 uint32_t sdev_cnt;
dea31012005-04-17 16:05:31 -05004888
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004889 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05004890 return -ENXIO;
4891
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004892 sdev->hostdata = rport->dd_data;
James Smartd7c47992010-06-08 18:31:54 -04004893 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05004894
4895 /*
4896 * Populate the cmds_per_lun count scsi_bufs into this host's globally
4897 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04004898 * HBA limit conveyed to the midlayer via the host structure. The
4899 * formula accounts for the lun_queue_depth + error handlers + 1
4900 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05004901 */
4902 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04004903 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05004904
James Smartd7c47992010-06-08 18:31:54 -04004905 /* If allocated buffers are enough do nothing */
4906 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
4907 return 0;
4908
James Smart92d7f7b2007-06-17 19:56:38 -05004909 /* Allow some exchanges to be available always to complete discovery */
4910 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04004911 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4912 "0704 At limitation of %d preallocated "
4913 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05004914 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05004915 /* Allow some exchanges to be available always to complete discovery */
4916 } else if (total + num_to_alloc >
4917 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04004918 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4919 "0705 Allocation request of %d "
4920 "command buffers will exceed max of %d. "
4921 "Reducing allocation request to %d.\n",
4922 num_to_alloc, phba->cfg_hba_queue_depth,
4923 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05004924 num_to_alloc = phba->cfg_hba_queue_depth - total;
4925 }
James Smart3772a992009-05-22 14:50:54 -04004926 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
4927 if (num_to_alloc != num_allocated) {
4928 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4929 "0708 Allocation request of %d "
4930 "command buffers did not succeed. "
4931 "Allocated %d buffers.\n",
4932 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05004933 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004934 if (num_allocated > 0)
4935 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05004936 return 0;
4937}
4938
James Smart9bad7672008-12-04 22:39:02 -05004939/**
James Smart3621a712009-04-06 18:47:14 -04004940 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05004941 * @sdev: Pointer to scsi_device.
4942 *
4943 * This routine configures following items
4944 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05004945 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
4946 *
4947 * Return codes:
4948 * 0 - Success
4949 **/
dea31012005-04-17 16:05:31 -05004950static int
4951lpfc_slave_configure(struct scsi_device *sdev)
4952{
James Smart2e0fef82007-06-17 19:56:36 -05004953 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
4954 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004955
4956 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04004957 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05004958 else
James Smart3de2a652007-08-02 11:09:59 -04004959 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05004960
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004961 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004962 lpfc_sli_handle_fast_ring_event(phba,
4963 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004964 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4965 lpfc_poll_rearm_timer(phba);
4966 }
4967
dea31012005-04-17 16:05:31 -05004968 return 0;
4969}
4970
James Smart9bad7672008-12-04 22:39:02 -05004971/**
James Smart3621a712009-04-06 18:47:14 -04004972 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05004973 * @sdev: Pointer to scsi_device.
4974 *
4975 * This routine sets @sdev hostatdata filed to null.
4976 **/
dea31012005-04-17 16:05:31 -05004977static void
4978lpfc_slave_destroy(struct scsi_device *sdev)
4979{
James Smartd7c47992010-06-08 18:31:54 -04004980 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
4981 struct lpfc_hba *phba = vport->phba;
4982 atomic_dec(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05004983 sdev->hostdata = NULL;
4984 return;
4985}
4986
James Smart92d7f7b2007-06-17 19:56:38 -05004987
dea31012005-04-17 16:05:31 -05004988struct scsi_host_template lpfc_template = {
4989 .module = THIS_MODULE,
4990 .name = LPFC_DRIVER_NAME,
4991 .info = lpfc_info,
4992 .queuecommand = lpfc_queuecommand,
4993 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04004994 .eh_device_reset_handler = lpfc_device_reset_handler,
4995 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04004996 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05004997 .slave_alloc = lpfc_slave_alloc,
4998 .slave_configure = lpfc_slave_configure,
4999 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04005000 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05005001 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005002 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05005003 .cmd_per_lun = LPFC_CMD_PER_LUN,
5004 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05005005 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04005006 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04005007 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05005008 .change_queue_depth = lpfc_change_queue_depth,
dea31012005-04-17 16:05:31 -05005009};
James Smart3de2a652007-08-02 11:09:59 -04005010
5011struct scsi_host_template lpfc_vport_template = {
5012 .module = THIS_MODULE,
5013 .name = LPFC_DRIVER_NAME,
5014 .info = lpfc_info,
5015 .queuecommand = lpfc_queuecommand,
5016 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005017 .eh_device_reset_handler = lpfc_device_reset_handler,
5018 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04005019 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5020 .slave_alloc = lpfc_slave_alloc,
5021 .slave_configure = lpfc_slave_configure,
5022 .slave_destroy = lpfc_slave_destroy,
5023 .scan_finished = lpfc_scan_finished,
5024 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005025 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04005026 .cmd_per_lun = LPFC_CMD_PER_LUN,
5027 .use_clustering = ENABLE_CLUSTERING,
5028 .shost_attrs = lpfc_vport_attrs,
5029 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05005030 .change_queue_depth = lpfc_change_queue_depth,
James Smart3de2a652007-08-02 11:09:59 -04005031};