rt2x00pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
  3. <http://rt2x00.serialmonkey.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the
  14. Free Software Foundation, Inc.,
  15. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. /*
  18. Module: rt2x00pci
  19. Abstract: rt2x00 generic pci device routines.
  20. */
  21. #include <linux/dma-mapping.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/pci.h>
  25. #include "rt2x00.h"
  26. #include "rt2x00pci.h"
  27. /*
  28. * TX data handlers.
  29. */
  30. int rt2x00pci_write_tx_data(struct queue_entry *entry)
  31. {
  32. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  33. struct skb_frame_desc *skbdesc;
  34. u32 word;
  35. rt2x00_desc_read(entry_priv->desc, 0, &word);
  36. /*
  37. * This should not happen, we already checked the entry
  38. * was ours. When the hardware disagrees there has been
  39. * a queue corruption!
  40. */
  41. if (unlikely(rt2x00_get_field32(word, TXD_ENTRY_OWNER_NIC) ||
  42. rt2x00_get_field32(word, TXD_ENTRY_VALID))) {
  43. ERROR(entry->queue->rt2x00dev,
  44. "Corrupt queue %d, accessing entry which is not ours.\n"
  45. "Please file bug report to %s.\n",
  46. entry->queue->qid, DRV_PROJECT);
  47. return -EINVAL;
  48. }
  49. /*
  50. * Fill in skb descriptor
  51. */
  52. skbdesc = get_skb_frame_desc(entry->skb);
  53. memset(skbdesc, 0, sizeof(*skbdesc));
  54. skbdesc->desc = entry_priv->desc;
  55. skbdesc->desc_len = entry->queue->desc_size;
  56. skbdesc->entry = entry;
  57. memcpy(entry_priv->data, entry->skb->data, entry->skb->len);
  58. return 0;
  59. }
  60. EXPORT_SYMBOL_GPL(rt2x00pci_write_tx_data);
  61. /*
  62. * TX/RX data handlers.
  63. */
  64. void rt2x00pci_rxdone(struct rt2x00_dev *rt2x00dev)
  65. {
  66. struct data_queue *queue = rt2x00dev->rx;
  67. struct queue_entry *entry;
  68. struct queue_entry_priv_pci *entry_priv;
  69. struct skb_frame_desc *skbdesc;
  70. struct rxdone_entry_desc rxdesc;
  71. u32 word;
  72. while (1) {
  73. entry = rt2x00queue_get_entry(queue, Q_INDEX);
  74. entry_priv = entry->priv_data;
  75. rt2x00_desc_read(entry_priv->desc, 0, &word);
  76. if (rt2x00_get_field32(word, RXD_ENTRY_OWNER_NIC))
  77. break;
  78. memset(&rxdesc, 0, sizeof(rxdesc));
  79. rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
  80. /*
  81. * Allocate the sk_buffer and copy all data into it.
  82. */
  83. entry->skb = rt2x00queue_alloc_rxskb(queue);
  84. if (!entry->skb)
  85. return;
  86. memcpy(entry->skb->data, entry_priv->data, rxdesc.size);
  87. skb_trim(entry->skb, rxdesc.size);
  88. /*
  89. * Fill in skb descriptor
  90. */
  91. skbdesc = get_skb_frame_desc(entry->skb);
  92. memset(skbdesc, 0, sizeof(*skbdesc));
  93. skbdesc->desc = entry_priv->desc;
  94. skbdesc->desc_len = queue->desc_size;
  95. skbdesc->entry = entry;
  96. /*
  97. * Send the frame to rt2x00lib for further processing.
  98. */
  99. rt2x00lib_rxdone(entry, &rxdesc);
  100. if (test_bit(DEVICE_ENABLED_RADIO, &queue->rt2x00dev->flags)) {
  101. rt2x00_set_field32(&word, RXD_ENTRY_OWNER_NIC, 1);
  102. rt2x00_desc_write(entry_priv->desc, 0, word);
  103. }
  104. rt2x00queue_index_inc(queue, Q_INDEX);
  105. }
  106. }
  107. EXPORT_SYMBOL_GPL(rt2x00pci_rxdone);
  108. void rt2x00pci_txdone(struct rt2x00_dev *rt2x00dev, struct queue_entry *entry,
  109. struct txdone_entry_desc *txdesc)
  110. {
  111. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  112. enum data_queue_qid qid = skb_get_queue_mapping(entry->skb);
  113. u32 word;
  114. rt2x00lib_txdone(entry, txdesc);
  115. /*
  116. * Make this entry available for reuse.
  117. */
  118. entry->flags = 0;
  119. rt2x00_desc_read(entry_priv->desc, 0, &word);
  120. rt2x00_set_field32(&word, TXD_ENTRY_OWNER_NIC, 0);
  121. rt2x00_set_field32(&word, TXD_ENTRY_VALID, 0);
  122. rt2x00_desc_write(entry_priv->desc, 0, word);
  123. __clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  124. rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
  125. /*
  126. * If the data queue was below the threshold before the txdone
  127. * handler we must make sure the packet queue in the mac80211 stack
  128. * is reenabled when the txdone handler has finished.
  129. */
  130. if (!rt2x00queue_threshold(entry->queue))
  131. ieee80211_wake_queue(rt2x00dev->hw, qid);
  132. }
  133. EXPORT_SYMBOL_GPL(rt2x00pci_txdone);
  134. /*
  135. * Device initialization handlers.
  136. */
  137. #define desc_size(__queue) \
  138. ({ \
  139. ((__queue)->limit * (__queue)->desc_size);\
  140. })
  141. #define data_size(__queue) \
  142. ({ \
  143. ((__queue)->limit * (__queue)->data_size);\
  144. })
  145. #define dma_size(__queue) \
  146. ({ \
  147. data_size(__queue) + desc_size(__queue);\
  148. })
  149. #define desc_offset(__queue, __base, __i) \
  150. ({ \
  151. (__base) + data_size(__queue) + \
  152. ((__i) * (__queue)->desc_size); \
  153. })
  154. #define data_offset(__queue, __base, __i) \
  155. ({ \
  156. (__base) + \
  157. ((__i) * (__queue)->data_size); \
  158. })
  159. static int rt2x00pci_alloc_queue_dma(struct rt2x00_dev *rt2x00dev,
  160. struct data_queue *queue)
  161. {
  162. struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
  163. struct queue_entry_priv_pci *entry_priv;
  164. void *addr;
  165. dma_addr_t dma;
  166. unsigned int i;
  167. /*
  168. * Allocate DMA memory for descriptor and buffer.
  169. */
  170. addr = pci_alloc_consistent(pci_dev, dma_size(queue), &dma);
  171. if (!addr)
  172. return -ENOMEM;
  173. memset(addr, 0, dma_size(queue));
  174. /*
  175. * Initialize all queue entries to contain valid addresses.
  176. */
  177. for (i = 0; i < queue->limit; i++) {
  178. entry_priv = queue->entries[i].priv_data;
  179. entry_priv->desc = desc_offset(queue, addr, i);
  180. entry_priv->desc_dma = desc_offset(queue, dma, i);
  181. entry_priv->data = data_offset(queue, addr, i);
  182. entry_priv->data_dma = data_offset(queue, dma, i);
  183. }
  184. return 0;
  185. }
  186. static void rt2x00pci_free_queue_dma(struct rt2x00_dev *rt2x00dev,
  187. struct data_queue *queue)
  188. {
  189. struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
  190. struct queue_entry_priv_pci *entry_priv =
  191. queue->entries[0].priv_data;
  192. if (entry_priv->data)
  193. pci_free_consistent(pci_dev, dma_size(queue),
  194. entry_priv->data, entry_priv->data_dma);
  195. entry_priv->data = NULL;
  196. }
  197. int rt2x00pci_initialize(struct rt2x00_dev *rt2x00dev)
  198. {
  199. struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
  200. struct data_queue *queue;
  201. int status;
  202. /*
  203. * Allocate DMA
  204. */
  205. queue_for_each(rt2x00dev, queue) {
  206. status = rt2x00pci_alloc_queue_dma(rt2x00dev, queue);
  207. if (status)
  208. goto exit;
  209. }
  210. /*
  211. * Register interrupt handler.
  212. */
  213. status = request_irq(pci_dev->irq, rt2x00dev->ops->lib->irq_handler,
  214. IRQF_SHARED, pci_name(pci_dev), rt2x00dev);
  215. if (status) {
  216. ERROR(rt2x00dev, "IRQ %d allocation failed (error %d).\n",
  217. pci_dev->irq, status);
  218. goto exit;
  219. }
  220. return 0;
  221. exit:
  222. queue_for_each(rt2x00dev, queue)
  223. rt2x00pci_free_queue_dma(rt2x00dev, queue);
  224. return status;
  225. }
  226. EXPORT_SYMBOL_GPL(rt2x00pci_initialize);
  227. void rt2x00pci_uninitialize(struct rt2x00_dev *rt2x00dev)
  228. {
  229. struct data_queue *queue;
  230. /*
  231. * Free irq line.
  232. */
  233. free_irq(rt2x00dev_pci(rt2x00dev)->irq, rt2x00dev);
  234. /*
  235. * Free DMA
  236. */
  237. queue_for_each(rt2x00dev, queue)
  238. rt2x00pci_free_queue_dma(rt2x00dev, queue);
  239. }
  240. EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize);
  241. /*
  242. * PCI driver handlers.
  243. */
  244. static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
  245. {
  246. kfree(rt2x00dev->rf);
  247. rt2x00dev->rf = NULL;
  248. kfree(rt2x00dev->eeprom);
  249. rt2x00dev->eeprom = NULL;
  250. if (rt2x00dev->csr.base) {
  251. iounmap(rt2x00dev->csr.base);
  252. rt2x00dev->csr.base = NULL;
  253. }
  254. }
  255. static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
  256. {
  257. struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
  258. rt2x00dev->csr.base = ioremap(pci_resource_start(pci_dev, 0),
  259. pci_resource_len(pci_dev, 0));
  260. if (!rt2x00dev->csr.base)
  261. goto exit;
  262. rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
  263. if (!rt2x00dev->eeprom)
  264. goto exit;
  265. rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
  266. if (!rt2x00dev->rf)
  267. goto exit;
  268. return 0;
  269. exit:
  270. ERROR_PROBE("Failed to allocate registers.\n");
  271. rt2x00pci_free_reg(rt2x00dev);
  272. return -ENOMEM;
  273. }
  274. int rt2x00pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
  275. {
  276. struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_data;
  277. struct ieee80211_hw *hw;
  278. struct rt2x00_dev *rt2x00dev;
  279. int retval;
  280. retval = pci_request_regions(pci_dev, pci_name(pci_dev));
  281. if (retval) {
  282. ERROR_PROBE("PCI request regions failed.\n");
  283. return retval;
  284. }
  285. retval = pci_enable_device(pci_dev);
  286. if (retval) {
  287. ERROR_PROBE("Enable device failed.\n");
  288. goto exit_release_regions;
  289. }
  290. pci_set_master(pci_dev);
  291. if (pci_set_mwi(pci_dev))
  292. ERROR_PROBE("MWI not available.\n");
  293. if (pci_set_dma_mask(pci_dev, DMA_64BIT_MASK) &&
  294. pci_set_dma_mask(pci_dev, DMA_32BIT_MASK)) {
  295. ERROR_PROBE("PCI DMA not supported.\n");
  296. retval = -EIO;
  297. goto exit_disable_device;
  298. }
  299. hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
  300. if (!hw) {
  301. ERROR_PROBE("Failed to allocate hardware.\n");
  302. retval = -ENOMEM;
  303. goto exit_disable_device;
  304. }
  305. pci_set_drvdata(pci_dev, hw);
  306. rt2x00dev = hw->priv;
  307. rt2x00dev->dev = pci_dev;
  308. rt2x00dev->ops = ops;
  309. rt2x00dev->hw = hw;
  310. retval = rt2x00pci_alloc_reg(rt2x00dev);
  311. if (retval)
  312. goto exit_free_device;
  313. retval = rt2x00lib_probe_dev(rt2x00dev);
  314. if (retval)
  315. goto exit_free_reg;
  316. return 0;
  317. exit_free_reg:
  318. rt2x00pci_free_reg(rt2x00dev);
  319. exit_free_device:
  320. ieee80211_free_hw(hw);
  321. exit_disable_device:
  322. if (retval != -EBUSY)
  323. pci_disable_device(pci_dev);
  324. exit_release_regions:
  325. pci_release_regions(pci_dev);
  326. pci_set_drvdata(pci_dev, NULL);
  327. return retval;
  328. }
  329. EXPORT_SYMBOL_GPL(rt2x00pci_probe);
  330. void rt2x00pci_remove(struct pci_dev *pci_dev)
  331. {
  332. struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
  333. struct rt2x00_dev *rt2x00dev = hw->priv;
  334. /*
  335. * Free all allocated data.
  336. */
  337. rt2x00lib_remove_dev(rt2x00dev);
  338. rt2x00pci_free_reg(rt2x00dev);
  339. ieee80211_free_hw(hw);
  340. /*
  341. * Free the PCI device data.
  342. */
  343. pci_set_drvdata(pci_dev, NULL);
  344. pci_disable_device(pci_dev);
  345. pci_release_regions(pci_dev);
  346. }
  347. EXPORT_SYMBOL_GPL(rt2x00pci_remove);
  348. #ifdef CONFIG_PM
  349. int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
  350. {
  351. struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
  352. struct rt2x00_dev *rt2x00dev = hw->priv;
  353. int retval;
  354. retval = rt2x00lib_suspend(rt2x00dev, state);
  355. if (retval)
  356. return retval;
  357. rt2x00pci_free_reg(rt2x00dev);
  358. pci_save_state(pci_dev);
  359. pci_disable_device(pci_dev);
  360. return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
  361. }
  362. EXPORT_SYMBOL_GPL(rt2x00pci_suspend);
  363. int rt2x00pci_resume(struct pci_dev *pci_dev)
  364. {
  365. struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
  366. struct rt2x00_dev *rt2x00dev = hw->priv;
  367. int retval;
  368. if (pci_set_power_state(pci_dev, PCI_D0) ||
  369. pci_enable_device(pci_dev) ||
  370. pci_restore_state(pci_dev)) {
  371. ERROR(rt2x00dev, "Failed to resume device.\n");
  372. return -EIO;
  373. }
  374. retval = rt2x00pci_alloc_reg(rt2x00dev);
  375. if (retval)
  376. return retval;
  377. retval = rt2x00lib_resume(rt2x00dev);
  378. if (retval)
  379. goto exit_free_reg;
  380. return 0;
  381. exit_free_reg:
  382. rt2x00pci_free_reg(rt2x00dev);
  383. return retval;
  384. }
  385. EXPORT_SYMBOL_GPL(rt2x00pci_resume);
  386. #endif /* CONFIG_PM */
  387. /*
  388. * rt2x00pci module information.
  389. */
  390. MODULE_AUTHOR(DRV_PROJECT);
  391. MODULE_VERSION(DRV_VERSION);
  392. MODULE_DESCRIPTION("rt2x00 pci library");
  393. MODULE_LICENSE("GPL");