sunvnet_common.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* sunvnet.c: Sun LDOM Virtual Network Driver.
  3. *
  4. * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
  5. * Copyright (C) 2016-2017 Oracle. All rights reserved.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/kernel.h>
  9. #include <linux/types.h>
  10. #include <linux/slab.h>
  11. #include <linux/delay.h>
  12. #include <linux/init.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/ethtool.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/mutex.h>
  17. #include <linux/highmem.h>
  18. #include <linux/if_vlan.h>
  19. #define CREATE_TRACE_POINTS
  20. #include <trace/events/sunvnet.h>
  21. #if IS_ENABLED(CONFIG_IPV6)
  22. #include <linux/icmpv6.h>
  23. #endif
  24. #include <net/ip.h>
  25. #include <net/icmp.h>
  26. #include <net/route.h>
  27. #include <asm/vio.h>
  28. #include <asm/ldc.h>
  29. #include "sunvnet_common.h"
  30. /* Heuristic for the number of times to exponentially backoff and
  31. * retry sending an LDC trigger when EAGAIN is encountered
  32. */
  33. #define VNET_MAX_RETRIES 10
  34. MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
  35. MODULE_DESCRIPTION("Sun LDOM virtual network support library");
  36. MODULE_LICENSE("GPL");
  37. MODULE_VERSION("1.1");
  38. static int __vnet_tx_trigger(struct vnet_port *port, u32 start);
  39. static inline u32 vnet_tx_dring_avail(struct vio_dring_state *dr)
  40. {
  41. return vio_dring_avail(dr, VNET_TX_RING_SIZE);
  42. }
  43. static int vnet_handle_unknown(struct vnet_port *port, void *arg)
  44. {
  45. struct vio_msg_tag *pkt = arg;
  46. pr_err("Received unknown msg [%02x:%02x:%04x:%08x]\n",
  47. pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
  48. pr_err("Resetting connection\n");
  49. ldc_disconnect(port->vio.lp);
  50. return -ECONNRESET;
  51. }
  52. static int vnet_port_alloc_tx_ring(struct vnet_port *port);
  53. int sunvnet_send_attr_common(struct vio_driver_state *vio)
  54. {
  55. struct vnet_port *port = to_vnet_port(vio);
  56. struct net_device *dev = VNET_PORT_TO_NET_DEVICE(port);
  57. struct vio_net_attr_info pkt;
  58. int framelen = ETH_FRAME_LEN;
  59. int i, err;
  60. err = vnet_port_alloc_tx_ring(to_vnet_port(vio));
  61. if (err)
  62. return err;
  63. memset(&pkt, 0, sizeof(pkt));
  64. pkt.tag.type = VIO_TYPE_CTRL;
  65. pkt.tag.stype = VIO_SUBTYPE_INFO;
  66. pkt.tag.stype_env = VIO_ATTR_INFO;
  67. pkt.tag.sid = vio_send_sid(vio);
  68. if (vio_version_before(vio, 1, 2))
  69. pkt.xfer_mode = VIO_DRING_MODE;
  70. else
  71. pkt.xfer_mode = VIO_NEW_DRING_MODE;
  72. pkt.addr_type = VNET_ADDR_ETHERMAC;
  73. pkt.ack_freq = 0;
  74. for (i = 0; i < 6; i++)
  75. pkt.addr |= (u64)dev->dev_addr[i] << ((5 - i) * 8);
  76. if (vio_version_after(vio, 1, 3)) {
  77. if (port->rmtu) {
  78. port->rmtu = min(VNET_MAXPACKET, port->rmtu);
  79. pkt.mtu = port->rmtu;
  80. } else {
  81. port->rmtu = VNET_MAXPACKET;
  82. pkt.mtu = port->rmtu;
  83. }
  84. if (vio_version_after_eq(vio, 1, 6))
  85. pkt.options = VIO_TX_DRING;
  86. } else if (vio_version_before(vio, 1, 3)) {
  87. pkt.mtu = framelen;
  88. } else { /* v1.3 */
  89. pkt.mtu = framelen + VLAN_HLEN;
  90. }
  91. pkt.cflags = 0;
  92. if (vio_version_after_eq(vio, 1, 7) && port->tso) {
  93. pkt.cflags |= VNET_LSO_IPV4_CAPAB;
  94. if (!port->tsolen)
  95. port->tsolen = VNET_MAXTSO;
  96. pkt.ipv4_lso_maxlen = port->tsolen;
  97. }
  98. pkt.plnk_updt = PHYSLINK_UPDATE_NONE;
  99. viodbg(HS, "SEND NET ATTR xmode[0x%x] atype[0x%x] addr[%llx] "
  100. "ackfreq[%u] plnk_updt[0x%02x] opts[0x%02x] mtu[%llu] "
  101. "cflags[0x%04x] lso_max[%u]\n",
  102. pkt.xfer_mode, pkt.addr_type,
  103. (unsigned long long)pkt.addr,
  104. pkt.ack_freq, pkt.plnk_updt, pkt.options,
  105. (unsigned long long)pkt.mtu, pkt.cflags, pkt.ipv4_lso_maxlen);
  106. return vio_ldc_send(vio, &pkt, sizeof(pkt));
  107. }
  108. EXPORT_SYMBOL_GPL(sunvnet_send_attr_common);
  109. static int handle_attr_info(struct vio_driver_state *vio,
  110. struct vio_net_attr_info *pkt)
  111. {
  112. struct vnet_port *port = to_vnet_port(vio);
  113. u64 localmtu;
  114. u8 xfer_mode;
  115. viodbg(HS, "GOT NET ATTR xmode[0x%x] atype[0x%x] addr[%llx] "
  116. "ackfreq[%u] plnk_updt[0x%02x] opts[0x%02x] mtu[%llu] "
  117. " (rmtu[%llu]) cflags[0x%04x] lso_max[%u]\n",
  118. pkt->xfer_mode, pkt->addr_type,
  119. (unsigned long long)pkt->addr,
  120. pkt->ack_freq, pkt->plnk_updt, pkt->options,
  121. (unsigned long long)pkt->mtu, port->rmtu, pkt->cflags,
  122. pkt->ipv4_lso_maxlen);
  123. pkt->tag.sid = vio_send_sid(vio);
  124. xfer_mode = pkt->xfer_mode;
  125. /* for version < 1.2, VIO_DRING_MODE = 0x3 and no bitmask */
  126. if (vio_version_before(vio, 1, 2) && xfer_mode == VIO_DRING_MODE)
  127. xfer_mode = VIO_NEW_DRING_MODE;
  128. /* MTU negotiation:
  129. * < v1.3 - ETH_FRAME_LEN exactly
  130. * > v1.3 - MIN(pkt.mtu, VNET_MAXPACKET, port->rmtu) and change
  131. * pkt->mtu for ACK
  132. * = v1.3 - ETH_FRAME_LEN + VLAN_HLEN exactly
  133. */
  134. if (vio_version_before(vio, 1, 3)) {
  135. localmtu = ETH_FRAME_LEN;
  136. } else if (vio_version_after(vio, 1, 3)) {
  137. localmtu = port->rmtu ? port->rmtu : VNET_MAXPACKET;
  138. localmtu = min(pkt->mtu, localmtu);
  139. pkt->mtu = localmtu;
  140. } else { /* v1.3 */
  141. localmtu = ETH_FRAME_LEN + VLAN_HLEN;
  142. }
  143. port->rmtu = localmtu;
  144. /* LSO negotiation */
  145. if (vio_version_after_eq(vio, 1, 7))
  146. port->tso &= !!(pkt->cflags & VNET_LSO_IPV4_CAPAB);
  147. else
  148. port->tso = false;
  149. if (port->tso) {
  150. if (!port->tsolen)
  151. port->tsolen = VNET_MAXTSO;
  152. port->tsolen = min(port->tsolen, pkt->ipv4_lso_maxlen);
  153. if (port->tsolen < VNET_MINTSO) {
  154. port->tso = false;
  155. port->tsolen = 0;
  156. pkt->cflags &= ~VNET_LSO_IPV4_CAPAB;
  157. }
  158. pkt->ipv4_lso_maxlen = port->tsolen;
  159. } else {
  160. pkt->cflags &= ~VNET_LSO_IPV4_CAPAB;
  161. pkt->ipv4_lso_maxlen = 0;
  162. port->tsolen = 0;
  163. }
  164. /* for version >= 1.6, ACK packet mode we support */
  165. if (vio_version_after_eq(vio, 1, 6)) {
  166. pkt->xfer_mode = VIO_NEW_DRING_MODE;
  167. pkt->options = VIO_TX_DRING;
  168. }
  169. if (!(xfer_mode | VIO_NEW_DRING_MODE) ||
  170. pkt->addr_type != VNET_ADDR_ETHERMAC ||
  171. pkt->mtu != localmtu) {
  172. viodbg(HS, "SEND NET ATTR NACK\n");
  173. pkt->tag.stype = VIO_SUBTYPE_NACK;
  174. (void)vio_ldc_send(vio, pkt, sizeof(*pkt));
  175. return -ECONNRESET;
  176. }
  177. viodbg(HS, "SEND NET ATTR ACK xmode[0x%x] atype[0x%x] "
  178. "addr[%llx] ackfreq[%u] plnk_updt[0x%02x] opts[0x%02x] "
  179. "mtu[%llu] (rmtu[%llu]) cflags[0x%04x] lso_max[%u]\n",
  180. pkt->xfer_mode, pkt->addr_type,
  181. (unsigned long long)pkt->addr,
  182. pkt->ack_freq, pkt->plnk_updt, pkt->options,
  183. (unsigned long long)pkt->mtu, port->rmtu, pkt->cflags,
  184. pkt->ipv4_lso_maxlen);
  185. pkt->tag.stype = VIO_SUBTYPE_ACK;
  186. return vio_ldc_send(vio, pkt, sizeof(*pkt));
  187. }
  188. static int handle_attr_ack(struct vio_driver_state *vio,
  189. struct vio_net_attr_info *pkt)
  190. {
  191. viodbg(HS, "GOT NET ATTR ACK\n");
  192. return 0;
  193. }
  194. static int handle_attr_nack(struct vio_driver_state *vio,
  195. struct vio_net_attr_info *pkt)
  196. {
  197. viodbg(HS, "GOT NET ATTR NACK\n");
  198. return -ECONNRESET;
  199. }
  200. int sunvnet_handle_attr_common(struct vio_driver_state *vio, void *arg)
  201. {
  202. struct vio_net_attr_info *pkt = arg;
  203. switch (pkt->tag.stype) {
  204. case VIO_SUBTYPE_INFO:
  205. return handle_attr_info(vio, pkt);
  206. case VIO_SUBTYPE_ACK:
  207. return handle_attr_ack(vio, pkt);
  208. case VIO_SUBTYPE_NACK:
  209. return handle_attr_nack(vio, pkt);
  210. default:
  211. return -ECONNRESET;
  212. }
  213. }
  214. EXPORT_SYMBOL_GPL(sunvnet_handle_attr_common);
  215. void sunvnet_handshake_complete_common(struct vio_driver_state *vio)
  216. {
  217. struct vio_dring_state *dr;
  218. dr = &vio->drings[VIO_DRIVER_RX_RING];
  219. dr->rcv_nxt = 1;
  220. dr->snd_nxt = 1;
  221. dr = &vio->drings[VIO_DRIVER_TX_RING];
  222. dr->rcv_nxt = 1;
  223. dr->snd_nxt = 1;
  224. }
  225. EXPORT_SYMBOL_GPL(sunvnet_handshake_complete_common);
  226. /* The hypervisor interface that implements copying to/from imported
  227. * memory from another domain requires that copies are done to 8-byte
  228. * aligned buffers, and that the lengths of such copies are also 8-byte
  229. * multiples.
  230. *
  231. * So we align skb->data to an 8-byte multiple and pad-out the data
  232. * area so we can round the copy length up to the next multiple of
  233. * 8 for the copy.
  234. *
  235. * The transmitter puts the actual start of the packet 6 bytes into
  236. * the buffer it sends over, so that the IP headers after the ethernet
  237. * header are aligned properly. These 6 bytes are not in the descriptor
  238. * length, they are simply implied. This offset is represented using
  239. * the VNET_PACKET_SKIP macro.
  240. */
  241. static struct sk_buff *alloc_and_align_skb(struct net_device *dev,
  242. unsigned int len)
  243. {
  244. struct sk_buff *skb;
  245. unsigned long addr, off;
  246. skb = netdev_alloc_skb(dev, len + VNET_PACKET_SKIP + 8 + 8);
  247. if (unlikely(!skb))
  248. return NULL;
  249. addr = (unsigned long)skb->data;
  250. off = ((addr + 7UL) & ~7UL) - addr;
  251. if (off)
  252. skb_reserve(skb, off);
  253. return skb;
  254. }
  255. static inline void vnet_fullcsum_ipv4(struct sk_buff *skb)
  256. {
  257. struct iphdr *iph = ip_hdr(skb);
  258. int offset = skb_transport_offset(skb);
  259. if (skb->protocol != htons(ETH_P_IP))
  260. return;
  261. if (iph->protocol != IPPROTO_TCP &&
  262. iph->protocol != IPPROTO_UDP)
  263. return;
  264. skb->ip_summed = CHECKSUM_NONE;
  265. skb->csum_level = 1;
  266. skb->csum = 0;
  267. if (iph->protocol == IPPROTO_TCP) {
  268. struct tcphdr *ptcp = tcp_hdr(skb);
  269. ptcp->check = 0;
  270. skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
  271. ptcp->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
  272. skb->len - offset, IPPROTO_TCP,
  273. skb->csum);
  274. } else if (iph->protocol == IPPROTO_UDP) {
  275. struct udphdr *pudp = udp_hdr(skb);
  276. pudp->check = 0;
  277. skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
  278. pudp->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
  279. skb->len - offset, IPPROTO_UDP,
  280. skb->csum);
  281. }
  282. }
  283. #if IS_ENABLED(CONFIG_IPV6)
  284. static inline void vnet_fullcsum_ipv6(struct sk_buff *skb)
  285. {
  286. struct ipv6hdr *ip6h = ipv6_hdr(skb);
  287. int offset = skb_transport_offset(skb);
  288. if (skb->protocol != htons(ETH_P_IPV6))
  289. return;
  290. if (ip6h->nexthdr != IPPROTO_TCP &&
  291. ip6h->nexthdr != IPPROTO_UDP)
  292. return;
  293. skb->ip_summed = CHECKSUM_NONE;
  294. skb->csum_level = 1;
  295. skb->csum = 0;
  296. if (ip6h->nexthdr == IPPROTO_TCP) {
  297. struct tcphdr *ptcp = tcp_hdr(skb);
  298. ptcp->check = 0;
  299. skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
  300. ptcp->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  301. skb->len - offset, IPPROTO_TCP,
  302. skb->csum);
  303. } else if (ip6h->nexthdr == IPPROTO_UDP) {
  304. struct udphdr *pudp = udp_hdr(skb);
  305. pudp->check = 0;
  306. skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
  307. pudp->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  308. skb->len - offset, IPPROTO_UDP,
  309. skb->csum);
  310. }
  311. }
  312. #endif
  313. static int vnet_rx_one(struct vnet_port *port, struct vio_net_desc *desc)
  314. {
  315. struct net_device *dev = VNET_PORT_TO_NET_DEVICE(port);
  316. unsigned int len = desc->size;
  317. unsigned int copy_len;
  318. struct sk_buff *skb;
  319. int maxlen;
  320. int err;
  321. err = -EMSGSIZE;
  322. if (port->tso && port->tsolen > port->rmtu)
  323. maxlen = port->tsolen;
  324. else
  325. maxlen = port->rmtu;
  326. if (unlikely(len < ETH_ZLEN || len > maxlen)) {
  327. dev->stats.rx_length_errors++;
  328. goto out_dropped;
  329. }
  330. skb = alloc_and_align_skb(dev, len);
  331. err = -ENOMEM;
  332. if (unlikely(!skb)) {
  333. dev->stats.rx_missed_errors++;
  334. goto out_dropped;
  335. }
  336. copy_len = (len + VNET_PACKET_SKIP + 7U) & ~7U;
  337. skb_put(skb, copy_len);
  338. err = ldc_copy(port->vio.lp, LDC_COPY_IN,
  339. skb->data, copy_len, 0,
  340. desc->cookies, desc->ncookies);
  341. if (unlikely(err < 0)) {
  342. dev->stats.rx_frame_errors++;
  343. goto out_free_skb;
  344. }
  345. skb_pull(skb, VNET_PACKET_SKIP);
  346. skb_trim(skb, len);
  347. skb->protocol = eth_type_trans(skb, dev);
  348. if (vio_version_after_eq(&port->vio, 1, 8)) {
  349. struct vio_net_dext *dext = vio_net_ext(desc);
  350. skb_reset_network_header(skb);
  351. if (dext->flags & VNET_PKT_HCK_IPV4_HDRCKSUM) {
  352. if (skb->protocol == ETH_P_IP) {
  353. struct iphdr *iph = ip_hdr(skb);
  354. iph->check = 0;
  355. ip_send_check(iph);
  356. }
  357. }
  358. if ((dext->flags & VNET_PKT_HCK_FULLCKSUM) &&
  359. skb->ip_summed == CHECKSUM_NONE) {
  360. if (skb->protocol == htons(ETH_P_IP)) {
  361. struct iphdr *iph = ip_hdr(skb);
  362. int ihl = iph->ihl * 4;
  363. skb_set_transport_header(skb, ihl);
  364. vnet_fullcsum_ipv4(skb);
  365. #if IS_ENABLED(CONFIG_IPV6)
  366. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  367. skb_set_transport_header(skb,
  368. sizeof(struct ipv6hdr));
  369. vnet_fullcsum_ipv6(skb);
  370. #endif
  371. }
  372. }
  373. if (dext->flags & VNET_PKT_HCK_IPV4_HDRCKSUM_OK) {
  374. skb->ip_summed = CHECKSUM_PARTIAL;
  375. skb->csum_level = 0;
  376. if (dext->flags & VNET_PKT_HCK_FULLCKSUM_OK)
  377. skb->csum_level = 1;
  378. }
  379. }
  380. skb->ip_summed = port->switch_port ? CHECKSUM_NONE : CHECKSUM_PARTIAL;
  381. if (unlikely(is_multicast_ether_addr(eth_hdr(skb)->h_dest)))
  382. dev->stats.multicast++;
  383. dev->stats.rx_packets++;
  384. dev->stats.rx_bytes += len;
  385. port->stats.rx_packets++;
  386. port->stats.rx_bytes += len;
  387. napi_gro_receive(&port->napi, skb);
  388. return 0;
  389. out_free_skb:
  390. kfree_skb(skb);
  391. out_dropped:
  392. dev->stats.rx_dropped++;
  393. return err;
  394. }
  395. static int vnet_send_ack(struct vnet_port *port, struct vio_dring_state *dr,
  396. u32 start, u32 end, u8 vio_dring_state)
  397. {
  398. struct vio_dring_data hdr = {
  399. .tag = {
  400. .type = VIO_TYPE_DATA,
  401. .stype = VIO_SUBTYPE_ACK,
  402. .stype_env = VIO_DRING_DATA,
  403. .sid = vio_send_sid(&port->vio),
  404. },
  405. .dring_ident = dr->ident,
  406. .start_idx = start,
  407. .end_idx = end,
  408. .state = vio_dring_state,
  409. };
  410. int err, delay;
  411. int retries = 0;
  412. hdr.seq = dr->snd_nxt;
  413. delay = 1;
  414. do {
  415. err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
  416. if (err > 0) {
  417. dr->snd_nxt++;
  418. break;
  419. }
  420. udelay(delay);
  421. if ((delay <<= 1) > 128)
  422. delay = 128;
  423. if (retries++ > VNET_MAX_RETRIES) {
  424. pr_info("ECONNRESET %x:%x:%x:%x:%x:%x\n",
  425. port->raddr[0], port->raddr[1],
  426. port->raddr[2], port->raddr[3],
  427. port->raddr[4], port->raddr[5]);
  428. break;
  429. }
  430. } while (err == -EAGAIN);
  431. if (err <= 0 && vio_dring_state == VIO_DRING_STOPPED) {
  432. port->stop_rx_idx = end;
  433. port->stop_rx = true;
  434. } else {
  435. port->stop_rx_idx = 0;
  436. port->stop_rx = false;
  437. }
  438. return err;
  439. }
  440. static struct vio_net_desc *get_rx_desc(struct vnet_port *port,
  441. struct vio_dring_state *dr,
  442. u32 index)
  443. {
  444. struct vio_net_desc *desc = port->vio.desc_buf;
  445. int err;
  446. err = ldc_get_dring_entry(port->vio.lp, desc, dr->entry_size,
  447. (index * dr->entry_size),
  448. dr->cookies, dr->ncookies);
  449. if (err < 0)
  450. return ERR_PTR(err);
  451. return desc;
  452. }
  453. static int put_rx_desc(struct vnet_port *port,
  454. struct vio_dring_state *dr,
  455. struct vio_net_desc *desc,
  456. u32 index)
  457. {
  458. int err;
  459. err = ldc_put_dring_entry(port->vio.lp, desc, dr->entry_size,
  460. (index * dr->entry_size),
  461. dr->cookies, dr->ncookies);
  462. if (err < 0)
  463. return err;
  464. return 0;
  465. }
  466. static int vnet_walk_rx_one(struct vnet_port *port,
  467. struct vio_dring_state *dr,
  468. u32 index, int *needs_ack)
  469. {
  470. struct vio_net_desc *desc = get_rx_desc(port, dr, index);
  471. struct vio_driver_state *vio = &port->vio;
  472. int err;
  473. BUG_ON(!desc);
  474. if (IS_ERR(desc))
  475. return PTR_ERR(desc);
  476. if (desc->hdr.state != VIO_DESC_READY)
  477. return 1;
  478. dma_rmb();
  479. viodbg(DATA, "vio_walk_rx_one desc[%02x:%02x:%08x:%08x:%llx:%llx]\n",
  480. desc->hdr.state, desc->hdr.ack,
  481. desc->size, desc->ncookies,
  482. desc->cookies[0].cookie_addr,
  483. desc->cookies[0].cookie_size);
  484. err = vnet_rx_one(port, desc);
  485. if (err == -ECONNRESET)
  486. return err;
  487. trace_vnet_rx_one(port->vio._local_sid, port->vio._peer_sid,
  488. index, desc->hdr.ack);
  489. desc->hdr.state = VIO_DESC_DONE;
  490. err = put_rx_desc(port, dr, desc, index);
  491. if (err < 0)
  492. return err;
  493. *needs_ack = desc->hdr.ack;
  494. return 0;
  495. }
  496. static int vnet_walk_rx(struct vnet_port *port, struct vio_dring_state *dr,
  497. u32 start, u32 end, int *npkts, int budget)
  498. {
  499. struct vio_driver_state *vio = &port->vio;
  500. int ack_start = -1, ack_end = -1;
  501. bool send_ack = true;
  502. end = (end == (u32)-1) ? vio_dring_prev(dr, start)
  503. : vio_dring_next(dr, end);
  504. viodbg(DATA, "vnet_walk_rx start[%08x] end[%08x]\n", start, end);
  505. while (start != end) {
  506. int ack = 0, err = vnet_walk_rx_one(port, dr, start, &ack);
  507. if (err == -ECONNRESET)
  508. return err;
  509. if (err != 0)
  510. break;
  511. (*npkts)++;
  512. if (ack_start == -1)
  513. ack_start = start;
  514. ack_end = start;
  515. start = vio_dring_next(dr, start);
  516. if (ack && start != end) {
  517. err = vnet_send_ack(port, dr, ack_start, ack_end,
  518. VIO_DRING_ACTIVE);
  519. if (err == -ECONNRESET)
  520. return err;
  521. ack_start = -1;
  522. }
  523. if ((*npkts) >= budget) {
  524. send_ack = false;
  525. break;
  526. }
  527. }
  528. if (unlikely(ack_start == -1)) {
  529. ack_end = vio_dring_prev(dr, start);
  530. ack_start = ack_end;
  531. }
  532. if (send_ack) {
  533. port->napi_resume = false;
  534. trace_vnet_tx_send_stopped_ack(port->vio._local_sid,
  535. port->vio._peer_sid,
  536. ack_end, *npkts);
  537. return vnet_send_ack(port, dr, ack_start, ack_end,
  538. VIO_DRING_STOPPED);
  539. } else {
  540. trace_vnet_tx_defer_stopped_ack(port->vio._local_sid,
  541. port->vio._peer_sid,
  542. ack_end, *npkts);
  543. port->napi_resume = true;
  544. port->napi_stop_idx = ack_end;
  545. return 1;
  546. }
  547. }
  548. static int vnet_rx(struct vnet_port *port, void *msgbuf, int *npkts,
  549. int budget)
  550. {
  551. struct vio_dring_data *pkt = msgbuf;
  552. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_RX_RING];
  553. struct vio_driver_state *vio = &port->vio;
  554. viodbg(DATA, "vnet_rx stype_env[%04x] seq[%016llx] rcv_nxt[%016llx]\n",
  555. pkt->tag.stype_env, pkt->seq, dr->rcv_nxt);
  556. if (unlikely(pkt->tag.stype_env != VIO_DRING_DATA))
  557. return 0;
  558. if (unlikely(pkt->seq != dr->rcv_nxt)) {
  559. pr_err("RX out of sequence seq[0x%llx] rcv_nxt[0x%llx]\n",
  560. pkt->seq, dr->rcv_nxt);
  561. return 0;
  562. }
  563. if (!port->napi_resume)
  564. dr->rcv_nxt++;
  565. /* XXX Validate pkt->start_idx and pkt->end_idx XXX */
  566. return vnet_walk_rx(port, dr, pkt->start_idx, pkt->end_idx,
  567. npkts, budget);
  568. }
  569. static int idx_is_pending(struct vio_dring_state *dr, u32 end)
  570. {
  571. u32 idx = dr->cons;
  572. int found = 0;
  573. while (idx != dr->prod) {
  574. if (idx == end) {
  575. found = 1;
  576. break;
  577. }
  578. idx = vio_dring_next(dr, idx);
  579. }
  580. return found;
  581. }
  582. static int vnet_ack(struct vnet_port *port, void *msgbuf)
  583. {
  584. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  585. struct vio_dring_data *pkt = msgbuf;
  586. struct net_device *dev;
  587. u32 end;
  588. struct vio_net_desc *desc;
  589. struct netdev_queue *txq;
  590. if (unlikely(pkt->tag.stype_env != VIO_DRING_DATA))
  591. return 0;
  592. end = pkt->end_idx;
  593. dev = VNET_PORT_TO_NET_DEVICE(port);
  594. netif_tx_lock(dev);
  595. if (unlikely(!idx_is_pending(dr, end))) {
  596. netif_tx_unlock(dev);
  597. return 0;
  598. }
  599. /* sync for race conditions with vnet_start_xmit() and tell xmit it
  600. * is time to send a trigger.
  601. */
  602. trace_vnet_rx_stopped_ack(port->vio._local_sid,
  603. port->vio._peer_sid, end);
  604. dr->cons = vio_dring_next(dr, end);
  605. desc = vio_dring_entry(dr, dr->cons);
  606. if (desc->hdr.state == VIO_DESC_READY && !port->start_cons) {
  607. /* vnet_start_xmit() just populated this dring but missed
  608. * sending the "start" LDC message to the consumer.
  609. * Send a "start" trigger on its behalf.
  610. */
  611. if (__vnet_tx_trigger(port, dr->cons) > 0)
  612. port->start_cons = false;
  613. else
  614. port->start_cons = true;
  615. } else {
  616. port->start_cons = true;
  617. }
  618. netif_tx_unlock(dev);
  619. txq = netdev_get_tx_queue(dev, port->q_index);
  620. if (unlikely(netif_tx_queue_stopped(txq) &&
  621. vnet_tx_dring_avail(dr) >= VNET_TX_WAKEUP_THRESH(dr)))
  622. return 1;
  623. return 0;
  624. }
  625. static int vnet_nack(struct vnet_port *port, void *msgbuf)
  626. {
  627. /* XXX just reset or similar XXX */
  628. return 0;
  629. }
  630. static int handle_mcast(struct vnet_port *port, void *msgbuf)
  631. {
  632. struct vio_net_mcast_info *pkt = msgbuf;
  633. struct net_device *dev = VNET_PORT_TO_NET_DEVICE(port);
  634. if (pkt->tag.stype != VIO_SUBTYPE_ACK)
  635. pr_err("%s: Got unexpected MCAST reply [%02x:%02x:%04x:%08x]\n",
  636. dev->name,
  637. pkt->tag.type,
  638. pkt->tag.stype,
  639. pkt->tag.stype_env,
  640. pkt->tag.sid);
  641. return 0;
  642. }
  643. /* If the queue is stopped, wake it up so that we'll
  644. * send out another START message at the next TX.
  645. */
  646. static void maybe_tx_wakeup(struct vnet_port *port)
  647. {
  648. struct netdev_queue *txq;
  649. txq = netdev_get_tx_queue(VNET_PORT_TO_NET_DEVICE(port),
  650. port->q_index);
  651. __netif_tx_lock(txq, smp_processor_id());
  652. if (likely(netif_tx_queue_stopped(txq)))
  653. netif_tx_wake_queue(txq);
  654. __netif_tx_unlock(txq);
  655. }
  656. bool sunvnet_port_is_up_common(struct vnet_port *vnet)
  657. {
  658. struct vio_driver_state *vio = &vnet->vio;
  659. return !!(vio->hs_state & VIO_HS_COMPLETE);
  660. }
  661. EXPORT_SYMBOL_GPL(sunvnet_port_is_up_common);
  662. static int vnet_event_napi(struct vnet_port *port, int budget)
  663. {
  664. struct net_device *dev = VNET_PORT_TO_NET_DEVICE(port);
  665. struct vio_driver_state *vio = &port->vio;
  666. int tx_wakeup, err;
  667. int npkts = 0;
  668. /* we don't expect any other bits */
  669. BUG_ON(port->rx_event & ~(LDC_EVENT_DATA_READY |
  670. LDC_EVENT_RESET |
  671. LDC_EVENT_UP));
  672. /* RESET takes precedent over any other event */
  673. if (port->rx_event & LDC_EVENT_RESET) {
  674. /* a link went down */
  675. if (port->vsw == 1) {
  676. netif_tx_stop_all_queues(dev);
  677. netif_carrier_off(dev);
  678. }
  679. vio_link_state_change(vio, LDC_EVENT_RESET);
  680. vnet_port_reset(port);
  681. vio_port_up(vio);
  682. /* If the device is running but its tx queue was
  683. * stopped (due to flow control), restart it.
  684. * This is necessary since vnet_port_reset()
  685. * clears the tx drings and thus we may never get
  686. * back a VIO_TYPE_DATA ACK packet - which is
  687. * the normal mechanism to restart the tx queue.
  688. */
  689. if (netif_running(dev))
  690. maybe_tx_wakeup(port);
  691. port->rx_event = 0;
  692. port->stats.event_reset++;
  693. return 0;
  694. }
  695. if (port->rx_event & LDC_EVENT_UP) {
  696. /* a link came up */
  697. if (port->vsw == 1) {
  698. netif_carrier_on(port->dev);
  699. netif_tx_start_all_queues(port->dev);
  700. }
  701. vio_link_state_change(vio, LDC_EVENT_UP);
  702. port->rx_event = 0;
  703. port->stats.event_up++;
  704. return 0;
  705. }
  706. err = 0;
  707. tx_wakeup = 0;
  708. while (1) {
  709. union {
  710. struct vio_msg_tag tag;
  711. u64 raw[8];
  712. } msgbuf;
  713. if (port->napi_resume) {
  714. struct vio_dring_data *pkt =
  715. (struct vio_dring_data *)&msgbuf;
  716. struct vio_dring_state *dr =
  717. &port->vio.drings[VIO_DRIVER_RX_RING];
  718. pkt->tag.type = VIO_TYPE_DATA;
  719. pkt->tag.stype = VIO_SUBTYPE_INFO;
  720. pkt->tag.stype_env = VIO_DRING_DATA;
  721. pkt->seq = dr->rcv_nxt;
  722. pkt->start_idx = vio_dring_next(dr,
  723. port->napi_stop_idx);
  724. pkt->end_idx = -1;
  725. } else {
  726. err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf));
  727. if (unlikely(err < 0)) {
  728. if (err == -ECONNRESET)
  729. vio_conn_reset(vio);
  730. break;
  731. }
  732. if (err == 0)
  733. break;
  734. viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n",
  735. msgbuf.tag.type,
  736. msgbuf.tag.stype,
  737. msgbuf.tag.stype_env,
  738. msgbuf.tag.sid);
  739. err = vio_validate_sid(vio, &msgbuf.tag);
  740. if (err < 0)
  741. break;
  742. }
  743. if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) {
  744. if (msgbuf.tag.stype == VIO_SUBTYPE_INFO) {
  745. if (!sunvnet_port_is_up_common(port)) {
  746. /* failures like handshake_failure()
  747. * may have cleaned up dring, but
  748. * NAPI polling may bring us here.
  749. */
  750. err = -ECONNRESET;
  751. break;
  752. }
  753. err = vnet_rx(port, &msgbuf, &npkts, budget);
  754. if (npkts >= budget)
  755. break;
  756. if (npkts == 0)
  757. break;
  758. } else if (msgbuf.tag.stype == VIO_SUBTYPE_ACK) {
  759. err = vnet_ack(port, &msgbuf);
  760. if (err > 0)
  761. tx_wakeup |= err;
  762. } else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK) {
  763. err = vnet_nack(port, &msgbuf);
  764. }
  765. } else if (msgbuf.tag.type == VIO_TYPE_CTRL) {
  766. if (msgbuf.tag.stype_env == VNET_MCAST_INFO)
  767. err = handle_mcast(port, &msgbuf);
  768. else
  769. err = vio_control_pkt_engine(vio, &msgbuf);
  770. if (err)
  771. break;
  772. } else {
  773. err = vnet_handle_unknown(port, &msgbuf);
  774. }
  775. if (err == -ECONNRESET)
  776. break;
  777. }
  778. if (unlikely(tx_wakeup && err != -ECONNRESET))
  779. maybe_tx_wakeup(port);
  780. return npkts;
  781. }
  782. int sunvnet_poll_common(struct napi_struct *napi, int budget)
  783. {
  784. struct vnet_port *port = container_of(napi, struct vnet_port, napi);
  785. struct vio_driver_state *vio = &port->vio;
  786. int processed = vnet_event_napi(port, budget);
  787. if (processed < budget) {
  788. napi_complete_done(napi, processed);
  789. port->rx_event &= ~LDC_EVENT_DATA_READY;
  790. vio_set_intr(vio->vdev->rx_ino, HV_INTR_ENABLED);
  791. }
  792. return processed;
  793. }
  794. EXPORT_SYMBOL_GPL(sunvnet_poll_common);
  795. void sunvnet_event_common(void *arg, int event)
  796. {
  797. struct vnet_port *port = arg;
  798. struct vio_driver_state *vio = &port->vio;
  799. port->rx_event |= event;
  800. vio_set_intr(vio->vdev->rx_ino, HV_INTR_DISABLED);
  801. napi_schedule(&port->napi);
  802. }
  803. EXPORT_SYMBOL_GPL(sunvnet_event_common);
  804. static int __vnet_tx_trigger(struct vnet_port *port, u32 start)
  805. {
  806. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  807. struct vio_dring_data hdr = {
  808. .tag = {
  809. .type = VIO_TYPE_DATA,
  810. .stype = VIO_SUBTYPE_INFO,
  811. .stype_env = VIO_DRING_DATA,
  812. .sid = vio_send_sid(&port->vio),
  813. },
  814. .dring_ident = dr->ident,
  815. .start_idx = start,
  816. .end_idx = (u32)-1,
  817. };
  818. int err, delay;
  819. int retries = 0;
  820. if (port->stop_rx) {
  821. trace_vnet_tx_pending_stopped_ack(port->vio._local_sid,
  822. port->vio._peer_sid,
  823. port->stop_rx_idx, -1);
  824. err = vnet_send_ack(port,
  825. &port->vio.drings[VIO_DRIVER_RX_RING],
  826. port->stop_rx_idx, -1,
  827. VIO_DRING_STOPPED);
  828. if (err <= 0)
  829. return err;
  830. }
  831. hdr.seq = dr->snd_nxt;
  832. delay = 1;
  833. do {
  834. err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
  835. if (err > 0) {
  836. dr->snd_nxt++;
  837. break;
  838. }
  839. udelay(delay);
  840. if ((delay <<= 1) > 128)
  841. delay = 128;
  842. if (retries++ > VNET_MAX_RETRIES)
  843. break;
  844. } while (err == -EAGAIN);
  845. trace_vnet_tx_trigger(port->vio._local_sid,
  846. port->vio._peer_sid, start, err);
  847. return err;
  848. }
  849. static struct sk_buff *vnet_clean_tx_ring(struct vnet_port *port,
  850. unsigned *pending)
  851. {
  852. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  853. struct sk_buff *skb = NULL;
  854. int i, txi;
  855. *pending = 0;
  856. txi = dr->prod;
  857. for (i = 0; i < VNET_TX_RING_SIZE; ++i) {
  858. struct vio_net_desc *d;
  859. --txi;
  860. if (txi < 0)
  861. txi = VNET_TX_RING_SIZE - 1;
  862. d = vio_dring_entry(dr, txi);
  863. if (d->hdr.state == VIO_DESC_READY) {
  864. (*pending)++;
  865. continue;
  866. }
  867. if (port->tx_bufs[txi].skb) {
  868. if (d->hdr.state != VIO_DESC_DONE)
  869. pr_notice("invalid ring buffer state %d\n",
  870. d->hdr.state);
  871. BUG_ON(port->tx_bufs[txi].skb->next);
  872. port->tx_bufs[txi].skb->next = skb;
  873. skb = port->tx_bufs[txi].skb;
  874. port->tx_bufs[txi].skb = NULL;
  875. ldc_unmap(port->vio.lp,
  876. port->tx_bufs[txi].cookies,
  877. port->tx_bufs[txi].ncookies);
  878. } else if (d->hdr.state == VIO_DESC_FREE) {
  879. break;
  880. }
  881. d->hdr.state = VIO_DESC_FREE;
  882. }
  883. return skb;
  884. }
  885. static inline void vnet_free_skbs(struct sk_buff *skb)
  886. {
  887. struct sk_buff *next;
  888. while (skb) {
  889. next = skb->next;
  890. skb->next = NULL;
  891. dev_kfree_skb(skb);
  892. skb = next;
  893. }
  894. }
  895. void sunvnet_clean_timer_expire_common(struct timer_list *t)
  896. {
  897. struct vnet_port *port = from_timer(port, t, clean_timer);
  898. struct sk_buff *freeskbs;
  899. unsigned pending;
  900. netif_tx_lock(VNET_PORT_TO_NET_DEVICE(port));
  901. freeskbs = vnet_clean_tx_ring(port, &pending);
  902. netif_tx_unlock(VNET_PORT_TO_NET_DEVICE(port));
  903. vnet_free_skbs(freeskbs);
  904. if (pending)
  905. (void)mod_timer(&port->clean_timer,
  906. jiffies + VNET_CLEAN_TIMEOUT);
  907. else
  908. del_timer(&port->clean_timer);
  909. }
  910. EXPORT_SYMBOL_GPL(sunvnet_clean_timer_expire_common);
  911. static inline int vnet_skb_map(struct ldc_channel *lp, struct sk_buff *skb,
  912. struct ldc_trans_cookie *cookies, int ncookies,
  913. unsigned int map_perm)
  914. {
  915. int i, nc, err, blen;
  916. /* header */
  917. blen = skb_headlen(skb);
  918. if (blen < ETH_ZLEN)
  919. blen = ETH_ZLEN;
  920. blen += VNET_PACKET_SKIP;
  921. blen += 8 - (blen & 7);
  922. err = ldc_map_single(lp, skb->data - VNET_PACKET_SKIP, blen, cookies,
  923. ncookies, map_perm);
  924. if (err < 0)
  925. return err;
  926. nc = err;
  927. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  928. skb_frag_t *f = &skb_shinfo(skb)->frags[i];
  929. u8 *vaddr;
  930. if (nc < ncookies) {
  931. vaddr = kmap_atomic(skb_frag_page(f));
  932. blen = skb_frag_size(f);
  933. blen += 8 - (blen & 7);
  934. err = ldc_map_single(lp, vaddr + f->page_offset,
  935. blen, cookies + nc, ncookies - nc,
  936. map_perm);
  937. kunmap_atomic(vaddr);
  938. } else {
  939. err = -EMSGSIZE;
  940. }
  941. if (err < 0) {
  942. ldc_unmap(lp, cookies, nc);
  943. return err;
  944. }
  945. nc += err;
  946. }
  947. return nc;
  948. }
  949. static inline struct sk_buff *vnet_skb_shape(struct sk_buff *skb, int ncookies)
  950. {
  951. struct sk_buff *nskb;
  952. int i, len, pad, docopy;
  953. len = skb->len;
  954. pad = 0;
  955. if (len < ETH_ZLEN) {
  956. pad += ETH_ZLEN - skb->len;
  957. len += pad;
  958. }
  959. len += VNET_PACKET_SKIP;
  960. pad += 8 - (len & 7);
  961. /* make sure we have enough cookies and alignment in every frag */
  962. docopy = skb_shinfo(skb)->nr_frags >= ncookies;
  963. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  964. skb_frag_t *f = &skb_shinfo(skb)->frags[i];
  965. docopy |= f->page_offset & 7;
  966. }
  967. if (((unsigned long)skb->data & 7) != VNET_PACKET_SKIP ||
  968. skb_tailroom(skb) < pad ||
  969. skb_headroom(skb) < VNET_PACKET_SKIP || docopy) {
  970. int start = 0, offset;
  971. __wsum csum;
  972. len = skb->len > ETH_ZLEN ? skb->len : ETH_ZLEN;
  973. nskb = alloc_and_align_skb(skb->dev, len);
  974. if (!nskb) {
  975. dev_kfree_skb(skb);
  976. return NULL;
  977. }
  978. skb_reserve(nskb, VNET_PACKET_SKIP);
  979. nskb->protocol = skb->protocol;
  980. offset = skb_mac_header(skb) - skb->data;
  981. skb_set_mac_header(nskb, offset);
  982. offset = skb_network_header(skb) - skb->data;
  983. skb_set_network_header(nskb, offset);
  984. offset = skb_transport_header(skb) - skb->data;
  985. skb_set_transport_header(nskb, offset);
  986. offset = 0;
  987. nskb->csum_offset = skb->csum_offset;
  988. nskb->ip_summed = skb->ip_summed;
  989. if (skb->ip_summed == CHECKSUM_PARTIAL)
  990. start = skb_checksum_start_offset(skb);
  991. if (start) {
  992. int offset = start + nskb->csum_offset;
  993. /* copy the headers, no csum here */
  994. if (skb_copy_bits(skb, 0, nskb->data, start)) {
  995. dev_kfree_skb(nskb);
  996. dev_kfree_skb(skb);
  997. return NULL;
  998. }
  999. /* copy the rest, with csum calculation */
  1000. *(__sum16 *)(skb->data + offset) = 0;
  1001. csum = skb_copy_and_csum_bits(skb, start,
  1002. nskb->data + start,
  1003. skb->len - start, 0);
  1004. /* add in the header checksums */
  1005. if (skb->protocol == htons(ETH_P_IP)) {
  1006. struct iphdr *iph = ip_hdr(nskb);
  1007. if (iph->protocol == IPPROTO_TCP ||
  1008. iph->protocol == IPPROTO_UDP) {
  1009. csum = csum_tcpudp_magic(iph->saddr,
  1010. iph->daddr,
  1011. skb->len - start,
  1012. iph->protocol,
  1013. csum);
  1014. }
  1015. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  1016. struct ipv6hdr *ip6h = ipv6_hdr(nskb);
  1017. if (ip6h->nexthdr == IPPROTO_TCP ||
  1018. ip6h->nexthdr == IPPROTO_UDP) {
  1019. csum = csum_ipv6_magic(&ip6h->saddr,
  1020. &ip6h->daddr,
  1021. skb->len - start,
  1022. ip6h->nexthdr,
  1023. csum);
  1024. }
  1025. }
  1026. /* save the final result */
  1027. *(__sum16 *)(nskb->data + offset) = csum;
  1028. nskb->ip_summed = CHECKSUM_NONE;
  1029. } else if (skb_copy_bits(skb, 0, nskb->data, skb->len)) {
  1030. dev_kfree_skb(nskb);
  1031. dev_kfree_skb(skb);
  1032. return NULL;
  1033. }
  1034. (void)skb_put(nskb, skb->len);
  1035. if (skb_is_gso(skb)) {
  1036. skb_shinfo(nskb)->gso_size = skb_shinfo(skb)->gso_size;
  1037. skb_shinfo(nskb)->gso_type = skb_shinfo(skb)->gso_type;
  1038. }
  1039. nskb->queue_mapping = skb->queue_mapping;
  1040. dev_kfree_skb(skb);
  1041. skb = nskb;
  1042. }
  1043. return skb;
  1044. }
  1045. static int vnet_handle_offloads(struct vnet_port *port, struct sk_buff *skb,
  1046. struct vnet_port *(*vnet_tx_port)
  1047. (struct sk_buff *, struct net_device *))
  1048. {
  1049. struct net_device *dev = VNET_PORT_TO_NET_DEVICE(port);
  1050. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  1051. struct sk_buff *segs;
  1052. int maclen, datalen;
  1053. int status;
  1054. int gso_size, gso_type, gso_segs;
  1055. int hlen = skb_transport_header(skb) - skb_mac_header(skb);
  1056. int proto = IPPROTO_IP;
  1057. if (skb->protocol == htons(ETH_P_IP))
  1058. proto = ip_hdr(skb)->protocol;
  1059. else if (skb->protocol == htons(ETH_P_IPV6))
  1060. proto = ipv6_hdr(skb)->nexthdr;
  1061. if (proto == IPPROTO_TCP) {
  1062. hlen += tcp_hdr(skb)->doff * 4;
  1063. } else if (proto == IPPROTO_UDP) {
  1064. hlen += sizeof(struct udphdr);
  1065. } else {
  1066. pr_err("vnet_handle_offloads GSO with unknown transport "
  1067. "protocol %d tproto %d\n", skb->protocol, proto);
  1068. hlen = 128; /* XXX */
  1069. }
  1070. datalen = port->tsolen - hlen;
  1071. gso_size = skb_shinfo(skb)->gso_size;
  1072. gso_type = skb_shinfo(skb)->gso_type;
  1073. gso_segs = skb_shinfo(skb)->gso_segs;
  1074. if (port->tso && gso_size < datalen)
  1075. gso_segs = DIV_ROUND_UP(skb->len - hlen, datalen);
  1076. if (unlikely(vnet_tx_dring_avail(dr) < gso_segs)) {
  1077. struct netdev_queue *txq;
  1078. txq = netdev_get_tx_queue(dev, port->q_index);
  1079. netif_tx_stop_queue(txq);
  1080. if (vnet_tx_dring_avail(dr) < skb_shinfo(skb)->gso_segs)
  1081. return NETDEV_TX_BUSY;
  1082. netif_tx_wake_queue(txq);
  1083. }
  1084. maclen = skb_network_header(skb) - skb_mac_header(skb);
  1085. skb_pull(skb, maclen);
  1086. if (port->tso && gso_size < datalen) {
  1087. if (skb_unclone(skb, GFP_ATOMIC))
  1088. goto out_dropped;
  1089. /* segment to TSO size */
  1090. skb_shinfo(skb)->gso_size = datalen;
  1091. skb_shinfo(skb)->gso_segs = gso_segs;
  1092. }
  1093. segs = skb_gso_segment(skb, dev->features & ~NETIF_F_TSO);
  1094. if (IS_ERR(segs))
  1095. goto out_dropped;
  1096. skb_push(skb, maclen);
  1097. skb_reset_mac_header(skb);
  1098. status = 0;
  1099. while (segs) {
  1100. struct sk_buff *curr = segs;
  1101. segs = segs->next;
  1102. curr->next = NULL;
  1103. if (port->tso && curr->len > dev->mtu) {
  1104. skb_shinfo(curr)->gso_size = gso_size;
  1105. skb_shinfo(curr)->gso_type = gso_type;
  1106. skb_shinfo(curr)->gso_segs =
  1107. DIV_ROUND_UP(curr->len - hlen, gso_size);
  1108. } else {
  1109. skb_shinfo(curr)->gso_size = 0;
  1110. }
  1111. skb_push(curr, maclen);
  1112. skb_reset_mac_header(curr);
  1113. memcpy(skb_mac_header(curr), skb_mac_header(skb),
  1114. maclen);
  1115. curr->csum_start = skb_transport_header(curr) - curr->head;
  1116. if (ip_hdr(curr)->protocol == IPPROTO_TCP)
  1117. curr->csum_offset = offsetof(struct tcphdr, check);
  1118. else if (ip_hdr(curr)->protocol == IPPROTO_UDP)
  1119. curr->csum_offset = offsetof(struct udphdr, check);
  1120. if (!(status & NETDEV_TX_MASK))
  1121. status = sunvnet_start_xmit_common(curr, dev,
  1122. vnet_tx_port);
  1123. if (status & NETDEV_TX_MASK)
  1124. dev_kfree_skb_any(curr);
  1125. }
  1126. if (!(status & NETDEV_TX_MASK))
  1127. dev_kfree_skb_any(skb);
  1128. return status;
  1129. out_dropped:
  1130. dev->stats.tx_dropped++;
  1131. dev_kfree_skb_any(skb);
  1132. return NETDEV_TX_OK;
  1133. }
  1134. int sunvnet_start_xmit_common(struct sk_buff *skb, struct net_device *dev,
  1135. struct vnet_port *(*vnet_tx_port)
  1136. (struct sk_buff *, struct net_device *))
  1137. {
  1138. struct vnet_port *port = NULL;
  1139. struct vio_dring_state *dr;
  1140. struct vio_net_desc *d;
  1141. unsigned int len;
  1142. struct sk_buff *freeskbs = NULL;
  1143. int i, err, txi;
  1144. unsigned pending = 0;
  1145. struct netdev_queue *txq;
  1146. rcu_read_lock();
  1147. port = vnet_tx_port(skb, dev);
  1148. if (unlikely(!port))
  1149. goto out_dropped;
  1150. if (skb_is_gso(skb) && skb->len > port->tsolen) {
  1151. err = vnet_handle_offloads(port, skb, vnet_tx_port);
  1152. rcu_read_unlock();
  1153. return err;
  1154. }
  1155. if (!skb_is_gso(skb) && skb->len > port->rmtu) {
  1156. unsigned long localmtu = port->rmtu - ETH_HLEN;
  1157. if (vio_version_after_eq(&port->vio, 1, 3))
  1158. localmtu -= VLAN_HLEN;
  1159. if (skb->protocol == htons(ETH_P_IP)) {
  1160. struct flowi4 fl4;
  1161. struct rtable *rt = NULL;
  1162. memset(&fl4, 0, sizeof(fl4));
  1163. fl4.flowi4_oif = dev->ifindex;
  1164. fl4.flowi4_tos = RT_TOS(ip_hdr(skb)->tos);
  1165. fl4.daddr = ip_hdr(skb)->daddr;
  1166. fl4.saddr = ip_hdr(skb)->saddr;
  1167. rt = ip_route_output_key(dev_net(dev), &fl4);
  1168. if (!IS_ERR(rt)) {
  1169. skb_dst_set(skb, &rt->dst);
  1170. icmp_send(skb, ICMP_DEST_UNREACH,
  1171. ICMP_FRAG_NEEDED,
  1172. htonl(localmtu));
  1173. }
  1174. }
  1175. #if IS_ENABLED(CONFIG_IPV6)
  1176. else if (skb->protocol == htons(ETH_P_IPV6))
  1177. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, localmtu);
  1178. #endif
  1179. goto out_dropped;
  1180. }
  1181. skb = vnet_skb_shape(skb, 2);
  1182. if (unlikely(!skb))
  1183. goto out_dropped;
  1184. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1185. if (skb->protocol == htons(ETH_P_IP))
  1186. vnet_fullcsum_ipv4(skb);
  1187. #if IS_ENABLED(CONFIG_IPV6)
  1188. else if (skb->protocol == htons(ETH_P_IPV6))
  1189. vnet_fullcsum_ipv6(skb);
  1190. #endif
  1191. }
  1192. dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  1193. i = skb_get_queue_mapping(skb);
  1194. txq = netdev_get_tx_queue(dev, i);
  1195. if (unlikely(vnet_tx_dring_avail(dr) < 1)) {
  1196. if (!netif_tx_queue_stopped(txq)) {
  1197. netif_tx_stop_queue(txq);
  1198. /* This is a hard error, log it. */
  1199. netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
  1200. dev->stats.tx_errors++;
  1201. }
  1202. rcu_read_unlock();
  1203. return NETDEV_TX_BUSY;
  1204. }
  1205. d = vio_dring_cur(dr);
  1206. txi = dr->prod;
  1207. freeskbs = vnet_clean_tx_ring(port, &pending);
  1208. BUG_ON(port->tx_bufs[txi].skb);
  1209. len = skb->len;
  1210. if (len < ETH_ZLEN)
  1211. len = ETH_ZLEN;
  1212. err = vnet_skb_map(port->vio.lp, skb, port->tx_bufs[txi].cookies, 2,
  1213. (LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_RW));
  1214. if (err < 0) {
  1215. netdev_info(dev, "tx buffer map error %d\n", err);
  1216. goto out_dropped;
  1217. }
  1218. port->tx_bufs[txi].skb = skb;
  1219. skb = NULL;
  1220. port->tx_bufs[txi].ncookies = err;
  1221. /* We don't rely on the ACKs to free the skb in vnet_start_xmit(),
  1222. * thus it is safe to not set VIO_ACK_ENABLE for each transmission:
  1223. * the protocol itself does not require it as long as the peer
  1224. * sends a VIO_SUBTYPE_ACK for VIO_DRING_STOPPED.
  1225. *
  1226. * An ACK for every packet in the ring is expensive as the
  1227. * sending of LDC messages is slow and affects performance.
  1228. */
  1229. d->hdr.ack = VIO_ACK_DISABLE;
  1230. d->size = len;
  1231. d->ncookies = port->tx_bufs[txi].ncookies;
  1232. for (i = 0; i < d->ncookies; i++)
  1233. d->cookies[i] = port->tx_bufs[txi].cookies[i];
  1234. if (vio_version_after_eq(&port->vio, 1, 7)) {
  1235. struct vio_net_dext *dext = vio_net_ext(d);
  1236. memset(dext, 0, sizeof(*dext));
  1237. if (skb_is_gso(port->tx_bufs[txi].skb)) {
  1238. dext->ipv4_lso_mss = skb_shinfo(port->tx_bufs[txi].skb)
  1239. ->gso_size;
  1240. dext->flags |= VNET_PKT_IPV4_LSO;
  1241. }
  1242. if (vio_version_after_eq(&port->vio, 1, 8) &&
  1243. !port->switch_port) {
  1244. dext->flags |= VNET_PKT_HCK_IPV4_HDRCKSUM_OK;
  1245. dext->flags |= VNET_PKT_HCK_FULLCKSUM_OK;
  1246. }
  1247. }
  1248. /* This has to be a non-SMP write barrier because we are writing
  1249. * to memory which is shared with the peer LDOM.
  1250. */
  1251. dma_wmb();
  1252. d->hdr.state = VIO_DESC_READY;
  1253. /* Exactly one ldc "start" trigger (for dr->cons) needs to be sent
  1254. * to notify the consumer that some descriptors are READY.
  1255. * After that "start" trigger, no additional triggers are needed until
  1256. * a DRING_STOPPED is received from the consumer. The dr->cons field
  1257. * (set up by vnet_ack()) has the value of the next dring index
  1258. * that has not yet been ack-ed. We send a "start" trigger here
  1259. * if, and only if, start_cons is true (reset it afterward). Conversely,
  1260. * vnet_ack() should check if the dring corresponding to cons
  1261. * is marked READY, but start_cons was false.
  1262. * If so, vnet_ack() should send out the missed "start" trigger.
  1263. *
  1264. * Note that the dma_wmb() above makes sure the cookies et al. are
  1265. * not globally visible before the VIO_DESC_READY, and that the
  1266. * stores are ordered correctly by the compiler. The consumer will
  1267. * not proceed until the VIO_DESC_READY is visible assuring that
  1268. * the consumer does not observe anything related to descriptors
  1269. * out of order. The HV trap from the LDC start trigger is the
  1270. * producer to consumer announcement that work is available to the
  1271. * consumer
  1272. */
  1273. if (!port->start_cons) { /* previous trigger suffices */
  1274. trace_vnet_skip_tx_trigger(port->vio._local_sid,
  1275. port->vio._peer_sid, dr->cons);
  1276. goto ldc_start_done;
  1277. }
  1278. err = __vnet_tx_trigger(port, dr->cons);
  1279. if (unlikely(err < 0)) {
  1280. netdev_info(dev, "TX trigger error %d\n", err);
  1281. d->hdr.state = VIO_DESC_FREE;
  1282. skb = port->tx_bufs[txi].skb;
  1283. port->tx_bufs[txi].skb = NULL;
  1284. dev->stats.tx_carrier_errors++;
  1285. goto out_dropped;
  1286. }
  1287. ldc_start_done:
  1288. port->start_cons = false;
  1289. dev->stats.tx_packets++;
  1290. dev->stats.tx_bytes += port->tx_bufs[txi].skb->len;
  1291. port->stats.tx_packets++;
  1292. port->stats.tx_bytes += port->tx_bufs[txi].skb->len;
  1293. dr->prod = (dr->prod + 1) & (VNET_TX_RING_SIZE - 1);
  1294. if (unlikely(vnet_tx_dring_avail(dr) < 1)) {
  1295. netif_tx_stop_queue(txq);
  1296. smp_rmb();
  1297. if (vnet_tx_dring_avail(dr) > VNET_TX_WAKEUP_THRESH(dr))
  1298. netif_tx_wake_queue(txq);
  1299. }
  1300. (void)mod_timer(&port->clean_timer, jiffies + VNET_CLEAN_TIMEOUT);
  1301. rcu_read_unlock();
  1302. vnet_free_skbs(freeskbs);
  1303. return NETDEV_TX_OK;
  1304. out_dropped:
  1305. if (pending)
  1306. (void)mod_timer(&port->clean_timer,
  1307. jiffies + VNET_CLEAN_TIMEOUT);
  1308. else if (port)
  1309. del_timer(&port->clean_timer);
  1310. rcu_read_unlock();
  1311. if (skb)
  1312. dev_kfree_skb(skb);
  1313. vnet_free_skbs(freeskbs);
  1314. dev->stats.tx_dropped++;
  1315. return NETDEV_TX_OK;
  1316. }
  1317. EXPORT_SYMBOL_GPL(sunvnet_start_xmit_common);
  1318. void sunvnet_tx_timeout_common(struct net_device *dev)
  1319. {
  1320. /* XXX Implement me XXX */
  1321. }
  1322. EXPORT_SYMBOL_GPL(sunvnet_tx_timeout_common);
  1323. int sunvnet_open_common(struct net_device *dev)
  1324. {
  1325. netif_carrier_on(dev);
  1326. netif_tx_start_all_queues(dev);
  1327. return 0;
  1328. }
  1329. EXPORT_SYMBOL_GPL(sunvnet_open_common);
  1330. int sunvnet_close_common(struct net_device *dev)
  1331. {
  1332. netif_tx_stop_all_queues(dev);
  1333. netif_carrier_off(dev);
  1334. return 0;
  1335. }
  1336. EXPORT_SYMBOL_GPL(sunvnet_close_common);
  1337. static struct vnet_mcast_entry *__vnet_mc_find(struct vnet *vp, u8 *addr)
  1338. {
  1339. struct vnet_mcast_entry *m;
  1340. for (m = vp->mcast_list; m; m = m->next) {
  1341. if (ether_addr_equal(m->addr, addr))
  1342. return m;
  1343. }
  1344. return NULL;
  1345. }
  1346. static void __update_mc_list(struct vnet *vp, struct net_device *dev)
  1347. {
  1348. struct netdev_hw_addr *ha;
  1349. netdev_for_each_mc_addr(ha, dev) {
  1350. struct vnet_mcast_entry *m;
  1351. m = __vnet_mc_find(vp, ha->addr);
  1352. if (m) {
  1353. m->hit = 1;
  1354. continue;
  1355. }
  1356. if (!m) {
  1357. m = kzalloc(sizeof(*m), GFP_ATOMIC);
  1358. if (!m)
  1359. continue;
  1360. memcpy(m->addr, ha->addr, ETH_ALEN);
  1361. m->hit = 1;
  1362. m->next = vp->mcast_list;
  1363. vp->mcast_list = m;
  1364. }
  1365. }
  1366. }
  1367. static void __send_mc_list(struct vnet *vp, struct vnet_port *port)
  1368. {
  1369. struct vio_net_mcast_info info;
  1370. struct vnet_mcast_entry *m, **pp;
  1371. int n_addrs;
  1372. memset(&info, 0, sizeof(info));
  1373. info.tag.type = VIO_TYPE_CTRL;
  1374. info.tag.stype = VIO_SUBTYPE_INFO;
  1375. info.tag.stype_env = VNET_MCAST_INFO;
  1376. info.tag.sid = vio_send_sid(&port->vio);
  1377. info.set = 1;
  1378. n_addrs = 0;
  1379. for (m = vp->mcast_list; m; m = m->next) {
  1380. if (m->sent)
  1381. continue;
  1382. m->sent = 1;
  1383. memcpy(&info.mcast_addr[n_addrs * ETH_ALEN],
  1384. m->addr, ETH_ALEN);
  1385. if (++n_addrs == VNET_NUM_MCAST) {
  1386. info.count = n_addrs;
  1387. (void)vio_ldc_send(&port->vio, &info,
  1388. sizeof(info));
  1389. n_addrs = 0;
  1390. }
  1391. }
  1392. if (n_addrs) {
  1393. info.count = n_addrs;
  1394. (void)vio_ldc_send(&port->vio, &info, sizeof(info));
  1395. }
  1396. info.set = 0;
  1397. n_addrs = 0;
  1398. pp = &vp->mcast_list;
  1399. while ((m = *pp) != NULL) {
  1400. if (m->hit) {
  1401. m->hit = 0;
  1402. pp = &m->next;
  1403. continue;
  1404. }
  1405. memcpy(&info.mcast_addr[n_addrs * ETH_ALEN],
  1406. m->addr, ETH_ALEN);
  1407. if (++n_addrs == VNET_NUM_MCAST) {
  1408. info.count = n_addrs;
  1409. (void)vio_ldc_send(&port->vio, &info,
  1410. sizeof(info));
  1411. n_addrs = 0;
  1412. }
  1413. *pp = m->next;
  1414. kfree(m);
  1415. }
  1416. if (n_addrs) {
  1417. info.count = n_addrs;
  1418. (void)vio_ldc_send(&port->vio, &info, sizeof(info));
  1419. }
  1420. }
  1421. void sunvnet_set_rx_mode_common(struct net_device *dev, struct vnet *vp)
  1422. {
  1423. struct vnet_port *port;
  1424. rcu_read_lock();
  1425. list_for_each_entry_rcu(port, &vp->port_list, list) {
  1426. if (port->switch_port) {
  1427. __update_mc_list(vp, dev);
  1428. __send_mc_list(vp, port);
  1429. break;
  1430. }
  1431. }
  1432. rcu_read_unlock();
  1433. }
  1434. EXPORT_SYMBOL_GPL(sunvnet_set_rx_mode_common);
  1435. int sunvnet_set_mac_addr_common(struct net_device *dev, void *p)
  1436. {
  1437. return -EINVAL;
  1438. }
  1439. EXPORT_SYMBOL_GPL(sunvnet_set_mac_addr_common);
  1440. void sunvnet_port_free_tx_bufs_common(struct vnet_port *port)
  1441. {
  1442. struct vio_dring_state *dr;
  1443. int i;
  1444. dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  1445. if (!dr->base)
  1446. return;
  1447. for (i = 0; i < VNET_TX_RING_SIZE; i++) {
  1448. struct vio_net_desc *d;
  1449. void *skb = port->tx_bufs[i].skb;
  1450. if (!skb)
  1451. continue;
  1452. d = vio_dring_entry(dr, i);
  1453. ldc_unmap(port->vio.lp,
  1454. port->tx_bufs[i].cookies,
  1455. port->tx_bufs[i].ncookies);
  1456. dev_kfree_skb(skb);
  1457. port->tx_bufs[i].skb = NULL;
  1458. d->hdr.state = VIO_DESC_FREE;
  1459. }
  1460. ldc_free_exp_dring(port->vio.lp, dr->base,
  1461. (dr->entry_size * dr->num_entries),
  1462. dr->cookies, dr->ncookies);
  1463. dr->base = NULL;
  1464. dr->entry_size = 0;
  1465. dr->num_entries = 0;
  1466. dr->pending = 0;
  1467. dr->ncookies = 0;
  1468. }
  1469. EXPORT_SYMBOL_GPL(sunvnet_port_free_tx_bufs_common);
  1470. void vnet_port_reset(struct vnet_port *port)
  1471. {
  1472. del_timer(&port->clean_timer);
  1473. sunvnet_port_free_tx_bufs_common(port);
  1474. port->rmtu = 0;
  1475. port->tso = (port->vsw == 0); /* no tso in vsw, misbehaves in bridge */
  1476. port->tsolen = 0;
  1477. }
  1478. EXPORT_SYMBOL_GPL(vnet_port_reset);
  1479. static int vnet_port_alloc_tx_ring(struct vnet_port *port)
  1480. {
  1481. struct vio_dring_state *dr;
  1482. unsigned long len, elen;
  1483. int i, err, ncookies;
  1484. void *dring;
  1485. dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  1486. elen = sizeof(struct vio_net_desc) +
  1487. sizeof(struct ldc_trans_cookie) * 2;
  1488. if (vio_version_after_eq(&port->vio, 1, 7))
  1489. elen += sizeof(struct vio_net_dext);
  1490. len = VNET_TX_RING_SIZE * elen;
  1491. ncookies = VIO_MAX_RING_COOKIES;
  1492. dring = ldc_alloc_exp_dring(port->vio.lp, len,
  1493. dr->cookies, &ncookies,
  1494. (LDC_MAP_SHADOW |
  1495. LDC_MAP_DIRECT |
  1496. LDC_MAP_RW));
  1497. if (IS_ERR(dring)) {
  1498. err = PTR_ERR(dring);
  1499. goto err_out;
  1500. }
  1501. dr->base = dring;
  1502. dr->entry_size = elen;
  1503. dr->num_entries = VNET_TX_RING_SIZE;
  1504. dr->prod = 0;
  1505. dr->cons = 0;
  1506. port->start_cons = true; /* need an initial trigger */
  1507. dr->pending = VNET_TX_RING_SIZE;
  1508. dr->ncookies = ncookies;
  1509. for (i = 0; i < VNET_TX_RING_SIZE; ++i) {
  1510. struct vio_net_desc *d;
  1511. d = vio_dring_entry(dr, i);
  1512. d->hdr.state = VIO_DESC_FREE;
  1513. }
  1514. return 0;
  1515. err_out:
  1516. sunvnet_port_free_tx_bufs_common(port);
  1517. return err;
  1518. }
  1519. #ifdef CONFIG_NET_POLL_CONTROLLER
  1520. void sunvnet_poll_controller_common(struct net_device *dev, struct vnet *vp)
  1521. {
  1522. struct vnet_port *port;
  1523. unsigned long flags;
  1524. spin_lock_irqsave(&vp->lock, flags);
  1525. if (!list_empty(&vp->port_list)) {
  1526. port = list_entry(vp->port_list.next, struct vnet_port, list);
  1527. napi_schedule(&port->napi);
  1528. }
  1529. spin_unlock_irqrestore(&vp->lock, flags);
  1530. }
  1531. EXPORT_SYMBOL_GPL(sunvnet_poll_controller_common);
  1532. #endif
  1533. void sunvnet_port_add_txq_common(struct vnet_port *port)
  1534. {
  1535. struct vnet *vp = port->vp;
  1536. int smallest = 0;
  1537. int i;
  1538. /* find the first least-used q
  1539. * When there are more ldoms than q's, we start to
  1540. * double up on ports per queue.
  1541. */
  1542. for (i = 0; i < VNET_MAX_TXQS; i++) {
  1543. if (vp->q_used[i] == 0) {
  1544. smallest = i;
  1545. break;
  1546. }
  1547. if (vp->q_used[i] < vp->q_used[smallest])
  1548. smallest = i;
  1549. }
  1550. vp->nports++;
  1551. vp->q_used[smallest]++;
  1552. port->q_index = smallest;
  1553. }
  1554. EXPORT_SYMBOL_GPL(sunvnet_port_add_txq_common);
  1555. void sunvnet_port_rm_txq_common(struct vnet_port *port)
  1556. {
  1557. port->vp->nports--;
  1558. port->vp->q_used[port->q_index]--;
  1559. port->q_index = 0;
  1560. }
  1561. EXPORT_SYMBOL_GPL(sunvnet_port_rm_txq_common);