xhci-hub.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987
  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/slab.h>
  11. #include <linux/unaligned.h>
  12. #include <linux/bitfield.h>
  13. #include "xhci.h"
  14. #include "xhci-trace.h"
  15. #define PORT_WAKE_BITS (PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E)
  16. #define PORT_RWC_BITS (PORT_CSC | PORT_PEC | PORT_WRC | PORT_OCC | \
  17. PORT_RC | PORT_PLC | PORT_PE)
  18. /* Default sublink speed attribute of each lane */
  19. static u32 ssp_cap_default_ssa[] = {
  20. 0x00050034, /* USB 3.0 SS Gen1x1 id:4 symmetric rx 5Gbps */
  21. 0x000500b4, /* USB 3.0 SS Gen1x1 id:4 symmetric tx 5Gbps */
  22. 0x000a4035, /* USB 3.1 SSP Gen2x1 id:5 symmetric rx 10Gbps */
  23. 0x000a40b5, /* USB 3.1 SSP Gen2x1 id:5 symmetric tx 10Gbps */
  24. 0x00054036, /* USB 3.2 SSP Gen1x2 id:6 symmetric rx 5Gbps */
  25. 0x000540b6, /* USB 3.2 SSP Gen1x2 id:6 symmetric tx 5Gbps */
  26. 0x000a4037, /* USB 3.2 SSP Gen2x2 id:7 symmetric rx 10Gbps */
  27. 0x000a40b7, /* USB 3.2 SSP Gen2x2 id:7 symmetric tx 10Gbps */
  28. };
  29. static int xhci_create_usb3x_bos_desc(struct xhci_hcd *xhci, char *buf,
  30. u16 wLength)
  31. {
  32. struct usb_bos_descriptor *bos;
  33. struct usb_ss_cap_descriptor *ss_cap;
  34. struct usb_ssp_cap_descriptor *ssp_cap;
  35. struct xhci_port_cap *port_cap = NULL;
  36. u16 bcdUSB;
  37. u32 reg;
  38. u32 min_rate = 0;
  39. u8 min_ssid;
  40. u8 ssac;
  41. u8 ssic;
  42. int offset;
  43. int i;
  44. /* BOS descriptor */
  45. bos = (struct usb_bos_descriptor *)buf;
  46. bos->bLength = USB_DT_BOS_SIZE;
  47. bos->bDescriptorType = USB_DT_BOS;
  48. bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE +
  49. USB_DT_USB_SS_CAP_SIZE);
  50. bos->bNumDeviceCaps = 1;
  51. /* Create the descriptor for port with the highest revision */
  52. for (i = 0; i < xhci->num_port_caps; i++) {
  53. u8 major = xhci->port_caps[i].maj_rev;
  54. u8 minor = xhci->port_caps[i].min_rev;
  55. u16 rev = (major << 8) | minor;
  56. if (i == 0 || bcdUSB < rev) {
  57. bcdUSB = rev;
  58. port_cap = &xhci->port_caps[i];
  59. }
  60. }
  61. if (bcdUSB >= 0x0310) {
  62. if (port_cap->psi_count) {
  63. u8 num_sym_ssa = 0;
  64. for (i = 0; i < port_cap->psi_count; i++) {
  65. if ((port_cap->psi[i] & PLT_MASK) == PLT_SYM)
  66. num_sym_ssa++;
  67. }
  68. ssac = port_cap->psi_count + num_sym_ssa - 1;
  69. ssic = port_cap->psi_uid_count - 1;
  70. } else {
  71. if (bcdUSB >= 0x0320)
  72. ssac = 7;
  73. else
  74. ssac = 3;
  75. ssic = (ssac + 1) / 2 - 1;
  76. }
  77. bos->bNumDeviceCaps++;
  78. bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE +
  79. USB_DT_USB_SS_CAP_SIZE +
  80. USB_DT_USB_SSP_CAP_SIZE(ssac));
  81. }
  82. if (wLength < USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE)
  83. return wLength;
  84. /* SuperSpeed USB Device Capability */
  85. ss_cap = (struct usb_ss_cap_descriptor *)&buf[USB_DT_BOS_SIZE];
  86. ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
  87. ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
  88. ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
  89. ss_cap->bmAttributes = 0; /* set later */
  90. ss_cap->wSpeedSupported = cpu_to_le16(USB_5GBPS_OPERATION);
  91. ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
  92. ss_cap->bU1devExitLat = 0; /* set later */
  93. ss_cap->bU2DevExitLat = 0; /* set later */
  94. reg = readl(&xhci->cap_regs->hcc_params);
  95. if (HCC_LTC(reg))
  96. ss_cap->bmAttributes |= USB_LTM_SUPPORT;
  97. if ((xhci->quirks & XHCI_LPM_SUPPORT)) {
  98. reg = readl(&xhci->cap_regs->hcs_params3);
  99. ss_cap->bU1devExitLat = HCS_U1_LATENCY(reg);
  100. ss_cap->bU2DevExitLat = cpu_to_le16(HCS_U2_LATENCY(reg));
  101. }
  102. if (wLength < le16_to_cpu(bos->wTotalLength))
  103. return wLength;
  104. if (bcdUSB < 0x0310)
  105. return le16_to_cpu(bos->wTotalLength);
  106. ssp_cap = (struct usb_ssp_cap_descriptor *)&buf[USB_DT_BOS_SIZE +
  107. USB_DT_USB_SS_CAP_SIZE];
  108. ssp_cap->bLength = USB_DT_USB_SSP_CAP_SIZE(ssac);
  109. ssp_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
  110. ssp_cap->bDevCapabilityType = USB_SSP_CAP_TYPE;
  111. ssp_cap->bReserved = 0;
  112. ssp_cap->wReserved = 0;
  113. ssp_cap->bmAttributes =
  114. cpu_to_le32(FIELD_PREP(USB_SSP_SUBLINK_SPEED_ATTRIBS, ssac) |
  115. FIELD_PREP(USB_SSP_SUBLINK_SPEED_IDS, ssic));
  116. if (!port_cap->psi_count) {
  117. for (i = 0; i < ssac + 1; i++)
  118. ssp_cap->bmSublinkSpeedAttr[i] =
  119. cpu_to_le32(ssp_cap_default_ssa[i]);
  120. min_ssid = 4;
  121. goto out;
  122. }
  123. offset = 0;
  124. for (i = 0; i < port_cap->psi_count; i++) {
  125. u32 psi;
  126. u32 attr;
  127. u8 ssid;
  128. u8 lp;
  129. u8 lse;
  130. u8 psie;
  131. u16 lane_mantissa;
  132. u16 psim;
  133. u16 plt;
  134. psi = port_cap->psi[i];
  135. ssid = XHCI_EXT_PORT_PSIV(psi);
  136. lp = XHCI_EXT_PORT_LP(psi);
  137. psie = XHCI_EXT_PORT_PSIE(psi);
  138. psim = XHCI_EXT_PORT_PSIM(psi);
  139. plt = psi & PLT_MASK;
  140. lse = psie;
  141. lane_mantissa = psim;
  142. /* Shift to Gbps and set SSP Link Protocol if 10Gpbs */
  143. for (; psie < USB_SSP_SUBLINK_SPEED_LSE_GBPS; psie++)
  144. psim /= 1000;
  145. if (!min_rate || psim < min_rate) {
  146. min_ssid = ssid;
  147. min_rate = psim;
  148. }
  149. /* Some host controllers don't set the link protocol for SSP */
  150. if (psim >= 10)
  151. lp = USB_SSP_SUBLINK_SPEED_LP_SSP;
  152. /*
  153. * PSIM and PSIE represent the total speed of PSI. The BOS
  154. * descriptor SSP sublink speed attribute lane mantissa
  155. * describes the lane speed. E.g. PSIM and PSIE for gen2x2
  156. * is 20Gbps, but the BOS descriptor lane speed mantissa is
  157. * 10Gbps. Check and modify the mantissa value to match the
  158. * lane speed.
  159. */
  160. if (bcdUSB == 0x0320 && plt == PLT_SYM) {
  161. /*
  162. * The PSI dword for gen1x2 and gen2x1 share the same
  163. * values. But the lane speed for gen1x2 is 5Gbps while
  164. * gen2x1 is 10Gbps. If the previous PSI dword SSID is
  165. * 5 and the PSIE and PSIM match with SSID 6, let's
  166. * assume that the controller follows the default speed
  167. * id with SSID 6 for gen1x2.
  168. */
  169. if (ssid == 6 && psie == 3 && psim == 10 && i) {
  170. u32 prev = port_cap->psi[i - 1];
  171. if ((prev & PLT_MASK) == PLT_SYM &&
  172. XHCI_EXT_PORT_PSIV(prev) == 5 &&
  173. XHCI_EXT_PORT_PSIE(prev) == 3 &&
  174. XHCI_EXT_PORT_PSIM(prev) == 10) {
  175. lse = USB_SSP_SUBLINK_SPEED_LSE_GBPS;
  176. lane_mantissa = 5;
  177. }
  178. }
  179. if (psie == 3 && psim > 10) {
  180. lse = USB_SSP_SUBLINK_SPEED_LSE_GBPS;
  181. lane_mantissa = 10;
  182. }
  183. }
  184. attr = (FIELD_PREP(USB_SSP_SUBLINK_SPEED_SSID, ssid) |
  185. FIELD_PREP(USB_SSP_SUBLINK_SPEED_LP, lp) |
  186. FIELD_PREP(USB_SSP_SUBLINK_SPEED_LSE, lse) |
  187. FIELD_PREP(USB_SSP_SUBLINK_SPEED_LSM, lane_mantissa));
  188. switch (plt) {
  189. case PLT_SYM:
  190. attr |= FIELD_PREP(USB_SSP_SUBLINK_SPEED_ST,
  191. USB_SSP_SUBLINK_SPEED_ST_SYM_RX);
  192. ssp_cap->bmSublinkSpeedAttr[offset++] = cpu_to_le32(attr);
  193. attr &= ~USB_SSP_SUBLINK_SPEED_ST;
  194. attr |= FIELD_PREP(USB_SSP_SUBLINK_SPEED_ST,
  195. USB_SSP_SUBLINK_SPEED_ST_SYM_TX);
  196. ssp_cap->bmSublinkSpeedAttr[offset++] = cpu_to_le32(attr);
  197. break;
  198. case PLT_ASYM_RX:
  199. attr |= FIELD_PREP(USB_SSP_SUBLINK_SPEED_ST,
  200. USB_SSP_SUBLINK_SPEED_ST_ASYM_RX);
  201. ssp_cap->bmSublinkSpeedAttr[offset++] = cpu_to_le32(attr);
  202. break;
  203. case PLT_ASYM_TX:
  204. attr |= FIELD_PREP(USB_SSP_SUBLINK_SPEED_ST,
  205. USB_SSP_SUBLINK_SPEED_ST_ASYM_TX);
  206. ssp_cap->bmSublinkSpeedAttr[offset++] = cpu_to_le32(attr);
  207. break;
  208. }
  209. }
  210. out:
  211. ssp_cap->wFunctionalitySupport =
  212. cpu_to_le16(FIELD_PREP(USB_SSP_MIN_SUBLINK_SPEED_ATTRIBUTE_ID,
  213. min_ssid) |
  214. FIELD_PREP(USB_SSP_MIN_RX_LANE_COUNT, 1) |
  215. FIELD_PREP(USB_SSP_MIN_TX_LANE_COUNT, 1));
  216. return le16_to_cpu(bos->wTotalLength);
  217. }
  218. static void xhci_common_hub_descriptor(struct xhci_hcd *xhci,
  219. struct usb_hub_descriptor *desc, int ports)
  220. {
  221. u16 temp;
  222. desc->bHubContrCurrent = 0;
  223. desc->bNbrPorts = ports;
  224. temp = 0;
  225. /* Bits 1:0 - support per-port power switching, or power always on */
  226. if (HCC_PPC(xhci->hcc_params))
  227. temp |= HUB_CHAR_INDV_PORT_LPSM;
  228. else
  229. temp |= HUB_CHAR_NO_LPSM;
  230. /* Bit 2 - root hubs are not part of a compound device */
  231. /* Bits 4:3 - individual port over current protection */
  232. temp |= HUB_CHAR_INDV_PORT_OCPM;
  233. /* Bits 6:5 - no TTs in root ports */
  234. /* Bit 7 - no port indicators */
  235. desc->wHubCharacteristics = cpu_to_le16(temp);
  236. }
  237. /* Fill in the USB 2.0 roothub descriptor */
  238. static void xhci_usb2_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  239. struct usb_hub_descriptor *desc)
  240. {
  241. int ports;
  242. u16 temp;
  243. __u8 port_removable[(USB_MAXCHILDREN + 1 + 7) / 8];
  244. u32 portsc;
  245. unsigned int i;
  246. struct xhci_hub *rhub;
  247. rhub = &xhci->usb2_rhub;
  248. ports = rhub->num_ports;
  249. xhci_common_hub_descriptor(xhci, desc, ports);
  250. desc->bDescriptorType = USB_DT_HUB;
  251. temp = 1 + (ports / 8);
  252. desc->bDescLength = USB_DT_HUB_NONVAR_SIZE + 2 * temp;
  253. desc->bPwrOn2PwrGood = 10; /* xhci section 5.4.8 says 20ms */
  254. /* The Device Removable bits are reported on a byte granularity.
  255. * If the port doesn't exist within that byte, the bit is set to 0.
  256. */
  257. memset(port_removable, 0, sizeof(port_removable));
  258. for (i = 0; i < ports; i++) {
  259. portsc = readl(rhub->ports[i]->addr);
  260. /* If a device is removable, PORTSC reports a 0, same as in the
  261. * hub descriptor DeviceRemovable bits.
  262. */
  263. if (portsc & PORT_DEV_REMOVE)
  264. /* This math is hairy because bit 0 of DeviceRemovable
  265. * is reserved, and bit 1 is for port 1, etc.
  266. */
  267. port_removable[(i + 1) / 8] |= 1 << ((i + 1) % 8);
  268. }
  269. /* ch11.h defines a hub descriptor that has room for USB_MAXCHILDREN
  270. * ports on it. The USB 2.0 specification says that there are two
  271. * variable length fields at the end of the hub descriptor:
  272. * DeviceRemovable and PortPwrCtrlMask. But since we can have less than
  273. * USB_MAXCHILDREN ports, we may need to use the DeviceRemovable array
  274. * to set PortPwrCtrlMask bits. PortPwrCtrlMask must always be set to
  275. * 0xFF, so we initialize the both arrays (DeviceRemovable and
  276. * PortPwrCtrlMask) to 0xFF. Then we set the DeviceRemovable for each
  277. * set of ports that actually exist.
  278. */
  279. memset(desc->u.hs.DeviceRemovable, 0xff,
  280. sizeof(desc->u.hs.DeviceRemovable));
  281. memset(desc->u.hs.PortPwrCtrlMask, 0xff,
  282. sizeof(desc->u.hs.PortPwrCtrlMask));
  283. for (i = 0; i < (ports + 1 + 7) / 8; i++)
  284. memset(&desc->u.hs.DeviceRemovable[i], port_removable[i],
  285. sizeof(__u8));
  286. }
  287. /* Fill in the USB 3.0 roothub descriptor */
  288. static void xhci_usb3_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  289. struct usb_hub_descriptor *desc)
  290. {
  291. int ports;
  292. u16 port_removable;
  293. u32 portsc;
  294. unsigned int i;
  295. struct xhci_hub *rhub;
  296. rhub = &xhci->usb3_rhub;
  297. ports = rhub->num_ports;
  298. xhci_common_hub_descriptor(xhci, desc, ports);
  299. desc->bDescriptorType = USB_DT_SS_HUB;
  300. desc->bDescLength = USB_DT_SS_HUB_SIZE;
  301. desc->bPwrOn2PwrGood = 50; /* usb 3.1 may fail if less than 100ms */
  302. /* header decode latency should be zero for roothubs,
  303. * see section 4.23.5.2.
  304. */
  305. desc->u.ss.bHubHdrDecLat = 0;
  306. desc->u.ss.wHubDelay = 0;
  307. port_removable = 0;
  308. /* bit 0 is reserved, bit 1 is for port 1, etc. */
  309. for (i = 0; i < ports; i++) {
  310. portsc = readl(rhub->ports[i]->addr);
  311. if (portsc & PORT_DEV_REMOVE)
  312. port_removable |= 1 << (i + 1);
  313. }
  314. desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable);
  315. }
  316. static void xhci_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  317. struct usb_hub_descriptor *desc)
  318. {
  319. if (hcd->speed >= HCD_USB3)
  320. xhci_usb3_hub_descriptor(hcd, xhci, desc);
  321. else
  322. xhci_usb2_hub_descriptor(hcd, xhci, desc);
  323. }
  324. static unsigned int xhci_port_speed(unsigned int port_status)
  325. {
  326. if (DEV_LOWSPEED(port_status))
  327. return USB_PORT_STAT_LOW_SPEED;
  328. if (DEV_HIGHSPEED(port_status))
  329. return USB_PORT_STAT_HIGH_SPEED;
  330. /*
  331. * FIXME: Yes, we should check for full speed, but the core uses that as
  332. * a default in portspeed() in usb/core/hub.c (which is the only place
  333. * USB_PORT_STAT_*_SPEED is used).
  334. */
  335. return 0;
  336. }
  337. /*
  338. * These bits are Read Only (RO) and should be saved and written to the
  339. * registers: 0, 3, 10:13, 30
  340. * connect status, over-current status, port speed, and device removable.
  341. * connect status and port speed are also sticky - meaning they're in
  342. * the AUX well and they aren't changed by a hot, warm, or cold reset.
  343. */
  344. #define XHCI_PORT_RO ((1<<0) | (1<<3) | (0xf<<10) | (1<<30))
  345. /*
  346. * These bits are RW; writing a 0 clears the bit, writing a 1 sets the bit:
  347. * bits 5:8, 9, 14:15, 25:27
  348. * link state, port power, port indicator state, "wake on" enable state
  349. */
  350. #define XHCI_PORT_RWS ((0xf<<5) | (1<<9) | (0x3<<14) | (0x7<<25))
  351. /*
  352. * These bits are RW; writing a 1 sets the bit, writing a 0 has no effect:
  353. * bit 4 (port reset)
  354. */
  355. #define XHCI_PORT_RW1S ((1<<4))
  356. /*
  357. * These bits are RW; writing a 1 clears the bit, writing a 0 has no effect:
  358. * bits 1, 17, 18, 19, 20, 21, 22, 23
  359. * port enable/disable, and
  360. * change bits: connect, PED, warm port reset changed (reserved zero for USB 2.0 ports),
  361. * over-current, reset, link state, and L1 change
  362. */
  363. #define XHCI_PORT_RW1CS ((1<<1) | (0x7f<<17))
  364. /*
  365. * Bit 16 is RW, and writing a '1' to it causes the link state control to be
  366. * latched in
  367. */
  368. #define XHCI_PORT_RW ((1<<16))
  369. /*
  370. * These bits are Reserved Zero (RsvdZ) and zero should be written to them:
  371. * bits 2, 24, 28:31
  372. */
  373. #define XHCI_PORT_RZ ((1<<2) | (1<<24) | (0xf<<28))
  374. /**
  375. * xhci_port_state_to_neutral() - Clean up read portsc value back into writeable
  376. * @state: u32 port value read from portsc register to be cleanup up
  377. *
  378. * Given a port state, this function returns a value that would result in the
  379. * port being in the same state, if the value was written to the port status
  380. * control register.
  381. * Save Read Only (RO) bits and save read/write bits where
  382. * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
  383. * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
  384. *
  385. * Return: u32 value that can be written back to portsc register without
  386. * changing port state.
  387. */
  388. u32 xhci_port_state_to_neutral(u32 state)
  389. {
  390. /* Save read-only status and port state */
  391. return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
  392. }
  393. EXPORT_SYMBOL_GPL(xhci_port_state_to_neutral);
  394. /*
  395. * Stop device
  396. * It issues stop endpoint command for EP 0 to 30. And wait the last command
  397. * to complete.
  398. * suspend will set to 1, if suspend bit need to set in command.
  399. */
  400. static int xhci_stop_device(struct xhci_hcd *xhci, int slot_id, int suspend)
  401. {
  402. struct xhci_virt_device *virt_dev;
  403. struct xhci_command *cmd;
  404. unsigned long flags;
  405. int ret;
  406. int i;
  407. ret = 0;
  408. virt_dev = xhci->devs[slot_id];
  409. if (!virt_dev)
  410. return -ENODEV;
  411. trace_xhci_stop_device(virt_dev);
  412. cmd = xhci_alloc_command(xhci, true, GFP_NOIO);
  413. if (!cmd)
  414. return -ENOMEM;
  415. spin_lock_irqsave(&xhci->lock, flags);
  416. for (i = LAST_EP_INDEX; i > 0; i--) {
  417. if (virt_dev->eps[i].ring && virt_dev->eps[i].ring->dequeue) {
  418. struct xhci_ep_ctx *ep_ctx;
  419. struct xhci_command *command;
  420. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->out_ctx, i);
  421. /* Check ep is running, required by AMD SNPS 3.1 xHC */
  422. if (GET_EP_CTX_STATE(ep_ctx) != EP_STATE_RUNNING)
  423. continue;
  424. command = xhci_alloc_command(xhci, false, GFP_NOWAIT);
  425. if (!command) {
  426. spin_unlock_irqrestore(&xhci->lock, flags);
  427. ret = -ENOMEM;
  428. goto cmd_cleanup;
  429. }
  430. ret = xhci_queue_stop_endpoint(xhci, command, slot_id,
  431. i, suspend);
  432. if (ret) {
  433. spin_unlock_irqrestore(&xhci->lock, flags);
  434. xhci_free_command(xhci, command);
  435. goto cmd_cleanup;
  436. }
  437. }
  438. }
  439. ret = xhci_queue_stop_endpoint(xhci, cmd, slot_id, 0, suspend);
  440. if (ret) {
  441. spin_unlock_irqrestore(&xhci->lock, flags);
  442. goto cmd_cleanup;
  443. }
  444. xhci_ring_cmd_db(xhci);
  445. spin_unlock_irqrestore(&xhci->lock, flags);
  446. /* Wait for last stop endpoint command to finish */
  447. wait_for_completion(cmd->completion);
  448. if (cmd->status == COMP_COMMAND_ABORTED ||
  449. cmd->status == COMP_COMMAND_RING_STOPPED) {
  450. xhci_warn(xhci, "Timeout while waiting for stop endpoint command\n");
  451. ret = -ETIME;
  452. }
  453. cmd_cleanup:
  454. xhci_free_command(xhci, cmd);
  455. return ret;
  456. }
  457. /*
  458. * Ring device, it rings the all doorbells unconditionally.
  459. */
  460. void xhci_ring_device(struct xhci_hcd *xhci, int slot_id)
  461. {
  462. int i, s;
  463. struct xhci_virt_ep *ep;
  464. for (i = 0; i < LAST_EP_INDEX + 1; i++) {
  465. ep = &xhci->devs[slot_id]->eps[i];
  466. if (ep->ep_state & EP_HAS_STREAMS) {
  467. for (s = 1; s < ep->stream_info->num_streams; s++)
  468. xhci_ring_ep_doorbell(xhci, slot_id, i, s);
  469. } else if (ep->ring && ep->ring->dequeue) {
  470. xhci_ring_ep_doorbell(xhci, slot_id, i, 0);
  471. }
  472. }
  473. return;
  474. }
  475. static void xhci_disable_port(struct xhci_hcd *xhci, struct xhci_port *port)
  476. {
  477. struct usb_hcd *hcd;
  478. u32 portsc;
  479. hcd = port->rhub->hcd;
  480. /* Don't allow the USB core to disable SuperSpeed ports. */
  481. if (hcd->speed >= HCD_USB3) {
  482. xhci_dbg(xhci, "Ignoring request to disable SuperSpeed port.\n");
  483. return;
  484. }
  485. if (xhci->quirks & XHCI_BROKEN_PORT_PED) {
  486. xhci_dbg(xhci,
  487. "Broken Port Enabled/Disabled, ignoring port disable request.\n");
  488. return;
  489. }
  490. portsc = readl(port->addr);
  491. portsc = xhci_port_state_to_neutral(portsc);
  492. /* Write 1 to disable the port */
  493. writel(portsc | PORT_PE, port->addr);
  494. portsc = readl(port->addr);
  495. xhci_dbg(xhci, "disable port %d-%d, portsc: 0x%x\n",
  496. hcd->self.busnum, port->hcd_portnum + 1, portsc);
  497. }
  498. static void xhci_clear_port_change_bit(struct xhci_hcd *xhci, u16 wValue,
  499. u16 wIndex, __le32 __iomem *addr, u32 port_status)
  500. {
  501. char *port_change_bit;
  502. u32 status;
  503. switch (wValue) {
  504. case USB_PORT_FEAT_C_RESET:
  505. status = PORT_RC;
  506. port_change_bit = "reset";
  507. break;
  508. case USB_PORT_FEAT_C_BH_PORT_RESET:
  509. status = PORT_WRC;
  510. port_change_bit = "warm(BH) reset";
  511. break;
  512. case USB_PORT_FEAT_C_CONNECTION:
  513. status = PORT_CSC;
  514. port_change_bit = "connect";
  515. break;
  516. case USB_PORT_FEAT_C_OVER_CURRENT:
  517. status = PORT_OCC;
  518. port_change_bit = "over-current";
  519. break;
  520. case USB_PORT_FEAT_C_ENABLE:
  521. status = PORT_PEC;
  522. port_change_bit = "enable/disable";
  523. break;
  524. case USB_PORT_FEAT_C_SUSPEND:
  525. status = PORT_PLC;
  526. port_change_bit = "suspend/resume";
  527. break;
  528. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  529. status = PORT_PLC;
  530. port_change_bit = "link state";
  531. break;
  532. case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
  533. status = PORT_CEC;
  534. port_change_bit = "config error";
  535. break;
  536. default:
  537. /* Should never happen */
  538. return;
  539. }
  540. /* Change bits are all write 1 to clear */
  541. writel(port_status | status, addr);
  542. port_status = readl(addr);
  543. xhci_dbg(xhci, "clear port%d %s change, portsc: 0x%x\n",
  544. wIndex + 1, port_change_bit, port_status);
  545. }
  546. struct xhci_hub *xhci_get_rhub(struct usb_hcd *hcd)
  547. {
  548. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  549. if (hcd->speed >= HCD_USB3)
  550. return &xhci->usb3_rhub;
  551. return &xhci->usb2_rhub;
  552. }
  553. /*
  554. * xhci_set_port_power() must be called with xhci->lock held.
  555. * It will release and re-aquire the lock while calling ACPI
  556. * method.
  557. */
  558. static void xhci_set_port_power(struct xhci_hcd *xhci, struct xhci_port *port,
  559. bool on, unsigned long *flags)
  560. __must_hold(&xhci->lock)
  561. {
  562. struct usb_hcd *hcd;
  563. u32 temp;
  564. hcd = port->rhub->hcd;
  565. temp = readl(port->addr);
  566. xhci_dbg(xhci, "set port power %d-%d %s, portsc: 0x%x\n",
  567. hcd->self.busnum, port->hcd_portnum + 1, on ? "ON" : "OFF", temp);
  568. temp = xhci_port_state_to_neutral(temp);
  569. if (on) {
  570. /* Power on */
  571. writel(temp | PORT_POWER, port->addr);
  572. readl(port->addr);
  573. } else {
  574. /* Power off */
  575. writel(temp & ~PORT_POWER, port->addr);
  576. }
  577. spin_unlock_irqrestore(&xhci->lock, *flags);
  578. temp = usb_acpi_power_manageable(hcd->self.root_hub,
  579. port->hcd_portnum);
  580. if (temp)
  581. usb_acpi_set_power_state(hcd->self.root_hub,
  582. port->hcd_portnum, on);
  583. spin_lock_irqsave(&xhci->lock, *flags);
  584. }
  585. static void xhci_port_set_test_mode(struct xhci_hcd *xhci,
  586. u16 test_mode, u16 wIndex)
  587. {
  588. u32 temp;
  589. struct xhci_port *port;
  590. /* xhci only supports test mode for usb2 ports */
  591. port = xhci->usb2_rhub.ports[wIndex];
  592. temp = readl(port->addr + PORTPMSC);
  593. temp |= test_mode << PORT_TEST_MODE_SHIFT;
  594. writel(temp, port->addr + PORTPMSC);
  595. xhci->test_mode = test_mode;
  596. if (test_mode == USB_TEST_FORCE_ENABLE)
  597. xhci_start(xhci);
  598. }
  599. static int xhci_enter_test_mode(struct xhci_hcd *xhci,
  600. u16 test_mode, u16 wIndex, unsigned long *flags)
  601. __must_hold(&xhci->lock)
  602. {
  603. int i, retval;
  604. /* Disable all Device Slots */
  605. xhci_dbg(xhci, "Disable all slots\n");
  606. spin_unlock_irqrestore(&xhci->lock, *flags);
  607. for (i = 1; i <= HCS_MAX_SLOTS(xhci->hcs_params1); i++) {
  608. if (!xhci->devs[i])
  609. continue;
  610. retval = xhci_disable_slot(xhci, i);
  611. xhci_free_virt_device(xhci, i);
  612. if (retval)
  613. xhci_err(xhci, "Failed to disable slot %d, %d. Enter test mode anyway\n",
  614. i, retval);
  615. }
  616. spin_lock_irqsave(&xhci->lock, *flags);
  617. /* Put all ports to the Disable state by clear PP */
  618. xhci_dbg(xhci, "Disable all port (PP = 0)\n");
  619. /* Power off USB3 ports*/
  620. for (i = 0; i < xhci->usb3_rhub.num_ports; i++)
  621. xhci_set_port_power(xhci, xhci->usb3_rhub.ports[i], false, flags);
  622. /* Power off USB2 ports*/
  623. for (i = 0; i < xhci->usb2_rhub.num_ports; i++)
  624. xhci_set_port_power(xhci, xhci->usb2_rhub.ports[i], false, flags);
  625. /* Stop the controller */
  626. xhci_dbg(xhci, "Stop controller\n");
  627. retval = xhci_halt(xhci);
  628. if (retval)
  629. return retval;
  630. /* Disable runtime PM for test mode */
  631. pm_runtime_forbid(xhci_to_hcd(xhci)->self.controller);
  632. /* Set PORTPMSC.PTC field to enter selected test mode */
  633. /* Port is selected by wIndex. port_id = wIndex + 1 */
  634. xhci_dbg(xhci, "Enter Test Mode: %d, Port_id=%d\n",
  635. test_mode, wIndex + 1);
  636. xhci_port_set_test_mode(xhci, test_mode, wIndex);
  637. return retval;
  638. }
  639. static int xhci_exit_test_mode(struct xhci_hcd *xhci)
  640. {
  641. int retval;
  642. if (!xhci->test_mode) {
  643. xhci_err(xhci, "Not in test mode, do nothing.\n");
  644. return 0;
  645. }
  646. if (xhci->test_mode == USB_TEST_FORCE_ENABLE &&
  647. !(xhci->xhc_state & XHCI_STATE_HALTED)) {
  648. retval = xhci_halt(xhci);
  649. if (retval)
  650. return retval;
  651. }
  652. pm_runtime_allow(xhci_to_hcd(xhci)->self.controller);
  653. xhci->test_mode = 0;
  654. return xhci_reset(xhci, XHCI_RESET_SHORT_USEC);
  655. }
  656. /**
  657. * xhci_port_is_tunneled() - Check if USB3 connection is tunneled over USB4
  658. * @xhci: xhci host controller
  659. * @port: USB3 port to be checked.
  660. *
  661. * Some hosts can detect if a USB3 connection is native USB3 or tunneled over
  662. * USB4. Intel hosts expose this via vendor specific extended capability 206
  663. * eSS PORT registers TUNEN (tunnel enabled) bit.
  664. *
  665. * A USB3 device must be connected to the port to detect the tunnel.
  666. *
  667. * Return: link tunnel mode enum, USB_LINK_UNKNOWN if host is incapable of
  668. * detecting USB3 over USB4 tunnels. USB_LINK_NATIVE or USB_LINK_TUNNELED
  669. * otherwise.
  670. */
  671. enum usb_link_tunnel_mode xhci_port_is_tunneled(struct xhci_hcd *xhci,
  672. struct xhci_port *port)
  673. {
  674. void __iomem *base;
  675. u32 offset;
  676. base = &xhci->cap_regs->hc_capbase;
  677. offset = xhci_find_next_ext_cap(base, 0, XHCI_EXT_CAPS_INTEL_SPR_SHADOW);
  678. if (offset && offset <= XHCI_INTEL_SPR_ESS_PORT_OFFSET) {
  679. offset = XHCI_INTEL_SPR_ESS_PORT_OFFSET + port->hcd_portnum * 0x20;
  680. if (readl(base + offset) & XHCI_INTEL_SPR_TUNEN)
  681. return USB_LINK_TUNNELED;
  682. else
  683. return USB_LINK_NATIVE;
  684. }
  685. return USB_LINK_UNKNOWN;
  686. }
  687. void xhci_set_link_state(struct xhci_hcd *xhci, struct xhci_port *port,
  688. u32 link_state)
  689. {
  690. u32 temp;
  691. u32 portsc;
  692. portsc = readl(port->addr);
  693. temp = xhci_port_state_to_neutral(portsc);
  694. temp &= ~PORT_PLS_MASK;
  695. temp |= PORT_LINK_STROBE | link_state;
  696. writel(temp, port->addr);
  697. xhci_dbg(xhci, "Set port %d-%d link state, portsc: 0x%x, write 0x%x",
  698. port->rhub->hcd->self.busnum, port->hcd_portnum + 1,
  699. portsc, temp);
  700. }
  701. static void xhci_set_remote_wake_mask(struct xhci_hcd *xhci,
  702. struct xhci_port *port, u16 wake_mask)
  703. {
  704. u32 temp;
  705. temp = readl(port->addr);
  706. temp = xhci_port_state_to_neutral(temp);
  707. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_CONNECT)
  708. temp |= PORT_WKCONN_E;
  709. else
  710. temp &= ~PORT_WKCONN_E;
  711. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT)
  712. temp |= PORT_WKDISC_E;
  713. else
  714. temp &= ~PORT_WKDISC_E;
  715. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT)
  716. temp |= PORT_WKOC_E;
  717. else
  718. temp &= ~PORT_WKOC_E;
  719. writel(temp, port->addr);
  720. }
  721. /* Test and clear port RWC bit */
  722. void xhci_test_and_clear_bit(struct xhci_hcd *xhci, struct xhci_port *port,
  723. u32 port_bit)
  724. {
  725. u32 temp;
  726. temp = readl(port->addr);
  727. if (temp & port_bit) {
  728. temp = xhci_port_state_to_neutral(temp);
  729. temp |= port_bit;
  730. writel(temp, port->addr);
  731. }
  732. }
  733. /* Updates Link Status for super Speed port */
  734. static void xhci_hub_report_usb3_link_state(struct xhci_hcd *xhci,
  735. u32 *status, u32 status_reg)
  736. {
  737. u32 pls = status_reg & PORT_PLS_MASK;
  738. /* When the CAS bit is set then warm reset
  739. * should be performed on port
  740. */
  741. if (status_reg & PORT_CAS) {
  742. /* The CAS bit can be set while the port is
  743. * in any link state.
  744. * Only roothubs have CAS bit, so we
  745. * pretend to be in compliance mode
  746. * unless we're already in compliance
  747. * or the inactive state.
  748. */
  749. if (pls != USB_SS_PORT_LS_COMP_MOD &&
  750. pls != USB_SS_PORT_LS_SS_INACTIVE) {
  751. pls = USB_SS_PORT_LS_COMP_MOD;
  752. }
  753. /* Return also connection bit -
  754. * hub state machine resets port
  755. * when this bit is set.
  756. */
  757. pls |= USB_PORT_STAT_CONNECTION;
  758. } else {
  759. /*
  760. * Resume state is an xHCI internal state. Do not report it to
  761. * usb core, instead, pretend to be U3, thus usb core knows
  762. * it's not ready for transfer.
  763. */
  764. if (pls == XDEV_RESUME) {
  765. *status |= USB_SS_PORT_LS_U3;
  766. return;
  767. }
  768. /*
  769. * If CAS bit isn't set but the Port is already at
  770. * Compliance Mode, fake a connection so the USB core
  771. * notices the Compliance state and resets the port.
  772. * This resolves an issue generated by the SN65LVPE502CP
  773. * in which sometimes the port enters compliance mode
  774. * caused by a delay on the host-device negotiation.
  775. */
  776. if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
  777. (pls == USB_SS_PORT_LS_COMP_MOD))
  778. pls |= USB_PORT_STAT_CONNECTION;
  779. }
  780. /* update status field */
  781. *status |= pls;
  782. }
  783. /*
  784. * Function for Compliance Mode Quirk.
  785. *
  786. * This Function verifies if all xhc USB3 ports have entered U0, if so,
  787. * the compliance mode timer is deleted. A port won't enter
  788. * compliance mode if it has previously entered U0.
  789. */
  790. static void xhci_del_comp_mod_timer(struct xhci_hcd *xhci, u32 status,
  791. u16 wIndex)
  792. {
  793. u32 all_ports_seen_u0 = ((1 << xhci->usb3_rhub.num_ports) - 1);
  794. bool port_in_u0 = ((status & PORT_PLS_MASK) == XDEV_U0);
  795. if (!(xhci->quirks & XHCI_COMP_MODE_QUIRK))
  796. return;
  797. if ((xhci->port_status_u0 != all_ports_seen_u0) && port_in_u0) {
  798. xhci->port_status_u0 |= 1 << wIndex;
  799. if (xhci->port_status_u0 == all_ports_seen_u0) {
  800. del_timer_sync(&xhci->comp_mode_recovery_timer);
  801. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  802. "All USB3 ports have entered U0 already!");
  803. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  804. "Compliance Mode Recovery Timer Deleted.");
  805. }
  806. }
  807. }
  808. static int xhci_handle_usb2_port_link_resume(struct xhci_port *port,
  809. u32 portsc,
  810. unsigned long *flags)
  811. {
  812. struct xhci_bus_state *bus_state;
  813. struct xhci_hcd *xhci;
  814. struct usb_hcd *hcd;
  815. u32 wIndex;
  816. hcd = port->rhub->hcd;
  817. bus_state = &port->rhub->bus_state;
  818. xhci = hcd_to_xhci(hcd);
  819. wIndex = port->hcd_portnum;
  820. if ((portsc & PORT_RESET) || !(portsc & PORT_PE)) {
  821. return -EINVAL;
  822. }
  823. /* did port event handler already start resume timing? */
  824. if (!port->resume_timestamp) {
  825. /* If not, maybe we are in a host initated resume? */
  826. if (test_bit(wIndex, &bus_state->resuming_ports)) {
  827. /* Host initated resume doesn't time the resume
  828. * signalling using resume_done[].
  829. * It manually sets RESUME state, sleeps 20ms
  830. * and sets U0 state. This should probably be
  831. * changed, but not right now.
  832. */
  833. } else {
  834. /* port resume was discovered now and here,
  835. * start resume timing
  836. */
  837. unsigned long timeout = jiffies +
  838. msecs_to_jiffies(USB_RESUME_TIMEOUT);
  839. set_bit(wIndex, &bus_state->resuming_ports);
  840. port->resume_timestamp = timeout;
  841. mod_timer(&hcd->rh_timer, timeout);
  842. usb_hcd_start_port_resume(&hcd->self, wIndex);
  843. }
  844. /* Has resume been signalled for USB_RESUME_TIME yet? */
  845. } else if (time_after_eq(jiffies, port->resume_timestamp)) {
  846. int time_left;
  847. xhci_dbg(xhci, "resume USB2 port %d-%d\n",
  848. hcd->self.busnum, wIndex + 1);
  849. port->resume_timestamp = 0;
  850. clear_bit(wIndex, &bus_state->resuming_ports);
  851. reinit_completion(&port->rexit_done);
  852. port->rexit_active = true;
  853. xhci_test_and_clear_bit(xhci, port, PORT_PLC);
  854. xhci_set_link_state(xhci, port, XDEV_U0);
  855. spin_unlock_irqrestore(&xhci->lock, *flags);
  856. time_left = wait_for_completion_timeout(
  857. &port->rexit_done,
  858. msecs_to_jiffies(XHCI_MAX_REXIT_TIMEOUT_MS));
  859. spin_lock_irqsave(&xhci->lock, *flags);
  860. if (time_left) {
  861. if (!port->slot_id) {
  862. xhci_dbg(xhci, "slot_id is zero\n");
  863. return -ENODEV;
  864. }
  865. xhci_ring_device(xhci, port->slot_id);
  866. } else {
  867. int port_status = readl(port->addr);
  868. xhci_warn(xhci, "Port resume timed out, port %d-%d: 0x%x\n",
  869. hcd->self.busnum, wIndex + 1, port_status);
  870. /*
  871. * keep rexit_active set if U0 transition failed so we
  872. * know to report PORT_STAT_SUSPEND status back to
  873. * usbcore. It will be cleared later once the port is
  874. * out of RESUME/U3 state
  875. */
  876. }
  877. usb_hcd_end_port_resume(&hcd->self, wIndex);
  878. bus_state->port_c_suspend |= 1 << wIndex;
  879. bus_state->suspended_ports &= ~(1 << wIndex);
  880. }
  881. return 0;
  882. }
  883. static u32 xhci_get_ext_port_status(u32 raw_port_status, u32 port_li)
  884. {
  885. u32 ext_stat = 0;
  886. int speed_id;
  887. /* only support rx and tx lane counts of 1 in usb3.1 spec */
  888. speed_id = DEV_PORT_SPEED(raw_port_status);
  889. ext_stat |= speed_id; /* bits 3:0, RX speed id */
  890. ext_stat |= speed_id << 4; /* bits 7:4, TX speed id */
  891. ext_stat |= PORT_RX_LANES(port_li) << 8; /* bits 11:8 Rx lane count */
  892. ext_stat |= PORT_TX_LANES(port_li) << 12; /* bits 15:12 Tx lane count */
  893. return ext_stat;
  894. }
  895. static void xhci_get_usb3_port_status(struct xhci_port *port, u32 *status,
  896. u32 portsc)
  897. {
  898. struct xhci_bus_state *bus_state;
  899. struct xhci_hcd *xhci;
  900. struct usb_hcd *hcd;
  901. u32 link_state;
  902. u32 portnum;
  903. bus_state = &port->rhub->bus_state;
  904. xhci = hcd_to_xhci(port->rhub->hcd);
  905. hcd = port->rhub->hcd;
  906. link_state = portsc & PORT_PLS_MASK;
  907. portnum = port->hcd_portnum;
  908. /* USB3 specific wPortChange bits
  909. *
  910. * Port link change with port in resume state should not be
  911. * reported to usbcore, as this is an internal state to be
  912. * handled by xhci driver. Reporting PLC to usbcore may
  913. * cause usbcore clearing PLC first and port change event
  914. * irq won't be generated.
  915. */
  916. if (portsc & PORT_PLC && (link_state != XDEV_RESUME))
  917. *status |= USB_PORT_STAT_C_LINK_STATE << 16;
  918. if (portsc & PORT_WRC)
  919. *status |= USB_PORT_STAT_C_BH_RESET << 16;
  920. if (portsc & PORT_CEC)
  921. *status |= USB_PORT_STAT_C_CONFIG_ERROR << 16;
  922. /* USB3 specific wPortStatus bits */
  923. if (portsc & PORT_POWER)
  924. *status |= USB_SS_PORT_STAT_POWER;
  925. /* no longer suspended or resuming */
  926. if (link_state != XDEV_U3 &&
  927. link_state != XDEV_RESUME &&
  928. link_state != XDEV_RECOVERY) {
  929. /* remote wake resume signaling complete */
  930. if (bus_state->port_remote_wakeup & (1 << portnum)) {
  931. bus_state->port_remote_wakeup &= ~(1 << portnum);
  932. usb_hcd_end_port_resume(&hcd->self, portnum);
  933. }
  934. bus_state->suspended_ports &= ~(1 << portnum);
  935. }
  936. xhci_hub_report_usb3_link_state(xhci, status, portsc);
  937. xhci_del_comp_mod_timer(xhci, portsc, portnum);
  938. }
  939. static void xhci_get_usb2_port_status(struct xhci_port *port, u32 *status,
  940. u32 portsc, unsigned long *flags)
  941. {
  942. struct xhci_bus_state *bus_state;
  943. u32 link_state;
  944. u32 portnum;
  945. int err;
  946. bus_state = &port->rhub->bus_state;
  947. link_state = portsc & PORT_PLS_MASK;
  948. portnum = port->hcd_portnum;
  949. /* USB2 wPortStatus bits */
  950. if (portsc & PORT_POWER) {
  951. *status |= USB_PORT_STAT_POWER;
  952. /* link state is only valid if port is powered */
  953. if (link_state == XDEV_U3)
  954. *status |= USB_PORT_STAT_SUSPEND;
  955. if (link_state == XDEV_U2)
  956. *status |= USB_PORT_STAT_L1;
  957. if (link_state == XDEV_U0) {
  958. if (bus_state->suspended_ports & (1 << portnum)) {
  959. bus_state->suspended_ports &= ~(1 << portnum);
  960. bus_state->port_c_suspend |= 1 << portnum;
  961. }
  962. }
  963. if (link_state == XDEV_RESUME) {
  964. err = xhci_handle_usb2_port_link_resume(port, portsc,
  965. flags);
  966. if (err < 0)
  967. *status = 0xffffffff;
  968. else if (port->resume_timestamp || port->rexit_active)
  969. *status |= USB_PORT_STAT_SUSPEND;
  970. }
  971. }
  972. /*
  973. * Clear usb2 resume signalling variables if port is no longer suspended
  974. * or resuming. Port either resumed to U0/U1/U2, disconnected, or in a
  975. * error state. Resume related variables should be cleared in all those cases.
  976. */
  977. if (link_state != XDEV_U3 && link_state != XDEV_RESUME) {
  978. if (port->resume_timestamp ||
  979. test_bit(portnum, &bus_state->resuming_ports)) {
  980. port->resume_timestamp = 0;
  981. clear_bit(portnum, &bus_state->resuming_ports);
  982. usb_hcd_end_port_resume(&port->rhub->hcd->self, portnum);
  983. }
  984. port->rexit_active = 0;
  985. bus_state->suspended_ports &= ~(1 << portnum);
  986. }
  987. }
  988. /*
  989. * Converts a raw xHCI port status into the format that external USB 2.0 or USB
  990. * 3.0 hubs use.
  991. *
  992. * Possible side effects:
  993. * - Mark a port as being done with device resume,
  994. * and ring the endpoint doorbells.
  995. * - Stop the Synopsys redriver Compliance Mode polling.
  996. * - Drop and reacquire the xHCI lock, in order to wait for port resume.
  997. */
  998. static u32 xhci_get_port_status(struct usb_hcd *hcd,
  999. struct xhci_bus_state *bus_state,
  1000. u16 wIndex, u32 raw_port_status,
  1001. unsigned long *flags)
  1002. __releases(&xhci->lock)
  1003. __acquires(&xhci->lock)
  1004. {
  1005. u32 status = 0;
  1006. struct xhci_hub *rhub;
  1007. struct xhci_port *port;
  1008. rhub = xhci_get_rhub(hcd);
  1009. port = rhub->ports[wIndex];
  1010. /* common wPortChange bits */
  1011. if (raw_port_status & PORT_CSC)
  1012. status |= USB_PORT_STAT_C_CONNECTION << 16;
  1013. if (raw_port_status & PORT_PEC)
  1014. status |= USB_PORT_STAT_C_ENABLE << 16;
  1015. if ((raw_port_status & PORT_OCC))
  1016. status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  1017. if ((raw_port_status & PORT_RC))
  1018. status |= USB_PORT_STAT_C_RESET << 16;
  1019. /* common wPortStatus bits */
  1020. if (raw_port_status & PORT_CONNECT) {
  1021. status |= USB_PORT_STAT_CONNECTION;
  1022. status |= xhci_port_speed(raw_port_status);
  1023. }
  1024. if (raw_port_status & PORT_PE)
  1025. status |= USB_PORT_STAT_ENABLE;
  1026. if (raw_port_status & PORT_OC)
  1027. status |= USB_PORT_STAT_OVERCURRENT;
  1028. if (raw_port_status & PORT_RESET)
  1029. status |= USB_PORT_STAT_RESET;
  1030. /* USB2 and USB3 specific bits, including Port Link State */
  1031. if (hcd->speed >= HCD_USB3)
  1032. xhci_get_usb3_port_status(port, &status, raw_port_status);
  1033. else
  1034. xhci_get_usb2_port_status(port, &status, raw_port_status,
  1035. flags);
  1036. if (bus_state->port_c_suspend & (1 << wIndex))
  1037. status |= USB_PORT_STAT_C_SUSPEND << 16;
  1038. return status;
  1039. }
  1040. int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  1041. u16 wIndex, char *buf, u16 wLength)
  1042. {
  1043. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1044. int max_ports;
  1045. unsigned long flags;
  1046. u32 temp, status;
  1047. int retval = 0;
  1048. struct xhci_bus_state *bus_state;
  1049. u16 link_state = 0;
  1050. u16 wake_mask = 0;
  1051. u16 timeout = 0;
  1052. u16 test_mode = 0;
  1053. struct xhci_hub *rhub;
  1054. struct xhci_port **ports;
  1055. struct xhci_port *port;
  1056. int portnum1;
  1057. rhub = xhci_get_rhub(hcd);
  1058. ports = rhub->ports;
  1059. max_ports = rhub->num_ports;
  1060. bus_state = &rhub->bus_state;
  1061. portnum1 = wIndex & 0xff;
  1062. spin_lock_irqsave(&xhci->lock, flags);
  1063. switch (typeReq) {
  1064. case GetHubStatus:
  1065. /* No power source, over-current reported per port */
  1066. memset(buf, 0, 4);
  1067. break;
  1068. case GetHubDescriptor:
  1069. /* Check to make sure userspace is asking for the USB 3.0 hub
  1070. * descriptor for the USB 3.0 roothub. If not, we stall the
  1071. * endpoint, like external hubs do.
  1072. */
  1073. if (hcd->speed >= HCD_USB3 &&
  1074. (wLength < USB_DT_SS_HUB_SIZE ||
  1075. wValue != (USB_DT_SS_HUB << 8))) {
  1076. xhci_dbg(xhci, "Wrong hub descriptor type for "
  1077. "USB 3.0 roothub.\n");
  1078. goto error;
  1079. }
  1080. xhci_hub_descriptor(hcd, xhci,
  1081. (struct usb_hub_descriptor *) buf);
  1082. break;
  1083. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  1084. if ((wValue & 0xff00) != (USB_DT_BOS << 8))
  1085. goto error;
  1086. if (hcd->speed < HCD_USB3)
  1087. goto error;
  1088. retval = xhci_create_usb3x_bos_desc(xhci, buf, wLength);
  1089. spin_unlock_irqrestore(&xhci->lock, flags);
  1090. return retval;
  1091. case GetPortStatus:
  1092. if (!portnum1 || portnum1 > max_ports)
  1093. goto error;
  1094. wIndex--;
  1095. port = ports[portnum1 - 1];
  1096. temp = readl(port->addr);
  1097. if (temp == ~(u32)0) {
  1098. xhci_hc_died(xhci);
  1099. retval = -ENODEV;
  1100. break;
  1101. }
  1102. trace_xhci_get_port_status(port, temp);
  1103. status = xhci_get_port_status(hcd, bus_state, wIndex, temp,
  1104. &flags);
  1105. if (status == 0xffffffff)
  1106. goto error;
  1107. xhci_dbg(xhci, "Get port status %d-%d read: 0x%x, return 0x%x",
  1108. hcd->self.busnum, portnum1, temp, status);
  1109. put_unaligned(cpu_to_le32(status), (__le32 *) buf);
  1110. /* if USB 3.1 extended port status return additional 4 bytes */
  1111. if (wValue == 0x02) {
  1112. u32 port_li;
  1113. if (hcd->speed < HCD_USB31 || wLength != 8) {
  1114. xhci_err(xhci, "get ext port status invalid parameter\n");
  1115. retval = -EINVAL;
  1116. break;
  1117. }
  1118. port_li = readl(port->addr + PORTLI);
  1119. status = xhci_get_ext_port_status(temp, port_li);
  1120. put_unaligned_le32(status, &buf[4]);
  1121. }
  1122. break;
  1123. case SetPortFeature:
  1124. if (wValue == USB_PORT_FEAT_LINK_STATE)
  1125. link_state = (wIndex & 0xff00) >> 3;
  1126. if (wValue == USB_PORT_FEAT_REMOTE_WAKE_MASK)
  1127. wake_mask = wIndex & 0xff00;
  1128. if (wValue == USB_PORT_FEAT_TEST)
  1129. test_mode = (wIndex & 0xff00) >> 8;
  1130. /* The MSB of wIndex is the U1/U2 timeout */
  1131. timeout = (wIndex & 0xff00) >> 8;
  1132. wIndex &= 0xff;
  1133. if (!portnum1 || portnum1 > max_ports)
  1134. goto error;
  1135. port = ports[portnum1 - 1];
  1136. wIndex--;
  1137. temp = readl(port->addr);
  1138. if (temp == ~(u32)0) {
  1139. xhci_hc_died(xhci);
  1140. retval = -ENODEV;
  1141. break;
  1142. }
  1143. temp = xhci_port_state_to_neutral(temp);
  1144. /* FIXME: What new port features do we need to support? */
  1145. switch (wValue) {
  1146. case USB_PORT_FEAT_SUSPEND:
  1147. temp = readl(port->addr);
  1148. if ((temp & PORT_PLS_MASK) != XDEV_U0) {
  1149. /* Resume the port to U0 first */
  1150. xhci_set_link_state(xhci, port, XDEV_U0);
  1151. spin_unlock_irqrestore(&xhci->lock, flags);
  1152. msleep(10);
  1153. spin_lock_irqsave(&xhci->lock, flags);
  1154. }
  1155. /* In spec software should not attempt to suspend
  1156. * a port unless the port reports that it is in the
  1157. * enabled (PED = ‘1’,PLS < ‘3’) state.
  1158. */
  1159. temp = readl(port->addr);
  1160. if ((temp & PORT_PE) == 0 || (temp & PORT_RESET)
  1161. || (temp & PORT_PLS_MASK) >= XDEV_U3) {
  1162. xhci_warn(xhci, "USB core suspending port %d-%d not in U0/U1/U2\n",
  1163. hcd->self.busnum, portnum1);
  1164. goto error;
  1165. }
  1166. if (!port->slot_id) {
  1167. xhci_warn(xhci, "slot_id is zero\n");
  1168. goto error;
  1169. }
  1170. /* unlock to execute stop endpoint commands */
  1171. spin_unlock_irqrestore(&xhci->lock, flags);
  1172. xhci_stop_device(xhci, port->slot_id, 1);
  1173. spin_lock_irqsave(&xhci->lock, flags);
  1174. xhci_set_link_state(xhci, port, XDEV_U3);
  1175. spin_unlock_irqrestore(&xhci->lock, flags);
  1176. msleep(10); /* wait device to enter */
  1177. spin_lock_irqsave(&xhci->lock, flags);
  1178. temp = readl(port->addr);
  1179. bus_state->suspended_ports |= 1 << wIndex;
  1180. break;
  1181. case USB_PORT_FEAT_LINK_STATE:
  1182. temp = readl(port->addr);
  1183. /* Disable port */
  1184. if (link_state == USB_SS_PORT_LS_SS_DISABLED) {
  1185. xhci_dbg(xhci, "Disable port %d-%d\n",
  1186. hcd->self.busnum, portnum1);
  1187. temp = xhci_port_state_to_neutral(temp);
  1188. /*
  1189. * Clear all change bits, so that we get a new
  1190. * connection event.
  1191. */
  1192. temp |= PORT_CSC | PORT_PEC | PORT_WRC |
  1193. PORT_OCC | PORT_RC | PORT_PLC |
  1194. PORT_CEC;
  1195. writel(temp | PORT_PE, port->addr);
  1196. temp = readl(port->addr);
  1197. break;
  1198. }
  1199. /* Put link in RxDetect (enable port) */
  1200. if (link_state == USB_SS_PORT_LS_RX_DETECT) {
  1201. xhci_dbg(xhci, "Enable port %d-%d\n",
  1202. hcd->self.busnum, portnum1);
  1203. xhci_set_link_state(xhci, port, link_state);
  1204. temp = readl(port->addr);
  1205. break;
  1206. }
  1207. /*
  1208. * For xHCI 1.1 according to section 4.19.1.2.4.1 a
  1209. * root hub port's transition to compliance mode upon
  1210. * detecting LFPS timeout may be controlled by an
  1211. * Compliance Transition Enabled (CTE) flag (not
  1212. * software visible). This flag is set by writing 0xA
  1213. * to PORTSC PLS field which will allow transition to
  1214. * compliance mode the next time LFPS timeout is
  1215. * encountered. A warm reset will clear it.
  1216. *
  1217. * The CTE flag is only supported if the HCCPARAMS2 CTC
  1218. * flag is set, otherwise, the compliance substate is
  1219. * automatically entered as on 1.0 and prior.
  1220. */
  1221. if (link_state == USB_SS_PORT_LS_COMP_MOD) {
  1222. if (!HCC2_CTC(xhci->hcc_params2)) {
  1223. xhci_dbg(xhci, "CTC flag is 0, port already supports entering compliance mode\n");
  1224. break;
  1225. }
  1226. if ((temp & PORT_CONNECT)) {
  1227. xhci_warn(xhci, "Can't set compliance mode when port is connected\n");
  1228. goto error;
  1229. }
  1230. xhci_dbg(xhci, "Enable compliance mode transition for port %d-%d\n",
  1231. hcd->self.busnum, portnum1);
  1232. xhci_set_link_state(xhci, port, link_state);
  1233. temp = readl(port->addr);
  1234. break;
  1235. }
  1236. /* Port must be enabled */
  1237. if (!(temp & PORT_PE)) {
  1238. retval = -ENODEV;
  1239. break;
  1240. }
  1241. /* Can't set port link state above '3' (U3) */
  1242. if (link_state > USB_SS_PORT_LS_U3) {
  1243. xhci_warn(xhci, "Cannot set port %d-%d link state %d\n",
  1244. hcd->self.busnum, portnum1, link_state);
  1245. goto error;
  1246. }
  1247. /*
  1248. * set link to U0, steps depend on current link state.
  1249. * U3: set link to U0 and wait for u3exit completion.
  1250. * U1/U2: no PLC complete event, only set link to U0.
  1251. * Resume/Recovery: device initiated U0, only wait for
  1252. * completion
  1253. */
  1254. if (link_state == USB_SS_PORT_LS_U0) {
  1255. u32 pls = temp & PORT_PLS_MASK;
  1256. bool wait_u0 = false;
  1257. /* already in U0 */
  1258. if (pls == XDEV_U0)
  1259. break;
  1260. if (pls == XDEV_U3 ||
  1261. pls == XDEV_RESUME ||
  1262. pls == XDEV_RECOVERY) {
  1263. wait_u0 = true;
  1264. reinit_completion(&port->u3exit_done);
  1265. }
  1266. if (pls <= XDEV_U3) /* U1, U2, U3 */
  1267. xhci_set_link_state(xhci, port, USB_SS_PORT_LS_U0);
  1268. if (!wait_u0) {
  1269. if (pls > XDEV_U3)
  1270. goto error;
  1271. break;
  1272. }
  1273. spin_unlock_irqrestore(&xhci->lock, flags);
  1274. if (!wait_for_completion_timeout(&port->u3exit_done,
  1275. msecs_to_jiffies(500)))
  1276. xhci_dbg(xhci, "missing U0 port change event for port %d-%d\n",
  1277. hcd->self.busnum, portnum1);
  1278. spin_lock_irqsave(&xhci->lock, flags);
  1279. temp = readl(port->addr);
  1280. break;
  1281. }
  1282. if (link_state == USB_SS_PORT_LS_U3) {
  1283. int retries = 16;
  1284. if (port->slot_id) {
  1285. /* unlock to execute stop endpoint
  1286. * commands */
  1287. spin_unlock_irqrestore(&xhci->lock,
  1288. flags);
  1289. xhci_stop_device(xhci, port->slot_id, 1);
  1290. spin_lock_irqsave(&xhci->lock, flags);
  1291. }
  1292. xhci_set_link_state(xhci, port, USB_SS_PORT_LS_U3);
  1293. spin_unlock_irqrestore(&xhci->lock, flags);
  1294. while (retries--) {
  1295. usleep_range(4000, 8000);
  1296. temp = readl(port->addr);
  1297. if ((temp & PORT_PLS_MASK) == XDEV_U3)
  1298. break;
  1299. }
  1300. spin_lock_irqsave(&xhci->lock, flags);
  1301. temp = readl(port->addr);
  1302. bus_state->suspended_ports |= 1 << wIndex;
  1303. }
  1304. break;
  1305. case USB_PORT_FEAT_POWER:
  1306. /*
  1307. * Turn on ports, even if there isn't per-port switching.
  1308. * HC will report connect events even before this is set.
  1309. * However, hub_wq will ignore the roothub events until
  1310. * the roothub is registered.
  1311. */
  1312. xhci_set_port_power(xhci, port, true, &flags);
  1313. break;
  1314. case USB_PORT_FEAT_RESET:
  1315. temp = (temp | PORT_RESET);
  1316. writel(temp, port->addr);
  1317. temp = readl(port->addr);
  1318. xhci_dbg(xhci, "set port reset, actual port %d-%d status = 0x%x\n",
  1319. hcd->self.busnum, portnum1, temp);
  1320. break;
  1321. case USB_PORT_FEAT_REMOTE_WAKE_MASK:
  1322. xhci_set_remote_wake_mask(xhci, port, wake_mask);
  1323. temp = readl(port->addr);
  1324. xhci_dbg(xhci, "set port remote wake mask, actual port %d-%d status = 0x%x\n",
  1325. hcd->self.busnum, portnum1, temp);
  1326. break;
  1327. case USB_PORT_FEAT_BH_PORT_RESET:
  1328. temp |= PORT_WR;
  1329. writel(temp, port->addr);
  1330. temp = readl(port->addr);
  1331. break;
  1332. case USB_PORT_FEAT_U1_TIMEOUT:
  1333. if (hcd->speed < HCD_USB3)
  1334. goto error;
  1335. temp = readl(port->addr + PORTPMSC);
  1336. temp &= ~PORT_U1_TIMEOUT_MASK;
  1337. temp |= PORT_U1_TIMEOUT(timeout);
  1338. writel(temp, port->addr + PORTPMSC);
  1339. break;
  1340. case USB_PORT_FEAT_U2_TIMEOUT:
  1341. if (hcd->speed < HCD_USB3)
  1342. goto error;
  1343. temp = readl(port->addr + PORTPMSC);
  1344. temp &= ~PORT_U2_TIMEOUT_MASK;
  1345. temp |= PORT_U2_TIMEOUT(timeout);
  1346. writel(temp, port->addr + PORTPMSC);
  1347. break;
  1348. case USB_PORT_FEAT_TEST:
  1349. /* 4.19.6 Port Test Modes (USB2 Test Mode) */
  1350. if (hcd->speed != HCD_USB2)
  1351. goto error;
  1352. if (test_mode > USB_TEST_FORCE_ENABLE ||
  1353. test_mode < USB_TEST_J)
  1354. goto error;
  1355. retval = xhci_enter_test_mode(xhci, test_mode, wIndex,
  1356. &flags);
  1357. break;
  1358. default:
  1359. goto error;
  1360. }
  1361. /* unblock any posted writes */
  1362. temp = readl(port->addr);
  1363. break;
  1364. case ClearPortFeature:
  1365. if (!portnum1 || portnum1 > max_ports)
  1366. goto error;
  1367. port = ports[portnum1 - 1];
  1368. wIndex--;
  1369. temp = readl(port->addr);
  1370. if (temp == ~(u32)0) {
  1371. xhci_hc_died(xhci);
  1372. retval = -ENODEV;
  1373. break;
  1374. }
  1375. /* FIXME: What new port features do we need to support? */
  1376. temp = xhci_port_state_to_neutral(temp);
  1377. switch (wValue) {
  1378. case USB_PORT_FEAT_SUSPEND:
  1379. temp = readl(port->addr);
  1380. xhci_dbg(xhci, "clear USB_PORT_FEAT_SUSPEND\n");
  1381. xhci_dbg(xhci, "PORTSC %04x\n", temp);
  1382. if (temp & PORT_RESET)
  1383. goto error;
  1384. if ((temp & PORT_PLS_MASK) == XDEV_U3) {
  1385. if ((temp & PORT_PE) == 0)
  1386. goto error;
  1387. set_bit(wIndex, &bus_state->resuming_ports);
  1388. usb_hcd_start_port_resume(&hcd->self, wIndex);
  1389. xhci_set_link_state(xhci, port, XDEV_RESUME);
  1390. spin_unlock_irqrestore(&xhci->lock, flags);
  1391. msleep(USB_RESUME_TIMEOUT);
  1392. spin_lock_irqsave(&xhci->lock, flags);
  1393. xhci_set_link_state(xhci, port, XDEV_U0);
  1394. clear_bit(wIndex, &bus_state->resuming_ports);
  1395. usb_hcd_end_port_resume(&hcd->self, wIndex);
  1396. }
  1397. bus_state->port_c_suspend |= 1 << wIndex;
  1398. if (!port->slot_id) {
  1399. xhci_dbg(xhci, "slot_id is zero\n");
  1400. goto error;
  1401. }
  1402. xhci_ring_device(xhci, port->slot_id);
  1403. break;
  1404. case USB_PORT_FEAT_C_SUSPEND:
  1405. bus_state->port_c_suspend &= ~(1 << wIndex);
  1406. fallthrough;
  1407. case USB_PORT_FEAT_C_RESET:
  1408. case USB_PORT_FEAT_C_BH_PORT_RESET:
  1409. case USB_PORT_FEAT_C_CONNECTION:
  1410. case USB_PORT_FEAT_C_OVER_CURRENT:
  1411. case USB_PORT_FEAT_C_ENABLE:
  1412. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  1413. case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
  1414. xhci_clear_port_change_bit(xhci, wValue, wIndex,
  1415. port->addr, temp);
  1416. break;
  1417. case USB_PORT_FEAT_ENABLE:
  1418. xhci_disable_port(xhci, port);
  1419. break;
  1420. case USB_PORT_FEAT_POWER:
  1421. xhci_set_port_power(xhci, port, false, &flags);
  1422. break;
  1423. case USB_PORT_FEAT_TEST:
  1424. retval = xhci_exit_test_mode(xhci);
  1425. break;
  1426. default:
  1427. goto error;
  1428. }
  1429. break;
  1430. default:
  1431. error:
  1432. /* "stall" on error */
  1433. retval = -EPIPE;
  1434. }
  1435. spin_unlock_irqrestore(&xhci->lock, flags);
  1436. return retval;
  1437. }
  1438. EXPORT_SYMBOL_GPL(xhci_hub_control);
  1439. /*
  1440. * Returns 0 if the status hasn't changed, or the number of bytes in buf.
  1441. * Ports are 0-indexed from the HCD point of view,
  1442. * and 1-indexed from the USB core pointer of view.
  1443. *
  1444. * Note that the status change bits will be cleared as soon as a port status
  1445. * change event is generated, so we use the saved status from that event.
  1446. */
  1447. int xhci_hub_status_data(struct usb_hcd *hcd, char *buf)
  1448. {
  1449. unsigned long flags;
  1450. u32 temp, status;
  1451. u32 mask;
  1452. int i, retval;
  1453. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1454. int max_ports;
  1455. struct xhci_bus_state *bus_state;
  1456. bool reset_change = false;
  1457. struct xhci_hub *rhub;
  1458. struct xhci_port **ports;
  1459. rhub = xhci_get_rhub(hcd);
  1460. ports = rhub->ports;
  1461. max_ports = rhub->num_ports;
  1462. bus_state = &rhub->bus_state;
  1463. /* Initial status is no changes */
  1464. retval = (max_ports + 8) / 8;
  1465. memset(buf, 0, retval);
  1466. /*
  1467. * Inform the usbcore about resume-in-progress by returning
  1468. * a non-zero value even if there are no status changes.
  1469. */
  1470. spin_lock_irqsave(&xhci->lock, flags);
  1471. status = bus_state->resuming_ports;
  1472. /*
  1473. * SS devices are only visible to roothub after link training completes.
  1474. * Keep polling roothubs for a grace period after xHC start
  1475. */
  1476. if (xhci->run_graceperiod) {
  1477. if (time_before(jiffies, xhci->run_graceperiod))
  1478. status = 1;
  1479. else
  1480. xhci->run_graceperiod = 0;
  1481. }
  1482. mask = PORT_CSC | PORT_PEC | PORT_OCC | PORT_PLC | PORT_WRC | PORT_CEC;
  1483. /* For each port, did anything change? If so, set that bit in buf. */
  1484. for (i = 0; i < max_ports; i++) {
  1485. temp = readl(ports[i]->addr);
  1486. if (temp == ~(u32)0) {
  1487. xhci_hc_died(xhci);
  1488. retval = -ENODEV;
  1489. break;
  1490. }
  1491. trace_xhci_hub_status_data(ports[i], temp);
  1492. if ((temp & mask) != 0 ||
  1493. (bus_state->port_c_suspend & 1 << i) ||
  1494. (ports[i]->resume_timestamp && time_after_eq(
  1495. jiffies, ports[i]->resume_timestamp))) {
  1496. buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
  1497. status = 1;
  1498. }
  1499. if ((temp & PORT_RC))
  1500. reset_change = true;
  1501. if (temp & PORT_OC)
  1502. status = 1;
  1503. }
  1504. if (!status && !reset_change) {
  1505. xhci_dbg(xhci, "%s: stopping usb%d port polling\n",
  1506. __func__, hcd->self.busnum);
  1507. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  1508. }
  1509. spin_unlock_irqrestore(&xhci->lock, flags);
  1510. return status ? retval : 0;
  1511. }
  1512. #ifdef CONFIG_PM
  1513. int xhci_bus_suspend(struct usb_hcd *hcd)
  1514. {
  1515. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1516. int max_ports, port_index;
  1517. struct xhci_bus_state *bus_state;
  1518. unsigned long flags;
  1519. struct xhci_hub *rhub;
  1520. struct xhci_port **ports;
  1521. u32 portsc_buf[USB_MAXCHILDREN];
  1522. bool wake_enabled;
  1523. rhub = xhci_get_rhub(hcd);
  1524. ports = rhub->ports;
  1525. max_ports = rhub->num_ports;
  1526. bus_state = &rhub->bus_state;
  1527. wake_enabled = hcd->self.root_hub->do_remote_wakeup;
  1528. spin_lock_irqsave(&xhci->lock, flags);
  1529. if (wake_enabled) {
  1530. if (bus_state->resuming_ports || /* USB2 */
  1531. bus_state->port_remote_wakeup) { /* USB3 */
  1532. spin_unlock_irqrestore(&xhci->lock, flags);
  1533. xhci_dbg(xhci, "usb%d bus suspend to fail because a port is resuming\n",
  1534. hcd->self.busnum);
  1535. return -EBUSY;
  1536. }
  1537. }
  1538. /*
  1539. * Prepare ports for suspend, but don't write anything before all ports
  1540. * are checked and we know bus suspend can proceed
  1541. */
  1542. bus_state->bus_suspended = 0;
  1543. port_index = max_ports;
  1544. while (port_index--) {
  1545. u32 t1, t2;
  1546. int retries = 10;
  1547. retry:
  1548. t1 = readl(ports[port_index]->addr);
  1549. t2 = xhci_port_state_to_neutral(t1);
  1550. portsc_buf[port_index] = 0;
  1551. /*
  1552. * Give a USB3 port in link training time to finish, but don't
  1553. * prevent suspend as port might be stuck
  1554. */
  1555. if ((hcd->speed >= HCD_USB3) && retries-- &&
  1556. (t1 & PORT_PLS_MASK) == XDEV_POLLING) {
  1557. spin_unlock_irqrestore(&xhci->lock, flags);
  1558. msleep(XHCI_PORT_POLLING_LFPS_TIME);
  1559. spin_lock_irqsave(&xhci->lock, flags);
  1560. xhci_dbg(xhci, "port %d-%d polling in bus suspend, waiting\n",
  1561. hcd->self.busnum, port_index + 1);
  1562. goto retry;
  1563. }
  1564. /* bail out if port detected a over-current condition */
  1565. if (t1 & PORT_OC) {
  1566. bus_state->bus_suspended = 0;
  1567. spin_unlock_irqrestore(&xhci->lock, flags);
  1568. xhci_dbg(xhci, "Bus suspend bailout, port over-current detected\n");
  1569. return -EBUSY;
  1570. }
  1571. /* suspend ports in U0, or bail out for new connect changes */
  1572. if ((t1 & PORT_PE) && (t1 & PORT_PLS_MASK) == XDEV_U0) {
  1573. if ((t1 & PORT_CSC) && wake_enabled) {
  1574. bus_state->bus_suspended = 0;
  1575. spin_unlock_irqrestore(&xhci->lock, flags);
  1576. xhci_dbg(xhci, "Bus suspend bailout, port connect change\n");
  1577. return -EBUSY;
  1578. }
  1579. xhci_dbg(xhci, "port %d-%d not suspended\n",
  1580. hcd->self.busnum, port_index + 1);
  1581. t2 &= ~PORT_PLS_MASK;
  1582. t2 |= PORT_LINK_STROBE | XDEV_U3;
  1583. set_bit(port_index, &bus_state->bus_suspended);
  1584. }
  1585. /* USB core sets remote wake mask for USB 3.0 hubs,
  1586. * including the USB 3.0 roothub, but only if CONFIG_PM
  1587. * is enabled, so also enable remote wake here.
  1588. */
  1589. if (wake_enabled) {
  1590. if (t1 & PORT_CONNECT) {
  1591. t2 |= PORT_WKOC_E | PORT_WKDISC_E;
  1592. t2 &= ~PORT_WKCONN_E;
  1593. } else {
  1594. t2 |= PORT_WKOC_E | PORT_WKCONN_E;
  1595. t2 &= ~PORT_WKDISC_E;
  1596. }
  1597. if ((xhci->quirks & XHCI_U2_DISABLE_WAKE) &&
  1598. (hcd->speed < HCD_USB3)) {
  1599. if (usb_amd_pt_check_port(hcd->self.controller,
  1600. port_index))
  1601. t2 &= ~PORT_WAKE_BITS;
  1602. }
  1603. } else
  1604. t2 &= ~PORT_WAKE_BITS;
  1605. t1 = xhci_port_state_to_neutral(t1);
  1606. if (t1 != t2)
  1607. portsc_buf[port_index] = t2;
  1608. }
  1609. /* write port settings, stopping and suspending ports if needed */
  1610. port_index = max_ports;
  1611. while (port_index--) {
  1612. if (!portsc_buf[port_index])
  1613. continue;
  1614. if (test_bit(port_index, &bus_state->bus_suspended)) {
  1615. int slot_id = ports[port_index]->slot_id;
  1616. if (slot_id) {
  1617. spin_unlock_irqrestore(&xhci->lock, flags);
  1618. xhci_stop_device(xhci, slot_id, 1);
  1619. spin_lock_irqsave(&xhci->lock, flags);
  1620. }
  1621. }
  1622. writel(portsc_buf[port_index], ports[port_index]->addr);
  1623. }
  1624. hcd->state = HC_STATE_SUSPENDED;
  1625. bus_state->next_statechange = jiffies + msecs_to_jiffies(10);
  1626. spin_unlock_irqrestore(&xhci->lock, flags);
  1627. if (bus_state->bus_suspended)
  1628. usleep_range(5000, 10000);
  1629. return 0;
  1630. }
  1631. /*
  1632. * Workaround for missing Cold Attach Status (CAS) if device re-plugged in S3.
  1633. * warm reset a USB3 device stuck in polling or compliance mode after resume.
  1634. * See Intel 100/c230 series PCH specification update Doc #332692-006 Errata #8
  1635. */
  1636. static bool xhci_port_missing_cas_quirk(struct xhci_port *port)
  1637. {
  1638. u32 portsc;
  1639. portsc = readl(port->addr);
  1640. /* if any of these are set we are not stuck */
  1641. if (portsc & (PORT_CONNECT | PORT_CAS))
  1642. return false;
  1643. if (((portsc & PORT_PLS_MASK) != XDEV_POLLING) &&
  1644. ((portsc & PORT_PLS_MASK) != XDEV_COMP_MODE))
  1645. return false;
  1646. /* clear wakeup/change bits, and do a warm port reset */
  1647. portsc &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
  1648. portsc |= PORT_WR;
  1649. writel(portsc, port->addr);
  1650. /* flush write */
  1651. readl(port->addr);
  1652. return true;
  1653. }
  1654. int xhci_bus_resume(struct usb_hcd *hcd)
  1655. {
  1656. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1657. struct xhci_bus_state *bus_state;
  1658. unsigned long flags;
  1659. int max_ports, port_index;
  1660. int sret;
  1661. u32 next_state;
  1662. u32 temp, portsc;
  1663. struct xhci_hub *rhub;
  1664. struct xhci_port **ports;
  1665. rhub = xhci_get_rhub(hcd);
  1666. ports = rhub->ports;
  1667. max_ports = rhub->num_ports;
  1668. bus_state = &rhub->bus_state;
  1669. if (time_before(jiffies, bus_state->next_statechange))
  1670. msleep(5);
  1671. spin_lock_irqsave(&xhci->lock, flags);
  1672. if (!HCD_HW_ACCESSIBLE(hcd)) {
  1673. spin_unlock_irqrestore(&xhci->lock, flags);
  1674. return -ESHUTDOWN;
  1675. }
  1676. /* delay the irqs */
  1677. temp = readl(&xhci->op_regs->command);
  1678. temp &= ~CMD_EIE;
  1679. writel(temp, &xhci->op_regs->command);
  1680. /* bus specific resume for ports we suspended at bus_suspend */
  1681. if (hcd->speed >= HCD_USB3)
  1682. next_state = XDEV_U0;
  1683. else
  1684. next_state = XDEV_RESUME;
  1685. port_index = max_ports;
  1686. while (port_index--) {
  1687. portsc = readl(ports[port_index]->addr);
  1688. /* warm reset CAS limited ports stuck in polling/compliance */
  1689. if ((xhci->quirks & XHCI_MISSING_CAS) &&
  1690. (hcd->speed >= HCD_USB3) &&
  1691. xhci_port_missing_cas_quirk(ports[port_index])) {
  1692. xhci_dbg(xhci, "reset stuck port %d-%d\n",
  1693. hcd->self.busnum, port_index + 1);
  1694. clear_bit(port_index, &bus_state->bus_suspended);
  1695. continue;
  1696. }
  1697. /* resume if we suspended the link, and it is still suspended */
  1698. if (test_bit(port_index, &bus_state->bus_suspended))
  1699. switch (portsc & PORT_PLS_MASK) {
  1700. case XDEV_U3:
  1701. portsc = xhci_port_state_to_neutral(portsc);
  1702. portsc &= ~PORT_PLS_MASK;
  1703. portsc |= PORT_LINK_STROBE | next_state;
  1704. break;
  1705. case XDEV_RESUME:
  1706. /* resume already initiated */
  1707. break;
  1708. default:
  1709. /* not in a resumeable state, ignore it */
  1710. clear_bit(port_index,
  1711. &bus_state->bus_suspended);
  1712. break;
  1713. }
  1714. /* disable wake for all ports, write new link state if needed */
  1715. portsc &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
  1716. writel(portsc, ports[port_index]->addr);
  1717. }
  1718. /* USB2 specific resume signaling delay and U0 link state transition */
  1719. if (hcd->speed < HCD_USB3) {
  1720. if (bus_state->bus_suspended) {
  1721. spin_unlock_irqrestore(&xhci->lock, flags);
  1722. msleep(USB_RESUME_TIMEOUT);
  1723. spin_lock_irqsave(&xhci->lock, flags);
  1724. }
  1725. for_each_set_bit(port_index, &bus_state->bus_suspended,
  1726. BITS_PER_LONG) {
  1727. /* Clear PLC to poll it later for U0 transition */
  1728. xhci_test_and_clear_bit(xhci, ports[port_index],
  1729. PORT_PLC);
  1730. xhci_set_link_state(xhci, ports[port_index], XDEV_U0);
  1731. }
  1732. }
  1733. /* poll for U0 link state complete, both USB2 and USB3 */
  1734. for_each_set_bit(port_index, &bus_state->bus_suspended, BITS_PER_LONG) {
  1735. sret = xhci_handshake(ports[port_index]->addr, PORT_PLC,
  1736. PORT_PLC, 10 * 1000);
  1737. if (sret) {
  1738. xhci_warn(xhci, "port %d-%d resume PLC timeout\n",
  1739. hcd->self.busnum, port_index + 1);
  1740. continue;
  1741. }
  1742. xhci_test_and_clear_bit(xhci, ports[port_index], PORT_PLC);
  1743. if (ports[port_index]->slot_id)
  1744. xhci_ring_device(xhci, ports[port_index]->slot_id);
  1745. }
  1746. (void) readl(&xhci->op_regs->command);
  1747. bus_state->next_statechange = jiffies + msecs_to_jiffies(5);
  1748. /* re-enable irqs */
  1749. temp = readl(&xhci->op_regs->command);
  1750. temp |= CMD_EIE;
  1751. writel(temp, &xhci->op_regs->command);
  1752. temp = readl(&xhci->op_regs->command);
  1753. spin_unlock_irqrestore(&xhci->lock, flags);
  1754. return 0;
  1755. }
  1756. unsigned long xhci_get_resuming_ports(struct usb_hcd *hcd)
  1757. {
  1758. struct xhci_hub *rhub = xhci_get_rhub(hcd);
  1759. /* USB3 port wakeups are reported via usb_wakeup_notification() */
  1760. return rhub->bus_state.resuming_ports; /* USB2 ports only */
  1761. }
  1762. #endif /* CONFIG_PM */