blob: 8892b658655ce0034cf3887e05e406e2915e9e7c [file] [log] [blame]
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001/*
2 * Copyright (C) 2006-2007 PA Semi, Inc
3 *
4 * Driver for the PA Semi PWRficient onchip 1G/10G Ethernet MACs
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/pci.h>
23#include <linux/interrupt.h>
24#include <linux/dmaengine.h>
25#include <linux/delay.h>
26#include <linux/netdevice.h>
27#include <linux/etherdevice.h>
28#include <asm/dma-mapping.h>
29#include <linux/in.h>
30#include <linux/skbuff.h>
31
32#include <linux/ip.h>
33#include <linux/tcp.h>
34#include <net/checksum.h>
35
Olof Johansson771f7402007-05-08 00:47:21 -050036#include <asm/irq.h>
37
Olof Johanssonf5cd7872007-01-31 21:43:54 -060038#include "pasemi_mac.h"
39
40
41/* TODO list
42 *
43 * - Get rid of pci_{read,write}_config(), map registers with ioremap
44 * for performance
45 * - PHY support
46 * - Multicast support
47 * - Large MTU support
48 * - Other performance improvements
49 */
50
51
52/* Must be a power of two */
53#define RX_RING_SIZE 512
54#define TX_RING_SIZE 512
55
Olof Johanssonceb51362007-05-08 00:47:49 -050056#define DEFAULT_MSG_ENABLE \
57 (NETIF_MSG_DRV | \
58 NETIF_MSG_PROBE | \
59 NETIF_MSG_LINK | \
60 NETIF_MSG_TIMER | \
61 NETIF_MSG_IFDOWN | \
62 NETIF_MSG_IFUP | \
63 NETIF_MSG_RX_ERR | \
64 NETIF_MSG_TX_ERR)
65
Olof Johanssonf5cd7872007-01-31 21:43:54 -060066#define TX_DESC(mac, num) ((mac)->tx->desc[(num) & (TX_RING_SIZE-1)])
67#define TX_DESC_INFO(mac, num) ((mac)->tx->desc_info[(num) & (TX_RING_SIZE-1)])
68#define RX_DESC(mac, num) ((mac)->rx->desc[(num) & (RX_RING_SIZE-1)])
69#define RX_DESC_INFO(mac, num) ((mac)->rx->desc_info[(num) & (RX_RING_SIZE-1)])
70#define RX_BUFF(mac, num) ((mac)->rx->buffers[(num) & (RX_RING_SIZE-1)])
71
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
163static int pasemi_mac_setup_rx_resources(struct net_device *dev)
164{
165 struct pasemi_mac_rxring *ring;
166 struct pasemi_mac *mac = netdev_priv(dev);
167 int chan_id = mac->dma_rxch;
168
169 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
170
171 if (!ring)
172 goto out_ring;
173
174 spin_lock_init(&ring->lock);
175
Olof Johansson021fa222007-08-22 09:13:11 -0500176 ring->size = RX_RING_SIZE;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600177 ring->desc_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
178 RX_RING_SIZE, GFP_KERNEL);
179
180 if (!ring->desc_info)
181 goto out_desc_info;
182
183 /* Allocate descriptors */
184 ring->desc = dma_alloc_coherent(&mac->dma_pdev->dev,
185 RX_RING_SIZE *
186 sizeof(struct pas_dma_xct_descr),
187 &ring->dma, GFP_KERNEL);
188
189 if (!ring->desc)
190 goto out_desc;
191
192 memset(ring->desc, 0, RX_RING_SIZE * sizeof(struct pas_dma_xct_descr));
193
194 ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev,
195 RX_RING_SIZE * sizeof(u64),
196 &ring->buf_dma, GFP_KERNEL);
197 if (!ring->buffers)
198 goto out_buffers;
199
200 memset(ring->buffers, 0, RX_RING_SIZE * sizeof(u64));
201
Olof Johanssona85b9422007-09-15 13:40:59 -0700202 write_dma_reg(mac, PAS_DMA_RXCHAN_BASEL(chan_id), PAS_DMA_RXCHAN_BASEL_BRBL(ring->dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600203
Olof Johanssona85b9422007-09-15 13:40:59 -0700204 write_dma_reg(mac, PAS_DMA_RXCHAN_BASEU(chan_id),
205 PAS_DMA_RXCHAN_BASEU_BRBH(ring->dma >> 32) |
206 PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 2));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600207
Olof Johanssona85b9422007-09-15 13:40:59 -0700208 write_dma_reg(mac, PAS_DMA_RXCHAN_CFG(chan_id),
Olof Johanssonc0efd522007-08-22 09:12:52 -0500209 PAS_DMA_RXCHAN_CFG_HBU(2));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600210
Olof Johanssona85b9422007-09-15 13:40:59 -0700211 write_dma_reg(mac, PAS_DMA_RXINT_BASEL(mac->dma_if),
212 PAS_DMA_RXINT_BASEL_BRBL(__pa(ring->buffers)));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600213
Olof Johanssona85b9422007-09-15 13:40:59 -0700214 write_dma_reg(mac, PAS_DMA_RXINT_BASEU(mac->dma_if),
215 PAS_DMA_RXINT_BASEU_BRBH(__pa(ring->buffers) >> 32) |
216 PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600217
Olof Johanssonc0efd522007-08-22 09:12:52 -0500218 write_dma_reg(mac, PAS_DMA_RXINT_CFG(mac->dma_if),
219 PAS_DMA_RXINT_CFG_DHL(2));
220
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600221 ring->next_to_fill = 0;
222 ring->next_to_clean = 0;
223
224 snprintf(ring->irq_name, sizeof(ring->irq_name),
225 "%s rx", dev->name);
226 mac->rx = ring;
227
228 return 0;
229
230out_buffers:
231 dma_free_coherent(&mac->dma_pdev->dev,
232 RX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
233 mac->rx->desc, mac->rx->dma);
234out_desc:
235 kfree(ring->desc_info);
236out_desc_info:
237 kfree(ring);
238out_ring:
239 return -ENOMEM;
240}
241
242
243static int pasemi_mac_setup_tx_resources(struct net_device *dev)
244{
245 struct pasemi_mac *mac = netdev_priv(dev);
246 u32 val;
247 int chan_id = mac->dma_txch;
248 struct pasemi_mac_txring *ring;
249
250 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
251 if (!ring)
252 goto out_ring;
253
254 spin_lock_init(&ring->lock);
255
Olof Johansson021fa222007-08-22 09:13:11 -0500256 ring->size = TX_RING_SIZE;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600257 ring->desc_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
258 TX_RING_SIZE, GFP_KERNEL);
259 if (!ring->desc_info)
260 goto out_desc_info;
261
262 /* Allocate descriptors */
263 ring->desc = dma_alloc_coherent(&mac->dma_pdev->dev,
264 TX_RING_SIZE *
265 sizeof(struct pas_dma_xct_descr),
266 &ring->dma, GFP_KERNEL);
267 if (!ring->desc)
268 goto out_desc;
269
270 memset(ring->desc, 0, TX_RING_SIZE * sizeof(struct pas_dma_xct_descr));
271
Olof Johanssona85b9422007-09-15 13:40:59 -0700272 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEL(chan_id),
273 PAS_DMA_TXCHAN_BASEL_BRBL(ring->dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600274 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->dma >> 32);
275 val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 2);
276
Olof Johanssona85b9422007-09-15 13:40:59 -0700277 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEU(chan_id), val);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600278
Olof Johanssona85b9422007-09-15 13:40:59 -0700279 write_dma_reg(mac, PAS_DMA_TXCHAN_CFG(chan_id),
280 PAS_DMA_TXCHAN_CFG_TY_IFACE |
281 PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
282 PAS_DMA_TXCHAN_CFG_UP |
283 PAS_DMA_TXCHAN_CFG_WT(2));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600284
Olof Johansson021fa222007-08-22 09:13:11 -0500285 ring->next_to_fill = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600286 ring->next_to_clean = 0;
287
288 snprintf(ring->irq_name, sizeof(ring->irq_name),
289 "%s tx", dev->name);
290 mac->tx = ring;
291
292 return 0;
293
294out_desc:
295 kfree(ring->desc_info);
296out_desc_info:
297 kfree(ring);
298out_ring:
299 return -ENOMEM;
300}
301
302static void pasemi_mac_free_tx_resources(struct net_device *dev)
303{
304 struct pasemi_mac *mac = netdev_priv(dev);
305 unsigned int i;
306 struct pasemi_mac_buffer *info;
307 struct pas_dma_xct_descr *dp;
308
309 for (i = 0; i < TX_RING_SIZE; i++) {
310 info = &TX_DESC_INFO(mac, i);
311 dp = &TX_DESC(mac, i);
312 if (info->dma) {
313 if (info->skb) {
314 pci_unmap_single(mac->dma_pdev,
315 info->dma,
316 info->skb->len,
317 PCI_DMA_TODEVICE);
318 dev_kfree_skb_any(info->skb);
319 }
320 info->dma = 0;
321 info->skb = NULL;
322 dp->mactx = 0;
323 dp->ptr = 0;
324 }
325 }
326
327 dma_free_coherent(&mac->dma_pdev->dev,
328 TX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
329 mac->tx->desc, mac->tx->dma);
330
331 kfree(mac->tx->desc_info);
332 kfree(mac->tx);
333 mac->tx = NULL;
334}
335
336static void pasemi_mac_free_rx_resources(struct net_device *dev)
337{
338 struct pasemi_mac *mac = netdev_priv(dev);
339 unsigned int i;
340 struct pasemi_mac_buffer *info;
341 struct pas_dma_xct_descr *dp;
342
343 for (i = 0; i < RX_RING_SIZE; i++) {
344 info = &RX_DESC_INFO(mac, i);
345 dp = &RX_DESC(mac, i);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500346 if (info->skb) {
347 if (info->dma) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600348 pci_unmap_single(mac->dma_pdev,
349 info->dma,
350 info->skb->len,
351 PCI_DMA_FROMDEVICE);
352 dev_kfree_skb_any(info->skb);
353 }
354 info->dma = 0;
355 info->skb = NULL;
356 dp->macrx = 0;
357 dp->ptr = 0;
358 }
359 }
360
361 dma_free_coherent(&mac->dma_pdev->dev,
362 RX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
363 mac->rx->desc, mac->rx->dma);
364
365 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
366 mac->rx->buffers, mac->rx->buf_dma);
367
368 kfree(mac->rx->desc_info);
369 kfree(mac->rx);
370 mac->rx = NULL;
371}
372
373static void pasemi_mac_replenish_rx_ring(struct net_device *dev)
374{
375 struct pasemi_mac *mac = netdev_priv(dev);
376 unsigned int i;
377 int start = mac->rx->next_to_fill;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500378 unsigned int limit, count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600379
Olof Johansson021fa222007-08-22 09:13:11 -0500380 limit = RING_AVAIL(mac->rx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600381 /* Check to see if we're doing first-time setup */
382 if (unlikely(mac->rx->next_to_clean == 0 && mac->rx->next_to_fill == 0))
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500383 limit = RX_RING_SIZE;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600384
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500385 if (limit <= 0)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600386 return;
387
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500388 i = start;
389 for (count = limit; count; count--) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600390 struct pasemi_mac_buffer *info = &RX_DESC_INFO(mac, i);
391 u64 *buff = &RX_BUFF(mac, i);
392 struct sk_buff *skb;
393 dma_addr_t dma;
394
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500395 /* skb might still be in there for recycle on short receives */
396 if (info->skb)
397 skb = info->skb;
398 else
399 skb = dev_alloc_skb(BUF_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600400
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500401 if (unlikely(!skb))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600402 break;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600403
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600404 dma = pci_map_single(mac->dma_pdev, skb->data, skb->len,
405 PCI_DMA_FROMDEVICE);
406
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500407 if (unlikely(dma_mapping_error(dma))) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600408 dev_kfree_skb_irq(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600409 break;
410 }
411
412 info->skb = skb;
413 info->dma = dma;
414 *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500415 i++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600416 }
417
418 wmb();
419
Olof Johanssona85b9422007-09-15 13:40:59 -0700420 write_dma_reg(mac, PAS_DMA_RXCHAN_INCR(mac->dma_rxch), limit - count);
421 write_dma_reg(mac, PAS_DMA_RXINT_INCR(mac->dma_if), limit - count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600422
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500423 mac->rx->next_to_fill += limit - count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600424}
425
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500426static void pasemi_mac_restart_rx_intr(struct pasemi_mac *mac)
427{
Olof Johansson52a94352007-05-12 18:01:09 -0500428 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500429 /* Re-enable packet count interrupts: finally
430 * ack the packet count interrupt we got in rx_intr.
431 */
432
Olof Johansson52a94352007-05-12 18:01:09 -0500433 pcnt = *mac->rx_status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500434
Olof Johansson52a94352007-05-12 18:01:09 -0500435 reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500436
Olof Johanssona85b9422007-09-15 13:40:59 -0700437 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500438}
439
440static void pasemi_mac_restart_tx_intr(struct pasemi_mac *mac)
441{
Olof Johansson52a94352007-05-12 18:01:09 -0500442 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500443
444 /* Re-enable packet count interrupts */
Olof Johansson52a94352007-05-12 18:01:09 -0500445 pcnt = *mac->tx_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_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_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_TXCH_RESET(mac->dma_txch), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500450}
451
452
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600453static int pasemi_mac_clean_rx(struct pasemi_mac *mac, int limit)
454{
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500455 unsigned int n;
456 int count;
457 struct pas_dma_xct_descr *dp;
458 struct pasemi_mac_buffer *info;
459 struct sk_buff *skb;
460 unsigned int i, len;
461 u64 macrx;
462 dma_addr_t dma;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600463
464 spin_lock(&mac->rx->lock);
465
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500466 n = mac->rx->next_to_clean;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600467
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500468 for (count = limit; count; count--) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600469
470 rmb();
471
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500472 dp = &RX_DESC(mac, n);
Olof Johansson26fcfa92007-08-22 09:12:59 -0500473 prefetchw(dp);
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500474 macrx = dp->macrx;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600475
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500476 if (!(macrx & XCT_MACRX_O))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600477 break;
478
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600479
480 info = NULL;
481
482 /* We have to scan for our skb since there's no way
483 * to back-map them from the descriptor, and if we
484 * have several receive channels then they might not
485 * show up in the same order as they were put on the
486 * interface ring.
487 */
488
489 dma = (dp->ptr & XCT_PTR_ADDR_M);
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500490 for (i = n; i < (n + RX_RING_SIZE); i++) {
491 info = &RX_DESC_INFO(mac, i);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600492 if (info->dma == dma)
493 break;
494 }
Olof Johansson26fcfa92007-08-22 09:12:59 -0500495 prefetchw(info);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600496
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500497 skb = info->skb;
Olof Johansson26fcfa92007-08-22 09:12:59 -0500498 prefetchw(skb);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500499 info->dma = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600500
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500501 pci_unmap_single(mac->dma_pdev, dma, skb->len,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600502 PCI_DMA_FROMDEVICE);
503
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500504 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600505
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500506 if (len < 256) {
507 struct sk_buff *new_skb =
508 netdev_alloc_skb(mac->netdev, len + NET_IP_ALIGN);
509 if (new_skb) {
510 skb_reserve(new_skb, NET_IP_ALIGN);
Olof Johansson73344862007-08-22 09:12:55 -0500511 memcpy(new_skb->data, skb->data, len);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500512 /* save the skb in buffer_info as good */
513 skb = new_skb;
514 }
515 /* else just continue with the old one */
516 } else
517 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600518
519 skb_put(skb, len);
520
Olof Johansson26fcfa92007-08-22 09:12:59 -0500521 if (likely((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK)) {
Olof Johansson38bf3182007-08-22 09:13:24 -0500522 skb->ip_summed = CHECKSUM_UNNECESSARY;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500523 skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600524 XCT_MACRX_CSUM_S;
525 } else
526 skb->ip_summed = CHECKSUM_NONE;
527
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700528 mac->netdev->stats.rx_bytes += len;
529 mac->netdev->stats.rx_packets++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600530
Olof Johansson26fcfa92007-08-22 09:12:59 -0500531 skb->protocol = eth_type_trans(skb, mac->netdev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600532 netif_receive_skb(skb);
533
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600534 dp->ptr = 0;
535 dp->macrx = 0;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500536
537 n++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600538 }
539
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500540 mac->rx->next_to_clean += limit - count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600541 pasemi_mac_replenish_rx_ring(mac->netdev);
542
543 spin_unlock(&mac->rx->lock);
544
545 return count;
546}
547
548static int pasemi_mac_clean_tx(struct pasemi_mac *mac)
549{
550 int i;
551 struct pasemi_mac_buffer *info;
552 struct pas_dma_xct_descr *dp;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500553 unsigned int start, count, limit;
554 unsigned int total_count;
Olof Johanssonca7e2352007-09-26 16:23:59 -0500555 unsigned long flags;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500556 struct sk_buff *skbs[32];
557 dma_addr_t dmas[32];
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600558
Olof Johansson02df6cf2007-08-22 09:13:03 -0500559 total_count = 0;
560restart:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600561 spin_lock_irqsave(&mac->tx->lock, flags);
562
563 start = mac->tx->next_to_clean;
Olof Johansson021fa222007-08-22 09:13:11 -0500564 limit = min(mac->tx->next_to_fill, start+32);
Olof Johansson02df6cf2007-08-22 09:13:03 -0500565
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600566 count = 0;
567
Olof Johansson02df6cf2007-08-22 09:13:03 -0500568 for (i = start; i < limit; i++) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600569 dp = &TX_DESC(mac, i);
Olof Johansson02df6cf2007-08-22 09:13:03 -0500570
Olof Johansson26fcfa92007-08-22 09:12:59 -0500571 if (unlikely(dp->mactx & XCT_MACTX_O))
Olof Johansson02df6cf2007-08-22 09:13:03 -0500572 /* Not yet transmitted */
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600573 break;
574
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600575 info = &TX_DESC_INFO(mac, i);
Olof Johansson02df6cf2007-08-22 09:13:03 -0500576 skbs[count] = info->skb;
577 dmas[count] = info->dma;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600578
579 info->skb = NULL;
580 info->dma = 0;
581 dp->mactx = 0;
582 dp->ptr = 0;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500583
584 count++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600585 }
586 mac->tx->next_to_clean += count;
587 spin_unlock_irqrestore(&mac->tx->lock, flags);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500588 netif_wake_queue(mac->netdev);
589
Olof Johansson02df6cf2007-08-22 09:13:03 -0500590 for (i = 0; i < count; i++) {
591 pci_unmap_single(mac->dma_pdev, dmas[i],
592 skbs[i]->len, PCI_DMA_TODEVICE);
593 dev_kfree_skb_irq(skbs[i]);
594 }
595
596 total_count += count;
597
598 /* If the batch was full, try to clean more */
599 if (count == 32)
600 goto restart;
601
602 return total_count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600603}
604
605
606static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
607{
608 struct net_device *dev = data;
609 struct pasemi_mac *mac = netdev_priv(dev);
610 unsigned int reg;
611
Olof Johansson6dfa7522007-05-08 00:47:32 -0500612 if (!(*mac->rx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600613 return IRQ_NONE;
614
Olof Johansson6dfa7522007-05-08 00:47:32 -0500615 if (*mac->rx_status & PAS_STATUS_ERROR)
616 printk("rx_status reported error\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600617
Olof Johansson6dfa7522007-05-08 00:47:32 -0500618 /* Don't reset packet count so it won't fire again but clear
619 * all others.
620 */
621
Olof Johansson6dfa7522007-05-08 00:47:32 -0500622 reg = 0;
623 if (*mac->rx_status & PAS_STATUS_SOFT)
624 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
625 if (*mac->rx_status & PAS_STATUS_ERROR)
626 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600627 if (*mac->rx_status & PAS_STATUS_TIMER)
628 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
629
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700630 netif_rx_schedule(dev, &mac->napi);
Olof Johansson6dfa7522007-05-08 00:47:32 -0500631
Olof Johanssona85b9422007-09-15 13:40:59 -0700632 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600633
634 return IRQ_HANDLED;
635}
636
637static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
638{
639 struct net_device *dev = data;
640 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson52a94352007-05-12 18:01:09 -0500641 unsigned int reg, pcnt;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600642
Olof Johansson6dfa7522007-05-08 00:47:32 -0500643 if (!(*mac->tx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600644 return IRQ_NONE;
645
646 pasemi_mac_clean_tx(mac);
647
Olof Johansson52a94352007-05-12 18:01:09 -0500648 pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
649
650 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson6dfa7522007-05-08 00:47:32 -0500651
652 if (*mac->tx_status & PAS_STATUS_SOFT)
653 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
654 if (*mac->tx_status & PAS_STATUS_ERROR)
655 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600656
Olof Johanssona85b9422007-09-15 13:40:59 -0700657 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600658
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600659 return IRQ_HANDLED;
660}
661
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500662static void pasemi_adjust_link(struct net_device *dev)
663{
664 struct pasemi_mac *mac = netdev_priv(dev);
665 int msg;
666 unsigned int flags;
667 unsigned int new_flags;
668
669 if (!mac->phydev->link) {
670 /* If no link, MAC speed settings don't matter. Just report
671 * link down and return.
672 */
673 if (mac->link && netif_msg_link(mac))
674 printk(KERN_INFO "%s: Link is down.\n", dev->name);
675
676 netif_carrier_off(dev);
677 mac->link = 0;
678
679 return;
680 } else
681 netif_carrier_on(dev);
682
Olof Johanssona85b9422007-09-15 13:40:59 -0700683 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500684 new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
685 PAS_MAC_CFG_PCFG_TSR_M);
686
687 if (!mac->phydev->duplex)
688 new_flags |= PAS_MAC_CFG_PCFG_HD;
689
690 switch (mac->phydev->speed) {
691 case 1000:
692 new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
693 PAS_MAC_CFG_PCFG_TSR_1G;
694 break;
695 case 100:
696 new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
697 PAS_MAC_CFG_PCFG_TSR_100M;
698 break;
699 case 10:
700 new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
701 PAS_MAC_CFG_PCFG_TSR_10M;
702 break;
703 default:
704 printk("Unsupported speed %d\n", mac->phydev->speed);
705 }
706
707 /* Print on link or speed/duplex change */
708 msg = mac->link != mac->phydev->link || flags != new_flags;
709
710 mac->duplex = mac->phydev->duplex;
711 mac->speed = mac->phydev->speed;
712 mac->link = mac->phydev->link;
713
714 if (new_flags != flags)
Olof Johanssona85b9422007-09-15 13:40:59 -0700715 write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500716
717 if (msg && netif_msg_link(mac))
718 printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
719 dev->name, mac->speed, mac->duplex ? "full" : "half");
720}
721
722static int pasemi_mac_phy_init(struct net_device *dev)
723{
724 struct pasemi_mac *mac = netdev_priv(dev);
725 struct device_node *dn, *phy_dn;
726 struct phy_device *phydev;
727 unsigned int phy_id;
728 const phandle *ph;
729 const unsigned int *prop;
730 struct resource r;
731 int ret;
732
733 dn = pci_device_to_OF_node(mac->pdev);
Linus Torvalds90287802007-05-08 11:57:17 -0700734 ph = of_get_property(dn, "phy-handle", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500735 if (!ph)
736 return -ENODEV;
737 phy_dn = of_find_node_by_phandle(*ph);
738
Linus Torvalds90287802007-05-08 11:57:17 -0700739 prop = of_get_property(phy_dn, "reg", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500740 ret = of_address_to_resource(phy_dn->parent, 0, &r);
741 if (ret)
742 goto err;
743
744 phy_id = *prop;
745 snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);
746
747 of_node_put(phy_dn);
748
749 mac->link = 0;
750 mac->speed = 0;
751 mac->duplex = -1;
752
753 phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);
754
755 if (IS_ERR(phydev)) {
756 printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
757 return PTR_ERR(phydev);
758 }
759
760 mac->phydev = phydev;
761
762 return 0;
763
764err:
765 of_node_put(phy_dn);
766 return -ENODEV;
767}
768
769
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600770static int pasemi_mac_open(struct net_device *dev)
771{
772 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson771f7402007-05-08 00:47:21 -0500773 int base_irq;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600774 unsigned int flags;
775 int ret;
776
777 /* enable rx section */
Olof Johanssona85b9422007-09-15 13:40:59 -0700778 write_dma_reg(mac, PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600779
780 /* enable tx section */
Olof Johanssona85b9422007-09-15 13:40:59 -0700781 write_dma_reg(mac, PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600782
783 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
784 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
785 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
786
Olof Johanssona85b9422007-09-15 13:40:59 -0700787 write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600788
789 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
790 PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
791
Olof Johansson6a290e32007-09-26 16:23:31 -0500792 if (mac->type == MAC_TYPE_GMAC)
793 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
794 else
795 flags |= PAS_MAC_CFG_PCFG_TSR_10G | PAS_MAC_CFG_PCFG_SPD_10G;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600796
Olof Johanssona85b9422007-09-15 13:40:59 -0700797 write_iob_reg(mac, PAS_IOB_DMA_RXCH_CFG(mac->dma_rxch),
798 PAS_IOB_DMA_RXCH_CFG_CNTTH(0));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600799
Olof Johanssona85b9422007-09-15 13:40:59 -0700800 write_iob_reg(mac, PAS_IOB_DMA_TXCH_CFG(mac->dma_txch),
Olof Johansson02df6cf2007-08-22 09:13:03 -0500801 PAS_IOB_DMA_TXCH_CFG_CNTTH(128));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600802
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500803 /* Clear out any residual packet count state from firmware */
804 pasemi_mac_restart_rx_intr(mac);
805 pasemi_mac_restart_tx_intr(mac);
806
Olof Johansson6dfa7522007-05-08 00:47:32 -0500807 /* 0xffffff is max value, about 16ms */
Olof Johanssona85b9422007-09-15 13:40:59 -0700808 write_iob_reg(mac, PAS_IOB_DMA_COM_TIMEOUTCFG,
809 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0xffffff));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600810
Olof Johanssona85b9422007-09-15 13:40:59 -0700811 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600812
813 ret = pasemi_mac_setup_rx_resources(dev);
814 if (ret)
815 goto out_rx_resources;
816
817 ret = pasemi_mac_setup_tx_resources(dev);
818 if (ret)
819 goto out_tx_resources;
820
Olof Johanssona85b9422007-09-15 13:40:59 -0700821 write_mac_reg(mac, PAS_MAC_IPC_CHNL,
822 PAS_MAC_IPC_CHNL_DCHNO(mac->dma_rxch) |
823 PAS_MAC_IPC_CHNL_BCH(mac->dma_rxch));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600824
825 /* enable rx if */
Olof Johanssona85b9422007-09-15 13:40:59 -0700826 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
827 PAS_DMA_RXINT_RCMDSTA_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600828
829 /* enable rx channel */
Olof Johanssona85b9422007-09-15 13:40:59 -0700830 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
831 PAS_DMA_RXCHAN_CCMDSTA_EN |
832 PAS_DMA_RXCHAN_CCMDSTA_DU);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600833
834 /* enable tx channel */
Olof Johanssona85b9422007-09-15 13:40:59 -0700835 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
836 PAS_DMA_TXCHAN_TCMDSTA_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600837
838 pasemi_mac_replenish_rx_ring(dev);
839
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500840 ret = pasemi_mac_phy_init(dev);
841 /* Some configs don't have PHYs (XAUI etc), so don't complain about
842 * failed init due to -ENODEV.
843 */
844 if (ret && ret != -ENODEV)
845 dev_warn(&mac->pdev->dev, "phy init failed: %d\n", ret);
846
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600847 netif_start_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700848 napi_enable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600849
Olof Johansson771f7402007-05-08 00:47:21 -0500850 /* Interrupts are a bit different for our DMA controller: While
851 * it's got one a regular PCI device header, the interrupt there
852 * is really the base of the range it's using. Each tx and rx
853 * channel has it's own interrupt source.
854 */
855
856 base_irq = virq_to_hw(mac->dma_pdev->irq);
857
858 mac->tx_irq = irq_create_mapping(NULL, base_irq + mac->dma_txch);
859 mac->rx_irq = irq_create_mapping(NULL, base_irq + 20 + mac->dma_txch);
860
861 ret = request_irq(mac->tx_irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600862 mac->tx->irq_name, dev);
863 if (ret) {
864 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500865 base_irq + mac->dma_txch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600866 goto out_tx_int;
867 }
868
Olof Johansson771f7402007-05-08 00:47:21 -0500869 ret = request_irq(mac->rx_irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600870 mac->rx->irq_name, dev);
871 if (ret) {
872 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500873 base_irq + 20 + mac->dma_rxch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600874 goto out_rx_int;
875 }
876
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500877 if (mac->phydev)
878 phy_start(mac->phydev);
879
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600880 return 0;
881
882out_rx_int:
Olof Johansson771f7402007-05-08 00:47:21 -0500883 free_irq(mac->tx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600884out_tx_int:
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700885 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600886 netif_stop_queue(dev);
887 pasemi_mac_free_tx_resources(dev);
888out_tx_resources:
889 pasemi_mac_free_rx_resources(dev);
890out_rx_resources:
891
892 return ret;
893}
894
895#define MAX_RETRIES 5000
896
897static int pasemi_mac_close(struct net_device *dev)
898{
899 struct pasemi_mac *mac = netdev_priv(dev);
900 unsigned int stat;
901 int retries;
902
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500903 if (mac->phydev) {
904 phy_stop(mac->phydev);
905 phy_disconnect(mac->phydev);
906 }
907
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600908 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700909 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600910
911 /* Clean out any pending buffers */
912 pasemi_mac_clean_tx(mac);
913 pasemi_mac_clean_rx(mac, RX_RING_SIZE);
914
915 /* Disable interface */
Olof Johanssona85b9422007-09-15 13:40:59 -0700916 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), PAS_DMA_TXCHAN_TCMDSTA_ST);
917 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), PAS_DMA_RXINT_RCMDSTA_ST);
918 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), PAS_DMA_RXCHAN_CCMDSTA_ST);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600919
920 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -0700921 stat = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
Olof Johansson0ce68c72007-04-28 15:36:40 -0500922 if (!(stat & PAS_DMA_TXCHAN_TCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600923 break;
924 cond_resched();
925 }
926
Olof Johansson0ce68c72007-04-28 15:36:40 -0500927 if (stat & PAS_DMA_TXCHAN_TCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600928 dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600929
930 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -0700931 stat = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
Olof Johansson0ce68c72007-04-28 15:36:40 -0500932 if (!(stat & PAS_DMA_RXCHAN_CCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600933 break;
934 cond_resched();
935 }
936
Olof Johansson0ce68c72007-04-28 15:36:40 -0500937 if (stat & PAS_DMA_RXCHAN_CCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600938 dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600939
940 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -0700941 stat = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
Olof Johansson0ce68c72007-04-28 15:36:40 -0500942 if (!(stat & PAS_DMA_RXINT_RCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600943 break;
944 cond_resched();
945 }
946
Olof Johansson0ce68c72007-04-28 15:36:40 -0500947 if (stat & PAS_DMA_RXINT_RCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600948 dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600949
950 /* Then, disable the channel. This must be done separately from
951 * stopping, since you can't disable when active.
952 */
953
Olof Johanssona85b9422007-09-15 13:40:59 -0700954 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), 0);
955 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), 0);
956 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600957
Olof Johansson771f7402007-05-08 00:47:21 -0500958 free_irq(mac->tx_irq, dev);
959 free_irq(mac->rx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600960
961 /* Free resources */
962 pasemi_mac_free_rx_resources(dev);
963 pasemi_mac_free_tx_resources(dev);
964
965 return 0;
966}
967
968static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
969{
970 struct pasemi_mac *mac = netdev_priv(dev);
971 struct pasemi_mac_txring *txring;
972 struct pasemi_mac_buffer *info;
973 struct pas_dma_xct_descr *dp;
Olof Johansson26fcfa92007-08-22 09:12:59 -0500974 u64 dflags, mactx, ptr;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600975 dma_addr_t map;
Olof Johanssonca7e2352007-09-26 16:23:59 -0500976 unsigned long flags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600977
978 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_SS | XCT_MACTX_CRC_PAD;
979
980 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700981 const unsigned char *nh = skb_network_header(skb);
982
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700983 switch (ip_hdr(skb)->protocol) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600984 case IPPROTO_TCP:
985 dflags |= XCT_MACTX_CSUM_TCP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300986 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700987 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600988 break;
989 case IPPROTO_UDP:
990 dflags |= XCT_MACTX_CSUM_UDP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300991 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700992 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600993 break;
994 }
995 }
996
997 map = pci_map_single(mac->dma_pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
998
999 if (dma_mapping_error(map))
1000 return NETDEV_TX_BUSY;
1001
Olof Johansson26fcfa92007-08-22 09:12:59 -05001002 mactx = dflags | XCT_MACTX_LLEN(skb->len);
1003 ptr = XCT_PTR_LEN(skb->len) | XCT_PTR_ADDR(map);
1004
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001005 txring = mac->tx;
1006
1007 spin_lock_irqsave(&txring->lock, flags);
1008
Olof Johansson021fa222007-08-22 09:13:11 -05001009 if (RING_AVAIL(txring) <= 1) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001010 spin_unlock_irqrestore(&txring->lock, flags);
1011 pasemi_mac_clean_tx(mac);
Olof Johansson52a94352007-05-12 18:01:09 -05001012 pasemi_mac_restart_tx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001013 spin_lock_irqsave(&txring->lock, flags);
1014
Olof Johansson021fa222007-08-22 09:13:11 -05001015 if (RING_AVAIL(txring) <= 1) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001016 /* Still no room -- stop the queue and wait for tx
1017 * intr when there's room.
1018 */
1019 netif_stop_queue(dev);
1020 goto out_err;
1021 }
1022 }
1023
Olof Johansson021fa222007-08-22 09:13:11 -05001024 dp = &TX_DESC(mac, txring->next_to_fill);
1025 info = &TX_DESC_INFO(mac, txring->next_to_fill);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001026
Olof Johansson26fcfa92007-08-22 09:12:59 -05001027 dp->mactx = mactx;
1028 dp->ptr = ptr;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001029 info->dma = map;
1030 info->skb = skb;
1031
Olof Johansson021fa222007-08-22 09:13:11 -05001032 txring->next_to_fill++;
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001033 dev->stats.tx_packets++;
1034 dev->stats.tx_bytes += skb->len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001035
1036 spin_unlock_irqrestore(&txring->lock, flags);
1037
Olof Johanssona85b9422007-09-15 13:40:59 -07001038 write_dma_reg(mac, PAS_DMA_TXCHAN_INCR(mac->dma_txch), 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001039
1040 return NETDEV_TX_OK;
1041
1042out_err:
1043 spin_unlock_irqrestore(&txring->lock, flags);
1044 pci_unmap_single(mac->dma_pdev, map, skb->len, PCI_DMA_TODEVICE);
1045 return NETDEV_TX_BUSY;
1046}
1047
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001048static void pasemi_mac_set_rx_mode(struct net_device *dev)
1049{
1050 struct pasemi_mac *mac = netdev_priv(dev);
1051 unsigned int flags;
1052
Olof Johanssona85b9422007-09-15 13:40:59 -07001053 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001054
1055 /* Set promiscuous */
1056 if (dev->flags & IFF_PROMISC)
1057 flags |= PAS_MAC_CFG_PCFG_PR;
1058 else
1059 flags &= ~PAS_MAC_CFG_PCFG_PR;
1060
Olof Johanssona85b9422007-09-15 13:40:59 -07001061 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001062}
1063
1064
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001065static int pasemi_mac_poll(struct napi_struct *napi, int budget)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001066{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001067 struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
1068 struct net_device *dev = mac->netdev;
1069 int pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001070
Olof Johansson829185e2007-09-15 13:53:19 -07001071 pasemi_mac_clean_tx(mac);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001072 pkts = pasemi_mac_clean_rx(mac, budget);
1073 if (pkts < budget) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001074 /* all done, no more packets present */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001075 netif_rx_complete(dev, napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001076
Olof Johansson1b0335ea2007-05-08 00:47:26 -05001077 pasemi_mac_restart_rx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001078 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001079 return pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001080}
1081
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001082static void __iomem * __devinit map_onedev(struct pci_dev *p, int index)
1083{
1084 struct device_node *dn;
1085 void __iomem *ret;
1086
1087 dn = pci_device_to_OF_node(p);
1088 if (!dn)
1089 goto fallback;
1090
1091 ret = of_iomap(dn, index);
1092 if (!ret)
1093 goto fallback;
1094
1095 return ret;
1096fallback:
1097 /* This is hardcoded and ugly, but we have some firmware versions
1098 * that don't provide the register space in the device tree. Luckily
1099 * they are at well-known locations so we can just do the math here.
1100 */
1101 return ioremap(0xe0000000 + (p->devfn << 12), 0x2000);
1102}
1103
1104static int __devinit pasemi_mac_map_regs(struct pasemi_mac *mac)
1105{
1106 struct resource res;
1107 struct device_node *dn;
1108 int err;
1109
1110 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
1111 if (!mac->dma_pdev) {
1112 dev_err(&mac->pdev->dev, "Can't find DMA Controller\n");
1113 return -ENODEV;
1114 }
1115
1116 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
1117 if (!mac->iob_pdev) {
1118 dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n");
1119 return -ENODEV;
1120 }
1121
1122 mac->regs = map_onedev(mac->pdev, 0);
1123 mac->dma_regs = map_onedev(mac->dma_pdev, 0);
1124 mac->iob_regs = map_onedev(mac->iob_pdev, 0);
1125
1126 if (!mac->regs || !mac->dma_regs || !mac->iob_regs) {
1127 dev_err(&mac->pdev->dev, "Can't map registers\n");
1128 return -ENODEV;
1129 }
1130
1131 /* The dma status structure is located in the I/O bridge, and
1132 * is cache coherent.
1133 */
1134 if (!dma_status) {
1135 dn = pci_device_to_OF_node(mac->iob_pdev);
1136 if (dn)
1137 err = of_address_to_resource(dn, 1, &res);
1138 if (!dn || err) {
1139 /* Fallback for old firmware */
1140 res.start = 0xfd800000;
1141 res.end = res.start + 0x1000;
1142 }
1143 dma_status = __ioremap(res.start, res.end-res.start, 0);
1144 }
1145
1146 return 0;
1147}
1148
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001149static int __devinit
1150pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1151{
1152 static int index = 0;
1153 struct net_device *dev;
1154 struct pasemi_mac *mac;
1155 int err;
Joe Perches0795af52007-10-03 17:59:30 -07001156 DECLARE_MAC_BUF(mac_buf);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001157
1158 err = pci_enable_device(pdev);
1159 if (err)
1160 return err;
1161
1162 dev = alloc_etherdev(sizeof(struct pasemi_mac));
1163 if (dev == NULL) {
1164 dev_err(&pdev->dev,
1165 "pasemi_mac: Could not allocate ethernet device.\n");
1166 err = -ENOMEM;
1167 goto out_disable_device;
1168 }
1169
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001170 pci_set_drvdata(pdev, dev);
1171 SET_NETDEV_DEV(dev, &pdev->dev);
1172
1173 mac = netdev_priv(dev);
1174
1175 mac->pdev = pdev;
1176 mac->netdev = dev;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001177
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001178 netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
1179
Olof Johansson6fba8482007-09-15 13:51:11 -07001180 dev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001181
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001182 /* These should come out of the device tree eventually */
1183 mac->dma_txch = index;
1184 mac->dma_rxch = index;
1185
1186 /* We probe GMAC before XAUI, but the DMA interfaces are
1187 * in XAUI, GMAC order.
1188 */
1189 if (index < 4)
1190 mac->dma_if = index + 2;
1191 else
1192 mac->dma_if = index - 4;
1193 index++;
1194
1195 switch (pdev->device) {
1196 case 0xa005:
1197 mac->type = MAC_TYPE_GMAC;
1198 break;
1199 case 0xa006:
1200 mac->type = MAC_TYPE_XAUI;
1201 break;
1202 default:
1203 err = -ENODEV;
1204 goto out;
1205 }
1206
1207 /* get mac addr from device tree */
1208 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
1209 err = -ENODEV;
1210 goto out;
1211 }
1212 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
1213
1214 dev->open = pasemi_mac_open;
1215 dev->stop = pasemi_mac_close;
1216 dev->hard_start_xmit = pasemi_mac_start_tx;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001217 dev->set_multicast_list = pasemi_mac_set_rx_mode;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001218
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001219 err = pasemi_mac_map_regs(mac);
1220 if (err)
1221 goto out;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001222
1223 mac->rx_status = &dma_status->rx_sta[mac->dma_rxch];
1224 mac->tx_status = &dma_status->tx_sta[mac->dma_txch];
1225
Olof Johanssonceb51362007-05-08 00:47:49 -05001226 mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1227
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001228 /* Enable most messages by default */
1229 mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1230
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001231 err = register_netdev(dev);
1232
1233 if (err) {
1234 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1235 err);
1236 goto out;
1237 } else
1238 printk(KERN_INFO "%s: PA Semi %s: intf %d, txch %d, rxch %d, "
Joe Perches0795af52007-10-03 17:59:30 -07001239 "hw addr %s\n",
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001240 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
1241 mac->dma_if, mac->dma_txch, mac->dma_rxch,
Joe Perches0795af52007-10-03 17:59:30 -07001242 print_mac(mac_buf, dev->dev_addr));
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001243
1244 return err;
1245
1246out:
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001247 if (mac->iob_pdev)
1248 pci_dev_put(mac->iob_pdev);
1249 if (mac->dma_pdev)
1250 pci_dev_put(mac->dma_pdev);
1251 if (mac->dma_regs)
1252 iounmap(mac->dma_regs);
1253 if (mac->iob_regs)
1254 iounmap(mac->iob_regs);
1255 if (mac->regs)
1256 iounmap(mac->regs);
1257
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001258 free_netdev(dev);
1259out_disable_device:
1260 pci_disable_device(pdev);
1261 return err;
1262
1263}
1264
1265static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1266{
1267 struct net_device *netdev = pci_get_drvdata(pdev);
1268 struct pasemi_mac *mac;
1269
1270 if (!netdev)
1271 return;
1272
1273 mac = netdev_priv(netdev);
1274
1275 unregister_netdev(netdev);
1276
1277 pci_disable_device(pdev);
1278 pci_dev_put(mac->dma_pdev);
1279 pci_dev_put(mac->iob_pdev);
1280
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001281 iounmap(mac->regs);
1282 iounmap(mac->dma_regs);
1283 iounmap(mac->iob_regs);
1284
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001285 pci_set_drvdata(pdev, NULL);
1286 free_netdev(netdev);
1287}
1288
1289static struct pci_device_id pasemi_mac_pci_tbl[] = {
1290 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1291 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
olof@lixom.netfd178252007-05-12 14:57:36 -05001292 { },
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001293};
1294
1295MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1296
1297static struct pci_driver pasemi_mac_driver = {
1298 .name = "pasemi_mac",
1299 .id_table = pasemi_mac_pci_tbl,
1300 .probe = pasemi_mac_probe,
1301 .remove = __devexit_p(pasemi_mac_remove),
1302};
1303
1304static void __exit pasemi_mac_cleanup_module(void)
1305{
1306 pci_unregister_driver(&pasemi_mac_driver);
1307 __iounmap(dma_status);
1308 dma_status = NULL;
1309}
1310
1311int pasemi_mac_init_module(void)
1312{
1313 return pci_register_driver(&pasemi_mac_driver);
1314}
1315
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001316module_init(pasemi_mac_init_module);
1317module_exit(pasemi_mac_cleanup_module);