lpfc_scsi.c 37 KB

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