blob: f9d0d76f87064a6a708c663267dd1441e5f6875e [file] [log] [blame]
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001/*
Ivo van Doorn811aa9c2008-02-03 15:42:53 +01002 Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
Ivo van Doorn95ea3622007-09-25 17:57:13 -07003 <http://rt2x00.serialmonkey.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the
17 Free Software Foundation, Inc.,
18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21/*
22 Module: rt2x00pci
23 Abstract: rt2x00 generic pci device routines.
24 */
25
Ivo van Doorn95ea3622007-09-25 17:57:13 -070026#include <linux/dma-mapping.h>
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/pci.h>
30
31#include "rt2x00.h"
32#include "rt2x00pci.h"
33
34/*
Ivo van Doorn95ea3622007-09-25 17:57:13 -070035 * TX data handlers.
36 */
Ivo van Doorn6db37862008-06-06 22:50:28 +020037int rt2x00pci_write_tx_data(struct queue_entry *entry)
Ivo van Doorn95ea3622007-09-25 17:57:13 -070038{
Ivo van Doornb8be63f2008-05-10 13:46:03 +020039 struct queue_entry_priv_pci *entry_priv = entry->priv_data;
Ivo van Doorn181d6902008-02-05 16:42:23 -050040 struct skb_frame_desc *skbdesc;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070041 u32 word;
42
Ivo van Doornb8be63f2008-05-10 13:46:03 +020043 rt2x00_desc_read(entry_priv->desc, 0, &word);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070044
Ivo van Doorn6db37862008-06-06 22:50:28 +020045 /*
46 * This should not happen, we already checked the entry
47 * was ours. When the hardware disagrees there has been
48 * a queue corruption!
49 */
50 if (unlikely(rt2x00_get_field32(word, TXD_ENTRY_OWNER_NIC) ||
51 rt2x00_get_field32(word, TXD_ENTRY_VALID))) {
52 ERROR(entry->queue->rt2x00dev,
53 "Corrupt queue %d, accessing entry which is not ours.\n"
Ivo van Doorn95ea3622007-09-25 17:57:13 -070054 "Please file bug report to %s.\n",
Ivo van Doorne58c6ac2008-04-21 19:00:47 +020055 entry->queue->qid, DRV_PROJECT);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070056 return -EINVAL;
57 }
58
Ivo van Doorn08992f72008-01-24 01:56:25 -080059 /*
60 * Fill in skb descriptor
61 */
Ivo van Doorn6db37862008-06-06 22:50:28 +020062 skbdesc = get_skb_frame_desc(entry->skb);
Johannes Berge039fa42008-05-15 12:55:29 +020063 memset(skbdesc, 0, sizeof(*skbdesc));
Ivo van Doornb8be63f2008-05-10 13:46:03 +020064 skbdesc->desc = entry_priv->desc;
Ivo van Doorn6db37862008-06-06 22:50:28 +020065 skbdesc->desc_len = entry->queue->desc_size;
Ivo van Doorn181d6902008-02-05 16:42:23 -050066 skbdesc->entry = entry;
Ivo van Doorn08992f72008-01-24 01:56:25 -080067
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +020068 rt2x00queue_map_txskb(entry->queue->rt2x00dev, entry->skb);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070069
Ivo van Doorn95ea3622007-09-25 17:57:13 -070070 return 0;
71}
72EXPORT_SYMBOL_GPL(rt2x00pci_write_tx_data);
73
74/*
Ivo van Doorn3957ccb2007-11-12 15:02:40 +010075 * TX/RX data handlers.
Ivo van Doorn95ea3622007-09-25 17:57:13 -070076 */
77void rt2x00pci_rxdone(struct rt2x00_dev *rt2x00dev)
78{
Ivo van Doorn181d6902008-02-05 16:42:23 -050079 struct data_queue *queue = rt2x00dev->rx;
80 struct queue_entry *entry;
Ivo van Doornb8be63f2008-05-10 13:46:03 +020081 struct queue_entry_priv_pci *entry_priv;
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +020082 struct skb_frame_desc *skbdesc;
Johannes Berg4150c572007-09-17 01:29:23 -040083 u32 word;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070084
85 while (1) {
Ivo van Doorn181d6902008-02-05 16:42:23 -050086 entry = rt2x00queue_get_entry(queue, Q_INDEX);
Ivo van Doornb8be63f2008-05-10 13:46:03 +020087 entry_priv = entry->priv_data;
88 rt2x00_desc_read(entry_priv->desc, 0, &word);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070089
Johannes Berg4150c572007-09-17 01:29:23 -040090 if (rt2x00_get_field32(word, RXD_ENTRY_OWNER_NIC))
Ivo van Doorn95ea3622007-09-25 17:57:13 -070091 break;
92
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +020093 /*
94 * Fill in desc fields of the skb descriptor
95 */
96 skbdesc = get_skb_frame_desc(entry->skb);
97 skbdesc->desc = entry_priv->desc;
98 skbdesc->desc_len = entry->queue->desc_size;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070099
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200100 /*
101 * Send the frame to rt2x00lib for further processing.
102 */
103 rt2x00lib_rxdone(rt2x00dev, entry);
104
105 /*
106 * Reset the RXD for this entry.
107 */
108 rt2x00dev->ops->lib->init_rxentry(rt2x00dev, entry);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700109
Ivo van Doorn181d6902008-02-05 16:42:23 -0500110 rt2x00queue_index_inc(queue, Q_INDEX);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700111 }
112}
113EXPORT_SYMBOL_GPL(rt2x00pci_rxdone);
114
Ivo van Doorn181d6902008-02-05 16:42:23 -0500115void rt2x00pci_txdone(struct rt2x00_dev *rt2x00dev, struct queue_entry *entry,
116 struct txdone_entry_desc *txdesc)
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100117{
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200118 struct queue_entry_priv_pci *entry_priv = entry->priv_data;
Johannes Berge039fa42008-05-15 12:55:29 +0200119 enum data_queue_qid qid = skb_get_queue_mapping(entry->skb);
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100120 u32 word;
121
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200122 /*
123 * Unmap the skb.
124 */
125 rt2x00queue_unmap_skb(rt2x00dev, entry->skb);
126
Ivo van Doorn181d6902008-02-05 16:42:23 -0500127 rt2x00lib_txdone(entry, txdesc);
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100128
129 /*
130 * Make this entry available for reuse.
131 */
132 entry->flags = 0;
133
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200134 rt2x00_desc_read(entry_priv->desc, 0, &word);
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100135 rt2x00_set_field32(&word, TXD_ENTRY_OWNER_NIC, 0);
136 rt2x00_set_field32(&word, TXD_ENTRY_VALID, 0);
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200137 rt2x00_desc_write(entry_priv->desc, 0, word);
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100138
Ivo van Doorn6db37862008-06-06 22:50:28 +0200139 __clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500140 rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100141
142 /*
Ivo van Doornb8697672008-06-06 22:53:14 +0200143 * If the data queue was below the threshold before the txdone
144 * handler we must make sure the packet queue in the mac80211 stack
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100145 * is reenabled when the txdone handler has finished.
146 */
Ivo van Doornb8697672008-06-06 22:53:14 +0200147 if (!rt2x00queue_threshold(entry->queue))
Johannes Berge039fa42008-05-15 12:55:29 +0200148 ieee80211_wake_queue(rt2x00dev->hw, qid);
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100149
150}
151EXPORT_SYMBOL_GPL(rt2x00pci_txdone);
152
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700153/*
154 * Device initialization handlers.
155 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500156static int rt2x00pci_alloc_queue_dma(struct rt2x00_dev *rt2x00dev,
157 struct data_queue *queue)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700158{
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200159 struct queue_entry_priv_pci *entry_priv;
Ivo van Doorn30b3a232008-02-17 17:33:24 +0100160 void *addr;
Ivo van Doorn9c9dd2c2008-02-10 22:46:52 +0100161 dma_addr_t dma;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700162 unsigned int i;
163
164 /*
165 * Allocate DMA memory for descriptor and buffer.
166 */
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200167 addr = dma_alloc_coherent(rt2x00dev->dev,
168 queue->limit * queue->desc_size,
169 &dma, GFP_KERNEL | GFP_DMA);
Ivo van Doorn30b3a232008-02-17 17:33:24 +0100170 if (!addr)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700171 return -ENOMEM;
172
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200173 memset(addr, 0, queue->limit * queue->desc_size);
Ivo van Doorn9c9dd2c2008-02-10 22:46:52 +0100174
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700175 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500176 * Initialize all queue entries to contain valid addresses.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700177 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500178 for (i = 0; i < queue->limit; i++) {
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200179 entry_priv = queue->entries[i].priv_data;
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200180 entry_priv->desc = addr + i * queue->desc_size;
181 entry_priv->desc_dma = dma + i * queue->desc_size;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700182 }
183
184 return 0;
185}
186
Ivo van Doorn181d6902008-02-05 16:42:23 -0500187static void rt2x00pci_free_queue_dma(struct rt2x00_dev *rt2x00dev,
188 struct data_queue *queue)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700189{
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200190 struct queue_entry_priv_pci *entry_priv =
191 queue->entries[0].priv_data;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500192
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200193 if (entry_priv->desc)
194 dma_free_coherent(rt2x00dev->dev,
195 queue->limit * queue->desc_size,
196 entry_priv->desc, entry_priv->desc_dma);
197 entry_priv->desc = NULL;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700198}
199
200int rt2x00pci_initialize(struct rt2x00_dev *rt2x00dev)
201{
Gertjan van Wingerde14a3bf82008-06-16 19:55:43 +0200202 struct pci_dev *pci_dev = to_pci_dev(rt2x00dev->dev);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500203 struct data_queue *queue;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700204 int status;
205
206 /*
207 * Allocate DMA
208 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500209 queue_for_each(rt2x00dev, queue) {
210 status = rt2x00pci_alloc_queue_dma(rt2x00dev, queue);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700211 if (status)
212 goto exit;
213 }
214
215 /*
216 * Register interrupt handler.
217 */
218 status = request_irq(pci_dev->irq, rt2x00dev->ops->lib->irq_handler,
219 IRQF_SHARED, pci_name(pci_dev), rt2x00dev);
220 if (status) {
221 ERROR(rt2x00dev, "IRQ %d allocation failed (error %d).\n",
222 pci_dev->irq, status);
Ivo van Doornb30cdfc2008-05-05 17:24:03 +0200223 goto exit;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700224 }
225
226 return 0;
227
228exit:
Ivo van Doornb30cdfc2008-05-05 17:24:03 +0200229 queue_for_each(rt2x00dev, queue)
230 rt2x00pci_free_queue_dma(rt2x00dev, queue);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700231
232 return status;
233}
234EXPORT_SYMBOL_GPL(rt2x00pci_initialize);
235
236void rt2x00pci_uninitialize(struct rt2x00_dev *rt2x00dev)
237{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500238 struct data_queue *queue;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700239
240 /*
241 * Free irq line.
242 */
Gertjan van Wingerde14a3bf82008-06-16 19:55:43 +0200243 free_irq(to_pci_dev(rt2x00dev->dev)->irq, rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700244
245 /*
246 * Free DMA
247 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500248 queue_for_each(rt2x00dev, queue)
249 rt2x00pci_free_queue_dma(rt2x00dev, queue);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700250}
251EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize);
252
253/*
254 * PCI driver handlers.
255 */
256static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
257{
258 kfree(rt2x00dev->rf);
259 rt2x00dev->rf = NULL;
260
261 kfree(rt2x00dev->eeprom);
262 rt2x00dev->eeprom = NULL;
263
Ivo van Doorn21795092008-02-10 22:49:13 +0100264 if (rt2x00dev->csr.base) {
265 iounmap(rt2x00dev->csr.base);
266 rt2x00dev->csr.base = NULL;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700267 }
268}
269
270static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
271{
Gertjan van Wingerde14a3bf82008-06-16 19:55:43 +0200272 struct pci_dev *pci_dev = to_pci_dev(rt2x00dev->dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700273
Ivo van Doorn21795092008-02-10 22:49:13 +0100274 rt2x00dev->csr.base = ioremap(pci_resource_start(pci_dev, 0),
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700275 pci_resource_len(pci_dev, 0));
Ivo van Doorn21795092008-02-10 22:49:13 +0100276 if (!rt2x00dev->csr.base)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700277 goto exit;
278
279 rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
280 if (!rt2x00dev->eeprom)
281 goto exit;
282
283 rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
284 if (!rt2x00dev->rf)
285 goto exit;
286
287 return 0;
288
289exit:
290 ERROR_PROBE("Failed to allocate registers.\n");
291
292 rt2x00pci_free_reg(rt2x00dev);
293
294 return -ENOMEM;
295}
296
297int rt2x00pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
298{
299 struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_data;
300 struct ieee80211_hw *hw;
301 struct rt2x00_dev *rt2x00dev;
302 int retval;
303
304 retval = pci_request_regions(pci_dev, pci_name(pci_dev));
305 if (retval) {
306 ERROR_PROBE("PCI request regions failed.\n");
307 return retval;
308 }
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
316 pci_set_master(pci_dev);
317
318 if (pci_set_mwi(pci_dev))
319 ERROR_PROBE("MWI not available.\n");
320
Gertjan van Wingerde14a3bf82008-06-16 19:55:43 +0200321 if (dma_set_mask(&pci_dev->dev, DMA_32BIT_MASK)) {
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700322 ERROR_PROBE("PCI DMA not supported.\n");
323 retval = -EIO;
324 goto exit_disable_device;
325 }
326
327 hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
328 if (!hw) {
329 ERROR_PROBE("Failed to allocate hardware.\n");
330 retval = -ENOMEM;
331 goto exit_disable_device;
332 }
333
334 pci_set_drvdata(pci_dev, hw);
335
336 rt2x00dev = hw->priv;
Gertjan van Wingerde14a3bf82008-06-16 19:55:43 +0200337 rt2x00dev->dev = &pci_dev->dev;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700338 rt2x00dev->ops = ops;
339 rt2x00dev->hw = hw;
340
341 retval = rt2x00pci_alloc_reg(rt2x00dev);
342 if (retval)
343 goto exit_free_device;
344
345 retval = rt2x00lib_probe_dev(rt2x00dev);
346 if (retval)
347 goto exit_free_reg;
348
349 return 0;
350
351exit_free_reg:
352 rt2x00pci_free_reg(rt2x00dev);
353
354exit_free_device:
355 ieee80211_free_hw(hw);
356
357exit_disable_device:
358 if (retval != -EBUSY)
359 pci_disable_device(pci_dev);
360
361exit_release_regions:
362 pci_release_regions(pci_dev);
363
364 pci_set_drvdata(pci_dev, NULL);
365
366 return retval;
367}
368EXPORT_SYMBOL_GPL(rt2x00pci_probe);
369
370void rt2x00pci_remove(struct pci_dev *pci_dev)
371{
372 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
373 struct rt2x00_dev *rt2x00dev = hw->priv;
374
375 /*
376 * Free all allocated data.
377 */
378 rt2x00lib_remove_dev(rt2x00dev);
379 rt2x00pci_free_reg(rt2x00dev);
380 ieee80211_free_hw(hw);
381
382 /*
383 * Free the PCI device data.
384 */
385 pci_set_drvdata(pci_dev, NULL);
386 pci_disable_device(pci_dev);
387 pci_release_regions(pci_dev);
388}
389EXPORT_SYMBOL_GPL(rt2x00pci_remove);
390
391#ifdef CONFIG_PM
392int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
393{
394 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
395 struct rt2x00_dev *rt2x00dev = hw->priv;
396 int retval;
397
398 retval = rt2x00lib_suspend(rt2x00dev, state);
399 if (retval)
400 return retval;
401
402 rt2x00pci_free_reg(rt2x00dev);
403
404 pci_save_state(pci_dev);
405 pci_disable_device(pci_dev);
406 return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
407}
408EXPORT_SYMBOL_GPL(rt2x00pci_suspend);
409
410int rt2x00pci_resume(struct pci_dev *pci_dev)
411{
412 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
413 struct rt2x00_dev *rt2x00dev = hw->priv;
414 int retval;
415
416 if (pci_set_power_state(pci_dev, PCI_D0) ||
417 pci_enable_device(pci_dev) ||
418 pci_restore_state(pci_dev)) {
419 ERROR(rt2x00dev, "Failed to resume device.\n");
420 return -EIO;
421 }
422
423 retval = rt2x00pci_alloc_reg(rt2x00dev);
424 if (retval)
425 return retval;
426
427 retval = rt2x00lib_resume(rt2x00dev);
428 if (retval)
429 goto exit_free_reg;
430
431 return 0;
432
433exit_free_reg:
434 rt2x00pci_free_reg(rt2x00dev);
435
436 return retval;
437}
438EXPORT_SYMBOL_GPL(rt2x00pci_resume);
439#endif /* CONFIG_PM */
440
441/*
442 * rt2x00pci module information.
443 */
444MODULE_AUTHOR(DRV_PROJECT);
445MODULE_VERSION(DRV_VERSION);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500446MODULE_DESCRIPTION("rt2x00 pci library");
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700447MODULE_LICENSE("GPL");