blob: 55d0614588b267bdec9193eb7b19ceefb3acb53a [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
26/*
27 * Set enviroment defines for rt2x00.h
28 */
29#define DRV_NAME "rt2x00pci"
30
31#include <linux/dma-mapping.h>
32#include <linux/kernel.h>
33#include <linux/module.h>
34#include <linux/pci.h>
35
36#include "rt2x00.h"
37#include "rt2x00pci.h"
38
39/*
40 * Beacon handlers.
41 */
42int rt2x00pci_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
43 struct ieee80211_tx_control *control)
44{
45 struct rt2x00_dev *rt2x00dev = hw->priv;
46 struct data_ring *ring =
47 rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_BEACON);
48 struct data_entry *entry = rt2x00_get_data_entry(ring);
49
50 /*
51 * Just in case mac80211 doesn't set this correctly,
52 * but we need this queue set for the descriptor
53 * initialization.
54 */
55 control->queue = IEEE80211_TX_QUEUE_BEACON;
56
57 /*
58 * Update the beacon entry.
59 */
60 memcpy(entry->data_addr, skb->data, skb->len);
61 rt2x00lib_write_tx_desc(rt2x00dev, entry->priv,
62 (struct ieee80211_hdr *)skb->data,
63 skb->len, control);
64
65 /*
66 * Enable beacon generation.
67 */
68 rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, control->queue);
69
70 return 0;
71}
72EXPORT_SYMBOL_GPL(rt2x00pci_beacon_update);
73
74/*
75 * TX data handlers.
76 */
77int rt2x00pci_write_tx_data(struct rt2x00_dev *rt2x00dev,
78 struct data_ring *ring, struct sk_buff *skb,
79 struct ieee80211_tx_control *control)
80{
81 struct ieee80211_hdr *ieee80211hdr = (struct ieee80211_hdr *)skb->data;
82 struct data_entry *entry = rt2x00_get_data_entry(ring);
Ivo van Doorn4bd7c452008-01-24 00:48:03 -080083 __le32 *txd = entry->priv;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070084 u32 word;
85
86 if (rt2x00_ring_full(ring)) {
87 ieee80211_stop_queue(rt2x00dev->hw, control->queue);
88 return -EINVAL;
89 }
90
91 rt2x00_desc_read(txd, 0, &word);
92
93 if (rt2x00_get_field32(word, TXD_ENTRY_OWNER_NIC) ||
94 rt2x00_get_field32(word, TXD_ENTRY_VALID)) {
95 ERROR(rt2x00dev,
96 "Arrived at non-free entry in the non-full queue %d.\n"
97 "Please file bug report to %s.\n",
98 control->queue, DRV_PROJECT);
99 ieee80211_stop_queue(rt2x00dev->hw, control->queue);
100 return -EINVAL;
101 }
102
103 entry->skb = skb;
104 memcpy(&entry->tx_status.control, control, sizeof(*control));
105 memcpy(entry->data_addr, skb->data, skb->len);
106 rt2x00lib_write_tx_desc(rt2x00dev, txd, ieee80211hdr,
107 skb->len, control);
108
109 rt2x00_ring_index_inc(ring);
110
111 if (rt2x00_ring_full(ring))
112 ieee80211_stop_queue(rt2x00dev->hw, control->queue);
113
114 return 0;
115}
116EXPORT_SYMBOL_GPL(rt2x00pci_write_tx_data);
117
118/*
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100119 * TX/RX data handlers.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700120 */
121void rt2x00pci_rxdone(struct rt2x00_dev *rt2x00dev)
122{
123 struct data_ring *ring = rt2x00dev->rx;
124 struct data_entry *entry;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700125 struct sk_buff *skb;
Ivo van Doornc5d0dc52008-01-06 23:40:27 +0100126 struct ieee80211_hdr *hdr;
Johannes Berg4150c572007-09-17 01:29:23 -0400127 struct rxdata_entry_desc desc;
Ivo van Doornc5d0dc52008-01-06 23:40:27 +0100128 int header_size;
Ivo van Doorn4bd7c452008-01-24 00:48:03 -0800129 __le32 *rxd;
Ivo van Doornc5d0dc52008-01-06 23:40:27 +0100130 int align;
Johannes Berg4150c572007-09-17 01:29:23 -0400131 u32 word;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700132
133 while (1) {
134 entry = rt2x00_get_data_entry(ring);
135 rxd = entry->priv;
Johannes Berg4150c572007-09-17 01:29:23 -0400136 rt2x00_desc_read(rxd, 0, &word);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700137
Johannes Berg4150c572007-09-17 01:29:23 -0400138 if (rt2x00_get_field32(word, RXD_ENTRY_OWNER_NIC))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700139 break;
140
Johannes Berg4150c572007-09-17 01:29:23 -0400141 memset(&desc, 0x00, sizeof(desc));
142 rt2x00dev->ops->lib->fill_rxdone(entry, &desc);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700143
Ivo van Doornc5d0dc52008-01-06 23:40:27 +0100144 hdr = (struct ieee80211_hdr *)entry->data_addr;
145 header_size =
146 ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
147
148 /*
149 * The data behind the ieee80211 header must be
150 * aligned on a 4 byte boundary.
151 */
Ivo van Doornd101f642008-01-11 20:53:07 +0100152 align = header_size % 4;
Ivo van Doornc5d0dc52008-01-06 23:40:27 +0100153
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700154 /*
155 * Allocate the sk_buffer, initialize it and copy
156 * all data into it.
157 */
Ivo van Doornc5d0dc52008-01-06 23:40:27 +0100158 skb = dev_alloc_skb(desc.size + align);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700159 if (!skb)
160 return;
161
Ivo van Doornc5d0dc52008-01-06 23:40:27 +0100162 skb_reserve(skb, align);
163 memcpy(skb_put(skb, desc.size), entry->data_addr, desc.size);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700164
165 /*
166 * Send the frame to rt2x00lib for further processing.
167 */
Johannes Berg4150c572007-09-17 01:29:23 -0400168 rt2x00lib_rxdone(entry, skb, &desc);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700169
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700170 if (test_bit(DEVICE_ENABLED_RADIO, &ring->rt2x00dev->flags)) {
Johannes Berg4150c572007-09-17 01:29:23 -0400171 rt2x00_set_field32(&word, RXD_ENTRY_OWNER_NIC, 1);
172 rt2x00_desc_write(rxd, 0, word);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700173 }
174
175 rt2x00_ring_index_inc(ring);
176 }
177}
178EXPORT_SYMBOL_GPL(rt2x00pci_rxdone);
179
Ivo van Doorn3957ccb2007-11-12 15:02:40 +0100180void rt2x00pci_txdone(struct rt2x00_dev *rt2x00dev, struct data_entry *entry,
181 const int tx_status, const int retry)
182{
183 u32 word;
184
185 rt2x00lib_txdone(entry, tx_status, retry);
186
187 /*
188 * Make this entry available for reuse.
189 */
190 entry->flags = 0;
191
192 rt2x00_desc_read(entry->priv, 0, &word);
193 rt2x00_set_field32(&word, TXD_ENTRY_OWNER_NIC, 0);
194 rt2x00_set_field32(&word, TXD_ENTRY_VALID, 0);
195 rt2x00_desc_write(entry->priv, 0, word);
196
197 rt2x00_ring_index_done_inc(entry->ring);
198
199 /*
200 * If the data ring was full before the txdone handler
201 * we must make sure the packet queue in the mac80211 stack
202 * is reenabled when the txdone handler has finished.
203 */
204 if (!rt2x00_ring_full(entry->ring))
205 ieee80211_wake_queue(rt2x00dev->hw,
206 entry->tx_status.control.queue);
207
208}
209EXPORT_SYMBOL_GPL(rt2x00pci_txdone);
210
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700211/*
212 * Device initialization handlers.
213 */
214#define priv_offset(__ring, __i) \
215({ \
216 ring->data_addr + (i * ring->desc_size); \
217})
218
219#define data_addr_offset(__ring, __i) \
220({ \
221 (__ring)->data_addr + \
222 ((__ring)->stats.limit * (__ring)->desc_size) + \
223 ((__i) * (__ring)->data_size); \
224})
225
226#define data_dma_offset(__ring, __i) \
227({ \
228 (__ring)->data_dma + \
229 ((__ring)->stats.limit * (__ring)->desc_size) + \
230 ((__i) * (__ring)->data_size); \
231})
232
233static int rt2x00pci_alloc_dma(struct rt2x00_dev *rt2x00dev,
234 struct data_ring *ring)
235{
236 unsigned int i;
237
238 /*
239 * Allocate DMA memory for descriptor and buffer.
240 */
241 ring->data_addr = pci_alloc_consistent(rt2x00dev_pci(rt2x00dev),
242 rt2x00_get_ring_size(ring),
243 &ring->data_dma);
244 if (!ring->data_addr)
245 return -ENOMEM;
246
247 /*
248 * Initialize all ring entries to contain valid
249 * addresses.
250 */
251 for (i = 0; i < ring->stats.limit; i++) {
252 ring->entry[i].priv = priv_offset(ring, i);
253 ring->entry[i].data_addr = data_addr_offset(ring, i);
254 ring->entry[i].data_dma = data_dma_offset(ring, i);
255 }
256
257 return 0;
258}
259
260static void rt2x00pci_free_dma(struct rt2x00_dev *rt2x00dev,
261 struct data_ring *ring)
262{
263 if (ring->data_addr)
264 pci_free_consistent(rt2x00dev_pci(rt2x00dev),
265 rt2x00_get_ring_size(ring),
266 ring->data_addr, ring->data_dma);
267 ring->data_addr = NULL;
268}
269
270int rt2x00pci_initialize(struct rt2x00_dev *rt2x00dev)
271{
272 struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
273 struct data_ring *ring;
274 int status;
275
276 /*
277 * Allocate DMA
278 */
279 ring_for_each(rt2x00dev, ring) {
280 status = rt2x00pci_alloc_dma(rt2x00dev, ring);
281 if (status)
282 goto exit;
283 }
284
285 /*
286 * Register interrupt handler.
287 */
288 status = request_irq(pci_dev->irq, rt2x00dev->ops->lib->irq_handler,
289 IRQF_SHARED, pci_name(pci_dev), rt2x00dev);
290 if (status) {
291 ERROR(rt2x00dev, "IRQ %d allocation failed (error %d).\n",
292 pci_dev->irq, status);
293 return status;
294 }
295
296 return 0;
297
298exit:
299 rt2x00pci_uninitialize(rt2x00dev);
300
301 return status;
302}
303EXPORT_SYMBOL_GPL(rt2x00pci_initialize);
304
305void rt2x00pci_uninitialize(struct rt2x00_dev *rt2x00dev)
306{
307 struct data_ring *ring;
308
309 /*
310 * Free irq line.
311 */
312 free_irq(rt2x00dev_pci(rt2x00dev)->irq, rt2x00dev);
313
314 /*
315 * Free DMA
316 */
317 ring_for_each(rt2x00dev, ring)
318 rt2x00pci_free_dma(rt2x00dev, ring);
319}
320EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize);
321
322/*
323 * PCI driver handlers.
324 */
325static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
326{
327 kfree(rt2x00dev->rf);
328 rt2x00dev->rf = NULL;
329
330 kfree(rt2x00dev->eeprom);
331 rt2x00dev->eeprom = NULL;
332
333 if (rt2x00dev->csr_addr) {
334 iounmap(rt2x00dev->csr_addr);
335 rt2x00dev->csr_addr = NULL;
336 }
337}
338
339static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
340{
341 struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
342
343 rt2x00dev->csr_addr = ioremap(pci_resource_start(pci_dev, 0),
344 pci_resource_len(pci_dev, 0));
345 if (!rt2x00dev->csr_addr)
346 goto exit;
347
348 rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
349 if (!rt2x00dev->eeprom)
350 goto exit;
351
352 rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
353 if (!rt2x00dev->rf)
354 goto exit;
355
356 return 0;
357
358exit:
359 ERROR_PROBE("Failed to allocate registers.\n");
360
361 rt2x00pci_free_reg(rt2x00dev);
362
363 return -ENOMEM;
364}
365
366int rt2x00pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
367{
368 struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_data;
369 struct ieee80211_hw *hw;
370 struct rt2x00_dev *rt2x00dev;
371 int retval;
372
373 retval = pci_request_regions(pci_dev, pci_name(pci_dev));
374 if (retval) {
375 ERROR_PROBE("PCI request regions failed.\n");
376 return retval;
377 }
378
379 retval = pci_enable_device(pci_dev);
380 if (retval) {
381 ERROR_PROBE("Enable device failed.\n");
382 goto exit_release_regions;
383 }
384
385 pci_set_master(pci_dev);
386
387 if (pci_set_mwi(pci_dev))
388 ERROR_PROBE("MWI not available.\n");
389
390 if (pci_set_dma_mask(pci_dev, DMA_64BIT_MASK) &&
391 pci_set_dma_mask(pci_dev, DMA_32BIT_MASK)) {
392 ERROR_PROBE("PCI DMA not supported.\n");
393 retval = -EIO;
394 goto exit_disable_device;
395 }
396
397 hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
398 if (!hw) {
399 ERROR_PROBE("Failed to allocate hardware.\n");
400 retval = -ENOMEM;
401 goto exit_disable_device;
402 }
403
404 pci_set_drvdata(pci_dev, hw);
405
406 rt2x00dev = hw->priv;
407 rt2x00dev->dev = pci_dev;
408 rt2x00dev->ops = ops;
409 rt2x00dev->hw = hw;
410
411 retval = rt2x00pci_alloc_reg(rt2x00dev);
412 if (retval)
413 goto exit_free_device;
414
415 retval = rt2x00lib_probe_dev(rt2x00dev);
416 if (retval)
417 goto exit_free_reg;
418
419 return 0;
420
421exit_free_reg:
422 rt2x00pci_free_reg(rt2x00dev);
423
424exit_free_device:
425 ieee80211_free_hw(hw);
426
427exit_disable_device:
428 if (retval != -EBUSY)
429 pci_disable_device(pci_dev);
430
431exit_release_regions:
432 pci_release_regions(pci_dev);
433
434 pci_set_drvdata(pci_dev, NULL);
435
436 return retval;
437}
438EXPORT_SYMBOL_GPL(rt2x00pci_probe);
439
440void rt2x00pci_remove(struct pci_dev *pci_dev)
441{
442 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
443 struct rt2x00_dev *rt2x00dev = hw->priv;
444
445 /*
446 * Free all allocated data.
447 */
448 rt2x00lib_remove_dev(rt2x00dev);
449 rt2x00pci_free_reg(rt2x00dev);
450 ieee80211_free_hw(hw);
451
452 /*
453 * Free the PCI device data.
454 */
455 pci_set_drvdata(pci_dev, NULL);
456 pci_disable_device(pci_dev);
457 pci_release_regions(pci_dev);
458}
459EXPORT_SYMBOL_GPL(rt2x00pci_remove);
460
461#ifdef CONFIG_PM
462int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
463{
464 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
465 struct rt2x00_dev *rt2x00dev = hw->priv;
466 int retval;
467
468 retval = rt2x00lib_suspend(rt2x00dev, state);
469 if (retval)
470 return retval;
471
472 rt2x00pci_free_reg(rt2x00dev);
473
474 pci_save_state(pci_dev);
475 pci_disable_device(pci_dev);
476 return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
477}
478EXPORT_SYMBOL_GPL(rt2x00pci_suspend);
479
480int rt2x00pci_resume(struct pci_dev *pci_dev)
481{
482 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
483 struct rt2x00_dev *rt2x00dev = hw->priv;
484 int retval;
485
486 if (pci_set_power_state(pci_dev, PCI_D0) ||
487 pci_enable_device(pci_dev) ||
488 pci_restore_state(pci_dev)) {
489 ERROR(rt2x00dev, "Failed to resume device.\n");
490 return -EIO;
491 }
492
493 retval = rt2x00pci_alloc_reg(rt2x00dev);
494 if (retval)
495 return retval;
496
497 retval = rt2x00lib_resume(rt2x00dev);
498 if (retval)
499 goto exit_free_reg;
500
501 return 0;
502
503exit_free_reg:
504 rt2x00pci_free_reg(rt2x00dev);
505
506 return retval;
507}
508EXPORT_SYMBOL_GPL(rt2x00pci_resume);
509#endif /* CONFIG_PM */
510
511/*
512 * rt2x00pci module information.
513 */
514MODULE_AUTHOR(DRV_PROJECT);
515MODULE_VERSION(DRV_VERSION);
516MODULE_DESCRIPTION("rt2x00 library");
517MODULE_LICENSE("GPL");