lpfc_scsi.c 34 KB

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