]> err.no Git - linux-2.6/blob - drivers/net/wireless/rt2x00/rt2x00pci.c
rt2x00: Move generic TX frame writing code into rt2x00queue
[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         memset(skbdesc, 0, sizeof(*skbdesc));
64         skbdesc->data = entry->skb->data;
65         skbdesc->data_len = entry->skb->len;
66         skbdesc->desc = entry_priv->desc;
67         skbdesc->desc_len = entry->queue->desc_size;
68         skbdesc->entry = entry;
69
70         memcpy(entry_priv->data, entry->skb->data, entry->skb->len);
71
72         return 0;
73 }
74 EXPORT_SYMBOL_GPL(rt2x00pci_write_tx_data);
75
76 /*
77  * TX/RX data handlers.
78  */
79 void rt2x00pci_rxdone(struct rt2x00_dev *rt2x00dev)
80 {
81         struct data_queue *queue = rt2x00dev->rx;
82         struct queue_entry *entry;
83         struct queue_entry_priv_pci *entry_priv;
84         struct ieee80211_hdr *hdr;
85         struct skb_frame_desc *skbdesc;
86         struct rxdone_entry_desc rxdesc;
87         int header_size;
88         int align;
89         u32 word;
90
91         while (1) {
92                 entry = rt2x00queue_get_entry(queue, Q_INDEX);
93                 entry_priv = entry->priv_data;
94                 rt2x00_desc_read(entry_priv->desc, 0, &word);
95
96                 if (rt2x00_get_field32(word, RXD_ENTRY_OWNER_NIC))
97                         break;
98
99                 memset(&rxdesc, 0, sizeof(rxdesc));
100                 rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
101
102                 hdr = (struct ieee80211_hdr *)entry_priv->data;
103                 header_size =
104                     ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
105
106                 /*
107                  * The data behind the ieee80211 header must be
108                  * aligned on a 4 byte boundary.
109                  */
110                 align = header_size % 4;
111
112                 /*
113                  * Allocate the sk_buffer, initialize it and copy
114                  * all data into it.
115                  */
116                 entry->skb = dev_alloc_skb(rxdesc.size + align);
117                 if (!entry->skb)
118                         return;
119
120                 skb_reserve(entry->skb, align);
121                 memcpy(skb_put(entry->skb, rxdesc.size),
122                        entry_priv->data, rxdesc.size);
123
124                 /*
125                  * Fill in skb descriptor
126                  */
127                 skbdesc = get_skb_frame_desc(entry->skb);
128                 memset(skbdesc, 0, sizeof(*skbdesc));
129                 skbdesc->data = entry->skb->data;
130                 skbdesc->data_len = entry->skb->len;
131                 skbdesc->desc = entry_priv->desc;
132                 skbdesc->desc_len = queue->desc_size;
133                 skbdesc->entry = entry;
134
135                 /*
136                  * Send the frame to rt2x00lib for further processing.
137                  */
138                 rt2x00lib_rxdone(entry, &rxdesc);
139
140                 if (test_bit(DEVICE_ENABLED_RADIO, &queue->rt2x00dev->flags)) {
141                         rt2x00_set_field32(&word, RXD_ENTRY_OWNER_NIC, 1);
142                         rt2x00_desc_write(entry_priv->desc, 0, word);
143                 }
144
145                 rt2x00queue_index_inc(queue, Q_INDEX);
146         }
147 }
148 EXPORT_SYMBOL_GPL(rt2x00pci_rxdone);
149
150 void rt2x00pci_txdone(struct rt2x00_dev *rt2x00dev, struct queue_entry *entry,
151                       struct txdone_entry_desc *txdesc)
152 {
153         struct queue_entry_priv_pci *entry_priv = entry->priv_data;
154         enum data_queue_qid qid = skb_get_queue_mapping(entry->skb);
155         u32 word;
156
157         rt2x00lib_txdone(entry, txdesc);
158
159         /*
160          * Make this entry available for reuse.
161          */
162         entry->flags = 0;
163
164         rt2x00_desc_read(entry_priv->desc, 0, &word);
165         rt2x00_set_field32(&word, TXD_ENTRY_OWNER_NIC, 0);
166         rt2x00_set_field32(&word, TXD_ENTRY_VALID, 0);
167         rt2x00_desc_write(entry_priv->desc, 0, word);
168
169         __clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
170         rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
171
172         /*
173          * If the data queue was full before the txdone handler
174          * we must make sure the packet queue in the mac80211 stack
175          * is reenabled when the txdone handler has finished.
176          */
177         if (!rt2x00queue_full(entry->queue))
178                 ieee80211_wake_queue(rt2x00dev->hw, qid);
179
180 }
181 EXPORT_SYMBOL_GPL(rt2x00pci_txdone);
182
183 /*
184  * Device initialization handlers.
185  */
186 #define desc_size(__queue)                      \
187 ({                                              \
188          ((__queue)->limit * (__queue)->desc_size);\
189 })
190
191 #define data_size(__queue)                      \
192 ({                                              \
193          ((__queue)->limit * (__queue)->data_size);\
194 })
195
196 #define dma_size(__queue)                       \
197 ({                                              \
198         data_size(__queue) + desc_size(__queue);\
199 })
200
201 #define desc_offset(__queue, __base, __i)       \
202 ({                                              \
203         (__base) + data_size(__queue) +         \
204             ((__i) * (__queue)->desc_size);     \
205 })
206
207 #define data_offset(__queue, __base, __i)       \
208 ({                                              \
209         (__base) +                              \
210             ((__i) * (__queue)->data_size);     \
211 })
212
213 static int rt2x00pci_alloc_queue_dma(struct rt2x00_dev *rt2x00dev,
214                                      struct data_queue *queue)
215 {
216         struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
217         struct queue_entry_priv_pci *entry_priv;
218         void *addr;
219         dma_addr_t dma;
220         unsigned int i;
221
222         /*
223          * Allocate DMA memory for descriptor and buffer.
224          */
225         addr = pci_alloc_consistent(pci_dev, dma_size(queue), &dma);
226         if (!addr)
227                 return -ENOMEM;
228
229         memset(addr, 0, dma_size(queue));
230
231         /*
232          * Initialize all queue entries to contain valid addresses.
233          */
234         for (i = 0; i < queue->limit; i++) {
235                 entry_priv = queue->entries[i].priv_data;
236                 entry_priv->desc = desc_offset(queue, addr, i);
237                 entry_priv->desc_dma = desc_offset(queue, dma, i);
238                 entry_priv->data = data_offset(queue, addr, i);
239                 entry_priv->data_dma = data_offset(queue, dma, i);
240         }
241
242         return 0;
243 }
244
245 static void rt2x00pci_free_queue_dma(struct rt2x00_dev *rt2x00dev,
246                                      struct data_queue *queue)
247 {
248         struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
249         struct queue_entry_priv_pci *entry_priv =
250             queue->entries[0].priv_data;
251
252         if (entry_priv->data)
253                 pci_free_consistent(pci_dev, dma_size(queue),
254                                     entry_priv->data, entry_priv->data_dma);
255         entry_priv->data = NULL;
256 }
257
258 int rt2x00pci_initialize(struct rt2x00_dev *rt2x00dev)
259 {
260         struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
261         struct data_queue *queue;
262         int status;
263
264         /*
265          * Allocate DMA
266          */
267         queue_for_each(rt2x00dev, queue) {
268                 status = rt2x00pci_alloc_queue_dma(rt2x00dev, queue);
269                 if (status)
270                         goto exit;
271         }
272
273         /*
274          * Register interrupt handler.
275          */
276         status = request_irq(pci_dev->irq, rt2x00dev->ops->lib->irq_handler,
277                              IRQF_SHARED, pci_name(pci_dev), rt2x00dev);
278         if (status) {
279                 ERROR(rt2x00dev, "IRQ %d allocation failed (error %d).\n",
280                       pci_dev->irq, status);
281                 goto exit;
282         }
283
284         return 0;
285
286 exit:
287         queue_for_each(rt2x00dev, queue)
288                 rt2x00pci_free_queue_dma(rt2x00dev, queue);
289
290         return status;
291 }
292 EXPORT_SYMBOL_GPL(rt2x00pci_initialize);
293
294 void rt2x00pci_uninitialize(struct rt2x00_dev *rt2x00dev)
295 {
296         struct data_queue *queue;
297
298         /*
299          * Free irq line.
300          */
301         free_irq(rt2x00dev_pci(rt2x00dev)->irq, rt2x00dev);
302
303         /*
304          * Free DMA
305          */
306         queue_for_each(rt2x00dev, queue)
307                 rt2x00pci_free_queue_dma(rt2x00dev, queue);
308 }
309 EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize);
310
311 /*
312  * PCI driver handlers.
313  */
314 static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
315 {
316         kfree(rt2x00dev->rf);
317         rt2x00dev->rf = NULL;
318
319         kfree(rt2x00dev->eeprom);
320         rt2x00dev->eeprom = NULL;
321
322         if (rt2x00dev->csr.base) {
323                 iounmap(rt2x00dev->csr.base);
324                 rt2x00dev->csr.base = NULL;
325         }
326 }
327
328 static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
329 {
330         struct pci_dev *pci_dev = rt2x00dev_pci(rt2x00dev);
331
332         rt2x00dev->csr.base = ioremap(pci_resource_start(pci_dev, 0),
333                                       pci_resource_len(pci_dev, 0));
334         if (!rt2x00dev->csr.base)
335                 goto exit;
336
337         rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
338         if (!rt2x00dev->eeprom)
339                 goto exit;
340
341         rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
342         if (!rt2x00dev->rf)
343                 goto exit;
344
345         return 0;
346
347 exit:
348         ERROR_PROBE("Failed to allocate registers.\n");
349
350         rt2x00pci_free_reg(rt2x00dev);
351
352         return -ENOMEM;
353 }
354
355 int rt2x00pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
356 {
357         struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_data;
358         struct ieee80211_hw *hw;
359         struct rt2x00_dev *rt2x00dev;
360         int retval;
361
362         retval = pci_request_regions(pci_dev, pci_name(pci_dev));
363         if (retval) {
364                 ERROR_PROBE("PCI request regions failed.\n");
365                 return retval;
366         }
367
368         retval = pci_enable_device(pci_dev);
369         if (retval) {
370                 ERROR_PROBE("Enable device failed.\n");
371                 goto exit_release_regions;
372         }
373
374         pci_set_master(pci_dev);
375
376         if (pci_set_mwi(pci_dev))
377                 ERROR_PROBE("MWI not available.\n");
378
379         if (pci_set_dma_mask(pci_dev, DMA_64BIT_MASK) &&
380             pci_set_dma_mask(pci_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;
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");