xhci-hub.c 43 KB

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