PageRenderTime 111ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/scsi/lpfc/lpfc_scsi.c

https://bitbucket.org/abioy/linux
C | 3658 lines | 2408 code | 346 blank | 904 comment | 397 complexity | d91239933dccdd38e2ea663db82a2018 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2009 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/slab.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <asm/unaligned.h>
  26. #include <scsi/scsi.h>
  27. #include <scsi/scsi_device.h>
  28. #include <scsi/scsi_eh.h>
  29. #include <scsi/scsi_host.h>
  30. #include <scsi/scsi_tcq.h>
  31. #include <scsi/scsi_transport_fc.h>
  32. #include "lpfc_version.h"
  33. #include "lpfc_hw4.h"
  34. #include "lpfc_hw.h"
  35. #include "lpfc_sli.h"
  36. #include "lpfc_sli4.h"
  37. #include "lpfc_nl.h"
  38. #include "lpfc_disc.h"
  39. #include "lpfc_scsi.h"
  40. #include "lpfc.h"
  41. #include "lpfc_logmsg.h"
  42. #include "lpfc_crtn.h"
  43. #include "lpfc_vport.h"
  44. #define LPFC_RESET_WAIT 2
  45. #define LPFC_ABORT_WAIT 2
  46. int _dump_buf_done;
  47. static char *dif_op_str[] = {
  48. "SCSI_PROT_NORMAL",
  49. "SCSI_PROT_READ_INSERT",
  50. "SCSI_PROT_WRITE_STRIP",
  51. "SCSI_PROT_READ_STRIP",
  52. "SCSI_PROT_WRITE_INSERT",
  53. "SCSI_PROT_READ_PASS",
  54. "SCSI_PROT_WRITE_PASS",
  55. };
  56. static void
  57. lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
  58. static void
  59. lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
  60. static void
  61. lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
  62. {
  63. void *src, *dst;
  64. struct scatterlist *sgde = scsi_sglist(cmnd);
  65. if (!_dump_buf_data) {
  66. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  67. "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
  68. __func__);
  69. return;
  70. }
  71. if (!sgde) {
  72. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  73. "9051 BLKGRD: ERROR: data scatterlist is null\n");
  74. return;
  75. }
  76. dst = (void *) _dump_buf_data;
  77. while (sgde) {
  78. src = sg_virt(sgde);
  79. memcpy(dst, src, sgde->length);
  80. dst += sgde->length;
  81. sgde = sg_next(sgde);
  82. }
  83. }
  84. static void
  85. lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
  86. {
  87. void *src, *dst;
  88. struct scatterlist *sgde = scsi_prot_sglist(cmnd);
  89. if (!_dump_buf_dif) {
  90. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  91. "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
  92. __func__);
  93. return;
  94. }
  95. if (!sgde) {
  96. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  97. "9053 BLKGRD: ERROR: prot scatterlist is null\n");
  98. return;
  99. }
  100. dst = _dump_buf_dif;
  101. while (sgde) {
  102. src = sg_virt(sgde);
  103. memcpy(dst, src, sgde->length);
  104. dst += sgde->length;
  105. sgde = sg_next(sgde);
  106. }
  107. }
  108. /**
  109. * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
  110. * @phba: Pointer to HBA object.
  111. * @lpfc_cmd: lpfc scsi command object pointer.
  112. *
  113. * This function is called from the lpfc_prep_task_mgmt_cmd function to
  114. * set the last bit in the response sge entry.
  115. **/
  116. static void
  117. lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
  118. struct lpfc_scsi_buf *lpfc_cmd)
  119. {
  120. struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
  121. if (sgl) {
  122. sgl += 1;
  123. sgl->word2 = le32_to_cpu(sgl->word2);
  124. bf_set(lpfc_sli4_sge_last, sgl, 1);
  125. sgl->word2 = cpu_to_le32(sgl->word2);
  126. }
  127. }
  128. /**
  129. * lpfc_update_stats - Update statistical data for the command completion
  130. * @phba: Pointer to HBA object.
  131. * @lpfc_cmd: lpfc scsi command object pointer.
  132. *
  133. * This function is called when there is a command completion and this
  134. * function updates the statistical data for the command completion.
  135. **/
  136. static void
  137. lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
  138. {
  139. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  140. struct lpfc_nodelist *pnode = rdata->pnode;
  141. struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
  142. unsigned long flags;
  143. struct Scsi_Host *shost = cmd->device->host;
  144. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  145. unsigned long latency;
  146. int i;
  147. if (cmd->result)
  148. return;
  149. latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
  150. spin_lock_irqsave(shost->host_lock, flags);
  151. if (!vport->stat_data_enabled ||
  152. vport->stat_data_blocked ||
  153. !pnode->lat_data ||
  154. (phba->bucket_type == LPFC_NO_BUCKET)) {
  155. spin_unlock_irqrestore(shost->host_lock, flags);
  156. return;
  157. }
  158. if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
  159. i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
  160. phba->bucket_step;
  161. /* check array subscript bounds */
  162. if (i < 0)
  163. i = 0;
  164. else if (i >= LPFC_MAX_BUCKET_COUNT)
  165. i = LPFC_MAX_BUCKET_COUNT - 1;
  166. } else {
  167. for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
  168. if (latency <= (phba->bucket_base +
  169. ((1<<i)*phba->bucket_step)))
  170. break;
  171. }
  172. pnode->lat_data[i].cmd_count++;
  173. spin_unlock_irqrestore(shost->host_lock, flags);
  174. }
  175. /**
  176. * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
  177. * @phba: Pointer to HBA context object.
  178. * @vport: Pointer to vport object.
  179. * @ndlp: Pointer to FC node associated with the target.
  180. * @lun: Lun number of the scsi device.
  181. * @old_val: Old value of the queue depth.
  182. * @new_val: New value of the queue depth.
  183. *
  184. * This function sends an event to the mgmt application indicating
  185. * there is a change in the scsi device queue depth.
  186. **/
  187. static void
  188. lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
  189. struct lpfc_vport *vport,
  190. struct lpfc_nodelist *ndlp,
  191. uint32_t lun,
  192. uint32_t old_val,
  193. uint32_t new_val)
  194. {
  195. struct lpfc_fast_path_event *fast_path_evt;
  196. unsigned long flags;
  197. fast_path_evt = lpfc_alloc_fast_evt(phba);
  198. if (!fast_path_evt)
  199. return;
  200. fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
  201. FC_REG_SCSI_EVENT;
  202. fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
  203. LPFC_EVENT_VARQUEDEPTH;
  204. /* Report all luns with change in queue depth */
  205. fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
  206. if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
  207. memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
  208. &ndlp->nlp_portname, sizeof(struct lpfc_name));
  209. memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
  210. &ndlp->nlp_nodename, sizeof(struct lpfc_name));
  211. }
  212. fast_path_evt->un.queue_depth_evt.oldval = old_val;
  213. fast_path_evt->un.queue_depth_evt.newval = new_val;
  214. fast_path_evt->vport = vport;
  215. fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
  216. spin_lock_irqsave(&phba->hbalock, flags);
  217. list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
  218. spin_unlock_irqrestore(&phba->hbalock, flags);
  219. lpfc_worker_wake_up(phba);
  220. return;
  221. }
  222. /**
  223. * lpfc_change_queue_depth - Alter scsi device queue depth
  224. * @sdev: Pointer the scsi device on which to change the queue depth.
  225. * @qdepth: New queue depth to set the sdev to.
  226. * @reason: The reason for the queue depth change.
  227. *
  228. * This function is called by the midlayer and the LLD to alter the queue
  229. * depth for a scsi device. This function sets the queue depth to the new
  230. * value and sends an event out to log the queue depth change.
  231. **/
  232. int
  233. lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
  234. {
  235. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  236. struct lpfc_hba *phba = vport->phba;
  237. struct lpfc_rport_data *rdata;
  238. unsigned long new_queue_depth, old_queue_depth;
  239. old_queue_depth = sdev->queue_depth;
  240. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
  241. new_queue_depth = sdev->queue_depth;
  242. rdata = sdev->hostdata;
  243. if (rdata)
  244. lpfc_send_sdev_queuedepth_change_event(phba, vport,
  245. rdata->pnode, sdev->lun,
  246. old_queue_depth,
  247. new_queue_depth);
  248. return sdev->queue_depth;
  249. }
  250. /**
  251. * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
  252. * @phba: The Hba for which this call is being executed.
  253. *
  254. * This routine is called when there is resource error in driver or firmware.
  255. * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
  256. * posts at most 1 event each second. This routine wakes up worker thread of
  257. * @phba to process WORKER_RAM_DOWN_EVENT event.
  258. *
  259. * This routine should be called with no lock held.
  260. **/
  261. void
  262. lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
  263. {
  264. unsigned long flags;
  265. uint32_t evt_posted;
  266. spin_lock_irqsave(&phba->hbalock, flags);
  267. atomic_inc(&phba->num_rsrc_err);
  268. phba->last_rsrc_error_time = jiffies;
  269. if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
  270. spin_unlock_irqrestore(&phba->hbalock, flags);
  271. return;
  272. }
  273. phba->last_ramp_down_time = jiffies;
  274. spin_unlock_irqrestore(&phba->hbalock, flags);
  275. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  276. evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
  277. if (!evt_posted)
  278. phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
  279. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  280. if (!evt_posted)
  281. lpfc_worker_wake_up(phba);
  282. return;
  283. }
  284. /**
  285. * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
  286. * @phba: The Hba for which this call is being executed.
  287. *
  288. * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
  289. * post at most 1 event every 5 minute after last_ramp_up_time or
  290. * last_rsrc_error_time. This routine wakes up worker thread of @phba
  291. * to process WORKER_RAM_DOWN_EVENT event.
  292. *
  293. * This routine should be called with no lock held.
  294. **/
  295. static inline void
  296. lpfc_rampup_queue_depth(struct lpfc_vport *vport,
  297. uint32_t queue_depth)
  298. {
  299. unsigned long flags;
  300. struct lpfc_hba *phba = vport->phba;
  301. uint32_t evt_posted;
  302. atomic_inc(&phba->num_cmd_success);
  303. if (vport->cfg_lun_queue_depth <= queue_depth)
  304. return;
  305. spin_lock_irqsave(&phba->hbalock, flags);
  306. if (time_before(jiffies,
  307. phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
  308. time_before(jiffies,
  309. phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
  310. spin_unlock_irqrestore(&phba->hbalock, flags);
  311. return;
  312. }
  313. phba->last_ramp_up_time = jiffies;
  314. spin_unlock_irqrestore(&phba->hbalock, flags);
  315. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  316. evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
  317. if (!evt_posted)
  318. phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
  319. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  320. if (!evt_posted)
  321. lpfc_worker_wake_up(phba);
  322. return;
  323. }
  324. /**
  325. * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
  326. * @phba: The Hba for which this call is being executed.
  327. *
  328. * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
  329. * thread.This routine reduces queue depth for all scsi device on each vport
  330. * associated with @phba.
  331. **/
  332. void
  333. lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
  334. {
  335. struct lpfc_vport **vports;
  336. struct Scsi_Host *shost;
  337. struct scsi_device *sdev;
  338. unsigned long new_queue_depth;
  339. unsigned long num_rsrc_err, num_cmd_success;
  340. int i;
  341. num_rsrc_err = atomic_read(&phba->num_rsrc_err);
  342. num_cmd_success = atomic_read(&phba->num_cmd_success);
  343. vports = lpfc_create_vport_work_array(phba);
  344. if (vports != NULL)
  345. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  346. shost = lpfc_shost_from_vport(vports[i]);
  347. shost_for_each_device(sdev, shost) {
  348. new_queue_depth =
  349. sdev->queue_depth * num_rsrc_err /
  350. (num_rsrc_err + num_cmd_success);
  351. if (!new_queue_depth)
  352. new_queue_depth = sdev->queue_depth - 1;
  353. else
  354. new_queue_depth = sdev->queue_depth -
  355. new_queue_depth;
  356. lpfc_change_queue_depth(sdev, new_queue_depth,
  357. SCSI_QDEPTH_DEFAULT);
  358. }
  359. }
  360. lpfc_destroy_vport_work_array(phba, vports);
  361. atomic_set(&phba->num_rsrc_err, 0);
  362. atomic_set(&phba->num_cmd_success, 0);
  363. }
  364. /**
  365. * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
  366. * @phba: The Hba for which this call is being executed.
  367. *
  368. * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
  369. * thread.This routine increases queue depth for all scsi device on each vport
  370. * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
  371. * num_cmd_success to zero.
  372. **/
  373. void
  374. lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
  375. {
  376. struct lpfc_vport **vports;
  377. struct Scsi_Host *shost;
  378. struct scsi_device *sdev;
  379. int i;
  380. vports = lpfc_create_vport_work_array(phba);
  381. if (vports != NULL)
  382. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  383. shost = lpfc_shost_from_vport(vports[i]);
  384. shost_for_each_device(sdev, shost) {
  385. if (vports[i]->cfg_lun_queue_depth <=
  386. sdev->queue_depth)
  387. continue;
  388. lpfc_change_queue_depth(sdev,
  389. sdev->queue_depth+1,
  390. SCSI_QDEPTH_RAMP_UP);
  391. }
  392. }
  393. lpfc_destroy_vport_work_array(phba, vports);
  394. atomic_set(&phba->num_rsrc_err, 0);
  395. atomic_set(&phba->num_cmd_success, 0);
  396. }
  397. /**
  398. * lpfc_scsi_dev_block - set all scsi hosts to block state
  399. * @phba: Pointer to HBA context object.
  400. *
  401. * This function walks vport list and set each SCSI host to block state
  402. * by invoking fc_remote_port_delete() routine. This function is invoked
  403. * with EEH when device's PCI slot has been permanently disabled.
  404. **/
  405. void
  406. lpfc_scsi_dev_block(struct lpfc_hba *phba)
  407. {
  408. struct lpfc_vport **vports;
  409. struct Scsi_Host *shost;
  410. struct scsi_device *sdev;
  411. struct fc_rport *rport;
  412. int i;
  413. vports = lpfc_create_vport_work_array(phba);
  414. if (vports != NULL)
  415. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  416. shost = lpfc_shost_from_vport(vports[i]);
  417. shost_for_each_device(sdev, shost) {
  418. rport = starget_to_rport(scsi_target(sdev));
  419. fc_remote_port_delete(rport);
  420. }
  421. }
  422. lpfc_destroy_vport_work_array(phba, vports);
  423. }
  424. /**
  425. * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
  426. * @vport: The virtual port for which this call being executed.
  427. * @num_to_allocate: The requested number of buffers to allocate.
  428. *
  429. * This routine allocates a scsi buffer for device with SLI-3 interface spec,
  430. * the scsi buffer contains all the necessary information needed to initiate
  431. * a SCSI I/O. The non-DMAable buffer region contains information to build
  432. * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
  433. * and the initial BPL. In addition to allocating memory, the FCP CMND and
  434. * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
  435. *
  436. * Return codes:
  437. * int - number of scsi buffers that were allocated.
  438. * 0 = failure, less than num_to_alloc is a partial failure.
  439. **/
  440. static int
  441. lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
  442. {
  443. struct lpfc_hba *phba = vport->phba;
  444. struct lpfc_scsi_buf *psb;
  445. struct ulp_bde64 *bpl;
  446. IOCB_t *iocb;
  447. dma_addr_t pdma_phys_fcp_cmd;
  448. dma_addr_t pdma_phys_fcp_rsp;
  449. dma_addr_t pdma_phys_bpl;
  450. uint16_t iotag;
  451. int bcnt;
  452. for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
  453. psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
  454. if (!psb)
  455. break;
  456. /*
  457. * Get memory from the pci pool to map the virt space to pci
  458. * bus space for an I/O. The DMA buffer includes space for the
  459. * struct fcp_cmnd, struct fcp_rsp and the number of bde's
  460. * necessary to support the sg_tablesize.
  461. */
  462. psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
  463. GFP_KERNEL, &psb->dma_handle);
  464. if (!psb->data) {
  465. kfree(psb);
  466. break;
  467. }
  468. /* Initialize virtual ptrs to dma_buf region. */
  469. memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
  470. /* Allocate iotag for psb->cur_iocbq. */
  471. iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
  472. if (iotag == 0) {
  473. pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
  474. psb->data, psb->dma_handle);
  475. kfree(psb);
  476. break;
  477. }
  478. psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
  479. psb->fcp_cmnd = psb->data;
  480. psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
  481. psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
  482. sizeof(struct fcp_rsp);
  483. /* Initialize local short-hand pointers. */
  484. bpl = psb->fcp_bpl;
  485. pdma_phys_fcp_cmd = psb->dma_handle;
  486. pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
  487. pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
  488. sizeof(struct fcp_rsp);
  489. /*
  490. * The first two bdes are the FCP_CMD and FCP_RSP. The balance
  491. * are sg list bdes. Initialize the first two and leave the
  492. * rest for queuecommand.
  493. */
  494. bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
  495. bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
  496. bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
  497. bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  498. bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
  499. /* Setup the physical region for the FCP RSP */
  500. bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
  501. bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
  502. bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
  503. bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  504. bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
  505. /*
  506. * Since the IOCB for the FCP I/O is built into this
  507. * lpfc_scsi_buf, initialize it with all known data now.
  508. */
  509. iocb = &psb->cur_iocbq.iocb;
  510. iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
  511. if ((phba->sli_rev == 3) &&
  512. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
  513. /* fill in immediate fcp command BDE */
  514. iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
  515. iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
  516. iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
  517. unsli3.fcp_ext.icd);
  518. iocb->un.fcpi64.bdl.addrHigh = 0;
  519. iocb->ulpBdeCount = 0;
  520. iocb->ulpLe = 0;
  521. /* fill in responce BDE */
  522. iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
  523. BUFF_TYPE_BDE_64;
  524. iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
  525. sizeof(struct fcp_rsp);
  526. iocb->unsli3.fcp_ext.rbde.addrLow =
  527. putPaddrLow(pdma_phys_fcp_rsp);
  528. iocb->unsli3.fcp_ext.rbde.addrHigh =
  529. putPaddrHigh(pdma_phys_fcp_rsp);
  530. } else {
  531. iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
  532. iocb->un.fcpi64.bdl.bdeSize =
  533. (2 * sizeof(struct ulp_bde64));
  534. iocb->un.fcpi64.bdl.addrLow =
  535. putPaddrLow(pdma_phys_bpl);
  536. iocb->un.fcpi64.bdl.addrHigh =
  537. putPaddrHigh(pdma_phys_bpl);
  538. iocb->ulpBdeCount = 1;
  539. iocb->ulpLe = 1;
  540. }
  541. iocb->ulpClass = CLASS3;
  542. psb->status = IOSTAT_SUCCESS;
  543. /* Put it back into the SCSI buffer list */
  544. lpfc_release_scsi_buf_s3(phba, psb);
  545. }
  546. return bcnt;
  547. }
  548. /**
  549. * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
  550. * @phba: pointer to lpfc hba data structure.
  551. * @axri: pointer to the fcp xri abort wcqe structure.
  552. *
  553. * This routine is invoked by the worker thread to process a SLI4 fast-path
  554. * FCP aborted xri.
  555. **/
  556. void
  557. lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
  558. struct sli4_wcqe_xri_aborted *axri)
  559. {
  560. uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
  561. struct lpfc_scsi_buf *psb, *next_psb;
  562. unsigned long iflag = 0;
  563. struct lpfc_iocbq *iocbq;
  564. int i;
  565. spin_lock_irqsave(&phba->hbalock, iflag);
  566. spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
  567. list_for_each_entry_safe(psb, next_psb,
  568. &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
  569. if (psb->cur_iocbq.sli4_xritag == xri) {
  570. list_del(&psb->list);
  571. psb->exch_busy = 0;
  572. psb->status = IOSTAT_SUCCESS;
  573. spin_unlock(
  574. &phba->sli4_hba.abts_scsi_buf_list_lock);
  575. spin_unlock_irqrestore(&phba->hbalock, iflag);
  576. lpfc_release_scsi_buf_s4(phba, psb);
  577. return;
  578. }
  579. }
  580. spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
  581. for (i = 1; i <= phba->sli.last_iotag; i++) {
  582. iocbq = phba->sli.iocbq_lookup[i];
  583. if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
  584. (iocbq->iocb_flag & LPFC_IO_LIBDFC))
  585. continue;
  586. if (iocbq->sli4_xritag != xri)
  587. continue;
  588. psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  589. psb->exch_busy = 0;
  590. spin_unlock_irqrestore(&phba->hbalock, iflag);
  591. return;
  592. }
  593. spin_unlock_irqrestore(&phba->hbalock, iflag);
  594. }
  595. /**
  596. * lpfc_sli4_repost_scsi_sgl_list - Repsot the Scsi buffers sgl pages as block
  597. * @phba: pointer to lpfc hba data structure.
  598. *
  599. * This routine walks the list of scsi buffers that have been allocated and
  600. * repost them to the HBA by using SGL block post. This is needed after a
  601. * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
  602. * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
  603. * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
  604. *
  605. * Returns: 0 = success, non-zero failure.
  606. **/
  607. int
  608. lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
  609. {
  610. struct lpfc_scsi_buf *psb;
  611. int index, status, bcnt = 0, rcnt = 0, rc = 0;
  612. LIST_HEAD(sblist);
  613. for (index = 0; index < phba->sli4_hba.scsi_xri_cnt; index++) {
  614. psb = phba->sli4_hba.lpfc_scsi_psb_array[index];
  615. if (psb) {
  616. /* Remove from SCSI buffer list */
  617. list_del(&psb->list);
  618. /* Add it to a local SCSI buffer list */
  619. list_add_tail(&psb->list, &sblist);
  620. if (++rcnt == LPFC_NEMBED_MBOX_SGL_CNT) {
  621. bcnt = rcnt;
  622. rcnt = 0;
  623. }
  624. } else
  625. /* A hole present in the XRI array, need to skip */
  626. bcnt = rcnt;
  627. if (index == phba->sli4_hba.scsi_xri_cnt - 1)
  628. /* End of XRI array for SCSI buffer, complete */
  629. bcnt = rcnt;
  630. /* Continue until collect up to a nembed page worth of sgls */
  631. if (bcnt == 0)
  632. continue;
  633. /* Now, post the SCSI buffer list sgls as a block */
  634. status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
  635. /* Reset SCSI buffer count for next round of posting */
  636. bcnt = 0;
  637. while (!list_empty(&sblist)) {
  638. list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
  639. list);
  640. if (status) {
  641. /* Put this back on the abort scsi list */
  642. psb->exch_busy = 1;
  643. rc++;
  644. } else {
  645. psb->exch_busy = 0;
  646. psb->status = IOSTAT_SUCCESS;
  647. }
  648. /* Put it back into the SCSI buffer list */
  649. lpfc_release_scsi_buf_s4(phba, psb);
  650. }
  651. }
  652. return rc;
  653. }
  654. /**
  655. * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
  656. * @vport: The virtual port for which this call being executed.
  657. * @num_to_allocate: The requested number of buffers to allocate.
  658. *
  659. * This routine allocates a scsi buffer for device with SLI-4 interface spec,
  660. * the scsi buffer contains all the necessary information needed to initiate
  661. * a SCSI I/O.
  662. *
  663. * Return codes:
  664. * int - number of scsi buffers that were allocated.
  665. * 0 = failure, less than num_to_alloc is a partial failure.
  666. **/
  667. static int
  668. lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
  669. {
  670. struct lpfc_hba *phba = vport->phba;
  671. struct lpfc_scsi_buf *psb;
  672. struct sli4_sge *sgl;
  673. IOCB_t *iocb;
  674. dma_addr_t pdma_phys_fcp_cmd;
  675. dma_addr_t pdma_phys_fcp_rsp;
  676. dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
  677. uint16_t iotag, last_xritag = NO_XRI;
  678. int status = 0, index;
  679. int bcnt;
  680. int non_sequential_xri = 0;
  681. int rc = 0;
  682. LIST_HEAD(sblist);
  683. for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
  684. psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
  685. if (!psb)
  686. break;
  687. /*
  688. * Get memory from the pci pool to map the virt space to pci bus
  689. * space for an I/O. The DMA buffer includes space for the
  690. * struct fcp_cmnd, struct fcp_rsp and the number of bde's
  691. * necessary to support the sg_tablesize.
  692. */
  693. psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
  694. GFP_KERNEL, &psb->dma_handle);
  695. if (!psb->data) {
  696. kfree(psb);
  697. break;
  698. }
  699. /* Initialize virtual ptrs to dma_buf region. */
  700. memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
  701. /* Allocate iotag for psb->cur_iocbq. */
  702. iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
  703. if (iotag == 0) {
  704. kfree(psb);
  705. break;
  706. }
  707. psb->cur_iocbq.sli4_xritag = lpfc_sli4_next_xritag(phba);
  708. if (psb->cur_iocbq.sli4_xritag == NO_XRI) {
  709. pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
  710. psb->data, psb->dma_handle);
  711. kfree(psb);
  712. break;
  713. }
  714. if (last_xritag != NO_XRI
  715. && psb->cur_iocbq.sli4_xritag != (last_xritag+1)) {
  716. non_sequential_xri = 1;
  717. } else
  718. list_add_tail(&psb->list, &sblist);
  719. last_xritag = psb->cur_iocbq.sli4_xritag;
  720. index = phba->sli4_hba.scsi_xri_cnt++;
  721. psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
  722. psb->fcp_bpl = psb->data;
  723. psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
  724. - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
  725. psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
  726. sizeof(struct fcp_cmnd));
  727. /* Initialize local short-hand pointers. */
  728. sgl = (struct sli4_sge *)psb->fcp_bpl;
  729. pdma_phys_bpl = psb->dma_handle;
  730. pdma_phys_fcp_cmd =
  731. (psb->dma_handle + phba->cfg_sg_dma_buf_size)
  732. - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
  733. pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
  734. /*
  735. * The first two bdes are the FCP_CMD and FCP_RSP. The balance
  736. * are sg list bdes. Initialize the first two and leave the
  737. * rest for queuecommand.
  738. */
  739. sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
  740. sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
  741. bf_set(lpfc_sli4_sge_last, sgl, 0);
  742. sgl->word2 = cpu_to_le32(sgl->word2);
  743. sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
  744. sgl++;
  745. /* Setup the physical region for the FCP RSP */
  746. sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
  747. sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
  748. bf_set(lpfc_sli4_sge_last, sgl, 1);
  749. sgl->word2 = cpu_to_le32(sgl->word2);
  750. sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
  751. /*
  752. * Since the IOCB for the FCP I/O is built into this
  753. * lpfc_scsi_buf, initialize it with all known data now.
  754. */
  755. iocb = &psb->cur_iocbq.iocb;
  756. iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
  757. iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
  758. /* setting the BLP size to 2 * sizeof BDE may not be correct.
  759. * We are setting the bpl to point to out sgl. An sgl's
  760. * entries are 16 bytes, a bpl entries are 12 bytes.
  761. */
  762. iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
  763. iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
  764. iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
  765. iocb->ulpBdeCount = 1;
  766. iocb->ulpLe = 1;
  767. iocb->ulpClass = CLASS3;
  768. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  769. pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
  770. else
  771. pdma_phys_bpl1 = 0;
  772. psb->dma_phys_bpl = pdma_phys_bpl;
  773. phba->sli4_hba.lpfc_scsi_psb_array[index] = psb;
  774. if (non_sequential_xri) {
  775. status = lpfc_sli4_post_sgl(phba, pdma_phys_bpl,
  776. pdma_phys_bpl1,
  777. psb->cur_iocbq.sli4_xritag);
  778. if (status) {
  779. /* Put this back on the abort scsi list */
  780. psb->exch_busy = 1;
  781. rc++;
  782. } else {
  783. psb->exch_busy = 0;
  784. psb->status = IOSTAT_SUCCESS;
  785. }
  786. /* Put it back into the SCSI buffer list */
  787. lpfc_release_scsi_buf_s4(phba, psb);
  788. break;
  789. }
  790. }
  791. if (bcnt) {
  792. status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
  793. /* Reset SCSI buffer count for next round of posting */
  794. while (!list_empty(&sblist)) {
  795. list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
  796. list);
  797. if (status) {
  798. /* Put this back on the abort scsi list */
  799. psb->exch_busy = 1;
  800. rc++;
  801. } else {
  802. psb->exch_busy = 0;
  803. psb->status = IOSTAT_SUCCESS;
  804. }
  805. /* Put it back into the SCSI buffer list */
  806. lpfc_release_scsi_buf_s4(phba, psb);
  807. }
  808. }
  809. return bcnt + non_sequential_xri - rc;
  810. }
  811. /**
  812. * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
  813. * @vport: The virtual port for which this call being executed.
  814. * @num_to_allocate: The requested number of buffers to allocate.
  815. *
  816. * This routine wraps the actual SCSI buffer allocator function pointer from
  817. * the lpfc_hba struct.
  818. *
  819. * Return codes:
  820. * int - number of scsi buffers that were allocated.
  821. * 0 = failure, less than num_to_alloc is a partial failure.
  822. **/
  823. static inline int
  824. lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
  825. {
  826. return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
  827. }
  828. /**
  829. * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
  830. * @phba: The HBA for which this call is being executed.
  831. *
  832. * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
  833. * and returns to caller.
  834. *
  835. * Return codes:
  836. * NULL - Error
  837. * Pointer to lpfc_scsi_buf - Success
  838. **/
  839. static struct lpfc_scsi_buf*
  840. lpfc_get_scsi_buf(struct lpfc_hba * phba)
  841. {
  842. struct lpfc_scsi_buf * lpfc_cmd = NULL;
  843. struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
  844. unsigned long iflag = 0;
  845. spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
  846. list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
  847. if (lpfc_cmd) {
  848. lpfc_cmd->seg_cnt = 0;
  849. lpfc_cmd->nonsg_phys = 0;
  850. lpfc_cmd->prot_seg_cnt = 0;
  851. }
  852. spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
  853. return lpfc_cmd;
  854. }
  855. /**
  856. * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
  857. * @phba: The Hba for which this call is being executed.
  858. * @psb: The scsi buffer which is being released.
  859. *
  860. * This routine releases @psb scsi buffer by adding it to tail of @phba
  861. * lpfc_scsi_buf_list list.
  862. **/
  863. static void
  864. lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
  865. {
  866. unsigned long iflag = 0;
  867. spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
  868. psb->pCmd = NULL;
  869. list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
  870. spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
  871. }
  872. /**
  873. * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
  874. * @phba: The Hba for which this call is being executed.
  875. * @psb: The scsi buffer which is being released.
  876. *
  877. * This routine releases @psb scsi buffer by adding it to tail of @phba
  878. * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
  879. * and cannot be reused for at least RA_TOV amount of time if it was
  880. * aborted.
  881. **/
  882. static void
  883. lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
  884. {
  885. unsigned long iflag = 0;
  886. if (psb->exch_busy) {
  887. spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
  888. iflag);
  889. psb->pCmd = NULL;
  890. list_add_tail(&psb->list,
  891. &phba->sli4_hba.lpfc_abts_scsi_buf_list);
  892. spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
  893. iflag);
  894. } else {
  895. spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
  896. psb->pCmd = NULL;
  897. list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
  898. spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
  899. }
  900. }
  901. /**
  902. * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
  903. * @phba: The Hba for which this call is being executed.
  904. * @psb: The scsi buffer which is being released.
  905. *
  906. * This routine releases @psb scsi buffer by adding it to tail of @phba
  907. * lpfc_scsi_buf_list list.
  908. **/
  909. static void
  910. lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
  911. {
  912. phba->lpfc_release_scsi_buf(phba, psb);
  913. }
  914. /**
  915. * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
  916. * @phba: The Hba for which this call is being executed.
  917. * @lpfc_cmd: The scsi buffer which is going to be mapped.
  918. *
  919. * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
  920. * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
  921. * through sg elements and format the bdea. This routine also initializes all
  922. * IOCB fields which are dependent on scsi command request buffer.
  923. *
  924. * Return codes:
  925. * 1 - Error
  926. * 0 - Success
  927. **/
  928. static int
  929. lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
  930. {
  931. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  932. struct scatterlist *sgel = NULL;
  933. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  934. struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
  935. struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
  936. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  937. struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
  938. dma_addr_t physaddr;
  939. uint32_t num_bde = 0;
  940. int nseg, datadir = scsi_cmnd->sc_data_direction;
  941. /*
  942. * There are three possibilities here - use scatter-gather segment, use
  943. * the single mapping, or neither. Start the lpfc command prep by
  944. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  945. * data bde entry.
  946. */
  947. bpl += 2;
  948. if (scsi_sg_count(scsi_cmnd)) {
  949. /*
  950. * The driver stores the segment count returned from pci_map_sg
  951. * because this a count of dma-mappings used to map the use_sg
  952. * pages. They are not guaranteed to be the same for those
  953. * architectures that implement an IOMMU.
  954. */
  955. nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
  956. scsi_sg_count(scsi_cmnd), datadir);
  957. if (unlikely(!nseg))
  958. return 1;
  959. lpfc_cmd->seg_cnt = nseg;
  960. if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
  961. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  962. "9064 BLKGRD: %s: Too many sg segments from "
  963. "dma_map_sg. Config %d, seg_cnt %d\n",
  964. __func__, phba->cfg_sg_seg_cnt,
  965. lpfc_cmd->seg_cnt);
  966. scsi_dma_unmap(scsi_cmnd);
  967. return 1;
  968. }
  969. /*
  970. * The driver established a maximum scatter-gather segment count
  971. * during probe that limits the number of sg elements in any
  972. * single scsi command. Just run through the seg_cnt and format
  973. * the bde's.
  974. * When using SLI-3 the driver will try to fit all the BDEs into
  975. * the IOCB. If it can't then the BDEs get added to a BPL as it
  976. * does for SLI-2 mode.
  977. */
  978. scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
  979. physaddr = sg_dma_address(sgel);
  980. if (phba->sli_rev == 3 &&
  981. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
  982. !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
  983. nseg <= LPFC_EXT_DATA_BDE_COUNT) {
  984. data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  985. data_bde->tus.f.bdeSize = sg_dma_len(sgel);
  986. data_bde->addrLow = putPaddrLow(physaddr);
  987. data_bde->addrHigh = putPaddrHigh(physaddr);
  988. data_bde++;
  989. } else {
  990. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  991. bpl->tus.f.bdeSize = sg_dma_len(sgel);
  992. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  993. bpl->addrLow =
  994. le32_to_cpu(putPaddrLow(physaddr));
  995. bpl->addrHigh =
  996. le32_to_cpu(putPaddrHigh(physaddr));
  997. bpl++;
  998. }
  999. }
  1000. }
  1001. /*
  1002. * Finish initializing those IOCB fields that are dependent on the
  1003. * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
  1004. * explicitly reinitialized and for SLI-3 the extended bde count is
  1005. * explicitly reinitialized since all iocb memory resources are reused.
  1006. */
  1007. if (phba->sli_rev == 3 &&
  1008. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
  1009. !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
  1010. if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
  1011. /*
  1012. * The extended IOCB format can only fit 3 BDE or a BPL.
  1013. * This I/O has more than 3 BDE so the 1st data bde will
  1014. * be a BPL that is filled in here.
  1015. */
  1016. physaddr = lpfc_cmd->dma_handle;
  1017. data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
  1018. data_bde->tus.f.bdeSize = (num_bde *
  1019. sizeof(struct ulp_bde64));
  1020. physaddr += (sizeof(struct fcp_cmnd) +
  1021. sizeof(struct fcp_rsp) +
  1022. (2 * sizeof(struct ulp_bde64)));
  1023. data_bde->addrHigh = putPaddrHigh(physaddr);
  1024. data_bde->addrLow = putPaddrLow(physaddr);
  1025. /* ebde count includes the responce bde and data bpl */
  1026. iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
  1027. } else {
  1028. /* ebde count includes the responce bde and data bdes */
  1029. iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
  1030. }
  1031. } else {
  1032. iocb_cmd->un.fcpi64.bdl.bdeSize =
  1033. ((num_bde + 2) * sizeof(struct ulp_bde64));
  1034. iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
  1035. }
  1036. fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
  1037. /*
  1038. * Due to difference in data length between DIF/non-DIF paths,
  1039. * we need to set word 4 of IOCB here
  1040. */
  1041. iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
  1042. return 0;
  1043. }
  1044. /*
  1045. * Given a scsi cmnd, determine the BlockGuard profile to be used
  1046. * with the cmd
  1047. */
  1048. static int
  1049. lpfc_sc_to_sli_prof(struct lpfc_hba *phba, struct scsi_cmnd *sc)
  1050. {
  1051. uint8_t guard_type = scsi_host_get_guard(sc->device->host);
  1052. uint8_t ret_prof = LPFC_PROF_INVALID;
  1053. if (guard_type == SHOST_DIX_GUARD_IP) {
  1054. switch (scsi_get_prot_op(sc)) {
  1055. case SCSI_PROT_READ_INSERT:
  1056. case SCSI_PROT_WRITE_STRIP:
  1057. ret_prof = LPFC_PROF_AST2;
  1058. break;
  1059. case SCSI_PROT_READ_STRIP:
  1060. case SCSI_PROT_WRITE_INSERT:
  1061. ret_prof = LPFC_PROF_A1;
  1062. break;
  1063. case SCSI_PROT_READ_PASS:
  1064. case SCSI_PROT_WRITE_PASS:
  1065. ret_prof = LPFC_PROF_AST1;
  1066. break;
  1067. case SCSI_PROT_NORMAL:
  1068. default:
  1069. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1070. "9063 BLKGRD:Bad op/guard:%d/%d combination\n",
  1071. scsi_get_prot_op(sc), guard_type);
  1072. break;
  1073. }
  1074. } else if (guard_type == SHOST_DIX_GUARD_CRC) {
  1075. switch (scsi_get_prot_op(sc)) {
  1076. case SCSI_PROT_READ_STRIP:
  1077. case SCSI_PROT_WRITE_INSERT:
  1078. ret_prof = LPFC_PROF_A1;
  1079. break;
  1080. case SCSI_PROT_READ_PASS:
  1081. case SCSI_PROT_WRITE_PASS:
  1082. ret_prof = LPFC_PROF_C1;
  1083. break;
  1084. case SCSI_PROT_READ_INSERT:
  1085. case SCSI_PROT_WRITE_STRIP:
  1086. case SCSI_PROT_NORMAL:
  1087. default:
  1088. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1089. "9075 BLKGRD: Bad op/guard:%d/%d combination\n",
  1090. scsi_get_prot_op(sc), guard_type);
  1091. break;
  1092. }
  1093. } else {
  1094. /* unsupported format */
  1095. BUG();
  1096. }
  1097. return ret_prof;
  1098. }
  1099. struct scsi_dif_tuple {
  1100. __be16 guard_tag; /* Checksum */
  1101. __be16 app_tag; /* Opaque storage */
  1102. __be32 ref_tag; /* Target LBA or indirect LBA */
  1103. };
  1104. static inline unsigned
  1105. lpfc_cmd_blksize(struct scsi_cmnd *sc)
  1106. {
  1107. return sc->device->sector_size;
  1108. }
  1109. /**
  1110. * lpfc_get_cmd_dif_parms - Extract DIF parameters from SCSI command
  1111. * @sc: in: SCSI command
  1112. * @apptagmask: out: app tag mask
  1113. * @apptagval: out: app tag value
  1114. * @reftag: out: ref tag (reference tag)
  1115. *
  1116. * Description:
  1117. * Extract DIF parameters from the command if possible. Otherwise,
  1118. * use default parameters.
  1119. *
  1120. **/
  1121. static inline void
  1122. lpfc_get_cmd_dif_parms(struct scsi_cmnd *sc, uint16_t *apptagmask,
  1123. uint16_t *apptagval, uint32_t *reftag)
  1124. {
  1125. struct scsi_dif_tuple *spt;
  1126. unsigned char op = scsi_get_prot_op(sc);
  1127. unsigned int protcnt = scsi_prot_sg_count(sc);
  1128. static int cnt;
  1129. if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
  1130. op == SCSI_PROT_WRITE_PASS)) {
  1131. cnt++;
  1132. spt = page_address(sg_page(scsi_prot_sglist(sc))) +
  1133. scsi_prot_sglist(sc)[0].offset;
  1134. *apptagmask = 0;
  1135. *apptagval = 0;
  1136. *reftag = cpu_to_be32(spt->ref_tag);
  1137. } else {
  1138. /* SBC defines ref tag to be lower 32bits of LBA */
  1139. *reftag = (uint32_t) (0xffffffff & scsi_get_lba(sc));
  1140. *apptagmask = 0;
  1141. *apptagval = 0;
  1142. }
  1143. }
  1144. /*
  1145. * This function sets up buffer list for protection groups of
  1146. * type LPFC_PG_TYPE_NO_DIF
  1147. *
  1148. * This is usually used when the HBA is instructed to generate
  1149. * DIFs and insert them into data stream (or strip DIF from
  1150. * incoming data stream)
  1151. *
  1152. * The buffer list consists of just one protection group described
  1153. * below:
  1154. * +-------------------------+
  1155. * start of prot group --> | PDE_1 |
  1156. * +-------------------------+
  1157. * | Data BDE |
  1158. * +-------------------------+
  1159. * |more Data BDE's ... (opt)|
  1160. * +-------------------------+
  1161. *
  1162. * @sc: pointer to scsi command we're working on
  1163. * @bpl: pointer to buffer list for protection groups
  1164. * @datacnt: number of segments of data that have been dma mapped
  1165. *
  1166. * Note: Data s/g buffers have been dma mapped
  1167. */
  1168. static int
  1169. lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  1170. struct ulp_bde64 *bpl, int datasegcnt)
  1171. {
  1172. struct scatterlist *sgde = NULL; /* s/g data entry */
  1173. struct lpfc_pde *pde1 = NULL;
  1174. dma_addr_t physaddr;
  1175. int i = 0, num_bde = 0;
  1176. int datadir = sc->sc_data_direction;
  1177. int prof = LPFC_PROF_INVALID;
  1178. unsigned blksize;
  1179. uint32_t reftag;
  1180. uint16_t apptagmask, apptagval;
  1181. pde1 = (struct lpfc_pde *) bpl;
  1182. prof = lpfc_sc_to_sli_prof(phba, sc);
  1183. if (prof == LPFC_PROF_INVALID)
  1184. goto out;
  1185. /* extract some info from the scsi command for PDE1*/
  1186. blksize = lpfc_cmd_blksize(sc);
  1187. lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
  1188. /* setup PDE1 with what we have */
  1189. lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
  1190. BG_EC_STOP_ERR);
  1191. lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
  1192. num_bde++;
  1193. bpl++;
  1194. /* assumption: caller has already run dma_map_sg on command data */
  1195. scsi_for_each_sg(sc, sgde, datasegcnt, i) {
  1196. physaddr = sg_dma_address(sgde);
  1197. bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1198. bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1199. bpl->tus.f.bdeSize = sg_dma_len(sgde);
  1200. if (datadir == DMA_TO_DEVICE)
  1201. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  1202. else
  1203. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  1204. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  1205. bpl++;
  1206. num_bde++;
  1207. }
  1208. out:
  1209. return num_bde;
  1210. }
  1211. /*
  1212. * This function sets up buffer list for protection groups of
  1213. * type LPFC_PG_TYPE_DIF_BUF
  1214. *
  1215. * This is usually used when DIFs are in their own buffers,
  1216. * separate from the data. The HBA can then by instructed
  1217. * to place the DIFs in the outgoing stream. For read operations,
  1218. * The HBA could extract the DIFs and place it in DIF buffers.
  1219. *
  1220. * The buffer list for this type consists of one or more of the
  1221. * protection groups described below:
  1222. * +-------------------------+
  1223. * start of first prot group --> | PDE_1 |
  1224. * +-------------------------+
  1225. * | PDE_3 (Prot BDE) |
  1226. * +-------------------------+
  1227. * | Data BDE |
  1228. * +-------------------------+
  1229. * |more Data BDE's ... (opt)|
  1230. * +-------------------------+
  1231. * start of new prot group --> | PDE_1 |
  1232. * +-------------------------+
  1233. * | ... |
  1234. * +-------------------------+
  1235. *
  1236. * @sc: pointer to scsi command we're working on
  1237. * @bpl: pointer to buffer list for protection groups
  1238. * @datacnt: number of segments of data that have been dma mapped
  1239. * @protcnt: number of segment of protection data that have been dma mapped
  1240. *
  1241. * Note: It is assumed that both data and protection s/g buffers have been
  1242. * mapped for DMA
  1243. */
  1244. static int
  1245. lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  1246. struct ulp_bde64 *bpl, int datacnt, int protcnt)
  1247. {
  1248. struct scatterlist *sgde = NULL; /* s/g data entry */
  1249. struct scatterlist *sgpe = NULL; /* s/g prot entry */
  1250. struct lpfc_pde *pde1 = NULL;
  1251. struct ulp_bde64 *prot_bde = NULL;
  1252. dma_addr_t dataphysaddr, protphysaddr;
  1253. unsigned short curr_data = 0, curr_prot = 0;
  1254. unsigned int split_offset, protgroup_len;
  1255. unsigned int protgrp_blks, protgrp_bytes;
  1256. unsigned int remainder, subtotal;
  1257. int prof = LPFC_PROF_INVALID;
  1258. int datadir = sc->sc_data_direction;
  1259. unsigned char pgdone = 0, alldone = 0;
  1260. unsigned blksize;
  1261. uint32_t reftag;
  1262. uint16_t apptagmask, apptagval;
  1263. int num_bde = 0;
  1264. sgpe = scsi_prot_sglist(sc);
  1265. sgde = scsi_sglist(sc);
  1266. if (!sgpe || !sgde) {
  1267. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1268. "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
  1269. sgpe, sgde);
  1270. return 0;
  1271. }
  1272. prof = lpfc_sc_to_sli_prof(phba, sc);
  1273. if (prof == LPFC_PROF_INVALID)
  1274. goto out;
  1275. /* extract some info from the scsi command for PDE1*/
  1276. blksize = lpfc_cmd_blksize(sc);
  1277. lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
  1278. split_offset = 0;
  1279. do {
  1280. /* setup the first PDE_1 */
  1281. pde1 = (struct lpfc_pde *) bpl;
  1282. lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
  1283. BG_EC_STOP_ERR);
  1284. lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
  1285. num_bde++;
  1286. bpl++;
  1287. /* setup the first BDE that points to protection buffer */
  1288. prot_bde = (struct ulp_bde64 *) bpl;
  1289. protphysaddr = sg_dma_address(sgpe);
  1290. prot_bde->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
  1291. prot_bde->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
  1292. protgroup_len = sg_dma_len(sgpe);
  1293. /* must be integer multiple of the DIF block length */
  1294. BUG_ON(protgroup_len % 8);
  1295. protgrp_blks = protgroup_len / 8;
  1296. protgrp_bytes = protgrp_blks * blksize;
  1297. prot_bde->tus.f.bdeSize = protgroup_len;
  1298. if (datadir == DMA_TO_DEVICE)
  1299. prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  1300. else
  1301. prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  1302. prot_bde->tus.w = le32_to_cpu(bpl->tus.w);
  1303. curr_prot++;
  1304. num_bde++;
  1305. /* setup BDE's for data blocks associated with DIF data */
  1306. pgdone = 0;
  1307. subtotal = 0; /* total bytes processed for current prot grp */
  1308. while (!pgdone) {
  1309. if (!sgde) {
  1310. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1311. "9065 BLKGRD:%s Invalid data segment\n",
  1312. __func__);
  1313. return 0;
  1314. }
  1315. bpl++;
  1316. dataphysaddr = sg_dma_address(sgde) + split_offset;
  1317. bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
  1318. bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
  1319. remainder = sg_dma_len(sgde) - split_offset;
  1320. if ((subtotal + remainder) <= protgrp_bytes) {
  1321. /* we can use this whole buffer */
  1322. bpl->tus.f.bdeSize = remainder;
  1323. split_offset = 0;
  1324. if ((subtotal + remainder) == protgrp_bytes)
  1325. pgdone = 1;
  1326. } else {
  1327. /* must split this buffer with next prot grp */
  1328. bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
  1329. split_offset += bpl->tus.f.bdeSize;
  1330. }
  1331. subtotal += bpl->tus.f.bdeSize;
  1332. if (datadir == DMA_TO_DEVICE)
  1333. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  1334. else
  1335. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  1336. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  1337. num_bde++;
  1338. curr_data++;
  1339. if (split_offset)
  1340. break;
  1341. /* Move to the next s/g segment if possible */
  1342. sgde = sg_next(sgde);
  1343. }
  1344. /* are we done ? */
  1345. if (curr_prot == protcnt) {
  1346. alldone = 1;
  1347. } else if (curr_prot < protcnt) {
  1348. /* advance to next prot buffer */
  1349. sgpe = sg_next(sgpe);
  1350. bpl++;
  1351. /* update the reference tag */
  1352. reftag += protgrp_blks;
  1353. } else {
  1354. /* if we're here, we have a bug */
  1355. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1356. "9054 BLKGRD: bug in %s\n", __func__);
  1357. }
  1358. } while (!alldone);
  1359. out:
  1360. return num_bde;
  1361. }
  1362. /*
  1363. * Given a SCSI command that supports DIF, determine composition of protection
  1364. * groups involved in setting up buffer lists
  1365. *
  1366. * Returns:
  1367. * for DIF (for both read and write)
  1368. * */
  1369. static int
  1370. lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
  1371. {
  1372. int ret = LPFC_PG_TYPE_INVALID;
  1373. unsigned char op = scsi_get_prot_op(sc);
  1374. switch (op) {
  1375. case SCSI_PROT_READ_STRIP:
  1376. case SCSI_PROT_WRITE_INSERT:
  1377. ret = LPFC_PG_TYPE_NO_DIF;
  1378. break;
  1379. case SCSI_PROT_READ_INSERT:
  1380. case SCSI_PROT_WRITE_STRIP:
  1381. case SCSI_PROT_READ_PASS:
  1382. case SCSI_PROT_WRITE_PASS:
  1383. ret = LPFC_PG_TYPE_DIF_BUF;
  1384. break;
  1385. default:
  1386. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1387. "9021 Unsupported protection op:%d\n", op);
  1388. break;
  1389. }
  1390. return ret;
  1391. }
  1392. /*
  1393. * This is the protection/DIF aware version of
  1394. * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
  1395. * two functions eventually, but for now, it's here
  1396. */
  1397. static int
  1398. lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba,
  1399. struct lpfc_scsi_buf *lpfc_cmd)
  1400. {
  1401. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  1402. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  1403. struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
  1404. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  1405. uint32_t num_bde = 0;
  1406. int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
  1407. int prot_group_type = 0;
  1408. int diflen, fcpdl;
  1409. unsigned blksize;
  1410. /*
  1411. * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
  1412. * fcp_rsp regions to the first data bde entry
  1413. */
  1414. bpl += 2;
  1415. if (scsi_sg_count(scsi_cmnd)) {
  1416. /*
  1417. * The driver stores the segment count returned from pci_map_sg
  1418. * because this a count of dma-mappings used to map the use_sg
  1419. * pages. They are not guaranteed to be the same for those
  1420. * architectures that implement an IOMMU.
  1421. */
  1422. datasegcnt = dma_map_sg(&phba->pcidev->dev,
  1423. scsi_sglist(scsi_cmnd),
  1424. scsi_sg_count(scsi_cmnd), datadir);
  1425. if (unlikely(!datasegcnt))
  1426. return 1;
  1427. lpfc_cmd->seg_cnt = datasegcnt;
  1428. if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
  1429. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1430. "9067 BLKGRD: %s: Too many sg segments"
  1431. " from dma_map_sg. Config %d, seg_cnt"
  1432. " %d\n",
  1433. __func__, phba->cfg_sg_seg_cnt,
  1434. lpfc_cmd->seg_cnt);
  1435. scsi_dma_unmap(scsi_cmnd);
  1436. return 1;
  1437. }
  1438. prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
  1439. switch (prot_group_type) {
  1440. case LPFC_PG_TYPE_NO_DIF:
  1441. num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
  1442. datasegcnt);
  1443. /* we should have 2 or more entries in buffer list */
  1444. if (num_bde < 2)
  1445. goto err;
  1446. break;
  1447. case LPFC_PG_TYPE_DIF_BUF:{
  1448. /*
  1449. * This type indicates that protection buffers are
  1450. * passed to the driver, so that needs to be prepared
  1451. * for DMA
  1452. */
  1453. protsegcnt = dma_map_sg(&phba->pcidev->dev,
  1454. scsi_prot_sglist(scsi_cmnd),
  1455. scsi_prot_sg_count(scsi_cmnd), datadir);
  1456. if (unlikely(!protsegcnt)) {
  1457. scsi_dma_unmap(scsi_cmnd);
  1458. return 1;
  1459. }
  1460. lpfc_cmd->prot_seg_cnt = protsegcnt;
  1461. if (lpfc_cmd->prot_seg_cnt
  1462. > phba->cfg_prot_sg_seg_cnt) {
  1463. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1464. "9068 BLKGRD: %s: Too many prot sg "
  1465. "segments from dma_map_sg. Config %d,"
  1466. "prot_seg_cnt %d\n", __func__,
  1467. phba->cfg_prot_sg_seg_cnt,
  1468. lpfc_cmd->prot_seg_cnt);
  1469. dma_unmap_sg(&phba->pcidev->dev,
  1470. scsi_prot_sglist(scsi_cmnd),
  1471. scsi_prot_sg_count(scsi_cmnd),
  1472. datadir);
  1473. scsi_dma_unmap(scsi_cmnd);
  1474. return 1;
  1475. }
  1476. num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
  1477. datasegcnt, protsegcnt);
  1478. /* we should have 3 or more entries in buffer list */
  1479. if (num_bde < 3)
  1480. goto err;
  1481. break;
  1482. }
  1483. case LPFC_PG_TYPE_INVALID:
  1484. default:
  1485. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1486. "9022 Unexpected protection group %i\n",
  1487. prot_group_type);
  1488. return 1;
  1489. }
  1490. }
  1491. /*
  1492. * Finish initializing those IOCB fields that are dependent on the
  1493. * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
  1494. * reinitialized since all iocb memory resources are used many times
  1495. * for transmit, receive, and continuation bpl's.
  1496. */
  1497. iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
  1498. iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
  1499. iocb_cmd->ulpBdeCount = 1;
  1500. iocb_cmd->ulpLe = 1;
  1501. fcpdl = scsi_bufflen(scsi_cmnd);
  1502. if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
  1503. /*
  1504. * We are in DIF Type 1 mode
  1505. * Every data block has a 8 byte DIF (trailer)
  1506. * attached to it. Must ajust FCP data length
  1507. */
  1508. blksize = lpfc_cmd_blksize(scsi_cmnd);
  1509. diflen = (fcpdl / blksize) * 8;
  1510. fcpdl += diflen;
  1511. }
  1512. fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
  1513. /*
  1514. * Due to difference in data length between DIF/non-DIF paths,
  1515. * we need to set word 4 of IOCB here
  1516. */
  1517. iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
  1518. return 0;
  1519. err:
  1520. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1521. "9023 Could not setup all needed BDE's"
  1522. "prot_group_type=%d, num_bde=%d\n",
  1523. prot_group_type, num_bde);
  1524. return 1;
  1525. }
  1526. /*
  1527. * This function checks for BlockGuard errors detected by
  1528. * the HBA. In case of errors, the ASC/ASCQ fields in the
  1529. * sense buffer will be set accordingly, paired with
  1530. * ILLEGAL_REQUEST to signal to the kernel that the HBA
  1531. * detected corruption.
  1532. *
  1533. * Returns:
  1534. * 0 - No error found
  1535. * 1 - BlockGuard error found
  1536. * -1 - Internal error (bad profile, ...etc)
  1537. */
  1538. static int
  1539. lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
  1540. struct lpfc_iocbq *pIocbOut)
  1541. {
  1542. struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
  1543. struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
  1544. int ret = 0;
  1545. uint32_t bghm = bgf->bghm;
  1546. uint32_t bgstat = bgf->bgstat;
  1547. uint64_t failing_sector = 0;
  1548. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
  1549. " 0x%x lba 0x%llx blk cnt 0x%x "
  1550. "bgstat=0x%x bghm=0x%x\n",
  1551. cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
  1552. blk_rq_sectors(cmd->request), bgstat, bghm);
  1553. spin_lock(&_dump_buf_lock);
  1554. if (!_dump_buf_done) {
  1555. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
  1556. " Data for %u blocks to debugfs\n",
  1557. (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
  1558. lpfc_debug_save_data(phba, cmd);
  1559. /* If we have a prot sgl, save the DIF buffer */
  1560. if (lpfc_prot_group_type(phba, cmd) ==
  1561. LPFC_PG_TYPE_DIF_BUF) {
  1562. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
  1563. "Saving DIF for %u blocks to debugfs\n",
  1564. (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
  1565. lpfc_debug_save_dif(phba, cmd);
  1566. }
  1567. _dump_buf_done = 1;
  1568. }
  1569. spin_unlock(&_dump_buf_lock);
  1570. if (lpfc_bgs_get_invalid_prof(bgstat)) {
  1571. cmd->result = ScsiResult(DID_ERROR, 0);
  1572. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
  1573. " BlockGuard profile. bgstat:0x%x\n",
  1574. bgstat);
  1575. ret = (-1);
  1576. goto out;
  1577. }
  1578. if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
  1579. cmd->result = ScsiResult(DID_ERROR, 0);
  1580. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
  1581. "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
  1582. bgstat);
  1583. ret = (-1);
  1584. goto out;
  1585. }
  1586. if (lpfc_bgs_get_guard_err(bgstat)) {
  1587. ret = 1;
  1588. scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
  1589. 0x10, 0x1);
  1590. cmd->result = DRIVER_SENSE << 24
  1591. | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
  1592. phba->bg_guard_err_cnt++;
  1593. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1594. "9055 BLKGRD: guard_tag error\n");
  1595. }
  1596. if (lpfc_bgs_get_reftag_err(bgstat)) {
  1597. ret = 1;
  1598. scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
  1599. 0x10, 0x3);
  1600. cmd->result = DRIVER_SENSE << 24
  1601. | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
  1602. phba->bg_reftag_err_cnt++;
  1603. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1604. "9056 BLKGRD: ref_tag error\n");
  1605. }
  1606. if (lpfc_bgs_get_apptag_err(bgstat)) {
  1607. ret = 1;
  1608. scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
  1609. 0x10, 0x2);
  1610. cmd->result = DRIVER_SENSE << 24
  1611. | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
  1612. phba->bg_apptag_err_cnt++;
  1613. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1614. "9061 BLKGRD: app_tag error\n");
  1615. }
  1616. if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
  1617. /*
  1618. * setup sense data descriptor 0 per SPC-4 as an information
  1619. * field, and put the failing LBA in it
  1620. */
  1621. cmd->sense_buffer[8] = 0; /* Information */
  1622. cmd->sense_buffer[9] = 0xa; /* Add. length */
  1623. bghm /= cmd->device->sector_size;
  1624. failing_sector = scsi_get_lba(cmd);
  1625. failing_sector += bghm;
  1626. put_unaligned_be64(failing_sector, &cmd->sense_buffer[10]);
  1627. }
  1628. if (!ret) {
  1629. /* No error was reported - problem in FW? */
  1630. cmd->result = ScsiResult(DID_ERROR, 0);
  1631. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1632. "9057 BLKGRD: no errors reported!\n");
  1633. }
  1634. out:
  1635. return ret;
  1636. }
  1637. /**
  1638. * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
  1639. * @phba: The Hba for which this call is being executed.
  1640. * @lpfc_cmd: The scsi buffer which is going to be mapped.
  1641. *
  1642. * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
  1643. * field of @lpfc_cmd for device with SLI-4 interface spec.
  1644. *
  1645. * Return codes:
  1646. * 1 - Error
  1647. * 0 - Success
  1648. **/
  1649. static int
  1650. lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
  1651. {
  1652. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  1653. struct scatterlist *sgel = NULL;
  1654. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  1655. struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
  1656. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  1657. dma_addr_t physaddr;
  1658. uint32_t num_bde = 0;
  1659. uint32_t dma_len;
  1660. uint32_t dma_offset = 0;
  1661. int nseg;
  1662. /*
  1663. * There are three possibilities here - use scatter-gather segment, use
  1664. * the single mapping, or neither. Start the lpfc command prep by
  1665. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  1666. * data bde entry.
  1667. */
  1668. if (scsi_sg_count(scsi_cmnd)) {
  1669. /*
  1670. * The driver stores the segment count returned from pci_map_sg
  1671. * because this a count of dma-mappings used to map the use_sg
  1672. * pages. They are not guaranteed to be the same for those
  1673. * architectures that implement an IOMMU.
  1674. */
  1675. nseg = scsi_dma_map(scsi_cmnd);
  1676. if (unlikely(!nseg))
  1677. return 1;
  1678. sgl += 1;
  1679. /* clear the last flag in the fcp_rsp map entry */
  1680. sgl->word2 = le32_to_cpu(sgl->word2);
  1681. bf_set(lpfc_sli4_sge_last, sgl, 0);
  1682. sgl->word2 = cpu_to_le32(sgl->word2);
  1683. sgl += 1;
  1684. lpfc_cmd->seg_cnt = nseg;
  1685. if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
  1686. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
  1687. " %s: Too many sg segments from "
  1688. "dma_map_sg. Config %d, seg_cnt %d\n",
  1689. __func__, phba->cfg_sg_seg_cnt,
  1690. lpfc_cmd->seg_cnt);
  1691. scsi_dma_unmap(scsi_cmnd);
  1692. return 1;
  1693. }
  1694. /*
  1695. * The driver established a maximum scatter-gather segment count
  1696. * during probe that limits the number of sg elements in any
  1697. * single scsi command. Just run through the seg_cnt and format
  1698. * the sge's.
  1699. * When using SLI-3 the driver will try to fit all the BDEs into
  1700. * the IOCB. If it can't then the BDEs get added to a BPL as it
  1701. * does for SLI-2 mode.
  1702. */
  1703. scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
  1704. physaddr = sg_dma_address(sgel);
  1705. dma_len = sg_dma_len(sgel);
  1706. sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
  1707. sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
  1708. if ((num_bde + 1) == nseg)
  1709. bf_set(lpfc_sli4_sge_last, sgl, 1);
  1710. else
  1711. bf_set(lpfc_sli4_sge_last, sgl, 0);
  1712. bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
  1713. sgl->word2 = cpu_to_le32(sgl->word2);
  1714. sgl->sge_len = cpu_to_le32(dma_len);
  1715. dma_offset += dma_len;
  1716. sgl++;
  1717. }
  1718. } else {
  1719. sgl += 1;
  1720. /* clear the last flag in the fcp_rsp map entry */
  1721. sgl->word2 = le32_to_cpu(sgl->word2);
  1722. bf_set(lpfc_sli4_sge_last, sgl, 1);
  1723. sgl->word2 = cpu_to_le32(sgl->word2);
  1724. }
  1725. /*
  1726. * Finish initializing those IOCB fields that are dependent on the
  1727. * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
  1728. * explicitly reinitialized.
  1729. * all iocb memory resources are reused.
  1730. */
  1731. fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
  1732. /*
  1733. * Due to difference in data length between DIF/non-DIF paths,
  1734. * we need to set word 4 of IOCB here
  1735. */
  1736. iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
  1737. return 0;
  1738. }
  1739. /**
  1740. * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
  1741. * @phba: The Hba for which this call is being executed.
  1742. * @lpfc_cmd: The scsi buffer which is going to be mapped.
  1743. *
  1744. * This routine wraps the actual DMA mapping function pointer from the
  1745. * lpfc_hba struct.
  1746. *
  1747. * Return codes:
  1748. * 1 - Error
  1749. * 0 - Success
  1750. **/
  1751. static inline int
  1752. lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
  1753. {
  1754. return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
  1755. }
  1756. /**
  1757. * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
  1758. * @phba: Pointer to hba context object.
  1759. * @vport: Pointer to vport object.
  1760. * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
  1761. * @rsp_iocb: Pointer to response iocb object which reported error.
  1762. *
  1763. * This function posts an event when there is a SCSI command reporting
  1764. * error from the scsi device.
  1765. **/
  1766. static void
  1767. lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
  1768. struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
  1769. struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
  1770. struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
  1771. uint32_t resp_info = fcprsp->rspStatus2;
  1772. uint32_t scsi_status = fcprsp->rspStatus3;
  1773. uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
  1774. struct lpfc_fast_path_event *fast_path_evt = NULL;
  1775. struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
  1776. unsigned long flags;
  1777. /* If there is queuefull or busy condition send a scsi event */
  1778. if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
  1779. (cmnd->result == SAM_STAT_BUSY)) {
  1780. fast_path_evt = lpfc_alloc_fast_evt(phba);
  1781. if (!fast_path_evt)
  1782. return;
  1783. fast_path_evt->un.scsi_evt.event_type =
  1784. FC_REG_SCSI_EVENT;
  1785. fast_path_evt->un.scsi_evt.subcategory =
  1786. (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
  1787. LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
  1788. fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
  1789. memcpy(&fast_path_evt->un.scsi_evt.wwpn,
  1790. &pnode->nlp_portname, sizeof(struct lpfc_name));
  1791. memcpy(&fast_path_evt->un.scsi_evt.wwnn,
  1792. &pnode->nlp_nodename, sizeof(struct lpfc_name));
  1793. } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
  1794. ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
  1795. fast_path_evt = lpfc_alloc_fast_evt(phba);
  1796. if (!fast_path_evt)
  1797. return;
  1798. fast_path_evt->un.check_cond_evt.scsi_event.event_type =
  1799. FC_REG_SCSI_EVENT;
  1800. fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
  1801. LPFC_EVENT_CHECK_COND;
  1802. fast_path_evt->un.check_cond_evt.scsi_event.lun =
  1803. cmnd->device->lun;
  1804. memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
  1805. &pnode->nlp_portname, sizeof(struct lpfc_name));
  1806. memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
  1807. &pnode->nlp_nodename, sizeof(struct lpfc_name));
  1808. fast_path_evt->un.check_cond_evt.sense_key =
  1809. cmnd->sense_buffer[2] & 0xf;
  1810. fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
  1811. fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
  1812. } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
  1813. fcpi_parm &&
  1814. ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
  1815. ((scsi_status == SAM_STAT_GOOD) &&
  1816. !(resp_info & (RESID_UNDER | RESID_OVER))))) {
  1817. /*
  1818. * If status is good or resid does not match with fcp_param and
  1819. * there is valid fcpi_parm, then there is a read_check error
  1820. */
  1821. fast_path_evt = lpfc_alloc_fast_evt(phba);
  1822. if (!fast_path_evt)
  1823. return;
  1824. fast_path_evt->un.read_check_error.header.event_type =
  1825. FC_REG_FABRIC_EVENT;
  1826. fast_path_evt->un.read_check_error.header.subcategory =
  1827. LPFC_EVENT_FCPRDCHKERR;
  1828. memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
  1829. &pnode->nlp_portname, sizeof(struct lpfc_name));
  1830. memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
  1831. &pnode->nlp_nodename, sizeof(struct lpfc_name));
  1832. fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
  1833. fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
  1834. fast_path_evt->un.read_check_error.fcpiparam =
  1835. fcpi_parm;
  1836. } else
  1837. return;
  1838. fast_path_evt->vport = vport;
  1839. spin_lock_irqsave(&phba->hbalock, flags);
  1840. list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
  1841. spin_unlock_irqrestore(&phba->hbalock, flags);
  1842. lpfc_worker_wake_up(phba);
  1843. return;
  1844. }
  1845. /**
  1846. * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
  1847. * @phba: The HBA for which this call is being executed.
  1848. * @psb: The scsi buffer which is going to be un-mapped.
  1849. *
  1850. * This routine does DMA un-mapping of scatter gather list of scsi command
  1851. * field of @lpfc_cmd for device with SLI-3 interface spec.
  1852. **/
  1853. static void
  1854. lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
  1855. {
  1856. /*
  1857. * There are only two special cases to consider. (1) the scsi command
  1858. * requested scatter-gather usage or (2) the scsi command allocated
  1859. * a request buffer, but did not request use_sg. There is a third
  1860. * case, but it does not require resource deallocation.
  1861. */
  1862. if (psb->seg_cnt > 0)
  1863. scsi_dma_unmap(psb->pCmd);
  1864. if (psb->prot_seg_cnt > 0)
  1865. dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
  1866. scsi_prot_sg_count(psb->pCmd),
  1867. psb->pCmd->sc_data_direction);
  1868. }
  1869. /**
  1870. * lpfc_handler_fcp_err - FCP response handler
  1871. * @vport: The virtual port for which this call is being executed.
  1872. * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
  1873. * @rsp_iocb: The response IOCB which contains FCP error.
  1874. *
  1875. * This routine is called to process response IOCB with status field
  1876. * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
  1877. * based upon SCSI and FCP error.
  1878. **/
  1879. static void
  1880. lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
  1881. struct lpfc_iocbq *rsp_iocb)
  1882. {
  1883. struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
  1884. struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
  1885. struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
  1886. uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
  1887. uint32_t resp_info = fcprsp->rspStatus2;
  1888. uint32_t scsi_status = fcprsp->rspStatus3;
  1889. uint32_t *lp;
  1890. uint32_t host_status = DID_OK;
  1891. uint32_t rsplen = 0;
  1892. uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
  1893. /*
  1894. * If this is a task management command, there is no
  1895. * scsi packet associated with this lpfc_cmd. The driver
  1896. * consumes it.
  1897. */
  1898. if (fcpcmd->fcpCntl2) {
  1899. scsi_status = 0;
  1900. goto out;
  1901. }
  1902. if (resp_info & RSP_LEN_VALID) {
  1903. rsplen = be32_to_cpu(fcprsp->rspRspLen);
  1904. if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
  1905. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  1906. "2719 Invalid response length: "
  1907. "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
  1908. cmnd->device->id,
  1909. cmnd->device->lun, cmnd->cmnd[0],
  1910. rsplen);
  1911. host_status = DID_ERROR;
  1912. goto out;
  1913. }
  1914. if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
  1915. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  1916. "2757 Protocol failure detected during "
  1917. "processing of FCP I/O op: "
  1918. "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
  1919. cmnd->device->id,
  1920. cmnd->device->lun, cmnd->cmnd[0],
  1921. fcprsp->rspInfo3);
  1922. host_status = DID_ERROR;
  1923. goto out;
  1924. }
  1925. }
  1926. if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
  1927. uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
  1928. if (snslen > SCSI_SENSE_BUFFERSIZE)
  1929. snslen = SCSI_SENSE_BUFFERSIZE;
  1930. if (resp_info & RSP_LEN_VALID)
  1931. rsplen = be32_to_cpu(fcprsp->rspRspLen);
  1932. memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
  1933. }
  1934. lp = (uint32_t *)cmnd->sense_buffer;
  1935. if (!scsi_status && (resp_info & RESID_UNDER))
  1936. logit = LOG_FCP;
  1937. lpfc_printf_vlog(vport, KERN_WARNING, logit,
  1938. "9024 FCP command x%x failed: x%x SNS x%x x%x "
  1939. "Data: x%x x%x x%x x%x x%x\n",
  1940. cmnd->cmnd[0], scsi_status,
  1941. be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
  1942. be32_to_cpu(fcprsp->rspResId),
  1943. be32_to_cpu(fcprsp->rspSnsLen),
  1944. be32_to_cpu(fcprsp->rspRspLen),
  1945. fcprsp->rspInfo3);
  1946. scsi_set_resid(cmnd, 0);
  1947. if (resp_info & RESID_UNDER) {
  1948. scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
  1949. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  1950. "9025 FCP Read Underrun, expected %d, "
  1951. "residual %d Data: x%x x%x x%x\n",
  1952. be32_to_cpu(fcpcmd->fcpDl),
  1953. scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
  1954. cmnd->underflow);
  1955. /*
  1956. * If there is an under run check if under run reported by
  1957. * storage array is same as the under run reported by HBA.
  1958. * If this is not same, there is a dropped frame.
  1959. */
  1960. if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
  1961. fcpi_parm &&
  1962. (scsi_get_resid(cmnd) != fcpi_parm)) {
  1963. lpfc_printf_vlog(vport, KERN_WARNING,
  1964. LOG_FCP | LOG_FCP_ERROR,
  1965. "9026 FCP Read Check Error "
  1966. "and Underrun Data: x%x x%x x%x x%x\n",
  1967. be32_to_cpu(fcpcmd->fcpDl),
  1968. scsi_get_resid(cmnd), fcpi_parm,
  1969. cmnd->cmnd[0]);
  1970. scsi_set_resid(cmnd, scsi_bufflen(cmnd));
  1971. host_status = DID_ERROR;
  1972. }
  1973. /*
  1974. * The cmnd->underflow is the minimum number of bytes that must
  1975. * be transfered for this command. Provided a sense condition
  1976. * is not present, make sure the actual amount transferred is at
  1977. * least the underflow value or fail.
  1978. */
  1979. if (!(resp_info & SNS_LEN_VALID) &&
  1980. (scsi_status == SAM_STAT_GOOD) &&
  1981. (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
  1982. < cmnd->underflow)) {
  1983. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  1984. "9027 FCP command x%x residual "
  1985. "underrun converted to error "
  1986. "Data: x%x x%x x%x\n",
  1987. cmnd->cmnd[0], scsi_bufflen(cmnd),
  1988. scsi_get_resid(cmnd), cmnd->underflow);
  1989. host_status = DID_ERROR;
  1990. }
  1991. } else if (resp_info & RESID_OVER) {
  1992. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  1993. "9028 FCP command x%x residual overrun error. "
  1994. "Data: x%x x%x\n", cmnd->cmnd[0],
  1995. scsi_bufflen(cmnd), scsi_get_resid(cmnd));
  1996. host_status = DID_ERROR;
  1997. /*
  1998. * Check SLI validation that all the transfer was actually done
  1999. * (fcpi_parm should be zero). Apply check only to reads.
  2000. */
  2001. } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
  2002. (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
  2003. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
  2004. "9029 FCP Read Check Error Data: "
  2005. "x%x x%x x%x x%x\n",
  2006. be32_to_cpu(fcpcmd->fcpDl),
  2007. be32_to_cpu(fcprsp->rspResId),
  2008. fcpi_parm, cmnd->cmnd[0]);
  2009. host_status = DID_ERROR;
  2010. scsi_set_resid(cmnd, scsi_bufflen(cmnd));
  2011. }
  2012. out:
  2013. cmnd->result = ScsiResult(host_status, scsi_status);
  2014. lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
  2015. }
  2016. /**
  2017. * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
  2018. * @phba: The Hba for which this call is being executed.
  2019. * @pIocbIn: The command IOCBQ for the scsi cmnd.
  2020. * @pIocbOut: The response IOCBQ for the scsi cmnd.
  2021. *
  2022. * This routine assigns scsi command result by looking into response IOCB
  2023. * status field appropriately. This routine handles QUEUE FULL condition as
  2024. * well by ramping down device queue depth.
  2025. **/
  2026. static void
  2027. lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
  2028. struct lpfc_iocbq *pIocbOut)
  2029. {
  2030. struct lpfc_scsi_buf *lpfc_cmd =
  2031. (struct lpfc_scsi_buf *) pIocbIn->context1;
  2032. struct lpfc_vport *vport = pIocbIn->vport;
  2033. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  2034. struct lpfc_nodelist *pnode = rdata->pnode;
  2035. struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
  2036. int result;
  2037. struct scsi_device *tmp_sdev;
  2038. int depth;
  2039. unsigned long flags;
  2040. struct lpfc_fast_path_event *fast_path_evt;
  2041. struct Scsi_Host *shost = cmd->device->host;
  2042. uint32_t queue_depth, scsi_id;
  2043. lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
  2044. lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
  2045. /* pick up SLI4 exhange busy status from HBA */
  2046. lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
  2047. if (pnode && NLP_CHK_NODE_ACT(pnode))
  2048. atomic_dec(&pnode->cmd_pending);
  2049. if (lpfc_cmd->status) {
  2050. if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
  2051. (lpfc_cmd->result & IOERR_DRVR_MASK))
  2052. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  2053. else if (lpfc_cmd->status >= IOSTAT_CNT)
  2054. lpfc_cmd->status = IOSTAT_DEFAULT;
  2055. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  2056. "9030 FCP cmd x%x failed <%d/%d> "
  2057. "status: x%x result: x%x Data: x%x x%x\n",
  2058. cmd->cmnd[0],
  2059. cmd->device ? cmd->device->id : 0xffff,
  2060. cmd->device ? cmd->device->lun : 0xffff,
  2061. lpfc_cmd->status, lpfc_cmd->result,
  2062. pIocbOut->iocb.ulpContext,
  2063. lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
  2064. switch (lpfc_cmd->status) {
  2065. case IOSTAT_FCP_RSP_ERROR:
  2066. /* Call FCP RSP handler to determine result */
  2067. lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
  2068. break;
  2069. case IOSTAT_NPORT_BSY:
  2070. case IOSTAT_FABRIC_BSY:
  2071. cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
  2072. fast_path_evt = lpfc_alloc_fast_evt(phba);
  2073. if (!fast_path_evt)
  2074. break;
  2075. fast_path_evt->un.fabric_evt.event_type =
  2076. FC_REG_FABRIC_EVENT;
  2077. fast_path_evt->un.fabric_evt.subcategory =
  2078. (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
  2079. LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
  2080. if (pnode && NLP_CHK_NODE_ACT(pnode)) {
  2081. memcpy(&fast_path_evt->un.fabric_evt.wwpn,
  2082. &pnode->nlp_portname,
  2083. sizeof(struct lpfc_name));
  2084. memcpy(&fast_path_evt->un.fabric_evt.wwnn,
  2085. &pnode->nlp_nodename,
  2086. sizeof(struct lpfc_name));
  2087. }
  2088. fast_path_evt->vport = vport;
  2089. fast_path_evt->work_evt.evt =
  2090. LPFC_EVT_FASTPATH_MGMT_EVT;
  2091. spin_lock_irqsave(&phba->hbalock, flags);
  2092. list_add_tail(&fast_path_evt->work_evt.evt_listp,
  2093. &phba->work_list);
  2094. spin_unlock_irqrestore(&phba->hbalock, flags);
  2095. lpfc_worker_wake_up(phba);
  2096. break;
  2097. case IOSTAT_LOCAL_REJECT:
  2098. if (lpfc_cmd->result == IOERR_INVALID_RPI ||
  2099. lpfc_cmd->result == IOERR_NO_RESOURCES ||
  2100. lpfc_cmd->result == IOERR_ABORT_REQUESTED) {
  2101. cmd->result = ScsiResult(DID_REQUEUE, 0);
  2102. break;
  2103. }
  2104. if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
  2105. lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
  2106. pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
  2107. if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
  2108. /*
  2109. * This is a response for a BG enabled
  2110. * cmd. Parse BG error
  2111. */
  2112. lpfc_parse_bg_err(phba, lpfc_cmd,
  2113. pIocbOut);
  2114. break;
  2115. } else {
  2116. lpfc_printf_vlog(vport, KERN_WARNING,
  2117. LOG_BG,
  2118. "9031 non-zero BGSTAT "
  2119. "on unprotected cmd\n");
  2120. }
  2121. }
  2122. /* else: fall through */
  2123. default:
  2124. cmd->result = ScsiResult(DID_ERROR, 0);
  2125. break;
  2126. }
  2127. if (!pnode || !NLP_CHK_NODE_ACT(pnode)
  2128. || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
  2129. cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
  2130. SAM_STAT_BUSY);
  2131. } else {
  2132. cmd->result = ScsiResult(DID_OK, 0);
  2133. }
  2134. if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
  2135. uint32_t *lp = (uint32_t *)cmd->sense_buffer;
  2136. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  2137. "0710 Iodone <%d/%d> cmd %p, error "
  2138. "x%x SNS x%x x%x Data: x%x x%x\n",
  2139. cmd->device->id, cmd->device->lun, cmd,
  2140. cmd->result, *lp, *(lp + 3), cmd->retries,
  2141. scsi_get_resid(cmd));
  2142. }
  2143. lpfc_update_stats(phba, lpfc_cmd);
  2144. result = cmd->result;
  2145. if (vport->cfg_max_scsicmpl_time &&
  2146. time_after(jiffies, lpfc_cmd->start_time +
  2147. msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
  2148. spin_lock_irqsave(shost->host_lock, flags);
  2149. if (pnode && NLP_CHK_NODE_ACT(pnode)) {
  2150. if (pnode->cmd_qdepth >
  2151. atomic_read(&pnode->cmd_pending) &&
  2152. (atomic_read(&pnode->cmd_pending) >
  2153. LPFC_MIN_TGT_QDEPTH) &&
  2154. ((cmd->cmnd[0] == READ_10) ||
  2155. (cmd->cmnd[0] == WRITE_10)))
  2156. pnode->cmd_qdepth =
  2157. atomic_read(&pnode->cmd_pending);
  2158. pnode->last_change_time = jiffies;
  2159. }
  2160. spin_unlock_irqrestore(shost->host_lock, flags);
  2161. } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
  2162. if ((pnode->cmd_qdepth < LPFC_MAX_TGT_QDEPTH) &&
  2163. time_after(jiffies, pnode->last_change_time +
  2164. msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
  2165. spin_lock_irqsave(shost->host_lock, flags);
  2166. pnode->cmd_qdepth += pnode->cmd_qdepth *
  2167. LPFC_TGTQ_RAMPUP_PCENT / 100;
  2168. if (pnode->cmd_qdepth > LPFC_MAX_TGT_QDEPTH)
  2169. pnode->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
  2170. pnode->last_change_time = jiffies;
  2171. spin_unlock_irqrestore(shost->host_lock, flags);
  2172. }
  2173. }
  2174. lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
  2175. /* The sdev is not guaranteed to be valid post scsi_done upcall. */
  2176. queue_depth = cmd->device->queue_depth;
  2177. scsi_id = cmd->device->id;
  2178. cmd->scsi_done(cmd);
  2179. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  2180. /*
  2181. * If there is a thread waiting for command completion
  2182. * wake up the thread.
  2183. */
  2184. spin_lock_irqsave(shost->host_lock, flags);
  2185. lpfc_cmd->pCmd = NULL;
  2186. if (lpfc_cmd->waitq)
  2187. wake_up(lpfc_cmd->waitq);
  2188. spin_unlock_irqrestore(shost->host_lock, flags);
  2189. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2190. return;
  2191. }
  2192. if (!result)
  2193. lpfc_rampup_queue_depth(vport, queue_depth);
  2194. /*
  2195. * Check for queue full. If the lun is reporting queue full, then
  2196. * back off the lun queue depth to prevent target overloads.
  2197. */
  2198. if (result == SAM_STAT_TASK_SET_FULL && pnode &&
  2199. NLP_CHK_NODE_ACT(pnode)) {
  2200. shost_for_each_device(tmp_sdev, shost) {
  2201. if (tmp_sdev->id != scsi_id)
  2202. continue;
  2203. depth = scsi_track_queue_full(tmp_sdev,
  2204. tmp_sdev->queue_depth-1);
  2205. if (depth <= 0)
  2206. continue;
  2207. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  2208. "0711 detected queue full - lun queue "
  2209. "depth adjusted to %d.\n", depth);
  2210. lpfc_send_sdev_queuedepth_change_event(phba, vport,
  2211. pnode,
  2212. tmp_sdev->lun,
  2213. depth+1, depth);
  2214. }
  2215. }
  2216. /*
  2217. * If there is a thread waiting for command completion
  2218. * wake up the thread.
  2219. */
  2220. spin_lock_irqsave(shost->host_lock, flags);
  2221. lpfc_cmd->pCmd = NULL;
  2222. if (lpfc_cmd->waitq)
  2223. wake_up(lpfc_cmd->waitq);
  2224. spin_unlock_irqrestore(shost->host_lock, flags);
  2225. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2226. }
  2227. /**
  2228. * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
  2229. * @data: A pointer to the immediate command data portion of the IOCB.
  2230. * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
  2231. *
  2232. * The routine copies the entire FCP command from @fcp_cmnd to @data while
  2233. * byte swapping the data to big endian format for transmission on the wire.
  2234. **/
  2235. static void
  2236. lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
  2237. {
  2238. int i, j;
  2239. for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
  2240. i += sizeof(uint32_t), j++) {
  2241. ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
  2242. }
  2243. }
  2244. /**
  2245. * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
  2246. * @vport: The virtual port for which this call is being executed.
  2247. * @lpfc_cmd: The scsi command which needs to send.
  2248. * @pnode: Pointer to lpfc_nodelist.
  2249. *
  2250. * This routine initializes fcp_cmnd and iocb data structure from scsi command
  2251. * to transfer for device with SLI3 interface spec.
  2252. **/
  2253. static void
  2254. lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
  2255. struct lpfc_nodelist *pnode)
  2256. {
  2257. struct lpfc_hba *phba = vport->phba;
  2258. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  2259. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  2260. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  2261. struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
  2262. int datadir = scsi_cmnd->sc_data_direction;
  2263. char tag[2];
  2264. if (!pnode || !NLP_CHK_NODE_ACT(pnode))
  2265. return;
  2266. lpfc_cmd->fcp_rsp->rspSnsLen = 0;
  2267. /* clear task management bits */
  2268. lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
  2269. int_to_scsilun(lpfc_cmd->pCmd->device->lun,
  2270. &lpfc_cmd->fcp_cmnd->fcp_lun);
  2271. memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
  2272. if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
  2273. switch (tag[0]) {
  2274. case HEAD_OF_QUEUE_TAG:
  2275. fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
  2276. break;
  2277. case ORDERED_QUEUE_TAG:
  2278. fcp_cmnd->fcpCntl1 = ORDERED_Q;
  2279. break;
  2280. default:
  2281. fcp_cmnd->fcpCntl1 = SIMPLE_Q;
  2282. break;
  2283. }
  2284. } else
  2285. fcp_cmnd->fcpCntl1 = 0;
  2286. /*
  2287. * There are three possibilities here - use scatter-gather segment, use
  2288. * the single mapping, or neither. Start the lpfc command prep by
  2289. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  2290. * data bde entry.
  2291. */
  2292. if (scsi_sg_count(scsi_cmnd)) {
  2293. if (datadir == DMA_TO_DEVICE) {
  2294. iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
  2295. if (phba->sli_rev < LPFC_SLI_REV4) {
  2296. iocb_cmd->un.fcpi.fcpi_parm = 0;
  2297. iocb_cmd->ulpPU = 0;
  2298. } else
  2299. iocb_cmd->ulpPU = PARM_READ_CHECK;
  2300. fcp_cmnd->fcpCntl3 = WRITE_DATA;
  2301. phba->fc4OutputRequests++;
  2302. } else {
  2303. iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
  2304. iocb_cmd->ulpPU = PARM_READ_CHECK;
  2305. fcp_cmnd->fcpCntl3 = READ_DATA;
  2306. phba->fc4InputRequests++;
  2307. }
  2308. } else {
  2309. iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
  2310. iocb_cmd->un.fcpi.fcpi_parm = 0;
  2311. iocb_cmd->ulpPU = 0;
  2312. fcp_cmnd->fcpCntl3 = 0;
  2313. phba->fc4ControlRequests++;
  2314. }
  2315. if (phba->sli_rev == 3 &&
  2316. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
  2317. lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
  2318. /*
  2319. * Finish initializing those IOCB fields that are independent
  2320. * of the scsi_cmnd request_buffer
  2321. */
  2322. piocbq->iocb.ulpContext = pnode->nlp_rpi;
  2323. if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
  2324. piocbq->iocb.ulpFCP2Rcvy = 1;
  2325. else
  2326. piocbq->iocb.ulpFCP2Rcvy = 0;
  2327. piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
  2328. piocbq->context1 = lpfc_cmd;
  2329. piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
  2330. piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
  2331. piocbq->vport = vport;
  2332. }
  2333. /**
  2334. * lpfc_scsi_prep_task_mgmt_cmnd - Convert SLI3 scsi TM cmd to FCP info unit
  2335. * @vport: The virtual port for which this call is being executed.
  2336. * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
  2337. * @lun: Logical unit number.
  2338. * @task_mgmt_cmd: SCSI task management command.
  2339. *
  2340. * This routine creates FCP information unit corresponding to @task_mgmt_cmd
  2341. * for device with SLI-3 interface spec.
  2342. *
  2343. * Return codes:
  2344. * 0 - Error
  2345. * 1 - Success
  2346. **/
  2347. static int
  2348. lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
  2349. struct lpfc_scsi_buf *lpfc_cmd,
  2350. unsigned int lun,
  2351. uint8_t task_mgmt_cmd)
  2352. {
  2353. struct lpfc_iocbq *piocbq;
  2354. IOCB_t *piocb;
  2355. struct fcp_cmnd *fcp_cmnd;
  2356. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  2357. struct lpfc_nodelist *ndlp = rdata->pnode;
  2358. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
  2359. ndlp->nlp_state != NLP_STE_MAPPED_NODE)
  2360. return 0;
  2361. piocbq = &(lpfc_cmd->cur_iocbq);
  2362. piocbq->vport = vport;
  2363. piocb = &piocbq->iocb;
  2364. fcp_cmnd = lpfc_cmd->fcp_cmnd;
  2365. /* Clear out any old data in the FCP command area */
  2366. memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
  2367. int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
  2368. fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
  2369. if (vport->phba->sli_rev == 3 &&
  2370. !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
  2371. lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
  2372. piocb->ulpCommand = CMD_FCP_ICMND64_CR;
  2373. piocb->ulpContext = ndlp->nlp_rpi;
  2374. if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
  2375. piocb->ulpFCP2Rcvy = 1;
  2376. }
  2377. piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
  2378. /* ulpTimeout is only one byte */
  2379. if (lpfc_cmd->timeout > 0xff) {
  2380. /*
  2381. * Do not timeout the command at the firmware level.
  2382. * The driver will provide the timeout mechanism.
  2383. */
  2384. piocb->ulpTimeout = 0;
  2385. } else
  2386. piocb->ulpTimeout = lpfc_cmd->timeout;
  2387. if (vport->phba->sli_rev == LPFC_SLI_REV4)
  2388. lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
  2389. return 1;
  2390. }
  2391. /**
  2392. * lpfc_scsi_api_table_setup - Set up scsi api fucntion jump table
  2393. * @phba: The hba struct for which this call is being executed.
  2394. * @dev_grp: The HBA PCI-Device group number.
  2395. *
  2396. * This routine sets up the SCSI interface API function jump table in @phba
  2397. * struct.
  2398. * Returns: 0 - success, -ENODEV - failure.
  2399. **/
  2400. int
  2401. lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  2402. {
  2403. phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
  2404. phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
  2405. phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf;
  2406. switch (dev_grp) {
  2407. case LPFC_PCI_DEV_LP:
  2408. phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
  2409. phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
  2410. phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
  2411. break;
  2412. case LPFC_PCI_DEV_OC:
  2413. phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
  2414. phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
  2415. phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
  2416. break;
  2417. default:
  2418. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2419. "1418 Invalid HBA PCI-device group: 0x%x\n",
  2420. dev_grp);
  2421. return -ENODEV;
  2422. break;
  2423. }
  2424. phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf;
  2425. phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
  2426. phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
  2427. return 0;
  2428. }
  2429. /**
  2430. * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
  2431. * @phba: The Hba for which this call is being executed.
  2432. * @cmdiocbq: Pointer to lpfc_iocbq data structure.
  2433. * @rspiocbq: Pointer to lpfc_iocbq data structure.
  2434. *
  2435. * This routine is IOCB completion routine for device reset and target reset
  2436. * routine. This routine release scsi buffer associated with lpfc_cmd.
  2437. **/
  2438. static void
  2439. lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
  2440. struct lpfc_iocbq *cmdiocbq,
  2441. struct lpfc_iocbq *rspiocbq)
  2442. {
  2443. struct lpfc_scsi_buf *lpfc_cmd =
  2444. (struct lpfc_scsi_buf *) cmdiocbq->context1;
  2445. if (lpfc_cmd)
  2446. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2447. return;
  2448. }
  2449. /**
  2450. * lpfc_info - Info entry point of scsi_host_template data structure
  2451. * @host: The scsi host for which this call is being executed.
  2452. *
  2453. * This routine provides module information about hba.
  2454. *
  2455. * Reutrn code:
  2456. * Pointer to char - Success.
  2457. **/
  2458. const char *
  2459. lpfc_info(struct Scsi_Host *host)
  2460. {
  2461. struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
  2462. struct lpfc_hba *phba = vport->phba;
  2463. int len;
  2464. static char lpfcinfobuf[384];
  2465. memset(lpfcinfobuf,0,384);
  2466. if (phba && phba->pcidev){
  2467. strncpy(lpfcinfobuf, phba->ModelDesc, 256);
  2468. len = strlen(lpfcinfobuf);
  2469. snprintf(lpfcinfobuf + len,
  2470. 384-len,
  2471. " on PCI bus %02x device %02x irq %d",
  2472. phba->pcidev->bus->number,
  2473. phba->pcidev->devfn,
  2474. phba->pcidev->irq);
  2475. len = strlen(lpfcinfobuf);
  2476. if (phba->Port[0]) {
  2477. snprintf(lpfcinfobuf + len,
  2478. 384-len,
  2479. " port %s",
  2480. phba->Port);
  2481. }
  2482. len = strlen(lpfcinfobuf);
  2483. if (phba->sli4_hba.link_state.logical_speed) {
  2484. snprintf(lpfcinfobuf + len,
  2485. 384-len,
  2486. " Logical Link Speed: %d Mbps",
  2487. phba->sli4_hba.link_state.logical_speed * 10);
  2488. }
  2489. }
  2490. return lpfcinfobuf;
  2491. }
  2492. /**
  2493. * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
  2494. * @phba: The Hba for which this call is being executed.
  2495. *
  2496. * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
  2497. * The default value of cfg_poll_tmo is 10 milliseconds.
  2498. **/
  2499. static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
  2500. {
  2501. unsigned long poll_tmo_expires =
  2502. (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
  2503. if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
  2504. mod_timer(&phba->fcp_poll_timer,
  2505. poll_tmo_expires);
  2506. }
  2507. /**
  2508. * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
  2509. * @phba: The Hba for which this call is being executed.
  2510. *
  2511. * This routine starts the fcp_poll_timer of @phba.
  2512. **/
  2513. void lpfc_poll_start_timer(struct lpfc_hba * phba)
  2514. {
  2515. lpfc_poll_rearm_timer(phba);
  2516. }
  2517. /**
  2518. * lpfc_poll_timeout - Restart polling timer
  2519. * @ptr: Map to lpfc_hba data structure pointer.
  2520. *
  2521. * This routine restarts fcp_poll timer, when FCP ring polling is enable
  2522. * and FCP Ring interrupt is disable.
  2523. **/
  2524. void lpfc_poll_timeout(unsigned long ptr)
  2525. {
  2526. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  2527. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  2528. lpfc_sli_handle_fast_ring_event(phba,
  2529. &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
  2530. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  2531. lpfc_poll_rearm_timer(phba);
  2532. }
  2533. }
  2534. /**
  2535. * lpfc_queuecommand - scsi_host_template queuecommand entry point
  2536. * @cmnd: Pointer to scsi_cmnd data structure.
  2537. * @done: Pointer to done routine.
  2538. *
  2539. * Driver registers this routine to scsi midlayer to submit a @cmd to process.
  2540. * This routine prepares an IOCB from scsi command and provides to firmware.
  2541. * The @done callback is invoked after driver finished processing the command.
  2542. *
  2543. * Return value :
  2544. * 0 - Success
  2545. * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
  2546. **/
  2547. static int
  2548. lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
  2549. {
  2550. struct Scsi_Host *shost = cmnd->device->host;
  2551. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  2552. struct lpfc_hba *phba = vport->phba;
  2553. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  2554. struct lpfc_nodelist *ndlp;
  2555. struct lpfc_scsi_buf *lpfc_cmd;
  2556. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  2557. int err;
  2558. err = fc_remote_port_chkready(rport);
  2559. if (err) {
  2560. cmnd->result = err;
  2561. goto out_fail_command;
  2562. }
  2563. ndlp = rdata->pnode;
  2564. if (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
  2565. scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
  2566. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  2567. "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
  2568. " op:%02x str=%s without registering for"
  2569. " BlockGuard - Rejecting command\n",
  2570. cmnd->cmnd[0], scsi_get_prot_op(cmnd),
  2571. dif_op_str[scsi_get_prot_op(cmnd)]);
  2572. goto out_fail_command;
  2573. }
  2574. /*
  2575. * Catch race where our node has transitioned, but the
  2576. * transport is still transitioning.
  2577. */
  2578. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  2579. cmnd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
  2580. goto out_fail_command;
  2581. }
  2582. if (vport->cfg_max_scsicmpl_time &&
  2583. (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth))
  2584. goto out_host_busy;
  2585. lpfc_cmd = lpfc_get_scsi_buf(phba);
  2586. if (lpfc_cmd == NULL) {
  2587. lpfc_rampdown_queue_depth(phba);
  2588. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  2589. "0707 driver's buffer pool is empty, "
  2590. "IO busied\n");
  2591. goto out_host_busy;
  2592. }
  2593. /*
  2594. * Store the midlayer's command structure for the completion phase
  2595. * and complete the command initialization.
  2596. */
  2597. lpfc_cmd->pCmd = cmnd;
  2598. lpfc_cmd->rdata = rdata;
  2599. lpfc_cmd->timeout = 0;
  2600. lpfc_cmd->start_time = jiffies;
  2601. cmnd->host_scribble = (unsigned char *)lpfc_cmd;
  2602. cmnd->scsi_done = done;
  2603. if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
  2604. if (vport->phba->cfg_enable_bg) {
  2605. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2606. "9033 BLKGRD: rcvd protected cmd:%02x op:%02x "
  2607. "str=%s\n",
  2608. cmnd->cmnd[0], scsi_get_prot_op(cmnd),
  2609. dif_op_str[scsi_get_prot_op(cmnd)]);
  2610. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2611. "9034 BLKGRD: CDB: %02x %02x %02x %02x %02x "
  2612. "%02x %02x %02x %02x %02x\n",
  2613. cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
  2614. cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
  2615. cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
  2616. cmnd->cmnd[9]);
  2617. if (cmnd->cmnd[0] == READ_10)
  2618. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2619. "9035 BLKGRD: READ @ sector %llu, "
  2620. "count %u\n",
  2621. (unsigned long long)scsi_get_lba(cmnd),
  2622. blk_rq_sectors(cmnd->request));
  2623. else if (cmnd->cmnd[0] == WRITE_10)
  2624. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2625. "9036 BLKGRD: WRITE @ sector %llu, "
  2626. "count %u cmd=%p\n",
  2627. (unsigned long long)scsi_get_lba(cmnd),
  2628. blk_rq_sectors(cmnd->request),
  2629. cmnd);
  2630. }
  2631. err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
  2632. } else {
  2633. if (vport->phba->cfg_enable_bg) {
  2634. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2635. "9038 BLKGRD: rcvd unprotected cmd:"
  2636. "%02x op:%02x str=%s\n",
  2637. cmnd->cmnd[0], scsi_get_prot_op(cmnd),
  2638. dif_op_str[scsi_get_prot_op(cmnd)]);
  2639. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2640. "9039 BLKGRD: CDB: %02x %02x %02x "
  2641. "%02x %02x %02x %02x %02x %02x %02x\n",
  2642. cmnd->cmnd[0], cmnd->cmnd[1],
  2643. cmnd->cmnd[2], cmnd->cmnd[3],
  2644. cmnd->cmnd[4], cmnd->cmnd[5],
  2645. cmnd->cmnd[6], cmnd->cmnd[7],
  2646. cmnd->cmnd[8], cmnd->cmnd[9]);
  2647. if (cmnd->cmnd[0] == READ_10)
  2648. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2649. "9040 dbg: READ @ sector %llu, "
  2650. "count %u\n",
  2651. (unsigned long long)scsi_get_lba(cmnd),
  2652. blk_rq_sectors(cmnd->request));
  2653. else if (cmnd->cmnd[0] == WRITE_10)
  2654. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2655. "9041 dbg: WRITE @ sector %llu, "
  2656. "count %u cmd=%p\n",
  2657. (unsigned long long)scsi_get_lba(cmnd),
  2658. blk_rq_sectors(cmnd->request), cmnd);
  2659. else
  2660. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2661. "9042 dbg: parser not implemented\n");
  2662. }
  2663. err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
  2664. }
  2665. if (err)
  2666. goto out_host_busy_free_buf;
  2667. lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
  2668. atomic_inc(&ndlp->cmd_pending);
  2669. err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
  2670. &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
  2671. if (err) {
  2672. atomic_dec(&ndlp->cmd_pending);
  2673. goto out_host_busy_free_buf;
  2674. }
  2675. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  2676. spin_unlock(shost->host_lock);
  2677. lpfc_sli_handle_fast_ring_event(phba,
  2678. &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
  2679. spin_lock(shost->host_lock);
  2680. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  2681. lpfc_poll_rearm_timer(phba);
  2682. }
  2683. return 0;
  2684. out_host_busy_free_buf:
  2685. lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
  2686. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2687. out_host_busy:
  2688. return SCSI_MLQUEUE_HOST_BUSY;
  2689. out_fail_command:
  2690. done(cmnd);
  2691. return 0;
  2692. }
  2693. /**
  2694. * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
  2695. * @cmnd: Pointer to scsi_cmnd data structure.
  2696. *
  2697. * This routine aborts @cmnd pending in base driver.
  2698. *
  2699. * Return code :
  2700. * 0x2003 - Error
  2701. * 0x2002 - Success
  2702. **/
  2703. static int
  2704. lpfc_abort_handler(struct scsi_cmnd *cmnd)
  2705. {
  2706. struct Scsi_Host *shost = cmnd->device->host;
  2707. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  2708. struct lpfc_hba *phba = vport->phba;
  2709. struct lpfc_iocbq *iocb;
  2710. struct lpfc_iocbq *abtsiocb;
  2711. struct lpfc_scsi_buf *lpfc_cmd;
  2712. IOCB_t *cmd, *icmd;
  2713. int ret = SUCCESS;
  2714. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
  2715. fc_block_scsi_eh(cmnd);
  2716. lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
  2717. BUG_ON(!lpfc_cmd);
  2718. /*
  2719. * If pCmd field of the corresponding lpfc_scsi_buf structure
  2720. * points to a different SCSI command, then the driver has
  2721. * already completed this command, but the midlayer did not
  2722. * see the completion before the eh fired. Just return
  2723. * SUCCESS.
  2724. */
  2725. iocb = &lpfc_cmd->cur_iocbq;
  2726. if (lpfc_cmd->pCmd != cmnd)
  2727. goto out;
  2728. BUG_ON(iocb->context1 != lpfc_cmd);
  2729. abtsiocb = lpfc_sli_get_iocbq(phba);
  2730. if (abtsiocb == NULL) {
  2731. ret = FAILED;
  2732. goto out;
  2733. }
  2734. /*
  2735. * The scsi command can not be in txq and it is in flight because the
  2736. * pCmd is still pointig at the SCSI command we have to abort. There
  2737. * is no need to search the txcmplq. Just send an abort to the FW.
  2738. */
  2739. cmd = &iocb->iocb;
  2740. icmd = &abtsiocb->iocb;
  2741. icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
  2742. icmd->un.acxri.abortContextTag = cmd->ulpContext;
  2743. if (phba->sli_rev == LPFC_SLI_REV4)
  2744. icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
  2745. else
  2746. icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
  2747. icmd->ulpLe = 1;
  2748. icmd->ulpClass = cmd->ulpClass;
  2749. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  2750. abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
  2751. abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
  2752. if (lpfc_is_link_up(phba))
  2753. icmd->ulpCommand = CMD_ABORT_XRI_CN;
  2754. else
  2755. icmd->ulpCommand = CMD_CLOSE_XRI_CN;
  2756. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  2757. abtsiocb->vport = vport;
  2758. if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
  2759. IOCB_ERROR) {
  2760. lpfc_sli_release_iocbq(phba, abtsiocb);
  2761. ret = FAILED;
  2762. goto out;
  2763. }
  2764. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  2765. lpfc_sli_handle_fast_ring_event(phba,
  2766. &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
  2767. lpfc_cmd->waitq = &waitq;
  2768. /* Wait for abort to complete */
  2769. wait_event_timeout(waitq,
  2770. (lpfc_cmd->pCmd != cmnd),
  2771. (2*vport->cfg_devloss_tmo*HZ));
  2772. spin_lock_irq(shost->host_lock);
  2773. lpfc_cmd->waitq = NULL;
  2774. spin_unlock_irq(shost->host_lock);
  2775. if (lpfc_cmd->pCmd == cmnd) {
  2776. ret = FAILED;
  2777. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2778. "0748 abort handler timed out waiting "
  2779. "for abort to complete: ret %#x, ID %d, "
  2780. "LUN %d, snum %#lx\n",
  2781. ret, cmnd->device->id, cmnd->device->lun,
  2782. cmnd->serial_number);
  2783. }
  2784. out:
  2785. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  2786. "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
  2787. "LUN %d snum %#lx\n", ret, cmnd->device->id,
  2788. cmnd->device->lun, cmnd->serial_number);
  2789. return ret;
  2790. }
  2791. static char *
  2792. lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
  2793. {
  2794. switch (task_mgmt_cmd) {
  2795. case FCP_ABORT_TASK_SET:
  2796. return "ABORT_TASK_SET";
  2797. case FCP_CLEAR_TASK_SET:
  2798. return "FCP_CLEAR_TASK_SET";
  2799. case FCP_BUS_RESET:
  2800. return "FCP_BUS_RESET";
  2801. case FCP_LUN_RESET:
  2802. return "FCP_LUN_RESET";
  2803. case FCP_TARGET_RESET:
  2804. return "FCP_TARGET_RESET";
  2805. case FCP_CLEAR_ACA:
  2806. return "FCP_CLEAR_ACA";
  2807. case FCP_TERMINATE_TASK:
  2808. return "FCP_TERMINATE_TASK";
  2809. default:
  2810. return "unknown";
  2811. }
  2812. }
  2813. /**
  2814. * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
  2815. * @vport: The virtual port for which this call is being executed.
  2816. * @rdata: Pointer to remote port local data
  2817. * @tgt_id: Target ID of remote device.
  2818. * @lun_id: Lun number for the TMF
  2819. * @task_mgmt_cmd: type of TMF to send
  2820. *
  2821. * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
  2822. * a remote port.
  2823. *
  2824. * Return Code:
  2825. * 0x2003 - Error
  2826. * 0x2002 - Success.
  2827. **/
  2828. static int
  2829. lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
  2830. unsigned tgt_id, unsigned int lun_id,
  2831. uint8_t task_mgmt_cmd)
  2832. {
  2833. struct lpfc_hba *phba = vport->phba;
  2834. struct lpfc_scsi_buf *lpfc_cmd;
  2835. struct lpfc_iocbq *iocbq;
  2836. struct lpfc_iocbq *iocbqrsp;
  2837. int ret;
  2838. int status;
  2839. if (!rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
  2840. return FAILED;
  2841. lpfc_cmd = lpfc_get_scsi_buf(phba);
  2842. if (lpfc_cmd == NULL)
  2843. return FAILED;
  2844. lpfc_cmd->timeout = 60;
  2845. lpfc_cmd->rdata = rdata;
  2846. status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
  2847. task_mgmt_cmd);
  2848. if (!status) {
  2849. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2850. return FAILED;
  2851. }
  2852. iocbq = &lpfc_cmd->cur_iocbq;
  2853. iocbqrsp = lpfc_sli_get_iocbq(phba);
  2854. if (iocbqrsp == NULL) {
  2855. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2856. return FAILED;
  2857. }
  2858. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  2859. "0702 Issue %s to TGT %d LUN %d "
  2860. "rpi x%x nlp_flag x%x\n",
  2861. lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
  2862. rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
  2863. status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
  2864. iocbq, iocbqrsp, lpfc_cmd->timeout);
  2865. if (status != IOCB_SUCCESS) {
  2866. if (status == IOCB_TIMEDOUT) {
  2867. iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
  2868. ret = TIMEOUT_ERROR;
  2869. } else
  2870. ret = FAILED;
  2871. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  2872. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2873. "0727 TMF %s to TGT %d LUN %d failed (%d, %d)\n",
  2874. lpfc_taskmgmt_name(task_mgmt_cmd),
  2875. tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
  2876. iocbqrsp->iocb.un.ulpWord[4]);
  2877. } else
  2878. ret = SUCCESS;
  2879. lpfc_sli_release_iocbq(phba, iocbqrsp);
  2880. if (ret != TIMEOUT_ERROR)
  2881. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2882. return ret;
  2883. }
  2884. /**
  2885. * lpfc_chk_tgt_mapped -
  2886. * @vport: The virtual port to check on
  2887. * @cmnd: Pointer to scsi_cmnd data structure.
  2888. *
  2889. * This routine delays until the scsi target (aka rport) for the
  2890. * command exists (is present and logged in) or we declare it non-existent.
  2891. *
  2892. * Return code :
  2893. * 0x2003 - Error
  2894. * 0x2002 - Success
  2895. **/
  2896. static int
  2897. lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
  2898. {
  2899. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  2900. struct lpfc_nodelist *pnode;
  2901. unsigned long later;
  2902. if (!rdata) {
  2903. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  2904. "0797 Tgt Map rport failure: rdata x%p\n", rdata);
  2905. return FAILED;
  2906. }
  2907. pnode = rdata->pnode;
  2908. /*
  2909. * If target is not in a MAPPED state, delay until
  2910. * target is rediscovered or devloss timeout expires.
  2911. */
  2912. later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
  2913. while (time_after(later, jiffies)) {
  2914. if (!pnode || !NLP_CHK_NODE_ACT(pnode))
  2915. return FAILED;
  2916. if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
  2917. return SUCCESS;
  2918. schedule_timeout_uninterruptible(msecs_to_jiffies(500));
  2919. rdata = cmnd->device->hostdata;
  2920. if (!rdata)
  2921. return FAILED;
  2922. pnode = rdata->pnode;
  2923. }
  2924. if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
  2925. (pnode->nlp_state != NLP_STE_MAPPED_NODE))
  2926. return FAILED;
  2927. return SUCCESS;
  2928. }
  2929. /**
  2930. * lpfc_reset_flush_io_context -
  2931. * @vport: The virtual port (scsi_host) for the flush context
  2932. * @tgt_id: If aborting by Target contect - specifies the target id
  2933. * @lun_id: If aborting by Lun context - specifies the lun id
  2934. * @context: specifies the context level to flush at.
  2935. *
  2936. * After a reset condition via TMF, we need to flush orphaned i/o
  2937. * contexts from the adapter. This routine aborts any contexts
  2938. * outstanding, then waits for their completions. The wait is
  2939. * bounded by devloss_tmo though.
  2940. *
  2941. * Return code :
  2942. * 0x2003 - Error
  2943. * 0x2002 - Success
  2944. **/
  2945. static int
  2946. lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
  2947. uint64_t lun_id, lpfc_ctx_cmd context)
  2948. {
  2949. struct lpfc_hba *phba = vport->phba;
  2950. unsigned long later;
  2951. int cnt;
  2952. cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
  2953. if (cnt)
  2954. lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
  2955. tgt_id, lun_id, context);
  2956. later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
  2957. while (time_after(later, jiffies) && cnt) {
  2958. schedule_timeout_uninterruptible(msecs_to_jiffies(20));
  2959. cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
  2960. }
  2961. if (cnt) {
  2962. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2963. "0724 I/O flush failure for context %s : cnt x%x\n",
  2964. ((context == LPFC_CTX_LUN) ? "LUN" :
  2965. ((context == LPFC_CTX_TGT) ? "TGT" :
  2966. ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
  2967. cnt);
  2968. return FAILED;
  2969. }
  2970. return SUCCESS;
  2971. }
  2972. /**
  2973. * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
  2974. * @cmnd: Pointer to scsi_cmnd data structure.
  2975. *
  2976. * This routine does a device reset by sending a LUN_RESET task management
  2977. * command.
  2978. *
  2979. * Return code :
  2980. * 0x2003 - Error
  2981. * 0x2002 - Success
  2982. **/
  2983. static int
  2984. lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
  2985. {
  2986. struct Scsi_Host *shost = cmnd->device->host;
  2987. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  2988. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  2989. struct lpfc_nodelist *pnode;
  2990. unsigned tgt_id = cmnd->device->id;
  2991. unsigned int lun_id = cmnd->device->lun;
  2992. struct lpfc_scsi_event_header scsi_event;
  2993. int status;
  2994. if (!rdata) {
  2995. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2996. "0798 Device Reset rport failure: rdata x%p\n", rdata);
  2997. return FAILED;
  2998. }
  2999. pnode = rdata->pnode;
  3000. fc_block_scsi_eh(cmnd);
  3001. status = lpfc_chk_tgt_mapped(vport, cmnd);
  3002. if (status == FAILED) {
  3003. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3004. "0721 Device Reset rport failure: rdata x%p\n", rdata);
  3005. return FAILED;
  3006. }
  3007. scsi_event.event_type = FC_REG_SCSI_EVENT;
  3008. scsi_event.subcategory = LPFC_EVENT_LUNRESET;
  3009. scsi_event.lun = lun_id;
  3010. memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
  3011. memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
  3012. fc_host_post_vendor_event(shost, fc_get_event_number(),
  3013. sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
  3014. status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
  3015. FCP_LUN_RESET);
  3016. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3017. "0713 SCSI layer issued Device Reset (%d, %d) "
  3018. "return x%x\n", tgt_id, lun_id, status);
  3019. /*
  3020. * We have to clean up i/o as : they may be orphaned by the TMF;
  3021. * or if the TMF failed, they may be in an indeterminate state.
  3022. * So, continue on.
  3023. * We will report success if all the i/o aborts successfully.
  3024. */
  3025. status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
  3026. LPFC_CTX_LUN);
  3027. return status;
  3028. }
  3029. /**
  3030. * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
  3031. * @cmnd: Pointer to scsi_cmnd data structure.
  3032. *
  3033. * This routine does a target reset by sending a TARGET_RESET task management
  3034. * command.
  3035. *
  3036. * Return code :
  3037. * 0x2003 - Error
  3038. * 0x2002 - Success
  3039. **/
  3040. static int
  3041. lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
  3042. {
  3043. struct Scsi_Host *shost = cmnd->device->host;
  3044. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  3045. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  3046. struct lpfc_nodelist *pnode;
  3047. unsigned tgt_id = cmnd->device->id;
  3048. unsigned int lun_id = cmnd->device->lun;
  3049. struct lpfc_scsi_event_header scsi_event;
  3050. int status;
  3051. if (!rdata) {
  3052. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3053. "0799 Target Reset rport failure: rdata x%p\n", rdata);
  3054. return FAILED;
  3055. }
  3056. pnode = rdata->pnode;
  3057. fc_block_scsi_eh(cmnd);
  3058. status = lpfc_chk_tgt_mapped(vport, cmnd);
  3059. if (status == FAILED) {
  3060. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3061. "0722 Target Reset rport failure: rdata x%p\n", rdata);
  3062. return FAILED;
  3063. }
  3064. scsi_event.event_type = FC_REG_SCSI_EVENT;
  3065. scsi_event.subcategory = LPFC_EVENT_TGTRESET;
  3066. scsi_event.lun = 0;
  3067. memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
  3068. memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
  3069. fc_host_post_vendor_event(shost, fc_get_event_number(),
  3070. sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
  3071. status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
  3072. FCP_TARGET_RESET);
  3073. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3074. "0723 SCSI layer issued Target Reset (%d, %d) "
  3075. "return x%x\n", tgt_id, lun_id, status);
  3076. /*
  3077. * We have to clean up i/o as : they may be orphaned by the TMF;
  3078. * or if the TMF failed, they may be in an indeterminate state.
  3079. * So, continue on.
  3080. * We will report success if all the i/o aborts successfully.
  3081. */
  3082. status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
  3083. LPFC_CTX_TGT);
  3084. return status;
  3085. }
  3086. /**
  3087. * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
  3088. * @cmnd: Pointer to scsi_cmnd data structure.
  3089. *
  3090. * This routine does target reset to all targets on @cmnd->device->host.
  3091. * This emulates Parallel SCSI Bus Reset Semantics.
  3092. *
  3093. * Return code :
  3094. * 0x2003 - Error
  3095. * 0x2002 - Success
  3096. **/
  3097. static int
  3098. lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
  3099. {
  3100. struct Scsi_Host *shost = cmnd->device->host;
  3101. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  3102. struct lpfc_nodelist *ndlp = NULL;
  3103. struct lpfc_scsi_event_header scsi_event;
  3104. int match;
  3105. int ret = SUCCESS, status, i;
  3106. scsi_event.event_type = FC_REG_SCSI_EVENT;
  3107. scsi_event.subcategory = LPFC_EVENT_BUSRESET;
  3108. scsi_event.lun = 0;
  3109. memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
  3110. memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
  3111. fc_host_post_vendor_event(shost, fc_get_event_number(),
  3112. sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
  3113. fc_block_scsi_eh(cmnd);
  3114. /*
  3115. * Since the driver manages a single bus device, reset all
  3116. * targets known to the driver. Should any target reset
  3117. * fail, this routine returns failure to the midlayer.
  3118. */
  3119. for (i = 0; i < LPFC_MAX_TARGET; i++) {
  3120. /* Search for mapped node by target ID */
  3121. match = 0;
  3122. spin_lock_irq(shost->host_lock);
  3123. list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
  3124. if (!NLP_CHK_NODE_ACT(ndlp))
  3125. continue;
  3126. if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
  3127. ndlp->nlp_sid == i &&
  3128. ndlp->rport) {
  3129. match = 1;
  3130. break;
  3131. }
  3132. }
  3133. spin_unlock_irq(shost->host_lock);
  3134. if (!match)
  3135. continue;
  3136. status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
  3137. i, 0, FCP_TARGET_RESET);
  3138. if (status != SUCCESS) {
  3139. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3140. "0700 Bus Reset on target %d failed\n",
  3141. i);
  3142. ret = FAILED;
  3143. }
  3144. }
  3145. /*
  3146. * We have to clean up i/o as : they may be orphaned by the TMFs
  3147. * above; or if any of the TMFs failed, they may be in an
  3148. * indeterminate state.
  3149. * We will report success if all the i/o aborts successfully.
  3150. */
  3151. status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
  3152. if (status != SUCCESS)
  3153. ret = FAILED;
  3154. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3155. "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
  3156. return ret;
  3157. }
  3158. /**
  3159. * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
  3160. * @sdev: Pointer to scsi_device.
  3161. *
  3162. * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
  3163. * globally available list of scsi buffers. This routine also makes sure scsi
  3164. * buffer is not allocated more than HBA limit conveyed to midlayer. This list
  3165. * of scsi buffer exists for the lifetime of the driver.
  3166. *
  3167. * Return codes:
  3168. * non-0 - Error
  3169. * 0 - Success
  3170. **/
  3171. static int
  3172. lpfc_slave_alloc(struct scsi_device *sdev)
  3173. {
  3174. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  3175. struct lpfc_hba *phba = vport->phba;
  3176. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  3177. uint32_t total = 0;
  3178. uint32_t num_to_alloc = 0;
  3179. int num_allocated = 0;
  3180. if (!rport || fc_remote_port_chkready(rport))
  3181. return -ENXIO;
  3182. sdev->hostdata = rport->dd_data;
  3183. /*
  3184. * Populate the cmds_per_lun count scsi_bufs into this host's globally
  3185. * available list of scsi buffers. Don't allocate more than the
  3186. * HBA limit conveyed to the midlayer via the host structure. The
  3187. * formula accounts for the lun_queue_depth + error handlers + 1
  3188. * extra. This list of scsi bufs exists for the lifetime of the driver.
  3189. */
  3190. total = phba->total_scsi_bufs;
  3191. num_to_alloc = vport->cfg_lun_queue_depth + 2;
  3192. /* Allow some exchanges to be available always to complete discovery */
  3193. if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
  3194. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  3195. "0704 At limitation of %d preallocated "
  3196. "command buffers\n", total);
  3197. return 0;
  3198. /* Allow some exchanges to be available always to complete discovery */
  3199. } else if (total + num_to_alloc >
  3200. phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
  3201. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  3202. "0705 Allocation request of %d "
  3203. "command buffers will exceed max of %d. "
  3204. "Reducing allocation request to %d.\n",
  3205. num_to_alloc, phba->cfg_hba_queue_depth,
  3206. (phba->cfg_hba_queue_depth - total));
  3207. num_to_alloc = phba->cfg_hba_queue_depth - total;
  3208. }
  3209. num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
  3210. if (num_to_alloc != num_allocated) {
  3211. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  3212. "0708 Allocation request of %d "
  3213. "command buffers did not succeed. "
  3214. "Allocated %d buffers.\n",
  3215. num_to_alloc, num_allocated);
  3216. }
  3217. if (num_allocated > 0)
  3218. phba->total_scsi_bufs += num_allocated;
  3219. return 0;
  3220. }
  3221. /**
  3222. * lpfc_slave_configure - scsi_host_template slave_configure entry point
  3223. * @sdev: Pointer to scsi_device.
  3224. *
  3225. * This routine configures following items
  3226. * - Tag command queuing support for @sdev if supported.
  3227. * - Dev loss time out value of fc_rport.
  3228. * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
  3229. *
  3230. * Return codes:
  3231. * 0 - Success
  3232. **/
  3233. static int
  3234. lpfc_slave_configure(struct scsi_device *sdev)
  3235. {
  3236. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  3237. struct lpfc_hba *phba = vport->phba;
  3238. struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
  3239. if (sdev->tagged_supported)
  3240. scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
  3241. else
  3242. scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
  3243. /*
  3244. * Initialize the fc transport attributes for the target
  3245. * containing this scsi device. Also note that the driver's
  3246. * target pointer is stored in the starget_data for the
  3247. * driver's sysfs entry point functions.
  3248. */
  3249. rport->dev_loss_tmo = vport->cfg_devloss_tmo;
  3250. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  3251. lpfc_sli_handle_fast_ring_event(phba,
  3252. &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
  3253. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  3254. lpfc_poll_rearm_timer(phba);
  3255. }
  3256. return 0;
  3257. }
  3258. /**
  3259. * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
  3260. * @sdev: Pointer to scsi_device.
  3261. *
  3262. * This routine sets @sdev hostatdata filed to null.
  3263. **/
  3264. static void
  3265. lpfc_slave_destroy(struct scsi_device *sdev)
  3266. {
  3267. sdev->hostdata = NULL;
  3268. return;
  3269. }
  3270. struct scsi_host_template lpfc_template = {
  3271. .module = THIS_MODULE,
  3272. .name = LPFC_DRIVER_NAME,
  3273. .info = lpfc_info,
  3274. .queuecommand = lpfc_queuecommand,
  3275. .eh_abort_handler = lpfc_abort_handler,
  3276. .eh_device_reset_handler = lpfc_device_reset_handler,
  3277. .eh_target_reset_handler = lpfc_target_reset_handler,
  3278. .eh_bus_reset_handler = lpfc_bus_reset_handler,
  3279. .slave_alloc = lpfc_slave_alloc,
  3280. .slave_configure = lpfc_slave_configure,
  3281. .slave_destroy = lpfc_slave_destroy,
  3282. .scan_finished = lpfc_scan_finished,
  3283. .this_id = -1,
  3284. .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
  3285. .cmd_per_lun = LPFC_CMD_PER_LUN,
  3286. .use_clustering = ENABLE_CLUSTERING,
  3287. .shost_attrs = lpfc_hba_attrs,
  3288. .max_sectors = 0xFFFF,
  3289. .vendor_id = LPFC_NL_VENDOR_ID,
  3290. .change_queue_depth = lpfc_change_queue_depth,
  3291. };
  3292. struct scsi_host_template lpfc_vport_template = {
  3293. .module = THIS_MODULE,
  3294. .name = LPFC_DRIVER_NAME,
  3295. .info = lpfc_info,
  3296. .queuecommand = lpfc_queuecommand,
  3297. .eh_abort_handler = lpfc_abort_handler,
  3298. .eh_device_reset_handler = lpfc_device_reset_handler,
  3299. .eh_target_reset_handler = lpfc_target_reset_handler,
  3300. .eh_bus_reset_handler = lpfc_bus_reset_handler,
  3301. .slave_alloc = lpfc_slave_alloc,
  3302. .slave_configure = lpfc_slave_configure,
  3303. .slave_destroy = lpfc_slave_destroy,
  3304. .scan_finished = lpfc_scan_finished,
  3305. .this_id = -1,
  3306. .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
  3307. .cmd_per_lun = LPFC_CMD_PER_LUN,
  3308. .use_clustering = ENABLE_CLUSTERING,
  3309. .shost_attrs = lpfc_vport_attrs,
  3310. .max_sectors = 0xFFFF,
  3311. .change_queue_depth = lpfc_change_queue_depth,
  3312. };