blob: c1d7c10e58fecc2413d9c1977a1019d1b5c11b62 [file] [log] [blame]
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001/*
2 Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
3 <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/*
35 * Beacon handlers.
36 */
37int rt2x00pci_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
38 struct ieee80211_tx_control *control)
39{
40 struct rt2x00_dev *rt2x00dev = hw->priv;
Ivo van Doorn08992f72008-01-24 01:56:25 -080041 struct skb_desc *desc;
42 struct data_ring *ring;
43 struct data_entry *entry;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070044
45 /*
46 * Just in case mac80211 doesn't set this correctly,
47 * but we need this queue set for the descriptor
48 * initialization.
49 */
50 control->queue = IEEE80211_TX_QUEUE_BEACON;
Ivo van Doorn08992f72008-01-24 01:56:25 -080051 ring = rt2x00lib_get_ring(rt2x00dev, control->queue);
52 entry = rt2x00_get_data_entry(ring);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070053
54 /*
Ivo van Doorn08992f72008-01-24 01:56:25 -080055 * Fill in skb descriptor
Ivo van Doorn95ea3622007-09-25 17:57:13 -070056 */
Ivo van Doorn08992f72008-01-24 01:56:25 -080057 desc = get_skb_desc(skb);
58 desc->desc_len = ring->desc_size;
59 desc->data_len = skb->len;
60 desc->desc = entry->priv;
61 desc->data = skb->data;
62 desc->ring = ring;
63 desc->entry = entry;
64
Ivo van Doorn95ea3622007-09-25 17:57:13 -070065 memcpy(entry->data_addr, skb->data, skb->len);
Ivo van Doorn08992f72008-01-24 01:56:25 -080066 rt2x00lib_write_tx_desc(rt2x00dev, skb, control);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070067
68 /*
69 * Enable beacon generation.
70 */
71 rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, control->queue);
72
73 return 0;
74}
75EXPORT_SYMBOL_GPL(rt2x00pci_beacon_update);
76
77/*
78 * TX data handlers.
79 */
80int rt2x00pci_write_tx_data(struct rt2x00_dev *rt2x00dev,
81 struct data_ring *ring, struct sk_buff *skb,
82 struct ieee80211_tx_control *control)
83{
Ivo van Doorn95ea3622007-09-25 17:57:13 -070084 struct data_entry *entry = rt2x00_get_data_entry(ring);
Ivo van Doorn4bd7c452008-01-24 00:48:03 -080085 __le32 *txd = entry->priv;
Ivo van Doorn08992f72008-01-24 01:56:25 -080086 struct skb_desc *desc;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070087 u32 word;
88
89 if (rt2x00_ring_full(ring)) {
90 ieee80211_stop_queue(rt2x00dev->hw, control->queue);
91 return -EINVAL;
92 }
93
94 rt2x00_desc_read(txd, 0, &word);
95
96 if (rt2x00_get_field32(word, TXD_ENTRY_OWNER_NIC) ||
97 rt2x00_get_field32(word, TXD_ENTRY_VALID)) {
98 ERROR(rt2x00dev,
99 "Arrived at non-free entry in the non-full queue %d.\n"
100 "Please file bug report to %s.\n",
101 control->queue, DRV_PROJECT);
102 ieee80211_stop_queue(rt2x00dev->hw, control->queue);
103 return -EINVAL;
104 }
105
Ivo van Doorn08992f72008-01-24 01:56:25 -0800106 /*
107 * Fill in skb descriptor
108 */
109 desc = get_skb_desc(skb);
110 desc->desc_len = ring->desc_size;
111 desc->data_len = skb->len;
112 desc->desc = entry->priv;
113 desc->data = skb->data;
114 desc->ring = ring;
115 desc->entry = entry;
116
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700117 memcpy(entry->data_addr, skb->data, skb->len);
Ivo van Doorn08992f72008-01-24 01:56:25 -0800118 rt2x00lib_write_tx_desc(rt2x00dev, skb, control);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700119
120 rt2x00_ring_index_inc(ring);
121
122 if (rt2x00_ring_full(ring))
123 ieee80211_stop_queue(rt2x00dev->hw, control->queue);
124
125 return 0;
126}
127EXPORT_SYMBOL_GPL(rt2x00pci_write_tx_data);
128
129/*
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100130 * TX/RX data handlers.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700131 */
132void rt2x00pci_rxdone(struct rt2x00_dev *rt2x00dev)
133{
134 struct data_ring *ring = rt2x00dev->rx;
135 struct data_entry *entry;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700136 struct sk_buff *skb;
Ivo van Doornc5d0dc52008-01-06 23:40:27 +0100137 struct ieee80211_hdr *hdr;
Ivo van Doorn08992f72008-01-24 01:56:25 -0800138 struct skb_desc *skbdesc;
Johannes Berg4150c572007-09-17 01:29:23 -0400139 struct rxdata_entry_desc desc;
Ivo van Doornc5d0dc52008-01-06 23:40:27 +0100140 int header_size;
Ivo van Doorn4bd7c452008-01-24 00:48:03 -0800141 __le32 *rxd;
Ivo van Doornc5d0dc52008-01-06 23:40:27 +0100142 int align;
Johannes Berg4150c572007-09-17 01:29:23 -0400143 u32 word;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700144
145 while (1) {
146 entry = rt2x00_get_data_entry(ring);
147 rxd = entry->priv;
Johannes Berg4150c572007-09-17 01:29:23 -0400148 rt2x00_desc_read(rxd, 0, &word);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700149
Johannes Berg4150c572007-09-17 01:29:23 -0400150 if (rt2x00_get_field32(word, RXD_ENTRY_OWNER_NIC))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700151 break;
152
Ivo van Doorn08992f72008-01-24 01:56:25 -0800153 memset(&desc, 0, sizeof(desc));
Johannes Berg4150c572007-09-17 01:29:23 -0400154 rt2x00dev->ops->lib->fill_rxdone(entry, &desc);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700155
Ivo van Doornc5d0dc52008-01-06 23:40:27 +0100156 hdr = (struct ieee80211_hdr *)entry->data_addr;
157 header_size =
158 ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
159
160 /*
161 * The data behind the ieee80211 header must be
162 * aligned on a 4 byte boundary.
163 */
Ivo van Doornd101f642008-01-11 20:53:07 +0100164 align = header_size % 4;
Ivo van Doornc5d0dc52008-01-06 23:40:27 +0100165
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700166 /*
167 * Allocate the sk_buffer, initialize it and copy
168 * all data into it.
169 */
Ivo van Doornc5d0dc52008-01-06 23:40:27 +0100170 skb = dev_alloc_skb(desc.size + align);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700171 if (!skb)
172 return;
173
Ivo van Doornc5d0dc52008-01-06 23:40:27 +0100174 skb_reserve(skb, align);
175 memcpy(skb_put(skb, desc.size), entry->data_addr, desc.size);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700176
177 /*
Ivo van Doorn08992f72008-01-24 01:56:25 -0800178 * Fill in skb descriptor
179 */
180 skbdesc = get_skb_desc(skb);
181 skbdesc->desc_len = desc.size;
182 skbdesc->data_len = entry->ring->desc_size;
183 skbdesc->desc = entry->priv;
184 skbdesc->data = skb->data;
185 skbdesc->ring = ring;
186 skbdesc->entry = entry;
187
188 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700189 * Send the frame to rt2x00lib for further processing.
190 */
Johannes Berg4150c572007-09-17 01:29:23 -0400191 rt2x00lib_rxdone(entry, skb, &desc);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700192
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700193 if (test_bit(DEVICE_ENABLED_RADIO, &ring->rt2x00dev->flags)) {
Johannes Berg4150c572007-09-17 01:29:23 -0400194 rt2x00_set_field32(&word, RXD_ENTRY_OWNER_NIC, 1);
195 rt2x00_desc_write(rxd, 0, word);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700196 }
197
198 rt2x00_ring_index_inc(ring);
199 }
200}
201EXPORT_SYMBOL_GPL(rt2x00pci_rxdone);
202
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100203void rt2x00pci_txdone(struct rt2x00_dev *rt2x00dev, struct data_entry *entry,
204 const int tx_status, const int retry)
205{
206 u32 word;
207
208 rt2x00lib_txdone(entry, tx_status, retry);
209
210 /*
211 * Make this entry available for reuse.
212 */
213 entry->flags = 0;
214
215 rt2x00_desc_read(entry->priv, 0, &word);
216 rt2x00_set_field32(&word, TXD_ENTRY_OWNER_NIC, 0);
217 rt2x00_set_field32(&word, TXD_ENTRY_VALID, 0);
218 rt2x00_desc_write(entry->priv, 0, word);
219
220 rt2x00_ring_index_done_inc(entry->ring);
221
222 /*
223 * If the data ring was full before the txdone handler
224 * we must make sure the packet queue in the mac80211 stack
225 * is reenabled when the txdone handler has finished.
226 */
227 if (!rt2x00_ring_full(entry->ring))
228 ieee80211_wake_queue(rt2x00dev->hw,
229 entry->tx_status.control.queue);
230
231}
232EXPORT_SYMBOL_GPL(rt2x00pci_txdone);
233
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700234/*
235 * Device initialization handlers.
236 */
237#define priv_offset(__ring, __i) \
238({ \
239 ring->data_addr + (i * ring->desc_size); \
240})
241
242#define data_addr_offset(__ring, __i) \
243({ \
244 (__ring)->data_addr + \
245 ((__ring)->stats.limit * (__ring)->desc_size) + \
246 ((__i) * (__ring)->data_size); \
247})
248
249#define data_dma_offset(__ring, __i) \
250({ \
251 (__ring)->data_dma + \
252 ((__ring)->stats.limit * (__ring)->desc_size) + \
253 ((__i) * (__ring)->data_size); \
254})
255
256static int rt2x00pci_alloc_dma(struct rt2x00_dev *rt2x00dev,
257 struct data_ring *ring)
258{
259 unsigned int i;
260
261 /*
262 * Allocate DMA memory for descriptor and buffer.
263 */
264 ring->data_addr = pci_alloc_consistent(rt2x00dev_pci(rt2x00dev),
265 rt2x00_get_ring_size(ring),
266 &ring->data_dma);
267 if (!ring->data_addr)
268 return -ENOMEM;
269
270 /*
271 * Initialize all ring entries to contain valid
272 * addresses.
273 */
274 for (i = 0; i < ring->stats.limit; i++) {
275 ring->entry[i].priv = priv_offset(ring, i);
276 ring->entry[i].data_addr = data_addr_offset(ring, i);
277 ring->entry[i].data_dma = data_dma_offset(ring, i);
278 }
279
280 return 0;
281}
282
283static void rt2x00pci_free_dma(struct rt2x00_dev *rt2x00dev,
284 struct data_ring *ring)
285{
286 if (ring->data_addr)
287 pci_free_consistent(rt2x00dev_pci(rt2x00dev),
288 rt2x00_get_ring_size(ring),
289 ring->data_addr, ring->data_dma);
290 ring->data_addr = NULL;
291}
292
293int rt2x00pci_initialize(struct rt2x00_dev *rt2x00dev)
294{
295 struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
296 struct data_ring *ring;
297 int status;
298
299 /*
300 * Allocate DMA
301 */
302 ring_for_each(rt2x00dev, ring) {
303 status = rt2x00pci_alloc_dma(rt2x00dev, ring);
304 if (status)
305 goto exit;
306 }
307
308 /*
309 * Register interrupt handler.
310 */
311 status = request_irq(pci_dev->irq, rt2x00dev->ops->lib->irq_handler,
312 IRQF_SHARED, pci_name(pci_dev), rt2x00dev);
313 if (status) {
314 ERROR(rt2x00dev, "IRQ %d allocation failed (error %d).\n",
315 pci_dev->irq, status);
316 return status;
317 }
318
319 return 0;
320
321exit:
322 rt2x00pci_uninitialize(rt2x00dev);
323
324 return status;
325}
326EXPORT_SYMBOL_GPL(rt2x00pci_initialize);
327
328void rt2x00pci_uninitialize(struct rt2x00_dev *rt2x00dev)
329{
330 struct data_ring *ring;
331
332 /*
333 * Free irq line.
334 */
335 free_irq(rt2x00dev_pci(rt2x00dev)->irq, rt2x00dev);
336
337 /*
338 * Free DMA
339 */
340 ring_for_each(rt2x00dev, ring)
341 rt2x00pci_free_dma(rt2x00dev, ring);
342}
343EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize);
344
345/*
346 * PCI driver handlers.
347 */
348static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
349{
350 kfree(rt2x00dev->rf);
351 rt2x00dev->rf = NULL;
352
353 kfree(rt2x00dev->eeprom);
354 rt2x00dev->eeprom = NULL;
355
356 if (rt2x00dev->csr_addr) {
357 iounmap(rt2x00dev->csr_addr);
358 rt2x00dev->csr_addr = NULL;
359 }
360}
361
362static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
363{
364 struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
365
366 rt2x00dev->csr_addr = ioremap(pci_resource_start(pci_dev, 0),
367 pci_resource_len(pci_dev, 0));
368 if (!rt2x00dev->csr_addr)
369 goto exit;
370
371 rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
372 if (!rt2x00dev->eeprom)
373 goto exit;
374
375 rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
376 if (!rt2x00dev->rf)
377 goto exit;
378
379 return 0;
380
381exit:
382 ERROR_PROBE("Failed to allocate registers.\n");
383
384 rt2x00pci_free_reg(rt2x00dev);
385
386 return -ENOMEM;
387}
388
389int rt2x00pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
390{
391 struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_data;
392 struct ieee80211_hw *hw;
393 struct rt2x00_dev *rt2x00dev;
394 int retval;
395
396 retval = pci_request_regions(pci_dev, pci_name(pci_dev));
397 if (retval) {
398 ERROR_PROBE("PCI request regions failed.\n");
399 return retval;
400 }
401
402 retval = pci_enable_device(pci_dev);
403 if (retval) {
404 ERROR_PROBE("Enable device failed.\n");
405 goto exit_release_regions;
406 }
407
408 pci_set_master(pci_dev);
409
410 if (pci_set_mwi(pci_dev))
411 ERROR_PROBE("MWI not available.\n");
412
413 if (pci_set_dma_mask(pci_dev, DMA_64BIT_MASK) &&
414 pci_set_dma_mask(pci_dev, DMA_32BIT_MASK)) {
415 ERROR_PROBE("PCI DMA not supported.\n");
416 retval = -EIO;
417 goto exit_disable_device;
418 }
419
420 hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
421 if (!hw) {
422 ERROR_PROBE("Failed to allocate hardware.\n");
423 retval = -ENOMEM;
424 goto exit_disable_device;
425 }
426
427 pci_set_drvdata(pci_dev, hw);
428
429 rt2x00dev = hw->priv;
430 rt2x00dev->dev = pci_dev;
431 rt2x00dev->ops = ops;
432 rt2x00dev->hw = hw;
433
434 retval = rt2x00pci_alloc_reg(rt2x00dev);
435 if (retval)
436 goto exit_free_device;
437
438 retval = rt2x00lib_probe_dev(rt2x00dev);
439 if (retval)
440 goto exit_free_reg;
441
442 return 0;
443
444exit_free_reg:
445 rt2x00pci_free_reg(rt2x00dev);
446
447exit_free_device:
448 ieee80211_free_hw(hw);
449
450exit_disable_device:
451 if (retval != -EBUSY)
452 pci_disable_device(pci_dev);
453
454exit_release_regions:
455 pci_release_regions(pci_dev);
456
457 pci_set_drvdata(pci_dev, NULL);
458
459 return retval;
460}
461EXPORT_SYMBOL_GPL(rt2x00pci_probe);
462
463void rt2x00pci_remove(struct pci_dev *pci_dev)
464{
465 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
466 struct rt2x00_dev *rt2x00dev = hw->priv;
467
468 /*
469 * Free all allocated data.
470 */
471 rt2x00lib_remove_dev(rt2x00dev);
472 rt2x00pci_free_reg(rt2x00dev);
473 ieee80211_free_hw(hw);
474
475 /*
476 * Free the PCI device data.
477 */
478 pci_set_drvdata(pci_dev, NULL);
479 pci_disable_device(pci_dev);
480 pci_release_regions(pci_dev);
481}
482EXPORT_SYMBOL_GPL(rt2x00pci_remove);
483
484#ifdef CONFIG_PM
485int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
486{
487 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
488 struct rt2x00_dev *rt2x00dev = hw->priv;
489 int retval;
490
491 retval = rt2x00lib_suspend(rt2x00dev, state);
492 if (retval)
493 return retval;
494
495 rt2x00pci_free_reg(rt2x00dev);
496
497 pci_save_state(pci_dev);
498 pci_disable_device(pci_dev);
499 return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
500}
501EXPORT_SYMBOL_GPL(rt2x00pci_suspend);
502
503int rt2x00pci_resume(struct pci_dev *pci_dev)
504{
505 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
506 struct rt2x00_dev *rt2x00dev = hw->priv;
507 int retval;
508
509 if (pci_set_power_state(pci_dev, PCI_D0) ||
510 pci_enable_device(pci_dev) ||
511 pci_restore_state(pci_dev)) {
512 ERROR(rt2x00dev, "Failed to resume device.\n");
513 return -EIO;
514 }
515
516 retval = rt2x00pci_alloc_reg(rt2x00dev);
517 if (retval)
518 return retval;
519
520 retval = rt2x00lib_resume(rt2x00dev);
521 if (retval)
522 goto exit_free_reg;
523
524 return 0;
525
526exit_free_reg:
527 rt2x00pci_free_reg(rt2x00dev);
528
529 return retval;
530}
531EXPORT_SYMBOL_GPL(rt2x00pci_resume);
532#endif /* CONFIG_PM */
533
534/*
535 * rt2x00pci module information.
536 */
537MODULE_AUTHOR(DRV_PROJECT);
538MODULE_VERSION(DRV_VERSION);
539MODULE_DESCRIPTION("rt2x00 library");
540MODULE_LICENSE("GPL");