blob: be311e9404b44c3764f2b86c531ba1a59fd91d85 [file] [log] [blame]
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001/*
2 * Copyright (C) 2006-2007 PA Semi, Inc
3 *
4 * Driver for the PA Semi PWRficient onchip 1G/10G Ethernet MACs
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
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 Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/pci.h>
23#include <linux/interrupt.h>
24#include <linux/dmaengine.h>
25#include <linux/delay.h>
26#include <linux/netdevice.h>
27#include <linux/etherdevice.h>
28#include <asm/dma-mapping.h>
29#include <linux/in.h>
30#include <linux/skbuff.h>
31
32#include <linux/ip.h>
33#include <linux/tcp.h>
34#include <net/checksum.h>
35
Olof Johansson771f7402007-05-08 00:47:21 -050036#include <asm/irq.h>
37
Olof Johanssonf5cd7872007-01-31 21:43:54 -060038#include "pasemi_mac.h"
39
40
41/* TODO list
42 *
43 * - Get rid of pci_{read,write}_config(), map registers with ioremap
44 * for performance
45 * - PHY support
46 * - Multicast support
47 * - Large MTU support
48 * - Other performance improvements
49 */
50
51
52/* Must be a power of two */
53#define RX_RING_SIZE 512
54#define TX_RING_SIZE 512
55
Olof Johanssonceb51362007-05-08 00:47:49 -050056#define DEFAULT_MSG_ENABLE \
57 (NETIF_MSG_DRV | \
58 NETIF_MSG_PROBE | \
59 NETIF_MSG_LINK | \
60 NETIF_MSG_TIMER | \
61 NETIF_MSG_IFDOWN | \
62 NETIF_MSG_IFUP | \
63 NETIF_MSG_RX_ERR | \
64 NETIF_MSG_TX_ERR)
65
Olof Johanssonf5cd7872007-01-31 21:43:54 -060066#define TX_DESC(mac, num) ((mac)->tx->desc[(num) & (TX_RING_SIZE-1)])
67#define TX_DESC_INFO(mac, num) ((mac)->tx->desc_info[(num) & (TX_RING_SIZE-1)])
68#define RX_DESC(mac, num) ((mac)->rx->desc[(num) & (RX_RING_SIZE-1)])
69#define RX_DESC_INFO(mac, num) ((mac)->rx->desc_info[(num) & (RX_RING_SIZE-1)])
70#define RX_BUFF(mac, num) ((mac)->rx->buffers[(num) & (RX_RING_SIZE-1)])
71
72#define BUF_SIZE 1646 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
73
Olof Johanssonceb51362007-05-08 00:47:49 -050074MODULE_LICENSE("GPL");
75MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
76MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
77
78static int debug = -1; /* -1 == use DEFAULT_MSG_ENABLE as value */
79module_param(debug, int, 0);
80MODULE_PARM_DESC(debug, "PA Semi MAC bitmapped debugging message enable value");
Olof Johanssonf5cd7872007-01-31 21:43:54 -060081
82static struct pasdma_status *dma_status;
83
Olof Johanssona85b9422007-09-15 13:40:59 -070084static unsigned int read_iob_reg(struct pasemi_mac *mac, unsigned int reg)
85{
Olof Johanssonb6e05a12007-09-15 13:44:07 -070086 return in_le32(mac->iob_regs+reg);
Olof Johanssona85b9422007-09-15 13:40:59 -070087}
88
89static void write_iob_reg(struct pasemi_mac *mac, unsigned int reg,
90 unsigned int val)
91{
Olof Johanssonb6e05a12007-09-15 13:44:07 -070092 out_le32(mac->iob_regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -070093}
94
95static unsigned int read_mac_reg(struct pasemi_mac *mac, unsigned int reg)
96{
Olof Johanssonb6e05a12007-09-15 13:44:07 -070097 return in_le32(mac->regs+reg);
Olof Johanssona85b9422007-09-15 13:40:59 -070098}
99
100static void write_mac_reg(struct pasemi_mac *mac, unsigned int reg,
101 unsigned int val)
102{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700103 out_le32(mac->regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700104}
105
106static unsigned int read_dma_reg(struct pasemi_mac *mac, unsigned int reg)
107{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700108 return in_le32(mac->dma_regs+reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700109}
110
111static void write_dma_reg(struct pasemi_mac *mac, unsigned int reg,
112 unsigned int val)
113{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700114 out_le32(mac->dma_regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700115}
116
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600117static int pasemi_get_mac_addr(struct pasemi_mac *mac)
118{
119 struct pci_dev *pdev = mac->pdev;
120 struct device_node *dn = pci_device_to_OF_node(pdev);
olof@lixom.net1af7f052007-05-12 14:57:46 -0500121 int len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600122 const u8 *maddr;
123 u8 addr[6];
124
125 if (!dn) {
126 dev_dbg(&pdev->dev,
127 "No device node for mac, not configuring\n");
128 return -ENOENT;
129 }
130
olof@lixom.net1af7f052007-05-12 14:57:46 -0500131 maddr = of_get_property(dn, "local-mac-address", &len);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500132
olof@lixom.net1af7f052007-05-12 14:57:46 -0500133 if (maddr && len == 6) {
134 memcpy(mac->mac_addr, maddr, 6);
135 return 0;
136 }
137
138 /* Some old versions of firmware mistakenly uses mac-address
139 * (and as a string) instead of a byte array in local-mac-address.
140 */
141
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500142 if (maddr == NULL)
Linus Torvalds90287802007-05-08 11:57:17 -0700143 maddr = of_get_property(dn, "mac-address", NULL);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500144
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600145 if (maddr == NULL) {
146 dev_warn(&pdev->dev,
147 "no mac address in device tree, not configuring\n");
148 return -ENOENT;
149 }
150
olof@lixom.net1af7f052007-05-12 14:57:46 -0500151
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600152 if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0],
153 &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) {
154 dev_warn(&pdev->dev,
155 "can't parse mac address, not configuring\n");
156 return -EINVAL;
157 }
158
olof@lixom.net1af7f052007-05-12 14:57:46 -0500159 memcpy(mac->mac_addr, addr, 6);
160
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600161 return 0;
162}
163
164static int pasemi_mac_setup_rx_resources(struct net_device *dev)
165{
166 struct pasemi_mac_rxring *ring;
167 struct pasemi_mac *mac = netdev_priv(dev);
168 int chan_id = mac->dma_rxch;
169
170 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
171
172 if (!ring)
173 goto out_ring;
174
175 spin_lock_init(&ring->lock);
176
177 ring->desc_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
178 RX_RING_SIZE, GFP_KERNEL);
179
180 if (!ring->desc_info)
181 goto out_desc_info;
182
183 /* Allocate descriptors */
184 ring->desc = dma_alloc_coherent(&mac->dma_pdev->dev,
185 RX_RING_SIZE *
186 sizeof(struct pas_dma_xct_descr),
187 &ring->dma, GFP_KERNEL);
188
189 if (!ring->desc)
190 goto out_desc;
191
192 memset(ring->desc, 0, RX_RING_SIZE * sizeof(struct pas_dma_xct_descr));
193
194 ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev,
195 RX_RING_SIZE * sizeof(u64),
196 &ring->buf_dma, GFP_KERNEL);
197 if (!ring->buffers)
198 goto out_buffers;
199
200 memset(ring->buffers, 0, RX_RING_SIZE * sizeof(u64));
201
Olof Johanssona85b9422007-09-15 13:40:59 -0700202 write_dma_reg(mac, PAS_DMA_RXCHAN_BASEL(chan_id), PAS_DMA_RXCHAN_BASEL_BRBL(ring->dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600203
Olof Johanssona85b9422007-09-15 13:40:59 -0700204 write_dma_reg(mac, PAS_DMA_RXCHAN_BASEU(chan_id),
205 PAS_DMA_RXCHAN_BASEU_BRBH(ring->dma >> 32) |
206 PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 2));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600207
Olof Johanssona85b9422007-09-15 13:40:59 -0700208 write_dma_reg(mac, PAS_DMA_RXCHAN_CFG(chan_id),
209 PAS_DMA_RXCHAN_CFG_HBU(1));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600210
Olof Johanssona85b9422007-09-15 13:40:59 -0700211 write_dma_reg(mac, PAS_DMA_RXINT_BASEL(mac->dma_if),
212 PAS_DMA_RXINT_BASEL_BRBL(__pa(ring->buffers)));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600213
Olof Johanssona85b9422007-09-15 13:40:59 -0700214 write_dma_reg(mac, PAS_DMA_RXINT_BASEU(mac->dma_if),
215 PAS_DMA_RXINT_BASEU_BRBH(__pa(ring->buffers) >> 32) |
216 PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600217
218 ring->next_to_fill = 0;
219 ring->next_to_clean = 0;
220
221 snprintf(ring->irq_name, sizeof(ring->irq_name),
222 "%s rx", dev->name);
223 mac->rx = ring;
224
225 return 0;
226
227out_buffers:
228 dma_free_coherent(&mac->dma_pdev->dev,
229 RX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
230 mac->rx->desc, mac->rx->dma);
231out_desc:
232 kfree(ring->desc_info);
233out_desc_info:
234 kfree(ring);
235out_ring:
236 return -ENOMEM;
237}
238
239
240static int pasemi_mac_setup_tx_resources(struct net_device *dev)
241{
242 struct pasemi_mac *mac = netdev_priv(dev);
243 u32 val;
244 int chan_id = mac->dma_txch;
245 struct pasemi_mac_txring *ring;
246
247 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
248 if (!ring)
249 goto out_ring;
250
251 spin_lock_init(&ring->lock);
252
253 ring->desc_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
254 TX_RING_SIZE, GFP_KERNEL);
255 if (!ring->desc_info)
256 goto out_desc_info;
257
258 /* Allocate descriptors */
259 ring->desc = dma_alloc_coherent(&mac->dma_pdev->dev,
260 TX_RING_SIZE *
261 sizeof(struct pas_dma_xct_descr),
262 &ring->dma, GFP_KERNEL);
263 if (!ring->desc)
264 goto out_desc;
265
266 memset(ring->desc, 0, TX_RING_SIZE * sizeof(struct pas_dma_xct_descr));
267
Olof Johanssona85b9422007-09-15 13:40:59 -0700268 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEL(chan_id),
269 PAS_DMA_TXCHAN_BASEL_BRBL(ring->dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600270 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->dma >> 32);
271 val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 2);
272
Olof Johanssona85b9422007-09-15 13:40:59 -0700273 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEU(chan_id), val);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600274
Olof Johanssona85b9422007-09-15 13:40:59 -0700275 write_dma_reg(mac, PAS_DMA_TXCHAN_CFG(chan_id),
276 PAS_DMA_TXCHAN_CFG_TY_IFACE |
277 PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
278 PAS_DMA_TXCHAN_CFG_UP |
279 PAS_DMA_TXCHAN_CFG_WT(2));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600280
281 ring->next_to_use = 0;
282 ring->next_to_clean = 0;
283
284 snprintf(ring->irq_name, sizeof(ring->irq_name),
285 "%s tx", dev->name);
286 mac->tx = ring;
287
288 return 0;
289
290out_desc:
291 kfree(ring->desc_info);
292out_desc_info:
293 kfree(ring);
294out_ring:
295 return -ENOMEM;
296}
297
298static void pasemi_mac_free_tx_resources(struct net_device *dev)
299{
300 struct pasemi_mac *mac = netdev_priv(dev);
301 unsigned int i;
302 struct pasemi_mac_buffer *info;
303 struct pas_dma_xct_descr *dp;
304
305 for (i = 0; i < TX_RING_SIZE; i++) {
306 info = &TX_DESC_INFO(mac, i);
307 dp = &TX_DESC(mac, i);
308 if (info->dma) {
309 if (info->skb) {
310 pci_unmap_single(mac->dma_pdev,
311 info->dma,
312 info->skb->len,
313 PCI_DMA_TODEVICE);
314 dev_kfree_skb_any(info->skb);
315 }
316 info->dma = 0;
317 info->skb = NULL;
318 dp->mactx = 0;
319 dp->ptr = 0;
320 }
321 }
322
323 dma_free_coherent(&mac->dma_pdev->dev,
324 TX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
325 mac->tx->desc, mac->tx->dma);
326
327 kfree(mac->tx->desc_info);
328 kfree(mac->tx);
329 mac->tx = NULL;
330}
331
332static void pasemi_mac_free_rx_resources(struct net_device *dev)
333{
334 struct pasemi_mac *mac = netdev_priv(dev);
335 unsigned int i;
336 struct pasemi_mac_buffer *info;
337 struct pas_dma_xct_descr *dp;
338
339 for (i = 0; i < RX_RING_SIZE; i++) {
340 info = &RX_DESC_INFO(mac, i);
341 dp = &RX_DESC(mac, i);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500342 if (info->skb) {
343 if (info->dma) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600344 pci_unmap_single(mac->dma_pdev,
345 info->dma,
346 info->skb->len,
347 PCI_DMA_FROMDEVICE);
348 dev_kfree_skb_any(info->skb);
349 }
350 info->dma = 0;
351 info->skb = NULL;
352 dp->macrx = 0;
353 dp->ptr = 0;
354 }
355 }
356
357 dma_free_coherent(&mac->dma_pdev->dev,
358 RX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
359 mac->rx->desc, mac->rx->dma);
360
361 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
362 mac->rx->buffers, mac->rx->buf_dma);
363
364 kfree(mac->rx->desc_info);
365 kfree(mac->rx);
366 mac->rx = NULL;
367}
368
369static void pasemi_mac_replenish_rx_ring(struct net_device *dev)
370{
371 struct pasemi_mac *mac = netdev_priv(dev);
372 unsigned int i;
373 int start = mac->rx->next_to_fill;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500374 unsigned int limit, count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600375
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500376 limit = (mac->rx->next_to_clean + RX_RING_SIZE -
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600377 mac->rx->next_to_fill) & (RX_RING_SIZE - 1);
378
379 /* Check to see if we're doing first-time setup */
380 if (unlikely(mac->rx->next_to_clean == 0 && mac->rx->next_to_fill == 0))
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500381 limit = RX_RING_SIZE;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600382
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500383 if (limit <= 0)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600384 return;
385
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500386 i = start;
387 for (count = limit; count; count--) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600388 struct pasemi_mac_buffer *info = &RX_DESC_INFO(mac, i);
389 u64 *buff = &RX_BUFF(mac, i);
390 struct sk_buff *skb;
391 dma_addr_t dma;
392
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500393 /* skb might still be in there for recycle on short receives */
394 if (info->skb)
395 skb = info->skb;
396 else
397 skb = dev_alloc_skb(BUF_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600398
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500399 if (unlikely(!skb))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600400 break;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600401
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600402 dma = pci_map_single(mac->dma_pdev, skb->data, skb->len,
403 PCI_DMA_FROMDEVICE);
404
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500405 if (unlikely(dma_mapping_error(dma))) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600406 dev_kfree_skb_irq(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600407 break;
408 }
409
410 info->skb = skb;
411 info->dma = dma;
412 *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500413 i++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600414 }
415
416 wmb();
417
Olof Johanssona85b9422007-09-15 13:40:59 -0700418 write_dma_reg(mac, PAS_DMA_RXCHAN_INCR(mac->dma_rxch), limit - count);
419 write_dma_reg(mac, PAS_DMA_RXINT_INCR(mac->dma_if), limit - count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600420
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500421 mac->rx->next_to_fill += limit - count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600422}
423
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500424static void pasemi_mac_restart_rx_intr(struct pasemi_mac *mac)
425{
Olof Johansson52a94352007-05-12 18:01:09 -0500426 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500427 /* Re-enable packet count interrupts: finally
428 * ack the packet count interrupt we got in rx_intr.
429 */
430
Olof Johansson52a94352007-05-12 18:01:09 -0500431 pcnt = *mac->rx_status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500432
Olof Johansson52a94352007-05-12 18:01:09 -0500433 reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500434
Olof Johanssona85b9422007-09-15 13:40:59 -0700435 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500436}
437
438static void pasemi_mac_restart_tx_intr(struct pasemi_mac *mac)
439{
Olof Johansson52a94352007-05-12 18:01:09 -0500440 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500441
442 /* Re-enable packet count interrupts */
Olof Johansson52a94352007-05-12 18:01:09 -0500443 pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500444
Olof Johansson52a94352007-05-12 18:01:09 -0500445 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500446
Olof Johanssona85b9422007-09-15 13:40:59 -0700447 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500448}
449
450
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600451static int pasemi_mac_clean_rx(struct pasemi_mac *mac, int limit)
452{
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500453 unsigned int n;
454 int count;
455 struct pas_dma_xct_descr *dp;
456 struct pasemi_mac_buffer *info;
457 struct sk_buff *skb;
458 unsigned int i, len;
459 u64 macrx;
460 dma_addr_t dma;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600461
462 spin_lock(&mac->rx->lock);
463
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500464 n = mac->rx->next_to_clean;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600465
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500466 for (count = limit; count; count--) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600467
468 rmb();
469
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500470 dp = &RX_DESC(mac, n);
471 macrx = dp->macrx;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600472
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500473 if (!(macrx & XCT_MACRX_O))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600474 break;
475
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600476
477 info = NULL;
478
479 /* We have to scan for our skb since there's no way
480 * to back-map them from the descriptor, and if we
481 * have several receive channels then they might not
482 * show up in the same order as they were put on the
483 * interface ring.
484 */
485
486 dma = (dp->ptr & XCT_PTR_ADDR_M);
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500487 for (i = n; i < (n + RX_RING_SIZE); i++) {
488 info = &RX_DESC_INFO(mac, i);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600489 if (info->dma == dma)
490 break;
491 }
492
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500493 skb = info->skb;
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500494 info->dma = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600495
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500496 pci_unmap_single(mac->dma_pdev, dma, skb->len,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600497 PCI_DMA_FROMDEVICE);
498
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500499 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600500
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500501 if (len < 256) {
502 struct sk_buff *new_skb =
503 netdev_alloc_skb(mac->netdev, len + NET_IP_ALIGN);
504 if (new_skb) {
505 skb_reserve(new_skb, NET_IP_ALIGN);
506 memcpy(new_skb->data - NET_IP_ALIGN,
507 skb->data - NET_IP_ALIGN,
508 len + NET_IP_ALIGN);
509 /* save the skb in buffer_info as good */
510 skb = new_skb;
511 }
512 /* else just continue with the old one */
513 } else
514 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600515
516 skb_put(skb, len);
517
518 skb->protocol = eth_type_trans(skb, mac->netdev);
519
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500520 if ((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600521 skb->ip_summed = CHECKSUM_COMPLETE;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500522 skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600523 XCT_MACRX_CSUM_S;
524 } else
525 skb->ip_summed = CHECKSUM_NONE;
526
527 mac->stats.rx_bytes += len;
528 mac->stats.rx_packets++;
529
530 netif_receive_skb(skb);
531
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600532 dp->ptr = 0;
533 dp->macrx = 0;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500534
535 n++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600536 }
537
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500538 mac->rx->next_to_clean += limit - count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600539 pasemi_mac_replenish_rx_ring(mac->netdev);
540
541 spin_unlock(&mac->rx->lock);
542
543 return count;
544}
545
546static int pasemi_mac_clean_tx(struct pasemi_mac *mac)
547{
548 int i;
549 struct pasemi_mac_buffer *info;
550 struct pas_dma_xct_descr *dp;
551 int start, count;
552 int flags;
553
554 spin_lock_irqsave(&mac->tx->lock, flags);
555
556 start = mac->tx->next_to_clean;
557 count = 0;
558
559 for (i = start; i < mac->tx->next_to_use; i++) {
560 dp = &TX_DESC(mac, i);
561 if (!dp || (dp->mactx & XCT_MACTX_O))
562 break;
563
564 count++;
565
566 info = &TX_DESC_INFO(mac, i);
567
568 pci_unmap_single(mac->dma_pdev, info->dma,
569 info->skb->len, PCI_DMA_TODEVICE);
570 dev_kfree_skb_irq(info->skb);
571
572 info->skb = NULL;
573 info->dma = 0;
574 dp->mactx = 0;
575 dp->ptr = 0;
576 }
577 mac->tx->next_to_clean += count;
578 spin_unlock_irqrestore(&mac->tx->lock, flags);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500579 netif_wake_queue(mac->netdev);
580
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600581 return count;
582}
583
584
585static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
586{
587 struct net_device *dev = data;
588 struct pasemi_mac *mac = netdev_priv(dev);
589 unsigned int reg;
590
Olof Johansson6dfa7522007-05-08 00:47:32 -0500591 if (!(*mac->rx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600592 return IRQ_NONE;
593
Olof Johansson6dfa7522007-05-08 00:47:32 -0500594 if (*mac->rx_status & PAS_STATUS_ERROR)
595 printk("rx_status reported error\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600596
Olof Johansson6dfa7522007-05-08 00:47:32 -0500597 /* Don't reset packet count so it won't fire again but clear
598 * all others.
599 */
600
Olof Johansson6dfa7522007-05-08 00:47:32 -0500601 reg = 0;
602 if (*mac->rx_status & PAS_STATUS_SOFT)
603 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
604 if (*mac->rx_status & PAS_STATUS_ERROR)
605 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600606 if (*mac->rx_status & PAS_STATUS_TIMER)
607 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
608
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700609 netif_rx_schedule(dev, &mac->napi);
Olof Johansson6dfa7522007-05-08 00:47:32 -0500610
Olof Johanssona85b9422007-09-15 13:40:59 -0700611 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600612
613 return IRQ_HANDLED;
614}
615
616static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
617{
618 struct net_device *dev = data;
619 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson52a94352007-05-12 18:01:09 -0500620 unsigned int reg, pcnt;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600621
Olof Johansson6dfa7522007-05-08 00:47:32 -0500622 if (!(*mac->tx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600623 return IRQ_NONE;
624
625 pasemi_mac_clean_tx(mac);
626
Olof Johansson52a94352007-05-12 18:01:09 -0500627 pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
628
629 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson6dfa7522007-05-08 00:47:32 -0500630
631 if (*mac->tx_status & PAS_STATUS_SOFT)
632 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
633 if (*mac->tx_status & PAS_STATUS_ERROR)
634 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600635
Olof Johanssona85b9422007-09-15 13:40:59 -0700636 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600637
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600638 return IRQ_HANDLED;
639}
640
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500641static void pasemi_adjust_link(struct net_device *dev)
642{
643 struct pasemi_mac *mac = netdev_priv(dev);
644 int msg;
645 unsigned int flags;
646 unsigned int new_flags;
647
648 if (!mac->phydev->link) {
649 /* If no link, MAC speed settings don't matter. Just report
650 * link down and return.
651 */
652 if (mac->link && netif_msg_link(mac))
653 printk(KERN_INFO "%s: Link is down.\n", dev->name);
654
655 netif_carrier_off(dev);
656 mac->link = 0;
657
658 return;
659 } else
660 netif_carrier_on(dev);
661
Olof Johanssona85b9422007-09-15 13:40:59 -0700662 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500663 new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
664 PAS_MAC_CFG_PCFG_TSR_M);
665
666 if (!mac->phydev->duplex)
667 new_flags |= PAS_MAC_CFG_PCFG_HD;
668
669 switch (mac->phydev->speed) {
670 case 1000:
671 new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
672 PAS_MAC_CFG_PCFG_TSR_1G;
673 break;
674 case 100:
675 new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
676 PAS_MAC_CFG_PCFG_TSR_100M;
677 break;
678 case 10:
679 new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
680 PAS_MAC_CFG_PCFG_TSR_10M;
681 break;
682 default:
683 printk("Unsupported speed %d\n", mac->phydev->speed);
684 }
685
686 /* Print on link or speed/duplex change */
687 msg = mac->link != mac->phydev->link || flags != new_flags;
688
689 mac->duplex = mac->phydev->duplex;
690 mac->speed = mac->phydev->speed;
691 mac->link = mac->phydev->link;
692
693 if (new_flags != flags)
Olof Johanssona85b9422007-09-15 13:40:59 -0700694 write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500695
696 if (msg && netif_msg_link(mac))
697 printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
698 dev->name, mac->speed, mac->duplex ? "full" : "half");
699}
700
701static int pasemi_mac_phy_init(struct net_device *dev)
702{
703 struct pasemi_mac *mac = netdev_priv(dev);
704 struct device_node *dn, *phy_dn;
705 struct phy_device *phydev;
706 unsigned int phy_id;
707 const phandle *ph;
708 const unsigned int *prop;
709 struct resource r;
710 int ret;
711
712 dn = pci_device_to_OF_node(mac->pdev);
Linus Torvalds90287802007-05-08 11:57:17 -0700713 ph = of_get_property(dn, "phy-handle", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500714 if (!ph)
715 return -ENODEV;
716 phy_dn = of_find_node_by_phandle(*ph);
717
Linus Torvalds90287802007-05-08 11:57:17 -0700718 prop = of_get_property(phy_dn, "reg", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500719 ret = of_address_to_resource(phy_dn->parent, 0, &r);
720 if (ret)
721 goto err;
722
723 phy_id = *prop;
724 snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);
725
726 of_node_put(phy_dn);
727
728 mac->link = 0;
729 mac->speed = 0;
730 mac->duplex = -1;
731
732 phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);
733
734 if (IS_ERR(phydev)) {
735 printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
736 return PTR_ERR(phydev);
737 }
738
739 mac->phydev = phydev;
740
741 return 0;
742
743err:
744 of_node_put(phy_dn);
745 return -ENODEV;
746}
747
748
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600749static int pasemi_mac_open(struct net_device *dev)
750{
751 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson771f7402007-05-08 00:47:21 -0500752 int base_irq;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600753 unsigned int flags;
754 int ret;
755
756 /* enable rx section */
Olof Johanssona85b9422007-09-15 13:40:59 -0700757 write_dma_reg(mac, PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600758
759 /* enable tx section */
Olof Johanssona85b9422007-09-15 13:40:59 -0700760 write_dma_reg(mac, PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600761
762 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
763 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
764 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
765
Olof Johanssona85b9422007-09-15 13:40:59 -0700766 write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600767
768 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
769 PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
770
771 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
772
Olof Johanssona85b9422007-09-15 13:40:59 -0700773 write_iob_reg(mac, PAS_IOB_DMA_RXCH_CFG(mac->dma_rxch),
774 PAS_IOB_DMA_RXCH_CFG_CNTTH(0));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600775
Olof Johanssona85b9422007-09-15 13:40:59 -0700776 write_iob_reg(mac, PAS_IOB_DMA_TXCH_CFG(mac->dma_txch),
777 PAS_IOB_DMA_TXCH_CFG_CNTTH(32));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600778
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500779 /* Clear out any residual packet count state from firmware */
780 pasemi_mac_restart_rx_intr(mac);
781 pasemi_mac_restart_tx_intr(mac);
782
Olof Johansson6dfa7522007-05-08 00:47:32 -0500783 /* 0xffffff is max value, about 16ms */
Olof Johanssona85b9422007-09-15 13:40:59 -0700784 write_iob_reg(mac, PAS_IOB_DMA_COM_TIMEOUTCFG,
785 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0xffffff));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600786
Olof Johanssona85b9422007-09-15 13:40:59 -0700787 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600788
789 ret = pasemi_mac_setup_rx_resources(dev);
790 if (ret)
791 goto out_rx_resources;
792
793 ret = pasemi_mac_setup_tx_resources(dev);
794 if (ret)
795 goto out_tx_resources;
796
Olof Johanssona85b9422007-09-15 13:40:59 -0700797 write_mac_reg(mac, PAS_MAC_IPC_CHNL,
798 PAS_MAC_IPC_CHNL_DCHNO(mac->dma_rxch) |
799 PAS_MAC_IPC_CHNL_BCH(mac->dma_rxch));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600800
801 /* enable rx if */
Olof Johanssona85b9422007-09-15 13:40:59 -0700802 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
803 PAS_DMA_RXINT_RCMDSTA_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600804
805 /* enable rx channel */
Olof Johanssona85b9422007-09-15 13:40:59 -0700806 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
807 PAS_DMA_RXCHAN_CCMDSTA_EN |
808 PAS_DMA_RXCHAN_CCMDSTA_DU);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600809
810 /* enable tx channel */
Olof Johanssona85b9422007-09-15 13:40:59 -0700811 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
812 PAS_DMA_TXCHAN_TCMDSTA_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600813
814 pasemi_mac_replenish_rx_ring(dev);
815
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500816 ret = pasemi_mac_phy_init(dev);
817 /* Some configs don't have PHYs (XAUI etc), so don't complain about
818 * failed init due to -ENODEV.
819 */
820 if (ret && ret != -ENODEV)
821 dev_warn(&mac->pdev->dev, "phy init failed: %d\n", ret);
822
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600823 netif_start_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700824 napi_enable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600825
Olof Johansson771f7402007-05-08 00:47:21 -0500826 /* Interrupts are a bit different for our DMA controller: While
827 * it's got one a regular PCI device header, the interrupt there
828 * is really the base of the range it's using. Each tx and rx
829 * channel has it's own interrupt source.
830 */
831
832 base_irq = virq_to_hw(mac->dma_pdev->irq);
833
834 mac->tx_irq = irq_create_mapping(NULL, base_irq + mac->dma_txch);
835 mac->rx_irq = irq_create_mapping(NULL, base_irq + 20 + mac->dma_txch);
836
837 ret = request_irq(mac->tx_irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600838 mac->tx->irq_name, dev);
839 if (ret) {
840 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500841 base_irq + mac->dma_txch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600842 goto out_tx_int;
843 }
844
Olof Johansson771f7402007-05-08 00:47:21 -0500845 ret = request_irq(mac->rx_irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600846 mac->rx->irq_name, dev);
847 if (ret) {
848 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500849 base_irq + 20 + mac->dma_rxch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600850 goto out_rx_int;
851 }
852
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500853 if (mac->phydev)
854 phy_start(mac->phydev);
855
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600856 return 0;
857
858out_rx_int:
Olof Johansson771f7402007-05-08 00:47:21 -0500859 free_irq(mac->tx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600860out_tx_int:
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700861 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600862 netif_stop_queue(dev);
863 pasemi_mac_free_tx_resources(dev);
864out_tx_resources:
865 pasemi_mac_free_rx_resources(dev);
866out_rx_resources:
867
868 return ret;
869}
870
871#define MAX_RETRIES 5000
872
873static int pasemi_mac_close(struct net_device *dev)
874{
875 struct pasemi_mac *mac = netdev_priv(dev);
876 unsigned int stat;
877 int retries;
878
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500879 if (mac->phydev) {
880 phy_stop(mac->phydev);
881 phy_disconnect(mac->phydev);
882 }
883
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600884 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700885 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600886
887 /* Clean out any pending buffers */
888 pasemi_mac_clean_tx(mac);
889 pasemi_mac_clean_rx(mac, RX_RING_SIZE);
890
891 /* Disable interface */
Olof Johanssona85b9422007-09-15 13:40:59 -0700892 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), PAS_DMA_TXCHAN_TCMDSTA_ST);
893 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), PAS_DMA_RXINT_RCMDSTA_ST);
894 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), PAS_DMA_RXCHAN_CCMDSTA_ST);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600895
896 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -0700897 stat = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
Olof Johansson0ce68c72007-04-28 15:36:40 -0500898 if (!(stat & PAS_DMA_TXCHAN_TCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600899 break;
900 cond_resched();
901 }
902
Olof Johansson0ce68c72007-04-28 15:36:40 -0500903 if (stat & PAS_DMA_TXCHAN_TCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600904 dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600905
906 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -0700907 stat = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
Olof Johansson0ce68c72007-04-28 15:36:40 -0500908 if (!(stat & PAS_DMA_RXCHAN_CCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600909 break;
910 cond_resched();
911 }
912
Olof Johansson0ce68c72007-04-28 15:36:40 -0500913 if (stat & PAS_DMA_RXCHAN_CCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600914 dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600915
916 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -0700917 stat = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
Olof Johansson0ce68c72007-04-28 15:36:40 -0500918 if (!(stat & PAS_DMA_RXINT_RCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600919 break;
920 cond_resched();
921 }
922
Olof Johansson0ce68c72007-04-28 15:36:40 -0500923 if (stat & PAS_DMA_RXINT_RCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600924 dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600925
926 /* Then, disable the channel. This must be done separately from
927 * stopping, since you can't disable when active.
928 */
929
Olof Johanssona85b9422007-09-15 13:40:59 -0700930 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), 0);
931 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), 0);
932 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600933
Olof Johansson771f7402007-05-08 00:47:21 -0500934 free_irq(mac->tx_irq, dev);
935 free_irq(mac->rx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600936
937 /* Free resources */
938 pasemi_mac_free_rx_resources(dev);
939 pasemi_mac_free_tx_resources(dev);
940
941 return 0;
942}
943
944static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
945{
946 struct pasemi_mac *mac = netdev_priv(dev);
947 struct pasemi_mac_txring *txring;
948 struct pasemi_mac_buffer *info;
949 struct pas_dma_xct_descr *dp;
950 u64 dflags;
951 dma_addr_t map;
952 int flags;
953
954 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_SS | XCT_MACTX_CRC_PAD;
955
956 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700957 const unsigned char *nh = skb_network_header(skb);
958
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700959 switch (ip_hdr(skb)->protocol) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600960 case IPPROTO_TCP:
961 dflags |= XCT_MACTX_CSUM_TCP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300962 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700963 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600964 break;
965 case IPPROTO_UDP:
966 dflags |= XCT_MACTX_CSUM_UDP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300967 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700968 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600969 break;
970 }
971 }
972
973 map = pci_map_single(mac->dma_pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
974
975 if (dma_mapping_error(map))
976 return NETDEV_TX_BUSY;
977
978 txring = mac->tx;
979
980 spin_lock_irqsave(&txring->lock, flags);
981
982 if (txring->next_to_clean - txring->next_to_use == TX_RING_SIZE) {
983 spin_unlock_irqrestore(&txring->lock, flags);
984 pasemi_mac_clean_tx(mac);
Olof Johansson52a94352007-05-12 18:01:09 -0500985 pasemi_mac_restart_tx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600986 spin_lock_irqsave(&txring->lock, flags);
987
988 if (txring->next_to_clean - txring->next_to_use ==
989 TX_RING_SIZE) {
990 /* Still no room -- stop the queue and wait for tx
991 * intr when there's room.
992 */
993 netif_stop_queue(dev);
994 goto out_err;
995 }
996 }
997
998
999 dp = &TX_DESC(mac, txring->next_to_use);
1000 info = &TX_DESC_INFO(mac, txring->next_to_use);
1001
1002 dp->mactx = dflags | XCT_MACTX_LLEN(skb->len);
1003 dp->ptr = XCT_PTR_LEN(skb->len) | XCT_PTR_ADDR(map);
1004 info->dma = map;
1005 info->skb = skb;
1006
1007 txring->next_to_use++;
1008 mac->stats.tx_packets++;
1009 mac->stats.tx_bytes += skb->len;
1010
1011 spin_unlock_irqrestore(&txring->lock, flags);
1012
Olof Johanssona85b9422007-09-15 13:40:59 -07001013 write_dma_reg(mac, PAS_DMA_TXCHAN_INCR(mac->dma_txch), 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001014
1015 return NETDEV_TX_OK;
1016
1017out_err:
1018 spin_unlock_irqrestore(&txring->lock, flags);
1019 pci_unmap_single(mac->dma_pdev, map, skb->len, PCI_DMA_TODEVICE);
1020 return NETDEV_TX_BUSY;
1021}
1022
1023static struct net_device_stats *pasemi_mac_get_stats(struct net_device *dev)
1024{
1025 struct pasemi_mac *mac = netdev_priv(dev);
1026
1027 return &mac->stats;
1028}
1029
Olof Johanssonceb51362007-05-08 00:47:49 -05001030
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001031static void pasemi_mac_set_rx_mode(struct net_device *dev)
1032{
1033 struct pasemi_mac *mac = netdev_priv(dev);
1034 unsigned int flags;
1035
Olof Johanssona85b9422007-09-15 13:40:59 -07001036 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001037
1038 /* Set promiscuous */
1039 if (dev->flags & IFF_PROMISC)
1040 flags |= PAS_MAC_CFG_PCFG_PR;
1041 else
1042 flags &= ~PAS_MAC_CFG_PCFG_PR;
1043
Olof Johanssona85b9422007-09-15 13:40:59 -07001044 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001045}
1046
1047
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001048static int pasemi_mac_poll(struct napi_struct *napi, int budget)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001049{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001050 struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
1051 struct net_device *dev = mac->netdev;
1052 int pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001053
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001054 pkts = pasemi_mac_clean_rx(mac, budget);
1055 if (pkts < budget) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001056 /* all done, no more packets present */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001057 netif_rx_complete(dev, napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001058
Olof Johansson1b0335ea2007-05-08 00:47:26 -05001059 pasemi_mac_restart_rx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001060 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001061 return pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001062}
1063
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001064static void __iomem * __devinit map_onedev(struct pci_dev *p, int index)
1065{
1066 struct device_node *dn;
1067 void __iomem *ret;
1068
1069 dn = pci_device_to_OF_node(p);
1070 if (!dn)
1071 goto fallback;
1072
1073 ret = of_iomap(dn, index);
1074 if (!ret)
1075 goto fallback;
1076
1077 return ret;
1078fallback:
1079 /* This is hardcoded and ugly, but we have some firmware versions
1080 * that don't provide the register space in the device tree. Luckily
1081 * they are at well-known locations so we can just do the math here.
1082 */
1083 return ioremap(0xe0000000 + (p->devfn << 12), 0x2000);
1084}
1085
1086static int __devinit pasemi_mac_map_regs(struct pasemi_mac *mac)
1087{
1088 struct resource res;
1089 struct device_node *dn;
1090 int err;
1091
1092 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
1093 if (!mac->dma_pdev) {
1094 dev_err(&mac->pdev->dev, "Can't find DMA Controller\n");
1095 return -ENODEV;
1096 }
1097
1098 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
1099 if (!mac->iob_pdev) {
1100 dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n");
1101 return -ENODEV;
1102 }
1103
1104 mac->regs = map_onedev(mac->pdev, 0);
1105 mac->dma_regs = map_onedev(mac->dma_pdev, 0);
1106 mac->iob_regs = map_onedev(mac->iob_pdev, 0);
1107
1108 if (!mac->regs || !mac->dma_regs || !mac->iob_regs) {
1109 dev_err(&mac->pdev->dev, "Can't map registers\n");
1110 return -ENODEV;
1111 }
1112
1113 /* The dma status structure is located in the I/O bridge, and
1114 * is cache coherent.
1115 */
1116 if (!dma_status) {
1117 dn = pci_device_to_OF_node(mac->iob_pdev);
1118 if (dn)
1119 err = of_address_to_resource(dn, 1, &res);
1120 if (!dn || err) {
1121 /* Fallback for old firmware */
1122 res.start = 0xfd800000;
1123 res.end = res.start + 0x1000;
1124 }
1125 dma_status = __ioremap(res.start, res.end-res.start, 0);
1126 }
1127
1128 return 0;
1129}
1130
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001131static int __devinit
1132pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1133{
1134 static int index = 0;
1135 struct net_device *dev;
1136 struct pasemi_mac *mac;
1137 int err;
1138
1139 err = pci_enable_device(pdev);
1140 if (err)
1141 return err;
1142
1143 dev = alloc_etherdev(sizeof(struct pasemi_mac));
1144 if (dev == NULL) {
1145 dev_err(&pdev->dev,
1146 "pasemi_mac: Could not allocate ethernet device.\n");
1147 err = -ENOMEM;
1148 goto out_disable_device;
1149 }
1150
1151 SET_MODULE_OWNER(dev);
1152 pci_set_drvdata(pdev, dev);
1153 SET_NETDEV_DEV(dev, &pdev->dev);
1154
1155 mac = netdev_priv(dev);
1156
1157 mac->pdev = pdev;
1158 mac->netdev = dev;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001159
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001160 netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
1161
1162 dev->features = NETIF_F_HW_CSUM;
1163
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001164 /* These should come out of the device tree eventually */
1165 mac->dma_txch = index;
1166 mac->dma_rxch = index;
1167
1168 /* We probe GMAC before XAUI, but the DMA interfaces are
1169 * in XAUI, GMAC order.
1170 */
1171 if (index < 4)
1172 mac->dma_if = index + 2;
1173 else
1174 mac->dma_if = index - 4;
1175 index++;
1176
1177 switch (pdev->device) {
1178 case 0xa005:
1179 mac->type = MAC_TYPE_GMAC;
1180 break;
1181 case 0xa006:
1182 mac->type = MAC_TYPE_XAUI;
1183 break;
1184 default:
1185 err = -ENODEV;
1186 goto out;
1187 }
1188
1189 /* get mac addr from device tree */
1190 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
1191 err = -ENODEV;
1192 goto out;
1193 }
1194 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
1195
1196 dev->open = pasemi_mac_open;
1197 dev->stop = pasemi_mac_close;
1198 dev->hard_start_xmit = pasemi_mac_start_tx;
1199 dev->get_stats = pasemi_mac_get_stats;
1200 dev->set_multicast_list = pasemi_mac_set_rx_mode;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001201
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001202 err = pasemi_mac_map_regs(mac);
1203 if (err)
1204 goto out;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001205
1206 mac->rx_status = &dma_status->rx_sta[mac->dma_rxch];
1207 mac->tx_status = &dma_status->tx_sta[mac->dma_txch];
1208
Olof Johanssonceb51362007-05-08 00:47:49 -05001209 mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1210
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001211 /* Enable most messages by default */
1212 mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1213
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001214 err = register_netdev(dev);
1215
1216 if (err) {
1217 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1218 err);
1219 goto out;
1220 } else
1221 printk(KERN_INFO "%s: PA Semi %s: intf %d, txch %d, rxch %d, "
1222 "hw addr %02x:%02x:%02x:%02x:%02x:%02x\n",
1223 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
1224 mac->dma_if, mac->dma_txch, mac->dma_rxch,
1225 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1226 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1227
1228 return err;
1229
1230out:
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001231 if (mac->iob_pdev)
1232 pci_dev_put(mac->iob_pdev);
1233 if (mac->dma_pdev)
1234 pci_dev_put(mac->dma_pdev);
1235 if (mac->dma_regs)
1236 iounmap(mac->dma_regs);
1237 if (mac->iob_regs)
1238 iounmap(mac->iob_regs);
1239 if (mac->regs)
1240 iounmap(mac->regs);
1241
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001242 free_netdev(dev);
1243out_disable_device:
1244 pci_disable_device(pdev);
1245 return err;
1246
1247}
1248
1249static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1250{
1251 struct net_device *netdev = pci_get_drvdata(pdev);
1252 struct pasemi_mac *mac;
1253
1254 if (!netdev)
1255 return;
1256
1257 mac = netdev_priv(netdev);
1258
1259 unregister_netdev(netdev);
1260
1261 pci_disable_device(pdev);
1262 pci_dev_put(mac->dma_pdev);
1263 pci_dev_put(mac->iob_pdev);
1264
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001265 iounmap(mac->regs);
1266 iounmap(mac->dma_regs);
1267 iounmap(mac->iob_regs);
1268
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001269 pci_set_drvdata(pdev, NULL);
1270 free_netdev(netdev);
1271}
1272
1273static struct pci_device_id pasemi_mac_pci_tbl[] = {
1274 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1275 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
olof@lixom.netfd178252007-05-12 14:57:36 -05001276 { },
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001277};
1278
1279MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1280
1281static struct pci_driver pasemi_mac_driver = {
1282 .name = "pasemi_mac",
1283 .id_table = pasemi_mac_pci_tbl,
1284 .probe = pasemi_mac_probe,
1285 .remove = __devexit_p(pasemi_mac_remove),
1286};
1287
1288static void __exit pasemi_mac_cleanup_module(void)
1289{
1290 pci_unregister_driver(&pasemi_mac_driver);
1291 __iounmap(dma_status);
1292 dma_status = NULL;
1293}
1294
1295int pasemi_mac_init_module(void)
1296{
1297 return pci_register_driver(&pasemi_mac_driver);
1298}
1299
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001300module_init(pasemi_mac_init_module);
1301module_exit(pasemi_mac_cleanup_module);