Lennert Buytenhek | 15d014d | 2005-11-11 18:23:13 +0100 | [diff] [blame] | 1 | /* |
| 2 | * IXP2000 MSF network device driver |
| 3 | * Copyright (C) 2004, 2005 Lennert Buytenhek <buytenh@wantstofly.org> |
| 4 | * Dedicated to Marija Kulikova. |
| 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 as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/config.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/netdevice.h> |
| 16 | #include <linux/etherdevice.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/moduleparam.h> |
| 19 | #include <asm/arch/uengine.h> |
| 20 | #include <asm/mach-types.h> |
| 21 | #include <asm/io.h> |
| 22 | #include "ixp2400_rx.ucode" |
| 23 | #include "ixp2400_tx.ucode" |
| 24 | #include "ixpdev_priv.h" |
| 25 | #include "ixpdev.h" |
| 26 | |
| 27 | static int nds_count; |
| 28 | static struct net_device **nds; |
| 29 | static int nds_open; |
| 30 | static void (*set_port_admin_status)(int port, int up); |
| 31 | |
| 32 | static struct ixpdev_rx_desc * const rx_desc = |
| 33 | (struct ixpdev_rx_desc *)(IXP2000_SRAM0_VIRT_BASE + RX_BUF_DESC_BASE); |
| 34 | static struct ixpdev_tx_desc * const tx_desc = |
| 35 | (struct ixpdev_tx_desc *)(IXP2000_SRAM0_VIRT_BASE + TX_BUF_DESC_BASE); |
| 36 | static int tx_pointer; |
| 37 | |
| 38 | |
| 39 | static int ixpdev_xmit(struct sk_buff *skb, struct net_device *dev) |
| 40 | { |
| 41 | struct ixpdev_priv *ip = netdev_priv(dev); |
| 42 | struct ixpdev_tx_desc *desc; |
| 43 | int entry; |
| 44 | |
| 45 | if (unlikely(skb->len > PAGE_SIZE)) { |
| 46 | /* @@@ Count drops. */ |
| 47 | dev_kfree_skb(skb); |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | entry = tx_pointer; |
| 52 | tx_pointer = (tx_pointer + 1) % TX_BUF_COUNT; |
| 53 | |
| 54 | desc = tx_desc + entry; |
| 55 | desc->pkt_length = skb->len; |
| 56 | desc->channel = ip->channel; |
| 57 | |
| 58 | skb_copy_and_csum_dev(skb, phys_to_virt(desc->buf_addr)); |
| 59 | dev_kfree_skb(skb); |
| 60 | |
| 61 | ixp2000_reg_write(RING_TX_PENDING, |
| 62 | TX_BUF_DESC_BASE + (entry * sizeof(struct ixpdev_tx_desc))); |
| 63 | |
| 64 | dev->trans_start = jiffies; |
| 65 | |
| 66 | local_irq_disable(); |
| 67 | ip->tx_queue_entries++; |
| 68 | if (ip->tx_queue_entries == TX_BUF_COUNT_PER_CHAN) |
| 69 | netif_stop_queue(dev); |
| 70 | local_irq_enable(); |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | static int ixpdev_rx(struct net_device *dev, int *budget) |
| 77 | { |
| 78 | while (*budget > 0) { |
| 79 | struct ixpdev_rx_desc *desc; |
| 80 | struct sk_buff *skb; |
| 81 | void *buf; |
| 82 | u32 _desc; |
| 83 | |
| 84 | _desc = ixp2000_reg_read(RING_RX_DONE); |
| 85 | if (_desc == 0) |
| 86 | return 0; |
| 87 | |
| 88 | desc = rx_desc + |
| 89 | ((_desc - RX_BUF_DESC_BASE) / sizeof(struct ixpdev_rx_desc)); |
| 90 | buf = phys_to_virt(desc->buf_addr); |
| 91 | |
| 92 | if (desc->pkt_length < 4 || desc->pkt_length > PAGE_SIZE) { |
| 93 | printk(KERN_ERR "ixp2000: rx err, length %d\n", |
| 94 | desc->pkt_length); |
| 95 | goto err; |
| 96 | } |
| 97 | |
| 98 | if (desc->channel < 0 || desc->channel >= nds_count) { |
| 99 | printk(KERN_ERR "ixp2000: rx err, channel %d\n", |
| 100 | desc->channel); |
| 101 | goto err; |
| 102 | } |
| 103 | |
| 104 | /* @@@ Make FCS stripping configurable. */ |
| 105 | desc->pkt_length -= 4; |
| 106 | |
| 107 | if (unlikely(!netif_running(nds[desc->channel]))) |
| 108 | goto err; |
| 109 | |
| 110 | skb = dev_alloc_skb(desc->pkt_length + 2); |
| 111 | if (likely(skb != NULL)) { |
| 112 | skb->dev = nds[desc->channel]; |
| 113 | skb_reserve(skb, 2); |
| 114 | eth_copy_and_sum(skb, buf, desc->pkt_length, 0); |
| 115 | skb_put(skb, desc->pkt_length); |
| 116 | skb->protocol = eth_type_trans(skb, skb->dev); |
| 117 | |
| 118 | skb->dev->last_rx = jiffies; |
| 119 | |
| 120 | netif_receive_skb(skb); |
| 121 | } |
| 122 | |
| 123 | err: |
| 124 | ixp2000_reg_write(RING_RX_PENDING, _desc); |
| 125 | dev->quota--; |
| 126 | (*budget)--; |
| 127 | } |
| 128 | |
| 129 | return 1; |
| 130 | } |
| 131 | |
| 132 | /* dev always points to nds[0]. */ |
| 133 | static int ixpdev_poll(struct net_device *dev, int *budget) |
| 134 | { |
| 135 | /* @@@ Have to stop polling when nds[0] is administratively |
| 136 | * downed while we are polling. */ |
| 137 | do { |
| 138 | ixp2000_reg_write(IXP2000_IRQ_THD_RAW_STATUS_A_0, 0x00ff); |
| 139 | |
| 140 | if (ixpdev_rx(dev, budget)) |
| 141 | return 1; |
| 142 | } while (ixp2000_reg_read(IXP2000_IRQ_THD_RAW_STATUS_A_0) & 0x00ff); |
| 143 | |
| 144 | netif_rx_complete(dev); |
| 145 | ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_SET_A_0, 0x00ff); |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | /* @@@ Ugly hack. */ |
| 151 | static inline int netif_rx_schedule_prep_notup(struct net_device *dev) |
| 152 | { |
| 153 | return !test_and_set_bit(__LINK_STATE_RX_SCHED, &dev->state); |
| 154 | } |
| 155 | |
| 156 | static void ixpdev_tx_complete(void) |
| 157 | { |
| 158 | int channel; |
| 159 | u32 wake; |
| 160 | |
| 161 | wake = 0; |
| 162 | while (1) { |
| 163 | struct ixpdev_priv *ip; |
| 164 | u32 desc; |
| 165 | int entry; |
| 166 | |
| 167 | desc = ixp2000_reg_read(RING_TX_DONE); |
| 168 | if (desc == 0) |
| 169 | break; |
| 170 | |
| 171 | /* @@@ Check whether entries come back in order. */ |
| 172 | entry = (desc - TX_BUF_DESC_BASE) / sizeof(struct ixpdev_tx_desc); |
| 173 | channel = tx_desc[entry].channel; |
| 174 | |
| 175 | if (channel < 0 || channel >= nds_count) { |
| 176 | printk(KERN_ERR "ixp2000: txcomp channel index " |
| 177 | "out of bounds (%d, %.8i, %d)\n", |
| 178 | channel, (unsigned int)desc, entry); |
| 179 | continue; |
| 180 | } |
| 181 | |
| 182 | ip = netdev_priv(nds[channel]); |
| 183 | if (ip->tx_queue_entries == TX_BUF_COUNT_PER_CHAN) |
| 184 | wake |= 1 << channel; |
| 185 | ip->tx_queue_entries--; |
| 186 | } |
| 187 | |
| 188 | for (channel = 0; wake != 0; channel++) { |
| 189 | if (wake & (1 << channel)) { |
| 190 | netif_wake_queue(nds[channel]); |
| 191 | wake &= ~(1 << channel); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | static irqreturn_t ixpdev_interrupt(int irq, void *dev_id, struct pt_regs *regs) |
| 197 | { |
| 198 | u32 status; |
| 199 | |
| 200 | status = ixp2000_reg_read(IXP2000_IRQ_THD_STATUS_A_0); |
| 201 | if (status == 0) |
| 202 | return IRQ_NONE; |
| 203 | |
| 204 | /* |
| 205 | * Any of the eight receive units signaled RX? |
| 206 | */ |
| 207 | if (status & 0x00ff) { |
| 208 | ixp2000_reg_wrb(IXP2000_IRQ_THD_ENABLE_CLEAR_A_0, 0x00ff); |
| 209 | if (likely(netif_rx_schedule_prep_notup(nds[0]))) { |
| 210 | __netif_rx_schedule(nds[0]); |
| 211 | } else { |
| 212 | printk(KERN_CRIT "ixp2000: irq while polling!!\n"); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Any of the eight transmit units signaled TXdone? |
| 218 | */ |
| 219 | if (status & 0xff00) { |
| 220 | ixp2000_reg_wrb(IXP2000_IRQ_THD_RAW_STATUS_A_0, 0xff00); |
| 221 | ixpdev_tx_complete(); |
| 222 | } |
| 223 | |
| 224 | return IRQ_HANDLED; |
| 225 | } |
| 226 | |
| 227 | static int ixpdev_open(struct net_device *dev) |
| 228 | { |
| 229 | struct ixpdev_priv *ip = netdev_priv(dev); |
| 230 | int err; |
| 231 | |
| 232 | if (!nds_open++) { |
| 233 | err = request_irq(IRQ_IXP2000_THDA0, ixpdev_interrupt, |
| 234 | SA_SHIRQ, "ixp2000_eth", nds); |
| 235 | if (err) { |
| 236 | nds_open--; |
| 237 | return err; |
| 238 | } |
| 239 | |
| 240 | ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_SET_A_0, 0xffff); |
| 241 | } |
| 242 | |
| 243 | set_port_admin_status(ip->channel, 1); |
| 244 | netif_start_queue(dev); |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static int ixpdev_close(struct net_device *dev) |
| 250 | { |
| 251 | struct ixpdev_priv *ip = netdev_priv(dev); |
| 252 | |
| 253 | netif_stop_queue(dev); |
| 254 | set_port_admin_status(ip->channel, 0); |
| 255 | |
| 256 | if (!--nds_open) { |
| 257 | ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_CLEAR_A_0, 0xffff); |
| 258 | free_irq(IRQ_IXP2000_THDA0, nds); |
| 259 | } |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | struct net_device *ixpdev_alloc(int channel, int sizeof_priv) |
| 265 | { |
| 266 | struct net_device *dev; |
| 267 | struct ixpdev_priv *ip; |
| 268 | |
| 269 | dev = alloc_etherdev(sizeof_priv); |
| 270 | if (dev == NULL) |
| 271 | return NULL; |
| 272 | |
| 273 | dev->hard_start_xmit = ixpdev_xmit; |
| 274 | dev->poll = ixpdev_poll; |
| 275 | dev->open = ixpdev_open; |
| 276 | dev->stop = ixpdev_close; |
| 277 | |
| 278 | dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM; |
| 279 | dev->weight = 64; |
| 280 | |
| 281 | ip = netdev_priv(dev); |
| 282 | ip->channel = channel; |
| 283 | ip->tx_queue_entries = 0; |
| 284 | |
| 285 | return dev; |
| 286 | } |
| 287 | |
| 288 | int ixpdev_init(int __nds_count, struct net_device **__nds, |
| 289 | void (*__set_port_admin_status)(int port, int up)) |
| 290 | { |
| 291 | int i; |
| 292 | int err; |
| 293 | |
| 294 | if (RX_BUF_COUNT > 192 || TX_BUF_COUNT > 192) { |
| 295 | static void __too_many_rx_or_tx_buffers(void); |
| 296 | __too_many_rx_or_tx_buffers(); |
| 297 | } |
| 298 | |
| 299 | nds_count = __nds_count; |
| 300 | nds = __nds; |
| 301 | set_port_admin_status = __set_port_admin_status; |
| 302 | |
Lennert Buytenhek | 15d014d | 2005-11-11 18:23:13 +0100 | [diff] [blame] | 303 | for (i = 0; i < RX_BUF_COUNT; i++) { |
| 304 | void *buf; |
| 305 | |
| 306 | buf = (void *)get_zeroed_page(GFP_KERNEL); |
| 307 | if (buf == NULL) { |
| 308 | err = -ENOMEM; |
| 309 | while (--i >= 0) |
| 310 | free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr)); |
Lennert Buytenhek | 0c49919 | 2005-11-23 12:49:05 +0100 | [diff] [blame^] | 311 | goto err_out; |
Lennert Buytenhek | 15d014d | 2005-11-11 18:23:13 +0100 | [diff] [blame] | 312 | } |
| 313 | rx_desc[i].buf_addr = virt_to_phys(buf); |
| 314 | rx_desc[i].buf_length = PAGE_SIZE; |
| 315 | } |
| 316 | |
| 317 | /* @@@ Maybe we shouldn't be preallocating TX buffers. */ |
| 318 | for (i = 0; i < TX_BUF_COUNT; i++) { |
| 319 | void *buf; |
| 320 | |
| 321 | buf = (void *)get_zeroed_page(GFP_KERNEL); |
| 322 | if (buf == NULL) { |
| 323 | err = -ENOMEM; |
| 324 | while (--i >= 0) |
| 325 | free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr)); |
| 326 | goto err_free_rx; |
| 327 | } |
| 328 | tx_desc[i].buf_addr = virt_to_phys(buf); |
| 329 | } |
| 330 | |
| 331 | /* 256 entries, ring status set means 'empty', base address 0x0000. */ |
| 332 | ixp2000_reg_write(RING_RX_PENDING_BASE, 0x44000000); |
| 333 | ixp2000_reg_write(RING_RX_PENDING_HEAD, 0x00000000); |
| 334 | ixp2000_reg_write(RING_RX_PENDING_TAIL, 0x00000000); |
| 335 | |
| 336 | /* 256 entries, ring status set means 'full', base address 0x0400. */ |
| 337 | ixp2000_reg_write(RING_RX_DONE_BASE, 0x40000400); |
| 338 | ixp2000_reg_write(RING_RX_DONE_HEAD, 0x00000000); |
| 339 | ixp2000_reg_write(RING_RX_DONE_TAIL, 0x00000000); |
| 340 | |
| 341 | for (i = 0; i < RX_BUF_COUNT; i++) { |
| 342 | ixp2000_reg_write(RING_RX_PENDING, |
| 343 | RX_BUF_DESC_BASE + (i * sizeof(struct ixpdev_rx_desc))); |
| 344 | } |
| 345 | |
| 346 | ixp2000_uengine_load(0, &ixp2400_rx); |
| 347 | ixp2000_uengine_start_contexts(0, 0xff); |
| 348 | |
Lennert Buytenhek | 15d014d | 2005-11-11 18:23:13 +0100 | [diff] [blame] | 349 | /* 256 entries, ring status set means 'empty', base address 0x0800. */ |
| 350 | ixp2000_reg_write(RING_TX_PENDING_BASE, 0x44000800); |
| 351 | ixp2000_reg_write(RING_TX_PENDING_HEAD, 0x00000000); |
| 352 | ixp2000_reg_write(RING_TX_PENDING_TAIL, 0x00000000); |
| 353 | |
| 354 | /* 256 entries, ring status set means 'full', base address 0x0c00. */ |
| 355 | ixp2000_reg_write(RING_TX_DONE_BASE, 0x40000c00); |
| 356 | ixp2000_reg_write(RING_TX_DONE_HEAD, 0x00000000); |
| 357 | ixp2000_reg_write(RING_TX_DONE_TAIL, 0x00000000); |
| 358 | |
| 359 | ixp2000_uengine_load(1, &ixp2400_tx); |
| 360 | ixp2000_uengine_start_contexts(1, 0xff); |
| 361 | |
Lennert Buytenhek | 0c49919 | 2005-11-23 12:49:05 +0100 | [diff] [blame^] | 362 | for (i = 0; i < nds_count; i++) { |
| 363 | err = register_netdev(nds[i]); |
| 364 | if (err) { |
| 365 | while (--i >= 0) |
| 366 | unregister_netdev(nds[i]); |
| 367 | goto err_free_tx; |
| 368 | } |
| 369 | } |
| 370 | |
Lennert Buytenhek | 15d014d | 2005-11-11 18:23:13 +0100 | [diff] [blame] | 371 | return 0; |
| 372 | |
Lennert Buytenhek | 0c49919 | 2005-11-23 12:49:05 +0100 | [diff] [blame^] | 373 | err_free_tx: |
| 374 | for (i = 0; i < TX_BUF_COUNT; i++) |
| 375 | free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr)); |
| 376 | |
Lennert Buytenhek | 15d014d | 2005-11-11 18:23:13 +0100 | [diff] [blame] | 377 | err_free_rx: |
| 378 | for (i = 0; i < RX_BUF_COUNT; i++) |
| 379 | free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr)); |
| 380 | |
Lennert Buytenhek | 15d014d | 2005-11-11 18:23:13 +0100 | [diff] [blame] | 381 | err_out: |
| 382 | return err; |
| 383 | } |
| 384 | |
| 385 | void ixpdev_deinit(void) |
| 386 | { |
| 387 | int i; |
| 388 | |
| 389 | /* @@@ Flush out pending packets. */ |
| 390 | |
Lennert Buytenhek | 0c49919 | 2005-11-23 12:49:05 +0100 | [diff] [blame^] | 391 | for (i = 0; i < nds_count; i++) |
| 392 | unregister_netdev(nds[i]); |
| 393 | |
Lennert Buytenhek | 15d014d | 2005-11-11 18:23:13 +0100 | [diff] [blame] | 394 | ixp2000_uengine_stop_contexts(1, 0xff); |
| 395 | ixp2000_uengine_stop_contexts(0, 0xff); |
| 396 | ixp2000_uengine_reset(0x3); |
| 397 | |
| 398 | for (i = 0; i < TX_BUF_COUNT; i++) |
| 399 | free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr)); |
| 400 | |
| 401 | for (i = 0; i < RX_BUF_COUNT; i++) |
| 402 | free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr)); |
Lennert Buytenhek | 15d014d | 2005-11-11 18:23:13 +0100 | [diff] [blame] | 403 | } |