blob: c538c66d4b4ff25bd6f5269ad1d847d06dc9a049 [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 Johanssonfc9e4d22007-10-02 16:25:53 -050066#define TX_RING(mac, num) ((mac)->tx->ring[(num) & (TX_RING_SIZE-1)])
67#define TX_RING_INFO(mac, num) ((mac)->tx->ring_info[(num) & (TX_RING_SIZE-1)])
68#define RX_RING(mac, num) ((mac)->rx->ring[(num) & (RX_RING_SIZE-1)])
69#define RX_RING_INFO(mac, num) ((mac)->rx->ring_info[(num) & (RX_RING_SIZE-1)])
Olof Johanssonf5cd7872007-01-31 21:43:54 -060070#define RX_BUFF(mac, num) ((mac)->rx->buffers[(num) & (RX_RING_SIZE-1)])
71
Olof Johansson021fa222007-08-22 09:13:11 -050072#define RING_USED(ring) (((ring)->next_to_fill - (ring)->next_to_clean) \
73 & ((ring)->size - 1))
74#define RING_AVAIL(ring) ((ring->size) - RING_USED(ring))
75
Olof Johanssonf5cd7872007-01-31 21:43:54 -060076#define BUF_SIZE 1646 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
77
Olof Johanssonceb51362007-05-08 00:47:49 -050078MODULE_LICENSE("GPL");
79MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
80MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
81
82static int debug = -1; /* -1 == use DEFAULT_MSG_ENABLE as value */
83module_param(debug, int, 0);
84MODULE_PARM_DESC(debug, "PA Semi MAC bitmapped debugging message enable value");
Olof Johanssonf5cd7872007-01-31 21:43:54 -060085
86static struct pasdma_status *dma_status;
87
Olof Johanssona85b9422007-09-15 13:40:59 -070088static void write_iob_reg(struct pasemi_mac *mac, unsigned int reg,
89 unsigned int val)
90{
Olof Johanssonb6e05a12007-09-15 13:44:07 -070091 out_le32(mac->iob_regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -070092}
93
94static unsigned int read_mac_reg(struct pasemi_mac *mac, unsigned int reg)
95{
Olof Johanssonb6e05a12007-09-15 13:44:07 -070096 return in_le32(mac->regs+reg);
Olof Johanssona85b9422007-09-15 13:40:59 -070097}
98
99static void write_mac_reg(struct pasemi_mac *mac, unsigned int reg,
100 unsigned int val)
101{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700102 out_le32(mac->regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700103}
104
105static unsigned int read_dma_reg(struct pasemi_mac *mac, unsigned int reg)
106{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700107 return in_le32(mac->dma_regs+reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700108}
109
110static void write_dma_reg(struct pasemi_mac *mac, unsigned int reg,
111 unsigned int val)
112{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700113 out_le32(mac->dma_regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700114}
115
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600116static int pasemi_get_mac_addr(struct pasemi_mac *mac)
117{
118 struct pci_dev *pdev = mac->pdev;
119 struct device_node *dn = pci_device_to_OF_node(pdev);
olof@lixom.net1af7f052007-05-12 14:57:46 -0500120 int len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600121 const u8 *maddr;
122 u8 addr[6];
123
124 if (!dn) {
125 dev_dbg(&pdev->dev,
126 "No device node for mac, not configuring\n");
127 return -ENOENT;
128 }
129
olof@lixom.net1af7f052007-05-12 14:57:46 -0500130 maddr = of_get_property(dn, "local-mac-address", &len);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500131
olof@lixom.net1af7f052007-05-12 14:57:46 -0500132 if (maddr && len == 6) {
133 memcpy(mac->mac_addr, maddr, 6);
134 return 0;
135 }
136
137 /* Some old versions of firmware mistakenly uses mac-address
138 * (and as a string) instead of a byte array in local-mac-address.
139 */
140
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500141 if (maddr == NULL)
Linus Torvalds90287802007-05-08 11:57:17 -0700142 maddr = of_get_property(dn, "mac-address", NULL);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500143
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600144 if (maddr == NULL) {
145 dev_warn(&pdev->dev,
146 "no mac address in device tree, not configuring\n");
147 return -ENOENT;
148 }
149
olof@lixom.net1af7f052007-05-12 14:57:46 -0500150
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600151 if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0],
152 &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) {
153 dev_warn(&pdev->dev,
154 "can't parse mac address, not configuring\n");
155 return -EINVAL;
156 }
157
olof@lixom.net1af7f052007-05-12 14:57:46 -0500158 memcpy(mac->mac_addr, addr, 6);
159
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600160 return 0;
161}
162
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500163static int pasemi_mac_unmap_tx_skb(struct pasemi_mac *mac,
164 struct sk_buff *skb,
165 dma_addr_t *dmas)
166{
167 int f;
168 int nfrags = skb_shinfo(skb)->nr_frags;
169
170 pci_unmap_single(mac->dma_pdev, dmas[0], skb_headlen(skb),
171 PCI_DMA_TODEVICE);
172
173 for (f = 0; f < nfrags; f++) {
174 skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
175
176 pci_unmap_page(mac->dma_pdev, dmas[f+1], frag->size,
177 PCI_DMA_TODEVICE);
178 }
179 dev_kfree_skb_irq(skb);
180
181 /* Freed descriptor slot + main SKB ptr + nfrags additional ptrs,
182 * aligned up to a power of 2
183 */
184 return (nfrags + 3) & ~1;
185}
186
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600187static int pasemi_mac_setup_rx_resources(struct net_device *dev)
188{
189 struct pasemi_mac_rxring *ring;
190 struct pasemi_mac *mac = netdev_priv(dev);
191 int chan_id = mac->dma_rxch;
192
193 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
194
195 if (!ring)
196 goto out_ring;
197
198 spin_lock_init(&ring->lock);
199
Olof Johansson021fa222007-08-22 09:13:11 -0500200 ring->size = RX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500201 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600202 RX_RING_SIZE, GFP_KERNEL);
203
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500204 if (!ring->ring_info)
205 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600206
207 /* Allocate descriptors */
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500208 ring->ring = dma_alloc_coherent(&mac->dma_pdev->dev,
209 RX_RING_SIZE * sizeof(u64),
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600210 &ring->dma, GFP_KERNEL);
211
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500212 if (!ring->ring)
213 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600214
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500215 memset(ring->ring, 0, RX_RING_SIZE * sizeof(u64));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600216
217 ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev,
218 RX_RING_SIZE * sizeof(u64),
219 &ring->buf_dma, GFP_KERNEL);
220 if (!ring->buffers)
221 goto out_buffers;
222
223 memset(ring->buffers, 0, RX_RING_SIZE * sizeof(u64));
224
Olof Johanssona85b9422007-09-15 13:40:59 -0700225 write_dma_reg(mac, PAS_DMA_RXCHAN_BASEL(chan_id), PAS_DMA_RXCHAN_BASEL_BRBL(ring->dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600226
Olof Johanssona85b9422007-09-15 13:40:59 -0700227 write_dma_reg(mac, PAS_DMA_RXCHAN_BASEU(chan_id),
228 PAS_DMA_RXCHAN_BASEU_BRBH(ring->dma >> 32) |
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500229 PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600230
Olof Johanssona85b9422007-09-15 13:40:59 -0700231 write_dma_reg(mac, PAS_DMA_RXCHAN_CFG(chan_id),
Olof Johanssonc0efd522007-08-22 09:12:52 -0500232 PAS_DMA_RXCHAN_CFG_HBU(2));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600233
Olof Johanssona85b9422007-09-15 13:40:59 -0700234 write_dma_reg(mac, PAS_DMA_RXINT_BASEL(mac->dma_if),
235 PAS_DMA_RXINT_BASEL_BRBL(__pa(ring->buffers)));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600236
Olof Johanssona85b9422007-09-15 13:40:59 -0700237 write_dma_reg(mac, PAS_DMA_RXINT_BASEU(mac->dma_if),
238 PAS_DMA_RXINT_BASEU_BRBH(__pa(ring->buffers) >> 32) |
239 PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600240
Olof Johanssonc0efd522007-08-22 09:12:52 -0500241 write_dma_reg(mac, PAS_DMA_RXINT_CFG(mac->dma_if),
Olof Johansson9a50beb2007-10-02 16:26:30 -0500242 PAS_DMA_RXINT_CFG_DHL(3) |
243 PAS_DMA_RXINT_CFG_L2 |
244 PAS_DMA_RXINT_CFG_LW);
Olof Johanssonc0efd522007-08-22 09:12:52 -0500245
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600246 ring->next_to_fill = 0;
247 ring->next_to_clean = 0;
248
249 snprintf(ring->irq_name, sizeof(ring->irq_name),
250 "%s rx", dev->name);
251 mac->rx = ring;
252
253 return 0;
254
255out_buffers:
256 dma_free_coherent(&mac->dma_pdev->dev,
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500257 RX_RING_SIZE * sizeof(u64),
258 mac->rx->ring, mac->rx->dma);
259out_ring_desc:
260 kfree(ring->ring_info);
261out_ring_info:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600262 kfree(ring);
263out_ring:
264 return -ENOMEM;
265}
266
267
268static int pasemi_mac_setup_tx_resources(struct net_device *dev)
269{
270 struct pasemi_mac *mac = netdev_priv(dev);
271 u32 val;
272 int chan_id = mac->dma_txch;
273 struct pasemi_mac_txring *ring;
274
275 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
276 if (!ring)
277 goto out_ring;
278
279 spin_lock_init(&ring->lock);
280
Olof Johansson021fa222007-08-22 09:13:11 -0500281 ring->size = TX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500282 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600283 TX_RING_SIZE, GFP_KERNEL);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500284 if (!ring->ring_info)
285 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600286
287 /* Allocate descriptors */
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500288 ring->ring = dma_alloc_coherent(&mac->dma_pdev->dev,
289 TX_RING_SIZE * sizeof(u64),
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600290 &ring->dma, GFP_KERNEL);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500291 if (!ring->ring)
292 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600293
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500294 memset(ring->ring, 0, TX_RING_SIZE * sizeof(u64));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600295
Olof Johanssona85b9422007-09-15 13:40:59 -0700296 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEL(chan_id),
297 PAS_DMA_TXCHAN_BASEL_BRBL(ring->dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600298 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->dma >> 32);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500299 val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 3);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600300
Olof Johanssona85b9422007-09-15 13:40:59 -0700301 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEU(chan_id), val);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600302
Olof Johanssona85b9422007-09-15 13:40:59 -0700303 write_dma_reg(mac, PAS_DMA_TXCHAN_CFG(chan_id),
304 PAS_DMA_TXCHAN_CFG_TY_IFACE |
305 PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
306 PAS_DMA_TXCHAN_CFG_UP |
307 PAS_DMA_TXCHAN_CFG_WT(2));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600308
Olof Johansson021fa222007-08-22 09:13:11 -0500309 ring->next_to_fill = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600310 ring->next_to_clean = 0;
311
312 snprintf(ring->irq_name, sizeof(ring->irq_name),
313 "%s tx", dev->name);
314 mac->tx = ring;
315
316 return 0;
317
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500318out_ring_desc:
319 kfree(ring->ring_info);
320out_ring_info:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600321 kfree(ring);
322out_ring:
323 return -ENOMEM;
324}
325
326static void pasemi_mac_free_tx_resources(struct net_device *dev)
327{
328 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500329 unsigned int i, j;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600330 struct pasemi_mac_buffer *info;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500331 dma_addr_t dmas[MAX_SKB_FRAGS+1];
332 int freed;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600333
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500334 for (i = 0; i < TX_RING_SIZE; i += freed) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500335 info = &TX_RING_INFO(mac, i+1);
336 if (info->dma && info->skb) {
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500337 for (j = 0; j <= skb_shinfo(info->skb)->nr_frags; j++)
338 dmas[j] = TX_RING_INFO(mac, i+1+j).dma;
339 freed = pasemi_mac_unmap_tx_skb(mac, info->skb, dmas);
340 } else
341 freed = 2;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600342 }
343
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500344 for (i = 0; i < TX_RING_SIZE; i++)
345 TX_RING(mac, i) = 0;
346
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600347 dma_free_coherent(&mac->dma_pdev->dev,
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500348 TX_RING_SIZE * sizeof(u64),
349 mac->tx->ring, mac->tx->dma);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600350
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500351 kfree(mac->tx->ring_info);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600352 kfree(mac->tx);
353 mac->tx = NULL;
354}
355
356static void pasemi_mac_free_rx_resources(struct net_device *dev)
357{
358 struct pasemi_mac *mac = netdev_priv(dev);
359 unsigned int i;
360 struct pasemi_mac_buffer *info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600361
362 for (i = 0; i < RX_RING_SIZE; i++) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500363 info = &RX_RING_INFO(mac, i);
364 if (info->skb && info->dma) {
365 pci_unmap_single(mac->dma_pdev,
366 info->dma,
367 info->skb->len,
368 PCI_DMA_FROMDEVICE);
369 dev_kfree_skb_any(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600370 }
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500371 info->dma = 0;
372 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600373 }
374
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500375 for (i = 0; i < RX_RING_SIZE; i++)
376 RX_RING(mac, i) = 0;
377
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600378 dma_free_coherent(&mac->dma_pdev->dev,
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500379 RX_RING_SIZE * sizeof(u64),
380 mac->rx->ring, mac->rx->dma);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600381
382 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
383 mac->rx->buffers, mac->rx->buf_dma);
384
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500385 kfree(mac->rx->ring_info);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600386 kfree(mac->rx);
387 mac->rx = NULL;
388}
389
Olof Johansson928773c2007-09-26 16:25:06 -0500390static void pasemi_mac_replenish_rx_ring(struct net_device *dev, int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600391{
392 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600393 int start = mac->rx->next_to_fill;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500394 unsigned int fill, count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600395
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500396 if (limit <= 0)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600397 return;
398
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500399 fill = start;
Olof Johansson928773c2007-09-26 16:25:06 -0500400 for (count = 0; count < limit; count++) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500401 struct pasemi_mac_buffer *info = &RX_RING_INFO(mac, fill);
402 u64 *buff = &RX_BUFF(mac, fill);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600403 struct sk_buff *skb;
404 dma_addr_t dma;
405
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500406 /* Entry in use? */
407 WARN_ON(*buff);
408
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500409 /* skb might still be in there for recycle on short receives */
410 if (info->skb)
411 skb = info->skb;
412 else
413 skb = dev_alloc_skb(BUF_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600414
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500415 if (unlikely(!skb))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600416 break;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600417
Olof Johansson18eec692007-10-02 16:25:14 -0500418 dma = pci_map_single(mac->dma_pdev, skb->data, BUF_SIZE,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600419 PCI_DMA_FROMDEVICE);
420
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500421 if (unlikely(dma_mapping_error(dma))) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600422 dev_kfree_skb_irq(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600423 break;
424 }
425
426 info->skb = skb;
427 info->dma = dma;
428 *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500429 fill++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600430 }
431
432 wmb();
433
Olof Johansson928773c2007-09-26 16:25:06 -0500434 write_dma_reg(mac, PAS_DMA_RXCHAN_INCR(mac->dma_rxch), count);
435 write_dma_reg(mac, PAS_DMA_RXINT_INCR(mac->dma_if), count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600436
Olof Johansson928773c2007-09-26 16:25:06 -0500437 mac->rx->next_to_fill += count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600438}
439
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500440static void pasemi_mac_restart_rx_intr(struct pasemi_mac *mac)
441{
Olof Johansson52a94352007-05-12 18:01:09 -0500442 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500443 /* Re-enable packet count interrupts: finally
444 * ack the packet count interrupt we got in rx_intr.
445 */
446
Olof Johansson52a94352007-05-12 18:01:09 -0500447 pcnt = *mac->rx_status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500448
Olof Johansson52a94352007-05-12 18:01:09 -0500449 reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500450
Olof Johanssona85b9422007-09-15 13:40:59 -0700451 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500452}
453
454static void pasemi_mac_restart_tx_intr(struct pasemi_mac *mac)
455{
Olof Johansson52a94352007-05-12 18:01:09 -0500456 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500457
458 /* Re-enable packet count interrupts */
Olof Johansson52a94352007-05-12 18:01:09 -0500459 pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500460
Olof Johansson52a94352007-05-12 18:01:09 -0500461 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500462
Olof Johanssona85b9422007-09-15 13:40:59 -0700463 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500464}
465
466
Olof Johansson69c29d82007-10-02 16:24:51 -0500467static inline void pasemi_mac_rx_error(struct pasemi_mac *mac, u64 macrx)
468{
469 unsigned int rcmdsta, ccmdsta;
470
471 if (!netif_msg_rx_err(mac))
472 return;
473
474 rcmdsta = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
475 ccmdsta = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
476
477 printk(KERN_ERR "pasemi_mac: rx error. macrx %016lx, rx status %lx\n",
478 macrx, *mac->rx_status);
479
480 printk(KERN_ERR "pasemi_mac: rcmdsta %08x ccmdsta %08x\n",
481 rcmdsta, ccmdsta);
482}
483
484static inline void pasemi_mac_tx_error(struct pasemi_mac *mac, u64 mactx)
485{
486 unsigned int cmdsta;
487
488 if (!netif_msg_tx_err(mac))
489 return;
490
491 cmdsta = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
492
493 printk(KERN_ERR "pasemi_mac: tx error. mactx 0x%016lx, "\
494 "tx status 0x%016lx\n", mactx, *mac->tx_status);
495
496 printk(KERN_ERR "pasemi_mac: tcmdsta 0x%08x\n", cmdsta);
497}
498
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600499static int pasemi_mac_clean_rx(struct pasemi_mac *mac, int limit)
500{
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500501 unsigned int n;
502 int count;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500503 struct pasemi_mac_buffer *info;
504 struct sk_buff *skb;
505 unsigned int i, len;
506 u64 macrx;
507 dma_addr_t dma;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600508
509 spin_lock(&mac->rx->lock);
510
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500511 n = mac->rx->next_to_clean;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600512
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500513 for (count = limit; count; count--) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600514
515 rmb();
516
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500517 macrx = RX_RING(mac, n);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600518
Olof Johansson69c29d82007-10-02 16:24:51 -0500519 if ((macrx & XCT_MACRX_E) ||
520 (*mac->rx_status & PAS_STATUS_ERROR))
521 pasemi_mac_rx_error(mac, macrx);
522
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500523 if (!(macrx & XCT_MACRX_O))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600524 break;
525
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600526 info = NULL;
527
528 /* We have to scan for our skb since there's no way
529 * to back-map them from the descriptor, and if we
530 * have several receive channels then they might not
531 * show up in the same order as they were put on the
532 * interface ring.
533 */
534
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500535 dma = (RX_RING(mac, n+1) & XCT_PTR_ADDR_M);
536 for (i = mac->rx->next_to_fill;
537 i < (mac->rx->next_to_fill + RX_RING_SIZE);
538 i++) {
539 info = &RX_RING_INFO(mac, i);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600540 if (info->dma == dma)
541 break;
542 }
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500543
Olof Johansson26fcfa92007-08-22 09:12:59 -0500544 prefetchw(info);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600545
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500546 skb = info->skb;
Olof Johansson26fcfa92007-08-22 09:12:59 -0500547 prefetchw(skb);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500548 info->dma = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600549
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500550 pci_unmap_single(mac->dma_pdev, dma, skb->len,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600551 PCI_DMA_FROMDEVICE);
552
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500553 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600554
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500555 if (len < 256) {
556 struct sk_buff *new_skb =
557 netdev_alloc_skb(mac->netdev, len + NET_IP_ALIGN);
558 if (new_skb) {
559 skb_reserve(new_skb, NET_IP_ALIGN);
Olof Johansson73344862007-08-22 09:12:55 -0500560 memcpy(new_skb->data, skb->data, len);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500561 /* save the skb in buffer_info as good */
562 skb = new_skb;
563 }
564 /* else just continue with the old one */
565 } else
566 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600567
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500568 /* Need to zero it out since hardware doesn't, since the
569 * replenish loop uses it to tell when it's done.
570 */
571 RX_BUFF(mac, i) = 0;
572
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600573 skb_put(skb, len);
574
Olof Johansson26fcfa92007-08-22 09:12:59 -0500575 if (likely((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK)) {
Olof Johansson38bf3182007-08-22 09:13:24 -0500576 skb->ip_summed = CHECKSUM_UNNECESSARY;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500577 skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600578 XCT_MACRX_CSUM_S;
579 } else
580 skb->ip_summed = CHECKSUM_NONE;
581
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700582 mac->netdev->stats.rx_bytes += len;
583 mac->netdev->stats.rx_packets++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600584
Olof Johansson26fcfa92007-08-22 09:12:59 -0500585 skb->protocol = eth_type_trans(skb, mac->netdev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600586 netif_receive_skb(skb);
587
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500588 RX_RING(mac, n) = 0;
589 RX_RING(mac, n+1) = 0;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500590
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500591 n += 2;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600592 }
593
Olof Johansson9a50beb2007-10-02 16:26:30 -0500594 if (n > RX_RING_SIZE) {
595 /* Errata 5971 workaround: L2 target of headers */
596 write_iob_reg(mac, PAS_IOB_COM_PKTHDRCNT, 0);
597 n &= (RX_RING_SIZE-1);
598 }
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500599 mac->rx->next_to_clean = n;
Olof Johansson928773c2007-09-26 16:25:06 -0500600 pasemi_mac_replenish_rx_ring(mac->netdev, limit-count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600601
602 spin_unlock(&mac->rx->lock);
603
604 return count;
605}
606
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500607/* Can't make this too large or we blow the kernel stack limits */
608#define TX_CLEAN_BATCHSIZE (128/MAX_SKB_FRAGS)
609
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600610static int pasemi_mac_clean_tx(struct pasemi_mac *mac)
611{
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500612 int i, j;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600613 struct pasemi_mac_buffer *info;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500614 unsigned int start, descr_count, buf_count, limit;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500615 unsigned int total_count;
Olof Johanssonca7e2352007-09-26 16:23:59 -0500616 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500617 struct sk_buff *skbs[TX_CLEAN_BATCHSIZE];
618 dma_addr_t dmas[TX_CLEAN_BATCHSIZE][MAX_SKB_FRAGS+1];
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600619
Olof Johansson02df6cf2007-08-22 09:13:03 -0500620 total_count = 0;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500621 limit = TX_CLEAN_BATCHSIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500622restart:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600623 spin_lock_irqsave(&mac->tx->lock, flags);
624
625 start = mac->tx->next_to_clean;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500626
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500627 buf_count = 0;
628 descr_count = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600629
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500630 for (i = start;
631 descr_count < limit && i < mac->tx->next_to_fill;
632 i += buf_count) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500633 u64 mactx = TX_RING(mac, i);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500634
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500635 if ((mactx & XCT_MACTX_E) ||
Olof Johansson69c29d82007-10-02 16:24:51 -0500636 (*mac->tx_status & PAS_STATUS_ERROR))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500637 pasemi_mac_tx_error(mac, mactx);
Olof Johansson69c29d82007-10-02 16:24:51 -0500638
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500639 if (unlikely(mactx & XCT_MACTX_O))
Olof Johansson02df6cf2007-08-22 09:13:03 -0500640 /* Not yet transmitted */
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600641 break;
642
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500643 info = &TX_RING_INFO(mac, i+1);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500644 skbs[descr_count] = info->skb;
645
646 buf_count = 2 + skb_shinfo(info->skb)->nr_frags;
647 for (j = 0; j <= skb_shinfo(info->skb)->nr_frags; j++)
648 dmas[descr_count][j] = TX_RING_INFO(mac, i+1+j).dma;
649
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600650
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600651 info->dma = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500652 TX_RING(mac, i) = 0;
653 TX_RING(mac, i+1) = 0;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500654 TX_RING_INFO(mac, i+1).skb = 0;
655 TX_RING_INFO(mac, i+1).dma = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500656
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500657 /* Since we always fill with an even number of entries, make
658 * sure we skip any unused one at the end as well.
659 */
660 if (buf_count & 1)
661 buf_count++;
662 descr_count++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600663 }
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500664 mac->tx->next_to_clean = i;
665
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600666 spin_unlock_irqrestore(&mac->tx->lock, flags);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500667 netif_wake_queue(mac->netdev);
668
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500669 for (i = 0; i < descr_count; i++)
670 pasemi_mac_unmap_tx_skb(mac, skbs[i], dmas[i]);
Olof Johansson02df6cf2007-08-22 09:13:03 -0500671
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500672 total_count += descr_count;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500673
674 /* If the batch was full, try to clean more */
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500675 if (descr_count == limit)
Olof Johansson02df6cf2007-08-22 09:13:03 -0500676 goto restart;
677
678 return total_count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600679}
680
681
682static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
683{
684 struct net_device *dev = data;
685 struct pasemi_mac *mac = netdev_priv(dev);
686 unsigned int reg;
687
Olof Johansson6dfa7522007-05-08 00:47:32 -0500688 if (!(*mac->rx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600689 return IRQ_NONE;
690
Olof Johansson6dfa7522007-05-08 00:47:32 -0500691 /* Don't reset packet count so it won't fire again but clear
692 * all others.
693 */
694
Olof Johansson6dfa7522007-05-08 00:47:32 -0500695 reg = 0;
696 if (*mac->rx_status & PAS_STATUS_SOFT)
697 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
698 if (*mac->rx_status & PAS_STATUS_ERROR)
699 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600700 if (*mac->rx_status & PAS_STATUS_TIMER)
701 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
702
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700703 netif_rx_schedule(dev, &mac->napi);
Olof Johansson6dfa7522007-05-08 00:47:32 -0500704
Olof Johanssona85b9422007-09-15 13:40:59 -0700705 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600706
707 return IRQ_HANDLED;
708}
709
710static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
711{
712 struct net_device *dev = data;
713 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson52a94352007-05-12 18:01:09 -0500714 unsigned int reg, pcnt;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600715
Olof Johansson6dfa7522007-05-08 00:47:32 -0500716 if (!(*mac->tx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600717 return IRQ_NONE;
718
719 pasemi_mac_clean_tx(mac);
720
Olof Johansson52a94352007-05-12 18:01:09 -0500721 pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
722
723 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson6dfa7522007-05-08 00:47:32 -0500724
725 if (*mac->tx_status & PAS_STATUS_SOFT)
726 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
727 if (*mac->tx_status & PAS_STATUS_ERROR)
728 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600729
Olof Johanssona85b9422007-09-15 13:40:59 -0700730 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600731
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600732 return IRQ_HANDLED;
733}
734
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500735static void pasemi_adjust_link(struct net_device *dev)
736{
737 struct pasemi_mac *mac = netdev_priv(dev);
738 int msg;
739 unsigned int flags;
740 unsigned int new_flags;
741
742 if (!mac->phydev->link) {
743 /* If no link, MAC speed settings don't matter. Just report
744 * link down and return.
745 */
746 if (mac->link && netif_msg_link(mac))
747 printk(KERN_INFO "%s: Link is down.\n", dev->name);
748
749 netif_carrier_off(dev);
750 mac->link = 0;
751
752 return;
753 } else
754 netif_carrier_on(dev);
755
Olof Johanssona85b9422007-09-15 13:40:59 -0700756 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500757 new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
758 PAS_MAC_CFG_PCFG_TSR_M);
759
760 if (!mac->phydev->duplex)
761 new_flags |= PAS_MAC_CFG_PCFG_HD;
762
763 switch (mac->phydev->speed) {
764 case 1000:
765 new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
766 PAS_MAC_CFG_PCFG_TSR_1G;
767 break;
768 case 100:
769 new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
770 PAS_MAC_CFG_PCFG_TSR_100M;
771 break;
772 case 10:
773 new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
774 PAS_MAC_CFG_PCFG_TSR_10M;
775 break;
776 default:
777 printk("Unsupported speed %d\n", mac->phydev->speed);
778 }
779
780 /* Print on link or speed/duplex change */
781 msg = mac->link != mac->phydev->link || flags != new_flags;
782
783 mac->duplex = mac->phydev->duplex;
784 mac->speed = mac->phydev->speed;
785 mac->link = mac->phydev->link;
786
787 if (new_flags != flags)
Olof Johanssona85b9422007-09-15 13:40:59 -0700788 write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500789
790 if (msg && netif_msg_link(mac))
791 printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
792 dev->name, mac->speed, mac->duplex ? "full" : "half");
793}
794
795static int pasemi_mac_phy_init(struct net_device *dev)
796{
797 struct pasemi_mac *mac = netdev_priv(dev);
798 struct device_node *dn, *phy_dn;
799 struct phy_device *phydev;
800 unsigned int phy_id;
801 const phandle *ph;
802 const unsigned int *prop;
803 struct resource r;
804 int ret;
805
806 dn = pci_device_to_OF_node(mac->pdev);
Linus Torvalds90287802007-05-08 11:57:17 -0700807 ph = of_get_property(dn, "phy-handle", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500808 if (!ph)
809 return -ENODEV;
810 phy_dn = of_find_node_by_phandle(*ph);
811
Linus Torvalds90287802007-05-08 11:57:17 -0700812 prop = of_get_property(phy_dn, "reg", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500813 ret = of_address_to_resource(phy_dn->parent, 0, &r);
814 if (ret)
815 goto err;
816
817 phy_id = *prop;
818 snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);
819
820 of_node_put(phy_dn);
821
822 mac->link = 0;
823 mac->speed = 0;
824 mac->duplex = -1;
825
826 phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);
827
828 if (IS_ERR(phydev)) {
829 printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
830 return PTR_ERR(phydev);
831 }
832
833 mac->phydev = phydev;
834
835 return 0;
836
837err:
838 of_node_put(phy_dn);
839 return -ENODEV;
840}
841
842
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600843static int pasemi_mac_open(struct net_device *dev)
844{
845 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson771f7402007-05-08 00:47:21 -0500846 int base_irq;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600847 unsigned int flags;
848 int ret;
849
850 /* enable rx section */
Olof Johanssona85b9422007-09-15 13:40:59 -0700851 write_dma_reg(mac, PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600852
853 /* enable tx section */
Olof Johanssona85b9422007-09-15 13:40:59 -0700854 write_dma_reg(mac, PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600855
856 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
857 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
858 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
859
Olof Johanssona85b9422007-09-15 13:40:59 -0700860 write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600861
Olof Johanssona85b9422007-09-15 13:40:59 -0700862 write_iob_reg(mac, PAS_IOB_DMA_RXCH_CFG(mac->dma_rxch),
863 PAS_IOB_DMA_RXCH_CFG_CNTTH(0));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600864
Olof Johanssona85b9422007-09-15 13:40:59 -0700865 write_iob_reg(mac, PAS_IOB_DMA_TXCH_CFG(mac->dma_txch),
Olof Johansson02df6cf2007-08-22 09:13:03 -0500866 PAS_IOB_DMA_TXCH_CFG_CNTTH(128));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600867
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500868 /* Clear out any residual packet count state from firmware */
869 pasemi_mac_restart_rx_intr(mac);
870 pasemi_mac_restart_tx_intr(mac);
871
Olof Johansson6dfa7522007-05-08 00:47:32 -0500872 /* 0xffffff is max value, about 16ms */
Olof Johanssona85b9422007-09-15 13:40:59 -0700873 write_iob_reg(mac, PAS_IOB_DMA_COM_TIMEOUTCFG,
874 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0xffffff));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600875
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600876 ret = pasemi_mac_setup_rx_resources(dev);
877 if (ret)
878 goto out_rx_resources;
879
880 ret = pasemi_mac_setup_tx_resources(dev);
881 if (ret)
882 goto out_tx_resources;
883
Olof Johanssona85b9422007-09-15 13:40:59 -0700884 write_mac_reg(mac, PAS_MAC_IPC_CHNL,
885 PAS_MAC_IPC_CHNL_DCHNO(mac->dma_rxch) |
886 PAS_MAC_IPC_CHNL_BCH(mac->dma_rxch));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600887
888 /* enable rx if */
Olof Johanssona85b9422007-09-15 13:40:59 -0700889 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
890 PAS_DMA_RXINT_RCMDSTA_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600891
892 /* enable rx channel */
Olof Johanssona85b9422007-09-15 13:40:59 -0700893 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
894 PAS_DMA_RXCHAN_CCMDSTA_EN |
895 PAS_DMA_RXCHAN_CCMDSTA_DU);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600896
897 /* enable tx channel */
Olof Johanssona85b9422007-09-15 13:40:59 -0700898 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
899 PAS_DMA_TXCHAN_TCMDSTA_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600900
Olof Johansson928773c2007-09-26 16:25:06 -0500901 pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600902
Olof Johansson36033762007-09-26 16:24:42 -0500903 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
904 PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
905
906 if (mac->type == MAC_TYPE_GMAC)
907 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
908 else
909 flags |= PAS_MAC_CFG_PCFG_TSR_10G | PAS_MAC_CFG_PCFG_SPD_10G;
910
911 /* Enable interface in MAC */
912 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
913
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500914 ret = pasemi_mac_phy_init(dev);
915 /* Some configs don't have PHYs (XAUI etc), so don't complain about
916 * failed init due to -ENODEV.
917 */
918 if (ret && ret != -ENODEV)
919 dev_warn(&mac->pdev->dev, "phy init failed: %d\n", ret);
920
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600921 netif_start_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700922 napi_enable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600923
Olof Johansson771f7402007-05-08 00:47:21 -0500924 /* Interrupts are a bit different for our DMA controller: While
925 * it's got one a regular PCI device header, the interrupt there
926 * is really the base of the range it's using. Each tx and rx
927 * channel has it's own interrupt source.
928 */
929
930 base_irq = virq_to_hw(mac->dma_pdev->irq);
931
932 mac->tx_irq = irq_create_mapping(NULL, base_irq + mac->dma_txch);
933 mac->rx_irq = irq_create_mapping(NULL, base_irq + 20 + mac->dma_txch);
934
935 ret = request_irq(mac->tx_irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600936 mac->tx->irq_name, dev);
937 if (ret) {
938 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500939 base_irq + mac->dma_txch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600940 goto out_tx_int;
941 }
942
Olof Johansson771f7402007-05-08 00:47:21 -0500943 ret = request_irq(mac->rx_irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600944 mac->rx->irq_name, dev);
945 if (ret) {
946 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500947 base_irq + 20 + mac->dma_rxch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600948 goto out_rx_int;
949 }
950
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500951 if (mac->phydev)
952 phy_start(mac->phydev);
953
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600954 return 0;
955
956out_rx_int:
Olof Johansson771f7402007-05-08 00:47:21 -0500957 free_irq(mac->tx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600958out_tx_int:
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700959 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600960 netif_stop_queue(dev);
961 pasemi_mac_free_tx_resources(dev);
962out_tx_resources:
963 pasemi_mac_free_rx_resources(dev);
964out_rx_resources:
965
966 return ret;
967}
968
969#define MAX_RETRIES 5000
970
971static int pasemi_mac_close(struct net_device *dev)
972{
973 struct pasemi_mac *mac = netdev_priv(dev);
974 unsigned int stat;
975 int retries;
976
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500977 if (mac->phydev) {
978 phy_stop(mac->phydev);
979 phy_disconnect(mac->phydev);
980 }
981
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600982 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700983 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600984
985 /* Clean out any pending buffers */
986 pasemi_mac_clean_tx(mac);
987 pasemi_mac_clean_rx(mac, RX_RING_SIZE);
988
989 /* Disable interface */
Olof Johanssona85b9422007-09-15 13:40:59 -0700990 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), PAS_DMA_TXCHAN_TCMDSTA_ST);
991 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), PAS_DMA_RXINT_RCMDSTA_ST);
992 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), PAS_DMA_RXCHAN_CCMDSTA_ST);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600993
994 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -0700995 stat = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
Olof Johansson0ce68c72007-04-28 15:36:40 -0500996 if (!(stat & PAS_DMA_TXCHAN_TCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600997 break;
998 cond_resched();
999 }
1000
Olof Johansson0ce68c72007-04-28 15:36:40 -05001001 if (stat & PAS_DMA_TXCHAN_TCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001002 dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001003
1004 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -07001005 stat = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
Olof Johansson0ce68c72007-04-28 15:36:40 -05001006 if (!(stat & PAS_DMA_RXCHAN_CCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001007 break;
1008 cond_resched();
1009 }
1010
Olof Johansson0ce68c72007-04-28 15:36:40 -05001011 if (stat & PAS_DMA_RXCHAN_CCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001012 dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001013
1014 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -07001015 stat = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
Olof Johansson0ce68c72007-04-28 15:36:40 -05001016 if (!(stat & PAS_DMA_RXINT_RCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001017 break;
1018 cond_resched();
1019 }
1020
Olof Johansson0ce68c72007-04-28 15:36:40 -05001021 if (stat & PAS_DMA_RXINT_RCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001022 dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001023
1024 /* Then, disable the channel. This must be done separately from
1025 * stopping, since you can't disable when active.
1026 */
1027
Olof Johanssona85b9422007-09-15 13:40:59 -07001028 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), 0);
1029 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), 0);
1030 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001031
Olof Johansson771f7402007-05-08 00:47:21 -05001032 free_irq(mac->tx_irq, dev);
1033 free_irq(mac->rx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001034
1035 /* Free resources */
1036 pasemi_mac_free_rx_resources(dev);
1037 pasemi_mac_free_tx_resources(dev);
1038
1039 return 0;
1040}
1041
1042static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
1043{
1044 struct pasemi_mac *mac = netdev_priv(dev);
1045 struct pasemi_mac_txring *txring;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001046 u64 dflags, mactx;
1047 dma_addr_t map[MAX_SKB_FRAGS+1];
1048 unsigned int map_size[MAX_SKB_FRAGS+1];
Olof Johanssonca7e2352007-09-26 16:23:59 -05001049 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001050 int i, nfrags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001051
1052 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_SS | XCT_MACTX_CRC_PAD;
1053
1054 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001055 const unsigned char *nh = skb_network_header(skb);
1056
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001057 switch (ip_hdr(skb)->protocol) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001058 case IPPROTO_TCP:
1059 dflags |= XCT_MACTX_CSUM_TCP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001060 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001061 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001062 break;
1063 case IPPROTO_UDP:
1064 dflags |= XCT_MACTX_CSUM_UDP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001065 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001066 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001067 break;
1068 }
1069 }
1070
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001071 nfrags = skb_shinfo(skb)->nr_frags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001072
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001073 map[0] = pci_map_single(mac->dma_pdev, skb->data, skb_headlen(skb),
1074 PCI_DMA_TODEVICE);
1075 map_size[0] = skb_headlen(skb);
1076 if (dma_mapping_error(map[0]))
1077 goto out_err_nolock;
1078
1079 for (i = 0; i < nfrags; i++) {
1080 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1081
1082 map[i+1] = pci_map_page(mac->dma_pdev, frag->page,
1083 frag->page_offset, frag->size,
1084 PCI_DMA_TODEVICE);
1085 map_size[i+1] = frag->size;
1086 if (dma_mapping_error(map[i+1])) {
1087 nfrags = i;
1088 goto out_err_nolock;
1089 }
1090 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001091
Olof Johansson26fcfa92007-08-22 09:12:59 -05001092 mactx = dflags | XCT_MACTX_LLEN(skb->len);
Olof Johansson26fcfa92007-08-22 09:12:59 -05001093
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001094 txring = mac->tx;
1095
1096 spin_lock_irqsave(&txring->lock, flags);
1097
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001098 if (RING_AVAIL(txring) <= nfrags+3) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001099 spin_unlock_irqrestore(&txring->lock, flags);
1100 pasemi_mac_clean_tx(mac);
Olof Johansson52a94352007-05-12 18:01:09 -05001101 pasemi_mac_restart_tx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001102 spin_lock_irqsave(&txring->lock, flags);
1103
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001104 if (RING_AVAIL(txring) <= nfrags+3) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001105 /* Still no room -- stop the queue and wait for tx
1106 * intr when there's room.
1107 */
1108 netif_stop_queue(dev);
1109 goto out_err;
1110 }
1111 }
1112
Olof Johanssonfc9e4d22007-10-02 16:25:53 -05001113 TX_RING(mac, txring->next_to_fill) = mactx;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001114 txring->next_to_fill++;
1115 TX_RING_INFO(mac, txring->next_to_fill).skb = skb;
1116 for (i = 0; i <= nfrags; i++) {
1117 TX_RING(mac, txring->next_to_fill+i) =
1118 XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]);
1119 TX_RING_INFO(mac, txring->next_to_fill+i).dma = map[i];
1120 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001121
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001122 /* We have to add an even number of 8-byte entries to the ring
1123 * even if the last one is unused. That means always an odd number
1124 * of pointers + one mactx descriptor.
1125 */
1126 if (nfrags & 1)
1127 nfrags++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001128
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001129 txring->next_to_fill += nfrags + 1;
1130
Olof Johanssonfc9e4d22007-10-02 16:25:53 -05001131
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001132 dev->stats.tx_packets++;
1133 dev->stats.tx_bytes += skb->len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001134
1135 spin_unlock_irqrestore(&txring->lock, flags);
1136
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001137 write_dma_reg(mac, PAS_DMA_TXCHAN_INCR(mac->dma_txch), (nfrags+2) >> 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001138
1139 return NETDEV_TX_OK;
1140
1141out_err:
1142 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001143out_err_nolock:
1144 while (nfrags--)
1145 pci_unmap_single(mac->dma_pdev, map[nfrags], map_size[nfrags],
1146 PCI_DMA_TODEVICE);
1147
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001148 return NETDEV_TX_BUSY;
1149}
1150
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001151static void pasemi_mac_set_rx_mode(struct net_device *dev)
1152{
1153 struct pasemi_mac *mac = netdev_priv(dev);
1154 unsigned int flags;
1155
Olof Johanssona85b9422007-09-15 13:40:59 -07001156 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001157
1158 /* Set promiscuous */
1159 if (dev->flags & IFF_PROMISC)
1160 flags |= PAS_MAC_CFG_PCFG_PR;
1161 else
1162 flags &= ~PAS_MAC_CFG_PCFG_PR;
1163
Olof Johanssona85b9422007-09-15 13:40:59 -07001164 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001165}
1166
1167
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001168static int pasemi_mac_poll(struct napi_struct *napi, int budget)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001169{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001170 struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
1171 struct net_device *dev = mac->netdev;
1172 int pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001173
Olof Johansson829185e2007-09-15 13:53:19 -07001174 pasemi_mac_clean_tx(mac);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001175 pkts = pasemi_mac_clean_rx(mac, budget);
1176 if (pkts < budget) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001177 /* all done, no more packets present */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001178 netif_rx_complete(dev, napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001179
Olof Johansson1b0335ea2007-05-08 00:47:26 -05001180 pasemi_mac_restart_rx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001181 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001182 return pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001183}
1184
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001185static void __iomem * __devinit map_onedev(struct pci_dev *p, int index)
1186{
1187 struct device_node *dn;
1188 void __iomem *ret;
1189
1190 dn = pci_device_to_OF_node(p);
1191 if (!dn)
1192 goto fallback;
1193
1194 ret = of_iomap(dn, index);
1195 if (!ret)
1196 goto fallback;
1197
1198 return ret;
1199fallback:
1200 /* This is hardcoded and ugly, but we have some firmware versions
1201 * that don't provide the register space in the device tree. Luckily
1202 * they are at well-known locations so we can just do the math here.
1203 */
1204 return ioremap(0xe0000000 + (p->devfn << 12), 0x2000);
1205}
1206
1207static int __devinit pasemi_mac_map_regs(struct pasemi_mac *mac)
1208{
1209 struct resource res;
1210 struct device_node *dn;
1211 int err;
1212
1213 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
1214 if (!mac->dma_pdev) {
1215 dev_err(&mac->pdev->dev, "Can't find DMA Controller\n");
1216 return -ENODEV;
1217 }
1218
1219 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
1220 if (!mac->iob_pdev) {
1221 dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n");
1222 return -ENODEV;
1223 }
1224
1225 mac->regs = map_onedev(mac->pdev, 0);
1226 mac->dma_regs = map_onedev(mac->dma_pdev, 0);
1227 mac->iob_regs = map_onedev(mac->iob_pdev, 0);
1228
1229 if (!mac->regs || !mac->dma_regs || !mac->iob_regs) {
1230 dev_err(&mac->pdev->dev, "Can't map registers\n");
1231 return -ENODEV;
1232 }
1233
1234 /* The dma status structure is located in the I/O bridge, and
1235 * is cache coherent.
1236 */
1237 if (!dma_status) {
1238 dn = pci_device_to_OF_node(mac->iob_pdev);
1239 if (dn)
1240 err = of_address_to_resource(dn, 1, &res);
1241 if (!dn || err) {
1242 /* Fallback for old firmware */
1243 res.start = 0xfd800000;
1244 res.end = res.start + 0x1000;
1245 }
1246 dma_status = __ioremap(res.start, res.end-res.start, 0);
1247 }
1248
1249 return 0;
1250}
1251
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001252static int __devinit
1253pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1254{
1255 static int index = 0;
1256 struct net_device *dev;
1257 struct pasemi_mac *mac;
1258 int err;
Joe Perches0795af52007-10-03 17:59:30 -07001259 DECLARE_MAC_BUF(mac_buf);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001260
1261 err = pci_enable_device(pdev);
1262 if (err)
1263 return err;
1264
1265 dev = alloc_etherdev(sizeof(struct pasemi_mac));
1266 if (dev == NULL) {
1267 dev_err(&pdev->dev,
1268 "pasemi_mac: Could not allocate ethernet device.\n");
1269 err = -ENOMEM;
1270 goto out_disable_device;
1271 }
1272
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001273 pci_set_drvdata(pdev, dev);
1274 SET_NETDEV_DEV(dev, &pdev->dev);
1275
1276 mac = netdev_priv(dev);
1277
1278 mac->pdev = pdev;
1279 mac->netdev = dev;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001280
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001281 netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
1282
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001283 dev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX | NETIF_F_SG;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001284
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001285 /* These should come out of the device tree eventually */
1286 mac->dma_txch = index;
1287 mac->dma_rxch = index;
1288
1289 /* We probe GMAC before XAUI, but the DMA interfaces are
1290 * in XAUI, GMAC order.
1291 */
1292 if (index < 4)
1293 mac->dma_if = index + 2;
1294 else
1295 mac->dma_if = index - 4;
1296 index++;
1297
1298 switch (pdev->device) {
1299 case 0xa005:
1300 mac->type = MAC_TYPE_GMAC;
1301 break;
1302 case 0xa006:
1303 mac->type = MAC_TYPE_XAUI;
1304 break;
1305 default:
1306 err = -ENODEV;
1307 goto out;
1308 }
1309
1310 /* get mac addr from device tree */
1311 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
1312 err = -ENODEV;
1313 goto out;
1314 }
1315 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
1316
1317 dev->open = pasemi_mac_open;
1318 dev->stop = pasemi_mac_close;
1319 dev->hard_start_xmit = pasemi_mac_start_tx;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001320 dev->set_multicast_list = pasemi_mac_set_rx_mode;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001321
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001322 err = pasemi_mac_map_regs(mac);
1323 if (err)
1324 goto out;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001325
1326 mac->rx_status = &dma_status->rx_sta[mac->dma_rxch];
1327 mac->tx_status = &dma_status->tx_sta[mac->dma_txch];
1328
Olof Johanssonceb51362007-05-08 00:47:49 -05001329 mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1330
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001331 /* Enable most messages by default */
1332 mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1333
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001334 err = register_netdev(dev);
1335
1336 if (err) {
1337 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1338 err);
1339 goto out;
Olof Johansson69c29d82007-10-02 16:24:51 -05001340 } else if netif_msg_probe(mac)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001341 printk(KERN_INFO "%s: PA Semi %s: intf %d, txch %d, rxch %d, "
Joe Perches0795af52007-10-03 17:59:30 -07001342 "hw addr %s\n",
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001343 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
1344 mac->dma_if, mac->dma_txch, mac->dma_rxch,
Joe Perches0795af52007-10-03 17:59:30 -07001345 print_mac(mac_buf, dev->dev_addr));
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001346
1347 return err;
1348
1349out:
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001350 if (mac->iob_pdev)
1351 pci_dev_put(mac->iob_pdev);
1352 if (mac->dma_pdev)
1353 pci_dev_put(mac->dma_pdev);
1354 if (mac->dma_regs)
1355 iounmap(mac->dma_regs);
1356 if (mac->iob_regs)
1357 iounmap(mac->iob_regs);
1358 if (mac->regs)
1359 iounmap(mac->regs);
1360
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001361 free_netdev(dev);
1362out_disable_device:
1363 pci_disable_device(pdev);
1364 return err;
1365
1366}
1367
1368static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1369{
1370 struct net_device *netdev = pci_get_drvdata(pdev);
1371 struct pasemi_mac *mac;
1372
1373 if (!netdev)
1374 return;
1375
1376 mac = netdev_priv(netdev);
1377
1378 unregister_netdev(netdev);
1379
1380 pci_disable_device(pdev);
1381 pci_dev_put(mac->dma_pdev);
1382 pci_dev_put(mac->iob_pdev);
1383
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001384 iounmap(mac->regs);
1385 iounmap(mac->dma_regs);
1386 iounmap(mac->iob_regs);
1387
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001388 pci_set_drvdata(pdev, NULL);
1389 free_netdev(netdev);
1390}
1391
1392static struct pci_device_id pasemi_mac_pci_tbl[] = {
1393 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1394 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
olof@lixom.netfd178252007-05-12 14:57:36 -05001395 { },
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001396};
1397
1398MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1399
1400static struct pci_driver pasemi_mac_driver = {
1401 .name = "pasemi_mac",
1402 .id_table = pasemi_mac_pci_tbl,
1403 .probe = pasemi_mac_probe,
1404 .remove = __devexit_p(pasemi_mac_remove),
1405};
1406
1407static void __exit pasemi_mac_cleanup_module(void)
1408{
1409 pci_unregister_driver(&pasemi_mac_driver);
1410 __iounmap(dma_status);
1411 dma_status = NULL;
1412}
1413
1414int pasemi_mac_init_module(void)
1415{
1416 return pci_register_driver(&pasemi_mac_driver);
1417}
1418
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001419module_init(pasemi_mac_init_module);
1420module_exit(pasemi_mac_cleanup_module);