blob: 5b8790b3cf4bba7a2791da9acdd68ed8f7c7cbaa [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smart792581d2011-03-11 16:06:44 -05004 * Copyright (C) 2004-2011 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
dea31012005-04-17 16:05:31 -050021#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050023#include <linux/interrupt.h>
James Smarta90f5682006-08-17 11:58:04 -040024#include <linux/delay.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050025#include <asm/unaligned.h>
dea31012005-04-17 16:05:31 -050026
27#include <scsi/scsi.h>
28#include <scsi/scsi_device.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050029#include <scsi/scsi_eh.h>
dea31012005-04-17 16:05:31 -050030#include <scsi/scsi_host.h>
31#include <scsi/scsi_tcq.h>
32#include <scsi/scsi_transport_fc.h>
33
34#include "lpfc_version.h"
James Smartda0436e2009-05-22 14:51:39 -040035#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050036#include "lpfc_hw.h"
37#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040038#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040039#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050040#include "lpfc_disc.h"
41#include "lpfc_scsi.h"
42#include "lpfc.h"
43#include "lpfc_logmsg.h"
44#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050045#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050046
47#define LPFC_RESET_WAIT 2
48#define LPFC_ABORT_WAIT 2
49
James Smarte2a0a9d2008-12-04 22:40:02 -050050int _dump_buf_done;
51
52static char *dif_op_str[] = {
53 "SCSI_PROT_NORMAL",
54 "SCSI_PROT_READ_INSERT",
55 "SCSI_PROT_WRITE_STRIP",
56 "SCSI_PROT_READ_STRIP",
57 "SCSI_PROT_WRITE_INSERT",
58 "SCSI_PROT_READ_PASS",
59 "SCSI_PROT_WRITE_PASS",
James Smarte2a0a9d2008-12-04 22:40:02 -050060};
James Smartf9bb2da2011-10-10 21:34:11 -040061
62struct scsi_dif_tuple {
63 __be16 guard_tag; /* Checksum */
64 __be16 app_tag; /* Opaque storage */
65 __be32 ref_tag; /* Target LBA or indirect LBA */
66};
67
James Smartda0436e2009-05-22 14:51:39 -040068static void
69lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smart1c6f4ef52009-11-18 15:40:49 -050070static void
71lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smarte2a0a9d2008-12-04 22:40:02 -050072
73static void
James Smart6a9c52c2009-10-02 15:16:51 -040074lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -050075{
76 void *src, *dst;
77 struct scatterlist *sgde = scsi_sglist(cmnd);
78
79 if (!_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040080 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
81 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -050082 __func__);
83 return;
84 }
85
86
87 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -040088 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
89 "9051 BLKGRD: ERROR: data scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -050090 return;
91 }
92
93 dst = (void *) _dump_buf_data;
94 while (sgde) {
95 src = sg_virt(sgde);
96 memcpy(dst, src, sgde->length);
97 dst += sgde->length;
98 sgde = sg_next(sgde);
99 }
100}
101
102static void
James Smart6a9c52c2009-10-02 15:16:51 -0400103lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -0500104{
105 void *src, *dst;
106 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
107
108 if (!_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -0400109 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
110 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -0500111 __func__);
112 return;
113 }
114
115 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -0400116 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
117 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -0500118 return;
119 }
120
121 dst = _dump_buf_dif;
122 while (sgde) {
123 src = sg_virt(sgde);
124 memcpy(dst, src, sgde->length);
125 dst += sgde->length;
126 sgde = sg_next(sgde);
127 }
128}
129
James Smartea2151b2008-09-07 11:52:10 -0400130/**
James Smartf1126682009-06-10 17:22:44 -0400131 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
132 * @phba: Pointer to HBA object.
133 * @lpfc_cmd: lpfc scsi command object pointer.
134 *
135 * This function is called from the lpfc_prep_task_mgmt_cmd function to
136 * set the last bit in the response sge entry.
137 **/
138static void
139lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
140 struct lpfc_scsi_buf *lpfc_cmd)
141{
142 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
143 if (sgl) {
144 sgl += 1;
145 sgl->word2 = le32_to_cpu(sgl->word2);
146 bf_set(lpfc_sli4_sge_last, sgl, 1);
147 sgl->word2 = cpu_to_le32(sgl->word2);
148 }
149}
150
151/**
James Smart3621a712009-04-06 18:47:14 -0400152 * lpfc_update_stats - Update statistical data for the command completion
James Smartea2151b2008-09-07 11:52:10 -0400153 * @phba: Pointer to HBA object.
154 * @lpfc_cmd: lpfc scsi command object pointer.
155 *
156 * This function is called when there is a command completion and this
157 * function updates the statistical data for the command completion.
158 **/
159static void
160lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
161{
162 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
163 struct lpfc_nodelist *pnode = rdata->pnode;
164 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
165 unsigned long flags;
166 struct Scsi_Host *shost = cmd->device->host;
167 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
168 unsigned long latency;
169 int i;
170
171 if (cmd->result)
172 return;
173
James Smart9f1e1b52008-12-04 22:39:40 -0500174 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
175
James Smartea2151b2008-09-07 11:52:10 -0400176 spin_lock_irqsave(shost->host_lock, flags);
177 if (!vport->stat_data_enabled ||
178 vport->stat_data_blocked ||
James Smart5989b8d2010-10-22 11:06:56 -0400179 !pnode ||
James Smartea2151b2008-09-07 11:52:10 -0400180 !pnode->lat_data ||
181 (phba->bucket_type == LPFC_NO_BUCKET)) {
182 spin_unlock_irqrestore(shost->host_lock, flags);
183 return;
184 }
James Smartea2151b2008-09-07 11:52:10 -0400185
186 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
187 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
188 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -0500189 /* check array subscript bounds */
190 if (i < 0)
191 i = 0;
192 else if (i >= LPFC_MAX_BUCKET_COUNT)
193 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -0400194 } else {
195 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
196 if (latency <= (phba->bucket_base +
197 ((1<<i)*phba->bucket_step)))
198 break;
199 }
200
201 pnode->lat_data[i].cmd_count++;
202 spin_unlock_irqrestore(shost->host_lock, flags);
203}
204
James Smartea2151b2008-09-07 11:52:10 -0400205/**
James Smart3621a712009-04-06 18:47:14 -0400206 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
James Smartea2151b2008-09-07 11:52:10 -0400207 * @phba: Pointer to HBA context object.
208 * @vport: Pointer to vport object.
209 * @ndlp: Pointer to FC node associated with the target.
210 * @lun: Lun number of the scsi device.
211 * @old_val: Old value of the queue depth.
212 * @new_val: New value of the queue depth.
213 *
214 * This function sends an event to the mgmt application indicating
215 * there is a change in the scsi device queue depth.
216 **/
217static void
218lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
219 struct lpfc_vport *vport,
220 struct lpfc_nodelist *ndlp,
221 uint32_t lun,
222 uint32_t old_val,
223 uint32_t new_val)
224{
225 struct lpfc_fast_path_event *fast_path_evt;
226 unsigned long flags;
227
228 fast_path_evt = lpfc_alloc_fast_evt(phba);
229 if (!fast_path_evt)
230 return;
231
232 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
233 FC_REG_SCSI_EVENT;
234 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
235 LPFC_EVENT_VARQUEDEPTH;
236
237 /* Report all luns with change in queue depth */
238 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
239 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
240 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
241 &ndlp->nlp_portname, sizeof(struct lpfc_name));
242 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
243 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
244 }
245
246 fast_path_evt->un.queue_depth_evt.oldval = old_val;
247 fast_path_evt->un.queue_depth_evt.newval = new_val;
248 fast_path_evt->vport = vport;
249
250 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
251 spin_lock_irqsave(&phba->hbalock, flags);
252 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
253 spin_unlock_irqrestore(&phba->hbalock, flags);
254 lpfc_worker_wake_up(phba);
255
256 return;
257}
258
James Smart9bad7672008-12-04 22:39:02 -0500259/**
James Smart5ffc2662009-11-18 15:39:44 -0500260 * lpfc_change_queue_depth - Alter scsi device queue depth
261 * @sdev: Pointer the scsi device on which to change the queue depth.
262 * @qdepth: New queue depth to set the sdev to.
263 * @reason: The reason for the queue depth change.
264 *
265 * This function is called by the midlayer and the LLD to alter the queue
266 * depth for a scsi device. This function sets the queue depth to the new
267 * value and sends an event out to log the queue depth change.
268 **/
269int
270lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
271{
272 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
273 struct lpfc_hba *phba = vport->phba;
274 struct lpfc_rport_data *rdata;
275 unsigned long new_queue_depth, old_queue_depth;
276
277 old_queue_depth = sdev->queue_depth;
278 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
279 new_queue_depth = sdev->queue_depth;
280 rdata = sdev->hostdata;
281 if (rdata)
282 lpfc_send_sdev_queuedepth_change_event(phba, vport,
283 rdata->pnode, sdev->lun,
284 old_queue_depth,
285 new_queue_depth);
286 return sdev->queue_depth;
287}
288
289/**
James Smart3621a712009-04-06 18:47:14 -0400290 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500291 * @phba: The Hba for which this call is being executed.
292 *
293 * This routine is called when there is resource error in driver or firmware.
294 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
295 * posts at most 1 event each second. This routine wakes up worker thread of
296 * @phba to process WORKER_RAM_DOWN_EVENT event.
297 *
298 * This routine should be called with no lock held.
299 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500300void
James Smarteaf15d52008-12-04 22:39:29 -0500301lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500302{
303 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400304 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500305
306 spin_lock_irqsave(&phba->hbalock, flags);
307 atomic_inc(&phba->num_rsrc_err);
308 phba->last_rsrc_error_time = jiffies;
309
310 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
311 spin_unlock_irqrestore(&phba->hbalock, flags);
312 return;
313 }
314
315 phba->last_ramp_down_time = jiffies;
316
317 spin_unlock_irqrestore(&phba->hbalock, flags);
318
319 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400320 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
321 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500322 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500323 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
324
James Smart5e9d9b82008-06-14 22:52:53 -0400325 if (!evt_posted)
326 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500327 return;
328}
329
James Smart9bad7672008-12-04 22:39:02 -0500330/**
James Smart3621a712009-04-06 18:47:14 -0400331 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
James Smart9bad7672008-12-04 22:39:02 -0500332 * @phba: The Hba for which this call is being executed.
333 *
334 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
335 * post at most 1 event every 5 minute after last_ramp_up_time or
336 * last_rsrc_error_time. This routine wakes up worker thread of @phba
337 * to process WORKER_RAM_DOWN_EVENT event.
338 *
339 * This routine should be called with no lock held.
340 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500341static inline void
James Smart3de2a652007-08-02 11:09:59 -0400342lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smarta257bf92009-04-06 18:48:10 -0400343 uint32_t queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500344{
345 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -0400346 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400347 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500348 atomic_inc(&phba->num_cmd_success);
349
James Smarta257bf92009-04-06 18:48:10 -0400350 if (vport->cfg_lun_queue_depth <= queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500351 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500352 spin_lock_irqsave(&phba->hbalock, flags);
James Smart5ffc2662009-11-18 15:39:44 -0500353 if (time_before(jiffies,
354 phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
355 time_before(jiffies,
356 phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500357 spin_unlock_irqrestore(&phba->hbalock, flags);
358 return;
359 }
James Smart92d7f7b2007-06-17 19:56:38 -0500360 phba->last_ramp_up_time = jiffies;
361 spin_unlock_irqrestore(&phba->hbalock, flags);
362
363 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400364 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
365 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500366 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500367 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
368
James Smart5e9d9b82008-06-14 22:52:53 -0400369 if (!evt_posted)
370 lpfc_worker_wake_up(phba);
371 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500372}
373
James Smart9bad7672008-12-04 22:39:02 -0500374/**
James Smart3621a712009-04-06 18:47:14 -0400375 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500376 * @phba: The Hba for which this call is being executed.
377 *
378 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
379 * thread.This routine reduces queue depth for all scsi device on each vport
380 * associated with @phba.
381 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500382void
383lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
384{
James Smart549e55c2007-08-02 11:09:51 -0400385 struct lpfc_vport **vports;
386 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500387 struct scsi_device *sdev;
James Smart5ffc2662009-11-18 15:39:44 -0500388 unsigned long new_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500389 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400390 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500391
392 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
393 num_cmd_success = atomic_read(&phba->num_cmd_success);
394
James Smart549e55c2007-08-02 11:09:51 -0400395 vports = lpfc_create_vport_work_array(phba);
396 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400397 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400398 shost = lpfc_shost_from_vport(vports[i]);
399 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500400 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400401 sdev->queue_depth * num_rsrc_err /
402 (num_rsrc_err + num_cmd_success);
403 if (!new_queue_depth)
404 new_queue_depth = sdev->queue_depth - 1;
405 else
406 new_queue_depth = sdev->queue_depth -
407 new_queue_depth;
James Smart5ffc2662009-11-18 15:39:44 -0500408 lpfc_change_queue_depth(sdev, new_queue_depth,
409 SCSI_QDEPTH_DEFAULT);
James Smart549e55c2007-08-02 11:09:51 -0400410 }
James Smart92d7f7b2007-06-17 19:56:38 -0500411 }
James Smart09372822008-01-11 01:52:54 -0500412 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500413 atomic_set(&phba->num_rsrc_err, 0);
414 atomic_set(&phba->num_cmd_success, 0);
415}
416
James Smart9bad7672008-12-04 22:39:02 -0500417/**
James Smart3621a712009-04-06 18:47:14 -0400418 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500419 * @phba: The Hba for which this call is being executed.
420 *
421 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
422 * thread.This routine increases queue depth for all scsi device on each vport
423 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
424 * num_cmd_success to zero.
425 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500426void
427lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
428{
James Smart549e55c2007-08-02 11:09:51 -0400429 struct lpfc_vport **vports;
430 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500431 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400432 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500433
James Smart549e55c2007-08-02 11:09:51 -0400434 vports = lpfc_create_vport_work_array(phba);
435 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400436 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400437 shost = lpfc_shost_from_vport(vports[i]);
438 shost_for_each_device(sdev, shost) {
James Smart97eab632008-04-07 10:16:05 -0400439 if (vports[i]->cfg_lun_queue_depth <=
440 sdev->queue_depth)
441 continue;
James Smart5ffc2662009-11-18 15:39:44 -0500442 lpfc_change_queue_depth(sdev,
443 sdev->queue_depth+1,
444 SCSI_QDEPTH_RAMP_UP);
James Smart549e55c2007-08-02 11:09:51 -0400445 }
James Smart92d7f7b2007-06-17 19:56:38 -0500446 }
James Smart09372822008-01-11 01:52:54 -0500447 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500448 atomic_set(&phba->num_rsrc_err, 0);
449 atomic_set(&phba->num_cmd_success, 0);
450}
451
James Smarta8e497d2008-08-24 21:50:11 -0400452/**
James Smart3621a712009-04-06 18:47:14 -0400453 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400454 * @phba: Pointer to HBA context object.
455 *
456 * This function walks vport list and set each SCSI host to block state
457 * by invoking fc_remote_port_delete() routine. This function is invoked
458 * with EEH when device's PCI slot has been permanently disabled.
459 **/
460void
461lpfc_scsi_dev_block(struct lpfc_hba *phba)
462{
463 struct lpfc_vport **vports;
464 struct Scsi_Host *shost;
465 struct scsi_device *sdev;
466 struct fc_rport *rport;
467 int i;
468
469 vports = lpfc_create_vport_work_array(phba);
470 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400471 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400472 shost = lpfc_shost_from_vport(vports[i]);
473 shost_for_each_device(sdev, shost) {
474 rport = starget_to_rport(scsi_target(sdev));
475 fc_remote_port_delete(rport);
476 }
477 }
478 lpfc_destroy_vport_work_array(phba, vports);
479}
480
James Smart9bad7672008-12-04 22:39:02 -0500481/**
James Smart3772a992009-05-22 14:50:54 -0400482 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500483 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400484 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500485 *
James Smart3772a992009-05-22 14:50:54 -0400486 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
487 * the scsi buffer contains all the necessary information needed to initiate
488 * a SCSI I/O. The non-DMAable buffer region contains information to build
489 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
490 * and the initial BPL. In addition to allocating memory, the FCP CMND and
491 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500492 *
493 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400494 * int - number of scsi buffers that were allocated.
495 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500496 **/
James Smart3772a992009-05-22 14:50:54 -0400497static int
498lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500499{
James Smart2e0fef82007-06-17 19:56:36 -0500500 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500501 struct lpfc_scsi_buf *psb;
502 struct ulp_bde64 *bpl;
503 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400504 dma_addr_t pdma_phys_fcp_cmd;
505 dma_addr_t pdma_phys_fcp_rsp;
506 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500507 uint16_t iotag;
James Smart3772a992009-05-22 14:50:54 -0400508 int bcnt;
dea31012005-04-17 16:05:31 -0500509
James Smart3772a992009-05-22 14:50:54 -0400510 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
511 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
512 if (!psb)
513 break;
dea31012005-04-17 16:05:31 -0500514
James Smart3772a992009-05-22 14:50:54 -0400515 /*
516 * Get memory from the pci pool to map the virt space to pci
517 * bus space for an I/O. The DMA buffer includes space for the
518 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
519 * necessary to support the sg_tablesize.
520 */
521 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
522 GFP_KERNEL, &psb->dma_handle);
523 if (!psb->data) {
524 kfree(psb);
525 break;
526 }
dea31012005-04-17 16:05:31 -0500527
James Smart3772a992009-05-22 14:50:54 -0400528 /* Initialize virtual ptrs to dma_buf region. */
529 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
dea31012005-04-17 16:05:31 -0500530
James Smart3772a992009-05-22 14:50:54 -0400531 /* Allocate iotag for psb->cur_iocbq. */
532 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
533 if (iotag == 0) {
534 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
535 psb->data, psb->dma_handle);
536 kfree(psb);
537 break;
538 }
539 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500540
James Smart3772a992009-05-22 14:50:54 -0400541 psb->fcp_cmnd = psb->data;
542 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
543 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400544 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500545
James Smart3772a992009-05-22 14:50:54 -0400546 /* Initialize local short-hand pointers. */
547 bpl = psb->fcp_bpl;
548 pdma_phys_fcp_cmd = psb->dma_handle;
549 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
550 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
551 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500552
James Smart3772a992009-05-22 14:50:54 -0400553 /*
554 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
555 * are sg list bdes. Initialize the first two and leave the
556 * rest for queuecommand.
557 */
558 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
559 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
560 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
561 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
562 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500563
James Smart3772a992009-05-22 14:50:54 -0400564 /* Setup the physical region for the FCP RSP */
565 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
566 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
567 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
568 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
569 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
570
571 /*
572 * Since the IOCB for the FCP I/O is built into this
573 * lpfc_scsi_buf, initialize it with all known data now.
574 */
575 iocb = &psb->cur_iocbq.iocb;
576 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
577 if ((phba->sli_rev == 3) &&
578 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
579 /* fill in immediate fcp command BDE */
580 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
581 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
582 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
583 unsli3.fcp_ext.icd);
584 iocb->un.fcpi64.bdl.addrHigh = 0;
585 iocb->ulpBdeCount = 0;
586 iocb->ulpLe = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300587 /* fill in response BDE */
James Smart3772a992009-05-22 14:50:54 -0400588 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
589 BUFF_TYPE_BDE_64;
590 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
591 sizeof(struct fcp_rsp);
592 iocb->unsli3.fcp_ext.rbde.addrLow =
593 putPaddrLow(pdma_phys_fcp_rsp);
594 iocb->unsli3.fcp_ext.rbde.addrHigh =
595 putPaddrHigh(pdma_phys_fcp_rsp);
596 } else {
597 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
598 iocb->un.fcpi64.bdl.bdeSize =
599 (2 * sizeof(struct ulp_bde64));
600 iocb->un.fcpi64.bdl.addrLow =
601 putPaddrLow(pdma_phys_bpl);
602 iocb->un.fcpi64.bdl.addrHigh =
603 putPaddrHigh(pdma_phys_bpl);
604 iocb->ulpBdeCount = 1;
605 iocb->ulpLe = 1;
606 }
607 iocb->ulpClass = CLASS3;
608 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400609 /* Put it back into the SCSI buffer list */
James Smarteee88772010-09-29 11:19:08 -0400610 psb->cur_iocbq.context1 = psb;
James Smart1c6f4ef52009-11-18 15:40:49 -0500611 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400612
James Smart34b02dc2008-08-24 21:49:55 -0400613 }
dea31012005-04-17 16:05:31 -0500614
James Smart3772a992009-05-22 14:50:54 -0400615 return bcnt;
dea31012005-04-17 16:05:31 -0500616}
617
James Smart9bad7672008-12-04 22:39:02 -0500618/**
James Smart1151e3e2011-02-16 12:39:35 -0500619 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
620 * @vport: pointer to lpfc vport data structure.
621 *
622 * This routine is invoked by the vport cleanup for deletions and the cleanup
623 * for an ndlp on removal.
624 **/
625void
626lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
627{
628 struct lpfc_hba *phba = vport->phba;
629 struct lpfc_scsi_buf *psb, *next_psb;
630 unsigned long iflag = 0;
631
632 spin_lock_irqsave(&phba->hbalock, iflag);
633 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
634 list_for_each_entry_safe(psb, next_psb,
635 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
636 if (psb->rdata && psb->rdata->pnode
637 && psb->rdata->pnode->vport == vport)
638 psb->rdata = NULL;
639 }
640 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
641 spin_unlock_irqrestore(&phba->hbalock, iflag);
642}
643
644/**
James Smartda0436e2009-05-22 14:51:39 -0400645 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
646 * @phba: pointer to lpfc hba data structure.
647 * @axri: pointer to the fcp xri abort wcqe structure.
648 *
649 * This routine is invoked by the worker thread to process a SLI4 fast-path
650 * FCP aborted xri.
651 **/
652void
653lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
654 struct sli4_wcqe_xri_aborted *axri)
655{
656 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500657 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartda0436e2009-05-22 14:51:39 -0400658 struct lpfc_scsi_buf *psb, *next_psb;
659 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500660 struct lpfc_iocbq *iocbq;
661 int i;
James Smart19ca7602010-11-20 23:11:55 -0500662 struct lpfc_nodelist *ndlp;
663 int rrq_empty = 0;
James Smart589a52d2010-07-14 15:30:54 -0400664 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smartda0436e2009-05-22 14:51:39 -0400665
James Smart0f65ff62010-02-26 14:14:23 -0500666 spin_lock_irqsave(&phba->hbalock, iflag);
667 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400668 list_for_each_entry_safe(psb, next_psb,
669 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
670 if (psb->cur_iocbq.sli4_xritag == xri) {
671 list_del(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500672 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400673 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500674 spin_unlock(
675 &phba->sli4_hba.abts_scsi_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500676 if (psb->rdata && psb->rdata->pnode)
677 ndlp = psb->rdata->pnode;
678 else
679 ndlp = NULL;
680
James Smart19ca7602010-11-20 23:11:55 -0500681 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500682 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart19ca7602010-11-20 23:11:55 -0500683 if (ndlp)
684 lpfc_set_rrq_active(phba, ndlp, xri, rxid, 1);
James Smartda0436e2009-05-22 14:51:39 -0400685 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500686 if (rrq_empty)
687 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400688 return;
689 }
690 }
James Smart0f65ff62010-02-26 14:14:23 -0500691 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
692 for (i = 1; i <= phba->sli.last_iotag; i++) {
693 iocbq = phba->sli.iocbq_lookup[i];
694
695 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
696 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
697 continue;
698 if (iocbq->sli4_xritag != xri)
699 continue;
700 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
701 psb->exch_busy = 0;
702 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart589a52d2010-07-14 15:30:54 -0400703 if (pring->txq_cnt)
704 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500705 return;
706
707 }
708 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400709}
710
711/**
712 * lpfc_sli4_repost_scsi_sgl_list - Repsot the Scsi buffers sgl pages as block
713 * @phba: pointer to lpfc hba data structure.
714 *
715 * This routine walks the list of scsi buffers that have been allocated and
716 * repost them to the HBA by using SGL block post. This is needed after a
717 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
718 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
719 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
720 *
721 * Returns: 0 = success, non-zero failure.
722 **/
723int
724lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
725{
726 struct lpfc_scsi_buf *psb;
727 int index, status, bcnt = 0, rcnt = 0, rc = 0;
728 LIST_HEAD(sblist);
729
730 for (index = 0; index < phba->sli4_hba.scsi_xri_cnt; index++) {
731 psb = phba->sli4_hba.lpfc_scsi_psb_array[index];
732 if (psb) {
733 /* Remove from SCSI buffer list */
734 list_del(&psb->list);
735 /* Add it to a local SCSI buffer list */
736 list_add_tail(&psb->list, &sblist);
737 if (++rcnt == LPFC_NEMBED_MBOX_SGL_CNT) {
738 bcnt = rcnt;
739 rcnt = 0;
740 }
741 } else
742 /* A hole present in the XRI array, need to skip */
743 bcnt = rcnt;
744
745 if (index == phba->sli4_hba.scsi_xri_cnt - 1)
746 /* End of XRI array for SCSI buffer, complete */
747 bcnt = rcnt;
748
749 /* Continue until collect up to a nembed page worth of sgls */
750 if (bcnt == 0)
751 continue;
752 /* Now, post the SCSI buffer list sgls as a block */
James Smart6d368e52011-05-24 11:44:12 -0400753 if (!phba->sli4_hba.extents_in_use)
754 status = lpfc_sli4_post_scsi_sgl_block(phba,
755 &sblist,
756 bcnt);
757 else
758 status = lpfc_sli4_post_scsi_sgl_blk_ext(phba,
759 &sblist,
760 bcnt);
James Smartda0436e2009-05-22 14:51:39 -0400761 /* Reset SCSI buffer count for next round of posting */
762 bcnt = 0;
763 while (!list_empty(&sblist)) {
764 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
765 list);
766 if (status) {
767 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500768 psb->exch_busy = 1;
James Smartda0436e2009-05-22 14:51:39 -0400769 rc++;
James Smart341af102010-01-26 23:07:37 -0500770 } else {
771 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400772 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500773 }
James Smartda0436e2009-05-22 14:51:39 -0400774 /* Put it back into the SCSI buffer list */
775 lpfc_release_scsi_buf_s4(phba, psb);
776 }
777 }
778 return rc;
779}
780
781/**
782 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
783 * @vport: The virtual port for which this call being executed.
784 * @num_to_allocate: The requested number of buffers to allocate.
785 *
786 * This routine allocates a scsi buffer for device with SLI-4 interface spec,
787 * the scsi buffer contains all the necessary information needed to initiate
788 * a SCSI I/O.
789 *
790 * Return codes:
791 * int - number of scsi buffers that were allocated.
792 * 0 = failure, less than num_to_alloc is a partial failure.
793 **/
794static int
795lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
796{
797 struct lpfc_hba *phba = vport->phba;
798 struct lpfc_scsi_buf *psb;
799 struct sli4_sge *sgl;
800 IOCB_t *iocb;
801 dma_addr_t pdma_phys_fcp_cmd;
802 dma_addr_t pdma_phys_fcp_rsp;
803 dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
James Smart6d368e52011-05-24 11:44:12 -0400804 uint16_t iotag, last_xritag = NO_XRI, lxri = 0;
James Smartda0436e2009-05-22 14:51:39 -0400805 int status = 0, index;
806 int bcnt;
807 int non_sequential_xri = 0;
James Smartda0436e2009-05-22 14:51:39 -0400808 LIST_HEAD(sblist);
809
810 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
811 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
812 if (!psb)
813 break;
814
815 /*
816 * Get memory from the pci pool to map the virt space to pci bus
817 * space for an I/O. The DMA buffer includes space for the
818 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
819 * necessary to support the sg_tablesize.
820 */
821 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
822 GFP_KERNEL, &psb->dma_handle);
823 if (!psb->data) {
824 kfree(psb);
825 break;
826 }
827
828 /* Initialize virtual ptrs to dma_buf region. */
829 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
830
831 /* Allocate iotag for psb->cur_iocbq. */
832 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
833 if (iotag == 0) {
James Smartb92938b2010-06-07 15:24:12 -0400834 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
835 psb->data, psb->dma_handle);
James Smartda0436e2009-05-22 14:51:39 -0400836 kfree(psb);
837 break;
838 }
839
James Smart6d368e52011-05-24 11:44:12 -0400840 lxri = lpfc_sli4_next_xritag(phba);
841 if (lxri == NO_XRI) {
James Smartda0436e2009-05-22 14:51:39 -0400842 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
843 psb->data, psb->dma_handle);
844 kfree(psb);
845 break;
846 }
James Smart6d368e52011-05-24 11:44:12 -0400847 psb->cur_iocbq.sli4_lxritag = lxri;
848 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smartda0436e2009-05-22 14:51:39 -0400849 if (last_xritag != NO_XRI
850 && psb->cur_iocbq.sli4_xritag != (last_xritag+1)) {
851 non_sequential_xri = 1;
852 } else
853 list_add_tail(&psb->list, &sblist);
854 last_xritag = psb->cur_iocbq.sli4_xritag;
855
856 index = phba->sli4_hba.scsi_xri_cnt++;
857 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
858
859 psb->fcp_bpl = psb->data;
860 psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
861 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
862 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
863 sizeof(struct fcp_cmnd));
864
865 /* Initialize local short-hand pointers. */
866 sgl = (struct sli4_sge *)psb->fcp_bpl;
867 pdma_phys_bpl = psb->dma_handle;
868 pdma_phys_fcp_cmd =
869 (psb->dma_handle + phba->cfg_sg_dma_buf_size)
870 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
871 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
872
873 /*
874 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
875 * are sg list bdes. Initialize the first two and leave the
876 * rest for queuecommand.
877 */
878 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
879 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
James Smart05580562011-05-24 11:40:48 -0400880 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400881 bf_set(lpfc_sli4_sge_last, sgl, 0);
882 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500883 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
James Smartda0436e2009-05-22 14:51:39 -0400884 sgl++;
885
886 /* Setup the physical region for the FCP RSP */
887 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
888 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smart05580562011-05-24 11:40:48 -0400889 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400890 bf_set(lpfc_sli4_sge_last, sgl, 1);
891 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500892 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -0400893
894 /*
895 * Since the IOCB for the FCP I/O is built into this
896 * lpfc_scsi_buf, initialize it with all known data now.
897 */
898 iocb = &psb->cur_iocbq.iocb;
899 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
900 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
901 /* setting the BLP size to 2 * sizeof BDE may not be correct.
902 * We are setting the bpl to point to out sgl. An sgl's
903 * entries are 16 bytes, a bpl entries are 12 bytes.
904 */
905 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
906 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
907 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
908 iocb->ulpBdeCount = 1;
909 iocb->ulpLe = 1;
910 iocb->ulpClass = CLASS3;
James Smarteee88772010-09-29 11:19:08 -0400911 psb->cur_iocbq.context1 = psb;
James Smartda0436e2009-05-22 14:51:39 -0400912 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
913 pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
914 else
915 pdma_phys_bpl1 = 0;
916 psb->dma_phys_bpl = pdma_phys_bpl;
917 phba->sli4_hba.lpfc_scsi_psb_array[index] = psb;
918 if (non_sequential_xri) {
919 status = lpfc_sli4_post_sgl(phba, pdma_phys_bpl,
920 pdma_phys_bpl1,
921 psb->cur_iocbq.sli4_xritag);
922 if (status) {
923 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500924 psb->exch_busy = 1;
James Smart341af102010-01-26 23:07:37 -0500925 } else {
926 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400927 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500928 }
James Smartda0436e2009-05-22 14:51:39 -0400929 /* Put it back into the SCSI buffer list */
930 lpfc_release_scsi_buf_s4(phba, psb);
931 break;
932 }
933 }
934 if (bcnt) {
James Smart6d368e52011-05-24 11:44:12 -0400935 if (!phba->sli4_hba.extents_in_use)
936 status = lpfc_sli4_post_scsi_sgl_block(phba,
937 &sblist,
938 bcnt);
939 else
940 status = lpfc_sli4_post_scsi_sgl_blk_ext(phba,
941 &sblist,
942 bcnt);
943
944 if (status) {
945 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
946 "3021 SCSI SGL post error %d\n",
947 status);
948 bcnt = 0;
949 }
James Smartda0436e2009-05-22 14:51:39 -0400950 /* Reset SCSI buffer count for next round of posting */
951 while (!list_empty(&sblist)) {
952 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
953 list);
954 if (status) {
955 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500956 psb->exch_busy = 1;
James Smart341af102010-01-26 23:07:37 -0500957 } else {
958 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400959 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500960 }
James Smartda0436e2009-05-22 14:51:39 -0400961 /* Put it back into the SCSI buffer list */
962 lpfc_release_scsi_buf_s4(phba, psb);
963 }
964 }
965
James Smartd7c47992010-06-08 18:31:54 -0400966 return bcnt + non_sequential_xri;
James Smartda0436e2009-05-22 14:51:39 -0400967}
968
969/**
James Smart3772a992009-05-22 14:50:54 -0400970 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
971 * @vport: The virtual port for which this call being executed.
972 * @num_to_allocate: The requested number of buffers to allocate.
973 *
974 * This routine wraps the actual SCSI buffer allocator function pointer from
975 * the lpfc_hba struct.
976 *
977 * Return codes:
978 * int - number of scsi buffers that were allocated.
979 * 0 = failure, less than num_to_alloc is a partial failure.
980 **/
981static inline int
982lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
983{
984 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
985}
986
987/**
James Smart19ca7602010-11-20 23:11:55 -0500988 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -0400989 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -0500990 *
991 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
992 * and returns to caller.
993 *
994 * Return codes:
995 * NULL - Error
996 * Pointer to lpfc_scsi_buf - Success
997 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +0100998static struct lpfc_scsi_buf*
James Smart19ca7602010-11-20 23:11:55 -0500999lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001000{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001001 struct lpfc_scsi_buf * lpfc_cmd = NULL;
1002 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001003 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001004
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001005 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001006 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -04001007 if (lpfc_cmd) {
1008 lpfc_cmd->seg_cnt = 0;
1009 lpfc_cmd->nonsg_phys = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001010 lpfc_cmd->prot_seg_cnt = 0;
James Smart1dcb58e2007-04-25 09:51:30 -04001011 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001012 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001013 return lpfc_cmd;
1014}
James Smart19ca7602010-11-20 23:11:55 -05001015/**
1016 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1017 * @phba: The HBA for which this call is being executed.
1018 *
1019 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1020 * and returns to caller.
1021 *
1022 * Return codes:
1023 * NULL - Error
1024 * Pointer to lpfc_scsi_buf - Success
1025 **/
1026static struct lpfc_scsi_buf*
1027lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1028{
James Smart1151e3e2011-02-16 12:39:35 -05001029 struct lpfc_scsi_buf *lpfc_cmd ;
James Smart19ca7602010-11-20 23:11:55 -05001030 unsigned long iflag = 0;
1031 int found = 0;
1032
1033 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -05001034 list_for_each_entry(lpfc_cmd, &phba->lpfc_scsi_buf_list,
1035 list) {
James Smart19ca7602010-11-20 23:11:55 -05001036 if (lpfc_test_rrq_active(phba, ndlp,
James Smart1151e3e2011-02-16 12:39:35 -05001037 lpfc_cmd->cur_iocbq.sli4_xritag))
1038 continue;
1039 list_del(&lpfc_cmd->list);
James Smart19ca7602010-11-20 23:11:55 -05001040 found = 1;
1041 lpfc_cmd->seg_cnt = 0;
1042 lpfc_cmd->nonsg_phys = 0;
1043 lpfc_cmd->prot_seg_cnt = 0;
James Smart1151e3e2011-02-16 12:39:35 -05001044 break;
James Smart19ca7602010-11-20 23:11:55 -05001045 }
James Smart1151e3e2011-02-16 12:39:35 -05001046 spin_unlock_irqrestore(&phba->scsi_buf_list_lock,
1047 iflag);
1048 if (!found)
1049 return NULL;
1050 else
1051 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001052}
1053/**
1054 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1055 * @phba: The HBA for which this call is being executed.
1056 *
1057 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1058 * and returns to caller.
1059 *
1060 * Return codes:
1061 * NULL - Error
1062 * Pointer to lpfc_scsi_buf - Success
1063 **/
1064static struct lpfc_scsi_buf*
1065lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1066{
1067 return phba->lpfc_get_scsi_buf(phba, ndlp);
1068}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001069
James Smart9bad7672008-12-04 22:39:02 -05001070/**
James Smart3772a992009-05-22 14:50:54 -04001071 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -05001072 * @phba: The Hba for which this call is being executed.
1073 * @psb: The scsi buffer which is being released.
1074 *
1075 * This routine releases @psb scsi buffer by adding it to tail of @phba
1076 * lpfc_scsi_buf_list list.
1077 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001078static void
James Smart3772a992009-05-22 14:50:54 -04001079lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001080{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001081 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001082
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001083 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001084 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -05001085 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001086 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -05001087}
1088
James Smart9bad7672008-12-04 22:39:02 -05001089/**
James Smartda0436e2009-05-22 14:51:39 -04001090 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1091 * @phba: The Hba for which this call is being executed.
1092 * @psb: The scsi buffer which is being released.
1093 *
1094 * This routine releases @psb scsi buffer by adding it to tail of @phba
1095 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1096 * and cannot be reused for at least RA_TOV amount of time if it was
1097 * aborted.
1098 **/
1099static void
1100lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1101{
1102 unsigned long iflag = 0;
1103
James Smart341af102010-01-26 23:07:37 -05001104 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -04001105 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1106 iflag);
1107 psb->pCmd = NULL;
1108 list_add_tail(&psb->list,
1109 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1110 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1111 iflag);
1112 } else {
1113
1114 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1115 psb->pCmd = NULL;
1116 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
1117 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
1118 }
1119}
1120
1121/**
James Smart3772a992009-05-22 14:50:54 -04001122 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1123 * @phba: The Hba for which this call is being executed.
1124 * @psb: The scsi buffer which is being released.
1125 *
1126 * This routine releases @psb scsi buffer by adding it to tail of @phba
1127 * lpfc_scsi_buf_list list.
1128 **/
1129static void
1130lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1131{
1132
1133 phba->lpfc_release_scsi_buf(phba, psb);
1134}
1135
1136/**
1137 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001138 * @phba: The Hba for which this call is being executed.
1139 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1140 *
1141 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001142 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1143 * through sg elements and format the bdea. This routine also initializes all
1144 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001145 *
1146 * Return codes:
1147 * 1 - Error
1148 * 0 - Success
1149 **/
dea31012005-04-17 16:05:31 -05001150static int
James Smart3772a992009-05-22 14:50:54 -04001151lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001152{
1153 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1154 struct scatterlist *sgel = NULL;
1155 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1156 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001157 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001158 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001159 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001160 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001161 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001162 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001163
1164 /*
1165 * There are three possibilities here - use scatter-gather segment, use
1166 * the single mapping, or neither. Start the lpfc command prep by
1167 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1168 * data bde entry.
1169 */
1170 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001171 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001172 /*
1173 * The driver stores the segment count returned from pci_map_sg
1174 * because this a count of dma-mappings used to map the use_sg
1175 * pages. They are not guaranteed to be the same for those
1176 * architectures that implement an IOMMU.
1177 */
dea31012005-04-17 16:05:31 -05001178
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001179 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1180 scsi_sg_count(scsi_cmnd), datadir);
1181 if (unlikely(!nseg))
1182 return 1;
1183
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001184 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001185 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001186 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1187 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001188 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001189 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001190 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001191 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001192 return 1;
1193 }
1194
1195 /*
1196 * The driver established a maximum scatter-gather segment count
1197 * during probe that limits the number of sg elements in any
1198 * single scsi command. Just run through the seg_cnt and format
1199 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001200 * When using SLI-3 the driver will try to fit all the BDEs into
1201 * the IOCB. If it can't then the BDEs get added to a BPL as it
1202 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001203 */
James Smart34b02dc2008-08-24 21:49:55 -04001204 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001205 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001206 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001207 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001208 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001209 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1210 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1211 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1212 data_bde->addrLow = putPaddrLow(physaddr);
1213 data_bde->addrHigh = putPaddrHigh(physaddr);
1214 data_bde++;
1215 } else {
1216 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1217 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1218 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1219 bpl->addrLow =
1220 le32_to_cpu(putPaddrLow(physaddr));
1221 bpl->addrHigh =
1222 le32_to_cpu(putPaddrHigh(physaddr));
1223 bpl++;
1224 }
dea31012005-04-17 16:05:31 -05001225 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001226 }
dea31012005-04-17 16:05:31 -05001227
1228 /*
1229 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001230 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1231 * explicitly reinitialized and for SLI-3 the extended bde count is
1232 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001233 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001234 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001235 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1236 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001237 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1238 /*
1239 * The extended IOCB format can only fit 3 BDE or a BPL.
1240 * This I/O has more than 3 BDE so the 1st data bde will
1241 * be a BPL that is filled in here.
1242 */
1243 physaddr = lpfc_cmd->dma_handle;
1244 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1245 data_bde->tus.f.bdeSize = (num_bde *
1246 sizeof(struct ulp_bde64));
1247 physaddr += (sizeof(struct fcp_cmnd) +
1248 sizeof(struct fcp_rsp) +
1249 (2 * sizeof(struct ulp_bde64)));
1250 data_bde->addrHigh = putPaddrHigh(physaddr);
1251 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001252 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -04001253 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1254 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001255 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -04001256 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1257 }
1258 } else {
1259 iocb_cmd->un.fcpi64.bdl.bdeSize =
1260 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001261 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001262 }
James Smart09372822008-01-11 01:52:54 -05001263 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001264
1265 /*
1266 * Due to difference in data length between DIF/non-DIF paths,
1267 * we need to set word 4 of IOCB here
1268 */
James Smarta257bf92009-04-06 18:48:10 -04001269 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001270 return 0;
1271}
1272
James Smartf9bb2da2011-10-10 21:34:11 -04001273static inline unsigned
1274lpfc_cmd_blksize(struct scsi_cmnd *sc)
1275{
1276 return sc->device->sector_size;
1277}
1278
1279#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1280/*
1281 * Given a scsi cmnd, determine the BlockGuard tags to be used with it
1282 * @sc: The SCSI command to examine
1283 * @reftag: (out) BlockGuard reference tag for transmitted data
1284 * @apptag: (out) BlockGuard application tag for transmitted data
1285 * @new_guard (in) Value to replace CRC with if needed
1286 *
1287 * Returns (1) if error injection was performed, (0) otherwise
1288 */
1289static int
1290lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1291 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1292{
1293 struct scatterlist *sgpe; /* s/g prot entry */
1294 struct scatterlist *sgde; /* s/g data entry */
1295 struct scsi_dif_tuple *src;
1296 uint32_t op = scsi_get_prot_op(sc);
1297 uint32_t blksize;
1298 uint32_t numblks;
1299 sector_t lba;
1300 int rc = 0;
1301
1302 if (op == SCSI_PROT_NORMAL)
1303 return 0;
1304
1305 lba = scsi_get_lba(sc);
1306 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1307 blksize = lpfc_cmd_blksize(sc);
1308 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1309
1310 /* Make sure we have the right LBA if one is specified */
1311 if ((phba->lpfc_injerr_lba < lba) ||
1312 (phba->lpfc_injerr_lba >= (lba + numblks)))
1313 return 0;
1314 }
1315
1316 sgpe = scsi_prot_sglist(sc);
1317 sgde = scsi_sglist(sc);
1318
1319 /* Should we change the Reference Tag */
1320 if (reftag) {
1321 /*
1322 * If we are SCSI_PROT_WRITE_STRIP, the protection data is
1323 * being stripped from the wire, thus it doesn't matter.
1324 */
1325 if ((op == SCSI_PROT_WRITE_PASS) ||
1326 (op == SCSI_PROT_WRITE_INSERT)) {
1327 if (phba->lpfc_injerr_wref_cnt) {
1328
1329 /* DEADBEEF will be the reftag on the wire */
1330 *reftag = 0xDEADBEEF;
1331 phba->lpfc_injerr_wref_cnt--;
1332 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1333 rc = 1;
1334
1335 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1336 "9081 BLKGRD: Injecting reftag error: "
1337 "write lba x%lx\n", (unsigned long)lba);
1338 }
1339 } else {
1340 if (phba->lpfc_injerr_rref_cnt) {
1341 *reftag = 0xDEADBEEF;
1342 phba->lpfc_injerr_rref_cnt--;
1343 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1344 rc = 1;
1345
1346 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1347 "9076 BLKGRD: Injecting reftag error: "
1348 "read lba x%lx\n", (unsigned long)lba);
1349 }
1350 }
1351 }
1352
1353 /* Should we change the Application Tag */
1354 if (apptag) {
1355 /*
1356 * If we are SCSI_PROT_WRITE_STRIP, the protection data is
1357 * being stripped from the wire, thus it doesn't matter.
1358 */
1359 if ((op == SCSI_PROT_WRITE_PASS) ||
1360 (op == SCSI_PROT_WRITE_INSERT)) {
1361 if (phba->lpfc_injerr_wapp_cnt) {
1362
1363 /* DEAD will be the apptag on the wire */
1364 *apptag = 0xDEAD;
1365 phba->lpfc_injerr_wapp_cnt--;
1366 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1367 rc = 1;
1368
1369 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1370 "9077 BLKGRD: Injecting apptag error: "
1371 "write lba x%lx\n", (unsigned long)lba);
1372 }
1373 } else {
1374 if (phba->lpfc_injerr_rapp_cnt) {
1375 *apptag = 0xDEAD;
1376 phba->lpfc_injerr_rapp_cnt--;
1377 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1378 rc = 1;
1379
1380 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1381 "9078 BLKGRD: Injecting apptag error: "
1382 "read lba x%lx\n", (unsigned long)lba);
1383 }
1384 }
1385 }
1386
1387 /* Should we change the Guard Tag */
1388
1389 /*
1390 * If we are SCSI_PROT_WRITE_INSERT, the protection data is
1391 * being on the wire is being fully generated on the HBA.
1392 * The host cannot change it or force an error.
1393 */
1394 if (((op == SCSI_PROT_WRITE_STRIP) ||
1395 (op == SCSI_PROT_WRITE_PASS)) &&
1396 phba->lpfc_injerr_wgrd_cnt) {
1397 if (sgpe) {
1398 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1399 /*
1400 * Just inject an error in the first
1401 * prot block.
1402 */
1403 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1404 "9079 BLKGRD: Injecting guard error: "
1405 "write lba x%lx oldGuard x%x refTag x%x\n",
1406 (unsigned long)lba, src->guard_tag,
1407 src->ref_tag);
1408
1409 src->guard_tag = (uint16_t)new_guard;
1410 phba->lpfc_injerr_wgrd_cnt--;
1411 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1412 rc = 1;
1413
1414 } else {
1415 blksize = lpfc_cmd_blksize(sc);
1416 /*
1417 * Jump past the first data block
1418 * and inject an error in the
1419 * prot data. The prot data is already
1420 * embedded after the regular data.
1421 */
1422 src = (struct scsi_dif_tuple *)
1423 (sg_virt(sgde) + blksize);
1424
1425 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1426 "9080 BLKGRD: Injecting guard error: "
1427 "write lba x%lx oldGuard x%x refTag x%x\n",
1428 (unsigned long)lba, src->guard_tag,
1429 src->ref_tag);
1430
1431 src->guard_tag = (uint16_t)new_guard;
1432 phba->lpfc_injerr_wgrd_cnt--;
1433 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1434 rc = 1;
1435 }
1436 }
1437 return rc;
1438}
1439#endif
1440
James Smarte2a0a9d2008-12-04 22:40:02 -05001441/*
James Smart6c8eea52010-04-06 14:49:53 -04001442 * Given a scsi cmnd, determine the BlockGuard opcodes to be used with it
1443 * @sc: The SCSI command to examine
1444 * @txopt: (out) BlockGuard operation for transmitted data
1445 * @rxopt: (out) BlockGuard operation for received data
1446 *
1447 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1448 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001449 */
1450static int
James Smart6c8eea52010-04-06 14:49:53 -04001451lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1452 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001453{
1454 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
James Smart6c8eea52010-04-06 14:49:53 -04001455 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001456
1457 if (guard_type == SHOST_DIX_GUARD_IP) {
1458 switch (scsi_get_prot_op(sc)) {
1459 case SCSI_PROT_READ_INSERT:
1460 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001461 *txop = BG_OP_IN_CSUM_OUT_NODIF;
1462 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smarte2a0a9d2008-12-04 22:40:02 -05001463 break;
1464
1465 case SCSI_PROT_READ_STRIP:
1466 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001467 *txop = BG_OP_IN_NODIF_OUT_CRC;
1468 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001469 break;
1470
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001471 case SCSI_PROT_READ_PASS:
1472 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001473 *txop = BG_OP_IN_CSUM_OUT_CRC;
1474 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smarte2a0a9d2008-12-04 22:40:02 -05001475 break;
1476
James Smarte2a0a9d2008-12-04 22:40:02 -05001477 case SCSI_PROT_NORMAL:
1478 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001479 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001480 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1481 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001482 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001483 break;
1484
1485 }
James Smart7c56b9f2011-07-22 18:36:25 -04001486 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001487 switch (scsi_get_prot_op(sc)) {
1488 case SCSI_PROT_READ_STRIP:
1489 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001490 *txop = BG_OP_IN_NODIF_OUT_CRC;
1491 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001492 break;
1493
1494 case SCSI_PROT_READ_PASS:
1495 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001496 *txop = BG_OP_IN_CRC_OUT_CRC;
1497 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001498 break;
1499
James Smarte2a0a9d2008-12-04 22:40:02 -05001500 case SCSI_PROT_READ_INSERT:
1501 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001502 *txop = BG_OP_IN_CRC_OUT_NODIF;
1503 *rxop = BG_OP_IN_NODIF_OUT_CRC;
1504 break;
1505
James Smarte2a0a9d2008-12-04 22:40:02 -05001506 case SCSI_PROT_NORMAL:
1507 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001508 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001509 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1510 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001511 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001512 break;
1513 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001514 }
1515
James Smart6c8eea52010-04-06 14:49:53 -04001516 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001517}
1518
James Smarte2a0a9d2008-12-04 22:40:02 -05001519/*
1520 * This function sets up buffer list for protection groups of
1521 * type LPFC_PG_TYPE_NO_DIF
1522 *
1523 * This is usually used when the HBA is instructed to generate
1524 * DIFs and insert them into data stream (or strip DIF from
1525 * incoming data stream)
1526 *
1527 * The buffer list consists of just one protection group described
1528 * below:
1529 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001530 * start of prot group --> | PDE_5 |
1531 * +-------------------------+
1532 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001533 * +-------------------------+
1534 * | Data BDE |
1535 * +-------------------------+
1536 * |more Data BDE's ... (opt)|
1537 * +-------------------------+
1538 *
1539 * @sc: pointer to scsi command we're working on
1540 * @bpl: pointer to buffer list for protection groups
1541 * @datacnt: number of segments of data that have been dma mapped
1542 *
1543 * Note: Data s/g buffers have been dma mapped
1544 */
1545static int
1546lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1547 struct ulp_bde64 *bpl, int datasegcnt)
1548{
1549 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001550 struct lpfc_pde5 *pde5 = NULL;
1551 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001552 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001553 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001554 int datadir = sc->sc_data_direction;
James Smarte2a0a9d2008-12-04 22:40:02 -05001555 uint32_t reftag;
James Smart7c56b9f2011-07-22 18:36:25 -04001556 unsigned blksize;
James Smart6c8eea52010-04-06 14:49:53 -04001557 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001558
James Smart6c8eea52010-04-06 14:49:53 -04001559 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1560 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001561 goto out;
1562
James Smart6c8eea52010-04-06 14:49:53 -04001563 /* extract some info from the scsi command for pde*/
James Smarte2a0a9d2008-12-04 22:40:02 -05001564 blksize = lpfc_cmd_blksize(sc);
James Smart7c56b9f2011-07-22 18:36:25 -04001565 reftag = scsi_get_lba(sc) & 0xffffffff;
James Smarte2a0a9d2008-12-04 22:40:02 -05001566
James Smartf9bb2da2011-10-10 21:34:11 -04001567#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1568 /* reftag is the only error we can inject here */
1569 lpfc_bg_err_inject(phba, sc, &reftag, 0, 0);
1570#endif
1571
James Smart6c8eea52010-04-06 14:49:53 -04001572 /* setup PDE5 with what we have */
1573 pde5 = (struct lpfc_pde5 *) bpl;
1574 memset(pde5, 0, sizeof(struct lpfc_pde5));
1575 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001576
James Smartbc739052010-08-04 16:11:18 -04001577 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001578 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001579 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001580
James Smart6c8eea52010-04-06 14:49:53 -04001581 /* advance bpl and increment bde count */
1582 num_bde++;
1583 bpl++;
1584 pde6 = (struct lpfc_pde6 *) bpl;
1585
1586 /* setup PDE6 with the rest of the info */
1587 memset(pde6, 0, sizeof(struct lpfc_pde6));
1588 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1589 bf_set(pde6_optx, pde6, txop);
1590 bf_set(pde6_oprx, pde6, rxop);
1591 if (datadir == DMA_FROM_DEVICE) {
1592 bf_set(pde6_ce, pde6, 1);
1593 bf_set(pde6_re, pde6, 1);
James Smart6c8eea52010-04-06 14:49:53 -04001594 }
1595 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001596 bf_set(pde6_ae, pde6, 0);
1597 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001598
James Smartbc739052010-08-04 16:11:18 -04001599 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001600 pde6->word0 = cpu_to_le32(pde6->word0);
1601 pde6->word1 = cpu_to_le32(pde6->word1);
1602 pde6->word2 = cpu_to_le32(pde6->word2);
1603
James Smart6c8eea52010-04-06 14:49:53 -04001604 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001605 num_bde++;
1606 bpl++;
1607
1608 /* assumption: caller has already run dma_map_sg on command data */
1609 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1610 physaddr = sg_dma_address(sgde);
1611 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1612 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1613 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1614 if (datadir == DMA_TO_DEVICE)
1615 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1616 else
1617 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1618 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1619 bpl++;
1620 num_bde++;
1621 }
1622
1623out:
1624 return num_bde;
1625}
1626
1627/*
1628 * This function sets up buffer list for protection groups of
1629 * type LPFC_PG_TYPE_DIF_BUF
1630 *
1631 * This is usually used when DIFs are in their own buffers,
1632 * separate from the data. The HBA can then by instructed
1633 * to place the DIFs in the outgoing stream. For read operations,
1634 * The HBA could extract the DIFs and place it in DIF buffers.
1635 *
1636 * The buffer list for this type consists of one or more of the
1637 * protection groups described below:
1638 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001639 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001640 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001641 * | PDE_6 |
1642 * +-------------------------+
1643 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05001644 * +-------------------------+
1645 * | Data BDE |
1646 * +-------------------------+
1647 * |more Data BDE's ... (opt)|
1648 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001649 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001650 * +-------------------------+
1651 * | ... |
1652 * +-------------------------+
1653 *
1654 * @sc: pointer to scsi command we're working on
1655 * @bpl: pointer to buffer list for protection groups
1656 * @datacnt: number of segments of data that have been dma mapped
1657 * @protcnt: number of segment of protection data that have been dma mapped
1658 *
1659 * Note: It is assumed that both data and protection s/g buffers have been
1660 * mapped for DMA
1661 */
1662static int
1663lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1664 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1665{
1666 struct scatterlist *sgde = NULL; /* s/g data entry */
1667 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04001668 struct lpfc_pde5 *pde5 = NULL;
1669 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05001670 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001671 dma_addr_t dataphysaddr, protphysaddr;
1672 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05001673 unsigned int split_offset;
1674 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05001675 unsigned int protgrp_blks, protgrp_bytes;
1676 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04001677 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001678 int datadir = sc->sc_data_direction;
1679 unsigned char pgdone = 0, alldone = 0;
1680 unsigned blksize;
1681 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04001682 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001683 int num_bde = 0;
1684
1685 sgpe = scsi_prot_sglist(sc);
1686 sgde = scsi_sglist(sc);
1687
1688 if (!sgpe || !sgde) {
1689 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1690 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
1691 sgpe, sgde);
1692 return 0;
1693 }
1694
James Smart6c8eea52010-04-06 14:49:53 -04001695 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1696 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001697 goto out;
1698
James Smart6c8eea52010-04-06 14:49:53 -04001699 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05001700 blksize = lpfc_cmd_blksize(sc);
James Smart7c56b9f2011-07-22 18:36:25 -04001701 reftag = scsi_get_lba(sc) & 0xffffffff;
James Smarte2a0a9d2008-12-04 22:40:02 -05001702
James Smartf9bb2da2011-10-10 21:34:11 -04001703#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1704 /* reftag / guard tag are the only errors we can inject here */
1705 lpfc_bg_err_inject(phba, sc, &reftag, 0, 0xDEAD);
1706#endif
1707
James Smarte2a0a9d2008-12-04 22:40:02 -05001708 split_offset = 0;
1709 do {
James Smart6c8eea52010-04-06 14:49:53 -04001710 /* setup PDE5 with what we have */
1711 pde5 = (struct lpfc_pde5 *) bpl;
1712 memset(pde5, 0, sizeof(struct lpfc_pde5));
1713 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001714
James Smartbc739052010-08-04 16:11:18 -04001715 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001716 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001717 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001718
James Smart6c8eea52010-04-06 14:49:53 -04001719 /* advance bpl and increment bde count */
1720 num_bde++;
1721 bpl++;
1722 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05001723
James Smart6c8eea52010-04-06 14:49:53 -04001724 /* setup PDE6 with the rest of the info */
1725 memset(pde6, 0, sizeof(struct lpfc_pde6));
1726 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1727 bf_set(pde6_optx, pde6, txop);
1728 bf_set(pde6_oprx, pde6, rxop);
1729 bf_set(pde6_ce, pde6, 1);
1730 bf_set(pde6_re, pde6, 1);
James Smart6c8eea52010-04-06 14:49:53 -04001731 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001732 bf_set(pde6_ae, pde6, 0);
1733 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001734
James Smartbc739052010-08-04 16:11:18 -04001735 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001736 pde6->word0 = cpu_to_le32(pde6->word0);
1737 pde6->word1 = cpu_to_le32(pde6->word1);
1738 pde6->word2 = cpu_to_le32(pde6->word2);
1739
James Smart6c8eea52010-04-06 14:49:53 -04001740 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001741 num_bde++;
1742 bpl++;
1743
1744 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05001745 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
1746 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05001747
James Smarte2a0a9d2008-12-04 22:40:02 -05001748 /* must be integer multiple of the DIF block length */
1749 BUG_ON(protgroup_len % 8);
1750
James Smart7f860592011-03-11 16:05:52 -05001751 pde7 = (struct lpfc_pde7 *) bpl;
1752 memset(pde7, 0, sizeof(struct lpfc_pde7));
1753 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
1754
James Smart7c56b9f2011-07-22 18:36:25 -04001755 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1756 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05001757
James Smarte2a0a9d2008-12-04 22:40:02 -05001758 protgrp_blks = protgroup_len / 8;
1759 protgrp_bytes = protgrp_blks * blksize;
1760
James Smart7f860592011-03-11 16:05:52 -05001761 /* check if this pde is crossing the 4K boundary; if so split */
1762 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
1763 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
1764 protgroup_offset += protgroup_remainder;
1765 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04001766 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05001767 } else {
1768 protgroup_offset = 0;
1769 curr_prot++;
1770 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001771
James Smarte2a0a9d2008-12-04 22:40:02 -05001772 num_bde++;
1773
1774 /* setup BDE's for data blocks associated with DIF data */
1775 pgdone = 0;
1776 subtotal = 0; /* total bytes processed for current prot grp */
1777 while (!pgdone) {
1778 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04001779 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1780 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001781 __func__);
1782 return 0;
1783 }
1784 bpl++;
1785 dataphysaddr = sg_dma_address(sgde) + split_offset;
1786 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1787 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1788
1789 remainder = sg_dma_len(sgde) - split_offset;
1790
1791 if ((subtotal + remainder) <= protgrp_bytes) {
1792 /* we can use this whole buffer */
1793 bpl->tus.f.bdeSize = remainder;
1794 split_offset = 0;
1795
1796 if ((subtotal + remainder) == protgrp_bytes)
1797 pgdone = 1;
1798 } else {
1799 /* must split this buffer with next prot grp */
1800 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1801 split_offset += bpl->tus.f.bdeSize;
1802 }
1803
1804 subtotal += bpl->tus.f.bdeSize;
1805
1806 if (datadir == DMA_TO_DEVICE)
1807 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1808 else
1809 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1810 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1811
1812 num_bde++;
1813 curr_data++;
1814
1815 if (split_offset)
1816 break;
1817
1818 /* Move to the next s/g segment if possible */
1819 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04001820
James Smarte2a0a9d2008-12-04 22:40:02 -05001821 }
1822
James Smart7f860592011-03-11 16:05:52 -05001823 if (protgroup_offset) {
1824 /* update the reference tag */
1825 reftag += protgrp_blks;
1826 bpl++;
1827 continue;
1828 }
1829
James Smarte2a0a9d2008-12-04 22:40:02 -05001830 /* are we done ? */
1831 if (curr_prot == protcnt) {
1832 alldone = 1;
1833 } else if (curr_prot < protcnt) {
1834 /* advance to next prot buffer */
1835 sgpe = sg_next(sgpe);
1836 bpl++;
1837
1838 /* update the reference tag */
1839 reftag += protgrp_blks;
1840 } else {
1841 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04001842 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1843 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05001844 }
1845
1846 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05001847out:
1848
James Smarte2a0a9d2008-12-04 22:40:02 -05001849 return num_bde;
1850}
James Smart7f860592011-03-11 16:05:52 -05001851
James Smarte2a0a9d2008-12-04 22:40:02 -05001852/*
1853 * Given a SCSI command that supports DIF, determine composition of protection
1854 * groups involved in setting up buffer lists
1855 *
1856 * Returns:
1857 * for DIF (for both read and write)
1858 * */
1859static int
1860lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
1861{
1862 int ret = LPFC_PG_TYPE_INVALID;
1863 unsigned char op = scsi_get_prot_op(sc);
1864
1865 switch (op) {
1866 case SCSI_PROT_READ_STRIP:
1867 case SCSI_PROT_WRITE_INSERT:
1868 ret = LPFC_PG_TYPE_NO_DIF;
1869 break;
1870 case SCSI_PROT_READ_INSERT:
1871 case SCSI_PROT_WRITE_STRIP:
1872 case SCSI_PROT_READ_PASS:
1873 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05001874 ret = LPFC_PG_TYPE_DIF_BUF;
1875 break;
1876 default:
1877 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1878 "9021 Unsupported protection op:%d\n", op);
1879 break;
1880 }
1881
1882 return ret;
1883}
1884
1885/*
1886 * This is the protection/DIF aware version of
1887 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
1888 * two functions eventually, but for now, it's here
1889 */
1890static int
1891lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba,
1892 struct lpfc_scsi_buf *lpfc_cmd)
1893{
1894 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1895 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1896 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1897 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1898 uint32_t num_bde = 0;
1899 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
1900 int prot_group_type = 0;
1901 int diflen, fcpdl;
1902 unsigned blksize;
1903
1904 /*
1905 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
1906 * fcp_rsp regions to the first data bde entry
1907 */
1908 bpl += 2;
1909 if (scsi_sg_count(scsi_cmnd)) {
1910 /*
1911 * The driver stores the segment count returned from pci_map_sg
1912 * because this a count of dma-mappings used to map the use_sg
1913 * pages. They are not guaranteed to be the same for those
1914 * architectures that implement an IOMMU.
1915 */
1916 datasegcnt = dma_map_sg(&phba->pcidev->dev,
1917 scsi_sglist(scsi_cmnd),
1918 scsi_sg_count(scsi_cmnd), datadir);
1919 if (unlikely(!datasegcnt))
1920 return 1;
1921
1922 lpfc_cmd->seg_cnt = datasegcnt;
1923 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001924 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1925 "9067 BLKGRD: %s: Too many sg segments"
1926 " from dma_map_sg. Config %d, seg_cnt"
1927 " %d\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001928 __func__, phba->cfg_sg_seg_cnt,
1929 lpfc_cmd->seg_cnt);
1930 scsi_dma_unmap(scsi_cmnd);
1931 return 1;
1932 }
1933
1934 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
1935
1936 switch (prot_group_type) {
1937 case LPFC_PG_TYPE_NO_DIF:
1938 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
1939 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05001940 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05001941 if (num_bde < 2)
1942 goto err;
1943 break;
1944 case LPFC_PG_TYPE_DIF_BUF:{
1945 /*
1946 * This type indicates that protection buffers are
1947 * passed to the driver, so that needs to be prepared
1948 * for DMA
1949 */
1950 protsegcnt = dma_map_sg(&phba->pcidev->dev,
1951 scsi_prot_sglist(scsi_cmnd),
1952 scsi_prot_sg_count(scsi_cmnd), datadir);
1953 if (unlikely(!protsegcnt)) {
1954 scsi_dma_unmap(scsi_cmnd);
1955 return 1;
1956 }
1957
1958 lpfc_cmd->prot_seg_cnt = protsegcnt;
1959 if (lpfc_cmd->prot_seg_cnt
1960 > phba->cfg_prot_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001961 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1962 "9068 BLKGRD: %s: Too many prot sg "
1963 "segments from dma_map_sg. Config %d,"
James Smarte2a0a9d2008-12-04 22:40:02 -05001964 "prot_seg_cnt %d\n", __func__,
1965 phba->cfg_prot_sg_seg_cnt,
1966 lpfc_cmd->prot_seg_cnt);
1967 dma_unmap_sg(&phba->pcidev->dev,
1968 scsi_prot_sglist(scsi_cmnd),
1969 scsi_prot_sg_count(scsi_cmnd),
1970 datadir);
1971 scsi_dma_unmap(scsi_cmnd);
1972 return 1;
1973 }
1974
1975 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
1976 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05001977 /* we should have 3 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05001978 if (num_bde < 3)
1979 goto err;
1980 break;
1981 }
1982 case LPFC_PG_TYPE_INVALID:
1983 default:
1984 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1985 "9022 Unexpected protection group %i\n",
1986 prot_group_type);
1987 return 1;
1988 }
1989 }
1990
1991 /*
1992 * Finish initializing those IOCB fields that are dependent on the
1993 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
1994 * reinitialized since all iocb memory resources are used many times
1995 * for transmit, receive, and continuation bpl's.
1996 */
1997 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
1998 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
1999 iocb_cmd->ulpBdeCount = 1;
2000 iocb_cmd->ulpLe = 1;
2001
2002 fcpdl = scsi_bufflen(scsi_cmnd);
2003
2004 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
2005 /*
2006 * We are in DIF Type 1 mode
2007 * Every data block has a 8 byte DIF (trailer)
2008 * attached to it. Must ajust FCP data length
2009 */
2010 blksize = lpfc_cmd_blksize(scsi_cmnd);
2011 diflen = (fcpdl / blksize) * 8;
2012 fcpdl += diflen;
2013 }
2014 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2015
2016 /*
2017 * Due to difference in data length between DIF/non-DIF paths,
2018 * we need to set word 4 of IOCB here
2019 */
2020 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2021
2022 return 0;
2023err:
2024 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2025 "9023 Could not setup all needed BDE's"
2026 "prot_group_type=%d, num_bde=%d\n",
2027 prot_group_type, num_bde);
2028 return 1;
2029}
2030
2031/*
2032 * This function checks for BlockGuard errors detected by
2033 * the HBA. In case of errors, the ASC/ASCQ fields in the
2034 * sense buffer will be set accordingly, paired with
2035 * ILLEGAL_REQUEST to signal to the kernel that the HBA
2036 * detected corruption.
2037 *
2038 * Returns:
2039 * 0 - No error found
2040 * 1 - BlockGuard error found
2041 * -1 - Internal error (bad profile, ...etc)
2042 */
2043static int
2044lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
2045 struct lpfc_iocbq *pIocbOut)
2046{
2047 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2048 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
2049 int ret = 0;
2050 uint32_t bghm = bgf->bghm;
2051 uint32_t bgstat = bgf->bgstat;
2052 uint64_t failing_sector = 0;
2053
James Smart6a9c52c2009-10-02 15:16:51 -04002054 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
2055 " 0x%x lba 0x%llx blk cnt 0x%x "
James Smarte2a0a9d2008-12-04 22:40:02 -05002056 "bgstat=0x%x bghm=0x%x\n",
James Smart87b5c322008-12-16 10:34:09 -05002057 cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
Tejun Heo83096eb2009-05-07 22:24:39 +09002058 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05002059
2060 spin_lock(&_dump_buf_lock);
2061 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04002062 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
2063 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002064 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04002065 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002066
2067 /* If we have a prot sgl, save the DIF buffer */
2068 if (lpfc_prot_group_type(phba, cmd) ==
2069 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04002070 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
2071 "Saving DIF for %u blocks to debugfs\n",
2072 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
2073 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002074 }
2075
2076 _dump_buf_done = 1;
2077 }
2078 spin_unlock(&_dump_buf_lock);
2079
2080 if (lpfc_bgs_get_invalid_prof(bgstat)) {
2081 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002082 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
2083 " BlockGuard profile. bgstat:0x%x\n",
2084 bgstat);
James Smarte2a0a9d2008-12-04 22:40:02 -05002085 ret = (-1);
2086 goto out;
2087 }
2088
2089 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
2090 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002091 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
2092 "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002093 bgstat);
2094 ret = (-1);
2095 goto out;
2096 }
2097
2098 if (lpfc_bgs_get_guard_err(bgstat)) {
2099 ret = 1;
2100
2101 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2102 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002103 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002104 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2105 phba->bg_guard_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002106 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2107 "9055 BLKGRD: guard_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002108 }
2109
2110 if (lpfc_bgs_get_reftag_err(bgstat)) {
2111 ret = 1;
2112
2113 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2114 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002115 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002116 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2117
2118 phba->bg_reftag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002119 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2120 "9056 BLKGRD: ref_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002121 }
2122
2123 if (lpfc_bgs_get_apptag_err(bgstat)) {
2124 ret = 1;
2125
2126 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2127 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05002128 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05002129 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2130
2131 phba->bg_apptag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04002132 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2133 "9061 BLKGRD: app_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002134 }
2135
2136 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
2137 /*
2138 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04002139 * field, and put the failing LBA in it.
2140 * This code assumes there was also a guard/app/ref tag error
2141 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05002142 */
James Smart7c56b9f2011-07-22 18:36:25 -04002143 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
2144 cmd->sense_buffer[8] = 0; /* Information descriptor type */
2145 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
2146 cmd->sense_buffer[10] = 0x80; /* Validity bit */
David Howells2344b5b2009-04-14 17:08:34 +01002147 bghm /= cmd->device->sector_size;
James Smarte2a0a9d2008-12-04 22:40:02 -05002148
2149 failing_sector = scsi_get_lba(cmd);
2150 failing_sector += bghm;
2151
James Smart7c56b9f2011-07-22 18:36:25 -04002152 /* Descriptor Information */
2153 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05002154 }
2155
2156 if (!ret) {
2157 /* No error was reported - problem in FW? */
2158 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04002159 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2160 "9057 BLKGRD: no errors reported!\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002161 }
2162
2163out:
2164 return ret;
2165}
2166
James Smartea2151b2008-09-07 11:52:10 -04002167/**
James Smartda0436e2009-05-22 14:51:39 -04002168 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
2169 * @phba: The Hba for which this call is being executed.
2170 * @lpfc_cmd: The scsi buffer which is going to be mapped.
2171 *
2172 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
2173 * field of @lpfc_cmd for device with SLI-4 interface spec.
2174 *
2175 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04002176 * 1 - Error
2177 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04002178 **/
2179static int
2180lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2181{
2182 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2183 struct scatterlist *sgel = NULL;
2184 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2185 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
James Smartfedd3b72011-02-16 12:39:24 -05002186 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04002187 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2188 dma_addr_t physaddr;
2189 uint32_t num_bde = 0;
2190 uint32_t dma_len;
2191 uint32_t dma_offset = 0;
2192 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05002193 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04002194
2195 /*
2196 * There are three possibilities here - use scatter-gather segment, use
2197 * the single mapping, or neither. Start the lpfc command prep by
2198 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2199 * data bde entry.
2200 */
2201 if (scsi_sg_count(scsi_cmnd)) {
2202 /*
2203 * The driver stores the segment count returned from pci_map_sg
2204 * because this a count of dma-mappings used to map the use_sg
2205 * pages. They are not guaranteed to be the same for those
2206 * architectures that implement an IOMMU.
2207 */
2208
2209 nseg = scsi_dma_map(scsi_cmnd);
2210 if (unlikely(!nseg))
2211 return 1;
2212 sgl += 1;
2213 /* clear the last flag in the fcp_rsp map entry */
2214 sgl->word2 = le32_to_cpu(sgl->word2);
2215 bf_set(lpfc_sli4_sge_last, sgl, 0);
2216 sgl->word2 = cpu_to_le32(sgl->word2);
2217 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05002218 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04002219 lpfc_cmd->seg_cnt = nseg;
2220 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04002221 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
2222 " %s: Too many sg segments from "
2223 "dma_map_sg. Config %d, seg_cnt %d\n",
2224 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04002225 lpfc_cmd->seg_cnt);
2226 scsi_dma_unmap(scsi_cmnd);
2227 return 1;
2228 }
2229
2230 /*
2231 * The driver established a maximum scatter-gather segment count
2232 * during probe that limits the number of sg elements in any
2233 * single scsi command. Just run through the seg_cnt and format
2234 * the sge's.
2235 * When using SLI-3 the driver will try to fit all the BDEs into
2236 * the IOCB. If it can't then the BDEs get added to a BPL as it
2237 * does for SLI-2 mode.
2238 */
2239 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
2240 physaddr = sg_dma_address(sgel);
2241 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04002242 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2243 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04002244 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04002245 if ((num_bde + 1) == nseg)
2246 bf_set(lpfc_sli4_sge_last, sgl, 1);
2247 else
2248 bf_set(lpfc_sli4_sge_last, sgl, 0);
2249 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04002250 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04002251 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05002252 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04002253 dma_offset += dma_len;
2254 sgl++;
2255 }
James Smartfedd3b72011-02-16 12:39:24 -05002256 /* setup the performance hint (first data BDE) if enabled */
2257 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
2258 bde = (struct ulp_bde64 *)
2259 &(iocb_cmd->unsli3.sli3Words[5]);
2260 bde->addrLow = first_data_sgl->addr_lo;
2261 bde->addrHigh = first_data_sgl->addr_hi;
2262 bde->tus.f.bdeSize =
2263 le32_to_cpu(first_data_sgl->sge_len);
2264 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2265 bde->tus.w = cpu_to_le32(bde->tus.w);
2266 }
James Smartda0436e2009-05-22 14:51:39 -04002267 } else {
2268 sgl += 1;
2269 /* clear the last flag in the fcp_rsp map entry */
2270 sgl->word2 = le32_to_cpu(sgl->word2);
2271 bf_set(lpfc_sli4_sge_last, sgl, 1);
2272 sgl->word2 = cpu_to_le32(sgl->word2);
2273 }
2274
2275 /*
2276 * Finish initializing those IOCB fields that are dependent on the
2277 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
2278 * explicitly reinitialized.
2279 * all iocb memory resources are reused.
2280 */
2281 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
2282
2283 /*
2284 * Due to difference in data length between DIF/non-DIF paths,
2285 * we need to set word 4 of IOCB here
2286 */
2287 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
2288 return 0;
2289}
2290
2291/**
James Smart3772a992009-05-22 14:50:54 -04002292 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
2293 * @phba: The Hba for which this call is being executed.
2294 * @lpfc_cmd: The scsi buffer which is going to be mapped.
2295 *
2296 * This routine wraps the actual DMA mapping function pointer from the
2297 * lpfc_hba struct.
2298 *
2299 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04002300 * 1 - Error
2301 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04002302 **/
2303static inline int
2304lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2305{
2306 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
2307}
2308
2309/**
James Smart3621a712009-04-06 18:47:14 -04002310 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04002311 * @phba: Pointer to hba context object.
2312 * @vport: Pointer to vport object.
2313 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
2314 * @rsp_iocb: Pointer to response iocb object which reported error.
2315 *
2316 * This function posts an event when there is a SCSI command reporting
2317 * error from the scsi device.
2318 **/
2319static void
2320lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
2321 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
2322 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2323 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
2324 uint32_t resp_info = fcprsp->rspStatus2;
2325 uint32_t scsi_status = fcprsp->rspStatus3;
2326 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
2327 struct lpfc_fast_path_event *fast_path_evt = NULL;
2328 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
2329 unsigned long flags;
2330
James Smart5989b8d2010-10-22 11:06:56 -04002331 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2332 return;
2333
James Smartea2151b2008-09-07 11:52:10 -04002334 /* If there is queuefull or busy condition send a scsi event */
2335 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
2336 (cmnd->result == SAM_STAT_BUSY)) {
2337 fast_path_evt = lpfc_alloc_fast_evt(phba);
2338 if (!fast_path_evt)
2339 return;
2340 fast_path_evt->un.scsi_evt.event_type =
2341 FC_REG_SCSI_EVENT;
2342 fast_path_evt->un.scsi_evt.subcategory =
2343 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
2344 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
2345 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
2346 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
2347 &pnode->nlp_portname, sizeof(struct lpfc_name));
2348 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
2349 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2350 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
2351 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
2352 fast_path_evt = lpfc_alloc_fast_evt(phba);
2353 if (!fast_path_evt)
2354 return;
2355 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
2356 FC_REG_SCSI_EVENT;
2357 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
2358 LPFC_EVENT_CHECK_COND;
2359 fast_path_evt->un.check_cond_evt.scsi_event.lun =
2360 cmnd->device->lun;
2361 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
2362 &pnode->nlp_portname, sizeof(struct lpfc_name));
2363 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
2364 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2365 fast_path_evt->un.check_cond_evt.sense_key =
2366 cmnd->sense_buffer[2] & 0xf;
2367 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
2368 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
2369 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2370 fcpi_parm &&
2371 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
2372 ((scsi_status == SAM_STAT_GOOD) &&
2373 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
2374 /*
2375 * If status is good or resid does not match with fcp_param and
2376 * there is valid fcpi_parm, then there is a read_check error
2377 */
2378 fast_path_evt = lpfc_alloc_fast_evt(phba);
2379 if (!fast_path_evt)
2380 return;
2381 fast_path_evt->un.read_check_error.header.event_type =
2382 FC_REG_FABRIC_EVENT;
2383 fast_path_evt->un.read_check_error.header.subcategory =
2384 LPFC_EVENT_FCPRDCHKERR;
2385 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
2386 &pnode->nlp_portname, sizeof(struct lpfc_name));
2387 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
2388 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2389 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
2390 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
2391 fast_path_evt->un.read_check_error.fcpiparam =
2392 fcpi_parm;
2393 } else
2394 return;
2395
2396 fast_path_evt->vport = vport;
2397 spin_lock_irqsave(&phba->hbalock, flags);
2398 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
2399 spin_unlock_irqrestore(&phba->hbalock, flags);
2400 lpfc_worker_wake_up(phba);
2401 return;
2402}
James Smart9bad7672008-12-04 22:39:02 -05002403
2404/**
James Smartf1126682009-06-10 17:22:44 -04002405 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04002406 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05002407 * @psb: The scsi buffer which is going to be un-mapped.
2408 *
2409 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04002410 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002411 **/
dea31012005-04-17 16:05:31 -05002412static void
James Smartf1126682009-06-10 17:22:44 -04002413lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04002414{
2415 /*
2416 * There are only two special cases to consider. (1) the scsi command
2417 * requested scatter-gather usage or (2) the scsi command allocated
2418 * a request buffer, but did not request use_sg. There is a third
2419 * case, but it does not require resource deallocation.
2420 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002421 if (psb->seg_cnt > 0)
2422 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002423 if (psb->prot_seg_cnt > 0)
2424 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
2425 scsi_prot_sg_count(psb->pCmd),
2426 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04002427}
2428
James Smart9bad7672008-12-04 22:39:02 -05002429/**
James Smart3621a712009-04-06 18:47:14 -04002430 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05002431 * @vport: The virtual port for which this call is being executed.
2432 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2433 * @rsp_iocb: The response IOCB which contains FCP error.
2434 *
2435 * This routine is called to process response IOCB with status field
2436 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
2437 * based upon SCSI and FCP error.
2438 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04002439static void
James Smart2e0fef82007-06-17 19:56:36 -05002440lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2441 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05002442{
2443 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2444 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
2445 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04002446 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05002447 uint32_t resp_info = fcprsp->rspStatus2;
2448 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05002449 uint32_t *lp;
dea31012005-04-17 16:05:31 -05002450 uint32_t host_status = DID_OK;
2451 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05002452 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05002453
James Smartea2151b2008-09-07 11:52:10 -04002454
dea31012005-04-17 16:05:31 -05002455 /*
2456 * If this is a task management command, there is no
2457 * scsi packet associated with this lpfc_cmd. The driver
2458 * consumes it.
2459 */
2460 if (fcpcmd->fcpCntl2) {
2461 scsi_status = 0;
2462 goto out;
2463 }
2464
James Smart6a9c52c2009-10-02 15:16:51 -04002465 if (resp_info & RSP_LEN_VALID) {
2466 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05002467 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04002468 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2469 "2719 Invalid response length: "
2470 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
2471 cmnd->device->id,
2472 cmnd->device->lun, cmnd->cmnd[0],
2473 rsplen);
2474 host_status = DID_ERROR;
2475 goto out;
2476 }
James Smarte40a02c2010-02-26 14:13:54 -05002477 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
2478 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2479 "2757 Protocol failure detected during "
2480 "processing of FCP I/O op: "
2481 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
2482 cmnd->device->id,
2483 cmnd->device->lun, cmnd->cmnd[0],
2484 fcprsp->rspInfo3);
2485 host_status = DID_ERROR;
2486 goto out;
2487 }
James Smart6a9c52c2009-10-02 15:16:51 -04002488 }
2489
James Smartc7743952006-12-02 13:34:42 -05002490 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
2491 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
2492 if (snslen > SCSI_SENSE_BUFFERSIZE)
2493 snslen = SCSI_SENSE_BUFFERSIZE;
2494
2495 if (resp_info & RSP_LEN_VALID)
2496 rsplen = be32_to_cpu(fcprsp->rspRspLen);
2497 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
2498 }
2499 lp = (uint32_t *)cmnd->sense_buffer;
2500
James Smart73d91e52011-10-10 21:32:10 -04002501 if (!scsi_status && (resp_info & RESID_UNDER) &&
2502 vport->cfg_log_verbose & LOG_FCP_UNDER)
2503 logit = LOG_FCP_UNDER;
James Smartc7743952006-12-02 13:34:42 -05002504
James Smarte8b62012007-08-02 11:10:09 -04002505 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05002506 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04002507 "Data: x%x x%x x%x x%x x%x\n",
2508 cmnd->cmnd[0], scsi_status,
2509 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
2510 be32_to_cpu(fcprsp->rspResId),
2511 be32_to_cpu(fcprsp->rspSnsLen),
2512 be32_to_cpu(fcprsp->rspRspLen),
2513 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05002514
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002515 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05002516 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002517 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05002518
James Smart73d91e52011-10-10 21:32:10 -04002519 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smarte2a0a9d2008-12-04 22:40:02 -05002520 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04002521 "residual %d Data: x%x x%x x%x\n",
2522 be32_to_cpu(fcpcmd->fcpDl),
2523 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
2524 cmnd->underflow);
dea31012005-04-17 16:05:31 -05002525
2526 /*
James Smart7054a602007-04-25 09:52:34 -04002527 * If there is an under run check if under run reported by
2528 * storage array is same as the under run reported by HBA.
2529 * If this is not same, there is a dropped frame.
2530 */
2531 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2532 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002533 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04002534 lpfc_printf_vlog(vport, KERN_WARNING,
2535 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05002536 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04002537 "and Underrun Data: x%x x%x x%x x%x\n",
2538 be32_to_cpu(fcpcmd->fcpDl),
2539 scsi_get_resid(cmnd), fcpi_parm,
2540 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002541 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04002542 host_status = DID_ERROR;
2543 }
2544 /*
dea31012005-04-17 16:05:31 -05002545 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002546 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05002547 * is not present, make sure the actual amount transferred is at
2548 * least the underflow value or fail.
2549 */
2550 if (!(resp_info & SNS_LEN_VALID) &&
2551 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002552 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
2553 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04002554 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002555 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04002556 "underrun converted to error "
2557 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04002558 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04002559 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05002560 host_status = DID_ERROR;
2561 }
2562 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04002563 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002564 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04002565 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04002566 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05002567 host_status = DID_ERROR;
2568
2569 /*
2570 * Check SLI validation that all the transfer was actually done
2571 * (fcpi_parm should be zero). Apply check only to reads.
2572 */
James Smarteee88772010-09-29 11:19:08 -04002573 } else if (fcpi_parm && (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -04002574 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05002575 "9029 FCP Read Check Error Data: "
James Smarteee88772010-09-29 11:19:08 -04002576 "x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002577 be32_to_cpu(fcpcmd->fcpDl),
2578 be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04002579 fcpi_parm, cmnd->cmnd[0], scsi_status);
2580 switch (scsi_status) {
2581 case SAM_STAT_GOOD:
2582 case SAM_STAT_CHECK_CONDITION:
2583 /* Fabric dropped a data frame. Fail any successful
2584 * command in which we detected dropped frames.
2585 * A status of good or some check conditions could
2586 * be considered a successful command.
2587 */
2588 host_status = DID_ERROR;
2589 break;
2590 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002591 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05002592 }
2593
2594 out:
2595 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04002596 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05002597}
2598
James Smart9bad7672008-12-04 22:39:02 -05002599/**
James Smart3621a712009-04-06 18:47:14 -04002600 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05002601 * @phba: The Hba for which this call is being executed.
2602 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04002603 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05002604 *
2605 * This routine assigns scsi command result by looking into response IOCB
2606 * status field appropriately. This routine handles QUEUE FULL condition as
2607 * well by ramping down device queue depth.
2608 **/
dea31012005-04-17 16:05:31 -05002609static void
2610lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
2611 struct lpfc_iocbq *pIocbOut)
2612{
2613 struct lpfc_scsi_buf *lpfc_cmd =
2614 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002615 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05002616 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
2617 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04002618 struct scsi_cmnd *cmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002619 int result;
James Smarta257bf92009-04-06 18:48:10 -04002620 struct scsi_device *tmp_sdev;
James Smart5ffc2662009-11-18 15:39:44 -05002621 int depth;
James Smartfa61a542008-01-11 01:52:42 -05002622 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04002623 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04002624 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04002625 uint32_t queue_depth, scsi_id;
James Smart73d91e52011-10-10 21:32:10 -04002626 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05002627
James Smart75baf692010-06-08 18:31:21 -04002628 /* Sanity check on return of outstanding command */
2629 if (!(lpfc_cmd->pCmd))
2630 return;
2631 cmd = lpfc_cmd->pCmd;
2632 shost = cmd->device->host;
2633
dea31012005-04-17 16:05:31 -05002634 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
2635 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05002636 /* pick up SLI4 exhange busy status from HBA */
2637 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
2638
James Smart109f6ed2008-12-04 22:39:08 -05002639 if (pnode && NLP_CHK_NODE_ACT(pnode))
2640 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05002641
2642 if (lpfc_cmd->status) {
2643 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
2644 (lpfc_cmd->result & IOERR_DRVR_MASK))
2645 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
2646 else if (lpfc_cmd->status >= IOSTAT_CNT)
2647 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smart73d91e52011-10-10 21:32:10 -04002648 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR
2649 && !lpfc_cmd->fcp_rsp->rspStatus3
2650 && (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER)
2651 && !(phba->cfg_log_verbose & LOG_FCP_UNDER))
2652 logit = 0;
2653 else
2654 logit = LOG_FCP | LOG_FCP_UNDER;
2655 lpfc_printf_vlog(vport, KERN_WARNING, logit,
2656 "9030 FCP cmd x%x failed <%d/%d> "
2657 "status: x%x result: x%x Data: x%x x%x\n",
2658 cmd->cmnd[0],
2659 cmd->device ? cmd->device->id : 0xffff,
2660 cmd->device ? cmd->device->lun : 0xffff,
2661 lpfc_cmd->status, lpfc_cmd->result,
2662 pIocbOut->iocb.ulpContext,
2663 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05002664
2665 switch (lpfc_cmd->status) {
2666 case IOSTAT_FCP_RSP_ERROR:
2667 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05002668 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05002669 break;
2670 case IOSTAT_NPORT_BSY:
2671 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04002672 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04002673 fast_path_evt = lpfc_alloc_fast_evt(phba);
2674 if (!fast_path_evt)
2675 break;
2676 fast_path_evt->un.fabric_evt.event_type =
2677 FC_REG_FABRIC_EVENT;
2678 fast_path_evt->un.fabric_evt.subcategory =
2679 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
2680 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
2681 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2682 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
2683 &pnode->nlp_portname,
2684 sizeof(struct lpfc_name));
2685 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
2686 &pnode->nlp_nodename,
2687 sizeof(struct lpfc_name));
2688 }
2689 fast_path_evt->vport = vport;
2690 fast_path_evt->work_evt.evt =
2691 LPFC_EVT_FASTPATH_MGMT_EVT;
2692 spin_lock_irqsave(&phba->hbalock, flags);
2693 list_add_tail(&fast_path_evt->work_evt.evt_listp,
2694 &phba->work_list);
2695 spin_unlock_irqrestore(&phba->hbalock, flags);
2696 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05002697 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002698 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05002699 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05002700 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
2701 lpfc_cmd->result ==
2702 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
2703 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
2704 lpfc_cmd->result ==
2705 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
2706 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
2707 break;
2708 }
James Smartd7c255b2008-08-24 21:50:00 -04002709 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05002710 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04002711 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
2712 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
James Smart92d7f7b2007-06-17 19:56:38 -05002713 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04002714 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05002715 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002716 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
2717 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
2718 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
2719 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
2720 /*
2721 * This is a response for a BG enabled
2722 * cmd. Parse BG error
2723 */
2724 lpfc_parse_bg_err(phba, lpfc_cmd,
2725 pIocbOut);
2726 break;
2727 } else {
2728 lpfc_printf_vlog(vport, KERN_WARNING,
2729 LOG_BG,
2730 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04002731 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002732 }
2733 }
James Smart1151e3e2011-02-16 12:39:35 -05002734 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
2735 && (phba->sli_rev == LPFC_SLI_REV4)
2736 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
2737 /* This IO was aborted by the target, we don't
2738 * know the rxid and because we did not send the
2739 * ABTS we cannot generate and RRQ.
2740 */
2741 lpfc_set_rrq_active(phba, pnode,
2742 lpfc_cmd->cur_iocbq.sli4_xritag,
2743 0, 0);
2744 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002745 /* else: fall through */
dea31012005-04-17 16:05:31 -05002746 default:
2747 cmd->result = ScsiResult(DID_ERROR, 0);
2748 break;
2749 }
2750
James Smart58da1ff2008-04-07 10:15:56 -04002751 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002752 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04002753 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
2754 SAM_STAT_BUSY);
James Smartab56dc22011-02-16 12:39:57 -05002755 } else
dea31012005-04-17 16:05:31 -05002756 cmd->result = ScsiResult(DID_OK, 0);
dea31012005-04-17 16:05:31 -05002757
2758 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
2759 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
2760
James Smarte8b62012007-08-02 11:10:09 -04002761 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2762 "0710 Iodone <%d/%d> cmd %p, error "
2763 "x%x SNS x%x x%x Data: x%x x%x\n",
2764 cmd->device->id, cmd->device->lun, cmd,
2765 cmd->result, *lp, *(lp + 3), cmd->retries,
2766 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05002767 }
2768
James Smartea2151b2008-09-07 11:52:10 -04002769 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002770 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04002771 if (vport->cfg_max_scsicmpl_time &&
2772 time_after(jiffies, lpfc_cmd->start_time +
2773 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04002774 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002775 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2776 if (pnode->cmd_qdepth >
2777 atomic_read(&pnode->cmd_pending) &&
2778 (atomic_read(&pnode->cmd_pending) >
2779 LPFC_MIN_TGT_QDEPTH) &&
2780 ((cmd->cmnd[0] == READ_10) ||
2781 (cmd->cmnd[0] == WRITE_10)))
2782 pnode->cmd_qdepth =
2783 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04002784
James Smart109f6ed2008-12-04 22:39:08 -05002785 pnode->last_change_time = jiffies;
2786 }
James Smarta257bf92009-04-06 18:48:10 -04002787 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002788 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smart7dc517d2010-07-14 15:32:10 -04002789 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
James Smart977b5a02008-09-07 11:52:04 -04002790 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05002791 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04002792 spin_lock_irqsave(shost->host_lock, flags);
James Smart7dc517d2010-07-14 15:32:10 -04002793 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
2794 / 100;
2795 depth = depth ? depth : 1;
2796 pnode->cmd_qdepth += depth;
2797 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
2798 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05002799 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04002800 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002801 }
James Smart977b5a02008-09-07 11:52:04 -04002802 }
2803
James Smart1dcb58e2007-04-25 09:51:30 -04002804 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04002805
2806 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
2807 queue_depth = cmd->device->queue_depth;
2808 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05002809 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002810
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002811 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smartfa61a542008-01-11 01:52:42 -05002812 /*
2813 * If there is a thread waiting for command completion
2814 * wake up the thread.
2815 */
James Smarta257bf92009-04-06 18:48:10 -04002816 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04002817 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05002818 if (lpfc_cmd->waitq)
2819 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04002820 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002821 lpfc_release_scsi_buf(phba, lpfc_cmd);
2822 return;
2823 }
2824
James Smart92d7f7b2007-06-17 19:56:38 -05002825 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04002826 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05002827
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002828 /*
2829 * Check for queue full. If the lun is reporting queue full, then
2830 * back off the lun queue depth to prevent target overloads.
2831 */
James Smart58da1ff2008-04-07 10:15:56 -04002832 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
2833 NLP_CHK_NODE_ACT(pnode)) {
James Smarta257bf92009-04-06 18:48:10 -04002834 shost_for_each_device(tmp_sdev, shost) {
2835 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002836 continue;
2837 depth = scsi_track_queue_full(tmp_sdev,
James Smart5ffc2662009-11-18 15:39:44 -05002838 tmp_sdev->queue_depth-1);
2839 if (depth <= 0)
2840 continue;
James Smarte8b62012007-08-02 11:10:09 -04002841 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2842 "0711 detected queue full - lun queue "
2843 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04002844 lpfc_send_sdev_queuedepth_change_event(phba, vport,
James Smart5ffc2662009-11-18 15:39:44 -05002845 pnode,
2846 tmp_sdev->lun,
2847 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002848 }
2849 }
2850
James Smartfa61a542008-01-11 01:52:42 -05002851 /*
2852 * If there is a thread waiting for command completion
2853 * wake up the thread.
2854 */
James Smarta257bf92009-04-06 18:48:10 -04002855 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04002856 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05002857 if (lpfc_cmd->waitq)
2858 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04002859 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05002860
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002861 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05002862}
2863
James Smart34b02dc2008-08-24 21:49:55 -04002864/**
James Smart3621a712009-04-06 18:47:14 -04002865 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04002866 * @data: A pointer to the immediate command data portion of the IOCB.
2867 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
2868 *
2869 * The routine copies the entire FCP command from @fcp_cmnd to @data while
2870 * byte swapping the data to big endian format for transmission on the wire.
2871 **/
2872static void
2873lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
2874{
2875 int i, j;
2876 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
2877 i += sizeof(uint32_t), j++) {
2878 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
2879 }
2880}
2881
James Smart9bad7672008-12-04 22:39:02 -05002882/**
James Smartf1126682009-06-10 17:22:44 -04002883 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05002884 * @vport: The virtual port for which this call is being executed.
2885 * @lpfc_cmd: The scsi command which needs to send.
2886 * @pnode: Pointer to lpfc_nodelist.
2887 *
2888 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04002889 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002890 **/
dea31012005-04-17 16:05:31 -05002891static void
James Smartf1126682009-06-10 17:22:44 -04002892lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05002893 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05002894{
James Smart2e0fef82007-06-17 19:56:36 -05002895 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002896 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2897 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2898 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2899 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
2900 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04002901 char tag[2];
dea31012005-04-17 16:05:31 -05002902
James Smart58da1ff2008-04-07 10:15:56 -04002903 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2904 return;
2905
dea31012005-04-17 16:05:31 -05002906 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04002907 /* clear task management bits */
2908 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05002909
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04002910 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
2911 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05002912
2913 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
2914
James Smart7e2b19f2007-10-29 11:00:39 -04002915 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
2916 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05002917 case HEAD_OF_QUEUE_TAG:
2918 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
2919 break;
2920 case ORDERED_QUEUE_TAG:
2921 fcp_cmnd->fcpCntl1 = ORDERED_Q;
2922 break;
2923 default:
2924 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
2925 break;
2926 }
2927 } else
2928 fcp_cmnd->fcpCntl1 = 0;
2929
2930 /*
2931 * There are three possibilities here - use scatter-gather segment, use
2932 * the single mapping, or neither. Start the lpfc command prep by
2933 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2934 * data bde entry.
2935 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002936 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05002937 if (datadir == DMA_TO_DEVICE) {
2938 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart3772a992009-05-22 14:50:54 -04002939 if (phba->sli_rev < LPFC_SLI_REV4) {
2940 iocb_cmd->un.fcpi.fcpi_parm = 0;
2941 iocb_cmd->ulpPU = 0;
2942 } else
2943 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05002944 fcp_cmnd->fcpCntl3 = WRITE_DATA;
2945 phba->fc4OutputRequests++;
2946 } else {
2947 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
2948 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05002949 fcp_cmnd->fcpCntl3 = READ_DATA;
2950 phba->fc4InputRequests++;
2951 }
2952 } else {
2953 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
2954 iocb_cmd->un.fcpi.fcpi_parm = 0;
2955 iocb_cmd->ulpPU = 0;
2956 fcp_cmnd->fcpCntl3 = 0;
2957 phba->fc4ControlRequests++;
2958 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002959 if (phba->sli_rev == 3 &&
2960 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04002961 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05002962 /*
2963 * Finish initializing those IOCB fields that are independent
2964 * of the scsi_cmnd request_buffer
2965 */
2966 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04002967 if (phba->sli_rev == LPFC_SLI_REV4)
2968 piocbq->iocb.ulpContext =
2969 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05002970 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
2971 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05002972 else
2973 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05002974
2975 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
2976 piocbq->context1 = lpfc_cmd;
2977 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
2978 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05002979 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05002980}
2981
James Smart9bad7672008-12-04 22:39:02 -05002982/**
James Smart6d368e52011-05-24 11:44:12 -04002983 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05002984 * @vport: The virtual port for which this call is being executed.
2985 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2986 * @lun: Logical unit number.
2987 * @task_mgmt_cmd: SCSI task management command.
2988 *
James Smart3772a992009-05-22 14:50:54 -04002989 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
2990 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002991 *
2992 * Return codes:
2993 * 0 - Error
2994 * 1 - Success
2995 **/
dea31012005-04-17 16:05:31 -05002996static int
James Smartf1126682009-06-10 17:22:44 -04002997lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05002998 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04002999 unsigned int lun,
dea31012005-04-17 16:05:31 -05003000 uint8_t task_mgmt_cmd)
3001{
dea31012005-04-17 16:05:31 -05003002 struct lpfc_iocbq *piocbq;
3003 IOCB_t *piocb;
3004 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04003005 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05003006 struct lpfc_nodelist *ndlp = rdata->pnode;
3007
James Smart58da1ff2008-04-07 10:15:56 -04003008 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
3009 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05003010 return 0;
dea31012005-04-17 16:05:31 -05003011
dea31012005-04-17 16:05:31 -05003012 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003013 piocbq->vport = vport;
3014
dea31012005-04-17 16:05:31 -05003015 piocb = &piocbq->iocb;
3016
3017 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04003018 /* Clear out any old data in the FCP command area */
3019 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
3020 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05003021 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05003022 if (vport->phba->sli_rev == 3 &&
3023 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04003024 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05003025 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05003026 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04003027 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
3028 piocb->ulpContext =
3029 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
3030 }
dea31012005-04-17 16:05:31 -05003031 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
3032 piocb->ulpFCP2Rcvy = 1;
3033 }
3034 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
3035
3036 /* ulpTimeout is only one byte */
3037 if (lpfc_cmd->timeout > 0xff) {
3038 /*
3039 * Do not timeout the command at the firmware level.
3040 * The driver will provide the timeout mechanism.
3041 */
3042 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04003043 } else
dea31012005-04-17 16:05:31 -05003044 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04003045
3046 if (vport->phba->sli_rev == LPFC_SLI_REV4)
3047 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05003048
James Smart2e0fef82007-06-17 19:56:36 -05003049 return 1;
dea31012005-04-17 16:05:31 -05003050}
3051
James Smart9bad7672008-12-04 22:39:02 -05003052/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003053 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04003054 * @phba: The hba struct for which this call is being executed.
3055 * @dev_grp: The HBA PCI-Device group number.
3056 *
3057 * This routine sets up the SCSI interface API function jump table in @phba
3058 * struct.
3059 * Returns: 0 - success, -ENODEV - failure.
3060 **/
3061int
3062lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
3063{
3064
James Smartf1126682009-06-10 17:22:44 -04003065 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
3066 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04003067
James Smart3772a992009-05-22 14:50:54 -04003068 switch (dev_grp) {
3069 case LPFC_PCI_DEV_LP:
3070 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
3071 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04003072 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05003073 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04003074 break;
James Smartda0436e2009-05-22 14:51:39 -04003075 case LPFC_PCI_DEV_OC:
3076 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
3077 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04003078 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05003079 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04003080 break;
James Smart3772a992009-05-22 14:50:54 -04003081 default:
3082 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3083 "1418 Invalid HBA PCI-device group: 0x%x\n",
3084 dev_grp);
3085 return -ENODEV;
3086 break;
3087 }
James Smart3772a992009-05-22 14:50:54 -04003088 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05003089 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04003090 return 0;
3091}
3092
3093/**
James Smart3621a712009-04-06 18:47:14 -04003094 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05003095 * @phba: The Hba for which this call is being executed.
3096 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
3097 * @rspiocbq: Pointer to lpfc_iocbq data structure.
3098 *
3099 * This routine is IOCB completion routine for device reset and target reset
3100 * routine. This routine release scsi buffer associated with lpfc_cmd.
3101 **/
James Smart7054a602007-04-25 09:52:34 -04003102static void
3103lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
3104 struct lpfc_iocbq *cmdiocbq,
3105 struct lpfc_iocbq *rspiocbq)
3106{
3107 struct lpfc_scsi_buf *lpfc_cmd =
3108 (struct lpfc_scsi_buf *) cmdiocbq->context1;
3109 if (lpfc_cmd)
3110 lpfc_release_scsi_buf(phba, lpfc_cmd);
3111 return;
3112}
3113
James Smart9bad7672008-12-04 22:39:02 -05003114/**
James Smart3621a712009-04-06 18:47:14 -04003115 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05003116 * @host: The scsi host for which this call is being executed.
3117 *
3118 * This routine provides module information about hba.
3119 *
3120 * Reutrn code:
3121 * Pointer to char - Success.
3122 **/
dea31012005-04-17 16:05:31 -05003123const char *
3124lpfc_info(struct Scsi_Host *host)
3125{
James Smart2e0fef82007-06-17 19:56:36 -05003126 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
3127 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003128 int len;
3129 static char lpfcinfobuf[384];
3130
3131 memset(lpfcinfobuf,0,384);
3132 if (phba && phba->pcidev){
3133 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
3134 len = strlen(lpfcinfobuf);
3135 snprintf(lpfcinfobuf + len,
3136 384-len,
3137 " on PCI bus %02x device %02x irq %d",
3138 phba->pcidev->bus->number,
3139 phba->pcidev->devfn,
3140 phba->pcidev->irq);
3141 len = strlen(lpfcinfobuf);
3142 if (phba->Port[0]) {
3143 snprintf(lpfcinfobuf + len,
3144 384-len,
3145 " port %s",
3146 phba->Port);
3147 }
James Smart65467b62010-01-26 23:08:29 -05003148 len = strlen(lpfcinfobuf);
3149 if (phba->sli4_hba.link_state.logical_speed) {
3150 snprintf(lpfcinfobuf + len,
3151 384-len,
3152 " Logical Link Speed: %d Mbps",
3153 phba->sli4_hba.link_state.logical_speed * 10);
3154 }
dea31012005-04-17 16:05:31 -05003155 }
3156 return lpfcinfobuf;
3157}
3158
James Smart9bad7672008-12-04 22:39:02 -05003159/**
James Smart3621a712009-04-06 18:47:14 -04003160 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05003161 * @phba: The Hba for which this call is being executed.
3162 *
3163 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
3164 * The default value of cfg_poll_tmo is 10 milliseconds.
3165 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003166static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
3167{
3168 unsigned long poll_tmo_expires =
3169 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
3170
3171 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
3172 mod_timer(&phba->fcp_poll_timer,
3173 poll_tmo_expires);
3174}
3175
James Smart9bad7672008-12-04 22:39:02 -05003176/**
James Smart3621a712009-04-06 18:47:14 -04003177 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05003178 * @phba: The Hba for which this call is being executed.
3179 *
3180 * This routine starts the fcp_poll_timer of @phba.
3181 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003182void lpfc_poll_start_timer(struct lpfc_hba * phba)
3183{
3184 lpfc_poll_rearm_timer(phba);
3185}
3186
James Smart9bad7672008-12-04 22:39:02 -05003187/**
James Smart3621a712009-04-06 18:47:14 -04003188 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05003189 * @ptr: Map to lpfc_hba data structure pointer.
3190 *
3191 * This routine restarts fcp_poll timer, when FCP ring polling is enable
3192 * and FCP Ring interrupt is disable.
3193 **/
3194
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003195void lpfc_poll_timeout(unsigned long ptr)
3196{
James Smart2e0fef82007-06-17 19:56:36 -05003197 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003198
3199 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04003200 lpfc_sli_handle_fast_ring_event(phba,
3201 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
3202
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003203 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3204 lpfc_poll_rearm_timer(phba);
3205 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003206}
3207
James Smart9bad7672008-12-04 22:39:02 -05003208/**
James Smart3621a712009-04-06 18:47:14 -04003209 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05003210 * @cmnd: Pointer to scsi_cmnd data structure.
3211 * @done: Pointer to done routine.
3212 *
3213 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
3214 * This routine prepares an IOCB from scsi command and provides to firmware.
3215 * The @done callback is invoked after driver finished processing the command.
3216 *
3217 * Return value :
3218 * 0 - Success
3219 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
3220 **/
dea31012005-04-17 16:05:31 -05003221static int
Jeff Garzikf2812332010-11-16 02:10:29 -05003222lpfc_queuecommand_lck(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
dea31012005-04-17 16:05:31 -05003223{
James Smart2e0fef82007-06-17 19:56:36 -05003224 struct Scsi_Host *shost = cmnd->device->host;
3225 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3226 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003227 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003228 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003229 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003230 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003231 int err;
dea31012005-04-17 16:05:31 -05003232
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003233 err = fc_remote_port_chkready(rport);
3234 if (err) {
3235 cmnd->result = err;
dea31012005-04-17 16:05:31 -05003236 goto out_fail_command;
3237 }
James Smart1c6f4ef52009-11-18 15:40:49 -05003238 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05003239
James Smartbf086112011-08-21 21:48:13 -04003240 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
3241 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED) ||
3242 (phba->sli_rev == LPFC_SLI_REV4))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05003243
James Smart6a9c52c2009-10-02 15:16:51 -04003244 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3245 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
3246 " op:%02x str=%s without registering for"
3247 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05003248 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
3249 dif_op_str[scsi_get_prot_op(cmnd)]);
3250 goto out_fail_command;
3251 }
3252
dea31012005-04-17 16:05:31 -05003253 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003254 * Catch race where our node has transitioned, but the
3255 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05003256 */
James Smartb522d7d2008-09-07 11:51:56 -04003257 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
Mike Christief55ca84d2011-02-25 14:04:27 -06003258 cmnd->result = ScsiResult(DID_IMM_RETRY, 0);
James Smartb522d7d2008-09-07 11:51:56 -04003259 goto out_fail_command;
3260 }
James Smart7dc517d2010-07-14 15:32:10 -04003261 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
Mike Christie34963432011-02-25 14:04:28 -06003262 goto out_tgt_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05003263
James Smart19ca7602010-11-20 23:11:55 -05003264 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05003265 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05003266 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003267
James Smarte8b62012007-08-02 11:10:09 -04003268 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3269 "0707 driver's buffer pool is empty, "
3270 "IO busied\n");
dea31012005-04-17 16:05:31 -05003271 goto out_host_busy;
3272 }
3273
3274 /*
3275 * Store the midlayer's command structure for the completion phase
3276 * and complete the command initialization.
3277 */
3278 lpfc_cmd->pCmd = cmnd;
3279 lpfc_cmd->rdata = rdata;
3280 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04003281 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05003282 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
3283 cmnd->scsi_done = done;
3284
James Smarte2a0a9d2008-12-04 22:40:02 -05003285 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04003286 if (vport->phba->cfg_enable_bg) {
3287 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003288 "9033 BLKGRD: rcvd protected cmd:%02x op:%02x "
3289 "str=%s\n",
3290 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
3291 dif_op_str[scsi_get_prot_op(cmnd)]);
James Smart6a9c52c2009-10-02 15:16:51 -04003292 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003293 "9034 BLKGRD: CDB: %02x %02x %02x %02x %02x "
James Smarte4e74272009-07-19 10:01:38 -04003294 "%02x %02x %02x %02x %02x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05003295 cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
3296 cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
3297 cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
3298 cmnd->cmnd[9]);
James Smart6a9c52c2009-10-02 15:16:51 -04003299 if (cmnd->cmnd[0] == READ_10)
3300 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003301 "9035 BLKGRD: READ @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09003302 "count %u\n",
3303 (unsigned long long)scsi_get_lba(cmnd),
3304 blk_rq_sectors(cmnd->request));
James Smart6a9c52c2009-10-02 15:16:51 -04003305 else if (cmnd->cmnd[0] == WRITE_10)
3306 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003307 "9036 BLKGRD: WRITE @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09003308 "count %u cmd=%p\n",
James Smart87b5c322008-12-16 10:34:09 -05003309 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09003310 blk_rq_sectors(cmnd->request),
James Smarte2a0a9d2008-12-04 22:40:02 -05003311 cmnd);
James Smart6a9c52c2009-10-02 15:16:51 -04003312 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003313
3314 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3315 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04003316 if (vport->phba->cfg_enable_bg) {
James Smarte2a0a9d2008-12-04 22:40:02 -05003317 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smart6a9c52c2009-10-02 15:16:51 -04003318 "9038 BLKGRD: rcvd unprotected cmd:"
3319 "%02x op:%02x str=%s\n",
3320 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
3321 dif_op_str[scsi_get_prot_op(cmnd)]);
3322 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3323 "9039 BLKGRD: CDB: %02x %02x %02x "
3324 "%02x %02x %02x %02x %02x %02x %02x\n",
3325 cmnd->cmnd[0], cmnd->cmnd[1],
3326 cmnd->cmnd[2], cmnd->cmnd[3],
3327 cmnd->cmnd[4], cmnd->cmnd[5],
3328 cmnd->cmnd[6], cmnd->cmnd[7],
3329 cmnd->cmnd[8], cmnd->cmnd[9]);
3330 if (cmnd->cmnd[0] == READ_10)
3331 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3332 "9040 dbg: READ @ sector %llu, "
3333 "count %u\n",
3334 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09003335 blk_rq_sectors(cmnd->request));
James Smart6a9c52c2009-10-02 15:16:51 -04003336 else if (cmnd->cmnd[0] == WRITE_10)
3337 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003338 "9041 dbg: WRITE @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09003339 "count %u cmd=%p\n",
James Smart87b5c322008-12-16 10:34:09 -05003340 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09003341 blk_rq_sectors(cmnd->request), cmnd);
James Smart6a9c52c2009-10-02 15:16:51 -04003342 else
3343 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003344 "9042 dbg: parser not implemented\n");
James Smart6a9c52c2009-10-02 15:16:51 -04003345 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003346 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3347 }
3348
dea31012005-04-17 16:05:31 -05003349 if (err)
3350 goto out_host_busy_free_buf;
3351
James Smart2e0fef82007-06-17 19:56:36 -05003352 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05003353
James Smart977b5a02008-09-07 11:52:04 -04003354 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04003355 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05003356 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05003357 if (err) {
3358 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05003359 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05003360 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003361 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04003362 spin_unlock(shost->host_lock);
3363 lpfc_sli_handle_fast_ring_event(phba,
3364 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
3365
3366 spin_lock(shost->host_lock);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003367 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3368 lpfc_poll_rearm_timer(phba);
3369 }
3370
dea31012005-04-17 16:05:31 -05003371 return 0;
3372
3373 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04003374 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003375 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05003376 out_host_busy:
3377 return SCSI_MLQUEUE_HOST_BUSY;
3378
Mike Christie34963432011-02-25 14:04:28 -06003379 out_tgt_busy:
3380 return SCSI_MLQUEUE_TARGET_BUSY;
3381
dea31012005-04-17 16:05:31 -05003382 out_fail_command:
3383 done(cmnd);
3384 return 0;
3385}
3386
Jeff Garzikf2812332010-11-16 02:10:29 -05003387static DEF_SCSI_QCMD(lpfc_queuecommand)
3388
James Smart9bad7672008-12-04 22:39:02 -05003389/**
James Smart3621a712009-04-06 18:47:14 -04003390 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05003391 * @cmnd: Pointer to scsi_cmnd data structure.
3392 *
3393 * This routine aborts @cmnd pending in base driver.
3394 *
3395 * Return code :
3396 * 0x2003 - Error
3397 * 0x2002 - Success
3398 **/
dea31012005-04-17 16:05:31 -05003399static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05003400lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003401{
James Smart2e0fef82007-06-17 19:56:36 -05003402 struct Scsi_Host *shost = cmnd->device->host;
3403 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3404 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003405 struct lpfc_iocbq *iocb;
3406 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05003407 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05003408 IOCB_t *cmd, *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003409 int ret = SUCCESS;
James Smartfa61a542008-01-11 01:52:42 -05003410 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003411
James Smart589a52d2010-07-14 15:30:54 -04003412 ret = fc_block_scsi_eh(cmnd);
3413 if (ret)
3414 return ret;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003415 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smarteee88772010-09-29 11:19:08 -04003416 if (!lpfc_cmd) {
3417 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3418 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003419 "x%x ID %d LUN %d\n",
3420 ret, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04003421 return SUCCESS;
3422 }
dea31012005-04-17 16:05:31 -05003423
3424 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003425 * If pCmd field of the corresponding lpfc_scsi_buf structure
3426 * points to a different SCSI command, then the driver has
3427 * already completed this command, but the midlayer did not
3428 * see the completion before the eh fired. Just return
3429 * SUCCESS.
dea31012005-04-17 16:05:31 -05003430 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003431 iocb = &lpfc_cmd->cur_iocbq;
3432 if (lpfc_cmd->pCmd != cmnd)
3433 goto out;
dea31012005-04-17 16:05:31 -05003434
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003435 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05003436
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003437 abtsiocb = lpfc_sli_get_iocbq(phba);
3438 if (abtsiocb == NULL) {
3439 ret = FAILED;
dea31012005-04-17 16:05:31 -05003440 goto out;
3441 }
3442
dea31012005-04-17 16:05:31 -05003443 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003444 * The scsi command can not be in txq and it is in flight because the
3445 * pCmd is still pointig at the SCSI command we have to abort. There
3446 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05003447 */
dea31012005-04-17 16:05:31 -05003448
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003449 cmd = &iocb->iocb;
3450 icmd = &abtsiocb->iocb;
3451 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
3452 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04003453 if (phba->sli_rev == LPFC_SLI_REV4)
3454 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
3455 else
3456 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05003457
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003458 icmd->ulpLe = 1;
3459 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05003460
3461 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
3462 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05003463 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05003464
James Smart2e0fef82007-06-17 19:56:36 -05003465 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003466 icmd->ulpCommand = CMD_ABORT_XRI_CN;
3467 else
3468 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05003469
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003470 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05003471 abtsiocb->vport = vport;
James Smart3772a992009-05-22 14:50:54 -04003472 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
3473 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003474 lpfc_sli_release_iocbq(phba, abtsiocb);
3475 ret = FAILED;
3476 goto out;
3477 }
3478
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003479 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04003480 lpfc_sli_handle_fast_ring_event(phba,
3481 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003482
James Smartfa61a542008-01-11 01:52:42 -05003483 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003484 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05003485 wait_event_timeout(waitq,
3486 (lpfc_cmd->pCmd != cmnd),
3487 (2*vport->cfg_devloss_tmo*HZ));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003488
James Smartfa61a542008-01-11 01:52:42 -05003489 spin_lock_irq(shost->host_lock);
3490 lpfc_cmd->waitq = NULL;
3491 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003492
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003493 if (lpfc_cmd->pCmd == cmnd) {
3494 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04003495 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3496 "0748 abort handler timed out waiting "
3497 "for abort to complete: ret %#x, ID %d, "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003498 "LUN %d\n",
3499 ret, cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05003500 }
3501
3502 out:
James Smarte8b62012007-08-02 11:10:09 -04003503 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3504 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003505 "LUN %d\n", ret, cmnd->device->id,
3506 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003507 return ret;
dea31012005-04-17 16:05:31 -05003508}
3509
James Smartbbb9d182009-06-10 17:23:16 -04003510static char *
3511lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
3512{
3513 switch (task_mgmt_cmd) {
3514 case FCP_ABORT_TASK_SET:
3515 return "ABORT_TASK_SET";
3516 case FCP_CLEAR_TASK_SET:
3517 return "FCP_CLEAR_TASK_SET";
3518 case FCP_BUS_RESET:
3519 return "FCP_BUS_RESET";
3520 case FCP_LUN_RESET:
3521 return "FCP_LUN_RESET";
3522 case FCP_TARGET_RESET:
3523 return "FCP_TARGET_RESET";
3524 case FCP_CLEAR_ACA:
3525 return "FCP_CLEAR_ACA";
3526 case FCP_TERMINATE_TASK:
3527 return "FCP_TERMINATE_TASK";
3528 default:
3529 return "unknown";
3530 }
3531}
3532
3533/**
3534 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
3535 * @vport: The virtual port for which this call is being executed.
3536 * @rdata: Pointer to remote port local data
3537 * @tgt_id: Target ID of remote device.
3538 * @lun_id: Lun number for the TMF
3539 * @task_mgmt_cmd: type of TMF to send
3540 *
3541 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
3542 * a remote port.
3543 *
3544 * Return Code:
3545 * 0x2003 - Error
3546 * 0x2002 - Success.
3547 **/
3548static int
3549lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
3550 unsigned tgt_id, unsigned int lun_id,
3551 uint8_t task_mgmt_cmd)
3552{
3553 struct lpfc_hba *phba = vport->phba;
3554 struct lpfc_scsi_buf *lpfc_cmd;
3555 struct lpfc_iocbq *iocbq;
3556 struct lpfc_iocbq *iocbqrsp;
James Smart5989b8d2010-10-22 11:06:56 -04003557 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003558 int ret;
3559 int status;
3560
James Smart5989b8d2010-10-22 11:06:56 -04003561 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smartbbb9d182009-06-10 17:23:16 -04003562 return FAILED;
3563
James Smart19ca7602010-11-20 23:11:55 -05003564 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
James Smartbbb9d182009-06-10 17:23:16 -04003565 if (lpfc_cmd == NULL)
3566 return FAILED;
3567 lpfc_cmd->timeout = 60;
3568 lpfc_cmd->rdata = rdata;
3569
3570 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
3571 task_mgmt_cmd);
3572 if (!status) {
3573 lpfc_release_scsi_buf(phba, lpfc_cmd);
3574 return FAILED;
3575 }
3576
3577 iocbq = &lpfc_cmd->cur_iocbq;
3578 iocbqrsp = lpfc_sli_get_iocbq(phba);
3579 if (iocbqrsp == NULL) {
3580 lpfc_release_scsi_buf(phba, lpfc_cmd);
3581 return FAILED;
3582 }
3583
3584 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3585 "0702 Issue %s to TGT %d LUN %d "
James Smart6d368e52011-05-24 11:44:12 -04003586 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04003587 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04003588 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
3589 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04003590
3591 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
3592 iocbq, iocbqrsp, lpfc_cmd->timeout);
3593 if (status != IOCB_SUCCESS) {
3594 if (status == IOCB_TIMEDOUT) {
3595 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
3596 ret = TIMEOUT_ERROR;
3597 } else
3598 ret = FAILED;
3599 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3600 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart6d368e52011-05-24 11:44:12 -04003601 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
3602 "iocb_flag x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04003603 lpfc_taskmgmt_name(task_mgmt_cmd),
3604 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
James Smart6d368e52011-05-24 11:44:12 -04003605 iocbqrsp->iocb.un.ulpWord[4],
3606 iocbq->iocb_flag);
James Smart2a9bf3d2010-06-07 15:24:45 -04003607 } else if (status == IOCB_BUSY)
3608 ret = FAILED;
3609 else
James Smartbbb9d182009-06-10 17:23:16 -04003610 ret = SUCCESS;
3611
3612 lpfc_sli_release_iocbq(phba, iocbqrsp);
3613
3614 if (ret != TIMEOUT_ERROR)
3615 lpfc_release_scsi_buf(phba, lpfc_cmd);
3616
3617 return ret;
3618}
3619
3620/**
3621 * lpfc_chk_tgt_mapped -
3622 * @vport: The virtual port to check on
3623 * @cmnd: Pointer to scsi_cmnd data structure.
3624 *
3625 * This routine delays until the scsi target (aka rport) for the
3626 * command exists (is present and logged in) or we declare it non-existent.
3627 *
3628 * Return code :
3629 * 0x2003 - Error
3630 * 0x2002 - Success
3631 **/
3632static int
3633lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
3634{
3635 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003636 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003637 unsigned long later;
3638
James Smart1c6f4ef52009-11-18 15:40:49 -05003639 if (!rdata) {
3640 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3641 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
3642 return FAILED;
3643 }
3644 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003645 /*
3646 * If target is not in a MAPPED state, delay until
3647 * target is rediscovered or devloss timeout expires.
3648 */
3649 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3650 while (time_after(later, jiffies)) {
3651 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3652 return FAILED;
3653 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
3654 return SUCCESS;
3655 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
3656 rdata = cmnd->device->hostdata;
3657 if (!rdata)
3658 return FAILED;
3659 pnode = rdata->pnode;
3660 }
3661 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
3662 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
3663 return FAILED;
3664 return SUCCESS;
3665}
3666
3667/**
3668 * lpfc_reset_flush_io_context -
3669 * @vport: The virtual port (scsi_host) for the flush context
3670 * @tgt_id: If aborting by Target contect - specifies the target id
3671 * @lun_id: If aborting by Lun context - specifies the lun id
3672 * @context: specifies the context level to flush at.
3673 *
3674 * After a reset condition via TMF, we need to flush orphaned i/o
3675 * contexts from the adapter. This routine aborts any contexts
3676 * outstanding, then waits for their completions. The wait is
3677 * bounded by devloss_tmo though.
3678 *
3679 * Return code :
3680 * 0x2003 - Error
3681 * 0x2002 - Success
3682 **/
3683static int
3684lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
3685 uint64_t lun_id, lpfc_ctx_cmd context)
3686{
3687 struct lpfc_hba *phba = vport->phba;
3688 unsigned long later;
3689 int cnt;
3690
3691 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3692 if (cnt)
3693 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
3694 tgt_id, lun_id, context);
3695 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3696 while (time_after(later, jiffies) && cnt) {
3697 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
3698 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3699 }
3700 if (cnt) {
3701 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3702 "0724 I/O flush failure for context %s : cnt x%x\n",
3703 ((context == LPFC_CTX_LUN) ? "LUN" :
3704 ((context == LPFC_CTX_TGT) ? "TGT" :
3705 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
3706 cnt);
3707 return FAILED;
3708 }
3709 return SUCCESS;
3710}
3711
James Smart9bad7672008-12-04 22:39:02 -05003712/**
James Smart3621a712009-04-06 18:47:14 -04003713 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05003714 * @cmnd: Pointer to scsi_cmnd data structure.
3715 *
James Smartbbb9d182009-06-10 17:23:16 -04003716 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05003717 * command.
3718 *
3719 * Return code :
3720 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04003721 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05003722 **/
dea31012005-04-17 16:05:31 -05003723static int
James Smart7054a602007-04-25 09:52:34 -04003724lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003725{
James Smart2e0fef82007-06-17 19:56:36 -05003726 struct Scsi_Host *shost = cmnd->device->host;
3727 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05003728 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003729 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003730 unsigned tgt_id = cmnd->device->id;
3731 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04003732 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04003733 int status;
dea31012005-04-17 16:05:31 -05003734
James Smart1c6f4ef52009-11-18 15:40:49 -05003735 if (!rdata) {
3736 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3737 "0798 Device Reset rport failure: rdata x%p\n", rdata);
3738 return FAILED;
3739 }
3740 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04003741 status = fc_block_scsi_eh(cmnd);
3742 if (status)
3743 return status;
James Smartbbb9d182009-06-10 17:23:16 -04003744
3745 status = lpfc_chk_tgt_mapped(vport, cmnd);
3746 if (status == FAILED) {
3747 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3748 "0721 Device Reset rport failure: rdata x%p\n", rdata);
3749 return FAILED;
3750 }
3751
3752 scsi_event.event_type = FC_REG_SCSI_EVENT;
3753 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
3754 scsi_event.lun = lun_id;
3755 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3756 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3757
3758 fc_host_post_vendor_event(shost, fc_get_event_number(),
3759 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3760
3761 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3762 FCP_LUN_RESET);
3763
3764 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3765 "0713 SCSI layer issued Device Reset (%d, %d) "
3766 "return x%x\n", tgt_id, lun_id, status);
3767
dea31012005-04-17 16:05:31 -05003768 /*
James Smartbbb9d182009-06-10 17:23:16 -04003769 * We have to clean up i/o as : they may be orphaned by the TMF;
3770 * or if the TMF failed, they may be in an indeterminate state.
3771 * So, continue on.
3772 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05003773 */
James Smartbbb9d182009-06-10 17:23:16 -04003774 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3775 LPFC_CTX_LUN);
3776 return status;
3777}
3778
3779/**
3780 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
3781 * @cmnd: Pointer to scsi_cmnd data structure.
3782 *
3783 * This routine does a target reset by sending a TARGET_RESET task management
3784 * command.
3785 *
3786 * Return code :
3787 * 0x2003 - Error
3788 * 0x2002 - Success
3789 **/
3790static int
3791lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
3792{
3793 struct Scsi_Host *shost = cmnd->device->host;
3794 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3795 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003796 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003797 unsigned tgt_id = cmnd->device->id;
3798 unsigned int lun_id = cmnd->device->lun;
3799 struct lpfc_scsi_event_header scsi_event;
3800 int status;
3801
James Smart1c6f4ef52009-11-18 15:40:49 -05003802 if (!rdata) {
3803 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3804 "0799 Target Reset rport failure: rdata x%p\n", rdata);
3805 return FAILED;
3806 }
3807 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04003808 status = fc_block_scsi_eh(cmnd);
3809 if (status)
3810 return status;
James Smartbbb9d182009-06-10 17:23:16 -04003811
3812 status = lpfc_chk_tgt_mapped(vport, cmnd);
3813 if (status == FAILED) {
3814 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3815 "0722 Target Reset rport failure: rdata x%p\n", rdata);
3816 return FAILED;
dea31012005-04-17 16:05:31 -05003817 }
James Smartea2151b2008-09-07 11:52:10 -04003818
3819 scsi_event.event_type = FC_REG_SCSI_EVENT;
3820 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
3821 scsi_event.lun = 0;
3822 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3823 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3824
James Smartbbb9d182009-06-10 17:23:16 -04003825 fc_host_post_vendor_event(shost, fc_get_event_number(),
3826 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04003827
James Smartbbb9d182009-06-10 17:23:16 -04003828 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3829 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05003830
James Smarte8b62012007-08-02 11:10:09 -04003831 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04003832 "0723 SCSI layer issued Target Reset (%d, %d) "
3833 "return x%x\n", tgt_id, lun_id, status);
3834
3835 /*
3836 * We have to clean up i/o as : they may be orphaned by the TMF;
3837 * or if the TMF failed, they may be in an indeterminate state.
3838 * So, continue on.
3839 * We will report success if all the i/o aborts successfully.
3840 */
3841 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3842 LPFC_CTX_TGT);
3843 return status;
dea31012005-04-17 16:05:31 -05003844}
3845
James Smart9bad7672008-12-04 22:39:02 -05003846/**
James Smart3621a712009-04-06 18:47:14 -04003847 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05003848 * @cmnd: Pointer to scsi_cmnd data structure.
3849 *
James Smartbbb9d182009-06-10 17:23:16 -04003850 * This routine does target reset to all targets on @cmnd->device->host.
3851 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05003852 *
James Smartbbb9d182009-06-10 17:23:16 -04003853 * Return code :
3854 * 0x2003 - Error
3855 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05003856 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04003857static int
James Smart7054a602007-04-25 09:52:34 -04003858lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003859{
James Smart2e0fef82007-06-17 19:56:36 -05003860 struct Scsi_Host *shost = cmnd->device->host;
3861 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05003862 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04003863 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04003864 int match;
3865 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04003866
3867 scsi_event.event_type = FC_REG_SCSI_EVENT;
3868 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
3869 scsi_event.lun = 0;
3870 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
3871 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
3872
James Smartbbb9d182009-06-10 17:23:16 -04003873 fc_host_post_vendor_event(shost, fc_get_event_number(),
3874 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05003875
James Smartbf086112011-08-21 21:48:13 -04003876 status = fc_block_scsi_eh(cmnd);
3877 if (status)
3878 return status;
James Smartbbb9d182009-06-10 17:23:16 -04003879
dea31012005-04-17 16:05:31 -05003880 /*
3881 * Since the driver manages a single bus device, reset all
3882 * targets known to the driver. Should any target reset
3883 * fail, this routine returns failure to the midlayer.
3884 */
James Smarte17da182006-07-06 15:49:25 -04003885 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04003886 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05003887 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003888 spin_lock_irq(shost->host_lock);
3889 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003890 if (!NLP_CHK_NODE_ACT(ndlp))
3891 continue;
James Smart685f0bf2007-04-25 09:53:08 -04003892 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04003893 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04003894 ndlp->rport) {
dea31012005-04-17 16:05:31 -05003895 match = 1;
3896 break;
3897 }
3898 }
James Smart2e0fef82007-06-17 19:56:36 -05003899 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003900 if (!match)
3901 continue;
James Smartbbb9d182009-06-10 17:23:16 -04003902
3903 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
3904 i, 0, FCP_TARGET_RESET);
3905
3906 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04003907 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3908 "0700 Bus Reset on target %d failed\n",
3909 i);
James Smart915caaa2008-06-14 22:52:38 -04003910 ret = FAILED;
dea31012005-04-17 16:05:31 -05003911 }
3912 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003913 /*
James Smartbbb9d182009-06-10 17:23:16 -04003914 * We have to clean up i/o as : they may be orphaned by the TMFs
3915 * above; or if any of the TMFs failed, they may be in an
3916 * indeterminate state.
3917 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003918 */
James Smartbbb9d182009-06-10 17:23:16 -04003919
3920 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
3921 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003922 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04003923
James Smarte8b62012007-08-02 11:10:09 -04003924 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3925 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05003926 return ret;
3927}
3928
James Smart9bad7672008-12-04 22:39:02 -05003929/**
James Smart3621a712009-04-06 18:47:14 -04003930 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05003931 * @sdev: Pointer to scsi_device.
3932 *
3933 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
3934 * globally available list of scsi buffers. This routine also makes sure scsi
3935 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
3936 * of scsi buffer exists for the lifetime of the driver.
3937 *
3938 * Return codes:
3939 * non-0 - Error
3940 * 0 - Success
3941 **/
dea31012005-04-17 16:05:31 -05003942static int
dea31012005-04-17 16:05:31 -05003943lpfc_slave_alloc(struct scsi_device *sdev)
3944{
James Smart2e0fef82007-06-17 19:56:36 -05003945 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3946 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003947 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04003948 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05003949 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04003950 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04003951 uint32_t sdev_cnt;
dea31012005-04-17 16:05:31 -05003952
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003953 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05003954 return -ENXIO;
3955
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003956 sdev->hostdata = rport->dd_data;
James Smartd7c47992010-06-08 18:31:54 -04003957 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05003958
3959 /*
3960 * Populate the cmds_per_lun count scsi_bufs into this host's globally
3961 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04003962 * HBA limit conveyed to the midlayer via the host structure. The
3963 * formula accounts for the lun_queue_depth + error handlers + 1
3964 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05003965 */
3966 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04003967 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05003968
James Smartd7c47992010-06-08 18:31:54 -04003969 /* If allocated buffers are enough do nothing */
3970 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
3971 return 0;
3972
James Smart92d7f7b2007-06-17 19:56:38 -05003973 /* Allow some exchanges to be available always to complete discovery */
3974 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04003975 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3976 "0704 At limitation of %d preallocated "
3977 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05003978 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003979 /* Allow some exchanges to be available always to complete discovery */
3980 } else if (total + num_to_alloc >
3981 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04003982 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3983 "0705 Allocation request of %d "
3984 "command buffers will exceed max of %d. "
3985 "Reducing allocation request to %d.\n",
3986 num_to_alloc, phba->cfg_hba_queue_depth,
3987 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05003988 num_to_alloc = phba->cfg_hba_queue_depth - total;
3989 }
James Smart3772a992009-05-22 14:50:54 -04003990 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
3991 if (num_to_alloc != num_allocated) {
3992 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3993 "0708 Allocation request of %d "
3994 "command buffers did not succeed. "
3995 "Allocated %d buffers.\n",
3996 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05003997 }
James Smart1c6f4ef52009-11-18 15:40:49 -05003998 if (num_allocated > 0)
3999 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05004000 return 0;
4001}
4002
James Smart9bad7672008-12-04 22:39:02 -05004003/**
James Smart3621a712009-04-06 18:47:14 -04004004 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05004005 * @sdev: Pointer to scsi_device.
4006 *
4007 * This routine configures following items
4008 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05004009 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
4010 *
4011 * Return codes:
4012 * 0 - Success
4013 **/
dea31012005-04-17 16:05:31 -05004014static int
4015lpfc_slave_configure(struct scsi_device *sdev)
4016{
James Smart2e0fef82007-06-17 19:56:36 -05004017 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
4018 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004019
4020 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04004021 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05004022 else
James Smart3de2a652007-08-02 11:09:59 -04004023 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05004024
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004025 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004026 lpfc_sli_handle_fast_ring_event(phba,
4027 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004028 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4029 lpfc_poll_rearm_timer(phba);
4030 }
4031
dea31012005-04-17 16:05:31 -05004032 return 0;
4033}
4034
James Smart9bad7672008-12-04 22:39:02 -05004035/**
James Smart3621a712009-04-06 18:47:14 -04004036 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05004037 * @sdev: Pointer to scsi_device.
4038 *
4039 * This routine sets @sdev hostatdata filed to null.
4040 **/
dea31012005-04-17 16:05:31 -05004041static void
4042lpfc_slave_destroy(struct scsi_device *sdev)
4043{
James Smartd7c47992010-06-08 18:31:54 -04004044 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
4045 struct lpfc_hba *phba = vport->phba;
4046 atomic_dec(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05004047 sdev->hostdata = NULL;
4048 return;
4049}
4050
James Smart92d7f7b2007-06-17 19:56:38 -05004051
dea31012005-04-17 16:05:31 -05004052struct scsi_host_template lpfc_template = {
4053 .module = THIS_MODULE,
4054 .name = LPFC_DRIVER_NAME,
4055 .info = lpfc_info,
4056 .queuecommand = lpfc_queuecommand,
4057 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04004058 .eh_device_reset_handler = lpfc_device_reset_handler,
4059 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04004060 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05004061 .slave_alloc = lpfc_slave_alloc,
4062 .slave_configure = lpfc_slave_configure,
4063 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04004064 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05004065 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05004066 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05004067 .cmd_per_lun = LPFC_CMD_PER_LUN,
4068 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05004069 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04004070 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04004071 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05004072 .change_queue_depth = lpfc_change_queue_depth,
dea31012005-04-17 16:05:31 -05004073};
James Smart3de2a652007-08-02 11:09:59 -04004074
4075struct scsi_host_template lpfc_vport_template = {
4076 .module = THIS_MODULE,
4077 .name = LPFC_DRIVER_NAME,
4078 .info = lpfc_info,
4079 .queuecommand = lpfc_queuecommand,
4080 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04004081 .eh_device_reset_handler = lpfc_device_reset_handler,
4082 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04004083 .eh_bus_reset_handler = lpfc_bus_reset_handler,
4084 .slave_alloc = lpfc_slave_alloc,
4085 .slave_configure = lpfc_slave_configure,
4086 .slave_destroy = lpfc_slave_destroy,
4087 .scan_finished = lpfc_scan_finished,
4088 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05004089 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04004090 .cmd_per_lun = LPFC_CMD_PER_LUN,
4091 .use_clustering = ENABLE_CLUSTERING,
4092 .shost_attrs = lpfc_vport_attrs,
4093 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05004094 .change_queue_depth = lpfc_change_queue_depth,
James Smart3de2a652007-08-02 11:09:59 -04004095};