blob: c88f59f0ce307c5ab12e57081c9e32443a50f63a [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 Smartd8e93df2009-05-22 14:53:05 -04004 * Copyright (C) 2004-2009 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>
22#include <linux/interrupt.h>
James Smarta90f5682006-08-17 11:58:04 -040023#include <linux/delay.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050024#include <asm/unaligned.h>
dea31012005-04-17 16:05:31 -050025
26#include <scsi/scsi.h>
27#include <scsi/scsi_device.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050028#include <scsi/scsi_eh.h>
dea31012005-04-17 16:05:31 -050029#include <scsi/scsi_host.h>
30#include <scsi/scsi_tcq.h>
31#include <scsi/scsi_transport_fc.h>
32
33#include "lpfc_version.h"
James Smartda0436e2009-05-22 14:51:39 -040034#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050035#include "lpfc_hw.h"
36#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040037#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040038#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050039#include "lpfc_disc.h"
40#include "lpfc_scsi.h"
41#include "lpfc.h"
42#include "lpfc_logmsg.h"
43#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050044#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050045
46#define LPFC_RESET_WAIT 2
47#define LPFC_ABORT_WAIT 2
48
James Smarte2a0a9d2008-12-04 22:40:02 -050049int _dump_buf_done;
50
51static char *dif_op_str[] = {
52 "SCSI_PROT_NORMAL",
53 "SCSI_PROT_READ_INSERT",
54 "SCSI_PROT_WRITE_STRIP",
55 "SCSI_PROT_READ_STRIP",
56 "SCSI_PROT_WRITE_INSERT",
57 "SCSI_PROT_READ_PASS",
58 "SCSI_PROT_WRITE_PASS",
James Smarte2a0a9d2008-12-04 22:40:02 -050059};
James Smartda0436e2009-05-22 14:51:39 -040060static void
61lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smarte2a0a9d2008-12-04 22:40:02 -050062
63static void
64lpfc_debug_save_data(struct scsi_cmnd *cmnd)
65{
66 void *src, *dst;
67 struct scatterlist *sgde = scsi_sglist(cmnd);
68
69 if (!_dump_buf_data) {
70 printk(KERN_ERR "BLKGRD ERROR %s _dump_buf_data is NULL\n",
71 __func__);
72 return;
73 }
74
75
76 if (!sgde) {
77 printk(KERN_ERR "BLKGRD ERROR: data scatterlist is null\n");
78 return;
79 }
80
81 dst = (void *) _dump_buf_data;
82 while (sgde) {
83 src = sg_virt(sgde);
84 memcpy(dst, src, sgde->length);
85 dst += sgde->length;
86 sgde = sg_next(sgde);
87 }
88}
89
90static void
91lpfc_debug_save_dif(struct scsi_cmnd *cmnd)
92{
93 void *src, *dst;
94 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
95
96 if (!_dump_buf_dif) {
97 printk(KERN_ERR "BLKGRD ERROR %s _dump_buf_data is NULL\n",
98 __func__);
99 return;
100 }
101
102 if (!sgde) {
103 printk(KERN_ERR "BLKGRD ERROR: prot scatterlist is null\n");
104 return;
105 }
106
107 dst = _dump_buf_dif;
108 while (sgde) {
109 src = sg_virt(sgde);
110 memcpy(dst, src, sgde->length);
111 dst += sgde->length;
112 sgde = sg_next(sgde);
113 }
114}
115
James Smartea2151b2008-09-07 11:52:10 -0400116/**
James Smartf1126682009-06-10 17:22:44 -0400117 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
118 * @phba: Pointer to HBA object.
119 * @lpfc_cmd: lpfc scsi command object pointer.
120 *
121 * This function is called from the lpfc_prep_task_mgmt_cmd function to
122 * set the last bit in the response sge entry.
123 **/
124static void
125lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
126 struct lpfc_scsi_buf *lpfc_cmd)
127{
128 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
129 if (sgl) {
130 sgl += 1;
131 sgl->word2 = le32_to_cpu(sgl->word2);
132 bf_set(lpfc_sli4_sge_last, sgl, 1);
133 sgl->word2 = cpu_to_le32(sgl->word2);
134 }
135}
136
137/**
James Smart3621a712009-04-06 18:47:14 -0400138 * lpfc_update_stats - Update statistical data for the command completion
James Smartea2151b2008-09-07 11:52:10 -0400139 * @phba: Pointer to HBA object.
140 * @lpfc_cmd: lpfc scsi command object pointer.
141 *
142 * This function is called when there is a command completion and this
143 * function updates the statistical data for the command completion.
144 **/
145static void
146lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
147{
148 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
149 struct lpfc_nodelist *pnode = rdata->pnode;
150 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
151 unsigned long flags;
152 struct Scsi_Host *shost = cmd->device->host;
153 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
154 unsigned long latency;
155 int i;
156
157 if (cmd->result)
158 return;
159
James Smart9f1e1b52008-12-04 22:39:40 -0500160 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
161
James Smartea2151b2008-09-07 11:52:10 -0400162 spin_lock_irqsave(shost->host_lock, flags);
163 if (!vport->stat_data_enabled ||
164 vport->stat_data_blocked ||
165 !pnode->lat_data ||
166 (phba->bucket_type == LPFC_NO_BUCKET)) {
167 spin_unlock_irqrestore(shost->host_lock, flags);
168 return;
169 }
James Smartea2151b2008-09-07 11:52:10 -0400170
171 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
172 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
173 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -0500174 /* check array subscript bounds */
175 if (i < 0)
176 i = 0;
177 else if (i >= LPFC_MAX_BUCKET_COUNT)
178 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -0400179 } else {
180 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
181 if (latency <= (phba->bucket_base +
182 ((1<<i)*phba->bucket_step)))
183 break;
184 }
185
186 pnode->lat_data[i].cmd_count++;
187 spin_unlock_irqrestore(shost->host_lock, flags);
188}
189
James Smartea2151b2008-09-07 11:52:10 -0400190/**
James Smart3621a712009-04-06 18:47:14 -0400191 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
James Smartea2151b2008-09-07 11:52:10 -0400192 * @phba: Pointer to HBA context object.
193 * @vport: Pointer to vport object.
194 * @ndlp: Pointer to FC node associated with the target.
195 * @lun: Lun number of the scsi device.
196 * @old_val: Old value of the queue depth.
197 * @new_val: New value of the queue depth.
198 *
199 * This function sends an event to the mgmt application indicating
200 * there is a change in the scsi device queue depth.
201 **/
202static void
203lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
204 struct lpfc_vport *vport,
205 struct lpfc_nodelist *ndlp,
206 uint32_t lun,
207 uint32_t old_val,
208 uint32_t new_val)
209{
210 struct lpfc_fast_path_event *fast_path_evt;
211 unsigned long flags;
212
213 fast_path_evt = lpfc_alloc_fast_evt(phba);
214 if (!fast_path_evt)
215 return;
216
217 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
218 FC_REG_SCSI_EVENT;
219 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
220 LPFC_EVENT_VARQUEDEPTH;
221
222 /* Report all luns with change in queue depth */
223 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
224 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
225 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
226 &ndlp->nlp_portname, sizeof(struct lpfc_name));
227 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
228 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
229 }
230
231 fast_path_evt->un.queue_depth_evt.oldval = old_val;
232 fast_path_evt->un.queue_depth_evt.newval = new_val;
233 fast_path_evt->vport = vport;
234
235 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
236 spin_lock_irqsave(&phba->hbalock, flags);
237 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
238 spin_unlock_irqrestore(&phba->hbalock, flags);
239 lpfc_worker_wake_up(phba);
240
241 return;
242}
243
James Smart9bad7672008-12-04 22:39:02 -0500244/**
James Smart3621a712009-04-06 18:47:14 -0400245 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500246 * @phba: The Hba for which this call is being executed.
247 *
248 * This routine is called when there is resource error in driver or firmware.
249 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
250 * posts at most 1 event each second. This routine wakes up worker thread of
251 * @phba to process WORKER_RAM_DOWN_EVENT event.
252 *
253 * This routine should be called with no lock held.
254 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500255void
James Smarteaf15d52008-12-04 22:39:29 -0500256lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500257{
258 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400259 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500260
261 spin_lock_irqsave(&phba->hbalock, flags);
262 atomic_inc(&phba->num_rsrc_err);
263 phba->last_rsrc_error_time = jiffies;
264
265 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
266 spin_unlock_irqrestore(&phba->hbalock, flags);
267 return;
268 }
269
270 phba->last_ramp_down_time = jiffies;
271
272 spin_unlock_irqrestore(&phba->hbalock, flags);
273
274 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400275 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
276 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500277 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500278 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
279
James Smart5e9d9b82008-06-14 22:52:53 -0400280 if (!evt_posted)
281 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500282 return;
283}
284
James Smart9bad7672008-12-04 22:39:02 -0500285/**
James Smart3621a712009-04-06 18:47:14 -0400286 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
James Smart9bad7672008-12-04 22:39:02 -0500287 * @phba: The Hba for which this call is being executed.
288 *
289 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
290 * post at most 1 event every 5 minute after last_ramp_up_time or
291 * last_rsrc_error_time. This routine wakes up worker thread of @phba
292 * to process WORKER_RAM_DOWN_EVENT event.
293 *
294 * This routine should be called with no lock held.
295 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500296static inline void
James Smart3de2a652007-08-02 11:09:59 -0400297lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smarta257bf92009-04-06 18:48:10 -0400298 uint32_t queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500299{
300 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -0400301 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400302 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500303 atomic_inc(&phba->num_cmd_success);
304
James Smarta257bf92009-04-06 18:48:10 -0400305 if (vport->cfg_lun_queue_depth <= queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500306 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500307 spin_lock_irqsave(&phba->hbalock, flags);
308 if (((phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) > jiffies) ||
309 ((phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL ) > jiffies)) {
310 spin_unlock_irqrestore(&phba->hbalock, flags);
311 return;
312 }
James Smart92d7f7b2007-06-17 19:56:38 -0500313 phba->last_ramp_up_time = jiffies;
314 spin_unlock_irqrestore(&phba->hbalock, flags);
315
316 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400317 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
318 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500319 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500320 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
321
James Smart5e9d9b82008-06-14 22:52:53 -0400322 if (!evt_posted)
323 lpfc_worker_wake_up(phba);
324 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500325}
326
James Smart9bad7672008-12-04 22:39:02 -0500327/**
James Smart3621a712009-04-06 18:47:14 -0400328 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500329 * @phba: The Hba for which this call is being executed.
330 *
331 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
332 * thread.This routine reduces queue depth for all scsi device on each vport
333 * associated with @phba.
334 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500335void
336lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
337{
James Smart549e55c2007-08-02 11:09:51 -0400338 struct lpfc_vport **vports;
339 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500340 struct scsi_device *sdev;
James Smartea2151b2008-09-07 11:52:10 -0400341 unsigned long new_queue_depth, old_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500342 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400343 int i;
James Smartea2151b2008-09-07 11:52:10 -0400344 struct lpfc_rport_data *rdata;
James Smart92d7f7b2007-06-17 19:56:38 -0500345
346 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
347 num_cmd_success = atomic_read(&phba->num_cmd_success);
348
James Smart549e55c2007-08-02 11:09:51 -0400349 vports = lpfc_create_vport_work_array(phba);
350 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400351 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400352 shost = lpfc_shost_from_vport(vports[i]);
353 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500354 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400355 sdev->queue_depth * num_rsrc_err /
356 (num_rsrc_err + num_cmd_success);
357 if (!new_queue_depth)
358 new_queue_depth = sdev->queue_depth - 1;
359 else
360 new_queue_depth = sdev->queue_depth -
361 new_queue_depth;
James Smartea2151b2008-09-07 11:52:10 -0400362 old_queue_depth = sdev->queue_depth;
James Smart549e55c2007-08-02 11:09:51 -0400363 if (sdev->ordered_tags)
364 scsi_adjust_queue_depth(sdev,
365 MSG_ORDERED_TAG,
366 new_queue_depth);
367 else
368 scsi_adjust_queue_depth(sdev,
369 MSG_SIMPLE_TAG,
370 new_queue_depth);
James Smartea2151b2008-09-07 11:52:10 -0400371 rdata = sdev->hostdata;
372 if (rdata)
373 lpfc_send_sdev_queuedepth_change_event(
374 phba, vports[i],
375 rdata->pnode,
376 sdev->lun, old_queue_depth,
377 new_queue_depth);
James Smart549e55c2007-08-02 11:09:51 -0400378 }
James Smart92d7f7b2007-06-17 19:56:38 -0500379 }
James Smart09372822008-01-11 01:52:54 -0500380 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500381 atomic_set(&phba->num_rsrc_err, 0);
382 atomic_set(&phba->num_cmd_success, 0);
383}
384
James Smart9bad7672008-12-04 22:39:02 -0500385/**
James Smart3621a712009-04-06 18:47:14 -0400386 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500387 * @phba: The Hba for which this call is being executed.
388 *
389 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
390 * thread.This routine increases queue depth for all scsi device on each vport
391 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
392 * num_cmd_success to zero.
393 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500394void
395lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
396{
James Smart549e55c2007-08-02 11:09:51 -0400397 struct lpfc_vport **vports;
398 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500399 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400400 int i;
James Smartea2151b2008-09-07 11:52:10 -0400401 struct lpfc_rport_data *rdata;
James Smart92d7f7b2007-06-17 19:56:38 -0500402
James Smart549e55c2007-08-02 11:09:51 -0400403 vports = lpfc_create_vport_work_array(phba);
404 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400405 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400406 shost = lpfc_shost_from_vport(vports[i]);
407 shost_for_each_device(sdev, shost) {
James Smart97eab632008-04-07 10:16:05 -0400408 if (vports[i]->cfg_lun_queue_depth <=
409 sdev->queue_depth)
410 continue;
James Smart549e55c2007-08-02 11:09:51 -0400411 if (sdev->ordered_tags)
412 scsi_adjust_queue_depth(sdev,
413 MSG_ORDERED_TAG,
414 sdev->queue_depth+1);
415 else
416 scsi_adjust_queue_depth(sdev,
417 MSG_SIMPLE_TAG,
418 sdev->queue_depth+1);
James Smartea2151b2008-09-07 11:52:10 -0400419 rdata = sdev->hostdata;
420 if (rdata)
421 lpfc_send_sdev_queuedepth_change_event(
422 phba, vports[i],
423 rdata->pnode,
424 sdev->lun,
425 sdev->queue_depth - 1,
426 sdev->queue_depth);
James Smart549e55c2007-08-02 11:09:51 -0400427 }
James Smart92d7f7b2007-06-17 19:56:38 -0500428 }
James Smart09372822008-01-11 01:52:54 -0500429 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500430 atomic_set(&phba->num_rsrc_err, 0);
431 atomic_set(&phba->num_cmd_success, 0);
432}
433
James Smarta8e497d2008-08-24 21:50:11 -0400434/**
James Smart3621a712009-04-06 18:47:14 -0400435 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400436 * @phba: Pointer to HBA context object.
437 *
438 * This function walks vport list and set each SCSI host to block state
439 * by invoking fc_remote_port_delete() routine. This function is invoked
440 * with EEH when device's PCI slot has been permanently disabled.
441 **/
442void
443lpfc_scsi_dev_block(struct lpfc_hba *phba)
444{
445 struct lpfc_vport **vports;
446 struct Scsi_Host *shost;
447 struct scsi_device *sdev;
448 struct fc_rport *rport;
449 int i;
450
451 vports = lpfc_create_vport_work_array(phba);
452 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400453 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400454 shost = lpfc_shost_from_vport(vports[i]);
455 shost_for_each_device(sdev, shost) {
456 rport = starget_to_rport(scsi_target(sdev));
457 fc_remote_port_delete(rport);
458 }
459 }
460 lpfc_destroy_vport_work_array(phba, vports);
461}
462
James Smart9bad7672008-12-04 22:39:02 -0500463/**
James Smart3772a992009-05-22 14:50:54 -0400464 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500465 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400466 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500467 *
James Smart3772a992009-05-22 14:50:54 -0400468 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
469 * the scsi buffer contains all the necessary information needed to initiate
470 * a SCSI I/O. The non-DMAable buffer region contains information to build
471 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
472 * and the initial BPL. In addition to allocating memory, the FCP CMND and
473 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500474 *
475 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400476 * int - number of scsi buffers that were allocated.
477 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500478 **/
James Smart3772a992009-05-22 14:50:54 -0400479static int
480lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500481{
James Smart2e0fef82007-06-17 19:56:36 -0500482 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500483 struct lpfc_scsi_buf *psb;
484 struct ulp_bde64 *bpl;
485 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400486 dma_addr_t pdma_phys_fcp_cmd;
487 dma_addr_t pdma_phys_fcp_rsp;
488 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500489 uint16_t iotag;
James Smart3772a992009-05-22 14:50:54 -0400490 int bcnt;
dea31012005-04-17 16:05:31 -0500491
James Smart3772a992009-05-22 14:50:54 -0400492 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
493 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
494 if (!psb)
495 break;
dea31012005-04-17 16:05:31 -0500496
James Smart3772a992009-05-22 14:50:54 -0400497 /*
498 * Get memory from the pci pool to map the virt space to pci
499 * bus space for an I/O. The DMA buffer includes space for the
500 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
501 * necessary to support the sg_tablesize.
502 */
503 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
504 GFP_KERNEL, &psb->dma_handle);
505 if (!psb->data) {
506 kfree(psb);
507 break;
508 }
dea31012005-04-17 16:05:31 -0500509
James Smart3772a992009-05-22 14:50:54 -0400510 /* Initialize virtual ptrs to dma_buf region. */
511 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
dea31012005-04-17 16:05:31 -0500512
James Smart3772a992009-05-22 14:50:54 -0400513 /* Allocate iotag for psb->cur_iocbq. */
514 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
515 if (iotag == 0) {
516 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
517 psb->data, psb->dma_handle);
518 kfree(psb);
519 break;
520 }
521 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500522
James Smart3772a992009-05-22 14:50:54 -0400523 psb->fcp_cmnd = psb->data;
524 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
525 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400526 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500527
James Smart3772a992009-05-22 14:50:54 -0400528 /* Initialize local short-hand pointers. */
529 bpl = psb->fcp_bpl;
530 pdma_phys_fcp_cmd = psb->dma_handle;
531 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
532 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
533 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500534
James Smart3772a992009-05-22 14:50:54 -0400535 /*
536 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
537 * are sg list bdes. Initialize the first two and leave the
538 * rest for queuecommand.
539 */
540 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
541 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
542 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
543 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
544 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500545
James Smart3772a992009-05-22 14:50:54 -0400546 /* Setup the physical region for the FCP RSP */
547 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
548 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
549 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
550 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
551 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
552
553 /*
554 * Since the IOCB for the FCP I/O is built into this
555 * lpfc_scsi_buf, initialize it with all known data now.
556 */
557 iocb = &psb->cur_iocbq.iocb;
558 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
559 if ((phba->sli_rev == 3) &&
560 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
561 /* fill in immediate fcp command BDE */
562 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
563 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
564 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
565 unsli3.fcp_ext.icd);
566 iocb->un.fcpi64.bdl.addrHigh = 0;
567 iocb->ulpBdeCount = 0;
568 iocb->ulpLe = 0;
569 /* fill in responce BDE */
570 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
571 BUFF_TYPE_BDE_64;
572 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
573 sizeof(struct fcp_rsp);
574 iocb->unsli3.fcp_ext.rbde.addrLow =
575 putPaddrLow(pdma_phys_fcp_rsp);
576 iocb->unsli3.fcp_ext.rbde.addrHigh =
577 putPaddrHigh(pdma_phys_fcp_rsp);
578 } else {
579 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
580 iocb->un.fcpi64.bdl.bdeSize =
581 (2 * sizeof(struct ulp_bde64));
582 iocb->un.fcpi64.bdl.addrLow =
583 putPaddrLow(pdma_phys_bpl);
584 iocb->un.fcpi64.bdl.addrHigh =
585 putPaddrHigh(pdma_phys_bpl);
586 iocb->ulpBdeCount = 1;
587 iocb->ulpLe = 1;
588 }
589 iocb->ulpClass = CLASS3;
590 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400591 /* Put it back into the SCSI buffer list */
592 lpfc_release_scsi_buf_s4(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400593
James Smart34b02dc2008-08-24 21:49:55 -0400594 }
dea31012005-04-17 16:05:31 -0500595
James Smart3772a992009-05-22 14:50:54 -0400596 return bcnt;
dea31012005-04-17 16:05:31 -0500597}
598
James Smart9bad7672008-12-04 22:39:02 -0500599/**
James Smartda0436e2009-05-22 14:51:39 -0400600 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
601 * @phba: pointer to lpfc hba data structure.
602 * @axri: pointer to the fcp xri abort wcqe structure.
603 *
604 * This routine is invoked by the worker thread to process a SLI4 fast-path
605 * FCP aborted xri.
606 **/
607void
608lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
609 struct sli4_wcqe_xri_aborted *axri)
610{
611 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
612 struct lpfc_scsi_buf *psb, *next_psb;
613 unsigned long iflag = 0;
614
615 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock, iflag);
616 list_for_each_entry_safe(psb, next_psb,
617 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
618 if (psb->cur_iocbq.sli4_xritag == xri) {
619 list_del(&psb->list);
620 psb->status = IOSTAT_SUCCESS;
621 spin_unlock_irqrestore(
622 &phba->sli4_hba.abts_scsi_buf_list_lock,
623 iflag);
624 lpfc_release_scsi_buf_s4(phba, psb);
625 return;
626 }
627 }
628 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
629 iflag);
630}
631
632/**
633 * lpfc_sli4_repost_scsi_sgl_list - Repsot the Scsi buffers sgl pages as block
634 * @phba: pointer to lpfc hba data structure.
635 *
636 * This routine walks the list of scsi buffers that have been allocated and
637 * repost them to the HBA by using SGL block post. This is needed after a
638 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
639 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
640 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
641 *
642 * Returns: 0 = success, non-zero failure.
643 **/
644int
645lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
646{
647 struct lpfc_scsi_buf *psb;
648 int index, status, bcnt = 0, rcnt = 0, rc = 0;
649 LIST_HEAD(sblist);
650
651 for (index = 0; index < phba->sli4_hba.scsi_xri_cnt; index++) {
652 psb = phba->sli4_hba.lpfc_scsi_psb_array[index];
653 if (psb) {
654 /* Remove from SCSI buffer list */
655 list_del(&psb->list);
656 /* Add it to a local SCSI buffer list */
657 list_add_tail(&psb->list, &sblist);
658 if (++rcnt == LPFC_NEMBED_MBOX_SGL_CNT) {
659 bcnt = rcnt;
660 rcnt = 0;
661 }
662 } else
663 /* A hole present in the XRI array, need to skip */
664 bcnt = rcnt;
665
666 if (index == phba->sli4_hba.scsi_xri_cnt - 1)
667 /* End of XRI array for SCSI buffer, complete */
668 bcnt = rcnt;
669
670 /* Continue until collect up to a nembed page worth of sgls */
671 if (bcnt == 0)
672 continue;
673 /* Now, post the SCSI buffer list sgls as a block */
674 status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
675 /* Reset SCSI buffer count for next round of posting */
676 bcnt = 0;
677 while (!list_empty(&sblist)) {
678 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
679 list);
680 if (status) {
681 /* Put this back on the abort scsi list */
682 psb->status = IOSTAT_LOCAL_REJECT;
683 psb->result = IOERR_ABORT_REQUESTED;
684 rc++;
685 } else
686 psb->status = IOSTAT_SUCCESS;
687 /* Put it back into the SCSI buffer list */
688 lpfc_release_scsi_buf_s4(phba, psb);
689 }
690 }
691 return rc;
692}
693
694/**
695 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
696 * @vport: The virtual port for which this call being executed.
697 * @num_to_allocate: The requested number of buffers to allocate.
698 *
699 * This routine allocates a scsi buffer for device with SLI-4 interface spec,
700 * the scsi buffer contains all the necessary information needed to initiate
701 * a SCSI I/O.
702 *
703 * Return codes:
704 * int - number of scsi buffers that were allocated.
705 * 0 = failure, less than num_to_alloc is a partial failure.
706 **/
707static int
708lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
709{
710 struct lpfc_hba *phba = vport->phba;
711 struct lpfc_scsi_buf *psb;
712 struct sli4_sge *sgl;
713 IOCB_t *iocb;
714 dma_addr_t pdma_phys_fcp_cmd;
715 dma_addr_t pdma_phys_fcp_rsp;
716 dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
717 uint16_t iotag, last_xritag = NO_XRI;
718 int status = 0, index;
719 int bcnt;
720 int non_sequential_xri = 0;
721 int rc = 0;
722 LIST_HEAD(sblist);
723
724 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
725 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
726 if (!psb)
727 break;
728
729 /*
730 * Get memory from the pci pool to map the virt space to pci bus
731 * space for an I/O. The DMA buffer includes space for the
732 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
733 * necessary to support the sg_tablesize.
734 */
735 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
736 GFP_KERNEL, &psb->dma_handle);
737 if (!psb->data) {
738 kfree(psb);
739 break;
740 }
741
742 /* Initialize virtual ptrs to dma_buf region. */
743 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
744
745 /* Allocate iotag for psb->cur_iocbq. */
746 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
747 if (iotag == 0) {
748 kfree(psb);
749 break;
750 }
751
752 psb->cur_iocbq.sli4_xritag = lpfc_sli4_next_xritag(phba);
753 if (psb->cur_iocbq.sli4_xritag == NO_XRI) {
754 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
755 psb->data, psb->dma_handle);
756 kfree(psb);
757 break;
758 }
759 if (last_xritag != NO_XRI
760 && psb->cur_iocbq.sli4_xritag != (last_xritag+1)) {
761 non_sequential_xri = 1;
762 } else
763 list_add_tail(&psb->list, &sblist);
764 last_xritag = psb->cur_iocbq.sli4_xritag;
765
766 index = phba->sli4_hba.scsi_xri_cnt++;
767 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
768
769 psb->fcp_bpl = psb->data;
770 psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
771 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
772 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
773 sizeof(struct fcp_cmnd));
774
775 /* Initialize local short-hand pointers. */
776 sgl = (struct sli4_sge *)psb->fcp_bpl;
777 pdma_phys_bpl = psb->dma_handle;
778 pdma_phys_fcp_cmd =
779 (psb->dma_handle + phba->cfg_sg_dma_buf_size)
780 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
781 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
782
783 /*
784 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
785 * are sg list bdes. Initialize the first two and leave the
786 * rest for queuecommand.
787 */
788 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
789 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
790 bf_set(lpfc_sli4_sge_len, sgl, sizeof(struct fcp_cmnd));
791 bf_set(lpfc_sli4_sge_last, sgl, 0);
792 sgl->word2 = cpu_to_le32(sgl->word2);
793 sgl->word3 = cpu_to_le32(sgl->word3);
794 sgl++;
795
796 /* Setup the physical region for the FCP RSP */
797 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
798 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
799 bf_set(lpfc_sli4_sge_len, sgl, sizeof(struct fcp_rsp));
800 bf_set(lpfc_sli4_sge_last, sgl, 1);
801 sgl->word2 = cpu_to_le32(sgl->word2);
802 sgl->word3 = cpu_to_le32(sgl->word3);
803
804 /*
805 * Since the IOCB for the FCP I/O is built into this
806 * lpfc_scsi_buf, initialize it with all known data now.
807 */
808 iocb = &psb->cur_iocbq.iocb;
809 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
810 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
811 /* setting the BLP size to 2 * sizeof BDE may not be correct.
812 * We are setting the bpl to point to out sgl. An sgl's
813 * entries are 16 bytes, a bpl entries are 12 bytes.
814 */
815 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
816 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
817 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
818 iocb->ulpBdeCount = 1;
819 iocb->ulpLe = 1;
820 iocb->ulpClass = CLASS3;
821 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
822 pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
823 else
824 pdma_phys_bpl1 = 0;
825 psb->dma_phys_bpl = pdma_phys_bpl;
826 phba->sli4_hba.lpfc_scsi_psb_array[index] = psb;
827 if (non_sequential_xri) {
828 status = lpfc_sli4_post_sgl(phba, pdma_phys_bpl,
829 pdma_phys_bpl1,
830 psb->cur_iocbq.sli4_xritag);
831 if (status) {
832 /* Put this back on the abort scsi list */
833 psb->status = IOSTAT_LOCAL_REJECT;
834 psb->result = IOERR_ABORT_REQUESTED;
835 rc++;
836 } else
837 psb->status = IOSTAT_SUCCESS;
838 /* Put it back into the SCSI buffer list */
839 lpfc_release_scsi_buf_s4(phba, psb);
840 break;
841 }
842 }
843 if (bcnt) {
844 status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
845 /* Reset SCSI buffer count for next round of posting */
846 while (!list_empty(&sblist)) {
847 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
848 list);
849 if (status) {
850 /* Put this back on the abort scsi list */
851 psb->status = IOSTAT_LOCAL_REJECT;
852 psb->result = IOERR_ABORT_REQUESTED;
853 rc++;
854 } else
855 psb->status = IOSTAT_SUCCESS;
856 /* Put it back into the SCSI buffer list */
857 lpfc_release_scsi_buf_s4(phba, psb);
858 }
859 }
860
861 return bcnt + non_sequential_xri - rc;
862}
863
864/**
James Smart3772a992009-05-22 14:50:54 -0400865 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
866 * @vport: The virtual port for which this call being executed.
867 * @num_to_allocate: The requested number of buffers to allocate.
868 *
869 * This routine wraps the actual SCSI buffer allocator function pointer from
870 * the lpfc_hba struct.
871 *
872 * Return codes:
873 * int - number of scsi buffers that were allocated.
874 * 0 = failure, less than num_to_alloc is a partial failure.
875 **/
876static inline int
877lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
878{
879 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
880}
881
882/**
883 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
884 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -0500885 *
886 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
887 * and returns to caller.
888 *
889 * Return codes:
890 * NULL - Error
891 * Pointer to lpfc_scsi_buf - Success
892 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +0100893static struct lpfc_scsi_buf*
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500894lpfc_get_scsi_buf(struct lpfc_hba * phba)
dea31012005-04-17 16:05:31 -0500895{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400896 struct lpfc_scsi_buf * lpfc_cmd = NULL;
897 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500898 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500899
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500900 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400901 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -0400902 if (lpfc_cmd) {
903 lpfc_cmd->seg_cnt = 0;
904 lpfc_cmd->nonsg_phys = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -0500905 lpfc_cmd->prot_seg_cnt = 0;
James Smart1dcb58e2007-04-25 09:51:30 -0400906 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500907 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400908 return lpfc_cmd;
909}
910
James Smart9bad7672008-12-04 22:39:02 -0500911/**
James Smart3772a992009-05-22 14:50:54 -0400912 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -0500913 * @phba: The Hba for which this call is being executed.
914 * @psb: The scsi buffer which is being released.
915 *
916 * This routine releases @psb scsi buffer by adding it to tail of @phba
917 * lpfc_scsi_buf_list list.
918 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400919static void
James Smart3772a992009-05-22 14:50:54 -0400920lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400921{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500922 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500923
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500924 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400925 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -0500926 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500927 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -0500928}
929
James Smart9bad7672008-12-04 22:39:02 -0500930/**
James Smartda0436e2009-05-22 14:51:39 -0400931 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
932 * @phba: The Hba for which this call is being executed.
933 * @psb: The scsi buffer which is being released.
934 *
935 * This routine releases @psb scsi buffer by adding it to tail of @phba
936 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
937 * and cannot be reused for at least RA_TOV amount of time if it was
938 * aborted.
939 **/
940static void
941lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
942{
943 unsigned long iflag = 0;
944
945 if (psb->status == IOSTAT_LOCAL_REJECT
946 && psb->result == IOERR_ABORT_REQUESTED) {
947 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
948 iflag);
949 psb->pCmd = NULL;
950 list_add_tail(&psb->list,
951 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
952 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
953 iflag);
954 } else {
955
956 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
957 psb->pCmd = NULL;
958 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
959 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
960 }
961}
962
963/**
James Smart3772a992009-05-22 14:50:54 -0400964 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
965 * @phba: The Hba for which this call is being executed.
966 * @psb: The scsi buffer which is being released.
967 *
968 * This routine releases @psb scsi buffer by adding it to tail of @phba
969 * lpfc_scsi_buf_list list.
970 **/
971static void
972lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
973{
974
975 phba->lpfc_release_scsi_buf(phba, psb);
976}
977
978/**
979 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500980 * @phba: The Hba for which this call is being executed.
981 * @lpfc_cmd: The scsi buffer which is going to be mapped.
982 *
983 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -0400984 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
985 * through sg elements and format the bdea. This routine also initializes all
986 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -0500987 *
988 * Return codes:
989 * 1 - Error
990 * 0 - Success
991 **/
dea31012005-04-17 16:05:31 -0500992static int
James Smart3772a992009-05-22 14:50:54 -0400993lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -0500994{
995 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
996 struct scatterlist *sgel = NULL;
997 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
998 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
999 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001000 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001001 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001002 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001003 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001004
1005 /*
1006 * There are three possibilities here - use scatter-gather segment, use
1007 * the single mapping, or neither. Start the lpfc command prep by
1008 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1009 * data bde entry.
1010 */
1011 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001012 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001013 /*
1014 * The driver stores the segment count returned from pci_map_sg
1015 * because this a count of dma-mappings used to map the use_sg
1016 * pages. They are not guaranteed to be the same for those
1017 * architectures that implement an IOMMU.
1018 */
dea31012005-04-17 16:05:31 -05001019
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001020 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1021 scsi_sg_count(scsi_cmnd), datadir);
1022 if (unlikely(!nseg))
1023 return 1;
1024
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001025 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001026 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
1027 printk(KERN_ERR "%s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001028 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001029 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001030 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001031 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001032 return 1;
1033 }
1034
1035 /*
1036 * The driver established a maximum scatter-gather segment count
1037 * during probe that limits the number of sg elements in any
1038 * single scsi command. Just run through the seg_cnt and format
1039 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001040 * When using SLI-3 the driver will try to fit all the BDEs into
1041 * the IOCB. If it can't then the BDEs get added to a BPL as it
1042 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001043 */
James Smart34b02dc2008-08-24 21:49:55 -04001044 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001045 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001046 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001047 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart34b02dc2008-08-24 21:49:55 -04001048 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1049 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1050 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1051 data_bde->addrLow = putPaddrLow(physaddr);
1052 data_bde->addrHigh = putPaddrHigh(physaddr);
1053 data_bde++;
1054 } else {
1055 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1056 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1057 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1058 bpl->addrLow =
1059 le32_to_cpu(putPaddrLow(physaddr));
1060 bpl->addrHigh =
1061 le32_to_cpu(putPaddrHigh(physaddr));
1062 bpl++;
1063 }
dea31012005-04-17 16:05:31 -05001064 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001065 }
dea31012005-04-17 16:05:31 -05001066
1067 /*
1068 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001069 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1070 * explicitly reinitialized and for SLI-3 the extended bde count is
1071 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001072 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001073 if (phba->sli_rev == 3 &&
1074 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
James Smart34b02dc2008-08-24 21:49:55 -04001075 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1076 /*
1077 * The extended IOCB format can only fit 3 BDE or a BPL.
1078 * This I/O has more than 3 BDE so the 1st data bde will
1079 * be a BPL that is filled in here.
1080 */
1081 physaddr = lpfc_cmd->dma_handle;
1082 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1083 data_bde->tus.f.bdeSize = (num_bde *
1084 sizeof(struct ulp_bde64));
1085 physaddr += (sizeof(struct fcp_cmnd) +
1086 sizeof(struct fcp_rsp) +
1087 (2 * sizeof(struct ulp_bde64)));
1088 data_bde->addrHigh = putPaddrHigh(physaddr);
1089 data_bde->addrLow = putPaddrLow(physaddr);
1090 /* ebde count includes the responce bde and data bpl */
1091 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1092 } else {
1093 /* ebde count includes the responce bde and data bdes */
1094 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1095 }
1096 } else {
1097 iocb_cmd->un.fcpi64.bdl.bdeSize =
1098 ((num_bde + 2) * sizeof(struct ulp_bde64));
1099 }
James Smart09372822008-01-11 01:52:54 -05001100 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001101
1102 /*
1103 * Due to difference in data length between DIF/non-DIF paths,
1104 * we need to set word 4 of IOCB here
1105 */
James Smarta257bf92009-04-06 18:48:10 -04001106 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001107 return 0;
1108}
1109
James Smarte2a0a9d2008-12-04 22:40:02 -05001110/*
1111 * Given a scsi cmnd, determine the BlockGuard profile to be used
1112 * with the cmd
1113 */
1114static int
1115lpfc_sc_to_sli_prof(struct scsi_cmnd *sc)
1116{
1117 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
1118 uint8_t ret_prof = LPFC_PROF_INVALID;
1119
1120 if (guard_type == SHOST_DIX_GUARD_IP) {
1121 switch (scsi_get_prot_op(sc)) {
1122 case SCSI_PROT_READ_INSERT:
1123 case SCSI_PROT_WRITE_STRIP:
1124 ret_prof = LPFC_PROF_AST2;
1125 break;
1126
1127 case SCSI_PROT_READ_STRIP:
1128 case SCSI_PROT_WRITE_INSERT:
1129 ret_prof = LPFC_PROF_A1;
1130 break;
1131
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001132 case SCSI_PROT_READ_PASS:
1133 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05001134 ret_prof = LPFC_PROF_AST1;
1135 break;
1136
James Smarte2a0a9d2008-12-04 22:40:02 -05001137 case SCSI_PROT_NORMAL:
1138 default:
1139 printk(KERN_ERR "Bad op/guard:%d/%d combination\n",
1140 scsi_get_prot_op(sc), guard_type);
1141 break;
1142
1143 }
1144 } else if (guard_type == SHOST_DIX_GUARD_CRC) {
1145 switch (scsi_get_prot_op(sc)) {
1146 case SCSI_PROT_READ_STRIP:
1147 case SCSI_PROT_WRITE_INSERT:
1148 ret_prof = LPFC_PROF_A1;
1149 break;
1150
1151 case SCSI_PROT_READ_PASS:
1152 case SCSI_PROT_WRITE_PASS:
1153 ret_prof = LPFC_PROF_C1;
1154 break;
1155
James Smarte2a0a9d2008-12-04 22:40:02 -05001156 case SCSI_PROT_READ_INSERT:
1157 case SCSI_PROT_WRITE_STRIP:
1158 case SCSI_PROT_NORMAL:
1159 default:
1160 printk(KERN_ERR "Bad op/guard:%d/%d combination\n",
1161 scsi_get_prot_op(sc), guard_type);
1162 break;
1163 }
1164 } else {
1165 /* unsupported format */
1166 BUG();
1167 }
1168
1169 return ret_prof;
1170}
1171
1172struct scsi_dif_tuple {
1173 __be16 guard_tag; /* Checksum */
1174 __be16 app_tag; /* Opaque storage */
1175 __be32 ref_tag; /* Target LBA or indirect LBA */
1176};
1177
1178static inline unsigned
1179lpfc_cmd_blksize(struct scsi_cmnd *sc)
1180{
1181 return sc->device->sector_size;
1182}
1183
1184/**
1185 * lpfc_get_cmd_dif_parms - Extract DIF parameters from SCSI command
1186 * @sc: in: SCSI command
James Smart3621a712009-04-06 18:47:14 -04001187 * @apptagmask: out: app tag mask
1188 * @apptagval: out: app tag value
1189 * @reftag: out: ref tag (reference tag)
James Smarte2a0a9d2008-12-04 22:40:02 -05001190 *
1191 * Description:
Martin Olsson98a17082009-04-22 18:21:29 +02001192 * Extract DIF parameters from the command if possible. Otherwise,
1193 * use default parameters.
James Smarte2a0a9d2008-12-04 22:40:02 -05001194 *
1195 **/
1196static inline void
1197lpfc_get_cmd_dif_parms(struct scsi_cmnd *sc, uint16_t *apptagmask,
1198 uint16_t *apptagval, uint32_t *reftag)
1199{
1200 struct scsi_dif_tuple *spt;
1201 unsigned char op = scsi_get_prot_op(sc);
1202 unsigned int protcnt = scsi_prot_sg_count(sc);
1203 static int cnt;
1204
1205 if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001206 op == SCSI_PROT_WRITE_PASS)) {
James Smarte2a0a9d2008-12-04 22:40:02 -05001207
1208 cnt++;
1209 spt = page_address(sg_page(scsi_prot_sglist(sc))) +
1210 scsi_prot_sglist(sc)[0].offset;
1211 *apptagmask = 0;
1212 *apptagval = 0;
1213 *reftag = cpu_to_be32(spt->ref_tag);
1214
1215 } else {
1216 /* SBC defines ref tag to be lower 32bits of LBA */
1217 *reftag = (uint32_t) (0xffffffff & scsi_get_lba(sc));
1218 *apptagmask = 0;
1219 *apptagval = 0;
1220 }
1221}
1222
1223/*
1224 * This function sets up buffer list for protection groups of
1225 * type LPFC_PG_TYPE_NO_DIF
1226 *
1227 * This is usually used when the HBA is instructed to generate
1228 * DIFs and insert them into data stream (or strip DIF from
1229 * incoming data stream)
1230 *
1231 * The buffer list consists of just one protection group described
1232 * below:
1233 * +-------------------------+
1234 * start of prot group --> | PDE_1 |
1235 * +-------------------------+
1236 * | Data BDE |
1237 * +-------------------------+
1238 * |more Data BDE's ... (opt)|
1239 * +-------------------------+
1240 *
1241 * @sc: pointer to scsi command we're working on
1242 * @bpl: pointer to buffer list for protection groups
1243 * @datacnt: number of segments of data that have been dma mapped
1244 *
1245 * Note: Data s/g buffers have been dma mapped
1246 */
1247static int
1248lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1249 struct ulp_bde64 *bpl, int datasegcnt)
1250{
1251 struct scatterlist *sgde = NULL; /* s/g data entry */
1252 struct lpfc_pde *pde1 = NULL;
1253 dma_addr_t physaddr;
1254 int i = 0, num_bde = 0;
1255 int datadir = sc->sc_data_direction;
1256 int prof = LPFC_PROF_INVALID;
1257 unsigned blksize;
1258 uint32_t reftag;
1259 uint16_t apptagmask, apptagval;
1260
1261 pde1 = (struct lpfc_pde *) bpl;
1262 prof = lpfc_sc_to_sli_prof(sc);
1263
1264 if (prof == LPFC_PROF_INVALID)
1265 goto out;
1266
1267 /* extract some info from the scsi command for PDE1*/
1268 blksize = lpfc_cmd_blksize(sc);
1269 lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1270
1271 /* setup PDE1 with what we have */
1272 lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
1273 BG_EC_STOP_ERR);
1274 lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
1275
1276 num_bde++;
1277 bpl++;
1278
1279 /* assumption: caller has already run dma_map_sg on command data */
1280 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1281 physaddr = sg_dma_address(sgde);
1282 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1283 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1284 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1285 if (datadir == DMA_TO_DEVICE)
1286 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1287 else
1288 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1289 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1290 bpl++;
1291 num_bde++;
1292 }
1293
1294out:
1295 return num_bde;
1296}
1297
1298/*
1299 * This function sets up buffer list for protection groups of
1300 * type LPFC_PG_TYPE_DIF_BUF
1301 *
1302 * This is usually used when DIFs are in their own buffers,
1303 * separate from the data. The HBA can then by instructed
1304 * to place the DIFs in the outgoing stream. For read operations,
1305 * The HBA could extract the DIFs and place it in DIF buffers.
1306 *
1307 * The buffer list for this type consists of one or more of the
1308 * protection groups described below:
1309 * +-------------------------+
1310 * start of first prot group --> | PDE_1 |
1311 * +-------------------------+
1312 * | PDE_3 (Prot BDE) |
1313 * +-------------------------+
1314 * | Data BDE |
1315 * +-------------------------+
1316 * |more Data BDE's ... (opt)|
1317 * +-------------------------+
1318 * start of new prot group --> | PDE_1 |
1319 * +-------------------------+
1320 * | ... |
1321 * +-------------------------+
1322 *
1323 * @sc: pointer to scsi command we're working on
1324 * @bpl: pointer to buffer list for protection groups
1325 * @datacnt: number of segments of data that have been dma mapped
1326 * @protcnt: number of segment of protection data that have been dma mapped
1327 *
1328 * Note: It is assumed that both data and protection s/g buffers have been
1329 * mapped for DMA
1330 */
1331static int
1332lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1333 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1334{
1335 struct scatterlist *sgde = NULL; /* s/g data entry */
1336 struct scatterlist *sgpe = NULL; /* s/g prot entry */
1337 struct lpfc_pde *pde1 = NULL;
1338 struct ulp_bde64 *prot_bde = NULL;
1339 dma_addr_t dataphysaddr, protphysaddr;
1340 unsigned short curr_data = 0, curr_prot = 0;
1341 unsigned int split_offset, protgroup_len;
1342 unsigned int protgrp_blks, protgrp_bytes;
1343 unsigned int remainder, subtotal;
1344 int prof = LPFC_PROF_INVALID;
1345 int datadir = sc->sc_data_direction;
1346 unsigned char pgdone = 0, alldone = 0;
1347 unsigned blksize;
1348 uint32_t reftag;
1349 uint16_t apptagmask, apptagval;
1350 int num_bde = 0;
1351
1352 sgpe = scsi_prot_sglist(sc);
1353 sgde = scsi_sglist(sc);
1354
1355 if (!sgpe || !sgde) {
1356 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1357 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
1358 sgpe, sgde);
1359 return 0;
1360 }
1361
1362 prof = lpfc_sc_to_sli_prof(sc);
1363 if (prof == LPFC_PROF_INVALID)
1364 goto out;
1365
1366 /* extract some info from the scsi command for PDE1*/
1367 blksize = lpfc_cmd_blksize(sc);
1368 lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1369
1370 split_offset = 0;
1371 do {
1372 /* setup the first PDE_1 */
1373 pde1 = (struct lpfc_pde *) bpl;
1374
1375 lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
1376 BG_EC_STOP_ERR);
1377 lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
1378
1379 num_bde++;
1380 bpl++;
1381
1382 /* setup the first BDE that points to protection buffer */
1383 prot_bde = (struct ulp_bde64 *) bpl;
1384 protphysaddr = sg_dma_address(sgpe);
1385 prot_bde->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
1386 prot_bde->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1387 protgroup_len = sg_dma_len(sgpe);
1388
1389
1390 /* must be integer multiple of the DIF block length */
1391 BUG_ON(protgroup_len % 8);
1392
1393 protgrp_blks = protgroup_len / 8;
1394 protgrp_bytes = protgrp_blks * blksize;
1395
1396 prot_bde->tus.f.bdeSize = protgroup_len;
1397 if (datadir == DMA_TO_DEVICE)
1398 prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1399 else
1400 prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1401 prot_bde->tus.w = le32_to_cpu(bpl->tus.w);
1402
1403 curr_prot++;
1404 num_bde++;
1405
1406 /* setup BDE's for data blocks associated with DIF data */
1407 pgdone = 0;
1408 subtotal = 0; /* total bytes processed for current prot grp */
1409 while (!pgdone) {
1410 if (!sgde) {
1411 printk(KERN_ERR "%s Invalid data segment\n",
1412 __func__);
1413 return 0;
1414 }
1415 bpl++;
1416 dataphysaddr = sg_dma_address(sgde) + split_offset;
1417 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1418 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1419
1420 remainder = sg_dma_len(sgde) - split_offset;
1421
1422 if ((subtotal + remainder) <= protgrp_bytes) {
1423 /* we can use this whole buffer */
1424 bpl->tus.f.bdeSize = remainder;
1425 split_offset = 0;
1426
1427 if ((subtotal + remainder) == protgrp_bytes)
1428 pgdone = 1;
1429 } else {
1430 /* must split this buffer with next prot grp */
1431 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1432 split_offset += bpl->tus.f.bdeSize;
1433 }
1434
1435 subtotal += bpl->tus.f.bdeSize;
1436
1437 if (datadir == DMA_TO_DEVICE)
1438 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1439 else
1440 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1441 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1442
1443 num_bde++;
1444 curr_data++;
1445
1446 if (split_offset)
1447 break;
1448
1449 /* Move to the next s/g segment if possible */
1450 sgde = sg_next(sgde);
1451 }
1452
1453 /* are we done ? */
1454 if (curr_prot == protcnt) {
1455 alldone = 1;
1456 } else if (curr_prot < protcnt) {
1457 /* advance to next prot buffer */
1458 sgpe = sg_next(sgpe);
1459 bpl++;
1460
1461 /* update the reference tag */
1462 reftag += protgrp_blks;
1463 } else {
1464 /* if we're here, we have a bug */
1465 printk(KERN_ERR "BLKGRD: bug in %s\n", __func__);
1466 }
1467
1468 } while (!alldone);
1469
1470out:
1471
1472
1473 return num_bde;
1474}
1475/*
1476 * Given a SCSI command that supports DIF, determine composition of protection
1477 * groups involved in setting up buffer lists
1478 *
1479 * Returns:
1480 * for DIF (for both read and write)
1481 * */
1482static int
1483lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
1484{
1485 int ret = LPFC_PG_TYPE_INVALID;
1486 unsigned char op = scsi_get_prot_op(sc);
1487
1488 switch (op) {
1489 case SCSI_PROT_READ_STRIP:
1490 case SCSI_PROT_WRITE_INSERT:
1491 ret = LPFC_PG_TYPE_NO_DIF;
1492 break;
1493 case SCSI_PROT_READ_INSERT:
1494 case SCSI_PROT_WRITE_STRIP:
1495 case SCSI_PROT_READ_PASS:
1496 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05001497 ret = LPFC_PG_TYPE_DIF_BUF;
1498 break;
1499 default:
1500 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1501 "9021 Unsupported protection op:%d\n", op);
1502 break;
1503 }
1504
1505 return ret;
1506}
1507
1508/*
1509 * This is the protection/DIF aware version of
1510 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
1511 * two functions eventually, but for now, it's here
1512 */
1513static int
1514lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba,
1515 struct lpfc_scsi_buf *lpfc_cmd)
1516{
1517 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1518 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1519 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1520 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1521 uint32_t num_bde = 0;
1522 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
1523 int prot_group_type = 0;
1524 int diflen, fcpdl;
1525 unsigned blksize;
1526
1527 /*
1528 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
1529 * fcp_rsp regions to the first data bde entry
1530 */
1531 bpl += 2;
1532 if (scsi_sg_count(scsi_cmnd)) {
1533 /*
1534 * The driver stores the segment count returned from pci_map_sg
1535 * because this a count of dma-mappings used to map the use_sg
1536 * pages. They are not guaranteed to be the same for those
1537 * architectures that implement an IOMMU.
1538 */
1539 datasegcnt = dma_map_sg(&phba->pcidev->dev,
1540 scsi_sglist(scsi_cmnd),
1541 scsi_sg_count(scsi_cmnd), datadir);
1542 if (unlikely(!datasegcnt))
1543 return 1;
1544
1545 lpfc_cmd->seg_cnt = datasegcnt;
1546 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
1547 printk(KERN_ERR "%s: Too many sg segments from "
1548 "dma_map_sg. Config %d, seg_cnt %d\n",
1549 __func__, phba->cfg_sg_seg_cnt,
1550 lpfc_cmd->seg_cnt);
1551 scsi_dma_unmap(scsi_cmnd);
1552 return 1;
1553 }
1554
1555 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
1556
1557 switch (prot_group_type) {
1558 case LPFC_PG_TYPE_NO_DIF:
1559 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
1560 datasegcnt);
1561 /* we shoud have 2 or more entries in buffer list */
1562 if (num_bde < 2)
1563 goto err;
1564 break;
1565 case LPFC_PG_TYPE_DIF_BUF:{
1566 /*
1567 * This type indicates that protection buffers are
1568 * passed to the driver, so that needs to be prepared
1569 * for DMA
1570 */
1571 protsegcnt = dma_map_sg(&phba->pcidev->dev,
1572 scsi_prot_sglist(scsi_cmnd),
1573 scsi_prot_sg_count(scsi_cmnd), datadir);
1574 if (unlikely(!protsegcnt)) {
1575 scsi_dma_unmap(scsi_cmnd);
1576 return 1;
1577 }
1578
1579 lpfc_cmd->prot_seg_cnt = protsegcnt;
1580 if (lpfc_cmd->prot_seg_cnt
1581 > phba->cfg_prot_sg_seg_cnt) {
1582 printk(KERN_ERR "%s: Too many prot sg segments "
1583 "from dma_map_sg. Config %d,"
1584 "prot_seg_cnt %d\n", __func__,
1585 phba->cfg_prot_sg_seg_cnt,
1586 lpfc_cmd->prot_seg_cnt);
1587 dma_unmap_sg(&phba->pcidev->dev,
1588 scsi_prot_sglist(scsi_cmnd),
1589 scsi_prot_sg_count(scsi_cmnd),
1590 datadir);
1591 scsi_dma_unmap(scsi_cmnd);
1592 return 1;
1593 }
1594
1595 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
1596 datasegcnt, protsegcnt);
1597 /* we shoud have 3 or more entries in buffer list */
1598 if (num_bde < 3)
1599 goto err;
1600 break;
1601 }
1602 case LPFC_PG_TYPE_INVALID:
1603 default:
1604 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1605 "9022 Unexpected protection group %i\n",
1606 prot_group_type);
1607 return 1;
1608 }
1609 }
1610
1611 /*
1612 * Finish initializing those IOCB fields that are dependent on the
1613 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
1614 * reinitialized since all iocb memory resources are used many times
1615 * for transmit, receive, and continuation bpl's.
1616 */
1617 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
1618 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
1619 iocb_cmd->ulpBdeCount = 1;
1620 iocb_cmd->ulpLe = 1;
1621
1622 fcpdl = scsi_bufflen(scsi_cmnd);
1623
1624 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
1625 /*
1626 * We are in DIF Type 1 mode
1627 * Every data block has a 8 byte DIF (trailer)
1628 * attached to it. Must ajust FCP data length
1629 */
1630 blksize = lpfc_cmd_blksize(scsi_cmnd);
1631 diflen = (fcpdl / blksize) * 8;
1632 fcpdl += diflen;
1633 }
1634 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
1635
1636 /*
1637 * Due to difference in data length between DIF/non-DIF paths,
1638 * we need to set word 4 of IOCB here
1639 */
1640 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
1641
1642 return 0;
1643err:
1644 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1645 "9023 Could not setup all needed BDE's"
1646 "prot_group_type=%d, num_bde=%d\n",
1647 prot_group_type, num_bde);
1648 return 1;
1649}
1650
1651/*
1652 * This function checks for BlockGuard errors detected by
1653 * the HBA. In case of errors, the ASC/ASCQ fields in the
1654 * sense buffer will be set accordingly, paired with
1655 * ILLEGAL_REQUEST to signal to the kernel that the HBA
1656 * detected corruption.
1657 *
1658 * Returns:
1659 * 0 - No error found
1660 * 1 - BlockGuard error found
1661 * -1 - Internal error (bad profile, ...etc)
1662 */
1663static int
1664lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
1665 struct lpfc_iocbq *pIocbOut)
1666{
1667 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
1668 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
1669 int ret = 0;
1670 uint32_t bghm = bgf->bghm;
1671 uint32_t bgstat = bgf->bgstat;
1672 uint64_t failing_sector = 0;
1673
Tejun Heo83096eb2009-05-07 22:24:39 +09001674 printk(KERN_ERR "BG ERROR in cmd 0x%x lba 0x%llx blk cnt 0x%x "
James Smarte2a0a9d2008-12-04 22:40:02 -05001675 "bgstat=0x%x bghm=0x%x\n",
James Smart87b5c322008-12-16 10:34:09 -05001676 cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
Tejun Heo83096eb2009-05-07 22:24:39 +09001677 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05001678
1679 spin_lock(&_dump_buf_lock);
1680 if (!_dump_buf_done) {
1681 printk(KERN_ERR "Saving Data for %u blocks to debugfs\n",
1682 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
1683 lpfc_debug_save_data(cmd);
1684
1685 /* If we have a prot sgl, save the DIF buffer */
1686 if (lpfc_prot_group_type(phba, cmd) ==
1687 LPFC_PG_TYPE_DIF_BUF) {
1688 printk(KERN_ERR "Saving DIF for %u blocks to debugfs\n",
1689 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
1690 lpfc_debug_save_dif(cmd);
1691 }
1692
1693 _dump_buf_done = 1;
1694 }
1695 spin_unlock(&_dump_buf_lock);
1696
1697 if (lpfc_bgs_get_invalid_prof(bgstat)) {
1698 cmd->result = ScsiResult(DID_ERROR, 0);
1699 printk(KERN_ERR "Invalid BlockGuard profile. bgstat:0x%x\n",
1700 bgstat);
1701 ret = (-1);
1702 goto out;
1703 }
1704
1705 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
1706 cmd->result = ScsiResult(DID_ERROR, 0);
1707 printk(KERN_ERR "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
1708 bgstat);
1709 ret = (-1);
1710 goto out;
1711 }
1712
1713 if (lpfc_bgs_get_guard_err(bgstat)) {
1714 ret = 1;
1715
1716 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1717 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001718 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001719 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1720 phba->bg_guard_err_cnt++;
1721 printk(KERN_ERR "BLKGRD: guard_tag error\n");
1722 }
1723
1724 if (lpfc_bgs_get_reftag_err(bgstat)) {
1725 ret = 1;
1726
1727 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1728 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001729 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001730 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1731
1732 phba->bg_reftag_err_cnt++;
1733 printk(KERN_ERR "BLKGRD: ref_tag error\n");
1734 }
1735
1736 if (lpfc_bgs_get_apptag_err(bgstat)) {
1737 ret = 1;
1738
1739 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1740 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001741 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001742 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1743
1744 phba->bg_apptag_err_cnt++;
1745 printk(KERN_ERR "BLKGRD: app_tag error\n");
1746 }
1747
1748 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
1749 /*
1750 * setup sense data descriptor 0 per SPC-4 as an information
1751 * field, and put the failing LBA in it
1752 */
1753 cmd->sense_buffer[8] = 0; /* Information */
1754 cmd->sense_buffer[9] = 0xa; /* Add. length */
David Howells2344b5b2009-04-14 17:08:34 +01001755 bghm /= cmd->device->sector_size;
James Smarte2a0a9d2008-12-04 22:40:02 -05001756
1757 failing_sector = scsi_get_lba(cmd);
1758 failing_sector += bghm;
1759
1760 put_unaligned_be64(failing_sector, &cmd->sense_buffer[10]);
1761 }
1762
1763 if (!ret) {
1764 /* No error was reported - problem in FW? */
1765 cmd->result = ScsiResult(DID_ERROR, 0);
1766 printk(KERN_ERR "BLKGRD: no errors reported!\n");
1767 }
1768
1769out:
1770 return ret;
1771}
1772
James Smartea2151b2008-09-07 11:52:10 -04001773/**
James Smartda0436e2009-05-22 14:51:39 -04001774 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
1775 * @phba: The Hba for which this call is being executed.
1776 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1777 *
1778 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
1779 * field of @lpfc_cmd for device with SLI-4 interface spec.
1780 *
1781 * Return codes:
1782 * 1 - Error
1783 * 0 - Success
1784 **/
1785static int
1786lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1787{
1788 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1789 struct scatterlist *sgel = NULL;
1790 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1791 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
1792 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1793 dma_addr_t physaddr;
1794 uint32_t num_bde = 0;
1795 uint32_t dma_len;
1796 uint32_t dma_offset = 0;
1797 int nseg;
1798
1799 /*
1800 * There are three possibilities here - use scatter-gather segment, use
1801 * the single mapping, or neither. Start the lpfc command prep by
1802 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1803 * data bde entry.
1804 */
1805 if (scsi_sg_count(scsi_cmnd)) {
1806 /*
1807 * The driver stores the segment count returned from pci_map_sg
1808 * because this a count of dma-mappings used to map the use_sg
1809 * pages. They are not guaranteed to be the same for those
1810 * architectures that implement an IOMMU.
1811 */
1812
1813 nseg = scsi_dma_map(scsi_cmnd);
1814 if (unlikely(!nseg))
1815 return 1;
1816 sgl += 1;
1817 /* clear the last flag in the fcp_rsp map entry */
1818 sgl->word2 = le32_to_cpu(sgl->word2);
1819 bf_set(lpfc_sli4_sge_last, sgl, 0);
1820 sgl->word2 = cpu_to_le32(sgl->word2);
1821 sgl += 1;
1822
1823 lpfc_cmd->seg_cnt = nseg;
1824 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
1825 printk(KERN_ERR "%s: Too many sg segments from "
1826 "dma_map_sg. Config %d, seg_cnt %d\n",
1827 __func__, phba->cfg_sg_seg_cnt,
1828 lpfc_cmd->seg_cnt);
1829 scsi_dma_unmap(scsi_cmnd);
1830 return 1;
1831 }
1832
1833 /*
1834 * The driver established a maximum scatter-gather segment count
1835 * during probe that limits the number of sg elements in any
1836 * single scsi command. Just run through the seg_cnt and format
1837 * the sge's.
1838 * When using SLI-3 the driver will try to fit all the BDEs into
1839 * the IOCB. If it can't then the BDEs get added to a BPL as it
1840 * does for SLI-2 mode.
1841 */
1842 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
1843 physaddr = sg_dma_address(sgel);
1844 dma_len = sg_dma_len(sgel);
1845 bf_set(lpfc_sli4_sge_len, sgl, sg_dma_len(sgel));
1846 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
1847 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
1848 if ((num_bde + 1) == nseg)
1849 bf_set(lpfc_sli4_sge_last, sgl, 1);
1850 else
1851 bf_set(lpfc_sli4_sge_last, sgl, 0);
1852 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
1853 sgl->word2 = cpu_to_le32(sgl->word2);
1854 sgl->word3 = cpu_to_le32(sgl->word3);
1855 dma_offset += dma_len;
1856 sgl++;
1857 }
1858 } else {
1859 sgl += 1;
1860 /* clear the last flag in the fcp_rsp map entry */
1861 sgl->word2 = le32_to_cpu(sgl->word2);
1862 bf_set(lpfc_sli4_sge_last, sgl, 1);
1863 sgl->word2 = cpu_to_le32(sgl->word2);
1864 }
1865
1866 /*
1867 * Finish initializing those IOCB fields that are dependent on the
1868 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1869 * explicitly reinitialized.
1870 * all iocb memory resources are reused.
1871 */
1872 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
1873
1874 /*
1875 * Due to difference in data length between DIF/non-DIF paths,
1876 * we need to set word 4 of IOCB here
1877 */
1878 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
1879 return 0;
1880}
1881
1882/**
James Smart3772a992009-05-22 14:50:54 -04001883 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
1884 * @phba: The Hba for which this call is being executed.
1885 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1886 *
1887 * This routine wraps the actual DMA mapping function pointer from the
1888 * lpfc_hba struct.
1889 *
1890 * Return codes:
1891 * 1 - Error
1892 * 0 - Success
1893 **/
1894static inline int
1895lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1896{
1897 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
1898}
1899
1900/**
James Smart3621a712009-04-06 18:47:14 -04001901 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04001902 * @phba: Pointer to hba context object.
1903 * @vport: Pointer to vport object.
1904 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
1905 * @rsp_iocb: Pointer to response iocb object which reported error.
1906 *
1907 * This function posts an event when there is a SCSI command reporting
1908 * error from the scsi device.
1909 **/
1910static void
1911lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
1912 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
1913 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
1914 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
1915 uint32_t resp_info = fcprsp->rspStatus2;
1916 uint32_t scsi_status = fcprsp->rspStatus3;
1917 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
1918 struct lpfc_fast_path_event *fast_path_evt = NULL;
1919 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
1920 unsigned long flags;
1921
1922 /* If there is queuefull or busy condition send a scsi event */
1923 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
1924 (cmnd->result == SAM_STAT_BUSY)) {
1925 fast_path_evt = lpfc_alloc_fast_evt(phba);
1926 if (!fast_path_evt)
1927 return;
1928 fast_path_evt->un.scsi_evt.event_type =
1929 FC_REG_SCSI_EVENT;
1930 fast_path_evt->un.scsi_evt.subcategory =
1931 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
1932 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
1933 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
1934 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
1935 &pnode->nlp_portname, sizeof(struct lpfc_name));
1936 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
1937 &pnode->nlp_nodename, sizeof(struct lpfc_name));
1938 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
1939 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
1940 fast_path_evt = lpfc_alloc_fast_evt(phba);
1941 if (!fast_path_evt)
1942 return;
1943 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
1944 FC_REG_SCSI_EVENT;
1945 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
1946 LPFC_EVENT_CHECK_COND;
1947 fast_path_evt->un.check_cond_evt.scsi_event.lun =
1948 cmnd->device->lun;
1949 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
1950 &pnode->nlp_portname, sizeof(struct lpfc_name));
1951 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
1952 &pnode->nlp_nodename, sizeof(struct lpfc_name));
1953 fast_path_evt->un.check_cond_evt.sense_key =
1954 cmnd->sense_buffer[2] & 0xf;
1955 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
1956 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
1957 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
1958 fcpi_parm &&
1959 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
1960 ((scsi_status == SAM_STAT_GOOD) &&
1961 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
1962 /*
1963 * If status is good or resid does not match with fcp_param and
1964 * there is valid fcpi_parm, then there is a read_check error
1965 */
1966 fast_path_evt = lpfc_alloc_fast_evt(phba);
1967 if (!fast_path_evt)
1968 return;
1969 fast_path_evt->un.read_check_error.header.event_type =
1970 FC_REG_FABRIC_EVENT;
1971 fast_path_evt->un.read_check_error.header.subcategory =
1972 LPFC_EVENT_FCPRDCHKERR;
1973 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
1974 &pnode->nlp_portname, sizeof(struct lpfc_name));
1975 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
1976 &pnode->nlp_nodename, sizeof(struct lpfc_name));
1977 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
1978 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
1979 fast_path_evt->un.read_check_error.fcpiparam =
1980 fcpi_parm;
1981 } else
1982 return;
1983
1984 fast_path_evt->vport = vport;
1985 spin_lock_irqsave(&phba->hbalock, flags);
1986 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
1987 spin_unlock_irqrestore(&phba->hbalock, flags);
1988 lpfc_worker_wake_up(phba);
1989 return;
1990}
James Smart9bad7672008-12-04 22:39:02 -05001991
1992/**
James Smartf1126682009-06-10 17:22:44 -04001993 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04001994 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05001995 * @psb: The scsi buffer which is going to be un-mapped.
1996 *
1997 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04001998 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05001999 **/
dea31012005-04-17 16:05:31 -05002000static void
James Smartf1126682009-06-10 17:22:44 -04002001lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04002002{
2003 /*
2004 * There are only two special cases to consider. (1) the scsi command
2005 * requested scatter-gather usage or (2) the scsi command allocated
2006 * a request buffer, but did not request use_sg. There is a third
2007 * case, but it does not require resource deallocation.
2008 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002009 if (psb->seg_cnt > 0)
2010 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002011 if (psb->prot_seg_cnt > 0)
2012 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
2013 scsi_prot_sg_count(psb->pCmd),
2014 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04002015}
2016
James Smart9bad7672008-12-04 22:39:02 -05002017/**
James Smart3621a712009-04-06 18:47:14 -04002018 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05002019 * @vport: The virtual port for which this call is being executed.
2020 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2021 * @rsp_iocb: The response IOCB which contains FCP error.
2022 *
2023 * This routine is called to process response IOCB with status field
2024 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
2025 * based upon SCSI and FCP error.
2026 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04002027static void
James Smart2e0fef82007-06-17 19:56:36 -05002028lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2029 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05002030{
2031 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2032 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
2033 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04002034 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05002035 uint32_t resp_info = fcprsp->rspStatus2;
2036 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05002037 uint32_t *lp;
dea31012005-04-17 16:05:31 -05002038 uint32_t host_status = DID_OK;
2039 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05002040 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05002041
James Smartea2151b2008-09-07 11:52:10 -04002042
dea31012005-04-17 16:05:31 -05002043 /*
2044 * If this is a task management command, there is no
2045 * scsi packet associated with this lpfc_cmd. The driver
2046 * consumes it.
2047 */
2048 if (fcpcmd->fcpCntl2) {
2049 scsi_status = 0;
2050 goto out;
2051 }
2052
James Smartc7743952006-12-02 13:34:42 -05002053 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
2054 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
2055 if (snslen > SCSI_SENSE_BUFFERSIZE)
2056 snslen = SCSI_SENSE_BUFFERSIZE;
2057
2058 if (resp_info & RSP_LEN_VALID)
2059 rsplen = be32_to_cpu(fcprsp->rspRspLen);
2060 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
2061 }
2062 lp = (uint32_t *)cmnd->sense_buffer;
2063
2064 if (!scsi_status && (resp_info & RESID_UNDER))
2065 logit = LOG_FCP;
2066
James Smarte8b62012007-08-02 11:10:09 -04002067 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05002068 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04002069 "Data: x%x x%x x%x x%x x%x\n",
2070 cmnd->cmnd[0], scsi_status,
2071 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
2072 be32_to_cpu(fcprsp->rspResId),
2073 be32_to_cpu(fcprsp->rspSnsLen),
2074 be32_to_cpu(fcprsp->rspRspLen),
2075 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05002076
2077 if (resp_info & RSP_LEN_VALID) {
2078 rsplen = be32_to_cpu(fcprsp->rspRspLen);
2079 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
2080 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
2081 host_status = DID_ERROR;
2082 goto out;
2083 }
2084 }
2085
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002086 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05002087 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002088 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05002089
James Smarte8b62012007-08-02 11:10:09 -04002090 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002091 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04002092 "residual %d Data: x%x x%x x%x\n",
2093 be32_to_cpu(fcpcmd->fcpDl),
2094 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
2095 cmnd->underflow);
dea31012005-04-17 16:05:31 -05002096
2097 /*
James Smart7054a602007-04-25 09:52:34 -04002098 * If there is an under run check if under run reported by
2099 * storage array is same as the under run reported by HBA.
2100 * If this is not same, there is a dropped frame.
2101 */
2102 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2103 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002104 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04002105 lpfc_printf_vlog(vport, KERN_WARNING,
2106 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05002107 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04002108 "and Underrun Data: x%x x%x x%x x%x\n",
2109 be32_to_cpu(fcpcmd->fcpDl),
2110 scsi_get_resid(cmnd), fcpi_parm,
2111 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002112 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04002113 host_status = DID_ERROR;
2114 }
2115 /*
dea31012005-04-17 16:05:31 -05002116 * The cmnd->underflow is the minimum number of bytes that must
2117 * be transfered for this command. Provided a sense condition
2118 * is not present, make sure the actual amount transferred is at
2119 * least the underflow value or fail.
2120 */
2121 if (!(resp_info & SNS_LEN_VALID) &&
2122 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002123 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
2124 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04002125 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002126 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04002127 "underrun converted to error "
2128 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04002129 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04002130 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05002131 host_status = DID_ERROR;
2132 }
2133 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04002134 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002135 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04002136 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04002137 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05002138 host_status = DID_ERROR;
2139
2140 /*
2141 * Check SLI validation that all the transfer was actually done
2142 * (fcpi_parm should be zero). Apply check only to reads.
2143 */
2144 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
2145 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -04002146 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05002147 "9029 FCP Read Check Error Data: "
James Smarte8b62012007-08-02 11:10:09 -04002148 "x%x x%x x%x x%x\n",
2149 be32_to_cpu(fcpcmd->fcpDl),
2150 be32_to_cpu(fcprsp->rspResId),
2151 fcpi_parm, cmnd->cmnd[0]);
dea31012005-04-17 16:05:31 -05002152 host_status = DID_ERROR;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002153 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05002154 }
2155
2156 out:
2157 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04002158 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05002159}
2160
James Smart9bad7672008-12-04 22:39:02 -05002161/**
James Smart3621a712009-04-06 18:47:14 -04002162 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05002163 * @phba: The Hba for which this call is being executed.
2164 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04002165 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05002166 *
2167 * This routine assigns scsi command result by looking into response IOCB
2168 * status field appropriately. This routine handles QUEUE FULL condition as
2169 * well by ramping down device queue depth.
2170 **/
dea31012005-04-17 16:05:31 -05002171static void
2172lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
2173 struct lpfc_iocbq *pIocbOut)
2174{
2175 struct lpfc_scsi_buf *lpfc_cmd =
2176 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002177 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05002178 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
2179 struct lpfc_nodelist *pnode = rdata->pnode;
2180 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002181 int result;
James Smarta257bf92009-04-06 18:48:10 -04002182 struct scsi_device *tmp_sdev;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002183 int depth = 0;
James Smartfa61a542008-01-11 01:52:42 -05002184 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04002185 struct lpfc_fast_path_event *fast_path_evt;
James Smarta257bf92009-04-06 18:48:10 -04002186 struct Scsi_Host *shost = cmd->device->host;
2187 uint32_t queue_depth, scsi_id;
dea31012005-04-17 16:05:31 -05002188
2189 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
2190 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart109f6ed2008-12-04 22:39:08 -05002191 if (pnode && NLP_CHK_NODE_ACT(pnode))
2192 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05002193
2194 if (lpfc_cmd->status) {
2195 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
2196 (lpfc_cmd->result & IOERR_DRVR_MASK))
2197 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
2198 else if (lpfc_cmd->status >= IOSTAT_CNT)
2199 lpfc_cmd->status = IOSTAT_DEFAULT;
2200
James Smarte8b62012007-08-02 11:10:09 -04002201 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002202 "9030 FCP cmd x%x failed <%d/%d> "
James Smarte8b62012007-08-02 11:10:09 -04002203 "status: x%x result: x%x Data: x%x x%x\n",
2204 cmd->cmnd[0],
2205 cmd->device ? cmd->device->id : 0xffff,
2206 cmd->device ? cmd->device->lun : 0xffff,
2207 lpfc_cmd->status, lpfc_cmd->result,
2208 pIocbOut->iocb.ulpContext,
2209 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05002210
2211 switch (lpfc_cmd->status) {
2212 case IOSTAT_FCP_RSP_ERROR:
2213 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05002214 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05002215 break;
2216 case IOSTAT_NPORT_BSY:
2217 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04002218 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04002219 fast_path_evt = lpfc_alloc_fast_evt(phba);
2220 if (!fast_path_evt)
2221 break;
2222 fast_path_evt->un.fabric_evt.event_type =
2223 FC_REG_FABRIC_EVENT;
2224 fast_path_evt->un.fabric_evt.subcategory =
2225 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
2226 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
2227 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2228 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
2229 &pnode->nlp_portname,
2230 sizeof(struct lpfc_name));
2231 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
2232 &pnode->nlp_nodename,
2233 sizeof(struct lpfc_name));
2234 }
2235 fast_path_evt->vport = vport;
2236 fast_path_evt->work_evt.evt =
2237 LPFC_EVT_FASTPATH_MGMT_EVT;
2238 spin_lock_irqsave(&phba->hbalock, flags);
2239 list_add_tail(&fast_path_evt->work_evt.evt_listp,
2240 &phba->work_list);
2241 spin_unlock_irqrestore(&phba->hbalock, flags);
2242 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05002243 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002244 case IOSTAT_LOCAL_REJECT:
James Smartd7c255b2008-08-24 21:50:00 -04002245 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05002246 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartd7c255b2008-08-24 21:50:00 -04002247 lpfc_cmd->result == IOERR_ABORT_REQUESTED) {
James Smart92d7f7b2007-06-17 19:56:38 -05002248 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04002249 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05002250 }
2251
2252 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
2253 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
2254 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
2255 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
2256 /*
2257 * This is a response for a BG enabled
2258 * cmd. Parse BG error
2259 */
2260 lpfc_parse_bg_err(phba, lpfc_cmd,
2261 pIocbOut);
2262 break;
2263 } else {
2264 lpfc_printf_vlog(vport, KERN_WARNING,
2265 LOG_BG,
2266 "9031 non-zero BGSTAT "
2267 "on unprotected cmd");
2268 }
2269 }
2270
2271 /* else: fall through */
dea31012005-04-17 16:05:31 -05002272 default:
2273 cmd->result = ScsiResult(DID_ERROR, 0);
2274 break;
2275 }
2276
James Smart58da1ff2008-04-07 10:15:56 -04002277 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002278 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04002279 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
2280 SAM_STAT_BUSY);
dea31012005-04-17 16:05:31 -05002281 } else {
2282 cmd->result = ScsiResult(DID_OK, 0);
2283 }
2284
2285 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
2286 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
2287
James Smarte8b62012007-08-02 11:10:09 -04002288 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2289 "0710 Iodone <%d/%d> cmd %p, error "
2290 "x%x SNS x%x x%x Data: x%x x%x\n",
2291 cmd->device->id, cmd->device->lun, cmd,
2292 cmd->result, *lp, *(lp + 3), cmd->retries,
2293 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05002294 }
2295
James Smartea2151b2008-09-07 11:52:10 -04002296 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002297 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04002298 if (vport->cfg_max_scsicmpl_time &&
2299 time_after(jiffies, lpfc_cmd->start_time +
2300 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04002301 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002302 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2303 if (pnode->cmd_qdepth >
2304 atomic_read(&pnode->cmd_pending) &&
2305 (atomic_read(&pnode->cmd_pending) >
2306 LPFC_MIN_TGT_QDEPTH) &&
2307 ((cmd->cmnd[0] == READ_10) ||
2308 (cmd->cmnd[0] == WRITE_10)))
2309 pnode->cmd_qdepth =
2310 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04002311
James Smart109f6ed2008-12-04 22:39:08 -05002312 pnode->last_change_time = jiffies;
2313 }
James Smarta257bf92009-04-06 18:48:10 -04002314 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002315 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2316 if ((pnode->cmd_qdepth < LPFC_MAX_TGT_QDEPTH) &&
James Smart977b5a02008-09-07 11:52:04 -04002317 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05002318 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04002319 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002320 pnode->cmd_qdepth += pnode->cmd_qdepth *
2321 LPFC_TGTQ_RAMPUP_PCENT / 100;
2322 if (pnode->cmd_qdepth > LPFC_MAX_TGT_QDEPTH)
2323 pnode->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
2324 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04002325 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002326 }
James Smart977b5a02008-09-07 11:52:04 -04002327 }
2328
James Smart1dcb58e2007-04-25 09:51:30 -04002329 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04002330
2331 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
2332 queue_depth = cmd->device->queue_depth;
2333 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05002334 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002335
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002336 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smartfa61a542008-01-11 01:52:42 -05002337 /*
2338 * If there is a thread waiting for command completion
2339 * wake up the thread.
2340 */
James Smarta257bf92009-04-06 18:48:10 -04002341 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04002342 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05002343 if (lpfc_cmd->waitq)
2344 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04002345 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002346 lpfc_release_scsi_buf(phba, lpfc_cmd);
2347 return;
2348 }
2349
James Smart92d7f7b2007-06-17 19:56:38 -05002350
2351 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04002352 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05002353
James Smart58da1ff2008-04-07 10:15:56 -04002354 if (!result && pnode && NLP_CHK_NODE_ACT(pnode) &&
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002355 ((jiffies - pnode->last_ramp_up_time) >
2356 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
2357 ((jiffies - pnode->last_q_full_time) >
2358 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
James Smarta257bf92009-04-06 18:48:10 -04002359 (vport->cfg_lun_queue_depth > queue_depth)) {
2360 shost_for_each_device(tmp_sdev, shost) {
James Smart3de2a652007-08-02 11:09:59 -04002361 if (vport->cfg_lun_queue_depth > tmp_sdev->queue_depth){
James Smarta257bf92009-04-06 18:48:10 -04002362 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002363 continue;
2364 if (tmp_sdev->ordered_tags)
2365 scsi_adjust_queue_depth(tmp_sdev,
2366 MSG_ORDERED_TAG,
2367 tmp_sdev->queue_depth+1);
2368 else
2369 scsi_adjust_queue_depth(tmp_sdev,
2370 MSG_SIMPLE_TAG,
2371 tmp_sdev->queue_depth+1);
2372
2373 pnode->last_ramp_up_time = jiffies;
2374 }
2375 }
James Smartea2151b2008-09-07 11:52:10 -04002376 lpfc_send_sdev_queuedepth_change_event(phba, vport, pnode,
2377 0xFFFFFFFF,
James Smarta257bf92009-04-06 18:48:10 -04002378 queue_depth , queue_depth + 1);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002379 }
2380
2381 /*
2382 * Check for queue full. If the lun is reporting queue full, then
2383 * back off the lun queue depth to prevent target overloads.
2384 */
James Smart58da1ff2008-04-07 10:15:56 -04002385 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
2386 NLP_CHK_NODE_ACT(pnode)) {
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002387 pnode->last_q_full_time = jiffies;
2388
James Smarta257bf92009-04-06 18:48:10 -04002389 shost_for_each_device(tmp_sdev, shost) {
2390 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002391 continue;
2392 depth = scsi_track_queue_full(tmp_sdev,
2393 tmp_sdev->queue_depth - 1);
2394 }
2395 /*
James Smart2e0fef82007-06-17 19:56:36 -05002396 * The queue depth cannot be lowered any more.
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002397 * Modify the returned error code to store
2398 * the final depth value set by
2399 * scsi_track_queue_full.
2400 */
2401 if (depth == -1)
James Smarta257bf92009-04-06 18:48:10 -04002402 depth = shost->cmd_per_lun;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002403
2404 if (depth) {
James Smarte8b62012007-08-02 11:10:09 -04002405 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2406 "0711 detected queue full - lun queue "
2407 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04002408 lpfc_send_sdev_queuedepth_change_event(phba, vport,
2409 pnode, 0xFFFFFFFF,
2410 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002411 }
2412 }
2413
James Smartfa61a542008-01-11 01:52:42 -05002414 /*
2415 * If there is a thread waiting for command completion
2416 * wake up the thread.
2417 */
James Smarta257bf92009-04-06 18:48:10 -04002418 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04002419 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05002420 if (lpfc_cmd->waitq)
2421 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04002422 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05002423
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002424 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05002425}
2426
James Smart34b02dc2008-08-24 21:49:55 -04002427/**
James Smart3621a712009-04-06 18:47:14 -04002428 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04002429 * @data: A pointer to the immediate command data portion of the IOCB.
2430 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
2431 *
2432 * The routine copies the entire FCP command from @fcp_cmnd to @data while
2433 * byte swapping the data to big endian format for transmission on the wire.
2434 **/
2435static void
2436lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
2437{
2438 int i, j;
2439 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
2440 i += sizeof(uint32_t), j++) {
2441 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
2442 }
2443}
2444
James Smart9bad7672008-12-04 22:39:02 -05002445/**
James Smartf1126682009-06-10 17:22:44 -04002446 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05002447 * @vport: The virtual port for which this call is being executed.
2448 * @lpfc_cmd: The scsi command which needs to send.
2449 * @pnode: Pointer to lpfc_nodelist.
2450 *
2451 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04002452 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002453 **/
dea31012005-04-17 16:05:31 -05002454static void
James Smartf1126682009-06-10 17:22:44 -04002455lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05002456 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05002457{
James Smart2e0fef82007-06-17 19:56:36 -05002458 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002459 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2460 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2461 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2462 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
2463 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04002464 char tag[2];
dea31012005-04-17 16:05:31 -05002465
James Smart58da1ff2008-04-07 10:15:56 -04002466 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2467 return;
2468
dea31012005-04-17 16:05:31 -05002469 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04002470 /* clear task management bits */
2471 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05002472
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04002473 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
2474 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05002475
2476 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
2477
James Smart7e2b19f2007-10-29 11:00:39 -04002478 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
2479 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05002480 case HEAD_OF_QUEUE_TAG:
2481 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
2482 break;
2483 case ORDERED_QUEUE_TAG:
2484 fcp_cmnd->fcpCntl1 = ORDERED_Q;
2485 break;
2486 default:
2487 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
2488 break;
2489 }
2490 } else
2491 fcp_cmnd->fcpCntl1 = 0;
2492
2493 /*
2494 * There are three possibilities here - use scatter-gather segment, use
2495 * the single mapping, or neither. Start the lpfc command prep by
2496 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2497 * data bde entry.
2498 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002499 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05002500 if (datadir == DMA_TO_DEVICE) {
2501 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart3772a992009-05-22 14:50:54 -04002502 if (phba->sli_rev < LPFC_SLI_REV4) {
2503 iocb_cmd->un.fcpi.fcpi_parm = 0;
2504 iocb_cmd->ulpPU = 0;
2505 } else
2506 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05002507 fcp_cmnd->fcpCntl3 = WRITE_DATA;
2508 phba->fc4OutputRequests++;
2509 } else {
2510 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
2511 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05002512 fcp_cmnd->fcpCntl3 = READ_DATA;
2513 phba->fc4InputRequests++;
2514 }
2515 } else {
2516 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
2517 iocb_cmd->un.fcpi.fcpi_parm = 0;
2518 iocb_cmd->ulpPU = 0;
2519 fcp_cmnd->fcpCntl3 = 0;
2520 phba->fc4ControlRequests++;
2521 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002522 if (phba->sli_rev == 3 &&
2523 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04002524 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05002525 /*
2526 * Finish initializing those IOCB fields that are independent
2527 * of the scsi_cmnd request_buffer
2528 */
2529 piocbq->iocb.ulpContext = pnode->nlp_rpi;
2530 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
2531 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05002532 else
2533 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05002534
2535 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
2536 piocbq->context1 = lpfc_cmd;
2537 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
2538 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05002539 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05002540}
2541
James Smart9bad7672008-12-04 22:39:02 -05002542/**
James Smartf1126682009-06-10 17:22:44 -04002543 * lpfc_scsi_prep_task_mgmt_cmnd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05002544 * @vport: The virtual port for which this call is being executed.
2545 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2546 * @lun: Logical unit number.
2547 * @task_mgmt_cmd: SCSI task management command.
2548 *
James Smart3772a992009-05-22 14:50:54 -04002549 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
2550 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002551 *
2552 * Return codes:
2553 * 0 - Error
2554 * 1 - Success
2555 **/
dea31012005-04-17 16:05:31 -05002556static int
James Smartf1126682009-06-10 17:22:44 -04002557lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05002558 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04002559 unsigned int lun,
dea31012005-04-17 16:05:31 -05002560 uint8_t task_mgmt_cmd)
2561{
dea31012005-04-17 16:05:31 -05002562 struct lpfc_iocbq *piocbq;
2563 IOCB_t *piocb;
2564 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04002565 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05002566 struct lpfc_nodelist *ndlp = rdata->pnode;
2567
James Smart58da1ff2008-04-07 10:15:56 -04002568 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2569 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05002570 return 0;
dea31012005-04-17 16:05:31 -05002571
dea31012005-04-17 16:05:31 -05002572 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002573 piocbq->vport = vport;
2574
dea31012005-04-17 16:05:31 -05002575 piocb = &piocbq->iocb;
2576
2577 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04002578 /* Clear out any old data in the FCP command area */
2579 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
2580 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05002581 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05002582 if (vport->phba->sli_rev == 3 &&
2583 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04002584 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05002585 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05002586 piocb->ulpContext = ndlp->nlp_rpi;
2587 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
2588 piocb->ulpFCP2Rcvy = 1;
2589 }
2590 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
2591
2592 /* ulpTimeout is only one byte */
2593 if (lpfc_cmd->timeout > 0xff) {
2594 /*
2595 * Do not timeout the command at the firmware level.
2596 * The driver will provide the timeout mechanism.
2597 */
2598 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04002599 } else
dea31012005-04-17 16:05:31 -05002600 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04002601
2602 if (vport->phba->sli_rev == LPFC_SLI_REV4)
2603 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05002604
James Smart2e0fef82007-06-17 19:56:36 -05002605 return 1;
dea31012005-04-17 16:05:31 -05002606}
2607
James Smart9bad7672008-12-04 22:39:02 -05002608/**
James Smart3772a992009-05-22 14:50:54 -04002609 * lpfc_scsi_api_table_setup - Set up scsi api fucntion jump table
2610 * @phba: The hba struct for which this call is being executed.
2611 * @dev_grp: The HBA PCI-Device group number.
2612 *
2613 * This routine sets up the SCSI interface API function jump table in @phba
2614 * struct.
2615 * Returns: 0 - success, -ENODEV - failure.
2616 **/
2617int
2618lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
2619{
2620
James Smartf1126682009-06-10 17:22:44 -04002621 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
2622 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
2623 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf;
2624
James Smart3772a992009-05-22 14:50:54 -04002625 switch (dev_grp) {
2626 case LPFC_PCI_DEV_LP:
2627 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
2628 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04002629 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
2630 break;
James Smartda0436e2009-05-22 14:51:39 -04002631 case LPFC_PCI_DEV_OC:
2632 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
2633 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04002634 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
2635 break;
James Smart3772a992009-05-22 14:50:54 -04002636 default:
2637 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2638 "1418 Invalid HBA PCI-device group: 0x%x\n",
2639 dev_grp);
2640 return -ENODEV;
2641 break;
2642 }
2643 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf;
2644 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
2645 return 0;
2646}
2647
2648/**
James Smart3621a712009-04-06 18:47:14 -04002649 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05002650 * @phba: The Hba for which this call is being executed.
2651 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
2652 * @rspiocbq: Pointer to lpfc_iocbq data structure.
2653 *
2654 * This routine is IOCB completion routine for device reset and target reset
2655 * routine. This routine release scsi buffer associated with lpfc_cmd.
2656 **/
James Smart7054a602007-04-25 09:52:34 -04002657static void
2658lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
2659 struct lpfc_iocbq *cmdiocbq,
2660 struct lpfc_iocbq *rspiocbq)
2661{
2662 struct lpfc_scsi_buf *lpfc_cmd =
2663 (struct lpfc_scsi_buf *) cmdiocbq->context1;
2664 if (lpfc_cmd)
2665 lpfc_release_scsi_buf(phba, lpfc_cmd);
2666 return;
2667}
2668
James Smart9bad7672008-12-04 22:39:02 -05002669/**
James Smart3621a712009-04-06 18:47:14 -04002670 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05002671 * @host: The scsi host for which this call is being executed.
2672 *
2673 * This routine provides module information about hba.
2674 *
2675 * Reutrn code:
2676 * Pointer to char - Success.
2677 **/
dea31012005-04-17 16:05:31 -05002678const char *
2679lpfc_info(struct Scsi_Host *host)
2680{
James Smart2e0fef82007-06-17 19:56:36 -05002681 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
2682 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002683 int len;
2684 static char lpfcinfobuf[384];
2685
2686 memset(lpfcinfobuf,0,384);
2687 if (phba && phba->pcidev){
2688 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
2689 len = strlen(lpfcinfobuf);
2690 snprintf(lpfcinfobuf + len,
2691 384-len,
2692 " on PCI bus %02x device %02x irq %d",
2693 phba->pcidev->bus->number,
2694 phba->pcidev->devfn,
2695 phba->pcidev->irq);
2696 len = strlen(lpfcinfobuf);
2697 if (phba->Port[0]) {
2698 snprintf(lpfcinfobuf + len,
2699 384-len,
2700 " port %s",
2701 phba->Port);
2702 }
2703 }
2704 return lpfcinfobuf;
2705}
2706
James Smart9bad7672008-12-04 22:39:02 -05002707/**
James Smart3621a712009-04-06 18:47:14 -04002708 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05002709 * @phba: The Hba for which this call is being executed.
2710 *
2711 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
2712 * The default value of cfg_poll_tmo is 10 milliseconds.
2713 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002714static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
2715{
2716 unsigned long poll_tmo_expires =
2717 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
2718
2719 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
2720 mod_timer(&phba->fcp_poll_timer,
2721 poll_tmo_expires);
2722}
2723
James Smart9bad7672008-12-04 22:39:02 -05002724/**
James Smart3621a712009-04-06 18:47:14 -04002725 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05002726 * @phba: The Hba for which this call is being executed.
2727 *
2728 * This routine starts the fcp_poll_timer of @phba.
2729 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002730void lpfc_poll_start_timer(struct lpfc_hba * phba)
2731{
2732 lpfc_poll_rearm_timer(phba);
2733}
2734
James Smart9bad7672008-12-04 22:39:02 -05002735/**
James Smart3621a712009-04-06 18:47:14 -04002736 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05002737 * @ptr: Map to lpfc_hba data structure pointer.
2738 *
2739 * This routine restarts fcp_poll timer, when FCP ring polling is enable
2740 * and FCP Ring interrupt is disable.
2741 **/
2742
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002743void lpfc_poll_timeout(unsigned long ptr)
2744{
James Smart2e0fef82007-06-17 19:56:36 -05002745 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002746
2747 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2748 lpfc_sli_poll_fcp_ring (phba);
2749 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
2750 lpfc_poll_rearm_timer(phba);
2751 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002752}
2753
James Smart9bad7672008-12-04 22:39:02 -05002754/**
James Smart3621a712009-04-06 18:47:14 -04002755 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05002756 * @cmnd: Pointer to scsi_cmnd data structure.
2757 * @done: Pointer to done routine.
2758 *
2759 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
2760 * This routine prepares an IOCB from scsi command and provides to firmware.
2761 * The @done callback is invoked after driver finished processing the command.
2762 *
2763 * Return value :
2764 * 0 - Success
2765 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
2766 **/
dea31012005-04-17 16:05:31 -05002767static int
2768lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
2769{
James Smart2e0fef82007-06-17 19:56:36 -05002770 struct Scsi_Host *shost = cmnd->device->host;
2771 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2772 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002773 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
2774 struct lpfc_nodelist *ndlp = rdata->pnode;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002775 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002776 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002777 int err;
dea31012005-04-17 16:05:31 -05002778
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002779 err = fc_remote_port_chkready(rport);
2780 if (err) {
2781 cmnd->result = err;
dea31012005-04-17 16:05:31 -05002782 goto out_fail_command;
2783 }
2784
James Smarte2a0a9d2008-12-04 22:40:02 -05002785 if (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
2786 scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
2787
2788 printk(KERN_ERR "BLKGRD ERROR: rcvd protected cmd:%02x op:%02x "
2789 "str=%s without registering for BlockGuard - "
2790 "Rejecting command\n",
2791 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2792 dif_op_str[scsi_get_prot_op(cmnd)]);
2793 goto out_fail_command;
2794 }
2795
dea31012005-04-17 16:05:31 -05002796 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002797 * Catch race where our node has transitioned, but the
2798 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05002799 */
James Smartb522d7d2008-09-07 11:51:56 -04002800 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
2801 cmnd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
2802 goto out_fail_command;
2803 }
James Smart109f6ed2008-12-04 22:39:08 -05002804 if (vport->cfg_max_scsicmpl_time &&
2805 (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth))
James Smart977b5a02008-09-07 11:52:04 -04002806 goto out_host_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05002807
James Smarted957682007-06-17 19:56:37 -05002808 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05002809 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05002810 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002811
James Smarte8b62012007-08-02 11:10:09 -04002812 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2813 "0707 driver's buffer pool is empty, "
2814 "IO busied\n");
dea31012005-04-17 16:05:31 -05002815 goto out_host_busy;
2816 }
2817
2818 /*
2819 * Store the midlayer's command structure for the completion phase
2820 * and complete the command initialization.
2821 */
2822 lpfc_cmd->pCmd = cmnd;
2823 lpfc_cmd->rdata = rdata;
2824 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04002825 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05002826 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
2827 cmnd->scsi_done = done;
2828
James Smarte2a0a9d2008-12-04 22:40:02 -05002829 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
2830 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2831 "9033 BLKGRD: rcvd protected cmd:%02x op:%02x "
2832 "str=%s\n",
2833 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2834 dif_op_str[scsi_get_prot_op(cmnd)]);
2835 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2836 "9034 BLKGRD: CDB: %02x %02x %02x %02x %02x "
James Smarte4e74272009-07-19 10:01:38 -04002837 "%02x %02x %02x %02x %02x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002838 cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
2839 cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
2840 cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
2841 cmnd->cmnd[9]);
2842 if (cmnd->cmnd[0] == READ_10)
2843 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2844 "9035 BLKGRD: READ @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09002845 "count %u\n",
2846 (unsigned long long)scsi_get_lba(cmnd),
2847 blk_rq_sectors(cmnd->request));
James Smarte2a0a9d2008-12-04 22:40:02 -05002848 else if (cmnd->cmnd[0] == WRITE_10)
2849 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2850 "9036 BLKGRD: WRITE @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09002851 "count %u cmd=%p\n",
James Smart87b5c322008-12-16 10:34:09 -05002852 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09002853 blk_rq_sectors(cmnd->request),
James Smarte2a0a9d2008-12-04 22:40:02 -05002854 cmnd);
2855
2856 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
2857 } else {
2858 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2859 "9038 BLKGRD: rcvd unprotected cmd:%02x op:%02x"
2860 " str=%s\n",
2861 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2862 dif_op_str[scsi_get_prot_op(cmnd)]);
2863 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2864 "9039 BLKGRD: CDB: %02x %02x %02x %02x %02x "
James Smarte4e74272009-07-19 10:01:38 -04002865 "%02x %02x %02x %02x %02x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002866 cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
2867 cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
2868 cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
2869 cmnd->cmnd[9]);
2870 if (cmnd->cmnd[0] == READ_10)
2871 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2872 "9040 dbg: READ @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09002873 "count %u\n",
James Smart87b5c322008-12-16 10:34:09 -05002874 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09002875 blk_rq_sectors(cmnd->request));
James Smarte2a0a9d2008-12-04 22:40:02 -05002876 else if (cmnd->cmnd[0] == WRITE_10)
2877 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2878 "9041 dbg: WRITE @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09002879 "count %u cmd=%p\n",
James Smart87b5c322008-12-16 10:34:09 -05002880 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09002881 blk_rq_sectors(cmnd->request), cmnd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002882 else
2883 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2884 "9042 dbg: parser not implemented\n");
2885 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
2886 }
2887
dea31012005-04-17 16:05:31 -05002888 if (err)
2889 goto out_host_busy_free_buf;
2890
James Smart2e0fef82007-06-17 19:56:36 -05002891 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05002892
James Smart977b5a02008-09-07 11:52:04 -04002893 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04002894 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05002895 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05002896 if (err) {
2897 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05002898 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05002899 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002900 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2901 lpfc_sli_poll_fcp_ring(phba);
2902 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
2903 lpfc_poll_rearm_timer(phba);
2904 }
2905
dea31012005-04-17 16:05:31 -05002906 return 0;
2907
2908 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04002909 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002910 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05002911 out_host_busy:
2912 return SCSI_MLQUEUE_HOST_BUSY;
2913
2914 out_fail_command:
2915 done(cmnd);
2916 return 0;
2917}
2918
James Smart9bad7672008-12-04 22:39:02 -05002919/**
James Smart3621a712009-04-06 18:47:14 -04002920 * lpfc_block_error_handler - Routine to block error handler
James Smart9bad7672008-12-04 22:39:02 -05002921 * @cmnd: Pointer to scsi_cmnd data structure.
2922 *
2923 * This routine blocks execution till fc_rport state is not FC_PORSTAT_BLCOEKD.
2924 **/
James Smarta90f5682006-08-17 11:58:04 -04002925static void
2926lpfc_block_error_handler(struct scsi_cmnd *cmnd)
2927{
2928 struct Scsi_Host *shost = cmnd->device->host;
2929 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
2930
2931 spin_lock_irq(shost->host_lock);
2932 while (rport->port_state == FC_PORTSTATE_BLOCKED) {
2933 spin_unlock_irq(shost->host_lock);
2934 msleep(1000);
2935 spin_lock_irq(shost->host_lock);
2936 }
2937 spin_unlock_irq(shost->host_lock);
2938 return;
2939}
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05002940
James Smart9bad7672008-12-04 22:39:02 -05002941/**
James Smart3621a712009-04-06 18:47:14 -04002942 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05002943 * @cmnd: Pointer to scsi_cmnd data structure.
2944 *
2945 * This routine aborts @cmnd pending in base driver.
2946 *
2947 * Return code :
2948 * 0x2003 - Error
2949 * 0x2002 - Success
2950 **/
dea31012005-04-17 16:05:31 -05002951static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05002952lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05002953{
James Smart2e0fef82007-06-17 19:56:36 -05002954 struct Scsi_Host *shost = cmnd->device->host;
2955 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2956 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002957 struct lpfc_iocbq *iocb;
2958 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05002959 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05002960 IOCB_t *cmd, *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002961 int ret = SUCCESS;
James Smartfa61a542008-01-11 01:52:42 -05002962 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002963
James Smarta90f5682006-08-17 11:58:04 -04002964 lpfc_block_error_handler(cmnd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002965 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
2966 BUG_ON(!lpfc_cmd);
dea31012005-04-17 16:05:31 -05002967
2968 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002969 * If pCmd field of the corresponding lpfc_scsi_buf structure
2970 * points to a different SCSI command, then the driver has
2971 * already completed this command, but the midlayer did not
2972 * see the completion before the eh fired. Just return
2973 * SUCCESS.
dea31012005-04-17 16:05:31 -05002974 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002975 iocb = &lpfc_cmd->cur_iocbq;
2976 if (lpfc_cmd->pCmd != cmnd)
2977 goto out;
dea31012005-04-17 16:05:31 -05002978
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002979 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05002980
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002981 abtsiocb = lpfc_sli_get_iocbq(phba);
2982 if (abtsiocb == NULL) {
2983 ret = FAILED;
dea31012005-04-17 16:05:31 -05002984 goto out;
2985 }
2986
dea31012005-04-17 16:05:31 -05002987 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002988 * The scsi command can not be in txq and it is in flight because the
2989 * pCmd is still pointig at the SCSI command we have to abort. There
2990 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05002991 */
dea31012005-04-17 16:05:31 -05002992
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002993 cmd = &iocb->iocb;
2994 icmd = &abtsiocb->iocb;
2995 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
2996 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04002997 if (phba->sli_rev == LPFC_SLI_REV4)
2998 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
2999 else
3000 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05003001
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003002 icmd->ulpLe = 1;
3003 icmd->ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05003004 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003005 icmd->ulpCommand = CMD_ABORT_XRI_CN;
3006 else
3007 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05003008
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003009 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05003010 abtsiocb->vport = vport;
James Smart3772a992009-05-22 14:50:54 -04003011 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
3012 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003013 lpfc_sli_release_iocbq(phba, abtsiocb);
3014 ret = FAILED;
3015 goto out;
3016 }
3017
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003018 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3019 lpfc_sli_poll_fcp_ring (phba);
3020
James Smartfa61a542008-01-11 01:52:42 -05003021 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003022 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05003023 wait_event_timeout(waitq,
3024 (lpfc_cmd->pCmd != cmnd),
3025 (2*vport->cfg_devloss_tmo*HZ));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003026
James Smartfa61a542008-01-11 01:52:42 -05003027 spin_lock_irq(shost->host_lock);
3028 lpfc_cmd->waitq = NULL;
3029 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003030
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003031 if (lpfc_cmd->pCmd == cmnd) {
3032 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04003033 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3034 "0748 abort handler timed out waiting "
3035 "for abort to complete: ret %#x, ID %d, "
3036 "LUN %d, snum %#lx\n",
3037 ret, cmnd->device->id, cmnd->device->lun,
3038 cmnd->serial_number);
dea31012005-04-17 16:05:31 -05003039 }
3040
3041 out:
James Smarte8b62012007-08-02 11:10:09 -04003042 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3043 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
3044 "LUN %d snum %#lx\n", ret, cmnd->device->id,
3045 cmnd->device->lun, cmnd->serial_number);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003046 return ret;
dea31012005-04-17 16:05:31 -05003047}
3048
James Smartbbb9d182009-06-10 17:23:16 -04003049static char *
3050lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
3051{
3052 switch (task_mgmt_cmd) {
3053 case FCP_ABORT_TASK_SET:
3054 return "ABORT_TASK_SET";
3055 case FCP_CLEAR_TASK_SET:
3056 return "FCP_CLEAR_TASK_SET";
3057 case FCP_BUS_RESET:
3058 return "FCP_BUS_RESET";
3059 case FCP_LUN_RESET:
3060 return "FCP_LUN_RESET";
3061 case FCP_TARGET_RESET:
3062 return "FCP_TARGET_RESET";
3063 case FCP_CLEAR_ACA:
3064 return "FCP_CLEAR_ACA";
3065 case FCP_TERMINATE_TASK:
3066 return "FCP_TERMINATE_TASK";
3067 default:
3068 return "unknown";
3069 }
3070}
3071
3072/**
3073 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
3074 * @vport: The virtual port for which this call is being executed.
3075 * @rdata: Pointer to remote port local data
3076 * @tgt_id: Target ID of remote device.
3077 * @lun_id: Lun number for the TMF
3078 * @task_mgmt_cmd: type of TMF to send
3079 *
3080 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
3081 * a remote port.
3082 *
3083 * Return Code:
3084 * 0x2003 - Error
3085 * 0x2002 - Success.
3086 **/
3087static int
3088lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
3089 unsigned tgt_id, unsigned int lun_id,
3090 uint8_t task_mgmt_cmd)
3091{
3092 struct lpfc_hba *phba = vport->phba;
3093 struct lpfc_scsi_buf *lpfc_cmd;
3094 struct lpfc_iocbq *iocbq;
3095 struct lpfc_iocbq *iocbqrsp;
3096 int ret;
3097 int status;
3098
3099 if (!rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
3100 return FAILED;
3101
3102 lpfc_cmd = lpfc_get_scsi_buf(phba);
3103 if (lpfc_cmd == NULL)
3104 return FAILED;
3105 lpfc_cmd->timeout = 60;
3106 lpfc_cmd->rdata = rdata;
3107
3108 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
3109 task_mgmt_cmd);
3110 if (!status) {
3111 lpfc_release_scsi_buf(phba, lpfc_cmd);
3112 return FAILED;
3113 }
3114
3115 iocbq = &lpfc_cmd->cur_iocbq;
3116 iocbqrsp = lpfc_sli_get_iocbq(phba);
3117 if (iocbqrsp == NULL) {
3118 lpfc_release_scsi_buf(phba, lpfc_cmd);
3119 return FAILED;
3120 }
3121
3122 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3123 "0702 Issue %s to TGT %d LUN %d "
3124 "rpi x%x nlp_flag x%x\n",
3125 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
3126 rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
3127
3128 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
3129 iocbq, iocbqrsp, lpfc_cmd->timeout);
3130 if (status != IOCB_SUCCESS) {
3131 if (status == IOCB_TIMEDOUT) {
3132 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
3133 ret = TIMEOUT_ERROR;
3134 } else
3135 ret = FAILED;
3136 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3137 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3138 "0727 TMF %s to TGT %d LUN %d failed (%d, %d)\n",
3139 lpfc_taskmgmt_name(task_mgmt_cmd),
3140 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
3141 iocbqrsp->iocb.un.ulpWord[4]);
3142 } else
3143 ret = SUCCESS;
3144
3145 lpfc_sli_release_iocbq(phba, iocbqrsp);
3146
3147 if (ret != TIMEOUT_ERROR)
3148 lpfc_release_scsi_buf(phba, lpfc_cmd);
3149
3150 return ret;
3151}
3152
3153/**
3154 * lpfc_chk_tgt_mapped -
3155 * @vport: The virtual port to check on
3156 * @cmnd: Pointer to scsi_cmnd data structure.
3157 *
3158 * This routine delays until the scsi target (aka rport) for the
3159 * command exists (is present and logged in) or we declare it non-existent.
3160 *
3161 * Return code :
3162 * 0x2003 - Error
3163 * 0x2002 - Success
3164 **/
3165static int
3166lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
3167{
3168 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3169 struct lpfc_nodelist *pnode = rdata->pnode;
3170 unsigned long later;
3171
3172 /*
3173 * If target is not in a MAPPED state, delay until
3174 * target is rediscovered or devloss timeout expires.
3175 */
3176 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3177 while (time_after(later, jiffies)) {
3178 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3179 return FAILED;
3180 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
3181 return SUCCESS;
3182 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
3183 rdata = cmnd->device->hostdata;
3184 if (!rdata)
3185 return FAILED;
3186 pnode = rdata->pnode;
3187 }
3188 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
3189 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
3190 return FAILED;
3191 return SUCCESS;
3192}
3193
3194/**
3195 * lpfc_reset_flush_io_context -
3196 * @vport: The virtual port (scsi_host) for the flush context
3197 * @tgt_id: If aborting by Target contect - specifies the target id
3198 * @lun_id: If aborting by Lun context - specifies the lun id
3199 * @context: specifies the context level to flush at.
3200 *
3201 * After a reset condition via TMF, we need to flush orphaned i/o
3202 * contexts from the adapter. This routine aborts any contexts
3203 * outstanding, then waits for their completions. The wait is
3204 * bounded by devloss_tmo though.
3205 *
3206 * Return code :
3207 * 0x2003 - Error
3208 * 0x2002 - Success
3209 **/
3210static int
3211lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
3212 uint64_t lun_id, lpfc_ctx_cmd context)
3213{
3214 struct lpfc_hba *phba = vport->phba;
3215 unsigned long later;
3216 int cnt;
3217
3218 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3219 if (cnt)
3220 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
3221 tgt_id, lun_id, context);
3222 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3223 while (time_after(later, jiffies) && cnt) {
3224 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
3225 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3226 }
3227 if (cnt) {
3228 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3229 "0724 I/O flush failure for context %s : cnt x%x\n",
3230 ((context == LPFC_CTX_LUN) ? "LUN" :
3231 ((context == LPFC_CTX_TGT) ? "TGT" :
3232 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
3233 cnt);
3234 return FAILED;
3235 }
3236 return SUCCESS;
3237}
3238
James Smart9bad7672008-12-04 22:39:02 -05003239/**
James Smart3621a712009-04-06 18:47:14 -04003240 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05003241 * @cmnd: Pointer to scsi_cmnd data structure.
3242 *
James Smartbbb9d182009-06-10 17:23:16 -04003243 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05003244 * command.
3245 *
3246 * Return code :
3247 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04003248 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05003249 **/
dea31012005-04-17 16:05:31 -05003250static int
James Smart7054a602007-04-25 09:52:34 -04003251lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003252{
James Smart2e0fef82007-06-17 19:56:36 -05003253 struct Scsi_Host *shost = cmnd->device->host;
3254 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05003255 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3256 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003257 unsigned tgt_id = cmnd->device->id;
3258 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04003259 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04003260 int status;
dea31012005-04-17 16:05:31 -05003261
James Smarta90f5682006-08-17 11:58:04 -04003262 lpfc_block_error_handler(cmnd);
James Smartbbb9d182009-06-10 17:23:16 -04003263
3264 status = lpfc_chk_tgt_mapped(vport, cmnd);
3265 if (status == FAILED) {
3266 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3267 "0721 Device Reset rport failure: rdata x%p\n", rdata);
3268 return FAILED;
3269 }
3270
3271 scsi_event.event_type = FC_REG_SCSI_EVENT;
3272 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
3273 scsi_event.lun = lun_id;
3274 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3275 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3276
3277 fc_host_post_vendor_event(shost, fc_get_event_number(),
3278 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3279
3280 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3281 FCP_LUN_RESET);
3282
3283 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3284 "0713 SCSI layer issued Device Reset (%d, %d) "
3285 "return x%x\n", tgt_id, lun_id, status);
3286
dea31012005-04-17 16:05:31 -05003287 /*
James Smartbbb9d182009-06-10 17:23:16 -04003288 * We have to clean up i/o as : they may be orphaned by the TMF;
3289 * or if the TMF failed, they may be in an indeterminate state.
3290 * So, continue on.
3291 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05003292 */
James Smartbbb9d182009-06-10 17:23:16 -04003293 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3294 LPFC_CTX_LUN);
3295 return status;
3296}
3297
3298/**
3299 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
3300 * @cmnd: Pointer to scsi_cmnd data structure.
3301 *
3302 * This routine does a target reset by sending a TARGET_RESET task management
3303 * command.
3304 *
3305 * Return code :
3306 * 0x2003 - Error
3307 * 0x2002 - Success
3308 **/
3309static int
3310lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
3311{
3312 struct Scsi_Host *shost = cmnd->device->host;
3313 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3314 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3315 struct lpfc_nodelist *pnode = rdata->pnode;
3316 unsigned tgt_id = cmnd->device->id;
3317 unsigned int lun_id = cmnd->device->lun;
3318 struct lpfc_scsi_event_header scsi_event;
3319 int status;
3320
3321 lpfc_block_error_handler(cmnd);
3322
3323 status = lpfc_chk_tgt_mapped(vport, cmnd);
3324 if (status == FAILED) {
3325 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3326 "0722 Target Reset rport failure: rdata x%p\n", rdata);
3327 return FAILED;
dea31012005-04-17 16:05:31 -05003328 }
James Smartea2151b2008-09-07 11:52:10 -04003329
3330 scsi_event.event_type = FC_REG_SCSI_EVENT;
3331 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
3332 scsi_event.lun = 0;
3333 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3334 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3335
James Smartbbb9d182009-06-10 17:23:16 -04003336 fc_host_post_vendor_event(shost, fc_get_event_number(),
3337 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04003338
James Smartbbb9d182009-06-10 17:23:16 -04003339 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3340 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05003341
James Smarte8b62012007-08-02 11:10:09 -04003342 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04003343 "0723 SCSI layer issued Target Reset (%d, %d) "
3344 "return x%x\n", tgt_id, lun_id, status);
3345
3346 /*
3347 * We have to clean up i/o as : they may be orphaned by the TMF;
3348 * or if the TMF failed, they may be in an indeterminate state.
3349 * So, continue on.
3350 * We will report success if all the i/o aborts successfully.
3351 */
3352 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3353 LPFC_CTX_TGT);
3354 return status;
dea31012005-04-17 16:05:31 -05003355}
3356
James Smart9bad7672008-12-04 22:39:02 -05003357/**
James Smart3621a712009-04-06 18:47:14 -04003358 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05003359 * @cmnd: Pointer to scsi_cmnd data structure.
3360 *
James Smartbbb9d182009-06-10 17:23:16 -04003361 * This routine does target reset to all targets on @cmnd->device->host.
3362 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05003363 *
James Smartbbb9d182009-06-10 17:23:16 -04003364 * Return code :
3365 * 0x2003 - Error
3366 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05003367 **/
Jeff Garzik 94d0e7b2005-05-28 07:55:48 -04003368static int
James Smart7054a602007-04-25 09:52:34 -04003369lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003370{
James Smart2e0fef82007-06-17 19:56:36 -05003371 struct Scsi_Host *shost = cmnd->device->host;
3372 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05003373 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04003374 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04003375 int match;
3376 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04003377
3378 scsi_event.event_type = FC_REG_SCSI_EVENT;
3379 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
3380 scsi_event.lun = 0;
3381 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
3382 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
3383
James Smartbbb9d182009-06-10 17:23:16 -04003384 fc_host_post_vendor_event(shost, fc_get_event_number(),
3385 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05003386
James Smarta90f5682006-08-17 11:58:04 -04003387 lpfc_block_error_handler(cmnd);
James Smartbbb9d182009-06-10 17:23:16 -04003388
dea31012005-04-17 16:05:31 -05003389 /*
3390 * Since the driver manages a single bus device, reset all
3391 * targets known to the driver. Should any target reset
3392 * fail, this routine returns failure to the midlayer.
3393 */
James Smarte17da182006-07-06 15:49:25 -04003394 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04003395 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05003396 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003397 spin_lock_irq(shost->host_lock);
3398 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003399 if (!NLP_CHK_NODE_ACT(ndlp))
3400 continue;
James Smart685f0bf2007-04-25 09:53:08 -04003401 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04003402 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04003403 ndlp->rport) {
dea31012005-04-17 16:05:31 -05003404 match = 1;
3405 break;
3406 }
3407 }
James Smart2e0fef82007-06-17 19:56:36 -05003408 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003409 if (!match)
3410 continue;
James Smartbbb9d182009-06-10 17:23:16 -04003411
3412 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
3413 i, 0, FCP_TARGET_RESET);
3414
3415 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04003416 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3417 "0700 Bus Reset on target %d failed\n",
3418 i);
James Smart915caaa2008-06-14 22:52:38 -04003419 ret = FAILED;
dea31012005-04-17 16:05:31 -05003420 }
3421 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003422 /*
James Smartbbb9d182009-06-10 17:23:16 -04003423 * We have to clean up i/o as : they may be orphaned by the TMFs
3424 * above; or if any of the TMFs failed, they may be in an
3425 * indeterminate state.
3426 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003427 */
James Smartbbb9d182009-06-10 17:23:16 -04003428
3429 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
3430 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003431 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04003432
James Smarte8b62012007-08-02 11:10:09 -04003433 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3434 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05003435 return ret;
3436}
3437
James Smart9bad7672008-12-04 22:39:02 -05003438/**
James Smart3621a712009-04-06 18:47:14 -04003439 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05003440 * @sdev: Pointer to scsi_device.
3441 *
3442 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
3443 * globally available list of scsi buffers. This routine also makes sure scsi
3444 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
3445 * of scsi buffer exists for the lifetime of the driver.
3446 *
3447 * Return codes:
3448 * non-0 - Error
3449 * 0 - Success
3450 **/
dea31012005-04-17 16:05:31 -05003451static int
dea31012005-04-17 16:05:31 -05003452lpfc_slave_alloc(struct scsi_device *sdev)
3453{
James Smart2e0fef82007-06-17 19:56:36 -05003454 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3455 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003456 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04003457 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05003458 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04003459 int num_allocated = 0;
dea31012005-04-17 16:05:31 -05003460
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003461 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05003462 return -ENXIO;
3463
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003464 sdev->hostdata = rport->dd_data;
dea31012005-04-17 16:05:31 -05003465
3466 /*
3467 * Populate the cmds_per_lun count scsi_bufs into this host's globally
3468 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04003469 * HBA limit conveyed to the midlayer via the host structure. The
3470 * formula accounts for the lun_queue_depth + error handlers + 1
3471 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05003472 */
3473 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04003474 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05003475
3476 /* Allow some exchanges to be available always to complete discovery */
3477 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04003478 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3479 "0704 At limitation of %d preallocated "
3480 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05003481 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003482 /* Allow some exchanges to be available always to complete discovery */
3483 } else if (total + num_to_alloc >
3484 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04003485 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3486 "0705 Allocation request of %d "
3487 "command buffers will exceed max of %d. "
3488 "Reducing allocation request to %d.\n",
3489 num_to_alloc, phba->cfg_hba_queue_depth,
3490 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05003491 num_to_alloc = phba->cfg_hba_queue_depth - total;
3492 }
James Smart3772a992009-05-22 14:50:54 -04003493 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
3494 if (num_to_alloc != num_allocated) {
3495 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3496 "0708 Allocation request of %d "
3497 "command buffers did not succeed. "
3498 "Allocated %d buffers.\n",
3499 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05003500 }
3501 return 0;
3502}
3503
James Smart9bad7672008-12-04 22:39:02 -05003504/**
James Smart3621a712009-04-06 18:47:14 -04003505 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05003506 * @sdev: Pointer to scsi_device.
3507 *
3508 * This routine configures following items
3509 * - Tag command queuing support for @sdev if supported.
3510 * - Dev loss time out value of fc_rport.
3511 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
3512 *
3513 * Return codes:
3514 * 0 - Success
3515 **/
dea31012005-04-17 16:05:31 -05003516static int
3517lpfc_slave_configure(struct scsi_device *sdev)
3518{
James Smart2e0fef82007-06-17 19:56:36 -05003519 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3520 struct lpfc_hba *phba = vport->phba;
3521 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
dea31012005-04-17 16:05:31 -05003522
3523 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04003524 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05003525 else
James Smart3de2a652007-08-02 11:09:59 -04003526 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05003527
3528 /*
3529 * Initialize the fc transport attributes for the target
3530 * containing this scsi device. Also note that the driver's
3531 * target pointer is stored in the starget_data for the
3532 * driver's sysfs entry point functions.
3533 */
James Smart3de2a652007-08-02 11:09:59 -04003534 rport->dev_loss_tmo = vport->cfg_devloss_tmo;
dea31012005-04-17 16:05:31 -05003535
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003536 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
3537 lpfc_sli_poll_fcp_ring(phba);
3538 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3539 lpfc_poll_rearm_timer(phba);
3540 }
3541
dea31012005-04-17 16:05:31 -05003542 return 0;
3543}
3544
James Smart9bad7672008-12-04 22:39:02 -05003545/**
James Smart3621a712009-04-06 18:47:14 -04003546 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05003547 * @sdev: Pointer to scsi_device.
3548 *
3549 * This routine sets @sdev hostatdata filed to null.
3550 **/
dea31012005-04-17 16:05:31 -05003551static void
3552lpfc_slave_destroy(struct scsi_device *sdev)
3553{
3554 sdev->hostdata = NULL;
3555 return;
3556}
3557
James Smart92d7f7b2007-06-17 19:56:38 -05003558
dea31012005-04-17 16:05:31 -05003559struct scsi_host_template lpfc_template = {
3560 .module = THIS_MODULE,
3561 .name = LPFC_DRIVER_NAME,
3562 .info = lpfc_info,
3563 .queuecommand = lpfc_queuecommand,
3564 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04003565 .eh_device_reset_handler = lpfc_device_reset_handler,
3566 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04003567 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05003568 .slave_alloc = lpfc_slave_alloc,
3569 .slave_configure = lpfc_slave_configure,
3570 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04003571 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05003572 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05003573 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05003574 .cmd_per_lun = LPFC_CMD_PER_LUN,
3575 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05003576 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04003577 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04003578 .vendor_id = LPFC_NL_VENDOR_ID,
dea31012005-04-17 16:05:31 -05003579};
James Smart3de2a652007-08-02 11:09:59 -04003580
3581struct scsi_host_template lpfc_vport_template = {
3582 .module = THIS_MODULE,
3583 .name = LPFC_DRIVER_NAME,
3584 .info = lpfc_info,
3585 .queuecommand = lpfc_queuecommand,
3586 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04003587 .eh_device_reset_handler = lpfc_device_reset_handler,
3588 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04003589 .eh_bus_reset_handler = lpfc_bus_reset_handler,
3590 .slave_alloc = lpfc_slave_alloc,
3591 .slave_configure = lpfc_slave_configure,
3592 .slave_destroy = lpfc_slave_destroy,
3593 .scan_finished = lpfc_scan_finished,
3594 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05003595 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04003596 .cmd_per_lun = LPFC_CMD_PER_LUN,
3597 .use_clustering = ENABLE_CLUSTERING,
3598 .shost_attrs = lpfc_vport_attrs,
3599 .max_sectors = 0xFFFF,
3600};