lpfc_scsi.c 41 KB

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