blob: cf3f1c0c43822fe70d0390bfd4ff060c306c07c3 [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 */
Ivo van Doorn6db37862008-06-06 22:50:28 +020066int rt2x00pci_write_tx_data(struct queue_entry *entry)
Ivo van Doorn95ea3622007-09-25 17:57:13 -070067{
Ivo van Doorn798b7ad2008-11-08 15:25:33 +010068 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
Ivo van Doornb8be63f2008-05-10 13:46:03 +020069 struct queue_entry_priv_pci *entry_priv = entry->priv_data;
Ivo van Doorn181d6902008-02-05 16:42:23 -050070 struct skb_frame_desc *skbdesc;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070071
Ivo van Doorn6db37862008-06-06 22:50:28 +020072 /*
73 * This should not happen, we already checked the entry
74 * was ours. When the hardware disagrees there has been
75 * a queue corruption!
76 */
Ivo van Doorn798b7ad2008-11-08 15:25:33 +010077 if (unlikely(rt2x00dev->ops->lib->get_entry_state(entry))) {
78 ERROR(rt2x00dev,
Ivo van Doorn6db37862008-06-06 22:50:28 +020079 "Corrupt queue %d, accessing entry which is not ours.\n"
Ivo van Doorn95ea3622007-09-25 17:57:13 -070080 "Please file bug report to %s.\n",
Ivo van Doorne58c6ac2008-04-21 19:00:47 +020081 entry->queue->qid, DRV_PROJECT);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070082 return -EINVAL;
83 }
84
Ivo van Doorn08992f72008-01-24 01:56:25 -080085 /*
86 * Fill in skb descriptor
87 */
Ivo van Doorn6db37862008-06-06 22:50:28 +020088 skbdesc = get_skb_frame_desc(entry->skb);
Ivo van Doornb8be63f2008-05-10 13:46:03 +020089 skbdesc->desc = entry_priv->desc;
Ivo van Doorn6db37862008-06-06 22:50:28 +020090 skbdesc->desc_len = entry->queue->desc_size;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070091
Ivo van Doorn95ea3622007-09-25 17:57:13 -070092 return 0;
93}
94EXPORT_SYMBOL_GPL(rt2x00pci_write_tx_data);
95
96/*
Ivo van Doorn3957ccb2007-11-12 15:02:40 +010097 * TX/RX data handlers.
Ivo van Doorn95ea3622007-09-25 17:57:13 -070098 */
99void rt2x00pci_rxdone(struct rt2x00_dev *rt2x00dev)
100{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500101 struct data_queue *queue = rt2x00dev->rx;
102 struct queue_entry *entry;
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200103 struct queue_entry_priv_pci *entry_priv;
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200104 struct skb_frame_desc *skbdesc;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700105
106 while (1) {
Ivo van Doorn181d6902008-02-05 16:42:23 -0500107 entry = rt2x00queue_get_entry(queue, Q_INDEX);
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200108 entry_priv = entry->priv_data;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700109
Ivo van Doorn798b7ad2008-11-08 15:25:33 +0100110 if (rt2x00dev->ops->lib->get_entry_state(entry))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700111 break;
112
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200113 /*
114 * Fill in desc fields of the skb descriptor
115 */
116 skbdesc = get_skb_frame_desc(entry->skb);
117 skbdesc->desc = entry_priv->desc;
118 skbdesc->desc_len = entry->queue->desc_size;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700119
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200120 /*
121 * Send the frame to rt2x00lib for further processing.
122 */
123 rt2x00lib_rxdone(rt2x00dev, entry);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700124 }
125}
126EXPORT_SYMBOL_GPL(rt2x00pci_rxdone);
127
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700128/*
129 * Device initialization handlers.
130 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500131static int rt2x00pci_alloc_queue_dma(struct rt2x00_dev *rt2x00dev,
132 struct data_queue *queue)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700133{
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200134 struct queue_entry_priv_pci *entry_priv;
Ivo van Doorn30b3a232008-02-17 17:33:24 +0100135 void *addr;
Ivo van Doorn9c9dd2c2008-02-10 22:46:52 +0100136 dma_addr_t dma;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700137 unsigned int i;
138
139 /*
140 * Allocate DMA memory for descriptor and buffer.
141 */
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200142 addr = dma_alloc_coherent(rt2x00dev->dev,
143 queue->limit * queue->desc_size,
144 &dma, GFP_KERNEL | GFP_DMA);
Ivo van Doorn30b3a232008-02-17 17:33:24 +0100145 if (!addr)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700146 return -ENOMEM;
147
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200148 memset(addr, 0, queue->limit * queue->desc_size);
Ivo van Doorn9c9dd2c2008-02-10 22:46:52 +0100149
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700150 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500151 * Initialize all queue entries to contain valid addresses.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700152 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500153 for (i = 0; i < queue->limit; i++) {
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200154 entry_priv = queue->entries[i].priv_data;
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200155 entry_priv->desc = addr + i * queue->desc_size;
156 entry_priv->desc_dma = dma + i * queue->desc_size;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700157 }
158
159 return 0;
160}
161
Ivo van Doorn181d6902008-02-05 16:42:23 -0500162static void rt2x00pci_free_queue_dma(struct rt2x00_dev *rt2x00dev,
163 struct data_queue *queue)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700164{
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200165 struct queue_entry_priv_pci *entry_priv =
166 queue->entries[0].priv_data;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500167
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200168 if (entry_priv->desc)
169 dma_free_coherent(rt2x00dev->dev,
170 queue->limit * queue->desc_size,
171 entry_priv->desc, entry_priv->desc_dma);
172 entry_priv->desc = NULL;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700173}
174
175int rt2x00pci_initialize(struct rt2x00_dev *rt2x00dev)
176{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500177 struct data_queue *queue;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700178 int status;
179
180 /*
181 * Allocate DMA
182 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500183 queue_for_each(rt2x00dev, queue) {
184 status = rt2x00pci_alloc_queue_dma(rt2x00dev, queue);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700185 if (status)
186 goto exit;
187 }
188
189 /*
190 * Register interrupt handler.
191 */
Ivo van Doorn440ddad2009-03-28 20:51:24 +0100192 status = request_irq(rt2x00dev->irq, rt2x00dev->ops->lib->irq_handler,
193 IRQF_SHARED, rt2x00dev->name, rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700194 if (status) {
195 ERROR(rt2x00dev, "IRQ %d allocation failed (error %d).\n",
Ivo van Doorn440ddad2009-03-28 20:51:24 +0100196 rt2x00dev->irq, status);
Ivo van Doornb30cdfc2008-05-05 17:24:03 +0200197 goto exit;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700198 }
199
200 return 0;
201
202exit:
Ivo van Doornb30cdfc2008-05-05 17:24:03 +0200203 queue_for_each(rt2x00dev, queue)
204 rt2x00pci_free_queue_dma(rt2x00dev, queue);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700205
206 return status;
207}
208EXPORT_SYMBOL_GPL(rt2x00pci_initialize);
209
210void rt2x00pci_uninitialize(struct rt2x00_dev *rt2x00dev)
211{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500212 struct data_queue *queue;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700213
214 /*
215 * Free irq line.
216 */
Gertjan van Wingerde14a3bf82008-06-16 19:55:43 +0200217 free_irq(to_pci_dev(rt2x00dev->dev)->irq, rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700218
219 /*
220 * Free DMA
221 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500222 queue_for_each(rt2x00dev, queue)
223 rt2x00pci_free_queue_dma(rt2x00dev, queue);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700224}
225EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize);
226
227/*
228 * PCI driver handlers.
229 */
230static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
231{
232 kfree(rt2x00dev->rf);
233 rt2x00dev->rf = NULL;
234
235 kfree(rt2x00dev->eeprom);
236 rt2x00dev->eeprom = NULL;
237
Ivo van Doorn21795092008-02-10 22:49:13 +0100238 if (rt2x00dev->csr.base) {
239 iounmap(rt2x00dev->csr.base);
240 rt2x00dev->csr.base = NULL;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700241 }
242}
243
244static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
245{
Gertjan van Wingerde14a3bf82008-06-16 19:55:43 +0200246 struct pci_dev *pci_dev = to_pci_dev(rt2x00dev->dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700247
Arjan van de Ven275f1652008-10-20 21:42:39 -0700248 rt2x00dev->csr.base = pci_ioremap_bar(pci_dev, 0);
Ivo van Doorn21795092008-02-10 22:49:13 +0100249 if (!rt2x00dev->csr.base)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700250 goto exit;
251
252 rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
253 if (!rt2x00dev->eeprom)
254 goto exit;
255
256 rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
257 if (!rt2x00dev->rf)
258 goto exit;
259
260 return 0;
261
262exit:
263 ERROR_PROBE("Failed to allocate registers.\n");
264
265 rt2x00pci_free_reg(rt2x00dev);
266
267 return -ENOMEM;
268}
269
270int rt2x00pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
271{
272 struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_data;
273 struct ieee80211_hw *hw;
274 struct rt2x00_dev *rt2x00dev;
275 int retval;
276
277 retval = pci_request_regions(pci_dev, pci_name(pci_dev));
278 if (retval) {
279 ERROR_PROBE("PCI request regions failed.\n");
280 return retval;
281 }
282
283 retval = pci_enable_device(pci_dev);
284 if (retval) {
285 ERROR_PROBE("Enable device failed.\n");
286 goto exit_release_regions;
287 }
288
289 pci_set_master(pci_dev);
290
291 if (pci_set_mwi(pci_dev))
292 ERROR_PROBE("MWI not available.\n");
293
Yang Hongyang284901a2009-04-06 19:01:15 -0700294 if (dma_set_mask(&pci_dev->dev, DMA_BIT_MASK(32))) {
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700295 ERROR_PROBE("PCI DMA not supported.\n");
296 retval = -EIO;
297 goto exit_disable_device;
298 }
299
300 hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
301 if (!hw) {
302 ERROR_PROBE("Failed to allocate hardware.\n");
303 retval = -ENOMEM;
304 goto exit_disable_device;
305 }
306
307 pci_set_drvdata(pci_dev, hw);
308
309 rt2x00dev = hw->priv;
Gertjan van Wingerde14a3bf82008-06-16 19:55:43 +0200310 rt2x00dev->dev = &pci_dev->dev;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700311 rt2x00dev->ops = ops;
312 rt2x00dev->hw = hw;
Ivo van Doorn440ddad2009-03-28 20:51:24 +0100313 rt2x00dev->irq = pci_dev->irq;
314 rt2x00dev->name = pci_name(pci_dev);
315
Gertjan van Wingerde2015d192009-11-08 12:30:14 +0100316 rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_PCI);
317
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700318 retval = rt2x00pci_alloc_reg(rt2x00dev);
319 if (retval)
320 goto exit_free_device;
321
322 retval = rt2x00lib_probe_dev(rt2x00dev);
323 if (retval)
324 goto exit_free_reg;
325
326 return 0;
327
328exit_free_reg:
329 rt2x00pci_free_reg(rt2x00dev);
330
331exit_free_device:
332 ieee80211_free_hw(hw);
333
334exit_disable_device:
335 if (retval != -EBUSY)
336 pci_disable_device(pci_dev);
337
338exit_release_regions:
339 pci_release_regions(pci_dev);
340
341 pci_set_drvdata(pci_dev, NULL);
342
343 return retval;
344}
345EXPORT_SYMBOL_GPL(rt2x00pci_probe);
346
347void rt2x00pci_remove(struct pci_dev *pci_dev)
348{
349 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
350 struct rt2x00_dev *rt2x00dev = hw->priv;
351
352 /*
353 * Free all allocated data.
354 */
355 rt2x00lib_remove_dev(rt2x00dev);
356 rt2x00pci_free_reg(rt2x00dev);
357 ieee80211_free_hw(hw);
358
359 /*
360 * Free the PCI device data.
361 */
362 pci_set_drvdata(pci_dev, NULL);
363 pci_disable_device(pci_dev);
364 pci_release_regions(pci_dev);
365}
366EXPORT_SYMBOL_GPL(rt2x00pci_remove);
367
368#ifdef CONFIG_PM
369int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
370{
371 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
372 struct rt2x00_dev *rt2x00dev = hw->priv;
373 int retval;
374
375 retval = rt2x00lib_suspend(rt2x00dev, state);
376 if (retval)
377 return retval;
378
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700379 pci_save_state(pci_dev);
380 pci_disable_device(pci_dev);
381 return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
382}
383EXPORT_SYMBOL_GPL(rt2x00pci_suspend);
384
385int rt2x00pci_resume(struct pci_dev *pci_dev)
386{
387 struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
388 struct rt2x00_dev *rt2x00dev = hw->priv;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700389
390 if (pci_set_power_state(pci_dev, PCI_D0) ||
391 pci_enable_device(pci_dev) ||
392 pci_restore_state(pci_dev)) {
393 ERROR(rt2x00dev, "Failed to resume device.\n");
394 return -EIO;
395 }
396
Ivo van Doorn499a2142009-03-28 20:51:58 +0100397 return rt2x00lib_resume(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700398}
399EXPORT_SYMBOL_GPL(rt2x00pci_resume);
400#endif /* CONFIG_PM */
401
402/*
403 * rt2x00pci module information.
404 */
405MODULE_AUTHOR(DRV_PROJECT);
406MODULE_VERSION(DRV_VERSION);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500407MODULE_DESCRIPTION("rt2x00 pci library");
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700408MODULE_LICENSE("GPL");