blob: ef1ebb4b8af27af194bb1ba06d3f9e037fcae81f [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
Olof Johansson8dc121a2007-10-02 16:26:53 -050040/* We have our own align, since ppc64 in general has it at 0 because
41 * of design flaws in some of the server bridge chips. However, for
42 * PWRficient doing the unaligned copies is more expensive than doing
43 * unaligned DMA, so make sure the data is aligned instead.
44 */
45#define LOCAL_SKB_ALIGN 2
Olof Johanssonf5cd7872007-01-31 21:43:54 -060046
47/* TODO list
48 *
Olof Johanssonf5cd7872007-01-31 21:43:54 -060049 * - Multicast support
50 * - Large MTU support
Olof Johansson7ddeae22007-10-02 16:27:28 -050051 * - SW LRO
52 * - Multiqueue RX/TX
Olof Johanssonf5cd7872007-01-31 21:43:54 -060053 */
54
55
56/* Must be a power of two */
Olof Johanssonad5da102007-10-02 16:27:15 -050057#define RX_RING_SIZE 4096
58#define TX_RING_SIZE 4096
Olof Johanssonf5cd7872007-01-31 21:43:54 -060059
Olof Johanssonceb51362007-05-08 00:47:49 -050060#define DEFAULT_MSG_ENABLE \
61 (NETIF_MSG_DRV | \
62 NETIF_MSG_PROBE | \
63 NETIF_MSG_LINK | \
64 NETIF_MSG_TIMER | \
65 NETIF_MSG_IFDOWN | \
66 NETIF_MSG_IFUP | \
67 NETIF_MSG_RX_ERR | \
68 NETIF_MSG_TX_ERR)
69
Olof Johanssonfc9e4d22007-10-02 16:25:53 -050070#define TX_RING(mac, num) ((mac)->tx->ring[(num) & (TX_RING_SIZE-1)])
71#define TX_RING_INFO(mac, num) ((mac)->tx->ring_info[(num) & (TX_RING_SIZE-1)])
72#define RX_RING(mac, num) ((mac)->rx->ring[(num) & (RX_RING_SIZE-1)])
73#define RX_RING_INFO(mac, num) ((mac)->rx->ring_info[(num) & (RX_RING_SIZE-1)])
Olof Johanssonf5cd7872007-01-31 21:43:54 -060074#define RX_BUFF(mac, num) ((mac)->rx->buffers[(num) & (RX_RING_SIZE-1)])
75
Olof Johansson021fa222007-08-22 09:13:11 -050076#define RING_USED(ring) (((ring)->next_to_fill - (ring)->next_to_clean) \
77 & ((ring)->size - 1))
78#define RING_AVAIL(ring) ((ring->size) - RING_USED(ring))
79
Olof Johanssonf5cd7872007-01-31 21:43:54 -060080#define BUF_SIZE 1646 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
81
Olof Johanssonceb51362007-05-08 00:47:49 -050082MODULE_LICENSE("GPL");
83MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
84MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
85
86static int debug = -1; /* -1 == use DEFAULT_MSG_ENABLE as value */
87module_param(debug, int, 0);
88MODULE_PARM_DESC(debug, "PA Semi MAC bitmapped debugging message enable value");
Olof Johanssonf5cd7872007-01-31 21:43:54 -060089
90static struct pasdma_status *dma_status;
91
Olof Johanssona85b9422007-09-15 13:40:59 -070092static void write_iob_reg(struct pasemi_mac *mac, unsigned int reg,
93 unsigned int val)
94{
Olof Johanssonb6e05a12007-09-15 13:44:07 -070095 out_le32(mac->iob_regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -070096}
97
98static unsigned int read_mac_reg(struct pasemi_mac *mac, unsigned int reg)
99{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700100 return in_le32(mac->regs+reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700101}
102
103static void write_mac_reg(struct pasemi_mac *mac, unsigned int reg,
104 unsigned int val)
105{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700106 out_le32(mac->regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700107}
108
109static unsigned int read_dma_reg(struct pasemi_mac *mac, unsigned int reg)
110{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700111 return in_le32(mac->dma_regs+reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700112}
113
114static void write_dma_reg(struct pasemi_mac *mac, unsigned int reg,
115 unsigned int val)
116{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700117 out_le32(mac->dma_regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700118}
119
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600120static int pasemi_get_mac_addr(struct pasemi_mac *mac)
121{
122 struct pci_dev *pdev = mac->pdev;
123 struct device_node *dn = pci_device_to_OF_node(pdev);
olof@lixom.net1af7f052007-05-12 14:57:46 -0500124 int len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600125 const u8 *maddr;
126 u8 addr[6];
127
128 if (!dn) {
129 dev_dbg(&pdev->dev,
130 "No device node for mac, not configuring\n");
131 return -ENOENT;
132 }
133
olof@lixom.net1af7f052007-05-12 14:57:46 -0500134 maddr = of_get_property(dn, "local-mac-address", &len);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500135
olof@lixom.net1af7f052007-05-12 14:57:46 -0500136 if (maddr && len == 6) {
137 memcpy(mac->mac_addr, maddr, 6);
138 return 0;
139 }
140
141 /* Some old versions of firmware mistakenly uses mac-address
142 * (and as a string) instead of a byte array in local-mac-address.
143 */
144
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500145 if (maddr == NULL)
Linus Torvalds90287802007-05-08 11:57:17 -0700146 maddr = of_get_property(dn, "mac-address", NULL);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500147
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600148 if (maddr == NULL) {
149 dev_warn(&pdev->dev,
150 "no mac address in device tree, not configuring\n");
151 return -ENOENT;
152 }
153
olof@lixom.net1af7f052007-05-12 14:57:46 -0500154
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600155 if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0],
156 &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) {
157 dev_warn(&pdev->dev,
158 "can't parse mac address, not configuring\n");
159 return -EINVAL;
160 }
161
olof@lixom.net1af7f052007-05-12 14:57:46 -0500162 memcpy(mac->mac_addr, addr, 6);
163
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600164 return 0;
165}
166
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500167static int pasemi_mac_unmap_tx_skb(struct pasemi_mac *mac,
168 struct sk_buff *skb,
169 dma_addr_t *dmas)
170{
171 int f;
172 int nfrags = skb_shinfo(skb)->nr_frags;
173
174 pci_unmap_single(mac->dma_pdev, dmas[0], skb_headlen(skb),
175 PCI_DMA_TODEVICE);
176
177 for (f = 0; f < nfrags; f++) {
178 skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
179
180 pci_unmap_page(mac->dma_pdev, dmas[f+1], frag->size,
181 PCI_DMA_TODEVICE);
182 }
183 dev_kfree_skb_irq(skb);
184
185 /* Freed descriptor slot + main SKB ptr + nfrags additional ptrs,
186 * aligned up to a power of 2
187 */
188 return (nfrags + 3) & ~1;
189}
190
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600191static int pasemi_mac_setup_rx_resources(struct net_device *dev)
192{
193 struct pasemi_mac_rxring *ring;
194 struct pasemi_mac *mac = netdev_priv(dev);
195 int chan_id = mac->dma_rxch;
196
197 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
198
199 if (!ring)
200 goto out_ring;
201
202 spin_lock_init(&ring->lock);
203
Olof Johansson021fa222007-08-22 09:13:11 -0500204 ring->size = RX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500205 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600206 RX_RING_SIZE, GFP_KERNEL);
207
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500208 if (!ring->ring_info)
209 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600210
211 /* Allocate descriptors */
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500212 ring->ring = dma_alloc_coherent(&mac->dma_pdev->dev,
213 RX_RING_SIZE * sizeof(u64),
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600214 &ring->dma, GFP_KERNEL);
215
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500216 if (!ring->ring)
217 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600218
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500219 memset(ring->ring, 0, RX_RING_SIZE * sizeof(u64));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600220
221 ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev,
222 RX_RING_SIZE * sizeof(u64),
223 &ring->buf_dma, GFP_KERNEL);
224 if (!ring->buffers)
225 goto out_buffers;
226
227 memset(ring->buffers, 0, RX_RING_SIZE * sizeof(u64));
228
Olof Johanssona85b9422007-09-15 13:40:59 -0700229 write_dma_reg(mac, PAS_DMA_RXCHAN_BASEL(chan_id), PAS_DMA_RXCHAN_BASEL_BRBL(ring->dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600230
Olof Johanssona85b9422007-09-15 13:40:59 -0700231 write_dma_reg(mac, PAS_DMA_RXCHAN_BASEU(chan_id),
232 PAS_DMA_RXCHAN_BASEU_BRBH(ring->dma >> 32) |
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500233 PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600234
Olof Johanssona85b9422007-09-15 13:40:59 -0700235 write_dma_reg(mac, PAS_DMA_RXCHAN_CFG(chan_id),
Olof Johanssonc0efd522007-08-22 09:12:52 -0500236 PAS_DMA_RXCHAN_CFG_HBU(2));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600237
Olof Johanssona85b9422007-09-15 13:40:59 -0700238 write_dma_reg(mac, PAS_DMA_RXINT_BASEL(mac->dma_if),
239 PAS_DMA_RXINT_BASEL_BRBL(__pa(ring->buffers)));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600240
Olof Johanssona85b9422007-09-15 13:40:59 -0700241 write_dma_reg(mac, PAS_DMA_RXINT_BASEU(mac->dma_if),
242 PAS_DMA_RXINT_BASEU_BRBH(__pa(ring->buffers) >> 32) |
243 PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600244
Olof Johanssonc0efd522007-08-22 09:12:52 -0500245 write_dma_reg(mac, PAS_DMA_RXINT_CFG(mac->dma_if),
Olof Johansson9a50beb2007-10-02 16:26:30 -0500246 PAS_DMA_RXINT_CFG_DHL(3) |
247 PAS_DMA_RXINT_CFG_L2 |
248 PAS_DMA_RXINT_CFG_LW);
Olof Johanssonc0efd522007-08-22 09:12:52 -0500249
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600250 ring->next_to_fill = 0;
251 ring->next_to_clean = 0;
252
253 snprintf(ring->irq_name, sizeof(ring->irq_name),
254 "%s rx", dev->name);
255 mac->rx = ring;
256
257 return 0;
258
259out_buffers:
260 dma_free_coherent(&mac->dma_pdev->dev,
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500261 RX_RING_SIZE * sizeof(u64),
262 mac->rx->ring, mac->rx->dma);
263out_ring_desc:
264 kfree(ring->ring_info);
265out_ring_info:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600266 kfree(ring);
267out_ring:
268 return -ENOMEM;
269}
270
271
272static int pasemi_mac_setup_tx_resources(struct net_device *dev)
273{
274 struct pasemi_mac *mac = netdev_priv(dev);
275 u32 val;
276 int chan_id = mac->dma_txch;
277 struct pasemi_mac_txring *ring;
278
279 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
280 if (!ring)
281 goto out_ring;
282
283 spin_lock_init(&ring->lock);
284
Olof Johansson021fa222007-08-22 09:13:11 -0500285 ring->size = TX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500286 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600287 TX_RING_SIZE, GFP_KERNEL);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500288 if (!ring->ring_info)
289 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600290
291 /* Allocate descriptors */
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500292 ring->ring = dma_alloc_coherent(&mac->dma_pdev->dev,
293 TX_RING_SIZE * sizeof(u64),
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600294 &ring->dma, GFP_KERNEL);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500295 if (!ring->ring)
296 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600297
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500298 memset(ring->ring, 0, TX_RING_SIZE * sizeof(u64));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600299
Olof Johanssona85b9422007-09-15 13:40:59 -0700300 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEL(chan_id),
301 PAS_DMA_TXCHAN_BASEL_BRBL(ring->dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600302 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->dma >> 32);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500303 val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 3);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600304
Olof Johanssona85b9422007-09-15 13:40:59 -0700305 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEU(chan_id), val);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600306
Olof Johanssona85b9422007-09-15 13:40:59 -0700307 write_dma_reg(mac, PAS_DMA_TXCHAN_CFG(chan_id),
308 PAS_DMA_TXCHAN_CFG_TY_IFACE |
309 PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
310 PAS_DMA_TXCHAN_CFG_UP |
311 PAS_DMA_TXCHAN_CFG_WT(2));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600312
Olof Johansson021fa222007-08-22 09:13:11 -0500313 ring->next_to_fill = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600314 ring->next_to_clean = 0;
315
316 snprintf(ring->irq_name, sizeof(ring->irq_name),
317 "%s tx", dev->name);
318 mac->tx = ring;
319
320 return 0;
321
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500322out_ring_desc:
323 kfree(ring->ring_info);
324out_ring_info:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600325 kfree(ring);
326out_ring:
327 return -ENOMEM;
328}
329
330static void pasemi_mac_free_tx_resources(struct net_device *dev)
331{
332 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500333 unsigned int i, j;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600334 struct pasemi_mac_buffer *info;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500335 dma_addr_t dmas[MAX_SKB_FRAGS+1];
336 int freed;
Olof Johanssonad5da102007-10-02 16:27:15 -0500337 int start, limit;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600338
Olof Johanssonad5da102007-10-02 16:27:15 -0500339 start = mac->tx->next_to_clean;
340 limit = mac->tx->next_to_fill;
341
342 /* Compensate for when fill has wrapped and clean has not */
343 if (start > limit)
344 limit += TX_RING_SIZE;
345
346 for (i = start; i < limit; i += freed) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500347 info = &TX_RING_INFO(mac, i+1);
348 if (info->dma && info->skb) {
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500349 for (j = 0; j <= skb_shinfo(info->skb)->nr_frags; j++)
350 dmas[j] = TX_RING_INFO(mac, i+1+j).dma;
351 freed = pasemi_mac_unmap_tx_skb(mac, info->skb, dmas);
352 } else
353 freed = 2;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600354 }
355
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500356 for (i = 0; i < TX_RING_SIZE; i++)
357 TX_RING(mac, i) = 0;
358
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600359 dma_free_coherent(&mac->dma_pdev->dev,
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500360 TX_RING_SIZE * sizeof(u64),
361 mac->tx->ring, mac->tx->dma);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600362
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500363 kfree(mac->tx->ring_info);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600364 kfree(mac->tx);
365 mac->tx = NULL;
366}
367
368static void pasemi_mac_free_rx_resources(struct net_device *dev)
369{
370 struct pasemi_mac *mac = netdev_priv(dev);
371 unsigned int i;
372 struct pasemi_mac_buffer *info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600373
374 for (i = 0; i < RX_RING_SIZE; i++) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500375 info = &RX_RING_INFO(mac, i);
376 if (info->skb && info->dma) {
377 pci_unmap_single(mac->dma_pdev,
378 info->dma,
379 info->skb->len,
380 PCI_DMA_FROMDEVICE);
381 dev_kfree_skb_any(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600382 }
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500383 info->dma = 0;
384 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600385 }
386
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500387 for (i = 0; i < RX_RING_SIZE; i++)
388 RX_RING(mac, i) = 0;
389
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600390 dma_free_coherent(&mac->dma_pdev->dev,
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500391 RX_RING_SIZE * sizeof(u64),
392 mac->rx->ring, mac->rx->dma);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600393
394 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
395 mac->rx->buffers, mac->rx->buf_dma);
396
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500397 kfree(mac->rx->ring_info);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600398 kfree(mac->rx);
399 mac->rx = NULL;
400}
401
Olof Johansson928773c2007-09-26 16:25:06 -0500402static void pasemi_mac_replenish_rx_ring(struct net_device *dev, int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600403{
404 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600405 int start = mac->rx->next_to_fill;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500406 unsigned int fill, count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600407
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500408 if (limit <= 0)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600409 return;
410
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500411 fill = start;
Olof Johansson928773c2007-09-26 16:25:06 -0500412 for (count = 0; count < limit; count++) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500413 struct pasemi_mac_buffer *info = &RX_RING_INFO(mac, fill);
414 u64 *buff = &RX_BUFF(mac, fill);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600415 struct sk_buff *skb;
416 dma_addr_t dma;
417
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500418 /* Entry in use? */
419 WARN_ON(*buff);
420
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500421 /* skb might still be in there for recycle on short receives */
422 if (info->skb)
423 skb = info->skb;
Olof Johansson8dc121a2007-10-02 16:26:53 -0500424 else {
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500425 skb = dev_alloc_skb(BUF_SIZE);
Olof Johansson8dc121a2007-10-02 16:26:53 -0500426 skb_reserve(skb, LOCAL_SKB_ALIGN);
427 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600428
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500429 if (unlikely(!skb))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600430 break;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600431
Olof Johansson8dc121a2007-10-02 16:26:53 -0500432 dma = pci_map_single(mac->dma_pdev, skb->data,
433 BUF_SIZE - LOCAL_SKB_ALIGN,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600434 PCI_DMA_FROMDEVICE);
435
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500436 if (unlikely(dma_mapping_error(dma))) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600437 dev_kfree_skb_irq(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600438 break;
439 }
440
441 info->skb = skb;
442 info->dma = dma;
443 *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500444 fill++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600445 }
446
447 wmb();
448
Olof Johansson928773c2007-09-26 16:25:06 -0500449 write_dma_reg(mac, PAS_DMA_RXCHAN_INCR(mac->dma_rxch), count);
450 write_dma_reg(mac, PAS_DMA_RXINT_INCR(mac->dma_if), count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600451
Olof Johansson928773c2007-09-26 16:25:06 -0500452 mac->rx->next_to_fill += count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600453}
454
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500455static void pasemi_mac_restart_rx_intr(struct pasemi_mac *mac)
456{
Olof Johansson52a94352007-05-12 18:01:09 -0500457 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500458 /* Re-enable packet count interrupts: finally
459 * ack the packet count interrupt we got in rx_intr.
460 */
461
Olof Johansson52a94352007-05-12 18:01:09 -0500462 pcnt = *mac->rx_status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500463
Olof Johansson52a94352007-05-12 18:01:09 -0500464 reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500465
Olof Johanssona85b9422007-09-15 13:40:59 -0700466 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500467}
468
469static void pasemi_mac_restart_tx_intr(struct pasemi_mac *mac)
470{
Olof Johansson52a94352007-05-12 18:01:09 -0500471 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500472
473 /* Re-enable packet count interrupts */
Olof Johansson52a94352007-05-12 18:01:09 -0500474 pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500475
Olof Johansson52a94352007-05-12 18:01:09 -0500476 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500477
Olof Johanssona85b9422007-09-15 13:40:59 -0700478 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500479}
480
481
Olof Johansson69c29d82007-10-02 16:24:51 -0500482static inline void pasemi_mac_rx_error(struct pasemi_mac *mac, u64 macrx)
483{
484 unsigned int rcmdsta, ccmdsta;
485
486 if (!netif_msg_rx_err(mac))
487 return;
488
489 rcmdsta = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
490 ccmdsta = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
491
492 printk(KERN_ERR "pasemi_mac: rx error. macrx %016lx, rx status %lx\n",
493 macrx, *mac->rx_status);
494
495 printk(KERN_ERR "pasemi_mac: rcmdsta %08x ccmdsta %08x\n",
496 rcmdsta, ccmdsta);
497}
498
499static inline void pasemi_mac_tx_error(struct pasemi_mac *mac, u64 mactx)
500{
501 unsigned int cmdsta;
502
503 if (!netif_msg_tx_err(mac))
504 return;
505
506 cmdsta = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
507
508 printk(KERN_ERR "pasemi_mac: tx error. mactx 0x%016lx, "\
509 "tx status 0x%016lx\n", mactx, *mac->tx_status);
510
511 printk(KERN_ERR "pasemi_mac: tcmdsta 0x%08x\n", cmdsta);
512}
513
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600514static int pasemi_mac_clean_rx(struct pasemi_mac *mac, int limit)
515{
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500516 unsigned int n;
517 int count;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500518 struct pasemi_mac_buffer *info;
519 struct sk_buff *skb;
520 unsigned int i, len;
521 u64 macrx;
522 dma_addr_t dma;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600523
524 spin_lock(&mac->rx->lock);
525
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500526 n = mac->rx->next_to_clean;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600527
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500528 for (count = limit; count; count--) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500529 macrx = RX_RING(mac, n);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600530
Olof Johansson69c29d82007-10-02 16:24:51 -0500531 if ((macrx & XCT_MACRX_E) ||
532 (*mac->rx_status & PAS_STATUS_ERROR))
533 pasemi_mac_rx_error(mac, macrx);
534
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500535 if (!(macrx & XCT_MACRX_O))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600536 break;
537
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600538 info = NULL;
539
540 /* We have to scan for our skb since there's no way
541 * to back-map them from the descriptor, and if we
542 * have several receive channels then they might not
543 * show up in the same order as they were put on the
544 * interface ring.
545 */
546
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500547 dma = (RX_RING(mac, n+1) & XCT_PTR_ADDR_M);
548 for (i = mac->rx->next_to_fill;
549 i < (mac->rx->next_to_fill + RX_RING_SIZE);
550 i++) {
551 info = &RX_RING_INFO(mac, i);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600552 if (info->dma == dma)
553 break;
554 }
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500555
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500556 skb = info->skb;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600557
Olof Johanssonad5da102007-10-02 16:27:15 -0500558 prefetch(skb);
559 prefetch(&skb->data_len);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600560
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500561 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600562
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500563 if (len < 256) {
Olof Johansson8dc121a2007-10-02 16:26:53 -0500564 struct sk_buff *new_skb;
565
566 new_skb = netdev_alloc_skb(mac->netdev,
567 len + LOCAL_SKB_ALIGN);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500568 if (new_skb) {
Olof Johansson8dc121a2007-10-02 16:26:53 -0500569 skb_reserve(new_skb, LOCAL_SKB_ALIGN);
Olof Johansson73344862007-08-22 09:12:55 -0500570 memcpy(new_skb->data, skb->data, len);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500571 /* save the skb in buffer_info as good */
572 skb = new_skb;
573 }
574 /* else just continue with the old one */
575 } else
576 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600577
Olof Johanssonad5da102007-10-02 16:27:15 -0500578 pci_unmap_single(mac->dma_pdev, dma, len, PCI_DMA_FROMDEVICE);
579
580 info->dma = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500581
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600582 skb_put(skb, len);
583
Olof Johansson26fcfa92007-08-22 09:12:59 -0500584 if (likely((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK)) {
Olof Johansson38bf3182007-08-22 09:13:24 -0500585 skb->ip_summed = CHECKSUM_UNNECESSARY;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500586 skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600587 XCT_MACRX_CSUM_S;
588 } else
589 skb->ip_summed = CHECKSUM_NONE;
590
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700591 mac->netdev->stats.rx_bytes += len;
592 mac->netdev->stats.rx_packets++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600593
Olof Johansson26fcfa92007-08-22 09:12:59 -0500594 skb->protocol = eth_type_trans(skb, mac->netdev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600595 netif_receive_skb(skb);
596
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500597 RX_RING(mac, n) = 0;
598 RX_RING(mac, n+1) = 0;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500599
Olof Johanssonad5da102007-10-02 16:27:15 -0500600 /* Need to zero it out since hardware doesn't, since the
601 * replenish loop uses it to tell when it's done.
602 */
603 RX_BUFF(mac, i) = 0;
604
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500605 n += 2;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600606 }
607
Olof Johansson9a50beb2007-10-02 16:26:30 -0500608 if (n > RX_RING_SIZE) {
609 /* Errata 5971 workaround: L2 target of headers */
610 write_iob_reg(mac, PAS_IOB_COM_PKTHDRCNT, 0);
611 n &= (RX_RING_SIZE-1);
612 }
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500613 mac->rx->next_to_clean = n;
Olof Johansson928773c2007-09-26 16:25:06 -0500614 pasemi_mac_replenish_rx_ring(mac->netdev, limit-count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600615
616 spin_unlock(&mac->rx->lock);
617
618 return count;
619}
620
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500621/* Can't make this too large or we blow the kernel stack limits */
622#define TX_CLEAN_BATCHSIZE (128/MAX_SKB_FRAGS)
623
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600624static int pasemi_mac_clean_tx(struct pasemi_mac *mac)
625{
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500626 int i, j;
Olof Johanssonad5da102007-10-02 16:27:15 -0500627 unsigned int start, descr_count, buf_count, batch_limit;
628 unsigned int ring_limit;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500629 unsigned int total_count;
Olof Johanssonca7e2352007-09-26 16:23:59 -0500630 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500631 struct sk_buff *skbs[TX_CLEAN_BATCHSIZE];
632 dma_addr_t dmas[TX_CLEAN_BATCHSIZE][MAX_SKB_FRAGS+1];
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600633
Olof Johansson02df6cf2007-08-22 09:13:03 -0500634 total_count = 0;
Olof Johanssonad5da102007-10-02 16:27:15 -0500635 batch_limit = TX_CLEAN_BATCHSIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500636restart:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600637 spin_lock_irqsave(&mac->tx->lock, flags);
638
639 start = mac->tx->next_to_clean;
Olof Johanssonad5da102007-10-02 16:27:15 -0500640 ring_limit = mac->tx->next_to_fill;
641
642 /* Compensate for when fill has wrapped but clean has not */
643 if (start > ring_limit)
644 ring_limit += TX_RING_SIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500645
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500646 buf_count = 0;
647 descr_count = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600648
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500649 for (i = start;
Olof Johanssonad5da102007-10-02 16:27:15 -0500650 descr_count < batch_limit && i < ring_limit;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500651 i += buf_count) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500652 u64 mactx = TX_RING(mac, i);
Olof Johanssonad5da102007-10-02 16:27:15 -0500653 struct sk_buff *skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500654
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500655 if ((mactx & XCT_MACTX_E) ||
Olof Johansson69c29d82007-10-02 16:24:51 -0500656 (*mac->tx_status & PAS_STATUS_ERROR))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500657 pasemi_mac_tx_error(mac, mactx);
Olof Johansson69c29d82007-10-02 16:24:51 -0500658
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500659 if (unlikely(mactx & XCT_MACTX_O))
Olof Johansson02df6cf2007-08-22 09:13:03 -0500660 /* Not yet transmitted */
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600661 break;
662
Olof Johanssonad5da102007-10-02 16:27:15 -0500663 skb = TX_RING_INFO(mac, i+1).skb;
664 skbs[descr_count] = skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500665
Olof Johanssonad5da102007-10-02 16:27:15 -0500666 buf_count = 2 + skb_shinfo(skb)->nr_frags;
667 for (j = 0; j <= skb_shinfo(skb)->nr_frags; j++)
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500668 dmas[descr_count][j] = TX_RING_INFO(mac, i+1+j).dma;
669
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500670 TX_RING(mac, i) = 0;
671 TX_RING(mac, i+1) = 0;
672
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500673 /* Since we always fill with an even number of entries, make
674 * sure we skip any unused one at the end as well.
675 */
676 if (buf_count & 1)
677 buf_count++;
678 descr_count++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600679 }
Olof Johanssonad5da102007-10-02 16:27:15 -0500680 mac->tx->next_to_clean = i & (TX_RING_SIZE-1);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500681
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600682 spin_unlock_irqrestore(&mac->tx->lock, flags);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500683 netif_wake_queue(mac->netdev);
684
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500685 for (i = 0; i < descr_count; i++)
686 pasemi_mac_unmap_tx_skb(mac, skbs[i], dmas[i]);
Olof Johansson02df6cf2007-08-22 09:13:03 -0500687
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500688 total_count += descr_count;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500689
690 /* If the batch was full, try to clean more */
Olof Johanssonad5da102007-10-02 16:27:15 -0500691 if (descr_count == batch_limit)
Olof Johansson02df6cf2007-08-22 09:13:03 -0500692 goto restart;
693
694 return total_count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600695}
696
697
698static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
699{
700 struct net_device *dev = data;
701 struct pasemi_mac *mac = netdev_priv(dev);
702 unsigned int reg;
703
Olof Johansson6dfa7522007-05-08 00:47:32 -0500704 if (!(*mac->rx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600705 return IRQ_NONE;
706
Olof Johansson6dfa7522007-05-08 00:47:32 -0500707 /* Don't reset packet count so it won't fire again but clear
708 * all others.
709 */
710
Olof Johansson6dfa7522007-05-08 00:47:32 -0500711 reg = 0;
712 if (*mac->rx_status & PAS_STATUS_SOFT)
713 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
714 if (*mac->rx_status & PAS_STATUS_ERROR)
715 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600716 if (*mac->rx_status & PAS_STATUS_TIMER)
717 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
718
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700719 netif_rx_schedule(dev, &mac->napi);
Olof Johansson6dfa7522007-05-08 00:47:32 -0500720
Olof Johanssona85b9422007-09-15 13:40:59 -0700721 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600722
723 return IRQ_HANDLED;
724}
725
726static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
727{
728 struct net_device *dev = data;
729 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson52a94352007-05-12 18:01:09 -0500730 unsigned int reg, pcnt;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600731
Olof Johansson6dfa7522007-05-08 00:47:32 -0500732 if (!(*mac->tx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600733 return IRQ_NONE;
734
735 pasemi_mac_clean_tx(mac);
736
Olof Johansson52a94352007-05-12 18:01:09 -0500737 pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
738
739 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson6dfa7522007-05-08 00:47:32 -0500740
741 if (*mac->tx_status & PAS_STATUS_SOFT)
742 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
743 if (*mac->tx_status & PAS_STATUS_ERROR)
744 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600745
Olof Johanssona85b9422007-09-15 13:40:59 -0700746 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600747
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600748 return IRQ_HANDLED;
749}
750
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500751static void pasemi_adjust_link(struct net_device *dev)
752{
753 struct pasemi_mac *mac = netdev_priv(dev);
754 int msg;
755 unsigned int flags;
756 unsigned int new_flags;
757
758 if (!mac->phydev->link) {
759 /* If no link, MAC speed settings don't matter. Just report
760 * link down and return.
761 */
762 if (mac->link && netif_msg_link(mac))
763 printk(KERN_INFO "%s: Link is down.\n", dev->name);
764
765 netif_carrier_off(dev);
766 mac->link = 0;
767
768 return;
769 } else
770 netif_carrier_on(dev);
771
Olof Johanssona85b9422007-09-15 13:40:59 -0700772 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500773 new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
774 PAS_MAC_CFG_PCFG_TSR_M);
775
776 if (!mac->phydev->duplex)
777 new_flags |= PAS_MAC_CFG_PCFG_HD;
778
779 switch (mac->phydev->speed) {
780 case 1000:
781 new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
782 PAS_MAC_CFG_PCFG_TSR_1G;
783 break;
784 case 100:
785 new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
786 PAS_MAC_CFG_PCFG_TSR_100M;
787 break;
788 case 10:
789 new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
790 PAS_MAC_CFG_PCFG_TSR_10M;
791 break;
792 default:
793 printk("Unsupported speed %d\n", mac->phydev->speed);
794 }
795
796 /* Print on link or speed/duplex change */
797 msg = mac->link != mac->phydev->link || flags != new_flags;
798
799 mac->duplex = mac->phydev->duplex;
800 mac->speed = mac->phydev->speed;
801 mac->link = mac->phydev->link;
802
803 if (new_flags != flags)
Olof Johanssona85b9422007-09-15 13:40:59 -0700804 write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500805
806 if (msg && netif_msg_link(mac))
807 printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
808 dev->name, mac->speed, mac->duplex ? "full" : "half");
809}
810
811static int pasemi_mac_phy_init(struct net_device *dev)
812{
813 struct pasemi_mac *mac = netdev_priv(dev);
814 struct device_node *dn, *phy_dn;
815 struct phy_device *phydev;
816 unsigned int phy_id;
817 const phandle *ph;
818 const unsigned int *prop;
819 struct resource r;
820 int ret;
821
822 dn = pci_device_to_OF_node(mac->pdev);
Linus Torvalds90287802007-05-08 11:57:17 -0700823 ph = of_get_property(dn, "phy-handle", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500824 if (!ph)
825 return -ENODEV;
826 phy_dn = of_find_node_by_phandle(*ph);
827
Linus Torvalds90287802007-05-08 11:57:17 -0700828 prop = of_get_property(phy_dn, "reg", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500829 ret = of_address_to_resource(phy_dn->parent, 0, &r);
830 if (ret)
831 goto err;
832
833 phy_id = *prop;
834 snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);
835
836 of_node_put(phy_dn);
837
838 mac->link = 0;
839 mac->speed = 0;
840 mac->duplex = -1;
841
842 phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);
843
844 if (IS_ERR(phydev)) {
845 printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
846 return PTR_ERR(phydev);
847 }
848
849 mac->phydev = phydev;
850
851 return 0;
852
853err:
854 of_node_put(phy_dn);
855 return -ENODEV;
856}
857
858
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600859static int pasemi_mac_open(struct net_device *dev)
860{
861 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson771f7402007-05-08 00:47:21 -0500862 int base_irq;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600863 unsigned int flags;
864 int ret;
865
866 /* enable rx section */
Olof Johanssona85b9422007-09-15 13:40:59 -0700867 write_dma_reg(mac, PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600868
869 /* enable tx section */
Olof Johanssona85b9422007-09-15 13:40:59 -0700870 write_dma_reg(mac, PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600871
872 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
873 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
874 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
875
Olof Johanssona85b9422007-09-15 13:40:59 -0700876 write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600877
Olof Johanssona85b9422007-09-15 13:40:59 -0700878 write_iob_reg(mac, PAS_IOB_DMA_RXCH_CFG(mac->dma_rxch),
879 PAS_IOB_DMA_RXCH_CFG_CNTTH(0));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600880
Olof Johanssona85b9422007-09-15 13:40:59 -0700881 write_iob_reg(mac, PAS_IOB_DMA_TXCH_CFG(mac->dma_txch),
Olof Johansson02df6cf2007-08-22 09:13:03 -0500882 PAS_IOB_DMA_TXCH_CFG_CNTTH(128));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600883
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500884 /* Clear out any residual packet count state from firmware */
885 pasemi_mac_restart_rx_intr(mac);
886 pasemi_mac_restart_tx_intr(mac);
887
Olof Johansson6dfa7522007-05-08 00:47:32 -0500888 /* 0xffffff is max value, about 16ms */
Olof Johanssona85b9422007-09-15 13:40:59 -0700889 write_iob_reg(mac, PAS_IOB_DMA_COM_TIMEOUTCFG,
890 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0xffffff));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600891
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600892 ret = pasemi_mac_setup_rx_resources(dev);
893 if (ret)
894 goto out_rx_resources;
895
896 ret = pasemi_mac_setup_tx_resources(dev);
897 if (ret)
898 goto out_tx_resources;
899
Olof Johanssona85b9422007-09-15 13:40:59 -0700900 write_mac_reg(mac, PAS_MAC_IPC_CHNL,
901 PAS_MAC_IPC_CHNL_DCHNO(mac->dma_rxch) |
902 PAS_MAC_IPC_CHNL_BCH(mac->dma_rxch));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600903
904 /* enable rx if */
Olof Johanssona85b9422007-09-15 13:40:59 -0700905 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
906 PAS_DMA_RXINT_RCMDSTA_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600907
908 /* enable rx channel */
Olof Johanssona85b9422007-09-15 13:40:59 -0700909 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
910 PAS_DMA_RXCHAN_CCMDSTA_EN |
911 PAS_DMA_RXCHAN_CCMDSTA_DU);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600912
913 /* enable tx channel */
Olof Johanssona85b9422007-09-15 13:40:59 -0700914 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
915 PAS_DMA_TXCHAN_TCMDSTA_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600916
Olof Johansson928773c2007-09-26 16:25:06 -0500917 pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600918
Olof Johansson36033762007-09-26 16:24:42 -0500919 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
920 PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
921
922 if (mac->type == MAC_TYPE_GMAC)
923 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
924 else
925 flags |= PAS_MAC_CFG_PCFG_TSR_10G | PAS_MAC_CFG_PCFG_SPD_10G;
926
927 /* Enable interface in MAC */
928 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
929
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500930 ret = pasemi_mac_phy_init(dev);
931 /* Some configs don't have PHYs (XAUI etc), so don't complain about
932 * failed init due to -ENODEV.
933 */
934 if (ret && ret != -ENODEV)
935 dev_warn(&mac->pdev->dev, "phy init failed: %d\n", ret);
936
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600937 netif_start_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700938 napi_enable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600939
Olof Johansson771f7402007-05-08 00:47:21 -0500940 /* Interrupts are a bit different for our DMA controller: While
941 * it's got one a regular PCI device header, the interrupt there
942 * is really the base of the range it's using. Each tx and rx
943 * channel has it's own interrupt source.
944 */
945
946 base_irq = virq_to_hw(mac->dma_pdev->irq);
947
948 mac->tx_irq = irq_create_mapping(NULL, base_irq + mac->dma_txch);
949 mac->rx_irq = irq_create_mapping(NULL, base_irq + 20 + mac->dma_txch);
950
951 ret = request_irq(mac->tx_irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600952 mac->tx->irq_name, dev);
953 if (ret) {
954 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500955 base_irq + mac->dma_txch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600956 goto out_tx_int;
957 }
958
Olof Johansson771f7402007-05-08 00:47:21 -0500959 ret = request_irq(mac->rx_irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600960 mac->rx->irq_name, dev);
961 if (ret) {
962 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500963 base_irq + 20 + mac->dma_rxch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600964 goto out_rx_int;
965 }
966
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500967 if (mac->phydev)
968 phy_start(mac->phydev);
969
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600970 return 0;
971
972out_rx_int:
Olof Johansson771f7402007-05-08 00:47:21 -0500973 free_irq(mac->tx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600974out_tx_int:
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700975 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600976 netif_stop_queue(dev);
977 pasemi_mac_free_tx_resources(dev);
978out_tx_resources:
979 pasemi_mac_free_rx_resources(dev);
980out_rx_resources:
981
982 return ret;
983}
984
985#define MAX_RETRIES 5000
986
987static int pasemi_mac_close(struct net_device *dev)
988{
989 struct pasemi_mac *mac = netdev_priv(dev);
990 unsigned int stat;
991 int retries;
992
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500993 if (mac->phydev) {
994 phy_stop(mac->phydev);
995 phy_disconnect(mac->phydev);
996 }
997
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600998 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700999 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001000
1001 /* Clean out any pending buffers */
1002 pasemi_mac_clean_tx(mac);
1003 pasemi_mac_clean_rx(mac, RX_RING_SIZE);
1004
1005 /* Disable interface */
Olof Johanssona85b9422007-09-15 13:40:59 -07001006 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), PAS_DMA_TXCHAN_TCMDSTA_ST);
1007 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), PAS_DMA_RXINT_RCMDSTA_ST);
1008 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), PAS_DMA_RXCHAN_CCMDSTA_ST);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001009
1010 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -07001011 stat = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
Olof Johansson0ce68c72007-04-28 15:36:40 -05001012 if (!(stat & PAS_DMA_TXCHAN_TCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001013 break;
1014 cond_resched();
1015 }
1016
Olof Johansson0ce68c72007-04-28 15:36:40 -05001017 if (stat & PAS_DMA_TXCHAN_TCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001018 dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001019
1020 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -07001021 stat = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
Olof Johansson0ce68c72007-04-28 15:36:40 -05001022 if (!(stat & PAS_DMA_RXCHAN_CCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001023 break;
1024 cond_resched();
1025 }
1026
Olof Johansson0ce68c72007-04-28 15:36:40 -05001027 if (stat & PAS_DMA_RXCHAN_CCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001028 dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001029
1030 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -07001031 stat = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
Olof Johansson0ce68c72007-04-28 15:36:40 -05001032 if (!(stat & PAS_DMA_RXINT_RCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001033 break;
1034 cond_resched();
1035 }
1036
Olof Johansson0ce68c72007-04-28 15:36:40 -05001037 if (stat & PAS_DMA_RXINT_RCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001038 dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001039
1040 /* Then, disable the channel. This must be done separately from
1041 * stopping, since you can't disable when active.
1042 */
1043
Olof Johanssona85b9422007-09-15 13:40:59 -07001044 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), 0);
1045 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), 0);
1046 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001047
Olof Johansson771f7402007-05-08 00:47:21 -05001048 free_irq(mac->tx_irq, dev);
1049 free_irq(mac->rx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001050
1051 /* Free resources */
1052 pasemi_mac_free_rx_resources(dev);
1053 pasemi_mac_free_tx_resources(dev);
1054
1055 return 0;
1056}
1057
1058static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
1059{
1060 struct pasemi_mac *mac = netdev_priv(dev);
1061 struct pasemi_mac_txring *txring;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001062 u64 dflags, mactx;
1063 dma_addr_t map[MAX_SKB_FRAGS+1];
1064 unsigned int map_size[MAX_SKB_FRAGS+1];
Olof Johanssonca7e2352007-09-26 16:23:59 -05001065 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001066 int i, nfrags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001067
1068 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_SS | XCT_MACTX_CRC_PAD;
1069
1070 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001071 const unsigned char *nh = skb_network_header(skb);
1072
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001073 switch (ip_hdr(skb)->protocol) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001074 case IPPROTO_TCP:
1075 dflags |= XCT_MACTX_CSUM_TCP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001076 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001077 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001078 break;
1079 case IPPROTO_UDP:
1080 dflags |= XCT_MACTX_CSUM_UDP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001081 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001082 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001083 break;
1084 }
1085 }
1086
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001087 nfrags = skb_shinfo(skb)->nr_frags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001088
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001089 map[0] = pci_map_single(mac->dma_pdev, skb->data, skb_headlen(skb),
1090 PCI_DMA_TODEVICE);
1091 map_size[0] = skb_headlen(skb);
1092 if (dma_mapping_error(map[0]))
1093 goto out_err_nolock;
1094
1095 for (i = 0; i < nfrags; i++) {
1096 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1097
1098 map[i+1] = pci_map_page(mac->dma_pdev, frag->page,
1099 frag->page_offset, frag->size,
1100 PCI_DMA_TODEVICE);
1101 map_size[i+1] = frag->size;
1102 if (dma_mapping_error(map[i+1])) {
1103 nfrags = i;
1104 goto out_err_nolock;
1105 }
1106 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001107
Olof Johansson26fcfa92007-08-22 09:12:59 -05001108 mactx = dflags | XCT_MACTX_LLEN(skb->len);
Olof Johansson26fcfa92007-08-22 09:12:59 -05001109
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001110 txring = mac->tx;
1111
1112 spin_lock_irqsave(&txring->lock, flags);
1113
Olof Johanssonad5da102007-10-02 16:27:15 -05001114 /* Avoid stepping on the same cache line that the DMA controller
1115 * is currently about to send, so leave at least 8 words available.
1116 * Total free space needed is mactx + fragments + 8
1117 */
1118 if (RING_AVAIL(txring) < nfrags + 10) {
1119 /* no room -- stop the queue and wait for tx intr */
1120 netif_stop_queue(dev);
1121 goto out_err;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001122 }
1123
Olof Johanssonfc9e4d22007-10-02 16:25:53 -05001124 TX_RING(mac, txring->next_to_fill) = mactx;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001125 txring->next_to_fill++;
1126 TX_RING_INFO(mac, txring->next_to_fill).skb = skb;
1127 for (i = 0; i <= nfrags; i++) {
1128 TX_RING(mac, txring->next_to_fill+i) =
1129 XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]);
1130 TX_RING_INFO(mac, txring->next_to_fill+i).dma = map[i];
1131 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001132
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001133 /* We have to add an even number of 8-byte entries to the ring
1134 * even if the last one is unused. That means always an odd number
1135 * of pointers + one mactx descriptor.
1136 */
1137 if (nfrags & 1)
1138 nfrags++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001139
Olof Johanssonad5da102007-10-02 16:27:15 -05001140 txring->next_to_fill = (txring->next_to_fill + nfrags + 1) &
1141 (TX_RING_SIZE-1);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -05001142
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001143 dev->stats.tx_packets++;
1144 dev->stats.tx_bytes += skb->len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001145
1146 spin_unlock_irqrestore(&txring->lock, flags);
1147
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001148 write_dma_reg(mac, PAS_DMA_TXCHAN_INCR(mac->dma_txch), (nfrags+2) >> 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001149
1150 return NETDEV_TX_OK;
1151
1152out_err:
1153 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001154out_err_nolock:
1155 while (nfrags--)
1156 pci_unmap_single(mac->dma_pdev, map[nfrags], map_size[nfrags],
1157 PCI_DMA_TODEVICE);
1158
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001159 return NETDEV_TX_BUSY;
1160}
1161
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001162static void pasemi_mac_set_rx_mode(struct net_device *dev)
1163{
1164 struct pasemi_mac *mac = netdev_priv(dev);
1165 unsigned int flags;
1166
Olof Johanssona85b9422007-09-15 13:40:59 -07001167 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001168
1169 /* Set promiscuous */
1170 if (dev->flags & IFF_PROMISC)
1171 flags |= PAS_MAC_CFG_PCFG_PR;
1172 else
1173 flags &= ~PAS_MAC_CFG_PCFG_PR;
1174
Olof Johanssona85b9422007-09-15 13:40:59 -07001175 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001176}
1177
1178
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001179static int pasemi_mac_poll(struct napi_struct *napi, int budget)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001180{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001181 struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
1182 struct net_device *dev = mac->netdev;
1183 int pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001184
Olof Johansson829185e2007-09-15 13:53:19 -07001185 pasemi_mac_clean_tx(mac);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001186 pkts = pasemi_mac_clean_rx(mac, budget);
1187 if (pkts < budget) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001188 /* all done, no more packets present */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001189 netif_rx_complete(dev, napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001190
Olof Johansson1b0335ea2007-05-08 00:47:26 -05001191 pasemi_mac_restart_rx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001192 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001193 return pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001194}
1195
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001196static void __iomem * __devinit map_onedev(struct pci_dev *p, int index)
1197{
1198 struct device_node *dn;
1199 void __iomem *ret;
1200
1201 dn = pci_device_to_OF_node(p);
1202 if (!dn)
1203 goto fallback;
1204
1205 ret = of_iomap(dn, index);
1206 if (!ret)
1207 goto fallback;
1208
1209 return ret;
1210fallback:
1211 /* This is hardcoded and ugly, but we have some firmware versions
1212 * that don't provide the register space in the device tree. Luckily
1213 * they are at well-known locations so we can just do the math here.
1214 */
1215 return ioremap(0xe0000000 + (p->devfn << 12), 0x2000);
1216}
1217
1218static int __devinit pasemi_mac_map_regs(struct pasemi_mac *mac)
1219{
1220 struct resource res;
1221 struct device_node *dn;
1222 int err;
1223
1224 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
1225 if (!mac->dma_pdev) {
1226 dev_err(&mac->pdev->dev, "Can't find DMA Controller\n");
1227 return -ENODEV;
1228 }
1229
1230 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
1231 if (!mac->iob_pdev) {
1232 dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n");
1233 return -ENODEV;
1234 }
1235
1236 mac->regs = map_onedev(mac->pdev, 0);
1237 mac->dma_regs = map_onedev(mac->dma_pdev, 0);
1238 mac->iob_regs = map_onedev(mac->iob_pdev, 0);
1239
1240 if (!mac->regs || !mac->dma_regs || !mac->iob_regs) {
1241 dev_err(&mac->pdev->dev, "Can't map registers\n");
1242 return -ENODEV;
1243 }
1244
1245 /* The dma status structure is located in the I/O bridge, and
1246 * is cache coherent.
1247 */
1248 if (!dma_status) {
1249 dn = pci_device_to_OF_node(mac->iob_pdev);
1250 if (dn)
1251 err = of_address_to_resource(dn, 1, &res);
1252 if (!dn || err) {
1253 /* Fallback for old firmware */
1254 res.start = 0xfd800000;
1255 res.end = res.start + 0x1000;
1256 }
1257 dma_status = __ioremap(res.start, res.end-res.start, 0);
1258 }
1259
1260 return 0;
1261}
1262
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001263static int __devinit
1264pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1265{
1266 static int index = 0;
1267 struct net_device *dev;
1268 struct pasemi_mac *mac;
1269 int err;
Joe Perches0795af52007-10-03 17:59:30 -07001270 DECLARE_MAC_BUF(mac_buf);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001271
1272 err = pci_enable_device(pdev);
1273 if (err)
1274 return err;
1275
1276 dev = alloc_etherdev(sizeof(struct pasemi_mac));
1277 if (dev == NULL) {
1278 dev_err(&pdev->dev,
1279 "pasemi_mac: Could not allocate ethernet device.\n");
1280 err = -ENOMEM;
1281 goto out_disable_device;
1282 }
1283
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001284 pci_set_drvdata(pdev, dev);
1285 SET_NETDEV_DEV(dev, &pdev->dev);
1286
1287 mac = netdev_priv(dev);
1288
1289 mac->pdev = pdev;
1290 mac->netdev = dev;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001291
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001292 netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
1293
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001294 dev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX | NETIF_F_SG;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001295
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001296 /* These should come out of the device tree eventually */
1297 mac->dma_txch = index;
1298 mac->dma_rxch = index;
1299
1300 /* We probe GMAC before XAUI, but the DMA interfaces are
1301 * in XAUI, GMAC order.
1302 */
1303 if (index < 4)
1304 mac->dma_if = index + 2;
1305 else
1306 mac->dma_if = index - 4;
1307 index++;
1308
1309 switch (pdev->device) {
1310 case 0xa005:
1311 mac->type = MAC_TYPE_GMAC;
1312 break;
1313 case 0xa006:
1314 mac->type = MAC_TYPE_XAUI;
1315 break;
1316 default:
1317 err = -ENODEV;
1318 goto out;
1319 }
1320
1321 /* get mac addr from device tree */
1322 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
1323 err = -ENODEV;
1324 goto out;
1325 }
1326 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
1327
1328 dev->open = pasemi_mac_open;
1329 dev->stop = pasemi_mac_close;
1330 dev->hard_start_xmit = pasemi_mac_start_tx;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001331 dev->set_multicast_list = pasemi_mac_set_rx_mode;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001332
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001333 err = pasemi_mac_map_regs(mac);
1334 if (err)
1335 goto out;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001336
1337 mac->rx_status = &dma_status->rx_sta[mac->dma_rxch];
1338 mac->tx_status = &dma_status->tx_sta[mac->dma_txch];
1339
Olof Johanssonceb51362007-05-08 00:47:49 -05001340 mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1341
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001342 /* Enable most messages by default */
1343 mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1344
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001345 err = register_netdev(dev);
1346
1347 if (err) {
1348 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1349 err);
1350 goto out;
Olof Johansson69c29d82007-10-02 16:24:51 -05001351 } else if netif_msg_probe(mac)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001352 printk(KERN_INFO "%s: PA Semi %s: intf %d, txch %d, rxch %d, "
Joe Perches0795af52007-10-03 17:59:30 -07001353 "hw addr %s\n",
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001354 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
1355 mac->dma_if, mac->dma_txch, mac->dma_rxch,
Joe Perches0795af52007-10-03 17:59:30 -07001356 print_mac(mac_buf, dev->dev_addr));
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001357
1358 return err;
1359
1360out:
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001361 if (mac->iob_pdev)
1362 pci_dev_put(mac->iob_pdev);
1363 if (mac->dma_pdev)
1364 pci_dev_put(mac->dma_pdev);
1365 if (mac->dma_regs)
1366 iounmap(mac->dma_regs);
1367 if (mac->iob_regs)
1368 iounmap(mac->iob_regs);
1369 if (mac->regs)
1370 iounmap(mac->regs);
1371
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001372 free_netdev(dev);
1373out_disable_device:
1374 pci_disable_device(pdev);
1375 return err;
1376
1377}
1378
1379static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1380{
1381 struct net_device *netdev = pci_get_drvdata(pdev);
1382 struct pasemi_mac *mac;
1383
1384 if (!netdev)
1385 return;
1386
1387 mac = netdev_priv(netdev);
1388
1389 unregister_netdev(netdev);
1390
1391 pci_disable_device(pdev);
1392 pci_dev_put(mac->dma_pdev);
1393 pci_dev_put(mac->iob_pdev);
1394
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001395 iounmap(mac->regs);
1396 iounmap(mac->dma_regs);
1397 iounmap(mac->iob_regs);
1398
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001399 pci_set_drvdata(pdev, NULL);
1400 free_netdev(netdev);
1401}
1402
1403static struct pci_device_id pasemi_mac_pci_tbl[] = {
1404 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1405 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
olof@lixom.netfd178252007-05-12 14:57:36 -05001406 { },
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001407};
1408
1409MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1410
1411static struct pci_driver pasemi_mac_driver = {
1412 .name = "pasemi_mac",
1413 .id_table = pasemi_mac_pci_tbl,
1414 .probe = pasemi_mac_probe,
1415 .remove = __devexit_p(pasemi_mac_remove),
1416};
1417
1418static void __exit pasemi_mac_cleanup_module(void)
1419{
1420 pci_unregister_driver(&pasemi_mac_driver);
1421 __iounmap(dma_status);
1422 dma_status = NULL;
1423}
1424
1425int pasemi_mac_init_module(void)
1426{
1427 return pci_register_driver(&pasemi_mac_driver);
1428}
1429
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001430module_init(pasemi_mac_init_module);
1431module_exit(pasemi_mac_cleanup_module);