lpfc_scsi.c 34 KB

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