sunvnet_common.c 45 KB

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