Version:  2.0.40 2.2.26 2.4.37 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19

Linux/drivers/net/ethernet/sun/sunvnet.c

  1 /* sunvnet.c: Sun LDOM Virtual Network Driver.
  2  *
  3  * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
  4  */
  5 
  6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7 
  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>
 17 #include <linux/mutex.h>
 18 #include <linux/highmem.h>
 19 #include <linux/if_vlan.h>
 20 
 21 #if IS_ENABLED(CONFIG_IPV6)
 22 #include <linux/icmpv6.h>
 23 #endif
 24 
 25 #include <net/ip.h>
 26 #include <net/icmp.h>
 27 #include <net/route.h>
 28 
 29 #include <asm/vio.h>
 30 #include <asm/ldc.h>
 31 
 32 #include "sunvnet.h"
 33 
 34 #define DRV_MODULE_NAME         "sunvnet"
 35 #define DRV_MODULE_VERSION      "1.0"
 36 #define DRV_MODULE_RELDATE      "June 25, 2007"
 37 
 38 static char version[] =
 39         DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
 40 MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
 41 MODULE_DESCRIPTION("Sun LDOM virtual network driver");
 42 MODULE_LICENSE("GPL");
 43 MODULE_VERSION(DRV_MODULE_VERSION);
 44 
 45 #define VNET_MAX_TXQS           16
 46 
 47 /* Heuristic for the number of times to exponentially backoff and
 48  * retry sending an LDC trigger when EAGAIN is encountered
 49  */
 50 #define VNET_MAX_RETRIES        10
 51 
 52 static int __vnet_tx_trigger(struct vnet_port *port, u32 start);
 53 
 54 /* Ordered from largest major to lowest */
 55 static struct vio_version vnet_versions[] = {
 56         { .major = 1, .minor = 8 },
 57         { .major = 1, .minor = 7 },
 58         { .major = 1, .minor = 6 },
 59         { .major = 1, .minor = 0 },
 60 };
 61 
 62 static inline u32 vnet_tx_dring_avail(struct vio_dring_state *dr)
 63 {
 64         return vio_dring_avail(dr, VNET_TX_RING_SIZE);
 65 }
 66 
 67 static int vnet_handle_unknown(struct vnet_port *port, void *arg)
 68 {
 69         struct vio_msg_tag *pkt = arg;
 70 
 71         pr_err("Received unknown msg [%02x:%02x:%04x:%08x]\n",
 72                pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
 73         pr_err("Resetting connection\n");
 74 
 75         ldc_disconnect(port->vio.lp);
 76 
 77         return -ECONNRESET;
 78 }
 79 
 80 static int vnet_port_alloc_tx_ring(struct vnet_port *port);
 81 
 82 static int vnet_send_attr(struct vio_driver_state *vio)
 83 {
 84         struct vnet_port *port = to_vnet_port(vio);
 85         struct net_device *dev = port->vp->dev;
 86         struct vio_net_attr_info pkt;
 87         int framelen = ETH_FRAME_LEN;
 88         int i, err;
 89 
 90         err = vnet_port_alloc_tx_ring(to_vnet_port(vio));
 91         if (err)
 92                 return err;
 93 
 94         memset(&pkt, 0, sizeof(pkt));
 95         pkt.tag.type = VIO_TYPE_CTRL;
 96         pkt.tag.stype = VIO_SUBTYPE_INFO;
 97         pkt.tag.stype_env = VIO_ATTR_INFO;
 98         pkt.tag.sid = vio_send_sid(vio);
 99         if (vio_version_before(vio, 1, 2))
100                 pkt.xfer_mode = VIO_DRING_MODE;
101         else
102                 pkt.xfer_mode = VIO_NEW_DRING_MODE;
103         pkt.addr_type = VNET_ADDR_ETHERMAC;
104         pkt.ack_freq = 0;
105         for (i = 0; i < 6; i++)
106                 pkt.addr |= (u64)dev->dev_addr[i] << ((5 - i) * 8);
107         if (vio_version_after(vio, 1, 3)) {
108                 if (port->rmtu) {
109                         port->rmtu = min(VNET_MAXPACKET, port->rmtu);
110                         pkt.mtu = port->rmtu;
111                 } else {
112                         port->rmtu = VNET_MAXPACKET;
113                         pkt.mtu = port->rmtu;
114                 }
115                 if (vio_version_after_eq(vio, 1, 6))
116                         pkt.options = VIO_TX_DRING;
117         } else if (vio_version_before(vio, 1, 3)) {
118                 pkt.mtu = framelen;
119         } else { /* v1.3 */
120                 pkt.mtu = framelen + VLAN_HLEN;
121         }
122 
123         pkt.cflags = 0;
124         if (vio_version_after_eq(vio, 1, 7) && port->tso) {
125                 pkt.cflags |= VNET_LSO_IPV4_CAPAB;
126                 if (!port->tsolen)
127                         port->tsolen = VNET_MAXTSO;
128                 pkt.ipv4_lso_maxlen = port->tsolen;
129         }
130 
131         pkt.plnk_updt = PHYSLINK_UPDATE_NONE;
132 
133         viodbg(HS, "SEND NET ATTR xmode[0x%x] atype[0x%x] addr[%llx] "
134                "ackfreq[%u] plnk_updt[0x%02x] opts[0x%02x] mtu[%llu] "
135                "cflags[0x%04x] lso_max[%u]\n",
136                pkt.xfer_mode, pkt.addr_type,
137                (unsigned long long)pkt.addr,
138                pkt.ack_freq, pkt.plnk_updt, pkt.options,
139                (unsigned long long)pkt.mtu, pkt.cflags, pkt.ipv4_lso_maxlen);
140 
141 
142         return vio_ldc_send(vio, &pkt, sizeof(pkt));
143 }
144 
145 static int handle_attr_info(struct vio_driver_state *vio,
146                             struct vio_net_attr_info *pkt)
147 {
148         struct vnet_port *port = to_vnet_port(vio);
149         u64     localmtu;
150         u8      xfer_mode;
151 
152         viodbg(HS, "GOT NET ATTR xmode[0x%x] atype[0x%x] addr[%llx] "
153                "ackfreq[%u] plnk_updt[0x%02x] opts[0x%02x] mtu[%llu] "
154                " (rmtu[%llu]) cflags[0x%04x] lso_max[%u]\n",
155                pkt->xfer_mode, pkt->addr_type,
156                (unsigned long long)pkt->addr,
157                pkt->ack_freq, pkt->plnk_updt, pkt->options,
158                (unsigned long long)pkt->mtu, port->rmtu, pkt->cflags,
159                pkt->ipv4_lso_maxlen);
160 
161         pkt->tag.sid = vio_send_sid(vio);
162 
163         xfer_mode = pkt->xfer_mode;
164         /* for version < 1.2, VIO_DRING_MODE = 0x3 and no bitmask */
165         if (vio_version_before(vio, 1, 2) && xfer_mode == VIO_DRING_MODE)
166                 xfer_mode = VIO_NEW_DRING_MODE;
167 
168         /* MTU negotiation:
169          *      < v1.3 - ETH_FRAME_LEN exactly
170          *      > v1.3 - MIN(pkt.mtu, VNET_MAXPACKET, port->rmtu) and change
171          *                      pkt->mtu for ACK
172          *      = v1.3 - ETH_FRAME_LEN + VLAN_HLEN exactly
173          */
174         if (vio_version_before(vio, 1, 3)) {
175                 localmtu = ETH_FRAME_LEN;
176         } else if (vio_version_after(vio, 1, 3)) {
177                 localmtu = port->rmtu ? port->rmtu : VNET_MAXPACKET;
178                 localmtu = min(pkt->mtu, localmtu);
179                 pkt->mtu = localmtu;
180         } else { /* v1.3 */
181                 localmtu = ETH_FRAME_LEN + VLAN_HLEN;
182         }
183         port->rmtu = localmtu;
184 
185         /* LSO negotiation */
186         if (vio_version_after_eq(vio, 1, 7))
187                 port->tso &= !!(pkt->cflags & VNET_LSO_IPV4_CAPAB);
188         else
189                 port->tso = false;
190         if (port->tso) {
191                 if (!port->tsolen)
192                         port->tsolen = VNET_MAXTSO;
193                 port->tsolen = min(port->tsolen, pkt->ipv4_lso_maxlen);
194                 if (port->tsolen < VNET_MINTSO) {
195                         port->tso = false;
196                         port->tsolen = 0;
197                         pkt->cflags &= ~VNET_LSO_IPV4_CAPAB;
198                 }
199                 pkt->ipv4_lso_maxlen = port->tsolen;
200         } else {
201                 pkt->cflags &= ~VNET_LSO_IPV4_CAPAB;
202                 pkt->ipv4_lso_maxlen = 0;
203         }
204 
205         /* for version >= 1.6, ACK packet mode we support */
206         if (vio_version_after_eq(vio, 1, 6)) {
207                 pkt->xfer_mode = VIO_NEW_DRING_MODE;
208                 pkt->options = VIO_TX_DRING;
209         }
210 
211         if (!(xfer_mode | VIO_NEW_DRING_MODE) ||
212             pkt->addr_type != VNET_ADDR_ETHERMAC ||
213             pkt->mtu != localmtu) {
214                 viodbg(HS, "SEND NET ATTR NACK\n");
215 
216                 pkt->tag.stype = VIO_SUBTYPE_NACK;
217 
218                 (void) vio_ldc_send(vio, pkt, sizeof(*pkt));
219 
220                 return -ECONNRESET;
221         } else {
222                 viodbg(HS, "SEND NET ATTR ACK xmode[0x%x] atype[0x%x] "
223                        "addr[%llx] ackfreq[%u] plnk_updt[0x%02x] opts[0x%02x] "
224                        "mtu[%llu] (rmtu[%llu]) cflags[0x%04x] lso_max[%u]\n",
225                        pkt->xfer_mode, pkt->addr_type,
226                        (unsigned long long)pkt->addr,
227                        pkt->ack_freq, pkt->plnk_updt, pkt->options,
228                        (unsigned long long)pkt->mtu, port->rmtu, pkt->cflags,
229                        pkt->ipv4_lso_maxlen);
230 
231                 pkt->tag.stype = VIO_SUBTYPE_ACK;
232 
233                 return vio_ldc_send(vio, pkt, sizeof(*pkt));
234         }
235 
236 }
237 
238 static int handle_attr_ack(struct vio_driver_state *vio,
239                            struct vio_net_attr_info *pkt)
240 {
241         viodbg(HS, "GOT NET ATTR ACK\n");
242 
243         return 0;
244 }
245 
246 static int handle_attr_nack(struct vio_driver_state *vio,
247                             struct vio_net_attr_info *pkt)
248 {
249         viodbg(HS, "GOT NET ATTR NACK\n");
250 
251         return -ECONNRESET;
252 }
253 
254 static int vnet_handle_attr(struct vio_driver_state *vio, void *arg)
255 {
256         struct vio_net_attr_info *pkt = arg;
257 
258         switch (pkt->tag.stype) {
259         case VIO_SUBTYPE_INFO:
260                 return handle_attr_info(vio, pkt);
261 
262         case VIO_SUBTYPE_ACK:
263                 return handle_attr_ack(vio, pkt);
264 
265         case VIO_SUBTYPE_NACK:
266                 return handle_attr_nack(vio, pkt);
267 
268         default:
269                 return -ECONNRESET;
270         }
271 }
272 
273 static void vnet_handshake_complete(struct vio_driver_state *vio)
274 {
275         struct vio_dring_state *dr;
276 
277         dr = &vio->drings[VIO_DRIVER_RX_RING];
278         dr->snd_nxt = dr->rcv_nxt = 1;
279 
280         dr = &vio->drings[VIO_DRIVER_TX_RING];
281         dr->snd_nxt = dr->rcv_nxt = 1;
282 }
283 
284 /* The hypervisor interface that implements copying to/from imported
285  * memory from another domain requires that copies are done to 8-byte
286  * aligned buffers, and that the lengths of such copies are also 8-byte
287  * multiples.
288  *
289  * So we align skb->data to an 8-byte multiple and pad-out the data
290  * area so we can round the copy length up to the next multiple of
291  * 8 for the copy.
292  *
293  * The transmitter puts the actual start of the packet 6 bytes into
294  * the buffer it sends over, so that the IP headers after the ethernet
295  * header are aligned properly.  These 6 bytes are not in the descriptor
296  * length, they are simply implied.  This offset is represented using
297  * the VNET_PACKET_SKIP macro.
298  */
299 static struct sk_buff *alloc_and_align_skb(struct net_device *dev,
300                                            unsigned int len)
301 {
302         struct sk_buff *skb = netdev_alloc_skb(dev, len+VNET_PACKET_SKIP+8+8);
303         unsigned long addr, off;
304 
305         if (unlikely(!skb))
306                 return NULL;
307 
308         addr = (unsigned long) skb->data;
309         off = ((addr + 7UL) & ~7UL) - addr;
310         if (off)
311                 skb_reserve(skb, off);
312 
313         return skb;
314 }
315 
316 static inline void vnet_fullcsum(struct sk_buff *skb)
317 {
318         struct iphdr *iph = ip_hdr(skb);
319         int offset = skb_transport_offset(skb);
320 
321         if (skb->protocol != htons(ETH_P_IP))
322                 return;
323         if (iph->protocol != IPPROTO_TCP &&
324             iph->protocol != IPPROTO_UDP)
325                 return;
326         skb->ip_summed = CHECKSUM_NONE;
327         skb->csum_level = 1;
328         skb->csum = 0;
329         if (iph->protocol == IPPROTO_TCP) {
330                 struct tcphdr *ptcp = tcp_hdr(skb);
331 
332                 ptcp->check = 0;
333                 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
334                 ptcp->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
335                                                 skb->len - offset, IPPROTO_TCP,
336                                                 skb->csum);
337         } else if (iph->protocol == IPPROTO_UDP) {
338                 struct udphdr *pudp = udp_hdr(skb);
339 
340                 pudp->check = 0;
341                 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
342                 pudp->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
343                                                 skb->len - offset, IPPROTO_UDP,
344                                                 skb->csum);
345         }
346 }
347 
348 static int vnet_rx_one(struct vnet_port *port, struct vio_net_desc *desc)
349 {
350         struct net_device *dev = port->vp->dev;
351         unsigned int len = desc->size;
352         unsigned int copy_len;
353         struct sk_buff *skb;
354         int err;
355 
356         err = -EMSGSIZE;
357         if (unlikely(len < ETH_ZLEN || len > port->rmtu)) {
358                 dev->stats.rx_length_errors++;
359                 goto out_dropped;
360         }
361 
362         skb = alloc_and_align_skb(dev, len);
363         err = -ENOMEM;
364         if (unlikely(!skb)) {
365                 dev->stats.rx_missed_errors++;
366                 goto out_dropped;
367         }
368 
369         copy_len = (len + VNET_PACKET_SKIP + 7U) & ~7U;
370         skb_put(skb, copy_len);
371         err = ldc_copy(port->vio.lp, LDC_COPY_IN,
372                        skb->data, copy_len, 0,
373                        desc->cookies, desc->ncookies);
374         if (unlikely(err < 0)) {
375                 dev->stats.rx_frame_errors++;
376                 goto out_free_skb;
377         }
378 
379         skb_pull(skb, VNET_PACKET_SKIP);
380         skb_trim(skb, len);
381         skb->protocol = eth_type_trans(skb, dev);
382 
383         if (vio_version_after_eq(&port->vio, 1, 8)) {
384                 struct vio_net_dext *dext = vio_net_ext(desc);
385 
386                 if (dext->flags & VNET_PKT_HCK_IPV4_HDRCKSUM) {
387                         if (skb->protocol == ETH_P_IP) {
388                                 struct iphdr *iph = (struct iphdr *)skb->data;
389 
390                                 iph->check = 0;
391                                 ip_send_check(iph);
392                         }
393                 }
394                 if ((dext->flags & VNET_PKT_HCK_FULLCKSUM) &&
395                     skb->ip_summed == CHECKSUM_NONE)
396                         vnet_fullcsum(skb);
397                 if (dext->flags & VNET_PKT_HCK_IPV4_HDRCKSUM_OK) {
398                         skb->ip_summed = CHECKSUM_PARTIAL;
399                         skb->csum_level = 0;
400                         if (dext->flags & VNET_PKT_HCK_FULLCKSUM_OK)
401                                 skb->csum_level = 1;
402                 }
403         }
404 
405         skb->ip_summed = port->switch_port ? CHECKSUM_NONE : CHECKSUM_PARTIAL;
406 
407         dev->stats.rx_packets++;
408         dev->stats.rx_bytes += len;
409         napi_gro_receive(&port->napi, skb);
410         return 0;
411 
412 out_free_skb:
413         kfree_skb(skb);
414 
415 out_dropped:
416         dev->stats.rx_dropped++;
417         return err;
418 }
419 
420 static int vnet_send_ack(struct vnet_port *port, struct vio_dring_state *dr,
421                          u32 start, u32 end, u8 vio_dring_state)
422 {
423         struct vio_dring_data hdr = {
424                 .tag = {
425                         .type           = VIO_TYPE_DATA,
426                         .stype          = VIO_SUBTYPE_ACK,
427                         .stype_env      = VIO_DRING_DATA,
428                         .sid            = vio_send_sid(&port->vio),
429                 },
430                 .dring_ident            = dr->ident,
431                 .start_idx              = start,
432                 .end_idx                = end,
433                 .state                  = vio_dring_state,
434         };
435         int err, delay;
436         int retries = 0;
437 
438         hdr.seq = dr->snd_nxt;
439         delay = 1;
440         do {
441                 err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
442                 if (err > 0) {
443                         dr->snd_nxt++;
444                         break;
445                 }
446                 udelay(delay);
447                 if ((delay <<= 1) > 128)
448                         delay = 128;
449                 if (retries++ > VNET_MAX_RETRIES) {
450                         pr_info("ECONNRESET %x:%x:%x:%x:%x:%x\n",
451                                 port->raddr[0], port->raddr[1],
452                                 port->raddr[2], port->raddr[3],
453                                 port->raddr[4], port->raddr[5]);
454                         break;
455                 }
456         } while (err == -EAGAIN);
457 
458         if (err <= 0 && vio_dring_state == VIO_DRING_STOPPED) {
459                 port->stop_rx_idx = end;
460                 port->stop_rx = true;
461         } else {
462                 port->stop_rx_idx = 0;
463                 port->stop_rx = false;
464         }
465 
466         return err;
467 }
468 
469 static struct vio_net_desc *get_rx_desc(struct vnet_port *port,
470                                         struct vio_dring_state *dr,
471                                         u32 index)
472 {
473         struct vio_net_desc *desc = port->vio.desc_buf;
474         int err;
475 
476         err = ldc_get_dring_entry(port->vio.lp, desc, dr->entry_size,
477                                   (index * dr->entry_size),
478                                   dr->cookies, dr->ncookies);
479         if (err < 0)
480                 return ERR_PTR(err);
481 
482         return desc;
483 }
484 
485 static int put_rx_desc(struct vnet_port *port,
486                        struct vio_dring_state *dr,
487                        struct vio_net_desc *desc,
488                        u32 index)
489 {
490         int err;
491 
492         err = ldc_put_dring_entry(port->vio.lp, desc, dr->entry_size,
493                                   (index * dr->entry_size),
494                                   dr->cookies, dr->ncookies);
495         if (err < 0)
496                 return err;
497 
498         return 0;
499 }
500 
501 static int vnet_walk_rx_one(struct vnet_port *port,
502                             struct vio_dring_state *dr,
503                             u32 index, int *needs_ack)
504 {
505         struct vio_net_desc *desc = get_rx_desc(port, dr, index);
506         struct vio_driver_state *vio = &port->vio;
507         int err;
508 
509         BUG_ON(desc == NULL);
510         if (IS_ERR(desc))
511                 return PTR_ERR(desc);
512 
513         if (desc->hdr.state != VIO_DESC_READY)
514                 return 1;
515 
516         rmb();
517 
518         viodbg(DATA, "vio_walk_rx_one desc[%02x:%02x:%08x:%08x:%llx:%llx]\n",
519                desc->hdr.state, desc->hdr.ack,
520                desc->size, desc->ncookies,
521                desc->cookies[0].cookie_addr,
522                desc->cookies[0].cookie_size);
523 
524         err = vnet_rx_one(port, desc);
525         if (err == -ECONNRESET)
526                 return err;
527         desc->hdr.state = VIO_DESC_DONE;
528         err = put_rx_desc(port, dr, desc, index);
529         if (err < 0)
530                 return err;
531         *needs_ack = desc->hdr.ack;
532         return 0;
533 }
534 
535 static int vnet_walk_rx(struct vnet_port *port, struct vio_dring_state *dr,
536                         u32 start, u32 end, int *npkts, int budget)
537 {
538         struct vio_driver_state *vio = &port->vio;
539         int ack_start = -1, ack_end = -1;
540         bool send_ack = true;
541 
542         end = (end == (u32) -1) ? vio_dring_prev(dr, start)
543                                 : vio_dring_next(dr, end);
544 
545         viodbg(DATA, "vnet_walk_rx start[%08x] end[%08x]\n", start, end);
546 
547         while (start != end) {
548                 int ack = 0, err = vnet_walk_rx_one(port, dr, start, &ack);
549                 if (err == -ECONNRESET)
550                         return err;
551                 if (err != 0)
552                         break;
553                 (*npkts)++;
554                 if (ack_start == -1)
555                         ack_start = start;
556                 ack_end = start;
557                 start = vio_dring_next(dr, start);
558                 if (ack && start != end) {
559                         err = vnet_send_ack(port, dr, ack_start, ack_end,
560                                             VIO_DRING_ACTIVE);
561                         if (err == -ECONNRESET)
562                                 return err;
563                         ack_start = -1;
564                 }
565                 if ((*npkts) >= budget) {
566                         send_ack = false;
567                         break;
568                 }
569         }
570         if (unlikely(ack_start == -1))
571                 ack_start = ack_end = vio_dring_prev(dr, start);
572         if (send_ack) {
573                 port->napi_resume = false;
574                 return vnet_send_ack(port, dr, ack_start, ack_end,
575                                      VIO_DRING_STOPPED);
576         } else  {
577                 port->napi_resume = true;
578                 port->napi_stop_idx = ack_end;
579                 return 1;
580         }
581 }
582 
583 static int vnet_rx(struct vnet_port *port, void *msgbuf, int *npkts,
584                    int budget)
585 {
586         struct vio_dring_data *pkt = msgbuf;
587         struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_RX_RING];
588         struct vio_driver_state *vio = &port->vio;
589 
590         viodbg(DATA, "vnet_rx stype_env[%04x] seq[%016llx] rcv_nxt[%016llx]\n",
591                pkt->tag.stype_env, pkt->seq, dr->rcv_nxt);
592 
593         if (unlikely(pkt->tag.stype_env != VIO_DRING_DATA))
594                 return 0;
595         if (unlikely(pkt->seq != dr->rcv_nxt)) {
596                 pr_err("RX out of sequence seq[0x%llx] rcv_nxt[0x%llx]\n",
597                        pkt->seq, dr->rcv_nxt);
598                 return 0;
599         }
600 
601         if (!port->napi_resume)
602                 dr->rcv_nxt++;
603 
604         /* XXX Validate pkt->start_idx and pkt->end_idx XXX */
605 
606         return vnet_walk_rx(port, dr, pkt->start_idx, pkt->end_idx,
607                             npkts, budget);
608 }
609 
610 static int idx_is_pending(struct vio_dring_state *dr, u32 end)
611 {
612         u32 idx = dr->cons;
613         int found = 0;
614 
615         while (idx != dr->prod) {
616                 if (idx == end) {
617                         found = 1;
618                         break;
619                 }
620                 idx = vio_dring_next(dr, idx);
621         }
622         return found;
623 }
624 
625 static int vnet_ack(struct vnet_port *port, void *msgbuf)
626 {
627         struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
628         struct vio_dring_data *pkt = msgbuf;
629         struct net_device *dev;
630         struct vnet *vp;
631         u32 end;
632         struct vio_net_desc *desc;
633         struct netdev_queue *txq;
634 
635         if (unlikely(pkt->tag.stype_env != VIO_DRING_DATA))
636                 return 0;
637 
638         end = pkt->end_idx;
639         vp = port->vp;
640         dev = vp->dev;
641         netif_tx_lock(dev);
642         if (unlikely(!idx_is_pending(dr, end))) {
643                 netif_tx_unlock(dev);
644                 return 0;
645         }
646 
647         /* sync for race conditions with vnet_start_xmit() and tell xmit it
648          * is time to send a trigger.
649          */
650         dr->cons = vio_dring_next(dr, end);
651         desc = vio_dring_entry(dr, dr->cons);
652         if (desc->hdr.state == VIO_DESC_READY && !port->start_cons) {
653                 /* vnet_start_xmit() just populated this dring but missed
654                  * sending the "start" LDC message to the consumer.
655                  * Send a "start" trigger on its behalf.
656                  */
657                 if (__vnet_tx_trigger(port, dr->cons) > 0)
658                         port->start_cons = false;
659                 else
660                         port->start_cons = true;
661         } else {
662                 port->start_cons = true;
663         }
664         netif_tx_unlock(dev);
665 
666         txq = netdev_get_tx_queue(dev, port->q_index);
667         if (unlikely(netif_tx_queue_stopped(txq) &&
668                      vnet_tx_dring_avail(dr) >= VNET_TX_WAKEUP_THRESH(dr)))
669                 return 1;
670 
671         return 0;
672 }
673 
674 static int vnet_nack(struct vnet_port *port, void *msgbuf)
675 {
676         /* XXX just reset or similar XXX */
677         return 0;
678 }
679 
680 static int handle_mcast(struct vnet_port *port, void *msgbuf)
681 {
682         struct vio_net_mcast_info *pkt = msgbuf;
683 
684         if (pkt->tag.stype != VIO_SUBTYPE_ACK)
685                 pr_err("%s: Got unexpected MCAST reply [%02x:%02x:%04x:%08x]\n",
686                        port->vp->dev->name,
687                        pkt->tag.type,
688                        pkt->tag.stype,
689                        pkt->tag.stype_env,
690                        pkt->tag.sid);
691 
692         return 0;
693 }
694 
695 /* Got back a STOPPED LDC message on port. If the queue is stopped,
696  * wake it up so that we'll send out another START message at the
697  * next TX.
698  */
699 static void maybe_tx_wakeup(struct vnet_port *port)
700 {
701         struct netdev_queue *txq;
702 
703         txq = netdev_get_tx_queue(port->vp->dev, port->q_index);
704         __netif_tx_lock(txq, smp_processor_id());
705         if (likely(netif_tx_queue_stopped(txq))) {
706                 struct vio_dring_state *dr;
707 
708                 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
709                 netif_tx_wake_queue(txq);
710         }
711         __netif_tx_unlock(txq);
712 }
713 
714 static inline bool port_is_up(struct vnet_port *vnet)
715 {
716         struct vio_driver_state *vio = &vnet->vio;
717 
718         return !!(vio->hs_state & VIO_HS_COMPLETE);
719 }
720 
721 static int vnet_event_napi(struct vnet_port *port, int budget)
722 {
723         struct vio_driver_state *vio = &port->vio;
724         int tx_wakeup, err;
725         int npkts = 0;
726         int event = (port->rx_event & LDC_EVENT_RESET);
727 
728 ldc_ctrl:
729         if (unlikely(event == LDC_EVENT_RESET ||
730                      event == LDC_EVENT_UP)) {
731                 vio_link_state_change(vio, event);
732 
733                 if (event == LDC_EVENT_RESET) {
734                         port->rmtu = 0;
735                         port->tso = true;
736                         port->tsolen = 0;
737                         vio_port_up(vio);
738                 }
739                 port->rx_event = 0;
740                 return 0;
741         }
742         /* We may have multiple LDC events in rx_event. Unroll send_events() */
743         event = (port->rx_event & LDC_EVENT_UP);
744         port->rx_event &= ~(LDC_EVENT_RESET|LDC_EVENT_UP);
745         if (event == LDC_EVENT_UP)
746                 goto ldc_ctrl;
747         event = port->rx_event;
748         if (!(event & LDC_EVENT_DATA_READY))
749                 return 0;
750 
751         /* we dont expect any other bits than RESET, UP, DATA_READY */
752         BUG_ON(event != LDC_EVENT_DATA_READY);
753 
754         tx_wakeup = err = 0;
755         while (1) {
756                 union {
757                         struct vio_msg_tag tag;
758                         u64 raw[8];
759                 } msgbuf;
760 
761                 if (port->napi_resume) {
762                         struct vio_dring_data *pkt =
763                                 (struct vio_dring_data *)&msgbuf;
764                         struct vio_dring_state *dr =
765                                 &port->vio.drings[VIO_DRIVER_RX_RING];
766 
767                         pkt->tag.type = VIO_TYPE_DATA;
768                         pkt->tag.stype = VIO_SUBTYPE_INFO;
769                         pkt->tag.stype_env = VIO_DRING_DATA;
770                         pkt->seq = dr->rcv_nxt;
771                         pkt->start_idx = vio_dring_next(dr, port->napi_stop_idx);
772                         pkt->end_idx = -1;
773                         goto napi_resume;
774                 }
775                 err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf));
776                 if (unlikely(err < 0)) {
777                         if (err == -ECONNRESET)
778                                 vio_conn_reset(vio);
779                         break;
780                 }
781                 if (err == 0)
782                         break;
783                 viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n",
784                        msgbuf.tag.type,
785                        msgbuf.tag.stype,
786                        msgbuf.tag.stype_env,
787                        msgbuf.tag.sid);
788                 err = vio_validate_sid(vio, &msgbuf.tag);
789                 if (err < 0)
790                         break;
791 napi_resume:
792                 if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) {
793                         if (msgbuf.tag.stype == VIO_SUBTYPE_INFO) {
794                                 if (!port_is_up(port)) {
795                                         /* failures like handshake_failure()
796                                          * may have cleaned up dring, but
797                                          * NAPI polling may bring us here.
798                                          */
799                                         err = -ECONNRESET;
800                                         break;
801                                 }
802                                 err = vnet_rx(port, &msgbuf, &npkts, budget);
803                                 if (npkts >= budget)
804                                         break;
805                                 if (npkts == 0)
806                                         break;
807                         } else if (msgbuf.tag.stype == VIO_SUBTYPE_ACK) {
808                                 err = vnet_ack(port, &msgbuf);
809                                 if (err > 0)
810                                         tx_wakeup |= err;
811                         } else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK) {
812                                 err = vnet_nack(port, &msgbuf);
813                         }
814                 } else if (msgbuf.tag.type == VIO_TYPE_CTRL) {
815                         if (msgbuf.tag.stype_env == VNET_MCAST_INFO)
816                                 err = handle_mcast(port, &msgbuf);
817                         else
818                                 err = vio_control_pkt_engine(vio, &msgbuf);
819                         if (err)
820                                 break;
821                 } else {
822                         err = vnet_handle_unknown(port, &msgbuf);
823                 }
824                 if (err == -ECONNRESET)
825                         break;
826         }
827         if (unlikely(tx_wakeup && err != -ECONNRESET))
828                 maybe_tx_wakeup(port);
829         return npkts;
830 }
831 
832 static int vnet_poll(struct napi_struct *napi, int budget)
833 {
834         struct vnet_port *port = container_of(napi, struct vnet_port, napi);
835         struct vio_driver_state *vio = &port->vio;
836         int processed = vnet_event_napi(port, budget);
837 
838         if (processed < budget) {
839                 napi_complete(napi);
840                 port->rx_event &= ~LDC_EVENT_DATA_READY;
841                 vio_set_intr(vio->vdev->rx_ino, HV_INTR_ENABLED);
842         }
843         return processed;
844 }
845 
846 static void vnet_event(void *arg, int event)
847 {
848         struct vnet_port *port = arg;
849         struct vio_driver_state *vio = &port->vio;
850 
851         port->rx_event |= event;
852         vio_set_intr(vio->vdev->rx_ino, HV_INTR_DISABLED);
853         napi_schedule(&port->napi);
854 
855 }
856 
857 static int __vnet_tx_trigger(struct vnet_port *port, u32 start)
858 {
859         struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
860         struct vio_dring_data hdr = {
861                 .tag = {
862                         .type           = VIO_TYPE_DATA,
863                         .stype          = VIO_SUBTYPE_INFO,
864                         .stype_env      = VIO_DRING_DATA,
865                         .sid            = vio_send_sid(&port->vio),
866                 },
867                 .dring_ident            = dr->ident,
868                 .start_idx              = start,
869                 .end_idx                = (u32) -1,
870         };
871         int err, delay;
872         int retries = 0;
873 
874         if (port->stop_rx) {
875                 err = vnet_send_ack(port,
876                                     &port->vio.drings[VIO_DRIVER_RX_RING],
877                                     port->stop_rx_idx, -1,
878                                     VIO_DRING_STOPPED);
879                 if (err <= 0)
880                         return err;
881         }
882 
883         hdr.seq = dr->snd_nxt;
884         delay = 1;
885         do {
886                 err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
887                 if (err > 0) {
888                         dr->snd_nxt++;
889                         break;
890                 }
891                 udelay(delay);
892                 if ((delay <<= 1) > 128)
893                         delay = 128;
894                 if (retries++ > VNET_MAX_RETRIES)
895                         break;
896         } while (err == -EAGAIN);
897 
898         return err;
899 }
900 
901 struct vnet_port *__tx_port_find(struct vnet *vp, struct sk_buff *skb)
902 {
903         unsigned int hash = vnet_hashfn(skb->data);
904         struct hlist_head *hp = &vp->port_hash[hash];
905         struct vnet_port *port;
906 
907         hlist_for_each_entry_rcu(port, hp, hash) {
908                 if (!port_is_up(port))
909                         continue;
910                 if (ether_addr_equal(port->raddr, skb->data))
911                         return port;
912         }
913         list_for_each_entry_rcu(port, &vp->port_list, list) {
914                 if (!port->switch_port)
915                         continue;
916                 if (!port_is_up(port))
917                         continue;
918                 return port;
919         }
920         return NULL;
921 }
922 
923 static struct sk_buff *vnet_clean_tx_ring(struct vnet_port *port,
924                                           unsigned *pending)
925 {
926         struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
927         struct sk_buff *skb = NULL;
928         int i, txi;
929 
930         *pending = 0;
931 
932         txi = dr->prod-1;
933         if (txi < 0)
934                 txi = VNET_TX_RING_SIZE-1;
935 
936         for (i = 0; i < VNET_TX_RING_SIZE; ++i) {
937                 struct vio_net_desc *d;
938 
939                 d = vio_dring_entry(dr, txi);
940 
941                 if (d->hdr.state == VIO_DESC_DONE) {
942                         if (port->tx_bufs[txi].skb) {
943                                 BUG_ON(port->tx_bufs[txi].skb->next);
944 
945                                 port->tx_bufs[txi].skb->next = skb;
946                                 skb = port->tx_bufs[txi].skb;
947                                 port->tx_bufs[txi].skb = NULL;
948 
949                                 ldc_unmap(port->vio.lp,
950                                           port->tx_bufs[txi].cookies,
951                                           port->tx_bufs[txi].ncookies);
952                         }
953                         d->hdr.state = VIO_DESC_FREE;
954                 } else if (d->hdr.state == VIO_DESC_READY) {
955                         (*pending)++;
956                 } else if (d->hdr.state == VIO_DESC_FREE) {
957                         break;
958                 }
959                 --txi;
960                 if (txi < 0)
961                         txi = VNET_TX_RING_SIZE-1;
962         }
963         return skb;
964 }
965 
966 static inline void vnet_free_skbs(struct sk_buff *skb)
967 {
968         struct sk_buff *next;
969 
970         while (skb) {
971                 next = skb->next;
972                 skb->next = NULL;
973                 dev_kfree_skb(skb);
974                 skb = next;
975         }
976 }
977 
978 static void vnet_clean_timer_expire(unsigned long port0)
979 {
980         struct vnet_port *port = (struct vnet_port *)port0;
981         struct sk_buff *freeskbs;
982         unsigned pending;
983 
984         netif_tx_lock(port->vp->dev);
985         freeskbs = vnet_clean_tx_ring(port, &pending);
986         netif_tx_unlock(port->vp->dev);
987 
988         vnet_free_skbs(freeskbs);
989 
990         if (pending)
991                 (void)mod_timer(&port->clean_timer,
992                                 jiffies + VNET_CLEAN_TIMEOUT);
993          else
994                 del_timer(&port->clean_timer);
995 }
996 
997 static inline int vnet_skb_map(struct ldc_channel *lp, struct sk_buff *skb,
998                                struct ldc_trans_cookie *cookies, int ncookies,
999                                unsigned int map_perm)
1000 {
1001         int i, nc, err, blen;
1002 
1003         /* header */
1004         blen = skb_headlen(skb);
1005         if (blen < ETH_ZLEN)
1006                 blen = ETH_ZLEN;
1007         blen += VNET_PACKET_SKIP;
1008         blen += 8 - (blen & 7);
1009 
1010         err = ldc_map_single(lp, skb->data-VNET_PACKET_SKIP, blen, cookies,
1011                              ncookies, map_perm);
1012         if (err < 0)
1013                 return err;
1014         nc = err;
1015 
1016         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1017                 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1018                 u8 *vaddr;
1019 
1020                 if (nc < ncookies) {
1021                         vaddr = kmap_atomic(skb_frag_page(f));
1022                         blen = skb_frag_size(f);
1023                         blen += 8 - (blen & 7);
1024                         err = ldc_map_single(lp, vaddr + f->page_offset,
1025                                              blen, cookies + nc, ncookies - nc,
1026                                              map_perm);
1027                         kunmap_atomic(vaddr);
1028                 } else {
1029                         err = -EMSGSIZE;
1030                 }
1031 
1032                 if (err < 0) {
1033                         ldc_unmap(lp, cookies, nc);
1034                         return err;
1035                 }
1036                 nc += err;
1037         }
1038         return nc;
1039 }
1040 
1041 static inline struct sk_buff *vnet_skb_shape(struct sk_buff *skb, int ncookies)
1042 {
1043         struct sk_buff *nskb;
1044         int i, len, pad, docopy;
1045 
1046         len = skb->len;
1047         pad = 0;
1048         if (len < ETH_ZLEN) {
1049                 pad += ETH_ZLEN - skb->len;
1050                 len += pad;
1051         }
1052         len += VNET_PACKET_SKIP;
1053         pad += 8 - (len & 7);
1054 
1055         /* make sure we have enough cookies and alignment in every frag */
1056         docopy = skb_shinfo(skb)->nr_frags >= ncookies;
1057         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1058                 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1059 
1060                 docopy |= f->page_offset & 7;
1061         }
1062         if (((unsigned long)skb->data & 7) != VNET_PACKET_SKIP ||
1063             skb_tailroom(skb) < pad ||
1064             skb_headroom(skb) < VNET_PACKET_SKIP || docopy) {
1065                 int start = 0, offset;
1066                 __wsum csum;
1067 
1068                 len = skb->len > ETH_ZLEN ? skb->len : ETH_ZLEN;
1069                 nskb = alloc_and_align_skb(skb->dev, len);
1070                 if (nskb == NULL) {
1071                         dev_kfree_skb(skb);
1072                         return NULL;
1073                 }
1074                 skb_reserve(nskb, VNET_PACKET_SKIP);
1075 
1076                 nskb->protocol = skb->protocol;
1077                 offset = skb_mac_header(skb) - skb->data;
1078                 skb_set_mac_header(nskb, offset);
1079                 offset = skb_network_header(skb) - skb->data;
1080                 skb_set_network_header(nskb, offset);
1081                 offset = skb_transport_header(skb) - skb->data;
1082                 skb_set_transport_header(nskb, offset);
1083 
1084                 offset = 0;
1085                 nskb->csum_offset = skb->csum_offset;
1086                 nskb->ip_summed = skb->ip_summed;
1087 
1088                 if (skb->ip_summed == CHECKSUM_PARTIAL)
1089                         start = skb_checksum_start_offset(skb);
1090                 if (start) {
1091                         struct iphdr *iph = ip_hdr(nskb);
1092                         int offset = start + nskb->csum_offset;
1093 
1094                         if (skb_copy_bits(skb, 0, nskb->data, start)) {
1095                                 dev_kfree_skb(nskb);
1096                                 dev_kfree_skb(skb);
1097                                 return NULL;
1098                         }
1099                         *(__sum16 *)(skb->data + offset) = 0;
1100                         csum = skb_copy_and_csum_bits(skb, start,
1101                                                       nskb->data + start,
1102                                                       skb->len - start, 0);
1103                         if (iph->protocol == IPPROTO_TCP ||
1104                             iph->protocol == IPPROTO_UDP) {
1105                                 csum = csum_tcpudp_magic(iph->saddr, iph->daddr,
1106                                                          skb->len - start,
1107                                                          iph->protocol, csum);
1108                         }
1109                         *(__sum16 *)(nskb->data + offset) = csum;
1110 
1111                         nskb->ip_summed = CHECKSUM_NONE;
1112                 } else if (skb_copy_bits(skb, 0, nskb->data, skb->len)) {
1113                         dev_kfree_skb(nskb);
1114                         dev_kfree_skb(skb);
1115                         return NULL;
1116                 }
1117                 (void)skb_put(nskb, skb->len);
1118                 if (skb_is_gso(skb)) {
1119                         skb_shinfo(nskb)->gso_size = skb_shinfo(skb)->gso_size;
1120                         skb_shinfo(nskb)->gso_type = skb_shinfo(skb)->gso_type;
1121                 }
1122                 nskb->queue_mapping = skb->queue_mapping;
1123                 dev_kfree_skb(skb);
1124                 skb = nskb;
1125         }
1126         return skb;
1127 }
1128 
1129 static u16
1130 vnet_select_queue(struct net_device *dev, struct sk_buff *skb,
1131                   void *accel_priv, select_queue_fallback_t fallback)
1132 {
1133         struct vnet *vp = netdev_priv(dev);
1134         struct vnet_port *port = __tx_port_find(vp, skb);
1135 
1136         if (port == NULL)
1137                 return 0;
1138         return port->q_index;
1139 }
1140 
1141 static int vnet_start_xmit(struct sk_buff *skb, struct net_device *dev);
1142 
1143 static int vnet_handle_offloads(struct vnet_port *port, struct sk_buff *skb)
1144 {
1145         struct net_device *dev = port->vp->dev;
1146         struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
1147         struct sk_buff *segs;
1148         int maclen, datalen;
1149         int status;
1150         int gso_size, gso_type, gso_segs;
1151         int hlen = skb_transport_header(skb) - skb_mac_header(skb);
1152         int proto = IPPROTO_IP;
1153 
1154         if (skb->protocol == htons(ETH_P_IP))
1155                 proto = ip_hdr(skb)->protocol;
1156         else if (skb->protocol == htons(ETH_P_IPV6))
1157                 proto = ipv6_hdr(skb)->nexthdr;
1158 
1159         if (proto == IPPROTO_TCP)
1160                 hlen += tcp_hdr(skb)->doff * 4;
1161         else if (proto == IPPROTO_UDP)
1162                 hlen += sizeof(struct udphdr);
1163         else {
1164                 pr_err("vnet_handle_offloads GSO with unknown transport "
1165                        "protocol %d tproto %d\n", skb->protocol, proto);
1166                 hlen = 128; /* XXX */
1167         }
1168         datalen = port->tsolen - hlen;
1169 
1170         gso_size = skb_shinfo(skb)->gso_size;
1171         gso_type = skb_shinfo(skb)->gso_type;
1172         gso_segs = skb_shinfo(skb)->gso_segs;
1173 
1174         if (port->tso && gso_size < datalen)
1175                 gso_segs = DIV_ROUND_UP(skb->len - hlen, datalen);
1176 
1177         if (unlikely(vnet_tx_dring_avail(dr) < gso_segs)) {
1178                 struct netdev_queue *txq;
1179 
1180                 txq  = netdev_get_tx_queue(dev, port->q_index);
1181                 netif_tx_stop_queue(txq);
1182                 if (vnet_tx_dring_avail(dr) < skb_shinfo(skb)->gso_segs)
1183                         return NETDEV_TX_BUSY;
1184                 netif_tx_wake_queue(txq);
1185         }
1186 
1187         maclen = skb_network_header(skb) - skb_mac_header(skb);
1188         skb_pull(skb, maclen);
1189 
1190         if (port->tso && gso_size < datalen) {
1191                 /* segment to TSO size */
1192                 skb_shinfo(skb)->gso_size = datalen;
1193                 skb_shinfo(skb)->gso_segs = gso_segs;
1194 
1195                 segs = skb_gso_segment(skb, dev->features & ~NETIF_F_TSO);
1196 
1197                 /* restore gso_size & gso_segs */
1198                 skb_shinfo(skb)->gso_size = gso_size;
1199                 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len - hlen,
1200                                                          gso_size);
1201         } else
1202                 segs = skb_gso_segment(skb, dev->features & ~NETIF_F_TSO);
1203         if (IS_ERR(segs)) {
1204                 dev->stats.tx_dropped++;
1205                 dev_kfree_skb_any(skb);
1206                 return NETDEV_TX_OK;
1207         }
1208 
1209         skb_push(skb, maclen);
1210         skb_reset_mac_header(skb);
1211 
1212         status = 0;
1213         while (segs) {
1214                 struct sk_buff *curr = segs;
1215 
1216                 segs = segs->next;
1217                 curr->next = NULL;
1218                 if (port->tso && curr->len > dev->mtu) {
1219                         skb_shinfo(curr)->gso_size = gso_size;
1220                         skb_shinfo(curr)->gso_type = gso_type;
1221                         skb_shinfo(curr)->gso_segs =
1222                                 DIV_ROUND_UP(curr->len - hlen, gso_size);
1223                 } else
1224                         skb_shinfo(curr)->gso_size = 0;
1225 
1226                 skb_push(curr, maclen);
1227                 skb_reset_mac_header(curr);
1228                 memcpy(skb_mac_header(curr), skb_mac_header(skb),
1229                        maclen);
1230                 curr->csum_start = skb_transport_header(curr) - curr->head;
1231                 if (ip_hdr(curr)->protocol == IPPROTO_TCP)
1232                         curr->csum_offset = offsetof(struct tcphdr, check);
1233                 else if (ip_hdr(curr)->protocol == IPPROTO_UDP)
1234                         curr->csum_offset = offsetof(struct udphdr, check);
1235 
1236                 if (!(status & NETDEV_TX_MASK))
1237                         status = vnet_start_xmit(curr, dev);
1238                 if (status & NETDEV_TX_MASK)
1239                         dev_kfree_skb_any(curr);
1240         }
1241 
1242         if (!(status & NETDEV_TX_MASK))
1243                 dev_kfree_skb_any(skb);
1244         return status;
1245 }
1246 
1247 static int vnet_start_xmit(struct sk_buff *skb, struct net_device *dev)
1248 {
1249         struct vnet *vp = netdev_priv(dev);
1250         struct vnet_port *port = NULL;
1251         struct vio_dring_state *dr;
1252         struct vio_net_desc *d;
1253         unsigned int len;
1254         struct sk_buff *freeskbs = NULL;
1255         int i, err, txi;
1256         unsigned pending = 0;
1257         struct netdev_queue *txq;
1258 
1259         rcu_read_lock();
1260         port = __tx_port_find(vp, skb);
1261         if (unlikely(!port)) {
1262                 rcu_read_unlock();
1263                 goto out_dropped;
1264         }
1265 
1266         if (skb_is_gso(skb) && skb->len > port->tsolen) {
1267                 err = vnet_handle_offloads(port, skb);
1268                 rcu_read_unlock();
1269                 return err;
1270         }
1271 
1272         if (!skb_is_gso(skb) && skb->len > port->rmtu) {
1273                 unsigned long localmtu = port->rmtu - ETH_HLEN;
1274 
1275                 if (vio_version_after_eq(&port->vio, 1, 3))
1276                         localmtu -= VLAN_HLEN;
1277 
1278                 if (skb->protocol == htons(ETH_P_IP)) {
1279                         struct flowi4 fl4;
1280                         struct rtable *rt = NULL;
1281 
1282                         memset(&fl4, 0, sizeof(fl4));
1283                         fl4.flowi4_oif = dev->ifindex;
1284                         fl4.flowi4_tos = RT_TOS(ip_hdr(skb)->tos);
1285                         fl4.daddr = ip_hdr(skb)->daddr;
1286                         fl4.saddr = ip_hdr(skb)->saddr;
1287 
1288                         rt = ip_route_output_key(dev_net(dev), &fl4);
1289                         rcu_read_unlock();
1290                         if (!IS_ERR(rt)) {
1291                                 skb_dst_set(skb, &rt->dst);
1292                                 icmp_send(skb, ICMP_DEST_UNREACH,
1293                                           ICMP_FRAG_NEEDED,
1294                                           htonl(localmtu));
1295                         }
1296                 }
1297 #if IS_ENABLED(CONFIG_IPV6)
1298                 else if (skb->protocol == htons(ETH_P_IPV6))
1299                         icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, localmtu);
1300 #endif
1301                 goto out_dropped;
1302         }
1303 
1304         skb = vnet_skb_shape(skb, 2);
1305 
1306         if (unlikely(!skb))
1307                 goto out_dropped;
1308 
1309         if (skb->ip_summed == CHECKSUM_PARTIAL)
1310                 vnet_fullcsum(skb);
1311 
1312         dr = &port->vio.drings[VIO_DRIVER_TX_RING];
1313         i = skb_get_queue_mapping(skb);
1314         txq = netdev_get_tx_queue(dev, i);
1315         if (unlikely(vnet_tx_dring_avail(dr) < 1)) {
1316                 if (!netif_tx_queue_stopped(txq)) {
1317                         netif_tx_stop_queue(txq);
1318 
1319                         /* This is a hard error, log it. */
1320                         netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
1321                         dev->stats.tx_errors++;
1322                 }
1323                 rcu_read_unlock();
1324                 return NETDEV_TX_BUSY;
1325         }
1326 
1327         d = vio_dring_cur(dr);
1328 
1329         txi = dr->prod;
1330 
1331         freeskbs = vnet_clean_tx_ring(port, &pending);
1332 
1333         BUG_ON(port->tx_bufs[txi].skb);
1334 
1335         len = skb->len;
1336         if (len < ETH_ZLEN)
1337                 len = ETH_ZLEN;
1338 
1339         err = vnet_skb_map(port->vio.lp, skb, port->tx_bufs[txi].cookies, 2,
1340                            (LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_RW));
1341         if (err < 0) {
1342                 netdev_info(dev, "tx buffer map error %d\n", err);
1343                 goto out_dropped;
1344         }
1345 
1346         port->tx_bufs[txi].skb = skb;
1347         skb = NULL;
1348         port->tx_bufs[txi].ncookies = err;
1349 
1350         /* We don't rely on the ACKs to free the skb in vnet_start_xmit(),
1351          * thus it is safe to not set VIO_ACK_ENABLE for each transmission:
1352          * the protocol itself does not require it as long as the peer
1353          * sends a VIO_SUBTYPE_ACK for VIO_DRING_STOPPED.
1354          *
1355          * An ACK for every packet in the ring is expensive as the
1356          * sending of LDC messages is slow and affects performance.
1357          */
1358         d->hdr.ack = VIO_ACK_DISABLE;
1359         d->size = len;
1360         d->ncookies = port->tx_bufs[txi].ncookies;
1361         for (i = 0; i < d->ncookies; i++)
1362                 d->cookies[i] = port->tx_bufs[txi].cookies[i];
1363         if (vio_version_after_eq(&port->vio, 1, 7)) {
1364                 struct vio_net_dext *dext = vio_net_ext(d);
1365 
1366                 memset(dext, 0, sizeof(*dext));
1367                 if (skb_is_gso(port->tx_bufs[txi].skb)) {
1368                         dext->ipv4_lso_mss = skb_shinfo(port->tx_bufs[txi].skb)
1369                                              ->gso_size;
1370                         dext->flags |= VNET_PKT_IPV4_LSO;
1371                 }
1372                 if (vio_version_after_eq(&port->vio, 1, 8) &&
1373                     !port->switch_port) {
1374                         dext->flags |= VNET_PKT_HCK_IPV4_HDRCKSUM_OK;
1375                         dext->flags |= VNET_PKT_HCK_FULLCKSUM_OK;
1376                 }
1377         }
1378 
1379         /* This has to be a non-SMP write barrier because we are writing
1380          * to memory which is shared with the peer LDOM.
1381          */
1382         wmb();
1383 
1384         d->hdr.state = VIO_DESC_READY;
1385 
1386         /* Exactly one ldc "start" trigger (for dr->cons) needs to be sent
1387          * to notify the consumer that some descriptors are READY.
1388          * After that "start" trigger, no additional triggers are needed until
1389          * a DRING_STOPPED is received from the consumer. The dr->cons field
1390          * (set up by vnet_ack()) has the value of the next dring index
1391          * that has not yet been ack-ed. We send a "start" trigger here
1392          * if, and only if, start_cons is true (reset it afterward). Conversely,
1393          * vnet_ack() should check if the dring corresponding to cons
1394          * is marked READY, but start_cons was false.
1395          * If so, vnet_ack() should send out the missed "start" trigger.
1396          *
1397          * Note that the wmb() above makes sure the cookies et al. are
1398          * not globally visible before the VIO_DESC_READY, and that the
1399          * stores are ordered correctly by the compiler. The consumer will
1400          * not proceed until the VIO_DESC_READY is visible assuring that
1401          * the consumer does not observe anything related to descriptors
1402          * out of order. The HV trap from the LDC start trigger is the
1403          * producer to consumer announcement that work is available to the
1404          * consumer
1405          */
1406         if (!port->start_cons)
1407                 goto ldc_start_done; /* previous trigger suffices */
1408 
1409         err = __vnet_tx_trigger(port, dr->cons);
1410         if (unlikely(err < 0)) {
1411                 netdev_info(dev, "TX trigger error %d\n", err);
1412                 d->hdr.state = VIO_DESC_FREE;
1413                 dev->stats.tx_carrier_errors++;
1414                 goto out_dropped;
1415         }
1416 
1417 ldc_start_done:
1418         port->start_cons = false;
1419 
1420         dev->stats.tx_packets++;
1421         dev->stats.tx_bytes += port->tx_bufs[txi].skb->len;
1422 
1423         dr->prod = (dr->prod + 1) & (VNET_TX_RING_SIZE - 1);
1424         if (unlikely(vnet_tx_dring_avail(dr) < 1)) {
1425                 netif_tx_stop_queue(txq);
1426                 if (vnet_tx_dring_avail(dr) > VNET_TX_WAKEUP_THRESH(dr))
1427                         netif_tx_wake_queue(txq);
1428         }
1429 
1430         (void)mod_timer(&port->clean_timer, jiffies + VNET_CLEAN_TIMEOUT);
1431         rcu_read_unlock();
1432 
1433         vnet_free_skbs(freeskbs);
1434 
1435         return NETDEV_TX_OK;
1436 
1437 out_dropped:
1438         if (pending)
1439                 (void)mod_timer(&port->clean_timer,
1440                                 jiffies + VNET_CLEAN_TIMEOUT);
1441         else if (port)
1442                 del_timer(&port->clean_timer);
1443         if (port)
1444                 rcu_read_unlock();
1445         if (skb)
1446                 dev_kfree_skb(skb);
1447         vnet_free_skbs(freeskbs);
1448         dev->stats.tx_dropped++;
1449         return NETDEV_TX_OK;
1450 }
1451 
1452 static void vnet_tx_timeout(struct net_device *dev)
1453 {
1454         /* XXX Implement me XXX */
1455 }
1456 
1457 static int vnet_open(struct net_device *dev)
1458 {
1459         netif_carrier_on(dev);
1460         netif_tx_start_all_queues(dev);
1461 
1462         return 0;
1463 }
1464 
1465 static int vnet_close(struct net_device *dev)
1466 {
1467         netif_tx_stop_all_queues(dev);
1468         netif_carrier_off(dev);
1469 
1470         return 0;
1471 }
1472 
1473 static struct vnet_mcast_entry *__vnet_mc_find(struct vnet *vp, u8 *addr)
1474 {
1475         struct vnet_mcast_entry *m;
1476 
1477         for (m = vp->mcast_list; m; m = m->next) {
1478                 if (ether_addr_equal(m->addr, addr))
1479                         return m;
1480         }
1481         return NULL;
1482 }
1483 
1484 static void __update_mc_list(struct vnet *vp, struct net_device *dev)
1485 {
1486         struct netdev_hw_addr *ha;
1487 
1488         netdev_for_each_mc_addr(ha, dev) {
1489                 struct vnet_mcast_entry *m;
1490 
1491                 m = __vnet_mc_find(vp, ha->addr);
1492                 if (m) {
1493                         m->hit = 1;
1494                         continue;
1495                 }
1496 
1497                 if (!m) {
1498                         m = kzalloc(sizeof(*m), GFP_ATOMIC);
1499                         if (!m)
1500                                 continue;
1501                         memcpy(m->addr, ha->addr, ETH_ALEN);
1502                         m->hit = 1;
1503 
1504                         m->next = vp->mcast_list;
1505                         vp->mcast_list = m;
1506                 }
1507         }
1508 }
1509 
1510 static void __send_mc_list(struct vnet *vp, struct vnet_port *port)
1511 {
1512         struct vio_net_mcast_info info;
1513         struct vnet_mcast_entry *m, **pp;
1514         int n_addrs;
1515 
1516         memset(&info, 0, sizeof(info));
1517 
1518         info.tag.type = VIO_TYPE_CTRL;
1519         info.tag.stype = VIO_SUBTYPE_INFO;
1520         info.tag.stype_env = VNET_MCAST_INFO;
1521         info.tag.sid = vio_send_sid(&port->vio);
1522         info.set = 1;
1523 
1524         n_addrs = 0;
1525         for (m = vp->mcast_list; m; m = m->next) {
1526                 if (m->sent)
1527                         continue;
1528                 m->sent = 1;
1529                 memcpy(&info.mcast_addr[n_addrs * ETH_ALEN],
1530                        m->addr, ETH_ALEN);
1531                 if (++n_addrs == VNET_NUM_MCAST) {
1532                         info.count = n_addrs;
1533 
1534                         (void) vio_ldc_send(&port->vio, &info,
1535                                             sizeof(info));
1536                         n_addrs = 0;
1537                 }
1538         }
1539         if (n_addrs) {
1540                 info.count = n_addrs;
1541                 (void) vio_ldc_send(&port->vio, &info, sizeof(info));
1542         }
1543 
1544         info.set = 0;
1545 
1546         n_addrs = 0;
1547         pp = &vp->mcast_list;
1548         while ((m = *pp) != NULL) {
1549                 if (m->hit) {
1550                         m->hit = 0;
1551                         pp = &m->next;
1552                         continue;
1553                 }
1554 
1555                 memcpy(&info.mcast_addr[n_addrs * ETH_ALEN],
1556                        m->addr, ETH_ALEN);
1557                 if (++n_addrs == VNET_NUM_MCAST) {
1558                         info.count = n_addrs;
1559                         (void) vio_ldc_send(&port->vio, &info,
1560                                             sizeof(info));
1561                         n_addrs = 0;
1562                 }
1563 
1564                 *pp = m->next;
1565                 kfree(m);
1566         }
1567         if (n_addrs) {
1568                 info.count = n_addrs;
1569                 (void) vio_ldc_send(&port->vio, &info, sizeof(info));
1570         }
1571 }
1572 
1573 static void vnet_set_rx_mode(struct net_device *dev)
1574 {
1575         struct vnet *vp = netdev_priv(dev);
1576         struct vnet_port *port;
1577 
1578         rcu_read_lock();
1579         list_for_each_entry_rcu(port, &vp->port_list, list) {
1580 
1581                 if (port->switch_port) {
1582                         __update_mc_list(vp, dev);
1583                         __send_mc_list(vp, port);
1584                         break;
1585                 }
1586         }
1587         rcu_read_unlock();
1588 }
1589 
1590 static int vnet_change_mtu(struct net_device *dev, int new_mtu)
1591 {
1592         if (new_mtu < 68 || new_mtu > 65535)
1593                 return -EINVAL;
1594 
1595         dev->mtu = new_mtu;
1596         return 0;
1597 }
1598 
1599 static int vnet_set_mac_addr(struct net_device *dev, void *p)
1600 {
1601         return -EINVAL;
1602 }
1603 
1604 static void vnet_get_drvinfo(struct net_device *dev,
1605                              struct ethtool_drvinfo *info)
1606 {
1607         strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
1608         strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
1609 }
1610 
1611 static u32 vnet_get_msglevel(struct net_device *dev)
1612 {
1613         struct vnet *vp = netdev_priv(dev);
1614         return vp->msg_enable;
1615 }
1616 
1617 static void vnet_set_msglevel(struct net_device *dev, u32 value)
1618 {
1619         struct vnet *vp = netdev_priv(dev);
1620         vp->msg_enable = value;
1621 }
1622 
1623 static const struct ethtool_ops vnet_ethtool_ops = {
1624         .get_drvinfo            = vnet_get_drvinfo,
1625         .get_msglevel           = vnet_get_msglevel,
1626         .set_msglevel           = vnet_set_msglevel,
1627         .get_link               = ethtool_op_get_link,
1628 };
1629 
1630 static void vnet_port_free_tx_bufs(struct vnet_port *port)
1631 {
1632         struct vio_dring_state *dr;
1633         int i;
1634 
1635         dr = &port->vio.drings[VIO_DRIVER_TX_RING];
1636         if (dr->base) {
1637                 ldc_free_exp_dring(port->vio.lp, dr->base,
1638                                    (dr->entry_size * dr->num_entries),
1639                                    dr->cookies, dr->ncookies);
1640                 dr->base = NULL;
1641                 dr->entry_size = 0;
1642                 dr->num_entries = 0;
1643                 dr->pending = 0;
1644                 dr->ncookies = 0;
1645         }
1646 
1647         for (i = 0; i < VNET_TX_RING_SIZE; i++) {
1648                 struct vio_net_desc *d;
1649                 void *skb = port->tx_bufs[i].skb;
1650 
1651                 if (!skb)
1652                         continue;
1653 
1654                 d = vio_dring_entry(dr, i);
1655                 if (d->hdr.state == VIO_DESC_READY)
1656                         pr_warn("active transmit buffers freed\n");
1657 
1658                 ldc_unmap(port->vio.lp,
1659                           port->tx_bufs[i].cookies,
1660                           port->tx_bufs[i].ncookies);
1661                 dev_kfree_skb(skb);
1662                 port->tx_bufs[i].skb = NULL;
1663                 d->hdr.state = VIO_DESC_FREE;
1664         }
1665 }
1666 
1667 static int vnet_port_alloc_tx_ring(struct vnet_port *port)
1668 {
1669         struct vio_dring_state *dr;
1670         unsigned long len, elen;
1671         int i, err, ncookies;
1672         void *dring;
1673 
1674         dr = &port->vio.drings[VIO_DRIVER_TX_RING];
1675 
1676         elen = sizeof(struct vio_net_desc) +
1677                sizeof(struct ldc_trans_cookie) * 2;
1678         if (vio_version_after_eq(&port->vio, 1, 7))
1679                 elen += sizeof(struct vio_net_dext);
1680         len = VNET_TX_RING_SIZE * elen;
1681 
1682         ncookies = VIO_MAX_RING_COOKIES;
1683         dring = ldc_alloc_exp_dring(port->vio.lp, len,
1684                                     dr->cookies, &ncookies,
1685                                     (LDC_MAP_SHADOW |
1686                                      LDC_MAP_DIRECT |
1687                                      LDC_MAP_RW));
1688         if (IS_ERR(dring)) {
1689                 err = PTR_ERR(dring);
1690                 goto err_out;
1691         }
1692 
1693         dr->base = dring;
1694         dr->entry_size = elen;
1695         dr->num_entries = VNET_TX_RING_SIZE;
1696         dr->prod = dr->cons = 0;
1697         port->start_cons  = true; /* need an initial trigger */
1698         dr->pending = VNET_TX_RING_SIZE;
1699         dr->ncookies = ncookies;
1700 
1701         for (i = 0; i < VNET_TX_RING_SIZE; ++i) {
1702                 struct vio_net_desc *d;
1703 
1704                 d = vio_dring_entry(dr, i);
1705                 d->hdr.state = VIO_DESC_FREE;
1706         }
1707         return 0;
1708 
1709 err_out:
1710         vnet_port_free_tx_bufs(port);
1711 
1712         return err;
1713 }
1714 
1715 #ifdef CONFIG_NET_POLL_CONTROLLER
1716 static void vnet_poll_controller(struct net_device *dev)
1717 {
1718         struct vnet *vp = netdev_priv(dev);
1719         struct vnet_port *port;
1720         unsigned long flags;
1721 
1722         spin_lock_irqsave(&vp->lock, flags);
1723         if (!list_empty(&vp->port_list)) {
1724                 port = list_entry(vp->port_list.next, struct vnet_port, list);
1725                 napi_schedule(&port->napi);
1726         }
1727         spin_unlock_irqrestore(&vp->lock, flags);
1728 }
1729 #endif
1730 static LIST_HEAD(vnet_list);
1731 static DEFINE_MUTEX(vnet_list_mutex);
1732 
1733 static const struct net_device_ops vnet_ops = {
1734         .ndo_open               = vnet_open,
1735         .ndo_stop               = vnet_close,
1736         .ndo_set_rx_mode        = vnet_set_rx_mode,
1737         .ndo_set_mac_address    = vnet_set_mac_addr,
1738         .ndo_validate_addr      = eth_validate_addr,
1739         .ndo_tx_timeout         = vnet_tx_timeout,
1740         .ndo_change_mtu         = vnet_change_mtu,
1741         .ndo_start_xmit         = vnet_start_xmit,
1742         .ndo_select_queue       = vnet_select_queue,
1743 #ifdef CONFIG_NET_POLL_CONTROLLER
1744         .ndo_poll_controller    = vnet_poll_controller,
1745 #endif
1746 };
1747 
1748 static struct vnet *vnet_new(const u64 *local_mac)
1749 {
1750         struct net_device *dev;
1751         struct vnet *vp;
1752         int err, i;
1753 
1754         dev = alloc_etherdev_mqs(sizeof(*vp), VNET_MAX_TXQS, 1);
1755         if (!dev)
1756                 return ERR_PTR(-ENOMEM);
1757         dev->needed_headroom = VNET_PACKET_SKIP + 8;
1758         dev->needed_tailroom = 8;
1759 
1760         for (i = 0; i < ETH_ALEN; i++)
1761                 dev->dev_addr[i] = (*local_mac >> (5 - i) * 8) & 0xff;
1762 
1763         vp = netdev_priv(dev);
1764 
1765         spin_lock_init(&vp->lock);
1766         vp->dev = dev;
1767 
1768         INIT_LIST_HEAD(&vp->port_list);
1769         for (i = 0; i < VNET_PORT_HASH_SIZE; i++)
1770                 INIT_HLIST_HEAD(&vp->port_hash[i]);
1771         INIT_LIST_HEAD(&vp->list);
1772         vp->local_mac = *local_mac;
1773 
1774         dev->netdev_ops = &vnet_ops;
1775         dev->ethtool_ops = &vnet_ethtool_ops;
1776         dev->watchdog_timeo = VNET_TX_TIMEOUT;
1777 
1778         dev->hw_features = NETIF_F_TSO | NETIF_F_GSO | NETIF_F_GSO_SOFTWARE |
1779                            NETIF_F_HW_CSUM | NETIF_F_SG;
1780         dev->features = dev->hw_features;
1781 
1782         err = register_netdev(dev);
1783         if (err) {
1784                 pr_err("Cannot register net device, aborting\n");
1785                 goto err_out_free_dev;
1786         }
1787 
1788         netdev_info(dev, "Sun LDOM vnet %pM\n", dev->dev_addr);
1789 
1790         list_add(&vp->list, &vnet_list);
1791 
1792         return vp;
1793 
1794 err_out_free_dev:
1795         free_netdev(dev);
1796 
1797         return ERR_PTR(err);
1798 }
1799 
1800 static struct vnet *vnet_find_or_create(const u64 *local_mac)
1801 {
1802         struct vnet *iter, *vp;
1803 
1804         mutex_lock(&vnet_list_mutex);
1805         vp = NULL;
1806         list_for_each_entry(iter, &vnet_list, list) {
1807                 if (iter->local_mac == *local_mac) {
1808                         vp = iter;
1809                         break;
1810                 }
1811         }
1812         if (!vp)
1813                 vp = vnet_new(local_mac);
1814         mutex_unlock(&vnet_list_mutex);
1815 
1816         return vp;
1817 }
1818 
1819 static void vnet_cleanup(void)
1820 {
1821         struct vnet *vp;
1822         struct net_device *dev;
1823 
1824         mutex_lock(&vnet_list_mutex);
1825         while (!list_empty(&vnet_list)) {
1826                 vp = list_first_entry(&vnet_list, struct vnet, list);
1827                 list_del(&vp->list);
1828                 dev = vp->dev;
1829                 /* vio_unregister_driver() should have cleaned up port_list */
1830                 BUG_ON(!list_empty(&vp->port_list));
1831                 unregister_netdev(dev);
1832                 free_netdev(dev);
1833         }
1834         mutex_unlock(&vnet_list_mutex);
1835 }
1836 
1837 static const char *local_mac_prop = "local-mac-address";
1838 
1839 static struct vnet *vnet_find_parent(struct mdesc_handle *hp,
1840                                                 u64 port_node)
1841 {
1842         const u64 *local_mac = NULL;
1843         u64 a;
1844 
1845         mdesc_for_each_arc(a, hp, port_node, MDESC_ARC_TYPE_BACK) {
1846                 u64 target = mdesc_arc_target(hp, a);
1847                 const char *name;
1848 
1849                 name = mdesc_get_property(hp, target, "name", NULL);
1850                 if (!name || strcmp(name, "network"))
1851                         continue;
1852 
1853                 local_mac = mdesc_get_property(hp, target,
1854                                                local_mac_prop, NULL);
1855                 if (local_mac)
1856                         break;
1857         }
1858         if (!local_mac)
1859                 return ERR_PTR(-ENODEV);
1860 
1861         return vnet_find_or_create(local_mac);
1862 }
1863 
1864 static struct ldc_channel_config vnet_ldc_cfg = {
1865         .event          = vnet_event,
1866         .mtu            = 64,
1867         .mode           = LDC_MODE_UNRELIABLE,
1868 };
1869 
1870 static struct vio_driver_ops vnet_vio_ops = {
1871         .send_attr              = vnet_send_attr,
1872         .handle_attr            = vnet_handle_attr,
1873         .handshake_complete     = vnet_handshake_complete,
1874 };
1875 
1876 static void print_version(void)
1877 {
1878         printk_once(KERN_INFO "%s", version);
1879 }
1880 
1881 const char *remote_macaddr_prop = "remote-mac-address";
1882 
1883 static void
1884 vnet_port_add_txq(struct vnet_port *port)
1885 {
1886         struct vnet *vp = port->vp;
1887         int n;
1888 
1889         n = vp->nports++;
1890         n = n & (VNET_MAX_TXQS - 1);
1891         port->q_index = n;
1892         netif_tx_wake_queue(netdev_get_tx_queue(vp->dev, port->q_index));
1893 }
1894 
1895 static void
1896 vnet_port_rm_txq(struct vnet_port *port)
1897 {
1898         port->vp->nports--;
1899         netif_tx_stop_queue(netdev_get_tx_queue(port->vp->dev, port->q_index));
1900 }
1901 
1902 static int vnet_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
1903 {
1904         struct mdesc_handle *hp;
1905         struct vnet_port *port;
1906         unsigned long flags;
1907         struct vnet *vp;
1908         const u64 *rmac;
1909         int len, i, err, switch_port;
1910 
1911         print_version();
1912 
1913         hp = mdesc_grab();
1914 
1915         vp = vnet_find_parent(hp, vdev->mp);
1916         if (IS_ERR(vp)) {
1917                 pr_err("Cannot find port parent vnet\n");
1918                 err = PTR_ERR(vp);
1919                 goto err_out_put_mdesc;
1920         }
1921 
1922         rmac = mdesc_get_property(hp, vdev->mp, remote_macaddr_prop, &len);
1923         err = -ENODEV;
1924         if (!rmac) {
1925                 pr_err("Port lacks %s property\n", remote_macaddr_prop);
1926                 goto err_out_put_mdesc;
1927         }
1928 
1929         port = kzalloc(sizeof(*port), GFP_KERNEL);
1930         err = -ENOMEM;
1931         if (!port)
1932                 goto err_out_put_mdesc;
1933 
1934         for (i = 0; i < ETH_ALEN; i++)
1935                 port->raddr[i] = (*rmac >> (5 - i) * 8) & 0xff;
1936 
1937         port->vp = vp;
1938 
1939         err = vio_driver_init(&port->vio, vdev, VDEV_NETWORK,
1940                               vnet_versions, ARRAY_SIZE(vnet_versions),
1941                               &vnet_vio_ops, vp->dev->name);
1942         if (err)
1943                 goto err_out_free_port;
1944 
1945         err = vio_ldc_alloc(&port->vio, &vnet_ldc_cfg, port);
1946         if (err)
1947                 goto err_out_free_port;
1948 
1949         netif_napi_add(port->vp->dev, &port->napi, vnet_poll, NAPI_POLL_WEIGHT);
1950 
1951         INIT_HLIST_NODE(&port->hash);
1952         INIT_LIST_HEAD(&port->list);
1953 
1954         switch_port = 0;
1955         if (mdesc_get_property(hp, vdev->mp, "switch-port", NULL) != NULL)
1956                 switch_port = 1;
1957         port->switch_port = switch_port;
1958         port->tso = true;
1959         port->tsolen = 0;
1960 
1961         spin_lock_irqsave(&vp->lock, flags);
1962         if (switch_port)
1963                 list_add_rcu(&port->list, &vp->port_list);
1964         else
1965                 list_add_tail_rcu(&port->list, &vp->port_list);
1966         hlist_add_head_rcu(&port->hash,
1967                            &vp->port_hash[vnet_hashfn(port->raddr)]);
1968         vnet_port_add_txq(port);
1969         spin_unlock_irqrestore(&vp->lock, flags);
1970 
1971         dev_set_drvdata(&vdev->dev, port);
1972 
1973         pr_info("%s: PORT ( remote-mac %pM%s )\n",
1974                 vp->dev->name, port->raddr, switch_port ? " switch-port" : "");
1975 
1976         setup_timer(&port->clean_timer, vnet_clean_timer_expire,
1977                     (unsigned long)port);
1978 
1979         napi_enable(&port->napi);
1980         vio_port_up(&port->vio);
1981 
1982         mdesc_release(hp);
1983 
1984         return 0;
1985 
1986 err_out_free_port:
1987         kfree(port);
1988 
1989 err_out_put_mdesc:
1990         mdesc_release(hp);
1991         return err;
1992 }
1993 
1994 static int vnet_port_remove(struct vio_dev *vdev)
1995 {
1996         struct vnet_port *port = dev_get_drvdata(&vdev->dev);
1997 
1998         if (port) {
1999 
2000                 del_timer_sync(&port->vio.timer);
2001 
2002                 napi_disable(&port->napi);
2003 
2004                 list_del_rcu(&port->list);
2005                 hlist_del_rcu(&port->hash);
2006 
2007                 synchronize_rcu();
2008                 del_timer_sync(&port->clean_timer);
2009                 vnet_port_rm_txq(port);
2010                 netif_napi_del(&port->napi);
2011                 vnet_port_free_tx_bufs(port);
2012                 vio_ldc_free(&port->vio);
2013 
2014                 dev_set_drvdata(&vdev->dev, NULL);
2015 
2016                 kfree(port);
2017 
2018         }
2019         return 0;
2020 }
2021 
2022 static const struct vio_device_id vnet_port_match[] = {
2023         {
2024                 .type = "vnet-port",
2025         },
2026         {},
2027 };
2028 MODULE_DEVICE_TABLE(vio, vnet_port_match);
2029 
2030 static struct vio_driver vnet_port_driver = {
2031         .id_table       = vnet_port_match,
2032         .probe          = vnet_port_probe,
2033         .remove         = vnet_port_remove,
2034         .name           = "vnet_port",
2035 };
2036 
2037 static int __init vnet_init(void)
2038 {
2039         return vio_register_driver(&vnet_port_driver);
2040 }
2041 
2042 static void __exit vnet_exit(void)
2043 {
2044         vio_unregister_driver(&vnet_port_driver);
2045         vnet_cleanup();
2046 }
2047 
2048 module_init(vnet_init);
2049 module_exit(vnet_exit);
2050 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us