whc-rc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * Wireless Host Controller: Radio Control Interface (WHCI v0.95[2.3])
  3. * Radio Control command/event transport to the UWB stack
  4. *
  5. * Copyright (C) 2005-2006 Intel Corporation
  6. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. * 02110-1301, USA.
  21. *
  22. *
  23. * Initialize and hook up the Radio Control interface.
  24. *
  25. * For each device probed, creates an 'struct whcrc' which contains
  26. * just the representation of the UWB Radio Controller, and the logic
  27. * for reading notifications and passing them to the UWB Core.
  28. *
  29. * So we initialize all of those, register the UWB Radio Controller
  30. * and setup the notification/event handle to pipe the notifications
  31. * to the UWB management Daemon.
  32. *
  33. * Once uwb_rc_add() is called, the UWB stack takes control, resets
  34. * the radio and readies the device to take commands the UWB
  35. * API/user-space.
  36. *
  37. * Note this driver is just a transport driver; the commands are
  38. * formed at the UWB stack and given to this driver who will deliver
  39. * them to the hw and transfer the replies/notifications back to the
  40. * UWB stack through the UWB daemon (UWBD).
  41. */
  42. #include <linux/version.h>
  43. #include <linux/init.h>
  44. #include <linux/module.h>
  45. #include <linux/pci.h>
  46. #include <linux/dma-mapping.h>
  47. #include <linux/interrupt.h>
  48. #include <linux/workqueue.h>
  49. #include <linux/uwb.h>
  50. #include <linux/uwb/whci.h>
  51. #include <linux/uwb/umc.h>
  52. #include "uwb-internal.h"
  53. #define D_LOCAL 0
  54. #include <linux/uwb/debug.h>
  55. /**
  56. * Descriptor for an instance of the UWB Radio Control Driver that
  57. * attaches to the URC interface of the WHCI PCI card.
  58. *
  59. * Unless there is a lock specific to the 'data members', all access
  60. * is protected by uwb_rc->mutex.
  61. */
  62. struct whcrc {
  63. struct umc_dev *umc_dev;
  64. struct uwb_rc *uwb_rc; /* UWB host controller */
  65. unsigned long area;
  66. void __iomem *rc_base;
  67. size_t rc_len;
  68. spinlock_t irq_lock;
  69. void *evt_buf, *cmd_buf;
  70. dma_addr_t evt_dma_buf, cmd_dma_buf;
  71. wait_queue_head_t cmd_wq;
  72. struct work_struct event_work;
  73. };
  74. /**
  75. * Execute an UWB RC command on WHCI/RC
  76. *
  77. * @rc: Instance of a Radio Controller that is a whcrc
  78. * @cmd: Buffer containing the RCCB and payload to execute
  79. * @cmd_size: Size of the command buffer.
  80. *
  81. * We copy the command into whcrc->cmd_buf (as it is pretty and
  82. * aligned`and physically contiguous) and then press the right keys in
  83. * the controller's URCCMD register to get it to read it. We might
  84. * have to wait for the cmd_sem to be open to us.
  85. *
  86. * NOTE: rc's mutex has to be locked
  87. */
  88. static int whcrc_cmd(struct uwb_rc *uwb_rc,
  89. const struct uwb_rccb *cmd, size_t cmd_size)
  90. {
  91. int result = 0;
  92. struct whcrc *whcrc = uwb_rc->priv;
  93. struct device *dev = &whcrc->umc_dev->dev;
  94. u32 urccmd;
  95. d_fnstart(3, dev, "(%p, %p, %zu)\n", uwb_rc, cmd, cmd_size);
  96. might_sleep();
  97. if (cmd_size >= 4096) {
  98. result = -E2BIG;
  99. goto error;
  100. }
  101. /*
  102. * If the URC is halted, then the hardware has reset itself.
  103. * Attempt to recover by restarting the device and then return
  104. * an error as it's likely that the current command isn't
  105. * valid for a newly started RC.
  106. */
  107. if (le_readl(whcrc->rc_base + URCSTS) & URCSTS_HALTED) {
  108. dev_err(dev, "requesting reset of halted radio controller\n");
  109. uwb_rc_reset_all(uwb_rc);
  110. result = -EIO;
  111. goto error;
  112. }
  113. result = wait_event_timeout(whcrc->cmd_wq,
  114. !(le_readl(whcrc->rc_base + URCCMD) & URCCMD_ACTIVE), HZ/2);
  115. if (result == 0) {
  116. dev_err(dev, "device is not ready to execute commands\n");
  117. result = -ETIMEDOUT;
  118. goto error;
  119. }
  120. memmove(whcrc->cmd_buf, cmd, cmd_size);
  121. le_writeq(whcrc->cmd_dma_buf, whcrc->rc_base + URCCMDADDR);
  122. spin_lock(&whcrc->irq_lock);
  123. urccmd = le_readl(whcrc->rc_base + URCCMD);
  124. urccmd &= ~(URCCMD_EARV | URCCMD_SIZE_MASK);
  125. le_writel(urccmd | URCCMD_ACTIVE | URCCMD_IWR | cmd_size,
  126. whcrc->rc_base + URCCMD);
  127. spin_unlock(&whcrc->irq_lock);
  128. error:
  129. d_fnend(3, dev, "(%p, %p, %zu) = %d\n",
  130. uwb_rc, cmd, cmd_size, result);
  131. return result;
  132. }
  133. static int whcrc_reset(struct uwb_rc *rc)
  134. {
  135. struct whcrc *whcrc = rc->priv;
  136. return umc_controller_reset(whcrc->umc_dev);
  137. }
  138. /**
  139. * Reset event reception mechanism and tell hw we are ready to get more
  140. *
  141. * We have read all the events in the event buffer, so we are ready to
  142. * reset it to the beginning.
  143. *
  144. * This is only called during initialization or after an event buffer
  145. * has been retired. This means we can be sure that event processing
  146. * is disabled and it's safe to update the URCEVTADDR register.
  147. *
  148. * There's no need to wait for the event processing to start as the
  149. * URC will not clear URCCMD_ACTIVE until (internal) event buffer
  150. * space is available.
  151. */
  152. static
  153. void whcrc_enable_events(struct whcrc *whcrc)
  154. {
  155. struct device *dev = &whcrc->umc_dev->dev;
  156. u32 urccmd;
  157. d_fnstart(4, dev, "(whcrc %p)\n", whcrc);
  158. le_writeq(whcrc->evt_dma_buf, whcrc->rc_base + URCEVTADDR);
  159. spin_lock(&whcrc->irq_lock);
  160. urccmd = le_readl(whcrc->rc_base + URCCMD) & ~URCCMD_ACTIVE;
  161. le_writel(urccmd | URCCMD_EARV, whcrc->rc_base + URCCMD);
  162. spin_unlock(&whcrc->irq_lock);
  163. d_fnend(4, dev, "(whcrc %p) = void\n", whcrc);
  164. }
  165. static void whcrc_event_work(struct work_struct *work)
  166. {
  167. struct whcrc *whcrc = container_of(work, struct whcrc, event_work);
  168. struct device *dev = &whcrc->umc_dev->dev;
  169. size_t size;
  170. u64 urcevtaddr;
  171. urcevtaddr = le_readq(whcrc->rc_base + URCEVTADDR);
  172. size = urcevtaddr & URCEVTADDR_OFFSET_MASK;
  173. d_printf(3, dev, "received %zu octet event\n", size);
  174. d_dump(4, dev, whcrc->evt_buf, size > 32 ? 32 : size);
  175. uwb_rc_neh_grok(whcrc->uwb_rc, whcrc->evt_buf, size);
  176. whcrc_enable_events(whcrc);
  177. }
  178. /**
  179. * Catch interrupts?
  180. *
  181. * We ack inmediately (and expect the hw to do the right thing and
  182. * raise another IRQ if things have changed :)
  183. */
  184. static
  185. irqreturn_t whcrc_irq_cb(int irq, void *_whcrc)
  186. {
  187. struct whcrc *whcrc = _whcrc;
  188. struct device *dev = &whcrc->umc_dev->dev;
  189. u32 urcsts;
  190. d_fnstart(4, dev, "irq %d _whcrc %p)\n", irq, _whcrc);
  191. urcsts = le_readl(whcrc->rc_base + URCSTS);
  192. if (!(urcsts & URCSTS_INT_MASK))
  193. return IRQ_NONE;
  194. le_writel(urcsts & URCSTS_INT_MASK, whcrc->rc_base + URCSTS);
  195. d_printf(4, dev, "acked 0x%08x, urcsts 0x%08x\n",
  196. le_readl(whcrc->rc_base + URCSTS), urcsts);
  197. if (whcrc->uwb_rc == NULL) {
  198. if (printk_ratelimit())
  199. dev_dbg(dev, "Received interrupt when not yet "
  200. "ready!\n");
  201. goto out;
  202. }
  203. if (urcsts & URCSTS_HSE) {
  204. dev_err(dev, "host system error -- hardware halted\n");
  205. /* FIXME: do something sensible here */
  206. goto out;
  207. }
  208. if (urcsts & URCSTS_ER) {
  209. d_printf(3, dev, "ER: event ready\n");
  210. schedule_work(&whcrc->event_work);
  211. }
  212. if (urcsts & URCSTS_RCI) {
  213. d_printf(3, dev, "RCI: ready to execute another command\n");
  214. wake_up_all(&whcrc->cmd_wq);
  215. }
  216. out:
  217. return IRQ_HANDLED;
  218. }
  219. /**
  220. * Initialize a UMC RC interface: map regions, get (shared) IRQ
  221. */
  222. static
  223. int whcrc_setup_rc_umc(struct whcrc *whcrc)
  224. {
  225. int result = 0;
  226. struct device *dev = &whcrc->umc_dev->dev;
  227. struct umc_dev *umc_dev = whcrc->umc_dev;
  228. whcrc->area = umc_dev->resource.start;
  229. whcrc->rc_len = umc_dev->resource.end - umc_dev->resource.start + 1;
  230. result = -EBUSY;
  231. if (request_mem_region(whcrc->area, whcrc->rc_len, KBUILD_MODNAME)
  232. == NULL) {
  233. dev_err(dev, "can't request URC region (%zu bytes @ 0x%lx): %d\n",
  234. whcrc->rc_len, whcrc->area, result);
  235. goto error_request_region;
  236. }
  237. whcrc->rc_base = ioremap_nocache(whcrc->area, whcrc->rc_len);
  238. if (whcrc->rc_base == NULL) {
  239. dev_err(dev, "can't ioremap registers (%zu bytes @ 0x%lx): %d\n",
  240. whcrc->rc_len, whcrc->area, result);
  241. goto error_ioremap_nocache;
  242. }
  243. result = request_irq(umc_dev->irq, whcrc_irq_cb, IRQF_SHARED,
  244. KBUILD_MODNAME, whcrc);
  245. if (result < 0) {
  246. dev_err(dev, "can't allocate IRQ %d: %d\n",
  247. umc_dev->irq, result);
  248. goto error_request_irq;
  249. }
  250. result = -ENOMEM;
  251. whcrc->cmd_buf = dma_alloc_coherent(&umc_dev->dev, PAGE_SIZE,
  252. &whcrc->cmd_dma_buf, GFP_KERNEL);
  253. if (whcrc->cmd_buf == NULL) {
  254. dev_err(dev, "Can't allocate cmd transfer buffer\n");
  255. goto error_cmd_buffer;
  256. }
  257. whcrc->evt_buf = dma_alloc_coherent(&umc_dev->dev, PAGE_SIZE,
  258. &whcrc->evt_dma_buf, GFP_KERNEL);
  259. if (whcrc->evt_buf == NULL) {
  260. dev_err(dev, "Can't allocate evt transfer buffer\n");
  261. goto error_evt_buffer;
  262. }
  263. d_printf(3, dev, "UWB RC Interface: %zu bytes at 0x%p, irq %u\n",
  264. whcrc->rc_len, whcrc->rc_base, umc_dev->irq);
  265. return 0;
  266. error_evt_buffer:
  267. dma_free_coherent(&umc_dev->dev, PAGE_SIZE, whcrc->cmd_buf,
  268. whcrc->cmd_dma_buf);
  269. error_cmd_buffer:
  270. free_irq(umc_dev->irq, whcrc);
  271. error_request_irq:
  272. iounmap(whcrc->rc_base);
  273. error_ioremap_nocache:
  274. release_mem_region(whcrc->area, whcrc->rc_len);
  275. error_request_region:
  276. return result;
  277. }
  278. /**
  279. * Release RC's UMC resources
  280. */
  281. static
  282. void whcrc_release_rc_umc(struct whcrc *whcrc)
  283. {
  284. struct umc_dev *umc_dev = whcrc->umc_dev;
  285. dma_free_coherent(&umc_dev->dev, PAGE_SIZE, whcrc->evt_buf,
  286. whcrc->evt_dma_buf);
  287. dma_free_coherent(&umc_dev->dev, PAGE_SIZE, whcrc->cmd_buf,
  288. whcrc->cmd_dma_buf);
  289. free_irq(umc_dev->irq, whcrc);
  290. iounmap(whcrc->rc_base);
  291. release_mem_region(whcrc->area, whcrc->rc_len);
  292. }
  293. /**
  294. * whcrc_start_rc - start a WHCI radio controller
  295. * @whcrc: the radio controller to start
  296. *
  297. * Reset the UMC device, start the radio controller, enable events and
  298. * finally enable interrupts.
  299. */
  300. static int whcrc_start_rc(struct uwb_rc *rc)
  301. {
  302. struct whcrc *whcrc = rc->priv;
  303. int result = 0;
  304. struct device *dev = &whcrc->umc_dev->dev;
  305. unsigned long start, duration;
  306. /* Reset the thing */
  307. le_writel(URCCMD_RESET, whcrc->rc_base + URCCMD);
  308. if (d_test(3))
  309. start = jiffies;
  310. if (whci_wait_for(dev, whcrc->rc_base + URCCMD, URCCMD_RESET, 0,
  311. 5000, "device to reset at init") < 0) {
  312. result = -EBUSY;
  313. goto error;
  314. } else if (d_test(3)) {
  315. duration = jiffies - start;
  316. if (duration > msecs_to_jiffies(40))
  317. dev_err(dev, "Device took %ums to "
  318. "reset. MAX expected: 40ms\n",
  319. jiffies_to_msecs(duration));
  320. }
  321. /* Set the event buffer, start the controller (enable IRQs later) */
  322. le_writel(0, whcrc->rc_base + URCINTR);
  323. le_writel(URCCMD_RS, whcrc->rc_base + URCCMD);
  324. result = -ETIMEDOUT;
  325. if (d_test(3))
  326. start = jiffies;
  327. if (whci_wait_for(dev, whcrc->rc_base + URCSTS, URCSTS_HALTED, 0,
  328. 5000, "device to start") < 0)
  329. goto error;
  330. if (d_test(3)) {
  331. duration = jiffies - start;
  332. if (duration > msecs_to_jiffies(40))
  333. dev_err(dev, "Device took %ums to start. "
  334. "MAX expected: 40ms\n",
  335. jiffies_to_msecs(duration));
  336. }
  337. whcrc_enable_events(whcrc);
  338. result = 0;
  339. le_writel(URCINTR_EN_ALL, whcrc->rc_base + URCINTR);
  340. error:
  341. return result;
  342. }
  343. /**
  344. * whcrc_stop_rc - stop a WHCI radio controller
  345. * @whcrc: the radio controller to stop
  346. *
  347. * Disable interrupts and cancel any pending event processing work
  348. * before clearing the Run/Stop bit.
  349. */
  350. static
  351. void whcrc_stop_rc(struct uwb_rc *rc)
  352. {
  353. struct whcrc *whcrc = rc->priv;
  354. struct umc_dev *umc_dev = whcrc->umc_dev;
  355. le_writel(0, whcrc->rc_base + URCINTR);
  356. cancel_work_sync(&whcrc->event_work);
  357. le_writel(0, whcrc->rc_base + URCCMD);
  358. whci_wait_for(&umc_dev->dev, whcrc->rc_base + URCSTS,
  359. URCSTS_HALTED, 0, 40, "URCSTS.HALTED");
  360. }
  361. static void whcrc_init(struct whcrc *whcrc)
  362. {
  363. spin_lock_init(&whcrc->irq_lock);
  364. init_waitqueue_head(&whcrc->cmd_wq);
  365. INIT_WORK(&whcrc->event_work, whcrc_event_work);
  366. }
  367. /**
  368. * Initialize the radio controller.
  369. *
  370. * NOTE: we setup whcrc->uwb_rc before calling uwb_rc_add(); in the
  371. * IRQ handler we use that to determine if the hw is ready to
  372. * handle events. Looks like a race condition, but it really is
  373. * not.
  374. */
  375. static
  376. int whcrc_probe(struct umc_dev *umc_dev)
  377. {
  378. int result;
  379. struct uwb_rc *uwb_rc;
  380. struct whcrc *whcrc;
  381. struct device *dev = &umc_dev->dev;
  382. d_fnstart(3, dev, "(umc_dev %p)\n", umc_dev);
  383. result = -ENOMEM;
  384. uwb_rc = uwb_rc_alloc();
  385. if (uwb_rc == NULL) {
  386. dev_err(dev, "unable to allocate RC instance\n");
  387. goto error_rc_alloc;
  388. }
  389. whcrc = kzalloc(sizeof(*whcrc), GFP_KERNEL);
  390. if (whcrc == NULL) {
  391. dev_err(dev, "unable to allocate WHC-RC instance\n");
  392. goto error_alloc;
  393. }
  394. whcrc_init(whcrc);
  395. whcrc->umc_dev = umc_dev;
  396. result = whcrc_setup_rc_umc(whcrc);
  397. if (result < 0) {
  398. dev_err(dev, "Can't setup RC UMC interface: %d\n", result);
  399. goto error_setup_rc_umc;
  400. }
  401. whcrc->uwb_rc = uwb_rc;
  402. uwb_rc->owner = THIS_MODULE;
  403. uwb_rc->cmd = whcrc_cmd;
  404. uwb_rc->reset = whcrc_reset;
  405. uwb_rc->start = whcrc_start_rc;
  406. uwb_rc->stop = whcrc_stop_rc;
  407. result = uwb_rc_add(uwb_rc, dev, whcrc);
  408. if (result < 0)
  409. goto error_rc_add;
  410. umc_set_drvdata(umc_dev, whcrc);
  411. d_fnend(3, dev, "(umc_dev %p) = 0\n", umc_dev);
  412. return 0;
  413. error_rc_add:
  414. whcrc_release_rc_umc(whcrc);
  415. error_setup_rc_umc:
  416. kfree(whcrc);
  417. error_alloc:
  418. uwb_rc_put(uwb_rc);
  419. error_rc_alloc:
  420. d_fnend(3, dev, "(umc_dev %p) = %d\n", umc_dev, result);
  421. return result;
  422. }
  423. /**
  424. * Clean up the radio control resources
  425. *
  426. * When we up the command semaphore, everybody possibly held trying to
  427. * execute a command should be granted entry and then they'll see the
  428. * host is quiescing and up it (so it will chain to the next waiter).
  429. * This should not happen (in any case), as we can only remove when
  430. * there are no handles open...
  431. */
  432. static void whcrc_remove(struct umc_dev *umc_dev)
  433. {
  434. struct whcrc *whcrc = umc_get_drvdata(umc_dev);
  435. struct uwb_rc *uwb_rc = whcrc->uwb_rc;
  436. umc_set_drvdata(umc_dev, NULL);
  437. uwb_rc_rm(uwb_rc);
  438. whcrc_release_rc_umc(whcrc);
  439. kfree(whcrc);
  440. uwb_rc_put(uwb_rc);
  441. d_printf(1, &umc_dev->dev, "freed whcrc %p\n", whcrc);
  442. }
  443. /* PCI device ID's that we handle [so it gets loaded] */
  444. static struct pci_device_id whcrc_id_table[] = {
  445. { PCI_DEVICE_CLASS(PCI_CLASS_WIRELESS_WHCI, ~0) },
  446. { /* empty last entry */ }
  447. };
  448. MODULE_DEVICE_TABLE(pci, whcrc_id_table);
  449. static struct umc_driver whcrc_driver = {
  450. .name = "whc-rc",
  451. .cap_id = UMC_CAP_ID_WHCI_RC,
  452. .probe = whcrc_probe,
  453. .remove = whcrc_remove,
  454. };
  455. static int __init whcrc_driver_init(void)
  456. {
  457. return umc_driver_register(&whcrc_driver);
  458. }
  459. module_init(whcrc_driver_init);
  460. static void __exit whcrc_driver_exit(void)
  461. {
  462. umc_driver_unregister(&whcrc_driver);
  463. }
  464. module_exit(whcrc_driver_exit);
  465. MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>");
  466. MODULE_DESCRIPTION("Wireless Host Controller Radio Control Driver");
  467. MODULE_LICENSE("GPL");