blob: 2e1e54e5c3ae89514ef1de659fe8963237ee41c9 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * 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 Smart792581d2011-03-11 16:06:44 -05004 * Copyright (C) 2004-2011 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"
42#include "lpfc_scsi.h"
43#include "lpfc.h"
44#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[] = {
54 "SCSI_PROT_NORMAL",
55 "SCSI_PROT_READ_INSERT",
56 "SCSI_PROT_WRITE_STRIP",
57 "SCSI_PROT_READ_STRIP",
58 "SCSI_PROT_WRITE_INSERT",
59 "SCSI_PROT_READ_PASS",
60 "SCSI_PROT_WRITE_PASS",
James Smarte2a0a9d2008-12-04 22:40:02 -050061};
James Smartf9bb2da2011-10-10 21:34:11 -040062
63struct scsi_dif_tuple {
64 __be16 guard_tag; /* Checksum */
65 __be16 app_tag; /* Opaque storage */
66 __be32 ref_tag; /* Target LBA or indirect LBA */
67};
68
James Smartda0436e2009-05-22 14:51:39 -040069static void
70lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smart1c6f4ef52009-11-18 15:40:49 -050071static void
72lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smarte2a0a9d2008-12-04 22:40:02 -050073
74static void
James Smart6a9c52c2009-10-02 15:16:51 -040075lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -050076{
77 void *src, *dst;
78 struct scatterlist *sgde = scsi_sglist(cmnd);
79
80 if (!_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040081 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
82 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -050083 __func__);
84 return;
85 }
86
87
88 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -040089 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
90 "9051 BLKGRD: ERROR: data scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -050091 return;
92 }
93
94 dst = (void *) _dump_buf_data;
95 while (sgde) {
96 src = sg_virt(sgde);
97 memcpy(dst, src, sgde->length);
98 dst += sgde->length;
99 sgde = sg_next(sgde);
100 }
101}
102
103static void
James Smart6a9c52c2009-10-02 15:16:51 -0400104lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -0500105{
106 void *src, *dst;
107 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
108
109 if (!_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -0400110 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
111 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -0500112 __func__);
113 return;
114 }
115
116 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -0400117 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
118 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -0500119 return;
120 }
121
122 dst = _dump_buf_dif;
123 while (sgde) {
124 src = sg_virt(sgde);
125 memcpy(dst, src, sgde->length);
126 dst += sgde->length;
127 sgde = sg_next(sgde);
128 }
129}
130
James Smartea2151b2008-09-07 11:52:10 -0400131/**
James Smartf1126682009-06-10 17:22:44 -0400132 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
133 * @phba: Pointer to HBA object.
134 * @lpfc_cmd: lpfc scsi command object pointer.
135 *
136 * This function is called from the lpfc_prep_task_mgmt_cmd function to
137 * set the last bit in the response sge entry.
138 **/
139static void
140lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
141 struct lpfc_scsi_buf *lpfc_cmd)
142{
143 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
144 if (sgl) {
145 sgl += 1;
146 sgl->word2 = le32_to_cpu(sgl->word2);
147 bf_set(lpfc_sli4_sge_last, sgl, 1);
148 sgl->word2 = cpu_to_le32(sgl->word2);
149 }
150}
151
152/**
James Smart3621a712009-04-06 18:47:14 -0400153 * lpfc_update_stats - Update statistical data for the command completion
James Smartea2151b2008-09-07 11:52:10 -0400154 * @phba: Pointer to HBA object.
155 * @lpfc_cmd: lpfc scsi command object pointer.
156 *
157 * This function is called when there is a command completion and this
158 * function updates the statistical data for the command completion.
159 **/
160static void
161lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
162{
163 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
164 struct lpfc_nodelist *pnode = rdata->pnode;
165 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
166 unsigned long flags;
167 struct Scsi_Host *shost = cmd->device->host;
168 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
169 unsigned long latency;
170 int i;
171
172 if (cmd->result)
173 return;
174
James Smart9f1e1b52008-12-04 22:39:40 -0500175 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
176
James Smartea2151b2008-09-07 11:52:10 -0400177 spin_lock_irqsave(shost->host_lock, flags);
178 if (!vport->stat_data_enabled ||
179 vport->stat_data_blocked ||
James Smart5989b8d2010-10-22 11:06:56 -0400180 !pnode ||
James Smartea2151b2008-09-07 11:52:10 -0400181 !pnode->lat_data ||
182 (phba->bucket_type == LPFC_NO_BUCKET)) {
183 spin_unlock_irqrestore(shost->host_lock, flags);
184 return;
185 }
James Smartea2151b2008-09-07 11:52:10 -0400186
187 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
188 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
189 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -0500190 /* check array subscript bounds */
191 if (i < 0)
192 i = 0;
193 else if (i >= LPFC_MAX_BUCKET_COUNT)
194 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -0400195 } else {
196 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
197 if (latency <= (phba->bucket_base +
198 ((1<<i)*phba->bucket_step)))
199 break;
200 }
201
202 pnode->lat_data[i].cmd_count++;
203 spin_unlock_irqrestore(shost->host_lock, flags);
204}
205
James Smartea2151b2008-09-07 11:52:10 -0400206/**
James Smart3621a712009-04-06 18:47:14 -0400207 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
James Smartea2151b2008-09-07 11:52:10 -0400208 * @phba: Pointer to HBA context object.
209 * @vport: Pointer to vport object.
210 * @ndlp: Pointer to FC node associated with the target.
211 * @lun: Lun number of the scsi device.
212 * @old_val: Old value of the queue depth.
213 * @new_val: New value of the queue depth.
214 *
215 * This function sends an event to the mgmt application indicating
216 * there is a change in the scsi device queue depth.
217 **/
218static void
219lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
220 struct lpfc_vport *vport,
221 struct lpfc_nodelist *ndlp,
222 uint32_t lun,
223 uint32_t old_val,
224 uint32_t new_val)
225{
226 struct lpfc_fast_path_event *fast_path_evt;
227 unsigned long flags;
228
229 fast_path_evt = lpfc_alloc_fast_evt(phba);
230 if (!fast_path_evt)
231 return;
232
233 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
234 FC_REG_SCSI_EVENT;
235 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
236 LPFC_EVENT_VARQUEDEPTH;
237
238 /* Report all luns with change in queue depth */
239 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
240 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
241 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
242 &ndlp->nlp_portname, sizeof(struct lpfc_name));
243 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
244 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
245 }
246
247 fast_path_evt->un.queue_depth_evt.oldval = old_val;
248 fast_path_evt->un.queue_depth_evt.newval = new_val;
249 fast_path_evt->vport = vport;
250
251 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
252 spin_lock_irqsave(&phba->hbalock, flags);
253 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
254 spin_unlock_irqrestore(&phba->hbalock, flags);
255 lpfc_worker_wake_up(phba);
256
257 return;
258}
259
James Smart9bad7672008-12-04 22:39:02 -0500260/**
James Smart5ffc2662009-11-18 15:39:44 -0500261 * lpfc_change_queue_depth - Alter scsi device queue depth
262 * @sdev: Pointer the scsi device on which to change the queue depth.
263 * @qdepth: New queue depth to set the sdev to.
264 * @reason: The reason for the queue depth change.
265 *
266 * This function is called by the midlayer and the LLD to alter the queue
267 * depth for a scsi device. This function sets the queue depth to the new
268 * value and sends an event out to log the queue depth change.
269 **/
270int
271lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
272{
273 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
274 struct lpfc_hba *phba = vport->phba;
275 struct lpfc_rport_data *rdata;
276 unsigned long new_queue_depth, old_queue_depth;
277
278 old_queue_depth = sdev->queue_depth;
279 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
280 new_queue_depth = sdev->queue_depth;
281 rdata = sdev->hostdata;
282 if (rdata)
283 lpfc_send_sdev_queuedepth_change_event(phba, vport,
284 rdata->pnode, sdev->lun,
285 old_queue_depth,
286 new_queue_depth);
287 return sdev->queue_depth;
288}
289
290/**
James Smart3621a712009-04-06 18:47:14 -0400291 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500292 * @phba: The Hba for which this call is being executed.
293 *
294 * This routine is called when there is resource error in driver or firmware.
295 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
296 * posts at most 1 event each second. This routine wakes up worker thread of
297 * @phba to process WORKER_RAM_DOWN_EVENT event.
298 *
299 * This routine should be called with no lock held.
300 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500301void
James Smarteaf15d52008-12-04 22:39:29 -0500302lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500303{
304 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400305 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500306
307 spin_lock_irqsave(&phba->hbalock, flags);
308 atomic_inc(&phba->num_rsrc_err);
309 phba->last_rsrc_error_time = jiffies;
310
311 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
312 spin_unlock_irqrestore(&phba->hbalock, flags);
313 return;
314 }
315
316 phba->last_ramp_down_time = jiffies;
317
318 spin_unlock_irqrestore(&phba->hbalock, flags);
319
320 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400321 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
322 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500323 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500324 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
325
James Smart5e9d9b82008-06-14 22:52:53 -0400326 if (!evt_posted)
327 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500328 return;
329}
330
James Smart9bad7672008-12-04 22:39:02 -0500331/**
James Smart3621a712009-04-06 18:47:14 -0400332 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
James Smart9bad7672008-12-04 22:39:02 -0500333 * @phba: The Hba for which this call is being executed.
334 *
335 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
336 * post at most 1 event every 5 minute after last_ramp_up_time or
337 * last_rsrc_error_time. This routine wakes up worker thread of @phba
338 * to process WORKER_RAM_DOWN_EVENT event.
339 *
340 * This routine should be called with no lock held.
341 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500342static inline void
James Smart3de2a652007-08-02 11:09:59 -0400343lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smarta257bf92009-04-06 18:48:10 -0400344 uint32_t queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500345{
346 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -0400347 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400348 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500349 atomic_inc(&phba->num_cmd_success);
350
James Smarta257bf92009-04-06 18:48:10 -0400351 if (vport->cfg_lun_queue_depth <= queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500352 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500353 spin_lock_irqsave(&phba->hbalock, flags);
James Smart5ffc2662009-11-18 15:39:44 -0500354 if (time_before(jiffies,
355 phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
356 time_before(jiffies,
357 phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500358 spin_unlock_irqrestore(&phba->hbalock, flags);
359 return;
360 }
James Smart92d7f7b2007-06-17 19:56:38 -0500361 phba->last_ramp_up_time = jiffies;
362 spin_unlock_irqrestore(&phba->hbalock, flags);
363
364 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400365 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
366 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500367 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500368 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
369
James Smart5e9d9b82008-06-14 22:52:53 -0400370 if (!evt_posted)
371 lpfc_worker_wake_up(phba);
372 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500373}
374
James Smart9bad7672008-12-04 22:39:02 -0500375/**
James Smart3621a712009-04-06 18:47:14 -0400376 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500377 * @phba: The Hba for which this call is being executed.
378 *
379 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
380 * thread.This routine reduces queue depth for all scsi device on each vport
381 * associated with @phba.
382 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500383void
384lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
385{
James Smart549e55c2007-08-02 11:09:51 -0400386 struct lpfc_vport **vports;
387 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500388 struct scsi_device *sdev;
James Smart5ffc2662009-11-18 15:39:44 -0500389 unsigned long new_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500390 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400391 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500392
393 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
394 num_cmd_success = atomic_read(&phba->num_cmd_success);
395
James Smart549e55c2007-08-02 11:09:51 -0400396 vports = lpfc_create_vport_work_array(phba);
397 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400398 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400399 shost = lpfc_shost_from_vport(vports[i]);
400 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500401 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400402 sdev->queue_depth * num_rsrc_err /
403 (num_rsrc_err + num_cmd_success);
404 if (!new_queue_depth)
405 new_queue_depth = sdev->queue_depth - 1;
406 else
407 new_queue_depth = sdev->queue_depth -
408 new_queue_depth;
James Smart5ffc2662009-11-18 15:39:44 -0500409 lpfc_change_queue_depth(sdev, new_queue_depth,
410 SCSI_QDEPTH_DEFAULT);
James Smart549e55c2007-08-02 11:09:51 -0400411 }
James Smart92d7f7b2007-06-17 19:56:38 -0500412 }
James Smart09372822008-01-11 01:52:54 -0500413 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500414 atomic_set(&phba->num_rsrc_err, 0);
415 atomic_set(&phba->num_cmd_success, 0);
416}
417
James Smart9bad7672008-12-04 22:39:02 -0500418/**
James Smart3621a712009-04-06 18:47:14 -0400419 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500420 * @phba: The Hba for which this call is being executed.
421 *
422 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
423 * thread.This routine increases queue depth for all scsi device on each vport
424 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
425 * num_cmd_success to zero.
426 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500427void
428lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
429{
James Smart549e55c2007-08-02 11:09:51 -0400430 struct lpfc_vport **vports;
431 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500432 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400433 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500434
James Smart549e55c2007-08-02 11:09:51 -0400435 vports = lpfc_create_vport_work_array(phba);
436 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400437 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400438 shost = lpfc_shost_from_vport(vports[i]);
439 shost_for_each_device(sdev, shost) {
James Smart97eab632008-04-07 10:16:05 -0400440 if (vports[i]->cfg_lun_queue_depth <=
441 sdev->queue_depth)
442 continue;
James Smart5ffc2662009-11-18 15:39:44 -0500443 lpfc_change_queue_depth(sdev,
444 sdev->queue_depth+1,
445 SCSI_QDEPTH_RAMP_UP);
James Smart549e55c2007-08-02 11:09:51 -0400446 }
James Smart92d7f7b2007-06-17 19:56:38 -0500447 }
James Smart09372822008-01-11 01:52:54 -0500448 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500449 atomic_set(&phba->num_rsrc_err, 0);
450 atomic_set(&phba->num_cmd_success, 0);
451}
452
James Smarta8e497d2008-08-24 21:50:11 -0400453/**
James Smart3621a712009-04-06 18:47:14 -0400454 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400455 * @phba: Pointer to HBA context object.
456 *
457 * This function walks vport list and set each SCSI host to block state
458 * by invoking fc_remote_port_delete() routine. This function is invoked
459 * with EEH when device's PCI slot has been permanently disabled.
460 **/
461void
462lpfc_scsi_dev_block(struct lpfc_hba *phba)
463{
464 struct lpfc_vport **vports;
465 struct Scsi_Host *shost;
466 struct scsi_device *sdev;
467 struct fc_rport *rport;
468 int i;
469
470 vports = lpfc_create_vport_work_array(phba);
471 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400472 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400473 shost = lpfc_shost_from_vport(vports[i]);
474 shost_for_each_device(sdev, shost) {
475 rport = starget_to_rport(scsi_target(sdev));
476 fc_remote_port_delete(rport);
477 }
478 }
479 lpfc_destroy_vport_work_array(phba, vports);
480}
481
James Smart9bad7672008-12-04 22:39:02 -0500482/**
James Smart3772a992009-05-22 14:50:54 -0400483 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500484 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400485 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500486 *
James Smart3772a992009-05-22 14:50:54 -0400487 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
488 * the scsi buffer contains all the necessary information needed to initiate
489 * a SCSI I/O. The non-DMAable buffer region contains information to build
490 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
491 * and the initial BPL. In addition to allocating memory, the FCP CMND and
492 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500493 *
494 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400495 * int - number of scsi buffers that were allocated.
496 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500497 **/
James Smart3772a992009-05-22 14:50:54 -0400498static int
499lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500500{
James Smart2e0fef82007-06-17 19:56:36 -0500501 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500502 struct lpfc_scsi_buf *psb;
503 struct ulp_bde64 *bpl;
504 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400505 dma_addr_t pdma_phys_fcp_cmd;
506 dma_addr_t pdma_phys_fcp_rsp;
507 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500508 uint16_t iotag;
James Smart3772a992009-05-22 14:50:54 -0400509 int bcnt;
dea31012005-04-17 16:05:31 -0500510
James Smart3772a992009-05-22 14:50:54 -0400511 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
512 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
513 if (!psb)
514 break;
dea31012005-04-17 16:05:31 -0500515
James Smart3772a992009-05-22 14:50:54 -0400516 /*
517 * Get memory from the pci pool to map the virt space to pci
518 * bus space for an I/O. The DMA buffer includes space for the
519 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
520 * necessary to support the sg_tablesize.
521 */
522 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
523 GFP_KERNEL, &psb->dma_handle);
524 if (!psb->data) {
525 kfree(psb);
526 break;
527 }
dea31012005-04-17 16:05:31 -0500528
James Smart3772a992009-05-22 14:50:54 -0400529 /* Initialize virtual ptrs to dma_buf region. */
530 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
dea31012005-04-17 16:05:31 -0500531
James Smart3772a992009-05-22 14:50:54 -0400532 /* Allocate iotag for psb->cur_iocbq. */
533 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
534 if (iotag == 0) {
535 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
536 psb->data, psb->dma_handle);
537 kfree(psb);
538 break;
539 }
540 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500541
James Smart3772a992009-05-22 14:50:54 -0400542 psb->fcp_cmnd = psb->data;
543 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
544 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400545 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500546
James Smart3772a992009-05-22 14:50:54 -0400547 /* Initialize local short-hand pointers. */
548 bpl = psb->fcp_bpl;
549 pdma_phys_fcp_cmd = psb->dma_handle;
550 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
551 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
552 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500553
James Smart3772a992009-05-22 14:50:54 -0400554 /*
555 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
556 * are sg list bdes. Initialize the first two and leave the
557 * rest for queuecommand.
558 */
559 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
560 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
561 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
562 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
563 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500564
James Smart3772a992009-05-22 14:50:54 -0400565 /* Setup the physical region for the FCP RSP */
566 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
567 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
568 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
569 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
570 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
571
572 /*
573 * Since the IOCB for the FCP I/O is built into this
574 * lpfc_scsi_buf, initialize it with all known data now.
575 */
576 iocb = &psb->cur_iocbq.iocb;
577 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
578 if ((phba->sli_rev == 3) &&
579 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
580 /* fill in immediate fcp command BDE */
581 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
582 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
583 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
584 unsli3.fcp_ext.icd);
585 iocb->un.fcpi64.bdl.addrHigh = 0;
586 iocb->ulpBdeCount = 0;
587 iocb->ulpLe = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300588 /* fill in response BDE */
James Smart3772a992009-05-22 14:50:54 -0400589 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
590 BUFF_TYPE_BDE_64;
591 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
592 sizeof(struct fcp_rsp);
593 iocb->unsli3.fcp_ext.rbde.addrLow =
594 putPaddrLow(pdma_phys_fcp_rsp);
595 iocb->unsli3.fcp_ext.rbde.addrHigh =
596 putPaddrHigh(pdma_phys_fcp_rsp);
597 } else {
598 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
599 iocb->un.fcpi64.bdl.bdeSize =
600 (2 * sizeof(struct ulp_bde64));
601 iocb->un.fcpi64.bdl.addrLow =
602 putPaddrLow(pdma_phys_bpl);
603 iocb->un.fcpi64.bdl.addrHigh =
604 putPaddrHigh(pdma_phys_bpl);
605 iocb->ulpBdeCount = 1;
606 iocb->ulpLe = 1;
607 }
608 iocb->ulpClass = CLASS3;
609 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400610 /* Put it back into the SCSI buffer list */
James Smarteee88772010-09-29 11:19:08 -0400611 psb->cur_iocbq.context1 = psb;
James Smart1c6f4ef52009-11-18 15:40:49 -0500612 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400613
James Smart34b02dc2008-08-24 21:49:55 -0400614 }
dea31012005-04-17 16:05:31 -0500615
James Smart3772a992009-05-22 14:50:54 -0400616 return bcnt;
dea31012005-04-17 16:05:31 -0500617}
618
James Smart9bad7672008-12-04 22:39:02 -0500619/**
James Smart1151e3e2011-02-16 12:39:35 -0500620 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
621 * @vport: pointer to lpfc vport data structure.
622 *
623 * This routine is invoked by the vport cleanup for deletions and the cleanup
624 * for an ndlp on removal.
625 **/
626void
627lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
628{
629 struct lpfc_hba *phba = vport->phba;
630 struct lpfc_scsi_buf *psb, *next_psb;
631 unsigned long iflag = 0;
632
633 spin_lock_irqsave(&phba->hbalock, iflag);
634 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
635 list_for_each_entry_safe(psb, next_psb,
636 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
637 if (psb->rdata && psb->rdata->pnode
638 && psb->rdata->pnode->vport == vport)
639 psb->rdata = NULL;
640 }
641 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
642 spin_unlock_irqrestore(&phba->hbalock, iflag);
643}
644
645/**
James Smartda0436e2009-05-22 14:51:39 -0400646 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
647 * @phba: pointer to lpfc hba data structure.
648 * @axri: pointer to the fcp xri abort wcqe structure.
649 *
650 * This routine is invoked by the worker thread to process a SLI4 fast-path
651 * FCP aborted xri.
652 **/
653void
654lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
655 struct sli4_wcqe_xri_aborted *axri)
656{
657 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500658 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartda0436e2009-05-22 14:51:39 -0400659 struct lpfc_scsi_buf *psb, *next_psb;
660 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500661 struct lpfc_iocbq *iocbq;
662 int i;
James Smart19ca7602010-11-20 23:11:55 -0500663 struct lpfc_nodelist *ndlp;
664 int rrq_empty = 0;
James Smart589a52d2010-07-14 15:30:54 -0400665 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smartda0436e2009-05-22 14:51:39 -0400666
James Smart0f65ff62010-02-26 14:14:23 -0500667 spin_lock_irqsave(&phba->hbalock, iflag);
668 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400669 list_for_each_entry_safe(psb, next_psb,
670 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
671 if (psb->cur_iocbq.sli4_xritag == xri) {
672 list_del(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500673 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400674 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500675 spin_unlock(
676 &phba->sli4_hba.abts_scsi_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500677 if (psb->rdata && psb->rdata->pnode)
678 ndlp = psb->rdata->pnode;
679 else
680 ndlp = NULL;
681
James Smart19ca7602010-11-20 23:11:55 -0500682 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500683 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart19ca7602010-11-20 23:11:55 -0500684 if (ndlp)
685 lpfc_set_rrq_active(phba, ndlp, xri, rxid, 1);
James Smartda0436e2009-05-22 14:51:39 -0400686 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500687 if (rrq_empty)
688 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400689 return;
690 }
691 }
James Smart0f65ff62010-02-26 14:14:23 -0500692 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
693 for (i = 1; i <= phba->sli.last_iotag; i++) {
694 iocbq = phba->sli.iocbq_lookup[i];
695
696 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
697 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
698 continue;
699 if (iocbq->sli4_xritag != xri)
700 continue;
701 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
702 psb->exch_busy = 0;
703 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart589a52d2010-07-14 15:30:54 -0400704 if (pring->txq_cnt)
705 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500706 return;
707
708 }
709 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400710}
711
712/**
713 * lpfc_sli4_repost_scsi_sgl_list - Repsot the Scsi buffers sgl pages as block
714 * @phba: pointer to lpfc hba data structure.
715 *
716 * This routine walks the list of scsi buffers that have been allocated and
717 * repost them to the HBA by using SGL block post. This is needed after a
718 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
719 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
720 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
721 *
722 * Returns: 0 = success, non-zero failure.
723 **/
724int
725lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
726{
727 struct lpfc_scsi_buf *psb;
728 int index, status, bcnt = 0, rcnt = 0, rc = 0;
729 LIST_HEAD(sblist);
730
731 for (index = 0; index < phba->sli4_hba.scsi_xri_cnt; index++) {
732 psb = phba->sli4_hba.lpfc_scsi_psb_array[index];
733 if (psb) {
734 /* Remove from SCSI buffer list */
735 list_del(&psb->list);
736 /* Add it to a local SCSI buffer list */
737 list_add_tail(&psb->list, &sblist);
738 if (++rcnt == LPFC_NEMBED_MBOX_SGL_CNT) {
739 bcnt = rcnt;
740 rcnt = 0;
741 }
742 } else
743 /* A hole present in the XRI array, need to skip */
744 bcnt = rcnt;
745
746 if (index == phba->sli4_hba.scsi_xri_cnt - 1)
747 /* End of XRI array for SCSI buffer, complete */
748 bcnt = rcnt;
749
750 /* Continue until collect up to a nembed page worth of sgls */
751 if (bcnt == 0)
752 continue;
753 /* Now, post the SCSI buffer list sgls as a block */
James Smart6d368e52011-05-24 11:44:12 -0400754 if (!phba->sli4_hba.extents_in_use)
755 status = lpfc_sli4_post_scsi_sgl_block(phba,
756 &sblist,
757 bcnt);
758 else
759 status = lpfc_sli4_post_scsi_sgl_blk_ext(phba,
760 &sblist,
761 bcnt);
James Smartda0436e2009-05-22 14:51:39 -0400762 /* Reset SCSI buffer count for next round of posting */
763 bcnt = 0;
764 while (!list_empty(&sblist)) {
765 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
766 list);
767 if (status) {
768 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500769 psb->exch_busy = 1;
James Smartda0436e2009-05-22 14:51:39 -0400770 rc++;
James Smart341af102010-01-26 23:07:37 -0500771 } else {
772 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400773 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500774 }
James Smartda0436e2009-05-22 14:51:39 -0400775 /* Put it back into the SCSI buffer list */
776 lpfc_release_scsi_buf_s4(phba, psb);
777 }
778 }
779 return rc;
780}
781
782/**
783 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
784 * @vport: The virtual port for which this call being executed.
785 * @num_to_allocate: The requested number of buffers to allocate.
786 *
787 * This routine allocates a scsi buffer for device with SLI-4 interface spec,
788 * the scsi buffer contains all the necessary information needed to initiate
789 * a SCSI I/O.
790 *
791 * Return codes:
792 * int - number of scsi buffers that were allocated.
793 * 0 = failure, less than num_to_alloc is a partial failure.
794 **/
795static int
796lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
797{
798 struct lpfc_hba *phba = vport->phba;
799 struct lpfc_scsi_buf *psb;
800 struct sli4_sge *sgl;
801 IOCB_t *iocb;
802 dma_addr_t pdma_phys_fcp_cmd;
803 dma_addr_t pdma_phys_fcp_rsp;
804 dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
James Smart6d368e52011-05-24 11:44:12 -0400805 uint16_t iotag, last_xritag = NO_XRI, lxri = 0;
James Smartda0436e2009-05-22 14:51:39 -0400806 int status = 0, index;
807 int bcnt;
808 int non_sequential_xri = 0;
James Smartda0436e2009-05-22 14:51:39 -0400809 LIST_HEAD(sblist);
810
811 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
812 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
813 if (!psb)
814 break;
815
816 /*
817 * Get memory from the pci pool to map the virt space to pci bus
818 * space for an I/O. The DMA buffer includes space for the
819 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
820 * necessary to support the sg_tablesize.
821 */
822 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
823 GFP_KERNEL, &psb->dma_handle);
824 if (!psb->data) {
825 kfree(psb);
826 break;
827 }
828
829 /* Initialize virtual ptrs to dma_buf region. */
830 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
831
832 /* Allocate iotag for psb->cur_iocbq. */
833 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
834 if (iotag == 0) {
James Smartb92938b2010-06-07 15:24:12 -0400835 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
836 psb->data, psb->dma_handle);
James Smartda0436e2009-05-22 14:51:39 -0400837 kfree(psb);
838 break;
839 }
840
James Smart6d368e52011-05-24 11:44:12 -0400841 lxri = lpfc_sli4_next_xritag(phba);
842 if (lxri == NO_XRI) {
James Smartda0436e2009-05-22 14:51:39 -0400843 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
844 psb->data, psb->dma_handle);
845 kfree(psb);
846 break;
847 }
James Smart6d368e52011-05-24 11:44:12 -0400848 psb->cur_iocbq.sli4_lxritag = lxri;
849 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smartda0436e2009-05-22 14:51:39 -0400850 if (last_xritag != NO_XRI
851 && psb->cur_iocbq.sli4_xritag != (last_xritag+1)) {
852 non_sequential_xri = 1;
853 } else
854 list_add_tail(&psb->list, &sblist);
855 last_xritag = psb->cur_iocbq.sli4_xritag;
856
857 index = phba->sli4_hba.scsi_xri_cnt++;
858 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
859
860 psb->fcp_bpl = psb->data;
861 psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
862 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
863 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
864 sizeof(struct fcp_cmnd));
865
866 /* Initialize local short-hand pointers. */
867 sgl = (struct sli4_sge *)psb->fcp_bpl;
868 pdma_phys_bpl = psb->dma_handle;
869 pdma_phys_fcp_cmd =
870 (psb->dma_handle + phba->cfg_sg_dma_buf_size)
871 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
872 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
873
874 /*
875 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
876 * are sg list bdes. Initialize the first two and leave the
877 * rest for queuecommand.
878 */
879 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
880 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
James Smart05580562011-05-24 11:40:48 -0400881 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400882 bf_set(lpfc_sli4_sge_last, sgl, 0);
883 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500884 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
James Smartda0436e2009-05-22 14:51:39 -0400885 sgl++;
886
887 /* Setup the physical region for the FCP RSP */
888 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
889 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smart05580562011-05-24 11:40:48 -0400890 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400891 bf_set(lpfc_sli4_sge_last, sgl, 1);
892 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500893 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -0400894
895 /*
896 * Since the IOCB for the FCP I/O is built into this
897 * lpfc_scsi_buf, initialize it with all known data now.
898 */
899 iocb = &psb->cur_iocbq.iocb;
900 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
901 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
902 /* setting the BLP size to 2 * sizeof BDE may not be correct.
903 * We are setting the bpl to point to out sgl. An sgl's
904 * entries are 16 bytes, a bpl entries are 12 bytes.
905 */
906 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
907 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
908 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
909 iocb->ulpBdeCount = 1;
910 iocb->ulpLe = 1;
911 iocb->ulpClass = CLASS3;
James Smarteee88772010-09-29 11:19:08 -0400912 psb->cur_iocbq.context1 = psb;
James Smartda0436e2009-05-22 14:51:39 -0400913 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
914 pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
915 else
916 pdma_phys_bpl1 = 0;
917 psb->dma_phys_bpl = pdma_phys_bpl;
918 phba->sli4_hba.lpfc_scsi_psb_array[index] = psb;
919 if (non_sequential_xri) {
920 status = lpfc_sli4_post_sgl(phba, pdma_phys_bpl,
921 pdma_phys_bpl1,
922 psb->cur_iocbq.sli4_xritag);
923 if (status) {
924 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500925 psb->exch_busy = 1;
James Smart341af102010-01-26 23:07:37 -0500926 } else {
927 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400928 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500929 }
James Smartda0436e2009-05-22 14:51:39 -0400930 /* Put it back into the SCSI buffer list */
931 lpfc_release_scsi_buf_s4(phba, psb);
932 break;
933 }
934 }
935 if (bcnt) {
James Smart6d368e52011-05-24 11:44:12 -0400936 if (!phba->sli4_hba.extents_in_use)
937 status = lpfc_sli4_post_scsi_sgl_block(phba,
938 &sblist,
939 bcnt);
940 else
941 status = lpfc_sli4_post_scsi_sgl_blk_ext(phba,
942 &sblist,
943 bcnt);
944
945 if (status) {
946 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
947 "3021 SCSI SGL post error %d\n",
948 status);
949 bcnt = 0;
950 }
James Smartda0436e2009-05-22 14:51:39 -0400951 /* Reset SCSI buffer count for next round of posting */
952 while (!list_empty(&sblist)) {
953 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
954 list);
955 if (status) {
956 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500957 psb->exch_busy = 1;
James Smart341af102010-01-26 23:07:37 -0500958 } else {
959 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400960 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500961 }
James Smartda0436e2009-05-22 14:51:39 -0400962 /* Put it back into the SCSI buffer list */
963 lpfc_release_scsi_buf_s4(phba, psb);
964 }
965 }
966
James Smartd7c47992010-06-08 18:31:54 -0400967 return bcnt + non_sequential_xri;
James Smartda0436e2009-05-22 14:51:39 -0400968}
969
970/**
James Smart3772a992009-05-22 14:50:54 -0400971 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
972 * @vport: The virtual port for which this call being executed.
973 * @num_to_allocate: The requested number of buffers to allocate.
974 *
975 * This routine wraps the actual SCSI buffer allocator function pointer from
976 * the lpfc_hba struct.
977 *
978 * Return codes:
979 * int - number of scsi buffers that were allocated.
980 * 0 = failure, less than num_to_alloc is a partial failure.
981 **/
982static inline int
983lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
984{
985 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
986}
987
988/**
James Smart19ca7602010-11-20 23:11:55 -0500989 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -0400990 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -0500991 *
992 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
993 * and returns to caller.
994 *
995 * Return codes:
996 * NULL - Error
997 * Pointer to lpfc_scsi_buf - Success
998 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +0100999static struct lpfc_scsi_buf*
James Smart19ca7602010-11-20 23:11:55 -05001000lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001001{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001002 struct lpfc_scsi_buf * lpfc_cmd = NULL;
1003 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001004 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001005
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001006 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001007 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -04001008 if (lpfc_cmd) {
1009 lpfc_cmd->seg_cnt = 0;
1010 lpfc_cmd->nonsg_phys = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001011 lpfc_cmd->prot_seg_cnt = 0;
James Smart1dcb58e2007-04-25 09:51:30 -04001012 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001013 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001014 return lpfc_cmd;
1015}
James Smart19ca7602010-11-20 23:11:55 -05001016/**
1017 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1018 * @phba: The HBA for which this call is being executed.
1019 *
1020 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1021 * and returns to caller.
1022 *
1023 * Return codes:
1024 * NULL - Error
1025 * Pointer to lpfc_scsi_buf - Success
1026 **/
1027static struct lpfc_scsi_buf*
1028lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1029{
James Smart1151e3e2011-02-16 12:39:35 -05001030 struct lpfc_scsi_buf *lpfc_cmd ;
James Smart19ca7602010-11-20 23:11:55 -05001031 unsigned long iflag = 0;
1032 int found = 0;
1033
1034 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -05001035 list_for_each_entry(lpfc_cmd, &phba->lpfc_scsi_buf_list,
1036 list) {
James Smart19ca7602010-11-20 23:11:55 -05001037 if (lpfc_test_rrq_active(phba, ndlp,
James Smart1151e3e2011-02-16 12:39:35 -05001038 lpfc_cmd->cur_iocbq.sli4_xritag))
1039 continue;
1040 list_del(&lpfc_cmd->list);
James Smart19ca7602010-11-20 23:11:55 -05001041 found = 1;
1042 lpfc_cmd->seg_cnt = 0;
1043 lpfc_cmd->nonsg_phys = 0;
1044 lpfc_cmd->prot_seg_cnt = 0;
James Smart1151e3e2011-02-16 12:39:35 -05001045 break;
James Smart19ca7602010-11-20 23:11:55 -05001046 }
James Smart1151e3e2011-02-16 12:39:35 -05001047 spin_unlock_irqrestore(&phba->scsi_buf_list_lock,
1048 iflag);
1049 if (!found)
1050 return NULL;
1051 else
1052 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001053}
1054/**
1055 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1056 * @phba: The HBA for which this call is being executed.
1057 *
1058 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1059 * and returns to caller.
1060 *
1061 * Return codes:
1062 * NULL - Error
1063 * Pointer to lpfc_scsi_buf - Success
1064 **/
1065static struct lpfc_scsi_buf*
1066lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1067{
1068 return phba->lpfc_get_scsi_buf(phba, ndlp);
1069}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001070
James Smart9bad7672008-12-04 22:39:02 -05001071/**
James Smart3772a992009-05-22 14:50:54 -04001072 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -05001073 * @phba: The Hba for which this call is being executed.
1074 * @psb: The scsi buffer which is being released.
1075 *
1076 * This routine releases @psb scsi buffer by adding it to tail of @phba
1077 * lpfc_scsi_buf_list list.
1078 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001079static void
James Smart3772a992009-05-22 14:50:54 -04001080lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001081{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001082 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001083
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001084 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001085 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -05001086 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001087 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -05001088}
1089
James Smart9bad7672008-12-04 22:39:02 -05001090/**
James Smartda0436e2009-05-22 14:51:39 -04001091 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1092 * @phba: The Hba for which this call is being executed.
1093 * @psb: The scsi buffer which is being released.
1094 *
1095 * This routine releases @psb scsi buffer by adding it to tail of @phba
1096 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1097 * and cannot be reused for at least RA_TOV amount of time if it was
1098 * aborted.
1099 **/
1100static void
1101lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1102{
1103 unsigned long iflag = 0;
1104
James Smart341af102010-01-26 23:07:37 -05001105 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -04001106 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1107 iflag);
1108 psb->pCmd = NULL;
1109 list_add_tail(&psb->list,
1110 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1111 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1112 iflag);
1113 } else {
1114
1115 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1116 psb->pCmd = NULL;
1117 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
1118 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
1119 }
1120}
1121
1122/**
James Smart3772a992009-05-22 14:50:54 -04001123 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1124 * @phba: The Hba for which this call is being executed.
1125 * @psb: The scsi buffer which is being released.
1126 *
1127 * This routine releases @psb scsi buffer by adding it to tail of @phba
1128 * lpfc_scsi_buf_list list.
1129 **/
1130static void
1131lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1132{
1133
1134 phba->lpfc_release_scsi_buf(phba, psb);
1135}
1136
1137/**
1138 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001139 * @phba: The Hba for which this call is being executed.
1140 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1141 *
1142 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001143 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1144 * through sg elements and format the bdea. This routine also initializes all
1145 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001146 *
1147 * Return codes:
1148 * 1 - Error
1149 * 0 - Success
1150 **/
dea31012005-04-17 16:05:31 -05001151static int
James Smart3772a992009-05-22 14:50:54 -04001152lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001153{
1154 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1155 struct scatterlist *sgel = NULL;
1156 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1157 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001158 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001159 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001160 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001161 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001162 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001163 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001164
1165 /*
1166 * There are three possibilities here - use scatter-gather segment, use
1167 * the single mapping, or neither. Start the lpfc command prep by
1168 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1169 * data bde entry.
1170 */
1171 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001172 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001173 /*
1174 * The driver stores the segment count returned from pci_map_sg
1175 * because this a count of dma-mappings used to map the use_sg
1176 * pages. They are not guaranteed to be the same for those
1177 * architectures that implement an IOMMU.
1178 */
dea31012005-04-17 16:05:31 -05001179
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001180 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1181 scsi_sg_count(scsi_cmnd), datadir);
1182 if (unlikely(!nseg))
1183 return 1;
1184
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001185 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001186 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001187 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1188 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001189 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001190 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001191 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001192 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001193 return 1;
1194 }
1195
1196 /*
1197 * The driver established a maximum scatter-gather segment count
1198 * during probe that limits the number of sg elements in any
1199 * single scsi command. Just run through the seg_cnt and format
1200 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001201 * When using SLI-3 the driver will try to fit all the BDEs into
1202 * the IOCB. If it can't then the BDEs get added to a BPL as it
1203 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001204 */
James Smart34b02dc2008-08-24 21:49:55 -04001205 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001206 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001207 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001208 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001209 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001210 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1211 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1212 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1213 data_bde->addrLow = putPaddrLow(physaddr);
1214 data_bde->addrHigh = putPaddrHigh(physaddr);
1215 data_bde++;
1216 } else {
1217 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1218 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1219 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1220 bpl->addrLow =
1221 le32_to_cpu(putPaddrLow(physaddr));
1222 bpl->addrHigh =
1223 le32_to_cpu(putPaddrHigh(physaddr));
1224 bpl++;
1225 }
dea31012005-04-17 16:05:31 -05001226 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001227 }
dea31012005-04-17 16:05:31 -05001228
1229 /*
1230 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001231 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1232 * explicitly reinitialized and for SLI-3 the extended bde count is
1233 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001234 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001235 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001236 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1237 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001238 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1239 /*
1240 * The extended IOCB format can only fit 3 BDE or a BPL.
1241 * This I/O has more than 3 BDE so the 1st data bde will
1242 * be a BPL that is filled in here.
1243 */
1244 physaddr = lpfc_cmd->dma_handle;
1245 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1246 data_bde->tus.f.bdeSize = (num_bde *
1247 sizeof(struct ulp_bde64));
1248 physaddr += (sizeof(struct fcp_cmnd) +
1249 sizeof(struct fcp_rsp) +
1250 (2 * sizeof(struct ulp_bde64)));
1251 data_bde->addrHigh = putPaddrHigh(physaddr);
1252 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001253 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -04001254 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1255 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001256 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -04001257 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1258 }
1259 } else {
1260 iocb_cmd->un.fcpi64.bdl.bdeSize =
1261 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001262 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001263 }
James Smart09372822008-01-11 01:52:54 -05001264 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001265
1266 /*
1267 * Due to difference in data length between DIF/non-DIF paths,
1268 * we need to set word 4 of IOCB here
1269 */
James Smarta257bf92009-04-06 18:48:10 -04001270 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001271 return 0;
1272}
1273
James Smartf9bb2da2011-10-10 21:34:11 -04001274static inline unsigned
1275lpfc_cmd_blksize(struct scsi_cmnd *sc)
1276{
1277 return sc->device->sector_size;
1278}
1279
1280#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1281/*
1282 * Given a scsi cmnd, determine the BlockGuard tags to be used with it
1283 * @sc: The SCSI command to examine
1284 * @reftag: (out) BlockGuard reference tag for transmitted data
1285 * @apptag: (out) BlockGuard application tag for transmitted data
1286 * @new_guard (in) Value to replace CRC with if needed
1287 *
1288 * Returns (1) if error injection was performed, (0) otherwise
1289 */
1290static int
1291lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1292 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1293{
1294 struct scatterlist *sgpe; /* s/g prot entry */
1295 struct scatterlist *sgde; /* s/g data entry */
1296 struct scsi_dif_tuple *src;
1297 uint32_t op = scsi_get_prot_op(sc);
1298 uint32_t blksize;
1299 uint32_t numblks;
1300 sector_t lba;
1301 int rc = 0;
1302
1303 if (op == SCSI_PROT_NORMAL)
1304 return 0;
1305
1306 lba = scsi_get_lba(sc);
1307 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1308 blksize = lpfc_cmd_blksize(sc);
1309 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1310
1311 /* Make sure we have the right LBA if one is specified */
1312 if ((phba->lpfc_injerr_lba < lba) ||
1313 (phba->lpfc_injerr_lba >= (lba + numblks)))
1314 return 0;
1315 }
1316
1317 sgpe = scsi_prot_sglist(sc);
1318 sgde = scsi_sglist(sc);
1319
1320 /* Should we change the Reference Tag */
1321 if (reftag) {
1322 /*
1323 * If we are SCSI_PROT_WRITE_STRIP, the protection data is
1324 * being stripped from the wire, thus it doesn't matter.
1325 */
1326 if ((op == SCSI_PROT_WRITE_PASS) ||
1327 (op == SCSI_PROT_WRITE_INSERT)) {
1328 if (phba->lpfc_injerr_wref_cnt) {
1329
1330 /* DEADBEEF will be the reftag on the wire */
1331 *reftag = 0xDEADBEEF;
1332 phba->lpfc_injerr_wref_cnt--;
1333 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1334 rc = 1;
1335
1336 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1337 "9081 BLKGRD: Injecting reftag error: "
1338 "write lba x%lx\n", (unsigned long)lba);
1339 }
1340 } else {
1341 if (phba->lpfc_injerr_rref_cnt) {
1342 *reftag = 0xDEADBEEF;
1343 phba->lpfc_injerr_rref_cnt--;
1344 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1345 rc = 1;
1346
1347 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1348 "9076 BLKGRD: Injecting reftag error: "
1349 "read lba x%lx\n", (unsigned long)lba);
1350 }
1351 }
1352 }
1353
1354 /* Should we change the Application Tag */
1355 if (apptag) {
1356 /*
1357 * If we are SCSI_PROT_WRITE_STRIP, the protection data is
1358 * being stripped from the wire, thus it doesn't matter.
1359 */
1360 if ((op == SCSI_PROT_WRITE_PASS) ||
1361 (op == SCSI_PROT_WRITE_INSERT)) {
1362 if (phba->lpfc_injerr_wapp_cnt) {
1363
1364 /* DEAD will be the apptag on the wire */
1365 *apptag = 0xDEAD;
1366 phba->lpfc_injerr_wapp_cnt--;
1367 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1368 rc = 1;
1369
1370 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1371 "9077 BLKGRD: Injecting apptag error: "
1372 "write lba x%lx\n", (unsigned long)lba);
1373 }
1374 } else {
1375 if (phba->lpfc_injerr_rapp_cnt) {
1376 *apptag = 0xDEAD;
1377 phba->lpfc_injerr_rapp_cnt--;
1378 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1379 rc = 1;
1380
1381 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1382 "9078 BLKGRD: Injecting apptag error: "
1383 "read lba x%lx\n", (unsigned long)lba);
1384 }
1385 }
1386 }
1387
1388 /* Should we change the Guard Tag */
1389
1390 /*
1391 * If we are SCSI_PROT_WRITE_INSERT, the protection data is
1392 * being on the wire is being fully generated on the HBA.
1393 * The host cannot change it or force an error.
1394 */
1395 if (((op == SCSI_PROT_WRITE_STRIP) ||
1396 (op == SCSI_PROT_WRITE_PASS)) &&
1397 phba->lpfc_injerr_wgrd_cnt) {
1398 if (sgpe) {
1399 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1400 /*
1401 * Just inject an error in the first
1402 * prot block.
1403 */
1404 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1405 "9079 BLKGRD: Injecting guard error: "
1406 "write lba x%lx oldGuard x%x refTag x%x\n",
1407 (unsigned long)lba, src->guard_tag,
1408 src->ref_tag);
1409
1410 src->guard_tag = (uint16_t)new_guard;
1411 phba->lpfc_injerr_wgrd_cnt--;
1412 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1413 rc = 1;
1414
1415 } else {
1416 blksize = lpfc_cmd_blksize(sc);
1417 /*
1418 * Jump past the first data block
1419 * and inject an error in the
1420 * prot data. The prot data is already
1421 * embedded after the regular data.
1422 */
1423 src = (struct scsi_dif_tuple *)
1424 (sg_virt(sgde) + blksize);
1425
1426 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1427 "9080 BLKGRD: Injecting guard error: "
1428 "write lba x%lx oldGuard x%x refTag x%x\n",
1429 (unsigned long)lba, src->guard_tag,
1430 src->ref_tag);
1431
1432 src->guard_tag = (uint16_t)new_guard;
1433 phba->lpfc_injerr_wgrd_cnt--;
1434 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1435 rc = 1;
1436 }
1437 }
1438 return rc;
1439}
1440#endif
1441
James Smarte2a0a9d2008-12-04 22:40:02 -05001442/*
James Smart6c8eea52010-04-06 14:49:53 -04001443 * Given a scsi cmnd, determine the BlockGuard opcodes to be used with it
1444 * @sc: The SCSI command to examine
1445 * @txopt: (out) BlockGuard operation for transmitted data
1446 * @rxopt: (out) BlockGuard operation for received data
1447 *
1448 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1449 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001450 */
1451static int
James Smart6c8eea52010-04-06 14:49:53 -04001452lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1453 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001454{
1455 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
James Smart6c8eea52010-04-06 14:49:53 -04001456 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001457
1458 if (guard_type == SHOST_DIX_GUARD_IP) {
1459 switch (scsi_get_prot_op(sc)) {
1460 case SCSI_PROT_READ_INSERT:
1461 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001462 *txop = BG_OP_IN_CSUM_OUT_NODIF;
1463 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smarte2a0a9d2008-12-04 22:40:02 -05001464 break;
1465
1466 case SCSI_PROT_READ_STRIP:
1467 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001468 *txop = BG_OP_IN_NODIF_OUT_CRC;
1469 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001470 break;
1471
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001472 case SCSI_PROT_READ_PASS:
1473 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001474 *txop = BG_OP_IN_CSUM_OUT_CRC;
1475 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smarte2a0a9d2008-12-04 22:40:02 -05001476 break;
1477
James Smarte2a0a9d2008-12-04 22:40:02 -05001478 case SCSI_PROT_NORMAL:
1479 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001480 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001481 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1482 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001483 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001484 break;
1485
1486 }
James Smart7c56b9f2011-07-22 18:36:25 -04001487 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001488 switch (scsi_get_prot_op(sc)) {
1489 case SCSI_PROT_READ_STRIP:
1490 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001491 *txop = BG_OP_IN_NODIF_OUT_CRC;
1492 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001493 break;
1494
1495 case SCSI_PROT_READ_PASS:
1496 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001497 *txop = BG_OP_IN_CRC_OUT_CRC;
1498 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001499 break;
1500
James Smarte2a0a9d2008-12-04 22:40:02 -05001501 case SCSI_PROT_READ_INSERT:
1502 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001503 *txop = BG_OP_IN_CRC_OUT_NODIF;
1504 *rxop = BG_OP_IN_NODIF_OUT_CRC;
1505 break;
1506
James Smarte2a0a9d2008-12-04 22:40:02 -05001507 case SCSI_PROT_NORMAL:
1508 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001509 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001510 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1511 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001512 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001513 break;
1514 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001515 }
1516
James Smart6c8eea52010-04-06 14:49:53 -04001517 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001518}
1519
James Smarte2a0a9d2008-12-04 22:40:02 -05001520/*
1521 * This function sets up buffer list for protection groups of
1522 * type LPFC_PG_TYPE_NO_DIF
1523 *
1524 * This is usually used when the HBA is instructed to generate
1525 * DIFs and insert them into data stream (or strip DIF from
1526 * incoming data stream)
1527 *
1528 * The buffer list consists of just one protection group described
1529 * below:
1530 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001531 * start of prot group --> | PDE_5 |
1532 * +-------------------------+
1533 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001534 * +-------------------------+
1535 * | Data BDE |
1536 * +-------------------------+
1537 * |more Data BDE's ... (opt)|
1538 * +-------------------------+
1539 *
1540 * @sc: pointer to scsi command we're working on
1541 * @bpl: pointer to buffer list for protection groups
1542 * @datacnt: number of segments of data that have been dma mapped
1543 *
1544 * Note: Data s/g buffers have been dma mapped
1545 */
1546static int
1547lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1548 struct ulp_bde64 *bpl, int datasegcnt)
1549{
1550 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001551 struct lpfc_pde5 *pde5 = NULL;
1552 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001553 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001554 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001555 int datadir = sc->sc_data_direction;
James Smarte2a0a9d2008-12-04 22:40:02 -05001556 uint32_t reftag;
James Smart7c56b9f2011-07-22 18:36:25 -04001557 unsigned blksize;
James Smart6c8eea52010-04-06 14:49:53 -04001558 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001559
James Smart6c8eea52010-04-06 14:49:53 -04001560 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1561 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001562 goto out;
1563
James Smart6c8eea52010-04-06 14:49:53 -04001564 /* extract some info from the scsi command for pde*/
James Smarte2a0a9d2008-12-04 22:40:02 -05001565 blksize = lpfc_cmd_blksize(sc);
James Smart7c56b9f2011-07-22 18:36:25 -04001566 reftag = scsi_get_lba(sc) & 0xffffffff;
James Smarte2a0a9d2008-12-04 22:40:02 -05001567
James Smartf9bb2da2011-10-10 21:34:11 -04001568#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1569 /* reftag is the only error we can inject here */
1570 lpfc_bg_err_inject(phba, sc, &reftag, 0, 0);
1571#endif
1572
James Smart6c8eea52010-04-06 14:49:53 -04001573 /* setup PDE5 with what we have */
1574 pde5 = (struct lpfc_pde5 *) bpl;
1575 memset(pde5, 0, sizeof(struct lpfc_pde5));
1576 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001577
James Smartbc739052010-08-04 16:11:18 -04001578 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001579 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001580 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001581
James Smart6c8eea52010-04-06 14:49:53 -04001582 /* advance bpl and increment bde count */
1583 num_bde++;
1584 bpl++;
1585 pde6 = (struct lpfc_pde6 *) bpl;
1586
1587 /* setup PDE6 with the rest of the info */
1588 memset(pde6, 0, sizeof(struct lpfc_pde6));
1589 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1590 bf_set(pde6_optx, pde6, txop);
1591 bf_set(pde6_oprx, pde6, rxop);
1592 if (datadir == DMA_FROM_DEVICE) {
1593 bf_set(pde6_ce, pde6, 1);
1594 bf_set(pde6_re, pde6, 1);
James Smart6c8eea52010-04-06 14:49:53 -04001595 }
1596 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001597 bf_set(pde6_ae, pde6, 0);
1598 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001599
James Smartbc739052010-08-04 16:11:18 -04001600 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001601 pde6->word0 = cpu_to_le32(pde6->word0);
1602 pde6->word1 = cpu_to_le32(pde6->word1);
1603 pde6->word2 = cpu_to_le32(pde6->word2);
1604
James Smart6c8eea52010-04-06 14:49:53 -04001605 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001606 num_bde++;
1607 bpl++;
1608
1609 /* assumption: caller has already run dma_map_sg on command data */
1610 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1611 physaddr = sg_dma_address(sgde);
1612 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1613 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1614 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1615 if (datadir == DMA_TO_DEVICE)
1616 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1617 else
1618 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1619 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1620 bpl++;
1621 num_bde++;
1622 }
1623
1624out:
1625 return num_bde;
1626}
1627
1628/*
1629 * This function sets up buffer list for protection groups of
1630 * type LPFC_PG_TYPE_DIF_BUF
1631 *
1632 * This is usually used when DIFs are in their own buffers,
1633 * separate from the data. The HBA can then by instructed
1634 * to place the DIFs in the outgoing stream. For read operations,
1635 * The HBA could extract the DIFs and place it in DIF buffers.
1636 *
1637 * The buffer list for this type consists of one or more of the
1638 * protection groups described below:
1639 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001640 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001641 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001642 * | PDE_6 |
1643 * +-------------------------+
1644 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05001645 * +-------------------------+
1646 * | Data BDE |
1647 * +-------------------------+
1648 * |more Data BDE's ... (opt)|
1649 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001650 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001651 * +-------------------------+
1652 * | ... |
1653 * +-------------------------+
1654 *
1655 * @sc: pointer to scsi command we're working on
1656 * @bpl: pointer to buffer list for protection groups
1657 * @datacnt: number of segments of data that have been dma mapped
1658 * @protcnt: number of segment of protection data that have been dma mapped
1659 *
1660 * Note: It is assumed that both data and protection s/g buffers have been
1661 * mapped for DMA
1662 */
1663static int
1664lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1665 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1666{
1667 struct scatterlist *sgde = NULL; /* s/g data entry */
1668 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04001669 struct lpfc_pde5 *pde5 = NULL;
1670 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05001671 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001672 dma_addr_t dataphysaddr, protphysaddr;
1673 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05001674 unsigned int split_offset;
1675 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05001676 unsigned int protgrp_blks, protgrp_bytes;
1677 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04001678 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001679 int datadir = sc->sc_data_direction;
1680 unsigned char pgdone = 0, alldone = 0;
1681 unsigned blksize;
1682 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04001683 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001684 int num_bde = 0;
1685
1686 sgpe = scsi_prot_sglist(sc);
1687 sgde = scsi_sglist(sc);
1688
1689 if (!sgpe || !sgde) {
1690 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1691 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
1692 sgpe, sgde);
1693 return 0;
1694 }
1695
James Smart6c8eea52010-04-06 14:49:53 -04001696 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1697 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001698 goto out;
1699
James Smart6c8eea52010-04-06 14:49:53 -04001700 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05001701 blksize = lpfc_cmd_blksize(sc);
James Smart7c56b9f2011-07-22 18:36:25 -04001702 reftag = scsi_get_lba(sc) & 0xffffffff;
James Smarte2a0a9d2008-12-04 22:40:02 -05001703
James Smartf9bb2da2011-10-10 21:34:11 -04001704#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1705 /* reftag / guard tag are the only errors we can inject here */
1706 lpfc_bg_err_inject(phba, sc, &reftag, 0, 0xDEAD);
1707#endif
1708
James Smarte2a0a9d2008-12-04 22:40:02 -05001709 split_offset = 0;
1710 do {
James Smart6c8eea52010-04-06 14:49:53 -04001711 /* setup PDE5 with what we have */
1712 pde5 = (struct lpfc_pde5 *) bpl;
1713 memset(pde5, 0, sizeof(struct lpfc_pde5));
1714 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001715
James Smartbc739052010-08-04 16:11:18 -04001716 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001717 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001718 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001719
James Smart6c8eea52010-04-06 14:49:53 -04001720 /* advance bpl and increment bde count */
1721 num_bde++;
1722 bpl++;
1723 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05001724
James Smart6c8eea52010-04-06 14:49:53 -04001725 /* setup PDE6 with the rest of the info */
1726 memset(pde6, 0, sizeof(struct lpfc_pde6));
1727 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1728 bf_set(pde6_optx, pde6, txop);
1729 bf_set(pde6_oprx, pde6, rxop);
1730 bf_set(pde6_ce, pde6, 1);
1731 bf_set(pde6_re, pde6, 1);
James Smart6c8eea52010-04-06 14:49:53 -04001732 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001733 bf_set(pde6_ae, pde6, 0);
1734 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001735
James Smartbc739052010-08-04 16:11:18 -04001736 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001737 pde6->word0 = cpu_to_le32(pde6->word0);
1738 pde6->word1 = cpu_to_le32(pde6->word1);
1739 pde6->word2 = cpu_to_le32(pde6->word2);
1740
James Smart6c8eea52010-04-06 14:49:53 -04001741 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001742 num_bde++;
1743 bpl++;
1744
1745 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05001746 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
1747 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05001748
James Smarte2a0a9d2008-12-04 22:40:02 -05001749 /* must be integer multiple of the DIF block length */
1750 BUG_ON(protgroup_len % 8);
1751
James Smart7f860592011-03-11 16:05:52 -05001752 pde7 = (struct lpfc_pde7 *) bpl;
1753 memset(pde7, 0, sizeof(struct lpfc_pde7));
1754 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
1755
James Smart7c56b9f2011-07-22 18:36:25 -04001756 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1757 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05001758
James Smarte2a0a9d2008-12-04 22:40:02 -05001759 protgrp_blks = protgroup_len / 8;
1760 protgrp_bytes = protgrp_blks * blksize;
1761
James Smart7f860592011-03-11 16:05:52 -05001762 /* check if this pde is crossing the 4K boundary; if so split */
1763 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
1764 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
1765 protgroup_offset += protgroup_remainder;
1766 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04001767 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05001768 } else {
1769 protgroup_offset = 0;
1770 curr_prot++;
1771 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001772
James Smarte2a0a9d2008-12-04 22:40:02 -05001773 num_bde++;
1774
1775 /* setup BDE's for data blocks associated with DIF data */
1776 pgdone = 0;
1777 subtotal = 0; /* total bytes processed for current prot grp */
1778 while (!pgdone) {
1779 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04001780 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1781 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001782 __func__);
1783 return 0;
1784 }
1785 bpl++;
1786 dataphysaddr = sg_dma_address(sgde) + split_offset;
1787 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1788 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1789
1790 remainder = sg_dma_len(sgde) - split_offset;
1791
1792 if ((subtotal + remainder) <= protgrp_bytes) {
1793 /* we can use this whole buffer */
1794 bpl->tus.f.bdeSize = remainder;
1795 split_offset = 0;
1796
1797 if ((subtotal + remainder) == protgrp_bytes)
1798 pgdone = 1;
1799 } else {
1800 /* must split this buffer with next prot grp */
1801 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1802 split_offset += bpl->tus.f.bdeSize;
1803 }
1804
1805 subtotal += bpl->tus.f.bdeSize;
1806
1807 if (datadir == DMA_TO_DEVICE)
1808 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1809 else
1810 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1811 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1812
1813 num_bde++;
1814 curr_data++;
1815
1816 if (split_offset)
1817 break;
1818
1819 /* Move to the next s/g segment if possible */
1820 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04001821
James Smarte2a0a9d2008-12-04 22:40:02 -05001822 }
1823
James Smart7f860592011-03-11 16:05:52 -05001824 if (protgroup_offset) {
1825 /* update the reference tag */
1826 reftag += protgrp_blks;
1827 bpl++;
1828 continue;
1829 }
1830
James Smarte2a0a9d2008-12-04 22:40:02 -05001831 /* are we done ? */
1832 if (curr_prot == protcnt) {
1833 alldone = 1;
1834 } else if (curr_prot < protcnt) {
1835 /* advance to next prot buffer */
1836 sgpe = sg_next(sgpe);
1837 bpl++;
1838
1839 /* update the reference tag */
1840 reftag += protgrp_blks;
1841 } else {
1842 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04001843 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1844 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05001845 }
1846
1847 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05001848out:
1849
James Smarte2a0a9d2008-12-04 22:40:02 -05001850 return num_bde;
1851}
James Smart7f860592011-03-11 16:05:52 -05001852
James Smarte2a0a9d2008-12-04 22:40:02 -05001853/*
1854 * Given a SCSI command that supports DIF, determine composition of protection
1855 * groups involved in setting up buffer lists
1856 *
1857 * Returns:
1858 * for DIF (for both read and write)
1859 * */
1860static int
1861lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
1862{
1863 int ret = LPFC_PG_TYPE_INVALID;
1864 unsigned char op = scsi_get_prot_op(sc);
1865
1866 switch (op) {
1867 case SCSI_PROT_READ_STRIP:
1868 case SCSI_PROT_WRITE_INSERT:
1869 ret = LPFC_PG_TYPE_NO_DIF;
1870 break;
1871 case SCSI_PROT_READ_INSERT:
1872 case SCSI_PROT_WRITE_STRIP:
1873 case SCSI_PROT_READ_PASS:
1874 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05001875 ret = LPFC_PG_TYPE_DIF_BUF;
1876 break;
1877 default:
1878 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1879 "9021 Unsupported protection op:%d\n", op);
1880 break;
1881 }
1882
1883 return ret;
1884}
1885
1886/*
1887 * This is the protection/DIF aware version of
1888 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
1889 * two functions eventually, but for now, it's here
1890 */
1891static int
1892lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba,
1893 struct lpfc_scsi_buf *lpfc_cmd)
1894{
1895 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1896 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1897 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1898 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1899 uint32_t num_bde = 0;
1900 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
1901 int prot_group_type = 0;
1902 int diflen, fcpdl;
1903 unsigned blksize;
1904
1905 /*
1906 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
1907 * fcp_rsp regions to the first data bde entry
1908 */
1909 bpl += 2;
1910 if (scsi_sg_count(scsi_cmnd)) {
1911 /*
1912 * The driver stores the segment count returned from pci_map_sg
1913 * because this a count of dma-mappings used to map the use_sg
1914 * pages. They are not guaranteed to be the same for those
1915 * architectures that implement an IOMMU.
1916 */
1917 datasegcnt = dma_map_sg(&phba->pcidev->dev,
1918 scsi_sglist(scsi_cmnd),
1919 scsi_sg_count(scsi_cmnd), datadir);
1920 if (unlikely(!datasegcnt))
1921 return 1;
1922
1923 lpfc_cmd->seg_cnt = datasegcnt;
1924 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001925 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1926 "9067 BLKGRD: %s: Too many sg segments"
1927 " from dma_map_sg. Config %d, seg_cnt"
1928 " %d\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001929 __func__, phba->cfg_sg_seg_cnt,
1930 lpfc_cmd->seg_cnt);
1931 scsi_dma_unmap(scsi_cmnd);
1932 return 1;
1933 }
1934
1935 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
1936
1937 switch (prot_group_type) {
1938 case LPFC_PG_TYPE_NO_DIF:
1939 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
1940 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05001941 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05001942 if (num_bde < 2)
1943 goto err;
1944 break;
1945 case LPFC_PG_TYPE_DIF_BUF:{
1946 /*
1947 * This type indicates that protection buffers are
1948 * passed to the driver, so that needs to be prepared
1949 * for DMA
1950 */
1951 protsegcnt = dma_map_sg(&phba->pcidev->dev,
1952 scsi_prot_sglist(scsi_cmnd),
1953 scsi_prot_sg_count(scsi_cmnd), datadir);
1954 if (unlikely(!protsegcnt)) {
1955 scsi_dma_unmap(scsi_cmnd);
1956 return 1;
1957 }
1958
1959 lpfc_cmd->prot_seg_cnt = protsegcnt;
1960 if (lpfc_cmd->prot_seg_cnt
1961 > phba->cfg_prot_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001962 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1963 "9068 BLKGRD: %s: Too many prot sg "
1964 "segments from dma_map_sg. Config %d,"
James Smarte2a0a9d2008-12-04 22:40:02 -05001965 "prot_seg_cnt %d\n", __func__,
1966 phba->cfg_prot_sg_seg_cnt,
1967 lpfc_cmd->prot_seg_cnt);
1968 dma_unmap_sg(&phba->pcidev->dev,
1969 scsi_prot_sglist(scsi_cmnd),
1970 scsi_prot_sg_count(scsi_cmnd),
1971 datadir);
1972 scsi_dma_unmap(scsi_cmnd);
1973 return 1;
1974 }
1975
1976 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
1977 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05001978 /* we should have 3 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05001979 if (num_bde < 3)
1980 goto err;
1981 break;
1982 }
1983 case LPFC_PG_TYPE_INVALID:
1984 default:
1985 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1986 "9022 Unexpected protection group %i\n",
1987 prot_group_type);
1988 return 1;
1989 }
1990 }
1991
1992 /*
1993 * Finish initializing those IOCB fields that are dependent on the
1994 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
1995 * reinitialized since all iocb memory resources are used many times
1996 * for transmit, receive, and continuation bpl's.
1997 */
1998 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
1999 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2000 iocb_cmd->ulpBdeCount = 1;
2001 iocb_cmd->ulpLe = 1;
2002
2003 fcpdl = scsi_bufflen(scsi_cmnd);
2004
2005 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
2006 /*
2007 * We are in DIF Type 1 mode
2008 * Every data block has a 8 byte DIF (trailer)
2009 * attached to it. Must ajust FCP data length
2010 */
2011 blksize = lpfc_cmd_blksize(scsi_cmnd);
2012 diflen = (fcpdl / blksize) * 8;
2013 fcpdl += diflen;
2014 }
2015 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2016
2017 /*
2018 * Due to difference in data length between DIF/non-DIF paths,
2019 * we need to set word 4 of IOCB here
2020 */
2021 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2022
2023 return 0;
2024err:
2025 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2026 "9023 Could not setup all needed BDE's"
2027 "prot_group_type=%d, num_bde=%d\n",
2028 prot_group_type, num_bde);
2029 return 1;
2030}
2031
2032/*
2033 * This function checks for BlockGuard errors detected by
2034 * the HBA. In case of errors, the ASC/ASCQ fields in the
2035 * sense buffer will be set accordingly, paired with
2036 * ILLEGAL_REQUEST to signal to the kernel that the HBA
2037 * detected corruption.
2038 *
2039 * Returns:
2040 * 0 - No error found
2041 * 1 - BlockGuard error found
2042 * -1 - Internal error (bad profile, ...etc)
2043 */
2044static int
2045lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
2046 struct lpfc_iocbq *pIocbOut)
2047{
2048 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2049 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
2050 int ret = 0;
2051 uint32_t bghm = bgf->bghm;
2052 uint32_t bgstat = bgf->bgstat;
2053 uint64_t failing_sector = 0;
2054
James Smart6a9c52c2009-10-02 15:16:51 -04002055 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
2056 " 0x%x lba 0x%llx blk cnt 0x%x "
James Smarte2a0a9d2008-12-04 22:40:02 -05002057 "bgstat=0x%x bghm=0x%x\n",
James Smart87b5c322008-12-16 10:34:09 -05002058 cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
Tejun Heo83096eb2009-05-07 22:24:39 +09002059 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002060
2061 spin_lock(&_dump_buf_lock);
2062 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04002063 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
2064 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002065 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04002066 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002067
2068 /* If we have a prot sgl, save the DIF buffer */
2069 if (lpfc_prot_group_type(phba, cmd) ==
2070 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04002071 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
2072 "Saving DIF for %u blocks to debugfs\n",
2073 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
2074 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002075 }
2076
2077 _dump_buf_done = 1;
2078 }
2079 spin_unlock(&_dump_buf_lock);
2080
2081 if (lpfc_bgs_get_invalid_prof(bgstat)) {
2082 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002083 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
2084 " BlockGuard profile. bgstat:0x%x\n",
2085 bgstat);
James Smarte2a0a9d2008-12-04 22:40:02 -05002086 ret = (-1);
2087 goto out;
2088 }
2089
2090 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
2091 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002092 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
2093 "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002094 bgstat);
2095 ret = (-1);
2096 goto out;
2097 }
2098
2099 if (lpfc_bgs_get_guard_err(bgstat)) {
2100 ret = 1;
2101
2102 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2103 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002104 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002105 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2106 phba->bg_guard_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002107 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2108 "9055 BLKGRD: guard_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002109 }
2110
2111 if (lpfc_bgs_get_reftag_err(bgstat)) {
2112 ret = 1;
2113
2114 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2115 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002116 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002117 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2118
2119 phba->bg_reftag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002120 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2121 "9056 BLKGRD: ref_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002122 }
2123
2124 if (lpfc_bgs_get_apptag_err(bgstat)) {
2125 ret = 1;
2126
2127 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2128 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002129 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002130 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2131
2132 phba->bg_apptag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002133 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2134 "9061 BLKGRD: app_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002135 }
2136
2137 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
2138 /*
2139 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04002140 * field, and put the failing LBA in it.
2141 * This code assumes there was also a guard/app/ref tag error
2142 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05002143 */
James Smart7c56b9f2011-07-22 18:36:25 -04002144 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
2145 cmd->sense_buffer[8] = 0; /* Information descriptor type */
2146 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
2147 cmd->sense_buffer[10] = 0x80; /* Validity bit */
David Howells2344b5b2009-04-14 17:08:34 +01002148 bghm /= cmd->device->sector_size;
James Smarte2a0a9d2008-12-04 22:40:02 -05002149
2150 failing_sector = scsi_get_lba(cmd);
2151 failing_sector += bghm;
2152
James Smart7c56b9f2011-07-22 18:36:25 -04002153 /* Descriptor Information */
2154 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05002155 }
2156
2157 if (!ret) {
2158 /* No error was reported - problem in FW? */
2159 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002160 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2161 "9057 BLKGRD: no errors reported!\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002162 }
2163
2164out:
2165 return ret;
2166}
2167
James Smartea2151b2008-09-07 11:52:10 -04002168/**
James Smartda0436e2009-05-22 14:51:39 -04002169 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
2170 * @phba: The Hba for which this call is being executed.
2171 * @lpfc_cmd: The scsi buffer which is going to be mapped.
2172 *
2173 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
2174 * field of @lpfc_cmd for device with SLI-4 interface spec.
2175 *
2176 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04002177 * 1 - Error
2178 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04002179 **/
2180static int
2181lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2182{
2183 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2184 struct scatterlist *sgel = NULL;
2185 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2186 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
James Smartfedd3b72011-02-16 12:39:24 -05002187 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04002188 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2189 dma_addr_t physaddr;
2190 uint32_t num_bde = 0;
2191 uint32_t dma_len;
2192 uint32_t dma_offset = 0;
2193 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05002194 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04002195
2196 /*
2197 * There are three possibilities here - use scatter-gather segment, use
2198 * the single mapping, or neither. Start the lpfc command prep by
2199 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2200 * data bde entry.
2201 */
2202 if (scsi_sg_count(scsi_cmnd)) {
2203 /*
2204 * The driver stores the segment count returned from pci_map_sg
2205 * because this a count of dma-mappings used to map the use_sg
2206 * pages. They are not guaranteed to be the same for those
2207 * architectures that implement an IOMMU.
2208 */
2209
2210 nseg = scsi_dma_map(scsi_cmnd);
2211 if (unlikely(!nseg))
2212 return 1;
2213 sgl += 1;
2214 /* clear the last flag in the fcp_rsp map entry */
2215 sgl->word2 = le32_to_cpu(sgl->word2);
2216 bf_set(lpfc_sli4_sge_last, sgl, 0);
2217 sgl->word2 = cpu_to_le32(sgl->word2);
2218 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05002219 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04002220 lpfc_cmd->seg_cnt = nseg;
2221 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04002222 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
2223 " %s: Too many sg segments from "
2224 "dma_map_sg. Config %d, seg_cnt %d\n",
2225 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04002226 lpfc_cmd->seg_cnt);
2227 scsi_dma_unmap(scsi_cmnd);
2228 return 1;
2229 }
2230
2231 /*
2232 * The driver established a maximum scatter-gather segment count
2233 * during probe that limits the number of sg elements in any
2234 * single scsi command. Just run through the seg_cnt and format
2235 * the sge's.
2236 * When using SLI-3 the driver will try to fit all the BDEs into
2237 * the IOCB. If it can't then the BDEs get added to a BPL as it
2238 * does for SLI-2 mode.
2239 */
2240 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
2241 physaddr = sg_dma_address(sgel);
2242 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04002243 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2244 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04002245 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04002246 if ((num_bde + 1) == nseg)
2247 bf_set(lpfc_sli4_sge_last, sgl, 1);
2248 else
2249 bf_set(lpfc_sli4_sge_last, sgl, 0);
2250 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04002251 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04002252 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05002253 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04002254 dma_offset += dma_len;
2255 sgl++;
2256 }
James Smartfedd3b72011-02-16 12:39:24 -05002257 /* setup the performance hint (first data BDE) if enabled */
2258 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
2259 bde = (struct ulp_bde64 *)
2260 &(iocb_cmd->unsli3.sli3Words[5]);
2261 bde->addrLow = first_data_sgl->addr_lo;
2262 bde->addrHigh = first_data_sgl->addr_hi;
2263 bde->tus.f.bdeSize =
2264 le32_to_cpu(first_data_sgl->sge_len);
2265 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2266 bde->tus.w = cpu_to_le32(bde->tus.w);
2267 }
James Smartda0436e2009-05-22 14:51:39 -04002268 } else {
2269 sgl += 1;
2270 /* clear the last flag in the fcp_rsp map entry */
2271 sgl->word2 = le32_to_cpu(sgl->word2);
2272 bf_set(lpfc_sli4_sge_last, sgl, 1);
2273 sgl->word2 = cpu_to_le32(sgl->word2);
2274 }
2275
2276 /*
2277 * Finish initializing those IOCB fields that are dependent on the
2278 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
2279 * explicitly reinitialized.
2280 * all iocb memory resources are reused.
2281 */
2282 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
2283
2284 /*
2285 * Due to difference in data length between DIF/non-DIF paths,
2286 * we need to set word 4 of IOCB here
2287 */
2288 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
2289 return 0;
2290}
2291
2292/**
James Smart3772a992009-05-22 14:50:54 -04002293 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
2294 * @phba: The Hba for which this call is being executed.
2295 * @lpfc_cmd: The scsi buffer which is going to be mapped.
2296 *
2297 * This routine wraps the actual DMA mapping function pointer from the
2298 * lpfc_hba struct.
2299 *
2300 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04002301 * 1 - Error
2302 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04002303 **/
2304static inline int
2305lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2306{
2307 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
2308}
2309
2310/**
James Smart3621a712009-04-06 18:47:14 -04002311 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04002312 * @phba: Pointer to hba context object.
2313 * @vport: Pointer to vport object.
2314 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
2315 * @rsp_iocb: Pointer to response iocb object which reported error.
2316 *
2317 * This function posts an event when there is a SCSI command reporting
2318 * error from the scsi device.
2319 **/
2320static void
2321lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
2322 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
2323 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2324 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
2325 uint32_t resp_info = fcprsp->rspStatus2;
2326 uint32_t scsi_status = fcprsp->rspStatus3;
2327 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
2328 struct lpfc_fast_path_event *fast_path_evt = NULL;
2329 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
2330 unsigned long flags;
2331
James Smart5989b8d2010-10-22 11:06:56 -04002332 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2333 return;
2334
James Smartea2151b2008-09-07 11:52:10 -04002335 /* If there is queuefull or busy condition send a scsi event */
2336 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
2337 (cmnd->result == SAM_STAT_BUSY)) {
2338 fast_path_evt = lpfc_alloc_fast_evt(phba);
2339 if (!fast_path_evt)
2340 return;
2341 fast_path_evt->un.scsi_evt.event_type =
2342 FC_REG_SCSI_EVENT;
2343 fast_path_evt->un.scsi_evt.subcategory =
2344 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
2345 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
2346 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
2347 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
2348 &pnode->nlp_portname, sizeof(struct lpfc_name));
2349 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
2350 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2351 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
2352 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
2353 fast_path_evt = lpfc_alloc_fast_evt(phba);
2354 if (!fast_path_evt)
2355 return;
2356 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
2357 FC_REG_SCSI_EVENT;
2358 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
2359 LPFC_EVENT_CHECK_COND;
2360 fast_path_evt->un.check_cond_evt.scsi_event.lun =
2361 cmnd->device->lun;
2362 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
2363 &pnode->nlp_portname, sizeof(struct lpfc_name));
2364 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
2365 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2366 fast_path_evt->un.check_cond_evt.sense_key =
2367 cmnd->sense_buffer[2] & 0xf;
2368 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
2369 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
2370 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2371 fcpi_parm &&
2372 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
2373 ((scsi_status == SAM_STAT_GOOD) &&
2374 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
2375 /*
2376 * If status is good or resid does not match with fcp_param and
2377 * there is valid fcpi_parm, then there is a read_check error
2378 */
2379 fast_path_evt = lpfc_alloc_fast_evt(phba);
2380 if (!fast_path_evt)
2381 return;
2382 fast_path_evt->un.read_check_error.header.event_type =
2383 FC_REG_FABRIC_EVENT;
2384 fast_path_evt->un.read_check_error.header.subcategory =
2385 LPFC_EVENT_FCPRDCHKERR;
2386 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
2387 &pnode->nlp_portname, sizeof(struct lpfc_name));
2388 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
2389 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2390 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
2391 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
2392 fast_path_evt->un.read_check_error.fcpiparam =
2393 fcpi_parm;
2394 } else
2395 return;
2396
2397 fast_path_evt->vport = vport;
2398 spin_lock_irqsave(&phba->hbalock, flags);
2399 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
2400 spin_unlock_irqrestore(&phba->hbalock, flags);
2401 lpfc_worker_wake_up(phba);
2402 return;
2403}
James Smart9bad7672008-12-04 22:39:02 -05002404
2405/**
James Smartf1126682009-06-10 17:22:44 -04002406 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04002407 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05002408 * @psb: The scsi buffer which is going to be un-mapped.
2409 *
2410 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04002411 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002412 **/
dea31012005-04-17 16:05:31 -05002413static void
James Smartf1126682009-06-10 17:22:44 -04002414lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04002415{
2416 /*
2417 * There are only two special cases to consider. (1) the scsi command
2418 * requested scatter-gather usage or (2) the scsi command allocated
2419 * a request buffer, but did not request use_sg. There is a third
2420 * case, but it does not require resource deallocation.
2421 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002422 if (psb->seg_cnt > 0)
2423 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002424 if (psb->prot_seg_cnt > 0)
2425 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
2426 scsi_prot_sg_count(psb->pCmd),
2427 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04002428}
2429
James Smart9bad7672008-12-04 22:39:02 -05002430/**
James Smart3621a712009-04-06 18:47:14 -04002431 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05002432 * @vport: The virtual port for which this call is being executed.
2433 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2434 * @rsp_iocb: The response IOCB which contains FCP error.
2435 *
2436 * This routine is called to process response IOCB with status field
2437 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
2438 * based upon SCSI and FCP error.
2439 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04002440static void
James Smart2e0fef82007-06-17 19:56:36 -05002441lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2442 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05002443{
2444 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2445 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
2446 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04002447 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05002448 uint32_t resp_info = fcprsp->rspStatus2;
2449 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05002450 uint32_t *lp;
dea31012005-04-17 16:05:31 -05002451 uint32_t host_status = DID_OK;
2452 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05002453 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05002454
James Smartea2151b2008-09-07 11:52:10 -04002455
dea31012005-04-17 16:05:31 -05002456 /*
2457 * If this is a task management command, there is no
2458 * scsi packet associated with this lpfc_cmd. The driver
2459 * consumes it.
2460 */
2461 if (fcpcmd->fcpCntl2) {
2462 scsi_status = 0;
2463 goto out;
2464 }
2465
James Smart6a9c52c2009-10-02 15:16:51 -04002466 if (resp_info & RSP_LEN_VALID) {
2467 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05002468 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04002469 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2470 "2719 Invalid response length: "
2471 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
2472 cmnd->device->id,
2473 cmnd->device->lun, cmnd->cmnd[0],
2474 rsplen);
2475 host_status = DID_ERROR;
2476 goto out;
2477 }
James Smarte40a02c2010-02-26 14:13:54 -05002478 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
2479 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2480 "2757 Protocol failure detected during "
2481 "processing of FCP I/O op: "
2482 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
2483 cmnd->device->id,
2484 cmnd->device->lun, cmnd->cmnd[0],
2485 fcprsp->rspInfo3);
2486 host_status = DID_ERROR;
2487 goto out;
2488 }
James Smart6a9c52c2009-10-02 15:16:51 -04002489 }
2490
James Smartc7743952006-12-02 13:34:42 -05002491 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
2492 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
2493 if (snslen > SCSI_SENSE_BUFFERSIZE)
2494 snslen = SCSI_SENSE_BUFFERSIZE;
2495
2496 if (resp_info & RSP_LEN_VALID)
2497 rsplen = be32_to_cpu(fcprsp->rspRspLen);
2498 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
2499 }
2500 lp = (uint32_t *)cmnd->sense_buffer;
2501
James Smart73d91e52011-10-10 21:32:10 -04002502 if (!scsi_status && (resp_info & RESID_UNDER) &&
2503 vport->cfg_log_verbose & LOG_FCP_UNDER)
2504 logit = LOG_FCP_UNDER;
James Smartc7743952006-12-02 13:34:42 -05002505
James Smarte8b62012007-08-02 11:10:09 -04002506 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05002507 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04002508 "Data: x%x x%x x%x x%x x%x\n",
2509 cmnd->cmnd[0], scsi_status,
2510 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
2511 be32_to_cpu(fcprsp->rspResId),
2512 be32_to_cpu(fcprsp->rspSnsLen),
2513 be32_to_cpu(fcprsp->rspRspLen),
2514 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05002515
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002516 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05002517 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002518 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05002519
James Smart73d91e52011-10-10 21:32:10 -04002520 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smarte2a0a9d2008-12-04 22:40:02 -05002521 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04002522 "residual %d Data: x%x x%x x%x\n",
2523 be32_to_cpu(fcpcmd->fcpDl),
2524 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
2525 cmnd->underflow);
dea31012005-04-17 16:05:31 -05002526
2527 /*
James Smart7054a602007-04-25 09:52:34 -04002528 * If there is an under run check if under run reported by
2529 * storage array is same as the under run reported by HBA.
2530 * If this is not same, there is a dropped frame.
2531 */
2532 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2533 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002534 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04002535 lpfc_printf_vlog(vport, KERN_WARNING,
2536 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05002537 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04002538 "and Underrun Data: x%x x%x x%x x%x\n",
2539 be32_to_cpu(fcpcmd->fcpDl),
2540 scsi_get_resid(cmnd), fcpi_parm,
2541 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002542 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04002543 host_status = DID_ERROR;
2544 }
2545 /*
dea31012005-04-17 16:05:31 -05002546 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002547 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05002548 * is not present, make sure the actual amount transferred is at
2549 * least the underflow value or fail.
2550 */
2551 if (!(resp_info & SNS_LEN_VALID) &&
2552 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002553 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
2554 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04002555 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002556 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04002557 "underrun converted to error "
2558 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04002559 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04002560 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05002561 host_status = DID_ERROR;
2562 }
2563 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04002564 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002565 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04002566 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04002567 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05002568 host_status = DID_ERROR;
2569
2570 /*
2571 * Check SLI validation that all the transfer was actually done
2572 * (fcpi_parm should be zero). Apply check only to reads.
2573 */
James Smarteee88772010-09-29 11:19:08 -04002574 } else if (fcpi_parm && (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -04002575 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05002576 "9029 FCP Read Check Error Data: "
James Smarteee88772010-09-29 11:19:08 -04002577 "x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002578 be32_to_cpu(fcpcmd->fcpDl),
2579 be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04002580 fcpi_parm, cmnd->cmnd[0], scsi_status);
2581 switch (scsi_status) {
2582 case SAM_STAT_GOOD:
2583 case SAM_STAT_CHECK_CONDITION:
2584 /* Fabric dropped a data frame. Fail any successful
2585 * command in which we detected dropped frames.
2586 * A status of good or some check conditions could
2587 * be considered a successful command.
2588 */
2589 host_status = DID_ERROR;
2590 break;
2591 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002592 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05002593 }
2594
2595 out:
2596 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04002597 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05002598}
2599
James Smart9bad7672008-12-04 22:39:02 -05002600/**
James Smart3621a712009-04-06 18:47:14 -04002601 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05002602 * @phba: The Hba for which this call is being executed.
2603 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04002604 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05002605 *
2606 * This routine assigns scsi command result by looking into response IOCB
2607 * status field appropriately. This routine handles QUEUE FULL condition as
2608 * well by ramping down device queue depth.
2609 **/
dea31012005-04-17 16:05:31 -05002610static void
2611lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
2612 struct lpfc_iocbq *pIocbOut)
2613{
2614 struct lpfc_scsi_buf *lpfc_cmd =
2615 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002616 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05002617 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
2618 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04002619 struct scsi_cmnd *cmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002620 int result;
James Smarta257bf92009-04-06 18:48:10 -04002621 struct scsi_device *tmp_sdev;
James Smart5ffc2662009-11-18 15:39:44 -05002622 int depth;
James Smartfa61a542008-01-11 01:52:42 -05002623 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04002624 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04002625 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04002626 uint32_t queue_depth, scsi_id;
James Smart73d91e52011-10-10 21:32:10 -04002627 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05002628
James Smart75baf692010-06-08 18:31:21 -04002629 /* Sanity check on return of outstanding command */
2630 if (!(lpfc_cmd->pCmd))
2631 return;
2632 cmd = lpfc_cmd->pCmd;
2633 shost = cmd->device->host;
2634
dea31012005-04-17 16:05:31 -05002635 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
2636 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05002637 /* pick up SLI4 exhange busy status from HBA */
2638 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
2639
James Smart109f6ed2008-12-04 22:39:08 -05002640 if (pnode && NLP_CHK_NODE_ACT(pnode))
2641 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05002642
2643 if (lpfc_cmd->status) {
2644 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
2645 (lpfc_cmd->result & IOERR_DRVR_MASK))
2646 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
2647 else if (lpfc_cmd->status >= IOSTAT_CNT)
2648 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smart73d91e52011-10-10 21:32:10 -04002649 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR
2650 && !lpfc_cmd->fcp_rsp->rspStatus3
2651 && (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER)
2652 && !(phba->cfg_log_verbose & LOG_FCP_UNDER))
2653 logit = 0;
2654 else
2655 logit = LOG_FCP | LOG_FCP_UNDER;
2656 lpfc_printf_vlog(vport, KERN_WARNING, logit,
2657 "9030 FCP cmd x%x failed <%d/%d> "
2658 "status: x%x result: x%x Data: x%x x%x\n",
2659 cmd->cmnd[0],
2660 cmd->device ? cmd->device->id : 0xffff,
2661 cmd->device ? cmd->device->lun : 0xffff,
2662 lpfc_cmd->status, lpfc_cmd->result,
2663 pIocbOut->iocb.ulpContext,
2664 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05002665
2666 switch (lpfc_cmd->status) {
2667 case IOSTAT_FCP_RSP_ERROR:
2668 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05002669 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05002670 break;
2671 case IOSTAT_NPORT_BSY:
2672 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04002673 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04002674 fast_path_evt = lpfc_alloc_fast_evt(phba);
2675 if (!fast_path_evt)
2676 break;
2677 fast_path_evt->un.fabric_evt.event_type =
2678 FC_REG_FABRIC_EVENT;
2679 fast_path_evt->un.fabric_evt.subcategory =
2680 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
2681 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
2682 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2683 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
2684 &pnode->nlp_portname,
2685 sizeof(struct lpfc_name));
2686 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
2687 &pnode->nlp_nodename,
2688 sizeof(struct lpfc_name));
2689 }
2690 fast_path_evt->vport = vport;
2691 fast_path_evt->work_evt.evt =
2692 LPFC_EVT_FASTPATH_MGMT_EVT;
2693 spin_lock_irqsave(&phba->hbalock, flags);
2694 list_add_tail(&fast_path_evt->work_evt.evt_listp,
2695 &phba->work_list);
2696 spin_unlock_irqrestore(&phba->hbalock, flags);
2697 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05002698 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002699 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05002700 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05002701 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
2702 lpfc_cmd->result ==
2703 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
2704 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
2705 lpfc_cmd->result ==
2706 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
2707 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
2708 break;
2709 }
James Smartd7c255b2008-08-24 21:50:00 -04002710 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05002711 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04002712 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
2713 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
James Smart92d7f7b2007-06-17 19:56:38 -05002714 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04002715 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05002716 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002717 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
2718 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
2719 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
2720 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
2721 /*
2722 * This is a response for a BG enabled
2723 * cmd. Parse BG error
2724 */
2725 lpfc_parse_bg_err(phba, lpfc_cmd,
2726 pIocbOut);
2727 break;
2728 } else {
2729 lpfc_printf_vlog(vport, KERN_WARNING,
2730 LOG_BG,
2731 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04002732 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002733 }
2734 }
James Smart1151e3e2011-02-16 12:39:35 -05002735 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
2736 && (phba->sli_rev == LPFC_SLI_REV4)
2737 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
2738 /* This IO was aborted by the target, we don't
2739 * know the rxid and because we did not send the
2740 * ABTS we cannot generate and RRQ.
2741 */
2742 lpfc_set_rrq_active(phba, pnode,
2743 lpfc_cmd->cur_iocbq.sli4_xritag,
2744 0, 0);
2745 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002746 /* else: fall through */
dea31012005-04-17 16:05:31 -05002747 default:
2748 cmd->result = ScsiResult(DID_ERROR, 0);
2749 break;
2750 }
2751
James Smart58da1ff2008-04-07 10:15:56 -04002752 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002753 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04002754 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
2755 SAM_STAT_BUSY);
James Smartab56dc22011-02-16 12:39:57 -05002756 } else
dea31012005-04-17 16:05:31 -05002757 cmd->result = ScsiResult(DID_OK, 0);
dea31012005-04-17 16:05:31 -05002758
2759 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
2760 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
2761
James Smarte8b62012007-08-02 11:10:09 -04002762 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2763 "0710 Iodone <%d/%d> cmd %p, error "
2764 "x%x SNS x%x x%x Data: x%x x%x\n",
2765 cmd->device->id, cmd->device->lun, cmd,
2766 cmd->result, *lp, *(lp + 3), cmd->retries,
2767 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05002768 }
2769
James Smartea2151b2008-09-07 11:52:10 -04002770 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002771 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04002772 if (vport->cfg_max_scsicmpl_time &&
2773 time_after(jiffies, lpfc_cmd->start_time +
2774 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04002775 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002776 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2777 if (pnode->cmd_qdepth >
2778 atomic_read(&pnode->cmd_pending) &&
2779 (atomic_read(&pnode->cmd_pending) >
2780 LPFC_MIN_TGT_QDEPTH) &&
2781 ((cmd->cmnd[0] == READ_10) ||
2782 (cmd->cmnd[0] == WRITE_10)))
2783 pnode->cmd_qdepth =
2784 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04002785
James Smart109f6ed2008-12-04 22:39:08 -05002786 pnode->last_change_time = jiffies;
2787 }
James Smarta257bf92009-04-06 18:48:10 -04002788 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002789 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smart7dc517d2010-07-14 15:32:10 -04002790 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
James Smart977b5a02008-09-07 11:52:04 -04002791 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05002792 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04002793 spin_lock_irqsave(shost->host_lock, flags);
James Smart7dc517d2010-07-14 15:32:10 -04002794 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
2795 / 100;
2796 depth = depth ? depth : 1;
2797 pnode->cmd_qdepth += depth;
2798 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
2799 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05002800 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04002801 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002802 }
James Smart977b5a02008-09-07 11:52:04 -04002803 }
2804
James Smart1dcb58e2007-04-25 09:51:30 -04002805 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04002806
2807 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
2808 queue_depth = cmd->device->queue_depth;
2809 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05002810 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002811
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002812 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smartfa61a542008-01-11 01:52:42 -05002813 /*
2814 * If there is a thread waiting for command completion
2815 * wake up the thread.
2816 */
James Smarta257bf92009-04-06 18:48:10 -04002817 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04002818 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05002819 if (lpfc_cmd->waitq)
2820 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04002821 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002822 lpfc_release_scsi_buf(phba, lpfc_cmd);
2823 return;
2824 }
2825
James Smart92d7f7b2007-06-17 19:56:38 -05002826 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04002827 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05002828
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002829 /*
2830 * Check for queue full. If the lun is reporting queue full, then
2831 * back off the lun queue depth to prevent target overloads.
2832 */
James Smart58da1ff2008-04-07 10:15:56 -04002833 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
2834 NLP_CHK_NODE_ACT(pnode)) {
James Smarta257bf92009-04-06 18:48:10 -04002835 shost_for_each_device(tmp_sdev, shost) {
2836 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002837 continue;
2838 depth = scsi_track_queue_full(tmp_sdev,
James Smart5ffc2662009-11-18 15:39:44 -05002839 tmp_sdev->queue_depth-1);
2840 if (depth <= 0)
2841 continue;
James Smarte8b62012007-08-02 11:10:09 -04002842 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2843 "0711 detected queue full - lun queue "
2844 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04002845 lpfc_send_sdev_queuedepth_change_event(phba, vport,
James Smart5ffc2662009-11-18 15:39:44 -05002846 pnode,
2847 tmp_sdev->lun,
2848 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002849 }
2850 }
2851
James Smartfa61a542008-01-11 01:52:42 -05002852 /*
2853 * If there is a thread waiting for command completion
2854 * wake up the thread.
2855 */
James Smarta257bf92009-04-06 18:48:10 -04002856 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04002857 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05002858 if (lpfc_cmd->waitq)
2859 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04002860 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05002861
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002862 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05002863}
2864
James Smart34b02dc2008-08-24 21:49:55 -04002865/**
James Smart3621a712009-04-06 18:47:14 -04002866 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04002867 * @data: A pointer to the immediate command data portion of the IOCB.
2868 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
2869 *
2870 * The routine copies the entire FCP command from @fcp_cmnd to @data while
2871 * byte swapping the data to big endian format for transmission on the wire.
2872 **/
2873static void
2874lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
2875{
2876 int i, j;
2877 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
2878 i += sizeof(uint32_t), j++) {
2879 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
2880 }
2881}
2882
James Smart9bad7672008-12-04 22:39:02 -05002883/**
James Smartf1126682009-06-10 17:22:44 -04002884 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05002885 * @vport: The virtual port for which this call is being executed.
2886 * @lpfc_cmd: The scsi command which needs to send.
2887 * @pnode: Pointer to lpfc_nodelist.
2888 *
2889 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04002890 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002891 **/
dea31012005-04-17 16:05:31 -05002892static void
James Smartf1126682009-06-10 17:22:44 -04002893lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05002894 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05002895{
James Smart2e0fef82007-06-17 19:56:36 -05002896 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002897 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2898 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2899 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2900 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
2901 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04002902 char tag[2];
dea31012005-04-17 16:05:31 -05002903
James Smart58da1ff2008-04-07 10:15:56 -04002904 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2905 return;
2906
dea31012005-04-17 16:05:31 -05002907 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04002908 /* clear task management bits */
2909 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05002910
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04002911 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
2912 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05002913
2914 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
2915
James Smart7e2b19f2007-10-29 11:00:39 -04002916 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
2917 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05002918 case HEAD_OF_QUEUE_TAG:
2919 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
2920 break;
2921 case ORDERED_QUEUE_TAG:
2922 fcp_cmnd->fcpCntl1 = ORDERED_Q;
2923 break;
2924 default:
2925 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
2926 break;
2927 }
2928 } else
2929 fcp_cmnd->fcpCntl1 = 0;
2930
2931 /*
2932 * There are three possibilities here - use scatter-gather segment, use
2933 * the single mapping, or neither. Start the lpfc command prep by
2934 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2935 * data bde entry.
2936 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002937 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05002938 if (datadir == DMA_TO_DEVICE) {
2939 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart3772a992009-05-22 14:50:54 -04002940 if (phba->sli_rev < LPFC_SLI_REV4) {
2941 iocb_cmd->un.fcpi.fcpi_parm = 0;
2942 iocb_cmd->ulpPU = 0;
2943 } else
2944 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05002945 fcp_cmnd->fcpCntl3 = WRITE_DATA;
2946 phba->fc4OutputRequests++;
2947 } else {
2948 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
2949 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05002950 fcp_cmnd->fcpCntl3 = READ_DATA;
2951 phba->fc4InputRequests++;
2952 }
2953 } else {
2954 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
2955 iocb_cmd->un.fcpi.fcpi_parm = 0;
2956 iocb_cmd->ulpPU = 0;
2957 fcp_cmnd->fcpCntl3 = 0;
2958 phba->fc4ControlRequests++;
2959 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002960 if (phba->sli_rev == 3 &&
2961 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04002962 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05002963 /*
2964 * Finish initializing those IOCB fields that are independent
2965 * of the scsi_cmnd request_buffer
2966 */
2967 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04002968 if (phba->sli_rev == LPFC_SLI_REV4)
2969 piocbq->iocb.ulpContext =
2970 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05002971 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
2972 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05002973 else
2974 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05002975
2976 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
2977 piocbq->context1 = lpfc_cmd;
2978 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
2979 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05002980 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05002981}
2982
James Smart9bad7672008-12-04 22:39:02 -05002983/**
James Smart6d368e52011-05-24 11:44:12 -04002984 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05002985 * @vport: The virtual port for which this call is being executed.
2986 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2987 * @lun: Logical unit number.
2988 * @task_mgmt_cmd: SCSI task management command.
2989 *
James Smart3772a992009-05-22 14:50:54 -04002990 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
2991 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002992 *
2993 * Return codes:
2994 * 0 - Error
2995 * 1 - Success
2996 **/
dea31012005-04-17 16:05:31 -05002997static int
James Smartf1126682009-06-10 17:22:44 -04002998lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05002999 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04003000 unsigned int lun,
dea31012005-04-17 16:05:31 -05003001 uint8_t task_mgmt_cmd)
3002{
dea31012005-04-17 16:05:31 -05003003 struct lpfc_iocbq *piocbq;
3004 IOCB_t *piocb;
3005 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04003006 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05003007 struct lpfc_nodelist *ndlp = rdata->pnode;
3008
James Smart58da1ff2008-04-07 10:15:56 -04003009 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
3010 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05003011 return 0;
dea31012005-04-17 16:05:31 -05003012
dea31012005-04-17 16:05:31 -05003013 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003014 piocbq->vport = vport;
3015
dea31012005-04-17 16:05:31 -05003016 piocb = &piocbq->iocb;
3017
3018 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04003019 /* Clear out any old data in the FCP command area */
3020 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
3021 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05003022 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05003023 if (vport->phba->sli_rev == 3 &&
3024 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04003025 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05003026 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05003027 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04003028 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
3029 piocb->ulpContext =
3030 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
3031 }
dea31012005-04-17 16:05:31 -05003032 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
3033 piocb->ulpFCP2Rcvy = 1;
3034 }
3035 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
3036
3037 /* ulpTimeout is only one byte */
3038 if (lpfc_cmd->timeout > 0xff) {
3039 /*
3040 * Do not timeout the command at the firmware level.
3041 * The driver will provide the timeout mechanism.
3042 */
3043 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04003044 } else
dea31012005-04-17 16:05:31 -05003045 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04003046
3047 if (vport->phba->sli_rev == LPFC_SLI_REV4)
3048 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05003049
James Smart2e0fef82007-06-17 19:56:36 -05003050 return 1;
dea31012005-04-17 16:05:31 -05003051}
3052
James Smart9bad7672008-12-04 22:39:02 -05003053/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003054 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04003055 * @phba: The hba struct for which this call is being executed.
3056 * @dev_grp: The HBA PCI-Device group number.
3057 *
3058 * This routine sets up the SCSI interface API function jump table in @phba
3059 * struct.
3060 * Returns: 0 - success, -ENODEV - failure.
3061 **/
3062int
3063lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
3064{
3065
James Smartf1126682009-06-10 17:22:44 -04003066 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
3067 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04003068
James Smart3772a992009-05-22 14:50:54 -04003069 switch (dev_grp) {
3070 case LPFC_PCI_DEV_LP:
3071 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
3072 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04003073 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05003074 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04003075 break;
James Smartda0436e2009-05-22 14:51:39 -04003076 case LPFC_PCI_DEV_OC:
3077 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
3078 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04003079 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05003080 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04003081 break;
James Smart3772a992009-05-22 14:50:54 -04003082 default:
3083 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3084 "1418 Invalid HBA PCI-device group: 0x%x\n",
3085 dev_grp);
3086 return -ENODEV;
3087 break;
3088 }
James Smart3772a992009-05-22 14:50:54 -04003089 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05003090 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04003091 return 0;
3092}
3093
3094/**
James Smart3621a712009-04-06 18:47:14 -04003095 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05003096 * @phba: The Hba for which this call is being executed.
3097 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
3098 * @rspiocbq: Pointer to lpfc_iocbq data structure.
3099 *
3100 * This routine is IOCB completion routine for device reset and target reset
3101 * routine. This routine release scsi buffer associated with lpfc_cmd.
3102 **/
James Smart7054a602007-04-25 09:52:34 -04003103static void
3104lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
3105 struct lpfc_iocbq *cmdiocbq,
3106 struct lpfc_iocbq *rspiocbq)
3107{
3108 struct lpfc_scsi_buf *lpfc_cmd =
3109 (struct lpfc_scsi_buf *) cmdiocbq->context1;
3110 if (lpfc_cmd)
3111 lpfc_release_scsi_buf(phba, lpfc_cmd);
3112 return;
3113}
3114
James Smart9bad7672008-12-04 22:39:02 -05003115/**
James Smart3621a712009-04-06 18:47:14 -04003116 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05003117 * @host: The scsi host for which this call is being executed.
3118 *
3119 * This routine provides module information about hba.
3120 *
3121 * Reutrn code:
3122 * Pointer to char - Success.
3123 **/
dea31012005-04-17 16:05:31 -05003124const char *
3125lpfc_info(struct Scsi_Host *host)
3126{
James Smart2e0fef82007-06-17 19:56:36 -05003127 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
3128 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003129 int len;
3130 static char lpfcinfobuf[384];
3131
3132 memset(lpfcinfobuf,0,384);
3133 if (phba && phba->pcidev){
3134 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
3135 len = strlen(lpfcinfobuf);
3136 snprintf(lpfcinfobuf + len,
3137 384-len,
3138 " on PCI bus %02x device %02x irq %d",
3139 phba->pcidev->bus->number,
3140 phba->pcidev->devfn,
3141 phba->pcidev->irq);
3142 len = strlen(lpfcinfobuf);
3143 if (phba->Port[0]) {
3144 snprintf(lpfcinfobuf + len,
3145 384-len,
3146 " port %s",
3147 phba->Port);
3148 }
James Smart65467b62010-01-26 23:08:29 -05003149 len = strlen(lpfcinfobuf);
3150 if (phba->sli4_hba.link_state.logical_speed) {
3151 snprintf(lpfcinfobuf + len,
3152 384-len,
3153 " Logical Link Speed: %d Mbps",
3154 phba->sli4_hba.link_state.logical_speed * 10);
3155 }
dea31012005-04-17 16:05:31 -05003156 }
3157 return lpfcinfobuf;
3158}
3159
James Smart9bad7672008-12-04 22:39:02 -05003160/**
James Smart3621a712009-04-06 18:47:14 -04003161 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05003162 * @phba: The Hba for which this call is being executed.
3163 *
3164 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
3165 * The default value of cfg_poll_tmo is 10 milliseconds.
3166 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003167static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
3168{
3169 unsigned long poll_tmo_expires =
3170 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
3171
3172 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
3173 mod_timer(&phba->fcp_poll_timer,
3174 poll_tmo_expires);
3175}
3176
James Smart9bad7672008-12-04 22:39:02 -05003177/**
James Smart3621a712009-04-06 18:47:14 -04003178 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05003179 * @phba: The Hba for which this call is being executed.
3180 *
3181 * This routine starts the fcp_poll_timer of @phba.
3182 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003183void lpfc_poll_start_timer(struct lpfc_hba * phba)
3184{
3185 lpfc_poll_rearm_timer(phba);
3186}
3187
James Smart9bad7672008-12-04 22:39:02 -05003188/**
James Smart3621a712009-04-06 18:47:14 -04003189 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05003190 * @ptr: Map to lpfc_hba data structure pointer.
3191 *
3192 * This routine restarts fcp_poll timer, when FCP ring polling is enable
3193 * and FCP Ring interrupt is disable.
3194 **/
3195
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003196void lpfc_poll_timeout(unsigned long ptr)
3197{
James Smart2e0fef82007-06-17 19:56:36 -05003198 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003199
3200 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04003201 lpfc_sli_handle_fast_ring_event(phba,
3202 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
3203
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003204 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3205 lpfc_poll_rearm_timer(phba);
3206 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003207}
3208
James Smart9bad7672008-12-04 22:39:02 -05003209/**
James Smart3621a712009-04-06 18:47:14 -04003210 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05003211 * @cmnd: Pointer to scsi_cmnd data structure.
3212 * @done: Pointer to done routine.
3213 *
3214 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
3215 * This routine prepares an IOCB from scsi command and provides to firmware.
3216 * The @done callback is invoked after driver finished processing the command.
3217 *
3218 * Return value :
3219 * 0 - Success
3220 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
3221 **/
dea31012005-04-17 16:05:31 -05003222static int
Jeff Garzikf2812332010-11-16 02:10:29 -05003223lpfc_queuecommand_lck(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
dea31012005-04-17 16:05:31 -05003224{
James Smart2e0fef82007-06-17 19:56:36 -05003225 struct Scsi_Host *shost = cmnd->device->host;
3226 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3227 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003228 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003229 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003230 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003231 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003232 int err;
dea31012005-04-17 16:05:31 -05003233
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003234 err = fc_remote_port_chkready(rport);
3235 if (err) {
3236 cmnd->result = err;
dea31012005-04-17 16:05:31 -05003237 goto out_fail_command;
3238 }
James Smart1c6f4ef52009-11-18 15:40:49 -05003239 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05003240
James Smartbf086112011-08-21 21:48:13 -04003241 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
3242 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED) ||
3243 (phba->sli_rev == LPFC_SLI_REV4))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05003244
James Smart6a9c52c2009-10-02 15:16:51 -04003245 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3246 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
3247 " op:%02x str=%s without registering for"
3248 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05003249 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
3250 dif_op_str[scsi_get_prot_op(cmnd)]);
3251 goto out_fail_command;
3252 }
3253
dea31012005-04-17 16:05:31 -05003254 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003255 * Catch race where our node has transitioned, but the
3256 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05003257 */
James Smartb522d7d2008-09-07 11:51:56 -04003258 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
Mike Christief55ca84d2011-02-25 14:04:27 -06003259 cmnd->result = ScsiResult(DID_IMM_RETRY, 0);
James Smartb522d7d2008-09-07 11:51:56 -04003260 goto out_fail_command;
3261 }
James Smart7dc517d2010-07-14 15:32:10 -04003262 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
Mike Christie34963432011-02-25 14:04:28 -06003263 goto out_tgt_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05003264
James Smart19ca7602010-11-20 23:11:55 -05003265 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05003266 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05003267 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003268
James Smarte8b62012007-08-02 11:10:09 -04003269 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3270 "0707 driver's buffer pool is empty, "
3271 "IO busied\n");
dea31012005-04-17 16:05:31 -05003272 goto out_host_busy;
3273 }
3274
3275 /*
3276 * Store the midlayer's command structure for the completion phase
3277 * and complete the command initialization.
3278 */
3279 lpfc_cmd->pCmd = cmnd;
3280 lpfc_cmd->rdata = rdata;
3281 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04003282 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05003283 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
3284 cmnd->scsi_done = done;
3285
James Smarte2a0a9d2008-12-04 22:40:02 -05003286 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04003287 if (vport->phba->cfg_enable_bg) {
3288 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003289 "9033 BLKGRD: rcvd protected cmd:%02x op:%02x "
3290 "str=%s\n",
3291 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
3292 dif_op_str[scsi_get_prot_op(cmnd)]);
James Smart6a9c52c2009-10-02 15:16:51 -04003293 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003294 "9034 BLKGRD: CDB: %02x %02x %02x %02x %02x "
James Smarte4e74272009-07-19 10:01:38 -04003295 "%02x %02x %02x %02x %02x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05003296 cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
3297 cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
3298 cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
3299 cmnd->cmnd[9]);
James Smart6a9c52c2009-10-02 15:16:51 -04003300 if (cmnd->cmnd[0] == READ_10)
3301 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003302 "9035 BLKGRD: READ @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09003303 "count %u\n",
3304 (unsigned long long)scsi_get_lba(cmnd),
3305 blk_rq_sectors(cmnd->request));
James Smart6a9c52c2009-10-02 15:16:51 -04003306 else if (cmnd->cmnd[0] == WRITE_10)
3307 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003308 "9036 BLKGRD: WRITE @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09003309 "count %u cmd=%p\n",
James Smart87b5c322008-12-16 10:34:09 -05003310 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09003311 blk_rq_sectors(cmnd->request),
James Smarte2a0a9d2008-12-04 22:40:02 -05003312 cmnd);
James Smart6a9c52c2009-10-02 15:16:51 -04003313 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003314
3315 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3316 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04003317 if (vport->phba->cfg_enable_bg) {
James Smarte2a0a9d2008-12-04 22:40:02 -05003318 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smart6a9c52c2009-10-02 15:16:51 -04003319 "9038 BLKGRD: rcvd unprotected cmd:"
3320 "%02x op:%02x str=%s\n",
3321 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
3322 dif_op_str[scsi_get_prot_op(cmnd)]);
3323 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3324 "9039 BLKGRD: CDB: %02x %02x %02x "
3325 "%02x %02x %02x %02x %02x %02x %02x\n",
3326 cmnd->cmnd[0], cmnd->cmnd[1],
3327 cmnd->cmnd[2], cmnd->cmnd[3],
3328 cmnd->cmnd[4], cmnd->cmnd[5],
3329 cmnd->cmnd[6], cmnd->cmnd[7],
3330 cmnd->cmnd[8], cmnd->cmnd[9]);
3331 if (cmnd->cmnd[0] == READ_10)
3332 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3333 "9040 dbg: READ @ sector %llu, "
3334 "count %u\n",
3335 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09003336 blk_rq_sectors(cmnd->request));
James Smart6a9c52c2009-10-02 15:16:51 -04003337 else if (cmnd->cmnd[0] == WRITE_10)
3338 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003339 "9041 dbg: WRITE @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09003340 "count %u cmd=%p\n",
James Smart87b5c322008-12-16 10:34:09 -05003341 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09003342 blk_rq_sectors(cmnd->request), cmnd);
James Smart6a9c52c2009-10-02 15:16:51 -04003343 else
3344 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003345 "9042 dbg: parser not implemented\n");
James Smart6a9c52c2009-10-02 15:16:51 -04003346 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003347 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3348 }
3349
dea31012005-04-17 16:05:31 -05003350 if (err)
3351 goto out_host_busy_free_buf;
3352
James Smart2e0fef82007-06-17 19:56:36 -05003353 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05003354
James Smart977b5a02008-09-07 11:52:04 -04003355 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04003356 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05003357 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05003358 if (err) {
3359 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05003360 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05003361 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003362 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04003363 spin_unlock(shost->host_lock);
3364 lpfc_sli_handle_fast_ring_event(phba,
3365 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
3366
3367 spin_lock(shost->host_lock);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003368 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3369 lpfc_poll_rearm_timer(phba);
3370 }
3371
dea31012005-04-17 16:05:31 -05003372 return 0;
3373
3374 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04003375 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003376 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05003377 out_host_busy:
3378 return SCSI_MLQUEUE_HOST_BUSY;
3379
Mike Christie34963432011-02-25 14:04:28 -06003380 out_tgt_busy:
3381 return SCSI_MLQUEUE_TARGET_BUSY;
3382
dea31012005-04-17 16:05:31 -05003383 out_fail_command:
3384 done(cmnd);
3385 return 0;
3386}
3387
Jeff Garzikf2812332010-11-16 02:10:29 -05003388static DEF_SCSI_QCMD(lpfc_queuecommand)
3389
James Smart9bad7672008-12-04 22:39:02 -05003390/**
James Smart3621a712009-04-06 18:47:14 -04003391 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05003392 * @cmnd: Pointer to scsi_cmnd data structure.
3393 *
3394 * This routine aborts @cmnd pending in base driver.
3395 *
3396 * Return code :
3397 * 0x2003 - Error
3398 * 0x2002 - Success
3399 **/
dea31012005-04-17 16:05:31 -05003400static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05003401lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003402{
James Smart2e0fef82007-06-17 19:56:36 -05003403 struct Scsi_Host *shost = cmnd->device->host;
3404 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3405 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003406 struct lpfc_iocbq *iocb;
3407 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05003408 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05003409 IOCB_t *cmd, *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003410 int ret = SUCCESS;
James Smartfa61a542008-01-11 01:52:42 -05003411 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003412
James Smart589a52d2010-07-14 15:30:54 -04003413 ret = fc_block_scsi_eh(cmnd);
3414 if (ret)
3415 return ret;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003416 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smarteee88772010-09-29 11:19:08 -04003417 if (!lpfc_cmd) {
3418 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3419 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003420 "x%x ID %d LUN %d\n",
3421 ret, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04003422 return SUCCESS;
3423 }
dea31012005-04-17 16:05:31 -05003424
3425 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003426 * If pCmd field of the corresponding lpfc_scsi_buf structure
3427 * points to a different SCSI command, then the driver has
3428 * already completed this command, but the midlayer did not
3429 * see the completion before the eh fired. Just return
3430 * SUCCESS.
dea31012005-04-17 16:05:31 -05003431 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003432 iocb = &lpfc_cmd->cur_iocbq;
3433 if (lpfc_cmd->pCmd != cmnd)
3434 goto out;
dea31012005-04-17 16:05:31 -05003435
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003436 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05003437
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003438 abtsiocb = lpfc_sli_get_iocbq(phba);
3439 if (abtsiocb == NULL) {
3440 ret = FAILED;
dea31012005-04-17 16:05:31 -05003441 goto out;
3442 }
3443
dea31012005-04-17 16:05:31 -05003444 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003445 * The scsi command can not be in txq and it is in flight because the
3446 * pCmd is still pointig at the SCSI command we have to abort. There
3447 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05003448 */
dea31012005-04-17 16:05:31 -05003449
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003450 cmd = &iocb->iocb;
3451 icmd = &abtsiocb->iocb;
3452 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
3453 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04003454 if (phba->sli_rev == LPFC_SLI_REV4)
3455 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
3456 else
3457 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05003458
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003459 icmd->ulpLe = 1;
3460 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05003461
3462 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
3463 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05003464 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05003465
James Smart2e0fef82007-06-17 19:56:36 -05003466 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003467 icmd->ulpCommand = CMD_ABORT_XRI_CN;
3468 else
3469 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05003470
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003471 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05003472 abtsiocb->vport = vport;
James Smart3772a992009-05-22 14:50:54 -04003473 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
3474 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003475 lpfc_sli_release_iocbq(phba, abtsiocb);
3476 ret = FAILED;
3477 goto out;
3478 }
3479
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003480 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04003481 lpfc_sli_handle_fast_ring_event(phba,
3482 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003483
James Smartfa61a542008-01-11 01:52:42 -05003484 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003485 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05003486 wait_event_timeout(waitq,
3487 (lpfc_cmd->pCmd != cmnd),
3488 (2*vport->cfg_devloss_tmo*HZ));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003489
James Smartfa61a542008-01-11 01:52:42 -05003490 spin_lock_irq(shost->host_lock);
3491 lpfc_cmd->waitq = NULL;
3492 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003493
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003494 if (lpfc_cmd->pCmd == cmnd) {
3495 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04003496 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3497 "0748 abort handler timed out waiting "
3498 "for abort to complete: ret %#x, ID %d, "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003499 "LUN %d\n",
3500 ret, cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05003501 }
3502
3503 out:
James Smarte8b62012007-08-02 11:10:09 -04003504 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3505 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003506 "LUN %d\n", ret, cmnd->device->id,
3507 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003508 return ret;
dea31012005-04-17 16:05:31 -05003509}
3510
James Smartbbb9d182009-06-10 17:23:16 -04003511static char *
3512lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
3513{
3514 switch (task_mgmt_cmd) {
3515 case FCP_ABORT_TASK_SET:
3516 return "ABORT_TASK_SET";
3517 case FCP_CLEAR_TASK_SET:
3518 return "FCP_CLEAR_TASK_SET";
3519 case FCP_BUS_RESET:
3520 return "FCP_BUS_RESET";
3521 case FCP_LUN_RESET:
3522 return "FCP_LUN_RESET";
3523 case FCP_TARGET_RESET:
3524 return "FCP_TARGET_RESET";
3525 case FCP_CLEAR_ACA:
3526 return "FCP_CLEAR_ACA";
3527 case FCP_TERMINATE_TASK:
3528 return "FCP_TERMINATE_TASK";
3529 default:
3530 return "unknown";
3531 }
3532}
3533
3534/**
3535 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
3536 * @vport: The virtual port for which this call is being executed.
3537 * @rdata: Pointer to remote port local data
3538 * @tgt_id: Target ID of remote device.
3539 * @lun_id: Lun number for the TMF
3540 * @task_mgmt_cmd: type of TMF to send
3541 *
3542 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
3543 * a remote port.
3544 *
3545 * Return Code:
3546 * 0x2003 - Error
3547 * 0x2002 - Success.
3548 **/
3549static int
3550lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
3551 unsigned tgt_id, unsigned int lun_id,
3552 uint8_t task_mgmt_cmd)
3553{
3554 struct lpfc_hba *phba = vport->phba;
3555 struct lpfc_scsi_buf *lpfc_cmd;
3556 struct lpfc_iocbq *iocbq;
3557 struct lpfc_iocbq *iocbqrsp;
James Smart5989b8d2010-10-22 11:06:56 -04003558 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003559 int ret;
3560 int status;
3561
James Smart5989b8d2010-10-22 11:06:56 -04003562 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smartbbb9d182009-06-10 17:23:16 -04003563 return FAILED;
3564
James Smart19ca7602010-11-20 23:11:55 -05003565 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
James Smartbbb9d182009-06-10 17:23:16 -04003566 if (lpfc_cmd == NULL)
3567 return FAILED;
3568 lpfc_cmd->timeout = 60;
3569 lpfc_cmd->rdata = rdata;
3570
3571 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
3572 task_mgmt_cmd);
3573 if (!status) {
3574 lpfc_release_scsi_buf(phba, lpfc_cmd);
3575 return FAILED;
3576 }
3577
3578 iocbq = &lpfc_cmd->cur_iocbq;
3579 iocbqrsp = lpfc_sli_get_iocbq(phba);
3580 if (iocbqrsp == NULL) {
3581 lpfc_release_scsi_buf(phba, lpfc_cmd);
3582 return FAILED;
3583 }
3584
3585 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3586 "0702 Issue %s to TGT %d LUN %d "
James Smart6d368e52011-05-24 11:44:12 -04003587 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04003588 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04003589 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
3590 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04003591
3592 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
3593 iocbq, iocbqrsp, lpfc_cmd->timeout);
3594 if (status != IOCB_SUCCESS) {
3595 if (status == IOCB_TIMEDOUT) {
3596 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
3597 ret = TIMEOUT_ERROR;
3598 } else
3599 ret = FAILED;
3600 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3601 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart6d368e52011-05-24 11:44:12 -04003602 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
3603 "iocb_flag x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04003604 lpfc_taskmgmt_name(task_mgmt_cmd),
3605 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
James Smart6d368e52011-05-24 11:44:12 -04003606 iocbqrsp->iocb.un.ulpWord[4],
3607 iocbq->iocb_flag);
James Smart2a9bf3d2010-06-07 15:24:45 -04003608 } else if (status == IOCB_BUSY)
3609 ret = FAILED;
3610 else
James Smartbbb9d182009-06-10 17:23:16 -04003611 ret = SUCCESS;
3612
3613 lpfc_sli_release_iocbq(phba, iocbqrsp);
3614
3615 if (ret != TIMEOUT_ERROR)
3616 lpfc_release_scsi_buf(phba, lpfc_cmd);
3617
3618 return ret;
3619}
3620
3621/**
3622 * lpfc_chk_tgt_mapped -
3623 * @vport: The virtual port to check on
3624 * @cmnd: Pointer to scsi_cmnd data structure.
3625 *
3626 * This routine delays until the scsi target (aka rport) for the
3627 * command exists (is present and logged in) or we declare it non-existent.
3628 *
3629 * Return code :
3630 * 0x2003 - Error
3631 * 0x2002 - Success
3632 **/
3633static int
3634lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
3635{
3636 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003637 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003638 unsigned long later;
3639
James Smart1c6f4ef52009-11-18 15:40:49 -05003640 if (!rdata) {
3641 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3642 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
3643 return FAILED;
3644 }
3645 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003646 /*
3647 * If target is not in a MAPPED state, delay until
3648 * target is rediscovered or devloss timeout expires.
3649 */
3650 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3651 while (time_after(later, jiffies)) {
3652 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3653 return FAILED;
3654 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
3655 return SUCCESS;
3656 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
3657 rdata = cmnd->device->hostdata;
3658 if (!rdata)
3659 return FAILED;
3660 pnode = rdata->pnode;
3661 }
3662 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
3663 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
3664 return FAILED;
3665 return SUCCESS;
3666}
3667
3668/**
3669 * lpfc_reset_flush_io_context -
3670 * @vport: The virtual port (scsi_host) for the flush context
3671 * @tgt_id: If aborting by Target contect - specifies the target id
3672 * @lun_id: If aborting by Lun context - specifies the lun id
3673 * @context: specifies the context level to flush at.
3674 *
3675 * After a reset condition via TMF, we need to flush orphaned i/o
3676 * contexts from the adapter. This routine aborts any contexts
3677 * outstanding, then waits for their completions. The wait is
3678 * bounded by devloss_tmo though.
3679 *
3680 * Return code :
3681 * 0x2003 - Error
3682 * 0x2002 - Success
3683 **/
3684static int
3685lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
3686 uint64_t lun_id, lpfc_ctx_cmd context)
3687{
3688 struct lpfc_hba *phba = vport->phba;
3689 unsigned long later;
3690 int cnt;
3691
3692 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3693 if (cnt)
3694 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
3695 tgt_id, lun_id, context);
3696 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3697 while (time_after(later, jiffies) && cnt) {
3698 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
3699 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3700 }
3701 if (cnt) {
3702 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3703 "0724 I/O flush failure for context %s : cnt x%x\n",
3704 ((context == LPFC_CTX_LUN) ? "LUN" :
3705 ((context == LPFC_CTX_TGT) ? "TGT" :
3706 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
3707 cnt);
3708 return FAILED;
3709 }
3710 return SUCCESS;
3711}
3712
James Smart9bad7672008-12-04 22:39:02 -05003713/**
James Smart3621a712009-04-06 18:47:14 -04003714 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05003715 * @cmnd: Pointer to scsi_cmnd data structure.
3716 *
James Smartbbb9d182009-06-10 17:23:16 -04003717 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05003718 * command.
3719 *
3720 * Return code :
3721 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04003722 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05003723 **/
dea31012005-04-17 16:05:31 -05003724static int
James Smart7054a602007-04-25 09:52:34 -04003725lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003726{
James Smart2e0fef82007-06-17 19:56:36 -05003727 struct Scsi_Host *shost = cmnd->device->host;
3728 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05003729 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003730 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003731 unsigned tgt_id = cmnd->device->id;
3732 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04003733 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04003734 int status;
dea31012005-04-17 16:05:31 -05003735
James Smart1c6f4ef52009-11-18 15:40:49 -05003736 if (!rdata) {
3737 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3738 "0798 Device Reset rport failure: rdata x%p\n", rdata);
3739 return FAILED;
3740 }
3741 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04003742 status = fc_block_scsi_eh(cmnd);
3743 if (status)
3744 return status;
James Smartbbb9d182009-06-10 17:23:16 -04003745
3746 status = lpfc_chk_tgt_mapped(vport, cmnd);
3747 if (status == FAILED) {
3748 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3749 "0721 Device Reset rport failure: rdata x%p\n", rdata);
3750 return FAILED;
3751 }
3752
3753 scsi_event.event_type = FC_REG_SCSI_EVENT;
3754 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
3755 scsi_event.lun = lun_id;
3756 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3757 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3758
3759 fc_host_post_vendor_event(shost, fc_get_event_number(),
3760 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3761
3762 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3763 FCP_LUN_RESET);
3764
3765 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3766 "0713 SCSI layer issued Device Reset (%d, %d) "
3767 "return x%x\n", tgt_id, lun_id, status);
3768
dea31012005-04-17 16:05:31 -05003769 /*
James Smartbbb9d182009-06-10 17:23:16 -04003770 * We have to clean up i/o as : they may be orphaned by the TMF;
3771 * or if the TMF failed, they may be in an indeterminate state.
3772 * So, continue on.
3773 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05003774 */
James Smartbbb9d182009-06-10 17:23:16 -04003775 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3776 LPFC_CTX_LUN);
3777 return status;
3778}
3779
3780/**
3781 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
3782 * @cmnd: Pointer to scsi_cmnd data structure.
3783 *
3784 * This routine does a target reset by sending a TARGET_RESET task management
3785 * command.
3786 *
3787 * Return code :
3788 * 0x2003 - Error
3789 * 0x2002 - Success
3790 **/
3791static int
3792lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
3793{
3794 struct Scsi_Host *shost = cmnd->device->host;
3795 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3796 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003797 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003798 unsigned tgt_id = cmnd->device->id;
3799 unsigned int lun_id = cmnd->device->lun;
3800 struct lpfc_scsi_event_header scsi_event;
3801 int status;
3802
James Smart1c6f4ef52009-11-18 15:40:49 -05003803 if (!rdata) {
3804 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3805 "0799 Target Reset rport failure: rdata x%p\n", rdata);
3806 return FAILED;
3807 }
3808 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04003809 status = fc_block_scsi_eh(cmnd);
3810 if (status)
3811 return status;
James Smartbbb9d182009-06-10 17:23:16 -04003812
3813 status = lpfc_chk_tgt_mapped(vport, cmnd);
3814 if (status == FAILED) {
3815 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3816 "0722 Target Reset rport failure: rdata x%p\n", rdata);
3817 return FAILED;
dea31012005-04-17 16:05:31 -05003818 }
James Smartea2151b2008-09-07 11:52:10 -04003819
3820 scsi_event.event_type = FC_REG_SCSI_EVENT;
3821 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
3822 scsi_event.lun = 0;
3823 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3824 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3825
James Smartbbb9d182009-06-10 17:23:16 -04003826 fc_host_post_vendor_event(shost, fc_get_event_number(),
3827 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04003828
James Smartbbb9d182009-06-10 17:23:16 -04003829 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3830 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05003831
James Smarte8b62012007-08-02 11:10:09 -04003832 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04003833 "0723 SCSI layer issued Target Reset (%d, %d) "
3834 "return x%x\n", tgt_id, lun_id, status);
3835
3836 /*
3837 * We have to clean up i/o as : they may be orphaned by the TMF;
3838 * or if the TMF failed, they may be in an indeterminate state.
3839 * So, continue on.
3840 * We will report success if all the i/o aborts successfully.
3841 */
3842 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3843 LPFC_CTX_TGT);
3844 return status;
dea31012005-04-17 16:05:31 -05003845}
3846
James Smart9bad7672008-12-04 22:39:02 -05003847/**
James Smart3621a712009-04-06 18:47:14 -04003848 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05003849 * @cmnd: Pointer to scsi_cmnd data structure.
3850 *
James Smartbbb9d182009-06-10 17:23:16 -04003851 * This routine does target reset to all targets on @cmnd->device->host.
3852 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05003853 *
James Smartbbb9d182009-06-10 17:23:16 -04003854 * Return code :
3855 * 0x2003 - Error
3856 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05003857 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04003858static int
James Smart7054a602007-04-25 09:52:34 -04003859lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003860{
James Smart2e0fef82007-06-17 19:56:36 -05003861 struct Scsi_Host *shost = cmnd->device->host;
3862 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05003863 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04003864 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04003865 int match;
3866 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04003867
3868 scsi_event.event_type = FC_REG_SCSI_EVENT;
3869 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
3870 scsi_event.lun = 0;
3871 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
3872 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
3873
James Smartbbb9d182009-06-10 17:23:16 -04003874 fc_host_post_vendor_event(shost, fc_get_event_number(),
3875 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05003876
James Smartbf086112011-08-21 21:48:13 -04003877 status = fc_block_scsi_eh(cmnd);
3878 if (status)
3879 return status;
James Smartbbb9d182009-06-10 17:23:16 -04003880
dea31012005-04-17 16:05:31 -05003881 /*
3882 * Since the driver manages a single bus device, reset all
3883 * targets known to the driver. Should any target reset
3884 * fail, this routine returns failure to the midlayer.
3885 */
James Smarte17da182006-07-06 15:49:25 -04003886 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04003887 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05003888 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003889 spin_lock_irq(shost->host_lock);
3890 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003891 if (!NLP_CHK_NODE_ACT(ndlp))
3892 continue;
James Smart685f0bf2007-04-25 09:53:08 -04003893 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04003894 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04003895 ndlp->rport) {
dea31012005-04-17 16:05:31 -05003896 match = 1;
3897 break;
3898 }
3899 }
James Smart2e0fef82007-06-17 19:56:36 -05003900 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003901 if (!match)
3902 continue;
James Smartbbb9d182009-06-10 17:23:16 -04003903
3904 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
3905 i, 0, FCP_TARGET_RESET);
3906
3907 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04003908 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3909 "0700 Bus Reset on target %d failed\n",
3910 i);
James Smart915caaa2008-06-14 22:52:38 -04003911 ret = FAILED;
dea31012005-04-17 16:05:31 -05003912 }
3913 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003914 /*
James Smartbbb9d182009-06-10 17:23:16 -04003915 * We have to clean up i/o as : they may be orphaned by the TMFs
3916 * above; or if any of the TMFs failed, they may be in an
3917 * indeterminate state.
3918 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003919 */
James Smartbbb9d182009-06-10 17:23:16 -04003920
3921 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
3922 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003923 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04003924
James Smarte8b62012007-08-02 11:10:09 -04003925 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3926 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05003927 return ret;
3928}
3929
James Smart9bad7672008-12-04 22:39:02 -05003930/**
James Smart3621a712009-04-06 18:47:14 -04003931 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05003932 * @sdev: Pointer to scsi_device.
3933 *
3934 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
3935 * globally available list of scsi buffers. This routine also makes sure scsi
3936 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
3937 * of scsi buffer exists for the lifetime of the driver.
3938 *
3939 * Return codes:
3940 * non-0 - Error
3941 * 0 - Success
3942 **/
dea31012005-04-17 16:05:31 -05003943static int
dea31012005-04-17 16:05:31 -05003944lpfc_slave_alloc(struct scsi_device *sdev)
3945{
James Smart2e0fef82007-06-17 19:56:36 -05003946 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3947 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003948 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04003949 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05003950 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04003951 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04003952 uint32_t sdev_cnt;
dea31012005-04-17 16:05:31 -05003953
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003954 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05003955 return -ENXIO;
3956
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003957 sdev->hostdata = rport->dd_data;
James Smartd7c47992010-06-08 18:31:54 -04003958 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05003959
3960 /*
3961 * Populate the cmds_per_lun count scsi_bufs into this host's globally
3962 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04003963 * HBA limit conveyed to the midlayer via the host structure. The
3964 * formula accounts for the lun_queue_depth + error handlers + 1
3965 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05003966 */
3967 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04003968 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05003969
James Smartd7c47992010-06-08 18:31:54 -04003970 /* If allocated buffers are enough do nothing */
3971 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
3972 return 0;
3973
James Smart92d7f7b2007-06-17 19:56:38 -05003974 /* Allow some exchanges to be available always to complete discovery */
3975 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04003976 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3977 "0704 At limitation of %d preallocated "
3978 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05003979 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003980 /* Allow some exchanges to be available always to complete discovery */
3981 } else if (total + num_to_alloc >
3982 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04003983 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3984 "0705 Allocation request of %d "
3985 "command buffers will exceed max of %d. "
3986 "Reducing allocation request to %d.\n",
3987 num_to_alloc, phba->cfg_hba_queue_depth,
3988 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05003989 num_to_alloc = phba->cfg_hba_queue_depth - total;
3990 }
James Smart3772a992009-05-22 14:50:54 -04003991 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
3992 if (num_to_alloc != num_allocated) {
3993 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3994 "0708 Allocation request of %d "
3995 "command buffers did not succeed. "
3996 "Allocated %d buffers.\n",
3997 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05003998 }
James Smart1c6f4ef52009-11-18 15:40:49 -05003999 if (num_allocated > 0)
4000 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05004001 return 0;
4002}
4003
James Smart9bad7672008-12-04 22:39:02 -05004004/**
James Smart3621a712009-04-06 18:47:14 -04004005 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05004006 * @sdev: Pointer to scsi_device.
4007 *
4008 * This routine configures following items
4009 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05004010 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
4011 *
4012 * Return codes:
4013 * 0 - Success
4014 **/
dea31012005-04-17 16:05:31 -05004015static int
4016lpfc_slave_configure(struct scsi_device *sdev)
4017{
James Smart2e0fef82007-06-17 19:56:36 -05004018 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
4019 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004020
4021 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04004022 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05004023 else
James Smart3de2a652007-08-02 11:09:59 -04004024 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05004025
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004026 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004027 lpfc_sli_handle_fast_ring_event(phba,
4028 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004029 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4030 lpfc_poll_rearm_timer(phba);
4031 }
4032
dea31012005-04-17 16:05:31 -05004033 return 0;
4034}
4035
James Smart9bad7672008-12-04 22:39:02 -05004036/**
James Smart3621a712009-04-06 18:47:14 -04004037 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05004038 * @sdev: Pointer to scsi_device.
4039 *
4040 * This routine sets @sdev hostatdata filed to null.
4041 **/
dea31012005-04-17 16:05:31 -05004042static void
4043lpfc_slave_destroy(struct scsi_device *sdev)
4044{
James Smartd7c47992010-06-08 18:31:54 -04004045 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
4046 struct lpfc_hba *phba = vport->phba;
4047 atomic_dec(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05004048 sdev->hostdata = NULL;
4049 return;
4050}
4051
James Smart92d7f7b2007-06-17 19:56:38 -05004052
dea31012005-04-17 16:05:31 -05004053struct scsi_host_template lpfc_template = {
4054 .module = THIS_MODULE,
4055 .name = LPFC_DRIVER_NAME,
4056 .info = lpfc_info,
4057 .queuecommand = lpfc_queuecommand,
4058 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04004059 .eh_device_reset_handler = lpfc_device_reset_handler,
4060 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04004061 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05004062 .slave_alloc = lpfc_slave_alloc,
4063 .slave_configure = lpfc_slave_configure,
4064 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04004065 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05004066 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05004067 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05004068 .cmd_per_lun = LPFC_CMD_PER_LUN,
4069 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05004070 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04004071 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04004072 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05004073 .change_queue_depth = lpfc_change_queue_depth,
dea31012005-04-17 16:05:31 -05004074};
James Smart3de2a652007-08-02 11:09:59 -04004075
4076struct scsi_host_template lpfc_vport_template = {
4077 .module = THIS_MODULE,
4078 .name = LPFC_DRIVER_NAME,
4079 .info = lpfc_info,
4080 .queuecommand = lpfc_queuecommand,
4081 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04004082 .eh_device_reset_handler = lpfc_device_reset_handler,
4083 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04004084 .eh_bus_reset_handler = lpfc_bus_reset_handler,
4085 .slave_alloc = lpfc_slave_alloc,
4086 .slave_configure = lpfc_slave_configure,
4087 .slave_destroy = lpfc_slave_destroy,
4088 .scan_finished = lpfc_scan_finished,
4089 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05004090 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04004091 .cmd_per_lun = LPFC_CMD_PER_LUN,
4092 .use_clustering = ENABLE_CLUSTERING,
4093 .shost_attrs = lpfc_vport_attrs,
4094 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05004095 .change_queue_depth = lpfc_change_queue_depth,
James Smart3de2a652007-08-02 11:09:59 -04004096};