blob: 443bbca9694cbd4b429b4e0d2523589a00191009 [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 Smart92c13f22013-05-31 17:05:45 -04004 * Copyright (C) 2004-2013 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 Smart9c6aa9d2013-05-31 17:03:39 -040075static int
76lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc);
James Smarte2a0a9d2008-12-04 22:40:02 -050077
78static void
James Smart6a9c52c2009-10-02 15:16:51 -040079lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -050080{
81 void *src, *dst;
82 struct scatterlist *sgde = scsi_sglist(cmnd);
83
84 if (!_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040085 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
86 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -050087 __func__);
88 return;
89 }
90
91
92 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -040093 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
94 "9051 BLKGRD: ERROR: data scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -050095 return;
96 }
97
98 dst = (void *) _dump_buf_data;
99 while (sgde) {
100 src = sg_virt(sgde);
101 memcpy(dst, src, sgde->length);
102 dst += sgde->length;
103 sgde = sg_next(sgde);
104 }
105}
106
107static void
James Smart6a9c52c2009-10-02 15:16:51 -0400108lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -0500109{
110 void *src, *dst;
111 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
112
113 if (!_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -0400114 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
115 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -0500116 __func__);
117 return;
118 }
119
120 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -0400121 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
122 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -0500123 return;
124 }
125
126 dst = _dump_buf_dif;
127 while (sgde) {
128 src = sg_virt(sgde);
129 memcpy(dst, src, sgde->length);
130 dst += sgde->length;
131 sgde = sg_next(sgde);
132 }
133}
134
James Smart9c6aa9d2013-05-31 17:03:39 -0400135static inline unsigned
136lpfc_cmd_blksize(struct scsi_cmnd *sc)
137{
138 return sc->device->sector_size;
139}
140
141#define LPFC_CHECK_PROTECT_GUARD 1
142#define LPFC_CHECK_PROTECT_REF 2
143static inline unsigned
144lpfc_cmd_protect(struct scsi_cmnd *sc, int flag)
145{
146 return 1;
147}
148
149static inline unsigned
150lpfc_cmd_guard_csum(struct scsi_cmnd *sc)
151{
152 if (lpfc_prot_group_type(NULL, sc) == LPFC_PG_TYPE_NO_DIF)
153 return 0;
154 if (scsi_host_get_guard(sc->device->host) == SHOST_DIX_GUARD_IP)
155 return 1;
156 return 0;
157}
158
James Smartea2151b2008-09-07 11:52:10 -0400159/**
James Smartf1126682009-06-10 17:22:44 -0400160 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
161 * @phba: Pointer to HBA object.
162 * @lpfc_cmd: lpfc scsi command object pointer.
163 *
164 * This function is called from the lpfc_prep_task_mgmt_cmd function to
165 * set the last bit in the response sge entry.
166 **/
167static void
168lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
169 struct lpfc_scsi_buf *lpfc_cmd)
170{
171 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
172 if (sgl) {
173 sgl += 1;
174 sgl->word2 = le32_to_cpu(sgl->word2);
175 bf_set(lpfc_sli4_sge_last, sgl, 1);
176 sgl->word2 = cpu_to_le32(sgl->word2);
177 }
178}
179
180/**
James Smart3621a712009-04-06 18:47:14 -0400181 * lpfc_update_stats - Update statistical data for the command completion
James Smartea2151b2008-09-07 11:52:10 -0400182 * @phba: Pointer to HBA object.
183 * @lpfc_cmd: lpfc scsi command object pointer.
184 *
185 * This function is called when there is a command completion and this
186 * function updates the statistical data for the command completion.
187 **/
188static void
189lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
190{
191 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
192 struct lpfc_nodelist *pnode = rdata->pnode;
193 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
194 unsigned long flags;
195 struct Scsi_Host *shost = cmd->device->host;
196 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
197 unsigned long latency;
198 int i;
199
200 if (cmd->result)
201 return;
202
James Smart9f1e1b52008-12-04 22:39:40 -0500203 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
204
James Smartea2151b2008-09-07 11:52:10 -0400205 spin_lock_irqsave(shost->host_lock, flags);
206 if (!vport->stat_data_enabled ||
207 vport->stat_data_blocked ||
James Smart5989b8d2010-10-22 11:06:56 -0400208 !pnode ||
James Smartea2151b2008-09-07 11:52:10 -0400209 !pnode->lat_data ||
210 (phba->bucket_type == LPFC_NO_BUCKET)) {
211 spin_unlock_irqrestore(shost->host_lock, flags);
212 return;
213 }
James Smartea2151b2008-09-07 11:52:10 -0400214
215 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
216 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
217 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -0500218 /* check array subscript bounds */
219 if (i < 0)
220 i = 0;
221 else if (i >= LPFC_MAX_BUCKET_COUNT)
222 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -0400223 } else {
224 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
225 if (latency <= (phba->bucket_base +
226 ((1<<i)*phba->bucket_step)))
227 break;
228 }
229
230 pnode->lat_data[i].cmd_count++;
231 spin_unlock_irqrestore(shost->host_lock, flags);
232}
233
James Smartea2151b2008-09-07 11:52:10 -0400234/**
James Smart3621a712009-04-06 18:47:14 -0400235 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
James Smartea2151b2008-09-07 11:52:10 -0400236 * @phba: Pointer to HBA context object.
237 * @vport: Pointer to vport object.
238 * @ndlp: Pointer to FC node associated with the target.
239 * @lun: Lun number of the scsi device.
240 * @old_val: Old value of the queue depth.
241 * @new_val: New value of the queue depth.
242 *
243 * This function sends an event to the mgmt application indicating
244 * there is a change in the scsi device queue depth.
245 **/
246static void
247lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
248 struct lpfc_vport *vport,
249 struct lpfc_nodelist *ndlp,
250 uint32_t lun,
251 uint32_t old_val,
252 uint32_t new_val)
253{
254 struct lpfc_fast_path_event *fast_path_evt;
255 unsigned long flags;
256
257 fast_path_evt = lpfc_alloc_fast_evt(phba);
258 if (!fast_path_evt)
259 return;
260
261 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
262 FC_REG_SCSI_EVENT;
263 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
264 LPFC_EVENT_VARQUEDEPTH;
265
266 /* Report all luns with change in queue depth */
267 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
268 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
269 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
270 &ndlp->nlp_portname, sizeof(struct lpfc_name));
271 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
272 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
273 }
274
275 fast_path_evt->un.queue_depth_evt.oldval = old_val;
276 fast_path_evt->un.queue_depth_evt.newval = new_val;
277 fast_path_evt->vport = vport;
278
279 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
280 spin_lock_irqsave(&phba->hbalock, flags);
281 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
282 spin_unlock_irqrestore(&phba->hbalock, flags);
283 lpfc_worker_wake_up(phba);
284
285 return;
286}
287
James Smart9bad7672008-12-04 22:39:02 -0500288/**
James Smart5ffc2662009-11-18 15:39:44 -0500289 * lpfc_change_queue_depth - Alter scsi device queue depth
290 * @sdev: Pointer the scsi device on which to change the queue depth.
291 * @qdepth: New queue depth to set the sdev to.
292 * @reason: The reason for the queue depth change.
293 *
294 * This function is called by the midlayer and the LLD to alter the queue
295 * depth for a scsi device. This function sets the queue depth to the new
296 * value and sends an event out to log the queue depth change.
297 **/
298int
299lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
300{
301 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
302 struct lpfc_hba *phba = vport->phba;
303 struct lpfc_rport_data *rdata;
304 unsigned long new_queue_depth, old_queue_depth;
305
306 old_queue_depth = sdev->queue_depth;
307 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
308 new_queue_depth = sdev->queue_depth;
309 rdata = sdev->hostdata;
310 if (rdata)
311 lpfc_send_sdev_queuedepth_change_event(phba, vport,
312 rdata->pnode, sdev->lun,
313 old_queue_depth,
314 new_queue_depth);
315 return sdev->queue_depth;
316}
317
318/**
James Smartfe8f7f92013-01-03 15:43:03 -0500319 * lpfc_change_queue_type() - Change a device's scsi tag queuing type
320 * @sdev: Pointer the scsi device whose queue depth is to change
321 * @tag_type: Identifier for queue tag type
322 */
323static int
324lpfc_change_queue_type(struct scsi_device *sdev, int tag_type)
325{
326 if (sdev->tagged_supported) {
327 scsi_set_tag_type(sdev, tag_type);
328 if (tag_type)
329 scsi_activate_tcq(sdev, sdev->queue_depth);
330 else
331 scsi_deactivate_tcq(sdev, sdev->queue_depth);
332 } else
333 tag_type = 0;
334
335 return tag_type;
336}
337
338/**
James Smart3621a712009-04-06 18:47:14 -0400339 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500340 * @phba: The Hba for which this call is being executed.
341 *
342 * This routine is called when there is resource error in driver or firmware.
343 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
344 * posts at most 1 event each second. This routine wakes up worker thread of
345 * @phba to process WORKER_RAM_DOWN_EVENT event.
346 *
347 * This routine should be called with no lock held.
348 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500349void
James Smarteaf15d52008-12-04 22:39:29 -0500350lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500351{
352 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400353 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500354
355 spin_lock_irqsave(&phba->hbalock, flags);
356 atomic_inc(&phba->num_rsrc_err);
357 phba->last_rsrc_error_time = jiffies;
358
359 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
360 spin_unlock_irqrestore(&phba->hbalock, flags);
361 return;
362 }
363
364 phba->last_ramp_down_time = jiffies;
365
366 spin_unlock_irqrestore(&phba->hbalock, flags);
367
368 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400369 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
370 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500371 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500372 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
373
James Smart5e9d9b82008-06-14 22:52:53 -0400374 if (!evt_posted)
375 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500376 return;
377}
378
James Smart9bad7672008-12-04 22:39:02 -0500379/**
James Smart3621a712009-04-06 18:47:14 -0400380 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
James Smart9bad7672008-12-04 22:39:02 -0500381 * @phba: The Hba for which this call is being executed.
382 *
383 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
384 * post at most 1 event every 5 minute after last_ramp_up_time or
385 * last_rsrc_error_time. This routine wakes up worker thread of @phba
386 * to process WORKER_RAM_DOWN_EVENT event.
387 *
388 * This routine should be called with no lock held.
389 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500390static inline void
James Smart3de2a652007-08-02 11:09:59 -0400391lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smarta257bf92009-04-06 18:48:10 -0400392 uint32_t queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500393{
394 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -0400395 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400396 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500397 atomic_inc(&phba->num_cmd_success);
398
James Smarta257bf92009-04-06 18:48:10 -0400399 if (vport->cfg_lun_queue_depth <= queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500400 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500401 spin_lock_irqsave(&phba->hbalock, flags);
James Smart5ffc2662009-11-18 15:39:44 -0500402 if (time_before(jiffies,
403 phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
404 time_before(jiffies,
405 phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500406 spin_unlock_irqrestore(&phba->hbalock, flags);
407 return;
408 }
James Smart92d7f7b2007-06-17 19:56:38 -0500409 phba->last_ramp_up_time = jiffies;
410 spin_unlock_irqrestore(&phba->hbalock, flags);
411
412 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400413 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
414 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500415 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500416 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
417
James Smart5e9d9b82008-06-14 22:52:53 -0400418 if (!evt_posted)
419 lpfc_worker_wake_up(phba);
420 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500421}
422
James Smart9bad7672008-12-04 22:39:02 -0500423/**
James Smart3621a712009-04-06 18:47:14 -0400424 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500425 * @phba: The Hba for which this call is being executed.
426 *
427 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
428 * thread.This routine reduces queue depth for all scsi device on each vport
429 * associated with @phba.
430 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500431void
432lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
433{
James Smart549e55c2007-08-02 11:09:51 -0400434 struct lpfc_vport **vports;
435 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500436 struct scsi_device *sdev;
James Smart5ffc2662009-11-18 15:39:44 -0500437 unsigned long new_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500438 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400439 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500440
441 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
442 num_cmd_success = atomic_read(&phba->num_cmd_success);
443
James Smart75ad83a2012-05-09 21:18:40 -0400444 /*
445 * The error and success command counters are global per
446 * driver instance. If another handler has already
447 * operated on this error event, just exit.
448 */
449 if (num_rsrc_err == 0)
450 return;
451
James Smart549e55c2007-08-02 11:09:51 -0400452 vports = lpfc_create_vport_work_array(phba);
453 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400454 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400455 shost = lpfc_shost_from_vport(vports[i]);
456 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500457 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400458 sdev->queue_depth * num_rsrc_err /
459 (num_rsrc_err + num_cmd_success);
460 if (!new_queue_depth)
461 new_queue_depth = sdev->queue_depth - 1;
462 else
463 new_queue_depth = sdev->queue_depth -
464 new_queue_depth;
James Smart5ffc2662009-11-18 15:39:44 -0500465 lpfc_change_queue_depth(sdev, new_queue_depth,
466 SCSI_QDEPTH_DEFAULT);
James Smart549e55c2007-08-02 11:09:51 -0400467 }
James Smart92d7f7b2007-06-17 19:56:38 -0500468 }
James Smart09372822008-01-11 01:52:54 -0500469 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500470 atomic_set(&phba->num_rsrc_err, 0);
471 atomic_set(&phba->num_cmd_success, 0);
472}
473
James Smart9bad7672008-12-04 22:39:02 -0500474/**
James Smart3621a712009-04-06 18:47:14 -0400475 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500476 * @phba: The Hba for which this call is being executed.
477 *
478 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
479 * thread.This routine increases queue depth for all scsi device on each vport
480 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
481 * num_cmd_success to zero.
482 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500483void
484lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
485{
James Smart549e55c2007-08-02 11:09:51 -0400486 struct lpfc_vport **vports;
487 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500488 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400489 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500490
James Smart549e55c2007-08-02 11:09:51 -0400491 vports = lpfc_create_vport_work_array(phba);
492 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400493 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400494 shost = lpfc_shost_from_vport(vports[i]);
495 shost_for_each_device(sdev, shost) {
James Smart97eab632008-04-07 10:16:05 -0400496 if (vports[i]->cfg_lun_queue_depth <=
497 sdev->queue_depth)
498 continue;
James Smart5ffc2662009-11-18 15:39:44 -0500499 lpfc_change_queue_depth(sdev,
500 sdev->queue_depth+1,
501 SCSI_QDEPTH_RAMP_UP);
James Smart549e55c2007-08-02 11:09:51 -0400502 }
James Smart92d7f7b2007-06-17 19:56:38 -0500503 }
James Smart09372822008-01-11 01:52:54 -0500504 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500505 atomic_set(&phba->num_rsrc_err, 0);
506 atomic_set(&phba->num_cmd_success, 0);
507}
508
James Smarta8e497d2008-08-24 21:50:11 -0400509/**
James Smart3621a712009-04-06 18:47:14 -0400510 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400511 * @phba: Pointer to HBA context object.
512 *
513 * This function walks vport list and set each SCSI host to block state
514 * by invoking fc_remote_port_delete() routine. This function is invoked
515 * with EEH when device's PCI slot has been permanently disabled.
516 **/
517void
518lpfc_scsi_dev_block(struct lpfc_hba *phba)
519{
520 struct lpfc_vport **vports;
521 struct Scsi_Host *shost;
522 struct scsi_device *sdev;
523 struct fc_rport *rport;
524 int i;
525
526 vports = lpfc_create_vport_work_array(phba);
527 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400528 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400529 shost = lpfc_shost_from_vport(vports[i]);
530 shost_for_each_device(sdev, shost) {
531 rport = starget_to_rport(scsi_target(sdev));
532 fc_remote_port_delete(rport);
533 }
534 }
535 lpfc_destroy_vport_work_array(phba, vports);
536}
537
James Smart9bad7672008-12-04 22:39:02 -0500538/**
James Smart3772a992009-05-22 14:50:54 -0400539 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500540 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400541 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500542 *
James Smart3772a992009-05-22 14:50:54 -0400543 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
544 * the scsi buffer contains all the necessary information needed to initiate
545 * a SCSI I/O. The non-DMAable buffer region contains information to build
546 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
547 * and the initial BPL. In addition to allocating memory, the FCP CMND and
548 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500549 *
550 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400551 * int - number of scsi buffers that were allocated.
552 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500553 **/
James Smart3772a992009-05-22 14:50:54 -0400554static int
555lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500556{
James Smart2e0fef82007-06-17 19:56:36 -0500557 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500558 struct lpfc_scsi_buf *psb;
559 struct ulp_bde64 *bpl;
560 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400561 dma_addr_t pdma_phys_fcp_cmd;
562 dma_addr_t pdma_phys_fcp_rsp;
563 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500564 uint16_t iotag;
James Smart96f70772013-04-17 20:16:15 -0400565 int bcnt, bpl_size;
566
567 bpl_size = phba->cfg_sg_dma_buf_size -
568 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
569
570 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
571 "9067 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
572 num_to_alloc, phba->cfg_sg_dma_buf_size,
573 (int)sizeof(struct fcp_cmnd),
574 (int)sizeof(struct fcp_rsp), bpl_size);
dea31012005-04-17 16:05:31 -0500575
James Smart3772a992009-05-22 14:50:54 -0400576 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
577 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
578 if (!psb)
579 break;
dea31012005-04-17 16:05:31 -0500580
James Smart3772a992009-05-22 14:50:54 -0400581 /*
582 * Get memory from the pci pool to map the virt space to pci
583 * bus space for an I/O. The DMA buffer includes space for the
584 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
585 * necessary to support the sg_tablesize.
586 */
587 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
588 GFP_KERNEL, &psb->dma_handle);
589 if (!psb->data) {
590 kfree(psb);
591 break;
592 }
dea31012005-04-17 16:05:31 -0500593
James Smart3772a992009-05-22 14:50:54 -0400594 /* Initialize virtual ptrs to dma_buf region. */
595 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
dea31012005-04-17 16:05:31 -0500596
James Smart3772a992009-05-22 14:50:54 -0400597 /* Allocate iotag for psb->cur_iocbq. */
598 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
599 if (iotag == 0) {
600 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
601 psb->data, psb->dma_handle);
602 kfree(psb);
603 break;
604 }
605 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500606
James Smart3772a992009-05-22 14:50:54 -0400607 psb->fcp_cmnd = psb->data;
608 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
609 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400610 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500611
James Smart3772a992009-05-22 14:50:54 -0400612 /* Initialize local short-hand pointers. */
613 bpl = psb->fcp_bpl;
614 pdma_phys_fcp_cmd = psb->dma_handle;
615 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
616 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
617 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500618
James Smart3772a992009-05-22 14:50:54 -0400619 /*
620 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
621 * are sg list bdes. Initialize the first two and leave the
622 * rest for queuecommand.
623 */
624 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
625 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
626 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
627 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
628 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500629
James Smart3772a992009-05-22 14:50:54 -0400630 /* Setup the physical region for the FCP RSP */
631 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
632 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
633 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
634 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
635 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
636
637 /*
638 * Since the IOCB for the FCP I/O is built into this
639 * lpfc_scsi_buf, initialize it with all known data now.
640 */
641 iocb = &psb->cur_iocbq.iocb;
642 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
643 if ((phba->sli_rev == 3) &&
644 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
645 /* fill in immediate fcp command BDE */
646 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
647 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
648 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
649 unsli3.fcp_ext.icd);
650 iocb->un.fcpi64.bdl.addrHigh = 0;
651 iocb->ulpBdeCount = 0;
652 iocb->ulpLe = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300653 /* fill in response BDE */
James Smart3772a992009-05-22 14:50:54 -0400654 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
655 BUFF_TYPE_BDE_64;
656 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
657 sizeof(struct fcp_rsp);
658 iocb->unsli3.fcp_ext.rbde.addrLow =
659 putPaddrLow(pdma_phys_fcp_rsp);
660 iocb->unsli3.fcp_ext.rbde.addrHigh =
661 putPaddrHigh(pdma_phys_fcp_rsp);
662 } else {
663 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
664 iocb->un.fcpi64.bdl.bdeSize =
665 (2 * sizeof(struct ulp_bde64));
666 iocb->un.fcpi64.bdl.addrLow =
667 putPaddrLow(pdma_phys_bpl);
668 iocb->un.fcpi64.bdl.addrHigh =
669 putPaddrHigh(pdma_phys_bpl);
670 iocb->ulpBdeCount = 1;
671 iocb->ulpLe = 1;
672 }
673 iocb->ulpClass = CLASS3;
674 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400675 /* Put it back into the SCSI buffer list */
James Smarteee88772010-09-29 11:19:08 -0400676 psb->cur_iocbq.context1 = psb;
James Smart1c6f4ef52009-11-18 15:40:49 -0500677 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400678
James Smart34b02dc2008-08-24 21:49:55 -0400679 }
dea31012005-04-17 16:05:31 -0500680
James Smart3772a992009-05-22 14:50:54 -0400681 return bcnt;
dea31012005-04-17 16:05:31 -0500682}
683
James Smart9bad7672008-12-04 22:39:02 -0500684/**
James Smart1151e3e2011-02-16 12:39:35 -0500685 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
686 * @vport: pointer to lpfc vport data structure.
687 *
688 * This routine is invoked by the vport cleanup for deletions and the cleanup
689 * for an ndlp on removal.
690 **/
691void
692lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
693{
694 struct lpfc_hba *phba = vport->phba;
695 struct lpfc_scsi_buf *psb, *next_psb;
696 unsigned long iflag = 0;
697
698 spin_lock_irqsave(&phba->hbalock, iflag);
699 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
700 list_for_each_entry_safe(psb, next_psb,
701 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
702 if (psb->rdata && psb->rdata->pnode
703 && psb->rdata->pnode->vport == vport)
704 psb->rdata = NULL;
705 }
706 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
707 spin_unlock_irqrestore(&phba->hbalock, iflag);
708}
709
710/**
James Smartda0436e2009-05-22 14:51:39 -0400711 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
712 * @phba: pointer to lpfc hba data structure.
713 * @axri: pointer to the fcp xri abort wcqe structure.
714 *
715 * This routine is invoked by the worker thread to process a SLI4 fast-path
716 * FCP aborted xri.
717 **/
718void
719lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
720 struct sli4_wcqe_xri_aborted *axri)
721{
722 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500723 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartda0436e2009-05-22 14:51:39 -0400724 struct lpfc_scsi_buf *psb, *next_psb;
725 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500726 struct lpfc_iocbq *iocbq;
727 int i;
James Smart19ca7602010-11-20 23:11:55 -0500728 struct lpfc_nodelist *ndlp;
729 int rrq_empty = 0;
James Smart589a52d2010-07-14 15:30:54 -0400730 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smartda0436e2009-05-22 14:51:39 -0400731
James Smart0f65ff62010-02-26 14:14:23 -0500732 spin_lock_irqsave(&phba->hbalock, iflag);
733 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400734 list_for_each_entry_safe(psb, next_psb,
735 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
736 if (psb->cur_iocbq.sli4_xritag == xri) {
737 list_del(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500738 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400739 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500740 spin_unlock(
741 &phba->sli4_hba.abts_scsi_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500742 if (psb->rdata && psb->rdata->pnode)
743 ndlp = psb->rdata->pnode;
744 else
745 ndlp = NULL;
746
James Smart19ca7602010-11-20 23:11:55 -0500747 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500748 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartcb69f7d2011-12-13 13:21:57 -0500749 if (ndlp) {
James Smartee0f4fe2012-05-09 21:19:14 -0400750 lpfc_set_rrq_active(phba, ndlp,
751 psb->cur_iocbq.sli4_lxritag, rxid, 1);
James Smartcb69f7d2011-12-13 13:21:57 -0500752 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
753 }
James Smartda0436e2009-05-22 14:51:39 -0400754 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500755 if (rrq_empty)
756 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400757 return;
758 }
759 }
James Smart0f65ff62010-02-26 14:14:23 -0500760 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
761 for (i = 1; i <= phba->sli.last_iotag; i++) {
762 iocbq = phba->sli.iocbq_lookup[i];
763
764 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
765 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
766 continue;
767 if (iocbq->sli4_xritag != xri)
768 continue;
769 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
770 psb->exch_busy = 0;
771 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart0e9bb8d2013-03-01 16:35:12 -0500772 if (!list_empty(&pring->txq))
James Smart589a52d2010-07-14 15:30:54 -0400773 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500774 return;
775
776 }
777 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400778}
779
780/**
James Smart8a9d2e82012-05-09 21:16:12 -0400781 * lpfc_sli4_post_scsi_sgl_list - Psot blocks of scsi buffer sgls from a list
782 * @phba: pointer to lpfc hba data structure.
783 * @post_sblist: pointer to the scsi buffer list.
784 *
785 * This routine walks a list of scsi buffers that was passed in. It attempts
786 * to construct blocks of scsi buffer sgls which contains contiguous xris and
787 * uses the non-embedded SGL block post mailbox commands to post to the port.
788 * For single SCSI buffer sgl with non-contiguous xri, if any, it shall use
789 * embedded SGL post mailbox command for posting. The @post_sblist passed in
790 * must be local list, thus no lock is needed when manipulate the list.
791 *
792 * Returns: 0 = failure, non-zero number of successfully posted buffers.
793 **/
794int
795lpfc_sli4_post_scsi_sgl_list(struct lpfc_hba *phba,
796 struct list_head *post_sblist, int sb_count)
797{
798 struct lpfc_scsi_buf *psb, *psb_next;
James Smart96f70772013-04-17 20:16:15 -0400799 int status, sgl_size;
James Smart8a9d2e82012-05-09 21:16:12 -0400800 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
801 dma_addr_t pdma_phys_bpl1;
802 int last_xritag = NO_XRI;
803 LIST_HEAD(prep_sblist);
804 LIST_HEAD(blck_sblist);
805 LIST_HEAD(scsi_sblist);
806
807 /* sanity check */
808 if (sb_count <= 0)
809 return -EINVAL;
810
James Smart96f70772013-04-17 20:16:15 -0400811 sgl_size = phba->cfg_sg_dma_buf_size -
812 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
813
James Smart8a9d2e82012-05-09 21:16:12 -0400814 list_for_each_entry_safe(psb, psb_next, post_sblist, list) {
815 list_del_init(&psb->list);
816 block_cnt++;
817 if ((last_xritag != NO_XRI) &&
818 (psb->cur_iocbq.sli4_xritag != last_xritag + 1)) {
819 /* a hole in xri block, form a sgl posting block */
820 list_splice_init(&prep_sblist, &blck_sblist);
821 post_cnt = block_cnt - 1;
822 /* prepare list for next posting block */
823 list_add_tail(&psb->list, &prep_sblist);
824 block_cnt = 1;
825 } else {
826 /* prepare list for next posting block */
827 list_add_tail(&psb->list, &prep_sblist);
828 /* enough sgls for non-embed sgl mbox command */
829 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
830 list_splice_init(&prep_sblist, &blck_sblist);
831 post_cnt = block_cnt;
832 block_cnt = 0;
833 }
834 }
835 num_posting++;
836 last_xritag = psb->cur_iocbq.sli4_xritag;
837
838 /* end of repost sgl list condition for SCSI buffers */
839 if (num_posting == sb_count) {
840 if (post_cnt == 0) {
841 /* last sgl posting block */
842 list_splice_init(&prep_sblist, &blck_sblist);
843 post_cnt = block_cnt;
844 } else if (block_cnt == 1) {
845 /* last single sgl with non-contiguous xri */
James Smart96f70772013-04-17 20:16:15 -0400846 if (sgl_size > SGL_PAGE_SIZE)
James Smart8a9d2e82012-05-09 21:16:12 -0400847 pdma_phys_bpl1 = psb->dma_phys_bpl +
848 SGL_PAGE_SIZE;
849 else
850 pdma_phys_bpl1 = 0;
851 status = lpfc_sli4_post_sgl(phba,
852 psb->dma_phys_bpl,
853 pdma_phys_bpl1,
854 psb->cur_iocbq.sli4_xritag);
855 if (status) {
856 /* failure, put on abort scsi list */
857 psb->exch_busy = 1;
858 } else {
859 /* success, put on SCSI buffer list */
860 psb->exch_busy = 0;
861 psb->status = IOSTAT_SUCCESS;
862 num_posted++;
863 }
864 /* success, put on SCSI buffer sgl list */
865 list_add_tail(&psb->list, &scsi_sblist);
866 }
867 }
868
869 /* continue until a nembed page worth of sgls */
870 if (post_cnt == 0)
871 continue;
872
873 /* post block of SCSI buffer list sgls */
874 status = lpfc_sli4_post_scsi_sgl_block(phba, &blck_sblist,
875 post_cnt);
876
877 /* don't reset xirtag due to hole in xri block */
878 if (block_cnt == 0)
879 last_xritag = NO_XRI;
880
881 /* reset SCSI buffer post count for next round of posting */
882 post_cnt = 0;
883
884 /* put posted SCSI buffer-sgl posted on SCSI buffer sgl list */
885 while (!list_empty(&blck_sblist)) {
886 list_remove_head(&blck_sblist, psb,
887 struct lpfc_scsi_buf, list);
888 if (status) {
889 /* failure, put on abort scsi list */
890 psb->exch_busy = 1;
891 } else {
892 /* success, put on SCSI buffer list */
893 psb->exch_busy = 0;
894 psb->status = IOSTAT_SUCCESS;
895 num_posted++;
896 }
897 list_add_tail(&psb->list, &scsi_sblist);
898 }
899 }
900 /* Push SCSI buffers with sgl posted to the availble list */
901 while (!list_empty(&scsi_sblist)) {
902 list_remove_head(&scsi_sblist, psb,
903 struct lpfc_scsi_buf, list);
904 lpfc_release_scsi_buf_s4(phba, psb);
905 }
906 return num_posted;
907}
908
909/**
910 * lpfc_sli4_repost_scsi_sgl_list - Repsot all the allocated scsi buffer sgls
James Smartda0436e2009-05-22 14:51:39 -0400911 * @phba: pointer to lpfc hba data structure.
912 *
913 * This routine walks the list of scsi buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -0400914 * repost them to the port by using SGL block post. This is needed after a
James Smartda0436e2009-05-22 14:51:39 -0400915 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
916 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
917 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
918 *
919 * Returns: 0 = success, non-zero failure.
920 **/
921int
922lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
923{
James Smart8a9d2e82012-05-09 21:16:12 -0400924 LIST_HEAD(post_sblist);
925 int num_posted, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -0400926
James Smart8a9d2e82012-05-09 21:16:12 -0400927 /* get all SCSI buffers need to repost to a local list */
James Smarta40fc5f2013-04-17 20:17:40 -0400928 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -0400929 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400930 list_splice_init(&phba->lpfc_scsi_buf_list_get, &post_sblist);
931 list_splice(&phba->lpfc_scsi_buf_list_put, &post_sblist);
James Smart164cecd2013-09-06 12:22:38 -0400932 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400933 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smartda0436e2009-05-22 14:51:39 -0400934
James Smart8a9d2e82012-05-09 21:16:12 -0400935 /* post the list of scsi buffer sgls to port if available */
936 if (!list_empty(&post_sblist)) {
937 num_posted = lpfc_sli4_post_scsi_sgl_list(phba, &post_sblist,
938 phba->sli4_hba.scsi_xri_cnt);
939 /* failed to post any scsi buffer, return error */
940 if (num_posted == 0)
941 rc = -EIO;
James Smartda0436e2009-05-22 14:51:39 -0400942 }
943 return rc;
944}
945
946/**
947 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
948 * @vport: The virtual port for which this call being executed.
949 * @num_to_allocate: The requested number of buffers to allocate.
950 *
James Smart8a9d2e82012-05-09 21:16:12 -0400951 * This routine allocates scsi buffers for device with SLI-4 interface spec,
James Smartda0436e2009-05-22 14:51:39 -0400952 * the scsi buffer contains all the necessary information needed to initiate
James Smart8a9d2e82012-05-09 21:16:12 -0400953 * a SCSI I/O. After allocating up to @num_to_allocate SCSI buffers and put
954 * them on a list, it post them to the port by using SGL block post.
James Smartda0436e2009-05-22 14:51:39 -0400955 *
956 * Return codes:
James Smart8a9d2e82012-05-09 21:16:12 -0400957 * int - number of scsi buffers that were allocated and posted.
James Smartda0436e2009-05-22 14:51:39 -0400958 * 0 = failure, less than num_to_alloc is a partial failure.
959 **/
960static int
961lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
962{
963 struct lpfc_hba *phba = vport->phba;
964 struct lpfc_scsi_buf *psb;
965 struct sli4_sge *sgl;
966 IOCB_t *iocb;
967 dma_addr_t pdma_phys_fcp_cmd;
968 dma_addr_t pdma_phys_fcp_rsp;
James Smart96f70772013-04-17 20:16:15 -0400969 dma_addr_t pdma_phys_bpl;
James Smart8a9d2e82012-05-09 21:16:12 -0400970 uint16_t iotag, lxri = 0;
James Smart96f70772013-04-17 20:16:15 -0400971 int bcnt, num_posted, sgl_size;
James Smart8a9d2e82012-05-09 21:16:12 -0400972 LIST_HEAD(prep_sblist);
973 LIST_HEAD(post_sblist);
974 LIST_HEAD(scsi_sblist);
James Smartda0436e2009-05-22 14:51:39 -0400975
James Smart96f70772013-04-17 20:16:15 -0400976 sgl_size = phba->cfg_sg_dma_buf_size -
977 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
978
979 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
980 "9068 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
981 num_to_alloc, phba->cfg_sg_dma_buf_size, sgl_size,
982 (int)sizeof(struct fcp_cmnd),
983 (int)sizeof(struct fcp_rsp));
984
James Smartda0436e2009-05-22 14:51:39 -0400985 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
986 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
987 if (!psb)
988 break;
James Smartda0436e2009-05-22 14:51:39 -0400989 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400990 * Get memory from the pci pool to map the virt space to
991 * pci bus space for an I/O. The DMA buffer includes space
992 * for the struct fcp_cmnd, struct fcp_rsp and the number
993 * of bde's necessary to support the sg_tablesize.
James Smartda0436e2009-05-22 14:51:39 -0400994 */
995 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
996 GFP_KERNEL, &psb->dma_handle);
997 if (!psb->data) {
998 kfree(psb);
999 break;
1000 }
James Smartda0436e2009-05-22 14:51:39 -04001001 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
1002
James Smart092cb032013-09-06 12:21:50 -04001003 /*
1004 * 4K Page alignment is CRITICAL to BlockGuard, double check
1005 * to be sure.
1006 */
1007 if (phba->cfg_enable_bg && (((unsigned long)(psb->data) &
1008 (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0)) {
James Smart96f70772013-04-17 20:16:15 -04001009 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
1010 psb->data, psb->dma_handle);
1011 kfree(psb);
1012 break;
1013 }
1014
James Smartda0436e2009-05-22 14:51:39 -04001015
James Smart6d368e52011-05-24 11:44:12 -04001016 lxri = lpfc_sli4_next_xritag(phba);
1017 if (lxri == NO_XRI) {
James Smartda0436e2009-05-22 14:51:39 -04001018 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
1019 psb->data, psb->dma_handle);
1020 kfree(psb);
1021 break;
1022 }
James Smartf7bc6432013-10-10 12:19:53 -04001023
1024 /* Allocate iotag for psb->cur_iocbq. */
1025 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
1026 if (iotag == 0) {
1027 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
1028 psb->data, psb->dma_handle);
1029 kfree(psb);
1030 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1031 "3368 Failed to allocated IOTAG for"
1032 " XRI:0x%x\n", lxri);
1033 lpfc_sli4_free_xri(phba, lxri);
1034 break;
1035 }
James Smart6d368e52011-05-24 11:44:12 -04001036 psb->cur_iocbq.sli4_lxritag = lxri;
1037 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smartda0436e2009-05-22 14:51:39 -04001038 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Smartda0436e2009-05-22 14:51:39 -04001039 psb->fcp_bpl = psb->data;
James Smart96f70772013-04-17 20:16:15 -04001040 psb->fcp_cmnd = (psb->data + sgl_size);
James Smartda0436e2009-05-22 14:51:39 -04001041 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
1042 sizeof(struct fcp_cmnd));
1043
1044 /* Initialize local short-hand pointers. */
1045 sgl = (struct sli4_sge *)psb->fcp_bpl;
1046 pdma_phys_bpl = psb->dma_handle;
James Smart96f70772013-04-17 20:16:15 -04001047 pdma_phys_fcp_cmd = (psb->dma_handle + sgl_size);
James Smartda0436e2009-05-22 14:51:39 -04001048 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
1049
1050 /*
James Smart8a9d2e82012-05-09 21:16:12 -04001051 * The first two bdes are the FCP_CMD and FCP_RSP.
1052 * The balance are sg list bdes. Initialize the
1053 * first two and leave the rest for queuecommand.
James Smartda0436e2009-05-22 14:51:39 -04001054 */
1055 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
1056 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
James Smart05580562011-05-24 11:40:48 -04001057 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04001058 bf_set(lpfc_sli4_sge_last, sgl, 0);
1059 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05001060 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
James Smartda0436e2009-05-22 14:51:39 -04001061 sgl++;
1062
1063 /* Setup the physical region for the FCP RSP */
1064 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
1065 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smart05580562011-05-24 11:40:48 -04001066 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04001067 bf_set(lpfc_sli4_sge_last, sgl, 1);
1068 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05001069 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -04001070
1071 /*
1072 * Since the IOCB for the FCP I/O is built into this
1073 * lpfc_scsi_buf, initialize it with all known data now.
1074 */
1075 iocb = &psb->cur_iocbq.iocb;
1076 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
1077 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
1078 /* setting the BLP size to 2 * sizeof BDE may not be correct.
1079 * We are setting the bpl to point to out sgl. An sgl's
1080 * entries are 16 bytes, a bpl entries are 12 bytes.
1081 */
1082 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
1083 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
1084 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
1085 iocb->ulpBdeCount = 1;
1086 iocb->ulpLe = 1;
1087 iocb->ulpClass = CLASS3;
James Smart8a9d2e82012-05-09 21:16:12 -04001088 psb->cur_iocbq.context1 = psb;
James Smartda0436e2009-05-22 14:51:39 -04001089 psb->dma_phys_bpl = pdma_phys_bpl;
James Smart6d368e52011-05-24 11:44:12 -04001090
James Smart8a9d2e82012-05-09 21:16:12 -04001091 /* add the scsi buffer to a post list */
1092 list_add_tail(&psb->list, &post_sblist);
James Smarta40fc5f2013-04-17 20:17:40 -04001093 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04001094 phba->sli4_hba.scsi_xri_cnt++;
James Smarta40fc5f2013-04-17 20:17:40 -04001095 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smartda0436e2009-05-22 14:51:39 -04001096 }
James Smart8a9d2e82012-05-09 21:16:12 -04001097 lpfc_printf_log(phba, KERN_INFO, LOG_BG,
1098 "3021 Allocate %d out of %d requested new SCSI "
1099 "buffers\n", bcnt, num_to_alloc);
James Smartda0436e2009-05-22 14:51:39 -04001100
James Smart8a9d2e82012-05-09 21:16:12 -04001101 /* post the list of scsi buffer sgls to port if available */
1102 if (!list_empty(&post_sblist))
1103 num_posted = lpfc_sli4_post_scsi_sgl_list(phba,
1104 &post_sblist, bcnt);
1105 else
1106 num_posted = 0;
1107
1108 return num_posted;
James Smartda0436e2009-05-22 14:51:39 -04001109}
1110
1111/**
James Smart3772a992009-05-22 14:50:54 -04001112 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
1113 * @vport: The virtual port for which this call being executed.
1114 * @num_to_allocate: The requested number of buffers to allocate.
1115 *
1116 * This routine wraps the actual SCSI buffer allocator function pointer from
1117 * the lpfc_hba struct.
1118 *
1119 * Return codes:
1120 * int - number of scsi buffers that were allocated.
1121 * 0 = failure, less than num_to_alloc is a partial failure.
1122 **/
1123static inline int
1124lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
1125{
1126 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
1127}
1128
1129/**
James Smart19ca7602010-11-20 23:11:55 -05001130 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -04001131 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05001132 *
1133 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1134 * and returns to caller.
1135 *
1136 * Return codes:
1137 * NULL - Error
1138 * Pointer to lpfc_scsi_buf - Success
1139 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +01001140static struct lpfc_scsi_buf*
James Smart19ca7602010-11-20 23:11:55 -05001141lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001142{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001143 struct lpfc_scsi_buf * lpfc_cmd = NULL;
James Smarta40fc5f2013-04-17 20:17:40 -04001144 struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
James Smart164cecd2013-09-06 12:22:38 -04001145 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001146
James Smart164cecd2013-09-06 12:22:38 -04001147 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
James Smarta40fc5f2013-04-17 20:17:40 -04001148 list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_scsi_buf,
1149 list);
1150 if (!lpfc_cmd) {
James Smart164cecd2013-09-06 12:22:38 -04001151 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04001152 list_splice(&phba->lpfc_scsi_buf_list_put,
1153 &phba->lpfc_scsi_buf_list_get);
1154 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
1155 list_remove_head(scsi_buf_list_get, lpfc_cmd,
1156 struct lpfc_scsi_buf, list);
James Smart164cecd2013-09-06 12:22:38 -04001157 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smart1dcb58e2007-04-25 09:51:30 -04001158 }
James Smart164cecd2013-09-06 12:22:38 -04001159 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001160 return lpfc_cmd;
1161}
James Smart19ca7602010-11-20 23:11:55 -05001162/**
1163 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1164 * @phba: The HBA for which this call is being executed.
1165 *
1166 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1167 * and returns to caller.
1168 *
1169 * Return codes:
1170 * NULL - Error
1171 * Pointer to lpfc_scsi_buf - Success
1172 **/
1173static struct lpfc_scsi_buf*
1174lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1175{
James Smart3be30e02013-05-31 17:05:17 -04001176 struct lpfc_scsi_buf *lpfc_cmd, *lpfc_cmd_next;
James Smart164cecd2013-09-06 12:22:38 -04001177 unsigned long iflag = 0;
James Smart19ca7602010-11-20 23:11:55 -05001178 int found = 0;
1179
James Smart164cecd2013-09-06 12:22:38 -04001180 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
James Smart3be30e02013-05-31 17:05:17 -04001181 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1182 &phba->lpfc_scsi_buf_list_get, list) {
James Smart19ca7602010-11-20 23:11:55 -05001183 if (lpfc_test_rrq_active(phba, ndlp,
James Smartee0f4fe2012-05-09 21:19:14 -04001184 lpfc_cmd->cur_iocbq.sli4_lxritag))
James Smart1151e3e2011-02-16 12:39:35 -05001185 continue;
1186 list_del(&lpfc_cmd->list);
James Smart19ca7602010-11-20 23:11:55 -05001187 found = 1;
James Smart1151e3e2011-02-16 12:39:35 -05001188 break;
James Smart19ca7602010-11-20 23:11:55 -05001189 }
James Smarta40fc5f2013-04-17 20:17:40 -04001190 if (!found) {
James Smart164cecd2013-09-06 12:22:38 -04001191 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04001192 list_splice(&phba->lpfc_scsi_buf_list_put,
1193 &phba->lpfc_scsi_buf_list_get);
1194 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart164cecd2013-09-06 12:22:38 -04001195 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smart3be30e02013-05-31 17:05:17 -04001196 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1197 &phba->lpfc_scsi_buf_list_get, list) {
James Smarta40fc5f2013-04-17 20:17:40 -04001198 if (lpfc_test_rrq_active(
1199 phba, ndlp, lpfc_cmd->cur_iocbq.sli4_lxritag))
1200 continue;
1201 list_del(&lpfc_cmd->list);
1202 found = 1;
1203 break;
1204 }
1205 }
James Smart164cecd2013-09-06 12:22:38 -04001206 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -05001207 if (!found)
1208 return NULL;
James Smarta40fc5f2013-04-17 20:17:40 -04001209 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001210}
1211/**
1212 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1213 * @phba: The HBA for which this call is being executed.
1214 *
1215 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1216 * and returns to caller.
1217 *
1218 * Return codes:
1219 * NULL - Error
1220 * Pointer to lpfc_scsi_buf - Success
1221 **/
1222static struct lpfc_scsi_buf*
1223lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1224{
1225 return phba->lpfc_get_scsi_buf(phba, ndlp);
1226}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001227
James Smart9bad7672008-12-04 22:39:02 -05001228/**
James Smart3772a992009-05-22 14:50:54 -04001229 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -05001230 * @phba: The Hba for which this call is being executed.
1231 * @psb: The scsi buffer which is being released.
1232 *
1233 * This routine releases @psb scsi buffer by adding it to tail of @phba
1234 * lpfc_scsi_buf_list list.
1235 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001236static void
James Smart3772a992009-05-22 14:50:54 -04001237lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001238{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001239 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001240
James Smarta40fc5f2013-04-17 20:17:40 -04001241 psb->seg_cnt = 0;
1242 psb->nonsg_phys = 0;
1243 psb->prot_seg_cnt = 0;
1244
1245 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001246 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -04001247 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -04001248 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1249 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
dea31012005-04-17 16:05:31 -05001250}
1251
James Smart9bad7672008-12-04 22:39:02 -05001252/**
James Smartda0436e2009-05-22 14:51:39 -04001253 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1254 * @phba: The Hba for which this call is being executed.
1255 * @psb: The scsi buffer which is being released.
1256 *
1257 * This routine releases @psb scsi buffer by adding it to tail of @phba
1258 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1259 * and cannot be reused for at least RA_TOV amount of time if it was
1260 * aborted.
1261 **/
1262static void
1263lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1264{
1265 unsigned long iflag = 0;
1266
James Smarta40fc5f2013-04-17 20:17:40 -04001267 psb->seg_cnt = 0;
1268 psb->nonsg_phys = 0;
1269 psb->prot_seg_cnt = 0;
1270
James Smart341af102010-01-26 23:07:37 -05001271 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -04001272 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1273 iflag);
1274 psb->pCmd = NULL;
1275 list_add_tail(&psb->list,
1276 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1277 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1278 iflag);
1279 } else {
James Smartda0436e2009-05-22 14:51:39 -04001280 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -04001281 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -04001282 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1283 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1284 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -04001285 }
1286}
1287
1288/**
James Smart3772a992009-05-22 14:50:54 -04001289 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1290 * @phba: The Hba for which this call is being executed.
1291 * @psb: The scsi buffer which is being released.
1292 *
1293 * This routine releases @psb scsi buffer by adding it to tail of @phba
1294 * lpfc_scsi_buf_list list.
1295 **/
1296static void
1297lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1298{
1299
1300 phba->lpfc_release_scsi_buf(phba, psb);
1301}
1302
1303/**
1304 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001305 * @phba: The Hba for which this call is being executed.
1306 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1307 *
1308 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001309 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1310 * through sg elements and format the bdea. This routine also initializes all
1311 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001312 *
1313 * Return codes:
1314 * 1 - Error
1315 * 0 - Success
1316 **/
dea31012005-04-17 16:05:31 -05001317static int
James Smart3772a992009-05-22 14:50:54 -04001318lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001319{
1320 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1321 struct scatterlist *sgel = NULL;
1322 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1323 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001324 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001325 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001326 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001327 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001328 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001329 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001330
1331 /*
1332 * There are three possibilities here - use scatter-gather segment, use
1333 * the single mapping, or neither. Start the lpfc command prep by
1334 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1335 * data bde entry.
1336 */
1337 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001338 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001339 /*
1340 * The driver stores the segment count returned from pci_map_sg
1341 * because this a count of dma-mappings used to map the use_sg
1342 * pages. They are not guaranteed to be the same for those
1343 * architectures that implement an IOMMU.
1344 */
dea31012005-04-17 16:05:31 -05001345
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001346 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1347 scsi_sg_count(scsi_cmnd), datadir);
1348 if (unlikely(!nseg))
1349 return 1;
1350
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001351 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001352 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001353 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1354 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001355 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001356 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001357 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04001358 lpfc_cmd->seg_cnt = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001359 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001360 return 1;
1361 }
1362
1363 /*
1364 * The driver established a maximum scatter-gather segment count
1365 * during probe that limits the number of sg elements in any
1366 * single scsi command. Just run through the seg_cnt and format
1367 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001368 * When using SLI-3 the driver will try to fit all the BDEs into
1369 * the IOCB. If it can't then the BDEs get added to a BPL as it
1370 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001371 */
James Smart34b02dc2008-08-24 21:49:55 -04001372 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001373 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001374 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001375 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001376 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001377 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1378 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1379 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1380 data_bde->addrLow = putPaddrLow(physaddr);
1381 data_bde->addrHigh = putPaddrHigh(physaddr);
1382 data_bde++;
1383 } else {
1384 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1385 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1386 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1387 bpl->addrLow =
1388 le32_to_cpu(putPaddrLow(physaddr));
1389 bpl->addrHigh =
1390 le32_to_cpu(putPaddrHigh(physaddr));
1391 bpl++;
1392 }
dea31012005-04-17 16:05:31 -05001393 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001394 }
dea31012005-04-17 16:05:31 -05001395
1396 /*
1397 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001398 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1399 * explicitly reinitialized and for SLI-3 the extended bde count is
1400 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001401 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001402 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001403 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1404 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001405 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1406 /*
1407 * The extended IOCB format can only fit 3 BDE or a BPL.
1408 * This I/O has more than 3 BDE so the 1st data bde will
1409 * be a BPL that is filled in here.
1410 */
1411 physaddr = lpfc_cmd->dma_handle;
1412 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1413 data_bde->tus.f.bdeSize = (num_bde *
1414 sizeof(struct ulp_bde64));
1415 physaddr += (sizeof(struct fcp_cmnd) +
1416 sizeof(struct fcp_rsp) +
1417 (2 * sizeof(struct ulp_bde64)));
1418 data_bde->addrHigh = putPaddrHigh(physaddr);
1419 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001420 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -04001421 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1422 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001423 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -04001424 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1425 }
1426 } else {
1427 iocb_cmd->un.fcpi64.bdl.bdeSize =
1428 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001429 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001430 }
James Smart09372822008-01-11 01:52:54 -05001431 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001432
1433 /*
1434 * Due to difference in data length between DIF/non-DIF paths,
1435 * we need to set word 4 of IOCB here
1436 */
James Smarta257bf92009-04-06 18:48:10 -04001437 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001438 return 0;
1439}
1440
James Smartf9bb2da2011-10-10 21:34:11 -04001441#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001442
James Smart9a6b09c2012-03-01 22:37:42 -05001443/* Return if if error injection is detected by Initiator */
1444#define BG_ERR_INIT 0x1
1445/* Return if if error injection is detected by Target */
1446#define BG_ERR_TGT 0x2
1447/* Return if if swapping CSUM<-->CRC is required for error injection */
1448#define BG_ERR_SWAP 0x10
1449/* Return if disabling Guard/Ref/App checking is required for error injection */
1450#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -05001451
1452/**
1453 * lpfc_bg_err_inject - Determine if we should inject an error
1454 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -04001455 * @sc: The SCSI command to examine
1456 * @reftag: (out) BlockGuard reference tag for transmitted data
1457 * @apptag: (out) BlockGuard application tag for transmitted data
1458 * @new_guard (in) Value to replace CRC with if needed
1459 *
James Smart9a6b09c2012-03-01 22:37:42 -05001460 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -05001461 **/
James Smartf9bb2da2011-10-10 21:34:11 -04001462static int
1463lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1464 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1465{
1466 struct scatterlist *sgpe; /* s/g prot entry */
1467 struct scatterlist *sgde; /* s/g data entry */
James Smart9a6b09c2012-03-01 22:37:42 -05001468 struct lpfc_scsi_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -05001469 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -05001470 struct lpfc_nodelist *ndlp;
1471 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -04001472 uint32_t op = scsi_get_prot_op(sc);
1473 uint32_t blksize;
1474 uint32_t numblks;
1475 sector_t lba;
1476 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001477 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001478
1479 if (op == SCSI_PROT_NORMAL)
1480 return 0;
1481
James Smartacd68592012-01-18 16:25:09 -05001482 sgpe = scsi_prot_sglist(sc);
1483 sgde = scsi_sglist(sc);
James Smartf9bb2da2011-10-10 21:34:11 -04001484 lba = scsi_get_lba(sc);
James Smart4ac9b222012-03-01 22:38:29 -05001485
1486 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001487 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1488 blksize = lpfc_cmd_blksize(sc);
1489 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1490
1491 /* Make sure we have the right LBA if one is specified */
1492 if ((phba->lpfc_injerr_lba < lba) ||
1493 (phba->lpfc_injerr_lba >= (lba + numblks)))
1494 return 0;
James Smartacd68592012-01-18 16:25:09 -05001495 if (sgpe) {
1496 blockoff = phba->lpfc_injerr_lba - lba;
1497 numblks = sg_dma_len(sgpe) /
1498 sizeof(struct scsi_dif_tuple);
1499 if (numblks < blockoff)
1500 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001501 }
James Smartf9bb2da2011-10-10 21:34:11 -04001502 }
1503
James Smart4ac9b222012-03-01 22:38:29 -05001504 /* Next check if we need to match the remote NPortID or WWPN */
1505 rdata = sc->device->hostdata;
1506 if (rdata && rdata->pnode) {
1507 ndlp = rdata->pnode;
1508
1509 /* Make sure we have the right NPortID if one is specified */
1510 if (phba->lpfc_injerr_nportid &&
1511 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1512 return 0;
1513
1514 /*
1515 * Make sure we have the right WWPN if one is specified.
1516 * wwn[0] should be a non-zero NAA in a good WWPN.
1517 */
1518 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1519 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1520 sizeof(struct lpfc_name)) != 0))
1521 return 0;
1522 }
1523
1524 /* Setup a ptr to the protection data if the SCSI host provides it */
1525 if (sgpe) {
1526 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1527 src += blockoff;
1528 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1529 }
1530
James Smartf9bb2da2011-10-10 21:34:11 -04001531 /* Should we change the Reference Tag */
1532 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001533 if (phba->lpfc_injerr_wref_cnt) {
1534 switch (op) {
1535 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001536 if (src) {
1537 /*
1538 * For WRITE_PASS, force the error
1539 * to be sent on the wire. It should
1540 * be detected by the Target.
1541 * If blockoff != 0 error will be
1542 * inserted in middle of the IO.
1543 */
James Smartf9bb2da2011-10-10 21:34:11 -04001544
James Smartacd68592012-01-18 16:25:09 -05001545 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1546 "9076 BLKGRD: Injecting reftag error: "
1547 "write lba x%lx + x%x oldrefTag x%x\n",
1548 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001549 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001550
1551 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001552 * Save the old ref_tag so we can
1553 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001554 */
James Smart9a6b09c2012-03-01 22:37:42 -05001555 if (lpfc_cmd) {
1556 lpfc_cmd->prot_data_type =
1557 LPFC_INJERR_REFTAG;
1558 lpfc_cmd->prot_data_segment =
1559 src;
1560 lpfc_cmd->prot_data =
1561 src->ref_tag;
1562 }
1563 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001564 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001565 if (phba->lpfc_injerr_wref_cnt == 0) {
1566 phba->lpfc_injerr_nportid = 0;
1567 phba->lpfc_injerr_lba =
1568 LPFC_INJERR_LBA_OFF;
1569 memset(&phba->lpfc_injerr_wwpn,
1570 0, sizeof(struct lpfc_name));
1571 }
James Smart9a6b09c2012-03-01 22:37:42 -05001572 rc = BG_ERR_TGT | BG_ERR_CHECK;
1573
James Smartacd68592012-01-18 16:25:09 -05001574 break;
1575 }
1576 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001577 case SCSI_PROT_WRITE_INSERT:
1578 /*
1579 * For WRITE_INSERT, force the error
1580 * to be sent on the wire. It should be
1581 * detected by the Target.
1582 */
1583 /* DEADBEEF will be the reftag on the wire */
1584 *reftag = 0xDEADBEEF;
1585 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001586 if (phba->lpfc_injerr_wref_cnt == 0) {
1587 phba->lpfc_injerr_nportid = 0;
1588 phba->lpfc_injerr_lba =
1589 LPFC_INJERR_LBA_OFF;
1590 memset(&phba->lpfc_injerr_wwpn,
1591 0, sizeof(struct lpfc_name));
1592 }
James Smart9a6b09c2012-03-01 22:37:42 -05001593 rc = BG_ERR_TGT | BG_ERR_CHECK;
1594
1595 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1596 "9078 BLKGRD: Injecting reftag error: "
1597 "write lba x%lx\n", (unsigned long)lba);
1598 break;
James Smartacd68592012-01-18 16:25:09 -05001599 case SCSI_PROT_WRITE_STRIP:
1600 /*
1601 * For WRITE_STRIP and WRITE_PASS,
1602 * force the error on data
1603 * being copied from SLI-Host to SLI-Port.
1604 */
1605 *reftag = 0xDEADBEEF;
1606 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001607 if (phba->lpfc_injerr_wref_cnt == 0) {
1608 phba->lpfc_injerr_nportid = 0;
1609 phba->lpfc_injerr_lba =
1610 LPFC_INJERR_LBA_OFF;
1611 memset(&phba->lpfc_injerr_wwpn,
1612 0, sizeof(struct lpfc_name));
1613 }
James Smartacd68592012-01-18 16:25:09 -05001614 rc = BG_ERR_INIT;
1615
1616 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1617 "9077 BLKGRD: Injecting reftag error: "
1618 "write lba x%lx\n", (unsigned long)lba);
1619 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001620 }
James Smartacd68592012-01-18 16:25:09 -05001621 }
1622 if (phba->lpfc_injerr_rref_cnt) {
1623 switch (op) {
1624 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001625 case SCSI_PROT_READ_STRIP:
1626 case SCSI_PROT_READ_PASS:
1627 /*
1628 * For READ_STRIP and READ_PASS, force the
1629 * error on data being read off the wire. It
1630 * should force an IO error to the driver.
1631 */
James Smartf9bb2da2011-10-10 21:34:11 -04001632 *reftag = 0xDEADBEEF;
1633 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001634 if (phba->lpfc_injerr_rref_cnt == 0) {
1635 phba->lpfc_injerr_nportid = 0;
1636 phba->lpfc_injerr_lba =
1637 LPFC_INJERR_LBA_OFF;
1638 memset(&phba->lpfc_injerr_wwpn,
1639 0, sizeof(struct lpfc_name));
1640 }
James Smartacd68592012-01-18 16:25:09 -05001641 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001642
1643 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001644 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001645 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001646 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001647 }
1648 }
1649 }
1650
1651 /* Should we change the Application Tag */
1652 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001653 if (phba->lpfc_injerr_wapp_cnt) {
1654 switch (op) {
1655 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001656 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001657 /*
1658 * For WRITE_PASS, force the error
1659 * to be sent on the wire. It should
1660 * be detected by the Target.
1661 * If blockoff != 0 error will be
1662 * inserted in middle of the IO.
1663 */
1664
James Smartacd68592012-01-18 16:25:09 -05001665 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1666 "9080 BLKGRD: Injecting apptag error: "
1667 "write lba x%lx + x%x oldappTag x%x\n",
1668 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001669 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001670
1671 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001672 * Save the old app_tag so we can
1673 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001674 */
James Smart9a6b09c2012-03-01 22:37:42 -05001675 if (lpfc_cmd) {
1676 lpfc_cmd->prot_data_type =
1677 LPFC_INJERR_APPTAG;
1678 lpfc_cmd->prot_data_segment =
1679 src;
1680 lpfc_cmd->prot_data =
1681 src->app_tag;
1682 }
1683 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001684 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001685 if (phba->lpfc_injerr_wapp_cnt == 0) {
1686 phba->lpfc_injerr_nportid = 0;
1687 phba->lpfc_injerr_lba =
1688 LPFC_INJERR_LBA_OFF;
1689 memset(&phba->lpfc_injerr_wwpn,
1690 0, sizeof(struct lpfc_name));
1691 }
James Smart9a6b09c2012-03-01 22:37:42 -05001692 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001693 break;
1694 }
1695 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001696 case SCSI_PROT_WRITE_INSERT:
1697 /*
1698 * For WRITE_INSERT, force the
1699 * error to be sent on the wire. It should be
1700 * detected by the Target.
1701 */
1702 /* DEAD will be the apptag on the wire */
1703 *apptag = 0xDEAD;
1704 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001705 if (phba->lpfc_injerr_wapp_cnt == 0) {
1706 phba->lpfc_injerr_nportid = 0;
1707 phba->lpfc_injerr_lba =
1708 LPFC_INJERR_LBA_OFF;
1709 memset(&phba->lpfc_injerr_wwpn,
1710 0, sizeof(struct lpfc_name));
1711 }
James Smart9a6b09c2012-03-01 22:37:42 -05001712 rc = BG_ERR_TGT | BG_ERR_CHECK;
1713
1714 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1715 "0813 BLKGRD: Injecting apptag error: "
1716 "write lba x%lx\n", (unsigned long)lba);
1717 break;
James Smartacd68592012-01-18 16:25:09 -05001718 case SCSI_PROT_WRITE_STRIP:
1719 /*
1720 * For WRITE_STRIP and WRITE_PASS,
1721 * force the error on data
1722 * being copied from SLI-Host to SLI-Port.
1723 */
1724 *apptag = 0xDEAD;
1725 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001726 if (phba->lpfc_injerr_wapp_cnt == 0) {
1727 phba->lpfc_injerr_nportid = 0;
1728 phba->lpfc_injerr_lba =
1729 LPFC_INJERR_LBA_OFF;
1730 memset(&phba->lpfc_injerr_wwpn,
1731 0, sizeof(struct lpfc_name));
1732 }
James Smartacd68592012-01-18 16:25:09 -05001733 rc = BG_ERR_INIT;
1734
1735 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1736 "0812 BLKGRD: Injecting apptag error: "
1737 "write lba x%lx\n", (unsigned long)lba);
1738 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001739 }
James Smartacd68592012-01-18 16:25:09 -05001740 }
1741 if (phba->lpfc_injerr_rapp_cnt) {
1742 switch (op) {
1743 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001744 case SCSI_PROT_READ_STRIP:
1745 case SCSI_PROT_READ_PASS:
1746 /*
1747 * For READ_STRIP and READ_PASS, force the
1748 * error on data being read off the wire. It
1749 * should force an IO error to the driver.
1750 */
James Smartf9bb2da2011-10-10 21:34:11 -04001751 *apptag = 0xDEAD;
1752 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001753 if (phba->lpfc_injerr_rapp_cnt == 0) {
1754 phba->lpfc_injerr_nportid = 0;
1755 phba->lpfc_injerr_lba =
1756 LPFC_INJERR_LBA_OFF;
1757 memset(&phba->lpfc_injerr_wwpn,
1758 0, sizeof(struct lpfc_name));
1759 }
James Smartacd68592012-01-18 16:25:09 -05001760 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001761
1762 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001763 "0814 BLKGRD: Injecting apptag error: "
1764 "read lba x%lx\n", (unsigned long)lba);
1765 break;
1766 }
1767 }
1768 }
1769
1770
1771 /* Should we change the Guard Tag */
1772 if (new_guard) {
1773 if (phba->lpfc_injerr_wgrd_cnt) {
1774 switch (op) {
1775 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001776 rc = BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001777 /* Drop thru */
James Smartacd68592012-01-18 16:25:09 -05001778
James Smartacd68592012-01-18 16:25:09 -05001779 case SCSI_PROT_WRITE_INSERT:
1780 /*
1781 * For WRITE_INSERT, force the
1782 * error to be sent on the wire. It should be
1783 * detected by the Target.
1784 */
1785 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001786 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1787 phba->lpfc_injerr_nportid = 0;
1788 phba->lpfc_injerr_lba =
1789 LPFC_INJERR_LBA_OFF;
1790 memset(&phba->lpfc_injerr_wwpn,
1791 0, sizeof(struct lpfc_name));
1792 }
James Smartacd68592012-01-18 16:25:09 -05001793
James Smart9a6b09c2012-03-01 22:37:42 -05001794 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001795 /* Signals the caller to swap CRC->CSUM */
1796
1797 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1798 "0817 BLKGRD: Injecting guard error: "
1799 "write lba x%lx\n", (unsigned long)lba);
1800 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001801 case SCSI_PROT_WRITE_STRIP:
1802 /*
1803 * For WRITE_STRIP and WRITE_PASS,
1804 * force the error on data
1805 * being copied from SLI-Host to SLI-Port.
1806 */
1807 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001808 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1809 phba->lpfc_injerr_nportid = 0;
1810 phba->lpfc_injerr_lba =
1811 LPFC_INJERR_LBA_OFF;
1812 memset(&phba->lpfc_injerr_wwpn,
1813 0, sizeof(struct lpfc_name));
1814 }
James Smart9a6b09c2012-03-01 22:37:42 -05001815
1816 rc = BG_ERR_INIT | BG_ERR_SWAP;
1817 /* Signals the caller to swap CRC->CSUM */
1818
1819 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1820 "0816 BLKGRD: Injecting guard error: "
1821 "write lba x%lx\n", (unsigned long)lba);
1822 break;
James Smartacd68592012-01-18 16:25:09 -05001823 }
1824 }
1825 if (phba->lpfc_injerr_rgrd_cnt) {
1826 switch (op) {
1827 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001828 case SCSI_PROT_READ_STRIP:
1829 case SCSI_PROT_READ_PASS:
1830 /*
1831 * For READ_STRIP and READ_PASS, force the
1832 * error on data being read off the wire. It
1833 * should force an IO error to the driver.
1834 */
James Smartacd68592012-01-18 16:25:09 -05001835 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001836 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1837 phba->lpfc_injerr_nportid = 0;
1838 phba->lpfc_injerr_lba =
1839 LPFC_INJERR_LBA_OFF;
1840 memset(&phba->lpfc_injerr_wwpn,
1841 0, sizeof(struct lpfc_name));
1842 }
James Smartacd68592012-01-18 16:25:09 -05001843
James Smart9a6b09c2012-03-01 22:37:42 -05001844 rc = BG_ERR_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001845 /* Signals the caller to swap CRC->CSUM */
1846
1847 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1848 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001849 "read lba x%lx\n", (unsigned long)lba);
1850 }
1851 }
1852 }
1853
James Smartf9bb2da2011-10-10 21:34:11 -04001854 return rc;
1855}
1856#endif
1857
James Smartacd68592012-01-18 16:25:09 -05001858/**
1859 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1860 * the specified SCSI command.
1861 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001862 * @sc: The SCSI command to examine
1863 * @txopt: (out) BlockGuard operation for transmitted data
1864 * @rxopt: (out) BlockGuard operation for received data
1865 *
1866 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1867 *
James Smartacd68592012-01-18 16:25:09 -05001868 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001869static int
James Smart6c8eea52010-04-06 14:49:53 -04001870lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1871 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001872{
James Smart6c8eea52010-04-06 14:49:53 -04001873 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001874
James Smart9c6aa9d2013-05-31 17:03:39 -04001875 if (lpfc_cmd_guard_csum(sc)) {
James Smarte2a0a9d2008-12-04 22:40:02 -05001876 switch (scsi_get_prot_op(sc)) {
1877 case SCSI_PROT_READ_INSERT:
1878 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001879 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001880 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001881 break;
1882
1883 case SCSI_PROT_READ_STRIP:
1884 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001885 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001886 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001887 break;
1888
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001889 case SCSI_PROT_READ_PASS:
1890 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001891 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001892 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001893 break;
1894
James Smarte2a0a9d2008-12-04 22:40:02 -05001895 case SCSI_PROT_NORMAL:
1896 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001897 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001898 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1899 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001900 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001901 break;
1902
1903 }
James Smart7c56b9f2011-07-22 18:36:25 -04001904 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001905 switch (scsi_get_prot_op(sc)) {
1906 case SCSI_PROT_READ_STRIP:
1907 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001908 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001909 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001910 break;
1911
1912 case SCSI_PROT_READ_PASS:
1913 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001914 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001915 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001916 break;
1917
James Smarte2a0a9d2008-12-04 22:40:02 -05001918 case SCSI_PROT_READ_INSERT:
1919 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001920 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001921 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001922 break;
1923
James Smarte2a0a9d2008-12-04 22:40:02 -05001924 case SCSI_PROT_NORMAL:
1925 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001926 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001927 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1928 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001929 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001930 break;
1931 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001932 }
1933
James Smart6c8eea52010-04-06 14:49:53 -04001934 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001935}
1936
James Smartacd68592012-01-18 16:25:09 -05001937#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1938/**
1939 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1940 * the specified SCSI command in order to force a guard tag error.
1941 * @phba: The Hba for which this call is being executed.
1942 * @sc: The SCSI command to examine
1943 * @txopt: (out) BlockGuard operation for transmitted data
1944 * @rxopt: (out) BlockGuard operation for received data
1945 *
1946 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1947 *
1948 **/
1949static int
1950lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1951 uint8_t *txop, uint8_t *rxop)
1952{
James Smartacd68592012-01-18 16:25:09 -05001953 uint8_t ret = 0;
1954
James Smart9c6aa9d2013-05-31 17:03:39 -04001955 if (lpfc_cmd_guard_csum(sc)) {
James Smartacd68592012-01-18 16:25:09 -05001956 switch (scsi_get_prot_op(sc)) {
1957 case SCSI_PROT_READ_INSERT:
1958 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001959 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001960 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001961 break;
1962
1963 case SCSI_PROT_READ_STRIP:
1964 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001965 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001966 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001967 break;
1968
1969 case SCSI_PROT_READ_PASS:
1970 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001971 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001972 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001973 break;
1974
1975 case SCSI_PROT_NORMAL:
1976 default:
1977 break;
1978
1979 }
1980 } else {
1981 switch (scsi_get_prot_op(sc)) {
1982 case SCSI_PROT_READ_STRIP:
1983 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001984 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001985 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001986 break;
1987
1988 case SCSI_PROT_READ_PASS:
1989 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001990 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05001991 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001992 break;
1993
1994 case SCSI_PROT_READ_INSERT:
1995 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001996 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001997 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001998 break;
1999
2000 case SCSI_PROT_NORMAL:
2001 default:
2002 break;
2003 }
2004 }
2005
2006 return ret;
2007}
2008#endif
2009
2010/**
2011 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
2012 * @phba: The Hba for which this call is being executed.
2013 * @sc: pointer to scsi command we're working on
2014 * @bpl: pointer to buffer list for protection groups
2015 * @datacnt: number of segments of data that have been dma mapped
2016 *
2017 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05002018 * type LPFC_PG_TYPE_NO_DIF
2019 *
2020 * This is usually used when the HBA is instructed to generate
2021 * DIFs and insert them into data stream (or strip DIF from
2022 * incoming data stream)
2023 *
2024 * The buffer list consists of just one protection group described
2025 * below:
2026 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002027 * start of prot group --> | PDE_5 |
2028 * +-------------------------+
2029 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002030 * +-------------------------+
2031 * | Data BDE |
2032 * +-------------------------+
2033 * |more Data BDE's ... (opt)|
2034 * +-------------------------+
2035 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002036 *
2037 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05002038 *
2039 * Returns the number of BDEs added to the BPL.
2040 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002041static int
2042lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2043 struct ulp_bde64 *bpl, int datasegcnt)
2044{
2045 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04002046 struct lpfc_pde5 *pde5 = NULL;
2047 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002048 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04002049 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002050 int datadir = sc->sc_data_direction;
James Smart0829a192012-05-09 21:18:12 -04002051#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002052 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002053#endif
James Smartacd68592012-01-18 16:25:09 -05002054 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002055 uint32_t reftag;
James Smart7c56b9f2011-07-22 18:36:25 -04002056 unsigned blksize;
James Smart6c8eea52010-04-06 14:49:53 -04002057 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002058
James Smart6c8eea52010-04-06 14:49:53 -04002059 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2060 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002061 goto out;
2062
James Smart6c8eea52010-04-06 14:49:53 -04002063 /* extract some info from the scsi command for pde*/
James Smarte2a0a9d2008-12-04 22:40:02 -05002064 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002065 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002066
James Smartf9bb2da2011-10-10 21:34:11 -04002067#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002068 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002069 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002070 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002071 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002072 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002073 checking = 0;
2074 }
James Smartf9bb2da2011-10-10 21:34:11 -04002075#endif
2076
James Smart6c8eea52010-04-06 14:49:53 -04002077 /* setup PDE5 with what we have */
2078 pde5 = (struct lpfc_pde5 *) bpl;
2079 memset(pde5, 0, sizeof(struct lpfc_pde5));
2080 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002081
James Smartbc739052010-08-04 16:11:18 -04002082 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002083 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002084 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002085
James Smart6c8eea52010-04-06 14:49:53 -04002086 /* advance bpl and increment bde count */
2087 num_bde++;
2088 bpl++;
2089 pde6 = (struct lpfc_pde6 *) bpl;
2090
2091 /* setup PDE6 with the rest of the info */
2092 memset(pde6, 0, sizeof(struct lpfc_pde6));
2093 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2094 bf_set(pde6_optx, pde6, txop);
2095 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002096
2097 /*
2098 * We only need to check the data on READs, for WRITEs
2099 * protection data is automatically generated, not checked.
2100 */
James Smart6c8eea52010-04-06 14:49:53 -04002101 if (datadir == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002102 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002103 bf_set(pde6_ce, pde6, checking);
2104 else
2105 bf_set(pde6_ce, pde6, 0);
2106
James Smart9c6aa9d2013-05-31 17:03:39 -04002107 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002108 bf_set(pde6_re, pde6, checking);
2109 else
2110 bf_set(pde6_re, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002111 }
2112 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002113 bf_set(pde6_ae, pde6, 0);
2114 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002115
James Smartbc739052010-08-04 16:11:18 -04002116 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002117 pde6->word0 = cpu_to_le32(pde6->word0);
2118 pde6->word1 = cpu_to_le32(pde6->word1);
2119 pde6->word2 = cpu_to_le32(pde6->word2);
2120
James Smart6c8eea52010-04-06 14:49:53 -04002121 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002122 num_bde++;
2123 bpl++;
2124
2125 /* assumption: caller has already run dma_map_sg on command data */
2126 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2127 physaddr = sg_dma_address(sgde);
2128 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
2129 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2130 bpl->tus.f.bdeSize = sg_dma_len(sgde);
2131 if (datadir == DMA_TO_DEVICE)
2132 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2133 else
2134 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2135 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2136 bpl++;
2137 num_bde++;
2138 }
2139
2140out:
2141 return num_bde;
2142}
2143
James Smartacd68592012-01-18 16:25:09 -05002144/**
2145 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
2146 * @phba: The Hba for which this call is being executed.
2147 * @sc: pointer to scsi command we're working on
2148 * @bpl: pointer to buffer list for protection groups
2149 * @datacnt: number of segments of data that have been dma mapped
2150 * @protcnt: number of segment of protection data that have been dma mapped
2151 *
2152 * This function sets up BPL buffer list for protection groups of
2153 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05002154 *
2155 * This is usually used when DIFs are in their own buffers,
2156 * separate from the data. The HBA can then by instructed
2157 * to place the DIFs in the outgoing stream. For read operations,
2158 * The HBA could extract the DIFs and place it in DIF buffers.
2159 *
2160 * The buffer list for this type consists of one or more of the
2161 * protection groups described below:
2162 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002163 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002164 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002165 * | PDE_6 |
2166 * +-------------------------+
2167 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05002168 * +-------------------------+
2169 * | Data BDE |
2170 * +-------------------------+
2171 * |more Data BDE's ... (opt)|
2172 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002173 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002174 * +-------------------------+
2175 * | ... |
2176 * +-------------------------+
2177 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002178 * Note: It is assumed that both data and protection s/g buffers have been
2179 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05002180 *
2181 * Returns the number of BDEs added to the BPL.
2182 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002183static int
2184lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2185 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2186{
2187 struct scatterlist *sgde = NULL; /* s/g data entry */
2188 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04002189 struct lpfc_pde5 *pde5 = NULL;
2190 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05002191 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002192 dma_addr_t dataphysaddr, protphysaddr;
2193 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05002194 unsigned int split_offset;
2195 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05002196 unsigned int protgrp_blks, protgrp_bytes;
2197 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04002198 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002199 int datadir = sc->sc_data_direction;
2200 unsigned char pgdone = 0, alldone = 0;
2201 unsigned blksize;
James Smart0829a192012-05-09 21:18:12 -04002202#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002203 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002204#endif
James Smartacd68592012-01-18 16:25:09 -05002205 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002206 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04002207 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002208 int num_bde = 0;
2209
2210 sgpe = scsi_prot_sglist(sc);
2211 sgde = scsi_sglist(sc);
2212
2213 if (!sgpe || !sgde) {
2214 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2215 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2216 sgpe, sgde);
2217 return 0;
2218 }
2219
James Smart6c8eea52010-04-06 14:49:53 -04002220 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2221 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002222 goto out;
2223
James Smart6c8eea52010-04-06 14:49:53 -04002224 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05002225 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002226 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002227
James Smartf9bb2da2011-10-10 21:34:11 -04002228#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002229 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002230 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002231 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002232 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002233 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002234 checking = 0;
2235 }
James Smartf9bb2da2011-10-10 21:34:11 -04002236#endif
2237
James Smarte2a0a9d2008-12-04 22:40:02 -05002238 split_offset = 0;
2239 do {
James Smart96f70772013-04-17 20:16:15 -04002240 /* Check to see if we ran out of space */
2241 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
2242 return num_bde + 3;
2243
James Smart6c8eea52010-04-06 14:49:53 -04002244 /* setup PDE5 with what we have */
2245 pde5 = (struct lpfc_pde5 *) bpl;
2246 memset(pde5, 0, sizeof(struct lpfc_pde5));
2247 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002248
James Smartbc739052010-08-04 16:11:18 -04002249 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002250 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002251 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002252
James Smart6c8eea52010-04-06 14:49:53 -04002253 /* advance bpl and increment bde count */
2254 num_bde++;
2255 bpl++;
2256 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002257
James Smart6c8eea52010-04-06 14:49:53 -04002258 /* setup PDE6 with the rest of the info */
2259 memset(pde6, 0, sizeof(struct lpfc_pde6));
2260 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2261 bf_set(pde6_optx, pde6, txop);
2262 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002263
James Smart9c6aa9d2013-05-31 17:03:39 -04002264 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002265 bf_set(pde6_ce, pde6, checking);
2266 else
2267 bf_set(pde6_ce, pde6, 0);
2268
James Smart9c6aa9d2013-05-31 17:03:39 -04002269 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002270 bf_set(pde6_re, pde6, checking);
2271 else
2272 bf_set(pde6_re, pde6, 0);
2273
James Smart6c8eea52010-04-06 14:49:53 -04002274 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002275 bf_set(pde6_ae, pde6, 0);
2276 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002277
James Smartbc739052010-08-04 16:11:18 -04002278 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002279 pde6->word0 = cpu_to_le32(pde6->word0);
2280 pde6->word1 = cpu_to_le32(pde6->word1);
2281 pde6->word2 = cpu_to_le32(pde6->word2);
2282
James Smart6c8eea52010-04-06 14:49:53 -04002283 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002284 num_bde++;
2285 bpl++;
2286
2287 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05002288 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2289 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05002290
James Smarte2a0a9d2008-12-04 22:40:02 -05002291 /* must be integer multiple of the DIF block length */
2292 BUG_ON(protgroup_len % 8);
2293
James Smart7f860592011-03-11 16:05:52 -05002294 pde7 = (struct lpfc_pde7 *) bpl;
2295 memset(pde7, 0, sizeof(struct lpfc_pde7));
2296 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2297
James Smart7c56b9f2011-07-22 18:36:25 -04002298 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2299 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05002300
James Smarte2a0a9d2008-12-04 22:40:02 -05002301 protgrp_blks = protgroup_len / 8;
2302 protgrp_bytes = protgrp_blks * blksize;
2303
James Smart7f860592011-03-11 16:05:52 -05002304 /* check if this pde is crossing the 4K boundary; if so split */
2305 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2306 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2307 protgroup_offset += protgroup_remainder;
2308 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04002309 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05002310 } else {
2311 protgroup_offset = 0;
2312 curr_prot++;
2313 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002314
James Smarte2a0a9d2008-12-04 22:40:02 -05002315 num_bde++;
2316
2317 /* setup BDE's for data blocks associated with DIF data */
2318 pgdone = 0;
2319 subtotal = 0; /* total bytes processed for current prot grp */
2320 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002321 /* Check to see if we ran out of space */
2322 if (num_bde >= phba->cfg_total_seg_cnt)
2323 return num_bde + 1;
2324
James Smarte2a0a9d2008-12-04 22:40:02 -05002325 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04002326 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2327 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002328 __func__);
2329 return 0;
2330 }
2331 bpl++;
2332 dataphysaddr = sg_dma_address(sgde) + split_offset;
2333 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2334 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2335
2336 remainder = sg_dma_len(sgde) - split_offset;
2337
2338 if ((subtotal + remainder) <= protgrp_bytes) {
2339 /* we can use this whole buffer */
2340 bpl->tus.f.bdeSize = remainder;
2341 split_offset = 0;
2342
2343 if ((subtotal + remainder) == protgrp_bytes)
2344 pgdone = 1;
2345 } else {
2346 /* must split this buffer with next prot grp */
2347 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2348 split_offset += bpl->tus.f.bdeSize;
2349 }
2350
2351 subtotal += bpl->tus.f.bdeSize;
2352
2353 if (datadir == DMA_TO_DEVICE)
2354 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2355 else
2356 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2357 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2358
2359 num_bde++;
2360 curr_data++;
2361
2362 if (split_offset)
2363 break;
2364
2365 /* Move to the next s/g segment if possible */
2366 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04002367
James Smarte2a0a9d2008-12-04 22:40:02 -05002368 }
2369
James Smart7f860592011-03-11 16:05:52 -05002370 if (protgroup_offset) {
2371 /* update the reference tag */
2372 reftag += protgrp_blks;
2373 bpl++;
2374 continue;
2375 }
2376
James Smarte2a0a9d2008-12-04 22:40:02 -05002377 /* are we done ? */
2378 if (curr_prot == protcnt) {
2379 alldone = 1;
2380 } else if (curr_prot < protcnt) {
2381 /* advance to next prot buffer */
2382 sgpe = sg_next(sgpe);
2383 bpl++;
2384
2385 /* update the reference tag */
2386 reftag += protgrp_blks;
2387 } else {
2388 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04002389 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2390 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05002391 }
2392
2393 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05002394out:
2395
James Smarte2a0a9d2008-12-04 22:40:02 -05002396 return num_bde;
2397}
James Smart7f860592011-03-11 16:05:52 -05002398
James Smartacd68592012-01-18 16:25:09 -05002399/**
2400 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2401 * @phba: The Hba for which this call is being executed.
2402 * @sc: pointer to scsi command we're working on
2403 * @sgl: pointer to buffer list for protection groups
2404 * @datacnt: number of segments of data that have been dma mapped
2405 *
2406 * This function sets up SGL buffer list for protection groups of
2407 * type LPFC_PG_TYPE_NO_DIF
2408 *
2409 * This is usually used when the HBA is instructed to generate
2410 * DIFs and insert them into data stream (or strip DIF from
2411 * incoming data stream)
2412 *
2413 * The buffer list consists of just one protection group described
2414 * below:
2415 * +-------------------------+
2416 * start of prot group --> | DI_SEED |
2417 * +-------------------------+
2418 * | Data SGE |
2419 * +-------------------------+
2420 * |more Data SGE's ... (opt)|
2421 * +-------------------------+
2422 *
2423 *
2424 * Note: Data s/g buffers have been dma mapped
2425 *
2426 * Returns the number of SGEs added to the SGL.
2427 **/
2428static int
2429lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2430 struct sli4_sge *sgl, int datasegcnt)
2431{
2432 struct scatterlist *sgde = NULL; /* s/g data entry */
2433 struct sli4_sge_diseed *diseed = NULL;
2434 dma_addr_t physaddr;
2435 int i = 0, num_sge = 0, status;
James Smartacd68592012-01-18 16:25:09 -05002436 uint32_t reftag;
2437 unsigned blksize;
2438 uint8_t txop, rxop;
James Smart0829a192012-05-09 21:18:12 -04002439#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002440 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002441#endif
James Smartacd68592012-01-18 16:25:09 -05002442 uint32_t checking = 1;
2443 uint32_t dma_len;
2444 uint32_t dma_offset = 0;
2445
2446 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2447 if (status)
2448 goto out;
2449
2450 /* extract some info from the scsi command for pde*/
2451 blksize = lpfc_cmd_blksize(sc);
2452 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2453
2454#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002455 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002456 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002457 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002458 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002459 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002460 checking = 0;
2461 }
2462#endif
2463
2464 /* setup DISEED with what we have */
2465 diseed = (struct sli4_sge_diseed *) sgl;
2466 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2467 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2468
2469 /* Endianness conversion if necessary */
2470 diseed->ref_tag = cpu_to_le32(reftag);
2471 diseed->ref_tag_tran = diseed->ref_tag;
2472
James Smarta6887e22013-04-17 20:18:07 -04002473 /*
2474 * We only need to check the data on READs, for WRITEs
2475 * protection data is automatically generated, not checked.
2476 */
2477 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002478 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002479 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2480 else
2481 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2482
James Smart9c6aa9d2013-05-31 17:03:39 -04002483 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002484 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2485 else
2486 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2487 }
2488
James Smartacd68592012-01-18 16:25:09 -05002489 /* setup DISEED with the rest of the info */
2490 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2491 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002492
James Smartacd68592012-01-18 16:25:09 -05002493 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2494 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2495
2496 /* Endianness conversion if necessary for DISEED */
2497 diseed->word2 = cpu_to_le32(diseed->word2);
2498 diseed->word3 = cpu_to_le32(diseed->word3);
2499
2500 /* advance bpl and increment sge count */
2501 num_sge++;
2502 sgl++;
2503
2504 /* assumption: caller has already run dma_map_sg on command data */
2505 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2506 physaddr = sg_dma_address(sgde);
2507 dma_len = sg_dma_len(sgde);
2508 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2509 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2510 if ((i + 1) == datasegcnt)
2511 bf_set(lpfc_sli4_sge_last, sgl, 1);
2512 else
2513 bf_set(lpfc_sli4_sge_last, sgl, 0);
2514 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2515 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2516
2517 sgl->sge_len = cpu_to_le32(dma_len);
2518 dma_offset += dma_len;
2519
2520 sgl++;
2521 num_sge++;
2522 }
2523
2524out:
2525 return num_sge;
2526}
2527
2528/**
2529 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2530 * @phba: The Hba for which this call is being executed.
2531 * @sc: pointer to scsi command we're working on
2532 * @sgl: pointer to buffer list for protection groups
2533 * @datacnt: number of segments of data that have been dma mapped
2534 * @protcnt: number of segment of protection data that have been dma mapped
2535 *
2536 * This function sets up SGL buffer list for protection groups of
2537 * type LPFC_PG_TYPE_DIF
2538 *
2539 * This is usually used when DIFs are in their own buffers,
2540 * separate from the data. The HBA can then by instructed
2541 * to place the DIFs in the outgoing stream. For read operations,
2542 * The HBA could extract the DIFs and place it in DIF buffers.
2543 *
2544 * The buffer list for this type consists of one or more of the
2545 * protection groups described below:
2546 * +-------------------------+
2547 * start of first prot group --> | DISEED |
2548 * +-------------------------+
2549 * | DIF (Prot SGE) |
2550 * +-------------------------+
2551 * | Data SGE |
2552 * +-------------------------+
2553 * |more Data SGE's ... (opt)|
2554 * +-------------------------+
2555 * start of new prot group --> | DISEED |
2556 * +-------------------------+
2557 * | ... |
2558 * +-------------------------+
2559 *
2560 * Note: It is assumed that both data and protection s/g buffers have been
2561 * mapped for DMA
2562 *
2563 * Returns the number of SGEs added to the SGL.
2564 **/
2565static int
2566lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2567 struct sli4_sge *sgl, int datacnt, int protcnt)
2568{
2569 struct scatterlist *sgde = NULL; /* s/g data entry */
2570 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2571 struct sli4_sge_diseed *diseed = NULL;
2572 dma_addr_t dataphysaddr, protphysaddr;
2573 unsigned short curr_data = 0, curr_prot = 0;
2574 unsigned int split_offset;
2575 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2576 unsigned int protgrp_blks, protgrp_bytes;
2577 unsigned int remainder, subtotal;
2578 int status;
2579 unsigned char pgdone = 0, alldone = 0;
2580 unsigned blksize;
2581 uint32_t reftag;
2582 uint8_t txop, rxop;
2583 uint32_t dma_len;
James Smart0829a192012-05-09 21:18:12 -04002584#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002585 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002586#endif
James Smartacd68592012-01-18 16:25:09 -05002587 uint32_t checking = 1;
2588 uint32_t dma_offset = 0;
2589 int num_sge = 0;
2590
2591 sgpe = scsi_prot_sglist(sc);
2592 sgde = scsi_sglist(sc);
2593
2594 if (!sgpe || !sgde) {
2595 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2596 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2597 sgpe, sgde);
2598 return 0;
2599 }
2600
2601 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2602 if (status)
2603 goto out;
2604
2605 /* extract some info from the scsi command */
2606 blksize = lpfc_cmd_blksize(sc);
2607 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2608
2609#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002610 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002611 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002612 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002613 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002614 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002615 checking = 0;
2616 }
2617#endif
2618
2619 split_offset = 0;
2620 do {
James Smart96f70772013-04-17 20:16:15 -04002621 /* Check to see if we ran out of space */
2622 if (num_sge >= (phba->cfg_total_seg_cnt - 2))
2623 return num_sge + 3;
2624
James Smartacd68592012-01-18 16:25:09 -05002625 /* setup DISEED with what we have */
2626 diseed = (struct sli4_sge_diseed *) sgl;
2627 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2628 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2629
2630 /* Endianness conversion if necessary */
2631 diseed->ref_tag = cpu_to_le32(reftag);
2632 diseed->ref_tag_tran = diseed->ref_tag;
2633
James Smart9c6aa9d2013-05-31 17:03:39 -04002634 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD)) {
James Smarta6887e22013-04-17 20:18:07 -04002635 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2636
2637 } else {
2638 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2639 /*
2640 * When in this mode, the hardware will replace
2641 * the guard tag from the host with a
2642 * newly generated good CRC for the wire.
2643 * Switch to raw mode here to avoid this
2644 * behavior. What the host sends gets put on the wire.
2645 */
2646 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2647 txop = BG_OP_RAW_MODE;
2648 rxop = BG_OP_RAW_MODE;
2649 }
2650 }
2651
2652
James Smart9c6aa9d2013-05-31 17:03:39 -04002653 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002654 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2655 else
2656 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2657
James Smartacd68592012-01-18 16:25:09 -05002658 /* setup DISEED with the rest of the info */
2659 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2660 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002661
James Smartacd68592012-01-18 16:25:09 -05002662 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2663 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2664
2665 /* Endianness conversion if necessary for DISEED */
2666 diseed->word2 = cpu_to_le32(diseed->word2);
2667 diseed->word3 = cpu_to_le32(diseed->word3);
2668
2669 /* advance sgl and increment bde count */
2670 num_sge++;
2671 sgl++;
2672
2673 /* setup the first BDE that points to protection buffer */
2674 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2675 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2676
2677 /* must be integer multiple of the DIF block length */
2678 BUG_ON(protgroup_len % 8);
2679
2680 /* Now setup DIF SGE */
2681 sgl->word2 = 0;
2682 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2683 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2684 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2685 sgl->word2 = cpu_to_le32(sgl->word2);
2686
2687 protgrp_blks = protgroup_len / 8;
2688 protgrp_bytes = protgrp_blks * blksize;
2689
2690 /* check if DIF SGE is crossing the 4K boundary; if so split */
2691 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2692 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2693 protgroup_offset += protgroup_remainder;
2694 protgrp_blks = protgroup_remainder / 8;
2695 protgrp_bytes = protgrp_blks * blksize;
2696 } else {
2697 protgroup_offset = 0;
2698 curr_prot++;
2699 }
2700
2701 num_sge++;
2702
2703 /* setup SGE's for data blocks associated with DIF data */
2704 pgdone = 0;
2705 subtotal = 0; /* total bytes processed for current prot grp */
2706 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002707 /* Check to see if we ran out of space */
2708 if (num_sge >= phba->cfg_total_seg_cnt)
2709 return num_sge + 1;
2710
James Smartacd68592012-01-18 16:25:09 -05002711 if (!sgde) {
2712 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2713 "9086 BLKGRD:%s Invalid data segment\n",
2714 __func__);
2715 return 0;
2716 }
2717 sgl++;
2718 dataphysaddr = sg_dma_address(sgde) + split_offset;
2719
2720 remainder = sg_dma_len(sgde) - split_offset;
2721
2722 if ((subtotal + remainder) <= protgrp_bytes) {
2723 /* we can use this whole buffer */
2724 dma_len = remainder;
2725 split_offset = 0;
2726
2727 if ((subtotal + remainder) == protgrp_bytes)
2728 pgdone = 1;
2729 } else {
2730 /* must split this buffer with next prot grp */
2731 dma_len = protgrp_bytes - subtotal;
2732 split_offset += dma_len;
2733 }
2734
2735 subtotal += dma_len;
2736
2737 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2738 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2739 bf_set(lpfc_sli4_sge_last, sgl, 0);
2740 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2741 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2742
2743 sgl->sge_len = cpu_to_le32(dma_len);
2744 dma_offset += dma_len;
2745
2746 num_sge++;
2747 curr_data++;
2748
2749 if (split_offset)
2750 break;
2751
2752 /* Move to the next s/g segment if possible */
2753 sgde = sg_next(sgde);
2754 }
2755
2756 if (protgroup_offset) {
2757 /* update the reference tag */
2758 reftag += protgrp_blks;
2759 sgl++;
2760 continue;
2761 }
2762
2763 /* are we done ? */
2764 if (curr_prot == protcnt) {
2765 bf_set(lpfc_sli4_sge_last, sgl, 1);
2766 alldone = 1;
2767 } else if (curr_prot < protcnt) {
2768 /* advance to next prot buffer */
2769 sgpe = sg_next(sgpe);
2770 sgl++;
2771
2772 /* update the reference tag */
2773 reftag += protgrp_blks;
2774 } else {
2775 /* if we're here, we have a bug */
2776 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2777 "9085 BLKGRD: bug in %s\n", __func__);
2778 }
2779
2780 } while (!alldone);
2781
2782out:
2783
2784 return num_sge;
2785}
2786
2787/**
2788 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2789 * @phba: The Hba for which this call is being executed.
2790 * @sc: pointer to scsi command we're working on
2791 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002792 * Given a SCSI command that supports DIF, determine composition of protection
2793 * groups involved in setting up buffer lists
2794 *
James Smartacd68592012-01-18 16:25:09 -05002795 * Returns: Protection group type (with or without DIF)
2796 *
2797 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002798static int
2799lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2800{
2801 int ret = LPFC_PG_TYPE_INVALID;
2802 unsigned char op = scsi_get_prot_op(sc);
2803
2804 switch (op) {
2805 case SCSI_PROT_READ_STRIP:
2806 case SCSI_PROT_WRITE_INSERT:
2807 ret = LPFC_PG_TYPE_NO_DIF;
2808 break;
2809 case SCSI_PROT_READ_INSERT:
2810 case SCSI_PROT_WRITE_STRIP:
2811 case SCSI_PROT_READ_PASS:
2812 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002813 ret = LPFC_PG_TYPE_DIF_BUF;
2814 break;
2815 default:
James Smart9c6aa9d2013-05-31 17:03:39 -04002816 if (phba)
2817 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2818 "9021 Unsupported protection op:%d\n",
2819 op);
James Smarte2a0a9d2008-12-04 22:40:02 -05002820 break;
2821 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002822 return ret;
2823}
2824
James Smartacd68592012-01-18 16:25:09 -05002825/**
James Smarta6887e22013-04-17 20:18:07 -04002826 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2827 * @phba: The Hba for which this call is being executed.
2828 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2829 *
2830 * Adjust the data length to account for how much data
2831 * is actually on the wire.
2832 *
2833 * returns the adjusted data length
2834 **/
2835static int
2836lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
2837 struct lpfc_scsi_buf *lpfc_cmd)
2838{
2839 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2840 int fcpdl;
2841
2842 fcpdl = scsi_bufflen(sc);
2843
2844 /* Check if there is protection data on the wire */
2845 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002846 /* Read check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002847 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2848 return fcpdl;
2849
2850 } else {
James Smart9c6aa9d2013-05-31 17:03:39 -04002851 /* Write check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002852 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2853 return fcpdl;
2854 }
2855
2856 /*
2857 * If we are in DIF Type 1 mode every data block has a 8 byte
James Smart9c6aa9d2013-05-31 17:03:39 -04002858 * DIF (trailer) attached to it. Must ajust FCP data length
2859 * to account for the protection data.
James Smarta6887e22013-04-17 20:18:07 -04002860 */
James Smart9c6aa9d2013-05-31 17:03:39 -04002861 fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
James Smarta6887e22013-04-17 20:18:07 -04002862
2863 return fcpdl;
2864}
2865
2866/**
James Smartacd68592012-01-18 16:25:09 -05002867 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2868 * @phba: The Hba for which this call is being executed.
2869 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2870 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002871 * This is the protection/DIF aware version of
2872 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2873 * two functions eventually, but for now, it's here
James Smartacd68592012-01-18 16:25:09 -05002874 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002875static int
James Smartacd68592012-01-18 16:25:09 -05002876lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smarte2a0a9d2008-12-04 22:40:02 -05002877 struct lpfc_scsi_buf *lpfc_cmd)
2878{
2879 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2880 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2881 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2882 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2883 uint32_t num_bde = 0;
2884 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2885 int prot_group_type = 0;
James Smarta6887e22013-04-17 20:18:07 -04002886 int fcpdl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002887
2888 /*
2889 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2890 * fcp_rsp regions to the first data bde entry
2891 */
2892 bpl += 2;
2893 if (scsi_sg_count(scsi_cmnd)) {
2894 /*
2895 * The driver stores the segment count returned from pci_map_sg
2896 * because this a count of dma-mappings used to map the use_sg
2897 * pages. They are not guaranteed to be the same for those
2898 * architectures that implement an IOMMU.
2899 */
2900 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2901 scsi_sglist(scsi_cmnd),
2902 scsi_sg_count(scsi_cmnd), datadir);
2903 if (unlikely(!datasegcnt))
2904 return 1;
2905
2906 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04002907
2908 /* First check if data segment count from SCSI Layer is good */
2909 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
2910 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002911
2912 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2913
2914 switch (prot_group_type) {
2915 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04002916
2917 /* Here we need to add a PDE5 and PDE6 to the count */
2918 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt)
2919 goto err;
2920
James Smarte2a0a9d2008-12-04 22:40:02 -05002921 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2922 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002923 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002924 if (num_bde < 2)
2925 goto err;
2926 break;
James Smart96f70772013-04-17 20:16:15 -04002927
2928 case LPFC_PG_TYPE_DIF_BUF:
James Smarte2a0a9d2008-12-04 22:40:02 -05002929 /*
2930 * This type indicates that protection buffers are
2931 * passed to the driver, so that needs to be prepared
2932 * for DMA
2933 */
2934 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2935 scsi_prot_sglist(scsi_cmnd),
2936 scsi_prot_sg_count(scsi_cmnd), datadir);
2937 if (unlikely(!protsegcnt)) {
2938 scsi_dma_unmap(scsi_cmnd);
2939 return 1;
2940 }
2941
2942 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04002943
2944 /*
2945 * There is a minimun of 4 BPLs used for every
2946 * protection data segment.
2947 */
2948 if ((lpfc_cmd->prot_seg_cnt * 4) >
2949 (phba->cfg_total_seg_cnt - 2))
2950 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002951
2952 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2953 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002954 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04002955 if ((num_bde < 3) ||
2956 (num_bde > phba->cfg_total_seg_cnt))
James Smarte2a0a9d2008-12-04 22:40:02 -05002957 goto err;
2958 break;
James Smart96f70772013-04-17 20:16:15 -04002959
James Smarte2a0a9d2008-12-04 22:40:02 -05002960 case LPFC_PG_TYPE_INVALID:
2961 default:
James Smart96f70772013-04-17 20:16:15 -04002962 scsi_dma_unmap(scsi_cmnd);
2963 lpfc_cmd->seg_cnt = 0;
2964
James Smarte2a0a9d2008-12-04 22:40:02 -05002965 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2966 "9022 Unexpected protection group %i\n",
2967 prot_group_type);
2968 return 1;
2969 }
2970 }
2971
2972 /*
2973 * Finish initializing those IOCB fields that are dependent on the
2974 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2975 * reinitialized since all iocb memory resources are used many times
2976 * for transmit, receive, and continuation bpl's.
2977 */
2978 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2979 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2980 iocb_cmd->ulpBdeCount = 1;
2981 iocb_cmd->ulpLe = 1;
2982
James Smarta6887e22013-04-17 20:18:07 -04002983 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002984 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2985
2986 /*
2987 * Due to difference in data length between DIF/non-DIF paths,
2988 * we need to set word 4 of IOCB here
2989 */
2990 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2991
2992 return 0;
2993err:
James Smart96f70772013-04-17 20:16:15 -04002994 if (lpfc_cmd->seg_cnt)
2995 scsi_dma_unmap(scsi_cmnd);
2996 if (lpfc_cmd->prot_seg_cnt)
2997 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2998 scsi_prot_sg_count(scsi_cmnd),
2999 scsi_cmnd->sc_data_direction);
3000
James Smarte2a0a9d2008-12-04 22:40:02 -05003001 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04003002 "9023 Cannot setup S/G List for HBA"
3003 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
3004 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3005 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
James Smarte2a0a9d2008-12-04 22:40:02 -05003006 prot_group_type, num_bde);
James Smart96f70772013-04-17 20:16:15 -04003007
3008 lpfc_cmd->seg_cnt = 0;
3009 lpfc_cmd->prot_seg_cnt = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05003010 return 1;
3011}
3012
3013/*
James Smart737d4242013-04-17 20:14:49 -04003014 * This function calcuates the T10 DIF guard tag
3015 * on the specified data using a CRC algorithmn
3016 * using crc_t10dif.
3017 */
3018uint16_t
3019lpfc_bg_crc(uint8_t *data, int count)
3020{
3021 uint16_t crc = 0;
3022 uint16_t x;
3023
3024 crc = crc_t10dif(data, count);
3025 x = cpu_to_be16(crc);
3026 return x;
3027}
3028
3029/*
3030 * This function calcuates the T10 DIF guard tag
3031 * on the specified data using a CSUM algorithmn
3032 * using ip_compute_csum.
3033 */
3034uint16_t
3035lpfc_bg_csum(uint8_t *data, int count)
3036{
3037 uint16_t ret;
3038
3039 ret = ip_compute_csum(data, count);
3040 return ret;
3041}
3042
3043/*
3044 * This function examines the protection data to try to determine
3045 * what type of T10-DIF error occurred.
3046 */
3047void
3048lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3049{
3050 struct scatterlist *sgpe; /* s/g prot entry */
3051 struct scatterlist *sgde; /* s/g data entry */
3052 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3053 struct scsi_dif_tuple *src = NULL;
3054 uint8_t *data_src = NULL;
3055 uint16_t guard_tag, guard_type;
3056 uint16_t start_app_tag, app_tag;
3057 uint32_t start_ref_tag, ref_tag;
3058 int prot, protsegcnt;
3059 int err_type, len, data_len;
3060 int chk_ref, chk_app, chk_guard;
3061 uint16_t sum;
3062 unsigned blksize;
3063
3064 err_type = BGS_GUARD_ERR_MASK;
3065 sum = 0;
3066 guard_tag = 0;
3067
3068 /* First check to see if there is protection data to examine */
3069 prot = scsi_get_prot_op(cmd);
3070 if ((prot == SCSI_PROT_READ_STRIP) ||
3071 (prot == SCSI_PROT_WRITE_INSERT) ||
3072 (prot == SCSI_PROT_NORMAL))
3073 goto out;
3074
3075 /* Currently the driver just supports ref_tag and guard_tag checking */
3076 chk_ref = 1;
3077 chk_app = 0;
3078 chk_guard = 0;
3079
3080 /* Setup a ptr to the protection data provided by the SCSI host */
3081 sgpe = scsi_prot_sglist(cmd);
3082 protsegcnt = lpfc_cmd->prot_seg_cnt;
3083
3084 if (sgpe && protsegcnt) {
3085
3086 /*
3087 * We will only try to verify guard tag if the segment
3088 * data length is a multiple of the blksize.
3089 */
3090 sgde = scsi_sglist(cmd);
3091 blksize = lpfc_cmd_blksize(cmd);
3092 data_src = (uint8_t *)sg_virt(sgde);
3093 data_len = sgde->length;
3094 if ((data_len & (blksize - 1)) == 0)
3095 chk_guard = 1;
3096 guard_type = scsi_host_get_guard(cmd->device->host);
3097
James Smarte85d8f92013-05-31 17:04:10 -04003098 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
James Smarta6887e22013-04-17 20:18:07 -04003099 start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
James Smart737d4242013-04-17 20:14:49 -04003100 start_app_tag = src->app_tag;
James Smart737d4242013-04-17 20:14:49 -04003101 len = sgpe->length;
3102 while (src && protsegcnt) {
3103 while (len) {
3104
3105 /*
3106 * First check to see if a protection data
3107 * check is valid
3108 */
3109 if ((src->ref_tag == 0xffffffff) ||
3110 (src->app_tag == 0xffff)) {
3111 start_ref_tag++;
3112 goto skipit;
3113 }
3114
James Smart9c6aa9d2013-05-31 17:03:39 -04003115 /* First Guard Tag checking */
3116 if (chk_guard) {
3117 guard_tag = src->guard_tag;
3118 if (lpfc_cmd_guard_csum(cmd))
3119 sum = lpfc_bg_csum(data_src,
3120 blksize);
3121 else
3122 sum = lpfc_bg_crc(data_src,
3123 blksize);
3124 if ((guard_tag != sum)) {
3125 err_type = BGS_GUARD_ERR_MASK;
3126 goto out;
3127 }
James Smart737d4242013-04-17 20:14:49 -04003128 }
3129
3130 /* Reference Tag checking */
3131 ref_tag = be32_to_cpu(src->ref_tag);
3132 if (chk_ref && (ref_tag != start_ref_tag)) {
3133 err_type = BGS_REFTAG_ERR_MASK;
3134 goto out;
3135 }
3136 start_ref_tag++;
3137
James Smart9c6aa9d2013-05-31 17:03:39 -04003138 /* App Tag checking */
3139 app_tag = src->app_tag;
3140 if (chk_app && (app_tag != start_app_tag)) {
3141 err_type = BGS_APPTAG_ERR_MASK;
3142 goto out;
James Smart737d4242013-04-17 20:14:49 -04003143 }
3144skipit:
3145 len -= sizeof(struct scsi_dif_tuple);
3146 if (len < 0)
3147 len = 0;
3148 src++;
3149
3150 data_src += blksize;
3151 data_len -= blksize;
3152
3153 /*
3154 * Are we at the end of the Data segment?
3155 * The data segment is only used for Guard
3156 * tag checking.
3157 */
3158 if (chk_guard && (data_len == 0)) {
3159 chk_guard = 0;
3160 sgde = sg_next(sgde);
3161 if (!sgde)
3162 goto out;
3163
3164 data_src = (uint8_t *)sg_virt(sgde);
3165 data_len = sgde->length;
3166 if ((data_len & (blksize - 1)) == 0)
3167 chk_guard = 1;
3168 }
3169 }
3170
3171 /* Goto the next Protection data segment */
3172 sgpe = sg_next(sgpe);
3173 if (sgpe) {
3174 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
3175 len = sgpe->length;
3176 } else {
3177 src = NULL;
3178 }
3179 protsegcnt--;
3180 }
3181 }
3182out:
3183 if (err_type == BGS_GUARD_ERR_MASK) {
3184 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3185 0x10, 0x1);
3186 cmd->result = DRIVER_SENSE << 24
3187 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3188 phba->bg_guard_err_cnt++;
3189 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3190 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
3191 (unsigned long)scsi_get_lba(cmd),
3192 sum, guard_tag);
3193
3194 } else if (err_type == BGS_REFTAG_ERR_MASK) {
3195 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3196 0x10, 0x3);
3197 cmd->result = DRIVER_SENSE << 24
3198 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3199
3200 phba->bg_reftag_err_cnt++;
3201 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3202 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
3203 (unsigned long)scsi_get_lba(cmd),
3204 ref_tag, start_ref_tag);
3205
3206 } else if (err_type == BGS_APPTAG_ERR_MASK) {
3207 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3208 0x10, 0x2);
3209 cmd->result = DRIVER_SENSE << 24
3210 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3211
3212 phba->bg_apptag_err_cnt++;
3213 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3214 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
3215 (unsigned long)scsi_get_lba(cmd),
3216 app_tag, start_app_tag);
3217 }
3218}
3219
3220
3221/*
James Smarte2a0a9d2008-12-04 22:40:02 -05003222 * This function checks for BlockGuard errors detected by
3223 * the HBA. In case of errors, the ASC/ASCQ fields in the
3224 * sense buffer will be set accordingly, paired with
3225 * ILLEGAL_REQUEST to signal to the kernel that the HBA
3226 * detected corruption.
3227 *
3228 * Returns:
3229 * 0 - No error found
3230 * 1 - BlockGuard error found
3231 * -1 - Internal error (bad profile, ...etc)
3232 */
3233static int
3234lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
3235 struct lpfc_iocbq *pIocbOut)
3236{
3237 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3238 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
3239 int ret = 0;
3240 uint32_t bghm = bgf->bghm;
3241 uint32_t bgstat = bgf->bgstat;
3242 uint64_t failing_sector = 0;
3243
James Smarte2a0a9d2008-12-04 22:40:02 -05003244 spin_lock(&_dump_buf_lock);
3245 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04003246 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
3247 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05003248 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04003249 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003250
3251 /* If we have a prot sgl, save the DIF buffer */
3252 if (lpfc_prot_group_type(phba, cmd) ==
3253 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04003254 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
3255 "Saving DIF for %u blocks to debugfs\n",
3256 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
3257 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003258 }
3259
3260 _dump_buf_done = 1;
3261 }
3262 spin_unlock(&_dump_buf_lock);
3263
3264 if (lpfc_bgs_get_invalid_prof(bgstat)) {
3265 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart737d4242013-04-17 20:14:49 -04003266 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3267 "9072 BLKGRD: Invalid BG Profile in cmd"
3268 " 0x%x lba 0x%llx blk cnt 0x%x "
3269 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3270 (unsigned long long)scsi_get_lba(cmd),
3271 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003272 ret = (-1);
3273 goto out;
3274 }
3275
3276 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
3277 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart737d4242013-04-17 20:14:49 -04003278 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3279 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
3280 " 0x%x lba 0x%llx blk cnt 0x%x "
3281 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3282 (unsigned long long)scsi_get_lba(cmd),
3283 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003284 ret = (-1);
3285 goto out;
3286 }
3287
3288 if (lpfc_bgs_get_guard_err(bgstat)) {
3289 ret = 1;
3290
3291 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3292 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003293 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003294 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3295 phba->bg_guard_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003296 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3297 "9055 BLKGRD: Guard Tag error in cmd"
3298 " 0x%x lba 0x%llx blk cnt 0x%x "
3299 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3300 (unsigned long long)scsi_get_lba(cmd),
3301 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003302 }
3303
3304 if (lpfc_bgs_get_reftag_err(bgstat)) {
3305 ret = 1;
3306
3307 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3308 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003309 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003310 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3311
3312 phba->bg_reftag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003313 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3314 "9056 BLKGRD: Ref Tag error in cmd"
3315 " 0x%x lba 0x%llx blk cnt 0x%x "
3316 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3317 (unsigned long long)scsi_get_lba(cmd),
3318 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003319 }
3320
3321 if (lpfc_bgs_get_apptag_err(bgstat)) {
3322 ret = 1;
3323
3324 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3325 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003326 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003327 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3328
3329 phba->bg_apptag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003330 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3331 "9061 BLKGRD: App Tag error in cmd"
3332 " 0x%x lba 0x%llx blk cnt 0x%x "
3333 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3334 (unsigned long long)scsi_get_lba(cmd),
3335 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003336 }
3337
3338 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
3339 /*
3340 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04003341 * field, and put the failing LBA in it.
3342 * This code assumes there was also a guard/app/ref tag error
3343 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05003344 */
James Smart7c56b9f2011-07-22 18:36:25 -04003345 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
3346 cmd->sense_buffer[8] = 0; /* Information descriptor type */
3347 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
3348 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05003349
3350 /* bghm is a "on the wire" FC frame based count */
3351 switch (scsi_get_prot_op(cmd)) {
3352 case SCSI_PROT_READ_INSERT:
3353 case SCSI_PROT_WRITE_STRIP:
3354 bghm /= cmd->device->sector_size;
3355 break;
3356 case SCSI_PROT_READ_STRIP:
3357 case SCSI_PROT_WRITE_INSERT:
3358 case SCSI_PROT_READ_PASS:
3359 case SCSI_PROT_WRITE_PASS:
3360 bghm /= (cmd->device->sector_size +
3361 sizeof(struct scsi_dif_tuple));
3362 break;
3363 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003364
3365 failing_sector = scsi_get_lba(cmd);
3366 failing_sector += bghm;
3367
James Smart7c56b9f2011-07-22 18:36:25 -04003368 /* Descriptor Information */
3369 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05003370 }
3371
3372 if (!ret) {
3373 /* No error was reported - problem in FW? */
James Smart737d4242013-04-17 20:14:49 -04003374 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3375 "9057 BLKGRD: Unknown error in cmd"
3376 " 0x%x lba 0x%llx blk cnt 0x%x "
3377 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3378 (unsigned long long)scsi_get_lba(cmd),
3379 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003380
James Smart737d4242013-04-17 20:14:49 -04003381 /* Calcuate what type of error it was */
3382 lpfc_calc_bg_err(phba, lpfc_cmd);
3383 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003384out:
3385 return ret;
3386}
3387
James Smartea2151b2008-09-07 11:52:10 -04003388/**
James Smartda0436e2009-05-22 14:51:39 -04003389 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3390 * @phba: The Hba for which this call is being executed.
3391 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3392 *
3393 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3394 * field of @lpfc_cmd for device with SLI-4 interface spec.
3395 *
3396 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003397 * 1 - Error
3398 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04003399 **/
3400static int
3401lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3402{
3403 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3404 struct scatterlist *sgel = NULL;
3405 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3406 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
James Smartfedd3b72011-02-16 12:39:24 -05003407 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04003408 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3409 dma_addr_t physaddr;
3410 uint32_t num_bde = 0;
3411 uint32_t dma_len;
3412 uint32_t dma_offset = 0;
3413 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05003414 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04003415
3416 /*
3417 * There are three possibilities here - use scatter-gather segment, use
3418 * the single mapping, or neither. Start the lpfc command prep by
3419 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3420 * data bde entry.
3421 */
3422 if (scsi_sg_count(scsi_cmnd)) {
3423 /*
3424 * The driver stores the segment count returned from pci_map_sg
3425 * because this a count of dma-mappings used to map the use_sg
3426 * pages. They are not guaranteed to be the same for those
3427 * architectures that implement an IOMMU.
3428 */
3429
3430 nseg = scsi_dma_map(scsi_cmnd);
3431 if (unlikely(!nseg))
3432 return 1;
3433 sgl += 1;
3434 /* clear the last flag in the fcp_rsp map entry */
3435 sgl->word2 = le32_to_cpu(sgl->word2);
3436 bf_set(lpfc_sli4_sge_last, sgl, 0);
3437 sgl->word2 = cpu_to_le32(sgl->word2);
3438 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05003439 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04003440 lpfc_cmd->seg_cnt = nseg;
3441 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04003442 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3443 " %s: Too many sg segments from "
3444 "dma_map_sg. Config %d, seg_cnt %d\n",
3445 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04003446 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04003447 lpfc_cmd->seg_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04003448 scsi_dma_unmap(scsi_cmnd);
3449 return 1;
3450 }
3451
3452 /*
3453 * The driver established a maximum scatter-gather segment count
3454 * during probe that limits the number of sg elements in any
3455 * single scsi command. Just run through the seg_cnt and format
3456 * the sge's.
3457 * When using SLI-3 the driver will try to fit all the BDEs into
3458 * the IOCB. If it can't then the BDEs get added to a BPL as it
3459 * does for SLI-2 mode.
3460 */
3461 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3462 physaddr = sg_dma_address(sgel);
3463 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04003464 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3465 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04003466 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04003467 if ((num_bde + 1) == nseg)
3468 bf_set(lpfc_sli4_sge_last, sgl, 1);
3469 else
3470 bf_set(lpfc_sli4_sge_last, sgl, 0);
3471 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04003472 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04003473 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05003474 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04003475 dma_offset += dma_len;
3476 sgl++;
3477 }
James Smartfedd3b72011-02-16 12:39:24 -05003478 /* setup the performance hint (first data BDE) if enabled */
3479 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
3480 bde = (struct ulp_bde64 *)
3481 &(iocb_cmd->unsli3.sli3Words[5]);
3482 bde->addrLow = first_data_sgl->addr_lo;
3483 bde->addrHigh = first_data_sgl->addr_hi;
3484 bde->tus.f.bdeSize =
3485 le32_to_cpu(first_data_sgl->sge_len);
3486 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3487 bde->tus.w = cpu_to_le32(bde->tus.w);
3488 }
James Smartda0436e2009-05-22 14:51:39 -04003489 } else {
3490 sgl += 1;
3491 /* clear the last flag in the fcp_rsp map entry */
3492 sgl->word2 = le32_to_cpu(sgl->word2);
3493 bf_set(lpfc_sli4_sge_last, sgl, 1);
3494 sgl->word2 = cpu_to_le32(sgl->word2);
3495 }
3496
3497 /*
3498 * Finish initializing those IOCB fields that are dependent on the
3499 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3500 * explicitly reinitialized.
3501 * all iocb memory resources are reused.
3502 */
3503 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3504
3505 /*
3506 * Due to difference in data length between DIF/non-DIF paths,
3507 * we need to set word 4 of IOCB here
3508 */
3509 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
3510 return 0;
3511}
3512
3513/**
James Smartacd68592012-01-18 16:25:09 -05003514 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3515 * @phba: The Hba for which this call is being executed.
3516 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3517 *
3518 * This is the protection/DIF aware version of
3519 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3520 * two functions eventually, but for now, it's here
3521 **/
3522static int
3523lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3524 struct lpfc_scsi_buf *lpfc_cmd)
3525{
3526 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3527 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3528 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3529 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart96f70772013-04-17 20:16:15 -04003530 uint32_t num_sge = 0;
James Smartacd68592012-01-18 16:25:09 -05003531 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3532 int prot_group_type = 0;
3533 int fcpdl;
3534
3535 /*
3536 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
James Smart96f70772013-04-17 20:16:15 -04003537 * fcp_rsp regions to the first data sge entry
James Smartacd68592012-01-18 16:25:09 -05003538 */
3539 if (scsi_sg_count(scsi_cmnd)) {
3540 /*
3541 * The driver stores the segment count returned from pci_map_sg
3542 * because this a count of dma-mappings used to map the use_sg
3543 * pages. They are not guaranteed to be the same for those
3544 * architectures that implement an IOMMU.
3545 */
3546 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3547 scsi_sglist(scsi_cmnd),
3548 scsi_sg_count(scsi_cmnd), datadir);
3549 if (unlikely(!datasegcnt))
3550 return 1;
3551
3552 sgl += 1;
3553 /* clear the last flag in the fcp_rsp map entry */
3554 sgl->word2 = le32_to_cpu(sgl->word2);
3555 bf_set(lpfc_sli4_sge_last, sgl, 0);
3556 sgl->word2 = cpu_to_le32(sgl->word2);
3557
3558 sgl += 1;
3559 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04003560
3561 /* First check if data segment count from SCSI Layer is good */
3562 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
3563 goto err;
James Smartacd68592012-01-18 16:25:09 -05003564
3565 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3566
3567 switch (prot_group_type) {
3568 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04003569 /* Here we need to add a DISEED to the count */
3570 if ((lpfc_cmd->seg_cnt + 1) > phba->cfg_total_seg_cnt)
3571 goto err;
3572
3573 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003574 datasegcnt);
James Smart96f70772013-04-17 20:16:15 -04003575
James Smartacd68592012-01-18 16:25:09 -05003576 /* we should have 2 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003577 if (num_sge < 2)
James Smartacd68592012-01-18 16:25:09 -05003578 goto err;
3579 break;
James Smart96f70772013-04-17 20:16:15 -04003580
3581 case LPFC_PG_TYPE_DIF_BUF:
James Smartacd68592012-01-18 16:25:09 -05003582 /*
3583 * This type indicates that protection buffers are
3584 * passed to the driver, so that needs to be prepared
3585 * for DMA
3586 */
3587 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3588 scsi_prot_sglist(scsi_cmnd),
3589 scsi_prot_sg_count(scsi_cmnd), datadir);
3590 if (unlikely(!protsegcnt)) {
3591 scsi_dma_unmap(scsi_cmnd);
3592 return 1;
3593 }
3594
3595 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04003596 /*
3597 * There is a minimun of 3 SGEs used for every
3598 * protection data segment.
3599 */
3600 if ((lpfc_cmd->prot_seg_cnt * 3) >
3601 (phba->cfg_total_seg_cnt - 2))
3602 goto err;
James Smartacd68592012-01-18 16:25:09 -05003603
James Smart96f70772013-04-17 20:16:15 -04003604 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003605 datasegcnt, protsegcnt);
James Smart96f70772013-04-17 20:16:15 -04003606
James Smartacd68592012-01-18 16:25:09 -05003607 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003608 if ((num_sge < 3) ||
3609 (num_sge > phba->cfg_total_seg_cnt))
James Smartacd68592012-01-18 16:25:09 -05003610 goto err;
3611 break;
James Smart96f70772013-04-17 20:16:15 -04003612
James Smartacd68592012-01-18 16:25:09 -05003613 case LPFC_PG_TYPE_INVALID:
3614 default:
James Smart96f70772013-04-17 20:16:15 -04003615 scsi_dma_unmap(scsi_cmnd);
3616 lpfc_cmd->seg_cnt = 0;
3617
James Smartacd68592012-01-18 16:25:09 -05003618 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3619 "9083 Unexpected protection group %i\n",
3620 prot_group_type);
3621 return 1;
3622 }
3623 }
3624
James Smart8012cc32012-10-31 14:44:49 -04003625 switch (scsi_get_prot_op(scsi_cmnd)) {
3626 case SCSI_PROT_WRITE_STRIP:
3627 case SCSI_PROT_READ_STRIP:
3628 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3629 break;
3630 case SCSI_PROT_WRITE_INSERT:
3631 case SCSI_PROT_READ_INSERT:
3632 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3633 break;
3634 case SCSI_PROT_WRITE_PASS:
3635 case SCSI_PROT_READ_PASS:
3636 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3637 break;
3638 }
3639
James Smartacd68592012-01-18 16:25:09 -05003640 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smartacd68592012-01-18 16:25:09 -05003641 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3642
3643 /*
3644 * Due to difference in data length between DIF/non-DIF paths,
3645 * we need to set word 4 of IOCB here
3646 */
3647 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
James Smartacd68592012-01-18 16:25:09 -05003648
3649 return 0;
3650err:
James Smart96f70772013-04-17 20:16:15 -04003651 if (lpfc_cmd->seg_cnt)
3652 scsi_dma_unmap(scsi_cmnd);
3653 if (lpfc_cmd->prot_seg_cnt)
3654 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3655 scsi_prot_sg_count(scsi_cmnd),
3656 scsi_cmnd->sc_data_direction);
3657
James Smartacd68592012-01-18 16:25:09 -05003658 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04003659 "9084 Cannot setup S/G List for HBA"
3660 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3661 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3662 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3663 prot_group_type, num_sge);
3664
3665 lpfc_cmd->seg_cnt = 0;
3666 lpfc_cmd->prot_seg_cnt = 0;
James Smartacd68592012-01-18 16:25:09 -05003667 return 1;
3668}
3669
3670/**
James Smart3772a992009-05-22 14:50:54 -04003671 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3672 * @phba: The Hba for which this call is being executed.
3673 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3674 *
3675 * This routine wraps the actual DMA mapping function pointer from the
3676 * lpfc_hba struct.
3677 *
3678 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003679 * 1 - Error
3680 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003681 **/
3682static inline int
3683lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3684{
3685 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3686}
3687
3688/**
James Smartacd68592012-01-18 16:25:09 -05003689 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3690 * using BlockGuard.
3691 * @phba: The Hba for which this call is being executed.
3692 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3693 *
3694 * This routine wraps the actual DMA mapping function pointer from the
3695 * lpfc_hba struct.
3696 *
3697 * Return codes:
3698 * 1 - Error
3699 * 0 - Success
3700 **/
3701static inline int
3702lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3703{
3704 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3705}
3706
3707/**
James Smart3621a712009-04-06 18:47:14 -04003708 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003709 * @phba: Pointer to hba context object.
3710 * @vport: Pointer to vport object.
3711 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3712 * @rsp_iocb: Pointer to response iocb object which reported error.
3713 *
3714 * This function posts an event when there is a SCSI command reporting
3715 * error from the scsi device.
3716 **/
3717static void
3718lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3719 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3720 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3721 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3722 uint32_t resp_info = fcprsp->rspStatus2;
3723 uint32_t scsi_status = fcprsp->rspStatus3;
3724 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3725 struct lpfc_fast_path_event *fast_path_evt = NULL;
3726 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3727 unsigned long flags;
3728
James Smart5989b8d2010-10-22 11:06:56 -04003729 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3730 return;
3731
James Smartea2151b2008-09-07 11:52:10 -04003732 /* If there is queuefull or busy condition send a scsi event */
3733 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3734 (cmnd->result == SAM_STAT_BUSY)) {
3735 fast_path_evt = lpfc_alloc_fast_evt(phba);
3736 if (!fast_path_evt)
3737 return;
3738 fast_path_evt->un.scsi_evt.event_type =
3739 FC_REG_SCSI_EVENT;
3740 fast_path_evt->un.scsi_evt.subcategory =
3741 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3742 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3743 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3744 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3745 &pnode->nlp_portname, sizeof(struct lpfc_name));
3746 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3747 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3748 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3749 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3750 fast_path_evt = lpfc_alloc_fast_evt(phba);
3751 if (!fast_path_evt)
3752 return;
3753 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3754 FC_REG_SCSI_EVENT;
3755 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3756 LPFC_EVENT_CHECK_COND;
3757 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3758 cmnd->device->lun;
3759 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3760 &pnode->nlp_portname, sizeof(struct lpfc_name));
3761 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3762 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3763 fast_path_evt->un.check_cond_evt.sense_key =
3764 cmnd->sense_buffer[2] & 0xf;
3765 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3766 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3767 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3768 fcpi_parm &&
3769 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3770 ((scsi_status == SAM_STAT_GOOD) &&
3771 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3772 /*
3773 * If status is good or resid does not match with fcp_param and
3774 * there is valid fcpi_parm, then there is a read_check error
3775 */
3776 fast_path_evt = lpfc_alloc_fast_evt(phba);
3777 if (!fast_path_evt)
3778 return;
3779 fast_path_evt->un.read_check_error.header.event_type =
3780 FC_REG_FABRIC_EVENT;
3781 fast_path_evt->un.read_check_error.header.subcategory =
3782 LPFC_EVENT_FCPRDCHKERR;
3783 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3784 &pnode->nlp_portname, sizeof(struct lpfc_name));
3785 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3786 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3787 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3788 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3789 fast_path_evt->un.read_check_error.fcpiparam =
3790 fcpi_parm;
3791 } else
3792 return;
3793
3794 fast_path_evt->vport = vport;
3795 spin_lock_irqsave(&phba->hbalock, flags);
3796 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3797 spin_unlock_irqrestore(&phba->hbalock, flags);
3798 lpfc_worker_wake_up(phba);
3799 return;
3800}
James Smart9bad7672008-12-04 22:39:02 -05003801
3802/**
James Smartf1126682009-06-10 17:22:44 -04003803 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003804 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003805 * @psb: The scsi buffer which is going to be un-mapped.
3806 *
3807 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003808 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003809 **/
dea31012005-04-17 16:05:31 -05003810static void
James Smartf1126682009-06-10 17:22:44 -04003811lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003812{
3813 /*
3814 * There are only two special cases to consider. (1) the scsi command
3815 * requested scatter-gather usage or (2) the scsi command allocated
3816 * a request buffer, but did not request use_sg. There is a third
3817 * case, but it does not require resource deallocation.
3818 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003819 if (psb->seg_cnt > 0)
3820 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003821 if (psb->prot_seg_cnt > 0)
3822 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3823 scsi_prot_sg_count(psb->pCmd),
3824 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003825}
3826
James Smart9bad7672008-12-04 22:39:02 -05003827/**
James Smart3621a712009-04-06 18:47:14 -04003828 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003829 * @vport: The virtual port for which this call is being executed.
3830 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3831 * @rsp_iocb: The response IOCB which contains FCP error.
3832 *
3833 * This routine is called to process response IOCB with status field
3834 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3835 * based upon SCSI and FCP error.
3836 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003837static void
James Smart2e0fef82007-06-17 19:56:36 -05003838lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3839 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003840{
3841 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3842 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3843 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003844 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003845 uint32_t resp_info = fcprsp->rspStatus2;
3846 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003847 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003848 uint32_t host_status = DID_OK;
3849 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05003850 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003851
James Smartea2151b2008-09-07 11:52:10 -04003852
dea31012005-04-17 16:05:31 -05003853 /*
3854 * If this is a task management command, there is no
3855 * scsi packet associated with this lpfc_cmd. The driver
3856 * consumes it.
3857 */
3858 if (fcpcmd->fcpCntl2) {
3859 scsi_status = 0;
3860 goto out;
3861 }
3862
James Smart6a9c52c2009-10-02 15:16:51 -04003863 if (resp_info & RSP_LEN_VALID) {
3864 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003865 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04003866 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3867 "2719 Invalid response length: "
3868 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
3869 cmnd->device->id,
3870 cmnd->device->lun, cmnd->cmnd[0],
3871 rsplen);
3872 host_status = DID_ERROR;
3873 goto out;
3874 }
James Smarte40a02c2010-02-26 14:13:54 -05003875 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3876 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3877 "2757 Protocol failure detected during "
3878 "processing of FCP I/O op: "
3879 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
3880 cmnd->device->id,
3881 cmnd->device->lun, cmnd->cmnd[0],
3882 fcprsp->rspInfo3);
3883 host_status = DID_ERROR;
3884 goto out;
3885 }
James Smart6a9c52c2009-10-02 15:16:51 -04003886 }
3887
James Smartc7743952006-12-02 13:34:42 -05003888 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3889 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3890 if (snslen > SCSI_SENSE_BUFFERSIZE)
3891 snslen = SCSI_SENSE_BUFFERSIZE;
3892
3893 if (resp_info & RSP_LEN_VALID)
3894 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3895 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3896 }
3897 lp = (uint32_t *)cmnd->sense_buffer;
3898
James Smartaa1c7ee2012-08-14 14:26:06 -04003899 /* special handling for under run conditions */
3900 if (!scsi_status && (resp_info & RESID_UNDER)) {
3901 /* don't log under runs if fcp set... */
3902 if (vport->cfg_log_verbose & LOG_FCP)
3903 logit = LOG_FCP_ERROR;
3904 /* unless operator says so */
3905 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3906 logit = LOG_FCP_UNDER;
3907 }
James Smartc7743952006-12-02 13:34:42 -05003908
James Smarte8b62012007-08-02 11:10:09 -04003909 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003910 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003911 "Data: x%x x%x x%x x%x x%x\n",
3912 cmnd->cmnd[0], scsi_status,
3913 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3914 be32_to_cpu(fcprsp->rspResId),
3915 be32_to_cpu(fcprsp->rspSnsLen),
3916 be32_to_cpu(fcprsp->rspRspLen),
3917 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003918
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003919 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05003920 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003921 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003922
James Smart73d91e52011-10-10 21:32:10 -04003923 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smarte2a0a9d2008-12-04 22:40:02 -05003924 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003925 "residual %d Data: x%x x%x x%x\n",
3926 be32_to_cpu(fcpcmd->fcpDl),
3927 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3928 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003929
3930 /*
James Smart7054a602007-04-25 09:52:34 -04003931 * If there is an under run check if under run reported by
3932 * storage array is same as the under run reported by HBA.
3933 * If this is not same, there is a dropped frame.
3934 */
3935 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3936 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003937 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003938 lpfc_printf_vlog(vport, KERN_WARNING,
3939 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003940 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003941 "and Underrun Data: x%x x%x x%x x%x\n",
3942 be32_to_cpu(fcpcmd->fcpDl),
3943 scsi_get_resid(cmnd), fcpi_parm,
3944 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003945 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003946 host_status = DID_ERROR;
3947 }
3948 /*
dea31012005-04-17 16:05:31 -05003949 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003950 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003951 * is not present, make sure the actual amount transferred is at
3952 * least the underflow value or fail.
3953 */
3954 if (!(resp_info & SNS_LEN_VALID) &&
3955 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003956 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3957 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003958 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003959 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003960 "underrun converted to error "
3961 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003962 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003963 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003964 host_status = DID_ERROR;
3965 }
3966 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003967 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003968 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003969 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003970 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003971 host_status = DID_ERROR;
3972
3973 /*
3974 * Check SLI validation that all the transfer was actually done
James Smart26373d22013-09-06 12:19:17 -04003975 * (fcpi_parm should be zero). Apply check only to reads.
dea31012005-04-17 16:05:31 -05003976 */
James Smart26373d22013-09-06 12:19:17 -04003977 } else if (fcpi_parm && (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -04003978 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart26373d22013-09-06 12:19:17 -04003979 "9029 FCP Read Check Error Data: "
James Smarteee88772010-09-29 11:19:08 -04003980 "x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003981 be32_to_cpu(fcpcmd->fcpDl),
3982 be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04003983 fcpi_parm, cmnd->cmnd[0], scsi_status);
3984 switch (scsi_status) {
3985 case SAM_STAT_GOOD:
3986 case SAM_STAT_CHECK_CONDITION:
3987 /* Fabric dropped a data frame. Fail any successful
3988 * command in which we detected dropped frames.
3989 * A status of good or some check conditions could
3990 * be considered a successful command.
3991 */
3992 host_status = DID_ERROR;
3993 break;
3994 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003995 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05003996 }
3997
3998 out:
3999 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04004000 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05004001}
4002
James Smart9bad7672008-12-04 22:39:02 -05004003/**
James Smart3621a712009-04-06 18:47:14 -04004004 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05004005 * @phba: The Hba for which this call is being executed.
4006 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04004007 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05004008 *
4009 * This routine assigns scsi command result by looking into response IOCB
4010 * status field appropriately. This routine handles QUEUE FULL condition as
4011 * well by ramping down device queue depth.
4012 **/
dea31012005-04-17 16:05:31 -05004013static void
4014lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
4015 struct lpfc_iocbq *pIocbOut)
4016{
4017 struct lpfc_scsi_buf *lpfc_cmd =
4018 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05004019 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05004020 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
4021 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04004022 struct scsi_cmnd *cmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004023 int result;
James Smarta257bf92009-04-06 18:48:10 -04004024 struct scsi_device *tmp_sdev;
James Smart5ffc2662009-11-18 15:39:44 -05004025 int depth;
James Smartfa61a542008-01-11 01:52:42 -05004026 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04004027 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04004028 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04004029 uint32_t queue_depth, scsi_id;
James Smart73d91e52011-10-10 21:32:10 -04004030 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05004031
James Smart75baf692010-06-08 18:31:21 -04004032 /* Sanity check on return of outstanding command */
4033 if (!(lpfc_cmd->pCmd))
4034 return;
4035 cmd = lpfc_cmd->pCmd;
4036 shost = cmd->device->host;
4037
James Smarte3d2b802012-08-14 14:25:43 -04004038 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
dea31012005-04-17 16:05:31 -05004039 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05004040 /* pick up SLI4 exhange busy status from HBA */
4041 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
4042
James Smart9a6b09c2012-03-01 22:37:42 -05004043#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4044 if (lpfc_cmd->prot_data_type) {
4045 struct scsi_dif_tuple *src = NULL;
4046
4047 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
4048 /*
4049 * Used to restore any changes to protection
4050 * data for error injection.
4051 */
4052 switch (lpfc_cmd->prot_data_type) {
4053 case LPFC_INJERR_REFTAG:
4054 src->ref_tag =
4055 lpfc_cmd->prot_data;
4056 break;
4057 case LPFC_INJERR_APPTAG:
4058 src->app_tag =
4059 (uint16_t)lpfc_cmd->prot_data;
4060 break;
4061 case LPFC_INJERR_GUARD:
4062 src->guard_tag =
4063 (uint16_t)lpfc_cmd->prot_data;
4064 break;
4065 default:
4066 break;
4067 }
4068
4069 lpfc_cmd->prot_data = 0;
4070 lpfc_cmd->prot_data_type = 0;
4071 lpfc_cmd->prot_data_segment = NULL;
4072 }
4073#endif
James Smart109f6ed2008-12-04 22:39:08 -05004074 if (pnode && NLP_CHK_NODE_ACT(pnode))
4075 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05004076
4077 if (lpfc_cmd->status) {
4078 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
4079 (lpfc_cmd->result & IOERR_DRVR_MASK))
4080 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4081 else if (lpfc_cmd->status >= IOSTAT_CNT)
4082 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smartaa1c7ee2012-08-14 14:26:06 -04004083 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
4084 !lpfc_cmd->fcp_rsp->rspStatus3 &&
4085 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
4086 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
James Smart73d91e52011-10-10 21:32:10 -04004087 logit = 0;
4088 else
4089 logit = LOG_FCP | LOG_FCP_UNDER;
4090 lpfc_printf_vlog(vport, KERN_WARNING, logit,
4091 "9030 FCP cmd x%x failed <%d/%d> "
James Smart5a0d80f2012-05-09 21:18:20 -04004092 "status: x%x result: x%x "
4093 "sid: x%x did: x%x oxid: x%x "
4094 "Data: x%x x%x\n",
James Smart73d91e52011-10-10 21:32:10 -04004095 cmd->cmnd[0],
4096 cmd->device ? cmd->device->id : 0xffff,
4097 cmd->device ? cmd->device->lun : 0xffff,
4098 lpfc_cmd->status, lpfc_cmd->result,
James Smart3bf41ba2013-05-31 17:03:18 -04004099 vport->fc_myDID,
4100 (pnode) ? pnode->nlp_DID : 0,
James Smart5a0d80f2012-05-09 21:18:20 -04004101 phba->sli_rev == LPFC_SLI_REV4 ?
4102 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
James Smart73d91e52011-10-10 21:32:10 -04004103 pIocbOut->iocb.ulpContext,
4104 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05004105
4106 switch (lpfc_cmd->status) {
4107 case IOSTAT_FCP_RSP_ERROR:
4108 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05004109 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05004110 break;
4111 case IOSTAT_NPORT_BSY:
4112 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04004113 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04004114 fast_path_evt = lpfc_alloc_fast_evt(phba);
4115 if (!fast_path_evt)
4116 break;
4117 fast_path_evt->un.fabric_evt.event_type =
4118 FC_REG_FABRIC_EVENT;
4119 fast_path_evt->un.fabric_evt.subcategory =
4120 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
4121 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
4122 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4123 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
4124 &pnode->nlp_portname,
4125 sizeof(struct lpfc_name));
4126 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
4127 &pnode->nlp_nodename,
4128 sizeof(struct lpfc_name));
4129 }
4130 fast_path_evt->vport = vport;
4131 fast_path_evt->work_evt.evt =
4132 LPFC_EVT_FASTPATH_MGMT_EVT;
4133 spin_lock_irqsave(&phba->hbalock, flags);
4134 list_add_tail(&fast_path_evt->work_evt.evt_listp,
4135 &phba->work_list);
4136 spin_unlock_irqrestore(&phba->hbalock, flags);
4137 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004138 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004139 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05004140 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05004141 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
4142 lpfc_cmd->result ==
4143 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
4144 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
4145 lpfc_cmd->result ==
4146 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
4147 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
4148 break;
4149 }
James Smartd7c255b2008-08-24 21:50:00 -04004150 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05004151 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04004152 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
4153 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
James Smart92d7f7b2007-06-17 19:56:38 -05004154 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04004155 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05004156 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004157 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
4158 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
4159 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
4160 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
4161 /*
4162 * This is a response for a BG enabled
4163 * cmd. Parse BG error
4164 */
4165 lpfc_parse_bg_err(phba, lpfc_cmd,
4166 pIocbOut);
4167 break;
4168 } else {
4169 lpfc_printf_vlog(vport, KERN_WARNING,
4170 LOG_BG,
4171 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04004172 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05004173 }
4174 }
James Smart1151e3e2011-02-16 12:39:35 -05004175 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
4176 && (phba->sli_rev == LPFC_SLI_REV4)
4177 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
4178 /* This IO was aborted by the target, we don't
4179 * know the rxid and because we did not send the
4180 * ABTS we cannot generate and RRQ.
4181 */
4182 lpfc_set_rrq_active(phba, pnode,
James Smartee0f4fe2012-05-09 21:19:14 -04004183 lpfc_cmd->cur_iocbq.sli4_lxritag,
4184 0, 0);
James Smart1151e3e2011-02-16 12:39:35 -05004185 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004186 /* else: fall through */
dea31012005-04-17 16:05:31 -05004187 default:
4188 cmd->result = ScsiResult(DID_ERROR, 0);
4189 break;
4190 }
4191
James Smart58da1ff2008-04-07 10:15:56 -04004192 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004193 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04004194 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
4195 SAM_STAT_BUSY);
James Smartab56dc22011-02-16 12:39:57 -05004196 } else
dea31012005-04-17 16:05:31 -05004197 cmd->result = ScsiResult(DID_OK, 0);
dea31012005-04-17 16:05:31 -05004198
4199 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4200 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
4201
James Smarte8b62012007-08-02 11:10:09 -04004202 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4203 "0710 Iodone <%d/%d> cmd %p, error "
4204 "x%x SNS x%x x%x Data: x%x x%x\n",
4205 cmd->device->id, cmd->device->lun, cmd,
4206 cmd->result, *lp, *(lp + 3), cmd->retries,
4207 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05004208 }
4209
James Smartea2151b2008-09-07 11:52:10 -04004210 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004211 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04004212 if (vport->cfg_max_scsicmpl_time &&
4213 time_after(jiffies, lpfc_cmd->start_time +
4214 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04004215 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004216 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4217 if (pnode->cmd_qdepth >
4218 atomic_read(&pnode->cmd_pending) &&
4219 (atomic_read(&pnode->cmd_pending) >
4220 LPFC_MIN_TGT_QDEPTH) &&
4221 ((cmd->cmnd[0] == READ_10) ||
4222 (cmd->cmnd[0] == WRITE_10)))
4223 pnode->cmd_qdepth =
4224 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04004225
James Smart109f6ed2008-12-04 22:39:08 -05004226 pnode->last_change_time = jiffies;
4227 }
James Smarta257bf92009-04-06 18:48:10 -04004228 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004229 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smart7dc517d2010-07-14 15:32:10 -04004230 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
James Smart977b5a02008-09-07 11:52:04 -04004231 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05004232 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04004233 spin_lock_irqsave(shost->host_lock, flags);
James Smart7dc517d2010-07-14 15:32:10 -04004234 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
4235 / 100;
4236 depth = depth ? depth : 1;
4237 pnode->cmd_qdepth += depth;
4238 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
4239 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05004240 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04004241 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004242 }
James Smart977b5a02008-09-07 11:52:04 -04004243 }
4244
James Smart1dcb58e2007-04-25 09:51:30 -04004245 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04004246
4247 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
4248 queue_depth = cmd->device->queue_depth;
4249 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05004250 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004251
Jamie Wellnitzb8086082006-02-28 22:33:12 -05004252 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart876dd7d2012-09-29 11:31:28 -04004253 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004254 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04004255 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004256
James Smartfa61a542008-01-11 01:52:42 -05004257 /*
4258 * If there is a thread waiting for command completion
4259 * wake up the thread.
4260 */
James Smarta257bf92009-04-06 18:48:10 -04004261 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004262 if (lpfc_cmd->waitq)
4263 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04004264 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05004265 lpfc_release_scsi_buf(phba, lpfc_cmd);
4266 return;
4267 }
4268
James Smart92d7f7b2007-06-17 19:56:38 -05004269 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04004270 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05004271
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004272 /*
4273 * Check for queue full. If the lun is reporting queue full, then
4274 * back off the lun queue depth to prevent target overloads.
4275 */
James Smart58da1ff2008-04-07 10:15:56 -04004276 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
4277 NLP_CHK_NODE_ACT(pnode)) {
James Smarta257bf92009-04-06 18:48:10 -04004278 shost_for_each_device(tmp_sdev, shost) {
4279 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004280 continue;
4281 depth = scsi_track_queue_full(tmp_sdev,
James Smart5ffc2662009-11-18 15:39:44 -05004282 tmp_sdev->queue_depth-1);
4283 if (depth <= 0)
4284 continue;
James Smarte8b62012007-08-02 11:10:09 -04004285 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4286 "0711 detected queue full - lun queue "
4287 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04004288 lpfc_send_sdev_queuedepth_change_event(phba, vport,
James Smart5ffc2662009-11-18 15:39:44 -05004289 pnode,
4290 tmp_sdev->lun,
4291 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004292 }
4293 }
4294
James Smart876dd7d2012-09-29 11:31:28 -04004295 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004296 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04004297 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004298
James Smartfa61a542008-01-11 01:52:42 -05004299 /*
4300 * If there is a thread waiting for command completion
4301 * wake up the thread.
4302 */
James Smarta257bf92009-04-06 18:48:10 -04004303 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004304 if (lpfc_cmd->waitq)
4305 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04004306 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004307
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004308 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004309}
4310
James Smart34b02dc2008-08-24 21:49:55 -04004311/**
James Smart3621a712009-04-06 18:47:14 -04004312 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04004313 * @data: A pointer to the immediate command data portion of the IOCB.
4314 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
4315 *
4316 * The routine copies the entire FCP command from @fcp_cmnd to @data while
4317 * byte swapping the data to big endian format for transmission on the wire.
4318 **/
4319static void
4320lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
4321{
4322 int i, j;
4323 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
4324 i += sizeof(uint32_t), j++) {
4325 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
4326 }
4327}
4328
James Smart9bad7672008-12-04 22:39:02 -05004329/**
James Smartf1126682009-06-10 17:22:44 -04004330 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004331 * @vport: The virtual port for which this call is being executed.
4332 * @lpfc_cmd: The scsi command which needs to send.
4333 * @pnode: Pointer to lpfc_nodelist.
4334 *
4335 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04004336 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004337 **/
dea31012005-04-17 16:05:31 -05004338static void
James Smartf1126682009-06-10 17:22:44 -04004339lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05004340 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05004341{
James Smart2e0fef82007-06-17 19:56:36 -05004342 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004343 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4344 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4345 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4346 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
4347 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04004348 char tag[2];
James Smart027140e2012-08-03 12:35:44 -04004349 uint8_t *ptr;
4350 bool sli4;
James Smart98bbf5f2013-09-06 12:18:45 -04004351 uint32_t fcpdl;
dea31012005-04-17 16:05:31 -05004352
James Smart58da1ff2008-04-07 10:15:56 -04004353 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4354 return;
4355
dea31012005-04-17 16:05:31 -05004356 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04004357 /* clear task management bits */
4358 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05004359
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04004360 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4361 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004362
James Smart027140e2012-08-03 12:35:44 -04004363 ptr = &fcp_cmnd->fcpCdb[0];
4364 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4365 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4366 ptr += scsi_cmnd->cmd_len;
4367 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4368 }
4369
James Smart7e2b19f2007-10-29 11:00:39 -04004370 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
4371 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05004372 case HEAD_OF_QUEUE_TAG:
4373 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
4374 break;
4375 case ORDERED_QUEUE_TAG:
4376 fcp_cmnd->fcpCntl1 = ORDERED_Q;
4377 break;
4378 default:
4379 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
4380 break;
4381 }
4382 } else
James Smartfe8f7f92013-01-03 15:43:03 -05004383 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
dea31012005-04-17 16:05:31 -05004384
James Smart027140e2012-08-03 12:35:44 -04004385 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
4386
dea31012005-04-17 16:05:31 -05004387 /*
4388 * There are three possibilities here - use scatter-gather segment, use
4389 * the single mapping, or neither. Start the lpfc command prep by
4390 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4391 * data bde entry.
4392 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05004393 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05004394 if (datadir == DMA_TO_DEVICE) {
4395 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart182ba752013-07-15 18:34:05 -04004396 iocb_cmd->ulpPU = PARM_READ_CHECK;
James Smart3cb01c52013-07-15 18:35:04 -04004397 if (vport->cfg_first_burst_size &&
4398 (pnode->nlp_flag & NLP_FIRSTBURST)) {
James Smart98bbf5f2013-09-06 12:18:45 -04004399 fcpdl = scsi_bufflen(scsi_cmnd);
4400 if (fcpdl < vport->cfg_first_burst_size)
4401 piocbq->iocb.un.fcpi.fcpi_XRdy = fcpdl;
4402 else
4403 piocbq->iocb.un.fcpi.fcpi_XRdy =
4404 vport->cfg_first_burst_size;
James Smart3cb01c52013-07-15 18:35:04 -04004405 }
dea31012005-04-17 16:05:31 -05004406 fcp_cmnd->fcpCntl3 = WRITE_DATA;
4407 phba->fc4OutputRequests++;
4408 } else {
4409 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4410 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05004411 fcp_cmnd->fcpCntl3 = READ_DATA;
4412 phba->fc4InputRequests++;
4413 }
4414 } else {
4415 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4416 iocb_cmd->un.fcpi.fcpi_parm = 0;
4417 iocb_cmd->ulpPU = 0;
4418 fcp_cmnd->fcpCntl3 = 0;
4419 phba->fc4ControlRequests++;
4420 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004421 if (phba->sli_rev == 3 &&
4422 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004423 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004424 /*
4425 * Finish initializing those IOCB fields that are independent
4426 * of the scsi_cmnd request_buffer
4427 */
4428 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart027140e2012-08-03 12:35:44 -04004429 if (sli4)
James Smart6d368e52011-05-24 11:44:12 -04004430 piocbq->iocb.ulpContext =
4431 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05004432 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4433 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05004434 else
4435 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05004436
4437 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4438 piocbq->context1 = lpfc_cmd;
4439 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4440 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05004441 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05004442}
4443
James Smart9bad7672008-12-04 22:39:02 -05004444/**
James Smart6d368e52011-05-24 11:44:12 -04004445 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004446 * @vport: The virtual port for which this call is being executed.
4447 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4448 * @lun: Logical unit number.
4449 * @task_mgmt_cmd: SCSI task management command.
4450 *
James Smart3772a992009-05-22 14:50:54 -04004451 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4452 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004453 *
4454 * Return codes:
4455 * 0 - Error
4456 * 1 - Success
4457 **/
dea31012005-04-17 16:05:31 -05004458static int
James Smartf1126682009-06-10 17:22:44 -04004459lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05004460 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04004461 unsigned int lun,
dea31012005-04-17 16:05:31 -05004462 uint8_t task_mgmt_cmd)
4463{
dea31012005-04-17 16:05:31 -05004464 struct lpfc_iocbq *piocbq;
4465 IOCB_t *piocb;
4466 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04004467 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05004468 struct lpfc_nodelist *ndlp = rdata->pnode;
4469
James Smart58da1ff2008-04-07 10:15:56 -04004470 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4471 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05004472 return 0;
dea31012005-04-17 16:05:31 -05004473
dea31012005-04-17 16:05:31 -05004474 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004475 piocbq->vport = vport;
4476
dea31012005-04-17 16:05:31 -05004477 piocb = &piocbq->iocb;
4478
4479 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04004480 /* Clear out any old data in the FCP command area */
4481 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4482 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004483 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05004484 if (vport->phba->sli_rev == 3 &&
4485 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004486 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004487 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05004488 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04004489 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4490 piocb->ulpContext =
4491 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4492 }
dea31012005-04-17 16:05:31 -05004493 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
4494 piocb->ulpFCP2Rcvy = 1;
4495 }
4496 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
4497
4498 /* ulpTimeout is only one byte */
4499 if (lpfc_cmd->timeout > 0xff) {
4500 /*
4501 * Do not timeout the command at the firmware level.
4502 * The driver will provide the timeout mechanism.
4503 */
4504 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04004505 } else
dea31012005-04-17 16:05:31 -05004506 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04004507
4508 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4509 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004510
James Smart2e0fef82007-06-17 19:56:36 -05004511 return 1;
dea31012005-04-17 16:05:31 -05004512}
4513
James Smart9bad7672008-12-04 22:39:02 -05004514/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004515 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04004516 * @phba: The hba struct for which this call is being executed.
4517 * @dev_grp: The HBA PCI-Device group number.
4518 *
4519 * This routine sets up the SCSI interface API function jump table in @phba
4520 * struct.
4521 * Returns: 0 - success, -ENODEV - failure.
4522 **/
4523int
4524lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4525{
4526
James Smartf1126682009-06-10 17:22:44 -04004527 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4528 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04004529
James Smart3772a992009-05-22 14:50:54 -04004530 switch (dev_grp) {
4531 case LPFC_PCI_DEV_LP:
4532 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4533 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05004534 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004535 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05004536 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004537 break;
James Smartda0436e2009-05-22 14:51:39 -04004538 case LPFC_PCI_DEV_OC:
4539 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4540 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05004541 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004542 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05004543 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004544 break;
James Smart3772a992009-05-22 14:50:54 -04004545 default:
4546 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4547 "1418 Invalid HBA PCI-device group: 0x%x\n",
4548 dev_grp);
4549 return -ENODEV;
4550 break;
4551 }
James Smart3772a992009-05-22 14:50:54 -04004552 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05004553 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04004554 return 0;
4555}
4556
4557/**
James Smart3621a712009-04-06 18:47:14 -04004558 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05004559 * @phba: The Hba for which this call is being executed.
4560 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4561 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4562 *
4563 * This routine is IOCB completion routine for device reset and target reset
4564 * routine. This routine release scsi buffer associated with lpfc_cmd.
4565 **/
James Smart7054a602007-04-25 09:52:34 -04004566static void
4567lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4568 struct lpfc_iocbq *cmdiocbq,
4569 struct lpfc_iocbq *rspiocbq)
4570{
4571 struct lpfc_scsi_buf *lpfc_cmd =
4572 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4573 if (lpfc_cmd)
4574 lpfc_release_scsi_buf(phba, lpfc_cmd);
4575 return;
4576}
4577
James Smart9bad7672008-12-04 22:39:02 -05004578/**
James Smart3621a712009-04-06 18:47:14 -04004579 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05004580 * @host: The scsi host for which this call is being executed.
4581 *
4582 * This routine provides module information about hba.
4583 *
4584 * Reutrn code:
4585 * Pointer to char - Success.
4586 **/
dea31012005-04-17 16:05:31 -05004587const char *
4588lpfc_info(struct Scsi_Host *host)
4589{
James Smart2e0fef82007-06-17 19:56:36 -05004590 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4591 struct lpfc_hba *phba = vport->phba;
James Smart8b68cd52012-09-29 11:32:37 -04004592 int len, link_speed = 0;
dea31012005-04-17 16:05:31 -05004593 static char lpfcinfobuf[384];
4594
4595 memset(lpfcinfobuf,0,384);
4596 if (phba && phba->pcidev){
4597 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4598 len = strlen(lpfcinfobuf);
4599 snprintf(lpfcinfobuf + len,
4600 384-len,
4601 " on PCI bus %02x device %02x irq %d",
4602 phba->pcidev->bus->number,
4603 phba->pcidev->devfn,
4604 phba->pcidev->irq);
4605 len = strlen(lpfcinfobuf);
4606 if (phba->Port[0]) {
4607 snprintf(lpfcinfobuf + len,
4608 384-len,
4609 " port %s",
4610 phba->Port);
4611 }
James Smart65467b62010-01-26 23:08:29 -05004612 len = strlen(lpfcinfobuf);
James Smart8b68cd52012-09-29 11:32:37 -04004613 if (phba->sli_rev <= LPFC_SLI_REV3) {
4614 link_speed = lpfc_sli_port_speed_get(phba);
4615 } else {
4616 if (phba->sli4_hba.link_state.logical_speed)
4617 link_speed =
4618 phba->sli4_hba.link_state.logical_speed;
4619 else
4620 link_speed = phba->sli4_hba.link_state.speed;
James Smart65467b62010-01-26 23:08:29 -05004621 }
James Smart8b68cd52012-09-29 11:32:37 -04004622 if (link_speed != 0)
4623 snprintf(lpfcinfobuf + len, 384-len,
4624 " Logical Link Speed: %d Mbps", link_speed);
dea31012005-04-17 16:05:31 -05004625 }
4626 return lpfcinfobuf;
4627}
4628
James Smart9bad7672008-12-04 22:39:02 -05004629/**
James Smart3621a712009-04-06 18:47:14 -04004630 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05004631 * @phba: The Hba for which this call is being executed.
4632 *
4633 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4634 * The default value of cfg_poll_tmo is 10 milliseconds.
4635 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004636static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4637{
4638 unsigned long poll_tmo_expires =
4639 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4640
James Smart0e9bb8d2013-03-01 16:35:12 -05004641 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004642 mod_timer(&phba->fcp_poll_timer,
4643 poll_tmo_expires);
4644}
4645
James Smart9bad7672008-12-04 22:39:02 -05004646/**
James Smart3621a712009-04-06 18:47:14 -04004647 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004648 * @phba: The Hba for which this call is being executed.
4649 *
4650 * This routine starts the fcp_poll_timer of @phba.
4651 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004652void lpfc_poll_start_timer(struct lpfc_hba * phba)
4653{
4654 lpfc_poll_rearm_timer(phba);
4655}
4656
James Smart9bad7672008-12-04 22:39:02 -05004657/**
James Smart3621a712009-04-06 18:47:14 -04004658 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05004659 * @ptr: Map to lpfc_hba data structure pointer.
4660 *
4661 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4662 * and FCP Ring interrupt is disable.
4663 **/
4664
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004665void lpfc_poll_timeout(unsigned long ptr)
4666{
James Smart2e0fef82007-06-17 19:56:36 -05004667 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004668
4669 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004670 lpfc_sli_handle_fast_ring_event(phba,
4671 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4672
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004673 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4674 lpfc_poll_rearm_timer(phba);
4675 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004676}
4677
James Smart9bad7672008-12-04 22:39:02 -05004678/**
James Smart3621a712009-04-06 18:47:14 -04004679 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05004680 * @cmnd: Pointer to scsi_cmnd data structure.
4681 * @done: Pointer to done routine.
4682 *
4683 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4684 * This routine prepares an IOCB from scsi command and provides to firmware.
4685 * The @done callback is invoked after driver finished processing the command.
4686 *
4687 * Return value :
4688 * 0 - Success
4689 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4690 **/
dea31012005-04-17 16:05:31 -05004691static int
James Smartb9a7c632012-08-03 12:35:24 -04004692lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004693{
James Smart2e0fef82007-06-17 19:56:36 -05004694 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4695 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004696 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004697 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004698 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004699 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004700 int err;
dea31012005-04-17 16:05:31 -05004701
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004702 err = fc_remote_port_chkready(rport);
4703 if (err) {
4704 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004705 goto out_fail_command;
4706 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004707 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004708
James Smartbf086112011-08-21 21:48:13 -04004709 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004710 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004711
James Smart6a9c52c2009-10-02 15:16:51 -04004712 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4713 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4714 " op:%02x str=%s without registering for"
4715 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004716 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4717 dif_op_str[scsi_get_prot_op(cmnd)]);
4718 goto out_fail_command;
4719 }
4720
dea31012005-04-17 16:05:31 -05004721 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004722 * Catch race where our node has transitioned, but the
4723 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004724 */
James Smart6b415f52012-06-12 13:54:59 -04004725 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4726 goto out_tgt_busy;
James Smart7dc517d2010-07-14 15:32:10 -04004727 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
Mike Christie34963432011-02-25 14:04:28 -06004728 goto out_tgt_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05004729
James Smart19ca7602010-11-20 23:11:55 -05004730 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05004731 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004732 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004733
James Smarte8b62012007-08-02 11:10:09 -04004734 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4735 "0707 driver's buffer pool is empty, "
4736 "IO busied\n");
dea31012005-04-17 16:05:31 -05004737 goto out_host_busy;
4738 }
4739
4740 /*
4741 * Store the midlayer's command structure for the completion phase
4742 * and complete the command initialization.
4743 */
4744 lpfc_cmd->pCmd = cmnd;
4745 lpfc_cmd->rdata = rdata;
4746 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04004747 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05004748 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
dea31012005-04-17 16:05:31 -05004749
James Smarte2a0a9d2008-12-04 22:40:02 -05004750 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004751 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004752 lpfc_printf_vlog(vport,
4753 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004754 "9033 BLKGRD: rcvd %s cmd:x%x "
4755 "sector x%llx cnt %u pt %x\n",
4756 dif_op_str[scsi_get_prot_op(cmnd)],
4757 cmnd->cmnd[0],
4758 (unsigned long long)scsi_get_lba(cmnd),
4759 blk_rq_sectors(cmnd->request),
4760 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004761 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004762 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4763 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004764 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004765 lpfc_printf_vlog(vport,
4766 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004767 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4768 "x%x sector x%llx cnt %u pt %x\n",
4769 cmnd->cmnd[0],
4770 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004771 blk_rq_sectors(cmnd->request),
James Smart26134702012-08-14 14:25:50 -04004772 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004773 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004774 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4775 }
4776
dea31012005-04-17 16:05:31 -05004777 if (err)
4778 goto out_host_busy_free_buf;
4779
James Smart2e0fef82007-06-17 19:56:36 -05004780 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004781
James Smart977b5a02008-09-07 11:52:04 -04004782 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04004783 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004784 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05004785 if (err) {
4786 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05004787 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004788 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004789 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004790 lpfc_sli_handle_fast_ring_event(phba,
4791 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4792
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004793 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4794 lpfc_poll_rearm_timer(phba);
4795 }
4796
dea31012005-04-17 16:05:31 -05004797 return 0;
4798
4799 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04004800 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004801 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004802 out_host_busy:
4803 return SCSI_MLQUEUE_HOST_BUSY;
4804
Mike Christie34963432011-02-25 14:04:28 -06004805 out_tgt_busy:
4806 return SCSI_MLQUEUE_TARGET_BUSY;
4807
dea31012005-04-17 16:05:31 -05004808 out_fail_command:
James Smartb9a7c632012-08-03 12:35:24 -04004809 cmnd->scsi_done(cmnd);
dea31012005-04-17 16:05:31 -05004810 return 0;
4811}
4812
Jeff Garzikf2812332010-11-16 02:10:29 -05004813
James Smart9bad7672008-12-04 22:39:02 -05004814/**
James Smart3621a712009-04-06 18:47:14 -04004815 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004816 * @cmnd: Pointer to scsi_cmnd data structure.
4817 *
4818 * This routine aborts @cmnd pending in base driver.
4819 *
4820 * Return code :
4821 * 0x2003 - Error
4822 * 0x2002 - Success
4823 **/
dea31012005-04-17 16:05:31 -05004824static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004825lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004826{
James Smart2e0fef82007-06-17 19:56:36 -05004827 struct Scsi_Host *shost = cmnd->device->host;
4828 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4829 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004830 struct lpfc_iocbq *iocb;
4831 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004832 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004833 IOCB_t *cmd, *icmd;
James Smart3a707302012-06-12 13:54:42 -04004834 int ret = SUCCESS, status = 0;
James Smart876dd7d2012-09-29 11:31:28 -04004835 unsigned long flags;
James Smartfa61a542008-01-11 01:52:42 -05004836 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004837
James Smart3a707302012-06-12 13:54:42 -04004838 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004839 if (status != 0 && status != SUCCESS)
James Smart3a707302012-06-12 13:54:42 -04004840 return status;
James Smart4f2e66c2012-05-09 21:17:07 -04004841
James Smart876dd7d2012-09-29 11:31:28 -04004842 spin_lock_irqsave(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004843 /* driver queued commands are in process of being flushed */
4844 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
James Smart876dd7d2012-09-29 11:31:28 -04004845 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004846 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4847 "3168 SCSI Layer abort requested I/O has been "
4848 "flushed by LLD.\n");
4849 return FAILED;
4850 }
4851
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004852 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smart92e3af62012-08-14 14:26:28 -04004853 if (!lpfc_cmd || !lpfc_cmd->pCmd) {
James Smart876dd7d2012-09-29 11:31:28 -04004854 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarteee88772010-09-29 11:19:08 -04004855 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4856 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004857 "x%x ID %d LUN %d\n",
James Smart3a707302012-06-12 13:54:42 -04004858 SUCCESS, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04004859 return SUCCESS;
4860 }
dea31012005-04-17 16:05:31 -05004861
James Smart4f2e66c2012-05-09 21:17:07 -04004862 iocb = &lpfc_cmd->cur_iocbq;
4863 /* the command is in process of being cancelled */
4864 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
James Smart876dd7d2012-09-29 11:31:28 -04004865 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004866 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4867 "3169 SCSI Layer abort requested I/O has been "
4868 "cancelled by LLD.\n");
4869 return FAILED;
4870 }
dea31012005-04-17 16:05:31 -05004871 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004872 * If pCmd field of the corresponding lpfc_scsi_buf structure
4873 * points to a different SCSI command, then the driver has
4874 * already completed this command, but the midlayer did not
James Smart4f2e66c2012-05-09 21:17:07 -04004875 * see the completion before the eh fired. Just return SUCCESS.
dea31012005-04-17 16:05:31 -05004876 */
James Smart4f2e66c2012-05-09 21:17:07 -04004877 if (lpfc_cmd->pCmd != cmnd) {
4878 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4879 "3170 SCSI Layer abort requested I/O has been "
4880 "completed by LLD.\n");
4881 goto out_unlock;
4882 }
dea31012005-04-17 16:05:31 -05004883
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004884 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004885
James Smart4f2e66c2012-05-09 21:17:07 -04004886 abtsiocb = __lpfc_sli_get_iocbq(phba);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004887 if (abtsiocb == NULL) {
4888 ret = FAILED;
James Smart4f2e66c2012-05-09 21:17:07 -04004889 goto out_unlock;
dea31012005-04-17 16:05:31 -05004890 }
4891
James Smartafbd8d82013-09-06 12:22:13 -04004892 /* Indicate the IO is being aborted by the driver. */
4893 iocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4894
dea31012005-04-17 16:05:31 -05004895 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004896 * The scsi command can not be in txq and it is in flight because the
4897 * pCmd is still pointig at the SCSI command we have to abort. There
4898 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004899 */
dea31012005-04-17 16:05:31 -05004900
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004901 cmd = &iocb->iocb;
4902 icmd = &abtsiocb->iocb;
4903 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4904 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004905 if (phba->sli_rev == LPFC_SLI_REV4)
4906 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4907 else
4908 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004909
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004910 icmd->ulpLe = 1;
4911 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004912
4913 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4914 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05004915 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05004916
James Smart2e0fef82007-06-17 19:56:36 -05004917 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004918 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4919 else
4920 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004921
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004922 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004923 abtsiocb->vport = vport;
James Smart4f2e66c2012-05-09 21:17:07 -04004924 /* no longer need the lock after this point */
James Smart876dd7d2012-09-29 11:31:28 -04004925 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004926
James Smart3772a992009-05-22 14:50:54 -04004927 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
4928 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004929 lpfc_sli_release_iocbq(phba, abtsiocb);
4930 ret = FAILED;
4931 goto out;
4932 }
4933
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004934 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004935 lpfc_sli_handle_fast_ring_event(phba,
4936 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004937
James Smartfa61a542008-01-11 01:52:42 -05004938 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004939 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004940 wait_event_timeout(waitq,
4941 (lpfc_cmd->pCmd != cmnd),
James Smart256ec0d2013-04-17 20:14:58 -04004942 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
James Smartfa61a542008-01-11 01:52:42 -05004943 lpfc_cmd->waitq = NULL;
dea31012005-04-17 16:05:31 -05004944
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004945 if (lpfc_cmd->pCmd == cmnd) {
4946 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04004947 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4948 "0748 abort handler timed out waiting "
James Smart247ca942012-08-14 14:26:13 -04004949 "for abortng I/O (xri:x%x) to complete: "
4950 "ret %#x, ID %d, LUN %d\n",
4951 iocb->sli4_xritag, ret,
4952 cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004953 }
James Smart4f2e66c2012-05-09 21:17:07 -04004954 goto out;
dea31012005-04-17 16:05:31 -05004955
James Smart4f2e66c2012-05-09 21:17:07 -04004956out_unlock:
James Smart876dd7d2012-09-29 11:31:28 -04004957 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004958out:
James Smarte8b62012007-08-02 11:10:09 -04004959 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4960 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004961 "LUN %d\n", ret, cmnd->device->id,
4962 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004963 return ret;
dea31012005-04-17 16:05:31 -05004964}
4965
James Smartbbb9d182009-06-10 17:23:16 -04004966static char *
4967lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4968{
4969 switch (task_mgmt_cmd) {
4970 case FCP_ABORT_TASK_SET:
4971 return "ABORT_TASK_SET";
4972 case FCP_CLEAR_TASK_SET:
4973 return "FCP_CLEAR_TASK_SET";
4974 case FCP_BUS_RESET:
4975 return "FCP_BUS_RESET";
4976 case FCP_LUN_RESET:
4977 return "FCP_LUN_RESET";
4978 case FCP_TARGET_RESET:
4979 return "FCP_TARGET_RESET";
4980 case FCP_CLEAR_ACA:
4981 return "FCP_CLEAR_ACA";
4982 case FCP_TERMINATE_TASK:
4983 return "FCP_TERMINATE_TASK";
4984 default:
4985 return "unknown";
4986 }
4987}
4988
4989/**
4990 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
4991 * @vport: The virtual port for which this call is being executed.
4992 * @rdata: Pointer to remote port local data
4993 * @tgt_id: Target ID of remote device.
4994 * @lun_id: Lun number for the TMF
4995 * @task_mgmt_cmd: type of TMF to send
4996 *
4997 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
4998 * a remote port.
4999 *
5000 * Return Code:
5001 * 0x2003 - Error
5002 * 0x2002 - Success.
5003 **/
5004static int
5005lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
5006 unsigned tgt_id, unsigned int lun_id,
5007 uint8_t task_mgmt_cmd)
5008{
5009 struct lpfc_hba *phba = vport->phba;
5010 struct lpfc_scsi_buf *lpfc_cmd;
5011 struct lpfc_iocbq *iocbq;
5012 struct lpfc_iocbq *iocbqrsp;
James Smart5989b8d2010-10-22 11:06:56 -04005013 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005014 int ret;
5015 int status;
5016
James Smart5989b8d2010-10-22 11:06:56 -04005017 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smartbbb9d182009-06-10 17:23:16 -04005018 return FAILED;
5019
James Smart19ca7602010-11-20 23:11:55 -05005020 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
James Smartbbb9d182009-06-10 17:23:16 -04005021 if (lpfc_cmd == NULL)
5022 return FAILED;
James Smart0c411222013-09-06 12:22:46 -04005023 lpfc_cmd->timeout = phba->cfg_task_mgmt_tmo;
James Smartbbb9d182009-06-10 17:23:16 -04005024 lpfc_cmd->rdata = rdata;
5025
5026 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
5027 task_mgmt_cmd);
5028 if (!status) {
5029 lpfc_release_scsi_buf(phba, lpfc_cmd);
5030 return FAILED;
5031 }
5032
5033 iocbq = &lpfc_cmd->cur_iocbq;
5034 iocbqrsp = lpfc_sli_get_iocbq(phba);
5035 if (iocbqrsp == NULL) {
5036 lpfc_release_scsi_buf(phba, lpfc_cmd);
5037 return FAILED;
5038 }
James Smart5a0916b2013-07-15 18:31:42 -04005039 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
James Smartbbb9d182009-06-10 17:23:16 -04005040
5041 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5042 "0702 Issue %s to TGT %d LUN %d "
James Smart6d368e52011-05-24 11:44:12 -04005043 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04005044 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04005045 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
5046 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04005047
5048 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
5049 iocbq, iocbqrsp, lpfc_cmd->timeout);
5050 if (status != IOCB_SUCCESS) {
5051 if (status == IOCB_TIMEDOUT) {
James Smartbbb9d182009-06-10 17:23:16 -04005052 ret = TIMEOUT_ERROR;
5053 } else
5054 ret = FAILED;
5055 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
5056 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart6d368e52011-05-24 11:44:12 -04005057 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
5058 "iocb_flag x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04005059 lpfc_taskmgmt_name(task_mgmt_cmd),
5060 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
James Smart6d368e52011-05-24 11:44:12 -04005061 iocbqrsp->iocb.un.ulpWord[4],
5062 iocbq->iocb_flag);
James Smart2a9bf3d2010-06-07 15:24:45 -04005063 } else if (status == IOCB_BUSY)
5064 ret = FAILED;
5065 else
James Smartbbb9d182009-06-10 17:23:16 -04005066 ret = SUCCESS;
5067
5068 lpfc_sli_release_iocbq(phba, iocbqrsp);
5069
5070 if (ret != TIMEOUT_ERROR)
5071 lpfc_release_scsi_buf(phba, lpfc_cmd);
5072
5073 return ret;
5074}
5075
5076/**
5077 * lpfc_chk_tgt_mapped -
5078 * @vport: The virtual port to check on
5079 * @cmnd: Pointer to scsi_cmnd data structure.
5080 *
5081 * This routine delays until the scsi target (aka rport) for the
5082 * command exists (is present and logged in) or we declare it non-existent.
5083 *
5084 * Return code :
5085 * 0x2003 - Error
5086 * 0x2002 - Success
5087 **/
5088static int
5089lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
5090{
5091 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005092 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005093 unsigned long later;
5094
James Smart1c6f4ef52009-11-18 15:40:49 -05005095 if (!rdata) {
5096 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5097 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
5098 return FAILED;
5099 }
5100 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005101 /*
5102 * If target is not in a MAPPED state, delay until
5103 * target is rediscovered or devloss timeout expires.
5104 */
5105 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5106 while (time_after(later, jiffies)) {
5107 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
5108 return FAILED;
5109 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
5110 return SUCCESS;
5111 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
5112 rdata = cmnd->device->hostdata;
5113 if (!rdata)
5114 return FAILED;
5115 pnode = rdata->pnode;
5116 }
5117 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
5118 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
5119 return FAILED;
5120 return SUCCESS;
5121}
5122
5123/**
5124 * lpfc_reset_flush_io_context -
5125 * @vport: The virtual port (scsi_host) for the flush context
5126 * @tgt_id: If aborting by Target contect - specifies the target id
5127 * @lun_id: If aborting by Lun context - specifies the lun id
5128 * @context: specifies the context level to flush at.
5129 *
5130 * After a reset condition via TMF, we need to flush orphaned i/o
5131 * contexts from the adapter. This routine aborts any contexts
5132 * outstanding, then waits for their completions. The wait is
5133 * bounded by devloss_tmo though.
5134 *
5135 * Return code :
5136 * 0x2003 - Error
5137 * 0x2002 - Success
5138 **/
5139static int
5140lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5141 uint64_t lun_id, lpfc_ctx_cmd context)
5142{
5143 struct lpfc_hba *phba = vport->phba;
5144 unsigned long later;
5145 int cnt;
5146
5147 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5148 if (cnt)
5149 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
5150 tgt_id, lun_id, context);
5151 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5152 while (time_after(later, jiffies) && cnt) {
5153 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5154 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5155 }
5156 if (cnt) {
5157 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5158 "0724 I/O flush failure for context %s : cnt x%x\n",
5159 ((context == LPFC_CTX_LUN) ? "LUN" :
5160 ((context == LPFC_CTX_TGT) ? "TGT" :
5161 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5162 cnt);
5163 return FAILED;
5164 }
5165 return SUCCESS;
5166}
5167
James Smart9bad7672008-12-04 22:39:02 -05005168/**
James Smart3621a712009-04-06 18:47:14 -04005169 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05005170 * @cmnd: Pointer to scsi_cmnd data structure.
5171 *
James Smartbbb9d182009-06-10 17:23:16 -04005172 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05005173 * command.
5174 *
5175 * Return code :
5176 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04005177 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005178 **/
dea31012005-04-17 16:05:31 -05005179static int
James Smart7054a602007-04-25 09:52:34 -04005180lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005181{
James Smart2e0fef82007-06-17 19:56:36 -05005182 struct Scsi_Host *shost = cmnd->device->host;
5183 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005184 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005185 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005186 unsigned tgt_id = cmnd->device->id;
5187 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04005188 struct lpfc_scsi_event_header scsi_event;
James Smart3a707302012-06-12 13:54:42 -04005189 int status, ret = SUCCESS;
dea31012005-04-17 16:05:31 -05005190
James Smart1c6f4ef52009-11-18 15:40:49 -05005191 if (!rdata) {
5192 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5193 "0798 Device Reset rport failure: rdata x%p\n", rdata);
5194 return FAILED;
5195 }
5196 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005197 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005198 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005199 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005200
5201 status = lpfc_chk_tgt_mapped(vport, cmnd);
5202 if (status == FAILED) {
5203 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5204 "0721 Device Reset rport failure: rdata x%p\n", rdata);
5205 return FAILED;
5206 }
5207
5208 scsi_event.event_type = FC_REG_SCSI_EVENT;
5209 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5210 scsi_event.lun = lun_id;
5211 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5212 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5213
5214 fc_host_post_vendor_event(shost, fc_get_event_number(),
5215 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5216
5217 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5218 FCP_LUN_RESET);
5219
5220 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5221 "0713 SCSI layer issued Device Reset (%d, %d) "
5222 "return x%x\n", tgt_id, lun_id, status);
5223
dea31012005-04-17 16:05:31 -05005224 /*
James Smartbbb9d182009-06-10 17:23:16 -04005225 * We have to clean up i/o as : they may be orphaned by the TMF;
5226 * or if the TMF failed, they may be in an indeterminate state.
5227 * So, continue on.
5228 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05005229 */
James Smart3a707302012-06-12 13:54:42 -04005230 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005231 LPFC_CTX_LUN);
James Smart3a707302012-06-12 13:54:42 -04005232 return ret;
James Smartbbb9d182009-06-10 17:23:16 -04005233}
5234
5235/**
5236 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5237 * @cmnd: Pointer to scsi_cmnd data structure.
5238 *
5239 * This routine does a target reset by sending a TARGET_RESET task management
5240 * command.
5241 *
5242 * Return code :
5243 * 0x2003 - Error
5244 * 0x2002 - Success
5245 **/
5246static int
5247lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5248{
5249 struct Scsi_Host *shost = cmnd->device->host;
5250 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5251 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005252 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005253 unsigned tgt_id = cmnd->device->id;
5254 unsigned int lun_id = cmnd->device->lun;
5255 struct lpfc_scsi_event_header scsi_event;
James Smart3a707302012-06-12 13:54:42 -04005256 int status, ret = SUCCESS;
James Smartbbb9d182009-06-10 17:23:16 -04005257
James Smart1c6f4ef52009-11-18 15:40:49 -05005258 if (!rdata) {
5259 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5260 "0799 Target Reset rport failure: rdata x%p\n", rdata);
5261 return FAILED;
5262 }
5263 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005264 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005265 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005266 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005267
5268 status = lpfc_chk_tgt_mapped(vport, cmnd);
5269 if (status == FAILED) {
5270 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5271 "0722 Target Reset rport failure: rdata x%p\n", rdata);
5272 return FAILED;
dea31012005-04-17 16:05:31 -05005273 }
James Smartea2151b2008-09-07 11:52:10 -04005274
5275 scsi_event.event_type = FC_REG_SCSI_EVENT;
5276 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5277 scsi_event.lun = 0;
5278 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5279 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5280
James Smartbbb9d182009-06-10 17:23:16 -04005281 fc_host_post_vendor_event(shost, fc_get_event_number(),
5282 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04005283
James Smartbbb9d182009-06-10 17:23:16 -04005284 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5285 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05005286
James Smarte8b62012007-08-02 11:10:09 -04005287 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04005288 "0723 SCSI layer issued Target Reset (%d, %d) "
5289 "return x%x\n", tgt_id, lun_id, status);
5290
5291 /*
5292 * We have to clean up i/o as : they may be orphaned by the TMF;
5293 * or if the TMF failed, they may be in an indeterminate state.
5294 * So, continue on.
5295 * We will report success if all the i/o aborts successfully.
5296 */
James Smart3a707302012-06-12 13:54:42 -04005297 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5298 LPFC_CTX_TGT);
5299 return ret;
dea31012005-04-17 16:05:31 -05005300}
5301
James Smart9bad7672008-12-04 22:39:02 -05005302/**
James Smart3621a712009-04-06 18:47:14 -04005303 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05005304 * @cmnd: Pointer to scsi_cmnd data structure.
5305 *
James Smartbbb9d182009-06-10 17:23:16 -04005306 * This routine does target reset to all targets on @cmnd->device->host.
5307 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05005308 *
James Smartbbb9d182009-06-10 17:23:16 -04005309 * Return code :
5310 * 0x2003 - Error
5311 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005312 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04005313static int
James Smart7054a602007-04-25 09:52:34 -04005314lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005315{
James Smart2e0fef82007-06-17 19:56:36 -05005316 struct Scsi_Host *shost = cmnd->device->host;
5317 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005318 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04005319 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04005320 int match;
5321 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04005322
5323 scsi_event.event_type = FC_REG_SCSI_EVENT;
5324 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5325 scsi_event.lun = 0;
5326 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5327 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5328
James Smartbbb9d182009-06-10 17:23:16 -04005329 fc_host_post_vendor_event(shost, fc_get_event_number(),
5330 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05005331
James Smartbf086112011-08-21 21:48:13 -04005332 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005333 if (status != 0 && status != SUCCESS)
James Smartbf086112011-08-21 21:48:13 -04005334 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005335
dea31012005-04-17 16:05:31 -05005336 /*
5337 * Since the driver manages a single bus device, reset all
5338 * targets known to the driver. Should any target reset
5339 * fail, this routine returns failure to the midlayer.
5340 */
James Smarte17da182006-07-06 15:49:25 -04005341 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04005342 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05005343 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05005344 spin_lock_irq(shost->host_lock);
5345 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005346 if (!NLP_CHK_NODE_ACT(ndlp))
5347 continue;
James Smarta6571c62012-10-31 14:44:42 -04005348 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5349 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5350 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005351 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04005352 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04005353 ndlp->rport) {
dea31012005-04-17 16:05:31 -05005354 match = 1;
5355 break;
5356 }
5357 }
James Smart2e0fef82007-06-17 19:56:36 -05005358 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005359 if (!match)
5360 continue;
James Smartbbb9d182009-06-10 17:23:16 -04005361
5362 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
5363 i, 0, FCP_TARGET_RESET);
5364
5365 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04005366 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5367 "0700 Bus Reset on target %d failed\n",
5368 i);
James Smart915caaa2008-06-14 22:52:38 -04005369 ret = FAILED;
dea31012005-04-17 16:05:31 -05005370 }
5371 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005372 /*
James Smartbbb9d182009-06-10 17:23:16 -04005373 * We have to clean up i/o as : they may be orphaned by the TMFs
5374 * above; or if any of the TMFs failed, they may be in an
5375 * indeterminate state.
5376 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005377 */
James Smartbbb9d182009-06-10 17:23:16 -04005378
5379 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5380 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005381 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04005382
James Smarte8b62012007-08-02 11:10:09 -04005383 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5384 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05005385 return ret;
5386}
5387
James Smart9bad7672008-12-04 22:39:02 -05005388/**
James Smart27b01b82012-05-09 21:19:44 -04005389 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
5390 * @cmnd: Pointer to scsi_cmnd data structure.
5391 *
5392 * This routine does host reset to the adaptor port. It brings the HBA
5393 * offline, performs a board restart, and then brings the board back online.
5394 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
5395 * reject all outstanding SCSI commands to the host and error returned
5396 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5397 * of error handling, it will only return error if resetting of the adapter
5398 * is not successful; in all other cases, will return success.
5399 *
5400 * Return code :
5401 * 0x2003 - Error
5402 * 0x2002 - Success
5403 **/
5404static int
5405lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5406{
5407 struct Scsi_Host *shost = cmnd->device->host;
5408 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5409 struct lpfc_hba *phba = vport->phba;
5410 int rc, ret = SUCCESS;
5411
James Smarta88dbb62013-04-17 20:18:39 -04005412 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5413 "3172 SCSI layer issued Host Reset Data:\n");
5414
James Smart618a5232012-06-12 13:54:36 -04005415 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart27b01b82012-05-09 21:19:44 -04005416 lpfc_offline(phba);
5417 rc = lpfc_sli_brdrestart(phba);
5418 if (rc)
5419 ret = FAILED;
James Smarta88dbb62013-04-17 20:18:39 -04005420 rc = lpfc_online(phba);
5421 if (rc)
5422 ret = FAILED;
James Smart27b01b82012-05-09 21:19:44 -04005423 lpfc_unblock_mgmt_io(phba);
5424
James Smarta88dbb62013-04-17 20:18:39 -04005425 if (ret == FAILED) {
5426 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5427 "3323 Failed host reset, bring it offline\n");
5428 lpfc_sli4_offline_eratt(phba);
5429 }
James Smart27b01b82012-05-09 21:19:44 -04005430 return ret;
5431}
5432
5433/**
James Smart3621a712009-04-06 18:47:14 -04005434 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05005435 * @sdev: Pointer to scsi_device.
5436 *
5437 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5438 * globally available list of scsi buffers. This routine also makes sure scsi
5439 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5440 * of scsi buffer exists for the lifetime of the driver.
5441 *
5442 * Return codes:
5443 * non-0 - Error
5444 * 0 - Success
5445 **/
dea31012005-04-17 16:05:31 -05005446static int
dea31012005-04-17 16:05:31 -05005447lpfc_slave_alloc(struct scsi_device *sdev)
5448{
James Smart2e0fef82007-06-17 19:56:36 -05005449 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5450 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005451 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04005452 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05005453 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04005454 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04005455 uint32_t sdev_cnt;
dea31012005-04-17 16:05:31 -05005456
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005457 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05005458 return -ENXIO;
5459
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005460 sdev->hostdata = rport->dd_data;
James Smartd7c47992010-06-08 18:31:54 -04005461 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005462
5463 /*
5464 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5465 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04005466 * HBA limit conveyed to the midlayer via the host structure. The
5467 * formula accounts for the lun_queue_depth + error handlers + 1
5468 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05005469 */
5470 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04005471 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05005472
James Smartd7c47992010-06-08 18:31:54 -04005473 /* If allocated buffers are enough do nothing */
5474 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5475 return 0;
5476
James Smart92d7f7b2007-06-17 19:56:38 -05005477 /* Allow some exchanges to be available always to complete discovery */
5478 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005479 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5480 "0704 At limitation of %d preallocated "
5481 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05005482 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05005483 /* Allow some exchanges to be available always to complete discovery */
5484 } else if (total + num_to_alloc >
5485 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005486 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5487 "0705 Allocation request of %d "
5488 "command buffers will exceed max of %d. "
5489 "Reducing allocation request to %d.\n",
5490 num_to_alloc, phba->cfg_hba_queue_depth,
5491 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05005492 num_to_alloc = phba->cfg_hba_queue_depth - total;
5493 }
James Smart3772a992009-05-22 14:50:54 -04005494 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
5495 if (num_to_alloc != num_allocated) {
James Smart96f70772013-04-17 20:16:15 -04005496 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5497 "0708 Allocation request of %d "
5498 "command buffers did not succeed. "
5499 "Allocated %d buffers.\n",
5500 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05005501 }
James Smart1c6f4ef52009-11-18 15:40:49 -05005502 if (num_allocated > 0)
5503 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05005504 return 0;
5505}
5506
James Smart9bad7672008-12-04 22:39:02 -05005507/**
James Smart3621a712009-04-06 18:47:14 -04005508 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05005509 * @sdev: Pointer to scsi_device.
5510 *
5511 * This routine configures following items
5512 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05005513 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5514 *
5515 * Return codes:
5516 * 0 - Success
5517 **/
dea31012005-04-17 16:05:31 -05005518static int
5519lpfc_slave_configure(struct scsi_device *sdev)
5520{
James Smart2e0fef82007-06-17 19:56:36 -05005521 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5522 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005523
5524 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04005525 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005526 else
James Smart3de2a652007-08-02 11:09:59 -04005527 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005528
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005529 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005530 lpfc_sli_handle_fast_ring_event(phba,
5531 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005532 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5533 lpfc_poll_rearm_timer(phba);
5534 }
5535
dea31012005-04-17 16:05:31 -05005536 return 0;
5537}
5538
James Smart9bad7672008-12-04 22:39:02 -05005539/**
James Smart3621a712009-04-06 18:47:14 -04005540 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05005541 * @sdev: Pointer to scsi_device.
5542 *
5543 * This routine sets @sdev hostatdata filed to null.
5544 **/
dea31012005-04-17 16:05:31 -05005545static void
5546lpfc_slave_destroy(struct scsi_device *sdev)
5547{
James Smartd7c47992010-06-08 18:31:54 -04005548 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5549 struct lpfc_hba *phba = vport->phba;
5550 atomic_dec(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005551 sdev->hostdata = NULL;
5552 return;
5553}
5554
James Smart92d7f7b2007-06-17 19:56:38 -05005555
dea31012005-04-17 16:05:31 -05005556struct scsi_host_template lpfc_template = {
5557 .module = THIS_MODULE,
5558 .name = LPFC_DRIVER_NAME,
5559 .info = lpfc_info,
5560 .queuecommand = lpfc_queuecommand,
5561 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005562 .eh_device_reset_handler = lpfc_device_reset_handler,
5563 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04005564 .eh_bus_reset_handler = lpfc_bus_reset_handler,
James Smart27b01b82012-05-09 21:19:44 -04005565 .eh_host_reset_handler = lpfc_host_reset_handler,
dea31012005-04-17 16:05:31 -05005566 .slave_alloc = lpfc_slave_alloc,
5567 .slave_configure = lpfc_slave_configure,
5568 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04005569 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05005570 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005571 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05005572 .cmd_per_lun = LPFC_CMD_PER_LUN,
5573 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05005574 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04005575 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04005576 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05005577 .change_queue_depth = lpfc_change_queue_depth,
James Smartfe8f7f92013-01-03 15:43:03 -05005578 .change_queue_type = lpfc_change_queue_type,
dea31012005-04-17 16:05:31 -05005579};
James Smart3de2a652007-08-02 11:09:59 -04005580
5581struct scsi_host_template lpfc_vport_template = {
5582 .module = THIS_MODULE,
5583 .name = LPFC_DRIVER_NAME,
5584 .info = lpfc_info,
5585 .queuecommand = lpfc_queuecommand,
5586 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005587 .eh_device_reset_handler = lpfc_device_reset_handler,
5588 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04005589 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5590 .slave_alloc = lpfc_slave_alloc,
5591 .slave_configure = lpfc_slave_configure,
5592 .slave_destroy = lpfc_slave_destroy,
5593 .scan_finished = lpfc_scan_finished,
5594 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005595 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04005596 .cmd_per_lun = LPFC_CMD_PER_LUN,
5597 .use_clustering = ENABLE_CLUSTERING,
5598 .shost_attrs = lpfc_vport_attrs,
5599 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05005600 .change_queue_depth = lpfc_change_queue_depth,
James Smartfe8f7f92013-01-03 15:43:03 -05005601 .change_queue_type = lpfc_change_queue_type,
James Smart3de2a652007-08-02 11:09:59 -04005602};