nhi.c 30 KB

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