0019-grub-core-bus-usb-Add-xhci-support.patch 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815
  1. From 8c9e61e7b0f28a66d0f63c07b10fc6617a709010 Mon Sep 17 00:00:00 2001
  2. From: Patrick Rudolph <patrick.rudolph@9elements.com>
  3. Date: Sun, 15 Nov 2020 19:59:25 +0100
  4. Subject: [PATCH 19/22] grub-core/bus/usb: Add xhci support
  5. Add support for xHCI USB controllers.
  6. The code is based on seabios implementation, but has been heavily
  7. modified to match grubs internals.
  8. Changes done in version 2:
  9. * Code cleanup
  10. * Code style fixes
  11. * Don't leak memory buffers
  12. * Compile without warnings
  13. * Add more defines
  14. * Add more helper functions
  15. * Don't assume a 1:1 virtual to physical mapping
  16. * Flush cachelines after writing buffers
  17. * Don't use hardcoded page size
  18. * Proper scratchpad register setup
  19. * xHCI bios ownership handoff
  20. Changes done in version 3:
  21. * Fixed a race condition detecting events, which doesn't appear on
  22. qemu based xHCI controllers
  23. * Don't accidently disable USB3.0 devices after first command
  24. * Support arbitrary protocol speed IDs
  25. * Coding style cleanup
  26. Tested:
  27. * Qemu system x86_64
  28. * virtual USB HID keyboard (usb-kbd)
  29. * virtual USB HID mass storage (usb-storage)
  30. * init Supermicro X11SSH-F
  31. * iKVM HID keyboard
  32. * USB3 HID mass storage (controller root port)
  33. * USB HID keyboard
  34. TODO:
  35. * Test on more hardware
  36. * Test on USB3 hubs
  37. * Support for USB 3.1 and USB 3.2 controllers
  38. Tested on qemu using coreboot and grub as payload:
  39. qemu-system-x86_64 -M q35 -bios $firmware -device qemu-xhci,id=xhci -accel kvm -m 1024M \
  40. -device usb-storage,drive=thumbdrive,bus=xhci.0,port=3 \
  41. -drive if=none,format=raw,id=thumbdrive,file=ubuntu-20.04.1-desktop-amd64.iso \
  42. -device usb-kbd,bus=xhci.0
  43. Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
  44. Signed-off-by: sylv <sylv@sylv.io>
  45. ---
  46. Makefile.am | 2 +-
  47. grub-core/Makefile.core.def | 7 +
  48. grub-core/bus/usb/xhci-pci.c | 195 +++
  49. grub-core/bus/usb/xhci.c | 2496 ++++++++++++++++++++++++++++++++++
  50. include/grub/usb.h | 4 +
  51. 5 files changed, 2703 insertions(+), 1 deletion(-)
  52. create mode 100644 grub-core/bus/usb/xhci-pci.c
  53. create mode 100644 grub-core/bus/usb/xhci.c
  54. diff --git a/Makefile.am b/Makefile.am
  55. index 43635d5ff..65016f856 100644
  56. --- a/Makefile.am
  57. +++ b/Makefile.am
  58. @@ -434,7 +434,7 @@ if COND_i386_coreboot
  59. FS_PAYLOAD_MODULES ?= $(shell cat grub-core/fs.lst)
  60. default_payload.elf: grub-mkstandalone grub-mkimage FORCE
  61. test -f $@ && rm $@ || true
  62. - pkgdatadir=. ./grub-mkstandalone --grub-mkimage=./grub-mkimage -O i386-coreboot -o $@ --modules='ahci pata ehci uhci ohci usb_keyboard usbms part_msdos ext2 fat at_keyboard part_gpt usbserial_usbdebug cbfs' --install-modules='ls linux search configfile normal cbtime cbls memrw iorw minicmd lsmmap lspci halt reboot hexdump pcidump regexp setpci lsacpi chain test serial multiboot cbmemc linux16 gzio echo help syslinuxcfg xnu $(FS_PAYLOAD_MODULES) password_pbkdf2 $(EXTRA_PAYLOAD_MODULES)' --fonts= --themes= --locales= -d grub-core/ /boot/grub/grub.cfg=$(srcdir)/coreboot.cfg
  63. + pkgdatadir=. ./grub-mkstandalone --grub-mkimage=./grub-mkimage -O i386-coreboot -o $@ --modules='ahci pata xhci ehci uhci ohci usb_keyboard usbms part_msdos ext2 fat at_keyboard part_gpt usbserial_usbdebug cbfs' --install-modules='ls linux search configfile normal cbtime cbls memrw iorw minicmd lsmmap lspci halt reboot hexdump pcidump regexp setpci lsacpi chain test serial multiboot cbmemc linux16 gzio echo help syslinuxcfg xnu $(FS_PAYLOAD_MODULES) password_pbkdf2 $(EXTRA_PAYLOAD_MODULES)' --fonts= --themes= --locales= -d grub-core/ /boot/grub/grub.cfg=$(srcdir)/coreboot.cfg
  64. endif
  65. endif
  66. diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
  67. index 5c1af8682..9d59acd1e 100644
  68. --- a/grub-core/Makefile.core.def
  69. +++ b/grub-core/Makefile.core.def
  70. @@ -667,6 +667,13 @@ module = {
  71. enable = arm_coreboot;
  72. };
  73. +module = {
  74. + name = xhci;
  75. + common = bus/usb/xhci.c;
  76. + pci = bus/usb/xhci-pci.c;
  77. + enable = pci;
  78. +};
  79. +
  80. module = {
  81. name = pci;
  82. common = bus/pci.c;
  83. diff --git a/grub-core/bus/usb/xhci-pci.c b/grub-core/bus/usb/xhci-pci.c
  84. new file mode 100644
  85. index 000000000..a5bd3c97d
  86. --- /dev/null
  87. +++ b/grub-core/bus/usb/xhci-pci.c
  88. @@ -0,0 +1,195 @@
  89. +/* xhci.c - XHCI Support. */
  90. +/*
  91. + * GRUB -- GRand Unified Bootloader
  92. + * Copyright (C) 2020 9elements Cyber Security
  93. + *
  94. + * GRUB is free software: you can redistribute it and/or modify
  95. + * it under the terms of the GNU General Public License as published by
  96. + * the Free Software Foundation, either version 3 of the License, or
  97. + * (at your option) any later version.
  98. + *
  99. + * GRUB is distributed in the hope that it will be useful,
  100. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  101. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  102. + * GNU General Public License for more details.
  103. + *
  104. + * You should have received a copy of the GNU General Public License
  105. + * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  106. + */
  107. +
  108. +#include <grub/pci.h>
  109. +#include <grub/cpu/pci.h>
  110. +#include <grub/cs5536.h>
  111. +#include <grub/misc.h>
  112. +#include <grub/mm.h>
  113. +#include <grub/time.h>
  114. +#include <grub/usb.h>
  115. +
  116. +#define GRUB_XHCI_PCI_SBRN_REG 0x60
  117. +#define GRUB_XHCI_ADDR_MEM_MASK (~0xff)
  118. +
  119. +/* USBLEGSUP bits and related OS OWNED byte offset */
  120. +enum
  121. +{
  122. + GRUB_XHCI_BIOS_OWNED = (1 << 16),
  123. + GRUB_XHCI_OS_OWNED = (1 << 24)
  124. +};
  125. +
  126. +/* PCI iteration function... */
  127. +static int
  128. +grub_xhci_pci_iter (grub_pci_device_t dev, grub_pci_id_t pciid,
  129. + void *data __attribute__ ((unused)))
  130. +{
  131. + volatile grub_uint32_t *regs;
  132. + grub_uint32_t base, base_h;
  133. + grub_uint32_t eecp_offset;
  134. + grub_uint32_t usblegsup = 0;
  135. + grub_uint64_t maxtime;
  136. + grub_uint32_t interf;
  137. + grub_uint32_t subclass;
  138. + grub_uint32_t class;
  139. + grub_uint8_t release;
  140. + grub_uint32_t class_code;
  141. +
  142. + grub_dprintf ("xhci", "XHCI grub_xhci_pci_iter: begin\n");
  143. +
  144. + if (pciid == GRUB_CS5536_PCIID)
  145. + {
  146. + grub_dprintf ("xhci", "CS5536 not supported\n");
  147. + return 0;
  148. + }
  149. + else
  150. + {
  151. + grub_pci_address_t addr;
  152. + addr = grub_pci_make_address (dev, GRUB_PCI_REG_CLASS);
  153. + class_code = grub_pci_read (addr) >> 8;
  154. + interf = class_code & 0xFF;
  155. + subclass = (class_code >> 8) & 0xFF;
  156. + class = class_code >> 16;
  157. +
  158. + /* If this is not an XHCI controller, just return. */
  159. + if (class != 0x0c || subclass != 0x03 || interf != 0x30)
  160. + return 0;
  161. +
  162. + grub_dprintf ("xhci", "XHCI grub_xhci_pci_iter: class OK\n");
  163. +
  164. + /* Check Serial Bus Release Number */
  165. + addr = grub_pci_make_address (dev, GRUB_XHCI_PCI_SBRN_REG);
  166. + release = grub_pci_read_byte (addr);
  167. + if (release != 0x30)
  168. + {
  169. + grub_dprintf ("xhci", "XHCI grub_xhci_pci_iter: Wrong SBRN: %0x\n",
  170. + release);
  171. + return 0;
  172. + }
  173. + grub_dprintf ("xhci", "XHCI grub_xhci_pci_iter: bus rev. num. OK\n");
  174. +
  175. + /* Determine XHCI XHCC registers base address. */
  176. + addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESS_REG0);
  177. + base = grub_pci_read (addr);
  178. + addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESS_REG1);
  179. + base_h = grub_pci_read (addr);
  180. + /* Stop if registers are mapped above 4G - GRUB does not currently
  181. + * work with registers mapped above 4G */
  182. + if (((base & GRUB_PCI_ADDR_MEM_TYPE_MASK) != GRUB_PCI_ADDR_MEM_TYPE_32)
  183. + && (base_h != 0))
  184. + {
  185. + grub_dprintf ("xhci",
  186. + "XHCI grub_xhci_pci_iter: registers above 4G are not supported\n");
  187. + return 0;
  188. + }
  189. + base &= GRUB_PCI_ADDR_MEM_MASK;
  190. + if (!base)
  191. + {
  192. + grub_dprintf ("xhci",
  193. + "XHCI: XHCI is not mapped\n");
  194. + return 0;
  195. + }
  196. +
  197. + /* Set bus master - needed for coreboot, VMware, broken BIOSes etc. */
  198. + addr = grub_pci_make_address (dev, GRUB_PCI_REG_COMMAND);
  199. + grub_pci_write_word(addr,
  200. + GRUB_PCI_COMMAND_MEM_ENABLED
  201. + | GRUB_PCI_COMMAND_BUS_MASTER
  202. + | grub_pci_read_word(addr));
  203. +
  204. + grub_dprintf ("xhci", "XHCI grub_xhci_pci_iter: 32-bit XHCI OK\n");
  205. + }
  206. +
  207. + grub_dprintf ("xhci", "XHCI grub_xhci_pci_iter: iobase of XHCC: %08x\n",
  208. + (base & GRUB_XHCI_ADDR_MEM_MASK));
  209. +
  210. + regs = grub_pci_device_map_range (dev,
  211. + (base & GRUB_XHCI_ADDR_MEM_MASK),
  212. + 0x100);
  213. +
  214. + /* Is there EECP ? */
  215. + eecp_offset = (grub_le_to_cpu32 (regs[2]) >> 8) & 0xff;
  216. +
  217. + /* Determine and change ownership. */
  218. + /* EECP offset valid in HCCPARAMS */
  219. + /* Ownership can be changed via EECP only */
  220. + if (pciid != GRUB_CS5536_PCIID && eecp_offset >= 0x40)
  221. + {
  222. + grub_pci_address_t pciaddr_eecp;
  223. + pciaddr_eecp = grub_pci_make_address (dev, eecp_offset);
  224. +
  225. + usblegsup = grub_pci_read (pciaddr_eecp);
  226. + if (usblegsup & GRUB_XHCI_BIOS_OWNED)
  227. + {
  228. + grub_boot_time ("Taking ownership of XHCI controller");
  229. + grub_dprintf ("xhci",
  230. + "XHCI grub_xhci_pci_iter: XHCI owned by: BIOS\n");
  231. + /* Ownership change - set OS_OWNED bit */
  232. + grub_pci_write (pciaddr_eecp, usblegsup | GRUB_XHCI_OS_OWNED);
  233. + /* Ensure PCI register is written */
  234. + grub_pci_read (pciaddr_eecp);
  235. +
  236. + /* Wait for finish of ownership change, XHCI specification
  237. + * doesn't say how long it can take... */
  238. + maxtime = grub_get_time_ms () + 1000;
  239. + while ((grub_pci_read (pciaddr_eecp) & GRUB_XHCI_BIOS_OWNED)
  240. + && (grub_get_time_ms () < maxtime));
  241. + if (grub_pci_read (pciaddr_eecp) & GRUB_XHCI_BIOS_OWNED)
  242. + {
  243. + grub_dprintf ("xhci",
  244. + "XHCI grub_xhci_pci_iter: XHCI change ownership timeout");
  245. + /* Change ownership in "hard way" - reset BIOS ownership */
  246. + grub_pci_write (pciaddr_eecp, GRUB_XHCI_OS_OWNED);
  247. + /* Ensure PCI register is written */
  248. + grub_pci_read (pciaddr_eecp);
  249. + }
  250. + }
  251. + else if (usblegsup & GRUB_XHCI_OS_OWNED)
  252. + /* XXX: What to do in this case - nothing ? Can it happen ? */
  253. + grub_dprintf ("xhci", "XHCI grub_xhci_pci_iter: XHCI owned by: OS\n");
  254. + else
  255. + {
  256. + grub_dprintf ("xhci",
  257. + "XHCI grub_Xhci_pci_iter: XHCI owned by: NONE\n");
  258. + /* XXX: What to do in this case ? Can it happen ?
  259. + * Is code below correct ? */
  260. + /* Ownership change - set OS_OWNED bit */
  261. + grub_pci_write (pciaddr_eecp, GRUB_XHCI_OS_OWNED);
  262. + /* Ensure PCI register is written */
  263. + grub_pci_read (pciaddr_eecp);
  264. + }
  265. +
  266. + /* Disable SMI, just to be sure. */
  267. + pciaddr_eecp = grub_pci_make_address (dev, eecp_offset + 4);
  268. + grub_pci_write (pciaddr_eecp, 0);
  269. + /* Ensure PCI register is written */
  270. + grub_pci_read (pciaddr_eecp);
  271. + }
  272. +
  273. + grub_dprintf ("xhci", "inithw: XHCI grub_xhci_pci_iter: ownership OK\n");
  274. +
  275. + grub_xhci_init_device (regs);
  276. + return 0;
  277. +}
  278. +
  279. +void
  280. +grub_xhci_pci_scan (void)
  281. +{
  282. + grub_pci_iterate (grub_xhci_pci_iter, NULL);
  283. +}
  284. diff --git a/grub-core/bus/usb/xhci.c b/grub-core/bus/usb/xhci.c
  285. new file mode 100644
  286. index 000000000..f4591ffb5
  287. --- /dev/null
  288. +++ b/grub-core/bus/usb/xhci.c
  289. @@ -0,0 +1,2496 @@
  290. +/* xhci.c - XHCI Support. */
  291. +/*
  292. + * GRUB -- GRand Unified Bootloader
  293. + * Copyright (C) 2020 9elements Cyber Security
  294. + *
  295. + * GRUB is free software: you can redistribute it and/or modify
  296. + * it under the terms of the GNU General Public License as published by
  297. + * the Free Software Foundation, either version 3 of the License, or
  298. + * (at your option) any later version.
  299. + *
  300. + * GRUB is distributed in the hope that it will be useful,
  301. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  302. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  303. + * GNU General Public License for more details.
  304. + *
  305. + * You should have received a copy of the GNU General Public License
  306. + * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  307. + *
  308. + * Big parts of this software are inspired by seabios XHCI implementation
  309. + * Released under LGPLv3. Credits to:
  310. + *
  311. + * Copyright (C) 2013 Gerd Hoffmann <kraxel@redhat.com>
  312. + * Copyright (C) 2014 Kevin O'Connor <kevin@koconnor.net>
  313. + */
  314. +
  315. +#include <grub/dl.h>
  316. +#include <grub/err.h>
  317. +#include <grub/mm.h>
  318. +#include <grub/usb.h>
  319. +#include <grub/usbtrans.h>
  320. +#include <grub/misc.h>
  321. +#include <grub/time.h>
  322. +#include <grub/loader.h>
  323. +#include <grub/disk.h>
  324. +#include <grub/dma.h>
  325. +#include <grub/cache.h>
  326. +#include <grub/i386/cpuid.h>
  327. +
  328. +GRUB_MOD_LICENSE ("GPLv3+");
  329. +
  330. +/* This simple GRUB implementation of XHCI driver */
  331. +/* Based on the specification
  332. + * "eXtensible Host Controller Interface for Universal Serial Bus" Revision 1.2
  333. + */
  334. +
  335. +
  336. +#define xhci_get_field(data, field) \
  337. + (((data) >> field##_SHIFT) & field##_MASK)
  338. +#define XHCI_PORTSC_PLS_MASK 0xf
  339. +#define XHCI_PORTSC_PLS_SHIFT 5
  340. +#define XHCI_PORTSC_SPEED_MASK 0xf
  341. +#define XHCI_PORTSC_SPEED_SHIFT 10
  342. +
  343. +enum
  344. +{
  345. + XHCI_USB_FULLSPEED = 1,
  346. + XHCI_USB_LOWSPEED,
  347. + XHCI_USB_HIGHSPEED,
  348. + XHCI_USB_SUPERSPEED
  349. +};
  350. +
  351. +/* Chapter 5.3 Host Controller Capability Registers */
  352. +struct grub_xhci_caps {
  353. + grub_uint8_t caplength;
  354. + grub_uint8_t reserved_01;
  355. + grub_uint16_t hciversion;
  356. + grub_uint32_t hcsparams1;
  357. + grub_uint32_t hcsparams2;
  358. + grub_uint32_t hcsparams3;
  359. + grub_uint32_t hccparams;
  360. + grub_uint32_t dboff;
  361. + grub_uint32_t rtsoff;
  362. + grub_uint32_t hccparams2;
  363. +} GRUB_PACKED;
  364. +
  365. +/* extended capabilities */
  366. +struct grub_xhci_xcap {
  367. + grub_uint32_t cap;
  368. + grub_uint32_t data[];
  369. +} GRUB_PACKED;
  370. +
  371. +#define XHCI_CAP_LEGACY_SUPPORT 1
  372. +#define XHCI_CAP_SUPPORTED_PROTOCOL 2
  373. +
  374. +struct xhci_portmap {
  375. + grub_uint8_t start;
  376. + grub_uint8_t count;
  377. +} GRUB_PACKED;
  378. +
  379. +struct grub_xhci_op {
  380. + grub_uint32_t usbcmd;
  381. + grub_uint32_t usbsts;
  382. + grub_uint32_t pagesize;
  383. + grub_uint32_t reserved_01[2];
  384. + grub_uint32_t dnctl;
  385. + grub_uint32_t crcr_low;
  386. + grub_uint32_t crcr_high;
  387. + grub_uint32_t reserved_02[4];
  388. + grub_uint32_t dcbaap_low;
  389. + grub_uint32_t dcbaap_high;
  390. + grub_uint32_t config;
  391. +} GRUB_PACKED;
  392. +
  393. +enum
  394. +{
  395. + GRUB_XHCI_CMD_RS = (1<<0),
  396. + GRUB_XHCI_CMD_HCRST = (1<<1),
  397. + GRUB_XHCI_CMD_INTE = (1<<2),
  398. + GRUB_XHCI_CMD_HSEE = (1<<3),
  399. + GRUB_XHCI_CMD_LHCRST = (1<<7),
  400. + GRUB_XHCI_CMD_CSS = (1<<8),
  401. + GRUB_XHCI_CMD_CRS = (1<<9),
  402. + GRUB_XHCI_CMD_EWE = (1<<10),
  403. + GRUB_XHCI_CMD_EU3S = (1<<11)
  404. +};
  405. +
  406. +enum
  407. +{
  408. + GRUB_XHCI_STS_HCH = (1<<0),
  409. + GRUB_XHCI_STS_HSE = (1<<2),
  410. + GRUB_XHCI_STS_EINT = (1<<3),
  411. + GRUB_XHCI_STS_PCD = (1<<4),
  412. + GRUB_XHCI_STS_SSS = (1<<8),
  413. + GRUB_XHCI_STS_RSS = (1<<9),
  414. + GRUB_XHCI_STS_SRE = (1<<10),
  415. + GRUB_XHCI_STS_CNR = (1<<11),
  416. + GRUB_XHCI_STS_HCE = (1<<12)
  417. +};
  418. +
  419. +
  420. +/* Port Registers Offset */
  421. +#define GRUB_XHCI_PR_OFFSET 0x400
  422. +/* Interrupter Registers Offset */
  423. +#define GRUB_XHCI_IR_OFFSET 0x20
  424. +
  425. +/* Port Status and Control registers offsets */
  426. +
  427. +/* Chapter 6 Data Structures */
  428. +#define ALIGN_SPBA 64
  429. +#define ALIGN_DCBAA 64
  430. +#define ALIGN_CMD_RING_SEG 64
  431. +#define ALIGN_EVT_RING_SEG 64
  432. +#define ALIGN_EVT_RING_TABLE 64
  433. +#define ALIGN_TRB 16
  434. +#define ALIGN_INCTX 64
  435. +#define ALIGN_SLOTCTX 32
  436. +
  437. +#define BOUNDARY_RING 0x10000
  438. +
  439. +enum
  440. +{
  441. + GRUB_XHCI_PORTSC_CCS = (1<<0),
  442. + GRUB_XHCI_PORTSC_PED = (1<<1),
  443. + GRUB_XHCI_PORTSC_OCA = (1<<3),
  444. + GRUB_XHCI_PORTSC_PR = (1<<4),
  445. + GRUB_XHCI_PORTSC_PP = (1<<9),
  446. + GRUB_XHCI_PORTSC_SPEED_FULL = (1<<10),
  447. + GRUB_XHCI_PORTSC_SPEED_LOW = (2<<10),
  448. + GRUB_XHCI_PORTSC_SPEED_HIGH = (3<<10),
  449. + GRUB_XHCI_PORTSC_SPEED_SUPER = (4<<10),
  450. + GRUB_XHCI_PORTSC_LWS = (1<<16),
  451. + GRUB_XHCI_PORTSC_CSC = (1<<17),
  452. + GRUB_XHCI_PORTSC_PEC = (1<<18),
  453. + GRUB_XHCI_PORTSC_WRC = (1<<19),
  454. + GRUB_XHCI_PORTSC_OCC = (1<<20),
  455. + GRUB_XHCI_PORTSC_PRC = (1<<21),
  456. + GRUB_XHCI_PORTSC_PLC = (1<<22),
  457. + GRUB_XHCI_PORTSC_CEC = (1<<23),
  458. + GRUB_XHCI_PORTSC_CAS = (1<<24),
  459. + GRUB_XHCI_PORTSC_WCE = (1<<25),
  460. + GRUB_XHCI_PORTSC_WDE = (1<<26),
  461. + GRUB_XHCI_PORTSC_WOE = (1<<27),
  462. + GRUB_XHCI_PORTSC_DR = (1<<30),
  463. + GRUB_XHCI_PORTSC_WPR = (1<<31)
  464. +};
  465. +
  466. +/* XHCI memory data structs */
  467. +#define GRUB_XHCI_MAX_ENDPOINTS 32
  468. +
  469. +#define GRUB_XHCI_RING_ITEMS 128
  470. +#define GRUB_XHCI_RING_SIZE (GRUB_XHCI_RING_ITEMS*sizeof(struct grub_xhci_trb))
  471. +/*
  472. + * xhci_ring structs are allocated with XHCI_RING_SIZE alignment,
  473. + * then we can get it from a trb pointer (provided by evt ring).
  474. + */
  475. +#define XHCI_RING(_trb) \
  476. + ((struct grub_xhci_ring*)((grub_uint32_t)(_trb) & ~(GRUB_XHCI_RING_SIZE-1)))
  477. +
  478. +/* slot context */
  479. +struct grub_xhci_slotctx {
  480. + grub_uint32_t ctx[4];
  481. + grub_uint32_t reserved_01[4];
  482. +} GRUB_PACKED;
  483. +
  484. +/* endpoint context */
  485. +struct grub_xhci_epctx {
  486. + grub_uint32_t ctx[2];
  487. + grub_uint32_t deq_low;
  488. + grub_uint32_t deq_high;
  489. + grub_uint32_t length;
  490. + grub_uint32_t reserved_01[3];
  491. +} GRUB_PACKED;
  492. +
  493. +/* device context array element */
  494. +struct grub_xhci_devlist {
  495. + grub_uint32_t ptr_low;
  496. + grub_uint32_t ptr_high;
  497. +} GRUB_PACKED;
  498. +
  499. +/* input context */
  500. +struct grub_xhci_inctx {
  501. + grub_uint32_t del;
  502. + grub_uint32_t add;
  503. + grub_uint32_t reserved_01[6];
  504. +} GRUB_PACKED;
  505. +
  506. +/* transfer block (ring element) */
  507. +struct grub_xhci_trb {
  508. + grub_uint32_t ptr_low;
  509. + grub_uint32_t ptr_high;
  510. + grub_uint32_t status;
  511. + grub_uint32_t control;
  512. +} GRUB_PACKED;
  513. +
  514. +#define TRB_C (1<<0)
  515. +#define TRB_TYPE_SHIFT 10
  516. +#define TRB_TYPE_MASK 0x3f
  517. +#define TRB_TYPE(t) (((t) >> TRB_TYPE_SHIFT) & TRB_TYPE_MASK)
  518. +
  519. +#define TRB_EV_ED (1<<2)
  520. +
  521. +#define TRB_TR_ENT (1<<1)
  522. +#define TRB_TR_ISP (1<<2)
  523. +#define TRB_TR_NS (1<<3)
  524. +#define TRB_TR_CH (1<<4)
  525. +#define TRB_TR_IOC (1<<5)
  526. +#define TRB_TR_IDT (1<<6)
  527. +#define TRB_TR_TBC_SHIFT 7
  528. +#define TRB_TR_TBC_MASK 0x3
  529. +#define TRB_TR_BEI (1<<9)
  530. +#define TRB_TR_TLBPC_SHIFT 16
  531. +#define TRB_TR_TLBPC_MASK 0xf
  532. +#define TRB_TR_FRAMEID_SHIFT 20
  533. +#define TRB_TR_FRAMEID_MASK 0x7ff
  534. +#define TRB_TR_SIA (1<<31)
  535. +
  536. +#define TRB_TR_DIR (1<<16)
  537. +
  538. +#define TRB_CR_SLOTID_SHIFT 24
  539. +#define TRB_CR_SLOTID_MASK 0xff
  540. +#define TRB_CR_EPID_SHIFT 16
  541. +#define TRB_CR_EPID_MASK 0x1f
  542. +
  543. +#define TRB_CR_BSR (1<<9)
  544. +#define TRB_CR_DC (1<<9)
  545. +
  546. +#define TRB_LK_TC (1<<1)
  547. +
  548. +#define TRB_INTR_SHIFT 22
  549. +#define TRB_INTR_MASK 0x3ff
  550. +#define TRB_INTR(t) (((t).status >> TRB_INTR_SHIFT) & TRB_INTR_MASK)
  551. +
  552. +typedef enum TRBType {
  553. + TRB_RESERVED = 0,
  554. + TR_NORMAL,
  555. + TR_SETUP,
  556. + TR_DATA,
  557. + TR_STATUS,
  558. + TR_ISOCH,
  559. + TR_LINK,
  560. + TR_EVDATA,
  561. + TR_NOOP,
  562. + CR_ENABLE_SLOT,
  563. + CR_DISABLE_SLOT,
  564. + CR_ADDRESS_DEVICE,
  565. + CR_CONFIGURE_ENDPOINT,
  566. + CR_EVALUATE_CONTEXT,
  567. + CR_RESET_ENDPOINT,
  568. + CR_STOP_ENDPOINT,
  569. + CR_SET_TR_DEQUEUE,
  570. + CR_RESET_DEVICE,
  571. + CR_FORCE_EVENT,
  572. + CR_NEGOTIATE_BW,
  573. + CR_SET_LATENCY_TOLERANCE,
  574. + CR_GET_PORT_BANDWIDTH,
  575. + CR_FORCE_HEADER,
  576. + CR_NOOP,
  577. + ER_TRANSFER = 32,
  578. + ER_COMMAND_COMPLETE,
  579. + ER_PORT_STATUS_CHANGE,
  580. + ER_BANDWIDTH_REQUEST,
  581. + ER_DOORBELL,
  582. + ER_HOST_CONTROLLER,
  583. + ER_DEVICE_NOTIFICATION,
  584. + ER_MFINDEX_WRAP,
  585. +} TRBType;
  586. +
  587. +typedef enum TRBCCode {
  588. + CC_INVALID = 0,
  589. + CC_SUCCESS,
  590. + CC_DATA_BUFFER_ERROR,
  591. + CC_BABBLE_DETECTED,
  592. + CC_USB_TRANSACTION_ERROR,
  593. + CC_TRB_ERROR,
  594. + CC_STALL_ERROR,
  595. + CC_RESOURCE_ERROR,
  596. + CC_BANDWIDTH_ERROR,
  597. + CC_NO_SLOTS_ERROR,
  598. + CC_INVALID_STREAM_TYPE_ERROR,
  599. + CC_SLOT_NOT_ENABLED_ERROR,
  600. + CC_EP_NOT_ENABLED_ERROR,
  601. + CC_SHORT_PACKET,
  602. + CC_RING_UNDERRUN,
  603. + CC_RING_OVERRUN,
  604. + CC_VF_ER_FULL,
  605. + CC_PARAMETER_ERROR,
  606. + CC_BANDWIDTH_OVERRUN,
  607. + CC_CONTEXT_STATE_ERROR,
  608. + CC_NO_PING_RESPONSE_ERROR,
  609. + CC_EVENT_RING_FULL_ERROR,
  610. + CC_INCOMPATIBLE_DEVICE_ERROR,
  611. + CC_MISSED_SERVICE_ERROR,
  612. + CC_COMMAND_RING_STOPPED,
  613. + CC_COMMAND_ABORTED,
  614. + CC_STOPPED,
  615. + CC_STOPPED_LENGTH_INVALID,
  616. + CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR = 29,
  617. + CC_ISOCH_BUFFER_OVERRUN = 31,
  618. + CC_EVENT_LOST_ERROR,
  619. + CC_UNDEFINED_ERROR,
  620. + CC_INVALID_STREAM_ID_ERROR,
  621. + CC_SECONDARY_BANDWIDTH_ERROR,
  622. + CC_SPLIT_TRANSACTION_ERROR
  623. +} TRBCCode;
  624. +
  625. +enum {
  626. + PLS_U0 = 0,
  627. + PLS_U1 = 1,
  628. + PLS_U2 = 2,
  629. + PLS_U3 = 3,
  630. + PLS_DISABLED = 4,
  631. + PLS_RX_DETECT = 5,
  632. + PLS_INACTIVE = 6,
  633. + PLS_POLLING = 7,
  634. + PLS_RECOVERY = 8,
  635. + PLS_HOT_RESET = 9,
  636. + PLS_COMPILANCE_MODE = 10,
  637. + PLS_TEST_MODE = 11,
  638. + PLS_RESUME = 15,
  639. +};
  640. +
  641. +/* event ring segment */
  642. +struct grub_xhci_er_seg {
  643. + grub_uint32_t ptr_low;
  644. + grub_uint32_t ptr_high;
  645. + grub_uint32_t size;
  646. + grub_uint32_t reserved_01;
  647. +} GRUB_PACKED;
  648. +
  649. +struct grub_xhci_ring {
  650. + struct grub_xhci_trb ring[GRUB_XHCI_RING_ITEMS];
  651. + struct grub_xhci_trb evt;
  652. + grub_uint32_t eidx;
  653. + grub_uint32_t nidx;
  654. + grub_uint32_t cs;
  655. +};
  656. +
  657. +/* port registers */
  658. +struct grub_xhci_pr {
  659. + grub_uint32_t portsc;
  660. + grub_uint32_t portpmsc;
  661. + grub_uint32_t portli;
  662. + grub_uint32_t reserved_01;
  663. +} GRUB_PACKED;
  664. +
  665. +/* doorbell registers */
  666. +struct grub_xhci_db {
  667. + grub_uint32_t doorbell;
  668. +} GRUB_PACKED;
  669. +
  670. +/* runtime registers */
  671. +struct grub_xhci_rts {
  672. + grub_uint32_t mfindex;
  673. +} GRUB_PACKED;
  674. +
  675. +/* interrupter registers */
  676. +struct grub_xhci_ir {
  677. + grub_uint32_t iman;
  678. + grub_uint32_t imod;
  679. + grub_uint32_t erstsz;
  680. + grub_uint32_t reserved_01;
  681. + grub_uint32_t erstba_low;
  682. + grub_uint32_t erstba_high;
  683. + grub_uint32_t erdp_low;
  684. + grub_uint32_t erdp_high;
  685. +} GRUB_PACKED;
  686. +
  687. +struct grub_xhci_psid {
  688. + grub_uint8_t id;
  689. + grub_uint8_t psie;
  690. + grub_uint16_t psim;
  691. + grub_uint64_t bitrate;
  692. + grub_usb_speed_t grub_usb_speed;
  693. +};
  694. +
  695. +struct grub_xhci_psids {
  696. + grub_uint8_t major;
  697. + grub_uint8_t minor;
  698. + struct grub_xhci_psid psids[16];
  699. +};
  700. +
  701. +struct grub_xhci
  702. +{
  703. + grub_uint8_t shutdown; /* 1 if preparing shutdown of controller */
  704. + /* xhci registers */
  705. + volatile struct grub_xhci_caps *caps; /* Capability registers */
  706. + volatile struct grub_xhci_op *op; /* Operational registers */
  707. + volatile struct grub_xhci_pr *pr; /* Port Registers */
  708. + volatile struct grub_xhci_db *db; /* doorbell */
  709. + volatile struct grub_xhci_ir *ir; /* Interrupt Registers */
  710. + /* devinfo */
  711. + grub_uint32_t xcap;
  712. + grub_uint32_t ports;
  713. + grub_uint32_t slots;
  714. + grub_uint8_t flag64;
  715. + grub_uint16_t spb;
  716. + grub_uint32_t pagesize;
  717. + struct xhci_portmap usb2;
  718. + struct xhci_portmap usb3;
  719. + struct grub_xhci_psids *psids;
  720. + /* xhci data structures */
  721. + struct grub_pci_dma_chunk *devs_dma;
  722. + volatile struct grub_xhci_devlist *devs;
  723. + struct grub_pci_dma_chunk *cmds_dma;
  724. + volatile struct grub_xhci_ring *cmds;
  725. + struct grub_pci_dma_chunk *evts_dma;
  726. + volatile struct grub_xhci_ring *evts;
  727. + struct grub_pci_dma_chunk *eseg_dma;
  728. + volatile struct grub_xhci_er_seg *eseg;
  729. + struct grub_pci_dma_chunk *spba_dma;
  730. + struct grub_pci_dma_chunk *spad_dma;
  731. +
  732. + struct grub_xhci *next;
  733. +};
  734. +
  735. +struct grub_xhci_priv {
  736. + grub_uint8_t slotid;
  737. + grub_uint32_t max_packet;
  738. + struct grub_pci_dma_chunk *enpoint_trbs_dma[32];
  739. + volatile struct grub_xhci_ring *enpoint_trbs[32];
  740. + struct grub_pci_dma_chunk *slotctx_dma;
  741. +};
  742. +
  743. +struct grub_xhci_port {
  744. + grub_uint32_t portsc;
  745. + grub_uint32_t portpmsc;
  746. + grub_uint32_t portli;
  747. + grub_uint32_t reserved_01;
  748. +};
  749. +
  750. +struct grub_xhci_transfer_controller_data {
  751. + grub_uint32_t transfer_size;
  752. +};
  753. +
  754. +static struct grub_xhci *xhci;
  755. +
  756. +/****************************************************************
  757. + * general access functions
  758. + ****************************************************************/
  759. +
  760. +static inline void
  761. +grub_xhci_write32(volatile void *addr, grub_uint32_t val) {
  762. + *(volatile grub_uint32_t *)addr = val;
  763. +}
  764. +static inline void
  765. +grub_xhci_write16(volatile void *addr, grub_uint16_t val) {
  766. + *(volatile grub_uint16_t *)addr = val;
  767. +}
  768. +static inline void
  769. +grub_xhci_write8(void *addr, grub_uint8_t val) {
  770. + *(volatile grub_uint8_t *)addr = val;
  771. +}
  772. +
  773. +static inline grub_uint32_t
  774. +grub_xhci_read32(volatile void *addr) {
  775. + return grub_le_to_cpu32 (*((volatile grub_uint32_t *)addr));
  776. +}
  777. +
  778. +static inline grub_uint16_t
  779. +grub_xhci_read16(volatile void *addr) {
  780. + return grub_le_to_cpu16 (*((volatile grub_uint32_t *)addr));
  781. +}
  782. +static inline grub_uint8_t
  783. +grub_xhci_read8(volatile void *addr) {
  784. + return (*((volatile grub_uint32_t *)addr));
  785. +}
  786. +
  787. +static inline grub_uint32_t
  788. +grub_xhci_port_read (struct grub_xhci *x, grub_uint32_t port)
  789. +{
  790. + return grub_xhci_read32(&x->pr[port].portsc);
  791. +}
  792. +
  793. +static inline void
  794. +grub_xhci_port_write (struct grub_xhci *x, grub_uint32_t port,
  795. + grub_uint32_t and_mask, grub_uint32_t or_mask)
  796. +{
  797. + grub_uint32_t reg = grub_xhci_port_read(x, port);
  798. + reg &= and_mask;
  799. + reg |= or_mask;
  800. +
  801. + grub_xhci_write32(&x->pr[port].portsc, reg);
  802. +}
  803. +
  804. +/****************************************************************
  805. + * xhci status and support functions
  806. + ****************************************************************/
  807. +
  808. +static grub_uint32_t xhci_get_pagesize(struct grub_xhci *x)
  809. +{
  810. + /* Chapter 5.4.3 Page Size Register (PAGESIZE) */
  811. + for (grub_uint8_t i = 0; i < 16; i++)
  812. + {
  813. + if (grub_xhci_read32(&x->op->pagesize) & (1 << i))
  814. + return 1 << (12 + i);
  815. + }
  816. + return 0;
  817. +}
  818. +
  819. +static grub_uint8_t xhci_is_halted(struct grub_xhci *x)
  820. +{
  821. + return !!(grub_xhci_read32(&x->op->usbsts) & 1);
  822. +}
  823. +
  824. +/* Just for debugging */
  825. +static void xhci_check_status(struct grub_xhci *x)
  826. +{
  827. + grub_uint32_t reg;
  828. +
  829. + reg = grub_xhci_read32(&x->op->usbsts);
  830. + if (reg & 1)
  831. + grub_dprintf("xhci", "%s: xHCI halted\n", __func__);
  832. + if (reg & 2)
  833. + grub_dprintf("xhci", "%s: Host system error detected\n", __func__);
  834. + if (reg & (1 << 12))
  835. + grub_dprintf("xhci", "%s: Internal error detected\n", __func__);
  836. + reg = grub_xhci_read32(&x->op->crcr_low);
  837. + if (reg & (1 << 3))
  838. + grub_dprintf("xhci", "%s: Command ring running\n", __func__);
  839. +}
  840. +
  841. +/* xhci_memalign_dma32 allocates DMA memory satisfying alignment and boundary
  842. + * requirements without wasting to much memory */
  843. +static struct grub_pci_dma_chunk *
  844. +xhci_memalign_dma32(grub_size_t align,
  845. + grub_size_t size,
  846. + grub_size_t boundary)
  847. +{
  848. + struct grub_pci_dma_chunk *tmp;
  849. + const grub_uint32_t mask = boundary - 1;
  850. + grub_uint32_t start, end;
  851. +
  852. + /* Allocate some memory and check if it doesn't cross boundary */
  853. + tmp = grub_memalign_dma32(align, size);
  854. + start = grub_dma_get_phys(tmp);
  855. + end = start + size - 1;
  856. + if ((start & mask) == (end & mask))
  857. + return tmp;
  858. + /* Buffer isn't usable, allocate bigger one */
  859. + grub_dma_free(tmp);
  860. +
  861. + return grub_memalign_dma32(boundary, size);
  862. +}
  863. +
  864. +/****************************************************************
  865. + * helper functions for in context DMA buffer
  866. + ****************************************************************/
  867. +
  868. +static int
  869. +grub_xhci_inctx_size(struct grub_xhci *x)
  870. +{
  871. + const grub_uint8_t cnt = GRUB_XHCI_MAX_ENDPOINTS + 1;
  872. + return (sizeof(struct grub_xhci_inctx) * cnt) << x->flag64;
  873. +}
  874. +
  875. +static void
  876. +grub_xhci_inctx_sync_dma_caches(struct grub_xhci *x, struct grub_pci_dma_chunk *inctx)
  877. +{
  878. + grub_arch_sync_dma_caches(inctx, grub_xhci_inctx_size(x));
  879. +}
  880. +
  881. +static struct grub_pci_dma_chunk *
  882. +grub_xhci_alloc_inctx(struct grub_xhci *x, int maxepid,
  883. + struct grub_usb_device *dev)
  884. +{
  885. + int size = grub_xhci_inctx_size(x);
  886. + struct grub_pci_dma_chunk *dma = xhci_memalign_dma32(ALIGN_INCTX, size,
  887. + x->pagesize);
  888. + if (!dma)
  889. + return NULL;
  890. +
  891. + volatile struct grub_xhci_inctx *in = grub_dma_get_virt(dma);
  892. + grub_memset((void *)in, 0, size);
  893. +
  894. + struct grub_xhci_slotctx *slot = (void*)&in[1 << x->flag64];
  895. + slot->ctx[0] |= maxepid << 27; /* context entries */
  896. + grub_dprintf("xhci", "%s: speed=%d root_port=%d\n", __func__, dev->speed, dev->root_port);
  897. + switch (dev->speed) {
  898. + case GRUB_USB_SPEED_FULL:
  899. + slot->ctx[0] |= XHCI_USB_FULLSPEED << 20;
  900. + break;
  901. + case GRUB_USB_SPEED_HIGH:
  902. + slot->ctx[0] |= XHCI_USB_HIGHSPEED << 20;
  903. + break;
  904. + case GRUB_USB_SPEED_LOW:
  905. + slot->ctx[0] |= XHCI_USB_LOWSPEED << 20;
  906. + break;
  907. + case GRUB_USB_SPEED_SUPER:
  908. + slot->ctx[0] |= XHCI_USB_SUPERSPEED << 20;
  909. + break;
  910. + case GRUB_USB_SPEED_NONE:
  911. + slot->ctx[0] |= 0 << 20;
  912. + break;
  913. + }
  914. +
  915. + /* Route is greater zero on devices that are connected to a non root hub */
  916. + if (dev->route)
  917. + {
  918. + /* FIXME: Implement this code for non SuperSpeed hub devices */
  919. + }
  920. + slot->ctx[0] |= dev->route;
  921. + slot->ctx[1] |= (dev->root_port+1) << 16;
  922. +
  923. + grub_arch_sync_dma_caches(in, size);
  924. +
  925. + return dma;
  926. +}
  927. +
  928. +/****************************************************************
  929. + * xHCI event processing
  930. + ****************************************************************/
  931. +
  932. +/* Dequeue events on the XHCI event ring generated by the hardware */
  933. +static void xhci_process_events(struct grub_xhci *x)
  934. +{
  935. + volatile struct grub_xhci_ring *evts = x->evts;
  936. + /* XXX invalidate caches */
  937. +
  938. + for (;;) {
  939. + /* check for event */
  940. + grub_uint32_t nidx = grub_xhci_read32(&evts->nidx);
  941. + grub_uint32_t cs = grub_xhci_read32(&evts->cs);
  942. + volatile struct grub_xhci_trb *etrb = evts->ring + nidx;
  943. + grub_uint32_t control = grub_xhci_read32(&etrb->control);
  944. + if ((control & TRB_C) != (cs ? 1 : 0))
  945. + return;
  946. +
  947. + /* process event */
  948. + grub_uint32_t evt_type = TRB_TYPE(control);
  949. + grub_uint32_t evt_cc = (grub_xhci_read32(&etrb->status) >> 24) & 0xff;
  950. +
  951. + switch (evt_type)
  952. + {
  953. + case ER_TRANSFER:
  954. + case ER_COMMAND_COMPLETE:
  955. + {
  956. + struct grub_xhci_trb *rtrb = (void*)grub_xhci_read32(&etrb->ptr_low);
  957. + struct grub_xhci_ring *ring = XHCI_RING(rtrb);
  958. + volatile struct grub_xhci_trb *evt = &ring->evt;
  959. + grub_uint32_t eidx = rtrb - ring->ring + 1;
  960. + grub_dprintf("xhci", "%s: ring %p [trb %p, evt %p, type %d, eidx %d, cc %d]\n",
  961. + __func__, ring, rtrb, evt, evt_type, eidx, evt_cc);
  962. + *evt = *etrb;
  963. + grub_xhci_write32(&ring->eidx, eidx);
  964. + break;
  965. + }
  966. + case ER_PORT_STATUS_CHANGE:
  967. + {
  968. + /* Nothing to do here. grub_xhci_detect_dev will handle it */
  969. + break;
  970. + }
  971. + default:
  972. + {
  973. + grub_dprintf("xhci", "%s: unknown event, type %d, cc %d\n",
  974. + __func__, evt_type, evt_cc);
  975. + break;
  976. + }
  977. + }
  978. +
  979. + /* move ring index, notify xhci */
  980. + nidx++;
  981. + if (nidx == GRUB_XHCI_RING_ITEMS)
  982. + {
  983. + nidx = 0;
  984. + cs = cs ? 0 : 1;
  985. + grub_xhci_write32(&evts->cs, cs);
  986. + }
  987. + grub_xhci_write32(&evts->nidx, nidx);
  988. + volatile struct grub_xhci_ir *ir = x->ir;
  989. + grub_uint32_t erdp = (grub_uint32_t)(evts->ring + nidx);
  990. + grub_xhci_write32(&ir->erdp_low, erdp);
  991. + grub_xhci_write32(&ir->erdp_high, 0);
  992. + }
  993. +}
  994. +
  995. +/****************************************************************
  996. + * TRB handling
  997. + ****************************************************************/
  998. +
  999. +/* Signal the hardware to process events on a TRB ring */
  1000. +static void xhci_doorbell(struct grub_xhci *x, grub_uint32_t slotid, grub_uint32_t value)
  1001. +{
  1002. + xhci_check_status(x);
  1003. + grub_dprintf("xhci", "%s: slotid %d, epid %d\n", __func__, slotid, value);
  1004. + grub_xhci_write32(&x->db[slotid].doorbell, value);
  1005. +}
  1006. +
  1007. +/* Check if a ring has any pending TRBs */
  1008. +static int xhci_ring_busy(volatile struct grub_xhci_ring *ring)
  1009. +{
  1010. + grub_uint32_t eidx = grub_xhci_read32(&ring->eidx);
  1011. + grub_uint32_t nidx = grub_xhci_read32(&ring->nidx);
  1012. +
  1013. + return (eidx != nidx);
  1014. +}
  1015. +
  1016. +/* Returns free space in ring */
  1017. +static int xhci_ring_free_space(volatile struct grub_xhci_ring *ring)
  1018. +{
  1019. + grub_uint32_t eidx = grub_xhci_read32(&ring->eidx);
  1020. + grub_uint32_t nidx = grub_xhci_read32(&ring->nidx);
  1021. +
  1022. + /* nidx is never 0, so reduce ring buffer size by one */
  1023. + return (eidx > nidx) ? eidx-nidx
  1024. + : (ARRAY_SIZE(ring->ring) - 1) - nidx + eidx;
  1025. +}
  1026. +
  1027. +/* Check if a ring is full */
  1028. +static int xhci_ring_full(volatile struct grub_xhci_ring *ring)
  1029. +{
  1030. + /* Might need to insert one link TRB */
  1031. + return xhci_ring_free_space(ring) <= 1;
  1032. +}
  1033. +
  1034. +/* Check if a ring is almost full */
  1035. +static int xhci_ring_almost_full(volatile struct grub_xhci_ring *ring)
  1036. +{
  1037. + /* Might need to insert one link TRB */
  1038. + return xhci_ring_free_space(ring) <= 2;
  1039. +}
  1040. +
  1041. +/* Wait for a ring to empty (all TRBs processed by hardware) */
  1042. +static int xhci_event_wait(struct grub_xhci *x,
  1043. + volatile struct grub_xhci_ring *ring,
  1044. + grub_uint32_t timeout)
  1045. +{
  1046. + grub_uint32_t end = grub_get_time_ms () + timeout;
  1047. +
  1048. + for (;;)
  1049. + {
  1050. + xhci_check_status(x);
  1051. + xhci_process_events(x);
  1052. + if (!xhci_ring_busy(ring))
  1053. + {
  1054. + grub_uint32_t status = ring->evt.status;
  1055. + return (status >> 24) & 0xff;
  1056. + }
  1057. + if (grub_get_time_ms () > end)
  1058. + {
  1059. + xhci_check_status(x);
  1060. + grub_dprintf("xhci", "%s: Timeout waiting for event\n", __func__);
  1061. + return -1;
  1062. + }
  1063. + }
  1064. +}
  1065. +
  1066. +/* Add a TRB to the given ring, either regular or inline */
  1067. +static void xhci_trb_fill(volatile struct grub_xhci_ring *ring,
  1068. + grub_uint64_t ptr, grub_uint32_t xferlen,
  1069. + grub_uint32_t flags)
  1070. +{
  1071. + volatile struct grub_xhci_trb *dst = &ring->ring[ring->nidx];
  1072. + dst->ptr_low = ptr & 0xffffffff;
  1073. + dst->ptr_high = ptr >> 32;
  1074. + dst->status = xferlen;
  1075. + dst->control = flags | (ring->cs ? TRB_C : 0);
  1076. +
  1077. + grub_arch_sync_dma_caches(dst, sizeof(ring->ring[0]));
  1078. +}
  1079. +
  1080. +/*
  1081. + * Queue a TRB onto a ring.
  1082. + *
  1083. + * The caller must pass a pointer to the data in physical address-space or the
  1084. + * data itself (but no more than 8 bytes) in data_or_addr. Inline data must have
  1085. + * the flag TRB_TR_IDT set.
  1086. + */
  1087. +static void xhci_trb_queue(volatile struct grub_xhci_ring *ring,
  1088. + grub_uint64_t data_or_addr,
  1089. + grub_uint32_t xferlen, grub_uint32_t flags)
  1090. +{
  1091. + grub_dprintf("xhci", "%s: ring %p data %llx len %d flags 0x%x remain 0x%x\n", __func__,
  1092. + ring, data_or_addr, xferlen & 0x1ffff, flags, xferlen >> 17);
  1093. +
  1094. + if (xhci_ring_full(ring))
  1095. + {
  1096. + grub_dprintf("xhci", "%s: ERROR: ring %p is full, discarding TRB\n",
  1097. + __func__, ring);
  1098. + return;
  1099. + }
  1100. +
  1101. + if (ring->nidx >= ARRAY_SIZE(ring->ring) - 1)
  1102. + {
  1103. + /* Reset to command buffer pointer to the first element */
  1104. + xhci_trb_fill(ring, (grub_addr_t)ring->ring, 0, (TR_LINK << 10) | TRB_LK_TC);
  1105. + ring->nidx = 0;
  1106. + ring->cs ^= 1;
  1107. + grub_dprintf("xhci", "%s: ring %p [linked]\n", __func__, ring);
  1108. + }
  1109. +
  1110. + xhci_trb_fill(ring, data_or_addr, xferlen, flags);
  1111. + ring->nidx++;
  1112. + grub_dprintf("xhci", "%s: ring %p [nidx %d, len %d]\n",
  1113. + __func__, ring, ring->nidx, xferlen);
  1114. +}
  1115. +
  1116. +/*
  1117. + * Queue a TRB onto a ring and flush it if necessary.
  1118. + *
  1119. + * The caller must pass a pointer to the data in physical address-space or the
  1120. + * data itself (but no more than 8 bytes) in data_or_addr. Inline data must have
  1121. + * the flag TRB_TR_IDT set.
  1122. + */
  1123. +static int xhci_trb_queue_and_flush(struct grub_xhci *x,
  1124. + grub_uint32_t slotid,
  1125. + grub_uint32_t epid,
  1126. + volatile struct grub_xhci_ring *ring,
  1127. + grub_uint64_t data_or_addr,
  1128. + grub_uint32_t xferlen, grub_uint32_t flags)
  1129. +{
  1130. + grub_uint8_t submit = 0;
  1131. + if (xhci_ring_almost_full(ring))
  1132. + {
  1133. + grub_dprintf("xhci", "%s: almost full e %d n %d\n", __func__, ring->eidx, ring->nidx);
  1134. + flags |= TRB_TR_IOC;
  1135. + submit = 1;
  1136. + }
  1137. + /* Note: xhci_trb_queue might queue on or two elements, if the end of the TRB
  1138. + * has been reached. The caller must account for that when filling the TRB. */
  1139. + xhci_trb_queue(ring, data_or_addr, xferlen, flags);
  1140. + /* Submit if less no free slot is remaining, we might need an additional
  1141. + * one on the next call to this function. */
  1142. + if (submit)
  1143. + {
  1144. + xhci_doorbell(x, slotid, epid);
  1145. + int rc = xhci_event_wait(x, ring, 1000);
  1146. + grub_dprintf("xhci", "%s: xhci_event_wait = %d\n", __func__, rc);
  1147. + return rc;
  1148. + }
  1149. + return 0;
  1150. +}
  1151. +
  1152. +/****************************************************************
  1153. + * xHCI command functions
  1154. + ****************************************************************/
  1155. +
  1156. +/* Submit a command to the xHCI command TRB */
  1157. +static int xhci_cmd_submit(struct grub_xhci *x,
  1158. + struct grub_pci_dma_chunk *inctx_dma,
  1159. + grub_uint32_t flags)
  1160. +{
  1161. + volatile struct grub_xhci_inctx *inctx;
  1162. + /* Don't submit if halted, it will fail */
  1163. + if (xhci_is_halted(x))
  1164. + return -1;
  1165. +
  1166. + if (inctx_dma)
  1167. + {
  1168. + grub_xhci_inctx_sync_dma_caches(x, inctx_dma);
  1169. +
  1170. + inctx = grub_dma_get_virt(inctx_dma);
  1171. +
  1172. + struct grub_xhci_slotctx *slot = (void*)&inctx[1 << x->flag64];
  1173. + grub_uint32_t port = ((slot->ctx[1] >> 16) & 0xff) - 1;
  1174. + grub_uint32_t portsc = grub_xhci_port_read(x, port);
  1175. + if (!(portsc & GRUB_XHCI_PORTSC_CCS))
  1176. + {
  1177. + grub_dprintf("xhci", "%s: root port %d no longer connected\n",
  1178. + __func__, port);
  1179. + return -1;
  1180. + }
  1181. + xhci_trb_queue(x->cmds, grub_dma_get_phys(inctx_dma), 0, flags);
  1182. + }
  1183. + else
  1184. + {
  1185. + xhci_trb_queue(x->cmds, 0, 0, flags);
  1186. + }
  1187. +
  1188. + xhci_doorbell(x, 0, 0);
  1189. + int rc = xhci_event_wait(x, x->cmds, 1000);
  1190. + grub_dprintf("xhci", "%s: xhci_event_wait = %d\n", __func__, rc);
  1191. +
  1192. + return rc;
  1193. +}
  1194. +
  1195. +static int xhci_cmd_enable_slot(struct grub_xhci *x)
  1196. +{
  1197. + grub_uint32_t flags = 0;
  1198. + flags |= (CR_ENABLE_SLOT << 10);
  1199. +
  1200. + grub_dprintf("xhci", "%s:\n", __func__);
  1201. + int cc = xhci_cmd_submit(x, NULL, flags);
  1202. + if (cc != CC_SUCCESS)
  1203. + return -1;
  1204. + grub_dprintf("xhci", "%s: %p\n", __func__, &x->cmds->evt.control);
  1205. + grub_dprintf("xhci", "%s: %x\n", __func__, grub_xhci_read32(&x->cmds->evt.control));
  1206. +
  1207. + return (grub_xhci_read32(&x->cmds->evt.control) >> 24) & 0xff;
  1208. +}
  1209. +
  1210. +static int xhci_cmd_disable_slot(struct grub_xhci *x, grub_uint32_t slotid)
  1211. +{
  1212. + grub_uint32_t flags = 0;
  1213. + flags |= (CR_DISABLE_SLOT << 10);
  1214. + flags |= (slotid << 24);
  1215. +
  1216. + grub_dprintf("xhci", "%s: slotid %d\n", __func__, slotid);
  1217. + return xhci_cmd_submit(x, NULL, flags);
  1218. +}
  1219. +
  1220. +static int xhci_cmd_stop_endpoint(struct grub_xhci *x, grub_uint32_t slotid
  1221. + , grub_uint32_t epid
  1222. + , grub_uint32_t suspend)
  1223. +{
  1224. + grub_uint32_t flags = 0;
  1225. + flags |= (CR_STOP_ENDPOINT << 10);
  1226. + flags |= (epid << 16);
  1227. + flags |= (suspend << 23) ;
  1228. + flags |= (slotid << 24);
  1229. +
  1230. + return xhci_cmd_submit(x, NULL, flags);
  1231. +}
  1232. +
  1233. +static int xhci_cmd_reset_endpoint(struct grub_xhci *x, grub_uint32_t slotid
  1234. + , grub_uint32_t epid
  1235. + , grub_uint32_t preserve)
  1236. +{
  1237. + grub_uint32_t flags = 0;
  1238. + flags |= (preserve << 9);
  1239. + flags |= (CR_RESET_ENDPOINT << 10);
  1240. + flags |= (epid << 16);
  1241. + flags |= (slotid << 24);
  1242. +
  1243. + return xhci_cmd_submit(x, NULL, flags);
  1244. +}
  1245. +
  1246. +static int xhci_cmd_set_dequeue_pointer(struct grub_xhci *x, grub_uint32_t slotid
  1247. + , grub_uint32_t epid
  1248. + , grub_addr_t tr_deque_pointer)
  1249. +{
  1250. + grub_uint32_t flags = 0;
  1251. + flags |= (CR_SET_TR_DEQUEUE << 10);
  1252. + flags |= (epid << 16);
  1253. + flags |= (slotid << 24);
  1254. +
  1255. + xhci_trb_queue(x->cmds, tr_deque_pointer, 0, flags);
  1256. +
  1257. + xhci_doorbell(x, 0, 0);
  1258. + int rc = xhci_event_wait(x, x->cmds, 1000);
  1259. + grub_dprintf("xhci", "%s: xhci_event_wait = %d\n", __func__, rc);
  1260. +
  1261. + return rc;
  1262. +}
  1263. +
  1264. +static int xhci_cmd_address_device(struct grub_xhci *x, grub_uint32_t slotid,
  1265. + struct grub_pci_dma_chunk *inctx_dma)
  1266. +{
  1267. + grub_uint32_t flags = 0;
  1268. + flags |= (CR_ADDRESS_DEVICE << 10);
  1269. + flags |= (slotid << 24);
  1270. +
  1271. + grub_dprintf("xhci", "%s: slotid %d\n", __func__, slotid);
  1272. + return xhci_cmd_submit(x, inctx_dma, flags);
  1273. +}
  1274. +
  1275. +static int xhci_cmd_configure_endpoint(struct grub_xhci *x, grub_uint32_t slotid,
  1276. + struct grub_pci_dma_chunk *inctx_dma)
  1277. +{
  1278. + grub_uint32_t flags = 0;
  1279. + flags |= (CR_CONFIGURE_ENDPOINT << 10);
  1280. + flags |= (slotid << 24);
  1281. +
  1282. + grub_dprintf("xhci", "%s: slotid %d\n", __func__, slotid);
  1283. + return xhci_cmd_submit(x, inctx_dma, flags);
  1284. +}
  1285. +
  1286. +static int xhci_cmd_evaluate_context(struct grub_xhci *x, grub_uint32_t slotid,
  1287. + struct grub_pci_dma_chunk *inctx_dma)
  1288. +{
  1289. + grub_uint32_t flags = 0;
  1290. + flags |= (CR_EVALUATE_CONTEXT << 10);
  1291. + flags |= (slotid << 24);
  1292. +
  1293. + grub_dprintf("xhci", "%s: slotid %d\n", __func__, slotid);
  1294. + return xhci_cmd_submit(x, inctx_dma, flags);
  1295. +}
  1296. +
  1297. +/****************************************************************
  1298. + * xHCI host controller initialization
  1299. + ****************************************************************/
  1300. +
  1301. +static grub_usb_err_t
  1302. +grub_xhci_reset (struct grub_xhci *x)
  1303. +{
  1304. + grub_uint32_t reg;
  1305. + grub_uint32_t end;
  1306. +
  1307. + reg = grub_xhci_read32(&x->op->usbcmd);
  1308. + if (reg & GRUB_XHCI_CMD_RS)
  1309. + {
  1310. + reg &= ~GRUB_XHCI_CMD_RS;
  1311. + grub_xhci_write32(&x->op->usbcmd, reg);
  1312. +
  1313. + end = grub_get_time_ms () + 32;
  1314. + while (grub_xhci_read32(&x->op->usbcmd) & GRUB_XHCI_STS_HCH)
  1315. + {
  1316. + if (grub_get_time_ms () > end)
  1317. + return GRUB_USB_ERR_TIMEOUT;
  1318. +
  1319. + grub_millisleep(1);
  1320. + }
  1321. + }
  1322. +
  1323. + grub_dprintf("xhci", "grub_xhci_reset: resetting HC\n");
  1324. + grub_xhci_write32(&x->op->usbcmd, GRUB_XHCI_CMD_HCRST);
  1325. +
  1326. + /* Wait for device to complete reset and be enabled */
  1327. + end = grub_get_time_ms () + 100;
  1328. + while (grub_xhci_read32(&x->op->usbcmd) & GRUB_XHCI_CMD_HCRST)
  1329. + {
  1330. + if (grub_get_time_ms () > end)
  1331. + return GRUB_USB_ERR_TIMEOUT;
  1332. +
  1333. + grub_millisleep(1);
  1334. + }
  1335. +
  1336. + /* Wait for device to complete reset and be enabled */
  1337. + end = grub_get_time_ms () + 100;
  1338. + while (grub_xhci_read32(&x->op->usbsts) & GRUB_XHCI_STS_CNR)
  1339. + {
  1340. + if (grub_get_time_ms () > end)
  1341. + return GRUB_USB_ERR_TIMEOUT;
  1342. +
  1343. + grub_millisleep(1);
  1344. + }
  1345. +
  1346. + grub_xhci_write32(&x->op->config, x->slots);
  1347. + grub_xhci_write32(&x->op->dcbaap_low, grub_dma_get_phys(x->devs_dma));
  1348. + grub_xhci_write32(&x->op->dcbaap_high, 0);
  1349. + grub_xhci_write32(&x->op->crcr_low, grub_dma_get_phys(x->cmds_dma)| 1);
  1350. + grub_xhci_write32(&x->op->crcr_high, 0);
  1351. + x->cmds->cs = 1;
  1352. +
  1353. + grub_arch_sync_dma_caches(x->cmds, sizeof(*x->cmds));
  1354. +
  1355. + x->eseg->ptr_low = grub_dma_get_phys(x->evts_dma);
  1356. + x->eseg->ptr_high = 0;
  1357. + x->eseg->size = GRUB_XHCI_RING_ITEMS;
  1358. +
  1359. + grub_arch_sync_dma_caches(x->eseg, sizeof(*x->eseg));
  1360. +
  1361. + grub_xhci_write32(&x->ir->erstsz, 1);
  1362. + grub_xhci_write32(&x->ir->erdp_low, grub_dma_get_phys(x->evts_dma));
  1363. + grub_xhci_write32(&x->ir->erdp_high, 0);
  1364. + grub_xhci_write32(&x->ir->erstba_low, grub_dma_get_phys(x->eseg_dma));
  1365. + grub_xhci_write32(&x->ir->erstba_high, 0);
  1366. + x->evts->cs = 1;
  1367. +
  1368. + grub_arch_sync_dma_caches(x->evts, sizeof(*x->eseg));
  1369. +
  1370. + xhci_check_status(x);
  1371. +
  1372. + grub_dprintf ("xhci", "XHCI OP COMMAND: %08x\n",
  1373. + grub_xhci_read32 (&x->op->usbcmd));
  1374. + grub_dprintf ("xhci", "XHCI OP STATUS: %08x\n",
  1375. + grub_xhci_read32 (&x->op->usbsts));
  1376. + grub_dprintf ("xhci", "XHCI OP PAGESIZE: %08x\n",
  1377. + grub_xhci_read32 (&x->op->pagesize));
  1378. + grub_dprintf ("xhci", "XHCI OP DNCTRL: %08x\n",
  1379. + grub_xhci_read32 (&x->op->dnctl));
  1380. + grub_dprintf ("xhci", "XHCI OP CRCR_LOW: %08x\n",
  1381. + grub_xhci_read32 (&x->op->crcr_low));
  1382. + grub_dprintf ("xhci", "XHCI OP CRCR_HIGH: %08x\n",
  1383. + grub_xhci_read32 (&x->op->crcr_high));
  1384. + grub_dprintf ("xhci", "XHCI OP DCBAAP_LOW: %08x\n",
  1385. + grub_xhci_read32 (&x->op->dcbaap_low));
  1386. + grub_dprintf ("xhci", "XHCI OP DCBAAP_HIGH: %08x\n",
  1387. + grub_xhci_read32 (&x->op->dcbaap_high));
  1388. + grub_dprintf ("xhci", "XHCI OP CONFIG: %08x\n",
  1389. + grub_xhci_read32 (&x->op->config));
  1390. + grub_dprintf ("xhci", "XHCI IR ERSTSZ: %08x\n",
  1391. + grub_xhci_read32 (&x->ir->erstsz));
  1392. + grub_dprintf ("xhci", "XHCI IR ERDP: %08x\n",
  1393. + grub_xhci_read32 (&x->ir->erdp_low));
  1394. + grub_dprintf ("xhci", "XHCI IR ERSTBA: %08x\n",
  1395. + grub_xhci_read32 (&x->ir->erstba_low));
  1396. +
  1397. + xhci_check_status(x);
  1398. +
  1399. + return GRUB_USB_ERR_NONE;
  1400. +}
  1401. +
  1402. +static grub_usb_err_t
  1403. +grub_xhci_request_legacy_handoff(volatile struct grub_xhci_xcap *xcap)
  1404. +{
  1405. + grub_uint32_t end;
  1406. +
  1407. + end = grub_get_time_ms () + 10;
  1408. + for (;;)
  1409. + {
  1410. + grub_uint32_t cap = grub_xhci_read32(&xcap->cap);
  1411. + if (cap & (1 << 16))
  1412. + grub_xhci_write32(&xcap->cap, cap | (1 << 24));
  1413. + else
  1414. + break;
  1415. +
  1416. + if (grub_get_time_ms () > end)
  1417. + {
  1418. + grub_dprintf ("xhci","ERROR: %s TIMEOUT\n", __func__);
  1419. + return GRUB_USB_ERR_TIMEOUT;
  1420. + }
  1421. + grub_millisleep(1);
  1422. + }
  1423. + return GRUB_USB_ERR_NONE;
  1424. +}
  1425. +
  1426. +static void
  1427. +grub_xhci_fill_default_speed_mapping(struct grub_xhci_psids *ids)
  1428. +{
  1429. + /* Chapter 7.2.2.1.1 "Default USB Speed ID Mapping" */
  1430. + ids->psids[0].id = 1;
  1431. + ids->psids[0].psie = 2;
  1432. + ids->psids[0].psim = 12;
  1433. + ids->psids[1].id = 2;
  1434. + ids->psids[1].psie = 1;
  1435. + ids->psids[1].psim = 1500;
  1436. + ids->psids[2].id = 3;
  1437. + ids->psids[2].psie = 2;
  1438. + ids->psids[2].psim = 480;
  1439. + ids->psids[3].id = 4;
  1440. + ids->psids[3].psie = 3;
  1441. + ids->psids[3].psim = 5;
  1442. + ids->psids[4].id = 5;
  1443. + ids->psids[4].psie = 3;
  1444. + ids->psids[4].psim = 10;
  1445. + ids->psids[5].id = 6;
  1446. + ids->psids[5].psie = 3;
  1447. + ids->psids[5].psim = 10;
  1448. + ids->psids[6].id = 7;
  1449. + ids->psids[6].psie = 3;
  1450. + ids->psids[6].psim = 20;
  1451. +}
  1452. +
  1453. +static void
  1454. +grub_xhci_calc_speed_mapping(struct grub_xhci_psids *ids)
  1455. +{
  1456. + const grub_uint64_t mult[4] = {1ULL, 1000ULL, 1000000ULL, 1000000000ULL};
  1457. +
  1458. + for (grub_uint8_t i = 0; i < 16; i++)
  1459. + {
  1460. + if (ids->psids[i].id == 0)
  1461. + continue;
  1462. + ids->psids[i].bitrate = mult[ids->psids[i].psie & 3] * (grub_uint64_t)ids->psids[i].psim;
  1463. + if (ids->psids[i].bitrate < 12000000ULL)
  1464. + ids->psids[i].grub_usb_speed = GRUB_USB_SPEED_LOW;
  1465. + else if (ids->psids[i].bitrate < 480000000ULL)
  1466. + ids->psids[i].grub_usb_speed = GRUB_USB_SPEED_FULL;
  1467. + else if (ids->psids[i].bitrate > 1200000000ULL)
  1468. + ids->psids[i].grub_usb_speed = GRUB_USB_SPEED_SUPER;
  1469. + else
  1470. + ids->psids[i].grub_usb_speed = GRUB_USB_SPEED_HIGH;
  1471. + }
  1472. +}
  1473. +
  1474. +
  1475. +/* PCI iteration function... */
  1476. +void
  1477. +grub_xhci_init_device (volatile void *regs)
  1478. +{
  1479. + struct grub_xhci *x;
  1480. + grub_uint32_t hcs1, hcc, reg;
  1481. +
  1482. + /* Allocate memory for the controller and fill basic values. */
  1483. + x = grub_zalloc (sizeof (*x));
  1484. + if (!x)
  1485. + {
  1486. + grub_dprintf("xhci", "Failed to allocate memory\n");
  1487. + return;
  1488. + }
  1489. + x->caps = (volatile struct grub_xhci_caps *) regs;
  1490. + x->op = (volatile struct grub_xhci_op *) (((grub_uint8_t *)regs) +
  1491. + grub_xhci_read8(&x->caps->caplength));
  1492. + x->pr = (volatile struct grub_xhci_pr *) (((grub_uint8_t *)x->op) +
  1493. + GRUB_XHCI_PR_OFFSET);
  1494. + x->db = (volatile struct grub_xhci_db *) (((grub_uint8_t *)regs) +
  1495. + grub_xhci_read32(&x->caps->dboff));
  1496. + x->ir = (volatile struct grub_xhci_ir *) (((grub_uint8_t *)regs) +
  1497. + grub_xhci_read32(&x->caps->rtsoff) + GRUB_XHCI_IR_OFFSET);
  1498. +
  1499. + grub_dprintf ("xhci", "XHCI init: CAPLENGTH: 0x%02x\n",
  1500. + grub_xhci_read8 (&x->caps->caplength));
  1501. + grub_dprintf ("xhci", "XHCI init: HCIVERSION: 0x%04x\n",
  1502. + grub_xhci_read16 (&x->caps->hciversion));
  1503. + grub_dprintf ("xhci", "XHCI init: HCSPARAMS1: 0x%08x\n",
  1504. + grub_xhci_read32 (&x->caps->hcsparams1));
  1505. + grub_dprintf ("xhci", "XHCI init: HCSPARAMS2: 0x%08x\n",
  1506. + grub_xhci_read32 (&x->caps->hcsparams2));
  1507. + grub_dprintf ("xhci", "XHCI init: HCSPARAMS3: 0x%08x\n",
  1508. + grub_xhci_read32 (&x->caps->hcsparams3));
  1509. + grub_dprintf ("xhci", "XHCI init: HCCPARAMS: 0x%08x\n",
  1510. + grub_xhci_read32 (&x->caps->hcsparams3));
  1511. + grub_dprintf ("xhci", "XHCI init: DBOFF: 0x%08x\n",
  1512. + grub_xhci_read32 (&x->caps->dboff));
  1513. + grub_dprintf ("xhci", "XHCI init: RTOFF: 0x%08x\n",
  1514. + grub_xhci_read32 (&x->caps->rtsoff));
  1515. +
  1516. + hcs1 = grub_xhci_read32(&x->caps->hcsparams1);
  1517. + hcc = grub_xhci_read32(&x->caps->hccparams);
  1518. + x->ports = (grub_uint32_t) ((hcs1 >> 24) & 0xff);
  1519. + x->slots = (grub_uint32_t) (hcs1 & 0xff);
  1520. + x->xcap = (grub_uint32_t) ((hcc >> 16) & 0xffff) * sizeof(grub_uint32_t);
  1521. + x->flag64 = (grub_uint8_t) ((hcc & 0x04) ? 1 : 0);
  1522. + grub_dprintf("xhci", "XHCI init: %d ports, %d slots, %d byte contexts\n"
  1523. + , x->ports, x->slots, x->flag64 ? 64 : 32);
  1524. +
  1525. + x->psids = grub_zalloc (sizeof (struct grub_xhci_psids) * x->ports);
  1526. + if (x->xcap)
  1527. + {
  1528. + grub_uint32_t off;
  1529. + volatile grub_uint8_t *addr = (grub_uint8_t *) x->caps + x->xcap;
  1530. + do
  1531. + {
  1532. + volatile struct grub_xhci_xcap *xcap = (void *)addr;
  1533. + grub_uint32_t ports, name, cap = grub_xhci_read32(&xcap->cap);
  1534. + switch (cap & 0xff) {
  1535. + case XHCI_CAP_LEGACY_SUPPORT:
  1536. + {
  1537. + if (grub_xhci_request_legacy_handoff(xcap) != GRUB_USB_ERR_NONE)
  1538. + {
  1539. + grub_dprintf("xhci", "XHCI init: Failed to get xHCI ownership\n");
  1540. + goto fail;
  1541. + }
  1542. + break;
  1543. + }
  1544. + case XHCI_CAP_SUPPORTED_PROTOCOL:
  1545. + {
  1546. + name = grub_xhci_read32(&xcap->data[0]);
  1547. + ports = grub_xhci_read32(&xcap->data[1]);
  1548. + const grub_uint8_t major = (cap >> 24) & 0xff;
  1549. + const grub_uint8_t minor = (cap >> 16) & 0xff;
  1550. + const grub_uint8_t psic = (ports >> 28) & 0xf;
  1551. + const grub_uint8_t count = (ports >> 8) & 0xff;
  1552. + const grub_uint8_t start = (ports >> 0) & 0xff;
  1553. + grub_dprintf("xhci", "XHCI init: protocol %c%c%c%c %x.%02x"
  1554. + ", %d ports (offset %d), def %x, psic %d\n"
  1555. + , (name >> 0) & 0xff
  1556. + , (name >> 8) & 0xff
  1557. + , (name >> 16) & 0xff
  1558. + , (name >> 24) & 0xff
  1559. + , major, minor
  1560. + , count, start
  1561. + , ports >> 16
  1562. + , psic);
  1563. + if (name == 0x20425355 /* "USB " */)
  1564. + {
  1565. + if (major == 2)
  1566. + {
  1567. + x->usb2.start = start;
  1568. + x->usb2.count = count;
  1569. + }
  1570. + else if (major == 3)
  1571. + {
  1572. + x->usb3.start = start;
  1573. + x->usb3.count = count;
  1574. + }
  1575. +
  1576. + for (grub_uint32_t p = start - 1; p < start + count - 1UL; p++)
  1577. + {
  1578. + x->psids[p].major = major;
  1579. + x->psids[p].minor = minor;
  1580. + grub_xhci_fill_default_speed_mapping(&x->psids[p]);
  1581. + for (grub_uint8_t i = 0; i < psic; i++)
  1582. + {
  1583. + grub_uint32_t psid = grub_xhci_read32(&xcap->data[3 + i]);
  1584. + x->psids[p].psids[i].id = (psid >> 0) & 0xf;
  1585. + x->psids[p].psids[i].psie = (psid >> 4) & 0x3;
  1586. + x->psids[p].psids[i].psim = (psid >> 16) & 0xfffff;
  1587. + }
  1588. + grub_xhci_calc_speed_mapping(&x->psids[p]);
  1589. + }
  1590. + }
  1591. +
  1592. + break;
  1593. + }
  1594. + default:
  1595. + {
  1596. + grub_dprintf("xhci", "XHCI extcap 0x%x @ %p\n", cap & 0xff, addr);
  1597. + break;
  1598. + }
  1599. + }
  1600. + off = (cap >> 8) & 0xff;
  1601. + addr += off << 2;
  1602. + }
  1603. + while (off > 0);
  1604. + }
  1605. +
  1606. + x->pagesize = xhci_get_pagesize(x);
  1607. + grub_dprintf("xhci", "XHCI init: Minimum supported page size 0x%x\n",
  1608. + x->pagesize);
  1609. +
  1610. + /* Chapter 6.1 Device Context Base Address Array */
  1611. + x->devs_dma = xhci_memalign_dma32(ALIGN_DCBAA,
  1612. + sizeof(*x->devs) * (x->slots + 1),
  1613. + x->pagesize);
  1614. + if (!x->devs_dma)
  1615. + goto fail;
  1616. + x->devs = grub_dma_get_virt(x->devs_dma);
  1617. + grub_memset((void *)x->devs, 0, sizeof(*x->devs) * (x->slots + 1));
  1618. + grub_arch_sync_dma_caches(x->devs, sizeof(*x->devs) * (x->slots + 1));
  1619. + grub_dprintf ("xhci", "XHCI init: device memory %p (%x)\n",
  1620. + grub_dma_get_virt(x->devs_dma),
  1621. + grub_dma_get_phys(x->devs_dma));
  1622. +
  1623. + /* Chapter 6.5 Event Ring Segment Table */
  1624. + x->eseg_dma = xhci_memalign_dma32(ALIGN_EVT_RING_TABLE, sizeof(*x->eseg), 0);
  1625. + if (!x->eseg_dma)
  1626. + goto fail;
  1627. + x->eseg = grub_dma_get_virt(x->eseg_dma);
  1628. + grub_memset((void *)x->eseg, 0, sizeof(*x->eseg));
  1629. + grub_arch_sync_dma_caches(x->eseg, sizeof(*x->eseg));
  1630. + grub_dprintf ("xhci", "XHCI init: event ring table memory %p (%x)\n",
  1631. + grub_dma_get_virt(x->eseg_dma),
  1632. + grub_dma_get_phys(x->eseg_dma));
  1633. +
  1634. + x->cmds_dma = xhci_memalign_dma32(ALIGN_CMD_RING_SEG, sizeof(*x->cmds),
  1635. + BOUNDARY_RING);
  1636. + if (!x->cmds_dma)
  1637. + goto fail;
  1638. + x->cmds = grub_dma_get_virt(x->cmds_dma);
  1639. + grub_memset((void *)x->cmds, 0, sizeof(*x->cmds));
  1640. + grub_arch_sync_dma_caches(x->cmds, sizeof(*x->cmds));
  1641. + grub_dprintf ("xhci", "XHCI init: command ring memory %p (%x)\n",
  1642. + grub_dma_get_virt(x->cmds_dma),
  1643. + grub_dma_get_phys(x->cmds_dma));
  1644. +
  1645. + x->evts_dma = xhci_memalign_dma32(ALIGN_EVT_RING_SEG, sizeof(*x->evts),
  1646. + BOUNDARY_RING);
  1647. + if (!x->evts_dma)
  1648. + goto fail;
  1649. + x->evts = grub_dma_get_virt(x->evts_dma);
  1650. + grub_memset((void *)x->evts, 0, sizeof(*x->evts));
  1651. + grub_arch_sync_dma_caches(x->evts, sizeof(*x->evts));
  1652. + grub_dprintf ("xhci", "XHCI init: event ring memory %p (%x)\n",
  1653. + grub_dma_get_virt(x->evts_dma),
  1654. + grub_dma_get_phys(x->evts_dma));
  1655. +
  1656. + /* Chapter 4.20 Scratchpad Buffers */
  1657. + reg = grub_xhci_read32(&x->caps->hcsparams2);
  1658. + x->spb = (reg >> 21 & 0x1f) << 5 | reg >> 27;
  1659. + if (x->spb)
  1660. + {
  1661. + volatile grub_uint64_t *spba;
  1662. + grub_dprintf("xhci", "XHCI init: set up %d scratch pad buffers\n",
  1663. + x->spb);
  1664. + x->spba_dma = xhci_memalign_dma32(ALIGN_SPBA, sizeof(*spba) * x->spb,
  1665. + x->pagesize);
  1666. + if (!x->spba_dma)
  1667. + goto fail;
  1668. +
  1669. + x->spad_dma = xhci_memalign_dma32(x->pagesize, x->pagesize * x->spb,
  1670. + x->pagesize);
  1671. + if (!x->spad_dma)
  1672. + {
  1673. + grub_dma_free(x->spba_dma);
  1674. + goto fail;
  1675. + }
  1676. +
  1677. + spba = grub_dma_get_virt(x->spba_dma);
  1678. + for (grub_uint32_t i = 0; i < x->spb; i++)
  1679. + spba[i] = (grub_addr_t)grub_dma_get_phys(x->spad_dma) + (i * x->pagesize);
  1680. + grub_arch_sync_dma_caches(x->spba_dma, sizeof(*spba) * x->spb);
  1681. +
  1682. + x->devs[0].ptr_low = grub_dma_get_phys(x->spba_dma);
  1683. + x->devs[0].ptr_high = 0;
  1684. + grub_arch_sync_dma_caches(x->devs_dma, sizeof(x->devs[0]));
  1685. + grub_dprintf ("xhci", "XHCI init: Allocated %d scratch buffers of size 0x%x\n",
  1686. + x->spb, x->pagesize);
  1687. + }
  1688. +
  1689. + grub_xhci_reset(x);
  1690. +
  1691. + /* Set the running bit */
  1692. + reg = grub_xhci_read32 (&x->op->usbcmd);
  1693. + reg |= GRUB_XHCI_CMD_RS;
  1694. + grub_xhci_write32 (&x->op->usbcmd, reg);
  1695. +
  1696. +
  1697. + /* Link to xhci now that initialisation is successful. */
  1698. + x->next = xhci;
  1699. + xhci = x;
  1700. +
  1701. + return;
  1702. +
  1703. +fail:
  1704. + grub_dprintf ("xhci", "XHCI grub_xhci_pci_iter: FAILED!\n");
  1705. + if (x)
  1706. + {
  1707. + if (x->devs_dma)
  1708. + grub_dma_free (x->devs_dma);
  1709. + if (x->eseg_dma)
  1710. + grub_dma_free (x->eseg_dma);
  1711. + if (x->cmds_dma)
  1712. + grub_dma_free (x->cmds_dma);
  1713. + if (x->evts_dma)
  1714. + grub_dma_free (x->evts_dma);
  1715. + if (x->spad_dma)
  1716. + grub_dma_free (x->spad_dma);
  1717. + if (x->spba_dma)
  1718. + grub_dma_free (x->spba_dma);
  1719. + }
  1720. + grub_free (x);
  1721. +
  1722. + return;
  1723. +}
  1724. +
  1725. +static int
  1726. +grub_xhci_iterate (grub_usb_controller_iterate_hook_t hook, void *hook_data)
  1727. +{
  1728. + struct grub_xhci *x;
  1729. + struct grub_usb_controller dev;
  1730. +
  1731. + for (x = xhci; x; x = x->next)
  1732. + {
  1733. + dev.data = x;
  1734. + if (hook (&dev, hook_data))
  1735. + return 1;
  1736. + }
  1737. +
  1738. + return 0;
  1739. +}
  1740. +
  1741. +/****************************************************************
  1742. + * xHCI maintainance functions
  1743. + ****************************************************************/
  1744. +
  1745. +static grub_usb_err_t
  1746. +grub_xhci_update_hub_portcount (struct grub_xhci *x,
  1747. + grub_usb_transfer_t transfer,
  1748. + grub_uint32_t slotid)
  1749. +{
  1750. + struct grub_pci_dma_chunk *in_dma;
  1751. + volatile struct grub_xhci_slotctx *hdslot;
  1752. + grub_uint32_t epid = 0;
  1753. +
  1754. + if (!transfer || !transfer->dev || !transfer->dev->nports)
  1755. + return GRUB_USB_ERR_NONE;
  1756. +
  1757. + hdslot = grub_dma_phys2virt(x->devs[slotid].ptr_low, x->devs_dma);
  1758. + if ((hdslot->ctx[3] >> 27) == 3)
  1759. + /* Already configured */
  1760. + return 0;
  1761. +
  1762. + grub_dprintf("xhci", "%s: updating hub config to %d ports\n", __func__,
  1763. + transfer->dev->nports);
  1764. +
  1765. + xhci_check_status(x);
  1766. +
  1767. + /* Allocate input context and initialize endpoint info. */
  1768. + in_dma = grub_xhci_alloc_inctx(x, epid, transfer->dev);
  1769. + if (!in_dma)
  1770. + return GRUB_USB_ERR_INTERNAL;
  1771. + volatile struct grub_xhci_inctx *in = grub_dma_get_virt(in_dma);
  1772. +
  1773. + in->add = (1 << epid);
  1774. +
  1775. + struct grub_xhci_epctx *ep = (void*)&in[(epid+1) << x->flag64];
  1776. + ep->ctx[0] |= 1 << 26;
  1777. + ep->ctx[1] |= transfer->dev->nports << 24;
  1778. +
  1779. + int cc = xhci_cmd_configure_endpoint(x, slotid, in_dma);
  1780. + grub_dma_free(in_dma);
  1781. +
  1782. + if (cc != CC_SUCCESS)
  1783. + {
  1784. + grub_dprintf("xhci", "%s: reconf ctl endpoint: failed (cc %d)\n",
  1785. + __func__, cc);
  1786. + return GRUB_USB_ERR_BADDEVICE;
  1787. + }
  1788. +
  1789. + return GRUB_USB_ERR_NONE;
  1790. +}
  1791. +
  1792. +static grub_usb_err_t
  1793. +grub_xhci_update_max_paket_size (struct grub_xhci *x,
  1794. + grub_usb_transfer_t transfer,
  1795. + grub_uint32_t slotid,
  1796. + grub_uint32_t max_packet)
  1797. +{
  1798. + struct grub_pci_dma_chunk *in_dma;
  1799. + grub_uint32_t epid = 1;
  1800. +
  1801. + if (!transfer || !transfer->dev || !max_packet)
  1802. + return GRUB_USB_ERR_NONE;
  1803. +
  1804. + grub_dprintf("xhci", "%s: updating max packet size to 0x%x\n", __func__,
  1805. + max_packet);
  1806. +
  1807. + xhci_check_status(x);
  1808. +
  1809. + /* Allocate input context and initialize endpoint info. */
  1810. + in_dma = grub_xhci_alloc_inctx(x, epid, transfer->dev);
  1811. + if (!in_dma)
  1812. + return GRUB_USB_ERR_INTERNAL;
  1813. + volatile struct grub_xhci_inctx *in = grub_dma_get_virt(in_dma);
  1814. + in->add = (1 << epid);
  1815. +
  1816. + struct grub_xhci_epctx *ep = (void*)&in[(epid+1) << x->flag64];
  1817. + ep->ctx[1] |= max_packet << 16;
  1818. +
  1819. + int cc = xhci_cmd_evaluate_context(x, slotid, in_dma);
  1820. + grub_dma_free(in_dma);
  1821. +
  1822. + if (cc != CC_SUCCESS)
  1823. + {
  1824. + grub_dprintf("xhci", "%s: reconf ctl endpoint: failed (cc %d)\n",
  1825. + __func__, cc);
  1826. + return GRUB_USB_ERR_BADDEVICE;
  1827. + }
  1828. +
  1829. + return GRUB_USB_ERR_NONE;
  1830. +}
  1831. +
  1832. +/****************************************************************
  1833. + * xHCI endpoint enablement functions
  1834. + ****************************************************************/
  1835. +
  1836. +static grub_usb_err_t
  1837. +grub_xhci_prepare_endpoint (struct grub_xhci *x,
  1838. + struct grub_usb_device *dev,
  1839. + grub_uint8_t endpoint,
  1840. + grub_transfer_type_t dir,
  1841. + grub_transaction_type_t type,
  1842. + grub_uint32_t maxpaket,
  1843. + struct grub_xhci_priv *priv)
  1844. +{
  1845. + grub_uint32_t epid;
  1846. + struct grub_pci_dma_chunk *reqs_dma;
  1847. + struct grub_pci_dma_chunk *in_dma;
  1848. + volatile struct grub_xhci_ring *reqs;
  1849. + volatile struct grub_xhci_slotctx *slotctx;
  1850. +
  1851. + if (!x || !priv)
  1852. + return GRUB_USB_ERR_INTERNAL;
  1853. +
  1854. + xhci_check_status(x);
  1855. +
  1856. + if (endpoint == 0)
  1857. + {
  1858. + epid = 1;
  1859. + }
  1860. + else
  1861. + {
  1862. + epid = (endpoint & 0x0f) * 2;
  1863. + epid += (dir == GRUB_USB_TRANSFER_TYPE_IN) ? 1 : 0;
  1864. + }
  1865. + grub_dprintf("xhci", "%s: epid %d\n", __func__, epid);
  1866. +
  1867. + /* Test if already prepared */
  1868. + if (priv->slotid > 0 && priv->enpoint_trbs[epid] != NULL)
  1869. + return GRUB_USB_ERR_NONE;
  1870. +
  1871. + /* Allocate DMA buffer as endpoint cmd TRB */
  1872. + reqs_dma = xhci_memalign_dma32(ALIGN_TRB, sizeof(*reqs),
  1873. + BOUNDARY_RING);
  1874. + if (!reqs_dma)
  1875. + return GRUB_USB_ERR_INTERNAL;
  1876. + reqs = grub_dma_get_virt(reqs_dma);
  1877. + grub_memset((void *)reqs, 0, sizeof(*reqs));
  1878. + reqs->cs = 1;
  1879. +
  1880. + grub_arch_sync_dma_caches(reqs, sizeof(*reqs));
  1881. +
  1882. + /* Allocate input context and initialize endpoint info. */
  1883. + in_dma = grub_xhci_alloc_inctx(x, epid, dev);
  1884. + if (!in_dma)
  1885. + {
  1886. + grub_dma_free(reqs_dma);
  1887. + return GRUB_USB_ERR_INTERNAL;
  1888. + }
  1889. + volatile struct grub_xhci_inctx *in = grub_dma_get_virt(in_dma);
  1890. + in->add = 0x01 | (1 << epid);
  1891. +
  1892. + struct grub_xhci_epctx *ep = (void*)&in[(epid+1) << x->flag64];
  1893. + switch (type)
  1894. + {
  1895. + case GRUB_USB_TRANSACTION_TYPE_CONTROL:
  1896. + ep->ctx[1] |= 0 << 3;
  1897. + break;
  1898. + case GRUB_USB_TRANSACTION_TYPE_BULK:
  1899. + ep->ctx[1] |= 2 << 3;
  1900. + break;
  1901. + }
  1902. + if (dir == GRUB_USB_TRANSFER_TYPE_IN
  1903. + || type== GRUB_USB_TRANSACTION_TYPE_CONTROL)
  1904. + ep->ctx[1] |= 1 << 5;
  1905. + ep->ctx[1] |= maxpaket << 16;
  1906. + ep->deq_low = grub_dma_get_phys(reqs_dma);
  1907. + ep->deq_low |= 1; /* dcs */
  1908. + ep->length = maxpaket;
  1909. +
  1910. + grub_dprintf("xhci", "%s: ring %p, epid %d, max %d\n", __func__,
  1911. + reqs, epid, maxpaket);
  1912. + if (epid == 1 || priv->slotid == 0) {
  1913. + /* Enable slot. */
  1914. + int slotid = xhci_cmd_enable_slot(x);
  1915. + if (slotid < 0)
  1916. + {
  1917. + grub_dprintf("xhci", "%s: enable slot: failed\n", __func__);
  1918. + grub_dma_free(reqs_dma);
  1919. + grub_dma_free(in_dma);
  1920. + return GRUB_USB_ERR_BADDEVICE;
  1921. + }
  1922. + grub_dprintf("xhci", "%s: get slot %d assigned\n", __func__, slotid);
  1923. +
  1924. + grub_uint32_t size = (sizeof(struct grub_xhci_slotctx) * GRUB_XHCI_MAX_ENDPOINTS) << x->flag64;
  1925. +
  1926. + /* Allocate memory for the device specific slot context */
  1927. + priv->slotctx_dma = xhci_memalign_dma32(ALIGN_SLOTCTX, size,
  1928. + x->pagesize);
  1929. + if (!priv->slotctx_dma)
  1930. + {
  1931. + grub_dprintf("xhci", "%s: grub_memalign_dma32 failed\n", __func__);
  1932. + grub_dma_free(reqs_dma);
  1933. + grub_dma_free(in_dma);
  1934. + return GRUB_USB_ERR_INTERNAL;
  1935. + }
  1936. + slotctx = grub_dma_get_virt(priv->slotctx_dma);
  1937. +
  1938. + grub_dprintf("xhci", "%s: enable slot: got slotid %d\n", __func__, slotid);
  1939. + grub_memset((void *)slotctx, 0, size);
  1940. + grub_arch_sync_dma_caches(slotctx, size);
  1941. +
  1942. + x->devs[slotid].ptr_low = grub_dma_get_phys(priv->slotctx_dma);
  1943. + x->devs[slotid].ptr_high = 0;
  1944. + grub_arch_sync_dma_caches(&x->devs[slotid], sizeof(x->devs[0]));
  1945. +
  1946. + /* Send set_address command. */
  1947. + int cc = xhci_cmd_address_device(x, slotid, in_dma);
  1948. + if (cc != CC_SUCCESS)
  1949. + {
  1950. + grub_dprintf("xhci","%s: address device: failed (cc %d)\n", __func__, cc);
  1951. + cc = xhci_cmd_disable_slot(x, slotid);
  1952. + if (cc != CC_SUCCESS) {
  1953. + grub_dprintf("xhci", "%s: disable failed (cc %d)\n", __func__, cc);
  1954. + } else {
  1955. + x->devs[slotid].ptr_low = 0;
  1956. + x->devs[slotid].ptr_high = 0;
  1957. + grub_arch_sync_dma_caches(&x->devs[slotid], sizeof(x->devs[0]));
  1958. + }
  1959. + grub_dma_free(priv->slotctx_dma);
  1960. + grub_dma_free(reqs_dma);
  1961. + grub_dma_free(in_dma);
  1962. + return GRUB_USB_ERR_BADDEVICE;
  1963. + }
  1964. + priv->enpoint_trbs[epid] = reqs;
  1965. + priv->enpoint_trbs_dma[epid] = reqs_dma;
  1966. + priv->slotid = slotid;
  1967. + priv->max_packet = 0;
  1968. + }
  1969. + if (epid != 1)
  1970. + {
  1971. + /* Send configure command. */
  1972. + int cc = xhci_cmd_configure_endpoint(x, priv->slotid, in_dma);
  1973. + if (cc != CC_SUCCESS)
  1974. + {
  1975. + grub_dprintf("xhci", "%s: configure endpoint: failed (cc %d)\n",
  1976. + __func__, cc);
  1977. + grub_dma_free(reqs_dma);
  1978. + grub_dma_free(in_dma);
  1979. + return GRUB_USB_ERR_BADDEVICE;
  1980. + }
  1981. + priv->enpoint_trbs[epid] = reqs;
  1982. + priv->enpoint_trbs_dma[epid] = reqs_dma;
  1983. + }
  1984. +
  1985. + grub_dprintf("xhci", "%s: done\n", __func__);
  1986. + grub_dma_free(in_dma);
  1987. +
  1988. + return GRUB_USB_ERR_NONE;
  1989. +}
  1990. +
  1991. +
  1992. +/****************************************************************
  1993. + * xHCI transfer helper functions
  1994. + ****************************************************************/
  1995. +
  1996. +static grub_usb_err_t
  1997. +grub_xhci_usb_to_grub_err (unsigned char status)
  1998. +{
  1999. + if (status != CC_SUCCESS)
  2000. + grub_dprintf("xhci", "%s: xfer failed (cc %d)\n", __func__, status);
  2001. + else
  2002. + grub_dprintf("xhci", "%s: xfer done (cc %d)\n", __func__, status);
  2003. +
  2004. + if (status == CC_BABBLE_DETECTED)
  2005. + return GRUB_USB_ERR_BABBLE;
  2006. + else if (status == CC_DATA_BUFFER_ERROR)
  2007. + return GRUB_USB_ERR_DATA;
  2008. + else if (status == CC_STALL_ERROR)
  2009. + return GRUB_USB_ERR_STALL;
  2010. + else if (status != CC_SUCCESS)
  2011. + return GRUB_USB_ERR_NAK;
  2012. +
  2013. + return GRUB_USB_ERR_NONE;
  2014. +}
  2015. +
  2016. +static int
  2017. +grub_xhci_transfer_is_zlp(grub_usb_transfer_t transfer, int idx)
  2018. +{
  2019. + if (idx >= transfer->transcnt)
  2020. + return 0;
  2021. +
  2022. + grub_usb_transaction_t tr = &transfer->transactions[idx];
  2023. +
  2024. + return (tr->size == 0) &&
  2025. + ((tr->pid == GRUB_USB_TRANSFER_TYPE_OUT) ||
  2026. + (tr->pid == GRUB_USB_TRANSFER_TYPE_IN));
  2027. +}
  2028. +
  2029. +static int
  2030. +grub_xhci_transfer_is_last(grub_usb_transfer_t transfer, int idx)
  2031. +{
  2032. + return (idx + 1) == transfer->transcnt;
  2033. +}
  2034. +
  2035. +static int
  2036. +grub_xhci_transfer_is_data(grub_usb_transfer_t transfer, int idx)
  2037. +{
  2038. + grub_usb_transaction_t tr;
  2039. +
  2040. + if (idx >= transfer->transcnt)
  2041. + return 0;
  2042. +
  2043. + tr = &transfer->transactions[idx];
  2044. + if (tr->size == 0 ||
  2045. + (tr->pid == GRUB_USB_TRANSFER_TYPE_SETUP))
  2046. + return 0;
  2047. +
  2048. + /* If there's are no DATA pakets before it's a DATA paket */
  2049. + for (int i = idx - 1; i >= 0; i--)
  2050. + {
  2051. + tr = &transfer->transactions[i];
  2052. + if (tr->size > 0 &&
  2053. + ((tr->pid == GRUB_USB_TRANSFER_TYPE_OUT) ||
  2054. + (tr->pid == GRUB_USB_TRANSFER_TYPE_IN)))
  2055. + return 0;
  2056. + }
  2057. + return 1;
  2058. +}
  2059. +
  2060. +static int
  2061. +grub_xhci_transfer_is_in(grub_usb_transfer_t transfer, int idx)
  2062. +{
  2063. + grub_usb_transaction_t tr;
  2064. +
  2065. + if (idx >= transfer->transcnt)
  2066. + return 0;
  2067. +
  2068. + tr = &transfer->transactions[idx];
  2069. +
  2070. + return tr->pid == GRUB_USB_TRANSFER_TYPE_IN;
  2071. +}
  2072. +
  2073. +static int
  2074. +grub_xhci_transfer_is_normal(grub_usb_transfer_t transfer, int idx)
  2075. +{
  2076. + grub_usb_transaction_t tr;
  2077. +
  2078. + if (idx >= transfer->transcnt)
  2079. + return 0;
  2080. +
  2081. + tr = &transfer->transactions[idx];
  2082. + if (tr->size == 0 ||
  2083. + (tr->pid == GRUB_USB_TRANSFER_TYPE_SETUP))
  2084. + return 0;
  2085. +
  2086. + /* If there's at least one DATA paket before it's a normal */
  2087. + for (int i = idx - 1; i >= 0; i--)
  2088. + {
  2089. + tr = &transfer->transactions[i];
  2090. + if (tr->size > 0 &&
  2091. + ((tr->pid == GRUB_USB_TRANSFER_TYPE_OUT) ||
  2092. + (tr->pid == GRUB_USB_TRANSFER_TYPE_IN)))
  2093. + return 1;
  2094. +
  2095. + }
  2096. + return 0;
  2097. +}
  2098. +
  2099. +static int
  2100. +grub_xhci_transfer_next_is_normal(grub_usb_transfer_t transfer, int idx)
  2101. +{
  2102. + return grub_xhci_transfer_is_normal(transfer, idx + 1);
  2103. +}
  2104. +
  2105. +static int
  2106. +grub_xhci_transfer_next_is_in(grub_usb_transfer_t transfer, int idx)
  2107. +{
  2108. + return grub_xhci_transfer_is_in(transfer, idx + 1);
  2109. +}
  2110. +
  2111. +static grub_uint8_t grub_xhci_epid_from_transfer(grub_usb_transfer_t transfer)
  2112. +{
  2113. + grub_uint8_t epid;
  2114. +
  2115. + if (transfer->endpoint == 0) {
  2116. + epid = 1;
  2117. + } else {
  2118. + epid = (transfer->endpoint & 0x0f) * 2;
  2119. + epid += (transfer->dir == GRUB_USB_TRANSFER_TYPE_IN) ? 1 : 0;
  2120. + }
  2121. + return epid;
  2122. +}
  2123. +
  2124. +/****************************************************************
  2125. + * xHCI transfer functions
  2126. + ****************************************************************/
  2127. +
  2128. +static grub_usb_err_t
  2129. +grub_xhci_setup_transfer (grub_usb_controller_t dev,
  2130. + grub_usb_transfer_t transfer)
  2131. +{
  2132. + struct grub_xhci_transfer_controller_data *cdata;
  2133. + struct grub_xhci *x = (struct grub_xhci *) dev->data;
  2134. + grub_uint8_t epid;
  2135. + grub_usb_err_t err;
  2136. + volatile struct grub_xhci_ring *reqs;
  2137. + int rc;
  2138. + struct grub_xhci_priv *priv;
  2139. +
  2140. + xhci_check_status(x);
  2141. +
  2142. + if (!dev || !transfer || !transfer->dev || !transfer->dev->xhci_priv)
  2143. + return GRUB_USB_ERR_INTERNAL;
  2144. +
  2145. + priv = transfer->dev->xhci_priv;
  2146. + err = grub_xhci_prepare_endpoint(x, transfer->dev,
  2147. + transfer->endpoint,
  2148. + transfer->dir,
  2149. + transfer->type,
  2150. + transfer->max,
  2151. + priv);
  2152. +
  2153. + if (err != GRUB_USB_ERR_NONE)
  2154. + return err;
  2155. +
  2156. + epid = grub_xhci_epid_from_transfer(transfer);
  2157. +
  2158. + /* Update the max packet size once descdev.maxsize0 is valid */
  2159. + if (epid == 1 &&
  2160. + (priv->max_packet == 0) &&
  2161. + (transfer->dev->descdev.maxsize0 > 0))
  2162. + {
  2163. + if (transfer->dev->speed == GRUB_USB_SPEED_SUPER)
  2164. + priv->max_packet = 1UL << transfer->dev->descdev.maxsize0;
  2165. + else
  2166. + priv->max_packet = transfer->dev->descdev.maxsize0;
  2167. + err = grub_xhci_update_max_paket_size(x, transfer, priv->slotid, priv->max_packet);
  2168. + if (err != GRUB_USB_ERR_NONE)
  2169. + {
  2170. + grub_dprintf("xhci", "%s: Updating max paket size failed\n", __func__);
  2171. + return err;
  2172. + }
  2173. + }
  2174. + if (epid == 1 &&
  2175. + transfer->dev->descdev.class == 9 &&
  2176. + transfer->dev->nports > 0)
  2177. + {
  2178. + err = grub_xhci_update_hub_portcount(x, transfer, priv->slotid);
  2179. + if (err != GRUB_USB_ERR_NONE)
  2180. + {
  2181. + grub_dprintf("xhci", "%s: Updating max paket size failed\n", __func__);
  2182. + return err;
  2183. + }
  2184. + }
  2185. +
  2186. + /* Allocate private data for the transfer */
  2187. + cdata = grub_zalloc(sizeof(*cdata));
  2188. + if (!cdata)
  2189. + return GRUB_USB_ERR_INTERNAL;
  2190. +
  2191. + reqs = priv->enpoint_trbs[epid];
  2192. +
  2193. + transfer->controller_data = cdata;
  2194. +
  2195. + /* Now queue the transfer onto the TRB */
  2196. + if (transfer->type == GRUB_USB_TRANSACTION_TYPE_CONTROL)
  2197. + {
  2198. + volatile struct grub_usb_packet_setup *setupdata;
  2199. + setupdata = (void *)transfer->transactions[0].data;
  2200. + grub_dprintf("xhci", "%s: CONTROLL TRANS req %d\n", __func__, setupdata->request);
  2201. + grub_dprintf("xhci", "%s: CONTROLL TRANS length %d\n", __func__, setupdata->length);
  2202. +
  2203. + if (setupdata && setupdata->request == GRUB_USB_REQ_SET_ADDRESS)
  2204. + return GRUB_USB_ERR_NONE;
  2205. +
  2206. + if (transfer->transcnt < 2)
  2207. + return GRUB_USB_ERR_INTERNAL;
  2208. +
  2209. + for (int i = 0; i < transfer->transcnt; i++)
  2210. + {
  2211. + grub_uint32_t flags = 0;
  2212. + grub_uint64_t inline_data;
  2213. + grub_usb_transaction_t tr = &transfer->transactions[i];
  2214. +
  2215. + switch (tr->pid)
  2216. + {
  2217. + case GRUB_USB_TRANSFER_TYPE_SETUP:
  2218. + {
  2219. + grub_dprintf("xhci", "%s: SETUP PKG\n", __func__);
  2220. + grub_dprintf("xhci", "%s: transfer->size %d\n", __func__, transfer->size);
  2221. + grub_dprintf("xhci", "%s: tr->size %d SETUP PKG\n", __func__, tr->size);
  2222. +
  2223. + flags |= (TR_SETUP << 10);
  2224. + flags |= TRB_TR_IDT;
  2225. +
  2226. + if (transfer->size > 0)
  2227. + {
  2228. + if (grub_xhci_transfer_next_is_in(transfer, i))
  2229. + flags |= (3 << 16); /* TRT IN */
  2230. + else
  2231. + flags |= (2 << 16); /* TRT OUT */
  2232. + }
  2233. + break;
  2234. + }
  2235. + case GRUB_USB_TRANSFER_TYPE_OUT:
  2236. + {
  2237. + grub_dprintf("xhci", "%s: OUT PKG\n", __func__);
  2238. + cdata->transfer_size += tr->size;
  2239. + break;
  2240. + }
  2241. + case GRUB_USB_TRANSFER_TYPE_IN:
  2242. + {
  2243. + grub_dprintf("xhci", "%s: IN PKG\n", __func__);
  2244. + cdata->transfer_size += tr->size;
  2245. + flags |= TRB_TR_DIR;
  2246. + break;
  2247. + }
  2248. + }
  2249. +
  2250. + if (grub_xhci_transfer_is_normal(transfer, i))
  2251. + flags |= (TR_NORMAL << 10);
  2252. + else if (grub_xhci_transfer_is_data(transfer, i))
  2253. + flags |= (TR_DATA << 10);
  2254. + else if (grub_xhci_transfer_is_zlp(transfer, i))
  2255. + flags |= (TR_STATUS << 10);
  2256. +
  2257. + if (grub_xhci_transfer_next_is_normal(transfer, i))
  2258. + flags |= TRB_TR_CH;
  2259. +
  2260. + if (grub_xhci_transfer_is_last(transfer, i))
  2261. + flags |= TRB_TR_IOC;
  2262. +
  2263. + /* Assume the ring has enough free space for all TRBs */
  2264. + if (flags & TRB_TR_IDT && tr->size <= (int)sizeof(inline_data))
  2265. + {
  2266. + grub_memcpy(&inline_data, (void *)tr->data, tr->size);
  2267. + xhci_trb_queue(reqs, inline_data, tr->size, flags);
  2268. + }
  2269. + else
  2270. + {
  2271. + xhci_trb_queue(reqs, tr->data, tr->size, flags);
  2272. + }
  2273. + }
  2274. + }
  2275. + else if (transfer->type == GRUB_USB_TRANSACTION_TYPE_BULK)
  2276. + {
  2277. + for (int i = 0; i < transfer->transcnt; i++)
  2278. + {
  2279. + grub_uint32_t flags = (TR_NORMAL << 10);
  2280. + grub_usb_transaction_t tr = &transfer->transactions[i];
  2281. + switch (tr->pid)
  2282. + {
  2283. + case GRUB_USB_TRANSFER_TYPE_OUT:
  2284. + {
  2285. + grub_dprintf("xhci", "%s: OUT PKG\n", __func__);
  2286. + cdata->transfer_size += tr->size;
  2287. + break;
  2288. + }
  2289. + case GRUB_USB_TRANSFER_TYPE_IN:
  2290. + {
  2291. + grub_dprintf("xhci", "%s: IN PKG\n", __func__);
  2292. + cdata->transfer_size += tr->size;
  2293. + flags |= TRB_TR_DIR;
  2294. + break;
  2295. + }
  2296. + case GRUB_USB_TRANSFER_TYPE_SETUP:
  2297. + break;
  2298. + }
  2299. + if (grub_xhci_transfer_is_last(transfer, i))
  2300. + flags |= TRB_TR_IOC;
  2301. +
  2302. + /* The ring might be to small, submit while adding new entries */
  2303. + rc = xhci_trb_queue_and_flush(x, priv->slotid, epid,
  2304. + reqs, tr->data, tr->size, flags);
  2305. + if (rc < 0)
  2306. + return GRUB_USB_ERR_TIMEOUT;
  2307. + else if (rc > 1)
  2308. + return grub_xhci_usb_to_grub_err(rc);
  2309. +
  2310. + }
  2311. + }
  2312. + xhci_doorbell(x, priv->slotid, epid);
  2313. +
  2314. + return GRUB_USB_ERR_NONE;
  2315. +}
  2316. +
  2317. +static grub_usb_err_t
  2318. +grub_xhci_check_transfer (grub_usb_controller_t dev,
  2319. + grub_usb_transfer_t transfer, grub_size_t * actual)
  2320. +{
  2321. + grub_uint32_t status;
  2322. + grub_uint32_t remaining;
  2323. + grub_uint8_t epid;
  2324. + volatile struct grub_xhci_ring *reqs;
  2325. + grub_usb_err_t err;
  2326. + int rc;
  2327. +
  2328. + if (!dev->data || !transfer->controller_data || !transfer->dev ||
  2329. + !transfer->dev->xhci_priv)
  2330. + return GRUB_USB_ERR_INTERNAL;
  2331. +
  2332. +
  2333. + struct grub_xhci_priv *priv = transfer->dev->xhci_priv;
  2334. + struct grub_xhci *x = (struct grub_xhci *) dev->data;
  2335. + struct grub_xhci_transfer_controller_data *cdata =
  2336. + transfer->controller_data;
  2337. +
  2338. + xhci_check_status(x);
  2339. + xhci_process_events(x);
  2340. +
  2341. + epid = grub_xhci_epid_from_transfer(transfer);
  2342. +
  2343. + reqs = priv->enpoint_trbs[epid];
  2344. +
  2345. + /* XXX: invalidate caches */
  2346. +
  2347. + /* Get current status from event ring buffer */
  2348. + status = (reqs->evt.status>> 24) & 0xff;
  2349. + remaining = reqs->evt.status & 0xffffff;
  2350. +
  2351. + if (status != CC_STOPPED_LENGTH_INVALID)
  2352. + *actual = cdata->transfer_size - remaining;
  2353. + else
  2354. + *actual = 0;
  2355. +
  2356. + if (xhci_ring_busy(reqs))
  2357. + return GRUB_USB_ERR_WAIT;
  2358. +
  2359. + grub_free(cdata);
  2360. +
  2361. + grub_dprintf("xhci", "%s: xfer done\n", __func__);
  2362. +
  2363. + err = grub_xhci_usb_to_grub_err(status);
  2364. + if (err != GRUB_USB_ERR_NONE)
  2365. + {
  2366. + if (status == CC_STALL_ERROR)
  2367. + {
  2368. + /* Clear the stall by resetting the endpoint */
  2369. + rc = xhci_cmd_reset_endpoint(x, priv->slotid, epid, 1);
  2370. +
  2371. + if (rc < 0)
  2372. + return GRUB_USB_ERR_TIMEOUT;
  2373. +
  2374. + return GRUB_USB_ERR_STALL;
  2375. + }
  2376. + else if (remaining > 0)
  2377. + {
  2378. + return GRUB_USB_ERR_DATA;
  2379. + }
  2380. + }
  2381. +
  2382. + return err;
  2383. +}
  2384. +
  2385. +static grub_usb_err_t
  2386. +grub_xhci_cancel_transfer (grub_usb_controller_t dev,
  2387. + grub_usb_transfer_t transfer)
  2388. +{
  2389. + grub_uint8_t epid;
  2390. + volatile struct grub_xhci_ring *reqs;
  2391. + struct grub_pci_dma_chunk *enpoint_trbs_dma;
  2392. + grub_addr_t deque_pointer;
  2393. + int rc;
  2394. +
  2395. + if (!dev->data || !transfer->controller_data || !transfer->dev ||
  2396. + !transfer->dev->xhci_priv)
  2397. + return GRUB_USB_ERR_INTERNAL;
  2398. +
  2399. + struct grub_xhci *x = (struct grub_xhci *) dev->data;
  2400. + struct grub_xhci_transfer_controller_data *cdata =
  2401. + transfer->controller_data;
  2402. + struct grub_xhci_priv *priv = transfer->dev->xhci_priv;
  2403. +
  2404. + epid = grub_xhci_epid_from_transfer(transfer);
  2405. +
  2406. + enpoint_trbs_dma = priv->enpoint_trbs_dma[epid];
  2407. + reqs = priv->enpoint_trbs[epid];
  2408. +
  2409. + /* Abort current command */
  2410. + rc = xhci_cmd_stop_endpoint(x, priv->slotid, epid, 0);
  2411. + if (rc < 0)
  2412. + return GRUB_USB_ERR_TIMEOUT;
  2413. +
  2414. + /* Reset state */
  2415. + reqs->nidx = 0;
  2416. + reqs->eidx = 0;
  2417. + reqs->cs = 1;
  2418. +
  2419. + grub_arch_sync_dma_caches(reqs, sizeof(*reqs));
  2420. +
  2421. + /* Reset the dequeue pointer to the begging of the TRB */
  2422. + deque_pointer = grub_dma_get_phys(enpoint_trbs_dma);
  2423. + rc = xhci_cmd_set_dequeue_pointer(x, priv->slotid, epid, deque_pointer| 1 );
  2424. + if (rc < 0)
  2425. + return GRUB_USB_ERR_TIMEOUT;
  2426. +
  2427. + reqs->evt.ptr_low = 0;
  2428. + reqs->evt.ptr_high = 0;
  2429. + reqs->evt.control = 0;
  2430. + reqs->evt.status = 0;
  2431. +
  2432. + grub_arch_sync_dma_caches(reqs, sizeof(*reqs));
  2433. +
  2434. + /* Restart ring buffer processing */
  2435. + xhci_doorbell(x, priv->slotid, epid);
  2436. +
  2437. + grub_free (cdata);
  2438. +
  2439. + return GRUB_USB_ERR_NONE;
  2440. +}
  2441. +
  2442. +/****************************************************************
  2443. + * xHCI port status functions
  2444. + ****************************************************************/
  2445. +
  2446. +static int
  2447. +grub_xhci_hubports (grub_usb_controller_t dev)
  2448. +{
  2449. + struct grub_xhci *x = (struct grub_xhci *) dev->data;
  2450. + grub_uint32_t portinfo;
  2451. +
  2452. + portinfo = x->ports;
  2453. + grub_dprintf ("xhci", "root hub ports=%d\n", portinfo);
  2454. + return portinfo;
  2455. +}
  2456. +
  2457. +static grub_usb_err_t
  2458. +grub_xhci_portstatus (grub_usb_controller_t dev,
  2459. + unsigned int port, unsigned int enable)
  2460. +{
  2461. + struct grub_xhci *x = (struct grub_xhci *) dev->data;
  2462. + grub_uint32_t portsc, pls;
  2463. + grub_uint32_t end;
  2464. +
  2465. + portsc = grub_xhci_port_read(x, port);
  2466. + pls = xhci_get_field(portsc, XHCI_PORTSC_PLS);
  2467. +
  2468. + grub_dprintf("xhci", "grub_xhci_portstatus port #%d: 0x%08x,%s%s pls %d enable %d\n",
  2469. + port, portsc,
  2470. + (portsc & GRUB_XHCI_PORTSC_PP) ? " powered," : "",
  2471. + (portsc & GRUB_XHCI_PORTSC_PED) ? " enabled," : "",
  2472. + pls, enable);
  2473. + xhci_check_status(x);
  2474. +
  2475. + if ((enable && (portsc & GRUB_XHCI_PORTSC_PED)) ||
  2476. + (!enable && !(portsc & GRUB_XHCI_PORTSC_PED)))
  2477. + return GRUB_USB_ERR_NONE;
  2478. +
  2479. + if (!enable)
  2480. + {
  2481. + /* Disable port */
  2482. + grub_xhci_port_write(x, port, ~0, GRUB_XHCI_PORTSC_PED);
  2483. + return GRUB_USB_ERR_NONE;
  2484. + }
  2485. +
  2486. + grub_dprintf ("xhci", "portstatus: XHCI STATUS: %08x\n",
  2487. + grub_xhci_read32(&x->op->usbsts));
  2488. + grub_dprintf ("xhci",
  2489. + "portstatus: begin, iobase=%p, port=%d, status=0x%08x\n",
  2490. + x->caps, port, portsc);
  2491. +
  2492. + switch (pls)
  2493. + {
  2494. + case PLS_U0:
  2495. + /* A USB3 port - controller automatically performs reset */
  2496. + break;
  2497. + case PLS_POLLING:
  2498. + /* A USB2 port - perform device reset */
  2499. + grub_xhci_port_write(x, port, ~GRUB_XHCI_PORTSC_PED, GRUB_XHCI_PORTSC_PR);
  2500. + break;
  2501. + default:
  2502. + return GRUB_USB_ERR_NONE;
  2503. + }
  2504. +
  2505. + /* Wait for device to complete reset and be enabled */
  2506. + end = grub_get_time_ms () + 100;
  2507. + for (;;)
  2508. + {
  2509. + portsc = grub_xhci_port_read(x, port);
  2510. + if (!(portsc & GRUB_XHCI_PORTSC_CCS))
  2511. + {
  2512. + /* Device disconnected during reset */
  2513. + grub_dprintf ("xhci","ERROR: %s device disconnected\n", __func__);
  2514. + return GRUB_USB_ERR_BADDEVICE;
  2515. + }
  2516. + if (portsc & GRUB_XHCI_PORTSC_PED)
  2517. + /* Reset complete */
  2518. + break;
  2519. + if (grub_get_time_ms () > end)
  2520. + {
  2521. + grub_dprintf ("xhci","ERROR: %s TIMEOUT\n", __func__);
  2522. + return GRUB_USB_ERR_TIMEOUT;
  2523. + }
  2524. + }
  2525. + xhci_check_status(x);
  2526. +
  2527. + return GRUB_USB_ERR_NONE;
  2528. +}
  2529. +
  2530. +/****************************************************************
  2531. + * xHCI detect device functions
  2532. + ****************************************************************/
  2533. +
  2534. +static grub_usb_speed_t
  2535. +grub_xhci_detect_dev (grub_usb_controller_t dev, int port, int *changed)
  2536. +{
  2537. + struct grub_xhci *x = (struct grub_xhci *) dev->data;
  2538. + grub_uint32_t portsc, speed;
  2539. +
  2540. + *changed = 0;
  2541. + grub_dprintf("xhci", "%s: dev=%p USB%d_%d port %d\n", __func__, dev,
  2542. + x->psids[port-1].major, x->psids[port-1].minor, port);
  2543. +
  2544. + /* On shutdown advertise all ports as disconnected. This will trigger
  2545. + * a gracefull detatch. */
  2546. + if (x->shutdown)
  2547. + {
  2548. + *changed = 1;
  2549. + return GRUB_USB_SPEED_NONE;
  2550. + }
  2551. +
  2552. + /* Don't advertise new devices, connecting will fail if halted */
  2553. + if (xhci_is_halted(x))
  2554. + return GRUB_USB_SPEED_NONE;
  2555. +
  2556. + portsc = grub_xhci_port_read(x, port);
  2557. + speed = xhci_get_field(portsc, XHCI_PORTSC_SPEED);
  2558. + grub_uint8_t pls = xhci_get_field(portsc, XHCI_PORTSC_PLS);
  2559. +
  2560. + grub_dprintf("xhci", "grub_xhci_portstatus port #%d: 0x%08x,%s%s pls %d\n",
  2561. + port, portsc,
  2562. + (portsc & GRUB_XHCI_PORTSC_PP) ? " powered," : "",
  2563. + (portsc & GRUB_XHCI_PORTSC_PED) ? " enabled," : "",
  2564. + pls);
  2565. +
  2566. + /* Connect Status Change bit - it detects change of connection */
  2567. + if (portsc & GRUB_XHCI_PORTSC_CSC)
  2568. + {
  2569. + *changed = 1;
  2570. +
  2571. + grub_xhci_port_write(x, port, ~GRUB_XHCI_PORTSC_PED, GRUB_XHCI_PORTSC_CSC);
  2572. + }
  2573. +
  2574. + if (!(portsc & GRUB_XHCI_PORTSC_CCS))
  2575. + return GRUB_USB_SPEED_NONE;
  2576. +
  2577. + for (grub_uint8_t i = 0; i < 16 && x->psids[port-1].psids[i].id > 0; i++)
  2578. + {
  2579. + if (x->psids[port-1].psids[i].id == speed)
  2580. + {
  2581. + grub_dprintf("xhci", "%s: grub_usb_speed = %d\n", __func__,
  2582. + x->psids[port-1].psids[i].grub_usb_speed );
  2583. + return x->psids[port-1].psids[i].grub_usb_speed;
  2584. + }
  2585. + }
  2586. +
  2587. + return GRUB_USB_SPEED_NONE;
  2588. +}
  2589. +
  2590. +/****************************************************************
  2591. + * xHCI attach/detach functions
  2592. + ****************************************************************/
  2593. +
  2594. +static grub_usb_err_t
  2595. +grub_xhci_attach_dev (grub_usb_controller_t ctrl, grub_usb_device_t dev)
  2596. +{
  2597. + struct grub_xhci *x = (struct grub_xhci *) ctrl->data;
  2598. + grub_usb_err_t err;
  2599. + grub_uint32_t max;
  2600. +
  2601. + grub_dprintf("xhci", "%s: dev=%p\n", __func__, dev);
  2602. +
  2603. + if (!dev || !x)
  2604. + return GRUB_USB_ERR_INTERNAL;
  2605. +
  2606. + dev->xhci_priv = grub_zalloc (sizeof (struct grub_xhci_priv));
  2607. + if (!dev->xhci_priv)
  2608. + return GRUB_USB_ERR_INTERNAL;
  2609. +
  2610. +
  2611. + switch (dev->speed)
  2612. + {
  2613. + case GRUB_USB_SPEED_LOW:
  2614. + {
  2615. + max = 8;
  2616. + break;
  2617. + }
  2618. + case GRUB_USB_SPEED_FULL:
  2619. + case GRUB_USB_SPEED_HIGH:
  2620. + {
  2621. + max = 64;
  2622. + break;
  2623. + }
  2624. + case GRUB_USB_SPEED_SUPER:
  2625. + {
  2626. + max = 512;
  2627. + break;
  2628. + }
  2629. + default:
  2630. + case GRUB_USB_SPEED_NONE:
  2631. + {
  2632. + max = 0;
  2633. + }
  2634. + }
  2635. +
  2636. + /* Assign a slot, assign an address and configure endpoint 0 */
  2637. + err = grub_xhci_prepare_endpoint(x, dev,
  2638. + 0,
  2639. + 0,
  2640. + GRUB_USB_TRANSACTION_TYPE_CONTROL,
  2641. + max,
  2642. + dev->xhci_priv);
  2643. +
  2644. + return err;
  2645. +}
  2646. +
  2647. +static grub_usb_err_t
  2648. +grub_xhci_detach_dev (grub_usb_controller_t ctrl, grub_usb_device_t dev)
  2649. +{
  2650. + struct grub_xhci *x = (struct grub_xhci *) ctrl->data;
  2651. + struct grub_xhci_priv *priv;
  2652. + int cc = CC_SUCCESS;
  2653. +
  2654. + grub_dprintf("xhci", "%s: dev=%p\n", __func__, dev);
  2655. +
  2656. + if (!dev)
  2657. + return GRUB_USB_ERR_INTERNAL;
  2658. +
  2659. + if (dev->xhci_priv)
  2660. + {
  2661. + priv = dev->xhci_priv;
  2662. + /* Stop endpoints and free ring buffer */
  2663. + for (int i = 0; i < GRUB_XHCI_MAX_ENDPOINTS; i++)
  2664. + {
  2665. + if (priv->enpoint_trbs[i] != NULL)
  2666. + {
  2667. + cc = xhci_cmd_stop_endpoint(x, priv->slotid, i, 1);
  2668. + if (cc != CC_SUCCESS)
  2669. + grub_dprintf("xhci", "Failed to disable EP%d on slot %d\n", i,
  2670. + priv->slotid);
  2671. +
  2672. + grub_dprintf("xhci", "grub_dma_free[%d]\n", i);
  2673. +
  2674. + grub_dma_free(priv->enpoint_trbs_dma[i]);
  2675. + priv->enpoint_trbs[i] = NULL;
  2676. + priv->enpoint_trbs_dma[i] = NULL;
  2677. + }
  2678. + }
  2679. +
  2680. + cc = xhci_cmd_disable_slot(x, priv->slotid);
  2681. + if (cc == CC_SUCCESS)
  2682. + {
  2683. + if (priv->slotctx_dma)
  2684. + grub_dma_free(priv->slotctx_dma);
  2685. + x->devs[priv->slotid].ptr_low = 0;
  2686. + x->devs[priv->slotid].ptr_high = 0;
  2687. + grub_arch_sync_dma_caches(&x->devs[priv->slotid], sizeof(x->devs[0]));
  2688. + }
  2689. + else
  2690. + grub_dprintf("xhci", "Failed to disable slot %d\n", priv->slotid);
  2691. +
  2692. + grub_free(dev->xhci_priv);
  2693. + }
  2694. +
  2695. + dev->xhci_priv = NULL;
  2696. +
  2697. + if (cc != CC_SUCCESS)
  2698. + return GRUB_USB_ERR_BADDEVICE;
  2699. + return GRUB_USB_ERR_NONE;
  2700. +}
  2701. +
  2702. +/****************************************************************
  2703. + * xHCI terminate functions
  2704. + ****************************************************************/
  2705. +
  2706. +static void
  2707. +grub_xhci_halt(struct grub_xhci *x)
  2708. +{
  2709. + grub_uint32_t reg;
  2710. +
  2711. + /* Halt the command ring */
  2712. + reg = grub_xhci_read32(&x->op->crcr_low);
  2713. + grub_xhci_write32(&x->op->crcr_low, reg | 4);
  2714. +
  2715. + int rc = xhci_event_wait(x, x->cmds, 100);
  2716. + grub_dprintf("xhci", "%s: xhci_event_wait = %d\n", __func__, rc);
  2717. + if (rc < 0)
  2718. + return;
  2719. +
  2720. + /* Stop the controller */
  2721. + reg = grub_xhci_read32(&x->op->usbcmd);
  2722. + if (reg & GRUB_XHCI_CMD_RS)
  2723. + {
  2724. + reg &= ~GRUB_XHCI_CMD_RS;
  2725. + grub_xhci_write32(&x->op->usbcmd, reg);
  2726. + }
  2727. +
  2728. + return;
  2729. +}
  2730. +
  2731. +static grub_err_t
  2732. +grub_xhci_fini_hw (int noreturn __attribute__ ((unused)))
  2733. +{
  2734. + struct grub_xhci *x;
  2735. +
  2736. + /* We should disable all XHCI HW to prevent any DMA access etc. */
  2737. + for (x = xhci; x; x = x->next)
  2738. + {
  2739. + x->shutdown = 1;
  2740. +
  2741. + /* Gracefully detach active devices */
  2742. + grub_usb_poll_devices(0);
  2743. +
  2744. + /* Check if xHCI is halted and halt it if not */
  2745. + grub_xhci_halt (x);
  2746. +
  2747. + /* Reset xHCI */
  2748. + if (grub_xhci_reset (x) != GRUB_USB_ERR_NONE)
  2749. + return GRUB_ERR_BAD_DEVICE;
  2750. + }
  2751. +
  2752. + return GRUB_ERR_NONE;
  2753. +}
  2754. +
  2755. +static struct grub_usb_controller_dev usb_controller = {
  2756. + .name = "xhci",
  2757. + .iterate = grub_xhci_iterate,
  2758. + .setup_transfer = grub_xhci_setup_transfer,
  2759. + .check_transfer = grub_xhci_check_transfer,
  2760. + .cancel_transfer = grub_xhci_cancel_transfer,
  2761. + .hubports = grub_xhci_hubports,
  2762. + .portstatus = grub_xhci_portstatus,
  2763. + .detect_dev = grub_xhci_detect_dev,
  2764. + .attach_dev = grub_xhci_attach_dev,
  2765. + .detach_dev = grub_xhci_detach_dev,
  2766. + /* estimated max. count of TDs for one bulk transfer */
  2767. + .max_bulk_tds = GRUB_XHCI_RING_ITEMS - 3
  2768. +};
  2769. +
  2770. +GRUB_MOD_INIT (xhci)
  2771. +{
  2772. + grub_stop_disk_firmware ();
  2773. +
  2774. + grub_boot_time ("Initing XHCI hardware");
  2775. + grub_xhci_pci_scan ();
  2776. + grub_boot_time ("Registering XHCI driver");
  2777. + grub_usb_controller_dev_register (&usb_controller);
  2778. + grub_boot_time ("XHCI driver registered");
  2779. +}
  2780. +
  2781. +GRUB_MOD_FINI (xhci)
  2782. +{
  2783. + grub_xhci_fini_hw (0);
  2784. + grub_usb_controller_dev_unregister (&usb_controller);
  2785. +}
  2786. diff --git a/include/grub/usb.h b/include/grub/usb.h
  2787. index 609faf7d0..eb71fa1c7 100644
  2788. --- a/include/grub/usb.h
  2789. +++ b/include/grub/usb.h
  2790. @@ -338,6 +338,10 @@ grub_usb_cancel_transfer (grub_usb_transfer_t trans);
  2791. void
  2792. grub_ehci_init_device (volatile void *regs);
  2793. void
  2794. +grub_xhci_init_device (volatile void *regs);
  2795. +void
  2796. grub_ehci_pci_scan (void);
  2797. +void
  2798. +grub_xhci_pci_scan (void);
  2799. #endif /* GRUB_USB_H */
  2800. --
  2801. 2.39.2