lpfc_scsi.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  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 < LPFC_MAX_VPORTS && 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(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 < LPFC_MAX_VPORTS && 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(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 = be32_to_cpu(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. lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
  490. lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
  491. if (lpfc_cmd->status) {
  492. if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
  493. (lpfc_cmd->result & IOERR_DRVR_MASK))
  494. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  495. else if (lpfc_cmd->status >= IOSTAT_CNT)
  496. lpfc_cmd->status = IOSTAT_DEFAULT;
  497. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  498. "0729 FCP cmd x%x failed <%d/%d> "
  499. "status: x%x result: x%x Data: x%x x%x\n",
  500. cmd->cmnd[0],
  501. cmd->device ? cmd->device->id : 0xffff,
  502. cmd->device ? cmd->device->lun : 0xffff,
  503. lpfc_cmd->status, lpfc_cmd->result,
  504. pIocbOut->iocb.ulpContext,
  505. lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
  506. switch (lpfc_cmd->status) {
  507. case IOSTAT_FCP_RSP_ERROR:
  508. /* Call FCP RSP handler to determine result */
  509. lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
  510. break;
  511. case IOSTAT_NPORT_BSY:
  512. case IOSTAT_FABRIC_BSY:
  513. cmd->result = ScsiResult(DID_BUS_BUSY, 0);
  514. break;
  515. case IOSTAT_LOCAL_REJECT:
  516. if (lpfc_cmd->result == RJT_UNAVAIL_PERM ||
  517. lpfc_cmd->result == IOERR_NO_RESOURCES ||
  518. lpfc_cmd->result == RJT_LOGIN_REQUIRED) {
  519. cmd->result = ScsiResult(DID_REQUEUE, 0);
  520. break;
  521. } /* else: fall through */
  522. default:
  523. cmd->result = ScsiResult(DID_ERROR, 0);
  524. break;
  525. }
  526. if ((pnode == NULL )
  527. || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
  528. cmd->result = ScsiResult(DID_BUS_BUSY, SAM_STAT_BUSY);
  529. } else {
  530. cmd->result = ScsiResult(DID_OK, 0);
  531. }
  532. if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
  533. uint32_t *lp = (uint32_t *)cmd->sense_buffer;
  534. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  535. "0710 Iodone <%d/%d> cmd %p, error "
  536. "x%x SNS x%x x%x Data: x%x x%x\n",
  537. cmd->device->id, cmd->device->lun, cmd,
  538. cmd->result, *lp, *(lp + 3), cmd->retries,
  539. scsi_get_resid(cmd));
  540. }
  541. result = cmd->result;
  542. sdev = cmd->device;
  543. lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
  544. cmd->scsi_done(cmd);
  545. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  546. lpfc_release_scsi_buf(phba, lpfc_cmd);
  547. return;
  548. }
  549. if (!result)
  550. lpfc_rampup_queue_depth(vport, sdev);
  551. if (!result && pnode != NULL &&
  552. ((jiffies - pnode->last_ramp_up_time) >
  553. LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
  554. ((jiffies - pnode->last_q_full_time) >
  555. LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
  556. (vport->cfg_lun_queue_depth > sdev->queue_depth)) {
  557. shost_for_each_device(tmp_sdev, sdev->host) {
  558. if (vport->cfg_lun_queue_depth > tmp_sdev->queue_depth){
  559. if (tmp_sdev->id != sdev->id)
  560. continue;
  561. if (tmp_sdev->ordered_tags)
  562. scsi_adjust_queue_depth(tmp_sdev,
  563. MSG_ORDERED_TAG,
  564. tmp_sdev->queue_depth+1);
  565. else
  566. scsi_adjust_queue_depth(tmp_sdev,
  567. MSG_SIMPLE_TAG,
  568. tmp_sdev->queue_depth+1);
  569. pnode->last_ramp_up_time = jiffies;
  570. }
  571. }
  572. }
  573. /*
  574. * Check for queue full. If the lun is reporting queue full, then
  575. * back off the lun queue depth to prevent target overloads.
  576. */
  577. if (result == SAM_STAT_TASK_SET_FULL && pnode != NULL) {
  578. pnode->last_q_full_time = jiffies;
  579. shost_for_each_device(tmp_sdev, sdev->host) {
  580. if (tmp_sdev->id != sdev->id)
  581. continue;
  582. depth = scsi_track_queue_full(tmp_sdev,
  583. tmp_sdev->queue_depth - 1);
  584. }
  585. /*
  586. * The queue depth cannot be lowered any more.
  587. * Modify the returned error code to store
  588. * the final depth value set by
  589. * scsi_track_queue_full.
  590. */
  591. if (depth == -1)
  592. depth = sdev->host->cmd_per_lun;
  593. if (depth) {
  594. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  595. "0711 detected queue full - lun queue "
  596. "depth adjusted to %d.\n", depth);
  597. }
  598. }
  599. lpfc_release_scsi_buf(phba, lpfc_cmd);
  600. }
  601. static void
  602. lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
  603. struct lpfc_nodelist *pnode)
  604. {
  605. struct lpfc_hba *phba = vport->phba;
  606. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  607. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  608. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  609. struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
  610. int datadir = scsi_cmnd->sc_data_direction;
  611. lpfc_cmd->fcp_rsp->rspSnsLen = 0;
  612. /* clear task management bits */
  613. lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
  614. int_to_scsilun(lpfc_cmd->pCmd->device->lun,
  615. &lpfc_cmd->fcp_cmnd->fcp_lun);
  616. memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
  617. if (scsi_cmnd->device->tagged_supported) {
  618. switch (scsi_cmnd->tag) {
  619. case HEAD_OF_QUEUE_TAG:
  620. fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
  621. break;
  622. case ORDERED_QUEUE_TAG:
  623. fcp_cmnd->fcpCntl1 = ORDERED_Q;
  624. break;
  625. default:
  626. fcp_cmnd->fcpCntl1 = SIMPLE_Q;
  627. break;
  628. }
  629. } else
  630. fcp_cmnd->fcpCntl1 = 0;
  631. /*
  632. * There are three possibilities here - use scatter-gather segment, use
  633. * the single mapping, or neither. Start the lpfc command prep by
  634. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  635. * data bde entry.
  636. */
  637. if (scsi_sg_count(scsi_cmnd)) {
  638. if (datadir == DMA_TO_DEVICE) {
  639. iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
  640. iocb_cmd->un.fcpi.fcpi_parm = 0;
  641. iocb_cmd->ulpPU = 0;
  642. fcp_cmnd->fcpCntl3 = WRITE_DATA;
  643. phba->fc4OutputRequests++;
  644. } else {
  645. iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
  646. iocb_cmd->ulpPU = PARM_READ_CHECK;
  647. iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
  648. fcp_cmnd->fcpCntl3 = READ_DATA;
  649. phba->fc4InputRequests++;
  650. }
  651. } else {
  652. iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
  653. iocb_cmd->un.fcpi.fcpi_parm = 0;
  654. iocb_cmd->ulpPU = 0;
  655. fcp_cmnd->fcpCntl3 = 0;
  656. phba->fc4ControlRequests++;
  657. }
  658. /*
  659. * Finish initializing those IOCB fields that are independent
  660. * of the scsi_cmnd request_buffer
  661. */
  662. piocbq->iocb.ulpContext = pnode->nlp_rpi;
  663. if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
  664. piocbq->iocb.ulpFCP2Rcvy = 1;
  665. piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
  666. piocbq->context1 = lpfc_cmd;
  667. piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
  668. piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
  669. piocbq->vport = vport;
  670. }
  671. static int
  672. lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
  673. struct lpfc_scsi_buf *lpfc_cmd,
  674. unsigned int lun,
  675. uint8_t task_mgmt_cmd)
  676. {
  677. struct lpfc_iocbq *piocbq;
  678. IOCB_t *piocb;
  679. struct fcp_cmnd *fcp_cmnd;
  680. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  681. struct lpfc_nodelist *ndlp = rdata->pnode;
  682. if ((ndlp == NULL) || (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
  683. return 0;
  684. }
  685. piocbq = &(lpfc_cmd->cur_iocbq);
  686. piocbq->vport = vport;
  687. piocb = &piocbq->iocb;
  688. fcp_cmnd = lpfc_cmd->fcp_cmnd;
  689. int_to_scsilun(lun, &lpfc_cmd->fcp_cmnd->fcp_lun);
  690. fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
  691. piocb->ulpCommand = CMD_FCP_ICMND64_CR;
  692. piocb->ulpContext = ndlp->nlp_rpi;
  693. if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
  694. piocb->ulpFCP2Rcvy = 1;
  695. }
  696. piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
  697. /* ulpTimeout is only one byte */
  698. if (lpfc_cmd->timeout > 0xff) {
  699. /*
  700. * Do not timeout the command at the firmware level.
  701. * The driver will provide the timeout mechanism.
  702. */
  703. piocb->ulpTimeout = 0;
  704. } else {
  705. piocb->ulpTimeout = lpfc_cmd->timeout;
  706. }
  707. return 1;
  708. }
  709. static void
  710. lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
  711. struct lpfc_iocbq *cmdiocbq,
  712. struct lpfc_iocbq *rspiocbq)
  713. {
  714. struct lpfc_scsi_buf *lpfc_cmd =
  715. (struct lpfc_scsi_buf *) cmdiocbq->context1;
  716. if (lpfc_cmd)
  717. lpfc_release_scsi_buf(phba, lpfc_cmd);
  718. return;
  719. }
  720. static int
  721. lpfc_scsi_tgt_reset(struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_vport *vport,
  722. unsigned tgt_id, unsigned int lun,
  723. struct lpfc_rport_data *rdata)
  724. {
  725. struct lpfc_hba *phba = vport->phba;
  726. struct lpfc_iocbq *iocbq;
  727. struct lpfc_iocbq *iocbqrsp;
  728. int ret;
  729. if (!rdata->pnode)
  730. return FAILED;
  731. lpfc_cmd->rdata = rdata;
  732. ret = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun,
  733. FCP_TARGET_RESET);
  734. if (!ret)
  735. return FAILED;
  736. iocbq = &lpfc_cmd->cur_iocbq;
  737. iocbqrsp = lpfc_sli_get_iocbq(phba);
  738. if (!iocbqrsp)
  739. return FAILED;
  740. /* Issue Target Reset to TGT <num> */
  741. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  742. "0702 Issue Target Reset to TGT %d Data: x%x x%x\n",
  743. tgt_id, rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
  744. ret = lpfc_sli_issue_iocb_wait(phba,
  745. &phba->sli.ring[phba->sli.fcp_ring],
  746. iocbq, iocbqrsp, lpfc_cmd->timeout);
  747. if (ret != IOCB_SUCCESS) {
  748. if (ret == IOCB_TIMEDOUT)
  749. iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
  750. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  751. } else {
  752. ret = SUCCESS;
  753. lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
  754. lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
  755. if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
  756. (lpfc_cmd->result & IOERR_DRVR_MASK))
  757. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  758. }
  759. lpfc_sli_release_iocbq(phba, iocbqrsp);
  760. return ret;
  761. }
  762. const char *
  763. lpfc_info(struct Scsi_Host *host)
  764. {
  765. struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
  766. struct lpfc_hba *phba = vport->phba;
  767. int len;
  768. static char lpfcinfobuf[384];
  769. memset(lpfcinfobuf,0,384);
  770. if (phba && phba->pcidev){
  771. strncpy(lpfcinfobuf, phba->ModelDesc, 256);
  772. len = strlen(lpfcinfobuf);
  773. snprintf(lpfcinfobuf + len,
  774. 384-len,
  775. " on PCI bus %02x device %02x irq %d",
  776. phba->pcidev->bus->number,
  777. phba->pcidev->devfn,
  778. phba->pcidev->irq);
  779. len = strlen(lpfcinfobuf);
  780. if (phba->Port[0]) {
  781. snprintf(lpfcinfobuf + len,
  782. 384-len,
  783. " port %s",
  784. phba->Port);
  785. }
  786. }
  787. return lpfcinfobuf;
  788. }
  789. static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
  790. {
  791. unsigned long poll_tmo_expires =
  792. (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
  793. if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
  794. mod_timer(&phba->fcp_poll_timer,
  795. poll_tmo_expires);
  796. }
  797. void lpfc_poll_start_timer(struct lpfc_hba * phba)
  798. {
  799. lpfc_poll_rearm_timer(phba);
  800. }
  801. void lpfc_poll_timeout(unsigned long ptr)
  802. {
  803. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  804. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  805. lpfc_sli_poll_fcp_ring (phba);
  806. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  807. lpfc_poll_rearm_timer(phba);
  808. }
  809. }
  810. static int
  811. lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
  812. {
  813. struct Scsi_Host *shost = cmnd->device->host;
  814. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  815. struct lpfc_hba *phba = vport->phba;
  816. struct lpfc_sli *psli = &phba->sli;
  817. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  818. struct lpfc_nodelist *ndlp = rdata->pnode;
  819. struct lpfc_scsi_buf *lpfc_cmd;
  820. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  821. int err;
  822. err = fc_remote_port_chkready(rport);
  823. if (err) {
  824. cmnd->result = err;
  825. goto out_fail_command;
  826. }
  827. /*
  828. * Catch race where our node has transitioned, but the
  829. * transport is still transitioning.
  830. */
  831. if (!ndlp) {
  832. cmnd->result = ScsiResult(DID_BUS_BUSY, 0);
  833. goto out_fail_command;
  834. }
  835. lpfc_cmd = lpfc_get_scsi_buf(phba);
  836. if (lpfc_cmd == NULL) {
  837. lpfc_adjust_queue_depth(phba);
  838. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  839. "0707 driver's buffer pool is empty, "
  840. "IO busied\n");
  841. goto out_host_busy;
  842. }
  843. /*
  844. * Store the midlayer's command structure for the completion phase
  845. * and complete the command initialization.
  846. */
  847. lpfc_cmd->pCmd = cmnd;
  848. lpfc_cmd->rdata = rdata;
  849. lpfc_cmd->timeout = 0;
  850. cmnd->host_scribble = (unsigned char *)lpfc_cmd;
  851. cmnd->scsi_done = done;
  852. err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
  853. if (err)
  854. goto out_host_busy_free_buf;
  855. lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
  856. err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
  857. &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
  858. if (err)
  859. goto out_host_busy_free_buf;
  860. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  861. lpfc_sli_poll_fcp_ring(phba);
  862. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  863. lpfc_poll_rearm_timer(phba);
  864. }
  865. return 0;
  866. out_host_busy_free_buf:
  867. lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
  868. lpfc_release_scsi_buf(phba, lpfc_cmd);
  869. out_host_busy:
  870. return SCSI_MLQUEUE_HOST_BUSY;
  871. out_fail_command:
  872. done(cmnd);
  873. return 0;
  874. }
  875. static void
  876. lpfc_block_error_handler(struct scsi_cmnd *cmnd)
  877. {
  878. struct Scsi_Host *shost = cmnd->device->host;
  879. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  880. spin_lock_irq(shost->host_lock);
  881. while (rport->port_state == FC_PORTSTATE_BLOCKED) {
  882. spin_unlock_irq(shost->host_lock);
  883. msleep(1000);
  884. spin_lock_irq(shost->host_lock);
  885. }
  886. spin_unlock_irq(shost->host_lock);
  887. return;
  888. }
  889. static int
  890. lpfc_abort_handler(struct scsi_cmnd *cmnd)
  891. {
  892. struct Scsi_Host *shost = cmnd->device->host;
  893. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  894. struct lpfc_hba *phba = vport->phba;
  895. struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
  896. struct lpfc_iocbq *iocb;
  897. struct lpfc_iocbq *abtsiocb;
  898. struct lpfc_scsi_buf *lpfc_cmd;
  899. IOCB_t *cmd, *icmd;
  900. unsigned int loop_count = 0;
  901. int ret = SUCCESS;
  902. lpfc_block_error_handler(cmnd);
  903. lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
  904. BUG_ON(!lpfc_cmd);
  905. /*
  906. * If pCmd field of the corresponding lpfc_scsi_buf structure
  907. * points to a different SCSI command, then the driver has
  908. * already completed this command, but the midlayer did not
  909. * see the completion before the eh fired. Just return
  910. * SUCCESS.
  911. */
  912. iocb = &lpfc_cmd->cur_iocbq;
  913. if (lpfc_cmd->pCmd != cmnd)
  914. goto out;
  915. BUG_ON(iocb->context1 != lpfc_cmd);
  916. abtsiocb = lpfc_sli_get_iocbq(phba);
  917. if (abtsiocb == NULL) {
  918. ret = FAILED;
  919. goto out;
  920. }
  921. /*
  922. * The scsi command can not be in txq and it is in flight because the
  923. * pCmd is still pointig at the SCSI command we have to abort. There
  924. * is no need to search the txcmplq. Just send an abort to the FW.
  925. */
  926. cmd = &iocb->iocb;
  927. icmd = &abtsiocb->iocb;
  928. icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
  929. icmd->un.acxri.abortContextTag = cmd->ulpContext;
  930. icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
  931. icmd->ulpLe = 1;
  932. icmd->ulpClass = cmd->ulpClass;
  933. if (lpfc_is_link_up(phba))
  934. icmd->ulpCommand = CMD_ABORT_XRI_CN;
  935. else
  936. icmd->ulpCommand = CMD_CLOSE_XRI_CN;
  937. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  938. abtsiocb->vport = vport;
  939. if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
  940. lpfc_sli_release_iocbq(phba, abtsiocb);
  941. ret = FAILED;
  942. goto out;
  943. }
  944. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  945. lpfc_sli_poll_fcp_ring (phba);
  946. /* Wait for abort to complete */
  947. while (lpfc_cmd->pCmd == cmnd)
  948. {
  949. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  950. lpfc_sli_poll_fcp_ring (phba);
  951. schedule_timeout_uninterruptible(LPFC_ABORT_WAIT * HZ);
  952. if (++loop_count
  953. > (2 * vport->cfg_devloss_tmo)/LPFC_ABORT_WAIT)
  954. break;
  955. }
  956. if (lpfc_cmd->pCmd == cmnd) {
  957. ret = FAILED;
  958. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  959. "0748 abort handler timed out waiting "
  960. "for abort to complete: ret %#x, ID %d, "
  961. "LUN %d, snum %#lx\n",
  962. ret, cmnd->device->id, cmnd->device->lun,
  963. cmnd->serial_number);
  964. }
  965. out:
  966. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  967. "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
  968. "LUN %d snum %#lx\n", ret, cmnd->device->id,
  969. cmnd->device->lun, cmnd->serial_number);
  970. return ret;
  971. }
  972. static int
  973. lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
  974. {
  975. struct Scsi_Host *shost = cmnd->device->host;
  976. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  977. struct lpfc_hba *phba = vport->phba;
  978. struct lpfc_scsi_buf *lpfc_cmd;
  979. struct lpfc_iocbq *iocbq, *iocbqrsp;
  980. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  981. struct lpfc_nodelist *pnode = rdata->pnode;
  982. uint32_t cmd_result = 0, cmd_status = 0;
  983. int ret = FAILED;
  984. int iocb_status = IOCB_SUCCESS;
  985. int cnt, loopcnt;
  986. lpfc_block_error_handler(cmnd);
  987. loopcnt = 0;
  988. /*
  989. * If target is not in a MAPPED state, delay the reset until
  990. * target is rediscovered or devloss timeout expires.
  991. */
  992. while (1) {
  993. if (!pnode)
  994. goto out;
  995. if (pnode->nlp_state != NLP_STE_MAPPED_NODE) {
  996. schedule_timeout_uninterruptible(msecs_to_jiffies(500));
  997. loopcnt++;
  998. rdata = cmnd->device->hostdata;
  999. if (!rdata ||
  1000. (loopcnt > ((vport->cfg_devloss_tmo * 2) + 1))){
  1001. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  1002. "0721 LUN Reset rport "
  1003. "failure: cnt x%x rdata x%p\n",
  1004. loopcnt, rdata);
  1005. goto out;
  1006. }
  1007. pnode = rdata->pnode;
  1008. if (!pnode)
  1009. goto out;
  1010. }
  1011. if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
  1012. break;
  1013. }
  1014. lpfc_cmd = lpfc_get_scsi_buf(phba);
  1015. if (lpfc_cmd == NULL)
  1016. goto out;
  1017. lpfc_cmd->timeout = 60;
  1018. lpfc_cmd->rdata = rdata;
  1019. ret = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, cmnd->device->lun,
  1020. FCP_TARGET_RESET);
  1021. if (!ret)
  1022. goto out_free_scsi_buf;
  1023. iocbq = &lpfc_cmd->cur_iocbq;
  1024. /* get a buffer for this IOCB command response */
  1025. iocbqrsp = lpfc_sli_get_iocbq(phba);
  1026. if (iocbqrsp == NULL)
  1027. goto out_free_scsi_buf;
  1028. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  1029. "0703 Issue target reset to TGT %d LUN %d "
  1030. "rpi x%x nlp_flag x%x\n", cmnd->device->id,
  1031. cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
  1032. iocb_status = lpfc_sli_issue_iocb_wait(phba,
  1033. &phba->sli.ring[phba->sli.fcp_ring],
  1034. iocbq, iocbqrsp, lpfc_cmd->timeout);
  1035. if (iocb_status == IOCB_TIMEDOUT)
  1036. iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
  1037. if (iocb_status == IOCB_SUCCESS)
  1038. ret = SUCCESS;
  1039. else
  1040. ret = iocb_status;
  1041. cmd_result = iocbqrsp->iocb.un.ulpWord[4];
  1042. cmd_status = iocbqrsp->iocb.ulpStatus;
  1043. lpfc_sli_release_iocbq(phba, iocbqrsp);
  1044. /*
  1045. * All outstanding txcmplq I/Os should have been aborted by the device.
  1046. * Unfortunately, some targets do not abide by this forcing the driver
  1047. * to double check.
  1048. */
  1049. cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id, cmnd->device->lun,
  1050. LPFC_CTX_LUN);
  1051. if (cnt)
  1052. lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
  1053. cmnd->device->id, cmnd->device->lun,
  1054. LPFC_CTX_LUN);
  1055. loopcnt = 0;
  1056. while(cnt) {
  1057. schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
  1058. if (++loopcnt
  1059. > (2 * vport->cfg_devloss_tmo)/LPFC_RESET_WAIT)
  1060. break;
  1061. cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id,
  1062. cmnd->device->lun, LPFC_CTX_LUN);
  1063. }
  1064. if (cnt) {
  1065. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  1066. "0719 device reset I/O flush failure: "
  1067. "cnt x%x\n", cnt);
  1068. ret = FAILED;
  1069. }
  1070. out_free_scsi_buf:
  1071. if (iocb_status != IOCB_TIMEDOUT) {
  1072. lpfc_release_scsi_buf(phba, lpfc_cmd);
  1073. }
  1074. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  1075. "0713 SCSI layer issued device reset (%d, %d) "
  1076. "return x%x status x%x result x%x\n",
  1077. cmnd->device->id, cmnd->device->lun, ret,
  1078. cmd_status, cmd_result);
  1079. out:
  1080. return ret;
  1081. }
  1082. static int
  1083. lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
  1084. {
  1085. struct Scsi_Host *shost = cmnd->device->host;
  1086. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  1087. struct lpfc_hba *phba = vport->phba;
  1088. struct lpfc_nodelist *ndlp = NULL;
  1089. int match;
  1090. int ret = FAILED, i, err_count = 0;
  1091. int cnt, loopcnt;
  1092. struct lpfc_scsi_buf * lpfc_cmd;
  1093. lpfc_block_error_handler(cmnd);
  1094. lpfc_cmd = lpfc_get_scsi_buf(phba);
  1095. if (lpfc_cmd == NULL)
  1096. goto out;
  1097. /* The lpfc_cmd storage is reused. Set all loop invariants. */
  1098. lpfc_cmd->timeout = 60;
  1099. /*
  1100. * Since the driver manages a single bus device, reset all
  1101. * targets known to the driver. Should any target reset
  1102. * fail, this routine returns failure to the midlayer.
  1103. */
  1104. for (i = 0; i < LPFC_MAX_TARGET; i++) {
  1105. /* Search for mapped node by target ID */
  1106. match = 0;
  1107. spin_lock_irq(shost->host_lock);
  1108. list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
  1109. if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
  1110. i == ndlp->nlp_sid &&
  1111. ndlp->rport) {
  1112. match = 1;
  1113. break;
  1114. }
  1115. }
  1116. spin_unlock_irq(shost->host_lock);
  1117. if (!match)
  1118. continue;
  1119. ret = lpfc_scsi_tgt_reset(lpfc_cmd, vport, i,
  1120. cmnd->device->lun,
  1121. ndlp->rport->dd_data);
  1122. if (ret != SUCCESS) {
  1123. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  1124. "0700 Bus Reset on target %d failed\n",
  1125. i);
  1126. err_count++;
  1127. break;
  1128. }
  1129. }
  1130. if (ret != IOCB_TIMEDOUT)
  1131. lpfc_release_scsi_buf(phba, lpfc_cmd);
  1132. if (err_count == 0)
  1133. ret = SUCCESS;
  1134. else
  1135. ret = FAILED;
  1136. /*
  1137. * All outstanding txcmplq I/Os should have been aborted by
  1138. * the targets. Unfortunately, some targets do not abide by
  1139. * this forcing the driver to double check.
  1140. */
  1141. cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
  1142. if (cnt)
  1143. lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
  1144. 0, 0, LPFC_CTX_HOST);
  1145. loopcnt = 0;
  1146. while(cnt) {
  1147. schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
  1148. if (++loopcnt
  1149. > (2 * vport->cfg_devloss_tmo)/LPFC_RESET_WAIT)
  1150. break;
  1151. cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
  1152. }
  1153. if (cnt) {
  1154. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  1155. "0715 Bus Reset I/O flush failure: "
  1156. "cnt x%x left x%x\n", cnt, i);
  1157. ret = FAILED;
  1158. }
  1159. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  1160. "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
  1161. out:
  1162. return ret;
  1163. }
  1164. static int
  1165. lpfc_slave_alloc(struct scsi_device *sdev)
  1166. {
  1167. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  1168. struct lpfc_hba *phba = vport->phba;
  1169. struct lpfc_scsi_buf *scsi_buf = NULL;
  1170. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1171. uint32_t total = 0, i;
  1172. uint32_t num_to_alloc = 0;
  1173. unsigned long flags;
  1174. if (!rport || fc_remote_port_chkready(rport))
  1175. return -ENXIO;
  1176. sdev->hostdata = rport->dd_data;
  1177. /*
  1178. * Populate the cmds_per_lun count scsi_bufs into this host's globally
  1179. * available list of scsi buffers. Don't allocate more than the
  1180. * HBA limit conveyed to the midlayer via the host structure. The
  1181. * formula accounts for the lun_queue_depth + error handlers + 1
  1182. * extra. This list of scsi bufs exists for the lifetime of the driver.
  1183. */
  1184. total = phba->total_scsi_bufs;
  1185. num_to_alloc = vport->cfg_lun_queue_depth + 2;
  1186. /* Allow some exchanges to be available always to complete discovery */
  1187. if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
  1188. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  1189. "0704 At limitation of %d preallocated "
  1190. "command buffers\n", total);
  1191. return 0;
  1192. /* Allow some exchanges to be available always to complete discovery */
  1193. } else if (total + num_to_alloc >
  1194. phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
  1195. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  1196. "0705 Allocation request of %d "
  1197. "command buffers will exceed max of %d. "
  1198. "Reducing allocation request to %d.\n",
  1199. num_to_alloc, phba->cfg_hba_queue_depth,
  1200. (phba->cfg_hba_queue_depth - total));
  1201. num_to_alloc = phba->cfg_hba_queue_depth - total;
  1202. }
  1203. for (i = 0; i < num_to_alloc; i++) {
  1204. scsi_buf = lpfc_new_scsi_buf(vport);
  1205. if (!scsi_buf) {
  1206. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  1207. "0706 Failed to allocate "
  1208. "command buffer\n");
  1209. break;
  1210. }
  1211. spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
  1212. phba->total_scsi_bufs++;
  1213. list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
  1214. spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
  1215. }
  1216. return 0;
  1217. }
  1218. static int
  1219. lpfc_slave_configure(struct scsi_device *sdev)
  1220. {
  1221. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  1222. struct lpfc_hba *phba = vport->phba;
  1223. struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
  1224. if (sdev->tagged_supported)
  1225. scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
  1226. else
  1227. scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
  1228. /*
  1229. * Initialize the fc transport attributes for the target
  1230. * containing this scsi device. Also note that the driver's
  1231. * target pointer is stored in the starget_data for the
  1232. * driver's sysfs entry point functions.
  1233. */
  1234. rport->dev_loss_tmo = vport->cfg_devloss_tmo;
  1235. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  1236. lpfc_sli_poll_fcp_ring(phba);
  1237. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  1238. lpfc_poll_rearm_timer(phba);
  1239. }
  1240. return 0;
  1241. }
  1242. static void
  1243. lpfc_slave_destroy(struct scsi_device *sdev)
  1244. {
  1245. sdev->hostdata = NULL;
  1246. return;
  1247. }
  1248. struct scsi_host_template lpfc_template = {
  1249. .module = THIS_MODULE,
  1250. .name = LPFC_DRIVER_NAME,
  1251. .info = lpfc_info,
  1252. .queuecommand = lpfc_queuecommand,
  1253. .eh_abort_handler = lpfc_abort_handler,
  1254. .eh_device_reset_handler= lpfc_device_reset_handler,
  1255. .eh_bus_reset_handler = lpfc_bus_reset_handler,
  1256. .slave_alloc = lpfc_slave_alloc,
  1257. .slave_configure = lpfc_slave_configure,
  1258. .slave_destroy = lpfc_slave_destroy,
  1259. .scan_finished = lpfc_scan_finished,
  1260. .this_id = -1,
  1261. .sg_tablesize = LPFC_SG_SEG_CNT,
  1262. .use_sg_chaining = ENABLE_SG_CHAINING,
  1263. .cmd_per_lun = LPFC_CMD_PER_LUN,
  1264. .use_clustering = ENABLE_CLUSTERING,
  1265. .shost_attrs = lpfc_hba_attrs,
  1266. .max_sectors = 0xFFFF,
  1267. };
  1268. struct scsi_host_template lpfc_vport_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_SG_SEG_CNT,
  1282. .cmd_per_lun = LPFC_CMD_PER_LUN,
  1283. .use_clustering = ENABLE_CLUSTERING,
  1284. .use_sg_chaining = ENABLE_SG_CHAINING,
  1285. .shost_attrs = lpfc_vport_attrs,
  1286. .max_sectors = 0xFFFF,
  1287. };