whc-rc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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/init.h>
  43. #include <linux/module.h>
  44. #include <linux/pci.h>
  45. #include <linux/sched.h>
  46. #include <linux/dma-mapping.h>
  47. #include <linux/interrupt.h>
  48. #include <linux/slab.h>
  49. #include <linux/workqueue.h>
  50. #include <linux/uwb.h>
  51. #include <linux/uwb/whci.h>
  52. #include <linux/uwb/umc.h>
  53. #include "uwb-internal.h"
  54. /**
  55. * Descriptor for an instance of the UWB Radio Control Driver that
  56. * attaches to the URC interface of the WHCI PCI card.
  57. *
  58. * Unless there is a lock specific to the 'data members', all access
  59. * is protected by uwb_rc->mutex.
  60. */
  61. struct whcrc {
  62. struct umc_dev *umc_dev;
  63. struct uwb_rc *uwb_rc; /* UWB host controller */
  64. unsigned long area;
  65. void __iomem *rc_base;
  66. size_t rc_len;
  67. spinlock_t irq_lock;
  68. void *evt_buf, *cmd_buf;
  69. dma_addr_t evt_dma_buf, cmd_dma_buf;
  70. wait_queue_head_t cmd_wq;
  71. struct work_struct event_work;
  72. };
  73. /**
  74. * Execute an UWB RC command on WHCI/RC
  75. *
  76. * @rc: Instance of a Radio Controller that is a whcrc
  77. * @cmd: Buffer containing the RCCB and payload to execute
  78. * @cmd_size: Size of the command buffer.
  79. *
  80. * We copy the command into whcrc->cmd_buf (as it is pretty and
  81. * aligned`and physically contiguous) and then press the right keys in
  82. * the controller's URCCMD register to get it to read it. We might
  83. * have to wait for the cmd_sem to be open to us.
  84. *
  85. * NOTE: rc's mutex has to be locked
  86. */
  87. static int whcrc_cmd(struct uwb_rc *uwb_rc,
  88. const struct uwb_rccb *cmd, size_t cmd_size)
  89. {
  90. int result = 0;
  91. struct whcrc *whcrc = uwb_rc->priv;
  92. struct device *dev = &whcrc->umc_dev->dev;
  93. u32 urccmd;
  94. if (cmd_size >= 4096)
  95. return -EINVAL;
  96. /*
  97. * If the URC is halted, then the hardware has reset itself.
  98. * Attempt to recover by restarting the device and then return
  99. * an error as it's likely that the current command isn't
  100. * valid for a newly started RC.
  101. */
  102. if (le_readl(whcrc->rc_base + URCSTS) & URCSTS_HALTED) {
  103. dev_err(dev, "requesting reset of halted radio controller\n");
  104. uwb_rc_reset_all(uwb_rc);
  105. return -EIO;
  106. }
  107. result = wait_event_timeout(whcrc->cmd_wq,
  108. !(le_readl(whcrc->rc_base + URCCMD) & URCCMD_ACTIVE), HZ/2);
  109. if (result == 0) {
  110. dev_err(dev, "device is not ready to execute commands\n");
  111. return -ETIMEDOUT;
  112. }
  113. memmove(whcrc->cmd_buf, cmd, cmd_size);
  114. le_writeq(whcrc->cmd_dma_buf, whcrc->rc_base + URCCMDADDR);
  115. spin_lock(&whcrc->irq_lock);
  116. urccmd = le_readl(whcrc->rc_base + URCCMD);
  117. urccmd &= ~(URCCMD_EARV | URCCMD_SIZE_MASK);
  118. le_writel(urccmd | URCCMD_ACTIVE | URCCMD_IWR | cmd_size,
  119. whcrc->rc_base + URCCMD);
  120. spin_unlock(&whcrc->irq_lock);
  121. return 0;
  122. }
  123. static int whcrc_reset(struct uwb_rc *rc)
  124. {
  125. struct whcrc *whcrc = rc->priv;
  126. return umc_controller_reset(whcrc->umc_dev);
  127. }
  128. /**
  129. * Reset event reception mechanism and tell hw we are ready to get more
  130. *
  131. * We have read all the events in the event buffer, so we are ready to
  132. * reset it to the beginning.
  133. *
  134. * This is only called during initialization or after an event buffer
  135. * has been retired. This means we can be sure that event processing
  136. * is disabled and it's safe to update the URCEVTADDR register.
  137. *
  138. * There's no need to wait for the event processing to start as the
  139. * URC will not clear URCCMD_ACTIVE until (internal) event buffer
  140. * space is available.
  141. */
  142. static
  143. void whcrc_enable_events(struct whcrc *whcrc)
  144. {
  145. u32 urccmd;
  146. le_writeq(whcrc->evt_dma_buf, whcrc->rc_base + URCEVTADDR);
  147. spin_lock(&whcrc->irq_lock);
  148. urccmd = le_readl(whcrc->rc_base + URCCMD) & ~URCCMD_ACTIVE;
  149. le_writel(urccmd | URCCMD_EARV, whcrc->rc_base + URCCMD);
  150. spin_unlock(&whcrc->irq_lock);
  151. }
  152. static void whcrc_event_work(struct work_struct *work)
  153. {
  154. struct whcrc *whcrc = container_of(work, struct whcrc, event_work);
  155. size_t size;
  156. u64 urcevtaddr;
  157. urcevtaddr = le_readq(whcrc->rc_base + URCEVTADDR);
  158. size = urcevtaddr & URCEVTADDR_OFFSET_MASK;
  159. uwb_rc_neh_grok(whcrc->uwb_rc, whcrc->evt_buf, size);
  160. whcrc_enable_events(whcrc);
  161. }
  162. /**
  163. * Catch interrupts?
  164. *
  165. * We ack inmediately (and expect the hw to do the right thing and
  166. * raise another IRQ if things have changed :)
  167. */
  168. static
  169. irqreturn_t whcrc_irq_cb(int irq, void *_whcrc)
  170. {
  171. struct whcrc *whcrc = _whcrc;
  172. struct device *dev = &whcrc->umc_dev->dev;
  173. u32 urcsts;
  174. urcsts = le_readl(whcrc->rc_base + URCSTS);
  175. if (!(urcsts & URCSTS_INT_MASK))
  176. return IRQ_NONE;
  177. le_writel(urcsts & URCSTS_INT_MASK, whcrc->rc_base + URCSTS);
  178. if (urcsts & URCSTS_HSE) {
  179. dev_err(dev, "host system error -- hardware halted\n");
  180. /* FIXME: do something sensible here */
  181. goto out;
  182. }
  183. if (urcsts & URCSTS_ER)
  184. schedule_work(&whcrc->event_work);
  185. if (urcsts & URCSTS_RCI)
  186. wake_up_all(&whcrc->cmd_wq);
  187. out:
  188. return IRQ_HANDLED;
  189. }
  190. /**
  191. * Initialize a UMC RC interface: map regions, get (shared) IRQ
  192. */
  193. static
  194. int whcrc_setup_rc_umc(struct whcrc *whcrc)
  195. {
  196. int result = 0;
  197. struct device *dev = &whcrc->umc_dev->dev;
  198. struct umc_dev *umc_dev = whcrc->umc_dev;
  199. whcrc->area = umc_dev->resource.start;
  200. whcrc->rc_len = resource_size(&umc_dev->resource);
  201. result = -EBUSY;
  202. if (request_mem_region(whcrc->area, whcrc->rc_len, KBUILD_MODNAME) == NULL) {
  203. dev_err(dev, "can't request URC region (%zu bytes @ 0x%lx): %d\n",
  204. whcrc->rc_len, whcrc->area, result);
  205. goto error_request_region;
  206. }
  207. whcrc->rc_base = ioremap_nocache(whcrc->area, whcrc->rc_len);
  208. if (whcrc->rc_base == NULL) {
  209. dev_err(dev, "can't ioremap registers (%zu bytes @ 0x%lx): %d\n",
  210. whcrc->rc_len, whcrc->area, result);
  211. goto error_ioremap_nocache;
  212. }
  213. result = request_irq(umc_dev->irq, whcrc_irq_cb, IRQF_SHARED,
  214. KBUILD_MODNAME, whcrc);
  215. if (result < 0) {
  216. dev_err(dev, "can't allocate IRQ %d: %d\n",
  217. umc_dev->irq, result);
  218. goto error_request_irq;
  219. }
  220. result = -ENOMEM;
  221. whcrc->cmd_buf = dma_alloc_coherent(&umc_dev->dev, PAGE_SIZE,
  222. &whcrc->cmd_dma_buf, GFP_KERNEL);
  223. if (whcrc->cmd_buf == NULL) {
  224. dev_err(dev, "Can't allocate cmd transfer buffer\n");
  225. goto error_cmd_buffer;
  226. }
  227. whcrc->evt_buf = dma_alloc_coherent(&umc_dev->dev, PAGE_SIZE,
  228. &whcrc->evt_dma_buf, GFP_KERNEL);
  229. if (whcrc->evt_buf == NULL) {
  230. dev_err(dev, "Can't allocate evt transfer buffer\n");
  231. goto error_evt_buffer;
  232. }
  233. return 0;
  234. error_evt_buffer:
  235. dma_free_coherent(&umc_dev->dev, PAGE_SIZE, whcrc->cmd_buf,
  236. whcrc->cmd_dma_buf);
  237. error_cmd_buffer:
  238. free_irq(umc_dev->irq, whcrc);
  239. error_request_irq:
  240. iounmap(whcrc->rc_base);
  241. error_ioremap_nocache:
  242. release_mem_region(whcrc->area, whcrc->rc_len);
  243. error_request_region:
  244. return result;
  245. }
  246. /**
  247. * Release RC's UMC resources
  248. */
  249. static
  250. void whcrc_release_rc_umc(struct whcrc *whcrc)
  251. {
  252. struct umc_dev *umc_dev = whcrc->umc_dev;
  253. dma_free_coherent(&umc_dev->dev, PAGE_SIZE, whcrc->evt_buf,
  254. whcrc->evt_dma_buf);
  255. dma_free_coherent(&umc_dev->dev, PAGE_SIZE, whcrc->cmd_buf,
  256. whcrc->cmd_dma_buf);
  257. free_irq(umc_dev->irq, whcrc);
  258. iounmap(whcrc->rc_base);
  259. release_mem_region(whcrc->area, whcrc->rc_len);
  260. }
  261. /**
  262. * whcrc_start_rc - start a WHCI radio controller
  263. * @whcrc: the radio controller to start
  264. *
  265. * Reset the UMC device, start the radio controller, enable events and
  266. * finally enable interrupts.
  267. */
  268. static int whcrc_start_rc(struct uwb_rc *rc)
  269. {
  270. struct whcrc *whcrc = rc->priv;
  271. struct device *dev = &whcrc->umc_dev->dev;
  272. /* Reset the thing */
  273. le_writel(URCCMD_RESET, whcrc->rc_base + URCCMD);
  274. if (whci_wait_for(dev, whcrc->rc_base + URCCMD, URCCMD_RESET, 0,
  275. 5000, "hardware reset") < 0)
  276. return -EBUSY;
  277. /* Set the event buffer, start the controller (enable IRQs later) */
  278. le_writel(0, whcrc->rc_base + URCINTR);
  279. le_writel(URCCMD_RS, whcrc->rc_base + URCCMD);
  280. if (whci_wait_for(dev, whcrc->rc_base + URCSTS, URCSTS_HALTED, 0,
  281. 5000, "radio controller start") < 0)
  282. return -ETIMEDOUT;
  283. whcrc_enable_events(whcrc);
  284. le_writel(URCINTR_EN_ALL, whcrc->rc_base + URCINTR);
  285. return 0;
  286. }
  287. /**
  288. * whcrc_stop_rc - stop a WHCI radio controller
  289. * @whcrc: the radio controller to stop
  290. *
  291. * Disable interrupts and cancel any pending event processing work
  292. * before clearing the Run/Stop bit.
  293. */
  294. static
  295. void whcrc_stop_rc(struct uwb_rc *rc)
  296. {
  297. struct whcrc *whcrc = rc->priv;
  298. struct umc_dev *umc_dev = whcrc->umc_dev;
  299. le_writel(0, whcrc->rc_base + URCINTR);
  300. cancel_work_sync(&whcrc->event_work);
  301. le_writel(0, whcrc->rc_base + URCCMD);
  302. whci_wait_for(&umc_dev->dev, whcrc->rc_base + URCSTS,
  303. URCSTS_HALTED, URCSTS_HALTED, 100, "radio controller stop");
  304. }
  305. static void whcrc_init(struct whcrc *whcrc)
  306. {
  307. spin_lock_init(&whcrc->irq_lock);
  308. init_waitqueue_head(&whcrc->cmd_wq);
  309. INIT_WORK(&whcrc->event_work, whcrc_event_work);
  310. }
  311. /**
  312. * Initialize the radio controller.
  313. *
  314. * NOTE: we setup whcrc->uwb_rc before calling uwb_rc_add(); in the
  315. * IRQ handler we use that to determine if the hw is ready to
  316. * handle events. Looks like a race condition, but it really is
  317. * not.
  318. */
  319. static
  320. int whcrc_probe(struct umc_dev *umc_dev)
  321. {
  322. int result;
  323. struct uwb_rc *uwb_rc;
  324. struct whcrc *whcrc;
  325. struct device *dev = &umc_dev->dev;
  326. result = -ENOMEM;
  327. uwb_rc = uwb_rc_alloc();
  328. if (uwb_rc == NULL) {
  329. dev_err(dev, "unable to allocate RC instance\n");
  330. goto error_rc_alloc;
  331. }
  332. whcrc = kzalloc(sizeof(*whcrc), GFP_KERNEL);
  333. if (whcrc == NULL) {
  334. dev_err(dev, "unable to allocate WHC-RC instance\n");
  335. goto error_alloc;
  336. }
  337. whcrc_init(whcrc);
  338. whcrc->umc_dev = umc_dev;
  339. result = whcrc_setup_rc_umc(whcrc);
  340. if (result < 0) {
  341. dev_err(dev, "Can't setup RC UMC interface: %d\n", result);
  342. goto error_setup_rc_umc;
  343. }
  344. whcrc->uwb_rc = uwb_rc;
  345. uwb_rc->owner = THIS_MODULE;
  346. uwb_rc->cmd = whcrc_cmd;
  347. uwb_rc->reset = whcrc_reset;
  348. uwb_rc->start = whcrc_start_rc;
  349. uwb_rc->stop = whcrc_stop_rc;
  350. result = uwb_rc_add(uwb_rc, dev, whcrc);
  351. if (result < 0)
  352. goto error_rc_add;
  353. umc_set_drvdata(umc_dev, whcrc);
  354. return 0;
  355. error_rc_add:
  356. whcrc_release_rc_umc(whcrc);
  357. error_setup_rc_umc:
  358. kfree(whcrc);
  359. error_alloc:
  360. uwb_rc_put(uwb_rc);
  361. error_rc_alloc:
  362. return result;
  363. }
  364. /**
  365. * Clean up the radio control resources
  366. *
  367. * When we up the command semaphore, everybody possibly held trying to
  368. * execute a command should be granted entry and then they'll see the
  369. * host is quiescing and up it (so it will chain to the next waiter).
  370. * This should not happen (in any case), as we can only remove when
  371. * there are no handles open...
  372. */
  373. static void whcrc_remove(struct umc_dev *umc_dev)
  374. {
  375. struct whcrc *whcrc = umc_get_drvdata(umc_dev);
  376. struct uwb_rc *uwb_rc = whcrc->uwb_rc;
  377. umc_set_drvdata(umc_dev, NULL);
  378. uwb_rc_rm(uwb_rc);
  379. whcrc_release_rc_umc(whcrc);
  380. kfree(whcrc);
  381. uwb_rc_put(uwb_rc);
  382. }
  383. static int whcrc_pre_reset(struct umc_dev *umc)
  384. {
  385. struct whcrc *whcrc = umc_get_drvdata(umc);
  386. struct uwb_rc *uwb_rc = whcrc->uwb_rc;
  387. uwb_rc_pre_reset(uwb_rc);
  388. return 0;
  389. }
  390. static int whcrc_post_reset(struct umc_dev *umc)
  391. {
  392. struct whcrc *whcrc = umc_get_drvdata(umc);
  393. struct uwb_rc *uwb_rc = whcrc->uwb_rc;
  394. return uwb_rc_post_reset(uwb_rc);
  395. }
  396. /* PCI device ID's that we handle [so it gets loaded] */
  397. static struct pci_device_id __used whcrc_id_table[] = {
  398. { PCI_DEVICE_CLASS(PCI_CLASS_WIRELESS_WHCI, ~0) },
  399. { /* empty last entry */ }
  400. };
  401. MODULE_DEVICE_TABLE(pci, whcrc_id_table);
  402. static struct umc_driver whcrc_driver = {
  403. .name = "whc-rc",
  404. .cap_id = UMC_CAP_ID_WHCI_RC,
  405. .probe = whcrc_probe,
  406. .remove = whcrc_remove,
  407. .pre_reset = whcrc_pre_reset,
  408. .post_reset = whcrc_post_reset,
  409. };
  410. static int __init whcrc_driver_init(void)
  411. {
  412. return umc_driver_register(&whcrc_driver);
  413. }
  414. module_init(whcrc_driver_init);
  415. static void __exit whcrc_driver_exit(void)
  416. {
  417. umc_driver_unregister(&whcrc_driver);
  418. }
  419. module_exit(whcrc_driver_exit);
  420. MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>");
  421. MODULE_DESCRIPTION("Wireless Host Controller Radio Control Driver");
  422. MODULE_LICENSE("GPL");