blob: adf2876ed8ab77de00d5c3676b781d4c62edeabf [file] [log] [blame]
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001/*
Ivo van Doorn811aa9c2008-02-03 15:42:53 +01002 Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
Ivo van Doorn95ea3622007-09-25 17:57:13 -07003 <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
Ivo van Doorn95ea3622007-09-25 17:57:13 -070026#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/*
Ivo van Doorn95ea3622007-09-25 17:57:13 -070035 * TX data handlers.
36 */
Ivo van Doorn6db37862008-06-06 22:50:28 +020037int rt2x00pci_write_tx_data(struct queue_entry *entry)
Ivo van Doorn95ea3622007-09-25 17:57:13 -070038{
Ivo van Doornb8be63f2008-05-10 13:46:03 +020039 struct queue_entry_priv_pci *entry_priv = entry->priv_data;
Ivo van Doorn181d6902008-02-05 16:42:23 -050040 struct skb_frame_desc *skbdesc;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070041 u32 word;
42
Ivo van Doornb8be63f2008-05-10 13:46:03 +020043 rt2x00_desc_read(entry_priv->desc, 0, &word);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070044
Ivo van Doorn6db37862008-06-06 22:50:28 +020045 /*
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"
Ivo van Doorn95ea3622007-09-25 17:57:13 -070054 "Please file bug report to %s.\n",
Ivo van Doorne58c6ac2008-04-21 19:00:47 +020055 entry->queue->qid, DRV_PROJECT);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070056 return -EINVAL;
57 }
58
Ivo van Doorn08992f72008-01-24 01:56:25 -080059 /*
60 * Fill in skb descriptor
61 */
Ivo van Doorn6db37862008-06-06 22:50:28 +020062 skbdesc = get_skb_frame_desc(entry->skb);
Ivo van Doornb8be63f2008-05-10 13:46:03 +020063 skbdesc->desc = entry_priv->desc;
Ivo van Doorn6db37862008-06-06 22:50:28 +020064 skbdesc->desc_len = entry->queue->desc_size;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070065
Ivo van Doorn95ea3622007-09-25 17:57:13 -070066 return 0;
67}
68EXPORT_SYMBOL_GPL(rt2x00pci_write_tx_data);
69
70/*
Ivo van Doorn3957ccb2007-11-12 15:02:40 +010071 * TX/RX data handlers.
Ivo van Doorn95ea3622007-09-25 17:57:13 -070072 */
73void rt2x00pci_rxdone(struct rt2x00_dev *rt2x00dev)
74{
Ivo van Doorn181d6902008-02-05 16:42:23 -050075 struct data_queue *queue = rt2x00dev->rx;
76 struct queue_entry *entry;
Ivo van Doornb8be63f2008-05-10 13:46:03 +020077 struct queue_entry_priv_pci *entry_priv;
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +020078 struct skb_frame_desc *skbdesc;
Johannes Berg4150c572007-09-17 01:29:23 -040079 u32 word;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070080
81 while (1) {
Ivo van Doorn181d6902008-02-05 16:42:23 -050082 entry = rt2x00queue_get_entry(queue, Q_INDEX);
Ivo van Doornb8be63f2008-05-10 13:46:03 +020083 entry_priv = entry->priv_data;
84 rt2x00_desc_read(entry_priv->desc, 0, &word);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070085
Johannes Berg4150c572007-09-17 01:29:23 -040086 if (rt2x00_get_field32(word, RXD_ENTRY_OWNER_NIC))
Ivo van Doorn95ea3622007-09-25 17:57:13 -070087 break;
88
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +020089 /*
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;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070095
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +020096 /*
97 * Send the frame to rt2x00lib for further processing.
98 */
99 rt2x00lib_rxdone(rt2x00dev, entry);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700100 }
101}
102EXPORT_SYMBOL_GPL(rt2x00pci_rxdone);
103
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700104/*
105 * Device initialization handlers.
106 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500107static int rt2x00pci_alloc_queue_dma(struct rt2x00_dev *rt2x00dev,
108 struct data_queue *queue)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700109{
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200110 struct queue_entry_priv_pci *entry_priv;
Ivo van Doorn30b3a232008-02-17 17:33:24 +0100111 void *addr;
Ivo van Doorn9c9dd2c2008-02-10 22:46:52 +0100112 dma_addr_t dma;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700113 unsigned int i;
114
115 /*
116 * Allocate DMA memory for descriptor and buffer.
117 */
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200118 addr = dma_alloc_coherent(rt2x00dev->dev,
119 queue->limit * queue->desc_size,
120 &dma, GFP_KERNEL | GFP_DMA);
Ivo van Doorn30b3a232008-02-17 17:33:24 +0100121 if (!addr)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700122 return -ENOMEM;
123
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200124 memset(addr, 0, queue->limit * queue->desc_size);
Ivo van Doorn9c9dd2c2008-02-10 22:46:52 +0100125
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700126 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500127 * Initialize all queue entries to contain valid addresses.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700128 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500129 for (i = 0; i < queue->limit; i++) {
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200130 entry_priv = queue->entries[i].priv_data;
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200131 entry_priv->desc = addr + i * queue->desc_size;
132 entry_priv->desc_dma = dma + i * queue->desc_size;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700133 }
134
135 return 0;
136}
137
Ivo van Doorn181d6902008-02-05 16:42:23 -0500138static void rt2x00pci_free_queue_dma(struct rt2x00_dev *rt2x00dev,
139 struct data_queue *queue)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700140{
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200141 struct queue_entry_priv_pci *entry_priv =
142 queue->entries[0].priv_data;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500143
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200144 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;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700149}
150
151int rt2x00pci_initialize(struct rt2x00_dev *rt2x00dev)
152{
Gertjan van Wingerde14a3bf82008-06-16 19:55:43 +0200153 struct pci_dev *pci_dev = to_pci_dev(rt2x00dev->dev);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500154 struct data_queue *queue;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700155 int status;
156
157 /*
158 * Allocate DMA
159 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500160 queue_for_each(rt2x00dev, queue) {
161 status = rt2x00pci_alloc_queue_dma(rt2x00dev, queue);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700162 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);
Ivo van Doornb30cdfc2008-05-05 17:24:03 +0200174 goto exit;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700175 }
176
177 return 0;
178
179exit:
Ivo van Doornb30cdfc2008-05-05 17:24:03 +0200180 queue_for_each(rt2x00dev, queue)
181 rt2x00pci_free_queue_dma(rt2x00dev, queue);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700182
183 return status;
184}
185EXPORT_SYMBOL_GPL(rt2x00pci_initialize);
186
187void rt2x00pci_uninitialize(struct rt2x00_dev *rt2x00dev)
188{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500189 struct data_queue *queue;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700190
191 /*
192 * Free irq line.
193 */
Gertjan van Wingerde14a3bf82008-06-16 19:55:43 +0200194 free_irq(to_pci_dev(rt2x00dev->dev)->irq, rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700195
196 /*
197 * Free DMA
198 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500199 queue_for_each(rt2x00dev, queue)
200 rt2x00pci_free_queue_dma(rt2x00dev, queue);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700201}
202EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize);
203
204/*
205 * PCI driver handlers.
206 */
207static 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
Ivo van Doorn21795092008-02-10 22:49:13 +0100215 if (rt2x00dev->csr.base) {
216 iounmap(rt2x00dev->csr.base);
217 rt2x00dev->csr.base = NULL;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700218 }
219}
220
221static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
222{
Gertjan van Wingerde14a3bf82008-06-16 19:55:43 +0200223 struct pci_dev *pci_dev = to_pci_dev(rt2x00dev->dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700224
Ivo van Doorn21795092008-02-10 22:49:13 +0100225 rt2x00dev->csr.base = ioremap(pci_resource_start(pci_dev, 0),
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700226 pci_resource_len(pci_dev, 0));
Ivo van Doorn21795092008-02-10 22:49:13 +0100227 if (!rt2x00dev->csr.base)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700228 goto exit;
229
230 rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
231 if (!rt2x00dev->eeprom)
232 goto exit;
233
234 rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
235 if (!rt2x00dev->rf)
236 goto exit;
237
238 return 0;
239
240exit:
241 ERROR_PROBE("Failed to allocate registers.\n");
242
243 rt2x00pci_free_reg(rt2x00dev);
244
245 return -ENOMEM;
246}
247
248int rt2x00pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
249{
250 struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_data;
251 struct ieee80211_hw *hw;
252 struct rt2x00_dev *rt2x00dev;
253 int retval;
254
255 retval = pci_request_regions(pci_dev, pci_name(pci_dev));
256 if (retval) {
257 ERROR_PROBE("PCI request regions failed.\n");
258 return retval;
259 }
260
261 retval = pci_enable_device(pci_dev);
262 if (retval) {
263 ERROR_PROBE("Enable device failed.\n");
264 goto exit_release_regions;
265 }
266
267 pci_set_master(pci_dev);
268
269 if (pci_set_mwi(pci_dev))
270 ERROR_PROBE("MWI not available.\n");
271
Gertjan van Wingerde14a3bf82008-06-16 19:55:43 +0200272 if (dma_set_mask(&pci_dev->dev, DMA_32BIT_MASK)) {
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700273 ERROR_PROBE("PCI DMA not supported.\n");
274 retval = -EIO;
275 goto exit_disable_device;
276 }
277
278 hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
279 if (!hw) {
280 ERROR_PROBE("Failed to allocate hardware.\n");
281 retval = -ENOMEM;
282 goto exit_disable_device;
283 }
284
285 pci_set_drvdata(pci_dev, hw);
286
287 rt2x00dev = hw->priv;
Gertjan van Wingerde14a3bf82008-06-16 19:55:43 +0200288 rt2x00dev->dev = &pci_dev->dev;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700289 rt2x00dev->ops = ops;
290 rt2x00dev->hw = hw;
291
292 retval = rt2x00pci_alloc_reg(rt2x00dev);
293 if (retval)
294 goto exit_free_device;
295
296 retval = rt2x00lib_probe_dev(rt2x00dev);
297 if (retval)
298 goto exit_free_reg;
299
300 return 0;
301
302exit_free_reg:
303 rt2x00pci_free_reg(rt2x00dev);
304
305exit_free_device:
306 ieee80211_free_hw(hw);
307
308exit_disable_device:
309 if (retval != -EBUSY)
310 pci_disable_device(pci_dev);
311
312exit_release_regions:
313 pci_release_regions(pci_dev);
314
315 pci_set_drvdata(pci_dev, NULL);
316
317 return retval;
318}
319EXPORT_SYMBOL_GPL(rt2x00pci_probe);
320
321void rt2x00pci_remove(struct pci_dev *pci_dev)
322{
323 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
324 struct rt2x00_dev *rt2x00dev = hw->priv;
325
326 /*
327 * Free all allocated data.
328 */
329 rt2x00lib_remove_dev(rt2x00dev);
330 rt2x00pci_free_reg(rt2x00dev);
331 ieee80211_free_hw(hw);
332
333 /*
334 * Free the PCI device data.
335 */
336 pci_set_drvdata(pci_dev, NULL);
337 pci_disable_device(pci_dev);
338 pci_release_regions(pci_dev);
339}
340EXPORT_SYMBOL_GPL(rt2x00pci_remove);
341
342#ifdef CONFIG_PM
343int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
344{
345 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
346 struct rt2x00_dev *rt2x00dev = hw->priv;
347 int retval;
348
349 retval = rt2x00lib_suspend(rt2x00dev, state);
350 if (retval)
351 return retval;
352
353 rt2x00pci_free_reg(rt2x00dev);
354
355 pci_save_state(pci_dev);
356 pci_disable_device(pci_dev);
357 return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
358}
359EXPORT_SYMBOL_GPL(rt2x00pci_suspend);
360
361int rt2x00pci_resume(struct pci_dev *pci_dev)
362{
363 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
364 struct rt2x00_dev *rt2x00dev = hw->priv;
365 int retval;
366
367 if (pci_set_power_state(pci_dev, PCI_D0) ||
368 pci_enable_device(pci_dev) ||
369 pci_restore_state(pci_dev)) {
370 ERROR(rt2x00dev, "Failed to resume device.\n");
371 return -EIO;
372 }
373
374 retval = rt2x00pci_alloc_reg(rt2x00dev);
375 if (retval)
376 return retval;
377
378 retval = rt2x00lib_resume(rt2x00dev);
379 if (retval)
380 goto exit_free_reg;
381
382 return 0;
383
384exit_free_reg:
385 rt2x00pci_free_reg(rt2x00dev);
386
387 return retval;
388}
389EXPORT_SYMBOL_GPL(rt2x00pci_resume);
390#endif /* CONFIG_PM */
391
392/*
393 * rt2x00pci module information.
394 */
395MODULE_AUTHOR(DRV_PROJECT);
396MODULE_VERSION(DRV_VERSION);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500397MODULE_DESCRIPTION("rt2x00 pci library");
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700398MODULE_LICENSE("GPL");