xhci.c 165 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * xHCI host controller driver
  4. *
  5. * Copyright (C) 2008 Intel Corp.
  6. *
  7. * Author: Sarah Sharp
  8. * Some code borrowed from the Linux EHCI driver.
  9. */
  10. #include <linux/pci.h>
  11. #ifndef __GENKSYMS__ /* ANDROID: KABI CRC preservation hack */
  12. #include <linux/iommu.h>
  13. #endif
  14. #include <linux/iopoll.h>
  15. #include <linux/irq.h>
  16. #include <linux/log2.h>
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/slab.h>
  20. #include <linux/dmi.h>
  21. #include <linux/dma-mapping.h>
  22. #include "xhci.h"
  23. #include "xhci-trace.h"
  24. #include "xhci-debugfs.h"
  25. #include "xhci-dbgcap.h"
  26. #define DRIVER_AUTHOR "Sarah Sharp"
  27. #define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
  28. #define PORT_WAKE_BITS (PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E)
  29. /* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */
  30. static int link_quirk;
  31. module_param(link_quirk, int, S_IRUGO | S_IWUSR);
  32. MODULE_PARM_DESC(link_quirk, "Don't clear the chain bit on a link TRB");
  33. static unsigned long long quirks;
  34. module_param(quirks, ullong, S_IRUGO);
  35. MODULE_PARM_DESC(quirks, "Bit flags for quirks to be enabled as default");
  36. static bool td_on_ring(struct xhci_td *td, struct xhci_ring *ring)
  37. {
  38. struct xhci_segment *seg = ring->first_seg;
  39. if (!td || !td->start_seg)
  40. return false;
  41. do {
  42. if (seg == td->start_seg)
  43. return true;
  44. seg = seg->next;
  45. } while (seg && seg != ring->first_seg);
  46. return false;
  47. }
  48. /*
  49. * xhci_handshake - spin reading hc until handshake completes or fails
  50. * @ptr: address of hc register to be read
  51. * @mask: bits to look at in result of read
  52. * @done: value of those bits when handshake succeeds
  53. * @usec: timeout in microseconds
  54. *
  55. * Returns negative errno, or zero on success
  56. *
  57. * Success happens when the "mask" bits have the specified value (hardware
  58. * handshake done). There are two failure modes: "usec" have passed (major
  59. * hardware flakeout), or the register reads as all-ones (hardware removed).
  60. */
  61. int xhci_handshake(void __iomem *ptr, u32 mask, u32 done, u64 timeout_us)
  62. {
  63. u32 result;
  64. int ret;
  65. ret = readl_poll_timeout_atomic(ptr, result,
  66. (result & mask) == done ||
  67. result == U32_MAX,
  68. 1, timeout_us);
  69. if (result == U32_MAX) /* card removed */
  70. return -ENODEV;
  71. return ret;
  72. }
  73. /*
  74. * Disable interrupts and begin the xHCI halting process.
  75. */
  76. void xhci_quiesce(struct xhci_hcd *xhci)
  77. {
  78. u32 halted;
  79. u32 cmd;
  80. u32 mask;
  81. mask = ~(XHCI_IRQS);
  82. halted = readl(&xhci->op_regs->status) & STS_HALT;
  83. if (!halted)
  84. mask &= ~CMD_RUN;
  85. cmd = readl(&xhci->op_regs->command);
  86. cmd &= mask;
  87. writel(cmd, &xhci->op_regs->command);
  88. }
  89. /*
  90. * Force HC into halt state.
  91. *
  92. * Disable any IRQs and clear the run/stop bit.
  93. * HC will complete any current and actively pipelined transactions, and
  94. * should halt within 16 ms of the run/stop bit being cleared.
  95. * Read HC Halted bit in the status register to see when the HC is finished.
  96. */
  97. int xhci_halt(struct xhci_hcd *xhci)
  98. {
  99. int ret;
  100. xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Halt the HC");
  101. xhci_quiesce(xhci);
  102. ret = xhci_handshake(&xhci->op_regs->status,
  103. STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
  104. if (ret) {
  105. xhci_warn(xhci, "Host halt failed, %d\n", ret);
  106. return ret;
  107. }
  108. xhci->xhc_state |= XHCI_STATE_HALTED;
  109. xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;
  110. return ret;
  111. }
  112. /*
  113. * Set the run bit and wait for the host to be running.
  114. */
  115. int xhci_start(struct xhci_hcd *xhci)
  116. {
  117. u32 temp;
  118. int ret;
  119. temp = readl(&xhci->op_regs->command);
  120. temp |= (CMD_RUN);
  121. xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Turn on HC, cmd = 0x%x.",
  122. temp);
  123. writel(temp, &xhci->op_regs->command);
  124. /*
  125. * Wait for the HCHalted Status bit to be 0 to indicate the host is
  126. * running.
  127. */
  128. ret = xhci_handshake(&xhci->op_regs->status,
  129. STS_HALT, 0, XHCI_MAX_HALT_USEC);
  130. if (ret == -ETIMEDOUT)
  131. xhci_err(xhci, "Host took too long to start, "
  132. "waited %u microseconds.\n",
  133. XHCI_MAX_HALT_USEC);
  134. if (!ret) {
  135. /* clear state flags. Including dying, halted or removing */
  136. xhci->xhc_state = 0;
  137. xhci->run_graceperiod = jiffies + msecs_to_jiffies(500);
  138. }
  139. return ret;
  140. }
  141. /*
  142. * Reset a halted HC.
  143. *
  144. * This resets pipelines, timers, counters, state machines, etc.
  145. * Transactions will be terminated immediately, and operational registers
  146. * will be set to their defaults.
  147. */
  148. int xhci_reset(struct xhci_hcd *xhci, u64 timeout_us)
  149. {
  150. u32 command;
  151. u32 state;
  152. int ret;
  153. state = readl(&xhci->op_regs->status);
  154. if (state == ~(u32)0) {
  155. xhci_warn(xhci, "Host not accessible, reset failed.\n");
  156. return -ENODEV;
  157. }
  158. if ((state & STS_HALT) == 0) {
  159. xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
  160. return 0;
  161. }
  162. xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Reset the HC");
  163. command = readl(&xhci->op_regs->command);
  164. command |= CMD_RESET;
  165. writel(command, &xhci->op_regs->command);
  166. /* Existing Intel xHCI controllers require a delay of 1 mS,
  167. * after setting the CMD_RESET bit, and before accessing any
  168. * HC registers. This allows the HC to complete the
  169. * reset operation and be ready for HC register access.
  170. * Without this delay, the subsequent HC register access,
  171. * may result in a system hang very rarely.
  172. */
  173. if (xhci->quirks & XHCI_INTEL_HOST)
  174. udelay(1000);
  175. ret = xhci_handshake(&xhci->op_regs->command, CMD_RESET, 0, timeout_us);
  176. if (ret)
  177. return ret;
  178. if (xhci->quirks & XHCI_ASMEDIA_MODIFY_FLOWCONTROL)
  179. usb_asmedia_modifyflowcontrol(to_pci_dev(xhci_to_hcd(xhci)->self.controller));
  180. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  181. "Wait for controller to be ready for doorbell rings");
  182. /*
  183. * xHCI cannot write to any doorbells or operational registers other
  184. * than status until the "Controller Not Ready" flag is cleared.
  185. */
  186. ret = xhci_handshake(&xhci->op_regs->status, STS_CNR, 0, timeout_us);
  187. xhci->usb2_rhub.bus_state.port_c_suspend = 0;
  188. xhci->usb2_rhub.bus_state.suspended_ports = 0;
  189. xhci->usb2_rhub.bus_state.resuming_ports = 0;
  190. xhci->usb3_rhub.bus_state.port_c_suspend = 0;
  191. xhci->usb3_rhub.bus_state.suspended_ports = 0;
  192. xhci->usb3_rhub.bus_state.resuming_ports = 0;
  193. return ret;
  194. }
  195. static void xhci_zero_64b_regs(struct xhci_hcd *xhci)
  196. {
  197. struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
  198. struct iommu_domain *domain;
  199. int err, i;
  200. u64 val;
  201. u32 intrs;
  202. /*
  203. * Some Renesas controllers get into a weird state if they are
  204. * reset while programmed with 64bit addresses (they will preserve
  205. * the top half of the address in internal, non visible
  206. * registers). You end up with half the address coming from the
  207. * kernel, and the other half coming from the firmware. Also,
  208. * changing the programming leads to extra accesses even if the
  209. * controller is supposed to be halted. The controller ends up with
  210. * a fatal fault, and is then ripe for being properly reset.
  211. *
  212. * Special care is taken to only apply this if the device is behind
  213. * an iommu. Doing anything when there is no iommu is definitely
  214. * unsafe...
  215. */
  216. domain = iommu_get_domain_for_dev(dev);
  217. if (!(xhci->quirks & XHCI_ZERO_64B_REGS) || !domain ||
  218. domain->type == IOMMU_DOMAIN_IDENTITY)
  219. return;
  220. xhci_info(xhci, "Zeroing 64bit base registers, expecting fault\n");
  221. /* Clear HSEIE so that faults do not get signaled */
  222. val = readl(&xhci->op_regs->command);
  223. val &= ~CMD_HSEIE;
  224. writel(val, &xhci->op_regs->command);
  225. /* Clear HSE (aka FATAL) */
  226. val = readl(&xhci->op_regs->status);
  227. val |= STS_FATAL;
  228. writel(val, &xhci->op_regs->status);
  229. /* Now zero the registers, and brace for impact */
  230. val = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
  231. if (upper_32_bits(val))
  232. xhci_write_64(xhci, 0, &xhci->op_regs->dcbaa_ptr);
  233. val = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
  234. if (upper_32_bits(val))
  235. xhci_write_64(xhci, 0, &xhci->op_regs->cmd_ring);
  236. intrs = min_t(u32, HCS_MAX_INTRS(xhci->hcs_params1),
  237. ARRAY_SIZE(xhci->run_regs->ir_set));
  238. for (i = 0; i < intrs; i++) {
  239. struct xhci_intr_reg __iomem *ir;
  240. ir = &xhci->run_regs->ir_set[i];
  241. val = xhci_read_64(xhci, &ir->erst_base);
  242. if (upper_32_bits(val))
  243. xhci_write_64(xhci, 0, &ir->erst_base);
  244. val= xhci_read_64(xhci, &ir->erst_dequeue);
  245. if (upper_32_bits(val))
  246. xhci_write_64(xhci, 0, &ir->erst_dequeue);
  247. }
  248. /* Wait for the fault to appear. It will be cleared on reset */
  249. err = xhci_handshake(&xhci->op_regs->status,
  250. STS_FATAL, STS_FATAL,
  251. XHCI_MAX_HALT_USEC);
  252. if (!err)
  253. xhci_info(xhci, "Fault detected\n");
  254. }
  255. #ifdef CONFIG_USB_PCI
  256. /*
  257. * Set up MSI
  258. */
  259. static int xhci_setup_msi(struct xhci_hcd *xhci)
  260. {
  261. int ret;
  262. /*
  263. * TODO:Check with MSI Soc for sysdev
  264. */
  265. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  266. ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
  267. if (ret < 0) {
  268. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  269. "failed to allocate MSI entry");
  270. return ret;
  271. }
  272. ret = request_irq(pdev->irq, xhci_msi_irq,
  273. 0, "xhci_hcd", xhci_to_hcd(xhci));
  274. if (ret) {
  275. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  276. "disable MSI interrupt");
  277. pci_free_irq_vectors(pdev);
  278. }
  279. return ret;
  280. }
  281. /*
  282. * Set up MSI-X
  283. */
  284. static int xhci_setup_msix(struct xhci_hcd *xhci)
  285. {
  286. int i, ret;
  287. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  288. struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
  289. /*
  290. * calculate number of msi-x vectors supported.
  291. * - HCS_MAX_INTRS: the max number of interrupts the host can handle,
  292. * with max number of interrupters based on the xhci HCSPARAMS1.
  293. * - num_online_cpus: maximum msi-x vectors per CPUs core.
  294. * Add additional 1 vector to ensure always available interrupt.
  295. */
  296. xhci->msix_count = min(num_online_cpus() + 1,
  297. HCS_MAX_INTRS(xhci->hcs_params1));
  298. ret = pci_alloc_irq_vectors(pdev, xhci->msix_count, xhci->msix_count,
  299. PCI_IRQ_MSIX);
  300. if (ret < 0) {
  301. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  302. "Failed to enable MSI-X");
  303. return ret;
  304. }
  305. for (i = 0; i < xhci->msix_count; i++) {
  306. ret = request_irq(pci_irq_vector(pdev, i), xhci_msi_irq, 0,
  307. "xhci_hcd", xhci_to_hcd(xhci));
  308. if (ret)
  309. goto disable_msix;
  310. }
  311. hcd->msix_enabled = 1;
  312. return ret;
  313. disable_msix:
  314. xhci_dbg_trace(xhci, trace_xhci_dbg_init, "disable MSI-X interrupt");
  315. while (--i >= 0)
  316. free_irq(pci_irq_vector(pdev, i), xhci_to_hcd(xhci));
  317. pci_free_irq_vectors(pdev);
  318. return ret;
  319. }
  320. /* Free any IRQs and disable MSI-X */
  321. static void xhci_cleanup_msix(struct xhci_hcd *xhci)
  322. {
  323. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  324. struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
  325. if (xhci->quirks & XHCI_PLAT)
  326. return;
  327. /* return if using legacy interrupt */
  328. if (hcd->irq > 0)
  329. return;
  330. if (hcd->msix_enabled) {
  331. int i;
  332. for (i = 0; i < xhci->msix_count; i++)
  333. free_irq(pci_irq_vector(pdev, i), xhci_to_hcd(xhci));
  334. } else {
  335. free_irq(pci_irq_vector(pdev, 0), xhci_to_hcd(xhci));
  336. }
  337. pci_free_irq_vectors(pdev);
  338. hcd->msix_enabled = 0;
  339. }
  340. static void __maybe_unused xhci_msix_sync_irqs(struct xhci_hcd *xhci)
  341. {
  342. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  343. if (hcd->msix_enabled) {
  344. struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
  345. int i;
  346. for (i = 0; i < xhci->msix_count; i++)
  347. synchronize_irq(pci_irq_vector(pdev, i));
  348. }
  349. }
  350. static int xhci_try_enable_msi(struct usb_hcd *hcd)
  351. {
  352. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  353. struct pci_dev *pdev;
  354. int ret;
  355. /* The xhci platform device has set up IRQs through usb_add_hcd. */
  356. if (xhci->quirks & XHCI_PLAT)
  357. return 0;
  358. pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  359. /*
  360. * Some Fresco Logic host controllers advertise MSI, but fail to
  361. * generate interrupts. Don't even try to enable MSI.
  362. */
  363. if (xhci->quirks & XHCI_BROKEN_MSI)
  364. goto legacy_irq;
  365. /* unregister the legacy interrupt */
  366. if (hcd->irq)
  367. free_irq(hcd->irq, hcd);
  368. hcd->irq = 0;
  369. ret = xhci_setup_msix(xhci);
  370. if (ret)
  371. /* fall back to msi*/
  372. ret = xhci_setup_msi(xhci);
  373. if (!ret) {
  374. hcd->msi_enabled = 1;
  375. return 0;
  376. }
  377. if (!pdev->irq) {
  378. xhci_err(xhci, "No msi-x/msi found and no IRQ in BIOS\n");
  379. return -EINVAL;
  380. }
  381. legacy_irq:
  382. if (!strlen(hcd->irq_descr))
  383. snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
  384. hcd->driver->description, hcd->self.busnum);
  385. /* fall back to legacy interrupt*/
  386. ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
  387. hcd->irq_descr, hcd);
  388. if (ret) {
  389. xhci_err(xhci, "request interrupt %d failed\n",
  390. pdev->irq);
  391. return ret;
  392. }
  393. hcd->irq = pdev->irq;
  394. return 0;
  395. }
  396. #else
  397. static inline int xhci_try_enable_msi(struct usb_hcd *hcd)
  398. {
  399. return 0;
  400. }
  401. static inline void xhci_cleanup_msix(struct xhci_hcd *xhci)
  402. {
  403. }
  404. static inline void xhci_msix_sync_irqs(struct xhci_hcd *xhci)
  405. {
  406. }
  407. #endif
  408. static void compliance_mode_recovery(struct timer_list *t)
  409. {
  410. struct xhci_hcd *xhci;
  411. struct usb_hcd *hcd;
  412. struct xhci_hub *rhub;
  413. u32 temp;
  414. int i;
  415. xhci = from_timer(xhci, t, comp_mode_recovery_timer);
  416. rhub = &xhci->usb3_rhub;
  417. hcd = rhub->hcd;
  418. if (!hcd)
  419. return;
  420. for (i = 0; i < rhub->num_ports; i++) {
  421. temp = readl(rhub->ports[i]->addr);
  422. if ((temp & PORT_PLS_MASK) == USB_SS_PORT_LS_COMP_MOD) {
  423. /*
  424. * Compliance Mode Detected. Letting USB Core
  425. * handle the Warm Reset
  426. */
  427. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  428. "Compliance mode detected->port %d",
  429. i + 1);
  430. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  431. "Attempting compliance mode recovery");
  432. if (hcd->state == HC_STATE_SUSPENDED)
  433. usb_hcd_resume_root_hub(hcd);
  434. usb_hcd_poll_rh_status(hcd);
  435. }
  436. }
  437. if (xhci->port_status_u0 != ((1 << rhub->num_ports) - 1))
  438. mod_timer(&xhci->comp_mode_recovery_timer,
  439. jiffies + msecs_to_jiffies(COMP_MODE_RCVRY_MSECS));
  440. }
  441. /*
  442. * Quirk to work around issue generated by the SN65LVPE502CP USB3.0 re-driver
  443. * that causes ports behind that hardware to enter compliance mode sometimes.
  444. * The quirk creates a timer that polls every 2 seconds the link state of
  445. * each host controller's port and recovers it by issuing a Warm reset
  446. * if Compliance mode is detected, otherwise the port will become "dead" (no
  447. * device connections or disconnections will be detected anymore). Becasue no
  448. * status event is generated when entering compliance mode (per xhci spec),
  449. * this quirk is needed on systems that have the failing hardware installed.
  450. */
  451. static void compliance_mode_recovery_timer_init(struct xhci_hcd *xhci)
  452. {
  453. xhci->port_status_u0 = 0;
  454. timer_setup(&xhci->comp_mode_recovery_timer, compliance_mode_recovery,
  455. 0);
  456. xhci->comp_mode_recovery_timer.expires = jiffies +
  457. msecs_to_jiffies(COMP_MODE_RCVRY_MSECS);
  458. add_timer(&xhci->comp_mode_recovery_timer);
  459. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  460. "Compliance mode recovery timer initialized");
  461. }
  462. /*
  463. * This function identifies the systems that have installed the SN65LVPE502CP
  464. * USB3.0 re-driver and that need the Compliance Mode Quirk.
  465. * Systems:
  466. * Vendor: Hewlett-Packard -> System Models: Z420, Z620 and Z820
  467. */
  468. static bool xhci_compliance_mode_recovery_timer_quirk_check(void)
  469. {
  470. const char *dmi_product_name, *dmi_sys_vendor;
  471. dmi_product_name = dmi_get_system_info(DMI_PRODUCT_NAME);
  472. dmi_sys_vendor = dmi_get_system_info(DMI_SYS_VENDOR);
  473. if (!dmi_product_name || !dmi_sys_vendor)
  474. return false;
  475. if (!(strstr(dmi_sys_vendor, "Hewlett-Packard")))
  476. return false;
  477. if (strstr(dmi_product_name, "Z420") ||
  478. strstr(dmi_product_name, "Z620") ||
  479. strstr(dmi_product_name, "Z820") ||
  480. strstr(dmi_product_name, "Z1 Workstation"))
  481. return true;
  482. return false;
  483. }
  484. static int xhci_all_ports_seen_u0(struct xhci_hcd *xhci)
  485. {
  486. return (xhci->port_status_u0 == ((1 << xhci->usb3_rhub.num_ports) - 1));
  487. }
  488. /*
  489. * Initialize memory for HCD and xHC (one-time init).
  490. *
  491. * Program the PAGESIZE register, initialize the device context array, create
  492. * device contexts (?), set up a command ring segment (or two?), create event
  493. * ring (one for now).
  494. */
  495. static int xhci_init(struct usb_hcd *hcd)
  496. {
  497. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  498. int retval;
  499. xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_init");
  500. spin_lock_init(&xhci->lock);
  501. if (xhci->hci_version == 0x95 && link_quirk) {
  502. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  503. "QUIRK: Not clearing Link TRB chain bits.");
  504. xhci->quirks |= XHCI_LINK_TRB_QUIRK;
  505. } else {
  506. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  507. "xHCI doesn't need link TRB QUIRK");
  508. }
  509. retval = xhci_mem_init(xhci, GFP_KERNEL);
  510. xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Finished xhci_init");
  511. /* Initializing Compliance Mode Recovery Data If Needed */
  512. if (xhci_compliance_mode_recovery_timer_quirk_check()) {
  513. xhci->quirks |= XHCI_COMP_MODE_QUIRK;
  514. compliance_mode_recovery_timer_init(xhci);
  515. }
  516. return retval;
  517. }
  518. /*-------------------------------------------------------------------------*/
  519. static int xhci_run_finished(struct xhci_hcd *xhci)
  520. {
  521. unsigned long flags;
  522. u32 temp;
  523. /*
  524. * Enable interrupts before starting the host (xhci 4.2 and 5.5.2).
  525. * Protect the short window before host is running with a lock
  526. */
  527. spin_lock_irqsave(&xhci->lock, flags);
  528. xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Enable interrupts");
  529. temp = readl(&xhci->op_regs->command);
  530. temp |= (CMD_EIE);
  531. writel(temp, &xhci->op_regs->command);
  532. xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Enable primary interrupter");
  533. temp = readl(&xhci->ir_set->irq_pending);
  534. writel(ER_IRQ_ENABLE(temp), &xhci->ir_set->irq_pending);
  535. if (xhci_start(xhci)) {
  536. xhci_halt(xhci);
  537. spin_unlock_irqrestore(&xhci->lock, flags);
  538. return -ENODEV;
  539. }
  540. xhci->cmd_ring_state = CMD_RING_STATE_RUNNING;
  541. if (xhci->quirks & XHCI_NEC_HOST)
  542. xhci_ring_cmd_db(xhci);
  543. spin_unlock_irqrestore(&xhci->lock, flags);
  544. return 0;
  545. }
  546. /*
  547. * Start the HC after it was halted.
  548. *
  549. * This function is called by the USB core when the HC driver is added.
  550. * Its opposite is xhci_stop().
  551. *
  552. * xhci_init() must be called once before this function can be called.
  553. * Reset the HC, enable device slot contexts, program DCBAAP, and
  554. * set command ring pointer and event ring pointer.
  555. *
  556. * Setup MSI-X vectors and enable interrupts.
  557. */
  558. int xhci_run(struct usb_hcd *hcd)
  559. {
  560. u32 temp;
  561. u64 temp_64;
  562. int ret;
  563. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  564. /* Start the xHCI host controller running only after the USB 2.0 roothub
  565. * is setup.
  566. */
  567. hcd->uses_new_polling = 1;
  568. if (!usb_hcd_is_primary_hcd(hcd))
  569. return xhci_run_finished(xhci);
  570. xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_run");
  571. ret = xhci_try_enable_msi(hcd);
  572. if (ret)
  573. return ret;
  574. temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
  575. temp_64 &= ~ERST_PTR_MASK;
  576. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  577. "ERST deq = 64'h%0lx", (long unsigned int) temp_64);
  578. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  579. "// Set the interrupt modulation register");
  580. temp = readl(&xhci->ir_set->irq_control);
  581. temp &= ~ER_IRQ_INTERVAL_MASK;
  582. temp |= (xhci->imod_interval / 250) & ER_IRQ_INTERVAL_MASK;
  583. writel(temp, &xhci->ir_set->irq_control);
  584. if (xhci->quirks & XHCI_NEC_HOST) {
  585. struct xhci_command *command;
  586. command = xhci_alloc_command(xhci, false, GFP_KERNEL);
  587. if (!command)
  588. return -ENOMEM;
  589. ret = xhci_queue_vendor_command(xhci, command, 0, 0, 0,
  590. TRB_TYPE(TRB_NEC_GET_FW));
  591. if (ret)
  592. xhci_free_command(xhci, command);
  593. }
  594. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  595. "Finished %s for main hcd", __func__);
  596. xhci_create_dbc_dev(xhci);
  597. xhci_debugfs_init(xhci);
  598. if (xhci_has_one_roothub(xhci))
  599. return xhci_run_finished(xhci);
  600. set_bit(HCD_FLAG_DEFER_RH_REGISTER, &hcd->flags);
  601. return 0;
  602. }
  603. EXPORT_SYMBOL_GPL(xhci_run);
  604. /*
  605. * Stop xHCI driver.
  606. *
  607. * This function is called by the USB core when the HC driver is removed.
  608. * Its opposite is xhci_run().
  609. *
  610. * Disable device contexts, disable IRQs, and quiesce the HC.
  611. * Reset the HC, finish any completed transactions, and cleanup memory.
  612. */
  613. static void xhci_stop(struct usb_hcd *hcd)
  614. {
  615. u32 temp;
  616. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  617. mutex_lock(&xhci->mutex);
  618. /* Only halt host and free memory after both hcds are removed */
  619. if (!usb_hcd_is_primary_hcd(hcd)) {
  620. mutex_unlock(&xhci->mutex);
  621. return;
  622. }
  623. xhci_remove_dbc_dev(xhci);
  624. spin_lock_irq(&xhci->lock);
  625. xhci->xhc_state |= XHCI_STATE_HALTED;
  626. xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;
  627. xhci_halt(xhci);
  628. xhci_reset(xhci, XHCI_RESET_SHORT_USEC);
  629. spin_unlock_irq(&xhci->lock);
  630. xhci_cleanup_msix(xhci);
  631. /* Deleting Compliance Mode Recovery Timer */
  632. if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
  633. (!(xhci_all_ports_seen_u0(xhci)))) {
  634. del_timer_sync(&xhci->comp_mode_recovery_timer);
  635. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  636. "%s: compliance mode recovery timer deleted",
  637. __func__);
  638. }
  639. if (xhci->quirks & XHCI_AMD_PLL_FIX)
  640. usb_amd_dev_put();
  641. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  642. "// Disabling event ring interrupts");
  643. temp = readl(&xhci->op_regs->status);
  644. writel((temp & ~0x1fff) | STS_EINT, &xhci->op_regs->status);
  645. temp = readl(&xhci->ir_set->irq_pending);
  646. writel(ER_IRQ_DISABLE(temp), &xhci->ir_set->irq_pending);
  647. xhci_dbg_trace(xhci, trace_xhci_dbg_init, "cleaning up memory");
  648. xhci_mem_cleanup(xhci);
  649. xhci_debugfs_exit(xhci);
  650. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  651. "xhci_stop completed - status = %x",
  652. readl(&xhci->op_regs->status));
  653. mutex_unlock(&xhci->mutex);
  654. }
  655. /*
  656. * Shutdown HC (not bus-specific)
  657. *
  658. * This is called when the machine is rebooting or halting. We assume that the
  659. * machine will be powered off, and the HC's internal state will be reset.
  660. * Don't bother to free memory.
  661. *
  662. * This will only ever be called with the main usb_hcd (the USB3 roothub).
  663. */
  664. void xhci_shutdown(struct usb_hcd *hcd)
  665. {
  666. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  667. if (xhci->quirks & XHCI_SPURIOUS_REBOOT)
  668. usb_disable_xhci_ports(to_pci_dev(hcd->self.sysdev));
  669. /* Don't poll the roothubs after shutdown. */
  670. xhci_dbg(xhci, "%s: stopping usb%d port polling.\n",
  671. __func__, hcd->self.busnum);
  672. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  673. del_timer_sync(&hcd->rh_timer);
  674. if (xhci->shared_hcd) {
  675. clear_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
  676. del_timer_sync(&xhci->shared_hcd->rh_timer);
  677. }
  678. spin_lock_irq(&xhci->lock);
  679. xhci_halt(xhci);
  680. /*
  681. * Workaround for spurious wakeps at shutdown with HSW, and for boot
  682. * firmware delay in ADL-P PCH if port are left in U3 at shutdown
  683. */
  684. if (xhci->quirks & XHCI_SPURIOUS_WAKEUP ||
  685. xhci->quirks & XHCI_RESET_TO_DEFAULT)
  686. xhci_reset(xhci, XHCI_RESET_SHORT_USEC);
  687. spin_unlock_irq(&xhci->lock);
  688. xhci_cleanup_msix(xhci);
  689. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  690. "xhci_shutdown completed - status = %x",
  691. readl(&xhci->op_regs->status));
  692. }
  693. EXPORT_SYMBOL_GPL(xhci_shutdown);
  694. #ifdef CONFIG_PM
  695. static void xhci_save_registers(struct xhci_hcd *xhci)
  696. {
  697. xhci->s3.command = readl(&xhci->op_regs->command);
  698. xhci->s3.dev_nt = readl(&xhci->op_regs->dev_notification);
  699. xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
  700. xhci->s3.config_reg = readl(&xhci->op_regs->config_reg);
  701. xhci->s3.erst_size = readl(&xhci->ir_set->erst_size);
  702. xhci->s3.erst_base = xhci_read_64(xhci, &xhci->ir_set->erst_base);
  703. xhci->s3.erst_dequeue = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
  704. xhci->s3.irq_pending = readl(&xhci->ir_set->irq_pending);
  705. xhci->s3.irq_control = readl(&xhci->ir_set->irq_control);
  706. }
  707. static void xhci_restore_registers(struct xhci_hcd *xhci)
  708. {
  709. writel(xhci->s3.command, &xhci->op_regs->command);
  710. writel(xhci->s3.dev_nt, &xhci->op_regs->dev_notification);
  711. xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr);
  712. writel(xhci->s3.config_reg, &xhci->op_regs->config_reg);
  713. writel(xhci->s3.erst_size, &xhci->ir_set->erst_size);
  714. xhci_write_64(xhci, xhci->s3.erst_base, &xhci->ir_set->erst_base);
  715. xhci_write_64(xhci, xhci->s3.erst_dequeue, &xhci->ir_set->erst_dequeue);
  716. writel(xhci->s3.irq_pending, &xhci->ir_set->irq_pending);
  717. writel(xhci->s3.irq_control, &xhci->ir_set->irq_control);
  718. }
  719. static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci)
  720. {
  721. u64 val_64;
  722. /* step 2: initialize command ring buffer */
  723. val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
  724. val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
  725. (xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
  726. xhci->cmd_ring->dequeue) &
  727. (u64) ~CMD_RING_RSVD_BITS) |
  728. xhci->cmd_ring->cycle_state;
  729. xhci_dbg_trace(xhci, trace_xhci_dbg_init,
  730. "// Setting command ring address to 0x%llx",
  731. (long unsigned long) val_64);
  732. xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
  733. }
  734. /*
  735. * The whole command ring must be cleared to zero when we suspend the host.
  736. *
  737. * The host doesn't save the command ring pointer in the suspend well, so we
  738. * need to re-program it on resume. Unfortunately, the pointer must be 64-byte
  739. * aligned, because of the reserved bits in the command ring dequeue pointer
  740. * register. Therefore, we can't just set the dequeue pointer back in the
  741. * middle of the ring (TRBs are 16-byte aligned).
  742. */
  743. static void xhci_clear_command_ring(struct xhci_hcd *xhci)
  744. {
  745. struct xhci_ring *ring;
  746. struct xhci_segment *seg;
  747. ring = xhci->cmd_ring;
  748. seg = ring->deq_seg;
  749. do {
  750. memset(seg->trbs, 0,
  751. sizeof(union xhci_trb) * (TRBS_PER_SEGMENT - 1));
  752. seg->trbs[TRBS_PER_SEGMENT - 1].link.control &=
  753. cpu_to_le32(~TRB_CYCLE);
  754. seg = seg->next;
  755. } while (seg != ring->deq_seg);
  756. /* Reset the software enqueue and dequeue pointers */
  757. ring->deq_seg = ring->first_seg;
  758. ring->dequeue = ring->first_seg->trbs;
  759. ring->enq_seg = ring->deq_seg;
  760. ring->enqueue = ring->dequeue;
  761. ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1;
  762. /*
  763. * Ring is now zeroed, so the HW should look for change of ownership
  764. * when the cycle bit is set to 1.
  765. */
  766. ring->cycle_state = 1;
  767. /*
  768. * Reset the hardware dequeue pointer.
  769. * Yes, this will need to be re-written after resume, but we're paranoid
  770. * and want to make sure the hardware doesn't access bogus memory
  771. * because, say, the BIOS or an SMI started the host without changing
  772. * the command ring pointers.
  773. */
  774. xhci_set_cmd_ring_deq(xhci);
  775. }
  776. /*
  777. * Disable port wake bits if do_wakeup is not set.
  778. *
  779. * Also clear a possible internal port wake state left hanging for ports that
  780. * detected termination but never successfully enumerated (trained to 0U).
  781. * Internal wake causes immediate xHCI wake after suspend. PORT_CSC write done
  782. * at enumeration clears this wake, force one here as well for unconnected ports
  783. */
  784. static void xhci_disable_hub_port_wake(struct xhci_hcd *xhci,
  785. struct xhci_hub *rhub,
  786. bool do_wakeup)
  787. {
  788. unsigned long flags;
  789. u32 t1, t2, portsc;
  790. int i;
  791. spin_lock_irqsave(&xhci->lock, flags);
  792. for (i = 0; i < rhub->num_ports; i++) {
  793. portsc = readl(rhub->ports[i]->addr);
  794. t1 = xhci_port_state_to_neutral(portsc);
  795. t2 = t1;
  796. /* clear wake bits if do_wake is not set */
  797. if (!do_wakeup)
  798. t2 &= ~PORT_WAKE_BITS;
  799. /* Don't touch csc bit if connected or connect change is set */
  800. if (!(portsc & (PORT_CSC | PORT_CONNECT)))
  801. t2 |= PORT_CSC;
  802. if (t1 != t2) {
  803. writel(t2, rhub->ports[i]->addr);
  804. xhci_dbg(xhci, "config port %d-%d wake bits, portsc: 0x%x, write: 0x%x\n",
  805. rhub->hcd->self.busnum, i + 1, portsc, t2);
  806. }
  807. }
  808. spin_unlock_irqrestore(&xhci->lock, flags);
  809. }
  810. static bool xhci_pending_portevent(struct xhci_hcd *xhci)
  811. {
  812. struct xhci_port **ports;
  813. int port_index;
  814. u32 status;
  815. u32 portsc;
  816. status = readl(&xhci->op_regs->status);
  817. if (status & STS_EINT)
  818. return true;
  819. /*
  820. * Checking STS_EINT is not enough as there is a lag between a change
  821. * bit being set and the Port Status Change Event that it generated
  822. * being written to the Event Ring. See note in xhci 1.1 section 4.19.2.
  823. */
  824. port_index = xhci->usb2_rhub.num_ports;
  825. ports = xhci->usb2_rhub.ports;
  826. while (port_index--) {
  827. portsc = readl(ports[port_index]->addr);
  828. if (portsc & PORT_CHANGE_MASK ||
  829. (portsc & PORT_PLS_MASK) == XDEV_RESUME)
  830. return true;
  831. }
  832. port_index = xhci->usb3_rhub.num_ports;
  833. ports = xhci->usb3_rhub.ports;
  834. while (port_index--) {
  835. portsc = readl(ports[port_index]->addr);
  836. if (portsc & PORT_CHANGE_MASK ||
  837. (portsc & PORT_PLS_MASK) == XDEV_RESUME)
  838. return true;
  839. }
  840. return false;
  841. }
  842. /*
  843. * Stop HC (not bus-specific)
  844. *
  845. * This is called when the machine transition into S3/S4 mode.
  846. *
  847. */
  848. int xhci_suspend(struct xhci_hcd *xhci, bool do_wakeup)
  849. {
  850. int rc = 0;
  851. unsigned int delay = XHCI_MAX_HALT_USEC * 2;
  852. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  853. u32 command;
  854. u32 res;
  855. if (!hcd->state)
  856. return 0;
  857. if (hcd->state != HC_STATE_SUSPENDED ||
  858. (xhci->shared_hcd && xhci->shared_hcd->state != HC_STATE_SUSPENDED))
  859. return -EINVAL;
  860. /* Clear root port wake on bits if wakeup not allowed. */
  861. xhci_disable_hub_port_wake(xhci, &xhci->usb3_rhub, do_wakeup);
  862. xhci_disable_hub_port_wake(xhci, &xhci->usb2_rhub, do_wakeup);
  863. if (!HCD_HW_ACCESSIBLE(hcd))
  864. return 0;
  865. xhci_dbc_suspend(xhci);
  866. /* Don't poll the roothubs on bus suspend. */
  867. xhci_dbg(xhci, "%s: stopping usb%d port polling.\n",
  868. __func__, hcd->self.busnum);
  869. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  870. del_timer_sync(&hcd->rh_timer);
  871. if (xhci->shared_hcd) {
  872. clear_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
  873. del_timer_sync(&xhci->shared_hcd->rh_timer);
  874. }
  875. if (xhci->quirks & XHCI_SUSPEND_DELAY)
  876. usleep_range(1000, 1500);
  877. spin_lock_irq(&xhci->lock);
  878. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  879. if (xhci->shared_hcd)
  880. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
  881. /* step 1: stop endpoint */
  882. /* skipped assuming that port suspend has done */
  883. /* step 2: clear Run/Stop bit */
  884. command = readl(&xhci->op_regs->command);
  885. command &= ~CMD_RUN;
  886. writel(command, &xhci->op_regs->command);
  887. /* Some chips from Fresco Logic need an extraordinary delay */
  888. delay *= (xhci->quirks & XHCI_SLOW_SUSPEND) ? 10 : 1;
  889. if (xhci_handshake(&xhci->op_regs->status,
  890. STS_HALT, STS_HALT, delay)) {
  891. xhci_warn(xhci, "WARN: xHC CMD_RUN timeout\n");
  892. spin_unlock_irq(&xhci->lock);
  893. return -ETIMEDOUT;
  894. }
  895. xhci_clear_command_ring(xhci);
  896. /* step 3: save registers */
  897. xhci_save_registers(xhci);
  898. /* step 4: set CSS flag */
  899. command = readl(&xhci->op_regs->command);
  900. command |= CMD_CSS;
  901. writel(command, &xhci->op_regs->command);
  902. xhci->broken_suspend = 0;
  903. if (xhci_handshake(&xhci->op_regs->status,
  904. STS_SAVE, 0, 20 * 1000)) {
  905. /*
  906. * AMD SNPS xHC 3.0 occasionally does not clear the
  907. * SSS bit of USBSTS and when driver tries to poll
  908. * to see if the xHC clears BIT(8) which never happens
  909. * and driver assumes that controller is not responding
  910. * and times out. To workaround this, its good to check
  911. * if SRE and HCE bits are not set (as per xhci
  912. * Section 5.4.2) and bypass the timeout.
  913. */
  914. res = readl(&xhci->op_regs->status);
  915. if ((xhci->quirks & XHCI_SNPS_BROKEN_SUSPEND) &&
  916. (((res & STS_SRE) == 0) &&
  917. ((res & STS_HCE) == 0))) {
  918. xhci->broken_suspend = 1;
  919. } else {
  920. xhci_warn(xhci, "WARN: xHC save state timeout\n");
  921. spin_unlock_irq(&xhci->lock);
  922. return -ETIMEDOUT;
  923. }
  924. }
  925. spin_unlock_irq(&xhci->lock);
  926. /*
  927. * Deleting Compliance Mode Recovery Timer because the xHCI Host
  928. * is about to be suspended.
  929. */
  930. if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
  931. (!(xhci_all_ports_seen_u0(xhci)))) {
  932. del_timer_sync(&xhci->comp_mode_recovery_timer);
  933. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  934. "%s: compliance mode recovery timer deleted",
  935. __func__);
  936. }
  937. /* step 5: remove core well power */
  938. /* synchronize irq when using MSI-X */
  939. xhci_msix_sync_irqs(xhci);
  940. return rc;
  941. }
  942. EXPORT_SYMBOL_GPL(xhci_suspend);
  943. /*
  944. * start xHC (not bus-specific)
  945. *
  946. * This is called when the machine transition from S3/S4 mode.
  947. *
  948. */
  949. int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
  950. {
  951. u32 command, temp = 0;
  952. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  953. int retval = 0;
  954. bool comp_timer_running = false;
  955. bool pending_portevent = false;
  956. bool suspended_usb3_devs = false;
  957. bool reinit_xhc = false;
  958. if (!hcd->state)
  959. return 0;
  960. /* Wait a bit if either of the roothubs need to settle from the
  961. * transition into bus suspend.
  962. */
  963. if (time_before(jiffies, xhci->usb2_rhub.bus_state.next_statechange) ||
  964. time_before(jiffies, xhci->usb3_rhub.bus_state.next_statechange))
  965. msleep(100);
  966. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  967. if (xhci->shared_hcd)
  968. set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
  969. spin_lock_irq(&xhci->lock);
  970. if (hibernated || xhci->quirks & XHCI_RESET_ON_RESUME || xhci->broken_suspend)
  971. reinit_xhc = true;
  972. if (!reinit_xhc) {
  973. /*
  974. * Some controllers might lose power during suspend, so wait
  975. * for controller not ready bit to clear, just as in xHC init.
  976. */
  977. retval = xhci_handshake(&xhci->op_regs->status,
  978. STS_CNR, 0, 10 * 1000 * 1000);
  979. if (retval) {
  980. xhci_warn(xhci, "Controller not ready at resume %d\n",
  981. retval);
  982. spin_unlock_irq(&xhci->lock);
  983. return retval;
  984. }
  985. /* step 1: restore register */
  986. xhci_restore_registers(xhci);
  987. /* step 2: initialize command ring buffer */
  988. xhci_set_cmd_ring_deq(xhci);
  989. /* step 3: restore state and start state*/
  990. /* step 3: set CRS flag */
  991. command = readl(&xhci->op_regs->command);
  992. command |= CMD_CRS;
  993. writel(command, &xhci->op_regs->command);
  994. /*
  995. * Some controllers take up to 55+ ms to complete the controller
  996. * restore so setting the timeout to 100ms. Xhci specification
  997. * doesn't mention any timeout value.
  998. */
  999. if (xhci_handshake(&xhci->op_regs->status,
  1000. STS_RESTORE, 0, 100 * 1000)) {
  1001. xhci_warn(xhci, "WARN: xHC restore state timeout\n");
  1002. spin_unlock_irq(&xhci->lock);
  1003. return -ETIMEDOUT;
  1004. }
  1005. }
  1006. temp = readl(&xhci->op_regs->status);
  1007. /* re-initialize the HC on Restore Error, or Host Controller Error */
  1008. if ((temp & (STS_SRE | STS_HCE)) &&
  1009. !(xhci->xhc_state & XHCI_STATE_REMOVING)) {
  1010. reinit_xhc = true;
  1011. if (!xhci->broken_suspend)
  1012. xhci_warn(xhci, "xHC error in resume, USBSTS 0x%x, Reinit\n", temp);
  1013. }
  1014. if (reinit_xhc) {
  1015. if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
  1016. !(xhci_all_ports_seen_u0(xhci))) {
  1017. del_timer_sync(&xhci->comp_mode_recovery_timer);
  1018. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  1019. "Compliance Mode Recovery Timer deleted!");
  1020. }
  1021. /* Let the USB core know _both_ roothubs lost power. */
  1022. usb_root_hub_lost_power(xhci->main_hcd->self.root_hub);
  1023. if (xhci->shared_hcd)
  1024. usb_root_hub_lost_power(xhci->shared_hcd->self.root_hub);
  1025. xhci_dbg(xhci, "Stop HCD\n");
  1026. xhci_halt(xhci);
  1027. xhci_zero_64b_regs(xhci);
  1028. retval = xhci_reset(xhci, XHCI_RESET_LONG_USEC);
  1029. spin_unlock_irq(&xhci->lock);
  1030. if (retval)
  1031. return retval;
  1032. xhci_cleanup_msix(xhci);
  1033. xhci_dbg(xhci, "// Disabling event ring interrupts\n");
  1034. temp = readl(&xhci->op_regs->status);
  1035. writel((temp & ~0x1fff) | STS_EINT, &xhci->op_regs->status);
  1036. temp = readl(&xhci->ir_set->irq_pending);
  1037. writel(ER_IRQ_DISABLE(temp), &xhci->ir_set->irq_pending);
  1038. xhci_dbg(xhci, "cleaning up memory\n");
  1039. xhci_mem_cleanup(xhci);
  1040. xhci_debugfs_exit(xhci);
  1041. xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
  1042. readl(&xhci->op_regs->status));
  1043. /* USB core calls the PCI reinit and start functions twice:
  1044. * first with the primary HCD, and then with the secondary HCD.
  1045. * If we don't do the same, the host will never be started.
  1046. */
  1047. xhci_dbg(xhci, "Initialize the xhci_hcd\n");
  1048. retval = xhci_init(hcd);
  1049. if (retval)
  1050. return retval;
  1051. comp_timer_running = true;
  1052. xhci_dbg(xhci, "Start the primary HCD\n");
  1053. retval = xhci_run(hcd);
  1054. if (!retval && xhci->shared_hcd) {
  1055. xhci_dbg(xhci, "Start the secondary HCD\n");
  1056. retval = xhci_run(xhci->shared_hcd);
  1057. }
  1058. hcd->state = HC_STATE_SUSPENDED;
  1059. if (xhci->shared_hcd)
  1060. xhci->shared_hcd->state = HC_STATE_SUSPENDED;
  1061. goto done;
  1062. }
  1063. /* step 4: set Run/Stop bit */
  1064. command = readl(&xhci->op_regs->command);
  1065. command |= CMD_RUN;
  1066. writel(command, &xhci->op_regs->command);
  1067. xhci_handshake(&xhci->op_regs->status, STS_HALT,
  1068. 0, 250 * 1000);
  1069. /* step 5: walk topology and initialize portsc,
  1070. * portpmsc and portli
  1071. */
  1072. /* this is done in bus_resume */
  1073. /* step 6: restart each of the previously
  1074. * Running endpoints by ringing their doorbells
  1075. */
  1076. spin_unlock_irq(&xhci->lock);
  1077. xhci_dbc_resume(xhci);
  1078. done:
  1079. if (retval == 0) {
  1080. /*
  1081. * Resume roothubs only if there are pending events.
  1082. * USB 3 devices resend U3 LFPS wake after a 100ms delay if
  1083. * the first wake signalling failed, give it that chance if
  1084. * there are suspended USB 3 devices.
  1085. */
  1086. if (xhci->usb3_rhub.bus_state.suspended_ports ||
  1087. xhci->usb3_rhub.bus_state.bus_suspended)
  1088. suspended_usb3_devs = true;
  1089. pending_portevent = xhci_pending_portevent(xhci);
  1090. if (suspended_usb3_devs && !pending_portevent) {
  1091. msleep(120);
  1092. pending_portevent = xhci_pending_portevent(xhci);
  1093. }
  1094. if (pending_portevent) {
  1095. if (xhci->shared_hcd)
  1096. usb_hcd_resume_root_hub(xhci->shared_hcd);
  1097. usb_hcd_resume_root_hub(hcd);
  1098. }
  1099. }
  1100. /*
  1101. * If system is subject to the Quirk, Compliance Mode Timer needs to
  1102. * be re-initialized Always after a system resume. Ports are subject
  1103. * to suffer the Compliance Mode issue again. It doesn't matter if
  1104. * ports have entered previously to U0 before system's suspension.
  1105. */
  1106. if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && !comp_timer_running)
  1107. compliance_mode_recovery_timer_init(xhci);
  1108. if (xhci->quirks & XHCI_ASMEDIA_MODIFY_FLOWCONTROL)
  1109. usb_asmedia_modifyflowcontrol(to_pci_dev(hcd->self.controller));
  1110. /* Re-enable port polling. */
  1111. xhci_dbg(xhci, "%s: starting usb%d port polling.\n",
  1112. __func__, hcd->self.busnum);
  1113. if (xhci->shared_hcd) {
  1114. set_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
  1115. usb_hcd_poll_rh_status(xhci->shared_hcd);
  1116. }
  1117. set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  1118. usb_hcd_poll_rh_status(hcd);
  1119. return retval;
  1120. }
  1121. EXPORT_SYMBOL_GPL(xhci_resume);
  1122. #endif /* CONFIG_PM */
  1123. /*-------------------------------------------------------------------------*/
  1124. static int xhci_map_temp_buffer(struct usb_hcd *hcd, struct urb *urb)
  1125. {
  1126. void *temp;
  1127. int ret = 0;
  1128. unsigned int buf_len;
  1129. enum dma_data_direction dir;
  1130. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  1131. buf_len = urb->transfer_buffer_length;
  1132. temp = kzalloc_node(buf_len, GFP_ATOMIC,
  1133. dev_to_node(hcd->self.sysdev));
  1134. if (usb_urb_dir_out(urb))
  1135. sg_pcopy_to_buffer(urb->sg, urb->num_sgs,
  1136. temp, buf_len, 0);
  1137. urb->transfer_buffer = temp;
  1138. urb->transfer_dma = dma_map_single(hcd->self.sysdev,
  1139. urb->transfer_buffer,
  1140. urb->transfer_buffer_length,
  1141. dir);
  1142. if (dma_mapping_error(hcd->self.sysdev,
  1143. urb->transfer_dma)) {
  1144. ret = -EAGAIN;
  1145. kfree(temp);
  1146. } else {
  1147. urb->transfer_flags |= URB_DMA_MAP_SINGLE;
  1148. }
  1149. return ret;
  1150. }
  1151. static bool xhci_urb_temp_buffer_required(struct usb_hcd *hcd,
  1152. struct urb *urb)
  1153. {
  1154. bool ret = false;
  1155. unsigned int i;
  1156. unsigned int len = 0;
  1157. unsigned int trb_size;
  1158. unsigned int max_pkt;
  1159. struct scatterlist *sg;
  1160. struct scatterlist *tail_sg;
  1161. tail_sg = urb->sg;
  1162. max_pkt = usb_endpoint_maxp(&urb->ep->desc);
  1163. if (!urb->num_sgs)
  1164. return ret;
  1165. if (urb->dev->speed >= USB_SPEED_SUPER)
  1166. trb_size = TRB_CACHE_SIZE_SS;
  1167. else
  1168. trb_size = TRB_CACHE_SIZE_HS;
  1169. if (urb->transfer_buffer_length != 0 &&
  1170. !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
  1171. for_each_sg(urb->sg, sg, urb->num_sgs, i) {
  1172. len = len + sg->length;
  1173. if (i > trb_size - 2) {
  1174. len = len - tail_sg->length;
  1175. if (len < max_pkt) {
  1176. ret = true;
  1177. break;
  1178. }
  1179. tail_sg = sg_next(tail_sg);
  1180. }
  1181. }
  1182. }
  1183. return ret;
  1184. }
  1185. static void xhci_unmap_temp_buf(struct usb_hcd *hcd, struct urb *urb)
  1186. {
  1187. unsigned int len;
  1188. unsigned int buf_len;
  1189. enum dma_data_direction dir;
  1190. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  1191. buf_len = urb->transfer_buffer_length;
  1192. if (IS_ENABLED(CONFIG_HAS_DMA) &&
  1193. (urb->transfer_flags & URB_DMA_MAP_SINGLE))
  1194. dma_unmap_single(hcd->self.sysdev,
  1195. urb->transfer_dma,
  1196. urb->transfer_buffer_length,
  1197. dir);
  1198. if (usb_urb_dir_in(urb)) {
  1199. len = sg_pcopy_from_buffer(urb->sg, urb->num_sgs,
  1200. urb->transfer_buffer,
  1201. buf_len,
  1202. 0);
  1203. if (len != buf_len) {
  1204. xhci_dbg(hcd_to_xhci(hcd),
  1205. "Copy from tmp buf to urb sg list failed\n");
  1206. urb->actual_length = len;
  1207. }
  1208. }
  1209. urb->transfer_flags &= ~URB_DMA_MAP_SINGLE;
  1210. kfree(urb->transfer_buffer);
  1211. urb->transfer_buffer = NULL;
  1212. }
  1213. /*
  1214. * Bypass the DMA mapping if URB is suitable for Immediate Transfer (IDT),
  1215. * we'll copy the actual data into the TRB address register. This is limited to
  1216. * transfers up to 8 bytes on output endpoints of any kind with wMaxPacketSize
  1217. * >= 8 bytes. If suitable for IDT only one Transfer TRB per TD is allowed.
  1218. */
  1219. static int xhci_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  1220. gfp_t mem_flags)
  1221. {
  1222. struct xhci_hcd *xhci;
  1223. xhci = hcd_to_xhci(hcd);
  1224. if (xhci_urb_suitable_for_idt(urb))
  1225. return 0;
  1226. if (xhci->quirks & XHCI_SG_TRB_CACHE_SIZE_QUIRK) {
  1227. if (xhci_urb_temp_buffer_required(hcd, urb))
  1228. return xhci_map_temp_buffer(hcd, urb);
  1229. }
  1230. return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
  1231. }
  1232. static void xhci_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  1233. {
  1234. struct xhci_hcd *xhci;
  1235. bool unmap_temp_buf = false;
  1236. xhci = hcd_to_xhci(hcd);
  1237. if (urb->num_sgs && (urb->transfer_flags & URB_DMA_MAP_SINGLE))
  1238. unmap_temp_buf = true;
  1239. if ((xhci->quirks & XHCI_SG_TRB_CACHE_SIZE_QUIRK) && unmap_temp_buf)
  1240. xhci_unmap_temp_buf(hcd, urb);
  1241. else
  1242. usb_hcd_unmap_urb_for_dma(hcd, urb);
  1243. }
  1244. /**
  1245. * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
  1246. * HCDs. Find the index for an endpoint given its descriptor. Use the return
  1247. * value to right shift 1 for the bitmask.
  1248. *
  1249. * Index = (epnum * 2) + direction - 1,
  1250. * where direction = 0 for OUT, 1 for IN.
  1251. * For control endpoints, the IN index is used (OUT index is unused), so
  1252. * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
  1253. */
  1254. unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
  1255. {
  1256. unsigned int index;
  1257. if (usb_endpoint_xfer_control(desc))
  1258. index = (unsigned int) (usb_endpoint_num(desc)*2);
  1259. else
  1260. index = (unsigned int) (usb_endpoint_num(desc)*2) +
  1261. (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
  1262. return index;
  1263. }
  1264. EXPORT_SYMBOL_GPL(xhci_get_endpoint_index);
  1265. /* The reverse operation to xhci_get_endpoint_index. Calculate the USB endpoint
  1266. * address from the XHCI endpoint index.
  1267. */
  1268. static unsigned int xhci_get_endpoint_address(unsigned int ep_index)
  1269. {
  1270. unsigned int number = DIV_ROUND_UP(ep_index, 2);
  1271. unsigned int direction = ep_index % 2 ? USB_DIR_OUT : USB_DIR_IN;
  1272. return direction | number;
  1273. }
  1274. /* Find the flag for this endpoint (for use in the control context). Use the
  1275. * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
  1276. * bit 1, etc.
  1277. */
  1278. static unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
  1279. {
  1280. return 1 << (xhci_get_endpoint_index(desc) + 1);
  1281. }
  1282. /* Compute the last valid endpoint context index. Basically, this is the
  1283. * endpoint index plus one. For slot contexts with more than valid endpoint,
  1284. * we find the most significant bit set in the added contexts flags.
  1285. * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
  1286. * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
  1287. */
  1288. unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
  1289. {
  1290. return fls(added_ctxs) - 1;
  1291. }
  1292. /* Returns 1 if the arguments are OK;
  1293. * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
  1294. */
  1295. static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
  1296. struct usb_host_endpoint *ep, int check_ep, bool check_virt_dev,
  1297. const char *func) {
  1298. struct xhci_hcd *xhci;
  1299. struct xhci_virt_device *virt_dev;
  1300. if (!hcd || (check_ep && !ep) || !udev) {
  1301. pr_debug("xHCI %s called with invalid args\n", func);
  1302. return -EINVAL;
  1303. }
  1304. if (!udev->parent) {
  1305. pr_debug("xHCI %s called for root hub\n", func);
  1306. return 0;
  1307. }
  1308. xhci = hcd_to_xhci(hcd);
  1309. if (check_virt_dev) {
  1310. if (!udev->slot_id || !xhci->devs[udev->slot_id]) {
  1311. xhci_dbg(xhci, "xHCI %s called with unaddressed device\n",
  1312. func);
  1313. return -EINVAL;
  1314. }
  1315. virt_dev = xhci->devs[udev->slot_id];
  1316. if (virt_dev->udev != udev) {
  1317. xhci_dbg(xhci, "xHCI %s called with udev and "
  1318. "virt_dev does not match\n", func);
  1319. return -EINVAL;
  1320. }
  1321. }
  1322. if (xhci->xhc_state & XHCI_STATE_HALTED)
  1323. return -ENODEV;
  1324. return 1;
  1325. }
  1326. static int xhci_configure_endpoint(struct xhci_hcd *xhci,
  1327. struct usb_device *udev, struct xhci_command *command,
  1328. bool ctx_change, bool must_succeed);
  1329. /*
  1330. * Full speed devices may have a max packet size greater than 8 bytes, but the
  1331. * USB core doesn't know that until it reads the first 8 bytes of the
  1332. * descriptor. If the usb_device's max packet size changes after that point,
  1333. * we need to issue an evaluate context command and wait on it.
  1334. */
  1335. static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
  1336. unsigned int ep_index, struct urb *urb, gfp_t mem_flags)
  1337. {
  1338. struct xhci_container_ctx *out_ctx;
  1339. struct xhci_input_control_ctx *ctrl_ctx;
  1340. struct xhci_ep_ctx *ep_ctx;
  1341. struct xhci_command *command;
  1342. int max_packet_size;
  1343. int hw_max_packet_size;
  1344. int ret = 0;
  1345. out_ctx = xhci->devs[slot_id]->out_ctx;
  1346. ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
  1347. hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
  1348. max_packet_size = usb_endpoint_maxp(&urb->dev->ep0.desc);
  1349. if (hw_max_packet_size != max_packet_size) {
  1350. xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
  1351. "Max Packet Size for ep 0 changed.");
  1352. xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
  1353. "Max packet size in usb_device = %d",
  1354. max_packet_size);
  1355. xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
  1356. "Max packet size in xHCI HW = %d",
  1357. hw_max_packet_size);
  1358. xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
  1359. "Issuing evaluate context command.");
  1360. /* Set up the input context flags for the command */
  1361. /* FIXME: This won't work if a non-default control endpoint
  1362. * changes max packet sizes.
  1363. */
  1364. command = xhci_alloc_command(xhci, true, mem_flags);
  1365. if (!command)
  1366. return -ENOMEM;
  1367. command->in_ctx = xhci->devs[slot_id]->in_ctx;
  1368. ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
  1369. if (!ctrl_ctx) {
  1370. xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
  1371. __func__);
  1372. ret = -ENOMEM;
  1373. goto command_cleanup;
  1374. }
  1375. /* Set up the modified control endpoint 0 */
  1376. xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
  1377. xhci->devs[slot_id]->out_ctx, ep_index);
  1378. ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
  1379. ep_ctx->ep_info &= cpu_to_le32(~EP_STATE_MASK);/* must clear */
  1380. ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK);
  1381. ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
  1382. ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
  1383. ctrl_ctx->drop_flags = 0;
  1384. ret = xhci_configure_endpoint(xhci, urb->dev, command,
  1385. true, false);
  1386. /* Clean up the input context for later use by bandwidth
  1387. * functions.
  1388. */
  1389. ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
  1390. command_cleanup:
  1391. kfree(command->completion);
  1392. kfree(command);
  1393. }
  1394. return ret;
  1395. }
  1396. /*
  1397. * non-error returns are a promise to giveback() the urb later
  1398. * we drop ownership so next owner (or urb unlink) can get it
  1399. */
  1400. static int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
  1401. {
  1402. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1403. unsigned long flags;
  1404. int ret = 0;
  1405. unsigned int slot_id, ep_index;
  1406. unsigned int *ep_state;
  1407. struct urb_priv *urb_priv;
  1408. int num_tds;
  1409. if (!urb)
  1410. return -EINVAL;
  1411. ret = xhci_check_args(hcd, urb->dev, urb->ep,
  1412. true, true, __func__);
  1413. if (ret <= 0)
  1414. return ret ? ret : -EINVAL;
  1415. slot_id = urb->dev->slot_id;
  1416. ep_index = xhci_get_endpoint_index(&urb->ep->desc);
  1417. ep_state = &xhci->devs[slot_id]->eps[ep_index].ep_state;
  1418. if (!HCD_HW_ACCESSIBLE(hcd))
  1419. return -ESHUTDOWN;
  1420. if (xhci->devs[slot_id]->flags & VDEV_PORT_ERROR) {
  1421. xhci_dbg(xhci, "Can't queue urb, port error, link inactive\n");
  1422. return -ENODEV;
  1423. }
  1424. if (xhci_vendor_usb_offload_skip_urb(xhci, urb)) {
  1425. xhci_dbg(xhci, "skip urb for usb offload\n");
  1426. return -EOPNOTSUPP;
  1427. }
  1428. if (usb_endpoint_xfer_isoc(&urb->ep->desc))
  1429. num_tds = urb->number_of_packets;
  1430. else if (usb_endpoint_is_bulk_out(&urb->ep->desc) &&
  1431. urb->transfer_buffer_length > 0 &&
  1432. urb->transfer_flags & URB_ZERO_PACKET &&
  1433. !(urb->transfer_buffer_length % usb_endpoint_maxp(&urb->ep->desc)))
  1434. num_tds = 2;
  1435. else
  1436. num_tds = 1;
  1437. urb_priv = kzalloc(struct_size(urb_priv, td, num_tds), mem_flags);
  1438. if (!urb_priv)
  1439. return -ENOMEM;
  1440. urb_priv->num_tds = num_tds;
  1441. urb_priv->num_tds_done = 0;
  1442. urb->hcpriv = urb_priv;
  1443. trace_xhci_urb_enqueue(urb);
  1444. if (usb_endpoint_xfer_control(&urb->ep->desc)) {
  1445. /* Check to see if the max packet size for the default control
  1446. * endpoint changed during FS device enumeration
  1447. */
  1448. if (urb->dev->speed == USB_SPEED_FULL) {
  1449. ret = xhci_check_maxpacket(xhci, slot_id,
  1450. ep_index, urb, mem_flags);
  1451. if (ret < 0) {
  1452. xhci_urb_free_priv(urb_priv);
  1453. urb->hcpriv = NULL;
  1454. return ret;
  1455. }
  1456. }
  1457. }
  1458. spin_lock_irqsave(&xhci->lock, flags);
  1459. if (xhci->xhc_state & XHCI_STATE_DYING) {
  1460. xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for non-responsive xHCI host.\n",
  1461. urb->ep->desc.bEndpointAddress, urb);
  1462. ret = -ESHUTDOWN;
  1463. goto free_priv;
  1464. }
  1465. if (*ep_state & (EP_GETTING_STREAMS | EP_GETTING_NO_STREAMS)) {
  1466. xhci_warn(xhci, "WARN: Can't enqueue URB, ep in streams transition state %x\n",
  1467. *ep_state);
  1468. ret = -EINVAL;
  1469. goto free_priv;
  1470. }
  1471. if (*ep_state & EP_SOFT_CLEAR_TOGGLE) {
  1472. xhci_warn(xhci, "Can't enqueue URB while manually clearing toggle\n");
  1473. ret = -EINVAL;
  1474. goto free_priv;
  1475. }
  1476. switch (usb_endpoint_type(&urb->ep->desc)) {
  1477. case USB_ENDPOINT_XFER_CONTROL:
  1478. ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
  1479. slot_id, ep_index);
  1480. break;
  1481. case USB_ENDPOINT_XFER_BULK:
  1482. ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
  1483. slot_id, ep_index);
  1484. break;
  1485. case USB_ENDPOINT_XFER_INT:
  1486. ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
  1487. slot_id, ep_index);
  1488. break;
  1489. case USB_ENDPOINT_XFER_ISOC:
  1490. ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb,
  1491. slot_id, ep_index);
  1492. }
  1493. if (ret) {
  1494. free_priv:
  1495. xhci_urb_free_priv(urb_priv);
  1496. urb->hcpriv = NULL;
  1497. }
  1498. spin_unlock_irqrestore(&xhci->lock, flags);
  1499. return ret;
  1500. }
  1501. /*
  1502. * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
  1503. * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
  1504. * should pick up where it left off in the TD, unless a Set Transfer Ring
  1505. * Dequeue Pointer is issued.
  1506. *
  1507. * The TRBs that make up the buffers for the canceled URB will be "removed" from
  1508. * the ring. Since the ring is a contiguous structure, they can't be physically
  1509. * removed. Instead, there are two options:
  1510. *
  1511. * 1) If the HC is in the middle of processing the URB to be canceled, we
  1512. * simply move the ring's dequeue pointer past those TRBs using the Set
  1513. * Transfer Ring Dequeue Pointer command. This will be the common case,
  1514. * when drivers timeout on the last submitted URB and attempt to cancel.
  1515. *
  1516. * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
  1517. * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
  1518. * HC will need to invalidate the any TRBs it has cached after the stop
  1519. * endpoint command, as noted in the xHCI 0.95 errata.
  1520. *
  1521. * 3) The TD may have completed by the time the Stop Endpoint Command
  1522. * completes, so software needs to handle that case too.
  1523. *
  1524. * This function should protect against the TD enqueueing code ringing the
  1525. * doorbell while this code is waiting for a Stop Endpoint command to complete.
  1526. * It also needs to account for multiple cancellations on happening at the same
  1527. * time for the same endpoint.
  1528. *
  1529. * Note that this function can be called in any context, or so says
  1530. * usb_hcd_unlink_urb()
  1531. */
  1532. static int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  1533. {
  1534. unsigned long flags;
  1535. int ret, i;
  1536. u32 temp;
  1537. struct xhci_hcd *xhci;
  1538. struct urb_priv *urb_priv;
  1539. struct xhci_td *td;
  1540. unsigned int ep_index;
  1541. struct xhci_ring *ep_ring;
  1542. struct xhci_virt_ep *ep;
  1543. struct xhci_command *command;
  1544. struct xhci_virt_device *vdev;
  1545. xhci = hcd_to_xhci(hcd);
  1546. spin_lock_irqsave(&xhci->lock, flags);
  1547. trace_xhci_urb_dequeue(urb);
  1548. /* Make sure the URB hasn't completed or been unlinked already */
  1549. ret = usb_hcd_check_unlink_urb(hcd, urb, status);
  1550. if (ret)
  1551. goto done;
  1552. /* give back URB now if we can't queue it for cancel */
  1553. vdev = xhci->devs[urb->dev->slot_id];
  1554. urb_priv = urb->hcpriv;
  1555. if (!vdev || !urb_priv)
  1556. goto err_giveback;
  1557. ep_index = xhci_get_endpoint_index(&urb->ep->desc);
  1558. ep = &vdev->eps[ep_index];
  1559. ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
  1560. if (!ep || !ep_ring)
  1561. goto err_giveback;
  1562. /* If xHC is dead take it down and return ALL URBs in xhci_hc_died() */
  1563. temp = readl(&xhci->op_regs->status);
  1564. if (temp == ~(u32)0 || xhci->xhc_state & XHCI_STATE_DYING) {
  1565. xhci_hc_died(xhci);
  1566. goto done;
  1567. }
  1568. /*
  1569. * check ring is not re-allocated since URB was enqueued. If it is, then
  1570. * make sure none of the ring related pointers in this URB private data
  1571. * are touched, such as td_list, otherwise we overwrite freed data
  1572. */
  1573. if (!td_on_ring(&urb_priv->td[0], ep_ring)) {
  1574. xhci_err(xhci, "Canceled URB td not found on endpoint ring");
  1575. for (i = urb_priv->num_tds_done; i < urb_priv->num_tds; i++) {
  1576. td = &urb_priv->td[i];
  1577. if (!list_empty(&td->cancelled_td_list))
  1578. list_del_init(&td->cancelled_td_list);
  1579. }
  1580. goto err_giveback;
  1581. }
  1582. if (xhci->xhc_state & XHCI_STATE_HALTED) {
  1583. xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
  1584. "HC halted, freeing TD manually.");
  1585. for (i = urb_priv->num_tds_done;
  1586. i < urb_priv->num_tds;
  1587. i++) {
  1588. td = &urb_priv->td[i];
  1589. if (!list_empty(&td->td_list))
  1590. list_del_init(&td->td_list);
  1591. if (!list_empty(&td->cancelled_td_list))
  1592. list_del_init(&td->cancelled_td_list);
  1593. }
  1594. goto err_giveback;
  1595. }
  1596. i = urb_priv->num_tds_done;
  1597. if (i < urb_priv->num_tds)
  1598. xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
  1599. "Cancel URB %p, dev %s, ep 0x%x, "
  1600. "starting at offset 0x%llx",
  1601. urb, urb->dev->devpath,
  1602. urb->ep->desc.bEndpointAddress,
  1603. (unsigned long long) xhci_trb_virt_to_dma(
  1604. urb_priv->td[i].start_seg,
  1605. urb_priv->td[i].first_trb));
  1606. for (; i < urb_priv->num_tds; i++) {
  1607. td = &urb_priv->td[i];
  1608. /* TD can already be on cancelled list if ep halted on it */
  1609. if (list_empty(&td->cancelled_td_list)) {
  1610. td->cancel_status = TD_DIRTY;
  1611. list_add_tail(&td->cancelled_td_list,
  1612. &ep->cancelled_td_list);
  1613. }
  1614. }
  1615. /* Queue a stop endpoint command, but only if this is
  1616. * the first cancellation to be handled.
  1617. */
  1618. if (!(ep->ep_state & EP_STOP_CMD_PENDING)) {
  1619. command = xhci_alloc_command(xhci, false, GFP_ATOMIC);
  1620. if (!command) {
  1621. ret = -ENOMEM;
  1622. goto done;
  1623. }
  1624. ep->ep_state |= EP_STOP_CMD_PENDING;
  1625. xhci_queue_stop_endpoint(xhci, command, urb->dev->slot_id,
  1626. ep_index, 0);
  1627. xhci_ring_cmd_db(xhci);
  1628. }
  1629. done:
  1630. spin_unlock_irqrestore(&xhci->lock, flags);
  1631. return ret;
  1632. err_giveback:
  1633. if (urb_priv)
  1634. xhci_urb_free_priv(urb_priv);
  1635. usb_hcd_unlink_urb_from_ep(hcd, urb);
  1636. spin_unlock_irqrestore(&xhci->lock, flags);
  1637. usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN);
  1638. return ret;
  1639. }
  1640. /* Drop an endpoint from a new bandwidth configuration for this device.
  1641. * Only one call to this function is allowed per endpoint before
  1642. * check_bandwidth() or reset_bandwidth() must be called.
  1643. * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
  1644. * add the endpoint to the schedule with possibly new parameters denoted by a
  1645. * different endpoint descriptor in usb_host_endpoint.
  1646. * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
  1647. * not allowed.
  1648. *
  1649. * The USB core will not allow URBs to be queued to an endpoint that is being
  1650. * disabled, so there's no need for mutual exclusion to protect
  1651. * the xhci->devs[slot_id] structure.
  1652. */
  1653. int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
  1654. struct usb_host_endpoint *ep)
  1655. {
  1656. struct xhci_hcd *xhci;
  1657. struct xhci_container_ctx *in_ctx, *out_ctx;
  1658. struct xhci_input_control_ctx *ctrl_ctx;
  1659. unsigned int ep_index;
  1660. struct xhci_ep_ctx *ep_ctx;
  1661. u32 drop_flag;
  1662. u32 new_add_flags, new_drop_flags;
  1663. int ret;
  1664. ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
  1665. if (ret <= 0)
  1666. return ret;
  1667. xhci = hcd_to_xhci(hcd);
  1668. if (xhci->xhc_state & XHCI_STATE_DYING)
  1669. return -ENODEV;
  1670. xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
  1671. drop_flag = xhci_get_endpoint_flag(&ep->desc);
  1672. if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
  1673. xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
  1674. __func__, drop_flag);
  1675. return 0;
  1676. }
  1677. in_ctx = xhci->devs[udev->slot_id]->in_ctx;
  1678. out_ctx = xhci->devs[udev->slot_id]->out_ctx;
  1679. ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
  1680. if (!ctrl_ctx) {
  1681. xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
  1682. __func__);
  1683. return 0;
  1684. }
  1685. ep_index = xhci_get_endpoint_index(&ep->desc);
  1686. ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
  1687. /* If the HC already knows the endpoint is disabled,
  1688. * or the HCD has noted it is disabled, ignore this request
  1689. */
  1690. if ((GET_EP_CTX_STATE(ep_ctx) == EP_STATE_DISABLED) ||
  1691. le32_to_cpu(ctrl_ctx->drop_flags) &
  1692. xhci_get_endpoint_flag(&ep->desc)) {
  1693. /* Do not warn when called after a usb_device_reset */
  1694. if (xhci->devs[udev->slot_id]->eps[ep_index].ring != NULL)
  1695. xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
  1696. __func__, ep);
  1697. return 0;
  1698. }
  1699. ctrl_ctx->drop_flags |= cpu_to_le32(drop_flag);
  1700. new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
  1701. ctrl_ctx->add_flags &= cpu_to_le32(~drop_flag);
  1702. new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
  1703. xhci_debugfs_remove_endpoint(xhci, xhci->devs[udev->slot_id], ep_index);
  1704. xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
  1705. xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x\n",
  1706. (unsigned int) ep->desc.bEndpointAddress,
  1707. udev->slot_id,
  1708. (unsigned int) new_drop_flags,
  1709. (unsigned int) new_add_flags);
  1710. return 0;
  1711. }
  1712. EXPORT_SYMBOL_GPL(xhci_drop_endpoint);
  1713. /* Add an endpoint to a new possible bandwidth configuration for this device.
  1714. * Only one call to this function is allowed per endpoint before
  1715. * check_bandwidth() or reset_bandwidth() must be called.
  1716. * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
  1717. * add the endpoint to the schedule with possibly new parameters denoted by a
  1718. * different endpoint descriptor in usb_host_endpoint.
  1719. * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
  1720. * not allowed.
  1721. *
  1722. * The USB core will not allow URBs to be queued to an endpoint until the
  1723. * configuration or alt setting is installed in the device, so there's no need
  1724. * for mutual exclusion to protect the xhci->devs[slot_id] structure.
  1725. */
  1726. int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
  1727. struct usb_host_endpoint *ep)
  1728. {
  1729. struct xhci_hcd *xhci;
  1730. struct xhci_container_ctx *in_ctx;
  1731. unsigned int ep_index;
  1732. struct xhci_input_control_ctx *ctrl_ctx;
  1733. struct xhci_ep_ctx *ep_ctx;
  1734. u32 added_ctxs;
  1735. u32 new_add_flags, new_drop_flags;
  1736. struct xhci_virt_device *virt_dev;
  1737. int ret = 0;
  1738. ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
  1739. if (ret <= 0) {
  1740. /* So we won't queue a reset ep command for a root hub */
  1741. ep->hcpriv = NULL;
  1742. return ret;
  1743. }
  1744. xhci = hcd_to_xhci(hcd);
  1745. if (xhci->xhc_state & XHCI_STATE_DYING)
  1746. return -ENODEV;
  1747. added_ctxs = xhci_get_endpoint_flag(&ep->desc);
  1748. if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
  1749. /* FIXME when we have to issue an evaluate endpoint command to
  1750. * deal with ep0 max packet size changing once we get the
  1751. * descriptors
  1752. */
  1753. xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
  1754. __func__, added_ctxs);
  1755. return 0;
  1756. }
  1757. virt_dev = xhci->devs[udev->slot_id];
  1758. in_ctx = virt_dev->in_ctx;
  1759. ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
  1760. if (!ctrl_ctx) {
  1761. xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
  1762. __func__);
  1763. return 0;
  1764. }
  1765. ep_index = xhci_get_endpoint_index(&ep->desc);
  1766. /* If this endpoint is already in use, and the upper layers are trying
  1767. * to add it again without dropping it, reject the addition.
  1768. */
  1769. if (virt_dev->eps[ep_index].ring &&
  1770. !(le32_to_cpu(ctrl_ctx->drop_flags) & added_ctxs)) {
  1771. xhci_warn(xhci, "Trying to add endpoint 0x%x "
  1772. "without dropping it.\n",
  1773. (unsigned int) ep->desc.bEndpointAddress);
  1774. return -EINVAL;
  1775. }
  1776. /* If the HCD has already noted the endpoint is enabled,
  1777. * ignore this request.
  1778. */
  1779. if (le32_to_cpu(ctrl_ctx->add_flags) & added_ctxs) {
  1780. xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
  1781. __func__, ep);
  1782. return 0;
  1783. }
  1784. /*
  1785. * Configuration and alternate setting changes must be done in
  1786. * process context, not interrupt context (or so documenation
  1787. * for usb_set_interface() and usb_set_configuration() claim).
  1788. */
  1789. if (xhci_endpoint_init(xhci, virt_dev, udev, ep, GFP_NOIO) < 0) {
  1790. dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
  1791. __func__, ep->desc.bEndpointAddress);
  1792. return -ENOMEM;
  1793. }
  1794. ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs);
  1795. new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
  1796. /* If xhci_endpoint_disable() was called for this endpoint, but the
  1797. * xHC hasn't been notified yet through the check_bandwidth() call,
  1798. * this re-adds a new state for the endpoint from the new endpoint
  1799. * descriptors. We must drop and re-add this endpoint, so we leave the
  1800. * drop flags alone.
  1801. */
  1802. new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
  1803. /* Store the usb_device pointer for later use */
  1804. ep->hcpriv = udev;
  1805. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
  1806. trace_xhci_add_endpoint(ep_ctx);
  1807. xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x\n",
  1808. (unsigned int) ep->desc.bEndpointAddress,
  1809. udev->slot_id,
  1810. (unsigned int) new_drop_flags,
  1811. (unsigned int) new_add_flags);
  1812. return 0;
  1813. }
  1814. EXPORT_SYMBOL_GPL(xhci_add_endpoint);
  1815. static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
  1816. {
  1817. struct xhci_input_control_ctx *ctrl_ctx;
  1818. struct xhci_ep_ctx *ep_ctx;
  1819. struct xhci_slot_ctx *slot_ctx;
  1820. int i;
  1821. ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
  1822. if (!ctrl_ctx) {
  1823. xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
  1824. __func__);
  1825. return;
  1826. }
  1827. /* When a device's add flag and drop flag are zero, any subsequent
  1828. * configure endpoint command will leave that endpoint's state
  1829. * untouched. Make sure we don't leave any old state in the input
  1830. * endpoint contexts.
  1831. */
  1832. ctrl_ctx->drop_flags = 0;
  1833. ctrl_ctx->add_flags = 0;
  1834. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  1835. slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
  1836. /* Endpoint 0 is always valid */
  1837. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
  1838. for (i = 1; i < 31; i++) {
  1839. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
  1840. ep_ctx->ep_info = 0;
  1841. ep_ctx->ep_info2 = 0;
  1842. ep_ctx->deq = 0;
  1843. ep_ctx->tx_info = 0;
  1844. }
  1845. }
  1846. static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
  1847. struct usb_device *udev, u32 *cmd_status)
  1848. {
  1849. int ret;
  1850. switch (*cmd_status) {
  1851. case COMP_COMMAND_ABORTED:
  1852. case COMP_COMMAND_RING_STOPPED:
  1853. xhci_warn(xhci, "Timeout while waiting for configure endpoint command\n");
  1854. ret = -ETIME;
  1855. break;
  1856. case COMP_RESOURCE_ERROR:
  1857. dev_warn(&udev->dev,
  1858. "Not enough host controller resources for new device state.\n");
  1859. ret = -ENOMEM;
  1860. /* FIXME: can we allocate more resources for the HC? */
  1861. break;
  1862. case COMP_BANDWIDTH_ERROR:
  1863. case COMP_SECONDARY_BANDWIDTH_ERROR:
  1864. dev_warn(&udev->dev,
  1865. "Not enough bandwidth for new device state.\n");
  1866. ret = -ENOSPC;
  1867. /* FIXME: can we go back to the old state? */
  1868. break;
  1869. case COMP_TRB_ERROR:
  1870. /* the HCD set up something wrong */
  1871. dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
  1872. "add flag = 1, "
  1873. "and endpoint is not disabled.\n");
  1874. ret = -EINVAL;
  1875. break;
  1876. case COMP_INCOMPATIBLE_DEVICE_ERROR:
  1877. dev_warn(&udev->dev,
  1878. "ERROR: Incompatible device for endpoint configure command.\n");
  1879. ret = -ENODEV;
  1880. break;
  1881. case COMP_SUCCESS:
  1882. xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
  1883. "Successful Endpoint Configure command");
  1884. ret = 0;
  1885. break;
  1886. default:
  1887. xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n",
  1888. *cmd_status);
  1889. ret = -EINVAL;
  1890. break;
  1891. }
  1892. return ret;
  1893. }
  1894. static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
  1895. struct usb_device *udev, u32 *cmd_status)
  1896. {
  1897. int ret;
  1898. switch (*cmd_status) {
  1899. case COMP_COMMAND_ABORTED:
  1900. case COMP_COMMAND_RING_STOPPED:
  1901. xhci_warn(xhci, "Timeout while waiting for evaluate context command\n");
  1902. ret = -ETIME;
  1903. break;
  1904. case COMP_PARAMETER_ERROR:
  1905. dev_warn(&udev->dev,
  1906. "WARN: xHCI driver setup invalid evaluate context command.\n");
  1907. ret = -EINVAL;
  1908. break;
  1909. case COMP_SLOT_NOT_ENABLED_ERROR:
  1910. dev_warn(&udev->dev,
  1911. "WARN: slot not enabled for evaluate context command.\n");
  1912. ret = -EINVAL;
  1913. break;
  1914. case COMP_CONTEXT_STATE_ERROR:
  1915. dev_warn(&udev->dev,
  1916. "WARN: invalid context state for evaluate context command.\n");
  1917. ret = -EINVAL;
  1918. break;
  1919. case COMP_INCOMPATIBLE_DEVICE_ERROR:
  1920. dev_warn(&udev->dev,
  1921. "ERROR: Incompatible device for evaluate context command.\n");
  1922. ret = -ENODEV;
  1923. break;
  1924. case COMP_MAX_EXIT_LATENCY_TOO_LARGE_ERROR:
  1925. /* Max Exit Latency too large error */
  1926. dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n");
  1927. ret = -EINVAL;
  1928. break;
  1929. case COMP_SUCCESS:
  1930. xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
  1931. "Successful evaluate context command");
  1932. ret = 0;
  1933. break;
  1934. default:
  1935. xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n",
  1936. *cmd_status);
  1937. ret = -EINVAL;
  1938. break;
  1939. }
  1940. return ret;
  1941. }
  1942. static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci,
  1943. struct xhci_input_control_ctx *ctrl_ctx)
  1944. {
  1945. u32 valid_add_flags;
  1946. u32 valid_drop_flags;
  1947. /* Ignore the slot flag (bit 0), and the default control endpoint flag
  1948. * (bit 1). The default control endpoint is added during the Address
  1949. * Device command and is never removed until the slot is disabled.
  1950. */
  1951. valid_add_flags = le32_to_cpu(ctrl_ctx->add_flags) >> 2;
  1952. valid_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags) >> 2;
  1953. /* Use hweight32 to count the number of ones in the add flags, or
  1954. * number of endpoints added. Don't count endpoints that are changed
  1955. * (both added and dropped).
  1956. */
  1957. return hweight32(valid_add_flags) -
  1958. hweight32(valid_add_flags & valid_drop_flags);
  1959. }
  1960. static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd *xhci,
  1961. struct xhci_input_control_ctx *ctrl_ctx)
  1962. {
  1963. u32 valid_add_flags;
  1964. u32 valid_drop_flags;
  1965. valid_add_flags = le32_to_cpu(ctrl_ctx->add_flags) >> 2;
  1966. valid_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags) >> 2;
  1967. return hweight32(valid_drop_flags) -
  1968. hweight32(valid_add_flags & valid_drop_flags);
  1969. }
  1970. /*
  1971. * We need to reserve the new number of endpoints before the configure endpoint
  1972. * command completes. We can't subtract the dropped endpoints from the number
  1973. * of active endpoints until the command completes because we can oversubscribe
  1974. * the host in this case:
  1975. *
  1976. * - the first configure endpoint command drops more endpoints than it adds
  1977. * - a second configure endpoint command that adds more endpoints is queued
  1978. * - the first configure endpoint command fails, so the config is unchanged
  1979. * - the second command may succeed, even though there isn't enough resources
  1980. *
  1981. * Must be called with xhci->lock held.
  1982. */
  1983. static int xhci_reserve_host_resources(struct xhci_hcd *xhci,
  1984. struct xhci_input_control_ctx *ctrl_ctx)
  1985. {
  1986. u32 added_eps;
  1987. added_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx);
  1988. if (xhci->num_active_eps + added_eps > xhci->limit_active_eps) {
  1989. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  1990. "Not enough ep ctxs: "
  1991. "%u active, need to add %u, limit is %u.",
  1992. xhci->num_active_eps, added_eps,
  1993. xhci->limit_active_eps);
  1994. return -ENOMEM;
  1995. }
  1996. xhci->num_active_eps += added_eps;
  1997. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  1998. "Adding %u ep ctxs, %u now active.", added_eps,
  1999. xhci->num_active_eps);
  2000. return 0;
  2001. }
  2002. /*
  2003. * The configure endpoint was failed by the xHC for some other reason, so we
  2004. * need to revert the resources that failed configuration would have used.
  2005. *
  2006. * Must be called with xhci->lock held.
  2007. */
  2008. static void xhci_free_host_resources(struct xhci_hcd *xhci,
  2009. struct xhci_input_control_ctx *ctrl_ctx)
  2010. {
  2011. u32 num_failed_eps;
  2012. num_failed_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx);
  2013. xhci->num_active_eps -= num_failed_eps;
  2014. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  2015. "Removing %u failed ep ctxs, %u now active.",
  2016. num_failed_eps,
  2017. xhci->num_active_eps);
  2018. }
  2019. /*
  2020. * Now that the command has completed, clean up the active endpoint count by
  2021. * subtracting out the endpoints that were dropped (but not changed).
  2022. *
  2023. * Must be called with xhci->lock held.
  2024. */
  2025. static void xhci_finish_resource_reservation(struct xhci_hcd *xhci,
  2026. struct xhci_input_control_ctx *ctrl_ctx)
  2027. {
  2028. u32 num_dropped_eps;
  2029. num_dropped_eps = xhci_count_num_dropped_endpoints(xhci, ctrl_ctx);
  2030. xhci->num_active_eps -= num_dropped_eps;
  2031. if (num_dropped_eps)
  2032. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  2033. "Removing %u dropped ep ctxs, %u now active.",
  2034. num_dropped_eps,
  2035. xhci->num_active_eps);
  2036. }
  2037. static unsigned int xhci_get_block_size(struct usb_device *udev)
  2038. {
  2039. switch (udev->speed) {
  2040. case USB_SPEED_LOW:
  2041. case USB_SPEED_FULL:
  2042. return FS_BLOCK;
  2043. case USB_SPEED_HIGH:
  2044. return HS_BLOCK;
  2045. case USB_SPEED_SUPER:
  2046. case USB_SPEED_SUPER_PLUS:
  2047. return SS_BLOCK;
  2048. case USB_SPEED_UNKNOWN:
  2049. case USB_SPEED_WIRELESS:
  2050. default:
  2051. /* Should never happen */
  2052. return 1;
  2053. }
  2054. }
  2055. static unsigned int
  2056. xhci_get_largest_overhead(struct xhci_interval_bw *interval_bw)
  2057. {
  2058. if (interval_bw->overhead[LS_OVERHEAD_TYPE])
  2059. return LS_OVERHEAD;
  2060. if (interval_bw->overhead[FS_OVERHEAD_TYPE])
  2061. return FS_OVERHEAD;
  2062. return HS_OVERHEAD;
  2063. }
  2064. /* If we are changing a LS/FS device under a HS hub,
  2065. * make sure (if we are activating a new TT) that the HS bus has enough
  2066. * bandwidth for this new TT.
  2067. */
  2068. static int xhci_check_tt_bw_table(struct xhci_hcd *xhci,
  2069. struct xhci_virt_device *virt_dev,
  2070. int old_active_eps)
  2071. {
  2072. struct xhci_interval_bw_table *bw_table;
  2073. struct xhci_tt_bw_info *tt_info;
  2074. /* Find the bandwidth table for the root port this TT is attached to. */
  2075. bw_table = &xhci->rh_bw[virt_dev->real_port - 1].bw_table;
  2076. tt_info = virt_dev->tt_info;
  2077. /* If this TT already had active endpoints, the bandwidth for this TT
  2078. * has already been added. Removing all periodic endpoints (and thus
  2079. * making the TT enactive) will only decrease the bandwidth used.
  2080. */
  2081. if (old_active_eps)
  2082. return 0;
  2083. if (old_active_eps == 0 && tt_info->active_eps != 0) {
  2084. if (bw_table->bw_used + TT_HS_OVERHEAD > HS_BW_LIMIT)
  2085. return -ENOMEM;
  2086. return 0;
  2087. }
  2088. /* Not sure why we would have no new active endpoints...
  2089. *
  2090. * Maybe because of an Evaluate Context change for a hub update or a
  2091. * control endpoint 0 max packet size change?
  2092. * FIXME: skip the bandwidth calculation in that case.
  2093. */
  2094. return 0;
  2095. }
  2096. static int xhci_check_ss_bw(struct xhci_hcd *xhci,
  2097. struct xhci_virt_device *virt_dev)
  2098. {
  2099. unsigned int bw_reserved;
  2100. bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_IN, 100);
  2101. if (virt_dev->bw_table->ss_bw_in > (SS_BW_LIMIT_IN - bw_reserved))
  2102. return -ENOMEM;
  2103. bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_OUT, 100);
  2104. if (virt_dev->bw_table->ss_bw_out > (SS_BW_LIMIT_OUT - bw_reserved))
  2105. return -ENOMEM;
  2106. return 0;
  2107. }
  2108. /*
  2109. * This algorithm is a very conservative estimate of the worst-case scheduling
  2110. * scenario for any one interval. The hardware dynamically schedules the
  2111. * packets, so we can't tell which microframe could be the limiting factor in
  2112. * the bandwidth scheduling. This only takes into account periodic endpoints.
  2113. *
  2114. * Obviously, we can't solve an NP complete problem to find the minimum worst
  2115. * case scenario. Instead, we come up with an estimate that is no less than
  2116. * the worst case bandwidth used for any one microframe, but may be an
  2117. * over-estimate.
  2118. *
  2119. * We walk the requirements for each endpoint by interval, starting with the
  2120. * smallest interval, and place packets in the schedule where there is only one
  2121. * possible way to schedule packets for that interval. In order to simplify
  2122. * this algorithm, we record the largest max packet size for each interval, and
  2123. * assume all packets will be that size.
  2124. *
  2125. * For interval 0, we obviously must schedule all packets for each interval.
  2126. * The bandwidth for interval 0 is just the amount of data to be transmitted
  2127. * (the sum of all max ESIT payload sizes, plus any overhead per packet times
  2128. * the number of packets).
  2129. *
  2130. * For interval 1, we have two possible microframes to schedule those packets
  2131. * in. For this algorithm, if we can schedule the same number of packets for
  2132. * each possible scheduling opportunity (each microframe), we will do so. The
  2133. * remaining number of packets will be saved to be transmitted in the gaps in
  2134. * the next interval's scheduling sequence.
  2135. *
  2136. * As we move those remaining packets to be scheduled with interval 2 packets,
  2137. * we have to double the number of remaining packets to transmit. This is
  2138. * because the intervals are actually powers of 2, and we would be transmitting
  2139. * the previous interval's packets twice in this interval. We also have to be
  2140. * sure that when we look at the largest max packet size for this interval, we
  2141. * also look at the largest max packet size for the remaining packets and take
  2142. * the greater of the two.
  2143. *
  2144. * The algorithm continues to evenly distribute packets in each scheduling
  2145. * opportunity, and push the remaining packets out, until we get to the last
  2146. * interval. Then those packets and their associated overhead are just added
  2147. * to the bandwidth used.
  2148. */
  2149. static int xhci_check_bw_table(struct xhci_hcd *xhci,
  2150. struct xhci_virt_device *virt_dev,
  2151. int old_active_eps)
  2152. {
  2153. unsigned int bw_reserved;
  2154. unsigned int max_bandwidth;
  2155. unsigned int bw_used;
  2156. unsigned int block_size;
  2157. struct xhci_interval_bw_table *bw_table;
  2158. unsigned int packet_size = 0;
  2159. unsigned int overhead = 0;
  2160. unsigned int packets_transmitted = 0;
  2161. unsigned int packets_remaining = 0;
  2162. unsigned int i;
  2163. if (virt_dev->udev->speed >= USB_SPEED_SUPER)
  2164. return xhci_check_ss_bw(xhci, virt_dev);
  2165. if (virt_dev->udev->speed == USB_SPEED_HIGH) {
  2166. max_bandwidth = HS_BW_LIMIT;
  2167. /* Convert percent of bus BW reserved to blocks reserved */
  2168. bw_reserved = DIV_ROUND_UP(HS_BW_RESERVED * max_bandwidth, 100);
  2169. } else {
  2170. max_bandwidth = FS_BW_LIMIT;
  2171. bw_reserved = DIV_ROUND_UP(FS_BW_RESERVED * max_bandwidth, 100);
  2172. }
  2173. bw_table = virt_dev->bw_table;
  2174. /* We need to translate the max packet size and max ESIT payloads into
  2175. * the units the hardware uses.
  2176. */
  2177. block_size = xhci_get_block_size(virt_dev->udev);
  2178. /* If we are manipulating a LS/FS device under a HS hub, double check
  2179. * that the HS bus has enough bandwidth if we are activing a new TT.
  2180. */
  2181. if (virt_dev->tt_info) {
  2182. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  2183. "Recalculating BW for rootport %u",
  2184. virt_dev->real_port);
  2185. if (xhci_check_tt_bw_table(xhci, virt_dev, old_active_eps)) {
  2186. xhci_warn(xhci, "Not enough bandwidth on HS bus for "
  2187. "newly activated TT.\n");
  2188. return -ENOMEM;
  2189. }
  2190. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  2191. "Recalculating BW for TT slot %u port %u",
  2192. virt_dev->tt_info->slot_id,
  2193. virt_dev->tt_info->ttport);
  2194. } else {
  2195. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  2196. "Recalculating BW for rootport %u",
  2197. virt_dev->real_port);
  2198. }
  2199. /* Add in how much bandwidth will be used for interval zero, or the
  2200. * rounded max ESIT payload + number of packets * largest overhead.
  2201. */
  2202. bw_used = DIV_ROUND_UP(bw_table->interval0_esit_payload, block_size) +
  2203. bw_table->interval_bw[0].num_packets *
  2204. xhci_get_largest_overhead(&bw_table->interval_bw[0]);
  2205. for (i = 1; i < XHCI_MAX_INTERVAL; i++) {
  2206. unsigned int bw_added;
  2207. unsigned int largest_mps;
  2208. unsigned int interval_overhead;
  2209. /*
  2210. * How many packets could we transmit in this interval?
  2211. * If packets didn't fit in the previous interval, we will need
  2212. * to transmit that many packets twice within this interval.
  2213. */
  2214. packets_remaining = 2 * packets_remaining +
  2215. bw_table->interval_bw[i].num_packets;
  2216. /* Find the largest max packet size of this or the previous
  2217. * interval.
  2218. */
  2219. if (list_empty(&bw_table->interval_bw[i].endpoints))
  2220. largest_mps = 0;
  2221. else {
  2222. struct xhci_virt_ep *virt_ep;
  2223. struct list_head *ep_entry;
  2224. ep_entry = bw_table->interval_bw[i].endpoints.next;
  2225. virt_ep = list_entry(ep_entry,
  2226. struct xhci_virt_ep, bw_endpoint_list);
  2227. /* Convert to blocks, rounding up */
  2228. largest_mps = DIV_ROUND_UP(
  2229. virt_ep->bw_info.max_packet_size,
  2230. block_size);
  2231. }
  2232. if (largest_mps > packet_size)
  2233. packet_size = largest_mps;
  2234. /* Use the larger overhead of this or the previous interval. */
  2235. interval_overhead = xhci_get_largest_overhead(
  2236. &bw_table->interval_bw[i]);
  2237. if (interval_overhead > overhead)
  2238. overhead = interval_overhead;
  2239. /* How many packets can we evenly distribute across
  2240. * (1 << (i + 1)) possible scheduling opportunities?
  2241. */
  2242. packets_transmitted = packets_remaining >> (i + 1);
  2243. /* Add in the bandwidth used for those scheduled packets */
  2244. bw_added = packets_transmitted * (overhead + packet_size);
  2245. /* How many packets do we have remaining to transmit? */
  2246. packets_remaining = packets_remaining % (1 << (i + 1));
  2247. /* What largest max packet size should those packets have? */
  2248. /* If we've transmitted all packets, don't carry over the
  2249. * largest packet size.
  2250. */
  2251. if (packets_remaining == 0) {
  2252. packet_size = 0;
  2253. overhead = 0;
  2254. } else if (packets_transmitted > 0) {
  2255. /* Otherwise if we do have remaining packets, and we've
  2256. * scheduled some packets in this interval, take the
  2257. * largest max packet size from endpoints with this
  2258. * interval.
  2259. */
  2260. packet_size = largest_mps;
  2261. overhead = interval_overhead;
  2262. }
  2263. /* Otherwise carry over packet_size and overhead from the last
  2264. * time we had a remainder.
  2265. */
  2266. bw_used += bw_added;
  2267. if (bw_used > max_bandwidth) {
  2268. xhci_warn(xhci, "Not enough bandwidth. "
  2269. "Proposed: %u, Max: %u\n",
  2270. bw_used, max_bandwidth);
  2271. return -ENOMEM;
  2272. }
  2273. }
  2274. /*
  2275. * Ok, we know we have some packets left over after even-handedly
  2276. * scheduling interval 15. We don't know which microframes they will
  2277. * fit into, so we over-schedule and say they will be scheduled every
  2278. * microframe.
  2279. */
  2280. if (packets_remaining > 0)
  2281. bw_used += overhead + packet_size;
  2282. if (!virt_dev->tt_info && virt_dev->udev->speed == USB_SPEED_HIGH) {
  2283. unsigned int port_index = virt_dev->real_port - 1;
  2284. /* OK, we're manipulating a HS device attached to a
  2285. * root port bandwidth domain. Include the number of active TTs
  2286. * in the bandwidth used.
  2287. */
  2288. bw_used += TT_HS_OVERHEAD *
  2289. xhci->rh_bw[port_index].num_active_tts;
  2290. }
  2291. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  2292. "Final bandwidth: %u, Limit: %u, Reserved: %u, "
  2293. "Available: %u " "percent",
  2294. bw_used, max_bandwidth, bw_reserved,
  2295. (max_bandwidth - bw_used - bw_reserved) * 100 /
  2296. max_bandwidth);
  2297. bw_used += bw_reserved;
  2298. if (bw_used > max_bandwidth) {
  2299. xhci_warn(xhci, "Not enough bandwidth. Proposed: %u, Max: %u\n",
  2300. bw_used, max_bandwidth);
  2301. return -ENOMEM;
  2302. }
  2303. bw_table->bw_used = bw_used;
  2304. return 0;
  2305. }
  2306. static bool xhci_is_async_ep(unsigned int ep_type)
  2307. {
  2308. return (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP &&
  2309. ep_type != ISOC_IN_EP &&
  2310. ep_type != INT_IN_EP);
  2311. }
  2312. static bool xhci_is_sync_in_ep(unsigned int ep_type)
  2313. {
  2314. return (ep_type == ISOC_IN_EP || ep_type == INT_IN_EP);
  2315. }
  2316. static unsigned int xhci_get_ss_bw_consumed(struct xhci_bw_info *ep_bw)
  2317. {
  2318. unsigned int mps = DIV_ROUND_UP(ep_bw->max_packet_size, SS_BLOCK);
  2319. if (ep_bw->ep_interval == 0)
  2320. return SS_OVERHEAD_BURST +
  2321. (ep_bw->mult * ep_bw->num_packets *
  2322. (SS_OVERHEAD + mps));
  2323. return DIV_ROUND_UP(ep_bw->mult * ep_bw->num_packets *
  2324. (SS_OVERHEAD + mps + SS_OVERHEAD_BURST),
  2325. 1 << ep_bw->ep_interval);
  2326. }
  2327. static void xhci_drop_ep_from_interval_table(struct xhci_hcd *xhci,
  2328. struct xhci_bw_info *ep_bw,
  2329. struct xhci_interval_bw_table *bw_table,
  2330. struct usb_device *udev,
  2331. struct xhci_virt_ep *virt_ep,
  2332. struct xhci_tt_bw_info *tt_info)
  2333. {
  2334. struct xhci_interval_bw *interval_bw;
  2335. int normalized_interval;
  2336. if (xhci_is_async_ep(ep_bw->type))
  2337. return;
  2338. if (udev->speed >= USB_SPEED_SUPER) {
  2339. if (xhci_is_sync_in_ep(ep_bw->type))
  2340. xhci->devs[udev->slot_id]->bw_table->ss_bw_in -=
  2341. xhci_get_ss_bw_consumed(ep_bw);
  2342. else
  2343. xhci->devs[udev->slot_id]->bw_table->ss_bw_out -=
  2344. xhci_get_ss_bw_consumed(ep_bw);
  2345. return;
  2346. }
  2347. /* SuperSpeed endpoints never get added to intervals in the table, so
  2348. * this check is only valid for HS/FS/LS devices.
  2349. */
  2350. if (list_empty(&virt_ep->bw_endpoint_list))
  2351. return;
  2352. /* For LS/FS devices, we need to translate the interval expressed in
  2353. * microframes to frames.
  2354. */
  2355. if (udev->speed == USB_SPEED_HIGH)
  2356. normalized_interval = ep_bw->ep_interval;
  2357. else
  2358. normalized_interval = ep_bw->ep_interval - 3;
  2359. if (normalized_interval == 0)
  2360. bw_table->interval0_esit_payload -= ep_bw->max_esit_payload;
  2361. interval_bw = &bw_table->interval_bw[normalized_interval];
  2362. interval_bw->num_packets -= ep_bw->num_packets;
  2363. switch (udev->speed) {
  2364. case USB_SPEED_LOW:
  2365. interval_bw->overhead[LS_OVERHEAD_TYPE] -= 1;
  2366. break;
  2367. case USB_SPEED_FULL:
  2368. interval_bw->overhead[FS_OVERHEAD_TYPE] -= 1;
  2369. break;
  2370. case USB_SPEED_HIGH:
  2371. interval_bw->overhead[HS_OVERHEAD_TYPE] -= 1;
  2372. break;
  2373. case USB_SPEED_SUPER:
  2374. case USB_SPEED_SUPER_PLUS:
  2375. case USB_SPEED_UNKNOWN:
  2376. case USB_SPEED_WIRELESS:
  2377. /* Should never happen because only LS/FS/HS endpoints will get
  2378. * added to the endpoint list.
  2379. */
  2380. return;
  2381. }
  2382. if (tt_info)
  2383. tt_info->active_eps -= 1;
  2384. list_del_init(&virt_ep->bw_endpoint_list);
  2385. }
  2386. static void xhci_add_ep_to_interval_table(struct xhci_hcd *xhci,
  2387. struct xhci_bw_info *ep_bw,
  2388. struct xhci_interval_bw_table *bw_table,
  2389. struct usb_device *udev,
  2390. struct xhci_virt_ep *virt_ep,
  2391. struct xhci_tt_bw_info *tt_info)
  2392. {
  2393. struct xhci_interval_bw *interval_bw;
  2394. struct xhci_virt_ep *smaller_ep;
  2395. int normalized_interval;
  2396. if (xhci_is_async_ep(ep_bw->type))
  2397. return;
  2398. if (udev->speed == USB_SPEED_SUPER) {
  2399. if (xhci_is_sync_in_ep(ep_bw->type))
  2400. xhci->devs[udev->slot_id]->bw_table->ss_bw_in +=
  2401. xhci_get_ss_bw_consumed(ep_bw);
  2402. else
  2403. xhci->devs[udev->slot_id]->bw_table->ss_bw_out +=
  2404. xhci_get_ss_bw_consumed(ep_bw);
  2405. return;
  2406. }
  2407. /* For LS/FS devices, we need to translate the interval expressed in
  2408. * microframes to frames.
  2409. */
  2410. if (udev->speed == USB_SPEED_HIGH)
  2411. normalized_interval = ep_bw->ep_interval;
  2412. else
  2413. normalized_interval = ep_bw->ep_interval - 3;
  2414. if (normalized_interval == 0)
  2415. bw_table->interval0_esit_payload += ep_bw->max_esit_payload;
  2416. interval_bw = &bw_table->interval_bw[normalized_interval];
  2417. interval_bw->num_packets += ep_bw->num_packets;
  2418. switch (udev->speed) {
  2419. case USB_SPEED_LOW:
  2420. interval_bw->overhead[LS_OVERHEAD_TYPE] += 1;
  2421. break;
  2422. case USB_SPEED_FULL:
  2423. interval_bw->overhead[FS_OVERHEAD_TYPE] += 1;
  2424. break;
  2425. case USB_SPEED_HIGH:
  2426. interval_bw->overhead[HS_OVERHEAD_TYPE] += 1;
  2427. break;
  2428. case USB_SPEED_SUPER:
  2429. case USB_SPEED_SUPER_PLUS:
  2430. case USB_SPEED_UNKNOWN:
  2431. case USB_SPEED_WIRELESS:
  2432. /* Should never happen because only LS/FS/HS endpoints will get
  2433. * added to the endpoint list.
  2434. */
  2435. return;
  2436. }
  2437. if (tt_info)
  2438. tt_info->active_eps += 1;
  2439. /* Insert the endpoint into the list, largest max packet size first. */
  2440. list_for_each_entry(smaller_ep, &interval_bw->endpoints,
  2441. bw_endpoint_list) {
  2442. if (ep_bw->max_packet_size >=
  2443. smaller_ep->bw_info.max_packet_size) {
  2444. /* Add the new ep before the smaller endpoint */
  2445. list_add_tail(&virt_ep->bw_endpoint_list,
  2446. &smaller_ep->bw_endpoint_list);
  2447. return;
  2448. }
  2449. }
  2450. /* Add the new endpoint at the end of the list. */
  2451. list_add_tail(&virt_ep->bw_endpoint_list,
  2452. &interval_bw->endpoints);
  2453. }
  2454. void xhci_update_tt_active_eps(struct xhci_hcd *xhci,
  2455. struct xhci_virt_device *virt_dev,
  2456. int old_active_eps)
  2457. {
  2458. struct xhci_root_port_bw_info *rh_bw_info;
  2459. if (!virt_dev->tt_info)
  2460. return;
  2461. rh_bw_info = &xhci->rh_bw[virt_dev->real_port - 1];
  2462. if (old_active_eps == 0 &&
  2463. virt_dev->tt_info->active_eps != 0) {
  2464. rh_bw_info->num_active_tts += 1;
  2465. rh_bw_info->bw_table.bw_used += TT_HS_OVERHEAD;
  2466. } else if (old_active_eps != 0 &&
  2467. virt_dev->tt_info->active_eps == 0) {
  2468. rh_bw_info->num_active_tts -= 1;
  2469. rh_bw_info->bw_table.bw_used -= TT_HS_OVERHEAD;
  2470. }
  2471. }
  2472. static int xhci_reserve_bandwidth(struct xhci_hcd *xhci,
  2473. struct xhci_virt_device *virt_dev,
  2474. struct xhci_container_ctx *in_ctx)
  2475. {
  2476. struct xhci_bw_info ep_bw_info[31];
  2477. int i;
  2478. struct xhci_input_control_ctx *ctrl_ctx;
  2479. int old_active_eps = 0;
  2480. if (virt_dev->tt_info)
  2481. old_active_eps = virt_dev->tt_info->active_eps;
  2482. ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
  2483. if (!ctrl_ctx) {
  2484. xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
  2485. __func__);
  2486. return -ENOMEM;
  2487. }
  2488. for (i = 0; i < 31; i++) {
  2489. if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
  2490. continue;
  2491. /* Make a copy of the BW info in case we need to revert this */
  2492. memcpy(&ep_bw_info[i], &virt_dev->eps[i].bw_info,
  2493. sizeof(ep_bw_info[i]));
  2494. /* Drop the endpoint from the interval table if the endpoint is
  2495. * being dropped or changed.
  2496. */
  2497. if (EP_IS_DROPPED(ctrl_ctx, i))
  2498. xhci_drop_ep_from_interval_table(xhci,
  2499. &virt_dev->eps[i].bw_info,
  2500. virt_dev->bw_table,
  2501. virt_dev->udev,
  2502. &virt_dev->eps[i],
  2503. virt_dev->tt_info);
  2504. }
  2505. /* Overwrite the information stored in the endpoints' bw_info */
  2506. xhci_update_bw_info(xhci, virt_dev->in_ctx, ctrl_ctx, virt_dev);
  2507. for (i = 0; i < 31; i++) {
  2508. /* Add any changed or added endpoints to the interval table */
  2509. if (EP_IS_ADDED(ctrl_ctx, i))
  2510. xhci_add_ep_to_interval_table(xhci,
  2511. &virt_dev->eps[i].bw_info,
  2512. virt_dev->bw_table,
  2513. virt_dev->udev,
  2514. &virt_dev->eps[i],
  2515. virt_dev->tt_info);
  2516. }
  2517. if (!xhci_check_bw_table(xhci, virt_dev, old_active_eps)) {
  2518. /* Ok, this fits in the bandwidth we have.
  2519. * Update the number of active TTs.
  2520. */
  2521. xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
  2522. return 0;
  2523. }
  2524. /* We don't have enough bandwidth for this, revert the stored info. */
  2525. for (i = 0; i < 31; i++) {
  2526. if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
  2527. continue;
  2528. /* Drop the new copies of any added or changed endpoints from
  2529. * the interval table.
  2530. */
  2531. if (EP_IS_ADDED(ctrl_ctx, i)) {
  2532. xhci_drop_ep_from_interval_table(xhci,
  2533. &virt_dev->eps[i].bw_info,
  2534. virt_dev->bw_table,
  2535. virt_dev->udev,
  2536. &virt_dev->eps[i],
  2537. virt_dev->tt_info);
  2538. }
  2539. /* Revert the endpoint back to its old information */
  2540. memcpy(&virt_dev->eps[i].bw_info, &ep_bw_info[i],
  2541. sizeof(ep_bw_info[i]));
  2542. /* Add any changed or dropped endpoints back into the table */
  2543. if (EP_IS_DROPPED(ctrl_ctx, i))
  2544. xhci_add_ep_to_interval_table(xhci,
  2545. &virt_dev->eps[i].bw_info,
  2546. virt_dev->bw_table,
  2547. virt_dev->udev,
  2548. &virt_dev->eps[i],
  2549. virt_dev->tt_info);
  2550. }
  2551. return -ENOMEM;
  2552. }
  2553. /* Issue a configure endpoint command or evaluate context command
  2554. * and wait for it to finish.
  2555. */
  2556. static int xhci_configure_endpoint(struct xhci_hcd *xhci,
  2557. struct usb_device *udev,
  2558. struct xhci_command *command,
  2559. bool ctx_change, bool must_succeed)
  2560. {
  2561. int ret;
  2562. unsigned long flags;
  2563. struct xhci_input_control_ctx *ctrl_ctx;
  2564. struct xhci_virt_device *virt_dev;
  2565. struct xhci_slot_ctx *slot_ctx;
  2566. if (!command)
  2567. return -EINVAL;
  2568. spin_lock_irqsave(&xhci->lock, flags);
  2569. if (xhci->xhc_state & XHCI_STATE_DYING) {
  2570. spin_unlock_irqrestore(&xhci->lock, flags);
  2571. return -ESHUTDOWN;
  2572. }
  2573. virt_dev = xhci->devs[udev->slot_id];
  2574. ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
  2575. if (!ctrl_ctx) {
  2576. spin_unlock_irqrestore(&xhci->lock, flags);
  2577. xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
  2578. __func__);
  2579. return -ENOMEM;
  2580. }
  2581. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) &&
  2582. xhci_reserve_host_resources(xhci, ctrl_ctx)) {
  2583. spin_unlock_irqrestore(&xhci->lock, flags);
  2584. xhci_warn(xhci, "Not enough host resources, "
  2585. "active endpoint contexts = %u\n",
  2586. xhci->num_active_eps);
  2587. return -ENOMEM;
  2588. }
  2589. if ((xhci->quirks & XHCI_SW_BW_CHECKING) &&
  2590. xhci_reserve_bandwidth(xhci, virt_dev, command->in_ctx)) {
  2591. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
  2592. xhci_free_host_resources(xhci, ctrl_ctx);
  2593. spin_unlock_irqrestore(&xhci->lock, flags);
  2594. xhci_warn(xhci, "Not enough bandwidth\n");
  2595. return -ENOMEM;
  2596. }
  2597. slot_ctx = xhci_get_slot_ctx(xhci, command->in_ctx);
  2598. trace_xhci_configure_endpoint_ctrl_ctx(ctrl_ctx);
  2599. trace_xhci_configure_endpoint(slot_ctx);
  2600. if (!ctx_change)
  2601. ret = xhci_queue_configure_endpoint(xhci, command,
  2602. command->in_ctx->dma,
  2603. udev->slot_id, must_succeed);
  2604. else
  2605. ret = xhci_queue_evaluate_context(xhci, command,
  2606. command->in_ctx->dma,
  2607. udev->slot_id, must_succeed);
  2608. if (ret < 0) {
  2609. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
  2610. xhci_free_host_resources(xhci, ctrl_ctx);
  2611. spin_unlock_irqrestore(&xhci->lock, flags);
  2612. xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
  2613. "FIXME allocate a new ring segment");
  2614. return -ENOMEM;
  2615. }
  2616. xhci_ring_cmd_db(xhci);
  2617. spin_unlock_irqrestore(&xhci->lock, flags);
  2618. /* Wait for the configure endpoint command to complete */
  2619. wait_for_completion(command->completion);
  2620. if (!ctx_change)
  2621. ret = xhci_configure_endpoint_result(xhci, udev,
  2622. &command->status);
  2623. else
  2624. ret = xhci_evaluate_context_result(xhci, udev,
  2625. &command->status);
  2626. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
  2627. spin_lock_irqsave(&xhci->lock, flags);
  2628. /* If the command failed, remove the reserved resources.
  2629. * Otherwise, clean up the estimate to include dropped eps.
  2630. */
  2631. if (ret)
  2632. xhci_free_host_resources(xhci, ctrl_ctx);
  2633. else
  2634. xhci_finish_resource_reservation(xhci, ctrl_ctx);
  2635. spin_unlock_irqrestore(&xhci->lock, flags);
  2636. }
  2637. if (ret)
  2638. goto failed;
  2639. ret = xhci_vendor_sync_dev_ctx(xhci, udev->slot_id);
  2640. if (ret)
  2641. xhci_warn(xhci, "sync device context failed, ret=%d", ret);
  2642. failed:
  2643. return ret;
  2644. }
  2645. static void xhci_check_bw_drop_ep_streams(struct xhci_hcd *xhci,
  2646. struct xhci_virt_device *vdev, int i)
  2647. {
  2648. struct xhci_virt_ep *ep = &vdev->eps[i];
  2649. if (ep->ep_state & EP_HAS_STREAMS) {
  2650. xhci_warn(xhci, "WARN: endpoint 0x%02x has streams on set_interface, freeing streams.\n",
  2651. xhci_get_endpoint_address(i));
  2652. xhci_free_stream_info(xhci, ep->stream_info);
  2653. ep->stream_info = NULL;
  2654. ep->ep_state &= ~EP_HAS_STREAMS;
  2655. }
  2656. }
  2657. /* Called after one or more calls to xhci_add_endpoint() or
  2658. * xhci_drop_endpoint(). If this call fails, the USB core is expected
  2659. * to call xhci_reset_bandwidth().
  2660. *
  2661. * Since we are in the middle of changing either configuration or
  2662. * installing a new alt setting, the USB core won't allow URBs to be
  2663. * enqueued for any endpoint on the old config or interface. Nothing
  2664. * else should be touching the xhci->devs[slot_id] structure, so we
  2665. * don't need to take the xhci->lock for manipulating that.
  2666. */
  2667. int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
  2668. {
  2669. int i;
  2670. int ret = 0;
  2671. struct xhci_hcd *xhci;
  2672. struct xhci_virt_device *virt_dev;
  2673. struct xhci_input_control_ctx *ctrl_ctx;
  2674. struct xhci_slot_ctx *slot_ctx;
  2675. struct xhci_command *command;
  2676. ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
  2677. if (ret <= 0)
  2678. return ret;
  2679. xhci = hcd_to_xhci(hcd);
  2680. if ((xhci->xhc_state & XHCI_STATE_DYING) ||
  2681. (xhci->xhc_state & XHCI_STATE_REMOVING))
  2682. return -ENODEV;
  2683. xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
  2684. virt_dev = xhci->devs[udev->slot_id];
  2685. command = xhci_alloc_command(xhci, true, GFP_KERNEL);
  2686. if (!command)
  2687. return -ENOMEM;
  2688. command->in_ctx = virt_dev->in_ctx;
  2689. /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
  2690. ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
  2691. if (!ctrl_ctx) {
  2692. xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
  2693. __func__);
  2694. ret = -ENOMEM;
  2695. goto command_cleanup;
  2696. }
  2697. ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
  2698. ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
  2699. ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
  2700. /* Don't issue the command if there's no endpoints to update. */
  2701. if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) &&
  2702. ctrl_ctx->drop_flags == 0) {
  2703. ret = 0;
  2704. goto command_cleanup;
  2705. }
  2706. /* Fix up Context Entries field. Minimum value is EP0 == BIT(1). */
  2707. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  2708. for (i = 31; i >= 1; i--) {
  2709. __le32 le32 = cpu_to_le32(BIT(i));
  2710. if ((virt_dev->eps[i-1].ring && !(ctrl_ctx->drop_flags & le32))
  2711. || (ctrl_ctx->add_flags & le32) || i == 1) {
  2712. slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
  2713. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(i));
  2714. break;
  2715. }
  2716. }
  2717. ret = xhci_configure_endpoint(xhci, udev, command,
  2718. false, false);
  2719. if (ret)
  2720. /* Callee should call reset_bandwidth() */
  2721. goto command_cleanup;
  2722. /* Free any rings that were dropped, but not changed. */
  2723. for (i = 1; i < 31; i++) {
  2724. if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) &&
  2725. !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1)))) {
  2726. xhci_free_endpoint_ring(xhci, virt_dev, i);
  2727. xhci_check_bw_drop_ep_streams(xhci, virt_dev, i);
  2728. }
  2729. }
  2730. xhci_zero_in_ctx(xhci, virt_dev);
  2731. /*
  2732. * Install any rings for completely new endpoints or changed endpoints,
  2733. * and free any old rings from changed endpoints.
  2734. */
  2735. for (i = 1; i < 31; i++) {
  2736. if (!virt_dev->eps[i].new_ring)
  2737. continue;
  2738. /* Only free the old ring if it exists.
  2739. * It may not if this is the first add of an endpoint.
  2740. */
  2741. if (virt_dev->eps[i].ring) {
  2742. xhci_free_endpoint_ring(xhci, virt_dev, i);
  2743. }
  2744. xhci_check_bw_drop_ep_streams(xhci, virt_dev, i);
  2745. virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
  2746. virt_dev->eps[i].new_ring = NULL;
  2747. xhci_debugfs_create_endpoint(xhci, virt_dev, i);
  2748. }
  2749. command_cleanup:
  2750. kfree(command->completion);
  2751. kfree(command);
  2752. return ret;
  2753. }
  2754. EXPORT_SYMBOL_GPL(xhci_check_bandwidth);
  2755. void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
  2756. {
  2757. struct xhci_hcd *xhci;
  2758. struct xhci_virt_device *virt_dev;
  2759. int i, ret;
  2760. ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
  2761. if (ret <= 0)
  2762. return;
  2763. xhci = hcd_to_xhci(hcd);
  2764. xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
  2765. virt_dev = xhci->devs[udev->slot_id];
  2766. /* Free any rings allocated for added endpoints */
  2767. for (i = 0; i < 31; i++) {
  2768. if (virt_dev->eps[i].new_ring) {
  2769. xhci_debugfs_remove_endpoint(xhci, virt_dev, i);
  2770. if (xhci_vendor_is_usb_offload_enabled(xhci, virt_dev, i))
  2771. xhci_vendor_free_transfer_ring(xhci, virt_dev, i);
  2772. else
  2773. xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
  2774. virt_dev->eps[i].new_ring = NULL;
  2775. }
  2776. }
  2777. xhci_zero_in_ctx(xhci, virt_dev);
  2778. }
  2779. EXPORT_SYMBOL_GPL(xhci_reset_bandwidth);
  2780. static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
  2781. struct xhci_container_ctx *in_ctx,
  2782. struct xhci_container_ctx *out_ctx,
  2783. struct xhci_input_control_ctx *ctrl_ctx,
  2784. u32 add_flags, u32 drop_flags)
  2785. {
  2786. ctrl_ctx->add_flags = cpu_to_le32(add_flags);
  2787. ctrl_ctx->drop_flags = cpu_to_le32(drop_flags);
  2788. xhci_slot_copy(xhci, in_ctx, out_ctx);
  2789. ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
  2790. }
  2791. static void xhci_endpoint_disable(struct usb_hcd *hcd,
  2792. struct usb_host_endpoint *host_ep)
  2793. {
  2794. struct xhci_hcd *xhci;
  2795. struct xhci_virt_device *vdev;
  2796. struct xhci_virt_ep *ep;
  2797. struct usb_device *udev;
  2798. unsigned long flags;
  2799. unsigned int ep_index;
  2800. xhci = hcd_to_xhci(hcd);
  2801. rescan:
  2802. spin_lock_irqsave(&xhci->lock, flags);
  2803. udev = (struct usb_device *)host_ep->hcpriv;
  2804. if (!udev || !udev->slot_id)
  2805. goto done;
  2806. vdev = xhci->devs[udev->slot_id];
  2807. if (!vdev)
  2808. goto done;
  2809. ep_index = xhci_get_endpoint_index(&host_ep->desc);
  2810. ep = &vdev->eps[ep_index];
  2811. /* wait for hub_tt_work to finish clearing hub TT */
  2812. if (ep->ep_state & EP_CLEARING_TT) {
  2813. spin_unlock_irqrestore(&xhci->lock, flags);
  2814. schedule_timeout_uninterruptible(1);
  2815. goto rescan;
  2816. }
  2817. if (ep->ep_state)
  2818. xhci_dbg(xhci, "endpoint disable with ep_state 0x%x\n",
  2819. ep->ep_state);
  2820. done:
  2821. host_ep->hcpriv = NULL;
  2822. spin_unlock_irqrestore(&xhci->lock, flags);
  2823. }
  2824. /*
  2825. * Called after usb core issues a clear halt control message.
  2826. * The host side of the halt should already be cleared by a reset endpoint
  2827. * command issued when the STALL event was received.
  2828. *
  2829. * The reset endpoint command may only be issued to endpoints in the halted
  2830. * state. For software that wishes to reset the data toggle or sequence number
  2831. * of an endpoint that isn't in the halted state this function will issue a
  2832. * configure endpoint command with the Drop and Add bits set for the target
  2833. * endpoint. Refer to the additional note in xhci spcification section 4.6.8.
  2834. */
  2835. static void xhci_endpoint_reset(struct usb_hcd *hcd,
  2836. struct usb_host_endpoint *host_ep)
  2837. {
  2838. struct xhci_hcd *xhci;
  2839. struct usb_device *udev;
  2840. struct xhci_virt_device *vdev;
  2841. struct xhci_virt_ep *ep;
  2842. struct xhci_input_control_ctx *ctrl_ctx;
  2843. struct xhci_command *stop_cmd, *cfg_cmd;
  2844. unsigned int ep_index;
  2845. unsigned long flags;
  2846. u32 ep_flag;
  2847. int err;
  2848. xhci = hcd_to_xhci(hcd);
  2849. if (!host_ep->hcpriv)
  2850. return;
  2851. udev = (struct usb_device *) host_ep->hcpriv;
  2852. vdev = xhci->devs[udev->slot_id];
  2853. /*
  2854. * vdev may be lost due to xHC restore error and re-initialization
  2855. * during S3/S4 resume. A new vdev will be allocated later by
  2856. * xhci_discover_or_reset_device()
  2857. */
  2858. if (!udev->slot_id || !vdev)
  2859. return;
  2860. ep_index = xhci_get_endpoint_index(&host_ep->desc);
  2861. ep = &vdev->eps[ep_index];
  2862. /* Bail out if toggle is already being cleared by a endpoint reset */
  2863. spin_lock_irqsave(&xhci->lock, flags);
  2864. if (ep->ep_state & EP_HARD_CLEAR_TOGGLE) {
  2865. ep->ep_state &= ~EP_HARD_CLEAR_TOGGLE;
  2866. spin_unlock_irqrestore(&xhci->lock, flags);
  2867. return;
  2868. }
  2869. spin_unlock_irqrestore(&xhci->lock, flags);
  2870. /* Only interrupt and bulk ep's use data toggle, USB2 spec 5.5.4-> */
  2871. if (usb_endpoint_xfer_control(&host_ep->desc) ||
  2872. usb_endpoint_xfer_isoc(&host_ep->desc))
  2873. return;
  2874. ep_flag = xhci_get_endpoint_flag(&host_ep->desc);
  2875. if (ep_flag == SLOT_FLAG || ep_flag == EP0_FLAG)
  2876. return;
  2877. stop_cmd = xhci_alloc_command(xhci, true, GFP_NOWAIT);
  2878. if (!stop_cmd)
  2879. return;
  2880. cfg_cmd = xhci_alloc_command_with_ctx(xhci, true, GFP_NOWAIT);
  2881. if (!cfg_cmd)
  2882. goto cleanup;
  2883. spin_lock_irqsave(&xhci->lock, flags);
  2884. /* block queuing new trbs and ringing ep doorbell */
  2885. ep->ep_state |= EP_SOFT_CLEAR_TOGGLE;
  2886. /*
  2887. * Make sure endpoint ring is empty before resetting the toggle/seq.
  2888. * Driver is required to synchronously cancel all transfer request.
  2889. * Stop the endpoint to force xHC to update the output context
  2890. */
  2891. if (!list_empty(&ep->ring->td_list)) {
  2892. dev_err(&udev->dev, "EP not empty, refuse reset\n");
  2893. spin_unlock_irqrestore(&xhci->lock, flags);
  2894. xhci_free_command(xhci, cfg_cmd);
  2895. goto cleanup;
  2896. }
  2897. err = xhci_queue_stop_endpoint(xhci, stop_cmd, udev->slot_id,
  2898. ep_index, 0);
  2899. if (err < 0) {
  2900. spin_unlock_irqrestore(&xhci->lock, flags);
  2901. xhci_free_command(xhci, cfg_cmd);
  2902. xhci_dbg(xhci, "%s: Failed to queue stop ep command, %d ",
  2903. __func__, err);
  2904. goto cleanup;
  2905. }
  2906. xhci_ring_cmd_db(xhci);
  2907. spin_unlock_irqrestore(&xhci->lock, flags);
  2908. wait_for_completion(stop_cmd->completion);
  2909. err = xhci_vendor_sync_dev_ctx(xhci, udev->slot_id);
  2910. if (err) {
  2911. xhci_warn(xhci, "%s: Failed to sync device context failed, err=%d",
  2912. __func__, err);
  2913. goto cleanup;
  2914. }
  2915. spin_lock_irqsave(&xhci->lock, flags);
  2916. /* config ep command clears toggle if add and drop ep flags are set */
  2917. ctrl_ctx = xhci_get_input_control_ctx(cfg_cmd->in_ctx);
  2918. if (!ctrl_ctx) {
  2919. spin_unlock_irqrestore(&xhci->lock, flags);
  2920. xhci_free_command(xhci, cfg_cmd);
  2921. xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
  2922. __func__);
  2923. goto cleanup;
  2924. }
  2925. xhci_setup_input_ctx_for_config_ep(xhci, cfg_cmd->in_ctx, vdev->out_ctx,
  2926. ctrl_ctx, ep_flag, ep_flag);
  2927. xhci_endpoint_copy(xhci, cfg_cmd->in_ctx, vdev->out_ctx, ep_index);
  2928. err = xhci_queue_configure_endpoint(xhci, cfg_cmd, cfg_cmd->in_ctx->dma,
  2929. udev->slot_id, false);
  2930. if (err < 0) {
  2931. spin_unlock_irqrestore(&xhci->lock, flags);
  2932. xhci_free_command(xhci, cfg_cmd);
  2933. xhci_dbg(xhci, "%s: Failed to queue config ep command, %d ",
  2934. __func__, err);
  2935. goto cleanup;
  2936. }
  2937. xhci_ring_cmd_db(xhci);
  2938. spin_unlock_irqrestore(&xhci->lock, flags);
  2939. wait_for_completion(cfg_cmd->completion);
  2940. err = xhci_vendor_sync_dev_ctx(xhci, udev->slot_id);
  2941. if (err)
  2942. xhci_warn(xhci, "%s: Failed to sync device context failed, err=%d",
  2943. __func__, err);
  2944. xhci_free_command(xhci, cfg_cmd);
  2945. cleanup:
  2946. xhci_free_command(xhci, stop_cmd);
  2947. spin_lock_irqsave(&xhci->lock, flags);
  2948. if (ep->ep_state & EP_SOFT_CLEAR_TOGGLE)
  2949. ep->ep_state &= ~EP_SOFT_CLEAR_TOGGLE;
  2950. spin_unlock_irqrestore(&xhci->lock, flags);
  2951. }
  2952. static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
  2953. struct usb_device *udev, struct usb_host_endpoint *ep,
  2954. unsigned int slot_id)
  2955. {
  2956. int ret;
  2957. unsigned int ep_index;
  2958. unsigned int ep_state;
  2959. if (!ep)
  2960. return -EINVAL;
  2961. ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__);
  2962. if (ret <= 0)
  2963. return ret ? ret : -EINVAL;
  2964. if (usb_ss_max_streams(&ep->ss_ep_comp) == 0) {
  2965. xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
  2966. " descriptor for ep 0x%x does not support streams\n",
  2967. ep->desc.bEndpointAddress);
  2968. return -EINVAL;
  2969. }
  2970. ep_index = xhci_get_endpoint_index(&ep->desc);
  2971. ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
  2972. if (ep_state & EP_HAS_STREAMS ||
  2973. ep_state & EP_GETTING_STREAMS) {
  2974. xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
  2975. "already has streams set up.\n",
  2976. ep->desc.bEndpointAddress);
  2977. xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
  2978. "dynamic stream context array reallocation.\n");
  2979. return -EINVAL;
  2980. }
  2981. if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
  2982. xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
  2983. "endpoint 0x%x; URBs are pending.\n",
  2984. ep->desc.bEndpointAddress);
  2985. return -EINVAL;
  2986. }
  2987. return 0;
  2988. }
  2989. static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
  2990. unsigned int *num_streams, unsigned int *num_stream_ctxs)
  2991. {
  2992. unsigned int max_streams;
  2993. /* The stream context array size must be a power of two */
  2994. *num_stream_ctxs = roundup_pow_of_two(*num_streams);
  2995. /*
  2996. * Find out how many primary stream array entries the host controller
  2997. * supports. Later we may use secondary stream arrays (similar to 2nd
  2998. * level page entries), but that's an optional feature for xHCI host
  2999. * controllers. xHCs must support at least 4 stream IDs.
  3000. */
  3001. max_streams = HCC_MAX_PSA(xhci->hcc_params);
  3002. if (*num_stream_ctxs > max_streams) {
  3003. xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
  3004. max_streams);
  3005. *num_stream_ctxs = max_streams;
  3006. *num_streams = max_streams;
  3007. }
  3008. }
  3009. /* Returns an error code if one of the endpoint already has streams.
  3010. * This does not change any data structures, it only checks and gathers
  3011. * information.
  3012. */
  3013. static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
  3014. struct usb_device *udev,
  3015. struct usb_host_endpoint **eps, unsigned int num_eps,
  3016. unsigned int *num_streams, u32 *changed_ep_bitmask)
  3017. {
  3018. unsigned int max_streams;
  3019. unsigned int endpoint_flag;
  3020. int i;
  3021. int ret;
  3022. for (i = 0; i < num_eps; i++) {
  3023. ret = xhci_check_streams_endpoint(xhci, udev,
  3024. eps[i], udev->slot_id);
  3025. if (ret < 0)
  3026. return ret;
  3027. max_streams = usb_ss_max_streams(&eps[i]->ss_ep_comp);
  3028. if (max_streams < (*num_streams - 1)) {
  3029. xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
  3030. eps[i]->desc.bEndpointAddress,
  3031. max_streams);
  3032. *num_streams = max_streams+1;
  3033. }
  3034. endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
  3035. if (*changed_ep_bitmask & endpoint_flag)
  3036. return -EINVAL;
  3037. *changed_ep_bitmask |= endpoint_flag;
  3038. }
  3039. return 0;
  3040. }
  3041. static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
  3042. struct usb_device *udev,
  3043. struct usb_host_endpoint **eps, unsigned int num_eps)
  3044. {
  3045. u32 changed_ep_bitmask = 0;
  3046. unsigned int slot_id;
  3047. unsigned int ep_index;
  3048. unsigned int ep_state;
  3049. int i;
  3050. slot_id = udev->slot_id;
  3051. if (!xhci->devs[slot_id])
  3052. return 0;
  3053. for (i = 0; i < num_eps; i++) {
  3054. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  3055. ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
  3056. /* Are streams already being freed for the endpoint? */
  3057. if (ep_state & EP_GETTING_NO_STREAMS) {
  3058. xhci_warn(xhci, "WARN Can't disable streams for "
  3059. "endpoint 0x%x, "
  3060. "streams are being disabled already\n",
  3061. eps[i]->desc.bEndpointAddress);
  3062. return 0;
  3063. }
  3064. /* Are there actually any streams to free? */
  3065. if (!(ep_state & EP_HAS_STREAMS) &&
  3066. !(ep_state & EP_GETTING_STREAMS)) {
  3067. xhci_warn(xhci, "WARN Can't disable streams for "
  3068. "endpoint 0x%x, "
  3069. "streams are already disabled!\n",
  3070. eps[i]->desc.bEndpointAddress);
  3071. xhci_warn(xhci, "WARN xhci_free_streams() called "
  3072. "with non-streams endpoint\n");
  3073. return 0;
  3074. }
  3075. changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
  3076. }
  3077. return changed_ep_bitmask;
  3078. }
  3079. /*
  3080. * The USB device drivers use this function (through the HCD interface in USB
  3081. * core) to prepare a set of bulk endpoints to use streams. Streams are used to
  3082. * coordinate mass storage command queueing across multiple endpoints (basically
  3083. * a stream ID == a task ID).
  3084. *
  3085. * Setting up streams involves allocating the same size stream context array
  3086. * for each endpoint and issuing a configure endpoint command for all endpoints.
  3087. *
  3088. * Don't allow the call to succeed if one endpoint only supports one stream
  3089. * (which means it doesn't support streams at all).
  3090. *
  3091. * Drivers may get less stream IDs than they asked for, if the host controller
  3092. * hardware or endpoints claim they can't support the number of requested
  3093. * stream IDs.
  3094. */
  3095. static int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
  3096. struct usb_host_endpoint **eps, unsigned int num_eps,
  3097. unsigned int num_streams, gfp_t mem_flags)
  3098. {
  3099. int i, ret;
  3100. struct xhci_hcd *xhci;
  3101. struct xhci_virt_device *vdev;
  3102. struct xhci_command *config_cmd;
  3103. struct xhci_input_control_ctx *ctrl_ctx;
  3104. unsigned int ep_index;
  3105. unsigned int num_stream_ctxs;
  3106. unsigned int max_packet;
  3107. unsigned long flags;
  3108. u32 changed_ep_bitmask = 0;
  3109. if (!eps)
  3110. return -EINVAL;
  3111. /* Add one to the number of streams requested to account for
  3112. * stream 0 that is reserved for xHCI usage.
  3113. */
  3114. num_streams += 1;
  3115. xhci = hcd_to_xhci(hcd);
  3116. xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
  3117. num_streams);
  3118. /* MaxPSASize value 0 (2 streams) means streams are not supported */
  3119. if ((xhci->quirks & XHCI_BROKEN_STREAMS) ||
  3120. HCC_MAX_PSA(xhci->hcc_params) < 4) {
  3121. xhci_dbg(xhci, "xHCI controller does not support streams.\n");
  3122. return -ENOSYS;
  3123. }
  3124. config_cmd = xhci_alloc_command_with_ctx(xhci, true, mem_flags);
  3125. if (!config_cmd)
  3126. return -ENOMEM;
  3127. ctrl_ctx = xhci_get_input_control_ctx(config_cmd->in_ctx);
  3128. if (!ctrl_ctx) {
  3129. xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
  3130. __func__);
  3131. xhci_free_command(xhci, config_cmd);
  3132. return -ENOMEM;
  3133. }
  3134. /* Check to make sure all endpoints are not already configured for
  3135. * streams. While we're at it, find the maximum number of streams that
  3136. * all the endpoints will support and check for duplicate endpoints.
  3137. */
  3138. spin_lock_irqsave(&xhci->lock, flags);
  3139. ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
  3140. num_eps, &num_streams, &changed_ep_bitmask);
  3141. if (ret < 0) {
  3142. xhci_free_command(xhci, config_cmd);
  3143. spin_unlock_irqrestore(&xhci->lock, flags);
  3144. return ret;
  3145. }
  3146. if (num_streams <= 1) {
  3147. xhci_warn(xhci, "WARN: endpoints can't handle "
  3148. "more than one stream.\n");
  3149. xhci_free_command(xhci, config_cmd);
  3150. spin_unlock_irqrestore(&xhci->lock, flags);
  3151. return -EINVAL;
  3152. }
  3153. vdev = xhci->devs[udev->slot_id];
  3154. /* Mark each endpoint as being in transition, so
  3155. * xhci_urb_enqueue() will reject all URBs.
  3156. */
  3157. for (i = 0; i < num_eps; i++) {
  3158. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  3159. vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
  3160. }
  3161. spin_unlock_irqrestore(&xhci->lock, flags);
  3162. /* Setup internal data structures and allocate HW data structures for
  3163. * streams (but don't install the HW structures in the input context
  3164. * until we're sure all memory allocation succeeded).
  3165. */
  3166. xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
  3167. xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
  3168. num_stream_ctxs, num_streams);
  3169. for (i = 0; i < num_eps; i++) {
  3170. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  3171. max_packet = usb_endpoint_maxp(&eps[i]->desc);
  3172. vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
  3173. num_stream_ctxs,
  3174. num_streams,
  3175. max_packet, mem_flags);
  3176. if (!vdev->eps[ep_index].stream_info)
  3177. goto cleanup;
  3178. /* Set maxPstreams in endpoint context and update deq ptr to
  3179. * point to stream context array. FIXME
  3180. */
  3181. }
  3182. /* Set up the input context for a configure endpoint command. */
  3183. for (i = 0; i < num_eps; i++) {
  3184. struct xhci_ep_ctx *ep_ctx;
  3185. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  3186. ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
  3187. xhci_endpoint_copy(xhci, config_cmd->in_ctx,
  3188. vdev->out_ctx, ep_index);
  3189. xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
  3190. vdev->eps[ep_index].stream_info);
  3191. }
  3192. /* Tell the HW to drop its old copy of the endpoint context info
  3193. * and add the updated copy from the input context.
  3194. */
  3195. xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
  3196. vdev->out_ctx, ctrl_ctx,
  3197. changed_ep_bitmask, changed_ep_bitmask);
  3198. /* Issue and wait for the configure endpoint command */
  3199. ret = xhci_configure_endpoint(xhci, udev, config_cmd,
  3200. false, false);
  3201. /* xHC rejected the configure endpoint command for some reason, so we
  3202. * leave the old ring intact and free our internal streams data
  3203. * structure.
  3204. */
  3205. if (ret < 0)
  3206. goto cleanup;
  3207. spin_lock_irqsave(&xhci->lock, flags);
  3208. for (i = 0; i < num_eps; i++) {
  3209. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  3210. vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
  3211. xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
  3212. udev->slot_id, ep_index);
  3213. vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
  3214. }
  3215. xhci_free_command(xhci, config_cmd);
  3216. spin_unlock_irqrestore(&xhci->lock, flags);
  3217. for (i = 0; i < num_eps; i++) {
  3218. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  3219. xhci_debugfs_create_stream_files(xhci, vdev, ep_index);
  3220. }
  3221. /* Subtract 1 for stream 0, which drivers can't use */
  3222. return num_streams - 1;
  3223. cleanup:
  3224. /* If it didn't work, free the streams! */
  3225. for (i = 0; i < num_eps; i++) {
  3226. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  3227. xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
  3228. vdev->eps[ep_index].stream_info = NULL;
  3229. /* FIXME Unset maxPstreams in endpoint context and
  3230. * update deq ptr to point to normal string ring.
  3231. */
  3232. vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
  3233. vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
  3234. xhci_endpoint_zero(xhci, vdev, eps[i]);
  3235. }
  3236. xhci_free_command(xhci, config_cmd);
  3237. return -ENOMEM;
  3238. }
  3239. /* Transition the endpoint from using streams to being a "normal" endpoint
  3240. * without streams.
  3241. *
  3242. * Modify the endpoint context state, submit a configure endpoint command,
  3243. * and free all endpoint rings for streams if that completes successfully.
  3244. */
  3245. static int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
  3246. struct usb_host_endpoint **eps, unsigned int num_eps,
  3247. gfp_t mem_flags)
  3248. {
  3249. int i, ret;
  3250. struct xhci_hcd *xhci;
  3251. struct xhci_virt_device *vdev;
  3252. struct xhci_command *command;
  3253. struct xhci_input_control_ctx *ctrl_ctx;
  3254. unsigned int ep_index;
  3255. unsigned long flags;
  3256. u32 changed_ep_bitmask;
  3257. xhci = hcd_to_xhci(hcd);
  3258. vdev = xhci->devs[udev->slot_id];
  3259. /* Set up a configure endpoint command to remove the streams rings */
  3260. spin_lock_irqsave(&xhci->lock, flags);
  3261. changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
  3262. udev, eps, num_eps);
  3263. if (changed_ep_bitmask == 0) {
  3264. spin_unlock_irqrestore(&xhci->lock, flags);
  3265. return -EINVAL;
  3266. }
  3267. /* Use the xhci_command structure from the first endpoint. We may have
  3268. * allocated too many, but the driver may call xhci_free_streams() for
  3269. * each endpoint it grouped into one call to xhci_alloc_streams().
  3270. */
  3271. ep_index = xhci_get_endpoint_index(&eps[0]->desc);
  3272. command = vdev->eps[ep_index].stream_info->free_streams_command;
  3273. ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
  3274. if (!ctrl_ctx) {
  3275. spin_unlock_irqrestore(&xhci->lock, flags);
  3276. xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
  3277. __func__);
  3278. return -EINVAL;
  3279. }
  3280. for (i = 0; i < num_eps; i++) {
  3281. struct xhci_ep_ctx *ep_ctx;
  3282. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  3283. ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
  3284. xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
  3285. EP_GETTING_NO_STREAMS;
  3286. xhci_endpoint_copy(xhci, command->in_ctx,
  3287. vdev->out_ctx, ep_index);
  3288. xhci_setup_no_streams_ep_input_ctx(ep_ctx,
  3289. &vdev->eps[ep_index]);
  3290. }
  3291. xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
  3292. vdev->out_ctx, ctrl_ctx,
  3293. changed_ep_bitmask, changed_ep_bitmask);
  3294. spin_unlock_irqrestore(&xhci->lock, flags);
  3295. /* Issue and wait for the configure endpoint command,
  3296. * which must succeed.
  3297. */
  3298. ret = xhci_configure_endpoint(xhci, udev, command,
  3299. false, true);
  3300. /* xHC rejected the configure endpoint command for some reason, so we
  3301. * leave the streams rings intact.
  3302. */
  3303. if (ret < 0)
  3304. return ret;
  3305. spin_lock_irqsave(&xhci->lock, flags);
  3306. for (i = 0; i < num_eps; i++) {
  3307. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  3308. xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
  3309. vdev->eps[ep_index].stream_info = NULL;
  3310. /* FIXME Unset maxPstreams in endpoint context and
  3311. * update deq ptr to point to normal string ring.
  3312. */
  3313. vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
  3314. vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
  3315. }
  3316. spin_unlock_irqrestore(&xhci->lock, flags);
  3317. return 0;
  3318. }
  3319. /*
  3320. * Deletes endpoint resources for endpoints that were active before a Reset
  3321. * Device command, or a Disable Slot command. The Reset Device command leaves
  3322. * the control endpoint intact, whereas the Disable Slot command deletes it.
  3323. *
  3324. * Must be called with xhci->lock held.
  3325. */
  3326. void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci,
  3327. struct xhci_virt_device *virt_dev, bool drop_control_ep)
  3328. {
  3329. int i;
  3330. unsigned int num_dropped_eps = 0;
  3331. unsigned int drop_flags = 0;
  3332. for (i = (drop_control_ep ? 0 : 1); i < 31; i++) {
  3333. if (virt_dev->eps[i].ring) {
  3334. drop_flags |= 1 << i;
  3335. num_dropped_eps++;
  3336. }
  3337. }
  3338. xhci->num_active_eps -= num_dropped_eps;
  3339. if (num_dropped_eps)
  3340. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  3341. "Dropped %u ep ctxs, flags = 0x%x, "
  3342. "%u now active.",
  3343. num_dropped_eps, drop_flags,
  3344. xhci->num_active_eps);
  3345. }
  3346. /*
  3347. * This submits a Reset Device Command, which will set the device state to 0,
  3348. * set the device address to 0, and disable all the endpoints except the default
  3349. * control endpoint. The USB core should come back and call
  3350. * xhci_address_device(), and then re-set up the configuration. If this is
  3351. * called because of a usb_reset_and_verify_device(), then the old alternate
  3352. * settings will be re-installed through the normal bandwidth allocation
  3353. * functions.
  3354. *
  3355. * Wait for the Reset Device command to finish. Remove all structures
  3356. * associated with the endpoints that were disabled. Clear the input device
  3357. * structure? Reset the control endpoint 0 max packet size?
  3358. *
  3359. * If the virt_dev to be reset does not exist or does not match the udev,
  3360. * it means the device is lost, possibly due to the xHC restore error and
  3361. * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
  3362. * re-allocate the device.
  3363. */
  3364. static int xhci_discover_or_reset_device(struct usb_hcd *hcd,
  3365. struct usb_device *udev)
  3366. {
  3367. int ret, i;
  3368. unsigned long flags;
  3369. struct xhci_hcd *xhci;
  3370. unsigned int slot_id;
  3371. struct xhci_virt_device *virt_dev;
  3372. struct xhci_command *reset_device_cmd;
  3373. struct xhci_slot_ctx *slot_ctx;
  3374. int old_active_eps = 0;
  3375. ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__);
  3376. if (ret <= 0)
  3377. return ret;
  3378. xhci = hcd_to_xhci(hcd);
  3379. slot_id = udev->slot_id;
  3380. virt_dev = xhci->devs[slot_id];
  3381. if (!virt_dev) {
  3382. xhci_dbg(xhci, "The device to be reset with slot ID %u does "
  3383. "not exist. Re-allocate the device\n", slot_id);
  3384. ret = xhci_alloc_dev(hcd, udev);
  3385. if (ret == 1)
  3386. return 0;
  3387. else
  3388. return -EINVAL;
  3389. }
  3390. if (virt_dev->tt_info)
  3391. old_active_eps = virt_dev->tt_info->active_eps;
  3392. if (virt_dev->udev != udev) {
  3393. /* If the virt_dev and the udev does not match, this virt_dev
  3394. * may belong to another udev.
  3395. * Re-allocate the device.
  3396. */
  3397. xhci_dbg(xhci, "The device to be reset with slot ID %u does "
  3398. "not match the udev. Re-allocate the device\n",
  3399. slot_id);
  3400. ret = xhci_alloc_dev(hcd, udev);
  3401. if (ret == 1)
  3402. return 0;
  3403. else
  3404. return -EINVAL;
  3405. }
  3406. /* If device is not setup, there is no point in resetting it */
  3407. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
  3408. if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
  3409. SLOT_STATE_DISABLED)
  3410. return 0;
  3411. trace_xhci_discover_or_reset_device(slot_ctx);
  3412. xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
  3413. /* Allocate the command structure that holds the struct completion.
  3414. * Assume we're in process context, since the normal device reset
  3415. * process has to wait for the device anyway. Storage devices are
  3416. * reset as part of error handling, so use GFP_NOIO instead of
  3417. * GFP_KERNEL.
  3418. */
  3419. reset_device_cmd = xhci_alloc_command(xhci, true, GFP_NOIO);
  3420. if (!reset_device_cmd) {
  3421. xhci_dbg(xhci, "Couldn't allocate command structure.\n");
  3422. return -ENOMEM;
  3423. }
  3424. /* Attempt to submit the Reset Device command to the command ring */
  3425. spin_lock_irqsave(&xhci->lock, flags);
  3426. ret = xhci_queue_reset_device(xhci, reset_device_cmd, slot_id);
  3427. if (ret) {
  3428. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  3429. spin_unlock_irqrestore(&xhci->lock, flags);
  3430. goto command_cleanup;
  3431. }
  3432. xhci_ring_cmd_db(xhci);
  3433. spin_unlock_irqrestore(&xhci->lock, flags);
  3434. /* Wait for the Reset Device command to finish */
  3435. wait_for_completion(reset_device_cmd->completion);
  3436. ret = xhci_vendor_sync_dev_ctx(xhci, slot_id);
  3437. if (ret) {
  3438. xhci_warn(xhci, "%s: Failed to sync device context failed, err=%d",
  3439. __func__, ret);
  3440. goto command_cleanup;
  3441. }
  3442. /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
  3443. * unless we tried to reset a slot ID that wasn't enabled,
  3444. * or the device wasn't in the addressed or configured state.
  3445. */
  3446. ret = reset_device_cmd->status;
  3447. switch (ret) {
  3448. case COMP_COMMAND_ABORTED:
  3449. case COMP_COMMAND_RING_STOPPED:
  3450. xhci_warn(xhci, "Timeout waiting for reset device command\n");
  3451. ret = -ETIME;
  3452. goto command_cleanup;
  3453. case COMP_SLOT_NOT_ENABLED_ERROR: /* 0.95 completion for bad slot ID */
  3454. case COMP_CONTEXT_STATE_ERROR: /* 0.96 completion code for same thing */
  3455. xhci_dbg(xhci, "Can't reset device (slot ID %u) in %s state\n",
  3456. slot_id,
  3457. xhci_get_slot_state(xhci, virt_dev->out_ctx));
  3458. xhci_dbg(xhci, "Not freeing device rings.\n");
  3459. /* Don't treat this as an error. May change my mind later. */
  3460. ret = 0;
  3461. goto command_cleanup;
  3462. case COMP_SUCCESS:
  3463. xhci_dbg(xhci, "Successful reset device command.\n");
  3464. break;
  3465. default:
  3466. if (xhci_is_vendor_info_code(xhci, ret))
  3467. break;
  3468. xhci_warn(xhci, "Unknown completion code %u for "
  3469. "reset device command.\n", ret);
  3470. ret = -EINVAL;
  3471. goto command_cleanup;
  3472. }
  3473. /* Free up host controller endpoint resources */
  3474. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
  3475. spin_lock_irqsave(&xhci->lock, flags);
  3476. /* Don't delete the default control endpoint resources */
  3477. xhci_free_device_endpoint_resources(xhci, virt_dev, false);
  3478. spin_unlock_irqrestore(&xhci->lock, flags);
  3479. }
  3480. /* Everything but endpoint 0 is disabled, so free the rings. */
  3481. for (i = 1; i < 31; i++) {
  3482. struct xhci_virt_ep *ep = &virt_dev->eps[i];
  3483. if (ep->ep_state & EP_HAS_STREAMS) {
  3484. xhci_warn(xhci, "WARN: endpoint 0x%02x has streams on device reset, freeing streams.\n",
  3485. xhci_get_endpoint_address(i));
  3486. xhci_free_stream_info(xhci, ep->stream_info);
  3487. ep->stream_info = NULL;
  3488. ep->ep_state &= ~EP_HAS_STREAMS;
  3489. }
  3490. if (ep->ring) {
  3491. xhci_debugfs_remove_endpoint(xhci, virt_dev, i);
  3492. xhci_free_endpoint_ring(xhci, virt_dev, i);
  3493. }
  3494. if (!list_empty(&virt_dev->eps[i].bw_endpoint_list))
  3495. xhci_drop_ep_from_interval_table(xhci,
  3496. &virt_dev->eps[i].bw_info,
  3497. virt_dev->bw_table,
  3498. udev,
  3499. &virt_dev->eps[i],
  3500. virt_dev->tt_info);
  3501. xhci_clear_endpoint_bw_info(&virt_dev->eps[i].bw_info);
  3502. }
  3503. /* If necessary, update the number of active TTs on this root port */
  3504. xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
  3505. virt_dev->flags = 0;
  3506. ret = 0;
  3507. command_cleanup:
  3508. xhci_free_command(xhci, reset_device_cmd);
  3509. return ret;
  3510. }
  3511. /*
  3512. * At this point, the struct usb_device is about to go away, the device has
  3513. * disconnected, and all traffic has been stopped and the endpoints have been
  3514. * disabled. Free any HC data structures associated with that device.
  3515. */
  3516. static void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
  3517. {
  3518. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  3519. struct xhci_virt_device *virt_dev;
  3520. struct xhci_slot_ctx *slot_ctx;
  3521. unsigned long flags;
  3522. int i, ret;
  3523. /*
  3524. * We called pm_runtime_get_noresume when the device was attached.
  3525. * Decrement the counter here to allow controller to runtime suspend
  3526. * if no devices remain.
  3527. */
  3528. if (xhci->quirks & XHCI_RESET_ON_RESUME)
  3529. pm_runtime_put_noidle(hcd->self.controller);
  3530. ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
  3531. /* If the host is halted due to driver unload, we still need to free the
  3532. * device.
  3533. */
  3534. if (ret <= 0 && ret != -ENODEV)
  3535. return;
  3536. virt_dev = xhci->devs[udev->slot_id];
  3537. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
  3538. trace_xhci_free_dev(slot_ctx);
  3539. /* Stop any wayward timer functions (which may grab the lock) */
  3540. for (i = 0; i < 31; i++)
  3541. virt_dev->eps[i].ep_state &= ~EP_STOP_CMD_PENDING;
  3542. virt_dev->udev = NULL;
  3543. xhci_disable_slot(xhci, udev->slot_id);
  3544. spin_lock_irqsave(&xhci->lock, flags);
  3545. xhci_free_virt_device(xhci, udev->slot_id);
  3546. spin_unlock_irqrestore(&xhci->lock, flags);
  3547. }
  3548. int xhci_disable_slot(struct xhci_hcd *xhci, u32 slot_id)
  3549. {
  3550. struct xhci_command *command;
  3551. unsigned long flags;
  3552. u32 state;
  3553. int ret;
  3554. command = xhci_alloc_command(xhci, true, GFP_KERNEL);
  3555. if (!command)
  3556. return -ENOMEM;
  3557. xhci_debugfs_remove_slot(xhci, slot_id);
  3558. spin_lock_irqsave(&xhci->lock, flags);
  3559. /* Don't disable the slot if the host controller is dead. */
  3560. state = readl(&xhci->op_regs->status);
  3561. if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
  3562. (xhci->xhc_state & XHCI_STATE_HALTED)) {
  3563. spin_unlock_irqrestore(&xhci->lock, flags);
  3564. kfree(command);
  3565. return -ENODEV;
  3566. }
  3567. ret = xhci_queue_slot_control(xhci, command, TRB_DISABLE_SLOT,
  3568. slot_id);
  3569. if (ret) {
  3570. spin_unlock_irqrestore(&xhci->lock, flags);
  3571. kfree(command);
  3572. return ret;
  3573. }
  3574. xhci_ring_cmd_db(xhci);
  3575. spin_unlock_irqrestore(&xhci->lock, flags);
  3576. wait_for_completion(command->completion);
  3577. if (command->status != COMP_SUCCESS)
  3578. xhci_warn(xhci, "Unsuccessful disable slot %u command, status %d\n",
  3579. slot_id, command->status);
  3580. xhci_free_command(xhci, command);
  3581. return 0;
  3582. }
  3583. /*
  3584. * Checks if we have enough host controller resources for the default control
  3585. * endpoint.
  3586. *
  3587. * Must be called with xhci->lock held.
  3588. */
  3589. static int xhci_reserve_host_control_ep_resources(struct xhci_hcd *xhci)
  3590. {
  3591. if (xhci->num_active_eps + 1 > xhci->limit_active_eps) {
  3592. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  3593. "Not enough ep ctxs: "
  3594. "%u active, need to add 1, limit is %u.",
  3595. xhci->num_active_eps, xhci->limit_active_eps);
  3596. return -ENOMEM;
  3597. }
  3598. xhci->num_active_eps += 1;
  3599. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  3600. "Adding 1 ep ctx, %u now active.",
  3601. xhci->num_active_eps);
  3602. return 0;
  3603. }
  3604. /*
  3605. * Returns 0 if the xHC ran out of device slots, the Enable Slot command
  3606. * timed out, or allocating memory failed. Returns 1 on success.
  3607. */
  3608. int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
  3609. {
  3610. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  3611. struct xhci_virt_device *vdev;
  3612. struct xhci_slot_ctx *slot_ctx;
  3613. unsigned long flags;
  3614. int ret, slot_id;
  3615. struct xhci_command *command;
  3616. command = xhci_alloc_command(xhci, true, GFP_KERNEL);
  3617. if (!command)
  3618. return 0;
  3619. spin_lock_irqsave(&xhci->lock, flags);
  3620. ret = xhci_queue_slot_control(xhci, command, TRB_ENABLE_SLOT, 0);
  3621. if (ret) {
  3622. spin_unlock_irqrestore(&xhci->lock, flags);
  3623. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  3624. xhci_free_command(xhci, command);
  3625. return 0;
  3626. }
  3627. xhci_ring_cmd_db(xhci);
  3628. spin_unlock_irqrestore(&xhci->lock, flags);
  3629. wait_for_completion(command->completion);
  3630. slot_id = command->slot_id;
  3631. if (!slot_id || command->status != COMP_SUCCESS) {
  3632. xhci_err(xhci, "Error while assigning device slot ID: %s\n",
  3633. xhci_trb_comp_code_string(command->status));
  3634. xhci_err(xhci, "Max number of devices this xHCI host supports is %u.\n",
  3635. HCS_MAX_SLOTS(
  3636. readl(&xhci->cap_regs->hcs_params1)));
  3637. xhci_free_command(xhci, command);
  3638. return 0;
  3639. }
  3640. xhci_free_command(xhci, command);
  3641. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
  3642. spin_lock_irqsave(&xhci->lock, flags);
  3643. ret = xhci_reserve_host_control_ep_resources(xhci);
  3644. if (ret) {
  3645. spin_unlock_irqrestore(&xhci->lock, flags);
  3646. xhci_warn(xhci, "Not enough host resources, "
  3647. "active endpoint contexts = %u\n",
  3648. xhci->num_active_eps);
  3649. goto disable_slot;
  3650. }
  3651. spin_unlock_irqrestore(&xhci->lock, flags);
  3652. }
  3653. /* Use GFP_NOIO, since this function can be called from
  3654. * xhci_discover_or_reset_device(), which may be called as part of
  3655. * mass storage driver error handling.
  3656. */
  3657. if (!xhci_alloc_virt_device(xhci, slot_id, udev, GFP_NOIO)) {
  3658. xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
  3659. goto disable_slot;
  3660. }
  3661. ret = xhci_vendor_sync_dev_ctx(xhci, slot_id);
  3662. if (ret) {
  3663. xhci_warn(xhci, "%s: Failed to sync device context failed, err=%d",
  3664. __func__, ret);
  3665. goto disable_slot;
  3666. }
  3667. vdev = xhci->devs[slot_id];
  3668. slot_ctx = xhci_get_slot_ctx(xhci, vdev->out_ctx);
  3669. trace_xhci_alloc_dev(slot_ctx);
  3670. udev->slot_id = slot_id;
  3671. xhci_debugfs_create_slot(xhci, slot_id);
  3672. /*
  3673. * If resetting upon resume, we can't put the controller into runtime
  3674. * suspend if there is a device attached.
  3675. */
  3676. if (xhci->quirks & XHCI_RESET_ON_RESUME)
  3677. pm_runtime_get_noresume(hcd->self.controller);
  3678. /* Is this a LS or FS device under a HS hub? */
  3679. /* Hub or peripherial? */
  3680. return 1;
  3681. disable_slot:
  3682. xhci_disable_slot(xhci, udev->slot_id);
  3683. xhci_free_virt_device(xhci, udev->slot_id);
  3684. return 0;
  3685. }
  3686. /*
  3687. * Issue an Address Device command and optionally send a corresponding
  3688. * SetAddress request to the device.
  3689. */
  3690. static int xhci_setup_device(struct usb_hcd *hcd, struct usb_device *udev,
  3691. enum xhci_setup_dev setup)
  3692. {
  3693. const char *act = setup == SETUP_CONTEXT_ONLY ? "context" : "address";
  3694. unsigned long flags;
  3695. struct xhci_virt_device *virt_dev;
  3696. int ret = 0;
  3697. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  3698. struct xhci_slot_ctx *slot_ctx;
  3699. struct xhci_input_control_ctx *ctrl_ctx;
  3700. u64 temp_64;
  3701. struct xhci_command *command = NULL;
  3702. mutex_lock(&xhci->mutex);
  3703. if (xhci->xhc_state) { /* dying, removing or halted */
  3704. ret = -ESHUTDOWN;
  3705. goto out;
  3706. }
  3707. if (!udev->slot_id) {
  3708. xhci_dbg_trace(xhci, trace_xhci_dbg_address,
  3709. "Bad Slot ID %d", udev->slot_id);
  3710. ret = -EINVAL;
  3711. goto out;
  3712. }
  3713. virt_dev = xhci->devs[udev->slot_id];
  3714. if (WARN_ON(!virt_dev)) {
  3715. /*
  3716. * In plug/unplug torture test with an NEC controller,
  3717. * a zero-dereference was observed once due to virt_dev = 0.
  3718. * Print useful debug rather than crash if it is observed again!
  3719. */
  3720. xhci_warn(xhci, "Virt dev invalid for slot_id 0x%x!\n",
  3721. udev->slot_id);
  3722. ret = -EINVAL;
  3723. goto out;
  3724. }
  3725. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
  3726. trace_xhci_setup_device_slot(slot_ctx);
  3727. if (setup == SETUP_CONTEXT_ONLY) {
  3728. if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
  3729. SLOT_STATE_DEFAULT) {
  3730. xhci_dbg(xhci, "Slot already in default state\n");
  3731. goto out;
  3732. }
  3733. }
  3734. command = xhci_alloc_command(xhci, true, GFP_KERNEL);
  3735. if (!command) {
  3736. ret = -ENOMEM;
  3737. goto out;
  3738. }
  3739. command->in_ctx = virt_dev->in_ctx;
  3740. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  3741. ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
  3742. if (!ctrl_ctx) {
  3743. xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
  3744. __func__);
  3745. ret = -EINVAL;
  3746. goto out;
  3747. }
  3748. /*
  3749. * If this is the first Set Address since device plug-in or
  3750. * virt_device realloaction after a resume with an xHCI power loss,
  3751. * then set up the slot context.
  3752. */
  3753. if (!slot_ctx->dev_info)
  3754. xhci_setup_addressable_virt_dev(xhci, udev);
  3755. /* Otherwise, update the control endpoint ring enqueue pointer. */
  3756. else
  3757. xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
  3758. ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
  3759. ctrl_ctx->drop_flags = 0;
  3760. trace_xhci_address_ctx(xhci, virt_dev->in_ctx,
  3761. le32_to_cpu(slot_ctx->dev_info) >> 27);
  3762. trace_xhci_address_ctrl_ctx(ctrl_ctx);
  3763. spin_lock_irqsave(&xhci->lock, flags);
  3764. trace_xhci_setup_device(virt_dev);
  3765. ret = xhci_queue_address_device(xhci, command, virt_dev->in_ctx->dma,
  3766. udev->slot_id, setup);
  3767. if (ret) {
  3768. spin_unlock_irqrestore(&xhci->lock, flags);
  3769. xhci_dbg_trace(xhci, trace_xhci_dbg_address,
  3770. "FIXME: allocate a command ring segment");
  3771. goto out;
  3772. }
  3773. xhci_ring_cmd_db(xhci);
  3774. spin_unlock_irqrestore(&xhci->lock, flags);
  3775. /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
  3776. wait_for_completion(command->completion);
  3777. ret = xhci_vendor_sync_dev_ctx(xhci, udev->slot_id);
  3778. if (ret) {
  3779. xhci_warn(xhci, "%s: Failed to sync device context failed, err=%d",
  3780. __func__, ret);
  3781. goto out;
  3782. }
  3783. /* FIXME: From section 4.3.4: "Software shall be responsible for timing
  3784. * the SetAddress() "recovery interval" required by USB and aborting the
  3785. * command on a timeout.
  3786. */
  3787. switch (command->status) {
  3788. case COMP_COMMAND_ABORTED:
  3789. case COMP_COMMAND_RING_STOPPED:
  3790. xhci_warn(xhci, "Timeout while waiting for setup device command\n");
  3791. ret = -ETIME;
  3792. break;
  3793. case COMP_CONTEXT_STATE_ERROR:
  3794. case COMP_SLOT_NOT_ENABLED_ERROR:
  3795. xhci_err(xhci, "Setup ERROR: setup %s command for slot %d.\n",
  3796. act, udev->slot_id);
  3797. ret = -EINVAL;
  3798. break;
  3799. case COMP_USB_TRANSACTION_ERROR:
  3800. dev_warn(&udev->dev, "Device not responding to setup %s.\n", act);
  3801. mutex_unlock(&xhci->mutex);
  3802. ret = xhci_disable_slot(xhci, udev->slot_id);
  3803. xhci_free_virt_device(xhci, udev->slot_id);
  3804. if (!ret)
  3805. xhci_alloc_dev(hcd, udev);
  3806. kfree(command->completion);
  3807. kfree(command);
  3808. return -EPROTO;
  3809. case COMP_INCOMPATIBLE_DEVICE_ERROR:
  3810. dev_warn(&udev->dev,
  3811. "ERROR: Incompatible device for setup %s command\n", act);
  3812. ret = -ENODEV;
  3813. break;
  3814. case COMP_SUCCESS:
  3815. xhci_dbg_trace(xhci, trace_xhci_dbg_address,
  3816. "Successful setup %s command", act);
  3817. break;
  3818. default:
  3819. xhci_err(xhci,
  3820. "ERROR: unexpected setup %s command completion code 0x%x.\n",
  3821. act, command->status);
  3822. trace_xhci_address_ctx(xhci, virt_dev->out_ctx, 1);
  3823. ret = -EINVAL;
  3824. break;
  3825. }
  3826. if (ret)
  3827. goto out;
  3828. temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
  3829. xhci_dbg_trace(xhci, trace_xhci_dbg_address,
  3830. "Op regs DCBAA ptr = %#016llx", temp_64);
  3831. xhci_dbg_trace(xhci, trace_xhci_dbg_address,
  3832. "Slot ID %d dcbaa entry @%p = %#016llx",
  3833. udev->slot_id,
  3834. &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
  3835. (unsigned long long)
  3836. le64_to_cpu(xhci->dcbaa->dev_context_ptrs[udev->slot_id]));
  3837. xhci_dbg_trace(xhci, trace_xhci_dbg_address,
  3838. "Output Context DMA address = %#08llx",
  3839. (unsigned long long)virt_dev->out_ctx->dma);
  3840. trace_xhci_address_ctx(xhci, virt_dev->in_ctx,
  3841. le32_to_cpu(slot_ctx->dev_info) >> 27);
  3842. /*
  3843. * USB core uses address 1 for the roothubs, so we add one to the
  3844. * address given back to us by the HC.
  3845. */
  3846. trace_xhci_address_ctx(xhci, virt_dev->out_ctx,
  3847. le32_to_cpu(slot_ctx->dev_info) >> 27);
  3848. /* Zero the input context control for later use */
  3849. ctrl_ctx->add_flags = 0;
  3850. ctrl_ctx->drop_flags = 0;
  3851. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
  3852. udev->devaddr = (u8)(le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK);
  3853. xhci_dbg_trace(xhci, trace_xhci_dbg_address,
  3854. "Internal device address = %d",
  3855. le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK);
  3856. out:
  3857. mutex_unlock(&xhci->mutex);
  3858. if (command) {
  3859. kfree(command->completion);
  3860. kfree(command);
  3861. }
  3862. return ret;
  3863. }
  3864. int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
  3865. {
  3866. return xhci_setup_device(hcd, udev, SETUP_CONTEXT_ADDRESS);
  3867. }
  3868. EXPORT_SYMBOL_GPL(xhci_address_device);
  3869. static int xhci_enable_device(struct usb_hcd *hcd, struct usb_device *udev)
  3870. {
  3871. return xhci_setup_device(hcd, udev, SETUP_CONTEXT_ONLY);
  3872. }
  3873. /*
  3874. * Transfer the port index into real index in the HW port status
  3875. * registers. Caculate offset between the port's PORTSC register
  3876. * and port status base. Divide the number of per port register
  3877. * to get the real index. The raw port number bases 1.
  3878. */
  3879. int xhci_find_raw_port_number(struct usb_hcd *hcd, int port1)
  3880. {
  3881. struct xhci_hub *rhub;
  3882. rhub = xhci_get_rhub(hcd);
  3883. return rhub->ports[port1 - 1]->hw_portnum + 1;
  3884. }
  3885. /*
  3886. * Issue an Evaluate Context command to change the Maximum Exit Latency in the
  3887. * slot context. If that succeeds, store the new MEL in the xhci_virt_device.
  3888. */
  3889. static int __maybe_unused xhci_change_max_exit_latency(struct xhci_hcd *xhci,
  3890. struct usb_device *udev, u16 max_exit_latency)
  3891. {
  3892. struct xhci_virt_device *virt_dev;
  3893. struct xhci_command *command;
  3894. struct xhci_input_control_ctx *ctrl_ctx;
  3895. struct xhci_slot_ctx *slot_ctx;
  3896. unsigned long flags;
  3897. int ret;
  3898. command = xhci_alloc_command_with_ctx(xhci, true, GFP_KERNEL);
  3899. if (!command)
  3900. return -ENOMEM;
  3901. spin_lock_irqsave(&xhci->lock, flags);
  3902. virt_dev = xhci->devs[udev->slot_id];
  3903. /*
  3904. * virt_dev might not exists yet if xHC resumed from hibernate (S4) and
  3905. * xHC was re-initialized. Exit latency will be set later after
  3906. * hub_port_finish_reset() is done and xhci->devs[] are re-allocated
  3907. */
  3908. if (!virt_dev || max_exit_latency == virt_dev->current_mel) {
  3909. spin_unlock_irqrestore(&xhci->lock, flags);
  3910. xhci_free_command(xhci, command);
  3911. return 0;
  3912. }
  3913. /* Attempt to issue an Evaluate Context command to change the MEL. */
  3914. ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
  3915. if (!ctrl_ctx) {
  3916. spin_unlock_irqrestore(&xhci->lock, flags);
  3917. xhci_free_command(xhci, command);
  3918. xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
  3919. __func__);
  3920. return -ENOMEM;
  3921. }
  3922. ret = xhci_vendor_sync_dev_ctx(xhci, udev->slot_id);
  3923. if (ret) {
  3924. spin_unlock_irqrestore(&xhci->lock, flags);
  3925. xhci_warn(xhci, "%s: Failed to sync device context failed, err=%d",
  3926. __func__, ret);
  3927. return ret;
  3928. }
  3929. xhci_slot_copy(xhci, command->in_ctx, virt_dev->out_ctx);
  3930. spin_unlock_irqrestore(&xhci->lock, flags);
  3931. ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
  3932. slot_ctx = xhci_get_slot_ctx(xhci, command->in_ctx);
  3933. slot_ctx->dev_info2 &= cpu_to_le32(~((u32) MAX_EXIT));
  3934. slot_ctx->dev_info2 |= cpu_to_le32(max_exit_latency);
  3935. slot_ctx->dev_state = 0;
  3936. xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
  3937. "Set up evaluate context for LPM MEL change.");
  3938. /* Issue and wait for the evaluate context command. */
  3939. ret = xhci_configure_endpoint(xhci, udev, command,
  3940. true, true);
  3941. if (!ret) {
  3942. spin_lock_irqsave(&xhci->lock, flags);
  3943. virt_dev->current_mel = max_exit_latency;
  3944. spin_unlock_irqrestore(&xhci->lock, flags);
  3945. }
  3946. xhci_free_command(xhci, command);
  3947. return ret;
  3948. }
  3949. struct xhci_vendor_ops *xhci_vendor_get_ops(struct xhci_hcd *xhci)
  3950. {
  3951. return xhci->vendor_ops;
  3952. }
  3953. EXPORT_SYMBOL_GPL(xhci_vendor_get_ops);
  3954. int xhci_vendor_sync_dev_ctx(struct xhci_hcd *xhci, unsigned int slot_id)
  3955. {
  3956. struct xhci_vendor_ops *ops = xhci_vendor_get_ops(xhci);
  3957. if (ops && ops->sync_dev_ctx)
  3958. return ops->sync_dev_ctx(xhci, slot_id);
  3959. return 0;
  3960. }
  3961. bool xhci_vendor_usb_offload_skip_urb(struct xhci_hcd *xhci, struct urb *urb)
  3962. {
  3963. struct xhci_vendor_ops *ops = xhci_vendor_get_ops(xhci);
  3964. if (ops && ops->usb_offload_skip_urb)
  3965. return ops->usb_offload_skip_urb(xhci, urb);
  3966. return false;
  3967. }
  3968. #ifdef CONFIG_PM
  3969. /* BESL to HIRD Encoding array for USB2 LPM */
  3970. static int xhci_besl_encoding[16] = {125, 150, 200, 300, 400, 500, 1000, 2000,
  3971. 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000};
  3972. /* Calculate HIRD/BESL for USB2 PORTPMSC*/
  3973. static int xhci_calculate_hird_besl(struct xhci_hcd *xhci,
  3974. struct usb_device *udev)
  3975. {
  3976. int u2del, besl, besl_host;
  3977. int besl_device = 0;
  3978. u32 field;
  3979. u2del = HCS_U2_LATENCY(xhci->hcs_params3);
  3980. field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
  3981. if (field & USB_BESL_SUPPORT) {
  3982. for (besl_host = 0; besl_host < 16; besl_host++) {
  3983. if (xhci_besl_encoding[besl_host] >= u2del)
  3984. break;
  3985. }
  3986. /* Use baseline BESL value as default */
  3987. if (field & USB_BESL_BASELINE_VALID)
  3988. besl_device = USB_GET_BESL_BASELINE(field);
  3989. else if (field & USB_BESL_DEEP_VALID)
  3990. besl_device = USB_GET_BESL_DEEP(field);
  3991. } else {
  3992. if (u2del <= 50)
  3993. besl_host = 0;
  3994. else
  3995. besl_host = (u2del - 51) / 75 + 1;
  3996. }
  3997. besl = besl_host + besl_device;
  3998. if (besl > 15)
  3999. besl = 15;
  4000. return besl;
  4001. }
  4002. /* Calculate BESLD, L1 timeout and HIRDM for USB2 PORTHLPMC */
  4003. static int xhci_calculate_usb2_hw_lpm_params(struct usb_device *udev)
  4004. {
  4005. u32 field;
  4006. int l1;
  4007. int besld = 0;
  4008. int hirdm = 0;
  4009. field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
  4010. /* xHCI l1 is set in steps of 256us, xHCI 1.0 section 5.4.11.2 */
  4011. l1 = udev->l1_params.timeout / 256;
  4012. /* device has preferred BESLD */
  4013. if (field & USB_BESL_DEEP_VALID) {
  4014. besld = USB_GET_BESL_DEEP(field);
  4015. hirdm = 1;
  4016. }
  4017. return PORT_BESLD(besld) | PORT_L1_TIMEOUT(l1) | PORT_HIRDM(hirdm);
  4018. }
  4019. static int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
  4020. struct usb_device *udev, int enable)
  4021. {
  4022. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  4023. struct xhci_port **ports;
  4024. __le32 __iomem *pm_addr, *hlpm_addr;
  4025. u32 pm_val, hlpm_val, field;
  4026. unsigned int port_num;
  4027. unsigned long flags;
  4028. int hird, exit_latency;
  4029. int ret;
  4030. if (xhci->quirks & XHCI_HW_LPM_DISABLE)
  4031. return -EPERM;
  4032. if (hcd->speed >= HCD_USB3 || !xhci->hw_lpm_support ||
  4033. !udev->lpm_capable)
  4034. return -EPERM;
  4035. if (!udev->parent || udev->parent->parent ||
  4036. udev->descriptor.bDeviceClass == USB_CLASS_HUB)
  4037. return -EPERM;
  4038. if (udev->usb2_hw_lpm_capable != 1)
  4039. return -EPERM;
  4040. spin_lock_irqsave(&xhci->lock, flags);
  4041. ports = xhci->usb2_rhub.ports;
  4042. port_num = udev->portnum - 1;
  4043. pm_addr = ports[port_num]->addr + PORTPMSC;
  4044. pm_val = readl(pm_addr);
  4045. hlpm_addr = ports[port_num]->addr + PORTHLPMC;
  4046. xhci_dbg(xhci, "%s port %d USB2 hardware LPM\n",
  4047. enable ? "enable" : "disable", port_num + 1);
  4048. if (enable) {
  4049. /* Host supports BESL timeout instead of HIRD */
  4050. if (udev->usb2_hw_lpm_besl_capable) {
  4051. /* if device doesn't have a preferred BESL value use a
  4052. * default one which works with mixed HIRD and BESL
  4053. * systems. See XHCI_DEFAULT_BESL definition in xhci.h
  4054. */
  4055. field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
  4056. if ((field & USB_BESL_SUPPORT) &&
  4057. (field & USB_BESL_BASELINE_VALID))
  4058. hird = USB_GET_BESL_BASELINE(field);
  4059. else
  4060. hird = udev->l1_params.besl;
  4061. exit_latency = xhci_besl_encoding[hird];
  4062. spin_unlock_irqrestore(&xhci->lock, flags);
  4063. ret = xhci_change_max_exit_latency(xhci, udev,
  4064. exit_latency);
  4065. if (ret < 0)
  4066. return ret;
  4067. spin_lock_irqsave(&xhci->lock, flags);
  4068. hlpm_val = xhci_calculate_usb2_hw_lpm_params(udev);
  4069. writel(hlpm_val, hlpm_addr);
  4070. /* flush write */
  4071. readl(hlpm_addr);
  4072. } else {
  4073. hird = xhci_calculate_hird_besl(xhci, udev);
  4074. }
  4075. pm_val &= ~PORT_HIRD_MASK;
  4076. pm_val |= PORT_HIRD(hird) | PORT_RWE | PORT_L1DS(udev->slot_id);
  4077. writel(pm_val, pm_addr);
  4078. pm_val = readl(pm_addr);
  4079. pm_val |= PORT_HLE;
  4080. writel(pm_val, pm_addr);
  4081. /* flush write */
  4082. readl(pm_addr);
  4083. } else {
  4084. pm_val &= ~(PORT_HLE | PORT_RWE | PORT_HIRD_MASK | PORT_L1DS_MASK);
  4085. writel(pm_val, pm_addr);
  4086. /* flush write */
  4087. readl(pm_addr);
  4088. if (udev->usb2_hw_lpm_besl_capable) {
  4089. spin_unlock_irqrestore(&xhci->lock, flags);
  4090. xhci_change_max_exit_latency(xhci, udev, 0);
  4091. readl_poll_timeout(ports[port_num]->addr, pm_val,
  4092. (pm_val & PORT_PLS_MASK) == XDEV_U0,
  4093. 100, 10000);
  4094. return 0;
  4095. }
  4096. }
  4097. spin_unlock_irqrestore(&xhci->lock, flags);
  4098. return 0;
  4099. }
  4100. /* check if a usb2 port supports a given extened capability protocol
  4101. * only USB2 ports extended protocol capability values are cached.
  4102. * Return 1 if capability is supported
  4103. */
  4104. static int xhci_check_usb2_port_capability(struct xhci_hcd *xhci, int port,
  4105. unsigned capability)
  4106. {
  4107. u32 port_offset, port_count;
  4108. int i;
  4109. for (i = 0; i < xhci->num_ext_caps; i++) {
  4110. if (xhci->ext_caps[i] & capability) {
  4111. /* port offsets starts at 1 */
  4112. port_offset = XHCI_EXT_PORT_OFF(xhci->ext_caps[i]) - 1;
  4113. port_count = XHCI_EXT_PORT_COUNT(xhci->ext_caps[i]);
  4114. if (port >= port_offset &&
  4115. port < port_offset + port_count)
  4116. return 1;
  4117. }
  4118. }
  4119. return 0;
  4120. }
  4121. static int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
  4122. {
  4123. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  4124. int portnum = udev->portnum - 1;
  4125. if (hcd->speed >= HCD_USB3 || !udev->lpm_capable)
  4126. return 0;
  4127. /* we only support lpm for non-hub device connected to root hub yet */
  4128. if (!udev->parent || udev->parent->parent ||
  4129. udev->descriptor.bDeviceClass == USB_CLASS_HUB)
  4130. return 0;
  4131. if (xhci->hw_lpm_support == 1 &&
  4132. xhci_check_usb2_port_capability(
  4133. xhci, portnum, XHCI_HLC)) {
  4134. udev->usb2_hw_lpm_capable = 1;
  4135. udev->l1_params.timeout = XHCI_L1_TIMEOUT;
  4136. udev->l1_params.besl = XHCI_DEFAULT_BESL;
  4137. if (xhci_check_usb2_port_capability(xhci, portnum,
  4138. XHCI_BLC))
  4139. udev->usb2_hw_lpm_besl_capable = 1;
  4140. }
  4141. return 0;
  4142. }
  4143. /*---------------------- USB 3.0 Link PM functions ------------------------*/
  4144. /* Service interval in nanoseconds = 2^(bInterval - 1) * 125us * 1000ns / 1us */
  4145. static unsigned long long xhci_service_interval_to_ns(
  4146. struct usb_endpoint_descriptor *desc)
  4147. {
  4148. return (1ULL << (desc->bInterval - 1)) * 125 * 1000;
  4149. }
  4150. static u16 xhci_get_timeout_no_hub_lpm(struct usb_device *udev,
  4151. enum usb3_link_state state)
  4152. {
  4153. unsigned long long sel;
  4154. unsigned long long pel;
  4155. unsigned int max_sel_pel;
  4156. char *state_name;
  4157. switch (state) {
  4158. case USB3_LPM_U1:
  4159. /* Convert SEL and PEL stored in nanoseconds to microseconds */
  4160. sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
  4161. pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
  4162. max_sel_pel = USB3_LPM_MAX_U1_SEL_PEL;
  4163. state_name = "U1";
  4164. break;
  4165. case USB3_LPM_U2:
  4166. sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
  4167. pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
  4168. max_sel_pel = USB3_LPM_MAX_U2_SEL_PEL;
  4169. state_name = "U2";
  4170. break;
  4171. default:
  4172. dev_warn(&udev->dev, "%s: Can't get timeout for non-U1 or U2 state.\n",
  4173. __func__);
  4174. return USB3_LPM_DISABLED;
  4175. }
  4176. if (sel <= max_sel_pel && pel <= max_sel_pel)
  4177. return USB3_LPM_DEVICE_INITIATED;
  4178. if (sel > max_sel_pel)
  4179. dev_dbg(&udev->dev, "Device-initiated %s disabled "
  4180. "due to long SEL %llu ms\n",
  4181. state_name, sel);
  4182. else
  4183. dev_dbg(&udev->dev, "Device-initiated %s disabled "
  4184. "due to long PEL %llu ms\n",
  4185. state_name, pel);
  4186. return USB3_LPM_DISABLED;
  4187. }
  4188. /* The U1 timeout should be the maximum of the following values:
  4189. * - For control endpoints, U1 system exit latency (SEL) * 3
  4190. * - For bulk endpoints, U1 SEL * 5
  4191. * - For interrupt endpoints:
  4192. * - Notification EPs, U1 SEL * 3
  4193. * - Periodic EPs, max(105% of bInterval, U1 SEL * 2)
  4194. * - For isochronous endpoints, max(105% of bInterval, U1 SEL * 2)
  4195. */
  4196. static unsigned long long xhci_calculate_intel_u1_timeout(
  4197. struct usb_device *udev,
  4198. struct usb_endpoint_descriptor *desc)
  4199. {
  4200. unsigned long long timeout_ns;
  4201. int ep_type;
  4202. int intr_type;
  4203. ep_type = usb_endpoint_type(desc);
  4204. switch (ep_type) {
  4205. case USB_ENDPOINT_XFER_CONTROL:
  4206. timeout_ns = udev->u1_params.sel * 3;
  4207. break;
  4208. case USB_ENDPOINT_XFER_BULK:
  4209. timeout_ns = udev->u1_params.sel * 5;
  4210. break;
  4211. case USB_ENDPOINT_XFER_INT:
  4212. intr_type = usb_endpoint_interrupt_type(desc);
  4213. if (intr_type == USB_ENDPOINT_INTR_NOTIFICATION) {
  4214. timeout_ns = udev->u1_params.sel * 3;
  4215. break;
  4216. }
  4217. /* Otherwise the calculation is the same as isoc eps */
  4218. fallthrough;
  4219. case USB_ENDPOINT_XFER_ISOC:
  4220. timeout_ns = xhci_service_interval_to_ns(desc);
  4221. timeout_ns = DIV_ROUND_UP_ULL(timeout_ns * 105, 100);
  4222. if (timeout_ns < udev->u1_params.sel * 2)
  4223. timeout_ns = udev->u1_params.sel * 2;
  4224. break;
  4225. default:
  4226. return 0;
  4227. }
  4228. return timeout_ns;
  4229. }
  4230. /* Returns the hub-encoded U1 timeout value. */
  4231. static u16 xhci_calculate_u1_timeout(struct xhci_hcd *xhci,
  4232. struct usb_device *udev,
  4233. struct usb_endpoint_descriptor *desc)
  4234. {
  4235. unsigned long long timeout_ns;
  4236. /* Prevent U1 if service interval is shorter than U1 exit latency */
  4237. if (usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc)) {
  4238. if (xhci_service_interval_to_ns(desc) <= udev->u1_params.mel) {
  4239. dev_dbg(&udev->dev, "Disable U1, ESIT shorter than exit latency\n");
  4240. return USB3_LPM_DISABLED;
  4241. }
  4242. }
  4243. if (xhci->quirks & XHCI_INTEL_HOST)
  4244. timeout_ns = xhci_calculate_intel_u1_timeout(udev, desc);
  4245. else
  4246. timeout_ns = udev->u1_params.sel;
  4247. /* The U1 timeout is encoded in 1us intervals.
  4248. * Don't return a timeout of zero, because that's USB3_LPM_DISABLED.
  4249. */
  4250. if (timeout_ns == USB3_LPM_DISABLED)
  4251. timeout_ns = 1;
  4252. else
  4253. timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 1000);
  4254. /* If the necessary timeout value is bigger than what we can set in the
  4255. * USB 3.0 hub, we have to disable hub-initiated U1.
  4256. */
  4257. if (timeout_ns <= USB3_LPM_U1_MAX_TIMEOUT)
  4258. return timeout_ns;
  4259. dev_dbg(&udev->dev, "Hub-initiated U1 disabled "
  4260. "due to long timeout %llu ms\n", timeout_ns);
  4261. return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U1);
  4262. }
  4263. /* The U2 timeout should be the maximum of:
  4264. * - 10 ms (to avoid the bandwidth impact on the scheduler)
  4265. * - largest bInterval of any active periodic endpoint (to avoid going
  4266. * into lower power link states between intervals).
  4267. * - the U2 Exit Latency of the device
  4268. */
  4269. static unsigned long long xhci_calculate_intel_u2_timeout(
  4270. struct usb_device *udev,
  4271. struct usb_endpoint_descriptor *desc)
  4272. {
  4273. unsigned long long timeout_ns;
  4274. unsigned long long u2_del_ns;
  4275. timeout_ns = 10 * 1000 * 1000;
  4276. if ((usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc)) &&
  4277. (xhci_service_interval_to_ns(desc) > timeout_ns))
  4278. timeout_ns = xhci_service_interval_to_ns(desc);
  4279. u2_del_ns = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat) * 1000ULL;
  4280. if (u2_del_ns > timeout_ns)
  4281. timeout_ns = u2_del_ns;
  4282. return timeout_ns;
  4283. }
  4284. /* Returns the hub-encoded U2 timeout value. */
  4285. static u16 xhci_calculate_u2_timeout(struct xhci_hcd *xhci,
  4286. struct usb_device *udev,
  4287. struct usb_endpoint_descriptor *desc)
  4288. {
  4289. unsigned long long timeout_ns;
  4290. /* Prevent U2 if service interval is shorter than U2 exit latency */
  4291. if (usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc)) {
  4292. if (xhci_service_interval_to_ns(desc) <= udev->u2_params.mel) {
  4293. dev_dbg(&udev->dev, "Disable U2, ESIT shorter than exit latency\n");
  4294. return USB3_LPM_DISABLED;
  4295. }
  4296. }
  4297. if (xhci->quirks & XHCI_INTEL_HOST)
  4298. timeout_ns = xhci_calculate_intel_u2_timeout(udev, desc);
  4299. else
  4300. timeout_ns = udev->u2_params.sel;
  4301. /* The U2 timeout is encoded in 256us intervals */
  4302. timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 256 * 1000);
  4303. /* If the necessary timeout value is bigger than what we can set in the
  4304. * USB 3.0 hub, we have to disable hub-initiated U2.
  4305. */
  4306. if (timeout_ns <= USB3_LPM_U2_MAX_TIMEOUT)
  4307. return timeout_ns;
  4308. dev_dbg(&udev->dev, "Hub-initiated U2 disabled "
  4309. "due to long timeout %llu ms\n", timeout_ns);
  4310. return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U2);
  4311. }
  4312. static u16 xhci_call_host_update_timeout_for_endpoint(struct xhci_hcd *xhci,
  4313. struct usb_device *udev,
  4314. struct usb_endpoint_descriptor *desc,
  4315. enum usb3_link_state state,
  4316. u16 *timeout)
  4317. {
  4318. if (state == USB3_LPM_U1)
  4319. return xhci_calculate_u1_timeout(xhci, udev, desc);
  4320. else if (state == USB3_LPM_U2)
  4321. return xhci_calculate_u2_timeout(xhci, udev, desc);
  4322. return USB3_LPM_DISABLED;
  4323. }
  4324. static int xhci_update_timeout_for_endpoint(struct xhci_hcd *xhci,
  4325. struct usb_device *udev,
  4326. struct usb_endpoint_descriptor *desc,
  4327. enum usb3_link_state state,
  4328. u16 *timeout)
  4329. {
  4330. u16 alt_timeout;
  4331. alt_timeout = xhci_call_host_update_timeout_for_endpoint(xhci, udev,
  4332. desc, state, timeout);
  4333. /* If we found we can't enable hub-initiated LPM, and
  4334. * the U1 or U2 exit latency was too high to allow
  4335. * device-initiated LPM as well, then we will disable LPM
  4336. * for this device, so stop searching any further.
  4337. */
  4338. if (alt_timeout == USB3_LPM_DISABLED) {
  4339. *timeout = alt_timeout;
  4340. return -E2BIG;
  4341. }
  4342. if (alt_timeout > *timeout)
  4343. *timeout = alt_timeout;
  4344. return 0;
  4345. }
  4346. static int xhci_update_timeout_for_interface(struct xhci_hcd *xhci,
  4347. struct usb_device *udev,
  4348. struct usb_host_interface *alt,
  4349. enum usb3_link_state state,
  4350. u16 *timeout)
  4351. {
  4352. int j;
  4353. for (j = 0; j < alt->desc.bNumEndpoints; j++) {
  4354. if (xhci_update_timeout_for_endpoint(xhci, udev,
  4355. &alt->endpoint[j].desc, state, timeout))
  4356. return -E2BIG;
  4357. }
  4358. return 0;
  4359. }
  4360. static int xhci_check_intel_tier_policy(struct usb_device *udev,
  4361. enum usb3_link_state state)
  4362. {
  4363. struct usb_device *parent;
  4364. unsigned int num_hubs;
  4365. /* Don't enable U1 if the device is on a 2nd tier hub or lower. */
  4366. for (parent = udev->parent, num_hubs = 0; parent->parent;
  4367. parent = parent->parent)
  4368. num_hubs++;
  4369. if (num_hubs < 2)
  4370. return 0;
  4371. dev_dbg(&udev->dev, "Disabling U1/U2 link state for device"
  4372. " below second-tier hub.\n");
  4373. dev_dbg(&udev->dev, "Plug device into first-tier hub "
  4374. "to decrease power consumption.\n");
  4375. return -E2BIG;
  4376. }
  4377. static int xhci_check_tier_policy(struct xhci_hcd *xhci,
  4378. struct usb_device *udev,
  4379. enum usb3_link_state state)
  4380. {
  4381. if (xhci->quirks & XHCI_INTEL_HOST)
  4382. return xhci_check_intel_tier_policy(udev, state);
  4383. else
  4384. return 0;
  4385. }
  4386. /* Returns the U1 or U2 timeout that should be enabled.
  4387. * If the tier check or timeout setting functions return with a non-zero exit
  4388. * code, that means the timeout value has been finalized and we shouldn't look
  4389. * at any more endpoints.
  4390. */
  4391. static u16 xhci_calculate_lpm_timeout(struct usb_hcd *hcd,
  4392. struct usb_device *udev, enum usb3_link_state state)
  4393. {
  4394. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  4395. struct usb_host_config *config;
  4396. char *state_name;
  4397. int i;
  4398. u16 timeout = USB3_LPM_DISABLED;
  4399. if (state == USB3_LPM_U1)
  4400. state_name = "U1";
  4401. else if (state == USB3_LPM_U2)
  4402. state_name = "U2";
  4403. else {
  4404. dev_warn(&udev->dev, "Can't enable unknown link state %i\n",
  4405. state);
  4406. return timeout;
  4407. }
  4408. /* Gather some information about the currently installed configuration
  4409. * and alternate interface settings.
  4410. */
  4411. if (xhci_update_timeout_for_endpoint(xhci, udev, &udev->ep0.desc,
  4412. state, &timeout))
  4413. return timeout;
  4414. config = udev->actconfig;
  4415. if (!config)
  4416. return timeout;
  4417. for (i = 0; i < config->desc.bNumInterfaces; i++) {
  4418. struct usb_driver *driver;
  4419. struct usb_interface *intf = config->interface[i];
  4420. if (!intf)
  4421. continue;
  4422. /* Check if any currently bound drivers want hub-initiated LPM
  4423. * disabled.
  4424. */
  4425. if (intf->dev.driver) {
  4426. driver = to_usb_driver(intf->dev.driver);
  4427. if (driver && driver->disable_hub_initiated_lpm) {
  4428. dev_dbg(&udev->dev, "Hub-initiated %s disabled at request of driver %s\n",
  4429. state_name, driver->name);
  4430. timeout = xhci_get_timeout_no_hub_lpm(udev,
  4431. state);
  4432. if (timeout == USB3_LPM_DISABLED)
  4433. return timeout;
  4434. }
  4435. }
  4436. /* Not sure how this could happen... */
  4437. if (!intf->cur_altsetting)
  4438. continue;
  4439. if (xhci_update_timeout_for_interface(xhci, udev,
  4440. intf->cur_altsetting,
  4441. state, &timeout))
  4442. return timeout;
  4443. }
  4444. return timeout;
  4445. }
  4446. static int calculate_max_exit_latency(struct usb_device *udev,
  4447. enum usb3_link_state state_changed,
  4448. u16 hub_encoded_timeout)
  4449. {
  4450. unsigned long long u1_mel_us = 0;
  4451. unsigned long long u2_mel_us = 0;
  4452. unsigned long long mel_us = 0;
  4453. bool disabling_u1;
  4454. bool disabling_u2;
  4455. bool enabling_u1;
  4456. bool enabling_u2;
  4457. disabling_u1 = (state_changed == USB3_LPM_U1 &&
  4458. hub_encoded_timeout == USB3_LPM_DISABLED);
  4459. disabling_u2 = (state_changed == USB3_LPM_U2 &&
  4460. hub_encoded_timeout == USB3_LPM_DISABLED);
  4461. enabling_u1 = (state_changed == USB3_LPM_U1 &&
  4462. hub_encoded_timeout != USB3_LPM_DISABLED);
  4463. enabling_u2 = (state_changed == USB3_LPM_U2 &&
  4464. hub_encoded_timeout != USB3_LPM_DISABLED);
  4465. /* If U1 was already enabled and we're not disabling it,
  4466. * or we're going to enable U1, account for the U1 max exit latency.
  4467. */
  4468. if ((udev->u1_params.timeout != USB3_LPM_DISABLED && !disabling_u1) ||
  4469. enabling_u1)
  4470. u1_mel_us = DIV_ROUND_UP(udev->u1_params.mel, 1000);
  4471. if ((udev->u2_params.timeout != USB3_LPM_DISABLED && !disabling_u2) ||
  4472. enabling_u2)
  4473. u2_mel_us = DIV_ROUND_UP(udev->u2_params.mel, 1000);
  4474. mel_us = max(u1_mel_us, u2_mel_us);
  4475. /* xHCI host controller max exit latency field is only 16 bits wide. */
  4476. if (mel_us > MAX_EXIT) {
  4477. dev_warn(&udev->dev, "Link PM max exit latency of %lluus "
  4478. "is too big.\n", mel_us);
  4479. return -E2BIG;
  4480. }
  4481. return mel_us;
  4482. }
  4483. /* Returns the USB3 hub-encoded value for the U1/U2 timeout. */
  4484. static int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
  4485. struct usb_device *udev, enum usb3_link_state state)
  4486. {
  4487. struct xhci_hcd *xhci;
  4488. struct xhci_port *port;
  4489. u16 hub_encoded_timeout;
  4490. int mel;
  4491. int ret;
  4492. xhci = hcd_to_xhci(hcd);
  4493. /* The LPM timeout values are pretty host-controller specific, so don't
  4494. * enable hub-initiated timeouts unless the vendor has provided
  4495. * information about their timeout algorithm.
  4496. */
  4497. if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) ||
  4498. !xhci->devs[udev->slot_id])
  4499. return USB3_LPM_DISABLED;
  4500. if (xhci_check_tier_policy(xhci, udev, state) < 0)
  4501. return USB3_LPM_DISABLED;
  4502. /* If connected to root port then check port can handle lpm */
  4503. if (udev->parent && !udev->parent->parent) {
  4504. port = xhci->usb3_rhub.ports[udev->portnum - 1];
  4505. if (port->lpm_incapable)
  4506. return USB3_LPM_DISABLED;
  4507. }
  4508. hub_encoded_timeout = xhci_calculate_lpm_timeout(hcd, udev, state);
  4509. mel = calculate_max_exit_latency(udev, state, hub_encoded_timeout);
  4510. if (mel < 0) {
  4511. /* Max Exit Latency is too big, disable LPM. */
  4512. hub_encoded_timeout = USB3_LPM_DISABLED;
  4513. mel = 0;
  4514. }
  4515. ret = xhci_change_max_exit_latency(xhci, udev, mel);
  4516. if (ret)
  4517. return ret;
  4518. return hub_encoded_timeout;
  4519. }
  4520. static int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
  4521. struct usb_device *udev, enum usb3_link_state state)
  4522. {
  4523. struct xhci_hcd *xhci;
  4524. u16 mel;
  4525. xhci = hcd_to_xhci(hcd);
  4526. if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) ||
  4527. !xhci->devs[udev->slot_id])
  4528. return 0;
  4529. mel = calculate_max_exit_latency(udev, state, USB3_LPM_DISABLED);
  4530. return xhci_change_max_exit_latency(xhci, udev, mel);
  4531. }
  4532. #else /* CONFIG_PM */
  4533. static int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
  4534. struct usb_device *udev, int enable)
  4535. {
  4536. return 0;
  4537. }
  4538. static int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
  4539. {
  4540. return 0;
  4541. }
  4542. static int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
  4543. struct usb_device *udev, enum usb3_link_state state)
  4544. {
  4545. return USB3_LPM_DISABLED;
  4546. }
  4547. static int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
  4548. struct usb_device *udev, enum usb3_link_state state)
  4549. {
  4550. return 0;
  4551. }
  4552. #endif /* CONFIG_PM */
  4553. /*-------------------------------------------------------------------------*/
  4554. /* Once a hub descriptor is fetched for a device, we need to update the xHC's
  4555. * internal data structures for the device.
  4556. */
  4557. int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
  4558. struct usb_tt *tt, gfp_t mem_flags)
  4559. {
  4560. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  4561. struct xhci_virt_device *vdev;
  4562. struct xhci_command *config_cmd;
  4563. struct xhci_input_control_ctx *ctrl_ctx;
  4564. struct xhci_slot_ctx *slot_ctx;
  4565. unsigned long flags;
  4566. unsigned think_time;
  4567. int ret;
  4568. /* Ignore root hubs */
  4569. if (!hdev->parent)
  4570. return 0;
  4571. vdev = xhci->devs[hdev->slot_id];
  4572. if (!vdev) {
  4573. xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
  4574. return -EINVAL;
  4575. }
  4576. config_cmd = xhci_alloc_command_with_ctx(xhci, true, mem_flags);
  4577. if (!config_cmd)
  4578. return -ENOMEM;
  4579. ctrl_ctx = xhci_get_input_control_ctx(config_cmd->in_ctx);
  4580. if (!ctrl_ctx) {
  4581. xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
  4582. __func__);
  4583. xhci_free_command(xhci, config_cmd);
  4584. return -ENOMEM;
  4585. }
  4586. spin_lock_irqsave(&xhci->lock, flags);
  4587. if (hdev->speed == USB_SPEED_HIGH &&
  4588. xhci_alloc_tt_info(xhci, vdev, hdev, tt, GFP_ATOMIC)) {
  4589. xhci_dbg(xhci, "Could not allocate xHCI TT structure.\n");
  4590. xhci_free_command(xhci, config_cmd);
  4591. spin_unlock_irqrestore(&xhci->lock, flags);
  4592. return -ENOMEM;
  4593. }
  4594. ret = xhci_vendor_sync_dev_ctx(xhci, hdev->slot_id);
  4595. if (ret) {
  4596. xhci_warn(xhci, "%s: Failed to sync device context failed, err=%d",
  4597. __func__, ret);
  4598. xhci_free_command(xhci, config_cmd);
  4599. spin_unlock_irqrestore(&xhci->lock, flags);
  4600. return ret;
  4601. }
  4602. xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
  4603. ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
  4604. slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
  4605. slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
  4606. /*
  4607. * refer to section 6.2.2: MTT should be 0 for full speed hub,
  4608. * but it may be already set to 1 when setup an xHCI virtual
  4609. * device, so clear it anyway.
  4610. */
  4611. if (tt->multi)
  4612. slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
  4613. else if (hdev->speed == USB_SPEED_FULL)
  4614. slot_ctx->dev_info &= cpu_to_le32(~DEV_MTT);
  4615. if (xhci->hci_version > 0x95) {
  4616. xhci_dbg(xhci, "xHCI version %x needs hub "
  4617. "TT think time and number of ports\n",
  4618. (unsigned int) xhci->hci_version);
  4619. slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(hdev->maxchild));
  4620. /* Set TT think time - convert from ns to FS bit times.
  4621. * 0 = 8 FS bit times, 1 = 16 FS bit times,
  4622. * 2 = 24 FS bit times, 3 = 32 FS bit times.
  4623. *
  4624. * xHCI 1.0: this field shall be 0 if the device is not a
  4625. * High-spped hub.
  4626. */
  4627. think_time = tt->think_time;
  4628. if (think_time != 0)
  4629. think_time = (think_time / 666) - 1;
  4630. if (xhci->hci_version < 0x100 || hdev->speed == USB_SPEED_HIGH)
  4631. slot_ctx->tt_info |=
  4632. cpu_to_le32(TT_THINK_TIME(think_time));
  4633. } else {
  4634. xhci_dbg(xhci, "xHCI version %x doesn't need hub "
  4635. "TT think time or number of ports\n",
  4636. (unsigned int) xhci->hci_version);
  4637. }
  4638. slot_ctx->dev_state = 0;
  4639. spin_unlock_irqrestore(&xhci->lock, flags);
  4640. xhci_dbg(xhci, "Set up %s for hub device.\n",
  4641. (xhci->hci_version > 0x95) ?
  4642. "configure endpoint" : "evaluate context");
  4643. /* Issue and wait for the configure endpoint or
  4644. * evaluate context command.
  4645. */
  4646. if (xhci->hci_version > 0x95)
  4647. ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
  4648. false, false);
  4649. else
  4650. ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
  4651. true, false);
  4652. xhci_free_command(xhci, config_cmd);
  4653. return ret;
  4654. }
  4655. EXPORT_SYMBOL_GPL(xhci_update_hub_device);
  4656. static int xhci_get_frame(struct usb_hcd *hcd)
  4657. {
  4658. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  4659. /* EHCI mods by the periodic size. Why? */
  4660. return readl(&xhci->run_regs->microframe_index) >> 3;
  4661. }
  4662. static void xhci_hcd_init_usb2_data(struct xhci_hcd *xhci, struct usb_hcd *hcd)
  4663. {
  4664. xhci->usb2_rhub.hcd = hcd;
  4665. hcd->speed = HCD_USB2;
  4666. hcd->self.root_hub->speed = USB_SPEED_HIGH;
  4667. /*
  4668. * USB 2.0 roothub under xHCI has an integrated TT,
  4669. * (rate matching hub) as opposed to having an OHCI/UHCI
  4670. * companion controller.
  4671. */
  4672. hcd->has_tt = 1;
  4673. }
  4674. static void xhci_hcd_init_usb3_data(struct xhci_hcd *xhci, struct usb_hcd *hcd)
  4675. {
  4676. unsigned int minor_rev;
  4677. /*
  4678. * Early xHCI 1.1 spec did not mention USB 3.1 capable hosts
  4679. * should return 0x31 for sbrn, or that the minor revision
  4680. * is a two digit BCD containig minor and sub-minor numbers.
  4681. * This was later clarified in xHCI 1.2.
  4682. *
  4683. * Some USB 3.1 capable hosts therefore have sbrn 0x30, and
  4684. * minor revision set to 0x1 instead of 0x10.
  4685. */
  4686. if (xhci->usb3_rhub.min_rev == 0x1)
  4687. minor_rev = 1;
  4688. else
  4689. minor_rev = xhci->usb3_rhub.min_rev / 0x10;
  4690. switch (minor_rev) {
  4691. case 2:
  4692. hcd->speed = HCD_USB32;
  4693. hcd->self.root_hub->speed = USB_SPEED_SUPER_PLUS;
  4694. hcd->self.root_hub->rx_lanes = 2;
  4695. hcd->self.root_hub->tx_lanes = 2;
  4696. hcd->self.root_hub->ssp_rate = USB_SSP_GEN_2x2;
  4697. break;
  4698. case 1:
  4699. hcd->speed = HCD_USB31;
  4700. hcd->self.root_hub->speed = USB_SPEED_SUPER_PLUS;
  4701. hcd->self.root_hub->ssp_rate = USB_SSP_GEN_2x1;
  4702. break;
  4703. }
  4704. xhci_info(xhci, "Host supports USB 3.%x %sSuperSpeed\n",
  4705. minor_rev, minor_rev ? "Enhanced " : "");
  4706. xhci->usb3_rhub.hcd = hcd;
  4707. }
  4708. int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks)
  4709. {
  4710. struct xhci_hcd *xhci;
  4711. /*
  4712. * TODO: Check with DWC3 clients for sysdev according to
  4713. * quirks
  4714. */
  4715. struct device *dev = hcd->self.sysdev;
  4716. int retval;
  4717. /* Accept arbitrarily long scatter-gather lists */
  4718. hcd->self.sg_tablesize = ~0;
  4719. /* support to build packet from discontinuous buffers */
  4720. hcd->self.no_sg_constraint = 1;
  4721. /* XHCI controllers don't stop the ep queue on short packets :| */
  4722. hcd->self.no_stop_on_short = 1;
  4723. xhci = hcd_to_xhci(hcd);
  4724. if (!usb_hcd_is_primary_hcd(hcd)) {
  4725. xhci_hcd_init_usb3_data(xhci, hcd);
  4726. return 0;
  4727. }
  4728. mutex_init(&xhci->mutex);
  4729. xhci->main_hcd = hcd;
  4730. xhci->cap_regs = hcd->regs;
  4731. xhci->op_regs = hcd->regs +
  4732. HC_LENGTH(readl(&xhci->cap_regs->hc_capbase));
  4733. xhci->run_regs = hcd->regs +
  4734. (readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK);
  4735. /* Cache read-only capability registers */
  4736. xhci->hcs_params1 = readl(&xhci->cap_regs->hcs_params1);
  4737. xhci->hcs_params2 = readl(&xhci->cap_regs->hcs_params2);
  4738. xhci->hcs_params3 = readl(&xhci->cap_regs->hcs_params3);
  4739. xhci->hci_version = HC_VERSION(readl(&xhci->cap_regs->hc_capbase));
  4740. xhci->hcc_params = readl(&xhci->cap_regs->hcc_params);
  4741. if (xhci->hci_version > 0x100)
  4742. xhci->hcc_params2 = readl(&xhci->cap_regs->hcc_params2);
  4743. xhci->quirks |= quirks;
  4744. get_quirks(dev, xhci);
  4745. /* In xhci controllers which follow xhci 1.0 spec gives a spurious
  4746. * success event after a short transfer. This quirk will ignore such
  4747. * spurious event.
  4748. */
  4749. if (xhci->hci_version > 0x96)
  4750. xhci->quirks |= XHCI_SPURIOUS_SUCCESS;
  4751. /* Make sure the HC is halted. */
  4752. retval = xhci_halt(xhci);
  4753. if (retval)
  4754. return retval;
  4755. xhci_zero_64b_regs(xhci);
  4756. xhci_dbg(xhci, "Resetting HCD\n");
  4757. /* Reset the internal HC memory state and registers. */
  4758. retval = xhci_reset(xhci, XHCI_RESET_LONG_USEC);
  4759. if (retval)
  4760. return retval;
  4761. xhci_dbg(xhci, "Reset complete\n");
  4762. /*
  4763. * On some xHCI controllers (e.g. R-Car SoCs), the AC64 bit (bit 0)
  4764. * of HCCPARAMS1 is set to 1. However, the xHCs don't support 64-bit
  4765. * address memory pointers actually. So, this driver clears the AC64
  4766. * bit of xhci->hcc_params to call dma_set_coherent_mask(dev,
  4767. * DMA_BIT_MASK(32)) in this xhci_gen_setup().
  4768. */
  4769. if (xhci->quirks & XHCI_NO_64BIT_SUPPORT)
  4770. xhci->hcc_params &= ~BIT(0);
  4771. /* Set dma_mask and coherent_dma_mask to 64-bits,
  4772. * if xHC supports 64-bit addressing */
  4773. if (HCC_64BIT_ADDR(xhci->hcc_params) &&
  4774. !dma_set_mask(dev, DMA_BIT_MASK(64))) {
  4775. xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
  4776. dma_set_coherent_mask(dev, DMA_BIT_MASK(64));
  4777. } else {
  4778. /*
  4779. * This is to avoid error in cases where a 32-bit USB
  4780. * controller is used on a 64-bit capable system.
  4781. */
  4782. retval = dma_set_mask(dev, DMA_BIT_MASK(32));
  4783. if (retval)
  4784. return retval;
  4785. xhci_dbg(xhci, "Enabling 32-bit DMA addresses.\n");
  4786. dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
  4787. }
  4788. xhci_dbg(xhci, "Calling HCD init\n");
  4789. /* Initialize HCD and host controller data structures. */
  4790. retval = xhci_init(hcd);
  4791. if (retval)
  4792. return retval;
  4793. xhci_dbg(xhci, "Called HCD init\n");
  4794. if (xhci_hcd_is_usb3(hcd))
  4795. xhci_hcd_init_usb3_data(xhci, hcd);
  4796. else
  4797. xhci_hcd_init_usb2_data(xhci, hcd);
  4798. xhci_info(xhci, "hcc params 0x%08x hci version 0x%x quirks 0x%016llx\n",
  4799. xhci->hcc_params, xhci->hci_version, xhci->quirks);
  4800. return 0;
  4801. }
  4802. EXPORT_SYMBOL_GPL(xhci_gen_setup);
  4803. static void xhci_clear_tt_buffer_complete(struct usb_hcd *hcd,
  4804. struct usb_host_endpoint *ep)
  4805. {
  4806. struct xhci_hcd *xhci;
  4807. struct usb_device *udev;
  4808. unsigned int slot_id;
  4809. unsigned int ep_index;
  4810. unsigned long flags;
  4811. xhci = hcd_to_xhci(hcd);
  4812. spin_lock_irqsave(&xhci->lock, flags);
  4813. udev = (struct usb_device *)ep->hcpriv;
  4814. slot_id = udev->slot_id;
  4815. ep_index = xhci_get_endpoint_index(&ep->desc);
  4816. xhci->devs[slot_id]->eps[ep_index].ep_state &= ~EP_CLEARING_TT;
  4817. xhci_ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
  4818. spin_unlock_irqrestore(&xhci->lock, flags);
  4819. }
  4820. static const struct hc_driver xhci_hc_driver = {
  4821. .description = "xhci-hcd",
  4822. .product_desc = "xHCI Host Controller",
  4823. .hcd_priv_size = sizeof(struct xhci_hcd),
  4824. /*
  4825. * generic hardware linkage
  4826. */
  4827. .irq = xhci_irq,
  4828. .flags = HCD_MEMORY | HCD_DMA | HCD_USB3 | HCD_SHARED |
  4829. HCD_BH,
  4830. /*
  4831. * basic lifecycle operations
  4832. */
  4833. .reset = NULL, /* set in xhci_init_driver() */
  4834. .start = xhci_run,
  4835. .stop = xhci_stop,
  4836. .shutdown = xhci_shutdown,
  4837. /*
  4838. * managing i/o requests and associated device resources
  4839. */
  4840. .map_urb_for_dma = xhci_map_urb_for_dma,
  4841. .unmap_urb_for_dma = xhci_unmap_urb_for_dma,
  4842. .urb_enqueue = xhci_urb_enqueue,
  4843. .urb_dequeue = xhci_urb_dequeue,
  4844. .alloc_dev = xhci_alloc_dev,
  4845. .free_dev = xhci_free_dev,
  4846. .alloc_streams = xhci_alloc_streams,
  4847. .free_streams = xhci_free_streams,
  4848. .add_endpoint = xhci_add_endpoint,
  4849. .drop_endpoint = xhci_drop_endpoint,
  4850. .endpoint_disable = xhci_endpoint_disable,
  4851. .endpoint_reset = xhci_endpoint_reset,
  4852. .check_bandwidth = xhci_check_bandwidth,
  4853. .reset_bandwidth = xhci_reset_bandwidth,
  4854. .address_device = xhci_address_device,
  4855. .enable_device = xhci_enable_device,
  4856. .update_hub_device = xhci_update_hub_device,
  4857. .reset_device = xhci_discover_or_reset_device,
  4858. /*
  4859. * scheduling support
  4860. */
  4861. .get_frame_number = xhci_get_frame,
  4862. /*
  4863. * root hub support
  4864. */
  4865. .hub_control = xhci_hub_control,
  4866. .hub_status_data = xhci_hub_status_data,
  4867. .bus_suspend = xhci_bus_suspend,
  4868. .bus_resume = xhci_bus_resume,
  4869. .get_resuming_ports = xhci_get_resuming_ports,
  4870. /*
  4871. * call back when device connected and addressed
  4872. */
  4873. .update_device = xhci_update_device,
  4874. .set_usb2_hw_lpm = xhci_set_usb2_hardware_lpm,
  4875. .enable_usb3_lpm_timeout = xhci_enable_usb3_lpm_timeout,
  4876. .disable_usb3_lpm_timeout = xhci_disable_usb3_lpm_timeout,
  4877. .find_raw_port_number = xhci_find_raw_port_number,
  4878. .clear_tt_buffer_complete = xhci_clear_tt_buffer_complete,
  4879. };
  4880. void xhci_init_driver(struct hc_driver *drv,
  4881. const struct xhci_driver_overrides *over)
  4882. {
  4883. BUG_ON(!over);
  4884. /* Copy the generic table to drv then apply the overrides */
  4885. *drv = xhci_hc_driver;
  4886. if (over) {
  4887. drv->hcd_priv_size += over->extra_priv_size;
  4888. if (over->reset)
  4889. drv->reset = over->reset;
  4890. if (over->start)
  4891. drv->start = over->start;
  4892. if (over->add_endpoint)
  4893. drv->add_endpoint = over->add_endpoint;
  4894. if (over->drop_endpoint)
  4895. drv->drop_endpoint = over->drop_endpoint;
  4896. if (over->check_bandwidth)
  4897. drv->check_bandwidth = over->check_bandwidth;
  4898. if (over->reset_bandwidth)
  4899. drv->reset_bandwidth = over->reset_bandwidth;
  4900. if (over->update_hub_device)
  4901. drv->update_hub_device = over->update_hub_device;
  4902. if (over->address_device)
  4903. drv->address_device = over->address_device;
  4904. if (over->bus_suspend)
  4905. drv->bus_suspend = over->bus_suspend;
  4906. if (over->bus_resume)
  4907. drv->bus_resume = over->bus_resume;
  4908. }
  4909. }
  4910. EXPORT_SYMBOL_GPL(xhci_init_driver);
  4911. MODULE_DESCRIPTION(DRIVER_DESC);
  4912. MODULE_AUTHOR(DRIVER_AUTHOR);
  4913. MODULE_LICENSE("GPL");
  4914. static int __init xhci_hcd_init(void)
  4915. {
  4916. /*
  4917. * Check the compiler generated sizes of structures that must be laid
  4918. * out in specific ways for hardware access.
  4919. */
  4920. BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
  4921. BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
  4922. BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
  4923. /* xhci_device_control has eight fields, and also
  4924. * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
  4925. */
  4926. BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
  4927. BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
  4928. BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
  4929. BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 8*32/8);
  4930. BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
  4931. /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
  4932. BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
  4933. if (usb_disabled())
  4934. return -ENODEV;
  4935. xhci_debugfs_create_root();
  4936. xhci_dbc_init();
  4937. return 0;
  4938. }
  4939. /*
  4940. * If an init function is provided, an exit function must also be provided
  4941. * to allow module unload.
  4942. */
  4943. static void __exit xhci_hcd_fini(void)
  4944. {
  4945. xhci_debugfs_remove_root();
  4946. xhci_dbc_exit();
  4947. }
  4948. module_init(xhci_hcd_init);
  4949. module_exit(xhci_hcd_fini);