nhi.c 30 KB

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