xhci-hub.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611
  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. trace_xhci_stop_device(virt_dev);
  350. cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
  351. if (!cmd)
  352. return -ENOMEM;
  353. spin_lock_irqsave(&xhci->lock, flags);
  354. for (i = LAST_EP_INDEX; i > 0; i--) {
  355. if (virt_dev->eps[i].ring && virt_dev->eps[i].ring->dequeue) {
  356. struct xhci_command *command;
  357. command = xhci_alloc_command(xhci, false, false,
  358. GFP_NOWAIT);
  359. if (!command) {
  360. spin_unlock_irqrestore(&xhci->lock, flags);
  361. xhci_free_command(xhci, cmd);
  362. return -ENOMEM;
  363. }
  364. xhci_queue_stop_endpoint(xhci, command, slot_id, i,
  365. suspend);
  366. }
  367. }
  368. xhci_queue_stop_endpoint(xhci, cmd, slot_id, 0, suspend);
  369. xhci_ring_cmd_db(xhci);
  370. spin_unlock_irqrestore(&xhci->lock, flags);
  371. /* Wait for last stop endpoint command to finish */
  372. wait_for_completion(cmd->completion);
  373. if (cmd->status == COMP_COMMAND_ABORTED ||
  374. cmd->status == COMP_COMMAND_RING_STOPPED) {
  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. if (xhci->quirks & XHCI_BROKEN_PORT_PED) {
  409. xhci_dbg(xhci,
  410. "Broken Port Enabled/Disabled, ignoring port disable request.\n");
  411. return;
  412. }
  413. /* Write 1 to disable the port */
  414. writel(port_status | PORT_PE, addr);
  415. port_status = readl(addr);
  416. xhci_dbg(xhci, "disable port, actual port %d status = 0x%x\n",
  417. wIndex, port_status);
  418. }
  419. static void xhci_clear_port_change_bit(struct xhci_hcd *xhci, u16 wValue,
  420. u16 wIndex, __le32 __iomem *addr, u32 port_status)
  421. {
  422. char *port_change_bit;
  423. u32 status;
  424. switch (wValue) {
  425. case USB_PORT_FEAT_C_RESET:
  426. status = PORT_RC;
  427. port_change_bit = "reset";
  428. break;
  429. case USB_PORT_FEAT_C_BH_PORT_RESET:
  430. status = PORT_WRC;
  431. port_change_bit = "warm(BH) reset";
  432. break;
  433. case USB_PORT_FEAT_C_CONNECTION:
  434. status = PORT_CSC;
  435. port_change_bit = "connect";
  436. break;
  437. case USB_PORT_FEAT_C_OVER_CURRENT:
  438. status = PORT_OCC;
  439. port_change_bit = "over-current";
  440. break;
  441. case USB_PORT_FEAT_C_ENABLE:
  442. status = PORT_PEC;
  443. port_change_bit = "enable/disable";
  444. break;
  445. case USB_PORT_FEAT_C_SUSPEND:
  446. status = PORT_PLC;
  447. port_change_bit = "suspend/resume";
  448. break;
  449. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  450. status = PORT_PLC;
  451. port_change_bit = "link state";
  452. break;
  453. case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
  454. status = PORT_CEC;
  455. port_change_bit = "config error";
  456. break;
  457. default:
  458. /* Should never happen */
  459. return;
  460. }
  461. /* Change bits are all write 1 to clear */
  462. writel(port_status | status, addr);
  463. port_status = readl(addr);
  464. xhci_dbg(xhci, "clear port %s change, actual port %d status = 0x%x\n",
  465. port_change_bit, wIndex, port_status);
  466. }
  467. static int xhci_get_ports(struct usb_hcd *hcd, __le32 __iomem ***port_array)
  468. {
  469. int max_ports;
  470. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  471. if (hcd->speed >= HCD_USB3) {
  472. max_ports = xhci->num_usb3_ports;
  473. *port_array = xhci->usb3_ports;
  474. } else {
  475. max_ports = xhci->num_usb2_ports;
  476. *port_array = xhci->usb2_ports;
  477. }
  478. return max_ports;
  479. }
  480. static __le32 __iomem *xhci_get_port_io_addr(struct usb_hcd *hcd, int index)
  481. {
  482. __le32 __iomem **port_array;
  483. xhci_get_ports(hcd, &port_array);
  484. return port_array[index];
  485. }
  486. /*
  487. * xhci_set_port_power() must be called with xhci->lock held.
  488. * It will release and re-aquire the lock while calling ACPI
  489. * method.
  490. */
  491. static void xhci_set_port_power(struct xhci_hcd *xhci, struct usb_hcd *hcd,
  492. u16 index, bool on, unsigned long *flags)
  493. {
  494. __le32 __iomem *addr;
  495. u32 temp;
  496. addr = xhci_get_port_io_addr(hcd, index);
  497. temp = readl(addr);
  498. temp = xhci_port_state_to_neutral(temp);
  499. if (on) {
  500. /* Power on */
  501. writel(temp | PORT_POWER, addr);
  502. temp = readl(addr);
  503. xhci_dbg(xhci, "set port power, actual port %d status = 0x%x\n",
  504. index, temp);
  505. } else {
  506. /* Power off */
  507. writel(temp & ~PORT_POWER, addr);
  508. }
  509. spin_unlock_irqrestore(&xhci->lock, *flags);
  510. temp = usb_acpi_power_manageable(hcd->self.root_hub,
  511. index);
  512. if (temp)
  513. usb_acpi_set_power_state(hcd->self.root_hub,
  514. index, on);
  515. spin_lock_irqsave(&xhci->lock, *flags);
  516. }
  517. static void xhci_port_set_test_mode(struct xhci_hcd *xhci,
  518. u16 test_mode, u16 wIndex)
  519. {
  520. u32 temp;
  521. __le32 __iomem *addr;
  522. /* xhci only supports test mode for usb2 ports, i.e. xhci->main_hcd */
  523. addr = xhci_get_port_io_addr(xhci->main_hcd, wIndex);
  524. temp = readl(addr + PORTPMSC);
  525. temp |= test_mode << PORT_TEST_MODE_SHIFT;
  526. writel(temp, addr + PORTPMSC);
  527. xhci->test_mode = test_mode;
  528. if (test_mode == TEST_FORCE_EN)
  529. xhci_start(xhci);
  530. }
  531. static int xhci_enter_test_mode(struct xhci_hcd *xhci,
  532. u16 test_mode, u16 wIndex, unsigned long *flags)
  533. {
  534. int i, retval;
  535. /* Disable all Device Slots */
  536. xhci_dbg(xhci, "Disable all slots\n");
  537. for (i = 1; i <= HCS_MAX_SLOTS(xhci->hcs_params1); i++) {
  538. retval = xhci_disable_slot(xhci, NULL, i);
  539. if (retval)
  540. xhci_err(xhci, "Failed to disable slot %d, %d. Enter test mode anyway\n",
  541. i, retval);
  542. }
  543. /* Put all ports to the Disable state by clear PP */
  544. xhci_dbg(xhci, "Disable all port (PP = 0)\n");
  545. /* Power off USB3 ports*/
  546. for (i = 0; i < xhci->num_usb3_ports; i++)
  547. xhci_set_port_power(xhci, xhci->shared_hcd, i, false, flags);
  548. /* Power off USB2 ports*/
  549. for (i = 0; i < xhci->num_usb2_ports; i++)
  550. xhci_set_port_power(xhci, xhci->main_hcd, i, false, flags);
  551. /* Stop the controller */
  552. xhci_dbg(xhci, "Stop controller\n");
  553. retval = xhci_halt(xhci);
  554. if (retval)
  555. return retval;
  556. /* Disable runtime PM for test mode */
  557. pm_runtime_forbid(xhci_to_hcd(xhci)->self.controller);
  558. /* Set PORTPMSC.PTC field to enter selected test mode */
  559. /* Port is selected by wIndex. port_id = wIndex + 1 */
  560. xhci_dbg(xhci, "Enter Test Mode: %d, Port_id=%d\n",
  561. test_mode, wIndex + 1);
  562. xhci_port_set_test_mode(xhci, test_mode, wIndex);
  563. return retval;
  564. }
  565. static int xhci_exit_test_mode(struct xhci_hcd *xhci)
  566. {
  567. int retval;
  568. if (!xhci->test_mode) {
  569. xhci_err(xhci, "Not in test mode, do nothing.\n");
  570. return 0;
  571. }
  572. if (xhci->test_mode == TEST_FORCE_EN &&
  573. !(xhci->xhc_state & XHCI_STATE_HALTED)) {
  574. retval = xhci_halt(xhci);
  575. if (retval)
  576. return retval;
  577. }
  578. pm_runtime_allow(xhci_to_hcd(xhci)->self.controller);
  579. xhci->test_mode = 0;
  580. return xhci_reset(xhci);
  581. }
  582. void xhci_set_link_state(struct xhci_hcd *xhci, __le32 __iomem **port_array,
  583. int port_id, u32 link_state)
  584. {
  585. u32 temp;
  586. temp = readl(port_array[port_id]);
  587. temp = xhci_port_state_to_neutral(temp);
  588. temp &= ~PORT_PLS_MASK;
  589. temp |= PORT_LINK_STROBE | link_state;
  590. writel(temp, port_array[port_id]);
  591. }
  592. static void xhci_set_remote_wake_mask(struct xhci_hcd *xhci,
  593. __le32 __iomem **port_array, int port_id, u16 wake_mask)
  594. {
  595. u32 temp;
  596. temp = readl(port_array[port_id]);
  597. temp = xhci_port_state_to_neutral(temp);
  598. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_CONNECT)
  599. temp |= PORT_WKCONN_E;
  600. else
  601. temp &= ~PORT_WKCONN_E;
  602. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT)
  603. temp |= PORT_WKDISC_E;
  604. else
  605. temp &= ~PORT_WKDISC_E;
  606. if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT)
  607. temp |= PORT_WKOC_E;
  608. else
  609. temp &= ~PORT_WKOC_E;
  610. writel(temp, port_array[port_id]);
  611. }
  612. /* Test and clear port RWC bit */
  613. void xhci_test_and_clear_bit(struct xhci_hcd *xhci, __le32 __iomem **port_array,
  614. int port_id, u32 port_bit)
  615. {
  616. u32 temp;
  617. temp = readl(port_array[port_id]);
  618. if (temp & port_bit) {
  619. temp = xhci_port_state_to_neutral(temp);
  620. temp |= port_bit;
  621. writel(temp, port_array[port_id]);
  622. }
  623. }
  624. /* Updates Link Status for USB 2.1 port */
  625. static void xhci_hub_report_usb2_link_state(u32 *status, u32 status_reg)
  626. {
  627. if ((status_reg & PORT_PLS_MASK) == XDEV_U2)
  628. *status |= USB_PORT_STAT_L1;
  629. }
  630. /* Updates Link Status for super Speed port */
  631. static void xhci_hub_report_usb3_link_state(struct xhci_hcd *xhci,
  632. u32 *status, u32 status_reg)
  633. {
  634. u32 pls = status_reg & PORT_PLS_MASK;
  635. /* resume state is a xHCI internal state.
  636. * Do not report it to usb core, instead, pretend to be U3,
  637. * thus usb core knows it's not ready for transfer
  638. */
  639. if (pls == XDEV_RESUME) {
  640. *status |= USB_SS_PORT_LS_U3;
  641. return;
  642. }
  643. /* When the CAS bit is set then warm reset
  644. * should be performed on port
  645. */
  646. if (status_reg & PORT_CAS) {
  647. /* The CAS bit can be set while the port is
  648. * in any link state.
  649. * Only roothubs have CAS bit, so we
  650. * pretend to be in compliance mode
  651. * unless we're already in compliance
  652. * or the inactive state.
  653. */
  654. if (pls != USB_SS_PORT_LS_COMP_MOD &&
  655. pls != USB_SS_PORT_LS_SS_INACTIVE) {
  656. pls = USB_SS_PORT_LS_COMP_MOD;
  657. }
  658. /* Return also connection bit -
  659. * hub state machine resets port
  660. * when this bit is set.
  661. */
  662. pls |= USB_PORT_STAT_CONNECTION;
  663. } else {
  664. /*
  665. * If CAS bit isn't set but the Port is already at
  666. * Compliance Mode, fake a connection so the USB core
  667. * notices the Compliance state and resets the port.
  668. * This resolves an issue generated by the SN65LVPE502CP
  669. * in which sometimes the port enters compliance mode
  670. * caused by a delay on the host-device negotiation.
  671. */
  672. if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
  673. (pls == USB_SS_PORT_LS_COMP_MOD))
  674. pls |= USB_PORT_STAT_CONNECTION;
  675. }
  676. /* update status field */
  677. *status |= pls;
  678. }
  679. /*
  680. * Function for Compliance Mode Quirk.
  681. *
  682. * This Function verifies if all xhc USB3 ports have entered U0, if so,
  683. * the compliance mode timer is deleted. A port won't enter
  684. * compliance mode if it has previously entered U0.
  685. */
  686. static void xhci_del_comp_mod_timer(struct xhci_hcd *xhci, u32 status,
  687. u16 wIndex)
  688. {
  689. u32 all_ports_seen_u0 = ((1 << xhci->num_usb3_ports)-1);
  690. bool port_in_u0 = ((status & PORT_PLS_MASK) == XDEV_U0);
  691. if (!(xhci->quirks & XHCI_COMP_MODE_QUIRK))
  692. return;
  693. if ((xhci->port_status_u0 != all_ports_seen_u0) && port_in_u0) {
  694. xhci->port_status_u0 |= 1 << wIndex;
  695. if (xhci->port_status_u0 == all_ports_seen_u0) {
  696. del_timer_sync(&xhci->comp_mode_recovery_timer);
  697. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  698. "All USB3 ports have entered U0 already!");
  699. xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
  700. "Compliance Mode Recovery Timer Deleted.");
  701. }
  702. }
  703. }
  704. static u32 xhci_get_ext_port_status(u32 raw_port_status, u32 port_li)
  705. {
  706. u32 ext_stat = 0;
  707. int speed_id;
  708. /* only support rx and tx lane counts of 1 in usb3.1 spec */
  709. speed_id = DEV_PORT_SPEED(raw_port_status);
  710. ext_stat |= speed_id; /* bits 3:0, RX speed id */
  711. ext_stat |= speed_id << 4; /* bits 7:4, TX speed id */
  712. ext_stat |= PORT_RX_LANES(port_li) << 8; /* bits 11:8 Rx lane count */
  713. ext_stat |= PORT_TX_LANES(port_li) << 12; /* bits 15:12 Tx lane count */
  714. return ext_stat;
  715. }
  716. /*
  717. * Converts a raw xHCI port status into the format that external USB 2.0 or USB
  718. * 3.0 hubs use.
  719. *
  720. * Possible side effects:
  721. * - Mark a port as being done with device resume,
  722. * and ring the endpoint doorbells.
  723. * - Stop the Synopsys redriver Compliance Mode polling.
  724. * - Drop and reacquire the xHCI lock, in order to wait for port resume.
  725. */
  726. static u32 xhci_get_port_status(struct usb_hcd *hcd,
  727. struct xhci_bus_state *bus_state,
  728. __le32 __iomem **port_array,
  729. u16 wIndex, u32 raw_port_status,
  730. unsigned long flags)
  731. __releases(&xhci->lock)
  732. __acquires(&xhci->lock)
  733. {
  734. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  735. u32 status = 0;
  736. int slot_id;
  737. /* wPortChange bits */
  738. if (raw_port_status & PORT_CSC)
  739. status |= USB_PORT_STAT_C_CONNECTION << 16;
  740. if (raw_port_status & PORT_PEC)
  741. status |= USB_PORT_STAT_C_ENABLE << 16;
  742. if ((raw_port_status & PORT_OCC))
  743. status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  744. if ((raw_port_status & PORT_RC))
  745. status |= USB_PORT_STAT_C_RESET << 16;
  746. /* USB3.0 only */
  747. if (hcd->speed >= HCD_USB3) {
  748. /* Port link change with port in resume state should not be
  749. * reported to usbcore, as this is an internal state to be
  750. * handled by xhci driver. Reporting PLC to usbcore may
  751. * cause usbcore clearing PLC first and port change event
  752. * irq won't be generated.
  753. */
  754. if ((raw_port_status & PORT_PLC) &&
  755. (raw_port_status & PORT_PLS_MASK) != XDEV_RESUME)
  756. status |= USB_PORT_STAT_C_LINK_STATE << 16;
  757. if ((raw_port_status & PORT_WRC))
  758. status |= USB_PORT_STAT_C_BH_RESET << 16;
  759. if ((raw_port_status & PORT_CEC))
  760. status |= USB_PORT_STAT_C_CONFIG_ERROR << 16;
  761. }
  762. if (hcd->speed < HCD_USB3) {
  763. if ((raw_port_status & PORT_PLS_MASK) == XDEV_U3
  764. && (raw_port_status & PORT_POWER))
  765. status |= USB_PORT_STAT_SUSPEND;
  766. }
  767. if ((raw_port_status & PORT_PLS_MASK) == XDEV_RESUME &&
  768. !DEV_SUPERSPEED_ANY(raw_port_status)) {
  769. if ((raw_port_status & PORT_RESET) ||
  770. !(raw_port_status & PORT_PE))
  771. return 0xffffffff;
  772. /* did port event handler already start resume timing? */
  773. if (!bus_state->resume_done[wIndex]) {
  774. /* If not, maybe we are in a host initated resume? */
  775. if (test_bit(wIndex, &bus_state->resuming_ports)) {
  776. /* Host initated resume doesn't time the resume
  777. * signalling using resume_done[].
  778. * It manually sets RESUME state, sleeps 20ms
  779. * and sets U0 state. This should probably be
  780. * changed, but not right now.
  781. */
  782. } else {
  783. /* port resume was discovered now and here,
  784. * start resume timing
  785. */
  786. unsigned long timeout = jiffies +
  787. msecs_to_jiffies(USB_RESUME_TIMEOUT);
  788. set_bit(wIndex, &bus_state->resuming_ports);
  789. bus_state->resume_done[wIndex] = timeout;
  790. mod_timer(&hcd->rh_timer, timeout);
  791. }
  792. /* Has resume been signalled for USB_RESUME_TIME yet? */
  793. } else if (time_after_eq(jiffies,
  794. bus_state->resume_done[wIndex])) {
  795. int time_left;
  796. xhci_dbg(xhci, "Resume USB2 port %d\n",
  797. wIndex + 1);
  798. bus_state->resume_done[wIndex] = 0;
  799. clear_bit(wIndex, &bus_state->resuming_ports);
  800. set_bit(wIndex, &bus_state->rexit_ports);
  801. xhci_set_link_state(xhci, port_array, wIndex,
  802. XDEV_U0);
  803. spin_unlock_irqrestore(&xhci->lock, flags);
  804. time_left = wait_for_completion_timeout(
  805. &bus_state->rexit_done[wIndex],
  806. msecs_to_jiffies(
  807. XHCI_MAX_REXIT_TIMEOUT));
  808. spin_lock_irqsave(&xhci->lock, flags);
  809. if (time_left) {
  810. slot_id = xhci_find_slot_id_by_port(hcd,
  811. xhci, wIndex + 1);
  812. if (!slot_id) {
  813. xhci_dbg(xhci, "slot_id is zero\n");
  814. return 0xffffffff;
  815. }
  816. xhci_ring_device(xhci, slot_id);
  817. } else {
  818. int port_status = readl(port_array[wIndex]);
  819. xhci_warn(xhci, "Port resume took longer than %i msec, port status = 0x%x\n",
  820. XHCI_MAX_REXIT_TIMEOUT,
  821. port_status);
  822. status |= USB_PORT_STAT_SUSPEND;
  823. clear_bit(wIndex, &bus_state->rexit_ports);
  824. }
  825. bus_state->port_c_suspend |= 1 << wIndex;
  826. bus_state->suspended_ports &= ~(1 << wIndex);
  827. } else {
  828. /*
  829. * The resume has been signaling for less than
  830. * USB_RESUME_TIME. Report the port status as SUSPEND,
  831. * let the usbcore check port status again and clear
  832. * resume signaling later.
  833. */
  834. status |= USB_PORT_STAT_SUSPEND;
  835. }
  836. }
  837. /*
  838. * Clear stale usb2 resume signalling variables in case port changed
  839. * state during resume signalling. For example on error
  840. */
  841. if ((bus_state->resume_done[wIndex] ||
  842. test_bit(wIndex, &bus_state->resuming_ports)) &&
  843. (raw_port_status & PORT_PLS_MASK) != XDEV_U3 &&
  844. (raw_port_status & PORT_PLS_MASK) != XDEV_RESUME) {
  845. bus_state->resume_done[wIndex] = 0;
  846. clear_bit(wIndex, &bus_state->resuming_ports);
  847. }
  848. if ((raw_port_status & PORT_PLS_MASK) == XDEV_U0 &&
  849. (raw_port_status & PORT_POWER)) {
  850. if (bus_state->suspended_ports & (1 << wIndex)) {
  851. bus_state->suspended_ports &= ~(1 << wIndex);
  852. if (hcd->speed < HCD_USB3)
  853. bus_state->port_c_suspend |= 1 << wIndex;
  854. }
  855. bus_state->resume_done[wIndex] = 0;
  856. clear_bit(wIndex, &bus_state->resuming_ports);
  857. }
  858. if (raw_port_status & PORT_CONNECT) {
  859. status |= USB_PORT_STAT_CONNECTION;
  860. status |= xhci_port_speed(raw_port_status);
  861. }
  862. if (raw_port_status & PORT_PE)
  863. status |= USB_PORT_STAT_ENABLE;
  864. if (raw_port_status & PORT_OC)
  865. status |= USB_PORT_STAT_OVERCURRENT;
  866. if (raw_port_status & PORT_RESET)
  867. status |= USB_PORT_STAT_RESET;
  868. if (raw_port_status & PORT_POWER) {
  869. if (hcd->speed >= HCD_USB3)
  870. status |= USB_SS_PORT_STAT_POWER;
  871. else
  872. status |= USB_PORT_STAT_POWER;
  873. }
  874. /* Update Port Link State */
  875. if (hcd->speed >= HCD_USB3) {
  876. xhci_hub_report_usb3_link_state(xhci, &status, raw_port_status);
  877. /*
  878. * Verify if all USB3 Ports Have entered U0 already.
  879. * Delete Compliance Mode Timer if so.
  880. */
  881. xhci_del_comp_mod_timer(xhci, raw_port_status, wIndex);
  882. } else {
  883. xhci_hub_report_usb2_link_state(&status, raw_port_status);
  884. }
  885. if (bus_state->port_c_suspend & (1 << wIndex))
  886. status |= USB_PORT_STAT_C_SUSPEND << 16;
  887. return status;
  888. }
  889. int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  890. u16 wIndex, char *buf, u16 wLength)
  891. {
  892. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  893. int max_ports;
  894. unsigned long flags;
  895. u32 temp, status;
  896. int retval = 0;
  897. __le32 __iomem **port_array;
  898. int slot_id;
  899. struct xhci_bus_state *bus_state;
  900. u16 link_state = 0;
  901. u16 wake_mask = 0;
  902. u16 timeout = 0;
  903. u16 test_mode = 0;
  904. max_ports = xhci_get_ports(hcd, &port_array);
  905. bus_state = &xhci->bus_state[hcd_index(hcd)];
  906. spin_lock_irqsave(&xhci->lock, flags);
  907. switch (typeReq) {
  908. case GetHubStatus:
  909. /* No power source, over-current reported per port */
  910. memset(buf, 0, 4);
  911. break;
  912. case GetHubDescriptor:
  913. /* Check to make sure userspace is asking for the USB 3.0 hub
  914. * descriptor for the USB 3.0 roothub. If not, we stall the
  915. * endpoint, like external hubs do.
  916. */
  917. if (hcd->speed >= HCD_USB3 &&
  918. (wLength < USB_DT_SS_HUB_SIZE ||
  919. wValue != (USB_DT_SS_HUB << 8))) {
  920. xhci_dbg(xhci, "Wrong hub descriptor type for "
  921. "USB 3.0 roothub.\n");
  922. goto error;
  923. }
  924. xhci_hub_descriptor(hcd, xhci,
  925. (struct usb_hub_descriptor *) buf);
  926. break;
  927. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  928. if ((wValue & 0xff00) != (USB_DT_BOS << 8))
  929. goto error;
  930. if (hcd->speed < HCD_USB3)
  931. goto error;
  932. retval = xhci_create_usb3_bos_desc(xhci, buf, wLength);
  933. spin_unlock_irqrestore(&xhci->lock, flags);
  934. return retval;
  935. case GetPortStatus:
  936. if (!wIndex || wIndex > max_ports)
  937. goto error;
  938. wIndex--;
  939. temp = readl(port_array[wIndex]);
  940. if (temp == ~(u32)0) {
  941. xhci_hc_died(xhci);
  942. retval = -ENODEV;
  943. break;
  944. }
  945. status = xhci_get_port_status(hcd, bus_state, port_array,
  946. wIndex, temp, flags);
  947. if (status == 0xffffffff)
  948. goto error;
  949. xhci_dbg(xhci, "get port status, actual port %d status = 0x%x\n",
  950. wIndex, temp);
  951. xhci_dbg(xhci, "Get port status returned 0x%x\n", status);
  952. put_unaligned(cpu_to_le32(status), (__le32 *) buf);
  953. /* if USB 3.1 extended port status return additional 4 bytes */
  954. if (wValue == 0x02) {
  955. u32 port_li;
  956. if (hcd->speed < HCD_USB31 || wLength != 8) {
  957. xhci_err(xhci, "get ext port status invalid parameter\n");
  958. retval = -EINVAL;
  959. break;
  960. }
  961. port_li = readl(port_array[wIndex] + PORTLI);
  962. status = xhci_get_ext_port_status(temp, port_li);
  963. put_unaligned_le32(cpu_to_le32(status), &buf[4]);
  964. }
  965. break;
  966. case SetPortFeature:
  967. if (wValue == USB_PORT_FEAT_LINK_STATE)
  968. link_state = (wIndex & 0xff00) >> 3;
  969. if (wValue == USB_PORT_FEAT_REMOTE_WAKE_MASK)
  970. wake_mask = wIndex & 0xff00;
  971. if (wValue == USB_PORT_FEAT_TEST)
  972. test_mode = (wIndex & 0xff00) >> 8;
  973. /* The MSB of wIndex is the U1/U2 timeout */
  974. timeout = (wIndex & 0xff00) >> 8;
  975. wIndex &= 0xff;
  976. if (!wIndex || wIndex > max_ports)
  977. goto error;
  978. wIndex--;
  979. temp = readl(port_array[wIndex]);
  980. if (temp == ~(u32)0) {
  981. xhci_hc_died(xhci);
  982. retval = -ENODEV;
  983. break;
  984. }
  985. temp = xhci_port_state_to_neutral(temp);
  986. /* FIXME: What new port features do we need to support? */
  987. switch (wValue) {
  988. case USB_PORT_FEAT_SUSPEND:
  989. temp = readl(port_array[wIndex]);
  990. if ((temp & PORT_PLS_MASK) != XDEV_U0) {
  991. /* Resume the port to U0 first */
  992. xhci_set_link_state(xhci, port_array, wIndex,
  993. XDEV_U0);
  994. spin_unlock_irqrestore(&xhci->lock, flags);
  995. msleep(10);
  996. spin_lock_irqsave(&xhci->lock, flags);
  997. }
  998. /* In spec software should not attempt to suspend
  999. * a port unless the port reports that it is in the
  1000. * enabled (PED = ‘1’,PLS < ‘3’) state.
  1001. */
  1002. temp = readl(port_array[wIndex]);
  1003. if ((temp & PORT_PE) == 0 || (temp & PORT_RESET)
  1004. || (temp & PORT_PLS_MASK) >= XDEV_U3) {
  1005. xhci_warn(xhci, "USB core suspending device not in U0/U1/U2.\n");
  1006. goto error;
  1007. }
  1008. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1009. wIndex + 1);
  1010. if (!slot_id) {
  1011. xhci_warn(xhci, "slot_id is zero\n");
  1012. goto error;
  1013. }
  1014. /* unlock to execute stop endpoint commands */
  1015. spin_unlock_irqrestore(&xhci->lock, flags);
  1016. xhci_stop_device(xhci, slot_id, 1);
  1017. spin_lock_irqsave(&xhci->lock, flags);
  1018. xhci_set_link_state(xhci, port_array, wIndex, XDEV_U3);
  1019. spin_unlock_irqrestore(&xhci->lock, flags);
  1020. msleep(10); /* wait device to enter */
  1021. spin_lock_irqsave(&xhci->lock, flags);
  1022. temp = readl(port_array[wIndex]);
  1023. bus_state->suspended_ports |= 1 << wIndex;
  1024. break;
  1025. case USB_PORT_FEAT_LINK_STATE:
  1026. temp = readl(port_array[wIndex]);
  1027. /* Disable port */
  1028. if (link_state == USB_SS_PORT_LS_SS_DISABLED) {
  1029. xhci_dbg(xhci, "Disable port %d\n", wIndex);
  1030. temp = xhci_port_state_to_neutral(temp);
  1031. /*
  1032. * Clear all change bits, so that we get a new
  1033. * connection event.
  1034. */
  1035. temp |= PORT_CSC | PORT_PEC | PORT_WRC |
  1036. PORT_OCC | PORT_RC | PORT_PLC |
  1037. PORT_CEC;
  1038. writel(temp | PORT_PE, port_array[wIndex]);
  1039. temp = readl(port_array[wIndex]);
  1040. break;
  1041. }
  1042. /* Put link in RxDetect (enable port) */
  1043. if (link_state == USB_SS_PORT_LS_RX_DETECT) {
  1044. xhci_dbg(xhci, "Enable port %d\n", wIndex);
  1045. xhci_set_link_state(xhci, port_array, wIndex,
  1046. link_state);
  1047. temp = readl(port_array[wIndex]);
  1048. break;
  1049. }
  1050. /* Software should not attempt to set
  1051. * port link state above '3' (U3) and the port
  1052. * must be enabled.
  1053. */
  1054. if ((temp & PORT_PE) == 0 ||
  1055. (link_state > USB_SS_PORT_LS_U3)) {
  1056. xhci_warn(xhci, "Cannot set link state.\n");
  1057. goto error;
  1058. }
  1059. if (link_state == USB_SS_PORT_LS_U3) {
  1060. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1061. wIndex + 1);
  1062. if (slot_id) {
  1063. /* unlock to execute stop endpoint
  1064. * commands */
  1065. spin_unlock_irqrestore(&xhci->lock,
  1066. flags);
  1067. xhci_stop_device(xhci, slot_id, 1);
  1068. spin_lock_irqsave(&xhci->lock, flags);
  1069. }
  1070. }
  1071. xhci_set_link_state(xhci, port_array, wIndex,
  1072. link_state);
  1073. spin_unlock_irqrestore(&xhci->lock, flags);
  1074. msleep(20); /* wait device to enter */
  1075. spin_lock_irqsave(&xhci->lock, flags);
  1076. temp = readl(port_array[wIndex]);
  1077. if (link_state == USB_SS_PORT_LS_U3)
  1078. bus_state->suspended_ports |= 1 << wIndex;
  1079. break;
  1080. case USB_PORT_FEAT_POWER:
  1081. /*
  1082. * Turn on ports, even if there isn't per-port switching.
  1083. * HC will report connect events even before this is set.
  1084. * However, hub_wq will ignore the roothub events until
  1085. * the roothub is registered.
  1086. */
  1087. xhci_set_port_power(xhci, hcd, wIndex, true, &flags);
  1088. break;
  1089. case USB_PORT_FEAT_RESET:
  1090. temp = (temp | PORT_RESET);
  1091. writel(temp, port_array[wIndex]);
  1092. temp = readl(port_array[wIndex]);
  1093. xhci_dbg(xhci, "set port reset, actual port %d status = 0x%x\n", wIndex, temp);
  1094. break;
  1095. case USB_PORT_FEAT_REMOTE_WAKE_MASK:
  1096. xhci_set_remote_wake_mask(xhci, port_array,
  1097. wIndex, wake_mask);
  1098. temp = readl(port_array[wIndex]);
  1099. xhci_dbg(xhci, "set port remote wake mask, "
  1100. "actual port %d status = 0x%x\n",
  1101. wIndex, temp);
  1102. break;
  1103. case USB_PORT_FEAT_BH_PORT_RESET:
  1104. temp |= PORT_WR;
  1105. writel(temp, port_array[wIndex]);
  1106. temp = readl(port_array[wIndex]);
  1107. break;
  1108. case USB_PORT_FEAT_U1_TIMEOUT:
  1109. if (hcd->speed < HCD_USB3)
  1110. goto error;
  1111. temp = readl(port_array[wIndex] + PORTPMSC);
  1112. temp &= ~PORT_U1_TIMEOUT_MASK;
  1113. temp |= PORT_U1_TIMEOUT(timeout);
  1114. writel(temp, port_array[wIndex] + PORTPMSC);
  1115. break;
  1116. case USB_PORT_FEAT_U2_TIMEOUT:
  1117. if (hcd->speed < HCD_USB3)
  1118. goto error;
  1119. temp = readl(port_array[wIndex] + PORTPMSC);
  1120. temp &= ~PORT_U2_TIMEOUT_MASK;
  1121. temp |= PORT_U2_TIMEOUT(timeout);
  1122. writel(temp, port_array[wIndex] + PORTPMSC);
  1123. break;
  1124. case USB_PORT_FEAT_TEST:
  1125. /* 4.19.6 Port Test Modes (USB2 Test Mode) */
  1126. if (hcd->speed != HCD_USB2)
  1127. goto error;
  1128. if (test_mode > TEST_FORCE_EN || test_mode < TEST_J)
  1129. goto error;
  1130. retval = xhci_enter_test_mode(xhci, test_mode, wIndex,
  1131. &flags);
  1132. break;
  1133. default:
  1134. goto error;
  1135. }
  1136. /* unblock any posted writes */
  1137. temp = readl(port_array[wIndex]);
  1138. break;
  1139. case ClearPortFeature:
  1140. if (!wIndex || wIndex > max_ports)
  1141. goto error;
  1142. wIndex--;
  1143. temp = readl(port_array[wIndex]);
  1144. if (temp == ~(u32)0) {
  1145. xhci_hc_died(xhci);
  1146. retval = -ENODEV;
  1147. break;
  1148. }
  1149. /* FIXME: What new port features do we need to support? */
  1150. temp = xhci_port_state_to_neutral(temp);
  1151. switch (wValue) {
  1152. case USB_PORT_FEAT_SUSPEND:
  1153. temp = readl(port_array[wIndex]);
  1154. xhci_dbg(xhci, "clear USB_PORT_FEAT_SUSPEND\n");
  1155. xhci_dbg(xhci, "PORTSC %04x\n", temp);
  1156. if (temp & PORT_RESET)
  1157. goto error;
  1158. if ((temp & PORT_PLS_MASK) == XDEV_U3) {
  1159. if ((temp & PORT_PE) == 0)
  1160. goto error;
  1161. set_bit(wIndex, &bus_state->resuming_ports);
  1162. xhci_set_link_state(xhci, port_array, wIndex,
  1163. XDEV_RESUME);
  1164. spin_unlock_irqrestore(&xhci->lock, flags);
  1165. msleep(USB_RESUME_TIMEOUT);
  1166. spin_lock_irqsave(&xhci->lock, flags);
  1167. xhci_set_link_state(xhci, port_array, wIndex,
  1168. XDEV_U0);
  1169. clear_bit(wIndex, &bus_state->resuming_ports);
  1170. }
  1171. bus_state->port_c_suspend |= 1 << wIndex;
  1172. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1173. wIndex + 1);
  1174. if (!slot_id) {
  1175. xhci_dbg(xhci, "slot_id is zero\n");
  1176. goto error;
  1177. }
  1178. xhci_ring_device(xhci, slot_id);
  1179. break;
  1180. case USB_PORT_FEAT_C_SUSPEND:
  1181. bus_state->port_c_suspend &= ~(1 << wIndex);
  1182. case USB_PORT_FEAT_C_RESET:
  1183. case USB_PORT_FEAT_C_BH_PORT_RESET:
  1184. case USB_PORT_FEAT_C_CONNECTION:
  1185. case USB_PORT_FEAT_C_OVER_CURRENT:
  1186. case USB_PORT_FEAT_C_ENABLE:
  1187. case USB_PORT_FEAT_C_PORT_LINK_STATE:
  1188. case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
  1189. xhci_clear_port_change_bit(xhci, wValue, wIndex,
  1190. port_array[wIndex], temp);
  1191. break;
  1192. case USB_PORT_FEAT_ENABLE:
  1193. xhci_disable_port(hcd, xhci, wIndex,
  1194. port_array[wIndex], temp);
  1195. break;
  1196. case USB_PORT_FEAT_POWER:
  1197. xhci_set_port_power(xhci, hcd, wIndex, false, &flags);
  1198. break;
  1199. case USB_PORT_FEAT_TEST:
  1200. retval = xhci_exit_test_mode(xhci);
  1201. break;
  1202. default:
  1203. goto error;
  1204. }
  1205. break;
  1206. default:
  1207. error:
  1208. /* "stall" on error */
  1209. retval = -EPIPE;
  1210. }
  1211. spin_unlock_irqrestore(&xhci->lock, flags);
  1212. return retval;
  1213. }
  1214. /*
  1215. * Returns 0 if the status hasn't changed, or the number of bytes in buf.
  1216. * Ports are 0-indexed from the HCD point of view,
  1217. * and 1-indexed from the USB core pointer of view.
  1218. *
  1219. * Note that the status change bits will be cleared as soon as a port status
  1220. * change event is generated, so we use the saved status from that event.
  1221. */
  1222. int xhci_hub_status_data(struct usb_hcd *hcd, char *buf)
  1223. {
  1224. unsigned long flags;
  1225. u32 temp, status;
  1226. u32 mask;
  1227. int i, retval;
  1228. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1229. int max_ports;
  1230. __le32 __iomem **port_array;
  1231. struct xhci_bus_state *bus_state;
  1232. bool reset_change = false;
  1233. max_ports = xhci_get_ports(hcd, &port_array);
  1234. bus_state = &xhci->bus_state[hcd_index(hcd)];
  1235. /* Initial status is no changes */
  1236. retval = (max_ports + 8) / 8;
  1237. memset(buf, 0, retval);
  1238. /*
  1239. * Inform the usbcore about resume-in-progress by returning
  1240. * a non-zero value even if there are no status changes.
  1241. */
  1242. status = bus_state->resuming_ports;
  1243. mask = PORT_CSC | PORT_PEC | PORT_OCC | PORT_PLC | PORT_WRC | PORT_CEC;
  1244. spin_lock_irqsave(&xhci->lock, flags);
  1245. /* For each port, did anything change? If so, set that bit in buf. */
  1246. for (i = 0; i < max_ports; i++) {
  1247. temp = readl(port_array[i]);
  1248. if (temp == ~(u32)0) {
  1249. xhci_hc_died(xhci);
  1250. retval = -ENODEV;
  1251. break;
  1252. }
  1253. if ((temp & mask) != 0 ||
  1254. (bus_state->port_c_suspend & 1 << i) ||
  1255. (bus_state->resume_done[i] && time_after_eq(
  1256. jiffies, bus_state->resume_done[i]))) {
  1257. buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
  1258. status = 1;
  1259. }
  1260. if ((temp & PORT_RC))
  1261. reset_change = true;
  1262. }
  1263. if (!status && !reset_change) {
  1264. xhci_dbg(xhci, "%s: stopping port polling.\n", __func__);
  1265. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  1266. }
  1267. spin_unlock_irqrestore(&xhci->lock, flags);
  1268. return status ? retval : 0;
  1269. }
  1270. #ifdef CONFIG_PM
  1271. int xhci_bus_suspend(struct usb_hcd *hcd)
  1272. {
  1273. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1274. int max_ports, port_index;
  1275. __le32 __iomem **port_array;
  1276. struct xhci_bus_state *bus_state;
  1277. unsigned long flags;
  1278. max_ports = xhci_get_ports(hcd, &port_array);
  1279. bus_state = &xhci->bus_state[hcd_index(hcd)];
  1280. spin_lock_irqsave(&xhci->lock, flags);
  1281. if (hcd->self.root_hub->do_remote_wakeup) {
  1282. if (bus_state->resuming_ports || /* USB2 */
  1283. bus_state->port_remote_wakeup) { /* USB3 */
  1284. spin_unlock_irqrestore(&xhci->lock, flags);
  1285. xhci_dbg(xhci, "suspend failed because a port is resuming\n");
  1286. return -EBUSY;
  1287. }
  1288. }
  1289. port_index = max_ports;
  1290. bus_state->bus_suspended = 0;
  1291. while (port_index--) {
  1292. /* suspend the port if the port is not suspended */
  1293. u32 t1, t2;
  1294. int slot_id;
  1295. t1 = readl(port_array[port_index]);
  1296. t2 = xhci_port_state_to_neutral(t1);
  1297. if ((t1 & PORT_PE) && !(t1 & PORT_PLS_MASK)) {
  1298. xhci_dbg(xhci, "port %d not suspended\n", port_index);
  1299. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1300. port_index + 1);
  1301. if (slot_id) {
  1302. spin_unlock_irqrestore(&xhci->lock, flags);
  1303. xhci_stop_device(xhci, slot_id, 1);
  1304. spin_lock_irqsave(&xhci->lock, flags);
  1305. }
  1306. t2 &= ~PORT_PLS_MASK;
  1307. t2 |= PORT_LINK_STROBE | XDEV_U3;
  1308. set_bit(port_index, &bus_state->bus_suspended);
  1309. }
  1310. /* USB core sets remote wake mask for USB 3.0 hubs,
  1311. * including the USB 3.0 roothub, but only if CONFIG_PM
  1312. * is enabled, so also enable remote wake here.
  1313. */
  1314. if (hcd->self.root_hub->do_remote_wakeup) {
  1315. if (t1 & PORT_CONNECT) {
  1316. t2 |= PORT_WKOC_E | PORT_WKDISC_E;
  1317. t2 &= ~PORT_WKCONN_E;
  1318. } else {
  1319. t2 |= PORT_WKOC_E | PORT_WKCONN_E;
  1320. t2 &= ~PORT_WKDISC_E;
  1321. }
  1322. } else
  1323. t2 &= ~PORT_WAKE_BITS;
  1324. t1 = xhci_port_state_to_neutral(t1);
  1325. if (t1 != t2)
  1326. writel(t2, port_array[port_index]);
  1327. }
  1328. hcd->state = HC_STATE_SUSPENDED;
  1329. bus_state->next_statechange = jiffies + msecs_to_jiffies(10);
  1330. spin_unlock_irqrestore(&xhci->lock, flags);
  1331. return 0;
  1332. }
  1333. /*
  1334. * Workaround for missing Cold Attach Status (CAS) if device re-plugged in S3.
  1335. * warm reset a USB3 device stuck in polling or compliance mode after resume.
  1336. * See Intel 100/c230 series PCH specification update Doc #332692-006 Errata #8
  1337. */
  1338. static bool xhci_port_missing_cas_quirk(int port_index,
  1339. __le32 __iomem **port_array)
  1340. {
  1341. u32 portsc;
  1342. portsc = readl(port_array[port_index]);
  1343. /* if any of these are set we are not stuck */
  1344. if (portsc & (PORT_CONNECT | PORT_CAS))
  1345. return false;
  1346. if (((portsc & PORT_PLS_MASK) != XDEV_POLLING) &&
  1347. ((portsc & PORT_PLS_MASK) != XDEV_COMP_MODE))
  1348. return false;
  1349. /* clear wakeup/change bits, and do a warm port reset */
  1350. portsc &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
  1351. portsc |= PORT_WR;
  1352. writel(portsc, port_array[port_index]);
  1353. /* flush write */
  1354. readl(port_array[port_index]);
  1355. return true;
  1356. }
  1357. int xhci_bus_resume(struct usb_hcd *hcd)
  1358. {
  1359. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1360. int max_ports, port_index;
  1361. __le32 __iomem **port_array;
  1362. struct xhci_bus_state *bus_state;
  1363. u32 temp;
  1364. unsigned long flags;
  1365. unsigned long port_was_suspended = 0;
  1366. bool need_usb2_u3_exit = false;
  1367. int slot_id;
  1368. int sret;
  1369. max_ports = xhci_get_ports(hcd, &port_array);
  1370. bus_state = &xhci->bus_state[hcd_index(hcd)];
  1371. if (time_before(jiffies, bus_state->next_statechange))
  1372. msleep(5);
  1373. spin_lock_irqsave(&xhci->lock, flags);
  1374. if (!HCD_HW_ACCESSIBLE(hcd)) {
  1375. spin_unlock_irqrestore(&xhci->lock, flags);
  1376. return -ESHUTDOWN;
  1377. }
  1378. /* delay the irqs */
  1379. temp = readl(&xhci->op_regs->command);
  1380. temp &= ~CMD_EIE;
  1381. writel(temp, &xhci->op_regs->command);
  1382. port_index = max_ports;
  1383. while (port_index--) {
  1384. /* Check whether need resume ports. If needed
  1385. resume port and disable remote wakeup */
  1386. u32 temp;
  1387. temp = readl(port_array[port_index]);
  1388. /* warm reset CAS limited ports stuck in polling/compliance */
  1389. if ((xhci->quirks & XHCI_MISSING_CAS) &&
  1390. (hcd->speed >= HCD_USB3) &&
  1391. xhci_port_missing_cas_quirk(port_index, port_array)) {
  1392. xhci_dbg(xhci, "reset stuck port %d\n", port_index);
  1393. continue;
  1394. }
  1395. if (DEV_SUPERSPEED_ANY(temp))
  1396. temp &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
  1397. else
  1398. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  1399. if (test_bit(port_index, &bus_state->bus_suspended) &&
  1400. (temp & PORT_PLS_MASK)) {
  1401. set_bit(port_index, &port_was_suspended);
  1402. if (!DEV_SUPERSPEED_ANY(temp)) {
  1403. xhci_set_link_state(xhci, port_array,
  1404. port_index, XDEV_RESUME);
  1405. need_usb2_u3_exit = true;
  1406. }
  1407. } else
  1408. writel(temp, port_array[port_index]);
  1409. }
  1410. if (need_usb2_u3_exit) {
  1411. spin_unlock_irqrestore(&xhci->lock, flags);
  1412. msleep(USB_RESUME_TIMEOUT);
  1413. spin_lock_irqsave(&xhci->lock, flags);
  1414. }
  1415. port_index = max_ports;
  1416. while (port_index--) {
  1417. if (!(port_was_suspended & BIT(port_index)))
  1418. continue;
  1419. /* Clear PLC to poll it later after XDEV_U0 */
  1420. xhci_test_and_clear_bit(xhci, port_array, port_index, PORT_PLC);
  1421. xhci_set_link_state(xhci, port_array, port_index, XDEV_U0);
  1422. }
  1423. port_index = max_ports;
  1424. while (port_index--) {
  1425. if (!(port_was_suspended & BIT(port_index)))
  1426. continue;
  1427. /* Poll and Clear PLC */
  1428. sret = xhci_handshake(port_array[port_index], PORT_PLC,
  1429. PORT_PLC, 10 * 1000);
  1430. if (sret)
  1431. xhci_warn(xhci, "port %d resume PLC timeout\n",
  1432. port_index);
  1433. xhci_test_and_clear_bit(xhci, port_array, port_index, PORT_PLC);
  1434. slot_id = xhci_find_slot_id_by_port(hcd, xhci, port_index + 1);
  1435. if (slot_id)
  1436. xhci_ring_device(xhci, slot_id);
  1437. }
  1438. (void) readl(&xhci->op_regs->command);
  1439. bus_state->next_statechange = jiffies + msecs_to_jiffies(5);
  1440. /* re-enable irqs */
  1441. temp = readl(&xhci->op_regs->command);
  1442. temp |= CMD_EIE;
  1443. writel(temp, &xhci->op_regs->command);
  1444. temp = readl(&xhci->op_regs->command);
  1445. spin_unlock_irqrestore(&xhci->lock, flags);
  1446. return 0;
  1447. }
  1448. #endif /* CONFIG_PM */