lpfc_scsi.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2008 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. uint32_t evt_posted;
  49. spin_lock_irqsave(&phba->hbalock, flags);
  50. atomic_inc(&phba->num_rsrc_err);
  51. phba->last_rsrc_error_time = jiffies;
  52. if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
  53. spin_unlock_irqrestore(&phba->hbalock, flags);
  54. return;
  55. }
  56. phba->last_ramp_down_time = jiffies;
  57. spin_unlock_irqrestore(&phba->hbalock, flags);
  58. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  59. evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
  60. if (!evt_posted)
  61. phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
  62. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  63. if (!evt_posted)
  64. lpfc_worker_wake_up(phba);
  65. return;
  66. }
  67. /*
  68. * This function is called with no lock held when there is a successful
  69. * SCSI command completion.
  70. */
  71. static inline void
  72. lpfc_rampup_queue_depth(struct lpfc_vport *vport,
  73. struct scsi_device *sdev)
  74. {
  75. unsigned long flags;
  76. struct lpfc_hba *phba = vport->phba;
  77. uint32_t evt_posted;
  78. atomic_inc(&phba->num_cmd_success);
  79. if (vport->cfg_lun_queue_depth <= sdev->queue_depth)
  80. return;
  81. spin_lock_irqsave(&phba->hbalock, flags);
  82. if (((phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) > jiffies) ||
  83. ((phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL ) > jiffies)) {
  84. spin_unlock_irqrestore(&phba->hbalock, flags);
  85. return;
  86. }
  87. phba->last_ramp_up_time = jiffies;
  88. spin_unlock_irqrestore(&phba->hbalock, flags);
  89. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  90. evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
  91. if (!evt_posted)
  92. phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
  93. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  94. if (!evt_posted)
  95. lpfc_worker_wake_up(phba);
  96. return;
  97. }
  98. void
  99. lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
  100. {
  101. struct lpfc_vport **vports;
  102. struct Scsi_Host *shost;
  103. struct scsi_device *sdev;
  104. unsigned long new_queue_depth;
  105. unsigned long num_rsrc_err, num_cmd_success;
  106. int i;
  107. num_rsrc_err = atomic_read(&phba->num_rsrc_err);
  108. num_cmd_success = atomic_read(&phba->num_cmd_success);
  109. vports = lpfc_create_vport_work_array(phba);
  110. if (vports != NULL)
  111. for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  112. shost = lpfc_shost_from_vport(vports[i]);
  113. shost_for_each_device(sdev, shost) {
  114. new_queue_depth =
  115. sdev->queue_depth * num_rsrc_err /
  116. (num_rsrc_err + num_cmd_success);
  117. if (!new_queue_depth)
  118. new_queue_depth = sdev->queue_depth - 1;
  119. else
  120. new_queue_depth = sdev->queue_depth -
  121. new_queue_depth;
  122. if (sdev->ordered_tags)
  123. scsi_adjust_queue_depth(sdev,
  124. MSG_ORDERED_TAG,
  125. new_queue_depth);
  126. else
  127. scsi_adjust_queue_depth(sdev,
  128. MSG_SIMPLE_TAG,
  129. new_queue_depth);
  130. }
  131. }
  132. lpfc_destroy_vport_work_array(phba, vports);
  133. atomic_set(&phba->num_rsrc_err, 0);
  134. atomic_set(&phba->num_cmd_success, 0);
  135. }
  136. void
  137. lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
  138. {
  139. struct lpfc_vport **vports;
  140. struct Scsi_Host *shost;
  141. struct scsi_device *sdev;
  142. int i;
  143. vports = lpfc_create_vport_work_array(phba);
  144. if (vports != NULL)
  145. for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  146. shost = lpfc_shost_from_vport(vports[i]);
  147. shost_for_each_device(sdev, shost) {
  148. if (vports[i]->cfg_lun_queue_depth <=
  149. sdev->queue_depth)
  150. continue;
  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 || !NLP_CHK_NODE_ACT(pnode)
  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 && NLP_CHK_NODE_ACT(pnode) &&
  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 &&
  588. NLP_CHK_NODE_ACT(pnode)) {
  589. pnode->last_q_full_time = jiffies;
  590. shost_for_each_device(tmp_sdev, sdev->host) {
  591. if (tmp_sdev->id != sdev->id)
  592. continue;
  593. depth = scsi_track_queue_full(tmp_sdev,
  594. tmp_sdev->queue_depth - 1);
  595. }
  596. /*
  597. * The queue depth cannot be lowered any more.
  598. * Modify the returned error code to store
  599. * the final depth value set by
  600. * scsi_track_queue_full.
  601. */
  602. if (depth == -1)
  603. depth = sdev->host->cmd_per_lun;
  604. if (depth) {
  605. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  606. "0711 detected queue full - lun queue "
  607. "depth adjusted to %d.\n", depth);
  608. }
  609. }
  610. /*
  611. * If there is a thread waiting for command completion
  612. * wake up the thread.
  613. */
  614. spin_lock_irqsave(sdev->host->host_lock, flags);
  615. lpfc_cmd->pCmd = NULL;
  616. if (lpfc_cmd->waitq)
  617. wake_up(lpfc_cmd->waitq);
  618. spin_unlock_irqrestore(sdev->host->host_lock, flags);
  619. lpfc_release_scsi_buf(phba, lpfc_cmd);
  620. }
  621. static void
  622. lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
  623. struct lpfc_nodelist *pnode)
  624. {
  625. struct lpfc_hba *phba = vport->phba;
  626. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  627. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  628. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  629. struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
  630. int datadir = scsi_cmnd->sc_data_direction;
  631. char tag[2];
  632. if (!pnode || !NLP_CHK_NODE_ACT(pnode))
  633. return;
  634. lpfc_cmd->fcp_rsp->rspSnsLen = 0;
  635. /* clear task management bits */
  636. lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
  637. int_to_scsilun(lpfc_cmd->pCmd->device->lun,
  638. &lpfc_cmd->fcp_cmnd->fcp_lun);
  639. memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
  640. if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
  641. switch (tag[0]) {
  642. case HEAD_OF_QUEUE_TAG:
  643. fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
  644. break;
  645. case ORDERED_QUEUE_TAG:
  646. fcp_cmnd->fcpCntl1 = ORDERED_Q;
  647. break;
  648. default:
  649. fcp_cmnd->fcpCntl1 = SIMPLE_Q;
  650. break;
  651. }
  652. } else
  653. fcp_cmnd->fcpCntl1 = 0;
  654. /*
  655. * There are three possibilities here - use scatter-gather segment, use
  656. * the single mapping, or neither. Start the lpfc command prep by
  657. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  658. * data bde entry.
  659. */
  660. if (scsi_sg_count(scsi_cmnd)) {
  661. if (datadir == DMA_TO_DEVICE) {
  662. iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
  663. iocb_cmd->un.fcpi.fcpi_parm = 0;
  664. iocb_cmd->ulpPU = 0;
  665. fcp_cmnd->fcpCntl3 = WRITE_DATA;
  666. phba->fc4OutputRequests++;
  667. } else {
  668. iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
  669. iocb_cmd->ulpPU = PARM_READ_CHECK;
  670. iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
  671. fcp_cmnd->fcpCntl3 = READ_DATA;
  672. phba->fc4InputRequests++;
  673. }
  674. } else {
  675. iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
  676. iocb_cmd->un.fcpi.fcpi_parm = 0;
  677. iocb_cmd->ulpPU = 0;
  678. fcp_cmnd->fcpCntl3 = 0;
  679. phba->fc4ControlRequests++;
  680. }
  681. /*
  682. * Finish initializing those IOCB fields that are independent
  683. * of the scsi_cmnd request_buffer
  684. */
  685. piocbq->iocb.ulpContext = pnode->nlp_rpi;
  686. if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
  687. piocbq->iocb.ulpFCP2Rcvy = 1;
  688. else
  689. piocbq->iocb.ulpFCP2Rcvy = 0;
  690. piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
  691. piocbq->context1 = lpfc_cmd;
  692. piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
  693. piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
  694. piocbq->vport = vport;
  695. }
  696. static int
  697. lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
  698. struct lpfc_scsi_buf *lpfc_cmd,
  699. unsigned int lun,
  700. uint8_t task_mgmt_cmd)
  701. {
  702. struct lpfc_iocbq *piocbq;
  703. IOCB_t *piocb;
  704. struct fcp_cmnd *fcp_cmnd;
  705. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  706. struct lpfc_nodelist *ndlp = rdata->pnode;
  707. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
  708. ndlp->nlp_state != NLP_STE_MAPPED_NODE)
  709. return 0;
  710. piocbq = &(lpfc_cmd->cur_iocbq);
  711. piocbq->vport = vport;
  712. piocb = &piocbq->iocb;
  713. fcp_cmnd = lpfc_cmd->fcp_cmnd;
  714. int_to_scsilun(lun, &lpfc_cmd->fcp_cmnd->fcp_lun);
  715. fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
  716. piocb->ulpCommand = CMD_FCP_ICMND64_CR;
  717. piocb->ulpContext = ndlp->nlp_rpi;
  718. if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
  719. piocb->ulpFCP2Rcvy = 1;
  720. }
  721. piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
  722. /* ulpTimeout is only one byte */
  723. if (lpfc_cmd->timeout > 0xff) {
  724. /*
  725. * Do not timeout the command at the firmware level.
  726. * The driver will provide the timeout mechanism.
  727. */
  728. piocb->ulpTimeout = 0;
  729. } else {
  730. piocb->ulpTimeout = lpfc_cmd->timeout;
  731. }
  732. return 1;
  733. }
  734. static void
  735. lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
  736. struct lpfc_iocbq *cmdiocbq,
  737. struct lpfc_iocbq *rspiocbq)
  738. {
  739. struct lpfc_scsi_buf *lpfc_cmd =
  740. (struct lpfc_scsi_buf *) cmdiocbq->context1;
  741. if (lpfc_cmd)
  742. lpfc_release_scsi_buf(phba, lpfc_cmd);
  743. return;
  744. }
  745. static int
  746. lpfc_scsi_tgt_reset(struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_vport *vport,
  747. unsigned tgt_id, unsigned int lun,
  748. struct lpfc_rport_data *rdata)
  749. {
  750. struct lpfc_hba *phba = vport->phba;
  751. struct lpfc_iocbq *iocbq;
  752. struct lpfc_iocbq *iocbqrsp;
  753. int ret;
  754. int status;
  755. if (!rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
  756. return FAILED;
  757. lpfc_cmd->rdata = rdata;
  758. status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun,
  759. FCP_TARGET_RESET);
  760. if (!status)
  761. return FAILED;
  762. iocbq = &lpfc_cmd->cur_iocbq;
  763. iocbqrsp = lpfc_sli_get_iocbq(phba);
  764. if (!iocbqrsp)
  765. return FAILED;
  766. /* Issue Target Reset to TGT <num> */
  767. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  768. "0702 Issue Target Reset to TGT %d Data: x%x x%x\n",
  769. tgt_id, rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
  770. status = lpfc_sli_issue_iocb_wait(phba,
  771. &phba->sli.ring[phba->sli.fcp_ring],
  772. iocbq, iocbqrsp, lpfc_cmd->timeout);
  773. if (status != IOCB_SUCCESS) {
  774. if (status == IOCB_TIMEDOUT) {
  775. iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
  776. ret = TIMEOUT_ERROR;
  777. } else
  778. ret = FAILED;
  779. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  780. } else {
  781. ret = SUCCESS;
  782. lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
  783. lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
  784. if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
  785. (lpfc_cmd->result & IOERR_DRVR_MASK))
  786. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  787. }
  788. lpfc_sli_release_iocbq(phba, iocbqrsp);
  789. return ret;
  790. }
  791. const char *
  792. lpfc_info(struct Scsi_Host *host)
  793. {
  794. struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
  795. struct lpfc_hba *phba = vport->phba;
  796. int len;
  797. static char lpfcinfobuf[384];
  798. memset(lpfcinfobuf,0,384);
  799. if (phba && phba->pcidev){
  800. strncpy(lpfcinfobuf, phba->ModelDesc, 256);
  801. len = strlen(lpfcinfobuf);
  802. snprintf(lpfcinfobuf + len,
  803. 384-len,
  804. " on PCI bus %02x device %02x irq %d",
  805. phba->pcidev->bus->number,
  806. phba->pcidev->devfn,
  807. phba->pcidev->irq);
  808. len = strlen(lpfcinfobuf);
  809. if (phba->Port[0]) {
  810. snprintf(lpfcinfobuf + len,
  811. 384-len,
  812. " port %s",
  813. phba->Port);
  814. }
  815. }
  816. return lpfcinfobuf;
  817. }
  818. static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
  819. {
  820. unsigned long poll_tmo_expires =
  821. (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
  822. if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
  823. mod_timer(&phba->fcp_poll_timer,
  824. poll_tmo_expires);
  825. }
  826. void lpfc_poll_start_timer(struct lpfc_hba * phba)
  827. {
  828. lpfc_poll_rearm_timer(phba);
  829. }
  830. void lpfc_poll_timeout(unsigned long ptr)
  831. {
  832. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  833. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  834. lpfc_sli_poll_fcp_ring (phba);
  835. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  836. lpfc_poll_rearm_timer(phba);
  837. }
  838. }
  839. static int
  840. lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
  841. {
  842. struct Scsi_Host *shost = cmnd->device->host;
  843. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  844. struct lpfc_hba *phba = vport->phba;
  845. struct lpfc_sli *psli = &phba->sli;
  846. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  847. struct lpfc_nodelist *ndlp = rdata->pnode;
  848. struct lpfc_scsi_buf *lpfc_cmd;
  849. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  850. int err;
  851. err = fc_remote_port_chkready(rport);
  852. if (err) {
  853. cmnd->result = err;
  854. goto out_fail_command;
  855. }
  856. /*
  857. * Catch race where our node has transitioned, but the
  858. * transport is still transitioning.
  859. */
  860. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  861. cmnd->result = ScsiResult(DID_BUS_BUSY, 0);
  862. goto out_fail_command;
  863. }
  864. lpfc_cmd = lpfc_get_scsi_buf(phba);
  865. if (lpfc_cmd == NULL) {
  866. lpfc_adjust_queue_depth(phba);
  867. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  868. "0707 driver's buffer pool is empty, "
  869. "IO busied\n");
  870. goto out_host_busy;
  871. }
  872. /*
  873. * Store the midlayer's command structure for the completion phase
  874. * and complete the command initialization.
  875. */
  876. lpfc_cmd->pCmd = cmnd;
  877. lpfc_cmd->rdata = rdata;
  878. lpfc_cmd->timeout = 0;
  879. cmnd->host_scribble = (unsigned char *)lpfc_cmd;
  880. cmnd->scsi_done = done;
  881. err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
  882. if (err)
  883. goto out_host_busy_free_buf;
  884. lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
  885. err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
  886. &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
  887. if (err)
  888. goto out_host_busy_free_buf;
  889. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  890. lpfc_sli_poll_fcp_ring(phba);
  891. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  892. lpfc_poll_rearm_timer(phba);
  893. }
  894. return 0;
  895. out_host_busy_free_buf:
  896. lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
  897. lpfc_release_scsi_buf(phba, lpfc_cmd);
  898. out_host_busy:
  899. return SCSI_MLQUEUE_HOST_BUSY;
  900. out_fail_command:
  901. done(cmnd);
  902. return 0;
  903. }
  904. static void
  905. lpfc_block_error_handler(struct scsi_cmnd *cmnd)
  906. {
  907. struct Scsi_Host *shost = cmnd->device->host;
  908. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  909. spin_lock_irq(shost->host_lock);
  910. while (rport->port_state == FC_PORTSTATE_BLOCKED) {
  911. spin_unlock_irq(shost->host_lock);
  912. msleep(1000);
  913. spin_lock_irq(shost->host_lock);
  914. }
  915. spin_unlock_irq(shost->host_lock);
  916. return;
  917. }
  918. static int
  919. lpfc_abort_handler(struct scsi_cmnd *cmnd)
  920. {
  921. struct Scsi_Host *shost = cmnd->device->host;
  922. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  923. struct lpfc_hba *phba = vport->phba;
  924. struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
  925. struct lpfc_iocbq *iocb;
  926. struct lpfc_iocbq *abtsiocb;
  927. struct lpfc_scsi_buf *lpfc_cmd;
  928. IOCB_t *cmd, *icmd;
  929. int ret = SUCCESS;
  930. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
  931. lpfc_block_error_handler(cmnd);
  932. lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
  933. BUG_ON(!lpfc_cmd);
  934. /*
  935. * If pCmd field of the corresponding lpfc_scsi_buf structure
  936. * points to a different SCSI command, then the driver has
  937. * already completed this command, but the midlayer did not
  938. * see the completion before the eh fired. Just return
  939. * SUCCESS.
  940. */
  941. iocb = &lpfc_cmd->cur_iocbq;
  942. if (lpfc_cmd->pCmd != cmnd)
  943. goto out;
  944. BUG_ON(iocb->context1 != lpfc_cmd);
  945. abtsiocb = lpfc_sli_get_iocbq(phba);
  946. if (abtsiocb == NULL) {
  947. ret = FAILED;
  948. goto out;
  949. }
  950. /*
  951. * The scsi command can not be in txq and it is in flight because the
  952. * pCmd is still pointig at the SCSI command we have to abort. There
  953. * is no need to search the txcmplq. Just send an abort to the FW.
  954. */
  955. cmd = &iocb->iocb;
  956. icmd = &abtsiocb->iocb;
  957. icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
  958. icmd->un.acxri.abortContextTag = cmd->ulpContext;
  959. icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
  960. icmd->ulpLe = 1;
  961. icmd->ulpClass = cmd->ulpClass;
  962. if (lpfc_is_link_up(phba))
  963. icmd->ulpCommand = CMD_ABORT_XRI_CN;
  964. else
  965. icmd->ulpCommand = CMD_CLOSE_XRI_CN;
  966. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  967. abtsiocb->vport = vport;
  968. if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
  969. lpfc_sli_release_iocbq(phba, abtsiocb);
  970. ret = FAILED;
  971. goto out;
  972. }
  973. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  974. lpfc_sli_poll_fcp_ring (phba);
  975. lpfc_cmd->waitq = &waitq;
  976. /* Wait for abort to complete */
  977. wait_event_timeout(waitq,
  978. (lpfc_cmd->pCmd != cmnd),
  979. (2*vport->cfg_devloss_tmo*HZ));
  980. spin_lock_irq(shost->host_lock);
  981. lpfc_cmd->waitq = NULL;
  982. spin_unlock_irq(shost->host_lock);
  983. if (lpfc_cmd->pCmd == cmnd) {
  984. ret = FAILED;
  985. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  986. "0748 abort handler timed out waiting "
  987. "for abort to complete: ret %#x, ID %d, "
  988. "LUN %d, snum %#lx\n",
  989. ret, cmnd->device->id, cmnd->device->lun,
  990. cmnd->serial_number);
  991. }
  992. out:
  993. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  994. "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
  995. "LUN %d snum %#lx\n", ret, cmnd->device->id,
  996. cmnd->device->lun, cmnd->serial_number);
  997. return ret;
  998. }
  999. static int
  1000. lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
  1001. {
  1002. struct Scsi_Host *shost = cmnd->device->host;
  1003. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  1004. struct lpfc_hba *phba = vport->phba;
  1005. struct lpfc_scsi_buf *lpfc_cmd;
  1006. struct lpfc_iocbq *iocbq, *iocbqrsp;
  1007. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  1008. struct lpfc_nodelist *pnode = rdata->pnode;
  1009. unsigned long later;
  1010. int ret = SUCCESS;
  1011. int status;
  1012. int cnt;
  1013. lpfc_block_error_handler(cmnd);
  1014. /*
  1015. * If target is not in a MAPPED state, delay the reset until
  1016. * target is rediscovered or devloss timeout expires.
  1017. */
  1018. later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
  1019. while (time_after(later, jiffies)) {
  1020. if (!pnode || !NLP_CHK_NODE_ACT(pnode))
  1021. return FAILED;
  1022. if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
  1023. break;
  1024. schedule_timeout_uninterruptible(msecs_to_jiffies(500));
  1025. rdata = cmnd->device->hostdata;
  1026. if (!rdata)
  1027. break;
  1028. pnode = rdata->pnode;
  1029. }
  1030. if (!rdata || pnode->nlp_state != NLP_STE_MAPPED_NODE) {
  1031. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  1032. "0721 LUN Reset rport "
  1033. "failure: msec x%x rdata x%p\n",
  1034. jiffies_to_msecs(jiffies - later), rdata);
  1035. return FAILED;
  1036. }
  1037. lpfc_cmd = lpfc_get_scsi_buf(phba);
  1038. if (lpfc_cmd == NULL)
  1039. return FAILED;
  1040. lpfc_cmd->timeout = 60;
  1041. lpfc_cmd->rdata = rdata;
  1042. status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd,
  1043. cmnd->device->lun,
  1044. FCP_TARGET_RESET);
  1045. if (!status) {
  1046. lpfc_release_scsi_buf(phba, lpfc_cmd);
  1047. return FAILED;
  1048. }
  1049. iocbq = &lpfc_cmd->cur_iocbq;
  1050. /* get a buffer for this IOCB command response */
  1051. iocbqrsp = lpfc_sli_get_iocbq(phba);
  1052. if (iocbqrsp == NULL) {
  1053. lpfc_release_scsi_buf(phba, lpfc_cmd);
  1054. return FAILED;
  1055. }
  1056. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  1057. "0703 Issue target reset to TGT %d LUN %d "
  1058. "rpi x%x nlp_flag x%x\n", cmnd->device->id,
  1059. cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
  1060. status = lpfc_sli_issue_iocb_wait(phba,
  1061. &phba->sli.ring[phba->sli.fcp_ring],
  1062. iocbq, iocbqrsp, lpfc_cmd->timeout);
  1063. if (status == IOCB_TIMEDOUT) {
  1064. iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
  1065. ret = TIMEOUT_ERROR;
  1066. } else {
  1067. if (status != IOCB_SUCCESS)
  1068. ret = FAILED;
  1069. lpfc_release_scsi_buf(phba, lpfc_cmd);
  1070. }
  1071. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  1072. "0713 SCSI layer issued device reset (%d, %d) "
  1073. "return x%x status x%x result x%x\n",
  1074. cmnd->device->id, cmnd->device->lun, ret,
  1075. iocbqrsp->iocb.ulpStatus,
  1076. iocbqrsp->iocb.un.ulpWord[4]);
  1077. lpfc_sli_release_iocbq(phba, iocbqrsp);
  1078. cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id, cmnd->device->lun,
  1079. LPFC_CTX_TGT);
  1080. if (cnt)
  1081. lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
  1082. cmnd->device->id, cmnd->device->lun,
  1083. LPFC_CTX_TGT);
  1084. later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
  1085. while (time_after(later, jiffies) && cnt) {
  1086. schedule_timeout_uninterruptible(msecs_to_jiffies(20));
  1087. cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id,
  1088. cmnd->device->lun, LPFC_CTX_TGT);
  1089. }
  1090. if (cnt) {
  1091. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  1092. "0719 device reset I/O flush failure: "
  1093. "cnt x%x\n", cnt);
  1094. ret = FAILED;
  1095. }
  1096. return ret;
  1097. }
  1098. static int
  1099. lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
  1100. {
  1101. struct Scsi_Host *shost = cmnd->device->host;
  1102. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  1103. struct lpfc_hba *phba = vport->phba;
  1104. struct lpfc_nodelist *ndlp = NULL;
  1105. int match;
  1106. int ret = SUCCESS, status, i;
  1107. int cnt;
  1108. struct lpfc_scsi_buf * lpfc_cmd;
  1109. unsigned long later;
  1110. lpfc_block_error_handler(cmnd);
  1111. /*
  1112. * Since the driver manages a single bus device, reset all
  1113. * targets known to the driver. Should any target reset
  1114. * fail, this routine returns failure to the midlayer.
  1115. */
  1116. for (i = 0; i < LPFC_MAX_TARGET; i++) {
  1117. /* Search for mapped node by target ID */
  1118. match = 0;
  1119. spin_lock_irq(shost->host_lock);
  1120. list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
  1121. if (!NLP_CHK_NODE_ACT(ndlp))
  1122. continue;
  1123. if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
  1124. ndlp->nlp_sid == i &&
  1125. ndlp->rport) {
  1126. match = 1;
  1127. break;
  1128. }
  1129. }
  1130. spin_unlock_irq(shost->host_lock);
  1131. if (!match)
  1132. continue;
  1133. lpfc_cmd = lpfc_get_scsi_buf(phba);
  1134. if (lpfc_cmd) {
  1135. lpfc_cmd->timeout = 60;
  1136. status = lpfc_scsi_tgt_reset(lpfc_cmd, vport, i,
  1137. cmnd->device->lun,
  1138. ndlp->rport->dd_data);
  1139. if (status != TIMEOUT_ERROR)
  1140. lpfc_release_scsi_buf(phba, lpfc_cmd);
  1141. }
  1142. if (!lpfc_cmd || status != SUCCESS) {
  1143. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  1144. "0700 Bus Reset on target %d failed\n",
  1145. i);
  1146. ret = FAILED;
  1147. }
  1148. }
  1149. /*
  1150. * All outstanding txcmplq I/Os should have been aborted by
  1151. * the targets. Unfortunately, some targets do not abide by
  1152. * this forcing the driver to double check.
  1153. */
  1154. cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
  1155. if (cnt)
  1156. lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
  1157. 0, 0, LPFC_CTX_HOST);
  1158. later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
  1159. while (time_after(later, jiffies) && cnt) {
  1160. schedule_timeout_uninterruptible(msecs_to_jiffies(20));
  1161. cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
  1162. }
  1163. if (cnt) {
  1164. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  1165. "0715 Bus Reset I/O flush failure: "
  1166. "cnt x%x left x%x\n", cnt, i);
  1167. ret = FAILED;
  1168. }
  1169. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  1170. "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
  1171. return ret;
  1172. }
  1173. static int
  1174. lpfc_slave_alloc(struct scsi_device *sdev)
  1175. {
  1176. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  1177. struct lpfc_hba *phba = vport->phba;
  1178. struct lpfc_scsi_buf *scsi_buf = NULL;
  1179. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1180. uint32_t total = 0, i;
  1181. uint32_t num_to_alloc = 0;
  1182. unsigned long flags;
  1183. if (!rport || fc_remote_port_chkready(rport))
  1184. return -ENXIO;
  1185. sdev->hostdata = rport->dd_data;
  1186. /*
  1187. * Populate the cmds_per_lun count scsi_bufs into this host's globally
  1188. * available list of scsi buffers. Don't allocate more than the
  1189. * HBA limit conveyed to the midlayer via the host structure. The
  1190. * formula accounts for the lun_queue_depth + error handlers + 1
  1191. * extra. This list of scsi bufs exists for the lifetime of the driver.
  1192. */
  1193. total = phba->total_scsi_bufs;
  1194. num_to_alloc = vport->cfg_lun_queue_depth + 2;
  1195. /* Allow some exchanges to be available always to complete discovery */
  1196. if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
  1197. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  1198. "0704 At limitation of %d preallocated "
  1199. "command buffers\n", total);
  1200. return 0;
  1201. /* Allow some exchanges to be available always to complete discovery */
  1202. } else if (total + num_to_alloc >
  1203. phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
  1204. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  1205. "0705 Allocation request of %d "
  1206. "command buffers will exceed max of %d. "
  1207. "Reducing allocation request to %d.\n",
  1208. num_to_alloc, phba->cfg_hba_queue_depth,
  1209. (phba->cfg_hba_queue_depth - total));
  1210. num_to_alloc = phba->cfg_hba_queue_depth - total;
  1211. }
  1212. for (i = 0; i < num_to_alloc; i++) {
  1213. scsi_buf = lpfc_new_scsi_buf(vport);
  1214. if (!scsi_buf) {
  1215. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  1216. "0706 Failed to allocate "
  1217. "command buffer\n");
  1218. break;
  1219. }
  1220. spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
  1221. phba->total_scsi_bufs++;
  1222. list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
  1223. spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
  1224. }
  1225. return 0;
  1226. }
  1227. static int
  1228. lpfc_slave_configure(struct scsi_device *sdev)
  1229. {
  1230. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  1231. struct lpfc_hba *phba = vport->phba;
  1232. struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
  1233. if (sdev->tagged_supported)
  1234. scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
  1235. else
  1236. scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
  1237. /*
  1238. * Initialize the fc transport attributes for the target
  1239. * containing this scsi device. Also note that the driver's
  1240. * target pointer is stored in the starget_data for the
  1241. * driver's sysfs entry point functions.
  1242. */
  1243. rport->dev_loss_tmo = vport->cfg_devloss_tmo;
  1244. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  1245. lpfc_sli_poll_fcp_ring(phba);
  1246. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  1247. lpfc_poll_rearm_timer(phba);
  1248. }
  1249. return 0;
  1250. }
  1251. static void
  1252. lpfc_slave_destroy(struct scsi_device *sdev)
  1253. {
  1254. sdev->hostdata = NULL;
  1255. return;
  1256. }
  1257. struct scsi_host_template lpfc_template = {
  1258. .module = THIS_MODULE,
  1259. .name = LPFC_DRIVER_NAME,
  1260. .info = lpfc_info,
  1261. .queuecommand = lpfc_queuecommand,
  1262. .eh_abort_handler = lpfc_abort_handler,
  1263. .eh_device_reset_handler= lpfc_device_reset_handler,
  1264. .eh_bus_reset_handler = lpfc_bus_reset_handler,
  1265. .slave_alloc = lpfc_slave_alloc,
  1266. .slave_configure = lpfc_slave_configure,
  1267. .slave_destroy = lpfc_slave_destroy,
  1268. .scan_finished = lpfc_scan_finished,
  1269. .this_id = -1,
  1270. .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
  1271. .cmd_per_lun = LPFC_CMD_PER_LUN,
  1272. .use_clustering = ENABLE_CLUSTERING,
  1273. .shost_attrs = lpfc_hba_attrs,
  1274. .max_sectors = 0xFFFF,
  1275. };
  1276. struct scsi_host_template lpfc_vport_template = {
  1277. .module = THIS_MODULE,
  1278. .name = LPFC_DRIVER_NAME,
  1279. .info = lpfc_info,
  1280. .queuecommand = lpfc_queuecommand,
  1281. .eh_abort_handler = lpfc_abort_handler,
  1282. .eh_device_reset_handler= lpfc_device_reset_handler,
  1283. .eh_bus_reset_handler = lpfc_bus_reset_handler,
  1284. .slave_alloc = lpfc_slave_alloc,
  1285. .slave_configure = lpfc_slave_configure,
  1286. .slave_destroy = lpfc_slave_destroy,
  1287. .scan_finished = lpfc_scan_finished,
  1288. .this_id = -1,
  1289. .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
  1290. .cmd_per_lun = LPFC_CMD_PER_LUN,
  1291. .use_clustering = ENABLE_CLUSTERING,
  1292. .shost_attrs = lpfc_vport_attrs,
  1293. .max_sectors = 0xFFFF,
  1294. };