nhi.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Thunderbolt driver - NHI driver
  4. *
  5. * The NHI (native host interface) is the pci device that allows us to send and
  6. * receive frames from the thunderbolt bus.
  7. *
  8. * Copyright (c) 2014 Andreas Noever <[email protected]>
  9. * Copyright (C) 2018, Intel Corporation
  10. */
  11. #include <linux/pm_runtime.h>
  12. #include <linux/slab.h>
  13. #include <linux/errno.h>
  14. #include <linux/pci.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/iommu.h>
  18. #include <linux/module.h>
  19. #include <linux/delay.h>
  20. #include <linux/property.h>
  21. #include <linux/string_helpers.h>
  22. #include "nhi.h"
  23. #include "nhi_regs.h"
  24. #include "tb.h"
  25. #define RING_TYPE(ring) ((ring)->is_tx ? "TX ring" : "RX ring")
  26. #define RING_FIRST_USABLE_HOPID 1
  27. /*
  28. * Used with QUIRK_E2E to specify an unused HopID the Rx credits are
  29. * transferred.
  30. */
  31. #define RING_E2E_RESERVED_HOPID RING_FIRST_USABLE_HOPID
  32. /*
  33. * Minimal number of vectors when we use MSI-X. Two for control channel
  34. * Rx/Tx and the rest four are for cross domain DMA paths.
  35. */
  36. #define MSIX_MIN_VECS 6
  37. #define MSIX_MAX_VECS 16
  38. #define NHI_MAILBOX_TIMEOUT 500 /* ms */
  39. /* Host interface quirks */
  40. #define QUIRK_AUTO_CLEAR_INT BIT(0)
  41. #define QUIRK_E2E BIT(1)
  42. static int ring_interrupt_index(const struct tb_ring *ring)
  43. {
  44. int bit = ring->hop;
  45. if (!ring->is_tx)
  46. bit += ring->nhi->hop_count;
  47. return bit;
  48. }
  49. static void nhi_mask_interrupt(struct tb_nhi *nhi, int mask, int ring)
  50. {
  51. if (nhi->quirks & QUIRK_AUTO_CLEAR_INT) {
  52. u32 val;
  53. val = ioread32(nhi->iobase + REG_RING_INTERRUPT_BASE + ring);
  54. iowrite32(val & ~mask, nhi->iobase + REG_RING_INTERRUPT_BASE + ring);
  55. } else {
  56. iowrite32(mask, nhi->iobase + REG_RING_INTERRUPT_MASK_CLEAR_BASE + ring);
  57. }
  58. }
  59. static void nhi_clear_interrupt(struct tb_nhi *nhi, int ring)
  60. {
  61. if (nhi->quirks & QUIRK_AUTO_CLEAR_INT)
  62. ioread32(nhi->iobase + REG_RING_NOTIFY_BASE + ring);
  63. else
  64. iowrite32(~0, nhi->iobase + REG_RING_INT_CLEAR + ring);
  65. }
  66. /*
  67. * ring_interrupt_active() - activate/deactivate interrupts for a single ring
  68. *
  69. * ring->nhi->lock must be held.
  70. */
  71. static void ring_interrupt_active(struct tb_ring *ring, bool active)
  72. {
  73. int index = ring_interrupt_index(ring) / 32 * 4;
  74. int reg = REG_RING_INTERRUPT_BASE + index;
  75. int interrupt_bit = ring_interrupt_index(ring) & 31;
  76. int mask = 1 << interrupt_bit;
  77. u32 old, new;
  78. if (ring->irq > 0) {
  79. u32 step, shift, ivr, misc;
  80. void __iomem *ivr_base;
  81. int auto_clear_bit;
  82. int index;
  83. if (ring->is_tx)
  84. index = ring->hop;
  85. else
  86. index = ring->hop + ring->nhi->hop_count;
  87. /*
  88. * Intel routers support a bit that isn't part of
  89. * the USB4 spec to ask the hardware to clear
  90. * interrupt status bits automatically since
  91. * we already know which interrupt was triggered.
  92. *
  93. * Other routers explicitly disable auto-clear
  94. * to prevent conditions that may occur where two
  95. * MSIX interrupts are simultaneously active and
  96. * reading the register clears both of them.
  97. */
  98. misc = ioread32(ring->nhi->iobase + REG_DMA_MISC);
  99. if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT)
  100. auto_clear_bit = REG_DMA_MISC_INT_AUTO_CLEAR;
  101. else
  102. auto_clear_bit = REG_DMA_MISC_DISABLE_AUTO_CLEAR;
  103. if (!(misc & auto_clear_bit))
  104. iowrite32(misc | auto_clear_bit,
  105. ring->nhi->iobase + REG_DMA_MISC);
  106. ivr_base = ring->nhi->iobase + REG_INT_VEC_ALLOC_BASE;
  107. step = index / REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS;
  108. shift = index % REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS;
  109. ivr = ioread32(ivr_base + step);
  110. ivr &= ~(REG_INT_VEC_ALLOC_MASK << shift);
  111. if (active)
  112. ivr |= ring->vector << shift;
  113. iowrite32(ivr, ivr_base + step);
  114. }
  115. old = ioread32(ring->nhi->iobase + reg);
  116. if (active)
  117. new = old | mask;
  118. else
  119. new = old & ~mask;
  120. dev_dbg(&ring->nhi->pdev->dev,
  121. "%s interrupt at register %#x bit %d (%#x -> %#x)\n",
  122. active ? "enabling" : "disabling", reg, interrupt_bit, old, new);
  123. if (new == old)
  124. dev_WARN(&ring->nhi->pdev->dev,
  125. "interrupt for %s %d is already %s\n",
  126. RING_TYPE(ring), ring->hop,
  127. active ? "enabled" : "disabled");
  128. if (active)
  129. iowrite32(new, ring->nhi->iobase + reg);
  130. else
  131. nhi_mask_interrupt(ring->nhi, mask, index);
  132. }
  133. /*
  134. * nhi_disable_interrupts() - disable interrupts for all rings
  135. *
  136. * Use only during init and shutdown.
  137. */
  138. static void nhi_disable_interrupts(struct tb_nhi *nhi)
  139. {
  140. int i = 0;
  141. /* disable interrupts */
  142. for (i = 0; i < RING_INTERRUPT_REG_COUNT(nhi); i++)
  143. nhi_mask_interrupt(nhi, ~0, 4 * i);
  144. /* clear interrupt status bits */
  145. for (i = 0; i < RING_NOTIFY_REG_COUNT(nhi); i++)
  146. nhi_clear_interrupt(nhi, 4 * i);
  147. }
  148. /* ring helper methods */
  149. static void __iomem *ring_desc_base(struct tb_ring *ring)
  150. {
  151. void __iomem *io = ring->nhi->iobase;
  152. io += ring->is_tx ? REG_TX_RING_BASE : REG_RX_RING_BASE;
  153. io += ring->hop * 16;
  154. return io;
  155. }
  156. static void __iomem *ring_options_base(struct tb_ring *ring)
  157. {
  158. void __iomem *io = ring->nhi->iobase;
  159. io += ring->is_tx ? REG_TX_OPTIONS_BASE : REG_RX_OPTIONS_BASE;
  160. io += ring->hop * 32;
  161. return io;
  162. }
  163. static void ring_iowrite_cons(struct tb_ring *ring, u16 cons)
  164. {
  165. /*
  166. * The other 16-bits in the register is read-only and writes to it
  167. * are ignored by the hardware so we can save one ioread32() by
  168. * filling the read-only bits with zeroes.
  169. */
  170. iowrite32(cons, ring_desc_base(ring) + 8);
  171. }
  172. static void ring_iowrite_prod(struct tb_ring *ring, u16 prod)
  173. {
  174. /* See ring_iowrite_cons() above for explanation */
  175. iowrite32(prod << 16, ring_desc_base(ring) + 8);
  176. }
  177. static void ring_iowrite32desc(struct tb_ring *ring, u32 value, u32 offset)
  178. {
  179. iowrite32(value, ring_desc_base(ring) + offset);
  180. }
  181. static void ring_iowrite64desc(struct tb_ring *ring, u64 value, u32 offset)
  182. {
  183. iowrite32(value, ring_desc_base(ring) + offset);
  184. iowrite32(value >> 32, ring_desc_base(ring) + offset + 4);
  185. }
  186. static void ring_iowrite32options(struct tb_ring *ring, u32 value, u32 offset)
  187. {
  188. iowrite32(value, ring_options_base(ring) + offset);
  189. }
  190. static bool ring_full(struct tb_ring *ring)
  191. {
  192. return ((ring->head + 1) % ring->size) == ring->tail;
  193. }
  194. static bool ring_empty(struct tb_ring *ring)
  195. {
  196. return ring->head == ring->tail;
  197. }
  198. /*
  199. * ring_write_descriptors() - post frames from ring->queue to the controller
  200. *
  201. * ring->lock is held.
  202. */
  203. static void ring_write_descriptors(struct tb_ring *ring)
  204. {
  205. struct ring_frame *frame, *n;
  206. struct ring_desc *descriptor;
  207. list_for_each_entry_safe(frame, n, &ring->queue, list) {
  208. if (ring_full(ring))
  209. break;
  210. list_move_tail(&frame->list, &ring->in_flight);
  211. descriptor = &ring->descriptors[ring->head];
  212. descriptor->phys = frame->buffer_phy;
  213. descriptor->time = 0;
  214. descriptor->flags = RING_DESC_POSTED | RING_DESC_INTERRUPT;
  215. if (ring->is_tx) {
  216. descriptor->length = frame->size;
  217. descriptor->eof = frame->eof;
  218. descriptor->sof = frame->sof;
  219. }
  220. ring->head = (ring->head + 1) % ring->size;
  221. if (ring->is_tx)
  222. ring_iowrite_prod(ring, ring->head);
  223. else
  224. ring_iowrite_cons(ring, ring->head);
  225. }
  226. }
  227. /*
  228. * ring_work() - progress completed frames
  229. *
  230. * If the ring is shutting down then all frames are marked as canceled and
  231. * their callbacks are invoked.
  232. *
  233. * Otherwise we collect all completed frame from the ring buffer, write new
  234. * frame to the ring buffer and invoke the callbacks for the completed frames.
  235. */
  236. static void ring_work(struct work_struct *work)
  237. {
  238. struct tb_ring *ring = container_of(work, typeof(*ring), work);
  239. struct ring_frame *frame;
  240. bool canceled = false;
  241. unsigned long flags;
  242. LIST_HEAD(done);
  243. spin_lock_irqsave(&ring->lock, flags);
  244. if (!ring->running) {
  245. /* Move all frames to done and mark them as canceled. */
  246. list_splice_tail_init(&ring->in_flight, &done);
  247. list_splice_tail_init(&ring->queue, &done);
  248. canceled = true;
  249. goto invoke_callback;
  250. }
  251. while (!ring_empty(ring)) {
  252. if (!(ring->descriptors[ring->tail].flags
  253. & RING_DESC_COMPLETED))
  254. break;
  255. frame = list_first_entry(&ring->in_flight, typeof(*frame),
  256. list);
  257. list_move_tail(&frame->list, &done);
  258. if (!ring->is_tx) {
  259. frame->size = ring->descriptors[ring->tail].length;
  260. frame->eof = ring->descriptors[ring->tail].eof;
  261. frame->sof = ring->descriptors[ring->tail].sof;
  262. frame->flags = ring->descriptors[ring->tail].flags;
  263. }
  264. ring->tail = (ring->tail + 1) % ring->size;
  265. }
  266. ring_write_descriptors(ring);
  267. invoke_callback:
  268. /* allow callbacks to schedule new work */
  269. spin_unlock_irqrestore(&ring->lock, flags);
  270. while (!list_empty(&done)) {
  271. frame = list_first_entry(&done, typeof(*frame), list);
  272. /*
  273. * The callback may reenqueue or delete frame.
  274. * Do not hold on to it.
  275. */
  276. list_del_init(&frame->list);
  277. if (frame->callback)
  278. frame->callback(ring, frame, canceled);
  279. }
  280. }
  281. int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame)
  282. {
  283. unsigned long flags;
  284. int ret = 0;
  285. spin_lock_irqsave(&ring->lock, flags);
  286. if (ring->running) {
  287. list_add_tail(&frame->list, &ring->queue);
  288. ring_write_descriptors(ring);
  289. } else {
  290. ret = -ESHUTDOWN;
  291. }
  292. spin_unlock_irqrestore(&ring->lock, flags);
  293. return ret;
  294. }
  295. EXPORT_SYMBOL_GPL(__tb_ring_enqueue);
  296. /**
  297. * tb_ring_poll() - Poll one completed frame from the ring
  298. * @ring: Ring to poll
  299. *
  300. * This function can be called when @start_poll callback of the @ring
  301. * has been called. It will read one completed frame from the ring and
  302. * return it to the caller. Returns %NULL if there is no more completed
  303. * frames.
  304. */
  305. struct ring_frame *tb_ring_poll(struct tb_ring *ring)
  306. {
  307. struct ring_frame *frame = NULL;
  308. unsigned long flags;
  309. spin_lock_irqsave(&ring->lock, flags);
  310. if (!ring->running)
  311. goto unlock;
  312. if (ring_empty(ring))
  313. goto unlock;
  314. if (ring->descriptors[ring->tail].flags & RING_DESC_COMPLETED) {
  315. frame = list_first_entry(&ring->in_flight, typeof(*frame),
  316. list);
  317. list_del_init(&frame->list);
  318. if (!ring->is_tx) {
  319. frame->size = ring->descriptors[ring->tail].length;
  320. frame->eof = ring->descriptors[ring->tail].eof;
  321. frame->sof = ring->descriptors[ring->tail].sof;
  322. frame->flags = ring->descriptors[ring->tail].flags;
  323. }
  324. ring->tail = (ring->tail + 1) % ring->size;
  325. }
  326. unlock:
  327. spin_unlock_irqrestore(&ring->lock, flags);
  328. return frame;
  329. }
  330. EXPORT_SYMBOL_GPL(tb_ring_poll);
  331. static void __ring_interrupt_mask(struct tb_ring *ring, bool mask)
  332. {
  333. int idx = ring_interrupt_index(ring);
  334. int reg = REG_RING_INTERRUPT_BASE + idx / 32 * 4;
  335. int bit = idx % 32;
  336. u32 val;
  337. val = ioread32(ring->nhi->iobase + reg);
  338. if (mask)
  339. val &= ~BIT(bit);
  340. else
  341. val |= BIT(bit);
  342. iowrite32(val, ring->nhi->iobase + reg);
  343. }
  344. /* Both @nhi->lock and @ring->lock should be held */
  345. static void __ring_interrupt(struct tb_ring *ring)
  346. {
  347. if (!ring->running)
  348. return;
  349. if (ring->start_poll) {
  350. __ring_interrupt_mask(ring, true);
  351. ring->start_poll(ring->poll_data);
  352. } else {
  353. schedule_work(&ring->work);
  354. }
  355. }
  356. /**
  357. * tb_ring_poll_complete() - Re-start interrupt for the ring
  358. * @ring: Ring to re-start the interrupt
  359. *
  360. * This will re-start (unmask) the ring interrupt once the user is done
  361. * with polling.
  362. */
  363. void tb_ring_poll_complete(struct tb_ring *ring)
  364. {
  365. unsigned long flags;
  366. spin_lock_irqsave(&ring->nhi->lock, flags);
  367. spin_lock(&ring->lock);
  368. if (ring->start_poll)
  369. __ring_interrupt_mask(ring, false);
  370. spin_unlock(&ring->lock);
  371. spin_unlock_irqrestore(&ring->nhi->lock, flags);
  372. }
  373. EXPORT_SYMBOL_GPL(tb_ring_poll_complete);
  374. static void ring_clear_msix(const struct tb_ring *ring)
  375. {
  376. int bit;
  377. if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT)
  378. return;
  379. bit = ring_interrupt_index(ring) & 31;
  380. if (ring->is_tx)
  381. iowrite32(BIT(bit), ring->nhi->iobase + REG_RING_INT_CLEAR);
  382. else
  383. iowrite32(BIT(bit), ring->nhi->iobase + REG_RING_INT_CLEAR +
  384. 4 * (ring->nhi->hop_count / 32));
  385. }
  386. static irqreturn_t ring_msix(int irq, void *data)
  387. {
  388. struct tb_ring *ring = data;
  389. spin_lock(&ring->nhi->lock);
  390. ring_clear_msix(ring);
  391. spin_lock(&ring->lock);
  392. __ring_interrupt(ring);
  393. spin_unlock(&ring->lock);
  394. spin_unlock(&ring->nhi->lock);
  395. return IRQ_HANDLED;
  396. }
  397. static int ring_request_msix(struct tb_ring *ring, bool no_suspend)
  398. {
  399. struct tb_nhi *nhi = ring->nhi;
  400. unsigned long irqflags;
  401. int ret;
  402. if (!nhi->pdev->msix_enabled)
  403. return 0;
  404. ret = ida_simple_get(&nhi->msix_ida, 0, MSIX_MAX_VECS, GFP_KERNEL);
  405. if (ret < 0)
  406. return ret;
  407. ring->vector = ret;
  408. ret = pci_irq_vector(ring->nhi->pdev, ring->vector);
  409. if (ret < 0)
  410. goto err_ida_remove;
  411. ring->irq = ret;
  412. irqflags = no_suspend ? IRQF_NO_SUSPEND : 0;
  413. ret = request_irq(ring->irq, ring_msix, irqflags, "thunderbolt", ring);
  414. if (ret)
  415. goto err_ida_remove;
  416. return 0;
  417. err_ida_remove:
  418. ida_simple_remove(&nhi->msix_ida, ring->vector);
  419. return ret;
  420. }
  421. static void ring_release_msix(struct tb_ring *ring)
  422. {
  423. if (ring->irq <= 0)
  424. return;
  425. free_irq(ring->irq, ring);
  426. ida_simple_remove(&ring->nhi->msix_ida, ring->vector);
  427. ring->vector = 0;
  428. ring->irq = 0;
  429. }
  430. static int nhi_alloc_hop(struct tb_nhi *nhi, struct tb_ring *ring)
  431. {
  432. unsigned int start_hop = RING_FIRST_USABLE_HOPID;
  433. int ret = 0;
  434. if (nhi->quirks & QUIRK_E2E) {
  435. start_hop = RING_FIRST_USABLE_HOPID + 1;
  436. if (ring->flags & RING_FLAG_E2E && !ring->is_tx) {
  437. dev_dbg(&nhi->pdev->dev, "quirking E2E TX HopID %u -> %u\n",
  438. ring->e2e_tx_hop, RING_E2E_RESERVED_HOPID);
  439. ring->e2e_tx_hop = RING_E2E_RESERVED_HOPID;
  440. }
  441. }
  442. spin_lock_irq(&nhi->lock);
  443. if (ring->hop < 0) {
  444. unsigned int i;
  445. /*
  446. * Automatically allocate HopID from the non-reserved
  447. * range 1 .. hop_count - 1.
  448. */
  449. for (i = start_hop; i < nhi->hop_count; i++) {
  450. if (ring->is_tx) {
  451. if (!nhi->tx_rings[i]) {
  452. ring->hop = i;
  453. break;
  454. }
  455. } else {
  456. if (!nhi->rx_rings[i]) {
  457. ring->hop = i;
  458. break;
  459. }
  460. }
  461. }
  462. }
  463. if (ring->hop > 0 && ring->hop < start_hop) {
  464. dev_warn(&nhi->pdev->dev, "invalid hop: %d\n", ring->hop);
  465. ret = -EINVAL;
  466. goto err_unlock;
  467. }
  468. if (ring->hop < 0 || ring->hop >= nhi->hop_count) {
  469. dev_warn(&nhi->pdev->dev, "invalid hop: %d\n", ring->hop);
  470. ret = -EINVAL;
  471. goto err_unlock;
  472. }
  473. if (ring->is_tx && nhi->tx_rings[ring->hop]) {
  474. dev_warn(&nhi->pdev->dev, "TX hop %d already allocated\n",
  475. ring->hop);
  476. ret = -EBUSY;
  477. goto err_unlock;
  478. } else if (!ring->is_tx && nhi->rx_rings[ring->hop]) {
  479. dev_warn(&nhi->pdev->dev, "RX hop %d already allocated\n",
  480. ring->hop);
  481. ret = -EBUSY;
  482. goto err_unlock;
  483. }
  484. if (ring->is_tx)
  485. nhi->tx_rings[ring->hop] = ring;
  486. else
  487. nhi->rx_rings[ring->hop] = ring;
  488. err_unlock:
  489. spin_unlock_irq(&nhi->lock);
  490. return ret;
  491. }
  492. static struct tb_ring *tb_ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
  493. bool transmit, unsigned int flags,
  494. int e2e_tx_hop, u16 sof_mask, u16 eof_mask,
  495. void (*start_poll)(void *),
  496. void *poll_data)
  497. {
  498. struct tb_ring *ring = NULL;
  499. dev_dbg(&nhi->pdev->dev, "allocating %s ring %d of size %d\n",
  500. transmit ? "TX" : "RX", hop, size);
  501. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  502. if (!ring)
  503. return NULL;
  504. spin_lock_init(&ring->lock);
  505. INIT_LIST_HEAD(&ring->queue);
  506. INIT_LIST_HEAD(&ring->in_flight);
  507. INIT_WORK(&ring->work, ring_work);
  508. ring->nhi = nhi;
  509. ring->hop = hop;
  510. ring->is_tx = transmit;
  511. ring->size = size;
  512. ring->flags = flags;
  513. ring->e2e_tx_hop = e2e_tx_hop;
  514. ring->sof_mask = sof_mask;
  515. ring->eof_mask = eof_mask;
  516. ring->head = 0;
  517. ring->tail = 0;
  518. ring->running = false;
  519. ring->start_poll = start_poll;
  520. ring->poll_data = poll_data;
  521. ring->descriptors = dma_alloc_coherent(&ring->nhi->pdev->dev,
  522. size * sizeof(*ring->descriptors),
  523. &ring->descriptors_dma, GFP_KERNEL | __GFP_ZERO);
  524. if (!ring->descriptors)
  525. goto err_free_ring;
  526. if (ring_request_msix(ring, flags & RING_FLAG_NO_SUSPEND))
  527. goto err_free_descs;
  528. if (nhi_alloc_hop(nhi, ring))
  529. goto err_release_msix;
  530. return ring;
  531. err_release_msix:
  532. ring_release_msix(ring);
  533. err_free_descs:
  534. dma_free_coherent(&ring->nhi->pdev->dev,
  535. ring->size * sizeof(*ring->descriptors),
  536. ring->descriptors, ring->descriptors_dma);
  537. err_free_ring:
  538. kfree(ring);
  539. return NULL;
  540. }
  541. /**
  542. * tb_ring_alloc_tx() - Allocate DMA ring for transmit
  543. * @nhi: Pointer to the NHI the ring is to be allocated
  544. * @hop: HopID (ring) to allocate
  545. * @size: Number of entries in the ring
  546. * @flags: Flags for the ring
  547. */
  548. struct tb_ring *tb_ring_alloc_tx(struct tb_nhi *nhi, int hop, int size,
  549. unsigned int flags)
  550. {
  551. return tb_ring_alloc(nhi, hop, size, true, flags, 0, 0, 0, NULL, NULL);
  552. }
  553. EXPORT_SYMBOL_GPL(tb_ring_alloc_tx);
  554. /**
  555. * tb_ring_alloc_rx() - Allocate DMA ring for receive
  556. * @nhi: Pointer to the NHI the ring is to be allocated
  557. * @hop: HopID (ring) to allocate. Pass %-1 for automatic allocation.
  558. * @size: Number of entries in the ring
  559. * @flags: Flags for the ring
  560. * @e2e_tx_hop: Transmit HopID when E2E is enabled in @flags
  561. * @sof_mask: Mask of PDF values that start a frame
  562. * @eof_mask: Mask of PDF values that end a frame
  563. * @start_poll: If not %NULL the ring will call this function when an
  564. * interrupt is triggered and masked, instead of callback
  565. * in each Rx frame.
  566. * @poll_data: Optional data passed to @start_poll
  567. */
  568. struct tb_ring *tb_ring_alloc_rx(struct tb_nhi *nhi, int hop, int size,
  569. unsigned int flags, int e2e_tx_hop,
  570. u16 sof_mask, u16 eof_mask,
  571. void (*start_poll)(void *), void *poll_data)
  572. {
  573. return tb_ring_alloc(nhi, hop, size, false, flags, e2e_tx_hop, sof_mask, eof_mask,
  574. start_poll, poll_data);
  575. }
  576. EXPORT_SYMBOL_GPL(tb_ring_alloc_rx);
  577. /**
  578. * tb_ring_start() - enable a ring
  579. * @ring: Ring to start
  580. *
  581. * Must not be invoked in parallel with tb_ring_stop().
  582. */
  583. void tb_ring_start(struct tb_ring *ring)
  584. {
  585. u16 frame_size;
  586. u32 flags;
  587. spin_lock_irq(&ring->nhi->lock);
  588. spin_lock(&ring->lock);
  589. if (ring->nhi->going_away)
  590. goto err;
  591. if (ring->running) {
  592. dev_WARN(&ring->nhi->pdev->dev, "ring already started\n");
  593. goto err;
  594. }
  595. dev_dbg(&ring->nhi->pdev->dev, "starting %s %d\n",
  596. RING_TYPE(ring), ring->hop);
  597. if (ring->flags & RING_FLAG_FRAME) {
  598. /* Means 4096 */
  599. frame_size = 0;
  600. flags = RING_FLAG_ENABLE;
  601. } else {
  602. frame_size = TB_FRAME_SIZE;
  603. flags = RING_FLAG_ENABLE | RING_FLAG_RAW;
  604. }
  605. ring_iowrite64desc(ring, ring->descriptors_dma, 0);
  606. if (ring->is_tx) {
  607. ring_iowrite32desc(ring, ring->size, 12);
  608. ring_iowrite32options(ring, 0, 4); /* time releated ? */
  609. ring_iowrite32options(ring, flags, 0);
  610. } else {
  611. u32 sof_eof_mask = ring->sof_mask << 16 | ring->eof_mask;
  612. ring_iowrite32desc(ring, (frame_size << 16) | ring->size, 12);
  613. ring_iowrite32options(ring, sof_eof_mask, 4);
  614. ring_iowrite32options(ring, flags, 0);
  615. }
  616. /*
  617. * Now that the ring valid bit is set we can configure E2E if
  618. * enabled for the ring.
  619. */
  620. if (ring->flags & RING_FLAG_E2E) {
  621. if (!ring->is_tx) {
  622. u32 hop;
  623. hop = ring->e2e_tx_hop << REG_RX_OPTIONS_E2E_HOP_SHIFT;
  624. hop &= REG_RX_OPTIONS_E2E_HOP_MASK;
  625. flags |= hop;
  626. dev_dbg(&ring->nhi->pdev->dev,
  627. "enabling E2E for %s %d with TX HopID %d\n",
  628. RING_TYPE(ring), ring->hop, ring->e2e_tx_hop);
  629. } else {
  630. dev_dbg(&ring->nhi->pdev->dev, "enabling E2E for %s %d\n",
  631. RING_TYPE(ring), ring->hop);
  632. }
  633. flags |= RING_FLAG_E2E_FLOW_CONTROL;
  634. ring_iowrite32options(ring, flags, 0);
  635. }
  636. ring_interrupt_active(ring, true);
  637. ring->running = true;
  638. err:
  639. spin_unlock(&ring->lock);
  640. spin_unlock_irq(&ring->nhi->lock);
  641. }
  642. EXPORT_SYMBOL_GPL(tb_ring_start);
  643. /**
  644. * tb_ring_stop() - shutdown a ring
  645. * @ring: Ring to stop
  646. *
  647. * Must not be invoked from a callback.
  648. *
  649. * This method will disable the ring. Further calls to
  650. * tb_ring_tx/tb_ring_rx will return -ESHUTDOWN until ring_stop has been
  651. * called.
  652. *
  653. * All enqueued frames will be canceled and their callbacks will be executed
  654. * with frame->canceled set to true (on the callback thread). This method
  655. * returns only after all callback invocations have finished.
  656. */
  657. void tb_ring_stop(struct tb_ring *ring)
  658. {
  659. spin_lock_irq(&ring->nhi->lock);
  660. spin_lock(&ring->lock);
  661. dev_dbg(&ring->nhi->pdev->dev, "stopping %s %d\n",
  662. RING_TYPE(ring), ring->hop);
  663. if (ring->nhi->going_away)
  664. goto err;
  665. if (!ring->running) {
  666. dev_WARN(&ring->nhi->pdev->dev, "%s %d already stopped\n",
  667. RING_TYPE(ring), ring->hop);
  668. goto err;
  669. }
  670. ring_interrupt_active(ring, false);
  671. ring_iowrite32options(ring, 0, 0);
  672. ring_iowrite64desc(ring, 0, 0);
  673. ring_iowrite32desc(ring, 0, 8);
  674. ring_iowrite32desc(ring, 0, 12);
  675. ring->head = 0;
  676. ring->tail = 0;
  677. ring->running = false;
  678. err:
  679. spin_unlock(&ring->lock);
  680. spin_unlock_irq(&ring->nhi->lock);
  681. /*
  682. * schedule ring->work to invoke callbacks on all remaining frames.
  683. */
  684. schedule_work(&ring->work);
  685. flush_work(&ring->work);
  686. }
  687. EXPORT_SYMBOL_GPL(tb_ring_stop);
  688. /*
  689. * tb_ring_free() - free ring
  690. *
  691. * When this method returns all invocations of ring->callback will have
  692. * finished.
  693. *
  694. * Ring must be stopped.
  695. *
  696. * Must NOT be called from ring_frame->callback!
  697. */
  698. void tb_ring_free(struct tb_ring *ring)
  699. {
  700. spin_lock_irq(&ring->nhi->lock);
  701. /*
  702. * Dissociate the ring from the NHI. This also ensures that
  703. * nhi_interrupt_work cannot reschedule ring->work.
  704. */
  705. if (ring->is_tx)
  706. ring->nhi->tx_rings[ring->hop] = NULL;
  707. else
  708. ring->nhi->rx_rings[ring->hop] = NULL;
  709. if (ring->running) {
  710. dev_WARN(&ring->nhi->pdev->dev, "%s %d still running\n",
  711. RING_TYPE(ring), ring->hop);
  712. }
  713. spin_unlock_irq(&ring->nhi->lock);
  714. ring_release_msix(ring);
  715. dma_free_coherent(&ring->nhi->pdev->dev,
  716. ring->size * sizeof(*ring->descriptors),
  717. ring->descriptors, ring->descriptors_dma);
  718. ring->descriptors = NULL;
  719. ring->descriptors_dma = 0;
  720. dev_dbg(&ring->nhi->pdev->dev, "freeing %s %d\n", RING_TYPE(ring),
  721. ring->hop);
  722. /*
  723. * ring->work can no longer be scheduled (it is scheduled only
  724. * by nhi_interrupt_work, ring_stop and ring_msix). Wait for it
  725. * to finish before freeing the ring.
  726. */
  727. flush_work(&ring->work);
  728. kfree(ring);
  729. }
  730. EXPORT_SYMBOL_GPL(tb_ring_free);
  731. /**
  732. * nhi_mailbox_cmd() - Send a command through NHI mailbox
  733. * @nhi: Pointer to the NHI structure
  734. * @cmd: Command to send
  735. * @data: Data to be send with the command
  736. *
  737. * Sends mailbox command to the firmware running on NHI. Returns %0 in
  738. * case of success and negative errno in case of failure.
  739. */
  740. int nhi_mailbox_cmd(struct tb_nhi *nhi, enum nhi_mailbox_cmd cmd, u32 data)
  741. {
  742. ktime_t timeout;
  743. u32 val;
  744. iowrite32(data, nhi->iobase + REG_INMAIL_DATA);
  745. val = ioread32(nhi->iobase + REG_INMAIL_CMD);
  746. val &= ~(REG_INMAIL_CMD_MASK | REG_INMAIL_ERROR);
  747. val |= REG_INMAIL_OP_REQUEST | cmd;
  748. iowrite32(val, nhi->iobase + REG_INMAIL_CMD);
  749. timeout = ktime_add_ms(ktime_get(), NHI_MAILBOX_TIMEOUT);
  750. do {
  751. val = ioread32(nhi->iobase + REG_INMAIL_CMD);
  752. if (!(val & REG_INMAIL_OP_REQUEST))
  753. break;
  754. usleep_range(10, 20);
  755. } while (ktime_before(ktime_get(), timeout));
  756. if (val & REG_INMAIL_OP_REQUEST)
  757. return -ETIMEDOUT;
  758. if (val & REG_INMAIL_ERROR)
  759. return -EIO;
  760. return 0;
  761. }
  762. /**
  763. * nhi_mailbox_mode() - Return current firmware operation mode
  764. * @nhi: Pointer to the NHI structure
  765. *
  766. * The function reads current firmware operation mode using NHI mailbox
  767. * registers and returns it to the caller.
  768. */
  769. enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi)
  770. {
  771. u32 val;
  772. val = ioread32(nhi->iobase + REG_OUTMAIL_CMD);
  773. val &= REG_OUTMAIL_CMD_OPMODE_MASK;
  774. val >>= REG_OUTMAIL_CMD_OPMODE_SHIFT;
  775. return (enum nhi_fw_mode)val;
  776. }
  777. static void nhi_interrupt_work(struct work_struct *work)
  778. {
  779. struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work);
  780. int value = 0; /* Suppress uninitialized usage warning. */
  781. int bit;
  782. int hop = -1;
  783. int type = 0; /* current interrupt type 0: TX, 1: RX, 2: RX overflow */
  784. struct tb_ring *ring;
  785. spin_lock_irq(&nhi->lock);
  786. /*
  787. * Starting at REG_RING_NOTIFY_BASE there are three status bitfields
  788. * (TX, RX, RX overflow). We iterate over the bits and read a new
  789. * dwords as required. The registers are cleared on read.
  790. */
  791. for (bit = 0; bit < 3 * nhi->hop_count; bit++) {
  792. if (bit % 32 == 0)
  793. value = ioread32(nhi->iobase
  794. + REG_RING_NOTIFY_BASE
  795. + 4 * (bit / 32));
  796. if (++hop == nhi->hop_count) {
  797. hop = 0;
  798. type++;
  799. }
  800. if ((value & (1 << (bit % 32))) == 0)
  801. continue;
  802. if (type == 2) {
  803. dev_warn(&nhi->pdev->dev,
  804. "RX overflow for ring %d\n",
  805. hop);
  806. continue;
  807. }
  808. if (type == 0)
  809. ring = nhi->tx_rings[hop];
  810. else
  811. ring = nhi->rx_rings[hop];
  812. if (ring == NULL) {
  813. dev_warn(&nhi->pdev->dev,
  814. "got interrupt for inactive %s ring %d\n",
  815. type ? "RX" : "TX",
  816. hop);
  817. continue;
  818. }
  819. spin_lock(&ring->lock);
  820. __ring_interrupt(ring);
  821. spin_unlock(&ring->lock);
  822. }
  823. spin_unlock_irq(&nhi->lock);
  824. }
  825. static irqreturn_t nhi_msi(int irq, void *data)
  826. {
  827. struct tb_nhi *nhi = data;
  828. schedule_work(&nhi->interrupt_work);
  829. return IRQ_HANDLED;
  830. }
  831. static int __nhi_suspend_noirq(struct device *dev, bool wakeup)
  832. {
  833. struct pci_dev *pdev = to_pci_dev(dev);
  834. struct tb *tb = pci_get_drvdata(pdev);
  835. struct tb_nhi *nhi = tb->nhi;
  836. int ret;
  837. ret = tb_domain_suspend_noirq(tb);
  838. if (ret)
  839. return ret;
  840. if (nhi->ops && nhi->ops->suspend_noirq) {
  841. ret = nhi->ops->suspend_noirq(tb->nhi, wakeup);
  842. if (ret)
  843. return ret;
  844. }
  845. return 0;
  846. }
  847. static int nhi_suspend_noirq(struct device *dev)
  848. {
  849. return __nhi_suspend_noirq(dev, device_may_wakeup(dev));
  850. }
  851. static int nhi_freeze_noirq(struct device *dev)
  852. {
  853. struct pci_dev *pdev = to_pci_dev(dev);
  854. struct tb *tb = pci_get_drvdata(pdev);
  855. return tb_domain_freeze_noirq(tb);
  856. }
  857. static int nhi_thaw_noirq(struct device *dev)
  858. {
  859. struct pci_dev *pdev = to_pci_dev(dev);
  860. struct tb *tb = pci_get_drvdata(pdev);
  861. return tb_domain_thaw_noirq(tb);
  862. }
  863. static bool nhi_wake_supported(struct pci_dev *pdev)
  864. {
  865. u8 val;
  866. /*
  867. * If power rails are sustainable for wakeup from S4 this
  868. * property is set by the BIOS.
  869. */
  870. if (device_property_read_u8(&pdev->dev, "WAKE_SUPPORTED", &val))
  871. return !!val;
  872. return true;
  873. }
  874. static int nhi_poweroff_noirq(struct device *dev)
  875. {
  876. struct pci_dev *pdev = to_pci_dev(dev);
  877. bool wakeup;
  878. wakeup = device_may_wakeup(dev) && nhi_wake_supported(pdev);
  879. return __nhi_suspend_noirq(dev, wakeup);
  880. }
  881. static void nhi_enable_int_throttling(struct tb_nhi *nhi)
  882. {
  883. /* Throttling is specified in 256ns increments */
  884. u32 throttle = DIV_ROUND_UP(128 * NSEC_PER_USEC, 256);
  885. unsigned int i;
  886. /*
  887. * Configure interrupt throttling for all vectors even if we
  888. * only use few.
  889. */
  890. for (i = 0; i < MSIX_MAX_VECS; i++) {
  891. u32 reg = REG_INT_THROTTLING_RATE + i * 4;
  892. iowrite32(throttle, nhi->iobase + reg);
  893. }
  894. }
  895. static int nhi_resume_noirq(struct device *dev)
  896. {
  897. struct pci_dev *pdev = to_pci_dev(dev);
  898. struct tb *tb = pci_get_drvdata(pdev);
  899. struct tb_nhi *nhi = tb->nhi;
  900. int ret;
  901. /*
  902. * Check that the device is still there. It may be that the user
  903. * unplugged last device which causes the host controller to go
  904. * away on PCs.
  905. */
  906. if (!pci_device_is_present(pdev)) {
  907. nhi->going_away = true;
  908. } else {
  909. if (nhi->ops && nhi->ops->resume_noirq) {
  910. ret = nhi->ops->resume_noirq(nhi);
  911. if (ret)
  912. return ret;
  913. }
  914. nhi_enable_int_throttling(tb->nhi);
  915. }
  916. return tb_domain_resume_noirq(tb);
  917. }
  918. static int nhi_suspend(struct device *dev)
  919. {
  920. struct pci_dev *pdev = to_pci_dev(dev);
  921. struct tb *tb = pci_get_drvdata(pdev);
  922. return tb_domain_suspend(tb);
  923. }
  924. static void nhi_complete(struct device *dev)
  925. {
  926. struct pci_dev *pdev = to_pci_dev(dev);
  927. struct tb *tb = pci_get_drvdata(pdev);
  928. /*
  929. * If we were runtime suspended when system suspend started,
  930. * schedule runtime resume now. It should bring the domain back
  931. * to functional state.
  932. */
  933. if (pm_runtime_suspended(&pdev->dev))
  934. pm_runtime_resume(&pdev->dev);
  935. else
  936. tb_domain_complete(tb);
  937. }
  938. static int nhi_runtime_suspend(struct device *dev)
  939. {
  940. struct pci_dev *pdev = to_pci_dev(dev);
  941. struct tb *tb = pci_get_drvdata(pdev);
  942. struct tb_nhi *nhi = tb->nhi;
  943. int ret;
  944. ret = tb_domain_runtime_suspend(tb);
  945. if (ret)
  946. return ret;
  947. if (nhi->ops && nhi->ops->runtime_suspend) {
  948. ret = nhi->ops->runtime_suspend(tb->nhi);
  949. if (ret)
  950. return ret;
  951. }
  952. return 0;
  953. }
  954. static int nhi_runtime_resume(struct device *dev)
  955. {
  956. struct pci_dev *pdev = to_pci_dev(dev);
  957. struct tb *tb = pci_get_drvdata(pdev);
  958. struct tb_nhi *nhi = tb->nhi;
  959. int ret;
  960. if (nhi->ops && nhi->ops->runtime_resume) {
  961. ret = nhi->ops->runtime_resume(nhi);
  962. if (ret)
  963. return ret;
  964. }
  965. nhi_enable_int_throttling(nhi);
  966. return tb_domain_runtime_resume(tb);
  967. }
  968. static void nhi_shutdown(struct tb_nhi *nhi)
  969. {
  970. int i;
  971. dev_dbg(&nhi->pdev->dev, "shutdown\n");
  972. for (i = 0; i < nhi->hop_count; i++) {
  973. if (nhi->tx_rings[i])
  974. dev_WARN(&nhi->pdev->dev,
  975. "TX ring %d is still active\n", i);
  976. if (nhi->rx_rings[i])
  977. dev_WARN(&nhi->pdev->dev,
  978. "RX ring %d is still active\n", i);
  979. }
  980. nhi_disable_interrupts(nhi);
  981. /*
  982. * We have to release the irq before calling flush_work. Otherwise an
  983. * already executing IRQ handler could call schedule_work again.
  984. */
  985. if (!nhi->pdev->msix_enabled) {
  986. devm_free_irq(&nhi->pdev->dev, nhi->pdev->irq, nhi);
  987. flush_work(&nhi->interrupt_work);
  988. }
  989. ida_destroy(&nhi->msix_ida);
  990. if (nhi->ops && nhi->ops->shutdown)
  991. nhi->ops->shutdown(nhi);
  992. }
  993. static void nhi_check_quirks(struct tb_nhi *nhi)
  994. {
  995. if (nhi->pdev->vendor == PCI_VENDOR_ID_INTEL) {
  996. /*
  997. * Intel hardware supports auto clear of the interrupt
  998. * status register right after interrupt is being
  999. * issued.
  1000. */
  1001. nhi->quirks |= QUIRK_AUTO_CLEAR_INT;
  1002. switch (nhi->pdev->device) {
  1003. case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
  1004. case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
  1005. /*
  1006. * Falcon Ridge controller needs the end-to-end
  1007. * flow control workaround to avoid losing Rx
  1008. * packets when RING_FLAG_E2E is set.
  1009. */
  1010. nhi->quirks |= QUIRK_E2E;
  1011. break;
  1012. }
  1013. }
  1014. }
  1015. static int nhi_check_iommu_pdev(struct pci_dev *pdev, void *data)
  1016. {
  1017. if (!pdev->external_facing ||
  1018. !device_iommu_capable(&pdev->dev, IOMMU_CAP_PRE_BOOT_PROTECTION))
  1019. return 0;
  1020. *(bool *)data = true;
  1021. return 1; /* Stop walking */
  1022. }
  1023. static void nhi_check_iommu(struct tb_nhi *nhi)
  1024. {
  1025. struct pci_bus *bus = nhi->pdev->bus;
  1026. bool port_ok = false;
  1027. /*
  1028. * Ideally what we'd do here is grab every PCI device that
  1029. * represents a tunnelling adapter for this NHI and check their
  1030. * status directly, but unfortunately USB4 seems to make it
  1031. * obnoxiously difficult to reliably make any correlation.
  1032. *
  1033. * So for now we'll have to bodge it... Hoping that the system
  1034. * is at least sane enough that an adapter is in the same PCI
  1035. * segment as its NHI, if we can find *something* on that segment
  1036. * which meets the requirements for Kernel DMA Protection, we'll
  1037. * take that to imply that firmware is aware and has (hopefully)
  1038. * done the right thing in general. We need to know that the PCI
  1039. * layer has seen the ExternalFacingPort property which will then
  1040. * inform the IOMMU layer to enforce the complete "untrusted DMA"
  1041. * flow, but also that the IOMMU driver itself can be trusted not
  1042. * to have been subverted by a pre-boot DMA attack.
  1043. */
  1044. while (bus->parent)
  1045. bus = bus->parent;
  1046. pci_walk_bus(bus, nhi_check_iommu_pdev, &port_ok);
  1047. nhi->iommu_dma_protection = port_ok;
  1048. dev_dbg(&nhi->pdev->dev, "IOMMU DMA protection is %s\n",
  1049. str_enabled_disabled(port_ok));
  1050. }
  1051. static int nhi_init_msi(struct tb_nhi *nhi)
  1052. {
  1053. struct pci_dev *pdev = nhi->pdev;
  1054. struct device *dev = &pdev->dev;
  1055. int res, irq, nvec;
  1056. /* In case someone left them on. */
  1057. nhi_disable_interrupts(nhi);
  1058. nhi_enable_int_throttling(nhi);
  1059. ida_init(&nhi->msix_ida);
  1060. /*
  1061. * The NHI has 16 MSI-X vectors or a single MSI. We first try to
  1062. * get all MSI-X vectors and if we succeed, each ring will have
  1063. * one MSI-X. If for some reason that does not work out, we
  1064. * fallback to a single MSI.
  1065. */
  1066. nvec = pci_alloc_irq_vectors(pdev, MSIX_MIN_VECS, MSIX_MAX_VECS,
  1067. PCI_IRQ_MSIX);
  1068. if (nvec < 0) {
  1069. nvec = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
  1070. if (nvec < 0)
  1071. return nvec;
  1072. INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
  1073. irq = pci_irq_vector(nhi->pdev, 0);
  1074. if (irq < 0)
  1075. return irq;
  1076. res = devm_request_irq(&pdev->dev, irq, nhi_msi,
  1077. IRQF_NO_SUSPEND, "thunderbolt", nhi);
  1078. if (res)
  1079. return dev_err_probe(dev, res, "request_irq failed, aborting\n");
  1080. }
  1081. return 0;
  1082. }
  1083. static bool nhi_imr_valid(struct pci_dev *pdev)
  1084. {
  1085. u8 val;
  1086. if (!device_property_read_u8(&pdev->dev, "IMR_VALID", &val))
  1087. return !!val;
  1088. return true;
  1089. }
  1090. static struct tb *nhi_select_cm(struct tb_nhi *nhi)
  1091. {
  1092. struct tb *tb;
  1093. /*
  1094. * USB4 case is simple. If we got control of any of the
  1095. * capabilities, we use software CM.
  1096. */
  1097. if (tb_acpi_is_native())
  1098. return tb_probe(nhi);
  1099. /*
  1100. * Either firmware based CM is running (we did not get control
  1101. * from the firmware) or this is pre-USB4 PC so try first
  1102. * firmware CM and then fallback to software CM.
  1103. */
  1104. tb = icm_probe(nhi);
  1105. if (!tb)
  1106. tb = tb_probe(nhi);
  1107. return tb;
  1108. }
  1109. static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1110. {
  1111. struct device *dev = &pdev->dev;
  1112. struct tb_nhi *nhi;
  1113. struct tb *tb;
  1114. int res;
  1115. if (!nhi_imr_valid(pdev))
  1116. return dev_err_probe(dev, -ENODEV, "firmware image not valid, aborting\n");
  1117. res = pcim_enable_device(pdev);
  1118. if (res)
  1119. return dev_err_probe(dev, res, "cannot enable PCI device, aborting\n");
  1120. res = pcim_iomap_regions(pdev, 1 << 0, "thunderbolt");
  1121. if (res)
  1122. return dev_err_probe(dev, res, "cannot obtain PCI resources, aborting\n");
  1123. nhi = devm_kzalloc(&pdev->dev, sizeof(*nhi), GFP_KERNEL);
  1124. if (!nhi)
  1125. return -ENOMEM;
  1126. nhi->pdev = pdev;
  1127. nhi->ops = (const struct tb_nhi_ops *)id->driver_data;
  1128. /* cannot fail - table is allocated in pcim_iomap_regions */
  1129. nhi->iobase = pcim_iomap_table(pdev)[0];
  1130. nhi->hop_count = ioread32(nhi->iobase + REG_HOP_COUNT) & 0x3ff;
  1131. dev_dbg(dev, "total paths: %d\n", nhi->hop_count);
  1132. nhi->tx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
  1133. sizeof(*nhi->tx_rings), GFP_KERNEL);
  1134. nhi->rx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
  1135. sizeof(*nhi->rx_rings), GFP_KERNEL);
  1136. if (!nhi->tx_rings || !nhi->rx_rings)
  1137. return -ENOMEM;
  1138. nhi_check_quirks(nhi);
  1139. nhi_check_iommu(nhi);
  1140. res = nhi_init_msi(nhi);
  1141. if (res)
  1142. return dev_err_probe(dev, res, "cannot enable MSI, aborting\n");
  1143. spin_lock_init(&nhi->lock);
  1144. res = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  1145. if (res)
  1146. return dev_err_probe(dev, res, "failed to set DMA mask\n");
  1147. pci_set_master(pdev);
  1148. if (nhi->ops && nhi->ops->init) {
  1149. res = nhi->ops->init(nhi);
  1150. if (res)
  1151. return res;
  1152. }
  1153. tb = nhi_select_cm(nhi);
  1154. if (!tb)
  1155. return dev_err_probe(dev, -ENODEV,
  1156. "failed to determine connection manager, aborting\n");
  1157. dev_dbg(dev, "NHI initialized, starting thunderbolt\n");
  1158. res = tb_domain_add(tb);
  1159. if (res) {
  1160. /*
  1161. * At this point the RX/TX rings might already have been
  1162. * activated. Do a proper shutdown.
  1163. */
  1164. tb_domain_put(tb);
  1165. nhi_shutdown(nhi);
  1166. return res;
  1167. }
  1168. pci_set_drvdata(pdev, tb);
  1169. device_wakeup_enable(&pdev->dev);
  1170. pm_runtime_allow(&pdev->dev);
  1171. pm_runtime_set_autosuspend_delay(&pdev->dev, TB_AUTOSUSPEND_DELAY);
  1172. pm_runtime_use_autosuspend(&pdev->dev);
  1173. pm_runtime_put_autosuspend(&pdev->dev);
  1174. return 0;
  1175. }
  1176. static void nhi_remove(struct pci_dev *pdev)
  1177. {
  1178. struct tb *tb = pci_get_drvdata(pdev);
  1179. struct tb_nhi *nhi = tb->nhi;
  1180. pm_runtime_get_sync(&pdev->dev);
  1181. pm_runtime_dont_use_autosuspend(&pdev->dev);
  1182. pm_runtime_forbid(&pdev->dev);
  1183. tb_domain_remove(tb);
  1184. nhi_shutdown(nhi);
  1185. }
  1186. /*
  1187. * The tunneled pci bridges are siblings of us. Use resume_noirq to reenable
  1188. * the tunnels asap. A corresponding pci quirk blocks the downstream bridges
  1189. * resume_noirq until we are done.
  1190. */
  1191. static const struct dev_pm_ops nhi_pm_ops = {
  1192. .suspend_noirq = nhi_suspend_noirq,
  1193. .resume_noirq = nhi_resume_noirq,
  1194. .freeze_noirq = nhi_freeze_noirq, /*
  1195. * we just disable hotplug, the
  1196. * pci-tunnels stay alive.
  1197. */
  1198. .thaw_noirq = nhi_thaw_noirq,
  1199. .restore_noirq = nhi_resume_noirq,
  1200. .suspend = nhi_suspend,
  1201. .poweroff_noirq = nhi_poweroff_noirq,
  1202. .poweroff = nhi_suspend,
  1203. .complete = nhi_complete,
  1204. .runtime_suspend = nhi_runtime_suspend,
  1205. .runtime_resume = nhi_runtime_resume,
  1206. };
  1207. static struct pci_device_id nhi_ids[] = {
  1208. /*
  1209. * We have to specify class, the TB bridges use the same device and
  1210. * vendor (sub)id on gen 1 and gen 2 controllers.
  1211. */
  1212. {
  1213. .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
  1214. .vendor = PCI_VENDOR_ID_INTEL,
  1215. .device = PCI_DEVICE_ID_INTEL_LIGHT_RIDGE,
  1216. .subvendor = 0x2222, .subdevice = 0x1111,
  1217. },
  1218. {
  1219. .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
  1220. .vendor = PCI_VENDOR_ID_INTEL,
  1221. .device = PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C,
  1222. .subvendor = 0x2222, .subdevice = 0x1111,
  1223. },
  1224. {
  1225. .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
  1226. .vendor = PCI_VENDOR_ID_INTEL,
  1227. .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI,
  1228. .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
  1229. },
  1230. {
  1231. .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
  1232. .vendor = PCI_VENDOR_ID_INTEL,
  1233. .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI,
  1234. .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
  1235. },
  1236. /* Thunderbolt 3 */
  1237. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI) },
  1238. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI) },
  1239. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_USBONLY_NHI) },
  1240. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI) },
  1241. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_USBONLY_NHI) },
  1242. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI) },
  1243. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI) },
  1244. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_USBONLY_NHI) },
  1245. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI) },
  1246. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI) },
  1247. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICL_NHI0),
  1248. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1249. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICL_NHI1),
  1250. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1251. /* Thunderbolt 4 */
  1252. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_NHI0),
  1253. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1254. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_NHI1),
  1255. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1256. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_H_NHI0),
  1257. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1258. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_H_NHI1),
  1259. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1260. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADL_NHI0),
  1261. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1262. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADL_NHI1),
  1263. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1264. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_RPL_NHI0),
  1265. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1266. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_RPL_NHI1),
  1267. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1268. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MTL_M_NHI0),
  1269. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1270. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MTL_P_NHI0),
  1271. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1272. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MTL_P_NHI1),
  1273. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1274. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BARLOW_RIDGE_HOST_80G_NHI) },
  1275. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BARLOW_RIDGE_HOST_40G_NHI) },
  1276. /* Any USB4 compliant host */
  1277. { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_USB4, ~0) },
  1278. { 0,}
  1279. };
  1280. MODULE_DEVICE_TABLE(pci, nhi_ids);
  1281. MODULE_LICENSE("GPL");
  1282. static struct pci_driver nhi_driver = {
  1283. .name = "thunderbolt",
  1284. .id_table = nhi_ids,
  1285. .probe = nhi_probe,
  1286. .remove = nhi_remove,
  1287. .shutdown = nhi_remove,
  1288. .driver.pm = &nhi_pm_ops,
  1289. };
  1290. static int __init nhi_init(void)
  1291. {
  1292. int ret;
  1293. ret = tb_domain_init();
  1294. if (ret)
  1295. return ret;
  1296. ret = pci_register_driver(&nhi_driver);
  1297. if (ret)
  1298. tb_domain_exit();
  1299. return ret;
  1300. }
  1301. static void __exit nhi_unload(void)
  1302. {
  1303. pci_unregister_driver(&nhi_driver);
  1304. tb_domain_exit();
  1305. }
  1306. rootfs_initcall(nhi_init);
  1307. module_exit(nhi_unload);