lpfc_scsi.c 36 KB

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