xhci-hub.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  1. /*
  2. * xHCI host controller driver
  3. *
  4. * Copyright (C) 2008 Intel Corp.
  5. *
  6. * Author: Sarah Sharp
  7. * Some code borrowed from the Linux EHCI driver.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16. * for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software Foundation,
  20. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/slab.h>
  23. #include <asm/unaligned.h>
  24. #include "xhci.h"
  25. #include "xhci-trace.h"
  26. #define PORT_WAKE_BITS (PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E)
  27. #define PORT_RWC_BITS (PORT_CSC | PORT_PEC | PORT_WRC | PORT_OCC | \
  28. PORT_RC | PORT_PLC | PORT_PE)
  29. /* USB 3 BOS descriptor and a capability descriptors, combined.
  30. * Fields will be adjusted and added later in xhci_create_usb3_bos_desc()
  31. */
  32. static u8 usb_bos_descriptor [] = {
  33. USB_DT_BOS_SIZE, /* __u8 bLength, 5 bytes */
  34. USB_DT_BOS, /* __u8 bDescriptorType */
  35. 0x0F, 0x00, /* __le16 wTotalLength, 15 bytes */
  36. 0x1, /* __u8 bNumDeviceCaps */
  37. /* First device capability, SuperSpeed */
  38. USB_DT_USB_SS_CAP_SIZE, /* __u8 bLength, 10 bytes */
  39. USB_DT_DEVICE_CAPABILITY, /* Device Capability */
  40. USB_SS_CAP_TYPE, /* bDevCapabilityType, SUPERSPEED_USB */
  41. 0x00, /* bmAttributes, LTM off by default */
  42. USB_5GBPS_OPERATION, 0x00, /* wSpeedsSupported, 5Gbps only */
  43. 0x03, /* bFunctionalitySupport,
  44. USB 3.0 speed only */
  45. 0x00, /* bU1DevExitLat, set later. */
  46. 0x00, 0x00, /* __le16 bU2DevExitLat, set later. */
  47. /* Second device capability, SuperSpeedPlus */
  48. 0x1c, /* bLength 28, will be adjusted later */
  49. USB_DT_DEVICE_CAPABILITY, /* Device Capability */
  50. USB_SSP_CAP_TYPE, /* bDevCapabilityType SUPERSPEED_PLUS */
  51. 0x00, /* bReserved 0 */
  52. 0x23, 0x00, 0x00, 0x00, /* bmAttributes, SSAC=3 SSIC=1 */
  53. 0x01, 0x00, /* wFunctionalitySupport */
  54. 0x00, 0x00, /* wReserved 0 */
  55. /* Default Sublink Speed Attributes, overwrite if custom PSI exists */
  56. 0x34, 0x00, 0x05, 0x00, /* 5Gbps, symmetric, rx, ID = 4 */
  57. 0xb4, 0x00, 0x05, 0x00, /* 5Gbps, symmetric, tx, ID = 4 */
  58. 0x35, 0x40, 0x0a, 0x00, /* 10Gbps, SSP, symmetric, rx, ID = 5 */
  59. 0xb5, 0x40, 0x0a, 0x00, /* 10Gbps, SSP, symmetric, tx, ID = 5 */
  60. };
  61. static int xhci_create_usb3_bos_desc(struct xhci_hcd *xhci, char *buf,
  62. u16 wLength)
  63. {
  64. int i, ssa_count;
  65. u32 temp;
  66. u16 desc_size, ssp_cap_size, ssa_size = 0;
  67. bool usb3_1 = false;
  68. desc_size = USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE;
  69. ssp_cap_size = sizeof(usb_bos_descriptor) - desc_size;
  70. /* does xhci support USB 3.1 Enhanced SuperSpeed */
  71. if (xhci->usb3_rhub.min_rev >= 0x01) {
  72. /* does xhci provide a PSI table for SSA speed attributes? */
  73. if (xhci->usb3_rhub.psi_count) {
  74. /* two SSA entries for each unique PSI ID, RX and TX */
  75. ssa_count = xhci->usb3_rhub.psi_uid_count * 2;
  76. ssa_size = ssa_count * sizeof(u32);
  77. ssp_cap_size -= 16; /* skip copying the default SSA */
  78. }
  79. desc_size += ssp_cap_size;
  80. usb3_1 = true;
  81. }
  82. memcpy(buf, &usb_bos_descriptor, min(desc_size, wLength));
  83. if (usb3_1) {
  84. /* modify bos descriptor bNumDeviceCaps and wTotalLength */
  85. buf[4] += 1;
  86. put_unaligned_le16(desc_size + ssa_size, &buf[2]);
  87. }
  88. if (wLength < USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE)
  89. return wLength;
  90. /* Indicate whether the host has LTM support. */
  91. temp = readl(&xhci->cap_regs->hcc_params);
  92. if (HCC_LTC(temp))
  93. buf[8] |= USB_LTM_SUPPORT;
  94. /* Set the U1 and U2 exit latencies. */
  95. if ((xhci->quirks & XHCI_LPM_SUPPORT)) {
  96. temp = readl(&xhci->cap_regs->hcs_params3);
  97. buf[12] = HCS_U1_LATENCY(temp);
  98. put_unaligned_le16(HCS_U2_LATENCY(temp), &buf[13]);
  99. }
  100. /* If PSI table exists, add the custom speed attributes from it */
  101. if (usb3_1 && xhci->usb3_rhub.psi_count) {
  102. u32 ssp_cap_base, bm_attrib, psi, psi_mant, psi_exp;
  103. int offset;
  104. ssp_cap_base = USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE;
  105. if (wLength < desc_size)
  106. return wLength;
  107. buf[ssp_cap_base] = ssp_cap_size + ssa_size;
  108. /* attribute count SSAC bits 4:0 and ID count SSIC bits 8:5 */
  109. bm_attrib = (ssa_count - 1) & 0x1f;
  110. bm_attrib |= (xhci->usb3_rhub.psi_uid_count - 1) << 5;
  111. put_unaligned_le32(bm_attrib, &buf[ssp_cap_base + 4]);
  112. if (wLength < desc_size + ssa_size)
  113. return wLength;
  114. /*
  115. * Create the Sublink Speed Attributes (SSA) array.
  116. * The xhci PSI field and USB 3.1 SSA fields are very similar,
  117. * but link type bits 7:6 differ for values 01b and 10b.
  118. * xhci has also only one PSI entry for a symmetric link when
  119. * USB 3.1 requires two SSA entries (RX and TX) for every link
  120. */
  121. offset = desc_size;
  122. for (i = 0; i < xhci->usb3_rhub.psi_count; i++) {
  123. psi = xhci->usb3_rhub.psi[i];
  124. psi &= ~USB_SSP_SUBLINK_SPEED_RSVD;
  125. psi_exp = XHCI_EXT_PORT_PSIE(psi);
  126. psi_mant = XHCI_EXT_PORT_PSIM(psi);
  127. /* Shift to Gbps and set SSP Link BIT(14) if 10Gpbs */
  128. for (; psi_exp < 3; psi_exp++)
  129. psi_mant /= 1000;
  130. if (psi_mant >= 10)
  131. psi |= BIT(14);
  132. if ((psi & PLT_MASK) == PLT_SYM) {
  133. /* Symmetric, create SSA RX and TX from one PSI entry */
  134. put_unaligned_le32(psi, &buf[offset]);
  135. psi |= 1 << 7; /* turn entry to TX */
  136. offset += 4;
  137. if (offset >= desc_size + ssa_size)
  138. return desc_size + ssa_size;
  139. } else if ((psi & PLT_MASK) == PLT_ASYM_RX) {
  140. /* Asymetric RX, flip bits 7:6 for SSA */
  141. psi ^= PLT_MASK;
  142. }
  143. put_unaligned_le32(psi, &buf[offset]);
  144. offset += 4;
  145. if (offset >= desc_size + ssa_size)
  146. return desc_size + ssa_size;
  147. }
  148. }
  149. /* ssa_size is 0 for other than usb 3.1 hosts */
  150. return desc_size + ssa_size;
  151. }
  152. static void xhci_common_hub_descriptor(struct xhci_hcd *xhci,
  153. struct usb_hub_descriptor *desc, int ports)
  154. {
  155. u16 temp;
  156. desc->bPwrOn2PwrGood = 10; /* xhci section 5.4.9 says 20ms max */
  157. desc->bHubContrCurrent = 0;
  158. desc->bNbrPorts = ports;
  159. temp = 0;
  160. /* Bits 1:0 - support per-port power switching, or power always on */
  161. if (HCC_PPC(xhci->hcc_params))
  162. temp |= HUB_CHAR_INDV_PORT_LPSM;
  163. else
  164. temp |= HUB_CHAR_NO_LPSM;
  165. /* Bit 2 - root hubs are not part of a compound device */
  166. /* Bits 4:3 - individual port over current protection */
  167. temp |= HUB_CHAR_INDV_PORT_OCPM;
  168. /* Bits 6:5 - no TTs in root ports */
  169. /* Bit 7 - no port indicators */
  170. desc->wHubCharacteristics = cpu_to_le16(temp);
  171. }
  172. /* Fill in the USB 2.0 roothub descriptor */
  173. static void xhci_usb2_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  174. struct usb_hub_descriptor *desc)
  175. {
  176. int ports;
  177. u16 temp;
  178. __u8 port_removable[(USB_MAXCHILDREN + 1 + 7) / 8];
  179. u32 portsc;
  180. unsigned int i;
  181. ports = xhci->num_usb2_ports;
  182. xhci_common_hub_descriptor(xhci, desc, ports);
  183. desc->bDescriptorType = USB_DT_HUB;
  184. temp = 1 + (ports / 8);
  185. desc->bDescLength = USB_DT_HUB_NONVAR_SIZE + 2 * temp;
  186. /* The Device Removable bits are reported on a byte granularity.
  187. * If the port doesn't exist within that byte, the bit is set to 0.
  188. */
  189. memset(port_removable, 0, sizeof(port_removable));
  190. for (i = 0; i < ports; i++) {
  191. portsc = readl(xhci->usb2_ports[i]);
  192. /* If a device is removable, PORTSC reports a 0, same as in the
  193. * hub descriptor DeviceRemovable bits.
  194. */
  195. if (portsc & PORT_DEV_REMOVE)
  196. /* This math is hairy because bit 0 of DeviceRemovable
  197. * is reserved, and bit 1 is for port 1, etc.
  198. */
  199. port_removable[(i + 1) / 8] |= 1 << ((i + 1) % 8);
  200. }
  201. /* ch11.h defines a hub descriptor that has room for USB_MAXCHILDREN
  202. * ports on it. The USB 2.0 specification says that there are two
  203. * variable length fields at the end of the hub descriptor:
  204. * DeviceRemovable and PortPwrCtrlMask. But since we can have less than
  205. * USB_MAXCHILDREN ports, we may need to use the DeviceRemovable array
  206. * to set PortPwrCtrlMask bits. PortPwrCtrlMask must always be set to
  207. * 0xFF, so we initialize the both arrays (DeviceRemovable and
  208. * PortPwrCtrlMask) to 0xFF. Then we set the DeviceRemovable for each
  209. * set of ports that actually exist.
  210. */
  211. memset(desc->u.hs.DeviceRemovable, 0xff,
  212. sizeof(desc->u.hs.DeviceRemovable));
  213. memset(desc->u.hs.PortPwrCtrlMask, 0xff,
  214. sizeof(desc->u.hs.PortPwrCtrlMask));
  215. for (i = 0; i < (ports + 1 + 7) / 8; i++)
  216. memset(&desc->u.hs.DeviceRemovable[i], port_removable[i],
  217. sizeof(__u8));
  218. }
  219. /* Fill in the USB 3.0 roothub descriptor */
  220. static void xhci_usb3_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  221. struct usb_hub_descriptor *desc)
  222. {
  223. int ports;
  224. u16 port_removable;
  225. u32 portsc;
  226. unsigned int i;
  227. ports = xhci->num_usb3_ports;
  228. xhci_common_hub_descriptor(xhci, desc, ports);
  229. desc->bDescriptorType = USB_DT_SS_HUB;
  230. desc->bDescLength = USB_DT_SS_HUB_SIZE;
  231. /* header decode latency should be zero for roothubs,
  232. * see section 4.23.5.2.
  233. */
  234. desc->u.ss.bHubHdrDecLat = 0;
  235. desc->u.ss.wHubDelay = 0;
  236. port_removable = 0;
  237. /* bit 0 is reserved, bit 1 is for port 1, etc. */
  238. for (i = 0; i < ports; i++) {
  239. portsc = readl(xhci->usb3_ports[i]);
  240. if (portsc & PORT_DEV_REMOVE)
  241. port_removable |= 1 << (i + 1);
  242. }
  243. desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable);
  244. }
  245. static void xhci_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  246. struct usb_hub_descriptor *desc)
  247. {
  248. if (hcd->speed >= HCD_USB3)
  249. xhci_usb3_hub_descriptor(hcd, xhci, desc);
  250. else
  251. xhci_usb2_hub_descriptor(hcd, xhci, desc);
  252. }
  253. static unsigned int xhci_port_speed(unsigned int port_status)
  254. {
  255. if (DEV_LOWSPEED(port_status))
  256. return USB_PORT_STAT_LOW_SPEED;
  257. if (DEV_HIGHSPEED(port_status))
  258. return USB_PORT_STAT_HIGH_SPEED;
  259. /*
  260. * FIXME: Yes, we should check for full speed, but the core uses that as
  261. * a default in portspeed() in usb/core/hub.c (which is the only place
  262. * USB_PORT_STAT_*_SPEED is used).
  263. */
  264. return 0;
  265. }
  266. /*
  267. * These bits are Read Only (RO) and should be saved and written to the
  268. * registers: 0, 3, 10:13, 30
  269. * connect status, over-current status, port speed, and device removable.
  270. * connect status and port speed are also sticky - meaning they're in
  271. * the AUX well and they aren't changed by a hot, warm, or cold reset.
  272. */
  273. #define XHCI_PORT_RO ((1<<0) | (1<<3) | (0xf<<10) | (1<<30))
  274. /*
  275. * These bits are RW; writing a 0 clears the bit, writing a 1 sets the bit:
  276. * bits 5:8, 9, 14:15, 25:27
  277. * link state, port power, port indicator state, "wake on" enable state
  278. */
  279. #define XHCI_PORT_RWS ((0xf<<5) | (1<<9) | (0x3<<14) | (0x7<<25))
  280. /*
  281. * These bits are RW; writing a 1 sets the bit, writing a 0 has no effect:
  282. * bit 4 (port reset)
  283. */
  284. #define XHCI_PORT_RW1S ((1<<4))
  285. /*
  286. * These bits are RW; writing a 1 clears the bit, writing a 0 has no effect:
  287. * bits 1, 17, 18, 19, 20, 21, 22, 23
  288. * port enable/disable, and
  289. * change bits: connect, PED, warm port reset changed (reserved zero for USB 2.0 ports),
  290. * over-current, reset, link state, and L1 change
  291. */
  292. #define XHCI_PORT_RW1CS ((1<<1) | (0x7f<<17))
  293. /*
  294. * Bit 16 is RW, and writing a '1' to it causes the link state control to be
  295. * latched in
  296. */
  297. #define XHCI_PORT_RW ((1<<16))
  298. /*
  299. * These bits are Reserved Zero (RsvdZ) and zero should be written to them:
  300. * bits 2, 24, 28:31
  301. */
  302. #define XHCI_PORT_RZ ((1<<2) | (1<<24) | (0xf<<28))
  303. /*
  304. * Given a port state, this function returns a value that would result in the
  305. * port being in the same state, if the value was written to the port status
  306. * control register.
  307. * Save Read Only (RO) bits and save read/write bits where
  308. * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
  309. * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
  310. */
  311. u32 xhci_port_state_to_neutral(u32 state)
  312. {
  313. /* Save read-only status and port state */
  314. return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
  315. }
  316. /*
  317. * find slot id based on port number.
  318. * @port: The one-based port number from one of the two split roothubs.
  319. */
  320. int xhci_find_slot_id_by_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  321. u16 port)
  322. {
  323. int slot_id;
  324. int i;
  325. enum usb_device_speed speed;
  326. slot_id = 0;
  327. for (i = 0; i < MAX_HC_SLOTS; i++) {
  328. if (!xhci->devs[i])
  329. continue;
  330. speed = xhci->devs[i]->udev->speed;
  331. if (((speed >= USB_SPEED_SUPER) == (hcd->speed >= HCD_USB3))
  332. && xhci->devs[i]->fake_port == port) {
  333. slot_id = i;
  334. break;
  335. }
  336. }
  337. return slot_id;
  338. }
  339. /*
  340. * Stop device
  341. * It issues stop endpoint command for EP 0 to 30. And wait the last command
  342. * to complete.
  343. * suspend will set to 1, if suspend bit need to set in command.
  344. */
  345. static int xhci_stop_device(struct xhci_hcd *xhci, int slot_id, int suspend)
  346. {
  347. struct xhci_virt_device *virt_dev;
  348. struct xhci_command *cmd;
  349. unsigned long flags;
  350. int ret;
  351. int i;
  352. ret = 0;
  353. virt_dev = xhci->devs[slot_id];
  354. if (!virt_dev)
  355. return -ENODEV;
  356. cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
  357. if (!cmd) {
  358. xhci_dbg(xhci, "Couldn't allocate command structure.\n");
  359. return -ENOMEM;
  360. }
  361. spin_lock_irqsave(&xhci->lock, flags);
  362. for (i = LAST_EP_INDEX; i > 0; i--) {
  363. if (virt_dev->eps[i].ring && virt_dev->eps[i].ring->dequeue) {
  364. struct xhci_command *command;
  365. command = xhci_alloc_command(xhci, false, false,
  366. GFP_NOWAIT);
  367. if (!command) {
  368. spin_unlock_irqrestore(&xhci->lock, flags);
  369. ret = -ENOMEM;
  370. goto cmd_cleanup;
  371. }
  372. ret = xhci_queue_stop_endpoint(xhci, command, slot_id,
  373. i, suspend);
  374. if (ret) {
  375. spin_unlock_irqrestore(&xhci->lock, flags);
  376. xhci_free_command(xhci, command);
  377. goto cmd_cleanup;
  378. }
  379. }
  380. }
  381. ret = xhci_queue_stop_endpoint(xhci, cmd, slot_id, 0, suspend);
  382. if (ret) {
  383. spin_unlock_irqrestore(&xhci->lock, flags);
  384. goto cmd_cleanup;
  385. }
  386. xhci_ring_cmd_db(xhci);
  387. spin_unlock_irqrestore(&xhci->lock, flags);
  388. /* Wait for last stop endpoint command to finish */
  389. wait_for_completion(cmd->completion);
  390. if (cmd->status == COMP_CMD_ABORT || cmd->status == COMP_CMD_STOP) {
  391. xhci_warn(xhci, "Timeout while waiting for stop endpoint command\n");
  392. ret = -ETIME;
  393. }
  394. cmd_cleanup:
  395. xhci_free_command(xhci, cmd);
  396. return ret;
  397. }
  398. /*
  399. * Ring device, it rings the all doorbells unconditionally.
  400. */
  401. void xhci_ring_device(struct xhci_hcd *xhci, int slot_id)
  402. {
  403. int i, s;
  404. struct xhci_virt_ep *ep;
  405. for (i = 0; i < LAST_EP_INDEX + 1; i++) {
  406. ep = &xhci->devs[slot_id]->eps[i];
  407. if (ep->ep_state & EP_HAS_STREAMS) {
  408. for (s = 1; s < ep->stream_info->num_streams; s++)
  409. xhci_ring_ep_doorbell(xhci, slot_id, i, s);
  410. } else if (ep->ring && ep->ring->dequeue) {
  411. xhci_ring_ep_doorbell(xhci, slot_id, i, 0);
  412. }
  413. }
  414. return;
  415. }
  416. static void xhci_disable_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
  417. u16 wIndex, __le32 __iomem *addr, u32 port_status)
  418. {
  419. /* Don't allow the USB core to disable SuperSpeed ports. */
  420. if (hcd->speed >= HCD_USB3) {
  421. xhci_dbg(xhci, "Ignoring request to disable "
  422. "SuperSpeed port.\n");
  423. return;
  424. }
  425. if (xhci->quirks & XHCI_BROKEN_PORT_PED) {
  426. xhci_dbg(xhci,
  427. "Broken Port Enabled/Disabled, ignoring port disable request.\n");
  428. return;
  429. }
  430. /* Write 1 to disable the port */
  431. writel(port_status | PORT_PE, addr);
  432. port_status = readl(addr);
  433. xhci_dbg(xhci, "disable port, actual port %d status = 0x%x\n",
  434. wIndex, port_status);
  435. }
  436. static void xhci_clear_port_change_bit(struct xhci_hcd *xhci, u16 wValue,
  437. u16 wIndex, __le32 __iomem *addr, u32 port_status)
  438. {
  439. char *port_change_bit;
  440. u32 status;
  441. switch (wValue) {
  442. case USB_PORT_FEAT_C_RESET:
  443. status = PORT_RC;
  444. port_change_bit = "reset";
  445. break;
  446. case USB_PORT_FEAT_C_BH_PORT_RESET:
  447. status = PORT_WRC;
  448. port_change_bit = "warm(BH) reset";
  449. break;
  450. case USB_PORT_FEAT_C_CONNECTION:
  451. status = PORT_CSC;
  452. port_change_bit = "connect";
  453. break;
  454. case USB_PORT_FEAT_C_OVER_CURRENT:
  455. status = PORT_OCC;
  456. port_change_bit = "over-current";
  457. break;
  458. case USB_PORT_FEAT_C_ENABLE:
  459. status = PORT_PEC;
  460. port_change_bit = "enable/disable";
  461. break;
  462. case USB_PORT_FEAT_C_SUSPEND:
  463. status = PORT_PLC;
  464. port_change_bit = "suspend/resume";
  465. break;
  466. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  467. status = PORT_PLC;
  468. port_change_bit = "link state";
  469. break;
  470. case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
  471. status = PORT_CEC;
  472. port_change_bit = "config error";
  473. break;
  474. default:
  475. /* Should never happen */
  476. return;
  477. }
  478. /* Change bits are all write 1 to clear */
  479. writel(port_status | status, addr);
  480. port_status = readl(addr);
  481. xhci_dbg(xhci, "clear port %s change, actual port %d status = 0x%x\n",
  482. port_change_bit, wIndex, port_status);
  483. }
  484. static int xhci_get_ports(struct usb_hcd *hcd, __le32 __iomem ***port_array)
  485. {
  486. int max_ports;
  487. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  488. if (hcd->speed >= HCD_USB3) {
  489. max_ports = xhci->num_usb3_ports;
  490. *port_array = xhci->usb3_ports;
  491. } else {
  492. max_ports = xhci->num_usb2_ports;
  493. *port_array = xhci->usb2_ports;
  494. }
  495. return max_ports;
  496. }
  497. void xhci_set_link_state(struct xhci_hcd *xhci, __le32 __iomem **port_array,
  498. int port_id, u32 link_state)
  499. {
  500. u32 temp;
  501. temp = readl(port_array[port_id]);
  502. temp = xhci_port_state_to_neutral(temp);
  503. temp &= ~PORT_PLS_MASK;
  504. temp |= PORT_LINK_STROBE | link_state;
  505. writel(temp, port_array[port_id]);
  506. }
  507. static void xhci_set_remote_wake_mask(struct xhci_hcd *xhci,
  508. __le32 __iomem **port_array, int port_id, u16 wake_mask)
  509. {
  510. u32 temp;
  511. temp = readl(port_array[port_id]);
  512. temp = xhci_port_state_to_neutral(temp);
  513. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_CONNECT)
  514. temp |= PORT_WKCONN_E;
  515. else
  516. temp &= ~PORT_WKCONN_E;
  517. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT)
  518. temp |= PORT_WKDISC_E;
  519. else
  520. temp &= ~PORT_WKDISC_E;
  521. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT)
  522. temp |= PORT_WKOC_E;
  523. else
  524. temp &= ~PORT_WKOC_E;
  525. writel(temp, port_array[port_id]);
  526. }
  527. /* Test and clear port RWC bit */
  528. void xhci_test_and_clear_bit(struct xhci_hcd *xhci, __le32 __iomem **port_array,
  529. int port_id, u32 port_bit)
  530. {
  531. u32 temp;
  532. temp = readl(port_array[port_id]);
  533. if (temp & port_bit) {
  534. temp = xhci_port_state_to_neutral(temp);
  535. temp |= port_bit;
  536. writel(temp, port_array[port_id]);
  537. }
  538. }
  539. /* Updates Link Status for USB 2.1 port */
  540. static void xhci_hub_report_usb2_link_state(u32 *status, u32 status_reg)
  541. {
  542. if ((status_reg & PORT_PLS_MASK) == XDEV_U2)
  543. *status |= USB_PORT_STAT_L1;
  544. }
  545. /* Updates Link Status for super Speed port */
  546. static void xhci_hub_report_usb3_link_state(struct xhci_hcd *xhci,
  547. u32 *status, u32 status_reg)
  548. {
  549. u32 pls = status_reg & PORT_PLS_MASK;
  550. /* resume state is a xHCI internal state.
  551. * Do not report it to usb core, instead, pretend to be U3,
  552. * thus usb core knows it's not ready for transfer
  553. */
  554. if (pls == XDEV_RESUME) {
  555. *status |= USB_SS_PORT_LS_U3;
  556. return;
  557. }
  558. /* When the CAS bit is set then warm reset
  559. * should be performed on port
  560. */
  561. if (status_reg & PORT_CAS) {
  562. /* The CAS bit can be set while the port is
  563. * in any link state.
  564. * Only roothubs have CAS bit, so we
  565. * pretend to be in compliance mode
  566. * unless we're already in compliance
  567. * or the inactive state.
  568. */
  569. if (pls != USB_SS_PORT_LS_COMP_MOD &&
  570. pls != USB_SS_PORT_LS_SS_INACTIVE) {
  571. pls = USB_SS_PORT_LS_COMP_MOD;
  572. }
  573. /* Return also connection bit -
  574. * hub state machine resets port
  575. * when this bit is set.
  576. */
  577. pls |= USB_PORT_STAT_CONNECTION;
  578. } else {
  579. /*
  580. * If CAS bit isn't set but the Port is already at
  581. * Compliance Mode, fake a connection so the USB core
  582. * notices the Compliance state and resets the port.
  583. * This resolves an issue generated by the SN65LVPE502CP
  584. * in which sometimes the port enters compliance mode
  585. * caused by a delay on the host-device negotiation.
  586. */
  587. if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
  588. (pls == USB_SS_PORT_LS_COMP_MOD))
  589. pls |= USB_PORT_STAT_CONNECTION;
  590. }
  591. /* update status field */
  592. *status |= pls;
  593. }
  594. /*
  595. * Function for Compliance Mode Quirk.
  596. *
  597. * This Function verifies if all xhc USB3 ports have entered U0, if so,
  598. * the compliance mode timer is deleted. A port won't enter
  599. * compliance mode if it has previously entered U0.
  600. */
  601. static void xhci_del_comp_mod_timer(struct xhci_hcd *xhci, u32 status,
  602. u16 wIndex)
  603. {
  604. u32 all_ports_seen_u0 = ((1 << xhci->num_usb3_ports)-1);
  605. bool port_in_u0 = ((status & PORT_PLS_MASK) == XDEV_U0);
  606. if (!(xhci->quirks & XHCI_COMP_MODE_QUIRK))
  607. return;
  608. if ((xhci->port_status_u0 != all_ports_seen_u0) && port_in_u0) {
  609. xhci->port_status_u0 |= 1 << wIndex;
  610. if (xhci->port_status_u0 == all_ports_seen_u0) {
  611. del_timer_sync(&xhci->comp_mode_recovery_timer);
  612. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  613. "All USB3 ports have entered U0 already!");
  614. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  615. "Compliance Mode Recovery Timer Deleted.");
  616. }
  617. }
  618. }
  619. static u32 xhci_get_ext_port_status(u32 raw_port_status, u32 port_li)
  620. {
  621. u32 ext_stat = 0;
  622. int speed_id;
  623. /* only support rx and tx lane counts of 1 in usb3.1 spec */
  624. speed_id = DEV_PORT_SPEED(raw_port_status);
  625. ext_stat |= speed_id; /* bits 3:0, RX speed id */
  626. ext_stat |= speed_id << 4; /* bits 7:4, TX speed id */
  627. ext_stat |= PORT_RX_LANES(port_li) << 8; /* bits 11:8 Rx lane count */
  628. ext_stat |= PORT_TX_LANES(port_li) << 12; /* bits 15:12 Tx lane count */
  629. return ext_stat;
  630. }
  631. /*
  632. * Converts a raw xHCI port status into the format that external USB 2.0 or USB
  633. * 3.0 hubs use.
  634. *
  635. * Possible side effects:
  636. * - Mark a port as being done with device resume,
  637. * and ring the endpoint doorbells.
  638. * - Stop the Synopsys redriver Compliance Mode polling.
  639. * - Drop and reacquire the xHCI lock, in order to wait for port resume.
  640. */
  641. static u32 xhci_get_port_status(struct usb_hcd *hcd,
  642. struct xhci_bus_state *bus_state,
  643. __le32 __iomem **port_array,
  644. u16 wIndex, u32 raw_port_status,
  645. unsigned long flags)
  646. __releases(&xhci->lock)
  647. __acquires(&xhci->lock)
  648. {
  649. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  650. u32 status = 0;
  651. int slot_id;
  652. /* wPortChange bits */
  653. if (raw_port_status & PORT_CSC)
  654. status |= USB_PORT_STAT_C_CONNECTION << 16;
  655. if (raw_port_status & PORT_PEC)
  656. status |= USB_PORT_STAT_C_ENABLE << 16;
  657. if ((raw_port_status & PORT_OCC))
  658. status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  659. if ((raw_port_status & PORT_RC))
  660. status |= USB_PORT_STAT_C_RESET << 16;
  661. /* USB3.0 only */
  662. if (hcd->speed >= HCD_USB3) {
  663. /* Port link change with port in resume state should not be
  664. * reported to usbcore, as this is an internal state to be
  665. * handled by xhci driver. Reporting PLC to usbcore may
  666. * cause usbcore clearing PLC first and port change event
  667. * irq won't be generated.
  668. */
  669. if ((raw_port_status & PORT_PLC) &&
  670. (raw_port_status & PORT_PLS_MASK) != XDEV_RESUME)
  671. status |= USB_PORT_STAT_C_LINK_STATE << 16;
  672. if ((raw_port_status & PORT_WRC))
  673. status |= USB_PORT_STAT_C_BH_RESET << 16;
  674. if ((raw_port_status & PORT_CEC))
  675. status |= USB_PORT_STAT_C_CONFIG_ERROR << 16;
  676. }
  677. if (hcd->speed < HCD_USB3) {
  678. if ((raw_port_status & PORT_PLS_MASK) == XDEV_U3
  679. && (raw_port_status & PORT_POWER))
  680. status |= USB_PORT_STAT_SUSPEND;
  681. }
  682. if ((raw_port_status & PORT_PLS_MASK) == XDEV_RESUME &&
  683. !DEV_SUPERSPEED_ANY(raw_port_status)) {
  684. if ((raw_port_status & PORT_RESET) ||
  685. !(raw_port_status & PORT_PE))
  686. return 0xffffffff;
  687. /* did port event handler already start resume timing? */
  688. if (!bus_state->resume_done[wIndex]) {
  689. /* If not, maybe we are in a host initated resume? */
  690. if (test_bit(wIndex, &bus_state->resuming_ports)) {
  691. /* Host initated resume doesn't time the resume
  692. * signalling using resume_done[].
  693. * It manually sets RESUME state, sleeps 20ms
  694. * and sets U0 state. This should probably be
  695. * changed, but not right now.
  696. */
  697. } else {
  698. /* port resume was discovered now and here,
  699. * start resume timing
  700. */
  701. unsigned long timeout = jiffies +
  702. msecs_to_jiffies(USB_RESUME_TIMEOUT);
  703. set_bit(wIndex, &bus_state->resuming_ports);
  704. bus_state->resume_done[wIndex] = timeout;
  705. mod_timer(&hcd->rh_timer, timeout);
  706. }
  707. /* Has resume been signalled for USB_RESUME_TIME yet? */
  708. } else if (time_after_eq(jiffies,
  709. bus_state->resume_done[wIndex])) {
  710. int time_left;
  711. xhci_dbg(xhci, "Resume USB2 port %d\n",
  712. wIndex + 1);
  713. bus_state->resume_done[wIndex] = 0;
  714. clear_bit(wIndex, &bus_state->resuming_ports);
  715. set_bit(wIndex, &bus_state->rexit_ports);
  716. xhci_test_and_clear_bit(xhci, port_array, wIndex,
  717. PORT_PLC);
  718. xhci_set_link_state(xhci, port_array, wIndex,
  719. XDEV_U0);
  720. spin_unlock_irqrestore(&xhci->lock, flags);
  721. time_left = wait_for_completion_timeout(
  722. &bus_state->rexit_done[wIndex],
  723. msecs_to_jiffies(
  724. XHCI_MAX_REXIT_TIMEOUT));
  725. spin_lock_irqsave(&xhci->lock, flags);
  726. if (time_left) {
  727. slot_id = xhci_find_slot_id_by_port(hcd,
  728. xhci, wIndex + 1);
  729. if (!slot_id) {
  730. xhci_dbg(xhci, "slot_id is zero\n");
  731. return 0xffffffff;
  732. }
  733. xhci_ring_device(xhci, slot_id);
  734. } else {
  735. int port_status = readl(port_array[wIndex]);
  736. xhci_warn(xhci, "Port resume took longer than %i msec, port status = 0x%x\n",
  737. XHCI_MAX_REXIT_TIMEOUT,
  738. port_status);
  739. status |= USB_PORT_STAT_SUSPEND;
  740. clear_bit(wIndex, &bus_state->rexit_ports);
  741. }
  742. bus_state->port_c_suspend |= 1 << wIndex;
  743. bus_state->suspended_ports &= ~(1 << wIndex);
  744. } else {
  745. /*
  746. * The resume has been signaling for less than
  747. * USB_RESUME_TIME. Report the port status as SUSPEND,
  748. * let the usbcore check port status again and clear
  749. * resume signaling later.
  750. */
  751. status |= USB_PORT_STAT_SUSPEND;
  752. }
  753. }
  754. /*
  755. * Clear stale usb2 resume signalling variables in case port changed
  756. * state during resume signalling. For example on error
  757. */
  758. if ((bus_state->resume_done[wIndex] ||
  759. test_bit(wIndex, &bus_state->resuming_ports)) &&
  760. (raw_port_status & PORT_PLS_MASK) != XDEV_U3 &&
  761. (raw_port_status & PORT_PLS_MASK) != XDEV_RESUME) {
  762. bus_state->resume_done[wIndex] = 0;
  763. clear_bit(wIndex, &bus_state->resuming_ports);
  764. }
  765. if ((raw_port_status & PORT_PLS_MASK) == XDEV_U0 &&
  766. (raw_port_status & PORT_POWER)) {
  767. if (bus_state->suspended_ports & (1 << wIndex)) {
  768. bus_state->suspended_ports &= ~(1 << wIndex);
  769. if (hcd->speed < HCD_USB3)
  770. bus_state->port_c_suspend |= 1 << wIndex;
  771. }
  772. bus_state->resume_done[wIndex] = 0;
  773. clear_bit(wIndex, &bus_state->resuming_ports);
  774. }
  775. if (raw_port_status & PORT_CONNECT) {
  776. status |= USB_PORT_STAT_CONNECTION;
  777. status |= xhci_port_speed(raw_port_status);
  778. }
  779. if (raw_port_status & PORT_PE)
  780. status |= USB_PORT_STAT_ENABLE;
  781. if (raw_port_status & PORT_OC)
  782. status |= USB_PORT_STAT_OVERCURRENT;
  783. if (raw_port_status & PORT_RESET)
  784. status |= USB_PORT_STAT_RESET;
  785. if (raw_port_status & PORT_POWER) {
  786. if (hcd->speed >= HCD_USB3)
  787. status |= USB_SS_PORT_STAT_POWER;
  788. else
  789. status |= USB_PORT_STAT_POWER;
  790. }
  791. /* Update Port Link State */
  792. if (hcd->speed >= HCD_USB3) {
  793. xhci_hub_report_usb3_link_state(xhci, &status, raw_port_status);
  794. /*
  795. * Verify if all USB3 Ports Have entered U0 already.
  796. * Delete Compliance Mode Timer if so.
  797. */
  798. xhci_del_comp_mod_timer(xhci, raw_port_status, wIndex);
  799. } else {
  800. xhci_hub_report_usb2_link_state(&status, raw_port_status);
  801. }
  802. if (bus_state->port_c_suspend & (1 << wIndex))
  803. status |= USB_PORT_STAT_C_SUSPEND << 16;
  804. return status;
  805. }
  806. int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  807. u16 wIndex, char *buf, u16 wLength)
  808. {
  809. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  810. int max_ports;
  811. unsigned long flags;
  812. u32 temp, status;
  813. int retval = 0;
  814. __le32 __iomem **port_array;
  815. int slot_id;
  816. struct xhci_bus_state *bus_state;
  817. u16 link_state = 0;
  818. u16 wake_mask = 0;
  819. u16 timeout = 0;
  820. max_ports = xhci_get_ports(hcd, &port_array);
  821. bus_state = &xhci->bus_state[hcd_index(hcd)];
  822. spin_lock_irqsave(&xhci->lock, flags);
  823. switch (typeReq) {
  824. case GetHubStatus:
  825. /* No power source, over-current reported per port */
  826. memset(buf, 0, 4);
  827. break;
  828. case GetHubDescriptor:
  829. /* Check to make sure userspace is asking for the USB 3.0 hub
  830. * descriptor for the USB 3.0 roothub. If not, we stall the
  831. * endpoint, like external hubs do.
  832. */
  833. if (hcd->speed >= HCD_USB3 &&
  834. (wLength < USB_DT_SS_HUB_SIZE ||
  835. wValue != (USB_DT_SS_HUB << 8))) {
  836. xhci_dbg(xhci, "Wrong hub descriptor type for "
  837. "USB 3.0 roothub.\n");
  838. goto error;
  839. }
  840. xhci_hub_descriptor(hcd, xhci,
  841. (struct usb_hub_descriptor *) buf);
  842. break;
  843. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  844. if ((wValue & 0xff00) != (USB_DT_BOS << 8))
  845. goto error;
  846. if (hcd->speed < HCD_USB3)
  847. goto error;
  848. retval = xhci_create_usb3_bos_desc(xhci, buf, wLength);
  849. spin_unlock_irqrestore(&xhci->lock, flags);
  850. return retval;
  851. case GetPortStatus:
  852. if (!wIndex || wIndex > max_ports)
  853. goto error;
  854. wIndex--;
  855. temp = readl(port_array[wIndex]);
  856. if (temp == 0xffffffff) {
  857. retval = -ENODEV;
  858. break;
  859. }
  860. status = xhci_get_port_status(hcd, bus_state, port_array,
  861. wIndex, temp, flags);
  862. if (status == 0xffffffff)
  863. goto error;
  864. xhci_dbg(xhci, "get port status, actual port %d status = 0x%x\n",
  865. wIndex, temp);
  866. xhci_dbg(xhci, "Get port status returned 0x%x\n", status);
  867. put_unaligned(cpu_to_le32(status), (__le32 *) buf);
  868. /* if USB 3.1 extended port status return additional 4 bytes */
  869. if (wValue == 0x02) {
  870. u32 port_li;
  871. if (hcd->speed < HCD_USB31 || wLength != 8) {
  872. xhci_err(xhci, "get ext port status invalid parameter\n");
  873. retval = -EINVAL;
  874. break;
  875. }
  876. port_li = readl(port_array[wIndex] + PORTLI);
  877. status = xhci_get_ext_port_status(temp, port_li);
  878. put_unaligned_le32(cpu_to_le32(status), &buf[4]);
  879. }
  880. break;
  881. case SetPortFeature:
  882. if (wValue == USB_PORT_FEAT_LINK_STATE)
  883. link_state = (wIndex & 0xff00) >> 3;
  884. if (wValue == USB_PORT_FEAT_REMOTE_WAKE_MASK)
  885. wake_mask = wIndex & 0xff00;
  886. /* The MSB of wIndex is the U1/U2 timeout */
  887. timeout = (wIndex & 0xff00) >> 8;
  888. wIndex &= 0xff;
  889. if (!wIndex || wIndex > max_ports)
  890. goto error;
  891. wIndex--;
  892. temp = readl(port_array[wIndex]);
  893. if (temp == 0xffffffff) {
  894. retval = -ENODEV;
  895. break;
  896. }
  897. temp = xhci_port_state_to_neutral(temp);
  898. /* FIXME: What new port features do we need to support? */
  899. switch (wValue) {
  900. case USB_PORT_FEAT_SUSPEND:
  901. temp = readl(port_array[wIndex]);
  902. if ((temp & PORT_PLS_MASK) != XDEV_U0) {
  903. /* Resume the port to U0 first */
  904. xhci_set_link_state(xhci, port_array, wIndex,
  905. XDEV_U0);
  906. spin_unlock_irqrestore(&xhci->lock, flags);
  907. msleep(10);
  908. spin_lock_irqsave(&xhci->lock, flags);
  909. }
  910. /* In spec software should not attempt to suspend
  911. * a port unless the port reports that it is in the
  912. * enabled (PED = ‘1’,PLS < ‘3’) state.
  913. */
  914. temp = readl(port_array[wIndex]);
  915. if ((temp & PORT_PE) == 0 || (temp & PORT_RESET)
  916. || (temp & PORT_PLS_MASK) >= XDEV_U3) {
  917. xhci_warn(xhci, "USB core suspending device "
  918. "not in U0/U1/U2.\n");
  919. goto error;
  920. }
  921. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  922. wIndex + 1);
  923. if (!slot_id) {
  924. xhci_warn(xhci, "slot_id is zero\n");
  925. goto error;
  926. }
  927. /* unlock to execute stop endpoint commands */
  928. spin_unlock_irqrestore(&xhci->lock, flags);
  929. xhci_stop_device(xhci, slot_id, 1);
  930. spin_lock_irqsave(&xhci->lock, flags);
  931. xhci_set_link_state(xhci, port_array, wIndex, XDEV_U3);
  932. spin_unlock_irqrestore(&xhci->lock, flags);
  933. msleep(10); /* wait device to enter */
  934. spin_lock_irqsave(&xhci->lock, flags);
  935. temp = readl(port_array[wIndex]);
  936. bus_state->suspended_ports |= 1 << wIndex;
  937. break;
  938. case USB_PORT_FEAT_LINK_STATE:
  939. temp = readl(port_array[wIndex]);
  940. /* Disable port */
  941. if (link_state == USB_SS_PORT_LS_SS_DISABLED) {
  942. xhci_dbg(xhci, "Disable port %d\n", wIndex);
  943. temp = xhci_port_state_to_neutral(temp);
  944. /*
  945. * Clear all change bits, so that we get a new
  946. * connection event.
  947. */
  948. temp |= PORT_CSC | PORT_PEC | PORT_WRC |
  949. PORT_OCC | PORT_RC | PORT_PLC |
  950. PORT_CEC;
  951. writel(temp | PORT_PE, port_array[wIndex]);
  952. temp = readl(port_array[wIndex]);
  953. break;
  954. }
  955. /* Put link in RxDetect (enable port) */
  956. if (link_state == USB_SS_PORT_LS_RX_DETECT) {
  957. xhci_dbg(xhci, "Enable port %d\n", wIndex);
  958. xhci_set_link_state(xhci, port_array, wIndex,
  959. link_state);
  960. temp = readl(port_array[wIndex]);
  961. break;
  962. }
  963. /* Software should not attempt to set
  964. * port link state above '3' (U3) and the port
  965. * must be enabled.
  966. */
  967. if ((temp & PORT_PE) == 0 ||
  968. (link_state > USB_SS_PORT_LS_U3)) {
  969. xhci_warn(xhci, "Cannot set link state.\n");
  970. goto error;
  971. }
  972. if (link_state == USB_SS_PORT_LS_U3) {
  973. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  974. wIndex + 1);
  975. if (slot_id) {
  976. /* unlock to execute stop endpoint
  977. * commands */
  978. spin_unlock_irqrestore(&xhci->lock,
  979. flags);
  980. xhci_stop_device(xhci, slot_id, 1);
  981. spin_lock_irqsave(&xhci->lock, flags);
  982. }
  983. }
  984. xhci_set_link_state(xhci, port_array, wIndex,
  985. link_state);
  986. spin_unlock_irqrestore(&xhci->lock, flags);
  987. msleep(20); /* wait device to enter */
  988. spin_lock_irqsave(&xhci->lock, flags);
  989. temp = readl(port_array[wIndex]);
  990. if (link_state == USB_SS_PORT_LS_U3)
  991. bus_state->suspended_ports |= 1 << wIndex;
  992. break;
  993. case USB_PORT_FEAT_POWER:
  994. /*
  995. * Turn on ports, even if there isn't per-port switching.
  996. * HC will report connect events even before this is set.
  997. * However, hub_wq will ignore the roothub events until
  998. * the roothub is registered.
  999. */
  1000. writel(temp | PORT_POWER, port_array[wIndex]);
  1001. temp = readl(port_array[wIndex]);
  1002. xhci_dbg(xhci, "set port power, actual port %d status = 0x%x\n", wIndex, temp);
  1003. spin_unlock_irqrestore(&xhci->lock, flags);
  1004. temp = usb_acpi_power_manageable(hcd->self.root_hub,
  1005. wIndex);
  1006. if (temp)
  1007. usb_acpi_set_power_state(hcd->self.root_hub,
  1008. wIndex, true);
  1009. spin_lock_irqsave(&xhci->lock, flags);
  1010. break;
  1011. case USB_PORT_FEAT_RESET:
  1012. temp = (temp | PORT_RESET);
  1013. writel(temp, port_array[wIndex]);
  1014. temp = readl(port_array[wIndex]);
  1015. xhci_dbg(xhci, "set port reset, actual port %d status = 0x%x\n", wIndex, temp);
  1016. break;
  1017. case USB_PORT_FEAT_REMOTE_WAKE_MASK:
  1018. xhci_set_remote_wake_mask(xhci, port_array,
  1019. wIndex, wake_mask);
  1020. temp = readl(port_array[wIndex]);
  1021. xhci_dbg(xhci, "set port remote wake mask, "
  1022. "actual port %d status = 0x%x\n",
  1023. wIndex, temp);
  1024. break;
  1025. case USB_PORT_FEAT_BH_PORT_RESET:
  1026. temp |= PORT_WR;
  1027. writel(temp, port_array[wIndex]);
  1028. temp = readl(port_array[wIndex]);
  1029. break;
  1030. case USB_PORT_FEAT_U1_TIMEOUT:
  1031. if (hcd->speed < HCD_USB3)
  1032. goto error;
  1033. temp = readl(port_array[wIndex] + PORTPMSC);
  1034. temp &= ~PORT_U1_TIMEOUT_MASK;
  1035. temp |= PORT_U1_TIMEOUT(timeout);
  1036. writel(temp, port_array[wIndex] + PORTPMSC);
  1037. break;
  1038. case USB_PORT_FEAT_U2_TIMEOUT:
  1039. if (hcd->speed < HCD_USB3)
  1040. goto error;
  1041. temp = readl(port_array[wIndex] + PORTPMSC);
  1042. temp &= ~PORT_U2_TIMEOUT_MASK;
  1043. temp |= PORT_U2_TIMEOUT(timeout);
  1044. writel(temp, port_array[wIndex] + PORTPMSC);
  1045. break;
  1046. default:
  1047. goto error;
  1048. }
  1049. /* unblock any posted writes */
  1050. temp = readl(port_array[wIndex]);
  1051. break;
  1052. case ClearPortFeature:
  1053. if (!wIndex || wIndex > max_ports)
  1054. goto error;
  1055. wIndex--;
  1056. temp = readl(port_array[wIndex]);
  1057. if (temp == 0xffffffff) {
  1058. retval = -ENODEV;
  1059. break;
  1060. }
  1061. /* FIXME: What new port features do we need to support? */
  1062. temp = xhci_port_state_to_neutral(temp);
  1063. switch (wValue) {
  1064. case USB_PORT_FEAT_SUSPEND:
  1065. temp = readl(port_array[wIndex]);
  1066. xhci_dbg(xhci, "clear USB_PORT_FEAT_SUSPEND\n");
  1067. xhci_dbg(xhci, "PORTSC %04x\n", temp);
  1068. if (temp & PORT_RESET)
  1069. goto error;
  1070. if ((temp & PORT_PLS_MASK) == XDEV_U3) {
  1071. if ((temp & PORT_PE) == 0)
  1072. goto error;
  1073. set_bit(wIndex, &bus_state->resuming_ports);
  1074. xhci_set_link_state(xhci, port_array, wIndex,
  1075. XDEV_RESUME);
  1076. spin_unlock_irqrestore(&xhci->lock, flags);
  1077. msleep(USB_RESUME_TIMEOUT);
  1078. spin_lock_irqsave(&xhci->lock, flags);
  1079. xhci_set_link_state(xhci, port_array, wIndex,
  1080. XDEV_U0);
  1081. clear_bit(wIndex, &bus_state->resuming_ports);
  1082. }
  1083. bus_state->port_c_suspend |= 1 << wIndex;
  1084. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1085. wIndex + 1);
  1086. if (!slot_id) {
  1087. xhci_dbg(xhci, "slot_id is zero\n");
  1088. goto error;
  1089. }
  1090. xhci_ring_device(xhci, slot_id);
  1091. break;
  1092. case USB_PORT_FEAT_C_SUSPEND:
  1093. bus_state->port_c_suspend &= ~(1 << wIndex);
  1094. case USB_PORT_FEAT_C_RESET:
  1095. case USB_PORT_FEAT_C_BH_PORT_RESET:
  1096. case USB_PORT_FEAT_C_CONNECTION:
  1097. case USB_PORT_FEAT_C_OVER_CURRENT:
  1098. case USB_PORT_FEAT_C_ENABLE:
  1099. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  1100. case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
  1101. xhci_clear_port_change_bit(xhci, wValue, wIndex,
  1102. port_array[wIndex], temp);
  1103. break;
  1104. case USB_PORT_FEAT_ENABLE:
  1105. xhci_disable_port(hcd, xhci, wIndex,
  1106. port_array[wIndex], temp);
  1107. break;
  1108. case USB_PORT_FEAT_POWER:
  1109. writel(temp & ~PORT_POWER, port_array[wIndex]);
  1110. spin_unlock_irqrestore(&xhci->lock, flags);
  1111. temp = usb_acpi_power_manageable(hcd->self.root_hub,
  1112. wIndex);
  1113. if (temp)
  1114. usb_acpi_set_power_state(hcd->self.root_hub,
  1115. wIndex, false);
  1116. spin_lock_irqsave(&xhci->lock, flags);
  1117. break;
  1118. default:
  1119. goto error;
  1120. }
  1121. break;
  1122. default:
  1123. error:
  1124. /* "stall" on error */
  1125. retval = -EPIPE;
  1126. }
  1127. spin_unlock_irqrestore(&xhci->lock, flags);
  1128. return retval;
  1129. }
  1130. /*
  1131. * Returns 0 if the status hasn't changed, or the number of bytes in buf.
  1132. * Ports are 0-indexed from the HCD point of view,
  1133. * and 1-indexed from the USB core pointer of view.
  1134. *
  1135. * Note that the status change bits will be cleared as soon as a port status
  1136. * change event is generated, so we use the saved status from that event.
  1137. */
  1138. int xhci_hub_status_data(struct usb_hcd *hcd, char *buf)
  1139. {
  1140. unsigned long flags;
  1141. u32 temp, status;
  1142. u32 mask;
  1143. int i, retval;
  1144. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1145. int max_ports;
  1146. __le32 __iomem **port_array;
  1147. struct xhci_bus_state *bus_state;
  1148. bool reset_change = false;
  1149. max_ports = xhci_get_ports(hcd, &port_array);
  1150. bus_state = &xhci->bus_state[hcd_index(hcd)];
  1151. /* Initial status is no changes */
  1152. retval = (max_ports + 8) / 8;
  1153. memset(buf, 0, retval);
  1154. /*
  1155. * Inform the usbcore about resume-in-progress by returning
  1156. * a non-zero value even if there are no status changes.
  1157. */
  1158. status = bus_state->resuming_ports;
  1159. mask = PORT_CSC | PORT_PEC | PORT_OCC | PORT_PLC | PORT_WRC | PORT_CEC;
  1160. spin_lock_irqsave(&xhci->lock, flags);
  1161. /* For each port, did anything change? If so, set that bit in buf. */
  1162. for (i = 0; i < max_ports; i++) {
  1163. temp = readl(port_array[i]);
  1164. if (temp == 0xffffffff) {
  1165. retval = -ENODEV;
  1166. break;
  1167. }
  1168. if ((temp & mask) != 0 ||
  1169. (bus_state->port_c_suspend & 1 << i) ||
  1170. (bus_state->resume_done[i] && time_after_eq(
  1171. jiffies, bus_state->resume_done[i]))) {
  1172. buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
  1173. status = 1;
  1174. }
  1175. if ((temp & PORT_RC))
  1176. reset_change = true;
  1177. }
  1178. if (!status && !reset_change) {
  1179. xhci_dbg(xhci, "%s: stopping port polling.\n", __func__);
  1180. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  1181. }
  1182. spin_unlock_irqrestore(&xhci->lock, flags);
  1183. return status ? retval : 0;
  1184. }
  1185. #ifdef CONFIG_PM
  1186. int xhci_bus_suspend(struct usb_hcd *hcd)
  1187. {
  1188. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1189. int max_ports, port_index;
  1190. __le32 __iomem **port_array;
  1191. struct xhci_bus_state *bus_state;
  1192. unsigned long flags;
  1193. max_ports = xhci_get_ports(hcd, &port_array);
  1194. bus_state = &xhci->bus_state[hcd_index(hcd)];
  1195. spin_lock_irqsave(&xhci->lock, flags);
  1196. if (hcd->self.root_hub->do_remote_wakeup) {
  1197. if (bus_state->resuming_ports || /* USB2 */
  1198. bus_state->port_remote_wakeup) { /* USB3 */
  1199. spin_unlock_irqrestore(&xhci->lock, flags);
  1200. xhci_dbg(xhci, "suspend failed because a port is resuming\n");
  1201. return -EBUSY;
  1202. }
  1203. }
  1204. port_index = max_ports;
  1205. bus_state->bus_suspended = 0;
  1206. while (port_index--) {
  1207. /* suspend the port if the port is not suspended */
  1208. u32 t1, t2;
  1209. int slot_id;
  1210. t1 = readl(port_array[port_index]);
  1211. t2 = xhci_port_state_to_neutral(t1);
  1212. if ((t1 & PORT_PE) && !(t1 & PORT_PLS_MASK)) {
  1213. xhci_dbg(xhci, "port %d not suspended\n", port_index);
  1214. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1215. port_index + 1);
  1216. if (slot_id) {
  1217. spin_unlock_irqrestore(&xhci->lock, flags);
  1218. xhci_stop_device(xhci, slot_id, 1);
  1219. spin_lock_irqsave(&xhci->lock, flags);
  1220. }
  1221. t2 &= ~PORT_PLS_MASK;
  1222. t2 |= PORT_LINK_STROBE | XDEV_U3;
  1223. set_bit(port_index, &bus_state->bus_suspended);
  1224. }
  1225. /* USB core sets remote wake mask for USB 3.0 hubs,
  1226. * including the USB 3.0 roothub, but only if CONFIG_PM
  1227. * is enabled, so also enable remote wake here.
  1228. */
  1229. if (hcd->self.root_hub->do_remote_wakeup) {
  1230. if (t1 & PORT_CONNECT) {
  1231. t2 |= PORT_WKOC_E | PORT_WKDISC_E;
  1232. t2 &= ~PORT_WKCONN_E;
  1233. } else {
  1234. t2 |= PORT_WKOC_E | PORT_WKCONN_E;
  1235. t2 &= ~PORT_WKDISC_E;
  1236. }
  1237. } else
  1238. t2 &= ~PORT_WAKE_BITS;
  1239. t1 = xhci_port_state_to_neutral(t1);
  1240. if (t1 != t2)
  1241. writel(t2, port_array[port_index]);
  1242. }
  1243. hcd->state = HC_STATE_SUSPENDED;
  1244. bus_state->next_statechange = jiffies + msecs_to_jiffies(10);
  1245. spin_unlock_irqrestore(&xhci->lock, flags);
  1246. return 0;
  1247. }
  1248. /*
  1249. * Workaround for missing Cold Attach Status (CAS) if device re-plugged in S3.
  1250. * warm reset a USB3 device stuck in polling or compliance mode after resume.
  1251. * See Intel 100/c230 series PCH specification update Doc #332692-006 Errata #8
  1252. */
  1253. static bool xhci_port_missing_cas_quirk(int port_index,
  1254. __le32 __iomem **port_array)
  1255. {
  1256. u32 portsc;
  1257. portsc = readl(port_array[port_index]);
  1258. /* if any of these are set we are not stuck */
  1259. if (portsc & (PORT_CONNECT | PORT_CAS))
  1260. return false;
  1261. if (((portsc & PORT_PLS_MASK) != XDEV_POLLING) &&
  1262. ((portsc & PORT_PLS_MASK) != XDEV_COMP_MODE))
  1263. return false;
  1264. /* clear wakeup/change bits, and do a warm port reset */
  1265. portsc &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
  1266. portsc |= PORT_WR;
  1267. writel(portsc, port_array[port_index]);
  1268. /* flush write */
  1269. readl(port_array[port_index]);
  1270. return true;
  1271. }
  1272. int xhci_bus_resume(struct usb_hcd *hcd)
  1273. {
  1274. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1275. int max_ports, port_index;
  1276. __le32 __iomem **port_array;
  1277. struct xhci_bus_state *bus_state;
  1278. u32 temp;
  1279. unsigned long flags;
  1280. unsigned long port_was_suspended = 0;
  1281. bool need_usb2_u3_exit = false;
  1282. int slot_id;
  1283. int sret;
  1284. max_ports = xhci_get_ports(hcd, &port_array);
  1285. bus_state = &xhci->bus_state[hcd_index(hcd)];
  1286. if (time_before(jiffies, bus_state->next_statechange))
  1287. msleep(5);
  1288. spin_lock_irqsave(&xhci->lock, flags);
  1289. if (!HCD_HW_ACCESSIBLE(hcd)) {
  1290. spin_unlock_irqrestore(&xhci->lock, flags);
  1291. return -ESHUTDOWN;
  1292. }
  1293. /* delay the irqs */
  1294. temp = readl(&xhci->op_regs->command);
  1295. temp &= ~CMD_EIE;
  1296. writel(temp, &xhci->op_regs->command);
  1297. port_index = max_ports;
  1298. while (port_index--) {
  1299. /* Check whether need resume ports. If needed
  1300. resume port and disable remote wakeup */
  1301. u32 temp;
  1302. temp = readl(port_array[port_index]);
  1303. /* warm reset CAS limited ports stuck in polling/compliance */
  1304. if ((xhci->quirks & XHCI_MISSING_CAS) &&
  1305. (hcd->speed >= HCD_USB3) &&
  1306. xhci_port_missing_cas_quirk(port_index, port_array)) {
  1307. xhci_dbg(xhci, "reset stuck port %d\n", port_index);
  1308. continue;
  1309. }
  1310. if (DEV_SUPERSPEED_ANY(temp))
  1311. temp &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
  1312. else
  1313. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  1314. if (test_bit(port_index, &bus_state->bus_suspended) &&
  1315. (temp & PORT_PLS_MASK)) {
  1316. set_bit(port_index, &port_was_suspended);
  1317. if (!DEV_SUPERSPEED_ANY(temp)) {
  1318. xhci_set_link_state(xhci, port_array,
  1319. port_index, XDEV_RESUME);
  1320. need_usb2_u3_exit = true;
  1321. }
  1322. } else
  1323. writel(temp, port_array[port_index]);
  1324. }
  1325. if (need_usb2_u3_exit) {
  1326. spin_unlock_irqrestore(&xhci->lock, flags);
  1327. msleep(USB_RESUME_TIMEOUT);
  1328. spin_lock_irqsave(&xhci->lock, flags);
  1329. }
  1330. port_index = max_ports;
  1331. while (port_index--) {
  1332. if (!(port_was_suspended & BIT(port_index)))
  1333. continue;
  1334. /* Clear PLC to poll it later after XDEV_U0 */
  1335. xhci_test_and_clear_bit(xhci, port_array, port_index, PORT_PLC);
  1336. xhci_set_link_state(xhci, port_array, port_index, XDEV_U0);
  1337. }
  1338. port_index = max_ports;
  1339. while (port_index--) {
  1340. if (!(port_was_suspended & BIT(port_index)))
  1341. continue;
  1342. /* Poll and Clear PLC */
  1343. sret = xhci_handshake(port_array[port_index], PORT_PLC,
  1344. PORT_PLC, 10 * 1000);
  1345. if (sret)
  1346. xhci_warn(xhci, "port %d resume PLC timeout\n",
  1347. port_index);
  1348. xhci_test_and_clear_bit(xhci, port_array, port_index, PORT_PLC);
  1349. slot_id = xhci_find_slot_id_by_port(hcd, xhci, port_index + 1);
  1350. if (slot_id)
  1351. xhci_ring_device(xhci, slot_id);
  1352. }
  1353. (void) readl(&xhci->op_regs->command);
  1354. bus_state->next_statechange = jiffies + msecs_to_jiffies(5);
  1355. /* re-enable irqs */
  1356. temp = readl(&xhci->op_regs->command);
  1357. temp |= CMD_EIE;
  1358. writel(temp, &xhci->op_regs->command);
  1359. temp = readl(&xhci->op_regs->command);
  1360. spin_unlock_irqrestore(&xhci->lock, flags);
  1361. return 0;
  1362. }
  1363. #endif /* CONFIG_PM */