summaryrefslogtreecommitdiff
path: root/sys/dev/thunderbolt/router.c
blob: a3b418d77faca1a96a8e2219eb525db4f18e0f89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2022 Scott Long
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "opt_thunderbolt.h"

/* Config space access for switches, ports, and devices in TB3 and USB4 */
#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/malloc.h>
#include <sys/queue.h>
#include <sys/sysctl.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/taskqueue.h>
#include <sys/gsb_crc32.h>
#include <sys/endian.h>
#include <vm/vm.h>
#include <vm/pmap.h>

#include <machine/bus.h>
#include <machine/stdarg.h>

#include <dev/thunderbolt/nhi_reg.h>
#include <dev/thunderbolt/nhi_var.h>
#include <dev/thunderbolt/tb_reg.h>
#include <dev/thunderbolt/tb_var.h>
#include <dev/thunderbolt/tbcfg_reg.h>
#include <dev/thunderbolt/router_var.h>
#include <dev/thunderbolt/tb_debug.h>

static int router_alloc_cmd(struct router_softc *, struct router_command **);
static void router_free_cmd(struct router_softc *, struct router_command *);
static int _tb_router_attach(struct router_softc *);
static void router_prepare_read(struct router_softc *, struct router_command *,
    int);
static int _tb_config_read(struct router_softc *, u_int, u_int, u_int, u_int,
    uint32_t *, void *, struct router_command **);
static int router_schedule(struct router_softc *, struct router_command *);
static int router_schedule_locked(struct router_softc *,
    struct router_command *);
static nhi_ring_cb_t router_complete_intr;
static nhi_ring_cb_t router_response_intr;
static nhi_ring_cb_t router_notify_intr;

#define CFG_DEFAULT_RETRIES	3
#define CFG_DEFAULT_TIMEOUT	2

static int
router_lookup_device(struct router_softc *sc, tb_route_t route,
    struct router_softc **dev)
{
	struct router_softc *cursor;
	uint64_t search_rt, remainder_rt, this_rt;
	uint8_t hop;

	KASSERT(dev != NULL, ("dev cannot be NULL\n"));

	cursor = tb_config_get_root(sc);
	remainder_rt = search_rt = route.lo | ((uint64_t)route.hi << 32);
	tb_debug(sc, DBG_ROUTER|DBG_EXTRA,
	    "%s: Searching for router 0x%016jx\n", __func__, search_rt);

	while (cursor != NULL) {
		this_rt = TB_ROUTE(cursor);
		tb_debug(sc, DBG_ROUTER|DBG_EXTRA,
		    "Comparing cursor route 0x%016jx\n", this_rt);
		if (this_rt == search_rt)
			break;

		/* Prepare to go to the next hop node in the route */
		hop = remainder_rt & 0xff;
		remainder_rt >>= 8;
		tb_debug(sc, DBG_ROUTER|DBG_EXTRA,
		    "hop= 0x%02x, remainder= 0x%016jx\n", hop, remainder_rt);

		/*
		 * An adapter index of 0x0 is only for the host interface
		 * adapter on the root route.  The only time that
		 * it's valid for searches is when you're looking for the
		 * root route, and that case has already been handled.
		 */
		if (hop == 0) {
			tb_debug(sc, DBG_ROUTER,
			    "End of route chain, route not found\n");
			return (ENOENT);
		}

		if (hop > cursor->max_adap) {
			tb_debug(sc, DBG_ROUTER,
			    "Route hop out of range for parent\n");
			return (EINVAL);
		}

		if (cursor->adapters == NULL) {
			tb_debug(sc, DBG_ROUTER,
			    "Error, router not fully initialized\n");
			return (EINVAL);
		}

		cursor = cursor->adapters[hop];
	}

	if (cursor == NULL)
		return (ENOENT);

	*dev = cursor;
	return (0);
}

static int
router_insert(struct router_softc *sc, struct router_softc *parent)
{
	uint64_t this_rt;
	uint8_t this_hop;

	tb_debug(sc, DBG_ROUTER|DBG_EXTRA, "router_insert called\n");

	if (parent == NULL) {
		tb_debug(sc, DBG_ROUTER, "Parent cannot be NULL in insert\n");
		return (EINVAL);
	}

	this_rt = TB_ROUTE(sc);
	if (((this_rt >> (sc->depth * 8)) > 0xffULL) ||
	    (parent->depth + 1 != sc->depth)) {
		tb_debug(sc, DBG_ROUTER, "Added route 0x%08x%08x is not a "
		    "direct child of the parent route 0x%08x%08x\n",
		    sc->route.hi, sc->route.lo, parent->route.hi,
		    parent->route.lo);
		return (EINVAL);
	}

	this_hop = (uint8_t)(this_rt >> (sc->depth * 8));

	tb_debug(sc, DBG_ROUTER, "Inserting route 0x%08x%08x with last hop "
	    "of 0x%02x and depth of %d\n", sc->route.hi, sc->route.lo,
	    this_hop, sc->depth);

	if (this_hop > parent->max_adap) {
		tb_debug(sc, DBG_ROUTER|DBG_EXTRA,
		    "Inserted route is out of range of the parent\n");
		return (EINVAL);
	}

	if (parent->adapters[this_hop] != NULL) {
		tb_debug(sc, DBG_ROUTER|DBG_EXTRA,
		    "Inserted route already exists\n");
		return (EEXIST);
	}

	parent->adapters[this_hop] = sc;

	tb_debug(sc, DBG_ROUTER, "Added router 0x%08x%08x to parent "
	    "0x%08x%08x\n", sc->route.hi, sc->route.lo, parent->route.hi,
	    parent->route.lo);
	return (0);
}

static int
router_register_interrupts(struct router_softc *sc)
{
	struct nhi_dispatch tx[] = { { PDF_READ, router_complete_intr, sc },
				     { PDF_WRITE, router_complete_intr, sc },
				     { 0, NULL, NULL } };
	struct nhi_dispatch rx[] = { { PDF_READ, router_response_intr, sc },
				     { PDF_WRITE, router_response_intr, sc },
				     { PDF_NOTIFY, router_notify_intr, sc },
				     { 0, NULL, NULL } };

	return (nhi_register_pdf(sc->ring0, tx, rx));
}

int
tb_router_attach(struct router_softc *parent, tb_route_t route)
{
	struct router_softc *sc;

	tb_debug(parent, DBG_ROUTER|DBG_EXTRA, "tb_router_attach called\n");

	sc = malloc(sizeof(*sc), M_THUNDERBOLT, M_ZERO|M_NOWAIT);
	if (sc == NULL) {
		tb_debug(parent, DBG_ROUTER, "Cannot allocate root router\n");
		return (ENOMEM);
	}

	sc->dev = parent->dev;
	sc->debug = parent->debug;
	sc->ring0 = parent->ring0;
	sc->route = route;
	sc->nsc = parent->nsc;

	mtx_init(&sc->mtx, "tbcfg", "Thunderbolt Router Config", MTX_DEF);
	TAILQ_INIT(&sc->cmd_queue);

	router_insert(sc, parent);

	return (_tb_router_attach(sc));
}

int
tb_router_attach_root(struct nhi_softc *nsc, tb_route_t route)
{
	struct router_softc *sc;
	int error;

	tb_debug(nsc, DBG_ROUTER|DBG_EXTRA, "tb_router_attach_root called\n");

	sc = malloc(sizeof(*sc), M_THUNDERBOLT, M_ZERO|M_NOWAIT);
	if (sc == NULL) {
		tb_debug(nsc, DBG_ROUTER, "Cannot allocate root router\n");
		return (ENOMEM);
	}

	sc->dev = nsc->dev;
	sc->debug = nsc->debug;
	sc->ring0 = nsc->ring0;
	sc->route = route;
	sc->nsc = nsc;

	mtx_init(&sc->mtx, "tbcfg", "Thunderbolt Router Config", MTX_DEF);
	TAILQ_INIT(&sc->cmd_queue);

	/*
	 * This router is semi-virtual and represents the router that's part
	 * of the NHI DMA engine.  Commands can't be issued to the topology
	 * until the NHI is initialized and this router is initialized, so
	 * there's no point in registering router interrupts earlier than this,
	 * even if other routers are found first.
	 */
	tb_config_set_root(sc);
	error = router_register_interrupts(sc);
	if (error) {
		tb_router_detach(sc);
		return (error);
	}

	error = _tb_router_attach(sc);
	if (error)
		return (error);

	bcopy((uint8_t *)sc->uuid, nsc->uuid, 16);
	return (0);
}

static int
_tb_router_attach(struct router_softc *sc)
{
	struct tb_cfg_router *cfg;
	uint32_t *buf;
	int error, up;

	buf = malloc(9 * 4, M_THUNDERBOLT, M_NOWAIT|M_ZERO);
	if (buf == NULL)
		return (ENOMEM);

	error = tb_config_router_read_polled(sc, 0, 9, buf);
	if (error != 0) {
		free(buf, M_THUNDERBOLT);
		return (error);
	}

	cfg = (struct tb_cfg_router *)buf;
	up = GET_ROUTER_CS_UPSTREAM_ADAP(cfg);
	sc->max_adap = GET_ROUTER_CS_MAX_ADAP(cfg);
	sc->depth = GET_ROUTER_CS_DEPTH(cfg);
	sc->uuid[0] = cfg->uuid_lo;
	sc->uuid[1] = cfg->uuid_hi;
	sc->uuid[2] = 0xffffffff;
	sc->uuid[3] = 0xffffffff;
	tb_debug(sc, DBG_ROUTER, "Router upstream_port= %d, max_port= %d, "
	    "depth= %d\n", up, sc->max_adap, sc->depth);
	free(buf, M_THUNDERBOLT);

	/* Downstream adapters are indexed in the array allocated here. */
	sc->max_adap = MIN(sc->max_adap, ROUTER_CS1_MAX_ADAPTERS);
	sc->adapters = malloc((1 + sc->max_adap) * sizeof(void *),
	    M_THUNDERBOLT, M_NOWAIT|M_ZERO);
	if (sc->adapters == NULL) {
		tb_debug(sc, DBG_ROUTER,
		    "Cannot allocate downstream adapter memory\n");
		return (ENOMEM);
	}

	tb_debug(sc, DBG_ROUTER, "Router created, route 0x%08x%08x\n",
	    sc->route.hi, sc->route.lo);

	return (0);
}

int
tb_router_detach(struct router_softc *sc)
{

	tb_debug(sc, DBG_ROUTER|DBG_EXTRA, "tb_router_deattach called\n");

	if (TAILQ_FIRST(&sc->cmd_queue) != NULL)
		return (EBUSY);

	mtx_destroy(&sc->mtx);

	if (sc->adapters != NULL)
		free(sc->adapters, M_THUNDERBOLT);

	if (sc != NULL)
		free(sc, M_THUNDERBOLT);

	return (0);
}

static void
router_get_config_cb(struct router_softc *sc, struct router_command *cmd,
    void *arg)
{
	tb_debug(sc, DBG_ROUTER|DBG_EXTRA, "router_get_config_cb called\n");

	/*
	 * Only do the copy if the command didn't have a notify event thrown.
	 * These events serve as asynchronous exception signals, which is
	 * cumbersome.
	 */
	if (cmd->ev == 0)
		bcopy((uint8_t *)cmd->resp_buffer,
		    (uint8_t *)cmd->callback_arg, cmd->dwlen * 4);

	mtx_lock(&sc->mtx);
	sc->inflight_cmd = NULL;

	if ((cmd->flags & RCMD_POLLED) == 0)
		wakeup(cmd);
	else
		cmd->flags |= RCMD_POLL_COMPLETE;

	router_schedule_locked(sc, NULL);
	mtx_unlock(&sc->mtx);
}

int
tb_config_read(struct router_softc *sc, u_int space, u_int adapter,
    u_int offset, u_int dwlen, uint32_t *buf)
{
	struct router_command *cmd;
	int error, retries;

	if ((error = _tb_config_read(sc, space, adapter, offset, dwlen, buf,
	    router_get_config_cb, &cmd)) != 0)
		return (error);

	retries = cmd->retries;
	mtx_lock(&sc->mtx);
	while (retries-- >= 0) {
		error = router_schedule_locked(sc, cmd);
		if (error)
			break;

		error = msleep(cmd, &sc->mtx, 0, "tbtcfg", cmd->timeout * hz);
		if (error != EWOULDBLOCK)
			break;
		sc->inflight_cmd = NULL;
		tb_debug(sc, DBG_ROUTER, "Config command timed out, retries=%d\n", retries);
	}

	if (cmd->ev != 0)
		error = EINVAL;
	router_free_cmd(sc, cmd);
	mtx_unlock(&sc->mtx);
	return (error);
}

int
tb_config_read_polled(struct router_softc *sc, u_int space, u_int adapter,
    u_int offset, u_int dwlen, uint32_t *buf)
{
	struct router_command *cmd;
	int error, retries, timeout;

	if ((error = _tb_config_read(sc, space, adapter, offset, dwlen, buf,
	    router_get_config_cb, &cmd)) != 0)
		return (error);

	retries = cmd->retries;
	cmd->flags |= RCMD_POLLED;
	timeout = cmd->timeout * 1000000;

	mtx_lock(&sc->mtx);
	while (retries-- >= 0) {
		error = router_schedule_locked(sc, cmd);
		if (error)
			break;
		mtx_unlock(&sc->mtx);

		while (timeout > 0) {
			DELAY(100 * 1000);
			if ((cmd->flags & RCMD_POLL_COMPLETE) != 0)
				break;
			timeout -= 100000;
		}

		mtx_lock(&sc->mtx);
		if ((cmd->flags & RCMD_POLL_COMPLETE) == 0) {
			error = ETIMEDOUT;
			sc->inflight_cmd = NULL;
			tb_debug(sc, DBG_ROUTER, "Config command timed out, retries=%d\n", retries);
			continue;
		} else
			break;
	}

	if (cmd->ev != 0)
		error = EINVAL;
	router_free_cmd(sc, cmd);
	mtx_unlock(&sc->mtx);
	return (error);
}

int
tb_config_read_async(struct router_softc *sc, u_int space, u_int adapter,
    u_int offset, u_int dwlen, uint32_t *buf, void *cb)
{
	struct router_command *cmd;
	int error;

	if ((error = _tb_config_read(sc, space, adapter, offset, dwlen, buf,
	    cb, &cmd)) != 0)
		return (error);

	error = router_schedule(sc, cmd);

	return (error);
}

static int
_tb_config_read(struct router_softc *sc, u_int space, u_int adapter,
    u_int offset, u_int dwlen, uint32_t *buf, void *cb,
    struct router_command **rcmd)
{
	struct router_command *cmd;
	struct tb_cfg_read *msg;
	int error;

	if ((error = router_alloc_cmd(sc, &cmd)) != 0)
		return (error);

	msg = router_get_frame_data(cmd);
	bzero(msg, sizeof(*msg));
	msg->route.hi = sc->route.hi;
	msg->route.lo = sc->route.lo;
	msg->addr_attrs = TB_CONFIG_ADDR(0, space, adapter, dwlen, offset);
	cmd->callback = cb;
	cmd->callback_arg = buf;
	cmd->dwlen = dwlen;
	router_prepare_read(sc, cmd, sizeof(*msg));

	if (rcmd != NULL)
		*rcmd = cmd;

	return (0);
}

int
tb_config_write(struct router_softc *sc, u_int space, u_int adapter,
    u_int offset, u_int dwlen, uint32_t *buf)
{

	return(0);
}

static int
router_alloc_cmd(struct router_softc *sc, struct router_command **rcmd)
{
	struct router_command *cmd;

	tb_debug(sc, DBG_ROUTER|DBG_EXTRA, "router_alloc_cmd\n");

	cmd = malloc(sizeof(*cmd), M_THUNDERBOLT, M_ZERO|M_NOWAIT);
	if (cmd == NULL) {
		tb_debug(sc, DBG_ROUTER, "Cannot allocate cmd/response\n");
		return (ENOMEM);
	}

	cmd->nhicmd = nhi_alloc_tx_frame(sc->ring0);
	if (cmd->nhicmd == NULL) {
		tb_debug(sc, DBG_ROUTER, "Cannot allocate command frame\n");
		free(cmd, M_THUNDERBOLT);
		return (EBUSY);
	}

	cmd->sc = sc;
	*rcmd = cmd;
	tb_debug(sc, DBG_ROUTER|DBG_EXTRA, "Allocated command with index %d\n",
	    cmd->nhicmd->idx);

	return (0);
}

static void
router_free_cmd(struct router_softc *sc, struct router_command *cmd)
{

	tb_debug(sc, DBG_ROUTER|DBG_EXTRA, "router_free_cmd\n");

	if (cmd == NULL)
		return;

	if (cmd->nhicmd != NULL) {
		tb_debug(sc, DBG_ROUTER|DBG_EXTRA, "Freeing nhi command %d\n",
		    cmd->nhicmd->idx);
		nhi_free_tx_frame(sc->ring0, cmd->nhicmd);
	}
	free(cmd, M_THUNDERBOLT);

	return;
}

static void
router_prepare_read(struct router_softc *sc, struct router_command *cmd,
    int len)
{
	struct nhi_cmd_frame *nhicmd;
	uint32_t *msg;
	int msglen, i;

	KASSERT(cmd != NULL, ("cmd cannot be NULL\n"));
	KASSERT(len != 0, ("Invalid zero-length command\n"));
	KASSERT(len % 4 == 0, ("Message must be 32bit padded\n"));

	nhicmd = cmd->nhicmd;
	msglen = (len - 4) / 4;
	for (i = 0; i < msglen; i++)
		nhicmd->data[i] = htobe32(nhicmd->data[i]);

	msg = (uint32_t *)nhicmd->data;
	msg[msglen] = htobe32(tb_calc_crc(nhicmd->data, len-4));

	nhicmd->pdf = PDF_READ;
	nhicmd->req_len = len;

	nhicmd->timeout = NHI_CMD_TIMEOUT;
	nhicmd->retries = 0;
	nhicmd->resp_buffer = (uint32_t *)cmd->resp_buffer;
	nhicmd->resp_len = (cmd->dwlen + 3) * 4;
	nhicmd->context = cmd;

	cmd->retries = CFG_DEFAULT_RETRIES;
	cmd->timeout = CFG_DEFAULT_TIMEOUT;

	return;
}

static int
router_schedule(struct router_softc *sc, struct router_command *cmd)
{
	int error;

	mtx_lock(&sc->mtx);
	error = router_schedule_locked(sc, cmd);
	mtx_unlock(&sc->mtx);

	return(error);
}

static int
router_schedule_locked(struct router_softc *sc, struct router_command *cmd)
{
	struct nhi_cmd_frame *nhicmd;
	int error;

	tb_debug(sc, DBG_ROUTER|DBG_EXTRA, "router_schedule\n");

	if (cmd != NULL)
		TAILQ_INSERT_TAIL(&sc->cmd_queue, cmd, link);

	while ((sc->inflight_cmd == NULL) &&
	    ((cmd = TAILQ_FIRST(&sc->cmd_queue)) != NULL)) {

		TAILQ_REMOVE(&sc->cmd_queue, cmd, link);
		nhicmd = cmd->nhicmd;
		tb_debug(sc, DBG_ROUTER|DBG_EXTRA,
		    "Scheduling command with index %d\n", nhicmd->idx);
		sc->inflight_cmd = cmd;
		if ((error = nhi_tx_schedule(sc->ring0, nhicmd)) != 0) {
			tb_debug(sc, DBG_ROUTER, "nhi ring error "
			    "%d\n", error);
			sc->inflight_cmd = NULL;
			if (error == EBUSY) {
				TAILQ_INSERT_HEAD(&sc->cmd_queue, cmd, link);
				error = 0;
			}
			break;
		}
	}

	return (error);
}

static void
router_complete_intr(void *context, union nhi_ring_desc *ring,
    struct nhi_cmd_frame *nhicmd)
{
	struct router_softc *sc;
	struct router_command *cmd;

	KASSERT(context != NULL, ("context cannot be NULL\n"));
	KASSERT(nhicmd != NULL, ("nhicmd cannot be NULL\n"));

	cmd = (struct router_command *)(nhicmd->context);
	sc = cmd->sc;
	tb_debug(sc, DBG_ROUTER|DBG_EXTRA, "router_complete_intr called\n");

	if (nhicmd->flags & CMD_RESP_COMPLETE) {
		cmd->callback(sc, cmd, cmd->callback_arg);
	}

	return;
}

static void
router_response_intr(void *context, union nhi_ring_desc *ring, struct nhi_cmd_frame *nhicmd)
{
	struct router_softc *sc, *dev;
	struct tb_cfg_read_resp *read;
	struct tb_cfg_write_resp *write;
	struct router_command *cmd;
	tb_route_t route;
	u_int error, i, eof, len;
	uint32_t attrs;

	KASSERT(context != NULL, ("context cannot be NULL\n"));

	sc = (struct router_softc *)context;
	tb_debug(sc, DBG_ROUTER|DBG_EXTRA, "router_response_intr called\n");

	eof = ring->rxpost.eof_len >> RX_BUFFER_DESC_EOF_SHIFT;

	if (eof == PDF_WRITE) {
		write = (struct tb_cfg_write_resp *)nhicmd->data;
		route.hi = be32toh(write->route.hi);
		route.lo = be32toh(write->route.lo);
	} else {
		read = (struct tb_cfg_read_resp *)nhicmd->data;
		route.hi = be32toh(read->route.hi);
		route.lo = be32toh(read->route.lo);
		attrs = be32toh(read->addr_attrs);
		len = (attrs & TB_CFG_SIZE_MASK) >> TB_CFG_SIZE_SHIFT;
	}

	/* XXX Is this a problem? */
	if ((route.hi & 0x80000000) == 0)
		tb_debug(sc, DBG_ROUTER, "Invalid route\n");
	route.hi &= ~0x80000000;

	tb_debug(sc, DBG_ROUTER|DBG_EXTRA, "Looking up route 0x%08x%08x\n",
	    route.hi, route.lo);

	error = router_lookup_device(sc, route, &dev);
	if (error != 0 || dev == NULL) {
		tb_debug(sc, DBG_ROUTER, "Cannot find device, error= %d\n",
		    error);
		return;
	}

	tb_debug(sc, DBG_ROUTER|DBG_EXTRA, "Found device %s route 0x%08x%08x, "
	    "inflight_cmd= %p\n", device_get_nameunit(dev->dev), dev->route.hi,
	    dev->route.lo, dev->inflight_cmd);

	cmd = dev->inflight_cmd;
	if (cmd == NULL) {
		tb_debug(dev, DBG_ROUTER, "Null inflight cmd\n");
		return;
	}

	if (eof == PDF_READ) {
		for (i = 0; i < len; i++)
			cmd->nhicmd->resp_buffer[i] = be32toh(read->data[i]);
	}

	cmd->nhicmd->flags |= CMD_RESP_COMPLETE;
	if (cmd->nhicmd->flags & CMD_REQ_COMPLETE) {
		tb_debug(sc, DBG_ROUTER|DBG_EXTRA, "TX_COMPLETE set\n");
		cmd->callback(dev, cmd, cmd->callback_arg);
	}

	return;
}

static void
router_notify_intr(void *context, union nhi_ring_desc *ring, struct nhi_cmd_frame *nhicmd)
{
	struct router_softc *sc;
	struct router_command *cmd;
	struct tb_cfg_notify event;
	u_int ev, adap;

	KASSERT(context != NULL, ("context cannot be NULL\n"));

	sc = (struct router_softc *)context;
	tb_debug(sc, DBG_ROUTER|DBG_EXTRA, "router_notify_intr called\n");

	event.route.hi = be32toh(nhicmd->data[0]);
	event.route.lo = be32toh(nhicmd->data[1]);
	event.event_adap = be32toh(nhicmd->data[2]);

	ev = GET_NOTIFY_EVENT(&event);
	adap = GET_NOTIFY_ADAPTER(&event);

	tb_debug(sc, DBG_ROUTER, "Event route 0x%08x%08x adap %d code %s\n",
	    event.route.hi, event.route.lo, adap,
	    tb_get_string(ev, tb_notify_event));

	switch (ev) {
	case TB_CFG_ERR_CONN:
	case TB_CFG_ERR_LINK:
	case TB_CFG_ERR_ADDR:
	case TB_CFG_ERR_ADP:
	case TB_CFG_ERR_ENUM:
	case TB_CFG_ERR_NUA:
	case TB_CFG_ERR_LEN:
	case TB_CFG_ERR_HEC:
	case TB_CFG_ERR_FC:
	case TB_CFG_ERR_PLUG:
	case TB_CFG_ERR_LOCK:
	case TB_CFG_HP_ACK:
	case TB_CFG_DP_BW:
		if (sc->inflight_cmd != NULL) {
			cmd = sc->inflight_cmd;
			cmd->ev = ev;
			cmd->callback(sc, cmd, cmd->callback_arg);
		}
		break;
	default:
		break;
	}
	return;
}

int
tb_config_next_cap(struct router_softc *sc, struct router_cfg_cap *cap)
{
	union tb_cfg_cap *tbcap;
	uint32_t *buf;
	uint16_t current;
	int error;

	KASSERT(cap != NULL, ("cap cannot be NULL\n"));
	KASSERT(cap->next_cap != 0, ("next_cap cannot be 0\n"));

	buf = malloc(sizeof(*tbcap), M_THUNDERBOLT, M_NOWAIT|M_ZERO);

	current = cap->next_cap;
	error = tb_config_read(sc, cap->space, cap->adap, current, 1, buf);
	if (error)
		return (error);

	tbcap = (union tb_cfg_cap *)buf;
	cap->cap_id = tbcap->hdr.cap_id;
	cap->next_cap = tbcap->hdr.next_cap;
	cap->current_cap = current;

	if ((cap->space != TB_CFG_CS_ROUTER) &&
	    (tbcap->hdr.cap_id != TB_CFG_CAP_VSC)) {
		free(buf, M_THUNDERBOLT);
		return (0);
	}

	tb_config_read(sc, cap->space, cap->adap, current, 2, buf);
	if (error) {
		free(buf, M_THUNDERBOLT);
		return (error);
	}

	cap->vsc_id = tbcap->vsc.vsc_id;
	cap->vsc_len = tbcap->vsc.len;
	if (tbcap->vsc.len == 0) {
		cap->next_cap = tbcap->vsec.vsec_next_cap;
		cap->vsec_len = tbcap->vsec.vsec_len;
	}

	free(buf, M_THUNDERBOLT);
	return (0);
}

int
tb_config_find_cap(struct router_softc *sc, struct router_cfg_cap *cap)
{
	u_int cap_id, vsc_id;
	int error;

	tb_debug(sc, DBG_ROUTER|DBG_EXTRA, "tb_config_find_cap called\n");

	cap_id = cap->cap_id;
	vsc_id = cap->vsc_id;

	cap->cap_id = cap->vsc_id = 0;
	while ((cap->cap_id != cap_id) || (cap->vsc_id != vsc_id)) {
		tb_debug(sc, DBG_ROUTER|DBG_EXTRA,
		    "Looking for cap %d at offset %d\n", cap->cap_id,
		    cap->next_cap);
		if ((cap->next_cap == 0) ||
		    (cap->next_cap > TB_CFG_CAP_OFFSET_MAX))
			return (EINVAL);
		error = tb_config_next_cap(sc, cap);
		if (error)
			break;
	}

	return (0);
}

int
tb_config_find_router_cap(struct router_softc *sc, u_int cap, u_int vsc, u_int *offset)
{
	struct router_cfg_cap rcap;
	struct tb_cfg_router *cfg;
	uint32_t *buf;
	int error;

	buf = malloc(8 * 4, M_THUNDERBOLT, M_NOWAIT|M_ZERO);
	if (buf == NULL)
		return (ENOMEM);

	error = tb_config_router_read(sc, 0, 5, buf);
	if (error != 0) {
		free(buf, M_THUNDERBOLT);
		return (error);
	}

	cfg = (struct tb_cfg_router *)buf;
	rcap.space = TB_CFG_CS_ROUTER;
	rcap.adap = 0;
	rcap.next_cap = GET_ROUTER_CS_NEXT_CAP(cfg);
	rcap.cap_id = cap;
	rcap.vsc_id = vsc;
	error = tb_config_find_cap(sc, &rcap);
	if (error == 0)
		*offset = rcap.current_cap;

	free(buf, M_THUNDERBOLT);
	return (error);
}

int
tb_config_find_router_vsc(struct router_softc *sc, u_int cap, u_int *offset)
{

	return (tb_config_find_router_cap(sc, TB_CFG_CAP_VSC, cap, offset));
}

int
tb_config_find_router_vsec(struct router_softc *sc, u_int cap, u_int *offset)
{

	return (tb_config_find_router_cap(sc, TB_CFG_CAP_VSEC, cap, offset));
}

int
tb_config_find_adapter_cap(struct router_softc *sc, u_int adap, u_int cap, u_int *offset)
{
	struct router_cfg_cap rcap;
	struct tb_cfg_adapter *cfg;
	uint32_t *buf;
	int error;

	buf = malloc(8 * 4, M_THUNDERBOLT, M_NOWAIT|M_ZERO);
	if (buf == NULL)
		return (ENOMEM);

	error = tb_config_adapter_read(sc, adap, 0, 8, buf);
	if (error != 0) {
		free(buf, M_THUNDERBOLT);
		return (error);
	}

	cfg = (struct tb_cfg_adapter *)buf;
	rcap.space = TB_CFG_CS_ADAPTER;
	rcap.adap = adap;
	rcap.next_cap = GET_ADP_CS_NEXT_CAP(cfg);
	rcap.cap_id = cap;
	rcap.vsc_id = 0;
	error = tb_config_find_cap(sc, &rcap);
	if (error == 0)
		*offset = rcap.current_cap;

	free(buf, M_THUNDERBOLT);
	return (error);
}

int
tb_config_get_lc_uuid(struct router_softc *rsc, uint8_t *uuid)
{
	u_int error, offset;
	uint32_t buf[8];

	bzero(buf, sizeof(buf));

	error = tb_config_find_router_vsec(rsc, TB_CFG_VSEC_LC, &offset);
	if (error != 0) {
		tb_debug(rsc, DBG_ROUTER, "Error finding LC registers: %d\n",
		    error);
		return (error);
	}

	error = tb_config_router_read(rsc, offset + TB_LC_UUID, 4, buf);
	if (error != 0) {
		tb_debug(rsc, DBG_ROUTER, "Error fetching UUID: %d\n", error);
		return (error);
	}

	bcopy(buf, uuid, 16);
	return (0);
}