blob: c3a0fc64de0c34d7bb78170a7c6391793e88bb73 [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{
86 unsigned int val;
87
88 pci_read_config_dword(mac->iob_pdev, reg, &val);
89 return val;
90}
91
92static void write_iob_reg(struct pasemi_mac *mac, unsigned int reg,
93 unsigned int val)
94{
95 pci_write_config_dword(mac->iob_pdev, reg, val);
96}
97
98static unsigned int read_mac_reg(struct pasemi_mac *mac, unsigned int reg)
99{
100 unsigned int val;
101
102 pci_read_config_dword(mac->pdev, reg, &val);
103 return val;
104}
105
106static void write_mac_reg(struct pasemi_mac *mac, unsigned int reg,
107 unsigned int val)
108{
109 pci_write_config_dword(mac->pdev, reg, val);
110}
111
112static unsigned int read_dma_reg(struct pasemi_mac *mac, unsigned int reg)
113{
114 unsigned int val;
115
116 pci_read_config_dword(mac->dma_pdev, reg, &val);
117 return val;
118}
119
120static void write_dma_reg(struct pasemi_mac *mac, unsigned int reg,
121 unsigned int val)
122{
123 pci_write_config_dword(mac->dma_pdev, reg, val);
124}
125
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600126static int pasemi_get_mac_addr(struct pasemi_mac *mac)
127{
128 struct pci_dev *pdev = mac->pdev;
129 struct device_node *dn = pci_device_to_OF_node(pdev);
olof@lixom.net1af7f052007-05-12 14:57:46 -0500130 int len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600131 const u8 *maddr;
132 u8 addr[6];
133
134 if (!dn) {
135 dev_dbg(&pdev->dev,
136 "No device node for mac, not configuring\n");
137 return -ENOENT;
138 }
139
olof@lixom.net1af7f052007-05-12 14:57:46 -0500140 maddr = of_get_property(dn, "local-mac-address", &len);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500141
olof@lixom.net1af7f052007-05-12 14:57:46 -0500142 if (maddr && len == 6) {
143 memcpy(mac->mac_addr, maddr, 6);
144 return 0;
145 }
146
147 /* Some old versions of firmware mistakenly uses mac-address
148 * (and as a string) instead of a byte array in local-mac-address.
149 */
150
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500151 if (maddr == NULL)
Linus Torvalds90287802007-05-08 11:57:17 -0700152 maddr = of_get_property(dn, "mac-address", NULL);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500153
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600154 if (maddr == NULL) {
155 dev_warn(&pdev->dev,
156 "no mac address in device tree, not configuring\n");
157 return -ENOENT;
158 }
159
olof@lixom.net1af7f052007-05-12 14:57:46 -0500160
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600161 if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0],
162 &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) {
163 dev_warn(&pdev->dev,
164 "can't parse mac address, not configuring\n");
165 return -EINVAL;
166 }
167
olof@lixom.net1af7f052007-05-12 14:57:46 -0500168 memcpy(mac->mac_addr, addr, 6);
169
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600170 return 0;
171}
172
173static int pasemi_mac_setup_rx_resources(struct net_device *dev)
174{
175 struct pasemi_mac_rxring *ring;
176 struct pasemi_mac *mac = netdev_priv(dev);
177 int chan_id = mac->dma_rxch;
178
179 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
180
181 if (!ring)
182 goto out_ring;
183
184 spin_lock_init(&ring->lock);
185
186 ring->desc_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
187 RX_RING_SIZE, GFP_KERNEL);
188
189 if (!ring->desc_info)
190 goto out_desc_info;
191
192 /* Allocate descriptors */
193 ring->desc = dma_alloc_coherent(&mac->dma_pdev->dev,
194 RX_RING_SIZE *
195 sizeof(struct pas_dma_xct_descr),
196 &ring->dma, GFP_KERNEL);
197
198 if (!ring->desc)
199 goto out_desc;
200
201 memset(ring->desc, 0, RX_RING_SIZE * sizeof(struct pas_dma_xct_descr));
202
203 ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev,
204 RX_RING_SIZE * sizeof(u64),
205 &ring->buf_dma, GFP_KERNEL);
206 if (!ring->buffers)
207 goto out_buffers;
208
209 memset(ring->buffers, 0, RX_RING_SIZE * sizeof(u64));
210
Olof Johanssona85b9422007-09-15 13:40:59 -0700211 write_dma_reg(mac, PAS_DMA_RXCHAN_BASEL(chan_id), PAS_DMA_RXCHAN_BASEL_BRBL(ring->dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600212
Olof Johanssona85b9422007-09-15 13:40:59 -0700213 write_dma_reg(mac, PAS_DMA_RXCHAN_BASEU(chan_id),
214 PAS_DMA_RXCHAN_BASEU_BRBH(ring->dma >> 32) |
215 PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 2));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600216
Olof Johanssona85b9422007-09-15 13:40:59 -0700217 write_dma_reg(mac, PAS_DMA_RXCHAN_CFG(chan_id),
218 PAS_DMA_RXCHAN_CFG_HBU(1));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600219
Olof Johanssona85b9422007-09-15 13:40:59 -0700220 write_dma_reg(mac, PAS_DMA_RXINT_BASEL(mac->dma_if),
221 PAS_DMA_RXINT_BASEL_BRBL(__pa(ring->buffers)));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600222
Olof Johanssona85b9422007-09-15 13:40:59 -0700223 write_dma_reg(mac, PAS_DMA_RXINT_BASEU(mac->dma_if),
224 PAS_DMA_RXINT_BASEU_BRBH(__pa(ring->buffers) >> 32) |
225 PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600226
227 ring->next_to_fill = 0;
228 ring->next_to_clean = 0;
229
230 snprintf(ring->irq_name, sizeof(ring->irq_name),
231 "%s rx", dev->name);
232 mac->rx = ring;
233
234 return 0;
235
236out_buffers:
237 dma_free_coherent(&mac->dma_pdev->dev,
238 RX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
239 mac->rx->desc, mac->rx->dma);
240out_desc:
241 kfree(ring->desc_info);
242out_desc_info:
243 kfree(ring);
244out_ring:
245 return -ENOMEM;
246}
247
248
249static int pasemi_mac_setup_tx_resources(struct net_device *dev)
250{
251 struct pasemi_mac *mac = netdev_priv(dev);
252 u32 val;
253 int chan_id = mac->dma_txch;
254 struct pasemi_mac_txring *ring;
255
256 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
257 if (!ring)
258 goto out_ring;
259
260 spin_lock_init(&ring->lock);
261
262 ring->desc_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
263 TX_RING_SIZE, GFP_KERNEL);
264 if (!ring->desc_info)
265 goto out_desc_info;
266
267 /* Allocate descriptors */
268 ring->desc = dma_alloc_coherent(&mac->dma_pdev->dev,
269 TX_RING_SIZE *
270 sizeof(struct pas_dma_xct_descr),
271 &ring->dma, GFP_KERNEL);
272 if (!ring->desc)
273 goto out_desc;
274
275 memset(ring->desc, 0, TX_RING_SIZE * sizeof(struct pas_dma_xct_descr));
276
Olof Johanssona85b9422007-09-15 13:40:59 -0700277 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEL(chan_id),
278 PAS_DMA_TXCHAN_BASEL_BRBL(ring->dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600279 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->dma >> 32);
280 val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 2);
281
Olof Johanssona85b9422007-09-15 13:40:59 -0700282 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEU(chan_id), val);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600283
Olof Johanssona85b9422007-09-15 13:40:59 -0700284 write_dma_reg(mac, PAS_DMA_TXCHAN_CFG(chan_id),
285 PAS_DMA_TXCHAN_CFG_TY_IFACE |
286 PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
287 PAS_DMA_TXCHAN_CFG_UP |
288 PAS_DMA_TXCHAN_CFG_WT(2));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600289
290 ring->next_to_use = 0;
291 ring->next_to_clean = 0;
292
293 snprintf(ring->irq_name, sizeof(ring->irq_name),
294 "%s tx", dev->name);
295 mac->tx = ring;
296
297 return 0;
298
299out_desc:
300 kfree(ring->desc_info);
301out_desc_info:
302 kfree(ring);
303out_ring:
304 return -ENOMEM;
305}
306
307static void pasemi_mac_free_tx_resources(struct net_device *dev)
308{
309 struct pasemi_mac *mac = netdev_priv(dev);
310 unsigned int i;
311 struct pasemi_mac_buffer *info;
312 struct pas_dma_xct_descr *dp;
313
314 for (i = 0; i < TX_RING_SIZE; i++) {
315 info = &TX_DESC_INFO(mac, i);
316 dp = &TX_DESC(mac, i);
317 if (info->dma) {
318 if (info->skb) {
319 pci_unmap_single(mac->dma_pdev,
320 info->dma,
321 info->skb->len,
322 PCI_DMA_TODEVICE);
323 dev_kfree_skb_any(info->skb);
324 }
325 info->dma = 0;
326 info->skb = NULL;
327 dp->mactx = 0;
328 dp->ptr = 0;
329 }
330 }
331
332 dma_free_coherent(&mac->dma_pdev->dev,
333 TX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
334 mac->tx->desc, mac->tx->dma);
335
336 kfree(mac->tx->desc_info);
337 kfree(mac->tx);
338 mac->tx = NULL;
339}
340
341static void pasemi_mac_free_rx_resources(struct net_device *dev)
342{
343 struct pasemi_mac *mac = netdev_priv(dev);
344 unsigned int i;
345 struct pasemi_mac_buffer *info;
346 struct pas_dma_xct_descr *dp;
347
348 for (i = 0; i < RX_RING_SIZE; i++) {
349 info = &RX_DESC_INFO(mac, i);
350 dp = &RX_DESC(mac, i);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500351 if (info->skb) {
352 if (info->dma) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600353 pci_unmap_single(mac->dma_pdev,
354 info->dma,
355 info->skb->len,
356 PCI_DMA_FROMDEVICE);
357 dev_kfree_skb_any(info->skb);
358 }
359 info->dma = 0;
360 info->skb = NULL;
361 dp->macrx = 0;
362 dp->ptr = 0;
363 }
364 }
365
366 dma_free_coherent(&mac->dma_pdev->dev,
367 RX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
368 mac->rx->desc, mac->rx->dma);
369
370 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
371 mac->rx->buffers, mac->rx->buf_dma);
372
373 kfree(mac->rx->desc_info);
374 kfree(mac->rx);
375 mac->rx = NULL;
376}
377
378static void pasemi_mac_replenish_rx_ring(struct net_device *dev)
379{
380 struct pasemi_mac *mac = netdev_priv(dev);
381 unsigned int i;
382 int start = mac->rx->next_to_fill;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500383 unsigned int limit, count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600384
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500385 limit = (mac->rx->next_to_clean + RX_RING_SIZE -
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600386 mac->rx->next_to_fill) & (RX_RING_SIZE - 1);
387
388 /* Check to see if we're doing first-time setup */
389 if (unlikely(mac->rx->next_to_clean == 0 && mac->rx->next_to_fill == 0))
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500390 limit = RX_RING_SIZE;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600391
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500392 if (limit <= 0)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600393 return;
394
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500395 i = start;
396 for (count = limit; count; count--) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600397 struct pasemi_mac_buffer *info = &RX_DESC_INFO(mac, i);
398 u64 *buff = &RX_BUFF(mac, i);
399 struct sk_buff *skb;
400 dma_addr_t dma;
401
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500402 /* skb might still be in there for recycle on short receives */
403 if (info->skb)
404 skb = info->skb;
405 else
406 skb = dev_alloc_skb(BUF_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600407
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500408 if (unlikely(!skb))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600409 break;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600410
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600411 dma = pci_map_single(mac->dma_pdev, skb->data, skb->len,
412 PCI_DMA_FROMDEVICE);
413
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500414 if (unlikely(dma_mapping_error(dma))) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600415 dev_kfree_skb_irq(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600416 break;
417 }
418
419 info->skb = skb;
420 info->dma = dma;
421 *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500422 i++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600423 }
424
425 wmb();
426
Olof Johanssona85b9422007-09-15 13:40:59 -0700427 write_dma_reg(mac, PAS_DMA_RXCHAN_INCR(mac->dma_rxch), limit - count);
428 write_dma_reg(mac, PAS_DMA_RXINT_INCR(mac->dma_if), limit - count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600429
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500430 mac->rx->next_to_fill += limit - count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600431}
432
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500433static void pasemi_mac_restart_rx_intr(struct pasemi_mac *mac)
434{
Olof Johansson52a94352007-05-12 18:01:09 -0500435 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500436 /* Re-enable packet count interrupts: finally
437 * ack the packet count interrupt we got in rx_intr.
438 */
439
Olof Johansson52a94352007-05-12 18:01:09 -0500440 pcnt = *mac->rx_status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500441
Olof Johansson52a94352007-05-12 18:01:09 -0500442 reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500443
Olof Johanssona85b9422007-09-15 13:40:59 -0700444 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500445}
446
447static void pasemi_mac_restart_tx_intr(struct pasemi_mac *mac)
448{
Olof Johansson52a94352007-05-12 18:01:09 -0500449 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500450
451 /* Re-enable packet count interrupts */
Olof Johansson52a94352007-05-12 18:01:09 -0500452 pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500453
Olof Johansson52a94352007-05-12 18:01:09 -0500454 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500455
Olof Johanssona85b9422007-09-15 13:40:59 -0700456 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500457}
458
459
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600460static int pasemi_mac_clean_rx(struct pasemi_mac *mac, int limit)
461{
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500462 unsigned int n;
463 int count;
464 struct pas_dma_xct_descr *dp;
465 struct pasemi_mac_buffer *info;
466 struct sk_buff *skb;
467 unsigned int i, len;
468 u64 macrx;
469 dma_addr_t dma;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600470
471 spin_lock(&mac->rx->lock);
472
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500473 n = mac->rx->next_to_clean;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600474
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500475 for (count = limit; count; count--) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600476
477 rmb();
478
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500479 dp = &RX_DESC(mac, n);
480 macrx = dp->macrx;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600481
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500482 if (!(macrx & XCT_MACRX_O))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600483 break;
484
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600485
486 info = NULL;
487
488 /* We have to scan for our skb since there's no way
489 * to back-map them from the descriptor, and if we
490 * have several receive channels then they might not
491 * show up in the same order as they were put on the
492 * interface ring.
493 */
494
495 dma = (dp->ptr & XCT_PTR_ADDR_M);
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500496 for (i = n; i < (n + RX_RING_SIZE); i++) {
497 info = &RX_DESC_INFO(mac, i);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600498 if (info->dma == dma)
499 break;
500 }
501
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500502 skb = info->skb;
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500503 info->dma = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600504
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500505 pci_unmap_single(mac->dma_pdev, dma, skb->len,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600506 PCI_DMA_FROMDEVICE);
507
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500508 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600509
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500510 if (len < 256) {
511 struct sk_buff *new_skb =
512 netdev_alloc_skb(mac->netdev, len + NET_IP_ALIGN);
513 if (new_skb) {
514 skb_reserve(new_skb, NET_IP_ALIGN);
515 memcpy(new_skb->data - NET_IP_ALIGN,
516 skb->data - NET_IP_ALIGN,
517 len + NET_IP_ALIGN);
518 /* save the skb in buffer_info as good */
519 skb = new_skb;
520 }
521 /* else just continue with the old one */
522 } else
523 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600524
525 skb_put(skb, len);
526
527 skb->protocol = eth_type_trans(skb, mac->netdev);
528
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500529 if ((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600530 skb->ip_summed = CHECKSUM_COMPLETE;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500531 skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600532 XCT_MACRX_CSUM_S;
533 } else
534 skb->ip_summed = CHECKSUM_NONE;
535
536 mac->stats.rx_bytes += len;
537 mac->stats.rx_packets++;
538
539 netif_receive_skb(skb);
540
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600541 dp->ptr = 0;
542 dp->macrx = 0;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500543
544 n++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600545 }
546
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500547 mac->rx->next_to_clean += limit - count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600548 pasemi_mac_replenish_rx_ring(mac->netdev);
549
550 spin_unlock(&mac->rx->lock);
551
552 return count;
553}
554
555static int pasemi_mac_clean_tx(struct pasemi_mac *mac)
556{
557 int i;
558 struct pasemi_mac_buffer *info;
559 struct pas_dma_xct_descr *dp;
560 int start, count;
561 int flags;
562
563 spin_lock_irqsave(&mac->tx->lock, flags);
564
565 start = mac->tx->next_to_clean;
566 count = 0;
567
568 for (i = start; i < mac->tx->next_to_use; i++) {
569 dp = &TX_DESC(mac, i);
570 if (!dp || (dp->mactx & XCT_MACTX_O))
571 break;
572
573 count++;
574
575 info = &TX_DESC_INFO(mac, i);
576
577 pci_unmap_single(mac->dma_pdev, info->dma,
578 info->skb->len, PCI_DMA_TODEVICE);
579 dev_kfree_skb_irq(info->skb);
580
581 info->skb = NULL;
582 info->dma = 0;
583 dp->mactx = 0;
584 dp->ptr = 0;
585 }
586 mac->tx->next_to_clean += count;
587 spin_unlock_irqrestore(&mac->tx->lock, flags);
588
Olof Johansson0ce68c72007-04-28 15:36:40 -0500589 netif_wake_queue(mac->netdev);
590
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600591 return count;
592}
593
594
595static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
596{
597 struct net_device *dev = data;
598 struct pasemi_mac *mac = netdev_priv(dev);
599 unsigned int reg;
600
Olof Johansson6dfa7522007-05-08 00:47:32 -0500601 if (!(*mac->rx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600602 return IRQ_NONE;
603
Olof Johansson6dfa7522007-05-08 00:47:32 -0500604 if (*mac->rx_status & PAS_STATUS_ERROR)
605 printk("rx_status reported error\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600606
Olof Johansson6dfa7522007-05-08 00:47:32 -0500607 /* Don't reset packet count so it won't fire again but clear
608 * all others.
609 */
610
Olof Johansson6dfa7522007-05-08 00:47:32 -0500611 reg = 0;
612 if (*mac->rx_status & PAS_STATUS_SOFT)
613 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
614 if (*mac->rx_status & PAS_STATUS_ERROR)
615 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600616 if (*mac->rx_status & PAS_STATUS_TIMER)
617 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
618
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700619 netif_rx_schedule(dev, &mac->napi);
Olof Johansson6dfa7522007-05-08 00:47:32 -0500620
Olof Johanssona85b9422007-09-15 13:40:59 -0700621 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600622
623 return IRQ_HANDLED;
624}
625
626static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
627{
628 struct net_device *dev = data;
629 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson52a94352007-05-12 18:01:09 -0500630 unsigned int reg, pcnt;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600631
Olof Johansson6dfa7522007-05-08 00:47:32 -0500632 if (!(*mac->tx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600633 return IRQ_NONE;
634
635 pasemi_mac_clean_tx(mac);
636
Olof Johansson52a94352007-05-12 18:01:09 -0500637 pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
638
639 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson6dfa7522007-05-08 00:47:32 -0500640
641 if (*mac->tx_status & PAS_STATUS_SOFT)
642 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
643 if (*mac->tx_status & PAS_STATUS_ERROR)
644 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600645
Olof Johanssona85b9422007-09-15 13:40:59 -0700646 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600647
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600648 return IRQ_HANDLED;
649}
650
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500651static void pasemi_adjust_link(struct net_device *dev)
652{
653 struct pasemi_mac *mac = netdev_priv(dev);
654 int msg;
655 unsigned int flags;
656 unsigned int new_flags;
657
658 if (!mac->phydev->link) {
659 /* If no link, MAC speed settings don't matter. Just report
660 * link down and return.
661 */
662 if (mac->link && netif_msg_link(mac))
663 printk(KERN_INFO "%s: Link is down.\n", dev->name);
664
665 netif_carrier_off(dev);
666 mac->link = 0;
667
668 return;
669 } else
670 netif_carrier_on(dev);
671
Olof Johanssona85b9422007-09-15 13:40:59 -0700672 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500673 new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
674 PAS_MAC_CFG_PCFG_TSR_M);
675
676 if (!mac->phydev->duplex)
677 new_flags |= PAS_MAC_CFG_PCFG_HD;
678
679 switch (mac->phydev->speed) {
680 case 1000:
681 new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
682 PAS_MAC_CFG_PCFG_TSR_1G;
683 break;
684 case 100:
685 new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
686 PAS_MAC_CFG_PCFG_TSR_100M;
687 break;
688 case 10:
689 new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
690 PAS_MAC_CFG_PCFG_TSR_10M;
691 break;
692 default:
693 printk("Unsupported speed %d\n", mac->phydev->speed);
694 }
695
696 /* Print on link or speed/duplex change */
697 msg = mac->link != mac->phydev->link || flags != new_flags;
698
699 mac->duplex = mac->phydev->duplex;
700 mac->speed = mac->phydev->speed;
701 mac->link = mac->phydev->link;
702
703 if (new_flags != flags)
Olof Johanssona85b9422007-09-15 13:40:59 -0700704 write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500705
706 if (msg && netif_msg_link(mac))
707 printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
708 dev->name, mac->speed, mac->duplex ? "full" : "half");
709}
710
711static int pasemi_mac_phy_init(struct net_device *dev)
712{
713 struct pasemi_mac *mac = netdev_priv(dev);
714 struct device_node *dn, *phy_dn;
715 struct phy_device *phydev;
716 unsigned int phy_id;
717 const phandle *ph;
718 const unsigned int *prop;
719 struct resource r;
720 int ret;
721
722 dn = pci_device_to_OF_node(mac->pdev);
Linus Torvalds90287802007-05-08 11:57:17 -0700723 ph = of_get_property(dn, "phy-handle", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500724 if (!ph)
725 return -ENODEV;
726 phy_dn = of_find_node_by_phandle(*ph);
727
Linus Torvalds90287802007-05-08 11:57:17 -0700728 prop = of_get_property(phy_dn, "reg", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500729 ret = of_address_to_resource(phy_dn->parent, 0, &r);
730 if (ret)
731 goto err;
732
733 phy_id = *prop;
734 snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);
735
736 of_node_put(phy_dn);
737
738 mac->link = 0;
739 mac->speed = 0;
740 mac->duplex = -1;
741
742 phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);
743
744 if (IS_ERR(phydev)) {
745 printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
746 return PTR_ERR(phydev);
747 }
748
749 mac->phydev = phydev;
750
751 return 0;
752
753err:
754 of_node_put(phy_dn);
755 return -ENODEV;
756}
757
758
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600759static int pasemi_mac_open(struct net_device *dev)
760{
761 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson771f7402007-05-08 00:47:21 -0500762 int base_irq;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600763 unsigned int flags;
764 int ret;
765
766 /* enable rx section */
Olof Johanssona85b9422007-09-15 13:40:59 -0700767 write_dma_reg(mac, PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600768
769 /* enable tx section */
Olof Johanssona85b9422007-09-15 13:40:59 -0700770 write_dma_reg(mac, PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600771
772 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
773 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
774 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
775
Olof Johanssona85b9422007-09-15 13:40:59 -0700776 write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600777
778 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
779 PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
780
781 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
782
Olof Johanssona85b9422007-09-15 13:40:59 -0700783 write_iob_reg(mac, PAS_IOB_DMA_RXCH_CFG(mac->dma_rxch),
784 PAS_IOB_DMA_RXCH_CFG_CNTTH(0));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600785
Olof Johanssona85b9422007-09-15 13:40:59 -0700786 write_iob_reg(mac, PAS_IOB_DMA_TXCH_CFG(mac->dma_txch),
787 PAS_IOB_DMA_TXCH_CFG_CNTTH(32));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600788
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500789 /* Clear out any residual packet count state from firmware */
790 pasemi_mac_restart_rx_intr(mac);
791 pasemi_mac_restart_tx_intr(mac);
792
Olof Johansson6dfa7522007-05-08 00:47:32 -0500793 /* 0xffffff is max value, about 16ms */
Olof Johanssona85b9422007-09-15 13:40:59 -0700794 write_iob_reg(mac, PAS_IOB_DMA_COM_TIMEOUTCFG,
795 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0xffffff));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600796
Olof Johanssona85b9422007-09-15 13:40:59 -0700797 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600798
799 ret = pasemi_mac_setup_rx_resources(dev);
800 if (ret)
801 goto out_rx_resources;
802
803 ret = pasemi_mac_setup_tx_resources(dev);
804 if (ret)
805 goto out_tx_resources;
806
Olof Johanssona85b9422007-09-15 13:40:59 -0700807 write_mac_reg(mac, PAS_MAC_IPC_CHNL,
808 PAS_MAC_IPC_CHNL_DCHNO(mac->dma_rxch) |
809 PAS_MAC_IPC_CHNL_BCH(mac->dma_rxch));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600810
811 /* enable rx if */
Olof Johanssona85b9422007-09-15 13:40:59 -0700812 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
813 PAS_DMA_RXINT_RCMDSTA_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600814
815 /* enable rx channel */
Olof Johanssona85b9422007-09-15 13:40:59 -0700816 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
817 PAS_DMA_RXCHAN_CCMDSTA_EN |
818 PAS_DMA_RXCHAN_CCMDSTA_DU);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600819
820 /* enable tx channel */
Olof Johanssona85b9422007-09-15 13:40:59 -0700821 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
822 PAS_DMA_TXCHAN_TCMDSTA_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600823
824 pasemi_mac_replenish_rx_ring(dev);
825
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500826 ret = pasemi_mac_phy_init(dev);
827 /* Some configs don't have PHYs (XAUI etc), so don't complain about
828 * failed init due to -ENODEV.
829 */
830 if (ret && ret != -ENODEV)
831 dev_warn(&mac->pdev->dev, "phy init failed: %d\n", ret);
832
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600833 netif_start_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700834 napi_enable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600835
Olof Johansson771f7402007-05-08 00:47:21 -0500836 /* Interrupts are a bit different for our DMA controller: While
837 * it's got one a regular PCI device header, the interrupt there
838 * is really the base of the range it's using. Each tx and rx
839 * channel has it's own interrupt source.
840 */
841
842 base_irq = virq_to_hw(mac->dma_pdev->irq);
843
844 mac->tx_irq = irq_create_mapping(NULL, base_irq + mac->dma_txch);
845 mac->rx_irq = irq_create_mapping(NULL, base_irq + 20 + mac->dma_txch);
846
847 ret = request_irq(mac->tx_irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600848 mac->tx->irq_name, dev);
849 if (ret) {
850 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500851 base_irq + mac->dma_txch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600852 goto out_tx_int;
853 }
854
Olof Johansson771f7402007-05-08 00:47:21 -0500855 ret = request_irq(mac->rx_irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600856 mac->rx->irq_name, dev);
857 if (ret) {
858 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500859 base_irq + 20 + mac->dma_rxch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600860 goto out_rx_int;
861 }
862
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500863 if (mac->phydev)
864 phy_start(mac->phydev);
865
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600866 return 0;
867
868out_rx_int:
Olof Johansson771f7402007-05-08 00:47:21 -0500869 free_irq(mac->tx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600870out_tx_int:
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700871 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600872 netif_stop_queue(dev);
873 pasemi_mac_free_tx_resources(dev);
874out_tx_resources:
875 pasemi_mac_free_rx_resources(dev);
876out_rx_resources:
877
878 return ret;
879}
880
881#define MAX_RETRIES 5000
882
883static int pasemi_mac_close(struct net_device *dev)
884{
885 struct pasemi_mac *mac = netdev_priv(dev);
886 unsigned int stat;
887 int retries;
888
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500889 if (mac->phydev) {
890 phy_stop(mac->phydev);
891 phy_disconnect(mac->phydev);
892 }
893
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600894 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700895 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600896
897 /* Clean out any pending buffers */
898 pasemi_mac_clean_tx(mac);
899 pasemi_mac_clean_rx(mac, RX_RING_SIZE);
900
901 /* Disable interface */
Olof Johanssona85b9422007-09-15 13:40:59 -0700902 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), PAS_DMA_TXCHAN_TCMDSTA_ST);
903 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), PAS_DMA_RXINT_RCMDSTA_ST);
904 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), PAS_DMA_RXCHAN_CCMDSTA_ST);
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_TXCHAN_TCMDSTA(mac->dma_txch));
Olof Johansson0ce68c72007-04-28 15:36:40 -0500908 if (!(stat & PAS_DMA_TXCHAN_TCMDSTA_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_TXCHAN_TCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600914 dev_err(&mac->dma_pdev->dev, "Failed to stop tx 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_RXCHAN_CCMDSTA(mac->dma_rxch));
Olof Johansson0ce68c72007-04-28 15:36:40 -0500918 if (!(stat & PAS_DMA_RXCHAN_CCMDSTA_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_RXCHAN_CCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600924 dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600925
926 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -0700927 stat = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
Olof Johansson0ce68c72007-04-28 15:36:40 -0500928 if (!(stat & PAS_DMA_RXINT_RCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600929 break;
930 cond_resched();
931 }
932
Olof Johansson0ce68c72007-04-28 15:36:40 -0500933 if (stat & PAS_DMA_RXINT_RCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600934 dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600935
936 /* Then, disable the channel. This must be done separately from
937 * stopping, since you can't disable when active.
938 */
939
Olof Johanssona85b9422007-09-15 13:40:59 -0700940 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), 0);
941 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), 0);
942 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600943
Olof Johansson771f7402007-05-08 00:47:21 -0500944 free_irq(mac->tx_irq, dev);
945 free_irq(mac->rx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600946
947 /* Free resources */
948 pasemi_mac_free_rx_resources(dev);
949 pasemi_mac_free_tx_resources(dev);
950
951 return 0;
952}
953
954static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
955{
956 struct pasemi_mac *mac = netdev_priv(dev);
957 struct pasemi_mac_txring *txring;
958 struct pasemi_mac_buffer *info;
959 struct pas_dma_xct_descr *dp;
960 u64 dflags;
961 dma_addr_t map;
962 int flags;
963
964 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_SS | XCT_MACTX_CRC_PAD;
965
966 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700967 const unsigned char *nh = skb_network_header(skb);
968
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700969 switch (ip_hdr(skb)->protocol) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600970 case IPPROTO_TCP:
971 dflags |= XCT_MACTX_CSUM_TCP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300972 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700973 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600974 break;
975 case IPPROTO_UDP:
976 dflags |= XCT_MACTX_CSUM_UDP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300977 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700978 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600979 break;
980 }
981 }
982
983 map = pci_map_single(mac->dma_pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
984
985 if (dma_mapping_error(map))
986 return NETDEV_TX_BUSY;
987
988 txring = mac->tx;
989
990 spin_lock_irqsave(&txring->lock, flags);
991
992 if (txring->next_to_clean - txring->next_to_use == TX_RING_SIZE) {
993 spin_unlock_irqrestore(&txring->lock, flags);
994 pasemi_mac_clean_tx(mac);
Olof Johansson52a94352007-05-12 18:01:09 -0500995 pasemi_mac_restart_tx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600996 spin_lock_irqsave(&txring->lock, flags);
997
998 if (txring->next_to_clean - txring->next_to_use ==
999 TX_RING_SIZE) {
1000 /* Still no room -- stop the queue and wait for tx
1001 * intr when there's room.
1002 */
1003 netif_stop_queue(dev);
1004 goto out_err;
1005 }
1006 }
1007
1008
1009 dp = &TX_DESC(mac, txring->next_to_use);
1010 info = &TX_DESC_INFO(mac, txring->next_to_use);
1011
1012 dp->mactx = dflags | XCT_MACTX_LLEN(skb->len);
1013 dp->ptr = XCT_PTR_LEN(skb->len) | XCT_PTR_ADDR(map);
1014 info->dma = map;
1015 info->skb = skb;
1016
1017 txring->next_to_use++;
1018 mac->stats.tx_packets++;
1019 mac->stats.tx_bytes += skb->len;
1020
1021 spin_unlock_irqrestore(&txring->lock, flags);
1022
Olof Johanssona85b9422007-09-15 13:40:59 -07001023 write_dma_reg(mac, PAS_DMA_TXCHAN_INCR(mac->dma_txch), 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001024
1025 return NETDEV_TX_OK;
1026
1027out_err:
1028 spin_unlock_irqrestore(&txring->lock, flags);
1029 pci_unmap_single(mac->dma_pdev, map, skb->len, PCI_DMA_TODEVICE);
1030 return NETDEV_TX_BUSY;
1031}
1032
1033static struct net_device_stats *pasemi_mac_get_stats(struct net_device *dev)
1034{
1035 struct pasemi_mac *mac = netdev_priv(dev);
1036
1037 return &mac->stats;
1038}
1039
Olof Johanssonceb51362007-05-08 00:47:49 -05001040
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001041static void pasemi_mac_set_rx_mode(struct net_device *dev)
1042{
1043 struct pasemi_mac *mac = netdev_priv(dev);
1044 unsigned int flags;
1045
Olof Johanssona85b9422007-09-15 13:40:59 -07001046 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001047
1048 /* Set promiscuous */
1049 if (dev->flags & IFF_PROMISC)
1050 flags |= PAS_MAC_CFG_PCFG_PR;
1051 else
1052 flags &= ~PAS_MAC_CFG_PCFG_PR;
1053
Olof Johanssona85b9422007-09-15 13:40:59 -07001054 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001055}
1056
1057
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001058static int pasemi_mac_poll(struct napi_struct *napi, int budget)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001059{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001060 struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
1061 struct net_device *dev = mac->netdev;
1062 int pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001063
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001064 pkts = pasemi_mac_clean_rx(mac, budget);
1065 if (pkts < budget) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001066 /* all done, no more packets present */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001067 netif_rx_complete(dev, napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001068
Olof Johansson1b0335ea2007-05-08 00:47:26 -05001069 pasemi_mac_restart_rx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001070 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001071 return pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001072}
1073
1074static int __devinit
1075pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1076{
1077 static int index = 0;
1078 struct net_device *dev;
1079 struct pasemi_mac *mac;
1080 int err;
1081
1082 err = pci_enable_device(pdev);
1083 if (err)
1084 return err;
1085
1086 dev = alloc_etherdev(sizeof(struct pasemi_mac));
1087 if (dev == NULL) {
1088 dev_err(&pdev->dev,
1089 "pasemi_mac: Could not allocate ethernet device.\n");
1090 err = -ENOMEM;
1091 goto out_disable_device;
1092 }
1093
1094 SET_MODULE_OWNER(dev);
1095 pci_set_drvdata(pdev, dev);
1096 SET_NETDEV_DEV(dev, &pdev->dev);
1097
1098 mac = netdev_priv(dev);
1099
1100 mac->pdev = pdev;
1101 mac->netdev = dev;
1102 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
1103
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001104 netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
1105
1106 dev->features = NETIF_F_HW_CSUM;
1107
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001108 if (!mac->dma_pdev) {
1109 dev_err(&pdev->dev, "Can't find DMA Controller\n");
1110 err = -ENODEV;
1111 goto out_free_netdev;
1112 }
1113
1114 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
1115
1116 if (!mac->iob_pdev) {
1117 dev_err(&pdev->dev, "Can't find I/O Bridge\n");
1118 err = -ENODEV;
1119 goto out_put_dma_pdev;
1120 }
1121
1122 /* These should come out of the device tree eventually */
1123 mac->dma_txch = index;
1124 mac->dma_rxch = index;
1125
1126 /* We probe GMAC before XAUI, but the DMA interfaces are
1127 * in XAUI, GMAC order.
1128 */
1129 if (index < 4)
1130 mac->dma_if = index + 2;
1131 else
1132 mac->dma_if = index - 4;
1133 index++;
1134
1135 switch (pdev->device) {
1136 case 0xa005:
1137 mac->type = MAC_TYPE_GMAC;
1138 break;
1139 case 0xa006:
1140 mac->type = MAC_TYPE_XAUI;
1141 break;
1142 default:
1143 err = -ENODEV;
1144 goto out;
1145 }
1146
1147 /* get mac addr from device tree */
1148 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
1149 err = -ENODEV;
1150 goto out;
1151 }
1152 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
1153
1154 dev->open = pasemi_mac_open;
1155 dev->stop = pasemi_mac_close;
1156 dev->hard_start_xmit = pasemi_mac_start_tx;
1157 dev->get_stats = pasemi_mac_get_stats;
1158 dev->set_multicast_list = pasemi_mac_set_rx_mode;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001159
1160 /* The dma status structure is located in the I/O bridge, and
1161 * is cache coherent.
1162 */
1163 if (!dma_status)
1164 /* XXXOJN This should come from the device tree */
1165 dma_status = __ioremap(0xfd800000, 0x1000, 0);
1166
1167 mac->rx_status = &dma_status->rx_sta[mac->dma_rxch];
1168 mac->tx_status = &dma_status->tx_sta[mac->dma_txch];
1169
Olof Johanssonceb51362007-05-08 00:47:49 -05001170 mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1171
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001172 /* Enable most messages by default */
1173 mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1174
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001175 err = register_netdev(dev);
1176
1177 if (err) {
1178 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1179 err);
1180 goto out;
1181 } else
1182 printk(KERN_INFO "%s: PA Semi %s: intf %d, txch %d, rxch %d, "
1183 "hw addr %02x:%02x:%02x:%02x:%02x:%02x\n",
1184 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
1185 mac->dma_if, mac->dma_txch, mac->dma_rxch,
1186 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1187 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1188
1189 return err;
1190
1191out:
1192 pci_dev_put(mac->iob_pdev);
1193out_put_dma_pdev:
1194 pci_dev_put(mac->dma_pdev);
1195out_free_netdev:
1196 free_netdev(dev);
1197out_disable_device:
1198 pci_disable_device(pdev);
1199 return err;
1200
1201}
1202
1203static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1204{
1205 struct net_device *netdev = pci_get_drvdata(pdev);
1206 struct pasemi_mac *mac;
1207
1208 if (!netdev)
1209 return;
1210
1211 mac = netdev_priv(netdev);
1212
1213 unregister_netdev(netdev);
1214
1215 pci_disable_device(pdev);
1216 pci_dev_put(mac->dma_pdev);
1217 pci_dev_put(mac->iob_pdev);
1218
1219 pci_set_drvdata(pdev, NULL);
1220 free_netdev(netdev);
1221}
1222
1223static struct pci_device_id pasemi_mac_pci_tbl[] = {
1224 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1225 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
olof@lixom.netfd178252007-05-12 14:57:36 -05001226 { },
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001227};
1228
1229MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1230
1231static struct pci_driver pasemi_mac_driver = {
1232 .name = "pasemi_mac",
1233 .id_table = pasemi_mac_pci_tbl,
1234 .probe = pasemi_mac_probe,
1235 .remove = __devexit_p(pasemi_mac_remove),
1236};
1237
1238static void __exit pasemi_mac_cleanup_module(void)
1239{
1240 pci_unregister_driver(&pasemi_mac_driver);
1241 __iounmap(dma_status);
1242 dma_status = NULL;
1243}
1244
1245int pasemi_mac_init_module(void)
1246{
1247 return pci_register_driver(&pasemi_mac_driver);
1248}
1249
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001250module_init(pasemi_mac_init_module);
1251module_exit(pasemi_mac_cleanup_module);