~ubuntu-branches/ubuntu/utopic/seabios/utopic-proposed

« back to all changes in this revision

Viewing changes to src/hw/usb-xhci.c

  • Committer: Package Import Robot
  • Author(s): Michael Tokarev
  • Date: 2014-05-31 12:29:13 UTC
  • mfrom: (1.1.9)
  • Revision ID: package-import@ubuntu.com-20140531122913-1107keyljv7hf4w6
Tags: 1.7.5-1
* new upstream release
* dropped all patches taken from upstream
* disabled more features (XEN & USB_UAS) from the 128k bios build to fit
* set upstream source URL (Closes: #740471) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Code for handling XHCI "Super speed" USB controllers.
 
2
//
 
3
// Copyright (C) 2013  Gerd Hoffmann <kraxel@redhat.com>
 
4
// Copyright (C) 2014  Kevin O'Connor <kevin@koconnor.net>
 
5
//
 
6
// This file may be distributed under the terms of the GNU LGPLv3 license.
 
7
 
 
8
#include "biosvar.h" // GET_LOWFLAT
1
9
#include "config.h" // CONFIG_*
 
10
#include "malloc.h" // memalign_low
 
11
#include "memmap.h" // PAGE_SIZE
2
12
#include "output.h" // dprintf
3
 
#include "string.h" // memcpy_fl
4
 
#include "util.h" // timer_calc
5
 
#include "x86.h" // readl
6
 
#include "malloc.h" // memalign_low
7
13
#include "pci.h" // pci_bdf_to_bus
 
14
#include "pci_ids.h" // PCI_CLASS_SERIAL_USB_XHCI
8
15
#include "pci_regs.h" // PCI_BASE_ADDRESS_0
 
16
#include "string.h" // memcpy_fl
9
17
#include "usb.h" // struct usb_s
10
18
#include "usb-xhci.h" // struct ehci_qh
11
 
#include "biosvar.h" // GET_LOWFLAT
 
19
#include "util.h" // timer_calc
 
20
#include "x86.h" // readl
12
21
 
13
22
// --------------------------------------------------------------
14
23
// configuration
227
236
    u32                  xcap;
228
237
    u32                  ports;
229
238
    u32                  slots;
 
239
    u8                   context64;
230
240
 
231
241
    /* xhci registers */
232
242
    struct xhci_caps     *caps;
240
250
    struct xhci_ring     *cmds;
241
251
    struct xhci_ring     *evts;
242
252
    struct xhci_er_seg   *eseg;
243
 
 
244
 
    /* usb devices */
245
 
    struct hlist_head    list;
246
 
};
247
 
 
248
 
struct xhci_device {
249
 
    struct xhci_devctx   devctx;
250
 
    struct xhci_inctx    inctx;
251
 
 
252
 
    struct usbdevice_s   *usbdev;
253
 
    struct usb_xhci_s    *xhci;
254
 
    u32                  slotid;
255
 
    struct hlist_node    next;
256
253
};
257
254
 
258
255
struct xhci_pipe {
259
256
    struct xhci_ring     reqs;
260
257
 
261
258
    struct usb_pipe      pipe;
262
 
    struct xhci_device   *dev;
 
259
    u32                  slotid;
263
260
    u32                  epid;
264
261
    void                 *buf;
265
262
    int                  bufused;
292
289
    [ USB_SUPERSPEED ] = 4,
293
290
};
294
291
 
295
 
static const int speed_to_ctlsize[] = {
296
 
    [ USB_FULLSPEED  ] = 8,
297
 
    [ USB_LOWSPEED   ] = 8,
298
 
    [ USB_HIGHSPEED  ] = 64,
299
 
    [ USB_SUPERSPEED ] = 256,
300
 
};
301
 
 
302
292
static const int eptype_to_xhci_in[] = {
303
293
    [ USB_ENDPOINT_XFER_CONTROL] = 4,
304
294
    [ USB_ENDPOINT_XFER_ISOC   ] = 5,
319
309
static void xhci_doorbell(struct usb_xhci_s *xhci, u32 slotid, u32 value)
320
310
{
321
311
    struct xhci_db *db = GET_LOWFLAT(xhci->db);
322
 
    u32 addr = (u32)(&db[slotid].doorbell);
323
 
    pci_writel(addr, value);
 
312
    void *addr = &db[slotid].doorbell;
 
313
    writel(addr, value);
324
314
}
325
315
 
326
316
static void xhci_process_events(struct usb_xhci_s *xhci)
375
365
        }
376
366
        SET_LOWFLAT(evts->nidx, nidx);
377
367
        struct xhci_ir *ir = GET_LOWFLAT(xhci->ir);
378
 
        u32 addr = (u32)(&ir->erdp_low);
379
368
        u32 erdp = (u32)(evts->ring + nidx);
380
 
        pci_writel(addr, erdp);
 
369
        writel(&ir->erdp_low, erdp);
 
370
        writel(&ir->erdp_high, 0);
381
371
    }
382
372
}
383
373
 
456
446
 
457
447
static void xhci_xfer_kick(struct xhci_pipe *pipe)
458
448
{
459
 
    struct xhci_device *dev = GET_LOWFLAT(pipe->dev);
460
 
    struct usb_xhci_s *xhci = GET_LOWFLAT(dev->xhci);
461
 
    u32 slotid = GET_LOWFLAT(dev->slotid);
 
449
    struct usb_xhci_s *xhci = container_of(
 
450
        GET_LOWFLAT(pipe->pipe.cntl), struct usb_xhci_s, usb);
 
451
    u32 slotid = GET_LOWFLAT(pipe->slotid);
462
452
    u32 epid = GET_LOWFLAT(pipe->epid);
463
453
 
464
454
    dprintf(5, "%s: ring %p, slotid %d, epid %d\n",
529
519
    return (xhci->cmds->evt.control >> 24) & 0xff;
530
520
}
531
521
 
532
 
static int xhci_cmd_disable_slot(struct xhci_device *dev)
 
522
#if 0
 
523
static int xhci_cmd_disable_slot(struct usb_xhci_s *xhci, u32 slotid)
533
524
{
534
525
    ASSERT32FLAT();
535
526
    struct xhci_trb cmd = {
536
527
        .ptr_low  = 0,
537
528
        .ptr_high = 0,
538
529
        .status   = 0,
539
 
        .control  = (dev->slotid << 24) | (CR_DISABLE_SLOT << 10)
540
 
    };
541
 
    dprintf(3, "%s: slotid %d\n", __func__, dev->slotid);
542
 
    return xhci_cmd_submit(dev->xhci, &cmd);
543
 
}
544
 
 
545
 
static int xhci_cmd_address_device(struct xhci_device *dev)
546
 
{
547
 
    ASSERT32FLAT();
548
 
    struct xhci_trb cmd = {
549
 
        .ptr_low  = (u32)&dev->inctx,
550
 
        .ptr_high = 0,
551
 
        .status   = 0,
552
 
        .control  = (dev->slotid << 24) | (CR_ADDRESS_DEVICE << 10)
553
 
    };
554
 
    dprintf(3, "%s: slotid %d\n", __func__, dev->slotid);
555
 
    return xhci_cmd_submit(dev->xhci, &cmd);
556
 
}
557
 
 
558
 
static int xhci_cmd_configure_endpoint(struct xhci_device *dev)
559
 
{
560
 
    ASSERT32FLAT();
561
 
    struct xhci_trb cmd = {
562
 
        .ptr_low  = (u32)&dev->inctx,
563
 
        .ptr_high = 0,
564
 
        .status   = 0,
565
 
        .control  = (dev->slotid << 24) | (CR_CONFIGURE_ENDPOINT << 10)
566
 
    };
567
 
    dprintf(3, "%s: slotid %d, add 0x%x, del 0x%x\n", __func__,
568
 
            dev->slotid, dev->inctx.add, dev->inctx.del);
569
 
    return xhci_cmd_submit(dev->xhci, &cmd);
570
 
}
571
 
 
572
 
static int xhci_cmd_evaluate_context(struct xhci_device *dev)
573
 
{
574
 
    ASSERT32FLAT();
575
 
    struct xhci_trb cmd = {
576
 
        .ptr_low  = (u32)&dev->inctx,
577
 
        .ptr_high = 0,
578
 
        .status   = 0,
579
 
        .control  = (dev->slotid << 24) | (CR_EVALUATE_CONTEXT << 10)
580
 
    };
581
 
    dprintf(3, "%s: slotid %d, add 0x%x, del 0x%x\n", __func__,
582
 
            dev->slotid, dev->inctx.add, dev->inctx.del);
583
 
    return xhci_cmd_submit(dev->xhci, &cmd);
 
530
        .control  = (slotid << 24) | (CR_DISABLE_SLOT << 10)
 
531
    };
 
532
    dprintf(3, "%s: slotid %d\n", __func__, slotid);
 
533
    return xhci_cmd_submit(xhci, &cmd);
 
534
}
 
535
#endif
 
536
 
 
537
static int xhci_cmd_address_device(struct usb_xhci_s *xhci, u32 slotid
 
538
                                   , struct xhci_inctx *inctx)
 
539
{
 
540
    ASSERT32FLAT();
 
541
    struct xhci_trb cmd = {
 
542
        .ptr_low  = (u32)inctx,
 
543
        .ptr_high = 0,
 
544
        .status   = 0,
 
545
        .control  = (slotid << 24) | (CR_ADDRESS_DEVICE << 10)
 
546
    };
 
547
    dprintf(3, "%s: slotid %d\n", __func__, slotid);
 
548
    return xhci_cmd_submit(xhci, &cmd);
 
549
}
 
550
 
 
551
static int xhci_cmd_configure_endpoint(struct usb_xhci_s *xhci, u32 slotid
 
552
                                       , struct xhci_inctx *inctx)
 
553
{
 
554
    ASSERT32FLAT();
 
555
    struct xhci_trb cmd = {
 
556
        .ptr_low  = (u32)inctx,
 
557
        .ptr_high = 0,
 
558
        .status   = 0,
 
559
        .control  = (slotid << 24) | (CR_CONFIGURE_ENDPOINT << 10)
 
560
    };
 
561
    dprintf(3, "%s: slotid %d, add 0x%x, del 0x%x\n", __func__,
 
562
            slotid, inctx->add, inctx->del);
 
563
    return xhci_cmd_submit(xhci, &cmd);
 
564
}
 
565
 
 
566
static int xhci_cmd_evaluate_context(struct usb_xhci_s *xhci, u32 slotid
 
567
                                     , struct xhci_inctx *inctx)
 
568
{
 
569
    ASSERT32FLAT();
 
570
    struct xhci_trb cmd = {
 
571
        .ptr_low  = (u32)inctx,
 
572
        .ptr_high = 0,
 
573
        .status   = 0,
 
574
        .control  = (slotid << 24) | (CR_EVALUATE_CONTEXT << 10)
 
575
    };
 
576
    dprintf(3, "%s: slotid %d, add 0x%x, del 0x%x\n", __func__,
 
577
            slotid, inctx->add, inctx->del);
 
578
    return xhci_cmd_submit(xhci, &cmd);
584
579
}
585
580
 
586
581
static void xhci_xfer_setup(struct xhci_pipe *pipe,
619
614
    xhci_xfer_queue(pipe, &trb);
620
615
}
621
616
 
622
 
static void xhci_xfer_status(struct xhci_pipe *pipe, int dir)
 
617
static void xhci_xfer_status(struct xhci_pipe *pipe, int dir, int datalen)
623
618
{
624
619
    ASSERT32FLAT();
625
620
    struct xhci_trb trb;
627
622
    memset(&trb, 0, sizeof(trb));
628
623
    trb.control  |= (TR_STATUS << 10); // trb type
629
624
    trb.control  |= TRB_TR_IOC;
630
 
    if (dir)
 
625
    if (!datalen || !dir)
631
626
        trb.control |= (1 << 16);
632
627
 
633
628
    xhci_xfer_queue(pipe, &trb);
634
629
    xhci_xfer_kick(pipe);
635
630
}
636
631
 
637
 
static struct xhci_device *xhci_find_alloc_device(struct usb_xhci_s *xhci,
638
 
                                                  struct usbdevice_s *usbdev)
639
 
{
640
 
    ASSERT32FLAT();
641
 
    struct xhci_device *dev;
642
 
 
643
 
    hlist_for_each_entry(dev, &xhci->list, next) {
644
 
        if (dev->usbdev == usbdev) {
645
 
            return dev;
646
 
        }
647
 
    }
648
 
 
649
 
    dev = memalign_low(64, sizeof(*dev));
650
 
    if (!dev) {
651
 
        warn_noalloc();
652
 
        return NULL;
653
 
    }
654
 
    memset(dev, 0, sizeof(*dev));
655
 
    dev->usbdev = usbdev;
656
 
    dev->xhci = xhci;
657
 
    hlist_add_head(&dev->next, &xhci->list);
658
 
    return dev;
659
 
}
660
 
 
661
632
static void
662
633
configure_xhci(void *data)
663
634
{
710
681
    writel(&xhci->ir->erstba_high, 0);
711
682
    xhci->evts->cs = 1;
712
683
 
 
684
    reg = readl(&xhci->caps->hcsparams2);
 
685
    u32 spb = reg >> 27;
 
686
    if (spb) {
 
687
        dprintf(3, "%s: setup %d scratch pad buffers\n", __func__, spb);
 
688
        u64 *spba = memalign_high(64, sizeof(*spba) * spb);
 
689
        void *pad = memalign_high(PAGE_SIZE, PAGE_SIZE * spb);
 
690
        if (!spba || !pad) {
 
691
            warn_noalloc();
 
692
            free(spba);
 
693
            free(pad);
 
694
            goto fail;
 
695
        }
 
696
        int i;
 
697
        for (i = 0; i < spb; i++)
 
698
            spba[i] = (u32)pad + (i * PAGE_SIZE);
 
699
        xhci->devs[0].ptr_low = (u32)spba;
 
700
        xhci->devs[0].ptr_high = 0;
 
701
    }
 
702
 
713
703
    reg = readl(&xhci->op->usbcmd);
714
704
    reg |= XHCI_CMD_RS;
715
705
    writel(&xhci->op->usbcmd, reg);
716
706
 
717
707
    // FIXME: try find a more elegant way than a fixed delay
718
 
    mdelay(100);
 
708
    msleep(100);
719
709
 
720
710
    usb_enumerate(&xhci->hub);
 
711
    // XXX - should walk list of pipes and free unused pipes.
721
712
    if (xhci->hub.devcount)
722
713
        return;
723
714
 
806
797
xhci_hub_disconnect(struct usbhub_s *hub, u32 port)
807
798
{
808
799
    ASSERT32FLAT();
809
 
    struct usb_xhci_s *xhci = container_of(hub->cntl, struct usb_xhci_s, usb);
810
 
    struct xhci_device *dev;
811
 
 
812
 
    hlist_for_each_entry(dev, &xhci->list, next) {
813
 
        if (dev->usbdev->hub == hub &&
814
 
            dev->usbdev->port == port &&
815
 
            dev->slotid != 0) {
816
 
            xhci_cmd_disable_slot(dev);
817
 
            hlist_del(&dev->next);
818
 
            return;
819
 
        }
820
 
    }
 
800
    // XXX - should turn the port power off.
821
801
}
822
802
 
823
803
static struct usbhub_op_s xhci_hub_ops = {
829
809
// --------------------------------------------------------------
830
810
// external interface
831
811
 
 
812
 
 
813
static struct xhci_inctx *
 
814
xhci_alloc_inctx(struct usbdevice_s *usbdev, int maxepid)
 
815
{
 
816
    struct usb_xhci_s *xhci = container_of(
 
817
        usbdev->hub->cntl, struct usb_xhci_s, usb);
 
818
    int size = (sizeof(struct xhci_inctx) * 33) << xhci->context64;
 
819
    struct xhci_inctx *in = memalign_tmphigh(2048 << xhci->context64, size);
 
820
    if (!in) {
 
821
        warn_noalloc();
 
822
        return NULL;
 
823
    }
 
824
    memset(in, 0, size);
 
825
 
 
826
    struct xhci_slotctx *slot = (void*)&in[1 << xhci->context64];
 
827
    slot->ctx[0]    |= maxepid << 27; // context entries
 
828
    slot->ctx[0]    |= speed_to_xhci[usbdev->speed] << 20;
 
829
 
 
830
    // Set high-speed hub flags.
 
831
    struct usbdevice_s *hubdev = usbdev->hub->usbdev;
 
832
    if (hubdev) {
 
833
        if (usbdev->speed == USB_LOWSPEED || usbdev->speed == USB_FULLSPEED) {
 
834
            struct xhci_pipe *hpipe = container_of(
 
835
                hubdev->defpipe, struct xhci_pipe, pipe);
 
836
            if (hubdev->speed == USB_HIGHSPEED) {
 
837
                slot->ctx[2] |= hpipe->slotid;
 
838
                slot->ctx[2] |= (usbdev->port+1) << 8;
 
839
            } else {
 
840
                struct xhci_slotctx *hslot = (void*)xhci->devs[hpipe->slotid].ptr_low;
 
841
                slot->ctx[2] = hslot->ctx[2];
 
842
            }
 
843
        }
 
844
        u32 route = 0;
 
845
        while (usbdev->hub->usbdev) {
 
846
            route <<= 4;
 
847
            route |= (usbdev->port+1) & 0xf;
 
848
            usbdev = usbdev->hub->usbdev;
 
849
        }
 
850
        slot->ctx[0]    |= route;
 
851
    }
 
852
 
 
853
    slot->ctx[1]    |= (usbdev->port+1) << 16;
 
854
 
 
855
    return in;
 
856
}
 
857
 
 
858
static int xhci_config_hub(struct usbhub_s *hub)
 
859
{
 
860
    struct usb_xhci_s *xhci = container_of(
 
861
        hub->cntl, struct usb_xhci_s, usb);
 
862
    struct xhci_pipe *pipe = container_of(
 
863
        hub->usbdev->defpipe, struct xhci_pipe, pipe);
 
864
    struct xhci_slotctx *hdslot = (void*)xhci->devs[pipe->slotid].ptr_low;
 
865
    if ((hdslot->ctx[3] >> 27) == 3)
 
866
        // Already configured
 
867
        return 0;
 
868
    struct xhci_inctx *in = xhci_alloc_inctx(hub->usbdev, 1);
 
869
    if (!in)
 
870
        return -1;
 
871
    in->add = 0x01;
 
872
    struct xhci_slotctx *slot = (void*)&in[1 << xhci->context64];
 
873
    slot->ctx[0] |= 1 << 26;
 
874
    slot->ctx[1] |= hub->portcount << 24;
 
875
 
 
876
    int cc = xhci_cmd_configure_endpoint(xhci, pipe->slotid, in);
 
877
    free(in);
 
878
    if (cc != CC_SUCCESS) {
 
879
        dprintf(1, "%s: configure hub: failed (cc %d)\n", __func__, cc);
 
880
        return -1;
 
881
    }
 
882
    return 0;
 
883
}
 
884
 
832
885
struct usb_pipe *
833
886
xhci_alloc_pipe(struct usbdevice_s *usbdev
834
887
                , struct usb_endpoint_descriptor *epdesc)
860
913
    memset(pipe, 0, sizeof(*pipe));
861
914
 
862
915
    usb_desc2pipe(&pipe->pipe, usbdev, epdesc);
863
 
    pipe->dev = xhci_find_alloc_device(xhci, usbdev);
864
 
    if (!pipe->dev) {
865
 
        free(pipe);
866
 
        return NULL;
867
 
    }
868
916
    pipe->epid = epid;
869
917
    pipe->reqs.cs = 1;
870
918
    if (eptype == USB_ENDPOINT_XFER_INT)
871
919
        pipe->buf = malloc_low(pipe->pipe.maxpacket);
872
920
 
 
921
    // Allocate input context and initialize endpoint info.
 
922
    struct xhci_inctx *in = xhci_alloc_inctx(usbdev, epid);
 
923
    if (!in)
 
924
        goto fail;
 
925
    in->add = 0x01 | (1 << epid);
 
926
    struct xhci_epctx *ep = (void*)&in[(pipe->epid+1) << xhci->context64];
 
927
    if (eptype == USB_ENDPOINT_XFER_INT)
 
928
        ep->ctx[0] = (usb_getFrameExp(usbdev, epdesc) + 3) << 16;
 
929
    ep->ctx[1]   |= eptype << 3;
 
930
    if (epdesc->bEndpointAddress & USB_DIR_IN
 
931
        || eptype == USB_ENDPOINT_XFER_CONTROL)
 
932
        ep->ctx[1] |= 1 << 5;
 
933
    ep->ctx[1]   |= pipe->pipe.maxpacket << 16;
 
934
    ep->deq_low  = (u32)&pipe->reqs.ring[0];
 
935
    ep->deq_low  |= 1;         // dcs
 
936
    ep->length   = pipe->pipe.maxpacket;
 
937
 
873
938
    dprintf(3, "%s: usbdev %p, ring %p, slotid %d, epid %d\n", __func__,
874
 
            usbdev, &pipe->reqs, pipe->dev->slotid, pipe->epid);
875
 
    if (pipe->epid > 1 && pipe->dev->slotid) {
876
 
        struct xhci_inctx *in = &pipe->dev->inctx;
877
 
        in->add = (1 << pipe->epid) | 1;
878
 
        in->del = 0;
879
 
 
880
 
        in->slot.ctx[0]    |= (31 << 27); // context entries
881
 
 
882
 
        int e = pipe->epid-1;
883
 
        in->ep[e].ctx[1]   |= (eptype << 3);
884
 
        if (epdesc->bEndpointAddress & USB_DIR_IN)
885
 
            in->ep[e].ctx[1] |= (1 << 5);
886
 
        in->ep[e].ctx[1]   |= (pipe->pipe.maxpacket << 16);
887
 
        in->ep[e].deq_low  = (u32)&pipe->reqs.ring[0];
888
 
        in->ep[e].deq_low  |= 1;         // dcs
889
 
        in->ep[e].deq_high = 0;
890
 
        in->ep[e].length   = pipe->pipe.maxpacket;
891
 
 
892
 
        int cc = xhci_cmd_configure_endpoint(pipe->dev);
 
939
            usbdev, &pipe->reqs, pipe->slotid, pipe->epid);
 
940
    if (pipe->epid == 1) {
 
941
        if (usbdev->hub->usbdev) {
 
942
            // Make sure parent hub is configured.
 
943
            int ret = xhci_config_hub(usbdev->hub);
 
944
            if (ret)
 
945
                goto fail;
 
946
        }
 
947
        // Enable slot.
 
948
        u32 size = (sizeof(struct xhci_slotctx) * 32) << xhci->context64;
 
949
        struct xhci_slotctx *dev = memalign_high(1024 << xhci->context64, size);
 
950
        if (!dev) {
 
951
            warn_noalloc();
 
952
            goto fail;
 
953
        }
 
954
        int slotid = xhci_cmd_enable_slot(xhci);
 
955
        if (slotid < 0) {
 
956
            dprintf(1, "%s: enable slot: failed\n", __func__);
 
957
            free(dev);
 
958
            goto fail;
 
959
        }
 
960
        dprintf(3, "%s: enable slot: got slotid %d\n", __func__, slotid);
 
961
        memset(dev, 0, size);
 
962
        pipe->slotid = usbdev->slotid = slotid;
 
963
        xhci->devs[slotid].ptr_low = (u32)dev;
 
964
        xhci->devs[slotid].ptr_high = 0;
 
965
 
 
966
        // Send set_address command.
 
967
        int cc = xhci_cmd_address_device(xhci, slotid, in);
 
968
        if (cc != CC_SUCCESS) {
 
969
            dprintf(1, "%s: address device: failed (cc %d)\n", __func__, cc);
 
970
            goto fail;
 
971
        }
 
972
    } else {
 
973
        pipe->slotid = usbdev->slotid;
 
974
        // Send configure command.
 
975
        int cc = xhci_cmd_configure_endpoint(xhci, pipe->slotid, in);
893
976
        if (cc != CC_SUCCESS) {
894
977
            dprintf(1, "%s: configure endpoint: failed (cc %d)\n", __func__, cc);
895
 
            free(pipe);
896
 
            return NULL;
 
978
            goto fail;
897
979
        }
898
980
    }
899
 
 
 
981
    free(in);
900
982
    return &pipe->pipe;
 
983
 
 
984
fail:
 
985
    free(pipe);
 
986
    free(in);
 
987
    return NULL;
901
988
}
902
989
 
903
990
struct usb_pipe *
909
996
        return NULL;
910
997
    u8 eptype = epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
911
998
    struct xhci_pipe *pipe = container_of(upipe, struct xhci_pipe, pipe);
 
999
    struct usb_xhci_s *xhci = container_of(
 
1000
        pipe->pipe.cntl, struct usb_xhci_s, usb);
912
1001
    dprintf(3, "%s: usbdev %p, ring %p, slotid %d, epid %d\n", __func__,
913
 
            usbdev, &pipe->reqs, pipe->dev->slotid, pipe->epid);
 
1002
            usbdev, &pipe->reqs, pipe->slotid, pipe->epid);
914
1003
    if (eptype == USB_ENDPOINT_XFER_CONTROL &&
915
 
        pipe->pipe.maxpacket !=  epdesc->wMaxPacketSize) {
 
1004
        pipe->pipe.maxpacket != epdesc->wMaxPacketSize) {
916
1005
        dprintf(1, "%s: reconf ctl endpoint pkt size: %d -> %d\n",
917
1006
                __func__, pipe->pipe.maxpacket, epdesc->wMaxPacketSize);
918
1007
        pipe->pipe.maxpacket = epdesc->wMaxPacketSize;
919
 
        struct xhci_inctx *in = &pipe->dev->inctx;
 
1008
        struct xhci_inctx *in = xhci_alloc_inctx(usbdev, 1);
 
1009
        if (!in)
 
1010
            return upipe;
920
1011
        in->add = (1 << 1);
921
 
        in->del = 0;
922
 
        in->ep[0].ctx[1] &= 0xffff;
923
 
        in->ep[0].ctx[1] |= (pipe->pipe.maxpacket << 16);
924
 
        int cc = xhci_cmd_evaluate_context(pipe->dev);
 
1012
        struct xhci_epctx *ep = (void*)&in[2 << xhci->context64];
 
1013
        ep->ctx[1] |= (pipe->pipe.maxpacket << 16);
 
1014
        int cc = xhci_cmd_evaluate_context(xhci, pipe->slotid, in);
925
1015
        if (cc != CC_SUCCESS) {
926
1016
            dprintf(1, "%s: reconf ctl endpoint: failed (cc %d)\n",
927
1017
                    __func__, cc);
928
1018
        }
 
1019
        free(in);
929
1020
    }
930
1021
    return upipe;
931
1022
}
939
1030
        return -1;
940
1031
    const struct usb_ctrlrequest *req = cmd;
941
1032
    struct xhci_pipe *pipe = container_of(p, struct xhci_pipe, pipe);
942
 
    struct usb_xhci_s *xhci = pipe->dev->xhci;
943
 
    int cc;
944
 
 
945
 
    if (req->bRequest == USB_REQ_SET_ADDRESS) {
946
 
        int slotid = xhci_cmd_enable_slot(xhci);
947
 
        if (slotid < 0) {
948
 
            dprintf(1, "%s: enable slot: failed\n", __func__);
949
 
            return -1;
950
 
        }
951
 
        dprintf(3, "%s: enable slot: got slotid %d\n", __func__, slotid);
952
 
        pipe->dev->slotid = slotid;
953
 
        xhci->devs[slotid].ptr_low = (u32)&pipe->dev->devctx;
954
 
        xhci->devs[slotid].ptr_high = 0;
955
 
 
956
 
        struct usbdevice_s *usbdev = pipe->dev->usbdev;
957
 
        u32 route = 0;
958
 
        while (usbdev->hub->usbdev) {
959
 
            route <<= 4;
960
 
            route |= (usbdev->port+1) & 0xf;
961
 
            usbdev = usbdev->hub->usbdev;
962
 
        }
963
 
        dprintf(3, "%s: root port %d, route 0x%x\n",
964
 
                __func__, usbdev->port+1, route);
965
 
 
966
 
        struct xhci_inctx *in = &pipe->dev->inctx;
967
 
        in->add = 0x03;
968
 
        in->slot.ctx[0]    |= (1 << 27); // context entries
969
 
        in->slot.ctx[0]    |= speed_to_xhci[pipe->dev->usbdev->speed] << 20;
970
 
        in->slot.ctx[0]    |= route;
971
 
        in->slot.ctx[1]    |= (usbdev->port+1) << 16;
972
 
        /* TODO ctx0: hub bit */
973
 
        /* TODO ctx1: hub ports */
974
 
 
975
 
        in->ep[0].ctx[0]   |= (3 << 16); // interval: 1ms
976
 
        in->ep[0].ctx[1]   |= (4 << 3);  // control pipe
977
 
        in->ep[0].ctx[1]   |= (speed_to_ctlsize[pipe->dev->usbdev->speed] << 16);
978
 
 
979
 
        in->ep[0].deq_low  = (u32)&pipe->reqs.ring[0];
980
 
        in->ep[0].deq_low  |= 1;         // dcs
981
 
        in->ep[0].deq_high = 0;
982
 
        in->ep[0].length   = 8;
983
 
 
984
 
        cc = xhci_cmd_address_device(pipe->dev);
985
 
        if (cc != CC_SUCCESS) {
986
 
            dprintf(1, "%s: address device: failed (cc %d)\n", __func__, cc);
987
 
            return -1;
988
 
        }
 
1033
    struct usb_xhci_s *xhci = container_of(
 
1034
        pipe->pipe.cntl, struct usb_xhci_s, usb);
 
1035
 
 
1036
    if (req->bRequest == USB_REQ_SET_ADDRESS)
 
1037
        // Set address command sent during xhci_alloc_pipe.
989
1038
        return 0;
990
 
    }
991
1039
 
992
1040
    xhci_xfer_setup(pipe, req, dir, datalen);
993
1041
    if (datalen)
994
1042
        xhci_xfer_data(pipe, dir, data, datalen);
995
 
    xhci_xfer_status(pipe, dir);
 
1043
    xhci_xfer_status(pipe, dir, datalen);
996
1044
 
997
 
    cc = xhci_event_wait(xhci, &pipe->reqs, 1000);
 
1045
    int cc = xhci_event_wait(xhci, &pipe->reqs, 1000);
998
1046
    if (cc != CC_SUCCESS) {
999
1047
        dprintf(1, "%s: control xfer failed (cc %d)\n", __func__, cc);
1000
1048
        return -1;
1003
1051
    return 0;
1004
1052
}
1005
1053
 
1006
 
int
 
1054
int VISIBLE32FLAT
1007
1055
xhci_send_bulk(struct usb_pipe *p, int dir, void *data, int datalen)
1008
1056
{
1009
1057
    if (!CONFIG_USB_XHCI)
1010
1058
        return -1;
1011
1059
 
1012
1060
    struct xhci_pipe *pipe = container_of(p, struct xhci_pipe, pipe);
1013
 
    struct xhci_device *dev = GET_LOWFLAT(pipe->dev);
1014
 
    struct usb_xhci_s *xhci = GET_LOWFLAT(dev->xhci);
 
1061
    struct usb_xhci_s *xhci = container_of(
 
1062
        GET_LOWFLAT(pipe->pipe.cntl), struct usb_xhci_s, usb);
1015
1063
 
1016
1064
    xhci_xfer_normal(pipe, data, datalen);
1017
1065
    int cc = xhci_event_wait(xhci, &pipe->reqs, 1000);
1022
1070
    return 0;
1023
1071
}
1024
1072
 
1025
 
int
 
1073
int VISIBLE32FLAT
1026
1074
xhci_poll_intr(struct usb_pipe *p, void *data)
1027
1075
{
1028
1076
    if (!CONFIG_USB_XHCI)
1029
1077
        return -1;
1030
1078
 
1031
1079
    struct xhci_pipe *pipe = container_of(p, struct xhci_pipe, pipe);
1032
 
    struct xhci_device *dev = GET_LOWFLAT(pipe->dev);
1033
 
    struct usb_xhci_s *xhci = GET_LOWFLAT(dev->xhci);
 
1080
    struct usb_xhci_s *xhci = container_of(
 
1081
        GET_LOWFLAT(pipe->pipe.cntl), struct usb_xhci_s, usb);
1034
1082
    u32 len = GET_LOWFLAT(pipe->pipe.maxpacket);
1035
1083
    void *buf = GET_LOWFLAT(pipe->buf);
1036
1084
    int bufused = GET_LOWFLAT(pipe->bufused);
1054
1102
    return 0;
1055
1103
}
1056
1104
 
1057
 
int
1058
 
xhci_setup(struct pci_device *pci, int busid)
 
1105
static void
 
1106
xhci_controller_setup(struct pci_device *pci)
1059
1107
{
1060
 
    ASSERT32FLAT();
1061
 
    if (!CONFIG_USB_XHCI)
1062
 
        return -1;
1063
 
 
1064
1108
    struct usb_xhci_s *xhci = malloc_low(sizeof(*xhci));
1065
1109
    if (!xhci) {
1066
1110
        warn_noalloc();
1067
 
        return -1;
 
1111
        return;
1068
1112
    }
1069
1113
    memset(xhci, 0, sizeof(*xhci));
1070
1114
 
 
1115
    wait_preempt();  // Avoid pci_config_readl when preempting
1071
1116
    xhci->baseaddr = pci_config_readl(pci->bdf, PCI_BASE_ADDRESS_0)
1072
1117
        & PCI_BASE_ADDRESS_MEM_MASK;
1073
1118
    xhci->caps  = (void*)(xhci->baseaddr);
1081
1126
    xhci->ports = (hcs1 >> 24) & 0xff;
1082
1127
    xhci->slots = hcs1         & 0xff;
1083
1128
    xhci->xcap  = ((hcc >> 16) & 0xffff) << 2;
 
1129
    xhci->context64 = (hcc & 0x04) ? 1 : 0;
1084
1130
 
1085
 
    xhci->usb.busid = busid;
1086
1131
    xhci->usb.pci = pci;
1087
1132
    xhci->usb.type = USB_TYPE_XHCI;
1088
1133
    xhci->hub.cntl = &xhci->usb;
1089
1134
    xhci->hub.portcount = xhci->ports;
1090
1135
    xhci->hub.op = &xhci_hub_ops;
1091
1136
 
1092
 
    dprintf(1, "XHCI init on dev %02x:%02x.%x: regs @ %p, %d ports, %d slots\n"
 
1137
    dprintf(1, "XHCI init on dev %02x:%02x.%x: regs @ %p, %d ports, %d slots"
 
1138
            ", %d byte contexts\n"
1093
1139
            , pci_bdf_to_bus(pci->bdf), pci_bdf_to_dev(pci->bdf)
1094
1140
            , pci_bdf_to_fn(pci->bdf), xhci->caps
1095
 
            , xhci->ports, xhci->slots);
 
1141
            , xhci->ports, xhci->slots, xhci->context64 ? 64 : 32);
1096
1142
 
1097
1143
    if (xhci->xcap) {
1098
1144
        u32 off, addr = xhci->baseaddr + xhci->xcap;
1103
1149
            case 0x02:
1104
1150
                name  = readl(&xcap->data[0]);
1105
1151
                ports = readl(&xcap->data[1]);
1106
 
                dprintf(1, "XHCI    protocol %c%c%c%c %x.%02x, %d ports (offset %d)\n"
 
1152
                dprintf(1, "XHCI    protocol %c%c%c%c %x.%02x"
 
1153
                        ", %d ports (offset %d), def %x\n"
1107
1154
                        , (name >>  0) & 0xff
1108
1155
                        , (name >>  8) & 0xff
1109
1156
                        , (name >> 16) & 0xff
1111
1158
                        , (cap >> 24) & 0xff
1112
1159
                        , (cap >> 16) & 0xff
1113
1160
                        , (ports >>  8) & 0xff
1114
 
                        , (ports >>  0) & 0xff);
 
1161
                        , (ports >>  0) & 0xff
 
1162
                        , ports >> 16);
1115
1163
                break;
1116
1164
            default:
1117
1165
                dprintf(1, "XHCI    extcap 0x%x @ %x\n", cap & 0xff, addr);
1122
1170
        } while (off > 0);
1123
1171
    }
1124
1172
 
 
1173
    u32 pagesize = readl(&xhci->op->pagesize);
 
1174
    if (PAGE_SIZE != (pagesize<<12)) {
 
1175
        dprintf(1, "XHCI driver does not support page size code %d\n"
 
1176
                , pagesize<<12);
 
1177
        free(xhci);
 
1178
        return;
 
1179
    }
 
1180
 
1125
1181
    pci_config_maskw(pci->bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
1126
1182
 
1127
1183
    run_thread(configure_xhci, xhci);
1128
 
    return 0;
 
1184
}
 
1185
 
 
1186
void
 
1187
xhci_setup(void)
 
1188
{
 
1189
    if (! CONFIG_USB_XHCI)
 
1190
        return;
 
1191
    struct pci_device *pci;
 
1192
    foreachpci(pci) {
 
1193
        if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_XHCI)
 
1194
            xhci_controller_setup(pci);
 
1195
    }
1129
1196
}