nhi.c 30 KB

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