Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6] / drivers / net / wireless / rt2x00 / rt2x00pci.c
1 /*
2         Copyright (C) 2004 - 2008 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 #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  * TX data handlers.
36  */
37 int rt2x00pci_write_tx_data(struct queue_entry *entry)
38 {
39         struct queue_entry_priv_pci *entry_priv = entry->priv_data;
40         struct skb_frame_desc *skbdesc;
41         u32 word;
42
43         rt2x00_desc_read(entry_priv->desc, 0, &word);
44
45         /*
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"
54                       "Please file bug report to %s.\n",
55                       entry->queue->qid, DRV_PROJECT);
56                 return -EINVAL;
57         }
58
59         /*
60          * Fill in skb descriptor
61          */
62         skbdesc = get_skb_frame_desc(entry->skb);
63         skbdesc->desc = entry_priv->desc;
64         skbdesc->desc_len = entry->queue->desc_size;
65
66         return 0;
67 }
68 EXPORT_SYMBOL_GPL(rt2x00pci_write_tx_data);
69
70 /*
71  * TX/RX data handlers.
72  */
73 void rt2x00pci_rxdone(struct rt2x00_dev *rt2x00dev)
74 {
75         struct data_queue *queue = rt2x00dev->rx;
76         struct queue_entry *entry;
77         struct queue_entry_priv_pci *entry_priv;
78         struct skb_frame_desc *skbdesc;
79         u32 word;
80
81         while (1) {
82                 entry = rt2x00queue_get_entry(queue, Q_INDEX);
83                 entry_priv = entry->priv_data;
84                 rt2x00_desc_read(entry_priv->desc, 0, &word);
85
86                 if (rt2x00_get_field32(word, RXD_ENTRY_OWNER_NIC))
87                         break;
88
89                 /*
90                  * Fill in desc fields of the skb descriptor
91                  */
92                 skbdesc = get_skb_frame_desc(entry->skb);
93                 skbdesc->desc = entry_priv->desc;
94                 skbdesc->desc_len = entry->queue->desc_size;
95
96                 /*
97                  * Send the frame to rt2x00lib for further processing.
98                  */
99                 rt2x00lib_rxdone(rt2x00dev, entry);
100         }
101 }
102 EXPORT_SYMBOL_GPL(rt2x00pci_rxdone);
103
104 /*
105  * Device initialization handlers.
106  */
107 static int rt2x00pci_alloc_queue_dma(struct rt2x00_dev *rt2x00dev,
108                                      struct data_queue *queue)
109 {
110         struct queue_entry_priv_pci *entry_priv;
111         void *addr;
112         dma_addr_t dma;
113         unsigned int i;
114
115         /*
116          * Allocate DMA memory for descriptor and buffer.
117          */
118         addr = dma_alloc_coherent(rt2x00dev->dev,
119                                   queue->limit * queue->desc_size,
120                                   &dma, GFP_KERNEL | GFP_DMA);
121         if (!addr)
122                 return -ENOMEM;
123
124         memset(addr, 0, queue->limit * queue->desc_size);
125
126         /*
127          * Initialize all queue entries to contain valid addresses.
128          */
129         for (i = 0; i < queue->limit; i++) {
130                 entry_priv = queue->entries[i].priv_data;
131                 entry_priv->desc = addr + i * queue->desc_size;
132                 entry_priv->desc_dma = dma + i * queue->desc_size;
133         }
134
135         return 0;
136 }
137
138 static void rt2x00pci_free_queue_dma(struct rt2x00_dev *rt2x00dev,
139                                      struct data_queue *queue)
140 {
141         struct queue_entry_priv_pci *entry_priv =
142             queue->entries[0].priv_data;
143
144         if (entry_priv->desc)
145                 dma_free_coherent(rt2x00dev->dev,
146                                   queue->limit * queue->desc_size,
147                                   entry_priv->desc, entry_priv->desc_dma);
148         entry_priv->desc = NULL;
149 }
150
151 int rt2x00pci_initialize(struct rt2x00_dev *rt2x00dev)
152 {
153         struct pci_dev *pci_dev = to_pci_dev(rt2x00dev->dev);
154         struct data_queue *queue;
155         int status;
156
157         /*
158          * Allocate DMA
159          */
160         queue_for_each(rt2x00dev, queue) {
161                 status = rt2x00pci_alloc_queue_dma(rt2x00dev, queue);
162                 if (status)
163                         goto exit;
164         }
165
166         /*
167          * Register interrupt handler.
168          */
169         status = request_irq(pci_dev->irq, rt2x00dev->ops->lib->irq_handler,
170                              IRQF_SHARED, pci_name(pci_dev), rt2x00dev);
171         if (status) {
172                 ERROR(rt2x00dev, "IRQ %d allocation failed (error %d).\n",
173                       pci_dev->irq, status);
174                 goto exit;
175         }
176
177         return 0;
178
179 exit:
180         queue_for_each(rt2x00dev, queue)
181                 rt2x00pci_free_queue_dma(rt2x00dev, queue);
182
183         return status;
184 }
185 EXPORT_SYMBOL_GPL(rt2x00pci_initialize);
186
187 void rt2x00pci_uninitialize(struct rt2x00_dev *rt2x00dev)
188 {
189         struct data_queue *queue;
190
191         /*
192          * Free irq line.
193          */
194         free_irq(to_pci_dev(rt2x00dev->dev)->irq, rt2x00dev);
195
196         /*
197          * Free DMA
198          */
199         queue_for_each(rt2x00dev, queue)
200                 rt2x00pci_free_queue_dma(rt2x00dev, queue);
201 }
202 EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize);
203
204 /*
205  * PCI driver handlers.
206  */
207 static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
208 {
209         kfree(rt2x00dev->rf);
210         rt2x00dev->rf = NULL;
211
212         kfree(rt2x00dev->eeprom);
213         rt2x00dev->eeprom = NULL;
214
215         if (rt2x00dev->csr.base) {
216                 iounmap(rt2x00dev->csr.base);
217                 rt2x00dev->csr.base = NULL;
218         }
219 }
220
221 static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
222 {
223         struct pci_dev *pci_dev = to_pci_dev(rt2x00dev->dev);
224
225         rt2x00dev->csr.base = pci_ioremap_bar(pci_dev, 0);
226         if (!rt2x00dev->csr.base)
227                 goto exit;
228
229         rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
230         if (!rt2x00dev->eeprom)
231                 goto exit;
232
233         rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
234         if (!rt2x00dev->rf)
235                 goto exit;
236
237         return 0;
238
239 exit:
240         ERROR_PROBE("Failed to allocate registers.\n");
241
242         rt2x00pci_free_reg(rt2x00dev);
243
244         return -ENOMEM;
245 }
246
247 int rt2x00pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
248 {
249         struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_data;
250         struct ieee80211_hw *hw;
251         struct rt2x00_dev *rt2x00dev;
252         int retval;
253
254         retval = pci_request_regions(pci_dev, pci_name(pci_dev));
255         if (retval) {
256                 ERROR_PROBE("PCI request regions failed.\n");
257                 return retval;
258         }
259
260         retval = pci_enable_device(pci_dev);
261         if (retval) {
262                 ERROR_PROBE("Enable device failed.\n");
263                 goto exit_release_regions;
264         }
265
266         pci_set_master(pci_dev);
267
268         if (pci_set_mwi(pci_dev))
269                 ERROR_PROBE("MWI not available.\n");
270
271         if (dma_set_mask(&pci_dev->dev, DMA_32BIT_MASK)) {
272                 ERROR_PROBE("PCI DMA not supported.\n");
273                 retval = -EIO;
274                 goto exit_disable_device;
275         }
276
277         hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
278         if (!hw) {
279                 ERROR_PROBE("Failed to allocate hardware.\n");
280                 retval = -ENOMEM;
281                 goto exit_disable_device;
282         }
283
284         pci_set_drvdata(pci_dev, hw);
285
286         rt2x00dev = hw->priv;
287         rt2x00dev->dev = &pci_dev->dev;
288         rt2x00dev->ops = ops;
289         rt2x00dev->hw = hw;
290
291         retval = rt2x00pci_alloc_reg(rt2x00dev);
292         if (retval)
293                 goto exit_free_device;
294
295         retval = rt2x00lib_probe_dev(rt2x00dev);
296         if (retval)
297                 goto exit_free_reg;
298
299         return 0;
300
301 exit_free_reg:
302         rt2x00pci_free_reg(rt2x00dev);
303
304 exit_free_device:
305         ieee80211_free_hw(hw);
306
307 exit_disable_device:
308         if (retval != -EBUSY)
309                 pci_disable_device(pci_dev);
310
311 exit_release_regions:
312         pci_release_regions(pci_dev);
313
314         pci_set_drvdata(pci_dev, NULL);
315
316         return retval;
317 }
318 EXPORT_SYMBOL_GPL(rt2x00pci_probe);
319
320 void rt2x00pci_remove(struct pci_dev *pci_dev)
321 {
322         struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
323         struct rt2x00_dev *rt2x00dev = hw->priv;
324
325         /*
326          * Free all allocated data.
327          */
328         rt2x00lib_remove_dev(rt2x00dev);
329         rt2x00pci_free_reg(rt2x00dev);
330         ieee80211_free_hw(hw);
331
332         /*
333          * Free the PCI device data.
334          */
335         pci_set_drvdata(pci_dev, NULL);
336         pci_disable_device(pci_dev);
337         pci_release_regions(pci_dev);
338 }
339 EXPORT_SYMBOL_GPL(rt2x00pci_remove);
340
341 #ifdef CONFIG_PM
342 int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
343 {
344         struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
345         struct rt2x00_dev *rt2x00dev = hw->priv;
346         int retval;
347
348         retval = rt2x00lib_suspend(rt2x00dev, state);
349         if (retval)
350                 return retval;
351
352         rt2x00pci_free_reg(rt2x00dev);
353
354         pci_save_state(pci_dev);
355         pci_disable_device(pci_dev);
356         return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
357 }
358 EXPORT_SYMBOL_GPL(rt2x00pci_suspend);
359
360 int rt2x00pci_resume(struct pci_dev *pci_dev)
361 {
362         struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
363         struct rt2x00_dev *rt2x00dev = hw->priv;
364         int retval;
365
366         if (pci_set_power_state(pci_dev, PCI_D0) ||
367             pci_enable_device(pci_dev) ||
368             pci_restore_state(pci_dev)) {
369                 ERROR(rt2x00dev, "Failed to resume device.\n");
370                 return -EIO;
371         }
372
373         retval = rt2x00pci_alloc_reg(rt2x00dev);
374         if (retval)
375                 return retval;
376
377         retval = rt2x00lib_resume(rt2x00dev);
378         if (retval)
379                 goto exit_free_reg;
380
381         return 0;
382
383 exit_free_reg:
384         rt2x00pci_free_reg(rt2x00dev);
385
386         return retval;
387 }
388 EXPORT_SYMBOL_GPL(rt2x00pci_resume);
389 #endif /* CONFIG_PM */
390
391 /*
392  * rt2x00pci module information.
393  */
394 MODULE_AUTHOR(DRV_PROJECT);
395 MODULE_VERSION(DRV_VERSION);
396 MODULE_DESCRIPTION("rt2x00 pci library");
397 MODULE_LICENSE("GPL");