blob: c79b4dc8aa4ae4a589b87ed03e01f4d137331e4a [file] [log] [blame]
James Smartd85296c2012-03-01 22:38:13 -05001/*******************************************************************
dea31012005-04-17 16:05:31 -05002 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartacd68592012-01-18 16:25:09 -05004 * Copyright (C) 2004-2012 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
dea31012005-04-17 16:05:31 -050021#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050023#include <linux/interrupt.h>
Paul Gortmaker09703662011-05-27 09:37:25 -040024#include <linux/export.h>
James Smarta90f5682006-08-17 11:58:04 -040025#include <linux/delay.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050026#include <asm/unaligned.h>
James Smart737d4242013-04-17 20:14:49 -040027#include <linux/crc-t10dif.h>
28#include <net/checksum.h>
dea31012005-04-17 16:05:31 -050029
30#include <scsi/scsi.h>
31#include <scsi/scsi_device.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050032#include <scsi/scsi_eh.h>
dea31012005-04-17 16:05:31 -050033#include <scsi/scsi_host.h>
34#include <scsi/scsi_tcq.h>
35#include <scsi/scsi_transport_fc.h>
36
37#include "lpfc_version.h"
James Smartda0436e2009-05-22 14:51:39 -040038#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050039#include "lpfc_hw.h"
40#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040041#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040042#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050043#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050044#include "lpfc.h"
James Smart9a6b09c2012-03-01 22:37:42 -050045#include "lpfc_scsi.h"
dea31012005-04-17 16:05:31 -050046#include "lpfc_logmsg.h"
47#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050048#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050049
50#define LPFC_RESET_WAIT 2
51#define LPFC_ABORT_WAIT 2
52
James Smart737d4242013-04-17 20:14:49 -040053int _dump_buf_done = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -050054
55static char *dif_op_str[] = {
James Smart9a6b09c2012-03-01 22:37:42 -050056 "PROT_NORMAL",
57 "PROT_READ_INSERT",
58 "PROT_WRITE_STRIP",
59 "PROT_READ_STRIP",
60 "PROT_WRITE_INSERT",
61 "PROT_READ_PASS",
62 "PROT_WRITE_PASS",
63};
64
James Smartf9bb2da2011-10-10 21:34:11 -040065struct scsi_dif_tuple {
66 __be16 guard_tag; /* Checksum */
67 __be16 app_tag; /* Opaque storage */
68 __be32 ref_tag; /* Target LBA or indirect LBA */
69};
70
James Smartda0436e2009-05-22 14:51:39 -040071static void
72lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smart1c6f4ef52009-11-18 15:40:49 -050073static void
74lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smarte2a0a9d2008-12-04 22:40:02 -050075
76static void
James Smart6a9c52c2009-10-02 15:16:51 -040077lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -050078{
79 void *src, *dst;
80 struct scatterlist *sgde = scsi_sglist(cmnd);
81
82 if (!_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040083 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
84 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -050085 __func__);
86 return;
87 }
88
89
90 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -040091 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
92 "9051 BLKGRD: ERROR: data scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -050093 return;
94 }
95
96 dst = (void *) _dump_buf_data;
97 while (sgde) {
98 src = sg_virt(sgde);
99 memcpy(dst, src, sgde->length);
100 dst += sgde->length;
101 sgde = sg_next(sgde);
102 }
103}
104
105static void
James Smart6a9c52c2009-10-02 15:16:51 -0400106lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -0500107{
108 void *src, *dst;
109 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
110
111 if (!_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -0400112 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
113 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -0500114 __func__);
115 return;
116 }
117
118 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -0400119 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
120 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -0500121 return;
122 }
123
124 dst = _dump_buf_dif;
125 while (sgde) {
126 src = sg_virt(sgde);
127 memcpy(dst, src, sgde->length);
128 dst += sgde->length;
129 sgde = sg_next(sgde);
130 }
131}
132
James Smartea2151b2008-09-07 11:52:10 -0400133/**
James Smartf1126682009-06-10 17:22:44 -0400134 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
135 * @phba: Pointer to HBA object.
136 * @lpfc_cmd: lpfc scsi command object pointer.
137 *
138 * This function is called from the lpfc_prep_task_mgmt_cmd function to
139 * set the last bit in the response sge entry.
140 **/
141static void
142lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
143 struct lpfc_scsi_buf *lpfc_cmd)
144{
145 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
146 if (sgl) {
147 sgl += 1;
148 sgl->word2 = le32_to_cpu(sgl->word2);
149 bf_set(lpfc_sli4_sge_last, sgl, 1);
150 sgl->word2 = cpu_to_le32(sgl->word2);
151 }
152}
153
154/**
James Smart3621a712009-04-06 18:47:14 -0400155 * lpfc_update_stats - Update statistical data for the command completion
James Smartea2151b2008-09-07 11:52:10 -0400156 * @phba: Pointer to HBA object.
157 * @lpfc_cmd: lpfc scsi command object pointer.
158 *
159 * This function is called when there is a command completion and this
160 * function updates the statistical data for the command completion.
161 **/
162static void
163lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
164{
165 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
166 struct lpfc_nodelist *pnode = rdata->pnode;
167 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
168 unsigned long flags;
169 struct Scsi_Host *shost = cmd->device->host;
170 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
171 unsigned long latency;
172 int i;
173
174 if (cmd->result)
175 return;
176
James Smart9f1e1b52008-12-04 22:39:40 -0500177 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
178
James Smartea2151b2008-09-07 11:52:10 -0400179 spin_lock_irqsave(shost->host_lock, flags);
180 if (!vport->stat_data_enabled ||
181 vport->stat_data_blocked ||
James Smart5989b8d2010-10-22 11:06:56 -0400182 !pnode ||
James Smartea2151b2008-09-07 11:52:10 -0400183 !pnode->lat_data ||
184 (phba->bucket_type == LPFC_NO_BUCKET)) {
185 spin_unlock_irqrestore(shost->host_lock, flags);
186 return;
187 }
James Smartea2151b2008-09-07 11:52:10 -0400188
189 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
190 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
191 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -0500192 /* check array subscript bounds */
193 if (i < 0)
194 i = 0;
195 else if (i >= LPFC_MAX_BUCKET_COUNT)
196 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -0400197 } else {
198 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
199 if (latency <= (phba->bucket_base +
200 ((1<<i)*phba->bucket_step)))
201 break;
202 }
203
204 pnode->lat_data[i].cmd_count++;
205 spin_unlock_irqrestore(shost->host_lock, flags);
206}
207
James Smartea2151b2008-09-07 11:52:10 -0400208/**
James Smart3621a712009-04-06 18:47:14 -0400209 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
James Smartea2151b2008-09-07 11:52:10 -0400210 * @phba: Pointer to HBA context object.
211 * @vport: Pointer to vport object.
212 * @ndlp: Pointer to FC node associated with the target.
213 * @lun: Lun number of the scsi device.
214 * @old_val: Old value of the queue depth.
215 * @new_val: New value of the queue depth.
216 *
217 * This function sends an event to the mgmt application indicating
218 * there is a change in the scsi device queue depth.
219 **/
220static void
221lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
222 struct lpfc_vport *vport,
223 struct lpfc_nodelist *ndlp,
224 uint32_t lun,
225 uint32_t old_val,
226 uint32_t new_val)
227{
228 struct lpfc_fast_path_event *fast_path_evt;
229 unsigned long flags;
230
231 fast_path_evt = lpfc_alloc_fast_evt(phba);
232 if (!fast_path_evt)
233 return;
234
235 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
236 FC_REG_SCSI_EVENT;
237 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
238 LPFC_EVENT_VARQUEDEPTH;
239
240 /* Report all luns with change in queue depth */
241 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
242 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
243 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
244 &ndlp->nlp_portname, sizeof(struct lpfc_name));
245 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
246 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
247 }
248
249 fast_path_evt->un.queue_depth_evt.oldval = old_val;
250 fast_path_evt->un.queue_depth_evt.newval = new_val;
251 fast_path_evt->vport = vport;
252
253 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
254 spin_lock_irqsave(&phba->hbalock, flags);
255 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
256 spin_unlock_irqrestore(&phba->hbalock, flags);
257 lpfc_worker_wake_up(phba);
258
259 return;
260}
261
James Smart9bad7672008-12-04 22:39:02 -0500262/**
James Smart5ffc2662009-11-18 15:39:44 -0500263 * lpfc_change_queue_depth - Alter scsi device queue depth
264 * @sdev: Pointer the scsi device on which to change the queue depth.
265 * @qdepth: New queue depth to set the sdev to.
266 * @reason: The reason for the queue depth change.
267 *
268 * This function is called by the midlayer and the LLD to alter the queue
269 * depth for a scsi device. This function sets the queue depth to the new
270 * value and sends an event out to log the queue depth change.
271 **/
272int
273lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
274{
275 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
276 struct lpfc_hba *phba = vport->phba;
277 struct lpfc_rport_data *rdata;
278 unsigned long new_queue_depth, old_queue_depth;
279
280 old_queue_depth = sdev->queue_depth;
281 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
282 new_queue_depth = sdev->queue_depth;
283 rdata = sdev->hostdata;
284 if (rdata)
285 lpfc_send_sdev_queuedepth_change_event(phba, vport,
286 rdata->pnode, sdev->lun,
287 old_queue_depth,
288 new_queue_depth);
289 return sdev->queue_depth;
290}
291
292/**
James Smartfe8f7f92013-01-03 15:43:03 -0500293 * lpfc_change_queue_type() - Change a device's scsi tag queuing type
294 * @sdev: Pointer the scsi device whose queue depth is to change
295 * @tag_type: Identifier for queue tag type
296 */
297static int
298lpfc_change_queue_type(struct scsi_device *sdev, int tag_type)
299{
300 if (sdev->tagged_supported) {
301 scsi_set_tag_type(sdev, tag_type);
302 if (tag_type)
303 scsi_activate_tcq(sdev, sdev->queue_depth);
304 else
305 scsi_deactivate_tcq(sdev, sdev->queue_depth);
306 } else
307 tag_type = 0;
308
309 return tag_type;
310}
311
312/**
James Smart3621a712009-04-06 18:47:14 -0400313 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500314 * @phba: The Hba for which this call is being executed.
315 *
316 * This routine is called when there is resource error in driver or firmware.
317 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
318 * posts at most 1 event each second. This routine wakes up worker thread of
319 * @phba to process WORKER_RAM_DOWN_EVENT event.
320 *
321 * This routine should be called with no lock held.
322 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500323void
James Smarteaf15d52008-12-04 22:39:29 -0500324lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500325{
326 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400327 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500328
329 spin_lock_irqsave(&phba->hbalock, flags);
330 atomic_inc(&phba->num_rsrc_err);
331 phba->last_rsrc_error_time = jiffies;
332
333 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
334 spin_unlock_irqrestore(&phba->hbalock, flags);
335 return;
336 }
337
338 phba->last_ramp_down_time = jiffies;
339
340 spin_unlock_irqrestore(&phba->hbalock, flags);
341
342 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400343 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
344 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500345 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500346 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
347
James Smart5e9d9b82008-06-14 22:52:53 -0400348 if (!evt_posted)
349 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500350 return;
351}
352
James Smart9bad7672008-12-04 22:39:02 -0500353/**
James Smart3621a712009-04-06 18:47:14 -0400354 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
James Smart9bad7672008-12-04 22:39:02 -0500355 * @phba: The Hba for which this call is being executed.
356 *
357 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
358 * post at most 1 event every 5 minute after last_ramp_up_time or
359 * last_rsrc_error_time. This routine wakes up worker thread of @phba
360 * to process WORKER_RAM_DOWN_EVENT event.
361 *
362 * This routine should be called with no lock held.
363 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500364static inline void
James Smart3de2a652007-08-02 11:09:59 -0400365lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smarta257bf92009-04-06 18:48:10 -0400366 uint32_t queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500367{
368 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -0400369 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400370 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500371 atomic_inc(&phba->num_cmd_success);
372
James Smarta257bf92009-04-06 18:48:10 -0400373 if (vport->cfg_lun_queue_depth <= queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500374 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500375 spin_lock_irqsave(&phba->hbalock, flags);
James Smart5ffc2662009-11-18 15:39:44 -0500376 if (time_before(jiffies,
377 phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
378 time_before(jiffies,
379 phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500380 spin_unlock_irqrestore(&phba->hbalock, flags);
381 return;
382 }
James Smart92d7f7b2007-06-17 19:56:38 -0500383 phba->last_ramp_up_time = jiffies;
384 spin_unlock_irqrestore(&phba->hbalock, flags);
385
386 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400387 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
388 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500389 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500390 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
391
James Smart5e9d9b82008-06-14 22:52:53 -0400392 if (!evt_posted)
393 lpfc_worker_wake_up(phba);
394 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500395}
396
James Smart9bad7672008-12-04 22:39:02 -0500397/**
James Smart3621a712009-04-06 18:47:14 -0400398 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500399 * @phba: The Hba for which this call is being executed.
400 *
401 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
402 * thread.This routine reduces queue depth for all scsi device on each vport
403 * associated with @phba.
404 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500405void
406lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
407{
James Smart549e55c2007-08-02 11:09:51 -0400408 struct lpfc_vport **vports;
409 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500410 struct scsi_device *sdev;
James Smart5ffc2662009-11-18 15:39:44 -0500411 unsigned long new_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500412 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400413 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500414
415 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
416 num_cmd_success = atomic_read(&phba->num_cmd_success);
417
James Smart75ad83a2012-05-09 21:18:40 -0400418 /*
419 * The error and success command counters are global per
420 * driver instance. If another handler has already
421 * operated on this error event, just exit.
422 */
423 if (num_rsrc_err == 0)
424 return;
425
James Smart549e55c2007-08-02 11:09:51 -0400426 vports = lpfc_create_vport_work_array(phba);
427 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400428 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400429 shost = lpfc_shost_from_vport(vports[i]);
430 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500431 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400432 sdev->queue_depth * num_rsrc_err /
433 (num_rsrc_err + num_cmd_success);
434 if (!new_queue_depth)
435 new_queue_depth = sdev->queue_depth - 1;
436 else
437 new_queue_depth = sdev->queue_depth -
438 new_queue_depth;
James Smart5ffc2662009-11-18 15:39:44 -0500439 lpfc_change_queue_depth(sdev, new_queue_depth,
440 SCSI_QDEPTH_DEFAULT);
James Smart549e55c2007-08-02 11:09:51 -0400441 }
James Smart92d7f7b2007-06-17 19:56:38 -0500442 }
James Smart09372822008-01-11 01:52:54 -0500443 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500444 atomic_set(&phba->num_rsrc_err, 0);
445 atomic_set(&phba->num_cmd_success, 0);
446}
447
James Smart9bad7672008-12-04 22:39:02 -0500448/**
James Smart3621a712009-04-06 18:47:14 -0400449 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500450 * @phba: The Hba for which this call is being executed.
451 *
452 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
453 * thread.This routine increases queue depth for all scsi device on each vport
454 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
455 * num_cmd_success to zero.
456 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500457void
458lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
459{
James Smart549e55c2007-08-02 11:09:51 -0400460 struct lpfc_vport **vports;
461 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500462 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400463 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500464
James Smart549e55c2007-08-02 11:09:51 -0400465 vports = lpfc_create_vport_work_array(phba);
466 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400467 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400468 shost = lpfc_shost_from_vport(vports[i]);
469 shost_for_each_device(sdev, shost) {
James Smart97eab632008-04-07 10:16:05 -0400470 if (vports[i]->cfg_lun_queue_depth <=
471 sdev->queue_depth)
472 continue;
James Smart5ffc2662009-11-18 15:39:44 -0500473 lpfc_change_queue_depth(sdev,
474 sdev->queue_depth+1,
475 SCSI_QDEPTH_RAMP_UP);
James Smart549e55c2007-08-02 11:09:51 -0400476 }
James Smart92d7f7b2007-06-17 19:56:38 -0500477 }
James Smart09372822008-01-11 01:52:54 -0500478 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500479 atomic_set(&phba->num_rsrc_err, 0);
480 atomic_set(&phba->num_cmd_success, 0);
481}
482
James Smarta8e497d2008-08-24 21:50:11 -0400483/**
James Smart3621a712009-04-06 18:47:14 -0400484 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400485 * @phba: Pointer to HBA context object.
486 *
487 * This function walks vport list and set each SCSI host to block state
488 * by invoking fc_remote_port_delete() routine. This function is invoked
489 * with EEH when device's PCI slot has been permanently disabled.
490 **/
491void
492lpfc_scsi_dev_block(struct lpfc_hba *phba)
493{
494 struct lpfc_vport **vports;
495 struct Scsi_Host *shost;
496 struct scsi_device *sdev;
497 struct fc_rport *rport;
498 int i;
499
500 vports = lpfc_create_vport_work_array(phba);
501 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400502 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400503 shost = lpfc_shost_from_vport(vports[i]);
504 shost_for_each_device(sdev, shost) {
505 rport = starget_to_rport(scsi_target(sdev));
506 fc_remote_port_delete(rport);
507 }
508 }
509 lpfc_destroy_vport_work_array(phba, vports);
510}
511
James Smart9bad7672008-12-04 22:39:02 -0500512/**
James Smart3772a992009-05-22 14:50:54 -0400513 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500514 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400515 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500516 *
James Smart3772a992009-05-22 14:50:54 -0400517 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
518 * the scsi buffer contains all the necessary information needed to initiate
519 * a SCSI I/O. The non-DMAable buffer region contains information to build
520 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
521 * and the initial BPL. In addition to allocating memory, the FCP CMND and
522 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500523 *
524 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400525 * int - number of scsi buffers that were allocated.
526 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500527 **/
James Smart3772a992009-05-22 14:50:54 -0400528static int
529lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500530{
James Smart2e0fef82007-06-17 19:56:36 -0500531 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500532 struct lpfc_scsi_buf *psb;
533 struct ulp_bde64 *bpl;
534 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400535 dma_addr_t pdma_phys_fcp_cmd;
536 dma_addr_t pdma_phys_fcp_rsp;
537 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500538 uint16_t iotag;
James Smart96f70772013-04-17 20:16:15 -0400539 int bcnt, bpl_size;
540
541 bpl_size = phba->cfg_sg_dma_buf_size -
542 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
543
544 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
545 "9067 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
546 num_to_alloc, phba->cfg_sg_dma_buf_size,
547 (int)sizeof(struct fcp_cmnd),
548 (int)sizeof(struct fcp_rsp), bpl_size);
dea31012005-04-17 16:05:31 -0500549
James Smart3772a992009-05-22 14:50:54 -0400550 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
551 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
552 if (!psb)
553 break;
dea31012005-04-17 16:05:31 -0500554
James Smart3772a992009-05-22 14:50:54 -0400555 /*
556 * Get memory from the pci pool to map the virt space to pci
557 * bus space for an I/O. The DMA buffer includes space for the
558 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
559 * necessary to support the sg_tablesize.
560 */
561 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
562 GFP_KERNEL, &psb->dma_handle);
563 if (!psb->data) {
564 kfree(psb);
565 break;
566 }
dea31012005-04-17 16:05:31 -0500567
James Smart3772a992009-05-22 14:50:54 -0400568 /* Initialize virtual ptrs to dma_buf region. */
569 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
dea31012005-04-17 16:05:31 -0500570
James Smart3772a992009-05-22 14:50:54 -0400571 /* Allocate iotag for psb->cur_iocbq. */
572 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
573 if (iotag == 0) {
574 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
575 psb->data, psb->dma_handle);
576 kfree(psb);
577 break;
578 }
579 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500580
James Smart3772a992009-05-22 14:50:54 -0400581 psb->fcp_cmnd = psb->data;
582 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
583 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400584 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500585
James Smart3772a992009-05-22 14:50:54 -0400586 /* Initialize local short-hand pointers. */
587 bpl = psb->fcp_bpl;
588 pdma_phys_fcp_cmd = psb->dma_handle;
589 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
590 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
591 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500592
James Smart3772a992009-05-22 14:50:54 -0400593 /*
594 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
595 * are sg list bdes. Initialize the first two and leave the
596 * rest for queuecommand.
597 */
598 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
599 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
600 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
601 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
602 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500603
James Smart3772a992009-05-22 14:50:54 -0400604 /* Setup the physical region for the FCP RSP */
605 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
606 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
607 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
608 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
609 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
610
611 /*
612 * Since the IOCB for the FCP I/O is built into this
613 * lpfc_scsi_buf, initialize it with all known data now.
614 */
615 iocb = &psb->cur_iocbq.iocb;
616 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
617 if ((phba->sli_rev == 3) &&
618 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
619 /* fill in immediate fcp command BDE */
620 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
621 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
622 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
623 unsli3.fcp_ext.icd);
624 iocb->un.fcpi64.bdl.addrHigh = 0;
625 iocb->ulpBdeCount = 0;
626 iocb->ulpLe = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300627 /* fill in response BDE */
James Smart3772a992009-05-22 14:50:54 -0400628 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
629 BUFF_TYPE_BDE_64;
630 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
631 sizeof(struct fcp_rsp);
632 iocb->unsli3.fcp_ext.rbde.addrLow =
633 putPaddrLow(pdma_phys_fcp_rsp);
634 iocb->unsli3.fcp_ext.rbde.addrHigh =
635 putPaddrHigh(pdma_phys_fcp_rsp);
636 } else {
637 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
638 iocb->un.fcpi64.bdl.bdeSize =
639 (2 * sizeof(struct ulp_bde64));
640 iocb->un.fcpi64.bdl.addrLow =
641 putPaddrLow(pdma_phys_bpl);
642 iocb->un.fcpi64.bdl.addrHigh =
643 putPaddrHigh(pdma_phys_bpl);
644 iocb->ulpBdeCount = 1;
645 iocb->ulpLe = 1;
646 }
647 iocb->ulpClass = CLASS3;
648 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400649 /* Put it back into the SCSI buffer list */
James Smarteee88772010-09-29 11:19:08 -0400650 psb->cur_iocbq.context1 = psb;
James Smart1c6f4ef52009-11-18 15:40:49 -0500651 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400652
James Smart34b02dc2008-08-24 21:49:55 -0400653 }
dea31012005-04-17 16:05:31 -0500654
James Smart3772a992009-05-22 14:50:54 -0400655 return bcnt;
dea31012005-04-17 16:05:31 -0500656}
657
James Smart9bad7672008-12-04 22:39:02 -0500658/**
James Smart1151e3e2011-02-16 12:39:35 -0500659 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
660 * @vport: pointer to lpfc vport data structure.
661 *
662 * This routine is invoked by the vport cleanup for deletions and the cleanup
663 * for an ndlp on removal.
664 **/
665void
666lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
667{
668 struct lpfc_hba *phba = vport->phba;
669 struct lpfc_scsi_buf *psb, *next_psb;
670 unsigned long iflag = 0;
671
672 spin_lock_irqsave(&phba->hbalock, iflag);
673 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
674 list_for_each_entry_safe(psb, next_psb,
675 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
676 if (psb->rdata && psb->rdata->pnode
677 && psb->rdata->pnode->vport == vport)
678 psb->rdata = NULL;
679 }
680 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
681 spin_unlock_irqrestore(&phba->hbalock, iflag);
682}
683
684/**
James Smartda0436e2009-05-22 14:51:39 -0400685 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
686 * @phba: pointer to lpfc hba data structure.
687 * @axri: pointer to the fcp xri abort wcqe structure.
688 *
689 * This routine is invoked by the worker thread to process a SLI4 fast-path
690 * FCP aborted xri.
691 **/
692void
693lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
694 struct sli4_wcqe_xri_aborted *axri)
695{
696 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500697 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartda0436e2009-05-22 14:51:39 -0400698 struct lpfc_scsi_buf *psb, *next_psb;
699 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500700 struct lpfc_iocbq *iocbq;
701 int i;
James Smart19ca7602010-11-20 23:11:55 -0500702 struct lpfc_nodelist *ndlp;
703 int rrq_empty = 0;
James Smart589a52d2010-07-14 15:30:54 -0400704 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smartda0436e2009-05-22 14:51:39 -0400705
James Smart0f65ff62010-02-26 14:14:23 -0500706 spin_lock_irqsave(&phba->hbalock, iflag);
707 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400708 list_for_each_entry_safe(psb, next_psb,
709 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
710 if (psb->cur_iocbq.sli4_xritag == xri) {
711 list_del(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500712 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400713 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500714 spin_unlock(
715 &phba->sli4_hba.abts_scsi_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500716 if (psb->rdata && psb->rdata->pnode)
717 ndlp = psb->rdata->pnode;
718 else
719 ndlp = NULL;
720
James Smart19ca7602010-11-20 23:11:55 -0500721 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500722 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartcb69f7d2011-12-13 13:21:57 -0500723 if (ndlp) {
James Smartee0f4fe2012-05-09 21:19:14 -0400724 lpfc_set_rrq_active(phba, ndlp,
725 psb->cur_iocbq.sli4_lxritag, rxid, 1);
James Smartcb69f7d2011-12-13 13:21:57 -0500726 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
727 }
James Smartda0436e2009-05-22 14:51:39 -0400728 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500729 if (rrq_empty)
730 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400731 return;
732 }
733 }
James Smart0f65ff62010-02-26 14:14:23 -0500734 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
735 for (i = 1; i <= phba->sli.last_iotag; i++) {
736 iocbq = phba->sli.iocbq_lookup[i];
737
738 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
739 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
740 continue;
741 if (iocbq->sli4_xritag != xri)
742 continue;
743 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
744 psb->exch_busy = 0;
745 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart0e9bb8d2013-03-01 16:35:12 -0500746 if (!list_empty(&pring->txq))
James Smart589a52d2010-07-14 15:30:54 -0400747 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500748 return;
749
750 }
751 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400752}
753
754/**
James Smart8a9d2e82012-05-09 21:16:12 -0400755 * lpfc_sli4_post_scsi_sgl_list - Psot blocks of scsi buffer sgls from a list
756 * @phba: pointer to lpfc hba data structure.
757 * @post_sblist: pointer to the scsi buffer list.
758 *
759 * This routine walks a list of scsi buffers that was passed in. It attempts
760 * to construct blocks of scsi buffer sgls which contains contiguous xris and
761 * uses the non-embedded SGL block post mailbox commands to post to the port.
762 * For single SCSI buffer sgl with non-contiguous xri, if any, it shall use
763 * embedded SGL post mailbox command for posting. The @post_sblist passed in
764 * must be local list, thus no lock is needed when manipulate the list.
765 *
766 * Returns: 0 = failure, non-zero number of successfully posted buffers.
767 **/
768int
769lpfc_sli4_post_scsi_sgl_list(struct lpfc_hba *phba,
770 struct list_head *post_sblist, int sb_count)
771{
772 struct lpfc_scsi_buf *psb, *psb_next;
James Smart96f70772013-04-17 20:16:15 -0400773 int status, sgl_size;
James Smart8a9d2e82012-05-09 21:16:12 -0400774 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
775 dma_addr_t pdma_phys_bpl1;
776 int last_xritag = NO_XRI;
777 LIST_HEAD(prep_sblist);
778 LIST_HEAD(blck_sblist);
779 LIST_HEAD(scsi_sblist);
780
781 /* sanity check */
782 if (sb_count <= 0)
783 return -EINVAL;
784
James Smart96f70772013-04-17 20:16:15 -0400785 sgl_size = phba->cfg_sg_dma_buf_size -
786 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
787
James Smart8a9d2e82012-05-09 21:16:12 -0400788 list_for_each_entry_safe(psb, psb_next, post_sblist, list) {
789 list_del_init(&psb->list);
790 block_cnt++;
791 if ((last_xritag != NO_XRI) &&
792 (psb->cur_iocbq.sli4_xritag != last_xritag + 1)) {
793 /* a hole in xri block, form a sgl posting block */
794 list_splice_init(&prep_sblist, &blck_sblist);
795 post_cnt = block_cnt - 1;
796 /* prepare list for next posting block */
797 list_add_tail(&psb->list, &prep_sblist);
798 block_cnt = 1;
799 } else {
800 /* prepare list for next posting block */
801 list_add_tail(&psb->list, &prep_sblist);
802 /* enough sgls for non-embed sgl mbox command */
803 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
804 list_splice_init(&prep_sblist, &blck_sblist);
805 post_cnt = block_cnt;
806 block_cnt = 0;
807 }
808 }
809 num_posting++;
810 last_xritag = psb->cur_iocbq.sli4_xritag;
811
812 /* end of repost sgl list condition for SCSI buffers */
813 if (num_posting == sb_count) {
814 if (post_cnt == 0) {
815 /* last sgl posting block */
816 list_splice_init(&prep_sblist, &blck_sblist);
817 post_cnt = block_cnt;
818 } else if (block_cnt == 1) {
819 /* last single sgl with non-contiguous xri */
James Smart96f70772013-04-17 20:16:15 -0400820 if (sgl_size > SGL_PAGE_SIZE)
James Smart8a9d2e82012-05-09 21:16:12 -0400821 pdma_phys_bpl1 = psb->dma_phys_bpl +
822 SGL_PAGE_SIZE;
823 else
824 pdma_phys_bpl1 = 0;
825 status = lpfc_sli4_post_sgl(phba,
826 psb->dma_phys_bpl,
827 pdma_phys_bpl1,
828 psb->cur_iocbq.sli4_xritag);
829 if (status) {
830 /* failure, put on abort scsi list */
831 psb->exch_busy = 1;
832 } else {
833 /* success, put on SCSI buffer list */
834 psb->exch_busy = 0;
835 psb->status = IOSTAT_SUCCESS;
836 num_posted++;
837 }
838 /* success, put on SCSI buffer sgl list */
839 list_add_tail(&psb->list, &scsi_sblist);
840 }
841 }
842
843 /* continue until a nembed page worth of sgls */
844 if (post_cnt == 0)
845 continue;
846
847 /* post block of SCSI buffer list sgls */
848 status = lpfc_sli4_post_scsi_sgl_block(phba, &blck_sblist,
849 post_cnt);
850
851 /* don't reset xirtag due to hole in xri block */
852 if (block_cnt == 0)
853 last_xritag = NO_XRI;
854
855 /* reset SCSI buffer post count for next round of posting */
856 post_cnt = 0;
857
858 /* put posted SCSI buffer-sgl posted on SCSI buffer sgl list */
859 while (!list_empty(&blck_sblist)) {
860 list_remove_head(&blck_sblist, psb,
861 struct lpfc_scsi_buf, list);
862 if (status) {
863 /* failure, put on abort scsi list */
864 psb->exch_busy = 1;
865 } else {
866 /* success, put on SCSI buffer list */
867 psb->exch_busy = 0;
868 psb->status = IOSTAT_SUCCESS;
869 num_posted++;
870 }
871 list_add_tail(&psb->list, &scsi_sblist);
872 }
873 }
874 /* Push SCSI buffers with sgl posted to the availble list */
875 while (!list_empty(&scsi_sblist)) {
876 list_remove_head(&scsi_sblist, psb,
877 struct lpfc_scsi_buf, list);
878 lpfc_release_scsi_buf_s4(phba, psb);
879 }
880 return num_posted;
881}
882
883/**
884 * lpfc_sli4_repost_scsi_sgl_list - Repsot all the allocated scsi buffer sgls
James Smartda0436e2009-05-22 14:51:39 -0400885 * @phba: pointer to lpfc hba data structure.
886 *
887 * This routine walks the list of scsi buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -0400888 * repost them to the port by using SGL block post. This is needed after a
James Smartda0436e2009-05-22 14:51:39 -0400889 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
890 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
891 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
892 *
893 * Returns: 0 = success, non-zero failure.
894 **/
895int
896lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
897{
James Smart8a9d2e82012-05-09 21:16:12 -0400898 LIST_HEAD(post_sblist);
899 int num_posted, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -0400900
James Smart8a9d2e82012-05-09 21:16:12 -0400901 /* get all SCSI buffers need to repost to a local list */
James Smart38c20672013-03-01 16:37:44 -0500902 spin_lock_irq(&phba->scsi_buf_list_lock);
James Smart8a9d2e82012-05-09 21:16:12 -0400903 list_splice_init(&phba->lpfc_scsi_buf_list, &post_sblist);
James Smart38c20672013-03-01 16:37:44 -0500904 spin_unlock_irq(&phba->scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400905
James Smart8a9d2e82012-05-09 21:16:12 -0400906 /* post the list of scsi buffer sgls to port if available */
907 if (!list_empty(&post_sblist)) {
908 num_posted = lpfc_sli4_post_scsi_sgl_list(phba, &post_sblist,
909 phba->sli4_hba.scsi_xri_cnt);
910 /* failed to post any scsi buffer, return error */
911 if (num_posted == 0)
912 rc = -EIO;
James Smartda0436e2009-05-22 14:51:39 -0400913 }
914 return rc;
915}
916
917/**
918 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
919 * @vport: The virtual port for which this call being executed.
920 * @num_to_allocate: The requested number of buffers to allocate.
921 *
James Smart8a9d2e82012-05-09 21:16:12 -0400922 * This routine allocates scsi buffers for device with SLI-4 interface spec,
James Smartda0436e2009-05-22 14:51:39 -0400923 * the scsi buffer contains all the necessary information needed to initiate
James Smart8a9d2e82012-05-09 21:16:12 -0400924 * a SCSI I/O. After allocating up to @num_to_allocate SCSI buffers and put
925 * them on a list, it post them to the port by using SGL block post.
James Smartda0436e2009-05-22 14:51:39 -0400926 *
927 * Return codes:
James Smart8a9d2e82012-05-09 21:16:12 -0400928 * int - number of scsi buffers that were allocated and posted.
James Smartda0436e2009-05-22 14:51:39 -0400929 * 0 = failure, less than num_to_alloc is a partial failure.
930 **/
931static int
932lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
933{
934 struct lpfc_hba *phba = vport->phba;
935 struct lpfc_scsi_buf *psb;
936 struct sli4_sge *sgl;
937 IOCB_t *iocb;
938 dma_addr_t pdma_phys_fcp_cmd;
939 dma_addr_t pdma_phys_fcp_rsp;
James Smart96f70772013-04-17 20:16:15 -0400940 dma_addr_t pdma_phys_bpl;
James Smart8a9d2e82012-05-09 21:16:12 -0400941 uint16_t iotag, lxri = 0;
James Smart96f70772013-04-17 20:16:15 -0400942 int bcnt, num_posted, sgl_size;
James Smart8a9d2e82012-05-09 21:16:12 -0400943 LIST_HEAD(prep_sblist);
944 LIST_HEAD(post_sblist);
945 LIST_HEAD(scsi_sblist);
James Smartda0436e2009-05-22 14:51:39 -0400946
James Smart96f70772013-04-17 20:16:15 -0400947 sgl_size = phba->cfg_sg_dma_buf_size -
948 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
949
950 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
951 "9068 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
952 num_to_alloc, phba->cfg_sg_dma_buf_size, sgl_size,
953 (int)sizeof(struct fcp_cmnd),
954 (int)sizeof(struct fcp_rsp));
955
James Smartda0436e2009-05-22 14:51:39 -0400956 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
957 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
958 if (!psb)
959 break;
James Smartda0436e2009-05-22 14:51:39 -0400960 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400961 * Get memory from the pci pool to map the virt space to
962 * pci bus space for an I/O. The DMA buffer includes space
963 * for the struct fcp_cmnd, struct fcp_rsp and the number
964 * of bde's necessary to support the sg_tablesize.
James Smartda0436e2009-05-22 14:51:39 -0400965 */
966 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
967 GFP_KERNEL, &psb->dma_handle);
968 if (!psb->data) {
969 kfree(psb);
970 break;
971 }
James Smartda0436e2009-05-22 14:51:39 -0400972 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
973
James Smart96f70772013-04-17 20:16:15 -0400974 /* Page alignment is CRITICAL, double check to be sure */
975 if (((unsigned long)(psb->data) &
976 (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0) {
977 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
978 psb->data, psb->dma_handle);
979 kfree(psb);
980 break;
981 }
982
James Smartda0436e2009-05-22 14:51:39 -0400983 /* Allocate iotag for psb->cur_iocbq. */
984 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
985 if (iotag == 0) {
James Smartb92938b2010-06-07 15:24:12 -0400986 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
987 psb->data, psb->dma_handle);
James Smartda0436e2009-05-22 14:51:39 -0400988 kfree(psb);
989 break;
990 }
991
James Smart6d368e52011-05-24 11:44:12 -0400992 lxri = lpfc_sli4_next_xritag(phba);
993 if (lxri == NO_XRI) {
James Smartda0436e2009-05-22 14:51:39 -0400994 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
995 psb->data, psb->dma_handle);
996 kfree(psb);
997 break;
998 }
James Smart6d368e52011-05-24 11:44:12 -0400999 psb->cur_iocbq.sli4_lxritag = lxri;
1000 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smartda0436e2009-05-22 14:51:39 -04001001 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Smartda0436e2009-05-22 14:51:39 -04001002 psb->fcp_bpl = psb->data;
James Smart96f70772013-04-17 20:16:15 -04001003 psb->fcp_cmnd = (psb->data + sgl_size);
James Smartda0436e2009-05-22 14:51:39 -04001004 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
1005 sizeof(struct fcp_cmnd));
1006
1007 /* Initialize local short-hand pointers. */
1008 sgl = (struct sli4_sge *)psb->fcp_bpl;
1009 pdma_phys_bpl = psb->dma_handle;
James Smart96f70772013-04-17 20:16:15 -04001010 pdma_phys_fcp_cmd = (psb->dma_handle + sgl_size);
James Smartda0436e2009-05-22 14:51:39 -04001011 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
1012
1013 /*
James Smart8a9d2e82012-05-09 21:16:12 -04001014 * The first two bdes are the FCP_CMD and FCP_RSP.
1015 * The balance are sg list bdes. Initialize the
1016 * first two and leave the rest for queuecommand.
James Smartda0436e2009-05-22 14:51:39 -04001017 */
1018 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
1019 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
James Smart05580562011-05-24 11:40:48 -04001020 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04001021 bf_set(lpfc_sli4_sge_last, sgl, 0);
1022 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05001023 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
James Smartda0436e2009-05-22 14:51:39 -04001024 sgl++;
1025
1026 /* Setup the physical region for the FCP RSP */
1027 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
1028 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smart05580562011-05-24 11:40:48 -04001029 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04001030 bf_set(lpfc_sli4_sge_last, sgl, 1);
1031 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05001032 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -04001033
1034 /*
1035 * Since the IOCB for the FCP I/O is built into this
1036 * lpfc_scsi_buf, initialize it with all known data now.
1037 */
1038 iocb = &psb->cur_iocbq.iocb;
1039 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
1040 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
1041 /* setting the BLP size to 2 * sizeof BDE may not be correct.
1042 * We are setting the bpl to point to out sgl. An sgl's
1043 * entries are 16 bytes, a bpl entries are 12 bytes.
1044 */
1045 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
1046 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
1047 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
1048 iocb->ulpBdeCount = 1;
1049 iocb->ulpLe = 1;
1050 iocb->ulpClass = CLASS3;
James Smart8a9d2e82012-05-09 21:16:12 -04001051 psb->cur_iocbq.context1 = psb;
James Smartda0436e2009-05-22 14:51:39 -04001052 psb->dma_phys_bpl = pdma_phys_bpl;
James Smart6d368e52011-05-24 11:44:12 -04001053
James Smart8a9d2e82012-05-09 21:16:12 -04001054 /* add the scsi buffer to a post list */
1055 list_add_tail(&psb->list, &post_sblist);
1056 spin_lock_irq(&phba->scsi_buf_list_lock);
1057 phba->sli4_hba.scsi_xri_cnt++;
1058 spin_unlock_irq(&phba->scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04001059 }
James Smart8a9d2e82012-05-09 21:16:12 -04001060 lpfc_printf_log(phba, KERN_INFO, LOG_BG,
1061 "3021 Allocate %d out of %d requested new SCSI "
1062 "buffers\n", bcnt, num_to_alloc);
James Smartda0436e2009-05-22 14:51:39 -04001063
James Smart8a9d2e82012-05-09 21:16:12 -04001064 /* post the list of scsi buffer sgls to port if available */
1065 if (!list_empty(&post_sblist))
1066 num_posted = lpfc_sli4_post_scsi_sgl_list(phba,
1067 &post_sblist, bcnt);
1068 else
1069 num_posted = 0;
1070
1071 return num_posted;
James Smartda0436e2009-05-22 14:51:39 -04001072}
1073
1074/**
James Smart3772a992009-05-22 14:50:54 -04001075 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
1076 * @vport: The virtual port for which this call being executed.
1077 * @num_to_allocate: The requested number of buffers to allocate.
1078 *
1079 * This routine wraps the actual SCSI buffer allocator function pointer from
1080 * the lpfc_hba struct.
1081 *
1082 * Return codes:
1083 * int - number of scsi buffers that were allocated.
1084 * 0 = failure, less than num_to_alloc is a partial failure.
1085 **/
1086static inline int
1087lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
1088{
1089 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
1090}
1091
1092/**
James Smart19ca7602010-11-20 23:11:55 -05001093 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -04001094 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05001095 *
1096 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1097 * and returns to caller.
1098 *
1099 * Return codes:
1100 * NULL - Error
1101 * Pointer to lpfc_scsi_buf - Success
1102 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +01001103static struct lpfc_scsi_buf*
James Smart19ca7602010-11-20 23:11:55 -05001104lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001105{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001106 struct lpfc_scsi_buf * lpfc_cmd = NULL;
1107 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001108 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001109
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001110 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001111 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -04001112 if (lpfc_cmd) {
1113 lpfc_cmd->seg_cnt = 0;
1114 lpfc_cmd->nonsg_phys = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001115 lpfc_cmd->prot_seg_cnt = 0;
James Smart1dcb58e2007-04-25 09:51:30 -04001116 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001117 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001118 return lpfc_cmd;
1119}
James Smart19ca7602010-11-20 23:11:55 -05001120/**
1121 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1122 * @phba: The HBA for which this call is being executed.
1123 *
1124 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1125 * and returns to caller.
1126 *
1127 * Return codes:
1128 * NULL - Error
1129 * Pointer to lpfc_scsi_buf - Success
1130 **/
1131static struct lpfc_scsi_buf*
1132lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1133{
James Smart1151e3e2011-02-16 12:39:35 -05001134 struct lpfc_scsi_buf *lpfc_cmd ;
James Smart19ca7602010-11-20 23:11:55 -05001135 unsigned long iflag = 0;
1136 int found = 0;
1137
1138 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -05001139 list_for_each_entry(lpfc_cmd, &phba->lpfc_scsi_buf_list,
1140 list) {
James Smart19ca7602010-11-20 23:11:55 -05001141 if (lpfc_test_rrq_active(phba, ndlp,
James Smartee0f4fe2012-05-09 21:19:14 -04001142 lpfc_cmd->cur_iocbq.sli4_lxritag))
James Smart1151e3e2011-02-16 12:39:35 -05001143 continue;
1144 list_del(&lpfc_cmd->list);
James Smart19ca7602010-11-20 23:11:55 -05001145 found = 1;
1146 lpfc_cmd->seg_cnt = 0;
1147 lpfc_cmd->nonsg_phys = 0;
1148 lpfc_cmd->prot_seg_cnt = 0;
James Smart1151e3e2011-02-16 12:39:35 -05001149 break;
James Smart19ca7602010-11-20 23:11:55 -05001150 }
James Smart1151e3e2011-02-16 12:39:35 -05001151 spin_unlock_irqrestore(&phba->scsi_buf_list_lock,
1152 iflag);
1153 if (!found)
1154 return NULL;
1155 else
1156 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001157}
1158/**
1159 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1160 * @phba: The HBA for which this call is being executed.
1161 *
1162 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1163 * and returns to caller.
1164 *
1165 * Return codes:
1166 * NULL - Error
1167 * Pointer to lpfc_scsi_buf - Success
1168 **/
1169static struct lpfc_scsi_buf*
1170lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1171{
1172 return phba->lpfc_get_scsi_buf(phba, ndlp);
1173}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001174
James Smart9bad7672008-12-04 22:39:02 -05001175/**
James Smart3772a992009-05-22 14:50:54 -04001176 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -05001177 * @phba: The Hba for which this call is being executed.
1178 * @psb: The scsi buffer which is being released.
1179 *
1180 * This routine releases @psb scsi buffer by adding it to tail of @phba
1181 * lpfc_scsi_buf_list list.
1182 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001183static void
James Smart3772a992009-05-22 14:50:54 -04001184lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001185{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001186 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001187
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001188 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001189 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -05001190 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001191 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -05001192}
1193
James Smart9bad7672008-12-04 22:39:02 -05001194/**
James Smartda0436e2009-05-22 14:51:39 -04001195 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1196 * @phba: The Hba for which this call is being executed.
1197 * @psb: The scsi buffer which is being released.
1198 *
1199 * This routine releases @psb scsi buffer by adding it to tail of @phba
1200 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1201 * and cannot be reused for at least RA_TOV amount of time if it was
1202 * aborted.
1203 **/
1204static void
1205lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1206{
1207 unsigned long iflag = 0;
1208
James Smart341af102010-01-26 23:07:37 -05001209 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -04001210 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1211 iflag);
1212 psb->pCmd = NULL;
1213 list_add_tail(&psb->list,
1214 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1215 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1216 iflag);
1217 } else {
1218
1219 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1220 psb->pCmd = NULL;
1221 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
1222 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
1223 }
1224}
1225
1226/**
James Smart3772a992009-05-22 14:50:54 -04001227 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1228 * @phba: The Hba for which this call is being executed.
1229 * @psb: The scsi buffer which is being released.
1230 *
1231 * This routine releases @psb scsi buffer by adding it to tail of @phba
1232 * lpfc_scsi_buf_list list.
1233 **/
1234static void
1235lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1236{
1237
1238 phba->lpfc_release_scsi_buf(phba, psb);
1239}
1240
1241/**
1242 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001243 * @phba: The Hba for which this call is being executed.
1244 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1245 *
1246 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001247 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1248 * through sg elements and format the bdea. This routine also initializes all
1249 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001250 *
1251 * Return codes:
1252 * 1 - Error
1253 * 0 - Success
1254 **/
dea31012005-04-17 16:05:31 -05001255static int
James Smart3772a992009-05-22 14:50:54 -04001256lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001257{
1258 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1259 struct scatterlist *sgel = NULL;
1260 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1261 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001262 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001263 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001264 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001265 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001266 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001267 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001268
1269 /*
1270 * There are three possibilities here - use scatter-gather segment, use
1271 * the single mapping, or neither. Start the lpfc command prep by
1272 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1273 * data bde entry.
1274 */
1275 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001276 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001277 /*
1278 * The driver stores the segment count returned from pci_map_sg
1279 * because this a count of dma-mappings used to map the use_sg
1280 * pages. They are not guaranteed to be the same for those
1281 * architectures that implement an IOMMU.
1282 */
dea31012005-04-17 16:05:31 -05001283
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001284 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1285 scsi_sg_count(scsi_cmnd), datadir);
1286 if (unlikely(!nseg))
1287 return 1;
1288
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001289 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001290 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001291 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1292 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001293 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001294 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001295 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04001296 lpfc_cmd->seg_cnt = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001297 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001298 return 1;
1299 }
1300
1301 /*
1302 * The driver established a maximum scatter-gather segment count
1303 * during probe that limits the number of sg elements in any
1304 * single scsi command. Just run through the seg_cnt and format
1305 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001306 * When using SLI-3 the driver will try to fit all the BDEs into
1307 * the IOCB. If it can't then the BDEs get added to a BPL as it
1308 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001309 */
James Smart34b02dc2008-08-24 21:49:55 -04001310 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001311 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001312 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001313 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001314 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001315 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1316 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1317 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1318 data_bde->addrLow = putPaddrLow(physaddr);
1319 data_bde->addrHigh = putPaddrHigh(physaddr);
1320 data_bde++;
1321 } else {
1322 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1323 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1324 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1325 bpl->addrLow =
1326 le32_to_cpu(putPaddrLow(physaddr));
1327 bpl->addrHigh =
1328 le32_to_cpu(putPaddrHigh(physaddr));
1329 bpl++;
1330 }
dea31012005-04-17 16:05:31 -05001331 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001332 }
dea31012005-04-17 16:05:31 -05001333
1334 /*
1335 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001336 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1337 * explicitly reinitialized and for SLI-3 the extended bde count is
1338 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001339 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001340 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001341 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1342 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001343 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1344 /*
1345 * The extended IOCB format can only fit 3 BDE or a BPL.
1346 * This I/O has more than 3 BDE so the 1st data bde will
1347 * be a BPL that is filled in here.
1348 */
1349 physaddr = lpfc_cmd->dma_handle;
1350 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1351 data_bde->tus.f.bdeSize = (num_bde *
1352 sizeof(struct ulp_bde64));
1353 physaddr += (sizeof(struct fcp_cmnd) +
1354 sizeof(struct fcp_rsp) +
1355 (2 * sizeof(struct ulp_bde64)));
1356 data_bde->addrHigh = putPaddrHigh(physaddr);
1357 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001358 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -04001359 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1360 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001361 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -04001362 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1363 }
1364 } else {
1365 iocb_cmd->un.fcpi64.bdl.bdeSize =
1366 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001367 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001368 }
James Smart09372822008-01-11 01:52:54 -05001369 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001370
1371 /*
1372 * Due to difference in data length between DIF/non-DIF paths,
1373 * we need to set word 4 of IOCB here
1374 */
James Smarta257bf92009-04-06 18:48:10 -04001375 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001376 return 0;
1377}
1378
James Smartf9bb2da2011-10-10 21:34:11 -04001379static inline unsigned
1380lpfc_cmd_blksize(struct scsi_cmnd *sc)
1381{
1382 return sc->device->sector_size;
1383}
1384
1385#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001386
James Smart9a6b09c2012-03-01 22:37:42 -05001387/* Return if if error injection is detected by Initiator */
1388#define BG_ERR_INIT 0x1
1389/* Return if if error injection is detected by Target */
1390#define BG_ERR_TGT 0x2
1391/* Return if if swapping CSUM<-->CRC is required for error injection */
1392#define BG_ERR_SWAP 0x10
1393/* Return if disabling Guard/Ref/App checking is required for error injection */
1394#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -05001395
1396/**
1397 * lpfc_bg_err_inject - Determine if we should inject an error
1398 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -04001399 * @sc: The SCSI command to examine
1400 * @reftag: (out) BlockGuard reference tag for transmitted data
1401 * @apptag: (out) BlockGuard application tag for transmitted data
1402 * @new_guard (in) Value to replace CRC with if needed
1403 *
James Smart9a6b09c2012-03-01 22:37:42 -05001404 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -05001405 **/
James Smartf9bb2da2011-10-10 21:34:11 -04001406static int
1407lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1408 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1409{
1410 struct scatterlist *sgpe; /* s/g prot entry */
1411 struct scatterlist *sgde; /* s/g data entry */
James Smart9a6b09c2012-03-01 22:37:42 -05001412 struct lpfc_scsi_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -05001413 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -05001414 struct lpfc_nodelist *ndlp;
1415 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -04001416 uint32_t op = scsi_get_prot_op(sc);
1417 uint32_t blksize;
1418 uint32_t numblks;
1419 sector_t lba;
1420 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001421 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001422
1423 if (op == SCSI_PROT_NORMAL)
1424 return 0;
1425
James Smartacd68592012-01-18 16:25:09 -05001426 sgpe = scsi_prot_sglist(sc);
1427 sgde = scsi_sglist(sc);
James Smartf9bb2da2011-10-10 21:34:11 -04001428 lba = scsi_get_lba(sc);
James Smart4ac9b222012-03-01 22:38:29 -05001429
1430 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001431 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1432 blksize = lpfc_cmd_blksize(sc);
1433 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1434
1435 /* Make sure we have the right LBA if one is specified */
1436 if ((phba->lpfc_injerr_lba < lba) ||
1437 (phba->lpfc_injerr_lba >= (lba + numblks)))
1438 return 0;
James Smartacd68592012-01-18 16:25:09 -05001439 if (sgpe) {
1440 blockoff = phba->lpfc_injerr_lba - lba;
1441 numblks = sg_dma_len(sgpe) /
1442 sizeof(struct scsi_dif_tuple);
1443 if (numblks < blockoff)
1444 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001445 }
James Smartf9bb2da2011-10-10 21:34:11 -04001446 }
1447
James Smart4ac9b222012-03-01 22:38:29 -05001448 /* Next check if we need to match the remote NPortID or WWPN */
1449 rdata = sc->device->hostdata;
1450 if (rdata && rdata->pnode) {
1451 ndlp = rdata->pnode;
1452
1453 /* Make sure we have the right NPortID if one is specified */
1454 if (phba->lpfc_injerr_nportid &&
1455 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1456 return 0;
1457
1458 /*
1459 * Make sure we have the right WWPN if one is specified.
1460 * wwn[0] should be a non-zero NAA in a good WWPN.
1461 */
1462 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1463 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1464 sizeof(struct lpfc_name)) != 0))
1465 return 0;
1466 }
1467
1468 /* Setup a ptr to the protection data if the SCSI host provides it */
1469 if (sgpe) {
1470 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1471 src += blockoff;
1472 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1473 }
1474
James Smartf9bb2da2011-10-10 21:34:11 -04001475 /* Should we change the Reference Tag */
1476 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001477 if (phba->lpfc_injerr_wref_cnt) {
1478 switch (op) {
1479 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001480 if (src) {
1481 /*
1482 * For WRITE_PASS, force the error
1483 * to be sent on the wire. It should
1484 * be detected by the Target.
1485 * If blockoff != 0 error will be
1486 * inserted in middle of the IO.
1487 */
James Smartf9bb2da2011-10-10 21:34:11 -04001488
James Smartacd68592012-01-18 16:25:09 -05001489 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1490 "9076 BLKGRD: Injecting reftag error: "
1491 "write lba x%lx + x%x oldrefTag x%x\n",
1492 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001493 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001494
1495 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001496 * Save the old ref_tag so we can
1497 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001498 */
James Smart9a6b09c2012-03-01 22:37:42 -05001499 if (lpfc_cmd) {
1500 lpfc_cmd->prot_data_type =
1501 LPFC_INJERR_REFTAG;
1502 lpfc_cmd->prot_data_segment =
1503 src;
1504 lpfc_cmd->prot_data =
1505 src->ref_tag;
1506 }
1507 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001508 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001509 if (phba->lpfc_injerr_wref_cnt == 0) {
1510 phba->lpfc_injerr_nportid = 0;
1511 phba->lpfc_injerr_lba =
1512 LPFC_INJERR_LBA_OFF;
1513 memset(&phba->lpfc_injerr_wwpn,
1514 0, sizeof(struct lpfc_name));
1515 }
James Smart9a6b09c2012-03-01 22:37:42 -05001516 rc = BG_ERR_TGT | BG_ERR_CHECK;
1517
James Smartacd68592012-01-18 16:25:09 -05001518 break;
1519 }
1520 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001521 case SCSI_PROT_WRITE_INSERT:
1522 /*
1523 * For WRITE_INSERT, force the error
1524 * to be sent on the wire. It should be
1525 * detected by the Target.
1526 */
1527 /* DEADBEEF will be the reftag on the wire */
1528 *reftag = 0xDEADBEEF;
1529 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001530 if (phba->lpfc_injerr_wref_cnt == 0) {
1531 phba->lpfc_injerr_nportid = 0;
1532 phba->lpfc_injerr_lba =
1533 LPFC_INJERR_LBA_OFF;
1534 memset(&phba->lpfc_injerr_wwpn,
1535 0, sizeof(struct lpfc_name));
1536 }
James Smart9a6b09c2012-03-01 22:37:42 -05001537 rc = BG_ERR_TGT | BG_ERR_CHECK;
1538
1539 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1540 "9078 BLKGRD: Injecting reftag error: "
1541 "write lba x%lx\n", (unsigned long)lba);
1542 break;
James Smartacd68592012-01-18 16:25:09 -05001543 case SCSI_PROT_WRITE_STRIP:
1544 /*
1545 * For WRITE_STRIP and WRITE_PASS,
1546 * force the error on data
1547 * being copied from SLI-Host to SLI-Port.
1548 */
1549 *reftag = 0xDEADBEEF;
1550 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001551 if (phba->lpfc_injerr_wref_cnt == 0) {
1552 phba->lpfc_injerr_nportid = 0;
1553 phba->lpfc_injerr_lba =
1554 LPFC_INJERR_LBA_OFF;
1555 memset(&phba->lpfc_injerr_wwpn,
1556 0, sizeof(struct lpfc_name));
1557 }
James Smartacd68592012-01-18 16:25:09 -05001558 rc = BG_ERR_INIT;
1559
1560 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1561 "9077 BLKGRD: Injecting reftag error: "
1562 "write lba x%lx\n", (unsigned long)lba);
1563 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001564 }
James Smartacd68592012-01-18 16:25:09 -05001565 }
1566 if (phba->lpfc_injerr_rref_cnt) {
1567 switch (op) {
1568 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001569 case SCSI_PROT_READ_STRIP:
1570 case SCSI_PROT_READ_PASS:
1571 /*
1572 * For READ_STRIP and READ_PASS, force the
1573 * error on data being read off the wire. It
1574 * should force an IO error to the driver.
1575 */
James Smartf9bb2da2011-10-10 21:34:11 -04001576 *reftag = 0xDEADBEEF;
1577 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001578 if (phba->lpfc_injerr_rref_cnt == 0) {
1579 phba->lpfc_injerr_nportid = 0;
1580 phba->lpfc_injerr_lba =
1581 LPFC_INJERR_LBA_OFF;
1582 memset(&phba->lpfc_injerr_wwpn,
1583 0, sizeof(struct lpfc_name));
1584 }
James Smartacd68592012-01-18 16:25:09 -05001585 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001586
1587 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001588 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001589 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001590 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001591 }
1592 }
1593 }
1594
1595 /* Should we change the Application Tag */
1596 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001597 if (phba->lpfc_injerr_wapp_cnt) {
1598 switch (op) {
1599 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001600 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001601 /*
1602 * For WRITE_PASS, force the error
1603 * to be sent on the wire. It should
1604 * be detected by the Target.
1605 * If blockoff != 0 error will be
1606 * inserted in middle of the IO.
1607 */
1608
James Smartacd68592012-01-18 16:25:09 -05001609 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1610 "9080 BLKGRD: Injecting apptag error: "
1611 "write lba x%lx + x%x oldappTag x%x\n",
1612 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001613 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001614
1615 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001616 * Save the old app_tag so we can
1617 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001618 */
James Smart9a6b09c2012-03-01 22:37:42 -05001619 if (lpfc_cmd) {
1620 lpfc_cmd->prot_data_type =
1621 LPFC_INJERR_APPTAG;
1622 lpfc_cmd->prot_data_segment =
1623 src;
1624 lpfc_cmd->prot_data =
1625 src->app_tag;
1626 }
1627 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001628 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001629 if (phba->lpfc_injerr_wapp_cnt == 0) {
1630 phba->lpfc_injerr_nportid = 0;
1631 phba->lpfc_injerr_lba =
1632 LPFC_INJERR_LBA_OFF;
1633 memset(&phba->lpfc_injerr_wwpn,
1634 0, sizeof(struct lpfc_name));
1635 }
James Smart9a6b09c2012-03-01 22:37:42 -05001636 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001637 break;
1638 }
1639 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001640 case SCSI_PROT_WRITE_INSERT:
1641 /*
1642 * For WRITE_INSERT, force the
1643 * error to be sent on the wire. It should be
1644 * detected by the Target.
1645 */
1646 /* DEAD will be the apptag on the wire */
1647 *apptag = 0xDEAD;
1648 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001649 if (phba->lpfc_injerr_wapp_cnt == 0) {
1650 phba->lpfc_injerr_nportid = 0;
1651 phba->lpfc_injerr_lba =
1652 LPFC_INJERR_LBA_OFF;
1653 memset(&phba->lpfc_injerr_wwpn,
1654 0, sizeof(struct lpfc_name));
1655 }
James Smart9a6b09c2012-03-01 22:37:42 -05001656 rc = BG_ERR_TGT | BG_ERR_CHECK;
1657
1658 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1659 "0813 BLKGRD: Injecting apptag error: "
1660 "write lba x%lx\n", (unsigned long)lba);
1661 break;
James Smartacd68592012-01-18 16:25:09 -05001662 case SCSI_PROT_WRITE_STRIP:
1663 /*
1664 * For WRITE_STRIP and WRITE_PASS,
1665 * force the error on data
1666 * being copied from SLI-Host to SLI-Port.
1667 */
1668 *apptag = 0xDEAD;
1669 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001670 if (phba->lpfc_injerr_wapp_cnt == 0) {
1671 phba->lpfc_injerr_nportid = 0;
1672 phba->lpfc_injerr_lba =
1673 LPFC_INJERR_LBA_OFF;
1674 memset(&phba->lpfc_injerr_wwpn,
1675 0, sizeof(struct lpfc_name));
1676 }
James Smartacd68592012-01-18 16:25:09 -05001677 rc = BG_ERR_INIT;
1678
1679 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1680 "0812 BLKGRD: Injecting apptag error: "
1681 "write lba x%lx\n", (unsigned long)lba);
1682 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001683 }
James Smartacd68592012-01-18 16:25:09 -05001684 }
1685 if (phba->lpfc_injerr_rapp_cnt) {
1686 switch (op) {
1687 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001688 case SCSI_PROT_READ_STRIP:
1689 case SCSI_PROT_READ_PASS:
1690 /*
1691 * For READ_STRIP and READ_PASS, force the
1692 * error on data being read off the wire. It
1693 * should force an IO error to the driver.
1694 */
James Smartf9bb2da2011-10-10 21:34:11 -04001695 *apptag = 0xDEAD;
1696 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001697 if (phba->lpfc_injerr_rapp_cnt == 0) {
1698 phba->lpfc_injerr_nportid = 0;
1699 phba->lpfc_injerr_lba =
1700 LPFC_INJERR_LBA_OFF;
1701 memset(&phba->lpfc_injerr_wwpn,
1702 0, sizeof(struct lpfc_name));
1703 }
James Smartacd68592012-01-18 16:25:09 -05001704 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001705
1706 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001707 "0814 BLKGRD: Injecting apptag error: "
1708 "read lba x%lx\n", (unsigned long)lba);
1709 break;
1710 }
1711 }
1712 }
1713
1714
1715 /* Should we change the Guard Tag */
1716 if (new_guard) {
1717 if (phba->lpfc_injerr_wgrd_cnt) {
1718 switch (op) {
1719 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001720 rc = BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001721 /* Drop thru */
James Smartacd68592012-01-18 16:25:09 -05001722
James Smartacd68592012-01-18 16:25:09 -05001723 case SCSI_PROT_WRITE_INSERT:
1724 /*
1725 * For WRITE_INSERT, force the
1726 * error to be sent on the wire. It should be
1727 * detected by the Target.
1728 */
1729 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001730 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1731 phba->lpfc_injerr_nportid = 0;
1732 phba->lpfc_injerr_lba =
1733 LPFC_INJERR_LBA_OFF;
1734 memset(&phba->lpfc_injerr_wwpn,
1735 0, sizeof(struct lpfc_name));
1736 }
James Smartacd68592012-01-18 16:25:09 -05001737
James Smart9a6b09c2012-03-01 22:37:42 -05001738 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001739 /* Signals the caller to swap CRC->CSUM */
1740
1741 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1742 "0817 BLKGRD: Injecting guard error: "
1743 "write lba x%lx\n", (unsigned long)lba);
1744 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001745 case SCSI_PROT_WRITE_STRIP:
1746 /*
1747 * For WRITE_STRIP and WRITE_PASS,
1748 * force the error on data
1749 * being copied from SLI-Host to SLI-Port.
1750 */
1751 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001752 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1753 phba->lpfc_injerr_nportid = 0;
1754 phba->lpfc_injerr_lba =
1755 LPFC_INJERR_LBA_OFF;
1756 memset(&phba->lpfc_injerr_wwpn,
1757 0, sizeof(struct lpfc_name));
1758 }
James Smart9a6b09c2012-03-01 22:37:42 -05001759
1760 rc = BG_ERR_INIT | BG_ERR_SWAP;
1761 /* Signals the caller to swap CRC->CSUM */
1762
1763 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1764 "0816 BLKGRD: Injecting guard error: "
1765 "write lba x%lx\n", (unsigned long)lba);
1766 break;
James Smartacd68592012-01-18 16:25:09 -05001767 }
1768 }
1769 if (phba->lpfc_injerr_rgrd_cnt) {
1770 switch (op) {
1771 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001772 case SCSI_PROT_READ_STRIP:
1773 case SCSI_PROT_READ_PASS:
1774 /*
1775 * For READ_STRIP and READ_PASS, force the
1776 * error on data being read off the wire. It
1777 * should force an IO error to the driver.
1778 */
James Smartacd68592012-01-18 16:25:09 -05001779 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001780 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1781 phba->lpfc_injerr_nportid = 0;
1782 phba->lpfc_injerr_lba =
1783 LPFC_INJERR_LBA_OFF;
1784 memset(&phba->lpfc_injerr_wwpn,
1785 0, sizeof(struct lpfc_name));
1786 }
James Smartacd68592012-01-18 16:25:09 -05001787
James Smart9a6b09c2012-03-01 22:37:42 -05001788 rc = BG_ERR_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001789 /* Signals the caller to swap CRC->CSUM */
1790
1791 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1792 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001793 "read lba x%lx\n", (unsigned long)lba);
1794 }
1795 }
1796 }
1797
James Smartf9bb2da2011-10-10 21:34:11 -04001798 return rc;
1799}
1800#endif
1801
James Smartacd68592012-01-18 16:25:09 -05001802/**
1803 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1804 * the specified SCSI command.
1805 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001806 * @sc: The SCSI command to examine
1807 * @txopt: (out) BlockGuard operation for transmitted data
1808 * @rxopt: (out) BlockGuard operation for received data
1809 *
1810 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1811 *
James Smartacd68592012-01-18 16:25:09 -05001812 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001813static int
James Smart6c8eea52010-04-06 14:49:53 -04001814lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1815 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001816{
1817 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
James Smart6c8eea52010-04-06 14:49:53 -04001818 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001819
1820 if (guard_type == SHOST_DIX_GUARD_IP) {
1821 switch (scsi_get_prot_op(sc)) {
1822 case SCSI_PROT_READ_INSERT:
1823 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001824 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001825 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001826 break;
1827
1828 case SCSI_PROT_READ_STRIP:
1829 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001830 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001831 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001832 break;
1833
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001834 case SCSI_PROT_READ_PASS:
1835 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001836 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001837 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001838 break;
1839
James Smarte2a0a9d2008-12-04 22:40:02 -05001840 case SCSI_PROT_NORMAL:
1841 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001842 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001843 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1844 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001845 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001846 break;
1847
1848 }
James Smart7c56b9f2011-07-22 18:36:25 -04001849 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001850 switch (scsi_get_prot_op(sc)) {
1851 case SCSI_PROT_READ_STRIP:
1852 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001853 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001854 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001855 break;
1856
1857 case SCSI_PROT_READ_PASS:
1858 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001859 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001860 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001861 break;
1862
James Smarte2a0a9d2008-12-04 22:40:02 -05001863 case SCSI_PROT_READ_INSERT:
1864 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001865 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001866 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001867 break;
1868
James Smarte2a0a9d2008-12-04 22:40:02 -05001869 case SCSI_PROT_NORMAL:
1870 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001871 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001872 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1873 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001874 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001875 break;
1876 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001877 }
1878
James Smart6c8eea52010-04-06 14:49:53 -04001879 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001880}
1881
James Smartacd68592012-01-18 16:25:09 -05001882#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1883/**
1884 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1885 * the specified SCSI command in order to force a guard tag error.
1886 * @phba: The Hba for which this call is being executed.
1887 * @sc: The SCSI command to examine
1888 * @txopt: (out) BlockGuard operation for transmitted data
1889 * @rxopt: (out) BlockGuard operation for received data
1890 *
1891 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1892 *
1893 **/
1894static int
1895lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1896 uint8_t *txop, uint8_t *rxop)
1897{
1898 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
1899 uint8_t ret = 0;
1900
1901 if (guard_type == SHOST_DIX_GUARD_IP) {
1902 switch (scsi_get_prot_op(sc)) {
1903 case SCSI_PROT_READ_INSERT:
1904 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001905 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001906 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001907 break;
1908
1909 case SCSI_PROT_READ_STRIP:
1910 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001911 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001912 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001913 break;
1914
1915 case SCSI_PROT_READ_PASS:
1916 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001917 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001918 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001919 break;
1920
1921 case SCSI_PROT_NORMAL:
1922 default:
1923 break;
1924
1925 }
1926 } else {
1927 switch (scsi_get_prot_op(sc)) {
1928 case SCSI_PROT_READ_STRIP:
1929 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001930 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001931 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001932 break;
1933
1934 case SCSI_PROT_READ_PASS:
1935 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001936 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05001937 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001938 break;
1939
1940 case SCSI_PROT_READ_INSERT:
1941 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001942 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001943 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001944 break;
1945
1946 case SCSI_PROT_NORMAL:
1947 default:
1948 break;
1949 }
1950 }
1951
1952 return ret;
1953}
1954#endif
1955
1956/**
1957 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1958 * @phba: The Hba for which this call is being executed.
1959 * @sc: pointer to scsi command we're working on
1960 * @bpl: pointer to buffer list for protection groups
1961 * @datacnt: number of segments of data that have been dma mapped
1962 *
1963 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05001964 * type LPFC_PG_TYPE_NO_DIF
1965 *
1966 * This is usually used when the HBA is instructed to generate
1967 * DIFs and insert them into data stream (or strip DIF from
1968 * incoming data stream)
1969 *
1970 * The buffer list consists of just one protection group described
1971 * below:
1972 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001973 * start of prot group --> | PDE_5 |
1974 * +-------------------------+
1975 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001976 * +-------------------------+
1977 * | Data BDE |
1978 * +-------------------------+
1979 * |more Data BDE's ... (opt)|
1980 * +-------------------------+
1981 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001982 *
1983 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05001984 *
1985 * Returns the number of BDEs added to the BPL.
1986 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001987static int
1988lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1989 struct ulp_bde64 *bpl, int datasegcnt)
1990{
1991 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001992 struct lpfc_pde5 *pde5 = NULL;
1993 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001994 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001995 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001996 int datadir = sc->sc_data_direction;
James Smart0829a192012-05-09 21:18:12 -04001997#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001998 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04001999#endif
James Smartacd68592012-01-18 16:25:09 -05002000 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002001 uint32_t reftag;
James Smart7c56b9f2011-07-22 18:36:25 -04002002 unsigned blksize;
James Smart6c8eea52010-04-06 14:49:53 -04002003 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002004
James Smart6c8eea52010-04-06 14:49:53 -04002005 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2006 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002007 goto out;
2008
James Smart6c8eea52010-04-06 14:49:53 -04002009 /* extract some info from the scsi command for pde*/
James Smarte2a0a9d2008-12-04 22:40:02 -05002010 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002011 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002012
James Smartf9bb2da2011-10-10 21:34:11 -04002013#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002014 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002015 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002016 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002017 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002018 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002019 checking = 0;
2020 }
James Smartf9bb2da2011-10-10 21:34:11 -04002021#endif
2022
James Smart6c8eea52010-04-06 14:49:53 -04002023 /* setup PDE5 with what we have */
2024 pde5 = (struct lpfc_pde5 *) bpl;
2025 memset(pde5, 0, sizeof(struct lpfc_pde5));
2026 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002027
James Smartbc739052010-08-04 16:11:18 -04002028 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002029 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002030 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002031
James Smart6c8eea52010-04-06 14:49:53 -04002032 /* advance bpl and increment bde count */
2033 num_bde++;
2034 bpl++;
2035 pde6 = (struct lpfc_pde6 *) bpl;
2036
2037 /* setup PDE6 with the rest of the info */
2038 memset(pde6, 0, sizeof(struct lpfc_pde6));
2039 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2040 bf_set(pde6_optx, pde6, txop);
2041 bf_set(pde6_oprx, pde6, rxop);
2042 if (datadir == DMA_FROM_DEVICE) {
James Smartacd68592012-01-18 16:25:09 -05002043 bf_set(pde6_ce, pde6, checking);
2044 bf_set(pde6_re, pde6, checking);
James Smart6c8eea52010-04-06 14:49:53 -04002045 }
2046 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002047 bf_set(pde6_ae, pde6, 0);
2048 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002049
James Smartbc739052010-08-04 16:11:18 -04002050 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002051 pde6->word0 = cpu_to_le32(pde6->word0);
2052 pde6->word1 = cpu_to_le32(pde6->word1);
2053 pde6->word2 = cpu_to_le32(pde6->word2);
2054
James Smart6c8eea52010-04-06 14:49:53 -04002055 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002056 num_bde++;
2057 bpl++;
2058
2059 /* assumption: caller has already run dma_map_sg on command data */
2060 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2061 physaddr = sg_dma_address(sgde);
2062 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
2063 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2064 bpl->tus.f.bdeSize = sg_dma_len(sgde);
2065 if (datadir == DMA_TO_DEVICE)
2066 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2067 else
2068 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2069 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2070 bpl++;
2071 num_bde++;
2072 }
2073
2074out:
2075 return num_bde;
2076}
2077
James Smartacd68592012-01-18 16:25:09 -05002078/**
2079 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
2080 * @phba: The Hba for which this call is being executed.
2081 * @sc: pointer to scsi command we're working on
2082 * @bpl: pointer to buffer list for protection groups
2083 * @datacnt: number of segments of data that have been dma mapped
2084 * @protcnt: number of segment of protection data that have been dma mapped
2085 *
2086 * This function sets up BPL buffer list for protection groups of
2087 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05002088 *
2089 * This is usually used when DIFs are in their own buffers,
2090 * separate from the data. The HBA can then by instructed
2091 * to place the DIFs in the outgoing stream. For read operations,
2092 * The HBA could extract the DIFs and place it in DIF buffers.
2093 *
2094 * The buffer list for this type consists of one or more of the
2095 * protection groups described below:
2096 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002097 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002098 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002099 * | PDE_6 |
2100 * +-------------------------+
2101 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05002102 * +-------------------------+
2103 * | Data BDE |
2104 * +-------------------------+
2105 * |more Data BDE's ... (opt)|
2106 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002107 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002108 * +-------------------------+
2109 * | ... |
2110 * +-------------------------+
2111 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002112 * Note: It is assumed that both data and protection s/g buffers have been
2113 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05002114 *
2115 * Returns the number of BDEs added to the BPL.
2116 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002117static int
2118lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2119 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2120{
2121 struct scatterlist *sgde = NULL; /* s/g data entry */
2122 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04002123 struct lpfc_pde5 *pde5 = NULL;
2124 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05002125 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002126 dma_addr_t dataphysaddr, protphysaddr;
2127 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05002128 unsigned int split_offset;
2129 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05002130 unsigned int protgrp_blks, protgrp_bytes;
2131 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04002132 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002133 int datadir = sc->sc_data_direction;
2134 unsigned char pgdone = 0, alldone = 0;
2135 unsigned blksize;
James Smart0829a192012-05-09 21:18:12 -04002136#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002137 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002138#endif
James Smartacd68592012-01-18 16:25:09 -05002139 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002140 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04002141 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002142 int num_bde = 0;
2143
2144 sgpe = scsi_prot_sglist(sc);
2145 sgde = scsi_sglist(sc);
2146
2147 if (!sgpe || !sgde) {
2148 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2149 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2150 sgpe, sgde);
2151 return 0;
2152 }
2153
James Smart6c8eea52010-04-06 14:49:53 -04002154 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2155 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002156 goto out;
2157
James Smart6c8eea52010-04-06 14:49:53 -04002158 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05002159 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002160 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002161
James Smartf9bb2da2011-10-10 21:34:11 -04002162#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002163 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002164 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002165 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002166 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002167 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002168 checking = 0;
2169 }
James Smartf9bb2da2011-10-10 21:34:11 -04002170#endif
2171
James Smarte2a0a9d2008-12-04 22:40:02 -05002172 split_offset = 0;
2173 do {
James Smart96f70772013-04-17 20:16:15 -04002174 /* Check to see if we ran out of space */
2175 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
2176 return num_bde + 3;
2177
James Smart6c8eea52010-04-06 14:49:53 -04002178 /* setup PDE5 with what we have */
2179 pde5 = (struct lpfc_pde5 *) bpl;
2180 memset(pde5, 0, sizeof(struct lpfc_pde5));
2181 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002182
James Smartbc739052010-08-04 16:11:18 -04002183 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002184 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002185 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002186
James Smart6c8eea52010-04-06 14:49:53 -04002187 /* advance bpl and increment bde count */
2188 num_bde++;
2189 bpl++;
2190 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002191
James Smart6c8eea52010-04-06 14:49:53 -04002192 /* setup PDE6 with the rest of the info */
2193 memset(pde6, 0, sizeof(struct lpfc_pde6));
2194 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2195 bf_set(pde6_optx, pde6, txop);
2196 bf_set(pde6_oprx, pde6, rxop);
James Smartacd68592012-01-18 16:25:09 -05002197 bf_set(pde6_ce, pde6, checking);
2198 bf_set(pde6_re, pde6, checking);
James Smart6c8eea52010-04-06 14:49:53 -04002199 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002200 bf_set(pde6_ae, pde6, 0);
2201 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002202
James Smartbc739052010-08-04 16:11:18 -04002203 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002204 pde6->word0 = cpu_to_le32(pde6->word0);
2205 pde6->word1 = cpu_to_le32(pde6->word1);
2206 pde6->word2 = cpu_to_le32(pde6->word2);
2207
James Smart6c8eea52010-04-06 14:49:53 -04002208 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002209 num_bde++;
2210 bpl++;
2211
2212 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05002213 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2214 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05002215
James Smarte2a0a9d2008-12-04 22:40:02 -05002216 /* must be integer multiple of the DIF block length */
2217 BUG_ON(protgroup_len % 8);
2218
James Smart7f860592011-03-11 16:05:52 -05002219 pde7 = (struct lpfc_pde7 *) bpl;
2220 memset(pde7, 0, sizeof(struct lpfc_pde7));
2221 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2222
James Smart7c56b9f2011-07-22 18:36:25 -04002223 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2224 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05002225
James Smarte2a0a9d2008-12-04 22:40:02 -05002226 protgrp_blks = protgroup_len / 8;
2227 protgrp_bytes = protgrp_blks * blksize;
2228
James Smart7f860592011-03-11 16:05:52 -05002229 /* check if this pde is crossing the 4K boundary; if so split */
2230 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2231 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2232 protgroup_offset += protgroup_remainder;
2233 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04002234 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05002235 } else {
2236 protgroup_offset = 0;
2237 curr_prot++;
2238 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002239
James Smarte2a0a9d2008-12-04 22:40:02 -05002240 num_bde++;
2241
2242 /* setup BDE's for data blocks associated with DIF data */
2243 pgdone = 0;
2244 subtotal = 0; /* total bytes processed for current prot grp */
2245 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002246 /* Check to see if we ran out of space */
2247 if (num_bde >= phba->cfg_total_seg_cnt)
2248 return num_bde + 1;
2249
James Smarte2a0a9d2008-12-04 22:40:02 -05002250 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04002251 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2252 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002253 __func__);
2254 return 0;
2255 }
2256 bpl++;
2257 dataphysaddr = sg_dma_address(sgde) + split_offset;
2258 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2259 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2260
2261 remainder = sg_dma_len(sgde) - split_offset;
2262
2263 if ((subtotal + remainder) <= protgrp_bytes) {
2264 /* we can use this whole buffer */
2265 bpl->tus.f.bdeSize = remainder;
2266 split_offset = 0;
2267
2268 if ((subtotal + remainder) == protgrp_bytes)
2269 pgdone = 1;
2270 } else {
2271 /* must split this buffer with next prot grp */
2272 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2273 split_offset += bpl->tus.f.bdeSize;
2274 }
2275
2276 subtotal += bpl->tus.f.bdeSize;
2277
2278 if (datadir == DMA_TO_DEVICE)
2279 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2280 else
2281 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2282 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2283
2284 num_bde++;
2285 curr_data++;
2286
2287 if (split_offset)
2288 break;
2289
2290 /* Move to the next s/g segment if possible */
2291 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04002292
James Smarte2a0a9d2008-12-04 22:40:02 -05002293 }
2294
James Smart7f860592011-03-11 16:05:52 -05002295 if (protgroup_offset) {
2296 /* update the reference tag */
2297 reftag += protgrp_blks;
2298 bpl++;
2299 continue;
2300 }
2301
James Smarte2a0a9d2008-12-04 22:40:02 -05002302 /* are we done ? */
2303 if (curr_prot == protcnt) {
2304 alldone = 1;
2305 } else if (curr_prot < protcnt) {
2306 /* advance to next prot buffer */
2307 sgpe = sg_next(sgpe);
2308 bpl++;
2309
2310 /* update the reference tag */
2311 reftag += protgrp_blks;
2312 } else {
2313 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04002314 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2315 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05002316 }
2317
2318 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05002319out:
2320
James Smarte2a0a9d2008-12-04 22:40:02 -05002321 return num_bde;
2322}
James Smart7f860592011-03-11 16:05:52 -05002323
James Smartacd68592012-01-18 16:25:09 -05002324/**
2325 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2326 * @phba: The Hba for which this call is being executed.
2327 * @sc: pointer to scsi command we're working on
2328 * @sgl: pointer to buffer list for protection groups
2329 * @datacnt: number of segments of data that have been dma mapped
2330 *
2331 * This function sets up SGL buffer list for protection groups of
2332 * type LPFC_PG_TYPE_NO_DIF
2333 *
2334 * This is usually used when the HBA is instructed to generate
2335 * DIFs and insert them into data stream (or strip DIF from
2336 * incoming data stream)
2337 *
2338 * The buffer list consists of just one protection group described
2339 * below:
2340 * +-------------------------+
2341 * start of prot group --> | DI_SEED |
2342 * +-------------------------+
2343 * | Data SGE |
2344 * +-------------------------+
2345 * |more Data SGE's ... (opt)|
2346 * +-------------------------+
2347 *
2348 *
2349 * Note: Data s/g buffers have been dma mapped
2350 *
2351 * Returns the number of SGEs added to the SGL.
2352 **/
2353static int
2354lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2355 struct sli4_sge *sgl, int datasegcnt)
2356{
2357 struct scatterlist *sgde = NULL; /* s/g data entry */
2358 struct sli4_sge_diseed *diseed = NULL;
2359 dma_addr_t physaddr;
2360 int i = 0, num_sge = 0, status;
2361 int datadir = sc->sc_data_direction;
2362 uint32_t reftag;
2363 unsigned blksize;
2364 uint8_t txop, rxop;
James Smart0829a192012-05-09 21:18:12 -04002365#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002366 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002367#endif
James Smartacd68592012-01-18 16:25:09 -05002368 uint32_t checking = 1;
2369 uint32_t dma_len;
2370 uint32_t dma_offset = 0;
2371
2372 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2373 if (status)
2374 goto out;
2375
2376 /* extract some info from the scsi command for pde*/
2377 blksize = lpfc_cmd_blksize(sc);
2378 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2379
2380#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002381 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002382 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002383 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002384 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002385 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002386 checking = 0;
2387 }
2388#endif
2389
2390 /* setup DISEED with what we have */
2391 diseed = (struct sli4_sge_diseed *) sgl;
2392 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2393 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2394
2395 /* Endianness conversion if necessary */
2396 diseed->ref_tag = cpu_to_le32(reftag);
2397 diseed->ref_tag_tran = diseed->ref_tag;
2398
2399 /* setup DISEED with the rest of the info */
2400 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2401 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2402 if (datadir == DMA_FROM_DEVICE) {
2403 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2404 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2405 }
2406 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2407 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2408
2409 /* Endianness conversion if necessary for DISEED */
2410 diseed->word2 = cpu_to_le32(diseed->word2);
2411 diseed->word3 = cpu_to_le32(diseed->word3);
2412
2413 /* advance bpl and increment sge count */
2414 num_sge++;
2415 sgl++;
2416
2417 /* assumption: caller has already run dma_map_sg on command data */
2418 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2419 physaddr = sg_dma_address(sgde);
2420 dma_len = sg_dma_len(sgde);
2421 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2422 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2423 if ((i + 1) == datasegcnt)
2424 bf_set(lpfc_sli4_sge_last, sgl, 1);
2425 else
2426 bf_set(lpfc_sli4_sge_last, sgl, 0);
2427 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2428 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2429
2430 sgl->sge_len = cpu_to_le32(dma_len);
2431 dma_offset += dma_len;
2432
2433 sgl++;
2434 num_sge++;
2435 }
2436
2437out:
2438 return num_sge;
2439}
2440
2441/**
2442 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2443 * @phba: The Hba for which this call is being executed.
2444 * @sc: pointer to scsi command we're working on
2445 * @sgl: pointer to buffer list for protection groups
2446 * @datacnt: number of segments of data that have been dma mapped
2447 * @protcnt: number of segment of protection data that have been dma mapped
2448 *
2449 * This function sets up SGL buffer list for protection groups of
2450 * type LPFC_PG_TYPE_DIF
2451 *
2452 * This is usually used when DIFs are in their own buffers,
2453 * separate from the data. The HBA can then by instructed
2454 * to place the DIFs in the outgoing stream. For read operations,
2455 * The HBA could extract the DIFs and place it in DIF buffers.
2456 *
2457 * The buffer list for this type consists of one or more of the
2458 * protection groups described below:
2459 * +-------------------------+
2460 * start of first prot group --> | DISEED |
2461 * +-------------------------+
2462 * | DIF (Prot SGE) |
2463 * +-------------------------+
2464 * | Data SGE |
2465 * +-------------------------+
2466 * |more Data SGE's ... (opt)|
2467 * +-------------------------+
2468 * start of new prot group --> | DISEED |
2469 * +-------------------------+
2470 * | ... |
2471 * +-------------------------+
2472 *
2473 * Note: It is assumed that both data and protection s/g buffers have been
2474 * mapped for DMA
2475 *
2476 * Returns the number of SGEs added to the SGL.
2477 **/
2478static int
2479lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2480 struct sli4_sge *sgl, int datacnt, int protcnt)
2481{
2482 struct scatterlist *sgde = NULL; /* s/g data entry */
2483 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2484 struct sli4_sge_diseed *diseed = NULL;
2485 dma_addr_t dataphysaddr, protphysaddr;
2486 unsigned short curr_data = 0, curr_prot = 0;
2487 unsigned int split_offset;
2488 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2489 unsigned int protgrp_blks, protgrp_bytes;
2490 unsigned int remainder, subtotal;
2491 int status;
2492 unsigned char pgdone = 0, alldone = 0;
2493 unsigned blksize;
2494 uint32_t reftag;
2495 uint8_t txop, rxop;
2496 uint32_t dma_len;
James Smart0829a192012-05-09 21:18:12 -04002497#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002498 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002499#endif
James Smartacd68592012-01-18 16:25:09 -05002500 uint32_t checking = 1;
2501 uint32_t dma_offset = 0;
2502 int num_sge = 0;
2503
2504 sgpe = scsi_prot_sglist(sc);
2505 sgde = scsi_sglist(sc);
2506
2507 if (!sgpe || !sgde) {
2508 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2509 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2510 sgpe, sgde);
2511 return 0;
2512 }
2513
2514 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2515 if (status)
2516 goto out;
2517
2518 /* extract some info from the scsi command */
2519 blksize = lpfc_cmd_blksize(sc);
2520 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2521
2522#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002523 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002524 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002525 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002526 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002527 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002528 checking = 0;
2529 }
2530#endif
2531
2532 split_offset = 0;
2533 do {
James Smart96f70772013-04-17 20:16:15 -04002534 /* Check to see if we ran out of space */
2535 if (num_sge >= (phba->cfg_total_seg_cnt - 2))
2536 return num_sge + 3;
2537
James Smartacd68592012-01-18 16:25:09 -05002538 /* setup DISEED with what we have */
2539 diseed = (struct sli4_sge_diseed *) sgl;
2540 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2541 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2542
2543 /* Endianness conversion if necessary */
2544 diseed->ref_tag = cpu_to_le32(reftag);
2545 diseed->ref_tag_tran = diseed->ref_tag;
2546
2547 /* setup DISEED with the rest of the info */
2548 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2549 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2550 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2551 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2552 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2553 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2554
2555 /* Endianness conversion if necessary for DISEED */
2556 diseed->word2 = cpu_to_le32(diseed->word2);
2557 diseed->word3 = cpu_to_le32(diseed->word3);
2558
2559 /* advance sgl and increment bde count */
2560 num_sge++;
2561 sgl++;
2562
2563 /* setup the first BDE that points to protection buffer */
2564 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2565 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2566
2567 /* must be integer multiple of the DIF block length */
2568 BUG_ON(protgroup_len % 8);
2569
2570 /* Now setup DIF SGE */
2571 sgl->word2 = 0;
2572 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2573 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2574 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2575 sgl->word2 = cpu_to_le32(sgl->word2);
2576
2577 protgrp_blks = protgroup_len / 8;
2578 protgrp_bytes = protgrp_blks * blksize;
2579
2580 /* check if DIF SGE is crossing the 4K boundary; if so split */
2581 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2582 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2583 protgroup_offset += protgroup_remainder;
2584 protgrp_blks = protgroup_remainder / 8;
2585 protgrp_bytes = protgrp_blks * blksize;
2586 } else {
2587 protgroup_offset = 0;
2588 curr_prot++;
2589 }
2590
2591 num_sge++;
2592
2593 /* setup SGE's for data blocks associated with DIF data */
2594 pgdone = 0;
2595 subtotal = 0; /* total bytes processed for current prot grp */
2596 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002597 /* Check to see if we ran out of space */
2598 if (num_sge >= phba->cfg_total_seg_cnt)
2599 return num_sge + 1;
2600
James Smartacd68592012-01-18 16:25:09 -05002601 if (!sgde) {
2602 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2603 "9086 BLKGRD:%s Invalid data segment\n",
2604 __func__);
2605 return 0;
2606 }
2607 sgl++;
2608 dataphysaddr = sg_dma_address(sgde) + split_offset;
2609
2610 remainder = sg_dma_len(sgde) - split_offset;
2611
2612 if ((subtotal + remainder) <= protgrp_bytes) {
2613 /* we can use this whole buffer */
2614 dma_len = remainder;
2615 split_offset = 0;
2616
2617 if ((subtotal + remainder) == protgrp_bytes)
2618 pgdone = 1;
2619 } else {
2620 /* must split this buffer with next prot grp */
2621 dma_len = protgrp_bytes - subtotal;
2622 split_offset += dma_len;
2623 }
2624
2625 subtotal += dma_len;
2626
2627 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2628 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2629 bf_set(lpfc_sli4_sge_last, sgl, 0);
2630 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2631 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2632
2633 sgl->sge_len = cpu_to_le32(dma_len);
2634 dma_offset += dma_len;
2635
2636 num_sge++;
2637 curr_data++;
2638
2639 if (split_offset)
2640 break;
2641
2642 /* Move to the next s/g segment if possible */
2643 sgde = sg_next(sgde);
2644 }
2645
2646 if (protgroup_offset) {
2647 /* update the reference tag */
2648 reftag += protgrp_blks;
2649 sgl++;
2650 continue;
2651 }
2652
2653 /* are we done ? */
2654 if (curr_prot == protcnt) {
2655 bf_set(lpfc_sli4_sge_last, sgl, 1);
2656 alldone = 1;
2657 } else if (curr_prot < protcnt) {
2658 /* advance to next prot buffer */
2659 sgpe = sg_next(sgpe);
2660 sgl++;
2661
2662 /* update the reference tag */
2663 reftag += protgrp_blks;
2664 } else {
2665 /* if we're here, we have a bug */
2666 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2667 "9085 BLKGRD: bug in %s\n", __func__);
2668 }
2669
2670 } while (!alldone);
2671
2672out:
2673
2674 return num_sge;
2675}
2676
2677/**
2678 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2679 * @phba: The Hba for which this call is being executed.
2680 * @sc: pointer to scsi command we're working on
2681 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002682 * Given a SCSI command that supports DIF, determine composition of protection
2683 * groups involved in setting up buffer lists
2684 *
James Smartacd68592012-01-18 16:25:09 -05002685 * Returns: Protection group type (with or without DIF)
2686 *
2687 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002688static int
2689lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2690{
2691 int ret = LPFC_PG_TYPE_INVALID;
2692 unsigned char op = scsi_get_prot_op(sc);
2693
2694 switch (op) {
2695 case SCSI_PROT_READ_STRIP:
2696 case SCSI_PROT_WRITE_INSERT:
2697 ret = LPFC_PG_TYPE_NO_DIF;
2698 break;
2699 case SCSI_PROT_READ_INSERT:
2700 case SCSI_PROT_WRITE_STRIP:
2701 case SCSI_PROT_READ_PASS:
2702 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002703 ret = LPFC_PG_TYPE_DIF_BUF;
2704 break;
2705 default:
2706 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2707 "9021 Unsupported protection op:%d\n", op);
2708 break;
2709 }
2710
2711 return ret;
2712}
2713
James Smartacd68592012-01-18 16:25:09 -05002714/**
2715 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2716 * @phba: The Hba for which this call is being executed.
2717 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2718 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002719 * This is the protection/DIF aware version of
2720 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2721 * two functions eventually, but for now, it's here
James Smartacd68592012-01-18 16:25:09 -05002722 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002723static int
James Smartacd68592012-01-18 16:25:09 -05002724lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smarte2a0a9d2008-12-04 22:40:02 -05002725 struct lpfc_scsi_buf *lpfc_cmd)
2726{
2727 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2728 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2729 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2730 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2731 uint32_t num_bde = 0;
2732 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2733 int prot_group_type = 0;
2734 int diflen, fcpdl;
2735 unsigned blksize;
2736
2737 /*
2738 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2739 * fcp_rsp regions to the first data bde entry
2740 */
2741 bpl += 2;
2742 if (scsi_sg_count(scsi_cmnd)) {
2743 /*
2744 * The driver stores the segment count returned from pci_map_sg
2745 * because this a count of dma-mappings used to map the use_sg
2746 * pages. They are not guaranteed to be the same for those
2747 * architectures that implement an IOMMU.
2748 */
2749 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2750 scsi_sglist(scsi_cmnd),
2751 scsi_sg_count(scsi_cmnd), datadir);
2752 if (unlikely(!datasegcnt))
2753 return 1;
2754
2755 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04002756
2757 /* First check if data segment count from SCSI Layer is good */
2758 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
2759 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002760
2761 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2762
2763 switch (prot_group_type) {
2764 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04002765
2766 /* Here we need to add a PDE5 and PDE6 to the count */
2767 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt)
2768 goto err;
2769
James Smarte2a0a9d2008-12-04 22:40:02 -05002770 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2771 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002772 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002773 if (num_bde < 2)
2774 goto err;
2775 break;
James Smart96f70772013-04-17 20:16:15 -04002776
2777 case LPFC_PG_TYPE_DIF_BUF:
James Smarte2a0a9d2008-12-04 22:40:02 -05002778 /*
2779 * This type indicates that protection buffers are
2780 * passed to the driver, so that needs to be prepared
2781 * for DMA
2782 */
2783 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2784 scsi_prot_sglist(scsi_cmnd),
2785 scsi_prot_sg_count(scsi_cmnd), datadir);
2786 if (unlikely(!protsegcnt)) {
2787 scsi_dma_unmap(scsi_cmnd);
2788 return 1;
2789 }
2790
2791 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04002792
2793 /*
2794 * There is a minimun of 4 BPLs used for every
2795 * protection data segment.
2796 */
2797 if ((lpfc_cmd->prot_seg_cnt * 4) >
2798 (phba->cfg_total_seg_cnt - 2))
2799 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002800
2801 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2802 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002803 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04002804 if ((num_bde < 3) ||
2805 (num_bde > phba->cfg_total_seg_cnt))
James Smarte2a0a9d2008-12-04 22:40:02 -05002806 goto err;
2807 break;
James Smart96f70772013-04-17 20:16:15 -04002808
James Smarte2a0a9d2008-12-04 22:40:02 -05002809 case LPFC_PG_TYPE_INVALID:
2810 default:
James Smart96f70772013-04-17 20:16:15 -04002811 scsi_dma_unmap(scsi_cmnd);
2812 lpfc_cmd->seg_cnt = 0;
2813
James Smarte2a0a9d2008-12-04 22:40:02 -05002814 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2815 "9022 Unexpected protection group %i\n",
2816 prot_group_type);
2817 return 1;
2818 }
2819 }
2820
2821 /*
2822 * Finish initializing those IOCB fields that are dependent on the
2823 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2824 * reinitialized since all iocb memory resources are used many times
2825 * for transmit, receive, and continuation bpl's.
2826 */
2827 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2828 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2829 iocb_cmd->ulpBdeCount = 1;
2830 iocb_cmd->ulpLe = 1;
2831
2832 fcpdl = scsi_bufflen(scsi_cmnd);
2833
2834 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
2835 /*
2836 * We are in DIF Type 1 mode
2837 * Every data block has a 8 byte DIF (trailer)
2838 * attached to it. Must ajust FCP data length
2839 */
2840 blksize = lpfc_cmd_blksize(scsi_cmnd);
2841 diflen = (fcpdl / blksize) * 8;
2842 fcpdl += diflen;
2843 }
2844 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2845
2846 /*
2847 * Due to difference in data length between DIF/non-DIF paths,
2848 * we need to set word 4 of IOCB here
2849 */
2850 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2851
2852 return 0;
2853err:
James Smart96f70772013-04-17 20:16:15 -04002854 if (lpfc_cmd->seg_cnt)
2855 scsi_dma_unmap(scsi_cmnd);
2856 if (lpfc_cmd->prot_seg_cnt)
2857 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2858 scsi_prot_sg_count(scsi_cmnd),
2859 scsi_cmnd->sc_data_direction);
2860
James Smarte2a0a9d2008-12-04 22:40:02 -05002861 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04002862 "9023 Cannot setup S/G List for HBA"
2863 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
2864 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
2865 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
James Smarte2a0a9d2008-12-04 22:40:02 -05002866 prot_group_type, num_bde);
James Smart96f70772013-04-17 20:16:15 -04002867
2868 lpfc_cmd->seg_cnt = 0;
2869 lpfc_cmd->prot_seg_cnt = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05002870 return 1;
2871}
2872
2873/*
James Smart737d4242013-04-17 20:14:49 -04002874 * This function calcuates the T10 DIF guard tag
2875 * on the specified data using a CRC algorithmn
2876 * using crc_t10dif.
2877 */
2878uint16_t
2879lpfc_bg_crc(uint8_t *data, int count)
2880{
2881 uint16_t crc = 0;
2882 uint16_t x;
2883
2884 crc = crc_t10dif(data, count);
2885 x = cpu_to_be16(crc);
2886 return x;
2887}
2888
2889/*
2890 * This function calcuates the T10 DIF guard tag
2891 * on the specified data using a CSUM algorithmn
2892 * using ip_compute_csum.
2893 */
2894uint16_t
2895lpfc_bg_csum(uint8_t *data, int count)
2896{
2897 uint16_t ret;
2898
2899 ret = ip_compute_csum(data, count);
2900 return ret;
2901}
2902
2903/*
2904 * This function examines the protection data to try to determine
2905 * what type of T10-DIF error occurred.
2906 */
2907void
2908lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2909{
2910 struct scatterlist *sgpe; /* s/g prot entry */
2911 struct scatterlist *sgde; /* s/g data entry */
2912 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2913 struct scsi_dif_tuple *src = NULL;
2914 uint8_t *data_src = NULL;
2915 uint16_t guard_tag, guard_type;
2916 uint16_t start_app_tag, app_tag;
2917 uint32_t start_ref_tag, ref_tag;
2918 int prot, protsegcnt;
2919 int err_type, len, data_len;
2920 int chk_ref, chk_app, chk_guard;
2921 uint16_t sum;
2922 unsigned blksize;
2923
2924 err_type = BGS_GUARD_ERR_MASK;
2925 sum = 0;
2926 guard_tag = 0;
2927
2928 /* First check to see if there is protection data to examine */
2929 prot = scsi_get_prot_op(cmd);
2930 if ((prot == SCSI_PROT_READ_STRIP) ||
2931 (prot == SCSI_PROT_WRITE_INSERT) ||
2932 (prot == SCSI_PROT_NORMAL))
2933 goto out;
2934
2935 /* Currently the driver just supports ref_tag and guard_tag checking */
2936 chk_ref = 1;
2937 chk_app = 0;
2938 chk_guard = 0;
2939
2940 /* Setup a ptr to the protection data provided by the SCSI host */
2941 sgpe = scsi_prot_sglist(cmd);
2942 protsegcnt = lpfc_cmd->prot_seg_cnt;
2943
2944 if (sgpe && protsegcnt) {
2945
2946 /*
2947 * We will only try to verify guard tag if the segment
2948 * data length is a multiple of the blksize.
2949 */
2950 sgde = scsi_sglist(cmd);
2951 blksize = lpfc_cmd_blksize(cmd);
2952 data_src = (uint8_t *)sg_virt(sgde);
2953 data_len = sgde->length;
2954 if ((data_len & (blksize - 1)) == 0)
2955 chk_guard = 1;
2956 guard_type = scsi_host_get_guard(cmd->device->host);
2957
2958 start_ref_tag = scsi_get_lba(cmd);
2959 start_app_tag = src->app_tag;
2960 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
2961 len = sgpe->length;
2962 while (src && protsegcnt) {
2963 while (len) {
2964
2965 /*
2966 * First check to see if a protection data
2967 * check is valid
2968 */
2969 if ((src->ref_tag == 0xffffffff) ||
2970 (src->app_tag == 0xffff)) {
2971 start_ref_tag++;
2972 goto skipit;
2973 }
2974
2975 /* App Tag checking */
2976 app_tag = src->app_tag;
2977 if (chk_app && (app_tag != start_app_tag)) {
2978 err_type = BGS_APPTAG_ERR_MASK;
2979 goto out;
2980 }
2981
2982 /* Reference Tag checking */
2983 ref_tag = be32_to_cpu(src->ref_tag);
2984 if (chk_ref && (ref_tag != start_ref_tag)) {
2985 err_type = BGS_REFTAG_ERR_MASK;
2986 goto out;
2987 }
2988 start_ref_tag++;
2989
2990 /* Guard Tag checking */
2991 if (chk_guard) {
2992 guard_tag = src->guard_tag;
2993 if (guard_type == SHOST_DIX_GUARD_IP)
2994 sum = lpfc_bg_csum(data_src,
2995 blksize);
2996 else
2997 sum = lpfc_bg_crc(data_src,
2998 blksize);
2999 if ((guard_tag != sum)) {
3000 err_type = BGS_GUARD_ERR_MASK;
3001 goto out;
3002 }
3003 }
3004skipit:
3005 len -= sizeof(struct scsi_dif_tuple);
3006 if (len < 0)
3007 len = 0;
3008 src++;
3009
3010 data_src += blksize;
3011 data_len -= blksize;
3012
3013 /*
3014 * Are we at the end of the Data segment?
3015 * The data segment is only used for Guard
3016 * tag checking.
3017 */
3018 if (chk_guard && (data_len == 0)) {
3019 chk_guard = 0;
3020 sgde = sg_next(sgde);
3021 if (!sgde)
3022 goto out;
3023
3024 data_src = (uint8_t *)sg_virt(sgde);
3025 data_len = sgde->length;
3026 if ((data_len & (blksize - 1)) == 0)
3027 chk_guard = 1;
3028 }
3029 }
3030
3031 /* Goto the next Protection data segment */
3032 sgpe = sg_next(sgpe);
3033 if (sgpe) {
3034 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
3035 len = sgpe->length;
3036 } else {
3037 src = NULL;
3038 }
3039 protsegcnt--;
3040 }
3041 }
3042out:
3043 if (err_type == BGS_GUARD_ERR_MASK) {
3044 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3045 0x10, 0x1);
3046 cmd->result = DRIVER_SENSE << 24
3047 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3048 phba->bg_guard_err_cnt++;
3049 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3050 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
3051 (unsigned long)scsi_get_lba(cmd),
3052 sum, guard_tag);
3053
3054 } else if (err_type == BGS_REFTAG_ERR_MASK) {
3055 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3056 0x10, 0x3);
3057 cmd->result = DRIVER_SENSE << 24
3058 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3059
3060 phba->bg_reftag_err_cnt++;
3061 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3062 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
3063 (unsigned long)scsi_get_lba(cmd),
3064 ref_tag, start_ref_tag);
3065
3066 } else if (err_type == BGS_APPTAG_ERR_MASK) {
3067 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3068 0x10, 0x2);
3069 cmd->result = DRIVER_SENSE << 24
3070 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3071
3072 phba->bg_apptag_err_cnt++;
3073 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3074 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
3075 (unsigned long)scsi_get_lba(cmd),
3076 app_tag, start_app_tag);
3077 }
3078}
3079
3080
3081/*
James Smarte2a0a9d2008-12-04 22:40:02 -05003082 * This function checks for BlockGuard errors detected by
3083 * the HBA. In case of errors, the ASC/ASCQ fields in the
3084 * sense buffer will be set accordingly, paired with
3085 * ILLEGAL_REQUEST to signal to the kernel that the HBA
3086 * detected corruption.
3087 *
3088 * Returns:
3089 * 0 - No error found
3090 * 1 - BlockGuard error found
3091 * -1 - Internal error (bad profile, ...etc)
3092 */
3093static int
3094lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
3095 struct lpfc_iocbq *pIocbOut)
3096{
3097 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3098 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
3099 int ret = 0;
3100 uint32_t bghm = bgf->bghm;
3101 uint32_t bgstat = bgf->bgstat;
3102 uint64_t failing_sector = 0;
3103
James Smarte2a0a9d2008-12-04 22:40:02 -05003104 spin_lock(&_dump_buf_lock);
3105 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04003106 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
3107 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05003108 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04003109 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003110
3111 /* If we have a prot sgl, save the DIF buffer */
3112 if (lpfc_prot_group_type(phba, cmd) ==
3113 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04003114 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
3115 "Saving DIF for %u blocks to debugfs\n",
3116 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
3117 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003118 }
3119
3120 _dump_buf_done = 1;
3121 }
3122 spin_unlock(&_dump_buf_lock);
3123
3124 if (lpfc_bgs_get_invalid_prof(bgstat)) {
3125 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart737d4242013-04-17 20:14:49 -04003126 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3127 "9072 BLKGRD: Invalid BG Profile in cmd"
3128 " 0x%x lba 0x%llx blk cnt 0x%x "
3129 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3130 (unsigned long long)scsi_get_lba(cmd),
3131 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003132 ret = (-1);
3133 goto out;
3134 }
3135
3136 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
3137 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart737d4242013-04-17 20:14:49 -04003138 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3139 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
3140 " 0x%x lba 0x%llx blk cnt 0x%x "
3141 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3142 (unsigned long long)scsi_get_lba(cmd),
3143 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003144 ret = (-1);
3145 goto out;
3146 }
3147
3148 if (lpfc_bgs_get_guard_err(bgstat)) {
3149 ret = 1;
3150
3151 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3152 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003153 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003154 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3155 phba->bg_guard_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003156 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3157 "9055 BLKGRD: Guard Tag error in cmd"
3158 " 0x%x lba 0x%llx blk cnt 0x%x "
3159 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3160 (unsigned long long)scsi_get_lba(cmd),
3161 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003162 }
3163
3164 if (lpfc_bgs_get_reftag_err(bgstat)) {
3165 ret = 1;
3166
3167 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3168 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003169 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003170 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3171
3172 phba->bg_reftag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003173 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3174 "9056 BLKGRD: Ref Tag error in cmd"
3175 " 0x%x lba 0x%llx blk cnt 0x%x "
3176 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3177 (unsigned long long)scsi_get_lba(cmd),
3178 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003179 }
3180
3181 if (lpfc_bgs_get_apptag_err(bgstat)) {
3182 ret = 1;
3183
3184 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3185 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003186 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003187 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3188
3189 phba->bg_apptag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003190 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3191 "9061 BLKGRD: App Tag error in cmd"
3192 " 0x%x lba 0x%llx blk cnt 0x%x "
3193 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3194 (unsigned long long)scsi_get_lba(cmd),
3195 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003196 }
3197
3198 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
3199 /*
3200 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04003201 * field, and put the failing LBA in it.
3202 * This code assumes there was also a guard/app/ref tag error
3203 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05003204 */
James Smart7c56b9f2011-07-22 18:36:25 -04003205 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
3206 cmd->sense_buffer[8] = 0; /* Information descriptor type */
3207 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
3208 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05003209
3210 /* bghm is a "on the wire" FC frame based count */
3211 switch (scsi_get_prot_op(cmd)) {
3212 case SCSI_PROT_READ_INSERT:
3213 case SCSI_PROT_WRITE_STRIP:
3214 bghm /= cmd->device->sector_size;
3215 break;
3216 case SCSI_PROT_READ_STRIP:
3217 case SCSI_PROT_WRITE_INSERT:
3218 case SCSI_PROT_READ_PASS:
3219 case SCSI_PROT_WRITE_PASS:
3220 bghm /= (cmd->device->sector_size +
3221 sizeof(struct scsi_dif_tuple));
3222 break;
3223 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003224
3225 failing_sector = scsi_get_lba(cmd);
3226 failing_sector += bghm;
3227
James Smart7c56b9f2011-07-22 18:36:25 -04003228 /* Descriptor Information */
3229 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05003230 }
3231
3232 if (!ret) {
3233 /* No error was reported - problem in FW? */
James Smart737d4242013-04-17 20:14:49 -04003234 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3235 "9057 BLKGRD: Unknown error in cmd"
3236 " 0x%x lba 0x%llx blk cnt 0x%x "
3237 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3238 (unsigned long long)scsi_get_lba(cmd),
3239 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003240
James Smart737d4242013-04-17 20:14:49 -04003241 /* Calcuate what type of error it was */
3242 lpfc_calc_bg_err(phba, lpfc_cmd);
3243 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003244out:
3245 return ret;
3246}
3247
James Smartea2151b2008-09-07 11:52:10 -04003248/**
James Smartda0436e2009-05-22 14:51:39 -04003249 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3250 * @phba: The Hba for which this call is being executed.
3251 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3252 *
3253 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3254 * field of @lpfc_cmd for device with SLI-4 interface spec.
3255 *
3256 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003257 * 1 - Error
3258 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04003259 **/
3260static int
3261lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3262{
3263 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3264 struct scatterlist *sgel = NULL;
3265 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3266 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
James Smartfedd3b72011-02-16 12:39:24 -05003267 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04003268 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3269 dma_addr_t physaddr;
3270 uint32_t num_bde = 0;
3271 uint32_t dma_len;
3272 uint32_t dma_offset = 0;
3273 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05003274 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04003275
3276 /*
3277 * There are three possibilities here - use scatter-gather segment, use
3278 * the single mapping, or neither. Start the lpfc command prep by
3279 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3280 * data bde entry.
3281 */
3282 if (scsi_sg_count(scsi_cmnd)) {
3283 /*
3284 * The driver stores the segment count returned from pci_map_sg
3285 * because this a count of dma-mappings used to map the use_sg
3286 * pages. They are not guaranteed to be the same for those
3287 * architectures that implement an IOMMU.
3288 */
3289
3290 nseg = scsi_dma_map(scsi_cmnd);
3291 if (unlikely(!nseg))
3292 return 1;
3293 sgl += 1;
3294 /* clear the last flag in the fcp_rsp map entry */
3295 sgl->word2 = le32_to_cpu(sgl->word2);
3296 bf_set(lpfc_sli4_sge_last, sgl, 0);
3297 sgl->word2 = cpu_to_le32(sgl->word2);
3298 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05003299 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04003300 lpfc_cmd->seg_cnt = nseg;
3301 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04003302 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3303 " %s: Too many sg segments from "
3304 "dma_map_sg. Config %d, seg_cnt %d\n",
3305 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04003306 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04003307 lpfc_cmd->seg_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04003308 scsi_dma_unmap(scsi_cmnd);
3309 return 1;
3310 }
3311
3312 /*
3313 * The driver established a maximum scatter-gather segment count
3314 * during probe that limits the number of sg elements in any
3315 * single scsi command. Just run through the seg_cnt and format
3316 * the sge's.
3317 * When using SLI-3 the driver will try to fit all the BDEs into
3318 * the IOCB. If it can't then the BDEs get added to a BPL as it
3319 * does for SLI-2 mode.
3320 */
3321 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3322 physaddr = sg_dma_address(sgel);
3323 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04003324 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3325 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04003326 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04003327 if ((num_bde + 1) == nseg)
3328 bf_set(lpfc_sli4_sge_last, sgl, 1);
3329 else
3330 bf_set(lpfc_sli4_sge_last, sgl, 0);
3331 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04003332 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04003333 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05003334 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04003335 dma_offset += dma_len;
3336 sgl++;
3337 }
James Smartfedd3b72011-02-16 12:39:24 -05003338 /* setup the performance hint (first data BDE) if enabled */
3339 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
3340 bde = (struct ulp_bde64 *)
3341 &(iocb_cmd->unsli3.sli3Words[5]);
3342 bde->addrLow = first_data_sgl->addr_lo;
3343 bde->addrHigh = first_data_sgl->addr_hi;
3344 bde->tus.f.bdeSize =
3345 le32_to_cpu(first_data_sgl->sge_len);
3346 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3347 bde->tus.w = cpu_to_le32(bde->tus.w);
3348 }
James Smartda0436e2009-05-22 14:51:39 -04003349 } else {
3350 sgl += 1;
3351 /* clear the last flag in the fcp_rsp map entry */
3352 sgl->word2 = le32_to_cpu(sgl->word2);
3353 bf_set(lpfc_sli4_sge_last, sgl, 1);
3354 sgl->word2 = cpu_to_le32(sgl->word2);
3355 }
3356
3357 /*
3358 * Finish initializing those IOCB fields that are dependent on the
3359 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3360 * explicitly reinitialized.
3361 * all iocb memory resources are reused.
3362 */
3363 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3364
3365 /*
3366 * Due to difference in data length between DIF/non-DIF paths,
3367 * we need to set word 4 of IOCB here
3368 */
3369 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
3370 return 0;
3371}
3372
3373/**
James Smartacd68592012-01-18 16:25:09 -05003374 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
3375 * @phba: The Hba for which this call is being executed.
3376 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
3377 *
3378 * Adjust the data length to account for how much data
3379 * is actually on the wire.
3380 *
3381 * returns the adjusted data length
3382 **/
3383static int
3384lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
3385 struct lpfc_scsi_buf *lpfc_cmd)
3386{
3387 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
3388 int diflen, fcpdl;
3389 unsigned blksize;
3390
3391 fcpdl = scsi_bufflen(sc);
3392
3393 /* Check if there is protection data on the wire */
3394 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
3395 /* Read */
3396 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
3397 return fcpdl;
3398
3399 } else {
3400 /* Write */
3401 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
3402 return fcpdl;
3403 }
3404
3405 /* If protection data on the wire, adjust the count accordingly */
3406 blksize = lpfc_cmd_blksize(sc);
3407 diflen = (fcpdl / blksize) * 8;
3408 fcpdl += diflen;
3409 return fcpdl;
3410}
3411
3412/**
3413 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3414 * @phba: The Hba for which this call is being executed.
3415 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3416 *
3417 * This is the protection/DIF aware version of
3418 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3419 * two functions eventually, but for now, it's here
3420 **/
3421static int
3422lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3423 struct lpfc_scsi_buf *lpfc_cmd)
3424{
3425 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3426 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3427 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3428 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart96f70772013-04-17 20:16:15 -04003429 uint32_t num_sge = 0;
James Smartacd68592012-01-18 16:25:09 -05003430 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3431 int prot_group_type = 0;
3432 int fcpdl;
3433
3434 /*
3435 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
James Smart96f70772013-04-17 20:16:15 -04003436 * fcp_rsp regions to the first data sge entry
James Smartacd68592012-01-18 16:25:09 -05003437 */
3438 if (scsi_sg_count(scsi_cmnd)) {
3439 /*
3440 * The driver stores the segment count returned from pci_map_sg
3441 * because this a count of dma-mappings used to map the use_sg
3442 * pages. They are not guaranteed to be the same for those
3443 * architectures that implement an IOMMU.
3444 */
3445 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3446 scsi_sglist(scsi_cmnd),
3447 scsi_sg_count(scsi_cmnd), datadir);
3448 if (unlikely(!datasegcnt))
3449 return 1;
3450
3451 sgl += 1;
3452 /* clear the last flag in the fcp_rsp map entry */
3453 sgl->word2 = le32_to_cpu(sgl->word2);
3454 bf_set(lpfc_sli4_sge_last, sgl, 0);
3455 sgl->word2 = cpu_to_le32(sgl->word2);
3456
3457 sgl += 1;
3458 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04003459
3460 /* First check if data segment count from SCSI Layer is good */
3461 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
3462 goto err;
James Smartacd68592012-01-18 16:25:09 -05003463
3464 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3465
3466 switch (prot_group_type) {
3467 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04003468 /* Here we need to add a DISEED to the count */
3469 if ((lpfc_cmd->seg_cnt + 1) > phba->cfg_total_seg_cnt)
3470 goto err;
3471
3472 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003473 datasegcnt);
James Smart96f70772013-04-17 20:16:15 -04003474
James Smartacd68592012-01-18 16:25:09 -05003475 /* we should have 2 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003476 if (num_sge < 2)
James Smartacd68592012-01-18 16:25:09 -05003477 goto err;
3478 break;
James Smart96f70772013-04-17 20:16:15 -04003479
3480 case LPFC_PG_TYPE_DIF_BUF:
James Smartacd68592012-01-18 16:25:09 -05003481 /*
3482 * This type indicates that protection buffers are
3483 * passed to the driver, so that needs to be prepared
3484 * for DMA
3485 */
3486 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3487 scsi_prot_sglist(scsi_cmnd),
3488 scsi_prot_sg_count(scsi_cmnd), datadir);
3489 if (unlikely(!protsegcnt)) {
3490 scsi_dma_unmap(scsi_cmnd);
3491 return 1;
3492 }
3493
3494 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04003495 /*
3496 * There is a minimun of 3 SGEs used for every
3497 * protection data segment.
3498 */
3499 if ((lpfc_cmd->prot_seg_cnt * 3) >
3500 (phba->cfg_total_seg_cnt - 2))
3501 goto err;
James Smartacd68592012-01-18 16:25:09 -05003502
James Smart96f70772013-04-17 20:16:15 -04003503 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003504 datasegcnt, protsegcnt);
James Smart96f70772013-04-17 20:16:15 -04003505
James Smartacd68592012-01-18 16:25:09 -05003506 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003507 if ((num_sge < 3) ||
3508 (num_sge > phba->cfg_total_seg_cnt))
James Smartacd68592012-01-18 16:25:09 -05003509 goto err;
3510 break;
James Smart96f70772013-04-17 20:16:15 -04003511
James Smartacd68592012-01-18 16:25:09 -05003512 case LPFC_PG_TYPE_INVALID:
3513 default:
James Smart96f70772013-04-17 20:16:15 -04003514 scsi_dma_unmap(scsi_cmnd);
3515 lpfc_cmd->seg_cnt = 0;
3516
James Smartacd68592012-01-18 16:25:09 -05003517 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3518 "9083 Unexpected protection group %i\n",
3519 prot_group_type);
3520 return 1;
3521 }
3522 }
3523
James Smart8012cc32012-10-31 14:44:49 -04003524 switch (scsi_get_prot_op(scsi_cmnd)) {
3525 case SCSI_PROT_WRITE_STRIP:
3526 case SCSI_PROT_READ_STRIP:
3527 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3528 break;
3529 case SCSI_PROT_WRITE_INSERT:
3530 case SCSI_PROT_READ_INSERT:
3531 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3532 break;
3533 case SCSI_PROT_WRITE_PASS:
3534 case SCSI_PROT_READ_PASS:
3535 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3536 break;
3537 }
3538
James Smartacd68592012-01-18 16:25:09 -05003539 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
3540
3541 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3542
3543 /*
3544 * Due to difference in data length between DIF/non-DIF paths,
3545 * we need to set word 4 of IOCB here
3546 */
3547 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
James Smartacd68592012-01-18 16:25:09 -05003548
3549 return 0;
3550err:
James Smart96f70772013-04-17 20:16:15 -04003551 if (lpfc_cmd->seg_cnt)
3552 scsi_dma_unmap(scsi_cmnd);
3553 if (lpfc_cmd->prot_seg_cnt)
3554 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3555 scsi_prot_sg_count(scsi_cmnd),
3556 scsi_cmnd->sc_data_direction);
3557
James Smartacd68592012-01-18 16:25:09 -05003558 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04003559 "9084 Cannot setup S/G List for HBA"
3560 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3561 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3562 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3563 prot_group_type, num_sge);
3564
3565 lpfc_cmd->seg_cnt = 0;
3566 lpfc_cmd->prot_seg_cnt = 0;
James Smartacd68592012-01-18 16:25:09 -05003567 return 1;
3568}
3569
3570/**
James Smart3772a992009-05-22 14:50:54 -04003571 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3572 * @phba: The Hba for which this call is being executed.
3573 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3574 *
3575 * This routine wraps the actual DMA mapping function pointer from the
3576 * lpfc_hba struct.
3577 *
3578 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003579 * 1 - Error
3580 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003581 **/
3582static inline int
3583lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3584{
3585 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3586}
3587
3588/**
James Smartacd68592012-01-18 16:25:09 -05003589 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3590 * using BlockGuard.
3591 * @phba: The Hba for which this call is being executed.
3592 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3593 *
3594 * This routine wraps the actual DMA mapping function pointer from the
3595 * lpfc_hba struct.
3596 *
3597 * Return codes:
3598 * 1 - Error
3599 * 0 - Success
3600 **/
3601static inline int
3602lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3603{
3604 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3605}
3606
3607/**
James Smart3621a712009-04-06 18:47:14 -04003608 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003609 * @phba: Pointer to hba context object.
3610 * @vport: Pointer to vport object.
3611 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3612 * @rsp_iocb: Pointer to response iocb object which reported error.
3613 *
3614 * This function posts an event when there is a SCSI command reporting
3615 * error from the scsi device.
3616 **/
3617static void
3618lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3619 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3620 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3621 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3622 uint32_t resp_info = fcprsp->rspStatus2;
3623 uint32_t scsi_status = fcprsp->rspStatus3;
3624 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3625 struct lpfc_fast_path_event *fast_path_evt = NULL;
3626 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3627 unsigned long flags;
3628
James Smart5989b8d2010-10-22 11:06:56 -04003629 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3630 return;
3631
James Smartea2151b2008-09-07 11:52:10 -04003632 /* If there is queuefull or busy condition send a scsi event */
3633 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3634 (cmnd->result == SAM_STAT_BUSY)) {
3635 fast_path_evt = lpfc_alloc_fast_evt(phba);
3636 if (!fast_path_evt)
3637 return;
3638 fast_path_evt->un.scsi_evt.event_type =
3639 FC_REG_SCSI_EVENT;
3640 fast_path_evt->un.scsi_evt.subcategory =
3641 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3642 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3643 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3644 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3645 &pnode->nlp_portname, sizeof(struct lpfc_name));
3646 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3647 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3648 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3649 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3650 fast_path_evt = lpfc_alloc_fast_evt(phba);
3651 if (!fast_path_evt)
3652 return;
3653 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3654 FC_REG_SCSI_EVENT;
3655 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3656 LPFC_EVENT_CHECK_COND;
3657 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3658 cmnd->device->lun;
3659 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3660 &pnode->nlp_portname, sizeof(struct lpfc_name));
3661 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3662 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3663 fast_path_evt->un.check_cond_evt.sense_key =
3664 cmnd->sense_buffer[2] & 0xf;
3665 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3666 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3667 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3668 fcpi_parm &&
3669 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3670 ((scsi_status == SAM_STAT_GOOD) &&
3671 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3672 /*
3673 * If status is good or resid does not match with fcp_param and
3674 * there is valid fcpi_parm, then there is a read_check error
3675 */
3676 fast_path_evt = lpfc_alloc_fast_evt(phba);
3677 if (!fast_path_evt)
3678 return;
3679 fast_path_evt->un.read_check_error.header.event_type =
3680 FC_REG_FABRIC_EVENT;
3681 fast_path_evt->un.read_check_error.header.subcategory =
3682 LPFC_EVENT_FCPRDCHKERR;
3683 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3684 &pnode->nlp_portname, sizeof(struct lpfc_name));
3685 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3686 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3687 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3688 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3689 fast_path_evt->un.read_check_error.fcpiparam =
3690 fcpi_parm;
3691 } else
3692 return;
3693
3694 fast_path_evt->vport = vport;
3695 spin_lock_irqsave(&phba->hbalock, flags);
3696 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3697 spin_unlock_irqrestore(&phba->hbalock, flags);
3698 lpfc_worker_wake_up(phba);
3699 return;
3700}
James Smart9bad7672008-12-04 22:39:02 -05003701
3702/**
James Smartf1126682009-06-10 17:22:44 -04003703 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003704 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003705 * @psb: The scsi buffer which is going to be un-mapped.
3706 *
3707 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003708 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003709 **/
dea31012005-04-17 16:05:31 -05003710static void
James Smartf1126682009-06-10 17:22:44 -04003711lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003712{
3713 /*
3714 * There are only two special cases to consider. (1) the scsi command
3715 * requested scatter-gather usage or (2) the scsi command allocated
3716 * a request buffer, but did not request use_sg. There is a third
3717 * case, but it does not require resource deallocation.
3718 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003719 if (psb->seg_cnt > 0)
3720 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003721 if (psb->prot_seg_cnt > 0)
3722 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3723 scsi_prot_sg_count(psb->pCmd),
3724 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003725}
3726
James Smart9bad7672008-12-04 22:39:02 -05003727/**
James Smart3621a712009-04-06 18:47:14 -04003728 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003729 * @vport: The virtual port for which this call is being executed.
3730 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3731 * @rsp_iocb: The response IOCB which contains FCP error.
3732 *
3733 * This routine is called to process response IOCB with status field
3734 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3735 * based upon SCSI and FCP error.
3736 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003737static void
James Smart2e0fef82007-06-17 19:56:36 -05003738lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3739 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003740{
3741 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3742 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3743 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003744 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003745 uint32_t resp_info = fcprsp->rspStatus2;
3746 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003747 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003748 uint32_t host_status = DID_OK;
3749 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05003750 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003751
James Smartea2151b2008-09-07 11:52:10 -04003752
dea31012005-04-17 16:05:31 -05003753 /*
3754 * If this is a task management command, there is no
3755 * scsi packet associated with this lpfc_cmd. The driver
3756 * consumes it.
3757 */
3758 if (fcpcmd->fcpCntl2) {
3759 scsi_status = 0;
3760 goto out;
3761 }
3762
James Smart6a9c52c2009-10-02 15:16:51 -04003763 if (resp_info & RSP_LEN_VALID) {
3764 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003765 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04003766 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3767 "2719 Invalid response length: "
3768 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
3769 cmnd->device->id,
3770 cmnd->device->lun, cmnd->cmnd[0],
3771 rsplen);
3772 host_status = DID_ERROR;
3773 goto out;
3774 }
James Smarte40a02c2010-02-26 14:13:54 -05003775 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3776 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3777 "2757 Protocol failure detected during "
3778 "processing of FCP I/O op: "
3779 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
3780 cmnd->device->id,
3781 cmnd->device->lun, cmnd->cmnd[0],
3782 fcprsp->rspInfo3);
3783 host_status = DID_ERROR;
3784 goto out;
3785 }
James Smart6a9c52c2009-10-02 15:16:51 -04003786 }
3787
James Smartc7743952006-12-02 13:34:42 -05003788 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3789 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3790 if (snslen > SCSI_SENSE_BUFFERSIZE)
3791 snslen = SCSI_SENSE_BUFFERSIZE;
3792
3793 if (resp_info & RSP_LEN_VALID)
3794 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3795 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3796 }
3797 lp = (uint32_t *)cmnd->sense_buffer;
3798
James Smartaa1c7ee2012-08-14 14:26:06 -04003799 /* special handling for under run conditions */
3800 if (!scsi_status && (resp_info & RESID_UNDER)) {
3801 /* don't log under runs if fcp set... */
3802 if (vport->cfg_log_verbose & LOG_FCP)
3803 logit = LOG_FCP_ERROR;
3804 /* unless operator says so */
3805 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3806 logit = LOG_FCP_UNDER;
3807 }
James Smartc7743952006-12-02 13:34:42 -05003808
James Smarte8b62012007-08-02 11:10:09 -04003809 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003810 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003811 "Data: x%x x%x x%x x%x x%x\n",
3812 cmnd->cmnd[0], scsi_status,
3813 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3814 be32_to_cpu(fcprsp->rspResId),
3815 be32_to_cpu(fcprsp->rspSnsLen),
3816 be32_to_cpu(fcprsp->rspRspLen),
3817 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003818
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003819 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05003820 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003821 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003822
James Smart73d91e52011-10-10 21:32:10 -04003823 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smarte2a0a9d2008-12-04 22:40:02 -05003824 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003825 "residual %d Data: x%x x%x x%x\n",
3826 be32_to_cpu(fcpcmd->fcpDl),
3827 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3828 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003829
3830 /*
James Smart7054a602007-04-25 09:52:34 -04003831 * If there is an under run check if under run reported by
3832 * storage array is same as the under run reported by HBA.
3833 * If this is not same, there is a dropped frame.
3834 */
3835 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3836 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003837 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003838 lpfc_printf_vlog(vport, KERN_WARNING,
3839 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003840 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003841 "and Underrun Data: x%x x%x x%x x%x\n",
3842 be32_to_cpu(fcpcmd->fcpDl),
3843 scsi_get_resid(cmnd), fcpi_parm,
3844 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003845 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003846 host_status = DID_ERROR;
3847 }
3848 /*
dea31012005-04-17 16:05:31 -05003849 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003850 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003851 * is not present, make sure the actual amount transferred is at
3852 * least the underflow value or fail.
3853 */
3854 if (!(resp_info & SNS_LEN_VALID) &&
3855 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003856 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3857 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003858 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003859 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003860 "underrun converted to error "
3861 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003862 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003863 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003864 host_status = DID_ERROR;
3865 }
3866 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003867 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003868 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003869 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003870 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003871 host_status = DID_ERROR;
3872
3873 /*
3874 * Check SLI validation that all the transfer was actually done
James Smart582dd792012-08-03 12:34:44 -04003875 * (fcpi_parm should be zero).
dea31012005-04-17 16:05:31 -05003876 */
James Smart582dd792012-08-03 12:34:44 -04003877 } else if (fcpi_parm) {
James Smarte8b62012007-08-02 11:10:09 -04003878 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart582dd792012-08-03 12:34:44 -04003879 "9029 FCP Data Transfer Check Error: "
James Smarteee88772010-09-29 11:19:08 -04003880 "x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003881 be32_to_cpu(fcpcmd->fcpDl),
3882 be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04003883 fcpi_parm, cmnd->cmnd[0], scsi_status);
3884 switch (scsi_status) {
3885 case SAM_STAT_GOOD:
3886 case SAM_STAT_CHECK_CONDITION:
3887 /* Fabric dropped a data frame. Fail any successful
3888 * command in which we detected dropped frames.
3889 * A status of good or some check conditions could
3890 * be considered a successful command.
3891 */
3892 host_status = DID_ERROR;
3893 break;
3894 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003895 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05003896 }
3897
3898 out:
3899 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04003900 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05003901}
3902
James Smart9bad7672008-12-04 22:39:02 -05003903/**
James Smart3621a712009-04-06 18:47:14 -04003904 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05003905 * @phba: The Hba for which this call is being executed.
3906 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04003907 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05003908 *
3909 * This routine assigns scsi command result by looking into response IOCB
3910 * status field appropriately. This routine handles QUEUE FULL condition as
3911 * well by ramping down device queue depth.
3912 **/
dea31012005-04-17 16:05:31 -05003913static void
3914lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3915 struct lpfc_iocbq *pIocbOut)
3916{
3917 struct lpfc_scsi_buf *lpfc_cmd =
3918 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003919 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05003920 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3921 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04003922 struct scsi_cmnd *cmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05003923 int result;
James Smarta257bf92009-04-06 18:48:10 -04003924 struct scsi_device *tmp_sdev;
James Smart5ffc2662009-11-18 15:39:44 -05003925 int depth;
James Smartfa61a542008-01-11 01:52:42 -05003926 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04003927 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04003928 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04003929 uint32_t queue_depth, scsi_id;
James Smart73d91e52011-10-10 21:32:10 -04003930 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05003931
James Smart75baf692010-06-08 18:31:21 -04003932 /* Sanity check on return of outstanding command */
3933 if (!(lpfc_cmd->pCmd))
3934 return;
3935 cmd = lpfc_cmd->pCmd;
3936 shost = cmd->device->host;
3937
James Smarte3d2b802012-08-14 14:25:43 -04003938 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
dea31012005-04-17 16:05:31 -05003939 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05003940 /* pick up SLI4 exhange busy status from HBA */
3941 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
3942
James Smart9a6b09c2012-03-01 22:37:42 -05003943#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3944 if (lpfc_cmd->prot_data_type) {
3945 struct scsi_dif_tuple *src = NULL;
3946
3947 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
3948 /*
3949 * Used to restore any changes to protection
3950 * data for error injection.
3951 */
3952 switch (lpfc_cmd->prot_data_type) {
3953 case LPFC_INJERR_REFTAG:
3954 src->ref_tag =
3955 lpfc_cmd->prot_data;
3956 break;
3957 case LPFC_INJERR_APPTAG:
3958 src->app_tag =
3959 (uint16_t)lpfc_cmd->prot_data;
3960 break;
3961 case LPFC_INJERR_GUARD:
3962 src->guard_tag =
3963 (uint16_t)lpfc_cmd->prot_data;
3964 break;
3965 default:
3966 break;
3967 }
3968
3969 lpfc_cmd->prot_data = 0;
3970 lpfc_cmd->prot_data_type = 0;
3971 lpfc_cmd->prot_data_segment = NULL;
3972 }
3973#endif
James Smart109f6ed2008-12-04 22:39:08 -05003974 if (pnode && NLP_CHK_NODE_ACT(pnode))
3975 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05003976
3977 if (lpfc_cmd->status) {
3978 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
3979 (lpfc_cmd->result & IOERR_DRVR_MASK))
3980 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3981 else if (lpfc_cmd->status >= IOSTAT_CNT)
3982 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smartaa1c7ee2012-08-14 14:26:06 -04003983 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
3984 !lpfc_cmd->fcp_rsp->rspStatus3 &&
3985 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
3986 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
James Smart73d91e52011-10-10 21:32:10 -04003987 logit = 0;
3988 else
3989 logit = LOG_FCP | LOG_FCP_UNDER;
3990 lpfc_printf_vlog(vport, KERN_WARNING, logit,
3991 "9030 FCP cmd x%x failed <%d/%d> "
James Smart5a0d80f2012-05-09 21:18:20 -04003992 "status: x%x result: x%x "
3993 "sid: x%x did: x%x oxid: x%x "
3994 "Data: x%x x%x\n",
James Smart73d91e52011-10-10 21:32:10 -04003995 cmd->cmnd[0],
3996 cmd->device ? cmd->device->id : 0xffff,
3997 cmd->device ? cmd->device->lun : 0xffff,
3998 lpfc_cmd->status, lpfc_cmd->result,
James Smart5a0d80f2012-05-09 21:18:20 -04003999 vport->fc_myDID, pnode->nlp_DID,
4000 phba->sli_rev == LPFC_SLI_REV4 ?
4001 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
James Smart73d91e52011-10-10 21:32:10 -04004002 pIocbOut->iocb.ulpContext,
4003 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05004004
4005 switch (lpfc_cmd->status) {
4006 case IOSTAT_FCP_RSP_ERROR:
4007 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05004008 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05004009 break;
4010 case IOSTAT_NPORT_BSY:
4011 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04004012 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04004013 fast_path_evt = lpfc_alloc_fast_evt(phba);
4014 if (!fast_path_evt)
4015 break;
4016 fast_path_evt->un.fabric_evt.event_type =
4017 FC_REG_FABRIC_EVENT;
4018 fast_path_evt->un.fabric_evt.subcategory =
4019 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
4020 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
4021 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4022 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
4023 &pnode->nlp_portname,
4024 sizeof(struct lpfc_name));
4025 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
4026 &pnode->nlp_nodename,
4027 sizeof(struct lpfc_name));
4028 }
4029 fast_path_evt->vport = vport;
4030 fast_path_evt->work_evt.evt =
4031 LPFC_EVT_FASTPATH_MGMT_EVT;
4032 spin_lock_irqsave(&phba->hbalock, flags);
4033 list_add_tail(&fast_path_evt->work_evt.evt_listp,
4034 &phba->work_list);
4035 spin_unlock_irqrestore(&phba->hbalock, flags);
4036 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004037 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004038 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05004039 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05004040 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
4041 lpfc_cmd->result ==
4042 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
4043 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
4044 lpfc_cmd->result ==
4045 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
4046 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
4047 break;
4048 }
James Smartd7c255b2008-08-24 21:50:00 -04004049 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05004050 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04004051 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
4052 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
James Smart92d7f7b2007-06-17 19:56:38 -05004053 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04004054 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05004055 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004056 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
4057 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
4058 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
4059 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
4060 /*
4061 * This is a response for a BG enabled
4062 * cmd. Parse BG error
4063 */
4064 lpfc_parse_bg_err(phba, lpfc_cmd,
4065 pIocbOut);
4066 break;
4067 } else {
4068 lpfc_printf_vlog(vport, KERN_WARNING,
4069 LOG_BG,
4070 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04004071 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05004072 }
4073 }
James Smart1151e3e2011-02-16 12:39:35 -05004074 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
4075 && (phba->sli_rev == LPFC_SLI_REV4)
4076 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
4077 /* This IO was aborted by the target, we don't
4078 * know the rxid and because we did not send the
4079 * ABTS we cannot generate and RRQ.
4080 */
4081 lpfc_set_rrq_active(phba, pnode,
James Smartee0f4fe2012-05-09 21:19:14 -04004082 lpfc_cmd->cur_iocbq.sli4_lxritag,
4083 0, 0);
James Smart1151e3e2011-02-16 12:39:35 -05004084 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004085 /* else: fall through */
dea31012005-04-17 16:05:31 -05004086 default:
4087 cmd->result = ScsiResult(DID_ERROR, 0);
4088 break;
4089 }
4090
James Smart58da1ff2008-04-07 10:15:56 -04004091 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004092 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04004093 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
4094 SAM_STAT_BUSY);
James Smartab56dc22011-02-16 12:39:57 -05004095 } else
dea31012005-04-17 16:05:31 -05004096 cmd->result = ScsiResult(DID_OK, 0);
dea31012005-04-17 16:05:31 -05004097
4098 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4099 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
4100
James Smarte8b62012007-08-02 11:10:09 -04004101 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4102 "0710 Iodone <%d/%d> cmd %p, error "
4103 "x%x SNS x%x x%x Data: x%x x%x\n",
4104 cmd->device->id, cmd->device->lun, cmd,
4105 cmd->result, *lp, *(lp + 3), cmd->retries,
4106 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05004107 }
4108
James Smartea2151b2008-09-07 11:52:10 -04004109 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004110 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04004111 if (vport->cfg_max_scsicmpl_time &&
4112 time_after(jiffies, lpfc_cmd->start_time +
4113 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04004114 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004115 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4116 if (pnode->cmd_qdepth >
4117 atomic_read(&pnode->cmd_pending) &&
4118 (atomic_read(&pnode->cmd_pending) >
4119 LPFC_MIN_TGT_QDEPTH) &&
4120 ((cmd->cmnd[0] == READ_10) ||
4121 (cmd->cmnd[0] == WRITE_10)))
4122 pnode->cmd_qdepth =
4123 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04004124
James Smart109f6ed2008-12-04 22:39:08 -05004125 pnode->last_change_time = jiffies;
4126 }
James Smarta257bf92009-04-06 18:48:10 -04004127 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004128 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smart7dc517d2010-07-14 15:32:10 -04004129 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
James Smart977b5a02008-09-07 11:52:04 -04004130 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05004131 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04004132 spin_lock_irqsave(shost->host_lock, flags);
James Smart7dc517d2010-07-14 15:32:10 -04004133 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
4134 / 100;
4135 depth = depth ? depth : 1;
4136 pnode->cmd_qdepth += depth;
4137 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
4138 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05004139 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04004140 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004141 }
James Smart977b5a02008-09-07 11:52:04 -04004142 }
4143
James Smart1dcb58e2007-04-25 09:51:30 -04004144 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04004145
4146 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
4147 queue_depth = cmd->device->queue_depth;
4148 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05004149 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004150
Jamie Wellnitzb8086082006-02-28 22:33:12 -05004151 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart876dd7d2012-09-29 11:31:28 -04004152 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004153 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04004154 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004155
James Smartfa61a542008-01-11 01:52:42 -05004156 /*
4157 * If there is a thread waiting for command completion
4158 * wake up the thread.
4159 */
James Smarta257bf92009-04-06 18:48:10 -04004160 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004161 if (lpfc_cmd->waitq)
4162 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04004163 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05004164 lpfc_release_scsi_buf(phba, lpfc_cmd);
4165 return;
4166 }
4167
James Smart92d7f7b2007-06-17 19:56:38 -05004168 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04004169 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05004170
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004171 /*
4172 * Check for queue full. If the lun is reporting queue full, then
4173 * back off the lun queue depth to prevent target overloads.
4174 */
James Smart58da1ff2008-04-07 10:15:56 -04004175 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
4176 NLP_CHK_NODE_ACT(pnode)) {
James Smarta257bf92009-04-06 18:48:10 -04004177 shost_for_each_device(tmp_sdev, shost) {
4178 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004179 continue;
4180 depth = scsi_track_queue_full(tmp_sdev,
James Smart5ffc2662009-11-18 15:39:44 -05004181 tmp_sdev->queue_depth-1);
4182 if (depth <= 0)
4183 continue;
James Smarte8b62012007-08-02 11:10:09 -04004184 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4185 "0711 detected queue full - lun queue "
4186 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04004187 lpfc_send_sdev_queuedepth_change_event(phba, vport,
James Smart5ffc2662009-11-18 15:39:44 -05004188 pnode,
4189 tmp_sdev->lun,
4190 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004191 }
4192 }
4193
James Smart876dd7d2012-09-29 11:31:28 -04004194 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004195 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04004196 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004197
James Smartfa61a542008-01-11 01:52:42 -05004198 /*
4199 * If there is a thread waiting for command completion
4200 * wake up the thread.
4201 */
James Smarta257bf92009-04-06 18:48:10 -04004202 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004203 if (lpfc_cmd->waitq)
4204 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04004205 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004206
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004207 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004208}
4209
James Smart34b02dc2008-08-24 21:49:55 -04004210/**
James Smart3621a712009-04-06 18:47:14 -04004211 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04004212 * @data: A pointer to the immediate command data portion of the IOCB.
4213 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
4214 *
4215 * The routine copies the entire FCP command from @fcp_cmnd to @data while
4216 * byte swapping the data to big endian format for transmission on the wire.
4217 **/
4218static void
4219lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
4220{
4221 int i, j;
4222 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
4223 i += sizeof(uint32_t), j++) {
4224 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
4225 }
4226}
4227
James Smart9bad7672008-12-04 22:39:02 -05004228/**
James Smartf1126682009-06-10 17:22:44 -04004229 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004230 * @vport: The virtual port for which this call is being executed.
4231 * @lpfc_cmd: The scsi command which needs to send.
4232 * @pnode: Pointer to lpfc_nodelist.
4233 *
4234 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04004235 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004236 **/
dea31012005-04-17 16:05:31 -05004237static void
James Smartf1126682009-06-10 17:22:44 -04004238lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05004239 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05004240{
James Smart2e0fef82007-06-17 19:56:36 -05004241 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004242 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4243 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4244 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4245 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
4246 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04004247 char tag[2];
James Smart027140e2012-08-03 12:35:44 -04004248 uint8_t *ptr;
4249 bool sli4;
dea31012005-04-17 16:05:31 -05004250
James Smart58da1ff2008-04-07 10:15:56 -04004251 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4252 return;
4253
dea31012005-04-17 16:05:31 -05004254 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04004255 /* clear task management bits */
4256 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05004257
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04004258 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4259 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004260
James Smart027140e2012-08-03 12:35:44 -04004261 ptr = &fcp_cmnd->fcpCdb[0];
4262 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4263 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4264 ptr += scsi_cmnd->cmd_len;
4265 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4266 }
4267
James Smart7e2b19f2007-10-29 11:00:39 -04004268 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
4269 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05004270 case HEAD_OF_QUEUE_TAG:
4271 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
4272 break;
4273 case ORDERED_QUEUE_TAG:
4274 fcp_cmnd->fcpCntl1 = ORDERED_Q;
4275 break;
4276 default:
4277 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
4278 break;
4279 }
4280 } else
James Smartfe8f7f92013-01-03 15:43:03 -05004281 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
dea31012005-04-17 16:05:31 -05004282
James Smart027140e2012-08-03 12:35:44 -04004283 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
4284
dea31012005-04-17 16:05:31 -05004285 /*
4286 * There are three possibilities here - use scatter-gather segment, use
4287 * the single mapping, or neither. Start the lpfc command prep by
4288 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4289 * data bde entry.
4290 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05004291 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05004292 if (datadir == DMA_TO_DEVICE) {
4293 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart027140e2012-08-03 12:35:44 -04004294 if (sli4)
4295 iocb_cmd->ulpPU = PARM_READ_CHECK;
4296 else {
James Smart3772a992009-05-22 14:50:54 -04004297 iocb_cmd->un.fcpi.fcpi_parm = 0;
4298 iocb_cmd->ulpPU = 0;
James Smart027140e2012-08-03 12:35:44 -04004299 }
dea31012005-04-17 16:05:31 -05004300 fcp_cmnd->fcpCntl3 = WRITE_DATA;
4301 phba->fc4OutputRequests++;
4302 } else {
4303 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4304 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05004305 fcp_cmnd->fcpCntl3 = READ_DATA;
4306 phba->fc4InputRequests++;
4307 }
4308 } else {
4309 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4310 iocb_cmd->un.fcpi.fcpi_parm = 0;
4311 iocb_cmd->ulpPU = 0;
4312 fcp_cmnd->fcpCntl3 = 0;
4313 phba->fc4ControlRequests++;
4314 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004315 if (phba->sli_rev == 3 &&
4316 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004317 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004318 /*
4319 * Finish initializing those IOCB fields that are independent
4320 * of the scsi_cmnd request_buffer
4321 */
4322 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart027140e2012-08-03 12:35:44 -04004323 if (sli4)
James Smart6d368e52011-05-24 11:44:12 -04004324 piocbq->iocb.ulpContext =
4325 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05004326 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4327 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05004328 else
4329 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05004330
4331 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4332 piocbq->context1 = lpfc_cmd;
4333 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4334 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05004335 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05004336}
4337
James Smart9bad7672008-12-04 22:39:02 -05004338/**
James Smart6d368e52011-05-24 11:44:12 -04004339 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004340 * @vport: The virtual port for which this call is being executed.
4341 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4342 * @lun: Logical unit number.
4343 * @task_mgmt_cmd: SCSI task management command.
4344 *
James Smart3772a992009-05-22 14:50:54 -04004345 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4346 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004347 *
4348 * Return codes:
4349 * 0 - Error
4350 * 1 - Success
4351 **/
dea31012005-04-17 16:05:31 -05004352static int
James Smartf1126682009-06-10 17:22:44 -04004353lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05004354 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04004355 unsigned int lun,
dea31012005-04-17 16:05:31 -05004356 uint8_t task_mgmt_cmd)
4357{
dea31012005-04-17 16:05:31 -05004358 struct lpfc_iocbq *piocbq;
4359 IOCB_t *piocb;
4360 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04004361 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05004362 struct lpfc_nodelist *ndlp = rdata->pnode;
4363
James Smart58da1ff2008-04-07 10:15:56 -04004364 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4365 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05004366 return 0;
dea31012005-04-17 16:05:31 -05004367
dea31012005-04-17 16:05:31 -05004368 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004369 piocbq->vport = vport;
4370
dea31012005-04-17 16:05:31 -05004371 piocb = &piocbq->iocb;
4372
4373 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04004374 /* Clear out any old data in the FCP command area */
4375 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4376 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004377 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05004378 if (vport->phba->sli_rev == 3 &&
4379 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004380 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004381 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05004382 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04004383 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4384 piocb->ulpContext =
4385 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4386 }
dea31012005-04-17 16:05:31 -05004387 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
4388 piocb->ulpFCP2Rcvy = 1;
4389 }
4390 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
4391
4392 /* ulpTimeout is only one byte */
4393 if (lpfc_cmd->timeout > 0xff) {
4394 /*
4395 * Do not timeout the command at the firmware level.
4396 * The driver will provide the timeout mechanism.
4397 */
4398 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04004399 } else
dea31012005-04-17 16:05:31 -05004400 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04004401
4402 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4403 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004404
James Smart2e0fef82007-06-17 19:56:36 -05004405 return 1;
dea31012005-04-17 16:05:31 -05004406}
4407
James Smart9bad7672008-12-04 22:39:02 -05004408/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004409 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04004410 * @phba: The hba struct for which this call is being executed.
4411 * @dev_grp: The HBA PCI-Device group number.
4412 *
4413 * This routine sets up the SCSI interface API function jump table in @phba
4414 * struct.
4415 * Returns: 0 - success, -ENODEV - failure.
4416 **/
4417int
4418lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4419{
4420
James Smartf1126682009-06-10 17:22:44 -04004421 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4422 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04004423
James Smart3772a992009-05-22 14:50:54 -04004424 switch (dev_grp) {
4425 case LPFC_PCI_DEV_LP:
4426 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4427 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05004428 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004429 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05004430 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004431 break;
James Smartda0436e2009-05-22 14:51:39 -04004432 case LPFC_PCI_DEV_OC:
4433 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4434 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05004435 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004436 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05004437 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004438 break;
James Smart3772a992009-05-22 14:50:54 -04004439 default:
4440 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4441 "1418 Invalid HBA PCI-device group: 0x%x\n",
4442 dev_grp);
4443 return -ENODEV;
4444 break;
4445 }
James Smart3772a992009-05-22 14:50:54 -04004446 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05004447 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04004448 return 0;
4449}
4450
4451/**
James Smart3621a712009-04-06 18:47:14 -04004452 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05004453 * @phba: The Hba for which this call is being executed.
4454 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4455 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4456 *
4457 * This routine is IOCB completion routine for device reset and target reset
4458 * routine. This routine release scsi buffer associated with lpfc_cmd.
4459 **/
James Smart7054a602007-04-25 09:52:34 -04004460static void
4461lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4462 struct lpfc_iocbq *cmdiocbq,
4463 struct lpfc_iocbq *rspiocbq)
4464{
4465 struct lpfc_scsi_buf *lpfc_cmd =
4466 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4467 if (lpfc_cmd)
4468 lpfc_release_scsi_buf(phba, lpfc_cmd);
4469 return;
4470}
4471
James Smart9bad7672008-12-04 22:39:02 -05004472/**
James Smart3621a712009-04-06 18:47:14 -04004473 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05004474 * @host: The scsi host for which this call is being executed.
4475 *
4476 * This routine provides module information about hba.
4477 *
4478 * Reutrn code:
4479 * Pointer to char - Success.
4480 **/
dea31012005-04-17 16:05:31 -05004481const char *
4482lpfc_info(struct Scsi_Host *host)
4483{
James Smart2e0fef82007-06-17 19:56:36 -05004484 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4485 struct lpfc_hba *phba = vport->phba;
James Smart8b68cd52012-09-29 11:32:37 -04004486 int len, link_speed = 0;
dea31012005-04-17 16:05:31 -05004487 static char lpfcinfobuf[384];
4488
4489 memset(lpfcinfobuf,0,384);
4490 if (phba && phba->pcidev){
4491 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4492 len = strlen(lpfcinfobuf);
4493 snprintf(lpfcinfobuf + len,
4494 384-len,
4495 " on PCI bus %02x device %02x irq %d",
4496 phba->pcidev->bus->number,
4497 phba->pcidev->devfn,
4498 phba->pcidev->irq);
4499 len = strlen(lpfcinfobuf);
4500 if (phba->Port[0]) {
4501 snprintf(lpfcinfobuf + len,
4502 384-len,
4503 " port %s",
4504 phba->Port);
4505 }
James Smart65467b62010-01-26 23:08:29 -05004506 len = strlen(lpfcinfobuf);
James Smart8b68cd52012-09-29 11:32:37 -04004507 if (phba->sli_rev <= LPFC_SLI_REV3) {
4508 link_speed = lpfc_sli_port_speed_get(phba);
4509 } else {
4510 if (phba->sli4_hba.link_state.logical_speed)
4511 link_speed =
4512 phba->sli4_hba.link_state.logical_speed;
4513 else
4514 link_speed = phba->sli4_hba.link_state.speed;
James Smart65467b62010-01-26 23:08:29 -05004515 }
James Smart8b68cd52012-09-29 11:32:37 -04004516 if (link_speed != 0)
4517 snprintf(lpfcinfobuf + len, 384-len,
4518 " Logical Link Speed: %d Mbps", link_speed);
dea31012005-04-17 16:05:31 -05004519 }
4520 return lpfcinfobuf;
4521}
4522
James Smart9bad7672008-12-04 22:39:02 -05004523/**
James Smart3621a712009-04-06 18:47:14 -04004524 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05004525 * @phba: The Hba for which this call is being executed.
4526 *
4527 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4528 * The default value of cfg_poll_tmo is 10 milliseconds.
4529 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004530static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4531{
4532 unsigned long poll_tmo_expires =
4533 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4534
James Smart0e9bb8d2013-03-01 16:35:12 -05004535 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004536 mod_timer(&phba->fcp_poll_timer,
4537 poll_tmo_expires);
4538}
4539
James Smart9bad7672008-12-04 22:39:02 -05004540/**
James Smart3621a712009-04-06 18:47:14 -04004541 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004542 * @phba: The Hba for which this call is being executed.
4543 *
4544 * This routine starts the fcp_poll_timer of @phba.
4545 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004546void lpfc_poll_start_timer(struct lpfc_hba * phba)
4547{
4548 lpfc_poll_rearm_timer(phba);
4549}
4550
James Smart9bad7672008-12-04 22:39:02 -05004551/**
James Smart3621a712009-04-06 18:47:14 -04004552 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05004553 * @ptr: Map to lpfc_hba data structure pointer.
4554 *
4555 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4556 * and FCP Ring interrupt is disable.
4557 **/
4558
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004559void lpfc_poll_timeout(unsigned long ptr)
4560{
James Smart2e0fef82007-06-17 19:56:36 -05004561 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004562
4563 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004564 lpfc_sli_handle_fast_ring_event(phba,
4565 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4566
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004567 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4568 lpfc_poll_rearm_timer(phba);
4569 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004570}
4571
James Smart9bad7672008-12-04 22:39:02 -05004572/**
James Smart3621a712009-04-06 18:47:14 -04004573 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05004574 * @cmnd: Pointer to scsi_cmnd data structure.
4575 * @done: Pointer to done routine.
4576 *
4577 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4578 * This routine prepares an IOCB from scsi command and provides to firmware.
4579 * The @done callback is invoked after driver finished processing the command.
4580 *
4581 * Return value :
4582 * 0 - Success
4583 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4584 **/
dea31012005-04-17 16:05:31 -05004585static int
James Smartb9a7c632012-08-03 12:35:24 -04004586lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004587{
James Smart2e0fef82007-06-17 19:56:36 -05004588 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4589 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004590 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004591 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004592 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004593 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004594 int err;
dea31012005-04-17 16:05:31 -05004595
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004596 err = fc_remote_port_chkready(rport);
4597 if (err) {
4598 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004599 goto out_fail_command;
4600 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004601 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004602
James Smartbf086112011-08-21 21:48:13 -04004603 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004604 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004605
James Smart6a9c52c2009-10-02 15:16:51 -04004606 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4607 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4608 " op:%02x str=%s without registering for"
4609 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004610 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4611 dif_op_str[scsi_get_prot_op(cmnd)]);
4612 goto out_fail_command;
4613 }
4614
dea31012005-04-17 16:05:31 -05004615 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004616 * Catch race where our node has transitioned, but the
4617 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004618 */
James Smart6b415f52012-06-12 13:54:59 -04004619 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4620 goto out_tgt_busy;
James Smart7dc517d2010-07-14 15:32:10 -04004621 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
Mike Christie34963432011-02-25 14:04:28 -06004622 goto out_tgt_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05004623
James Smart19ca7602010-11-20 23:11:55 -05004624 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05004625 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004626 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004627
James Smarte8b62012007-08-02 11:10:09 -04004628 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4629 "0707 driver's buffer pool is empty, "
4630 "IO busied\n");
dea31012005-04-17 16:05:31 -05004631 goto out_host_busy;
4632 }
4633
4634 /*
4635 * Store the midlayer's command structure for the completion phase
4636 * and complete the command initialization.
4637 */
4638 lpfc_cmd->pCmd = cmnd;
4639 lpfc_cmd->rdata = rdata;
4640 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04004641 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05004642 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
dea31012005-04-17 16:05:31 -05004643
James Smarte2a0a9d2008-12-04 22:40:02 -05004644 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004645 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004646 lpfc_printf_vlog(vport,
4647 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004648 "9033 BLKGRD: rcvd %s cmd:x%x "
4649 "sector x%llx cnt %u pt %x\n",
4650 dif_op_str[scsi_get_prot_op(cmnd)],
4651 cmnd->cmnd[0],
4652 (unsigned long long)scsi_get_lba(cmnd),
4653 blk_rq_sectors(cmnd->request),
4654 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004655 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004656 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4657 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004658 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004659 lpfc_printf_vlog(vport,
4660 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004661 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4662 "x%x sector x%llx cnt %u pt %x\n",
4663 cmnd->cmnd[0],
4664 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004665 blk_rq_sectors(cmnd->request),
James Smart26134702012-08-14 14:25:50 -04004666 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004667 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004668 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4669 }
4670
dea31012005-04-17 16:05:31 -05004671 if (err)
4672 goto out_host_busy_free_buf;
4673
James Smart2e0fef82007-06-17 19:56:36 -05004674 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004675
James Smart977b5a02008-09-07 11:52:04 -04004676 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04004677 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004678 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05004679 if (err) {
4680 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05004681 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004682 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004683 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004684 lpfc_sli_handle_fast_ring_event(phba,
4685 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4686
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004687 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4688 lpfc_poll_rearm_timer(phba);
4689 }
4690
dea31012005-04-17 16:05:31 -05004691 return 0;
4692
4693 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04004694 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004695 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004696 out_host_busy:
4697 return SCSI_MLQUEUE_HOST_BUSY;
4698
Mike Christie34963432011-02-25 14:04:28 -06004699 out_tgt_busy:
4700 return SCSI_MLQUEUE_TARGET_BUSY;
4701
dea31012005-04-17 16:05:31 -05004702 out_fail_command:
James Smartb9a7c632012-08-03 12:35:24 -04004703 cmnd->scsi_done(cmnd);
dea31012005-04-17 16:05:31 -05004704 return 0;
4705}
4706
Jeff Garzikf2812332010-11-16 02:10:29 -05004707
James Smart9bad7672008-12-04 22:39:02 -05004708/**
James Smart3621a712009-04-06 18:47:14 -04004709 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004710 * @cmnd: Pointer to scsi_cmnd data structure.
4711 *
4712 * This routine aborts @cmnd pending in base driver.
4713 *
4714 * Return code :
4715 * 0x2003 - Error
4716 * 0x2002 - Success
4717 **/
dea31012005-04-17 16:05:31 -05004718static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004719lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004720{
James Smart2e0fef82007-06-17 19:56:36 -05004721 struct Scsi_Host *shost = cmnd->device->host;
4722 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4723 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004724 struct lpfc_iocbq *iocb;
4725 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004726 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004727 IOCB_t *cmd, *icmd;
James Smart3a707302012-06-12 13:54:42 -04004728 int ret = SUCCESS, status = 0;
James Smart876dd7d2012-09-29 11:31:28 -04004729 unsigned long flags;
James Smartfa61a542008-01-11 01:52:42 -05004730 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004731
James Smart3a707302012-06-12 13:54:42 -04004732 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004733 if (status != 0 && status != SUCCESS)
James Smart3a707302012-06-12 13:54:42 -04004734 return status;
James Smart4f2e66c2012-05-09 21:17:07 -04004735
James Smart876dd7d2012-09-29 11:31:28 -04004736 spin_lock_irqsave(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004737 /* driver queued commands are in process of being flushed */
4738 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
James Smart876dd7d2012-09-29 11:31:28 -04004739 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004740 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4741 "3168 SCSI Layer abort requested I/O has been "
4742 "flushed by LLD.\n");
4743 return FAILED;
4744 }
4745
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004746 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smart92e3af62012-08-14 14:26:28 -04004747 if (!lpfc_cmd || !lpfc_cmd->pCmd) {
James Smart876dd7d2012-09-29 11:31:28 -04004748 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarteee88772010-09-29 11:19:08 -04004749 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4750 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004751 "x%x ID %d LUN %d\n",
James Smart3a707302012-06-12 13:54:42 -04004752 SUCCESS, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04004753 return SUCCESS;
4754 }
dea31012005-04-17 16:05:31 -05004755
James Smart4f2e66c2012-05-09 21:17:07 -04004756 iocb = &lpfc_cmd->cur_iocbq;
4757 /* the command is in process of being cancelled */
4758 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
James Smart876dd7d2012-09-29 11:31:28 -04004759 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004760 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4761 "3169 SCSI Layer abort requested I/O has been "
4762 "cancelled by LLD.\n");
4763 return FAILED;
4764 }
dea31012005-04-17 16:05:31 -05004765 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004766 * If pCmd field of the corresponding lpfc_scsi_buf structure
4767 * points to a different SCSI command, then the driver has
4768 * already completed this command, but the midlayer did not
James Smart4f2e66c2012-05-09 21:17:07 -04004769 * see the completion before the eh fired. Just return SUCCESS.
dea31012005-04-17 16:05:31 -05004770 */
James Smart4f2e66c2012-05-09 21:17:07 -04004771 if (lpfc_cmd->pCmd != cmnd) {
4772 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4773 "3170 SCSI Layer abort requested I/O has been "
4774 "completed by LLD.\n");
4775 goto out_unlock;
4776 }
dea31012005-04-17 16:05:31 -05004777
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004778 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004779
James Smart4f2e66c2012-05-09 21:17:07 -04004780 abtsiocb = __lpfc_sli_get_iocbq(phba);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004781 if (abtsiocb == NULL) {
4782 ret = FAILED;
James Smart4f2e66c2012-05-09 21:17:07 -04004783 goto out_unlock;
dea31012005-04-17 16:05:31 -05004784 }
4785
dea31012005-04-17 16:05:31 -05004786 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004787 * The scsi command can not be in txq and it is in flight because the
4788 * pCmd is still pointig at the SCSI command we have to abort. There
4789 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004790 */
dea31012005-04-17 16:05:31 -05004791
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004792 cmd = &iocb->iocb;
4793 icmd = &abtsiocb->iocb;
4794 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4795 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004796 if (phba->sli_rev == LPFC_SLI_REV4)
4797 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4798 else
4799 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004800
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004801 icmd->ulpLe = 1;
4802 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004803
4804 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4805 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05004806 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05004807
James Smart2e0fef82007-06-17 19:56:36 -05004808 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004809 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4810 else
4811 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004812
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004813 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004814 abtsiocb->vport = vport;
James Smart4f2e66c2012-05-09 21:17:07 -04004815 /* no longer need the lock after this point */
James Smart876dd7d2012-09-29 11:31:28 -04004816 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004817
James Smart3772a992009-05-22 14:50:54 -04004818 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
4819 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004820 lpfc_sli_release_iocbq(phba, abtsiocb);
4821 ret = FAILED;
4822 goto out;
4823 }
4824
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004825 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004826 lpfc_sli_handle_fast_ring_event(phba,
4827 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004828
James Smartfa61a542008-01-11 01:52:42 -05004829 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004830 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004831 wait_event_timeout(waitq,
4832 (lpfc_cmd->pCmd != cmnd),
James Smart256ec0d2013-04-17 20:14:58 -04004833 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
James Smartfa61a542008-01-11 01:52:42 -05004834 lpfc_cmd->waitq = NULL;
dea31012005-04-17 16:05:31 -05004835
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004836 if (lpfc_cmd->pCmd == cmnd) {
4837 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04004838 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4839 "0748 abort handler timed out waiting "
James Smart247ca942012-08-14 14:26:13 -04004840 "for abortng I/O (xri:x%x) to complete: "
4841 "ret %#x, ID %d, LUN %d\n",
4842 iocb->sli4_xritag, ret,
4843 cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004844 }
James Smart4f2e66c2012-05-09 21:17:07 -04004845 goto out;
dea31012005-04-17 16:05:31 -05004846
James Smart4f2e66c2012-05-09 21:17:07 -04004847out_unlock:
James Smart876dd7d2012-09-29 11:31:28 -04004848 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004849out:
James Smarte8b62012007-08-02 11:10:09 -04004850 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4851 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004852 "LUN %d\n", ret, cmnd->device->id,
4853 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004854 return ret;
dea31012005-04-17 16:05:31 -05004855}
4856
James Smartbbb9d182009-06-10 17:23:16 -04004857static char *
4858lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4859{
4860 switch (task_mgmt_cmd) {
4861 case FCP_ABORT_TASK_SET:
4862 return "ABORT_TASK_SET";
4863 case FCP_CLEAR_TASK_SET:
4864 return "FCP_CLEAR_TASK_SET";
4865 case FCP_BUS_RESET:
4866 return "FCP_BUS_RESET";
4867 case FCP_LUN_RESET:
4868 return "FCP_LUN_RESET";
4869 case FCP_TARGET_RESET:
4870 return "FCP_TARGET_RESET";
4871 case FCP_CLEAR_ACA:
4872 return "FCP_CLEAR_ACA";
4873 case FCP_TERMINATE_TASK:
4874 return "FCP_TERMINATE_TASK";
4875 default:
4876 return "unknown";
4877 }
4878}
4879
4880/**
4881 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
4882 * @vport: The virtual port for which this call is being executed.
4883 * @rdata: Pointer to remote port local data
4884 * @tgt_id: Target ID of remote device.
4885 * @lun_id: Lun number for the TMF
4886 * @task_mgmt_cmd: type of TMF to send
4887 *
4888 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
4889 * a remote port.
4890 *
4891 * Return Code:
4892 * 0x2003 - Error
4893 * 0x2002 - Success.
4894 **/
4895static int
4896lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
4897 unsigned tgt_id, unsigned int lun_id,
4898 uint8_t task_mgmt_cmd)
4899{
4900 struct lpfc_hba *phba = vport->phba;
4901 struct lpfc_scsi_buf *lpfc_cmd;
4902 struct lpfc_iocbq *iocbq;
4903 struct lpfc_iocbq *iocbqrsp;
James Smart5989b8d2010-10-22 11:06:56 -04004904 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004905 int ret;
4906 int status;
4907
James Smart5989b8d2010-10-22 11:06:56 -04004908 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smartbbb9d182009-06-10 17:23:16 -04004909 return FAILED;
4910
James Smart19ca7602010-11-20 23:11:55 -05004911 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
James Smartbbb9d182009-06-10 17:23:16 -04004912 if (lpfc_cmd == NULL)
4913 return FAILED;
4914 lpfc_cmd->timeout = 60;
4915 lpfc_cmd->rdata = rdata;
4916
4917 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
4918 task_mgmt_cmd);
4919 if (!status) {
4920 lpfc_release_scsi_buf(phba, lpfc_cmd);
4921 return FAILED;
4922 }
4923
4924 iocbq = &lpfc_cmd->cur_iocbq;
4925 iocbqrsp = lpfc_sli_get_iocbq(phba);
4926 if (iocbqrsp == NULL) {
4927 lpfc_release_scsi_buf(phba, lpfc_cmd);
4928 return FAILED;
4929 }
4930
4931 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4932 "0702 Issue %s to TGT %d LUN %d "
James Smart6d368e52011-05-24 11:44:12 -04004933 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04004934 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04004935 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
4936 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04004937
4938 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
4939 iocbq, iocbqrsp, lpfc_cmd->timeout);
4940 if (status != IOCB_SUCCESS) {
4941 if (status == IOCB_TIMEDOUT) {
4942 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
4943 ret = TIMEOUT_ERROR;
4944 } else
4945 ret = FAILED;
4946 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4947 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart6d368e52011-05-24 11:44:12 -04004948 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
4949 "iocb_flag x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04004950 lpfc_taskmgmt_name(task_mgmt_cmd),
4951 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
James Smart6d368e52011-05-24 11:44:12 -04004952 iocbqrsp->iocb.un.ulpWord[4],
4953 iocbq->iocb_flag);
James Smart2a9bf3d2010-06-07 15:24:45 -04004954 } else if (status == IOCB_BUSY)
4955 ret = FAILED;
4956 else
James Smartbbb9d182009-06-10 17:23:16 -04004957 ret = SUCCESS;
4958
4959 lpfc_sli_release_iocbq(phba, iocbqrsp);
4960
4961 if (ret != TIMEOUT_ERROR)
4962 lpfc_release_scsi_buf(phba, lpfc_cmd);
4963
4964 return ret;
4965}
4966
4967/**
4968 * lpfc_chk_tgt_mapped -
4969 * @vport: The virtual port to check on
4970 * @cmnd: Pointer to scsi_cmnd data structure.
4971 *
4972 * This routine delays until the scsi target (aka rport) for the
4973 * command exists (is present and logged in) or we declare it non-existent.
4974 *
4975 * Return code :
4976 * 0x2003 - Error
4977 * 0x2002 - Success
4978 **/
4979static int
4980lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
4981{
4982 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004983 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004984 unsigned long later;
4985
James Smart1c6f4ef52009-11-18 15:40:49 -05004986 if (!rdata) {
4987 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4988 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
4989 return FAILED;
4990 }
4991 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004992 /*
4993 * If target is not in a MAPPED state, delay until
4994 * target is rediscovered or devloss timeout expires.
4995 */
4996 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
4997 while (time_after(later, jiffies)) {
4998 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4999 return FAILED;
5000 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
5001 return SUCCESS;
5002 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
5003 rdata = cmnd->device->hostdata;
5004 if (!rdata)
5005 return FAILED;
5006 pnode = rdata->pnode;
5007 }
5008 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
5009 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
5010 return FAILED;
5011 return SUCCESS;
5012}
5013
5014/**
5015 * lpfc_reset_flush_io_context -
5016 * @vport: The virtual port (scsi_host) for the flush context
5017 * @tgt_id: If aborting by Target contect - specifies the target id
5018 * @lun_id: If aborting by Lun context - specifies the lun id
5019 * @context: specifies the context level to flush at.
5020 *
5021 * After a reset condition via TMF, we need to flush orphaned i/o
5022 * contexts from the adapter. This routine aborts any contexts
5023 * outstanding, then waits for their completions. The wait is
5024 * bounded by devloss_tmo though.
5025 *
5026 * Return code :
5027 * 0x2003 - Error
5028 * 0x2002 - Success
5029 **/
5030static int
5031lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5032 uint64_t lun_id, lpfc_ctx_cmd context)
5033{
5034 struct lpfc_hba *phba = vport->phba;
5035 unsigned long later;
5036 int cnt;
5037
5038 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5039 if (cnt)
5040 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
5041 tgt_id, lun_id, context);
5042 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5043 while (time_after(later, jiffies) && cnt) {
5044 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5045 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5046 }
5047 if (cnt) {
5048 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5049 "0724 I/O flush failure for context %s : cnt x%x\n",
5050 ((context == LPFC_CTX_LUN) ? "LUN" :
5051 ((context == LPFC_CTX_TGT) ? "TGT" :
5052 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5053 cnt);
5054 return FAILED;
5055 }
5056 return SUCCESS;
5057}
5058
James Smart9bad7672008-12-04 22:39:02 -05005059/**
James Smart3621a712009-04-06 18:47:14 -04005060 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05005061 * @cmnd: Pointer to scsi_cmnd data structure.
5062 *
James Smartbbb9d182009-06-10 17:23:16 -04005063 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05005064 * command.
5065 *
5066 * Return code :
5067 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04005068 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005069 **/
dea31012005-04-17 16:05:31 -05005070static int
James Smart7054a602007-04-25 09:52:34 -04005071lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005072{
James Smart2e0fef82007-06-17 19:56:36 -05005073 struct Scsi_Host *shost = cmnd->device->host;
5074 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005075 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005076 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005077 unsigned tgt_id = cmnd->device->id;
5078 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04005079 struct lpfc_scsi_event_header scsi_event;
James Smart3a707302012-06-12 13:54:42 -04005080 int status, ret = SUCCESS;
dea31012005-04-17 16:05:31 -05005081
James Smart1c6f4ef52009-11-18 15:40:49 -05005082 if (!rdata) {
5083 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5084 "0798 Device Reset rport failure: rdata x%p\n", rdata);
5085 return FAILED;
5086 }
5087 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005088 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005089 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005090 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005091
5092 status = lpfc_chk_tgt_mapped(vport, cmnd);
5093 if (status == FAILED) {
5094 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5095 "0721 Device Reset rport failure: rdata x%p\n", rdata);
5096 return FAILED;
5097 }
5098
5099 scsi_event.event_type = FC_REG_SCSI_EVENT;
5100 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5101 scsi_event.lun = lun_id;
5102 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5103 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5104
5105 fc_host_post_vendor_event(shost, fc_get_event_number(),
5106 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5107
5108 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5109 FCP_LUN_RESET);
5110
5111 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5112 "0713 SCSI layer issued Device Reset (%d, %d) "
5113 "return x%x\n", tgt_id, lun_id, status);
5114
dea31012005-04-17 16:05:31 -05005115 /*
James Smartbbb9d182009-06-10 17:23:16 -04005116 * We have to clean up i/o as : they may be orphaned by the TMF;
5117 * or if the TMF failed, they may be in an indeterminate state.
5118 * So, continue on.
5119 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05005120 */
James Smart3a707302012-06-12 13:54:42 -04005121 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005122 LPFC_CTX_LUN);
James Smart3a707302012-06-12 13:54:42 -04005123 return ret;
James Smartbbb9d182009-06-10 17:23:16 -04005124}
5125
5126/**
5127 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5128 * @cmnd: Pointer to scsi_cmnd data structure.
5129 *
5130 * This routine does a target reset by sending a TARGET_RESET task management
5131 * command.
5132 *
5133 * Return code :
5134 * 0x2003 - Error
5135 * 0x2002 - Success
5136 **/
5137static int
5138lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5139{
5140 struct Scsi_Host *shost = cmnd->device->host;
5141 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5142 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005143 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005144 unsigned tgt_id = cmnd->device->id;
5145 unsigned int lun_id = cmnd->device->lun;
5146 struct lpfc_scsi_event_header scsi_event;
James Smart3a707302012-06-12 13:54:42 -04005147 int status, ret = SUCCESS;
James Smartbbb9d182009-06-10 17:23:16 -04005148
James Smart1c6f4ef52009-11-18 15:40:49 -05005149 if (!rdata) {
5150 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5151 "0799 Target Reset rport failure: rdata x%p\n", rdata);
5152 return FAILED;
5153 }
5154 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005155 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005156 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005157 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005158
5159 status = lpfc_chk_tgt_mapped(vport, cmnd);
5160 if (status == FAILED) {
5161 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5162 "0722 Target Reset rport failure: rdata x%p\n", rdata);
5163 return FAILED;
dea31012005-04-17 16:05:31 -05005164 }
James Smartea2151b2008-09-07 11:52:10 -04005165
5166 scsi_event.event_type = FC_REG_SCSI_EVENT;
5167 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5168 scsi_event.lun = 0;
5169 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5170 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5171
James Smartbbb9d182009-06-10 17:23:16 -04005172 fc_host_post_vendor_event(shost, fc_get_event_number(),
5173 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04005174
James Smartbbb9d182009-06-10 17:23:16 -04005175 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5176 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05005177
James Smarte8b62012007-08-02 11:10:09 -04005178 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04005179 "0723 SCSI layer issued Target Reset (%d, %d) "
5180 "return x%x\n", tgt_id, lun_id, status);
5181
5182 /*
5183 * We have to clean up i/o as : they may be orphaned by the TMF;
5184 * or if the TMF failed, they may be in an indeterminate state.
5185 * So, continue on.
5186 * We will report success if all the i/o aborts successfully.
5187 */
James Smart3a707302012-06-12 13:54:42 -04005188 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5189 LPFC_CTX_TGT);
5190 return ret;
dea31012005-04-17 16:05:31 -05005191}
5192
James Smart9bad7672008-12-04 22:39:02 -05005193/**
James Smart3621a712009-04-06 18:47:14 -04005194 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05005195 * @cmnd: Pointer to scsi_cmnd data structure.
5196 *
James Smartbbb9d182009-06-10 17:23:16 -04005197 * This routine does target reset to all targets on @cmnd->device->host.
5198 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05005199 *
James Smartbbb9d182009-06-10 17:23:16 -04005200 * Return code :
5201 * 0x2003 - Error
5202 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005203 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04005204static int
James Smart7054a602007-04-25 09:52:34 -04005205lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005206{
James Smart2e0fef82007-06-17 19:56:36 -05005207 struct Scsi_Host *shost = cmnd->device->host;
5208 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005209 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04005210 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04005211 int match;
5212 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04005213
5214 scsi_event.event_type = FC_REG_SCSI_EVENT;
5215 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5216 scsi_event.lun = 0;
5217 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5218 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5219
James Smartbbb9d182009-06-10 17:23:16 -04005220 fc_host_post_vendor_event(shost, fc_get_event_number(),
5221 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05005222
James Smartbf086112011-08-21 21:48:13 -04005223 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005224 if (status != 0 && status != SUCCESS)
James Smartbf086112011-08-21 21:48:13 -04005225 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005226
dea31012005-04-17 16:05:31 -05005227 /*
5228 * Since the driver manages a single bus device, reset all
5229 * targets known to the driver. Should any target reset
5230 * fail, this routine returns failure to the midlayer.
5231 */
James Smarte17da182006-07-06 15:49:25 -04005232 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04005233 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05005234 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05005235 spin_lock_irq(shost->host_lock);
5236 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005237 if (!NLP_CHK_NODE_ACT(ndlp))
5238 continue;
James Smarta6571c62012-10-31 14:44:42 -04005239 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5240 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5241 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005242 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04005243 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04005244 ndlp->rport) {
dea31012005-04-17 16:05:31 -05005245 match = 1;
5246 break;
5247 }
5248 }
James Smart2e0fef82007-06-17 19:56:36 -05005249 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005250 if (!match)
5251 continue;
James Smartbbb9d182009-06-10 17:23:16 -04005252
5253 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
5254 i, 0, FCP_TARGET_RESET);
5255
5256 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04005257 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5258 "0700 Bus Reset on target %d failed\n",
5259 i);
James Smart915caaa2008-06-14 22:52:38 -04005260 ret = FAILED;
dea31012005-04-17 16:05:31 -05005261 }
5262 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005263 /*
James Smartbbb9d182009-06-10 17:23:16 -04005264 * We have to clean up i/o as : they may be orphaned by the TMFs
5265 * above; or if any of the TMFs failed, they may be in an
5266 * indeterminate state.
5267 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005268 */
James Smartbbb9d182009-06-10 17:23:16 -04005269
5270 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5271 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005272 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04005273
James Smarte8b62012007-08-02 11:10:09 -04005274 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5275 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05005276 return ret;
5277}
5278
James Smart9bad7672008-12-04 22:39:02 -05005279/**
James Smart27b01b82012-05-09 21:19:44 -04005280 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
5281 * @cmnd: Pointer to scsi_cmnd data structure.
5282 *
5283 * This routine does host reset to the adaptor port. It brings the HBA
5284 * offline, performs a board restart, and then brings the board back online.
5285 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
5286 * reject all outstanding SCSI commands to the host and error returned
5287 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5288 * of error handling, it will only return error if resetting of the adapter
5289 * is not successful; in all other cases, will return success.
5290 *
5291 * Return code :
5292 * 0x2003 - Error
5293 * 0x2002 - Success
5294 **/
5295static int
5296lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5297{
5298 struct Scsi_Host *shost = cmnd->device->host;
5299 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5300 struct lpfc_hba *phba = vport->phba;
5301 int rc, ret = SUCCESS;
5302
James Smart618a5232012-06-12 13:54:36 -04005303 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart27b01b82012-05-09 21:19:44 -04005304 lpfc_offline(phba);
5305 rc = lpfc_sli_brdrestart(phba);
5306 if (rc)
5307 ret = FAILED;
5308 lpfc_online(phba);
5309 lpfc_unblock_mgmt_io(phba);
5310
5311 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
5312 "3172 SCSI layer issued Host Reset Data: x%x\n", ret);
5313 return ret;
5314}
5315
5316/**
James Smart3621a712009-04-06 18:47:14 -04005317 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05005318 * @sdev: Pointer to scsi_device.
5319 *
5320 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5321 * globally available list of scsi buffers. This routine also makes sure scsi
5322 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5323 * of scsi buffer exists for the lifetime of the driver.
5324 *
5325 * Return codes:
5326 * non-0 - Error
5327 * 0 - Success
5328 **/
dea31012005-04-17 16:05:31 -05005329static int
dea31012005-04-17 16:05:31 -05005330lpfc_slave_alloc(struct scsi_device *sdev)
5331{
James Smart2e0fef82007-06-17 19:56:36 -05005332 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5333 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005334 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04005335 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05005336 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04005337 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04005338 uint32_t sdev_cnt;
dea31012005-04-17 16:05:31 -05005339
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005340 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05005341 return -ENXIO;
5342
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005343 sdev->hostdata = rport->dd_data;
James Smartd7c47992010-06-08 18:31:54 -04005344 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005345
5346 /*
5347 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5348 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04005349 * HBA limit conveyed to the midlayer via the host structure. The
5350 * formula accounts for the lun_queue_depth + error handlers + 1
5351 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05005352 */
5353 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04005354 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05005355
James Smartd7c47992010-06-08 18:31:54 -04005356 /* If allocated buffers are enough do nothing */
5357 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5358 return 0;
5359
James Smart92d7f7b2007-06-17 19:56:38 -05005360 /* Allow some exchanges to be available always to complete discovery */
5361 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005362 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5363 "0704 At limitation of %d preallocated "
5364 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05005365 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05005366 /* Allow some exchanges to be available always to complete discovery */
5367 } else if (total + num_to_alloc >
5368 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005369 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5370 "0705 Allocation request of %d "
5371 "command buffers will exceed max of %d. "
5372 "Reducing allocation request to %d.\n",
5373 num_to_alloc, phba->cfg_hba_queue_depth,
5374 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05005375 num_to_alloc = phba->cfg_hba_queue_depth - total;
5376 }
James Smart3772a992009-05-22 14:50:54 -04005377 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
5378 if (num_to_alloc != num_allocated) {
James Smart96f70772013-04-17 20:16:15 -04005379 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5380 "0708 Allocation request of %d "
5381 "command buffers did not succeed. "
5382 "Allocated %d buffers.\n",
5383 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05005384 }
James Smart1c6f4ef52009-11-18 15:40:49 -05005385 if (num_allocated > 0)
5386 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05005387 return 0;
5388}
5389
James Smart9bad7672008-12-04 22:39:02 -05005390/**
James Smart3621a712009-04-06 18:47:14 -04005391 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05005392 * @sdev: Pointer to scsi_device.
5393 *
5394 * This routine configures following items
5395 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05005396 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5397 *
5398 * Return codes:
5399 * 0 - Success
5400 **/
dea31012005-04-17 16:05:31 -05005401static int
5402lpfc_slave_configure(struct scsi_device *sdev)
5403{
James Smart2e0fef82007-06-17 19:56:36 -05005404 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5405 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005406
5407 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04005408 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005409 else
James Smart3de2a652007-08-02 11:09:59 -04005410 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005411
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005412 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005413 lpfc_sli_handle_fast_ring_event(phba,
5414 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005415 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5416 lpfc_poll_rearm_timer(phba);
5417 }
5418
dea31012005-04-17 16:05:31 -05005419 return 0;
5420}
5421
James Smart9bad7672008-12-04 22:39:02 -05005422/**
James Smart3621a712009-04-06 18:47:14 -04005423 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05005424 * @sdev: Pointer to scsi_device.
5425 *
5426 * This routine sets @sdev hostatdata filed to null.
5427 **/
dea31012005-04-17 16:05:31 -05005428static void
5429lpfc_slave_destroy(struct scsi_device *sdev)
5430{
James Smartd7c47992010-06-08 18:31:54 -04005431 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5432 struct lpfc_hba *phba = vport->phba;
5433 atomic_dec(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005434 sdev->hostdata = NULL;
5435 return;
5436}
5437
James Smart92d7f7b2007-06-17 19:56:38 -05005438
dea31012005-04-17 16:05:31 -05005439struct scsi_host_template lpfc_template = {
5440 .module = THIS_MODULE,
5441 .name = LPFC_DRIVER_NAME,
5442 .info = lpfc_info,
5443 .queuecommand = lpfc_queuecommand,
5444 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005445 .eh_device_reset_handler = lpfc_device_reset_handler,
5446 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04005447 .eh_bus_reset_handler = lpfc_bus_reset_handler,
James Smart27b01b82012-05-09 21:19:44 -04005448 .eh_host_reset_handler = lpfc_host_reset_handler,
dea31012005-04-17 16:05:31 -05005449 .slave_alloc = lpfc_slave_alloc,
5450 .slave_configure = lpfc_slave_configure,
5451 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04005452 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05005453 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005454 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05005455 .cmd_per_lun = LPFC_CMD_PER_LUN,
5456 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05005457 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04005458 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04005459 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05005460 .change_queue_depth = lpfc_change_queue_depth,
James Smartfe8f7f92013-01-03 15:43:03 -05005461 .change_queue_type = lpfc_change_queue_type,
dea31012005-04-17 16:05:31 -05005462};
James Smart3de2a652007-08-02 11:09:59 -04005463
5464struct scsi_host_template lpfc_vport_template = {
5465 .module = THIS_MODULE,
5466 .name = LPFC_DRIVER_NAME,
5467 .info = lpfc_info,
5468 .queuecommand = lpfc_queuecommand,
5469 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005470 .eh_device_reset_handler = lpfc_device_reset_handler,
5471 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04005472 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5473 .slave_alloc = lpfc_slave_alloc,
5474 .slave_configure = lpfc_slave_configure,
5475 .slave_destroy = lpfc_slave_destroy,
5476 .scan_finished = lpfc_scan_finished,
5477 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005478 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04005479 .cmd_per_lun = LPFC_CMD_PER_LUN,
5480 .use_clustering = ENABLE_CLUSTERING,
5481 .shost_attrs = lpfc_vport_attrs,
5482 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05005483 .change_queue_depth = lpfc_change_queue_depth,
James Smartfe8f7f92013-01-03 15:43:03 -05005484 .change_queue_type = lpfc_change_queue_type,
James Smart3de2a652007-08-02 11:09:59 -04005485};