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