nhi.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. /*
  2. * Thunderbolt Cactus Ridge driver - NHI driver
  3. *
  4. * The NHI (native host interface) is the pci device that allows us to send and
  5. * receive frames from the thunderbolt bus.
  6. *
  7. * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
  8. */
  9. #include <linux/pm_runtime.h>
  10. #include <linux/slab.h>
  11. #include <linux/errno.h>
  12. #include <linux/pci.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/module.h>
  15. #include <linux/delay.h>
  16. #include "nhi.h"
  17. #include "nhi_regs.h"
  18. #include "tb.h"
  19. #define RING_TYPE(ring) ((ring)->is_tx ? "TX ring" : "RX ring")
  20. /*
  21. * Used to enable end-to-end workaround for missing RX packets. Do not
  22. * use this ring for anything else.
  23. */
  24. #define RING_E2E_UNUSED_HOPID 2
  25. /* HopIDs 0-7 are reserved by the Thunderbolt protocol */
  26. #define RING_FIRST_USABLE_HOPID 8
  27. /*
  28. * Minimal number of vectors when we use MSI-X. Two for control channel
  29. * Rx/Tx and the rest four are for cross domain DMA paths.
  30. */
  31. #define MSIX_MIN_VECS 6
  32. #define MSIX_MAX_VECS 16
  33. #define NHI_MAILBOX_TIMEOUT 500 /* ms */
  34. static int ring_interrupt_index(struct tb_ring *ring)
  35. {
  36. int bit = ring->hop;
  37. if (!ring->is_tx)
  38. bit += ring->nhi->hop_count;
  39. return bit;
  40. }
  41. /**
  42. * ring_interrupt_active() - activate/deactivate interrupts for a single ring
  43. *
  44. * ring->nhi->lock must be held.
  45. */
  46. static void ring_interrupt_active(struct tb_ring *ring, bool active)
  47. {
  48. int reg = REG_RING_INTERRUPT_BASE +
  49. ring_interrupt_index(ring) / 32 * 4;
  50. int bit = ring_interrupt_index(ring) & 31;
  51. int mask = 1 << bit;
  52. u32 old, new;
  53. if (ring->irq > 0) {
  54. u32 step, shift, ivr, misc;
  55. void __iomem *ivr_base;
  56. int index;
  57. if (ring->is_tx)
  58. index = ring->hop;
  59. else
  60. index = ring->hop + ring->nhi->hop_count;
  61. /*
  62. * Ask the hardware to clear interrupt status bits automatically
  63. * since we already know which interrupt was triggered.
  64. */
  65. misc = ioread32(ring->nhi->iobase + REG_DMA_MISC);
  66. if (!(misc & REG_DMA_MISC_INT_AUTO_CLEAR)) {
  67. misc |= REG_DMA_MISC_INT_AUTO_CLEAR;
  68. iowrite32(misc, ring->nhi->iobase + REG_DMA_MISC);
  69. }
  70. ivr_base = ring->nhi->iobase + REG_INT_VEC_ALLOC_BASE;
  71. step = index / REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS;
  72. shift = index % REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS;
  73. ivr = ioread32(ivr_base + step);
  74. ivr &= ~(REG_INT_VEC_ALLOC_MASK << shift);
  75. if (active)
  76. ivr |= ring->vector << shift;
  77. iowrite32(ivr, ivr_base + step);
  78. }
  79. old = ioread32(ring->nhi->iobase + reg);
  80. if (active)
  81. new = old | mask;
  82. else
  83. new = old & ~mask;
  84. dev_info(&ring->nhi->pdev->dev,
  85. "%s interrupt at register %#x bit %d (%#x -> %#x)\n",
  86. active ? "enabling" : "disabling", reg, bit, old, new);
  87. if (new == old)
  88. dev_WARN(&ring->nhi->pdev->dev,
  89. "interrupt for %s %d is already %s\n",
  90. RING_TYPE(ring), ring->hop,
  91. active ? "enabled" : "disabled");
  92. iowrite32(new, ring->nhi->iobase + reg);
  93. }
  94. /**
  95. * nhi_disable_interrupts() - disable interrupts for all rings
  96. *
  97. * Use only during init and shutdown.
  98. */
  99. static void nhi_disable_interrupts(struct tb_nhi *nhi)
  100. {
  101. int i = 0;
  102. /* disable interrupts */
  103. for (i = 0; i < RING_INTERRUPT_REG_COUNT(nhi); i++)
  104. iowrite32(0, nhi->iobase + REG_RING_INTERRUPT_BASE + 4 * i);
  105. /* clear interrupt status bits */
  106. for (i = 0; i < RING_NOTIFY_REG_COUNT(nhi); i++)
  107. ioread32(nhi->iobase + REG_RING_NOTIFY_BASE + 4 * i);
  108. }
  109. /* ring helper methods */
  110. static void __iomem *ring_desc_base(struct tb_ring *ring)
  111. {
  112. void __iomem *io = ring->nhi->iobase;
  113. io += ring->is_tx ? REG_TX_RING_BASE : REG_RX_RING_BASE;
  114. io += ring->hop * 16;
  115. return io;
  116. }
  117. static void __iomem *ring_options_base(struct tb_ring *ring)
  118. {
  119. void __iomem *io = ring->nhi->iobase;
  120. io += ring->is_tx ? REG_TX_OPTIONS_BASE : REG_RX_OPTIONS_BASE;
  121. io += ring->hop * 32;
  122. return io;
  123. }
  124. static void ring_iowrite16desc(struct tb_ring *ring, u32 value, u32 offset)
  125. {
  126. iowrite16(value, ring_desc_base(ring) + offset);
  127. }
  128. static void ring_iowrite32desc(struct tb_ring *ring, u32 value, u32 offset)
  129. {
  130. iowrite32(value, ring_desc_base(ring) + offset);
  131. }
  132. static void ring_iowrite64desc(struct tb_ring *ring, u64 value, u32 offset)
  133. {
  134. iowrite32(value, ring_desc_base(ring) + offset);
  135. iowrite32(value >> 32, ring_desc_base(ring) + offset + 4);
  136. }
  137. static void ring_iowrite32options(struct tb_ring *ring, u32 value, u32 offset)
  138. {
  139. iowrite32(value, ring_options_base(ring) + offset);
  140. }
  141. static bool ring_full(struct tb_ring *ring)
  142. {
  143. return ((ring->head + 1) % ring->size) == ring->tail;
  144. }
  145. static bool ring_empty(struct tb_ring *ring)
  146. {
  147. return ring->head == ring->tail;
  148. }
  149. /**
  150. * ring_write_descriptors() - post frames from ring->queue to the controller
  151. *
  152. * ring->lock is held.
  153. */
  154. static void ring_write_descriptors(struct tb_ring *ring)
  155. {
  156. struct ring_frame *frame, *n;
  157. struct ring_desc *descriptor;
  158. list_for_each_entry_safe(frame, n, &ring->queue, list) {
  159. if (ring_full(ring))
  160. break;
  161. list_move_tail(&frame->list, &ring->in_flight);
  162. descriptor = &ring->descriptors[ring->head];
  163. descriptor->phys = frame->buffer_phy;
  164. descriptor->time = 0;
  165. descriptor->flags = RING_DESC_POSTED | RING_DESC_INTERRUPT;
  166. if (ring->is_tx) {
  167. descriptor->length = frame->size;
  168. descriptor->eof = frame->eof;
  169. descriptor->sof = frame->sof;
  170. }
  171. ring->head = (ring->head + 1) % ring->size;
  172. ring_iowrite16desc(ring, ring->head, ring->is_tx ? 10 : 8);
  173. }
  174. }
  175. /**
  176. * ring_work() - progress completed frames
  177. *
  178. * If the ring is shutting down then all frames are marked as canceled and
  179. * their callbacks are invoked.
  180. *
  181. * Otherwise we collect all completed frame from the ring buffer, write new
  182. * frame to the ring buffer and invoke the callbacks for the completed frames.
  183. */
  184. static void ring_work(struct work_struct *work)
  185. {
  186. struct tb_ring *ring = container_of(work, typeof(*ring), work);
  187. struct ring_frame *frame;
  188. bool canceled = false;
  189. unsigned long flags;
  190. LIST_HEAD(done);
  191. spin_lock_irqsave(&ring->lock, flags);
  192. if (!ring->running) {
  193. /* Move all frames to done and mark them as canceled. */
  194. list_splice_tail_init(&ring->in_flight, &done);
  195. list_splice_tail_init(&ring->queue, &done);
  196. canceled = true;
  197. goto invoke_callback;
  198. }
  199. while (!ring_empty(ring)) {
  200. if (!(ring->descriptors[ring->tail].flags
  201. & RING_DESC_COMPLETED))
  202. break;
  203. frame = list_first_entry(&ring->in_flight, typeof(*frame),
  204. list);
  205. list_move_tail(&frame->list, &done);
  206. if (!ring->is_tx) {
  207. frame->size = ring->descriptors[ring->tail].length;
  208. frame->eof = ring->descriptors[ring->tail].eof;
  209. frame->sof = ring->descriptors[ring->tail].sof;
  210. frame->flags = ring->descriptors[ring->tail].flags;
  211. }
  212. ring->tail = (ring->tail + 1) % ring->size;
  213. }
  214. ring_write_descriptors(ring);
  215. invoke_callback:
  216. /* allow callbacks to schedule new work */
  217. spin_unlock_irqrestore(&ring->lock, flags);
  218. while (!list_empty(&done)) {
  219. frame = list_first_entry(&done, typeof(*frame), list);
  220. /*
  221. * The callback may reenqueue or delete frame.
  222. * Do not hold on to it.
  223. */
  224. list_del_init(&frame->list);
  225. if (frame->callback)
  226. frame->callback(ring, frame, canceled);
  227. }
  228. }
  229. int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame)
  230. {
  231. unsigned long flags;
  232. int ret = 0;
  233. spin_lock_irqsave(&ring->lock, flags);
  234. if (ring->running) {
  235. list_add_tail(&frame->list, &ring->queue);
  236. ring_write_descriptors(ring);
  237. } else {
  238. ret = -ESHUTDOWN;
  239. }
  240. spin_unlock_irqrestore(&ring->lock, flags);
  241. return ret;
  242. }
  243. EXPORT_SYMBOL_GPL(__tb_ring_enqueue);
  244. /**
  245. * tb_ring_poll() - Poll one completed frame from the ring
  246. * @ring: Ring to poll
  247. *
  248. * This function can be called when @start_poll callback of the @ring
  249. * has been called. It will read one completed frame from the ring and
  250. * return it to the caller. Returns %NULL if there is no more completed
  251. * frames.
  252. */
  253. struct ring_frame *tb_ring_poll(struct tb_ring *ring)
  254. {
  255. struct ring_frame *frame = NULL;
  256. unsigned long flags;
  257. spin_lock_irqsave(&ring->lock, flags);
  258. if (!ring->running)
  259. goto unlock;
  260. if (ring_empty(ring))
  261. goto unlock;
  262. if (ring->descriptors[ring->tail].flags & RING_DESC_COMPLETED) {
  263. frame = list_first_entry(&ring->in_flight, typeof(*frame),
  264. list);
  265. list_del_init(&frame->list);
  266. if (!ring->is_tx) {
  267. frame->size = ring->descriptors[ring->tail].length;
  268. frame->eof = ring->descriptors[ring->tail].eof;
  269. frame->sof = ring->descriptors[ring->tail].sof;
  270. frame->flags = ring->descriptors[ring->tail].flags;
  271. }
  272. ring->tail = (ring->tail + 1) % ring->size;
  273. }
  274. unlock:
  275. spin_unlock_irqrestore(&ring->lock, flags);
  276. return frame;
  277. }
  278. EXPORT_SYMBOL_GPL(tb_ring_poll);
  279. static void __ring_interrupt_mask(struct tb_ring *ring, bool mask)
  280. {
  281. int idx = ring_interrupt_index(ring);
  282. int reg = REG_RING_INTERRUPT_BASE + idx / 32 * 4;
  283. int bit = idx % 32;
  284. u32 val;
  285. val = ioread32(ring->nhi->iobase + reg);
  286. if (mask)
  287. val &= ~BIT(bit);
  288. else
  289. val |= BIT(bit);
  290. iowrite32(val, ring->nhi->iobase + reg);
  291. }
  292. /* Both @nhi->lock and @ring->lock should be held */
  293. static void __ring_interrupt(struct tb_ring *ring)
  294. {
  295. if (!ring->running)
  296. return;
  297. if (ring->start_poll) {
  298. __ring_interrupt_mask(ring, true);
  299. ring->start_poll(ring->poll_data);
  300. } else {
  301. schedule_work(&ring->work);
  302. }
  303. }
  304. /**
  305. * tb_ring_poll_complete() - Re-start interrupt for the ring
  306. * @ring: Ring to re-start the interrupt
  307. *
  308. * This will re-start (unmask) the ring interrupt once the user is done
  309. * with polling.
  310. */
  311. void tb_ring_poll_complete(struct tb_ring *ring)
  312. {
  313. unsigned long flags;
  314. spin_lock_irqsave(&ring->nhi->lock, flags);
  315. spin_lock(&ring->lock);
  316. if (ring->start_poll)
  317. __ring_interrupt_mask(ring, false);
  318. spin_unlock(&ring->lock);
  319. spin_unlock_irqrestore(&ring->nhi->lock, flags);
  320. }
  321. EXPORT_SYMBOL_GPL(tb_ring_poll_complete);
  322. static irqreturn_t ring_msix(int irq, void *data)
  323. {
  324. struct tb_ring *ring = data;
  325. spin_lock(&ring->nhi->lock);
  326. spin_lock(&ring->lock);
  327. __ring_interrupt(ring);
  328. spin_unlock(&ring->lock);
  329. spin_unlock(&ring->nhi->lock);
  330. return IRQ_HANDLED;
  331. }
  332. static int ring_request_msix(struct tb_ring *ring, bool no_suspend)
  333. {
  334. struct tb_nhi *nhi = ring->nhi;
  335. unsigned long irqflags;
  336. int ret;
  337. if (!nhi->pdev->msix_enabled)
  338. return 0;
  339. ret = ida_simple_get(&nhi->msix_ida, 0, MSIX_MAX_VECS, GFP_KERNEL);
  340. if (ret < 0)
  341. return ret;
  342. ring->vector = ret;
  343. ring->irq = pci_irq_vector(ring->nhi->pdev, ring->vector);
  344. if (ring->irq < 0)
  345. return ring->irq;
  346. irqflags = no_suspend ? IRQF_NO_SUSPEND : 0;
  347. return request_irq(ring->irq, ring_msix, irqflags, "thunderbolt", ring);
  348. }
  349. static void ring_release_msix(struct tb_ring *ring)
  350. {
  351. if (ring->irq <= 0)
  352. return;
  353. free_irq(ring->irq, ring);
  354. ida_simple_remove(&ring->nhi->msix_ida, ring->vector);
  355. ring->vector = 0;
  356. ring->irq = 0;
  357. }
  358. static int nhi_alloc_hop(struct tb_nhi *nhi, struct tb_ring *ring)
  359. {
  360. int ret = 0;
  361. spin_lock_irq(&nhi->lock);
  362. if (ring->hop < 0) {
  363. unsigned int i;
  364. /*
  365. * Automatically allocate HopID from the non-reserved
  366. * range 8 .. hop_count - 1.
  367. */
  368. for (i = RING_FIRST_USABLE_HOPID; i < nhi->hop_count; i++) {
  369. if (ring->is_tx) {
  370. if (!nhi->tx_rings[i]) {
  371. ring->hop = i;
  372. break;
  373. }
  374. } else {
  375. if (!nhi->rx_rings[i]) {
  376. ring->hop = i;
  377. break;
  378. }
  379. }
  380. }
  381. }
  382. if (ring->hop < 0 || ring->hop >= nhi->hop_count) {
  383. dev_warn(&nhi->pdev->dev, "invalid hop: %d\n", ring->hop);
  384. ret = -EINVAL;
  385. goto err_unlock;
  386. }
  387. if (ring->is_tx && nhi->tx_rings[ring->hop]) {
  388. dev_warn(&nhi->pdev->dev, "TX hop %d already allocated\n",
  389. ring->hop);
  390. ret = -EBUSY;
  391. goto err_unlock;
  392. } else if (!ring->is_tx && nhi->rx_rings[ring->hop]) {
  393. dev_warn(&nhi->pdev->dev, "RX hop %d already allocated\n",
  394. ring->hop);
  395. ret = -EBUSY;
  396. goto err_unlock;
  397. }
  398. if (ring->is_tx)
  399. nhi->tx_rings[ring->hop] = ring;
  400. else
  401. nhi->rx_rings[ring->hop] = ring;
  402. err_unlock:
  403. spin_unlock_irq(&nhi->lock);
  404. return ret;
  405. }
  406. static struct tb_ring *tb_ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
  407. bool transmit, unsigned int flags,
  408. u16 sof_mask, u16 eof_mask,
  409. void (*start_poll)(void *),
  410. void *poll_data)
  411. {
  412. struct tb_ring *ring = NULL;
  413. dev_info(&nhi->pdev->dev, "allocating %s ring %d of size %d\n",
  414. transmit ? "TX" : "RX", hop, size);
  415. /* Tx Ring 2 is reserved for E2E workaround */
  416. if (transmit && hop == RING_E2E_UNUSED_HOPID)
  417. return NULL;
  418. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  419. if (!ring)
  420. return NULL;
  421. spin_lock_init(&ring->lock);
  422. INIT_LIST_HEAD(&ring->queue);
  423. INIT_LIST_HEAD(&ring->in_flight);
  424. INIT_WORK(&ring->work, ring_work);
  425. ring->nhi = nhi;
  426. ring->hop = hop;
  427. ring->is_tx = transmit;
  428. ring->size = size;
  429. ring->flags = flags;
  430. ring->sof_mask = sof_mask;
  431. ring->eof_mask = eof_mask;
  432. ring->head = 0;
  433. ring->tail = 0;
  434. ring->running = false;
  435. ring->start_poll = start_poll;
  436. ring->poll_data = poll_data;
  437. ring->descriptors = dma_alloc_coherent(&ring->nhi->pdev->dev,
  438. size * sizeof(*ring->descriptors),
  439. &ring->descriptors_dma, GFP_KERNEL | __GFP_ZERO);
  440. if (!ring->descriptors)
  441. goto err_free_ring;
  442. if (ring_request_msix(ring, flags & RING_FLAG_NO_SUSPEND))
  443. goto err_free_descs;
  444. if (nhi_alloc_hop(nhi, ring))
  445. goto err_release_msix;
  446. return ring;
  447. err_release_msix:
  448. ring_release_msix(ring);
  449. err_free_descs:
  450. dma_free_coherent(&ring->nhi->pdev->dev,
  451. ring->size * sizeof(*ring->descriptors),
  452. ring->descriptors, ring->descriptors_dma);
  453. err_free_ring:
  454. kfree(ring);
  455. return NULL;
  456. }
  457. /**
  458. * tb_ring_alloc_tx() - Allocate DMA ring for transmit
  459. * @nhi: Pointer to the NHI the ring is to be allocated
  460. * @hop: HopID (ring) to allocate
  461. * @size: Number of entries in the ring
  462. * @flags: Flags for the ring
  463. */
  464. struct tb_ring *tb_ring_alloc_tx(struct tb_nhi *nhi, int hop, int size,
  465. unsigned int flags)
  466. {
  467. return tb_ring_alloc(nhi, hop, size, true, flags, 0, 0, NULL, NULL);
  468. }
  469. EXPORT_SYMBOL_GPL(tb_ring_alloc_tx);
  470. /**
  471. * tb_ring_alloc_rx() - Allocate DMA ring for receive
  472. * @nhi: Pointer to the NHI the ring is to be allocated
  473. * @hop: HopID (ring) to allocate. Pass %-1 for automatic allocation.
  474. * @size: Number of entries in the ring
  475. * @flags: Flags for the ring
  476. * @sof_mask: Mask of PDF values that start a frame
  477. * @eof_mask: Mask of PDF values that end a frame
  478. * @start_poll: If not %NULL the ring will call this function when an
  479. * interrupt is triggered and masked, instead of callback
  480. * in each Rx frame.
  481. * @poll_data: Optional data passed to @start_poll
  482. */
  483. struct tb_ring *tb_ring_alloc_rx(struct tb_nhi *nhi, int hop, int size,
  484. unsigned int flags, u16 sof_mask, u16 eof_mask,
  485. void (*start_poll)(void *), void *poll_data)
  486. {
  487. return tb_ring_alloc(nhi, hop, size, false, flags, sof_mask, eof_mask,
  488. start_poll, poll_data);
  489. }
  490. EXPORT_SYMBOL_GPL(tb_ring_alloc_rx);
  491. /**
  492. * tb_ring_start() - enable a ring
  493. *
  494. * Must not be invoked in parallel with tb_ring_stop().
  495. */
  496. void tb_ring_start(struct tb_ring *ring)
  497. {
  498. u16 frame_size;
  499. u32 flags;
  500. spin_lock_irq(&ring->nhi->lock);
  501. spin_lock(&ring->lock);
  502. if (ring->nhi->going_away)
  503. goto err;
  504. if (ring->running) {
  505. dev_WARN(&ring->nhi->pdev->dev, "ring already started\n");
  506. goto err;
  507. }
  508. dev_info(&ring->nhi->pdev->dev, "starting %s %d\n",
  509. RING_TYPE(ring), ring->hop);
  510. if (ring->flags & RING_FLAG_FRAME) {
  511. /* Means 4096 */
  512. frame_size = 0;
  513. flags = RING_FLAG_ENABLE;
  514. } else {
  515. frame_size = TB_FRAME_SIZE;
  516. flags = RING_FLAG_ENABLE | RING_FLAG_RAW;
  517. }
  518. if (ring->flags & RING_FLAG_E2E && !ring->is_tx) {
  519. u32 hop;
  520. /*
  521. * In order not to lose Rx packets we enable end-to-end
  522. * workaround which transfers Rx credits to an unused Tx
  523. * HopID.
  524. */
  525. hop = RING_E2E_UNUSED_HOPID << REG_RX_OPTIONS_E2E_HOP_SHIFT;
  526. hop &= REG_RX_OPTIONS_E2E_HOP_MASK;
  527. flags |= hop | RING_FLAG_E2E_FLOW_CONTROL;
  528. }
  529. ring_iowrite64desc(ring, ring->descriptors_dma, 0);
  530. if (ring->is_tx) {
  531. ring_iowrite32desc(ring, ring->size, 12);
  532. ring_iowrite32options(ring, 0, 4); /* time releated ? */
  533. ring_iowrite32options(ring, flags, 0);
  534. } else {
  535. u32 sof_eof_mask = ring->sof_mask << 16 | ring->eof_mask;
  536. ring_iowrite32desc(ring, (frame_size << 16) | ring->size, 12);
  537. ring_iowrite32options(ring, sof_eof_mask, 4);
  538. ring_iowrite32options(ring, flags, 0);
  539. }
  540. ring_interrupt_active(ring, true);
  541. ring->running = true;
  542. err:
  543. spin_unlock(&ring->lock);
  544. spin_unlock_irq(&ring->nhi->lock);
  545. }
  546. EXPORT_SYMBOL_GPL(tb_ring_start);
  547. /**
  548. * tb_ring_stop() - shutdown a ring
  549. *
  550. * Must not be invoked from a callback.
  551. *
  552. * This method will disable the ring. Further calls to
  553. * tb_ring_tx/tb_ring_rx will return -ESHUTDOWN until ring_stop has been
  554. * called.
  555. *
  556. * All enqueued frames will be canceled and their callbacks will be executed
  557. * with frame->canceled set to true (on the callback thread). This method
  558. * returns only after all callback invocations have finished.
  559. */
  560. void tb_ring_stop(struct tb_ring *ring)
  561. {
  562. spin_lock_irq(&ring->nhi->lock);
  563. spin_lock(&ring->lock);
  564. dev_info(&ring->nhi->pdev->dev, "stopping %s %d\n",
  565. RING_TYPE(ring), ring->hop);
  566. if (ring->nhi->going_away)
  567. goto err;
  568. if (!ring->running) {
  569. dev_WARN(&ring->nhi->pdev->dev, "%s %d already stopped\n",
  570. RING_TYPE(ring), ring->hop);
  571. goto err;
  572. }
  573. ring_interrupt_active(ring, false);
  574. ring_iowrite32options(ring, 0, 0);
  575. ring_iowrite64desc(ring, 0, 0);
  576. ring_iowrite16desc(ring, 0, ring->is_tx ? 10 : 8);
  577. ring_iowrite32desc(ring, 0, 12);
  578. ring->head = 0;
  579. ring->tail = 0;
  580. ring->running = false;
  581. err:
  582. spin_unlock(&ring->lock);
  583. spin_unlock_irq(&ring->nhi->lock);
  584. /*
  585. * schedule ring->work to invoke callbacks on all remaining frames.
  586. */
  587. schedule_work(&ring->work);
  588. flush_work(&ring->work);
  589. }
  590. EXPORT_SYMBOL_GPL(tb_ring_stop);
  591. /*
  592. * tb_ring_free() - free ring
  593. *
  594. * When this method returns all invocations of ring->callback will have
  595. * finished.
  596. *
  597. * Ring must be stopped.
  598. *
  599. * Must NOT be called from ring_frame->callback!
  600. */
  601. void tb_ring_free(struct tb_ring *ring)
  602. {
  603. spin_lock_irq(&ring->nhi->lock);
  604. /*
  605. * Dissociate the ring from the NHI. This also ensures that
  606. * nhi_interrupt_work cannot reschedule ring->work.
  607. */
  608. if (ring->is_tx)
  609. ring->nhi->tx_rings[ring->hop] = NULL;
  610. else
  611. ring->nhi->rx_rings[ring->hop] = NULL;
  612. if (ring->running) {
  613. dev_WARN(&ring->nhi->pdev->dev, "%s %d still running\n",
  614. RING_TYPE(ring), ring->hop);
  615. }
  616. spin_unlock_irq(&ring->nhi->lock);
  617. ring_release_msix(ring);
  618. dma_free_coherent(&ring->nhi->pdev->dev,
  619. ring->size * sizeof(*ring->descriptors),
  620. ring->descriptors, ring->descriptors_dma);
  621. ring->descriptors = NULL;
  622. ring->descriptors_dma = 0;
  623. dev_info(&ring->nhi->pdev->dev,
  624. "freeing %s %d\n",
  625. RING_TYPE(ring),
  626. ring->hop);
  627. /**
  628. * ring->work can no longer be scheduled (it is scheduled only
  629. * by nhi_interrupt_work, ring_stop and ring_msix). Wait for it
  630. * to finish before freeing the ring.
  631. */
  632. flush_work(&ring->work);
  633. kfree(ring);
  634. }
  635. EXPORT_SYMBOL_GPL(tb_ring_free);
  636. /**
  637. * nhi_mailbox_cmd() - Send a command through NHI mailbox
  638. * @nhi: Pointer to the NHI structure
  639. * @cmd: Command to send
  640. * @data: Data to be send with the command
  641. *
  642. * Sends mailbox command to the firmware running on NHI. Returns %0 in
  643. * case of success and negative errno in case of failure.
  644. */
  645. int nhi_mailbox_cmd(struct tb_nhi *nhi, enum nhi_mailbox_cmd cmd, u32 data)
  646. {
  647. ktime_t timeout;
  648. u32 val;
  649. iowrite32(data, nhi->iobase + REG_INMAIL_DATA);
  650. val = ioread32(nhi->iobase + REG_INMAIL_CMD);
  651. val &= ~(REG_INMAIL_CMD_MASK | REG_INMAIL_ERROR);
  652. val |= REG_INMAIL_OP_REQUEST | cmd;
  653. iowrite32(val, nhi->iobase + REG_INMAIL_CMD);
  654. timeout = ktime_add_ms(ktime_get(), NHI_MAILBOX_TIMEOUT);
  655. do {
  656. val = ioread32(nhi->iobase + REG_INMAIL_CMD);
  657. if (!(val & REG_INMAIL_OP_REQUEST))
  658. break;
  659. usleep_range(10, 20);
  660. } while (ktime_before(ktime_get(), timeout));
  661. if (val & REG_INMAIL_OP_REQUEST)
  662. return -ETIMEDOUT;
  663. if (val & REG_INMAIL_ERROR)
  664. return -EIO;
  665. return 0;
  666. }
  667. /**
  668. * nhi_mailbox_mode() - Return current firmware operation mode
  669. * @nhi: Pointer to the NHI structure
  670. *
  671. * The function reads current firmware operation mode using NHI mailbox
  672. * registers and returns it to the caller.
  673. */
  674. enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi)
  675. {
  676. u32 val;
  677. val = ioread32(nhi->iobase + REG_OUTMAIL_CMD);
  678. val &= REG_OUTMAIL_CMD_OPMODE_MASK;
  679. val >>= REG_OUTMAIL_CMD_OPMODE_SHIFT;
  680. return (enum nhi_fw_mode)val;
  681. }
  682. static void nhi_interrupt_work(struct work_struct *work)
  683. {
  684. struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work);
  685. int value = 0; /* Suppress uninitialized usage warning. */
  686. int bit;
  687. int hop = -1;
  688. int type = 0; /* current interrupt type 0: TX, 1: RX, 2: RX overflow */
  689. struct tb_ring *ring;
  690. spin_lock_irq(&nhi->lock);
  691. /*
  692. * Starting at REG_RING_NOTIFY_BASE there are three status bitfields
  693. * (TX, RX, RX overflow). We iterate over the bits and read a new
  694. * dwords as required. The registers are cleared on read.
  695. */
  696. for (bit = 0; bit < 3 * nhi->hop_count; bit++) {
  697. if (bit % 32 == 0)
  698. value = ioread32(nhi->iobase
  699. + REG_RING_NOTIFY_BASE
  700. + 4 * (bit / 32));
  701. if (++hop == nhi->hop_count) {
  702. hop = 0;
  703. type++;
  704. }
  705. if ((value & (1 << (bit % 32))) == 0)
  706. continue;
  707. if (type == 2) {
  708. dev_warn(&nhi->pdev->dev,
  709. "RX overflow for ring %d\n",
  710. hop);
  711. continue;
  712. }
  713. if (type == 0)
  714. ring = nhi->tx_rings[hop];
  715. else
  716. ring = nhi->rx_rings[hop];
  717. if (ring == NULL) {
  718. dev_warn(&nhi->pdev->dev,
  719. "got interrupt for inactive %s ring %d\n",
  720. type ? "RX" : "TX",
  721. hop);
  722. continue;
  723. }
  724. spin_lock(&ring->lock);
  725. __ring_interrupt(ring);
  726. spin_unlock(&ring->lock);
  727. }
  728. spin_unlock_irq(&nhi->lock);
  729. }
  730. static irqreturn_t nhi_msi(int irq, void *data)
  731. {
  732. struct tb_nhi *nhi = data;
  733. schedule_work(&nhi->interrupt_work);
  734. return IRQ_HANDLED;
  735. }
  736. static int nhi_suspend_noirq(struct device *dev)
  737. {
  738. struct pci_dev *pdev = to_pci_dev(dev);
  739. struct tb *tb = pci_get_drvdata(pdev);
  740. return tb_domain_suspend_noirq(tb);
  741. }
  742. static void nhi_enable_int_throttling(struct tb_nhi *nhi)
  743. {
  744. /* Throttling is specified in 256ns increments */
  745. u32 throttle = DIV_ROUND_UP(128 * NSEC_PER_USEC, 256);
  746. unsigned int i;
  747. /*
  748. * Configure interrupt throttling for all vectors even if we
  749. * only use few.
  750. */
  751. for (i = 0; i < MSIX_MAX_VECS; i++) {
  752. u32 reg = REG_INT_THROTTLING_RATE + i * 4;
  753. iowrite32(throttle, nhi->iobase + reg);
  754. }
  755. }
  756. static int nhi_resume_noirq(struct device *dev)
  757. {
  758. struct pci_dev *pdev = to_pci_dev(dev);
  759. struct tb *tb = pci_get_drvdata(pdev);
  760. /*
  761. * Check that the device is still there. It may be that the user
  762. * unplugged last device which causes the host controller to go
  763. * away on PCs.
  764. */
  765. if (!pci_device_is_present(pdev))
  766. tb->nhi->going_away = true;
  767. else
  768. nhi_enable_int_throttling(tb->nhi);
  769. return tb_domain_resume_noirq(tb);
  770. }
  771. static int nhi_suspend(struct device *dev)
  772. {
  773. struct pci_dev *pdev = to_pci_dev(dev);
  774. struct tb *tb = pci_get_drvdata(pdev);
  775. return tb_domain_suspend(tb);
  776. }
  777. static void nhi_complete(struct device *dev)
  778. {
  779. struct pci_dev *pdev = to_pci_dev(dev);
  780. struct tb *tb = pci_get_drvdata(pdev);
  781. /*
  782. * If we were runtime suspended when system suspend started,
  783. * schedule runtime resume now. It should bring the domain back
  784. * to functional state.
  785. */
  786. if (pm_runtime_suspended(&pdev->dev))
  787. pm_runtime_resume(&pdev->dev);
  788. else
  789. tb_domain_complete(tb);
  790. }
  791. static int nhi_runtime_suspend(struct device *dev)
  792. {
  793. struct pci_dev *pdev = to_pci_dev(dev);
  794. struct tb *tb = pci_get_drvdata(pdev);
  795. return tb_domain_runtime_suspend(tb);
  796. }
  797. static int nhi_runtime_resume(struct device *dev)
  798. {
  799. struct pci_dev *pdev = to_pci_dev(dev);
  800. struct tb *tb = pci_get_drvdata(pdev);
  801. nhi_enable_int_throttling(tb->nhi);
  802. return tb_domain_runtime_resume(tb);
  803. }
  804. static void nhi_shutdown(struct tb_nhi *nhi)
  805. {
  806. int i;
  807. dev_info(&nhi->pdev->dev, "shutdown\n");
  808. for (i = 0; i < nhi->hop_count; i++) {
  809. if (nhi->tx_rings[i])
  810. dev_WARN(&nhi->pdev->dev,
  811. "TX ring %d is still active\n", i);
  812. if (nhi->rx_rings[i])
  813. dev_WARN(&nhi->pdev->dev,
  814. "RX ring %d is still active\n", i);
  815. }
  816. nhi_disable_interrupts(nhi);
  817. /*
  818. * We have to release the irq before calling flush_work. Otherwise an
  819. * already executing IRQ handler could call schedule_work again.
  820. */
  821. if (!nhi->pdev->msix_enabled) {
  822. devm_free_irq(&nhi->pdev->dev, nhi->pdev->irq, nhi);
  823. flush_work(&nhi->interrupt_work);
  824. }
  825. ida_destroy(&nhi->msix_ida);
  826. }
  827. static int nhi_init_msi(struct tb_nhi *nhi)
  828. {
  829. struct pci_dev *pdev = nhi->pdev;
  830. int res, irq, nvec;
  831. /* In case someone left them on. */
  832. nhi_disable_interrupts(nhi);
  833. nhi_enable_int_throttling(nhi);
  834. ida_init(&nhi->msix_ida);
  835. /*
  836. * The NHI has 16 MSI-X vectors or a single MSI. We first try to
  837. * get all MSI-X vectors and if we succeed, each ring will have
  838. * one MSI-X. If for some reason that does not work out, we
  839. * fallback to a single MSI.
  840. */
  841. nvec = pci_alloc_irq_vectors(pdev, MSIX_MIN_VECS, MSIX_MAX_VECS,
  842. PCI_IRQ_MSIX);
  843. if (nvec < 0) {
  844. nvec = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
  845. if (nvec < 0)
  846. return nvec;
  847. INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
  848. irq = pci_irq_vector(nhi->pdev, 0);
  849. if (irq < 0)
  850. return irq;
  851. res = devm_request_irq(&pdev->dev, irq, nhi_msi,
  852. IRQF_NO_SUSPEND, "thunderbolt", nhi);
  853. if (res) {
  854. dev_err(&pdev->dev, "request_irq failed, aborting\n");
  855. return res;
  856. }
  857. }
  858. return 0;
  859. }
  860. static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  861. {
  862. struct tb_nhi *nhi;
  863. struct tb *tb;
  864. int res;
  865. res = pcim_enable_device(pdev);
  866. if (res) {
  867. dev_err(&pdev->dev, "cannot enable PCI device, aborting\n");
  868. return res;
  869. }
  870. res = pcim_iomap_regions(pdev, 1 << 0, "thunderbolt");
  871. if (res) {
  872. dev_err(&pdev->dev, "cannot obtain PCI resources, aborting\n");
  873. return res;
  874. }
  875. nhi = devm_kzalloc(&pdev->dev, sizeof(*nhi), GFP_KERNEL);
  876. if (!nhi)
  877. return -ENOMEM;
  878. nhi->pdev = pdev;
  879. /* cannot fail - table is allocated bin pcim_iomap_regions */
  880. nhi->iobase = pcim_iomap_table(pdev)[0];
  881. nhi->hop_count = ioread32(nhi->iobase + REG_HOP_COUNT) & 0x3ff;
  882. if (nhi->hop_count != 12 && nhi->hop_count != 32)
  883. dev_warn(&pdev->dev, "unexpected hop count: %d\n",
  884. nhi->hop_count);
  885. nhi->tx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
  886. sizeof(*nhi->tx_rings), GFP_KERNEL);
  887. nhi->rx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
  888. sizeof(*nhi->rx_rings), GFP_KERNEL);
  889. if (!nhi->tx_rings || !nhi->rx_rings)
  890. return -ENOMEM;
  891. res = nhi_init_msi(nhi);
  892. if (res) {
  893. dev_err(&pdev->dev, "cannot enable MSI, aborting\n");
  894. return res;
  895. }
  896. spin_lock_init(&nhi->lock);
  897. res = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  898. if (res)
  899. res = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  900. if (res) {
  901. dev_err(&pdev->dev, "failed to set DMA mask\n");
  902. return res;
  903. }
  904. pci_set_master(pdev);
  905. tb = icm_probe(nhi);
  906. if (!tb)
  907. tb = tb_probe(nhi);
  908. if (!tb) {
  909. dev_err(&nhi->pdev->dev,
  910. "failed to determine connection manager, aborting\n");
  911. return -ENODEV;
  912. }
  913. dev_info(&nhi->pdev->dev, "NHI initialized, starting thunderbolt\n");
  914. res = tb_domain_add(tb);
  915. if (res) {
  916. /*
  917. * At this point the RX/TX rings might already have been
  918. * activated. Do a proper shutdown.
  919. */
  920. tb_domain_put(tb);
  921. nhi_shutdown(nhi);
  922. return res;
  923. }
  924. pci_set_drvdata(pdev, tb);
  925. pm_runtime_allow(&pdev->dev);
  926. pm_runtime_set_autosuspend_delay(&pdev->dev, TB_AUTOSUSPEND_DELAY);
  927. pm_runtime_use_autosuspend(&pdev->dev);
  928. pm_runtime_put_autosuspend(&pdev->dev);
  929. return 0;
  930. }
  931. static void nhi_remove(struct pci_dev *pdev)
  932. {
  933. struct tb *tb = pci_get_drvdata(pdev);
  934. struct tb_nhi *nhi = tb->nhi;
  935. pm_runtime_get_sync(&pdev->dev);
  936. pm_runtime_dont_use_autosuspend(&pdev->dev);
  937. pm_runtime_forbid(&pdev->dev);
  938. tb_domain_remove(tb);
  939. nhi_shutdown(nhi);
  940. }
  941. /*
  942. * The tunneled pci bridges are siblings of us. Use resume_noirq to reenable
  943. * the tunnels asap. A corresponding pci quirk blocks the downstream bridges
  944. * resume_noirq until we are done.
  945. */
  946. static const struct dev_pm_ops nhi_pm_ops = {
  947. .suspend_noirq = nhi_suspend_noirq,
  948. .resume_noirq = nhi_resume_noirq,
  949. .freeze_noirq = nhi_suspend_noirq, /*
  950. * we just disable hotplug, the
  951. * pci-tunnels stay alive.
  952. */
  953. .thaw_noirq = nhi_resume_noirq,
  954. .restore_noirq = nhi_resume_noirq,
  955. .suspend = nhi_suspend,
  956. .freeze = nhi_suspend,
  957. .poweroff = nhi_suspend,
  958. .complete = nhi_complete,
  959. .runtime_suspend = nhi_runtime_suspend,
  960. .runtime_resume = nhi_runtime_resume,
  961. };
  962. static struct pci_device_id nhi_ids[] = {
  963. /*
  964. * We have to specify class, the TB bridges use the same device and
  965. * vendor (sub)id on gen 1 and gen 2 controllers.
  966. */
  967. {
  968. .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
  969. .vendor = PCI_VENDOR_ID_INTEL,
  970. .device = PCI_DEVICE_ID_INTEL_LIGHT_RIDGE,
  971. .subvendor = 0x2222, .subdevice = 0x1111,
  972. },
  973. {
  974. .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
  975. .vendor = PCI_VENDOR_ID_INTEL,
  976. .device = PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C,
  977. .subvendor = 0x2222, .subdevice = 0x1111,
  978. },
  979. {
  980. .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
  981. .vendor = PCI_VENDOR_ID_INTEL,
  982. .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI,
  983. .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
  984. },
  985. {
  986. .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
  987. .vendor = PCI_VENDOR_ID_INTEL,
  988. .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI,
  989. .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
  990. },
  991. /* Thunderbolt 3 */
  992. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI) },
  993. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI) },
  994. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_USBONLY_NHI) },
  995. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI) },
  996. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_USBONLY_NHI) },
  997. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI) },
  998. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI) },
  999. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_USBONLY_NHI) },
  1000. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI) },
  1001. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI) },
  1002. { 0,}
  1003. };
  1004. MODULE_DEVICE_TABLE(pci, nhi_ids);
  1005. MODULE_LICENSE("GPL");
  1006. static struct pci_driver nhi_driver = {
  1007. .name = "thunderbolt",
  1008. .id_table = nhi_ids,
  1009. .probe = nhi_probe,
  1010. .remove = nhi_remove,
  1011. .driver.pm = &nhi_pm_ops,
  1012. };
  1013. static int __init nhi_init(void)
  1014. {
  1015. int ret;
  1016. ret = tb_domain_init();
  1017. if (ret)
  1018. return ret;
  1019. ret = pci_register_driver(&nhi_driver);
  1020. if (ret)
  1021. tb_domain_exit();
  1022. return ret;
  1023. }
  1024. static void __exit nhi_unload(void)
  1025. {
  1026. pci_unregister_driver(&nhi_driver);
  1027. tb_domain_exit();
  1028. }
  1029. fs_initcall(nhi_init);
  1030. module_exit(nhi_unload);