[PATCH] lpfc 8.1.3: Protect NPL lists with host lock
[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. *
0c6ac8ef 4 * Copyright (C) 2004-2006 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>
24
25#include <scsi/scsi.h>
26#include <scsi/scsi_device.h>
27#include <scsi/scsi_host.h>
28#include <scsi/scsi_tcq.h>
29#include <scsi/scsi_transport_fc.h>
30
31#include "lpfc_version.h"
32#include "lpfc_hw.h"
33#include "lpfc_sli.h"
34#include "lpfc_disc.h"
35#include "lpfc_scsi.h"
36#include "lpfc.h"
37#include "lpfc_logmsg.h"
38#include "lpfc_crtn.h"
39
40#define LPFC_RESET_WAIT 2
41#define LPFC_ABORT_WAIT 2
42
dea3101e 43
6175c02a
JSEC
44static inline void
45lpfc_block_requests(struct lpfc_hba * phba)
46{
47 down(&phba->hba_can_block);
48 scsi_block_requests(phba->host);
49}
50
51static inline void
52lpfc_unblock_requests(struct lpfc_hba * phba)
53{
54 scsi_unblock_requests(phba->host);
55 up(&phba->hba_can_block);
56}
57
dea3101e 58/*
59 * This routine allocates a scsi buffer, which contains all the necessary
60 * information needed to initiate a SCSI I/O. The non-DMAable buffer region
61 * contains information to build the IOCB. The DMAable region contains
62 * memory for the FCP CMND, FCP RSP, and the inital BPL. In addition to
63 * allocating memeory, the FCP CMND and FCP RSP BDEs are setup in the BPL
64 * and the BPL BDE is setup in the IOCB.
65 */
66static struct lpfc_scsi_buf *
0bd4ca25 67lpfc_new_scsi_buf(struct lpfc_hba * phba)
dea3101e 68{
69 struct lpfc_scsi_buf *psb;
70 struct ulp_bde64 *bpl;
71 IOCB_t *iocb;
72 dma_addr_t pdma_phys;
604a3e30 73 uint16_t iotag;
dea3101e 74
75 psb = kmalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
76 if (!psb)
77 return NULL;
78 memset(psb, 0, sizeof (struct lpfc_scsi_buf));
79 psb->scsi_hba = phba;
80
81 /*
82 * Get memory from the pci pool to map the virt space to pci bus space
83 * for an I/O. The DMA buffer includes space for the struct fcp_cmnd,
84 * struct fcp_rsp and the number of bde's necessary to support the
85 * sg_tablesize.
86 */
87 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool, GFP_KERNEL,
88 &psb->dma_handle);
89 if (!psb->data) {
90 kfree(psb);
91 return NULL;
92 }
93
94 /* Initialize virtual ptrs to dma_buf region. */
95 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
96
604a3e30
JB
97 /* Allocate iotag for psb->cur_iocbq. */
98 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
99 if (iotag == 0) {
100 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
101 psb->data, psb->dma_handle);
102 kfree (psb);
103 return NULL;
104 }
0bd4ca25 105 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
604a3e30 106
dea3101e 107 psb->fcp_cmnd = psb->data;
108 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
109 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
110 sizeof(struct fcp_rsp);
111
112 /* Initialize local short-hand pointers. */
113 bpl = psb->fcp_bpl;
114 pdma_phys = psb->dma_handle;
115
116 /*
117 * The first two bdes are the FCP_CMD and FCP_RSP. The balance are sg
118 * list bdes. Initialize the first two and leave the rest for
119 * queuecommand.
120 */
121 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
122 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
123 bpl->tus.f.bdeSize = sizeof (struct fcp_cmnd);
124 bpl->tus.f.bdeFlags = BUFF_USE_CMND;
125 bpl->tus.w = le32_to_cpu(bpl->tus.w);
126 bpl++;
127
128 /* Setup the physical region for the FCP RSP */
129 pdma_phys += sizeof (struct fcp_cmnd);
130 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
131 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
132 bpl->tus.f.bdeSize = sizeof (struct fcp_rsp);
133 bpl->tus.f.bdeFlags = (BUFF_USE_CMND | BUFF_USE_RCV);
134 bpl->tus.w = le32_to_cpu(bpl->tus.w);
135
136 /*
137 * Since the IOCB for the FCP I/O is built into this lpfc_scsi_buf,
138 * initialize it with all known data now.
139 */
140 pdma_phys += (sizeof (struct fcp_rsp));
141 iocb = &psb->cur_iocbq.iocb;
142 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
143 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys);
144 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys);
145 iocb->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
146 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDL;
147 iocb->ulpBdeCount = 1;
148 iocb->ulpClass = CLASS3;
149
150 return psb;
151}
152
455c53ec 153static struct lpfc_scsi_buf*
875fbdfe 154lpfc_get_scsi_buf(struct lpfc_hba * phba)
dea3101e 155{
0bd4ca25
JSEC
156 struct lpfc_scsi_buf * lpfc_cmd = NULL;
157 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
875fbdfe 158 unsigned long iflag = 0;
0bd4ca25 159
875fbdfe 160 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
0bd4ca25 161 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
875fbdfe 162 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
0bd4ca25
JSEC
163 return lpfc_cmd;
164}
dea3101e 165
0bd4ca25
JSEC
166static void
167lpfc_release_scsi_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * psb)
168{
875fbdfe 169 unsigned long iflag = 0;
dea3101e 170 /*
171 * There are only two special cases to consider. (1) the scsi command
172 * requested scatter-gather usage or (2) the scsi command allocated
173 * a request buffer, but did not request use_sg. There is a third
174 * case, but it does not require resource deallocation.
175 */
176 if ((psb->seg_cnt > 0) && (psb->pCmd->use_sg)) {
177 dma_unmap_sg(&phba->pcidev->dev, psb->pCmd->request_buffer,
178 psb->seg_cnt, psb->pCmd->sc_data_direction);
179 } else {
180 if ((psb->nonsg_phys) && (psb->pCmd->request_bufflen)) {
181 dma_unmap_single(&phba->pcidev->dev, psb->nonsg_phys,
182 psb->pCmd->request_bufflen,
183 psb->pCmd->sc_data_direction);
184 }
185 }
186
875fbdfe 187 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
0bd4ca25 188 psb->pCmd = NULL;
dea3101e 189 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
875fbdfe 190 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea3101e 191}
192
193static int
194lpfc_scsi_prep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * lpfc_cmd)
195{
196 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
197 struct scatterlist *sgel = NULL;
198 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
199 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
200 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
201 dma_addr_t physaddr;
202 uint32_t i, num_bde = 0;
203 int datadir = scsi_cmnd->sc_data_direction;
204 int dma_error;
205
206 /*
207 * There are three possibilities here - use scatter-gather segment, use
208 * the single mapping, or neither. Start the lpfc command prep by
209 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
210 * data bde entry.
211 */
212 bpl += 2;
213 if (scsi_cmnd->use_sg) {
214 /*
215 * The driver stores the segment count returned from pci_map_sg
216 * because this a count of dma-mappings used to map the use_sg
217 * pages. They are not guaranteed to be the same for those
218 * architectures that implement an IOMMU.
219 */
220 sgel = (struct scatterlist *)scsi_cmnd->request_buffer;
221 lpfc_cmd->seg_cnt = dma_map_sg(&phba->pcidev->dev, sgel,
222 scsi_cmnd->use_sg, datadir);
223 if (lpfc_cmd->seg_cnt == 0)
224 return 1;
225
226 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
227 printk(KERN_ERR "%s: Too many sg segments from "
228 "dma_map_sg. Config %d, seg_cnt %d",
229 __FUNCTION__, phba->cfg_sg_seg_cnt,
230 lpfc_cmd->seg_cnt);
231 dma_unmap_sg(&phba->pcidev->dev, sgel,
232 lpfc_cmd->seg_cnt, datadir);
233 return 1;
234 }
235
236 /*
237 * The driver established a maximum scatter-gather segment count
238 * during probe that limits the number of sg elements in any
239 * single scsi command. Just run through the seg_cnt and format
240 * the bde's.
241 */
242 for (i = 0; i < lpfc_cmd->seg_cnt; i++) {
243 physaddr = sg_dma_address(sgel);
244 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
245 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
246 bpl->tus.f.bdeSize = sg_dma_len(sgel);
247 if (datadir == DMA_TO_DEVICE)
248 bpl->tus.f.bdeFlags = 0;
249 else
250 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
251 bpl->tus.w = le32_to_cpu(bpl->tus.w);
252 bpl++;
253 sgel++;
254 num_bde++;
255 }
256 } else if (scsi_cmnd->request_buffer && scsi_cmnd->request_bufflen) {
257 physaddr = dma_map_single(&phba->pcidev->dev,
258 scsi_cmnd->request_buffer,
259 scsi_cmnd->request_bufflen,
260 datadir);
261 dma_error = dma_mapping_error(physaddr);
262 if (dma_error) {
263 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
264 "%d:0718 Unable to dma_map_single "
265 "request_buffer: x%x\n",
266 phba->brd_no, dma_error);
267 return 1;
268 }
269
270 lpfc_cmd->nonsg_phys = physaddr;
271 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
272 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
273 bpl->tus.f.bdeSize = scsi_cmnd->request_bufflen;
274 if (datadir == DMA_TO_DEVICE)
275 bpl->tus.f.bdeFlags = 0;
483f05f0
JSEC
276 else
277 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
dea3101e 278 bpl->tus.w = le32_to_cpu(bpl->tus.w);
279 num_bde = 1;
280 bpl++;
281 }
282
283 /*
284 * Finish initializing those IOCB fields that are dependent on the
483f05f0
JSEC
285 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
286 * reinitialized since all iocb memory resources are used many times
287 * for transmit, receive, and continuation bpl's.
dea3101e 288 */
483f05f0 289 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
dea3101e 290 iocb_cmd->un.fcpi64.bdl.bdeSize +=
291 (num_bde * sizeof (struct ulp_bde64));
292 iocb_cmd->ulpBdeCount = 1;
293 iocb_cmd->ulpLe = 1;
294 fcp_cmnd->fcpDl = be32_to_cpu(scsi_cmnd->request_bufflen);
295 return 0;
296}
297
298static void
299lpfc_handle_fcp_err(struct lpfc_scsi_buf *lpfc_cmd)
300{
301 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
302 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
303 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
304 struct lpfc_hba *phba = lpfc_cmd->scsi_hba;
305 uint32_t fcpi_parm = lpfc_cmd->cur_iocbq.iocb.un.fcpi.fcpi_parm;
306 uint32_t resp_info = fcprsp->rspStatus2;
307 uint32_t scsi_status = fcprsp->rspStatus3;
308 uint32_t host_status = DID_OK;
309 uint32_t rsplen = 0;
310
311 /*
312 * If this is a task management command, there is no
313 * scsi packet associated with this lpfc_cmd. The driver
314 * consumes it.
315 */
316 if (fcpcmd->fcpCntl2) {
317 scsi_status = 0;
318 goto out;
319 }
320
321 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
322 "%d:0730 FCP command failed: RSP "
323 "Data: x%x x%x x%x x%x x%x x%x\n",
324 phba->brd_no, resp_info, scsi_status,
325 be32_to_cpu(fcprsp->rspResId),
326 be32_to_cpu(fcprsp->rspSnsLen),
327 be32_to_cpu(fcprsp->rspRspLen),
328 fcprsp->rspInfo3);
329
330 if (resp_info & RSP_LEN_VALID) {
331 rsplen = be32_to_cpu(fcprsp->rspRspLen);
332 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
333 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
334 host_status = DID_ERROR;
335 goto out;
336 }
337 }
338
339 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
340 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
341 if (snslen > SCSI_SENSE_BUFFERSIZE)
342 snslen = SCSI_SENSE_BUFFERSIZE;
343
344 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
345 }
346
347 cmnd->resid = 0;
348 if (resp_info & RESID_UNDER) {
349 cmnd->resid = be32_to_cpu(fcprsp->rspResId);
350
351 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
352 "%d:0716 FCP Read Underrun, expected %d, "
353 "residual %d Data: x%x x%x x%x\n", phba->brd_no,
354 be32_to_cpu(fcpcmd->fcpDl), cmnd->resid,
355 fcpi_parm, cmnd->cmnd[0], cmnd->underflow);
356
357 /*
358 * The cmnd->underflow is the minimum number of bytes that must
359 * be transfered for this command. Provided a sense condition
360 * is not present, make sure the actual amount transferred is at
361 * least the underflow value or fail.
362 */
363 if (!(resp_info & SNS_LEN_VALID) &&
364 (scsi_status == SAM_STAT_GOOD) &&
365 (cmnd->request_bufflen - cmnd->resid) < cmnd->underflow) {
366 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
367 "%d:0717 FCP command x%x residual "
368 "underrun converted to error "
369 "Data: x%x x%x x%x\n", phba->brd_no,
370 cmnd->cmnd[0], cmnd->request_bufflen,
371 cmnd->resid, cmnd->underflow);
372
373 host_status = DID_ERROR;
374 }
375 } else if (resp_info & RESID_OVER) {
376 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
377 "%d:0720 FCP command x%x residual "
378 "overrun error. Data: x%x x%x \n",
379 phba->brd_no, cmnd->cmnd[0],
380 cmnd->request_bufflen, cmnd->resid);
381 host_status = DID_ERROR;
382
383 /*
384 * Check SLI validation that all the transfer was actually done
385 * (fcpi_parm should be zero). Apply check only to reads.
386 */
387 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
388 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
389 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
390 "%d:0734 FCP Read Check Error Data: "
391 "x%x x%x x%x x%x\n", phba->brd_no,
392 be32_to_cpu(fcpcmd->fcpDl),
393 be32_to_cpu(fcprsp->rspResId),
394 fcpi_parm, cmnd->cmnd[0]);
395 host_status = DID_ERROR;
396 cmnd->resid = cmnd->request_bufflen;
397 }
398
399 out:
400 cmnd->result = ScsiResult(host_status, scsi_status);
401}
402
403static void
404lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
405 struct lpfc_iocbq *pIocbOut)
406{
407 struct lpfc_scsi_buf *lpfc_cmd =
408 (struct lpfc_scsi_buf *) pIocbIn->context1;
409 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
410 struct lpfc_nodelist *pnode = rdata->pnode;
411 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
445cf4f4
JSEC
412 int result;
413 struct scsi_device *sdev, *tmp_sdev;
414 int depth = 0;
dea3101e 415
416 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
417 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
418
419 if (lpfc_cmd->status) {
420 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
421 (lpfc_cmd->result & IOERR_DRVR_MASK))
422 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
423 else if (lpfc_cmd->status >= IOSTAT_CNT)
424 lpfc_cmd->status = IOSTAT_DEFAULT;
425
426 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
427 "%d:0729 FCP cmd x%x failed <%d/%d> status: "
428 "x%x result: x%x Data: x%x x%x\n",
429 phba->brd_no, cmd->cmnd[0], cmd->device->id,
430 cmd->device->lun, lpfc_cmd->status,
431 lpfc_cmd->result, pIocbOut->iocb.ulpContext,
432 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
433
434 switch (lpfc_cmd->status) {
435 case IOSTAT_FCP_RSP_ERROR:
436 /* Call FCP RSP handler to determine result */
437 lpfc_handle_fcp_err(lpfc_cmd);
438 break;
439 case IOSTAT_NPORT_BSY:
440 case IOSTAT_FABRIC_BSY:
441 cmd->result = ScsiResult(DID_BUS_BUSY, 0);
442 break;
443 default:
444 cmd->result = ScsiResult(DID_ERROR, 0);
445 break;
446 }
447
19a7b4ae
JSEC
448 if ((pnode == NULL )
449 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
450 cmd->result = ScsiResult(DID_BUS_BUSY, SAM_STAT_BUSY);
dea3101e 451 } else {
452 cmd->result = ScsiResult(DID_OK, 0);
453 }
454
455 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
456 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
457
458 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
459 "%d:0710 Iodone <%d/%d> cmd %p, error x%x "
460 "SNS x%x x%x Data: x%x x%x\n",
461 phba->brd_no, cmd->device->id,
462 cmd->device->lun, cmd, cmd->result,
463 *lp, *(lp + 3), cmd->retries, cmd->resid);
464 }
465
445cf4f4
JSEC
466 result = cmd->result;
467 sdev = cmd->device;
0bd4ca25
JSEC
468 cmd->scsi_done(cmd);
469
0c6ac8ef 470 if (!result && pnode != NULL &&
445cf4f4
JSEC
471 ((jiffies - pnode->last_ramp_up_time) >
472 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
473 ((jiffies - pnode->last_q_full_time) >
474 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
475 (phba->cfg_lun_queue_depth > sdev->queue_depth)) {
476 shost_for_each_device(tmp_sdev, sdev->host) {
477 if (phba->cfg_lun_queue_depth > tmp_sdev->queue_depth) {
478 if (tmp_sdev->id != sdev->id)
479 continue;
480 if (tmp_sdev->ordered_tags)
481 scsi_adjust_queue_depth(tmp_sdev,
482 MSG_ORDERED_TAG,
483 tmp_sdev->queue_depth+1);
484 else
485 scsi_adjust_queue_depth(tmp_sdev,
486 MSG_SIMPLE_TAG,
487 tmp_sdev->queue_depth+1);
488
489 pnode->last_ramp_up_time = jiffies;
490 }
491 }
492 }
493
494 /*
495 * Check for queue full. If the lun is reporting queue full, then
496 * back off the lun queue depth to prevent target overloads.
497 */
0c6ac8ef 498 if (result == SAM_STAT_TASK_SET_FULL && pnode != NULL) {
445cf4f4
JSEC
499 pnode->last_q_full_time = jiffies;
500
501 shost_for_each_device(tmp_sdev, sdev->host) {
502 if (tmp_sdev->id != sdev->id)
503 continue;
504 depth = scsi_track_queue_full(tmp_sdev,
505 tmp_sdev->queue_depth - 1);
506 }
507 /*
508 * The queue depth cannot be lowered any more.
509 * Modify the returned error code to store
510 * the final depth value set by
511 * scsi_track_queue_full.
512 */
513 if (depth == -1)
514 depth = sdev->host->cmd_per_lun;
515
516 if (depth) {
517 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
518 "%d:0711 detected queue full - lun queue depth "
519 " adjusted to %d.\n", phba->brd_no, depth);
520 }
521 }
522
0bd4ca25 523 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea3101e 524}
525
526static void
527lpfc_scsi_prep_cmnd(struct lpfc_hba * phba, struct lpfc_scsi_buf * lpfc_cmd,
528 struct lpfc_nodelist *pnode)
529{
530 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
531 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
532 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
533 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
534 int datadir = scsi_cmnd->sc_data_direction;
535
536 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
69859dc4
JSEC
537 /* clear task management bits */
538 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea3101e 539
91886523
JSEC
540 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
541 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea3101e 542
543 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
544
545 if (scsi_cmnd->device->tagged_supported) {
546 switch (scsi_cmnd->tag) {
547 case HEAD_OF_QUEUE_TAG:
548 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
549 break;
550 case ORDERED_QUEUE_TAG:
551 fcp_cmnd->fcpCntl1 = ORDERED_Q;
552 break;
553 default:
554 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
555 break;
556 }
557 } else
558 fcp_cmnd->fcpCntl1 = 0;
559
560 /*
561 * There are three possibilities here - use scatter-gather segment, use
562 * the single mapping, or neither. Start the lpfc command prep by
563 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
564 * data bde entry.
565 */
566 if (scsi_cmnd->use_sg) {
567 if (datadir == DMA_TO_DEVICE) {
568 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
569 iocb_cmd->un.fcpi.fcpi_parm = 0;
570 iocb_cmd->ulpPU = 0;
571 fcp_cmnd->fcpCntl3 = WRITE_DATA;
572 phba->fc4OutputRequests++;
573 } else {
574 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
575 iocb_cmd->ulpPU = PARM_READ_CHECK;
576 iocb_cmd->un.fcpi.fcpi_parm =
577 scsi_cmnd->request_bufflen;
578 fcp_cmnd->fcpCntl3 = READ_DATA;
579 phba->fc4InputRequests++;
580 }
581 } else if (scsi_cmnd->request_buffer && scsi_cmnd->request_bufflen) {
582 if (datadir == DMA_TO_DEVICE) {
583 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
584 iocb_cmd->un.fcpi.fcpi_parm = 0;
585 iocb_cmd->ulpPU = 0;
586 fcp_cmnd->fcpCntl3 = WRITE_DATA;
587 phba->fc4OutputRequests++;
588 } else {
589 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
590 iocb_cmd->ulpPU = PARM_READ_CHECK;
591 iocb_cmd->un.fcpi.fcpi_parm =
592 scsi_cmnd->request_bufflen;
593 fcp_cmnd->fcpCntl3 = READ_DATA;
594 phba->fc4InputRequests++;
595 }
596 } else {
597 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
598 iocb_cmd->un.fcpi.fcpi_parm = 0;
599 iocb_cmd->ulpPU = 0;
600 fcp_cmnd->fcpCntl3 = 0;
601 phba->fc4ControlRequests++;
602 }
603
604 /*
605 * Finish initializing those IOCB fields that are independent
606 * of the scsi_cmnd request_buffer
607 */
608 piocbq->iocb.ulpContext = pnode->nlp_rpi;
609 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
610 piocbq->iocb.ulpFCP2Rcvy = 1;
611
612 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
613 piocbq->context1 = lpfc_cmd;
614 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
615 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
616}
617
618static int
619lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_hba *phba,
620 struct lpfc_scsi_buf *lpfc_cmd,
621 uint8_t task_mgmt_cmd)
622{
623 struct lpfc_sli *psli;
624 struct lpfc_iocbq *piocbq;
625 IOCB_t *piocb;
626 struct fcp_cmnd *fcp_cmnd;
627 struct scsi_device *scsi_dev = lpfc_cmd->pCmd->device;
628 struct lpfc_rport_data *rdata = scsi_dev->hostdata;
629 struct lpfc_nodelist *ndlp = rdata->pnode;
630
19a7b4ae 631 if ((ndlp == NULL) || (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
dea3101e 632 return 0;
633 }
634
635 psli = &phba->sli;
636 piocbq = &(lpfc_cmd->cur_iocbq);
637 piocb = &piocbq->iocb;
638
639 fcp_cmnd = lpfc_cmd->fcp_cmnd;
91886523
JSEC
640 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
641 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea3101e 642 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
643
644 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
645
646 piocb->ulpContext = ndlp->nlp_rpi;
647 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
648 piocb->ulpFCP2Rcvy = 1;
649 }
650 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
651
652 /* ulpTimeout is only one byte */
653 if (lpfc_cmd->timeout > 0xff) {
654 /*
655 * Do not timeout the command at the firmware level.
656 * The driver will provide the timeout mechanism.
657 */
658 piocb->ulpTimeout = 0;
659 } else {
660 piocb->ulpTimeout = lpfc_cmd->timeout;
661 }
662
663 lpfc_cmd->rdata = rdata;
664
665 switch (task_mgmt_cmd) {
666 case FCP_LUN_RESET:
667 /* Issue LUN Reset to TGT <num> LUN <num> */
668 lpfc_printf_log(phba,
669 KERN_INFO,
670 LOG_FCP,
671 "%d:0703 Issue LUN Reset to TGT %d LUN %d "
672 "Data: x%x x%x\n",
673 phba->brd_no,
674 scsi_dev->id, scsi_dev->lun,
675 ndlp->nlp_rpi, ndlp->nlp_flag);
676
677 break;
678 case FCP_ABORT_TASK_SET:
679 /* Issue Abort Task Set to TGT <num> LUN <num> */
680 lpfc_printf_log(phba,
681 KERN_INFO,
682 LOG_FCP,
683 "%d:0701 Issue Abort Task Set to TGT %d LUN %d "
684 "Data: x%x x%x\n",
685 phba->brd_no,
686 scsi_dev->id, scsi_dev->lun,
687 ndlp->nlp_rpi, ndlp->nlp_flag);
688
689 break;
690 case FCP_TARGET_RESET:
691 /* Issue Target Reset to TGT <num> */
692 lpfc_printf_log(phba,
693 KERN_INFO,
694 LOG_FCP,
695 "%d:0702 Issue Target Reset to TGT %d "
696 "Data: x%x x%x\n",
697 phba->brd_no,
698 scsi_dev->id, ndlp->nlp_rpi,
699 ndlp->nlp_flag);
700 break;
701 }
702
703 return (1);
704}
705
706static int
707lpfc_scsi_tgt_reset(struct lpfc_scsi_buf * lpfc_cmd, struct lpfc_hba * phba)
708{
709 struct lpfc_iocbq *iocbq;
0bd4ca25 710 struct lpfc_iocbq *iocbqrsp;
dea3101e 711 int ret;
712
713 ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, FCP_TARGET_RESET);
714 if (!ret)
715 return FAILED;
716
717 lpfc_cmd->scsi_hba = phba;
718 iocbq = &lpfc_cmd->cur_iocbq;
0bd4ca25
JSEC
719 iocbqrsp = lpfc_sli_get_iocbq(phba);
720
dea3101e 721 if (!iocbqrsp)
722 return FAILED;
dea3101e 723
68876920
JSEC
724 ret = lpfc_sli_issue_iocb_wait(phba,
725 &phba->sli.ring[phba->sli.fcp_ring],
726 iocbq, iocbqrsp, lpfc_cmd->timeout);
dea3101e 727 if (ret != IOCB_SUCCESS) {
728 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
729 ret = FAILED;
730 } else {
731 ret = SUCCESS;
732 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
733 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
734 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
735 (lpfc_cmd->result & IOERR_DRVR_MASK))
736 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
737 }
738
604a3e30 739 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea3101e 740 return ret;
741}
742
dea3101e 743const char *
744lpfc_info(struct Scsi_Host *host)
745{
7f0b5b19 746 struct lpfc_hba *phba = (struct lpfc_hba *) host->hostdata;
dea3101e 747 int len;
748 static char lpfcinfobuf[384];
749
750 memset(lpfcinfobuf,0,384);
751 if (phba && phba->pcidev){
752 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
753 len = strlen(lpfcinfobuf);
754 snprintf(lpfcinfobuf + len,
755 384-len,
756 " on PCI bus %02x device %02x irq %d",
757 phba->pcidev->bus->number,
758 phba->pcidev->devfn,
759 phba->pcidev->irq);
760 len = strlen(lpfcinfobuf);
761 if (phba->Port[0]) {
762 snprintf(lpfcinfobuf + len,
763 384-len,
764 " port %s",
765 phba->Port);
766 }
767 }
768 return lpfcinfobuf;
769}
770
875fbdfe
JSEC
771static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
772{
773 unsigned long poll_tmo_expires =
774 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
775
776 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
777 mod_timer(&phba->fcp_poll_timer,
778 poll_tmo_expires);
779}
780
781void lpfc_poll_start_timer(struct lpfc_hba * phba)
782{
783 lpfc_poll_rearm_timer(phba);
784}
785
786void lpfc_poll_timeout(unsigned long ptr)
787{
788 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
789 unsigned long iflag;
790
791 spin_lock_irqsave(phba->host->host_lock, iflag);
792
793 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
794 lpfc_sli_poll_fcp_ring (phba);
795 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
796 lpfc_poll_rearm_timer(phba);
797 }
798
799 spin_unlock_irqrestore(phba->host->host_lock, iflag);
800}
801
dea3101e 802static int
803lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
804{
805 struct lpfc_hba *phba =
7f0b5b19 806 (struct lpfc_hba *) cmnd->device->host->hostdata;
dea3101e 807 struct lpfc_sli *psli = &phba->sli;
808 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
809 struct lpfc_nodelist *ndlp = rdata->pnode;
0bd4ca25 810 struct lpfc_scsi_buf *lpfc_cmd;
19a7b4ae 811 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
19a7b4ae 812 int err;
dea3101e 813
19a7b4ae
JSEC
814 err = fc_remote_port_chkready(rport);
815 if (err) {
816 cmnd->result = err;
dea3101e 817 goto out_fail_command;
818 }
819
820 /*
19a7b4ae
JSEC
821 * Catch race where our node has transitioned, but the
822 * transport is still transitioning.
dea3101e 823 */
19a7b4ae
JSEC
824 if (!ndlp) {
825 cmnd->result = ScsiResult(DID_BUS_BUSY, 0);
826 goto out_fail_command;
dea3101e 827 }
875fbdfe 828 lpfc_cmd = lpfc_get_scsi_buf (phba);
dea3101e 829 if (lpfc_cmd == NULL) {
1de933f3
JSEC
830 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
831 "%d:0707 driver's buffer pool is empty, "
832 "IO busied\n", phba->brd_no);
dea3101e 833 goto out_host_busy;
834 }
835
836 /*
837 * Store the midlayer's command structure for the completion phase
838 * and complete the command initialization.
839 */
840 lpfc_cmd->pCmd = cmnd;
841 lpfc_cmd->rdata = rdata;
842 lpfc_cmd->timeout = 0;
843 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
844 cmnd->scsi_done = done;
845
846 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
847 if (err)
848 goto out_host_busy_free_buf;
849
850 lpfc_scsi_prep_cmnd(phba, lpfc_cmd, ndlp);
851
852 err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
853 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
854 if (err)
855 goto out_host_busy_free_buf;
875fbdfe
JSEC
856
857 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
858 lpfc_sli_poll_fcp_ring(phba);
859 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
860 lpfc_poll_rearm_timer(phba);
861 }
862
dea3101e 863 return 0;
864
865 out_host_busy_free_buf:
0bd4ca25 866 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea3101e 867 out_host_busy:
868 return SCSI_MLQUEUE_HOST_BUSY;
869
870 out_fail_command:
871 done(cmnd);
872 return 0;
873}
874
63c59c3b 875
dea3101e 876static int
63c59c3b 877lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea3101e 878{
63c59c3b 879 struct Scsi_Host *shost = cmnd->device->host;
7f0b5b19 880 struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata;
dea3101e 881 struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
0bd4ca25
JSEC
882 struct lpfc_iocbq *iocb;
883 struct lpfc_iocbq *abtsiocb;
dea3101e 884 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 885 IOCB_t *cmd, *icmd;
dea3101e 886 unsigned int loop_count = 0;
0bd4ca25 887 int ret = SUCCESS;
dea3101e 888
6175c02a 889 lpfc_block_requests(phba);
63c59c3b 890 spin_lock_irq(shost->host_lock);
dea3101e 891
0bd4ca25
JSEC
892 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
893 BUG_ON(!lpfc_cmd);
dea3101e 894
0bd4ca25
JSEC
895 /*
896 * If pCmd field of the corresponding lpfc_scsi_buf structure
897 * points to a different SCSI command, then the driver has
898 * already completed this command, but the midlayer did not
899 * see the completion before the eh fired. Just return
900 * SUCCESS.
901 */
902 iocb = &lpfc_cmd->cur_iocbq;
903 if (lpfc_cmd->pCmd != cmnd)
904 goto out;
dea3101e 905
0bd4ca25 906 BUG_ON(iocb->context1 != lpfc_cmd);
dea3101e 907
0bd4ca25
JSEC
908 abtsiocb = lpfc_sli_get_iocbq(phba);
909 if (abtsiocb == NULL) {
910 ret = FAILED;
dea3101e 911 goto out;
912 }
913
dea3101e 914 /*
0bd4ca25
JSEC
915 * The scsi command can not be in txq and it is in flight because the
916 * pCmd is still pointig at the SCSI command we have to abort. There
917 * is no need to search the txcmplq. Just send an abort to the FW.
dea3101e 918 */
dea3101e 919
0bd4ca25
JSEC
920 cmd = &iocb->iocb;
921 icmd = &abtsiocb->iocb;
922 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
923 icmd->un.acxri.abortContextTag = cmd->ulpContext;
924 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea3101e 925
0bd4ca25
JSEC
926 icmd->ulpLe = 1;
927 icmd->ulpClass = cmd->ulpClass;
928 if (phba->hba_state >= LPFC_LINK_UP)
929 icmd->ulpCommand = CMD_ABORT_XRI_CN;
930 else
931 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea3101e 932
0bd4ca25
JSEC
933 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
934 if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
935 lpfc_sli_release_iocbq(phba, abtsiocb);
936 ret = FAILED;
937 goto out;
938 }
dea3101e 939
875fbdfe
JSEC
940 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
941 lpfc_sli_poll_fcp_ring (phba);
942
0bd4ca25
JSEC
943 /* Wait for abort to complete */
944 while (lpfc_cmd->pCmd == cmnd)
945 {
875fbdfe
JSEC
946 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
947 lpfc_sli_poll_fcp_ring (phba);
948
0bd4ca25 949 spin_unlock_irq(phba->host->host_lock);
a9a3047d 950 schedule_timeout_uninterruptible(LPFC_ABORT_WAIT*HZ);
0bd4ca25
JSEC
951 spin_lock_irq(phba->host->host_lock);
952 if (++loop_count
953 > (2 * phba->cfg_nodev_tmo)/LPFC_ABORT_WAIT)
954 break;
955 }
dea3101e 956
0bd4ca25
JSEC
957 if (lpfc_cmd->pCmd == cmnd) {
958 ret = FAILED;
959 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
960 "%d:0748 abort handler timed out waiting for "
961 "abort to complete: ret %#x, ID %d, LUN %d, "
962 "snum %#lx\n",
963 phba->brd_no, ret, cmnd->device->id,
964 cmnd->device->lun, cmnd->serial_number);
dea3101e 965 }
966
967 out:
968 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
1de933f3
JSEC
969 "%d:0749 SCSI Layer I/O Abort Request "
970 "Status x%x ID %d LUN %d snum %#lx\n",
0bd4ca25
JSEC
971 phba->brd_no, ret, cmnd->device->id,
972 cmnd->device->lun, cmnd->serial_number);
dea3101e 973
63c59c3b 974 spin_unlock_irq(shost->host_lock);
6175c02a 975 lpfc_unblock_requests(phba);
dea3101e 976
63c59c3b 977 return ret;
8fa728a2
JG
978}
979
dea3101e 980static int
63c59c3b 981lpfc_reset_lun_handler(struct scsi_cmnd *cmnd)
dea3101e 982{
983 struct Scsi_Host *shost = cmnd->device->host;
7f0b5b19 984 struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata;
0bd4ca25
JSEC
985 struct lpfc_scsi_buf *lpfc_cmd;
986 struct lpfc_iocbq *iocbq, *iocbqrsp;
dea3101e 987 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
988 struct lpfc_nodelist *pnode = rdata->pnode;
6175c02a 989 uint32_t cmd_result = 0, cmd_status = 0;
dea3101e 990 int ret = FAILED;
991 int cnt, loopcnt;
992
6175c02a 993 lpfc_block_requests(phba);
63c59c3b 994 spin_lock_irq(shost->host_lock);
dea3101e 995 /*
996 * If target is not in a MAPPED state, delay the reset until
997 * target is rediscovered or nodev timeout expires.
998 */
999 while ( 1 ) {
1000 if (!pnode)
1001 break;
1002
1003 if (pnode->nlp_state != NLP_STE_MAPPED_NODE) {
1004 spin_unlock_irq(phba->host->host_lock);
a9a3047d 1005 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
dea3101e 1006 spin_lock_irq(phba->host->host_lock);
1007 }
1008 if ((pnode) && (pnode->nlp_state == NLP_STE_MAPPED_NODE))
1009 break;
1010 }
1011
875fbdfe 1012 lpfc_cmd = lpfc_get_scsi_buf (phba);
dea3101e 1013 if (lpfc_cmd == NULL)
1014 goto out;
1015
1016 lpfc_cmd->pCmd = cmnd;
1017 lpfc_cmd->timeout = 60;
1018 lpfc_cmd->scsi_hba = phba;
1019
1020 ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, FCP_LUN_RESET);
1021 if (!ret)
1022 goto out_free_scsi_buf;
1023
1024 iocbq = &lpfc_cmd->cur_iocbq;
1025
1026 /* get a buffer for this IOCB command response */
0bd4ca25 1027 iocbqrsp = lpfc_sli_get_iocbq(phba);
dea3101e 1028 if (iocbqrsp == NULL)
1029 goto out_free_scsi_buf;
1030
68876920
JSEC
1031 ret = lpfc_sli_issue_iocb_wait(phba,
1032 &phba->sli.ring[phba->sli.fcp_ring],
1033 iocbq, iocbqrsp, lpfc_cmd->timeout);
dea3101e 1034 if (ret == IOCB_SUCCESS)
1035 ret = SUCCESS;
1036
6175c02a
JSEC
1037
1038 cmd_result = iocbqrsp->iocb.un.ulpWord[4];
1039 cmd_status = iocbqrsp->iocb.ulpStatus;
1040
1041 lpfc_sli_release_iocbq(phba, iocbqrsp);
1042 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea3101e 1043
1044 /*
6175c02a 1045 * All outstanding txcmplq I/Os should have been aborted by the device.
dea3101e 1046 * Unfortunately, some targets do not abide by this forcing the driver
1047 * to double check.
1048 */
6175c02a
JSEC
1049 cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1050 cmnd->device->id, cmnd->device->lun,
1051 LPFC_CTX_LUN);
1052 if (cnt)
1053 lpfc_sli_abort_iocb(phba,
1054 &phba->sli.ring[phba->sli.fcp_ring],
1055 cmnd->device->id, cmnd->device->lun,
1056 0, LPFC_CTX_LUN);
dea3101e 1057 loopcnt = 0;
6175c02a 1058 while(cnt) {
dea3101e 1059 spin_unlock_irq(phba->host->host_lock);
a9a3047d 1060 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
dea3101e 1061 spin_lock_irq(phba->host->host_lock);
1062
1063 if (++loopcnt
1064 > (2 * phba->cfg_nodev_tmo)/LPFC_RESET_WAIT)
1065 break;
6175c02a
JSEC
1066
1067 cnt = lpfc_sli_sum_iocb(phba,
1068 &phba->sli.ring[phba->sli.fcp_ring],
1069 cmnd->device->id, cmnd->device->lun,
1070 LPFC_CTX_LUN);
dea3101e 1071 }
1072
1073 if (cnt) {
0bd4ca25 1074 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
dea3101e 1075 "%d:0719 LUN Reset I/O flush failure: cnt x%x\n",
1076 phba->brd_no, cnt);
0bd4ca25 1077 ret = FAILED;
dea3101e 1078 }
1079
dea3101e 1080out_free_scsi_buf:
1081 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1082 "%d:0713 SCSI layer issued LUN reset (%d, %d) "
1083 "Data: x%x x%x x%x\n",
6175c02a
JSEC
1084 phba->brd_no, cmnd->device->id,cmnd->device->lun,
1085 ret, cmd_status, cmd_result);
1086
dea3101e 1087out:
63c59c3b 1088 spin_unlock_irq(shost->host_lock);
6175c02a 1089 lpfc_unblock_requests(phba);
dea3101e 1090 return ret;
1091}
1092
94d0e7b8 1093static int
63c59c3b 1094lpfc_reset_bus_handler(struct scsi_cmnd *cmnd)
dea3101e 1095{
1096 struct Scsi_Host *shost = cmnd->device->host;
7f0b5b19 1097 struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata;
dea3101e 1098 struct lpfc_nodelist *ndlp = NULL;
1099 int match;
1100 int ret = FAILED, i, err_count = 0;
1101 int cnt, loopcnt;
1102 unsigned int midlayer_id = 0;
0bd4ca25 1103 struct lpfc_scsi_buf * lpfc_cmd;
dea3101e 1104
6175c02a 1105 lpfc_block_requests(phba);
63c59c3b
JSEC
1106 spin_lock_irq(shost->host_lock);
1107
875fbdfe 1108 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea3101e 1109 if (lpfc_cmd == NULL)
1110 goto out;
1111
1112 /* The lpfc_cmd storage is reused. Set all loop invariants. */
1113 lpfc_cmd->timeout = 60;
1114 lpfc_cmd->pCmd = cmnd;
1115 lpfc_cmd->scsi_hba = phba;
1116
1117 /*
1118 * Since the driver manages a single bus device, reset all
1119 * targets known to the driver. Should any target reset
1120 * fail, this routine returns failure to the midlayer.
1121 */
1122 midlayer_id = cmnd->device->id;
1123 for (i = 0; i < MAX_FCP_TARGET; i++) {
1124 /* Search the mapped list for this target ID */
1125 match = 0;
1126 list_for_each_entry(ndlp, &phba->fc_nlpmap_list, nlp_listp) {
1127 if ((i == ndlp->nlp_sid) && ndlp->rport) {
1128 match = 1;
1129 break;
1130 }
1131 }
1132 if (!match)
1133 continue;
1134
1135 lpfc_cmd->pCmd->device->id = i;
1136 lpfc_cmd->pCmd->device->hostdata = ndlp->rport->dd_data;
1137 ret = lpfc_scsi_tgt_reset(lpfc_cmd, phba);
1138 if (ret != SUCCESS) {
6175c02a 1139 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
dea3101e 1140 "%d:0713 Bus Reset on target %d failed\n",
1141 phba->brd_no, i);
1142 err_count++;
1143 }
1144 }
1145
6175c02a
JSEC
1146 if (err_count == 0)
1147 ret = SUCCESS;
1148
1149 lpfc_release_scsi_buf(phba, lpfc_cmd);
1150
1151 /*
1152 * All outstanding txcmplq I/Os should have been aborted by
1153 * the targets. Unfortunately, some targets do not abide by
1154 * this forcing the driver to double check.
1155 */
dea3101e 1156 cmnd->device->id = midlayer_id;
6175c02a
JSEC
1157 cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1158 0, 0, LPFC_CTX_HOST);
1159 if (cnt)
1160 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1161 0, 0, 0, LPFC_CTX_HOST);
dea3101e 1162 loopcnt = 0;
6175c02a 1163 while(cnt) {
dea3101e 1164 spin_unlock_irq(phba->host->host_lock);
a9a3047d 1165 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
dea3101e 1166 spin_lock_irq(phba->host->host_lock);
1167
1168 if (++loopcnt
1169 > (2 * phba->cfg_nodev_tmo)/LPFC_RESET_WAIT)
1170 break;
6175c02a
JSEC
1171
1172 cnt = lpfc_sli_sum_iocb(phba,
1173 &phba->sli.ring[phba->sli.fcp_ring],
1174 0, 0, LPFC_CTX_HOST);
dea3101e 1175 }
1176
1177 if (cnt) {
6175c02a 1178 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
dea3101e 1179 "%d:0715 Bus Reset I/O flush failure: cnt x%x left x%x\n",
1180 phba->brd_no, cnt, i);
0bd4ca25 1181 ret = FAILED;
6175c02a 1182 }
dea3101e 1183
dea3101e 1184 lpfc_printf_log(phba,
1185 KERN_ERR,
1186 LOG_FCP,
1187 "%d:0714 SCSI layer issued Bus Reset Data: x%x\n",
1188 phba->brd_no, ret);
1189out:
63c59c3b 1190 spin_unlock_irq(shost->host_lock);
6175c02a 1191 lpfc_unblock_requests(phba);
dea3101e 1192 return ret;
1193}
1194
1195static int
1196lpfc_slave_alloc(struct scsi_device *sdev)
1197{
7f0b5b19 1198 struct lpfc_hba *phba = (struct lpfc_hba *)sdev->host->hostdata;
dea3101e 1199 struct lpfc_scsi_buf *scsi_buf = NULL;
19a7b4ae 1200 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
dea3101e 1201 uint32_t total = 0, i;
1202 uint32_t num_to_alloc = 0;
1203 unsigned long flags;
dea3101e 1204
19a7b4ae 1205 if (!rport || fc_remote_port_chkready(rport))
dea3101e 1206 return -ENXIO;
1207
19a7b4ae 1208 sdev->hostdata = rport->dd_data;
dea3101e 1209
1210 /*
1211 * Populate the cmds_per_lun count scsi_bufs into this host's globally
1212 * available list of scsi buffers. Don't allocate more than the
a784efbf
JSEC
1213 * HBA limit conveyed to the midlayer via the host structure. The
1214 * formula accounts for the lun_queue_depth + error handlers + 1
1215 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea3101e 1216 */
1217 total = phba->total_scsi_bufs;
a784efbf 1218 num_to_alloc = phba->cfg_lun_queue_depth + 2;
dea3101e 1219 if (total >= phba->cfg_hba_queue_depth) {
a784efbf
JSEC
1220 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
1221 "%d:0704 At limitation of %d preallocated "
1222 "command buffers\n", phba->brd_no, total);
dea3101e 1223 return 0;
1224 } else if (total + num_to_alloc > phba->cfg_hba_queue_depth) {
a784efbf
JSEC
1225 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
1226 "%d:0705 Allocation request of %d command "
1227 "buffers will exceed max of %d. Reducing "
1228 "allocation request to %d.\n", phba->brd_no,
1229 num_to_alloc, phba->cfg_hba_queue_depth,
1230 (phba->cfg_hba_queue_depth - total));
dea3101e 1231 num_to_alloc = phba->cfg_hba_queue_depth - total;
1232 }
1233
1234 for (i = 0; i < num_to_alloc; i++) {
0bd4ca25 1235 scsi_buf = lpfc_new_scsi_buf(phba);
dea3101e 1236 if (!scsi_buf) {
a784efbf
JSEC
1237 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1238 "%d:0706 Failed to allocate command "
1239 "buffer\n", phba->brd_no);
dea3101e 1240 break;
1241 }
1242
875fbdfe 1243 spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
dea3101e 1244 phba->total_scsi_bufs++;
1245 list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
875fbdfe 1246 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
dea3101e 1247 }
1248 return 0;
1249}
1250
1251static int
1252lpfc_slave_configure(struct scsi_device *sdev)
1253{
7f0b5b19 1254 struct lpfc_hba *phba = (struct lpfc_hba *) sdev->host->hostdata;
dea3101e 1255 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
1256
1257 if (sdev->tagged_supported)
1258 scsi_activate_tcq(sdev, phba->cfg_lun_queue_depth);
1259 else
1260 scsi_deactivate_tcq(sdev, phba->cfg_lun_queue_depth);
1261
1262 /*
1263 * Initialize the fc transport attributes for the target
1264 * containing this scsi device. Also note that the driver's
1265 * target pointer is stored in the starget_data for the
1266 * driver's sysfs entry point functions.
1267 */
1268 rport->dev_loss_tmo = phba->cfg_nodev_tmo + 5;
1269
875fbdfe
JSEC
1270 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1271 lpfc_sli_poll_fcp_ring(phba);
1272 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1273 lpfc_poll_rearm_timer(phba);
1274 }
1275
dea3101e 1276 return 0;
1277}
1278
1279static void
1280lpfc_slave_destroy(struct scsi_device *sdev)
1281{
1282 sdev->hostdata = NULL;
1283 return;
1284}
1285
1286struct scsi_host_template lpfc_template = {
1287 .module = THIS_MODULE,
1288 .name = LPFC_DRIVER_NAME,
1289 .info = lpfc_info,
1290 .queuecommand = lpfc_queuecommand,
1291 .eh_abort_handler = lpfc_abort_handler,
1292 .eh_device_reset_handler= lpfc_reset_lun_handler,
1293 .eh_bus_reset_handler = lpfc_reset_bus_handler,
1294 .slave_alloc = lpfc_slave_alloc,
1295 .slave_configure = lpfc_slave_configure,
1296 .slave_destroy = lpfc_slave_destroy,
1297 .this_id = -1,
1298 .sg_tablesize = LPFC_SG_SEG_CNT,
1299 .cmd_per_lun = LPFC_CMD_PER_LUN,
1300 .use_clustering = ENABLE_CLUSTERING,
1301 .shost_attrs = lpfc_host_attrs,
564b2960 1302 .max_sectors = 0xFFFF,
dea3101e 1303};