xhci-dbgcap.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. // SPDX-License-Identifier: GPL-2.0
  2. /**
  3. * xhci-dbgcap.c - xHCI debug capability support
  4. *
  5. * Copyright (C) 2017 Intel Corporation
  6. *
  7. * Author: Lu Baolu <baolu.lu@linux.intel.com>
  8. */
  9. #include <linux/dma-mapping.h>
  10. #include <linux/slab.h>
  11. #include <linux/nls.h>
  12. #include "xhci.h"
  13. #include "xhci-trace.h"
  14. #include "xhci-dbgcap.h"
  15. static inline void *
  16. dbc_dma_alloc_coherent(struct xhci_hcd *xhci, size_t size,
  17. dma_addr_t *dma_handle, gfp_t flags)
  18. {
  19. void *vaddr;
  20. vaddr = dma_alloc_coherent(xhci_to_hcd(xhci)->self.sysdev,
  21. size, dma_handle, flags);
  22. memset(vaddr, 0, size);
  23. return vaddr;
  24. }
  25. static inline void
  26. dbc_dma_free_coherent(struct xhci_hcd *xhci, size_t size,
  27. void *cpu_addr, dma_addr_t dma_handle)
  28. {
  29. if (cpu_addr)
  30. dma_free_coherent(xhci_to_hcd(xhci)->self.sysdev,
  31. size, cpu_addr, dma_handle);
  32. }
  33. static u32 xhci_dbc_populate_strings(struct dbc_str_descs *strings)
  34. {
  35. struct usb_string_descriptor *s_desc;
  36. u32 string_length;
  37. /* Serial string: */
  38. s_desc = (struct usb_string_descriptor *)strings->serial;
  39. utf8s_to_utf16s(DBC_STRING_SERIAL, strlen(DBC_STRING_SERIAL),
  40. UTF16_LITTLE_ENDIAN, (wchar_t *)s_desc->wData,
  41. DBC_MAX_STRING_LENGTH);
  42. s_desc->bLength = (strlen(DBC_STRING_SERIAL) + 1) * 2;
  43. s_desc->bDescriptorType = USB_DT_STRING;
  44. string_length = s_desc->bLength;
  45. string_length <<= 8;
  46. /* Product string: */
  47. s_desc = (struct usb_string_descriptor *)strings->product;
  48. utf8s_to_utf16s(DBC_STRING_PRODUCT, strlen(DBC_STRING_PRODUCT),
  49. UTF16_LITTLE_ENDIAN, (wchar_t *)s_desc->wData,
  50. DBC_MAX_STRING_LENGTH);
  51. s_desc->bLength = (strlen(DBC_STRING_PRODUCT) + 1) * 2;
  52. s_desc->bDescriptorType = USB_DT_STRING;
  53. string_length += s_desc->bLength;
  54. string_length <<= 8;
  55. /* Manufacture string: */
  56. s_desc = (struct usb_string_descriptor *)strings->manufacturer;
  57. utf8s_to_utf16s(DBC_STRING_MANUFACTURER,
  58. strlen(DBC_STRING_MANUFACTURER),
  59. UTF16_LITTLE_ENDIAN, (wchar_t *)s_desc->wData,
  60. DBC_MAX_STRING_LENGTH);
  61. s_desc->bLength = (strlen(DBC_STRING_MANUFACTURER) + 1) * 2;
  62. s_desc->bDescriptorType = USB_DT_STRING;
  63. string_length += s_desc->bLength;
  64. string_length <<= 8;
  65. /* String0: */
  66. strings->string0[0] = 4;
  67. strings->string0[1] = USB_DT_STRING;
  68. strings->string0[2] = 0x09;
  69. strings->string0[3] = 0x04;
  70. string_length += 4;
  71. return string_length;
  72. }
  73. static void xhci_dbc_init_contexts(struct xhci_hcd *xhci, u32 string_length)
  74. {
  75. struct xhci_dbc *dbc;
  76. struct dbc_info_context *info;
  77. struct xhci_ep_ctx *ep_ctx;
  78. u32 dev_info;
  79. dma_addr_t deq, dma;
  80. unsigned int max_burst;
  81. dbc = xhci->dbc;
  82. if (!dbc)
  83. return;
  84. /* Populate info Context: */
  85. info = (struct dbc_info_context *)dbc->ctx->bytes;
  86. dma = dbc->string_dma;
  87. info->string0 = cpu_to_le64(dma);
  88. info->manufacturer = cpu_to_le64(dma + DBC_MAX_STRING_LENGTH);
  89. info->product = cpu_to_le64(dma + DBC_MAX_STRING_LENGTH * 2);
  90. info->serial = cpu_to_le64(dma + DBC_MAX_STRING_LENGTH * 3);
  91. info->length = cpu_to_le32(string_length);
  92. /* Populate bulk out endpoint context: */
  93. ep_ctx = dbc_bulkout_ctx(dbc);
  94. max_burst = DBC_CTRL_MAXBURST(readl(&dbc->regs->control));
  95. deq = dbc_bulkout_enq(dbc);
  96. ep_ctx->ep_info = 0;
  97. ep_ctx->ep_info2 = dbc_epctx_info2(BULK_OUT_EP, 1024, max_burst);
  98. ep_ctx->deq = cpu_to_le64(deq | dbc->ring_out->cycle_state);
  99. /* Populate bulk in endpoint context: */
  100. ep_ctx = dbc_bulkin_ctx(dbc);
  101. deq = dbc_bulkin_enq(dbc);
  102. ep_ctx->ep_info = 0;
  103. ep_ctx->ep_info2 = dbc_epctx_info2(BULK_IN_EP, 1024, max_burst);
  104. ep_ctx->deq = cpu_to_le64(deq | dbc->ring_in->cycle_state);
  105. /* Set DbC context and info registers: */
  106. xhci_write_64(xhci, dbc->ctx->dma, &dbc->regs->dccp);
  107. dev_info = cpu_to_le32((DBC_VENDOR_ID << 16) | DBC_PROTOCOL);
  108. writel(dev_info, &dbc->regs->devinfo1);
  109. dev_info = cpu_to_le32((DBC_DEVICE_REV << 16) | DBC_PRODUCT_ID);
  110. writel(dev_info, &dbc->regs->devinfo2);
  111. }
  112. static void xhci_dbc_giveback(struct dbc_request *req, int status)
  113. __releases(&dbc->lock)
  114. __acquires(&dbc->lock)
  115. {
  116. struct dbc_ep *dep = req->dep;
  117. struct xhci_dbc *dbc = dep->dbc;
  118. struct xhci_hcd *xhci = dbc->xhci;
  119. struct device *dev = xhci_to_hcd(dbc->xhci)->self.sysdev;
  120. list_del_init(&req->list_pending);
  121. req->trb_dma = 0;
  122. req->trb = NULL;
  123. if (req->status == -EINPROGRESS)
  124. req->status = status;
  125. trace_xhci_dbc_giveback_request(req);
  126. dma_unmap_single(dev,
  127. req->dma,
  128. req->length,
  129. dbc_ep_dma_direction(dep));
  130. /* Give back the transfer request: */
  131. spin_unlock(&dbc->lock);
  132. req->complete(xhci, req);
  133. spin_lock(&dbc->lock);
  134. }
  135. static void xhci_dbc_flush_single_request(struct dbc_request *req)
  136. {
  137. union xhci_trb *trb = req->trb;
  138. trb->generic.field[0] = 0;
  139. trb->generic.field[1] = 0;
  140. trb->generic.field[2] = 0;
  141. trb->generic.field[3] &= cpu_to_le32(TRB_CYCLE);
  142. trb->generic.field[3] |= cpu_to_le32(TRB_TYPE(TRB_TR_NOOP));
  143. xhci_dbc_giveback(req, -ESHUTDOWN);
  144. }
  145. static void xhci_dbc_flush_endpoint_requests(struct dbc_ep *dep)
  146. {
  147. struct dbc_request *req, *tmp;
  148. list_for_each_entry_safe(req, tmp, &dep->list_pending, list_pending)
  149. xhci_dbc_flush_single_request(req);
  150. }
  151. static void xhci_dbc_flush_reqests(struct xhci_dbc *dbc)
  152. {
  153. xhci_dbc_flush_endpoint_requests(&dbc->eps[BULK_OUT]);
  154. xhci_dbc_flush_endpoint_requests(&dbc->eps[BULK_IN]);
  155. }
  156. struct dbc_request *
  157. dbc_alloc_request(struct dbc_ep *dep, gfp_t gfp_flags)
  158. {
  159. struct dbc_request *req;
  160. req = kzalloc(sizeof(*req), gfp_flags);
  161. if (!req)
  162. return NULL;
  163. req->dep = dep;
  164. INIT_LIST_HEAD(&req->list_pending);
  165. INIT_LIST_HEAD(&req->list_pool);
  166. req->direction = dep->direction;
  167. trace_xhci_dbc_alloc_request(req);
  168. return req;
  169. }
  170. void
  171. dbc_free_request(struct dbc_ep *dep, struct dbc_request *req)
  172. {
  173. trace_xhci_dbc_free_request(req);
  174. kfree(req);
  175. }
  176. static void
  177. xhci_dbc_queue_trb(struct xhci_ring *ring, u32 field1,
  178. u32 field2, u32 field3, u32 field4)
  179. {
  180. union xhci_trb *trb, *next;
  181. trb = ring->enqueue;
  182. trb->generic.field[0] = cpu_to_le32(field1);
  183. trb->generic.field[1] = cpu_to_le32(field2);
  184. trb->generic.field[2] = cpu_to_le32(field3);
  185. trb->generic.field[3] = cpu_to_le32(field4);
  186. trace_xhci_dbc_gadget_ep_queue(ring, &trb->generic);
  187. ring->num_trbs_free--;
  188. next = ++(ring->enqueue);
  189. if (TRB_TYPE_LINK_LE32(next->link.control)) {
  190. next->link.control ^= cpu_to_le32(TRB_CYCLE);
  191. ring->enqueue = ring->enq_seg->trbs;
  192. ring->cycle_state ^= 1;
  193. }
  194. }
  195. static int xhci_dbc_queue_bulk_tx(struct dbc_ep *dep,
  196. struct dbc_request *req)
  197. {
  198. u64 addr;
  199. union xhci_trb *trb;
  200. unsigned int num_trbs;
  201. struct xhci_dbc *dbc = dep->dbc;
  202. struct xhci_ring *ring = dep->ring;
  203. u32 length, control, cycle;
  204. num_trbs = count_trbs(req->dma, req->length);
  205. WARN_ON(num_trbs != 1);
  206. if (ring->num_trbs_free < num_trbs)
  207. return -EBUSY;
  208. addr = req->dma;
  209. trb = ring->enqueue;
  210. cycle = ring->cycle_state;
  211. length = TRB_LEN(req->length);
  212. control = TRB_TYPE(TRB_NORMAL) | TRB_IOC;
  213. if (cycle)
  214. control &= cpu_to_le32(~TRB_CYCLE);
  215. else
  216. control |= cpu_to_le32(TRB_CYCLE);
  217. req->trb = ring->enqueue;
  218. req->trb_dma = xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue);
  219. xhci_dbc_queue_trb(ring,
  220. lower_32_bits(addr),
  221. upper_32_bits(addr),
  222. length, control);
  223. /*
  224. * Add a barrier between writes of trb fields and flipping
  225. * the cycle bit:
  226. */
  227. wmb();
  228. if (cycle)
  229. trb->generic.field[3] |= cpu_to_le32(TRB_CYCLE);
  230. else
  231. trb->generic.field[3] &= cpu_to_le32(~TRB_CYCLE);
  232. writel(DBC_DOOR_BELL_TARGET(dep->direction), &dbc->regs->doorbell);
  233. return 0;
  234. }
  235. static int
  236. dbc_ep_do_queue(struct dbc_ep *dep, struct dbc_request *req)
  237. {
  238. int ret;
  239. struct device *dev;
  240. struct xhci_dbc *dbc = dep->dbc;
  241. struct xhci_hcd *xhci = dbc->xhci;
  242. dev = xhci_to_hcd(xhci)->self.sysdev;
  243. if (!req->length || !req->buf)
  244. return -EINVAL;
  245. req->actual = 0;
  246. req->status = -EINPROGRESS;
  247. req->dma = dma_map_single(dev,
  248. req->buf,
  249. req->length,
  250. dbc_ep_dma_direction(dep));
  251. if (dma_mapping_error(dev, req->dma)) {
  252. xhci_err(xhci, "failed to map buffer\n");
  253. return -EFAULT;
  254. }
  255. ret = xhci_dbc_queue_bulk_tx(dep, req);
  256. if (ret) {
  257. xhci_err(xhci, "failed to queue trbs\n");
  258. dma_unmap_single(dev,
  259. req->dma,
  260. req->length,
  261. dbc_ep_dma_direction(dep));
  262. return -EFAULT;
  263. }
  264. list_add_tail(&req->list_pending, &dep->list_pending);
  265. return 0;
  266. }
  267. int dbc_ep_queue(struct dbc_ep *dep, struct dbc_request *req,
  268. gfp_t gfp_flags)
  269. {
  270. unsigned long flags;
  271. struct xhci_dbc *dbc = dep->dbc;
  272. int ret = -ESHUTDOWN;
  273. spin_lock_irqsave(&dbc->lock, flags);
  274. if (dbc->state == DS_CONFIGURED)
  275. ret = dbc_ep_do_queue(dep, req);
  276. spin_unlock_irqrestore(&dbc->lock, flags);
  277. mod_delayed_work(system_wq, &dbc->event_work, 0);
  278. trace_xhci_dbc_queue_request(req);
  279. return ret;
  280. }
  281. static inline void xhci_dbc_do_eps_init(struct xhci_hcd *xhci, bool direction)
  282. {
  283. struct dbc_ep *dep;
  284. struct xhci_dbc *dbc = xhci->dbc;
  285. dep = &dbc->eps[direction];
  286. dep->dbc = dbc;
  287. dep->direction = direction;
  288. dep->ring = direction ? dbc->ring_in : dbc->ring_out;
  289. INIT_LIST_HEAD(&dep->list_pending);
  290. }
  291. static void xhci_dbc_eps_init(struct xhci_hcd *xhci)
  292. {
  293. xhci_dbc_do_eps_init(xhci, BULK_OUT);
  294. xhci_dbc_do_eps_init(xhci, BULK_IN);
  295. }
  296. static void xhci_dbc_eps_exit(struct xhci_hcd *xhci)
  297. {
  298. struct xhci_dbc *dbc = xhci->dbc;
  299. memset(dbc->eps, 0, sizeof(struct dbc_ep) * ARRAY_SIZE(dbc->eps));
  300. }
  301. static int xhci_dbc_mem_init(struct xhci_hcd *xhci, gfp_t flags)
  302. {
  303. int ret;
  304. dma_addr_t deq;
  305. u32 string_length;
  306. struct xhci_dbc *dbc = xhci->dbc;
  307. /* Allocate various rings for events and transfers: */
  308. dbc->ring_evt = xhci_ring_alloc(xhci, 1, 1, TYPE_EVENT, 0, flags);
  309. if (!dbc->ring_evt)
  310. goto evt_fail;
  311. dbc->ring_in = xhci_ring_alloc(xhci, 1, 1, TYPE_BULK, 0, flags);
  312. if (!dbc->ring_in)
  313. goto in_fail;
  314. dbc->ring_out = xhci_ring_alloc(xhci, 1, 1, TYPE_BULK, 0, flags);
  315. if (!dbc->ring_out)
  316. goto out_fail;
  317. /* Allocate and populate ERST: */
  318. ret = xhci_alloc_erst(xhci, dbc->ring_evt, &dbc->erst, flags);
  319. if (ret)
  320. goto erst_fail;
  321. /* Allocate context data structure: */
  322. dbc->ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_DEVICE, flags);
  323. if (!dbc->ctx)
  324. goto ctx_fail;
  325. /* Allocate the string table: */
  326. dbc->string_size = sizeof(struct dbc_str_descs);
  327. dbc->string = dbc_dma_alloc_coherent(xhci,
  328. dbc->string_size,
  329. &dbc->string_dma,
  330. flags);
  331. if (!dbc->string)
  332. goto string_fail;
  333. /* Setup ERST register: */
  334. writel(dbc->erst.erst_size, &dbc->regs->ersts);
  335. xhci_write_64(xhci, dbc->erst.erst_dma_addr, &dbc->regs->erstba);
  336. deq = xhci_trb_virt_to_dma(dbc->ring_evt->deq_seg,
  337. dbc->ring_evt->dequeue);
  338. xhci_write_64(xhci, deq, &dbc->regs->erdp);
  339. /* Setup strings and contexts: */
  340. string_length = xhci_dbc_populate_strings(dbc->string);
  341. xhci_dbc_init_contexts(xhci, string_length);
  342. mmiowb();
  343. xhci_dbc_eps_init(xhci);
  344. dbc->state = DS_INITIALIZED;
  345. return 0;
  346. string_fail:
  347. xhci_free_container_ctx(xhci, dbc->ctx);
  348. dbc->ctx = NULL;
  349. ctx_fail:
  350. xhci_free_erst(xhci, &dbc->erst);
  351. erst_fail:
  352. xhci_ring_free(xhci, dbc->ring_out);
  353. dbc->ring_out = NULL;
  354. out_fail:
  355. xhci_ring_free(xhci, dbc->ring_in);
  356. dbc->ring_in = NULL;
  357. in_fail:
  358. xhci_ring_free(xhci, dbc->ring_evt);
  359. dbc->ring_evt = NULL;
  360. evt_fail:
  361. return -ENOMEM;
  362. }
  363. static void xhci_dbc_mem_cleanup(struct xhci_hcd *xhci)
  364. {
  365. struct xhci_dbc *dbc = xhci->dbc;
  366. if (!dbc)
  367. return;
  368. xhci_dbc_eps_exit(xhci);
  369. if (dbc->string) {
  370. dbc_dma_free_coherent(xhci,
  371. dbc->string_size,
  372. dbc->string, dbc->string_dma);
  373. dbc->string = NULL;
  374. }
  375. xhci_free_container_ctx(xhci, dbc->ctx);
  376. dbc->ctx = NULL;
  377. xhci_free_erst(xhci, &dbc->erst);
  378. xhci_ring_free(xhci, dbc->ring_out);
  379. xhci_ring_free(xhci, dbc->ring_in);
  380. xhci_ring_free(xhci, dbc->ring_evt);
  381. dbc->ring_in = NULL;
  382. dbc->ring_out = NULL;
  383. dbc->ring_evt = NULL;
  384. }
  385. static int xhci_do_dbc_start(struct xhci_hcd *xhci)
  386. {
  387. int ret;
  388. u32 ctrl;
  389. struct xhci_dbc *dbc = xhci->dbc;
  390. if (dbc->state != DS_DISABLED)
  391. return -EINVAL;
  392. writel(0, &dbc->regs->control);
  393. ret = xhci_handshake(&dbc->regs->control,
  394. DBC_CTRL_DBC_ENABLE,
  395. 0, 1000);
  396. if (ret)
  397. return ret;
  398. ret = xhci_dbc_mem_init(xhci, GFP_ATOMIC);
  399. if (ret)
  400. return ret;
  401. ctrl = readl(&dbc->regs->control);
  402. writel(ctrl | DBC_CTRL_DBC_ENABLE | DBC_CTRL_PORT_ENABLE,
  403. &dbc->regs->control);
  404. ret = xhci_handshake(&dbc->regs->control,
  405. DBC_CTRL_DBC_ENABLE,
  406. DBC_CTRL_DBC_ENABLE, 1000);
  407. if (ret)
  408. return ret;
  409. dbc->state = DS_ENABLED;
  410. return 0;
  411. }
  412. static void xhci_do_dbc_stop(struct xhci_hcd *xhci)
  413. {
  414. struct xhci_dbc *dbc = xhci->dbc;
  415. if (dbc->state == DS_DISABLED)
  416. return;
  417. writel(0, &dbc->regs->control);
  418. xhci_dbc_mem_cleanup(xhci);
  419. dbc->state = DS_DISABLED;
  420. }
  421. static int xhci_dbc_start(struct xhci_hcd *xhci)
  422. {
  423. int ret;
  424. unsigned long flags;
  425. struct xhci_dbc *dbc = xhci->dbc;
  426. WARN_ON(!dbc);
  427. pm_runtime_get_sync(xhci_to_hcd(xhci)->self.controller);
  428. spin_lock_irqsave(&dbc->lock, flags);
  429. ret = xhci_do_dbc_start(xhci);
  430. spin_unlock_irqrestore(&dbc->lock, flags);
  431. if (ret) {
  432. pm_runtime_put(xhci_to_hcd(xhci)->self.controller);
  433. return ret;
  434. }
  435. return mod_delayed_work(system_wq, &dbc->event_work, 1);
  436. }
  437. static void xhci_dbc_stop(struct xhci_hcd *xhci)
  438. {
  439. unsigned long flags;
  440. struct xhci_dbc *dbc = xhci->dbc;
  441. struct dbc_port *port = &dbc->port;
  442. WARN_ON(!dbc);
  443. cancel_delayed_work_sync(&dbc->event_work);
  444. if (port->registered)
  445. xhci_dbc_tty_unregister_device(xhci);
  446. spin_lock_irqsave(&dbc->lock, flags);
  447. xhci_do_dbc_stop(xhci);
  448. spin_unlock_irqrestore(&dbc->lock, flags);
  449. pm_runtime_put_sync(xhci_to_hcd(xhci)->self.controller);
  450. }
  451. static void
  452. dbc_handle_port_status(struct xhci_hcd *xhci, union xhci_trb *event)
  453. {
  454. u32 portsc;
  455. struct xhci_dbc *dbc = xhci->dbc;
  456. portsc = readl(&dbc->regs->portsc);
  457. if (portsc & DBC_PORTSC_CONN_CHANGE)
  458. xhci_info(xhci, "DbC port connect change\n");
  459. if (portsc & DBC_PORTSC_RESET_CHANGE)
  460. xhci_info(xhci, "DbC port reset change\n");
  461. if (portsc & DBC_PORTSC_LINK_CHANGE)
  462. xhci_info(xhci, "DbC port link status change\n");
  463. if (portsc & DBC_PORTSC_CONFIG_CHANGE)
  464. xhci_info(xhci, "DbC config error change\n");
  465. /* Port reset change bit will be cleared in other place: */
  466. writel(portsc & ~DBC_PORTSC_RESET_CHANGE, &dbc->regs->portsc);
  467. }
  468. static void dbc_handle_xfer_event(struct xhci_hcd *xhci, union xhci_trb *event)
  469. {
  470. struct dbc_ep *dep;
  471. struct xhci_ring *ring;
  472. int ep_id;
  473. int status;
  474. u32 comp_code;
  475. size_t remain_length;
  476. struct dbc_request *req = NULL, *r;
  477. comp_code = GET_COMP_CODE(le32_to_cpu(event->generic.field[2]));
  478. remain_length = EVENT_TRB_LEN(le32_to_cpu(event->generic.field[2]));
  479. ep_id = TRB_TO_EP_ID(le32_to_cpu(event->generic.field[3]));
  480. dep = (ep_id == EPID_OUT) ?
  481. get_out_ep(xhci) : get_in_ep(xhci);
  482. ring = dep->ring;
  483. switch (comp_code) {
  484. case COMP_SUCCESS:
  485. remain_length = 0;
  486. /* FALLTHROUGH */
  487. case COMP_SHORT_PACKET:
  488. status = 0;
  489. break;
  490. case COMP_TRB_ERROR:
  491. case COMP_BABBLE_DETECTED_ERROR:
  492. case COMP_USB_TRANSACTION_ERROR:
  493. case COMP_STALL_ERROR:
  494. xhci_warn(xhci, "tx error %d detected\n", comp_code);
  495. status = -comp_code;
  496. break;
  497. default:
  498. xhci_err(xhci, "unknown tx error %d\n", comp_code);
  499. status = -comp_code;
  500. break;
  501. }
  502. /* Match the pending request: */
  503. list_for_each_entry(r, &dep->list_pending, list_pending) {
  504. if (r->trb_dma == event->trans_event.buffer) {
  505. req = r;
  506. break;
  507. }
  508. }
  509. if (!req) {
  510. xhci_warn(xhci, "no matched request\n");
  511. return;
  512. }
  513. trace_xhci_dbc_handle_transfer(ring, &req->trb->generic);
  514. ring->num_trbs_free++;
  515. req->actual = req->length - remain_length;
  516. xhci_dbc_giveback(req, status);
  517. }
  518. static enum evtreturn xhci_dbc_do_handle_events(struct xhci_dbc *dbc)
  519. {
  520. dma_addr_t deq;
  521. struct dbc_ep *dep;
  522. union xhci_trb *evt;
  523. u32 ctrl, portsc;
  524. struct xhci_hcd *xhci = dbc->xhci;
  525. bool update_erdp = false;
  526. /* DbC state machine: */
  527. switch (dbc->state) {
  528. case DS_DISABLED:
  529. case DS_INITIALIZED:
  530. return EVT_ERR;
  531. case DS_ENABLED:
  532. portsc = readl(&dbc->regs->portsc);
  533. if (portsc & DBC_PORTSC_CONN_STATUS) {
  534. dbc->state = DS_CONNECTED;
  535. xhci_info(xhci, "DbC connected\n");
  536. }
  537. return EVT_DONE;
  538. case DS_CONNECTED:
  539. ctrl = readl(&dbc->regs->control);
  540. if (ctrl & DBC_CTRL_DBC_RUN) {
  541. dbc->state = DS_CONFIGURED;
  542. xhci_info(xhci, "DbC configured\n");
  543. portsc = readl(&dbc->regs->portsc);
  544. writel(portsc, &dbc->regs->portsc);
  545. return EVT_GSER;
  546. }
  547. return EVT_DONE;
  548. case DS_CONFIGURED:
  549. /* Handle cable unplug event: */
  550. portsc = readl(&dbc->regs->portsc);
  551. if (!(portsc & DBC_PORTSC_PORT_ENABLED) &&
  552. !(portsc & DBC_PORTSC_CONN_STATUS)) {
  553. xhci_info(xhci, "DbC cable unplugged\n");
  554. dbc->state = DS_ENABLED;
  555. xhci_dbc_flush_reqests(dbc);
  556. return EVT_DISC;
  557. }
  558. /* Handle debug port reset event: */
  559. if (portsc & DBC_PORTSC_RESET_CHANGE) {
  560. xhci_info(xhci, "DbC port reset\n");
  561. writel(portsc, &dbc->regs->portsc);
  562. dbc->state = DS_ENABLED;
  563. xhci_dbc_flush_reqests(dbc);
  564. return EVT_DISC;
  565. }
  566. /* Handle endpoint stall event: */
  567. ctrl = readl(&dbc->regs->control);
  568. if ((ctrl & DBC_CTRL_HALT_IN_TR) ||
  569. (ctrl & DBC_CTRL_HALT_OUT_TR)) {
  570. xhci_info(xhci, "DbC Endpoint stall\n");
  571. dbc->state = DS_STALLED;
  572. if (ctrl & DBC_CTRL_HALT_IN_TR) {
  573. dep = get_in_ep(xhci);
  574. xhci_dbc_flush_endpoint_requests(dep);
  575. }
  576. if (ctrl & DBC_CTRL_HALT_OUT_TR) {
  577. dep = get_out_ep(xhci);
  578. xhci_dbc_flush_endpoint_requests(dep);
  579. }
  580. return EVT_DONE;
  581. }
  582. /* Clear DbC run change bit: */
  583. if (ctrl & DBC_CTRL_DBC_RUN_CHANGE) {
  584. writel(ctrl, &dbc->regs->control);
  585. ctrl = readl(&dbc->regs->control);
  586. }
  587. break;
  588. case DS_STALLED:
  589. ctrl = readl(&dbc->regs->control);
  590. if (!(ctrl & DBC_CTRL_HALT_IN_TR) &&
  591. !(ctrl & DBC_CTRL_HALT_OUT_TR) &&
  592. (ctrl & DBC_CTRL_DBC_RUN)) {
  593. dbc->state = DS_CONFIGURED;
  594. break;
  595. }
  596. return EVT_DONE;
  597. default:
  598. xhci_err(xhci, "Unknown DbC state %d\n", dbc->state);
  599. break;
  600. }
  601. /* Handle the events in the event ring: */
  602. evt = dbc->ring_evt->dequeue;
  603. while ((le32_to_cpu(evt->event_cmd.flags) & TRB_CYCLE) ==
  604. dbc->ring_evt->cycle_state) {
  605. /*
  606. * Add a barrier between reading the cycle flag and any
  607. * reads of the event's flags/data below:
  608. */
  609. rmb();
  610. trace_xhci_dbc_handle_event(dbc->ring_evt, &evt->generic);
  611. switch (le32_to_cpu(evt->event_cmd.flags) & TRB_TYPE_BITMASK) {
  612. case TRB_TYPE(TRB_PORT_STATUS):
  613. dbc_handle_port_status(xhci, evt);
  614. break;
  615. case TRB_TYPE(TRB_TRANSFER):
  616. dbc_handle_xfer_event(xhci, evt);
  617. break;
  618. default:
  619. break;
  620. }
  621. inc_deq(xhci, dbc->ring_evt);
  622. evt = dbc->ring_evt->dequeue;
  623. update_erdp = true;
  624. }
  625. /* Update event ring dequeue pointer: */
  626. if (update_erdp) {
  627. deq = xhci_trb_virt_to_dma(dbc->ring_evt->deq_seg,
  628. dbc->ring_evt->dequeue);
  629. xhci_write_64(xhci, deq, &dbc->regs->erdp);
  630. }
  631. return EVT_DONE;
  632. }
  633. static void xhci_dbc_handle_events(struct work_struct *work)
  634. {
  635. int ret;
  636. enum evtreturn evtr;
  637. struct xhci_dbc *dbc;
  638. unsigned long flags;
  639. struct xhci_hcd *xhci;
  640. dbc = container_of(to_delayed_work(work), struct xhci_dbc, event_work);
  641. xhci = dbc->xhci;
  642. spin_lock_irqsave(&dbc->lock, flags);
  643. evtr = xhci_dbc_do_handle_events(dbc);
  644. spin_unlock_irqrestore(&dbc->lock, flags);
  645. switch (evtr) {
  646. case EVT_GSER:
  647. ret = xhci_dbc_tty_register_device(xhci);
  648. if (ret) {
  649. xhci_err(xhci, "failed to alloc tty device\n");
  650. break;
  651. }
  652. xhci_info(xhci, "DbC now attached to /dev/ttyDBC0\n");
  653. break;
  654. case EVT_DISC:
  655. xhci_dbc_tty_unregister_device(xhci);
  656. break;
  657. case EVT_DONE:
  658. break;
  659. default:
  660. xhci_info(xhci, "stop handling dbc events\n");
  661. return;
  662. }
  663. mod_delayed_work(system_wq, &dbc->event_work, 1);
  664. }
  665. static void xhci_do_dbc_exit(struct xhci_hcd *xhci)
  666. {
  667. unsigned long flags;
  668. spin_lock_irqsave(&xhci->lock, flags);
  669. kfree(xhci->dbc);
  670. xhci->dbc = NULL;
  671. spin_unlock_irqrestore(&xhci->lock, flags);
  672. }
  673. static int xhci_do_dbc_init(struct xhci_hcd *xhci)
  674. {
  675. u32 reg;
  676. struct xhci_dbc *dbc;
  677. unsigned long flags;
  678. void __iomem *base;
  679. int dbc_cap_offs;
  680. base = &xhci->cap_regs->hc_capbase;
  681. dbc_cap_offs = xhci_find_next_ext_cap(base, 0, XHCI_EXT_CAPS_DEBUG);
  682. if (!dbc_cap_offs)
  683. return -ENODEV;
  684. dbc = kzalloc(sizeof(*dbc), GFP_KERNEL);
  685. if (!dbc)
  686. return -ENOMEM;
  687. dbc->regs = base + dbc_cap_offs;
  688. /* We will avoid using DbC in xhci driver if it's in use. */
  689. reg = readl(&dbc->regs->control);
  690. if (reg & DBC_CTRL_DBC_ENABLE) {
  691. kfree(dbc);
  692. return -EBUSY;
  693. }
  694. spin_lock_irqsave(&xhci->lock, flags);
  695. if (xhci->dbc) {
  696. spin_unlock_irqrestore(&xhci->lock, flags);
  697. kfree(dbc);
  698. return -EBUSY;
  699. }
  700. xhci->dbc = dbc;
  701. spin_unlock_irqrestore(&xhci->lock, flags);
  702. dbc->xhci = xhci;
  703. INIT_DELAYED_WORK(&dbc->event_work, xhci_dbc_handle_events);
  704. spin_lock_init(&dbc->lock);
  705. return 0;
  706. }
  707. static ssize_t dbc_show(struct device *dev,
  708. struct device_attribute *attr,
  709. char *buf)
  710. {
  711. const char *p;
  712. struct xhci_dbc *dbc;
  713. struct xhci_hcd *xhci;
  714. xhci = hcd_to_xhci(dev_get_drvdata(dev));
  715. dbc = xhci->dbc;
  716. switch (dbc->state) {
  717. case DS_DISABLED:
  718. p = "disabled";
  719. break;
  720. case DS_INITIALIZED:
  721. p = "initialized";
  722. break;
  723. case DS_ENABLED:
  724. p = "enabled";
  725. break;
  726. case DS_CONNECTED:
  727. p = "connected";
  728. break;
  729. case DS_CONFIGURED:
  730. p = "configured";
  731. break;
  732. case DS_STALLED:
  733. p = "stalled";
  734. break;
  735. default:
  736. p = "unknown";
  737. }
  738. return sprintf(buf, "%s\n", p);
  739. }
  740. static ssize_t dbc_store(struct device *dev,
  741. struct device_attribute *attr,
  742. const char *buf, size_t count)
  743. {
  744. struct xhci_dbc *dbc;
  745. struct xhci_hcd *xhci;
  746. xhci = hcd_to_xhci(dev_get_drvdata(dev));
  747. dbc = xhci->dbc;
  748. if (!strncmp(buf, "enable", 6))
  749. xhci_dbc_start(xhci);
  750. else if (!strncmp(buf, "disable", 7))
  751. xhci_dbc_stop(xhci);
  752. else
  753. return -EINVAL;
  754. return count;
  755. }
  756. static DEVICE_ATTR_RW(dbc);
  757. int xhci_dbc_init(struct xhci_hcd *xhci)
  758. {
  759. int ret;
  760. struct device *dev = xhci_to_hcd(xhci)->self.controller;
  761. ret = xhci_do_dbc_init(xhci);
  762. if (ret)
  763. goto init_err3;
  764. ret = xhci_dbc_tty_register_driver(xhci);
  765. if (ret)
  766. goto init_err2;
  767. ret = device_create_file(dev, &dev_attr_dbc);
  768. if (ret)
  769. goto init_err1;
  770. return 0;
  771. init_err1:
  772. xhci_dbc_tty_unregister_driver();
  773. init_err2:
  774. xhci_do_dbc_exit(xhci);
  775. init_err3:
  776. return ret;
  777. }
  778. void xhci_dbc_exit(struct xhci_hcd *xhci)
  779. {
  780. struct device *dev = xhci_to_hcd(xhci)->self.controller;
  781. if (!xhci->dbc)
  782. return;
  783. device_remove_file(dev, &dev_attr_dbc);
  784. xhci_dbc_tty_unregister_driver();
  785. xhci_dbc_stop(xhci);
  786. xhci_do_dbc_exit(xhci);
  787. }
  788. #ifdef CONFIG_PM
  789. int xhci_dbc_suspend(struct xhci_hcd *xhci)
  790. {
  791. struct xhci_dbc *dbc = xhci->dbc;
  792. if (!dbc)
  793. return 0;
  794. if (dbc->state == DS_CONFIGURED)
  795. dbc->resume_required = 1;
  796. xhci_dbc_stop(xhci);
  797. return 0;
  798. }
  799. int xhci_dbc_resume(struct xhci_hcd *xhci)
  800. {
  801. int ret = 0;
  802. struct xhci_dbc *dbc = xhci->dbc;
  803. if (!dbc)
  804. return 0;
  805. if (dbc->resume_required) {
  806. dbc->resume_required = 0;
  807. xhci_dbc_start(xhci);
  808. }
  809. return ret;
  810. }
  811. #endif /* CONFIG_PM */