blob: 4015fcc6d9a1686e52782c8e67e280e8559f1369 [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 Smart1ba981f2014-02-20 09:56:45 -050071static struct lpfc_rport_data *
72lpfc_rport_data_from_scsi_device(struct scsi_device *sdev)
73{
74 struct lpfc_vport *vport = (struct lpfc_vport *)sdev->host->hostdata;
75
76 if (vport->phba->cfg_EnableXLane)
77 return ((struct lpfc_device_data *)sdev->hostdata)->rport_data;
78 else
79 return (struct lpfc_rport_data *)sdev->hostdata;
80}
81
James Smartda0436e2009-05-22 14:51:39 -040082static void
83lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smart1c6f4ef52009-11-18 15:40:49 -050084static void
85lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smart9c6aa9d2013-05-31 17:03:39 -040086static int
87lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc);
James Smarte2a0a9d2008-12-04 22:40:02 -050088
89static void
James Smart6a9c52c2009-10-02 15:16:51 -040090lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -050091{
92 void *src, *dst;
93 struct scatterlist *sgde = scsi_sglist(cmnd);
94
95 if (!_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040096 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
97 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -050098 __func__);
99 return;
100 }
101
102
103 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -0400104 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
105 "9051 BLKGRD: ERROR: data scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -0500106 return;
107 }
108
109 dst = (void *) _dump_buf_data;
110 while (sgde) {
111 src = sg_virt(sgde);
112 memcpy(dst, src, sgde->length);
113 dst += sgde->length;
114 sgde = sg_next(sgde);
115 }
116}
117
118static void
James Smart6a9c52c2009-10-02 15:16:51 -0400119lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -0500120{
121 void *src, *dst;
122 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
123
124 if (!_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -0400125 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
126 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -0500127 __func__);
128 return;
129 }
130
131 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -0400132 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
133 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -0500134 return;
135 }
136
137 dst = _dump_buf_dif;
138 while (sgde) {
139 src = sg_virt(sgde);
140 memcpy(dst, src, sgde->length);
141 dst += sgde->length;
142 sgde = sg_next(sgde);
143 }
144}
145
James Smart9c6aa9d2013-05-31 17:03:39 -0400146static inline unsigned
147lpfc_cmd_blksize(struct scsi_cmnd *sc)
148{
149 return sc->device->sector_size;
150}
151
152#define LPFC_CHECK_PROTECT_GUARD 1
153#define LPFC_CHECK_PROTECT_REF 2
154static inline unsigned
155lpfc_cmd_protect(struct scsi_cmnd *sc, int flag)
156{
157 return 1;
158}
159
160static inline unsigned
161lpfc_cmd_guard_csum(struct scsi_cmnd *sc)
162{
163 if (lpfc_prot_group_type(NULL, sc) == LPFC_PG_TYPE_NO_DIF)
164 return 0;
165 if (scsi_host_get_guard(sc->device->host) == SHOST_DIX_GUARD_IP)
166 return 1;
167 return 0;
168}
169
James Smartea2151b2008-09-07 11:52:10 -0400170/**
James Smartf1126682009-06-10 17:22:44 -0400171 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
172 * @phba: Pointer to HBA object.
173 * @lpfc_cmd: lpfc scsi command object pointer.
174 *
175 * This function is called from the lpfc_prep_task_mgmt_cmd function to
176 * set the last bit in the response sge entry.
177 **/
178static void
179lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
180 struct lpfc_scsi_buf *lpfc_cmd)
181{
182 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
183 if (sgl) {
184 sgl += 1;
185 sgl->word2 = le32_to_cpu(sgl->word2);
186 bf_set(lpfc_sli4_sge_last, sgl, 1);
187 sgl->word2 = cpu_to_le32(sgl->word2);
188 }
189}
190
191/**
James Smart3621a712009-04-06 18:47:14 -0400192 * lpfc_update_stats - Update statistical data for the command completion
James Smartea2151b2008-09-07 11:52:10 -0400193 * @phba: Pointer to HBA object.
194 * @lpfc_cmd: lpfc scsi command object pointer.
195 *
196 * This function is called when there is a command completion and this
197 * function updates the statistical data for the command completion.
198 **/
199static void
200lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
201{
202 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
203 struct lpfc_nodelist *pnode = rdata->pnode;
204 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
205 unsigned long flags;
206 struct Scsi_Host *shost = cmd->device->host;
207 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
208 unsigned long latency;
209 int i;
210
211 if (cmd->result)
212 return;
213
James Smart9f1e1b52008-12-04 22:39:40 -0500214 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
215
James Smartea2151b2008-09-07 11:52:10 -0400216 spin_lock_irqsave(shost->host_lock, flags);
217 if (!vport->stat_data_enabled ||
218 vport->stat_data_blocked ||
James Smart5989b8d2010-10-22 11:06:56 -0400219 !pnode ||
James Smartea2151b2008-09-07 11:52:10 -0400220 !pnode->lat_data ||
221 (phba->bucket_type == LPFC_NO_BUCKET)) {
222 spin_unlock_irqrestore(shost->host_lock, flags);
223 return;
224 }
James Smartea2151b2008-09-07 11:52:10 -0400225
226 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
227 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
228 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -0500229 /* check array subscript bounds */
230 if (i < 0)
231 i = 0;
232 else if (i >= LPFC_MAX_BUCKET_COUNT)
233 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -0400234 } else {
235 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
236 if (latency <= (phba->bucket_base +
237 ((1<<i)*phba->bucket_step)))
238 break;
239 }
240
241 pnode->lat_data[i].cmd_count++;
242 spin_unlock_irqrestore(shost->host_lock, flags);
243}
244
James Smartea2151b2008-09-07 11:52:10 -0400245/**
James Smart3621a712009-04-06 18:47:14 -0400246 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
James Smartea2151b2008-09-07 11:52:10 -0400247 * @phba: Pointer to HBA context object.
248 * @vport: Pointer to vport object.
249 * @ndlp: Pointer to FC node associated with the target.
250 * @lun: Lun number of the scsi device.
251 * @old_val: Old value of the queue depth.
252 * @new_val: New value of the queue depth.
253 *
254 * This function sends an event to the mgmt application indicating
255 * there is a change in the scsi device queue depth.
256 **/
257static void
258lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
259 struct lpfc_vport *vport,
260 struct lpfc_nodelist *ndlp,
261 uint32_t lun,
262 uint32_t old_val,
263 uint32_t new_val)
264{
265 struct lpfc_fast_path_event *fast_path_evt;
266 unsigned long flags;
267
268 fast_path_evt = lpfc_alloc_fast_evt(phba);
269 if (!fast_path_evt)
270 return;
271
272 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
273 FC_REG_SCSI_EVENT;
274 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
275 LPFC_EVENT_VARQUEDEPTH;
276
277 /* Report all luns with change in queue depth */
278 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
279 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
280 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
281 &ndlp->nlp_portname, sizeof(struct lpfc_name));
282 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
283 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
284 }
285
286 fast_path_evt->un.queue_depth_evt.oldval = old_val;
287 fast_path_evt->un.queue_depth_evt.newval = new_val;
288 fast_path_evt->vport = vport;
289
290 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
291 spin_lock_irqsave(&phba->hbalock, flags);
292 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
293 spin_unlock_irqrestore(&phba->hbalock, flags);
294 lpfc_worker_wake_up(phba);
295
296 return;
297}
298
James Smart9bad7672008-12-04 22:39:02 -0500299/**
James Smart5ffc2662009-11-18 15:39:44 -0500300 * lpfc_change_queue_depth - Alter scsi device queue depth
301 * @sdev: Pointer the scsi device on which to change the queue depth.
302 * @qdepth: New queue depth to set the sdev to.
303 * @reason: The reason for the queue depth change.
304 *
305 * This function is called by the midlayer and the LLD to alter the queue
306 * depth for a scsi device. This function sets the queue depth to the new
307 * value and sends an event out to log the queue depth change.
308 **/
309int
310lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
311{
312 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
313 struct lpfc_hba *phba = vport->phba;
314 struct lpfc_rport_data *rdata;
315 unsigned long new_queue_depth, old_queue_depth;
316
317 old_queue_depth = sdev->queue_depth;
318 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
319 new_queue_depth = sdev->queue_depth;
James Smart1ba981f2014-02-20 09:56:45 -0500320 rdata = lpfc_rport_data_from_scsi_device(sdev);
James Smart5ffc2662009-11-18 15:39:44 -0500321 if (rdata)
322 lpfc_send_sdev_queuedepth_change_event(phba, vport,
323 rdata->pnode, sdev->lun,
324 old_queue_depth,
325 new_queue_depth);
326 return sdev->queue_depth;
327}
328
329/**
James Smartfe8f7f92013-01-03 15:43:03 -0500330 * lpfc_change_queue_type() - Change a device's scsi tag queuing type
331 * @sdev: Pointer the scsi device whose queue depth is to change
332 * @tag_type: Identifier for queue tag type
333 */
334static int
335lpfc_change_queue_type(struct scsi_device *sdev, int tag_type)
336{
337 if (sdev->tagged_supported) {
338 scsi_set_tag_type(sdev, tag_type);
339 if (tag_type)
340 scsi_activate_tcq(sdev, sdev->queue_depth);
341 else
342 scsi_deactivate_tcq(sdev, sdev->queue_depth);
343 } else
344 tag_type = 0;
345
346 return tag_type;
347}
348
349/**
James Smart3621a712009-04-06 18:47:14 -0400350 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500351 * @phba: The Hba for which this call is being executed.
352 *
353 * This routine is called when there is resource error in driver or firmware.
354 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
355 * posts at most 1 event each second. This routine wakes up worker thread of
356 * @phba to process WORKER_RAM_DOWN_EVENT event.
357 *
358 * This routine should be called with no lock held.
359 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500360void
James Smarteaf15d52008-12-04 22:39:29 -0500361lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500362{
363 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400364 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500365
366 spin_lock_irqsave(&phba->hbalock, flags);
367 atomic_inc(&phba->num_rsrc_err);
368 phba->last_rsrc_error_time = jiffies;
369
370 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
371 spin_unlock_irqrestore(&phba->hbalock, flags);
372 return;
373 }
374
375 phba->last_ramp_down_time = jiffies;
376
377 spin_unlock_irqrestore(&phba->hbalock, flags);
378
379 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400380 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
381 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500382 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500383 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
384
James Smart5e9d9b82008-06-14 22:52:53 -0400385 if (!evt_posted)
386 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500387 return;
388}
389
James Smart9bad7672008-12-04 22:39:02 -0500390/**
James Smart3621a712009-04-06 18:47:14 -0400391 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
James Smart9bad7672008-12-04 22:39:02 -0500392 * @phba: The Hba for which this call is being executed.
393 *
394 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
395 * post at most 1 event every 5 minute after last_ramp_up_time or
396 * last_rsrc_error_time. This routine wakes up worker thread of @phba
397 * to process WORKER_RAM_DOWN_EVENT event.
398 *
399 * This routine should be called with no lock held.
400 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500401static inline void
James Smart3de2a652007-08-02 11:09:59 -0400402lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smarta257bf92009-04-06 18:48:10 -0400403 uint32_t queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500404{
405 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -0400406 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400407 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500408 atomic_inc(&phba->num_cmd_success);
409
James Smarta257bf92009-04-06 18:48:10 -0400410 if (vport->cfg_lun_queue_depth <= queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500411 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500412 spin_lock_irqsave(&phba->hbalock, flags);
James Smart5ffc2662009-11-18 15:39:44 -0500413 if (time_before(jiffies,
414 phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
415 time_before(jiffies,
416 phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500417 spin_unlock_irqrestore(&phba->hbalock, flags);
418 return;
419 }
James Smart92d7f7b2007-06-17 19:56:38 -0500420 phba->last_ramp_up_time = jiffies;
421 spin_unlock_irqrestore(&phba->hbalock, flags);
422
423 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400424 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
425 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500426 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500427 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
428
James Smart5e9d9b82008-06-14 22:52:53 -0400429 if (!evt_posted)
430 lpfc_worker_wake_up(phba);
431 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500432}
433
James Smart9bad7672008-12-04 22:39:02 -0500434/**
James Smart3621a712009-04-06 18:47:14 -0400435 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500436 * @phba: The Hba for which this call is being executed.
437 *
438 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
439 * thread.This routine reduces queue depth for all scsi device on each vport
440 * associated with @phba.
441 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500442void
443lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
444{
James Smart549e55c2007-08-02 11:09:51 -0400445 struct lpfc_vport **vports;
446 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500447 struct scsi_device *sdev;
James Smart5ffc2662009-11-18 15:39:44 -0500448 unsigned long new_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500449 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400450 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500451
452 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
453 num_cmd_success = atomic_read(&phba->num_cmd_success);
454
James Smart75ad83a2012-05-09 21:18:40 -0400455 /*
456 * The error and success command counters are global per
457 * driver instance. If another handler has already
458 * operated on this error event, just exit.
459 */
460 if (num_rsrc_err == 0)
461 return;
462
James Smart549e55c2007-08-02 11:09:51 -0400463 vports = lpfc_create_vport_work_array(phba);
464 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400465 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400466 shost = lpfc_shost_from_vport(vports[i]);
467 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500468 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400469 sdev->queue_depth * num_rsrc_err /
470 (num_rsrc_err + num_cmd_success);
471 if (!new_queue_depth)
472 new_queue_depth = sdev->queue_depth - 1;
473 else
474 new_queue_depth = sdev->queue_depth -
475 new_queue_depth;
James Smart5ffc2662009-11-18 15:39:44 -0500476 lpfc_change_queue_depth(sdev, new_queue_depth,
477 SCSI_QDEPTH_DEFAULT);
James Smart549e55c2007-08-02 11:09:51 -0400478 }
James Smart92d7f7b2007-06-17 19:56:38 -0500479 }
James Smart09372822008-01-11 01:52:54 -0500480 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500481 atomic_set(&phba->num_rsrc_err, 0);
482 atomic_set(&phba->num_cmd_success, 0);
483}
484
James Smart9bad7672008-12-04 22:39:02 -0500485/**
James Smart3621a712009-04-06 18:47:14 -0400486 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500487 * @phba: The Hba for which this call is being executed.
488 *
489 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
490 * thread.This routine increases queue depth for all scsi device on each vport
491 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
492 * num_cmd_success to zero.
493 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500494void
495lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
496{
James Smart549e55c2007-08-02 11:09:51 -0400497 struct lpfc_vport **vports;
498 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500499 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400500 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500501
James Smart549e55c2007-08-02 11:09:51 -0400502 vports = lpfc_create_vport_work_array(phba);
503 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400504 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400505 shost = lpfc_shost_from_vport(vports[i]);
506 shost_for_each_device(sdev, shost) {
James Smart97eab632008-04-07 10:16:05 -0400507 if (vports[i]->cfg_lun_queue_depth <=
508 sdev->queue_depth)
509 continue;
James Smart5ffc2662009-11-18 15:39:44 -0500510 lpfc_change_queue_depth(sdev,
511 sdev->queue_depth+1,
512 SCSI_QDEPTH_RAMP_UP);
James Smart549e55c2007-08-02 11:09:51 -0400513 }
James Smart92d7f7b2007-06-17 19:56:38 -0500514 }
James Smart09372822008-01-11 01:52:54 -0500515 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500516 atomic_set(&phba->num_rsrc_err, 0);
517 atomic_set(&phba->num_cmd_success, 0);
518}
519
James Smarta8e497d2008-08-24 21:50:11 -0400520/**
James Smart3621a712009-04-06 18:47:14 -0400521 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400522 * @phba: Pointer to HBA context object.
523 *
524 * This function walks vport list and set each SCSI host to block state
525 * by invoking fc_remote_port_delete() routine. This function is invoked
526 * with EEH when device's PCI slot has been permanently disabled.
527 **/
528void
529lpfc_scsi_dev_block(struct lpfc_hba *phba)
530{
531 struct lpfc_vport **vports;
532 struct Scsi_Host *shost;
533 struct scsi_device *sdev;
534 struct fc_rport *rport;
535 int i;
536
537 vports = lpfc_create_vport_work_array(phba);
538 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400539 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400540 shost = lpfc_shost_from_vport(vports[i]);
541 shost_for_each_device(sdev, shost) {
542 rport = starget_to_rport(scsi_target(sdev));
543 fc_remote_port_delete(rport);
544 }
545 }
546 lpfc_destroy_vport_work_array(phba, vports);
547}
548
James Smart9bad7672008-12-04 22:39:02 -0500549/**
James Smart3772a992009-05-22 14:50:54 -0400550 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500551 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400552 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500553 *
James Smart3772a992009-05-22 14:50:54 -0400554 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
555 * the scsi buffer contains all the necessary information needed to initiate
556 * a SCSI I/O. The non-DMAable buffer region contains information to build
557 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
558 * and the initial BPL. In addition to allocating memory, the FCP CMND and
559 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500560 *
561 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400562 * int - number of scsi buffers that were allocated.
563 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500564 **/
James Smart3772a992009-05-22 14:50:54 -0400565static int
566lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500567{
James Smart2e0fef82007-06-17 19:56:36 -0500568 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500569 struct lpfc_scsi_buf *psb;
570 struct ulp_bde64 *bpl;
571 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400572 dma_addr_t pdma_phys_fcp_cmd;
573 dma_addr_t pdma_phys_fcp_rsp;
574 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500575 uint16_t iotag;
James Smart96f70772013-04-17 20:16:15 -0400576 int bcnt, bpl_size;
577
578 bpl_size = phba->cfg_sg_dma_buf_size -
579 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
580
581 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
582 "9067 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
583 num_to_alloc, phba->cfg_sg_dma_buf_size,
584 (int)sizeof(struct fcp_cmnd),
585 (int)sizeof(struct fcp_rsp), bpl_size);
dea31012005-04-17 16:05:31 -0500586
James Smart3772a992009-05-22 14:50:54 -0400587 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
588 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
589 if (!psb)
590 break;
dea31012005-04-17 16:05:31 -0500591
James Smart3772a992009-05-22 14:50:54 -0400592 /*
593 * Get memory from the pci pool to map the virt space to pci
594 * bus space for an I/O. The DMA buffer includes space for the
595 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
596 * necessary to support the sg_tablesize.
597 */
598 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
599 GFP_KERNEL, &psb->dma_handle);
600 if (!psb->data) {
601 kfree(psb);
602 break;
603 }
dea31012005-04-17 16:05:31 -0500604
James Smart3772a992009-05-22 14:50:54 -0400605 /* Initialize virtual ptrs to dma_buf region. */
606 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
dea31012005-04-17 16:05:31 -0500607
James Smart3772a992009-05-22 14:50:54 -0400608 /* Allocate iotag for psb->cur_iocbq. */
609 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
610 if (iotag == 0) {
611 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
612 psb->data, psb->dma_handle);
613 kfree(psb);
614 break;
615 }
616 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500617
James Smart3772a992009-05-22 14:50:54 -0400618 psb->fcp_cmnd = psb->data;
619 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
620 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400621 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500622
James Smart3772a992009-05-22 14:50:54 -0400623 /* Initialize local short-hand pointers. */
624 bpl = psb->fcp_bpl;
625 pdma_phys_fcp_cmd = psb->dma_handle;
626 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
627 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
628 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500629
James Smart3772a992009-05-22 14:50:54 -0400630 /*
631 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
632 * are sg list bdes. Initialize the first two and leave the
633 * rest for queuecommand.
634 */
635 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
636 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
637 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
638 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
639 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500640
James Smart3772a992009-05-22 14:50:54 -0400641 /* Setup the physical region for the FCP RSP */
642 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
643 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
644 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
645 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
646 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
647
648 /*
649 * Since the IOCB for the FCP I/O is built into this
650 * lpfc_scsi_buf, initialize it with all known data now.
651 */
652 iocb = &psb->cur_iocbq.iocb;
653 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
654 if ((phba->sli_rev == 3) &&
655 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
656 /* fill in immediate fcp command BDE */
657 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
658 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
659 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
660 unsli3.fcp_ext.icd);
661 iocb->un.fcpi64.bdl.addrHigh = 0;
662 iocb->ulpBdeCount = 0;
663 iocb->ulpLe = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300664 /* fill in response BDE */
James Smart3772a992009-05-22 14:50:54 -0400665 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
666 BUFF_TYPE_BDE_64;
667 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
668 sizeof(struct fcp_rsp);
669 iocb->unsli3.fcp_ext.rbde.addrLow =
670 putPaddrLow(pdma_phys_fcp_rsp);
671 iocb->unsli3.fcp_ext.rbde.addrHigh =
672 putPaddrHigh(pdma_phys_fcp_rsp);
673 } else {
674 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
675 iocb->un.fcpi64.bdl.bdeSize =
676 (2 * sizeof(struct ulp_bde64));
677 iocb->un.fcpi64.bdl.addrLow =
678 putPaddrLow(pdma_phys_bpl);
679 iocb->un.fcpi64.bdl.addrHigh =
680 putPaddrHigh(pdma_phys_bpl);
681 iocb->ulpBdeCount = 1;
682 iocb->ulpLe = 1;
683 }
684 iocb->ulpClass = CLASS3;
685 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400686 /* Put it back into the SCSI buffer list */
James Smarteee88772010-09-29 11:19:08 -0400687 psb->cur_iocbq.context1 = psb;
James Smart1c6f4ef52009-11-18 15:40:49 -0500688 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400689
James Smart34b02dc2008-08-24 21:49:55 -0400690 }
dea31012005-04-17 16:05:31 -0500691
James Smart3772a992009-05-22 14:50:54 -0400692 return bcnt;
dea31012005-04-17 16:05:31 -0500693}
694
James Smart9bad7672008-12-04 22:39:02 -0500695/**
James Smart1151e3e2011-02-16 12:39:35 -0500696 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
697 * @vport: pointer to lpfc vport data structure.
698 *
699 * This routine is invoked by the vport cleanup for deletions and the cleanup
700 * for an ndlp on removal.
701 **/
702void
703lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
704{
705 struct lpfc_hba *phba = vport->phba;
706 struct lpfc_scsi_buf *psb, *next_psb;
707 unsigned long iflag = 0;
708
709 spin_lock_irqsave(&phba->hbalock, iflag);
710 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
711 list_for_each_entry_safe(psb, next_psb,
712 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
713 if (psb->rdata && psb->rdata->pnode
714 && psb->rdata->pnode->vport == vport)
715 psb->rdata = NULL;
716 }
717 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
718 spin_unlock_irqrestore(&phba->hbalock, iflag);
719}
720
721/**
James Smartda0436e2009-05-22 14:51:39 -0400722 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
723 * @phba: pointer to lpfc hba data structure.
724 * @axri: pointer to the fcp xri abort wcqe structure.
725 *
726 * This routine is invoked by the worker thread to process a SLI4 fast-path
727 * FCP aborted xri.
728 **/
729void
730lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
731 struct sli4_wcqe_xri_aborted *axri)
732{
733 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500734 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartda0436e2009-05-22 14:51:39 -0400735 struct lpfc_scsi_buf *psb, *next_psb;
736 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500737 struct lpfc_iocbq *iocbq;
738 int i;
James Smart19ca7602010-11-20 23:11:55 -0500739 struct lpfc_nodelist *ndlp;
740 int rrq_empty = 0;
James Smart589a52d2010-07-14 15:30:54 -0400741 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smartda0436e2009-05-22 14:51:39 -0400742
James Smart0f65ff62010-02-26 14:14:23 -0500743 spin_lock_irqsave(&phba->hbalock, iflag);
744 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400745 list_for_each_entry_safe(psb, next_psb,
746 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
747 if (psb->cur_iocbq.sli4_xritag == xri) {
748 list_del(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500749 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400750 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500751 spin_unlock(
752 &phba->sli4_hba.abts_scsi_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500753 if (psb->rdata && psb->rdata->pnode)
754 ndlp = psb->rdata->pnode;
755 else
756 ndlp = NULL;
757
James Smart19ca7602010-11-20 23:11:55 -0500758 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500759 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartcb69f7d2011-12-13 13:21:57 -0500760 if (ndlp) {
James Smartee0f4fe2012-05-09 21:19:14 -0400761 lpfc_set_rrq_active(phba, ndlp,
762 psb->cur_iocbq.sli4_lxritag, rxid, 1);
James Smartcb69f7d2011-12-13 13:21:57 -0500763 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
764 }
James Smartda0436e2009-05-22 14:51:39 -0400765 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500766 if (rrq_empty)
767 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400768 return;
769 }
770 }
James Smart0f65ff62010-02-26 14:14:23 -0500771 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
772 for (i = 1; i <= phba->sli.last_iotag; i++) {
773 iocbq = phba->sli.iocbq_lookup[i];
774
775 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
776 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
777 continue;
778 if (iocbq->sli4_xritag != xri)
779 continue;
780 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
781 psb->exch_busy = 0;
782 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart0e9bb8d2013-03-01 16:35:12 -0500783 if (!list_empty(&pring->txq))
James Smart589a52d2010-07-14 15:30:54 -0400784 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500785 return;
786
787 }
788 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400789}
790
791/**
James Smart8a9d2e82012-05-09 21:16:12 -0400792 * lpfc_sli4_post_scsi_sgl_list - Psot blocks of scsi buffer sgls from a list
793 * @phba: pointer to lpfc hba data structure.
794 * @post_sblist: pointer to the scsi buffer list.
795 *
796 * This routine walks a list of scsi buffers that was passed in. It attempts
797 * to construct blocks of scsi buffer sgls which contains contiguous xris and
798 * uses the non-embedded SGL block post mailbox commands to post to the port.
799 * For single SCSI buffer sgl with non-contiguous xri, if any, it shall use
800 * embedded SGL post mailbox command for posting. The @post_sblist passed in
801 * must be local list, thus no lock is needed when manipulate the list.
802 *
803 * Returns: 0 = failure, non-zero number of successfully posted buffers.
804 **/
805int
806lpfc_sli4_post_scsi_sgl_list(struct lpfc_hba *phba,
807 struct list_head *post_sblist, int sb_count)
808{
809 struct lpfc_scsi_buf *psb, *psb_next;
James Smart96f70772013-04-17 20:16:15 -0400810 int status, sgl_size;
James Smart8a9d2e82012-05-09 21:16:12 -0400811 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
812 dma_addr_t pdma_phys_bpl1;
813 int last_xritag = NO_XRI;
814 LIST_HEAD(prep_sblist);
815 LIST_HEAD(blck_sblist);
816 LIST_HEAD(scsi_sblist);
817
818 /* sanity check */
819 if (sb_count <= 0)
820 return -EINVAL;
821
James Smart96f70772013-04-17 20:16:15 -0400822 sgl_size = phba->cfg_sg_dma_buf_size -
823 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
824
James Smart8a9d2e82012-05-09 21:16:12 -0400825 list_for_each_entry_safe(psb, psb_next, post_sblist, list) {
826 list_del_init(&psb->list);
827 block_cnt++;
828 if ((last_xritag != NO_XRI) &&
829 (psb->cur_iocbq.sli4_xritag != last_xritag + 1)) {
830 /* a hole in xri block, form a sgl posting block */
831 list_splice_init(&prep_sblist, &blck_sblist);
832 post_cnt = block_cnt - 1;
833 /* prepare list for next posting block */
834 list_add_tail(&psb->list, &prep_sblist);
835 block_cnt = 1;
836 } else {
837 /* prepare list for next posting block */
838 list_add_tail(&psb->list, &prep_sblist);
839 /* enough sgls for non-embed sgl mbox command */
840 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
841 list_splice_init(&prep_sblist, &blck_sblist);
842 post_cnt = block_cnt;
843 block_cnt = 0;
844 }
845 }
846 num_posting++;
847 last_xritag = psb->cur_iocbq.sli4_xritag;
848
849 /* end of repost sgl list condition for SCSI buffers */
850 if (num_posting == sb_count) {
851 if (post_cnt == 0) {
852 /* last sgl posting block */
853 list_splice_init(&prep_sblist, &blck_sblist);
854 post_cnt = block_cnt;
855 } else if (block_cnt == 1) {
856 /* last single sgl with non-contiguous xri */
James Smart96f70772013-04-17 20:16:15 -0400857 if (sgl_size > SGL_PAGE_SIZE)
James Smart8a9d2e82012-05-09 21:16:12 -0400858 pdma_phys_bpl1 = psb->dma_phys_bpl +
859 SGL_PAGE_SIZE;
860 else
861 pdma_phys_bpl1 = 0;
862 status = lpfc_sli4_post_sgl(phba,
863 psb->dma_phys_bpl,
864 pdma_phys_bpl1,
865 psb->cur_iocbq.sli4_xritag);
866 if (status) {
867 /* failure, put on abort scsi list */
868 psb->exch_busy = 1;
869 } else {
870 /* success, put on SCSI buffer list */
871 psb->exch_busy = 0;
872 psb->status = IOSTAT_SUCCESS;
873 num_posted++;
874 }
875 /* success, put on SCSI buffer sgl list */
876 list_add_tail(&psb->list, &scsi_sblist);
877 }
878 }
879
880 /* continue until a nembed page worth of sgls */
881 if (post_cnt == 0)
882 continue;
883
884 /* post block of SCSI buffer list sgls */
885 status = lpfc_sli4_post_scsi_sgl_block(phba, &blck_sblist,
886 post_cnt);
887
888 /* don't reset xirtag due to hole in xri block */
889 if (block_cnt == 0)
890 last_xritag = NO_XRI;
891
892 /* reset SCSI buffer post count for next round of posting */
893 post_cnt = 0;
894
895 /* put posted SCSI buffer-sgl posted on SCSI buffer sgl list */
896 while (!list_empty(&blck_sblist)) {
897 list_remove_head(&blck_sblist, psb,
898 struct lpfc_scsi_buf, list);
899 if (status) {
900 /* failure, put on abort scsi list */
901 psb->exch_busy = 1;
902 } else {
903 /* success, put on SCSI buffer list */
904 psb->exch_busy = 0;
905 psb->status = IOSTAT_SUCCESS;
906 num_posted++;
907 }
908 list_add_tail(&psb->list, &scsi_sblist);
909 }
910 }
911 /* Push SCSI buffers with sgl posted to the availble list */
912 while (!list_empty(&scsi_sblist)) {
913 list_remove_head(&scsi_sblist, psb,
914 struct lpfc_scsi_buf, list);
915 lpfc_release_scsi_buf_s4(phba, psb);
916 }
917 return num_posted;
918}
919
920/**
921 * lpfc_sli4_repost_scsi_sgl_list - Repsot all the allocated scsi buffer sgls
James Smartda0436e2009-05-22 14:51:39 -0400922 * @phba: pointer to lpfc hba data structure.
923 *
924 * This routine walks the list of scsi buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -0400925 * repost them to the port by using SGL block post. This is needed after a
James Smartda0436e2009-05-22 14:51:39 -0400926 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
927 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
928 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
929 *
930 * Returns: 0 = success, non-zero failure.
931 **/
932int
933lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
934{
James Smart8a9d2e82012-05-09 21:16:12 -0400935 LIST_HEAD(post_sblist);
936 int num_posted, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -0400937
James Smart8a9d2e82012-05-09 21:16:12 -0400938 /* get all SCSI buffers need to repost to a local list */
James Smarta40fc5f2013-04-17 20:17:40 -0400939 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -0400940 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400941 list_splice_init(&phba->lpfc_scsi_buf_list_get, &post_sblist);
942 list_splice(&phba->lpfc_scsi_buf_list_put, &post_sblist);
James Smart164cecd2013-09-06 12:22:38 -0400943 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400944 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smartda0436e2009-05-22 14:51:39 -0400945
James Smart8a9d2e82012-05-09 21:16:12 -0400946 /* post the list of scsi buffer sgls to port if available */
947 if (!list_empty(&post_sblist)) {
948 num_posted = lpfc_sli4_post_scsi_sgl_list(phba, &post_sblist,
949 phba->sli4_hba.scsi_xri_cnt);
950 /* failed to post any scsi buffer, return error */
951 if (num_posted == 0)
952 rc = -EIO;
James Smartda0436e2009-05-22 14:51:39 -0400953 }
954 return rc;
955}
956
957/**
958 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
959 * @vport: The virtual port for which this call being executed.
960 * @num_to_allocate: The requested number of buffers to allocate.
961 *
James Smart8a9d2e82012-05-09 21:16:12 -0400962 * This routine allocates scsi buffers for device with SLI-4 interface spec,
James Smartda0436e2009-05-22 14:51:39 -0400963 * the scsi buffer contains all the necessary information needed to initiate
James Smart8a9d2e82012-05-09 21:16:12 -0400964 * a SCSI I/O. After allocating up to @num_to_allocate SCSI buffers and put
965 * them on a list, it post them to the port by using SGL block post.
James Smartda0436e2009-05-22 14:51:39 -0400966 *
967 * Return codes:
James Smart8a9d2e82012-05-09 21:16:12 -0400968 * int - number of scsi buffers that were allocated and posted.
James Smartda0436e2009-05-22 14:51:39 -0400969 * 0 = failure, less than num_to_alloc is a partial failure.
970 **/
971static int
972lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
973{
974 struct lpfc_hba *phba = vport->phba;
975 struct lpfc_scsi_buf *psb;
976 struct sli4_sge *sgl;
977 IOCB_t *iocb;
978 dma_addr_t pdma_phys_fcp_cmd;
979 dma_addr_t pdma_phys_fcp_rsp;
James Smart96f70772013-04-17 20:16:15 -0400980 dma_addr_t pdma_phys_bpl;
James Smart8a9d2e82012-05-09 21:16:12 -0400981 uint16_t iotag, lxri = 0;
James Smart96f70772013-04-17 20:16:15 -0400982 int bcnt, num_posted, sgl_size;
James Smart8a9d2e82012-05-09 21:16:12 -0400983 LIST_HEAD(prep_sblist);
984 LIST_HEAD(post_sblist);
985 LIST_HEAD(scsi_sblist);
James Smartda0436e2009-05-22 14:51:39 -0400986
James Smart96f70772013-04-17 20:16:15 -0400987 sgl_size = phba->cfg_sg_dma_buf_size -
988 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
989
990 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
991 "9068 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
992 num_to_alloc, phba->cfg_sg_dma_buf_size, sgl_size,
993 (int)sizeof(struct fcp_cmnd),
994 (int)sizeof(struct fcp_rsp));
995
James Smartda0436e2009-05-22 14:51:39 -0400996 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
997 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
998 if (!psb)
999 break;
James Smartda0436e2009-05-22 14:51:39 -04001000 /*
James Smart8a9d2e82012-05-09 21:16:12 -04001001 * Get memory from the pci pool to map the virt space to
1002 * pci bus space for an I/O. The DMA buffer includes space
1003 * for the struct fcp_cmnd, struct fcp_rsp and the number
1004 * of bde's necessary to support the sg_tablesize.
James Smartda0436e2009-05-22 14:51:39 -04001005 */
1006 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
1007 GFP_KERNEL, &psb->dma_handle);
1008 if (!psb->data) {
1009 kfree(psb);
1010 break;
1011 }
James Smartda0436e2009-05-22 14:51:39 -04001012 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
1013
James Smart092cb032013-09-06 12:21:50 -04001014 /*
1015 * 4K Page alignment is CRITICAL to BlockGuard, double check
1016 * to be sure.
1017 */
1018 if (phba->cfg_enable_bg && (((unsigned long)(psb->data) &
1019 (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0)) {
James Smart96f70772013-04-17 20:16:15 -04001020 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
1021 psb->data, psb->dma_handle);
1022 kfree(psb);
1023 break;
1024 }
1025
James Smartda0436e2009-05-22 14:51:39 -04001026
James Smart6d368e52011-05-24 11:44:12 -04001027 lxri = lpfc_sli4_next_xritag(phba);
1028 if (lxri == NO_XRI) {
James Smartda0436e2009-05-22 14:51:39 -04001029 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
1030 psb->data, psb->dma_handle);
1031 kfree(psb);
1032 break;
1033 }
James Smartf7bc6432013-10-10 12:19:53 -04001034
1035 /* Allocate iotag for psb->cur_iocbq. */
1036 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
1037 if (iotag == 0) {
1038 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
1039 psb->data, psb->dma_handle);
1040 kfree(psb);
1041 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1042 "3368 Failed to allocated IOTAG for"
1043 " XRI:0x%x\n", lxri);
1044 lpfc_sli4_free_xri(phba, lxri);
1045 break;
1046 }
James Smart6d368e52011-05-24 11:44:12 -04001047 psb->cur_iocbq.sli4_lxritag = lxri;
1048 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smartda0436e2009-05-22 14:51:39 -04001049 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Smartda0436e2009-05-22 14:51:39 -04001050 psb->fcp_bpl = psb->data;
James Smart96f70772013-04-17 20:16:15 -04001051 psb->fcp_cmnd = (psb->data + sgl_size);
James Smartda0436e2009-05-22 14:51:39 -04001052 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
1053 sizeof(struct fcp_cmnd));
1054
1055 /* Initialize local short-hand pointers. */
1056 sgl = (struct sli4_sge *)psb->fcp_bpl;
1057 pdma_phys_bpl = psb->dma_handle;
James Smart96f70772013-04-17 20:16:15 -04001058 pdma_phys_fcp_cmd = (psb->dma_handle + sgl_size);
James Smartda0436e2009-05-22 14:51:39 -04001059 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
1060
1061 /*
James Smart8a9d2e82012-05-09 21:16:12 -04001062 * The first two bdes are the FCP_CMD and FCP_RSP.
1063 * The balance are sg list bdes. Initialize the
1064 * first two and leave the rest for queuecommand.
James Smartda0436e2009-05-22 14:51:39 -04001065 */
1066 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
1067 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
James Smart05580562011-05-24 11:40:48 -04001068 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04001069 bf_set(lpfc_sli4_sge_last, sgl, 0);
1070 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05001071 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
James Smartda0436e2009-05-22 14:51:39 -04001072 sgl++;
1073
1074 /* Setup the physical region for the FCP RSP */
1075 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
1076 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smart05580562011-05-24 11:40:48 -04001077 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04001078 bf_set(lpfc_sli4_sge_last, sgl, 1);
1079 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05001080 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -04001081
1082 /*
1083 * Since the IOCB for the FCP I/O is built into this
1084 * lpfc_scsi_buf, initialize it with all known data now.
1085 */
1086 iocb = &psb->cur_iocbq.iocb;
1087 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
1088 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
1089 /* setting the BLP size to 2 * sizeof BDE may not be correct.
1090 * We are setting the bpl to point to out sgl. An sgl's
1091 * entries are 16 bytes, a bpl entries are 12 bytes.
1092 */
1093 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
1094 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
1095 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
1096 iocb->ulpBdeCount = 1;
1097 iocb->ulpLe = 1;
1098 iocb->ulpClass = CLASS3;
James Smart8a9d2e82012-05-09 21:16:12 -04001099 psb->cur_iocbq.context1 = psb;
James Smartda0436e2009-05-22 14:51:39 -04001100 psb->dma_phys_bpl = pdma_phys_bpl;
James Smart6d368e52011-05-24 11:44:12 -04001101
James Smart8a9d2e82012-05-09 21:16:12 -04001102 /* add the scsi buffer to a post list */
1103 list_add_tail(&psb->list, &post_sblist);
James Smarta40fc5f2013-04-17 20:17:40 -04001104 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04001105 phba->sli4_hba.scsi_xri_cnt++;
James Smarta40fc5f2013-04-17 20:17:40 -04001106 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smartda0436e2009-05-22 14:51:39 -04001107 }
James Smart8a9d2e82012-05-09 21:16:12 -04001108 lpfc_printf_log(phba, KERN_INFO, LOG_BG,
1109 "3021 Allocate %d out of %d requested new SCSI "
1110 "buffers\n", bcnt, num_to_alloc);
James Smartda0436e2009-05-22 14:51:39 -04001111
James Smart8a9d2e82012-05-09 21:16:12 -04001112 /* post the list of scsi buffer sgls to port if available */
1113 if (!list_empty(&post_sblist))
1114 num_posted = lpfc_sli4_post_scsi_sgl_list(phba,
1115 &post_sblist, bcnt);
1116 else
1117 num_posted = 0;
1118
1119 return num_posted;
James Smartda0436e2009-05-22 14:51:39 -04001120}
1121
1122/**
James Smart3772a992009-05-22 14:50:54 -04001123 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
1124 * @vport: The virtual port for which this call being executed.
1125 * @num_to_allocate: The requested number of buffers to allocate.
1126 *
1127 * This routine wraps the actual SCSI buffer allocator function pointer from
1128 * the lpfc_hba struct.
1129 *
1130 * Return codes:
1131 * int - number of scsi buffers that were allocated.
1132 * 0 = failure, less than num_to_alloc is a partial failure.
1133 **/
1134static inline int
1135lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
1136{
1137 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
1138}
1139
1140/**
James Smart19ca7602010-11-20 23:11:55 -05001141 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -04001142 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05001143 *
1144 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1145 * and returns to caller.
1146 *
1147 * Return codes:
1148 * NULL - Error
1149 * Pointer to lpfc_scsi_buf - Success
1150 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +01001151static struct lpfc_scsi_buf*
James Smart19ca7602010-11-20 23:11:55 -05001152lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001153{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001154 struct lpfc_scsi_buf * lpfc_cmd = NULL;
James Smarta40fc5f2013-04-17 20:17:40 -04001155 struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
James Smart164cecd2013-09-06 12:22:38 -04001156 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001157
James Smart164cecd2013-09-06 12:22:38 -04001158 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
James Smarta40fc5f2013-04-17 20:17:40 -04001159 list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_scsi_buf,
1160 list);
1161 if (!lpfc_cmd) {
James Smart164cecd2013-09-06 12:22:38 -04001162 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04001163 list_splice(&phba->lpfc_scsi_buf_list_put,
1164 &phba->lpfc_scsi_buf_list_get);
1165 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
1166 list_remove_head(scsi_buf_list_get, lpfc_cmd,
1167 struct lpfc_scsi_buf, list);
James Smart164cecd2013-09-06 12:22:38 -04001168 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smart1dcb58e2007-04-25 09:51:30 -04001169 }
James Smart164cecd2013-09-06 12:22:38 -04001170 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001171 return lpfc_cmd;
1172}
James Smart19ca7602010-11-20 23:11:55 -05001173/**
1174 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1175 * @phba: The HBA for which this call is being executed.
1176 *
1177 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1178 * and returns to caller.
1179 *
1180 * Return codes:
1181 * NULL - Error
1182 * Pointer to lpfc_scsi_buf - Success
1183 **/
1184static struct lpfc_scsi_buf*
1185lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1186{
James Smart3be30e02013-05-31 17:05:17 -04001187 struct lpfc_scsi_buf *lpfc_cmd, *lpfc_cmd_next;
James Smart164cecd2013-09-06 12:22:38 -04001188 unsigned long iflag = 0;
James Smart19ca7602010-11-20 23:11:55 -05001189 int found = 0;
1190
James Smart164cecd2013-09-06 12:22:38 -04001191 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
James Smart3be30e02013-05-31 17:05:17 -04001192 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1193 &phba->lpfc_scsi_buf_list_get, list) {
James Smart19ca7602010-11-20 23:11:55 -05001194 if (lpfc_test_rrq_active(phba, ndlp,
James Smartee0f4fe2012-05-09 21:19:14 -04001195 lpfc_cmd->cur_iocbq.sli4_lxritag))
James Smart1151e3e2011-02-16 12:39:35 -05001196 continue;
1197 list_del(&lpfc_cmd->list);
James Smart19ca7602010-11-20 23:11:55 -05001198 found = 1;
James Smart1151e3e2011-02-16 12:39:35 -05001199 break;
James Smart19ca7602010-11-20 23:11:55 -05001200 }
James Smarta40fc5f2013-04-17 20:17:40 -04001201 if (!found) {
James Smart164cecd2013-09-06 12:22:38 -04001202 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04001203 list_splice(&phba->lpfc_scsi_buf_list_put,
1204 &phba->lpfc_scsi_buf_list_get);
1205 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart164cecd2013-09-06 12:22:38 -04001206 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smart3be30e02013-05-31 17:05:17 -04001207 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1208 &phba->lpfc_scsi_buf_list_get, list) {
James Smarta40fc5f2013-04-17 20:17:40 -04001209 if (lpfc_test_rrq_active(
1210 phba, ndlp, lpfc_cmd->cur_iocbq.sli4_lxritag))
1211 continue;
1212 list_del(&lpfc_cmd->list);
1213 found = 1;
1214 break;
1215 }
1216 }
James Smart164cecd2013-09-06 12:22:38 -04001217 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -05001218 if (!found)
1219 return NULL;
James Smarta40fc5f2013-04-17 20:17:40 -04001220 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001221}
1222/**
1223 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1224 * @phba: The HBA for which this call is being executed.
1225 *
1226 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1227 * and returns to caller.
1228 *
1229 * Return codes:
1230 * NULL - Error
1231 * Pointer to lpfc_scsi_buf - Success
1232 **/
1233static struct lpfc_scsi_buf*
1234lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1235{
1236 return phba->lpfc_get_scsi_buf(phba, ndlp);
1237}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001238
James Smart9bad7672008-12-04 22:39:02 -05001239/**
James Smart3772a992009-05-22 14:50:54 -04001240 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -05001241 * @phba: The Hba for which this call is being executed.
1242 * @psb: The scsi buffer which is being released.
1243 *
1244 * This routine releases @psb scsi buffer by adding it to tail of @phba
1245 * lpfc_scsi_buf_list list.
1246 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001247static void
James Smart3772a992009-05-22 14:50:54 -04001248lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001249{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001250 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001251
James Smarta40fc5f2013-04-17 20:17:40 -04001252 psb->seg_cnt = 0;
1253 psb->nonsg_phys = 0;
1254 psb->prot_seg_cnt = 0;
1255
1256 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001257 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -04001258 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -04001259 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1260 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
dea31012005-04-17 16:05:31 -05001261}
1262
James Smart9bad7672008-12-04 22:39:02 -05001263/**
James Smartda0436e2009-05-22 14:51:39 -04001264 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1265 * @phba: The Hba for which this call is being executed.
1266 * @psb: The scsi buffer which is being released.
1267 *
1268 * This routine releases @psb scsi buffer by adding it to tail of @phba
1269 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1270 * and cannot be reused for at least RA_TOV amount of time if it was
1271 * aborted.
1272 **/
1273static void
1274lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1275{
1276 unsigned long iflag = 0;
1277
James Smarta40fc5f2013-04-17 20:17:40 -04001278 psb->seg_cnt = 0;
1279 psb->nonsg_phys = 0;
1280 psb->prot_seg_cnt = 0;
1281
James Smart341af102010-01-26 23:07:37 -05001282 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -04001283 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1284 iflag);
1285 psb->pCmd = NULL;
1286 list_add_tail(&psb->list,
1287 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1288 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1289 iflag);
1290 } else {
James Smartda0436e2009-05-22 14:51:39 -04001291 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -04001292 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -04001293 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1294 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1295 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -04001296 }
1297}
1298
1299/**
James Smart3772a992009-05-22 14:50:54 -04001300 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1301 * @phba: The Hba for which this call is being executed.
1302 * @psb: The scsi buffer which is being released.
1303 *
1304 * This routine releases @psb scsi buffer by adding it to tail of @phba
1305 * lpfc_scsi_buf_list list.
1306 **/
1307static void
1308lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1309{
1310
1311 phba->lpfc_release_scsi_buf(phba, psb);
1312}
1313
1314/**
1315 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001316 * @phba: The Hba for which this call is being executed.
1317 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1318 *
1319 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001320 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1321 * through sg elements and format the bdea. This routine also initializes all
1322 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001323 *
1324 * Return codes:
1325 * 1 - Error
1326 * 0 - Success
1327 **/
dea31012005-04-17 16:05:31 -05001328static int
James Smart3772a992009-05-22 14:50:54 -04001329lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001330{
1331 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1332 struct scatterlist *sgel = NULL;
1333 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1334 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001335 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001336 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001337 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001338 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001339 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001340 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001341
1342 /*
1343 * There are three possibilities here - use scatter-gather segment, use
1344 * the single mapping, or neither. Start the lpfc command prep by
1345 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1346 * data bde entry.
1347 */
1348 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001349 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001350 /*
1351 * The driver stores the segment count returned from pci_map_sg
1352 * because this a count of dma-mappings used to map the use_sg
1353 * pages. They are not guaranteed to be the same for those
1354 * architectures that implement an IOMMU.
1355 */
dea31012005-04-17 16:05:31 -05001356
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001357 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1358 scsi_sg_count(scsi_cmnd), datadir);
1359 if (unlikely(!nseg))
1360 return 1;
1361
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001362 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001363 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001364 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1365 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001366 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001367 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001368 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04001369 lpfc_cmd->seg_cnt = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001370 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001371 return 1;
1372 }
1373
1374 /*
1375 * The driver established a maximum scatter-gather segment count
1376 * during probe that limits the number of sg elements in any
1377 * single scsi command. Just run through the seg_cnt and format
1378 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001379 * When using SLI-3 the driver will try to fit all the BDEs into
1380 * the IOCB. If it can't then the BDEs get added to a BPL as it
1381 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001382 */
James Smart34b02dc2008-08-24 21:49:55 -04001383 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001384 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001385 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001386 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001387 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001388 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1389 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1390 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1391 data_bde->addrLow = putPaddrLow(physaddr);
1392 data_bde->addrHigh = putPaddrHigh(physaddr);
1393 data_bde++;
1394 } else {
1395 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1396 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1397 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1398 bpl->addrLow =
1399 le32_to_cpu(putPaddrLow(physaddr));
1400 bpl->addrHigh =
1401 le32_to_cpu(putPaddrHigh(physaddr));
1402 bpl++;
1403 }
dea31012005-04-17 16:05:31 -05001404 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001405 }
dea31012005-04-17 16:05:31 -05001406
1407 /*
1408 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001409 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1410 * explicitly reinitialized and for SLI-3 the extended bde count is
1411 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001412 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001413 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001414 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1415 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001416 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1417 /*
1418 * The extended IOCB format can only fit 3 BDE or a BPL.
1419 * This I/O has more than 3 BDE so the 1st data bde will
1420 * be a BPL that is filled in here.
1421 */
1422 physaddr = lpfc_cmd->dma_handle;
1423 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1424 data_bde->tus.f.bdeSize = (num_bde *
1425 sizeof(struct ulp_bde64));
1426 physaddr += (sizeof(struct fcp_cmnd) +
1427 sizeof(struct fcp_rsp) +
1428 (2 * sizeof(struct ulp_bde64)));
1429 data_bde->addrHigh = putPaddrHigh(physaddr);
1430 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001431 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -04001432 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1433 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001434 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -04001435 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1436 }
1437 } else {
1438 iocb_cmd->un.fcpi64.bdl.bdeSize =
1439 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001440 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001441 }
James Smart09372822008-01-11 01:52:54 -05001442 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001443
1444 /*
1445 * Due to difference in data length between DIF/non-DIF paths,
1446 * we need to set word 4 of IOCB here
1447 */
James Smarta257bf92009-04-06 18:48:10 -04001448 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001449 return 0;
1450}
1451
James Smartf9bb2da2011-10-10 21:34:11 -04001452#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001453
James Smart9a6b09c2012-03-01 22:37:42 -05001454/* Return if if error injection is detected by Initiator */
1455#define BG_ERR_INIT 0x1
1456/* Return if if error injection is detected by Target */
1457#define BG_ERR_TGT 0x2
1458/* Return if if swapping CSUM<-->CRC is required for error injection */
1459#define BG_ERR_SWAP 0x10
1460/* Return if disabling Guard/Ref/App checking is required for error injection */
1461#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -05001462
1463/**
1464 * lpfc_bg_err_inject - Determine if we should inject an error
1465 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -04001466 * @sc: The SCSI command to examine
1467 * @reftag: (out) BlockGuard reference tag for transmitted data
1468 * @apptag: (out) BlockGuard application tag for transmitted data
1469 * @new_guard (in) Value to replace CRC with if needed
1470 *
James Smart9a6b09c2012-03-01 22:37:42 -05001471 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -05001472 **/
James Smartf9bb2da2011-10-10 21:34:11 -04001473static int
1474lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1475 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1476{
1477 struct scatterlist *sgpe; /* s/g prot entry */
1478 struct scatterlist *sgde; /* s/g data entry */
James Smart9a6b09c2012-03-01 22:37:42 -05001479 struct lpfc_scsi_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -05001480 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -05001481 struct lpfc_nodelist *ndlp;
1482 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -04001483 uint32_t op = scsi_get_prot_op(sc);
1484 uint32_t blksize;
1485 uint32_t numblks;
1486 sector_t lba;
1487 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001488 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001489
1490 if (op == SCSI_PROT_NORMAL)
1491 return 0;
1492
James Smartacd68592012-01-18 16:25:09 -05001493 sgpe = scsi_prot_sglist(sc);
1494 sgde = scsi_sglist(sc);
James Smartf9bb2da2011-10-10 21:34:11 -04001495 lba = scsi_get_lba(sc);
James Smart4ac9b222012-03-01 22:38:29 -05001496
1497 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001498 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1499 blksize = lpfc_cmd_blksize(sc);
1500 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1501
1502 /* Make sure we have the right LBA if one is specified */
1503 if ((phba->lpfc_injerr_lba < lba) ||
1504 (phba->lpfc_injerr_lba >= (lba + numblks)))
1505 return 0;
James Smartacd68592012-01-18 16:25:09 -05001506 if (sgpe) {
1507 blockoff = phba->lpfc_injerr_lba - lba;
1508 numblks = sg_dma_len(sgpe) /
1509 sizeof(struct scsi_dif_tuple);
1510 if (numblks < blockoff)
1511 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001512 }
James Smartf9bb2da2011-10-10 21:34:11 -04001513 }
1514
James Smart4ac9b222012-03-01 22:38:29 -05001515 /* Next check if we need to match the remote NPortID or WWPN */
James Smart1ba981f2014-02-20 09:56:45 -05001516 rdata = lpfc_rport_data_from_scsi_device(sc->device);
James Smart4ac9b222012-03-01 22:38:29 -05001517 if (rdata && rdata->pnode) {
1518 ndlp = rdata->pnode;
1519
1520 /* Make sure we have the right NPortID if one is specified */
1521 if (phba->lpfc_injerr_nportid &&
1522 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1523 return 0;
1524
1525 /*
1526 * Make sure we have the right WWPN if one is specified.
1527 * wwn[0] should be a non-zero NAA in a good WWPN.
1528 */
1529 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1530 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1531 sizeof(struct lpfc_name)) != 0))
1532 return 0;
1533 }
1534
1535 /* Setup a ptr to the protection data if the SCSI host provides it */
1536 if (sgpe) {
1537 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1538 src += blockoff;
1539 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1540 }
1541
James Smartf9bb2da2011-10-10 21:34:11 -04001542 /* Should we change the Reference Tag */
1543 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001544 if (phba->lpfc_injerr_wref_cnt) {
1545 switch (op) {
1546 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001547 if (src) {
1548 /*
1549 * For WRITE_PASS, force the error
1550 * to be sent on the wire. It should
1551 * be detected by the Target.
1552 * If blockoff != 0 error will be
1553 * inserted in middle of the IO.
1554 */
James Smartf9bb2da2011-10-10 21:34:11 -04001555
James Smartacd68592012-01-18 16:25:09 -05001556 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1557 "9076 BLKGRD: Injecting reftag error: "
1558 "write lba x%lx + x%x oldrefTag x%x\n",
1559 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001560 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001561
1562 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001563 * Save the old ref_tag so we can
1564 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001565 */
James Smart9a6b09c2012-03-01 22:37:42 -05001566 if (lpfc_cmd) {
1567 lpfc_cmd->prot_data_type =
1568 LPFC_INJERR_REFTAG;
1569 lpfc_cmd->prot_data_segment =
1570 src;
1571 lpfc_cmd->prot_data =
1572 src->ref_tag;
1573 }
1574 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001575 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001576 if (phba->lpfc_injerr_wref_cnt == 0) {
1577 phba->lpfc_injerr_nportid = 0;
1578 phba->lpfc_injerr_lba =
1579 LPFC_INJERR_LBA_OFF;
1580 memset(&phba->lpfc_injerr_wwpn,
1581 0, sizeof(struct lpfc_name));
1582 }
James Smart9a6b09c2012-03-01 22:37:42 -05001583 rc = BG_ERR_TGT | BG_ERR_CHECK;
1584
James Smartacd68592012-01-18 16:25:09 -05001585 break;
1586 }
1587 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001588 case SCSI_PROT_WRITE_INSERT:
1589 /*
1590 * For WRITE_INSERT, force the error
1591 * to be sent on the wire. It should be
1592 * detected by the Target.
1593 */
1594 /* DEADBEEF will be the reftag on the wire */
1595 *reftag = 0xDEADBEEF;
1596 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001597 if (phba->lpfc_injerr_wref_cnt == 0) {
1598 phba->lpfc_injerr_nportid = 0;
1599 phba->lpfc_injerr_lba =
1600 LPFC_INJERR_LBA_OFF;
1601 memset(&phba->lpfc_injerr_wwpn,
1602 0, sizeof(struct lpfc_name));
1603 }
James Smart9a6b09c2012-03-01 22:37:42 -05001604 rc = BG_ERR_TGT | BG_ERR_CHECK;
1605
1606 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1607 "9078 BLKGRD: Injecting reftag error: "
1608 "write lba x%lx\n", (unsigned long)lba);
1609 break;
James Smartacd68592012-01-18 16:25:09 -05001610 case SCSI_PROT_WRITE_STRIP:
1611 /*
1612 * For WRITE_STRIP and WRITE_PASS,
1613 * force the error on data
1614 * being copied from SLI-Host to SLI-Port.
1615 */
1616 *reftag = 0xDEADBEEF;
1617 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001618 if (phba->lpfc_injerr_wref_cnt == 0) {
1619 phba->lpfc_injerr_nportid = 0;
1620 phba->lpfc_injerr_lba =
1621 LPFC_INJERR_LBA_OFF;
1622 memset(&phba->lpfc_injerr_wwpn,
1623 0, sizeof(struct lpfc_name));
1624 }
James Smartacd68592012-01-18 16:25:09 -05001625 rc = BG_ERR_INIT;
1626
1627 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1628 "9077 BLKGRD: Injecting reftag error: "
1629 "write lba x%lx\n", (unsigned long)lba);
1630 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001631 }
James Smartacd68592012-01-18 16:25:09 -05001632 }
1633 if (phba->lpfc_injerr_rref_cnt) {
1634 switch (op) {
1635 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001636 case SCSI_PROT_READ_STRIP:
1637 case SCSI_PROT_READ_PASS:
1638 /*
1639 * For READ_STRIP and READ_PASS, force the
1640 * error on data being read off the wire. It
1641 * should force an IO error to the driver.
1642 */
James Smartf9bb2da2011-10-10 21:34:11 -04001643 *reftag = 0xDEADBEEF;
1644 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001645 if (phba->lpfc_injerr_rref_cnt == 0) {
1646 phba->lpfc_injerr_nportid = 0;
1647 phba->lpfc_injerr_lba =
1648 LPFC_INJERR_LBA_OFF;
1649 memset(&phba->lpfc_injerr_wwpn,
1650 0, sizeof(struct lpfc_name));
1651 }
James Smartacd68592012-01-18 16:25:09 -05001652 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001653
1654 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001655 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001656 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001657 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001658 }
1659 }
1660 }
1661
1662 /* Should we change the Application Tag */
1663 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001664 if (phba->lpfc_injerr_wapp_cnt) {
1665 switch (op) {
1666 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001667 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001668 /*
1669 * For WRITE_PASS, force the error
1670 * to be sent on the wire. It should
1671 * be detected by the Target.
1672 * If blockoff != 0 error will be
1673 * inserted in middle of the IO.
1674 */
1675
James Smartacd68592012-01-18 16:25:09 -05001676 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1677 "9080 BLKGRD: Injecting apptag error: "
1678 "write lba x%lx + x%x oldappTag x%x\n",
1679 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001680 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001681
1682 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001683 * Save the old app_tag so we can
1684 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001685 */
James Smart9a6b09c2012-03-01 22:37:42 -05001686 if (lpfc_cmd) {
1687 lpfc_cmd->prot_data_type =
1688 LPFC_INJERR_APPTAG;
1689 lpfc_cmd->prot_data_segment =
1690 src;
1691 lpfc_cmd->prot_data =
1692 src->app_tag;
1693 }
1694 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001695 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001696 if (phba->lpfc_injerr_wapp_cnt == 0) {
1697 phba->lpfc_injerr_nportid = 0;
1698 phba->lpfc_injerr_lba =
1699 LPFC_INJERR_LBA_OFF;
1700 memset(&phba->lpfc_injerr_wwpn,
1701 0, sizeof(struct lpfc_name));
1702 }
James Smart9a6b09c2012-03-01 22:37:42 -05001703 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001704 break;
1705 }
1706 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001707 case SCSI_PROT_WRITE_INSERT:
1708 /*
1709 * For WRITE_INSERT, force the
1710 * error to be sent on the wire. It should be
1711 * detected by the Target.
1712 */
1713 /* DEAD will be the apptag on the wire */
1714 *apptag = 0xDEAD;
1715 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001716 if (phba->lpfc_injerr_wapp_cnt == 0) {
1717 phba->lpfc_injerr_nportid = 0;
1718 phba->lpfc_injerr_lba =
1719 LPFC_INJERR_LBA_OFF;
1720 memset(&phba->lpfc_injerr_wwpn,
1721 0, sizeof(struct lpfc_name));
1722 }
James Smart9a6b09c2012-03-01 22:37:42 -05001723 rc = BG_ERR_TGT | BG_ERR_CHECK;
1724
1725 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1726 "0813 BLKGRD: Injecting apptag error: "
1727 "write lba x%lx\n", (unsigned long)lba);
1728 break;
James Smartacd68592012-01-18 16:25:09 -05001729 case SCSI_PROT_WRITE_STRIP:
1730 /*
1731 * For WRITE_STRIP and WRITE_PASS,
1732 * force the error on data
1733 * being copied from SLI-Host to SLI-Port.
1734 */
1735 *apptag = 0xDEAD;
1736 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001737 if (phba->lpfc_injerr_wapp_cnt == 0) {
1738 phba->lpfc_injerr_nportid = 0;
1739 phba->lpfc_injerr_lba =
1740 LPFC_INJERR_LBA_OFF;
1741 memset(&phba->lpfc_injerr_wwpn,
1742 0, sizeof(struct lpfc_name));
1743 }
James Smartacd68592012-01-18 16:25:09 -05001744 rc = BG_ERR_INIT;
1745
1746 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1747 "0812 BLKGRD: Injecting apptag error: "
1748 "write lba x%lx\n", (unsigned long)lba);
1749 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001750 }
James Smartacd68592012-01-18 16:25:09 -05001751 }
1752 if (phba->lpfc_injerr_rapp_cnt) {
1753 switch (op) {
1754 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001755 case SCSI_PROT_READ_STRIP:
1756 case SCSI_PROT_READ_PASS:
1757 /*
1758 * For READ_STRIP and READ_PASS, force the
1759 * error on data being read off the wire. It
1760 * should force an IO error to the driver.
1761 */
James Smartf9bb2da2011-10-10 21:34:11 -04001762 *apptag = 0xDEAD;
1763 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001764 if (phba->lpfc_injerr_rapp_cnt == 0) {
1765 phba->lpfc_injerr_nportid = 0;
1766 phba->lpfc_injerr_lba =
1767 LPFC_INJERR_LBA_OFF;
1768 memset(&phba->lpfc_injerr_wwpn,
1769 0, sizeof(struct lpfc_name));
1770 }
James Smartacd68592012-01-18 16:25:09 -05001771 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001772
1773 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001774 "0814 BLKGRD: Injecting apptag error: "
1775 "read lba x%lx\n", (unsigned long)lba);
1776 break;
1777 }
1778 }
1779 }
1780
1781
1782 /* Should we change the Guard Tag */
1783 if (new_guard) {
1784 if (phba->lpfc_injerr_wgrd_cnt) {
1785 switch (op) {
1786 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001787 rc = BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001788 /* Drop thru */
James Smartacd68592012-01-18 16:25:09 -05001789
James Smartacd68592012-01-18 16:25:09 -05001790 case SCSI_PROT_WRITE_INSERT:
1791 /*
1792 * For WRITE_INSERT, force the
1793 * error to be sent on the wire. It should be
1794 * detected by the Target.
1795 */
1796 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001797 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1798 phba->lpfc_injerr_nportid = 0;
1799 phba->lpfc_injerr_lba =
1800 LPFC_INJERR_LBA_OFF;
1801 memset(&phba->lpfc_injerr_wwpn,
1802 0, sizeof(struct lpfc_name));
1803 }
James Smartacd68592012-01-18 16:25:09 -05001804
James Smart9a6b09c2012-03-01 22:37:42 -05001805 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001806 /* Signals the caller to swap CRC->CSUM */
1807
1808 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1809 "0817 BLKGRD: Injecting guard error: "
1810 "write lba x%lx\n", (unsigned long)lba);
1811 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001812 case SCSI_PROT_WRITE_STRIP:
1813 /*
1814 * For WRITE_STRIP and WRITE_PASS,
1815 * force the error on data
1816 * being copied from SLI-Host to SLI-Port.
1817 */
1818 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001819 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1820 phba->lpfc_injerr_nportid = 0;
1821 phba->lpfc_injerr_lba =
1822 LPFC_INJERR_LBA_OFF;
1823 memset(&phba->lpfc_injerr_wwpn,
1824 0, sizeof(struct lpfc_name));
1825 }
James Smart9a6b09c2012-03-01 22:37:42 -05001826
1827 rc = BG_ERR_INIT | BG_ERR_SWAP;
1828 /* Signals the caller to swap CRC->CSUM */
1829
1830 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1831 "0816 BLKGRD: Injecting guard error: "
1832 "write lba x%lx\n", (unsigned long)lba);
1833 break;
James Smartacd68592012-01-18 16:25:09 -05001834 }
1835 }
1836 if (phba->lpfc_injerr_rgrd_cnt) {
1837 switch (op) {
1838 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001839 case SCSI_PROT_READ_STRIP:
1840 case SCSI_PROT_READ_PASS:
1841 /*
1842 * For READ_STRIP and READ_PASS, force the
1843 * error on data being read off the wire. It
1844 * should force an IO error to the driver.
1845 */
James Smartacd68592012-01-18 16:25:09 -05001846 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001847 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1848 phba->lpfc_injerr_nportid = 0;
1849 phba->lpfc_injerr_lba =
1850 LPFC_INJERR_LBA_OFF;
1851 memset(&phba->lpfc_injerr_wwpn,
1852 0, sizeof(struct lpfc_name));
1853 }
James Smartacd68592012-01-18 16:25:09 -05001854
James Smart9a6b09c2012-03-01 22:37:42 -05001855 rc = BG_ERR_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001856 /* Signals the caller to swap CRC->CSUM */
1857
1858 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1859 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001860 "read lba x%lx\n", (unsigned long)lba);
1861 }
1862 }
1863 }
1864
James Smartf9bb2da2011-10-10 21:34:11 -04001865 return rc;
1866}
1867#endif
1868
James Smartacd68592012-01-18 16:25:09 -05001869/**
1870 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1871 * the specified SCSI command.
1872 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001873 * @sc: The SCSI command to examine
1874 * @txopt: (out) BlockGuard operation for transmitted data
1875 * @rxopt: (out) BlockGuard operation for received data
1876 *
1877 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1878 *
James Smartacd68592012-01-18 16:25:09 -05001879 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001880static int
James Smart6c8eea52010-04-06 14:49:53 -04001881lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1882 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001883{
James Smart6c8eea52010-04-06 14:49:53 -04001884 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001885
James Smart9c6aa9d2013-05-31 17:03:39 -04001886 if (lpfc_cmd_guard_csum(sc)) {
James Smarte2a0a9d2008-12-04 22:40:02 -05001887 switch (scsi_get_prot_op(sc)) {
1888 case SCSI_PROT_READ_INSERT:
1889 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001890 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001891 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001892 break;
1893
1894 case SCSI_PROT_READ_STRIP:
1895 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001896 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001897 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001898 break;
1899
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001900 case SCSI_PROT_READ_PASS:
1901 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001902 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001903 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001904 break;
1905
James Smarte2a0a9d2008-12-04 22:40:02 -05001906 case SCSI_PROT_NORMAL:
1907 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001908 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001909 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1910 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001911 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001912 break;
1913
1914 }
James Smart7c56b9f2011-07-22 18:36:25 -04001915 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001916 switch (scsi_get_prot_op(sc)) {
1917 case SCSI_PROT_READ_STRIP:
1918 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001919 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001920 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001921 break;
1922
1923 case SCSI_PROT_READ_PASS:
1924 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001925 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001926 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001927 break;
1928
James Smarte2a0a9d2008-12-04 22:40:02 -05001929 case SCSI_PROT_READ_INSERT:
1930 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001931 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001932 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001933 break;
1934
James Smarte2a0a9d2008-12-04 22:40:02 -05001935 case SCSI_PROT_NORMAL:
1936 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001937 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001938 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1939 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001940 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001941 break;
1942 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001943 }
1944
James Smart6c8eea52010-04-06 14:49:53 -04001945 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001946}
1947
James Smartacd68592012-01-18 16:25:09 -05001948#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1949/**
1950 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1951 * the specified SCSI command in order to force a guard tag error.
1952 * @phba: The Hba for which this call is being executed.
1953 * @sc: The SCSI command to examine
1954 * @txopt: (out) BlockGuard operation for transmitted data
1955 * @rxopt: (out) BlockGuard operation for received data
1956 *
1957 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1958 *
1959 **/
1960static int
1961lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1962 uint8_t *txop, uint8_t *rxop)
1963{
James Smartacd68592012-01-18 16:25:09 -05001964 uint8_t ret = 0;
1965
James Smart9c6aa9d2013-05-31 17:03:39 -04001966 if (lpfc_cmd_guard_csum(sc)) {
James Smartacd68592012-01-18 16:25:09 -05001967 switch (scsi_get_prot_op(sc)) {
1968 case SCSI_PROT_READ_INSERT:
1969 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001970 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001971 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001972 break;
1973
1974 case SCSI_PROT_READ_STRIP:
1975 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001976 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001977 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001978 break;
1979
1980 case SCSI_PROT_READ_PASS:
1981 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001982 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001983 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001984 break;
1985
1986 case SCSI_PROT_NORMAL:
1987 default:
1988 break;
1989
1990 }
1991 } else {
1992 switch (scsi_get_prot_op(sc)) {
1993 case SCSI_PROT_READ_STRIP:
1994 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001995 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001996 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001997 break;
1998
1999 case SCSI_PROT_READ_PASS:
2000 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05002001 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05002002 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05002003 break;
2004
2005 case SCSI_PROT_READ_INSERT:
2006 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05002007 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05002008 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05002009 break;
2010
2011 case SCSI_PROT_NORMAL:
2012 default:
2013 break;
2014 }
2015 }
2016
2017 return ret;
2018}
2019#endif
2020
2021/**
2022 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
2023 * @phba: The Hba for which this call is being executed.
2024 * @sc: pointer to scsi command we're working on
2025 * @bpl: pointer to buffer list for protection groups
2026 * @datacnt: number of segments of data that have been dma mapped
2027 *
2028 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05002029 * type LPFC_PG_TYPE_NO_DIF
2030 *
2031 * This is usually used when the HBA is instructed to generate
2032 * DIFs and insert them into data stream (or strip DIF from
2033 * incoming data stream)
2034 *
2035 * The buffer list consists of just one protection group described
2036 * below:
2037 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002038 * start of prot group --> | PDE_5 |
2039 * +-------------------------+
2040 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002041 * +-------------------------+
2042 * | Data BDE |
2043 * +-------------------------+
2044 * |more Data BDE's ... (opt)|
2045 * +-------------------------+
2046 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002047 *
2048 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05002049 *
2050 * Returns the number of BDEs added to the BPL.
2051 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002052static int
2053lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2054 struct ulp_bde64 *bpl, int datasegcnt)
2055{
2056 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04002057 struct lpfc_pde5 *pde5 = NULL;
2058 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002059 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04002060 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002061 int datadir = sc->sc_data_direction;
James Smart0829a192012-05-09 21:18:12 -04002062#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002063 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002064#endif
James Smartacd68592012-01-18 16:25:09 -05002065 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002066 uint32_t reftag;
James Smart7c56b9f2011-07-22 18:36:25 -04002067 unsigned blksize;
James Smart6c8eea52010-04-06 14:49:53 -04002068 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002069
James Smart6c8eea52010-04-06 14:49:53 -04002070 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2071 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002072 goto out;
2073
James Smart6c8eea52010-04-06 14:49:53 -04002074 /* extract some info from the scsi command for pde*/
James Smarte2a0a9d2008-12-04 22:40:02 -05002075 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002076 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002077
James Smartf9bb2da2011-10-10 21:34:11 -04002078#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002079 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002080 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002081 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002082 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002083 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002084 checking = 0;
2085 }
James Smartf9bb2da2011-10-10 21:34:11 -04002086#endif
2087
James Smart6c8eea52010-04-06 14:49:53 -04002088 /* setup PDE5 with what we have */
2089 pde5 = (struct lpfc_pde5 *) bpl;
2090 memset(pde5, 0, sizeof(struct lpfc_pde5));
2091 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002092
James Smartbc739052010-08-04 16:11:18 -04002093 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002094 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002095 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002096
James Smart6c8eea52010-04-06 14:49:53 -04002097 /* advance bpl and increment bde count */
2098 num_bde++;
2099 bpl++;
2100 pde6 = (struct lpfc_pde6 *) bpl;
2101
2102 /* setup PDE6 with the rest of the info */
2103 memset(pde6, 0, sizeof(struct lpfc_pde6));
2104 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2105 bf_set(pde6_optx, pde6, txop);
2106 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002107
2108 /*
2109 * We only need to check the data on READs, for WRITEs
2110 * protection data is automatically generated, not checked.
2111 */
James Smart6c8eea52010-04-06 14:49:53 -04002112 if (datadir == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002113 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002114 bf_set(pde6_ce, pde6, checking);
2115 else
2116 bf_set(pde6_ce, pde6, 0);
2117
James Smart9c6aa9d2013-05-31 17:03:39 -04002118 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002119 bf_set(pde6_re, pde6, checking);
2120 else
2121 bf_set(pde6_re, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002122 }
2123 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002124 bf_set(pde6_ae, pde6, 0);
2125 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002126
James Smartbc739052010-08-04 16:11:18 -04002127 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002128 pde6->word0 = cpu_to_le32(pde6->word0);
2129 pde6->word1 = cpu_to_le32(pde6->word1);
2130 pde6->word2 = cpu_to_le32(pde6->word2);
2131
James Smart6c8eea52010-04-06 14:49:53 -04002132 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002133 num_bde++;
2134 bpl++;
2135
2136 /* assumption: caller has already run dma_map_sg on command data */
2137 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2138 physaddr = sg_dma_address(sgde);
2139 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
2140 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2141 bpl->tus.f.bdeSize = sg_dma_len(sgde);
2142 if (datadir == DMA_TO_DEVICE)
2143 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2144 else
2145 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2146 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2147 bpl++;
2148 num_bde++;
2149 }
2150
2151out:
2152 return num_bde;
2153}
2154
James Smartacd68592012-01-18 16:25:09 -05002155/**
2156 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
2157 * @phba: The Hba for which this call is being executed.
2158 * @sc: pointer to scsi command we're working on
2159 * @bpl: pointer to buffer list for protection groups
2160 * @datacnt: number of segments of data that have been dma mapped
2161 * @protcnt: number of segment of protection data that have been dma mapped
2162 *
2163 * This function sets up BPL buffer list for protection groups of
2164 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05002165 *
2166 * This is usually used when DIFs are in their own buffers,
2167 * separate from the data. The HBA can then by instructed
2168 * to place the DIFs in the outgoing stream. For read operations,
2169 * The HBA could extract the DIFs and place it in DIF buffers.
2170 *
2171 * The buffer list for this type consists of one or more of the
2172 * protection groups described below:
2173 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002174 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002175 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002176 * | PDE_6 |
2177 * +-------------------------+
2178 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05002179 * +-------------------------+
2180 * | Data BDE |
2181 * +-------------------------+
2182 * |more Data BDE's ... (opt)|
2183 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002184 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002185 * +-------------------------+
2186 * | ... |
2187 * +-------------------------+
2188 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002189 * Note: It is assumed that both data and protection s/g buffers have been
2190 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05002191 *
2192 * Returns the number of BDEs added to the BPL.
2193 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002194static int
2195lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2196 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2197{
2198 struct scatterlist *sgde = NULL; /* s/g data entry */
2199 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04002200 struct lpfc_pde5 *pde5 = NULL;
2201 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05002202 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002203 dma_addr_t dataphysaddr, protphysaddr;
2204 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05002205 unsigned int split_offset;
2206 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05002207 unsigned int protgrp_blks, protgrp_bytes;
2208 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04002209 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002210 int datadir = sc->sc_data_direction;
2211 unsigned char pgdone = 0, alldone = 0;
2212 unsigned blksize;
James Smart0829a192012-05-09 21:18:12 -04002213#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002214 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002215#endif
James Smartacd68592012-01-18 16:25:09 -05002216 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002217 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04002218 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002219 int num_bde = 0;
2220
2221 sgpe = scsi_prot_sglist(sc);
2222 sgde = scsi_sglist(sc);
2223
2224 if (!sgpe || !sgde) {
2225 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2226 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2227 sgpe, sgde);
2228 return 0;
2229 }
2230
James Smart6c8eea52010-04-06 14:49:53 -04002231 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2232 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002233 goto out;
2234
James Smart6c8eea52010-04-06 14:49:53 -04002235 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05002236 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002237 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002238
James Smartf9bb2da2011-10-10 21:34:11 -04002239#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002240 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002241 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002242 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002243 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002244 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002245 checking = 0;
2246 }
James Smartf9bb2da2011-10-10 21:34:11 -04002247#endif
2248
James Smarte2a0a9d2008-12-04 22:40:02 -05002249 split_offset = 0;
2250 do {
James Smart96f70772013-04-17 20:16:15 -04002251 /* Check to see if we ran out of space */
2252 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
2253 return num_bde + 3;
2254
James Smart6c8eea52010-04-06 14:49:53 -04002255 /* setup PDE5 with what we have */
2256 pde5 = (struct lpfc_pde5 *) bpl;
2257 memset(pde5, 0, sizeof(struct lpfc_pde5));
2258 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002259
James Smartbc739052010-08-04 16:11:18 -04002260 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002261 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002262 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002263
James Smart6c8eea52010-04-06 14:49:53 -04002264 /* advance bpl and increment bde count */
2265 num_bde++;
2266 bpl++;
2267 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002268
James Smart6c8eea52010-04-06 14:49:53 -04002269 /* setup PDE6 with the rest of the info */
2270 memset(pde6, 0, sizeof(struct lpfc_pde6));
2271 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2272 bf_set(pde6_optx, pde6, txop);
2273 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002274
James Smart9c6aa9d2013-05-31 17:03:39 -04002275 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002276 bf_set(pde6_ce, pde6, checking);
2277 else
2278 bf_set(pde6_ce, pde6, 0);
2279
James Smart9c6aa9d2013-05-31 17:03:39 -04002280 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002281 bf_set(pde6_re, pde6, checking);
2282 else
2283 bf_set(pde6_re, pde6, 0);
2284
James Smart6c8eea52010-04-06 14:49:53 -04002285 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002286 bf_set(pde6_ae, pde6, 0);
2287 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002288
James Smartbc739052010-08-04 16:11:18 -04002289 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002290 pde6->word0 = cpu_to_le32(pde6->word0);
2291 pde6->word1 = cpu_to_le32(pde6->word1);
2292 pde6->word2 = cpu_to_le32(pde6->word2);
2293
James Smart6c8eea52010-04-06 14:49:53 -04002294 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002295 num_bde++;
2296 bpl++;
2297
2298 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05002299 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2300 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05002301
James Smarte2a0a9d2008-12-04 22:40:02 -05002302 /* must be integer multiple of the DIF block length */
2303 BUG_ON(protgroup_len % 8);
2304
James Smart7f860592011-03-11 16:05:52 -05002305 pde7 = (struct lpfc_pde7 *) bpl;
2306 memset(pde7, 0, sizeof(struct lpfc_pde7));
2307 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2308
James Smart7c56b9f2011-07-22 18:36:25 -04002309 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2310 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05002311
James Smarte2a0a9d2008-12-04 22:40:02 -05002312 protgrp_blks = protgroup_len / 8;
2313 protgrp_bytes = protgrp_blks * blksize;
2314
James Smart7f860592011-03-11 16:05:52 -05002315 /* check if this pde is crossing the 4K boundary; if so split */
2316 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2317 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2318 protgroup_offset += protgroup_remainder;
2319 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04002320 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05002321 } else {
2322 protgroup_offset = 0;
2323 curr_prot++;
2324 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002325
James Smarte2a0a9d2008-12-04 22:40:02 -05002326 num_bde++;
2327
2328 /* setup BDE's for data blocks associated with DIF data */
2329 pgdone = 0;
2330 subtotal = 0; /* total bytes processed for current prot grp */
2331 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002332 /* Check to see if we ran out of space */
2333 if (num_bde >= phba->cfg_total_seg_cnt)
2334 return num_bde + 1;
2335
James Smarte2a0a9d2008-12-04 22:40:02 -05002336 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04002337 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2338 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002339 __func__);
2340 return 0;
2341 }
2342 bpl++;
2343 dataphysaddr = sg_dma_address(sgde) + split_offset;
2344 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2345 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2346
2347 remainder = sg_dma_len(sgde) - split_offset;
2348
2349 if ((subtotal + remainder) <= protgrp_bytes) {
2350 /* we can use this whole buffer */
2351 bpl->tus.f.bdeSize = remainder;
2352 split_offset = 0;
2353
2354 if ((subtotal + remainder) == protgrp_bytes)
2355 pgdone = 1;
2356 } else {
2357 /* must split this buffer with next prot grp */
2358 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2359 split_offset += bpl->tus.f.bdeSize;
2360 }
2361
2362 subtotal += bpl->tus.f.bdeSize;
2363
2364 if (datadir == DMA_TO_DEVICE)
2365 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2366 else
2367 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2368 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2369
2370 num_bde++;
2371 curr_data++;
2372
2373 if (split_offset)
2374 break;
2375
2376 /* Move to the next s/g segment if possible */
2377 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04002378
James Smarte2a0a9d2008-12-04 22:40:02 -05002379 }
2380
James Smart7f860592011-03-11 16:05:52 -05002381 if (protgroup_offset) {
2382 /* update the reference tag */
2383 reftag += protgrp_blks;
2384 bpl++;
2385 continue;
2386 }
2387
James Smarte2a0a9d2008-12-04 22:40:02 -05002388 /* are we done ? */
2389 if (curr_prot == protcnt) {
2390 alldone = 1;
2391 } else if (curr_prot < protcnt) {
2392 /* advance to next prot buffer */
2393 sgpe = sg_next(sgpe);
2394 bpl++;
2395
2396 /* update the reference tag */
2397 reftag += protgrp_blks;
2398 } else {
2399 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04002400 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2401 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05002402 }
2403
2404 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05002405out:
2406
James Smarte2a0a9d2008-12-04 22:40:02 -05002407 return num_bde;
2408}
James Smart7f860592011-03-11 16:05:52 -05002409
James Smartacd68592012-01-18 16:25:09 -05002410/**
2411 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2412 * @phba: The Hba for which this call is being executed.
2413 * @sc: pointer to scsi command we're working on
2414 * @sgl: pointer to buffer list for protection groups
2415 * @datacnt: number of segments of data that have been dma mapped
2416 *
2417 * This function sets up SGL buffer list for protection groups of
2418 * type LPFC_PG_TYPE_NO_DIF
2419 *
2420 * This is usually used when the HBA is instructed to generate
2421 * DIFs and insert them into data stream (or strip DIF from
2422 * incoming data stream)
2423 *
2424 * The buffer list consists of just one protection group described
2425 * below:
2426 * +-------------------------+
2427 * start of prot group --> | DI_SEED |
2428 * +-------------------------+
2429 * | Data SGE |
2430 * +-------------------------+
2431 * |more Data SGE's ... (opt)|
2432 * +-------------------------+
2433 *
2434 *
2435 * Note: Data s/g buffers have been dma mapped
2436 *
2437 * Returns the number of SGEs added to the SGL.
2438 **/
2439static int
2440lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2441 struct sli4_sge *sgl, int datasegcnt)
2442{
2443 struct scatterlist *sgde = NULL; /* s/g data entry */
2444 struct sli4_sge_diseed *diseed = NULL;
2445 dma_addr_t physaddr;
2446 int i = 0, num_sge = 0, status;
James Smartacd68592012-01-18 16:25:09 -05002447 uint32_t reftag;
2448 unsigned blksize;
2449 uint8_t txop, rxop;
James Smart0829a192012-05-09 21:18:12 -04002450#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002451 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002452#endif
James Smartacd68592012-01-18 16:25:09 -05002453 uint32_t checking = 1;
2454 uint32_t dma_len;
2455 uint32_t dma_offset = 0;
2456
2457 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2458 if (status)
2459 goto out;
2460
2461 /* extract some info from the scsi command for pde*/
2462 blksize = lpfc_cmd_blksize(sc);
2463 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2464
2465#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002466 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002467 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002468 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002469 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002470 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002471 checking = 0;
2472 }
2473#endif
2474
2475 /* setup DISEED with what we have */
2476 diseed = (struct sli4_sge_diseed *) sgl;
2477 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2478 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2479
2480 /* Endianness conversion if necessary */
2481 diseed->ref_tag = cpu_to_le32(reftag);
2482 diseed->ref_tag_tran = diseed->ref_tag;
2483
James Smarta6887e22013-04-17 20:18:07 -04002484 /*
2485 * We only need to check the data on READs, for WRITEs
2486 * protection data is automatically generated, not checked.
2487 */
2488 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002489 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002490 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2491 else
2492 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2493
James Smart9c6aa9d2013-05-31 17:03:39 -04002494 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002495 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2496 else
2497 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2498 }
2499
James Smartacd68592012-01-18 16:25:09 -05002500 /* setup DISEED with the rest of the info */
2501 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2502 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002503
James Smartacd68592012-01-18 16:25:09 -05002504 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2505 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2506
2507 /* Endianness conversion if necessary for DISEED */
2508 diseed->word2 = cpu_to_le32(diseed->word2);
2509 diseed->word3 = cpu_to_le32(diseed->word3);
2510
2511 /* advance bpl and increment sge count */
2512 num_sge++;
2513 sgl++;
2514
2515 /* assumption: caller has already run dma_map_sg on command data */
2516 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2517 physaddr = sg_dma_address(sgde);
2518 dma_len = sg_dma_len(sgde);
2519 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2520 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2521 if ((i + 1) == datasegcnt)
2522 bf_set(lpfc_sli4_sge_last, sgl, 1);
2523 else
2524 bf_set(lpfc_sli4_sge_last, sgl, 0);
2525 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2526 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2527
2528 sgl->sge_len = cpu_to_le32(dma_len);
2529 dma_offset += dma_len;
2530
2531 sgl++;
2532 num_sge++;
2533 }
2534
2535out:
2536 return num_sge;
2537}
2538
2539/**
2540 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2541 * @phba: The Hba for which this call is being executed.
2542 * @sc: pointer to scsi command we're working on
2543 * @sgl: pointer to buffer list for protection groups
2544 * @datacnt: number of segments of data that have been dma mapped
2545 * @protcnt: number of segment of protection data that have been dma mapped
2546 *
2547 * This function sets up SGL buffer list for protection groups of
2548 * type LPFC_PG_TYPE_DIF
2549 *
2550 * This is usually used when DIFs are in their own buffers,
2551 * separate from the data. The HBA can then by instructed
2552 * to place the DIFs in the outgoing stream. For read operations,
2553 * The HBA could extract the DIFs and place it in DIF buffers.
2554 *
2555 * The buffer list for this type consists of one or more of the
2556 * protection groups described below:
2557 * +-------------------------+
2558 * start of first prot group --> | DISEED |
2559 * +-------------------------+
2560 * | DIF (Prot SGE) |
2561 * +-------------------------+
2562 * | Data SGE |
2563 * +-------------------------+
2564 * |more Data SGE's ... (opt)|
2565 * +-------------------------+
2566 * start of new prot group --> | DISEED |
2567 * +-------------------------+
2568 * | ... |
2569 * +-------------------------+
2570 *
2571 * Note: It is assumed that both data and protection s/g buffers have been
2572 * mapped for DMA
2573 *
2574 * Returns the number of SGEs added to the SGL.
2575 **/
2576static int
2577lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2578 struct sli4_sge *sgl, int datacnt, int protcnt)
2579{
2580 struct scatterlist *sgde = NULL; /* s/g data entry */
2581 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2582 struct sli4_sge_diseed *diseed = NULL;
2583 dma_addr_t dataphysaddr, protphysaddr;
2584 unsigned short curr_data = 0, curr_prot = 0;
2585 unsigned int split_offset;
2586 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2587 unsigned int protgrp_blks, protgrp_bytes;
2588 unsigned int remainder, subtotal;
2589 int status;
2590 unsigned char pgdone = 0, alldone = 0;
2591 unsigned blksize;
2592 uint32_t reftag;
2593 uint8_t txop, rxop;
2594 uint32_t dma_len;
James Smart0829a192012-05-09 21:18:12 -04002595#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002596 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002597#endif
James Smartacd68592012-01-18 16:25:09 -05002598 uint32_t checking = 1;
2599 uint32_t dma_offset = 0;
2600 int num_sge = 0;
2601
2602 sgpe = scsi_prot_sglist(sc);
2603 sgde = scsi_sglist(sc);
2604
2605 if (!sgpe || !sgde) {
2606 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2607 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2608 sgpe, sgde);
2609 return 0;
2610 }
2611
2612 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2613 if (status)
2614 goto out;
2615
2616 /* extract some info from the scsi command */
2617 blksize = lpfc_cmd_blksize(sc);
2618 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2619
2620#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002621 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002622 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002623 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002624 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002625 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002626 checking = 0;
2627 }
2628#endif
2629
2630 split_offset = 0;
2631 do {
James Smart96f70772013-04-17 20:16:15 -04002632 /* Check to see if we ran out of space */
2633 if (num_sge >= (phba->cfg_total_seg_cnt - 2))
2634 return num_sge + 3;
2635
James Smartacd68592012-01-18 16:25:09 -05002636 /* setup DISEED with what we have */
2637 diseed = (struct sli4_sge_diseed *) sgl;
2638 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2639 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2640
2641 /* Endianness conversion if necessary */
2642 diseed->ref_tag = cpu_to_le32(reftag);
2643 diseed->ref_tag_tran = diseed->ref_tag;
2644
James Smart9c6aa9d2013-05-31 17:03:39 -04002645 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD)) {
James Smarta6887e22013-04-17 20:18:07 -04002646 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2647
2648 } else {
2649 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2650 /*
2651 * When in this mode, the hardware will replace
2652 * the guard tag from the host with a
2653 * newly generated good CRC for the wire.
2654 * Switch to raw mode here to avoid this
2655 * behavior. What the host sends gets put on the wire.
2656 */
2657 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2658 txop = BG_OP_RAW_MODE;
2659 rxop = BG_OP_RAW_MODE;
2660 }
2661 }
2662
2663
James Smart9c6aa9d2013-05-31 17:03:39 -04002664 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002665 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2666 else
2667 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2668
James Smartacd68592012-01-18 16:25:09 -05002669 /* setup DISEED with the rest of the info */
2670 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2671 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002672
James Smartacd68592012-01-18 16:25:09 -05002673 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2674 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2675
2676 /* Endianness conversion if necessary for DISEED */
2677 diseed->word2 = cpu_to_le32(diseed->word2);
2678 diseed->word3 = cpu_to_le32(diseed->word3);
2679
2680 /* advance sgl and increment bde count */
2681 num_sge++;
2682 sgl++;
2683
2684 /* setup the first BDE that points to protection buffer */
2685 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2686 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2687
2688 /* must be integer multiple of the DIF block length */
2689 BUG_ON(protgroup_len % 8);
2690
2691 /* Now setup DIF SGE */
2692 sgl->word2 = 0;
2693 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2694 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2695 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2696 sgl->word2 = cpu_to_le32(sgl->word2);
2697
2698 protgrp_blks = protgroup_len / 8;
2699 protgrp_bytes = protgrp_blks * blksize;
2700
2701 /* check if DIF SGE is crossing the 4K boundary; if so split */
2702 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2703 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2704 protgroup_offset += protgroup_remainder;
2705 protgrp_blks = protgroup_remainder / 8;
2706 protgrp_bytes = protgrp_blks * blksize;
2707 } else {
2708 protgroup_offset = 0;
2709 curr_prot++;
2710 }
2711
2712 num_sge++;
2713
2714 /* setup SGE's for data blocks associated with DIF data */
2715 pgdone = 0;
2716 subtotal = 0; /* total bytes processed for current prot grp */
2717 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002718 /* Check to see if we ran out of space */
2719 if (num_sge >= phba->cfg_total_seg_cnt)
2720 return num_sge + 1;
2721
James Smartacd68592012-01-18 16:25:09 -05002722 if (!sgde) {
2723 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2724 "9086 BLKGRD:%s Invalid data segment\n",
2725 __func__);
2726 return 0;
2727 }
2728 sgl++;
2729 dataphysaddr = sg_dma_address(sgde) + split_offset;
2730
2731 remainder = sg_dma_len(sgde) - split_offset;
2732
2733 if ((subtotal + remainder) <= protgrp_bytes) {
2734 /* we can use this whole buffer */
2735 dma_len = remainder;
2736 split_offset = 0;
2737
2738 if ((subtotal + remainder) == protgrp_bytes)
2739 pgdone = 1;
2740 } else {
2741 /* must split this buffer with next prot grp */
2742 dma_len = protgrp_bytes - subtotal;
2743 split_offset += dma_len;
2744 }
2745
2746 subtotal += dma_len;
2747
2748 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2749 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2750 bf_set(lpfc_sli4_sge_last, sgl, 0);
2751 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2752 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2753
2754 sgl->sge_len = cpu_to_le32(dma_len);
2755 dma_offset += dma_len;
2756
2757 num_sge++;
2758 curr_data++;
2759
2760 if (split_offset)
2761 break;
2762
2763 /* Move to the next s/g segment if possible */
2764 sgde = sg_next(sgde);
2765 }
2766
2767 if (protgroup_offset) {
2768 /* update the reference tag */
2769 reftag += protgrp_blks;
2770 sgl++;
2771 continue;
2772 }
2773
2774 /* are we done ? */
2775 if (curr_prot == protcnt) {
2776 bf_set(lpfc_sli4_sge_last, sgl, 1);
2777 alldone = 1;
2778 } else if (curr_prot < protcnt) {
2779 /* advance to next prot buffer */
2780 sgpe = sg_next(sgpe);
2781 sgl++;
2782
2783 /* update the reference tag */
2784 reftag += protgrp_blks;
2785 } else {
2786 /* if we're here, we have a bug */
2787 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2788 "9085 BLKGRD: bug in %s\n", __func__);
2789 }
2790
2791 } while (!alldone);
2792
2793out:
2794
2795 return num_sge;
2796}
2797
2798/**
2799 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2800 * @phba: The Hba for which this call is being executed.
2801 * @sc: pointer to scsi command we're working on
2802 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002803 * Given a SCSI command that supports DIF, determine composition of protection
2804 * groups involved in setting up buffer lists
2805 *
James Smartacd68592012-01-18 16:25:09 -05002806 * Returns: Protection group type (with or without DIF)
2807 *
2808 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002809static int
2810lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2811{
2812 int ret = LPFC_PG_TYPE_INVALID;
2813 unsigned char op = scsi_get_prot_op(sc);
2814
2815 switch (op) {
2816 case SCSI_PROT_READ_STRIP:
2817 case SCSI_PROT_WRITE_INSERT:
2818 ret = LPFC_PG_TYPE_NO_DIF;
2819 break;
2820 case SCSI_PROT_READ_INSERT:
2821 case SCSI_PROT_WRITE_STRIP:
2822 case SCSI_PROT_READ_PASS:
2823 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002824 ret = LPFC_PG_TYPE_DIF_BUF;
2825 break;
2826 default:
James Smart9c6aa9d2013-05-31 17:03:39 -04002827 if (phba)
2828 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2829 "9021 Unsupported protection op:%d\n",
2830 op);
James Smarte2a0a9d2008-12-04 22:40:02 -05002831 break;
2832 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002833 return ret;
2834}
2835
James Smartacd68592012-01-18 16:25:09 -05002836/**
James Smarta6887e22013-04-17 20:18:07 -04002837 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2838 * @phba: The Hba for which this call is being executed.
2839 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2840 *
2841 * Adjust the data length to account for how much data
2842 * is actually on the wire.
2843 *
2844 * returns the adjusted data length
2845 **/
2846static int
2847lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
2848 struct lpfc_scsi_buf *lpfc_cmd)
2849{
2850 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2851 int fcpdl;
2852
2853 fcpdl = scsi_bufflen(sc);
2854
2855 /* Check if there is protection data on the wire */
2856 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002857 /* Read check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002858 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2859 return fcpdl;
2860
2861 } else {
James Smart9c6aa9d2013-05-31 17:03:39 -04002862 /* Write check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002863 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2864 return fcpdl;
2865 }
2866
2867 /*
2868 * If we are in DIF Type 1 mode every data block has a 8 byte
James Smart9c6aa9d2013-05-31 17:03:39 -04002869 * DIF (trailer) attached to it. Must ajust FCP data length
2870 * to account for the protection data.
James Smarta6887e22013-04-17 20:18:07 -04002871 */
James Smart9c6aa9d2013-05-31 17:03:39 -04002872 fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
James Smarta6887e22013-04-17 20:18:07 -04002873
2874 return fcpdl;
2875}
2876
2877/**
James Smartacd68592012-01-18 16:25:09 -05002878 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2879 * @phba: The Hba for which this call is being executed.
2880 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2881 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002882 * This is the protection/DIF aware version of
2883 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2884 * two functions eventually, but for now, it's here
James Smartacd68592012-01-18 16:25:09 -05002885 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002886static int
James Smartacd68592012-01-18 16:25:09 -05002887lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smarte2a0a9d2008-12-04 22:40:02 -05002888 struct lpfc_scsi_buf *lpfc_cmd)
2889{
2890 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2891 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2892 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2893 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2894 uint32_t num_bde = 0;
2895 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2896 int prot_group_type = 0;
James Smarta6887e22013-04-17 20:18:07 -04002897 int fcpdl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002898
2899 /*
2900 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2901 * fcp_rsp regions to the first data bde entry
2902 */
2903 bpl += 2;
2904 if (scsi_sg_count(scsi_cmnd)) {
2905 /*
2906 * The driver stores the segment count returned from pci_map_sg
2907 * because this a count of dma-mappings used to map the use_sg
2908 * pages. They are not guaranteed to be the same for those
2909 * architectures that implement an IOMMU.
2910 */
2911 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2912 scsi_sglist(scsi_cmnd),
2913 scsi_sg_count(scsi_cmnd), datadir);
2914 if (unlikely(!datasegcnt))
2915 return 1;
2916
2917 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04002918
2919 /* First check if data segment count from SCSI Layer is good */
2920 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
2921 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002922
2923 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2924
2925 switch (prot_group_type) {
2926 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04002927
2928 /* Here we need to add a PDE5 and PDE6 to the count */
2929 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt)
2930 goto err;
2931
James Smarte2a0a9d2008-12-04 22:40:02 -05002932 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2933 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002934 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002935 if (num_bde < 2)
2936 goto err;
2937 break;
James Smart96f70772013-04-17 20:16:15 -04002938
2939 case LPFC_PG_TYPE_DIF_BUF:
James Smarte2a0a9d2008-12-04 22:40:02 -05002940 /*
2941 * This type indicates that protection buffers are
2942 * passed to the driver, so that needs to be prepared
2943 * for DMA
2944 */
2945 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2946 scsi_prot_sglist(scsi_cmnd),
2947 scsi_prot_sg_count(scsi_cmnd), datadir);
2948 if (unlikely(!protsegcnt)) {
2949 scsi_dma_unmap(scsi_cmnd);
2950 return 1;
2951 }
2952
2953 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04002954
2955 /*
2956 * There is a minimun of 4 BPLs used for every
2957 * protection data segment.
2958 */
2959 if ((lpfc_cmd->prot_seg_cnt * 4) >
2960 (phba->cfg_total_seg_cnt - 2))
2961 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002962
2963 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2964 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002965 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04002966 if ((num_bde < 3) ||
2967 (num_bde > phba->cfg_total_seg_cnt))
James Smarte2a0a9d2008-12-04 22:40:02 -05002968 goto err;
2969 break;
James Smart96f70772013-04-17 20:16:15 -04002970
James Smarte2a0a9d2008-12-04 22:40:02 -05002971 case LPFC_PG_TYPE_INVALID:
2972 default:
James Smart96f70772013-04-17 20:16:15 -04002973 scsi_dma_unmap(scsi_cmnd);
2974 lpfc_cmd->seg_cnt = 0;
2975
James Smarte2a0a9d2008-12-04 22:40:02 -05002976 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2977 "9022 Unexpected protection group %i\n",
2978 prot_group_type);
2979 return 1;
2980 }
2981 }
2982
2983 /*
2984 * Finish initializing those IOCB fields that are dependent on the
2985 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2986 * reinitialized since all iocb memory resources are used many times
2987 * for transmit, receive, and continuation bpl's.
2988 */
2989 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2990 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2991 iocb_cmd->ulpBdeCount = 1;
2992 iocb_cmd->ulpLe = 1;
2993
James Smarta6887e22013-04-17 20:18:07 -04002994 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002995 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2996
2997 /*
2998 * Due to difference in data length between DIF/non-DIF paths,
2999 * we need to set word 4 of IOCB here
3000 */
3001 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
3002
3003 return 0;
3004err:
James Smart96f70772013-04-17 20:16:15 -04003005 if (lpfc_cmd->seg_cnt)
3006 scsi_dma_unmap(scsi_cmnd);
3007 if (lpfc_cmd->prot_seg_cnt)
3008 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3009 scsi_prot_sg_count(scsi_cmnd),
3010 scsi_cmnd->sc_data_direction);
3011
James Smarte2a0a9d2008-12-04 22:40:02 -05003012 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04003013 "9023 Cannot setup S/G List for HBA"
3014 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
3015 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3016 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
James Smarte2a0a9d2008-12-04 22:40:02 -05003017 prot_group_type, num_bde);
James Smart96f70772013-04-17 20:16:15 -04003018
3019 lpfc_cmd->seg_cnt = 0;
3020 lpfc_cmd->prot_seg_cnt = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05003021 return 1;
3022}
3023
3024/*
James Smart737d4242013-04-17 20:14:49 -04003025 * This function calcuates the T10 DIF guard tag
3026 * on the specified data using a CRC algorithmn
3027 * using crc_t10dif.
3028 */
3029uint16_t
3030lpfc_bg_crc(uint8_t *data, int count)
3031{
3032 uint16_t crc = 0;
3033 uint16_t x;
3034
3035 crc = crc_t10dif(data, count);
3036 x = cpu_to_be16(crc);
3037 return x;
3038}
3039
3040/*
3041 * This function calcuates the T10 DIF guard tag
3042 * on the specified data using a CSUM algorithmn
3043 * using ip_compute_csum.
3044 */
3045uint16_t
3046lpfc_bg_csum(uint8_t *data, int count)
3047{
3048 uint16_t ret;
3049
3050 ret = ip_compute_csum(data, count);
3051 return ret;
3052}
3053
3054/*
3055 * This function examines the protection data to try to determine
3056 * what type of T10-DIF error occurred.
3057 */
3058void
3059lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3060{
3061 struct scatterlist *sgpe; /* s/g prot entry */
3062 struct scatterlist *sgde; /* s/g data entry */
3063 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3064 struct scsi_dif_tuple *src = NULL;
3065 uint8_t *data_src = NULL;
3066 uint16_t guard_tag, guard_type;
3067 uint16_t start_app_tag, app_tag;
3068 uint32_t start_ref_tag, ref_tag;
3069 int prot, protsegcnt;
3070 int err_type, len, data_len;
3071 int chk_ref, chk_app, chk_guard;
3072 uint16_t sum;
3073 unsigned blksize;
3074
3075 err_type = BGS_GUARD_ERR_MASK;
3076 sum = 0;
3077 guard_tag = 0;
3078
3079 /* First check to see if there is protection data to examine */
3080 prot = scsi_get_prot_op(cmd);
3081 if ((prot == SCSI_PROT_READ_STRIP) ||
3082 (prot == SCSI_PROT_WRITE_INSERT) ||
3083 (prot == SCSI_PROT_NORMAL))
3084 goto out;
3085
3086 /* Currently the driver just supports ref_tag and guard_tag checking */
3087 chk_ref = 1;
3088 chk_app = 0;
3089 chk_guard = 0;
3090
3091 /* Setup a ptr to the protection data provided by the SCSI host */
3092 sgpe = scsi_prot_sglist(cmd);
3093 protsegcnt = lpfc_cmd->prot_seg_cnt;
3094
3095 if (sgpe && protsegcnt) {
3096
3097 /*
3098 * We will only try to verify guard tag if the segment
3099 * data length is a multiple of the blksize.
3100 */
3101 sgde = scsi_sglist(cmd);
3102 blksize = lpfc_cmd_blksize(cmd);
3103 data_src = (uint8_t *)sg_virt(sgde);
3104 data_len = sgde->length;
3105 if ((data_len & (blksize - 1)) == 0)
3106 chk_guard = 1;
3107 guard_type = scsi_host_get_guard(cmd->device->host);
3108
James Smarte85d8f92013-05-31 17:04:10 -04003109 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
James Smarta6887e22013-04-17 20:18:07 -04003110 start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
James Smart737d4242013-04-17 20:14:49 -04003111 start_app_tag = src->app_tag;
James Smart737d4242013-04-17 20:14:49 -04003112 len = sgpe->length;
3113 while (src && protsegcnt) {
3114 while (len) {
3115
3116 /*
3117 * First check to see if a protection data
3118 * check is valid
3119 */
3120 if ((src->ref_tag == 0xffffffff) ||
3121 (src->app_tag == 0xffff)) {
3122 start_ref_tag++;
3123 goto skipit;
3124 }
3125
James Smart9c6aa9d2013-05-31 17:03:39 -04003126 /* First Guard Tag checking */
3127 if (chk_guard) {
3128 guard_tag = src->guard_tag;
3129 if (lpfc_cmd_guard_csum(cmd))
3130 sum = lpfc_bg_csum(data_src,
3131 blksize);
3132 else
3133 sum = lpfc_bg_crc(data_src,
3134 blksize);
3135 if ((guard_tag != sum)) {
3136 err_type = BGS_GUARD_ERR_MASK;
3137 goto out;
3138 }
James Smart737d4242013-04-17 20:14:49 -04003139 }
3140
3141 /* Reference Tag checking */
3142 ref_tag = be32_to_cpu(src->ref_tag);
3143 if (chk_ref && (ref_tag != start_ref_tag)) {
3144 err_type = BGS_REFTAG_ERR_MASK;
3145 goto out;
3146 }
3147 start_ref_tag++;
3148
James Smart9c6aa9d2013-05-31 17:03:39 -04003149 /* App Tag checking */
3150 app_tag = src->app_tag;
3151 if (chk_app && (app_tag != start_app_tag)) {
3152 err_type = BGS_APPTAG_ERR_MASK;
3153 goto out;
James Smart737d4242013-04-17 20:14:49 -04003154 }
3155skipit:
3156 len -= sizeof(struct scsi_dif_tuple);
3157 if (len < 0)
3158 len = 0;
3159 src++;
3160
3161 data_src += blksize;
3162 data_len -= blksize;
3163
3164 /*
3165 * Are we at the end of the Data segment?
3166 * The data segment is only used for Guard
3167 * tag checking.
3168 */
3169 if (chk_guard && (data_len == 0)) {
3170 chk_guard = 0;
3171 sgde = sg_next(sgde);
3172 if (!sgde)
3173 goto out;
3174
3175 data_src = (uint8_t *)sg_virt(sgde);
3176 data_len = sgde->length;
3177 if ((data_len & (blksize - 1)) == 0)
3178 chk_guard = 1;
3179 }
3180 }
3181
3182 /* Goto the next Protection data segment */
3183 sgpe = sg_next(sgpe);
3184 if (sgpe) {
3185 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
3186 len = sgpe->length;
3187 } else {
3188 src = NULL;
3189 }
3190 protsegcnt--;
3191 }
3192 }
3193out:
3194 if (err_type == BGS_GUARD_ERR_MASK) {
3195 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3196 0x10, 0x1);
3197 cmd->result = DRIVER_SENSE << 24
3198 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3199 phba->bg_guard_err_cnt++;
3200 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3201 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
3202 (unsigned long)scsi_get_lba(cmd),
3203 sum, guard_tag);
3204
3205 } else if (err_type == BGS_REFTAG_ERR_MASK) {
3206 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3207 0x10, 0x3);
3208 cmd->result = DRIVER_SENSE << 24
3209 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3210
3211 phba->bg_reftag_err_cnt++;
3212 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3213 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
3214 (unsigned long)scsi_get_lba(cmd),
3215 ref_tag, start_ref_tag);
3216
3217 } else if (err_type == BGS_APPTAG_ERR_MASK) {
3218 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3219 0x10, 0x2);
3220 cmd->result = DRIVER_SENSE << 24
3221 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3222
3223 phba->bg_apptag_err_cnt++;
3224 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3225 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
3226 (unsigned long)scsi_get_lba(cmd),
3227 app_tag, start_app_tag);
3228 }
3229}
3230
3231
3232/*
James Smarte2a0a9d2008-12-04 22:40:02 -05003233 * This function checks for BlockGuard errors detected by
3234 * the HBA. In case of errors, the ASC/ASCQ fields in the
3235 * sense buffer will be set accordingly, paired with
3236 * ILLEGAL_REQUEST to signal to the kernel that the HBA
3237 * detected corruption.
3238 *
3239 * Returns:
3240 * 0 - No error found
3241 * 1 - BlockGuard error found
3242 * -1 - Internal error (bad profile, ...etc)
3243 */
3244static int
3245lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
3246 struct lpfc_iocbq *pIocbOut)
3247{
3248 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3249 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
3250 int ret = 0;
3251 uint32_t bghm = bgf->bghm;
3252 uint32_t bgstat = bgf->bgstat;
3253 uint64_t failing_sector = 0;
3254
James Smarte2a0a9d2008-12-04 22:40:02 -05003255 spin_lock(&_dump_buf_lock);
3256 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04003257 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
3258 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05003259 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04003260 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003261
3262 /* If we have a prot sgl, save the DIF buffer */
3263 if (lpfc_prot_group_type(phba, cmd) ==
3264 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04003265 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
3266 "Saving DIF for %u blocks to debugfs\n",
3267 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
3268 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003269 }
3270
3271 _dump_buf_done = 1;
3272 }
3273 spin_unlock(&_dump_buf_lock);
3274
3275 if (lpfc_bgs_get_invalid_prof(bgstat)) {
3276 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart737d4242013-04-17 20:14:49 -04003277 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3278 "9072 BLKGRD: Invalid BG Profile in cmd"
3279 " 0x%x lba 0x%llx blk cnt 0x%x "
3280 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3281 (unsigned long long)scsi_get_lba(cmd),
3282 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003283 ret = (-1);
3284 goto out;
3285 }
3286
3287 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
3288 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart737d4242013-04-17 20:14:49 -04003289 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3290 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
3291 " 0x%x lba 0x%llx blk cnt 0x%x "
3292 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3293 (unsigned long long)scsi_get_lba(cmd),
3294 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003295 ret = (-1);
3296 goto out;
3297 }
3298
3299 if (lpfc_bgs_get_guard_err(bgstat)) {
3300 ret = 1;
3301
3302 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3303 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003304 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003305 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3306 phba->bg_guard_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003307 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3308 "9055 BLKGRD: Guard Tag error in cmd"
3309 " 0x%x lba 0x%llx blk cnt 0x%x "
3310 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3311 (unsigned long long)scsi_get_lba(cmd),
3312 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003313 }
3314
3315 if (lpfc_bgs_get_reftag_err(bgstat)) {
3316 ret = 1;
3317
3318 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3319 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003320 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003321 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3322
3323 phba->bg_reftag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003324 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3325 "9056 BLKGRD: Ref Tag error in cmd"
3326 " 0x%x lba 0x%llx blk cnt 0x%x "
3327 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3328 (unsigned long long)scsi_get_lba(cmd),
3329 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003330 }
3331
3332 if (lpfc_bgs_get_apptag_err(bgstat)) {
3333 ret = 1;
3334
3335 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3336 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003337 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003338 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3339
3340 phba->bg_apptag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003341 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3342 "9061 BLKGRD: App Tag error in cmd"
3343 " 0x%x lba 0x%llx blk cnt 0x%x "
3344 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3345 (unsigned long long)scsi_get_lba(cmd),
3346 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003347 }
3348
3349 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
3350 /*
3351 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04003352 * field, and put the failing LBA in it.
3353 * This code assumes there was also a guard/app/ref tag error
3354 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05003355 */
James Smart7c56b9f2011-07-22 18:36:25 -04003356 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
3357 cmd->sense_buffer[8] = 0; /* Information descriptor type */
3358 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
3359 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05003360
3361 /* bghm is a "on the wire" FC frame based count */
3362 switch (scsi_get_prot_op(cmd)) {
3363 case SCSI_PROT_READ_INSERT:
3364 case SCSI_PROT_WRITE_STRIP:
3365 bghm /= cmd->device->sector_size;
3366 break;
3367 case SCSI_PROT_READ_STRIP:
3368 case SCSI_PROT_WRITE_INSERT:
3369 case SCSI_PROT_READ_PASS:
3370 case SCSI_PROT_WRITE_PASS:
3371 bghm /= (cmd->device->sector_size +
3372 sizeof(struct scsi_dif_tuple));
3373 break;
3374 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003375
3376 failing_sector = scsi_get_lba(cmd);
3377 failing_sector += bghm;
3378
James Smart7c56b9f2011-07-22 18:36:25 -04003379 /* Descriptor Information */
3380 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05003381 }
3382
3383 if (!ret) {
3384 /* No error was reported - problem in FW? */
James Smart737d4242013-04-17 20:14:49 -04003385 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3386 "9057 BLKGRD: Unknown error in cmd"
3387 " 0x%x lba 0x%llx blk cnt 0x%x "
3388 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3389 (unsigned long long)scsi_get_lba(cmd),
3390 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003391
James Smart737d4242013-04-17 20:14:49 -04003392 /* Calcuate what type of error it was */
3393 lpfc_calc_bg_err(phba, lpfc_cmd);
3394 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003395out:
3396 return ret;
3397}
3398
James Smartea2151b2008-09-07 11:52:10 -04003399/**
James Smartda0436e2009-05-22 14:51:39 -04003400 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3401 * @phba: The Hba for which this call is being executed.
3402 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3403 *
3404 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3405 * field of @lpfc_cmd for device with SLI-4 interface spec.
3406 *
3407 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003408 * 1 - Error
3409 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04003410 **/
3411static int
3412lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3413{
3414 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3415 struct scatterlist *sgel = NULL;
3416 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3417 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
James Smartfedd3b72011-02-16 12:39:24 -05003418 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04003419 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3420 dma_addr_t physaddr;
3421 uint32_t num_bde = 0;
3422 uint32_t dma_len;
3423 uint32_t dma_offset = 0;
3424 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05003425 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04003426
3427 /*
3428 * There are three possibilities here - use scatter-gather segment, use
3429 * the single mapping, or neither. Start the lpfc command prep by
3430 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3431 * data bde entry.
3432 */
3433 if (scsi_sg_count(scsi_cmnd)) {
3434 /*
3435 * The driver stores the segment count returned from pci_map_sg
3436 * because this a count of dma-mappings used to map the use_sg
3437 * pages. They are not guaranteed to be the same for those
3438 * architectures that implement an IOMMU.
3439 */
3440
3441 nseg = scsi_dma_map(scsi_cmnd);
3442 if (unlikely(!nseg))
3443 return 1;
3444 sgl += 1;
3445 /* clear the last flag in the fcp_rsp map entry */
3446 sgl->word2 = le32_to_cpu(sgl->word2);
3447 bf_set(lpfc_sli4_sge_last, sgl, 0);
3448 sgl->word2 = cpu_to_le32(sgl->word2);
3449 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05003450 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04003451 lpfc_cmd->seg_cnt = nseg;
3452 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04003453 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3454 " %s: Too many sg segments from "
3455 "dma_map_sg. Config %d, seg_cnt %d\n",
3456 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04003457 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04003458 lpfc_cmd->seg_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04003459 scsi_dma_unmap(scsi_cmnd);
3460 return 1;
3461 }
3462
3463 /*
3464 * The driver established a maximum scatter-gather segment count
3465 * during probe that limits the number of sg elements in any
3466 * single scsi command. Just run through the seg_cnt and format
3467 * the sge's.
3468 * When using SLI-3 the driver will try to fit all the BDEs into
3469 * the IOCB. If it can't then the BDEs get added to a BPL as it
3470 * does for SLI-2 mode.
3471 */
3472 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3473 physaddr = sg_dma_address(sgel);
3474 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04003475 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3476 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04003477 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04003478 if ((num_bde + 1) == nseg)
3479 bf_set(lpfc_sli4_sge_last, sgl, 1);
3480 else
3481 bf_set(lpfc_sli4_sge_last, sgl, 0);
3482 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04003483 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04003484 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05003485 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04003486 dma_offset += dma_len;
3487 sgl++;
3488 }
James Smartfedd3b72011-02-16 12:39:24 -05003489 /* setup the performance hint (first data BDE) if enabled */
3490 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
3491 bde = (struct ulp_bde64 *)
3492 &(iocb_cmd->unsli3.sli3Words[5]);
3493 bde->addrLow = first_data_sgl->addr_lo;
3494 bde->addrHigh = first_data_sgl->addr_hi;
3495 bde->tus.f.bdeSize =
3496 le32_to_cpu(first_data_sgl->sge_len);
3497 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3498 bde->tus.w = cpu_to_le32(bde->tus.w);
3499 }
James Smartda0436e2009-05-22 14:51:39 -04003500 } else {
3501 sgl += 1;
3502 /* clear the last flag in the fcp_rsp map entry */
3503 sgl->word2 = le32_to_cpu(sgl->word2);
3504 bf_set(lpfc_sli4_sge_last, sgl, 1);
3505 sgl->word2 = cpu_to_le32(sgl->word2);
3506 }
3507
3508 /*
3509 * Finish initializing those IOCB fields that are dependent on the
3510 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3511 * explicitly reinitialized.
3512 * all iocb memory resources are reused.
3513 */
3514 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3515
3516 /*
3517 * Due to difference in data length between DIF/non-DIF paths,
3518 * we need to set word 4 of IOCB here
3519 */
3520 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
James Smart1ba981f2014-02-20 09:56:45 -05003521
3522 /*
3523 * If the OAS driver feature is enabled and the lun is enabled for
3524 * OAS, set the oas iocb related flags.
3525 */
3526 if ((phba->cfg_EnableXLane) && ((struct lpfc_device_data *)
3527 scsi_cmnd->device->hostdata)->oas_enabled)
3528 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_OAS;
James Smartda0436e2009-05-22 14:51:39 -04003529 return 0;
3530}
3531
3532/**
James Smartacd68592012-01-18 16:25:09 -05003533 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3534 * @phba: The Hba for which this call is being executed.
3535 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3536 *
3537 * This is the protection/DIF aware version of
3538 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3539 * two functions eventually, but for now, it's here
3540 **/
3541static int
3542lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3543 struct lpfc_scsi_buf *lpfc_cmd)
3544{
3545 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3546 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3547 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3548 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart96f70772013-04-17 20:16:15 -04003549 uint32_t num_sge = 0;
James Smartacd68592012-01-18 16:25:09 -05003550 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3551 int prot_group_type = 0;
3552 int fcpdl;
3553
3554 /*
3555 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
James Smart96f70772013-04-17 20:16:15 -04003556 * fcp_rsp regions to the first data sge entry
James Smartacd68592012-01-18 16:25:09 -05003557 */
3558 if (scsi_sg_count(scsi_cmnd)) {
3559 /*
3560 * The driver stores the segment count returned from pci_map_sg
3561 * because this a count of dma-mappings used to map the use_sg
3562 * pages. They are not guaranteed to be the same for those
3563 * architectures that implement an IOMMU.
3564 */
3565 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3566 scsi_sglist(scsi_cmnd),
3567 scsi_sg_count(scsi_cmnd), datadir);
3568 if (unlikely(!datasegcnt))
3569 return 1;
3570
3571 sgl += 1;
3572 /* clear the last flag in the fcp_rsp map entry */
3573 sgl->word2 = le32_to_cpu(sgl->word2);
3574 bf_set(lpfc_sli4_sge_last, sgl, 0);
3575 sgl->word2 = cpu_to_le32(sgl->word2);
3576
3577 sgl += 1;
3578 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04003579
3580 /* First check if data segment count from SCSI Layer is good */
3581 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
3582 goto err;
James Smartacd68592012-01-18 16:25:09 -05003583
3584 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3585
3586 switch (prot_group_type) {
3587 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04003588 /* Here we need to add a DISEED to the count */
3589 if ((lpfc_cmd->seg_cnt + 1) > phba->cfg_total_seg_cnt)
3590 goto err;
3591
3592 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003593 datasegcnt);
James Smart96f70772013-04-17 20:16:15 -04003594
James Smartacd68592012-01-18 16:25:09 -05003595 /* we should have 2 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003596 if (num_sge < 2)
James Smartacd68592012-01-18 16:25:09 -05003597 goto err;
3598 break;
James Smart96f70772013-04-17 20:16:15 -04003599
3600 case LPFC_PG_TYPE_DIF_BUF:
James Smartacd68592012-01-18 16:25:09 -05003601 /*
3602 * This type indicates that protection buffers are
3603 * passed to the driver, so that needs to be prepared
3604 * for DMA
3605 */
3606 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3607 scsi_prot_sglist(scsi_cmnd),
3608 scsi_prot_sg_count(scsi_cmnd), datadir);
3609 if (unlikely(!protsegcnt)) {
3610 scsi_dma_unmap(scsi_cmnd);
3611 return 1;
3612 }
3613
3614 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04003615 /*
3616 * There is a minimun of 3 SGEs used for every
3617 * protection data segment.
3618 */
3619 if ((lpfc_cmd->prot_seg_cnt * 3) >
3620 (phba->cfg_total_seg_cnt - 2))
3621 goto err;
James Smartacd68592012-01-18 16:25:09 -05003622
James Smart96f70772013-04-17 20:16:15 -04003623 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003624 datasegcnt, protsegcnt);
James Smart96f70772013-04-17 20:16:15 -04003625
James Smartacd68592012-01-18 16:25:09 -05003626 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003627 if ((num_sge < 3) ||
3628 (num_sge > phba->cfg_total_seg_cnt))
James Smartacd68592012-01-18 16:25:09 -05003629 goto err;
3630 break;
James Smart96f70772013-04-17 20:16:15 -04003631
James Smartacd68592012-01-18 16:25:09 -05003632 case LPFC_PG_TYPE_INVALID:
3633 default:
James Smart96f70772013-04-17 20:16:15 -04003634 scsi_dma_unmap(scsi_cmnd);
3635 lpfc_cmd->seg_cnt = 0;
3636
James Smartacd68592012-01-18 16:25:09 -05003637 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3638 "9083 Unexpected protection group %i\n",
3639 prot_group_type);
3640 return 1;
3641 }
3642 }
3643
James Smart8012cc32012-10-31 14:44:49 -04003644 switch (scsi_get_prot_op(scsi_cmnd)) {
3645 case SCSI_PROT_WRITE_STRIP:
3646 case SCSI_PROT_READ_STRIP:
3647 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3648 break;
3649 case SCSI_PROT_WRITE_INSERT:
3650 case SCSI_PROT_READ_INSERT:
3651 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3652 break;
3653 case SCSI_PROT_WRITE_PASS:
3654 case SCSI_PROT_READ_PASS:
3655 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3656 break;
3657 }
3658
James Smartacd68592012-01-18 16:25:09 -05003659 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smartacd68592012-01-18 16:25:09 -05003660 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3661
3662 /*
3663 * Due to difference in data length between DIF/non-DIF paths,
3664 * we need to set word 4 of IOCB here
3665 */
3666 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
James Smartacd68592012-01-18 16:25:09 -05003667
3668 return 0;
3669err:
James Smart96f70772013-04-17 20:16:15 -04003670 if (lpfc_cmd->seg_cnt)
3671 scsi_dma_unmap(scsi_cmnd);
3672 if (lpfc_cmd->prot_seg_cnt)
3673 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3674 scsi_prot_sg_count(scsi_cmnd),
3675 scsi_cmnd->sc_data_direction);
3676
James Smartacd68592012-01-18 16:25:09 -05003677 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04003678 "9084 Cannot setup S/G List for HBA"
3679 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3680 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3681 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3682 prot_group_type, num_sge);
3683
3684 lpfc_cmd->seg_cnt = 0;
3685 lpfc_cmd->prot_seg_cnt = 0;
James Smartacd68592012-01-18 16:25:09 -05003686 return 1;
3687}
3688
3689/**
James Smart3772a992009-05-22 14:50:54 -04003690 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
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:
James Smart6c8eea52010-04-06 14:49:53 -04003698 * 1 - Error
3699 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003700 **/
3701static inline int
3702lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3703{
3704 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3705}
3706
3707/**
James Smartacd68592012-01-18 16:25:09 -05003708 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3709 * using BlockGuard.
3710 * @phba: The Hba for which this call is being executed.
3711 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3712 *
3713 * This routine wraps the actual DMA mapping function pointer from the
3714 * lpfc_hba struct.
3715 *
3716 * Return codes:
3717 * 1 - Error
3718 * 0 - Success
3719 **/
3720static inline int
3721lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3722{
3723 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3724}
3725
3726/**
James Smart3621a712009-04-06 18:47:14 -04003727 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003728 * @phba: Pointer to hba context object.
3729 * @vport: Pointer to vport object.
3730 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3731 * @rsp_iocb: Pointer to response iocb object which reported error.
3732 *
3733 * This function posts an event when there is a SCSI command reporting
3734 * error from the scsi device.
3735 **/
3736static void
3737lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3738 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3739 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3740 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3741 uint32_t resp_info = fcprsp->rspStatus2;
3742 uint32_t scsi_status = fcprsp->rspStatus3;
3743 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3744 struct lpfc_fast_path_event *fast_path_evt = NULL;
3745 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3746 unsigned long flags;
3747
James Smart5989b8d2010-10-22 11:06:56 -04003748 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3749 return;
3750
James Smartea2151b2008-09-07 11:52:10 -04003751 /* If there is queuefull or busy condition send a scsi event */
3752 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3753 (cmnd->result == SAM_STAT_BUSY)) {
3754 fast_path_evt = lpfc_alloc_fast_evt(phba);
3755 if (!fast_path_evt)
3756 return;
3757 fast_path_evt->un.scsi_evt.event_type =
3758 FC_REG_SCSI_EVENT;
3759 fast_path_evt->un.scsi_evt.subcategory =
3760 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3761 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3762 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3763 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3764 &pnode->nlp_portname, sizeof(struct lpfc_name));
3765 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3766 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3767 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3768 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3769 fast_path_evt = lpfc_alloc_fast_evt(phba);
3770 if (!fast_path_evt)
3771 return;
3772 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3773 FC_REG_SCSI_EVENT;
3774 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3775 LPFC_EVENT_CHECK_COND;
3776 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3777 cmnd->device->lun;
3778 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3779 &pnode->nlp_portname, sizeof(struct lpfc_name));
3780 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3781 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3782 fast_path_evt->un.check_cond_evt.sense_key =
3783 cmnd->sense_buffer[2] & 0xf;
3784 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3785 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3786 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3787 fcpi_parm &&
3788 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3789 ((scsi_status == SAM_STAT_GOOD) &&
3790 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3791 /*
3792 * If status is good or resid does not match with fcp_param and
3793 * there is valid fcpi_parm, then there is a read_check error
3794 */
3795 fast_path_evt = lpfc_alloc_fast_evt(phba);
3796 if (!fast_path_evt)
3797 return;
3798 fast_path_evt->un.read_check_error.header.event_type =
3799 FC_REG_FABRIC_EVENT;
3800 fast_path_evt->un.read_check_error.header.subcategory =
3801 LPFC_EVENT_FCPRDCHKERR;
3802 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3803 &pnode->nlp_portname, sizeof(struct lpfc_name));
3804 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3805 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3806 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3807 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3808 fast_path_evt->un.read_check_error.fcpiparam =
3809 fcpi_parm;
3810 } else
3811 return;
3812
3813 fast_path_evt->vport = vport;
3814 spin_lock_irqsave(&phba->hbalock, flags);
3815 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3816 spin_unlock_irqrestore(&phba->hbalock, flags);
3817 lpfc_worker_wake_up(phba);
3818 return;
3819}
James Smart9bad7672008-12-04 22:39:02 -05003820
3821/**
James Smartf1126682009-06-10 17:22:44 -04003822 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003823 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003824 * @psb: The scsi buffer which is going to be un-mapped.
3825 *
3826 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003827 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003828 **/
dea31012005-04-17 16:05:31 -05003829static void
James Smartf1126682009-06-10 17:22:44 -04003830lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003831{
3832 /*
3833 * There are only two special cases to consider. (1) the scsi command
3834 * requested scatter-gather usage or (2) the scsi command allocated
3835 * a request buffer, but did not request use_sg. There is a third
3836 * case, but it does not require resource deallocation.
3837 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003838 if (psb->seg_cnt > 0)
3839 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003840 if (psb->prot_seg_cnt > 0)
3841 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3842 scsi_prot_sg_count(psb->pCmd),
3843 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003844}
3845
James Smart9bad7672008-12-04 22:39:02 -05003846/**
James Smart3621a712009-04-06 18:47:14 -04003847 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003848 * @vport: The virtual port for which this call is being executed.
3849 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3850 * @rsp_iocb: The response IOCB which contains FCP error.
3851 *
3852 * This routine is called to process response IOCB with status field
3853 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3854 * based upon SCSI and FCP error.
3855 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003856static void
James Smart2e0fef82007-06-17 19:56:36 -05003857lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3858 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003859{
3860 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3861 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3862 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003863 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003864 uint32_t resp_info = fcprsp->rspStatus2;
3865 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003866 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003867 uint32_t host_status = DID_OK;
3868 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05003869 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003870
James Smartea2151b2008-09-07 11:52:10 -04003871
dea31012005-04-17 16:05:31 -05003872 /*
3873 * If this is a task management command, there is no
3874 * scsi packet associated with this lpfc_cmd. The driver
3875 * consumes it.
3876 */
3877 if (fcpcmd->fcpCntl2) {
3878 scsi_status = 0;
3879 goto out;
3880 }
3881
James Smart6a9c52c2009-10-02 15:16:51 -04003882 if (resp_info & RSP_LEN_VALID) {
3883 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003884 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04003885 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3886 "2719 Invalid response length: "
3887 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
3888 cmnd->device->id,
3889 cmnd->device->lun, cmnd->cmnd[0],
3890 rsplen);
3891 host_status = DID_ERROR;
3892 goto out;
3893 }
James Smarte40a02c2010-02-26 14:13:54 -05003894 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3895 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3896 "2757 Protocol failure detected during "
3897 "processing of FCP I/O op: "
3898 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
3899 cmnd->device->id,
3900 cmnd->device->lun, cmnd->cmnd[0],
3901 fcprsp->rspInfo3);
3902 host_status = DID_ERROR;
3903 goto out;
3904 }
James Smart6a9c52c2009-10-02 15:16:51 -04003905 }
3906
James Smartc7743952006-12-02 13:34:42 -05003907 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3908 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3909 if (snslen > SCSI_SENSE_BUFFERSIZE)
3910 snslen = SCSI_SENSE_BUFFERSIZE;
3911
3912 if (resp_info & RSP_LEN_VALID)
3913 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3914 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3915 }
3916 lp = (uint32_t *)cmnd->sense_buffer;
3917
James Smartaa1c7ee2012-08-14 14:26:06 -04003918 /* special handling for under run conditions */
3919 if (!scsi_status && (resp_info & RESID_UNDER)) {
3920 /* don't log under runs if fcp set... */
3921 if (vport->cfg_log_verbose & LOG_FCP)
3922 logit = LOG_FCP_ERROR;
3923 /* unless operator says so */
3924 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3925 logit = LOG_FCP_UNDER;
3926 }
James Smartc7743952006-12-02 13:34:42 -05003927
James Smarte8b62012007-08-02 11:10:09 -04003928 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003929 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003930 "Data: x%x x%x x%x x%x x%x\n",
3931 cmnd->cmnd[0], scsi_status,
3932 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3933 be32_to_cpu(fcprsp->rspResId),
3934 be32_to_cpu(fcprsp->rspSnsLen),
3935 be32_to_cpu(fcprsp->rspRspLen),
3936 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003937
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003938 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05003939 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003940 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003941
James Smart73d91e52011-10-10 21:32:10 -04003942 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smarte2a0a9d2008-12-04 22:40:02 -05003943 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003944 "residual %d Data: x%x x%x x%x\n",
3945 be32_to_cpu(fcpcmd->fcpDl),
3946 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3947 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003948
3949 /*
James Smart7054a602007-04-25 09:52:34 -04003950 * If there is an under run check if under run reported by
3951 * storage array is same as the under run reported by HBA.
3952 * If this is not same, there is a dropped frame.
3953 */
3954 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3955 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003956 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003957 lpfc_printf_vlog(vport, KERN_WARNING,
3958 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003959 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003960 "and Underrun Data: x%x x%x x%x x%x\n",
3961 be32_to_cpu(fcpcmd->fcpDl),
3962 scsi_get_resid(cmnd), fcpi_parm,
3963 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003964 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003965 host_status = DID_ERROR;
3966 }
3967 /*
dea31012005-04-17 16:05:31 -05003968 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003969 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003970 * is not present, make sure the actual amount transferred is at
3971 * least the underflow value or fail.
3972 */
3973 if (!(resp_info & SNS_LEN_VALID) &&
3974 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003975 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3976 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003977 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003978 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003979 "underrun converted to error "
3980 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003981 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003982 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003983 host_status = DID_ERROR;
3984 }
3985 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003986 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003987 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003988 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003989 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003990 host_status = DID_ERROR;
3991
3992 /*
3993 * Check SLI validation that all the transfer was actually done
James Smart26373d22013-09-06 12:19:17 -04003994 * (fcpi_parm should be zero). Apply check only to reads.
dea31012005-04-17 16:05:31 -05003995 */
James Smart26373d22013-09-06 12:19:17 -04003996 } else if (fcpi_parm && (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -04003997 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart26373d22013-09-06 12:19:17 -04003998 "9029 FCP Read Check Error Data: "
James Smarteee88772010-09-29 11:19:08 -04003999 "x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004000 be32_to_cpu(fcpcmd->fcpDl),
4001 be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04004002 fcpi_parm, cmnd->cmnd[0], scsi_status);
4003 switch (scsi_status) {
4004 case SAM_STAT_GOOD:
4005 case SAM_STAT_CHECK_CONDITION:
4006 /* Fabric dropped a data frame. Fail any successful
4007 * command in which we detected dropped frames.
4008 * A status of good or some check conditions could
4009 * be considered a successful command.
4010 */
4011 host_status = DID_ERROR;
4012 break;
4013 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05004014 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05004015 }
4016
4017 out:
4018 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04004019 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05004020}
4021
James Smart9bad7672008-12-04 22:39:02 -05004022/**
James Smart3621a712009-04-06 18:47:14 -04004023 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05004024 * @phba: The Hba for which this call is being executed.
4025 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04004026 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05004027 *
4028 * This routine assigns scsi command result by looking into response IOCB
4029 * status field appropriately. This routine handles QUEUE FULL condition as
4030 * well by ramping down device queue depth.
4031 **/
dea31012005-04-17 16:05:31 -05004032static void
4033lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
4034 struct lpfc_iocbq *pIocbOut)
4035{
4036 struct lpfc_scsi_buf *lpfc_cmd =
4037 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05004038 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05004039 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
4040 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04004041 struct scsi_cmnd *cmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004042 int result;
James Smarta257bf92009-04-06 18:48:10 -04004043 struct scsi_device *tmp_sdev;
James Smart5ffc2662009-11-18 15:39:44 -05004044 int depth;
James Smartfa61a542008-01-11 01:52:42 -05004045 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04004046 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04004047 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04004048 uint32_t queue_depth, scsi_id;
James Smart73d91e52011-10-10 21:32:10 -04004049 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05004050
James Smart75baf692010-06-08 18:31:21 -04004051 /* Sanity check on return of outstanding command */
4052 if (!(lpfc_cmd->pCmd))
4053 return;
4054 cmd = lpfc_cmd->pCmd;
4055 shost = cmd->device->host;
4056
James Smarte3d2b802012-08-14 14:25:43 -04004057 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
dea31012005-04-17 16:05:31 -05004058 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05004059 /* pick up SLI4 exhange busy status from HBA */
4060 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
4061
James Smart9a6b09c2012-03-01 22:37:42 -05004062#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4063 if (lpfc_cmd->prot_data_type) {
4064 struct scsi_dif_tuple *src = NULL;
4065
4066 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
4067 /*
4068 * Used to restore any changes to protection
4069 * data for error injection.
4070 */
4071 switch (lpfc_cmd->prot_data_type) {
4072 case LPFC_INJERR_REFTAG:
4073 src->ref_tag =
4074 lpfc_cmd->prot_data;
4075 break;
4076 case LPFC_INJERR_APPTAG:
4077 src->app_tag =
4078 (uint16_t)lpfc_cmd->prot_data;
4079 break;
4080 case LPFC_INJERR_GUARD:
4081 src->guard_tag =
4082 (uint16_t)lpfc_cmd->prot_data;
4083 break;
4084 default:
4085 break;
4086 }
4087
4088 lpfc_cmd->prot_data = 0;
4089 lpfc_cmd->prot_data_type = 0;
4090 lpfc_cmd->prot_data_segment = NULL;
4091 }
4092#endif
James Smart109f6ed2008-12-04 22:39:08 -05004093 if (pnode && NLP_CHK_NODE_ACT(pnode))
4094 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05004095
4096 if (lpfc_cmd->status) {
4097 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
4098 (lpfc_cmd->result & IOERR_DRVR_MASK))
4099 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4100 else if (lpfc_cmd->status >= IOSTAT_CNT)
4101 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smartaa1c7ee2012-08-14 14:26:06 -04004102 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
4103 !lpfc_cmd->fcp_rsp->rspStatus3 &&
4104 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
4105 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
James Smart73d91e52011-10-10 21:32:10 -04004106 logit = 0;
4107 else
4108 logit = LOG_FCP | LOG_FCP_UNDER;
4109 lpfc_printf_vlog(vport, KERN_WARNING, logit,
4110 "9030 FCP cmd x%x failed <%d/%d> "
James Smart5a0d80f2012-05-09 21:18:20 -04004111 "status: x%x result: x%x "
4112 "sid: x%x did: x%x oxid: x%x "
4113 "Data: x%x x%x\n",
James Smart73d91e52011-10-10 21:32:10 -04004114 cmd->cmnd[0],
4115 cmd->device ? cmd->device->id : 0xffff,
4116 cmd->device ? cmd->device->lun : 0xffff,
4117 lpfc_cmd->status, lpfc_cmd->result,
James Smart3bf41ba2013-05-31 17:03:18 -04004118 vport->fc_myDID,
4119 (pnode) ? pnode->nlp_DID : 0,
James Smart5a0d80f2012-05-09 21:18:20 -04004120 phba->sli_rev == LPFC_SLI_REV4 ?
4121 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
James Smart73d91e52011-10-10 21:32:10 -04004122 pIocbOut->iocb.ulpContext,
4123 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05004124
4125 switch (lpfc_cmd->status) {
4126 case IOSTAT_FCP_RSP_ERROR:
4127 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05004128 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05004129 break;
4130 case IOSTAT_NPORT_BSY:
4131 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04004132 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04004133 fast_path_evt = lpfc_alloc_fast_evt(phba);
4134 if (!fast_path_evt)
4135 break;
4136 fast_path_evt->un.fabric_evt.event_type =
4137 FC_REG_FABRIC_EVENT;
4138 fast_path_evt->un.fabric_evt.subcategory =
4139 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
4140 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
4141 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4142 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
4143 &pnode->nlp_portname,
4144 sizeof(struct lpfc_name));
4145 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
4146 &pnode->nlp_nodename,
4147 sizeof(struct lpfc_name));
4148 }
4149 fast_path_evt->vport = vport;
4150 fast_path_evt->work_evt.evt =
4151 LPFC_EVT_FASTPATH_MGMT_EVT;
4152 spin_lock_irqsave(&phba->hbalock, flags);
4153 list_add_tail(&fast_path_evt->work_evt.evt_listp,
4154 &phba->work_list);
4155 spin_unlock_irqrestore(&phba->hbalock, flags);
4156 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004157 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004158 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05004159 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05004160 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
4161 lpfc_cmd->result ==
4162 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
4163 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
4164 lpfc_cmd->result ==
4165 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
4166 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
4167 break;
4168 }
James Smartd7c255b2008-08-24 21:50:00 -04004169 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05004170 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04004171 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
4172 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
James Smart92d7f7b2007-06-17 19:56:38 -05004173 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04004174 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05004175 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004176 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
4177 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
4178 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
4179 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
4180 /*
4181 * This is a response for a BG enabled
4182 * cmd. Parse BG error
4183 */
4184 lpfc_parse_bg_err(phba, lpfc_cmd,
4185 pIocbOut);
4186 break;
4187 } else {
4188 lpfc_printf_vlog(vport, KERN_WARNING,
4189 LOG_BG,
4190 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04004191 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05004192 }
4193 }
James Smart1151e3e2011-02-16 12:39:35 -05004194 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
4195 && (phba->sli_rev == LPFC_SLI_REV4)
4196 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
4197 /* This IO was aborted by the target, we don't
4198 * know the rxid and because we did not send the
4199 * ABTS we cannot generate and RRQ.
4200 */
4201 lpfc_set_rrq_active(phba, pnode,
James Smartee0f4fe2012-05-09 21:19:14 -04004202 lpfc_cmd->cur_iocbq.sli4_lxritag,
4203 0, 0);
James Smart1151e3e2011-02-16 12:39:35 -05004204 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004205 /* else: fall through */
dea31012005-04-17 16:05:31 -05004206 default:
4207 cmd->result = ScsiResult(DID_ERROR, 0);
4208 break;
4209 }
4210
James Smart58da1ff2008-04-07 10:15:56 -04004211 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004212 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04004213 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
4214 SAM_STAT_BUSY);
James Smartab56dc22011-02-16 12:39:57 -05004215 } else
dea31012005-04-17 16:05:31 -05004216 cmd->result = ScsiResult(DID_OK, 0);
dea31012005-04-17 16:05:31 -05004217
4218 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4219 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
4220
James Smarte8b62012007-08-02 11:10:09 -04004221 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4222 "0710 Iodone <%d/%d> cmd %p, error "
4223 "x%x SNS x%x x%x Data: x%x x%x\n",
4224 cmd->device->id, cmd->device->lun, cmd,
4225 cmd->result, *lp, *(lp + 3), cmd->retries,
4226 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05004227 }
4228
James Smartea2151b2008-09-07 11:52:10 -04004229 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004230 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04004231 if (vport->cfg_max_scsicmpl_time &&
4232 time_after(jiffies, lpfc_cmd->start_time +
4233 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04004234 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004235 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4236 if (pnode->cmd_qdepth >
4237 atomic_read(&pnode->cmd_pending) &&
4238 (atomic_read(&pnode->cmd_pending) >
4239 LPFC_MIN_TGT_QDEPTH) &&
4240 ((cmd->cmnd[0] == READ_10) ||
4241 (cmd->cmnd[0] == WRITE_10)))
4242 pnode->cmd_qdepth =
4243 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04004244
James Smart109f6ed2008-12-04 22:39:08 -05004245 pnode->last_change_time = jiffies;
4246 }
James Smarta257bf92009-04-06 18:48:10 -04004247 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004248 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smart7dc517d2010-07-14 15:32:10 -04004249 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
James Smart977b5a02008-09-07 11:52:04 -04004250 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05004251 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04004252 spin_lock_irqsave(shost->host_lock, flags);
James Smart7dc517d2010-07-14 15:32:10 -04004253 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
4254 / 100;
4255 depth = depth ? depth : 1;
4256 pnode->cmd_qdepth += depth;
4257 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
4258 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05004259 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04004260 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004261 }
James Smart977b5a02008-09-07 11:52:04 -04004262 }
4263
James Smart1dcb58e2007-04-25 09:51:30 -04004264 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04004265
4266 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
4267 queue_depth = cmd->device->queue_depth;
4268 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05004269 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004270
Jamie Wellnitzb8086082006-02-28 22:33:12 -05004271 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart876dd7d2012-09-29 11:31:28 -04004272 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004273 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04004274 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004275
James Smartfa61a542008-01-11 01:52:42 -05004276 /*
4277 * If there is a thread waiting for command completion
4278 * wake up the thread.
4279 */
James Smarta257bf92009-04-06 18:48:10 -04004280 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004281 if (lpfc_cmd->waitq)
4282 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04004283 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05004284 lpfc_release_scsi_buf(phba, lpfc_cmd);
4285 return;
4286 }
4287
James Smart92d7f7b2007-06-17 19:56:38 -05004288 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04004289 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05004290
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004291 /*
4292 * Check for queue full. If the lun is reporting queue full, then
4293 * back off the lun queue depth to prevent target overloads.
4294 */
James Smart58da1ff2008-04-07 10:15:56 -04004295 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
4296 NLP_CHK_NODE_ACT(pnode)) {
James Smarta257bf92009-04-06 18:48:10 -04004297 shost_for_each_device(tmp_sdev, shost) {
4298 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004299 continue;
4300 depth = scsi_track_queue_full(tmp_sdev,
James Smart5ffc2662009-11-18 15:39:44 -05004301 tmp_sdev->queue_depth-1);
4302 if (depth <= 0)
4303 continue;
James Smarte8b62012007-08-02 11:10:09 -04004304 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4305 "0711 detected queue full - lun queue "
4306 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04004307 lpfc_send_sdev_queuedepth_change_event(phba, vport,
James Smart5ffc2662009-11-18 15:39:44 -05004308 pnode,
4309 tmp_sdev->lun,
4310 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004311 }
4312 }
4313
James Smart876dd7d2012-09-29 11:31:28 -04004314 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004315 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04004316 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004317
James Smartfa61a542008-01-11 01:52:42 -05004318 /*
4319 * If there is a thread waiting for command completion
4320 * wake up the thread.
4321 */
James Smarta257bf92009-04-06 18:48:10 -04004322 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004323 if (lpfc_cmd->waitq)
4324 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04004325 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004326
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004327 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004328}
4329
James Smart34b02dc2008-08-24 21:49:55 -04004330/**
James Smart3621a712009-04-06 18:47:14 -04004331 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04004332 * @data: A pointer to the immediate command data portion of the IOCB.
4333 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
4334 *
4335 * The routine copies the entire FCP command from @fcp_cmnd to @data while
4336 * byte swapping the data to big endian format for transmission on the wire.
4337 **/
4338static void
4339lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
4340{
4341 int i, j;
4342 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
4343 i += sizeof(uint32_t), j++) {
4344 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
4345 }
4346}
4347
James Smart9bad7672008-12-04 22:39:02 -05004348/**
James Smartf1126682009-06-10 17:22:44 -04004349 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004350 * @vport: The virtual port for which this call is being executed.
4351 * @lpfc_cmd: The scsi command which needs to send.
4352 * @pnode: Pointer to lpfc_nodelist.
4353 *
4354 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04004355 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004356 **/
dea31012005-04-17 16:05:31 -05004357static void
James Smartf1126682009-06-10 17:22:44 -04004358lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05004359 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05004360{
James Smart2e0fef82007-06-17 19:56:36 -05004361 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004362 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4363 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4364 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4365 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
4366 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04004367 char tag[2];
James Smart027140e2012-08-03 12:35:44 -04004368 uint8_t *ptr;
4369 bool sli4;
James Smart98bbf5f2013-09-06 12:18:45 -04004370 uint32_t fcpdl;
dea31012005-04-17 16:05:31 -05004371
James Smart58da1ff2008-04-07 10:15:56 -04004372 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4373 return;
4374
dea31012005-04-17 16:05:31 -05004375 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04004376 /* clear task management bits */
4377 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05004378
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04004379 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4380 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004381
James Smart027140e2012-08-03 12:35:44 -04004382 ptr = &fcp_cmnd->fcpCdb[0];
4383 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4384 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4385 ptr += scsi_cmnd->cmd_len;
4386 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4387 }
4388
James Smart7e2b19f2007-10-29 11:00:39 -04004389 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
4390 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05004391 case HEAD_OF_QUEUE_TAG:
4392 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
4393 break;
4394 case ORDERED_QUEUE_TAG:
4395 fcp_cmnd->fcpCntl1 = ORDERED_Q;
4396 break;
4397 default:
4398 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
4399 break;
4400 }
4401 } else
James Smartfe8f7f92013-01-03 15:43:03 -05004402 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
dea31012005-04-17 16:05:31 -05004403
James Smart027140e2012-08-03 12:35:44 -04004404 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
4405
dea31012005-04-17 16:05:31 -05004406 /*
4407 * There are three possibilities here - use scatter-gather segment, use
4408 * the single mapping, or neither. Start the lpfc command prep by
4409 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4410 * data bde entry.
4411 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05004412 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05004413 if (datadir == DMA_TO_DEVICE) {
4414 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart182ba752013-07-15 18:34:05 -04004415 iocb_cmd->ulpPU = PARM_READ_CHECK;
James Smart3cb01c52013-07-15 18:35:04 -04004416 if (vport->cfg_first_burst_size &&
4417 (pnode->nlp_flag & NLP_FIRSTBURST)) {
James Smart98bbf5f2013-09-06 12:18:45 -04004418 fcpdl = scsi_bufflen(scsi_cmnd);
4419 if (fcpdl < vport->cfg_first_burst_size)
4420 piocbq->iocb.un.fcpi.fcpi_XRdy = fcpdl;
4421 else
4422 piocbq->iocb.un.fcpi.fcpi_XRdy =
4423 vport->cfg_first_burst_size;
James Smart3cb01c52013-07-15 18:35:04 -04004424 }
dea31012005-04-17 16:05:31 -05004425 fcp_cmnd->fcpCntl3 = WRITE_DATA;
4426 phba->fc4OutputRequests++;
4427 } else {
4428 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4429 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05004430 fcp_cmnd->fcpCntl3 = READ_DATA;
4431 phba->fc4InputRequests++;
4432 }
4433 } else {
4434 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4435 iocb_cmd->un.fcpi.fcpi_parm = 0;
4436 iocb_cmd->ulpPU = 0;
4437 fcp_cmnd->fcpCntl3 = 0;
4438 phba->fc4ControlRequests++;
4439 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004440 if (phba->sli_rev == 3 &&
4441 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004442 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004443 /*
4444 * Finish initializing those IOCB fields that are independent
4445 * of the scsi_cmnd request_buffer
4446 */
4447 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart027140e2012-08-03 12:35:44 -04004448 if (sli4)
James Smart6d368e52011-05-24 11:44:12 -04004449 piocbq->iocb.ulpContext =
4450 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05004451 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4452 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05004453 else
4454 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05004455
4456 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4457 piocbq->context1 = lpfc_cmd;
4458 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4459 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05004460 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05004461}
4462
James Smart9bad7672008-12-04 22:39:02 -05004463/**
James Smart6d368e52011-05-24 11:44:12 -04004464 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004465 * @vport: The virtual port for which this call is being executed.
4466 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4467 * @lun: Logical unit number.
4468 * @task_mgmt_cmd: SCSI task management command.
4469 *
James Smart3772a992009-05-22 14:50:54 -04004470 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4471 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004472 *
4473 * Return codes:
4474 * 0 - Error
4475 * 1 - Success
4476 **/
dea31012005-04-17 16:05:31 -05004477static int
James Smartf1126682009-06-10 17:22:44 -04004478lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05004479 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04004480 unsigned int lun,
dea31012005-04-17 16:05:31 -05004481 uint8_t task_mgmt_cmd)
4482{
dea31012005-04-17 16:05:31 -05004483 struct lpfc_iocbq *piocbq;
4484 IOCB_t *piocb;
4485 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04004486 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05004487 struct lpfc_nodelist *ndlp = rdata->pnode;
4488
James Smart58da1ff2008-04-07 10:15:56 -04004489 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4490 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05004491 return 0;
dea31012005-04-17 16:05:31 -05004492
dea31012005-04-17 16:05:31 -05004493 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004494 piocbq->vport = vport;
4495
dea31012005-04-17 16:05:31 -05004496 piocb = &piocbq->iocb;
4497
4498 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04004499 /* Clear out any old data in the FCP command area */
4500 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4501 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004502 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05004503 if (vport->phba->sli_rev == 3 &&
4504 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004505 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004506 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05004507 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04004508 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4509 piocb->ulpContext =
4510 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4511 }
James Smart53151bb2013-10-10 12:24:07 -04004512 piocb->ulpFCP2Rcvy = (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) ? 1 : 0;
dea31012005-04-17 16:05:31 -05004513 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
4514
4515 /* ulpTimeout is only one byte */
4516 if (lpfc_cmd->timeout > 0xff) {
4517 /*
4518 * Do not timeout the command at the firmware level.
4519 * The driver will provide the timeout mechanism.
4520 */
4521 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04004522 } else
dea31012005-04-17 16:05:31 -05004523 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04004524
4525 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4526 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004527
James Smart2e0fef82007-06-17 19:56:36 -05004528 return 1;
dea31012005-04-17 16:05:31 -05004529}
4530
James Smart9bad7672008-12-04 22:39:02 -05004531/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004532 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04004533 * @phba: The hba struct for which this call is being executed.
4534 * @dev_grp: The HBA PCI-Device group number.
4535 *
4536 * This routine sets up the SCSI interface API function jump table in @phba
4537 * struct.
4538 * Returns: 0 - success, -ENODEV - failure.
4539 **/
4540int
4541lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4542{
4543
James Smartf1126682009-06-10 17:22:44 -04004544 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4545 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04004546
James Smart3772a992009-05-22 14:50:54 -04004547 switch (dev_grp) {
4548 case LPFC_PCI_DEV_LP:
4549 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4550 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05004551 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004552 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05004553 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004554 break;
James Smartda0436e2009-05-22 14:51:39 -04004555 case LPFC_PCI_DEV_OC:
4556 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4557 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05004558 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004559 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05004560 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004561 break;
James Smart3772a992009-05-22 14:50:54 -04004562 default:
4563 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4564 "1418 Invalid HBA PCI-device group: 0x%x\n",
4565 dev_grp);
4566 return -ENODEV;
4567 break;
4568 }
James Smart3772a992009-05-22 14:50:54 -04004569 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05004570 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04004571 return 0;
4572}
4573
4574/**
James Smart3621a712009-04-06 18:47:14 -04004575 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05004576 * @phba: The Hba for which this call is being executed.
4577 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4578 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4579 *
4580 * This routine is IOCB completion routine for device reset and target reset
4581 * routine. This routine release scsi buffer associated with lpfc_cmd.
4582 **/
James Smart7054a602007-04-25 09:52:34 -04004583static void
4584lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4585 struct lpfc_iocbq *cmdiocbq,
4586 struct lpfc_iocbq *rspiocbq)
4587{
4588 struct lpfc_scsi_buf *lpfc_cmd =
4589 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4590 if (lpfc_cmd)
4591 lpfc_release_scsi_buf(phba, lpfc_cmd);
4592 return;
4593}
4594
James Smart9bad7672008-12-04 22:39:02 -05004595/**
James Smart3621a712009-04-06 18:47:14 -04004596 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05004597 * @host: The scsi host for which this call is being executed.
4598 *
4599 * This routine provides module information about hba.
4600 *
4601 * Reutrn code:
4602 * Pointer to char - Success.
4603 **/
dea31012005-04-17 16:05:31 -05004604const char *
4605lpfc_info(struct Scsi_Host *host)
4606{
James Smart2e0fef82007-06-17 19:56:36 -05004607 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4608 struct lpfc_hba *phba = vport->phba;
James Smart8b68cd52012-09-29 11:32:37 -04004609 int len, link_speed = 0;
dea31012005-04-17 16:05:31 -05004610 static char lpfcinfobuf[384];
4611
4612 memset(lpfcinfobuf,0,384);
4613 if (phba && phba->pcidev){
4614 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4615 len = strlen(lpfcinfobuf);
4616 snprintf(lpfcinfobuf + len,
4617 384-len,
4618 " on PCI bus %02x device %02x irq %d",
4619 phba->pcidev->bus->number,
4620 phba->pcidev->devfn,
4621 phba->pcidev->irq);
4622 len = strlen(lpfcinfobuf);
4623 if (phba->Port[0]) {
4624 snprintf(lpfcinfobuf + len,
4625 384-len,
4626 " port %s",
4627 phba->Port);
4628 }
James Smart65467b62010-01-26 23:08:29 -05004629 len = strlen(lpfcinfobuf);
James Smart8b68cd52012-09-29 11:32:37 -04004630 if (phba->sli_rev <= LPFC_SLI_REV3) {
4631 link_speed = lpfc_sli_port_speed_get(phba);
4632 } else {
4633 if (phba->sli4_hba.link_state.logical_speed)
4634 link_speed =
4635 phba->sli4_hba.link_state.logical_speed;
4636 else
4637 link_speed = phba->sli4_hba.link_state.speed;
James Smart65467b62010-01-26 23:08:29 -05004638 }
James Smart8b68cd52012-09-29 11:32:37 -04004639 if (link_speed != 0)
4640 snprintf(lpfcinfobuf + len, 384-len,
4641 " Logical Link Speed: %d Mbps", link_speed);
dea31012005-04-17 16:05:31 -05004642 }
4643 return lpfcinfobuf;
4644}
4645
James Smart9bad7672008-12-04 22:39:02 -05004646/**
James Smart3621a712009-04-06 18:47:14 -04004647 * lpfc_poll_rearm_time - Routine to modify 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 modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4651 * The default value of cfg_poll_tmo is 10 milliseconds.
4652 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004653static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4654{
4655 unsigned long poll_tmo_expires =
4656 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4657
James Smart0e9bb8d2013-03-01 16:35:12 -05004658 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004659 mod_timer(&phba->fcp_poll_timer,
4660 poll_tmo_expires);
4661}
4662
James Smart9bad7672008-12-04 22:39:02 -05004663/**
James Smart3621a712009-04-06 18:47:14 -04004664 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004665 * @phba: The Hba for which this call is being executed.
4666 *
4667 * This routine starts the fcp_poll_timer of @phba.
4668 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004669void lpfc_poll_start_timer(struct lpfc_hba * phba)
4670{
4671 lpfc_poll_rearm_timer(phba);
4672}
4673
James Smart9bad7672008-12-04 22:39:02 -05004674/**
James Smart3621a712009-04-06 18:47:14 -04004675 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05004676 * @ptr: Map to lpfc_hba data structure pointer.
4677 *
4678 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4679 * and FCP Ring interrupt is disable.
4680 **/
4681
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004682void lpfc_poll_timeout(unsigned long ptr)
4683{
James Smart2e0fef82007-06-17 19:56:36 -05004684 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004685
4686 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004687 lpfc_sli_handle_fast_ring_event(phba,
4688 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4689
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004690 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4691 lpfc_poll_rearm_timer(phba);
4692 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004693}
4694
James Smart9bad7672008-12-04 22:39:02 -05004695/**
James Smart3621a712009-04-06 18:47:14 -04004696 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05004697 * @cmnd: Pointer to scsi_cmnd data structure.
4698 * @done: Pointer to done routine.
4699 *
4700 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4701 * This routine prepares an IOCB from scsi command and provides to firmware.
4702 * The @done callback is invoked after driver finished processing the command.
4703 *
4704 * Return value :
4705 * 0 - Success
4706 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4707 **/
dea31012005-04-17 16:05:31 -05004708static int
James Smartb9a7c632012-08-03 12:35:24 -04004709lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004710{
James Smart2e0fef82007-06-17 19:56:36 -05004711 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4712 struct lpfc_hba *phba = vport->phba;
James Smart1ba981f2014-02-20 09:56:45 -05004713 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004714 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004715 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004716 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004717 int err;
dea31012005-04-17 16:05:31 -05004718
James Smart1ba981f2014-02-20 09:56:45 -05004719 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004720 err = fc_remote_port_chkready(rport);
4721 if (err) {
4722 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004723 goto out_fail_command;
4724 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004725 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004726
James Smartbf086112011-08-21 21:48:13 -04004727 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004728 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004729
James Smart6a9c52c2009-10-02 15:16:51 -04004730 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4731 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4732 " op:%02x str=%s without registering for"
4733 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004734 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4735 dif_op_str[scsi_get_prot_op(cmnd)]);
4736 goto out_fail_command;
4737 }
4738
dea31012005-04-17 16:05:31 -05004739 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004740 * Catch race where our node has transitioned, but the
4741 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004742 */
James Smart6b415f52012-06-12 13:54:59 -04004743 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4744 goto out_tgt_busy;
James Smart7dc517d2010-07-14 15:32:10 -04004745 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
Mike Christie34963432011-02-25 14:04:28 -06004746 goto out_tgt_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05004747
James Smart19ca7602010-11-20 23:11:55 -05004748 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05004749 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004750 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004751
James Smarte8b62012007-08-02 11:10:09 -04004752 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4753 "0707 driver's buffer pool is empty, "
4754 "IO busied\n");
dea31012005-04-17 16:05:31 -05004755 goto out_host_busy;
4756 }
4757
4758 /*
4759 * Store the midlayer's command structure for the completion phase
4760 * and complete the command initialization.
4761 */
4762 lpfc_cmd->pCmd = cmnd;
4763 lpfc_cmd->rdata = rdata;
4764 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04004765 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05004766 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
dea31012005-04-17 16:05:31 -05004767
James Smarte2a0a9d2008-12-04 22:40:02 -05004768 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004769 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004770 lpfc_printf_vlog(vport,
4771 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004772 "9033 BLKGRD: rcvd %s cmd:x%x "
4773 "sector x%llx cnt %u pt %x\n",
4774 dif_op_str[scsi_get_prot_op(cmnd)],
4775 cmnd->cmnd[0],
4776 (unsigned long long)scsi_get_lba(cmnd),
4777 blk_rq_sectors(cmnd->request),
4778 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004779 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004780 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4781 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004782 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004783 lpfc_printf_vlog(vport,
4784 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004785 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4786 "x%x sector x%llx cnt %u pt %x\n",
4787 cmnd->cmnd[0],
4788 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004789 blk_rq_sectors(cmnd->request),
James Smart26134702012-08-14 14:25:50 -04004790 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004791 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004792 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4793 }
4794
dea31012005-04-17 16:05:31 -05004795 if (err)
4796 goto out_host_busy_free_buf;
4797
James Smart2e0fef82007-06-17 19:56:36 -05004798 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004799
James Smart977b5a02008-09-07 11:52:04 -04004800 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04004801 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004802 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05004803 if (err) {
4804 atomic_dec(&ndlp->cmd_pending);
James Smart76f96b62013-12-17 20:29:01 -05004805 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4806 "3376 FCP could not issue IOCB err %x"
4807 "FCP cmd x%x <%d/%d> "
4808 "sid: x%x did: x%x oxid: x%x "
4809 "Data: x%x x%x x%x x%x\n",
4810 err, cmnd->cmnd[0],
4811 cmnd->device ? cmnd->device->id : 0xffff,
4812 cmnd->device ? cmnd->device->lun : 0xffff,
4813 vport->fc_myDID, ndlp->nlp_DID,
4814 phba->sli_rev == LPFC_SLI_REV4 ?
4815 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
4816 lpfc_cmd->cur_iocbq.iocb.ulpContext,
4817 lpfc_cmd->cur_iocbq.iocb.ulpIoTag,
4818 lpfc_cmd->cur_iocbq.iocb.ulpTimeout,
4819 (uint32_t)
4820 (cmnd->request->timeout / 1000));
4821
4822
dea31012005-04-17 16:05:31 -05004823 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004824 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004825 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004826 lpfc_sli_handle_fast_ring_event(phba,
4827 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4828
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004829 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4830 lpfc_poll_rearm_timer(phba);
4831 }
4832
dea31012005-04-17 16:05:31 -05004833 return 0;
4834
4835 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04004836 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004837 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004838 out_host_busy:
4839 return SCSI_MLQUEUE_HOST_BUSY;
4840
Mike Christie34963432011-02-25 14:04:28 -06004841 out_tgt_busy:
4842 return SCSI_MLQUEUE_TARGET_BUSY;
4843
dea31012005-04-17 16:05:31 -05004844 out_fail_command:
James Smartb9a7c632012-08-03 12:35:24 -04004845 cmnd->scsi_done(cmnd);
dea31012005-04-17 16:05:31 -05004846 return 0;
4847}
4848
Jeff Garzikf2812332010-11-16 02:10:29 -05004849
James Smart9bad7672008-12-04 22:39:02 -05004850/**
James Smart3621a712009-04-06 18:47:14 -04004851 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004852 * @cmnd: Pointer to scsi_cmnd data structure.
4853 *
4854 * This routine aborts @cmnd pending in base driver.
4855 *
4856 * Return code :
4857 * 0x2003 - Error
4858 * 0x2002 - Success
4859 **/
dea31012005-04-17 16:05:31 -05004860static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004861lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004862{
James Smart2e0fef82007-06-17 19:56:36 -05004863 struct Scsi_Host *shost = cmnd->device->host;
4864 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4865 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004866 struct lpfc_iocbq *iocb;
4867 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004868 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004869 IOCB_t *cmd, *icmd;
James Smart3a707302012-06-12 13:54:42 -04004870 int ret = SUCCESS, status = 0;
James Smart876dd7d2012-09-29 11:31:28 -04004871 unsigned long flags;
James Smartfa61a542008-01-11 01:52:42 -05004872 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004873
James Smart3a707302012-06-12 13:54:42 -04004874 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004875 if (status != 0 && status != SUCCESS)
James Smart3a707302012-06-12 13:54:42 -04004876 return status;
James Smart4f2e66c2012-05-09 21:17:07 -04004877
James Smart876dd7d2012-09-29 11:31:28 -04004878 spin_lock_irqsave(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004879 /* driver queued commands are in process of being flushed */
4880 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
James Smart876dd7d2012-09-29 11:31:28 -04004881 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004882 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4883 "3168 SCSI Layer abort requested I/O has been "
4884 "flushed by LLD.\n");
4885 return FAILED;
4886 }
4887
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004888 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smart92e3af62012-08-14 14:26:28 -04004889 if (!lpfc_cmd || !lpfc_cmd->pCmd) {
James Smart876dd7d2012-09-29 11:31:28 -04004890 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarteee88772010-09-29 11:19:08 -04004891 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4892 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004893 "x%x ID %d LUN %d\n",
James Smart3a707302012-06-12 13:54:42 -04004894 SUCCESS, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04004895 return SUCCESS;
4896 }
dea31012005-04-17 16:05:31 -05004897
James Smart4f2e66c2012-05-09 21:17:07 -04004898 iocb = &lpfc_cmd->cur_iocbq;
4899 /* the command is in process of being cancelled */
4900 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
James Smart876dd7d2012-09-29 11:31:28 -04004901 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004902 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4903 "3169 SCSI Layer abort requested I/O has been "
4904 "cancelled by LLD.\n");
4905 return FAILED;
4906 }
dea31012005-04-17 16:05:31 -05004907 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004908 * If pCmd field of the corresponding lpfc_scsi_buf structure
4909 * points to a different SCSI command, then the driver has
4910 * already completed this command, but the midlayer did not
James Smart4f2e66c2012-05-09 21:17:07 -04004911 * see the completion before the eh fired. Just return SUCCESS.
dea31012005-04-17 16:05:31 -05004912 */
James Smart4f2e66c2012-05-09 21:17:07 -04004913 if (lpfc_cmd->pCmd != cmnd) {
4914 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4915 "3170 SCSI Layer abort requested I/O has been "
4916 "completed by LLD.\n");
4917 goto out_unlock;
4918 }
dea31012005-04-17 16:05:31 -05004919
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004920 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004921
James Smart4f2e66c2012-05-09 21:17:07 -04004922 abtsiocb = __lpfc_sli_get_iocbq(phba);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004923 if (abtsiocb == NULL) {
4924 ret = FAILED;
James Smart4f2e66c2012-05-09 21:17:07 -04004925 goto out_unlock;
dea31012005-04-17 16:05:31 -05004926 }
4927
James Smartafbd8d82013-09-06 12:22:13 -04004928 /* Indicate the IO is being aborted by the driver. */
4929 iocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4930
dea31012005-04-17 16:05:31 -05004931 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004932 * The scsi command can not be in txq and it is in flight because the
4933 * pCmd is still pointig at the SCSI command we have to abort. There
4934 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004935 */
dea31012005-04-17 16:05:31 -05004936
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004937 cmd = &iocb->iocb;
4938 icmd = &abtsiocb->iocb;
4939 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4940 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004941 if (phba->sli_rev == LPFC_SLI_REV4)
4942 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4943 else
4944 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004945
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004946 icmd->ulpLe = 1;
4947 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004948
4949 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4950 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05004951 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05004952
James Smart2e0fef82007-06-17 19:56:36 -05004953 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004954 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4955 else
4956 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004957
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004958 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004959 abtsiocb->vport = vport;
James Smart4f2e66c2012-05-09 21:17:07 -04004960 /* no longer need the lock after this point */
James Smart876dd7d2012-09-29 11:31:28 -04004961 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004962
James Smart3772a992009-05-22 14:50:54 -04004963 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
4964 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004965 lpfc_sli_release_iocbq(phba, abtsiocb);
4966 ret = FAILED;
4967 goto out;
4968 }
4969
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004970 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004971 lpfc_sli_handle_fast_ring_event(phba,
4972 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004973
James Smartfa61a542008-01-11 01:52:42 -05004974 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004975 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004976 wait_event_timeout(waitq,
4977 (lpfc_cmd->pCmd != cmnd),
James Smart256ec0d2013-04-17 20:14:58 -04004978 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
James Smartfa61a542008-01-11 01:52:42 -05004979 lpfc_cmd->waitq = NULL;
dea31012005-04-17 16:05:31 -05004980
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004981 if (lpfc_cmd->pCmd == cmnd) {
4982 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04004983 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4984 "0748 abort handler timed out waiting "
James Smart247ca942012-08-14 14:26:13 -04004985 "for abortng I/O (xri:x%x) to complete: "
4986 "ret %#x, ID %d, LUN %d\n",
4987 iocb->sli4_xritag, ret,
4988 cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004989 }
James Smart4f2e66c2012-05-09 21:17:07 -04004990 goto out;
dea31012005-04-17 16:05:31 -05004991
James Smart4f2e66c2012-05-09 21:17:07 -04004992out_unlock:
James Smart876dd7d2012-09-29 11:31:28 -04004993 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004994out:
James Smarte8b62012007-08-02 11:10:09 -04004995 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4996 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004997 "LUN %d\n", ret, cmnd->device->id,
4998 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004999 return ret;
dea31012005-04-17 16:05:31 -05005000}
5001
James Smartbbb9d182009-06-10 17:23:16 -04005002static char *
5003lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
5004{
5005 switch (task_mgmt_cmd) {
5006 case FCP_ABORT_TASK_SET:
5007 return "ABORT_TASK_SET";
5008 case FCP_CLEAR_TASK_SET:
5009 return "FCP_CLEAR_TASK_SET";
5010 case FCP_BUS_RESET:
5011 return "FCP_BUS_RESET";
5012 case FCP_LUN_RESET:
5013 return "FCP_LUN_RESET";
5014 case FCP_TARGET_RESET:
5015 return "FCP_TARGET_RESET";
5016 case FCP_CLEAR_ACA:
5017 return "FCP_CLEAR_ACA";
5018 case FCP_TERMINATE_TASK:
5019 return "FCP_TERMINATE_TASK";
5020 default:
5021 return "unknown";
5022 }
5023}
5024
James Smart53151bb2013-10-10 12:24:07 -04005025
5026/**
5027 * lpfc_check_fcp_rsp - check the returned fcp_rsp to see if task failed
5028 * @vport: The virtual port for which this call is being executed.
5029 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
5030 *
5031 * This routine checks the FCP RSP INFO to see if the tsk mgmt command succeded
5032 *
5033 * Return code :
5034 * 0x2003 - Error
5035 * 0x2002 - Success
5036 **/
5037static int
5038lpfc_check_fcp_rsp(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd)
5039{
5040 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
5041 uint32_t rsp_info;
5042 uint32_t rsp_len;
5043 uint8_t rsp_info_code;
5044 int ret = FAILED;
5045
5046
5047 if (fcprsp == NULL)
5048 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5049 "0703 fcp_rsp is missing\n");
5050 else {
5051 rsp_info = fcprsp->rspStatus2;
5052 rsp_len = be32_to_cpu(fcprsp->rspRspLen);
5053 rsp_info_code = fcprsp->rspInfo3;
5054
5055
5056 lpfc_printf_vlog(vport, KERN_INFO,
5057 LOG_FCP,
5058 "0706 fcp_rsp valid 0x%x,"
5059 " rsp len=%d code 0x%x\n",
5060 rsp_info,
5061 rsp_len, rsp_info_code);
5062
5063 if ((fcprsp->rspStatus2&RSP_LEN_VALID) && (rsp_len == 8)) {
5064 switch (rsp_info_code) {
5065 case RSP_NO_FAILURE:
5066 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5067 "0715 Task Mgmt No Failure\n");
5068 ret = SUCCESS;
5069 break;
5070 case RSP_TM_NOT_SUPPORTED: /* TM rejected */
5071 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5072 "0716 Task Mgmt Target "
5073 "reject\n");
5074 break;
5075 case RSP_TM_NOT_COMPLETED: /* TM failed */
5076 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5077 "0717 Task Mgmt Target "
5078 "failed TM\n");
5079 break;
5080 case RSP_TM_INVALID_LU: /* TM to invalid LU! */
5081 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5082 "0718 Task Mgmt to invalid "
5083 "LUN\n");
5084 break;
5085 }
5086 }
5087 }
5088 return ret;
5089}
5090
5091
James Smartbbb9d182009-06-10 17:23:16 -04005092/**
5093 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
5094 * @vport: The virtual port for which this call is being executed.
5095 * @rdata: Pointer to remote port local data
5096 * @tgt_id: Target ID of remote device.
5097 * @lun_id: Lun number for the TMF
5098 * @task_mgmt_cmd: type of TMF to send
5099 *
5100 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
5101 * a remote port.
5102 *
5103 * Return Code:
5104 * 0x2003 - Error
5105 * 0x2002 - Success.
5106 **/
5107static int
5108lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
5109 unsigned tgt_id, unsigned int lun_id,
5110 uint8_t task_mgmt_cmd)
5111{
5112 struct lpfc_hba *phba = vport->phba;
5113 struct lpfc_scsi_buf *lpfc_cmd;
5114 struct lpfc_iocbq *iocbq;
5115 struct lpfc_iocbq *iocbqrsp;
James Smart5989b8d2010-10-22 11:06:56 -04005116 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005117 int ret;
5118 int status;
5119
James Smart5989b8d2010-10-22 11:06:56 -04005120 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smartbbb9d182009-06-10 17:23:16 -04005121 return FAILED;
5122
James Smart19ca7602010-11-20 23:11:55 -05005123 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
James Smartbbb9d182009-06-10 17:23:16 -04005124 if (lpfc_cmd == NULL)
5125 return FAILED;
James Smart0c411222013-09-06 12:22:46 -04005126 lpfc_cmd->timeout = phba->cfg_task_mgmt_tmo;
James Smartbbb9d182009-06-10 17:23:16 -04005127 lpfc_cmd->rdata = rdata;
5128
5129 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
5130 task_mgmt_cmd);
5131 if (!status) {
5132 lpfc_release_scsi_buf(phba, lpfc_cmd);
5133 return FAILED;
5134 }
5135
5136 iocbq = &lpfc_cmd->cur_iocbq;
5137 iocbqrsp = lpfc_sli_get_iocbq(phba);
5138 if (iocbqrsp == NULL) {
5139 lpfc_release_scsi_buf(phba, lpfc_cmd);
5140 return FAILED;
5141 }
James Smart5a0916b2013-07-15 18:31:42 -04005142 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
James Smartbbb9d182009-06-10 17:23:16 -04005143
5144 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5145 "0702 Issue %s to TGT %d LUN %d "
James Smart6d368e52011-05-24 11:44:12 -04005146 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04005147 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04005148 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
5149 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04005150
5151 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
5152 iocbq, iocbqrsp, lpfc_cmd->timeout);
James Smart53151bb2013-10-10 12:24:07 -04005153 if ((status != IOCB_SUCCESS) ||
5154 (iocbqrsp->iocb.ulpStatus != IOSTAT_SUCCESS)) {
James Smartbbb9d182009-06-10 17:23:16 -04005155 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart6d368e52011-05-24 11:44:12 -04005156 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
5157 "iocb_flag x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04005158 lpfc_taskmgmt_name(task_mgmt_cmd),
5159 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
James Smart6d368e52011-05-24 11:44:12 -04005160 iocbqrsp->iocb.un.ulpWord[4],
5161 iocbq->iocb_flag);
James Smart53151bb2013-10-10 12:24:07 -04005162 /* if ulpStatus != IOCB_SUCCESS, then status == IOCB_SUCCESS */
5163 if (status == IOCB_SUCCESS) {
5164 if (iocbqrsp->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
5165 /* Something in the FCP_RSP was invalid.
5166 * Check conditions */
5167 ret = lpfc_check_fcp_rsp(vport, lpfc_cmd);
5168 else
5169 ret = FAILED;
5170 } else if (status == IOCB_TIMEDOUT) {
5171 ret = TIMEOUT_ERROR;
5172 } else {
5173 ret = FAILED;
5174 }
5175 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
5176 } else
James Smartbbb9d182009-06-10 17:23:16 -04005177 ret = SUCCESS;
5178
5179 lpfc_sli_release_iocbq(phba, iocbqrsp);
5180
5181 if (ret != TIMEOUT_ERROR)
5182 lpfc_release_scsi_buf(phba, lpfc_cmd);
5183
5184 return ret;
5185}
5186
5187/**
5188 * lpfc_chk_tgt_mapped -
5189 * @vport: The virtual port to check on
5190 * @cmnd: Pointer to scsi_cmnd data structure.
5191 *
5192 * This routine delays until the scsi target (aka rport) for the
5193 * command exists (is present and logged in) or we declare it non-existent.
5194 *
5195 * Return code :
5196 * 0x2003 - Error
5197 * 0x2002 - Success
5198 **/
5199static int
5200lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
5201{
James Smart1ba981f2014-02-20 09:56:45 -05005202 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005203 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005204 unsigned long later;
5205
James Smart1ba981f2014-02-20 09:56:45 -05005206 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smart1c6f4ef52009-11-18 15:40:49 -05005207 if (!rdata) {
5208 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5209 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
5210 return FAILED;
5211 }
5212 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005213 /*
5214 * If target is not in a MAPPED state, delay until
5215 * target is rediscovered or devloss timeout expires.
5216 */
5217 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5218 while (time_after(later, jiffies)) {
5219 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
5220 return FAILED;
5221 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
5222 return SUCCESS;
5223 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
James Smart1ba981f2014-02-20 09:56:45 -05005224 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smartbbb9d182009-06-10 17:23:16 -04005225 if (!rdata)
5226 return FAILED;
5227 pnode = rdata->pnode;
5228 }
5229 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
5230 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
5231 return FAILED;
5232 return SUCCESS;
5233}
5234
5235/**
5236 * lpfc_reset_flush_io_context -
5237 * @vport: The virtual port (scsi_host) for the flush context
5238 * @tgt_id: If aborting by Target contect - specifies the target id
5239 * @lun_id: If aborting by Lun context - specifies the lun id
5240 * @context: specifies the context level to flush at.
5241 *
5242 * After a reset condition via TMF, we need to flush orphaned i/o
5243 * contexts from the adapter. This routine aborts any contexts
5244 * outstanding, then waits for their completions. The wait is
5245 * bounded by devloss_tmo though.
5246 *
5247 * Return code :
5248 * 0x2003 - Error
5249 * 0x2002 - Success
5250 **/
5251static int
5252lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5253 uint64_t lun_id, lpfc_ctx_cmd context)
5254{
5255 struct lpfc_hba *phba = vport->phba;
5256 unsigned long later;
5257 int cnt;
5258
5259 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5260 if (cnt)
5261 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
5262 tgt_id, lun_id, context);
5263 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5264 while (time_after(later, jiffies) && cnt) {
5265 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5266 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5267 }
5268 if (cnt) {
5269 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5270 "0724 I/O flush failure for context %s : cnt x%x\n",
5271 ((context == LPFC_CTX_LUN) ? "LUN" :
5272 ((context == LPFC_CTX_TGT) ? "TGT" :
5273 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5274 cnt);
5275 return FAILED;
5276 }
5277 return SUCCESS;
5278}
5279
James Smart9bad7672008-12-04 22:39:02 -05005280/**
James Smart3621a712009-04-06 18:47:14 -04005281 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05005282 * @cmnd: Pointer to scsi_cmnd data structure.
5283 *
James Smartbbb9d182009-06-10 17:23:16 -04005284 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05005285 * command.
5286 *
5287 * Return code :
5288 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04005289 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005290 **/
dea31012005-04-17 16:05:31 -05005291static int
James Smart7054a602007-04-25 09:52:34 -04005292lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005293{
James Smart2e0fef82007-06-17 19:56:36 -05005294 struct Scsi_Host *shost = cmnd->device->host;
5295 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smart1ba981f2014-02-20 09:56:45 -05005296 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005297 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005298 unsigned tgt_id = cmnd->device->id;
5299 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04005300 struct lpfc_scsi_event_header scsi_event;
James Smart53151bb2013-10-10 12:24:07 -04005301 int status;
dea31012005-04-17 16:05:31 -05005302
James Smart1ba981f2014-02-20 09:56:45 -05005303 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smart1c6f4ef52009-11-18 15:40:49 -05005304 if (!rdata) {
5305 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5306 "0798 Device Reset rport failure: rdata x%p\n", rdata);
5307 return FAILED;
5308 }
5309 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005310 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005311 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005312 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005313
5314 status = lpfc_chk_tgt_mapped(vport, cmnd);
5315 if (status == FAILED) {
5316 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5317 "0721 Device Reset rport failure: rdata x%p\n", rdata);
5318 return FAILED;
5319 }
5320
5321 scsi_event.event_type = FC_REG_SCSI_EVENT;
5322 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5323 scsi_event.lun = lun_id;
5324 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5325 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5326
5327 fc_host_post_vendor_event(shost, fc_get_event_number(),
5328 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5329
5330 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5331 FCP_LUN_RESET);
5332
5333 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5334 "0713 SCSI layer issued Device Reset (%d, %d) "
5335 "return x%x\n", tgt_id, lun_id, status);
5336
dea31012005-04-17 16:05:31 -05005337 /*
James Smartbbb9d182009-06-10 17:23:16 -04005338 * We have to clean up i/o as : they may be orphaned by the TMF;
5339 * or if the TMF failed, they may be in an indeterminate state.
5340 * So, continue on.
5341 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05005342 */
James Smart53151bb2013-10-10 12:24:07 -04005343 if (status == SUCCESS)
5344 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005345 LPFC_CTX_LUN);
James Smart53151bb2013-10-10 12:24:07 -04005346
5347 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005348}
5349
5350/**
5351 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5352 * @cmnd: Pointer to scsi_cmnd data structure.
5353 *
5354 * This routine does a target reset by sending a TARGET_RESET task management
5355 * command.
5356 *
5357 * Return code :
5358 * 0x2003 - Error
5359 * 0x2002 - Success
5360 **/
5361static int
5362lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5363{
5364 struct Scsi_Host *shost = cmnd->device->host;
5365 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smart1ba981f2014-02-20 09:56:45 -05005366 struct lpfc_rport_data *rdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005367 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005368 unsigned tgt_id = cmnd->device->id;
5369 unsigned int lun_id = cmnd->device->lun;
5370 struct lpfc_scsi_event_header scsi_event;
James Smart53151bb2013-10-10 12:24:07 -04005371 int status;
James Smartbbb9d182009-06-10 17:23:16 -04005372
James Smart1ba981f2014-02-20 09:56:45 -05005373 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
James Smart1c6f4ef52009-11-18 15:40:49 -05005374 if (!rdata) {
5375 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5376 "0799 Target Reset rport failure: rdata x%p\n", rdata);
5377 return FAILED;
5378 }
5379 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005380 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005381 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005382 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005383
5384 status = lpfc_chk_tgt_mapped(vport, cmnd);
5385 if (status == FAILED) {
5386 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5387 "0722 Target Reset rport failure: rdata x%p\n", rdata);
5388 return FAILED;
dea31012005-04-17 16:05:31 -05005389 }
James Smartea2151b2008-09-07 11:52:10 -04005390
5391 scsi_event.event_type = FC_REG_SCSI_EVENT;
5392 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5393 scsi_event.lun = 0;
5394 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5395 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5396
James Smartbbb9d182009-06-10 17:23:16 -04005397 fc_host_post_vendor_event(shost, fc_get_event_number(),
5398 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04005399
James Smartbbb9d182009-06-10 17:23:16 -04005400 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5401 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05005402
James Smarte8b62012007-08-02 11:10:09 -04005403 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04005404 "0723 SCSI layer issued Target Reset (%d, %d) "
5405 "return x%x\n", tgt_id, lun_id, status);
5406
5407 /*
5408 * We have to clean up i/o as : they may be orphaned by the TMF;
5409 * or if the TMF failed, they may be in an indeterminate state.
5410 * So, continue on.
5411 * We will report success if all the i/o aborts successfully.
5412 */
James Smart53151bb2013-10-10 12:24:07 -04005413 if (status == SUCCESS)
5414 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smart3a707302012-06-12 13:54:42 -04005415 LPFC_CTX_TGT);
James Smart53151bb2013-10-10 12:24:07 -04005416 return status;
dea31012005-04-17 16:05:31 -05005417}
5418
James Smart9bad7672008-12-04 22:39:02 -05005419/**
James Smart3621a712009-04-06 18:47:14 -04005420 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05005421 * @cmnd: Pointer to scsi_cmnd data structure.
5422 *
James Smartbbb9d182009-06-10 17:23:16 -04005423 * This routine does target reset to all targets on @cmnd->device->host.
5424 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05005425 *
James Smartbbb9d182009-06-10 17:23:16 -04005426 * Return code :
5427 * 0x2003 - Error
5428 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005429 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04005430static int
James Smart7054a602007-04-25 09:52:34 -04005431lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005432{
James Smart2e0fef82007-06-17 19:56:36 -05005433 struct Scsi_Host *shost = cmnd->device->host;
5434 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005435 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04005436 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04005437 int match;
5438 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04005439
5440 scsi_event.event_type = FC_REG_SCSI_EVENT;
5441 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5442 scsi_event.lun = 0;
5443 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5444 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5445
James Smartbbb9d182009-06-10 17:23:16 -04005446 fc_host_post_vendor_event(shost, fc_get_event_number(),
5447 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05005448
James Smartbf086112011-08-21 21:48:13 -04005449 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005450 if (status != 0 && status != SUCCESS)
James Smartbf086112011-08-21 21:48:13 -04005451 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005452
dea31012005-04-17 16:05:31 -05005453 /*
5454 * Since the driver manages a single bus device, reset all
5455 * targets known to the driver. Should any target reset
5456 * fail, this routine returns failure to the midlayer.
5457 */
James Smarte17da182006-07-06 15:49:25 -04005458 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04005459 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05005460 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05005461 spin_lock_irq(shost->host_lock);
5462 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005463 if (!NLP_CHK_NODE_ACT(ndlp))
5464 continue;
James Smarta6571c62012-10-31 14:44:42 -04005465 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5466 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5467 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005468 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04005469 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04005470 ndlp->rport) {
dea31012005-04-17 16:05:31 -05005471 match = 1;
5472 break;
5473 }
5474 }
James Smart2e0fef82007-06-17 19:56:36 -05005475 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005476 if (!match)
5477 continue;
James Smartbbb9d182009-06-10 17:23:16 -04005478
5479 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
5480 i, 0, FCP_TARGET_RESET);
5481
5482 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04005483 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5484 "0700 Bus Reset on target %d failed\n",
5485 i);
James Smart915caaa2008-06-14 22:52:38 -04005486 ret = FAILED;
dea31012005-04-17 16:05:31 -05005487 }
5488 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005489 /*
James Smartbbb9d182009-06-10 17:23:16 -04005490 * We have to clean up i/o as : they may be orphaned by the TMFs
5491 * above; or if any of the TMFs failed, they may be in an
5492 * indeterminate state.
5493 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005494 */
James Smartbbb9d182009-06-10 17:23:16 -04005495
5496 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5497 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005498 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04005499
James Smarte8b62012007-08-02 11:10:09 -04005500 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5501 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05005502 return ret;
5503}
5504
James Smart9bad7672008-12-04 22:39:02 -05005505/**
James Smart27b01b82012-05-09 21:19:44 -04005506 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
5507 * @cmnd: Pointer to scsi_cmnd data structure.
5508 *
5509 * This routine does host reset to the adaptor port. It brings the HBA
5510 * offline, performs a board restart, and then brings the board back online.
5511 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
5512 * reject all outstanding SCSI commands to the host and error returned
5513 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5514 * of error handling, it will only return error if resetting of the adapter
5515 * is not successful; in all other cases, will return success.
5516 *
5517 * Return code :
5518 * 0x2003 - Error
5519 * 0x2002 - Success
5520 **/
5521static int
5522lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5523{
5524 struct Scsi_Host *shost = cmnd->device->host;
5525 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5526 struct lpfc_hba *phba = vport->phba;
5527 int rc, ret = SUCCESS;
5528
James Smarta88dbb62013-04-17 20:18:39 -04005529 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5530 "3172 SCSI layer issued Host Reset Data:\n");
5531
James Smart618a5232012-06-12 13:54:36 -04005532 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart27b01b82012-05-09 21:19:44 -04005533 lpfc_offline(phba);
5534 rc = lpfc_sli_brdrestart(phba);
5535 if (rc)
5536 ret = FAILED;
James Smarta88dbb62013-04-17 20:18:39 -04005537 rc = lpfc_online(phba);
5538 if (rc)
5539 ret = FAILED;
James Smart27b01b82012-05-09 21:19:44 -04005540 lpfc_unblock_mgmt_io(phba);
5541
James Smarta88dbb62013-04-17 20:18:39 -04005542 if (ret == FAILED) {
5543 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5544 "3323 Failed host reset, bring it offline\n");
5545 lpfc_sli4_offline_eratt(phba);
5546 }
James Smart27b01b82012-05-09 21:19:44 -04005547 return ret;
5548}
5549
5550/**
James Smart3621a712009-04-06 18:47:14 -04005551 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05005552 * @sdev: Pointer to scsi_device.
5553 *
5554 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5555 * globally available list of scsi buffers. This routine also makes sure scsi
5556 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5557 * of scsi buffer exists for the lifetime of the driver.
5558 *
5559 * Return codes:
5560 * non-0 - Error
5561 * 0 - Success
5562 **/
dea31012005-04-17 16:05:31 -05005563static int
dea31012005-04-17 16:05:31 -05005564lpfc_slave_alloc(struct scsi_device *sdev)
5565{
James Smart2e0fef82007-06-17 19:56:36 -05005566 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5567 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005568 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04005569 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05005570 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04005571 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04005572 uint32_t sdev_cnt;
James Smart1ba981f2014-02-20 09:56:45 -05005573 struct lpfc_device_data *device_data;
5574 unsigned long flags;
5575 struct lpfc_name target_wwpn;
dea31012005-04-17 16:05:31 -05005576
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005577 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05005578 return -ENXIO;
5579
James Smart1ba981f2014-02-20 09:56:45 -05005580 if (phba->cfg_EnableXLane) {
5581
5582 /*
5583 * Check to see if the device data structure for the lun
5584 * exists. If not, create one.
5585 */
5586
5587 u64_to_wwn(rport->port_name, target_wwpn.u.wwn);
5588 spin_lock_irqsave(&phba->devicelock, flags);
5589 device_data = __lpfc_get_device_data(phba,
5590 &phba->luns,
5591 &vport->fc_portname,
5592 &target_wwpn,
5593 sdev->lun);
5594 if (!device_data) {
5595 spin_unlock_irqrestore(&phba->devicelock, flags);
5596 device_data = lpfc_create_device_data(phba,
5597 &vport->fc_portname,
5598 &target_wwpn,
5599 sdev->lun, true);
5600 if (!device_data)
5601 return -ENOMEM;
5602 spin_lock_irqsave(&phba->devicelock, flags);
5603 list_add_tail(&device_data->listentry, &phba->luns);
5604 }
5605 device_data->rport_data = rport->dd_data;
5606 device_data->available = true;
5607 spin_unlock_irqrestore(&phba->devicelock, flags);
5608 sdev->hostdata = device_data;
5609 } else {
5610 sdev->hostdata = rport->dd_data;
5611 }
James Smartd7c47992010-06-08 18:31:54 -04005612 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005613
5614 /*
5615 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5616 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04005617 * HBA limit conveyed to the midlayer via the host structure. The
5618 * formula accounts for the lun_queue_depth + error handlers + 1
5619 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05005620 */
5621 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04005622 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05005623
James Smartd7c47992010-06-08 18:31:54 -04005624 /* If allocated buffers are enough do nothing */
5625 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5626 return 0;
5627
James Smart92d7f7b2007-06-17 19:56:38 -05005628 /* Allow some exchanges to be available always to complete discovery */
5629 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005630 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5631 "0704 At limitation of %d preallocated "
5632 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05005633 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05005634 /* Allow some exchanges to be available always to complete discovery */
5635 } else if (total + num_to_alloc >
5636 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005637 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5638 "0705 Allocation request of %d "
5639 "command buffers will exceed max of %d. "
5640 "Reducing allocation request to %d.\n",
5641 num_to_alloc, phba->cfg_hba_queue_depth,
5642 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05005643 num_to_alloc = phba->cfg_hba_queue_depth - total;
5644 }
James Smart3772a992009-05-22 14:50:54 -04005645 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
5646 if (num_to_alloc != num_allocated) {
James Smart96f70772013-04-17 20:16:15 -04005647 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5648 "0708 Allocation request of %d "
5649 "command buffers did not succeed. "
5650 "Allocated %d buffers.\n",
5651 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05005652 }
James Smart1c6f4ef52009-11-18 15:40:49 -05005653 if (num_allocated > 0)
5654 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05005655 return 0;
5656}
5657
James Smart9bad7672008-12-04 22:39:02 -05005658/**
James Smart3621a712009-04-06 18:47:14 -04005659 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05005660 * @sdev: Pointer to scsi_device.
5661 *
5662 * This routine configures following items
5663 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05005664 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5665 *
5666 * Return codes:
5667 * 0 - Success
5668 **/
dea31012005-04-17 16:05:31 -05005669static int
5670lpfc_slave_configure(struct scsi_device *sdev)
5671{
James Smart2e0fef82007-06-17 19:56:36 -05005672 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5673 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005674
5675 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04005676 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005677 else
James Smart3de2a652007-08-02 11:09:59 -04005678 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005679
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005680 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005681 lpfc_sli_handle_fast_ring_event(phba,
5682 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005683 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5684 lpfc_poll_rearm_timer(phba);
5685 }
5686
dea31012005-04-17 16:05:31 -05005687 return 0;
5688}
5689
James Smart9bad7672008-12-04 22:39:02 -05005690/**
James Smart3621a712009-04-06 18:47:14 -04005691 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05005692 * @sdev: Pointer to scsi_device.
5693 *
5694 * This routine sets @sdev hostatdata filed to null.
5695 **/
dea31012005-04-17 16:05:31 -05005696static void
5697lpfc_slave_destroy(struct scsi_device *sdev)
5698{
James Smartd7c47992010-06-08 18:31:54 -04005699 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5700 struct lpfc_hba *phba = vport->phba;
James Smart1ba981f2014-02-20 09:56:45 -05005701 unsigned long flags;
5702 struct lpfc_device_data *device_data = sdev->hostdata;
5703
James Smartd7c47992010-06-08 18:31:54 -04005704 atomic_dec(&phba->sdev_cnt);
James Smart1ba981f2014-02-20 09:56:45 -05005705 if ((phba->cfg_EnableXLane) && (device_data)) {
5706 spin_lock_irqsave(&phba->devicelock, flags);
5707 device_data->available = false;
5708 if (!device_data->oas_enabled)
5709 lpfc_delete_device_data(phba, device_data);
5710 spin_unlock_irqrestore(&phba->devicelock, flags);
5711 }
dea31012005-04-17 16:05:31 -05005712 sdev->hostdata = NULL;
5713 return;
5714}
5715
James Smart1ba981f2014-02-20 09:56:45 -05005716/**
5717 * lpfc_create_device_data - creates and initializes device data structure for OAS
5718 * @pha: Pointer to host bus adapter structure.
5719 * @vport_wwpn: Pointer to vport's wwpn information
5720 * @target_wwpn: Pointer to target's wwpn information
5721 * @lun: Lun on target
5722 * @atomic_create: Flag to indicate if memory should be allocated using the
5723 * GFP_ATOMIC flag or not.
5724 *
5725 * This routine creates a device data structure which will contain identifying
5726 * information for the device (host wwpn, target wwpn, lun), state of OAS,
5727 * whether or not the corresponding lun is available by the system,
5728 * and pointer to the rport data.
5729 *
5730 * Return codes:
5731 * NULL - Error
5732 * Pointer to lpfc_device_data - Success
5733 **/
5734struct lpfc_device_data*
5735lpfc_create_device_data(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5736 struct lpfc_name *target_wwpn, uint64_t lun,
5737 bool atomic_create)
5738{
5739
5740 struct lpfc_device_data *lun_info;
5741 int memory_flags;
5742
5743 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5744 !(phba->cfg_EnableXLane))
5745 return NULL;
5746
5747 /* Attempt to create the device data to contain lun info */
5748
5749 if (atomic_create)
5750 memory_flags = GFP_ATOMIC;
5751 else
5752 memory_flags = GFP_KERNEL;
5753 lun_info = mempool_alloc(phba->device_data_mem_pool, memory_flags);
5754 if (!lun_info)
5755 return NULL;
5756 INIT_LIST_HEAD(&lun_info->listentry);
5757 lun_info->rport_data = NULL;
5758 memcpy(&lun_info->device_id.vport_wwpn, vport_wwpn,
5759 sizeof(struct lpfc_name));
5760 memcpy(&lun_info->device_id.target_wwpn, target_wwpn,
5761 sizeof(struct lpfc_name));
5762 lun_info->device_id.lun = lun;
5763 lun_info->oas_enabled = false;
5764 lun_info->available = false;
5765 return lun_info;
5766}
5767
5768/**
5769 * lpfc_delete_device_data - frees a device data structure for OAS
5770 * @pha: Pointer to host bus adapter structure.
5771 * @lun_info: Pointer to device data structure to free.
5772 *
5773 * This routine frees the previously allocated device data structure passed.
5774 *
5775 **/
5776void
5777lpfc_delete_device_data(struct lpfc_hba *phba,
5778 struct lpfc_device_data *lun_info)
5779{
5780
5781 if (unlikely(!phba) || !lun_info ||
5782 !(phba->cfg_EnableXLane))
5783 return;
5784
5785 if (!list_empty(&lun_info->listentry))
5786 list_del(&lun_info->listentry);
5787 mempool_free(lun_info, phba->device_data_mem_pool);
5788 return;
5789}
5790
5791/**
5792 * __lpfc_get_device_data - returns the device data for the specified lun
5793 * @pha: Pointer to host bus adapter structure.
5794 * @list: Point to list to search.
5795 * @vport_wwpn: Pointer to vport's wwpn information
5796 * @target_wwpn: Pointer to target's wwpn information
5797 * @lun: Lun on target
5798 *
5799 * This routine searches the list passed for the specified lun's device data.
5800 * This function does not hold locks, it is the responsibility of the caller
5801 * to ensure the proper lock is held before calling the function.
5802 *
5803 * Return codes:
5804 * NULL - Error
5805 * Pointer to lpfc_device_data - Success
5806 **/
5807struct lpfc_device_data*
5808__lpfc_get_device_data(struct lpfc_hba *phba, struct list_head *list,
5809 struct lpfc_name *vport_wwpn,
5810 struct lpfc_name *target_wwpn, uint64_t lun)
5811{
5812
5813 struct lpfc_device_data *lun_info;
5814
5815 if (unlikely(!phba) || !list || !vport_wwpn || !target_wwpn ||
5816 !phba->cfg_EnableXLane)
5817 return NULL;
5818
5819 /* Check to see if the lun is already enabled for OAS. */
5820
5821 list_for_each_entry(lun_info, list, listentry) {
5822 if ((memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5823 sizeof(struct lpfc_name)) == 0) &&
5824 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5825 sizeof(struct lpfc_name)) == 0) &&
5826 (lun_info->device_id.lun == lun))
5827 return lun_info;
5828 }
5829
5830 return NULL;
5831}
5832
5833/**
5834 * lpfc_find_next_oas_lun - searches for the next oas lun
5835 * @pha: Pointer to host bus adapter structure.
5836 * @vport_wwpn: Pointer to vport's wwpn information
5837 * @target_wwpn: Pointer to target's wwpn information
5838 * @starting_lun: Pointer to the lun to start searching for
5839 * @found_vport_wwpn: Pointer to the found lun's vport wwpn information
5840 * @found_target_wwpn: Pointer to the found lun's target wwpn information
5841 * @found_lun: Pointer to the found lun.
5842 * @found_lun_status: Pointer to status of the found lun.
5843 *
5844 * This routine searches the luns list for the specified lun
5845 * or the first lun for the vport/target. If the vport wwpn contains
5846 * a zero value then a specific vport is not specified. In this case
5847 * any vport which contains the lun will be considered a match. If the
5848 * target wwpn contains a zero value then a specific target is not specified.
5849 * In this case any target which contains the lun will be considered a
5850 * match. If the lun is found, the lun, vport wwpn, target wwpn and lun status
5851 * are returned. The function will also return the next lun if available.
5852 * If the next lun is not found, starting_lun parameter will be set to
5853 * NO_MORE_OAS_LUN.
5854 *
5855 * Return codes:
5856 * non-0 - Error
5857 * 0 - Success
5858 **/
5859bool
5860lpfc_find_next_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5861 struct lpfc_name *target_wwpn, uint64_t *starting_lun,
5862 struct lpfc_name *found_vport_wwpn,
5863 struct lpfc_name *found_target_wwpn,
5864 uint64_t *found_lun,
5865 uint32_t *found_lun_status)
5866{
5867
5868 unsigned long flags;
5869 struct lpfc_device_data *lun_info;
5870 struct lpfc_device_id *device_id;
5871 uint64_t lun;
5872 bool found = false;
5873
5874 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5875 !starting_lun || !found_vport_wwpn ||
5876 !found_target_wwpn || !found_lun || !found_lun_status ||
5877 (*starting_lun == NO_MORE_OAS_LUN) ||
5878 !phba->cfg_EnableXLane)
5879 return false;
5880
5881 lun = *starting_lun;
5882 *found_lun = NO_MORE_OAS_LUN;
5883 *starting_lun = NO_MORE_OAS_LUN;
5884
5885 /* Search for lun or the lun closet in value */
5886
5887 spin_lock_irqsave(&phba->devicelock, flags);
5888 list_for_each_entry(lun_info, &phba->luns, listentry) {
5889 if (((wwn_to_u64(vport_wwpn->u.wwn) == 0) ||
5890 (memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5891 sizeof(struct lpfc_name)) == 0)) &&
5892 ((wwn_to_u64(target_wwpn->u.wwn) == 0) ||
5893 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5894 sizeof(struct lpfc_name)) == 0)) &&
5895 (lun_info->oas_enabled)) {
5896 device_id = &lun_info->device_id;
5897 if ((!found) &&
5898 ((lun == FIND_FIRST_OAS_LUN) ||
5899 (device_id->lun == lun))) {
5900 *found_lun = device_id->lun;
5901 memcpy(found_vport_wwpn,
5902 &device_id->vport_wwpn,
5903 sizeof(struct lpfc_name));
5904 memcpy(found_target_wwpn,
5905 &device_id->target_wwpn,
5906 sizeof(struct lpfc_name));
5907 if (lun_info->available)
5908 *found_lun_status =
5909 OAS_LUN_STATUS_EXISTS;
5910 else
5911 *found_lun_status = 0;
5912 if (phba->cfg_oas_flags & OAS_FIND_ANY_VPORT)
5913 memset(vport_wwpn, 0x0,
5914 sizeof(struct lpfc_name));
5915 if (phba->cfg_oas_flags & OAS_FIND_ANY_TARGET)
5916 memset(target_wwpn, 0x0,
5917 sizeof(struct lpfc_name));
5918 found = true;
5919 } else if (found) {
5920 *starting_lun = device_id->lun;
5921 memcpy(vport_wwpn, &device_id->vport_wwpn,
5922 sizeof(struct lpfc_name));
5923 memcpy(target_wwpn, &device_id->target_wwpn,
5924 sizeof(struct lpfc_name));
5925 break;
5926 }
5927 }
5928 }
5929 spin_unlock_irqrestore(&phba->devicelock, flags);
5930 return found;
5931}
5932
5933/**
5934 * lpfc_enable_oas_lun - enables a lun for OAS operations
5935 * @pha: Pointer to host bus adapter structure.
5936 * @vport_wwpn: Pointer to vport's wwpn information
5937 * @target_wwpn: Pointer to target's wwpn information
5938 * @lun: Lun
5939 *
5940 * This routine enables a lun for oas operations. The routines does so by
5941 * doing the following :
5942 *
5943 * 1) Checks to see if the device data for the lun has been created.
5944 * 2) If found, sets the OAS enabled flag if not set and returns.
5945 * 3) Otherwise, creates a device data structure.
5946 * 4) If successfully created, indicates the device data is for an OAS lun,
5947 * indicates the lun is not available and add to the list of luns.
5948 *
5949 * Return codes:
5950 * false - Error
5951 * true - Success
5952 **/
5953bool
5954lpfc_enable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5955 struct lpfc_name *target_wwpn, uint64_t lun)
5956{
5957
5958 struct lpfc_device_data *lun_info;
5959 unsigned long flags;
5960
5961 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5962 !phba->cfg_EnableXLane)
5963 return false;
5964
5965 spin_lock_irqsave(&phba->devicelock, flags);
5966
5967 /* Check to see if the device data for the lun has been created */
5968 lun_info = __lpfc_get_device_data(phba, &phba->luns, vport_wwpn,
5969 target_wwpn, lun);
5970 if (lun_info) {
5971 if (!lun_info->oas_enabled)
5972 lun_info->oas_enabled = true;
5973 spin_unlock_irqrestore(&phba->devicelock, flags);
5974 return true;
5975 }
5976
5977 /* Create an lun info structure and add to list of luns */
5978 lun_info = lpfc_create_device_data(phba, vport_wwpn, target_wwpn, lun,
5979 false);
5980 if (lun_info) {
5981 lun_info->oas_enabled = true;
5982 lun_info->available = false;
5983 list_add_tail(&lun_info->listentry, &phba->luns);
5984 spin_unlock_irqrestore(&phba->devicelock, flags);
5985 return true;
5986 }
5987 spin_unlock_irqrestore(&phba->devicelock, flags);
5988 return false;
5989}
5990
5991/**
5992 * lpfc_disable_oas_lun - disables a lun for OAS operations
5993 * @pha: Pointer to host bus adapter structure.
5994 * @vport_wwpn: Pointer to vport's wwpn information
5995 * @target_wwpn: Pointer to target's wwpn information
5996 * @lun: Lun
5997 *
5998 * This routine disables a lun for oas operations. The routines does so by
5999 * doing the following :
6000 *
6001 * 1) Checks to see if the device data for the lun is created.
6002 * 2) If present, clears the flag indicating this lun is for OAS.
6003 * 3) If the lun is not available by the system, the device data is
6004 * freed.
6005 *
6006 * Return codes:
6007 * false - Error
6008 * true - Success
6009 **/
6010bool
6011lpfc_disable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
6012 struct lpfc_name *target_wwpn, uint64_t lun)
6013{
6014
6015 struct lpfc_device_data *lun_info;
6016 unsigned long flags;
6017
6018 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
6019 !phba->cfg_EnableXLane)
6020 return false;
6021
6022 spin_lock_irqsave(&phba->devicelock, flags);
6023
6024 /* Check to see if the lun is available. */
6025 lun_info = __lpfc_get_device_data(phba,
6026 &phba->luns, vport_wwpn,
6027 target_wwpn, lun);
6028 if (lun_info) {
6029 lun_info->oas_enabled = false;
6030 if (!lun_info->available)
6031 lpfc_delete_device_data(phba, lun_info);
6032 spin_unlock_irqrestore(&phba->devicelock, flags);
6033 return true;
6034 }
6035
6036 spin_unlock_irqrestore(&phba->devicelock, flags);
6037 return false;
6038}
James Smart92d7f7b2007-06-17 19:56:38 -05006039
dea31012005-04-17 16:05:31 -05006040struct scsi_host_template lpfc_template = {
6041 .module = THIS_MODULE,
6042 .name = LPFC_DRIVER_NAME,
6043 .info = lpfc_info,
6044 .queuecommand = lpfc_queuecommand,
6045 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04006046 .eh_device_reset_handler = lpfc_device_reset_handler,
6047 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04006048 .eh_bus_reset_handler = lpfc_bus_reset_handler,
James Smart27b01b82012-05-09 21:19:44 -04006049 .eh_host_reset_handler = lpfc_host_reset_handler,
dea31012005-04-17 16:05:31 -05006050 .slave_alloc = lpfc_slave_alloc,
6051 .slave_configure = lpfc_slave_configure,
6052 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04006053 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05006054 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05006055 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05006056 .cmd_per_lun = LPFC_CMD_PER_LUN,
6057 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05006058 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04006059 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04006060 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05006061 .change_queue_depth = lpfc_change_queue_depth,
James Smartfe8f7f92013-01-03 15:43:03 -05006062 .change_queue_type = lpfc_change_queue_type,
dea31012005-04-17 16:05:31 -05006063};
James Smart3de2a652007-08-02 11:09:59 -04006064
6065struct scsi_host_template lpfc_vport_template = {
6066 .module = THIS_MODULE,
6067 .name = LPFC_DRIVER_NAME,
6068 .info = lpfc_info,
6069 .queuecommand = lpfc_queuecommand,
6070 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04006071 .eh_device_reset_handler = lpfc_device_reset_handler,
6072 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04006073 .eh_bus_reset_handler = lpfc_bus_reset_handler,
6074 .slave_alloc = lpfc_slave_alloc,
6075 .slave_configure = lpfc_slave_configure,
6076 .slave_destroy = lpfc_slave_destroy,
6077 .scan_finished = lpfc_scan_finished,
6078 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05006079 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04006080 .cmd_per_lun = LPFC_CMD_PER_LUN,
6081 .use_clustering = ENABLE_CLUSTERING,
6082 .shost_attrs = lpfc_vport_attrs,
6083 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05006084 .change_queue_depth = lpfc_change_queue_depth,
James Smartfe8f7f92013-01-03 15:43:03 -05006085 .change_queue_type = lpfc_change_queue_type,
James Smart3de2a652007-08-02 11:09:59 -04006086};