/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

Large files are truncated click here to view the full file

  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__,