dma.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /*
  2. * Filename: dma.c
  3. *
  4. *
  5. * Authors: Joshua Morris <josh.h.morris@us.ibm.com>
  6. * Philip Kelleher <pjk1939@linux.vnet.ibm.com>
  7. *
  8. * (C) Copyright 2013 IBM Corporation
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software Foundation,
  22. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/slab.h>
  25. #include "rsxx_priv.h"
  26. struct rsxx_dma {
  27. struct list_head list;
  28. u8 cmd;
  29. unsigned int laddr; /* Logical address */
  30. struct {
  31. u32 off;
  32. u32 cnt;
  33. } sub_page;
  34. dma_addr_t dma_addr;
  35. struct page *page;
  36. unsigned int pg_off; /* Page Offset */
  37. rsxx_dma_cb cb;
  38. void *cb_data;
  39. };
  40. /* This timeout is used to detect a stalled DMA channel */
  41. #define DMA_ACTIVITY_TIMEOUT msecs_to_jiffies(10000)
  42. struct hw_status {
  43. u8 status;
  44. u8 tag;
  45. __le16 count;
  46. __le32 _rsvd2;
  47. __le64 _rsvd3;
  48. } __packed;
  49. enum rsxx_dma_status {
  50. DMA_SW_ERR = 0x1,
  51. DMA_HW_FAULT = 0x2,
  52. DMA_CANCELLED = 0x4,
  53. };
  54. struct hw_cmd {
  55. u8 command;
  56. u8 tag;
  57. u8 _rsvd;
  58. u8 sub_page; /* Bit[0:2]: 512byte offset */
  59. /* Bit[4:6]: 512byte count */
  60. __le32 device_addr;
  61. __le64 host_addr;
  62. } __packed;
  63. enum rsxx_hw_cmd {
  64. HW_CMD_BLK_DISCARD = 0x70,
  65. HW_CMD_BLK_WRITE = 0x80,
  66. HW_CMD_BLK_READ = 0xC0,
  67. HW_CMD_BLK_RECON_READ = 0xE0,
  68. };
  69. enum rsxx_hw_status {
  70. HW_STATUS_CRC = 0x01,
  71. HW_STATUS_HARD_ERR = 0x02,
  72. HW_STATUS_SOFT_ERR = 0x04,
  73. HW_STATUS_FAULT = 0x08,
  74. };
  75. static struct kmem_cache *rsxx_dma_pool;
  76. struct dma_tracker {
  77. int next_tag;
  78. struct rsxx_dma *dma;
  79. };
  80. #define DMA_TRACKER_LIST_SIZE8 (sizeof(struct dma_tracker_list) + \
  81. (sizeof(struct dma_tracker) * RSXX_MAX_OUTSTANDING_CMDS))
  82. struct dma_tracker_list {
  83. spinlock_t lock;
  84. int head;
  85. struct dma_tracker list[0];
  86. };
  87. /*----------------- Misc Utility Functions -------------------*/
  88. static unsigned int rsxx_addr8_to_laddr(u64 addr8, struct rsxx_cardinfo *card)
  89. {
  90. unsigned long long tgt_addr8;
  91. tgt_addr8 = ((addr8 >> card->_stripe.upper_shift) &
  92. card->_stripe.upper_mask) |
  93. ((addr8) & card->_stripe.lower_mask);
  94. do_div(tgt_addr8, RSXX_HW_BLK_SIZE);
  95. return tgt_addr8;
  96. }
  97. static unsigned int rsxx_get_dma_tgt(struct rsxx_cardinfo *card, u64 addr8)
  98. {
  99. unsigned int tgt;
  100. tgt = (addr8 >> card->_stripe.target_shift) & card->_stripe.target_mask;
  101. return tgt;
  102. }
  103. void rsxx_dma_queue_reset(struct rsxx_cardinfo *card)
  104. {
  105. /* Reset all DMA Command/Status Queues */
  106. iowrite32(DMA_QUEUE_RESET, card->regmap + RESET);
  107. }
  108. static unsigned int get_dma_size(struct rsxx_dma *dma)
  109. {
  110. if (dma->sub_page.cnt)
  111. return dma->sub_page.cnt << 9;
  112. else
  113. return RSXX_HW_BLK_SIZE;
  114. }
  115. /*----------------- DMA Tracker -------------------*/
  116. static void set_tracker_dma(struct dma_tracker_list *trackers,
  117. int tag,
  118. struct rsxx_dma *dma)
  119. {
  120. trackers->list[tag].dma = dma;
  121. }
  122. static struct rsxx_dma *get_tracker_dma(struct dma_tracker_list *trackers,
  123. int tag)
  124. {
  125. return trackers->list[tag].dma;
  126. }
  127. static int pop_tracker(struct dma_tracker_list *trackers)
  128. {
  129. int tag;
  130. spin_lock(&trackers->lock);
  131. tag = trackers->head;
  132. if (tag != -1) {
  133. trackers->head = trackers->list[tag].next_tag;
  134. trackers->list[tag].next_tag = -1;
  135. }
  136. spin_unlock(&trackers->lock);
  137. return tag;
  138. }
  139. static void push_tracker(struct dma_tracker_list *trackers, int tag)
  140. {
  141. spin_lock(&trackers->lock);
  142. trackers->list[tag].next_tag = trackers->head;
  143. trackers->head = tag;
  144. trackers->list[tag].dma = NULL;
  145. spin_unlock(&trackers->lock);
  146. }
  147. /*----------------- Interrupt Coalescing -------------*/
  148. /*
  149. * Interrupt Coalescing Register Format:
  150. * Interrupt Timer (64ns units) [15:0]
  151. * Interrupt Count [24:16]
  152. * Reserved [31:25]
  153. */
  154. #define INTR_COAL_LATENCY_MASK (0x0000ffff)
  155. #define INTR_COAL_COUNT_SHIFT 16
  156. #define INTR_COAL_COUNT_BITS 9
  157. #define INTR_COAL_COUNT_MASK (((1 << INTR_COAL_COUNT_BITS) - 1) << \
  158. INTR_COAL_COUNT_SHIFT)
  159. #define INTR_COAL_LATENCY_UNITS_NS 64
  160. static u32 dma_intr_coal_val(u32 mode, u32 count, u32 latency)
  161. {
  162. u32 latency_units = latency / INTR_COAL_LATENCY_UNITS_NS;
  163. if (mode == RSXX_INTR_COAL_DISABLED)
  164. return 0;
  165. return ((count << INTR_COAL_COUNT_SHIFT) & INTR_COAL_COUNT_MASK) |
  166. (latency_units & INTR_COAL_LATENCY_MASK);
  167. }
  168. static void dma_intr_coal_auto_tune(struct rsxx_cardinfo *card)
  169. {
  170. int i;
  171. u32 q_depth = 0;
  172. u32 intr_coal;
  173. if (card->config.data.intr_coal.mode != RSXX_INTR_COAL_AUTO_TUNE ||
  174. unlikely(card->eeh_state))
  175. return;
  176. for (i = 0; i < card->n_targets; i++)
  177. q_depth += atomic_read(&card->ctrl[i].stats.hw_q_depth);
  178. intr_coal = dma_intr_coal_val(card->config.data.intr_coal.mode,
  179. q_depth / 2,
  180. card->config.data.intr_coal.latency);
  181. iowrite32(intr_coal, card->regmap + INTR_COAL);
  182. }
  183. /*----------------- RSXX DMA Handling -------------------*/
  184. static void rsxx_free_dma(struct rsxx_dma_ctrl *ctrl, struct rsxx_dma *dma)
  185. {
  186. if (dma->cmd != HW_CMD_BLK_DISCARD) {
  187. if (!pci_dma_mapping_error(ctrl->card->dev, dma->dma_addr)) {
  188. pci_unmap_page(ctrl->card->dev, dma->dma_addr,
  189. get_dma_size(dma),
  190. dma->cmd == HW_CMD_BLK_WRITE ?
  191. PCI_DMA_TODEVICE :
  192. PCI_DMA_FROMDEVICE);
  193. }
  194. }
  195. kmem_cache_free(rsxx_dma_pool, dma);
  196. }
  197. static void rsxx_complete_dma(struct rsxx_dma_ctrl *ctrl,
  198. struct rsxx_dma *dma,
  199. unsigned int status)
  200. {
  201. if (status & DMA_SW_ERR)
  202. ctrl->stats.dma_sw_err++;
  203. if (status & DMA_HW_FAULT)
  204. ctrl->stats.dma_hw_fault++;
  205. if (status & DMA_CANCELLED)
  206. ctrl->stats.dma_cancelled++;
  207. if (dma->cb)
  208. dma->cb(ctrl->card, dma->cb_data, status ? 1 : 0);
  209. rsxx_free_dma(ctrl, dma);
  210. }
  211. int rsxx_cleanup_dma_queue(struct rsxx_dma_ctrl *ctrl,
  212. struct list_head *q, unsigned int done)
  213. {
  214. struct rsxx_dma *dma;
  215. struct rsxx_dma *tmp;
  216. int cnt = 0;
  217. list_for_each_entry_safe(dma, tmp, q, list) {
  218. list_del(&dma->list);
  219. if (done & COMPLETE_DMA)
  220. rsxx_complete_dma(ctrl, dma, DMA_CANCELLED);
  221. else
  222. rsxx_free_dma(ctrl, dma);
  223. cnt++;
  224. }
  225. return cnt;
  226. }
  227. static void rsxx_requeue_dma(struct rsxx_dma_ctrl *ctrl,
  228. struct rsxx_dma *dma)
  229. {
  230. /*
  231. * Requeued DMAs go to the front of the queue so they are issued
  232. * first.
  233. */
  234. spin_lock_bh(&ctrl->queue_lock);
  235. ctrl->stats.sw_q_depth++;
  236. list_add(&dma->list, &ctrl->queue);
  237. spin_unlock_bh(&ctrl->queue_lock);
  238. }
  239. static void rsxx_handle_dma_error(struct rsxx_dma_ctrl *ctrl,
  240. struct rsxx_dma *dma,
  241. u8 hw_st)
  242. {
  243. unsigned int status = 0;
  244. int requeue_cmd = 0;
  245. dev_dbg(CARD_TO_DEV(ctrl->card),
  246. "Handling DMA error(cmd x%02x, laddr x%08x st:x%02x)\n",
  247. dma->cmd, dma->laddr, hw_st);
  248. if (hw_st & HW_STATUS_CRC)
  249. ctrl->stats.crc_errors++;
  250. if (hw_st & HW_STATUS_HARD_ERR)
  251. ctrl->stats.hard_errors++;
  252. if (hw_st & HW_STATUS_SOFT_ERR)
  253. ctrl->stats.soft_errors++;
  254. switch (dma->cmd) {
  255. case HW_CMD_BLK_READ:
  256. if (hw_st & (HW_STATUS_CRC | HW_STATUS_HARD_ERR)) {
  257. if (ctrl->card->scrub_hard) {
  258. dma->cmd = HW_CMD_BLK_RECON_READ;
  259. requeue_cmd = 1;
  260. ctrl->stats.reads_retried++;
  261. } else {
  262. status |= DMA_HW_FAULT;
  263. ctrl->stats.reads_failed++;
  264. }
  265. } else if (hw_st & HW_STATUS_FAULT) {
  266. status |= DMA_HW_FAULT;
  267. ctrl->stats.reads_failed++;
  268. }
  269. break;
  270. case HW_CMD_BLK_RECON_READ:
  271. if (hw_st & (HW_STATUS_CRC | HW_STATUS_HARD_ERR)) {
  272. /* Data could not be reconstructed. */
  273. status |= DMA_HW_FAULT;
  274. ctrl->stats.reads_failed++;
  275. }
  276. break;
  277. case HW_CMD_BLK_WRITE:
  278. status |= DMA_HW_FAULT;
  279. ctrl->stats.writes_failed++;
  280. break;
  281. case HW_CMD_BLK_DISCARD:
  282. status |= DMA_HW_FAULT;
  283. ctrl->stats.discards_failed++;
  284. break;
  285. default:
  286. dev_err(CARD_TO_DEV(ctrl->card),
  287. "Unknown command in DMA!(cmd: x%02x "
  288. "laddr x%08x st: x%02x\n",
  289. dma->cmd, dma->laddr, hw_st);
  290. status |= DMA_SW_ERR;
  291. break;
  292. }
  293. if (requeue_cmd)
  294. rsxx_requeue_dma(ctrl, dma);
  295. else
  296. rsxx_complete_dma(ctrl, dma, status);
  297. }
  298. static void dma_engine_stalled(unsigned long data)
  299. {
  300. struct rsxx_dma_ctrl *ctrl = (struct rsxx_dma_ctrl *)data;
  301. int cnt;
  302. if (atomic_read(&ctrl->stats.hw_q_depth) == 0 ||
  303. unlikely(ctrl->card->eeh_state))
  304. return;
  305. if (ctrl->cmd.idx != ioread32(ctrl->regmap + SW_CMD_IDX)) {
  306. /*
  307. * The dma engine was stalled because the SW_CMD_IDX write
  308. * was lost. Issue it again to recover.
  309. */
  310. dev_warn(CARD_TO_DEV(ctrl->card),
  311. "SW_CMD_IDX write was lost, re-writing...\n");
  312. iowrite32(ctrl->cmd.idx, ctrl->regmap + SW_CMD_IDX);
  313. mod_timer(&ctrl->activity_timer,
  314. jiffies + DMA_ACTIVITY_TIMEOUT);
  315. } else {
  316. dev_warn(CARD_TO_DEV(ctrl->card),
  317. "DMA channel %d has stalled, faulting interface.\n",
  318. ctrl->id);
  319. ctrl->card->dma_fault = 1;
  320. /* Clean up the DMA queue */
  321. spin_lock(&ctrl->queue_lock);
  322. cnt = rsxx_cleanup_dma_queue(ctrl, &ctrl->queue, COMPLETE_DMA);
  323. spin_unlock(&ctrl->queue_lock);
  324. cnt += rsxx_dma_cancel(ctrl);
  325. if (cnt)
  326. dev_info(CARD_TO_DEV(ctrl->card),
  327. "Freed %d queued DMAs on channel %d\n",
  328. cnt, ctrl->id);
  329. }
  330. }
  331. static void rsxx_issue_dmas(struct rsxx_dma_ctrl *ctrl)
  332. {
  333. struct rsxx_dma *dma;
  334. int tag;
  335. int cmds_pending = 0;
  336. struct hw_cmd *hw_cmd_buf;
  337. int dir;
  338. hw_cmd_buf = ctrl->cmd.buf;
  339. if (unlikely(ctrl->card->halt) ||
  340. unlikely(ctrl->card->eeh_state))
  341. return;
  342. while (1) {
  343. spin_lock_bh(&ctrl->queue_lock);
  344. if (list_empty(&ctrl->queue)) {
  345. spin_unlock_bh(&ctrl->queue_lock);
  346. break;
  347. }
  348. spin_unlock_bh(&ctrl->queue_lock);
  349. tag = pop_tracker(ctrl->trackers);
  350. if (tag == -1)
  351. break;
  352. spin_lock_bh(&ctrl->queue_lock);
  353. dma = list_entry(ctrl->queue.next, struct rsxx_dma, list);
  354. list_del(&dma->list);
  355. ctrl->stats.sw_q_depth--;
  356. spin_unlock_bh(&ctrl->queue_lock);
  357. /*
  358. * This will catch any DMAs that slipped in right before the
  359. * fault, but was queued after all the other DMAs were
  360. * cancelled.
  361. */
  362. if (unlikely(ctrl->card->dma_fault)) {
  363. push_tracker(ctrl->trackers, tag);
  364. rsxx_complete_dma(ctrl, dma, DMA_CANCELLED);
  365. continue;
  366. }
  367. if (dma->cmd != HW_CMD_BLK_DISCARD) {
  368. if (dma->cmd == HW_CMD_BLK_WRITE)
  369. dir = PCI_DMA_TODEVICE;
  370. else
  371. dir = PCI_DMA_FROMDEVICE;
  372. /*
  373. * The function pci_map_page is placed here because we
  374. * can only, by design, issue up to 255 commands to the
  375. * hardware at one time per DMA channel. So the maximum
  376. * amount of mapped memory would be 255 * 4 channels *
  377. * 4096 Bytes which is less than 2GB, the limit of a x8
  378. * Non-HWWD PCIe slot. This way the pci_map_page
  379. * function should never fail because of a lack of
  380. * mappable memory.
  381. */
  382. dma->dma_addr = pci_map_page(ctrl->card->dev, dma->page,
  383. dma->pg_off, dma->sub_page.cnt << 9, dir);
  384. if (pci_dma_mapping_error(ctrl->card->dev, dma->dma_addr)) {
  385. push_tracker(ctrl->trackers, tag);
  386. rsxx_complete_dma(ctrl, dma, DMA_CANCELLED);
  387. continue;
  388. }
  389. }
  390. set_tracker_dma(ctrl->trackers, tag, dma);
  391. hw_cmd_buf[ctrl->cmd.idx].command = dma->cmd;
  392. hw_cmd_buf[ctrl->cmd.idx].tag = tag;
  393. hw_cmd_buf[ctrl->cmd.idx]._rsvd = 0;
  394. hw_cmd_buf[ctrl->cmd.idx].sub_page =
  395. ((dma->sub_page.cnt & 0x7) << 4) |
  396. (dma->sub_page.off & 0x7);
  397. hw_cmd_buf[ctrl->cmd.idx].device_addr =
  398. cpu_to_le32(dma->laddr);
  399. hw_cmd_buf[ctrl->cmd.idx].host_addr =
  400. cpu_to_le64(dma->dma_addr);
  401. dev_dbg(CARD_TO_DEV(ctrl->card),
  402. "Issue DMA%d(laddr %d tag %d) to idx %d\n",
  403. ctrl->id, dma->laddr, tag, ctrl->cmd.idx);
  404. ctrl->cmd.idx = (ctrl->cmd.idx + 1) & RSXX_CS_IDX_MASK;
  405. cmds_pending++;
  406. if (dma->cmd == HW_CMD_BLK_WRITE)
  407. ctrl->stats.writes_issued++;
  408. else if (dma->cmd == HW_CMD_BLK_DISCARD)
  409. ctrl->stats.discards_issued++;
  410. else
  411. ctrl->stats.reads_issued++;
  412. }
  413. /* Let HW know we've queued commands. */
  414. if (cmds_pending) {
  415. atomic_add(cmds_pending, &ctrl->stats.hw_q_depth);
  416. mod_timer(&ctrl->activity_timer,
  417. jiffies + DMA_ACTIVITY_TIMEOUT);
  418. if (unlikely(ctrl->card->eeh_state)) {
  419. del_timer_sync(&ctrl->activity_timer);
  420. return;
  421. }
  422. iowrite32(ctrl->cmd.idx, ctrl->regmap + SW_CMD_IDX);
  423. }
  424. }
  425. static void rsxx_dma_done(struct rsxx_dma_ctrl *ctrl)
  426. {
  427. struct rsxx_dma *dma;
  428. unsigned long flags;
  429. u16 count;
  430. u8 status;
  431. u8 tag;
  432. struct hw_status *hw_st_buf;
  433. hw_st_buf = ctrl->status.buf;
  434. if (unlikely(ctrl->card->halt) ||
  435. unlikely(ctrl->card->dma_fault) ||
  436. unlikely(ctrl->card->eeh_state))
  437. return;
  438. count = le16_to_cpu(hw_st_buf[ctrl->status.idx].count);
  439. while (count == ctrl->e_cnt) {
  440. /*
  441. * The read memory-barrier is necessary to keep aggressive
  442. * processors/optimizers (such as the PPC Apple G5) from
  443. * reordering the following status-buffer tag & status read
  444. * *before* the count read on subsequent iterations of the
  445. * loop!
  446. */
  447. rmb();
  448. status = hw_st_buf[ctrl->status.idx].status;
  449. tag = hw_st_buf[ctrl->status.idx].tag;
  450. dma = get_tracker_dma(ctrl->trackers, tag);
  451. if (dma == NULL) {
  452. spin_lock_irqsave(&ctrl->card->irq_lock, flags);
  453. rsxx_disable_ier(ctrl->card, CR_INTR_DMA_ALL);
  454. spin_unlock_irqrestore(&ctrl->card->irq_lock, flags);
  455. dev_err(CARD_TO_DEV(ctrl->card),
  456. "No tracker for tag %d "
  457. "(idx %d id %d)\n",
  458. tag, ctrl->status.idx, ctrl->id);
  459. return;
  460. }
  461. dev_dbg(CARD_TO_DEV(ctrl->card),
  462. "Completing DMA%d"
  463. "(laddr x%x tag %d st: x%x cnt: x%04x) from idx %d.\n",
  464. ctrl->id, dma->laddr, tag, status, count,
  465. ctrl->status.idx);
  466. atomic_dec(&ctrl->stats.hw_q_depth);
  467. mod_timer(&ctrl->activity_timer,
  468. jiffies + DMA_ACTIVITY_TIMEOUT);
  469. if (status)
  470. rsxx_handle_dma_error(ctrl, dma, status);
  471. else
  472. rsxx_complete_dma(ctrl, dma, 0);
  473. push_tracker(ctrl->trackers, tag);
  474. ctrl->status.idx = (ctrl->status.idx + 1) &
  475. RSXX_CS_IDX_MASK;
  476. ctrl->e_cnt++;
  477. count = le16_to_cpu(hw_st_buf[ctrl->status.idx].count);
  478. }
  479. dma_intr_coal_auto_tune(ctrl->card);
  480. if (atomic_read(&ctrl->stats.hw_q_depth) == 0)
  481. del_timer_sync(&ctrl->activity_timer);
  482. spin_lock_irqsave(&ctrl->card->irq_lock, flags);
  483. rsxx_enable_ier(ctrl->card, CR_INTR_DMA(ctrl->id));
  484. spin_unlock_irqrestore(&ctrl->card->irq_lock, flags);
  485. spin_lock_bh(&ctrl->queue_lock);
  486. if (ctrl->stats.sw_q_depth)
  487. queue_work(ctrl->issue_wq, &ctrl->issue_dma_work);
  488. spin_unlock_bh(&ctrl->queue_lock);
  489. }
  490. static void rsxx_schedule_issue(struct work_struct *work)
  491. {
  492. struct rsxx_dma_ctrl *ctrl;
  493. ctrl = container_of(work, struct rsxx_dma_ctrl, issue_dma_work);
  494. mutex_lock(&ctrl->work_lock);
  495. rsxx_issue_dmas(ctrl);
  496. mutex_unlock(&ctrl->work_lock);
  497. }
  498. static void rsxx_schedule_done(struct work_struct *work)
  499. {
  500. struct rsxx_dma_ctrl *ctrl;
  501. ctrl = container_of(work, struct rsxx_dma_ctrl, dma_done_work);
  502. mutex_lock(&ctrl->work_lock);
  503. rsxx_dma_done(ctrl);
  504. mutex_unlock(&ctrl->work_lock);
  505. }
  506. static int rsxx_queue_discard(struct rsxx_cardinfo *card,
  507. struct list_head *q,
  508. unsigned int laddr,
  509. rsxx_dma_cb cb,
  510. void *cb_data)
  511. {
  512. struct rsxx_dma *dma;
  513. dma = kmem_cache_alloc(rsxx_dma_pool, GFP_KERNEL);
  514. if (!dma)
  515. return -ENOMEM;
  516. dma->cmd = HW_CMD_BLK_DISCARD;
  517. dma->laddr = laddr;
  518. dma->dma_addr = 0;
  519. dma->sub_page.off = 0;
  520. dma->sub_page.cnt = 0;
  521. dma->page = NULL;
  522. dma->pg_off = 0;
  523. dma->cb = cb;
  524. dma->cb_data = cb_data;
  525. dev_dbg(CARD_TO_DEV(card), "Queuing[D] laddr %x\n", dma->laddr);
  526. list_add_tail(&dma->list, q);
  527. return 0;
  528. }
  529. static int rsxx_queue_dma(struct rsxx_cardinfo *card,
  530. struct list_head *q,
  531. int dir,
  532. unsigned int dma_off,
  533. unsigned int dma_len,
  534. unsigned int laddr,
  535. struct page *page,
  536. unsigned int pg_off,
  537. rsxx_dma_cb cb,
  538. void *cb_data)
  539. {
  540. struct rsxx_dma *dma;
  541. dma = kmem_cache_alloc(rsxx_dma_pool, GFP_KERNEL);
  542. if (!dma)
  543. return -ENOMEM;
  544. dma->cmd = dir ? HW_CMD_BLK_WRITE : HW_CMD_BLK_READ;
  545. dma->laddr = laddr;
  546. dma->sub_page.off = (dma_off >> 9);
  547. dma->sub_page.cnt = (dma_len >> 9);
  548. dma->page = page;
  549. dma->pg_off = pg_off;
  550. dma->cb = cb;
  551. dma->cb_data = cb_data;
  552. dev_dbg(CARD_TO_DEV(card),
  553. "Queuing[%c] laddr %x off %d cnt %d page %p pg_off %d\n",
  554. dir ? 'W' : 'R', dma->laddr, dma->sub_page.off,
  555. dma->sub_page.cnt, dma->page, dma->pg_off);
  556. /* Queue the DMA */
  557. list_add_tail(&dma->list, q);
  558. return 0;
  559. }
  560. int rsxx_dma_queue_bio(struct rsxx_cardinfo *card,
  561. struct bio *bio,
  562. atomic_t *n_dmas,
  563. rsxx_dma_cb cb,
  564. void *cb_data)
  565. {
  566. struct list_head dma_list[RSXX_MAX_TARGETS];
  567. struct bio_vec bvec;
  568. struct bvec_iter iter;
  569. unsigned long long addr8;
  570. unsigned int laddr;
  571. unsigned int bv_len;
  572. unsigned int bv_off;
  573. unsigned int dma_off;
  574. unsigned int dma_len;
  575. int dma_cnt[RSXX_MAX_TARGETS];
  576. int tgt;
  577. int st;
  578. int i;
  579. addr8 = bio->bi_iter.bi_sector << 9; /* sectors are 512 bytes */
  580. atomic_set(n_dmas, 0);
  581. for (i = 0; i < card->n_targets; i++) {
  582. INIT_LIST_HEAD(&dma_list[i]);
  583. dma_cnt[i] = 0;
  584. }
  585. if (bio_op(bio) == REQ_OP_DISCARD) {
  586. bv_len = bio->bi_iter.bi_size;
  587. while (bv_len > 0) {
  588. tgt = rsxx_get_dma_tgt(card, addr8);
  589. laddr = rsxx_addr8_to_laddr(addr8, card);
  590. st = rsxx_queue_discard(card, &dma_list[tgt], laddr,
  591. cb, cb_data);
  592. if (st)
  593. goto bvec_err;
  594. dma_cnt[tgt]++;
  595. atomic_inc(n_dmas);
  596. addr8 += RSXX_HW_BLK_SIZE;
  597. bv_len -= RSXX_HW_BLK_SIZE;
  598. }
  599. } else {
  600. bio_for_each_segment(bvec, bio, iter) {
  601. bv_len = bvec.bv_len;
  602. bv_off = bvec.bv_offset;
  603. while (bv_len > 0) {
  604. tgt = rsxx_get_dma_tgt(card, addr8);
  605. laddr = rsxx_addr8_to_laddr(addr8, card);
  606. dma_off = addr8 & RSXX_HW_BLK_MASK;
  607. dma_len = min(bv_len,
  608. RSXX_HW_BLK_SIZE - dma_off);
  609. st = rsxx_queue_dma(card, &dma_list[tgt],
  610. bio_data_dir(bio),
  611. dma_off, dma_len,
  612. laddr, bvec.bv_page,
  613. bv_off, cb, cb_data);
  614. if (st)
  615. goto bvec_err;
  616. dma_cnt[tgt]++;
  617. atomic_inc(n_dmas);
  618. addr8 += dma_len;
  619. bv_off += dma_len;
  620. bv_len -= dma_len;
  621. }
  622. }
  623. }
  624. for (i = 0; i < card->n_targets; i++) {
  625. if (!list_empty(&dma_list[i])) {
  626. spin_lock_bh(&card->ctrl[i].queue_lock);
  627. card->ctrl[i].stats.sw_q_depth += dma_cnt[i];
  628. list_splice_tail(&dma_list[i], &card->ctrl[i].queue);
  629. spin_unlock_bh(&card->ctrl[i].queue_lock);
  630. queue_work(card->ctrl[i].issue_wq,
  631. &card->ctrl[i].issue_dma_work);
  632. }
  633. }
  634. return 0;
  635. bvec_err:
  636. for (i = 0; i < card->n_targets; i++)
  637. rsxx_cleanup_dma_queue(&card->ctrl[i], &dma_list[i],
  638. FREE_DMA);
  639. return st;
  640. }
  641. /*----------------- DMA Engine Initialization & Setup -------------------*/
  642. int rsxx_hw_buffers_init(struct pci_dev *dev, struct rsxx_dma_ctrl *ctrl)
  643. {
  644. ctrl->status.buf = pci_alloc_consistent(dev, STATUS_BUFFER_SIZE8,
  645. &ctrl->status.dma_addr);
  646. ctrl->cmd.buf = pci_alloc_consistent(dev, COMMAND_BUFFER_SIZE8,
  647. &ctrl->cmd.dma_addr);
  648. if (ctrl->status.buf == NULL || ctrl->cmd.buf == NULL)
  649. return -ENOMEM;
  650. memset(ctrl->status.buf, 0xac, STATUS_BUFFER_SIZE8);
  651. iowrite32(lower_32_bits(ctrl->status.dma_addr),
  652. ctrl->regmap + SB_ADD_LO);
  653. iowrite32(upper_32_bits(ctrl->status.dma_addr),
  654. ctrl->regmap + SB_ADD_HI);
  655. memset(ctrl->cmd.buf, 0x83, COMMAND_BUFFER_SIZE8);
  656. iowrite32(lower_32_bits(ctrl->cmd.dma_addr), ctrl->regmap + CB_ADD_LO);
  657. iowrite32(upper_32_bits(ctrl->cmd.dma_addr), ctrl->regmap + CB_ADD_HI);
  658. ctrl->status.idx = ioread32(ctrl->regmap + HW_STATUS_CNT);
  659. if (ctrl->status.idx > RSXX_MAX_OUTSTANDING_CMDS) {
  660. dev_crit(&dev->dev, "Failed reading status cnt x%x\n",
  661. ctrl->status.idx);
  662. return -EINVAL;
  663. }
  664. iowrite32(ctrl->status.idx, ctrl->regmap + HW_STATUS_CNT);
  665. iowrite32(ctrl->status.idx, ctrl->regmap + SW_STATUS_CNT);
  666. ctrl->cmd.idx = ioread32(ctrl->regmap + HW_CMD_IDX);
  667. if (ctrl->cmd.idx > RSXX_MAX_OUTSTANDING_CMDS) {
  668. dev_crit(&dev->dev, "Failed reading cmd cnt x%x\n",
  669. ctrl->status.idx);
  670. return -EINVAL;
  671. }
  672. iowrite32(ctrl->cmd.idx, ctrl->regmap + HW_CMD_IDX);
  673. iowrite32(ctrl->cmd.idx, ctrl->regmap + SW_CMD_IDX);
  674. return 0;
  675. }
  676. static int rsxx_dma_ctrl_init(struct pci_dev *dev,
  677. struct rsxx_dma_ctrl *ctrl)
  678. {
  679. int i;
  680. int st;
  681. memset(&ctrl->stats, 0, sizeof(ctrl->stats));
  682. ctrl->trackers = vmalloc(DMA_TRACKER_LIST_SIZE8);
  683. if (!ctrl->trackers)
  684. return -ENOMEM;
  685. ctrl->trackers->head = 0;
  686. for (i = 0; i < RSXX_MAX_OUTSTANDING_CMDS; i++) {
  687. ctrl->trackers->list[i].next_tag = i + 1;
  688. ctrl->trackers->list[i].dma = NULL;
  689. }
  690. ctrl->trackers->list[RSXX_MAX_OUTSTANDING_CMDS-1].next_tag = -1;
  691. spin_lock_init(&ctrl->trackers->lock);
  692. spin_lock_init(&ctrl->queue_lock);
  693. mutex_init(&ctrl->work_lock);
  694. INIT_LIST_HEAD(&ctrl->queue);
  695. setup_timer(&ctrl->activity_timer, dma_engine_stalled,
  696. (unsigned long)ctrl);
  697. ctrl->issue_wq = alloc_ordered_workqueue(DRIVER_NAME"_issue", 0);
  698. if (!ctrl->issue_wq)
  699. return -ENOMEM;
  700. ctrl->done_wq = alloc_ordered_workqueue(DRIVER_NAME"_done", 0);
  701. if (!ctrl->done_wq)
  702. return -ENOMEM;
  703. INIT_WORK(&ctrl->issue_dma_work, rsxx_schedule_issue);
  704. INIT_WORK(&ctrl->dma_done_work, rsxx_schedule_done);
  705. st = rsxx_hw_buffers_init(dev, ctrl);
  706. if (st)
  707. return st;
  708. return 0;
  709. }
  710. static int rsxx_dma_stripe_setup(struct rsxx_cardinfo *card,
  711. unsigned int stripe_size8)
  712. {
  713. if (!is_power_of_2(stripe_size8)) {
  714. dev_err(CARD_TO_DEV(card),
  715. "stripe_size is NOT a power of 2!\n");
  716. return -EINVAL;
  717. }
  718. card->_stripe.lower_mask = stripe_size8 - 1;
  719. card->_stripe.upper_mask = ~(card->_stripe.lower_mask);
  720. card->_stripe.upper_shift = ffs(card->n_targets) - 1;
  721. card->_stripe.target_mask = card->n_targets - 1;
  722. card->_stripe.target_shift = ffs(stripe_size8) - 1;
  723. dev_dbg(CARD_TO_DEV(card), "_stripe.lower_mask = x%016llx\n",
  724. card->_stripe.lower_mask);
  725. dev_dbg(CARD_TO_DEV(card), "_stripe.upper_shift = x%016llx\n",
  726. card->_stripe.upper_shift);
  727. dev_dbg(CARD_TO_DEV(card), "_stripe.upper_mask = x%016llx\n",
  728. card->_stripe.upper_mask);
  729. dev_dbg(CARD_TO_DEV(card), "_stripe.target_mask = x%016llx\n",
  730. card->_stripe.target_mask);
  731. dev_dbg(CARD_TO_DEV(card), "_stripe.target_shift = x%016llx\n",
  732. card->_stripe.target_shift);
  733. return 0;
  734. }
  735. int rsxx_dma_configure(struct rsxx_cardinfo *card)
  736. {
  737. u32 intr_coal;
  738. intr_coal = dma_intr_coal_val(card->config.data.intr_coal.mode,
  739. card->config.data.intr_coal.count,
  740. card->config.data.intr_coal.latency);
  741. iowrite32(intr_coal, card->regmap + INTR_COAL);
  742. return rsxx_dma_stripe_setup(card, card->config.data.stripe_size);
  743. }
  744. int rsxx_dma_setup(struct rsxx_cardinfo *card)
  745. {
  746. unsigned long flags;
  747. int st;
  748. int i;
  749. dev_info(CARD_TO_DEV(card),
  750. "Initializing %d DMA targets\n",
  751. card->n_targets);
  752. /* Regmap is divided up into 4K chunks. One for each DMA channel */
  753. for (i = 0; i < card->n_targets; i++)
  754. card->ctrl[i].regmap = card->regmap + (i * 4096);
  755. card->dma_fault = 0;
  756. /* Reset the DMA queues */
  757. rsxx_dma_queue_reset(card);
  758. /************* Setup DMA Control *************/
  759. for (i = 0; i < card->n_targets; i++) {
  760. st = rsxx_dma_ctrl_init(card->dev, &card->ctrl[i]);
  761. if (st)
  762. goto failed_dma_setup;
  763. card->ctrl[i].card = card;
  764. card->ctrl[i].id = i;
  765. }
  766. card->scrub_hard = 1;
  767. if (card->config_valid)
  768. rsxx_dma_configure(card);
  769. /* Enable the interrupts after all setup has completed. */
  770. for (i = 0; i < card->n_targets; i++) {
  771. spin_lock_irqsave(&card->irq_lock, flags);
  772. rsxx_enable_ier_and_isr(card, CR_INTR_DMA(i));
  773. spin_unlock_irqrestore(&card->irq_lock, flags);
  774. }
  775. return 0;
  776. failed_dma_setup:
  777. for (i = 0; i < card->n_targets; i++) {
  778. struct rsxx_dma_ctrl *ctrl = &card->ctrl[i];
  779. if (ctrl->issue_wq) {
  780. destroy_workqueue(ctrl->issue_wq);
  781. ctrl->issue_wq = NULL;
  782. }
  783. if (ctrl->done_wq) {
  784. destroy_workqueue(ctrl->done_wq);
  785. ctrl->done_wq = NULL;
  786. }
  787. if (ctrl->trackers)
  788. vfree(ctrl->trackers);
  789. if (ctrl->status.buf)
  790. pci_free_consistent(card->dev, STATUS_BUFFER_SIZE8,
  791. ctrl->status.buf,
  792. ctrl->status.dma_addr);
  793. if (ctrl->cmd.buf)
  794. pci_free_consistent(card->dev, COMMAND_BUFFER_SIZE8,
  795. ctrl->cmd.buf, ctrl->cmd.dma_addr);
  796. }
  797. return st;
  798. }
  799. int rsxx_dma_cancel(struct rsxx_dma_ctrl *ctrl)
  800. {
  801. struct rsxx_dma *dma;
  802. int i;
  803. int cnt = 0;
  804. /* Clean up issued DMAs */
  805. for (i = 0; i < RSXX_MAX_OUTSTANDING_CMDS; i++) {
  806. dma = get_tracker_dma(ctrl->trackers, i);
  807. if (dma) {
  808. atomic_dec(&ctrl->stats.hw_q_depth);
  809. rsxx_complete_dma(ctrl, dma, DMA_CANCELLED);
  810. push_tracker(ctrl->trackers, i);
  811. cnt++;
  812. }
  813. }
  814. return cnt;
  815. }
  816. void rsxx_dma_destroy(struct rsxx_cardinfo *card)
  817. {
  818. struct rsxx_dma_ctrl *ctrl;
  819. int i;
  820. for (i = 0; i < card->n_targets; i++) {
  821. ctrl = &card->ctrl[i];
  822. if (ctrl->issue_wq) {
  823. destroy_workqueue(ctrl->issue_wq);
  824. ctrl->issue_wq = NULL;
  825. }
  826. if (ctrl->done_wq) {
  827. destroy_workqueue(ctrl->done_wq);
  828. ctrl->done_wq = NULL;
  829. }
  830. if (timer_pending(&ctrl->activity_timer))
  831. del_timer_sync(&ctrl->activity_timer);
  832. /* Clean up the DMA queue */
  833. spin_lock_bh(&ctrl->queue_lock);
  834. rsxx_cleanup_dma_queue(ctrl, &ctrl->queue, COMPLETE_DMA);
  835. spin_unlock_bh(&ctrl->queue_lock);
  836. rsxx_dma_cancel(ctrl);
  837. vfree(ctrl->trackers);
  838. pci_free_consistent(card->dev, STATUS_BUFFER_SIZE8,
  839. ctrl->status.buf, ctrl->status.dma_addr);
  840. pci_free_consistent(card->dev, COMMAND_BUFFER_SIZE8,
  841. ctrl->cmd.buf, ctrl->cmd.dma_addr);
  842. }
  843. }
  844. int rsxx_eeh_save_issued_dmas(struct rsxx_cardinfo *card)
  845. {
  846. int i;
  847. int j;
  848. int cnt;
  849. struct rsxx_dma *dma;
  850. struct list_head *issued_dmas;
  851. issued_dmas = kzalloc(sizeof(*issued_dmas) * card->n_targets,
  852. GFP_KERNEL);
  853. if (!issued_dmas)
  854. return -ENOMEM;
  855. for (i = 0; i < card->n_targets; i++) {
  856. INIT_LIST_HEAD(&issued_dmas[i]);
  857. cnt = 0;
  858. for (j = 0; j < RSXX_MAX_OUTSTANDING_CMDS; j++) {
  859. dma = get_tracker_dma(card->ctrl[i].trackers, j);
  860. if (dma == NULL)
  861. continue;
  862. if (dma->cmd == HW_CMD_BLK_WRITE)
  863. card->ctrl[i].stats.writes_issued--;
  864. else if (dma->cmd == HW_CMD_BLK_DISCARD)
  865. card->ctrl[i].stats.discards_issued--;
  866. else
  867. card->ctrl[i].stats.reads_issued--;
  868. if (dma->cmd != HW_CMD_BLK_DISCARD) {
  869. pci_unmap_page(card->dev, dma->dma_addr,
  870. get_dma_size(dma),
  871. dma->cmd == HW_CMD_BLK_WRITE ?
  872. PCI_DMA_TODEVICE :
  873. PCI_DMA_FROMDEVICE);
  874. }
  875. list_add_tail(&dma->list, &issued_dmas[i]);
  876. push_tracker(card->ctrl[i].trackers, j);
  877. cnt++;
  878. }
  879. spin_lock_bh(&card->ctrl[i].queue_lock);
  880. list_splice(&issued_dmas[i], &card->ctrl[i].queue);
  881. atomic_sub(cnt, &card->ctrl[i].stats.hw_q_depth);
  882. card->ctrl[i].stats.sw_q_depth += cnt;
  883. card->ctrl[i].e_cnt = 0;
  884. spin_unlock_bh(&card->ctrl[i].queue_lock);
  885. }
  886. kfree(issued_dmas);
  887. return 0;
  888. }
  889. int rsxx_dma_init(void)
  890. {
  891. rsxx_dma_pool = KMEM_CACHE(rsxx_dma, SLAB_HWCACHE_ALIGN);
  892. if (!rsxx_dma_pool)
  893. return -ENOMEM;
  894. return 0;
  895. }
  896. void rsxx_dma_cleanup(void)
  897. {
  898. kmem_cache_destroy(rsxx_dma_pool);
  899. }