usbehci.c 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610
  1. /*
  2. * USB Enhanced Host Controller Interface (EHCI) driver
  3. * High speed USB 2.0.
  4. *
  5. * BUGS:
  6. * - Too many delays and ilocks.
  7. * - bandwidth admission control must be done per-frame.
  8. * - requires polling (some controllers miss interrupts).
  9. * - must warn of power overruns.
  10. */
  11. #include "u.h"
  12. #include "../port/lib.h"
  13. #include "mem.h"
  14. #include "dat.h"
  15. #include "fns.h"
  16. #include "io.h"
  17. #include "../port/error.h"
  18. #include "usb.h"
  19. #include "usbehci.h"
  20. #include "uncached.h"
  21. typedef struct Ctlio Ctlio;
  22. typedef struct Ctlr Ctlr;
  23. typedef union Ed Ed;
  24. typedef struct Edpool Edpool;
  25. typedef struct Fstn Fstn;
  26. typedef struct Isoio Isoio;
  27. typedef struct Itd Itd;
  28. typedef struct Kwusb Kwusb;
  29. typedef struct Kwusbtt Kwusbtt;
  30. typedef struct Poll Poll;
  31. typedef struct Qh Qh;
  32. typedef struct Qio Qio;
  33. typedef struct Qtd Qtd;
  34. typedef struct Qtree Qtree;
  35. typedef struct Sitd Sitd;
  36. typedef struct Td Td;
  37. typedef struct Usbwin Usbwin;
  38. enum {
  39. Debug = 0,
  40. };
  41. /*
  42. * EHCI interface registers and bits
  43. */
  44. enum
  45. {
  46. /* Queue states (software) */
  47. Qidle = 0,
  48. Qinstall,
  49. Qrun,
  50. Qdone,
  51. Qclose,
  52. Qfree,
  53. Enabledelay = 100, /* waiting for a port to enable */
  54. Abortdelay = 5, /* delay after cancelling Tds (ms) */
  55. Incr = 64, /* for pools of Tds, Qhs, etc. */
  56. Align = 128, /* in bytes for all those descriptors */
  57. /* Keep them as a power of 2, lower than ctlr->nframes */
  58. /* Also, keep Nisoframes >= Nintrleafs */
  59. Nintrleafs = 32, /* nb. of leaf frames in intr. tree */
  60. Nisoframes = 64, /* nb. of iso frames (in window) */
  61. /*
  62. * HW constants
  63. */
  64. /* Itd bits (csw[]) */
  65. Itdactive = 0x80000000, /* execution enabled */
  66. Itddberr = 0x40000000, /* data buffer error */
  67. Itdbabble = 0x20000000, /* babble error */
  68. Itdtrerr = 0x10000000, /* transaction error */
  69. Itdlenshift = 16, /* transaction length */
  70. Itdlenmask = 0xFFF,
  71. Itdioc = 0x00008000, /* interrupt on complete */
  72. Itdpgshift = 12, /* page select field */
  73. Itdoffshift = 0, /* transaction offset */
  74. /* Itd bits, buffer[] */
  75. Itdepshift = 8, /* endpoint address (buffer[0]) */
  76. Itddevshift = 0, /* device address (buffer[0]) */
  77. Itdin = 0x800, /* is input (buffer[1]) */
  78. Itdout = 0,
  79. Itdmaxpktshift = 0, /* max packet (buffer[1]) */
  80. Itdntdsshift = 0, /* nb. of tds per µframe (buffer[2]) */
  81. Itderrors = Itddberr|Itdbabble|Itdtrerr,
  82. /* Sitd bits (epc) */
  83. Stdin = 0x80000000, /* input direction */
  84. Stdportshift = 24, /* hub port number */
  85. Stdhubshift = 16, /* hub address */
  86. Stdepshift = 8, /* endpoint address */
  87. Stddevshift = 0, /* device address */
  88. /* Sitd bits (mfs) */
  89. Stdssmshift = 0, /* split start mask */
  90. Stdscmshift = 8, /* split complete mask */
  91. /* Sitd bits (csw) */
  92. Stdioc = 0x80000000, /* interrupt on complete */
  93. Stdpg = 0x40000000, /* page select */
  94. Stdlenshift = 16, /* total bytes to transfer */
  95. Stdlenmask = 0x3FF,
  96. Stdactive = 0x00000080, /* active */
  97. Stderr = 0x00000040, /* tr. translator error */
  98. Stddberr = 0x00000020, /* data buffer error */
  99. Stdbabble = 0x00000010, /* babble error */
  100. Stdtrerr = 0x00000008, /* transaction error */
  101. Stdmmf = 0x00000004, /* missed µframe */
  102. Stddcs = 0x00000002, /* do complete split */
  103. Stderrors = Stderr|Stddberr|Stdbabble|Stdtrerr|Stdmmf,
  104. /* Sitd bits buffer[1] */
  105. Stdtpall = 0x00000000, /* all payload here (188 bytes) */
  106. Stdtpbegin = 0x00000008, /* first payload for fs trans. */
  107. Stdtcntmask = 0x00000007, /* T-count */
  108. /* Td bits (csw) */
  109. Tddata1 = 0x80000000, /* data toggle 1 */
  110. Tddata0 = 0x00000000, /* data toggle 0 */
  111. Tdlenshift = 16, /* total bytes to transfer */
  112. Tdlenmask = 0x7FFF,
  113. Tdmaxpkt = 0x5000, /* max buffer for a Td */
  114. Tdioc = 0x00008000, /* interrupt on complete */
  115. Tdpgshift = 12, /* current page */
  116. Tdpgmask = 7,
  117. Tderr1 = 0x00000400, /* bit 0 of error counter */
  118. Tderr2 = 0x00000800, /* bit 1 of error counter */
  119. Tdtokout = 0x00000000, /* direction out */
  120. Tdtokin = 0x00000100, /* direction in */
  121. Tdtoksetup = 0x00000200, /* setup packet */
  122. Tdtok = 0x00000300, /* token bits */
  123. Tdactive = 0x00000080, /* active */
  124. Tdhalt = 0x00000040, /* halted */
  125. Tddberr = 0x00000020, /* data buffer error */
  126. Tdbabble = 0x00000010, /* babble error */
  127. Tdtrerr = 0x00000008, /* transaction error */
  128. Tdmmf = 0x00000004, /* missed µframe */
  129. Tddcs = 0x00000002, /* do complete split */
  130. Tdping = 0x00000001, /* do ping */
  131. Tderrors = Tdhalt|Tddberr|Tdbabble|Tdtrerr|Tdmmf,
  132. /* Qh bits (eps0) */
  133. Qhrlcmask = 0xF, /* nak reload count */
  134. Qhrlcshift = 28, /* nak reload count */
  135. Qhnhctl = 0x08000000, /* not-high speed ctl */
  136. Qhmplmask = 0x7FF, /* max packet */
  137. Qhmplshift = 16,
  138. Qhhrl = 0x00008000, /* head of reclamation list */
  139. Qhdtc = 0x00004000, /* data toggle ctl. */
  140. Qhint = 0x00000080, /* inactivate on next transition */
  141. Qhspeedmask = 0x00003000, /* speed bits */
  142. Qhfull = 0x00000000, /* full speed */
  143. Qhlow = 0x00001000, /* low speed */
  144. Qhhigh = 0x00002000, /* high speed */
  145. /* Qh bits (eps1) */
  146. Qhmultshift = 30, /* multiple tds per µframe */
  147. Qhmultmask = 3,
  148. Qhportshift = 23, /* hub port number */
  149. Qhhubshift = 16, /* hub address */
  150. Qhscmshift = 8, /* split completion mask bits */
  151. Qhismshift = 0, /* interrupt sched. mask bits */
  152. };
  153. /*
  154. * Endpoint tree (software)
  155. */
  156. struct Qtree
  157. {
  158. int nel;
  159. int depth;
  160. ulong* bw;
  161. Qh** root;
  162. };
  163. /*
  164. * One per endpoint per direction, to control I/O.
  165. */
  166. struct Qio
  167. {
  168. QLock; /* for the entire I/O process */
  169. Rendez; /* wait for completion */
  170. Qh* qh; /* Td list (field const after init) */
  171. int usbid; /* usb address for endpoint/device */
  172. int toggle; /* Tddata0/Tddata1 */
  173. int tok; /* Tdtoksetup, Tdtokin, Tdtokout */
  174. ulong iotime; /* last I/O time; to hold interrupt polls */
  175. int debug; /* debug flag from the endpoint */
  176. char* err; /* error string */
  177. char* tag; /* debug (no room in Qh for this) */
  178. ulong bw;
  179. };
  180. struct Ctlio
  181. {
  182. Qio; /* a single Qio for each RPC */
  183. uchar* data; /* read from last ctl req. */
  184. int ndata; /* number of bytes read */
  185. };
  186. struct Isoio
  187. {
  188. QLock;
  189. Rendez; /* wait for space/completion/errors */
  190. int usbid; /* address used for device/endpoint */
  191. int tok; /* Tdtokin or Tdtokout */
  192. int state; /* Qrun -> Qdone -> Qrun... -> Qclose */
  193. int nframes; /* number of frames ([S]Itds) used */
  194. uchar* data; /* iso data buffers if not embedded */
  195. char* err; /* error string */
  196. int nerrs; /* nb of consecutive I/O errors */
  197. ulong maxsize; /* ntds * ep->maxpkt */
  198. long nleft; /* number of bytes left from last write */
  199. int debug; /* debug flag from the endpoint */
  200. int hs; /* is high speed? */
  201. Isoio* next; /* in list of active Isoios */
  202. ulong td0frno; /* first frame used in ctlr */
  203. union{
  204. Itd* tdi; /* next td processed by interrupt */
  205. Sitd* stdi;
  206. };
  207. union{
  208. Itd* tdu; /* next td for user I/O in tdps */
  209. Sitd* stdu;
  210. };
  211. union{
  212. Itd** itdps; /* itdps[i]: ptr to Itd for i-th frame or nil */
  213. Sitd** sitdps; /* sitdps[i]: ptr to Sitd for i-th frame or nil */
  214. ulong** tdps; /* same thing, as seen by hw */
  215. };
  216. };
  217. struct Poll
  218. {
  219. Lock;
  220. Rendez;
  221. int must;
  222. int does;
  223. };
  224. struct Ctlr
  225. {
  226. Rendez; /* for waiting to async advance doorbell */
  227. Lock; /* for ilock. qh lists and basic ctlr I/O */
  228. QLock portlck; /* for port resets/enable... (and doorbell) */
  229. int active; /* in use or not */
  230. Ecapio* capio; /* Capability i/o regs */
  231. Eopio* opio; /* Operational i/o regs */
  232. int nframes; /* 1024, 512, or 256 frames in the list */
  233. ulong* frames; /* periodic frame list (hw) */
  234. Qh* qhs; /* async Qh circular list for bulk/ctl */
  235. Qtree* tree; /* tree of Qhs for the periodic list */
  236. int ntree; /* number of dummy qhs in tree */
  237. Qh* intrqhs; /* list of (not dummy) qhs in tree */
  238. Isoio* iso; /* list of active Iso I/O */
  239. ulong load;
  240. ulong isoload;
  241. int nintr; /* number of interrupts attended */
  242. int ntdintr; /* number of intrs. with something to do */
  243. int nqhintr; /* number of async td intrs. */
  244. int nisointr; /* number of periodic td intrs. */
  245. int nreqs;
  246. Poll poll;
  247. };
  248. struct Edpool
  249. {
  250. Lock;
  251. Ed* free;
  252. int nalloc;
  253. int ninuse;
  254. int nfree;
  255. };
  256. /*
  257. * We use the 64-bit version for Itd, Sitd, Td, and Qh.
  258. * If the ehci is 64-bit capable it assumes we are using those
  259. * structures even when the system is 32 bits.
  260. */
  261. /*
  262. * Iso transfer descriptor. hw: 92 bytes, 108 bytes total
  263. * aligned to 32.
  264. */
  265. struct Itd
  266. {
  267. ulong link; /* to next hw struct */
  268. ulong csw[8]; /* sts/length/pg/off. updated by hw */
  269. ulong buffer[7]; /* buffer pointers, addrs, maxsz */
  270. ulong xbuffer[7]; /* high 32 bits of buffer for 64-bits */
  271. ulong _pad0; /* pad to next cache line */
  272. /* cache-line boundary here */
  273. /* software */
  274. Itd* next;
  275. ulong ndata; /* number of bytes in data */
  276. ulong mdata; /* max number of bytes in data */
  277. uchar* data;
  278. };
  279. /*
  280. * Split transaction iso transfer descriptor.
  281. * hw: 36 bytes, 52 bytes total. aligned to 32.
  282. */
  283. struct Sitd
  284. {
  285. ulong link; /* to next hw struct */
  286. ulong epc; /* static endpoint state. addrs */
  287. ulong mfs; /* static endpoint state. µ-frame sched. */
  288. ulong csw; /* transfer state. updated by hw */
  289. ulong buffer[2]; /* buf. ptr/offset. offset updated by hw */
  290. /* buf ptr/TP/Tcnt. TP/Tcnt updated by hw */
  291. ulong blink; /* back pointer */
  292. /* cache-line boundary after xbuffer[0] */
  293. ulong xbuffer[2]; /* high 32 bits of buffer for 64-bits */
  294. /* software */
  295. Sitd* next;
  296. ulong ndata; /* number of bytes in data */
  297. ulong mdata; /* max number of bytes in data */
  298. uchar* data;
  299. };
  300. /*
  301. * Queue element transfer descriptor.
  302. * hw: first 52 bytes, total 68+sbuff bytes. aligned to 32 bytes.
  303. */
  304. struct Td
  305. {
  306. ulong nlink; /* to next Td */
  307. ulong alink; /* alternate link to next Td */
  308. ulong csw; /* cmd/sts. updated by hw */
  309. ulong buffer[5]; /* buf ptrs. offset updated by hw */
  310. /* cache-line boundary here */
  311. ulong xbuffer[5]; /* high 32 bits of buffer for 64-bits */
  312. /* software */
  313. Td* next; /* in qh or Isoio or free list */
  314. ulong ndata; /* bytes available/used at data */
  315. uchar* data; /* pointer to actual data */
  316. uchar* buff; /* allocated data buffer or nil */
  317. uchar sbuff[1]; /* first byte of embedded buffer */
  318. };
  319. /*
  320. * Queue head. Aligned to 32 bytes.
  321. * hw: first 68 bytes, 92 total.
  322. */
  323. struct Qh
  324. {
  325. ulong link; /* to next Qh in round robin */
  326. ulong eps0; /* static endpoint state. addrs */
  327. ulong eps1; /* static endpoint state. µ-frame sched. */
  328. /* updated by hw */
  329. ulong clink; /* current Td (No Term bit here!) */
  330. ulong nlink; /* to next Td */
  331. ulong alink; /* alternate link to next Td */
  332. ulong csw; /* cmd/sts. updated by hw */
  333. /* cache-line boundary after buffer[0] */
  334. ulong buffer[5]; /* buf ptrs. offset updated by hw */
  335. ulong xbuffer[5]; /* high 32 bits of buffer for 64-bits */
  336. /* software */
  337. Qh* next; /* in controller list/tree of Qhs */
  338. int state; /* Qidle -> Qinstall -> Qrun -> Qdone | Qclose */
  339. Qio* io; /* for this queue */
  340. Td* tds; /* for this queue */
  341. int sched; /* slot for for intr. Qhs */
  342. Qh* inext; /* next in list of intr. qhs */
  343. };
  344. /*
  345. * We can avoid frame span traversal nodes if we don't span frames.
  346. * Just schedule transfers that can fit on the current frame and
  347. * wait a little bit otherwise.
  348. */
  349. /*
  350. * Software. Ehci descriptors provided by pool.
  351. * There are soo few because we avoid using Fstn.
  352. */
  353. union Ed
  354. {
  355. Ed* next; /* in free list */
  356. Qh qh;
  357. Td td;
  358. Itd itd;
  359. Sitd sitd;
  360. uchar align[Align];
  361. };
  362. /* kirkwood usb transaction translator registers? (undocumented) */
  363. struct Kwusbtt { /* at soc.ehci */
  364. ulong id;
  365. ulong hwgeneral;
  366. ulong hwhost;
  367. ulong hwdevice;
  368. ulong hwtxbuf;
  369. ulong hwrxbuf;
  370. ulong hwtttxbuf;
  371. ulong hwttrxbuf;
  372. };
  373. /* kirkwood usb bridge & phy registers */
  374. struct Kwusb { /* at offset 0x300 from soc.ehci */
  375. ulong bcs; /* bridge ctl & sts */
  376. uchar _pad0[0x310-0x304];
  377. ulong bic; /* bridge intr. cause */
  378. ulong bim; /* bridge intr. mask */
  379. ulong _pad1;
  380. ulong bea; /* bridge error addr. */
  381. struct Usbwin {
  382. ulong ctl; /* see Winenable in io.h */
  383. ulong base;
  384. ulong _pad2[2];
  385. } win[4];
  386. ulong phycfg; /* phy config. */
  387. uchar _pad3[0x400-0x364];
  388. ulong pwrctl; /* power control */
  389. uchar _pad4[0x410-0x404];
  390. ulong phypll; /* phy pll control */
  391. uchar _pad5[0x420-0x414];
  392. ulong phytxctl; /* phy transmit control */
  393. uchar _pad6[0x430-0x424];
  394. ulong phyrxctl; /* phy receive control */
  395. uchar _pad7[0x440-0x434];
  396. ulong phyivref; /* phy ivref control */
  397. };
  398. #define diprint if(debug || iso->debug)print
  399. #define ddiprint if(debug>1 || iso->debug>1)print
  400. #define dqprint if(debug || (qh->io && qh->io->debug))print
  401. #define ddqprint if(debug>1 || (qh->io && qh->io->debug>1))print
  402. #define TRUNC(x, sz) ((x) & ((sz)-1))
  403. #define LPTR(q) ((ulong*)KADDR((q) & ~0x1F))
  404. static int debug;
  405. static Edpool edpool;
  406. static Ctlr* ctlrs[Nhcis];
  407. static char Ebug[] = "not yet implemented";
  408. static char* qhsname[] = { "idle", "install", "run", "done", "close", "FREE" };
  409. static int
  410. isphys(void *p)
  411. {
  412. return ((uintptr)p & KSEGM) == (PHYSDRAM & KSEGM);
  413. }
  414. static void
  415. xcachewbse(void *va, long sz)
  416. {
  417. #ifdef smalloc /* using uncached memory */
  418. USED(va, sz);
  419. coherence();
  420. #else
  421. if (isphys(va))
  422. panic("xcachewbse: phys addr %#p", va);
  423. cachedwbse(va, sz);
  424. l2cacheuwbse(va, sz);
  425. #endif
  426. }
  427. /*
  428. * this is almost always the wrong thing to do.
  429. * given a dma buffer to operate on,
  430. * you want to invalidate before reading and
  431. * write back after writing, but writing back and
  432. * then invalidating is rarely correct.
  433. */
  434. static void
  435. xcachewbinvse(void *va, long sz)
  436. {
  437. #ifdef smalloc /* using uncached memory */
  438. USED(va, sz);
  439. coherence();
  440. #else
  441. if (isphys(va))
  442. panic("xcachewbinvse: phys addr %#p", va);
  443. cachedwbinvse(va, sz);
  444. l2cacheuwbinvse(va, sz);
  445. #endif
  446. }
  447. static void
  448. xcacheinvse(void *va, long sz)
  449. {
  450. #ifdef smalloc /* using uncached memory */
  451. USED(va, sz);
  452. #else
  453. if (isphys(va))
  454. panic("xcacheinvse: phys addr %#p", va);
  455. l2cacheuinvse(va, sz);
  456. cachedinvse(va, sz);
  457. #endif
  458. }
  459. static void
  460. ehcirun(Ctlr *ctlr, int on)
  461. {
  462. int i;
  463. Eopio *opio;
  464. ddprint("ehci %#p %s\n", ctlr->capio, on ? "starting" : "halting");
  465. opio = ctlr->opio;
  466. if(on)
  467. opio->cmd |= Crun;
  468. else
  469. opio->cmd = Cstop;
  470. coherence();
  471. for(i = 0; i < 100; i++)
  472. if(on == 0 && (opio->sts & Shalted) != 0)
  473. break;
  474. else if(on != 0 && (opio->sts & Shalted) == 0)
  475. break;
  476. else
  477. delay(1);
  478. if(i == 100)
  479. print("ehci %#p %s cmd timed out\n",
  480. ctlr->capio, on ? "run" : "halt");
  481. ddprint("ehci %#p cmd %#lux sts %#lux\n",
  482. ctlr->capio, opio->cmd, opio->sts);
  483. }
  484. static void*
  485. edalloc(void)
  486. {
  487. Ed *ed, *pool;
  488. int i;
  489. lock(&edpool);
  490. if(edpool.free == nil){
  491. pool = xspanalloc(Incr*sizeof(Ed), Align, 0);
  492. if(pool == nil)
  493. panic("edalloc");
  494. for(i=Incr; --i>=0;){
  495. pool[i].next = edpool.free;
  496. edpool.free = &pool[i];
  497. }
  498. edpool.nalloc += Incr;
  499. edpool.nfree += Incr;
  500. dprint("ehci: edalloc: %d eds\n", edpool.nalloc);
  501. }
  502. ed = edpool.free;
  503. edpool.free = ed->next;
  504. edpool.ninuse++;
  505. edpool.nfree--;
  506. unlock(&edpool);
  507. memset(ed, 0, sizeof(Ed)); /* safety */
  508. assert(((ulong)ed & 0xF) == 0);
  509. return ed;
  510. }
  511. static void
  512. edfree(void *a)
  513. {
  514. Ed *ed;
  515. ed = a;
  516. lock(&edpool);
  517. ed->next = edpool.free;
  518. edpool.free = ed;
  519. edpool.ninuse--;
  520. edpool.nfree++;
  521. unlock(&edpool);
  522. }
  523. /*
  524. * Allocate and do some initialization.
  525. * Free after releasing buffers used.
  526. */
  527. static Itd*
  528. itdalloc(void)
  529. {
  530. Itd *td;
  531. td = edalloc();
  532. td->link = Lterm;
  533. return td;
  534. }
  535. static void
  536. itdfree(Itd *td)
  537. {
  538. edfree(td);
  539. }
  540. static Sitd*
  541. sitdalloc(void)
  542. {
  543. Sitd *td;
  544. td = edalloc();
  545. td->link = td->blink = Lterm;
  546. return td;
  547. }
  548. static void
  549. sitdfree(Sitd *td)
  550. {
  551. edfree(td);
  552. }
  553. static Td*
  554. tdalloc(void)
  555. {
  556. Td *td;
  557. td = edalloc();
  558. td->nlink = td->alink = Lterm;
  559. return td;
  560. }
  561. static void
  562. tdfree(Td *td)
  563. {
  564. if(td == nil)
  565. return;
  566. free(td->buff);
  567. edfree(td);
  568. }
  569. static void
  570. tdlinktd(Td *td, Td *next)
  571. {
  572. td->next = next;
  573. td->alink = Lterm;
  574. if(next == nil)
  575. td->nlink = Lterm;
  576. else
  577. td->nlink = PADDR(next);
  578. xcachewbse(&td->alink, sizeof td->alink); /* also nlink */
  579. }
  580. static Qh*
  581. qhlinkqh(Qh *qh, Qh *next)
  582. {
  583. qh->next = next;
  584. qh->link = PADDR(next)|Lqh;
  585. xcachewbse(&qh->link, sizeof qh->link); /* also ?link, csw */
  586. return qh;
  587. }
  588. static void
  589. qhsetaddr(Qh *qh, ulong addr)
  590. {
  591. ulong eps0;
  592. xcacheinvse(&qh->eps0, sizeof qh->eps0);
  593. eps0 = qh->eps0 & ~((Epmax<<8)|Devmax);
  594. qh->eps0 = eps0 | addr & Devmax | ((addr >> 7) & Epmax) << 8;
  595. xcachewbse(&qh->eps0, sizeof qh->eps0); /* also *link, csw */
  596. }
  597. /*
  598. * return smallest power of 2 <= n
  599. */
  600. static int
  601. flog2lower(int n)
  602. {
  603. int i;
  604. for(i = 0; (1 << (i + 1)) <= n; i++)
  605. ;
  606. return i;
  607. }
  608. static int
  609. pickschedq(Qtree *qt, int pollival, ulong bw, ulong limit)
  610. {
  611. int i, j, d, upperb, q;
  612. ulong best, worst, total;
  613. d = flog2lower(pollival);
  614. if(d > qt->depth)
  615. d = qt->depth;
  616. q = -1;
  617. worst = 0;
  618. best = ~0;
  619. upperb = (1 << (d+1)) - 1;
  620. for(i = (1 << d) - 1; i < upperb; i++){
  621. total = qt->bw[0];
  622. for(j = i; j > 0; j = (j - 1) / 2)
  623. total += qt->bw[j];
  624. if(total < best){
  625. best = total;
  626. q = i;
  627. }
  628. if(total > worst)
  629. worst = total;
  630. }
  631. if(worst + bw >= limit)
  632. return -1;
  633. return q;
  634. }
  635. static int
  636. schedq(Ctlr *ctlr, Qh *qh, int pollival)
  637. {
  638. int q;
  639. Qh *tqh;
  640. ulong bw;
  641. bw = qh->io->bw;
  642. q = pickschedq(ctlr->tree, pollival, 0, ~0);
  643. ddqprint("ehci: sched %#p q %d, ival %d, bw %uld\n",
  644. qh->io, q, pollival, bw);
  645. if(q < 0){
  646. print("ehci: no room for ed\n");
  647. return -1;
  648. }
  649. ctlr->tree->bw[q] += bw;
  650. tqh = ctlr->tree->root[q];
  651. qh->sched = q;
  652. qhlinkqh(qh, tqh->next);
  653. qhlinkqh(tqh, qh);
  654. qh->inext = ctlr->intrqhs;
  655. ctlr->intrqhs = qh;
  656. return 0;
  657. }
  658. static void
  659. unschedq(Ctlr *ctlr, Qh *qh)
  660. {
  661. int q;
  662. Qh *prev, *this, *next;
  663. Qh **l;
  664. ulong bw;
  665. bw = qh->io->bw;
  666. q = qh->sched;
  667. if(q < 0)
  668. return;
  669. ctlr->tree->bw[q] -= bw;
  670. prev = ctlr->tree->root[q];
  671. this = prev->next;
  672. while(this != nil && this != qh){
  673. prev = this;
  674. this = this->next;
  675. }
  676. if(this == nil)
  677. print("ehci: unschedq %d: not found\n", q);
  678. else{
  679. next = this->next;
  680. qhlinkqh(prev, next);
  681. }
  682. for(l = &ctlr->intrqhs; *l != nil; l = &(*l)->inext)
  683. if(*l == qh){
  684. *l = (*l)->inext;
  685. return;
  686. }
  687. print("ehci: unschedq: qh %#p not found\n", qh);
  688. }
  689. static ulong
  690. qhmaxpkt(Qh *qh)
  691. {
  692. xcacheinvse(&qh->eps0, sizeof qh->eps0); /* also *link, csw */
  693. return (qh->eps0 >> Qhmplshift) & Qhmplmask;
  694. }
  695. static void
  696. qhsetmaxpkt(Qh *qh, int maxpkt)
  697. {
  698. ulong eps0;
  699. xcacheinvse(&qh->eps0, sizeof qh->eps0); /* also *link, csw */
  700. eps0 = qh->eps0 & ~(Qhmplmask << Qhmplshift);
  701. qh->eps0 = eps0 | (maxpkt & Qhmplmask) << Qhmplshift;
  702. xcachewbse(&qh->eps0, sizeof qh->eps0); /* also *link, csw */
  703. }
  704. /*
  705. * Initialize the round-robin circular list of ctl/bulk Qhs
  706. * if ep is nil. Otherwise, allocate and link a new Qh in the ctlr.
  707. */
  708. static Qh*
  709. qhalloc(Ctlr *ctlr, Ep *ep, Qio *io, char* tag)
  710. {
  711. Qh *qh;
  712. int ttype;
  713. qh = edalloc();
  714. qh->nlink = Lterm;
  715. qh->alink = Lterm;
  716. qh->csw = Tdhalt;
  717. qh->state = Qidle;
  718. qh->sched = -1;
  719. qh->io = io;
  720. if(ep != nil){
  721. qh->eps0 = 0;
  722. qhsetmaxpkt(qh, ep->maxpkt);
  723. if(ep->dev->speed == Lowspeed)
  724. qh->eps0 |= Qhlow;
  725. if(ep->dev->speed == Highspeed)
  726. qh->eps0 |= Qhhigh;
  727. else if(ep->ttype == Tctl)
  728. qh->eps0 |= Qhnhctl;
  729. qh->eps0 |= Qhdtc;
  730. qh->eps0 |= (8 << Qhrlcshift); /* 8 naks max */
  731. qhsetaddr(qh, io->usbid);
  732. qh->eps1 = (ep->ntds & Qhmultmask) << Qhmultshift;
  733. qh->eps1 |= ep->dev->port << Qhportshift;
  734. qh->eps1 |= ep->dev->hub << Qhhubshift;
  735. qh->eps1 |= 034 << Qhscmshift;
  736. if(ep->ttype == Tintr)
  737. qh->eps1 |= (1 << Qhismshift); /* intr. start µf. */
  738. if(io != nil)
  739. io->tag = tag;
  740. }
  741. ilock(ctlr);
  742. ttype = Tctl;
  743. if(ep != nil)
  744. ttype = ep->ttype;
  745. xcachewbse(&qh->link, sizeof qh->link); /* also eps?, *link, csw */
  746. xcachewbse(qh->buffer, sizeof qh->buffer);
  747. switch(ttype){
  748. case Tctl:
  749. case Tbulk:
  750. if(ctlr->qhs == nil){
  751. ctlr->qhs = qhlinkqh(qh, qh);
  752. qh->eps0 |= Qhhigh | Qhhrl;
  753. xcachewbse(&qh->eps0, sizeof qh->eps0); /* also *link, csw */
  754. ctlr->opio->link = PADDR(qh)|Lqh;
  755. coherence();
  756. }else{
  757. qhlinkqh(qh, ctlr->qhs->next);
  758. qhlinkqh(ctlr->qhs, qh);
  759. }
  760. break;
  761. case Tintr:
  762. schedq(ctlr, qh, ep->pollival);
  763. break;
  764. default:
  765. print("ehci: qhalloc called for ttype != ctl/bulk\n");
  766. }
  767. iunlock(ctlr);
  768. return qh;
  769. }
  770. static int
  771. qhadvanced(void *a)
  772. {
  773. Ctlr *ctlr;
  774. ctlr = a;
  775. return (ctlr->opio->cmd & Ciasync) == 0;
  776. }
  777. /*
  778. * called when a qh is removed, to be sure the hw is not
  779. * keeping pointers into it.
  780. */
  781. static void
  782. qhcoherency(Ctlr *ctlr)
  783. {
  784. int i;
  785. qlock(&ctlr->portlck);
  786. ctlr->opio->cmd |= Ciasync; /* ask for intr. on async advance */
  787. coherence();
  788. for(i = 0; i < 3 && qhadvanced(ctlr) == 0; i++)
  789. if(!waserror()){
  790. tsleep(ctlr, qhadvanced, ctlr, Abortdelay);
  791. poperror();
  792. }
  793. dprint("ehci: qhcoherency: doorbell %d\n", qhadvanced(ctlr));
  794. if(i == 3)
  795. print("ehci: async advance doorbell did not ring\n");
  796. ctlr->opio->cmd &= ~Ciasync; /* try to clean */
  797. coherence();
  798. qunlock(&ctlr->portlck);
  799. }
  800. static void
  801. qhfree(Ctlr *ctlr, Qh *qh)
  802. {
  803. Td *td, *ltd;
  804. Qh *q;
  805. if(qh == nil)
  806. return;
  807. ilock(ctlr);
  808. if(qh->sched < 0){
  809. for(q = ctlr->qhs; q != nil; q = q->next)
  810. if(q->next == qh)
  811. break;
  812. if(q == nil)
  813. panic("qhfree: nil q");
  814. q->next = qh->next;
  815. q->link = qh->link;
  816. xcachewbse(&q->link, sizeof q->link); /* also eps?, ?link, csw */
  817. }else
  818. unschedq(ctlr, qh);
  819. iunlock(ctlr);
  820. qhcoherency(ctlr);
  821. for(td = qh->tds; td != nil; td = ltd){
  822. ltd = td->next;
  823. tdfree(td);
  824. }
  825. edfree(qh);
  826. }
  827. static void
  828. qhlinktd(Qh *qh, Td *td)
  829. {
  830. ulong csw;
  831. int i;
  832. xcacheinvse(&qh->csw, sizeof qh->csw); /* also eps?, *link */
  833. csw = qh->csw;
  834. qh->tds = td;
  835. if(td == nil)
  836. qh->csw = (csw & ~Tdactive) | Tdhalt;
  837. else{
  838. csw &= Tddata1 | Tdping; /* save */
  839. qh->csw = Tdhalt;
  840. qh->clink = 0;
  841. qh->alink = Lterm;
  842. qh->nlink = PADDR(td);
  843. for(i = 0; i < nelem(qh->buffer); i++)
  844. qh->buffer[i] = 0;
  845. xcachewbse(qh->buffer, sizeof qh->buffer);
  846. xcachewbse(&qh->csw, sizeof qh->csw); /* also eps?, *link */
  847. qh->csw = csw & ~(Tdhalt|Tdactive); /* activate next */
  848. }
  849. xcachewbse(&qh->csw, sizeof qh->csw); /* also eps?, *link */
  850. }
  851. static char*
  852. seprintlink(char *s, char *se, char *name, ulong l, int typed)
  853. {
  854. s = seprint(s, se, "%s %ulx", name, l);
  855. if((l & Lterm) != 0)
  856. return seprint(s, se, "T");
  857. if(typed == 0)
  858. return s;
  859. switch(l & (3<<1)){
  860. case Litd:
  861. return seprint(s, se, "I");
  862. case Lqh:
  863. return seprint(s, se, "Q");
  864. case Lsitd:
  865. return seprint(s, se, "S");
  866. default:
  867. return seprint(s, se, "F");
  868. }
  869. }
  870. static char*
  871. seprintitd(char *s, char *se, Itd *td)
  872. {
  873. int i;
  874. ulong b0, b1;
  875. char flags[6];
  876. char *rw;
  877. if(td == nil)
  878. return seprint(s, se, "<nil itd>\n");
  879. b0 = td->buffer[0];
  880. b1 = td->buffer[1];
  881. s = seprint(s, se, "itd %#p", td);
  882. rw = (b1 & Itdin) ? "in" : "out";
  883. s = seprint(s, se, " %s ep %uld dev %uld max %uld mult %uld",
  884. rw, (b0>>8)&Epmax, (b0&Devmax),
  885. td->buffer[1] & 0x7ff, b1 & 3);
  886. s = seprintlink(s, se, " link", td->link, 1);
  887. s = seprint(s, se, "\n");
  888. for(i = 0; i < nelem(td->csw); i++){
  889. xcacheinvse(&td->csw[i], sizeof td->csw[i]);
  890. memset(flags, '-', 5);
  891. if((td->csw[i] & Itdactive) != 0)
  892. flags[0] = 'a';
  893. if((td->csw[i] & Itdioc) != 0)
  894. flags[1] = 'i';
  895. if((td->csw[i] & Itddberr) != 0)
  896. flags[2] = 'd';
  897. if((td->csw[i] & Itdbabble) != 0)
  898. flags[3] = 'b';
  899. if((td->csw[i] & Itdtrerr) != 0)
  900. flags[4] = 't';
  901. flags[5] = 0;
  902. s = seprint(s, se, "\ttd%d %s", i, flags);
  903. s = seprint(s, se, " len %uld", (td->csw[i] >> 16) & 0x7ff);
  904. s = seprint(s, se, " pg %uld", (td->csw[i] >> 12) & 0x7);
  905. s = seprint(s, se, " off %uld\n", td->csw[i] & 0xfff);
  906. }
  907. s = seprint(s, se, "\tbuffs:");
  908. for(i = 0; i < nelem(td->buffer); i++)
  909. s = seprint(s, se, " %#lux", td->buffer[i] >> 12);
  910. return seprint(s, se, "\n");
  911. }
  912. static char*
  913. seprintsitd(char *s, char *se, Sitd *td)
  914. {
  915. char rw, pg, ss;
  916. char flags[8];
  917. static char pc[4] = { 'a', 'b', 'm', 'e' };
  918. if(td == nil)
  919. return seprint(s, se, "<nil sitd>\n");
  920. xcacheinvse(&td->link, sizeof td->link); /* all hw state */
  921. s = seprint(s, se, "sitd %#p", td);
  922. rw = (td->epc & Stdin) ? 'r' : 'w';
  923. s = seprint(s, se, " %c ep %uld dev %uld",
  924. rw, (td->epc>>8)&0xf, td->epc&0x7f);
  925. s = seprint(s, se, " max %uld", (td->csw >> 16) & 0x3ff);
  926. s = seprint(s, se, " hub %uld", (td->epc >> 16) & 0x7f);
  927. s = seprint(s, se, " port %uld\n", (td->epc >> 24) & 0x7f);
  928. memset(flags, '-', 7);
  929. if((td->csw & Stdactive) != 0)
  930. flags[0] = 'a';
  931. if((td->csw & Stdioc) != 0)
  932. flags[1] = 'i';
  933. if((td->csw & Stderr) != 0)
  934. flags[2] = 'e';
  935. if((td->csw & Stddberr) != 0)
  936. flags[3] = 'd';
  937. if((td->csw & Stdbabble) != 0)
  938. flags[4] = 'b';
  939. if((td->csw & Stdtrerr) != 0)
  940. flags[5] = 't';
  941. if((td->csw & Stdmmf) != 0)
  942. flags[6] = 'n';
  943. flags[7] = 0;
  944. ss = (td->csw & Stddcs) ? 'c' : 's';
  945. pg = (td->csw & Stdpg) ? '1' : '0';
  946. s = seprint(s, se, "\t%s %cs pg%c", flags, ss, pg);
  947. s = seprint(s, se, " b0 %#lux b1 %#lux off %uld\n",
  948. td->buffer[0] >> 12, td->buffer[1] >> 12, td->buffer[0] & 0xfff);
  949. s = seprint(s, se, "\ttpos %c tcnt %uld",
  950. pc[(td->buffer[0]>>3)&3], td->buffer[1] & 7);
  951. s = seprint(s, se, " ssm %#lux csm %#lux cspm %#lux",
  952. td->mfs & 0xff, (td->mfs>>8) & 0xff, (td->csw>>8) & 0xff);
  953. s = seprintlink(s, se, " link", td->link, 1);
  954. s = seprintlink(s, se, " blink", td->blink, 0);
  955. return seprint(s, se, "\n");
  956. }
  957. static long
  958. maxtdlen(Td *td)
  959. {
  960. xcacheinvse(&td->csw, sizeof td->csw); /* not really neeeded */
  961. return (td->csw >> Tdlenshift) & Tdlenmask;
  962. }
  963. static long
  964. tdlen(Td *td)
  965. {
  966. if(td->data == nil)
  967. return 0;
  968. return td->ndata - maxtdlen(td);
  969. }
  970. static char*
  971. seprinttd(char *s, char *se, Td *td, char *tag)
  972. {
  973. char flags[9];
  974. char t, ss;
  975. int i;
  976. static char *tok[4] = { "out", "in", "setup", "BUG" };
  977. if(td == nil)
  978. return seprint(s, se, "%s <nil td>\n", tag);
  979. xcacheinvse(&td->nlink, sizeof td->nlink); /* all hw state */
  980. s = seprint(s, se, "%s %#p", tag, td);
  981. s = seprintlink(s, se, " nlink", td->nlink, 0);
  982. s = seprintlink(s, se, " alink", td->alink, 0);
  983. s = seprint(s, se, " %s", tok[(td->csw & Tdtok) >> 8]);
  984. if((td->csw & Tdping) != 0)
  985. s = seprint(s, se, " png");
  986. memset(flags, '-', 8);
  987. if((td->csw & Tdactive) != 0)
  988. flags[0] = 'a';
  989. if((td->csw & Tdioc) != 0)
  990. flags[1] = 'i';
  991. if((td->csw & Tdhalt) != 0)
  992. flags[2] = 'h';
  993. if((td->csw & Tddberr) != 0)
  994. flags[3] = 'd';
  995. if((td->csw & Tdbabble) != 0)
  996. flags[4] = 'b';
  997. if((td->csw & Tdtrerr) != 0)
  998. flags[5] = 't';
  999. if((td->csw & Tdmmf) != 0)
  1000. flags[6] = 'n';
  1001. if((td->csw & (Tderr2|Tderr1)) == 0)
  1002. flags[7] = 'z';
  1003. flags[8] = 0;
  1004. t = (td->csw & Tddata1) ? '1' : '0';
  1005. ss = (td->csw & Tddcs) ? 'c' : 's';
  1006. s = seprint(s, se, "\n\td%c %s %cs", t, flags, ss);
  1007. s = seprint(s, se, " max %uld", maxtdlen(td));
  1008. s = seprint(s, se, " pg %uld off %#lux\n",
  1009. (td->csw >> Tdpgshift) & Tdpgmask, td->buffer[0] & 0xFFF);
  1010. s = seprint(s, se, "\tbuffs:");
  1011. for(i = 0; i < nelem(td->buffer); i++)
  1012. s = seprint(s, se, " %#lux", td->buffer[i]>>12);
  1013. if(td->data != nil)
  1014. s = seprintdata(s, se, td->data, td->ndata);
  1015. return seprint(s, se, "\n");
  1016. }
  1017. static void
  1018. dumptd(Td *td, char *pref)
  1019. {
  1020. char buf[256];
  1021. char *se;
  1022. int i;
  1023. i = 0;
  1024. se = buf+sizeof(buf);
  1025. for(; td != nil; td = td->next){
  1026. seprinttd(buf, se, td, pref);
  1027. print("%s", buf);
  1028. if(i++ > 20){
  1029. print("...more tds...\n");
  1030. break;
  1031. }
  1032. }
  1033. }
  1034. static void
  1035. qhdump(Qh *qh)
  1036. {
  1037. char buf[256];
  1038. char *s, *se, *tag;
  1039. Td td;
  1040. static char *speed[] = {"full", "low", "high", "BUG"};
  1041. if(qh == nil){
  1042. print("<nil qh>\n");
  1043. return;
  1044. }
  1045. xcacheinvse(&qh->link, sizeof qh->link); /* hw state but buffers */
  1046. if(qh->io == nil)
  1047. tag = "qh";
  1048. else
  1049. tag = qh->io->tag;
  1050. se = buf+sizeof(buf);
  1051. s = seprint(buf, se, "%s %#p", tag, qh);
  1052. s = seprint(s, se, " ep %uld dev %uld",
  1053. (qh->eps0>>8)&0xf, qh->eps0&0x7f);
  1054. s = seprint(s, se, " hub %uld", (qh->eps1 >> 16) & 0x7f);
  1055. s = seprint(s, se, " port %uld", (qh->eps1 >> 23) & 0x7f);
  1056. s = seprintlink(s, se, " link", qh->link, 1);
  1057. seprint(s, se, " clink %#lux", qh->clink);
  1058. print("%s\n", buf);
  1059. s = seprint(buf, se, "\tnrld %uld", (qh->eps0 >> Qhrlcshift) & Qhrlcmask);
  1060. s = seprint(s, se, " nak %uld", (qh->alink >> 1) & 0xf);
  1061. s = seprint(s, se, " max %uld ", qhmaxpkt(qh));
  1062. if((qh->eps0 & Qhnhctl) != 0)
  1063. s = seprint(s, se, "c");
  1064. if((qh->eps0 & Qhhrl) != 0)
  1065. s = seprint(s, se, "h");
  1066. if((qh->eps0 & Qhdtc) != 0)
  1067. s = seprint(s, se, "d");
  1068. if((qh->eps0 & Qhint) != 0)
  1069. s = seprint(s, se, "i");
  1070. s = seprint(s, se, " %s", speed[(qh->eps0 >> 12) & 3]);
  1071. s = seprint(s, se, " mult %uld", (qh->eps1 >> Qhmultshift) & Qhmultmask);
  1072. seprint(s, se, " scm %#lux ism %#lux\n",
  1073. (qh->eps1 >> 8 & 0xff), qh->eps1 & 0xff);
  1074. print("%s\n", buf);
  1075. memset(&td, 0, sizeof(td));
  1076. memmove(&td, &qh->nlink, 32); /* overlay area */
  1077. seprinttd(buf, se, &td, "\tovl");
  1078. print("%s", buf);
  1079. }
  1080. static void
  1081. isodump(Isoio* iso, int all)
  1082. {
  1083. Itd *td, *tdi, *tdu;
  1084. Sitd *std, *stdi, *stdu;
  1085. char buf[256];
  1086. int i;
  1087. if(iso == nil){
  1088. print("<nil iso>\n");
  1089. return;
  1090. }
  1091. print("iso %#p %s %s speed state %d nframes %d maxsz %uld",
  1092. iso, iso->tok == Tdtokin ? "in" : "out",
  1093. iso->hs ? "high" : "full",
  1094. iso->state, iso->nframes, iso->maxsize);
  1095. print(" td0 %uld tdi %#p tdu %#p data %#p\n",
  1096. iso->td0frno, iso->tdi, iso->tdu, iso->data);
  1097. if(iso->err != nil)
  1098. print("\terr %s\n", iso->err);
  1099. if(iso->err != nil)
  1100. print("\terr='%s'\n", iso->err);
  1101. if(all == 0)
  1102. if(iso->hs != 0){
  1103. tdi = iso->tdi;
  1104. seprintitd(buf, buf+sizeof(buf), tdi);
  1105. print("\ttdi %s\n", buf);
  1106. tdu = iso->tdu;
  1107. seprintitd(buf, buf+sizeof(buf), tdu);
  1108. print("\ttdu %s\n", buf);
  1109. }else{
  1110. stdi = iso->stdi;
  1111. seprintsitd(buf, buf+sizeof(buf), stdi);
  1112. print("\tstdi %s\n", buf);
  1113. stdu = iso->stdu;
  1114. seprintsitd(buf, buf+sizeof(buf), stdu);
  1115. print("\tstdu %s\n", buf);
  1116. }
  1117. else
  1118. for(i = 0; i < Nisoframes; i++)
  1119. if(iso->tdps[i] != nil)
  1120. if(iso->hs != 0){
  1121. td = iso->itdps[i];
  1122. seprintitd(buf, buf+sizeof(buf), td);
  1123. if(td == iso->tdi)
  1124. print("i->");
  1125. if(td == iso->tdu)
  1126. print("i->");
  1127. print("[%d]\t%s", i, buf);
  1128. }else{
  1129. std = iso->sitdps[i];
  1130. seprintsitd(buf, buf+sizeof(buf), std);
  1131. if(std == iso->stdi)
  1132. print("i->");
  1133. if(std == iso->stdu)
  1134. print("u->");
  1135. print("[%d]\t%s", i, buf);
  1136. }
  1137. }
  1138. static void
  1139. dump(Hci *hp)
  1140. {
  1141. int i;
  1142. char *s, *se;
  1143. char buf[128];
  1144. Ctlr *ctlr;
  1145. Eopio *opio;
  1146. Isoio *iso;
  1147. Qh *qh;
  1148. ctlr = hp->aux;
  1149. opio = ctlr->opio;
  1150. ilock(ctlr);
  1151. print("ehci port %#p frames %#p (%d fr.) nintr %d ntdintr %d",
  1152. ctlr->capio, ctlr->frames, ctlr->nframes,
  1153. ctlr->nintr, ctlr->ntdintr);
  1154. print(" nqhintr %d nisointr %d\n", ctlr->nqhintr, ctlr->nisointr);
  1155. print("\tcmd %#lux sts %#lux intr %#lux frno %uld",
  1156. opio->cmd, opio->sts, opio->intr, opio->frno);
  1157. print(" base %#lux link %#lux fr0 %#lux\n",
  1158. opio->frbase, opio->link, ctlr->frames[0]);
  1159. se = buf+sizeof(buf);
  1160. s = seprint(buf, se, "\t");
  1161. for(i = 0; i < hp->nports; i++){
  1162. s = seprint(s, se, "p%d %#lux ", i, opio->portsc[i]);
  1163. if(hp->nports > 4 && i == hp->nports/2 - 1)
  1164. s = seprint(s, se, "\n\t");
  1165. }
  1166. print("%s\n", buf);
  1167. qh = ctlr->qhs;
  1168. i = 0;
  1169. do{
  1170. qhdump(qh);
  1171. qh = qh->next;
  1172. }while(qh != ctlr->qhs && i++ < 100);
  1173. if(i > 100)
  1174. print("...too many Qhs...\n");
  1175. if(ctlr->intrqhs != nil)
  1176. print("intr qhs:\n");
  1177. for(qh = ctlr->intrqhs; qh != nil; qh = qh->inext)
  1178. qhdump(qh);
  1179. if(ctlr->iso != nil)
  1180. print("iso:\n");
  1181. for(iso = ctlr->iso; iso != nil; iso = iso->next)
  1182. isodump(ctlr->iso, 0);
  1183. print("%d eds in tree\n", ctlr->ntree);
  1184. iunlock(ctlr);
  1185. lock(&edpool);
  1186. print("%d eds allocated = %d in use + %d free\n",
  1187. edpool.nalloc, edpool.ninuse, edpool.nfree);
  1188. unlock(&edpool);
  1189. }
  1190. static char*
  1191. errmsg(int err)
  1192. {
  1193. if(err == 0)
  1194. return "ok";
  1195. if(err & Tddberr)
  1196. return "data buffer error";
  1197. if(err & Tdbabble)
  1198. return "babble detected";
  1199. if(err & Tdtrerr)
  1200. return "transaction error";
  1201. if(err & Tdmmf)
  1202. return "missed µframe";
  1203. if(err & Tdhalt)
  1204. return Estalled; /* [uo]hci report this error */
  1205. return Eio;
  1206. }
  1207. static char*
  1208. ierrmsg(int err)
  1209. {
  1210. if(err == 0)
  1211. return "ok";
  1212. if(err & Itddberr)
  1213. return "data buffer error";
  1214. if(err & Itdbabble)
  1215. return "babble detected";
  1216. if(err & Itdtrerr)
  1217. return "transaction error";
  1218. return Eio;
  1219. }
  1220. static char*
  1221. serrmsg(int err)
  1222. {
  1223. if(err & Stderr)
  1224. return "translation translator error";
  1225. /* other errors have same numbers than Td errors */
  1226. return errmsg(err);
  1227. }
  1228. static int
  1229. isocanread(void *a)
  1230. {
  1231. Isoio *iso;
  1232. iso = a;
  1233. if(iso->state == Qclose)
  1234. return 1;
  1235. if(iso->state == Qrun && iso->tok == Tdtokin){
  1236. if(iso->hs != 0 && iso->tdi != iso->tdu)
  1237. return 1;
  1238. if(iso->hs == 0 && iso->stdi != iso->stdu)
  1239. return 1;
  1240. }
  1241. return 0;
  1242. }
  1243. static int
  1244. isocanwrite(void *a)
  1245. {
  1246. Isoio *iso;
  1247. iso = a;
  1248. if(iso->state == Qclose)
  1249. return 1;
  1250. if(iso->state == Qrun && iso->tok == Tdtokout){
  1251. if(iso->hs != 0 && iso->tdu->next != iso->tdi)
  1252. return 1;
  1253. if(iso->hs == 0 && iso->stdu->next != iso->stdi)
  1254. return 1;
  1255. }
  1256. return 0;
  1257. }
  1258. static void
  1259. itdinit(Isoio *iso, Itd *td)
  1260. {
  1261. int p, t;
  1262. ulong pa, tsize, size;
  1263. /*
  1264. * BUG: This does not put an integral number of samples
  1265. * on each µframe unless samples per packet % 8 == 0
  1266. * Also, all samples are packed early on each frame.
  1267. */
  1268. p = 0;
  1269. size = td->ndata = td->mdata;
  1270. pa = PADDR(td->data);
  1271. for(t = 0; size > 0 && t < 8; t++){
  1272. tsize = size;
  1273. if(tsize > iso->maxsize)
  1274. tsize = iso->maxsize;
  1275. size -= tsize;
  1276. assert(p < nelem(td->buffer));
  1277. td->csw[t] = tsize << Itdlenshift | p << Itdpgshift |
  1278. (pa & 0xFFF) << Itdoffshift | Itdactive | Itdioc;
  1279. xcachewbse(&td->csw[t], sizeof td->csw[t]);
  1280. if(((pa+tsize) & ~0xFFF) != (pa & ~0xFFF))
  1281. p++;
  1282. pa += tsize;
  1283. }
  1284. }
  1285. static void
  1286. sitdinit(Isoio *iso, Sitd *td)
  1287. {
  1288. td->ndata = td->mdata & Stdlenmask;
  1289. td->buffer[0] = PADDR(td->data);
  1290. td->buffer[1] = (td->buffer[0] & ~0xFFF) + 0x1000;
  1291. if(iso->tok == Tdtokin || td->ndata <= 188)
  1292. td->buffer[1] |= Stdtpall;
  1293. else
  1294. td->buffer[1] |= Stdtpbegin;
  1295. if(iso->tok == Tdtokin)
  1296. td->buffer[1] |= 1;
  1297. else
  1298. td->buffer[1] |= ((td->ndata + 187) / 188) & Stdtcntmask;
  1299. xcachewbse(td->buffer, 2 * sizeof td->buffer[0]);
  1300. td->csw = td->ndata << Stdlenshift | Stdactive | Stdioc;
  1301. xcachewbse(&td->csw, sizeof td->csw);
  1302. }
  1303. static int
  1304. itdactive(Itd *td)
  1305. {
  1306. int i;
  1307. for(i = 0; i < nelem(td->csw); i++){
  1308. xcacheinvse(&td->csw[i], sizeof td->csw[i]);
  1309. if((td->csw[i] & Itdactive) != 0)
  1310. return 1;
  1311. }
  1312. return 0;
  1313. }
  1314. static int
  1315. isohsinterrupt(Ctlr *ctlr, Isoio *iso)
  1316. {
  1317. Itd *tdi;
  1318. int err, i, t, nframes;
  1319. tdi = iso->tdi;
  1320. assert(tdi != nil);
  1321. if(itdactive(tdi)) /* not all tds are done */
  1322. return 0;
  1323. ctlr->nisointr++;
  1324. ddiprint("isohsintr: iso %#p: tdi %#p tdu %#p\n", iso, tdi, iso->tdu);
  1325. if(iso->state != Qrun && iso->state != Qdone)
  1326. panic("isofsintr: iso state");
  1327. if(debug > 1 || iso->debug > 1)
  1328. isodump(iso, 0);
  1329. nframes = iso->nframes / 2; /* limit how many we look */
  1330. if(nframes > Nisoframes)
  1331. nframes = Nisoframes;
  1332. if(iso->tok == Tdtokin)
  1333. tdi->ndata = 0;
  1334. /* else, it has the number of bytes transferred */
  1335. for(i = 0; i < nframes && itdactive(tdi) == 0; i++){
  1336. xcacheinvse(&tdi->csw[i], sizeof tdi->csw[i]);
  1337. if(iso->tok == Tdtokin) {
  1338. tdi->ndata += (tdi->csw[i] >> Itdlenshift) & Itdlenmask;
  1339. }
  1340. err = 0;
  1341. for(t = 0; t < nelem(tdi->csw); t++){
  1342. tdi->csw[t] &= ~Itdioc;
  1343. xcachewbse(&tdi->csw[t], sizeof tdi->csw[t]);
  1344. err |= tdi->csw[t] & Itderrors;
  1345. }
  1346. if(err == 0)
  1347. iso->nerrs = 0;
  1348. else if(iso->nerrs++ > iso->nframes/2){
  1349. if(iso->err == nil){
  1350. iso->err = ierrmsg(err);
  1351. diprint("isohsintr: tdi %#p error %#ux %s\n",
  1352. tdi, err, iso->err);
  1353. diprint("ctlr load %uld\n", ctlr->load);
  1354. }
  1355. tdi->ndata = 0;
  1356. }else
  1357. tdi->ndata = 0;
  1358. if(tdi->next == iso->tdu || tdi->next->next == iso->tdu){
  1359. memset(iso->tdu->data, 0, iso->tdu->mdata);
  1360. itdinit(iso, iso->tdu);
  1361. iso->tdu = iso->tdu->next;
  1362. iso->nleft = 0;
  1363. }
  1364. tdi = tdi->next;
  1365. }
  1366. ddiprint("isohsintr: %d frames processed\n", nframes);
  1367. if(i == nframes){
  1368. tdi->csw[0] |= Itdioc;
  1369. xcachewbse(&tdi->csw[0], sizeof tdi->csw[0]);
  1370. }
  1371. iso->tdi = tdi;
  1372. if(isocanwrite(iso) || isocanread(iso)){
  1373. diprint("wakeup iso %#p tdi %#p tdu %#p\n", iso,
  1374. iso->tdi, iso->tdu);
  1375. wakeup(iso);
  1376. }
  1377. return 1;
  1378. }
  1379. static int
  1380. isofsinterrupt(Ctlr *ctlr, Isoio *iso)
  1381. {
  1382. Sitd *stdi;
  1383. int err, i, nframes;
  1384. stdi = iso->stdi;
  1385. assert(stdi != nil);
  1386. xcacheinvse(&stdi->csw, sizeof stdi->csw);
  1387. if((stdi->csw & Stdactive) != 0) /* nothing new done */
  1388. return 0;
  1389. ctlr->nisointr++;
  1390. ddiprint("isofsintr: iso %#p: tdi %#p tdu %#p\n", iso, stdi, iso->stdu);
  1391. if(iso->state != Qrun && iso->state != Qdone)
  1392. panic("isofsintr: iso state");
  1393. if(debug > 1 || iso->debug > 1)
  1394. isodump(iso, 0);
  1395. nframes = iso->nframes / 2; /* limit how many we look */
  1396. if(nframes > Nisoframes)
  1397. nframes = Nisoframes;
  1398. xcacheinvse(&stdi->csw, sizeof stdi->csw);
  1399. for(i = 0; i < nframes && (stdi->csw & Stdactive) == 0; i++){
  1400. stdi->csw &= ~Stdioc;
  1401. /* write back csw and see if it produces errors */
  1402. xcachewbinvse(&stdi->csw, sizeof stdi->csw);
  1403. err = stdi->csw & Stderrors;
  1404. if(err == 0){
  1405. iso->nerrs = 0;
  1406. if(iso->tok == Tdtokin)
  1407. stdi->ndata = (stdi->csw>>Stdlenshift)&Stdlenmask;
  1408. /* else len is assumed correct */
  1409. }else if(iso->nerrs++ > iso->nframes/2){
  1410. if(iso->err == nil){
  1411. iso->err = serrmsg(err);
  1412. diprint("isofsintr: tdi %#p error %#ux %s\n",
  1413. stdi, err, iso->err);
  1414. diprint("ctlr load %uld\n", ctlr->load);
  1415. }
  1416. stdi->ndata = 0;
  1417. }else
  1418. stdi->ndata = 0;
  1419. if(stdi->next == iso->stdu || stdi->next->next == iso->stdu){
  1420. memset(iso->stdu->data, 0, iso->stdu->mdata);
  1421. xcachewbse(iso->stdu->data, iso->stdu->mdata);
  1422. sitdinit(iso, iso->stdu);
  1423. iso->stdu = iso->stdu->next;
  1424. iso->nleft = 0;
  1425. }
  1426. stdi = stdi->next;
  1427. if(stdi != nil)
  1428. xcacheinvse(&stdi->csw, sizeof stdi->csw);
  1429. }
  1430. ddiprint("isofsintr: %d frames processed\n", nframes);
  1431. if(i == nframes){
  1432. stdi->csw |= Stdioc;
  1433. xcachewbse(&stdi->csw, sizeof stdi->csw);
  1434. }
  1435. iso->stdi = stdi;
  1436. if(isocanwrite(iso) || isocanread(iso)){
  1437. diprint("wakeup iso %#p tdi %#p tdu %#p\n", iso,
  1438. iso->stdi, iso->stdu);
  1439. wakeup(iso);
  1440. }
  1441. return 1;
  1442. }
  1443. static int
  1444. qhinterrupt(Ctlr *ctlr, Qh *qh)
  1445. {
  1446. Td *td;
  1447. int err;
  1448. ulong csw;
  1449. if(qh->state != Qrun)
  1450. panic("qhinterrupt: qh state");
  1451. td = qh->tds;
  1452. if(td == nil)
  1453. panic("qhinterrupt: no tds");
  1454. xcacheinvse(&td->csw, sizeof td->csw);
  1455. if((td->csw & Tdactive) == 0)
  1456. ddqprint("qhinterrupt port %#p qh %#p\n", ctlr->capio, qh);
  1457. for(; td != nil; td = td->next){
  1458. xcacheinvse(&td->csw, sizeof td->csw);
  1459. retry:
  1460. if(td->csw & Tdactive)
  1461. return 0;
  1462. err = td->csw & Tderrors;
  1463. if(err != 0){
  1464. if(qh->io->err == nil){
  1465. qh->io->err = errmsg(err);
  1466. dqprint("qhintr: td %#p csw %#lux error %#ux %s\n",
  1467. td, td->csw, err, qh->io->err);
  1468. }
  1469. break;
  1470. }
  1471. td->ndata = tdlen(td);
  1472. if(td->ndata < maxtdlen(td)){ /* EOT */
  1473. td = td->next;
  1474. break;
  1475. }
  1476. }
  1477. /*
  1478. * Done. Make void the Tds not used (errors or EOT) and wakeup epio.
  1479. */
  1480. for(; td != nil; td = td->next)
  1481. td->ndata = 0;
  1482. qh->state = Qdone;
  1483. wakeup(qh->io);
  1484. return 1;
  1485. }
  1486. static int
  1487. ehciintr(Hci *hp)
  1488. {
  1489. Ctlr *ctlr;
  1490. Eopio *opio;
  1491. Isoio *iso;
  1492. ulong sts;
  1493. Qh *qh;
  1494. int i, some;
  1495. ctlr = hp->aux;
  1496. opio = ctlr->opio;
  1497. /*
  1498. * Will we know in USB 3.0 who the interrupt was for?.
  1499. * Do they still teach indexing in CS?
  1500. * This is Intel's doing.
  1501. */
  1502. ilock(ctlr);
  1503. ctlr->nintr++;
  1504. sts = opio->sts & Sintrs;
  1505. if(sts == 0){ /* not ours; shared intr. */
  1506. iunlock(ctlr);
  1507. return 0;
  1508. }
  1509. opio->sts = sts;
  1510. coherence();
  1511. if((sts & Sherr) != 0)
  1512. print("ehci: port %#p fatal host system error\n", ctlr->capio);
  1513. if((sts & Shalted) != 0)
  1514. print("ehci: port %#p: halted\n", ctlr->capio);
  1515. if((sts & Sasync) != 0){
  1516. dprint("ehci: doorbell\n");
  1517. wakeup(ctlr);
  1518. }
  1519. /*
  1520. * We enter always this if, even if it seems the
  1521. * interrupt does not report anything done/failed.
  1522. * Some controllers don't post interrupts right.
  1523. */
  1524. some = 0;
  1525. if((sts & (Serrintr|Sintr)) != 0){
  1526. ctlr->ntdintr++;
  1527. if(debug > 1){
  1528. print("ehci port %#p frames %#p nintr %d ntdintr %d",
  1529. ctlr->capio, ctlr->frames,
  1530. ctlr->nintr, ctlr->ntdintr);
  1531. print(" nqhintr %d nisointr %d\n",
  1532. ctlr->nqhintr, ctlr->nisointr);
  1533. print("\tcmd %#lux sts %#lux intr %#lux frno %uld",
  1534. opio->cmd, opio->sts, opio->intr, opio->frno);
  1535. }
  1536. /* process the Iso transfers */
  1537. for(iso = ctlr->iso; iso != nil; iso = iso->next)
  1538. if(iso->state == Qrun || iso->state == Qdone)
  1539. if(iso->hs != 0)
  1540. some += isohsinterrupt(ctlr, iso);
  1541. else
  1542. some += isofsinterrupt(ctlr, iso);
  1543. /* process the qhs in the periodic tree */
  1544. for(qh = ctlr->intrqhs; qh != nil; qh = qh->inext)
  1545. if(qh->state == Qrun)
  1546. some += qhinterrupt(ctlr, qh);
  1547. /* process the async Qh circular list */
  1548. qh = ctlr->qhs;
  1549. i = 0;
  1550. do{
  1551. if (qh == nil)
  1552. panic("ehciintr: nil qh");
  1553. if(qh->state == Qrun)
  1554. some += qhinterrupt(ctlr, qh);
  1555. qh = qh->next;
  1556. }while(qh != ctlr->qhs && i++ < 100);
  1557. if(i > 100)
  1558. print("echi: interrupt: qh loop?\n");
  1559. }
  1560. iunlock(ctlr);
  1561. return some;
  1562. }
  1563. static void
  1564. interrupt(Ureg*, void* a)
  1565. {
  1566. ehciintr(a);
  1567. }
  1568. static int
  1569. portenable(Hci *hp, int port, int on)
  1570. {
  1571. Ctlr *ctlr;
  1572. Eopio *opio;
  1573. int s;
  1574. ctlr = hp->aux;
  1575. opio = ctlr->opio;
  1576. s = opio->portsc[port-1];
  1577. qlock(&ctlr->portlck);
  1578. if(waserror()){
  1579. qunlock(&ctlr->portlck);
  1580. nexterror();
  1581. }
  1582. dprint("ehci %#p port %d enable=%d; sts %#x\n",
  1583. ctlr->capio, port, on, s);
  1584. ilock(ctlr);
  1585. if(s & (Psstatuschg | Pschange))
  1586. opio->portsc[port-1] = s;
  1587. if(on)
  1588. opio->portsc[port-1] |= Psenable;
  1589. else
  1590. opio->portsc[port-1] &= ~Psenable;
  1591. coherence();
  1592. microdelay(64);
  1593. iunlock(ctlr);
  1594. tsleep(&up->sleep, return0, 0, Enabledelay);
  1595. dprint("ehci %#p port %d enable=%d: sts %#lux\n",
  1596. ctlr->capio, port, on, opio->portsc[port-1]);
  1597. qunlock(&ctlr->portlck);
  1598. poperror();
  1599. return 0;
  1600. }
  1601. /*
  1602. * If we detect during status that the port is low-speed or
  1603. * during reset that it's full-speed, the device is not for
  1604. * ourselves. The companion controller will take care.
  1605. * Low-speed devices will not be seen by usbd. Full-speed
  1606. * ones are seen because it's only after reset that we know what
  1607. * they are (usbd may notice a device not enabled in this case).
  1608. */
  1609. static void
  1610. portlend(Ctlr *ctlr, int port, char *ss)
  1611. {
  1612. Eopio *opio;
  1613. ulong s;
  1614. opio = ctlr->opio;
  1615. dprint("ehci %#p port %d: %s speed device: no longer owned\n",
  1616. ctlr->capio, port, ss);
  1617. s = opio->portsc[port-1] & ~(Pschange|Psstatuschg);
  1618. opio->portsc[port-1] = s | Psowner;
  1619. coherence();
  1620. }
  1621. static int
  1622. portreset(Hci *hp, int port, int on)
  1623. {
  1624. ulong s;
  1625. Eopio *opio;
  1626. Ctlr *ctlr;
  1627. int i;
  1628. if(on == 0)
  1629. return 0;
  1630. ctlr = hp->aux;
  1631. opio = ctlr->opio;
  1632. qlock(&ctlr->portlck);
  1633. if(waserror()){
  1634. iunlock(ctlr);
  1635. qunlock(&ctlr->portlck);
  1636. nexterror();
  1637. }
  1638. s = opio->portsc[port-1];
  1639. dprint("ehci %#p port %d reset; sts %#lux\n", ctlr->capio, port, s);
  1640. ilock(ctlr);
  1641. s &= ~(Psenable|Psreset);
  1642. opio->portsc[port-1] = s | Psreset; /* initiate reset */
  1643. coherence();
  1644. for(i = 0; i < 50; i++){ /* was 10 */
  1645. delay(10);
  1646. if((opio->portsc[port-1] & Psreset) == 0)
  1647. break;
  1648. }
  1649. if (opio->portsc[port-1] & Psreset)
  1650. iprint("ehci %#p: port %d didn't reset after %d ms; sts %#lux\n",
  1651. ctlr->capio, port, i * 10, opio->portsc[port-1]);
  1652. opio->portsc[port-1] &= ~Psreset; /* force appearance of reset done */
  1653. coherence();
  1654. delay(10);
  1655. if((opio->portsc[port-1] & Psenable) == 0)
  1656. portlend(ctlr, port, "full");
  1657. iunlock(ctlr);
  1658. dprint("ehci %#p after port %d reset; sts %#lux\n",
  1659. ctlr->capio, port, opio->portsc[port-1]);
  1660. qunlock(&ctlr->portlck);
  1661. poperror();
  1662. return 0;
  1663. }
  1664. static int
  1665. portstatus(Hci *hp, int port)
  1666. {
  1667. int s, r;
  1668. Eopio *opio;
  1669. Ctlr *ctlr;
  1670. ctlr = hp->aux;
  1671. opio = ctlr->opio;
  1672. qlock(&ctlr->portlck);
  1673. if(waserror()){
  1674. iunlock(ctlr);
  1675. qunlock(&ctlr->portlck);
  1676. nexterror();
  1677. }
  1678. ilock(ctlr);
  1679. s = opio->portsc[port-1];
  1680. if(s & (Psstatuschg | Pschange)){
  1681. opio->portsc[port-1] = s;
  1682. coherence();
  1683. ddprint("ehci %#p port %d status %#x\n", ctlr->capio, port, s);
  1684. }
  1685. /*
  1686. * If the port is a low speed port we yield ownership now
  1687. * to the [uo]hci companion controller and pretend it's not here.
  1688. */
  1689. if((s & Pspresent) != 0 && (s & Pslinemask) == Pslow){
  1690. portlend(ctlr, port, "low");
  1691. s &= ~Pspresent; /* not for us this time */
  1692. }
  1693. iunlock(ctlr);
  1694. qunlock(&ctlr->portlck);
  1695. poperror();
  1696. /*
  1697. * We must return status bits as a
  1698. * get port status hub request would do.
  1699. */
  1700. r = 0;
  1701. if(s & Pspresent)
  1702. r |= HPpresent|HPhigh;
  1703. if(s & Psenable)
  1704. r |= HPenable;
  1705. if(s & Pssuspend)
  1706. r |= HPsuspend;
  1707. if(s & Psreset)
  1708. r |= HPreset;
  1709. if(s & Psstatuschg)
  1710. r |= HPstatuschg;
  1711. if(s & Pschange)
  1712. r |= HPchange;
  1713. return r;
  1714. }
  1715. static char*
  1716. seprintio(char *s, char *e, Qio *io, char *pref)
  1717. {
  1718. s = seprint(s,e,"%s io %#p qh %#p id %#x", pref, io, io->qh, io->usbid);
  1719. s = seprint(s,e," iot %ld", io->iotime);
  1720. s = seprint(s,e," tog %#x tok %#x err %s", io->toggle, io->tok, io->err);
  1721. return s;
  1722. }
  1723. static char*
  1724. seprintep(char *s, char *e, Ep *ep)
  1725. {
  1726. Qio *io;
  1727. Ctlio *cio;
  1728. Ctlr *ctlr;
  1729. ctlr = ep->hp->aux;
  1730. ilock(ctlr);
  1731. if(ep->aux == nil){
  1732. *s = 0;
  1733. iunlock(ctlr);
  1734. return s;
  1735. }
  1736. switch(ep->ttype){
  1737. case Tctl:
  1738. cio = ep->aux;
  1739. s = seprintio(s, e, cio, "c");
  1740. s = seprint(s, e, "\trepl %d ndata %d\n", ep->rhrepl, cio->ndata);
  1741. break;
  1742. case Tbulk:
  1743. case Tintr:
  1744. io = ep->aux;
  1745. if(ep->mode != OWRITE)
  1746. s = seprintio(s, e, &io[OREAD], "r");
  1747. if(ep->mode != OREAD)
  1748. s = seprintio(s, e, &io[OWRITE], "w");
  1749. break;
  1750. case Tiso:
  1751. *s = 0;
  1752. break;
  1753. }
  1754. iunlock(ctlr);
  1755. return s;
  1756. }
  1757. /*
  1758. * halt condition was cleared on the endpoint. update our toggles.
  1759. */
  1760. static void
  1761. clrhalt(Ep *ep)
  1762. {
  1763. Qio *io;
  1764. ep->clrhalt = 0;
  1765. switch(ep->ttype){
  1766. case Tintr:
  1767. case Tbulk:
  1768. io = ep->aux;
  1769. if(ep->mode != OREAD){
  1770. qlock(&io[OWRITE]);
  1771. io[OWRITE].toggle = Tddata0;
  1772. deprint("ep clrhalt for io %#p\n", io+OWRITE);
  1773. qunlock(&io[OWRITE]);
  1774. }
  1775. if(ep->mode != OWRITE){
  1776. qlock(&io[OREAD]);
  1777. io[OREAD].toggle = Tddata0;
  1778. deprint("ep clrhalt for io %#p\n", io+OREAD);
  1779. qunlock(&io[OREAD]);
  1780. }
  1781. break;
  1782. }
  1783. }
  1784. static void
  1785. xdump(char* pref, void *qh)
  1786. {
  1787. int i;
  1788. ulong *u;
  1789. u = qh;
  1790. print("%s %#p:", pref, u);
  1791. for(i = 0; i < 16; i++)
  1792. if((i%4) == 0)
  1793. print("\n %#8.8ulx", u[i]);
  1794. else
  1795. print(" %#8.8ulx", u[i]);
  1796. print("\n");
  1797. }
  1798. static long
  1799. episohscpy(Ctlr *ctlr, Ep *ep, Isoio* iso, uchar *b, long count)
  1800. {
  1801. int nr;
  1802. long tot;
  1803. Itd *tdu;
  1804. for(tot = 0; iso->tdi != iso->tdu && tot < count; tot += nr){
  1805. tdu = iso->tdu;
  1806. if(itdactive(tdu))
  1807. break;
  1808. nr = tdu->ndata;
  1809. if(tot + nr > count)
  1810. nr = count - tot;
  1811. if(nr == 0)
  1812. print("ehci: ep%d.%d: too many polls\n",
  1813. ep->dev->nb, ep->nb);
  1814. else{
  1815. iunlock(ctlr); /* We could page fault here */
  1816. xcacheinvse(tdu->data, nr); /* filled by dma */
  1817. memmove(b+tot, tdu->data, nr);
  1818. ilock(ctlr);
  1819. if(nr < tdu->ndata)
  1820. memmove(tdu->data, tdu->data+nr, tdu->ndata - nr);
  1821. tdu->ndata -= nr;
  1822. xcachewbse(tdu->data, tdu->ndata);
  1823. }
  1824. if(tdu->ndata == 0){
  1825. itdinit(iso, tdu);
  1826. iso->tdu = tdu->next;
  1827. }
  1828. }
  1829. return tot;
  1830. }
  1831. static long
  1832. episofscpy(Ctlr *ctlr, Ep *ep, Isoio* iso, uchar *b, long count)
  1833. {
  1834. int nr;
  1835. long tot;
  1836. Sitd *stdu;
  1837. for(tot = 0; iso->stdi != iso->stdu && tot < count; tot += nr){
  1838. stdu = iso->stdu;
  1839. xcacheinvse(&stdu->csw, sizeof stdu->csw);
  1840. if(stdu->csw & Stdactive){
  1841. diprint("ehci: episoread: %#p tdu active\n", iso);
  1842. break;
  1843. }
  1844. nr = stdu->ndata;
  1845. if(tot + nr > count)
  1846. nr = count - tot;
  1847. if(nr == 0)
  1848. print("ehci: ep%d.%d: too many polls\n",
  1849. ep->dev->nb, ep->nb);
  1850. else{
  1851. iunlock(ctlr); /* We could page fault here */
  1852. xcacheinvse(stdu->data, nr); /* filled by dma */
  1853. memmove(b+tot, stdu->data, nr);
  1854. ilock(ctlr);
  1855. if(nr < stdu->ndata)
  1856. memmove(stdu->data, stdu->data+nr,
  1857. stdu->ndata - nr);
  1858. stdu->ndata -= nr;
  1859. xcachewbse(stdu->data, stdu->ndata);
  1860. }
  1861. if(stdu->ndata == 0){
  1862. sitdinit(iso, stdu);
  1863. iso->stdu = stdu->next;
  1864. }
  1865. }
  1866. return tot;
  1867. }
  1868. static long
  1869. episoread(Ep *ep, Isoio *iso, void *a, long count)
  1870. {
  1871. Ctlr *ctlr;
  1872. uchar *b;
  1873. long tot;
  1874. iso->debug = ep->debug;
  1875. diprint("ehci: episoread: %#p ep%d.%d\n", iso, ep->dev->nb, ep->nb);
  1876. b = a;
  1877. ctlr = ep->hp->aux;
  1878. qlock(iso);
  1879. if(waserror()){
  1880. qunlock(iso);
  1881. nexterror();
  1882. }
  1883. iso->err = nil;
  1884. iso->nerrs = 0;
  1885. ilock(ctlr);
  1886. if(iso->state == Qclose){
  1887. iunlock(ctlr);
  1888. error(iso->err ? iso->err : Eio);
  1889. }
  1890. iso->state = Qrun;
  1891. while(isocanread(iso) == 0){
  1892. iunlock(ctlr);
  1893. diprint("ehci: episoread: %#p sleep\n", iso);
  1894. if(waserror()){
  1895. if(iso->err == nil)
  1896. iso->err = "I/O timed out";
  1897. ilock(ctlr);
  1898. break;
  1899. }
  1900. tsleep(iso, isocanread, iso, ep->tmout);
  1901. poperror();
  1902. ilock(ctlr);
  1903. }
  1904. if(iso->state == Qclose){
  1905. iunlock(ctlr);
  1906. error(iso->err ? iso->err : Eio);
  1907. }
  1908. iso->state = Qdone;
  1909. assert(iso->tdu != iso->tdi);
  1910. if(iso->hs != 0)
  1911. tot = episohscpy(ctlr, ep, iso, b, count);
  1912. else
  1913. tot = episofscpy(ctlr, ep, iso, b, count);
  1914. iunlock(ctlr);
  1915. qunlock(iso);
  1916. poperror();
  1917. diprint("uhci: episoread: %#p %uld bytes err '%s'\n", iso, tot, iso->err);
  1918. if(iso->err != nil)
  1919. error(iso->err);
  1920. return tot;
  1921. }
  1922. /*
  1923. * iso->tdu is the next place to put data. When it gets full
  1924. * it is activated and tdu advanced.
  1925. */
  1926. static long
  1927. putsamples(Isoio *iso, uchar *b, long count)
  1928. {
  1929. long tot, n;
  1930. for(tot = 0; isocanwrite(iso) && tot < count; tot += n){
  1931. n = count-tot;
  1932. if(iso->hs != 0){
  1933. if(n > iso->tdu->mdata - iso->nleft)
  1934. n = iso->tdu->mdata - iso->nleft;
  1935. memmove(iso->tdu->data + iso->nleft, b + tot, n);
  1936. xcachewbse(iso->tdu->data + iso->nleft, n);
  1937. iso->nleft += n;
  1938. if(iso->nleft == iso->tdu->mdata){
  1939. itdinit(iso, iso->tdu);
  1940. iso->nleft = 0;
  1941. iso->tdu = iso->tdu->next;
  1942. }
  1943. }else{
  1944. if(n > iso->stdu->mdata - iso->nleft)
  1945. n = iso->stdu->mdata - iso->nleft;
  1946. memmove(iso->stdu->data + iso->nleft, b + tot, n);
  1947. xcachewbse(iso->tdu->data + iso->nleft, n);
  1948. iso->nleft += n;
  1949. if(iso->nleft == iso->stdu->mdata){
  1950. sitdinit(iso, iso->stdu);
  1951. iso->nleft = 0;
  1952. iso->stdu = iso->stdu->next;
  1953. }
  1954. }
  1955. }
  1956. return tot;
  1957. }
  1958. /*
  1959. * Queue data for writing and return error status from
  1960. * last writes done, to maintain buffered data.
  1961. */
  1962. static long
  1963. episowrite(Ep *ep, Isoio *iso, void *a, long count)
  1964. {
  1965. Ctlr *ctlr;
  1966. uchar *b;
  1967. int tot, nw;
  1968. char *err;
  1969. iso->debug = ep->debug;
  1970. diprint("ehci: episowrite: %#p ep%d.%d\n", iso, ep->dev->nb, ep->nb);
  1971. ctlr = ep->hp->aux;
  1972. qlock(iso);
  1973. if(waserror()){
  1974. qunlock(iso);
  1975. nexterror();
  1976. }
  1977. ilock(ctlr);
  1978. if(iso->state == Qclose){
  1979. iunlock(ctlr);
  1980. error(iso->err ? iso->err : Eio);
  1981. }
  1982. iso->state = Qrun;
  1983. b = a;
  1984. for(tot = 0; tot < count; tot += nw){
  1985. while(isocanwrite(iso) == 0){
  1986. iunlock(ctlr);
  1987. diprint("ehci: episowrite: %#p sleep\n", iso);
  1988. if(waserror()){
  1989. if(iso->err == nil)
  1990. iso->err = "I/O timed out";
  1991. ilock(ctlr);
  1992. break;
  1993. }
  1994. tsleep(iso, isocanwrite, iso, ep->tmout);
  1995. poperror();
  1996. ilock(ctlr);
  1997. }
  1998. err = iso->err;
  1999. iso->err = nil;
  2000. if(iso->state == Qclose || err != nil){
  2001. iunlock(ctlr);
  2002. error(err ? err : Eio);
  2003. }
  2004. if(iso->state != Qrun)
  2005. panic("episowrite: iso not running");
  2006. iunlock(ctlr); /* We could page fault here */
  2007. nw = putsamples(iso, b+tot, count-tot);
  2008. ilock(ctlr);
  2009. }
  2010. if(iso->state != Qclose)
  2011. iso->state = Qdone;
  2012. iunlock(ctlr);
  2013. err = iso->err; /* in case it failed early */
  2014. iso->err = nil;
  2015. qunlock(iso);
  2016. poperror();
  2017. if(err != nil)
  2018. error(err);
  2019. diprint("ehci: episowrite: %#p %d bytes\n", iso, tot);
  2020. return tot;
  2021. }
  2022. static int
  2023. nexttoggle(int toggle, int count, int maxpkt)
  2024. {
  2025. int np;
  2026. np = count / maxpkt;
  2027. if(np == 0)
  2028. np = 1;
  2029. if((np % 2) == 0)
  2030. return toggle;
  2031. if(toggle == Tddata1)
  2032. return Tddata0;
  2033. else
  2034. return Tddata1;
  2035. }
  2036. static Td*
  2037. epgettd(Qio *io, int flags, void *a, int count, int maxpkt)
  2038. {
  2039. Td *td;
  2040. ulong pa;
  2041. int i;
  2042. if(count > Tdmaxpkt)
  2043. panic("ehci: epgettd: too many bytes");
  2044. td = tdalloc();
  2045. td->csw = flags | io->toggle | io->tok | count << Tdlenshift |
  2046. Tderr2 | Tderr1;
  2047. /*
  2048. * use the space wasted by alignment as an
  2049. * embedded buffer if count bytes fit in there.
  2050. */
  2051. assert(Align > sizeof(Td));
  2052. if(count <= Align - sizeof(Td)){
  2053. td->data = td->sbuff;
  2054. td->buff = nil;
  2055. }else
  2056. td->data = td->buff = smalloc(Tdmaxpkt);
  2057. pa = PADDR(td->data);
  2058. for(i = 0; i < nelem(td->buffer); i++){
  2059. td->buffer[i] = pa;
  2060. if(i > 0)
  2061. td->buffer[i] &= ~0xFFF;
  2062. pa += 0x1000;
  2063. }
  2064. td->ndata = count;
  2065. if(a != nil && count > 0){
  2066. memmove(td->data, a, count);
  2067. xcachewbse(td->data, count);
  2068. }
  2069. xcachewbse(&td->nlink, sizeof td->nlink); /* all hw state */
  2070. io->toggle = nexttoggle(io->toggle, count, maxpkt);
  2071. return td;
  2072. }
  2073. /*
  2074. * Try to get them idle
  2075. */
  2076. static void
  2077. aborttds(Qh *qh)
  2078. {
  2079. Td *td;
  2080. qh->state = Qdone;
  2081. xcacheinvse(&qh->eps0, sizeof qh->eps0);
  2082. if(qh->sched >= 0 && (qh->eps0 & Qhspeedmask) != Qhhigh){
  2083. qh->eps0 |= Qhint; /* inactivate on next pass */
  2084. xcachewbse(&qh->eps0, sizeof qh->eps0);
  2085. }
  2086. for(td = qh->tds; td != nil; td = td->next){
  2087. xcacheinvse(&td->csw, sizeof td->csw);
  2088. if(td->csw & Tdactive)
  2089. td->ndata = 0;
  2090. td->csw |= Tdhalt;
  2091. xcachewbse(&td->csw, sizeof td->csw);
  2092. }
  2093. }
  2094. /*
  2095. * Some controllers do not post the usb/error interrupt after
  2096. * the work has been done. It seems that we must poll for them.
  2097. */
  2098. static int
  2099. workpending(void *a)
  2100. {
  2101. Ctlr *ctlr;
  2102. ctlr = a;
  2103. return ctlr->nreqs > 0;
  2104. }
  2105. static void
  2106. ehcipoll(void* a)
  2107. {
  2108. Hci *hp;
  2109. Ctlr *ctlr;
  2110. Poll *poll;
  2111. int i;
  2112. hp = a;
  2113. ctlr = hp->aux;
  2114. poll = &ctlr->poll;
  2115. for(;;){
  2116. if(ctlr->nreqs == 0){
  2117. if(0)ddprint("ehcipoll %#p sleep\n", ctlr->capio);
  2118. sleep(poll, workpending, ctlr);
  2119. if(0)ddprint("ehcipoll %#p awaken\n", ctlr->capio);
  2120. }
  2121. for(i = 0; i < 16 && ctlr->nreqs > 0; i++)
  2122. if(ehciintr(hp) == 0)
  2123. break;
  2124. do{
  2125. tsleep(&up->sleep, return0, 0, 1);
  2126. ehciintr(hp);
  2127. }while(ctlr->nreqs > 0);
  2128. }
  2129. }
  2130. static void
  2131. pollcheck(Hci *hp)
  2132. {
  2133. Ctlr *ctlr;
  2134. Poll *poll;
  2135. ctlr = hp->aux;
  2136. poll = &ctlr->poll;
  2137. if(poll->must != 0 && poll->does == 0){
  2138. lock(poll);
  2139. if(poll->must != 0 && poll->does == 0){
  2140. poll->does++;
  2141. print("ehci %#p: polling\n", ctlr->capio);
  2142. kproc("ehcipoll", ehcipoll, hp);
  2143. }
  2144. unlock(poll);
  2145. }
  2146. }
  2147. static int
  2148. epiodone(void *a)
  2149. {
  2150. Qh *qh;
  2151. qh = a;
  2152. return qh->state != Qrun;
  2153. }
  2154. static void
  2155. epiowait(Hci *hp, Qio *io, int tmout, ulong load)
  2156. {
  2157. Qh *qh;
  2158. int timedout;
  2159. Ctlr *ctlr;
  2160. ctlr = hp->aux;
  2161. qh = io->qh;
  2162. ddqprint("ehci io %#p sleep on qh %#p state %s\n",
  2163. io, qh, qhsname[qh->state]);
  2164. timedout = 0;
  2165. if(waserror()){
  2166. dqprint("ehci io %#p qh %#p timed out\n", io, qh);
  2167. timedout++;
  2168. }else{
  2169. if(tmout == 0)
  2170. sleep(io, epiodone, qh);
  2171. else
  2172. tsleep(io, epiodone, qh, tmout);
  2173. poperror();
  2174. }
  2175. ilock(ctlr);
  2176. /* Are we missing interrupts? */
  2177. if(qh->state == Qrun){
  2178. iunlock(ctlr);
  2179. ehciintr(hp);
  2180. ilock(ctlr);
  2181. if(qh->state == Qdone){
  2182. dqprint("ehci %#p: polling required\n", ctlr->capio);
  2183. ctlr->poll.must = 1;
  2184. pollcheck(hp);
  2185. }
  2186. }
  2187. if(qh->state == Qrun){
  2188. dqprint("ehci io %#p qh %#p timed out (no intr?)\n", io, qh);
  2189. timedout = 1;
  2190. }else if(qh->state != Qdone && qh->state != Qclose)
  2191. panic("ehci: epio: queue state %d", qh->state);
  2192. if(timedout){
  2193. aborttds(io->qh);
  2194. io->err = "request timed out";
  2195. iunlock(ctlr);
  2196. if(!waserror()){
  2197. tsleep(&up->sleep, return0, 0, Abortdelay);
  2198. poperror();
  2199. }
  2200. ilock(ctlr);
  2201. }
  2202. if(qh->state != Qclose)
  2203. qh->state = Qidle;
  2204. qhlinktd(qh, nil);
  2205. ctlr->load -= load;
  2206. ctlr->nreqs--;
  2207. iunlock(ctlr);
  2208. }
  2209. /*
  2210. * Non iso I/O.
  2211. * To make it work for control transfers, the caller may
  2212. * lock the Qio for the entire control transfer.
  2213. */
  2214. static long
  2215. epio(Ep *ep, Qio *io, void *a, long count, int mustlock)
  2216. {
  2217. int saved, ntds, tmout;
  2218. long n, tot;
  2219. ulong load;
  2220. char *err;
  2221. char buf[128];
  2222. uchar *c;
  2223. Ctlr *ctlr;
  2224. Qh* qh;
  2225. Td *td, *ltd, *td0, *ntd;
  2226. qh = io->qh;
  2227. ctlr = ep->hp->aux;
  2228. io->debug = ep->debug;
  2229. tmout = ep->tmout;
  2230. ddeprint("epio: %s ep%d.%d io %#p count %ld load %uld\n",
  2231. io->tok == Tdtokin ? "in" : "out",
  2232. ep->dev->nb, ep->nb, io, count, ctlr->load);
  2233. if((debug > 1 || ep->debug > 1) && io->tok != Tdtokin){
  2234. seprintdata(buf, buf+sizeof(buf), a, count);
  2235. print("echi epio: user data: %s\n", buf);
  2236. }
  2237. if(mustlock){
  2238. qlock(io);
  2239. if(waserror()){
  2240. qunlock(io);
  2241. nexterror();
  2242. }
  2243. }
  2244. io->err = nil;
  2245. ilock(ctlr);
  2246. if(qh->state == Qclose){ /* Tds released by cancelio */
  2247. iunlock(ctlr);
  2248. error(io->err ? io->err : Eio);
  2249. }
  2250. if(qh->state != Qidle)
  2251. panic("epio: qh not idle");
  2252. qh->state = Qinstall;
  2253. iunlock(ctlr);
  2254. c = a;
  2255. td0 = ltd = nil;
  2256. load = tot = 0;
  2257. do{
  2258. n = (Tdmaxpkt / ep->maxpkt) * ep->maxpkt;
  2259. if(count-tot < n)
  2260. n = count-tot;
  2261. if(io->tok != Tdtokin)
  2262. td = epgettd(io, Tdactive, c+tot, n, ep->maxpkt);
  2263. else
  2264. td = epgettd(io, Tdactive, nil, n, ep->maxpkt);
  2265. if(td0 == nil)
  2266. td0 = td;
  2267. else
  2268. tdlinktd(ltd, td);
  2269. ltd = td;
  2270. tot += n;
  2271. load += ep->load;
  2272. }while(tot < count);
  2273. if(td0 == nil || ltd == nil)
  2274. panic("epio: no td");
  2275. xcacheinvse(&ltd->csw, sizeof ltd->csw);
  2276. ltd->csw |= Tdioc; /* the last one interrupts */
  2277. xcachewbse(&ltd->csw, sizeof ltd->csw);
  2278. ddeprint("ehci: load %uld ctlr load %uld\n", load, ctlr->load);
  2279. if(debug > 1 || ep->debug > 1)
  2280. dumptd(td0, "epio: put: ");
  2281. ilock(ctlr);
  2282. if(qh->state != Qclose){
  2283. io->iotime = TK2MS(MACHP(0)->ticks);
  2284. qh->state = Qrun;
  2285. qhlinktd(qh, td0);
  2286. ctlr->nreqs++;
  2287. ctlr->load += load;
  2288. }
  2289. iunlock(ctlr);
  2290. if(ctlr->poll.does)
  2291. wakeup(&ctlr->poll);
  2292. epiowait(ep->hp, io, tmout, load);
  2293. if(debug > 1 || ep->debug > 1){
  2294. dumptd(td0, "epio: got: ");
  2295. qhdump(qh);
  2296. }
  2297. tot = 0;
  2298. c = a;
  2299. saved = 0;
  2300. ntds = 0;
  2301. for(td = td0; td != nil; td = ntd){
  2302. ntds++;
  2303. /*
  2304. * Use td tok, not io tok, because of setup packets.
  2305. * Also, if the Td was stalled or active (previous Td
  2306. * was a short packet), we must save the toggle as it is.
  2307. */
  2308. xcacheinvse(&ltd->csw, sizeof ltd->csw);
  2309. if(td->csw & (Tdhalt|Tdactive)){
  2310. if(saved++ == 0)
  2311. io->toggle = td->csw & Tddata1;
  2312. }else{
  2313. tot += td->ndata;
  2314. if((td->csw & Tdtok) == Tdtokin && td->ndata > 0){
  2315. xcacheinvse(td->data, td->ndata); /* filled by dma */
  2316. memmove(c, td->data, td->ndata);
  2317. c += td->ndata;
  2318. }
  2319. }
  2320. ntd = td->next;
  2321. tdfree(td);
  2322. }
  2323. err = io->err;
  2324. if(mustlock){
  2325. qunlock(io);
  2326. poperror();
  2327. }
  2328. ddeprint("epio: io %#p: %d tds: return %ld err '%s'\n",
  2329. io, ntds, tot, err);
  2330. if(err == Estalled)
  2331. return 0; /* that's our convention */
  2332. if(err != nil)
  2333. error(err);
  2334. if(tot < 0)
  2335. error(Eio);
  2336. return tot;
  2337. }
  2338. static long
  2339. epread(Ep *ep, void *a, long count)
  2340. {
  2341. Ctlio *cio;
  2342. Qio *io;
  2343. Isoio *iso;
  2344. char buf[160];
  2345. ulong delta;
  2346. ddeprint("ehci: epread\n");
  2347. if(ep->aux == nil)
  2348. panic("epread: not open");
  2349. pollcheck(ep->hp);
  2350. switch(ep->ttype){
  2351. case Tctl:
  2352. cio = ep->aux;
  2353. qlock(cio);
  2354. if(waserror()){
  2355. qunlock(cio);
  2356. nexterror();
  2357. }
  2358. ddeprint("epread ctl ndata %d\n", cio->ndata);
  2359. if(cio->ndata < 0)
  2360. error("request expected");
  2361. else if(cio->ndata == 0){
  2362. cio->ndata = -1;
  2363. count = 0;
  2364. }else{
  2365. if(count > cio->ndata)
  2366. count = cio->ndata;
  2367. if(count > 0)
  2368. memmove(a, cio->data, count);
  2369. /* BUG for big transfers */
  2370. free(cio->data);
  2371. cio->data = nil;
  2372. cio->ndata = 0; /* signal EOF next time */
  2373. }
  2374. qunlock(cio);
  2375. poperror();
  2376. if(debug>1 || ep->debug){
  2377. seprintdata(buf, buf+sizeof(buf), a, count);
  2378. print("epread: %s\n", buf);
  2379. }
  2380. return count;
  2381. case Tbulk:
  2382. io = ep->aux;
  2383. if(ep->clrhalt)
  2384. clrhalt(ep);
  2385. return epio(ep, &io[OREAD], a, count, 1);
  2386. case Tintr:
  2387. io = ep->aux;
  2388. delta = TK2MS(MACHP(0)->ticks) - io[OREAD].iotime + 1;
  2389. if(delta < ep->pollival / 2)
  2390. tsleep(&up->sleep, return0, 0, ep->pollival/2 - delta);
  2391. if(ep->clrhalt)
  2392. clrhalt(ep);
  2393. return epio(ep, &io[OREAD], a, count, 1);
  2394. case Tiso:
  2395. iso = ep->aux;
  2396. return episoread(ep, iso, a, count);
  2397. }
  2398. return -1;
  2399. }
  2400. /*
  2401. * Control transfers are one setup write (data0)
  2402. * plus zero or more reads/writes (data1, data0, ...)
  2403. * plus a final write/read with data1 to ack.
  2404. * For both host to device and device to host we perform
  2405. * the entire transfer when the user writes the request,
  2406. * and keep any data read from the device for a later read.
  2407. * We call epio three times instead of placing all Tds at
  2408. * the same time because doing so leads to crc/tmout errors
  2409. * for some devices.
  2410. * Upon errors on the data phase we must still run the status
  2411. * phase or the device may cease responding in the future.
  2412. */
  2413. static long
  2414. epctlio(Ep *ep, Ctlio *cio, void *a, long count)
  2415. {
  2416. uchar *c;
  2417. long len;
  2418. ddeprint("epctlio: cio %#p ep%d.%d count %ld\n",
  2419. cio, ep->dev->nb, ep->nb, count);
  2420. if(count < Rsetuplen)
  2421. error("short usb comand");
  2422. qlock(cio);
  2423. free(cio->data);
  2424. cio->data = nil;
  2425. cio->ndata = 0;
  2426. if(waserror()){
  2427. qunlock(cio);
  2428. free(cio->data);
  2429. cio->data = nil;
  2430. cio->ndata = 0;
  2431. nexterror();
  2432. }
  2433. /* set the address if unset and out of configuration state */
  2434. if(ep->dev->state != Dconfig && ep->dev->state != Dreset)
  2435. if(cio->usbid == 0){
  2436. cio->usbid = (ep->nb&Epmax) << 7 | ep->dev->nb&Devmax;
  2437. qhsetaddr(cio->qh, cio->usbid);
  2438. }
  2439. /* adjust maxpkt if the user has learned a different one */
  2440. if(qhmaxpkt(cio->qh) != ep->maxpkt)
  2441. qhsetmaxpkt(cio->qh, ep->maxpkt);
  2442. c = a;
  2443. cio->tok = Tdtoksetup;
  2444. cio->toggle = Tddata0;
  2445. if(epio(ep, cio, a, Rsetuplen, 0) < Rsetuplen)
  2446. error(Eio);
  2447. a = c + Rsetuplen;
  2448. count -= Rsetuplen;
  2449. cio->toggle = Tddata1;
  2450. if(c[Rtype] & Rd2h){
  2451. cio->tok = Tdtokin;
  2452. len = GET2(c+Rcount);
  2453. if(len <= 0)
  2454. error("bad length in d2h request");
  2455. if(len > Maxctllen)
  2456. error("d2h data too large to fit in ehci");
  2457. a = cio->data = smalloc(len+1);
  2458. }else{
  2459. cio->tok = Tdtokout;
  2460. len = count;
  2461. }
  2462. if(len > 0)
  2463. if(waserror())
  2464. len = -1;
  2465. else{
  2466. len = epio(ep, cio, a, len, 0);
  2467. poperror();
  2468. }
  2469. if(c[Rtype] & Rd2h){
  2470. count = Rsetuplen;
  2471. cio->ndata = len;
  2472. cio->tok = Tdtokout;
  2473. }else{
  2474. if(len < 0)
  2475. count = -1;
  2476. else
  2477. count = Rsetuplen + len;
  2478. cio->tok = Tdtokin;
  2479. }
  2480. cio->toggle = Tddata1;
  2481. epio(ep, cio, nil, 0, 0);
  2482. qunlock(cio);
  2483. poperror();
  2484. ddeprint("epctlio cio %#p return %ld\n", cio, count);
  2485. return count;
  2486. }
  2487. static long
  2488. epwrite(Ep *ep, void *a, long count)
  2489. {
  2490. Qio *io;
  2491. Ctlio *cio;
  2492. Isoio *iso;
  2493. ulong delta;
  2494. pollcheck(ep->hp);
  2495. ddeprint("ehci: epwrite ep%d.%d\n", ep->dev->nb, ep->nb);
  2496. if(ep->aux == nil)
  2497. panic("ehci: epwrite: not open");
  2498. switch(ep->ttype){
  2499. case Tctl:
  2500. cio = ep->aux;
  2501. return epctlio(ep, cio, a, count);
  2502. case Tbulk:
  2503. io = ep->aux;
  2504. if(ep->clrhalt)
  2505. clrhalt(ep);
  2506. return epio(ep, &io[OWRITE], a, count, 1);
  2507. case Tintr:
  2508. io = ep->aux;
  2509. delta = TK2MS(MACHP(0)->ticks) - io[OWRITE].iotime + 1;
  2510. if(delta < ep->pollival)
  2511. tsleep(&up->sleep, return0, 0, ep->pollival - delta);
  2512. if(ep->clrhalt)
  2513. clrhalt(ep);
  2514. return epio(ep, &io[OWRITE], a, count, 1);
  2515. case Tiso:
  2516. iso = ep->aux;
  2517. return episowrite(ep, iso, a, count);
  2518. }
  2519. return -1;
  2520. }
  2521. static void
  2522. isofsinit(Ep *ep, Isoio *iso)
  2523. {
  2524. long left;
  2525. Sitd *td, *ltd;
  2526. int i;
  2527. ulong frno;
  2528. left = 0;
  2529. ltd = nil;
  2530. frno = iso->td0frno;
  2531. for(i = 0; i < iso->nframes; i++){
  2532. td = sitdalloc();
  2533. td->data = iso->data + i * ep->maxpkt;
  2534. td->epc = ep->dev->port << Stdportshift;
  2535. td->epc |= ep->dev->hub << Stdhubshift;
  2536. td->epc |= ep->nb << Stdepshift;
  2537. td->epc |= ep->dev->nb << Stddevshift;
  2538. td->mfs = 034 << Stdscmshift | 1 << Stdssmshift;
  2539. if(ep->mode == OREAD){
  2540. td->epc |= Stdin;
  2541. td->mdata = ep->maxpkt;
  2542. }else{
  2543. td->mdata = (ep->hz+left) * ep->pollival / 1000;
  2544. td->mdata *= ep->samplesz;
  2545. left = (ep->hz+left) * ep->pollival % 1000;
  2546. if(td->mdata > ep->maxpkt){
  2547. print("ehci: ep%d.%d: size > maxpkt\n",
  2548. ep->dev->nb, ep->nb);
  2549. print("size = %ld max = %ld\n",
  2550. td->mdata,ep->maxpkt);
  2551. td->mdata = ep->maxpkt;
  2552. }
  2553. }
  2554. xcachewbse(&td->link, sizeof td->link); /* all hw state */
  2555. iso->sitdps[frno] = td;
  2556. sitdinit(iso, td);
  2557. if(ltd != nil)
  2558. ltd->next = td;
  2559. ltd = td;
  2560. frno = TRUNC(frno+ep->pollival, Nisoframes);
  2561. }
  2562. ltd->next = iso->sitdps[iso->td0frno];
  2563. }
  2564. static void
  2565. isohsinit(Ep *ep, Isoio *iso)
  2566. {
  2567. int ival, p;
  2568. long left;
  2569. ulong frno, i, pa;
  2570. Itd *ltd, *td;
  2571. iso->hs = 1;
  2572. ival = 1;
  2573. if(ep->pollival > 8)
  2574. ival = ep->pollival/8;
  2575. left = 0;
  2576. ltd = nil;
  2577. frno = iso->td0frno;
  2578. for(i = 0; i < iso->nframes; i++){
  2579. td = itdalloc();
  2580. td->data = iso->data + i * 8 * iso->maxsize;
  2581. pa = PADDR(td->data) & ~0xFFF;
  2582. for(p = 0; p < 8; p++)
  2583. td->buffer[i] = pa + p * 0x1000;
  2584. td->buffer[0] = PADDR(iso->data) & ~0xFFF |
  2585. ep->nb << Itdepshift | ep->dev->nb << Itddevshift;
  2586. if(ep->mode == OREAD)
  2587. td->buffer[1] |= Itdin;
  2588. else
  2589. td->buffer[1] |= Itdout;
  2590. td->buffer[1] |= ep->maxpkt << Itdmaxpktshift;
  2591. td->buffer[2] |= ep->ntds << Itdntdsshift;
  2592. if(ep->mode == OREAD)
  2593. td->mdata = 8 * iso->maxsize;
  2594. else{
  2595. td->mdata = (ep->hz + left) * ep->pollival / 1000;
  2596. td->mdata *= ep->samplesz;
  2597. left = (ep->hz + left) * ep->pollival % 1000;
  2598. }
  2599. xcachewbse(&td->link, sizeof td->link); /* hw state */
  2600. xcachewbse(td->buffer, sizeof td->buffer[0]); /* hw state */
  2601. iso->itdps[frno] = td;
  2602. itdinit(iso, td);
  2603. if(ltd != nil)
  2604. ltd->next = td;
  2605. ltd = td;
  2606. frno = TRUNC(frno + ival, Nisoframes);
  2607. }
  2608. }
  2609. static void
  2610. isoopen(Ctlr *ctlr, Ep *ep)
  2611. {
  2612. int ival; /* pollival in ms */
  2613. int tpf; /* tds per frame */
  2614. int i, n, w, woff;
  2615. ulong frno;
  2616. Isoio *iso;
  2617. iso = ep->aux;
  2618. switch(ep->mode){
  2619. case OREAD:
  2620. iso->tok = Tdtokin;
  2621. break;
  2622. case OWRITE:
  2623. iso->tok = Tdtokout;
  2624. break;
  2625. default:
  2626. error("iso i/o is half-duplex");
  2627. }
  2628. iso->usbid = ep->nb << 7 | ep->dev->nb & Devmax;
  2629. iso->state = Qidle;
  2630. iso->debug = ep->debug;
  2631. ival = ep->pollival;
  2632. tpf = 1;
  2633. if(ep->dev->speed == Highspeed){
  2634. tpf = 8;
  2635. if(ival <= 8)
  2636. ival = 1;
  2637. else
  2638. ival /= 8;
  2639. }
  2640. assert(ival != 0);
  2641. iso->nframes = Nisoframes / ival;
  2642. if(iso->nframes < 3)
  2643. error("uhci isoopen bug"); /* we need at least 3 tds */
  2644. iso->maxsize = ep->ntds * ep->maxpkt;
  2645. if(ctlr->load + ep->load > 800)
  2646. print("usb: ehci: bandwidth may be exceeded\n");
  2647. ilock(ctlr);
  2648. ctlr->load += ep->load;
  2649. ctlr->isoload += ep->load;
  2650. ctlr->nreqs++;
  2651. dprint("ehci: load %uld isoload %uld\n", ctlr->load, ctlr->isoload);
  2652. diprint("iso nframes %d pollival %uld ival %d maxpkt %uld ntds %d\n",
  2653. iso->nframes, ep->pollival, ival, ep->maxpkt, ep->ntds);
  2654. iunlock(ctlr);
  2655. if(ctlr->poll.does)
  2656. wakeup(&ctlr->poll);
  2657. /*
  2658. * From here on this cannot raise errors
  2659. * unless we catch them and release here all memory allocated.
  2660. */
  2661. assert(ep->maxpkt > 0 && ep->ntds > 0 && ep->ntds < 4);
  2662. assert(ep->maxpkt <= 1024);
  2663. iso->tdps = smalloc(sizeof(uintptr) * Nisoframes);
  2664. iso->data = smalloc(iso->nframes * tpf * ep->ntds * ep->maxpkt);
  2665. iso->td0frno = TRUNC(ctlr->opio->frno + 10, Nisoframes);
  2666. /* read: now; write: 1s ahead */
  2667. if(ep->dev->speed == Highspeed)
  2668. isohsinit(ep, iso);
  2669. else
  2670. isofsinit(ep, iso);
  2671. iso->tdu = iso->tdi = iso->itdps[iso->td0frno];
  2672. iso->stdu = iso->stdi = iso->sitdps[iso->td0frno];
  2673. ilock(ctlr);
  2674. frno = iso->td0frno;
  2675. for(i = 0; i < iso->nframes; i++){
  2676. *iso->tdps[frno] = ctlr->frames[frno];
  2677. frno = TRUNC(frno+ival, Nisoframes);
  2678. }
  2679. /*
  2680. * Iso uses a virtual frame window of Nisoframes, and we must
  2681. * fill the actual ctlr frame array by placing ctlr->nframes/Nisoframes
  2682. * copies of the window in the frame array.
  2683. */
  2684. assert(ctlr->nframes >= Nisoframes && Nisoframes >= iso->nframes);
  2685. assert(Nisoframes >= Nintrleafs);
  2686. n = ctlr->nframes / Nisoframes;
  2687. for(w = 0; w < n; w++){
  2688. frno = iso->td0frno;
  2689. woff = w * Nisoframes;
  2690. for(i = 0; i < iso->nframes ; i++){
  2691. assert(woff+frno < ctlr->nframes);
  2692. assert(iso->tdps[frno] != nil);
  2693. if(ep->dev->speed == Highspeed)
  2694. ctlr->frames[woff+frno] = PADDR(iso->tdps[frno])
  2695. |Litd;
  2696. else
  2697. ctlr->frames[woff+frno] = PADDR(iso->tdps[frno])
  2698. |Lsitd;
  2699. xcachewbse(&ctlr->frames[woff+frno],
  2700. sizeof ctlr->frames[0]);
  2701. frno = TRUNC(frno+ep->pollival, Nisoframes);
  2702. }
  2703. }
  2704. iso->next = ctlr->iso;
  2705. ctlr->iso = iso;
  2706. iso->state = Qdone;
  2707. iunlock(ctlr);
  2708. if(debug > 1 || iso->debug >1)
  2709. isodump(iso, 0);
  2710. }
  2711. /*
  2712. * Allocate the endpoint and set it up for I/O
  2713. * in the controller. This must follow what's said
  2714. * in Ep regarding configuration, including perhaps
  2715. * the saved toggles (saved on a previous close of
  2716. * the endpoint data file by epclose).
  2717. */
  2718. static void
  2719. epopen(Ep *ep)
  2720. {
  2721. Ctlr *ctlr;
  2722. Ctlio *cio;
  2723. Qio *io;
  2724. int usbid;
  2725. ctlr = ep->hp->aux;
  2726. deprint("ehci: epopen ep%d.%d\n", ep->dev->nb, ep->nb);
  2727. if(ep->aux != nil)
  2728. panic("ehci: epopen called with open ep");
  2729. if(waserror()){
  2730. free(ep->aux);
  2731. ep->aux = nil;
  2732. nexterror();
  2733. }
  2734. switch(ep->ttype){
  2735. case Tnone:
  2736. error("endpoint not configured");
  2737. case Tiso:
  2738. ep->aux = smalloc(sizeof(Isoio));
  2739. isoopen(ctlr, ep);
  2740. break;
  2741. case Tctl:
  2742. cio = ep->aux = smalloc(sizeof(Ctlio));
  2743. cio->debug = ep->debug;
  2744. cio->ndata = -1;
  2745. cio->data = nil;
  2746. if(ep->dev->isroot != 0 && ep->nb == 0) /* root hub */
  2747. break;
  2748. cio->qh = qhalloc(ctlr, ep, cio, "epc");
  2749. break;
  2750. case Tbulk:
  2751. ep->pollival = 1; /* assume this; doesn't really matter */
  2752. /* and fall... */
  2753. case Tintr:
  2754. io = ep->aux = smalloc(sizeof(Qio)*2);
  2755. io[OREAD].debug = io[OWRITE].debug = ep->debug;
  2756. usbid = (ep->nb&Epmax) << 7 | ep->dev->nb &Devmax;
  2757. assert(ep->pollival != 0);
  2758. if(ep->mode != OREAD){
  2759. if(ep->toggle[OWRITE] != 0)
  2760. io[OWRITE].toggle = Tddata1;
  2761. else
  2762. io[OWRITE].toggle = Tddata0;
  2763. io[OWRITE].tok = Tdtokout;
  2764. io[OWRITE].usbid = usbid;
  2765. io[OWRITE].bw = ep->maxpkt*1000/ep->pollival; /* bytes/s */
  2766. io[OWRITE].qh = qhalloc(ctlr, ep, io+OWRITE, "epw");
  2767. }
  2768. if(ep->mode != OWRITE){
  2769. if(ep->toggle[OREAD] != 0)
  2770. io[OREAD].toggle = Tddata1;
  2771. else
  2772. io[OREAD].toggle = Tddata0;
  2773. io[OREAD].tok = Tdtokin;
  2774. io[OREAD].usbid = usbid;
  2775. io[OREAD].bw = ep->maxpkt*1000/ep->pollival; /* bytes/s */
  2776. io[OREAD].qh = qhalloc(ctlr, ep, io+OREAD, "epr");
  2777. }
  2778. break;
  2779. }
  2780. if(debug>1 || ep->debug)
  2781. dump(ep->hp);
  2782. deprint("ehci: epopen done\n");
  2783. poperror();
  2784. }
  2785. static void
  2786. cancelio(Ctlr *ctlr, Qio *io)
  2787. {
  2788. Qh *qh;
  2789. ilock(ctlr);
  2790. qh = io->qh;
  2791. if(io == nil || io->qh == nil || io->qh->state == Qclose){
  2792. iunlock(ctlr);
  2793. return;
  2794. }
  2795. dqprint("ehci: cancelio for qh %#p state %s\n",
  2796. qh, qhsname[qh->state]);
  2797. aborttds(qh);
  2798. qh->state = Qclose;
  2799. iunlock(ctlr);
  2800. if(!waserror()){
  2801. tsleep(&up->sleep, return0, 0, Abortdelay);
  2802. poperror();
  2803. }
  2804. wakeup(io);
  2805. qlock(io);
  2806. /* wait for epio if running */
  2807. qunlock(io);
  2808. qhfree(ctlr, qh);
  2809. io->qh = nil;
  2810. }
  2811. static void
  2812. cancelisoio(Ctlr *ctlr, Isoio *iso, int pollival, ulong load)
  2813. {
  2814. int frno, i, n, t, w, woff;
  2815. ulong *lp, *tp;
  2816. Isoio **il;
  2817. Itd *td;
  2818. Sitd *std;
  2819. ilock(ctlr);
  2820. if(iso->state == Qclose){
  2821. iunlock(ctlr);
  2822. return;
  2823. }
  2824. ctlr->nreqs--;
  2825. if(iso->state != Qrun && iso->state != Qdone)
  2826. panic("bad iso state");
  2827. iso->state = Qclose;
  2828. if(ctlr->isoload < load)
  2829. panic("ehci: low isoload");
  2830. ctlr->isoload -= load;
  2831. ctlr->load -= load;
  2832. for(il = &ctlr->iso; *il != nil; il = &(*il)->next)
  2833. if(*il == iso)
  2834. break;
  2835. if(*il == nil)
  2836. panic("cancleiso: not found");
  2837. *il = iso->next;
  2838. frno = iso->td0frno;
  2839. for(i = 0; i < iso->nframes; i++){
  2840. tp = iso->tdps[frno];
  2841. if(iso->hs != 0){
  2842. td = iso->itdps[frno];
  2843. xcacheinvse(td->csw, sizeof td->csw);
  2844. for(t = 0; t < nelem(td->csw); t++)
  2845. td->csw[t] &= ~(Itdioc|Itdactive);
  2846. xcachewbse(td->csw, sizeof td->csw);
  2847. }else{
  2848. std = iso->sitdps[frno];
  2849. xcacheinvse(&std->csw, sizeof std->csw);
  2850. std->csw &= ~(Stdioc|Stdactive);
  2851. xcachewbse(&std->csw, sizeof std->csw);
  2852. }
  2853. for(lp = &ctlr->frames[frno]; !(*lp & Lterm);
  2854. lp = &LPTR(*lp)[0])
  2855. if(LPTR(*lp) == tp)
  2856. break;
  2857. if(*lp & Lterm)
  2858. panic("cancelisoio: td not found");
  2859. *lp = tp[0];
  2860. /*
  2861. * Iso uses a virtual frame window of Nisoframes, and we must
  2862. * restore pointers in copies of the window kept at ctlr->frames.
  2863. */
  2864. if(lp == &ctlr->frames[frno]){
  2865. n = ctlr->nframes / Nisoframes;
  2866. for(w = 1; w < n; w++){
  2867. woff = w * Nisoframes;
  2868. ctlr->frames[woff+frno] = *lp;
  2869. xcachewbse(&ctlr->frames[woff+frno],
  2870. sizeof ctlr->frames[0]);
  2871. }
  2872. }
  2873. frno = TRUNC(frno+pollival, Nisoframes);
  2874. }
  2875. iunlock(ctlr);
  2876. /*
  2877. * wakeup anyone waiting for I/O and
  2878. * wait to be sure no I/O is in progress in the controller.
  2879. * and then wait to be sure episo* is no longer running.
  2880. */
  2881. wakeup(iso);
  2882. diprint("cancelisoio iso %#p waiting for I/O to cease\n", iso);
  2883. tsleep(&up->sleep, return0, 0, 5);
  2884. qlock(iso);
  2885. qunlock(iso);
  2886. diprint("cancelisoio iso %#p releasing iso\n", iso);
  2887. frno = iso->td0frno;
  2888. for(i = 0; i < iso->nframes; i++){
  2889. if(iso->hs != 0)
  2890. itdfree(iso->itdps[frno]);
  2891. else
  2892. sitdfree(iso->sitdps[frno]);
  2893. iso->tdps[frno] = nil;
  2894. frno = TRUNC(frno+pollival, Nisoframes);
  2895. }
  2896. free(iso->tdps);
  2897. iso->tdps = nil;
  2898. free(iso->data);
  2899. iso->data = nil;
  2900. }
  2901. static void
  2902. epclose(Ep *ep)
  2903. {
  2904. Qio *io;
  2905. Ctlio *cio;
  2906. Isoio *iso;
  2907. Ctlr *ctlr;
  2908. ctlr = ep->hp->aux;
  2909. deprint("ehci: epclose ep%d.%d\n", ep->dev->nb, ep->nb);
  2910. if(ep->aux == nil)
  2911. panic("ehci: epclose called with closed ep");
  2912. switch(ep->ttype){
  2913. case Tctl:
  2914. cio = ep->aux;
  2915. cancelio(ctlr, cio);
  2916. free(cio->data);
  2917. cio->data = nil;
  2918. break;
  2919. case Tintr:
  2920. case Tbulk:
  2921. io = ep->aux;
  2922. ep->toggle[OREAD] = ep->toggle[OWRITE] = 0;
  2923. if(ep->mode != OWRITE){
  2924. cancelio(ctlr, &io[OREAD]);
  2925. if(io[OREAD].toggle == Tddata1)
  2926. ep->toggle[OREAD] = 1;
  2927. }
  2928. if(ep->mode != OREAD){
  2929. cancelio(ctlr, &io[OWRITE]);
  2930. if(io[OWRITE].toggle == Tddata1)
  2931. ep->toggle[OWRITE] = 1;
  2932. }
  2933. break;
  2934. case Tiso:
  2935. iso = ep->aux;
  2936. cancelisoio(ctlr, iso, ep->pollival, ep->load);
  2937. break;
  2938. default:
  2939. panic("epclose: bad ttype");
  2940. }
  2941. free(ep->aux);
  2942. ep->aux = nil;
  2943. }
  2944. static void
  2945. scanpci(void) /* actually just use fixed addresses on sheeva */
  2946. {
  2947. Ctlr *ctlr;
  2948. static int already = 0;
  2949. if(already)
  2950. return;
  2951. already = 1;
  2952. ctlr = mallocz(sizeof(Ctlr), 1);
  2953. /* the sheeva's usb 2.0 otg uses a superset of the ehci registers */
  2954. ctlr->capio = (Ecapio *)(soc.ehci + 0x100);
  2955. ctlr->opio = (Eopio *) (soc.ehci + 0x140);
  2956. dprint("usbehci: port %#p\n", ctlr->capio);
  2957. ctlrs[0] = ctlr;
  2958. }
  2959. /*
  2960. * return smallest power of 2 >= n
  2961. */
  2962. static int
  2963. flog2(int n)
  2964. {
  2965. int i;
  2966. for(i = 0; (1 << i) < n; i++)
  2967. ;
  2968. return i;
  2969. }
  2970. /*
  2971. * build the periodic scheduling tree:
  2972. * framesize must be a multiple of the tree size
  2973. */
  2974. static void
  2975. mkqhtree(Ctlr *ctlr)
  2976. {
  2977. int i, n, d, o, leaf0, depth;
  2978. ulong leafs[Nintrleafs];
  2979. Qh *qh;
  2980. Qh **tree;
  2981. Qtree *qt;
  2982. depth = flog2(Nintrleafs);
  2983. n = (1 << (depth+1)) - 1;
  2984. qt = mallocz(sizeof(*qt), 1);
  2985. if(qt == nil)
  2986. panic("ehci: mkqhtree: no memory");
  2987. qt->nel = n;
  2988. qt->depth = depth;
  2989. qt->bw = mallocz(n * sizeof(qt->bw), 1);
  2990. qt->root = tree = mallocz(n * sizeof(Qh *), 1);
  2991. if(qt->bw == nil || tree == nil)
  2992. panic("ehci: mkqhtree: no memory");
  2993. for(i = 0; i < n; i++){
  2994. qh = tree[i] = edalloc();
  2995. if(qh == nil)
  2996. panic("ehci: mkqhtree: no memory");
  2997. qh->nlink = qh->alink = qh->link = Lterm;
  2998. qh->csw = Tdhalt;
  2999. qh->state = Qidle;
  3000. xcachewbse(&qh->link, sizeof qh->link); /* hw state */
  3001. xcachewbse(qh->buffer, sizeof qh->buffer[0]); /* hw state */
  3002. if(i > 0)
  3003. qhlinkqh(tree[i], tree[(i-1)/2]);
  3004. }
  3005. ctlr->ntree = i;
  3006. dprint("ehci: tree: %d endpoints allocated\n", i);
  3007. /* distribute leaves evenly round the frame list */
  3008. leaf0 = n / 2;
  3009. for(i = 0; i < Nintrleafs; i++){
  3010. o = 0;
  3011. for(d = 0; d < depth; d++){
  3012. o <<= 1;
  3013. if(i & (1 << d))
  3014. o |= 1;
  3015. }
  3016. if(leaf0 + o >= n){
  3017. print("leaf0=%d o=%d i=%d n=%d\n", leaf0, o, i, n);
  3018. break;
  3019. }
  3020. leafs[i] = PADDR(tree[leaf0 + o]) | Lqh;
  3021. }
  3022. assert((ctlr->nframes % Nintrleafs) == 0);
  3023. for(i = 0; i < ctlr->nframes; i += Nintrleafs){
  3024. memmove(ctlr->frames + i, leafs, sizeof leafs);
  3025. xcachewbse(ctlr->frames + i, sizeof leafs);
  3026. }
  3027. ctlr->tree = qt;
  3028. }
  3029. static void
  3030. ehcimeminit(Ctlr *ctlr)
  3031. {
  3032. int i, frsize;
  3033. Eopio *opio;
  3034. opio = ctlr->opio;
  3035. frsize = ctlr->nframes * sizeof(ulong);
  3036. assert((frsize & 0xFFF) == 0); /* must be 4k aligned */
  3037. ctlr->frames = xspanalloc(frsize, frsize, 0);
  3038. if(ctlr->frames == nil)
  3039. panic("ehci reset: no memory");
  3040. for (i = 0; i < ctlr->nframes; i++)
  3041. ctlr->frames[i] = Lterm;
  3042. opio->frbase = PADDR(ctlr->frames);
  3043. opio->frno = 0;
  3044. qhalloc(ctlr, nil, nil, nil); /* init async list */
  3045. mkqhtree(ctlr); /* init sync list */
  3046. edfree(edalloc()); /* try to get some ones pre-allocated */
  3047. dprint("ehci %#p flb %#lux frno %#lux\n",
  3048. ctlr->capio, opio->frbase, opio->frno);
  3049. }
  3050. static void
  3051. init(Hci *hp)
  3052. {
  3053. Ctlr *ctlr;
  3054. Eopio *opio;
  3055. int i;
  3056. hp->highspeed = 1;
  3057. ctlr = hp->aux;
  3058. opio = ctlr->opio;
  3059. dprint("ehci %#p init\n", ctlr->capio);
  3060. ilock(ctlr);
  3061. /*
  3062. * Unless we activate frroll interrupt
  3063. * some machines won't post other interrupts.
  3064. */
  3065. opio->intr = Iusb|Ierr|Iportchg|Ihcerr|Iasync;
  3066. opio->cmd |= Cpse;
  3067. coherence();
  3068. opio->cmd |= Case;
  3069. coherence();
  3070. ehcirun(ctlr, 1);
  3071. opio->config = Callmine; /* reclaim all ports */
  3072. coherence();
  3073. for (i = 0; i < hp->nports; i++)
  3074. opio->portsc[i] = Pspower;
  3075. coherence();
  3076. iunlock(ctlr);
  3077. if(debug > 1)
  3078. dump(hp);
  3079. }
  3080. #define WINTARG(ctl) (((ctl) >> 4) & 017)
  3081. #define WINATTR(ctl) (((ctl) >> 8) & 0377)
  3082. #define WIN64KSIZE(ctl) (((ctl) >> 16) + 1)
  3083. #define SIZETO64KSIZE(size) ((size) / (64*1024) - 1)
  3084. static void
  3085. addrmapdump(void)
  3086. {
  3087. int i;
  3088. ulong ctl, targ, attr, size64k;
  3089. Kwusb *map;
  3090. Usbwin *win;
  3091. if (!Debug)
  3092. return;
  3093. map = (Kwusb *)(soc.ehci + 0x300);
  3094. for (i = 0; i < nelem(map->win); i++) {
  3095. win = &map->win[i];
  3096. ctl = win->ctl;
  3097. if (ctl & Winenable) {
  3098. targ = WINTARG(ctl);
  3099. attr = WINATTR(ctl);
  3100. size64k = WIN64KSIZE(ctl);
  3101. print("usbehci: address map window %d: "
  3102. "targ %ld attr %#lux size %,ld addr %#lux\n",
  3103. i, targ, attr, size64k * 64*1024, win->base);
  3104. }
  3105. }
  3106. }
  3107. /* assumes ctlr is ilocked */
  3108. static void
  3109. ctlrreset(Ctlr *ctlr)
  3110. {
  3111. int i;
  3112. Eopio *opio;
  3113. opio = ctlr->opio;
  3114. opio->cmd |= Chcreset;
  3115. coherence();
  3116. /* wait for it to come out of reset */
  3117. for(i = 0; i < 100 && opio->cmd & Chcreset; i++)
  3118. delay(1);
  3119. if(i >= 100)
  3120. print("ehci %#p controller reset timed out\n", ctlr->capio);
  3121. /*
  3122. * Marvell errata FE-USB-340 workaround: 1 << 4 magic:
  3123. * disable streaming. Magic 3 (usb host mode) from the linux driver
  3124. * makes it work. Ick.
  3125. */
  3126. opio->usbmode |= 1 << 4 | 3;
  3127. coherence();
  3128. }
  3129. /*
  3130. * configure window `win' as 256MB dram with attribute `attr' and
  3131. * base address
  3132. */
  3133. static void
  3134. setaddrwin(Kwusb *kw, int win, int attr, ulong base)
  3135. {
  3136. kw->win[win].ctl = Winenable | Targdram << 4 | attr << 8 |
  3137. SIZETO64KSIZE(256*MB) << 16;
  3138. kw->win[win].base = base;
  3139. }
  3140. static void
  3141. ehcireset(Ctlr *ctlr)
  3142. {
  3143. int i, amp, txvdd;
  3144. ulong v;
  3145. Eopio *opio;
  3146. Kwusb *kw;
  3147. ilock(ctlr);
  3148. dprint("ehci %#p reset\n", ctlr->capio);
  3149. opio = ctlr->opio;
  3150. kw = (Kwusb *)(soc.ehci + 0x300);
  3151. kw->bic = 0;
  3152. kw->bim = (1<<4) - 1; /* enable all defined intrs */
  3153. ctlrreset(ctlr);
  3154. /*
  3155. * clear high 32 bits of address signals if it's 64 bits capable.
  3156. * This is probably not needed but it does not hurt and others do it.
  3157. */
  3158. if((ctlr->capio->capparms & C64) != 0){
  3159. dprint("ehci: 64 bits\n");
  3160. opio->seg = 0;
  3161. }
  3162. /* requesting more interrupts per µframe may miss interrupts */
  3163. opio->cmd |= Citc8; /* 1 intr. per ms */
  3164. switch(opio->cmd & Cflsmask){
  3165. case Cfls1024:
  3166. ctlr->nframes = 1024;
  3167. break;
  3168. case Cfls512:
  3169. ctlr->nframes = 512;
  3170. break;
  3171. case Cfls256:
  3172. ctlr->nframes = 256;
  3173. break;
  3174. default:
  3175. panic("ehci: unknown fls %ld", opio->cmd & Cflsmask);
  3176. }
  3177. dprint("ehci: %d frames\n", ctlr->nframes);
  3178. /*
  3179. * set up the USB address map (bridge address decoding)
  3180. */
  3181. for (i = 0; i < nelem(kw->win); i++)
  3182. kw->win[i].ctl = kw->win[i].base = 0;
  3183. coherence();
  3184. setaddrwin(kw, 0, Attrcs0, 0);
  3185. setaddrwin(kw, 1, Attrcs1, 256*MB);
  3186. coherence();
  3187. if (Debug)
  3188. if (kw->bcs & (1 << 4))
  3189. print("usbehci: not swapping bytes\n");
  3190. else
  3191. print("usbehci: swapping bytes\n");
  3192. addrmapdump(); /* verify sanity */
  3193. kw->pwrctl |= 1 << 0 | 1 << 1; /* Pu | PuPll */
  3194. coherence();
  3195. /*
  3196. * Marvell guideline GL-USB-160.
  3197. */
  3198. kw->phypll |= 1 << 21; /* VCOCAL_START: PLL calibration */
  3199. coherence();
  3200. microdelay(100);
  3201. kw->phypll &= ~(1 << 21);
  3202. v = kw->phytxctl & ~(017 << 27 | 7); /* REG_EXT_FS_RCALL & AMP_2_0 */
  3203. switch (m->socrev) {
  3204. default:
  3205. print("usehci: bad 6281 soc rev %d\n", m->socrev);
  3206. /* fall through */
  3207. case Socreva0:
  3208. amp = 4;
  3209. txvdd = 1;
  3210. break;
  3211. case Socreva1:
  3212. amp = 3;
  3213. txvdd = 3;
  3214. break;
  3215. }
  3216. /* REG_EXT_FS_RCALL_EN | REG_RCAL_START | AMP_2_0 */
  3217. kw->phytxctl = v | 1 << 26 | 1 << 12 | amp;
  3218. coherence();
  3219. microdelay(100);
  3220. kw->phytxctl &= ~(1 << 12);
  3221. v = kw->phyrxctl & ~(3 << 2 | 017 << 4); /* LPF_COEF_1_0 & SQ_THRESH_3_0 */
  3222. kw->phyrxctl = v | 1 << 2 | 8 << 4;
  3223. v = kw->phyivref & ~(3 << 8); /* TXVDD12 */
  3224. kw->phyivref = v | txvdd << 8;
  3225. coherence();
  3226. ehcirun(ctlr, 0);
  3227. ctlrreset(ctlr);
  3228. iunlock(ctlr);
  3229. }
  3230. static void
  3231. setdebug(Hci*, int d)
  3232. {
  3233. debug = d;
  3234. }
  3235. static void
  3236. shutdown(Hci *hp)
  3237. {
  3238. Ctlr *ctlr;
  3239. Eopio *opio;
  3240. ctlr = hp->aux;
  3241. ilock(ctlr);
  3242. ctlrreset(ctlr);
  3243. delay(100);
  3244. ehcirun(ctlr, 0);
  3245. opio = ctlr->opio;
  3246. opio->frbase = 0;
  3247. coherence();
  3248. iunlock(ctlr);
  3249. }
  3250. static int
  3251. reset(Hci *hp)
  3252. {
  3253. static Lock resetlck;
  3254. int i;
  3255. Ctlr *ctlr;
  3256. Ecapio *capio;
  3257. ilock(&resetlck);
  3258. scanpci();
  3259. /*
  3260. * Any adapter matches if no hp->port is supplied,
  3261. * otherwise the ports must match.
  3262. */
  3263. ctlr = nil;
  3264. for(i = 0; i < Nhcis && ctlrs[i] != nil; i++){
  3265. ctlr = ctlrs[i];
  3266. if(ctlr->active == 0)
  3267. if(hp->port == 0 || hp->port == (uintptr)ctlr->capio){
  3268. ctlr->active = 1;
  3269. break;
  3270. }
  3271. }
  3272. iunlock(&resetlck);
  3273. if(ctlrs[i] == nil || i == Nhcis)
  3274. return -1;
  3275. hp->aux = ctlr;
  3276. hp->port = (uintptr)ctlr->capio;
  3277. hp->irq = IRQ0usb0;
  3278. hp->tbdf = 0;
  3279. capio = ctlr->capio;
  3280. hp->nports = capio->parms & Cnports;
  3281. ddprint("echi: %s, ncc %lud npcc %lud\n",
  3282. capio->parms & 0x10000 ? "leds" : "no leds",
  3283. (capio->parms >> 12) & 0xf, (capio->parms >> 8) & 0xf);
  3284. ddprint("ehci: routing %s, %sport power ctl, %d ports\n",
  3285. capio->parms & 0x40 ? "explicit" : "automatic",
  3286. capio->parms & 0x10 ? "" : "no ", hp->nports);
  3287. ehcireset(ctlr);
  3288. ehcimeminit(ctlr);
  3289. /*
  3290. * Linkage to the generic HCI driver.
  3291. */
  3292. hp->init = init;
  3293. hp->dump = dump;
  3294. hp->interrupt = interrupt;
  3295. hp->epopen = epopen;
  3296. hp->epclose = epclose;
  3297. hp->epread = epread;
  3298. hp->epwrite = epwrite;
  3299. hp->seprintep = seprintep;
  3300. hp->portenable = portenable;
  3301. hp->portreset = portreset;
  3302. hp->portstatus = portstatus;
  3303. hp->shutdown = shutdown;
  3304. hp->debug = setdebug;
  3305. hp->type = "ehci";
  3306. return 0;
  3307. }
  3308. void
  3309. usbehcilink(void)
  3310. {
  3311. addhcitype("ehci", reset);
  3312. }