blob: 46d5c0eef784656eab24f054f8d0081d8121e6b5 [file] [log] [blame]
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001/*
2 * Copyright (C) 2006-2007 PA Semi, Inc
3 *
4 * Driver for the PA Semi PWRficient onchip 1G/10G Ethernet MACs
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/pci.h>
23#include <linux/interrupt.h>
24#include <linux/dmaengine.h>
25#include <linux/delay.h>
26#include <linux/netdevice.h>
27#include <linux/etherdevice.h>
28#include <asm/dma-mapping.h>
29#include <linux/in.h>
30#include <linux/skbuff.h>
31
32#include <linux/ip.h>
33#include <linux/tcp.h>
34#include <net/checksum.h>
35
Olof Johansson771f7402007-05-08 00:47:21 -050036#include <asm/irq.h>
37
Olof Johanssonf5cd7872007-01-31 21:43:54 -060038#include "pasemi_mac.h"
39
40
41/* TODO list
42 *
43 * - Get rid of pci_{read,write}_config(), map registers with ioremap
44 * for performance
45 * - PHY support
46 * - Multicast support
47 * - Large MTU support
48 * - Other performance improvements
49 */
50
51
52/* Must be a power of two */
53#define RX_RING_SIZE 512
54#define TX_RING_SIZE 512
55
Olof Johanssonceb51362007-05-08 00:47:49 -050056#define DEFAULT_MSG_ENABLE \
57 (NETIF_MSG_DRV | \
58 NETIF_MSG_PROBE | \
59 NETIF_MSG_LINK | \
60 NETIF_MSG_TIMER | \
61 NETIF_MSG_IFDOWN | \
62 NETIF_MSG_IFUP | \
63 NETIF_MSG_RX_ERR | \
64 NETIF_MSG_TX_ERR)
65
Olof Johanssonf5cd7872007-01-31 21:43:54 -060066#define TX_DESC(mac, num) ((mac)->tx->desc[(num) & (TX_RING_SIZE-1)])
67#define TX_DESC_INFO(mac, num) ((mac)->tx->desc_info[(num) & (TX_RING_SIZE-1)])
68#define RX_DESC(mac, num) ((mac)->rx->desc[(num) & (RX_RING_SIZE-1)])
69#define RX_DESC_INFO(mac, num) ((mac)->rx->desc_info[(num) & (RX_RING_SIZE-1)])
70#define RX_BUFF(mac, num) ((mac)->rx->buffers[(num) & (RX_RING_SIZE-1)])
71
72#define BUF_SIZE 1646 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
73
Olof Johanssonceb51362007-05-08 00:47:49 -050074MODULE_LICENSE("GPL");
75MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
76MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
77
78static int debug = -1; /* -1 == use DEFAULT_MSG_ENABLE as value */
79module_param(debug, int, 0);
80MODULE_PARM_DESC(debug, "PA Semi MAC bitmapped debugging message enable value");
Olof Johanssonf5cd7872007-01-31 21:43:54 -060081
82static struct pasdma_status *dma_status;
83
Olof Johanssona85b9422007-09-15 13:40:59 -070084static unsigned int read_iob_reg(struct pasemi_mac *mac, unsigned int reg)
85{
Olof Johanssonb6e05a12007-09-15 13:44:07 -070086 return in_le32(mac->iob_regs+reg);
Olof Johanssona85b9422007-09-15 13:40:59 -070087}
88
89static void write_iob_reg(struct pasemi_mac *mac, unsigned int reg,
90 unsigned int val)
91{
Olof Johanssonb6e05a12007-09-15 13:44:07 -070092 out_le32(mac->iob_regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -070093}
94
95static unsigned int read_mac_reg(struct pasemi_mac *mac, unsigned int reg)
96{
Olof Johanssonb6e05a12007-09-15 13:44:07 -070097 return in_le32(mac->regs+reg);
Olof Johanssona85b9422007-09-15 13:40:59 -070098}
99
100static void write_mac_reg(struct pasemi_mac *mac, unsigned int reg,
101 unsigned int val)
102{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700103 out_le32(mac->regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700104}
105
106static unsigned int read_dma_reg(struct pasemi_mac *mac, unsigned int reg)
107{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700108 return in_le32(mac->dma_regs+reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700109}
110
111static void write_dma_reg(struct pasemi_mac *mac, unsigned int reg,
112 unsigned int val)
113{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700114 out_le32(mac->dma_regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700115}
116
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600117static int pasemi_get_mac_addr(struct pasemi_mac *mac)
118{
119 struct pci_dev *pdev = mac->pdev;
120 struct device_node *dn = pci_device_to_OF_node(pdev);
olof@lixom.net1af7f052007-05-12 14:57:46 -0500121 int len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600122 const u8 *maddr;
123 u8 addr[6];
124
125 if (!dn) {
126 dev_dbg(&pdev->dev,
127 "No device node for mac, not configuring\n");
128 return -ENOENT;
129 }
130
olof@lixom.net1af7f052007-05-12 14:57:46 -0500131 maddr = of_get_property(dn, "local-mac-address", &len);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500132
olof@lixom.net1af7f052007-05-12 14:57:46 -0500133 if (maddr && len == 6) {
134 memcpy(mac->mac_addr, maddr, 6);
135 return 0;
136 }
137
138 /* Some old versions of firmware mistakenly uses mac-address
139 * (and as a string) instead of a byte array in local-mac-address.
140 */
141
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500142 if (maddr == NULL)
Linus Torvalds90287802007-05-08 11:57:17 -0700143 maddr = of_get_property(dn, "mac-address", NULL);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500144
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600145 if (maddr == NULL) {
146 dev_warn(&pdev->dev,
147 "no mac address in device tree, not configuring\n");
148 return -ENOENT;
149 }
150
olof@lixom.net1af7f052007-05-12 14:57:46 -0500151
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600152 if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0],
153 &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) {
154 dev_warn(&pdev->dev,
155 "can't parse mac address, not configuring\n");
156 return -EINVAL;
157 }
158
olof@lixom.net1af7f052007-05-12 14:57:46 -0500159 memcpy(mac->mac_addr, addr, 6);
160
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600161 return 0;
162}
163
164static int pasemi_mac_setup_rx_resources(struct net_device *dev)
165{
166 struct pasemi_mac_rxring *ring;
167 struct pasemi_mac *mac = netdev_priv(dev);
168 int chan_id = mac->dma_rxch;
169
170 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
171
172 if (!ring)
173 goto out_ring;
174
175 spin_lock_init(&ring->lock);
176
177 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
256 ring->desc_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
257 TX_RING_SIZE, GFP_KERNEL);
258 if (!ring->desc_info)
259 goto out_desc_info;
260
261 /* Allocate descriptors */
262 ring->desc = dma_alloc_coherent(&mac->dma_pdev->dev,
263 TX_RING_SIZE *
264 sizeof(struct pas_dma_xct_descr),
265 &ring->dma, GFP_KERNEL);
266 if (!ring->desc)
267 goto out_desc;
268
269 memset(ring->desc, 0, TX_RING_SIZE * sizeof(struct pas_dma_xct_descr));
270
Olof Johanssona85b9422007-09-15 13:40:59 -0700271 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEL(chan_id),
272 PAS_DMA_TXCHAN_BASEL_BRBL(ring->dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600273 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->dma >> 32);
274 val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 2);
275
Olof Johanssona85b9422007-09-15 13:40:59 -0700276 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEU(chan_id), val);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600277
Olof Johanssona85b9422007-09-15 13:40:59 -0700278 write_dma_reg(mac, PAS_DMA_TXCHAN_CFG(chan_id),
279 PAS_DMA_TXCHAN_CFG_TY_IFACE |
280 PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
281 PAS_DMA_TXCHAN_CFG_UP |
282 PAS_DMA_TXCHAN_CFG_WT(2));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600283
284 ring->next_to_use = 0;
285 ring->next_to_clean = 0;
286
287 snprintf(ring->irq_name, sizeof(ring->irq_name),
288 "%s tx", dev->name);
289 mac->tx = ring;
290
291 return 0;
292
293out_desc:
294 kfree(ring->desc_info);
295out_desc_info:
296 kfree(ring);
297out_ring:
298 return -ENOMEM;
299}
300
301static void pasemi_mac_free_tx_resources(struct net_device *dev)
302{
303 struct pasemi_mac *mac = netdev_priv(dev);
304 unsigned int i;
305 struct pasemi_mac_buffer *info;
306 struct pas_dma_xct_descr *dp;
307
308 for (i = 0; i < TX_RING_SIZE; i++) {
309 info = &TX_DESC_INFO(mac, i);
310 dp = &TX_DESC(mac, i);
311 if (info->dma) {
312 if (info->skb) {
313 pci_unmap_single(mac->dma_pdev,
314 info->dma,
315 info->skb->len,
316 PCI_DMA_TODEVICE);
317 dev_kfree_skb_any(info->skb);
318 }
319 info->dma = 0;
320 info->skb = NULL;
321 dp->mactx = 0;
322 dp->ptr = 0;
323 }
324 }
325
326 dma_free_coherent(&mac->dma_pdev->dev,
327 TX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
328 mac->tx->desc, mac->tx->dma);
329
330 kfree(mac->tx->desc_info);
331 kfree(mac->tx);
332 mac->tx = NULL;
333}
334
335static void pasemi_mac_free_rx_resources(struct net_device *dev)
336{
337 struct pasemi_mac *mac = netdev_priv(dev);
338 unsigned int i;
339 struct pasemi_mac_buffer *info;
340 struct pas_dma_xct_descr *dp;
341
342 for (i = 0; i < RX_RING_SIZE; i++) {
343 info = &RX_DESC_INFO(mac, i);
344 dp = &RX_DESC(mac, i);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500345 if (info->skb) {
346 if (info->dma) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600347 pci_unmap_single(mac->dma_pdev,
348 info->dma,
349 info->skb->len,
350 PCI_DMA_FROMDEVICE);
351 dev_kfree_skb_any(info->skb);
352 }
353 info->dma = 0;
354 info->skb = NULL;
355 dp->macrx = 0;
356 dp->ptr = 0;
357 }
358 }
359
360 dma_free_coherent(&mac->dma_pdev->dev,
361 RX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
362 mac->rx->desc, mac->rx->dma);
363
364 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
365 mac->rx->buffers, mac->rx->buf_dma);
366
367 kfree(mac->rx->desc_info);
368 kfree(mac->rx);
369 mac->rx = NULL;
370}
371
372static void pasemi_mac_replenish_rx_ring(struct net_device *dev)
373{
374 struct pasemi_mac *mac = netdev_priv(dev);
375 unsigned int i;
376 int start = mac->rx->next_to_fill;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500377 unsigned int limit, count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600378
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500379 limit = (mac->rx->next_to_clean + RX_RING_SIZE -
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600380 mac->rx->next_to_fill) & (RX_RING_SIZE - 1);
381
382 /* Check to see if we're doing first-time setup */
383 if (unlikely(mac->rx->next_to_clean == 0 && mac->rx->next_to_fill == 0))
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500384 limit = RX_RING_SIZE;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600385
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500386 if (limit <= 0)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600387 return;
388
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500389 i = start;
390 for (count = limit; count; count--) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600391 struct pasemi_mac_buffer *info = &RX_DESC_INFO(mac, i);
392 u64 *buff = &RX_BUFF(mac, i);
393 struct sk_buff *skb;
394 dma_addr_t dma;
395
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500396 /* skb might still be in there for recycle on short receives */
397 if (info->skb)
398 skb = info->skb;
399 else
400 skb = dev_alloc_skb(BUF_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600401
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500402 if (unlikely(!skb))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600403 break;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600404
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600405 dma = pci_map_single(mac->dma_pdev, skb->data, skb->len,
406 PCI_DMA_FROMDEVICE);
407
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500408 if (unlikely(dma_mapping_error(dma))) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600409 dev_kfree_skb_irq(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600410 break;
411 }
412
413 info->skb = skb;
414 info->dma = dma;
415 *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500416 i++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600417 }
418
419 wmb();
420
Olof Johanssona85b9422007-09-15 13:40:59 -0700421 write_dma_reg(mac, PAS_DMA_RXCHAN_INCR(mac->dma_rxch), limit - count);
422 write_dma_reg(mac, PAS_DMA_RXINT_INCR(mac->dma_if), limit - count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600423
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500424 mac->rx->next_to_fill += limit - count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600425}
426
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500427static void pasemi_mac_restart_rx_intr(struct pasemi_mac *mac)
428{
Olof Johansson52a94352007-05-12 18:01:09 -0500429 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500430 /* Re-enable packet count interrupts: finally
431 * ack the packet count interrupt we got in rx_intr.
432 */
433
Olof Johansson52a94352007-05-12 18:01:09 -0500434 pcnt = *mac->rx_status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500435
Olof Johansson52a94352007-05-12 18:01:09 -0500436 reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500437
Olof Johanssona85b9422007-09-15 13:40:59 -0700438 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500439}
440
441static void pasemi_mac_restart_tx_intr(struct pasemi_mac *mac)
442{
Olof Johansson52a94352007-05-12 18:01:09 -0500443 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500444
445 /* Re-enable packet count interrupts */
Olof Johansson52a94352007-05-12 18:01:09 -0500446 pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500447
Olof Johansson52a94352007-05-12 18:01:09 -0500448 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500449
Olof Johanssona85b9422007-09-15 13:40:59 -0700450 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500451}
452
453
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600454static int pasemi_mac_clean_rx(struct pasemi_mac *mac, int limit)
455{
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500456 unsigned int n;
457 int count;
458 struct pas_dma_xct_descr *dp;
459 struct pasemi_mac_buffer *info;
460 struct sk_buff *skb;
461 unsigned int i, len;
462 u64 macrx;
463 dma_addr_t dma;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600464
465 spin_lock(&mac->rx->lock);
466
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500467 n = mac->rx->next_to_clean;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600468
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500469 for (count = limit; count; count--) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600470
471 rmb();
472
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500473 dp = &RX_DESC(mac, n);
474 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 }
495
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500496 skb = info->skb;
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500497 info->dma = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600498
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500499 pci_unmap_single(mac->dma_pdev, dma, skb->len,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600500 PCI_DMA_FROMDEVICE);
501
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500502 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600503
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500504 if (len < 256) {
505 struct sk_buff *new_skb =
506 netdev_alloc_skb(mac->netdev, len + NET_IP_ALIGN);
507 if (new_skb) {
508 skb_reserve(new_skb, NET_IP_ALIGN);
509 memcpy(new_skb->data - NET_IP_ALIGN,
510 skb->data - NET_IP_ALIGN,
511 len + NET_IP_ALIGN);
512 /* 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
521 skb->protocol = eth_type_trans(skb, mac->netdev);
522
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500523 if ((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600524 skb->ip_summed = CHECKSUM_COMPLETE;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500525 skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600526 XCT_MACRX_CSUM_S;
527 } else
528 skb->ip_summed = CHECKSUM_NONE;
529
530 mac->stats.rx_bytes += len;
531 mac->stats.rx_packets++;
532
533 netif_receive_skb(skb);
534
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600535 dp->ptr = 0;
536 dp->macrx = 0;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500537
538 n++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600539 }
540
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500541 mac->rx->next_to_clean += limit - count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600542 pasemi_mac_replenish_rx_ring(mac->netdev);
543
544 spin_unlock(&mac->rx->lock);
545
546 return count;
547}
548
549static int pasemi_mac_clean_tx(struct pasemi_mac *mac)
550{
551 int i;
552 struct pasemi_mac_buffer *info;
553 struct pas_dma_xct_descr *dp;
554 int start, count;
555 int flags;
556
557 spin_lock_irqsave(&mac->tx->lock, flags);
558
559 start = mac->tx->next_to_clean;
560 count = 0;
561
562 for (i = start; i < mac->tx->next_to_use; i++) {
563 dp = &TX_DESC(mac, i);
564 if (!dp || (dp->mactx & XCT_MACTX_O))
565 break;
566
567 count++;
568
569 info = &TX_DESC_INFO(mac, i);
570
571 pci_unmap_single(mac->dma_pdev, info->dma,
572 info->skb->len, PCI_DMA_TODEVICE);
573 dev_kfree_skb_irq(info->skb);
574
575 info->skb = NULL;
576 info->dma = 0;
577 dp->mactx = 0;
578 dp->ptr = 0;
579 }
580 mac->tx->next_to_clean += count;
581 spin_unlock_irqrestore(&mac->tx->lock, flags);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500582 netif_wake_queue(mac->netdev);
583
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600584 return count;
585}
586
587
588static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
589{
590 struct net_device *dev = data;
591 struct pasemi_mac *mac = netdev_priv(dev);
592 unsigned int reg;
593
Olof Johansson6dfa7522007-05-08 00:47:32 -0500594 if (!(*mac->rx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600595 return IRQ_NONE;
596
Olof Johansson6dfa7522007-05-08 00:47:32 -0500597 if (*mac->rx_status & PAS_STATUS_ERROR)
598 printk("rx_status reported error\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600599
Olof Johansson6dfa7522007-05-08 00:47:32 -0500600 /* Don't reset packet count so it won't fire again but clear
601 * all others.
602 */
603
Olof Johansson6dfa7522007-05-08 00:47:32 -0500604 reg = 0;
605 if (*mac->rx_status & PAS_STATUS_SOFT)
606 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
607 if (*mac->rx_status & PAS_STATUS_ERROR)
608 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600609 if (*mac->rx_status & PAS_STATUS_TIMER)
610 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
611
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700612 netif_rx_schedule(dev, &mac->napi);
Olof Johansson6dfa7522007-05-08 00:47:32 -0500613
Olof Johanssona85b9422007-09-15 13:40:59 -0700614 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600615
616 return IRQ_HANDLED;
617}
618
619static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
620{
621 struct net_device *dev = data;
622 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson52a94352007-05-12 18:01:09 -0500623 unsigned int reg, pcnt;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600624
Olof Johansson6dfa7522007-05-08 00:47:32 -0500625 if (!(*mac->tx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600626 return IRQ_NONE;
627
628 pasemi_mac_clean_tx(mac);
629
Olof Johansson52a94352007-05-12 18:01:09 -0500630 pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
631
632 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson6dfa7522007-05-08 00:47:32 -0500633
634 if (*mac->tx_status & PAS_STATUS_SOFT)
635 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
636 if (*mac->tx_status & PAS_STATUS_ERROR)
637 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600638
Olof Johanssona85b9422007-09-15 13:40:59 -0700639 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600640
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600641 return IRQ_HANDLED;
642}
643
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500644static void pasemi_adjust_link(struct net_device *dev)
645{
646 struct pasemi_mac *mac = netdev_priv(dev);
647 int msg;
648 unsigned int flags;
649 unsigned int new_flags;
650
651 if (!mac->phydev->link) {
652 /* If no link, MAC speed settings don't matter. Just report
653 * link down and return.
654 */
655 if (mac->link && netif_msg_link(mac))
656 printk(KERN_INFO "%s: Link is down.\n", dev->name);
657
658 netif_carrier_off(dev);
659 mac->link = 0;
660
661 return;
662 } else
663 netif_carrier_on(dev);
664
Olof Johanssona85b9422007-09-15 13:40:59 -0700665 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500666 new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
667 PAS_MAC_CFG_PCFG_TSR_M);
668
669 if (!mac->phydev->duplex)
670 new_flags |= PAS_MAC_CFG_PCFG_HD;
671
672 switch (mac->phydev->speed) {
673 case 1000:
674 new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
675 PAS_MAC_CFG_PCFG_TSR_1G;
676 break;
677 case 100:
678 new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
679 PAS_MAC_CFG_PCFG_TSR_100M;
680 break;
681 case 10:
682 new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
683 PAS_MAC_CFG_PCFG_TSR_10M;
684 break;
685 default:
686 printk("Unsupported speed %d\n", mac->phydev->speed);
687 }
688
689 /* Print on link or speed/duplex change */
690 msg = mac->link != mac->phydev->link || flags != new_flags;
691
692 mac->duplex = mac->phydev->duplex;
693 mac->speed = mac->phydev->speed;
694 mac->link = mac->phydev->link;
695
696 if (new_flags != flags)
Olof Johanssona85b9422007-09-15 13:40:59 -0700697 write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500698
699 if (msg && netif_msg_link(mac))
700 printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
701 dev->name, mac->speed, mac->duplex ? "full" : "half");
702}
703
704static int pasemi_mac_phy_init(struct net_device *dev)
705{
706 struct pasemi_mac *mac = netdev_priv(dev);
707 struct device_node *dn, *phy_dn;
708 struct phy_device *phydev;
709 unsigned int phy_id;
710 const phandle *ph;
711 const unsigned int *prop;
712 struct resource r;
713 int ret;
714
715 dn = pci_device_to_OF_node(mac->pdev);
Linus Torvalds90287802007-05-08 11:57:17 -0700716 ph = of_get_property(dn, "phy-handle", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500717 if (!ph)
718 return -ENODEV;
719 phy_dn = of_find_node_by_phandle(*ph);
720
Linus Torvalds90287802007-05-08 11:57:17 -0700721 prop = of_get_property(phy_dn, "reg", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500722 ret = of_address_to_resource(phy_dn->parent, 0, &r);
723 if (ret)
724 goto err;
725
726 phy_id = *prop;
727 snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);
728
729 of_node_put(phy_dn);
730
731 mac->link = 0;
732 mac->speed = 0;
733 mac->duplex = -1;
734
735 phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);
736
737 if (IS_ERR(phydev)) {
738 printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
739 return PTR_ERR(phydev);
740 }
741
742 mac->phydev = phydev;
743
744 return 0;
745
746err:
747 of_node_put(phy_dn);
748 return -ENODEV;
749}
750
751
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600752static int pasemi_mac_open(struct net_device *dev)
753{
754 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson771f7402007-05-08 00:47:21 -0500755 int base_irq;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600756 unsigned int flags;
757 int ret;
758
759 /* enable rx section */
Olof Johanssona85b9422007-09-15 13:40:59 -0700760 write_dma_reg(mac, PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600761
762 /* enable tx section */
Olof Johanssona85b9422007-09-15 13:40:59 -0700763 write_dma_reg(mac, PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600764
765 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
766 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
767 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
768
Olof Johanssona85b9422007-09-15 13:40:59 -0700769 write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600770
771 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
772 PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
773
774 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
775
Olof Johanssona85b9422007-09-15 13:40:59 -0700776 write_iob_reg(mac, PAS_IOB_DMA_RXCH_CFG(mac->dma_rxch),
777 PAS_IOB_DMA_RXCH_CFG_CNTTH(0));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600778
Olof Johanssona85b9422007-09-15 13:40:59 -0700779 write_iob_reg(mac, PAS_IOB_DMA_TXCH_CFG(mac->dma_txch),
780 PAS_IOB_DMA_TXCH_CFG_CNTTH(32));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600781
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500782 /* Clear out any residual packet count state from firmware */
783 pasemi_mac_restart_rx_intr(mac);
784 pasemi_mac_restart_tx_intr(mac);
785
Olof Johansson6dfa7522007-05-08 00:47:32 -0500786 /* 0xffffff is max value, about 16ms */
Olof Johanssona85b9422007-09-15 13:40:59 -0700787 write_iob_reg(mac, PAS_IOB_DMA_COM_TIMEOUTCFG,
788 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0xffffff));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600789
Olof Johanssona85b9422007-09-15 13:40:59 -0700790 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600791
792 ret = pasemi_mac_setup_rx_resources(dev);
793 if (ret)
794 goto out_rx_resources;
795
796 ret = pasemi_mac_setup_tx_resources(dev);
797 if (ret)
798 goto out_tx_resources;
799
Olof Johanssona85b9422007-09-15 13:40:59 -0700800 write_mac_reg(mac, PAS_MAC_IPC_CHNL,
801 PAS_MAC_IPC_CHNL_DCHNO(mac->dma_rxch) |
802 PAS_MAC_IPC_CHNL_BCH(mac->dma_rxch));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600803
804 /* enable rx if */
Olof Johanssona85b9422007-09-15 13:40:59 -0700805 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
806 PAS_DMA_RXINT_RCMDSTA_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600807
808 /* enable rx channel */
Olof Johanssona85b9422007-09-15 13:40:59 -0700809 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
810 PAS_DMA_RXCHAN_CCMDSTA_EN |
811 PAS_DMA_RXCHAN_CCMDSTA_DU);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600812
813 /* enable tx channel */
Olof Johanssona85b9422007-09-15 13:40:59 -0700814 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
815 PAS_DMA_TXCHAN_TCMDSTA_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600816
817 pasemi_mac_replenish_rx_ring(dev);
818
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500819 ret = pasemi_mac_phy_init(dev);
820 /* Some configs don't have PHYs (XAUI etc), so don't complain about
821 * failed init due to -ENODEV.
822 */
823 if (ret && ret != -ENODEV)
824 dev_warn(&mac->pdev->dev, "phy init failed: %d\n", ret);
825
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600826 netif_start_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700827 napi_enable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600828
Olof Johansson771f7402007-05-08 00:47:21 -0500829 /* Interrupts are a bit different for our DMA controller: While
830 * it's got one a regular PCI device header, the interrupt there
831 * is really the base of the range it's using. Each tx and rx
832 * channel has it's own interrupt source.
833 */
834
835 base_irq = virq_to_hw(mac->dma_pdev->irq);
836
837 mac->tx_irq = irq_create_mapping(NULL, base_irq + mac->dma_txch);
838 mac->rx_irq = irq_create_mapping(NULL, base_irq + 20 + mac->dma_txch);
839
840 ret = request_irq(mac->tx_irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600841 mac->tx->irq_name, dev);
842 if (ret) {
843 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500844 base_irq + mac->dma_txch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600845 goto out_tx_int;
846 }
847
Olof Johansson771f7402007-05-08 00:47:21 -0500848 ret = request_irq(mac->rx_irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600849 mac->rx->irq_name, dev);
850 if (ret) {
851 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500852 base_irq + 20 + mac->dma_rxch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600853 goto out_rx_int;
854 }
855
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500856 if (mac->phydev)
857 phy_start(mac->phydev);
858
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600859 return 0;
860
861out_rx_int:
Olof Johansson771f7402007-05-08 00:47:21 -0500862 free_irq(mac->tx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600863out_tx_int:
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700864 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600865 netif_stop_queue(dev);
866 pasemi_mac_free_tx_resources(dev);
867out_tx_resources:
868 pasemi_mac_free_rx_resources(dev);
869out_rx_resources:
870
871 return ret;
872}
873
874#define MAX_RETRIES 5000
875
876static int pasemi_mac_close(struct net_device *dev)
877{
878 struct pasemi_mac *mac = netdev_priv(dev);
879 unsigned int stat;
880 int retries;
881
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500882 if (mac->phydev) {
883 phy_stop(mac->phydev);
884 phy_disconnect(mac->phydev);
885 }
886
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600887 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700888 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600889
890 /* Clean out any pending buffers */
891 pasemi_mac_clean_tx(mac);
892 pasemi_mac_clean_rx(mac, RX_RING_SIZE);
893
894 /* Disable interface */
Olof Johanssona85b9422007-09-15 13:40:59 -0700895 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), PAS_DMA_TXCHAN_TCMDSTA_ST);
896 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), PAS_DMA_RXINT_RCMDSTA_ST);
897 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), PAS_DMA_RXCHAN_CCMDSTA_ST);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600898
899 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -0700900 stat = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
Olof Johansson0ce68c72007-04-28 15:36:40 -0500901 if (!(stat & PAS_DMA_TXCHAN_TCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600902 break;
903 cond_resched();
904 }
905
Olof Johansson0ce68c72007-04-28 15:36:40 -0500906 if (stat & PAS_DMA_TXCHAN_TCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600907 dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600908
909 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -0700910 stat = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
Olof Johansson0ce68c72007-04-28 15:36:40 -0500911 if (!(stat & PAS_DMA_RXCHAN_CCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600912 break;
913 cond_resched();
914 }
915
Olof Johansson0ce68c72007-04-28 15:36:40 -0500916 if (stat & PAS_DMA_RXCHAN_CCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600917 dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600918
919 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -0700920 stat = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
Olof Johansson0ce68c72007-04-28 15:36:40 -0500921 if (!(stat & PAS_DMA_RXINT_RCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600922 break;
923 cond_resched();
924 }
925
Olof Johansson0ce68c72007-04-28 15:36:40 -0500926 if (stat & PAS_DMA_RXINT_RCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600927 dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600928
929 /* Then, disable the channel. This must be done separately from
930 * stopping, since you can't disable when active.
931 */
932
Olof Johanssona85b9422007-09-15 13:40:59 -0700933 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), 0);
934 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), 0);
935 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600936
Olof Johansson771f7402007-05-08 00:47:21 -0500937 free_irq(mac->tx_irq, dev);
938 free_irq(mac->rx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600939
940 /* Free resources */
941 pasemi_mac_free_rx_resources(dev);
942 pasemi_mac_free_tx_resources(dev);
943
944 return 0;
945}
946
947static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
948{
949 struct pasemi_mac *mac = netdev_priv(dev);
950 struct pasemi_mac_txring *txring;
951 struct pasemi_mac_buffer *info;
952 struct pas_dma_xct_descr *dp;
953 u64 dflags;
954 dma_addr_t map;
955 int flags;
956
957 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_SS | XCT_MACTX_CRC_PAD;
958
959 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700960 const unsigned char *nh = skb_network_header(skb);
961
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700962 switch (ip_hdr(skb)->protocol) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600963 case IPPROTO_TCP:
964 dflags |= XCT_MACTX_CSUM_TCP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300965 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700966 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600967 break;
968 case IPPROTO_UDP:
969 dflags |= XCT_MACTX_CSUM_UDP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300970 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700971 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600972 break;
973 }
974 }
975
976 map = pci_map_single(mac->dma_pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
977
978 if (dma_mapping_error(map))
979 return NETDEV_TX_BUSY;
980
981 txring = mac->tx;
982
983 spin_lock_irqsave(&txring->lock, flags);
984
985 if (txring->next_to_clean - txring->next_to_use == TX_RING_SIZE) {
986 spin_unlock_irqrestore(&txring->lock, flags);
987 pasemi_mac_clean_tx(mac);
Olof Johansson52a94352007-05-12 18:01:09 -0500988 pasemi_mac_restart_tx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600989 spin_lock_irqsave(&txring->lock, flags);
990
991 if (txring->next_to_clean - txring->next_to_use ==
992 TX_RING_SIZE) {
993 /* Still no room -- stop the queue and wait for tx
994 * intr when there's room.
995 */
996 netif_stop_queue(dev);
997 goto out_err;
998 }
999 }
1000
1001
1002 dp = &TX_DESC(mac, txring->next_to_use);
1003 info = &TX_DESC_INFO(mac, txring->next_to_use);
1004
1005 dp->mactx = dflags | XCT_MACTX_LLEN(skb->len);
1006 dp->ptr = XCT_PTR_LEN(skb->len) | XCT_PTR_ADDR(map);
1007 info->dma = map;
1008 info->skb = skb;
1009
1010 txring->next_to_use++;
1011 mac->stats.tx_packets++;
1012 mac->stats.tx_bytes += skb->len;
1013
1014 spin_unlock_irqrestore(&txring->lock, flags);
1015
Olof Johanssona85b9422007-09-15 13:40:59 -07001016 write_dma_reg(mac, PAS_DMA_TXCHAN_INCR(mac->dma_txch), 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001017
1018 return NETDEV_TX_OK;
1019
1020out_err:
1021 spin_unlock_irqrestore(&txring->lock, flags);
1022 pci_unmap_single(mac->dma_pdev, map, skb->len, PCI_DMA_TODEVICE);
1023 return NETDEV_TX_BUSY;
1024}
1025
1026static struct net_device_stats *pasemi_mac_get_stats(struct net_device *dev)
1027{
1028 struct pasemi_mac *mac = netdev_priv(dev);
1029
1030 return &mac->stats;
1031}
1032
Olof Johanssonceb51362007-05-08 00:47:49 -05001033
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001034static void pasemi_mac_set_rx_mode(struct net_device *dev)
1035{
1036 struct pasemi_mac *mac = netdev_priv(dev);
1037 unsigned int flags;
1038
Olof Johanssona85b9422007-09-15 13:40:59 -07001039 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001040
1041 /* Set promiscuous */
1042 if (dev->flags & IFF_PROMISC)
1043 flags |= PAS_MAC_CFG_PCFG_PR;
1044 else
1045 flags &= ~PAS_MAC_CFG_PCFG_PR;
1046
Olof Johanssona85b9422007-09-15 13:40:59 -07001047 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001048}
1049
1050
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001051static int pasemi_mac_poll(struct napi_struct *napi, int budget)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001052{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001053 struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
1054 struct net_device *dev = mac->netdev;
1055 int pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001056
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001057 pkts = pasemi_mac_clean_rx(mac, budget);
1058 if (pkts < budget) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001059 /* all done, no more packets present */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001060 netif_rx_complete(dev, napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001061
Olof Johansson1b0335ea2007-05-08 00:47:26 -05001062 pasemi_mac_restart_rx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001063 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001064 return pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001065}
1066
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001067static void __iomem * __devinit map_onedev(struct pci_dev *p, int index)
1068{
1069 struct device_node *dn;
1070 void __iomem *ret;
1071
1072 dn = pci_device_to_OF_node(p);
1073 if (!dn)
1074 goto fallback;
1075
1076 ret = of_iomap(dn, index);
1077 if (!ret)
1078 goto fallback;
1079
1080 return ret;
1081fallback:
1082 /* This is hardcoded and ugly, but we have some firmware versions
1083 * that don't provide the register space in the device tree. Luckily
1084 * they are at well-known locations so we can just do the math here.
1085 */
1086 return ioremap(0xe0000000 + (p->devfn << 12), 0x2000);
1087}
1088
1089static int __devinit pasemi_mac_map_regs(struct pasemi_mac *mac)
1090{
1091 struct resource res;
1092 struct device_node *dn;
1093 int err;
1094
1095 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
1096 if (!mac->dma_pdev) {
1097 dev_err(&mac->pdev->dev, "Can't find DMA Controller\n");
1098 return -ENODEV;
1099 }
1100
1101 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
1102 if (!mac->iob_pdev) {
1103 dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n");
1104 return -ENODEV;
1105 }
1106
1107 mac->regs = map_onedev(mac->pdev, 0);
1108 mac->dma_regs = map_onedev(mac->dma_pdev, 0);
1109 mac->iob_regs = map_onedev(mac->iob_pdev, 0);
1110
1111 if (!mac->regs || !mac->dma_regs || !mac->iob_regs) {
1112 dev_err(&mac->pdev->dev, "Can't map registers\n");
1113 return -ENODEV;
1114 }
1115
1116 /* The dma status structure is located in the I/O bridge, and
1117 * is cache coherent.
1118 */
1119 if (!dma_status) {
1120 dn = pci_device_to_OF_node(mac->iob_pdev);
1121 if (dn)
1122 err = of_address_to_resource(dn, 1, &res);
1123 if (!dn || err) {
1124 /* Fallback for old firmware */
1125 res.start = 0xfd800000;
1126 res.end = res.start + 0x1000;
1127 }
1128 dma_status = __ioremap(res.start, res.end-res.start, 0);
1129 }
1130
1131 return 0;
1132}
1133
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001134static int __devinit
1135pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1136{
1137 static int index = 0;
1138 struct net_device *dev;
1139 struct pasemi_mac *mac;
1140 int err;
1141
1142 err = pci_enable_device(pdev);
1143 if (err)
1144 return err;
1145
1146 dev = alloc_etherdev(sizeof(struct pasemi_mac));
1147 if (dev == NULL) {
1148 dev_err(&pdev->dev,
1149 "pasemi_mac: Could not allocate ethernet device.\n");
1150 err = -ENOMEM;
1151 goto out_disable_device;
1152 }
1153
1154 SET_MODULE_OWNER(dev);
1155 pci_set_drvdata(pdev, dev);
1156 SET_NETDEV_DEV(dev, &pdev->dev);
1157
1158 mac = netdev_priv(dev);
1159
1160 mac->pdev = pdev;
1161 mac->netdev = dev;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001162
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001163 netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
1164
1165 dev->features = NETIF_F_HW_CSUM;
1166
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001167 /* These should come out of the device tree eventually */
1168 mac->dma_txch = index;
1169 mac->dma_rxch = index;
1170
1171 /* We probe GMAC before XAUI, but the DMA interfaces are
1172 * in XAUI, GMAC order.
1173 */
1174 if (index < 4)
1175 mac->dma_if = index + 2;
1176 else
1177 mac->dma_if = index - 4;
1178 index++;
1179
1180 switch (pdev->device) {
1181 case 0xa005:
1182 mac->type = MAC_TYPE_GMAC;
1183 break;
1184 case 0xa006:
1185 mac->type = MAC_TYPE_XAUI;
1186 break;
1187 default:
1188 err = -ENODEV;
1189 goto out;
1190 }
1191
1192 /* get mac addr from device tree */
1193 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
1194 err = -ENODEV;
1195 goto out;
1196 }
1197 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
1198
1199 dev->open = pasemi_mac_open;
1200 dev->stop = pasemi_mac_close;
1201 dev->hard_start_xmit = pasemi_mac_start_tx;
1202 dev->get_stats = pasemi_mac_get_stats;
1203 dev->set_multicast_list = pasemi_mac_set_rx_mode;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001204
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001205 err = pasemi_mac_map_regs(mac);
1206 if (err)
1207 goto out;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001208
1209 mac->rx_status = &dma_status->rx_sta[mac->dma_rxch];
1210 mac->tx_status = &dma_status->tx_sta[mac->dma_txch];
1211
Olof Johanssonceb51362007-05-08 00:47:49 -05001212 mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1213
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001214 /* Enable most messages by default */
1215 mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1216
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001217 err = register_netdev(dev);
1218
1219 if (err) {
1220 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1221 err);
1222 goto out;
1223 } else
1224 printk(KERN_INFO "%s: PA Semi %s: intf %d, txch %d, rxch %d, "
1225 "hw addr %02x:%02x:%02x:%02x:%02x:%02x\n",
1226 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
1227 mac->dma_if, mac->dma_txch, mac->dma_rxch,
1228 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1229 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1230
1231 return err;
1232
1233out:
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001234 if (mac->iob_pdev)
1235 pci_dev_put(mac->iob_pdev);
1236 if (mac->dma_pdev)
1237 pci_dev_put(mac->dma_pdev);
1238 if (mac->dma_regs)
1239 iounmap(mac->dma_regs);
1240 if (mac->iob_regs)
1241 iounmap(mac->iob_regs);
1242 if (mac->regs)
1243 iounmap(mac->regs);
1244
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001245 free_netdev(dev);
1246out_disable_device:
1247 pci_disable_device(pdev);
1248 return err;
1249
1250}
1251
1252static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1253{
1254 struct net_device *netdev = pci_get_drvdata(pdev);
1255 struct pasemi_mac *mac;
1256
1257 if (!netdev)
1258 return;
1259
1260 mac = netdev_priv(netdev);
1261
1262 unregister_netdev(netdev);
1263
1264 pci_disable_device(pdev);
1265 pci_dev_put(mac->dma_pdev);
1266 pci_dev_put(mac->iob_pdev);
1267
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001268 iounmap(mac->regs);
1269 iounmap(mac->dma_regs);
1270 iounmap(mac->iob_regs);
1271
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001272 pci_set_drvdata(pdev, NULL);
1273 free_netdev(netdev);
1274}
1275
1276static struct pci_device_id pasemi_mac_pci_tbl[] = {
1277 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1278 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
olof@lixom.netfd178252007-05-12 14:57:36 -05001279 { },
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001280};
1281
1282MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1283
1284static struct pci_driver pasemi_mac_driver = {
1285 .name = "pasemi_mac",
1286 .id_table = pasemi_mac_pci_tbl,
1287 .probe = pasemi_mac_probe,
1288 .remove = __devexit_p(pasemi_mac_remove),
1289};
1290
1291static void __exit pasemi_mac_cleanup_module(void)
1292{
1293 pci_unregister_driver(&pasemi_mac_driver);
1294 __iounmap(dma_status);
1295 dma_status = NULL;
1296}
1297
1298int pasemi_mac_init_module(void)
1299{
1300 return pci_register_driver(&pasemi_mac_driver);
1301}
1302
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001303module_init(pasemi_mac_init_module);
1304module_exit(pasemi_mac_cleanup_module);