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