sunvnet_common.c 43 KB

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