whc-rc.c 13 KB

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