e7e3a459b66a6b8196f0626fcb03ae332019e999
[pandora-kernel.git] / 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         memset(skbdesc, 0, sizeof(*skbdesc));
64         skbdesc->desc = entry_priv->desc;
65         skbdesc->desc_len = entry->queue->desc_size;
66         skbdesc->entry = entry;
67
68         memcpy(entry_priv->data, entry->skb->data, entry->skb->len);
69
70         return 0;
71 }
72 EXPORT_SYMBOL_GPL(rt2x00pci_write_tx_data);
73
74 /*
75  * TX/RX data handlers.
76  */
77 static void rt2x00pci_rxdone_entry(struct rt2x00_dev *rt2x00dev,
78                                    struct queue_entry *entry)
79 {
80         struct sk_buff *skb;
81         struct skb_frame_desc *skbdesc;
82         struct rxdone_entry_desc rxdesc;
83         struct queue_entry_priv_pci *entry_priv = entry->priv_data;
84
85         /*
86          * Allocate a new sk_buffer. If no new buffer available, drop the
87          * received frame and reuse the existing buffer.
88          */
89         skb = rt2x00queue_alloc_skb(entry->queue);
90         if (!skb)
91                 return;
92
93         /*
94          * Extract the RXD details.
95          */
96         memset(&rxdesc, 0, sizeof(rxdesc));
97         rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
98
99         /*
100          * Copy the received data to the entries' skb.
101          */
102         memcpy(entry->skb->data, entry_priv->data, rxdesc.size);
103         skb_trim(entry->skb, rxdesc.size);
104
105         /*
106          * Fill in skb descriptor
107          */
108         skbdesc = get_skb_frame_desc(entry->skb);
109         memset(skbdesc, 0, sizeof(*skbdesc));
110         skbdesc->desc = entry_priv->desc;
111         skbdesc->desc_len = entry->queue->desc_size;
112         skbdesc->entry = entry;
113
114         /*
115          * Send the frame to rt2x00lib for further processing.
116          */
117         rt2x00lib_rxdone(entry, &rxdesc);
118
119         /*
120          * Replace the entries' skb with the newly allocated one.
121          */
122         entry->skb = skb;
123 }
124
125 void rt2x00pci_rxdone(struct rt2x00_dev *rt2x00dev)
126 {
127         struct data_queue *queue = rt2x00dev->rx;
128         struct queue_entry *entry;
129         struct queue_entry_priv_pci *entry_priv;
130         u32 word;
131
132         while (1) {
133                 entry = rt2x00queue_get_entry(queue, Q_INDEX);
134                 entry_priv = entry->priv_data;
135                 rt2x00_desc_read(entry_priv->desc, 0, &word);
136
137                 if (rt2x00_get_field32(word, RXD_ENTRY_OWNER_NIC))
138                         break;
139
140                 rt2x00pci_rxdone_entry(rt2x00dev, entry);
141
142                 if (test_bit(DEVICE_ENABLED_RADIO, &queue->rt2x00dev->flags)) {
143                         rt2x00_set_field32(&word, RXD_ENTRY_OWNER_NIC, 1);
144                         rt2x00_desc_write(entry_priv->desc, 0, word);
145                 }
146
147                 rt2x00queue_index_inc(queue, Q_INDEX);
148         }
149 }
150 EXPORT_SYMBOL_GPL(rt2x00pci_rxdone);
151
152 void rt2x00pci_txdone(struct rt2x00_dev *rt2x00dev, struct queue_entry *entry,
153                       struct txdone_entry_desc *txdesc)
154 {
155         struct queue_entry_priv_pci *entry_priv = entry->priv_data;
156         enum data_queue_qid qid = skb_get_queue_mapping(entry->skb);
157         u32 word;
158
159         rt2x00lib_txdone(entry, txdesc);
160
161         /*
162          * Make this entry available for reuse.
163          */
164         entry->flags = 0;
165
166         rt2x00_desc_read(entry_priv->desc, 0, &word);
167         rt2x00_set_field32(&word, TXD_ENTRY_OWNER_NIC, 0);
168         rt2x00_set_field32(&word, TXD_ENTRY_VALID, 0);
169         rt2x00_desc_write(entry_priv->desc, 0, word);
170
171         __clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
172         rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
173
174         /*
175          * If the data queue was below the threshold before the txdone
176          * handler we must make sure the packet queue in the mac80211 stack
177          * is reenabled when the txdone handler has finished.
178          */
179         if (!rt2x00queue_threshold(entry->queue))
180                 ieee80211_wake_queue(rt2x00dev->hw, qid);
181
182 }
183 EXPORT_SYMBOL_GPL(rt2x00pci_txdone);
184
185 /*
186  * Device initialization handlers.
187  */
188 #define desc_size(__queue)                      \
189 ({                                              \
190          ((__queue)->limit * (__queue)->desc_size);\
191 })
192
193 #define data_size(__queue)                      \
194 ({                                              \
195          ((__queue)->limit * (__queue)->data_size);\
196 })
197
198 #define dma_size(__queue)                       \
199 ({                                              \
200         data_size(__queue) + desc_size(__queue);\
201 })
202
203 #define desc_offset(__queue, __base, __i)       \
204 ({                                              \
205         (__base) + data_size(__queue) +         \
206             ((__i) * (__queue)->desc_size);     \
207 })
208
209 #define data_offset(__queue, __base, __i)       \
210 ({                                              \
211         (__base) +                              \
212             ((__i) * (__queue)->data_size);     \
213 })
214
215 static int rt2x00pci_alloc_queue_dma(struct rt2x00_dev *rt2x00dev,
216                                      struct data_queue *queue)
217 {
218         struct queue_entry_priv_pci *entry_priv;
219         void *addr;
220         dma_addr_t dma;
221         unsigned int i;
222
223         /*
224          * Allocate DMA memory for descriptor and buffer.
225          */
226         addr = dma_alloc_coherent(rt2x00dev->dev, dma_size(queue), &dma,
227                                   GFP_KERNEL | GFP_DMA);
228         if (!addr)
229                 return -ENOMEM;
230
231         memset(addr, 0, dma_size(queue));
232
233         /*
234          * Initialize all queue entries to contain valid addresses.
235          */
236         for (i = 0; i < queue->limit; i++) {
237                 entry_priv = queue->entries[i].priv_data;
238                 entry_priv->desc = desc_offset(queue, addr, i);
239                 entry_priv->desc_dma = desc_offset(queue, dma, i);
240                 entry_priv->data = data_offset(queue, addr, i);
241                 entry_priv->data_dma = data_offset(queue, dma, i);
242         }
243
244         return 0;
245 }
246
247 static void rt2x00pci_free_queue_dma(struct rt2x00_dev *rt2x00dev,
248                                      struct data_queue *queue)
249 {
250         struct queue_entry_priv_pci *entry_priv =
251             queue->entries[0].priv_data;
252
253         if (entry_priv->data)
254                 dma_free_coherent(rt2x00dev->dev, dma_size(queue),
255                                   entry_priv->data, entry_priv->data_dma);
256         entry_priv->data = NULL;
257 }
258
259 int rt2x00pci_initialize(struct rt2x00_dev *rt2x00dev)
260 {
261         struct pci_dev *pci_dev = to_pci_dev(rt2x00dev->dev);
262         struct data_queue *queue;
263         int status;
264
265         /*
266          * Allocate DMA
267          */
268         queue_for_each(rt2x00dev, queue) {
269                 status = rt2x00pci_alloc_queue_dma(rt2x00dev, queue);
270                 if (status)
271                         goto exit;
272         }
273
274         /*
275          * Register interrupt handler.
276          */
277         status = request_irq(pci_dev->irq, rt2x00dev->ops->lib->irq_handler,
278                              IRQF_SHARED, pci_name(pci_dev), rt2x00dev);
279         if (status) {
280                 ERROR(rt2x00dev, "IRQ %d allocation failed (error %d).\n",
281                       pci_dev->irq, status);
282                 goto exit;
283         }
284
285         return 0;
286
287 exit:
288         queue_for_each(rt2x00dev, queue)
289                 rt2x00pci_free_queue_dma(rt2x00dev, queue);
290
291         return status;
292 }
293 EXPORT_SYMBOL_GPL(rt2x00pci_initialize);
294
295 void rt2x00pci_uninitialize(struct rt2x00_dev *rt2x00dev)
296 {
297         struct data_queue *queue;
298
299         /*
300          * Free irq line.
301          */
302         free_irq(to_pci_dev(rt2x00dev->dev)->irq, rt2x00dev);
303
304         /*
305          * Free DMA
306          */
307         queue_for_each(rt2x00dev, queue)
308                 rt2x00pci_free_queue_dma(rt2x00dev, queue);
309 }
310 EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize);
311
312 /*
313  * PCI driver handlers.
314  */
315 static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
316 {
317         kfree(rt2x00dev->rf);
318         rt2x00dev->rf = NULL;
319
320         kfree(rt2x00dev->eeprom);
321         rt2x00dev->eeprom = NULL;
322
323         if (rt2x00dev->csr.base) {
324                 iounmap(rt2x00dev->csr.base);
325                 rt2x00dev->csr.base = NULL;
326         }
327 }
328
329 static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
330 {
331         struct pci_dev *pci_dev = to_pci_dev(rt2x00dev->dev);
332
333         rt2x00dev->csr.base = ioremap(pci_resource_start(pci_dev, 0),
334                                       pci_resource_len(pci_dev, 0));
335         if (!rt2x00dev->csr.base)
336                 goto exit;
337
338         rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
339         if (!rt2x00dev->eeprom)
340                 goto exit;
341
342         rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
343         if (!rt2x00dev->rf)
344                 goto exit;
345
346         return 0;
347
348 exit:
349         ERROR_PROBE("Failed to allocate registers.\n");
350
351         rt2x00pci_free_reg(rt2x00dev);
352
353         return -ENOMEM;
354 }
355
356 int rt2x00pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
357 {
358         struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_data;
359         struct ieee80211_hw *hw;
360         struct rt2x00_dev *rt2x00dev;
361         int retval;
362
363         retval = pci_request_regions(pci_dev, pci_name(pci_dev));
364         if (retval) {
365                 ERROR_PROBE("PCI request regions failed.\n");
366                 return retval;
367         }
368
369         retval = pci_enable_device(pci_dev);
370         if (retval) {
371                 ERROR_PROBE("Enable device failed.\n");
372                 goto exit_release_regions;
373         }
374
375         pci_set_master(pci_dev);
376
377         if (pci_set_mwi(pci_dev))
378                 ERROR_PROBE("MWI not available.\n");
379
380         if (dma_set_mask(&pci_dev->dev, DMA_32BIT_MASK)) {
381                 ERROR_PROBE("PCI DMA not supported.\n");
382                 retval = -EIO;
383                 goto exit_disable_device;
384         }
385
386         hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
387         if (!hw) {
388                 ERROR_PROBE("Failed to allocate hardware.\n");
389                 retval = -ENOMEM;
390                 goto exit_disable_device;
391         }
392
393         pci_set_drvdata(pci_dev, hw);
394
395         rt2x00dev = hw->priv;
396         rt2x00dev->dev = &pci_dev->dev;
397         rt2x00dev->ops = ops;
398         rt2x00dev->hw = hw;
399
400         retval = rt2x00pci_alloc_reg(rt2x00dev);
401         if (retval)
402                 goto exit_free_device;
403
404         retval = rt2x00lib_probe_dev(rt2x00dev);
405         if (retval)
406                 goto exit_free_reg;
407
408         return 0;
409
410 exit_free_reg:
411         rt2x00pci_free_reg(rt2x00dev);
412
413 exit_free_device:
414         ieee80211_free_hw(hw);
415
416 exit_disable_device:
417         if (retval != -EBUSY)
418                 pci_disable_device(pci_dev);
419
420 exit_release_regions:
421         pci_release_regions(pci_dev);
422
423         pci_set_drvdata(pci_dev, NULL);
424
425         return retval;
426 }
427 EXPORT_SYMBOL_GPL(rt2x00pci_probe);
428
429 void rt2x00pci_remove(struct pci_dev *pci_dev)
430 {
431         struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
432         struct rt2x00_dev *rt2x00dev = hw->priv;
433
434         /*
435          * Free all allocated data.
436          */
437         rt2x00lib_remove_dev(rt2x00dev);
438         rt2x00pci_free_reg(rt2x00dev);
439         ieee80211_free_hw(hw);
440
441         /*
442          * Free the PCI device data.
443          */
444         pci_set_drvdata(pci_dev, NULL);
445         pci_disable_device(pci_dev);
446         pci_release_regions(pci_dev);
447 }
448 EXPORT_SYMBOL_GPL(rt2x00pci_remove);
449
450 #ifdef CONFIG_PM
451 int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
452 {
453         struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
454         struct rt2x00_dev *rt2x00dev = hw->priv;
455         int retval;
456
457         retval = rt2x00lib_suspend(rt2x00dev, state);
458         if (retval)
459                 return retval;
460
461         rt2x00pci_free_reg(rt2x00dev);
462
463         pci_save_state(pci_dev);
464         pci_disable_device(pci_dev);
465         return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
466 }
467 EXPORT_SYMBOL_GPL(rt2x00pci_suspend);
468
469 int rt2x00pci_resume(struct pci_dev *pci_dev)
470 {
471         struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
472         struct rt2x00_dev *rt2x00dev = hw->priv;
473         int retval;
474
475         if (pci_set_power_state(pci_dev, PCI_D0) ||
476             pci_enable_device(pci_dev) ||
477             pci_restore_state(pci_dev)) {
478                 ERROR(rt2x00dev, "Failed to resume device.\n");
479                 return -EIO;
480         }
481
482         retval = rt2x00pci_alloc_reg(rt2x00dev);
483         if (retval)
484                 return retval;
485
486         retval = rt2x00lib_resume(rt2x00dev);
487         if (retval)
488                 goto exit_free_reg;
489
490         return 0;
491
492 exit_free_reg:
493         rt2x00pci_free_reg(rt2x00dev);
494
495         return retval;
496 }
497 EXPORT_SYMBOL_GPL(rt2x00pci_resume);
498 #endif /* CONFIG_PM */
499
500 /*
501  * rt2x00pci module information.
502  */
503 MODULE_AUTHOR(DRV_PROJECT);
504 MODULE_VERSION(DRV_VERSION);
505 MODULE_DESCRIPTION("rt2x00 pci library");
506 MODULE_LICENSE("GPL");