lpfc_scsi.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  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. lpfc_cmd->rdata = rdata;
  609. ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, lun,
  610. FCP_TARGET_RESET);
  611. if (!ret)
  612. return FAILED;
  613. lpfc_cmd->scsi_hba = phba;
  614. iocbq = &lpfc_cmd->cur_iocbq;
  615. iocbqrsp = lpfc_sli_get_iocbq(phba);
  616. if (!iocbqrsp)
  617. return FAILED;
  618. /* Issue Target Reset to TGT <num> */
  619. lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
  620. "%d:0702 Issue Target Reset to TGT %d "
  621. "Data: x%x x%x\n",
  622. phba->brd_no, tgt_id, rdata->pnode->nlp_rpi,
  623. rdata->pnode->nlp_flag);
  624. ret = lpfc_sli_issue_iocb_wait(phba,
  625. &phba->sli.ring[phba->sli.fcp_ring],
  626. iocbq, iocbqrsp, lpfc_cmd->timeout);
  627. if (ret != IOCB_SUCCESS) {
  628. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  629. ret = FAILED;
  630. } else {
  631. ret = SUCCESS;
  632. lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
  633. lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
  634. if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
  635. (lpfc_cmd->result & IOERR_DRVR_MASK))
  636. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  637. }
  638. lpfc_sli_release_iocbq(phba, iocbqrsp);
  639. return ret;
  640. }
  641. const char *
  642. lpfc_info(struct Scsi_Host *host)
  643. {
  644. struct lpfc_hba *phba = (struct lpfc_hba *) host->hostdata;
  645. int len;
  646. static char lpfcinfobuf[384];
  647. memset(lpfcinfobuf,0,384);
  648. if (phba && phba->pcidev){
  649. strncpy(lpfcinfobuf, phba->ModelDesc, 256);
  650. len = strlen(lpfcinfobuf);
  651. snprintf(lpfcinfobuf + len,
  652. 384-len,
  653. " on PCI bus %02x device %02x irq %d",
  654. phba->pcidev->bus->number,
  655. phba->pcidev->devfn,
  656. phba->pcidev->irq);
  657. len = strlen(lpfcinfobuf);
  658. if (phba->Port[0]) {
  659. snprintf(lpfcinfobuf + len,
  660. 384-len,
  661. " port %s",
  662. phba->Port);
  663. }
  664. }
  665. return lpfcinfobuf;
  666. }
  667. static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
  668. {
  669. unsigned long poll_tmo_expires =
  670. (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
  671. if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
  672. mod_timer(&phba->fcp_poll_timer,
  673. poll_tmo_expires);
  674. }
  675. void lpfc_poll_start_timer(struct lpfc_hba * phba)
  676. {
  677. lpfc_poll_rearm_timer(phba);
  678. }
  679. void lpfc_poll_timeout(unsigned long ptr)
  680. {
  681. struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
  682. unsigned long iflag;
  683. spin_lock_irqsave(phba->host->host_lock, iflag);
  684. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  685. lpfc_sli_poll_fcp_ring (phba);
  686. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  687. lpfc_poll_rearm_timer(phba);
  688. }
  689. spin_unlock_irqrestore(phba->host->host_lock, iflag);
  690. }
  691. static int
  692. lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
  693. {
  694. struct lpfc_hba *phba =
  695. (struct lpfc_hba *) cmnd->device->host->hostdata;
  696. struct lpfc_sli *psli = &phba->sli;
  697. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  698. struct lpfc_nodelist *ndlp = rdata->pnode;
  699. struct lpfc_scsi_buf *lpfc_cmd;
  700. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  701. int err;
  702. err = fc_remote_port_chkready(rport);
  703. if (err) {
  704. cmnd->result = err;
  705. goto out_fail_command;
  706. }
  707. /*
  708. * Catch race where our node has transitioned, but the
  709. * transport is still transitioning.
  710. */
  711. if (!ndlp) {
  712. cmnd->result = ScsiResult(DID_BUS_BUSY, 0);
  713. goto out_fail_command;
  714. }
  715. lpfc_cmd = lpfc_get_scsi_buf (phba);
  716. if (lpfc_cmd == NULL) {
  717. lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
  718. "%d:0707 driver's buffer pool is empty, "
  719. "IO busied\n", phba->brd_no);
  720. goto out_host_busy;
  721. }
  722. /*
  723. * Store the midlayer's command structure for the completion phase
  724. * and complete the command initialization.
  725. */
  726. lpfc_cmd->pCmd = cmnd;
  727. lpfc_cmd->rdata = rdata;
  728. lpfc_cmd->timeout = 0;
  729. cmnd->host_scribble = (unsigned char *)lpfc_cmd;
  730. cmnd->scsi_done = done;
  731. err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
  732. if (err)
  733. goto out_host_busy_free_buf;
  734. lpfc_scsi_prep_cmnd(phba, lpfc_cmd, ndlp);
  735. err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
  736. &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
  737. if (err)
  738. goto out_host_busy_free_buf;
  739. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  740. lpfc_sli_poll_fcp_ring(phba);
  741. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  742. lpfc_poll_rearm_timer(phba);
  743. }
  744. return 0;
  745. out_host_busy_free_buf:
  746. lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
  747. lpfc_release_scsi_buf(phba, lpfc_cmd);
  748. out_host_busy:
  749. return SCSI_MLQUEUE_HOST_BUSY;
  750. out_fail_command:
  751. done(cmnd);
  752. return 0;
  753. }
  754. static void
  755. lpfc_block_error_handler(struct scsi_cmnd *cmnd)
  756. {
  757. struct Scsi_Host *shost = cmnd->device->host;
  758. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  759. spin_lock_irq(shost->host_lock);
  760. while (rport->port_state == FC_PORTSTATE_BLOCKED) {
  761. spin_unlock_irq(shost->host_lock);
  762. msleep(1000);
  763. spin_lock_irq(shost->host_lock);
  764. }
  765. spin_unlock_irq(shost->host_lock);
  766. return;
  767. }
  768. static int
  769. lpfc_abort_handler(struct scsi_cmnd *cmnd)
  770. {
  771. struct Scsi_Host *shost = cmnd->device->host;
  772. struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata;
  773. struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
  774. struct lpfc_iocbq *iocb;
  775. struct lpfc_iocbq *abtsiocb;
  776. struct lpfc_scsi_buf *lpfc_cmd;
  777. IOCB_t *cmd, *icmd;
  778. unsigned int loop_count = 0;
  779. int ret = SUCCESS;
  780. lpfc_block_error_handler(cmnd);
  781. spin_lock_irq(shost->host_lock);
  782. lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
  783. BUG_ON(!lpfc_cmd);
  784. /*
  785. * If pCmd field of the corresponding lpfc_scsi_buf structure
  786. * points to a different SCSI command, then the driver has
  787. * already completed this command, but the midlayer did not
  788. * see the completion before the eh fired. Just return
  789. * SUCCESS.
  790. */
  791. iocb = &lpfc_cmd->cur_iocbq;
  792. if (lpfc_cmd->pCmd != cmnd)
  793. goto out;
  794. BUG_ON(iocb->context1 != lpfc_cmd);
  795. abtsiocb = lpfc_sli_get_iocbq(phba);
  796. if (abtsiocb == NULL) {
  797. ret = FAILED;
  798. goto out;
  799. }
  800. /*
  801. * The scsi command can not be in txq and it is in flight because the
  802. * pCmd is still pointig at the SCSI command we have to abort. There
  803. * is no need to search the txcmplq. Just send an abort to the FW.
  804. */
  805. cmd = &iocb->iocb;
  806. icmd = &abtsiocb->iocb;
  807. icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
  808. icmd->un.acxri.abortContextTag = cmd->ulpContext;
  809. icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
  810. icmd->ulpLe = 1;
  811. icmd->ulpClass = cmd->ulpClass;
  812. if (phba->hba_state >= LPFC_LINK_UP)
  813. icmd->ulpCommand = CMD_ABORT_XRI_CN;
  814. else
  815. icmd->ulpCommand = CMD_CLOSE_XRI_CN;
  816. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  817. if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
  818. lpfc_sli_release_iocbq(phba, abtsiocb);
  819. ret = FAILED;
  820. goto out;
  821. }
  822. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  823. lpfc_sli_poll_fcp_ring (phba);
  824. /* Wait for abort to complete */
  825. while (lpfc_cmd->pCmd == cmnd)
  826. {
  827. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  828. lpfc_sli_poll_fcp_ring (phba);
  829. spin_unlock_irq(phba->host->host_lock);
  830. schedule_timeout_uninterruptible(LPFC_ABORT_WAIT*HZ);
  831. spin_lock_irq(phba->host->host_lock);
  832. if (++loop_count
  833. > (2 * phba->cfg_devloss_tmo)/LPFC_ABORT_WAIT)
  834. break;
  835. }
  836. if (lpfc_cmd->pCmd == cmnd) {
  837. ret = FAILED;
  838. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  839. "%d:0748 abort handler timed out waiting for "
  840. "abort to complete: ret %#x, ID %d, LUN %d, "
  841. "snum %#lx\n",
  842. phba->brd_no, ret, cmnd->device->id,
  843. cmnd->device->lun, cmnd->serial_number);
  844. }
  845. out:
  846. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
  847. "%d:0749 SCSI Layer I/O Abort Request "
  848. "Status x%x ID %d LUN %d snum %#lx\n",
  849. phba->brd_no, ret, cmnd->device->id,
  850. cmnd->device->lun, cmnd->serial_number);
  851. spin_unlock_irq(shost->host_lock);
  852. return ret;
  853. }
  854. static int
  855. lpfc_reset_lun_handler(struct scsi_cmnd *cmnd)
  856. {
  857. struct Scsi_Host *shost = cmnd->device->host;
  858. struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata;
  859. struct lpfc_scsi_buf *lpfc_cmd;
  860. struct lpfc_iocbq *iocbq, *iocbqrsp;
  861. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  862. struct lpfc_nodelist *pnode = rdata->pnode;
  863. uint32_t cmd_result = 0, cmd_status = 0;
  864. int ret = FAILED;
  865. int cnt, loopcnt;
  866. lpfc_block_error_handler(cmnd);
  867. spin_lock_irq(shost->host_lock);
  868. /*
  869. * If target is not in a MAPPED state, delay the reset until
  870. * target is rediscovered or devloss timeout expires.
  871. */
  872. while ( 1 ) {
  873. if (!pnode)
  874. break;
  875. if (pnode->nlp_state != NLP_STE_MAPPED_NODE) {
  876. spin_unlock_irq(phba->host->host_lock);
  877. schedule_timeout_uninterruptible(msecs_to_jiffies(500));
  878. spin_lock_irq(phba->host->host_lock);
  879. }
  880. if ((pnode) && (pnode->nlp_state == NLP_STE_MAPPED_NODE))
  881. break;
  882. }
  883. lpfc_cmd = lpfc_get_scsi_buf (phba);
  884. if (lpfc_cmd == NULL)
  885. goto out;
  886. lpfc_cmd->timeout = 60;
  887. lpfc_cmd->scsi_hba = phba;
  888. lpfc_cmd->rdata = rdata;
  889. ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, cmnd->device->lun,
  890. FCP_LUN_RESET);
  891. if (!ret)
  892. goto out_free_scsi_buf;
  893. iocbq = &lpfc_cmd->cur_iocbq;
  894. /* get a buffer for this IOCB command response */
  895. iocbqrsp = lpfc_sli_get_iocbq(phba);
  896. if (iocbqrsp == NULL)
  897. goto out_free_scsi_buf;
  898. lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
  899. "%d:0703 Issue LUN Reset to TGT %d LUN %d "
  900. "Data: x%x x%x\n", phba->brd_no, cmnd->device->id,
  901. cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
  902. ret = lpfc_sli_issue_iocb_wait(phba,
  903. &phba->sli.ring[phba->sli.fcp_ring],
  904. iocbq, iocbqrsp, lpfc_cmd->timeout);
  905. if (ret == IOCB_SUCCESS)
  906. ret = SUCCESS;
  907. cmd_result = iocbqrsp->iocb.un.ulpWord[4];
  908. cmd_status = iocbqrsp->iocb.ulpStatus;
  909. lpfc_sli_release_iocbq(phba, iocbqrsp);
  910. /*
  911. * All outstanding txcmplq I/Os should have been aborted by the device.
  912. * Unfortunately, some targets do not abide by this forcing the driver
  913. * to double check.
  914. */
  915. cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
  916. cmnd->device->id, cmnd->device->lun,
  917. LPFC_CTX_LUN);
  918. if (cnt)
  919. lpfc_sli_abort_iocb(phba,
  920. &phba->sli.ring[phba->sli.fcp_ring],
  921. cmnd->device->id, cmnd->device->lun,
  922. 0, LPFC_CTX_LUN);
  923. loopcnt = 0;
  924. while(cnt) {
  925. spin_unlock_irq(phba->host->host_lock);
  926. schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
  927. spin_lock_irq(phba->host->host_lock);
  928. if (++loopcnt
  929. > (2 * phba->cfg_devloss_tmo)/LPFC_RESET_WAIT)
  930. break;
  931. cnt = lpfc_sli_sum_iocb(phba,
  932. &phba->sli.ring[phba->sli.fcp_ring],
  933. cmnd->device->id, cmnd->device->lun,
  934. LPFC_CTX_LUN);
  935. }
  936. if (cnt) {
  937. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  938. "%d:0719 LUN Reset I/O flush failure: cnt x%x\n",
  939. phba->brd_no, cnt);
  940. ret = FAILED;
  941. }
  942. out_free_scsi_buf:
  943. lpfc_release_scsi_buf(phba, lpfc_cmd);
  944. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  945. "%d:0713 SCSI layer issued LUN reset (%d, %d) "
  946. "Data: x%x x%x x%x\n",
  947. phba->brd_no, cmnd->device->id,cmnd->device->lun,
  948. ret, cmd_status, cmd_result);
  949. out:
  950. spin_unlock_irq(shost->host_lock);
  951. return ret;
  952. }
  953. static int
  954. lpfc_reset_bus_handler(struct scsi_cmnd *cmnd)
  955. {
  956. struct Scsi_Host *shost = cmnd->device->host;
  957. struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata;
  958. struct lpfc_nodelist *ndlp = NULL;
  959. int match;
  960. int ret = FAILED, i, err_count = 0;
  961. int cnt, loopcnt;
  962. struct lpfc_scsi_buf * lpfc_cmd;
  963. lpfc_block_error_handler(cmnd);
  964. spin_lock_irq(shost->host_lock);
  965. lpfc_cmd = lpfc_get_scsi_buf(phba);
  966. if (lpfc_cmd == NULL)
  967. goto out;
  968. /* The lpfc_cmd storage is reused. Set all loop invariants. */
  969. lpfc_cmd->timeout = 60;
  970. lpfc_cmd->scsi_hba = phba;
  971. /*
  972. * Since the driver manages a single bus device, reset all
  973. * targets known to the driver. Should any target reset
  974. * fail, this routine returns failure to the midlayer.
  975. */
  976. for (i = 0; i < LPFC_MAX_TARGET; i++) {
  977. /* Search the mapped list for this target ID */
  978. match = 0;
  979. list_for_each_entry(ndlp, &phba->fc_nlpmap_list, nlp_listp) {
  980. if ((i == ndlp->nlp_sid) && ndlp->rport) {
  981. match = 1;
  982. break;
  983. }
  984. }
  985. if (!match)
  986. continue;
  987. ret = lpfc_scsi_tgt_reset(lpfc_cmd, phba, i, cmnd->device->lun,
  988. ndlp->rport->dd_data);
  989. if (ret != SUCCESS) {
  990. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  991. "%d:0700 Bus Reset on target %d failed\n",
  992. phba->brd_no, i);
  993. err_count++;
  994. }
  995. }
  996. if (err_count == 0)
  997. ret = SUCCESS;
  998. lpfc_release_scsi_buf(phba, lpfc_cmd);
  999. /*
  1000. * All outstanding txcmplq I/Os should have been aborted by
  1001. * the targets. Unfortunately, some targets do not abide by
  1002. * this forcing the driver to double check.
  1003. */
  1004. cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
  1005. 0, 0, LPFC_CTX_HOST);
  1006. if (cnt)
  1007. lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
  1008. 0, 0, 0, LPFC_CTX_HOST);
  1009. loopcnt = 0;
  1010. while(cnt) {
  1011. spin_unlock_irq(phba->host->host_lock);
  1012. schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
  1013. spin_lock_irq(phba->host->host_lock);
  1014. if (++loopcnt
  1015. > (2 * phba->cfg_devloss_tmo)/LPFC_RESET_WAIT)
  1016. break;
  1017. cnt = lpfc_sli_sum_iocb(phba,
  1018. &phba->sli.ring[phba->sli.fcp_ring],
  1019. 0, 0, LPFC_CTX_HOST);
  1020. }
  1021. if (cnt) {
  1022. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1023. "%d:0715 Bus Reset I/O flush failure: cnt x%x left x%x\n",
  1024. phba->brd_no, cnt, i);
  1025. ret = FAILED;
  1026. }
  1027. lpfc_printf_log(phba,
  1028. KERN_ERR,
  1029. LOG_FCP,
  1030. "%d:0714 SCSI layer issued Bus Reset Data: x%x\n",
  1031. phba->brd_no, ret);
  1032. out:
  1033. spin_unlock_irq(shost->host_lock);
  1034. return ret;
  1035. }
  1036. static int
  1037. lpfc_slave_alloc(struct scsi_device *sdev)
  1038. {
  1039. struct lpfc_hba *phba = (struct lpfc_hba *)sdev->host->hostdata;
  1040. struct lpfc_scsi_buf *scsi_buf = NULL;
  1041. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1042. uint32_t total = 0, i;
  1043. uint32_t num_to_alloc = 0;
  1044. unsigned long flags;
  1045. if (!rport || fc_remote_port_chkready(rport))
  1046. return -ENXIO;
  1047. sdev->hostdata = rport->dd_data;
  1048. /*
  1049. * Populate the cmds_per_lun count scsi_bufs into this host's globally
  1050. * available list of scsi buffers. Don't allocate more than the
  1051. * HBA limit conveyed to the midlayer via the host structure. The
  1052. * formula accounts for the lun_queue_depth + error handlers + 1
  1053. * extra. This list of scsi bufs exists for the lifetime of the driver.
  1054. */
  1055. total = phba->total_scsi_bufs;
  1056. num_to_alloc = phba->cfg_lun_queue_depth + 2;
  1057. if (total >= phba->cfg_hba_queue_depth) {
  1058. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
  1059. "%d:0704 At limitation of %d preallocated "
  1060. "command buffers\n", phba->brd_no, total);
  1061. return 0;
  1062. } else if (total + num_to_alloc > phba->cfg_hba_queue_depth) {
  1063. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
  1064. "%d:0705 Allocation request of %d command "
  1065. "buffers will exceed max of %d. Reducing "
  1066. "allocation request to %d.\n", phba->brd_no,
  1067. num_to_alloc, phba->cfg_hba_queue_depth,
  1068. (phba->cfg_hba_queue_depth - total));
  1069. num_to_alloc = phba->cfg_hba_queue_depth - total;
  1070. }
  1071. for (i = 0; i < num_to_alloc; i++) {
  1072. scsi_buf = lpfc_new_scsi_buf(phba);
  1073. if (!scsi_buf) {
  1074. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1075. "%d:0706 Failed to allocate command "
  1076. "buffer\n", phba->brd_no);
  1077. break;
  1078. }
  1079. spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
  1080. phba->total_scsi_bufs++;
  1081. list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
  1082. spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
  1083. }
  1084. return 0;
  1085. }
  1086. static int
  1087. lpfc_slave_configure(struct scsi_device *sdev)
  1088. {
  1089. struct lpfc_hba *phba = (struct lpfc_hba *) sdev->host->hostdata;
  1090. struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
  1091. if (sdev->tagged_supported)
  1092. scsi_activate_tcq(sdev, phba->cfg_lun_queue_depth);
  1093. else
  1094. scsi_deactivate_tcq(sdev, phba->cfg_lun_queue_depth);
  1095. /*
  1096. * Initialize the fc transport attributes for the target
  1097. * containing this scsi device. Also note that the driver's
  1098. * target pointer is stored in the starget_data for the
  1099. * driver's sysfs entry point functions.
  1100. */
  1101. rport->dev_loss_tmo = phba->cfg_devloss_tmo;
  1102. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  1103. lpfc_sli_poll_fcp_ring(phba);
  1104. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  1105. lpfc_poll_rearm_timer(phba);
  1106. }
  1107. return 0;
  1108. }
  1109. static void
  1110. lpfc_slave_destroy(struct scsi_device *sdev)
  1111. {
  1112. sdev->hostdata = NULL;
  1113. return;
  1114. }
  1115. struct scsi_host_template lpfc_template = {
  1116. .module = THIS_MODULE,
  1117. .name = LPFC_DRIVER_NAME,
  1118. .info = lpfc_info,
  1119. .queuecommand = lpfc_queuecommand,
  1120. .eh_abort_handler = lpfc_abort_handler,
  1121. .eh_device_reset_handler= lpfc_reset_lun_handler,
  1122. .eh_bus_reset_handler = lpfc_reset_bus_handler,
  1123. .slave_alloc = lpfc_slave_alloc,
  1124. .slave_configure = lpfc_slave_configure,
  1125. .slave_destroy = lpfc_slave_destroy,
  1126. .this_id = -1,
  1127. .sg_tablesize = LPFC_SG_SEG_CNT,
  1128. .cmd_per_lun = LPFC_CMD_PER_LUN,
  1129. .use_clustering = ENABLE_CLUSTERING,
  1130. .shost_attrs = lpfc_host_attrs,
  1131. .max_sectors = 0xFFFF,
  1132. };