inet: frag: enforce memory limits earlier
[linux/fpc-iii.git] / drivers / thunderbolt / nhi.c
blobcba6bc6ab9ed7624a404e577d2e8dde8059fa8ab
1 /*
2 * Thunderbolt Cactus Ridge driver - NHI driver
4 * The NHI (native host interface) is the pci device that allows us to send and
5 * receive frames from the thunderbolt bus.
7 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
8 */
10 #include <linux/pm_runtime.h>
11 #include <linux/slab.h>
12 #include <linux/errno.h>
13 #include <linux/pci.h>
14 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/dmi.h>
18 #include "nhi.h"
19 #include "nhi_regs.h"
20 #include "tb.h"
22 #define RING_TYPE(ring) ((ring)->is_tx ? "TX ring" : "RX ring")
25 static int ring_interrupt_index(struct tb_ring *ring)
27 int bit = ring->hop;
28 if (!ring->is_tx)
29 bit += ring->nhi->hop_count;
30 return bit;
33 /**
34 * ring_interrupt_active() - activate/deactivate interrupts for a single ring
36 * ring->nhi->lock must be held.
38 static void ring_interrupt_active(struct tb_ring *ring, bool active)
40 int reg = REG_RING_INTERRUPT_BASE +
41 ring_interrupt_index(ring) / 32 * 4;
42 int bit = ring_interrupt_index(ring) & 31;
43 int mask = 1 << bit;
44 u32 old, new;
45 old = ioread32(ring->nhi->iobase + reg);
46 if (active)
47 new = old | mask;
48 else
49 new = old & ~mask;
51 dev_info(&ring->nhi->pdev->dev,
52 "%s interrupt at register %#x bit %d (%#x -> %#x)\n",
53 active ? "enabling" : "disabling", reg, bit, old, new);
55 if (new == old)
56 dev_WARN(&ring->nhi->pdev->dev,
57 "interrupt for %s %d is already %s\n",
58 RING_TYPE(ring), ring->hop,
59 active ? "enabled" : "disabled");
60 iowrite32(new, ring->nhi->iobase + reg);
63 /**
64 * nhi_disable_interrupts() - disable interrupts for all rings
66 * Use only during init and shutdown.
68 static void nhi_disable_interrupts(struct tb_nhi *nhi)
70 int i = 0;
71 /* disable interrupts */
72 for (i = 0; i < RING_INTERRUPT_REG_COUNT(nhi); i++)
73 iowrite32(0, nhi->iobase + REG_RING_INTERRUPT_BASE + 4 * i);
75 /* clear interrupt status bits */
76 for (i = 0; i < RING_NOTIFY_REG_COUNT(nhi); i++)
77 ioread32(nhi->iobase + REG_RING_NOTIFY_BASE + 4 * i);
80 /* ring helper methods */
82 static void __iomem *ring_desc_base(struct tb_ring *ring)
84 void __iomem *io = ring->nhi->iobase;
85 io += ring->is_tx ? REG_TX_RING_BASE : REG_RX_RING_BASE;
86 io += ring->hop * 16;
87 return io;
90 static void __iomem *ring_options_base(struct tb_ring *ring)
92 void __iomem *io = ring->nhi->iobase;
93 io += ring->is_tx ? REG_TX_OPTIONS_BASE : REG_RX_OPTIONS_BASE;
94 io += ring->hop * 32;
95 return io;
98 static void ring_iowrite16desc(struct tb_ring *ring, u32 value, u32 offset)
100 iowrite16(value, ring_desc_base(ring) + offset);
103 static void ring_iowrite32desc(struct tb_ring *ring, u32 value, u32 offset)
105 iowrite32(value, ring_desc_base(ring) + offset);
108 static void ring_iowrite64desc(struct tb_ring *ring, u64 value, u32 offset)
110 iowrite32(value, ring_desc_base(ring) + offset);
111 iowrite32(value >> 32, ring_desc_base(ring) + offset + 4);
114 static void ring_iowrite32options(struct tb_ring *ring, u32 value, u32 offset)
116 iowrite32(value, ring_options_base(ring) + offset);
119 static bool ring_full(struct tb_ring *ring)
121 return ((ring->head + 1) % ring->size) == ring->tail;
124 static bool ring_empty(struct tb_ring *ring)
126 return ring->head == ring->tail;
130 * ring_write_descriptors() - post frames from ring->queue to the controller
132 * ring->lock is held.
134 static void ring_write_descriptors(struct tb_ring *ring)
136 struct ring_frame *frame, *n;
137 struct ring_desc *descriptor;
138 list_for_each_entry_safe(frame, n, &ring->queue, list) {
139 if (ring_full(ring))
140 break;
141 list_move_tail(&frame->list, &ring->in_flight);
142 descriptor = &ring->descriptors[ring->head];
143 descriptor->phys = frame->buffer_phy;
144 descriptor->time = 0;
145 descriptor->flags = RING_DESC_POSTED | RING_DESC_INTERRUPT;
146 if (ring->is_tx) {
147 descriptor->length = frame->size;
148 descriptor->eof = frame->eof;
149 descriptor->sof = frame->sof;
151 ring->head = (ring->head + 1) % ring->size;
152 ring_iowrite16desc(ring, ring->head, ring->is_tx ? 10 : 8);
157 * ring_work() - progress completed frames
159 * If the ring is shutting down then all frames are marked as canceled and
160 * their callbacks are invoked.
162 * Otherwise we collect all completed frame from the ring buffer, write new
163 * frame to the ring buffer and invoke the callbacks for the completed frames.
165 static void ring_work(struct work_struct *work)
167 struct tb_ring *ring = container_of(work, typeof(*ring), work);
168 struct ring_frame *frame;
169 bool canceled = false;
170 LIST_HEAD(done);
171 mutex_lock(&ring->lock);
173 if (!ring->running) {
174 /* Move all frames to done and mark them as canceled. */
175 list_splice_tail_init(&ring->in_flight, &done);
176 list_splice_tail_init(&ring->queue, &done);
177 canceled = true;
178 goto invoke_callback;
181 while (!ring_empty(ring)) {
182 if (!(ring->descriptors[ring->tail].flags
183 & RING_DESC_COMPLETED))
184 break;
185 frame = list_first_entry(&ring->in_flight, typeof(*frame),
186 list);
187 list_move_tail(&frame->list, &done);
188 if (!ring->is_tx) {
189 frame->size = ring->descriptors[ring->tail].length;
190 frame->eof = ring->descriptors[ring->tail].eof;
191 frame->sof = ring->descriptors[ring->tail].sof;
192 frame->flags = ring->descriptors[ring->tail].flags;
193 if (frame->sof != 0)
194 dev_WARN(&ring->nhi->pdev->dev,
195 "%s %d got unexpected SOF: %#x\n",
196 RING_TYPE(ring), ring->hop,
197 frame->sof);
199 * known flags:
200 * raw not enabled, interupt not set: 0x2=0010
201 * raw enabled: 0xa=1010
202 * raw not enabled: 0xb=1011
203 * partial frame (>MAX_FRAME_SIZE): 0xe=1110
205 if (frame->flags != 0xa)
206 dev_WARN(&ring->nhi->pdev->dev,
207 "%s %d got unexpected flags: %#x\n",
208 RING_TYPE(ring), ring->hop,
209 frame->flags);
211 ring->tail = (ring->tail + 1) % ring->size;
213 ring_write_descriptors(ring);
215 invoke_callback:
216 mutex_unlock(&ring->lock); /* allow callbacks to schedule new work */
217 while (!list_empty(&done)) {
218 frame = list_first_entry(&done, typeof(*frame), list);
220 * The callback may reenqueue or delete frame.
221 * Do not hold on to it.
223 list_del_init(&frame->list);
224 frame->callback(ring, frame, canceled);
228 int __ring_enqueue(struct tb_ring *ring, struct ring_frame *frame)
230 int ret = 0;
231 mutex_lock(&ring->lock);
232 if (ring->running) {
233 list_add_tail(&frame->list, &ring->queue);
234 ring_write_descriptors(ring);
235 } else {
236 ret = -ESHUTDOWN;
238 mutex_unlock(&ring->lock);
239 return ret;
242 static struct tb_ring *ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
243 bool transmit)
245 struct tb_ring *ring = NULL;
246 dev_info(&nhi->pdev->dev, "allocating %s ring %d of size %d\n",
247 transmit ? "TX" : "RX", hop, size);
249 mutex_lock(&nhi->lock);
250 if (hop >= nhi->hop_count) {
251 dev_WARN(&nhi->pdev->dev, "invalid hop: %d\n", hop);
252 goto err;
254 if (transmit && nhi->tx_rings[hop]) {
255 dev_WARN(&nhi->pdev->dev, "TX hop %d already allocated\n", hop);
256 goto err;
257 } else if (!transmit && nhi->rx_rings[hop]) {
258 dev_WARN(&nhi->pdev->dev, "RX hop %d already allocated\n", hop);
259 goto err;
261 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
262 if (!ring)
263 goto err;
265 mutex_init(&ring->lock);
266 INIT_LIST_HEAD(&ring->queue);
267 INIT_LIST_HEAD(&ring->in_flight);
268 INIT_WORK(&ring->work, ring_work);
270 ring->nhi = nhi;
271 ring->hop = hop;
272 ring->is_tx = transmit;
273 ring->size = size;
274 ring->head = 0;
275 ring->tail = 0;
276 ring->running = false;
277 ring->descriptors = dma_alloc_coherent(&ring->nhi->pdev->dev,
278 size * sizeof(*ring->descriptors),
279 &ring->descriptors_dma, GFP_KERNEL | __GFP_ZERO);
280 if (!ring->descriptors)
281 goto err;
283 if (transmit)
284 nhi->tx_rings[hop] = ring;
285 else
286 nhi->rx_rings[hop] = ring;
287 mutex_unlock(&nhi->lock);
288 return ring;
290 err:
291 if (ring)
292 mutex_destroy(&ring->lock);
293 kfree(ring);
294 mutex_unlock(&nhi->lock);
295 return NULL;
298 struct tb_ring *ring_alloc_tx(struct tb_nhi *nhi, int hop, int size)
300 return ring_alloc(nhi, hop, size, true);
303 struct tb_ring *ring_alloc_rx(struct tb_nhi *nhi, int hop, int size)
305 return ring_alloc(nhi, hop, size, false);
309 * ring_start() - enable a ring
311 * Must not be invoked in parallel with ring_stop().
313 void ring_start(struct tb_ring *ring)
315 mutex_lock(&ring->nhi->lock);
316 mutex_lock(&ring->lock);
317 if (ring->running) {
318 dev_WARN(&ring->nhi->pdev->dev, "ring already started\n");
319 goto err;
321 dev_info(&ring->nhi->pdev->dev, "starting %s %d\n",
322 RING_TYPE(ring), ring->hop);
324 ring_iowrite64desc(ring, ring->descriptors_dma, 0);
325 if (ring->is_tx) {
326 ring_iowrite32desc(ring, ring->size, 12);
327 ring_iowrite32options(ring, 0, 4); /* time releated ? */
328 ring_iowrite32options(ring,
329 RING_FLAG_ENABLE | RING_FLAG_RAW, 0);
330 } else {
331 ring_iowrite32desc(ring,
332 (TB_FRAME_SIZE << 16) | ring->size, 12);
333 ring_iowrite32options(ring, 0xffffffff, 4); /* SOF EOF mask */
334 ring_iowrite32options(ring,
335 RING_FLAG_ENABLE | RING_FLAG_RAW, 0);
337 ring_interrupt_active(ring, true);
338 ring->running = true;
339 err:
340 mutex_unlock(&ring->lock);
341 mutex_unlock(&ring->nhi->lock);
346 * ring_stop() - shutdown a ring
348 * Must not be invoked from a callback.
350 * This method will disable the ring. Further calls to ring_tx/ring_rx will
351 * return -ESHUTDOWN until ring_stop has been called.
353 * All enqueued frames will be canceled and their callbacks will be executed
354 * with frame->canceled set to true (on the callback thread). This method
355 * returns only after all callback invocations have finished.
357 void ring_stop(struct tb_ring *ring)
359 mutex_lock(&ring->nhi->lock);
360 mutex_lock(&ring->lock);
361 dev_info(&ring->nhi->pdev->dev, "stopping %s %d\n",
362 RING_TYPE(ring), ring->hop);
363 if (!ring->running) {
364 dev_WARN(&ring->nhi->pdev->dev, "%s %d already stopped\n",
365 RING_TYPE(ring), ring->hop);
366 goto err;
368 ring_interrupt_active(ring, false);
370 ring_iowrite32options(ring, 0, 0);
371 ring_iowrite64desc(ring, 0, 0);
372 ring_iowrite16desc(ring, 0, ring->is_tx ? 10 : 8);
373 ring_iowrite32desc(ring, 0, 12);
374 ring->head = 0;
375 ring->tail = 0;
376 ring->running = false;
378 err:
379 mutex_unlock(&ring->lock);
380 mutex_unlock(&ring->nhi->lock);
383 * schedule ring->work to invoke callbacks on all remaining frames.
385 schedule_work(&ring->work);
386 flush_work(&ring->work);
390 * ring_free() - free ring
392 * When this method returns all invocations of ring->callback will have
393 * finished.
395 * Ring must be stopped.
397 * Must NOT be called from ring_frame->callback!
399 void ring_free(struct tb_ring *ring)
401 mutex_lock(&ring->nhi->lock);
403 * Dissociate the ring from the NHI. This also ensures that
404 * nhi_interrupt_work cannot reschedule ring->work.
406 if (ring->is_tx)
407 ring->nhi->tx_rings[ring->hop] = NULL;
408 else
409 ring->nhi->rx_rings[ring->hop] = NULL;
411 if (ring->running) {
412 dev_WARN(&ring->nhi->pdev->dev, "%s %d still running\n",
413 RING_TYPE(ring), ring->hop);
416 dma_free_coherent(&ring->nhi->pdev->dev,
417 ring->size * sizeof(*ring->descriptors),
418 ring->descriptors, ring->descriptors_dma);
420 ring->descriptors = NULL;
421 ring->descriptors_dma = 0;
424 dev_info(&ring->nhi->pdev->dev,
425 "freeing %s %d\n",
426 RING_TYPE(ring),
427 ring->hop);
429 mutex_unlock(&ring->nhi->lock);
431 * ring->work can no longer be scheduled (it is scheduled only by
432 * nhi_interrupt_work and ring_stop). Wait for it to finish before
433 * freeing the ring.
435 flush_work(&ring->work);
436 mutex_destroy(&ring->lock);
437 kfree(ring);
440 static void nhi_interrupt_work(struct work_struct *work)
442 struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work);
443 int value = 0; /* Suppress uninitialized usage warning. */
444 int bit;
445 int hop = -1;
446 int type = 0; /* current interrupt type 0: TX, 1: RX, 2: RX overflow */
447 struct tb_ring *ring;
449 mutex_lock(&nhi->lock);
452 * Starting at REG_RING_NOTIFY_BASE there are three status bitfields
453 * (TX, RX, RX overflow). We iterate over the bits and read a new
454 * dwords as required. The registers are cleared on read.
456 for (bit = 0; bit < 3 * nhi->hop_count; bit++) {
457 if (bit % 32 == 0)
458 value = ioread32(nhi->iobase
459 + REG_RING_NOTIFY_BASE
460 + 4 * (bit / 32));
461 if (++hop == nhi->hop_count) {
462 hop = 0;
463 type++;
465 if ((value & (1 << (bit % 32))) == 0)
466 continue;
467 if (type == 2) {
468 dev_warn(&nhi->pdev->dev,
469 "RX overflow for ring %d\n",
470 hop);
471 continue;
473 if (type == 0)
474 ring = nhi->tx_rings[hop];
475 else
476 ring = nhi->rx_rings[hop];
477 if (ring == NULL) {
478 dev_warn(&nhi->pdev->dev,
479 "got interrupt for inactive %s ring %d\n",
480 type ? "RX" : "TX",
481 hop);
482 continue;
484 /* we do not check ring->running, this is done in ring->work */
485 schedule_work(&ring->work);
487 mutex_unlock(&nhi->lock);
490 static irqreturn_t nhi_msi(int irq, void *data)
492 struct tb_nhi *nhi = data;
493 schedule_work(&nhi->interrupt_work);
494 return IRQ_HANDLED;
497 static int nhi_suspend_noirq(struct device *dev)
499 struct pci_dev *pdev = to_pci_dev(dev);
500 struct tb *tb = pci_get_drvdata(pdev);
501 thunderbolt_suspend(tb);
502 return 0;
505 static int nhi_resume_noirq(struct device *dev)
507 struct pci_dev *pdev = to_pci_dev(dev);
508 struct tb *tb = pci_get_drvdata(pdev);
509 thunderbolt_resume(tb);
510 return 0;
513 static void nhi_shutdown(struct tb_nhi *nhi)
515 int i;
516 dev_info(&nhi->pdev->dev, "shutdown\n");
518 for (i = 0; i < nhi->hop_count; i++) {
519 if (nhi->tx_rings[i])
520 dev_WARN(&nhi->pdev->dev,
521 "TX ring %d is still active\n", i);
522 if (nhi->rx_rings[i])
523 dev_WARN(&nhi->pdev->dev,
524 "RX ring %d is still active\n", i);
526 nhi_disable_interrupts(nhi);
528 * We have to release the irq before calling flush_work. Otherwise an
529 * already executing IRQ handler could call schedule_work again.
531 devm_free_irq(&nhi->pdev->dev, nhi->pdev->irq, nhi);
532 flush_work(&nhi->interrupt_work);
533 mutex_destroy(&nhi->lock);
536 static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
538 struct tb_nhi *nhi;
539 struct tb *tb;
540 int res;
542 res = pcim_enable_device(pdev);
543 if (res) {
544 dev_err(&pdev->dev, "cannot enable PCI device, aborting\n");
545 return res;
548 res = pci_enable_msi(pdev);
549 if (res) {
550 dev_err(&pdev->dev, "cannot enable MSI, aborting\n");
551 return res;
554 res = pcim_iomap_regions(pdev, 1 << 0, "thunderbolt");
555 if (res) {
556 dev_err(&pdev->dev, "cannot obtain PCI resources, aborting\n");
557 return res;
560 nhi = devm_kzalloc(&pdev->dev, sizeof(*nhi), GFP_KERNEL);
561 if (!nhi)
562 return -ENOMEM;
564 nhi->pdev = pdev;
565 /* cannot fail - table is allocated bin pcim_iomap_regions */
566 nhi->iobase = pcim_iomap_table(pdev)[0];
567 nhi->hop_count = ioread32(nhi->iobase + REG_HOP_COUNT) & 0x3ff;
568 if (nhi->hop_count != 12 && nhi->hop_count != 32)
569 dev_warn(&pdev->dev, "unexpected hop count: %d\n",
570 nhi->hop_count);
571 INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
573 nhi->tx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
574 sizeof(*nhi->tx_rings), GFP_KERNEL);
575 nhi->rx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
576 sizeof(*nhi->rx_rings), GFP_KERNEL);
577 if (!nhi->tx_rings || !nhi->rx_rings)
578 return -ENOMEM;
580 nhi_disable_interrupts(nhi); /* In case someone left them on. */
581 res = devm_request_irq(&pdev->dev, pdev->irq, nhi_msi,
582 IRQF_NO_SUSPEND, /* must work during _noirq */
583 "thunderbolt", nhi);
584 if (res) {
585 dev_err(&pdev->dev, "request_irq failed, aborting\n");
586 return res;
589 mutex_init(&nhi->lock);
591 pci_set_master(pdev);
593 /* magic value - clock related? */
594 iowrite32(3906250 / 10000, nhi->iobase + 0x38c00);
596 dev_info(&nhi->pdev->dev, "NHI initialized, starting thunderbolt\n");
597 tb = thunderbolt_alloc_and_start(nhi);
598 if (!tb) {
600 * At this point the RX/TX rings might already have been
601 * activated. Do a proper shutdown.
603 nhi_shutdown(nhi);
604 return -EIO;
606 pci_set_drvdata(pdev, tb);
608 return 0;
611 static void nhi_remove(struct pci_dev *pdev)
613 struct tb *tb = pci_get_drvdata(pdev);
614 struct tb_nhi *nhi = tb->nhi;
615 thunderbolt_shutdown_and_free(tb);
616 nhi_shutdown(nhi);
620 * The tunneled pci bridges are siblings of us. Use resume_noirq to reenable
621 * the tunnels asap. A corresponding pci quirk blocks the downstream bridges
622 * resume_noirq until we are done.
624 static const struct dev_pm_ops nhi_pm_ops = {
625 .suspend_noirq = nhi_suspend_noirq,
626 .resume_noirq = nhi_resume_noirq,
627 .freeze_noirq = nhi_suspend_noirq, /*
628 * we just disable hotplug, the
629 * pci-tunnels stay alive.
631 .thaw_noirq = nhi_resume_noirq,
632 .restore_noirq = nhi_resume_noirq,
635 static struct pci_device_id nhi_ids[] = {
637 * We have to specify class, the TB bridges use the same device and
638 * vendor (sub)id on gen 1 and gen 2 controllers.
641 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
642 .vendor = PCI_VENDOR_ID_INTEL,
643 .device = PCI_DEVICE_ID_INTEL_LIGHT_RIDGE,
644 .subvendor = 0x2222, .subdevice = 0x1111,
647 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
648 .vendor = PCI_VENDOR_ID_INTEL,
649 .device = PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C,
650 .subvendor = 0x2222, .subdevice = 0x1111,
653 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
654 .vendor = PCI_VENDOR_ID_INTEL,
655 .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI,
656 .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
659 .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
660 .vendor = PCI_VENDOR_ID_INTEL,
661 .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI,
662 .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
664 { 0,}
667 MODULE_DEVICE_TABLE(pci, nhi_ids);
668 MODULE_LICENSE("GPL");
670 static struct pci_driver nhi_driver = {
671 .name = "thunderbolt",
672 .id_table = nhi_ids,
673 .probe = nhi_probe,
674 .remove = nhi_remove,
675 .driver.pm = &nhi_pm_ops,
678 static int __init nhi_init(void)
680 if (!dmi_match(DMI_BOARD_VENDOR, "Apple Inc."))
681 return -ENOSYS;
682 return pci_register_driver(&nhi_driver);
685 static void __exit nhi_unload(void)
687 pci_unregister_driver(&nhi_driver);
690 module_init(nhi_init);
691 module_exit(nhi_unload);