Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Combined Ethernet driver for Motorola MPC8xx and MPC82xx. |
| 3 | * |
| 4 | * Copyright (c) 2003 Intracom S.A. |
| 5 | * by Pantelis Antoniou <panto@intracom.gr> |
| 6 | * |
| 7 | * 2005 (c) MontaVista Software, Inc. |
| 8 | * Vitaly Bordug <vbordug@ru.mvista.com> |
| 9 | * |
| 10 | * Heavily based on original FEC driver by Dan Malek <dan@embeddededge.com> |
| 11 | * and modifications by Joakim Tjernlund <joakim.tjernlund@lumentis.se> |
| 12 | * |
| 13 | * This file is licensed under the terms of the GNU General Public License |
| 14 | * version 2. This program is licensed "as is" without any warranty of any |
| 15 | * kind, whether express or implied. |
| 16 | */ |
| 17 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 18 | #include <linux/module.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/types.h> |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 21 | #include <linux/string.h> |
| 22 | #include <linux/ptrace.h> |
| 23 | #include <linux/errno.h> |
| 24 | #include <linux/ioport.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/interrupt.h> |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 27 | #include <linux/init.h> |
| 28 | #include <linux/delay.h> |
| 29 | #include <linux/netdevice.h> |
| 30 | #include <linux/etherdevice.h> |
| 31 | #include <linux/skbuff.h> |
| 32 | #include <linux/spinlock.h> |
| 33 | #include <linux/mii.h> |
| 34 | #include <linux/ethtool.h> |
| 35 | #include <linux/bitops.h> |
| 36 | #include <linux/fs.h> |
Marcelo Tosatti | f7b9996 | 2005-11-09 11:00:16 -0200 | [diff] [blame] | 37 | #include <linux/platform_device.h> |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 38 | #include <linux/phy.h> |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 39 | |
| 40 | #include <linux/vmalloc.h> |
| 41 | #include <asm/pgtable.h> |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 42 | #include <asm/irq.h> |
| 43 | #include <asm/uaccess.h> |
| 44 | |
| 45 | #include "fs_enet.h" |
| 46 | |
| 47 | /*************************************************/ |
| 48 | |
| 49 | static char version[] __devinitdata = |
| 50 | DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")" "\n"; |
| 51 | |
| 52 | MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>"); |
| 53 | MODULE_DESCRIPTION("Freescale Ethernet Driver"); |
| 54 | MODULE_LICENSE("GPL"); |
| 55 | MODULE_VERSION(DRV_MODULE_VERSION); |
| 56 | |
Rusty Russell | 8d3b33f | 2006-03-25 03:07:05 -0800 | [diff] [blame] | 57 | int fs_enet_debug = -1; /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */ |
| 58 | module_param(fs_enet_debug, int, 0); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 59 | MODULE_PARM_DESC(fs_enet_debug, |
| 60 | "Freescale bitmapped debugging message enable value"); |
| 61 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 62 | |
| 63 | static void fs_set_multicast_list(struct net_device *dev) |
| 64 | { |
| 65 | struct fs_enet_private *fep = netdev_priv(dev); |
| 66 | |
| 67 | (*fep->ops->set_multicast_list)(dev); |
| 68 | } |
| 69 | |
| 70 | /* NAPI receive function */ |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 71 | static int fs_enet_rx_napi(struct napi_struct *napi, int budget) |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 72 | { |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 73 | struct fs_enet_private *fep = container_of(napi, struct fs_enet_private, napi); |
| 74 | struct net_device *dev = to_net_dev(fep->dev); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 75 | const struct fs_platform_info *fpi = fep->fpi; |
| 76 | cbd_t *bdp; |
| 77 | struct sk_buff *skb, *skbn, *skbt; |
| 78 | int received = 0; |
| 79 | u16 pkt_len, sc; |
| 80 | int curidx; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 81 | |
| 82 | if (!netif_running(dev)) |
| 83 | return 0; |
| 84 | |
| 85 | /* |
| 86 | * First, grab all of the stats for the incoming packet. |
| 87 | * These get messed up if we get called due to a busy condition. |
| 88 | */ |
| 89 | bdp = fep->cur_rx; |
| 90 | |
| 91 | /* clear RX status bits for napi*/ |
| 92 | (*fep->ops->napi_clear_rx_event)(dev); |
| 93 | |
| 94 | while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 95 | curidx = bdp - fep->rx_bd_base; |
| 96 | |
| 97 | /* |
| 98 | * Since we have allocated space to hold a complete frame, |
| 99 | * the last indicator should be set. |
| 100 | */ |
| 101 | if ((sc & BD_ENET_RX_LAST) == 0) |
| 102 | printk(KERN_WARNING DRV_MODULE_NAME |
| 103 | ": %s rcv is not +last\n", |
| 104 | dev->name); |
| 105 | |
| 106 | /* |
| 107 | * Check for errors. |
| 108 | */ |
| 109 | if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL | |
| 110 | BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) { |
| 111 | fep->stats.rx_errors++; |
| 112 | /* Frame too long or too short. */ |
| 113 | if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH)) |
| 114 | fep->stats.rx_length_errors++; |
| 115 | /* Frame alignment */ |
| 116 | if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL)) |
| 117 | fep->stats.rx_frame_errors++; |
| 118 | /* CRC Error */ |
| 119 | if (sc & BD_ENET_RX_CR) |
| 120 | fep->stats.rx_crc_errors++; |
| 121 | /* FIFO overrun */ |
| 122 | if (sc & BD_ENET_RX_OV) |
| 123 | fep->stats.rx_crc_errors++; |
| 124 | |
| 125 | skb = fep->rx_skbuff[curidx]; |
| 126 | |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 127 | dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 128 | L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), |
| 129 | DMA_FROM_DEVICE); |
| 130 | |
| 131 | skbn = skb; |
| 132 | |
| 133 | } else { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 134 | skb = fep->rx_skbuff[curidx]; |
| 135 | |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 136 | dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 137 | L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), |
| 138 | DMA_FROM_DEVICE); |
| 139 | |
| 140 | /* |
| 141 | * Process the incoming frame. |
| 142 | */ |
| 143 | fep->stats.rx_packets++; |
| 144 | pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */ |
| 145 | fep->stats.rx_bytes += pkt_len + 4; |
| 146 | |
| 147 | if (pkt_len <= fpi->rx_copybreak) { |
| 148 | /* +2 to make IP header L1 cache aligned */ |
| 149 | skbn = dev_alloc_skb(pkt_len + 2); |
| 150 | if (skbn != NULL) { |
| 151 | skb_reserve(skbn, 2); /* align IP header */ |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 152 | skb_copy_from_linear_data(skb, |
| 153 | skbn->data, pkt_len); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 154 | /* swap */ |
| 155 | skbt = skb; |
| 156 | skb = skbn; |
| 157 | skbn = skbt; |
| 158 | } |
| 159 | } else |
| 160 | skbn = dev_alloc_skb(ENET_RX_FRSIZE); |
| 161 | |
| 162 | if (skbn != NULL) { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 163 | skb_put(skb, pkt_len); /* Make room */ |
| 164 | skb->protocol = eth_type_trans(skb, dev); |
| 165 | received++; |
| 166 | netif_receive_skb(skb); |
| 167 | } else { |
| 168 | printk(KERN_WARNING DRV_MODULE_NAME |
| 169 | ": %s Memory squeeze, dropping packet.\n", |
| 170 | dev->name); |
| 171 | fep->stats.rx_dropped++; |
| 172 | skbn = skb; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | fep->rx_skbuff[curidx] = skbn; |
| 177 | CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data, |
| 178 | L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), |
| 179 | DMA_FROM_DEVICE)); |
| 180 | CBDW_DATLEN(bdp, 0); |
| 181 | CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY); |
| 182 | |
| 183 | /* |
| 184 | * Update BD pointer to next entry. |
| 185 | */ |
| 186 | if ((sc & BD_ENET_RX_WRAP) == 0) |
| 187 | bdp++; |
| 188 | else |
| 189 | bdp = fep->rx_bd_base; |
| 190 | |
| 191 | (*fep->ops->rx_bd_done)(dev); |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 192 | |
| 193 | if (received >= budget) |
| 194 | break; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | fep->cur_rx = bdp; |
| 198 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 199 | if (received >= budget) { |
| 200 | /* done */ |
| 201 | netif_rx_complete(dev, napi); |
| 202 | (*fep->ops->napi_enable_rx)(dev); |
| 203 | } |
| 204 | return received; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /* non NAPI receive function */ |
| 208 | static int fs_enet_rx_non_napi(struct net_device *dev) |
| 209 | { |
| 210 | struct fs_enet_private *fep = netdev_priv(dev); |
| 211 | const struct fs_platform_info *fpi = fep->fpi; |
| 212 | cbd_t *bdp; |
| 213 | struct sk_buff *skb, *skbn, *skbt; |
| 214 | int received = 0; |
| 215 | u16 pkt_len, sc; |
| 216 | int curidx; |
| 217 | /* |
| 218 | * First, grab all of the stats for the incoming packet. |
| 219 | * These get messed up if we get called due to a busy condition. |
| 220 | */ |
| 221 | bdp = fep->cur_rx; |
| 222 | |
| 223 | while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) { |
| 224 | |
| 225 | curidx = bdp - fep->rx_bd_base; |
| 226 | |
| 227 | /* |
| 228 | * Since we have allocated space to hold a complete frame, |
| 229 | * the last indicator should be set. |
| 230 | */ |
| 231 | if ((sc & BD_ENET_RX_LAST) == 0) |
| 232 | printk(KERN_WARNING DRV_MODULE_NAME |
| 233 | ": %s rcv is not +last\n", |
| 234 | dev->name); |
| 235 | |
| 236 | /* |
| 237 | * Check for errors. |
| 238 | */ |
| 239 | if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL | |
| 240 | BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) { |
| 241 | fep->stats.rx_errors++; |
| 242 | /* Frame too long or too short. */ |
| 243 | if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH)) |
| 244 | fep->stats.rx_length_errors++; |
| 245 | /* Frame alignment */ |
| 246 | if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL)) |
| 247 | fep->stats.rx_frame_errors++; |
| 248 | /* CRC Error */ |
| 249 | if (sc & BD_ENET_RX_CR) |
| 250 | fep->stats.rx_crc_errors++; |
| 251 | /* FIFO overrun */ |
| 252 | if (sc & BD_ENET_RX_OV) |
| 253 | fep->stats.rx_crc_errors++; |
| 254 | |
| 255 | skb = fep->rx_skbuff[curidx]; |
| 256 | |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 257 | dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 258 | L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), |
| 259 | DMA_FROM_DEVICE); |
| 260 | |
| 261 | skbn = skb; |
| 262 | |
| 263 | } else { |
| 264 | |
| 265 | skb = fep->rx_skbuff[curidx]; |
| 266 | |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 267 | dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 268 | L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), |
| 269 | DMA_FROM_DEVICE); |
| 270 | |
| 271 | /* |
| 272 | * Process the incoming frame. |
| 273 | */ |
| 274 | fep->stats.rx_packets++; |
| 275 | pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */ |
| 276 | fep->stats.rx_bytes += pkt_len + 4; |
| 277 | |
| 278 | if (pkt_len <= fpi->rx_copybreak) { |
| 279 | /* +2 to make IP header L1 cache aligned */ |
| 280 | skbn = dev_alloc_skb(pkt_len + 2); |
| 281 | if (skbn != NULL) { |
| 282 | skb_reserve(skbn, 2); /* align IP header */ |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 283 | skb_copy_from_linear_data(skb, |
| 284 | skbn->data, pkt_len); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 285 | /* swap */ |
| 286 | skbt = skb; |
| 287 | skb = skbn; |
| 288 | skbn = skbt; |
| 289 | } |
| 290 | } else |
| 291 | skbn = dev_alloc_skb(ENET_RX_FRSIZE); |
| 292 | |
| 293 | if (skbn != NULL) { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 294 | skb_put(skb, pkt_len); /* Make room */ |
| 295 | skb->protocol = eth_type_trans(skb, dev); |
| 296 | received++; |
| 297 | netif_rx(skb); |
| 298 | } else { |
| 299 | printk(KERN_WARNING DRV_MODULE_NAME |
| 300 | ": %s Memory squeeze, dropping packet.\n", |
| 301 | dev->name); |
| 302 | fep->stats.rx_dropped++; |
| 303 | skbn = skb; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | fep->rx_skbuff[curidx] = skbn; |
| 308 | CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data, |
| 309 | L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), |
| 310 | DMA_FROM_DEVICE)); |
| 311 | CBDW_DATLEN(bdp, 0); |
| 312 | CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY); |
| 313 | |
| 314 | /* |
| 315 | * Update BD pointer to next entry. |
| 316 | */ |
| 317 | if ((sc & BD_ENET_RX_WRAP) == 0) |
| 318 | bdp++; |
| 319 | else |
| 320 | bdp = fep->rx_bd_base; |
| 321 | |
| 322 | (*fep->ops->rx_bd_done)(dev); |
| 323 | } |
| 324 | |
| 325 | fep->cur_rx = bdp; |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | static void fs_enet_tx(struct net_device *dev) |
| 331 | { |
| 332 | struct fs_enet_private *fep = netdev_priv(dev); |
| 333 | cbd_t *bdp; |
| 334 | struct sk_buff *skb; |
| 335 | int dirtyidx, do_wake, do_restart; |
| 336 | u16 sc; |
| 337 | |
Vitaly Bordug | aa90f50 | 2007-09-18 20:05:27 +0400 | [diff] [blame^] | 338 | spin_lock(&fep->tx_lock); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 339 | bdp = fep->dirty_tx; |
| 340 | |
| 341 | do_wake = do_restart = 0; |
| 342 | while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) { |
| 343 | |
| 344 | dirtyidx = bdp - fep->tx_bd_base; |
| 345 | |
| 346 | if (fep->tx_free == fep->tx_ring) |
| 347 | break; |
| 348 | |
| 349 | skb = fep->tx_skbuff[dirtyidx]; |
| 350 | |
| 351 | /* |
| 352 | * Check for errors. |
| 353 | */ |
| 354 | if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC | |
| 355 | BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) { |
| 356 | |
| 357 | if (sc & BD_ENET_TX_HB) /* No heartbeat */ |
| 358 | fep->stats.tx_heartbeat_errors++; |
| 359 | if (sc & BD_ENET_TX_LC) /* Late collision */ |
| 360 | fep->stats.tx_window_errors++; |
| 361 | if (sc & BD_ENET_TX_RL) /* Retrans limit */ |
| 362 | fep->stats.tx_aborted_errors++; |
| 363 | if (sc & BD_ENET_TX_UN) /* Underrun */ |
| 364 | fep->stats.tx_fifo_errors++; |
| 365 | if (sc & BD_ENET_TX_CSL) /* Carrier lost */ |
| 366 | fep->stats.tx_carrier_errors++; |
| 367 | |
| 368 | if (sc & (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) { |
| 369 | fep->stats.tx_errors++; |
| 370 | do_restart = 1; |
| 371 | } |
| 372 | } else |
| 373 | fep->stats.tx_packets++; |
| 374 | |
| 375 | if (sc & BD_ENET_TX_READY) |
| 376 | printk(KERN_WARNING DRV_MODULE_NAME |
| 377 | ": %s HEY! Enet xmit interrupt and TX_READY.\n", |
| 378 | dev->name); |
| 379 | |
| 380 | /* |
| 381 | * Deferred means some collisions occurred during transmit, |
| 382 | * but we eventually sent the packet OK. |
| 383 | */ |
| 384 | if (sc & BD_ENET_TX_DEF) |
| 385 | fep->stats.collisions++; |
| 386 | |
| 387 | /* unmap */ |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 388 | dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), |
| 389 | skb->len, DMA_TO_DEVICE); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 390 | |
| 391 | /* |
| 392 | * Free the sk buffer associated with this last transmit. |
| 393 | */ |
| 394 | dev_kfree_skb_irq(skb); |
| 395 | fep->tx_skbuff[dirtyidx] = NULL; |
| 396 | |
| 397 | /* |
| 398 | * Update pointer to next buffer descriptor to be transmitted. |
| 399 | */ |
| 400 | if ((sc & BD_ENET_TX_WRAP) == 0) |
| 401 | bdp++; |
| 402 | else |
| 403 | bdp = fep->tx_bd_base; |
| 404 | |
| 405 | /* |
| 406 | * Since we have freed up a buffer, the ring is no longer |
| 407 | * full. |
| 408 | */ |
| 409 | if (!fep->tx_free++) |
| 410 | do_wake = 1; |
| 411 | } |
| 412 | |
| 413 | fep->dirty_tx = bdp; |
| 414 | |
| 415 | if (do_restart) |
| 416 | (*fep->ops->tx_restart)(dev); |
| 417 | |
Vitaly Bordug | aa90f50 | 2007-09-18 20:05:27 +0400 | [diff] [blame^] | 418 | spin_unlock(&fep->tx_lock); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 419 | |
| 420 | if (do_wake) |
| 421 | netif_wake_queue(dev); |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | * The interrupt handler. |
| 426 | * This is called from the MPC core interrupt. |
| 427 | */ |
| 428 | static irqreturn_t |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 429 | fs_enet_interrupt(int irq, void *dev_id) |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 430 | { |
| 431 | struct net_device *dev = dev_id; |
| 432 | struct fs_enet_private *fep; |
| 433 | const struct fs_platform_info *fpi; |
| 434 | u32 int_events; |
| 435 | u32 int_clr_events; |
| 436 | int nr, napi_ok; |
| 437 | int handled; |
| 438 | |
| 439 | fep = netdev_priv(dev); |
| 440 | fpi = fep->fpi; |
| 441 | |
| 442 | nr = 0; |
| 443 | while ((int_events = (*fep->ops->get_int_events)(dev)) != 0) { |
| 444 | |
| 445 | nr++; |
| 446 | |
| 447 | int_clr_events = int_events; |
| 448 | if (fpi->use_napi) |
| 449 | int_clr_events &= ~fep->ev_napi_rx; |
| 450 | |
| 451 | (*fep->ops->clear_int_events)(dev, int_clr_events); |
| 452 | |
| 453 | if (int_events & fep->ev_err) |
| 454 | (*fep->ops->ev_error)(dev, int_events); |
| 455 | |
| 456 | if (int_events & fep->ev_rx) { |
| 457 | if (!fpi->use_napi) |
| 458 | fs_enet_rx_non_napi(dev); |
| 459 | else { |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 460 | napi_ok = napi_schedule_prep(&fep->napi); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 461 | |
| 462 | (*fep->ops->napi_disable_rx)(dev); |
| 463 | (*fep->ops->clear_int_events)(dev, fep->ev_napi_rx); |
| 464 | |
| 465 | /* NOTE: it is possible for FCCs in NAPI mode */ |
| 466 | /* to submit a spurious interrupt while in poll */ |
| 467 | if (napi_ok) |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 468 | __netif_rx_schedule(dev, &fep->napi); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
| 472 | if (int_events & fep->ev_tx) |
| 473 | fs_enet_tx(dev); |
| 474 | } |
| 475 | |
| 476 | handled = nr > 0; |
| 477 | return IRQ_RETVAL(handled); |
| 478 | } |
| 479 | |
| 480 | void fs_init_bds(struct net_device *dev) |
| 481 | { |
| 482 | struct fs_enet_private *fep = netdev_priv(dev); |
| 483 | cbd_t *bdp; |
| 484 | struct sk_buff *skb; |
| 485 | int i; |
| 486 | |
| 487 | fs_cleanup_bds(dev); |
| 488 | |
| 489 | fep->dirty_tx = fep->cur_tx = fep->tx_bd_base; |
| 490 | fep->tx_free = fep->tx_ring; |
| 491 | fep->cur_rx = fep->rx_bd_base; |
| 492 | |
| 493 | /* |
| 494 | * Initialize the receive buffer descriptors. |
| 495 | */ |
| 496 | for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) { |
| 497 | skb = dev_alloc_skb(ENET_RX_FRSIZE); |
| 498 | if (skb == NULL) { |
| 499 | printk(KERN_WARNING DRV_MODULE_NAME |
| 500 | ": %s Memory squeeze, unable to allocate skb\n", |
| 501 | dev->name); |
| 502 | break; |
| 503 | } |
| 504 | fep->rx_skbuff[i] = skb; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 505 | CBDW_BUFADDR(bdp, |
| 506 | dma_map_single(fep->dev, skb->data, |
| 507 | L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), |
| 508 | DMA_FROM_DEVICE)); |
| 509 | CBDW_DATLEN(bdp, 0); /* zero */ |
| 510 | CBDW_SC(bdp, BD_ENET_RX_EMPTY | |
| 511 | ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP)); |
| 512 | } |
| 513 | /* |
| 514 | * if we failed, fillup remainder |
| 515 | */ |
| 516 | for (; i < fep->rx_ring; i++, bdp++) { |
| 517 | fep->rx_skbuff[i] = NULL; |
| 518 | CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP); |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | * ...and the same for transmit. |
| 523 | */ |
| 524 | for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) { |
| 525 | fep->tx_skbuff[i] = NULL; |
| 526 | CBDW_BUFADDR(bdp, 0); |
| 527 | CBDW_DATLEN(bdp, 0); |
| 528 | CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | void fs_cleanup_bds(struct net_device *dev) |
| 533 | { |
| 534 | struct fs_enet_private *fep = netdev_priv(dev); |
| 535 | struct sk_buff *skb; |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 536 | cbd_t *bdp; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 537 | int i; |
| 538 | |
| 539 | /* |
| 540 | * Reset SKB transmit buffers. |
| 541 | */ |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 542 | for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 543 | if ((skb = fep->tx_skbuff[i]) == NULL) |
| 544 | continue; |
| 545 | |
| 546 | /* unmap */ |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 547 | dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), |
| 548 | skb->len, DMA_TO_DEVICE); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 549 | |
| 550 | fep->tx_skbuff[i] = NULL; |
| 551 | dev_kfree_skb(skb); |
| 552 | } |
| 553 | |
| 554 | /* |
| 555 | * Reset SKB receive buffers |
| 556 | */ |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 557 | for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 558 | if ((skb = fep->rx_skbuff[i]) == NULL) |
| 559 | continue; |
| 560 | |
| 561 | /* unmap */ |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 562 | dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 563 | L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), |
| 564 | DMA_FROM_DEVICE); |
| 565 | |
| 566 | fep->rx_skbuff[i] = NULL; |
| 567 | |
| 568 | dev_kfree_skb(skb); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | /**********************************************************************************/ |
| 573 | |
| 574 | static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 575 | { |
| 576 | struct fs_enet_private *fep = netdev_priv(dev); |
| 577 | cbd_t *bdp; |
| 578 | int curidx; |
| 579 | u16 sc; |
| 580 | unsigned long flags; |
| 581 | |
| 582 | spin_lock_irqsave(&fep->tx_lock, flags); |
| 583 | |
| 584 | /* |
| 585 | * Fill in a Tx ring entry |
| 586 | */ |
| 587 | bdp = fep->cur_tx; |
| 588 | |
| 589 | if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) { |
| 590 | netif_stop_queue(dev); |
| 591 | spin_unlock_irqrestore(&fep->tx_lock, flags); |
| 592 | |
| 593 | /* |
| 594 | * Ooops. All transmit buffers are full. Bail out. |
| 595 | * This should not happen, since the tx queue should be stopped. |
| 596 | */ |
| 597 | printk(KERN_WARNING DRV_MODULE_NAME |
| 598 | ": %s tx queue full!.\n", dev->name); |
| 599 | return NETDEV_TX_BUSY; |
| 600 | } |
| 601 | |
| 602 | curidx = bdp - fep->tx_bd_base; |
| 603 | /* |
| 604 | * Clear all of the status flags. |
| 605 | */ |
| 606 | CBDC_SC(bdp, BD_ENET_TX_STATS); |
| 607 | |
| 608 | /* |
| 609 | * Save skb pointer. |
| 610 | */ |
| 611 | fep->tx_skbuff[curidx] = skb; |
| 612 | |
| 613 | fep->stats.tx_bytes += skb->len; |
| 614 | |
| 615 | /* |
| 616 | * Push the data cache so the CPM does not get stale memory data. |
| 617 | */ |
| 618 | CBDW_BUFADDR(bdp, dma_map_single(fep->dev, |
| 619 | skb->data, skb->len, DMA_TO_DEVICE)); |
| 620 | CBDW_DATLEN(bdp, skb->len); |
| 621 | |
| 622 | dev->trans_start = jiffies; |
| 623 | |
| 624 | /* |
| 625 | * If this was the last BD in the ring, start at the beginning again. |
| 626 | */ |
| 627 | if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0) |
| 628 | fep->cur_tx++; |
| 629 | else |
| 630 | fep->cur_tx = fep->tx_bd_base; |
| 631 | |
| 632 | if (!--fep->tx_free) |
| 633 | netif_stop_queue(dev); |
| 634 | |
| 635 | /* Trigger transmission start */ |
| 636 | sc = BD_ENET_TX_READY | BD_ENET_TX_INTR | |
| 637 | BD_ENET_TX_LAST | BD_ENET_TX_TC; |
| 638 | |
| 639 | /* note that while FEC does not have this bit |
| 640 | * it marks it as available for software use |
| 641 | * yay for hw reuse :) */ |
| 642 | if (skb->len <= 60) |
| 643 | sc |= BD_ENET_TX_PAD; |
| 644 | CBDS_SC(bdp, sc); |
| 645 | |
| 646 | (*fep->ops->tx_kickstart)(dev); |
| 647 | |
| 648 | spin_unlock_irqrestore(&fep->tx_lock, flags); |
| 649 | |
| 650 | return NETDEV_TX_OK; |
| 651 | } |
| 652 | |
| 653 | static int fs_request_irq(struct net_device *dev, int irq, const char *name, |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 654 | irq_handler_t irqf) |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 655 | { |
| 656 | struct fs_enet_private *fep = netdev_priv(dev); |
| 657 | |
| 658 | (*fep->ops->pre_request_irq)(dev, irq); |
Thomas Gleixner | 1fb9df5 | 2006-07-01 19:29:39 -0700 | [diff] [blame] | 659 | return request_irq(irq, irqf, IRQF_SHARED, name, dev); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | static void fs_free_irq(struct net_device *dev, int irq) |
| 663 | { |
| 664 | struct fs_enet_private *fep = netdev_priv(dev); |
| 665 | |
| 666 | free_irq(irq, dev); |
| 667 | (*fep->ops->post_free_irq)(dev, irq); |
| 668 | } |
| 669 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 670 | static void fs_timeout(struct net_device *dev) |
| 671 | { |
| 672 | struct fs_enet_private *fep = netdev_priv(dev); |
| 673 | unsigned long flags; |
| 674 | int wake = 0; |
| 675 | |
| 676 | fep->stats.tx_errors++; |
| 677 | |
| 678 | spin_lock_irqsave(&fep->lock, flags); |
| 679 | |
| 680 | if (dev->flags & IFF_UP) { |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 681 | phy_stop(fep->phydev); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 682 | (*fep->ops->stop)(dev); |
| 683 | (*fep->ops->restart)(dev); |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 684 | phy_start(fep->phydev); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 685 | } |
| 686 | |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 687 | phy_start(fep->phydev); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 688 | wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY); |
| 689 | spin_unlock_irqrestore(&fep->lock, flags); |
| 690 | |
| 691 | if (wake) |
| 692 | netif_wake_queue(dev); |
| 693 | } |
| 694 | |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 695 | /*----------------------------------------------------------------------------- |
| 696 | * generic link-change handler - should be sufficient for most cases |
| 697 | *-----------------------------------------------------------------------------*/ |
| 698 | static void generic_adjust_link(struct net_device *dev) |
| 699 | { |
| 700 | struct fs_enet_private *fep = netdev_priv(dev); |
| 701 | struct phy_device *phydev = fep->phydev; |
| 702 | int new_state = 0; |
| 703 | |
| 704 | if (phydev->link) { |
| 705 | |
| 706 | /* adjust to duplex mode */ |
| 707 | if (phydev->duplex != fep->oldduplex){ |
| 708 | new_state = 1; |
| 709 | fep->oldduplex = phydev->duplex; |
| 710 | } |
| 711 | |
| 712 | if (phydev->speed != fep->oldspeed) { |
| 713 | new_state = 1; |
| 714 | fep->oldspeed = phydev->speed; |
| 715 | } |
| 716 | |
| 717 | if (!fep->oldlink) { |
| 718 | new_state = 1; |
| 719 | fep->oldlink = 1; |
| 720 | netif_schedule(dev); |
| 721 | netif_carrier_on(dev); |
| 722 | netif_start_queue(dev); |
| 723 | } |
| 724 | |
| 725 | if (new_state) |
| 726 | fep->ops->restart(dev); |
| 727 | |
| 728 | } else if (fep->oldlink) { |
| 729 | new_state = 1; |
| 730 | fep->oldlink = 0; |
| 731 | fep->oldspeed = 0; |
| 732 | fep->oldduplex = -1; |
| 733 | netif_carrier_off(dev); |
| 734 | netif_stop_queue(dev); |
| 735 | } |
| 736 | |
| 737 | if (new_state && netif_msg_link(fep)) |
| 738 | phy_print_status(phydev); |
| 739 | } |
| 740 | |
| 741 | |
| 742 | static void fs_adjust_link(struct net_device *dev) |
| 743 | { |
| 744 | struct fs_enet_private *fep = netdev_priv(dev); |
| 745 | unsigned long flags; |
| 746 | |
| 747 | spin_lock_irqsave(&fep->lock, flags); |
| 748 | |
| 749 | if(fep->ops->adjust_link) |
| 750 | fep->ops->adjust_link(dev); |
| 751 | else |
| 752 | generic_adjust_link(dev); |
| 753 | |
| 754 | spin_unlock_irqrestore(&fep->lock, flags); |
| 755 | } |
| 756 | |
| 757 | static int fs_init_phy(struct net_device *dev) |
| 758 | { |
| 759 | struct fs_enet_private *fep = netdev_priv(dev); |
| 760 | struct phy_device *phydev; |
| 761 | |
| 762 | fep->oldlink = 0; |
| 763 | fep->oldspeed = 0; |
| 764 | fep->oldduplex = -1; |
| 765 | if(fep->fpi->bus_id) |
Andy Fleming | e8a2b6a | 2006-12-01 12:01:06 -0600 | [diff] [blame] | 766 | phydev = phy_connect(dev, fep->fpi->bus_id, &fs_adjust_link, 0, |
| 767 | PHY_INTERFACE_MODE_MII); |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 768 | else { |
| 769 | printk("No phy bus ID specified in BSP code\n"); |
| 770 | return -EINVAL; |
| 771 | } |
| 772 | if (IS_ERR(phydev)) { |
| 773 | printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name); |
| 774 | return PTR_ERR(phydev); |
| 775 | } |
| 776 | |
| 777 | fep->phydev = phydev; |
| 778 | |
| 779 | return 0; |
| 780 | } |
| 781 | |
| 782 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 783 | static int fs_enet_open(struct net_device *dev) |
| 784 | { |
| 785 | struct fs_enet_private *fep = netdev_priv(dev); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 786 | int r; |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 787 | int err; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 788 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 789 | napi_enable(&fep->napi); |
| 790 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 791 | /* Install our interrupt handler. */ |
| 792 | r = fs_request_irq(dev, fep->interrupt, "fs_enet-mac", fs_enet_interrupt); |
| 793 | if (r != 0) { |
| 794 | printk(KERN_ERR DRV_MODULE_NAME |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 795 | ": %s Could not allocate FS_ENET IRQ!", dev->name); |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 796 | napi_disable(&fep->napi); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 797 | return -EINVAL; |
| 798 | } |
| 799 | |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 800 | err = fs_init_phy(dev); |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 801 | if(err) { |
| 802 | napi_disable(&fep->napi); |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 803 | return err; |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 804 | } |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 805 | phy_start(fep->phydev); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 806 | |
| 807 | return 0; |
| 808 | } |
| 809 | |
| 810 | static int fs_enet_close(struct net_device *dev) |
| 811 | { |
| 812 | struct fs_enet_private *fep = netdev_priv(dev); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 813 | unsigned long flags; |
| 814 | |
| 815 | netif_stop_queue(dev); |
| 816 | netif_carrier_off(dev); |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 817 | napi_disable(&fep->napi); |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 818 | phy_stop(fep->phydev); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 819 | |
| 820 | spin_lock_irqsave(&fep->lock, flags); |
Vitaly Bordug | aa90f50 | 2007-09-18 20:05:27 +0400 | [diff] [blame^] | 821 | spin_lock(&fep->tx_lock); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 822 | (*fep->ops->stop)(dev); |
Vitaly Bordug | aa90f50 | 2007-09-18 20:05:27 +0400 | [diff] [blame^] | 823 | spin_unlock(&fep->tx_lock); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 824 | spin_unlock_irqrestore(&fep->lock, flags); |
| 825 | |
| 826 | /* release any irqs */ |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 827 | phy_disconnect(fep->phydev); |
| 828 | fep->phydev = NULL; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 829 | fs_free_irq(dev, fep->interrupt); |
| 830 | |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | static struct net_device_stats *fs_enet_get_stats(struct net_device *dev) |
| 835 | { |
| 836 | struct fs_enet_private *fep = netdev_priv(dev); |
| 837 | return &fep->stats; |
| 838 | } |
| 839 | |
| 840 | /*************************************************************************/ |
| 841 | |
| 842 | static void fs_get_drvinfo(struct net_device *dev, |
| 843 | struct ethtool_drvinfo *info) |
| 844 | { |
| 845 | strcpy(info->driver, DRV_MODULE_NAME); |
| 846 | strcpy(info->version, DRV_MODULE_VERSION); |
| 847 | } |
| 848 | |
| 849 | static int fs_get_regs_len(struct net_device *dev) |
| 850 | { |
| 851 | struct fs_enet_private *fep = netdev_priv(dev); |
| 852 | |
| 853 | return (*fep->ops->get_regs_len)(dev); |
| 854 | } |
| 855 | |
| 856 | static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs, |
| 857 | void *p) |
| 858 | { |
| 859 | struct fs_enet_private *fep = netdev_priv(dev); |
| 860 | unsigned long flags; |
| 861 | int r, len; |
| 862 | |
| 863 | len = regs->len; |
| 864 | |
| 865 | spin_lock_irqsave(&fep->lock, flags); |
| 866 | r = (*fep->ops->get_regs)(dev, p, &len); |
| 867 | spin_unlock_irqrestore(&fep->lock, flags); |
| 868 | |
| 869 | if (r == 0) |
| 870 | regs->version = 0; |
| 871 | } |
| 872 | |
| 873 | static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 874 | { |
| 875 | struct fs_enet_private *fep = netdev_priv(dev); |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 876 | return phy_ethtool_gset(fep->phydev, cmd); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 880 | { |
| 881 | struct fs_enet_private *fep = netdev_priv(dev); |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 882 | phy_ethtool_sset(fep->phydev, cmd); |
| 883 | return 0; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | static int fs_nway_reset(struct net_device *dev) |
| 887 | { |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 888 | return 0; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | static u32 fs_get_msglevel(struct net_device *dev) |
| 892 | { |
| 893 | struct fs_enet_private *fep = netdev_priv(dev); |
| 894 | return fep->msg_enable; |
| 895 | } |
| 896 | |
| 897 | static void fs_set_msglevel(struct net_device *dev, u32 value) |
| 898 | { |
| 899 | struct fs_enet_private *fep = netdev_priv(dev); |
| 900 | fep->msg_enable = value; |
| 901 | } |
| 902 | |
Jeff Garzik | 7282d49 | 2006-09-13 14:30:00 -0400 | [diff] [blame] | 903 | static const struct ethtool_ops fs_ethtool_ops = { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 904 | .get_drvinfo = fs_get_drvinfo, |
| 905 | .get_regs_len = fs_get_regs_len, |
| 906 | .get_settings = fs_get_settings, |
| 907 | .set_settings = fs_set_settings, |
| 908 | .nway_reset = fs_nway_reset, |
| 909 | .get_link = ethtool_op_get_link, |
| 910 | .get_msglevel = fs_get_msglevel, |
| 911 | .set_msglevel = fs_set_msglevel, |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 912 | .set_tx_csum = ethtool_op_set_tx_csum, /* local! */ |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 913 | .set_sg = ethtool_op_set_sg, |
| 914 | .get_regs = fs_get_regs, |
| 915 | }; |
| 916 | |
| 917 | static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) |
| 918 | { |
| 919 | struct fs_enet_private *fep = netdev_priv(dev); |
| 920 | struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&rq->ifr_data; |
| 921 | unsigned long flags; |
| 922 | int rc; |
| 923 | |
| 924 | if (!netif_running(dev)) |
| 925 | return -EINVAL; |
| 926 | |
| 927 | spin_lock_irqsave(&fep->lock, flags); |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 928 | rc = phy_mii_ioctl(fep->phydev, mii, cmd); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 929 | spin_unlock_irqrestore(&fep->lock, flags); |
| 930 | return rc; |
| 931 | } |
| 932 | |
| 933 | extern int fs_mii_connect(struct net_device *dev); |
| 934 | extern void fs_mii_disconnect(struct net_device *dev); |
| 935 | |
| 936 | static struct net_device *fs_init_instance(struct device *dev, |
Vitaly Bordug | 611a15a | 2006-09-21 22:38:05 +0400 | [diff] [blame] | 937 | struct fs_platform_info *fpi) |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 938 | { |
| 939 | struct net_device *ndev = NULL; |
| 940 | struct fs_enet_private *fep = NULL; |
| 941 | int privsize, i, r, err = 0, registered = 0; |
| 942 | |
Vitaly Bordug | 611a15a | 2006-09-21 22:38:05 +0400 | [diff] [blame] | 943 | fpi->fs_no = fs_get_id(fpi); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 944 | /* guard */ |
| 945 | if ((unsigned int)fpi->fs_no >= FS_MAX_INDEX) |
| 946 | return ERR_PTR(-EINVAL); |
| 947 | |
| 948 | privsize = sizeof(*fep) + (sizeof(struct sk_buff **) * |
| 949 | (fpi->rx_ring + fpi->tx_ring)); |
| 950 | |
| 951 | ndev = alloc_etherdev(privsize); |
| 952 | if (!ndev) { |
| 953 | err = -ENOMEM; |
| 954 | goto err; |
| 955 | } |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 956 | |
| 957 | fep = netdev_priv(ndev); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 958 | |
| 959 | fep->dev = dev; |
| 960 | dev_set_drvdata(dev, ndev); |
| 961 | fep->fpi = fpi; |
| 962 | if (fpi->init_ioports) |
Vitaly Bordug | d3465c9 | 2006-09-21 22:38:05 +0400 | [diff] [blame] | 963 | fpi->init_ioports((struct fs_platform_info *)fpi); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 964 | |
| 965 | #ifdef CONFIG_FS_ENET_HAS_FEC |
| 966 | if (fs_get_fec_index(fpi->fs_no) >= 0) |
| 967 | fep->ops = &fs_fec_ops; |
| 968 | #endif |
| 969 | |
| 970 | #ifdef CONFIG_FS_ENET_HAS_SCC |
| 971 | if (fs_get_scc_index(fpi->fs_no) >=0 ) |
| 972 | fep->ops = &fs_scc_ops; |
| 973 | #endif |
| 974 | |
| 975 | #ifdef CONFIG_FS_ENET_HAS_FCC |
| 976 | if (fs_get_fcc_index(fpi->fs_no) >= 0) |
| 977 | fep->ops = &fs_fcc_ops; |
| 978 | #endif |
| 979 | |
| 980 | if (fep->ops == NULL) { |
| 981 | printk(KERN_ERR DRV_MODULE_NAME |
| 982 | ": %s No matching ops found (%d).\n", |
| 983 | ndev->name, fpi->fs_no); |
| 984 | err = -EINVAL; |
| 985 | goto err; |
| 986 | } |
| 987 | |
| 988 | r = (*fep->ops->setup_data)(ndev); |
| 989 | if (r != 0) { |
| 990 | printk(KERN_ERR DRV_MODULE_NAME |
| 991 | ": %s setup_data failed\n", |
| 992 | ndev->name); |
| 993 | err = r; |
| 994 | goto err; |
| 995 | } |
| 996 | |
| 997 | /* point rx_skbuff, tx_skbuff */ |
| 998 | fep->rx_skbuff = (struct sk_buff **)&fep[1]; |
| 999 | fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring; |
| 1000 | |
| 1001 | /* init locks */ |
| 1002 | spin_lock_init(&fep->lock); |
| 1003 | spin_lock_init(&fep->tx_lock); |
| 1004 | |
| 1005 | /* |
| 1006 | * Set the Ethernet address. |
| 1007 | */ |
| 1008 | for (i = 0; i < 6; i++) |
| 1009 | ndev->dev_addr[i] = fpi->macaddr[i]; |
| 1010 | |
| 1011 | r = (*fep->ops->allocate_bd)(ndev); |
| 1012 | |
| 1013 | if (fep->ring_base == NULL) { |
| 1014 | printk(KERN_ERR DRV_MODULE_NAME |
| 1015 | ": %s buffer descriptor alloc failed (%d).\n", ndev->name, r); |
| 1016 | err = r; |
| 1017 | goto err; |
| 1018 | } |
| 1019 | |
| 1020 | /* |
| 1021 | * Set receive and transmit descriptor base. |
| 1022 | */ |
| 1023 | fep->rx_bd_base = fep->ring_base; |
| 1024 | fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring; |
| 1025 | |
| 1026 | /* initialize ring size variables */ |
| 1027 | fep->tx_ring = fpi->tx_ring; |
| 1028 | fep->rx_ring = fpi->rx_ring; |
| 1029 | |
| 1030 | /* |
| 1031 | * The FEC Ethernet specific entries in the device structure. |
| 1032 | */ |
| 1033 | ndev->open = fs_enet_open; |
| 1034 | ndev->hard_start_xmit = fs_enet_start_xmit; |
| 1035 | ndev->tx_timeout = fs_timeout; |
| 1036 | ndev->watchdog_timeo = 2 * HZ; |
| 1037 | ndev->stop = fs_enet_close; |
| 1038 | ndev->get_stats = fs_enet_get_stats; |
| 1039 | ndev->set_multicast_list = fs_set_multicast_list; |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1040 | netif_napi_add(ndev, &fep->napi, |
| 1041 | fs_enet_rx_napi, fpi->napi_weight); |
| 1042 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1043 | ndev->ethtool_ops = &fs_ethtool_ops; |
| 1044 | ndev->do_ioctl = fs_ioctl; |
| 1045 | |
| 1046 | init_timer(&fep->phy_timer_list); |
| 1047 | |
| 1048 | netif_carrier_off(ndev); |
| 1049 | |
| 1050 | err = register_netdev(ndev); |
| 1051 | if (err != 0) { |
| 1052 | printk(KERN_ERR DRV_MODULE_NAME |
| 1053 | ": %s register_netdev failed.\n", ndev->name); |
| 1054 | goto err; |
| 1055 | } |
| 1056 | registered = 1; |
| 1057 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1058 | |
| 1059 | return ndev; |
| 1060 | |
| 1061 | err: |
| 1062 | if (ndev != NULL) { |
| 1063 | |
| 1064 | if (registered) |
| 1065 | unregister_netdev(ndev); |
| 1066 | |
| 1067 | if (fep != NULL) { |
| 1068 | (*fep->ops->free_bd)(ndev); |
| 1069 | (*fep->ops->cleanup_data)(ndev); |
| 1070 | } |
| 1071 | |
| 1072 | free_netdev(ndev); |
| 1073 | } |
| 1074 | |
| 1075 | dev_set_drvdata(dev, NULL); |
| 1076 | |
| 1077 | return ERR_PTR(err); |
| 1078 | } |
| 1079 | |
| 1080 | static int fs_cleanup_instance(struct net_device *ndev) |
| 1081 | { |
| 1082 | struct fs_enet_private *fep; |
| 1083 | const struct fs_platform_info *fpi; |
| 1084 | struct device *dev; |
| 1085 | |
| 1086 | if (ndev == NULL) |
| 1087 | return -EINVAL; |
| 1088 | |
| 1089 | fep = netdev_priv(ndev); |
| 1090 | if (fep == NULL) |
| 1091 | return -EINVAL; |
| 1092 | |
| 1093 | fpi = fep->fpi; |
| 1094 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1095 | unregister_netdev(ndev); |
| 1096 | |
| 1097 | dma_free_coherent(fep->dev, (fpi->tx_ring + fpi->rx_ring) * sizeof(cbd_t), |
| 1098 | fep->ring_base, fep->ring_mem_addr); |
| 1099 | |
| 1100 | /* reset it */ |
| 1101 | (*fep->ops->cleanup_data)(ndev); |
| 1102 | |
| 1103 | dev = fep->dev; |
| 1104 | if (dev != NULL) { |
| 1105 | dev_set_drvdata(dev, NULL); |
| 1106 | fep->dev = NULL; |
| 1107 | } |
| 1108 | |
| 1109 | free_netdev(ndev); |
| 1110 | |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
| 1114 | /**************************************************************************************/ |
| 1115 | |
| 1116 | /* handy pointer to the immap */ |
| 1117 | void *fs_enet_immap = NULL; |
| 1118 | |
| 1119 | static int setup_immap(void) |
| 1120 | { |
| 1121 | phys_addr_t paddr = 0; |
| 1122 | unsigned long size = 0; |
| 1123 | |
| 1124 | #ifdef CONFIG_CPM1 |
| 1125 | paddr = IMAP_ADDR; |
| 1126 | size = 0x10000; /* map 64K */ |
| 1127 | #endif |
| 1128 | |
| 1129 | #ifdef CONFIG_CPM2 |
| 1130 | paddr = CPM_MAP_ADDR; |
| 1131 | size = 0x40000; /* map 256 K */ |
| 1132 | #endif |
| 1133 | fs_enet_immap = ioremap(paddr, size); |
| 1134 | if (fs_enet_immap == NULL) |
| 1135 | return -EBADF; /* XXX ahem; maybe just BUG_ON? */ |
| 1136 | |
| 1137 | return 0; |
| 1138 | } |
| 1139 | |
| 1140 | static void cleanup_immap(void) |
| 1141 | { |
| 1142 | if (fs_enet_immap != NULL) { |
| 1143 | iounmap(fs_enet_immap); |
| 1144 | fs_enet_immap = NULL; |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | /**************************************************************************************/ |
| 1149 | |
| 1150 | static int __devinit fs_enet_probe(struct device *dev) |
| 1151 | { |
| 1152 | struct net_device *ndev; |
| 1153 | |
| 1154 | /* no fixup - no device */ |
| 1155 | if (dev->platform_data == NULL) { |
| 1156 | printk(KERN_INFO "fs_enet: " |
| 1157 | "probe called with no platform data; " |
| 1158 | "remove unused devices\n"); |
| 1159 | return -ENODEV; |
| 1160 | } |
| 1161 | |
| 1162 | ndev = fs_init_instance(dev, dev->platform_data); |
| 1163 | if (IS_ERR(ndev)) |
| 1164 | return PTR_ERR(ndev); |
| 1165 | return 0; |
| 1166 | } |
| 1167 | |
| 1168 | static int fs_enet_remove(struct device *dev) |
| 1169 | { |
| 1170 | return fs_cleanup_instance(dev_get_drvdata(dev)); |
| 1171 | } |
| 1172 | |
| 1173 | static struct device_driver fs_enet_fec_driver = { |
| 1174 | .name = "fsl-cpm-fec", |
| 1175 | .bus = &platform_bus_type, |
| 1176 | .probe = fs_enet_probe, |
| 1177 | .remove = fs_enet_remove, |
| 1178 | #ifdef CONFIG_PM |
| 1179 | /* .suspend = fs_enet_suspend, TODO */ |
| 1180 | /* .resume = fs_enet_resume, TODO */ |
| 1181 | #endif |
| 1182 | }; |
| 1183 | |
| 1184 | static struct device_driver fs_enet_scc_driver = { |
| 1185 | .name = "fsl-cpm-scc", |
| 1186 | .bus = &platform_bus_type, |
| 1187 | .probe = fs_enet_probe, |
| 1188 | .remove = fs_enet_remove, |
| 1189 | #ifdef CONFIG_PM |
| 1190 | /* .suspend = fs_enet_suspend, TODO */ |
| 1191 | /* .resume = fs_enet_resume, TODO */ |
| 1192 | #endif |
| 1193 | }; |
| 1194 | |
| 1195 | static struct device_driver fs_enet_fcc_driver = { |
| 1196 | .name = "fsl-cpm-fcc", |
| 1197 | .bus = &platform_bus_type, |
| 1198 | .probe = fs_enet_probe, |
| 1199 | .remove = fs_enet_remove, |
| 1200 | #ifdef CONFIG_PM |
| 1201 | /* .suspend = fs_enet_suspend, TODO */ |
| 1202 | /* .resume = fs_enet_resume, TODO */ |
| 1203 | #endif |
| 1204 | }; |
| 1205 | |
| 1206 | static int __init fs_init(void) |
| 1207 | { |
| 1208 | int r; |
| 1209 | |
| 1210 | printk(KERN_INFO |
| 1211 | "%s", version); |
| 1212 | |
| 1213 | r = setup_immap(); |
| 1214 | if (r != 0) |
| 1215 | return r; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1216 | |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 1217 | #ifdef CONFIG_FS_ENET_HAS_FCC |
| 1218 | /* let's insert mii stuff */ |
| 1219 | r = fs_enet_mdio_bb_init(); |
| 1220 | |
| 1221 | if (r != 0) { |
| 1222 | printk(KERN_ERR DRV_MODULE_NAME |
| 1223 | "BB PHY init failed.\n"); |
| 1224 | return r; |
| 1225 | } |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1226 | r = driver_register(&fs_enet_fcc_driver); |
| 1227 | if (r != 0) |
| 1228 | goto err; |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 1229 | #endif |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1230 | |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 1231 | #ifdef CONFIG_FS_ENET_HAS_FEC |
| 1232 | r = fs_enet_mdio_fec_init(); |
| 1233 | if (r != 0) { |
| 1234 | printk(KERN_ERR DRV_MODULE_NAME |
| 1235 | "FEC PHY init failed.\n"); |
| 1236 | return r; |
| 1237 | } |
| 1238 | |
| 1239 | r = driver_register(&fs_enet_fec_driver); |
| 1240 | if (r != 0) |
| 1241 | goto err; |
| 1242 | #endif |
| 1243 | |
| 1244 | #ifdef CONFIG_FS_ENET_HAS_SCC |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1245 | r = driver_register(&fs_enet_scc_driver); |
| 1246 | if (r != 0) |
| 1247 | goto err; |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 1248 | #endif |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1249 | |
| 1250 | return 0; |
| 1251 | err: |
| 1252 | cleanup_immap(); |
| 1253 | return r; |
| 1254 | |
| 1255 | } |
| 1256 | |
| 1257 | static void __exit fs_cleanup(void) |
| 1258 | { |
| 1259 | driver_unregister(&fs_enet_fec_driver); |
| 1260 | driver_unregister(&fs_enet_fcc_driver); |
| 1261 | driver_unregister(&fs_enet_scc_driver); |
| 1262 | cleanup_immap(); |
| 1263 | } |
| 1264 | |
| 1265 | /**************************************************************************************/ |
| 1266 | |
| 1267 | module_init(fs_init); |
| 1268 | module_exit(fs_cleanup); |