[SCSI] lpfc 8.2.8 v2 : Revert target busy in favor of transport disrupted
[linux-2.6-block.git] / drivers / scsi / lpfc / lpfc_scsi.c
CommitLineData
dea3101e 1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
e47c9093 4 * Copyright (C) 2004-2008 Emulex. All rights reserved. *
c44ce173 5 * EMULEX and SLI are trademarks of Emulex. *
dea3101e 6 * www.emulex.com *
c44ce173 7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea3101e 8 * *
9 * This program is free software; you can redistribute it and/or *
c44ce173
JSEC
10 * 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. *
dea3101e 20 *******************************************************************/
21
dea3101e 22#include <linux/pci.h>
23#include <linux/interrupt.h>
a90f5684 24#include <linux/delay.h>
dea3101e 25
26#include <scsi/scsi.h>
27#include <scsi/scsi_device.h>
28#include <scsi/scsi_host.h>
29#include <scsi/scsi_tcq.h>
30#include <scsi/scsi_transport_fc.h>
31
32#include "lpfc_version.h"
33#include "lpfc_hw.h"
34#include "lpfc_sli.h"
35#include "lpfc_disc.h"
36#include "lpfc_scsi.h"
37#include "lpfc.h"
38#include "lpfc_logmsg.h"
39#include "lpfc_crtn.h"
92d7f7b0 40#include "lpfc_vport.h"
dea3101e 41
42#define LPFC_RESET_WAIT 2
43#define LPFC_ABORT_WAIT 2
44
92d7f7b0
JS
45/*
46 * This function is called with no lock held when there is a resource
47 * error in driver or in firmware.
48 */
49void
50lpfc_adjust_queue_depth(struct lpfc_hba *phba)
51{
52 unsigned long flags;
5e9d9b82 53 uint32_t evt_posted;
92d7f7b0
JS
54
55 spin_lock_irqsave(&phba->hbalock, flags);
56 atomic_inc(&phba->num_rsrc_err);
57 phba->last_rsrc_error_time = jiffies;
58
59 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
60 spin_unlock_irqrestore(&phba->hbalock, flags);
61 return;
62 }
63
64 phba->last_ramp_down_time = jiffies;
65
66 spin_unlock_irqrestore(&phba->hbalock, flags);
67
68 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
5e9d9b82
JS
69 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
70 if (!evt_posted)
92d7f7b0 71 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
92d7f7b0
JS
72 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
73
5e9d9b82
JS
74 if (!evt_posted)
75 lpfc_worker_wake_up(phba);
92d7f7b0
JS
76 return;
77}
78
79/*
80 * This function is called with no lock held when there is a successful
81 * SCSI command completion.
82 */
83static inline void
3de2a653 84lpfc_rampup_queue_depth(struct lpfc_vport *vport,
92d7f7b0
JS
85 struct scsi_device *sdev)
86{
87 unsigned long flags;
3de2a653 88 struct lpfc_hba *phba = vport->phba;
5e9d9b82 89 uint32_t evt_posted;
92d7f7b0
JS
90 atomic_inc(&phba->num_cmd_success);
91
3de2a653 92 if (vport->cfg_lun_queue_depth <= sdev->queue_depth)
92d7f7b0 93 return;
92d7f7b0
JS
94 spin_lock_irqsave(&phba->hbalock, flags);
95 if (((phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) > jiffies) ||
96 ((phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL ) > jiffies)) {
97 spin_unlock_irqrestore(&phba->hbalock, flags);
98 return;
99 }
92d7f7b0
JS
100 phba->last_ramp_up_time = jiffies;
101 spin_unlock_irqrestore(&phba->hbalock, flags);
102
103 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
5e9d9b82
JS
104 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
105 if (!evt_posted)
92d7f7b0 106 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
92d7f7b0
JS
107 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
108
5e9d9b82
JS
109 if (!evt_posted)
110 lpfc_worker_wake_up(phba);
111 return;
92d7f7b0
JS
112}
113
114void
115lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
116{
549e55cd
JS
117 struct lpfc_vport **vports;
118 struct Scsi_Host *shost;
92d7f7b0
JS
119 struct scsi_device *sdev;
120 unsigned long new_queue_depth;
121 unsigned long num_rsrc_err, num_cmd_success;
549e55cd 122 int i;
92d7f7b0
JS
123
124 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
125 num_cmd_success = atomic_read(&phba->num_cmd_success);
126
549e55cd
JS
127 vports = lpfc_create_vport_work_array(phba);
128 if (vports != NULL)
09372820 129 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
549e55cd
JS
130 shost = lpfc_shost_from_vport(vports[i]);
131 shost_for_each_device(sdev, shost) {
92d7f7b0 132 new_queue_depth =
549e55cd
JS
133 sdev->queue_depth * num_rsrc_err /
134 (num_rsrc_err + num_cmd_success);
135 if (!new_queue_depth)
136 new_queue_depth = sdev->queue_depth - 1;
137 else
138 new_queue_depth = sdev->queue_depth -
139 new_queue_depth;
140 if (sdev->ordered_tags)
141 scsi_adjust_queue_depth(sdev,
142 MSG_ORDERED_TAG,
143 new_queue_depth);
144 else
145 scsi_adjust_queue_depth(sdev,
146 MSG_SIMPLE_TAG,
147 new_queue_depth);
148 }
92d7f7b0 149 }
09372820 150 lpfc_destroy_vport_work_array(phba, vports);
92d7f7b0
JS
151 atomic_set(&phba->num_rsrc_err, 0);
152 atomic_set(&phba->num_cmd_success, 0);
153}
154
155void
156lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
157{
549e55cd
JS
158 struct lpfc_vport **vports;
159 struct Scsi_Host *shost;
92d7f7b0 160 struct scsi_device *sdev;
549e55cd
JS
161 int i;
162
163 vports = lpfc_create_vport_work_array(phba);
164 if (vports != NULL)
09372820 165 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
549e55cd
JS
166 shost = lpfc_shost_from_vport(vports[i]);
167 shost_for_each_device(sdev, shost) {
97eab634
JS
168 if (vports[i]->cfg_lun_queue_depth <=
169 sdev->queue_depth)
170 continue;
549e55cd
JS
171 if (sdev->ordered_tags)
172 scsi_adjust_queue_depth(sdev,
173 MSG_ORDERED_TAG,
174 sdev->queue_depth+1);
175 else
176 scsi_adjust_queue_depth(sdev,
177 MSG_SIMPLE_TAG,
178 sdev->queue_depth+1);
179 }
92d7f7b0 180 }
09372820 181 lpfc_destroy_vport_work_array(phba, vports);
92d7f7b0
JS
182 atomic_set(&phba->num_rsrc_err, 0);
183 atomic_set(&phba->num_cmd_success, 0);
184}
185
a8e497d5
JS
186/**
187 * lpfc_scsi_dev_block: set all scsi hosts to block state.
188 * @phba: Pointer to HBA context object.
189 *
190 * This function walks vport list and set each SCSI host to block state
191 * by invoking fc_remote_port_delete() routine. This function is invoked
192 * with EEH when device's PCI slot has been permanently disabled.
193 **/
194void
195lpfc_scsi_dev_block(struct lpfc_hba *phba)
196{
197 struct lpfc_vport **vports;
198 struct Scsi_Host *shost;
199 struct scsi_device *sdev;
200 struct fc_rport *rport;
201 int i;
202
203 vports = lpfc_create_vport_work_array(phba);
204 if (vports != NULL)
205 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
206 shost = lpfc_shost_from_vport(vports[i]);
207 shost_for_each_device(sdev, shost) {
208 rport = starget_to_rport(scsi_target(sdev));
209 fc_remote_port_delete(rport);
210 }
211 }
212 lpfc_destroy_vport_work_array(phba, vports);
213}
214
dea3101e 215/*
216 * This routine allocates a scsi buffer, which contains all the necessary
217 * information needed to initiate a SCSI I/O. The non-DMAable buffer region
218 * contains information to build the IOCB. The DMAable region contains
219 * memory for the FCP CMND, FCP RSP, and the inital BPL. In addition to
220 * allocating memeory, the FCP CMND and FCP RSP BDEs are setup in the BPL
221 * and the BPL BDE is setup in the IOCB.
222 */
223static struct lpfc_scsi_buf *
2e0fef85 224lpfc_new_scsi_buf(struct lpfc_vport *vport)
dea3101e 225{
2e0fef85 226 struct lpfc_hba *phba = vport->phba;
dea3101e 227 struct lpfc_scsi_buf *psb;
228 struct ulp_bde64 *bpl;
229 IOCB_t *iocb;
34b02dcd
JS
230 dma_addr_t pdma_phys_fcp_cmd;
231 dma_addr_t pdma_phys_fcp_rsp;
232 dma_addr_t pdma_phys_bpl;
604a3e30 233 uint16_t iotag;
dea3101e 234
bbfbbbc1 235 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
dea3101e 236 if (!psb)
237 return NULL;
dea3101e 238
239 /*
240 * Get memory from the pci pool to map the virt space to pci bus space
241 * for an I/O. The DMA buffer includes space for the struct fcp_cmnd,
242 * struct fcp_rsp and the number of bde's necessary to support the
243 * sg_tablesize.
244 */
245 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool, GFP_KERNEL,
246 &psb->dma_handle);
247 if (!psb->data) {
248 kfree(psb);
249 return NULL;
250 }
251
252 /* Initialize virtual ptrs to dma_buf region. */
253 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
254
604a3e30
JB
255 /* Allocate iotag for psb->cur_iocbq. */
256 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
257 if (iotag == 0) {
258 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
259 psb->data, psb->dma_handle);
260 kfree (psb);
261 return NULL;
262 }
0bd4ca25 263 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
604a3e30 264
dea3101e 265 psb->fcp_cmnd = psb->data;
266 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
267 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
268 sizeof(struct fcp_rsp);
269
270 /* Initialize local short-hand pointers. */
271 bpl = psb->fcp_bpl;
34b02dcd
JS
272 pdma_phys_fcp_cmd = psb->dma_handle;
273 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
274 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
275 sizeof(struct fcp_rsp);
dea3101e 276
277 /*
278 * The first two bdes are the FCP_CMD and FCP_RSP. The balance are sg
279 * list bdes. Initialize the first two and leave the rest for
280 * queuecommand.
281 */
34b02dcd
JS
282 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
283 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
284 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
285 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
286 bpl[0].tus.w = le32_to_cpu(bpl->tus.w);
dea3101e 287
288 /* Setup the physical region for the FCP RSP */
34b02dcd
JS
289 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
290 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
291 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
292 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
293 bpl[1].tus.w = le32_to_cpu(bpl->tus.w);
dea3101e 294
295 /*
296 * Since the IOCB for the FCP I/O is built into this lpfc_scsi_buf,
297 * initialize it with all known data now.
298 */
dea3101e 299 iocb = &psb->cur_iocbq.iocb;
300 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
34b02dcd
JS
301 if (phba->sli_rev == 3) {
302 /* fill in immediate fcp command BDE */
303 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
304 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
305 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
306 unsli3.fcp_ext.icd);
307 iocb->un.fcpi64.bdl.addrHigh = 0;
308 iocb->ulpBdeCount = 0;
309 iocb->ulpLe = 0;
310 /* fill in responce BDE */
311 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
312 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
313 sizeof(struct fcp_rsp);
314 iocb->unsli3.fcp_ext.rbde.addrLow =
315 putPaddrLow(pdma_phys_fcp_rsp);
316 iocb->unsli3.fcp_ext.rbde.addrHigh =
317 putPaddrHigh(pdma_phys_fcp_rsp);
318 } else {
319 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
320 iocb->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
321 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_bpl);
322 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_bpl);
323 iocb->ulpBdeCount = 1;
324 iocb->ulpLe = 1;
325 }
dea3101e 326 iocb->ulpClass = CLASS3;
327
328 return psb;
329}
330
455c53ec 331static struct lpfc_scsi_buf*
875fbdfe 332lpfc_get_scsi_buf(struct lpfc_hba * phba)
dea3101e 333{
0bd4ca25
JSEC
334 struct lpfc_scsi_buf * lpfc_cmd = NULL;
335 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
875fbdfe 336 unsigned long iflag = 0;
0bd4ca25 337
875fbdfe 338 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
0bd4ca25 339 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
1dcb58e5
JS
340 if (lpfc_cmd) {
341 lpfc_cmd->seg_cnt = 0;
342 lpfc_cmd->nonsg_phys = 0;
343 }
875fbdfe 344 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
0bd4ca25
JSEC
345 return lpfc_cmd;
346}
dea3101e 347
0bd4ca25 348static void
92d7f7b0 349lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
0bd4ca25 350{
875fbdfe 351 unsigned long iflag = 0;
dea3101e 352
875fbdfe 353 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
0bd4ca25 354 psb->pCmd = NULL;
dea3101e 355 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
875fbdfe 356 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea3101e 357}
358
359static int
92d7f7b0 360lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea3101e 361{
362 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
363 struct scatterlist *sgel = NULL;
364 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
365 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
366 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
34b02dcd 367 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea3101e 368 dma_addr_t physaddr;
34b02dcd 369 uint32_t num_bde = 0;
a0b4f78f 370 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea3101e 371
372 /*
373 * There are three possibilities here - use scatter-gather segment, use
374 * the single mapping, or neither. Start the lpfc command prep by
375 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
376 * data bde entry.
377 */
378 bpl += 2;
c59fd9eb 379 if (scsi_sg_count(scsi_cmnd)) {
dea3101e 380 /*
381 * The driver stores the segment count returned from pci_map_sg
382 * because this a count of dma-mappings used to map the use_sg
383 * pages. They are not guaranteed to be the same for those
384 * architectures that implement an IOMMU.
385 */
dea3101e 386
c59fd9eb
FT
387 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
388 scsi_sg_count(scsi_cmnd), datadir);
389 if (unlikely(!nseg))
390 return 1;
391
a0b4f78f 392 lpfc_cmd->seg_cnt = nseg;
dea3101e 393 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
394 printk(KERN_ERR "%s: Too many sg segments from "
395 "dma_map_sg. Config %d, seg_cnt %d",
cadbd4a5 396 __func__, phba->cfg_sg_seg_cnt,
dea3101e 397 lpfc_cmd->seg_cnt);
a0b4f78f 398 scsi_dma_unmap(scsi_cmnd);
dea3101e 399 return 1;
400 }
401
402 /*
403 * The driver established a maximum scatter-gather segment count
404 * during probe that limits the number of sg elements in any
405 * single scsi command. Just run through the seg_cnt and format
406 * the bde's.
34b02dcd
JS
407 * When using SLI-3 the driver will try to fit all the BDEs into
408 * the IOCB. If it can't then the BDEs get added to a BPL as it
409 * does for SLI-2 mode.
dea3101e 410 */
34b02dcd 411 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea3101e 412 physaddr = sg_dma_address(sgel);
34b02dcd
JS
413 if (phba->sli_rev == 3 &&
414 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
415 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
416 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
417 data_bde->addrLow = putPaddrLow(physaddr);
418 data_bde->addrHigh = putPaddrHigh(physaddr);
419 data_bde++;
420 } else {
421 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
422 bpl->tus.f.bdeSize = sg_dma_len(sgel);
423 bpl->tus.w = le32_to_cpu(bpl->tus.w);
424 bpl->addrLow =
425 le32_to_cpu(putPaddrLow(physaddr));
426 bpl->addrHigh =
427 le32_to_cpu(putPaddrHigh(physaddr));
428 bpl++;
429 }
dea3101e 430 }
c59fd9eb 431 }
dea3101e 432
433 /*
434 * Finish initializing those IOCB fields that are dependent on the
34b02dcd
JS
435 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
436 * explicitly reinitialized and for SLI-3 the extended bde count is
437 * explicitly reinitialized since all iocb memory resources are reused.
dea3101e 438 */
34b02dcd
JS
439 if (phba->sli_rev == 3) {
440 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
441 /*
442 * The extended IOCB format can only fit 3 BDE or a BPL.
443 * This I/O has more than 3 BDE so the 1st data bde will
444 * be a BPL that is filled in here.
445 */
446 physaddr = lpfc_cmd->dma_handle;
447 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
448 data_bde->tus.f.bdeSize = (num_bde *
449 sizeof(struct ulp_bde64));
450 physaddr += (sizeof(struct fcp_cmnd) +
451 sizeof(struct fcp_rsp) +
452 (2 * sizeof(struct ulp_bde64)));
453 data_bde->addrHigh = putPaddrHigh(physaddr);
454 data_bde->addrLow = putPaddrLow(physaddr);
455 /* ebde count includes the responce bde and data bpl */
456 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
457 } else {
458 /* ebde count includes the responce bde and data bdes */
459 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
460 }
461 } else {
462 iocb_cmd->un.fcpi64.bdl.bdeSize =
463 ((num_bde + 2) * sizeof(struct ulp_bde64));
464 }
09372820 465 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
dea3101e 466 return 0;
467}
468
bcf4dbfa
JS
469static void
470lpfc_scsi_unprep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * psb)
471{
472 /*
473 * There are only two special cases to consider. (1) the scsi command
474 * requested scatter-gather usage or (2) the scsi command allocated
475 * a request buffer, but did not request use_sg. There is a third
476 * case, but it does not require resource deallocation.
477 */
a0b4f78f
FT
478 if (psb->seg_cnt > 0)
479 scsi_dma_unmap(psb->pCmd);
bcf4dbfa
JS
480}
481
dea3101e 482static void
2e0fef85
JS
483lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
484 struct lpfc_iocbq *rsp_iocb)
dea3101e 485{
486 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
487 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
488 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
7054a606 489 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea3101e 490 uint32_t resp_info = fcprsp->rspStatus2;
491 uint32_t scsi_status = fcprsp->rspStatus3;
c7743956 492 uint32_t *lp;
dea3101e 493 uint32_t host_status = DID_OK;
494 uint32_t rsplen = 0;
c7743956 495 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea3101e 496
497 /*
498 * If this is a task management command, there is no
499 * scsi packet associated with this lpfc_cmd. The driver
500 * consumes it.
501 */
502 if (fcpcmd->fcpCntl2) {
503 scsi_status = 0;
504 goto out;
505 }
506
c7743956
JS
507 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
508 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
509 if (snslen > SCSI_SENSE_BUFFERSIZE)
510 snslen = SCSI_SENSE_BUFFERSIZE;
511
512 if (resp_info & RSP_LEN_VALID)
513 rsplen = be32_to_cpu(fcprsp->rspRspLen);
514 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
515 }
516 lp = (uint32_t *)cmnd->sense_buffer;
517
518 if (!scsi_status && (resp_info & RESID_UNDER))
519 logit = LOG_FCP;
520
e8b62011
JS
521 lpfc_printf_vlog(vport, KERN_WARNING, logit,
522 "0730 FCP command x%x failed: x%x SNS x%x x%x "
523 "Data: x%x x%x x%x x%x x%x\n",
524 cmnd->cmnd[0], scsi_status,
525 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
526 be32_to_cpu(fcprsp->rspResId),
527 be32_to_cpu(fcprsp->rspSnsLen),
528 be32_to_cpu(fcprsp->rspRspLen),
529 fcprsp->rspInfo3);
dea3101e 530
531 if (resp_info & RSP_LEN_VALID) {
532 rsplen = be32_to_cpu(fcprsp->rspRspLen);
533 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
534 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
535 host_status = DID_ERROR;
536 goto out;
537 }
538 }
539
a0b4f78f 540 scsi_set_resid(cmnd, 0);
dea3101e 541 if (resp_info & RESID_UNDER) {
a0b4f78f 542 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea3101e 543
e8b62011
JS
544 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
545 "0716 FCP Read Underrun, expected %d, "
546 "residual %d Data: x%x x%x x%x\n",
547 be32_to_cpu(fcpcmd->fcpDl),
548 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
549 cmnd->underflow);
dea3101e 550
7054a606
JS
551 /*
552 * If there is an under run check if under run reported by
553 * storage array is same as the under run reported by HBA.
554 * If this is not same, there is a dropped frame.
555 */
556 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
557 fcpi_parm &&
a0b4f78f 558 (scsi_get_resid(cmnd) != fcpi_parm)) {
e8b62011
JS
559 lpfc_printf_vlog(vport, KERN_WARNING,
560 LOG_FCP | LOG_FCP_ERROR,
561 "0735 FCP Read Check Error "
562 "and Underrun Data: x%x x%x x%x x%x\n",
563 be32_to_cpu(fcpcmd->fcpDl),
564 scsi_get_resid(cmnd), fcpi_parm,
565 cmnd->cmnd[0]);
a0b4f78f 566 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
7054a606
JS
567 host_status = DID_ERROR;
568 }
dea3101e 569 /*
570 * The cmnd->underflow is the minimum number of bytes that must
571 * be transfered for this command. Provided a sense condition
572 * is not present, make sure the actual amount transferred is at
573 * least the underflow value or fail.
574 */
575 if (!(resp_info & SNS_LEN_VALID) &&
576 (scsi_status == SAM_STAT_GOOD) &&
a0b4f78f
FT
577 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
578 < cmnd->underflow)) {
e8b62011
JS
579 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
580 "0717 FCP command x%x residual "
581 "underrun converted to error "
582 "Data: x%x x%x x%x\n",
66dbfbe6 583 cmnd->cmnd[0], scsi_bufflen(cmnd),
e8b62011 584 scsi_get_resid(cmnd), cmnd->underflow);
dea3101e 585 host_status = DID_ERROR;
586 }
587 } else if (resp_info & RESID_OVER) {
e8b62011
JS
588 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
589 "0720 FCP command x%x residual overrun error. "
590 "Data: x%x x%x \n", cmnd->cmnd[0],
591 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea3101e 592 host_status = DID_ERROR;
593
594 /*
595 * Check SLI validation that all the transfer was actually done
596 * (fcpi_parm should be zero). Apply check only to reads.
597 */
598 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
599 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
e8b62011
JS
600 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
601 "0734 FCP Read Check Error Data: "
602 "x%x x%x x%x x%x\n",
603 be32_to_cpu(fcpcmd->fcpDl),
604 be32_to_cpu(fcprsp->rspResId),
605 fcpi_parm, cmnd->cmnd[0]);
dea3101e 606 host_status = DID_ERROR;
a0b4f78f 607 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea3101e 608 }
609
610 out:
611 cmnd->result = ScsiResult(host_status, scsi_status);
612}
613
614static void
615lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
616 struct lpfc_iocbq *pIocbOut)
617{
618 struct lpfc_scsi_buf *lpfc_cmd =
619 (struct lpfc_scsi_buf *) pIocbIn->context1;
2e0fef85 620 struct lpfc_vport *vport = pIocbIn->vport;
dea3101e 621 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
622 struct lpfc_nodelist *pnode = rdata->pnode;
623 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
445cf4f4
JSEC
624 int result;
625 struct scsi_device *sdev, *tmp_sdev;
626 int depth = 0;
fa61a54e 627 unsigned long flags;
dea3101e 628
629 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
630 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
631
632 if (lpfc_cmd->status) {
633 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
634 (lpfc_cmd->result & IOERR_DRVR_MASK))
635 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
636 else if (lpfc_cmd->status >= IOSTAT_CNT)
637 lpfc_cmd->status = IOSTAT_DEFAULT;
638
e8b62011
JS
639 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
640 "0729 FCP cmd x%x failed <%d/%d> "
641 "status: x%x result: x%x Data: x%x x%x\n",
642 cmd->cmnd[0],
643 cmd->device ? cmd->device->id : 0xffff,
644 cmd->device ? cmd->device->lun : 0xffff,
645 lpfc_cmd->status, lpfc_cmd->result,
646 pIocbOut->iocb.ulpContext,
647 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea3101e 648
649 switch (lpfc_cmd->status) {
650 case IOSTAT_FCP_RSP_ERROR:
651 /* Call FCP RSP handler to determine result */
2e0fef85 652 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea3101e 653 break;
654 case IOSTAT_NPORT_BSY:
655 case IOSTAT_FABRIC_BSY:
0f1f53a7 656 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
dea3101e 657 break;
92d7f7b0 658 case IOSTAT_LOCAL_REJECT:
d7c255b2 659 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
92d7f7b0 660 lpfc_cmd->result == IOERR_NO_RESOURCES ||
d7c255b2 661 lpfc_cmd->result == IOERR_ABORT_REQUESTED) {
92d7f7b0 662 cmd->result = ScsiResult(DID_REQUEUE, 0);
58da1ffb
JS
663 break;
664 } /* else: fall through */
dea3101e 665 default:
666 cmd->result = ScsiResult(DID_ERROR, 0);
667 break;
668 }
669
58da1ffb 670 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
19a7b4ae 671 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
0f1f53a7
JS
672 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
673 SAM_STAT_BUSY);
dea3101e 674 } else {
675 cmd->result = ScsiResult(DID_OK, 0);
676 }
677
678 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
679 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
680
e8b62011
JS
681 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
682 "0710 Iodone <%d/%d> cmd %p, error "
683 "x%x SNS x%x x%x Data: x%x x%x\n",
684 cmd->device->id, cmd->device->lun, cmd,
685 cmd->result, *lp, *(lp + 3), cmd->retries,
686 scsi_get_resid(cmd));
dea3101e 687 }
688
445cf4f4
JSEC
689 result = cmd->result;
690 sdev = cmd->device;
1dcb58e5 691 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
0bd4ca25
JSEC
692 cmd->scsi_done(cmd);
693
b808608b 694 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
fa61a54e
JS
695 /*
696 * If there is a thread waiting for command completion
697 * wake up the thread.
698 */
699 spin_lock_irqsave(sdev->host->host_lock, flags);
495a714c 700 lpfc_cmd->pCmd = NULL;
fa61a54e
JS
701 if (lpfc_cmd->waitq)
702 wake_up(lpfc_cmd->waitq);
703 spin_unlock_irqrestore(sdev->host->host_lock, flags);
b808608b
JW
704 lpfc_release_scsi_buf(phba, lpfc_cmd);
705 return;
706 }
707
92d7f7b0
JS
708
709 if (!result)
3de2a653 710 lpfc_rampup_queue_depth(vport, sdev);
92d7f7b0 711
58da1ffb 712 if (!result && pnode && NLP_CHK_NODE_ACT(pnode) &&
445cf4f4
JSEC
713 ((jiffies - pnode->last_ramp_up_time) >
714 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
715 ((jiffies - pnode->last_q_full_time) >
716 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
3de2a653 717 (vport->cfg_lun_queue_depth > sdev->queue_depth)) {
445cf4f4 718 shost_for_each_device(tmp_sdev, sdev->host) {
3de2a653 719 if (vport->cfg_lun_queue_depth > tmp_sdev->queue_depth){
445cf4f4
JSEC
720 if (tmp_sdev->id != sdev->id)
721 continue;
722 if (tmp_sdev->ordered_tags)
723 scsi_adjust_queue_depth(tmp_sdev,
724 MSG_ORDERED_TAG,
725 tmp_sdev->queue_depth+1);
726 else
727 scsi_adjust_queue_depth(tmp_sdev,
728 MSG_SIMPLE_TAG,
729 tmp_sdev->queue_depth+1);
730
731 pnode->last_ramp_up_time = jiffies;
732 }
733 }
734 }
735
736 /*
737 * Check for queue full. If the lun is reporting queue full, then
738 * back off the lun queue depth to prevent target overloads.
739 */
58da1ffb
JS
740 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
741 NLP_CHK_NODE_ACT(pnode)) {
445cf4f4
JSEC
742 pnode->last_q_full_time = jiffies;
743
744 shost_for_each_device(tmp_sdev, sdev->host) {
745 if (tmp_sdev->id != sdev->id)
746 continue;
747 depth = scsi_track_queue_full(tmp_sdev,
748 tmp_sdev->queue_depth - 1);
749 }
750 /*
2e0fef85 751 * The queue depth cannot be lowered any more.
445cf4f4
JSEC
752 * Modify the returned error code to store
753 * the final depth value set by
754 * scsi_track_queue_full.
755 */
756 if (depth == -1)
757 depth = sdev->host->cmd_per_lun;
758
759 if (depth) {
e8b62011
JS
760 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
761 "0711 detected queue full - lun queue "
762 "depth adjusted to %d.\n", depth);
445cf4f4
JSEC
763 }
764 }
765
fa61a54e
JS
766 /*
767 * If there is a thread waiting for command completion
768 * wake up the thread.
769 */
770 spin_lock_irqsave(sdev->host->host_lock, flags);
495a714c 771 lpfc_cmd->pCmd = NULL;
fa61a54e
JS
772 if (lpfc_cmd->waitq)
773 wake_up(lpfc_cmd->waitq);
774 spin_unlock_irqrestore(sdev->host->host_lock, flags);
775
0bd4ca25 776 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea3101e 777}
778
34b02dcd
JS
779/**
780 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB.
781 * @data: A pointer to the immediate command data portion of the IOCB.
782 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
783 *
784 * The routine copies the entire FCP command from @fcp_cmnd to @data while
785 * byte swapping the data to big endian format for transmission on the wire.
786 **/
787static void
788lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
789{
790 int i, j;
791 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
792 i += sizeof(uint32_t), j++) {
793 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
794 }
795}
796
dea3101e 797static void
2e0fef85
JS
798lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
799 struct lpfc_nodelist *pnode)
dea3101e 800{
2e0fef85 801 struct lpfc_hba *phba = vport->phba;
dea3101e 802 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
803 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
804 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
805 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
806 int datadir = scsi_cmnd->sc_data_direction;
7e2b19fb 807 char tag[2];
dea3101e 808
58da1ffb
JS
809 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
810 return;
811
dea3101e 812 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
69859dc4
JSEC
813 /* clear task management bits */
814 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea3101e 815
91886523
JSEC
816 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
817 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea3101e 818
819 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
820
7e2b19fb
JS
821 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
822 switch (tag[0]) {
dea3101e 823 case HEAD_OF_QUEUE_TAG:
824 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
825 break;
826 case ORDERED_QUEUE_TAG:
827 fcp_cmnd->fcpCntl1 = ORDERED_Q;
828 break;
829 default:
830 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
831 break;
832 }
833 } else
834 fcp_cmnd->fcpCntl1 = 0;
835
836 /*
837 * There are three possibilities here - use scatter-gather segment, use
838 * the single mapping, or neither. Start the lpfc command prep by
839 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
840 * data bde entry.
841 */
a0b4f78f 842 if (scsi_sg_count(scsi_cmnd)) {
dea3101e 843 if (datadir == DMA_TO_DEVICE) {
844 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
845 iocb_cmd->un.fcpi.fcpi_parm = 0;
846 iocb_cmd->ulpPU = 0;
847 fcp_cmnd->fcpCntl3 = WRITE_DATA;
848 phba->fc4OutputRequests++;
849 } else {
850 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
851 iocb_cmd->ulpPU = PARM_READ_CHECK;
a0b4f78f 852 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea3101e 853 fcp_cmnd->fcpCntl3 = READ_DATA;
854 phba->fc4InputRequests++;
855 }
856 } else {
857 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
858 iocb_cmd->un.fcpi.fcpi_parm = 0;
859 iocb_cmd->ulpPU = 0;
860 fcp_cmnd->fcpCntl3 = 0;
861 phba->fc4ControlRequests++;
862 }
34b02dcd
JS
863 if (phba->sli_rev == 3)
864 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea3101e 865 /*
866 * Finish initializing those IOCB fields that are independent
867 * of the scsi_cmnd request_buffer
868 */
869 piocbq->iocb.ulpContext = pnode->nlp_rpi;
870 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
871 piocbq->iocb.ulpFCP2Rcvy = 1;
09372820
JS
872 else
873 piocbq->iocb.ulpFCP2Rcvy = 0;
dea3101e 874
875 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
876 piocbq->context1 = lpfc_cmd;
877 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
878 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
2e0fef85 879 piocbq->vport = vport;
dea3101e 880}
881
882static int
2e0fef85 883lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea3101e 884 struct lpfc_scsi_buf *lpfc_cmd,
420b630d 885 unsigned int lun,
dea3101e 886 uint8_t task_mgmt_cmd)
887{
dea3101e 888 struct lpfc_iocbq *piocbq;
889 IOCB_t *piocb;
890 struct fcp_cmnd *fcp_cmnd;
0b18ac42 891 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea3101e 892 struct lpfc_nodelist *ndlp = rdata->pnode;
893
58da1ffb
JS
894 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
895 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea3101e 896 return 0;
dea3101e 897
dea3101e 898 piocbq = &(lpfc_cmd->cur_iocbq);
2e0fef85
JS
899 piocbq->vport = vport;
900
dea3101e 901 piocb = &piocbq->iocb;
902
903 fcp_cmnd = lpfc_cmd->fcp_cmnd;
34b02dcd
JS
904 /* Clear out any old data in the FCP command area */
905 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
906 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea3101e 907 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
34b02dcd
JS
908 if (vport->phba->sli_rev == 3)
909 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea3101e 910 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea3101e 911 piocb->ulpContext = ndlp->nlp_rpi;
912 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
913 piocb->ulpFCP2Rcvy = 1;
914 }
915 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
916
917 /* ulpTimeout is only one byte */
918 if (lpfc_cmd->timeout > 0xff) {
919 /*
920 * Do not timeout the command at the firmware level.
921 * The driver will provide the timeout mechanism.
922 */
923 piocb->ulpTimeout = 0;
924 } else {
925 piocb->ulpTimeout = lpfc_cmd->timeout;
926 }
927
2e0fef85 928 return 1;
dea3101e 929}
930
7054a606
JS
931static void
932lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
933 struct lpfc_iocbq *cmdiocbq,
934 struct lpfc_iocbq *rspiocbq)
935{
936 struct lpfc_scsi_buf *lpfc_cmd =
937 (struct lpfc_scsi_buf *) cmdiocbq->context1;
938 if (lpfc_cmd)
939 lpfc_release_scsi_buf(phba, lpfc_cmd);
940 return;
941}
942
dea3101e 943static int
2e0fef85 944lpfc_scsi_tgt_reset(struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_vport *vport,
420b630d
JS
945 unsigned tgt_id, unsigned int lun,
946 struct lpfc_rport_data *rdata)
dea3101e 947{
2e0fef85 948 struct lpfc_hba *phba = vport->phba;
dea3101e 949 struct lpfc_iocbq *iocbq;
0bd4ca25 950 struct lpfc_iocbq *iocbqrsp;
dea3101e 951 int ret;
915caaaf 952 int status;
dea3101e 953
58da1ffb 954 if (!rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
f5603511
JS
955 return FAILED;
956
0b18ac42 957 lpfc_cmd->rdata = rdata;
915caaaf 958 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun,
420b630d 959 FCP_TARGET_RESET);
915caaaf 960 if (!status)
dea3101e 961 return FAILED;
962
dea3101e 963 iocbq = &lpfc_cmd->cur_iocbq;
0bd4ca25
JSEC
964 iocbqrsp = lpfc_sli_get_iocbq(phba);
965
dea3101e 966 if (!iocbqrsp)
967 return FAILED;
dea3101e 968
0b18ac42 969 /* Issue Target Reset to TGT <num> */
e8b62011
JS
970 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
971 "0702 Issue Target Reset to TGT %d Data: x%x x%x\n",
972 tgt_id, rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
915caaaf 973 status = lpfc_sli_issue_iocb_wait(phba,
68876920
JSEC
974 &phba->sli.ring[phba->sli.fcp_ring],
975 iocbq, iocbqrsp, lpfc_cmd->timeout);
915caaaf
JS
976 if (status != IOCB_SUCCESS) {
977 if (status == IOCB_TIMEDOUT) {
7054a606 978 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
915caaaf
JS
979 ret = TIMEOUT_ERROR;
980 } else
981 ret = FAILED;
dea3101e 982 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
dea3101e 983 } else {
984 ret = SUCCESS;
985 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
986 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
987 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
988 (lpfc_cmd->result & IOERR_DRVR_MASK))
989 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
990 }
991
604a3e30 992 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea3101e 993 return ret;
994}
995
dea3101e 996const char *
997lpfc_info(struct Scsi_Host *host)
998{
2e0fef85
JS
999 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
1000 struct lpfc_hba *phba = vport->phba;
dea3101e 1001 int len;
1002 static char lpfcinfobuf[384];
1003
1004 memset(lpfcinfobuf,0,384);
1005 if (phba && phba->pcidev){
1006 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
1007 len = strlen(lpfcinfobuf);
1008 snprintf(lpfcinfobuf + len,
1009 384-len,
1010 " on PCI bus %02x device %02x irq %d",
1011 phba->pcidev->bus->number,
1012 phba->pcidev->devfn,
1013 phba->pcidev->irq);
1014 len = strlen(lpfcinfobuf);
1015 if (phba->Port[0]) {
1016 snprintf(lpfcinfobuf + len,
1017 384-len,
1018 " port %s",
1019 phba->Port);
1020 }
1021 }
1022 return lpfcinfobuf;
1023}
1024
875fbdfe
JSEC
1025static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
1026{
1027 unsigned long poll_tmo_expires =
1028 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
1029
1030 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
1031 mod_timer(&phba->fcp_poll_timer,
1032 poll_tmo_expires);
1033}
1034
1035void lpfc_poll_start_timer(struct lpfc_hba * phba)
1036{
1037 lpfc_poll_rearm_timer(phba);
1038}
1039
1040void lpfc_poll_timeout(unsigned long ptr)
1041{
2e0fef85 1042 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
875fbdfe
JSEC
1043
1044 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1045 lpfc_sli_poll_fcp_ring (phba);
1046 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1047 lpfc_poll_rearm_timer(phba);
1048 }
875fbdfe
JSEC
1049}
1050
dea3101e 1051static int
1052lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
1053{
2e0fef85
JS
1054 struct Scsi_Host *shost = cmnd->device->host;
1055 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1056 struct lpfc_hba *phba = vport->phba;
1057 struct lpfc_sli *psli = &phba->sli;
dea3101e 1058 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1059 struct lpfc_nodelist *ndlp = rdata->pnode;
0bd4ca25 1060 struct lpfc_scsi_buf *lpfc_cmd;
19a7b4ae 1061 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
19a7b4ae 1062 int err;
dea3101e 1063
19a7b4ae
JSEC
1064 err = fc_remote_port_chkready(rport);
1065 if (err) {
1066 cmnd->result = err;
dea3101e 1067 goto out_fail_command;
1068 }
1069
1070 /*
19a7b4ae
JSEC
1071 * Catch race where our node has transitioned, but the
1072 * transport is still transitioning.
dea3101e 1073 */
b522d7d4
JS
1074 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1075 cmnd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
1076 goto out_fail_command;
1077 }
a93ce024 1078
ed957684 1079 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea3101e 1080 if (lpfc_cmd == NULL) {
92d7f7b0
JS
1081 lpfc_adjust_queue_depth(phba);
1082
e8b62011
JS
1083 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
1084 "0707 driver's buffer pool is empty, "
1085 "IO busied\n");
dea3101e 1086 goto out_host_busy;
1087 }
1088
1089 /*
1090 * Store the midlayer's command structure for the completion phase
1091 * and complete the command initialization.
1092 */
1093 lpfc_cmd->pCmd = cmnd;
1094 lpfc_cmd->rdata = rdata;
1095 lpfc_cmd->timeout = 0;
1096 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
1097 cmnd->scsi_done = done;
1098
1099 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
1100 if (err)
1101 goto out_host_busy_free_buf;
1102
2e0fef85 1103 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea3101e 1104
1105 err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
92d7f7b0 1106 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
dea3101e 1107 if (err)
1108 goto out_host_busy_free_buf;
875fbdfe
JSEC
1109
1110 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1111 lpfc_sli_poll_fcp_ring(phba);
1112 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1113 lpfc_poll_rearm_timer(phba);
1114 }
1115
dea3101e 1116 return 0;
1117
1118 out_host_busy_free_buf:
bcf4dbfa 1119 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
0bd4ca25 1120 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea3101e 1121 out_host_busy:
1122 return SCSI_MLQUEUE_HOST_BUSY;
1123
1124 out_fail_command:
1125 done(cmnd);
1126 return 0;
1127}
1128
a90f5684
JS
1129static void
1130lpfc_block_error_handler(struct scsi_cmnd *cmnd)
1131{
1132 struct Scsi_Host *shost = cmnd->device->host;
1133 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1134
1135 spin_lock_irq(shost->host_lock);
1136 while (rport->port_state == FC_PORTSTATE_BLOCKED) {
1137 spin_unlock_irq(shost->host_lock);
1138 msleep(1000);
1139 spin_lock_irq(shost->host_lock);
1140 }
1141 spin_unlock_irq(shost->host_lock);
1142 return;
1143}
63c59c3b 1144
dea3101e 1145static int
63c59c3b 1146lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea3101e 1147{
2e0fef85
JS
1148 struct Scsi_Host *shost = cmnd->device->host;
1149 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1150 struct lpfc_hba *phba = vport->phba;
dea3101e 1151 struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
0bd4ca25
JSEC
1152 struct lpfc_iocbq *iocb;
1153 struct lpfc_iocbq *abtsiocb;
dea3101e 1154 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 1155 IOCB_t *cmd, *icmd;
0bd4ca25 1156 int ret = SUCCESS;
fa61a54e 1157 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
dea3101e 1158
a90f5684 1159 lpfc_block_error_handler(cmnd);
0bd4ca25
JSEC
1160 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
1161 BUG_ON(!lpfc_cmd);
dea3101e 1162
0bd4ca25
JSEC
1163 /*
1164 * If pCmd field of the corresponding lpfc_scsi_buf structure
1165 * points to a different SCSI command, then the driver has
1166 * already completed this command, but the midlayer did not
1167 * see the completion before the eh fired. Just return
1168 * SUCCESS.
1169 */
1170 iocb = &lpfc_cmd->cur_iocbq;
1171 if (lpfc_cmd->pCmd != cmnd)
1172 goto out;
dea3101e 1173
0bd4ca25 1174 BUG_ON(iocb->context1 != lpfc_cmd);
dea3101e 1175
0bd4ca25
JSEC
1176 abtsiocb = lpfc_sli_get_iocbq(phba);
1177 if (abtsiocb == NULL) {
1178 ret = FAILED;
dea3101e 1179 goto out;
1180 }
1181
dea3101e 1182 /*
0bd4ca25
JSEC
1183 * The scsi command can not be in txq and it is in flight because the
1184 * pCmd is still pointig at the SCSI command we have to abort. There
1185 * is no need to search the txcmplq. Just send an abort to the FW.
dea3101e 1186 */
dea3101e 1187
0bd4ca25
JSEC
1188 cmd = &iocb->iocb;
1189 icmd = &abtsiocb->iocb;
1190 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
1191 icmd->un.acxri.abortContextTag = cmd->ulpContext;
1192 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea3101e 1193
0bd4ca25
JSEC
1194 icmd->ulpLe = 1;
1195 icmd->ulpClass = cmd->ulpClass;
2e0fef85 1196 if (lpfc_is_link_up(phba))
0bd4ca25
JSEC
1197 icmd->ulpCommand = CMD_ABORT_XRI_CN;
1198 else
1199 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea3101e 1200
0bd4ca25 1201 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
2e0fef85 1202 abtsiocb->vport = vport;
0bd4ca25
JSEC
1203 if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
1204 lpfc_sli_release_iocbq(phba, abtsiocb);
1205 ret = FAILED;
1206 goto out;
1207 }
dea3101e 1208
875fbdfe
JSEC
1209 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1210 lpfc_sli_poll_fcp_ring (phba);
1211
fa61a54e 1212 lpfc_cmd->waitq = &waitq;
0bd4ca25 1213 /* Wait for abort to complete */
fa61a54e
JS
1214 wait_event_timeout(waitq,
1215 (lpfc_cmd->pCmd != cmnd),
1216 (2*vport->cfg_devloss_tmo*HZ));
875fbdfe 1217
fa61a54e
JS
1218 spin_lock_irq(shost->host_lock);
1219 lpfc_cmd->waitq = NULL;
1220 spin_unlock_irq(shost->host_lock);
dea3101e 1221
0bd4ca25
JSEC
1222 if (lpfc_cmd->pCmd == cmnd) {
1223 ret = FAILED;
e8b62011
JS
1224 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1225 "0748 abort handler timed out waiting "
1226 "for abort to complete: ret %#x, ID %d, "
1227 "LUN %d, snum %#lx\n",
1228 ret, cmnd->device->id, cmnd->device->lun,
1229 cmnd->serial_number);
dea3101e 1230 }
1231
1232 out:
e8b62011
JS
1233 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1234 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
1235 "LUN %d snum %#lx\n", ret, cmnd->device->id,
1236 cmnd->device->lun, cmnd->serial_number);
63c59c3b 1237 return ret;
8fa728a2
JG
1238}
1239
dea3101e 1240static int
7054a606 1241lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea3101e 1242{
2e0fef85
JS
1243 struct Scsi_Host *shost = cmnd->device->host;
1244 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1245 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
1246 struct lpfc_scsi_buf *lpfc_cmd;
1247 struct lpfc_iocbq *iocbq, *iocbqrsp;
dea3101e 1248 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1249 struct lpfc_nodelist *pnode = rdata->pnode;
915caaaf
JS
1250 unsigned long later;
1251 int ret = SUCCESS;
1252 int status;
1253 int cnt;
dea3101e 1254
a90f5684 1255 lpfc_block_error_handler(cmnd);
dea3101e 1256 /*
1257 * If target is not in a MAPPED state, delay the reset until
c01f3208 1258 * target is rediscovered or devloss timeout expires.
dea3101e 1259 */
915caaaf
JS
1260 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
1261 while (time_after(later, jiffies)) {
58da1ffb 1262 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
915caaaf 1263 return FAILED;
f5603511 1264 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
dea3101e 1265 break;
915caaaf
JS
1266 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
1267 rdata = cmnd->device->hostdata;
1268 if (!rdata)
1269 break;
1270 pnode = rdata->pnode;
1271 }
1272 if (!rdata || pnode->nlp_state != NLP_STE_MAPPED_NODE) {
1273 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1274 "0721 LUN Reset rport "
1275 "failure: msec x%x rdata x%p\n",
1276 jiffies_to_msecs(jiffies - later), rdata);
1277 return FAILED;
dea3101e 1278 }
2e0fef85 1279 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea3101e 1280 if (lpfc_cmd == NULL)
915caaaf 1281 return FAILED;
dea3101e 1282 lpfc_cmd->timeout = 60;
0b18ac42 1283 lpfc_cmd->rdata = rdata;
dea3101e 1284
915caaaf
JS
1285 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd,
1286 cmnd->device->lun,
1287 FCP_TARGET_RESET);
1288 if (!status) {
1289 lpfc_release_scsi_buf(phba, lpfc_cmd);
1290 return FAILED;
1291 }
dea3101e 1292 iocbq = &lpfc_cmd->cur_iocbq;
1293
1294 /* get a buffer for this IOCB command response */
0bd4ca25 1295 iocbqrsp = lpfc_sli_get_iocbq(phba);
915caaaf
JS
1296 if (iocbqrsp == NULL) {
1297 lpfc_release_scsi_buf(phba, lpfc_cmd);
1298 return FAILED;
1299 }
e8b62011
JS
1300 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
1301 "0703 Issue target reset to TGT %d LUN %d "
1302 "rpi x%x nlp_flag x%x\n", cmnd->device->id,
1303 cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
915caaaf
JS
1304 status = lpfc_sli_issue_iocb_wait(phba,
1305 &phba->sli.ring[phba->sli.fcp_ring],
1306 iocbq, iocbqrsp, lpfc_cmd->timeout);
1307 if (status == IOCB_TIMEDOUT) {
7054a606 1308 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
915caaaf
JS
1309 ret = TIMEOUT_ERROR;
1310 } else {
1311 if (status != IOCB_SUCCESS)
1312 ret = FAILED;
1313 lpfc_release_scsi_buf(phba, lpfc_cmd);
1314 }
1315 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1316 "0713 SCSI layer issued device reset (%d, %d) "
1317 "return x%x status x%x result x%x\n",
1318 cmnd->device->id, cmnd->device->lun, ret,
1319 iocbqrsp->iocb.ulpStatus,
1320 iocbqrsp->iocb.un.ulpWord[4]);
6175c02a 1321 lpfc_sli_release_iocbq(phba, iocbqrsp);
51ef4c26 1322 cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id, cmnd->device->lun,
915caaaf 1323 LPFC_CTX_TGT);
6175c02a 1324 if (cnt)
51ef4c26 1325 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
6175c02a 1326 cmnd->device->id, cmnd->device->lun,
915caaaf
JS
1327 LPFC_CTX_TGT);
1328 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
1329 while (time_after(later, jiffies) && cnt) {
1330 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
51ef4c26 1331 cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id,
915caaaf 1332 cmnd->device->lun, LPFC_CTX_TGT);
dea3101e 1333 }
dea3101e 1334 if (cnt) {
e8b62011
JS
1335 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1336 "0719 device reset I/O flush failure: "
1337 "cnt x%x\n", cnt);
0bd4ca25 1338 ret = FAILED;
dea3101e 1339 }
dea3101e 1340 return ret;
1341}
1342
94d0e7b8 1343static int
7054a606 1344lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea3101e 1345{
2e0fef85
JS
1346 struct Scsi_Host *shost = cmnd->device->host;
1347 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1348 struct lpfc_hba *phba = vport->phba;
dea3101e 1349 struct lpfc_nodelist *ndlp = NULL;
1350 int match;
d7c255b2 1351 int ret = SUCCESS, status = SUCCESS, i;
915caaaf 1352 int cnt;
0bd4ca25 1353 struct lpfc_scsi_buf * lpfc_cmd;
915caaaf 1354 unsigned long later;
dea3101e 1355
a90f5684 1356 lpfc_block_error_handler(cmnd);
dea3101e 1357 /*
1358 * Since the driver manages a single bus device, reset all
1359 * targets known to the driver. Should any target reset
1360 * fail, this routine returns failure to the midlayer.
1361 */
e17da18e 1362 for (i = 0; i < LPFC_MAX_TARGET; i++) {
685f0bf7 1363 /* Search for mapped node by target ID */
dea3101e 1364 match = 0;
2e0fef85
JS
1365 spin_lock_irq(shost->host_lock);
1366 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
e47c9093
JS
1367 if (!NLP_CHK_NODE_ACT(ndlp))
1368 continue;
685f0bf7 1369 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
915caaaf 1370 ndlp->nlp_sid == i &&
685f0bf7 1371 ndlp->rport) {
dea3101e 1372 match = 1;
1373 break;
1374 }
1375 }
2e0fef85 1376 spin_unlock_irq(shost->host_lock);
dea3101e 1377 if (!match)
1378 continue;
915caaaf
JS
1379 lpfc_cmd = lpfc_get_scsi_buf(phba);
1380 if (lpfc_cmd) {
1381 lpfc_cmd->timeout = 60;
1382 status = lpfc_scsi_tgt_reset(lpfc_cmd, vport, i,
1383 cmnd->device->lun,
1384 ndlp->rport->dd_data);
1385 if (status != TIMEOUT_ERROR)
1386 lpfc_release_scsi_buf(phba, lpfc_cmd);
1387 }
1388 if (!lpfc_cmd || status != SUCCESS) {
e8b62011
JS
1389 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1390 "0700 Bus Reset on target %d failed\n",
1391 i);
915caaaf 1392 ret = FAILED;
dea3101e 1393 }
1394 }
6175c02a
JSEC
1395 /*
1396 * All outstanding txcmplq I/Os should have been aborted by
1397 * the targets. Unfortunately, some targets do not abide by
1398 * this forcing the driver to double check.
1399 */
51ef4c26 1400 cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
6175c02a 1401 if (cnt)
51ef4c26
JS
1402 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
1403 0, 0, LPFC_CTX_HOST);
915caaaf
JS
1404 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
1405 while (time_after(later, jiffies) && cnt) {
1406 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
51ef4c26 1407 cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
dea3101e 1408 }
dea3101e 1409 if (cnt) {
e8b62011
JS
1410 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1411 "0715 Bus Reset I/O flush failure: "
1412 "cnt x%x left x%x\n", cnt, i);
0bd4ca25 1413 ret = FAILED;
6175c02a 1414 }
e8b62011
JS
1415 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1416 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea3101e 1417 return ret;
1418}
1419
1420static int
1421lpfc_slave_alloc(struct scsi_device *sdev)
1422{
2e0fef85
JS
1423 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1424 struct lpfc_hba *phba = vport->phba;
dea3101e 1425 struct lpfc_scsi_buf *scsi_buf = NULL;
19a7b4ae 1426 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
dea3101e 1427 uint32_t total = 0, i;
1428 uint32_t num_to_alloc = 0;
1429 unsigned long flags;
dea3101e 1430
19a7b4ae 1431 if (!rport || fc_remote_port_chkready(rport))
dea3101e 1432 return -ENXIO;
1433
19a7b4ae 1434 sdev->hostdata = rport->dd_data;
dea3101e 1435
1436 /*
1437 * Populate the cmds_per_lun count scsi_bufs into this host's globally
1438 * available list of scsi buffers. Don't allocate more than the
a784efbf
JSEC
1439 * HBA limit conveyed to the midlayer via the host structure. The
1440 * formula accounts for the lun_queue_depth + error handlers + 1
1441 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea3101e 1442 */
1443 total = phba->total_scsi_bufs;
3de2a653 1444 num_to_alloc = vport->cfg_lun_queue_depth + 2;
92d7f7b0
JS
1445
1446 /* Allow some exchanges to be available always to complete discovery */
1447 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
e8b62011
JS
1448 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1449 "0704 At limitation of %d preallocated "
1450 "command buffers\n", total);
dea3101e 1451 return 0;
92d7f7b0
JS
1452 /* Allow some exchanges to be available always to complete discovery */
1453 } else if (total + num_to_alloc >
1454 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
e8b62011
JS
1455 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1456 "0705 Allocation request of %d "
1457 "command buffers will exceed max of %d. "
1458 "Reducing allocation request to %d.\n",
1459 num_to_alloc, phba->cfg_hba_queue_depth,
1460 (phba->cfg_hba_queue_depth - total));
dea3101e 1461 num_to_alloc = phba->cfg_hba_queue_depth - total;
1462 }
1463
1464 for (i = 0; i < num_to_alloc; i++) {
2e0fef85 1465 scsi_buf = lpfc_new_scsi_buf(vport);
dea3101e 1466 if (!scsi_buf) {
e8b62011
JS
1467 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1468 "0706 Failed to allocate "
1469 "command buffer\n");
dea3101e 1470 break;
1471 }
1472
875fbdfe 1473 spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
dea3101e 1474 phba->total_scsi_bufs++;
1475 list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
875fbdfe 1476 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
dea3101e 1477 }
1478 return 0;
1479}
1480
1481static int
1482lpfc_slave_configure(struct scsi_device *sdev)
1483{
2e0fef85
JS
1484 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1485 struct lpfc_hba *phba = vport->phba;
1486 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
dea3101e 1487
1488 if (sdev->tagged_supported)
3de2a653 1489 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea3101e 1490 else
3de2a653 1491 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea3101e 1492
1493 /*
1494 * Initialize the fc transport attributes for the target
1495 * containing this scsi device. Also note that the driver's
1496 * target pointer is stored in the starget_data for the
1497 * driver's sysfs entry point functions.
1498 */
3de2a653 1499 rport->dev_loss_tmo = vport->cfg_devloss_tmo;
dea3101e 1500
875fbdfe
JSEC
1501 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1502 lpfc_sli_poll_fcp_ring(phba);
1503 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1504 lpfc_poll_rearm_timer(phba);
1505 }
1506
dea3101e 1507 return 0;
1508}
1509
1510static void
1511lpfc_slave_destroy(struct scsi_device *sdev)
1512{
1513 sdev->hostdata = NULL;
1514 return;
1515}
1516
92d7f7b0 1517
dea3101e 1518struct scsi_host_template lpfc_template = {
1519 .module = THIS_MODULE,
1520 .name = LPFC_DRIVER_NAME,
1521 .info = lpfc_info,
1522 .queuecommand = lpfc_queuecommand,
1523 .eh_abort_handler = lpfc_abort_handler,
7054a606
JS
1524 .eh_device_reset_handler= lpfc_device_reset_handler,
1525 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea3101e 1526 .slave_alloc = lpfc_slave_alloc,
1527 .slave_configure = lpfc_slave_configure,
1528 .slave_destroy = lpfc_slave_destroy,
47a8617c 1529 .scan_finished = lpfc_scan_finished,
dea3101e 1530 .this_id = -1,
83108bd3 1531 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea3101e 1532 .cmd_per_lun = LPFC_CMD_PER_LUN,
1533 .use_clustering = ENABLE_CLUSTERING,
2e0fef85 1534 .shost_attrs = lpfc_hba_attrs,
564b2960 1535 .max_sectors = 0xFFFF,
dea3101e 1536};
3de2a653
JS
1537
1538struct scsi_host_template lpfc_vport_template = {
1539 .module = THIS_MODULE,
1540 .name = LPFC_DRIVER_NAME,
1541 .info = lpfc_info,
1542 .queuecommand = lpfc_queuecommand,
1543 .eh_abort_handler = lpfc_abort_handler,
1544 .eh_device_reset_handler= lpfc_device_reset_handler,
1545 .eh_bus_reset_handler = lpfc_bus_reset_handler,
1546 .slave_alloc = lpfc_slave_alloc,
1547 .slave_configure = lpfc_slave_configure,
1548 .slave_destroy = lpfc_slave_destroy,
1549 .scan_finished = lpfc_scan_finished,
1550 .this_id = -1,
83108bd3 1551 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
3de2a653
JS
1552 .cmd_per_lun = LPFC_CMD_PER_LUN,
1553 .use_clustering = ENABLE_CLUSTERING,
1554 .shost_attrs = lpfc_vport_attrs,
1555 .max_sectors = 0xFFFF,
1556};