lpfc_scsi.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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. set_current_state(TASK_UNINTERRUPTIBLE);
  733. schedule_timeout(LPFC_ABORT_WAIT*HZ);
  734. spin_lock_irq(phba->host->host_lock);
  735. if (++loop_count
  736. > (2 * phba->cfg_nodev_tmo)/LPFC_ABORT_WAIT)
  737. break;
  738. }
  739. if (lpfc_cmd->pCmd == cmnd) {
  740. ret = FAILED;
  741. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  742. "%d:0748 abort handler timed out waiting for "
  743. "abort to complete: ret %#x, ID %d, LUN %d, "
  744. "snum %#lx\n",
  745. phba->brd_no, ret, cmnd->device->id,
  746. cmnd->device->lun, cmnd->serial_number);
  747. }
  748. out:
  749. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
  750. "%d:0749 SCSI layer issued abort device: ret %#x, "
  751. "ID %d, LUN %d, snum %#lx\n",
  752. phba->brd_no, ret, cmnd->device->id,
  753. cmnd->device->lun, cmnd->serial_number);
  754. return ret;
  755. }
  756. static int
  757. lpfc_abort_handler(struct scsi_cmnd *cmnd)
  758. {
  759. int rc;
  760. spin_lock_irq(cmnd->device->host->host_lock);
  761. rc = __lpfc_abort_handler(cmnd);
  762. spin_unlock_irq(cmnd->device->host->host_lock);
  763. return rc;
  764. }
  765. static int
  766. __lpfc_reset_lun_handler(struct scsi_cmnd *cmnd)
  767. {
  768. struct Scsi_Host *shost = cmnd->device->host;
  769. struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata[0];
  770. struct lpfc_scsi_buf *lpfc_cmd;
  771. struct lpfc_iocbq *iocbq, *iocbqrsp;
  772. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  773. struct lpfc_nodelist *pnode = rdata->pnode;
  774. int ret = FAILED;
  775. int cnt, loopcnt;
  776. /*
  777. * If target is not in a MAPPED state, delay the reset until
  778. * target is rediscovered or nodev timeout expires.
  779. */
  780. while ( 1 ) {
  781. if (!pnode)
  782. break;
  783. if (pnode->nlp_state != NLP_STE_MAPPED_NODE) {
  784. spin_unlock_irq(phba->host->host_lock);
  785. set_current_state(TASK_UNINTERRUPTIBLE);
  786. schedule_timeout( HZ/2);
  787. spin_lock_irq(phba->host->host_lock);
  788. }
  789. if ((pnode) && (pnode->nlp_state == NLP_STE_MAPPED_NODE))
  790. break;
  791. }
  792. lpfc_cmd = lpfc_sli_get_scsi_buf (phba);
  793. if (lpfc_cmd == NULL)
  794. goto out;
  795. lpfc_cmd->pCmd = cmnd;
  796. lpfc_cmd->timeout = 60;
  797. lpfc_cmd->scsi_hba = phba;
  798. ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, FCP_LUN_RESET);
  799. if (!ret)
  800. goto out_free_scsi_buf;
  801. iocbq = &lpfc_cmd->cur_iocbq;
  802. /* get a buffer for this IOCB command response */
  803. iocbqrsp = lpfc_sli_get_iocbq(phba);
  804. if (iocbqrsp == NULL)
  805. goto out_free_scsi_buf;
  806. ret = lpfc_sli_issue_iocb_wait(phba,
  807. &phba->sli.ring[phba->sli.fcp_ring],
  808. iocbq, iocbqrsp, lpfc_cmd->timeout);
  809. if (ret == IOCB_SUCCESS)
  810. ret = SUCCESS;
  811. lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
  812. lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
  813. if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT)
  814. if (lpfc_cmd->result & IOERR_DRVR_MASK)
  815. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  816. /*
  817. * All outstanding txcmplq I/Os should have been aborted by the target.
  818. * Unfortunately, some targets do not abide by this forcing the driver
  819. * to double check.
  820. */
  821. lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
  822. cmnd->device->id, cmnd->device->lun, 0,
  823. LPFC_CTX_LUN);
  824. loopcnt = 0;
  825. while((cnt = lpfc_sli_sum_iocb(phba,
  826. &phba->sli.ring[phba->sli.fcp_ring],
  827. cmnd->device->id, cmnd->device->lun,
  828. LPFC_CTX_LUN))) {
  829. spin_unlock_irq(phba->host->host_lock);
  830. set_current_state(TASK_UNINTERRUPTIBLE);
  831. schedule_timeout(LPFC_RESET_WAIT*HZ);
  832. spin_lock_irq(phba->host->host_lock);
  833. if (++loopcnt
  834. > (2 * phba->cfg_nodev_tmo)/LPFC_RESET_WAIT)
  835. break;
  836. }
  837. if (cnt) {
  838. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  839. "%d:0719 LUN Reset I/O flush failure: cnt x%x\n",
  840. phba->brd_no, cnt);
  841. ret = FAILED;
  842. }
  843. lpfc_sli_release_iocbq(phba, iocbqrsp);
  844. out_free_scsi_buf:
  845. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  846. "%d:0713 SCSI layer issued LUN reset (%d, %d) "
  847. "Data: x%x x%x x%x\n",
  848. phba->brd_no, lpfc_cmd->pCmd->device->id,
  849. lpfc_cmd->pCmd->device->lun, ret, lpfc_cmd->status,
  850. lpfc_cmd->result);
  851. lpfc_release_scsi_buf(phba, lpfc_cmd);
  852. out:
  853. return ret;
  854. }
  855. static int
  856. lpfc_reset_lun_handler(struct scsi_cmnd *cmnd)
  857. {
  858. int rc;
  859. spin_lock_irq(cmnd->device->host->host_lock);
  860. rc = __lpfc_reset_lun_handler(cmnd);
  861. spin_unlock_irq(cmnd->device->host->host_lock);
  862. return rc;
  863. }
  864. /*
  865. * Note: midlayer calls this function with the host_lock held
  866. */
  867. static int
  868. __lpfc_reset_bus_handler(struct scsi_cmnd *cmnd)
  869. {
  870. struct Scsi_Host *shost = cmnd->device->host;
  871. struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata[0];
  872. struct lpfc_nodelist *ndlp = NULL;
  873. int match;
  874. int ret = FAILED, i, err_count = 0;
  875. int cnt, loopcnt;
  876. unsigned int midlayer_id = 0;
  877. struct lpfc_scsi_buf * lpfc_cmd;
  878. lpfc_cmd = lpfc_sli_get_scsi_buf (phba);
  879. if (lpfc_cmd == NULL)
  880. goto out;
  881. /* The lpfc_cmd storage is reused. Set all loop invariants. */
  882. lpfc_cmd->timeout = 60;
  883. lpfc_cmd->pCmd = cmnd;
  884. lpfc_cmd->scsi_hba = phba;
  885. /*
  886. * Since the driver manages a single bus device, reset all
  887. * targets known to the driver. Should any target reset
  888. * fail, this routine returns failure to the midlayer.
  889. */
  890. midlayer_id = cmnd->device->id;
  891. for (i = 0; i < MAX_FCP_TARGET; i++) {
  892. /* Search the mapped list for this target ID */
  893. match = 0;
  894. list_for_each_entry(ndlp, &phba->fc_nlpmap_list, nlp_listp) {
  895. if ((i == ndlp->nlp_sid) && ndlp->rport) {
  896. match = 1;
  897. break;
  898. }
  899. }
  900. if (!match)
  901. continue;
  902. lpfc_cmd->pCmd->device->id = i;
  903. lpfc_cmd->pCmd->device->hostdata = ndlp->rport->dd_data;
  904. ret = lpfc_scsi_tgt_reset(lpfc_cmd, phba);
  905. if (ret != SUCCESS) {
  906. lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
  907. "%d:0713 Bus Reset on target %d failed\n",
  908. phba->brd_no, i);
  909. err_count++;
  910. }
  911. }
  912. cmnd->device->id = midlayer_id;
  913. loopcnt = 0;
  914. while((cnt = lpfc_sli_sum_iocb(phba,
  915. &phba->sli.ring[phba->sli.fcp_ring],
  916. 0, 0, LPFC_CTX_HOST))) {
  917. spin_unlock_irq(phba->host->host_lock);
  918. set_current_state(TASK_UNINTERRUPTIBLE);
  919. schedule_timeout(LPFC_RESET_WAIT*HZ);
  920. spin_lock_irq(phba->host->host_lock);
  921. if (++loopcnt
  922. > (2 * phba->cfg_nodev_tmo)/LPFC_RESET_WAIT)
  923. break;
  924. }
  925. if (cnt) {
  926. /* flush all outstanding commands on the host */
  927. i = lpfc_sli_abort_iocb(phba,
  928. &phba->sli.ring[phba->sli.fcp_ring], 0, 0, 0,
  929. LPFC_CTX_HOST);
  930. lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
  931. "%d:0715 Bus Reset I/O flush failure: cnt x%x left x%x\n",
  932. phba->brd_no, cnt, i);
  933. }
  934. if (cnt == 0)
  935. ret = SUCCESS;
  936. else
  937. ret = FAILED;
  938. lpfc_release_scsi_buf(phba, lpfc_cmd);
  939. lpfc_printf_log(phba,
  940. KERN_ERR,
  941. LOG_FCP,
  942. "%d:0714 SCSI layer issued Bus Reset Data: x%x\n",
  943. phba->brd_no, ret);
  944. out:
  945. return ret;
  946. }
  947. static int
  948. lpfc_reset_bus_handler(struct scsi_cmnd *cmnd)
  949. {
  950. int rc;
  951. spin_lock_irq(cmnd->device->host->host_lock);
  952. rc = __lpfc_reset_bus_handler(cmnd);
  953. spin_unlock_irq(cmnd->device->host->host_lock);
  954. return rc;
  955. }
  956. static int
  957. lpfc_slave_alloc(struct scsi_device *sdev)
  958. {
  959. struct lpfc_hba *phba = (struct lpfc_hba *)sdev->host->hostdata[0];
  960. struct lpfc_scsi_buf *scsi_buf = NULL;
  961. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  962. uint32_t total = 0, i;
  963. uint32_t num_to_alloc = 0;
  964. unsigned long flags;
  965. if (!rport || fc_remote_port_chkready(rport))
  966. return -ENXIO;
  967. sdev->hostdata = rport->dd_data;
  968. /*
  969. * Populate the cmds_per_lun count scsi_bufs into this host's globally
  970. * available list of scsi buffers. Don't allocate more than the
  971. * HBA limit conveyed to the midlayer via the host structure. The
  972. * formula accounts for the lun_queue_depth + error handlers + 1
  973. * extra. This list of scsi bufs exists for the lifetime of the driver.
  974. */
  975. total = phba->total_scsi_bufs;
  976. num_to_alloc = phba->cfg_lun_queue_depth + 2;
  977. if (total >= phba->cfg_hba_queue_depth) {
  978. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
  979. "%d:0704 At limitation of %d preallocated "
  980. "command buffers\n", phba->brd_no, total);
  981. return 0;
  982. } else if (total + num_to_alloc > phba->cfg_hba_queue_depth) {
  983. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
  984. "%d:0705 Allocation request of %d command "
  985. "buffers will exceed max of %d. Reducing "
  986. "allocation request to %d.\n", phba->brd_no,
  987. num_to_alloc, phba->cfg_hba_queue_depth,
  988. (phba->cfg_hba_queue_depth - total));
  989. num_to_alloc = phba->cfg_hba_queue_depth - total;
  990. }
  991. for (i = 0; i < num_to_alloc; i++) {
  992. scsi_buf = lpfc_new_scsi_buf(phba);
  993. if (!scsi_buf) {
  994. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  995. "%d:0706 Failed to allocate command "
  996. "buffer\n", phba->brd_no);
  997. break;
  998. }
  999. spin_lock_irqsave(phba->host->host_lock, flags);
  1000. phba->total_scsi_bufs++;
  1001. list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
  1002. spin_unlock_irqrestore(phba->host->host_lock, flags);
  1003. }
  1004. return 0;
  1005. }
  1006. static int
  1007. lpfc_slave_configure(struct scsi_device *sdev)
  1008. {
  1009. struct lpfc_hba *phba = (struct lpfc_hba *) sdev->host->hostdata[0];
  1010. struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
  1011. if (sdev->tagged_supported)
  1012. scsi_activate_tcq(sdev, phba->cfg_lun_queue_depth);
  1013. else
  1014. scsi_deactivate_tcq(sdev, phba->cfg_lun_queue_depth);
  1015. /*
  1016. * Initialize the fc transport attributes for the target
  1017. * containing this scsi device. Also note that the driver's
  1018. * target pointer is stored in the starget_data for the
  1019. * driver's sysfs entry point functions.
  1020. */
  1021. rport->dev_loss_tmo = phba->cfg_nodev_tmo + 5;
  1022. return 0;
  1023. }
  1024. static void
  1025. lpfc_slave_destroy(struct scsi_device *sdev)
  1026. {
  1027. sdev->hostdata = NULL;
  1028. return;
  1029. }
  1030. struct scsi_host_template lpfc_template = {
  1031. .module = THIS_MODULE,
  1032. .name = LPFC_DRIVER_NAME,
  1033. .info = lpfc_info,
  1034. .queuecommand = lpfc_queuecommand,
  1035. .eh_abort_handler = lpfc_abort_handler,
  1036. .eh_device_reset_handler= lpfc_reset_lun_handler,
  1037. .eh_bus_reset_handler = lpfc_reset_bus_handler,
  1038. .slave_alloc = lpfc_slave_alloc,
  1039. .slave_configure = lpfc_slave_configure,
  1040. .slave_destroy = lpfc_slave_destroy,
  1041. .this_id = -1,
  1042. .sg_tablesize = LPFC_SG_SEG_CNT,
  1043. .cmd_per_lun = LPFC_CMD_PER_LUN,
  1044. .use_clustering = ENABLE_CLUSTERING,
  1045. .shost_attrs = lpfc_host_attrs,
  1046. .max_sectors = 0xFFFF,
  1047. };