enic_main.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036
  1. /*
  2. * Copyright 2008-2010 Cisco Systems, Inc. All rights reserved.
  3. * Copyright 2007 Nuova Systems, Inc. All rights reserved.
  4. *
  5. * This program is free software; you may redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; version 2 of the License.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  10. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  11. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  12. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  13. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  14. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  16. * SOFTWARE.
  17. *
  18. */
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/string.h>
  22. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/pci.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/etherdevice.h>
  30. #include <linux/if.h>
  31. #include <linux/if_ether.h>
  32. #include <linux/if_vlan.h>
  33. #include <linux/in.h>
  34. #include <linux/ip.h>
  35. #include <linux/ipv6.h>
  36. #include <linux/tcp.h>
  37. #include <linux/rtnetlink.h>
  38. #include <linux/prefetch.h>
  39. #include <net/ip6_checksum.h>
  40. #include <linux/ktime.h>
  41. #include <linux/numa.h>
  42. #ifdef CONFIG_RFS_ACCEL
  43. #include <linux/cpu_rmap.h>
  44. #endif
  45. #include <linux/crash_dump.h>
  46. #include <net/busy_poll.h>
  47. #include <net/vxlan.h>
  48. #include "cq_enet_desc.h"
  49. #include "vnic_dev.h"
  50. #include "vnic_intr.h"
  51. #include "vnic_stats.h"
  52. #include "vnic_vic.h"
  53. #include "enic_res.h"
  54. #include "enic.h"
  55. #include "enic_dev.h"
  56. #include "enic_pp.h"
  57. #include "enic_clsf.h"
  58. #define ENIC_NOTIFY_TIMER_PERIOD (2 * HZ)
  59. #define WQ_ENET_MAX_DESC_LEN (1 << WQ_ENET_LEN_BITS)
  60. #define MAX_TSO (1 << 16)
  61. #define ENIC_DESC_MAX_SPLITS (MAX_TSO / WQ_ENET_MAX_DESC_LEN + 1)
  62. #define PCI_DEVICE_ID_CISCO_VIC_ENET 0x0043 /* ethernet vnic */
  63. #define PCI_DEVICE_ID_CISCO_VIC_ENET_DYN 0x0044 /* enet dynamic vnic */
  64. #define PCI_DEVICE_ID_CISCO_VIC_ENET_VF 0x0071 /* enet SRIOV VF */
  65. #define RX_COPYBREAK_DEFAULT 256
  66. /* Supported devices */
  67. static const struct pci_device_id enic_id_table[] = {
  68. { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET) },
  69. { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_DYN) },
  70. { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_VF) },
  71. { 0, } /* end of table */
  72. };
  73. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  74. MODULE_AUTHOR("Scott Feldman <scofeldm@cisco.com>");
  75. MODULE_LICENSE("GPL");
  76. MODULE_VERSION(DRV_VERSION);
  77. MODULE_DEVICE_TABLE(pci, enic_id_table);
  78. #define ENIC_LARGE_PKT_THRESHOLD 1000
  79. #define ENIC_MAX_COALESCE_TIMERS 10
  80. /* Interrupt moderation table, which will be used to decide the
  81. * coalescing timer values
  82. * {rx_rate in Mbps, mapping percentage of the range}
  83. */
  84. static struct enic_intr_mod_table mod_table[ENIC_MAX_COALESCE_TIMERS + 1] = {
  85. {4000, 0},
  86. {4400, 10},
  87. {5060, 20},
  88. {5230, 30},
  89. {5540, 40},
  90. {5820, 50},
  91. {6120, 60},
  92. {6435, 70},
  93. {6745, 80},
  94. {7000, 90},
  95. {0xFFFFFFFF, 100}
  96. };
  97. /* This table helps the driver to pick different ranges for rx coalescing
  98. * timer depending on the link speed.
  99. */
  100. static struct enic_intr_mod_range mod_range[ENIC_MAX_LINK_SPEEDS] = {
  101. {0, 0}, /* 0 - 4 Gbps */
  102. {0, 3}, /* 4 - 10 Gbps */
  103. {3, 6}, /* 10 - 40 Gbps */
  104. };
  105. static void enic_init_affinity_hint(struct enic *enic)
  106. {
  107. int numa_node = dev_to_node(&enic->pdev->dev);
  108. int i;
  109. for (i = 0; i < enic->intr_count; i++) {
  110. if (enic_is_err_intr(enic, i) || enic_is_notify_intr(enic, i) ||
  111. (enic->msix[i].affinity_mask &&
  112. !cpumask_empty(enic->msix[i].affinity_mask)))
  113. continue;
  114. if (zalloc_cpumask_var(&enic->msix[i].affinity_mask,
  115. GFP_KERNEL))
  116. cpumask_set_cpu(cpumask_local_spread(i, numa_node),
  117. enic->msix[i].affinity_mask);
  118. }
  119. }
  120. static void enic_free_affinity_hint(struct enic *enic)
  121. {
  122. int i;
  123. for (i = 0; i < enic->intr_count; i++) {
  124. if (enic_is_err_intr(enic, i) || enic_is_notify_intr(enic, i))
  125. continue;
  126. free_cpumask_var(enic->msix[i].affinity_mask);
  127. }
  128. }
  129. static void enic_set_affinity_hint(struct enic *enic)
  130. {
  131. int i;
  132. int err;
  133. for (i = 0; i < enic->intr_count; i++) {
  134. if (enic_is_err_intr(enic, i) ||
  135. enic_is_notify_intr(enic, i) ||
  136. !enic->msix[i].affinity_mask ||
  137. cpumask_empty(enic->msix[i].affinity_mask))
  138. continue;
  139. err = irq_set_affinity_hint(enic->msix_entry[i].vector,
  140. enic->msix[i].affinity_mask);
  141. if (err)
  142. netdev_warn(enic->netdev, "irq_set_affinity_hint failed, err %d\n",
  143. err);
  144. }
  145. for (i = 0; i < enic->wq_count; i++) {
  146. int wq_intr = enic_msix_wq_intr(enic, i);
  147. if (enic->msix[wq_intr].affinity_mask &&
  148. !cpumask_empty(enic->msix[wq_intr].affinity_mask))
  149. netif_set_xps_queue(enic->netdev,
  150. enic->msix[wq_intr].affinity_mask,
  151. i);
  152. }
  153. }
  154. static void enic_unset_affinity_hint(struct enic *enic)
  155. {
  156. int i;
  157. for (i = 0; i < enic->intr_count; i++)
  158. irq_set_affinity_hint(enic->msix_entry[i].vector, NULL);
  159. }
  160. static void enic_udp_tunnel_add(struct net_device *netdev,
  161. struct udp_tunnel_info *ti)
  162. {
  163. struct enic *enic = netdev_priv(netdev);
  164. __be16 port = ti->port;
  165. int err;
  166. spin_lock_bh(&enic->devcmd_lock);
  167. if (ti->type != UDP_TUNNEL_TYPE_VXLAN) {
  168. netdev_info(netdev, "udp_tnl: only vxlan tunnel offload supported");
  169. goto error;
  170. }
  171. if (ti->sa_family != AF_INET) {
  172. netdev_info(netdev, "vxlan: only IPv4 offload supported");
  173. goto error;
  174. }
  175. if (enic->vxlan.vxlan_udp_port_number) {
  176. if (ntohs(port) == enic->vxlan.vxlan_udp_port_number)
  177. netdev_warn(netdev, "vxlan: udp port already offloaded");
  178. else
  179. netdev_info(netdev, "vxlan: offload supported for only one UDP port");
  180. goto error;
  181. }
  182. err = vnic_dev_overlay_offload_cfg(enic->vdev,
  183. OVERLAY_CFG_VXLAN_PORT_UPDATE,
  184. ntohs(port));
  185. if (err)
  186. goto error;
  187. err = vnic_dev_overlay_offload_ctrl(enic->vdev, OVERLAY_FEATURE_VXLAN,
  188. enic->vxlan.patch_level);
  189. if (err)
  190. goto error;
  191. enic->vxlan.vxlan_udp_port_number = ntohs(port);
  192. netdev_info(netdev, "vxlan fw-vers-%d: offload enabled for udp port: %d, sa_family: %d ",
  193. (int)enic->vxlan.patch_level, ntohs(port), ti->sa_family);
  194. goto unlock;
  195. error:
  196. netdev_info(netdev, "failed to offload udp port: %d, sa_family: %d, type: %d",
  197. ntohs(port), ti->sa_family, ti->type);
  198. unlock:
  199. spin_unlock_bh(&enic->devcmd_lock);
  200. }
  201. static void enic_udp_tunnel_del(struct net_device *netdev,
  202. struct udp_tunnel_info *ti)
  203. {
  204. struct enic *enic = netdev_priv(netdev);
  205. int err;
  206. spin_lock_bh(&enic->devcmd_lock);
  207. if ((ti->sa_family != AF_INET) ||
  208. ((ntohs(ti->port) != enic->vxlan.vxlan_udp_port_number)) ||
  209. (ti->type != UDP_TUNNEL_TYPE_VXLAN)) {
  210. netdev_info(netdev, "udp_tnl: port:%d, sa_family: %d, type: %d not offloaded",
  211. ntohs(ti->port), ti->sa_family, ti->type);
  212. goto unlock;
  213. }
  214. err = vnic_dev_overlay_offload_ctrl(enic->vdev, OVERLAY_FEATURE_VXLAN,
  215. OVERLAY_OFFLOAD_DISABLE);
  216. if (err) {
  217. netdev_err(netdev, "vxlan: del offload udp port: %d failed",
  218. ntohs(ti->port));
  219. goto unlock;
  220. }
  221. enic->vxlan.vxlan_udp_port_number = 0;
  222. netdev_info(netdev, "vxlan: del offload udp port %d, family %d\n",
  223. ntohs(ti->port), ti->sa_family);
  224. unlock:
  225. spin_unlock_bh(&enic->devcmd_lock);
  226. }
  227. static netdev_features_t enic_features_check(struct sk_buff *skb,
  228. struct net_device *dev,
  229. netdev_features_t features)
  230. {
  231. const struct ethhdr *eth = (struct ethhdr *)skb_inner_mac_header(skb);
  232. struct enic *enic = netdev_priv(dev);
  233. struct udphdr *udph;
  234. u16 port = 0;
  235. u16 proto;
  236. if (!skb->encapsulation)
  237. return features;
  238. features = vxlan_features_check(skb, features);
  239. /* hardware only supports IPv4 vxlan tunnel */
  240. if (vlan_get_protocol(skb) != htons(ETH_P_IP))
  241. goto out;
  242. /* hardware does not support offload of ipv6 inner pkt */
  243. if (eth->h_proto != ntohs(ETH_P_IP))
  244. goto out;
  245. proto = ip_hdr(skb)->protocol;
  246. if (proto == IPPROTO_UDP) {
  247. udph = udp_hdr(skb);
  248. port = be16_to_cpu(udph->dest);
  249. }
  250. /* HW supports offload of only one UDP port. Remove CSUM and GSO MASK
  251. * for other UDP port tunnels
  252. */
  253. if (port != enic->vxlan.vxlan_udp_port_number)
  254. goto out;
  255. return features;
  256. out:
  257. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  258. }
  259. int enic_is_dynamic(struct enic *enic)
  260. {
  261. return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_DYN;
  262. }
  263. int enic_sriov_enabled(struct enic *enic)
  264. {
  265. return (enic->priv_flags & ENIC_SRIOV_ENABLED) ? 1 : 0;
  266. }
  267. static int enic_is_sriov_vf(struct enic *enic)
  268. {
  269. return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_VF;
  270. }
  271. int enic_is_valid_vf(struct enic *enic, int vf)
  272. {
  273. #ifdef CONFIG_PCI_IOV
  274. return vf >= 0 && vf < enic->num_vfs;
  275. #else
  276. return 0;
  277. #endif
  278. }
  279. static void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
  280. {
  281. struct enic *enic = vnic_dev_priv(wq->vdev);
  282. if (buf->sop)
  283. pci_unmap_single(enic->pdev, buf->dma_addr,
  284. buf->len, PCI_DMA_TODEVICE);
  285. else
  286. pci_unmap_page(enic->pdev, buf->dma_addr,
  287. buf->len, PCI_DMA_TODEVICE);
  288. if (buf->os_buf)
  289. dev_kfree_skb_any(buf->os_buf);
  290. }
  291. static void enic_wq_free_buf(struct vnic_wq *wq,
  292. struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque)
  293. {
  294. enic_free_wq_buf(wq, buf);
  295. }
  296. static int enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
  297. u8 type, u16 q_number, u16 completed_index, void *opaque)
  298. {
  299. struct enic *enic = vnic_dev_priv(vdev);
  300. spin_lock(&enic->wq_lock[q_number]);
  301. vnic_wq_service(&enic->wq[q_number], cq_desc,
  302. completed_index, enic_wq_free_buf,
  303. opaque);
  304. if (netif_tx_queue_stopped(netdev_get_tx_queue(enic->netdev, q_number)) &&
  305. vnic_wq_desc_avail(&enic->wq[q_number]) >=
  306. (MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS))
  307. netif_wake_subqueue(enic->netdev, q_number);
  308. spin_unlock(&enic->wq_lock[q_number]);
  309. return 0;
  310. }
  311. static bool enic_log_q_error(struct enic *enic)
  312. {
  313. unsigned int i;
  314. u32 error_status;
  315. bool err = false;
  316. for (i = 0; i < enic->wq_count; i++) {
  317. error_status = vnic_wq_error_status(&enic->wq[i]);
  318. err |= error_status;
  319. if (error_status)
  320. netdev_err(enic->netdev, "WQ[%d] error_status %d\n",
  321. i, error_status);
  322. }
  323. for (i = 0; i < enic->rq_count; i++) {
  324. error_status = vnic_rq_error_status(&enic->rq[i]);
  325. err |= error_status;
  326. if (error_status)
  327. netdev_err(enic->netdev, "RQ[%d] error_status %d\n",
  328. i, error_status);
  329. }
  330. return err;
  331. }
  332. static void enic_msglvl_check(struct enic *enic)
  333. {
  334. u32 msg_enable = vnic_dev_msg_lvl(enic->vdev);
  335. if (msg_enable != enic->msg_enable) {
  336. netdev_info(enic->netdev, "msg lvl changed from 0x%x to 0x%x\n",
  337. enic->msg_enable, msg_enable);
  338. enic->msg_enable = msg_enable;
  339. }
  340. }
  341. static void enic_mtu_check(struct enic *enic)
  342. {
  343. u32 mtu = vnic_dev_mtu(enic->vdev);
  344. struct net_device *netdev = enic->netdev;
  345. if (mtu && mtu != enic->port_mtu) {
  346. enic->port_mtu = mtu;
  347. if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic)) {
  348. mtu = max_t(int, ENIC_MIN_MTU,
  349. min_t(int, ENIC_MAX_MTU, mtu));
  350. if (mtu != netdev->mtu)
  351. schedule_work(&enic->change_mtu_work);
  352. } else {
  353. if (mtu < netdev->mtu)
  354. netdev_warn(netdev,
  355. "interface MTU (%d) set higher "
  356. "than switch port MTU (%d)\n",
  357. netdev->mtu, mtu);
  358. }
  359. }
  360. }
  361. static void enic_link_check(struct enic *enic)
  362. {
  363. int link_status = vnic_dev_link_status(enic->vdev);
  364. int carrier_ok = netif_carrier_ok(enic->netdev);
  365. if (link_status && !carrier_ok) {
  366. netdev_info(enic->netdev, "Link UP\n");
  367. netif_carrier_on(enic->netdev);
  368. } else if (!link_status && carrier_ok) {
  369. netdev_info(enic->netdev, "Link DOWN\n");
  370. netif_carrier_off(enic->netdev);
  371. }
  372. }
  373. static void enic_notify_check(struct enic *enic)
  374. {
  375. enic_msglvl_check(enic);
  376. enic_mtu_check(enic);
  377. enic_link_check(enic);
  378. }
  379. #define ENIC_TEST_INTR(pba, i) (pba & (1 << i))
  380. static irqreturn_t enic_isr_legacy(int irq, void *data)
  381. {
  382. struct net_device *netdev = data;
  383. struct enic *enic = netdev_priv(netdev);
  384. unsigned int io_intr = enic_legacy_io_intr();
  385. unsigned int err_intr = enic_legacy_err_intr();
  386. unsigned int notify_intr = enic_legacy_notify_intr();
  387. u32 pba;
  388. vnic_intr_mask(&enic->intr[io_intr]);
  389. pba = vnic_intr_legacy_pba(enic->legacy_pba);
  390. if (!pba) {
  391. vnic_intr_unmask(&enic->intr[io_intr]);
  392. return IRQ_NONE; /* not our interrupt */
  393. }
  394. if (ENIC_TEST_INTR(pba, notify_intr)) {
  395. enic_notify_check(enic);
  396. vnic_intr_return_all_credits(&enic->intr[notify_intr]);
  397. }
  398. if (ENIC_TEST_INTR(pba, err_intr)) {
  399. vnic_intr_return_all_credits(&enic->intr[err_intr]);
  400. enic_log_q_error(enic);
  401. /* schedule recovery from WQ/RQ error */
  402. schedule_work(&enic->reset);
  403. return IRQ_HANDLED;
  404. }
  405. if (ENIC_TEST_INTR(pba, io_intr))
  406. napi_schedule_irqoff(&enic->napi[0]);
  407. else
  408. vnic_intr_unmask(&enic->intr[io_intr]);
  409. return IRQ_HANDLED;
  410. }
  411. static irqreturn_t enic_isr_msi(int irq, void *data)
  412. {
  413. struct enic *enic = data;
  414. /* With MSI, there is no sharing of interrupts, so this is
  415. * our interrupt and there is no need to ack it. The device
  416. * is not providing per-vector masking, so the OS will not
  417. * write to PCI config space to mask/unmask the interrupt.
  418. * We're using mask_on_assertion for MSI, so the device
  419. * automatically masks the interrupt when the interrupt is
  420. * generated. Later, when exiting polling, the interrupt
  421. * will be unmasked (see enic_poll).
  422. *
  423. * Also, the device uses the same PCIe Traffic Class (TC)
  424. * for Memory Write data and MSI, so there are no ordering
  425. * issues; the MSI will always arrive at the Root Complex
  426. * _after_ corresponding Memory Writes (i.e. descriptor
  427. * writes).
  428. */
  429. napi_schedule_irqoff(&enic->napi[0]);
  430. return IRQ_HANDLED;
  431. }
  432. static irqreturn_t enic_isr_msix(int irq, void *data)
  433. {
  434. struct napi_struct *napi = data;
  435. napi_schedule_irqoff(napi);
  436. return IRQ_HANDLED;
  437. }
  438. static irqreturn_t enic_isr_msix_err(int irq, void *data)
  439. {
  440. struct enic *enic = data;
  441. unsigned int intr = enic_msix_err_intr(enic);
  442. vnic_intr_return_all_credits(&enic->intr[intr]);
  443. if (enic_log_q_error(enic))
  444. /* schedule recovery from WQ/RQ error */
  445. schedule_work(&enic->reset);
  446. return IRQ_HANDLED;
  447. }
  448. static irqreturn_t enic_isr_msix_notify(int irq, void *data)
  449. {
  450. struct enic *enic = data;
  451. unsigned int intr = enic_msix_notify_intr(enic);
  452. enic_notify_check(enic);
  453. vnic_intr_return_all_credits(&enic->intr[intr]);
  454. return IRQ_HANDLED;
  455. }
  456. static int enic_queue_wq_skb_cont(struct enic *enic, struct vnic_wq *wq,
  457. struct sk_buff *skb, unsigned int len_left,
  458. int loopback)
  459. {
  460. const skb_frag_t *frag;
  461. dma_addr_t dma_addr;
  462. /* Queue additional data fragments */
  463. for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
  464. len_left -= skb_frag_size(frag);
  465. dma_addr = skb_frag_dma_map(&enic->pdev->dev, frag, 0,
  466. skb_frag_size(frag),
  467. DMA_TO_DEVICE);
  468. if (unlikely(enic_dma_map_check(enic, dma_addr)))
  469. return -ENOMEM;
  470. enic_queue_wq_desc_cont(wq, skb, dma_addr, skb_frag_size(frag),
  471. (len_left == 0), /* EOP? */
  472. loopback);
  473. }
  474. return 0;
  475. }
  476. static int enic_queue_wq_skb_vlan(struct enic *enic, struct vnic_wq *wq,
  477. struct sk_buff *skb, int vlan_tag_insert,
  478. unsigned int vlan_tag, int loopback)
  479. {
  480. unsigned int head_len = skb_headlen(skb);
  481. unsigned int len_left = skb->len - head_len;
  482. int eop = (len_left == 0);
  483. dma_addr_t dma_addr;
  484. int err = 0;
  485. dma_addr = pci_map_single(enic->pdev, skb->data, head_len,
  486. PCI_DMA_TODEVICE);
  487. if (unlikely(enic_dma_map_check(enic, dma_addr)))
  488. return -ENOMEM;
  489. /* Queue the main skb fragment. The fragments are no larger
  490. * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
  491. * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
  492. * per fragment is queued.
  493. */
  494. enic_queue_wq_desc(wq, skb, dma_addr, head_len, vlan_tag_insert,
  495. vlan_tag, eop, loopback);
  496. if (!eop)
  497. err = enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
  498. return err;
  499. }
  500. static int enic_queue_wq_skb_csum_l4(struct enic *enic, struct vnic_wq *wq,
  501. struct sk_buff *skb, int vlan_tag_insert,
  502. unsigned int vlan_tag, int loopback)
  503. {
  504. unsigned int head_len = skb_headlen(skb);
  505. unsigned int len_left = skb->len - head_len;
  506. unsigned int hdr_len = skb_checksum_start_offset(skb);
  507. unsigned int csum_offset = hdr_len + skb->csum_offset;
  508. int eop = (len_left == 0);
  509. dma_addr_t dma_addr;
  510. int err = 0;
  511. dma_addr = pci_map_single(enic->pdev, skb->data, head_len,
  512. PCI_DMA_TODEVICE);
  513. if (unlikely(enic_dma_map_check(enic, dma_addr)))
  514. return -ENOMEM;
  515. /* Queue the main skb fragment. The fragments are no larger
  516. * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
  517. * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
  518. * per fragment is queued.
  519. */
  520. enic_queue_wq_desc_csum_l4(wq, skb, dma_addr, head_len, csum_offset,
  521. hdr_len, vlan_tag_insert, vlan_tag, eop,
  522. loopback);
  523. if (!eop)
  524. err = enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
  525. return err;
  526. }
  527. static void enic_preload_tcp_csum_encap(struct sk_buff *skb)
  528. {
  529. if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
  530. inner_ip_hdr(skb)->check = 0;
  531. inner_tcp_hdr(skb)->check =
  532. ~csum_tcpudp_magic(inner_ip_hdr(skb)->saddr,
  533. inner_ip_hdr(skb)->daddr, 0,
  534. IPPROTO_TCP, 0);
  535. }
  536. }
  537. static void enic_preload_tcp_csum(struct sk_buff *skb)
  538. {
  539. /* Preload TCP csum field with IP pseudo hdr calculated
  540. * with IP length set to zero. HW will later add in length
  541. * to each TCP segment resulting from the TSO.
  542. */
  543. if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
  544. ip_hdr(skb)->check = 0;
  545. tcp_hdr(skb)->check = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  546. ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  547. } else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
  548. tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  549. &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  550. }
  551. }
  552. static int enic_queue_wq_skb_tso(struct enic *enic, struct vnic_wq *wq,
  553. struct sk_buff *skb, unsigned int mss,
  554. int vlan_tag_insert, unsigned int vlan_tag,
  555. int loopback)
  556. {
  557. unsigned int frag_len_left = skb_headlen(skb);
  558. unsigned int len_left = skb->len - frag_len_left;
  559. int eop = (len_left == 0);
  560. unsigned int offset = 0;
  561. unsigned int hdr_len;
  562. dma_addr_t dma_addr;
  563. unsigned int len;
  564. skb_frag_t *frag;
  565. if (skb->encapsulation) {
  566. hdr_len = skb_inner_transport_header(skb) - skb->data;
  567. hdr_len += inner_tcp_hdrlen(skb);
  568. enic_preload_tcp_csum_encap(skb);
  569. } else {
  570. hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  571. enic_preload_tcp_csum(skb);
  572. }
  573. /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
  574. * for the main skb fragment
  575. */
  576. while (frag_len_left) {
  577. len = min(frag_len_left, (unsigned int)WQ_ENET_MAX_DESC_LEN);
  578. dma_addr = pci_map_single(enic->pdev, skb->data + offset, len,
  579. PCI_DMA_TODEVICE);
  580. if (unlikely(enic_dma_map_check(enic, dma_addr)))
  581. return -ENOMEM;
  582. enic_queue_wq_desc_tso(wq, skb, dma_addr, len, mss, hdr_len,
  583. vlan_tag_insert, vlan_tag,
  584. eop && (len == frag_len_left), loopback);
  585. frag_len_left -= len;
  586. offset += len;
  587. }
  588. if (eop)
  589. return 0;
  590. /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
  591. * for additional data fragments
  592. */
  593. for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
  594. len_left -= skb_frag_size(frag);
  595. frag_len_left = skb_frag_size(frag);
  596. offset = 0;
  597. while (frag_len_left) {
  598. len = min(frag_len_left,
  599. (unsigned int)WQ_ENET_MAX_DESC_LEN);
  600. dma_addr = skb_frag_dma_map(&enic->pdev->dev, frag,
  601. offset, len,
  602. DMA_TO_DEVICE);
  603. if (unlikely(enic_dma_map_check(enic, dma_addr)))
  604. return -ENOMEM;
  605. enic_queue_wq_desc_cont(wq, skb, dma_addr, len,
  606. (len_left == 0) &&
  607. (len == frag_len_left),/*EOP*/
  608. loopback);
  609. frag_len_left -= len;
  610. offset += len;
  611. }
  612. }
  613. return 0;
  614. }
  615. static inline int enic_queue_wq_skb_encap(struct enic *enic, struct vnic_wq *wq,
  616. struct sk_buff *skb,
  617. int vlan_tag_insert,
  618. unsigned int vlan_tag, int loopback)
  619. {
  620. unsigned int head_len = skb_headlen(skb);
  621. unsigned int len_left = skb->len - head_len;
  622. /* Hardware will overwrite the checksum fields, calculating from
  623. * scratch and ignoring the value placed by software.
  624. * Offload mode = 00
  625. * mss[2], mss[1], mss[0] bits are set
  626. */
  627. unsigned int mss_or_csum = 7;
  628. int eop = (len_left == 0);
  629. dma_addr_t dma_addr;
  630. int err = 0;
  631. dma_addr = pci_map_single(enic->pdev, skb->data, head_len,
  632. PCI_DMA_TODEVICE);
  633. if (unlikely(enic_dma_map_check(enic, dma_addr)))
  634. return -ENOMEM;
  635. enic_queue_wq_desc_ex(wq, skb, dma_addr, head_len, mss_or_csum, 0,
  636. vlan_tag_insert, vlan_tag,
  637. WQ_ENET_OFFLOAD_MODE_CSUM, eop, 1 /* SOP */, eop,
  638. loopback);
  639. if (!eop)
  640. err = enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
  641. return err;
  642. }
  643. static inline void enic_queue_wq_skb(struct enic *enic,
  644. struct vnic_wq *wq, struct sk_buff *skb)
  645. {
  646. unsigned int mss = skb_shinfo(skb)->gso_size;
  647. unsigned int vlan_tag = 0;
  648. int vlan_tag_insert = 0;
  649. int loopback = 0;
  650. int err;
  651. if (skb_vlan_tag_present(skb)) {
  652. /* VLAN tag from trunking driver */
  653. vlan_tag_insert = 1;
  654. vlan_tag = skb_vlan_tag_get(skb);
  655. } else if (enic->loop_enable) {
  656. vlan_tag = enic->loop_tag;
  657. loopback = 1;
  658. }
  659. if (mss)
  660. err = enic_queue_wq_skb_tso(enic, wq, skb, mss,
  661. vlan_tag_insert, vlan_tag,
  662. loopback);
  663. else if (skb->encapsulation)
  664. err = enic_queue_wq_skb_encap(enic, wq, skb, vlan_tag_insert,
  665. vlan_tag, loopback);
  666. else if (skb->ip_summed == CHECKSUM_PARTIAL)
  667. err = enic_queue_wq_skb_csum_l4(enic, wq, skb, vlan_tag_insert,
  668. vlan_tag, loopback);
  669. else
  670. err = enic_queue_wq_skb_vlan(enic, wq, skb, vlan_tag_insert,
  671. vlan_tag, loopback);
  672. if (unlikely(err)) {
  673. struct vnic_wq_buf *buf;
  674. buf = wq->to_use->prev;
  675. /* while not EOP of previous pkt && queue not empty.
  676. * For all non EOP bufs, os_buf is NULL.
  677. */
  678. while (!buf->os_buf && (buf->next != wq->to_clean)) {
  679. enic_free_wq_buf(wq, buf);
  680. wq->ring.desc_avail++;
  681. buf = buf->prev;
  682. }
  683. wq->to_use = buf->next;
  684. dev_kfree_skb(skb);
  685. }
  686. }
  687. /* netif_tx_lock held, process context with BHs disabled, or BH */
  688. static netdev_tx_t enic_hard_start_xmit(struct sk_buff *skb,
  689. struct net_device *netdev)
  690. {
  691. struct enic *enic = netdev_priv(netdev);
  692. struct vnic_wq *wq;
  693. unsigned int txq_map;
  694. struct netdev_queue *txq;
  695. if (skb->len <= 0) {
  696. dev_kfree_skb_any(skb);
  697. return NETDEV_TX_OK;
  698. }
  699. txq_map = skb_get_queue_mapping(skb) % enic->wq_count;
  700. wq = &enic->wq[txq_map];
  701. txq = netdev_get_tx_queue(netdev, txq_map);
  702. /* Non-TSO sends must fit within ENIC_NON_TSO_MAX_DESC descs,
  703. * which is very likely. In the off chance it's going to take
  704. * more than * ENIC_NON_TSO_MAX_DESC, linearize the skb.
  705. */
  706. if (skb_shinfo(skb)->gso_size == 0 &&
  707. skb_shinfo(skb)->nr_frags + 1 > ENIC_NON_TSO_MAX_DESC &&
  708. skb_linearize(skb)) {
  709. dev_kfree_skb_any(skb);
  710. return NETDEV_TX_OK;
  711. }
  712. spin_lock(&enic->wq_lock[txq_map]);
  713. if (vnic_wq_desc_avail(wq) <
  714. skb_shinfo(skb)->nr_frags + ENIC_DESC_MAX_SPLITS) {
  715. netif_tx_stop_queue(txq);
  716. /* This is a hard error, log it */
  717. netdev_err(netdev, "BUG! Tx ring full when queue awake!\n");
  718. spin_unlock(&enic->wq_lock[txq_map]);
  719. return NETDEV_TX_BUSY;
  720. }
  721. enic_queue_wq_skb(enic, wq, skb);
  722. if (vnic_wq_desc_avail(wq) < MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS)
  723. netif_tx_stop_queue(txq);
  724. if (!skb->xmit_more || netif_xmit_stopped(txq))
  725. vnic_wq_doorbell(wq);
  726. spin_unlock(&enic->wq_lock[txq_map]);
  727. return NETDEV_TX_OK;
  728. }
  729. /* dev_base_lock rwlock held, nominally process context */
  730. static void enic_get_stats(struct net_device *netdev,
  731. struct rtnl_link_stats64 *net_stats)
  732. {
  733. struct enic *enic = netdev_priv(netdev);
  734. struct vnic_stats *stats;
  735. int err;
  736. err = enic_dev_stats_dump(enic, &stats);
  737. /* return only when pci_zalloc_consistent fails in vnic_dev_stats_dump
  738. * For other failures, like devcmd failure, we return previously
  739. * recorded stats.
  740. */
  741. if (err == -ENOMEM)
  742. return;
  743. net_stats->tx_packets = stats->tx.tx_frames_ok;
  744. net_stats->tx_bytes = stats->tx.tx_bytes_ok;
  745. net_stats->tx_errors = stats->tx.tx_errors;
  746. net_stats->tx_dropped = stats->tx.tx_drops;
  747. net_stats->rx_packets = stats->rx.rx_frames_ok;
  748. net_stats->rx_bytes = stats->rx.rx_bytes_ok;
  749. net_stats->rx_errors = stats->rx.rx_errors;
  750. net_stats->multicast = stats->rx.rx_multicast_frames_ok;
  751. net_stats->rx_over_errors = enic->rq_truncated_pkts;
  752. net_stats->rx_crc_errors = enic->rq_bad_fcs;
  753. net_stats->rx_dropped = stats->rx.rx_no_bufs + stats->rx.rx_drop;
  754. }
  755. static int enic_mc_sync(struct net_device *netdev, const u8 *mc_addr)
  756. {
  757. struct enic *enic = netdev_priv(netdev);
  758. if (enic->mc_count == ENIC_MULTICAST_PERFECT_FILTERS) {
  759. unsigned int mc_count = netdev_mc_count(netdev);
  760. netdev_warn(netdev, "Registering only %d out of %d multicast addresses\n",
  761. ENIC_MULTICAST_PERFECT_FILTERS, mc_count);
  762. return -ENOSPC;
  763. }
  764. enic_dev_add_addr(enic, mc_addr);
  765. enic->mc_count++;
  766. return 0;
  767. }
  768. static int enic_mc_unsync(struct net_device *netdev, const u8 *mc_addr)
  769. {
  770. struct enic *enic = netdev_priv(netdev);
  771. enic_dev_del_addr(enic, mc_addr);
  772. enic->mc_count--;
  773. return 0;
  774. }
  775. static int enic_uc_sync(struct net_device *netdev, const u8 *uc_addr)
  776. {
  777. struct enic *enic = netdev_priv(netdev);
  778. if (enic->uc_count == ENIC_UNICAST_PERFECT_FILTERS) {
  779. unsigned int uc_count = netdev_uc_count(netdev);
  780. netdev_warn(netdev, "Registering only %d out of %d unicast addresses\n",
  781. ENIC_UNICAST_PERFECT_FILTERS, uc_count);
  782. return -ENOSPC;
  783. }
  784. enic_dev_add_addr(enic, uc_addr);
  785. enic->uc_count++;
  786. return 0;
  787. }
  788. static int enic_uc_unsync(struct net_device *netdev, const u8 *uc_addr)
  789. {
  790. struct enic *enic = netdev_priv(netdev);
  791. enic_dev_del_addr(enic, uc_addr);
  792. enic->uc_count--;
  793. return 0;
  794. }
  795. void enic_reset_addr_lists(struct enic *enic)
  796. {
  797. struct net_device *netdev = enic->netdev;
  798. __dev_uc_unsync(netdev, NULL);
  799. __dev_mc_unsync(netdev, NULL);
  800. enic->mc_count = 0;
  801. enic->uc_count = 0;
  802. enic->flags = 0;
  803. }
  804. static int enic_set_mac_addr(struct net_device *netdev, char *addr)
  805. {
  806. struct enic *enic = netdev_priv(netdev);
  807. if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic)) {
  808. if (!is_valid_ether_addr(addr) && !is_zero_ether_addr(addr))
  809. return -EADDRNOTAVAIL;
  810. } else {
  811. if (!is_valid_ether_addr(addr))
  812. return -EADDRNOTAVAIL;
  813. }
  814. memcpy(netdev->dev_addr, addr, netdev->addr_len);
  815. return 0;
  816. }
  817. static int enic_set_mac_address_dynamic(struct net_device *netdev, void *p)
  818. {
  819. struct enic *enic = netdev_priv(netdev);
  820. struct sockaddr *saddr = p;
  821. char *addr = saddr->sa_data;
  822. int err;
  823. if (netif_running(enic->netdev)) {
  824. err = enic_dev_del_station_addr(enic);
  825. if (err)
  826. return err;
  827. }
  828. err = enic_set_mac_addr(netdev, addr);
  829. if (err)
  830. return err;
  831. if (netif_running(enic->netdev)) {
  832. err = enic_dev_add_station_addr(enic);
  833. if (err)
  834. return err;
  835. }
  836. return err;
  837. }
  838. static int enic_set_mac_address(struct net_device *netdev, void *p)
  839. {
  840. struct sockaddr *saddr = p;
  841. char *addr = saddr->sa_data;
  842. struct enic *enic = netdev_priv(netdev);
  843. int err;
  844. err = enic_dev_del_station_addr(enic);
  845. if (err)
  846. return err;
  847. err = enic_set_mac_addr(netdev, addr);
  848. if (err)
  849. return err;
  850. return enic_dev_add_station_addr(enic);
  851. }
  852. /* netif_tx_lock held, BHs disabled */
  853. static void enic_set_rx_mode(struct net_device *netdev)
  854. {
  855. struct enic *enic = netdev_priv(netdev);
  856. int directed = 1;
  857. int multicast = (netdev->flags & IFF_MULTICAST) ? 1 : 0;
  858. int broadcast = (netdev->flags & IFF_BROADCAST) ? 1 : 0;
  859. int promisc = (netdev->flags & IFF_PROMISC) ||
  860. netdev_uc_count(netdev) > ENIC_UNICAST_PERFECT_FILTERS;
  861. int allmulti = (netdev->flags & IFF_ALLMULTI) ||
  862. netdev_mc_count(netdev) > ENIC_MULTICAST_PERFECT_FILTERS;
  863. unsigned int flags = netdev->flags |
  864. (allmulti ? IFF_ALLMULTI : 0) |
  865. (promisc ? IFF_PROMISC : 0);
  866. if (enic->flags != flags) {
  867. enic->flags = flags;
  868. enic_dev_packet_filter(enic, directed,
  869. multicast, broadcast, promisc, allmulti);
  870. }
  871. if (!promisc) {
  872. __dev_uc_sync(netdev, enic_uc_sync, enic_uc_unsync);
  873. if (!allmulti)
  874. __dev_mc_sync(netdev, enic_mc_sync, enic_mc_unsync);
  875. }
  876. }
  877. /* netif_tx_lock held, BHs disabled */
  878. static void enic_tx_timeout(struct net_device *netdev)
  879. {
  880. struct enic *enic = netdev_priv(netdev);
  881. schedule_work(&enic->tx_hang_reset);
  882. }
  883. static int enic_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
  884. {
  885. struct enic *enic = netdev_priv(netdev);
  886. struct enic_port_profile *pp;
  887. int err;
  888. ENIC_PP_BY_INDEX(enic, vf, pp, &err);
  889. if (err)
  890. return err;
  891. if (is_valid_ether_addr(mac) || is_zero_ether_addr(mac)) {
  892. if (vf == PORT_SELF_VF) {
  893. memcpy(pp->vf_mac, mac, ETH_ALEN);
  894. return 0;
  895. } else {
  896. /*
  897. * For sriov vf's set the mac in hw
  898. */
  899. ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic,
  900. vnic_dev_set_mac_addr, mac);
  901. return enic_dev_status_to_errno(err);
  902. }
  903. } else
  904. return -EINVAL;
  905. }
  906. static int enic_set_vf_port(struct net_device *netdev, int vf,
  907. struct nlattr *port[])
  908. {
  909. struct enic *enic = netdev_priv(netdev);
  910. struct enic_port_profile prev_pp;
  911. struct enic_port_profile *pp;
  912. int err = 0, restore_pp = 1;
  913. ENIC_PP_BY_INDEX(enic, vf, pp, &err);
  914. if (err)
  915. return err;
  916. if (!port[IFLA_PORT_REQUEST])
  917. return -EOPNOTSUPP;
  918. memcpy(&prev_pp, pp, sizeof(*enic->pp));
  919. memset(pp, 0, sizeof(*enic->pp));
  920. pp->set |= ENIC_SET_REQUEST;
  921. pp->request = nla_get_u8(port[IFLA_PORT_REQUEST]);
  922. if (port[IFLA_PORT_PROFILE]) {
  923. pp->set |= ENIC_SET_NAME;
  924. memcpy(pp->name, nla_data(port[IFLA_PORT_PROFILE]),
  925. PORT_PROFILE_MAX);
  926. }
  927. if (port[IFLA_PORT_INSTANCE_UUID]) {
  928. pp->set |= ENIC_SET_INSTANCE;
  929. memcpy(pp->instance_uuid,
  930. nla_data(port[IFLA_PORT_INSTANCE_UUID]), PORT_UUID_MAX);
  931. }
  932. if (port[IFLA_PORT_HOST_UUID]) {
  933. pp->set |= ENIC_SET_HOST;
  934. memcpy(pp->host_uuid,
  935. nla_data(port[IFLA_PORT_HOST_UUID]), PORT_UUID_MAX);
  936. }
  937. if (vf == PORT_SELF_VF) {
  938. /* Special case handling: mac came from IFLA_VF_MAC */
  939. if (!is_zero_ether_addr(prev_pp.vf_mac))
  940. memcpy(pp->mac_addr, prev_pp.vf_mac, ETH_ALEN);
  941. if (is_zero_ether_addr(netdev->dev_addr))
  942. eth_hw_addr_random(netdev);
  943. } else {
  944. /* SR-IOV VF: get mac from adapter */
  945. ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic,
  946. vnic_dev_get_mac_addr, pp->mac_addr);
  947. if (err) {
  948. netdev_err(netdev, "Error getting mac for vf %d\n", vf);
  949. memcpy(pp, &prev_pp, sizeof(*pp));
  950. return enic_dev_status_to_errno(err);
  951. }
  952. }
  953. err = enic_process_set_pp_request(enic, vf, &prev_pp, &restore_pp);
  954. if (err) {
  955. if (restore_pp) {
  956. /* Things are still the way they were: Implicit
  957. * DISASSOCIATE failed
  958. */
  959. memcpy(pp, &prev_pp, sizeof(*pp));
  960. } else {
  961. memset(pp, 0, sizeof(*pp));
  962. if (vf == PORT_SELF_VF)
  963. eth_zero_addr(netdev->dev_addr);
  964. }
  965. } else {
  966. /* Set flag to indicate that the port assoc/disassoc
  967. * request has been sent out to fw
  968. */
  969. pp->set |= ENIC_PORT_REQUEST_APPLIED;
  970. /* If DISASSOCIATE, clean up all assigned/saved macaddresses */
  971. if (pp->request == PORT_REQUEST_DISASSOCIATE) {
  972. eth_zero_addr(pp->mac_addr);
  973. if (vf == PORT_SELF_VF)
  974. eth_zero_addr(netdev->dev_addr);
  975. }
  976. }
  977. if (vf == PORT_SELF_VF)
  978. eth_zero_addr(pp->vf_mac);
  979. return err;
  980. }
  981. static int enic_get_vf_port(struct net_device *netdev, int vf,
  982. struct sk_buff *skb)
  983. {
  984. struct enic *enic = netdev_priv(netdev);
  985. u16 response = PORT_PROFILE_RESPONSE_SUCCESS;
  986. struct enic_port_profile *pp;
  987. int err;
  988. ENIC_PP_BY_INDEX(enic, vf, pp, &err);
  989. if (err)
  990. return err;
  991. if (!(pp->set & ENIC_PORT_REQUEST_APPLIED))
  992. return -ENODATA;
  993. err = enic_process_get_pp_request(enic, vf, pp->request, &response);
  994. if (err)
  995. return err;
  996. if (nla_put_u16(skb, IFLA_PORT_REQUEST, pp->request) ||
  997. nla_put_u16(skb, IFLA_PORT_RESPONSE, response) ||
  998. ((pp->set & ENIC_SET_NAME) &&
  999. nla_put(skb, IFLA_PORT_PROFILE, PORT_PROFILE_MAX, pp->name)) ||
  1000. ((pp->set & ENIC_SET_INSTANCE) &&
  1001. nla_put(skb, IFLA_PORT_INSTANCE_UUID, PORT_UUID_MAX,
  1002. pp->instance_uuid)) ||
  1003. ((pp->set & ENIC_SET_HOST) &&
  1004. nla_put(skb, IFLA_PORT_HOST_UUID, PORT_UUID_MAX, pp->host_uuid)))
  1005. goto nla_put_failure;
  1006. return 0;
  1007. nla_put_failure:
  1008. return -EMSGSIZE;
  1009. }
  1010. static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
  1011. {
  1012. struct enic *enic = vnic_dev_priv(rq->vdev);
  1013. if (!buf->os_buf)
  1014. return;
  1015. pci_unmap_single(enic->pdev, buf->dma_addr,
  1016. buf->len, PCI_DMA_FROMDEVICE);
  1017. dev_kfree_skb_any(buf->os_buf);
  1018. buf->os_buf = NULL;
  1019. }
  1020. static int enic_rq_alloc_buf(struct vnic_rq *rq)
  1021. {
  1022. struct enic *enic = vnic_dev_priv(rq->vdev);
  1023. struct net_device *netdev = enic->netdev;
  1024. struct sk_buff *skb;
  1025. unsigned int len = netdev->mtu + VLAN_ETH_HLEN;
  1026. unsigned int os_buf_index = 0;
  1027. dma_addr_t dma_addr;
  1028. struct vnic_rq_buf *buf = rq->to_use;
  1029. if (buf->os_buf) {
  1030. enic_queue_rq_desc(rq, buf->os_buf, os_buf_index, buf->dma_addr,
  1031. buf->len);
  1032. return 0;
  1033. }
  1034. skb = netdev_alloc_skb_ip_align(netdev, len);
  1035. if (!skb)
  1036. return -ENOMEM;
  1037. dma_addr = pci_map_single(enic->pdev, skb->data, len,
  1038. PCI_DMA_FROMDEVICE);
  1039. if (unlikely(enic_dma_map_check(enic, dma_addr))) {
  1040. dev_kfree_skb(skb);
  1041. return -ENOMEM;
  1042. }
  1043. enic_queue_rq_desc(rq, skb, os_buf_index,
  1044. dma_addr, len);
  1045. return 0;
  1046. }
  1047. static void enic_intr_update_pkt_size(struct vnic_rx_bytes_counter *pkt_size,
  1048. u32 pkt_len)
  1049. {
  1050. if (ENIC_LARGE_PKT_THRESHOLD <= pkt_len)
  1051. pkt_size->large_pkt_bytes_cnt += pkt_len;
  1052. else
  1053. pkt_size->small_pkt_bytes_cnt += pkt_len;
  1054. }
  1055. static bool enic_rxcopybreak(struct net_device *netdev, struct sk_buff **skb,
  1056. struct vnic_rq_buf *buf, u16 len)
  1057. {
  1058. struct enic *enic = netdev_priv(netdev);
  1059. struct sk_buff *new_skb;
  1060. if (len > enic->rx_copybreak)
  1061. return false;
  1062. new_skb = netdev_alloc_skb_ip_align(netdev, len);
  1063. if (!new_skb)
  1064. return false;
  1065. pci_dma_sync_single_for_cpu(enic->pdev, buf->dma_addr, len,
  1066. DMA_FROM_DEVICE);
  1067. memcpy(new_skb->data, (*skb)->data, len);
  1068. *skb = new_skb;
  1069. return true;
  1070. }
  1071. static void enic_rq_indicate_buf(struct vnic_rq *rq,
  1072. struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
  1073. int skipped, void *opaque)
  1074. {
  1075. struct enic *enic = vnic_dev_priv(rq->vdev);
  1076. struct net_device *netdev = enic->netdev;
  1077. struct sk_buff *skb;
  1078. struct vnic_cq *cq = &enic->cq[enic_cq_rq(enic, rq->index)];
  1079. u8 type, color, eop, sop, ingress_port, vlan_stripped;
  1080. u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
  1081. u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
  1082. u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
  1083. u8 packet_error;
  1084. u16 q_number, completed_index, bytes_written, vlan_tci, checksum;
  1085. u32 rss_hash;
  1086. bool outer_csum_ok = true, encap = false;
  1087. if (skipped)
  1088. return;
  1089. skb = buf->os_buf;
  1090. cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
  1091. &type, &color, &q_number, &completed_index,
  1092. &ingress_port, &fcoe, &eop, &sop, &rss_type,
  1093. &csum_not_calc, &rss_hash, &bytes_written,
  1094. &packet_error, &vlan_stripped, &vlan_tci, &checksum,
  1095. &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
  1096. &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
  1097. &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
  1098. &fcs_ok);
  1099. if (packet_error) {
  1100. if (!fcs_ok) {
  1101. if (bytes_written > 0)
  1102. enic->rq_bad_fcs++;
  1103. else if (bytes_written == 0)
  1104. enic->rq_truncated_pkts++;
  1105. }
  1106. pci_unmap_single(enic->pdev, buf->dma_addr, buf->len,
  1107. PCI_DMA_FROMDEVICE);
  1108. dev_kfree_skb_any(skb);
  1109. buf->os_buf = NULL;
  1110. return;
  1111. }
  1112. if (eop && bytes_written > 0) {
  1113. /* Good receive
  1114. */
  1115. if (!enic_rxcopybreak(netdev, &skb, buf, bytes_written)) {
  1116. buf->os_buf = NULL;
  1117. pci_unmap_single(enic->pdev, buf->dma_addr, buf->len,
  1118. PCI_DMA_FROMDEVICE);
  1119. }
  1120. prefetch(skb->data - NET_IP_ALIGN);
  1121. skb_put(skb, bytes_written);
  1122. skb->protocol = eth_type_trans(skb, netdev);
  1123. skb_record_rx_queue(skb, q_number);
  1124. if ((netdev->features & NETIF_F_RXHASH) && rss_hash &&
  1125. (type == 3)) {
  1126. switch (rss_type) {
  1127. case CQ_ENET_RQ_DESC_RSS_TYPE_TCP_IPv4:
  1128. case CQ_ENET_RQ_DESC_RSS_TYPE_TCP_IPv6:
  1129. case CQ_ENET_RQ_DESC_RSS_TYPE_TCP_IPv6_EX:
  1130. skb_set_hash(skb, rss_hash, PKT_HASH_TYPE_L4);
  1131. break;
  1132. case CQ_ENET_RQ_DESC_RSS_TYPE_IPv4:
  1133. case CQ_ENET_RQ_DESC_RSS_TYPE_IPv6:
  1134. case CQ_ENET_RQ_DESC_RSS_TYPE_IPv6_EX:
  1135. skb_set_hash(skb, rss_hash, PKT_HASH_TYPE_L3);
  1136. break;
  1137. }
  1138. }
  1139. if (enic->vxlan.vxlan_udp_port_number) {
  1140. switch (enic->vxlan.patch_level) {
  1141. case 0:
  1142. if (fcoe) {
  1143. encap = true;
  1144. outer_csum_ok = fcoe_fc_crc_ok;
  1145. }
  1146. break;
  1147. case 2:
  1148. if ((type == 7) &&
  1149. (rss_hash & BIT(0))) {
  1150. encap = true;
  1151. outer_csum_ok = (rss_hash & BIT(1)) &&
  1152. (rss_hash & BIT(2));
  1153. }
  1154. break;
  1155. }
  1156. }
  1157. /* Hardware does not provide whole packet checksum. It only
  1158. * provides pseudo checksum. Since hw validates the packet
  1159. * checksum but not provide us the checksum value. use
  1160. * CHECSUM_UNNECESSARY.
  1161. *
  1162. * In case of encap pkt tcp_udp_csum_ok/tcp_udp_csum_ok is
  1163. * inner csum_ok. outer_csum_ok is set by hw when outer udp
  1164. * csum is correct or is zero.
  1165. */
  1166. if ((netdev->features & NETIF_F_RXCSUM) && !csum_not_calc &&
  1167. tcp_udp_csum_ok && ipv4_csum_ok && outer_csum_ok) {
  1168. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1169. skb->csum_level = encap;
  1170. }
  1171. if (vlan_stripped)
  1172. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tci);
  1173. skb_mark_napi_id(skb, &enic->napi[rq->index]);
  1174. if (!(netdev->features & NETIF_F_GRO))
  1175. netif_receive_skb(skb);
  1176. else
  1177. napi_gro_receive(&enic->napi[q_number], skb);
  1178. if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
  1179. enic_intr_update_pkt_size(&cq->pkt_size_counter,
  1180. bytes_written);
  1181. } else {
  1182. /* Buffer overflow
  1183. */
  1184. pci_unmap_single(enic->pdev, buf->dma_addr, buf->len,
  1185. PCI_DMA_FROMDEVICE);
  1186. dev_kfree_skb_any(skb);
  1187. buf->os_buf = NULL;
  1188. }
  1189. }
  1190. static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
  1191. u8 type, u16 q_number, u16 completed_index, void *opaque)
  1192. {
  1193. struct enic *enic = vnic_dev_priv(vdev);
  1194. vnic_rq_service(&enic->rq[q_number], cq_desc,
  1195. completed_index, VNIC_RQ_RETURN_DESC,
  1196. enic_rq_indicate_buf, opaque);
  1197. return 0;
  1198. }
  1199. static void enic_set_int_moderation(struct enic *enic, struct vnic_rq *rq)
  1200. {
  1201. unsigned int intr = enic_msix_rq_intr(enic, rq->index);
  1202. struct vnic_cq *cq = &enic->cq[enic_cq_rq(enic, rq->index)];
  1203. u32 timer = cq->tobe_rx_coal_timeval;
  1204. if (cq->tobe_rx_coal_timeval != cq->cur_rx_coal_timeval) {
  1205. vnic_intr_coalescing_timer_set(&enic->intr[intr], timer);
  1206. cq->cur_rx_coal_timeval = cq->tobe_rx_coal_timeval;
  1207. }
  1208. }
  1209. static void enic_calc_int_moderation(struct enic *enic, struct vnic_rq *rq)
  1210. {
  1211. struct enic_rx_coal *rx_coal = &enic->rx_coalesce_setting;
  1212. struct vnic_cq *cq = &enic->cq[enic_cq_rq(enic, rq->index)];
  1213. struct vnic_rx_bytes_counter *pkt_size_counter = &cq->pkt_size_counter;
  1214. int index;
  1215. u32 timer;
  1216. u32 range_start;
  1217. u32 traffic;
  1218. u64 delta;
  1219. ktime_t now = ktime_get();
  1220. delta = ktime_us_delta(now, cq->prev_ts);
  1221. if (delta < ENIC_AIC_TS_BREAK)
  1222. return;
  1223. cq->prev_ts = now;
  1224. traffic = pkt_size_counter->large_pkt_bytes_cnt +
  1225. pkt_size_counter->small_pkt_bytes_cnt;
  1226. /* The table takes Mbps
  1227. * traffic *= 8 => bits
  1228. * traffic *= (10^6 / delta) => bps
  1229. * traffic /= 10^6 => Mbps
  1230. *
  1231. * Combining, traffic *= (8 / delta)
  1232. */
  1233. traffic <<= 3;
  1234. traffic = delta > UINT_MAX ? 0 : traffic / (u32)delta;
  1235. for (index = 0; index < ENIC_MAX_COALESCE_TIMERS; index++)
  1236. if (traffic < mod_table[index].rx_rate)
  1237. break;
  1238. range_start = (pkt_size_counter->small_pkt_bytes_cnt >
  1239. pkt_size_counter->large_pkt_bytes_cnt << 1) ?
  1240. rx_coal->small_pkt_range_start :
  1241. rx_coal->large_pkt_range_start;
  1242. timer = range_start + ((rx_coal->range_end - range_start) *
  1243. mod_table[index].range_percent / 100);
  1244. /* Damping */
  1245. cq->tobe_rx_coal_timeval = (timer + cq->tobe_rx_coal_timeval) >> 1;
  1246. pkt_size_counter->large_pkt_bytes_cnt = 0;
  1247. pkt_size_counter->small_pkt_bytes_cnt = 0;
  1248. }
  1249. static int enic_poll(struct napi_struct *napi, int budget)
  1250. {
  1251. struct net_device *netdev = napi->dev;
  1252. struct enic *enic = netdev_priv(netdev);
  1253. unsigned int cq_rq = enic_cq_rq(enic, 0);
  1254. unsigned int cq_wq = enic_cq_wq(enic, 0);
  1255. unsigned int intr = enic_legacy_io_intr();
  1256. unsigned int rq_work_to_do = budget;
  1257. unsigned int wq_work_to_do = -1; /* no limit */
  1258. unsigned int work_done, rq_work_done = 0, wq_work_done;
  1259. int err;
  1260. wq_work_done = vnic_cq_service(&enic->cq[cq_wq], wq_work_to_do,
  1261. enic_wq_service, NULL);
  1262. if (budget > 0)
  1263. rq_work_done = vnic_cq_service(&enic->cq[cq_rq],
  1264. rq_work_to_do, enic_rq_service, NULL);
  1265. /* Accumulate intr event credits for this polling
  1266. * cycle. An intr event is the completion of a
  1267. * a WQ or RQ packet.
  1268. */
  1269. work_done = rq_work_done + wq_work_done;
  1270. if (work_done > 0)
  1271. vnic_intr_return_credits(&enic->intr[intr],
  1272. work_done,
  1273. 0 /* don't unmask intr */,
  1274. 0 /* don't reset intr timer */);
  1275. err = vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
  1276. /* Buffer allocation failed. Stay in polling
  1277. * mode so we can try to fill the ring again.
  1278. */
  1279. if (err)
  1280. rq_work_done = rq_work_to_do;
  1281. if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
  1282. /* Call the function which refreshes the intr coalescing timer
  1283. * value based on the traffic.
  1284. */
  1285. enic_calc_int_moderation(enic, &enic->rq[0]);
  1286. if ((rq_work_done < budget) && napi_complete_done(napi, rq_work_done)) {
  1287. /* Some work done, but not enough to stay in polling,
  1288. * exit polling
  1289. */
  1290. if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
  1291. enic_set_int_moderation(enic, &enic->rq[0]);
  1292. vnic_intr_unmask(&enic->intr[intr]);
  1293. }
  1294. return rq_work_done;
  1295. }
  1296. #ifdef CONFIG_RFS_ACCEL
  1297. static void enic_free_rx_cpu_rmap(struct enic *enic)
  1298. {
  1299. free_irq_cpu_rmap(enic->netdev->rx_cpu_rmap);
  1300. enic->netdev->rx_cpu_rmap = NULL;
  1301. }
  1302. static void enic_set_rx_cpu_rmap(struct enic *enic)
  1303. {
  1304. int i, res;
  1305. if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX) {
  1306. enic->netdev->rx_cpu_rmap = alloc_irq_cpu_rmap(enic->rq_count);
  1307. if (unlikely(!enic->netdev->rx_cpu_rmap))
  1308. return;
  1309. for (i = 0; i < enic->rq_count; i++) {
  1310. res = irq_cpu_rmap_add(enic->netdev->rx_cpu_rmap,
  1311. enic->msix_entry[i].vector);
  1312. if (unlikely(res)) {
  1313. enic_free_rx_cpu_rmap(enic);
  1314. return;
  1315. }
  1316. }
  1317. }
  1318. }
  1319. #else
  1320. static void enic_free_rx_cpu_rmap(struct enic *enic)
  1321. {
  1322. }
  1323. static void enic_set_rx_cpu_rmap(struct enic *enic)
  1324. {
  1325. }
  1326. #endif /* CONFIG_RFS_ACCEL */
  1327. static int enic_poll_msix_wq(struct napi_struct *napi, int budget)
  1328. {
  1329. struct net_device *netdev = napi->dev;
  1330. struct enic *enic = netdev_priv(netdev);
  1331. unsigned int wq_index = (napi - &enic->napi[0]) - enic->rq_count;
  1332. struct vnic_wq *wq = &enic->wq[wq_index];
  1333. unsigned int cq;
  1334. unsigned int intr;
  1335. unsigned int wq_work_to_do = -1; /* clean all desc possible */
  1336. unsigned int wq_work_done;
  1337. unsigned int wq_irq;
  1338. wq_irq = wq->index;
  1339. cq = enic_cq_wq(enic, wq_irq);
  1340. intr = enic_msix_wq_intr(enic, wq_irq);
  1341. wq_work_done = vnic_cq_service(&enic->cq[cq], wq_work_to_do,
  1342. enic_wq_service, NULL);
  1343. vnic_intr_return_credits(&enic->intr[intr], wq_work_done,
  1344. 0 /* don't unmask intr */,
  1345. 1 /* reset intr timer */);
  1346. if (!wq_work_done) {
  1347. napi_complete(napi);
  1348. vnic_intr_unmask(&enic->intr[intr]);
  1349. return 0;
  1350. }
  1351. return budget;
  1352. }
  1353. static int enic_poll_msix_rq(struct napi_struct *napi, int budget)
  1354. {
  1355. struct net_device *netdev = napi->dev;
  1356. struct enic *enic = netdev_priv(netdev);
  1357. unsigned int rq = (napi - &enic->napi[0]);
  1358. unsigned int cq = enic_cq_rq(enic, rq);
  1359. unsigned int intr = enic_msix_rq_intr(enic, rq);
  1360. unsigned int work_to_do = budget;
  1361. unsigned int work_done = 0;
  1362. int err;
  1363. /* Service RQ
  1364. */
  1365. if (budget > 0)
  1366. work_done = vnic_cq_service(&enic->cq[cq],
  1367. work_to_do, enic_rq_service, NULL);
  1368. /* Return intr event credits for this polling
  1369. * cycle. An intr event is the completion of a
  1370. * RQ packet.
  1371. */
  1372. if (work_done > 0)
  1373. vnic_intr_return_credits(&enic->intr[intr],
  1374. work_done,
  1375. 0 /* don't unmask intr */,
  1376. 0 /* don't reset intr timer */);
  1377. err = vnic_rq_fill(&enic->rq[rq], enic_rq_alloc_buf);
  1378. /* Buffer allocation failed. Stay in polling mode
  1379. * so we can try to fill the ring again.
  1380. */
  1381. if (err)
  1382. work_done = work_to_do;
  1383. if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
  1384. /* Call the function which refreshes the intr coalescing timer
  1385. * value based on the traffic.
  1386. */
  1387. enic_calc_int_moderation(enic, &enic->rq[rq]);
  1388. if ((work_done < budget) && napi_complete_done(napi, work_done)) {
  1389. /* Some work done, but not enough to stay in polling,
  1390. * exit polling
  1391. */
  1392. if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
  1393. enic_set_int_moderation(enic, &enic->rq[rq]);
  1394. vnic_intr_unmask(&enic->intr[intr]);
  1395. }
  1396. return work_done;
  1397. }
  1398. static void enic_notify_timer(unsigned long data)
  1399. {
  1400. struct enic *enic = (struct enic *)data;
  1401. enic_notify_check(enic);
  1402. mod_timer(&enic->notify_timer,
  1403. round_jiffies(jiffies + ENIC_NOTIFY_TIMER_PERIOD));
  1404. }
  1405. static void enic_free_intr(struct enic *enic)
  1406. {
  1407. struct net_device *netdev = enic->netdev;
  1408. unsigned int i;
  1409. enic_free_rx_cpu_rmap(enic);
  1410. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  1411. case VNIC_DEV_INTR_MODE_INTX:
  1412. free_irq(enic->pdev->irq, netdev);
  1413. break;
  1414. case VNIC_DEV_INTR_MODE_MSI:
  1415. free_irq(enic->pdev->irq, enic);
  1416. break;
  1417. case VNIC_DEV_INTR_MODE_MSIX:
  1418. for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
  1419. if (enic->msix[i].requested)
  1420. free_irq(enic->msix_entry[i].vector,
  1421. enic->msix[i].devid);
  1422. break;
  1423. default:
  1424. break;
  1425. }
  1426. }
  1427. static int enic_request_intr(struct enic *enic)
  1428. {
  1429. struct net_device *netdev = enic->netdev;
  1430. unsigned int i, intr;
  1431. int err = 0;
  1432. enic_set_rx_cpu_rmap(enic);
  1433. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  1434. case VNIC_DEV_INTR_MODE_INTX:
  1435. err = request_irq(enic->pdev->irq, enic_isr_legacy,
  1436. IRQF_SHARED, netdev->name, netdev);
  1437. break;
  1438. case VNIC_DEV_INTR_MODE_MSI:
  1439. err = request_irq(enic->pdev->irq, enic_isr_msi,
  1440. 0, netdev->name, enic);
  1441. break;
  1442. case VNIC_DEV_INTR_MODE_MSIX:
  1443. for (i = 0; i < enic->rq_count; i++) {
  1444. intr = enic_msix_rq_intr(enic, i);
  1445. snprintf(enic->msix[intr].devname,
  1446. sizeof(enic->msix[intr].devname),
  1447. "%s-rx-%u", netdev->name, i);
  1448. enic->msix[intr].isr = enic_isr_msix;
  1449. enic->msix[intr].devid = &enic->napi[i];
  1450. }
  1451. for (i = 0; i < enic->wq_count; i++) {
  1452. int wq = enic_cq_wq(enic, i);
  1453. intr = enic_msix_wq_intr(enic, i);
  1454. snprintf(enic->msix[intr].devname,
  1455. sizeof(enic->msix[intr].devname),
  1456. "%s-tx-%u", netdev->name, i);
  1457. enic->msix[intr].isr = enic_isr_msix;
  1458. enic->msix[intr].devid = &enic->napi[wq];
  1459. }
  1460. intr = enic_msix_err_intr(enic);
  1461. snprintf(enic->msix[intr].devname,
  1462. sizeof(enic->msix[intr].devname),
  1463. "%s-err", netdev->name);
  1464. enic->msix[intr].isr = enic_isr_msix_err;
  1465. enic->msix[intr].devid = enic;
  1466. intr = enic_msix_notify_intr(enic);
  1467. snprintf(enic->msix[intr].devname,
  1468. sizeof(enic->msix[intr].devname),
  1469. "%s-notify", netdev->name);
  1470. enic->msix[intr].isr = enic_isr_msix_notify;
  1471. enic->msix[intr].devid = enic;
  1472. for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
  1473. enic->msix[i].requested = 0;
  1474. for (i = 0; i < enic->intr_count; i++) {
  1475. err = request_irq(enic->msix_entry[i].vector,
  1476. enic->msix[i].isr, 0,
  1477. enic->msix[i].devname,
  1478. enic->msix[i].devid);
  1479. if (err) {
  1480. enic_free_intr(enic);
  1481. break;
  1482. }
  1483. enic->msix[i].requested = 1;
  1484. }
  1485. break;
  1486. default:
  1487. break;
  1488. }
  1489. return err;
  1490. }
  1491. static void enic_synchronize_irqs(struct enic *enic)
  1492. {
  1493. unsigned int i;
  1494. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  1495. case VNIC_DEV_INTR_MODE_INTX:
  1496. case VNIC_DEV_INTR_MODE_MSI:
  1497. synchronize_irq(enic->pdev->irq);
  1498. break;
  1499. case VNIC_DEV_INTR_MODE_MSIX:
  1500. for (i = 0; i < enic->intr_count; i++)
  1501. synchronize_irq(enic->msix_entry[i].vector);
  1502. break;
  1503. default:
  1504. break;
  1505. }
  1506. }
  1507. static void enic_set_rx_coal_setting(struct enic *enic)
  1508. {
  1509. unsigned int speed;
  1510. int index = -1;
  1511. struct enic_rx_coal *rx_coal = &enic->rx_coalesce_setting;
  1512. /* 1. Read the link speed from fw
  1513. * 2. Pick the default range for the speed
  1514. * 3. Update it in enic->rx_coalesce_setting
  1515. */
  1516. speed = vnic_dev_port_speed(enic->vdev);
  1517. if (ENIC_LINK_SPEED_10G < speed)
  1518. index = ENIC_LINK_40G_INDEX;
  1519. else if (ENIC_LINK_SPEED_4G < speed)
  1520. index = ENIC_LINK_10G_INDEX;
  1521. else
  1522. index = ENIC_LINK_4G_INDEX;
  1523. rx_coal->small_pkt_range_start = mod_range[index].small_pkt_range_start;
  1524. rx_coal->large_pkt_range_start = mod_range[index].large_pkt_range_start;
  1525. rx_coal->range_end = ENIC_RX_COALESCE_RANGE_END;
  1526. /* Start with the value provided by UCSM */
  1527. for (index = 0; index < enic->rq_count; index++)
  1528. enic->cq[index].cur_rx_coal_timeval =
  1529. enic->config.intr_timer_usec;
  1530. rx_coal->use_adaptive_rx_coalesce = 1;
  1531. }
  1532. static int enic_dev_notify_set(struct enic *enic)
  1533. {
  1534. int err;
  1535. spin_lock_bh(&enic->devcmd_lock);
  1536. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  1537. case VNIC_DEV_INTR_MODE_INTX:
  1538. err = vnic_dev_notify_set(enic->vdev,
  1539. enic_legacy_notify_intr());
  1540. break;
  1541. case VNIC_DEV_INTR_MODE_MSIX:
  1542. err = vnic_dev_notify_set(enic->vdev,
  1543. enic_msix_notify_intr(enic));
  1544. break;
  1545. default:
  1546. err = vnic_dev_notify_set(enic->vdev, -1 /* no intr */);
  1547. break;
  1548. }
  1549. spin_unlock_bh(&enic->devcmd_lock);
  1550. return err;
  1551. }
  1552. static void enic_notify_timer_start(struct enic *enic)
  1553. {
  1554. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  1555. case VNIC_DEV_INTR_MODE_MSI:
  1556. mod_timer(&enic->notify_timer, jiffies);
  1557. break;
  1558. default:
  1559. /* Using intr for notification for INTx/MSI-X */
  1560. break;
  1561. }
  1562. }
  1563. /* rtnl lock is held, process context */
  1564. static int enic_open(struct net_device *netdev)
  1565. {
  1566. struct enic *enic = netdev_priv(netdev);
  1567. unsigned int i;
  1568. int err;
  1569. err = enic_request_intr(enic);
  1570. if (err) {
  1571. netdev_err(netdev, "Unable to request irq.\n");
  1572. return err;
  1573. }
  1574. enic_init_affinity_hint(enic);
  1575. enic_set_affinity_hint(enic);
  1576. err = enic_dev_notify_set(enic);
  1577. if (err) {
  1578. netdev_err(netdev,
  1579. "Failed to alloc notify buffer, aborting.\n");
  1580. goto err_out_free_intr;
  1581. }
  1582. for (i = 0; i < enic->rq_count; i++) {
  1583. vnic_rq_fill(&enic->rq[i], enic_rq_alloc_buf);
  1584. /* Need at least one buffer on ring to get going */
  1585. if (vnic_rq_desc_used(&enic->rq[i]) == 0) {
  1586. netdev_err(netdev, "Unable to alloc receive buffers\n");
  1587. err = -ENOMEM;
  1588. goto err_out_free_rq;
  1589. }
  1590. }
  1591. for (i = 0; i < enic->wq_count; i++)
  1592. vnic_wq_enable(&enic->wq[i]);
  1593. for (i = 0; i < enic->rq_count; i++)
  1594. vnic_rq_enable(&enic->rq[i]);
  1595. if (!enic_is_dynamic(enic) && !enic_is_sriov_vf(enic))
  1596. enic_dev_add_station_addr(enic);
  1597. enic_set_rx_mode(netdev);
  1598. netif_tx_wake_all_queues(netdev);
  1599. for (i = 0; i < enic->rq_count; i++)
  1600. napi_enable(&enic->napi[i]);
  1601. if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX)
  1602. for (i = 0; i < enic->wq_count; i++)
  1603. napi_enable(&enic->napi[enic_cq_wq(enic, i)]);
  1604. enic_dev_enable(enic);
  1605. for (i = 0; i < enic->intr_count; i++)
  1606. vnic_intr_unmask(&enic->intr[i]);
  1607. enic_notify_timer_start(enic);
  1608. enic_rfs_flw_tbl_init(enic);
  1609. return 0;
  1610. err_out_free_rq:
  1611. for (i = 0; i < enic->rq_count; i++)
  1612. vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
  1613. enic_dev_notify_unset(enic);
  1614. err_out_free_intr:
  1615. enic_unset_affinity_hint(enic);
  1616. enic_free_intr(enic);
  1617. return err;
  1618. }
  1619. /* rtnl lock is held, process context */
  1620. static int enic_stop(struct net_device *netdev)
  1621. {
  1622. struct enic *enic = netdev_priv(netdev);
  1623. unsigned int i;
  1624. int err;
  1625. for (i = 0; i < enic->intr_count; i++) {
  1626. vnic_intr_mask(&enic->intr[i]);
  1627. (void)vnic_intr_masked(&enic->intr[i]); /* flush write */
  1628. }
  1629. enic_synchronize_irqs(enic);
  1630. del_timer_sync(&enic->notify_timer);
  1631. enic_rfs_flw_tbl_free(enic);
  1632. enic_dev_disable(enic);
  1633. for (i = 0; i < enic->rq_count; i++)
  1634. napi_disable(&enic->napi[i]);
  1635. netif_carrier_off(netdev);
  1636. netif_tx_disable(netdev);
  1637. if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX)
  1638. for (i = 0; i < enic->wq_count; i++)
  1639. napi_disable(&enic->napi[enic_cq_wq(enic, i)]);
  1640. if (!enic_is_dynamic(enic) && !enic_is_sriov_vf(enic))
  1641. enic_dev_del_station_addr(enic);
  1642. for (i = 0; i < enic->wq_count; i++) {
  1643. err = vnic_wq_disable(&enic->wq[i]);
  1644. if (err)
  1645. return err;
  1646. }
  1647. for (i = 0; i < enic->rq_count; i++) {
  1648. err = vnic_rq_disable(&enic->rq[i]);
  1649. if (err)
  1650. return err;
  1651. }
  1652. enic_dev_notify_unset(enic);
  1653. enic_unset_affinity_hint(enic);
  1654. enic_free_intr(enic);
  1655. for (i = 0; i < enic->wq_count; i++)
  1656. vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
  1657. for (i = 0; i < enic->rq_count; i++)
  1658. vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
  1659. for (i = 0; i < enic->cq_count; i++)
  1660. vnic_cq_clean(&enic->cq[i]);
  1661. for (i = 0; i < enic->intr_count; i++)
  1662. vnic_intr_clean(&enic->intr[i]);
  1663. return 0;
  1664. }
  1665. static int enic_change_mtu(struct net_device *netdev, int new_mtu)
  1666. {
  1667. struct enic *enic = netdev_priv(netdev);
  1668. int running = netif_running(netdev);
  1669. if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic))
  1670. return -EOPNOTSUPP;
  1671. if (running)
  1672. enic_stop(netdev);
  1673. netdev->mtu = new_mtu;
  1674. if (netdev->mtu > enic->port_mtu)
  1675. netdev_warn(netdev,
  1676. "interface MTU (%d) set higher than port MTU (%d)\n",
  1677. netdev->mtu, enic->port_mtu);
  1678. if (running)
  1679. enic_open(netdev);
  1680. return 0;
  1681. }
  1682. static void enic_change_mtu_work(struct work_struct *work)
  1683. {
  1684. struct enic *enic = container_of(work, struct enic, change_mtu_work);
  1685. struct net_device *netdev = enic->netdev;
  1686. int new_mtu = vnic_dev_mtu(enic->vdev);
  1687. int err;
  1688. unsigned int i;
  1689. new_mtu = max_t(int, ENIC_MIN_MTU, min_t(int, ENIC_MAX_MTU, new_mtu));
  1690. rtnl_lock();
  1691. /* Stop RQ */
  1692. del_timer_sync(&enic->notify_timer);
  1693. for (i = 0; i < enic->rq_count; i++)
  1694. napi_disable(&enic->napi[i]);
  1695. vnic_intr_mask(&enic->intr[0]);
  1696. enic_synchronize_irqs(enic);
  1697. err = vnic_rq_disable(&enic->rq[0]);
  1698. if (err) {
  1699. rtnl_unlock();
  1700. netdev_err(netdev, "Unable to disable RQ.\n");
  1701. return;
  1702. }
  1703. vnic_rq_clean(&enic->rq[0], enic_free_rq_buf);
  1704. vnic_cq_clean(&enic->cq[0]);
  1705. vnic_intr_clean(&enic->intr[0]);
  1706. /* Fill RQ with new_mtu-sized buffers */
  1707. netdev->mtu = new_mtu;
  1708. vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
  1709. /* Need at least one buffer on ring to get going */
  1710. if (vnic_rq_desc_used(&enic->rq[0]) == 0) {
  1711. rtnl_unlock();
  1712. netdev_err(netdev, "Unable to alloc receive buffers.\n");
  1713. return;
  1714. }
  1715. /* Start RQ */
  1716. vnic_rq_enable(&enic->rq[0]);
  1717. napi_enable(&enic->napi[0]);
  1718. vnic_intr_unmask(&enic->intr[0]);
  1719. enic_notify_timer_start(enic);
  1720. rtnl_unlock();
  1721. netdev_info(netdev, "interface MTU set as %d\n", netdev->mtu);
  1722. }
  1723. #ifdef CONFIG_NET_POLL_CONTROLLER
  1724. static void enic_poll_controller(struct net_device *netdev)
  1725. {
  1726. struct enic *enic = netdev_priv(netdev);
  1727. struct vnic_dev *vdev = enic->vdev;
  1728. unsigned int i, intr;
  1729. switch (vnic_dev_get_intr_mode(vdev)) {
  1730. case VNIC_DEV_INTR_MODE_MSIX:
  1731. for (i = 0; i < enic->rq_count; i++) {
  1732. intr = enic_msix_rq_intr(enic, i);
  1733. enic_isr_msix(enic->msix_entry[intr].vector,
  1734. &enic->napi[i]);
  1735. }
  1736. for (i = 0; i < enic->wq_count; i++) {
  1737. intr = enic_msix_wq_intr(enic, i);
  1738. enic_isr_msix(enic->msix_entry[intr].vector,
  1739. &enic->napi[enic_cq_wq(enic, i)]);
  1740. }
  1741. break;
  1742. case VNIC_DEV_INTR_MODE_MSI:
  1743. enic_isr_msi(enic->pdev->irq, enic);
  1744. break;
  1745. case VNIC_DEV_INTR_MODE_INTX:
  1746. enic_isr_legacy(enic->pdev->irq, netdev);
  1747. break;
  1748. default:
  1749. break;
  1750. }
  1751. }
  1752. #endif
  1753. static int enic_dev_wait(struct vnic_dev *vdev,
  1754. int (*start)(struct vnic_dev *, int),
  1755. int (*finished)(struct vnic_dev *, int *),
  1756. int arg)
  1757. {
  1758. unsigned long time;
  1759. int done;
  1760. int err;
  1761. BUG_ON(in_interrupt());
  1762. err = start(vdev, arg);
  1763. if (err)
  1764. return err;
  1765. /* Wait for func to complete...2 seconds max
  1766. */
  1767. time = jiffies + (HZ * 2);
  1768. do {
  1769. err = finished(vdev, &done);
  1770. if (err)
  1771. return err;
  1772. if (done)
  1773. return 0;
  1774. schedule_timeout_uninterruptible(HZ / 10);
  1775. } while (time_after(time, jiffies));
  1776. return -ETIMEDOUT;
  1777. }
  1778. static int enic_dev_open(struct enic *enic)
  1779. {
  1780. int err;
  1781. err = enic_dev_wait(enic->vdev, vnic_dev_open,
  1782. vnic_dev_open_done, 0);
  1783. if (err)
  1784. dev_err(enic_get_dev(enic), "vNIC device open failed, err %d\n",
  1785. err);
  1786. return err;
  1787. }
  1788. static int enic_dev_soft_reset(struct enic *enic)
  1789. {
  1790. int err;
  1791. err = enic_dev_wait(enic->vdev, vnic_dev_soft_reset,
  1792. vnic_dev_soft_reset_done, 0);
  1793. if (err)
  1794. netdev_err(enic->netdev, "vNIC soft reset failed, err %d\n",
  1795. err);
  1796. return err;
  1797. }
  1798. static int enic_dev_hang_reset(struct enic *enic)
  1799. {
  1800. int err;
  1801. err = enic_dev_wait(enic->vdev, vnic_dev_hang_reset,
  1802. vnic_dev_hang_reset_done, 0);
  1803. if (err)
  1804. netdev_err(enic->netdev, "vNIC hang reset failed, err %d\n",
  1805. err);
  1806. return err;
  1807. }
  1808. int __enic_set_rsskey(struct enic *enic)
  1809. {
  1810. union vnic_rss_key *rss_key_buf_va;
  1811. dma_addr_t rss_key_buf_pa;
  1812. int i, kidx, bidx, err;
  1813. rss_key_buf_va = pci_zalloc_consistent(enic->pdev,
  1814. sizeof(union vnic_rss_key),
  1815. &rss_key_buf_pa);
  1816. if (!rss_key_buf_va)
  1817. return -ENOMEM;
  1818. for (i = 0; i < ENIC_RSS_LEN; i++) {
  1819. kidx = i / ENIC_RSS_BYTES_PER_KEY;
  1820. bidx = i % ENIC_RSS_BYTES_PER_KEY;
  1821. rss_key_buf_va->key[kidx].b[bidx] = enic->rss_key[i];
  1822. }
  1823. spin_lock_bh(&enic->devcmd_lock);
  1824. err = enic_set_rss_key(enic,
  1825. rss_key_buf_pa,
  1826. sizeof(union vnic_rss_key));
  1827. spin_unlock_bh(&enic->devcmd_lock);
  1828. pci_free_consistent(enic->pdev, sizeof(union vnic_rss_key),
  1829. rss_key_buf_va, rss_key_buf_pa);
  1830. return err;
  1831. }
  1832. static int enic_set_rsskey(struct enic *enic)
  1833. {
  1834. netdev_rss_key_fill(enic->rss_key, ENIC_RSS_LEN);
  1835. return __enic_set_rsskey(enic);
  1836. }
  1837. static int enic_set_rsscpu(struct enic *enic, u8 rss_hash_bits)
  1838. {
  1839. dma_addr_t rss_cpu_buf_pa;
  1840. union vnic_rss_cpu *rss_cpu_buf_va = NULL;
  1841. unsigned int i;
  1842. int err;
  1843. rss_cpu_buf_va = pci_alloc_consistent(enic->pdev,
  1844. sizeof(union vnic_rss_cpu), &rss_cpu_buf_pa);
  1845. if (!rss_cpu_buf_va)
  1846. return -ENOMEM;
  1847. for (i = 0; i < (1 << rss_hash_bits); i++)
  1848. (*rss_cpu_buf_va).cpu[i/4].b[i%4] = i % enic->rq_count;
  1849. spin_lock_bh(&enic->devcmd_lock);
  1850. err = enic_set_rss_cpu(enic,
  1851. rss_cpu_buf_pa,
  1852. sizeof(union vnic_rss_cpu));
  1853. spin_unlock_bh(&enic->devcmd_lock);
  1854. pci_free_consistent(enic->pdev, sizeof(union vnic_rss_cpu),
  1855. rss_cpu_buf_va, rss_cpu_buf_pa);
  1856. return err;
  1857. }
  1858. static int enic_set_niccfg(struct enic *enic, u8 rss_default_cpu,
  1859. u8 rss_hash_type, u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable)
  1860. {
  1861. const u8 tso_ipid_split_en = 0;
  1862. const u8 ig_vlan_strip_en = 1;
  1863. int err;
  1864. /* Enable VLAN tag stripping.
  1865. */
  1866. spin_lock_bh(&enic->devcmd_lock);
  1867. err = enic_set_nic_cfg(enic,
  1868. rss_default_cpu, rss_hash_type,
  1869. rss_hash_bits, rss_base_cpu,
  1870. rss_enable, tso_ipid_split_en,
  1871. ig_vlan_strip_en);
  1872. spin_unlock_bh(&enic->devcmd_lock);
  1873. return err;
  1874. }
  1875. static int enic_set_rss_nic_cfg(struct enic *enic)
  1876. {
  1877. struct device *dev = enic_get_dev(enic);
  1878. const u8 rss_default_cpu = 0;
  1879. const u8 rss_hash_type = NIC_CFG_RSS_HASH_TYPE_IPV4 |
  1880. NIC_CFG_RSS_HASH_TYPE_TCP_IPV4 |
  1881. NIC_CFG_RSS_HASH_TYPE_IPV6 |
  1882. NIC_CFG_RSS_HASH_TYPE_TCP_IPV6;
  1883. const u8 rss_hash_bits = 7;
  1884. const u8 rss_base_cpu = 0;
  1885. u8 rss_enable = ENIC_SETTING(enic, RSS) && (enic->rq_count > 1);
  1886. if (rss_enable) {
  1887. if (!enic_set_rsskey(enic)) {
  1888. if (enic_set_rsscpu(enic, rss_hash_bits)) {
  1889. rss_enable = 0;
  1890. dev_warn(dev, "RSS disabled, "
  1891. "Failed to set RSS cpu indirection table.");
  1892. }
  1893. } else {
  1894. rss_enable = 0;
  1895. dev_warn(dev, "RSS disabled, Failed to set RSS key.\n");
  1896. }
  1897. }
  1898. return enic_set_niccfg(enic, rss_default_cpu, rss_hash_type,
  1899. rss_hash_bits, rss_base_cpu, rss_enable);
  1900. }
  1901. static void enic_reset(struct work_struct *work)
  1902. {
  1903. struct enic *enic = container_of(work, struct enic, reset);
  1904. if (!netif_running(enic->netdev))
  1905. return;
  1906. rtnl_lock();
  1907. spin_lock(&enic->enic_api_lock);
  1908. enic_stop(enic->netdev);
  1909. enic_dev_soft_reset(enic);
  1910. enic_reset_addr_lists(enic);
  1911. enic_init_vnic_resources(enic);
  1912. enic_set_rss_nic_cfg(enic);
  1913. enic_dev_set_ig_vlan_rewrite_mode(enic);
  1914. enic_open(enic->netdev);
  1915. spin_unlock(&enic->enic_api_lock);
  1916. call_netdevice_notifiers(NETDEV_REBOOT, enic->netdev);
  1917. rtnl_unlock();
  1918. }
  1919. static void enic_tx_hang_reset(struct work_struct *work)
  1920. {
  1921. struct enic *enic = container_of(work, struct enic, tx_hang_reset);
  1922. rtnl_lock();
  1923. spin_lock(&enic->enic_api_lock);
  1924. enic_dev_hang_notify(enic);
  1925. enic_stop(enic->netdev);
  1926. enic_dev_hang_reset(enic);
  1927. enic_reset_addr_lists(enic);
  1928. enic_init_vnic_resources(enic);
  1929. enic_set_rss_nic_cfg(enic);
  1930. enic_dev_set_ig_vlan_rewrite_mode(enic);
  1931. enic_open(enic->netdev);
  1932. spin_unlock(&enic->enic_api_lock);
  1933. call_netdevice_notifiers(NETDEV_REBOOT, enic->netdev);
  1934. rtnl_unlock();
  1935. }
  1936. static int enic_set_intr_mode(struct enic *enic)
  1937. {
  1938. unsigned int n = min_t(unsigned int, enic->rq_count, ENIC_RQ_MAX);
  1939. unsigned int m = min_t(unsigned int, enic->wq_count, ENIC_WQ_MAX);
  1940. unsigned int i;
  1941. /* Set interrupt mode (INTx, MSI, MSI-X) depending
  1942. * on system capabilities.
  1943. *
  1944. * Try MSI-X first
  1945. *
  1946. * We need n RQs, m WQs, n+m CQs, and n+m+2 INTRs
  1947. * (the second to last INTR is used for WQ/RQ errors)
  1948. * (the last INTR is used for notifications)
  1949. */
  1950. BUG_ON(ARRAY_SIZE(enic->msix_entry) < n + m + 2);
  1951. for (i = 0; i < n + m + 2; i++)
  1952. enic->msix_entry[i].entry = i;
  1953. /* Use multiple RQs if RSS is enabled
  1954. */
  1955. if (ENIC_SETTING(enic, RSS) &&
  1956. enic->config.intr_mode < 1 &&
  1957. enic->rq_count >= n &&
  1958. enic->wq_count >= m &&
  1959. enic->cq_count >= n + m &&
  1960. enic->intr_count >= n + m + 2) {
  1961. if (pci_enable_msix_range(enic->pdev, enic->msix_entry,
  1962. n + m + 2, n + m + 2) > 0) {
  1963. enic->rq_count = n;
  1964. enic->wq_count = m;
  1965. enic->cq_count = n + m;
  1966. enic->intr_count = n + m + 2;
  1967. vnic_dev_set_intr_mode(enic->vdev,
  1968. VNIC_DEV_INTR_MODE_MSIX);
  1969. return 0;
  1970. }
  1971. }
  1972. if (enic->config.intr_mode < 1 &&
  1973. enic->rq_count >= 1 &&
  1974. enic->wq_count >= m &&
  1975. enic->cq_count >= 1 + m &&
  1976. enic->intr_count >= 1 + m + 2) {
  1977. if (pci_enable_msix_range(enic->pdev, enic->msix_entry,
  1978. 1 + m + 2, 1 + m + 2) > 0) {
  1979. enic->rq_count = 1;
  1980. enic->wq_count = m;
  1981. enic->cq_count = 1 + m;
  1982. enic->intr_count = 1 + m + 2;
  1983. vnic_dev_set_intr_mode(enic->vdev,
  1984. VNIC_DEV_INTR_MODE_MSIX);
  1985. return 0;
  1986. }
  1987. }
  1988. /* Next try MSI
  1989. *
  1990. * We need 1 RQ, 1 WQ, 2 CQs, and 1 INTR
  1991. */
  1992. if (enic->config.intr_mode < 2 &&
  1993. enic->rq_count >= 1 &&
  1994. enic->wq_count >= 1 &&
  1995. enic->cq_count >= 2 &&
  1996. enic->intr_count >= 1 &&
  1997. !pci_enable_msi(enic->pdev)) {
  1998. enic->rq_count = 1;
  1999. enic->wq_count = 1;
  2000. enic->cq_count = 2;
  2001. enic->intr_count = 1;
  2002. vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSI);
  2003. return 0;
  2004. }
  2005. /* Next try INTx
  2006. *
  2007. * We need 1 RQ, 1 WQ, 2 CQs, and 3 INTRs
  2008. * (the first INTR is used for WQ/RQ)
  2009. * (the second INTR is used for WQ/RQ errors)
  2010. * (the last INTR is used for notifications)
  2011. */
  2012. if (enic->config.intr_mode < 3 &&
  2013. enic->rq_count >= 1 &&
  2014. enic->wq_count >= 1 &&
  2015. enic->cq_count >= 2 &&
  2016. enic->intr_count >= 3) {
  2017. enic->rq_count = 1;
  2018. enic->wq_count = 1;
  2019. enic->cq_count = 2;
  2020. enic->intr_count = 3;
  2021. vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_INTX);
  2022. return 0;
  2023. }
  2024. vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
  2025. return -EINVAL;
  2026. }
  2027. static void enic_clear_intr_mode(struct enic *enic)
  2028. {
  2029. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  2030. case VNIC_DEV_INTR_MODE_MSIX:
  2031. pci_disable_msix(enic->pdev);
  2032. break;
  2033. case VNIC_DEV_INTR_MODE_MSI:
  2034. pci_disable_msi(enic->pdev);
  2035. break;
  2036. default:
  2037. break;
  2038. }
  2039. vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
  2040. }
  2041. static const struct net_device_ops enic_netdev_dynamic_ops = {
  2042. .ndo_open = enic_open,
  2043. .ndo_stop = enic_stop,
  2044. .ndo_start_xmit = enic_hard_start_xmit,
  2045. .ndo_get_stats64 = enic_get_stats,
  2046. .ndo_validate_addr = eth_validate_addr,
  2047. .ndo_set_rx_mode = enic_set_rx_mode,
  2048. .ndo_set_mac_address = enic_set_mac_address_dynamic,
  2049. .ndo_change_mtu = enic_change_mtu,
  2050. .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
  2051. .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
  2052. .ndo_tx_timeout = enic_tx_timeout,
  2053. .ndo_set_vf_port = enic_set_vf_port,
  2054. .ndo_get_vf_port = enic_get_vf_port,
  2055. .ndo_set_vf_mac = enic_set_vf_mac,
  2056. #ifdef CONFIG_NET_POLL_CONTROLLER
  2057. .ndo_poll_controller = enic_poll_controller,
  2058. #endif
  2059. #ifdef CONFIG_RFS_ACCEL
  2060. .ndo_rx_flow_steer = enic_rx_flow_steer,
  2061. #endif
  2062. .ndo_udp_tunnel_add = enic_udp_tunnel_add,
  2063. .ndo_udp_tunnel_del = enic_udp_tunnel_del,
  2064. .ndo_features_check = enic_features_check,
  2065. };
  2066. static const struct net_device_ops enic_netdev_ops = {
  2067. .ndo_open = enic_open,
  2068. .ndo_stop = enic_stop,
  2069. .ndo_start_xmit = enic_hard_start_xmit,
  2070. .ndo_get_stats64 = enic_get_stats,
  2071. .ndo_validate_addr = eth_validate_addr,
  2072. .ndo_set_mac_address = enic_set_mac_address,
  2073. .ndo_set_rx_mode = enic_set_rx_mode,
  2074. .ndo_change_mtu = enic_change_mtu,
  2075. .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
  2076. .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
  2077. .ndo_tx_timeout = enic_tx_timeout,
  2078. .ndo_set_vf_port = enic_set_vf_port,
  2079. .ndo_get_vf_port = enic_get_vf_port,
  2080. .ndo_set_vf_mac = enic_set_vf_mac,
  2081. #ifdef CONFIG_NET_POLL_CONTROLLER
  2082. .ndo_poll_controller = enic_poll_controller,
  2083. #endif
  2084. #ifdef CONFIG_RFS_ACCEL
  2085. .ndo_rx_flow_steer = enic_rx_flow_steer,
  2086. #endif
  2087. .ndo_udp_tunnel_add = enic_udp_tunnel_add,
  2088. .ndo_udp_tunnel_del = enic_udp_tunnel_del,
  2089. .ndo_features_check = enic_features_check,
  2090. };
  2091. static void enic_dev_deinit(struct enic *enic)
  2092. {
  2093. unsigned int i;
  2094. for (i = 0; i < enic->rq_count; i++) {
  2095. napi_hash_del(&enic->napi[i]);
  2096. netif_napi_del(&enic->napi[i]);
  2097. }
  2098. if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX)
  2099. for (i = 0; i < enic->wq_count; i++)
  2100. netif_napi_del(&enic->napi[enic_cq_wq(enic, i)]);
  2101. enic_free_vnic_resources(enic);
  2102. enic_clear_intr_mode(enic);
  2103. enic_free_affinity_hint(enic);
  2104. }
  2105. static void enic_kdump_kernel_config(struct enic *enic)
  2106. {
  2107. if (is_kdump_kernel()) {
  2108. dev_info(enic_get_dev(enic), "Running from within kdump kernel. Using minimal resources\n");
  2109. enic->rq_count = 1;
  2110. enic->wq_count = 1;
  2111. enic->config.rq_desc_count = ENIC_MIN_RQ_DESCS;
  2112. enic->config.wq_desc_count = ENIC_MIN_WQ_DESCS;
  2113. enic->config.mtu = min_t(u16, 1500, enic->config.mtu);
  2114. }
  2115. }
  2116. static int enic_dev_init(struct enic *enic)
  2117. {
  2118. struct device *dev = enic_get_dev(enic);
  2119. struct net_device *netdev = enic->netdev;
  2120. unsigned int i;
  2121. int err;
  2122. /* Get interrupt coalesce timer info */
  2123. err = enic_dev_intr_coal_timer_info(enic);
  2124. if (err) {
  2125. dev_warn(dev, "Using default conversion factor for "
  2126. "interrupt coalesce timer\n");
  2127. vnic_dev_intr_coal_timer_info_default(enic->vdev);
  2128. }
  2129. /* Get vNIC configuration
  2130. */
  2131. err = enic_get_vnic_config(enic);
  2132. if (err) {
  2133. dev_err(dev, "Get vNIC configuration failed, aborting\n");
  2134. return err;
  2135. }
  2136. /* Get available resource counts
  2137. */
  2138. enic_get_res_counts(enic);
  2139. /* modify resource count if we are in kdump_kernel
  2140. */
  2141. enic_kdump_kernel_config(enic);
  2142. /* Set interrupt mode based on resource counts and system
  2143. * capabilities
  2144. */
  2145. err = enic_set_intr_mode(enic);
  2146. if (err) {
  2147. dev_err(dev, "Failed to set intr mode based on resource "
  2148. "counts and system capabilities, aborting\n");
  2149. return err;
  2150. }
  2151. /* Allocate and configure vNIC resources
  2152. */
  2153. err = enic_alloc_vnic_resources(enic);
  2154. if (err) {
  2155. dev_err(dev, "Failed to alloc vNIC resources, aborting\n");
  2156. goto err_out_free_vnic_resources;
  2157. }
  2158. enic_init_vnic_resources(enic);
  2159. err = enic_set_rss_nic_cfg(enic);
  2160. if (err) {
  2161. dev_err(dev, "Failed to config nic, aborting\n");
  2162. goto err_out_free_vnic_resources;
  2163. }
  2164. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  2165. default:
  2166. netif_napi_add(netdev, &enic->napi[0], enic_poll, 64);
  2167. break;
  2168. case VNIC_DEV_INTR_MODE_MSIX:
  2169. for (i = 0; i < enic->rq_count; i++) {
  2170. netif_napi_add(netdev, &enic->napi[i],
  2171. enic_poll_msix_rq, NAPI_POLL_WEIGHT);
  2172. }
  2173. for (i = 0; i < enic->wq_count; i++)
  2174. netif_napi_add(netdev, &enic->napi[enic_cq_wq(enic, i)],
  2175. enic_poll_msix_wq, NAPI_POLL_WEIGHT);
  2176. break;
  2177. }
  2178. return 0;
  2179. err_out_free_vnic_resources:
  2180. enic_free_affinity_hint(enic);
  2181. enic_clear_intr_mode(enic);
  2182. enic_free_vnic_resources(enic);
  2183. return err;
  2184. }
  2185. static void enic_iounmap(struct enic *enic)
  2186. {
  2187. unsigned int i;
  2188. for (i = 0; i < ARRAY_SIZE(enic->bar); i++)
  2189. if (enic->bar[i].vaddr)
  2190. iounmap(enic->bar[i].vaddr);
  2191. }
  2192. static int enic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  2193. {
  2194. struct device *dev = &pdev->dev;
  2195. struct net_device *netdev;
  2196. struct enic *enic;
  2197. int using_dac = 0;
  2198. unsigned int i;
  2199. int err;
  2200. #ifdef CONFIG_PCI_IOV
  2201. int pos = 0;
  2202. #endif
  2203. int num_pps = 1;
  2204. /* Allocate net device structure and initialize. Private
  2205. * instance data is initialized to zero.
  2206. */
  2207. netdev = alloc_etherdev_mqs(sizeof(struct enic),
  2208. ENIC_RQ_MAX, ENIC_WQ_MAX);
  2209. if (!netdev)
  2210. return -ENOMEM;
  2211. pci_set_drvdata(pdev, netdev);
  2212. SET_NETDEV_DEV(netdev, &pdev->dev);
  2213. enic = netdev_priv(netdev);
  2214. enic->netdev = netdev;
  2215. enic->pdev = pdev;
  2216. /* Setup PCI resources
  2217. */
  2218. err = pci_enable_device_mem(pdev);
  2219. if (err) {
  2220. dev_err(dev, "Cannot enable PCI device, aborting\n");
  2221. goto err_out_free_netdev;
  2222. }
  2223. err = pci_request_regions(pdev, DRV_NAME);
  2224. if (err) {
  2225. dev_err(dev, "Cannot request PCI regions, aborting\n");
  2226. goto err_out_disable_device;
  2227. }
  2228. pci_set_master(pdev);
  2229. /* Query PCI controller on system for DMA addressing
  2230. * limitation for the device. Try 64-bit first, and
  2231. * fail to 32-bit.
  2232. */
  2233. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  2234. if (err) {
  2235. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  2236. if (err) {
  2237. dev_err(dev, "No usable DMA configuration, aborting\n");
  2238. goto err_out_release_regions;
  2239. }
  2240. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  2241. if (err) {
  2242. dev_err(dev, "Unable to obtain %u-bit DMA "
  2243. "for consistent allocations, aborting\n", 32);
  2244. goto err_out_release_regions;
  2245. }
  2246. } else {
  2247. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  2248. if (err) {
  2249. dev_err(dev, "Unable to obtain %u-bit DMA "
  2250. "for consistent allocations, aborting\n", 64);
  2251. goto err_out_release_regions;
  2252. }
  2253. using_dac = 1;
  2254. }
  2255. /* Map vNIC resources from BAR0-5
  2256. */
  2257. for (i = 0; i < ARRAY_SIZE(enic->bar); i++) {
  2258. if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
  2259. continue;
  2260. enic->bar[i].len = pci_resource_len(pdev, i);
  2261. enic->bar[i].vaddr = pci_iomap(pdev, i, enic->bar[i].len);
  2262. if (!enic->bar[i].vaddr) {
  2263. dev_err(dev, "Cannot memory-map BAR %d, aborting\n", i);
  2264. err = -ENODEV;
  2265. goto err_out_iounmap;
  2266. }
  2267. enic->bar[i].bus_addr = pci_resource_start(pdev, i);
  2268. }
  2269. /* Register vNIC device
  2270. */
  2271. enic->vdev = vnic_dev_register(NULL, enic, pdev, enic->bar,
  2272. ARRAY_SIZE(enic->bar));
  2273. if (!enic->vdev) {
  2274. dev_err(dev, "vNIC registration failed, aborting\n");
  2275. err = -ENODEV;
  2276. goto err_out_iounmap;
  2277. }
  2278. err = vnic_devcmd_init(enic->vdev);
  2279. if (err)
  2280. goto err_out_vnic_unregister;
  2281. #ifdef CONFIG_PCI_IOV
  2282. /* Get number of subvnics */
  2283. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
  2284. if (pos) {
  2285. pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF,
  2286. &enic->num_vfs);
  2287. if (enic->num_vfs) {
  2288. err = pci_enable_sriov(pdev, enic->num_vfs);
  2289. if (err) {
  2290. dev_err(dev, "SRIOV enable failed, aborting."
  2291. " pci_enable_sriov() returned %d\n",
  2292. err);
  2293. goto err_out_vnic_unregister;
  2294. }
  2295. enic->priv_flags |= ENIC_SRIOV_ENABLED;
  2296. num_pps = enic->num_vfs;
  2297. }
  2298. }
  2299. #endif
  2300. /* Allocate structure for port profiles */
  2301. enic->pp = kcalloc(num_pps, sizeof(*enic->pp), GFP_KERNEL);
  2302. if (!enic->pp) {
  2303. err = -ENOMEM;
  2304. goto err_out_disable_sriov_pp;
  2305. }
  2306. /* Issue device open to get device in known state
  2307. */
  2308. err = enic_dev_open(enic);
  2309. if (err) {
  2310. dev_err(dev, "vNIC dev open failed, aborting\n");
  2311. goto err_out_disable_sriov;
  2312. }
  2313. /* Setup devcmd lock
  2314. */
  2315. spin_lock_init(&enic->devcmd_lock);
  2316. spin_lock_init(&enic->enic_api_lock);
  2317. /*
  2318. * Set ingress vlan rewrite mode before vnic initialization
  2319. */
  2320. err = enic_dev_set_ig_vlan_rewrite_mode(enic);
  2321. if (err) {
  2322. dev_err(dev,
  2323. "Failed to set ingress vlan rewrite mode, aborting.\n");
  2324. goto err_out_dev_close;
  2325. }
  2326. /* Issue device init to initialize the vnic-to-switch link.
  2327. * We'll start with carrier off and wait for link UP
  2328. * notification later to turn on carrier. We don't need
  2329. * to wait here for the vnic-to-switch link initialization
  2330. * to complete; link UP notification is the indication that
  2331. * the process is complete.
  2332. */
  2333. netif_carrier_off(netdev);
  2334. /* Do not call dev_init for a dynamic vnic.
  2335. * For a dynamic vnic, init_prov_info will be
  2336. * called later by an upper layer.
  2337. */
  2338. if (!enic_is_dynamic(enic)) {
  2339. err = vnic_dev_init(enic->vdev, 0);
  2340. if (err) {
  2341. dev_err(dev, "vNIC dev init failed, aborting\n");
  2342. goto err_out_dev_close;
  2343. }
  2344. }
  2345. err = enic_dev_init(enic);
  2346. if (err) {
  2347. dev_err(dev, "Device initialization failed, aborting\n");
  2348. goto err_out_dev_close;
  2349. }
  2350. netif_set_real_num_tx_queues(netdev, enic->wq_count);
  2351. netif_set_real_num_rx_queues(netdev, enic->rq_count);
  2352. /* Setup notification timer, HW reset task, and wq locks
  2353. */
  2354. init_timer(&enic->notify_timer);
  2355. enic->notify_timer.function = enic_notify_timer;
  2356. enic->notify_timer.data = (unsigned long)enic;
  2357. enic_set_rx_coal_setting(enic);
  2358. INIT_WORK(&enic->reset, enic_reset);
  2359. INIT_WORK(&enic->tx_hang_reset, enic_tx_hang_reset);
  2360. INIT_WORK(&enic->change_mtu_work, enic_change_mtu_work);
  2361. for (i = 0; i < enic->wq_count; i++)
  2362. spin_lock_init(&enic->wq_lock[i]);
  2363. /* Register net device
  2364. */
  2365. enic->port_mtu = enic->config.mtu;
  2366. (void)enic_change_mtu(netdev, enic->port_mtu);
  2367. err = enic_set_mac_addr(netdev, enic->mac_addr);
  2368. if (err) {
  2369. dev_err(dev, "Invalid MAC address, aborting\n");
  2370. goto err_out_dev_deinit;
  2371. }
  2372. enic->tx_coalesce_usecs = enic->config.intr_timer_usec;
  2373. /* rx coalesce time already got initialized. This gets used
  2374. * if adaptive coal is turned off
  2375. */
  2376. enic->rx_coalesce_usecs = enic->tx_coalesce_usecs;
  2377. if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic))
  2378. netdev->netdev_ops = &enic_netdev_dynamic_ops;
  2379. else
  2380. netdev->netdev_ops = &enic_netdev_ops;
  2381. netdev->watchdog_timeo = 2 * HZ;
  2382. enic_set_ethtool_ops(netdev);
  2383. netdev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
  2384. if (ENIC_SETTING(enic, LOOP)) {
  2385. netdev->features &= ~NETIF_F_HW_VLAN_CTAG_TX;
  2386. enic->loop_enable = 1;
  2387. enic->loop_tag = enic->config.loop_tag;
  2388. dev_info(dev, "loopback tag=0x%04x\n", enic->loop_tag);
  2389. }
  2390. if (ENIC_SETTING(enic, TXCSUM))
  2391. netdev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM;
  2392. if (ENIC_SETTING(enic, TSO))
  2393. netdev->hw_features |= NETIF_F_TSO |
  2394. NETIF_F_TSO6 | NETIF_F_TSO_ECN;
  2395. if (ENIC_SETTING(enic, RSS))
  2396. netdev->hw_features |= NETIF_F_RXHASH;
  2397. if (ENIC_SETTING(enic, RXCSUM))
  2398. netdev->hw_features |= NETIF_F_RXCSUM;
  2399. if (ENIC_SETTING(enic, VXLAN)) {
  2400. u64 patch_level;
  2401. netdev->hw_enc_features |= NETIF_F_RXCSUM |
  2402. NETIF_F_TSO |
  2403. NETIF_F_TSO_ECN |
  2404. NETIF_F_GSO_UDP_TUNNEL |
  2405. NETIF_F_HW_CSUM |
  2406. NETIF_F_GSO_UDP_TUNNEL_CSUM;
  2407. netdev->hw_features |= netdev->hw_enc_features;
  2408. /* get bit mask from hw about supported offload bit level
  2409. * BIT(0) = fw supports patch_level 0
  2410. * fcoe bit = encap
  2411. * fcoe_fc_crc_ok = outer csum ok
  2412. * BIT(1) = always set by fw
  2413. * BIT(2) = fw supports patch_level 2
  2414. * BIT(0) in rss_hash = encap
  2415. * BIT(1,2) in rss_hash = outer_ip_csum_ok/
  2416. * outer_tcp_csum_ok
  2417. * used in enic_rq_indicate_buf
  2418. */
  2419. err = vnic_dev_get_supported_feature_ver(enic->vdev,
  2420. VIC_FEATURE_VXLAN,
  2421. &patch_level);
  2422. if (err)
  2423. patch_level = 0;
  2424. /* mask bits that are supported by driver
  2425. */
  2426. patch_level &= BIT_ULL(0) | BIT_ULL(2);
  2427. patch_level = fls(patch_level);
  2428. patch_level = patch_level ? patch_level - 1 : 0;
  2429. enic->vxlan.patch_level = patch_level;
  2430. }
  2431. netdev->features |= netdev->hw_features;
  2432. netdev->vlan_features |= netdev->features;
  2433. #ifdef CONFIG_RFS_ACCEL
  2434. netdev->hw_features |= NETIF_F_NTUPLE;
  2435. #endif
  2436. if (using_dac)
  2437. netdev->features |= NETIF_F_HIGHDMA;
  2438. netdev->priv_flags |= IFF_UNICAST_FLT;
  2439. /* MTU range: 68 - 9000 */
  2440. netdev->min_mtu = ENIC_MIN_MTU;
  2441. netdev->max_mtu = ENIC_MAX_MTU;
  2442. err = register_netdev(netdev);
  2443. if (err) {
  2444. dev_err(dev, "Cannot register net device, aborting\n");
  2445. goto err_out_dev_deinit;
  2446. }
  2447. enic->rx_copybreak = RX_COPYBREAK_DEFAULT;
  2448. return 0;
  2449. err_out_dev_deinit:
  2450. enic_dev_deinit(enic);
  2451. err_out_dev_close:
  2452. vnic_dev_close(enic->vdev);
  2453. err_out_disable_sriov:
  2454. kfree(enic->pp);
  2455. err_out_disable_sriov_pp:
  2456. #ifdef CONFIG_PCI_IOV
  2457. if (enic_sriov_enabled(enic)) {
  2458. pci_disable_sriov(pdev);
  2459. enic->priv_flags &= ~ENIC_SRIOV_ENABLED;
  2460. }
  2461. #endif
  2462. err_out_vnic_unregister:
  2463. vnic_dev_unregister(enic->vdev);
  2464. err_out_iounmap:
  2465. enic_iounmap(enic);
  2466. err_out_release_regions:
  2467. pci_release_regions(pdev);
  2468. err_out_disable_device:
  2469. pci_disable_device(pdev);
  2470. err_out_free_netdev:
  2471. free_netdev(netdev);
  2472. return err;
  2473. }
  2474. static void enic_remove(struct pci_dev *pdev)
  2475. {
  2476. struct net_device *netdev = pci_get_drvdata(pdev);
  2477. if (netdev) {
  2478. struct enic *enic = netdev_priv(netdev);
  2479. cancel_work_sync(&enic->reset);
  2480. cancel_work_sync(&enic->change_mtu_work);
  2481. unregister_netdev(netdev);
  2482. enic_dev_deinit(enic);
  2483. vnic_dev_close(enic->vdev);
  2484. #ifdef CONFIG_PCI_IOV
  2485. if (enic_sriov_enabled(enic)) {
  2486. pci_disable_sriov(pdev);
  2487. enic->priv_flags &= ~ENIC_SRIOV_ENABLED;
  2488. }
  2489. #endif
  2490. kfree(enic->pp);
  2491. vnic_dev_unregister(enic->vdev);
  2492. enic_iounmap(enic);
  2493. pci_release_regions(pdev);
  2494. pci_disable_device(pdev);
  2495. free_netdev(netdev);
  2496. }
  2497. }
  2498. static struct pci_driver enic_driver = {
  2499. .name = DRV_NAME,
  2500. .id_table = enic_id_table,
  2501. .probe = enic_probe,
  2502. .remove = enic_remove,
  2503. };
  2504. static int __init enic_init_module(void)
  2505. {
  2506. pr_info("%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
  2507. return pci_register_driver(&enic_driver);
  2508. }
  2509. static void __exit enic_cleanup_module(void)
  2510. {
  2511. pci_unregister_driver(&enic_driver);
  2512. }
  2513. module_init(enic_init_module);
  2514. module_exit(enic_cleanup_module);