David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 1 | /* sunvnet.c: Sun LDOM Virtual Network Driver. |
| 2 | * |
David S. Miller | 3d452e5 | 2008-09-01 01:48:52 -0700 | [diff] [blame] | 3 | * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net> |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Joe Perches | 4d5870e | 2010-08-17 07:55:05 +0000 | [diff] [blame] | 6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 7 | |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 8 | #include <linux/module.h> |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/types.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/delay.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/netdevice.h> |
| 15 | #include <linux/ethtool.h> |
| 16 | #include <linux/etherdevice.h> |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 17 | #include <linux/mutex.h> |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 18 | |
| 19 | #include <asm/vio.h> |
| 20 | #include <asm/ldc.h> |
| 21 | |
| 22 | #include "sunvnet.h" |
| 23 | |
| 24 | #define DRV_MODULE_NAME "sunvnet" |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 25 | #define DRV_MODULE_VERSION "1.0" |
| 26 | #define DRV_MODULE_RELDATE "June 25, 2007" |
| 27 | |
| 28 | static char version[] __devinitdata = |
| 29 | DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n"; |
| 30 | MODULE_AUTHOR("David S. Miller (davem@davemloft.net)"); |
| 31 | MODULE_DESCRIPTION("Sun LDOM virtual network driver"); |
| 32 | MODULE_LICENSE("GPL"); |
| 33 | MODULE_VERSION(DRV_MODULE_VERSION); |
| 34 | |
| 35 | /* Ordered from largest major to lowest */ |
| 36 | static struct vio_version vnet_versions[] = { |
| 37 | { .major = 1, .minor = 0 }, |
| 38 | }; |
| 39 | |
| 40 | static inline u32 vnet_tx_dring_avail(struct vio_dring_state *dr) |
| 41 | { |
| 42 | return vio_dring_avail(dr, VNET_TX_RING_SIZE); |
| 43 | } |
| 44 | |
| 45 | static int vnet_handle_unknown(struct vnet_port *port, void *arg) |
| 46 | { |
| 47 | struct vio_msg_tag *pkt = arg; |
| 48 | |
Joe Perches | 4d5870e | 2010-08-17 07:55:05 +0000 | [diff] [blame] | 49 | pr_err("Received unknown msg [%02x:%02x:%04x:%08x]\n", |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 50 | pkt->type, pkt->stype, pkt->stype_env, pkt->sid); |
Joe Perches | 4d5870e | 2010-08-17 07:55:05 +0000 | [diff] [blame] | 51 | pr_err("Resetting connection\n"); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 52 | |
| 53 | ldc_disconnect(port->vio.lp); |
| 54 | |
| 55 | return -ECONNRESET; |
| 56 | } |
| 57 | |
| 58 | static int vnet_send_attr(struct vio_driver_state *vio) |
| 59 | { |
| 60 | struct vnet_port *port = to_vnet_port(vio); |
| 61 | struct net_device *dev = port->vp->dev; |
| 62 | struct vio_net_attr_info pkt; |
| 63 | int i; |
| 64 | |
| 65 | memset(&pkt, 0, sizeof(pkt)); |
| 66 | pkt.tag.type = VIO_TYPE_CTRL; |
| 67 | pkt.tag.stype = VIO_SUBTYPE_INFO; |
| 68 | pkt.tag.stype_env = VIO_ATTR_INFO; |
| 69 | pkt.tag.sid = vio_send_sid(vio); |
| 70 | pkt.xfer_mode = VIO_DRING_MODE; |
| 71 | pkt.addr_type = VNET_ADDR_ETHERMAC; |
| 72 | pkt.ack_freq = 0; |
| 73 | for (i = 0; i < 6; i++) |
| 74 | pkt.addr |= (u64)dev->dev_addr[i] << ((5 - i) * 8); |
| 75 | pkt.mtu = ETH_FRAME_LEN; |
| 76 | |
| 77 | viodbg(HS, "SEND NET ATTR xmode[0x%x] atype[0x%x] addr[%llx] " |
| 78 | "ackfreq[%u] mtu[%llu]\n", |
| 79 | pkt.xfer_mode, pkt.addr_type, |
| 80 | (unsigned long long) pkt.addr, |
| 81 | pkt.ack_freq, |
| 82 | (unsigned long long) pkt.mtu); |
| 83 | |
| 84 | return vio_ldc_send(vio, &pkt, sizeof(pkt)); |
| 85 | } |
| 86 | |
| 87 | static int handle_attr_info(struct vio_driver_state *vio, |
| 88 | struct vio_net_attr_info *pkt) |
| 89 | { |
| 90 | viodbg(HS, "GOT NET ATTR INFO xmode[0x%x] atype[0x%x] addr[%llx] " |
| 91 | "ackfreq[%u] mtu[%llu]\n", |
| 92 | pkt->xfer_mode, pkt->addr_type, |
| 93 | (unsigned long long) pkt->addr, |
| 94 | pkt->ack_freq, |
| 95 | (unsigned long long) pkt->mtu); |
| 96 | |
| 97 | pkt->tag.sid = vio_send_sid(vio); |
| 98 | |
| 99 | if (pkt->xfer_mode != VIO_DRING_MODE || |
| 100 | pkt->addr_type != VNET_ADDR_ETHERMAC || |
| 101 | pkt->mtu != ETH_FRAME_LEN) { |
| 102 | viodbg(HS, "SEND NET ATTR NACK\n"); |
| 103 | |
| 104 | pkt->tag.stype = VIO_SUBTYPE_NACK; |
| 105 | |
| 106 | (void) vio_ldc_send(vio, pkt, sizeof(*pkt)); |
| 107 | |
| 108 | return -ECONNRESET; |
| 109 | } else { |
| 110 | viodbg(HS, "SEND NET ATTR ACK\n"); |
| 111 | |
| 112 | pkt->tag.stype = VIO_SUBTYPE_ACK; |
| 113 | |
| 114 | return vio_ldc_send(vio, pkt, sizeof(*pkt)); |
| 115 | } |
| 116 | |
| 117 | } |
| 118 | |
| 119 | static int handle_attr_ack(struct vio_driver_state *vio, |
| 120 | struct vio_net_attr_info *pkt) |
| 121 | { |
| 122 | viodbg(HS, "GOT NET ATTR ACK\n"); |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static int handle_attr_nack(struct vio_driver_state *vio, |
| 128 | struct vio_net_attr_info *pkt) |
| 129 | { |
| 130 | viodbg(HS, "GOT NET ATTR NACK\n"); |
| 131 | |
| 132 | return -ECONNRESET; |
| 133 | } |
| 134 | |
| 135 | static int vnet_handle_attr(struct vio_driver_state *vio, void *arg) |
| 136 | { |
| 137 | struct vio_net_attr_info *pkt = arg; |
| 138 | |
| 139 | switch (pkt->tag.stype) { |
| 140 | case VIO_SUBTYPE_INFO: |
| 141 | return handle_attr_info(vio, pkt); |
| 142 | |
| 143 | case VIO_SUBTYPE_ACK: |
| 144 | return handle_attr_ack(vio, pkt); |
| 145 | |
| 146 | case VIO_SUBTYPE_NACK: |
| 147 | return handle_attr_nack(vio, pkt); |
| 148 | |
| 149 | default: |
| 150 | return -ECONNRESET; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | static void vnet_handshake_complete(struct vio_driver_state *vio) |
| 155 | { |
| 156 | struct vio_dring_state *dr; |
| 157 | |
| 158 | dr = &vio->drings[VIO_DRIVER_RX_RING]; |
| 159 | dr->snd_nxt = dr->rcv_nxt = 1; |
| 160 | |
| 161 | dr = &vio->drings[VIO_DRIVER_TX_RING]; |
| 162 | dr->snd_nxt = dr->rcv_nxt = 1; |
| 163 | } |
| 164 | |
| 165 | /* The hypervisor interface that implements copying to/from imported |
| 166 | * memory from another domain requires that copies are done to 8-byte |
| 167 | * aligned buffers, and that the lengths of such copies are also 8-byte |
| 168 | * multiples. |
| 169 | * |
| 170 | * So we align skb->data to an 8-byte multiple and pad-out the data |
| 171 | * area so we can round the copy length up to the next multiple of |
| 172 | * 8 for the copy. |
| 173 | * |
| 174 | * The transmitter puts the actual start of the packet 6 bytes into |
| 175 | * the buffer it sends over, so that the IP headers after the ethernet |
| 176 | * header are aligned properly. These 6 bytes are not in the descriptor |
| 177 | * length, they are simply implied. This offset is represented using |
| 178 | * the VNET_PACKET_SKIP macro. |
| 179 | */ |
| 180 | static struct sk_buff *alloc_and_align_skb(struct net_device *dev, |
| 181 | unsigned int len) |
| 182 | { |
| 183 | struct sk_buff *skb = netdev_alloc_skb(dev, len+VNET_PACKET_SKIP+8+8); |
| 184 | unsigned long addr, off; |
| 185 | |
| 186 | if (unlikely(!skb)) |
| 187 | return NULL; |
| 188 | |
| 189 | addr = (unsigned long) skb->data; |
| 190 | off = ((addr + 7UL) & ~7UL) - addr; |
| 191 | if (off) |
| 192 | skb_reserve(skb, off); |
| 193 | |
| 194 | return skb; |
| 195 | } |
| 196 | |
| 197 | static int vnet_rx_one(struct vnet_port *port, unsigned int len, |
| 198 | struct ldc_trans_cookie *cookies, int ncookies) |
| 199 | { |
| 200 | struct net_device *dev = port->vp->dev; |
| 201 | unsigned int copy_len; |
| 202 | struct sk_buff *skb; |
| 203 | int err; |
| 204 | |
| 205 | err = -EMSGSIZE; |
| 206 | if (unlikely(len < ETH_ZLEN || len > ETH_FRAME_LEN)) { |
| 207 | dev->stats.rx_length_errors++; |
| 208 | goto out_dropped; |
| 209 | } |
| 210 | |
| 211 | skb = alloc_and_align_skb(dev, len); |
| 212 | err = -ENOMEM; |
| 213 | if (unlikely(!skb)) { |
| 214 | dev->stats.rx_missed_errors++; |
| 215 | goto out_dropped; |
| 216 | } |
| 217 | |
| 218 | copy_len = (len + VNET_PACKET_SKIP + 7U) & ~7U; |
| 219 | skb_put(skb, copy_len); |
| 220 | err = ldc_copy(port->vio.lp, LDC_COPY_IN, |
| 221 | skb->data, copy_len, 0, |
| 222 | cookies, ncookies); |
| 223 | if (unlikely(err < 0)) { |
| 224 | dev->stats.rx_frame_errors++; |
| 225 | goto out_free_skb; |
| 226 | } |
| 227 | |
| 228 | skb_pull(skb, VNET_PACKET_SKIP); |
| 229 | skb_trim(skb, len); |
| 230 | skb->protocol = eth_type_trans(skb, dev); |
| 231 | |
| 232 | dev->stats.rx_packets++; |
| 233 | dev->stats.rx_bytes += len; |
| 234 | |
| 235 | netif_rx(skb); |
| 236 | |
| 237 | return 0; |
| 238 | |
| 239 | out_free_skb: |
| 240 | kfree_skb(skb); |
| 241 | |
| 242 | out_dropped: |
| 243 | dev->stats.rx_dropped++; |
| 244 | return err; |
| 245 | } |
| 246 | |
| 247 | static int vnet_send_ack(struct vnet_port *port, struct vio_dring_state *dr, |
| 248 | u32 start, u32 end, u8 vio_dring_state) |
| 249 | { |
| 250 | struct vio_dring_data hdr = { |
| 251 | .tag = { |
| 252 | .type = VIO_TYPE_DATA, |
| 253 | .stype = VIO_SUBTYPE_ACK, |
| 254 | .stype_env = VIO_DRING_DATA, |
| 255 | .sid = vio_send_sid(&port->vio), |
| 256 | }, |
| 257 | .dring_ident = dr->ident, |
| 258 | .start_idx = start, |
| 259 | .end_idx = end, |
| 260 | .state = vio_dring_state, |
| 261 | }; |
| 262 | int err, delay; |
| 263 | |
| 264 | hdr.seq = dr->snd_nxt; |
| 265 | delay = 1; |
| 266 | do { |
| 267 | err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr)); |
| 268 | if (err > 0) { |
| 269 | dr->snd_nxt++; |
| 270 | break; |
| 271 | } |
| 272 | udelay(delay); |
| 273 | if ((delay <<= 1) > 128) |
| 274 | delay = 128; |
| 275 | } while (err == -EAGAIN); |
| 276 | |
| 277 | return err; |
| 278 | } |
| 279 | |
| 280 | static u32 next_idx(u32 idx, struct vio_dring_state *dr) |
| 281 | { |
| 282 | if (++idx == dr->num_entries) |
| 283 | idx = 0; |
| 284 | return idx; |
| 285 | } |
| 286 | |
| 287 | static u32 prev_idx(u32 idx, struct vio_dring_state *dr) |
| 288 | { |
| 289 | if (idx == 0) |
| 290 | idx = dr->num_entries - 1; |
| 291 | else |
| 292 | idx--; |
| 293 | |
| 294 | return idx; |
| 295 | } |
| 296 | |
| 297 | static struct vio_net_desc *get_rx_desc(struct vnet_port *port, |
| 298 | struct vio_dring_state *dr, |
| 299 | u32 index) |
| 300 | { |
| 301 | struct vio_net_desc *desc = port->vio.desc_buf; |
| 302 | int err; |
| 303 | |
| 304 | err = ldc_get_dring_entry(port->vio.lp, desc, dr->entry_size, |
| 305 | (index * dr->entry_size), |
| 306 | dr->cookies, dr->ncookies); |
| 307 | if (err < 0) |
| 308 | return ERR_PTR(err); |
| 309 | |
| 310 | return desc; |
| 311 | } |
| 312 | |
| 313 | static int put_rx_desc(struct vnet_port *port, |
| 314 | struct vio_dring_state *dr, |
| 315 | struct vio_net_desc *desc, |
| 316 | u32 index) |
| 317 | { |
| 318 | int err; |
| 319 | |
| 320 | err = ldc_put_dring_entry(port->vio.lp, desc, dr->entry_size, |
| 321 | (index * dr->entry_size), |
| 322 | dr->cookies, dr->ncookies); |
| 323 | if (err < 0) |
| 324 | return err; |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static int vnet_walk_rx_one(struct vnet_port *port, |
| 330 | struct vio_dring_state *dr, |
| 331 | u32 index, int *needs_ack) |
| 332 | { |
| 333 | struct vio_net_desc *desc = get_rx_desc(port, dr, index); |
| 334 | struct vio_driver_state *vio = &port->vio; |
| 335 | int err; |
| 336 | |
| 337 | if (IS_ERR(desc)) |
| 338 | return PTR_ERR(desc); |
| 339 | |
Sam Ravnborg | 3f4528d | 2009-01-06 13:20:38 -0800 | [diff] [blame] | 340 | viodbg(DATA, "vio_walk_rx_one desc[%02x:%02x:%08x:%08x:%llx:%llx]\n", |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 341 | desc->hdr.state, desc->hdr.ack, |
| 342 | desc->size, desc->ncookies, |
| 343 | desc->cookies[0].cookie_addr, |
| 344 | desc->cookies[0].cookie_size); |
| 345 | |
| 346 | if (desc->hdr.state != VIO_DESC_READY) |
| 347 | return 1; |
| 348 | err = vnet_rx_one(port, desc->size, desc->cookies, desc->ncookies); |
| 349 | if (err == -ECONNRESET) |
| 350 | return err; |
| 351 | desc->hdr.state = VIO_DESC_DONE; |
| 352 | err = put_rx_desc(port, dr, desc, index); |
| 353 | if (err < 0) |
| 354 | return err; |
| 355 | *needs_ack = desc->hdr.ack; |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | static int vnet_walk_rx(struct vnet_port *port, struct vio_dring_state *dr, |
| 360 | u32 start, u32 end) |
| 361 | { |
| 362 | struct vio_driver_state *vio = &port->vio; |
| 363 | int ack_start = -1, ack_end = -1; |
| 364 | |
| 365 | end = (end == (u32) -1) ? prev_idx(start, dr) : next_idx(end, dr); |
| 366 | |
| 367 | viodbg(DATA, "vnet_walk_rx start[%08x] end[%08x]\n", start, end); |
| 368 | |
| 369 | while (start != end) { |
| 370 | int ack = 0, err = vnet_walk_rx_one(port, dr, start, &ack); |
| 371 | if (err == -ECONNRESET) |
| 372 | return err; |
| 373 | if (err != 0) |
| 374 | break; |
| 375 | if (ack_start == -1) |
| 376 | ack_start = start; |
| 377 | ack_end = start; |
| 378 | start = next_idx(start, dr); |
| 379 | if (ack && start != end) { |
| 380 | err = vnet_send_ack(port, dr, ack_start, ack_end, |
| 381 | VIO_DRING_ACTIVE); |
| 382 | if (err == -ECONNRESET) |
| 383 | return err; |
| 384 | ack_start = -1; |
| 385 | } |
| 386 | } |
| 387 | if (unlikely(ack_start == -1)) |
| 388 | ack_start = ack_end = prev_idx(start, dr); |
| 389 | return vnet_send_ack(port, dr, ack_start, ack_end, VIO_DRING_STOPPED); |
| 390 | } |
| 391 | |
| 392 | static int vnet_rx(struct vnet_port *port, void *msgbuf) |
| 393 | { |
| 394 | struct vio_dring_data *pkt = msgbuf; |
| 395 | struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_RX_RING]; |
| 396 | struct vio_driver_state *vio = &port->vio; |
| 397 | |
Sam Ravnborg | 3f4528d | 2009-01-06 13:20:38 -0800 | [diff] [blame] | 398 | viodbg(DATA, "vnet_rx stype_env[%04x] seq[%016llx] rcv_nxt[%016llx]\n", |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 399 | pkt->tag.stype_env, pkt->seq, dr->rcv_nxt); |
| 400 | |
| 401 | if (unlikely(pkt->tag.stype_env != VIO_DRING_DATA)) |
| 402 | return 0; |
| 403 | if (unlikely(pkt->seq != dr->rcv_nxt)) { |
Joe Perches | 4d5870e | 2010-08-17 07:55:05 +0000 | [diff] [blame] | 404 | pr_err("RX out of sequence seq[0x%llx] rcv_nxt[0x%llx]\n", |
| 405 | pkt->seq, dr->rcv_nxt); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | dr->rcv_nxt++; |
| 410 | |
| 411 | /* XXX Validate pkt->start_idx and pkt->end_idx XXX */ |
| 412 | |
| 413 | return vnet_walk_rx(port, dr, pkt->start_idx, pkt->end_idx); |
| 414 | } |
| 415 | |
| 416 | static int idx_is_pending(struct vio_dring_state *dr, u32 end) |
| 417 | { |
| 418 | u32 idx = dr->cons; |
| 419 | int found = 0; |
| 420 | |
| 421 | while (idx != dr->prod) { |
| 422 | if (idx == end) { |
| 423 | found = 1; |
| 424 | break; |
| 425 | } |
| 426 | idx = next_idx(idx, dr); |
| 427 | } |
| 428 | return found; |
| 429 | } |
| 430 | |
| 431 | static int vnet_ack(struct vnet_port *port, void *msgbuf) |
| 432 | { |
| 433 | struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING]; |
| 434 | struct vio_dring_data *pkt = msgbuf; |
| 435 | struct net_device *dev; |
| 436 | struct vnet *vp; |
| 437 | u32 end; |
| 438 | |
| 439 | if (unlikely(pkt->tag.stype_env != VIO_DRING_DATA)) |
| 440 | return 0; |
| 441 | |
| 442 | end = pkt->end_idx; |
| 443 | if (unlikely(!idx_is_pending(dr, end))) |
| 444 | return 0; |
| 445 | |
| 446 | dr->cons = next_idx(end, dr); |
| 447 | |
| 448 | vp = port->vp; |
| 449 | dev = vp->dev; |
| 450 | if (unlikely(netif_queue_stopped(dev) && |
| 451 | vnet_tx_dring_avail(dr) >= VNET_TX_WAKEUP_THRESH(dr))) |
| 452 | return 1; |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | static int vnet_nack(struct vnet_port *port, void *msgbuf) |
| 458 | { |
| 459 | /* XXX just reset or similar XXX */ |
| 460 | return 0; |
| 461 | } |
| 462 | |
David S. Miller | 028ebff | 2007-07-20 02:30:25 -0700 | [diff] [blame] | 463 | static int handle_mcast(struct vnet_port *port, void *msgbuf) |
| 464 | { |
| 465 | struct vio_net_mcast_info *pkt = msgbuf; |
| 466 | |
| 467 | if (pkt->tag.stype != VIO_SUBTYPE_ACK) |
Joe Perches | 4d5870e | 2010-08-17 07:55:05 +0000 | [diff] [blame] | 468 | pr_err("%s: Got unexpected MCAST reply [%02x:%02x:%04x:%08x]\n", |
David S. Miller | 028ebff | 2007-07-20 02:30:25 -0700 | [diff] [blame] | 469 | port->vp->dev->name, |
| 470 | pkt->tag.type, |
| 471 | pkt->tag.stype, |
| 472 | pkt->tag.stype_env, |
| 473 | pkt->tag.sid); |
| 474 | |
| 475 | return 0; |
| 476 | } |
| 477 | |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 478 | static void maybe_tx_wakeup(struct vnet *vp) |
| 479 | { |
| 480 | struct net_device *dev = vp->dev; |
| 481 | |
| 482 | netif_tx_lock(dev); |
| 483 | if (likely(netif_queue_stopped(dev))) { |
| 484 | struct vnet_port *port; |
| 485 | int wake = 1; |
| 486 | |
| 487 | list_for_each_entry(port, &vp->port_list, list) { |
| 488 | struct vio_dring_state *dr; |
| 489 | |
| 490 | dr = &port->vio.drings[VIO_DRIVER_TX_RING]; |
| 491 | if (vnet_tx_dring_avail(dr) < |
| 492 | VNET_TX_WAKEUP_THRESH(dr)) { |
| 493 | wake = 0; |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | if (wake) |
| 498 | netif_wake_queue(dev); |
| 499 | } |
| 500 | netif_tx_unlock(dev); |
| 501 | } |
| 502 | |
| 503 | static void vnet_event(void *arg, int event) |
| 504 | { |
| 505 | struct vnet_port *port = arg; |
| 506 | struct vio_driver_state *vio = &port->vio; |
| 507 | unsigned long flags; |
| 508 | int tx_wakeup, err; |
| 509 | |
| 510 | spin_lock_irqsave(&vio->lock, flags); |
| 511 | |
| 512 | if (unlikely(event == LDC_EVENT_RESET || |
| 513 | event == LDC_EVENT_UP)) { |
| 514 | vio_link_state_change(vio, event); |
| 515 | spin_unlock_irqrestore(&vio->lock, flags); |
| 516 | |
David S. Miller | d762acd | 2007-07-18 00:07:39 -0700 | [diff] [blame] | 517 | if (event == LDC_EVENT_RESET) |
| 518 | vio_port_up(vio); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 519 | return; |
| 520 | } |
| 521 | |
| 522 | if (unlikely(event != LDC_EVENT_DATA_READY)) { |
Joe Perches | 4d5870e | 2010-08-17 07:55:05 +0000 | [diff] [blame] | 523 | pr_warning("Unexpected LDC event %d\n", event); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 524 | spin_unlock_irqrestore(&vio->lock, flags); |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | tx_wakeup = err = 0; |
| 529 | while (1) { |
| 530 | union { |
| 531 | struct vio_msg_tag tag; |
| 532 | u64 raw[8]; |
| 533 | } msgbuf; |
| 534 | |
| 535 | err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf)); |
| 536 | if (unlikely(err < 0)) { |
| 537 | if (err == -ECONNRESET) |
| 538 | vio_conn_reset(vio); |
| 539 | break; |
| 540 | } |
| 541 | if (err == 0) |
| 542 | break; |
| 543 | viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n", |
| 544 | msgbuf.tag.type, |
| 545 | msgbuf.tag.stype, |
| 546 | msgbuf.tag.stype_env, |
| 547 | msgbuf.tag.sid); |
| 548 | err = vio_validate_sid(vio, &msgbuf.tag); |
| 549 | if (err < 0) |
| 550 | break; |
| 551 | |
| 552 | if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) { |
| 553 | if (msgbuf.tag.stype == VIO_SUBTYPE_INFO) { |
| 554 | err = vnet_rx(port, &msgbuf); |
| 555 | } else if (msgbuf.tag.stype == VIO_SUBTYPE_ACK) { |
| 556 | err = vnet_ack(port, &msgbuf); |
| 557 | if (err > 0) |
| 558 | tx_wakeup |= err; |
| 559 | } else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK) { |
| 560 | err = vnet_nack(port, &msgbuf); |
| 561 | } |
| 562 | } else if (msgbuf.tag.type == VIO_TYPE_CTRL) { |
David S. Miller | 028ebff | 2007-07-20 02:30:25 -0700 | [diff] [blame] | 563 | if (msgbuf.tag.stype_env == VNET_MCAST_INFO) |
| 564 | err = handle_mcast(port, &msgbuf); |
| 565 | else |
| 566 | err = vio_control_pkt_engine(vio, &msgbuf); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 567 | if (err) |
| 568 | break; |
| 569 | } else { |
| 570 | err = vnet_handle_unknown(port, &msgbuf); |
| 571 | } |
| 572 | if (err == -ECONNRESET) |
| 573 | break; |
| 574 | } |
| 575 | spin_unlock(&vio->lock); |
| 576 | if (unlikely(tx_wakeup && err != -ECONNRESET)) |
| 577 | maybe_tx_wakeup(port->vp); |
| 578 | local_irq_restore(flags); |
| 579 | } |
| 580 | |
| 581 | static int __vnet_tx_trigger(struct vnet_port *port) |
| 582 | { |
| 583 | struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING]; |
| 584 | struct vio_dring_data hdr = { |
| 585 | .tag = { |
| 586 | .type = VIO_TYPE_DATA, |
| 587 | .stype = VIO_SUBTYPE_INFO, |
| 588 | .stype_env = VIO_DRING_DATA, |
| 589 | .sid = vio_send_sid(&port->vio), |
| 590 | }, |
| 591 | .dring_ident = dr->ident, |
| 592 | .start_idx = dr->prod, |
| 593 | .end_idx = (u32) -1, |
| 594 | }; |
| 595 | int err, delay; |
| 596 | |
| 597 | hdr.seq = dr->snd_nxt; |
| 598 | delay = 1; |
| 599 | do { |
| 600 | err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr)); |
| 601 | if (err > 0) { |
| 602 | dr->snd_nxt++; |
| 603 | break; |
| 604 | } |
| 605 | udelay(delay); |
| 606 | if ((delay <<= 1) > 128) |
| 607 | delay = 128; |
| 608 | } while (err == -EAGAIN); |
| 609 | |
| 610 | return err; |
| 611 | } |
| 612 | |
| 613 | struct vnet_port *__tx_port_find(struct vnet *vp, struct sk_buff *skb) |
| 614 | { |
| 615 | unsigned int hash = vnet_hashfn(skb->data); |
| 616 | struct hlist_head *hp = &vp->port_hash[hash]; |
| 617 | struct hlist_node *n; |
| 618 | struct vnet_port *port; |
| 619 | |
| 620 | hlist_for_each_entry(port, n, hp, hash) { |
| 621 | if (!compare_ether_addr(port->raddr, skb->data)) |
| 622 | return port; |
| 623 | } |
| 624 | port = NULL; |
| 625 | if (!list_empty(&vp->port_list)) |
| 626 | port = list_entry(vp->port_list.next, struct vnet_port, list); |
| 627 | |
| 628 | return port; |
| 629 | } |
| 630 | |
| 631 | struct vnet_port *tx_port_find(struct vnet *vp, struct sk_buff *skb) |
| 632 | { |
| 633 | struct vnet_port *ret; |
| 634 | unsigned long flags; |
| 635 | |
| 636 | spin_lock_irqsave(&vp->lock, flags); |
| 637 | ret = __tx_port_find(vp, skb); |
| 638 | spin_unlock_irqrestore(&vp->lock, flags); |
| 639 | |
| 640 | return ret; |
| 641 | } |
| 642 | |
| 643 | static int vnet_start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 644 | { |
| 645 | struct vnet *vp = netdev_priv(dev); |
| 646 | struct vnet_port *port = tx_port_find(vp, skb); |
| 647 | struct vio_dring_state *dr; |
| 648 | struct vio_net_desc *d; |
| 649 | unsigned long flags; |
| 650 | unsigned int len; |
| 651 | void *tx_buf; |
| 652 | int i, err; |
| 653 | |
| 654 | if (unlikely(!port)) |
| 655 | goto out_dropped; |
| 656 | |
| 657 | spin_lock_irqsave(&port->vio.lock, flags); |
| 658 | |
| 659 | dr = &port->vio.drings[VIO_DRIVER_TX_RING]; |
| 660 | if (unlikely(vnet_tx_dring_avail(dr) < 2)) { |
| 661 | if (!netif_queue_stopped(dev)) { |
| 662 | netif_stop_queue(dev); |
| 663 | |
| 664 | /* This is a hard error, log it. */ |
Joe Perches | 4d5870e | 2010-08-17 07:55:05 +0000 | [diff] [blame] | 665 | netdev_err(dev, "BUG! Tx Ring full when queue awake!\n"); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 666 | dev->stats.tx_errors++; |
| 667 | } |
| 668 | spin_unlock_irqrestore(&port->vio.lock, flags); |
| 669 | return NETDEV_TX_BUSY; |
| 670 | } |
| 671 | |
| 672 | d = vio_dring_cur(dr); |
| 673 | |
| 674 | tx_buf = port->tx_bufs[dr->prod].buf; |
| 675 | skb_copy_from_linear_data(skb, tx_buf + VNET_PACKET_SKIP, skb->len); |
| 676 | |
| 677 | len = skb->len; |
| 678 | if (len < ETH_ZLEN) { |
| 679 | len = ETH_ZLEN; |
| 680 | memset(tx_buf+VNET_PACKET_SKIP+skb->len, 0, len - skb->len); |
| 681 | } |
| 682 | |
| 683 | d->hdr.ack = VIO_ACK_ENABLE; |
| 684 | d->size = len; |
| 685 | d->ncookies = port->tx_bufs[dr->prod].ncookies; |
| 686 | for (i = 0; i < d->ncookies; i++) |
| 687 | d->cookies[i] = port->tx_bufs[dr->prod].cookies[i]; |
| 688 | |
| 689 | /* This has to be a non-SMP write barrier because we are writing |
| 690 | * to memory which is shared with the peer LDOM. |
| 691 | */ |
| 692 | wmb(); |
| 693 | |
| 694 | d->hdr.state = VIO_DESC_READY; |
| 695 | |
| 696 | err = __vnet_tx_trigger(port); |
| 697 | if (unlikely(err < 0)) { |
Joe Perches | 4d5870e | 2010-08-17 07:55:05 +0000 | [diff] [blame] | 698 | netdev_info(dev, "TX trigger error %d\n", err); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 699 | d->hdr.state = VIO_DESC_FREE; |
| 700 | dev->stats.tx_carrier_errors++; |
| 701 | goto out_dropped_unlock; |
| 702 | } |
| 703 | |
| 704 | dev->stats.tx_packets++; |
| 705 | dev->stats.tx_bytes += skb->len; |
| 706 | |
| 707 | dr->prod = (dr->prod + 1) & (VNET_TX_RING_SIZE - 1); |
| 708 | if (unlikely(vnet_tx_dring_avail(dr) < 2)) { |
| 709 | netif_stop_queue(dev); |
| 710 | if (vnet_tx_dring_avail(dr) > VNET_TX_WAKEUP_THRESH(dr)) |
| 711 | netif_wake_queue(dev); |
| 712 | } |
| 713 | |
| 714 | spin_unlock_irqrestore(&port->vio.lock, flags); |
| 715 | |
| 716 | dev_kfree_skb(skb); |
| 717 | |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 718 | return NETDEV_TX_OK; |
| 719 | |
| 720 | out_dropped_unlock: |
| 721 | spin_unlock_irqrestore(&port->vio.lock, flags); |
| 722 | |
| 723 | out_dropped: |
| 724 | dev_kfree_skb(skb); |
| 725 | dev->stats.tx_dropped++; |
| 726 | return NETDEV_TX_OK; |
| 727 | } |
| 728 | |
| 729 | static void vnet_tx_timeout(struct net_device *dev) |
| 730 | { |
| 731 | /* XXX Implement me XXX */ |
| 732 | } |
| 733 | |
| 734 | static int vnet_open(struct net_device *dev) |
| 735 | { |
| 736 | netif_carrier_on(dev); |
| 737 | netif_start_queue(dev); |
| 738 | |
| 739 | return 0; |
| 740 | } |
| 741 | |
| 742 | static int vnet_close(struct net_device *dev) |
| 743 | { |
| 744 | netif_stop_queue(dev); |
| 745 | netif_carrier_off(dev); |
| 746 | |
| 747 | return 0; |
| 748 | } |
| 749 | |
David S. Miller | 028ebff | 2007-07-20 02:30:25 -0700 | [diff] [blame] | 750 | static struct vnet_mcast_entry *__vnet_mc_find(struct vnet *vp, u8 *addr) |
| 751 | { |
| 752 | struct vnet_mcast_entry *m; |
| 753 | |
| 754 | for (m = vp->mcast_list; m; m = m->next) { |
| 755 | if (!memcmp(m->addr, addr, ETH_ALEN)) |
| 756 | return m; |
| 757 | } |
| 758 | return NULL; |
| 759 | } |
| 760 | |
| 761 | static void __update_mc_list(struct vnet *vp, struct net_device *dev) |
| 762 | { |
Jiri Pirko | 22bedad | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 763 | struct netdev_hw_addr *ha; |
David S. Miller | 028ebff | 2007-07-20 02:30:25 -0700 | [diff] [blame] | 764 | |
Jiri Pirko | 22bedad | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 765 | netdev_for_each_mc_addr(ha, dev) { |
David S. Miller | 028ebff | 2007-07-20 02:30:25 -0700 | [diff] [blame] | 766 | struct vnet_mcast_entry *m; |
| 767 | |
Jiri Pirko | 22bedad | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 768 | m = __vnet_mc_find(vp, ha->addr); |
David S. Miller | 028ebff | 2007-07-20 02:30:25 -0700 | [diff] [blame] | 769 | if (m) { |
| 770 | m->hit = 1; |
| 771 | continue; |
| 772 | } |
| 773 | |
| 774 | if (!m) { |
| 775 | m = kzalloc(sizeof(*m), GFP_ATOMIC); |
| 776 | if (!m) |
| 777 | continue; |
Jiri Pirko | 22bedad | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 778 | memcpy(m->addr, ha->addr, ETH_ALEN); |
David S. Miller | 028ebff | 2007-07-20 02:30:25 -0700 | [diff] [blame] | 779 | m->hit = 1; |
| 780 | |
| 781 | m->next = vp->mcast_list; |
| 782 | vp->mcast_list = m; |
| 783 | } |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | static void __send_mc_list(struct vnet *vp, struct vnet_port *port) |
| 788 | { |
| 789 | struct vio_net_mcast_info info; |
| 790 | struct vnet_mcast_entry *m, **pp; |
| 791 | int n_addrs; |
| 792 | |
| 793 | memset(&info, 0, sizeof(info)); |
| 794 | |
| 795 | info.tag.type = VIO_TYPE_CTRL; |
| 796 | info.tag.stype = VIO_SUBTYPE_INFO; |
| 797 | info.tag.stype_env = VNET_MCAST_INFO; |
| 798 | info.tag.sid = vio_send_sid(&port->vio); |
| 799 | info.set = 1; |
| 800 | |
| 801 | n_addrs = 0; |
| 802 | for (m = vp->mcast_list; m; m = m->next) { |
| 803 | if (m->sent) |
| 804 | continue; |
| 805 | m->sent = 1; |
| 806 | memcpy(&info.mcast_addr[n_addrs * ETH_ALEN], |
| 807 | m->addr, ETH_ALEN); |
| 808 | if (++n_addrs == VNET_NUM_MCAST) { |
| 809 | info.count = n_addrs; |
| 810 | |
| 811 | (void) vio_ldc_send(&port->vio, &info, |
| 812 | sizeof(info)); |
| 813 | n_addrs = 0; |
| 814 | } |
| 815 | } |
| 816 | if (n_addrs) { |
| 817 | info.count = n_addrs; |
| 818 | (void) vio_ldc_send(&port->vio, &info, sizeof(info)); |
| 819 | } |
| 820 | |
| 821 | info.set = 0; |
| 822 | |
| 823 | n_addrs = 0; |
| 824 | pp = &vp->mcast_list; |
| 825 | while ((m = *pp) != NULL) { |
| 826 | if (m->hit) { |
| 827 | m->hit = 0; |
| 828 | pp = &m->next; |
| 829 | continue; |
| 830 | } |
| 831 | |
| 832 | memcpy(&info.mcast_addr[n_addrs * ETH_ALEN], |
| 833 | m->addr, ETH_ALEN); |
| 834 | if (++n_addrs == VNET_NUM_MCAST) { |
| 835 | info.count = n_addrs; |
| 836 | (void) vio_ldc_send(&port->vio, &info, |
| 837 | sizeof(info)); |
| 838 | n_addrs = 0; |
| 839 | } |
| 840 | |
| 841 | *pp = m->next; |
| 842 | kfree(m); |
| 843 | } |
| 844 | if (n_addrs) { |
| 845 | info.count = n_addrs; |
| 846 | (void) vio_ldc_send(&port->vio, &info, sizeof(info)); |
| 847 | } |
| 848 | } |
| 849 | |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 850 | static void vnet_set_rx_mode(struct net_device *dev) |
| 851 | { |
David S. Miller | 028ebff | 2007-07-20 02:30:25 -0700 | [diff] [blame] | 852 | struct vnet *vp = netdev_priv(dev); |
| 853 | struct vnet_port *port; |
| 854 | unsigned long flags; |
| 855 | |
| 856 | spin_lock_irqsave(&vp->lock, flags); |
| 857 | if (!list_empty(&vp->port_list)) { |
| 858 | port = list_entry(vp->port_list.next, struct vnet_port, list); |
| 859 | |
| 860 | if (port->switch_port) { |
| 861 | __update_mc_list(vp, dev); |
| 862 | __send_mc_list(vp, port); |
| 863 | } |
| 864 | } |
| 865 | spin_unlock_irqrestore(&vp->lock, flags); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | static int vnet_change_mtu(struct net_device *dev, int new_mtu) |
| 869 | { |
| 870 | if (new_mtu != ETH_DATA_LEN) |
| 871 | return -EINVAL; |
| 872 | |
| 873 | dev->mtu = new_mtu; |
| 874 | return 0; |
| 875 | } |
| 876 | |
| 877 | static int vnet_set_mac_addr(struct net_device *dev, void *p) |
| 878 | { |
| 879 | return -EINVAL; |
| 880 | } |
| 881 | |
| 882 | static void vnet_get_drvinfo(struct net_device *dev, |
| 883 | struct ethtool_drvinfo *info) |
| 884 | { |
| 885 | strcpy(info->driver, DRV_MODULE_NAME); |
| 886 | strcpy(info->version, DRV_MODULE_VERSION); |
| 887 | } |
| 888 | |
| 889 | static u32 vnet_get_msglevel(struct net_device *dev) |
| 890 | { |
| 891 | struct vnet *vp = netdev_priv(dev); |
| 892 | return vp->msg_enable; |
| 893 | } |
| 894 | |
| 895 | static void vnet_set_msglevel(struct net_device *dev, u32 value) |
| 896 | { |
| 897 | struct vnet *vp = netdev_priv(dev); |
| 898 | vp->msg_enable = value; |
| 899 | } |
| 900 | |
| 901 | static const struct ethtool_ops vnet_ethtool_ops = { |
| 902 | .get_drvinfo = vnet_get_drvinfo, |
| 903 | .get_msglevel = vnet_get_msglevel, |
| 904 | .set_msglevel = vnet_set_msglevel, |
| 905 | .get_link = ethtool_op_get_link, |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 906 | }; |
| 907 | |
| 908 | static void vnet_port_free_tx_bufs(struct vnet_port *port) |
| 909 | { |
| 910 | struct vio_dring_state *dr; |
| 911 | int i; |
| 912 | |
| 913 | dr = &port->vio.drings[VIO_DRIVER_TX_RING]; |
| 914 | if (dr->base) { |
| 915 | ldc_free_exp_dring(port->vio.lp, dr->base, |
| 916 | (dr->entry_size * dr->num_entries), |
| 917 | dr->cookies, dr->ncookies); |
| 918 | dr->base = NULL; |
| 919 | dr->entry_size = 0; |
| 920 | dr->num_entries = 0; |
| 921 | dr->pending = 0; |
| 922 | dr->ncookies = 0; |
| 923 | } |
| 924 | |
| 925 | for (i = 0; i < VNET_TX_RING_SIZE; i++) { |
| 926 | void *buf = port->tx_bufs[i].buf; |
| 927 | |
| 928 | if (!buf) |
| 929 | continue; |
| 930 | |
| 931 | ldc_unmap(port->vio.lp, |
| 932 | port->tx_bufs[i].cookies, |
| 933 | port->tx_bufs[i].ncookies); |
| 934 | |
| 935 | kfree(buf); |
| 936 | port->tx_bufs[i].buf = NULL; |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | static int __devinit vnet_port_alloc_tx_bufs(struct vnet_port *port) |
| 941 | { |
| 942 | struct vio_dring_state *dr; |
| 943 | unsigned long len; |
| 944 | int i, err, ncookies; |
| 945 | void *dring; |
| 946 | |
| 947 | for (i = 0; i < VNET_TX_RING_SIZE; i++) { |
| 948 | void *buf = kzalloc(ETH_FRAME_LEN + 8, GFP_KERNEL); |
| 949 | int map_len = (ETH_FRAME_LEN + 7) & ~7; |
| 950 | |
| 951 | err = -ENOMEM; |
| 952 | if (!buf) { |
Joe Perches | 4d5870e | 2010-08-17 07:55:05 +0000 | [diff] [blame] | 953 | pr_err("TX buffer allocation failure\n"); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 954 | goto err_out; |
| 955 | } |
| 956 | err = -EFAULT; |
| 957 | if ((unsigned long)buf & (8UL - 1)) { |
Joe Perches | 4d5870e | 2010-08-17 07:55:05 +0000 | [diff] [blame] | 958 | pr_err("TX buffer misaligned\n"); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 959 | kfree(buf); |
| 960 | goto err_out; |
| 961 | } |
| 962 | |
| 963 | err = ldc_map_single(port->vio.lp, buf, map_len, |
| 964 | port->tx_bufs[i].cookies, 2, |
| 965 | (LDC_MAP_SHADOW | |
| 966 | LDC_MAP_DIRECT | |
| 967 | LDC_MAP_RW)); |
| 968 | if (err < 0) { |
| 969 | kfree(buf); |
| 970 | goto err_out; |
| 971 | } |
| 972 | port->tx_bufs[i].buf = buf; |
| 973 | port->tx_bufs[i].ncookies = err; |
| 974 | } |
| 975 | |
| 976 | dr = &port->vio.drings[VIO_DRIVER_TX_RING]; |
| 977 | |
| 978 | len = (VNET_TX_RING_SIZE * |
| 979 | (sizeof(struct vio_net_desc) + |
| 980 | (sizeof(struct ldc_trans_cookie) * 2))); |
| 981 | |
| 982 | ncookies = VIO_MAX_RING_COOKIES; |
| 983 | dring = ldc_alloc_exp_dring(port->vio.lp, len, |
| 984 | dr->cookies, &ncookies, |
| 985 | (LDC_MAP_SHADOW | |
| 986 | LDC_MAP_DIRECT | |
| 987 | LDC_MAP_RW)); |
| 988 | if (IS_ERR(dring)) { |
| 989 | err = PTR_ERR(dring); |
| 990 | goto err_out; |
| 991 | } |
| 992 | |
| 993 | dr->base = dring; |
| 994 | dr->entry_size = (sizeof(struct vio_net_desc) + |
| 995 | (sizeof(struct ldc_trans_cookie) * 2)); |
| 996 | dr->num_entries = VNET_TX_RING_SIZE; |
| 997 | dr->prod = dr->cons = 0; |
| 998 | dr->pending = VNET_TX_RING_SIZE; |
| 999 | dr->ncookies = ncookies; |
| 1000 | |
| 1001 | return 0; |
| 1002 | |
| 1003 | err_out: |
| 1004 | vnet_port_free_tx_bufs(port); |
| 1005 | |
| 1006 | return err; |
| 1007 | } |
| 1008 | |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 1009 | static LIST_HEAD(vnet_list); |
| 1010 | static DEFINE_MUTEX(vnet_list_mutex); |
| 1011 | |
David S. Miller | 8fd17f2 | 2009-03-20 00:51:22 -0700 | [diff] [blame] | 1012 | static const struct net_device_ops vnet_ops = { |
| 1013 | .ndo_open = vnet_open, |
| 1014 | .ndo_stop = vnet_close, |
| 1015 | .ndo_set_multicast_list = vnet_set_rx_mode, |
| 1016 | .ndo_set_mac_address = vnet_set_mac_addr, |
Ben Hutchings | 240c102 | 2009-07-09 17:54:35 +0000 | [diff] [blame] | 1017 | .ndo_validate_addr = eth_validate_addr, |
David S. Miller | 8fd17f2 | 2009-03-20 00:51:22 -0700 | [diff] [blame] | 1018 | .ndo_tx_timeout = vnet_tx_timeout, |
| 1019 | .ndo_change_mtu = vnet_change_mtu, |
| 1020 | .ndo_start_xmit = vnet_start_xmit, |
| 1021 | }; |
| 1022 | |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 1023 | static struct vnet * __devinit vnet_new(const u64 *local_mac) |
| 1024 | { |
| 1025 | struct net_device *dev; |
| 1026 | struct vnet *vp; |
| 1027 | int err, i; |
| 1028 | |
| 1029 | dev = alloc_etherdev(sizeof(*vp)); |
| 1030 | if (!dev) { |
Joe Perches | 4d5870e | 2010-08-17 07:55:05 +0000 | [diff] [blame] | 1031 | pr_err("Etherdev alloc failed, aborting\n"); |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 1032 | return ERR_PTR(-ENOMEM); |
| 1033 | } |
| 1034 | |
| 1035 | for (i = 0; i < ETH_ALEN; i++) |
| 1036 | dev->dev_addr[i] = (*local_mac >> (5 - i) * 8) & 0xff; |
| 1037 | |
| 1038 | memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len); |
| 1039 | |
| 1040 | vp = netdev_priv(dev); |
| 1041 | |
| 1042 | spin_lock_init(&vp->lock); |
| 1043 | vp->dev = dev; |
| 1044 | |
| 1045 | INIT_LIST_HEAD(&vp->port_list); |
| 1046 | for (i = 0; i < VNET_PORT_HASH_SIZE; i++) |
| 1047 | INIT_HLIST_HEAD(&vp->port_hash[i]); |
| 1048 | INIT_LIST_HEAD(&vp->list); |
| 1049 | vp->local_mac = *local_mac; |
| 1050 | |
David S. Miller | 8fd17f2 | 2009-03-20 00:51:22 -0700 | [diff] [blame] | 1051 | dev->netdev_ops = &vnet_ops; |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 1052 | dev->ethtool_ops = &vnet_ethtool_ops; |
| 1053 | dev->watchdog_timeo = VNET_TX_TIMEOUT; |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 1054 | |
| 1055 | err = register_netdev(dev); |
| 1056 | if (err) { |
Joe Perches | 4d5870e | 2010-08-17 07:55:05 +0000 | [diff] [blame] | 1057 | pr_err("Cannot register net device, aborting\n"); |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 1058 | goto err_out_free_dev; |
| 1059 | } |
| 1060 | |
Joe Perches | 4d5870e | 2010-08-17 07:55:05 +0000 | [diff] [blame] | 1061 | netdev_info(dev, "Sun LDOM vnet %pM\n", dev->dev_addr); |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 1062 | |
| 1063 | list_add(&vp->list, &vnet_list); |
| 1064 | |
| 1065 | return vp; |
| 1066 | |
| 1067 | err_out_free_dev: |
| 1068 | free_netdev(dev); |
| 1069 | |
| 1070 | return ERR_PTR(err); |
| 1071 | } |
| 1072 | |
| 1073 | static struct vnet * __devinit vnet_find_or_create(const u64 *local_mac) |
| 1074 | { |
| 1075 | struct vnet *iter, *vp; |
| 1076 | |
| 1077 | mutex_lock(&vnet_list_mutex); |
| 1078 | vp = NULL; |
| 1079 | list_for_each_entry(iter, &vnet_list, list) { |
| 1080 | if (iter->local_mac == *local_mac) { |
| 1081 | vp = iter; |
| 1082 | break; |
| 1083 | } |
| 1084 | } |
| 1085 | if (!vp) |
| 1086 | vp = vnet_new(local_mac); |
| 1087 | mutex_unlock(&vnet_list_mutex); |
| 1088 | |
| 1089 | return vp; |
| 1090 | } |
| 1091 | |
| 1092 | static const char *local_mac_prop = "local-mac-address"; |
| 1093 | |
| 1094 | static struct vnet * __devinit vnet_find_parent(struct mdesc_handle *hp, |
| 1095 | u64 port_node) |
| 1096 | { |
| 1097 | const u64 *local_mac = NULL; |
| 1098 | u64 a; |
| 1099 | |
| 1100 | mdesc_for_each_arc(a, hp, port_node, MDESC_ARC_TYPE_BACK) { |
| 1101 | u64 target = mdesc_arc_target(hp, a); |
| 1102 | const char *name; |
| 1103 | |
| 1104 | name = mdesc_get_property(hp, target, "name", NULL); |
| 1105 | if (!name || strcmp(name, "network")) |
| 1106 | continue; |
| 1107 | |
| 1108 | local_mac = mdesc_get_property(hp, target, |
| 1109 | local_mac_prop, NULL); |
| 1110 | if (local_mac) |
| 1111 | break; |
| 1112 | } |
| 1113 | if (!local_mac) |
| 1114 | return ERR_PTR(-ENODEV); |
| 1115 | |
| 1116 | return vnet_find_or_create(local_mac); |
| 1117 | } |
| 1118 | |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 1119 | static struct ldc_channel_config vnet_ldc_cfg = { |
| 1120 | .event = vnet_event, |
| 1121 | .mtu = 64, |
| 1122 | .mode = LDC_MODE_UNRELIABLE, |
| 1123 | }; |
| 1124 | |
| 1125 | static struct vio_driver_ops vnet_vio_ops = { |
| 1126 | .send_attr = vnet_send_attr, |
| 1127 | .handle_attr = vnet_handle_attr, |
| 1128 | .handshake_complete = vnet_handshake_complete, |
| 1129 | }; |
| 1130 | |
Adrian Bunk | aa782d3 | 2008-01-30 22:03:32 +0200 | [diff] [blame] | 1131 | static void __devinit print_version(void) |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 1132 | { |
Joe Perches | 4d5870e | 2010-08-17 07:55:05 +0000 | [diff] [blame] | 1133 | printk_once(KERN_INFO "%s", version); |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 1136 | const char *remote_macaddr_prop = "remote-mac-address"; |
| 1137 | |
| 1138 | static int __devinit vnet_port_probe(struct vio_dev *vdev, |
| 1139 | const struct vio_device_id *id) |
| 1140 | { |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 1141 | struct mdesc_handle *hp; |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 1142 | struct vnet_port *port; |
| 1143 | unsigned long flags; |
| 1144 | struct vnet *vp; |
| 1145 | const u64 *rmac; |
| 1146 | int len, i, err, switch_port; |
| 1147 | |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 1148 | print_version(); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 1149 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 1150 | hp = mdesc_grab(); |
| 1151 | |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 1152 | vp = vnet_find_parent(hp, vdev->mp); |
| 1153 | if (IS_ERR(vp)) { |
Joe Perches | 4d5870e | 2010-08-17 07:55:05 +0000 | [diff] [blame] | 1154 | pr_err("Cannot find port parent vnet\n"); |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 1155 | err = PTR_ERR(vp); |
| 1156 | goto err_out_put_mdesc; |
| 1157 | } |
| 1158 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 1159 | rmac = mdesc_get_property(hp, vdev->mp, remote_macaddr_prop, &len); |
| 1160 | err = -ENODEV; |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 1161 | if (!rmac) { |
Joe Perches | 4d5870e | 2010-08-17 07:55:05 +0000 | [diff] [blame] | 1162 | pr_err("Port lacks %s property\n", remote_macaddr_prop); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 1163 | goto err_out_put_mdesc; |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | port = kzalloc(sizeof(*port), GFP_KERNEL); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 1167 | err = -ENOMEM; |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 1168 | if (!port) { |
Joe Perches | 4d5870e | 2010-08-17 07:55:05 +0000 | [diff] [blame] | 1169 | pr_err("Cannot allocate vnet_port\n"); |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 1170 | goto err_out_put_mdesc; |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | for (i = 0; i < ETH_ALEN; i++) |
| 1174 | port->raddr[i] = (*rmac >> (5 - i) * 8) & 0xff; |
| 1175 | |
| 1176 | port->vp = vp; |
| 1177 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 1178 | err = vio_driver_init(&port->vio, vdev, VDEV_NETWORK, |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 1179 | vnet_versions, ARRAY_SIZE(vnet_versions), |
| 1180 | &vnet_vio_ops, vp->dev->name); |
| 1181 | if (err) |
| 1182 | goto err_out_free_port; |
| 1183 | |
| 1184 | err = vio_ldc_alloc(&port->vio, &vnet_ldc_cfg, port); |
| 1185 | if (err) |
| 1186 | goto err_out_free_port; |
| 1187 | |
| 1188 | err = vnet_port_alloc_tx_bufs(port); |
| 1189 | if (err) |
| 1190 | goto err_out_free_ldc; |
| 1191 | |
| 1192 | INIT_HLIST_NODE(&port->hash); |
| 1193 | INIT_LIST_HEAD(&port->list); |
| 1194 | |
| 1195 | switch_port = 0; |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 1196 | if (mdesc_get_property(hp, vdev->mp, "switch-port", NULL) != NULL) |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 1197 | switch_port = 1; |
David S. Miller | 028ebff | 2007-07-20 02:30:25 -0700 | [diff] [blame] | 1198 | port->switch_port = switch_port; |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 1199 | |
| 1200 | spin_lock_irqsave(&vp->lock, flags); |
| 1201 | if (switch_port) |
| 1202 | list_add(&port->list, &vp->port_list); |
| 1203 | else |
| 1204 | list_add_tail(&port->list, &vp->port_list); |
| 1205 | hlist_add_head(&port->hash, &vp->port_hash[vnet_hashfn(port->raddr)]); |
| 1206 | spin_unlock_irqrestore(&vp->lock, flags); |
| 1207 | |
| 1208 | dev_set_drvdata(&vdev->dev, port); |
| 1209 | |
Joe Perches | 4d5870e | 2010-08-17 07:55:05 +0000 | [diff] [blame] | 1210 | pr_info("%s: PORT ( remote-mac %pM%s )\n", |
| 1211 | vp->dev->name, port->raddr, switch_port ? " switch-port" : ""); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 1212 | |
| 1213 | vio_port_up(&port->vio); |
| 1214 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 1215 | mdesc_release(hp); |
| 1216 | |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 1217 | return 0; |
| 1218 | |
| 1219 | err_out_free_ldc: |
| 1220 | vio_ldc_free(&port->vio); |
| 1221 | |
| 1222 | err_out_free_port: |
| 1223 | kfree(port); |
| 1224 | |
David S. Miller | 43fdf27 | 2007-07-12 13:47:50 -0700 | [diff] [blame] | 1225 | err_out_put_mdesc: |
| 1226 | mdesc_release(hp); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 1227 | return err; |
| 1228 | } |
| 1229 | |
| 1230 | static int vnet_port_remove(struct vio_dev *vdev) |
| 1231 | { |
| 1232 | struct vnet_port *port = dev_get_drvdata(&vdev->dev); |
| 1233 | |
| 1234 | if (port) { |
| 1235 | struct vnet *vp = port->vp; |
| 1236 | unsigned long flags; |
| 1237 | |
| 1238 | del_timer_sync(&port->vio.timer); |
| 1239 | |
| 1240 | spin_lock_irqsave(&vp->lock, flags); |
| 1241 | list_del(&port->list); |
| 1242 | hlist_del(&port->hash); |
| 1243 | spin_unlock_irqrestore(&vp->lock, flags); |
| 1244 | |
| 1245 | vnet_port_free_tx_bufs(port); |
| 1246 | vio_ldc_free(&port->vio); |
| 1247 | |
| 1248 | dev_set_drvdata(&vdev->dev, NULL); |
| 1249 | |
| 1250 | kfree(port); |
| 1251 | } |
| 1252 | return 0; |
| 1253 | } |
| 1254 | |
David S. Miller | 3d452e5 | 2008-09-01 01:48:52 -0700 | [diff] [blame] | 1255 | static const struct vio_device_id vnet_port_match[] = { |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 1256 | { |
| 1257 | .type = "vnet-port", |
| 1258 | }, |
| 1259 | {}, |
| 1260 | }; |
Fabio Massimo Di Nitto | da68e08 | 2007-07-18 14:35:23 -0700 | [diff] [blame] | 1261 | MODULE_DEVICE_TABLE(vio, vnet_port_match); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 1262 | |
| 1263 | static struct vio_driver vnet_port_driver = { |
| 1264 | .id_table = vnet_port_match, |
| 1265 | .probe = vnet_port_probe, |
| 1266 | .remove = vnet_port_remove, |
| 1267 | .driver = { |
| 1268 | .name = "vnet_port", |
| 1269 | .owner = THIS_MODULE, |
| 1270 | } |
| 1271 | }; |
| 1272 | |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 1273 | static int __init vnet_init(void) |
| 1274 | { |
David S. Miller | 9184a04 | 2007-07-17 22:19:10 -0700 | [diff] [blame] | 1275 | return vio_register_driver(&vnet_port_driver); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | static void __exit vnet_exit(void) |
| 1279 | { |
| 1280 | vio_unregister_driver(&vnet_port_driver); |
David S. Miller | 4c521e4 | 2007-07-09 22:23:51 -0700 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | module_init(vnet_init); |
| 1284 | module_exit(vnet_exit); |