blob: 5eb5e47120d07a409b869eeb8874718bc045d984 [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),
242 PAS_DMA_RXINT_CFG_DHL(2));
243
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600244 ring->next_to_fill = 0;
245 ring->next_to_clean = 0;
246
247 snprintf(ring->irq_name, sizeof(ring->irq_name),
248 "%s rx", dev->name);
249 mac->rx = ring;
250
251 return 0;
252
253out_buffers:
254 dma_free_coherent(&mac->dma_pdev->dev,
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500255 RX_RING_SIZE * sizeof(u64),
256 mac->rx->ring, mac->rx->dma);
257out_ring_desc:
258 kfree(ring->ring_info);
259out_ring_info:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600260 kfree(ring);
261out_ring:
262 return -ENOMEM;
263}
264
265
266static int pasemi_mac_setup_tx_resources(struct net_device *dev)
267{
268 struct pasemi_mac *mac = netdev_priv(dev);
269 u32 val;
270 int chan_id = mac->dma_txch;
271 struct pasemi_mac_txring *ring;
272
273 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
274 if (!ring)
275 goto out_ring;
276
277 spin_lock_init(&ring->lock);
278
Olof Johansson021fa222007-08-22 09:13:11 -0500279 ring->size = TX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500280 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600281 TX_RING_SIZE, GFP_KERNEL);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500282 if (!ring->ring_info)
283 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600284
285 /* Allocate descriptors */
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500286 ring->ring = dma_alloc_coherent(&mac->dma_pdev->dev,
287 TX_RING_SIZE * sizeof(u64),
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600288 &ring->dma, GFP_KERNEL);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500289 if (!ring->ring)
290 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600291
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500292 memset(ring->ring, 0, TX_RING_SIZE * sizeof(u64));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600293
Olof Johanssona85b9422007-09-15 13:40:59 -0700294 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEL(chan_id),
295 PAS_DMA_TXCHAN_BASEL_BRBL(ring->dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600296 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->dma >> 32);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500297 val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 3);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600298
Olof Johanssona85b9422007-09-15 13:40:59 -0700299 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEU(chan_id), val);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600300
Olof Johanssona85b9422007-09-15 13:40:59 -0700301 write_dma_reg(mac, PAS_DMA_TXCHAN_CFG(chan_id),
302 PAS_DMA_TXCHAN_CFG_TY_IFACE |
303 PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
304 PAS_DMA_TXCHAN_CFG_UP |
305 PAS_DMA_TXCHAN_CFG_WT(2));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600306
Olof Johansson021fa222007-08-22 09:13:11 -0500307 ring->next_to_fill = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600308 ring->next_to_clean = 0;
309
310 snprintf(ring->irq_name, sizeof(ring->irq_name),
311 "%s tx", dev->name);
312 mac->tx = ring;
313
314 return 0;
315
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500316out_ring_desc:
317 kfree(ring->ring_info);
318out_ring_info:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600319 kfree(ring);
320out_ring:
321 return -ENOMEM;
322}
323
324static void pasemi_mac_free_tx_resources(struct net_device *dev)
325{
326 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500327 unsigned int i, j;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600328 struct pasemi_mac_buffer *info;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500329 dma_addr_t dmas[MAX_SKB_FRAGS+1];
330 int freed;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600331
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500332 for (i = 0; i < TX_RING_SIZE; i += freed) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500333 info = &TX_RING_INFO(mac, i+1);
334 if (info->dma && info->skb) {
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500335 for (j = 0; j <= skb_shinfo(info->skb)->nr_frags; j++)
336 dmas[j] = TX_RING_INFO(mac, i+1+j).dma;
337 freed = pasemi_mac_unmap_tx_skb(mac, info->skb, dmas);
338 } else
339 freed = 2;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600340 }
341
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500342 for (i = 0; i < TX_RING_SIZE; i++)
343 TX_RING(mac, i) = 0;
344
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600345 dma_free_coherent(&mac->dma_pdev->dev,
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500346 TX_RING_SIZE * sizeof(u64),
347 mac->tx->ring, mac->tx->dma);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600348
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500349 kfree(mac->tx->ring_info);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600350 kfree(mac->tx);
351 mac->tx = NULL;
352}
353
354static void pasemi_mac_free_rx_resources(struct net_device *dev)
355{
356 struct pasemi_mac *mac = netdev_priv(dev);
357 unsigned int i;
358 struct pasemi_mac_buffer *info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600359
360 for (i = 0; i < RX_RING_SIZE; i++) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500361 info = &RX_RING_INFO(mac, i);
362 if (info->skb && info->dma) {
363 pci_unmap_single(mac->dma_pdev,
364 info->dma,
365 info->skb->len,
366 PCI_DMA_FROMDEVICE);
367 dev_kfree_skb_any(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600368 }
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500369 info->dma = 0;
370 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600371 }
372
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500373 for (i = 0; i < RX_RING_SIZE; i++)
374 RX_RING(mac, i) = 0;
375
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600376 dma_free_coherent(&mac->dma_pdev->dev,
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500377 RX_RING_SIZE * sizeof(u64),
378 mac->rx->ring, mac->rx->dma);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600379
380 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
381 mac->rx->buffers, mac->rx->buf_dma);
382
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500383 kfree(mac->rx->ring_info);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600384 kfree(mac->rx);
385 mac->rx = NULL;
386}
387
Olof Johansson928773c2007-09-26 16:25:06 -0500388static void pasemi_mac_replenish_rx_ring(struct net_device *dev, int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600389{
390 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600391 int start = mac->rx->next_to_fill;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500392 unsigned int fill, count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600393
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500394 if (limit <= 0)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600395 return;
396
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500397 fill = start;
Olof Johansson928773c2007-09-26 16:25:06 -0500398 for (count = 0; count < limit; count++) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500399 struct pasemi_mac_buffer *info = &RX_RING_INFO(mac, fill);
400 u64 *buff = &RX_BUFF(mac, fill);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600401 struct sk_buff *skb;
402 dma_addr_t dma;
403
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500404 /* Entry in use? */
405 WARN_ON(*buff);
406
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500407 /* skb might still be in there for recycle on short receives */
408 if (info->skb)
409 skb = info->skb;
410 else
411 skb = dev_alloc_skb(BUF_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600412
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500413 if (unlikely(!skb))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600414 break;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600415
Olof Johansson18eec692007-10-02 16:25:14 -0500416 dma = pci_map_single(mac->dma_pdev, skb->data, BUF_SIZE,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600417 PCI_DMA_FROMDEVICE);
418
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500419 if (unlikely(dma_mapping_error(dma))) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600420 dev_kfree_skb_irq(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600421 break;
422 }
423
424 info->skb = skb;
425 info->dma = dma;
426 *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500427 fill++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600428 }
429
430 wmb();
431
Olof Johansson928773c2007-09-26 16:25:06 -0500432 write_dma_reg(mac, PAS_DMA_RXCHAN_INCR(mac->dma_rxch), count);
433 write_dma_reg(mac, PAS_DMA_RXINT_INCR(mac->dma_if), count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600434
Olof Johansson928773c2007-09-26 16:25:06 -0500435 mac->rx->next_to_fill += count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600436}
437
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500438static void pasemi_mac_restart_rx_intr(struct pasemi_mac *mac)
439{
Olof Johansson52a94352007-05-12 18:01:09 -0500440 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500441 /* Re-enable packet count interrupts: finally
442 * ack the packet count interrupt we got in rx_intr.
443 */
444
Olof Johansson52a94352007-05-12 18:01:09 -0500445 pcnt = *mac->rx_status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500446
Olof Johansson52a94352007-05-12 18:01:09 -0500447 reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500448
Olof Johanssona85b9422007-09-15 13:40:59 -0700449 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500450}
451
452static void pasemi_mac_restart_tx_intr(struct pasemi_mac *mac)
453{
Olof Johansson52a94352007-05-12 18:01:09 -0500454 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500455
456 /* Re-enable packet count interrupts */
Olof Johansson52a94352007-05-12 18:01:09 -0500457 pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500458
Olof Johansson52a94352007-05-12 18:01:09 -0500459 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500460
Olof Johanssona85b9422007-09-15 13:40:59 -0700461 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500462}
463
464
Olof Johansson69c29d82007-10-02 16:24:51 -0500465static inline void pasemi_mac_rx_error(struct pasemi_mac *mac, u64 macrx)
466{
467 unsigned int rcmdsta, ccmdsta;
468
469 if (!netif_msg_rx_err(mac))
470 return;
471
472 rcmdsta = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
473 ccmdsta = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
474
475 printk(KERN_ERR "pasemi_mac: rx error. macrx %016lx, rx status %lx\n",
476 macrx, *mac->rx_status);
477
478 printk(KERN_ERR "pasemi_mac: rcmdsta %08x ccmdsta %08x\n",
479 rcmdsta, ccmdsta);
480}
481
482static inline void pasemi_mac_tx_error(struct pasemi_mac *mac, u64 mactx)
483{
484 unsigned int cmdsta;
485
486 if (!netif_msg_tx_err(mac))
487 return;
488
489 cmdsta = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
490
491 printk(KERN_ERR "pasemi_mac: tx error. mactx 0x%016lx, "\
492 "tx status 0x%016lx\n", mactx, *mac->tx_status);
493
494 printk(KERN_ERR "pasemi_mac: tcmdsta 0x%08x\n", cmdsta);
495}
496
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600497static int pasemi_mac_clean_rx(struct pasemi_mac *mac, int limit)
498{
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500499 unsigned int n;
500 int count;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500501 struct pasemi_mac_buffer *info;
502 struct sk_buff *skb;
503 unsigned int i, len;
504 u64 macrx;
505 dma_addr_t dma;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600506
507 spin_lock(&mac->rx->lock);
508
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500509 n = mac->rx->next_to_clean;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600510
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500511 for (count = limit; count; count--) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600512
513 rmb();
514
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500515 macrx = RX_RING(mac, n);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600516
Olof Johansson69c29d82007-10-02 16:24:51 -0500517 if ((macrx & XCT_MACRX_E) ||
518 (*mac->rx_status & PAS_STATUS_ERROR))
519 pasemi_mac_rx_error(mac, macrx);
520
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500521 if (!(macrx & XCT_MACRX_O))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600522 break;
523
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600524 info = NULL;
525
526 /* We have to scan for our skb since there's no way
527 * to back-map them from the descriptor, and if we
528 * have several receive channels then they might not
529 * show up in the same order as they were put on the
530 * interface ring.
531 */
532
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500533 dma = (RX_RING(mac, n+1) & XCT_PTR_ADDR_M);
534 for (i = mac->rx->next_to_fill;
535 i < (mac->rx->next_to_fill + RX_RING_SIZE);
536 i++) {
537 info = &RX_RING_INFO(mac, i);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600538 if (info->dma == dma)
539 break;
540 }
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500541
Olof Johansson26fcfa92007-08-22 09:12:59 -0500542 prefetchw(info);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600543
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500544 skb = info->skb;
Olof Johansson26fcfa92007-08-22 09:12:59 -0500545 prefetchw(skb);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500546 info->dma = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600547
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500548 pci_unmap_single(mac->dma_pdev, dma, skb->len,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600549 PCI_DMA_FROMDEVICE);
550
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500551 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600552
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500553 if (len < 256) {
554 struct sk_buff *new_skb =
555 netdev_alloc_skb(mac->netdev, len + NET_IP_ALIGN);
556 if (new_skb) {
557 skb_reserve(new_skb, NET_IP_ALIGN);
Olof Johansson73344862007-08-22 09:12:55 -0500558 memcpy(new_skb->data, skb->data, len);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500559 /* save the skb in buffer_info as good */
560 skb = new_skb;
561 }
562 /* else just continue with the old one */
563 } else
564 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600565
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500566 /* Need to zero it out since hardware doesn't, since the
567 * replenish loop uses it to tell when it's done.
568 */
569 RX_BUFF(mac, i) = 0;
570
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600571 skb_put(skb, len);
572
Olof Johansson26fcfa92007-08-22 09:12:59 -0500573 if (likely((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK)) {
Olof Johansson38bf3182007-08-22 09:13:24 -0500574 skb->ip_summed = CHECKSUM_UNNECESSARY;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500575 skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600576 XCT_MACRX_CSUM_S;
577 } else
578 skb->ip_summed = CHECKSUM_NONE;
579
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700580 mac->netdev->stats.rx_bytes += len;
581 mac->netdev->stats.rx_packets++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600582
Olof Johansson26fcfa92007-08-22 09:12:59 -0500583 skb->protocol = eth_type_trans(skb, mac->netdev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600584 netif_receive_skb(skb);
585
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500586 RX_RING(mac, n) = 0;
587 RX_RING(mac, n+1) = 0;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500588
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500589 n += 2;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600590 }
591
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500592 mac->rx->next_to_clean = n;
Olof Johansson928773c2007-09-26 16:25:06 -0500593 pasemi_mac_replenish_rx_ring(mac->netdev, limit-count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600594
595 spin_unlock(&mac->rx->lock);
596
597 return count;
598}
599
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500600/* Can't make this too large or we blow the kernel stack limits */
601#define TX_CLEAN_BATCHSIZE (128/MAX_SKB_FRAGS)
602
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600603static int pasemi_mac_clean_tx(struct pasemi_mac *mac)
604{
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500605 int i, j;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600606 struct pasemi_mac_buffer *info;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500607 unsigned int start, descr_count, buf_count, limit;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500608 unsigned int total_count;
Olof Johanssonca7e2352007-09-26 16:23:59 -0500609 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500610 struct sk_buff *skbs[TX_CLEAN_BATCHSIZE];
611 dma_addr_t dmas[TX_CLEAN_BATCHSIZE][MAX_SKB_FRAGS+1];
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600612
Olof Johansson02df6cf2007-08-22 09:13:03 -0500613 total_count = 0;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500614 limit = TX_CLEAN_BATCHSIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500615restart:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600616 spin_lock_irqsave(&mac->tx->lock, flags);
617
618 start = mac->tx->next_to_clean;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500619
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500620 buf_count = 0;
621 descr_count = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600622
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500623 for (i = start;
624 descr_count < limit && i < mac->tx->next_to_fill;
625 i += buf_count) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500626 u64 mactx = TX_RING(mac, i);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500627
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500628 if ((mactx & XCT_MACTX_E) ||
Olof Johansson69c29d82007-10-02 16:24:51 -0500629 (*mac->tx_status & PAS_STATUS_ERROR))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500630 pasemi_mac_tx_error(mac, mactx);
Olof Johansson69c29d82007-10-02 16:24:51 -0500631
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500632 if (unlikely(mactx & XCT_MACTX_O))
Olof Johansson02df6cf2007-08-22 09:13:03 -0500633 /* Not yet transmitted */
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600634 break;
635
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500636 info = &TX_RING_INFO(mac, i+1);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500637 skbs[descr_count] = info->skb;
638
639 buf_count = 2 + skb_shinfo(info->skb)->nr_frags;
640 for (j = 0; j <= skb_shinfo(info->skb)->nr_frags; j++)
641 dmas[descr_count][j] = TX_RING_INFO(mac, i+1+j).dma;
642
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600643
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600644 info->dma = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500645 TX_RING(mac, i) = 0;
646 TX_RING(mac, i+1) = 0;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500647 TX_RING_INFO(mac, i+1).skb = 0;
648 TX_RING_INFO(mac, i+1).dma = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500649
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500650 /* Since we always fill with an even number of entries, make
651 * sure we skip any unused one at the end as well.
652 */
653 if (buf_count & 1)
654 buf_count++;
655 descr_count++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600656 }
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500657 mac->tx->next_to_clean = i;
658
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600659 spin_unlock_irqrestore(&mac->tx->lock, flags);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500660 netif_wake_queue(mac->netdev);
661
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500662 for (i = 0; i < descr_count; i++)
663 pasemi_mac_unmap_tx_skb(mac, skbs[i], dmas[i]);
Olof Johansson02df6cf2007-08-22 09:13:03 -0500664
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500665 total_count += descr_count;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500666
667 /* If the batch was full, try to clean more */
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500668 if (descr_count == limit)
Olof Johansson02df6cf2007-08-22 09:13:03 -0500669 goto restart;
670
671 return total_count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600672}
673
674
675static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
676{
677 struct net_device *dev = data;
678 struct pasemi_mac *mac = netdev_priv(dev);
679 unsigned int reg;
680
Olof Johansson6dfa7522007-05-08 00:47:32 -0500681 if (!(*mac->rx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600682 return IRQ_NONE;
683
Olof Johansson6dfa7522007-05-08 00:47:32 -0500684 /* Don't reset packet count so it won't fire again but clear
685 * all others.
686 */
687
Olof Johansson6dfa7522007-05-08 00:47:32 -0500688 reg = 0;
689 if (*mac->rx_status & PAS_STATUS_SOFT)
690 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
691 if (*mac->rx_status & PAS_STATUS_ERROR)
692 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600693 if (*mac->rx_status & PAS_STATUS_TIMER)
694 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
695
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700696 netif_rx_schedule(dev, &mac->napi);
Olof Johansson6dfa7522007-05-08 00:47:32 -0500697
Olof Johanssona85b9422007-09-15 13:40:59 -0700698 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600699
700 return IRQ_HANDLED;
701}
702
703static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
704{
705 struct net_device *dev = data;
706 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson52a94352007-05-12 18:01:09 -0500707 unsigned int reg, pcnt;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600708
Olof Johansson6dfa7522007-05-08 00:47:32 -0500709 if (!(*mac->tx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600710 return IRQ_NONE;
711
712 pasemi_mac_clean_tx(mac);
713
Olof Johansson52a94352007-05-12 18:01:09 -0500714 pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
715
716 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson6dfa7522007-05-08 00:47:32 -0500717
718 if (*mac->tx_status & PAS_STATUS_SOFT)
719 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
720 if (*mac->tx_status & PAS_STATUS_ERROR)
721 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600722
Olof Johanssona85b9422007-09-15 13:40:59 -0700723 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600724
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600725 return IRQ_HANDLED;
726}
727
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500728static void pasemi_adjust_link(struct net_device *dev)
729{
730 struct pasemi_mac *mac = netdev_priv(dev);
731 int msg;
732 unsigned int flags;
733 unsigned int new_flags;
734
735 if (!mac->phydev->link) {
736 /* If no link, MAC speed settings don't matter. Just report
737 * link down and return.
738 */
739 if (mac->link && netif_msg_link(mac))
740 printk(KERN_INFO "%s: Link is down.\n", dev->name);
741
742 netif_carrier_off(dev);
743 mac->link = 0;
744
745 return;
746 } else
747 netif_carrier_on(dev);
748
Olof Johanssona85b9422007-09-15 13:40:59 -0700749 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500750 new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
751 PAS_MAC_CFG_PCFG_TSR_M);
752
753 if (!mac->phydev->duplex)
754 new_flags |= PAS_MAC_CFG_PCFG_HD;
755
756 switch (mac->phydev->speed) {
757 case 1000:
758 new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
759 PAS_MAC_CFG_PCFG_TSR_1G;
760 break;
761 case 100:
762 new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
763 PAS_MAC_CFG_PCFG_TSR_100M;
764 break;
765 case 10:
766 new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
767 PAS_MAC_CFG_PCFG_TSR_10M;
768 break;
769 default:
770 printk("Unsupported speed %d\n", mac->phydev->speed);
771 }
772
773 /* Print on link or speed/duplex change */
774 msg = mac->link != mac->phydev->link || flags != new_flags;
775
776 mac->duplex = mac->phydev->duplex;
777 mac->speed = mac->phydev->speed;
778 mac->link = mac->phydev->link;
779
780 if (new_flags != flags)
Olof Johanssona85b9422007-09-15 13:40:59 -0700781 write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500782
783 if (msg && netif_msg_link(mac))
784 printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
785 dev->name, mac->speed, mac->duplex ? "full" : "half");
786}
787
788static int pasemi_mac_phy_init(struct net_device *dev)
789{
790 struct pasemi_mac *mac = netdev_priv(dev);
791 struct device_node *dn, *phy_dn;
792 struct phy_device *phydev;
793 unsigned int phy_id;
794 const phandle *ph;
795 const unsigned int *prop;
796 struct resource r;
797 int ret;
798
799 dn = pci_device_to_OF_node(mac->pdev);
Linus Torvalds90287802007-05-08 11:57:17 -0700800 ph = of_get_property(dn, "phy-handle", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500801 if (!ph)
802 return -ENODEV;
803 phy_dn = of_find_node_by_phandle(*ph);
804
Linus Torvalds90287802007-05-08 11:57:17 -0700805 prop = of_get_property(phy_dn, "reg", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500806 ret = of_address_to_resource(phy_dn->parent, 0, &r);
807 if (ret)
808 goto err;
809
810 phy_id = *prop;
811 snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);
812
813 of_node_put(phy_dn);
814
815 mac->link = 0;
816 mac->speed = 0;
817 mac->duplex = -1;
818
819 phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);
820
821 if (IS_ERR(phydev)) {
822 printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
823 return PTR_ERR(phydev);
824 }
825
826 mac->phydev = phydev;
827
828 return 0;
829
830err:
831 of_node_put(phy_dn);
832 return -ENODEV;
833}
834
835
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600836static int pasemi_mac_open(struct net_device *dev)
837{
838 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson771f7402007-05-08 00:47:21 -0500839 int base_irq;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600840 unsigned int flags;
841 int ret;
842
843 /* enable rx section */
Olof Johanssona85b9422007-09-15 13:40:59 -0700844 write_dma_reg(mac, PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600845
846 /* enable tx section */
Olof Johanssona85b9422007-09-15 13:40:59 -0700847 write_dma_reg(mac, PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600848
849 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
850 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
851 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
852
Olof Johanssona85b9422007-09-15 13:40:59 -0700853 write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600854
Olof Johanssona85b9422007-09-15 13:40:59 -0700855 write_iob_reg(mac, PAS_IOB_DMA_RXCH_CFG(mac->dma_rxch),
856 PAS_IOB_DMA_RXCH_CFG_CNTTH(0));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600857
Olof Johanssona85b9422007-09-15 13:40:59 -0700858 write_iob_reg(mac, PAS_IOB_DMA_TXCH_CFG(mac->dma_txch),
Olof Johansson02df6cf2007-08-22 09:13:03 -0500859 PAS_IOB_DMA_TXCH_CFG_CNTTH(128));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600860
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500861 /* Clear out any residual packet count state from firmware */
862 pasemi_mac_restart_rx_intr(mac);
863 pasemi_mac_restart_tx_intr(mac);
864
Olof Johansson6dfa7522007-05-08 00:47:32 -0500865 /* 0xffffff is max value, about 16ms */
Olof Johanssona85b9422007-09-15 13:40:59 -0700866 write_iob_reg(mac, PAS_IOB_DMA_COM_TIMEOUTCFG,
867 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0xffffff));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600868
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600869 ret = pasemi_mac_setup_rx_resources(dev);
870 if (ret)
871 goto out_rx_resources;
872
873 ret = pasemi_mac_setup_tx_resources(dev);
874 if (ret)
875 goto out_tx_resources;
876
Olof Johanssona85b9422007-09-15 13:40:59 -0700877 write_mac_reg(mac, PAS_MAC_IPC_CHNL,
878 PAS_MAC_IPC_CHNL_DCHNO(mac->dma_rxch) |
879 PAS_MAC_IPC_CHNL_BCH(mac->dma_rxch));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600880
881 /* enable rx if */
Olof Johanssona85b9422007-09-15 13:40:59 -0700882 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
883 PAS_DMA_RXINT_RCMDSTA_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600884
885 /* enable rx channel */
Olof Johanssona85b9422007-09-15 13:40:59 -0700886 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
887 PAS_DMA_RXCHAN_CCMDSTA_EN |
888 PAS_DMA_RXCHAN_CCMDSTA_DU);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600889
890 /* enable tx channel */
Olof Johanssona85b9422007-09-15 13:40:59 -0700891 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
892 PAS_DMA_TXCHAN_TCMDSTA_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600893
Olof Johansson928773c2007-09-26 16:25:06 -0500894 pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600895
Olof Johansson36033762007-09-26 16:24:42 -0500896 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
897 PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
898
899 if (mac->type == MAC_TYPE_GMAC)
900 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
901 else
902 flags |= PAS_MAC_CFG_PCFG_TSR_10G | PAS_MAC_CFG_PCFG_SPD_10G;
903
904 /* Enable interface in MAC */
905 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
906
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500907 ret = pasemi_mac_phy_init(dev);
908 /* Some configs don't have PHYs (XAUI etc), so don't complain about
909 * failed init due to -ENODEV.
910 */
911 if (ret && ret != -ENODEV)
912 dev_warn(&mac->pdev->dev, "phy init failed: %d\n", ret);
913
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600914 netif_start_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700915 napi_enable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600916
Olof Johansson771f7402007-05-08 00:47:21 -0500917 /* Interrupts are a bit different for our DMA controller: While
918 * it's got one a regular PCI device header, the interrupt there
919 * is really the base of the range it's using. Each tx and rx
920 * channel has it's own interrupt source.
921 */
922
923 base_irq = virq_to_hw(mac->dma_pdev->irq);
924
925 mac->tx_irq = irq_create_mapping(NULL, base_irq + mac->dma_txch);
926 mac->rx_irq = irq_create_mapping(NULL, base_irq + 20 + mac->dma_txch);
927
928 ret = request_irq(mac->tx_irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600929 mac->tx->irq_name, dev);
930 if (ret) {
931 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500932 base_irq + mac->dma_txch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600933 goto out_tx_int;
934 }
935
Olof Johansson771f7402007-05-08 00:47:21 -0500936 ret = request_irq(mac->rx_irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600937 mac->rx->irq_name, dev);
938 if (ret) {
939 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500940 base_irq + 20 + mac->dma_rxch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600941 goto out_rx_int;
942 }
943
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500944 if (mac->phydev)
945 phy_start(mac->phydev);
946
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600947 return 0;
948
949out_rx_int:
Olof Johansson771f7402007-05-08 00:47:21 -0500950 free_irq(mac->tx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600951out_tx_int:
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700952 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600953 netif_stop_queue(dev);
954 pasemi_mac_free_tx_resources(dev);
955out_tx_resources:
956 pasemi_mac_free_rx_resources(dev);
957out_rx_resources:
958
959 return ret;
960}
961
962#define MAX_RETRIES 5000
963
964static int pasemi_mac_close(struct net_device *dev)
965{
966 struct pasemi_mac *mac = netdev_priv(dev);
967 unsigned int stat;
968 int retries;
969
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500970 if (mac->phydev) {
971 phy_stop(mac->phydev);
972 phy_disconnect(mac->phydev);
973 }
974
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600975 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700976 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600977
978 /* Clean out any pending buffers */
979 pasemi_mac_clean_tx(mac);
980 pasemi_mac_clean_rx(mac, RX_RING_SIZE);
981
982 /* Disable interface */
Olof Johanssona85b9422007-09-15 13:40:59 -0700983 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), PAS_DMA_TXCHAN_TCMDSTA_ST);
984 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), PAS_DMA_RXINT_RCMDSTA_ST);
985 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), PAS_DMA_RXCHAN_CCMDSTA_ST);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600986
987 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -0700988 stat = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
Olof Johansson0ce68c72007-04-28 15:36:40 -0500989 if (!(stat & PAS_DMA_TXCHAN_TCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600990 break;
991 cond_resched();
992 }
993
Olof Johansson0ce68c72007-04-28 15:36:40 -0500994 if (stat & PAS_DMA_TXCHAN_TCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600995 dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600996
997 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -0700998 stat = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
Olof Johansson0ce68c72007-04-28 15:36:40 -0500999 if (!(stat & PAS_DMA_RXCHAN_CCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001000 break;
1001 cond_resched();
1002 }
1003
Olof Johansson0ce68c72007-04-28 15:36:40 -05001004 if (stat & PAS_DMA_RXCHAN_CCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001005 dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001006
1007 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -07001008 stat = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
Olof Johansson0ce68c72007-04-28 15:36:40 -05001009 if (!(stat & PAS_DMA_RXINT_RCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001010 break;
1011 cond_resched();
1012 }
1013
Olof Johansson0ce68c72007-04-28 15:36:40 -05001014 if (stat & PAS_DMA_RXINT_RCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001015 dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001016
1017 /* Then, disable the channel. This must be done separately from
1018 * stopping, since you can't disable when active.
1019 */
1020
Olof Johanssona85b9422007-09-15 13:40:59 -07001021 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), 0);
1022 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), 0);
1023 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001024
Olof Johansson771f7402007-05-08 00:47:21 -05001025 free_irq(mac->tx_irq, dev);
1026 free_irq(mac->rx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001027
1028 /* Free resources */
1029 pasemi_mac_free_rx_resources(dev);
1030 pasemi_mac_free_tx_resources(dev);
1031
1032 return 0;
1033}
1034
1035static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
1036{
1037 struct pasemi_mac *mac = netdev_priv(dev);
1038 struct pasemi_mac_txring *txring;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001039 u64 dflags, mactx;
1040 dma_addr_t map[MAX_SKB_FRAGS+1];
1041 unsigned int map_size[MAX_SKB_FRAGS+1];
Olof Johanssonca7e2352007-09-26 16:23:59 -05001042 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001043 int i, nfrags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001044
1045 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_SS | XCT_MACTX_CRC_PAD;
1046
1047 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001048 const unsigned char *nh = skb_network_header(skb);
1049
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001050 switch (ip_hdr(skb)->protocol) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001051 case IPPROTO_TCP:
1052 dflags |= XCT_MACTX_CSUM_TCP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001053 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001054 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001055 break;
1056 case IPPROTO_UDP:
1057 dflags |= XCT_MACTX_CSUM_UDP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001058 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001059 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001060 break;
1061 }
1062 }
1063
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001064 nfrags = skb_shinfo(skb)->nr_frags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001065
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001066 map[0] = pci_map_single(mac->dma_pdev, skb->data, skb_headlen(skb),
1067 PCI_DMA_TODEVICE);
1068 map_size[0] = skb_headlen(skb);
1069 if (dma_mapping_error(map[0]))
1070 goto out_err_nolock;
1071
1072 for (i = 0; i < nfrags; i++) {
1073 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1074
1075 map[i+1] = pci_map_page(mac->dma_pdev, frag->page,
1076 frag->page_offset, frag->size,
1077 PCI_DMA_TODEVICE);
1078 map_size[i+1] = frag->size;
1079 if (dma_mapping_error(map[i+1])) {
1080 nfrags = i;
1081 goto out_err_nolock;
1082 }
1083 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001084
Olof Johansson26fcfa92007-08-22 09:12:59 -05001085 mactx = dflags | XCT_MACTX_LLEN(skb->len);
Olof Johansson26fcfa92007-08-22 09:12:59 -05001086
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001087 txring = mac->tx;
1088
1089 spin_lock_irqsave(&txring->lock, flags);
1090
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001091 if (RING_AVAIL(txring) <= nfrags+3) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001092 spin_unlock_irqrestore(&txring->lock, flags);
1093 pasemi_mac_clean_tx(mac);
Olof Johansson52a94352007-05-12 18:01:09 -05001094 pasemi_mac_restart_tx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001095 spin_lock_irqsave(&txring->lock, flags);
1096
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001097 if (RING_AVAIL(txring) <= nfrags+3) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001098 /* Still no room -- stop the queue and wait for tx
1099 * intr when there's room.
1100 */
1101 netif_stop_queue(dev);
1102 goto out_err;
1103 }
1104 }
1105
Olof Johanssonfc9e4d22007-10-02 16:25:53 -05001106 TX_RING(mac, txring->next_to_fill) = mactx;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001107 txring->next_to_fill++;
1108 TX_RING_INFO(mac, txring->next_to_fill).skb = skb;
1109 for (i = 0; i <= nfrags; i++) {
1110 TX_RING(mac, txring->next_to_fill+i) =
1111 XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]);
1112 TX_RING_INFO(mac, txring->next_to_fill+i).dma = map[i];
1113 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001114
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001115 /* We have to add an even number of 8-byte entries to the ring
1116 * even if the last one is unused. That means always an odd number
1117 * of pointers + one mactx descriptor.
1118 */
1119 if (nfrags & 1)
1120 nfrags++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001121
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001122 txring->next_to_fill += nfrags + 1;
1123
Olof Johanssonfc9e4d22007-10-02 16:25:53 -05001124
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001125 dev->stats.tx_packets++;
1126 dev->stats.tx_bytes += skb->len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001127
1128 spin_unlock_irqrestore(&txring->lock, flags);
1129
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001130 write_dma_reg(mac, PAS_DMA_TXCHAN_INCR(mac->dma_txch), (nfrags+2) >> 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001131
1132 return NETDEV_TX_OK;
1133
1134out_err:
1135 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001136out_err_nolock:
1137 while (nfrags--)
1138 pci_unmap_single(mac->dma_pdev, map[nfrags], map_size[nfrags],
1139 PCI_DMA_TODEVICE);
1140
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001141 return NETDEV_TX_BUSY;
1142}
1143
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001144static void pasemi_mac_set_rx_mode(struct net_device *dev)
1145{
1146 struct pasemi_mac *mac = netdev_priv(dev);
1147 unsigned int flags;
1148
Olof Johanssona85b9422007-09-15 13:40:59 -07001149 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001150
1151 /* Set promiscuous */
1152 if (dev->flags & IFF_PROMISC)
1153 flags |= PAS_MAC_CFG_PCFG_PR;
1154 else
1155 flags &= ~PAS_MAC_CFG_PCFG_PR;
1156
Olof Johanssona85b9422007-09-15 13:40:59 -07001157 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001158}
1159
1160
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001161static int pasemi_mac_poll(struct napi_struct *napi, int budget)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001162{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001163 struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
1164 struct net_device *dev = mac->netdev;
1165 int pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001166
Olof Johansson829185e2007-09-15 13:53:19 -07001167 pasemi_mac_clean_tx(mac);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001168 pkts = pasemi_mac_clean_rx(mac, budget);
1169 if (pkts < budget) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001170 /* all done, no more packets present */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001171 netif_rx_complete(dev, napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001172
Olof Johansson1b0335ea2007-05-08 00:47:26 -05001173 pasemi_mac_restart_rx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001174 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001175 return pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001176}
1177
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001178static void __iomem * __devinit map_onedev(struct pci_dev *p, int index)
1179{
1180 struct device_node *dn;
1181 void __iomem *ret;
1182
1183 dn = pci_device_to_OF_node(p);
1184 if (!dn)
1185 goto fallback;
1186
1187 ret = of_iomap(dn, index);
1188 if (!ret)
1189 goto fallback;
1190
1191 return ret;
1192fallback:
1193 /* This is hardcoded and ugly, but we have some firmware versions
1194 * that don't provide the register space in the device tree. Luckily
1195 * they are at well-known locations so we can just do the math here.
1196 */
1197 return ioremap(0xe0000000 + (p->devfn << 12), 0x2000);
1198}
1199
1200static int __devinit pasemi_mac_map_regs(struct pasemi_mac *mac)
1201{
1202 struct resource res;
1203 struct device_node *dn;
1204 int err;
1205
1206 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
1207 if (!mac->dma_pdev) {
1208 dev_err(&mac->pdev->dev, "Can't find DMA Controller\n");
1209 return -ENODEV;
1210 }
1211
1212 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
1213 if (!mac->iob_pdev) {
1214 dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n");
1215 return -ENODEV;
1216 }
1217
1218 mac->regs = map_onedev(mac->pdev, 0);
1219 mac->dma_regs = map_onedev(mac->dma_pdev, 0);
1220 mac->iob_regs = map_onedev(mac->iob_pdev, 0);
1221
1222 if (!mac->regs || !mac->dma_regs || !mac->iob_regs) {
1223 dev_err(&mac->pdev->dev, "Can't map registers\n");
1224 return -ENODEV;
1225 }
1226
1227 /* The dma status structure is located in the I/O bridge, and
1228 * is cache coherent.
1229 */
1230 if (!dma_status) {
1231 dn = pci_device_to_OF_node(mac->iob_pdev);
1232 if (dn)
1233 err = of_address_to_resource(dn, 1, &res);
1234 if (!dn || err) {
1235 /* Fallback for old firmware */
1236 res.start = 0xfd800000;
1237 res.end = res.start + 0x1000;
1238 }
1239 dma_status = __ioremap(res.start, res.end-res.start, 0);
1240 }
1241
1242 return 0;
1243}
1244
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001245static int __devinit
1246pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1247{
1248 static int index = 0;
1249 struct net_device *dev;
1250 struct pasemi_mac *mac;
1251 int err;
Joe Perches0795af52007-10-03 17:59:30 -07001252 DECLARE_MAC_BUF(mac_buf);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001253
1254 err = pci_enable_device(pdev);
1255 if (err)
1256 return err;
1257
1258 dev = alloc_etherdev(sizeof(struct pasemi_mac));
1259 if (dev == NULL) {
1260 dev_err(&pdev->dev,
1261 "pasemi_mac: Could not allocate ethernet device.\n");
1262 err = -ENOMEM;
1263 goto out_disable_device;
1264 }
1265
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001266 pci_set_drvdata(pdev, dev);
1267 SET_NETDEV_DEV(dev, &pdev->dev);
1268
1269 mac = netdev_priv(dev);
1270
1271 mac->pdev = pdev;
1272 mac->netdev = dev;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001273
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001274 netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
1275
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001276 dev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX | NETIF_F_SG;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001277
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001278 /* These should come out of the device tree eventually */
1279 mac->dma_txch = index;
1280 mac->dma_rxch = index;
1281
1282 /* We probe GMAC before XAUI, but the DMA interfaces are
1283 * in XAUI, GMAC order.
1284 */
1285 if (index < 4)
1286 mac->dma_if = index + 2;
1287 else
1288 mac->dma_if = index - 4;
1289 index++;
1290
1291 switch (pdev->device) {
1292 case 0xa005:
1293 mac->type = MAC_TYPE_GMAC;
1294 break;
1295 case 0xa006:
1296 mac->type = MAC_TYPE_XAUI;
1297 break;
1298 default:
1299 err = -ENODEV;
1300 goto out;
1301 }
1302
1303 /* get mac addr from device tree */
1304 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
1305 err = -ENODEV;
1306 goto out;
1307 }
1308 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
1309
1310 dev->open = pasemi_mac_open;
1311 dev->stop = pasemi_mac_close;
1312 dev->hard_start_xmit = pasemi_mac_start_tx;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001313 dev->set_multicast_list = pasemi_mac_set_rx_mode;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001314
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001315 err = pasemi_mac_map_regs(mac);
1316 if (err)
1317 goto out;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001318
1319 mac->rx_status = &dma_status->rx_sta[mac->dma_rxch];
1320 mac->tx_status = &dma_status->tx_sta[mac->dma_txch];
1321
Olof Johanssonceb51362007-05-08 00:47:49 -05001322 mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1323
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001324 /* Enable most messages by default */
1325 mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1326
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001327 err = register_netdev(dev);
1328
1329 if (err) {
1330 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1331 err);
1332 goto out;
Olof Johansson69c29d82007-10-02 16:24:51 -05001333 } else if netif_msg_probe(mac)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001334 printk(KERN_INFO "%s: PA Semi %s: intf %d, txch %d, rxch %d, "
Joe Perches0795af52007-10-03 17:59:30 -07001335 "hw addr %s\n",
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001336 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
1337 mac->dma_if, mac->dma_txch, mac->dma_rxch,
Joe Perches0795af52007-10-03 17:59:30 -07001338 print_mac(mac_buf, dev->dev_addr));
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001339
1340 return err;
1341
1342out:
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001343 if (mac->iob_pdev)
1344 pci_dev_put(mac->iob_pdev);
1345 if (mac->dma_pdev)
1346 pci_dev_put(mac->dma_pdev);
1347 if (mac->dma_regs)
1348 iounmap(mac->dma_regs);
1349 if (mac->iob_regs)
1350 iounmap(mac->iob_regs);
1351 if (mac->regs)
1352 iounmap(mac->regs);
1353
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001354 free_netdev(dev);
1355out_disable_device:
1356 pci_disable_device(pdev);
1357 return err;
1358
1359}
1360
1361static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1362{
1363 struct net_device *netdev = pci_get_drvdata(pdev);
1364 struct pasemi_mac *mac;
1365
1366 if (!netdev)
1367 return;
1368
1369 mac = netdev_priv(netdev);
1370
1371 unregister_netdev(netdev);
1372
1373 pci_disable_device(pdev);
1374 pci_dev_put(mac->dma_pdev);
1375 pci_dev_put(mac->iob_pdev);
1376
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001377 iounmap(mac->regs);
1378 iounmap(mac->dma_regs);
1379 iounmap(mac->iob_regs);
1380
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001381 pci_set_drvdata(pdev, NULL);
1382 free_netdev(netdev);
1383}
1384
1385static struct pci_device_id pasemi_mac_pci_tbl[] = {
1386 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1387 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
olof@lixom.netfd178252007-05-12 14:57:36 -05001388 { },
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001389};
1390
1391MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1392
1393static struct pci_driver pasemi_mac_driver = {
1394 .name = "pasemi_mac",
1395 .id_table = pasemi_mac_pci_tbl,
1396 .probe = pasemi_mac_probe,
1397 .remove = __devexit_p(pasemi_mac_remove),
1398};
1399
1400static void __exit pasemi_mac_cleanup_module(void)
1401{
1402 pci_unregister_driver(&pasemi_mac_driver);
1403 __iounmap(dma_status);
1404 dma_status = NULL;
1405}
1406
1407int pasemi_mac_init_module(void)
1408{
1409 return pci_register_driver(&pasemi_mac_driver);
1410}
1411
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001412module_init(pasemi_mac_init_module);
1413module_exit(pasemi_mac_cleanup_module);