nhi.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  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. tb_domain_complete(tb);
  782. }
  783. static void nhi_shutdown(struct tb_nhi *nhi)
  784. {
  785. int i;
  786. dev_info(&nhi->pdev->dev, "shutdown\n");
  787. for (i = 0; i < nhi->hop_count; i++) {
  788. if (nhi->tx_rings[i])
  789. dev_WARN(&nhi->pdev->dev,
  790. "TX ring %d is still active\n", i);
  791. if (nhi->rx_rings[i])
  792. dev_WARN(&nhi->pdev->dev,
  793. "RX ring %d is still active\n", i);
  794. }
  795. nhi_disable_interrupts(nhi);
  796. /*
  797. * We have to release the irq before calling flush_work. Otherwise an
  798. * already executing IRQ handler could call schedule_work again.
  799. */
  800. if (!nhi->pdev->msix_enabled) {
  801. devm_free_irq(&nhi->pdev->dev, nhi->pdev->irq, nhi);
  802. flush_work(&nhi->interrupt_work);
  803. }
  804. ida_destroy(&nhi->msix_ida);
  805. }
  806. static int nhi_init_msi(struct tb_nhi *nhi)
  807. {
  808. struct pci_dev *pdev = nhi->pdev;
  809. int res, irq, nvec;
  810. /* In case someone left them on. */
  811. nhi_disable_interrupts(nhi);
  812. nhi_enable_int_throttling(nhi);
  813. ida_init(&nhi->msix_ida);
  814. /*
  815. * The NHI has 16 MSI-X vectors or a single MSI. We first try to
  816. * get all MSI-X vectors and if we succeed, each ring will have
  817. * one MSI-X. If for some reason that does not work out, we
  818. * fallback to a single MSI.
  819. */
  820. nvec = pci_alloc_irq_vectors(pdev, MSIX_MIN_VECS, MSIX_MAX_VECS,
  821. PCI_IRQ_MSIX);
  822. if (nvec < 0) {
  823. nvec = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
  824. if (nvec < 0)
  825. return nvec;
  826. INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
  827. irq = pci_irq_vector(nhi->pdev, 0);
  828. if (irq < 0)
  829. return irq;
  830. res = devm_request_irq(&pdev->dev, irq, nhi_msi,
  831. IRQF_NO_SUSPEND, "thunderbolt", nhi);
  832. if (res) {
  833. dev_err(&pdev->dev, "request_irq failed, aborting\n");
  834. return res;
  835. }
  836. }
  837. return 0;
  838. }
  839. static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  840. {
  841. struct tb_nhi *nhi;
  842. struct tb *tb;
  843. int res;
  844. res = pcim_enable_device(pdev);
  845. if (res) {
  846. dev_err(&pdev->dev, "cannot enable PCI device, aborting\n");
  847. return res;
  848. }
  849. res = pcim_iomap_regions(pdev, 1 << 0, "thunderbolt");
  850. if (res) {
  851. dev_err(&pdev->dev, "cannot obtain PCI resources, aborting\n");
  852. return res;
  853. }
  854. nhi = devm_kzalloc(&pdev->dev, sizeof(*nhi), GFP_KERNEL);
  855. if (!nhi)
  856. return -ENOMEM;
  857. nhi->pdev = pdev;
  858. /* cannot fail - table is allocated bin pcim_iomap_regions */
  859. nhi->iobase = pcim_iomap_table(pdev)[0];
  860. nhi->hop_count = ioread32(nhi->iobase + REG_HOP_COUNT) & 0x3ff;
  861. if (nhi->hop_count != 12 && nhi->hop_count != 32)
  862. dev_warn(&pdev->dev, "unexpected hop count: %d\n",
  863. nhi->hop_count);
  864. nhi->tx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
  865. sizeof(*nhi->tx_rings), GFP_KERNEL);
  866. nhi->rx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
  867. sizeof(*nhi->rx_rings), GFP_KERNEL);
  868. if (!nhi->tx_rings || !nhi->rx_rings)
  869. return -ENOMEM;
  870. res = nhi_init_msi(nhi);
  871. if (res) {
  872. dev_err(&pdev->dev, "cannot enable MSI, aborting\n");
  873. return res;
  874. }
  875. spin_lock_init(&nhi->lock);
  876. pci_set_master(pdev);
  877. tb = icm_probe(nhi);
  878. if (!tb)
  879. tb = tb_probe(nhi);
  880. if (!tb) {
  881. dev_err(&nhi->pdev->dev,
  882. "failed to determine connection manager, aborting\n");
  883. return -ENODEV;
  884. }
  885. dev_info(&nhi->pdev->dev, "NHI initialized, starting thunderbolt\n");
  886. res = tb_domain_add(tb);
  887. if (res) {
  888. /*
  889. * At this point the RX/TX rings might already have been
  890. * activated. Do a proper shutdown.
  891. */
  892. tb_domain_put(tb);
  893. nhi_shutdown(nhi);
  894. return res;
  895. }
  896. pci_set_drvdata(pdev, tb);
  897. return 0;
  898. }
  899. static void nhi_remove(struct pci_dev *pdev)
  900. {
  901. struct tb *tb = pci_get_drvdata(pdev);
  902. struct tb_nhi *nhi = tb->nhi;
  903. tb_domain_remove(tb);
  904. nhi_shutdown(nhi);
  905. }
  906. /*
  907. * The tunneled pci bridges are siblings of us. Use resume_noirq to reenable
  908. * the tunnels asap. A corresponding pci quirk blocks the downstream bridges
  909. * resume_noirq until we are done.
  910. */
  911. static const struct dev_pm_ops nhi_pm_ops = {
  912. .suspend_noirq = nhi_suspend_noirq,
  913. .resume_noirq = nhi_resume_noirq,
  914. .freeze_noirq = nhi_suspend_noirq, /*
  915. * we just disable hotplug, the
  916. * pci-tunnels stay alive.
  917. */
  918. .thaw_noirq = nhi_resume_noirq,
  919. .restore_noirq = nhi_resume_noirq,
  920. .suspend = nhi_suspend,
  921. .freeze = nhi_suspend,
  922. .poweroff = nhi_suspend,
  923. .complete = nhi_complete,
  924. };
  925. static struct pci_device_id nhi_ids[] = {
  926. /*
  927. * We have to specify class, the TB bridges use the same device and
  928. * vendor (sub)id on gen 1 and gen 2 controllers.
  929. */
  930. {
  931. .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
  932. .vendor = PCI_VENDOR_ID_INTEL,
  933. .device = PCI_DEVICE_ID_INTEL_LIGHT_RIDGE,
  934. .subvendor = 0x2222, .subdevice = 0x1111,
  935. },
  936. {
  937. .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
  938. .vendor = PCI_VENDOR_ID_INTEL,
  939. .device = PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C,
  940. .subvendor = 0x2222, .subdevice = 0x1111,
  941. },
  942. {
  943. .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
  944. .vendor = PCI_VENDOR_ID_INTEL,
  945. .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI,
  946. .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
  947. },
  948. {
  949. .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
  950. .vendor = PCI_VENDOR_ID_INTEL,
  951. .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI,
  952. .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
  953. },
  954. /* Thunderbolt 3 */
  955. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI) },
  956. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI) },
  957. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_USBONLY_NHI) },
  958. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI) },
  959. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_USBONLY_NHI) },
  960. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI) },
  961. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI) },
  962. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_USBONLY_NHI) },
  963. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI) },
  964. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI) },
  965. { 0,}
  966. };
  967. MODULE_DEVICE_TABLE(pci, nhi_ids);
  968. MODULE_LICENSE("GPL");
  969. static struct pci_driver nhi_driver = {
  970. .name = "thunderbolt",
  971. .id_table = nhi_ids,
  972. .probe = nhi_probe,
  973. .remove = nhi_remove,
  974. .driver.pm = &nhi_pm_ops,
  975. };
  976. static int __init nhi_init(void)
  977. {
  978. int ret;
  979. ret = tb_domain_init();
  980. if (ret)
  981. return ret;
  982. ret = pci_register_driver(&nhi_driver);
  983. if (ret)
  984. tb_domain_exit();
  985. return ret;
  986. }
  987. static void __exit nhi_unload(void)
  988. {
  989. pci_unregister_driver(&nhi_driver);
  990. tb_domain_exit();
  991. }
  992. fs_initcall(nhi_init);
  993. module_exit(nhi_unload);