blob: 494b960e811ce8837c4a10058c4061177ced0b11 [file] [log] [blame]
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001/*
Gertjan van Wingerde9c9a0d12009-11-08 16:39:55 +01002 Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Ivo van Doorn95ea3622007-09-25 17:57:13 -070031
32#include "rt2x00.h"
33#include "rt2x00pci.h"
34
35/*
Ivo van Doornc9c3b1a2008-11-10 19:41:40 +010036 * 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
Alban Browaeysc70762f2009-12-04 23:46:57 +010045 if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
46 return 0;
47
Ivo van Doornc9c3b1a2008-11-10 19:41:40 +010048 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
63/*
Ivo van Doorn95ea3622007-09-25 17:57:13 -070064 * TX data handlers.
65 */
Helmut Schaa41086692010-04-15 09:13:13 +020066int rt2x00pci_write_tx_data(struct queue_entry *entry,
67 struct txentry_desc *txdesc)
Ivo van Doorn95ea3622007-09-25 17:57:13 -070068{
Ivo van Doorn798b7ad2008-11-08 15:25:33 +010069 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070070
Ivo van Doorn6db37862008-06-06 22:50:28 +020071 /*
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 */
Ivo van Doorn798b7ad2008-11-08 15:25:33 +010076 if (unlikely(rt2x00dev->ops->lib->get_entry_state(entry))) {
77 ERROR(rt2x00dev,
Ivo van Doorn6db37862008-06-06 22:50:28 +020078 "Corrupt queue %d, accessing entry which is not ours.\n"
Ivo van Doorn95ea3622007-09-25 17:57:13 -070079 "Please file bug report to %s.\n",
Ivo van Doorne58c6ac2008-04-21 19:00:47 +020080 entry->queue->qid, DRV_PROJECT);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070081 return -EINVAL;
82 }
83
Gertjan van Wingerdebaaffe62010-06-03 10:51:43 +020084 /*
85 * Call the driver's write_tx_datadesc function, if it exists.
86 */
87 if (rt2x00dev->ops->lib->write_tx_datadesc)
88 rt2x00dev->ops->lib->write_tx_datadesc(entry, txdesc);
89
Ivo van Doorn95ea3622007-09-25 17:57:13 -070090 return 0;
91}
92EXPORT_SYMBOL_GPL(rt2x00pci_write_tx_data);
93
94/*
Ivo van Doorn3957ccb2007-11-12 15:02:40 +010095 * TX/RX data handlers.
Ivo van Doorn95ea3622007-09-25 17:57:13 -070096 */
97void rt2x00pci_rxdone(struct rt2x00_dev *rt2x00dev)
98{
Ivo van Doorn181d6902008-02-05 16:42:23 -050099 struct data_queue *queue = rt2x00dev->rx;
100 struct queue_entry *entry;
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200101 struct queue_entry_priv_pci *entry_priv;
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200102 struct skb_frame_desc *skbdesc;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700103
104 while (1) {
Ivo van Doorn181d6902008-02-05 16:42:23 -0500105 entry = rt2x00queue_get_entry(queue, Q_INDEX);
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200106 entry_priv = entry->priv_data;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700107
Ivo van Doorn798b7ad2008-11-08 15:25:33 +0100108 if (rt2x00dev->ops->lib->get_entry_state(entry))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700109 break;
110
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200111 /*
112 * Fill in desc fields of the skb descriptor
113 */
114 skbdesc = get_skb_frame_desc(entry->skb);
115 skbdesc->desc = entry_priv->desc;
116 skbdesc->desc_len = entry->queue->desc_size;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700117
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200118 /*
119 * Send the frame to rt2x00lib for further processing.
120 */
121 rt2x00lib_rxdone(rt2x00dev, entry);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700122 }
123}
124EXPORT_SYMBOL_GPL(rt2x00pci_rxdone);
125
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700126/*
127 * Device initialization handlers.
128 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500129static int rt2x00pci_alloc_queue_dma(struct rt2x00_dev *rt2x00dev,
130 struct data_queue *queue)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700131{
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200132 struct queue_entry_priv_pci *entry_priv;
Ivo van Doorn30b3a232008-02-17 17:33:24 +0100133 void *addr;
Ivo van Doorn9c9dd2c2008-02-10 22:46:52 +0100134 dma_addr_t dma;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700135 unsigned int i;
136
137 /*
138 * Allocate DMA memory for descriptor and buffer.
139 */
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200140 addr = dma_alloc_coherent(rt2x00dev->dev,
141 queue->limit * queue->desc_size,
142 &dma, GFP_KERNEL | GFP_DMA);
Ivo van Doorn30b3a232008-02-17 17:33:24 +0100143 if (!addr)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700144 return -ENOMEM;
145
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200146 memset(addr, 0, queue->limit * queue->desc_size);
Ivo van Doorn9c9dd2c2008-02-10 22:46:52 +0100147
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700148 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500149 * Initialize all queue entries to contain valid addresses.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700150 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500151 for (i = 0; i < queue->limit; i++) {
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200152 entry_priv = queue->entries[i].priv_data;
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200153 entry_priv->desc = addr + i * queue->desc_size;
154 entry_priv->desc_dma = dma + i * queue->desc_size;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700155 }
156
157 return 0;
158}
159
Ivo van Doorn181d6902008-02-05 16:42:23 -0500160static void rt2x00pci_free_queue_dma(struct rt2x00_dev *rt2x00dev,
161 struct data_queue *queue)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700162{
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200163 struct queue_entry_priv_pci *entry_priv =
164 queue->entries[0].priv_data;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500165
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200166 if (entry_priv->desc)
167 dma_free_coherent(rt2x00dev->dev,
168 queue->limit * queue->desc_size,
169 entry_priv->desc, entry_priv->desc_dma);
170 entry_priv->desc = NULL;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700171}
172
173int rt2x00pci_initialize(struct rt2x00_dev *rt2x00dev)
174{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500175 struct data_queue *queue;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700176 int status;
177
178 /*
179 * Allocate DMA
180 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500181 queue_for_each(rt2x00dev, queue) {
182 status = rt2x00pci_alloc_queue_dma(rt2x00dev, queue);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700183 if (status)
184 goto exit;
185 }
186
187 /*
188 * Register interrupt handler.
189 */
Ivo van Doorn440ddad2009-03-28 20:51:24 +0100190 status = request_irq(rt2x00dev->irq, rt2x00dev->ops->lib->irq_handler,
191 IRQF_SHARED, rt2x00dev->name, rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700192 if (status) {
193 ERROR(rt2x00dev, "IRQ %d allocation failed (error %d).\n",
Ivo van Doorn440ddad2009-03-28 20:51:24 +0100194 rt2x00dev->irq, status);
Ivo van Doornb30cdfc2008-05-05 17:24:03 +0200195 goto exit;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700196 }
197
198 return 0;
199
200exit:
Ivo van Doornb30cdfc2008-05-05 17:24:03 +0200201 queue_for_each(rt2x00dev, queue)
202 rt2x00pci_free_queue_dma(rt2x00dev, queue);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700203
204 return status;
205}
206EXPORT_SYMBOL_GPL(rt2x00pci_initialize);
207
208void rt2x00pci_uninitialize(struct rt2x00_dev *rt2x00dev)
209{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500210 struct data_queue *queue;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700211
212 /*
213 * Free irq line.
214 */
Helmut Schaa52a9bd22010-05-19 08:47:59 +0200215 free_irq(rt2x00dev->irq, rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700216
217 /*
218 * Free DMA
219 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500220 queue_for_each(rt2x00dev, queue)
221 rt2x00pci_free_queue_dma(rt2x00dev, queue);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700222}
223EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize);
224
225/*
226 * PCI driver handlers.
227 */
228static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
229{
230 kfree(rt2x00dev->rf);
231 rt2x00dev->rf = NULL;
232
233 kfree(rt2x00dev->eeprom);
234 rt2x00dev->eeprom = NULL;
235
Ivo van Doorn21795092008-02-10 22:49:13 +0100236 if (rt2x00dev->csr.base) {
237 iounmap(rt2x00dev->csr.base);
238 rt2x00dev->csr.base = NULL;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700239 }
240}
241
242static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
243{
Gertjan van Wingerde14a3bf82008-06-16 19:55:43 +0200244 struct pci_dev *pci_dev = to_pci_dev(rt2x00dev->dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700245
Arjan van de Ven275f1652008-10-20 21:42:39 -0700246 rt2x00dev->csr.base = pci_ioremap_bar(pci_dev, 0);
Ivo van Doorn21795092008-02-10 22:49:13 +0100247 if (!rt2x00dev->csr.base)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700248 goto exit;
249
250 rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
251 if (!rt2x00dev->eeprom)
252 goto exit;
253
254 rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
255 if (!rt2x00dev->rf)
256 goto exit;
257
258 return 0;
259
260exit:
261 ERROR_PROBE("Failed to allocate registers.\n");
262
263 rt2x00pci_free_reg(rt2x00dev);
264
265 return -ENOMEM;
266}
267
268int rt2x00pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
269{
270 struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_data;
271 struct ieee80211_hw *hw;
272 struct rt2x00_dev *rt2x00dev;
273 int retval;
274
275 retval = pci_request_regions(pci_dev, pci_name(pci_dev));
276 if (retval) {
277 ERROR_PROBE("PCI request regions failed.\n");
278 return retval;
279 }
280
281 retval = pci_enable_device(pci_dev);
282 if (retval) {
283 ERROR_PROBE("Enable device failed.\n");
284 goto exit_release_regions;
285 }
286
287 pci_set_master(pci_dev);
288
289 if (pci_set_mwi(pci_dev))
290 ERROR_PROBE("MWI not available.\n");
291
Yang Hongyang284901a2009-04-06 19:01:15 -0700292 if (dma_set_mask(&pci_dev->dev, DMA_BIT_MASK(32))) {
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700293 ERROR_PROBE("PCI DMA not supported.\n");
294 retval = -EIO;
295 goto exit_disable_device;
296 }
297
298 hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
299 if (!hw) {
300 ERROR_PROBE("Failed to allocate hardware.\n");
301 retval = -ENOMEM;
302 goto exit_disable_device;
303 }
304
305 pci_set_drvdata(pci_dev, hw);
306
307 rt2x00dev = hw->priv;
Gertjan van Wingerde14a3bf82008-06-16 19:55:43 +0200308 rt2x00dev->dev = &pci_dev->dev;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700309 rt2x00dev->ops = ops;
310 rt2x00dev->hw = hw;
Ivo van Doorn440ddad2009-03-28 20:51:24 +0100311 rt2x00dev->irq = pci_dev->irq;
312 rt2x00dev->name = pci_name(pci_dev);
313
Gertjan van Wingerde2015d192009-11-08 12:30:14 +0100314 rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_PCI);
315
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700316 retval = rt2x00pci_alloc_reg(rt2x00dev);
317 if (retval)
318 goto exit_free_device;
319
320 retval = rt2x00lib_probe_dev(rt2x00dev);
321 if (retval)
322 goto exit_free_reg;
323
324 return 0;
325
326exit_free_reg:
327 rt2x00pci_free_reg(rt2x00dev);
328
329exit_free_device:
330 ieee80211_free_hw(hw);
331
332exit_disable_device:
333 if (retval != -EBUSY)
334 pci_disable_device(pci_dev);
335
336exit_release_regions:
337 pci_release_regions(pci_dev);
338
339 pci_set_drvdata(pci_dev, NULL);
340
341 return retval;
342}
343EXPORT_SYMBOL_GPL(rt2x00pci_probe);
344
345void rt2x00pci_remove(struct pci_dev *pci_dev)
346{
347 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
348 struct rt2x00_dev *rt2x00dev = hw->priv;
349
350 /*
351 * Free all allocated data.
352 */
353 rt2x00lib_remove_dev(rt2x00dev);
354 rt2x00pci_free_reg(rt2x00dev);
355 ieee80211_free_hw(hw);
356
357 /*
358 * Free the PCI device data.
359 */
360 pci_set_drvdata(pci_dev, NULL);
361 pci_disable_device(pci_dev);
362 pci_release_regions(pci_dev);
363}
364EXPORT_SYMBOL_GPL(rt2x00pci_remove);
365
366#ifdef CONFIG_PM
367int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
368{
369 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
370 struct rt2x00_dev *rt2x00dev = hw->priv;
371 int retval;
372
373 retval = rt2x00lib_suspend(rt2x00dev, state);
374 if (retval)
375 return retval;
376
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700377 pci_save_state(pci_dev);
378 pci_disable_device(pci_dev);
379 return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
380}
381EXPORT_SYMBOL_GPL(rt2x00pci_suspend);
382
383int rt2x00pci_resume(struct pci_dev *pci_dev)
384{
385 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
386 struct rt2x00_dev *rt2x00dev = hw->priv;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700387
388 if (pci_set_power_state(pci_dev, PCI_D0) ||
389 pci_enable_device(pci_dev) ||
390 pci_restore_state(pci_dev)) {
391 ERROR(rt2x00dev, "Failed to resume device.\n");
392 return -EIO;
393 }
394
Ivo van Doorn499a2142009-03-28 20:51:58 +0100395 return rt2x00lib_resume(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700396}
397EXPORT_SYMBOL_GPL(rt2x00pci_resume);
398#endif /* CONFIG_PM */
399
400/*
401 * rt2x00pci module information.
402 */
403MODULE_AUTHOR(DRV_PROJECT);
404MODULE_VERSION(DRV_VERSION);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500405MODULE_DESCRIPTION("rt2x00 pci library");
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700406MODULE_LICENSE("GPL");