nhi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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. * Minimal number of vectors when we use MSI-X. Two for control channel
  22. * Rx/Tx and the rest four are for cross domain DMA paths.
  23. */
  24. #define MSIX_MIN_VECS 6
  25. #define MSIX_MAX_VECS 16
  26. #define NHI_MAILBOX_TIMEOUT 500 /* ms */
  27. static int ring_interrupt_index(struct tb_ring *ring)
  28. {
  29. int bit = ring->hop;
  30. if (!ring->is_tx)
  31. bit += ring->nhi->hop_count;
  32. return bit;
  33. }
  34. /**
  35. * ring_interrupt_active() - activate/deactivate interrupts for a single ring
  36. *
  37. * ring->nhi->lock must be held.
  38. */
  39. static void ring_interrupt_active(struct tb_ring *ring, bool active)
  40. {
  41. int reg = REG_RING_INTERRUPT_BASE +
  42. ring_interrupt_index(ring) / 32 * 4;
  43. int bit = ring_interrupt_index(ring) & 31;
  44. int mask = 1 << bit;
  45. u32 old, new;
  46. if (ring->irq > 0) {
  47. u32 step, shift, ivr, misc;
  48. void __iomem *ivr_base;
  49. int index;
  50. if (ring->is_tx)
  51. index = ring->hop;
  52. else
  53. index = ring->hop + ring->nhi->hop_count;
  54. /*
  55. * Ask the hardware to clear interrupt status bits automatically
  56. * since we already know which interrupt was triggered.
  57. */
  58. misc = ioread32(ring->nhi->iobase + REG_DMA_MISC);
  59. if (!(misc & REG_DMA_MISC_INT_AUTO_CLEAR)) {
  60. misc |= REG_DMA_MISC_INT_AUTO_CLEAR;
  61. iowrite32(misc, ring->nhi->iobase + REG_DMA_MISC);
  62. }
  63. ivr_base = ring->nhi->iobase + REG_INT_VEC_ALLOC_BASE;
  64. step = index / REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS;
  65. shift = index % REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS;
  66. ivr = ioread32(ivr_base + step);
  67. ivr &= ~(REG_INT_VEC_ALLOC_MASK << shift);
  68. if (active)
  69. ivr |= ring->vector << shift;
  70. iowrite32(ivr, ivr_base + step);
  71. }
  72. old = ioread32(ring->nhi->iobase + reg);
  73. if (active)
  74. new = old | mask;
  75. else
  76. new = old & ~mask;
  77. dev_info(&ring->nhi->pdev->dev,
  78. "%s interrupt at register %#x bit %d (%#x -> %#x)\n",
  79. active ? "enabling" : "disabling", reg, bit, old, new);
  80. if (new == old)
  81. dev_WARN(&ring->nhi->pdev->dev,
  82. "interrupt for %s %d is already %s\n",
  83. RING_TYPE(ring), ring->hop,
  84. active ? "enabled" : "disabled");
  85. iowrite32(new, ring->nhi->iobase + reg);
  86. }
  87. /**
  88. * nhi_disable_interrupts() - disable interrupts for all rings
  89. *
  90. * Use only during init and shutdown.
  91. */
  92. static void nhi_disable_interrupts(struct tb_nhi *nhi)
  93. {
  94. int i = 0;
  95. /* disable interrupts */
  96. for (i = 0; i < RING_INTERRUPT_REG_COUNT(nhi); i++)
  97. iowrite32(0, nhi->iobase + REG_RING_INTERRUPT_BASE + 4 * i);
  98. /* clear interrupt status bits */
  99. for (i = 0; i < RING_NOTIFY_REG_COUNT(nhi); i++)
  100. ioread32(nhi->iobase + REG_RING_NOTIFY_BASE + 4 * i);
  101. }
  102. /* ring helper methods */
  103. static void __iomem *ring_desc_base(struct tb_ring *ring)
  104. {
  105. void __iomem *io = ring->nhi->iobase;
  106. io += ring->is_tx ? REG_TX_RING_BASE : REG_RX_RING_BASE;
  107. io += ring->hop * 16;
  108. return io;
  109. }
  110. static void __iomem *ring_options_base(struct tb_ring *ring)
  111. {
  112. void __iomem *io = ring->nhi->iobase;
  113. io += ring->is_tx ? REG_TX_OPTIONS_BASE : REG_RX_OPTIONS_BASE;
  114. io += ring->hop * 32;
  115. return io;
  116. }
  117. static void ring_iowrite16desc(struct tb_ring *ring, u32 value, u32 offset)
  118. {
  119. iowrite16(value, ring_desc_base(ring) + offset);
  120. }
  121. static void ring_iowrite32desc(struct tb_ring *ring, u32 value, u32 offset)
  122. {
  123. iowrite32(value, ring_desc_base(ring) + offset);
  124. }
  125. static void ring_iowrite64desc(struct tb_ring *ring, u64 value, u32 offset)
  126. {
  127. iowrite32(value, ring_desc_base(ring) + offset);
  128. iowrite32(value >> 32, ring_desc_base(ring) + offset + 4);
  129. }
  130. static void ring_iowrite32options(struct tb_ring *ring, u32 value, u32 offset)
  131. {
  132. iowrite32(value, ring_options_base(ring) + offset);
  133. }
  134. static bool ring_full(struct tb_ring *ring)
  135. {
  136. return ((ring->head + 1) % ring->size) == ring->tail;
  137. }
  138. static bool ring_empty(struct tb_ring *ring)
  139. {
  140. return ring->head == ring->tail;
  141. }
  142. /**
  143. * ring_write_descriptors() - post frames from ring->queue to the controller
  144. *
  145. * ring->lock is held.
  146. */
  147. static void ring_write_descriptors(struct tb_ring *ring)
  148. {
  149. struct ring_frame *frame, *n;
  150. struct ring_desc *descriptor;
  151. list_for_each_entry_safe(frame, n, &ring->queue, list) {
  152. if (ring_full(ring))
  153. break;
  154. list_move_tail(&frame->list, &ring->in_flight);
  155. descriptor = &ring->descriptors[ring->head];
  156. descriptor->phys = frame->buffer_phy;
  157. descriptor->time = 0;
  158. descriptor->flags = RING_DESC_POSTED | RING_DESC_INTERRUPT;
  159. if (ring->is_tx) {
  160. descriptor->length = frame->size;
  161. descriptor->eof = frame->eof;
  162. descriptor->sof = frame->sof;
  163. }
  164. ring->head = (ring->head + 1) % ring->size;
  165. ring_iowrite16desc(ring, ring->head, ring->is_tx ? 10 : 8);
  166. }
  167. }
  168. /**
  169. * ring_work() - progress completed frames
  170. *
  171. * If the ring is shutting down then all frames are marked as canceled and
  172. * their callbacks are invoked.
  173. *
  174. * Otherwise we collect all completed frame from the ring buffer, write new
  175. * frame to the ring buffer and invoke the callbacks for the completed frames.
  176. */
  177. static void ring_work(struct work_struct *work)
  178. {
  179. struct tb_ring *ring = container_of(work, typeof(*ring), work);
  180. struct ring_frame *frame;
  181. bool canceled = false;
  182. LIST_HEAD(done);
  183. mutex_lock(&ring->lock);
  184. if (!ring->running) {
  185. /* Move all frames to done and mark them as canceled. */
  186. list_splice_tail_init(&ring->in_flight, &done);
  187. list_splice_tail_init(&ring->queue, &done);
  188. canceled = true;
  189. goto invoke_callback;
  190. }
  191. while (!ring_empty(ring)) {
  192. if (!(ring->descriptors[ring->tail].flags
  193. & RING_DESC_COMPLETED))
  194. break;
  195. frame = list_first_entry(&ring->in_flight, typeof(*frame),
  196. list);
  197. list_move_tail(&frame->list, &done);
  198. if (!ring->is_tx) {
  199. frame->size = ring->descriptors[ring->tail].length;
  200. frame->eof = ring->descriptors[ring->tail].eof;
  201. frame->sof = ring->descriptors[ring->tail].sof;
  202. frame->flags = ring->descriptors[ring->tail].flags;
  203. if (frame->sof != 0)
  204. dev_WARN(&ring->nhi->pdev->dev,
  205. "%s %d got unexpected SOF: %#x\n",
  206. RING_TYPE(ring), ring->hop,
  207. frame->sof);
  208. /*
  209. * known flags:
  210. * raw not enabled, interupt not set: 0x2=0010
  211. * raw enabled: 0xa=1010
  212. * raw not enabled: 0xb=1011
  213. * partial frame (>MAX_FRAME_SIZE): 0xe=1110
  214. */
  215. if (frame->flags != 0xa)
  216. dev_WARN(&ring->nhi->pdev->dev,
  217. "%s %d got unexpected flags: %#x\n",
  218. RING_TYPE(ring), ring->hop,
  219. frame->flags);
  220. }
  221. ring->tail = (ring->tail + 1) % ring->size;
  222. }
  223. ring_write_descriptors(ring);
  224. invoke_callback:
  225. mutex_unlock(&ring->lock); /* allow callbacks to schedule new work */
  226. while (!list_empty(&done)) {
  227. frame = list_first_entry(&done, typeof(*frame), list);
  228. /*
  229. * The callback may reenqueue or delete frame.
  230. * Do not hold on to it.
  231. */
  232. list_del_init(&frame->list);
  233. frame->callback(ring, frame, canceled);
  234. }
  235. }
  236. int __ring_enqueue(struct tb_ring *ring, struct ring_frame *frame)
  237. {
  238. int ret = 0;
  239. mutex_lock(&ring->lock);
  240. if (ring->running) {
  241. list_add_tail(&frame->list, &ring->queue);
  242. ring_write_descriptors(ring);
  243. } else {
  244. ret = -ESHUTDOWN;
  245. }
  246. mutex_unlock(&ring->lock);
  247. return ret;
  248. }
  249. static irqreturn_t ring_msix(int irq, void *data)
  250. {
  251. struct tb_ring *ring = data;
  252. schedule_work(&ring->work);
  253. return IRQ_HANDLED;
  254. }
  255. static int ring_request_msix(struct tb_ring *ring, bool no_suspend)
  256. {
  257. struct tb_nhi *nhi = ring->nhi;
  258. unsigned long irqflags;
  259. int ret;
  260. if (!nhi->pdev->msix_enabled)
  261. return 0;
  262. ret = ida_simple_get(&nhi->msix_ida, 0, MSIX_MAX_VECS, GFP_KERNEL);
  263. if (ret < 0)
  264. return ret;
  265. ring->vector = ret;
  266. ring->irq = pci_irq_vector(ring->nhi->pdev, ring->vector);
  267. if (ring->irq < 0)
  268. return ring->irq;
  269. irqflags = no_suspend ? IRQF_NO_SUSPEND : 0;
  270. return request_irq(ring->irq, ring_msix, irqflags, "thunderbolt", ring);
  271. }
  272. static void ring_release_msix(struct tb_ring *ring)
  273. {
  274. if (ring->irq <= 0)
  275. return;
  276. free_irq(ring->irq, ring);
  277. ida_simple_remove(&ring->nhi->msix_ida, ring->vector);
  278. ring->vector = 0;
  279. ring->irq = 0;
  280. }
  281. static struct tb_ring *ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
  282. bool transmit, unsigned int flags)
  283. {
  284. struct tb_ring *ring = NULL;
  285. dev_info(&nhi->pdev->dev, "allocating %s ring %d of size %d\n",
  286. transmit ? "TX" : "RX", hop, size);
  287. mutex_lock(&nhi->lock);
  288. if (hop >= nhi->hop_count) {
  289. dev_WARN(&nhi->pdev->dev, "invalid hop: %d\n", hop);
  290. goto err;
  291. }
  292. if (transmit && nhi->tx_rings[hop]) {
  293. dev_WARN(&nhi->pdev->dev, "TX hop %d already allocated\n", hop);
  294. goto err;
  295. } else if (!transmit && nhi->rx_rings[hop]) {
  296. dev_WARN(&nhi->pdev->dev, "RX hop %d already allocated\n", hop);
  297. goto err;
  298. }
  299. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  300. if (!ring)
  301. goto err;
  302. mutex_init(&ring->lock);
  303. INIT_LIST_HEAD(&ring->queue);
  304. INIT_LIST_HEAD(&ring->in_flight);
  305. INIT_WORK(&ring->work, ring_work);
  306. ring->nhi = nhi;
  307. ring->hop = hop;
  308. ring->is_tx = transmit;
  309. ring->size = size;
  310. ring->flags = flags;
  311. ring->head = 0;
  312. ring->tail = 0;
  313. ring->running = false;
  314. if (ring_request_msix(ring, flags & RING_FLAG_NO_SUSPEND))
  315. goto err;
  316. ring->descriptors = dma_alloc_coherent(&ring->nhi->pdev->dev,
  317. size * sizeof(*ring->descriptors),
  318. &ring->descriptors_dma, GFP_KERNEL | __GFP_ZERO);
  319. if (!ring->descriptors)
  320. goto err;
  321. if (transmit)
  322. nhi->tx_rings[hop] = ring;
  323. else
  324. nhi->rx_rings[hop] = ring;
  325. mutex_unlock(&nhi->lock);
  326. return ring;
  327. err:
  328. if (ring)
  329. mutex_destroy(&ring->lock);
  330. kfree(ring);
  331. mutex_unlock(&nhi->lock);
  332. return NULL;
  333. }
  334. struct tb_ring *ring_alloc_tx(struct tb_nhi *nhi, int hop, int size,
  335. unsigned int flags)
  336. {
  337. return ring_alloc(nhi, hop, size, true, flags);
  338. }
  339. struct tb_ring *ring_alloc_rx(struct tb_nhi *nhi, int hop, int size,
  340. unsigned int flags)
  341. {
  342. return ring_alloc(nhi, hop, size, false, flags);
  343. }
  344. /**
  345. * ring_start() - enable a ring
  346. *
  347. * Must not be invoked in parallel with ring_stop().
  348. */
  349. void ring_start(struct tb_ring *ring)
  350. {
  351. mutex_lock(&ring->nhi->lock);
  352. mutex_lock(&ring->lock);
  353. if (ring->nhi->going_away)
  354. goto err;
  355. if (ring->running) {
  356. dev_WARN(&ring->nhi->pdev->dev, "ring already started\n");
  357. goto err;
  358. }
  359. dev_info(&ring->nhi->pdev->dev, "starting %s %d\n",
  360. RING_TYPE(ring), ring->hop);
  361. ring_iowrite64desc(ring, ring->descriptors_dma, 0);
  362. if (ring->is_tx) {
  363. ring_iowrite32desc(ring, ring->size, 12);
  364. ring_iowrite32options(ring, 0, 4); /* time releated ? */
  365. ring_iowrite32options(ring,
  366. RING_FLAG_ENABLE | RING_FLAG_RAW, 0);
  367. } else {
  368. ring_iowrite32desc(ring,
  369. (TB_FRAME_SIZE << 16) | ring->size, 12);
  370. ring_iowrite32options(ring, 0xffffffff, 4); /* SOF EOF mask */
  371. ring_iowrite32options(ring,
  372. RING_FLAG_ENABLE | RING_FLAG_RAW, 0);
  373. }
  374. ring_interrupt_active(ring, true);
  375. ring->running = true;
  376. err:
  377. mutex_unlock(&ring->lock);
  378. mutex_unlock(&ring->nhi->lock);
  379. }
  380. /**
  381. * ring_stop() - shutdown a ring
  382. *
  383. * Must not be invoked from a callback.
  384. *
  385. * This method will disable the ring. Further calls to ring_tx/ring_rx will
  386. * return -ESHUTDOWN until ring_stop has been called.
  387. *
  388. * All enqueued frames will be canceled and their callbacks will be executed
  389. * with frame->canceled set to true (on the callback thread). This method
  390. * returns only after all callback invocations have finished.
  391. */
  392. void ring_stop(struct tb_ring *ring)
  393. {
  394. mutex_lock(&ring->nhi->lock);
  395. mutex_lock(&ring->lock);
  396. dev_info(&ring->nhi->pdev->dev, "stopping %s %d\n",
  397. RING_TYPE(ring), ring->hop);
  398. if (ring->nhi->going_away)
  399. goto err;
  400. if (!ring->running) {
  401. dev_WARN(&ring->nhi->pdev->dev, "%s %d already stopped\n",
  402. RING_TYPE(ring), ring->hop);
  403. goto err;
  404. }
  405. ring_interrupt_active(ring, false);
  406. ring_iowrite32options(ring, 0, 0);
  407. ring_iowrite64desc(ring, 0, 0);
  408. ring_iowrite16desc(ring, 0, ring->is_tx ? 10 : 8);
  409. ring_iowrite32desc(ring, 0, 12);
  410. ring->head = 0;
  411. ring->tail = 0;
  412. ring->running = false;
  413. err:
  414. mutex_unlock(&ring->lock);
  415. mutex_unlock(&ring->nhi->lock);
  416. /*
  417. * schedule ring->work to invoke callbacks on all remaining frames.
  418. */
  419. schedule_work(&ring->work);
  420. flush_work(&ring->work);
  421. }
  422. /*
  423. * ring_free() - free ring
  424. *
  425. * When this method returns all invocations of ring->callback will have
  426. * finished.
  427. *
  428. * Ring must be stopped.
  429. *
  430. * Must NOT be called from ring_frame->callback!
  431. */
  432. void ring_free(struct tb_ring *ring)
  433. {
  434. mutex_lock(&ring->nhi->lock);
  435. /*
  436. * Dissociate the ring from the NHI. This also ensures that
  437. * nhi_interrupt_work cannot reschedule ring->work.
  438. */
  439. if (ring->is_tx)
  440. ring->nhi->tx_rings[ring->hop] = NULL;
  441. else
  442. ring->nhi->rx_rings[ring->hop] = NULL;
  443. if (ring->running) {
  444. dev_WARN(&ring->nhi->pdev->dev, "%s %d still running\n",
  445. RING_TYPE(ring), ring->hop);
  446. }
  447. ring_release_msix(ring);
  448. dma_free_coherent(&ring->nhi->pdev->dev,
  449. ring->size * sizeof(*ring->descriptors),
  450. ring->descriptors, ring->descriptors_dma);
  451. ring->descriptors = NULL;
  452. ring->descriptors_dma = 0;
  453. dev_info(&ring->nhi->pdev->dev,
  454. "freeing %s %d\n",
  455. RING_TYPE(ring),
  456. ring->hop);
  457. mutex_unlock(&ring->nhi->lock);
  458. /**
  459. * ring->work can no longer be scheduled (it is scheduled only
  460. * by nhi_interrupt_work, ring_stop and ring_msix). Wait for it
  461. * to finish before freeing the ring.
  462. */
  463. flush_work(&ring->work);
  464. mutex_destroy(&ring->lock);
  465. kfree(ring);
  466. }
  467. /**
  468. * nhi_mailbox_cmd() - Send a command through NHI mailbox
  469. * @nhi: Pointer to the NHI structure
  470. * @cmd: Command to send
  471. * @data: Data to be send with the command
  472. *
  473. * Sends mailbox command to the firmware running on NHI. Returns %0 in
  474. * case of success and negative errno in case of failure.
  475. */
  476. int nhi_mailbox_cmd(struct tb_nhi *nhi, enum nhi_mailbox_cmd cmd, u32 data)
  477. {
  478. ktime_t timeout;
  479. u32 val;
  480. iowrite32(data, nhi->iobase + REG_INMAIL_DATA);
  481. val = ioread32(nhi->iobase + REG_INMAIL_CMD);
  482. val &= ~(REG_INMAIL_CMD_MASK | REG_INMAIL_ERROR);
  483. val |= REG_INMAIL_OP_REQUEST | cmd;
  484. iowrite32(val, nhi->iobase + REG_INMAIL_CMD);
  485. timeout = ktime_add_ms(ktime_get(), NHI_MAILBOX_TIMEOUT);
  486. do {
  487. val = ioread32(nhi->iobase + REG_INMAIL_CMD);
  488. if (!(val & REG_INMAIL_OP_REQUEST))
  489. break;
  490. usleep_range(10, 20);
  491. } while (ktime_before(ktime_get(), timeout));
  492. if (val & REG_INMAIL_OP_REQUEST)
  493. return -ETIMEDOUT;
  494. if (val & REG_INMAIL_ERROR)
  495. return -EIO;
  496. return 0;
  497. }
  498. /**
  499. * nhi_mailbox_mode() - Return current firmware operation mode
  500. * @nhi: Pointer to the NHI structure
  501. *
  502. * The function reads current firmware operation mode using NHI mailbox
  503. * registers and returns it to the caller.
  504. */
  505. enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi)
  506. {
  507. u32 val;
  508. val = ioread32(nhi->iobase + REG_OUTMAIL_CMD);
  509. val &= REG_OUTMAIL_CMD_OPMODE_MASK;
  510. val >>= REG_OUTMAIL_CMD_OPMODE_SHIFT;
  511. return (enum nhi_fw_mode)val;
  512. }
  513. static void nhi_interrupt_work(struct work_struct *work)
  514. {
  515. struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work);
  516. int value = 0; /* Suppress uninitialized usage warning. */
  517. int bit;
  518. int hop = -1;
  519. int type = 0; /* current interrupt type 0: TX, 1: RX, 2: RX overflow */
  520. struct tb_ring *ring;
  521. mutex_lock(&nhi->lock);
  522. /*
  523. * Starting at REG_RING_NOTIFY_BASE there are three status bitfields
  524. * (TX, RX, RX overflow). We iterate over the bits and read a new
  525. * dwords as required. The registers are cleared on read.
  526. */
  527. for (bit = 0; bit < 3 * nhi->hop_count; bit++) {
  528. if (bit % 32 == 0)
  529. value = ioread32(nhi->iobase
  530. + REG_RING_NOTIFY_BASE
  531. + 4 * (bit / 32));
  532. if (++hop == nhi->hop_count) {
  533. hop = 0;
  534. type++;
  535. }
  536. if ((value & (1 << (bit % 32))) == 0)
  537. continue;
  538. if (type == 2) {
  539. dev_warn(&nhi->pdev->dev,
  540. "RX overflow for ring %d\n",
  541. hop);
  542. continue;
  543. }
  544. if (type == 0)
  545. ring = nhi->tx_rings[hop];
  546. else
  547. ring = nhi->rx_rings[hop];
  548. if (ring == NULL) {
  549. dev_warn(&nhi->pdev->dev,
  550. "got interrupt for inactive %s ring %d\n",
  551. type ? "RX" : "TX",
  552. hop);
  553. continue;
  554. }
  555. /* we do not check ring->running, this is done in ring->work */
  556. schedule_work(&ring->work);
  557. }
  558. mutex_unlock(&nhi->lock);
  559. }
  560. static irqreturn_t nhi_msi(int irq, void *data)
  561. {
  562. struct tb_nhi *nhi = data;
  563. schedule_work(&nhi->interrupt_work);
  564. return IRQ_HANDLED;
  565. }
  566. static int nhi_suspend_noirq(struct device *dev)
  567. {
  568. struct pci_dev *pdev = to_pci_dev(dev);
  569. struct tb *tb = pci_get_drvdata(pdev);
  570. return tb_domain_suspend_noirq(tb);
  571. }
  572. static int nhi_resume_noirq(struct device *dev)
  573. {
  574. struct pci_dev *pdev = to_pci_dev(dev);
  575. struct tb *tb = pci_get_drvdata(pdev);
  576. /*
  577. * Check that the device is still there. It may be that the user
  578. * unplugged last device which causes the host controller to go
  579. * away on PCs.
  580. */
  581. if (!pci_device_is_present(pdev))
  582. tb->nhi->going_away = true;
  583. return tb_domain_resume_noirq(tb);
  584. }
  585. static int nhi_suspend(struct device *dev)
  586. {
  587. struct pci_dev *pdev = to_pci_dev(dev);
  588. struct tb *tb = pci_get_drvdata(pdev);
  589. return tb_domain_suspend(tb);
  590. }
  591. static void nhi_complete(struct device *dev)
  592. {
  593. struct pci_dev *pdev = to_pci_dev(dev);
  594. struct tb *tb = pci_get_drvdata(pdev);
  595. tb_domain_complete(tb);
  596. }
  597. static void nhi_shutdown(struct tb_nhi *nhi)
  598. {
  599. int i;
  600. dev_info(&nhi->pdev->dev, "shutdown\n");
  601. for (i = 0; i < nhi->hop_count; i++) {
  602. if (nhi->tx_rings[i])
  603. dev_WARN(&nhi->pdev->dev,
  604. "TX ring %d is still active\n", i);
  605. if (nhi->rx_rings[i])
  606. dev_WARN(&nhi->pdev->dev,
  607. "RX ring %d is still active\n", i);
  608. }
  609. nhi_disable_interrupts(nhi);
  610. /*
  611. * We have to release the irq before calling flush_work. Otherwise an
  612. * already executing IRQ handler could call schedule_work again.
  613. */
  614. if (!nhi->pdev->msix_enabled) {
  615. devm_free_irq(&nhi->pdev->dev, nhi->pdev->irq, nhi);
  616. flush_work(&nhi->interrupt_work);
  617. }
  618. mutex_destroy(&nhi->lock);
  619. ida_destroy(&nhi->msix_ida);
  620. }
  621. static int nhi_init_msi(struct tb_nhi *nhi)
  622. {
  623. struct pci_dev *pdev = nhi->pdev;
  624. int res, irq, nvec;
  625. /* In case someone left them on. */
  626. nhi_disable_interrupts(nhi);
  627. ida_init(&nhi->msix_ida);
  628. /*
  629. * The NHI has 16 MSI-X vectors or a single MSI. We first try to
  630. * get all MSI-X vectors and if we succeed, each ring will have
  631. * one MSI-X. If for some reason that does not work out, we
  632. * fallback to a single MSI.
  633. */
  634. nvec = pci_alloc_irq_vectors(pdev, MSIX_MIN_VECS, MSIX_MAX_VECS,
  635. PCI_IRQ_MSIX);
  636. if (nvec < 0) {
  637. nvec = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
  638. if (nvec < 0)
  639. return nvec;
  640. INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
  641. irq = pci_irq_vector(nhi->pdev, 0);
  642. if (irq < 0)
  643. return irq;
  644. res = devm_request_irq(&pdev->dev, irq, nhi_msi,
  645. IRQF_NO_SUSPEND, "thunderbolt", nhi);
  646. if (res) {
  647. dev_err(&pdev->dev, "request_irq failed, aborting\n");
  648. return res;
  649. }
  650. }
  651. return 0;
  652. }
  653. static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  654. {
  655. struct tb_nhi *nhi;
  656. struct tb *tb;
  657. int res;
  658. res = pcim_enable_device(pdev);
  659. if (res) {
  660. dev_err(&pdev->dev, "cannot enable PCI device, aborting\n");
  661. return res;
  662. }
  663. res = pcim_iomap_regions(pdev, 1 << 0, "thunderbolt");
  664. if (res) {
  665. dev_err(&pdev->dev, "cannot obtain PCI resources, aborting\n");
  666. return res;
  667. }
  668. nhi = devm_kzalloc(&pdev->dev, sizeof(*nhi), GFP_KERNEL);
  669. if (!nhi)
  670. return -ENOMEM;
  671. nhi->pdev = pdev;
  672. /* cannot fail - table is allocated bin pcim_iomap_regions */
  673. nhi->iobase = pcim_iomap_table(pdev)[0];
  674. nhi->hop_count = ioread32(nhi->iobase + REG_HOP_COUNT) & 0x3ff;
  675. if (nhi->hop_count != 12 && nhi->hop_count != 32)
  676. dev_warn(&pdev->dev, "unexpected hop count: %d\n",
  677. nhi->hop_count);
  678. nhi->tx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
  679. sizeof(*nhi->tx_rings), GFP_KERNEL);
  680. nhi->rx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
  681. sizeof(*nhi->rx_rings), GFP_KERNEL);
  682. if (!nhi->tx_rings || !nhi->rx_rings)
  683. return -ENOMEM;
  684. res = nhi_init_msi(nhi);
  685. if (res) {
  686. dev_err(&pdev->dev, "cannot enable MSI, aborting\n");
  687. return res;
  688. }
  689. mutex_init(&nhi->lock);
  690. pci_set_master(pdev);
  691. /* magic value - clock related? */
  692. iowrite32(3906250 / 10000, nhi->iobase + 0x38c00);
  693. tb = icm_probe(nhi);
  694. if (!tb)
  695. tb = tb_probe(nhi);
  696. if (!tb) {
  697. dev_err(&nhi->pdev->dev,
  698. "failed to determine connection manager, aborting\n");
  699. return -ENODEV;
  700. }
  701. dev_info(&nhi->pdev->dev, "NHI initialized, starting thunderbolt\n");
  702. res = tb_domain_add(tb);
  703. if (res) {
  704. /*
  705. * At this point the RX/TX rings might already have been
  706. * activated. Do a proper shutdown.
  707. */
  708. tb_domain_put(tb);
  709. nhi_shutdown(nhi);
  710. return -EIO;
  711. }
  712. pci_set_drvdata(pdev, tb);
  713. return 0;
  714. }
  715. static void nhi_remove(struct pci_dev *pdev)
  716. {
  717. struct tb *tb = pci_get_drvdata(pdev);
  718. struct tb_nhi *nhi = tb->nhi;
  719. tb_domain_remove(tb);
  720. nhi_shutdown(nhi);
  721. }
  722. /*
  723. * The tunneled pci bridges are siblings of us. Use resume_noirq to reenable
  724. * the tunnels asap. A corresponding pci quirk blocks the downstream bridges
  725. * resume_noirq until we are done.
  726. */
  727. static const struct dev_pm_ops nhi_pm_ops = {
  728. .suspend_noirq = nhi_suspend_noirq,
  729. .resume_noirq = nhi_resume_noirq,
  730. .freeze_noirq = nhi_suspend_noirq, /*
  731. * we just disable hotplug, the
  732. * pci-tunnels stay alive.
  733. */
  734. .restore_noirq = nhi_resume_noirq,
  735. .suspend = nhi_suspend,
  736. .freeze = nhi_suspend,
  737. .poweroff = nhi_suspend,
  738. .complete = nhi_complete,
  739. };
  740. static struct pci_device_id nhi_ids[] = {
  741. /*
  742. * We have to specify class, the TB bridges use the same device and
  743. * vendor (sub)id on gen 1 and gen 2 controllers.
  744. */
  745. {
  746. .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
  747. .vendor = PCI_VENDOR_ID_INTEL,
  748. .device = PCI_DEVICE_ID_INTEL_LIGHT_RIDGE,
  749. .subvendor = 0x2222, .subdevice = 0x1111,
  750. },
  751. {
  752. .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
  753. .vendor = PCI_VENDOR_ID_INTEL,
  754. .device = PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C,
  755. .subvendor = 0x2222, .subdevice = 0x1111,
  756. },
  757. {
  758. .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
  759. .vendor = PCI_VENDOR_ID_INTEL,
  760. .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI,
  761. .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
  762. },
  763. {
  764. .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
  765. .vendor = PCI_VENDOR_ID_INTEL,
  766. .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI,
  767. .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
  768. },
  769. /* Thunderbolt 3 */
  770. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI) },
  771. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI) },
  772. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_USBONLY_NHI) },
  773. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI) },
  774. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_USBONLY_NHI) },
  775. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI) },
  776. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI) },
  777. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_USBONLY_NHI) },
  778. { 0,}
  779. };
  780. MODULE_DEVICE_TABLE(pci, nhi_ids);
  781. MODULE_LICENSE("GPL");
  782. static struct pci_driver nhi_driver = {
  783. .name = "thunderbolt",
  784. .id_table = nhi_ids,
  785. .probe = nhi_probe,
  786. .remove = nhi_remove,
  787. .driver.pm = &nhi_pm_ops,
  788. };
  789. static int __init nhi_init(void)
  790. {
  791. int ret;
  792. ret = tb_domain_init();
  793. if (ret)
  794. return ret;
  795. ret = pci_register_driver(&nhi_driver);
  796. if (ret)
  797. tb_domain_exit();
  798. return ret;
  799. }
  800. static void __exit nhi_unload(void)
  801. {
  802. pci_unregister_driver(&nhi_driver);
  803. tb_domain_exit();
  804. }
  805. module_init(nhi_init);
  806. module_exit(nhi_unload);