]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/wireless/rt2x00/rt2x00pci.c
Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[net-next-2.6.git] / drivers / net / wireless / rt2x00 / rt2x00pci.c
CommitLineData
95ea3627 1/*
9c9a0d14 2 Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
95ea3627
ID
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
95ea3627
ID
26#include <linux/dma-mapping.h>
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/pci.h>
5a0e3ad6 30#include <linux/slab.h>
95ea3627
ID
31
32#include "rt2x00.h"
33#include "rt2x00pci.h"
34
c9c3b1a5
ID
35/*
36 * Register access.
37 */
38int rt2x00pci_regbusy_read(struct rt2x00_dev *rt2x00dev,
39 const unsigned int offset,
40 const struct rt2x00_field32 field,
41 u32 *reg)
42{
43 unsigned int i;
44
c70762f9
AB
45 if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
46 return 0;
47
c9c3b1a5
ID
48 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
49 rt2x00pci_register_read(rt2x00dev, offset, reg);
50 if (!rt2x00_get_field32(*reg, field))
51 return 1;
52 udelay(REGISTER_BUSY_DELAY);
53 }
54
55 ERROR(rt2x00dev, "Indirect register access failed: "
56 "offset=0x%.08x, value=0x%.08x\n", offset, *reg);
57 *reg = ~0;
58
59 return 0;
60}
61EXPORT_SYMBOL_GPL(rt2x00pci_regbusy_read);
62
95ea3627
ID
63/*
64 * TX data handlers.
65 */
41086693
HS
66int rt2x00pci_write_tx_data(struct queue_entry *entry,
67 struct txentry_desc *txdesc)
95ea3627 68{
798b7adb 69 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
95ea3627 70
6db3786a
ID
71 /*
72 * This should not happen, we already checked the entry
73 * was ours. When the hardware disagrees there has been
74 * a queue corruption!
75 */
798b7adb
ID
76 if (unlikely(rt2x00dev->ops->lib->get_entry_state(entry))) {
77 ERROR(rt2x00dev,
6db3786a 78 "Corrupt queue %d, accessing entry which is not ours.\n"
95ea3627 79 "Please file bug report to %s.\n",
e58c6aca 80 entry->queue->qid, DRV_PROJECT);
95ea3627
ID
81 return -EINVAL;
82 }
83
0b8004aa
GW
84 /*
85 * Add the requested extra tx headroom in front of the skb.
86 */
87 skb_push(entry->skb, rt2x00dev->ops->extra_tx_headroom);
88 memset(entry->skb->data, 0, rt2x00dev->ops->extra_tx_headroom);
89
baaffe67
GW
90 /*
91 * Call the driver's write_tx_datadesc function, if it exists.
92 */
93 if (rt2x00dev->ops->lib->write_tx_datadesc)
94 rt2x00dev->ops->lib->write_tx_datadesc(entry, txdesc);
95
0b8004aa
GW
96 /*
97 * Map the skb to DMA.
98 */
99 if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags))
100 rt2x00queue_map_txskb(rt2x00dev, entry->skb);
101
95ea3627
ID
102 return 0;
103}
104EXPORT_SYMBOL_GPL(rt2x00pci_write_tx_data);
105
106/*
3957ccb5 107 * TX/RX data handlers.
95ea3627 108 */
0b8004aa
GW
109void rt2x00pci_txdone(struct queue_entry *entry,
110 struct txdone_entry_desc *txdesc)
111{
112 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
113 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
114
115 /*
116 * Unmap the skb.
117 */
118 rt2x00queue_unmap_skb(rt2x00dev, entry->skb);
119
120 /*
121 * Remove the extra tx headroom from the skb.
122 */
123 skb_pull(entry->skb, rt2x00dev->ops->extra_tx_headroom);
124
125 /*
126 * Signal that the TX descriptor is no longer in the skb.
127 */
128 skbdesc->flags &= ~SKBDESC_DESC_IN_SKB;
129
130 /*
131 * Pass on to rt2x00lib.
132 */
133 rt2x00lib_txdone(entry, txdesc);
134}
135EXPORT_SYMBOL_GPL(rt2x00pci_txdone);
136
95ea3627
ID
137void rt2x00pci_rxdone(struct rt2x00_dev *rt2x00dev)
138{
181d6902
ID
139 struct data_queue *queue = rt2x00dev->rx;
140 struct queue_entry *entry;
b8be63ff 141 struct queue_entry_priv_pci *entry_priv;
c4da0048 142 struct skb_frame_desc *skbdesc;
95ea3627
ID
143
144 while (1) {
181d6902 145 entry = rt2x00queue_get_entry(queue, Q_INDEX);
b8be63ff 146 entry_priv = entry->priv_data;
95ea3627 147
798b7adb 148 if (rt2x00dev->ops->lib->get_entry_state(entry))
95ea3627
ID
149 break;
150
c4da0048
GW
151 /*
152 * Fill in desc fields of the skb descriptor
153 */
154 skbdesc = get_skb_frame_desc(entry->skb);
155 skbdesc->desc = entry_priv->desc;
156 skbdesc->desc_len = entry->queue->desc_size;
157
158 /*
159 * Send the frame to rt2x00lib for further processing.
160 */
161 rt2x00lib_rxdone(rt2x00dev, entry);
95ea3627
ID
162 }
163}
164EXPORT_SYMBOL_GPL(rt2x00pci_rxdone);
165
166/*
167 * Device initialization handlers.
168 */
181d6902
ID
169static int rt2x00pci_alloc_queue_dma(struct rt2x00_dev *rt2x00dev,
170 struct data_queue *queue)
95ea3627 171{
b8be63ff 172 struct queue_entry_priv_pci *entry_priv;
30b3a23c
ID
173 void *addr;
174 dma_addr_t dma;
95ea3627
ID
175 unsigned int i;
176
177 /*
178 * Allocate DMA memory for descriptor and buffer.
179 */
c4da0048
GW
180 addr = dma_alloc_coherent(rt2x00dev->dev,
181 queue->limit * queue->desc_size,
182 &dma, GFP_KERNEL | GFP_DMA);
30b3a23c 183 if (!addr)
95ea3627
ID
184 return -ENOMEM;
185
c4da0048 186 memset(addr, 0, queue->limit * queue->desc_size);
9c9dd2c9 187
95ea3627 188 /*
181d6902 189 * Initialize all queue entries to contain valid addresses.
95ea3627 190 */
181d6902 191 for (i = 0; i < queue->limit; i++) {
b8be63ff 192 entry_priv = queue->entries[i].priv_data;
c4da0048
GW
193 entry_priv->desc = addr + i * queue->desc_size;
194 entry_priv->desc_dma = dma + i * queue->desc_size;
95ea3627
ID
195 }
196
197 return 0;
198}
199
181d6902
ID
200static void rt2x00pci_free_queue_dma(struct rt2x00_dev *rt2x00dev,
201 struct data_queue *queue)
95ea3627 202{
b8be63ff
ID
203 struct queue_entry_priv_pci *entry_priv =
204 queue->entries[0].priv_data;
181d6902 205
c4da0048
GW
206 if (entry_priv->desc)
207 dma_free_coherent(rt2x00dev->dev,
208 queue->limit * queue->desc_size,
209 entry_priv->desc, entry_priv->desc_dma);
210 entry_priv->desc = NULL;
95ea3627
ID
211}
212
213int rt2x00pci_initialize(struct rt2x00_dev *rt2x00dev)
214{
181d6902 215 struct data_queue *queue;
95ea3627
ID
216 int status;
217
218 /*
219 * Allocate DMA
220 */
181d6902
ID
221 queue_for_each(rt2x00dev, queue) {
222 status = rt2x00pci_alloc_queue_dma(rt2x00dev, queue);
95ea3627
ID
223 if (status)
224 goto exit;
225 }
226
227 /*
228 * Register interrupt handler.
229 */
440ddada
ID
230 status = request_irq(rt2x00dev->irq, rt2x00dev->ops->lib->irq_handler,
231 IRQF_SHARED, rt2x00dev->name, rt2x00dev);
95ea3627
ID
232 if (status) {
233 ERROR(rt2x00dev, "IRQ %d allocation failed (error %d).\n",
440ddada 234 rt2x00dev->irq, status);
b30cdfc5 235 goto exit;
95ea3627
ID
236 }
237
238 return 0;
239
240exit:
b30cdfc5
ID
241 queue_for_each(rt2x00dev, queue)
242 rt2x00pci_free_queue_dma(rt2x00dev, queue);
95ea3627
ID
243
244 return status;
245}
246EXPORT_SYMBOL_GPL(rt2x00pci_initialize);
247
248void rt2x00pci_uninitialize(struct rt2x00_dev *rt2x00dev)
249{
181d6902 250 struct data_queue *queue;
95ea3627
ID
251
252 /*
253 * Free irq line.
254 */
52a9bd2a 255 free_irq(rt2x00dev->irq, rt2x00dev);
95ea3627
ID
256
257 /*
258 * Free DMA
259 */
181d6902
ID
260 queue_for_each(rt2x00dev, queue)
261 rt2x00pci_free_queue_dma(rt2x00dev, queue);
95ea3627
ID
262}
263EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize);
264
265/*
266 * PCI driver handlers.
267 */
268static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
269{
270 kfree(rt2x00dev->rf);
271 rt2x00dev->rf = NULL;
272
273 kfree(rt2x00dev->eeprom);
274 rt2x00dev->eeprom = NULL;
275
21795094
ID
276 if (rt2x00dev->csr.base) {
277 iounmap(rt2x00dev->csr.base);
278 rt2x00dev->csr.base = NULL;
95ea3627
ID
279 }
280}
281
282static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
283{
14a3bf89 284 struct pci_dev *pci_dev = to_pci_dev(rt2x00dev->dev);
95ea3627 285
275f165f 286 rt2x00dev->csr.base = pci_ioremap_bar(pci_dev, 0);
21795094 287 if (!rt2x00dev->csr.base)
95ea3627
ID
288 goto exit;
289
290 rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
291 if (!rt2x00dev->eeprom)
292 goto exit;
293
294 rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
295 if (!rt2x00dev->rf)
296 goto exit;
297
298 return 0;
299
300exit:
301 ERROR_PROBE("Failed to allocate registers.\n");
302
303 rt2x00pci_free_reg(rt2x00dev);
304
305 return -ENOMEM;
306}
307
308int rt2x00pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
309{
310 struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_data;
311 struct ieee80211_hw *hw;
312 struct rt2x00_dev *rt2x00dev;
313 int retval;
314
315 retval = pci_request_regions(pci_dev, pci_name(pci_dev));
316 if (retval) {
317 ERROR_PROBE("PCI request regions failed.\n");
318 return retval;
319 }
320
321 retval = pci_enable_device(pci_dev);
322 if (retval) {
323 ERROR_PROBE("Enable device failed.\n");
324 goto exit_release_regions;
325 }
326
327 pci_set_master(pci_dev);
328
329 if (pci_set_mwi(pci_dev))
330 ERROR_PROBE("MWI not available.\n");
331
284901a9 332 if (dma_set_mask(&pci_dev->dev, DMA_BIT_MASK(32))) {
95ea3627
ID
333 ERROR_PROBE("PCI DMA not supported.\n");
334 retval = -EIO;
335 goto exit_disable_device;
336 }
337
338 hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
339 if (!hw) {
340 ERROR_PROBE("Failed to allocate hardware.\n");
341 retval = -ENOMEM;
342 goto exit_disable_device;
343 }
344
345 pci_set_drvdata(pci_dev, hw);
346
347 rt2x00dev = hw->priv;
14a3bf89 348 rt2x00dev->dev = &pci_dev->dev;
95ea3627
ID
349 rt2x00dev->ops = ops;
350 rt2x00dev->hw = hw;
440ddada
ID
351 rt2x00dev->irq = pci_dev->irq;
352 rt2x00dev->name = pci_name(pci_dev);
353
6e1fdd11
GW
354 if (pci_dev->is_pcie)
355 rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_PCIE);
356 else
357 rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_PCI);
2015d192 358
95ea3627
ID
359 retval = rt2x00pci_alloc_reg(rt2x00dev);
360 if (retval)
361 goto exit_free_device;
362
363 retval = rt2x00lib_probe_dev(rt2x00dev);
364 if (retval)
365 goto exit_free_reg;
366
367 return 0;
368
369exit_free_reg:
370 rt2x00pci_free_reg(rt2x00dev);
371
372exit_free_device:
373 ieee80211_free_hw(hw);
374
375exit_disable_device:
376 if (retval != -EBUSY)
377 pci_disable_device(pci_dev);
378
379exit_release_regions:
380 pci_release_regions(pci_dev);
381
382 pci_set_drvdata(pci_dev, NULL);
383
384 return retval;
385}
386EXPORT_SYMBOL_GPL(rt2x00pci_probe);
387
388void rt2x00pci_remove(struct pci_dev *pci_dev)
389{
390 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
391 struct rt2x00_dev *rt2x00dev = hw->priv;
392
393 /*
394 * Free all allocated data.
395 */
396 rt2x00lib_remove_dev(rt2x00dev);
397 rt2x00pci_free_reg(rt2x00dev);
398 ieee80211_free_hw(hw);
399
400 /*
401 * Free the PCI device data.
402 */
403 pci_set_drvdata(pci_dev, NULL);
404 pci_disable_device(pci_dev);
405 pci_release_regions(pci_dev);
406}
407EXPORT_SYMBOL_GPL(rt2x00pci_remove);
408
409#ifdef CONFIG_PM
410int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
411{
412 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
413 struct rt2x00_dev *rt2x00dev = hw->priv;
414 int retval;
415
416 retval = rt2x00lib_suspend(rt2x00dev, state);
417 if (retval)
418 return retval;
419
95ea3627
ID
420 pci_save_state(pci_dev);
421 pci_disable_device(pci_dev);
422 return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
423}
424EXPORT_SYMBOL_GPL(rt2x00pci_suspend);
425
426int rt2x00pci_resume(struct pci_dev *pci_dev)
427{
428 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
429 struct rt2x00_dev *rt2x00dev = hw->priv;
95ea3627
ID
430
431 if (pci_set_power_state(pci_dev, PCI_D0) ||
432 pci_enable_device(pci_dev) ||
433 pci_restore_state(pci_dev)) {
434 ERROR(rt2x00dev, "Failed to resume device.\n");
435 return -EIO;
436 }
437
499a214c 438 return rt2x00lib_resume(rt2x00dev);
95ea3627
ID
439}
440EXPORT_SYMBOL_GPL(rt2x00pci_resume);
441#endif /* CONFIG_PM */
442
443/*
444 * rt2x00pci module information.
445 */
446MODULE_AUTHOR(DRV_PROJECT);
447MODULE_VERSION(DRV_VERSION);
181d6902 448MODULE_DESCRIPTION("rt2x00 pci library");
95ea3627 449MODULE_LICENSE("GPL");