blob: bf80cdefb5066a53333078e3a0719282c12d8a18 [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
James Smart6a9c52c2009-10-02 15:16:51 -040064lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -050065{
66 void *src, *dst;
67 struct scatterlist *sgde = scsi_sglist(cmnd);
68
69 if (!_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040070 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
71 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -050072 __func__);
73 return;
74 }
75
76
77 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -040078 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
79 "9051 BLKGRD: ERROR: data scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -050080 return;
81 }
82
83 dst = (void *) _dump_buf_data;
84 while (sgde) {
85 src = sg_virt(sgde);
86 memcpy(dst, src, sgde->length);
87 dst += sgde->length;
88 sgde = sg_next(sgde);
89 }
90}
91
92static void
James Smart6a9c52c2009-10-02 15:16:51 -040093lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -050094{
95 void *src, *dst;
96 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
97
98 if (!_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040099 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
100 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -0500101 __func__);
102 return;
103 }
104
105 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -0400106 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
107 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -0500108 return;
109 }
110
111 dst = _dump_buf_dif;
112 while (sgde) {
113 src = sg_virt(sgde);
114 memcpy(dst, src, sgde->length);
115 dst += sgde->length;
116 sgde = sg_next(sgde);
117 }
118}
119
James Smartea2151b2008-09-07 11:52:10 -0400120/**
James Smartf1126682009-06-10 17:22:44 -0400121 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
122 * @phba: Pointer to HBA object.
123 * @lpfc_cmd: lpfc scsi command object pointer.
124 *
125 * This function is called from the lpfc_prep_task_mgmt_cmd function to
126 * set the last bit in the response sge entry.
127 **/
128static void
129lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
130 struct lpfc_scsi_buf *lpfc_cmd)
131{
132 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
133 if (sgl) {
134 sgl += 1;
135 sgl->word2 = le32_to_cpu(sgl->word2);
136 bf_set(lpfc_sli4_sge_last, sgl, 1);
137 sgl->word2 = cpu_to_le32(sgl->word2);
138 }
139}
140
141/**
James Smart3621a712009-04-06 18:47:14 -0400142 * lpfc_update_stats - Update statistical data for the command completion
James Smartea2151b2008-09-07 11:52:10 -0400143 * @phba: Pointer to HBA object.
144 * @lpfc_cmd: lpfc scsi command object pointer.
145 *
146 * This function is called when there is a command completion and this
147 * function updates the statistical data for the command completion.
148 **/
149static void
150lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
151{
152 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
153 struct lpfc_nodelist *pnode = rdata->pnode;
154 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
155 unsigned long flags;
156 struct Scsi_Host *shost = cmd->device->host;
157 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
158 unsigned long latency;
159 int i;
160
161 if (cmd->result)
162 return;
163
James Smart9f1e1b52008-12-04 22:39:40 -0500164 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
165
James Smartea2151b2008-09-07 11:52:10 -0400166 spin_lock_irqsave(shost->host_lock, flags);
167 if (!vport->stat_data_enabled ||
168 vport->stat_data_blocked ||
169 !pnode->lat_data ||
170 (phba->bucket_type == LPFC_NO_BUCKET)) {
171 spin_unlock_irqrestore(shost->host_lock, flags);
172 return;
173 }
James Smartea2151b2008-09-07 11:52:10 -0400174
175 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
176 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
177 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -0500178 /* check array subscript bounds */
179 if (i < 0)
180 i = 0;
181 else if (i >= LPFC_MAX_BUCKET_COUNT)
182 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -0400183 } else {
184 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
185 if (latency <= (phba->bucket_base +
186 ((1<<i)*phba->bucket_step)))
187 break;
188 }
189
190 pnode->lat_data[i].cmd_count++;
191 spin_unlock_irqrestore(shost->host_lock, flags);
192}
193
James Smartea2151b2008-09-07 11:52:10 -0400194/**
James Smart3621a712009-04-06 18:47:14 -0400195 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
James Smartea2151b2008-09-07 11:52:10 -0400196 * @phba: Pointer to HBA context object.
197 * @vport: Pointer to vport object.
198 * @ndlp: Pointer to FC node associated with the target.
199 * @lun: Lun number of the scsi device.
200 * @old_val: Old value of the queue depth.
201 * @new_val: New value of the queue depth.
202 *
203 * This function sends an event to the mgmt application indicating
204 * there is a change in the scsi device queue depth.
205 **/
206static void
207lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
208 struct lpfc_vport *vport,
209 struct lpfc_nodelist *ndlp,
210 uint32_t lun,
211 uint32_t old_val,
212 uint32_t new_val)
213{
214 struct lpfc_fast_path_event *fast_path_evt;
215 unsigned long flags;
216
217 fast_path_evt = lpfc_alloc_fast_evt(phba);
218 if (!fast_path_evt)
219 return;
220
221 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
222 FC_REG_SCSI_EVENT;
223 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
224 LPFC_EVENT_VARQUEDEPTH;
225
226 /* Report all luns with change in queue depth */
227 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
228 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
229 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
230 &ndlp->nlp_portname, sizeof(struct lpfc_name));
231 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
232 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
233 }
234
235 fast_path_evt->un.queue_depth_evt.oldval = old_val;
236 fast_path_evt->un.queue_depth_evt.newval = new_val;
237 fast_path_evt->vport = vport;
238
239 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
240 spin_lock_irqsave(&phba->hbalock, flags);
241 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
242 spin_unlock_irqrestore(&phba->hbalock, flags);
243 lpfc_worker_wake_up(phba);
244
245 return;
246}
247
James Smart9bad7672008-12-04 22:39:02 -0500248/**
James Smart5ffc2662009-11-18 15:39:44 -0500249 * lpfc_change_queue_depth - Alter scsi device queue depth
250 * @sdev: Pointer the scsi device on which to change the queue depth.
251 * @qdepth: New queue depth to set the sdev to.
252 * @reason: The reason for the queue depth change.
253 *
254 * This function is called by the midlayer and the LLD to alter the queue
255 * depth for a scsi device. This function sets the queue depth to the new
256 * value and sends an event out to log the queue depth change.
257 **/
258int
259lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
260{
261 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
262 struct lpfc_hba *phba = vport->phba;
263 struct lpfc_rport_data *rdata;
264 unsigned long new_queue_depth, old_queue_depth;
265
266 old_queue_depth = sdev->queue_depth;
267 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
268 new_queue_depth = sdev->queue_depth;
269 rdata = sdev->hostdata;
270 if (rdata)
271 lpfc_send_sdev_queuedepth_change_event(phba, vport,
272 rdata->pnode, sdev->lun,
273 old_queue_depth,
274 new_queue_depth);
275 return sdev->queue_depth;
276}
277
278/**
James Smart3621a712009-04-06 18:47:14 -0400279 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500280 * @phba: The Hba for which this call is being executed.
281 *
282 * This routine is called when there is resource error in driver or firmware.
283 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
284 * posts at most 1 event each second. This routine wakes up worker thread of
285 * @phba to process WORKER_RAM_DOWN_EVENT event.
286 *
287 * This routine should be called with no lock held.
288 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500289void
James Smarteaf15d52008-12-04 22:39:29 -0500290lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500291{
292 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400293 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500294
295 spin_lock_irqsave(&phba->hbalock, flags);
296 atomic_inc(&phba->num_rsrc_err);
297 phba->last_rsrc_error_time = jiffies;
298
299 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
300 spin_unlock_irqrestore(&phba->hbalock, flags);
301 return;
302 }
303
304 phba->last_ramp_down_time = jiffies;
305
306 spin_unlock_irqrestore(&phba->hbalock, flags);
307
308 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400309 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
310 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500311 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500312 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
313
James Smart5e9d9b82008-06-14 22:52:53 -0400314 if (!evt_posted)
315 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500316 return;
317}
318
James Smart9bad7672008-12-04 22:39:02 -0500319/**
James Smart3621a712009-04-06 18:47:14 -0400320 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
James Smart9bad7672008-12-04 22:39:02 -0500321 * @phba: The Hba for which this call is being executed.
322 *
323 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
324 * post at most 1 event every 5 minute after last_ramp_up_time or
325 * last_rsrc_error_time. This routine wakes up worker thread of @phba
326 * to process WORKER_RAM_DOWN_EVENT event.
327 *
328 * This routine should be called with no lock held.
329 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500330static inline void
James Smart3de2a652007-08-02 11:09:59 -0400331lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smarta257bf92009-04-06 18:48:10 -0400332 uint32_t queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500333{
334 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -0400335 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400336 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500337 atomic_inc(&phba->num_cmd_success);
338
James Smarta257bf92009-04-06 18:48:10 -0400339 if (vport->cfg_lun_queue_depth <= queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500340 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500341 spin_lock_irqsave(&phba->hbalock, flags);
James Smart5ffc2662009-11-18 15:39:44 -0500342 if (time_before(jiffies,
343 phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
344 time_before(jiffies,
345 phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500346 spin_unlock_irqrestore(&phba->hbalock, flags);
347 return;
348 }
James Smart92d7f7b2007-06-17 19:56:38 -0500349 phba->last_ramp_up_time = jiffies;
350 spin_unlock_irqrestore(&phba->hbalock, flags);
351
352 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400353 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
354 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500355 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500356 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
357
James Smart5e9d9b82008-06-14 22:52:53 -0400358 if (!evt_posted)
359 lpfc_worker_wake_up(phba);
360 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500361}
362
James Smart9bad7672008-12-04 22:39:02 -0500363/**
James Smart3621a712009-04-06 18:47:14 -0400364 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500365 * @phba: The Hba for which this call is being executed.
366 *
367 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
368 * thread.This routine reduces queue depth for all scsi device on each vport
369 * associated with @phba.
370 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500371void
372lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
373{
James Smart549e55c2007-08-02 11:09:51 -0400374 struct lpfc_vport **vports;
375 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500376 struct scsi_device *sdev;
James Smart5ffc2662009-11-18 15:39:44 -0500377 unsigned long new_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500378 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400379 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500380
381 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
382 num_cmd_success = atomic_read(&phba->num_cmd_success);
383
James Smart549e55c2007-08-02 11:09:51 -0400384 vports = lpfc_create_vport_work_array(phba);
385 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400386 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400387 shost = lpfc_shost_from_vport(vports[i]);
388 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500389 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400390 sdev->queue_depth * num_rsrc_err /
391 (num_rsrc_err + num_cmd_success);
392 if (!new_queue_depth)
393 new_queue_depth = sdev->queue_depth - 1;
394 else
395 new_queue_depth = sdev->queue_depth -
396 new_queue_depth;
James Smart5ffc2662009-11-18 15:39:44 -0500397 lpfc_change_queue_depth(sdev, new_queue_depth,
398 SCSI_QDEPTH_DEFAULT);
James Smart549e55c2007-08-02 11:09:51 -0400399 }
James Smart92d7f7b2007-06-17 19:56:38 -0500400 }
James Smart09372822008-01-11 01:52:54 -0500401 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500402 atomic_set(&phba->num_rsrc_err, 0);
403 atomic_set(&phba->num_cmd_success, 0);
404}
405
James Smart9bad7672008-12-04 22:39:02 -0500406/**
James Smart3621a712009-04-06 18:47:14 -0400407 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500408 * @phba: The Hba for which this call is being executed.
409 *
410 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
411 * thread.This routine increases queue depth for all scsi device on each vport
412 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
413 * num_cmd_success to zero.
414 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500415void
416lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
417{
James Smart549e55c2007-08-02 11:09:51 -0400418 struct lpfc_vport **vports;
419 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500420 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400421 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500422
James Smart549e55c2007-08-02 11:09:51 -0400423 vports = lpfc_create_vport_work_array(phba);
424 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400425 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400426 shost = lpfc_shost_from_vport(vports[i]);
427 shost_for_each_device(sdev, shost) {
James Smart97eab632008-04-07 10:16:05 -0400428 if (vports[i]->cfg_lun_queue_depth <=
429 sdev->queue_depth)
430 continue;
James Smart5ffc2662009-11-18 15:39:44 -0500431 lpfc_change_queue_depth(sdev,
432 sdev->queue_depth+1,
433 SCSI_QDEPTH_RAMP_UP);
James Smart549e55c2007-08-02 11:09:51 -0400434 }
James Smart92d7f7b2007-06-17 19:56:38 -0500435 }
James Smart09372822008-01-11 01:52:54 -0500436 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500437 atomic_set(&phba->num_rsrc_err, 0);
438 atomic_set(&phba->num_cmd_success, 0);
439}
440
James Smarta8e497d2008-08-24 21:50:11 -0400441/**
James Smart3621a712009-04-06 18:47:14 -0400442 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400443 * @phba: Pointer to HBA context object.
444 *
445 * This function walks vport list and set each SCSI host to block state
446 * by invoking fc_remote_port_delete() routine. This function is invoked
447 * with EEH when device's PCI slot has been permanently disabled.
448 **/
449void
450lpfc_scsi_dev_block(struct lpfc_hba *phba)
451{
452 struct lpfc_vport **vports;
453 struct Scsi_Host *shost;
454 struct scsi_device *sdev;
455 struct fc_rport *rport;
456 int i;
457
458 vports = lpfc_create_vport_work_array(phba);
459 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400460 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400461 shost = lpfc_shost_from_vport(vports[i]);
462 shost_for_each_device(sdev, shost) {
463 rport = starget_to_rport(scsi_target(sdev));
464 fc_remote_port_delete(rport);
465 }
466 }
467 lpfc_destroy_vport_work_array(phba, vports);
468}
469
James Smart9bad7672008-12-04 22:39:02 -0500470/**
James Smart3772a992009-05-22 14:50:54 -0400471 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500472 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400473 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500474 *
James Smart3772a992009-05-22 14:50:54 -0400475 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
476 * the scsi buffer contains all the necessary information needed to initiate
477 * a SCSI I/O. The non-DMAable buffer region contains information to build
478 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
479 * and the initial BPL. In addition to allocating memory, the FCP CMND and
480 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500481 *
482 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400483 * int - number of scsi buffers that were allocated.
484 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500485 **/
James Smart3772a992009-05-22 14:50:54 -0400486static int
487lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500488{
James Smart2e0fef82007-06-17 19:56:36 -0500489 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500490 struct lpfc_scsi_buf *psb;
491 struct ulp_bde64 *bpl;
492 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400493 dma_addr_t pdma_phys_fcp_cmd;
494 dma_addr_t pdma_phys_fcp_rsp;
495 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500496 uint16_t iotag;
James Smart3772a992009-05-22 14:50:54 -0400497 int bcnt;
dea31012005-04-17 16:05:31 -0500498
James Smart3772a992009-05-22 14:50:54 -0400499 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
500 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
501 if (!psb)
502 break;
dea31012005-04-17 16:05:31 -0500503
James Smart3772a992009-05-22 14:50:54 -0400504 /*
505 * Get memory from the pci pool to map the virt space to pci
506 * bus space for an I/O. The DMA buffer includes space for the
507 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
508 * necessary to support the sg_tablesize.
509 */
510 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
511 GFP_KERNEL, &psb->dma_handle);
512 if (!psb->data) {
513 kfree(psb);
514 break;
515 }
dea31012005-04-17 16:05:31 -0500516
James Smart3772a992009-05-22 14:50:54 -0400517 /* Initialize virtual ptrs to dma_buf region. */
518 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
dea31012005-04-17 16:05:31 -0500519
James Smart3772a992009-05-22 14:50:54 -0400520 /* Allocate iotag for psb->cur_iocbq. */
521 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
522 if (iotag == 0) {
523 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
524 psb->data, psb->dma_handle);
525 kfree(psb);
526 break;
527 }
528 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500529
James Smart3772a992009-05-22 14:50:54 -0400530 psb->fcp_cmnd = psb->data;
531 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
532 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400533 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500534
James Smart3772a992009-05-22 14:50:54 -0400535 /* Initialize local short-hand pointers. */
536 bpl = psb->fcp_bpl;
537 pdma_phys_fcp_cmd = psb->dma_handle;
538 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
539 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
540 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500541
James Smart3772a992009-05-22 14:50:54 -0400542 /*
543 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
544 * are sg list bdes. Initialize the first two and leave the
545 * rest for queuecommand.
546 */
547 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
548 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
549 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
550 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
551 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500552
James Smart3772a992009-05-22 14:50:54 -0400553 /* Setup the physical region for the FCP RSP */
554 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
555 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
556 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
557 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
558 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
559
560 /*
561 * Since the IOCB for the FCP I/O is built into this
562 * lpfc_scsi_buf, initialize it with all known data now.
563 */
564 iocb = &psb->cur_iocbq.iocb;
565 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
566 if ((phba->sli_rev == 3) &&
567 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
568 /* fill in immediate fcp command BDE */
569 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
570 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
571 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
572 unsli3.fcp_ext.icd);
573 iocb->un.fcpi64.bdl.addrHigh = 0;
574 iocb->ulpBdeCount = 0;
575 iocb->ulpLe = 0;
576 /* fill in responce BDE */
577 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
578 BUFF_TYPE_BDE_64;
579 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
580 sizeof(struct fcp_rsp);
581 iocb->unsli3.fcp_ext.rbde.addrLow =
582 putPaddrLow(pdma_phys_fcp_rsp);
583 iocb->unsli3.fcp_ext.rbde.addrHigh =
584 putPaddrHigh(pdma_phys_fcp_rsp);
585 } else {
586 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
587 iocb->un.fcpi64.bdl.bdeSize =
588 (2 * sizeof(struct ulp_bde64));
589 iocb->un.fcpi64.bdl.addrLow =
590 putPaddrLow(pdma_phys_bpl);
591 iocb->un.fcpi64.bdl.addrHigh =
592 putPaddrHigh(pdma_phys_bpl);
593 iocb->ulpBdeCount = 1;
594 iocb->ulpLe = 1;
595 }
596 iocb->ulpClass = CLASS3;
597 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400598 /* Put it back into the SCSI buffer list */
599 lpfc_release_scsi_buf_s4(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400600
James Smart34b02dc2008-08-24 21:49:55 -0400601 }
dea31012005-04-17 16:05:31 -0500602
James Smart3772a992009-05-22 14:50:54 -0400603 return bcnt;
dea31012005-04-17 16:05:31 -0500604}
605
James Smart9bad7672008-12-04 22:39:02 -0500606/**
James Smartda0436e2009-05-22 14:51:39 -0400607 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
608 * @phba: pointer to lpfc hba data structure.
609 * @axri: pointer to the fcp xri abort wcqe structure.
610 *
611 * This routine is invoked by the worker thread to process a SLI4 fast-path
612 * FCP aborted xri.
613 **/
614void
615lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
616 struct sli4_wcqe_xri_aborted *axri)
617{
618 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
619 struct lpfc_scsi_buf *psb, *next_psb;
620 unsigned long iflag = 0;
621
622 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock, iflag);
623 list_for_each_entry_safe(psb, next_psb,
624 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
625 if (psb->cur_iocbq.sli4_xritag == xri) {
626 list_del(&psb->list);
627 psb->status = IOSTAT_SUCCESS;
628 spin_unlock_irqrestore(
629 &phba->sli4_hba.abts_scsi_buf_list_lock,
630 iflag);
631 lpfc_release_scsi_buf_s4(phba, psb);
632 return;
633 }
634 }
635 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
636 iflag);
637}
638
639/**
640 * lpfc_sli4_repost_scsi_sgl_list - Repsot the Scsi buffers sgl pages as block
641 * @phba: pointer to lpfc hba data structure.
642 *
643 * This routine walks the list of scsi buffers that have been allocated and
644 * repost them to the HBA by using SGL block post. This is needed after a
645 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
646 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
647 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
648 *
649 * Returns: 0 = success, non-zero failure.
650 **/
651int
652lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
653{
654 struct lpfc_scsi_buf *psb;
655 int index, status, bcnt = 0, rcnt = 0, rc = 0;
656 LIST_HEAD(sblist);
657
658 for (index = 0; index < phba->sli4_hba.scsi_xri_cnt; index++) {
659 psb = phba->sli4_hba.lpfc_scsi_psb_array[index];
660 if (psb) {
661 /* Remove from SCSI buffer list */
662 list_del(&psb->list);
663 /* Add it to a local SCSI buffer list */
664 list_add_tail(&psb->list, &sblist);
665 if (++rcnt == LPFC_NEMBED_MBOX_SGL_CNT) {
666 bcnt = rcnt;
667 rcnt = 0;
668 }
669 } else
670 /* A hole present in the XRI array, need to skip */
671 bcnt = rcnt;
672
673 if (index == phba->sli4_hba.scsi_xri_cnt - 1)
674 /* End of XRI array for SCSI buffer, complete */
675 bcnt = rcnt;
676
677 /* Continue until collect up to a nembed page worth of sgls */
678 if (bcnt == 0)
679 continue;
680 /* Now, post the SCSI buffer list sgls as a block */
681 status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
682 /* Reset SCSI buffer count for next round of posting */
683 bcnt = 0;
684 while (!list_empty(&sblist)) {
685 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
686 list);
687 if (status) {
688 /* Put this back on the abort scsi list */
689 psb->status = IOSTAT_LOCAL_REJECT;
690 psb->result = IOERR_ABORT_REQUESTED;
691 rc++;
692 } else
693 psb->status = IOSTAT_SUCCESS;
694 /* Put it back into the SCSI buffer list */
695 lpfc_release_scsi_buf_s4(phba, psb);
696 }
697 }
698 return rc;
699}
700
701/**
702 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
703 * @vport: The virtual port for which this call being executed.
704 * @num_to_allocate: The requested number of buffers to allocate.
705 *
706 * This routine allocates a scsi buffer for device with SLI-4 interface spec,
707 * the scsi buffer contains all the necessary information needed to initiate
708 * a SCSI I/O.
709 *
710 * Return codes:
711 * int - number of scsi buffers that were allocated.
712 * 0 = failure, less than num_to_alloc is a partial failure.
713 **/
714static int
715lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
716{
717 struct lpfc_hba *phba = vport->phba;
718 struct lpfc_scsi_buf *psb;
719 struct sli4_sge *sgl;
720 IOCB_t *iocb;
721 dma_addr_t pdma_phys_fcp_cmd;
722 dma_addr_t pdma_phys_fcp_rsp;
723 dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
724 uint16_t iotag, last_xritag = NO_XRI;
725 int status = 0, index;
726 int bcnt;
727 int non_sequential_xri = 0;
728 int rc = 0;
729 LIST_HEAD(sblist);
730
731 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
732 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
733 if (!psb)
734 break;
735
736 /*
737 * Get memory from the pci pool to map the virt space to pci bus
738 * space for an I/O. The DMA buffer includes space for the
739 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
740 * necessary to support the sg_tablesize.
741 */
742 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
743 GFP_KERNEL, &psb->dma_handle);
744 if (!psb->data) {
745 kfree(psb);
746 break;
747 }
748
749 /* Initialize virtual ptrs to dma_buf region. */
750 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
751
752 /* Allocate iotag for psb->cur_iocbq. */
753 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
754 if (iotag == 0) {
755 kfree(psb);
756 break;
757 }
758
759 psb->cur_iocbq.sli4_xritag = lpfc_sli4_next_xritag(phba);
760 if (psb->cur_iocbq.sli4_xritag == NO_XRI) {
761 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
762 psb->data, psb->dma_handle);
763 kfree(psb);
764 break;
765 }
766 if (last_xritag != NO_XRI
767 && psb->cur_iocbq.sli4_xritag != (last_xritag+1)) {
768 non_sequential_xri = 1;
769 } else
770 list_add_tail(&psb->list, &sblist);
771 last_xritag = psb->cur_iocbq.sli4_xritag;
772
773 index = phba->sli4_hba.scsi_xri_cnt++;
774 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
775
776 psb->fcp_bpl = psb->data;
777 psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
778 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
779 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
780 sizeof(struct fcp_cmnd));
781
782 /* Initialize local short-hand pointers. */
783 sgl = (struct sli4_sge *)psb->fcp_bpl;
784 pdma_phys_bpl = psb->dma_handle;
785 pdma_phys_fcp_cmd =
786 (psb->dma_handle + phba->cfg_sg_dma_buf_size)
787 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
788 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
789
790 /*
791 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
792 * are sg list bdes. Initialize the first two and leave the
793 * rest for queuecommand.
794 */
795 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
796 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
797 bf_set(lpfc_sli4_sge_len, sgl, sizeof(struct fcp_cmnd));
798 bf_set(lpfc_sli4_sge_last, sgl, 0);
799 sgl->word2 = cpu_to_le32(sgl->word2);
800 sgl->word3 = cpu_to_le32(sgl->word3);
801 sgl++;
802
803 /* Setup the physical region for the FCP RSP */
804 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
805 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
806 bf_set(lpfc_sli4_sge_len, sgl, sizeof(struct fcp_rsp));
807 bf_set(lpfc_sli4_sge_last, sgl, 1);
808 sgl->word2 = cpu_to_le32(sgl->word2);
809 sgl->word3 = cpu_to_le32(sgl->word3);
810
811 /*
812 * Since the IOCB for the FCP I/O is built into this
813 * lpfc_scsi_buf, initialize it with all known data now.
814 */
815 iocb = &psb->cur_iocbq.iocb;
816 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
817 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
818 /* setting the BLP size to 2 * sizeof BDE may not be correct.
819 * We are setting the bpl to point to out sgl. An sgl's
820 * entries are 16 bytes, a bpl entries are 12 bytes.
821 */
822 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
823 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
824 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
825 iocb->ulpBdeCount = 1;
826 iocb->ulpLe = 1;
827 iocb->ulpClass = CLASS3;
828 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
829 pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
830 else
831 pdma_phys_bpl1 = 0;
832 psb->dma_phys_bpl = pdma_phys_bpl;
833 phba->sli4_hba.lpfc_scsi_psb_array[index] = psb;
834 if (non_sequential_xri) {
835 status = lpfc_sli4_post_sgl(phba, pdma_phys_bpl,
836 pdma_phys_bpl1,
837 psb->cur_iocbq.sli4_xritag);
838 if (status) {
839 /* Put this back on the abort scsi list */
840 psb->status = IOSTAT_LOCAL_REJECT;
841 psb->result = IOERR_ABORT_REQUESTED;
842 rc++;
843 } else
844 psb->status = IOSTAT_SUCCESS;
845 /* Put it back into the SCSI buffer list */
846 lpfc_release_scsi_buf_s4(phba, psb);
847 break;
848 }
849 }
850 if (bcnt) {
851 status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
852 /* Reset SCSI buffer count for next round of posting */
853 while (!list_empty(&sblist)) {
854 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
855 list);
856 if (status) {
857 /* Put this back on the abort scsi list */
858 psb->status = IOSTAT_LOCAL_REJECT;
859 psb->result = IOERR_ABORT_REQUESTED;
860 rc++;
861 } else
862 psb->status = IOSTAT_SUCCESS;
863 /* Put it back into the SCSI buffer list */
864 lpfc_release_scsi_buf_s4(phba, psb);
865 }
866 }
867
868 return bcnt + non_sequential_xri - rc;
869}
870
871/**
James Smart3772a992009-05-22 14:50:54 -0400872 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
873 * @vport: The virtual port for which this call being executed.
874 * @num_to_allocate: The requested number of buffers to allocate.
875 *
876 * This routine wraps the actual SCSI buffer allocator function pointer from
877 * the lpfc_hba struct.
878 *
879 * Return codes:
880 * int - number of scsi buffers that were allocated.
881 * 0 = failure, less than num_to_alloc is a partial failure.
882 **/
883static inline int
884lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
885{
886 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
887}
888
889/**
890 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
891 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -0500892 *
893 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
894 * and returns to caller.
895 *
896 * Return codes:
897 * NULL - Error
898 * Pointer to lpfc_scsi_buf - Success
899 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +0100900static struct lpfc_scsi_buf*
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500901lpfc_get_scsi_buf(struct lpfc_hba * phba)
dea31012005-04-17 16:05:31 -0500902{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400903 struct lpfc_scsi_buf * lpfc_cmd = NULL;
904 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500905 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500906
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500907 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400908 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -0400909 if (lpfc_cmd) {
910 lpfc_cmd->seg_cnt = 0;
911 lpfc_cmd->nonsg_phys = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -0500912 lpfc_cmd->prot_seg_cnt = 0;
James Smart1dcb58e2007-04-25 09:51:30 -0400913 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500914 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400915 return lpfc_cmd;
916}
917
James Smart9bad7672008-12-04 22:39:02 -0500918/**
James Smart3772a992009-05-22 14:50:54 -0400919 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -0500920 * @phba: The Hba for which this call is being executed.
921 * @psb: The scsi buffer which is being released.
922 *
923 * This routine releases @psb scsi buffer by adding it to tail of @phba
924 * lpfc_scsi_buf_list list.
925 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400926static void
James Smart3772a992009-05-22 14:50:54 -0400927lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400928{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500929 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500930
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500931 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400932 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -0500933 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500934 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -0500935}
936
James Smart9bad7672008-12-04 22:39:02 -0500937/**
James Smartda0436e2009-05-22 14:51:39 -0400938 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
939 * @phba: The Hba for which this call is being executed.
940 * @psb: The scsi buffer which is being released.
941 *
942 * This routine releases @psb scsi buffer by adding it to tail of @phba
943 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
944 * and cannot be reused for at least RA_TOV amount of time if it was
945 * aborted.
946 **/
947static void
948lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
949{
950 unsigned long iflag = 0;
951
952 if (psb->status == IOSTAT_LOCAL_REJECT
953 && psb->result == IOERR_ABORT_REQUESTED) {
954 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
955 iflag);
956 psb->pCmd = NULL;
957 list_add_tail(&psb->list,
958 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
959 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
960 iflag);
961 } else {
962
963 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
964 psb->pCmd = NULL;
965 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
966 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
967 }
968}
969
970/**
James Smart3772a992009-05-22 14:50:54 -0400971 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
972 * @phba: The Hba for which this call is being executed.
973 * @psb: The scsi buffer which is being released.
974 *
975 * This routine releases @psb scsi buffer by adding it to tail of @phba
976 * lpfc_scsi_buf_list list.
977 **/
978static void
979lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
980{
981
982 phba->lpfc_release_scsi_buf(phba, psb);
983}
984
985/**
986 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500987 * @phba: The Hba for which this call is being executed.
988 * @lpfc_cmd: The scsi buffer which is going to be mapped.
989 *
990 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -0400991 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
992 * through sg elements and format the bdea. This routine also initializes all
993 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -0500994 *
995 * Return codes:
996 * 1 - Error
997 * 0 - Success
998 **/
dea31012005-04-17 16:05:31 -0500999static int
James Smart3772a992009-05-22 14:50:54 -04001000lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001001{
1002 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1003 struct scatterlist *sgel = NULL;
1004 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1005 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1006 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001007 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001008 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001009 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001010 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001011
1012 /*
1013 * There are three possibilities here - use scatter-gather segment, use
1014 * the single mapping, or neither. Start the lpfc command prep by
1015 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1016 * data bde entry.
1017 */
1018 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001019 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001020 /*
1021 * The driver stores the segment count returned from pci_map_sg
1022 * because this a count of dma-mappings used to map the use_sg
1023 * pages. They are not guaranteed to be the same for those
1024 * architectures that implement an IOMMU.
1025 */
dea31012005-04-17 16:05:31 -05001026
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001027 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1028 scsi_sg_count(scsi_cmnd), datadir);
1029 if (unlikely(!nseg))
1030 return 1;
1031
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001032 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001033 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001034 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1035 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001036 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001037 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001038 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001039 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001040 return 1;
1041 }
1042
1043 /*
1044 * The driver established a maximum scatter-gather segment count
1045 * during probe that limits the number of sg elements in any
1046 * single scsi command. Just run through the seg_cnt and format
1047 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001048 * When using SLI-3 the driver will try to fit all the BDEs into
1049 * the IOCB. If it can't then the BDEs get added to a BPL as it
1050 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001051 */
James Smart34b02dc2008-08-24 21:49:55 -04001052 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001053 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001054 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001055 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart34b02dc2008-08-24 21:49:55 -04001056 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1057 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1058 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1059 data_bde->addrLow = putPaddrLow(physaddr);
1060 data_bde->addrHigh = putPaddrHigh(physaddr);
1061 data_bde++;
1062 } else {
1063 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1064 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1065 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1066 bpl->addrLow =
1067 le32_to_cpu(putPaddrLow(physaddr));
1068 bpl->addrHigh =
1069 le32_to_cpu(putPaddrHigh(physaddr));
1070 bpl++;
1071 }
dea31012005-04-17 16:05:31 -05001072 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001073 }
dea31012005-04-17 16:05:31 -05001074
1075 /*
1076 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001077 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1078 * explicitly reinitialized and for SLI-3 the extended bde count is
1079 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001080 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001081 if (phba->sli_rev == 3 &&
1082 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
James Smart34b02dc2008-08-24 21:49:55 -04001083 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1084 /*
1085 * The extended IOCB format can only fit 3 BDE or a BPL.
1086 * This I/O has more than 3 BDE so the 1st data bde will
1087 * be a BPL that is filled in here.
1088 */
1089 physaddr = lpfc_cmd->dma_handle;
1090 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1091 data_bde->tus.f.bdeSize = (num_bde *
1092 sizeof(struct ulp_bde64));
1093 physaddr += (sizeof(struct fcp_cmnd) +
1094 sizeof(struct fcp_rsp) +
1095 (2 * sizeof(struct ulp_bde64)));
1096 data_bde->addrHigh = putPaddrHigh(physaddr);
1097 data_bde->addrLow = putPaddrLow(physaddr);
1098 /* ebde count includes the responce bde and data bpl */
1099 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1100 } else {
1101 /* ebde count includes the responce bde and data bdes */
1102 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1103 }
1104 } else {
1105 iocb_cmd->un.fcpi64.bdl.bdeSize =
1106 ((num_bde + 2) * sizeof(struct ulp_bde64));
1107 }
James Smart09372822008-01-11 01:52:54 -05001108 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001109
1110 /*
1111 * Due to difference in data length between DIF/non-DIF paths,
1112 * we need to set word 4 of IOCB here
1113 */
James Smarta257bf92009-04-06 18:48:10 -04001114 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001115 return 0;
1116}
1117
James Smarte2a0a9d2008-12-04 22:40:02 -05001118/*
1119 * Given a scsi cmnd, determine the BlockGuard profile to be used
1120 * with the cmd
1121 */
1122static int
James Smart6a9c52c2009-10-02 15:16:51 -04001123lpfc_sc_to_sli_prof(struct lpfc_hba *phba, struct scsi_cmnd *sc)
James Smarte2a0a9d2008-12-04 22:40:02 -05001124{
1125 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
1126 uint8_t ret_prof = LPFC_PROF_INVALID;
1127
1128 if (guard_type == SHOST_DIX_GUARD_IP) {
1129 switch (scsi_get_prot_op(sc)) {
1130 case SCSI_PROT_READ_INSERT:
1131 case SCSI_PROT_WRITE_STRIP:
1132 ret_prof = LPFC_PROF_AST2;
1133 break;
1134
1135 case SCSI_PROT_READ_STRIP:
1136 case SCSI_PROT_WRITE_INSERT:
1137 ret_prof = LPFC_PROF_A1;
1138 break;
1139
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001140 case SCSI_PROT_READ_PASS:
1141 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05001142 ret_prof = LPFC_PROF_AST1;
1143 break;
1144
James Smarte2a0a9d2008-12-04 22:40:02 -05001145 case SCSI_PROT_NORMAL:
1146 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001147 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1148 "9063 BLKGRD:Bad op/guard:%d/%d combination\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001149 scsi_get_prot_op(sc), guard_type);
1150 break;
1151
1152 }
1153 } else if (guard_type == SHOST_DIX_GUARD_CRC) {
1154 switch (scsi_get_prot_op(sc)) {
1155 case SCSI_PROT_READ_STRIP:
1156 case SCSI_PROT_WRITE_INSERT:
1157 ret_prof = LPFC_PROF_A1;
1158 break;
1159
1160 case SCSI_PROT_READ_PASS:
1161 case SCSI_PROT_WRITE_PASS:
1162 ret_prof = LPFC_PROF_C1;
1163 break;
1164
James Smarte2a0a9d2008-12-04 22:40:02 -05001165 case SCSI_PROT_READ_INSERT:
1166 case SCSI_PROT_WRITE_STRIP:
1167 case SCSI_PROT_NORMAL:
1168 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001169 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1170 "9075 BLKGRD: Bad op/guard:%d/%d combination\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001171 scsi_get_prot_op(sc), guard_type);
1172 break;
1173 }
1174 } else {
1175 /* unsupported format */
1176 BUG();
1177 }
1178
1179 return ret_prof;
1180}
1181
1182struct scsi_dif_tuple {
1183 __be16 guard_tag; /* Checksum */
1184 __be16 app_tag; /* Opaque storage */
1185 __be32 ref_tag; /* Target LBA or indirect LBA */
1186};
1187
1188static inline unsigned
1189lpfc_cmd_blksize(struct scsi_cmnd *sc)
1190{
1191 return sc->device->sector_size;
1192}
1193
1194/**
1195 * lpfc_get_cmd_dif_parms - Extract DIF parameters from SCSI command
1196 * @sc: in: SCSI command
James Smart3621a712009-04-06 18:47:14 -04001197 * @apptagmask: out: app tag mask
1198 * @apptagval: out: app tag value
1199 * @reftag: out: ref tag (reference tag)
James Smarte2a0a9d2008-12-04 22:40:02 -05001200 *
1201 * Description:
Martin Olsson98a17082009-04-22 18:21:29 +02001202 * Extract DIF parameters from the command if possible. Otherwise,
1203 * use default parameters.
James Smarte2a0a9d2008-12-04 22:40:02 -05001204 *
1205 **/
1206static inline void
1207lpfc_get_cmd_dif_parms(struct scsi_cmnd *sc, uint16_t *apptagmask,
1208 uint16_t *apptagval, uint32_t *reftag)
1209{
1210 struct scsi_dif_tuple *spt;
1211 unsigned char op = scsi_get_prot_op(sc);
1212 unsigned int protcnt = scsi_prot_sg_count(sc);
1213 static int cnt;
1214
1215 if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001216 op == SCSI_PROT_WRITE_PASS)) {
James Smarte2a0a9d2008-12-04 22:40:02 -05001217
1218 cnt++;
1219 spt = page_address(sg_page(scsi_prot_sglist(sc))) +
1220 scsi_prot_sglist(sc)[0].offset;
1221 *apptagmask = 0;
1222 *apptagval = 0;
1223 *reftag = cpu_to_be32(spt->ref_tag);
1224
1225 } else {
1226 /* SBC defines ref tag to be lower 32bits of LBA */
1227 *reftag = (uint32_t) (0xffffffff & scsi_get_lba(sc));
1228 *apptagmask = 0;
1229 *apptagval = 0;
1230 }
1231}
1232
1233/*
1234 * This function sets up buffer list for protection groups of
1235 * type LPFC_PG_TYPE_NO_DIF
1236 *
1237 * This is usually used when the HBA is instructed to generate
1238 * DIFs and insert them into data stream (or strip DIF from
1239 * incoming data stream)
1240 *
1241 * The buffer list consists of just one protection group described
1242 * below:
1243 * +-------------------------+
1244 * start of prot group --> | PDE_1 |
1245 * +-------------------------+
1246 * | Data BDE |
1247 * +-------------------------+
1248 * |more Data BDE's ... (opt)|
1249 * +-------------------------+
1250 *
1251 * @sc: pointer to scsi command we're working on
1252 * @bpl: pointer to buffer list for protection groups
1253 * @datacnt: number of segments of data that have been dma mapped
1254 *
1255 * Note: Data s/g buffers have been dma mapped
1256 */
1257static int
1258lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1259 struct ulp_bde64 *bpl, int datasegcnt)
1260{
1261 struct scatterlist *sgde = NULL; /* s/g data entry */
1262 struct lpfc_pde *pde1 = NULL;
1263 dma_addr_t physaddr;
1264 int i = 0, num_bde = 0;
1265 int datadir = sc->sc_data_direction;
1266 int prof = LPFC_PROF_INVALID;
1267 unsigned blksize;
1268 uint32_t reftag;
1269 uint16_t apptagmask, apptagval;
1270
1271 pde1 = (struct lpfc_pde *) bpl;
James Smart6a9c52c2009-10-02 15:16:51 -04001272 prof = lpfc_sc_to_sli_prof(phba, sc);
James Smarte2a0a9d2008-12-04 22:40:02 -05001273
1274 if (prof == LPFC_PROF_INVALID)
1275 goto out;
1276
1277 /* extract some info from the scsi command for PDE1*/
1278 blksize = lpfc_cmd_blksize(sc);
1279 lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1280
1281 /* setup PDE1 with what we have */
1282 lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
1283 BG_EC_STOP_ERR);
1284 lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
1285
1286 num_bde++;
1287 bpl++;
1288
1289 /* assumption: caller has already run dma_map_sg on command data */
1290 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1291 physaddr = sg_dma_address(sgde);
1292 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1293 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1294 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1295 if (datadir == DMA_TO_DEVICE)
1296 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1297 else
1298 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1299 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1300 bpl++;
1301 num_bde++;
1302 }
1303
1304out:
1305 return num_bde;
1306}
1307
1308/*
1309 * This function sets up buffer list for protection groups of
1310 * type LPFC_PG_TYPE_DIF_BUF
1311 *
1312 * This is usually used when DIFs are in their own buffers,
1313 * separate from the data. The HBA can then by instructed
1314 * to place the DIFs in the outgoing stream. For read operations,
1315 * The HBA could extract the DIFs and place it in DIF buffers.
1316 *
1317 * The buffer list for this type consists of one or more of the
1318 * protection groups described below:
1319 * +-------------------------+
1320 * start of first prot group --> | PDE_1 |
1321 * +-------------------------+
1322 * | PDE_3 (Prot BDE) |
1323 * +-------------------------+
1324 * | Data BDE |
1325 * +-------------------------+
1326 * |more Data BDE's ... (opt)|
1327 * +-------------------------+
1328 * start of new prot group --> | PDE_1 |
1329 * +-------------------------+
1330 * | ... |
1331 * +-------------------------+
1332 *
1333 * @sc: pointer to scsi command we're working on
1334 * @bpl: pointer to buffer list for protection groups
1335 * @datacnt: number of segments of data that have been dma mapped
1336 * @protcnt: number of segment of protection data that have been dma mapped
1337 *
1338 * Note: It is assumed that both data and protection s/g buffers have been
1339 * mapped for DMA
1340 */
1341static int
1342lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1343 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1344{
1345 struct scatterlist *sgde = NULL; /* s/g data entry */
1346 struct scatterlist *sgpe = NULL; /* s/g prot entry */
1347 struct lpfc_pde *pde1 = NULL;
1348 struct ulp_bde64 *prot_bde = NULL;
1349 dma_addr_t dataphysaddr, protphysaddr;
1350 unsigned short curr_data = 0, curr_prot = 0;
1351 unsigned int split_offset, protgroup_len;
1352 unsigned int protgrp_blks, protgrp_bytes;
1353 unsigned int remainder, subtotal;
1354 int prof = LPFC_PROF_INVALID;
1355 int datadir = sc->sc_data_direction;
1356 unsigned char pgdone = 0, alldone = 0;
1357 unsigned blksize;
1358 uint32_t reftag;
1359 uint16_t apptagmask, apptagval;
1360 int num_bde = 0;
1361
1362 sgpe = scsi_prot_sglist(sc);
1363 sgde = scsi_sglist(sc);
1364
1365 if (!sgpe || !sgde) {
1366 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1367 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
1368 sgpe, sgde);
1369 return 0;
1370 }
1371
James Smart6a9c52c2009-10-02 15:16:51 -04001372 prof = lpfc_sc_to_sli_prof(phba, sc);
James Smarte2a0a9d2008-12-04 22:40:02 -05001373 if (prof == LPFC_PROF_INVALID)
1374 goto out;
1375
1376 /* extract some info from the scsi command for PDE1*/
1377 blksize = lpfc_cmd_blksize(sc);
1378 lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1379
1380 split_offset = 0;
1381 do {
1382 /* setup the first PDE_1 */
1383 pde1 = (struct lpfc_pde *) bpl;
1384
1385 lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
1386 BG_EC_STOP_ERR);
1387 lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
1388
1389 num_bde++;
1390 bpl++;
1391
1392 /* setup the first BDE that points to protection buffer */
1393 prot_bde = (struct ulp_bde64 *) bpl;
1394 protphysaddr = sg_dma_address(sgpe);
1395 prot_bde->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
1396 prot_bde->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1397 protgroup_len = sg_dma_len(sgpe);
1398
1399
1400 /* must be integer multiple of the DIF block length */
1401 BUG_ON(protgroup_len % 8);
1402
1403 protgrp_blks = protgroup_len / 8;
1404 protgrp_bytes = protgrp_blks * blksize;
1405
1406 prot_bde->tus.f.bdeSize = protgroup_len;
1407 if (datadir == DMA_TO_DEVICE)
1408 prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1409 else
1410 prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1411 prot_bde->tus.w = le32_to_cpu(bpl->tus.w);
1412
1413 curr_prot++;
1414 num_bde++;
1415
1416 /* setup BDE's for data blocks associated with DIF data */
1417 pgdone = 0;
1418 subtotal = 0; /* total bytes processed for current prot grp */
1419 while (!pgdone) {
1420 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04001421 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1422 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001423 __func__);
1424 return 0;
1425 }
1426 bpl++;
1427 dataphysaddr = sg_dma_address(sgde) + split_offset;
1428 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1429 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1430
1431 remainder = sg_dma_len(sgde) - split_offset;
1432
1433 if ((subtotal + remainder) <= protgrp_bytes) {
1434 /* we can use this whole buffer */
1435 bpl->tus.f.bdeSize = remainder;
1436 split_offset = 0;
1437
1438 if ((subtotal + remainder) == protgrp_bytes)
1439 pgdone = 1;
1440 } else {
1441 /* must split this buffer with next prot grp */
1442 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1443 split_offset += bpl->tus.f.bdeSize;
1444 }
1445
1446 subtotal += bpl->tus.f.bdeSize;
1447
1448 if (datadir == DMA_TO_DEVICE)
1449 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1450 else
1451 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1452 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1453
1454 num_bde++;
1455 curr_data++;
1456
1457 if (split_offset)
1458 break;
1459
1460 /* Move to the next s/g segment if possible */
1461 sgde = sg_next(sgde);
1462 }
1463
1464 /* are we done ? */
1465 if (curr_prot == protcnt) {
1466 alldone = 1;
1467 } else if (curr_prot < protcnt) {
1468 /* advance to next prot buffer */
1469 sgpe = sg_next(sgpe);
1470 bpl++;
1471
1472 /* update the reference tag */
1473 reftag += protgrp_blks;
1474 } else {
1475 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04001476 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1477 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05001478 }
1479
1480 } while (!alldone);
1481
1482out:
1483
1484
1485 return num_bde;
1486}
1487/*
1488 * Given a SCSI command that supports DIF, determine composition of protection
1489 * groups involved in setting up buffer lists
1490 *
1491 * Returns:
1492 * for DIF (for both read and write)
1493 * */
1494static int
1495lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
1496{
1497 int ret = LPFC_PG_TYPE_INVALID;
1498 unsigned char op = scsi_get_prot_op(sc);
1499
1500 switch (op) {
1501 case SCSI_PROT_READ_STRIP:
1502 case SCSI_PROT_WRITE_INSERT:
1503 ret = LPFC_PG_TYPE_NO_DIF;
1504 break;
1505 case SCSI_PROT_READ_INSERT:
1506 case SCSI_PROT_WRITE_STRIP:
1507 case SCSI_PROT_READ_PASS:
1508 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05001509 ret = LPFC_PG_TYPE_DIF_BUF;
1510 break;
1511 default:
1512 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1513 "9021 Unsupported protection op:%d\n", op);
1514 break;
1515 }
1516
1517 return ret;
1518}
1519
1520/*
1521 * This is the protection/DIF aware version of
1522 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
1523 * two functions eventually, but for now, it's here
1524 */
1525static int
1526lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba,
1527 struct lpfc_scsi_buf *lpfc_cmd)
1528{
1529 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1530 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1531 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1532 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1533 uint32_t num_bde = 0;
1534 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
1535 int prot_group_type = 0;
1536 int diflen, fcpdl;
1537 unsigned blksize;
1538
1539 /*
1540 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
1541 * fcp_rsp regions to the first data bde entry
1542 */
1543 bpl += 2;
1544 if (scsi_sg_count(scsi_cmnd)) {
1545 /*
1546 * The driver stores the segment count returned from pci_map_sg
1547 * because this a count of dma-mappings used to map the use_sg
1548 * pages. They are not guaranteed to be the same for those
1549 * architectures that implement an IOMMU.
1550 */
1551 datasegcnt = dma_map_sg(&phba->pcidev->dev,
1552 scsi_sglist(scsi_cmnd),
1553 scsi_sg_count(scsi_cmnd), datadir);
1554 if (unlikely(!datasegcnt))
1555 return 1;
1556
1557 lpfc_cmd->seg_cnt = datasegcnt;
1558 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001559 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1560 "9067 BLKGRD: %s: Too many sg segments"
1561 " from dma_map_sg. Config %d, seg_cnt"
1562 " %d\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001563 __func__, phba->cfg_sg_seg_cnt,
1564 lpfc_cmd->seg_cnt);
1565 scsi_dma_unmap(scsi_cmnd);
1566 return 1;
1567 }
1568
1569 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
1570
1571 switch (prot_group_type) {
1572 case LPFC_PG_TYPE_NO_DIF:
1573 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
1574 datasegcnt);
1575 /* we shoud have 2 or more entries in buffer list */
1576 if (num_bde < 2)
1577 goto err;
1578 break;
1579 case LPFC_PG_TYPE_DIF_BUF:{
1580 /*
1581 * This type indicates that protection buffers are
1582 * passed to the driver, so that needs to be prepared
1583 * for DMA
1584 */
1585 protsegcnt = dma_map_sg(&phba->pcidev->dev,
1586 scsi_prot_sglist(scsi_cmnd),
1587 scsi_prot_sg_count(scsi_cmnd), datadir);
1588 if (unlikely(!protsegcnt)) {
1589 scsi_dma_unmap(scsi_cmnd);
1590 return 1;
1591 }
1592
1593 lpfc_cmd->prot_seg_cnt = protsegcnt;
1594 if (lpfc_cmd->prot_seg_cnt
1595 > phba->cfg_prot_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001596 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1597 "9068 BLKGRD: %s: Too many prot sg "
1598 "segments from dma_map_sg. Config %d,"
James Smarte2a0a9d2008-12-04 22:40:02 -05001599 "prot_seg_cnt %d\n", __func__,
1600 phba->cfg_prot_sg_seg_cnt,
1601 lpfc_cmd->prot_seg_cnt);
1602 dma_unmap_sg(&phba->pcidev->dev,
1603 scsi_prot_sglist(scsi_cmnd),
1604 scsi_prot_sg_count(scsi_cmnd),
1605 datadir);
1606 scsi_dma_unmap(scsi_cmnd);
1607 return 1;
1608 }
1609
1610 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
1611 datasegcnt, protsegcnt);
1612 /* we shoud have 3 or more entries in buffer list */
1613 if (num_bde < 3)
1614 goto err;
1615 break;
1616 }
1617 case LPFC_PG_TYPE_INVALID:
1618 default:
1619 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1620 "9022 Unexpected protection group %i\n",
1621 prot_group_type);
1622 return 1;
1623 }
1624 }
1625
1626 /*
1627 * Finish initializing those IOCB fields that are dependent on the
1628 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
1629 * reinitialized since all iocb memory resources are used many times
1630 * for transmit, receive, and continuation bpl's.
1631 */
1632 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
1633 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
1634 iocb_cmd->ulpBdeCount = 1;
1635 iocb_cmd->ulpLe = 1;
1636
1637 fcpdl = scsi_bufflen(scsi_cmnd);
1638
1639 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
1640 /*
1641 * We are in DIF Type 1 mode
1642 * Every data block has a 8 byte DIF (trailer)
1643 * attached to it. Must ajust FCP data length
1644 */
1645 blksize = lpfc_cmd_blksize(scsi_cmnd);
1646 diflen = (fcpdl / blksize) * 8;
1647 fcpdl += diflen;
1648 }
1649 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
1650
1651 /*
1652 * Due to difference in data length between DIF/non-DIF paths,
1653 * we need to set word 4 of IOCB here
1654 */
1655 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
1656
1657 return 0;
1658err:
1659 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1660 "9023 Could not setup all needed BDE's"
1661 "prot_group_type=%d, num_bde=%d\n",
1662 prot_group_type, num_bde);
1663 return 1;
1664}
1665
1666/*
1667 * This function checks for BlockGuard errors detected by
1668 * the HBA. In case of errors, the ASC/ASCQ fields in the
1669 * sense buffer will be set accordingly, paired with
1670 * ILLEGAL_REQUEST to signal to the kernel that the HBA
1671 * detected corruption.
1672 *
1673 * Returns:
1674 * 0 - No error found
1675 * 1 - BlockGuard error found
1676 * -1 - Internal error (bad profile, ...etc)
1677 */
1678static int
1679lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
1680 struct lpfc_iocbq *pIocbOut)
1681{
1682 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
1683 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
1684 int ret = 0;
1685 uint32_t bghm = bgf->bghm;
1686 uint32_t bgstat = bgf->bgstat;
1687 uint64_t failing_sector = 0;
1688
James Smart6a9c52c2009-10-02 15:16:51 -04001689 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
1690 " 0x%x lba 0x%llx blk cnt 0x%x "
James Smarte2a0a9d2008-12-04 22:40:02 -05001691 "bgstat=0x%x bghm=0x%x\n",
James Smart87b5c322008-12-16 10:34:09 -05001692 cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
Tejun Heo83096eb2009-05-07 22:24:39 +09001693 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05001694
1695 spin_lock(&_dump_buf_lock);
1696 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04001697 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
1698 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001699 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04001700 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05001701
1702 /* If we have a prot sgl, save the DIF buffer */
1703 if (lpfc_prot_group_type(phba, cmd) ==
1704 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04001705 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
1706 "Saving DIF for %u blocks to debugfs\n",
1707 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
1708 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05001709 }
1710
1711 _dump_buf_done = 1;
1712 }
1713 spin_unlock(&_dump_buf_lock);
1714
1715 if (lpfc_bgs_get_invalid_prof(bgstat)) {
1716 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04001717 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
1718 " BlockGuard profile. bgstat:0x%x\n",
1719 bgstat);
James Smarte2a0a9d2008-12-04 22:40:02 -05001720 ret = (-1);
1721 goto out;
1722 }
1723
1724 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
1725 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04001726 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
1727 "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001728 bgstat);
1729 ret = (-1);
1730 goto out;
1731 }
1732
1733 if (lpfc_bgs_get_guard_err(bgstat)) {
1734 ret = 1;
1735
1736 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1737 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001738 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001739 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1740 phba->bg_guard_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04001741 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1742 "9055 BLKGRD: guard_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001743 }
1744
1745 if (lpfc_bgs_get_reftag_err(bgstat)) {
1746 ret = 1;
1747
1748 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1749 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001750 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001751 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1752
1753 phba->bg_reftag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04001754 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1755 "9056 BLKGRD: ref_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001756 }
1757
1758 if (lpfc_bgs_get_apptag_err(bgstat)) {
1759 ret = 1;
1760
1761 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1762 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001763 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001764 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1765
1766 phba->bg_apptag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04001767 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1768 "9061 BLKGRD: app_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001769 }
1770
1771 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
1772 /*
1773 * setup sense data descriptor 0 per SPC-4 as an information
1774 * field, and put the failing LBA in it
1775 */
1776 cmd->sense_buffer[8] = 0; /* Information */
1777 cmd->sense_buffer[9] = 0xa; /* Add. length */
David Howells2344b5b2009-04-14 17:08:34 +01001778 bghm /= cmd->device->sector_size;
James Smarte2a0a9d2008-12-04 22:40:02 -05001779
1780 failing_sector = scsi_get_lba(cmd);
1781 failing_sector += bghm;
1782
1783 put_unaligned_be64(failing_sector, &cmd->sense_buffer[10]);
1784 }
1785
1786 if (!ret) {
1787 /* No error was reported - problem in FW? */
1788 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04001789 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1790 "9057 BLKGRD: no errors reported!\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001791 }
1792
1793out:
1794 return ret;
1795}
1796
James Smartea2151b2008-09-07 11:52:10 -04001797/**
James Smartda0436e2009-05-22 14:51:39 -04001798 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
1799 * @phba: The Hba for which this call is being executed.
1800 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1801 *
1802 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
1803 * field of @lpfc_cmd for device with SLI-4 interface spec.
1804 *
1805 * Return codes:
1806 * 1 - Error
1807 * 0 - Success
1808 **/
1809static int
1810lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1811{
1812 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1813 struct scatterlist *sgel = NULL;
1814 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1815 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
1816 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1817 dma_addr_t physaddr;
1818 uint32_t num_bde = 0;
1819 uint32_t dma_len;
1820 uint32_t dma_offset = 0;
1821 int nseg;
1822
1823 /*
1824 * There are three possibilities here - use scatter-gather segment, use
1825 * the single mapping, or neither. Start the lpfc command prep by
1826 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1827 * data bde entry.
1828 */
1829 if (scsi_sg_count(scsi_cmnd)) {
1830 /*
1831 * The driver stores the segment count returned from pci_map_sg
1832 * because this a count of dma-mappings used to map the use_sg
1833 * pages. They are not guaranteed to be the same for those
1834 * architectures that implement an IOMMU.
1835 */
1836
1837 nseg = scsi_dma_map(scsi_cmnd);
1838 if (unlikely(!nseg))
1839 return 1;
1840 sgl += 1;
1841 /* clear the last flag in the fcp_rsp map entry */
1842 sgl->word2 = le32_to_cpu(sgl->word2);
1843 bf_set(lpfc_sli4_sge_last, sgl, 0);
1844 sgl->word2 = cpu_to_le32(sgl->word2);
1845 sgl += 1;
1846
1847 lpfc_cmd->seg_cnt = nseg;
1848 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001849 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
1850 " %s: Too many sg segments from "
1851 "dma_map_sg. Config %d, seg_cnt %d\n",
1852 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04001853 lpfc_cmd->seg_cnt);
1854 scsi_dma_unmap(scsi_cmnd);
1855 return 1;
1856 }
1857
1858 /*
1859 * The driver established a maximum scatter-gather segment count
1860 * during probe that limits the number of sg elements in any
1861 * single scsi command. Just run through the seg_cnt and format
1862 * the sge's.
1863 * When using SLI-3 the driver will try to fit all the BDEs into
1864 * the IOCB. If it can't then the BDEs get added to a BPL as it
1865 * does for SLI-2 mode.
1866 */
1867 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
1868 physaddr = sg_dma_address(sgel);
1869 dma_len = sg_dma_len(sgel);
1870 bf_set(lpfc_sli4_sge_len, sgl, sg_dma_len(sgel));
1871 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
1872 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
1873 if ((num_bde + 1) == nseg)
1874 bf_set(lpfc_sli4_sge_last, sgl, 1);
1875 else
1876 bf_set(lpfc_sli4_sge_last, sgl, 0);
1877 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
1878 sgl->word2 = cpu_to_le32(sgl->word2);
1879 sgl->word3 = cpu_to_le32(sgl->word3);
1880 dma_offset += dma_len;
1881 sgl++;
1882 }
1883 } else {
1884 sgl += 1;
1885 /* clear the last flag in the fcp_rsp map entry */
1886 sgl->word2 = le32_to_cpu(sgl->word2);
1887 bf_set(lpfc_sli4_sge_last, sgl, 1);
1888 sgl->word2 = cpu_to_le32(sgl->word2);
1889 }
1890
1891 /*
1892 * Finish initializing those IOCB fields that are dependent on the
1893 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1894 * explicitly reinitialized.
1895 * all iocb memory resources are reused.
1896 */
1897 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
1898
1899 /*
1900 * Due to difference in data length between DIF/non-DIF paths,
1901 * we need to set word 4 of IOCB here
1902 */
1903 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
1904 return 0;
1905}
1906
1907/**
James Smart3772a992009-05-22 14:50:54 -04001908 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
1909 * @phba: The Hba for which this call is being executed.
1910 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1911 *
1912 * This routine wraps the actual DMA mapping function pointer from the
1913 * lpfc_hba struct.
1914 *
1915 * Return codes:
1916 * 1 - Error
1917 * 0 - Success
1918 **/
1919static inline int
1920lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1921{
1922 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
1923}
1924
1925/**
James Smart3621a712009-04-06 18:47:14 -04001926 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04001927 * @phba: Pointer to hba context object.
1928 * @vport: Pointer to vport object.
1929 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
1930 * @rsp_iocb: Pointer to response iocb object which reported error.
1931 *
1932 * This function posts an event when there is a SCSI command reporting
1933 * error from the scsi device.
1934 **/
1935static void
1936lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
1937 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
1938 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
1939 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
1940 uint32_t resp_info = fcprsp->rspStatus2;
1941 uint32_t scsi_status = fcprsp->rspStatus3;
1942 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
1943 struct lpfc_fast_path_event *fast_path_evt = NULL;
1944 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
1945 unsigned long flags;
1946
1947 /* If there is queuefull or busy condition send a scsi event */
1948 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
1949 (cmnd->result == SAM_STAT_BUSY)) {
1950 fast_path_evt = lpfc_alloc_fast_evt(phba);
1951 if (!fast_path_evt)
1952 return;
1953 fast_path_evt->un.scsi_evt.event_type =
1954 FC_REG_SCSI_EVENT;
1955 fast_path_evt->un.scsi_evt.subcategory =
1956 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
1957 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
1958 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
1959 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
1960 &pnode->nlp_portname, sizeof(struct lpfc_name));
1961 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
1962 &pnode->nlp_nodename, sizeof(struct lpfc_name));
1963 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
1964 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
1965 fast_path_evt = lpfc_alloc_fast_evt(phba);
1966 if (!fast_path_evt)
1967 return;
1968 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
1969 FC_REG_SCSI_EVENT;
1970 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
1971 LPFC_EVENT_CHECK_COND;
1972 fast_path_evt->un.check_cond_evt.scsi_event.lun =
1973 cmnd->device->lun;
1974 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
1975 &pnode->nlp_portname, sizeof(struct lpfc_name));
1976 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
1977 &pnode->nlp_nodename, sizeof(struct lpfc_name));
1978 fast_path_evt->un.check_cond_evt.sense_key =
1979 cmnd->sense_buffer[2] & 0xf;
1980 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
1981 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
1982 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
1983 fcpi_parm &&
1984 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
1985 ((scsi_status == SAM_STAT_GOOD) &&
1986 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
1987 /*
1988 * If status is good or resid does not match with fcp_param and
1989 * there is valid fcpi_parm, then there is a read_check error
1990 */
1991 fast_path_evt = lpfc_alloc_fast_evt(phba);
1992 if (!fast_path_evt)
1993 return;
1994 fast_path_evt->un.read_check_error.header.event_type =
1995 FC_REG_FABRIC_EVENT;
1996 fast_path_evt->un.read_check_error.header.subcategory =
1997 LPFC_EVENT_FCPRDCHKERR;
1998 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
1999 &pnode->nlp_portname, sizeof(struct lpfc_name));
2000 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
2001 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2002 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
2003 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
2004 fast_path_evt->un.read_check_error.fcpiparam =
2005 fcpi_parm;
2006 } else
2007 return;
2008
2009 fast_path_evt->vport = vport;
2010 spin_lock_irqsave(&phba->hbalock, flags);
2011 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
2012 spin_unlock_irqrestore(&phba->hbalock, flags);
2013 lpfc_worker_wake_up(phba);
2014 return;
2015}
James Smart9bad7672008-12-04 22:39:02 -05002016
2017/**
James Smartf1126682009-06-10 17:22:44 -04002018 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04002019 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05002020 * @psb: The scsi buffer which is going to be un-mapped.
2021 *
2022 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04002023 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002024 **/
dea31012005-04-17 16:05:31 -05002025static void
James Smartf1126682009-06-10 17:22:44 -04002026lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04002027{
2028 /*
2029 * There are only two special cases to consider. (1) the scsi command
2030 * requested scatter-gather usage or (2) the scsi command allocated
2031 * a request buffer, but did not request use_sg. There is a third
2032 * case, but it does not require resource deallocation.
2033 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002034 if (psb->seg_cnt > 0)
2035 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002036 if (psb->prot_seg_cnt > 0)
2037 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
2038 scsi_prot_sg_count(psb->pCmd),
2039 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04002040}
2041
James Smart9bad7672008-12-04 22:39:02 -05002042/**
James Smart3621a712009-04-06 18:47:14 -04002043 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05002044 * @vport: The virtual port for which this call is being executed.
2045 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2046 * @rsp_iocb: The response IOCB which contains FCP error.
2047 *
2048 * This routine is called to process response IOCB with status field
2049 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
2050 * based upon SCSI and FCP error.
2051 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04002052static void
James Smart2e0fef82007-06-17 19:56:36 -05002053lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2054 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05002055{
2056 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2057 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
2058 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04002059 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05002060 uint32_t resp_info = fcprsp->rspStatus2;
2061 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05002062 uint32_t *lp;
dea31012005-04-17 16:05:31 -05002063 uint32_t host_status = DID_OK;
2064 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05002065 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05002066
James Smartea2151b2008-09-07 11:52:10 -04002067
dea31012005-04-17 16:05:31 -05002068 /*
2069 * If this is a task management command, there is no
2070 * scsi packet associated with this lpfc_cmd. The driver
2071 * consumes it.
2072 */
2073 if (fcpcmd->fcpCntl2) {
2074 scsi_status = 0;
2075 goto out;
2076 }
2077
James Smart6a9c52c2009-10-02 15:16:51 -04002078 if (resp_info & RSP_LEN_VALID) {
2079 rsplen = be32_to_cpu(fcprsp->rspRspLen);
2080 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
2081 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
2082 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2083 "2719 Invalid response length: "
2084 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
2085 cmnd->device->id,
2086 cmnd->device->lun, cmnd->cmnd[0],
2087 rsplen);
2088 host_status = DID_ERROR;
2089 goto out;
2090 }
2091 }
2092
James Smartc7743952006-12-02 13:34:42 -05002093 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
2094 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
2095 if (snslen > SCSI_SENSE_BUFFERSIZE)
2096 snslen = SCSI_SENSE_BUFFERSIZE;
2097
2098 if (resp_info & RSP_LEN_VALID)
2099 rsplen = be32_to_cpu(fcprsp->rspRspLen);
2100 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
2101 }
2102 lp = (uint32_t *)cmnd->sense_buffer;
2103
2104 if (!scsi_status && (resp_info & RESID_UNDER))
2105 logit = LOG_FCP;
2106
James Smarte8b62012007-08-02 11:10:09 -04002107 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05002108 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04002109 "Data: x%x x%x x%x x%x x%x\n",
2110 cmnd->cmnd[0], scsi_status,
2111 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
2112 be32_to_cpu(fcprsp->rspResId),
2113 be32_to_cpu(fcprsp->rspSnsLen),
2114 be32_to_cpu(fcprsp->rspRspLen),
2115 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05002116
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002117 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05002118 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002119 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05002120
James Smarte8b62012007-08-02 11:10:09 -04002121 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002122 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04002123 "residual %d Data: x%x x%x x%x\n",
2124 be32_to_cpu(fcpcmd->fcpDl),
2125 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
2126 cmnd->underflow);
dea31012005-04-17 16:05:31 -05002127
2128 /*
James Smart7054a602007-04-25 09:52:34 -04002129 * If there is an under run check if under run reported by
2130 * storage array is same as the under run reported by HBA.
2131 * If this is not same, there is a dropped frame.
2132 */
2133 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2134 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002135 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04002136 lpfc_printf_vlog(vport, KERN_WARNING,
2137 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05002138 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04002139 "and Underrun Data: x%x x%x x%x x%x\n",
2140 be32_to_cpu(fcpcmd->fcpDl),
2141 scsi_get_resid(cmnd), fcpi_parm,
2142 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002143 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04002144 host_status = DID_ERROR;
2145 }
2146 /*
dea31012005-04-17 16:05:31 -05002147 * The cmnd->underflow is the minimum number of bytes that must
2148 * be transfered for this command. Provided a sense condition
2149 * is not present, make sure the actual amount transferred is at
2150 * least the underflow value or fail.
2151 */
2152 if (!(resp_info & SNS_LEN_VALID) &&
2153 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002154 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
2155 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04002156 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002157 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04002158 "underrun converted to error "
2159 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04002160 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04002161 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05002162 host_status = DID_ERROR;
2163 }
2164 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04002165 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002166 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04002167 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04002168 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05002169 host_status = DID_ERROR;
2170
2171 /*
2172 * Check SLI validation that all the transfer was actually done
2173 * (fcpi_parm should be zero). Apply check only to reads.
2174 */
2175 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
2176 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -04002177 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05002178 "9029 FCP Read Check Error Data: "
James Smarte8b62012007-08-02 11:10:09 -04002179 "x%x x%x x%x x%x\n",
2180 be32_to_cpu(fcpcmd->fcpDl),
2181 be32_to_cpu(fcprsp->rspResId),
2182 fcpi_parm, cmnd->cmnd[0]);
dea31012005-04-17 16:05:31 -05002183 host_status = DID_ERROR;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002184 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05002185 }
2186
2187 out:
2188 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04002189 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05002190}
2191
James Smart9bad7672008-12-04 22:39:02 -05002192/**
James Smart3621a712009-04-06 18:47:14 -04002193 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05002194 * @phba: The Hba for which this call is being executed.
2195 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04002196 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05002197 *
2198 * This routine assigns scsi command result by looking into response IOCB
2199 * status field appropriately. This routine handles QUEUE FULL condition as
2200 * well by ramping down device queue depth.
2201 **/
dea31012005-04-17 16:05:31 -05002202static void
2203lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
2204 struct lpfc_iocbq *pIocbOut)
2205{
2206 struct lpfc_scsi_buf *lpfc_cmd =
2207 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002208 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05002209 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
2210 struct lpfc_nodelist *pnode = rdata->pnode;
2211 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002212 int result;
James Smarta257bf92009-04-06 18:48:10 -04002213 struct scsi_device *tmp_sdev;
James Smart5ffc2662009-11-18 15:39:44 -05002214 int depth;
James Smartfa61a542008-01-11 01:52:42 -05002215 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04002216 struct lpfc_fast_path_event *fast_path_evt;
James Smarta257bf92009-04-06 18:48:10 -04002217 struct Scsi_Host *shost = cmd->device->host;
2218 uint32_t queue_depth, scsi_id;
dea31012005-04-17 16:05:31 -05002219
2220 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
2221 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart109f6ed2008-12-04 22:39:08 -05002222 if (pnode && NLP_CHK_NODE_ACT(pnode))
2223 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05002224
2225 if (lpfc_cmd->status) {
2226 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
2227 (lpfc_cmd->result & IOERR_DRVR_MASK))
2228 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
2229 else if (lpfc_cmd->status >= IOSTAT_CNT)
2230 lpfc_cmd->status = IOSTAT_DEFAULT;
2231
James Smarte8b62012007-08-02 11:10:09 -04002232 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002233 "9030 FCP cmd x%x failed <%d/%d> "
James Smarte8b62012007-08-02 11:10:09 -04002234 "status: x%x result: x%x Data: x%x x%x\n",
2235 cmd->cmnd[0],
2236 cmd->device ? cmd->device->id : 0xffff,
2237 cmd->device ? cmd->device->lun : 0xffff,
2238 lpfc_cmd->status, lpfc_cmd->result,
2239 pIocbOut->iocb.ulpContext,
2240 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05002241
2242 switch (lpfc_cmd->status) {
2243 case IOSTAT_FCP_RSP_ERROR:
2244 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05002245 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05002246 break;
2247 case IOSTAT_NPORT_BSY:
2248 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04002249 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04002250 fast_path_evt = lpfc_alloc_fast_evt(phba);
2251 if (!fast_path_evt)
2252 break;
2253 fast_path_evt->un.fabric_evt.event_type =
2254 FC_REG_FABRIC_EVENT;
2255 fast_path_evt->un.fabric_evt.subcategory =
2256 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
2257 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
2258 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2259 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
2260 &pnode->nlp_portname,
2261 sizeof(struct lpfc_name));
2262 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
2263 &pnode->nlp_nodename,
2264 sizeof(struct lpfc_name));
2265 }
2266 fast_path_evt->vport = vport;
2267 fast_path_evt->work_evt.evt =
2268 LPFC_EVT_FASTPATH_MGMT_EVT;
2269 spin_lock_irqsave(&phba->hbalock, flags);
2270 list_add_tail(&fast_path_evt->work_evt.evt_listp,
2271 &phba->work_list);
2272 spin_unlock_irqrestore(&phba->hbalock, flags);
2273 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05002274 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002275 case IOSTAT_LOCAL_REJECT:
James Smartd7c255b2008-08-24 21:50:00 -04002276 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05002277 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartd7c255b2008-08-24 21:50:00 -04002278 lpfc_cmd->result == IOERR_ABORT_REQUESTED) {
James Smart92d7f7b2007-06-17 19:56:38 -05002279 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04002280 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05002281 }
2282
2283 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
2284 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
2285 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
2286 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
2287 /*
2288 * This is a response for a BG enabled
2289 * cmd. Parse BG error
2290 */
2291 lpfc_parse_bg_err(phba, lpfc_cmd,
2292 pIocbOut);
2293 break;
2294 } else {
2295 lpfc_printf_vlog(vport, KERN_WARNING,
2296 LOG_BG,
2297 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04002298 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002299 }
2300 }
2301
2302 /* else: fall through */
dea31012005-04-17 16:05:31 -05002303 default:
2304 cmd->result = ScsiResult(DID_ERROR, 0);
2305 break;
2306 }
2307
James Smart58da1ff2008-04-07 10:15:56 -04002308 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002309 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04002310 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
2311 SAM_STAT_BUSY);
dea31012005-04-17 16:05:31 -05002312 } else {
2313 cmd->result = ScsiResult(DID_OK, 0);
2314 }
2315
2316 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
2317 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
2318
James Smarte8b62012007-08-02 11:10:09 -04002319 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2320 "0710 Iodone <%d/%d> cmd %p, error "
2321 "x%x SNS x%x x%x Data: x%x x%x\n",
2322 cmd->device->id, cmd->device->lun, cmd,
2323 cmd->result, *lp, *(lp + 3), cmd->retries,
2324 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05002325 }
2326
James Smartea2151b2008-09-07 11:52:10 -04002327 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002328 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04002329 if (vport->cfg_max_scsicmpl_time &&
2330 time_after(jiffies, lpfc_cmd->start_time +
2331 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04002332 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002333 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2334 if (pnode->cmd_qdepth >
2335 atomic_read(&pnode->cmd_pending) &&
2336 (atomic_read(&pnode->cmd_pending) >
2337 LPFC_MIN_TGT_QDEPTH) &&
2338 ((cmd->cmnd[0] == READ_10) ||
2339 (cmd->cmnd[0] == WRITE_10)))
2340 pnode->cmd_qdepth =
2341 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04002342
James Smart109f6ed2008-12-04 22:39:08 -05002343 pnode->last_change_time = jiffies;
2344 }
James Smarta257bf92009-04-06 18:48:10 -04002345 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002346 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2347 if ((pnode->cmd_qdepth < LPFC_MAX_TGT_QDEPTH) &&
James Smart977b5a02008-09-07 11:52:04 -04002348 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05002349 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04002350 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002351 pnode->cmd_qdepth += pnode->cmd_qdepth *
2352 LPFC_TGTQ_RAMPUP_PCENT / 100;
2353 if (pnode->cmd_qdepth > LPFC_MAX_TGT_QDEPTH)
2354 pnode->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
2355 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04002356 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002357 }
James Smart977b5a02008-09-07 11:52:04 -04002358 }
2359
James Smart1dcb58e2007-04-25 09:51:30 -04002360 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04002361
2362 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
2363 queue_depth = cmd->device->queue_depth;
2364 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05002365 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002366
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002367 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smartfa61a542008-01-11 01:52:42 -05002368 /*
2369 * If there is a thread waiting for command completion
2370 * wake up the thread.
2371 */
James Smarta257bf92009-04-06 18:48:10 -04002372 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04002373 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05002374 if (lpfc_cmd->waitq)
2375 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04002376 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002377 lpfc_release_scsi_buf(phba, lpfc_cmd);
2378 return;
2379 }
2380
James Smart92d7f7b2007-06-17 19:56:38 -05002381 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04002382 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05002383
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002384 /*
2385 * Check for queue full. If the lun is reporting queue full, then
2386 * back off the lun queue depth to prevent target overloads.
2387 */
James Smart58da1ff2008-04-07 10:15:56 -04002388 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
2389 NLP_CHK_NODE_ACT(pnode)) {
James Smarta257bf92009-04-06 18:48:10 -04002390 shost_for_each_device(tmp_sdev, shost) {
2391 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002392 continue;
2393 depth = scsi_track_queue_full(tmp_sdev,
James Smart5ffc2662009-11-18 15:39:44 -05002394 tmp_sdev->queue_depth-1);
2395 if (depth <= 0)
2396 continue;
James Smarte8b62012007-08-02 11:10:09 -04002397 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2398 "0711 detected queue full - lun queue "
2399 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04002400 lpfc_send_sdev_queuedepth_change_event(phba, vport,
James Smart5ffc2662009-11-18 15:39:44 -05002401 pnode,
2402 tmp_sdev->lun,
2403 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002404 }
2405 }
2406
James Smartfa61a542008-01-11 01:52:42 -05002407 /*
2408 * If there is a thread waiting for command completion
2409 * wake up the thread.
2410 */
James Smarta257bf92009-04-06 18:48:10 -04002411 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04002412 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05002413 if (lpfc_cmd->waitq)
2414 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04002415 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05002416
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002417 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05002418}
2419
James Smart34b02dc2008-08-24 21:49:55 -04002420/**
James Smart3621a712009-04-06 18:47:14 -04002421 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04002422 * @data: A pointer to the immediate command data portion of the IOCB.
2423 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
2424 *
2425 * The routine copies the entire FCP command from @fcp_cmnd to @data while
2426 * byte swapping the data to big endian format for transmission on the wire.
2427 **/
2428static void
2429lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
2430{
2431 int i, j;
2432 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
2433 i += sizeof(uint32_t), j++) {
2434 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
2435 }
2436}
2437
James Smart9bad7672008-12-04 22:39:02 -05002438/**
James Smartf1126682009-06-10 17:22:44 -04002439 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05002440 * @vport: The virtual port for which this call is being executed.
2441 * @lpfc_cmd: The scsi command which needs to send.
2442 * @pnode: Pointer to lpfc_nodelist.
2443 *
2444 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04002445 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002446 **/
dea31012005-04-17 16:05:31 -05002447static void
James Smartf1126682009-06-10 17:22:44 -04002448lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05002449 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05002450{
James Smart2e0fef82007-06-17 19:56:36 -05002451 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002452 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2453 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2454 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2455 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
2456 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04002457 char tag[2];
dea31012005-04-17 16:05:31 -05002458
James Smart58da1ff2008-04-07 10:15:56 -04002459 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2460 return;
2461
dea31012005-04-17 16:05:31 -05002462 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04002463 /* clear task management bits */
2464 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05002465
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04002466 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
2467 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05002468
2469 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
2470
James Smart7e2b19f2007-10-29 11:00:39 -04002471 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
2472 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05002473 case HEAD_OF_QUEUE_TAG:
2474 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
2475 break;
2476 case ORDERED_QUEUE_TAG:
2477 fcp_cmnd->fcpCntl1 = ORDERED_Q;
2478 break;
2479 default:
2480 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
2481 break;
2482 }
2483 } else
2484 fcp_cmnd->fcpCntl1 = 0;
2485
2486 /*
2487 * There are three possibilities here - use scatter-gather segment, use
2488 * the single mapping, or neither. Start the lpfc command prep by
2489 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2490 * data bde entry.
2491 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002492 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05002493 if (datadir == DMA_TO_DEVICE) {
2494 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart3772a992009-05-22 14:50:54 -04002495 if (phba->sli_rev < LPFC_SLI_REV4) {
2496 iocb_cmd->un.fcpi.fcpi_parm = 0;
2497 iocb_cmd->ulpPU = 0;
2498 } else
2499 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05002500 fcp_cmnd->fcpCntl3 = WRITE_DATA;
2501 phba->fc4OutputRequests++;
2502 } else {
2503 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
2504 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05002505 fcp_cmnd->fcpCntl3 = READ_DATA;
2506 phba->fc4InputRequests++;
2507 }
2508 } else {
2509 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
2510 iocb_cmd->un.fcpi.fcpi_parm = 0;
2511 iocb_cmd->ulpPU = 0;
2512 fcp_cmnd->fcpCntl3 = 0;
2513 phba->fc4ControlRequests++;
2514 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002515 if (phba->sli_rev == 3 &&
2516 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04002517 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05002518 /*
2519 * Finish initializing those IOCB fields that are independent
2520 * of the scsi_cmnd request_buffer
2521 */
2522 piocbq->iocb.ulpContext = pnode->nlp_rpi;
2523 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
2524 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05002525 else
2526 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05002527
2528 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
2529 piocbq->context1 = lpfc_cmd;
2530 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
2531 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05002532 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05002533}
2534
James Smart9bad7672008-12-04 22:39:02 -05002535/**
James Smartf1126682009-06-10 17:22:44 -04002536 * lpfc_scsi_prep_task_mgmt_cmnd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05002537 * @vport: The virtual port for which this call is being executed.
2538 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2539 * @lun: Logical unit number.
2540 * @task_mgmt_cmd: SCSI task management command.
2541 *
James Smart3772a992009-05-22 14:50:54 -04002542 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
2543 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002544 *
2545 * Return codes:
2546 * 0 - Error
2547 * 1 - Success
2548 **/
dea31012005-04-17 16:05:31 -05002549static int
James Smartf1126682009-06-10 17:22:44 -04002550lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05002551 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04002552 unsigned int lun,
dea31012005-04-17 16:05:31 -05002553 uint8_t task_mgmt_cmd)
2554{
dea31012005-04-17 16:05:31 -05002555 struct lpfc_iocbq *piocbq;
2556 IOCB_t *piocb;
2557 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04002558 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05002559 struct lpfc_nodelist *ndlp = rdata->pnode;
2560
James Smart58da1ff2008-04-07 10:15:56 -04002561 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2562 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05002563 return 0;
dea31012005-04-17 16:05:31 -05002564
dea31012005-04-17 16:05:31 -05002565 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002566 piocbq->vport = vport;
2567
dea31012005-04-17 16:05:31 -05002568 piocb = &piocbq->iocb;
2569
2570 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04002571 /* Clear out any old data in the FCP command area */
2572 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
2573 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05002574 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05002575 if (vport->phba->sli_rev == 3 &&
2576 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04002577 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05002578 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05002579 piocb->ulpContext = ndlp->nlp_rpi;
2580 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
2581 piocb->ulpFCP2Rcvy = 1;
2582 }
2583 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
2584
2585 /* ulpTimeout is only one byte */
2586 if (lpfc_cmd->timeout > 0xff) {
2587 /*
2588 * Do not timeout the command at the firmware level.
2589 * The driver will provide the timeout mechanism.
2590 */
2591 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04002592 } else
dea31012005-04-17 16:05:31 -05002593 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04002594
2595 if (vport->phba->sli_rev == LPFC_SLI_REV4)
2596 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05002597
James Smart2e0fef82007-06-17 19:56:36 -05002598 return 1;
dea31012005-04-17 16:05:31 -05002599}
2600
James Smart9bad7672008-12-04 22:39:02 -05002601/**
James Smart3772a992009-05-22 14:50:54 -04002602 * lpfc_scsi_api_table_setup - Set up scsi api fucntion jump table
2603 * @phba: The hba struct for which this call is being executed.
2604 * @dev_grp: The HBA PCI-Device group number.
2605 *
2606 * This routine sets up the SCSI interface API function jump table in @phba
2607 * struct.
2608 * Returns: 0 - success, -ENODEV - failure.
2609 **/
2610int
2611lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
2612{
2613
James Smartf1126682009-06-10 17:22:44 -04002614 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
2615 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
2616 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf;
2617
James Smart3772a992009-05-22 14:50:54 -04002618 switch (dev_grp) {
2619 case LPFC_PCI_DEV_LP:
2620 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
2621 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04002622 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
2623 break;
James Smartda0436e2009-05-22 14:51:39 -04002624 case LPFC_PCI_DEV_OC:
2625 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
2626 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04002627 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
2628 break;
James Smart3772a992009-05-22 14:50:54 -04002629 default:
2630 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2631 "1418 Invalid HBA PCI-device group: 0x%x\n",
2632 dev_grp);
2633 return -ENODEV;
2634 break;
2635 }
2636 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf;
2637 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
2638 return 0;
2639}
2640
2641/**
James Smart3621a712009-04-06 18:47:14 -04002642 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05002643 * @phba: The Hba for which this call is being executed.
2644 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
2645 * @rspiocbq: Pointer to lpfc_iocbq data structure.
2646 *
2647 * This routine is IOCB completion routine for device reset and target reset
2648 * routine. This routine release scsi buffer associated with lpfc_cmd.
2649 **/
James Smart7054a602007-04-25 09:52:34 -04002650static void
2651lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
2652 struct lpfc_iocbq *cmdiocbq,
2653 struct lpfc_iocbq *rspiocbq)
2654{
2655 struct lpfc_scsi_buf *lpfc_cmd =
2656 (struct lpfc_scsi_buf *) cmdiocbq->context1;
2657 if (lpfc_cmd)
2658 lpfc_release_scsi_buf(phba, lpfc_cmd);
2659 return;
2660}
2661
James Smart9bad7672008-12-04 22:39:02 -05002662/**
James Smart3621a712009-04-06 18:47:14 -04002663 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05002664 * @host: The scsi host for which this call is being executed.
2665 *
2666 * This routine provides module information about hba.
2667 *
2668 * Reutrn code:
2669 * Pointer to char - Success.
2670 **/
dea31012005-04-17 16:05:31 -05002671const char *
2672lpfc_info(struct Scsi_Host *host)
2673{
James Smart2e0fef82007-06-17 19:56:36 -05002674 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
2675 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002676 int len;
2677 static char lpfcinfobuf[384];
2678
2679 memset(lpfcinfobuf,0,384);
2680 if (phba && phba->pcidev){
2681 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
2682 len = strlen(lpfcinfobuf);
2683 snprintf(lpfcinfobuf + len,
2684 384-len,
2685 " on PCI bus %02x device %02x irq %d",
2686 phba->pcidev->bus->number,
2687 phba->pcidev->devfn,
2688 phba->pcidev->irq);
2689 len = strlen(lpfcinfobuf);
2690 if (phba->Port[0]) {
2691 snprintf(lpfcinfobuf + len,
2692 384-len,
2693 " port %s",
2694 phba->Port);
2695 }
2696 }
2697 return lpfcinfobuf;
2698}
2699
James Smart9bad7672008-12-04 22:39:02 -05002700/**
James Smart3621a712009-04-06 18:47:14 -04002701 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05002702 * @phba: The Hba for which this call is being executed.
2703 *
2704 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
2705 * The default value of cfg_poll_tmo is 10 milliseconds.
2706 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002707static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
2708{
2709 unsigned long poll_tmo_expires =
2710 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
2711
2712 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
2713 mod_timer(&phba->fcp_poll_timer,
2714 poll_tmo_expires);
2715}
2716
James Smart9bad7672008-12-04 22:39:02 -05002717/**
James Smart3621a712009-04-06 18:47:14 -04002718 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05002719 * @phba: The Hba for which this call is being executed.
2720 *
2721 * This routine starts the fcp_poll_timer of @phba.
2722 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002723void lpfc_poll_start_timer(struct lpfc_hba * phba)
2724{
2725 lpfc_poll_rearm_timer(phba);
2726}
2727
James Smart9bad7672008-12-04 22:39:02 -05002728/**
James Smart3621a712009-04-06 18:47:14 -04002729 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05002730 * @ptr: Map to lpfc_hba data structure pointer.
2731 *
2732 * This routine restarts fcp_poll timer, when FCP ring polling is enable
2733 * and FCP Ring interrupt is disable.
2734 **/
2735
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002736void lpfc_poll_timeout(unsigned long ptr)
2737{
James Smart2e0fef82007-06-17 19:56:36 -05002738 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002739
2740 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04002741 lpfc_sli_handle_fast_ring_event(phba,
2742 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
2743
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002744 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
2745 lpfc_poll_rearm_timer(phba);
2746 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002747}
2748
James Smart9bad7672008-12-04 22:39:02 -05002749/**
James Smart3621a712009-04-06 18:47:14 -04002750 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05002751 * @cmnd: Pointer to scsi_cmnd data structure.
2752 * @done: Pointer to done routine.
2753 *
2754 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
2755 * This routine prepares an IOCB from scsi command and provides to firmware.
2756 * The @done callback is invoked after driver finished processing the command.
2757 *
2758 * Return value :
2759 * 0 - Success
2760 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
2761 **/
dea31012005-04-17 16:05:31 -05002762static int
2763lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
2764{
James Smart2e0fef82007-06-17 19:56:36 -05002765 struct Scsi_Host *shost = cmnd->device->host;
2766 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2767 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002768 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
2769 struct lpfc_nodelist *ndlp = rdata->pnode;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002770 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002771 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002772 int err;
dea31012005-04-17 16:05:31 -05002773
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002774 err = fc_remote_port_chkready(rport);
2775 if (err) {
2776 cmnd->result = err;
dea31012005-04-17 16:05:31 -05002777 goto out_fail_command;
2778 }
2779
James Smarte2a0a9d2008-12-04 22:40:02 -05002780 if (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
2781 scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
2782
James Smart6a9c52c2009-10-02 15:16:51 -04002783 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2784 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
2785 " op:%02x str=%s without registering for"
2786 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002787 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2788 dif_op_str[scsi_get_prot_op(cmnd)]);
2789 goto out_fail_command;
2790 }
2791
dea31012005-04-17 16:05:31 -05002792 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002793 * Catch race where our node has transitioned, but the
2794 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05002795 */
James Smartb522d7d2008-09-07 11:51:56 -04002796 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
2797 cmnd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
2798 goto out_fail_command;
2799 }
James Smart109f6ed2008-12-04 22:39:08 -05002800 if (vport->cfg_max_scsicmpl_time &&
2801 (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth))
James Smart977b5a02008-09-07 11:52:04 -04002802 goto out_host_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05002803
James Smarted957682007-06-17 19:56:37 -05002804 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05002805 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05002806 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002807
James Smarte8b62012007-08-02 11:10:09 -04002808 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2809 "0707 driver's buffer pool is empty, "
2810 "IO busied\n");
dea31012005-04-17 16:05:31 -05002811 goto out_host_busy;
2812 }
2813
2814 /*
2815 * Store the midlayer's command structure for the completion phase
2816 * and complete the command initialization.
2817 */
2818 lpfc_cmd->pCmd = cmnd;
2819 lpfc_cmd->rdata = rdata;
2820 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04002821 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05002822 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
2823 cmnd->scsi_done = done;
2824
James Smarte2a0a9d2008-12-04 22:40:02 -05002825 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04002826 if (vport->phba->cfg_enable_bg) {
2827 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002828 "9033 BLKGRD: rcvd protected cmd:%02x op:%02x "
2829 "str=%s\n",
2830 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2831 dif_op_str[scsi_get_prot_op(cmnd)]);
James Smart6a9c52c2009-10-02 15:16:51 -04002832 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002833 "9034 BLKGRD: CDB: %02x %02x %02x %02x %02x "
James Smarte4e74272009-07-19 10:01:38 -04002834 "%02x %02x %02x %02x %02x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002835 cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
2836 cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
2837 cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
2838 cmnd->cmnd[9]);
James Smart6a9c52c2009-10-02 15:16:51 -04002839 if (cmnd->cmnd[0] == READ_10)
2840 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002841 "9035 BLKGRD: READ @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09002842 "count %u\n",
2843 (unsigned long long)scsi_get_lba(cmnd),
2844 blk_rq_sectors(cmnd->request));
James Smart6a9c52c2009-10-02 15:16:51 -04002845 else if (cmnd->cmnd[0] == WRITE_10)
2846 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002847 "9036 BLKGRD: WRITE @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09002848 "count %u cmd=%p\n",
James Smart87b5c322008-12-16 10:34:09 -05002849 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09002850 blk_rq_sectors(cmnd->request),
James Smarte2a0a9d2008-12-04 22:40:02 -05002851 cmnd);
James Smart6a9c52c2009-10-02 15:16:51 -04002852 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002853
2854 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
2855 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04002856 if (vport->phba->cfg_enable_bg) {
James Smarte2a0a9d2008-12-04 22:40:02 -05002857 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smart6a9c52c2009-10-02 15:16:51 -04002858 "9038 BLKGRD: rcvd unprotected cmd:"
2859 "%02x op:%02x str=%s\n",
2860 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2861 dif_op_str[scsi_get_prot_op(cmnd)]);
2862 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2863 "9039 BLKGRD: CDB: %02x %02x %02x "
2864 "%02x %02x %02x %02x %02x %02x %02x\n",
2865 cmnd->cmnd[0], cmnd->cmnd[1],
2866 cmnd->cmnd[2], cmnd->cmnd[3],
2867 cmnd->cmnd[4], cmnd->cmnd[5],
2868 cmnd->cmnd[6], cmnd->cmnd[7],
2869 cmnd->cmnd[8], 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, "
2873 "count %u\n",
2874 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09002875 blk_rq_sectors(cmnd->request));
James Smart6a9c52c2009-10-02 15:16:51 -04002876 else if (cmnd->cmnd[0] == WRITE_10)
2877 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002878 "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 Smart6a9c52c2009-10-02 15:16:51 -04002882 else
2883 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002884 "9042 dbg: parser not implemented\n");
James Smart6a9c52c2009-10-02 15:16:51 -04002885 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002886 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
2887 }
2888
dea31012005-04-17 16:05:31 -05002889 if (err)
2890 goto out_host_busy_free_buf;
2891
James Smart2e0fef82007-06-17 19:56:36 -05002892 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05002893
James Smart977b5a02008-09-07 11:52:04 -04002894 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04002895 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05002896 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05002897 if (err) {
2898 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05002899 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05002900 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002901 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04002902 spin_unlock(shost->host_lock);
2903 lpfc_sli_handle_fast_ring_event(phba,
2904 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
2905
2906 spin_lock(shost->host_lock);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002907 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
2908 lpfc_poll_rearm_timer(phba);
2909 }
2910
dea31012005-04-17 16:05:31 -05002911 return 0;
2912
2913 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04002914 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002915 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05002916 out_host_busy:
2917 return SCSI_MLQUEUE_HOST_BUSY;
2918
2919 out_fail_command:
2920 done(cmnd);
2921 return 0;
2922}
2923
James Smart9bad7672008-12-04 22:39:02 -05002924/**
James Smart3621a712009-04-06 18:47:14 -04002925 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05002926 * @cmnd: Pointer to scsi_cmnd data structure.
2927 *
2928 * This routine aborts @cmnd pending in base driver.
2929 *
2930 * Return code :
2931 * 0x2003 - Error
2932 * 0x2002 - Success
2933 **/
dea31012005-04-17 16:05:31 -05002934static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05002935lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05002936{
James Smart2e0fef82007-06-17 19:56:36 -05002937 struct Scsi_Host *shost = cmnd->device->host;
2938 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2939 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002940 struct lpfc_iocbq *iocb;
2941 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05002942 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05002943 IOCB_t *cmd, *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002944 int ret = SUCCESS;
James Smartfa61a542008-01-11 01:52:42 -05002945 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002946
Christof Schmitt65d430f2009-10-30 17:59:29 +01002947 fc_block_scsi_eh(cmnd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002948 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
2949 BUG_ON(!lpfc_cmd);
dea31012005-04-17 16:05:31 -05002950
2951 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002952 * If pCmd field of the corresponding lpfc_scsi_buf structure
2953 * points to a different SCSI command, then the driver has
2954 * already completed this command, but the midlayer did not
2955 * see the completion before the eh fired. Just return
2956 * SUCCESS.
dea31012005-04-17 16:05:31 -05002957 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002958 iocb = &lpfc_cmd->cur_iocbq;
2959 if (lpfc_cmd->pCmd != cmnd)
2960 goto out;
dea31012005-04-17 16:05:31 -05002961
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002962 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05002963
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002964 abtsiocb = lpfc_sli_get_iocbq(phba);
2965 if (abtsiocb == NULL) {
2966 ret = FAILED;
dea31012005-04-17 16:05:31 -05002967 goto out;
2968 }
2969
dea31012005-04-17 16:05:31 -05002970 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002971 * The scsi command can not be in txq and it is in flight because the
2972 * pCmd is still pointig at the SCSI command we have to abort. There
2973 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05002974 */
dea31012005-04-17 16:05:31 -05002975
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002976 cmd = &iocb->iocb;
2977 icmd = &abtsiocb->iocb;
2978 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
2979 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04002980 if (phba->sli_rev == LPFC_SLI_REV4)
2981 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
2982 else
2983 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05002984
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002985 icmd->ulpLe = 1;
2986 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05002987
2988 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
2989 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
2990
James Smart2e0fef82007-06-17 19:56:36 -05002991 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002992 icmd->ulpCommand = CMD_ABORT_XRI_CN;
2993 else
2994 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05002995
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002996 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05002997 abtsiocb->vport = vport;
James Smart3772a992009-05-22 14:50:54 -04002998 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
2999 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003000 lpfc_sli_release_iocbq(phba, abtsiocb);
3001 ret = FAILED;
3002 goto out;
3003 }
3004
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003005 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04003006 lpfc_sli_handle_fast_ring_event(phba,
3007 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003008
James Smartfa61a542008-01-11 01:52:42 -05003009 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003010 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05003011 wait_event_timeout(waitq,
3012 (lpfc_cmd->pCmd != cmnd),
3013 (2*vport->cfg_devloss_tmo*HZ));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003014
James Smartfa61a542008-01-11 01:52:42 -05003015 spin_lock_irq(shost->host_lock);
3016 lpfc_cmd->waitq = NULL;
3017 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003018
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003019 if (lpfc_cmd->pCmd == cmnd) {
3020 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04003021 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3022 "0748 abort handler timed out waiting "
3023 "for abort to complete: ret %#x, ID %d, "
3024 "LUN %d, snum %#lx\n",
3025 ret, cmnd->device->id, cmnd->device->lun,
3026 cmnd->serial_number);
dea31012005-04-17 16:05:31 -05003027 }
3028
3029 out:
James Smarte8b62012007-08-02 11:10:09 -04003030 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3031 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
3032 "LUN %d snum %#lx\n", ret, cmnd->device->id,
3033 cmnd->device->lun, cmnd->serial_number);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003034 return ret;
dea31012005-04-17 16:05:31 -05003035}
3036
James Smartbbb9d182009-06-10 17:23:16 -04003037static char *
3038lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
3039{
3040 switch (task_mgmt_cmd) {
3041 case FCP_ABORT_TASK_SET:
3042 return "ABORT_TASK_SET";
3043 case FCP_CLEAR_TASK_SET:
3044 return "FCP_CLEAR_TASK_SET";
3045 case FCP_BUS_RESET:
3046 return "FCP_BUS_RESET";
3047 case FCP_LUN_RESET:
3048 return "FCP_LUN_RESET";
3049 case FCP_TARGET_RESET:
3050 return "FCP_TARGET_RESET";
3051 case FCP_CLEAR_ACA:
3052 return "FCP_CLEAR_ACA";
3053 case FCP_TERMINATE_TASK:
3054 return "FCP_TERMINATE_TASK";
3055 default:
3056 return "unknown";
3057 }
3058}
3059
3060/**
3061 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
3062 * @vport: The virtual port for which this call is being executed.
3063 * @rdata: Pointer to remote port local data
3064 * @tgt_id: Target ID of remote device.
3065 * @lun_id: Lun number for the TMF
3066 * @task_mgmt_cmd: type of TMF to send
3067 *
3068 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
3069 * a remote port.
3070 *
3071 * Return Code:
3072 * 0x2003 - Error
3073 * 0x2002 - Success.
3074 **/
3075static int
3076lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
3077 unsigned tgt_id, unsigned int lun_id,
3078 uint8_t task_mgmt_cmd)
3079{
3080 struct lpfc_hba *phba = vport->phba;
3081 struct lpfc_scsi_buf *lpfc_cmd;
3082 struct lpfc_iocbq *iocbq;
3083 struct lpfc_iocbq *iocbqrsp;
3084 int ret;
3085 int status;
3086
3087 if (!rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
3088 return FAILED;
3089
3090 lpfc_cmd = lpfc_get_scsi_buf(phba);
3091 if (lpfc_cmd == NULL)
3092 return FAILED;
3093 lpfc_cmd->timeout = 60;
3094 lpfc_cmd->rdata = rdata;
3095
3096 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
3097 task_mgmt_cmd);
3098 if (!status) {
3099 lpfc_release_scsi_buf(phba, lpfc_cmd);
3100 return FAILED;
3101 }
3102
3103 iocbq = &lpfc_cmd->cur_iocbq;
3104 iocbqrsp = lpfc_sli_get_iocbq(phba);
3105 if (iocbqrsp == NULL) {
3106 lpfc_release_scsi_buf(phba, lpfc_cmd);
3107 return FAILED;
3108 }
3109
3110 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3111 "0702 Issue %s to TGT %d LUN %d "
3112 "rpi x%x nlp_flag x%x\n",
3113 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
3114 rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
3115
3116 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
3117 iocbq, iocbqrsp, lpfc_cmd->timeout);
3118 if (status != IOCB_SUCCESS) {
3119 if (status == IOCB_TIMEDOUT) {
3120 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
3121 ret = TIMEOUT_ERROR;
3122 } else
3123 ret = FAILED;
3124 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3125 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3126 "0727 TMF %s to TGT %d LUN %d failed (%d, %d)\n",
3127 lpfc_taskmgmt_name(task_mgmt_cmd),
3128 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
3129 iocbqrsp->iocb.un.ulpWord[4]);
3130 } else
3131 ret = SUCCESS;
3132
3133 lpfc_sli_release_iocbq(phba, iocbqrsp);
3134
3135 if (ret != TIMEOUT_ERROR)
3136 lpfc_release_scsi_buf(phba, lpfc_cmd);
3137
3138 return ret;
3139}
3140
3141/**
3142 * lpfc_chk_tgt_mapped -
3143 * @vport: The virtual port to check on
3144 * @cmnd: Pointer to scsi_cmnd data structure.
3145 *
3146 * This routine delays until the scsi target (aka rport) for the
3147 * command exists (is present and logged in) or we declare it non-existent.
3148 *
3149 * Return code :
3150 * 0x2003 - Error
3151 * 0x2002 - Success
3152 **/
3153static int
3154lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
3155{
3156 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3157 struct lpfc_nodelist *pnode = rdata->pnode;
3158 unsigned long later;
3159
3160 /*
3161 * If target is not in a MAPPED state, delay until
3162 * target is rediscovered or devloss timeout expires.
3163 */
3164 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3165 while (time_after(later, jiffies)) {
3166 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3167 return FAILED;
3168 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
3169 return SUCCESS;
3170 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
3171 rdata = cmnd->device->hostdata;
3172 if (!rdata)
3173 return FAILED;
3174 pnode = rdata->pnode;
3175 }
3176 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
3177 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
3178 return FAILED;
3179 return SUCCESS;
3180}
3181
3182/**
3183 * lpfc_reset_flush_io_context -
3184 * @vport: The virtual port (scsi_host) for the flush context
3185 * @tgt_id: If aborting by Target contect - specifies the target id
3186 * @lun_id: If aborting by Lun context - specifies the lun id
3187 * @context: specifies the context level to flush at.
3188 *
3189 * After a reset condition via TMF, we need to flush orphaned i/o
3190 * contexts from the adapter. This routine aborts any contexts
3191 * outstanding, then waits for their completions. The wait is
3192 * bounded by devloss_tmo though.
3193 *
3194 * Return code :
3195 * 0x2003 - Error
3196 * 0x2002 - Success
3197 **/
3198static int
3199lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
3200 uint64_t lun_id, lpfc_ctx_cmd context)
3201{
3202 struct lpfc_hba *phba = vport->phba;
3203 unsigned long later;
3204 int cnt;
3205
3206 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3207 if (cnt)
3208 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
3209 tgt_id, lun_id, context);
3210 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3211 while (time_after(later, jiffies) && cnt) {
3212 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
3213 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3214 }
3215 if (cnt) {
3216 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3217 "0724 I/O flush failure for context %s : cnt x%x\n",
3218 ((context == LPFC_CTX_LUN) ? "LUN" :
3219 ((context == LPFC_CTX_TGT) ? "TGT" :
3220 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
3221 cnt);
3222 return FAILED;
3223 }
3224 return SUCCESS;
3225}
3226
James Smart9bad7672008-12-04 22:39:02 -05003227/**
James Smart3621a712009-04-06 18:47:14 -04003228 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05003229 * @cmnd: Pointer to scsi_cmnd data structure.
3230 *
James Smartbbb9d182009-06-10 17:23:16 -04003231 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05003232 * command.
3233 *
3234 * Return code :
3235 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04003236 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05003237 **/
dea31012005-04-17 16:05:31 -05003238static int
James Smart7054a602007-04-25 09:52:34 -04003239lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003240{
James Smart2e0fef82007-06-17 19:56:36 -05003241 struct Scsi_Host *shost = cmnd->device->host;
3242 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05003243 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3244 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003245 unsigned tgt_id = cmnd->device->id;
3246 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04003247 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04003248 int status;
dea31012005-04-17 16:05:31 -05003249
Christof Schmitt65d430f2009-10-30 17:59:29 +01003250 fc_block_scsi_eh(cmnd);
James Smartbbb9d182009-06-10 17:23:16 -04003251
3252 status = lpfc_chk_tgt_mapped(vport, cmnd);
3253 if (status == FAILED) {
3254 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3255 "0721 Device Reset rport failure: rdata x%p\n", rdata);
3256 return FAILED;
3257 }
3258
3259 scsi_event.event_type = FC_REG_SCSI_EVENT;
3260 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
3261 scsi_event.lun = lun_id;
3262 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3263 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3264
3265 fc_host_post_vendor_event(shost, fc_get_event_number(),
3266 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3267
3268 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3269 FCP_LUN_RESET);
3270
3271 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3272 "0713 SCSI layer issued Device Reset (%d, %d) "
3273 "return x%x\n", tgt_id, lun_id, status);
3274
dea31012005-04-17 16:05:31 -05003275 /*
James Smartbbb9d182009-06-10 17:23:16 -04003276 * We have to clean up i/o as : they may be orphaned by the TMF;
3277 * or if the TMF failed, they may be in an indeterminate state.
3278 * So, continue on.
3279 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05003280 */
James Smartbbb9d182009-06-10 17:23:16 -04003281 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3282 LPFC_CTX_LUN);
3283 return status;
3284}
3285
3286/**
3287 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
3288 * @cmnd: Pointer to scsi_cmnd data structure.
3289 *
3290 * This routine does a target reset by sending a TARGET_RESET task management
3291 * command.
3292 *
3293 * Return code :
3294 * 0x2003 - Error
3295 * 0x2002 - Success
3296 **/
3297static int
3298lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
3299{
3300 struct Scsi_Host *shost = cmnd->device->host;
3301 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3302 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3303 struct lpfc_nodelist *pnode = rdata->pnode;
3304 unsigned tgt_id = cmnd->device->id;
3305 unsigned int lun_id = cmnd->device->lun;
3306 struct lpfc_scsi_event_header scsi_event;
3307 int status;
3308
Christof Schmitt65d430f2009-10-30 17:59:29 +01003309 fc_block_scsi_eh(cmnd);
James Smartbbb9d182009-06-10 17:23:16 -04003310
3311 status = lpfc_chk_tgt_mapped(vport, cmnd);
3312 if (status == FAILED) {
3313 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3314 "0722 Target Reset rport failure: rdata x%p\n", rdata);
3315 return FAILED;
dea31012005-04-17 16:05:31 -05003316 }
James Smartea2151b2008-09-07 11:52:10 -04003317
3318 scsi_event.event_type = FC_REG_SCSI_EVENT;
3319 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
3320 scsi_event.lun = 0;
3321 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3322 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3323
James Smartbbb9d182009-06-10 17:23:16 -04003324 fc_host_post_vendor_event(shost, fc_get_event_number(),
3325 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04003326
James Smartbbb9d182009-06-10 17:23:16 -04003327 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3328 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05003329
James Smarte8b62012007-08-02 11:10:09 -04003330 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04003331 "0723 SCSI layer issued Target Reset (%d, %d) "
3332 "return x%x\n", tgt_id, lun_id, status);
3333
3334 /*
3335 * We have to clean up i/o as : they may be orphaned by the TMF;
3336 * or if the TMF failed, they may be in an indeterminate state.
3337 * So, continue on.
3338 * We will report success if all the i/o aborts successfully.
3339 */
3340 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3341 LPFC_CTX_TGT);
3342 return status;
dea31012005-04-17 16:05:31 -05003343}
3344
James Smart9bad7672008-12-04 22:39:02 -05003345/**
James Smart3621a712009-04-06 18:47:14 -04003346 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05003347 * @cmnd: Pointer to scsi_cmnd data structure.
3348 *
James Smartbbb9d182009-06-10 17:23:16 -04003349 * This routine does target reset to all targets on @cmnd->device->host.
3350 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05003351 *
James Smartbbb9d182009-06-10 17:23:16 -04003352 * Return code :
3353 * 0x2003 - Error
3354 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05003355 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04003356static int
James Smart7054a602007-04-25 09:52:34 -04003357lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003358{
James Smart2e0fef82007-06-17 19:56:36 -05003359 struct Scsi_Host *shost = cmnd->device->host;
3360 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05003361 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04003362 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04003363 int match;
3364 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04003365
3366 scsi_event.event_type = FC_REG_SCSI_EVENT;
3367 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
3368 scsi_event.lun = 0;
3369 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
3370 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
3371
James Smartbbb9d182009-06-10 17:23:16 -04003372 fc_host_post_vendor_event(shost, fc_get_event_number(),
3373 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05003374
Christof Schmitt65d430f2009-10-30 17:59:29 +01003375 fc_block_scsi_eh(cmnd);
James Smartbbb9d182009-06-10 17:23:16 -04003376
dea31012005-04-17 16:05:31 -05003377 /*
3378 * Since the driver manages a single bus device, reset all
3379 * targets known to the driver. Should any target reset
3380 * fail, this routine returns failure to the midlayer.
3381 */
James Smarte17da182006-07-06 15:49:25 -04003382 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04003383 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05003384 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003385 spin_lock_irq(shost->host_lock);
3386 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003387 if (!NLP_CHK_NODE_ACT(ndlp))
3388 continue;
James Smart685f0bf2007-04-25 09:53:08 -04003389 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04003390 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04003391 ndlp->rport) {
dea31012005-04-17 16:05:31 -05003392 match = 1;
3393 break;
3394 }
3395 }
James Smart2e0fef82007-06-17 19:56:36 -05003396 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003397 if (!match)
3398 continue;
James Smartbbb9d182009-06-10 17:23:16 -04003399
3400 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
3401 i, 0, FCP_TARGET_RESET);
3402
3403 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04003404 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3405 "0700 Bus Reset on target %d failed\n",
3406 i);
James Smart915caaa2008-06-14 22:52:38 -04003407 ret = FAILED;
dea31012005-04-17 16:05:31 -05003408 }
3409 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003410 /*
James Smartbbb9d182009-06-10 17:23:16 -04003411 * We have to clean up i/o as : they may be orphaned by the TMFs
3412 * above; or if any of the TMFs failed, they may be in an
3413 * indeterminate state.
3414 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003415 */
James Smartbbb9d182009-06-10 17:23:16 -04003416
3417 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
3418 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003419 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04003420
James Smarte8b62012007-08-02 11:10:09 -04003421 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3422 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05003423 return ret;
3424}
3425
James Smart9bad7672008-12-04 22:39:02 -05003426/**
James Smart3621a712009-04-06 18:47:14 -04003427 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05003428 * @sdev: Pointer to scsi_device.
3429 *
3430 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
3431 * globally available list of scsi buffers. This routine also makes sure scsi
3432 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
3433 * of scsi buffer exists for the lifetime of the driver.
3434 *
3435 * Return codes:
3436 * non-0 - Error
3437 * 0 - Success
3438 **/
dea31012005-04-17 16:05:31 -05003439static int
dea31012005-04-17 16:05:31 -05003440lpfc_slave_alloc(struct scsi_device *sdev)
3441{
James Smart2e0fef82007-06-17 19:56:36 -05003442 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3443 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003444 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04003445 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05003446 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04003447 int num_allocated = 0;
dea31012005-04-17 16:05:31 -05003448
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003449 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05003450 return -ENXIO;
3451
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003452 sdev->hostdata = rport->dd_data;
dea31012005-04-17 16:05:31 -05003453
3454 /*
3455 * Populate the cmds_per_lun count scsi_bufs into this host's globally
3456 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04003457 * HBA limit conveyed to the midlayer via the host structure. The
3458 * formula accounts for the lun_queue_depth + error handlers + 1
3459 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05003460 */
3461 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04003462 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05003463
3464 /* Allow some exchanges to be available always to complete discovery */
3465 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04003466 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3467 "0704 At limitation of %d preallocated "
3468 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05003469 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003470 /* Allow some exchanges to be available always to complete discovery */
3471 } else if (total + num_to_alloc >
3472 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04003473 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3474 "0705 Allocation request of %d "
3475 "command buffers will exceed max of %d. "
3476 "Reducing allocation request to %d.\n",
3477 num_to_alloc, phba->cfg_hba_queue_depth,
3478 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05003479 num_to_alloc = phba->cfg_hba_queue_depth - total;
3480 }
James Smart3772a992009-05-22 14:50:54 -04003481 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
3482 if (num_to_alloc != num_allocated) {
3483 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3484 "0708 Allocation request of %d "
3485 "command buffers did not succeed. "
3486 "Allocated %d buffers.\n",
3487 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05003488 }
3489 return 0;
3490}
3491
James Smart9bad7672008-12-04 22:39:02 -05003492/**
James Smart3621a712009-04-06 18:47:14 -04003493 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05003494 * @sdev: Pointer to scsi_device.
3495 *
3496 * This routine configures following items
3497 * - Tag command queuing support for @sdev if supported.
3498 * - Dev loss time out value of fc_rport.
3499 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
3500 *
3501 * Return codes:
3502 * 0 - Success
3503 **/
dea31012005-04-17 16:05:31 -05003504static int
3505lpfc_slave_configure(struct scsi_device *sdev)
3506{
James Smart2e0fef82007-06-17 19:56:36 -05003507 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3508 struct lpfc_hba *phba = vport->phba;
3509 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
dea31012005-04-17 16:05:31 -05003510
3511 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04003512 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05003513 else
James Smart3de2a652007-08-02 11:09:59 -04003514 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05003515
3516 /*
3517 * Initialize the fc transport attributes for the target
3518 * containing this scsi device. Also note that the driver's
3519 * target pointer is stored in the starget_data for the
3520 * driver's sysfs entry point functions.
3521 */
James Smart3de2a652007-08-02 11:09:59 -04003522 rport->dev_loss_tmo = vport->cfg_devloss_tmo;
dea31012005-04-17 16:05:31 -05003523
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003524 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04003525 lpfc_sli_handle_fast_ring_event(phba,
3526 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003527 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3528 lpfc_poll_rearm_timer(phba);
3529 }
3530
dea31012005-04-17 16:05:31 -05003531 return 0;
3532}
3533
James Smart9bad7672008-12-04 22:39:02 -05003534/**
James Smart3621a712009-04-06 18:47:14 -04003535 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05003536 * @sdev: Pointer to scsi_device.
3537 *
3538 * This routine sets @sdev hostatdata filed to null.
3539 **/
dea31012005-04-17 16:05:31 -05003540static void
3541lpfc_slave_destroy(struct scsi_device *sdev)
3542{
3543 sdev->hostdata = NULL;
3544 return;
3545}
3546
James Smart92d7f7b2007-06-17 19:56:38 -05003547
dea31012005-04-17 16:05:31 -05003548struct scsi_host_template lpfc_template = {
3549 .module = THIS_MODULE,
3550 .name = LPFC_DRIVER_NAME,
3551 .info = lpfc_info,
3552 .queuecommand = lpfc_queuecommand,
3553 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04003554 .eh_device_reset_handler = lpfc_device_reset_handler,
3555 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04003556 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05003557 .slave_alloc = lpfc_slave_alloc,
3558 .slave_configure = lpfc_slave_configure,
3559 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04003560 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05003561 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05003562 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05003563 .cmd_per_lun = LPFC_CMD_PER_LUN,
3564 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05003565 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04003566 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04003567 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05003568 .change_queue_depth = lpfc_change_queue_depth,
dea31012005-04-17 16:05:31 -05003569};
James Smart3de2a652007-08-02 11:09:59 -04003570
3571struct scsi_host_template lpfc_vport_template = {
3572 .module = THIS_MODULE,
3573 .name = LPFC_DRIVER_NAME,
3574 .info = lpfc_info,
3575 .queuecommand = lpfc_queuecommand,
3576 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04003577 .eh_device_reset_handler = lpfc_device_reset_handler,
3578 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04003579 .eh_bus_reset_handler = lpfc_bus_reset_handler,
3580 .slave_alloc = lpfc_slave_alloc,
3581 .slave_configure = lpfc_slave_configure,
3582 .slave_destroy = lpfc_slave_destroy,
3583 .scan_finished = lpfc_scan_finished,
3584 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05003585 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04003586 .cmd_per_lun = LPFC_CMD_PER_LUN,
3587 .use_clustering = ENABLE_CLUSTERING,
3588 .shost_attrs = lpfc_vport_attrs,
3589 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05003590 .change_queue_depth = lpfc_change_queue_depth,
James Smart3de2a652007-08-02 11:09:59 -04003591};