sunvnet_common.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817
  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 netdev_tx_t
  1046. vnet_handle_offloads(struct vnet_port *port, struct sk_buff *skb,
  1047. struct vnet_port *(*vnet_tx_port)
  1048. (struct sk_buff *, struct net_device *))
  1049. {
  1050. struct net_device *dev = VNET_PORT_TO_NET_DEVICE(port);
  1051. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  1052. struct sk_buff *segs;
  1053. int maclen, datalen;
  1054. int status;
  1055. int gso_size, gso_type, gso_segs;
  1056. int hlen = skb_transport_header(skb) - skb_mac_header(skb);
  1057. int proto = IPPROTO_IP;
  1058. if (skb->protocol == htons(ETH_P_IP))
  1059. proto = ip_hdr(skb)->protocol;
  1060. else if (skb->protocol == htons(ETH_P_IPV6))
  1061. proto = ipv6_hdr(skb)->nexthdr;
  1062. if (proto == IPPROTO_TCP) {
  1063. hlen += tcp_hdr(skb)->doff * 4;
  1064. } else if (proto == IPPROTO_UDP) {
  1065. hlen += sizeof(struct udphdr);
  1066. } else {
  1067. pr_err("vnet_handle_offloads GSO with unknown transport "
  1068. "protocol %d tproto %d\n", skb->protocol, proto);
  1069. hlen = 128; /* XXX */
  1070. }
  1071. datalen = port->tsolen - hlen;
  1072. gso_size = skb_shinfo(skb)->gso_size;
  1073. gso_type = skb_shinfo(skb)->gso_type;
  1074. gso_segs = skb_shinfo(skb)->gso_segs;
  1075. if (port->tso && gso_size < datalen)
  1076. gso_segs = DIV_ROUND_UP(skb->len - hlen, datalen);
  1077. if (unlikely(vnet_tx_dring_avail(dr) < gso_segs)) {
  1078. struct netdev_queue *txq;
  1079. txq = netdev_get_tx_queue(dev, port->q_index);
  1080. netif_tx_stop_queue(txq);
  1081. if (vnet_tx_dring_avail(dr) < skb_shinfo(skb)->gso_segs)
  1082. return NETDEV_TX_BUSY;
  1083. netif_tx_wake_queue(txq);
  1084. }
  1085. maclen = skb_network_header(skb) - skb_mac_header(skb);
  1086. skb_pull(skb, maclen);
  1087. if (port->tso && gso_size < datalen) {
  1088. if (skb_unclone(skb, GFP_ATOMIC))
  1089. goto out_dropped;
  1090. /* segment to TSO size */
  1091. skb_shinfo(skb)->gso_size = datalen;
  1092. skb_shinfo(skb)->gso_segs = gso_segs;
  1093. }
  1094. segs = skb_gso_segment(skb, dev->features & ~NETIF_F_TSO);
  1095. if (IS_ERR(segs))
  1096. goto out_dropped;
  1097. skb_push(skb, maclen);
  1098. skb_reset_mac_header(skb);
  1099. status = 0;
  1100. while (segs) {
  1101. struct sk_buff *curr = segs;
  1102. segs = segs->next;
  1103. curr->next = NULL;
  1104. if (port->tso && curr->len > dev->mtu) {
  1105. skb_shinfo(curr)->gso_size = gso_size;
  1106. skb_shinfo(curr)->gso_type = gso_type;
  1107. skb_shinfo(curr)->gso_segs =
  1108. DIV_ROUND_UP(curr->len - hlen, gso_size);
  1109. } else {
  1110. skb_shinfo(curr)->gso_size = 0;
  1111. }
  1112. skb_push(curr, maclen);
  1113. skb_reset_mac_header(curr);
  1114. memcpy(skb_mac_header(curr), skb_mac_header(skb),
  1115. maclen);
  1116. curr->csum_start = skb_transport_header(curr) - curr->head;
  1117. if (ip_hdr(curr)->protocol == IPPROTO_TCP)
  1118. curr->csum_offset = offsetof(struct tcphdr, check);
  1119. else if (ip_hdr(curr)->protocol == IPPROTO_UDP)
  1120. curr->csum_offset = offsetof(struct udphdr, check);
  1121. if (!(status & NETDEV_TX_MASK))
  1122. status = sunvnet_start_xmit_common(curr, dev,
  1123. vnet_tx_port);
  1124. if (status & NETDEV_TX_MASK)
  1125. dev_kfree_skb_any(curr);
  1126. }
  1127. if (!(status & NETDEV_TX_MASK))
  1128. dev_kfree_skb_any(skb);
  1129. return status;
  1130. out_dropped:
  1131. dev->stats.tx_dropped++;
  1132. dev_kfree_skb_any(skb);
  1133. return NETDEV_TX_OK;
  1134. }
  1135. netdev_tx_t
  1136. sunvnet_start_xmit_common(struct sk_buff *skb, struct net_device *dev,
  1137. struct vnet_port *(*vnet_tx_port)
  1138. (struct sk_buff *, struct net_device *))
  1139. {
  1140. struct vnet_port *port = NULL;
  1141. struct vio_dring_state *dr;
  1142. struct vio_net_desc *d;
  1143. unsigned int len;
  1144. struct sk_buff *freeskbs = NULL;
  1145. int i, err, txi;
  1146. unsigned pending = 0;
  1147. struct netdev_queue *txq;
  1148. rcu_read_lock();
  1149. port = vnet_tx_port(skb, dev);
  1150. if (unlikely(!port))
  1151. goto out_dropped;
  1152. if (skb_is_gso(skb) && skb->len > port->tsolen) {
  1153. err = vnet_handle_offloads(port, skb, vnet_tx_port);
  1154. rcu_read_unlock();
  1155. return err;
  1156. }
  1157. if (!skb_is_gso(skb) && skb->len > port->rmtu) {
  1158. unsigned long localmtu = port->rmtu - ETH_HLEN;
  1159. if (vio_version_after_eq(&port->vio, 1, 3))
  1160. localmtu -= VLAN_HLEN;
  1161. if (skb->protocol == htons(ETH_P_IP))
  1162. icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  1163. htonl(localmtu));
  1164. #if IS_ENABLED(CONFIG_IPV6)
  1165. else if (skb->protocol == htons(ETH_P_IPV6))
  1166. icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, localmtu);
  1167. #endif
  1168. goto out_dropped;
  1169. }
  1170. skb = vnet_skb_shape(skb, 2);
  1171. if (unlikely(!skb))
  1172. goto out_dropped;
  1173. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1174. if (skb->protocol == htons(ETH_P_IP))
  1175. vnet_fullcsum_ipv4(skb);
  1176. #if IS_ENABLED(CONFIG_IPV6)
  1177. else if (skb->protocol == htons(ETH_P_IPV6))
  1178. vnet_fullcsum_ipv6(skb);
  1179. #endif
  1180. }
  1181. dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  1182. i = skb_get_queue_mapping(skb);
  1183. txq = netdev_get_tx_queue(dev, i);
  1184. if (unlikely(vnet_tx_dring_avail(dr) < 1)) {
  1185. if (!netif_tx_queue_stopped(txq)) {
  1186. netif_tx_stop_queue(txq);
  1187. /* This is a hard error, log it. */
  1188. netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
  1189. dev->stats.tx_errors++;
  1190. }
  1191. rcu_read_unlock();
  1192. return NETDEV_TX_BUSY;
  1193. }
  1194. d = vio_dring_cur(dr);
  1195. txi = dr->prod;
  1196. freeskbs = vnet_clean_tx_ring(port, &pending);
  1197. BUG_ON(port->tx_bufs[txi].skb);
  1198. len = skb->len;
  1199. if (len < ETH_ZLEN)
  1200. len = ETH_ZLEN;
  1201. err = vnet_skb_map(port->vio.lp, skb, port->tx_bufs[txi].cookies, 2,
  1202. (LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_RW));
  1203. if (err < 0) {
  1204. netdev_info(dev, "tx buffer map error %d\n", err);
  1205. goto out_dropped;
  1206. }
  1207. port->tx_bufs[txi].skb = skb;
  1208. skb = NULL;
  1209. port->tx_bufs[txi].ncookies = err;
  1210. /* We don't rely on the ACKs to free the skb in vnet_start_xmit(),
  1211. * thus it is safe to not set VIO_ACK_ENABLE for each transmission:
  1212. * the protocol itself does not require it as long as the peer
  1213. * sends a VIO_SUBTYPE_ACK for VIO_DRING_STOPPED.
  1214. *
  1215. * An ACK for every packet in the ring is expensive as the
  1216. * sending of LDC messages is slow and affects performance.
  1217. */
  1218. d->hdr.ack = VIO_ACK_DISABLE;
  1219. d->size = len;
  1220. d->ncookies = port->tx_bufs[txi].ncookies;
  1221. for (i = 0; i < d->ncookies; i++)
  1222. d->cookies[i] = port->tx_bufs[txi].cookies[i];
  1223. if (vio_version_after_eq(&port->vio, 1, 7)) {
  1224. struct vio_net_dext *dext = vio_net_ext(d);
  1225. memset(dext, 0, sizeof(*dext));
  1226. if (skb_is_gso(port->tx_bufs[txi].skb)) {
  1227. dext->ipv4_lso_mss = skb_shinfo(port->tx_bufs[txi].skb)
  1228. ->gso_size;
  1229. dext->flags |= VNET_PKT_IPV4_LSO;
  1230. }
  1231. if (vio_version_after_eq(&port->vio, 1, 8) &&
  1232. !port->switch_port) {
  1233. dext->flags |= VNET_PKT_HCK_IPV4_HDRCKSUM_OK;
  1234. dext->flags |= VNET_PKT_HCK_FULLCKSUM_OK;
  1235. }
  1236. }
  1237. /* This has to be a non-SMP write barrier because we are writing
  1238. * to memory which is shared with the peer LDOM.
  1239. */
  1240. dma_wmb();
  1241. d->hdr.state = VIO_DESC_READY;
  1242. /* Exactly one ldc "start" trigger (for dr->cons) needs to be sent
  1243. * to notify the consumer that some descriptors are READY.
  1244. * After that "start" trigger, no additional triggers are needed until
  1245. * a DRING_STOPPED is received from the consumer. The dr->cons field
  1246. * (set up by vnet_ack()) has the value of the next dring index
  1247. * that has not yet been ack-ed. We send a "start" trigger here
  1248. * if, and only if, start_cons is true (reset it afterward). Conversely,
  1249. * vnet_ack() should check if the dring corresponding to cons
  1250. * is marked READY, but start_cons was false.
  1251. * If so, vnet_ack() should send out the missed "start" trigger.
  1252. *
  1253. * Note that the dma_wmb() above makes sure the cookies et al. are
  1254. * not globally visible before the VIO_DESC_READY, and that the
  1255. * stores are ordered correctly by the compiler. The consumer will
  1256. * not proceed until the VIO_DESC_READY is visible assuring that
  1257. * the consumer does not observe anything related to descriptors
  1258. * out of order. The HV trap from the LDC start trigger is the
  1259. * producer to consumer announcement that work is available to the
  1260. * consumer
  1261. */
  1262. if (!port->start_cons) { /* previous trigger suffices */
  1263. trace_vnet_skip_tx_trigger(port->vio._local_sid,
  1264. port->vio._peer_sid, dr->cons);
  1265. goto ldc_start_done;
  1266. }
  1267. err = __vnet_tx_trigger(port, dr->cons);
  1268. if (unlikely(err < 0)) {
  1269. netdev_info(dev, "TX trigger error %d\n", err);
  1270. d->hdr.state = VIO_DESC_FREE;
  1271. skb = port->tx_bufs[txi].skb;
  1272. port->tx_bufs[txi].skb = NULL;
  1273. dev->stats.tx_carrier_errors++;
  1274. goto out_dropped;
  1275. }
  1276. ldc_start_done:
  1277. port->start_cons = false;
  1278. dev->stats.tx_packets++;
  1279. dev->stats.tx_bytes += port->tx_bufs[txi].skb->len;
  1280. port->stats.tx_packets++;
  1281. port->stats.tx_bytes += port->tx_bufs[txi].skb->len;
  1282. dr->prod = (dr->prod + 1) & (VNET_TX_RING_SIZE - 1);
  1283. if (unlikely(vnet_tx_dring_avail(dr) < 1)) {
  1284. netif_tx_stop_queue(txq);
  1285. smp_rmb();
  1286. if (vnet_tx_dring_avail(dr) > VNET_TX_WAKEUP_THRESH(dr))
  1287. netif_tx_wake_queue(txq);
  1288. }
  1289. (void)mod_timer(&port->clean_timer, jiffies + VNET_CLEAN_TIMEOUT);
  1290. rcu_read_unlock();
  1291. vnet_free_skbs(freeskbs);
  1292. return NETDEV_TX_OK;
  1293. out_dropped:
  1294. if (pending)
  1295. (void)mod_timer(&port->clean_timer,
  1296. jiffies + VNET_CLEAN_TIMEOUT);
  1297. else if (port)
  1298. del_timer(&port->clean_timer);
  1299. rcu_read_unlock();
  1300. if (skb)
  1301. dev_kfree_skb(skb);
  1302. vnet_free_skbs(freeskbs);
  1303. dev->stats.tx_dropped++;
  1304. return NETDEV_TX_OK;
  1305. }
  1306. EXPORT_SYMBOL_GPL(sunvnet_start_xmit_common);
  1307. void sunvnet_tx_timeout_common(struct net_device *dev)
  1308. {
  1309. /* XXX Implement me XXX */
  1310. }
  1311. EXPORT_SYMBOL_GPL(sunvnet_tx_timeout_common);
  1312. int sunvnet_open_common(struct net_device *dev)
  1313. {
  1314. netif_carrier_on(dev);
  1315. netif_tx_start_all_queues(dev);
  1316. return 0;
  1317. }
  1318. EXPORT_SYMBOL_GPL(sunvnet_open_common);
  1319. int sunvnet_close_common(struct net_device *dev)
  1320. {
  1321. netif_tx_stop_all_queues(dev);
  1322. netif_carrier_off(dev);
  1323. return 0;
  1324. }
  1325. EXPORT_SYMBOL_GPL(sunvnet_close_common);
  1326. static struct vnet_mcast_entry *__vnet_mc_find(struct vnet *vp, u8 *addr)
  1327. {
  1328. struct vnet_mcast_entry *m;
  1329. for (m = vp->mcast_list; m; m = m->next) {
  1330. if (ether_addr_equal(m->addr, addr))
  1331. return m;
  1332. }
  1333. return NULL;
  1334. }
  1335. static void __update_mc_list(struct vnet *vp, struct net_device *dev)
  1336. {
  1337. struct netdev_hw_addr *ha;
  1338. netdev_for_each_mc_addr(ha, dev) {
  1339. struct vnet_mcast_entry *m;
  1340. m = __vnet_mc_find(vp, ha->addr);
  1341. if (m) {
  1342. m->hit = 1;
  1343. continue;
  1344. }
  1345. if (!m) {
  1346. m = kzalloc(sizeof(*m), GFP_ATOMIC);
  1347. if (!m)
  1348. continue;
  1349. memcpy(m->addr, ha->addr, ETH_ALEN);
  1350. m->hit = 1;
  1351. m->next = vp->mcast_list;
  1352. vp->mcast_list = m;
  1353. }
  1354. }
  1355. }
  1356. static void __send_mc_list(struct vnet *vp, struct vnet_port *port)
  1357. {
  1358. struct vio_net_mcast_info info;
  1359. struct vnet_mcast_entry *m, **pp;
  1360. int n_addrs;
  1361. memset(&info, 0, sizeof(info));
  1362. info.tag.type = VIO_TYPE_CTRL;
  1363. info.tag.stype = VIO_SUBTYPE_INFO;
  1364. info.tag.stype_env = VNET_MCAST_INFO;
  1365. info.tag.sid = vio_send_sid(&port->vio);
  1366. info.set = 1;
  1367. n_addrs = 0;
  1368. for (m = vp->mcast_list; m; m = m->next) {
  1369. if (m->sent)
  1370. continue;
  1371. m->sent = 1;
  1372. memcpy(&info.mcast_addr[n_addrs * ETH_ALEN],
  1373. m->addr, ETH_ALEN);
  1374. if (++n_addrs == VNET_NUM_MCAST) {
  1375. info.count = n_addrs;
  1376. (void)vio_ldc_send(&port->vio, &info,
  1377. sizeof(info));
  1378. n_addrs = 0;
  1379. }
  1380. }
  1381. if (n_addrs) {
  1382. info.count = n_addrs;
  1383. (void)vio_ldc_send(&port->vio, &info, sizeof(info));
  1384. }
  1385. info.set = 0;
  1386. n_addrs = 0;
  1387. pp = &vp->mcast_list;
  1388. while ((m = *pp) != NULL) {
  1389. if (m->hit) {
  1390. m->hit = 0;
  1391. pp = &m->next;
  1392. continue;
  1393. }
  1394. memcpy(&info.mcast_addr[n_addrs * ETH_ALEN],
  1395. m->addr, ETH_ALEN);
  1396. if (++n_addrs == VNET_NUM_MCAST) {
  1397. info.count = n_addrs;
  1398. (void)vio_ldc_send(&port->vio, &info,
  1399. sizeof(info));
  1400. n_addrs = 0;
  1401. }
  1402. *pp = m->next;
  1403. kfree(m);
  1404. }
  1405. if (n_addrs) {
  1406. info.count = n_addrs;
  1407. (void)vio_ldc_send(&port->vio, &info, sizeof(info));
  1408. }
  1409. }
  1410. void sunvnet_set_rx_mode_common(struct net_device *dev, struct vnet *vp)
  1411. {
  1412. struct vnet_port *port;
  1413. rcu_read_lock();
  1414. list_for_each_entry_rcu(port, &vp->port_list, list) {
  1415. if (port->switch_port) {
  1416. __update_mc_list(vp, dev);
  1417. __send_mc_list(vp, port);
  1418. break;
  1419. }
  1420. }
  1421. rcu_read_unlock();
  1422. }
  1423. EXPORT_SYMBOL_GPL(sunvnet_set_rx_mode_common);
  1424. int sunvnet_set_mac_addr_common(struct net_device *dev, void *p)
  1425. {
  1426. return -EINVAL;
  1427. }
  1428. EXPORT_SYMBOL_GPL(sunvnet_set_mac_addr_common);
  1429. void sunvnet_port_free_tx_bufs_common(struct vnet_port *port)
  1430. {
  1431. struct vio_dring_state *dr;
  1432. int i;
  1433. dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  1434. if (!dr->base)
  1435. return;
  1436. for (i = 0; i < VNET_TX_RING_SIZE; i++) {
  1437. struct vio_net_desc *d;
  1438. void *skb = port->tx_bufs[i].skb;
  1439. if (!skb)
  1440. continue;
  1441. d = vio_dring_entry(dr, i);
  1442. ldc_unmap(port->vio.lp,
  1443. port->tx_bufs[i].cookies,
  1444. port->tx_bufs[i].ncookies);
  1445. dev_kfree_skb(skb);
  1446. port->tx_bufs[i].skb = NULL;
  1447. d->hdr.state = VIO_DESC_FREE;
  1448. }
  1449. ldc_free_exp_dring(port->vio.lp, dr->base,
  1450. (dr->entry_size * dr->num_entries),
  1451. dr->cookies, dr->ncookies);
  1452. dr->base = NULL;
  1453. dr->entry_size = 0;
  1454. dr->num_entries = 0;
  1455. dr->pending = 0;
  1456. dr->ncookies = 0;
  1457. }
  1458. EXPORT_SYMBOL_GPL(sunvnet_port_free_tx_bufs_common);
  1459. void vnet_port_reset(struct vnet_port *port)
  1460. {
  1461. del_timer(&port->clean_timer);
  1462. sunvnet_port_free_tx_bufs_common(port);
  1463. port->rmtu = 0;
  1464. port->tso = (port->vsw == 0); /* no tso in vsw, misbehaves in bridge */
  1465. port->tsolen = 0;
  1466. }
  1467. EXPORT_SYMBOL_GPL(vnet_port_reset);
  1468. static int vnet_port_alloc_tx_ring(struct vnet_port *port)
  1469. {
  1470. struct vio_dring_state *dr;
  1471. unsigned long len, elen;
  1472. int i, err, ncookies;
  1473. void *dring;
  1474. dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  1475. elen = sizeof(struct vio_net_desc) +
  1476. sizeof(struct ldc_trans_cookie) * 2;
  1477. if (vio_version_after_eq(&port->vio, 1, 7))
  1478. elen += sizeof(struct vio_net_dext);
  1479. len = VNET_TX_RING_SIZE * elen;
  1480. ncookies = VIO_MAX_RING_COOKIES;
  1481. dring = ldc_alloc_exp_dring(port->vio.lp, len,
  1482. dr->cookies, &ncookies,
  1483. (LDC_MAP_SHADOW |
  1484. LDC_MAP_DIRECT |
  1485. LDC_MAP_RW));
  1486. if (IS_ERR(dring)) {
  1487. err = PTR_ERR(dring);
  1488. goto err_out;
  1489. }
  1490. dr->base = dring;
  1491. dr->entry_size = elen;
  1492. dr->num_entries = VNET_TX_RING_SIZE;
  1493. dr->prod = 0;
  1494. dr->cons = 0;
  1495. port->start_cons = true; /* need an initial trigger */
  1496. dr->pending = VNET_TX_RING_SIZE;
  1497. dr->ncookies = ncookies;
  1498. for (i = 0; i < VNET_TX_RING_SIZE; ++i) {
  1499. struct vio_net_desc *d;
  1500. d = vio_dring_entry(dr, i);
  1501. d->hdr.state = VIO_DESC_FREE;
  1502. }
  1503. return 0;
  1504. err_out:
  1505. sunvnet_port_free_tx_bufs_common(port);
  1506. return err;
  1507. }
  1508. #ifdef CONFIG_NET_POLL_CONTROLLER
  1509. void sunvnet_poll_controller_common(struct net_device *dev, struct vnet *vp)
  1510. {
  1511. struct vnet_port *port;
  1512. unsigned long flags;
  1513. spin_lock_irqsave(&vp->lock, flags);
  1514. if (!list_empty(&vp->port_list)) {
  1515. port = list_entry(vp->port_list.next, struct vnet_port, list);
  1516. napi_schedule(&port->napi);
  1517. }
  1518. spin_unlock_irqrestore(&vp->lock, flags);
  1519. }
  1520. EXPORT_SYMBOL_GPL(sunvnet_poll_controller_common);
  1521. #endif
  1522. void sunvnet_port_add_txq_common(struct vnet_port *port)
  1523. {
  1524. struct vnet *vp = port->vp;
  1525. int smallest = 0;
  1526. int i;
  1527. /* find the first least-used q
  1528. * When there are more ldoms than q's, we start to
  1529. * double up on ports per queue.
  1530. */
  1531. for (i = 0; i < VNET_MAX_TXQS; i++) {
  1532. if (vp->q_used[i] == 0) {
  1533. smallest = i;
  1534. break;
  1535. }
  1536. if (vp->q_used[i] < vp->q_used[smallest])
  1537. smallest = i;
  1538. }
  1539. vp->nports++;
  1540. vp->q_used[smallest]++;
  1541. port->q_index = smallest;
  1542. }
  1543. EXPORT_SYMBOL_GPL(sunvnet_port_add_txq_common);
  1544. void sunvnet_port_rm_txq_common(struct vnet_port *port)
  1545. {
  1546. port->vp->nports--;
  1547. port->vp->q_used[port->q_index]--;
  1548. port->q_index = 0;
  1549. }
  1550. EXPORT_SYMBOL_GPL(sunvnet_port_rm_txq_common);