blob: a2527b53da8be4e359bbcd43b5e92c0bf9f2ef53 [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 Johansson771f7402007-05-08 00:47:21 -050038
Olof Johanssonf5cd7872007-01-31 21:43:54 -060039#include "pasemi_mac.h"
40
Olof Johansson8dc121a2007-10-02 16:26:53 -050041/* We have our own align, since ppc64 in general has it at 0 because
42 * of design flaws in some of the server bridge chips. However, for
43 * PWRficient doing the unaligned copies is more expensive than doing
44 * unaligned DMA, so make sure the data is aligned instead.
45 */
46#define LOCAL_SKB_ALIGN 2
Olof Johanssonf5cd7872007-01-31 21:43:54 -060047
48/* TODO list
49 *
Olof Johanssonf5cd7872007-01-31 21:43:54 -060050 * - Multicast support
51 * - Large MTU support
Olof Johansson7ddeae22007-10-02 16:27:28 -050052 * - SW LRO
53 * - Multiqueue RX/TX
Olof Johanssonf5cd7872007-01-31 21:43:54 -060054 */
55
56
57/* Must be a power of two */
Olof Johanssonad5da102007-10-02 16:27:15 -050058#define RX_RING_SIZE 4096
59#define TX_RING_SIZE 4096
Olof Johanssonf5cd7872007-01-31 21:43:54 -060060
Olof Johanssonceb51362007-05-08 00:47:49 -050061#define DEFAULT_MSG_ENABLE \
62 (NETIF_MSG_DRV | \
63 NETIF_MSG_PROBE | \
64 NETIF_MSG_LINK | \
65 NETIF_MSG_TIMER | \
66 NETIF_MSG_IFDOWN | \
67 NETIF_MSG_IFUP | \
68 NETIF_MSG_RX_ERR | \
69 NETIF_MSG_TX_ERR)
70
Olof Johansson72b05b92007-11-28 20:54:28 -060071#define TX_DESC(tx, num) ((tx)->ring[(num) & (TX_RING_SIZE-1)])
72#define TX_DESC_INFO(tx, num) ((tx)->ring_info[(num) & (TX_RING_SIZE-1)])
73#define RX_DESC(rx, num) ((rx)->ring[(num) & (RX_RING_SIZE-1)])
74#define RX_DESC_INFO(rx, num) ((rx)->ring_info[(num) & (RX_RING_SIZE-1)])
75#define RX_BUFF(rx, num) ((rx)->buffers[(num) & (RX_RING_SIZE-1)])
Olof Johanssonf5cd7872007-01-31 21:43:54 -060076
Olof Johansson021fa222007-08-22 09:13:11 -050077#define RING_USED(ring) (((ring)->next_to_fill - (ring)->next_to_clean) \
78 & ((ring)->size - 1))
79#define RING_AVAIL(ring) ((ring->size) - RING_USED(ring))
80
Olof Johanssonf5cd7872007-01-31 21:43:54 -060081#define BUF_SIZE 1646 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
82
Olof Johanssonceb51362007-05-08 00:47:49 -050083MODULE_LICENSE("GPL");
84MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
85MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
86
87static int debug = -1; /* -1 == use DEFAULT_MSG_ENABLE as value */
88module_param(debug, int, 0);
89MODULE_PARM_DESC(debug, "PA Semi MAC bitmapped debugging message enable value");
Olof Johanssonf5cd7872007-01-31 21:43:54 -060090
91static struct pasdma_status *dma_status;
92
Olof Johanssonaf289e82007-10-03 13:03:54 -050093static int translation_enabled(void)
94{
95#if defined(CONFIG_PPC_PASEMI_IOMMU_DMA_FORCE)
96 return 1;
97#else
98 return firmware_has_feature(FW_FEATURE_LPAR);
99#endif
100}
101
Olof Johanssona85b9422007-09-15 13:40:59 -0700102static void write_iob_reg(struct pasemi_mac *mac, unsigned int reg,
103 unsigned int val)
104{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700105 out_le32(mac->iob_regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700106}
107
108static unsigned int read_mac_reg(struct pasemi_mac *mac, unsigned int reg)
109{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700110 return in_le32(mac->regs+reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700111}
112
113static void write_mac_reg(struct pasemi_mac *mac, unsigned int reg,
114 unsigned int val)
115{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700116 out_le32(mac->regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700117}
118
119static unsigned int read_dma_reg(struct pasemi_mac *mac, unsigned int reg)
120{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700121 return in_le32(mac->dma_regs+reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700122}
123
124static void write_dma_reg(struct pasemi_mac *mac, unsigned int reg,
125 unsigned int val)
126{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700127 out_le32(mac->dma_regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700128}
129
Olof Johansson72b05b92007-11-28 20:54:28 -0600130static struct pasemi_mac_rxring *rx_ring(struct pasemi_mac *mac)
131{
132 return mac->rx;
133}
134
135static struct pasemi_mac_txring *tx_ring(struct pasemi_mac *mac)
136{
137 return mac->tx;
138}
139
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600140static int pasemi_get_mac_addr(struct pasemi_mac *mac)
141{
142 struct pci_dev *pdev = mac->pdev;
143 struct device_node *dn = pci_device_to_OF_node(pdev);
olof@lixom.net1af7f052007-05-12 14:57:46 -0500144 int len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600145 const u8 *maddr;
146 u8 addr[6];
147
148 if (!dn) {
149 dev_dbg(&pdev->dev,
150 "No device node for mac, not configuring\n");
151 return -ENOENT;
152 }
153
olof@lixom.net1af7f052007-05-12 14:57:46 -0500154 maddr = of_get_property(dn, "local-mac-address", &len);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500155
olof@lixom.net1af7f052007-05-12 14:57:46 -0500156 if (maddr && len == 6) {
157 memcpy(mac->mac_addr, maddr, 6);
158 return 0;
159 }
160
161 /* Some old versions of firmware mistakenly uses mac-address
162 * (and as a string) instead of a byte array in local-mac-address.
163 */
164
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500165 if (maddr == NULL)
Linus Torvalds90287802007-05-08 11:57:17 -0700166 maddr = of_get_property(dn, "mac-address", NULL);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500167
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600168 if (maddr == NULL) {
169 dev_warn(&pdev->dev,
170 "no mac address in device tree, not configuring\n");
171 return -ENOENT;
172 }
173
olof@lixom.net1af7f052007-05-12 14:57:46 -0500174
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600175 if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0],
176 &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) {
177 dev_warn(&pdev->dev,
178 "can't parse mac address, not configuring\n");
179 return -EINVAL;
180 }
181
olof@lixom.net1af7f052007-05-12 14:57:46 -0500182 memcpy(mac->mac_addr, addr, 6);
183
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600184 return 0;
185}
186
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500187static int pasemi_mac_unmap_tx_skb(struct pasemi_mac *mac,
188 struct sk_buff *skb,
189 dma_addr_t *dmas)
190{
191 int f;
192 int nfrags = skb_shinfo(skb)->nr_frags;
193
194 pci_unmap_single(mac->dma_pdev, dmas[0], skb_headlen(skb),
195 PCI_DMA_TODEVICE);
196
197 for (f = 0; f < nfrags; f++) {
198 skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
199
200 pci_unmap_page(mac->dma_pdev, dmas[f+1], frag->size,
201 PCI_DMA_TODEVICE);
202 }
203 dev_kfree_skb_irq(skb);
204
205 /* Freed descriptor slot + main SKB ptr + nfrags additional ptrs,
206 * aligned up to a power of 2
207 */
208 return (nfrags + 3) & ~1;
209}
210
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600211static int pasemi_mac_setup_rx_resources(struct net_device *dev)
212{
213 struct pasemi_mac_rxring *ring;
214 struct pasemi_mac *mac = netdev_priv(dev);
215 int chan_id = mac->dma_rxch;
Olof Johanssonaf289e82007-10-03 13:03:54 -0500216 unsigned int cfg;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600217
218 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
219
220 if (!ring)
221 goto out_ring;
222
223 spin_lock_init(&ring->lock);
224
Olof Johansson021fa222007-08-22 09:13:11 -0500225 ring->size = RX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500226 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600227 RX_RING_SIZE, GFP_KERNEL);
228
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500229 if (!ring->ring_info)
230 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600231
232 /* Allocate descriptors */
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500233 ring->ring = dma_alloc_coherent(&mac->dma_pdev->dev,
234 RX_RING_SIZE * sizeof(u64),
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600235 &ring->dma, GFP_KERNEL);
236
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500237 if (!ring->ring)
238 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600239
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500240 memset(ring->ring, 0, RX_RING_SIZE * sizeof(u64));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600241
242 ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev,
243 RX_RING_SIZE * sizeof(u64),
244 &ring->buf_dma, GFP_KERNEL);
245 if (!ring->buffers)
246 goto out_buffers;
247
248 memset(ring->buffers, 0, RX_RING_SIZE * sizeof(u64));
249
Olof Johanssona85b9422007-09-15 13:40:59 -0700250 write_dma_reg(mac, PAS_DMA_RXCHAN_BASEL(chan_id), PAS_DMA_RXCHAN_BASEL_BRBL(ring->dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600251
Olof Johanssona85b9422007-09-15 13:40:59 -0700252 write_dma_reg(mac, PAS_DMA_RXCHAN_BASEU(chan_id),
253 PAS_DMA_RXCHAN_BASEU_BRBH(ring->dma >> 32) |
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500254 PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600255
Olof Johanssonaf289e82007-10-03 13:03:54 -0500256 cfg = PAS_DMA_RXCHAN_CFG_HBU(2);
257
258 if (translation_enabled())
259 cfg |= PAS_DMA_RXCHAN_CFG_CTR;
260
261 write_dma_reg(mac, PAS_DMA_RXCHAN_CFG(chan_id), cfg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600262
Olof Johanssona85b9422007-09-15 13:40:59 -0700263 write_dma_reg(mac, PAS_DMA_RXINT_BASEL(mac->dma_if),
Olof Johanssonaf289e82007-10-03 13:03:54 -0500264 PAS_DMA_RXINT_BASEL_BRBL(ring->buf_dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600265
Olof Johanssona85b9422007-09-15 13:40:59 -0700266 write_dma_reg(mac, PAS_DMA_RXINT_BASEU(mac->dma_if),
Olof Johanssonaf289e82007-10-03 13:03:54 -0500267 PAS_DMA_RXINT_BASEU_BRBH(ring->buf_dma >> 32) |
Olof Johanssona85b9422007-09-15 13:40:59 -0700268 PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600269
Olof Johanssonaf289e82007-10-03 13:03:54 -0500270 cfg = PAS_DMA_RXINT_CFG_DHL(3) | PAS_DMA_RXINT_CFG_L2 |
271 PAS_DMA_RXINT_CFG_LW | PAS_DMA_RXINT_CFG_RBP |
272 PAS_DMA_RXINT_CFG_HEN;
273
274 if (translation_enabled())
275 cfg |= PAS_DMA_RXINT_CFG_ITRR | PAS_DMA_RXINT_CFG_ITR;
276
277 write_dma_reg(mac, PAS_DMA_RXINT_CFG(mac->dma_if), cfg);
Olof Johanssonc0efd522007-08-22 09:12:52 -0500278
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600279 ring->next_to_fill = 0;
280 ring->next_to_clean = 0;
281
Olof Johansson72b05b92007-11-28 20:54:28 -0600282 ring->status = &dma_status->rx_sta[mac->dma_rxch];
283 ring->mac = mac;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600284 mac->rx = ring;
285
286 return 0;
287
288out_buffers:
289 dma_free_coherent(&mac->dma_pdev->dev,
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500290 RX_RING_SIZE * sizeof(u64),
Olof Johansson72b05b92007-11-28 20:54:28 -0600291 rx_ring(mac)->ring, rx_ring(mac)->dma);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500292out_ring_desc:
293 kfree(ring->ring_info);
294out_ring_info:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600295 kfree(ring);
296out_ring:
297 return -ENOMEM;
298}
299
Olof Johansson72b05b92007-11-28 20:54:28 -0600300static struct pasemi_mac_txring *
301pasemi_mac_setup_tx_resources(struct net_device *dev, int txch)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600302{
303 struct pasemi_mac *mac = netdev_priv(dev);
304 u32 val;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600305 struct pasemi_mac_txring *ring;
Olof Johanssonaf289e82007-10-03 13:03:54 -0500306 unsigned int cfg;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600307
308 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
309 if (!ring)
310 goto out_ring;
311
312 spin_lock_init(&ring->lock);
313
Olof Johansson021fa222007-08-22 09:13:11 -0500314 ring->size = TX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500315 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600316 TX_RING_SIZE, GFP_KERNEL);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500317 if (!ring->ring_info)
318 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600319
320 /* Allocate descriptors */
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500321 ring->ring = dma_alloc_coherent(&mac->dma_pdev->dev,
322 TX_RING_SIZE * sizeof(u64),
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600323 &ring->dma, GFP_KERNEL);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500324 if (!ring->ring)
325 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600326
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500327 memset(ring->ring, 0, TX_RING_SIZE * sizeof(u64));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600328
Olof Johansson72b05b92007-11-28 20:54:28 -0600329 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEL(txch),
Olof Johanssona85b9422007-09-15 13:40:59 -0700330 PAS_DMA_TXCHAN_BASEL_BRBL(ring->dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600331 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->dma >> 32);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500332 val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 3);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600333
Olof Johansson72b05b92007-11-28 20:54:28 -0600334 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEU(txch), val);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600335
Olof Johanssonaf289e82007-10-03 13:03:54 -0500336 cfg = PAS_DMA_TXCHAN_CFG_TY_IFACE |
337 PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
338 PAS_DMA_TXCHAN_CFG_UP |
339 PAS_DMA_TXCHAN_CFG_WT(2);
340
341 if (translation_enabled())
342 cfg |= PAS_DMA_TXCHAN_CFG_TRD | PAS_DMA_TXCHAN_CFG_TRR;
343
Olof Johansson72b05b92007-11-28 20:54:28 -0600344 write_dma_reg(mac, PAS_DMA_TXCHAN_CFG(txch), cfg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600345
Olof Johansson021fa222007-08-22 09:13:11 -0500346 ring->next_to_fill = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600347 ring->next_to_clean = 0;
Olof Johansson72b05b92007-11-28 20:54:28 -0600348 ring->status = &dma_status->tx_sta[txch];
349 ring->chan = txch;
350 ring->mac = mac;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600351
Olof Johansson72b05b92007-11-28 20:54:28 -0600352 return ring;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600353
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500354out_ring_desc:
355 kfree(ring->ring_info);
356out_ring_info:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600357 kfree(ring);
358out_ring:
Olof Johansson72b05b92007-11-28 20:54:28 -0600359 return NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600360}
361
Olof Johansson72b05b92007-11-28 20:54:28 -0600362static void pasemi_mac_free_tx_resources(struct pasemi_mac *mac)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600363{
Olof Johansson72b05b92007-11-28 20:54:28 -0600364 struct pasemi_mac_txring *txring = tx_ring(mac);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500365 unsigned int i, j;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600366 struct pasemi_mac_buffer *info;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500367 dma_addr_t dmas[MAX_SKB_FRAGS+1];
368 int freed;
Olof Johanssonad5da102007-10-02 16:27:15 -0500369 int start, limit;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600370
Olof Johansson72b05b92007-11-28 20:54:28 -0600371 start = txring->next_to_clean;
372 limit = txring->next_to_fill;
Olof Johanssonad5da102007-10-02 16:27:15 -0500373
374 /* Compensate for when fill has wrapped and clean has not */
375 if (start > limit)
376 limit += TX_RING_SIZE;
377
378 for (i = start; i < limit; i += freed) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600379 info = &txring->ring_info[(i+1) & (TX_RING_SIZE-1)];
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500380 if (info->dma && info->skb) {
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500381 for (j = 0; j <= skb_shinfo(info->skb)->nr_frags; j++)
Olof Johansson72b05b92007-11-28 20:54:28 -0600382 dmas[j] = txring->ring_info[(i+1+j) &
383 (TX_RING_SIZE-1)].dma;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500384 freed = pasemi_mac_unmap_tx_skb(mac, info->skb, dmas);
385 } else
386 freed = 2;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600387 }
388
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500389 for (i = 0; i < TX_RING_SIZE; i++)
Olof Johansson72b05b92007-11-28 20:54:28 -0600390 txring->ring[i] = 0;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500391
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600392 dma_free_coherent(&mac->dma_pdev->dev,
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500393 TX_RING_SIZE * sizeof(u64),
Olof Johansson72b05b92007-11-28 20:54:28 -0600394 txring->ring, txring->dma);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600395
Olof Johansson72b05b92007-11-28 20:54:28 -0600396 kfree(txring->ring_info);
397 kfree(txring);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600398}
399
Olof Johansson72b05b92007-11-28 20:54:28 -0600400static void pasemi_mac_free_rx_resources(struct pasemi_mac *mac)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600401{
Olof Johansson72b05b92007-11-28 20:54:28 -0600402 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600403 unsigned int i;
404 struct pasemi_mac_buffer *info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600405
406 for (i = 0; i < RX_RING_SIZE; i++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600407 info = &RX_DESC_INFO(rx, i);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500408 if (info->skb && info->dma) {
409 pci_unmap_single(mac->dma_pdev,
410 info->dma,
411 info->skb->len,
412 PCI_DMA_FROMDEVICE);
413 dev_kfree_skb_any(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600414 }
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500415 info->dma = 0;
416 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600417 }
418
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500419 for (i = 0; i < RX_RING_SIZE; i++)
Olof Johansson72b05b92007-11-28 20:54:28 -0600420 RX_DESC(rx, i) = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500421
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600422 dma_free_coherent(&mac->dma_pdev->dev,
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500423 RX_RING_SIZE * sizeof(u64),
Olof Johansson72b05b92007-11-28 20:54:28 -0600424 rx_ring(mac)->ring, rx_ring(mac)->dma);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600425
426 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
Olof Johansson72b05b92007-11-28 20:54:28 -0600427 rx_ring(mac)->buffers, rx_ring(mac)->buf_dma);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600428
Olof Johansson72b05b92007-11-28 20:54:28 -0600429 kfree(rx_ring(mac)->ring_info);
430 kfree(rx_ring(mac));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600431 mac->rx = NULL;
432}
433
Olof Johansson928773c2007-09-26 16:25:06 -0500434static void pasemi_mac_replenish_rx_ring(struct net_device *dev, int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600435{
436 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -0600437 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500438 int fill, count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600439
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500440 if (limit <= 0)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600441 return;
442
Olof Johansson72b05b92007-11-28 20:54:28 -0600443 fill = rx_ring(mac)->next_to_fill;
Olof Johansson928773c2007-09-26 16:25:06 -0500444 for (count = 0; count < limit; count++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600445 struct pasemi_mac_buffer *info = &RX_DESC_INFO(rx, fill);
446 u64 *buff = &RX_BUFF(rx, fill);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600447 struct sk_buff *skb;
448 dma_addr_t dma;
449
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500450 /* Entry in use? */
451 WARN_ON(*buff);
452
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500453 /* skb might still be in there for recycle on short receives */
454 if (info->skb)
455 skb = info->skb;
Olof Johansson8dc121a2007-10-02 16:26:53 -0500456 else {
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500457 skb = dev_alloc_skb(BUF_SIZE);
Olof Johansson8dc121a2007-10-02 16:26:53 -0500458 skb_reserve(skb, LOCAL_SKB_ALIGN);
459 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600460
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500461 if (unlikely(!skb))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600462 break;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600463
Olof Johansson8dc121a2007-10-02 16:26:53 -0500464 dma = pci_map_single(mac->dma_pdev, skb->data,
465 BUF_SIZE - LOCAL_SKB_ALIGN,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600466 PCI_DMA_FROMDEVICE);
467
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500468 if (unlikely(dma_mapping_error(dma))) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600469 dev_kfree_skb_irq(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600470 break;
471 }
472
473 info->skb = skb;
474 info->dma = dma;
475 *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500476 fill++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600477 }
478
479 wmb();
480
Olof Johansson928773c2007-09-26 16:25:06 -0500481 write_dma_reg(mac, PAS_DMA_RXINT_INCR(mac->dma_if), count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600482
Olof Johansson72b05b92007-11-28 20:54:28 -0600483 rx_ring(mac)->next_to_fill = (rx_ring(mac)->next_to_fill + count) &
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500484 (RX_RING_SIZE - 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600485}
486
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500487static void pasemi_mac_restart_rx_intr(struct pasemi_mac *mac)
488{
Olof Johansson52a94352007-05-12 18:01:09 -0500489 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500490 /* Re-enable packet count interrupts: finally
491 * ack the packet count interrupt we got in rx_intr.
492 */
493
Olof Johansson72b05b92007-11-28 20:54:28 -0600494 pcnt = *rx_ring(mac)->status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500495
Olof Johansson52a94352007-05-12 18:01:09 -0500496 reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500497
Olof Johanssona85b9422007-09-15 13:40:59 -0700498 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500499}
500
501static void pasemi_mac_restart_tx_intr(struct pasemi_mac *mac)
502{
Olof Johansson52a94352007-05-12 18:01:09 -0500503 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500504
505 /* Re-enable packet count interrupts */
Olof Johansson72b05b92007-11-28 20:54:28 -0600506 pcnt = *tx_ring(mac)->status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500507
Olof Johansson52a94352007-05-12 18:01:09 -0500508 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500509
Olof Johansson72b05b92007-11-28 20:54:28 -0600510 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(tx_ring(mac)->chan), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500511}
512
513
Olof Johansson69c29d82007-10-02 16:24:51 -0500514static inline void pasemi_mac_rx_error(struct pasemi_mac *mac, u64 macrx)
515{
516 unsigned int rcmdsta, ccmdsta;
517
518 if (!netif_msg_rx_err(mac))
519 return;
520
521 rcmdsta = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
522 ccmdsta = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
523
524 printk(KERN_ERR "pasemi_mac: rx error. macrx %016lx, rx status %lx\n",
Olof Johansson72b05b92007-11-28 20:54:28 -0600525 macrx, *rx_ring(mac)->status);
Olof Johansson69c29d82007-10-02 16:24:51 -0500526
527 printk(KERN_ERR "pasemi_mac: rcmdsta %08x ccmdsta %08x\n",
528 rcmdsta, ccmdsta);
529}
530
531static inline void pasemi_mac_tx_error(struct pasemi_mac *mac, u64 mactx)
532{
533 unsigned int cmdsta;
534
535 if (!netif_msg_tx_err(mac))
536 return;
537
538 cmdsta = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
539
540 printk(KERN_ERR "pasemi_mac: tx error. mactx 0x%016lx, "\
Olof Johansson72b05b92007-11-28 20:54:28 -0600541 "tx status 0x%016lx\n", mactx, *tx_ring(mac)->status);
Olof Johansson69c29d82007-10-02 16:24:51 -0500542
543 printk(KERN_ERR "pasemi_mac: tcmdsta 0x%08x\n", cmdsta);
544}
545
Olof Johansson72b05b92007-11-28 20:54:28 -0600546static int pasemi_mac_clean_rx(struct pasemi_mac_rxring *rx, int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600547{
Olof Johansson72b05b92007-11-28 20:54:28 -0600548 struct pasemi_mac *mac = rx->mac;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500549 unsigned int n;
550 int count;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500551 struct pasemi_mac_buffer *info;
552 struct sk_buff *skb;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500553 unsigned int len;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500554 u64 macrx;
555 dma_addr_t dma;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500556 int buf_index;
557 u64 eval;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600558
Olof Johansson72b05b92007-11-28 20:54:28 -0600559 spin_lock(&rx->lock);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600560
Olof Johansson72b05b92007-11-28 20:54:28 -0600561 n = rx->next_to_clean;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600562
Olof Johansson72b05b92007-11-28 20:54:28 -0600563 prefetch(&RX_DESC(rx, n));
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500564
565 for (count = 0; count < limit; count++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600566 macrx = RX_DESC(rx, n);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600567
Olof Johansson69c29d82007-10-02 16:24:51 -0500568 if ((macrx & XCT_MACRX_E) ||
Olof Johansson72b05b92007-11-28 20:54:28 -0600569 (*rx_ring(mac)->status & PAS_STATUS_ERROR))
Olof Johansson69c29d82007-10-02 16:24:51 -0500570 pasemi_mac_rx_error(mac, macrx);
571
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500572 if (!(macrx & XCT_MACRX_O))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600573 break;
574
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600575 info = NULL;
576
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500577 BUG_ON(!(macrx & XCT_MACRX_RR_8BRES));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600578
Olof Johansson72b05b92007-11-28 20:54:28 -0600579 eval = (RX_DESC(rx, n+1) & XCT_RXRES_8B_EVAL_M) >>
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500580 XCT_RXRES_8B_EVAL_S;
581 buf_index = eval-1;
582
Olof Johansson72b05b92007-11-28 20:54:28 -0600583 dma = (RX_DESC(rx, n+2) & XCT_PTR_ADDR_M);
584 info = &RX_DESC_INFO(rx, buf_index);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500585
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500586 skb = info->skb;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600587
Olof Johanssonad5da102007-10-02 16:27:15 -0500588 prefetch(skb);
589 prefetch(&skb->data_len);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600590
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500591 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600592
Olof Johansson32bee772007-11-06 22:21:38 -0600593 pci_unmap_single(mac->dma_pdev, dma, len, PCI_DMA_FROMDEVICE);
594
595 if (macrx & XCT_MACRX_CRC) {
596 /* CRC error flagged */
597 mac->netdev->stats.rx_errors++;
598 mac->netdev->stats.rx_crc_errors++;
Olof Johansson4352d822007-12-03 21:34:14 -0600599 /* No need to free skb, it'll be reused */
Olof Johansson32bee772007-11-06 22:21:38 -0600600 goto next;
601 }
602
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500603 if (len < 256) {
Olof Johansson8dc121a2007-10-02 16:26:53 -0500604 struct sk_buff *new_skb;
605
606 new_skb = netdev_alloc_skb(mac->netdev,
607 len + LOCAL_SKB_ALIGN);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500608 if (new_skb) {
Olof Johansson8dc121a2007-10-02 16:26:53 -0500609 skb_reserve(new_skb, LOCAL_SKB_ALIGN);
Olof Johansson73344862007-08-22 09:12:55 -0500610 memcpy(new_skb->data, skb->data, len);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500611 /* save the skb in buffer_info as good */
612 skb = new_skb;
613 }
614 /* else just continue with the old one */
615 } else
616 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600617
Olof Johanssonad5da102007-10-02 16:27:15 -0500618 info->dma = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500619
Olof Johansson32bee772007-11-06 22:21:38 -0600620 /* Don't include CRC */
621 skb_put(skb, len-4);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600622
Olof Johansson26fcfa92007-08-22 09:12:59 -0500623 if (likely((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK)) {
Olof Johansson38bf3182007-08-22 09:13:24 -0500624 skb->ip_summed = CHECKSUM_UNNECESSARY;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500625 skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600626 XCT_MACRX_CSUM_S;
627 } else
628 skb->ip_summed = CHECKSUM_NONE;
629
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700630 mac->netdev->stats.rx_bytes += len;
631 mac->netdev->stats.rx_packets++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600632
Olof Johansson26fcfa92007-08-22 09:12:59 -0500633 skb->protocol = eth_type_trans(skb, mac->netdev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600634 netif_receive_skb(skb);
635
Olof Johansson32bee772007-11-06 22:21:38 -0600636next:
Olof Johansson72b05b92007-11-28 20:54:28 -0600637 RX_DESC(rx, n) = 0;
638 RX_DESC(rx, n+1) = 0;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500639
Olof Johanssonad5da102007-10-02 16:27:15 -0500640 /* Need to zero it out since hardware doesn't, since the
641 * replenish loop uses it to tell when it's done.
642 */
Olof Johansson72b05b92007-11-28 20:54:28 -0600643 RX_BUFF(rx, buf_index) = 0;
Olof Johanssonad5da102007-10-02 16:27:15 -0500644
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500645 n += 4;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600646 }
647
Olof Johansson9a50beb2007-10-02 16:26:30 -0500648 if (n > RX_RING_SIZE) {
649 /* Errata 5971 workaround: L2 target of headers */
650 write_iob_reg(mac, PAS_IOB_COM_PKTHDRCNT, 0);
651 n &= (RX_RING_SIZE-1);
652 }
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500653
Olof Johansson72b05b92007-11-28 20:54:28 -0600654 rx_ring(mac)->next_to_clean = n;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500655
656 /* Increase is in number of 16-byte entries, and since each descriptor
657 * with an 8BRES takes up 3x8 bytes (padded to 4x8), increase with
658 * count*2.
659 */
660 write_dma_reg(mac, PAS_DMA_RXCHAN_INCR(mac->dma_rxch), count << 1);
661
662 pasemi_mac_replenish_rx_ring(mac->netdev, count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600663
Olof Johansson72b05b92007-11-28 20:54:28 -0600664 spin_unlock(&rx_ring(mac)->lock);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600665
666 return count;
667}
668
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500669/* Can't make this too large or we blow the kernel stack limits */
670#define TX_CLEAN_BATCHSIZE (128/MAX_SKB_FRAGS)
671
Olof Johansson72b05b92007-11-28 20:54:28 -0600672static int pasemi_mac_clean_tx(struct pasemi_mac_txring *txring)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600673{
Olof Johansson72b05b92007-11-28 20:54:28 -0600674 struct pasemi_mac *mac = txring->mac;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500675 int i, j;
Olof Johanssonad5da102007-10-02 16:27:15 -0500676 unsigned int start, descr_count, buf_count, batch_limit;
677 unsigned int ring_limit;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500678 unsigned int total_count;
Olof Johanssonca7e2352007-09-26 16:23:59 -0500679 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500680 struct sk_buff *skbs[TX_CLEAN_BATCHSIZE];
681 dma_addr_t dmas[TX_CLEAN_BATCHSIZE][MAX_SKB_FRAGS+1];
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600682
Olof Johansson02df6cf2007-08-22 09:13:03 -0500683 total_count = 0;
Olof Johanssonad5da102007-10-02 16:27:15 -0500684 batch_limit = TX_CLEAN_BATCHSIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500685restart:
Olof Johansson72b05b92007-11-28 20:54:28 -0600686 spin_lock_irqsave(&txring->lock, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600687
Olof Johansson72b05b92007-11-28 20:54:28 -0600688 start = txring->next_to_clean;
689 ring_limit = txring->next_to_fill;
Olof Johanssonad5da102007-10-02 16:27:15 -0500690
691 /* Compensate for when fill has wrapped but clean has not */
692 if (start > ring_limit)
693 ring_limit += TX_RING_SIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500694
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500695 buf_count = 0;
696 descr_count = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600697
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500698 for (i = start;
Olof Johanssonad5da102007-10-02 16:27:15 -0500699 descr_count < batch_limit && i < ring_limit;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500700 i += buf_count) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600701 u64 mactx = TX_DESC(txring, i);
Olof Johanssonad5da102007-10-02 16:27:15 -0500702 struct sk_buff *skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500703
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500704 if ((mactx & XCT_MACTX_E) ||
Olof Johansson72b05b92007-11-28 20:54:28 -0600705 (*tx_ring(mac)->status & PAS_STATUS_ERROR))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500706 pasemi_mac_tx_error(mac, mactx);
Olof Johansson69c29d82007-10-02 16:24:51 -0500707
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500708 if (unlikely(mactx & XCT_MACTX_O))
Olof Johansson02df6cf2007-08-22 09:13:03 -0500709 /* Not yet transmitted */
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600710 break;
711
Olof Johansson72b05b92007-11-28 20:54:28 -0600712 skb = TX_DESC_INFO(txring, i+1).skb;
Olof Johanssonad5da102007-10-02 16:27:15 -0500713 skbs[descr_count] = skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500714
Olof Johanssonad5da102007-10-02 16:27:15 -0500715 buf_count = 2 + skb_shinfo(skb)->nr_frags;
716 for (j = 0; j <= skb_shinfo(skb)->nr_frags; j++)
Olof Johansson72b05b92007-11-28 20:54:28 -0600717 dmas[descr_count][j] = TX_DESC_INFO(txring, i+1+j).dma;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500718
Olof Johansson72b05b92007-11-28 20:54:28 -0600719 TX_DESC(txring, i) = 0;
720 TX_DESC(txring, i+1) = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500721
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500722 /* Since we always fill with an even number of entries, make
723 * sure we skip any unused one at the end as well.
724 */
725 if (buf_count & 1)
726 buf_count++;
727 descr_count++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600728 }
Olof Johansson72b05b92007-11-28 20:54:28 -0600729 txring->next_to_clean = i & (TX_RING_SIZE-1);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500730
Olof Johansson72b05b92007-11-28 20:54:28 -0600731 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500732 netif_wake_queue(mac->netdev);
733
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500734 for (i = 0; i < descr_count; i++)
735 pasemi_mac_unmap_tx_skb(mac, skbs[i], dmas[i]);
Olof Johansson02df6cf2007-08-22 09:13:03 -0500736
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500737 total_count += descr_count;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500738
739 /* If the batch was full, try to clean more */
Olof Johanssonad5da102007-10-02 16:27:15 -0500740 if (descr_count == batch_limit)
Olof Johansson02df6cf2007-08-22 09:13:03 -0500741 goto restart;
742
743 return total_count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600744}
745
746
747static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
748{
749 struct net_device *dev = data;
750 struct pasemi_mac *mac = netdev_priv(dev);
751 unsigned int reg;
752
Olof Johansson72b05b92007-11-28 20:54:28 -0600753 if (!(*rx_ring(mac)->status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600754 return IRQ_NONE;
755
Olof Johansson6dfa7522007-05-08 00:47:32 -0500756 /* Don't reset packet count so it won't fire again but clear
757 * all others.
758 */
759
Olof Johansson6dfa7522007-05-08 00:47:32 -0500760 reg = 0;
Olof Johansson72b05b92007-11-28 20:54:28 -0600761 if (*rx_ring(mac)->status & PAS_STATUS_SOFT)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500762 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
Olof Johansson72b05b92007-11-28 20:54:28 -0600763 if (*rx_ring(mac)->status & PAS_STATUS_ERROR)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500764 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
Olof Johansson72b05b92007-11-28 20:54:28 -0600765 if (*rx_ring(mac)->status & PAS_STATUS_TIMER)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600766 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
767
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700768 netif_rx_schedule(dev, &mac->napi);
Olof Johansson6dfa7522007-05-08 00:47:32 -0500769
Olof Johanssona85b9422007-09-15 13:40:59 -0700770 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600771
772 return IRQ_HANDLED;
773}
774
775static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
776{
Olof Johansson72b05b92007-11-28 20:54:28 -0600777 struct pasemi_mac_txring *txring = data;
778 struct pasemi_mac *mac = txring->mac;
Olof Johansson52a94352007-05-12 18:01:09 -0500779 unsigned int reg, pcnt;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600780
Olof Johansson72b05b92007-11-28 20:54:28 -0600781 if (!(*txring->status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600782 return IRQ_NONE;
783
Olof Johansson72b05b92007-11-28 20:54:28 -0600784 pasemi_mac_clean_tx(txring);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600785
Olof Johansson72b05b92007-11-28 20:54:28 -0600786 pcnt = *txring->status & PAS_STATUS_PCNT_M;
Olof Johansson52a94352007-05-12 18:01:09 -0500787
788 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson6dfa7522007-05-08 00:47:32 -0500789
Olof Johansson72b05b92007-11-28 20:54:28 -0600790 if (*txring->status & PAS_STATUS_SOFT)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500791 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
Olof Johansson72b05b92007-11-28 20:54:28 -0600792 if (*txring->status & PAS_STATUS_ERROR)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500793 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600794
Olof Johansson72b05b92007-11-28 20:54:28 -0600795 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(txring->chan), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600796
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600797 return IRQ_HANDLED;
798}
799
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500800static void pasemi_adjust_link(struct net_device *dev)
801{
802 struct pasemi_mac *mac = netdev_priv(dev);
803 int msg;
804 unsigned int flags;
805 unsigned int new_flags;
806
807 if (!mac->phydev->link) {
808 /* If no link, MAC speed settings don't matter. Just report
809 * link down and return.
810 */
811 if (mac->link && netif_msg_link(mac))
812 printk(KERN_INFO "%s: Link is down.\n", dev->name);
813
814 netif_carrier_off(dev);
815 mac->link = 0;
816
817 return;
818 } else
819 netif_carrier_on(dev);
820
Olof Johanssona85b9422007-09-15 13:40:59 -0700821 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500822 new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
823 PAS_MAC_CFG_PCFG_TSR_M);
824
825 if (!mac->phydev->duplex)
826 new_flags |= PAS_MAC_CFG_PCFG_HD;
827
828 switch (mac->phydev->speed) {
829 case 1000:
830 new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
831 PAS_MAC_CFG_PCFG_TSR_1G;
832 break;
833 case 100:
834 new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
835 PAS_MAC_CFG_PCFG_TSR_100M;
836 break;
837 case 10:
838 new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
839 PAS_MAC_CFG_PCFG_TSR_10M;
840 break;
841 default:
842 printk("Unsupported speed %d\n", mac->phydev->speed);
843 }
844
845 /* Print on link or speed/duplex change */
846 msg = mac->link != mac->phydev->link || flags != new_flags;
847
848 mac->duplex = mac->phydev->duplex;
849 mac->speed = mac->phydev->speed;
850 mac->link = mac->phydev->link;
851
852 if (new_flags != flags)
Olof Johanssona85b9422007-09-15 13:40:59 -0700853 write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500854
855 if (msg && netif_msg_link(mac))
856 printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
857 dev->name, mac->speed, mac->duplex ? "full" : "half");
858}
859
860static int pasemi_mac_phy_init(struct net_device *dev)
861{
862 struct pasemi_mac *mac = netdev_priv(dev);
863 struct device_node *dn, *phy_dn;
864 struct phy_device *phydev;
865 unsigned int phy_id;
866 const phandle *ph;
867 const unsigned int *prop;
868 struct resource r;
869 int ret;
870
871 dn = pci_device_to_OF_node(mac->pdev);
Linus Torvalds90287802007-05-08 11:57:17 -0700872 ph = of_get_property(dn, "phy-handle", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500873 if (!ph)
874 return -ENODEV;
875 phy_dn = of_find_node_by_phandle(*ph);
876
Linus Torvalds90287802007-05-08 11:57:17 -0700877 prop = of_get_property(phy_dn, "reg", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500878 ret = of_address_to_resource(phy_dn->parent, 0, &r);
879 if (ret)
880 goto err;
881
882 phy_id = *prop;
883 snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);
884
885 of_node_put(phy_dn);
886
887 mac->link = 0;
888 mac->speed = 0;
889 mac->duplex = -1;
890
891 phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);
892
893 if (IS_ERR(phydev)) {
894 printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
895 return PTR_ERR(phydev);
896 }
897
898 mac->phydev = phydev;
899
900 return 0;
901
902err:
903 of_node_put(phy_dn);
904 return -ENODEV;
905}
906
907
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600908static int pasemi_mac_open(struct net_device *dev)
909{
910 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson771f7402007-05-08 00:47:21 -0500911 int base_irq;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600912 unsigned int flags;
913 int ret;
914
915 /* enable rx section */
Olof Johanssona85b9422007-09-15 13:40:59 -0700916 write_dma_reg(mac, PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600917
918 /* enable tx section */
Olof Johanssona85b9422007-09-15 13:40:59 -0700919 write_dma_reg(mac, PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600920
921 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
922 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
923 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
924
Olof Johanssona85b9422007-09-15 13:40:59 -0700925 write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600926
Olof Johanssona85b9422007-09-15 13:40:59 -0700927 write_iob_reg(mac, PAS_IOB_DMA_RXCH_CFG(mac->dma_rxch),
928 PAS_IOB_DMA_RXCH_CFG_CNTTH(0));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600929
Olof Johanssona85b9422007-09-15 13:40:59 -0700930 write_iob_reg(mac, PAS_IOB_DMA_TXCH_CFG(mac->dma_txch),
Olof Johansson02df6cf2007-08-22 09:13:03 -0500931 PAS_IOB_DMA_TXCH_CFG_CNTTH(128));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600932
Olof Johansson6dfa7522007-05-08 00:47:32 -0500933 /* 0xffffff is max value, about 16ms */
Olof Johanssona85b9422007-09-15 13:40:59 -0700934 write_iob_reg(mac, PAS_IOB_DMA_COM_TIMEOUTCFG,
935 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0xffffff));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600936
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600937 ret = pasemi_mac_setup_rx_resources(dev);
938 if (ret)
939 goto out_rx_resources;
940
Olof Johansson72b05b92007-11-28 20:54:28 -0600941 mac->tx = pasemi_mac_setup_tx_resources(dev, mac->dma_txch);
942
943 if (!mac->tx)
944 goto out_tx_ring;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600945
Olof Johanssona85b9422007-09-15 13:40:59 -0700946 write_mac_reg(mac, PAS_MAC_IPC_CHNL,
947 PAS_MAC_IPC_CHNL_DCHNO(mac->dma_rxch) |
948 PAS_MAC_IPC_CHNL_BCH(mac->dma_rxch));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600949
950 /* enable rx if */
Olof Johanssona85b9422007-09-15 13:40:59 -0700951 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
Olof Johansson9e81d332007-10-02 16:27:39 -0500952 PAS_DMA_RXINT_RCMDSTA_EN |
953 PAS_DMA_RXINT_RCMDSTA_DROPS_M |
954 PAS_DMA_RXINT_RCMDSTA_BP |
955 PAS_DMA_RXINT_RCMDSTA_OO |
956 PAS_DMA_RXINT_RCMDSTA_BT);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600957
958 /* enable rx channel */
Olof Johanssona85b9422007-09-15 13:40:59 -0700959 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
960 PAS_DMA_RXCHAN_CCMDSTA_EN |
Olof Johansson9e81d332007-10-02 16:27:39 -0500961 PAS_DMA_RXCHAN_CCMDSTA_DU |
962 PAS_DMA_RXCHAN_CCMDSTA_OD |
963 PAS_DMA_RXCHAN_CCMDSTA_FD |
964 PAS_DMA_RXCHAN_CCMDSTA_DT);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600965
966 /* enable tx channel */
Olof Johanssona85b9422007-09-15 13:40:59 -0700967 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
Olof Johansson9e81d332007-10-02 16:27:39 -0500968 PAS_DMA_TXCHAN_TCMDSTA_EN |
969 PAS_DMA_TXCHAN_TCMDSTA_SZ |
970 PAS_DMA_TXCHAN_TCMDSTA_DB |
971 PAS_DMA_TXCHAN_TCMDSTA_DE |
972 PAS_DMA_TXCHAN_TCMDSTA_DA);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600973
Olof Johansson928773c2007-09-26 16:25:06 -0500974 pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600975
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500976 write_dma_reg(mac, PAS_DMA_RXCHAN_INCR(mac->dma_rxch), RX_RING_SIZE>>1);
977
Olof Johansson72b05b92007-11-28 20:54:28 -0600978 /* Clear out any residual packet count state from firmware */
979 pasemi_mac_restart_rx_intr(mac);
980 pasemi_mac_restart_tx_intr(mac);
981
Olof Johansson36033762007-09-26 16:24:42 -0500982 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
983 PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
984
985 if (mac->type == MAC_TYPE_GMAC)
986 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
987 else
988 flags |= PAS_MAC_CFG_PCFG_TSR_10G | PAS_MAC_CFG_PCFG_SPD_10G;
989
990 /* Enable interface in MAC */
991 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
992
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500993 ret = pasemi_mac_phy_init(dev);
994 /* Some configs don't have PHYs (XAUI etc), so don't complain about
995 * failed init due to -ENODEV.
996 */
997 if (ret && ret != -ENODEV)
998 dev_warn(&mac->pdev->dev, "phy init failed: %d\n", ret);
999
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001000 netif_start_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001001 napi_enable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001002
Olof Johansson771f7402007-05-08 00:47:21 -05001003 /* Interrupts are a bit different for our DMA controller: While
1004 * it's got one a regular PCI device header, the interrupt there
1005 * is really the base of the range it's using. Each tx and rx
1006 * channel has it's own interrupt source.
1007 */
1008
1009 base_irq = virq_to_hw(mac->dma_pdev->irq);
1010
1011 mac->tx_irq = irq_create_mapping(NULL, base_irq + mac->dma_txch);
Olof Johansson72b05b92007-11-28 20:54:28 -06001012
1013 snprintf(mac->tx_irq_name, sizeof(mac->tx_irq_name), "%s tx",
1014 dev->name);
Olof Johansson771f7402007-05-08 00:47:21 -05001015
1016 ret = request_irq(mac->tx_irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
Olof Johansson72b05b92007-11-28 20:54:28 -06001017 mac->tx_irq_name, mac->tx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001018 if (ret) {
1019 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -05001020 base_irq + mac->dma_txch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001021 goto out_tx_int;
1022 }
1023
Olof Johansson72b05b92007-11-28 20:54:28 -06001024 mac->rx_irq = irq_create_mapping(NULL, base_irq + 20 + mac->dma_rxch);
1025
1026 snprintf(mac->rx_irq_name, sizeof(mac->rx_irq_name), "%s rx",
1027 dev->name);
1028
Olof Johansson771f7402007-05-08 00:47:21 -05001029 ret = request_irq(mac->rx_irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
Olof Johansson72b05b92007-11-28 20:54:28 -06001030 mac->rx_irq_name, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001031 if (ret) {
1032 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -05001033 base_irq + 20 + mac->dma_rxch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001034 goto out_rx_int;
1035 }
1036
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001037 if (mac->phydev)
1038 phy_start(mac->phydev);
1039
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001040 return 0;
1041
1042out_rx_int:
Olof Johansson72b05b92007-11-28 20:54:28 -06001043 free_irq(mac->tx_irq, mac->tx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001044out_tx_int:
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001045 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001046 netif_stop_queue(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -06001047out_tx_ring:
1048 if (mac->tx)
1049 pasemi_mac_free_tx_resources(mac);
1050 pasemi_mac_free_rx_resources(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001051out_rx_resources:
1052
1053 return ret;
1054}
1055
1056#define MAX_RETRIES 5000
1057
1058static int pasemi_mac_close(struct net_device *dev)
1059{
1060 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson9e81d332007-10-02 16:27:39 -05001061 unsigned int sta;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001062 int retries;
1063
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001064 if (mac->phydev) {
1065 phy_stop(mac->phydev);
1066 phy_disconnect(mac->phydev);
1067 }
1068
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001069 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001070 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001071
Olof Johansson9e81d332007-10-02 16:27:39 -05001072 sta = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
1073 if (sta & (PAS_DMA_RXINT_RCMDSTA_BP |
1074 PAS_DMA_RXINT_RCMDSTA_OO |
1075 PAS_DMA_RXINT_RCMDSTA_BT))
1076 printk(KERN_DEBUG "pasemi_mac: rcmdsta error: 0x%08x\n", sta);
1077
1078 sta = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
1079 if (sta & (PAS_DMA_RXCHAN_CCMDSTA_DU |
1080 PAS_DMA_RXCHAN_CCMDSTA_OD |
1081 PAS_DMA_RXCHAN_CCMDSTA_FD |
1082 PAS_DMA_RXCHAN_CCMDSTA_DT))
1083 printk(KERN_DEBUG "pasemi_mac: ccmdsta error: 0x%08x\n", sta);
1084
1085 sta = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
Olof Johansson72b05b92007-11-28 20:54:28 -06001086 if (sta & (PAS_DMA_TXCHAN_TCMDSTA_SZ | PAS_DMA_TXCHAN_TCMDSTA_DB |
1087 PAS_DMA_TXCHAN_TCMDSTA_DE | PAS_DMA_TXCHAN_TCMDSTA_DA))
Olof Johansson9e81d332007-10-02 16:27:39 -05001088 printk(KERN_DEBUG "pasemi_mac: tcmdsta error: 0x%08x\n", sta);
1089
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001090 /* Clean out any pending buffers */
Olof Johansson72b05b92007-11-28 20:54:28 -06001091 pasemi_mac_clean_tx(tx_ring(mac));
1092 pasemi_mac_clean_rx(rx_ring(mac), RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001093
1094 /* Disable interface */
Olof Johansson72b05b92007-11-28 20:54:28 -06001095 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
1096 PAS_DMA_TXCHAN_TCMDSTA_ST);
1097 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
1098 PAS_DMA_RXINT_RCMDSTA_ST);
1099 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
1100 PAS_DMA_RXCHAN_CCMDSTA_ST);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001101
1102 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johansson9e81d332007-10-02 16:27:39 -05001103 sta = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
1104 if (!(sta & PAS_DMA_TXCHAN_TCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001105 break;
1106 cond_resched();
1107 }
1108
Olof Johansson9e81d332007-10-02 16:27:39 -05001109 if (sta & PAS_DMA_TXCHAN_TCMDSTA_ACT)
Olof Johansson72b05b92007-11-28 20:54:28 -06001110 dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel %d\n",
1111 mac->dma_txch);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001112
1113 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johansson9e81d332007-10-02 16:27:39 -05001114 sta = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
1115 if (!(sta & PAS_DMA_RXCHAN_CCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001116 break;
1117 cond_resched();
1118 }
1119
Olof Johansson9e81d332007-10-02 16:27:39 -05001120 if (sta & PAS_DMA_RXCHAN_CCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001121 dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001122
1123 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johansson9e81d332007-10-02 16:27:39 -05001124 sta = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
1125 if (!(sta & PAS_DMA_RXINT_RCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001126 break;
1127 cond_resched();
1128 }
1129
Olof Johansson9e81d332007-10-02 16:27:39 -05001130 if (sta & PAS_DMA_RXINT_RCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001131 dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001132
1133 /* Then, disable the channel. This must be done separately from
1134 * stopping, since you can't disable when active.
1135 */
1136
Olof Johanssona85b9422007-09-15 13:40:59 -07001137 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), 0);
1138 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), 0);
1139 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001140
Olof Johansson72b05b92007-11-28 20:54:28 -06001141 free_irq(mac->tx_irq, mac->tx);
1142 free_irq(mac->rx_irq, mac->rx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001143
1144 /* Free resources */
Olof Johansson72b05b92007-11-28 20:54:28 -06001145 pasemi_mac_free_rx_resources(mac);
1146 pasemi_mac_free_tx_resources(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001147
1148 return 0;
1149}
1150
1151static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
1152{
1153 struct pasemi_mac *mac = netdev_priv(dev);
1154 struct pasemi_mac_txring *txring;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001155 u64 dflags, mactx;
1156 dma_addr_t map[MAX_SKB_FRAGS+1];
1157 unsigned int map_size[MAX_SKB_FRAGS+1];
Olof Johanssonca7e2352007-09-26 16:23:59 -05001158 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001159 int i, nfrags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001160
Olof Johanssondbd62af2007-11-06 22:20:39 -06001161 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_CRC_PAD;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001162
1163 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001164 const unsigned char *nh = skb_network_header(skb);
1165
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001166 switch (ip_hdr(skb)->protocol) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001167 case IPPROTO_TCP:
1168 dflags |= XCT_MACTX_CSUM_TCP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001169 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001170 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001171 break;
1172 case IPPROTO_UDP:
1173 dflags |= XCT_MACTX_CSUM_UDP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001174 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001175 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001176 break;
1177 }
1178 }
1179
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001180 nfrags = skb_shinfo(skb)->nr_frags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001181
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001182 map[0] = pci_map_single(mac->dma_pdev, skb->data, skb_headlen(skb),
1183 PCI_DMA_TODEVICE);
1184 map_size[0] = skb_headlen(skb);
1185 if (dma_mapping_error(map[0]))
1186 goto out_err_nolock;
1187
1188 for (i = 0; i < nfrags; i++) {
1189 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1190
1191 map[i+1] = pci_map_page(mac->dma_pdev, frag->page,
1192 frag->page_offset, frag->size,
1193 PCI_DMA_TODEVICE);
1194 map_size[i+1] = frag->size;
1195 if (dma_mapping_error(map[i+1])) {
1196 nfrags = i;
1197 goto out_err_nolock;
1198 }
1199 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001200
Olof Johansson26fcfa92007-08-22 09:12:59 -05001201 mactx = dflags | XCT_MACTX_LLEN(skb->len);
Olof Johansson26fcfa92007-08-22 09:12:59 -05001202
Olof Johansson72b05b92007-11-28 20:54:28 -06001203 txring = tx_ring(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001204
1205 spin_lock_irqsave(&txring->lock, flags);
1206
Olof Johanssonad5da102007-10-02 16:27:15 -05001207 /* Avoid stepping on the same cache line that the DMA controller
1208 * is currently about to send, so leave at least 8 words available.
1209 * Total free space needed is mactx + fragments + 8
1210 */
1211 if (RING_AVAIL(txring) < nfrags + 10) {
1212 /* no room -- stop the queue and wait for tx intr */
1213 netif_stop_queue(dev);
1214 goto out_err;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001215 }
1216
Olof Johansson72b05b92007-11-28 20:54:28 -06001217 TX_DESC(txring, txring->next_to_fill) = mactx;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001218 txring->next_to_fill++;
Olof Johansson72b05b92007-11-28 20:54:28 -06001219 TX_DESC_INFO(txring, txring->next_to_fill).skb = skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001220 for (i = 0; i <= nfrags; i++) {
Olof Johansson72b05b92007-11-28 20:54:28 -06001221 TX_DESC(txring, txring->next_to_fill+i) =
1222 XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]);
1223 TX_DESC_INFO(txring, txring->next_to_fill+i).dma = map[i];
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001224 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001225
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001226 /* We have to add an even number of 8-byte entries to the ring
1227 * even if the last one is unused. That means always an odd number
1228 * of pointers + one mactx descriptor.
1229 */
1230 if (nfrags & 1)
1231 nfrags++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001232
Olof Johanssonad5da102007-10-02 16:27:15 -05001233 txring->next_to_fill = (txring->next_to_fill + nfrags + 1) &
1234 (TX_RING_SIZE-1);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -05001235
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001236 dev->stats.tx_packets++;
1237 dev->stats.tx_bytes += skb->len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001238
1239 spin_unlock_irqrestore(&txring->lock, flags);
1240
Olof Johansson72b05b92007-11-28 20:54:28 -06001241 write_dma_reg(mac, PAS_DMA_TXCHAN_INCR(txring->chan), (nfrags+2) >> 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001242
1243 return NETDEV_TX_OK;
1244
1245out_err:
1246 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001247out_err_nolock:
1248 while (nfrags--)
1249 pci_unmap_single(mac->dma_pdev, map[nfrags], map_size[nfrags],
1250 PCI_DMA_TODEVICE);
1251
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001252 return NETDEV_TX_BUSY;
1253}
1254
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001255static void pasemi_mac_set_rx_mode(struct net_device *dev)
1256{
1257 struct pasemi_mac *mac = netdev_priv(dev);
1258 unsigned int flags;
1259
Olof Johanssona85b9422007-09-15 13:40:59 -07001260 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001261
1262 /* Set promiscuous */
1263 if (dev->flags & IFF_PROMISC)
1264 flags |= PAS_MAC_CFG_PCFG_PR;
1265 else
1266 flags &= ~PAS_MAC_CFG_PCFG_PR;
1267
Olof Johanssona85b9422007-09-15 13:40:59 -07001268 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001269}
1270
1271
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001272static int pasemi_mac_poll(struct napi_struct *napi, int budget)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001273{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001274 struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
1275 struct net_device *dev = mac->netdev;
1276 int pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001277
Olof Johansson72b05b92007-11-28 20:54:28 -06001278 pasemi_mac_clean_tx(tx_ring(mac));
1279 pkts = pasemi_mac_clean_rx(rx_ring(mac), budget);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001280 if (pkts < budget) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001281 /* all done, no more packets present */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001282 netif_rx_complete(dev, napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001283
Olof Johansson1b0335ea2007-05-08 00:47:26 -05001284 pasemi_mac_restart_rx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001285 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001286 return pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001287}
1288
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001289static void __iomem * __devinit map_onedev(struct pci_dev *p, int index)
1290{
1291 struct device_node *dn;
1292 void __iomem *ret;
1293
1294 dn = pci_device_to_OF_node(p);
1295 if (!dn)
1296 goto fallback;
1297
1298 ret = of_iomap(dn, index);
1299 if (!ret)
1300 goto fallback;
1301
1302 return ret;
1303fallback:
1304 /* This is hardcoded and ugly, but we have some firmware versions
1305 * that don't provide the register space in the device tree. Luckily
1306 * they are at well-known locations so we can just do the math here.
1307 */
1308 return ioremap(0xe0000000 + (p->devfn << 12), 0x2000);
1309}
1310
1311static int __devinit pasemi_mac_map_regs(struct pasemi_mac *mac)
1312{
1313 struct resource res;
1314 struct device_node *dn;
1315 int err;
1316
1317 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
1318 if (!mac->dma_pdev) {
1319 dev_err(&mac->pdev->dev, "Can't find DMA Controller\n");
1320 return -ENODEV;
1321 }
1322
1323 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
1324 if (!mac->iob_pdev) {
1325 dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n");
1326 return -ENODEV;
1327 }
1328
1329 mac->regs = map_onedev(mac->pdev, 0);
1330 mac->dma_regs = map_onedev(mac->dma_pdev, 0);
1331 mac->iob_regs = map_onedev(mac->iob_pdev, 0);
1332
1333 if (!mac->regs || !mac->dma_regs || !mac->iob_regs) {
1334 dev_err(&mac->pdev->dev, "Can't map registers\n");
1335 return -ENODEV;
1336 }
1337
1338 /* The dma status structure is located in the I/O bridge, and
1339 * is cache coherent.
1340 */
1341 if (!dma_status) {
1342 dn = pci_device_to_OF_node(mac->iob_pdev);
1343 if (dn)
1344 err = of_address_to_resource(dn, 1, &res);
1345 if (!dn || err) {
1346 /* Fallback for old firmware */
1347 res.start = 0xfd800000;
1348 res.end = res.start + 0x1000;
1349 }
1350 dma_status = __ioremap(res.start, res.end-res.start, 0);
1351 }
1352
1353 return 0;
1354}
1355
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001356static int __devinit
1357pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1358{
1359 static int index = 0;
1360 struct net_device *dev;
1361 struct pasemi_mac *mac;
1362 int err;
Joe Perches0795af52007-10-03 17:59:30 -07001363 DECLARE_MAC_BUF(mac_buf);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001364
1365 err = pci_enable_device(pdev);
1366 if (err)
1367 return err;
1368
1369 dev = alloc_etherdev(sizeof(struct pasemi_mac));
1370 if (dev == NULL) {
1371 dev_err(&pdev->dev,
1372 "pasemi_mac: Could not allocate ethernet device.\n");
1373 err = -ENOMEM;
1374 goto out_disable_device;
1375 }
1376
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001377 pci_set_drvdata(pdev, dev);
1378 SET_NETDEV_DEV(dev, &pdev->dev);
1379
1380 mac = netdev_priv(dev);
1381
1382 mac->pdev = pdev;
1383 mac->netdev = dev;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001384
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001385 netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
1386
David Woodhouse0581d3f2007-12-03 04:34:32 +00001387 dev->features = NETIF_F_IP_CSUM | NETIF_F_LLTX | NETIF_F_SG;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001388
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001389 /* These should come out of the device tree eventually */
1390 mac->dma_txch = index;
1391 mac->dma_rxch = index;
1392
1393 /* We probe GMAC before XAUI, but the DMA interfaces are
1394 * in XAUI, GMAC order.
1395 */
1396 if (index < 4)
1397 mac->dma_if = index + 2;
1398 else
1399 mac->dma_if = index - 4;
1400 index++;
1401
1402 switch (pdev->device) {
1403 case 0xa005:
1404 mac->type = MAC_TYPE_GMAC;
1405 break;
1406 case 0xa006:
1407 mac->type = MAC_TYPE_XAUI;
1408 break;
1409 default:
1410 err = -ENODEV;
1411 goto out;
1412 }
1413
1414 /* get mac addr from device tree */
1415 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
1416 err = -ENODEV;
1417 goto out;
1418 }
1419 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
1420
1421 dev->open = pasemi_mac_open;
1422 dev->stop = pasemi_mac_close;
1423 dev->hard_start_xmit = pasemi_mac_start_tx;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001424 dev->set_multicast_list = pasemi_mac_set_rx_mode;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001425
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001426 err = pasemi_mac_map_regs(mac);
1427 if (err)
1428 goto out;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001429
Olof Johanssonceb51362007-05-08 00:47:49 -05001430 mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1431
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001432 /* Enable most messages by default */
1433 mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1434
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001435 err = register_netdev(dev);
1436
1437 if (err) {
1438 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1439 err);
1440 goto out;
Olof Johansson69c29d82007-10-02 16:24:51 -05001441 } else if netif_msg_probe(mac)
Olof Johansson72b05b92007-11-28 20:54:28 -06001442 printk(KERN_INFO "%s: PA Semi %s: intf %d, hw addr %s\n",
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001443 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
Olof Johansson72b05b92007-11-28 20:54:28 -06001444 mac->dma_if, print_mac(mac_buf, dev->dev_addr));
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001445
1446 return err;
1447
1448out:
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001449 if (mac->iob_pdev)
1450 pci_dev_put(mac->iob_pdev);
1451 if (mac->dma_pdev)
1452 pci_dev_put(mac->dma_pdev);
1453 if (mac->dma_regs)
1454 iounmap(mac->dma_regs);
1455 if (mac->iob_regs)
1456 iounmap(mac->iob_regs);
1457 if (mac->regs)
1458 iounmap(mac->regs);
1459
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001460 free_netdev(dev);
1461out_disable_device:
1462 pci_disable_device(pdev);
1463 return err;
1464
1465}
1466
1467static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1468{
1469 struct net_device *netdev = pci_get_drvdata(pdev);
1470 struct pasemi_mac *mac;
1471
1472 if (!netdev)
1473 return;
1474
1475 mac = netdev_priv(netdev);
1476
1477 unregister_netdev(netdev);
1478
1479 pci_disable_device(pdev);
1480 pci_dev_put(mac->dma_pdev);
1481 pci_dev_put(mac->iob_pdev);
1482
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001483 iounmap(mac->regs);
1484 iounmap(mac->dma_regs);
1485 iounmap(mac->iob_regs);
1486
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001487 pci_set_drvdata(pdev, NULL);
1488 free_netdev(netdev);
1489}
1490
1491static struct pci_device_id pasemi_mac_pci_tbl[] = {
1492 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1493 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
olof@lixom.netfd178252007-05-12 14:57:36 -05001494 { },
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001495};
1496
1497MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1498
1499static struct pci_driver pasemi_mac_driver = {
1500 .name = "pasemi_mac",
1501 .id_table = pasemi_mac_pci_tbl,
1502 .probe = pasemi_mac_probe,
1503 .remove = __devexit_p(pasemi_mac_remove),
1504};
1505
1506static void __exit pasemi_mac_cleanup_module(void)
1507{
1508 pci_unregister_driver(&pasemi_mac_driver);
1509 __iounmap(dma_status);
1510 dma_status = NULL;
1511}
1512
1513int pasemi_mac_init_module(void)
1514{
1515 return pci_register_driver(&pasemi_mac_driver);
1516}
1517
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001518module_init(pasemi_mac_init_module);
1519module_exit(pasemi_mac_cleanup_module);