lpfc_scsi.c 42 KB

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