blob: 683e8de0ac891197bcfc3065834617f08e16a8f7 [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>
Olof Johanssonaf289e82007-10-03 13:03:54 -050037#include <asm/firmware.h>
Olof Johansson40afa532007-11-28 20:56:04 -060038#include <asm/pasemi_dma.h>
Olof Johansson771f7402007-05-08 00:47:21 -050039
Olof Johanssonf5cd7872007-01-31 21:43:54 -060040#include "pasemi_mac.h"
41
Olof Johansson8dc121a2007-10-02 16:26:53 -050042/* We have our own align, since ppc64 in general has it at 0 because
43 * of design flaws in some of the server bridge chips. However, for
44 * PWRficient doing the unaligned copies is more expensive than doing
45 * unaligned DMA, so make sure the data is aligned instead.
46 */
47#define LOCAL_SKB_ALIGN 2
Olof Johanssonf5cd7872007-01-31 21:43:54 -060048
49/* TODO list
50 *
Olof Johanssonf5cd7872007-01-31 21:43:54 -060051 * - Multicast support
52 * - Large MTU support
Olof Johansson7ddeae22007-10-02 16:27:28 -050053 * - SW LRO
54 * - Multiqueue RX/TX
Olof Johanssonf5cd7872007-01-31 21:43:54 -060055 */
56
57
58/* Must be a power of two */
Olof Johansson5c153322007-11-28 20:56:41 -060059#define RX_RING_SIZE 1024
Olof Johanssonad5da102007-10-02 16:27:15 -050060#define TX_RING_SIZE 4096
Olof Johanssonf5cd7872007-01-31 21:43:54 -060061
Olof Johanssonceb51362007-05-08 00:47:49 -050062#define DEFAULT_MSG_ENABLE \
63 (NETIF_MSG_DRV | \
64 NETIF_MSG_PROBE | \
65 NETIF_MSG_LINK | \
66 NETIF_MSG_TIMER | \
67 NETIF_MSG_IFDOWN | \
68 NETIF_MSG_IFUP | \
69 NETIF_MSG_RX_ERR | \
70 NETIF_MSG_TX_ERR)
71
Olof Johansson34c20622007-11-28 20:56:32 -060072#define TX_DESC(tx, num) ((tx)->chan.ring_virt[(num) & (TX_RING_SIZE-1)])
Olof Johansson72b05b92007-11-28 20:54:28 -060073#define TX_DESC_INFO(tx, num) ((tx)->ring_info[(num) & (TX_RING_SIZE-1)])
Olof Johansson34c20622007-11-28 20:56:32 -060074#define RX_DESC(rx, num) ((rx)->chan.ring_virt[(num) & (RX_RING_SIZE-1)])
Olof Johansson72b05b92007-11-28 20:54:28 -060075#define RX_DESC_INFO(rx, num) ((rx)->ring_info[(num) & (RX_RING_SIZE-1)])
76#define RX_BUFF(rx, num) ((rx)->buffers[(num) & (RX_RING_SIZE-1)])
Olof Johanssonf5cd7872007-01-31 21:43:54 -060077
Olof Johansson021fa222007-08-22 09:13:11 -050078#define RING_USED(ring) (((ring)->next_to_fill - (ring)->next_to_clean) \
79 & ((ring)->size - 1))
80#define RING_AVAIL(ring) ((ring->size) - RING_USED(ring))
81
Olof Johanssonf5cd7872007-01-31 21:43:54 -060082#define BUF_SIZE 1646 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
83
Olof Johanssonceb51362007-05-08 00:47:49 -050084MODULE_LICENSE("GPL");
85MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
86MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
87
88static int debug = -1; /* -1 == use DEFAULT_MSG_ENABLE as value */
89module_param(debug, int, 0);
90MODULE_PARM_DESC(debug, "PA Semi MAC bitmapped debugging message enable value");
Olof Johanssonf5cd7872007-01-31 21:43:54 -060091
Olof Johanssonaf289e82007-10-03 13:03:54 -050092static int translation_enabled(void)
93{
94#if defined(CONFIG_PPC_PASEMI_IOMMU_DMA_FORCE)
95 return 1;
96#else
97 return firmware_has_feature(FW_FEATURE_LPAR);
98#endif
99}
100
Olof Johansson34c20622007-11-28 20:56:32 -0600101static void write_iob_reg(unsigned int reg, unsigned int val)
Olof Johanssona85b9422007-09-15 13:40:59 -0700102{
Olof Johansson34c20622007-11-28 20:56:32 -0600103 pasemi_write_iob_reg(reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700104}
105
Olof Johansson5c153322007-11-28 20:56:41 -0600106static unsigned int read_mac_reg(const struct pasemi_mac *mac, unsigned int reg)
Olof Johanssona85b9422007-09-15 13:40:59 -0700107{
Olof Johansson34c20622007-11-28 20:56:32 -0600108 return pasemi_read_mac_reg(mac->dma_if, reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700109}
110
Olof Johansson5c153322007-11-28 20:56:41 -0600111static void write_mac_reg(const struct pasemi_mac *mac, unsigned int reg,
Olof Johanssona85b9422007-09-15 13:40:59 -0700112 unsigned int val)
113{
Olof Johansson34c20622007-11-28 20:56:32 -0600114 pasemi_write_mac_reg(mac->dma_if, reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700115}
116
Olof Johansson34c20622007-11-28 20:56:32 -0600117static unsigned int read_dma_reg(unsigned int reg)
Olof Johanssona85b9422007-09-15 13:40:59 -0700118{
Olof Johansson34c20622007-11-28 20:56:32 -0600119 return pasemi_read_dma_reg(reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700120}
121
Olof Johansson34c20622007-11-28 20:56:32 -0600122static void write_dma_reg(unsigned int reg, unsigned int val)
Olof Johanssona85b9422007-09-15 13:40:59 -0700123{
Olof Johansson34c20622007-11-28 20:56:32 -0600124 pasemi_write_dma_reg(reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700125}
126
Olof Johansson5c153322007-11-28 20:56:41 -0600127static struct pasemi_mac_rxring *rx_ring(const struct pasemi_mac *mac)
Olof Johansson72b05b92007-11-28 20:54:28 -0600128{
129 return mac->rx;
130}
131
Olof Johansson5c153322007-11-28 20:56:41 -0600132static struct pasemi_mac_txring *tx_ring(const struct pasemi_mac *mac)
Olof Johansson72b05b92007-11-28 20:54:28 -0600133{
134 return mac->tx;
135}
136
Olof Johansson5c153322007-11-28 20:56:41 -0600137static inline void prefetch_skb(const struct sk_buff *skb)
138{
139 const void *d = skb;
140
141 prefetch(d);
142 prefetch(d+64);
143 prefetch(d+128);
144 prefetch(d+192);
145}
146
Olof Johansson34c20622007-11-28 20:56:32 -0600147static int mac_to_intf(struct pasemi_mac *mac)
148{
149 struct pci_dev *pdev = mac->pdev;
150 u32 tmp;
151 int nintf, off, i, j;
152 int devfn = pdev->devfn;
153
154 tmp = read_dma_reg(PAS_DMA_CAP_IFI);
155 nintf = (tmp & PAS_DMA_CAP_IFI_NIN_M) >> PAS_DMA_CAP_IFI_NIN_S;
156 off = (tmp & PAS_DMA_CAP_IFI_IOFF_M) >> PAS_DMA_CAP_IFI_IOFF_S;
157
158 /* IOFF contains the offset to the registers containing the
159 * DMA interface-to-MAC-pci-id mappings, and NIN contains number
160 * of total interfaces. Each register contains 4 devfns.
161 * Just do a linear search until we find the devfn of the MAC
162 * we're trying to look up.
163 */
164
165 for (i = 0; i < (nintf+3)/4; i++) {
166 tmp = read_dma_reg(off+4*i);
167 for (j = 0; j < 4; j++) {
168 if (((tmp >> (8*j)) & 0xff) == devfn)
169 return i*4 + j;
170 }
171 }
172 return -1;
173}
174
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600175static int pasemi_get_mac_addr(struct pasemi_mac *mac)
176{
177 struct pci_dev *pdev = mac->pdev;
178 struct device_node *dn = pci_device_to_OF_node(pdev);
olof@lixom.net1af7f052007-05-12 14:57:46 -0500179 int len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600180 const u8 *maddr;
181 u8 addr[6];
182
183 if (!dn) {
184 dev_dbg(&pdev->dev,
185 "No device node for mac, not configuring\n");
186 return -ENOENT;
187 }
188
olof@lixom.net1af7f052007-05-12 14:57:46 -0500189 maddr = of_get_property(dn, "local-mac-address", &len);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500190
olof@lixom.net1af7f052007-05-12 14:57:46 -0500191 if (maddr && len == 6) {
192 memcpy(mac->mac_addr, maddr, 6);
193 return 0;
194 }
195
196 /* Some old versions of firmware mistakenly uses mac-address
197 * (and as a string) instead of a byte array in local-mac-address.
198 */
199
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500200 if (maddr == NULL)
Linus Torvalds90287802007-05-08 11:57:17 -0700201 maddr = of_get_property(dn, "mac-address", NULL);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500202
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600203 if (maddr == NULL) {
204 dev_warn(&pdev->dev,
205 "no mac address in device tree, not configuring\n");
206 return -ENOENT;
207 }
208
olof@lixom.net1af7f052007-05-12 14:57:46 -0500209
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600210 if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0],
211 &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) {
212 dev_warn(&pdev->dev,
213 "can't parse mac address, not configuring\n");
214 return -EINVAL;
215 }
216
olof@lixom.net1af7f052007-05-12 14:57:46 -0500217 memcpy(mac->mac_addr, addr, 6);
218
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600219 return 0;
220}
221
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500222static int pasemi_mac_unmap_tx_skb(struct pasemi_mac *mac,
223 struct sk_buff *skb,
Olof Johansson5c153322007-11-28 20:56:41 -0600224 const dma_addr_t *dmas)
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500225{
226 int f;
227 int nfrags = skb_shinfo(skb)->nr_frags;
Olof Johansson5c153322007-11-28 20:56:41 -0600228 struct pci_dev *pdev = mac->dma_pdev;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500229
Olof Johansson5c153322007-11-28 20:56:41 -0600230 pci_unmap_single(pdev, dmas[0], skb_headlen(skb), PCI_DMA_TODEVICE);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500231
232 for (f = 0; f < nfrags; f++) {
233 skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
234
Olof Johansson5c153322007-11-28 20:56:41 -0600235 pci_unmap_page(pdev, dmas[f+1], frag->size, PCI_DMA_TODEVICE);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500236 }
237 dev_kfree_skb_irq(skb);
238
239 /* Freed descriptor slot + main SKB ptr + nfrags additional ptrs,
240 * aligned up to a power of 2
241 */
242 return (nfrags + 3) & ~1;
243}
244
Olof Johansson5c153322007-11-28 20:56:41 -0600245static int pasemi_mac_setup_rx_resources(const struct net_device *dev)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600246{
247 struct pasemi_mac_rxring *ring;
248 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson34c20622007-11-28 20:56:32 -0600249 int chno;
Olof Johanssonaf289e82007-10-03 13:03:54 -0500250 unsigned int cfg;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600251
Olof Johansson34c20622007-11-28 20:56:32 -0600252 ring = pasemi_dma_alloc_chan(RXCHAN, sizeof(struct pasemi_mac_rxring),
253 offsetof(struct pasemi_mac_rxring, chan));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600254
Olof Johansson34c20622007-11-28 20:56:32 -0600255 if (!ring) {
256 dev_err(&mac->pdev->dev, "Can't allocate RX channel\n");
257 goto out_chan;
258 }
259 chno = ring->chan.chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600260
261 spin_lock_init(&ring->lock);
262
Olof Johansson021fa222007-08-22 09:13:11 -0500263 ring->size = RX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500264 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600265 RX_RING_SIZE, GFP_KERNEL);
266
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500267 if (!ring->ring_info)
268 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600269
270 /* Allocate descriptors */
Olof Johansson34c20622007-11-28 20:56:32 -0600271 if (pasemi_dma_alloc_ring(&ring->chan, RX_RING_SIZE))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500272 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600273
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600274 ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev,
275 RX_RING_SIZE * sizeof(u64),
276 &ring->buf_dma, GFP_KERNEL);
277 if (!ring->buffers)
Olof Johansson34c20622007-11-28 20:56:32 -0600278 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600279
280 memset(ring->buffers, 0, RX_RING_SIZE * sizeof(u64));
281
Olof Johansson34c20622007-11-28 20:56:32 -0600282 write_dma_reg(PAS_DMA_RXCHAN_BASEL(chno),
283 PAS_DMA_RXCHAN_BASEL_BRBL(ring->chan.ring_dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600284
Olof Johansson34c20622007-11-28 20:56:32 -0600285 write_dma_reg(PAS_DMA_RXCHAN_BASEU(chno),
286 PAS_DMA_RXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32) |
287 PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600288
Olof Johansson5c153322007-11-28 20:56:41 -0600289 cfg = PAS_DMA_RXCHAN_CFG_HBU(2);
Olof Johanssonaf289e82007-10-03 13:03:54 -0500290
291 if (translation_enabled())
292 cfg |= PAS_DMA_RXCHAN_CFG_CTR;
293
Olof Johansson34c20622007-11-28 20:56:32 -0600294 write_dma_reg(PAS_DMA_RXCHAN_CFG(chno), cfg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600295
Olof Johansson34c20622007-11-28 20:56:32 -0600296 write_dma_reg(PAS_DMA_RXINT_BASEL(mac->dma_if),
297 PAS_DMA_RXINT_BASEL_BRBL(ring->buf_dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600298
Olof Johansson34c20622007-11-28 20:56:32 -0600299 write_dma_reg(PAS_DMA_RXINT_BASEU(mac->dma_if),
300 PAS_DMA_RXINT_BASEU_BRBH(ring->buf_dma >> 32) |
301 PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600302
Olof Johansson5c153322007-11-28 20:56:41 -0600303 cfg = PAS_DMA_RXINT_CFG_DHL(2) | PAS_DMA_RXINT_CFG_L2 |
Olof Johanssonaf289e82007-10-03 13:03:54 -0500304 PAS_DMA_RXINT_CFG_LW | PAS_DMA_RXINT_CFG_RBP |
305 PAS_DMA_RXINT_CFG_HEN;
306
307 if (translation_enabled())
308 cfg |= PAS_DMA_RXINT_CFG_ITRR | PAS_DMA_RXINT_CFG_ITR;
309
Olof Johansson34c20622007-11-28 20:56:32 -0600310 write_dma_reg(PAS_DMA_RXINT_CFG(mac->dma_if), cfg);
Olof Johanssonc0efd522007-08-22 09:12:52 -0500311
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600312 ring->next_to_fill = 0;
313 ring->next_to_clean = 0;
Olof Johansson72b05b92007-11-28 20:54:28 -0600314 ring->mac = mac;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600315 mac->rx = ring;
316
317 return 0;
318
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500319out_ring_desc:
320 kfree(ring->ring_info);
321out_ring_info:
Olof Johansson34c20622007-11-28 20:56:32 -0600322 pasemi_dma_free_chan(&ring->chan);
323out_chan:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600324 return -ENOMEM;
325}
326
Olof Johansson72b05b92007-11-28 20:54:28 -0600327static struct pasemi_mac_txring *
Olof Johansson5c153322007-11-28 20:56:41 -0600328pasemi_mac_setup_tx_resources(const struct net_device *dev)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600329{
330 struct pasemi_mac *mac = netdev_priv(dev);
331 u32 val;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600332 struct pasemi_mac_txring *ring;
Olof Johanssonaf289e82007-10-03 13:03:54 -0500333 unsigned int cfg;
Olof Johansson34c20622007-11-28 20:56:32 -0600334 int chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600335
Olof Johansson34c20622007-11-28 20:56:32 -0600336 ring = pasemi_dma_alloc_chan(TXCHAN, sizeof(struct pasemi_mac_txring),
337 offsetof(struct pasemi_mac_txring, chan));
338
339 if (!ring) {
340 dev_err(&mac->pdev->dev, "Can't allocate TX channel\n");
341 goto out_chan;
342 }
343
344 chno = ring->chan.chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600345
346 spin_lock_init(&ring->lock);
347
Olof Johansson021fa222007-08-22 09:13:11 -0500348 ring->size = TX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500349 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600350 TX_RING_SIZE, GFP_KERNEL);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500351 if (!ring->ring_info)
352 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600353
354 /* Allocate descriptors */
Olof Johansson34c20622007-11-28 20:56:32 -0600355 if (pasemi_dma_alloc_ring(&ring->chan, TX_RING_SIZE))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500356 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600357
Olof Johansson34c20622007-11-28 20:56:32 -0600358 write_dma_reg(PAS_DMA_TXCHAN_BASEL(chno),
359 PAS_DMA_TXCHAN_BASEL_BRBL(ring->chan.ring_dma));
360 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500361 val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 3);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600362
Olof Johansson34c20622007-11-28 20:56:32 -0600363 write_dma_reg(PAS_DMA_TXCHAN_BASEU(chno), val);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600364
Olof Johanssonaf289e82007-10-03 13:03:54 -0500365 cfg = PAS_DMA_TXCHAN_CFG_TY_IFACE |
366 PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
367 PAS_DMA_TXCHAN_CFG_UP |
368 PAS_DMA_TXCHAN_CFG_WT(2);
369
370 if (translation_enabled())
371 cfg |= PAS_DMA_TXCHAN_CFG_TRD | PAS_DMA_TXCHAN_CFG_TRR;
372
Olof Johansson34c20622007-11-28 20:56:32 -0600373 write_dma_reg(PAS_DMA_TXCHAN_CFG(chno), cfg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600374
Olof Johansson021fa222007-08-22 09:13:11 -0500375 ring->next_to_fill = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600376 ring->next_to_clean = 0;
Olof Johansson72b05b92007-11-28 20:54:28 -0600377 ring->mac = mac;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600378
Olof Johansson72b05b92007-11-28 20:54:28 -0600379 return ring;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600380
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500381out_ring_desc:
382 kfree(ring->ring_info);
383out_ring_info:
Olof Johansson34c20622007-11-28 20:56:32 -0600384 pasemi_dma_free_chan(&ring->chan);
385out_chan:
Olof Johansson72b05b92007-11-28 20:54:28 -0600386 return NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600387}
388
Olof Johansson72b05b92007-11-28 20:54:28 -0600389static void pasemi_mac_free_tx_resources(struct pasemi_mac *mac)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600390{
Olof Johansson72b05b92007-11-28 20:54:28 -0600391 struct pasemi_mac_txring *txring = tx_ring(mac);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500392 unsigned int i, j;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600393 struct pasemi_mac_buffer *info;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500394 dma_addr_t dmas[MAX_SKB_FRAGS+1];
395 int freed;
Olof Johanssonad5da102007-10-02 16:27:15 -0500396 int start, limit;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600397
Olof Johansson72b05b92007-11-28 20:54:28 -0600398 start = txring->next_to_clean;
399 limit = txring->next_to_fill;
Olof Johanssonad5da102007-10-02 16:27:15 -0500400
401 /* Compensate for when fill has wrapped and clean has not */
402 if (start > limit)
403 limit += TX_RING_SIZE;
404
405 for (i = start; i < limit; i += freed) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600406 info = &txring->ring_info[(i+1) & (TX_RING_SIZE-1)];
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500407 if (info->dma && info->skb) {
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500408 for (j = 0; j <= skb_shinfo(info->skb)->nr_frags; j++)
Olof Johansson72b05b92007-11-28 20:54:28 -0600409 dmas[j] = txring->ring_info[(i+1+j) &
410 (TX_RING_SIZE-1)].dma;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500411 freed = pasemi_mac_unmap_tx_skb(mac, info->skb, dmas);
412 } else
413 freed = 2;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600414 }
415
Olof Johansson72b05b92007-11-28 20:54:28 -0600416 kfree(txring->ring_info);
Olof Johansson34c20622007-11-28 20:56:32 -0600417 pasemi_dma_free_chan(&txring->chan);
418
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600419}
420
Olof Johansson72b05b92007-11-28 20:54:28 -0600421static void pasemi_mac_free_rx_resources(struct pasemi_mac *mac)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600422{
Olof Johansson72b05b92007-11-28 20:54:28 -0600423 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600424 unsigned int i;
425 struct pasemi_mac_buffer *info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600426
427 for (i = 0; i < RX_RING_SIZE; i++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600428 info = &RX_DESC_INFO(rx, i);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500429 if (info->skb && info->dma) {
430 pci_unmap_single(mac->dma_pdev,
431 info->dma,
432 info->skb->len,
433 PCI_DMA_FROMDEVICE);
434 dev_kfree_skb_any(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600435 }
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500436 info->dma = 0;
437 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600438 }
439
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500440 for (i = 0; i < RX_RING_SIZE; i++)
Olof Johansson72b05b92007-11-28 20:54:28 -0600441 RX_DESC(rx, i) = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500442
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600443 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
Olof Johansson72b05b92007-11-28 20:54:28 -0600444 rx_ring(mac)->buffers, rx_ring(mac)->buf_dma);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600445
Olof Johansson72b05b92007-11-28 20:54:28 -0600446 kfree(rx_ring(mac)->ring_info);
Olof Johansson34c20622007-11-28 20:56:32 -0600447 pasemi_dma_free_chan(&rx_ring(mac)->chan);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600448 mac->rx = NULL;
449}
450
Olof Johansson5c153322007-11-28 20:56:41 -0600451static void pasemi_mac_replenish_rx_ring(const struct net_device *dev,
452 const int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600453{
Olof Johansson5c153322007-11-28 20:56:41 -0600454 const struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -0600455 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500456 int fill, count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600457
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500458 if (limit <= 0)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600459 return;
460
Olof Johansson72b05b92007-11-28 20:54:28 -0600461 fill = rx_ring(mac)->next_to_fill;
Olof Johansson928773c2007-09-26 16:25:06 -0500462 for (count = 0; count < limit; count++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600463 struct pasemi_mac_buffer *info = &RX_DESC_INFO(rx, fill);
464 u64 *buff = &RX_BUFF(rx, fill);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600465 struct sk_buff *skb;
466 dma_addr_t dma;
467
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500468 /* Entry in use? */
469 WARN_ON(*buff);
470
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500471 /* skb might still be in there for recycle on short receives */
472 if (info->skb)
473 skb = info->skb;
Olof Johansson8dc121a2007-10-02 16:26:53 -0500474 else {
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500475 skb = dev_alloc_skb(BUF_SIZE);
Olof Johansson8dc121a2007-10-02 16:26:53 -0500476 skb_reserve(skb, LOCAL_SKB_ALIGN);
477 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600478
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500479 if (unlikely(!skb))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600480 break;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600481
Olof Johansson8dc121a2007-10-02 16:26:53 -0500482 dma = pci_map_single(mac->dma_pdev, skb->data,
483 BUF_SIZE - LOCAL_SKB_ALIGN,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600484 PCI_DMA_FROMDEVICE);
485
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500486 if (unlikely(dma_mapping_error(dma))) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600487 dev_kfree_skb_irq(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600488 break;
489 }
490
491 info->skb = skb;
492 info->dma = dma;
493 *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500494 fill++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600495 }
496
497 wmb();
498
Olof Johansson34c20622007-11-28 20:56:32 -0600499 write_dma_reg(PAS_DMA_RXINT_INCR(mac->dma_if), count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600500
Olof Johansson72b05b92007-11-28 20:54:28 -0600501 rx_ring(mac)->next_to_fill = (rx_ring(mac)->next_to_fill + count) &
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500502 (RX_RING_SIZE - 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600503}
504
Olof Johansson5c153322007-11-28 20:56:41 -0600505static void pasemi_mac_restart_rx_intr(const struct pasemi_mac *mac)
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500506{
Olof Johansson52a94352007-05-12 18:01:09 -0500507 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500508 /* Re-enable packet count interrupts: finally
509 * ack the packet count interrupt we got in rx_intr.
510 */
511
Olof Johansson34c20622007-11-28 20:56:32 -0600512 pcnt = *rx_ring(mac)->chan.status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500513
Olof Johansson52a94352007-05-12 18:01:09 -0500514 reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500515
Olof Johansson34c20622007-11-28 20:56:32 -0600516 write_iob_reg(PAS_IOB_DMA_RXCH_RESET(mac->rx->chan.chno), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500517}
518
Olof Johansson5c153322007-11-28 20:56:41 -0600519static void pasemi_mac_restart_tx_intr(const struct pasemi_mac *mac)
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500520{
Olof Johansson52a94352007-05-12 18:01:09 -0500521 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500522
523 /* Re-enable packet count interrupts */
Olof Johansson34c20622007-11-28 20:56:32 -0600524 pcnt = *tx_ring(mac)->chan.status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500525
Olof Johansson52a94352007-05-12 18:01:09 -0500526 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500527
Olof Johansson34c20622007-11-28 20:56:32 -0600528 write_iob_reg(PAS_IOB_DMA_TXCH_RESET(tx_ring(mac)->chan.chno), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500529}
530
531
Olof Johansson5c153322007-11-28 20:56:41 -0600532static inline void pasemi_mac_rx_error(const struct pasemi_mac *mac,
533 const u64 macrx)
Olof Johansson69c29d82007-10-02 16:24:51 -0500534{
535 unsigned int rcmdsta, ccmdsta;
Olof Johansson34c20622007-11-28 20:56:32 -0600536 struct pasemi_dmachan *chan = &rx_ring(mac)->chan;
Olof Johansson69c29d82007-10-02 16:24:51 -0500537
538 if (!netif_msg_rx_err(mac))
539 return;
540
Olof Johansson34c20622007-11-28 20:56:32 -0600541 rcmdsta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
542 ccmdsta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(chan->chno));
Olof Johansson69c29d82007-10-02 16:24:51 -0500543
544 printk(KERN_ERR "pasemi_mac: rx error. macrx %016lx, rx status %lx\n",
Olof Johansson34c20622007-11-28 20:56:32 -0600545 macrx, *chan->status);
Olof Johansson69c29d82007-10-02 16:24:51 -0500546
547 printk(KERN_ERR "pasemi_mac: rcmdsta %08x ccmdsta %08x\n",
548 rcmdsta, ccmdsta);
549}
550
Olof Johansson5c153322007-11-28 20:56:41 -0600551static inline void pasemi_mac_tx_error(const struct pasemi_mac *mac,
552 const u64 mactx)
Olof Johansson69c29d82007-10-02 16:24:51 -0500553{
554 unsigned int cmdsta;
Olof Johansson34c20622007-11-28 20:56:32 -0600555 struct pasemi_dmachan *chan = &tx_ring(mac)->chan;
Olof Johansson69c29d82007-10-02 16:24:51 -0500556
557 if (!netif_msg_tx_err(mac))
558 return;
559
Olof Johansson34c20622007-11-28 20:56:32 -0600560 cmdsta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(chan->chno));
Olof Johansson69c29d82007-10-02 16:24:51 -0500561
562 printk(KERN_ERR "pasemi_mac: tx error. mactx 0x%016lx, "\
Olof Johansson34c20622007-11-28 20:56:32 -0600563 "tx status 0x%016lx\n", mactx, *chan->status);
Olof Johansson69c29d82007-10-02 16:24:51 -0500564
565 printk(KERN_ERR "pasemi_mac: tcmdsta 0x%08x\n", cmdsta);
566}
567
Olof Johansson5c153322007-11-28 20:56:41 -0600568static int pasemi_mac_clean_rx(struct pasemi_mac_rxring *rx,
569 const int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600570{
Olof Johansson5c153322007-11-28 20:56:41 -0600571 const struct pasemi_dmachan *chan = &rx->chan;
Olof Johansson72b05b92007-11-28 20:54:28 -0600572 struct pasemi_mac *mac = rx->mac;
Olof Johansson5c153322007-11-28 20:56:41 -0600573 struct pci_dev *pdev = mac->dma_pdev;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500574 unsigned int n;
Olof Johansson5c153322007-11-28 20:56:41 -0600575 int count, buf_index, tot_bytes, packets;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500576 struct pasemi_mac_buffer *info;
577 struct sk_buff *skb;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500578 unsigned int len;
Olof Johansson5c153322007-11-28 20:56:41 -0600579 u64 macrx, eval;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500580 dma_addr_t dma;
Olof Johansson5c153322007-11-28 20:56:41 -0600581
582 tot_bytes = 0;
583 packets = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600584
Olof Johansson72b05b92007-11-28 20:54:28 -0600585 spin_lock(&rx->lock);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600586
Olof Johansson72b05b92007-11-28 20:54:28 -0600587 n = rx->next_to_clean;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600588
Olof Johansson72b05b92007-11-28 20:54:28 -0600589 prefetch(&RX_DESC(rx, n));
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500590
591 for (count = 0; count < limit; count++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600592 macrx = RX_DESC(rx, n);
Olof Johansson5c153322007-11-28 20:56:41 -0600593 prefetch(&RX_DESC(rx, n+4));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600594
Olof Johansson69c29d82007-10-02 16:24:51 -0500595 if ((macrx & XCT_MACRX_E) ||
Olof Johansson34c20622007-11-28 20:56:32 -0600596 (*chan->status & PAS_STATUS_ERROR))
Olof Johansson69c29d82007-10-02 16:24:51 -0500597 pasemi_mac_rx_error(mac, macrx);
598
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500599 if (!(macrx & XCT_MACRX_O))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600600 break;
601
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600602 info = NULL;
603
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500604 BUG_ON(!(macrx & XCT_MACRX_RR_8BRES));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600605
Olof Johansson72b05b92007-11-28 20:54:28 -0600606 eval = (RX_DESC(rx, n+1) & XCT_RXRES_8B_EVAL_M) >>
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500607 XCT_RXRES_8B_EVAL_S;
608 buf_index = eval-1;
609
Olof Johansson72b05b92007-11-28 20:54:28 -0600610 dma = (RX_DESC(rx, n+2) & XCT_PTR_ADDR_M);
611 info = &RX_DESC_INFO(rx, buf_index);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500612
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500613 skb = info->skb;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600614
Olof Johansson5c153322007-11-28 20:56:41 -0600615 prefetch_skb(skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600616
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500617 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600618
Olof Johansson5c153322007-11-28 20:56:41 -0600619 pci_unmap_single(pdev, dma, BUF_SIZE-LOCAL_SKB_ALIGN,
620 PCI_DMA_FROMDEVICE);
Olof Johansson32bee772007-11-06 22:21:38 -0600621
622 if (macrx & XCT_MACRX_CRC) {
623 /* CRC error flagged */
624 mac->netdev->stats.rx_errors++;
625 mac->netdev->stats.rx_crc_errors++;
Olof Johansson4352d822007-12-03 21:34:14 -0600626 /* No need to free skb, it'll be reused */
Olof Johansson32bee772007-11-06 22:21:38 -0600627 goto next;
628 }
629
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500630 if (len < 256) {
Olof Johansson8dc121a2007-10-02 16:26:53 -0500631 struct sk_buff *new_skb;
632
633 new_skb = netdev_alloc_skb(mac->netdev,
634 len + LOCAL_SKB_ALIGN);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500635 if (new_skb) {
Olof Johansson8dc121a2007-10-02 16:26:53 -0500636 skb_reserve(new_skb, LOCAL_SKB_ALIGN);
Olof Johansson73344862007-08-22 09:12:55 -0500637 memcpy(new_skb->data, skb->data, len);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500638 /* save the skb in buffer_info as good */
639 skb = new_skb;
640 }
641 /* else just continue with the old one */
642 } else
643 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600644
Olof Johanssonad5da102007-10-02 16:27:15 -0500645 info->dma = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500646
Olof Johansson26fcfa92007-08-22 09:12:59 -0500647 if (likely((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK)) {
Olof Johansson38bf3182007-08-22 09:13:24 -0500648 skb->ip_summed = CHECKSUM_UNNECESSARY;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500649 skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600650 XCT_MACRX_CSUM_S;
651 } else
652 skb->ip_summed = CHECKSUM_NONE;
653
Olof Johansson5c153322007-11-28 20:56:41 -0600654 packets++;
655 tot_bytes += len;
656
657 /* Don't include CRC */
658 skb_put(skb, len-4);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600659
Olof Johansson26fcfa92007-08-22 09:12:59 -0500660 skb->protocol = eth_type_trans(skb, mac->netdev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600661 netif_receive_skb(skb);
662
Olof Johansson32bee772007-11-06 22:21:38 -0600663next:
Olof Johansson72b05b92007-11-28 20:54:28 -0600664 RX_DESC(rx, n) = 0;
665 RX_DESC(rx, n+1) = 0;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500666
Olof Johanssonad5da102007-10-02 16:27:15 -0500667 /* Need to zero it out since hardware doesn't, since the
668 * replenish loop uses it to tell when it's done.
669 */
Olof Johansson72b05b92007-11-28 20:54:28 -0600670 RX_BUFF(rx, buf_index) = 0;
Olof Johanssonad5da102007-10-02 16:27:15 -0500671
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500672 n += 4;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600673 }
674
Olof Johansson9a50beb2007-10-02 16:26:30 -0500675 if (n > RX_RING_SIZE) {
676 /* Errata 5971 workaround: L2 target of headers */
Olof Johansson34c20622007-11-28 20:56:32 -0600677 write_iob_reg(PAS_IOB_COM_PKTHDRCNT, 0);
Olof Johansson9a50beb2007-10-02 16:26:30 -0500678 n &= (RX_RING_SIZE-1);
679 }
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500680
Olof Johansson72b05b92007-11-28 20:54:28 -0600681 rx_ring(mac)->next_to_clean = n;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500682
683 /* Increase is in number of 16-byte entries, and since each descriptor
684 * with an 8BRES takes up 3x8 bytes (padded to 4x8), increase with
685 * count*2.
686 */
Olof Johansson34c20622007-11-28 20:56:32 -0600687 write_dma_reg(PAS_DMA_RXCHAN_INCR(mac->rx->chan.chno), count << 1);
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500688
689 pasemi_mac_replenish_rx_ring(mac->netdev, count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600690
Olof Johansson5c153322007-11-28 20:56:41 -0600691 mac->netdev->stats.rx_bytes += tot_bytes;
692 mac->netdev->stats.rx_packets += packets;
693
Olof Johansson72b05b92007-11-28 20:54:28 -0600694 spin_unlock(&rx_ring(mac)->lock);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600695
696 return count;
697}
698
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500699/* Can't make this too large or we blow the kernel stack limits */
700#define TX_CLEAN_BATCHSIZE (128/MAX_SKB_FRAGS)
701
Olof Johansson72b05b92007-11-28 20:54:28 -0600702static int pasemi_mac_clean_tx(struct pasemi_mac_txring *txring)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600703{
Olof Johansson34c20622007-11-28 20:56:32 -0600704 struct pasemi_dmachan *chan = &txring->chan;
Olof Johansson72b05b92007-11-28 20:54:28 -0600705 struct pasemi_mac *mac = txring->mac;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500706 int i, j;
Olof Johanssonad5da102007-10-02 16:27:15 -0500707 unsigned int start, descr_count, buf_count, batch_limit;
708 unsigned int ring_limit;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500709 unsigned int total_count;
Olof Johanssonca7e2352007-09-26 16:23:59 -0500710 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500711 struct sk_buff *skbs[TX_CLEAN_BATCHSIZE];
712 dma_addr_t dmas[TX_CLEAN_BATCHSIZE][MAX_SKB_FRAGS+1];
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600713
Olof Johansson02df6cf2007-08-22 09:13:03 -0500714 total_count = 0;
Olof Johanssonad5da102007-10-02 16:27:15 -0500715 batch_limit = TX_CLEAN_BATCHSIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500716restart:
Olof Johansson72b05b92007-11-28 20:54:28 -0600717 spin_lock_irqsave(&txring->lock, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600718
Olof Johansson72b05b92007-11-28 20:54:28 -0600719 start = txring->next_to_clean;
720 ring_limit = txring->next_to_fill;
Olof Johanssonad5da102007-10-02 16:27:15 -0500721
722 /* Compensate for when fill has wrapped but clean has not */
723 if (start > ring_limit)
724 ring_limit += TX_RING_SIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500725
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500726 buf_count = 0;
727 descr_count = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600728
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500729 for (i = start;
Olof Johanssonad5da102007-10-02 16:27:15 -0500730 descr_count < batch_limit && i < ring_limit;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500731 i += buf_count) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600732 u64 mactx = TX_DESC(txring, i);
Olof Johanssonad5da102007-10-02 16:27:15 -0500733 struct sk_buff *skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500734
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500735 if ((mactx & XCT_MACTX_E) ||
Olof Johansson34c20622007-11-28 20:56:32 -0600736 (*chan->status & PAS_STATUS_ERROR))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500737 pasemi_mac_tx_error(mac, mactx);
Olof Johansson69c29d82007-10-02 16:24:51 -0500738
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500739 if (unlikely(mactx & XCT_MACTX_O))
Olof Johansson02df6cf2007-08-22 09:13:03 -0500740 /* Not yet transmitted */
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600741 break;
742
Olof Johansson72b05b92007-11-28 20:54:28 -0600743 skb = TX_DESC_INFO(txring, i+1).skb;
Olof Johanssonad5da102007-10-02 16:27:15 -0500744 skbs[descr_count] = skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500745
Olof Johanssonad5da102007-10-02 16:27:15 -0500746 buf_count = 2 + skb_shinfo(skb)->nr_frags;
747 for (j = 0; j <= skb_shinfo(skb)->nr_frags; j++)
Olof Johansson72b05b92007-11-28 20:54:28 -0600748 dmas[descr_count][j] = TX_DESC_INFO(txring, i+1+j).dma;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500749
Olof Johansson72b05b92007-11-28 20:54:28 -0600750 TX_DESC(txring, i) = 0;
751 TX_DESC(txring, i+1) = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500752
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500753 /* Since we always fill with an even number of entries, make
754 * sure we skip any unused one at the end as well.
755 */
756 if (buf_count & 1)
757 buf_count++;
758 descr_count++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600759 }
Olof Johansson72b05b92007-11-28 20:54:28 -0600760 txring->next_to_clean = i & (TX_RING_SIZE-1);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500761
Olof Johansson72b05b92007-11-28 20:54:28 -0600762 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500763 netif_wake_queue(mac->netdev);
764
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500765 for (i = 0; i < descr_count; i++)
766 pasemi_mac_unmap_tx_skb(mac, skbs[i], dmas[i]);
Olof Johansson02df6cf2007-08-22 09:13:03 -0500767
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500768 total_count += descr_count;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500769
770 /* If the batch was full, try to clean more */
Olof Johanssonad5da102007-10-02 16:27:15 -0500771 if (descr_count == batch_limit)
Olof Johansson02df6cf2007-08-22 09:13:03 -0500772 goto restart;
773
774 return total_count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600775}
776
777
778static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
779{
Olof Johansson5c153322007-11-28 20:56:41 -0600780 const struct pasemi_mac_rxring *rxring = data;
Olof Johansson34c20622007-11-28 20:56:32 -0600781 struct pasemi_mac *mac = rxring->mac;
782 struct net_device *dev = mac->netdev;
Olof Johansson5c153322007-11-28 20:56:41 -0600783 const struct pasemi_dmachan *chan = &rxring->chan;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600784 unsigned int reg;
785
Olof Johansson34c20622007-11-28 20:56:32 -0600786 if (!(*chan->status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600787 return IRQ_NONE;
788
Olof Johansson6dfa7522007-05-08 00:47:32 -0500789 /* Don't reset packet count so it won't fire again but clear
790 * all others.
791 */
792
Olof Johansson6dfa7522007-05-08 00:47:32 -0500793 reg = 0;
Olof Johansson34c20622007-11-28 20:56:32 -0600794 if (*chan->status & PAS_STATUS_SOFT)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500795 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
Olof Johansson34c20622007-11-28 20:56:32 -0600796 if (*chan->status & PAS_STATUS_ERROR)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500797 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
Olof Johansson34c20622007-11-28 20:56:32 -0600798 if (*chan->status & PAS_STATUS_TIMER)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600799 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
800
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700801 netif_rx_schedule(dev, &mac->napi);
Olof Johansson6dfa7522007-05-08 00:47:32 -0500802
Olof Johansson34c20622007-11-28 20:56:32 -0600803 write_iob_reg(PAS_IOB_DMA_RXCH_RESET(chan->chno), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600804
805 return IRQ_HANDLED;
806}
807
Olof Johansson61cec3b2007-11-28 20:56:54 -0600808#define TX_CLEAN_INTERVAL HZ
809
810static void pasemi_mac_tx_timer(unsigned long data)
811{
812 struct pasemi_mac_txring *txring = (struct pasemi_mac_txring *)data;
813 struct pasemi_mac *mac = txring->mac;
814
815 pasemi_mac_clean_tx(txring);
816
817 mod_timer(&txring->clean_timer, jiffies + TX_CLEAN_INTERVAL);
818
819 pasemi_mac_restart_tx_intr(mac);
820}
821
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600822static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
823{
Olof Johansson72b05b92007-11-28 20:54:28 -0600824 struct pasemi_mac_txring *txring = data;
Olof Johansson5c153322007-11-28 20:56:41 -0600825 const struct pasemi_dmachan *chan = &txring->chan;
Olof Johansson61cec3b2007-11-28 20:56:54 -0600826 struct pasemi_mac *mac = txring->mac;
827 unsigned int reg;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600828
Olof Johansson34c20622007-11-28 20:56:32 -0600829 if (!(*chan->status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600830 return IRQ_NONE;
831
Olof Johansson61cec3b2007-11-28 20:56:54 -0600832 reg = 0;
Olof Johansson6dfa7522007-05-08 00:47:32 -0500833
Olof Johansson34c20622007-11-28 20:56:32 -0600834 if (*chan->status & PAS_STATUS_SOFT)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500835 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
Olof Johansson34c20622007-11-28 20:56:32 -0600836 if (*chan->status & PAS_STATUS_ERROR)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500837 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600838
Olof Johansson61cec3b2007-11-28 20:56:54 -0600839 mod_timer(&txring->clean_timer, jiffies + (TX_CLEAN_INTERVAL)*2);
840
841 netif_rx_schedule(mac->netdev, &mac->napi);
842
843 if (reg)
844 write_iob_reg(PAS_IOB_DMA_TXCH_RESET(chan->chno), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600845
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600846 return IRQ_HANDLED;
847}
848
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500849static void pasemi_adjust_link(struct net_device *dev)
850{
851 struct pasemi_mac *mac = netdev_priv(dev);
852 int msg;
853 unsigned int flags;
854 unsigned int new_flags;
855
856 if (!mac->phydev->link) {
857 /* If no link, MAC speed settings don't matter. Just report
858 * link down and return.
859 */
860 if (mac->link && netif_msg_link(mac))
861 printk(KERN_INFO "%s: Link is down.\n", dev->name);
862
863 netif_carrier_off(dev);
864 mac->link = 0;
865
866 return;
867 } else
868 netif_carrier_on(dev);
869
Olof Johanssona85b9422007-09-15 13:40:59 -0700870 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500871 new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
872 PAS_MAC_CFG_PCFG_TSR_M);
873
874 if (!mac->phydev->duplex)
875 new_flags |= PAS_MAC_CFG_PCFG_HD;
876
877 switch (mac->phydev->speed) {
878 case 1000:
879 new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
880 PAS_MAC_CFG_PCFG_TSR_1G;
881 break;
882 case 100:
883 new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
884 PAS_MAC_CFG_PCFG_TSR_100M;
885 break;
886 case 10:
887 new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
888 PAS_MAC_CFG_PCFG_TSR_10M;
889 break;
890 default:
891 printk("Unsupported speed %d\n", mac->phydev->speed);
892 }
893
894 /* Print on link or speed/duplex change */
895 msg = mac->link != mac->phydev->link || flags != new_flags;
896
897 mac->duplex = mac->phydev->duplex;
898 mac->speed = mac->phydev->speed;
899 mac->link = mac->phydev->link;
900
901 if (new_flags != flags)
Olof Johanssona85b9422007-09-15 13:40:59 -0700902 write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500903
904 if (msg && netif_msg_link(mac))
905 printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
906 dev->name, mac->speed, mac->duplex ? "full" : "half");
907}
908
909static int pasemi_mac_phy_init(struct net_device *dev)
910{
911 struct pasemi_mac *mac = netdev_priv(dev);
912 struct device_node *dn, *phy_dn;
913 struct phy_device *phydev;
914 unsigned int phy_id;
915 const phandle *ph;
916 const unsigned int *prop;
917 struct resource r;
918 int ret;
919
920 dn = pci_device_to_OF_node(mac->pdev);
Linus Torvalds90287802007-05-08 11:57:17 -0700921 ph = of_get_property(dn, "phy-handle", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500922 if (!ph)
923 return -ENODEV;
924 phy_dn = of_find_node_by_phandle(*ph);
925
Linus Torvalds90287802007-05-08 11:57:17 -0700926 prop = of_get_property(phy_dn, "reg", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500927 ret = of_address_to_resource(phy_dn->parent, 0, &r);
928 if (ret)
929 goto err;
930
931 phy_id = *prop;
932 snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);
933
934 of_node_put(phy_dn);
935
936 mac->link = 0;
937 mac->speed = 0;
938 mac->duplex = -1;
939
940 phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);
941
942 if (IS_ERR(phydev)) {
943 printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
944 return PTR_ERR(phydev);
945 }
946
947 mac->phydev = phydev;
948
949 return 0;
950
951err:
952 of_node_put(phy_dn);
953 return -ENODEV;
954}
955
956
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600957static int pasemi_mac_open(struct net_device *dev)
958{
959 struct pasemi_mac *mac = netdev_priv(dev);
960 unsigned int flags;
961 int ret;
962
963 /* enable rx section */
Olof Johansson34c20622007-11-28 20:56:32 -0600964 write_dma_reg(PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600965
966 /* enable tx section */
Olof Johansson34c20622007-11-28 20:56:32 -0600967 write_dma_reg(PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600968
969 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
970 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
971 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
972
Olof Johanssona85b9422007-09-15 13:40:59 -0700973 write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600974
Olof Johansson6dfa7522007-05-08 00:47:32 -0500975 /* 0xffffff is max value, about 16ms */
Olof Johansson34c20622007-11-28 20:56:32 -0600976 write_iob_reg(PAS_IOB_DMA_COM_TIMEOUTCFG,
977 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0xffffff));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600978
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600979 ret = pasemi_mac_setup_rx_resources(dev);
980 if (ret)
981 goto out_rx_resources;
982
Olof Johansson34c20622007-11-28 20:56:32 -0600983 mac->tx = pasemi_mac_setup_tx_resources(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -0600984
985 if (!mac->tx)
986 goto out_tx_ring;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600987
Olof Johansson34c20622007-11-28 20:56:32 -0600988 write_iob_reg(PAS_IOB_DMA_RXCH_CFG(mac->rx->chan.chno),
989 PAS_IOB_DMA_RXCH_CFG_CNTTH(0));
990
991 write_iob_reg(PAS_IOB_DMA_TXCH_CFG(mac->tx->chan.chno),
Olof Johansson61cec3b2007-11-28 20:56:54 -0600992 PAS_IOB_DMA_TXCH_CFG_CNTTH(32));
Olof Johansson34c20622007-11-28 20:56:32 -0600993
Olof Johanssona85b9422007-09-15 13:40:59 -0700994 write_mac_reg(mac, PAS_MAC_IPC_CHNL,
Olof Johansson34c20622007-11-28 20:56:32 -0600995 PAS_MAC_IPC_CHNL_DCHNO(mac->rx->chan.chno) |
996 PAS_MAC_IPC_CHNL_BCH(mac->rx->chan.chno));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600997
998 /* enable rx if */
Olof Johansson34c20622007-11-28 20:56:32 -0600999 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
1000 PAS_DMA_RXINT_RCMDSTA_EN |
1001 PAS_DMA_RXINT_RCMDSTA_DROPS_M |
1002 PAS_DMA_RXINT_RCMDSTA_BP |
1003 PAS_DMA_RXINT_RCMDSTA_OO |
1004 PAS_DMA_RXINT_RCMDSTA_BT);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001005
1006 /* enable rx channel */
Olof Johansson34c20622007-11-28 20:56:32 -06001007 pasemi_dma_start_chan(&rx_ring(mac)->chan, PAS_DMA_RXCHAN_CCMDSTA_DU |
1008 PAS_DMA_RXCHAN_CCMDSTA_OD |
1009 PAS_DMA_RXCHAN_CCMDSTA_FD |
1010 PAS_DMA_RXCHAN_CCMDSTA_DT);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001011
1012 /* enable tx channel */
Olof Johansson34c20622007-11-28 20:56:32 -06001013 pasemi_dma_start_chan(&tx_ring(mac)->chan, PAS_DMA_TXCHAN_TCMDSTA_SZ |
1014 PAS_DMA_TXCHAN_TCMDSTA_DB |
1015 PAS_DMA_TXCHAN_TCMDSTA_DE |
1016 PAS_DMA_TXCHAN_TCMDSTA_DA);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001017
Olof Johansson928773c2007-09-26 16:25:06 -05001018 pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001019
Olof Johansson34c20622007-11-28 20:56:32 -06001020 write_dma_reg(PAS_DMA_RXCHAN_INCR(rx_ring(mac)->chan.chno),
1021 RX_RING_SIZE>>1);
Olof Johanssonb5254ee2007-10-02 16:27:57 -05001022
Olof Johansson72b05b92007-11-28 20:54:28 -06001023 /* Clear out any residual packet count state from firmware */
1024 pasemi_mac_restart_rx_intr(mac);
1025 pasemi_mac_restart_tx_intr(mac);
1026
Olof Johansson36033762007-09-26 16:24:42 -05001027 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
1028 PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
1029
1030 if (mac->type == MAC_TYPE_GMAC)
1031 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
1032 else
1033 flags |= PAS_MAC_CFG_PCFG_TSR_10G | PAS_MAC_CFG_PCFG_SPD_10G;
1034
1035 /* Enable interface in MAC */
1036 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
1037
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001038 ret = pasemi_mac_phy_init(dev);
1039 /* Some configs don't have PHYs (XAUI etc), so don't complain about
1040 * failed init due to -ENODEV.
1041 */
1042 if (ret && ret != -ENODEV)
1043 dev_warn(&mac->pdev->dev, "phy init failed: %d\n", ret);
1044
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001045 netif_start_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001046 napi_enable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001047
Olof Johansson72b05b92007-11-28 20:54:28 -06001048 snprintf(mac->tx_irq_name, sizeof(mac->tx_irq_name), "%s tx",
1049 dev->name);
Olof Johansson771f7402007-05-08 00:47:21 -05001050
Olof Johansson34c20622007-11-28 20:56:32 -06001051 ret = request_irq(mac->tx->chan.irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
Olof Johansson72b05b92007-11-28 20:54:28 -06001052 mac->tx_irq_name, mac->tx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001053 if (ret) {
1054 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson34c20622007-11-28 20:56:32 -06001055 mac->tx->chan.irq, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001056 goto out_tx_int;
1057 }
1058
Olof Johansson72b05b92007-11-28 20:54:28 -06001059 snprintf(mac->rx_irq_name, sizeof(mac->rx_irq_name), "%s rx",
1060 dev->name);
1061
Olof Johansson34c20622007-11-28 20:56:32 -06001062 ret = request_irq(mac->rx->chan.irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
1063 mac->rx_irq_name, mac->rx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001064 if (ret) {
1065 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson34c20622007-11-28 20:56:32 -06001066 mac->rx->chan.irq, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001067 goto out_rx_int;
1068 }
1069
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001070 if (mac->phydev)
1071 phy_start(mac->phydev);
1072
Olof Johansson61cec3b2007-11-28 20:56:54 -06001073 init_timer(&mac->tx->clean_timer);
1074 mac->tx->clean_timer.function = pasemi_mac_tx_timer;
1075 mac->tx->clean_timer.data = (unsigned long)mac->tx;
1076 mac->tx->clean_timer.expires = jiffies+HZ;
1077 add_timer(&mac->tx->clean_timer);
1078
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001079 return 0;
1080
1081out_rx_int:
Olof Johansson34c20622007-11-28 20:56:32 -06001082 free_irq(mac->tx->chan.irq, mac->tx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001083out_tx_int:
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001084 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001085 netif_stop_queue(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -06001086out_tx_ring:
1087 if (mac->tx)
1088 pasemi_mac_free_tx_resources(mac);
1089 pasemi_mac_free_rx_resources(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001090out_rx_resources:
1091
1092 return ret;
1093}
1094
1095#define MAX_RETRIES 5000
1096
1097static int pasemi_mac_close(struct net_device *dev)
1098{
1099 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson9e81d332007-10-02 16:27:39 -05001100 unsigned int sta;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001101 int retries;
Olof Johansson34c20622007-11-28 20:56:32 -06001102 int rxch, txch;
1103
1104 rxch = rx_ring(mac)->chan.chno;
1105 txch = tx_ring(mac)->chan.chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001106
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001107 if (mac->phydev) {
1108 phy_stop(mac->phydev);
1109 phy_disconnect(mac->phydev);
1110 }
1111
Olof Johansson61cec3b2007-11-28 20:56:54 -06001112 del_timer_sync(&mac->tx->clean_timer);
1113
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001114 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001115 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001116
Olof Johansson34c20622007-11-28 20:56:32 -06001117 sta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
Olof Johansson9e81d332007-10-02 16:27:39 -05001118 if (sta & (PAS_DMA_RXINT_RCMDSTA_BP |
1119 PAS_DMA_RXINT_RCMDSTA_OO |
1120 PAS_DMA_RXINT_RCMDSTA_BT))
1121 printk(KERN_DEBUG "pasemi_mac: rcmdsta error: 0x%08x\n", sta);
1122
Olof Johansson34c20622007-11-28 20:56:32 -06001123 sta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch));
Olof Johansson9e81d332007-10-02 16:27:39 -05001124 if (sta & (PAS_DMA_RXCHAN_CCMDSTA_DU |
1125 PAS_DMA_RXCHAN_CCMDSTA_OD |
1126 PAS_DMA_RXCHAN_CCMDSTA_FD |
1127 PAS_DMA_RXCHAN_CCMDSTA_DT))
1128 printk(KERN_DEBUG "pasemi_mac: ccmdsta error: 0x%08x\n", sta);
1129
Olof Johansson34c20622007-11-28 20:56:32 -06001130 sta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch));
Olof Johansson72b05b92007-11-28 20:54:28 -06001131 if (sta & (PAS_DMA_TXCHAN_TCMDSTA_SZ | PAS_DMA_TXCHAN_TCMDSTA_DB |
1132 PAS_DMA_TXCHAN_TCMDSTA_DE | PAS_DMA_TXCHAN_TCMDSTA_DA))
Olof Johansson9e81d332007-10-02 16:27:39 -05001133 printk(KERN_DEBUG "pasemi_mac: tcmdsta error: 0x%08x\n", sta);
1134
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001135 /* Clean out any pending buffers */
Olof Johansson72b05b92007-11-28 20:54:28 -06001136 pasemi_mac_clean_tx(tx_ring(mac));
1137 pasemi_mac_clean_rx(rx_ring(mac), RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001138
1139 /* Disable interface */
Olof Johansson34c20622007-11-28 20:56:32 -06001140 write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch),
Olof Johansson72b05b92007-11-28 20:54:28 -06001141 PAS_DMA_TXCHAN_TCMDSTA_ST);
Olof Johansson34c20622007-11-28 20:56:32 -06001142 write_dma_reg( PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
Olof Johansson72b05b92007-11-28 20:54:28 -06001143 PAS_DMA_RXINT_RCMDSTA_ST);
Olof Johansson34c20622007-11-28 20:56:32 -06001144 write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch),
Olof Johansson72b05b92007-11-28 20:54:28 -06001145 PAS_DMA_RXCHAN_CCMDSTA_ST);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001146
1147 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johansson34c20622007-11-28 20:56:32 -06001148 sta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(rxch));
Olof Johansson9e81d332007-10-02 16:27:39 -05001149 if (!(sta & PAS_DMA_TXCHAN_TCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001150 break;
1151 cond_resched();
1152 }
1153
Olof Johansson9e81d332007-10-02 16:27:39 -05001154 if (sta & PAS_DMA_TXCHAN_TCMDSTA_ACT)
Olof Johansson34c20622007-11-28 20:56:32 -06001155 dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001156
1157 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johansson34c20622007-11-28 20:56:32 -06001158 sta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch));
Olof Johansson9e81d332007-10-02 16:27:39 -05001159 if (!(sta & PAS_DMA_RXCHAN_CCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001160 break;
1161 cond_resched();
1162 }
1163
Olof Johansson9e81d332007-10-02 16:27:39 -05001164 if (sta & PAS_DMA_RXCHAN_CCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001165 dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001166
1167 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johansson34c20622007-11-28 20:56:32 -06001168 sta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
Olof Johansson9e81d332007-10-02 16:27:39 -05001169 if (!(sta & PAS_DMA_RXINT_RCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001170 break;
1171 cond_resched();
1172 }
1173
Olof Johansson9e81d332007-10-02 16:27:39 -05001174 if (sta & PAS_DMA_RXINT_RCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001175 dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001176
1177 /* Then, disable the channel. This must be done separately from
1178 * stopping, since you can't disable when active.
1179 */
1180
Olof Johansson34c20622007-11-28 20:56:32 -06001181 write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch), 0);
1182 write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch), 0);
1183 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001184
Olof Johansson34c20622007-11-28 20:56:32 -06001185 free_irq(mac->tx->chan.irq, mac->tx);
1186 free_irq(mac->rx->chan.irq, mac->rx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001187
1188 /* Free resources */
Olof Johansson72b05b92007-11-28 20:54:28 -06001189 pasemi_mac_free_rx_resources(mac);
1190 pasemi_mac_free_tx_resources(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001191
1192 return 0;
1193}
1194
1195static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
1196{
1197 struct pasemi_mac *mac = netdev_priv(dev);
1198 struct pasemi_mac_txring *txring;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001199 u64 dflags, mactx;
1200 dma_addr_t map[MAX_SKB_FRAGS+1];
1201 unsigned int map_size[MAX_SKB_FRAGS+1];
Olof Johanssonca7e2352007-09-26 16:23:59 -05001202 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001203 int i, nfrags;
Olof Johansson5c153322007-11-28 20:56:41 -06001204 int fill;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001205
Olof Johanssondbd62af2007-11-06 22:20:39 -06001206 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_CRC_PAD;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001207
1208 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001209 const unsigned char *nh = skb_network_header(skb);
1210
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001211 switch (ip_hdr(skb)->protocol) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001212 case IPPROTO_TCP:
1213 dflags |= XCT_MACTX_CSUM_TCP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001214 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001215 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001216 break;
1217 case IPPROTO_UDP:
1218 dflags |= XCT_MACTX_CSUM_UDP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001219 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001220 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001221 break;
1222 }
1223 }
1224
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001225 nfrags = skb_shinfo(skb)->nr_frags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001226
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001227 map[0] = pci_map_single(mac->dma_pdev, skb->data, skb_headlen(skb),
1228 PCI_DMA_TODEVICE);
1229 map_size[0] = skb_headlen(skb);
1230 if (dma_mapping_error(map[0]))
1231 goto out_err_nolock;
1232
1233 for (i = 0; i < nfrags; i++) {
1234 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1235
1236 map[i+1] = pci_map_page(mac->dma_pdev, frag->page,
1237 frag->page_offset, frag->size,
1238 PCI_DMA_TODEVICE);
1239 map_size[i+1] = frag->size;
1240 if (dma_mapping_error(map[i+1])) {
1241 nfrags = i;
1242 goto out_err_nolock;
1243 }
1244 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001245
Olof Johansson26fcfa92007-08-22 09:12:59 -05001246 mactx = dflags | XCT_MACTX_LLEN(skb->len);
Olof Johansson26fcfa92007-08-22 09:12:59 -05001247
Olof Johansson72b05b92007-11-28 20:54:28 -06001248 txring = tx_ring(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001249
1250 spin_lock_irqsave(&txring->lock, flags);
1251
Olof Johansson5c153322007-11-28 20:56:41 -06001252 fill = txring->next_to_fill;
1253
Olof Johanssonad5da102007-10-02 16:27:15 -05001254 /* Avoid stepping on the same cache line that the DMA controller
1255 * is currently about to send, so leave at least 8 words available.
1256 * Total free space needed is mactx + fragments + 8
1257 */
1258 if (RING_AVAIL(txring) < nfrags + 10) {
1259 /* no room -- stop the queue and wait for tx intr */
1260 netif_stop_queue(dev);
1261 goto out_err;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001262 }
1263
Olof Johansson5c153322007-11-28 20:56:41 -06001264 TX_DESC(txring, fill) = mactx;
1265 fill++;
1266 TX_DESC_INFO(txring, fill).skb = skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001267 for (i = 0; i <= nfrags; i++) {
Olof Johansson5c153322007-11-28 20:56:41 -06001268 TX_DESC(txring, fill+i) =
Olof Johansson72b05b92007-11-28 20:54:28 -06001269 XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]);
Olof Johansson5c153322007-11-28 20:56:41 -06001270 TX_DESC_INFO(txring, fill+i).dma = map[i];
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001271 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001272
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001273 /* We have to add an even number of 8-byte entries to the ring
1274 * even if the last one is unused. That means always an odd number
1275 * of pointers + one mactx descriptor.
1276 */
1277 if (nfrags & 1)
1278 nfrags++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001279
Olof Johansson5c153322007-11-28 20:56:41 -06001280 txring->next_to_fill = (fill + nfrags + 1) & (TX_RING_SIZE-1);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -05001281
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001282 dev->stats.tx_packets++;
1283 dev->stats.tx_bytes += skb->len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001284
1285 spin_unlock_irqrestore(&txring->lock, flags);
1286
Olof Johansson34c20622007-11-28 20:56:32 -06001287 write_dma_reg(PAS_DMA_TXCHAN_INCR(txring->chan.chno), (nfrags+2) >> 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001288
1289 return NETDEV_TX_OK;
1290
1291out_err:
1292 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001293out_err_nolock:
1294 while (nfrags--)
1295 pci_unmap_single(mac->dma_pdev, map[nfrags], map_size[nfrags],
1296 PCI_DMA_TODEVICE);
1297
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001298 return NETDEV_TX_BUSY;
1299}
1300
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001301static void pasemi_mac_set_rx_mode(struct net_device *dev)
1302{
Olof Johansson5c153322007-11-28 20:56:41 -06001303 const struct pasemi_mac *mac = netdev_priv(dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001304 unsigned int flags;
1305
Olof Johanssona85b9422007-09-15 13:40:59 -07001306 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001307
1308 /* Set promiscuous */
1309 if (dev->flags & IFF_PROMISC)
1310 flags |= PAS_MAC_CFG_PCFG_PR;
1311 else
1312 flags &= ~PAS_MAC_CFG_PCFG_PR;
1313
Olof Johanssona85b9422007-09-15 13:40:59 -07001314 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001315}
1316
1317
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001318static int pasemi_mac_poll(struct napi_struct *napi, int budget)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001319{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001320 struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
1321 struct net_device *dev = mac->netdev;
1322 int pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001323
Olof Johansson72b05b92007-11-28 20:54:28 -06001324 pasemi_mac_clean_tx(tx_ring(mac));
1325 pkts = pasemi_mac_clean_rx(rx_ring(mac), budget);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001326 if (pkts < budget) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001327 /* all done, no more packets present */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001328 netif_rx_complete(dev, napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001329
Olof Johansson1b0335ea2007-05-08 00:47:26 -05001330 pasemi_mac_restart_rx_intr(mac);
Olof Johansson61cec3b2007-11-28 20:56:54 -06001331 pasemi_mac_restart_tx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001332 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001333 return pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001334}
1335
1336static int __devinit
1337pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1338{
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001339 struct net_device *dev;
1340 struct pasemi_mac *mac;
1341 int err;
Joe Perches0795af52007-10-03 17:59:30 -07001342 DECLARE_MAC_BUF(mac_buf);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001343
1344 err = pci_enable_device(pdev);
1345 if (err)
1346 return err;
1347
1348 dev = alloc_etherdev(sizeof(struct pasemi_mac));
1349 if (dev == NULL) {
1350 dev_err(&pdev->dev,
1351 "pasemi_mac: Could not allocate ethernet device.\n");
1352 err = -ENOMEM;
1353 goto out_disable_device;
1354 }
1355
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001356 pci_set_drvdata(pdev, dev);
1357 SET_NETDEV_DEV(dev, &pdev->dev);
1358
1359 mac = netdev_priv(dev);
1360
1361 mac->pdev = pdev;
1362 mac->netdev = dev;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001363
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001364 netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
1365
Olof Johansson5c153322007-11-28 20:56:41 -06001366 dev->features = NETIF_F_IP_CSUM | NETIF_F_LLTX | NETIF_F_SG |
1367 NETIF_F_HIGHDMA;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001368
Olof Johansson34c20622007-11-28 20:56:32 -06001369 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
1370 if (!mac->dma_pdev) {
1371 dev_err(&mac->pdev->dev, "Can't find DMA Controller\n");
1372 err = -ENODEV;
1373 goto out;
1374 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001375
Olof Johansson34c20622007-11-28 20:56:32 -06001376 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
1377 if (!mac->iob_pdev) {
1378 dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n");
1379 err = -ENODEV;
1380 goto out;
1381 }
1382
1383 /* get mac addr from device tree */
1384 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
1385 err = -ENODEV;
1386 goto out;
1387 }
1388 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
1389
1390 mac->dma_if = mac_to_intf(mac);
1391 if (mac->dma_if < 0) {
1392 dev_err(&mac->pdev->dev, "Can't map DMA interface\n");
1393 err = -ENODEV;
1394 goto out;
1395 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001396
1397 switch (pdev->device) {
1398 case 0xa005:
1399 mac->type = MAC_TYPE_GMAC;
1400 break;
1401 case 0xa006:
1402 mac->type = MAC_TYPE_XAUI;
1403 break;
1404 default:
1405 err = -ENODEV;
1406 goto out;
1407 }
1408
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001409 dev->open = pasemi_mac_open;
1410 dev->stop = pasemi_mac_close;
1411 dev->hard_start_xmit = pasemi_mac_start_tx;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001412 dev->set_multicast_list = pasemi_mac_set_rx_mode;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001413
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001414 if (err)
1415 goto out;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001416
Olof Johanssonceb51362007-05-08 00:47:49 -05001417 mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1418
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001419 /* Enable most messages by default */
1420 mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1421
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001422 err = register_netdev(dev);
1423
1424 if (err) {
1425 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1426 err);
1427 goto out;
Olof Johansson69c29d82007-10-02 16:24:51 -05001428 } else if netif_msg_probe(mac)
Olof Johansson72b05b92007-11-28 20:54:28 -06001429 printk(KERN_INFO "%s: PA Semi %s: intf %d, hw addr %s\n",
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001430 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
Olof Johansson72b05b92007-11-28 20:54:28 -06001431 mac->dma_if, print_mac(mac_buf, dev->dev_addr));
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001432
1433 return err;
1434
1435out:
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001436 if (mac->iob_pdev)
1437 pci_dev_put(mac->iob_pdev);
1438 if (mac->dma_pdev)
1439 pci_dev_put(mac->dma_pdev);
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001440
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001441 free_netdev(dev);
1442out_disable_device:
1443 pci_disable_device(pdev);
1444 return err;
1445
1446}
1447
1448static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1449{
1450 struct net_device *netdev = pci_get_drvdata(pdev);
1451 struct pasemi_mac *mac;
1452
1453 if (!netdev)
1454 return;
1455
1456 mac = netdev_priv(netdev);
1457
1458 unregister_netdev(netdev);
1459
1460 pci_disable_device(pdev);
1461 pci_dev_put(mac->dma_pdev);
1462 pci_dev_put(mac->iob_pdev);
1463
Olof Johansson34c20622007-11-28 20:56:32 -06001464 pasemi_dma_free_chan(&mac->tx->chan);
1465 pasemi_dma_free_chan(&mac->rx->chan);
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001466
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001467 pci_set_drvdata(pdev, NULL);
1468 free_netdev(netdev);
1469}
1470
1471static struct pci_device_id pasemi_mac_pci_tbl[] = {
1472 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1473 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
olof@lixom.netfd178252007-05-12 14:57:36 -05001474 { },
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001475};
1476
1477MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1478
1479static struct pci_driver pasemi_mac_driver = {
1480 .name = "pasemi_mac",
1481 .id_table = pasemi_mac_pci_tbl,
1482 .probe = pasemi_mac_probe,
1483 .remove = __devexit_p(pasemi_mac_remove),
1484};
1485
1486static void __exit pasemi_mac_cleanup_module(void)
1487{
1488 pci_unregister_driver(&pasemi_mac_driver);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001489}
1490
1491int pasemi_mac_init_module(void)
1492{
Olof Johansson34c20622007-11-28 20:56:32 -06001493 int err;
1494
1495 err = pasemi_dma_init();
1496 if (err)
1497 return err;
1498
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001499 return pci_register_driver(&pasemi_mac_driver);
1500}
1501
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001502module_init(pasemi_mac_init_module);
1503module_exit(pasemi_mac_cleanup_module);