blob: 275c8a1e6638d0ee493299120f25d35c33defd77 [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 */
37int rt2x00pci_write_tx_data(struct rt2x00_dev *rt2x00dev,
Ivo van Doorn181d6902008-02-05 16:42:23 -050038 struct data_queue *queue, struct sk_buff *skb,
Ivo van Doorn95ea3622007-09-25 17:57:13 -070039 struct ieee80211_tx_control *control)
40{
Ivo van Doorn181d6902008-02-05 16:42:23 -050041 struct queue_entry *entry = rt2x00queue_get_entry(queue, Q_INDEX);
42 struct queue_entry_priv_pci_tx *priv_tx = entry->priv_data;
43 struct skb_frame_desc *skbdesc;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070044 u32 word;
45
Ivo van Doorn181d6902008-02-05 16:42:23 -050046 if (rt2x00queue_full(queue))
Ivo van Doorn95ea3622007-09-25 17:57:13 -070047 return -EINVAL;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070048
Ivo van Doorn181d6902008-02-05 16:42:23 -050049 rt2x00_desc_read(priv_tx->desc, 0, &word);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070050
51 if (rt2x00_get_field32(word, TXD_ENTRY_OWNER_NIC) ||
52 rt2x00_get_field32(word, TXD_ENTRY_VALID)) {
53 ERROR(rt2x00dev,
54 "Arrived at non-free entry in the non-full queue %d.\n"
55 "Please file bug report to %s.\n",
56 control->queue, DRV_PROJECT);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070057 return -EINVAL;
58 }
59
Ivo van Doorn08992f72008-01-24 01:56:25 -080060 /*
61 * Fill in skb descriptor
62 */
Ivo van Doorn181d6902008-02-05 16:42:23 -050063 skbdesc = get_skb_frame_desc(skb);
64 memset(skbdesc, 0, sizeof(*skbdesc));
65 skbdesc->data = skb->data;
66 skbdesc->data_len = queue->data_size;
67 skbdesc->desc = priv_tx->desc;
68 skbdesc->desc_len = queue->desc_size;
69 skbdesc->entry = entry;
Ivo van Doorn08992f72008-01-24 01:56:25 -080070
Ivo van Doorn181d6902008-02-05 16:42:23 -050071 memcpy(priv_tx->data, skb->data, skb->len);
Ivo van Doorn08992f72008-01-24 01:56:25 -080072 rt2x00lib_write_tx_desc(rt2x00dev, skb, control);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070073
Ivo van Doorn181d6902008-02-05 16:42:23 -050074 rt2x00queue_index_inc(queue, Q_INDEX);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070075
Ivo van Doorn95ea3622007-09-25 17:57:13 -070076 return 0;
77}
78EXPORT_SYMBOL_GPL(rt2x00pci_write_tx_data);
79
80/*
Ivo van Doorn3957ccb2007-11-12 15:02:40 +010081 * TX/RX data handlers.
Ivo van Doorn95ea3622007-09-25 17:57:13 -070082 */
83void rt2x00pci_rxdone(struct rt2x00_dev *rt2x00dev)
84{
Ivo van Doorn181d6902008-02-05 16:42:23 -050085 struct data_queue *queue = rt2x00dev->rx;
86 struct queue_entry *entry;
87 struct queue_entry_priv_pci_rx *priv_rx;
Ivo van Doornc5d0dc52008-01-06 23:40:27 +010088 struct ieee80211_hdr *hdr;
Ivo van Doorn181d6902008-02-05 16:42:23 -050089 struct skb_frame_desc *skbdesc;
90 struct rxdone_entry_desc rxdesc;
Ivo van Doornc5d0dc52008-01-06 23:40:27 +010091 int header_size;
92 int align;
Johannes Berg4150c572007-09-17 01:29:23 -040093 u32 word;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070094
95 while (1) {
Ivo van Doorn181d6902008-02-05 16:42:23 -050096 entry = rt2x00queue_get_entry(queue, Q_INDEX);
97 priv_rx = entry->priv_data;
98 rt2x00_desc_read(priv_rx->desc, 0, &word);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070099
Johannes Berg4150c572007-09-17 01:29:23 -0400100 if (rt2x00_get_field32(word, RXD_ENTRY_OWNER_NIC))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700101 break;
102
Ivo van Doorn181d6902008-02-05 16:42:23 -0500103 memset(&rxdesc, 0, sizeof(rxdesc));
104 rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700105
Ivo van Doorn181d6902008-02-05 16:42:23 -0500106 hdr = (struct ieee80211_hdr *)priv_rx->data;
Ivo van Doornc5d0dc52008-01-06 23:40:27 +0100107 header_size =
108 ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
109
110 /*
111 * The data behind the ieee80211 header must be
112 * aligned on a 4 byte boundary.
113 */
Ivo van Doornd101f642008-01-11 20:53:07 +0100114 align = header_size % 4;
Ivo van Doornc5d0dc52008-01-06 23:40:27 +0100115
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700116 /*
117 * Allocate the sk_buffer, initialize it and copy
118 * all data into it.
119 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500120 entry->skb = dev_alloc_skb(rxdesc.size + align);
121 if (!entry->skb)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700122 return;
123
Ivo van Doorn181d6902008-02-05 16:42:23 -0500124 skb_reserve(entry->skb, align);
125 memcpy(skb_put(entry->skb, rxdesc.size),
126 priv_rx->data, rxdesc.size);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700127
128 /*
Ivo van Doorn08992f72008-01-24 01:56:25 -0800129 * Fill in skb descriptor
130 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500131 skbdesc = get_skb_frame_desc(entry->skb);
132 memset(skbdesc, 0, sizeof(*skbdesc));
133 skbdesc->data = entry->skb->data;
134 skbdesc->data_len = queue->data_size;
135 skbdesc->desc = priv_rx->desc;
136 skbdesc->desc_len = queue->desc_size;
Ivo van Doorn08992f72008-01-24 01:56:25 -0800137 skbdesc->entry = entry;
138
139 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700140 * Send the frame to rt2x00lib for further processing.
141 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500142 rt2x00lib_rxdone(entry, &rxdesc);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700143
Ivo van Doorn181d6902008-02-05 16:42:23 -0500144 if (test_bit(DEVICE_ENABLED_RADIO, &queue->rt2x00dev->flags)) {
Johannes Berg4150c572007-09-17 01:29:23 -0400145 rt2x00_set_field32(&word, RXD_ENTRY_OWNER_NIC, 1);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500146 rt2x00_desc_write(priv_rx->desc, 0, word);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700147 }
148
Ivo van Doorn181d6902008-02-05 16:42:23 -0500149 rt2x00queue_index_inc(queue, Q_INDEX);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700150 }
151}
152EXPORT_SYMBOL_GPL(rt2x00pci_rxdone);
153
Ivo van Doorn181d6902008-02-05 16:42:23 -0500154void rt2x00pci_txdone(struct rt2x00_dev *rt2x00dev, struct queue_entry *entry,
155 struct txdone_entry_desc *txdesc)
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100156{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500157 struct queue_entry_priv_pci_tx *priv_tx = entry->priv_data;
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100158 u32 word;
159
Ivo van Doorn181d6902008-02-05 16:42:23 -0500160 txdesc->control = &priv_tx->control;
161 rt2x00lib_txdone(entry, txdesc);
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100162
163 /*
164 * Make this entry available for reuse.
165 */
166 entry->flags = 0;
167
Ivo van Doorn181d6902008-02-05 16:42:23 -0500168 rt2x00_desc_read(priv_tx->desc, 0, &word);
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100169 rt2x00_set_field32(&word, TXD_ENTRY_OWNER_NIC, 0);
170 rt2x00_set_field32(&word, TXD_ENTRY_VALID, 0);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500171 rt2x00_desc_write(priv_tx->desc, 0, word);
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100172
Ivo van Doorn181d6902008-02-05 16:42:23 -0500173 rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100174
175 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500176 * If the data queue was full before the txdone handler
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100177 * we must make sure the packet queue in the mac80211 stack
178 * is reenabled when the txdone handler has finished.
179 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500180 if (!rt2x00queue_full(entry->queue))
181 ieee80211_wake_queue(rt2x00dev->hw, priv_tx->control.queue);
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100182
183}
184EXPORT_SYMBOL_GPL(rt2x00pci_txdone);
185
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700186/*
187 * Device initialization handlers.
188 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500189#define dma_size(__queue) \
190({ \
191 (__queue)->limit * \
192 ((__queue)->desc_size + (__queue)->data_size);\
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700193})
194
Ivo van Doorn181d6902008-02-05 16:42:23 -0500195#define priv_offset(__queue, __base, __i) \
196({ \
197 (__base) + ((__i) * (__queue)->desc_size); \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700198})
199
Ivo van Doorn181d6902008-02-05 16:42:23 -0500200#define data_addr_offset(__queue, __base, __i) \
201({ \
202 (__base) + \
203 ((__queue)->limit * (__queue)->desc_size) + \
204 ((__i) * (__queue)->data_size); \
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700205})
206
Ivo van Doorn181d6902008-02-05 16:42:23 -0500207#define data_dma_offset(__queue, __base, __i) \
208({ \
209 (__base) + \
210 ((__queue)->limit * (__queue)->desc_size) + \
211 ((__i) * (__queue)->data_size); \
212})
213
214static int rt2x00pci_alloc_queue_dma(struct rt2x00_dev *rt2x00dev,
215 struct data_queue *queue)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700216{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500217 struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
218 struct queue_entry_priv_pci_tx *priv_tx;
219 void *data_addr;
220 dma_addr_t data_dma;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700221 unsigned int i;
222
223 /*
224 * Allocate DMA memory for descriptor and buffer.
225 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500226 data_addr = pci_alloc_consistent(pci_dev, dma_size(queue), &data_dma);
227 if (!data_addr)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700228 return -ENOMEM;
229
230 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500231 * Initialize all queue entries to contain valid addresses.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700232 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500233 for (i = 0; i < queue->limit; i++) {
234 priv_tx = queue->entries[i].priv_data;
235 priv_tx->desc = priv_offset(queue, data_addr, i);
236 priv_tx->data = data_addr_offset(queue, data_addr, i);
237 priv_tx->dma = data_dma_offset(queue, data_dma, i);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700238 }
239
240 return 0;
241}
242
Ivo van Doorn181d6902008-02-05 16:42:23 -0500243static void rt2x00pci_free_queue_dma(struct rt2x00_dev *rt2x00dev,
244 struct data_queue *queue)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700245{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500246 struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
247 struct queue_entry_priv_pci_tx *priv_tx = queue->entries[0].priv_data;
248
249 if (priv_tx->data)
250 pci_free_consistent(pci_dev, dma_size(queue),
251 priv_tx->data, priv_tx->dma);
252 priv_tx->data = NULL;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700253}
254
255int rt2x00pci_initialize(struct rt2x00_dev *rt2x00dev)
256{
257 struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500258 struct data_queue *queue;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700259 int status;
260
261 /*
262 * Allocate DMA
263 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500264 queue_for_each(rt2x00dev, queue) {
265 status = rt2x00pci_alloc_queue_dma(rt2x00dev, queue);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700266 if (status)
267 goto exit;
268 }
269
270 /*
271 * Register interrupt handler.
272 */
273 status = request_irq(pci_dev->irq, rt2x00dev->ops->lib->irq_handler,
274 IRQF_SHARED, pci_name(pci_dev), rt2x00dev);
275 if (status) {
276 ERROR(rt2x00dev, "IRQ %d allocation failed (error %d).\n",
277 pci_dev->irq, status);
278 return status;
279 }
280
281 return 0;
282
283exit:
284 rt2x00pci_uninitialize(rt2x00dev);
285
286 return status;
287}
288EXPORT_SYMBOL_GPL(rt2x00pci_initialize);
289
290void rt2x00pci_uninitialize(struct rt2x00_dev *rt2x00dev)
291{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500292 struct data_queue *queue;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700293
294 /*
295 * Free irq line.
296 */
297 free_irq(rt2x00dev_pci(rt2x00dev)->irq, rt2x00dev);
298
299 /*
300 * Free DMA
301 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500302 queue_for_each(rt2x00dev, queue)
303 rt2x00pci_free_queue_dma(rt2x00dev, queue);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700304}
305EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize);
306
307/*
308 * PCI driver handlers.
309 */
310static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
311{
312 kfree(rt2x00dev->rf);
313 rt2x00dev->rf = NULL;
314
315 kfree(rt2x00dev->eeprom);
316 rt2x00dev->eeprom = NULL;
317
318 if (rt2x00dev->csr_addr) {
319 iounmap(rt2x00dev->csr_addr);
320 rt2x00dev->csr_addr = NULL;
321 }
322}
323
324static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
325{
326 struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
327
328 rt2x00dev->csr_addr = ioremap(pci_resource_start(pci_dev, 0),
329 pci_resource_len(pci_dev, 0));
330 if (!rt2x00dev->csr_addr)
331 goto exit;
332
333 rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
334 if (!rt2x00dev->eeprom)
335 goto exit;
336
337 rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
338 if (!rt2x00dev->rf)
339 goto exit;
340
341 return 0;
342
343exit:
344 ERROR_PROBE("Failed to allocate registers.\n");
345
346 rt2x00pci_free_reg(rt2x00dev);
347
348 return -ENOMEM;
349}
350
351int rt2x00pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
352{
353 struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_data;
354 struct ieee80211_hw *hw;
355 struct rt2x00_dev *rt2x00dev;
356 int retval;
357
358 retval = pci_request_regions(pci_dev, pci_name(pci_dev));
359 if (retval) {
360 ERROR_PROBE("PCI request regions failed.\n");
361 return retval;
362 }
363
364 retval = pci_enable_device(pci_dev);
365 if (retval) {
366 ERROR_PROBE("Enable device failed.\n");
367 goto exit_release_regions;
368 }
369
370 pci_set_master(pci_dev);
371
372 if (pci_set_mwi(pci_dev))
373 ERROR_PROBE("MWI not available.\n");
374
375 if (pci_set_dma_mask(pci_dev, DMA_64BIT_MASK) &&
376 pci_set_dma_mask(pci_dev, DMA_32BIT_MASK)) {
377 ERROR_PROBE("PCI DMA not supported.\n");
378 retval = -EIO;
379 goto exit_disable_device;
380 }
381
382 hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
383 if (!hw) {
384 ERROR_PROBE("Failed to allocate hardware.\n");
385 retval = -ENOMEM;
386 goto exit_disable_device;
387 }
388
389 pci_set_drvdata(pci_dev, hw);
390
391 rt2x00dev = hw->priv;
392 rt2x00dev->dev = pci_dev;
393 rt2x00dev->ops = ops;
394 rt2x00dev->hw = hw;
395
396 retval = rt2x00pci_alloc_reg(rt2x00dev);
397 if (retval)
398 goto exit_free_device;
399
400 retval = rt2x00lib_probe_dev(rt2x00dev);
401 if (retval)
402 goto exit_free_reg;
403
404 return 0;
405
406exit_free_reg:
407 rt2x00pci_free_reg(rt2x00dev);
408
409exit_free_device:
410 ieee80211_free_hw(hw);
411
412exit_disable_device:
413 if (retval != -EBUSY)
414 pci_disable_device(pci_dev);
415
416exit_release_regions:
417 pci_release_regions(pci_dev);
418
419 pci_set_drvdata(pci_dev, NULL);
420
421 return retval;
422}
423EXPORT_SYMBOL_GPL(rt2x00pci_probe);
424
425void rt2x00pci_remove(struct pci_dev *pci_dev)
426{
427 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
428 struct rt2x00_dev *rt2x00dev = hw->priv;
429
430 /*
431 * Free all allocated data.
432 */
433 rt2x00lib_remove_dev(rt2x00dev);
434 rt2x00pci_free_reg(rt2x00dev);
435 ieee80211_free_hw(hw);
436
437 /*
438 * Free the PCI device data.
439 */
440 pci_set_drvdata(pci_dev, NULL);
441 pci_disable_device(pci_dev);
442 pci_release_regions(pci_dev);
443}
444EXPORT_SYMBOL_GPL(rt2x00pci_remove);
445
446#ifdef CONFIG_PM
447int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
448{
449 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
450 struct rt2x00_dev *rt2x00dev = hw->priv;
451 int retval;
452
453 retval = rt2x00lib_suspend(rt2x00dev, state);
454 if (retval)
455 return retval;
456
457 rt2x00pci_free_reg(rt2x00dev);
458
459 pci_save_state(pci_dev);
460 pci_disable_device(pci_dev);
461 return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
462}
463EXPORT_SYMBOL_GPL(rt2x00pci_suspend);
464
465int rt2x00pci_resume(struct pci_dev *pci_dev)
466{
467 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
468 struct rt2x00_dev *rt2x00dev = hw->priv;
469 int retval;
470
471 if (pci_set_power_state(pci_dev, PCI_D0) ||
472 pci_enable_device(pci_dev) ||
473 pci_restore_state(pci_dev)) {
474 ERROR(rt2x00dev, "Failed to resume device.\n");
475 return -EIO;
476 }
477
478 retval = rt2x00pci_alloc_reg(rt2x00dev);
479 if (retval)
480 return retval;
481
482 retval = rt2x00lib_resume(rt2x00dev);
483 if (retval)
484 goto exit_free_reg;
485
486 return 0;
487
488exit_free_reg:
489 rt2x00pci_free_reg(rt2x00dev);
490
491 return retval;
492}
493EXPORT_SYMBOL_GPL(rt2x00pci_resume);
494#endif /* CONFIG_PM */
495
496/*
497 * rt2x00pci module information.
498 */
499MODULE_AUTHOR(DRV_PROJECT);
500MODULE_VERSION(DRV_VERSION);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500501MODULE_DESCRIPTION("rt2x00 pci library");
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700502MODULE_LICENSE("GPL");