xhci-hub.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002
  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 <asm/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. * Given a port state, this function returns a value that would result in the
  376. * port being in the same state, if the value was written to the port status
  377. * control register.
  378. * Save Read Only (RO) bits and save read/write bits where
  379. * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
  380. * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
  381. */
  382. u32 xhci_port_state_to_neutral(u32 state)
  383. {
  384. /* Save read-only status and port state */
  385. return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
  386. }
  387. /*
  388. * find slot id based on port number.
  389. * @port: The one-based port number from one of the two split roothubs.
  390. */
  391. int xhci_find_slot_id_by_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  392. u16 port)
  393. {
  394. int slot_id;
  395. int i;
  396. enum usb_device_speed speed;
  397. slot_id = 0;
  398. for (i = 0; i < MAX_HC_SLOTS; i++) {
  399. if (!xhci->devs[i] || !xhci->devs[i]->udev)
  400. continue;
  401. speed = xhci->devs[i]->udev->speed;
  402. if (((speed >= USB_SPEED_SUPER) == (hcd->speed >= HCD_USB3))
  403. && xhci->devs[i]->fake_port == port) {
  404. slot_id = i;
  405. break;
  406. }
  407. }
  408. return slot_id;
  409. }
  410. /*
  411. * Stop device
  412. * It issues stop endpoint command for EP 0 to 30. And wait the last command
  413. * to complete.
  414. * suspend will set to 1, if suspend bit need to set in command.
  415. */
  416. static int xhci_stop_device(struct xhci_hcd *xhci, int slot_id, int suspend)
  417. {
  418. struct xhci_virt_device *virt_dev;
  419. struct xhci_command *cmd;
  420. unsigned long flags;
  421. int ret;
  422. int i;
  423. ret = 0;
  424. virt_dev = xhci->devs[slot_id];
  425. if (!virt_dev)
  426. return -ENODEV;
  427. trace_xhci_stop_device(virt_dev);
  428. cmd = xhci_alloc_command(xhci, true, GFP_NOIO);
  429. if (!cmd)
  430. return -ENOMEM;
  431. spin_lock_irqsave(&xhci->lock, flags);
  432. for (i = LAST_EP_INDEX; i > 0; i--) {
  433. if (virt_dev->eps[i].ring && virt_dev->eps[i].ring->dequeue) {
  434. struct xhci_ep_ctx *ep_ctx;
  435. struct xhci_command *command;
  436. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->out_ctx, i);
  437. /* Check ep is running, required by AMD SNPS 3.1 xHC */
  438. if (GET_EP_CTX_STATE(ep_ctx) != EP_STATE_RUNNING)
  439. continue;
  440. command = xhci_alloc_command(xhci, false, GFP_NOWAIT);
  441. if (!command) {
  442. spin_unlock_irqrestore(&xhci->lock, flags);
  443. ret = -ENOMEM;
  444. goto cmd_cleanup;
  445. }
  446. ret = xhci_queue_stop_endpoint(xhci, command, slot_id,
  447. i, suspend);
  448. if (ret) {
  449. spin_unlock_irqrestore(&xhci->lock, flags);
  450. xhci_free_command(xhci, command);
  451. goto cmd_cleanup;
  452. }
  453. }
  454. }
  455. ret = xhci_queue_stop_endpoint(xhci, cmd, slot_id, 0, suspend);
  456. if (ret) {
  457. spin_unlock_irqrestore(&xhci->lock, flags);
  458. goto cmd_cleanup;
  459. }
  460. xhci_ring_cmd_db(xhci);
  461. spin_unlock_irqrestore(&xhci->lock, flags);
  462. /* Wait for last stop endpoint command to finish */
  463. wait_for_completion(cmd->completion);
  464. if (cmd->status == COMP_COMMAND_ABORTED ||
  465. cmd->status == COMP_COMMAND_RING_STOPPED) {
  466. xhci_warn(xhci, "Timeout while waiting for stop endpoint command\n");
  467. ret = -ETIME;
  468. goto cmd_cleanup;
  469. }
  470. ret = xhci_vendor_sync_dev_ctx(xhci, slot_id);
  471. if (ret)
  472. xhci_warn(xhci, "Sync device context failed, ret=%d\n", ret);
  473. cmd_cleanup:
  474. xhci_free_command(xhci, cmd);
  475. return ret;
  476. }
  477. /*
  478. * Ring device, it rings the all doorbells unconditionally.
  479. */
  480. void xhci_ring_device(struct xhci_hcd *xhci, int slot_id)
  481. {
  482. int i, s;
  483. struct xhci_virt_ep *ep;
  484. for (i = 0; i < LAST_EP_INDEX + 1; i++) {
  485. ep = &xhci->devs[slot_id]->eps[i];
  486. if (ep->ep_state & EP_HAS_STREAMS) {
  487. for (s = 1; s < ep->stream_info->num_streams; s++)
  488. xhci_ring_ep_doorbell(xhci, slot_id, i, s);
  489. } else if (ep->ring && ep->ring->dequeue) {
  490. xhci_ring_ep_doorbell(xhci, slot_id, i, 0);
  491. }
  492. }
  493. return;
  494. }
  495. static void xhci_disable_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  496. u16 wIndex, __le32 __iomem *addr, u32 port_status)
  497. {
  498. /* Don't allow the USB core to disable SuperSpeed ports. */
  499. if (hcd->speed >= HCD_USB3) {
  500. xhci_dbg(xhci, "Ignoring request to disable "
  501. "SuperSpeed port.\n");
  502. return;
  503. }
  504. if (xhci->quirks & XHCI_BROKEN_PORT_PED) {
  505. xhci_dbg(xhci,
  506. "Broken Port Enabled/Disabled, ignoring port disable request.\n");
  507. return;
  508. }
  509. /* Write 1 to disable the port */
  510. writel(port_status | PORT_PE, addr);
  511. port_status = readl(addr);
  512. xhci_dbg(xhci, "disable port %d-%d, portsc: 0x%x\n",
  513. hcd->self.busnum, wIndex + 1, port_status);
  514. }
  515. static void xhci_clear_port_change_bit(struct xhci_hcd *xhci, u16 wValue,
  516. u16 wIndex, __le32 __iomem *addr, u32 port_status)
  517. {
  518. char *port_change_bit;
  519. u32 status;
  520. switch (wValue) {
  521. case USB_PORT_FEAT_C_RESET:
  522. status = PORT_RC;
  523. port_change_bit = "reset";
  524. break;
  525. case USB_PORT_FEAT_C_BH_PORT_RESET:
  526. status = PORT_WRC;
  527. port_change_bit = "warm(BH) reset";
  528. break;
  529. case USB_PORT_FEAT_C_CONNECTION:
  530. status = PORT_CSC;
  531. port_change_bit = "connect";
  532. break;
  533. case USB_PORT_FEAT_C_OVER_CURRENT:
  534. status = PORT_OCC;
  535. port_change_bit = "over-current";
  536. break;
  537. case USB_PORT_FEAT_C_ENABLE:
  538. status = PORT_PEC;
  539. port_change_bit = "enable/disable";
  540. break;
  541. case USB_PORT_FEAT_C_SUSPEND:
  542. status = PORT_PLC;
  543. port_change_bit = "suspend/resume";
  544. break;
  545. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  546. status = PORT_PLC;
  547. port_change_bit = "link state";
  548. break;
  549. case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
  550. status = PORT_CEC;
  551. port_change_bit = "config error";
  552. break;
  553. default:
  554. /* Should never happen */
  555. return;
  556. }
  557. /* Change bits are all write 1 to clear */
  558. writel(port_status | status, addr);
  559. port_status = readl(addr);
  560. xhci_dbg(xhci, "clear port%d %s change, portsc: 0x%x\n",
  561. wIndex + 1, port_change_bit, port_status);
  562. }
  563. struct xhci_hub *xhci_get_rhub(struct usb_hcd *hcd)
  564. {
  565. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  566. if (hcd->speed >= HCD_USB3)
  567. return &xhci->usb3_rhub;
  568. return &xhci->usb2_rhub;
  569. }
  570. /*
  571. * xhci_set_port_power() must be called with xhci->lock held.
  572. * It will release and re-aquire the lock while calling ACPI
  573. * method.
  574. */
  575. static void xhci_set_port_power(struct xhci_hcd *xhci, struct usb_hcd *hcd,
  576. u16 index, bool on, unsigned long *flags)
  577. __must_hold(&xhci->lock)
  578. {
  579. struct xhci_hub *rhub;
  580. struct xhci_port *port;
  581. u32 temp;
  582. rhub = xhci_get_rhub(hcd);
  583. port = rhub->ports[index];
  584. temp = readl(port->addr);
  585. xhci_dbg(xhci, "set port power %d-%d %s, portsc: 0x%x\n",
  586. hcd->self.busnum, index + 1, on ? "ON" : "OFF", temp);
  587. temp = xhci_port_state_to_neutral(temp);
  588. if (on) {
  589. /* Power on */
  590. writel(temp | PORT_POWER, port->addr);
  591. readl(port->addr);
  592. } else {
  593. /* Power off */
  594. writel(temp & ~PORT_POWER, port->addr);
  595. }
  596. spin_unlock_irqrestore(&xhci->lock, *flags);
  597. temp = usb_acpi_power_manageable(hcd->self.root_hub,
  598. index);
  599. if (temp)
  600. usb_acpi_set_power_state(hcd->self.root_hub,
  601. index, on);
  602. spin_lock_irqsave(&xhci->lock, *flags);
  603. }
  604. static void xhci_port_set_test_mode(struct xhci_hcd *xhci,
  605. u16 test_mode, u16 wIndex)
  606. {
  607. u32 temp;
  608. struct xhci_port *port;
  609. /* xhci only supports test mode for usb2 ports */
  610. port = xhci->usb2_rhub.ports[wIndex];
  611. temp = readl(port->addr + PORTPMSC);
  612. temp |= test_mode << PORT_TEST_MODE_SHIFT;
  613. writel(temp, port->addr + PORTPMSC);
  614. xhci->test_mode = test_mode;
  615. if (test_mode == USB_TEST_FORCE_ENABLE)
  616. xhci_start(xhci);
  617. }
  618. static int xhci_enter_test_mode(struct xhci_hcd *xhci,
  619. u16 test_mode, u16 wIndex, unsigned long *flags)
  620. __must_hold(&xhci->lock)
  621. {
  622. struct usb_hcd *usb3_hcd = xhci_get_usb3_hcd(xhci);
  623. int i, retval;
  624. /* Disable all Device Slots */
  625. xhci_dbg(xhci, "Disable all slots\n");
  626. spin_unlock_irqrestore(&xhci->lock, *flags);
  627. for (i = 1; i <= HCS_MAX_SLOTS(xhci->hcs_params1); i++) {
  628. if (!xhci->devs[i])
  629. continue;
  630. retval = xhci_disable_slot(xhci, i);
  631. xhci_free_virt_device(xhci, i);
  632. if (retval)
  633. xhci_err(xhci, "Failed to disable slot %d, %d. Enter test mode anyway\n",
  634. i, retval);
  635. }
  636. spin_lock_irqsave(&xhci->lock, *flags);
  637. /* Put all ports to the Disable state by clear PP */
  638. xhci_dbg(xhci, "Disable all port (PP = 0)\n");
  639. /* Power off USB3 ports*/
  640. for (i = 0; i < xhci->usb3_rhub.num_ports; i++)
  641. xhci_set_port_power(xhci, usb3_hcd, i, false, flags);
  642. /* Power off USB2 ports*/
  643. for (i = 0; i < xhci->usb2_rhub.num_ports; i++)
  644. xhci_set_port_power(xhci, xhci->main_hcd, i, false, flags);
  645. /* Stop the controller */
  646. xhci_dbg(xhci, "Stop controller\n");
  647. retval = xhci_halt(xhci);
  648. if (retval)
  649. return retval;
  650. /* Disable runtime PM for test mode */
  651. pm_runtime_forbid(xhci_to_hcd(xhci)->self.controller);
  652. /* Set PORTPMSC.PTC field to enter selected test mode */
  653. /* Port is selected by wIndex. port_id = wIndex + 1 */
  654. xhci_dbg(xhci, "Enter Test Mode: %d, Port_id=%d\n",
  655. test_mode, wIndex + 1);
  656. xhci_port_set_test_mode(xhci, test_mode, wIndex);
  657. return retval;
  658. }
  659. static int xhci_exit_test_mode(struct xhci_hcd *xhci)
  660. {
  661. int retval;
  662. if (!xhci->test_mode) {
  663. xhci_err(xhci, "Not in test mode, do nothing.\n");
  664. return 0;
  665. }
  666. if (xhci->test_mode == USB_TEST_FORCE_ENABLE &&
  667. !(xhci->xhc_state & XHCI_STATE_HALTED)) {
  668. retval = xhci_halt(xhci);
  669. if (retval)
  670. return retval;
  671. }
  672. pm_runtime_allow(xhci_to_hcd(xhci)->self.controller);
  673. xhci->test_mode = 0;
  674. return xhci_reset(xhci, XHCI_RESET_SHORT_USEC);
  675. }
  676. void xhci_set_link_state(struct xhci_hcd *xhci, struct xhci_port *port,
  677. u32 link_state)
  678. {
  679. u32 temp;
  680. u32 portsc;
  681. portsc = readl(port->addr);
  682. temp = xhci_port_state_to_neutral(portsc);
  683. temp &= ~PORT_PLS_MASK;
  684. temp |= PORT_LINK_STROBE | link_state;
  685. writel(temp, port->addr);
  686. xhci_dbg(xhci, "Set port %d-%d link state, portsc: 0x%x, write 0x%x",
  687. port->rhub->hcd->self.busnum, port->hcd_portnum + 1,
  688. portsc, temp);
  689. }
  690. static void xhci_set_remote_wake_mask(struct xhci_hcd *xhci,
  691. struct xhci_port *port, u16 wake_mask)
  692. {
  693. u32 temp;
  694. temp = readl(port->addr);
  695. temp = xhci_port_state_to_neutral(temp);
  696. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_CONNECT)
  697. temp |= PORT_WKCONN_E;
  698. else
  699. temp &= ~PORT_WKCONN_E;
  700. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT)
  701. temp |= PORT_WKDISC_E;
  702. else
  703. temp &= ~PORT_WKDISC_E;
  704. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT)
  705. temp |= PORT_WKOC_E;
  706. else
  707. temp &= ~PORT_WKOC_E;
  708. writel(temp, port->addr);
  709. }
  710. /* Test and clear port RWC bit */
  711. void xhci_test_and_clear_bit(struct xhci_hcd *xhci, struct xhci_port *port,
  712. u32 port_bit)
  713. {
  714. u32 temp;
  715. temp = readl(port->addr);
  716. if (temp & port_bit) {
  717. temp = xhci_port_state_to_neutral(temp);
  718. temp |= port_bit;
  719. writel(temp, port->addr);
  720. }
  721. }
  722. /* Updates Link Status for super Speed port */
  723. static void xhci_hub_report_usb3_link_state(struct xhci_hcd *xhci,
  724. u32 *status, u32 status_reg)
  725. {
  726. u32 pls = status_reg & PORT_PLS_MASK;
  727. /* When the CAS bit is set then warm reset
  728. * should be performed on port
  729. */
  730. if (status_reg & PORT_CAS) {
  731. /* The CAS bit can be set while the port is
  732. * in any link state.
  733. * Only roothubs have CAS bit, so we
  734. * pretend to be in compliance mode
  735. * unless we're already in compliance
  736. * or the inactive state.
  737. */
  738. if (pls != USB_SS_PORT_LS_COMP_MOD &&
  739. pls != USB_SS_PORT_LS_SS_INACTIVE) {
  740. pls = USB_SS_PORT_LS_COMP_MOD;
  741. }
  742. /* Return also connection bit -
  743. * hub state machine resets port
  744. * when this bit is set.
  745. */
  746. pls |= USB_PORT_STAT_CONNECTION;
  747. } else {
  748. /*
  749. * Resume state is an xHCI internal state. Do not report it to
  750. * usb core, instead, pretend to be U3, thus usb core knows
  751. * it's not ready for transfer.
  752. */
  753. if (pls == XDEV_RESUME) {
  754. *status |= USB_SS_PORT_LS_U3;
  755. return;
  756. }
  757. /*
  758. * If CAS bit isn't set but the Port is already at
  759. * Compliance Mode, fake a connection so the USB core
  760. * notices the Compliance state and resets the port.
  761. * This resolves an issue generated by the SN65LVPE502CP
  762. * in which sometimes the port enters compliance mode
  763. * caused by a delay on the host-device negotiation.
  764. */
  765. if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
  766. (pls == USB_SS_PORT_LS_COMP_MOD))
  767. pls |= USB_PORT_STAT_CONNECTION;
  768. }
  769. /* update status field */
  770. *status |= pls;
  771. }
  772. /*
  773. * Function for Compliance Mode Quirk.
  774. *
  775. * This Function verifies if all xhc USB3 ports have entered U0, if so,
  776. * the compliance mode timer is deleted. A port won't enter
  777. * compliance mode if it has previously entered U0.
  778. */
  779. static void xhci_del_comp_mod_timer(struct xhci_hcd *xhci, u32 status,
  780. u16 wIndex)
  781. {
  782. u32 all_ports_seen_u0 = ((1 << xhci->usb3_rhub.num_ports) - 1);
  783. bool port_in_u0 = ((status & PORT_PLS_MASK) == XDEV_U0);
  784. if (!(xhci->quirks & XHCI_COMP_MODE_QUIRK))
  785. return;
  786. if ((xhci->port_status_u0 != all_ports_seen_u0) && port_in_u0) {
  787. xhci->port_status_u0 |= 1 << wIndex;
  788. if (xhci->port_status_u0 == all_ports_seen_u0) {
  789. del_timer_sync(&xhci->comp_mode_recovery_timer);
  790. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  791. "All USB3 ports have entered U0 already!");
  792. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  793. "Compliance Mode Recovery Timer Deleted.");
  794. }
  795. }
  796. }
  797. static int xhci_handle_usb2_port_link_resume(struct xhci_port *port,
  798. u32 *status, u32 portsc,
  799. unsigned long *flags)
  800. {
  801. struct xhci_bus_state *bus_state;
  802. struct xhci_hcd *xhci;
  803. struct usb_hcd *hcd;
  804. int slot_id;
  805. u32 wIndex;
  806. hcd = port->rhub->hcd;
  807. bus_state = &port->rhub->bus_state;
  808. xhci = hcd_to_xhci(hcd);
  809. wIndex = port->hcd_portnum;
  810. if ((portsc & PORT_RESET) || !(portsc & PORT_PE)) {
  811. *status = 0xffffffff;
  812. return -EINVAL;
  813. }
  814. /* did port event handler already start resume timing? */
  815. if (!bus_state->resume_done[wIndex]) {
  816. /* If not, maybe we are in a host initated resume? */
  817. if (test_bit(wIndex, &bus_state->resuming_ports)) {
  818. /* Host initated resume doesn't time the resume
  819. * signalling using resume_done[].
  820. * It manually sets RESUME state, sleeps 20ms
  821. * and sets U0 state. This should probably be
  822. * changed, but not right now.
  823. */
  824. } else {
  825. /* port resume was discovered now and here,
  826. * start resume timing
  827. */
  828. unsigned long timeout = jiffies +
  829. msecs_to_jiffies(USB_RESUME_TIMEOUT);
  830. set_bit(wIndex, &bus_state->resuming_ports);
  831. bus_state->resume_done[wIndex] = timeout;
  832. mod_timer(&hcd->rh_timer, timeout);
  833. usb_hcd_start_port_resume(&hcd->self, wIndex);
  834. }
  835. /* Has resume been signalled for USB_RESUME_TIME yet? */
  836. } else if (time_after_eq(jiffies, bus_state->resume_done[wIndex])) {
  837. int time_left;
  838. xhci_dbg(xhci, "resume USB2 port %d-%d\n",
  839. hcd->self.busnum, wIndex + 1);
  840. bus_state->resume_done[wIndex] = 0;
  841. clear_bit(wIndex, &bus_state->resuming_ports);
  842. set_bit(wIndex, &bus_state->rexit_ports);
  843. xhci_test_and_clear_bit(xhci, port, PORT_PLC);
  844. xhci_set_link_state(xhci, port, XDEV_U0);
  845. spin_unlock_irqrestore(&xhci->lock, *flags);
  846. time_left = wait_for_completion_timeout(
  847. &bus_state->rexit_done[wIndex],
  848. msecs_to_jiffies(XHCI_MAX_REXIT_TIMEOUT_MS));
  849. spin_lock_irqsave(&xhci->lock, *flags);
  850. if (time_left) {
  851. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  852. wIndex + 1);
  853. if (!slot_id) {
  854. xhci_dbg(xhci, "slot_id is zero\n");
  855. *status = 0xffffffff;
  856. return -ENODEV;
  857. }
  858. xhci_ring_device(xhci, slot_id);
  859. } else {
  860. int port_status = readl(port->addr);
  861. xhci_warn(xhci, "Port resume timed out, port %d-%d: 0x%x\n",
  862. hcd->self.busnum, wIndex + 1, port_status);
  863. *status |= USB_PORT_STAT_SUSPEND;
  864. clear_bit(wIndex, &bus_state->rexit_ports);
  865. }
  866. usb_hcd_end_port_resume(&hcd->self, wIndex);
  867. bus_state->port_c_suspend |= 1 << wIndex;
  868. bus_state->suspended_ports &= ~(1 << wIndex);
  869. } else {
  870. /*
  871. * The resume has been signaling for less than
  872. * USB_RESUME_TIME. Report the port status as SUSPEND,
  873. * let the usbcore check port status again and clear
  874. * resume signaling later.
  875. */
  876. *status |= USB_PORT_STAT_SUSPEND;
  877. }
  878. return 0;
  879. }
  880. static u32 xhci_get_ext_port_status(u32 raw_port_status, u32 port_li)
  881. {
  882. u32 ext_stat = 0;
  883. int speed_id;
  884. /* only support rx and tx lane counts of 1 in usb3.1 spec */
  885. speed_id = DEV_PORT_SPEED(raw_port_status);
  886. ext_stat |= speed_id; /* bits 3:0, RX speed id */
  887. ext_stat |= speed_id << 4; /* bits 7:4, TX speed id */
  888. ext_stat |= PORT_RX_LANES(port_li) << 8; /* bits 11:8 Rx lane count */
  889. ext_stat |= PORT_TX_LANES(port_li) << 12; /* bits 15:12 Tx lane count */
  890. return ext_stat;
  891. }
  892. static void xhci_get_usb3_port_status(struct xhci_port *port, u32 *status,
  893. u32 portsc)
  894. {
  895. struct xhci_bus_state *bus_state;
  896. struct xhci_hcd *xhci;
  897. struct usb_hcd *hcd;
  898. u32 link_state;
  899. u32 portnum;
  900. bus_state = &port->rhub->bus_state;
  901. xhci = hcd_to_xhci(port->rhub->hcd);
  902. hcd = port->rhub->hcd;
  903. link_state = portsc & PORT_PLS_MASK;
  904. portnum = port->hcd_portnum;
  905. /* USB3 specific wPortChange bits
  906. *
  907. * Port link change with port in resume state should not be
  908. * reported to usbcore, as this is an internal state to be
  909. * handled by xhci driver. Reporting PLC to usbcore may
  910. * cause usbcore clearing PLC first and port change event
  911. * irq won't be generated.
  912. */
  913. if (portsc & PORT_PLC && (link_state != XDEV_RESUME))
  914. *status |= USB_PORT_STAT_C_LINK_STATE << 16;
  915. if (portsc & PORT_WRC)
  916. *status |= USB_PORT_STAT_C_BH_RESET << 16;
  917. if (portsc & PORT_CEC)
  918. *status |= USB_PORT_STAT_C_CONFIG_ERROR << 16;
  919. /* USB3 specific wPortStatus bits */
  920. if (portsc & PORT_POWER)
  921. *status |= USB_SS_PORT_STAT_POWER;
  922. /* no longer suspended or resuming */
  923. if (link_state != XDEV_U3 &&
  924. link_state != XDEV_RESUME &&
  925. link_state != XDEV_RECOVERY) {
  926. /* remote wake resume signaling complete */
  927. if (bus_state->port_remote_wakeup & (1 << portnum)) {
  928. bus_state->port_remote_wakeup &= ~(1 << portnum);
  929. usb_hcd_end_port_resume(&hcd->self, portnum);
  930. }
  931. bus_state->suspended_ports &= ~(1 << portnum);
  932. }
  933. xhci_hub_report_usb3_link_state(xhci, status, portsc);
  934. xhci_del_comp_mod_timer(xhci, portsc, portnum);
  935. }
  936. static void xhci_get_usb2_port_status(struct xhci_port *port, u32 *status,
  937. u32 portsc, unsigned long *flags)
  938. {
  939. struct xhci_bus_state *bus_state;
  940. u32 link_state;
  941. u32 portnum;
  942. int ret;
  943. bus_state = &port->rhub->bus_state;
  944. link_state = portsc & PORT_PLS_MASK;
  945. portnum = port->hcd_portnum;
  946. /* USB2 wPortStatus bits */
  947. if (portsc & PORT_POWER) {
  948. *status |= USB_PORT_STAT_POWER;
  949. /* link state is only valid if port is powered */
  950. if (link_state == XDEV_U3)
  951. *status |= USB_PORT_STAT_SUSPEND;
  952. if (link_state == XDEV_U2)
  953. *status |= USB_PORT_STAT_L1;
  954. if (link_state == XDEV_U0) {
  955. if (bus_state->resume_done[portnum])
  956. usb_hcd_end_port_resume(&port->rhub->hcd->self,
  957. portnum);
  958. bus_state->resume_done[portnum] = 0;
  959. clear_bit(portnum, &bus_state->resuming_ports);
  960. if (bus_state->suspended_ports & (1 << portnum)) {
  961. bus_state->suspended_ports &= ~(1 << portnum);
  962. bus_state->port_c_suspend |= 1 << portnum;
  963. }
  964. }
  965. if (link_state == XDEV_RESUME) {
  966. ret = xhci_handle_usb2_port_link_resume(port, status,
  967. portsc, flags);
  968. if (ret)
  969. return;
  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 (bus_state->resume_done[portnum] ||
  979. test_bit(portnum, &bus_state->resuming_ports)) {
  980. bus_state->resume_done[portnum] = 0;
  981. clear_bit(portnum, &bus_state->resuming_ports);
  982. usb_hcd_end_port_resume(&port->rhub->hcd->self, portnum);
  983. }
  984. bus_state->suspended_ports &= ~(1 << portnum);
  985. }
  986. }
  987. /*
  988. * Converts a raw xHCI port status into the format that external USB 2.0 or USB
  989. * 3.0 hubs use.
  990. *
  991. * Possible side effects:
  992. * - Mark a port as being done with device resume,
  993. * and ring the endpoint doorbells.
  994. * - Stop the Synopsys redriver Compliance Mode polling.
  995. * - Drop and reacquire the xHCI lock, in order to wait for port resume.
  996. */
  997. static u32 xhci_get_port_status(struct usb_hcd *hcd,
  998. struct xhci_bus_state *bus_state,
  999. u16 wIndex, u32 raw_port_status,
  1000. unsigned long *flags)
  1001. __releases(&xhci->lock)
  1002. __acquires(&xhci->lock)
  1003. {
  1004. u32 status = 0;
  1005. struct xhci_hub *rhub;
  1006. struct xhci_port *port;
  1007. rhub = xhci_get_rhub(hcd);
  1008. port = rhub->ports[wIndex];
  1009. /* common wPortChange bits */
  1010. if (raw_port_status & PORT_CSC)
  1011. status |= USB_PORT_STAT_C_CONNECTION << 16;
  1012. if (raw_port_status & PORT_PEC)
  1013. status |= USB_PORT_STAT_C_ENABLE << 16;
  1014. if ((raw_port_status & PORT_OCC))
  1015. status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  1016. if ((raw_port_status & PORT_RC))
  1017. status |= USB_PORT_STAT_C_RESET << 16;
  1018. /* common wPortStatus bits */
  1019. if (raw_port_status & PORT_CONNECT) {
  1020. status |= USB_PORT_STAT_CONNECTION;
  1021. status |= xhci_port_speed(raw_port_status);
  1022. }
  1023. if (raw_port_status & PORT_PE)
  1024. status |= USB_PORT_STAT_ENABLE;
  1025. if (raw_port_status & PORT_OC)
  1026. status |= USB_PORT_STAT_OVERCURRENT;
  1027. if (raw_port_status & PORT_RESET)
  1028. status |= USB_PORT_STAT_RESET;
  1029. /* USB2 and USB3 specific bits, including Port Link State */
  1030. if (hcd->speed >= HCD_USB3)
  1031. xhci_get_usb3_port_status(port, &status, raw_port_status);
  1032. else
  1033. xhci_get_usb2_port_status(port, &status, raw_port_status,
  1034. flags);
  1035. /*
  1036. * Clear stale usb2 resume signalling variables in case port changed
  1037. * state during resume signalling. For example on error
  1038. */
  1039. if ((bus_state->resume_done[wIndex] ||
  1040. test_bit(wIndex, &bus_state->resuming_ports)) &&
  1041. (raw_port_status & PORT_PLS_MASK) != XDEV_U3 &&
  1042. (raw_port_status & PORT_PLS_MASK) != XDEV_RESUME) {
  1043. bus_state->resume_done[wIndex] = 0;
  1044. clear_bit(wIndex, &bus_state->resuming_ports);
  1045. usb_hcd_end_port_resume(&hcd->self, wIndex);
  1046. }
  1047. if (bus_state->port_c_suspend & (1 << wIndex))
  1048. status |= USB_PORT_STAT_C_SUSPEND << 16;
  1049. return status;
  1050. }
  1051. int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  1052. u16 wIndex, char *buf, u16 wLength)
  1053. {
  1054. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1055. int max_ports;
  1056. unsigned long flags;
  1057. u32 temp, status;
  1058. int retval = 0;
  1059. int slot_id;
  1060. struct xhci_bus_state *bus_state;
  1061. u16 link_state = 0;
  1062. u16 wake_mask = 0;
  1063. u16 timeout = 0;
  1064. u16 test_mode = 0;
  1065. struct xhci_hub *rhub;
  1066. struct xhci_port **ports;
  1067. rhub = xhci_get_rhub(hcd);
  1068. ports = rhub->ports;
  1069. max_ports = rhub->num_ports;
  1070. bus_state = &rhub->bus_state;
  1071. spin_lock_irqsave(&xhci->lock, flags);
  1072. switch (typeReq) {
  1073. case GetHubStatus:
  1074. /* No power source, over-current reported per port */
  1075. memset(buf, 0, 4);
  1076. break;
  1077. case GetHubDescriptor:
  1078. /* Check to make sure userspace is asking for the USB 3.0 hub
  1079. * descriptor for the USB 3.0 roothub. If not, we stall the
  1080. * endpoint, like external hubs do.
  1081. */
  1082. if (hcd->speed >= HCD_USB3 &&
  1083. (wLength < USB_DT_SS_HUB_SIZE ||
  1084. wValue != (USB_DT_SS_HUB << 8))) {
  1085. xhci_dbg(xhci, "Wrong hub descriptor type for "
  1086. "USB 3.0 roothub.\n");
  1087. goto error;
  1088. }
  1089. xhci_hub_descriptor(hcd, xhci,
  1090. (struct usb_hub_descriptor *) buf);
  1091. break;
  1092. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  1093. if ((wValue & 0xff00) != (USB_DT_BOS << 8))
  1094. goto error;
  1095. if (hcd->speed < HCD_USB3)
  1096. goto error;
  1097. retval = xhci_create_usb3x_bos_desc(xhci, buf, wLength);
  1098. spin_unlock_irqrestore(&xhci->lock, flags);
  1099. return retval;
  1100. case GetPortStatus:
  1101. if (!wIndex || wIndex > max_ports)
  1102. goto error;
  1103. wIndex--;
  1104. temp = readl(ports[wIndex]->addr);
  1105. if (temp == ~(u32)0) {
  1106. xhci_hc_died(xhci);
  1107. retval = -ENODEV;
  1108. break;
  1109. }
  1110. trace_xhci_get_port_status(wIndex, temp);
  1111. status = xhci_get_port_status(hcd, bus_state, wIndex, temp,
  1112. &flags);
  1113. if (status == 0xffffffff)
  1114. goto error;
  1115. xhci_dbg(xhci, "Get port status %d-%d read: 0x%x, return 0x%x",
  1116. hcd->self.busnum, wIndex + 1, temp, status);
  1117. put_unaligned(cpu_to_le32(status), (__le32 *) buf);
  1118. /* if USB 3.1 extended port status return additional 4 bytes */
  1119. if (wValue == 0x02) {
  1120. u32 port_li;
  1121. if (hcd->speed < HCD_USB31 || wLength != 8) {
  1122. xhci_err(xhci, "get ext port status invalid parameter\n");
  1123. retval = -EINVAL;
  1124. break;
  1125. }
  1126. port_li = readl(ports[wIndex]->addr + PORTLI);
  1127. status = xhci_get_ext_port_status(temp, port_li);
  1128. put_unaligned_le32(status, &buf[4]);
  1129. }
  1130. break;
  1131. case SetPortFeature:
  1132. if (wValue == USB_PORT_FEAT_LINK_STATE)
  1133. link_state = (wIndex & 0xff00) >> 3;
  1134. if (wValue == USB_PORT_FEAT_REMOTE_WAKE_MASK)
  1135. wake_mask = wIndex & 0xff00;
  1136. if (wValue == USB_PORT_FEAT_TEST)
  1137. test_mode = (wIndex & 0xff00) >> 8;
  1138. /* The MSB of wIndex is the U1/U2 timeout */
  1139. timeout = (wIndex & 0xff00) >> 8;
  1140. wIndex &= 0xff;
  1141. if (!wIndex || wIndex > max_ports)
  1142. goto error;
  1143. wIndex--;
  1144. temp = readl(ports[wIndex]->addr);
  1145. if (temp == ~(u32)0) {
  1146. xhci_hc_died(xhci);
  1147. retval = -ENODEV;
  1148. break;
  1149. }
  1150. temp = xhci_port_state_to_neutral(temp);
  1151. /* FIXME: What new port features do we need to support? */
  1152. switch (wValue) {
  1153. case USB_PORT_FEAT_SUSPEND:
  1154. temp = readl(ports[wIndex]->addr);
  1155. if ((temp & PORT_PLS_MASK) != XDEV_U0) {
  1156. /* Resume the port to U0 first */
  1157. xhci_set_link_state(xhci, ports[wIndex],
  1158. XDEV_U0);
  1159. spin_unlock_irqrestore(&xhci->lock, flags);
  1160. msleep(10);
  1161. spin_lock_irqsave(&xhci->lock, flags);
  1162. }
  1163. /* In spec software should not attempt to suspend
  1164. * a port unless the port reports that it is in the
  1165. * enabled (PED = ‘1’,PLS < ‘3’) state.
  1166. */
  1167. temp = readl(ports[wIndex]->addr);
  1168. if ((temp & PORT_PE) == 0 || (temp & PORT_RESET)
  1169. || (temp & PORT_PLS_MASK) >= XDEV_U3) {
  1170. xhci_warn(xhci, "USB core suspending port %d-%d not in U0/U1/U2\n",
  1171. hcd->self.busnum, wIndex + 1);
  1172. goto error;
  1173. }
  1174. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1175. wIndex + 1);
  1176. if (!slot_id) {
  1177. xhci_warn(xhci, "slot_id is zero\n");
  1178. goto error;
  1179. }
  1180. /* unlock to execute stop endpoint commands */
  1181. spin_unlock_irqrestore(&xhci->lock, flags);
  1182. xhci_stop_device(xhci, slot_id, 1);
  1183. spin_lock_irqsave(&xhci->lock, flags);
  1184. xhci_set_link_state(xhci, ports[wIndex], XDEV_U3);
  1185. spin_unlock_irqrestore(&xhci->lock, flags);
  1186. msleep(10); /* wait device to enter */
  1187. spin_lock_irqsave(&xhci->lock, flags);
  1188. temp = readl(ports[wIndex]->addr);
  1189. bus_state->suspended_ports |= 1 << wIndex;
  1190. break;
  1191. case USB_PORT_FEAT_LINK_STATE:
  1192. temp = readl(ports[wIndex]->addr);
  1193. /* Disable port */
  1194. if (link_state == USB_SS_PORT_LS_SS_DISABLED) {
  1195. xhci_dbg(xhci, "Disable port %d-%d\n",
  1196. hcd->self.busnum, wIndex + 1);
  1197. temp = xhci_port_state_to_neutral(temp);
  1198. /*
  1199. * Clear all change bits, so that we get a new
  1200. * connection event.
  1201. */
  1202. temp |= PORT_CSC | PORT_PEC | PORT_WRC |
  1203. PORT_OCC | PORT_RC | PORT_PLC |
  1204. PORT_CEC;
  1205. writel(temp | PORT_PE, ports[wIndex]->addr);
  1206. temp = readl(ports[wIndex]->addr);
  1207. break;
  1208. }
  1209. /* Put link in RxDetect (enable port) */
  1210. if (link_state == USB_SS_PORT_LS_RX_DETECT) {
  1211. xhci_dbg(xhci, "Enable port %d-%d\n",
  1212. hcd->self.busnum, wIndex + 1);
  1213. xhci_set_link_state(xhci, ports[wIndex],
  1214. link_state);
  1215. temp = readl(ports[wIndex]->addr);
  1216. break;
  1217. }
  1218. /*
  1219. * For xHCI 1.1 according to section 4.19.1.2.4.1 a
  1220. * root hub port's transition to compliance mode upon
  1221. * detecting LFPS timeout may be controlled by an
  1222. * Compliance Transition Enabled (CTE) flag (not
  1223. * software visible). This flag is set by writing 0xA
  1224. * to PORTSC PLS field which will allow transition to
  1225. * compliance mode the next time LFPS timeout is
  1226. * encountered. A warm reset will clear it.
  1227. *
  1228. * The CTE flag is only supported if the HCCPARAMS2 CTC
  1229. * flag is set, otherwise, the compliance substate is
  1230. * automatically entered as on 1.0 and prior.
  1231. */
  1232. if (link_state == USB_SS_PORT_LS_COMP_MOD) {
  1233. if (!HCC2_CTC(xhci->hcc_params2)) {
  1234. xhci_dbg(xhci, "CTC flag is 0, port already supports entering compliance mode\n");
  1235. break;
  1236. }
  1237. if ((temp & PORT_CONNECT)) {
  1238. xhci_warn(xhci, "Can't set compliance mode when port is connected\n");
  1239. goto error;
  1240. }
  1241. xhci_dbg(xhci, "Enable compliance mode transition for port %d-%d\n",
  1242. hcd->self.busnum, wIndex + 1);
  1243. xhci_set_link_state(xhci, ports[wIndex],
  1244. link_state);
  1245. temp = readl(ports[wIndex]->addr);
  1246. break;
  1247. }
  1248. /* Port must be enabled */
  1249. if (!(temp & PORT_PE)) {
  1250. retval = -ENODEV;
  1251. break;
  1252. }
  1253. /* Can't set port link state above '3' (U3) */
  1254. if (link_state > USB_SS_PORT_LS_U3) {
  1255. xhci_warn(xhci, "Cannot set port %d-%d link state %d\n",
  1256. hcd->self.busnum, wIndex + 1,
  1257. link_state);
  1258. goto error;
  1259. }
  1260. /*
  1261. * set link to U0, steps depend on current link state.
  1262. * U3: set link to U0 and wait for u3exit completion.
  1263. * U1/U2: no PLC complete event, only set link to U0.
  1264. * Resume/Recovery: device initiated U0, only wait for
  1265. * completion
  1266. */
  1267. if (link_state == USB_SS_PORT_LS_U0) {
  1268. u32 pls = temp & PORT_PLS_MASK;
  1269. bool wait_u0 = false;
  1270. /* already in U0 */
  1271. if (pls == XDEV_U0)
  1272. break;
  1273. if (pls == XDEV_U3 ||
  1274. pls == XDEV_RESUME ||
  1275. pls == XDEV_RECOVERY) {
  1276. wait_u0 = true;
  1277. reinit_completion(&bus_state->u3exit_done[wIndex]);
  1278. }
  1279. if (pls <= XDEV_U3) /* U1, U2, U3 */
  1280. xhci_set_link_state(xhci, ports[wIndex],
  1281. USB_SS_PORT_LS_U0);
  1282. if (!wait_u0) {
  1283. if (pls > XDEV_U3)
  1284. goto error;
  1285. break;
  1286. }
  1287. spin_unlock_irqrestore(&xhci->lock, flags);
  1288. if (!wait_for_completion_timeout(&bus_state->u3exit_done[wIndex],
  1289. msecs_to_jiffies(500)))
  1290. xhci_dbg(xhci, "missing U0 port change event for port %d-%d\n",
  1291. hcd->self.busnum, wIndex + 1);
  1292. spin_lock_irqsave(&xhci->lock, flags);
  1293. temp = readl(ports[wIndex]->addr);
  1294. break;
  1295. }
  1296. if (link_state == USB_SS_PORT_LS_U3) {
  1297. int retries = 16;
  1298. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1299. wIndex + 1);
  1300. if (slot_id) {
  1301. /* unlock to execute stop endpoint
  1302. * commands */
  1303. spin_unlock_irqrestore(&xhci->lock,
  1304. flags);
  1305. xhci_stop_device(xhci, slot_id, 1);
  1306. spin_lock_irqsave(&xhci->lock, flags);
  1307. }
  1308. xhci_set_link_state(xhci, ports[wIndex], USB_SS_PORT_LS_U3);
  1309. spin_unlock_irqrestore(&xhci->lock, flags);
  1310. while (retries--) {
  1311. usleep_range(4000, 8000);
  1312. temp = readl(ports[wIndex]->addr);
  1313. if ((temp & PORT_PLS_MASK) == XDEV_U3)
  1314. break;
  1315. }
  1316. spin_lock_irqsave(&xhci->lock, flags);
  1317. temp = readl(ports[wIndex]->addr);
  1318. bus_state->suspended_ports |= 1 << wIndex;
  1319. }
  1320. break;
  1321. case USB_PORT_FEAT_POWER:
  1322. /*
  1323. * Turn on ports, even if there isn't per-port switching.
  1324. * HC will report connect events even before this is set.
  1325. * However, hub_wq will ignore the roothub events until
  1326. * the roothub is registered.
  1327. */
  1328. xhci_set_port_power(xhci, hcd, wIndex, true, &flags);
  1329. break;
  1330. case USB_PORT_FEAT_RESET:
  1331. temp = (temp | PORT_RESET);
  1332. writel(temp, ports[wIndex]->addr);
  1333. temp = readl(ports[wIndex]->addr);
  1334. xhci_dbg(xhci, "set port reset, actual port %d-%d status = 0x%x\n",
  1335. hcd->self.busnum, wIndex + 1, temp);
  1336. break;
  1337. case USB_PORT_FEAT_REMOTE_WAKE_MASK:
  1338. xhci_set_remote_wake_mask(xhci, ports[wIndex],
  1339. wake_mask);
  1340. temp = readl(ports[wIndex]->addr);
  1341. xhci_dbg(xhci, "set port remote wake mask, actual port %d-%d status = 0x%x\n",
  1342. hcd->self.busnum, wIndex + 1, temp);
  1343. break;
  1344. case USB_PORT_FEAT_BH_PORT_RESET:
  1345. temp |= PORT_WR;
  1346. writel(temp, ports[wIndex]->addr);
  1347. temp = readl(ports[wIndex]->addr);
  1348. break;
  1349. case USB_PORT_FEAT_U1_TIMEOUT:
  1350. if (hcd->speed < HCD_USB3)
  1351. goto error;
  1352. temp = readl(ports[wIndex]->addr + PORTPMSC);
  1353. temp &= ~PORT_U1_TIMEOUT_MASK;
  1354. temp |= PORT_U1_TIMEOUT(timeout);
  1355. writel(temp, ports[wIndex]->addr + PORTPMSC);
  1356. break;
  1357. case USB_PORT_FEAT_U2_TIMEOUT:
  1358. if (hcd->speed < HCD_USB3)
  1359. goto error;
  1360. temp = readl(ports[wIndex]->addr + PORTPMSC);
  1361. temp &= ~PORT_U2_TIMEOUT_MASK;
  1362. temp |= PORT_U2_TIMEOUT(timeout);
  1363. writel(temp, ports[wIndex]->addr + PORTPMSC);
  1364. break;
  1365. case USB_PORT_FEAT_TEST:
  1366. /* 4.19.6 Port Test Modes (USB2 Test Mode) */
  1367. if (hcd->speed != HCD_USB2)
  1368. goto error;
  1369. if (test_mode > USB_TEST_FORCE_ENABLE ||
  1370. test_mode < USB_TEST_J)
  1371. goto error;
  1372. retval = xhci_enter_test_mode(xhci, test_mode, wIndex,
  1373. &flags);
  1374. break;
  1375. default:
  1376. goto error;
  1377. }
  1378. /* unblock any posted writes */
  1379. temp = readl(ports[wIndex]->addr);
  1380. break;
  1381. case ClearPortFeature:
  1382. if (!wIndex || wIndex > max_ports)
  1383. goto error;
  1384. wIndex--;
  1385. temp = readl(ports[wIndex]->addr);
  1386. if (temp == ~(u32)0) {
  1387. xhci_hc_died(xhci);
  1388. retval = -ENODEV;
  1389. break;
  1390. }
  1391. /* FIXME: What new port features do we need to support? */
  1392. temp = xhci_port_state_to_neutral(temp);
  1393. switch (wValue) {
  1394. case USB_PORT_FEAT_SUSPEND:
  1395. temp = readl(ports[wIndex]->addr);
  1396. xhci_dbg(xhci, "clear USB_PORT_FEAT_SUSPEND\n");
  1397. xhci_dbg(xhci, "PORTSC %04x\n", temp);
  1398. if (temp & PORT_RESET)
  1399. goto error;
  1400. if ((temp & PORT_PLS_MASK) == XDEV_U3) {
  1401. if ((temp & PORT_PE) == 0)
  1402. goto error;
  1403. set_bit(wIndex, &bus_state->resuming_ports);
  1404. usb_hcd_start_port_resume(&hcd->self, wIndex);
  1405. xhci_set_link_state(xhci, ports[wIndex],
  1406. XDEV_RESUME);
  1407. spin_unlock_irqrestore(&xhci->lock, flags);
  1408. msleep(USB_RESUME_TIMEOUT);
  1409. spin_lock_irqsave(&xhci->lock, flags);
  1410. xhci_set_link_state(xhci, ports[wIndex],
  1411. XDEV_U0);
  1412. clear_bit(wIndex, &bus_state->resuming_ports);
  1413. usb_hcd_end_port_resume(&hcd->self, wIndex);
  1414. }
  1415. bus_state->port_c_suspend |= 1 << wIndex;
  1416. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1417. wIndex + 1);
  1418. if (!slot_id) {
  1419. xhci_dbg(xhci, "slot_id is zero\n");
  1420. goto error;
  1421. }
  1422. xhci_ring_device(xhci, slot_id);
  1423. break;
  1424. case USB_PORT_FEAT_C_SUSPEND:
  1425. bus_state->port_c_suspend &= ~(1 << wIndex);
  1426. fallthrough;
  1427. case USB_PORT_FEAT_C_RESET:
  1428. case USB_PORT_FEAT_C_BH_PORT_RESET:
  1429. case USB_PORT_FEAT_C_CONNECTION:
  1430. case USB_PORT_FEAT_C_OVER_CURRENT:
  1431. case USB_PORT_FEAT_C_ENABLE:
  1432. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  1433. case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
  1434. xhci_clear_port_change_bit(xhci, wValue, wIndex,
  1435. ports[wIndex]->addr, temp);
  1436. break;
  1437. case USB_PORT_FEAT_ENABLE:
  1438. xhci_disable_port(hcd, xhci, wIndex,
  1439. ports[wIndex]->addr, temp);
  1440. break;
  1441. case USB_PORT_FEAT_POWER:
  1442. xhci_set_port_power(xhci, hcd, wIndex, false, &flags);
  1443. break;
  1444. case USB_PORT_FEAT_TEST:
  1445. retval = xhci_exit_test_mode(xhci);
  1446. break;
  1447. default:
  1448. goto error;
  1449. }
  1450. break;
  1451. default:
  1452. error:
  1453. /* "stall" on error */
  1454. retval = -EPIPE;
  1455. }
  1456. spin_unlock_irqrestore(&xhci->lock, flags);
  1457. return retval;
  1458. }
  1459. /*
  1460. * Returns 0 if the status hasn't changed, or the number of bytes in buf.
  1461. * Ports are 0-indexed from the HCD point of view,
  1462. * and 1-indexed from the USB core pointer of view.
  1463. *
  1464. * Note that the status change bits will be cleared as soon as a port status
  1465. * change event is generated, so we use the saved status from that event.
  1466. */
  1467. int xhci_hub_status_data(struct usb_hcd *hcd, char *buf)
  1468. {
  1469. unsigned long flags;
  1470. u32 temp, status;
  1471. u32 mask;
  1472. int i, retval;
  1473. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1474. int max_ports;
  1475. struct xhci_bus_state *bus_state;
  1476. bool reset_change = false;
  1477. struct xhci_hub *rhub;
  1478. struct xhci_port **ports;
  1479. rhub = xhci_get_rhub(hcd);
  1480. ports = rhub->ports;
  1481. max_ports = rhub->num_ports;
  1482. bus_state = &rhub->bus_state;
  1483. /* Initial status is no changes */
  1484. retval = (max_ports + 8) / 8;
  1485. memset(buf, 0, retval);
  1486. /*
  1487. * Inform the usbcore about resume-in-progress by returning
  1488. * a non-zero value even if there are no status changes.
  1489. */
  1490. spin_lock_irqsave(&xhci->lock, flags);
  1491. status = bus_state->resuming_ports;
  1492. /*
  1493. * SS devices are only visible to roothub after link training completes.
  1494. * Keep polling roothubs for a grace period after xHC start
  1495. */
  1496. if (xhci->run_graceperiod) {
  1497. if (time_before(jiffies, xhci->run_graceperiod))
  1498. status = 1;
  1499. else
  1500. xhci->run_graceperiod = 0;
  1501. }
  1502. mask = PORT_CSC | PORT_PEC | PORT_OCC | PORT_PLC | PORT_WRC | PORT_CEC;
  1503. /* For each port, did anything change? If so, set that bit in buf. */
  1504. for (i = 0; i < max_ports; i++) {
  1505. temp = readl(ports[i]->addr);
  1506. if (temp == ~(u32)0) {
  1507. xhci_hc_died(xhci);
  1508. retval = -ENODEV;
  1509. break;
  1510. }
  1511. trace_xhci_hub_status_data(i, temp);
  1512. if ((temp & mask) != 0 ||
  1513. (bus_state->port_c_suspend & 1 << i) ||
  1514. (bus_state->resume_done[i] && time_after_eq(
  1515. jiffies, bus_state->resume_done[i]))) {
  1516. buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
  1517. status = 1;
  1518. }
  1519. if ((temp & PORT_RC))
  1520. reset_change = true;
  1521. if (temp & PORT_OC)
  1522. status = 1;
  1523. }
  1524. if (!status && !reset_change) {
  1525. xhci_dbg(xhci, "%s: stopping usb%d port polling\n",
  1526. __func__, hcd->self.busnum);
  1527. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  1528. }
  1529. spin_unlock_irqrestore(&xhci->lock, flags);
  1530. return status ? retval : 0;
  1531. }
  1532. #ifdef CONFIG_PM
  1533. int xhci_bus_suspend(struct usb_hcd *hcd)
  1534. {
  1535. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1536. int max_ports, port_index;
  1537. struct xhci_bus_state *bus_state;
  1538. unsigned long flags;
  1539. struct xhci_hub *rhub;
  1540. struct xhci_port **ports;
  1541. u32 portsc_buf[USB_MAXCHILDREN];
  1542. bool wake_enabled;
  1543. rhub = xhci_get_rhub(hcd);
  1544. ports = rhub->ports;
  1545. max_ports = rhub->num_ports;
  1546. bus_state = &rhub->bus_state;
  1547. wake_enabled = hcd->self.root_hub->do_remote_wakeup;
  1548. spin_lock_irqsave(&xhci->lock, flags);
  1549. if (wake_enabled) {
  1550. if (bus_state->resuming_ports || /* USB2 */
  1551. bus_state->port_remote_wakeup) { /* USB3 */
  1552. spin_unlock_irqrestore(&xhci->lock, flags);
  1553. xhci_dbg(xhci, "usb%d bus suspend to fail because a port is resuming\n",
  1554. hcd->self.busnum);
  1555. return -EBUSY;
  1556. }
  1557. }
  1558. /*
  1559. * Prepare ports for suspend, but don't write anything before all ports
  1560. * are checked and we know bus suspend can proceed
  1561. */
  1562. bus_state->bus_suspended = 0;
  1563. port_index = max_ports;
  1564. while (port_index--) {
  1565. u32 t1, t2;
  1566. int retries = 10;
  1567. retry:
  1568. t1 = readl(ports[port_index]->addr);
  1569. t2 = xhci_port_state_to_neutral(t1);
  1570. portsc_buf[port_index] = 0;
  1571. /*
  1572. * Give a USB3 port in link training time to finish, but don't
  1573. * prevent suspend as port might be stuck
  1574. */
  1575. if ((hcd->speed >= HCD_USB3) && retries-- &&
  1576. (t1 & PORT_PLS_MASK) == XDEV_POLLING) {
  1577. spin_unlock_irqrestore(&xhci->lock, flags);
  1578. msleep(XHCI_PORT_POLLING_LFPS_TIME);
  1579. spin_lock_irqsave(&xhci->lock, flags);
  1580. xhci_dbg(xhci, "port %d-%d polling in bus suspend, waiting\n",
  1581. hcd->self.busnum, port_index + 1);
  1582. goto retry;
  1583. }
  1584. /* bail out if port detected a over-current condition */
  1585. if (t1 & PORT_OC) {
  1586. bus_state->bus_suspended = 0;
  1587. spin_unlock_irqrestore(&xhci->lock, flags);
  1588. xhci_dbg(xhci, "Bus suspend bailout, port over-current detected\n");
  1589. return -EBUSY;
  1590. }
  1591. /* suspend ports in U0, or bail out for new connect changes */
  1592. if ((t1 & PORT_PE) && (t1 & PORT_PLS_MASK) == XDEV_U0) {
  1593. if ((t1 & PORT_CSC) && wake_enabled) {
  1594. bus_state->bus_suspended = 0;
  1595. spin_unlock_irqrestore(&xhci->lock, flags);
  1596. xhci_dbg(xhci, "Bus suspend bailout, port connect change\n");
  1597. return -EBUSY;
  1598. }
  1599. xhci_dbg(xhci, "port %d-%d not suspended\n",
  1600. hcd->self.busnum, port_index + 1);
  1601. t2 &= ~PORT_PLS_MASK;
  1602. t2 |= PORT_LINK_STROBE | XDEV_U3;
  1603. set_bit(port_index, &bus_state->bus_suspended);
  1604. }
  1605. /* USB core sets remote wake mask for USB 3.0 hubs,
  1606. * including the USB 3.0 roothub, but only if CONFIG_PM
  1607. * is enabled, so also enable remote wake here.
  1608. */
  1609. if (wake_enabled) {
  1610. if (t1 & PORT_CONNECT) {
  1611. t2 |= PORT_WKOC_E | PORT_WKDISC_E;
  1612. t2 &= ~PORT_WKCONN_E;
  1613. } else {
  1614. t2 |= PORT_WKOC_E | PORT_WKCONN_E;
  1615. t2 &= ~PORT_WKDISC_E;
  1616. }
  1617. if ((xhci->quirks & XHCI_U2_DISABLE_WAKE) &&
  1618. (hcd->speed < HCD_USB3)) {
  1619. if (usb_amd_pt_check_port(hcd->self.controller,
  1620. port_index))
  1621. t2 &= ~PORT_WAKE_BITS;
  1622. }
  1623. } else
  1624. t2 &= ~PORT_WAKE_BITS;
  1625. t1 = xhci_port_state_to_neutral(t1);
  1626. if (t1 != t2)
  1627. portsc_buf[port_index] = t2;
  1628. }
  1629. /* write port settings, stopping and suspending ports if needed */
  1630. port_index = max_ports;
  1631. while (port_index--) {
  1632. if (!portsc_buf[port_index])
  1633. continue;
  1634. if (test_bit(port_index, &bus_state->bus_suspended)) {
  1635. int slot_id;
  1636. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1637. port_index + 1);
  1638. if (slot_id) {
  1639. spin_unlock_irqrestore(&xhci->lock, flags);
  1640. xhci_stop_device(xhci, slot_id, 1);
  1641. spin_lock_irqsave(&xhci->lock, flags);
  1642. }
  1643. }
  1644. writel(portsc_buf[port_index], ports[port_index]->addr);
  1645. }
  1646. hcd->state = HC_STATE_SUSPENDED;
  1647. bus_state->next_statechange = jiffies + msecs_to_jiffies(10);
  1648. spin_unlock_irqrestore(&xhci->lock, flags);
  1649. if (bus_state->bus_suspended)
  1650. usleep_range(5000, 10000);
  1651. return 0;
  1652. }
  1653. EXPORT_SYMBOL_GPL(xhci_bus_suspend);
  1654. /*
  1655. * Workaround for missing Cold Attach Status (CAS) if device re-plugged in S3.
  1656. * warm reset a USB3 device stuck in polling or compliance mode after resume.
  1657. * See Intel 100/c230 series PCH specification update Doc #332692-006 Errata #8
  1658. */
  1659. static bool xhci_port_missing_cas_quirk(struct xhci_port *port)
  1660. {
  1661. u32 portsc;
  1662. portsc = readl(port->addr);
  1663. /* if any of these are set we are not stuck */
  1664. if (portsc & (PORT_CONNECT | PORT_CAS))
  1665. return false;
  1666. if (((portsc & PORT_PLS_MASK) != XDEV_POLLING) &&
  1667. ((portsc & PORT_PLS_MASK) != XDEV_COMP_MODE))
  1668. return false;
  1669. /* clear wakeup/change bits, and do a warm port reset */
  1670. portsc &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
  1671. portsc |= PORT_WR;
  1672. writel(portsc, port->addr);
  1673. /* flush write */
  1674. readl(port->addr);
  1675. return true;
  1676. }
  1677. int xhci_bus_resume(struct usb_hcd *hcd)
  1678. {
  1679. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1680. struct xhci_bus_state *bus_state;
  1681. unsigned long flags;
  1682. int max_ports, port_index;
  1683. int slot_id;
  1684. int sret;
  1685. u32 next_state;
  1686. u32 temp, portsc;
  1687. struct xhci_hub *rhub;
  1688. struct xhci_port **ports;
  1689. rhub = xhci_get_rhub(hcd);
  1690. ports = rhub->ports;
  1691. max_ports = rhub->num_ports;
  1692. bus_state = &rhub->bus_state;
  1693. if (time_before(jiffies, bus_state->next_statechange))
  1694. msleep(5);
  1695. spin_lock_irqsave(&xhci->lock, flags);
  1696. if (!HCD_HW_ACCESSIBLE(hcd)) {
  1697. spin_unlock_irqrestore(&xhci->lock, flags);
  1698. return -ESHUTDOWN;
  1699. }
  1700. /* delay the irqs */
  1701. temp = readl(&xhci->op_regs->command);
  1702. temp &= ~CMD_EIE;
  1703. writel(temp, &xhci->op_regs->command);
  1704. /* bus specific resume for ports we suspended at bus_suspend */
  1705. if (hcd->speed >= HCD_USB3)
  1706. next_state = XDEV_U0;
  1707. else
  1708. next_state = XDEV_RESUME;
  1709. port_index = max_ports;
  1710. while (port_index--) {
  1711. portsc = readl(ports[port_index]->addr);
  1712. /* warm reset CAS limited ports stuck in polling/compliance */
  1713. if ((xhci->quirks & XHCI_MISSING_CAS) &&
  1714. (hcd->speed >= HCD_USB3) &&
  1715. xhci_port_missing_cas_quirk(ports[port_index])) {
  1716. xhci_dbg(xhci, "reset stuck port %d-%d\n",
  1717. hcd->self.busnum, port_index + 1);
  1718. clear_bit(port_index, &bus_state->bus_suspended);
  1719. continue;
  1720. }
  1721. /* resume if we suspended the link, and it is still suspended */
  1722. if (test_bit(port_index, &bus_state->bus_suspended))
  1723. switch (portsc & PORT_PLS_MASK) {
  1724. case XDEV_U3:
  1725. portsc = xhci_port_state_to_neutral(portsc);
  1726. portsc &= ~PORT_PLS_MASK;
  1727. portsc |= PORT_LINK_STROBE | next_state;
  1728. break;
  1729. case XDEV_RESUME:
  1730. /* resume already initiated */
  1731. break;
  1732. default:
  1733. /* not in a resumeable state, ignore it */
  1734. clear_bit(port_index,
  1735. &bus_state->bus_suspended);
  1736. break;
  1737. }
  1738. /* disable wake for all ports, write new link state if needed */
  1739. portsc &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
  1740. writel(portsc, ports[port_index]->addr);
  1741. }
  1742. /* USB2 specific resume signaling delay and U0 link state transition */
  1743. if (hcd->speed < HCD_USB3) {
  1744. if (bus_state->bus_suspended) {
  1745. spin_unlock_irqrestore(&xhci->lock, flags);
  1746. msleep(USB_RESUME_TIMEOUT);
  1747. spin_lock_irqsave(&xhci->lock, flags);
  1748. }
  1749. for_each_set_bit(port_index, &bus_state->bus_suspended,
  1750. BITS_PER_LONG) {
  1751. /* Clear PLC to poll it later for U0 transition */
  1752. xhci_test_and_clear_bit(xhci, ports[port_index],
  1753. PORT_PLC);
  1754. xhci_set_link_state(xhci, ports[port_index], XDEV_U0);
  1755. }
  1756. }
  1757. /* poll for U0 link state complete, both USB2 and USB3 */
  1758. for_each_set_bit(port_index, &bus_state->bus_suspended, BITS_PER_LONG) {
  1759. sret = xhci_handshake(ports[port_index]->addr, PORT_PLC,
  1760. PORT_PLC, 10 * 1000);
  1761. if (sret) {
  1762. xhci_warn(xhci, "port %d-%d resume PLC timeout\n",
  1763. hcd->self.busnum, port_index + 1);
  1764. continue;
  1765. }
  1766. xhci_test_and_clear_bit(xhci, ports[port_index], PORT_PLC);
  1767. slot_id = xhci_find_slot_id_by_port(hcd, xhci, port_index + 1);
  1768. if (slot_id)
  1769. xhci_ring_device(xhci, slot_id);
  1770. }
  1771. (void) readl(&xhci->op_regs->command);
  1772. bus_state->next_statechange = jiffies + msecs_to_jiffies(5);
  1773. /* re-enable irqs */
  1774. temp = readl(&xhci->op_regs->command);
  1775. temp |= CMD_EIE;
  1776. writel(temp, &xhci->op_regs->command);
  1777. temp = readl(&xhci->op_regs->command);
  1778. spin_unlock_irqrestore(&xhci->lock, flags);
  1779. return 0;
  1780. }
  1781. EXPORT_SYMBOL_GPL(xhci_bus_resume);
  1782. unsigned long xhci_get_resuming_ports(struct usb_hcd *hcd)
  1783. {
  1784. struct xhci_hub *rhub = xhci_get_rhub(hcd);
  1785. /* USB3 port wakeups are reported via usb_wakeup_notification() */
  1786. return rhub->bus_state.resuming_ports; /* USB2 ports only */
  1787. }
  1788. #endif /* CONFIG_PM */