rt2x00pci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. Copyright (C) 2004 - 2007 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. /*
  22. * Set enviroment defines for rt2x00.h
  23. */
  24. #define DRV_NAME "rt2x00pci"
  25. #include <linux/dma-mapping.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/pci.h>
  29. #include "rt2x00.h"
  30. #include "rt2x00pci.h"
  31. /*
  32. * Beacon handlers.
  33. */
  34. int rt2x00pci_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
  35. struct ieee80211_tx_control *control)
  36. {
  37. struct rt2x00_dev *rt2x00dev = hw->priv;
  38. struct data_ring *ring =
  39. rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_BEACON);
  40. struct data_entry *entry = rt2x00_get_data_entry(ring);
  41. /*
  42. * Just in case mac80211 doesn't set this correctly,
  43. * but we need this queue set for the descriptor
  44. * initialization.
  45. */
  46. control->queue = IEEE80211_TX_QUEUE_BEACON;
  47. /*
  48. * Update the beacon entry.
  49. */
  50. memcpy(entry->data_addr, skb->data, skb->len);
  51. rt2x00lib_write_tx_desc(rt2x00dev, entry->priv,
  52. (struct ieee80211_hdr *)skb->data,
  53. skb->len, control);
  54. /*
  55. * Enable beacon generation.
  56. */
  57. rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, control->queue);
  58. return 0;
  59. }
  60. EXPORT_SYMBOL_GPL(rt2x00pci_beacon_update);
  61. /*
  62. * TX data handlers.
  63. */
  64. int rt2x00pci_write_tx_data(struct rt2x00_dev *rt2x00dev,
  65. struct data_ring *ring, struct sk_buff *skb,
  66. struct ieee80211_tx_control *control)
  67. {
  68. struct ieee80211_hdr *ieee80211hdr = (struct ieee80211_hdr *)skb->data;
  69. struct data_entry *entry = rt2x00_get_data_entry(ring);
  70. __le32 *txd = entry->priv;
  71. u32 word;
  72. if (rt2x00_ring_full(ring)) {
  73. ieee80211_stop_queue(rt2x00dev->hw, control->queue);
  74. return -EINVAL;
  75. }
  76. rt2x00_desc_read(txd, 0, &word);
  77. if (rt2x00_get_field32(word, TXD_ENTRY_OWNER_NIC) ||
  78. rt2x00_get_field32(word, TXD_ENTRY_VALID)) {
  79. ERROR(rt2x00dev,
  80. "Arrived at non-free entry in the non-full queue %d.\n"
  81. "Please file bug report to %s.\n",
  82. control->queue, DRV_PROJECT);
  83. ieee80211_stop_queue(rt2x00dev->hw, control->queue);
  84. return -EINVAL;
  85. }
  86. entry->skb = skb;
  87. memcpy(&entry->tx_status.control, control, sizeof(*control));
  88. memcpy(entry->data_addr, skb->data, skb->len);
  89. rt2x00lib_write_tx_desc(rt2x00dev, txd, ieee80211hdr,
  90. skb->len, control);
  91. rt2x00_ring_index_inc(ring);
  92. if (rt2x00_ring_full(ring))
  93. ieee80211_stop_queue(rt2x00dev->hw, control->queue);
  94. return 0;
  95. }
  96. EXPORT_SYMBOL_GPL(rt2x00pci_write_tx_data);
  97. /*
  98. * TX/RX data handlers.
  99. */
  100. void rt2x00pci_rxdone(struct rt2x00_dev *rt2x00dev)
  101. {
  102. struct data_ring *ring = rt2x00dev->rx;
  103. struct data_entry *entry;
  104. struct sk_buff *skb;
  105. struct ieee80211_hdr *hdr;
  106. struct rxdata_entry_desc desc;
  107. int header_size;
  108. __le32 *rxd;
  109. int align;
  110. u32 word;
  111. while (1) {
  112. entry = rt2x00_get_data_entry(ring);
  113. rxd = entry->priv;
  114. rt2x00_desc_read(rxd, 0, &word);
  115. if (rt2x00_get_field32(word, RXD_ENTRY_OWNER_NIC))
  116. break;
  117. memset(&desc, 0x00, sizeof(desc));
  118. rt2x00dev->ops->lib->fill_rxdone(entry, &desc);
  119. hdr = (struct ieee80211_hdr *)entry->data_addr;
  120. header_size =
  121. ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
  122. /*
  123. * The data behind the ieee80211 header must be
  124. * aligned on a 4 byte boundary.
  125. */
  126. align = header_size % 4;
  127. /*
  128. * Allocate the sk_buffer, initialize it and copy
  129. * all data into it.
  130. */
  131. skb = dev_alloc_skb(desc.size + align);
  132. if (!skb)
  133. return;
  134. skb_reserve(skb, align);
  135. memcpy(skb_put(skb, desc.size), entry->data_addr, desc.size);
  136. /*
  137. * Send the frame to rt2x00lib for further processing.
  138. */
  139. rt2x00lib_rxdone(entry, skb, &desc);
  140. if (test_bit(DEVICE_ENABLED_RADIO, &ring->rt2x00dev->flags)) {
  141. rt2x00_set_field32(&word, RXD_ENTRY_OWNER_NIC, 1);
  142. rt2x00_desc_write(rxd, 0, word);
  143. }
  144. rt2x00_ring_index_inc(ring);
  145. }
  146. }
  147. EXPORT_SYMBOL_GPL(rt2x00pci_rxdone);
  148. void rt2x00pci_txdone(struct rt2x00_dev *rt2x00dev, struct data_entry *entry,
  149. const int tx_status, const int retry)
  150. {
  151. u32 word;
  152. rt2x00lib_txdone(entry, tx_status, retry);
  153. /*
  154. * Make this entry available for reuse.
  155. */
  156. entry->flags = 0;
  157. rt2x00_desc_read(entry->priv, 0, &word);
  158. rt2x00_set_field32(&word, TXD_ENTRY_OWNER_NIC, 0);
  159. rt2x00_set_field32(&word, TXD_ENTRY_VALID, 0);
  160. rt2x00_desc_write(entry->priv, 0, word);
  161. rt2x00_ring_index_done_inc(entry->ring);
  162. /*
  163. * If the data ring was full before the txdone handler
  164. * we must make sure the packet queue in the mac80211 stack
  165. * is reenabled when the txdone handler has finished.
  166. */
  167. if (!rt2x00_ring_full(entry->ring))
  168. ieee80211_wake_queue(rt2x00dev->hw,
  169. entry->tx_status.control.queue);
  170. }
  171. EXPORT_SYMBOL_GPL(rt2x00pci_txdone);
  172. /*
  173. * Device initialization handlers.
  174. */
  175. #define priv_offset(__ring, __i) \
  176. ({ \
  177. ring->data_addr + (i * ring->desc_size); \
  178. })
  179. #define data_addr_offset(__ring, __i) \
  180. ({ \
  181. (__ring)->data_addr + \
  182. ((__ring)->stats.limit * (__ring)->desc_size) + \
  183. ((__i) * (__ring)->data_size); \
  184. })
  185. #define data_dma_offset(__ring, __i) \
  186. ({ \
  187. (__ring)->data_dma + \
  188. ((__ring)->stats.limit * (__ring)->desc_size) + \
  189. ((__i) * (__ring)->data_size); \
  190. })
  191. static int rt2x00pci_alloc_dma(struct rt2x00_dev *rt2x00dev,
  192. struct data_ring *ring)
  193. {
  194. unsigned int i;
  195. /*
  196. * Allocate DMA memory for descriptor and buffer.
  197. */
  198. ring->data_addr = pci_alloc_consistent(rt2x00dev_pci(rt2x00dev),
  199. rt2x00_get_ring_size(ring),
  200. &ring->data_dma);
  201. if (!ring->data_addr)
  202. return -ENOMEM;
  203. /*
  204. * Initialize all ring entries to contain valid
  205. * addresses.
  206. */
  207. for (i = 0; i < ring->stats.limit; i++) {
  208. ring->entry[i].priv = priv_offset(ring, i);
  209. ring->entry[i].data_addr = data_addr_offset(ring, i);
  210. ring->entry[i].data_dma = data_dma_offset(ring, i);
  211. }
  212. return 0;
  213. }
  214. static void rt2x00pci_free_dma(struct rt2x00_dev *rt2x00dev,
  215. struct data_ring *ring)
  216. {
  217. if (ring->data_addr)
  218. pci_free_consistent(rt2x00dev_pci(rt2x00dev),
  219. rt2x00_get_ring_size(ring),
  220. ring->data_addr, ring->data_dma);
  221. ring->data_addr = NULL;
  222. }
  223. int rt2x00pci_initialize(struct rt2x00_dev *rt2x00dev)
  224. {
  225. struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
  226. struct data_ring *ring;
  227. int status;
  228. /*
  229. * Allocate DMA
  230. */
  231. ring_for_each(rt2x00dev, ring) {
  232. status = rt2x00pci_alloc_dma(rt2x00dev, ring);
  233. if (status)
  234. goto exit;
  235. }
  236. /*
  237. * Register interrupt handler.
  238. */
  239. status = request_irq(pci_dev->irq, rt2x00dev->ops->lib->irq_handler,
  240. IRQF_SHARED, pci_name(pci_dev), rt2x00dev);
  241. if (status) {
  242. ERROR(rt2x00dev, "IRQ %d allocation failed (error %d).\n",
  243. pci_dev->irq, status);
  244. return status;
  245. }
  246. return 0;
  247. exit:
  248. rt2x00pci_uninitialize(rt2x00dev);
  249. return status;
  250. }
  251. EXPORT_SYMBOL_GPL(rt2x00pci_initialize);
  252. void rt2x00pci_uninitialize(struct rt2x00_dev *rt2x00dev)
  253. {
  254. struct data_ring *ring;
  255. /*
  256. * Free irq line.
  257. */
  258. free_irq(rt2x00dev_pci(rt2x00dev)->irq, rt2x00dev);
  259. /*
  260. * Free DMA
  261. */
  262. ring_for_each(rt2x00dev, ring)
  263. rt2x00pci_free_dma(rt2x00dev, ring);
  264. }
  265. EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize);
  266. /*
  267. * PCI driver handlers.
  268. */
  269. static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
  270. {
  271. kfree(rt2x00dev->rf);
  272. rt2x00dev->rf = NULL;
  273. kfree(rt2x00dev->eeprom);
  274. rt2x00dev->eeprom = NULL;
  275. if (rt2x00dev->csr_addr) {
  276. iounmap(rt2x00dev->csr_addr);
  277. rt2x00dev->csr_addr = NULL;
  278. }
  279. }
  280. static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
  281. {
  282. struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
  283. rt2x00dev->csr_addr = ioremap(pci_resource_start(pci_dev, 0),
  284. pci_resource_len(pci_dev, 0));
  285. if (!rt2x00dev->csr_addr)
  286. goto exit;
  287. rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
  288. if (!rt2x00dev->eeprom)
  289. goto exit;
  290. rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
  291. if (!rt2x00dev->rf)
  292. goto exit;
  293. return 0;
  294. exit:
  295. ERROR_PROBE("Failed to allocate registers.\n");
  296. rt2x00pci_free_reg(rt2x00dev);
  297. return -ENOMEM;
  298. }
  299. int rt2x00pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
  300. {
  301. struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_data;
  302. struct ieee80211_hw *hw;
  303. struct rt2x00_dev *rt2x00dev;
  304. int retval;
  305. retval = pci_request_regions(pci_dev, pci_name(pci_dev));
  306. if (retval) {
  307. ERROR_PROBE("PCI request regions failed.\n");
  308. return retval;
  309. }
  310. retval = pci_enable_device(pci_dev);
  311. if (retval) {
  312. ERROR_PROBE("Enable device failed.\n");
  313. goto exit_release_regions;
  314. }
  315. pci_set_master(pci_dev);
  316. if (pci_set_mwi(pci_dev))
  317. ERROR_PROBE("MWI not available.\n");
  318. if (pci_set_dma_mask(pci_dev, DMA_64BIT_MASK) &&
  319. pci_set_dma_mask(pci_dev, DMA_32BIT_MASK)) {
  320. ERROR_PROBE("PCI DMA not supported.\n");
  321. retval = -EIO;
  322. goto exit_disable_device;
  323. }
  324. hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
  325. if (!hw) {
  326. ERROR_PROBE("Failed to allocate hardware.\n");
  327. retval = -ENOMEM;
  328. goto exit_disable_device;
  329. }
  330. pci_set_drvdata(pci_dev, hw);
  331. rt2x00dev = hw->priv;
  332. rt2x00dev->dev = pci_dev;
  333. rt2x00dev->ops = ops;
  334. rt2x00dev->hw = hw;
  335. retval = rt2x00pci_alloc_reg(rt2x00dev);
  336. if (retval)
  337. goto exit_free_device;
  338. retval = rt2x00lib_probe_dev(rt2x00dev);
  339. if (retval)
  340. goto exit_free_reg;
  341. return 0;
  342. exit_free_reg:
  343. rt2x00pci_free_reg(rt2x00dev);
  344. exit_free_device:
  345. ieee80211_free_hw(hw);
  346. exit_disable_device:
  347. if (retval != -EBUSY)
  348. pci_disable_device(pci_dev);
  349. exit_release_regions:
  350. pci_release_regions(pci_dev);
  351. pci_set_drvdata(pci_dev, NULL);
  352. return retval;
  353. }
  354. EXPORT_SYMBOL_GPL(rt2x00pci_probe);
  355. void rt2x00pci_remove(struct pci_dev *pci_dev)
  356. {
  357. struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
  358. struct rt2x00_dev *rt2x00dev = hw->priv;
  359. /*
  360. * Free all allocated data.
  361. */
  362. rt2x00lib_remove_dev(rt2x00dev);
  363. rt2x00pci_free_reg(rt2x00dev);
  364. ieee80211_free_hw(hw);
  365. /*
  366. * Free the PCI device data.
  367. */
  368. pci_set_drvdata(pci_dev, NULL);
  369. pci_disable_device(pci_dev);
  370. pci_release_regions(pci_dev);
  371. }
  372. EXPORT_SYMBOL_GPL(rt2x00pci_remove);
  373. #ifdef CONFIG_PM
  374. int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
  375. {
  376. struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
  377. struct rt2x00_dev *rt2x00dev = hw->priv;
  378. int retval;
  379. retval = rt2x00lib_suspend(rt2x00dev, state);
  380. if (retval)
  381. return retval;
  382. rt2x00pci_free_reg(rt2x00dev);
  383. pci_save_state(pci_dev);
  384. pci_disable_device(pci_dev);
  385. return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
  386. }
  387. EXPORT_SYMBOL_GPL(rt2x00pci_suspend);
  388. int rt2x00pci_resume(struct pci_dev *pci_dev)
  389. {
  390. struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
  391. struct rt2x00_dev *rt2x00dev = hw->priv;
  392. int retval;
  393. if (pci_set_power_state(pci_dev, PCI_D0) ||
  394. pci_enable_device(pci_dev) ||
  395. pci_restore_state(pci_dev)) {
  396. ERROR(rt2x00dev, "Failed to resume device.\n");
  397. return -EIO;
  398. }
  399. retval = rt2x00pci_alloc_reg(rt2x00dev);
  400. if (retval)
  401. return retval;
  402. retval = rt2x00lib_resume(rt2x00dev);
  403. if (retval)
  404. goto exit_free_reg;
  405. return 0;
  406. exit_free_reg:
  407. rt2x00pci_free_reg(rt2x00dev);
  408. return retval;
  409. }
  410. EXPORT_SYMBOL_GPL(rt2x00pci_resume);
  411. #endif /* CONFIG_PM */
  412. /*
  413. * rt2x00pci module information.
  414. */
  415. MODULE_AUTHOR(DRV_PROJECT);
  416. MODULE_VERSION(DRV_VERSION);
  417. MODULE_DESCRIPTION("rt2x00 library");
  418. MODULE_LICENSE("GPL");