lpfc_scsi.c 36 KB

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