enic_main.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871
  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/workqueue.h>
  26. #include <linux/pci.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/if_ether.h>
  30. #include <linux/if_vlan.h>
  31. #include <linux/ethtool.h>
  32. #include <linux/in.h>
  33. #include <linux/ip.h>
  34. #include <linux/ipv6.h>
  35. #include <linux/tcp.h>
  36. #include <linux/rtnetlink.h>
  37. #include <net/ip6_checksum.h>
  38. #include "cq_enet_desc.h"
  39. #include "vnic_dev.h"
  40. #include "vnic_intr.h"
  41. #include "vnic_stats.h"
  42. #include "vnic_vic.h"
  43. #include "enic_res.h"
  44. #include "enic.h"
  45. #define ENIC_NOTIFY_TIMER_PERIOD (2 * HZ)
  46. #define WQ_ENET_MAX_DESC_LEN (1 << WQ_ENET_LEN_BITS)
  47. #define MAX_TSO (1 << 16)
  48. #define ENIC_DESC_MAX_SPLITS (MAX_TSO / WQ_ENET_MAX_DESC_LEN + 1)
  49. #define PCI_DEVICE_ID_CISCO_VIC_ENET 0x0043 /* ethernet vnic */
  50. #define PCI_DEVICE_ID_CISCO_VIC_ENET_DYN 0x0044 /* enet dynamic vnic */
  51. /* Supported devices */
  52. static DEFINE_PCI_DEVICE_TABLE(enic_id_table) = {
  53. { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET) },
  54. { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_DYN) },
  55. { 0, } /* end of table */
  56. };
  57. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  58. MODULE_AUTHOR("Scott Feldman <scofeldm@cisco.com>");
  59. MODULE_LICENSE("GPL");
  60. MODULE_VERSION(DRV_VERSION);
  61. MODULE_DEVICE_TABLE(pci, enic_id_table);
  62. struct enic_stat {
  63. char name[ETH_GSTRING_LEN];
  64. unsigned int offset;
  65. };
  66. #define ENIC_TX_STAT(stat) \
  67. { .name = #stat, .offset = offsetof(struct vnic_tx_stats, stat) / 8 }
  68. #define ENIC_RX_STAT(stat) \
  69. { .name = #stat, .offset = offsetof(struct vnic_rx_stats, stat) / 8 }
  70. static const struct enic_stat enic_tx_stats[] = {
  71. ENIC_TX_STAT(tx_frames_ok),
  72. ENIC_TX_STAT(tx_unicast_frames_ok),
  73. ENIC_TX_STAT(tx_multicast_frames_ok),
  74. ENIC_TX_STAT(tx_broadcast_frames_ok),
  75. ENIC_TX_STAT(tx_bytes_ok),
  76. ENIC_TX_STAT(tx_unicast_bytes_ok),
  77. ENIC_TX_STAT(tx_multicast_bytes_ok),
  78. ENIC_TX_STAT(tx_broadcast_bytes_ok),
  79. ENIC_TX_STAT(tx_drops),
  80. ENIC_TX_STAT(tx_errors),
  81. ENIC_TX_STAT(tx_tso),
  82. };
  83. static const struct enic_stat enic_rx_stats[] = {
  84. ENIC_RX_STAT(rx_frames_ok),
  85. ENIC_RX_STAT(rx_frames_total),
  86. ENIC_RX_STAT(rx_unicast_frames_ok),
  87. ENIC_RX_STAT(rx_multicast_frames_ok),
  88. ENIC_RX_STAT(rx_broadcast_frames_ok),
  89. ENIC_RX_STAT(rx_bytes_ok),
  90. ENIC_RX_STAT(rx_unicast_bytes_ok),
  91. ENIC_RX_STAT(rx_multicast_bytes_ok),
  92. ENIC_RX_STAT(rx_broadcast_bytes_ok),
  93. ENIC_RX_STAT(rx_drop),
  94. ENIC_RX_STAT(rx_no_bufs),
  95. ENIC_RX_STAT(rx_errors),
  96. ENIC_RX_STAT(rx_rss),
  97. ENIC_RX_STAT(rx_crc_errors),
  98. ENIC_RX_STAT(rx_frames_64),
  99. ENIC_RX_STAT(rx_frames_127),
  100. ENIC_RX_STAT(rx_frames_255),
  101. ENIC_RX_STAT(rx_frames_511),
  102. ENIC_RX_STAT(rx_frames_1023),
  103. ENIC_RX_STAT(rx_frames_1518),
  104. ENIC_RX_STAT(rx_frames_to_max),
  105. };
  106. static const unsigned int enic_n_tx_stats = ARRAY_SIZE(enic_tx_stats);
  107. static const unsigned int enic_n_rx_stats = ARRAY_SIZE(enic_rx_stats);
  108. static int enic_is_dynamic(struct enic *enic)
  109. {
  110. return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_DYN;
  111. }
  112. static inline unsigned int enic_cq_rq(struct enic *enic, unsigned int rq)
  113. {
  114. return rq;
  115. }
  116. static inline unsigned int enic_cq_wq(struct enic *enic, unsigned int wq)
  117. {
  118. return enic->rq_count + wq;
  119. }
  120. static inline unsigned int enic_legacy_io_intr(void)
  121. {
  122. return 0;
  123. }
  124. static inline unsigned int enic_legacy_err_intr(void)
  125. {
  126. return 1;
  127. }
  128. static inline unsigned int enic_legacy_notify_intr(void)
  129. {
  130. return 2;
  131. }
  132. static inline unsigned int enic_msix_rq_intr(struct enic *enic, unsigned int rq)
  133. {
  134. return rq;
  135. }
  136. static inline unsigned int enic_msix_wq_intr(struct enic *enic, unsigned int wq)
  137. {
  138. return enic->rq_count + wq;
  139. }
  140. static inline unsigned int enic_msix_err_intr(struct enic *enic)
  141. {
  142. return enic->rq_count + enic->wq_count;
  143. }
  144. static inline unsigned int enic_msix_notify_intr(struct enic *enic)
  145. {
  146. return enic->rq_count + enic->wq_count + 1;
  147. }
  148. static int enic_get_settings(struct net_device *netdev,
  149. struct ethtool_cmd *ecmd)
  150. {
  151. struct enic *enic = netdev_priv(netdev);
  152. ecmd->supported = (SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE);
  153. ecmd->advertising = (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
  154. ecmd->port = PORT_FIBRE;
  155. ecmd->transceiver = XCVR_EXTERNAL;
  156. if (netif_carrier_ok(netdev)) {
  157. ecmd->speed = vnic_dev_port_speed(enic->vdev);
  158. ecmd->duplex = DUPLEX_FULL;
  159. } else {
  160. ecmd->speed = -1;
  161. ecmd->duplex = -1;
  162. }
  163. ecmd->autoneg = AUTONEG_DISABLE;
  164. return 0;
  165. }
  166. static int enic_dev_fw_info(struct enic *enic,
  167. struct vnic_devcmd_fw_info **fw_info)
  168. {
  169. int err;
  170. spin_lock(&enic->devcmd_lock);
  171. err = vnic_dev_fw_info(enic->vdev, fw_info);
  172. spin_unlock(&enic->devcmd_lock);
  173. return err;
  174. }
  175. static void enic_get_drvinfo(struct net_device *netdev,
  176. struct ethtool_drvinfo *drvinfo)
  177. {
  178. struct enic *enic = netdev_priv(netdev);
  179. struct vnic_devcmd_fw_info *fw_info;
  180. enic_dev_fw_info(enic, &fw_info);
  181. strncpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
  182. strncpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version));
  183. strncpy(drvinfo->fw_version, fw_info->fw_version,
  184. sizeof(drvinfo->fw_version));
  185. strncpy(drvinfo->bus_info, pci_name(enic->pdev),
  186. sizeof(drvinfo->bus_info));
  187. }
  188. static void enic_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
  189. {
  190. unsigned int i;
  191. switch (stringset) {
  192. case ETH_SS_STATS:
  193. for (i = 0; i < enic_n_tx_stats; i++) {
  194. memcpy(data, enic_tx_stats[i].name, ETH_GSTRING_LEN);
  195. data += ETH_GSTRING_LEN;
  196. }
  197. for (i = 0; i < enic_n_rx_stats; i++) {
  198. memcpy(data, enic_rx_stats[i].name, ETH_GSTRING_LEN);
  199. data += ETH_GSTRING_LEN;
  200. }
  201. break;
  202. }
  203. }
  204. static int enic_get_sset_count(struct net_device *netdev, int sset)
  205. {
  206. switch (sset) {
  207. case ETH_SS_STATS:
  208. return enic_n_tx_stats + enic_n_rx_stats;
  209. default:
  210. return -EOPNOTSUPP;
  211. }
  212. }
  213. static int enic_dev_stats_dump(struct enic *enic, struct vnic_stats **vstats)
  214. {
  215. int err;
  216. spin_lock(&enic->devcmd_lock);
  217. err = vnic_dev_stats_dump(enic->vdev, vstats);
  218. spin_unlock(&enic->devcmd_lock);
  219. return err;
  220. }
  221. static void enic_get_ethtool_stats(struct net_device *netdev,
  222. struct ethtool_stats *stats, u64 *data)
  223. {
  224. struct enic *enic = netdev_priv(netdev);
  225. struct vnic_stats *vstats;
  226. unsigned int i;
  227. enic_dev_stats_dump(enic, &vstats);
  228. for (i = 0; i < enic_n_tx_stats; i++)
  229. *(data++) = ((u64 *)&vstats->tx)[enic_tx_stats[i].offset];
  230. for (i = 0; i < enic_n_rx_stats; i++)
  231. *(data++) = ((u64 *)&vstats->rx)[enic_rx_stats[i].offset];
  232. }
  233. static u32 enic_get_rx_csum(struct net_device *netdev)
  234. {
  235. struct enic *enic = netdev_priv(netdev);
  236. return enic->csum_rx_enabled;
  237. }
  238. static int enic_set_rx_csum(struct net_device *netdev, u32 data)
  239. {
  240. struct enic *enic = netdev_priv(netdev);
  241. if (data && !ENIC_SETTING(enic, RXCSUM))
  242. return -EINVAL;
  243. enic->csum_rx_enabled = !!data;
  244. return 0;
  245. }
  246. static int enic_set_tx_csum(struct net_device *netdev, u32 data)
  247. {
  248. struct enic *enic = netdev_priv(netdev);
  249. if (data && !ENIC_SETTING(enic, TXCSUM))
  250. return -EINVAL;
  251. if (data)
  252. netdev->features |= NETIF_F_HW_CSUM;
  253. else
  254. netdev->features &= ~NETIF_F_HW_CSUM;
  255. return 0;
  256. }
  257. static int enic_set_tso(struct net_device *netdev, u32 data)
  258. {
  259. struct enic *enic = netdev_priv(netdev);
  260. if (data && !ENIC_SETTING(enic, TSO))
  261. return -EINVAL;
  262. if (data)
  263. netdev->features |=
  264. NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN;
  265. else
  266. netdev->features &=
  267. ~(NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN);
  268. return 0;
  269. }
  270. static u32 enic_get_msglevel(struct net_device *netdev)
  271. {
  272. struct enic *enic = netdev_priv(netdev);
  273. return enic->msg_enable;
  274. }
  275. static void enic_set_msglevel(struct net_device *netdev, u32 value)
  276. {
  277. struct enic *enic = netdev_priv(netdev);
  278. enic->msg_enable = value;
  279. }
  280. static int enic_get_coalesce(struct net_device *netdev,
  281. struct ethtool_coalesce *ecmd)
  282. {
  283. struct enic *enic = netdev_priv(netdev);
  284. ecmd->tx_coalesce_usecs = enic->tx_coalesce_usecs;
  285. ecmd->rx_coalesce_usecs = enic->rx_coalesce_usecs;
  286. return 0;
  287. }
  288. static int enic_set_coalesce(struct net_device *netdev,
  289. struct ethtool_coalesce *ecmd)
  290. {
  291. struct enic *enic = netdev_priv(netdev);
  292. u32 tx_coalesce_usecs;
  293. u32 rx_coalesce_usecs;
  294. unsigned int i, intr;
  295. tx_coalesce_usecs = min_t(u32,
  296. INTR_COALESCE_HW_TO_USEC(VNIC_INTR_TIMER_MAX),
  297. ecmd->tx_coalesce_usecs);
  298. rx_coalesce_usecs = min_t(u32,
  299. INTR_COALESCE_HW_TO_USEC(VNIC_INTR_TIMER_MAX),
  300. ecmd->rx_coalesce_usecs);
  301. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  302. case VNIC_DEV_INTR_MODE_INTX:
  303. if (tx_coalesce_usecs != rx_coalesce_usecs)
  304. return -EINVAL;
  305. intr = enic_legacy_io_intr();
  306. vnic_intr_coalescing_timer_set(&enic->intr[intr],
  307. INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs));
  308. break;
  309. case VNIC_DEV_INTR_MODE_MSI:
  310. if (tx_coalesce_usecs != rx_coalesce_usecs)
  311. return -EINVAL;
  312. vnic_intr_coalescing_timer_set(&enic->intr[0],
  313. INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs));
  314. break;
  315. case VNIC_DEV_INTR_MODE_MSIX:
  316. for (i = 0; i < enic->wq_count; i++) {
  317. intr = enic_msix_wq_intr(enic, i);
  318. vnic_intr_coalescing_timer_set(&enic->intr[intr],
  319. INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs));
  320. }
  321. for (i = 0; i < enic->rq_count; i++) {
  322. intr = enic_msix_rq_intr(enic, i);
  323. vnic_intr_coalescing_timer_set(&enic->intr[intr],
  324. INTR_COALESCE_USEC_TO_HW(rx_coalesce_usecs));
  325. }
  326. break;
  327. default:
  328. break;
  329. }
  330. enic->tx_coalesce_usecs = tx_coalesce_usecs;
  331. enic->rx_coalesce_usecs = rx_coalesce_usecs;
  332. return 0;
  333. }
  334. static const struct ethtool_ops enic_ethtool_ops = {
  335. .get_settings = enic_get_settings,
  336. .get_drvinfo = enic_get_drvinfo,
  337. .get_msglevel = enic_get_msglevel,
  338. .set_msglevel = enic_set_msglevel,
  339. .get_link = ethtool_op_get_link,
  340. .get_strings = enic_get_strings,
  341. .get_sset_count = enic_get_sset_count,
  342. .get_ethtool_stats = enic_get_ethtool_stats,
  343. .get_rx_csum = enic_get_rx_csum,
  344. .set_rx_csum = enic_set_rx_csum,
  345. .get_tx_csum = ethtool_op_get_tx_csum,
  346. .set_tx_csum = enic_set_tx_csum,
  347. .get_sg = ethtool_op_get_sg,
  348. .set_sg = ethtool_op_set_sg,
  349. .get_tso = ethtool_op_get_tso,
  350. .set_tso = enic_set_tso,
  351. .get_coalesce = enic_get_coalesce,
  352. .set_coalesce = enic_set_coalesce,
  353. .get_flags = ethtool_op_get_flags,
  354. };
  355. static void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
  356. {
  357. struct enic *enic = vnic_dev_priv(wq->vdev);
  358. if (buf->sop)
  359. pci_unmap_single(enic->pdev, buf->dma_addr,
  360. buf->len, PCI_DMA_TODEVICE);
  361. else
  362. pci_unmap_page(enic->pdev, buf->dma_addr,
  363. buf->len, PCI_DMA_TODEVICE);
  364. if (buf->os_buf)
  365. dev_kfree_skb_any(buf->os_buf);
  366. }
  367. static void enic_wq_free_buf(struct vnic_wq *wq,
  368. struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque)
  369. {
  370. enic_free_wq_buf(wq, buf);
  371. }
  372. static int enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
  373. u8 type, u16 q_number, u16 completed_index, void *opaque)
  374. {
  375. struct enic *enic = vnic_dev_priv(vdev);
  376. spin_lock(&enic->wq_lock[q_number]);
  377. vnic_wq_service(&enic->wq[q_number], cq_desc,
  378. completed_index, enic_wq_free_buf,
  379. opaque);
  380. if (netif_queue_stopped(enic->netdev) &&
  381. vnic_wq_desc_avail(&enic->wq[q_number]) >=
  382. (MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS))
  383. netif_wake_queue(enic->netdev);
  384. spin_unlock(&enic->wq_lock[q_number]);
  385. return 0;
  386. }
  387. static void enic_log_q_error(struct enic *enic)
  388. {
  389. unsigned int i;
  390. u32 error_status;
  391. for (i = 0; i < enic->wq_count; i++) {
  392. error_status = vnic_wq_error_status(&enic->wq[i]);
  393. if (error_status)
  394. netdev_err(enic->netdev, "WQ[%d] error_status %d\n",
  395. i, error_status);
  396. }
  397. for (i = 0; i < enic->rq_count; i++) {
  398. error_status = vnic_rq_error_status(&enic->rq[i]);
  399. if (error_status)
  400. netdev_err(enic->netdev, "RQ[%d] error_status %d\n",
  401. i, error_status);
  402. }
  403. }
  404. static void enic_msglvl_check(struct enic *enic)
  405. {
  406. u32 msg_enable = vnic_dev_msg_lvl(enic->vdev);
  407. if (msg_enable != enic->msg_enable) {
  408. netdev_info(enic->netdev, "msg lvl changed from 0x%x to 0x%x\n",
  409. enic->msg_enable, msg_enable);
  410. enic->msg_enable = msg_enable;
  411. }
  412. }
  413. static void enic_mtu_check(struct enic *enic)
  414. {
  415. u32 mtu = vnic_dev_mtu(enic->vdev);
  416. struct net_device *netdev = enic->netdev;
  417. if (mtu && mtu != enic->port_mtu) {
  418. enic->port_mtu = mtu;
  419. if (mtu < netdev->mtu)
  420. netdev_warn(netdev,
  421. "interface MTU (%d) set higher "
  422. "than switch port MTU (%d)\n",
  423. netdev->mtu, mtu);
  424. }
  425. }
  426. static void enic_link_check(struct enic *enic)
  427. {
  428. int link_status = vnic_dev_link_status(enic->vdev);
  429. int carrier_ok = netif_carrier_ok(enic->netdev);
  430. if (link_status && !carrier_ok) {
  431. netdev_info(enic->netdev, "Link UP\n");
  432. netif_carrier_on(enic->netdev);
  433. } else if (!link_status && carrier_ok) {
  434. netdev_info(enic->netdev, "Link DOWN\n");
  435. netif_carrier_off(enic->netdev);
  436. }
  437. }
  438. static void enic_notify_check(struct enic *enic)
  439. {
  440. enic_msglvl_check(enic);
  441. enic_mtu_check(enic);
  442. enic_link_check(enic);
  443. }
  444. #define ENIC_TEST_INTR(pba, i) (pba & (1 << i))
  445. static irqreturn_t enic_isr_legacy(int irq, void *data)
  446. {
  447. struct net_device *netdev = data;
  448. struct enic *enic = netdev_priv(netdev);
  449. unsigned int io_intr = enic_legacy_io_intr();
  450. unsigned int err_intr = enic_legacy_err_intr();
  451. unsigned int notify_intr = enic_legacy_notify_intr();
  452. u32 pba;
  453. vnic_intr_mask(&enic->intr[io_intr]);
  454. pba = vnic_intr_legacy_pba(enic->legacy_pba);
  455. if (!pba) {
  456. vnic_intr_unmask(&enic->intr[io_intr]);
  457. return IRQ_NONE; /* not our interrupt */
  458. }
  459. if (ENIC_TEST_INTR(pba, notify_intr)) {
  460. vnic_intr_return_all_credits(&enic->intr[notify_intr]);
  461. enic_notify_check(enic);
  462. }
  463. if (ENIC_TEST_INTR(pba, err_intr)) {
  464. vnic_intr_return_all_credits(&enic->intr[err_intr]);
  465. enic_log_q_error(enic);
  466. /* schedule recovery from WQ/RQ error */
  467. schedule_work(&enic->reset);
  468. return IRQ_HANDLED;
  469. }
  470. if (ENIC_TEST_INTR(pba, io_intr)) {
  471. if (napi_schedule_prep(&enic->napi[0]))
  472. __napi_schedule(&enic->napi[0]);
  473. } else {
  474. vnic_intr_unmask(&enic->intr[io_intr]);
  475. }
  476. return IRQ_HANDLED;
  477. }
  478. static irqreturn_t enic_isr_msi(int irq, void *data)
  479. {
  480. struct enic *enic = data;
  481. /* With MSI, there is no sharing of interrupts, so this is
  482. * our interrupt and there is no need to ack it. The device
  483. * is not providing per-vector masking, so the OS will not
  484. * write to PCI config space to mask/unmask the interrupt.
  485. * We're using mask_on_assertion for MSI, so the device
  486. * automatically masks the interrupt when the interrupt is
  487. * generated. Later, when exiting polling, the interrupt
  488. * will be unmasked (see enic_poll).
  489. *
  490. * Also, the device uses the same PCIe Traffic Class (TC)
  491. * for Memory Write data and MSI, so there are no ordering
  492. * issues; the MSI will always arrive at the Root Complex
  493. * _after_ corresponding Memory Writes (i.e. descriptor
  494. * writes).
  495. */
  496. napi_schedule(&enic->napi[0]);
  497. return IRQ_HANDLED;
  498. }
  499. static irqreturn_t enic_isr_msix_rq(int irq, void *data)
  500. {
  501. struct napi_struct *napi = data;
  502. /* schedule NAPI polling for RQ cleanup */
  503. napi_schedule(napi);
  504. return IRQ_HANDLED;
  505. }
  506. static irqreturn_t enic_isr_msix_wq(int irq, void *data)
  507. {
  508. struct enic *enic = data;
  509. unsigned int cq = enic_cq_wq(enic, 0);
  510. unsigned int intr = enic_msix_wq_intr(enic, 0);
  511. unsigned int wq_work_to_do = -1; /* no limit */
  512. unsigned int wq_work_done;
  513. wq_work_done = vnic_cq_service(&enic->cq[cq],
  514. wq_work_to_do, enic_wq_service, NULL);
  515. vnic_intr_return_credits(&enic->intr[intr],
  516. wq_work_done,
  517. 1 /* unmask intr */,
  518. 1 /* reset intr timer */);
  519. return IRQ_HANDLED;
  520. }
  521. static irqreturn_t enic_isr_msix_err(int irq, void *data)
  522. {
  523. struct enic *enic = data;
  524. unsigned int intr = enic_msix_err_intr(enic);
  525. vnic_intr_return_all_credits(&enic->intr[intr]);
  526. enic_log_q_error(enic);
  527. /* schedule recovery from WQ/RQ error */
  528. schedule_work(&enic->reset);
  529. return IRQ_HANDLED;
  530. }
  531. static irqreturn_t enic_isr_msix_notify(int irq, void *data)
  532. {
  533. struct enic *enic = data;
  534. unsigned int intr = enic_msix_notify_intr(enic);
  535. vnic_intr_return_all_credits(&enic->intr[intr]);
  536. enic_notify_check(enic);
  537. return IRQ_HANDLED;
  538. }
  539. static inline void enic_queue_wq_skb_cont(struct enic *enic,
  540. struct vnic_wq *wq, struct sk_buff *skb,
  541. unsigned int len_left, int loopback)
  542. {
  543. skb_frag_t *frag;
  544. /* Queue additional data fragments */
  545. for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
  546. len_left -= frag->size;
  547. enic_queue_wq_desc_cont(wq, skb,
  548. pci_map_page(enic->pdev, frag->page,
  549. frag->page_offset, frag->size,
  550. PCI_DMA_TODEVICE),
  551. frag->size,
  552. (len_left == 0), /* EOP? */
  553. loopback);
  554. }
  555. }
  556. static inline void enic_queue_wq_skb_vlan(struct enic *enic,
  557. struct vnic_wq *wq, struct sk_buff *skb,
  558. int vlan_tag_insert, unsigned int vlan_tag, int loopback)
  559. {
  560. unsigned int head_len = skb_headlen(skb);
  561. unsigned int len_left = skb->len - head_len;
  562. int eop = (len_left == 0);
  563. /* Queue the main skb fragment. The fragments are no larger
  564. * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
  565. * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
  566. * per fragment is queued.
  567. */
  568. enic_queue_wq_desc(wq, skb,
  569. pci_map_single(enic->pdev, skb->data,
  570. head_len, PCI_DMA_TODEVICE),
  571. head_len,
  572. vlan_tag_insert, vlan_tag,
  573. eop, loopback);
  574. if (!eop)
  575. enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
  576. }
  577. static inline void enic_queue_wq_skb_csum_l4(struct enic *enic,
  578. struct vnic_wq *wq, struct sk_buff *skb,
  579. int vlan_tag_insert, unsigned int vlan_tag, int loopback)
  580. {
  581. unsigned int head_len = skb_headlen(skb);
  582. unsigned int len_left = skb->len - head_len;
  583. unsigned int hdr_len = skb_checksum_start_offset(skb);
  584. unsigned int csum_offset = hdr_len + skb->csum_offset;
  585. int eop = (len_left == 0);
  586. /* Queue the main skb fragment. The fragments are no larger
  587. * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
  588. * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
  589. * per fragment is queued.
  590. */
  591. enic_queue_wq_desc_csum_l4(wq, skb,
  592. pci_map_single(enic->pdev, skb->data,
  593. head_len, PCI_DMA_TODEVICE),
  594. head_len,
  595. csum_offset,
  596. hdr_len,
  597. vlan_tag_insert, vlan_tag,
  598. eop, loopback);
  599. if (!eop)
  600. enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
  601. }
  602. static inline void enic_queue_wq_skb_tso(struct enic *enic,
  603. struct vnic_wq *wq, struct sk_buff *skb, unsigned int mss,
  604. int vlan_tag_insert, unsigned int vlan_tag, int loopback)
  605. {
  606. unsigned int frag_len_left = skb_headlen(skb);
  607. unsigned int len_left = skb->len - frag_len_left;
  608. unsigned int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  609. int eop = (len_left == 0);
  610. unsigned int len;
  611. dma_addr_t dma_addr;
  612. unsigned int offset = 0;
  613. skb_frag_t *frag;
  614. /* Preload TCP csum field with IP pseudo hdr calculated
  615. * with IP length set to zero. HW will later add in length
  616. * to each TCP segment resulting from the TSO.
  617. */
  618. if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
  619. ip_hdr(skb)->check = 0;
  620. tcp_hdr(skb)->check = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  621. ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  622. } else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
  623. tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  624. &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  625. }
  626. /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
  627. * for the main skb fragment
  628. */
  629. while (frag_len_left) {
  630. len = min(frag_len_left, (unsigned int)WQ_ENET_MAX_DESC_LEN);
  631. dma_addr = pci_map_single(enic->pdev, skb->data + offset,
  632. len, PCI_DMA_TODEVICE);
  633. enic_queue_wq_desc_tso(wq, skb,
  634. dma_addr,
  635. len,
  636. mss, hdr_len,
  637. vlan_tag_insert, vlan_tag,
  638. eop && (len == frag_len_left), loopback);
  639. frag_len_left -= len;
  640. offset += len;
  641. }
  642. if (eop)
  643. return;
  644. /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
  645. * for additional data fragments
  646. */
  647. for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
  648. len_left -= frag->size;
  649. frag_len_left = frag->size;
  650. offset = frag->page_offset;
  651. while (frag_len_left) {
  652. len = min(frag_len_left,
  653. (unsigned int)WQ_ENET_MAX_DESC_LEN);
  654. dma_addr = pci_map_page(enic->pdev, frag->page,
  655. offset, len,
  656. PCI_DMA_TODEVICE);
  657. enic_queue_wq_desc_cont(wq, skb,
  658. dma_addr,
  659. len,
  660. (len_left == 0) &&
  661. (len == frag_len_left), /* EOP? */
  662. loopback);
  663. frag_len_left -= len;
  664. offset += len;
  665. }
  666. }
  667. }
  668. static inline void enic_queue_wq_skb(struct enic *enic,
  669. struct vnic_wq *wq, struct sk_buff *skb)
  670. {
  671. unsigned int mss = skb_shinfo(skb)->gso_size;
  672. unsigned int vlan_tag = 0;
  673. int vlan_tag_insert = 0;
  674. int loopback = 0;
  675. if (vlan_tx_tag_present(skb)) {
  676. /* VLAN tag from trunking driver */
  677. vlan_tag_insert = 1;
  678. vlan_tag = vlan_tx_tag_get(skb);
  679. } else if (enic->loop_enable) {
  680. vlan_tag = enic->loop_tag;
  681. loopback = 1;
  682. }
  683. if (mss)
  684. enic_queue_wq_skb_tso(enic, wq, skb, mss,
  685. vlan_tag_insert, vlan_tag, loopback);
  686. else if (skb->ip_summed == CHECKSUM_PARTIAL)
  687. enic_queue_wq_skb_csum_l4(enic, wq, skb,
  688. vlan_tag_insert, vlan_tag, loopback);
  689. else
  690. enic_queue_wq_skb_vlan(enic, wq, skb,
  691. vlan_tag_insert, vlan_tag, loopback);
  692. }
  693. /* netif_tx_lock held, process context with BHs disabled, or BH */
  694. static netdev_tx_t enic_hard_start_xmit(struct sk_buff *skb,
  695. struct net_device *netdev)
  696. {
  697. struct enic *enic = netdev_priv(netdev);
  698. struct vnic_wq *wq = &enic->wq[0];
  699. unsigned long flags;
  700. if (skb->len <= 0) {
  701. dev_kfree_skb(skb);
  702. return NETDEV_TX_OK;
  703. }
  704. /* Non-TSO sends must fit within ENIC_NON_TSO_MAX_DESC descs,
  705. * which is very likely. In the off chance it's going to take
  706. * more than * ENIC_NON_TSO_MAX_DESC, linearize the skb.
  707. */
  708. if (skb_shinfo(skb)->gso_size == 0 &&
  709. skb_shinfo(skb)->nr_frags + 1 > ENIC_NON_TSO_MAX_DESC &&
  710. skb_linearize(skb)) {
  711. dev_kfree_skb(skb);
  712. return NETDEV_TX_OK;
  713. }
  714. spin_lock_irqsave(&enic->wq_lock[0], flags);
  715. if (vnic_wq_desc_avail(wq) <
  716. skb_shinfo(skb)->nr_frags + ENIC_DESC_MAX_SPLITS) {
  717. netif_stop_queue(netdev);
  718. /* This is a hard error, log it */
  719. netdev_err(netdev, "BUG! Tx ring full when queue awake!\n");
  720. spin_unlock_irqrestore(&enic->wq_lock[0], flags);
  721. return NETDEV_TX_BUSY;
  722. }
  723. enic_queue_wq_skb(enic, wq, skb);
  724. if (vnic_wq_desc_avail(wq) < MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS)
  725. netif_stop_queue(netdev);
  726. spin_unlock_irqrestore(&enic->wq_lock[0], flags);
  727. return NETDEV_TX_OK;
  728. }
  729. /* dev_base_lock rwlock held, nominally process context */
  730. static struct net_device_stats *enic_get_stats(struct net_device *netdev)
  731. {
  732. struct enic *enic = netdev_priv(netdev);
  733. struct net_device_stats *net_stats = &netdev->stats;
  734. struct vnic_stats *stats;
  735. enic_dev_stats_dump(enic, &stats);
  736. net_stats->tx_packets = stats->tx.tx_frames_ok;
  737. net_stats->tx_bytes = stats->tx.tx_bytes_ok;
  738. net_stats->tx_errors = stats->tx.tx_errors;
  739. net_stats->tx_dropped = stats->tx.tx_drops;
  740. net_stats->rx_packets = stats->rx.rx_frames_ok;
  741. net_stats->rx_bytes = stats->rx.rx_bytes_ok;
  742. net_stats->rx_errors = stats->rx.rx_errors;
  743. net_stats->multicast = stats->rx.rx_multicast_frames_ok;
  744. net_stats->rx_over_errors = enic->rq_truncated_pkts;
  745. net_stats->rx_crc_errors = enic->rq_bad_fcs;
  746. net_stats->rx_dropped = stats->rx.rx_no_bufs + stats->rx.rx_drop;
  747. return net_stats;
  748. }
  749. static void enic_reset_multicast_list(struct enic *enic)
  750. {
  751. enic->mc_count = 0;
  752. enic->flags = 0;
  753. }
  754. static int enic_set_mac_addr(struct net_device *netdev, char *addr)
  755. {
  756. struct enic *enic = netdev_priv(netdev);
  757. if (enic_is_dynamic(enic)) {
  758. if (!is_valid_ether_addr(addr) && !is_zero_ether_addr(addr))
  759. return -EADDRNOTAVAIL;
  760. } else {
  761. if (!is_valid_ether_addr(addr))
  762. return -EADDRNOTAVAIL;
  763. }
  764. memcpy(netdev->dev_addr, addr, netdev->addr_len);
  765. return 0;
  766. }
  767. static int enic_dev_add_station_addr(struct enic *enic)
  768. {
  769. int err = 0;
  770. if (is_valid_ether_addr(enic->netdev->dev_addr)) {
  771. spin_lock(&enic->devcmd_lock);
  772. err = vnic_dev_add_addr(enic->vdev, enic->netdev->dev_addr);
  773. spin_unlock(&enic->devcmd_lock);
  774. }
  775. return err;
  776. }
  777. static int enic_dev_del_station_addr(struct enic *enic)
  778. {
  779. int err = 0;
  780. if (is_valid_ether_addr(enic->netdev->dev_addr)) {
  781. spin_lock(&enic->devcmd_lock);
  782. err = vnic_dev_del_addr(enic->vdev, enic->netdev->dev_addr);
  783. spin_unlock(&enic->devcmd_lock);
  784. }
  785. return err;
  786. }
  787. static int enic_set_mac_address_dynamic(struct net_device *netdev, void *p)
  788. {
  789. struct enic *enic = netdev_priv(netdev);
  790. struct sockaddr *saddr = p;
  791. char *addr = saddr->sa_data;
  792. int err;
  793. if (netif_running(enic->netdev)) {
  794. err = enic_dev_del_station_addr(enic);
  795. if (err)
  796. return err;
  797. }
  798. err = enic_set_mac_addr(netdev, addr);
  799. if (err)
  800. return err;
  801. if (netif_running(enic->netdev)) {
  802. err = enic_dev_add_station_addr(enic);
  803. if (err)
  804. return err;
  805. }
  806. return err;
  807. }
  808. static int enic_set_mac_address(struct net_device *netdev, void *p)
  809. {
  810. struct sockaddr *saddr = p;
  811. char *addr = saddr->sa_data;
  812. struct enic *enic = netdev_priv(netdev);
  813. int err;
  814. err = enic_dev_del_station_addr(enic);
  815. if (err)
  816. return err;
  817. err = enic_set_mac_addr(netdev, addr);
  818. if (err)
  819. return err;
  820. return enic_dev_add_station_addr(enic);
  821. }
  822. static int enic_dev_packet_filter(struct enic *enic, int directed,
  823. int multicast, int broadcast, int promisc, int allmulti)
  824. {
  825. int err;
  826. spin_lock(&enic->devcmd_lock);
  827. err = vnic_dev_packet_filter(enic->vdev, directed,
  828. multicast, broadcast, promisc, allmulti);
  829. spin_unlock(&enic->devcmd_lock);
  830. return err;
  831. }
  832. static int enic_dev_add_addr(struct enic *enic, u8 *addr)
  833. {
  834. int err;
  835. spin_lock(&enic->devcmd_lock);
  836. err = vnic_dev_add_addr(enic->vdev, addr);
  837. spin_unlock(&enic->devcmd_lock);
  838. return err;
  839. }
  840. static int enic_dev_del_addr(struct enic *enic, u8 *addr)
  841. {
  842. int err;
  843. spin_lock(&enic->devcmd_lock);
  844. err = vnic_dev_del_addr(enic->vdev, addr);
  845. spin_unlock(&enic->devcmd_lock);
  846. return err;
  847. }
  848. static void enic_add_multicast_addr_list(struct enic *enic)
  849. {
  850. struct net_device *netdev = enic->netdev;
  851. struct netdev_hw_addr *ha;
  852. unsigned int mc_count = netdev_mc_count(netdev);
  853. u8 mc_addr[ENIC_MULTICAST_PERFECT_FILTERS][ETH_ALEN];
  854. unsigned int i, j;
  855. if (mc_count > ENIC_MULTICAST_PERFECT_FILTERS) {
  856. netdev_warn(netdev, "Registering only %d out of %d "
  857. "multicast addresses\n",
  858. ENIC_MULTICAST_PERFECT_FILTERS, mc_count);
  859. mc_count = ENIC_MULTICAST_PERFECT_FILTERS;
  860. }
  861. /* Is there an easier way? Trying to minimize to
  862. * calls to add/del multicast addrs. We keep the
  863. * addrs from the last call in enic->mc_addr and
  864. * look for changes to add/del.
  865. */
  866. i = 0;
  867. netdev_for_each_mc_addr(ha, netdev) {
  868. if (i == mc_count)
  869. break;
  870. memcpy(mc_addr[i++], ha->addr, ETH_ALEN);
  871. }
  872. for (i = 0; i < enic->mc_count; i++) {
  873. for (j = 0; j < mc_count; j++)
  874. if (compare_ether_addr(enic->mc_addr[i],
  875. mc_addr[j]) == 0)
  876. break;
  877. if (j == mc_count)
  878. enic_dev_del_addr(enic, enic->mc_addr[i]);
  879. }
  880. for (i = 0; i < mc_count; i++) {
  881. for (j = 0; j < enic->mc_count; j++)
  882. if (compare_ether_addr(mc_addr[i],
  883. enic->mc_addr[j]) == 0)
  884. break;
  885. if (j == enic->mc_count)
  886. enic_dev_add_addr(enic, mc_addr[i]);
  887. }
  888. /* Save the list to compare against next time
  889. */
  890. for (i = 0; i < mc_count; i++)
  891. memcpy(enic->mc_addr[i], mc_addr[i], ETH_ALEN);
  892. enic->mc_count = mc_count;
  893. }
  894. static void enic_add_unicast_addr_list(struct enic *enic)
  895. {
  896. struct net_device *netdev = enic->netdev;
  897. struct netdev_hw_addr *ha;
  898. unsigned int uc_count = netdev_uc_count(netdev);
  899. u8 uc_addr[ENIC_UNICAST_PERFECT_FILTERS][ETH_ALEN];
  900. unsigned int i, j;
  901. if (uc_count > ENIC_UNICAST_PERFECT_FILTERS) {
  902. netdev_warn(netdev, "Registering only %d out of %d "
  903. "unicast addresses\n",
  904. ENIC_UNICAST_PERFECT_FILTERS, uc_count);
  905. uc_count = ENIC_UNICAST_PERFECT_FILTERS;
  906. }
  907. /* Is there an easier way? Trying to minimize to
  908. * calls to add/del unicast addrs. We keep the
  909. * addrs from the last call in enic->uc_addr and
  910. * look for changes to add/del.
  911. */
  912. i = 0;
  913. netdev_for_each_uc_addr(ha, netdev) {
  914. if (i == uc_count)
  915. break;
  916. memcpy(uc_addr[i++], ha->addr, ETH_ALEN);
  917. }
  918. for (i = 0; i < enic->uc_count; i++) {
  919. for (j = 0; j < uc_count; j++)
  920. if (compare_ether_addr(enic->uc_addr[i],
  921. uc_addr[j]) == 0)
  922. break;
  923. if (j == uc_count)
  924. enic_dev_del_addr(enic, enic->uc_addr[i]);
  925. }
  926. for (i = 0; i < uc_count; i++) {
  927. for (j = 0; j < enic->uc_count; j++)
  928. if (compare_ether_addr(uc_addr[i],
  929. enic->uc_addr[j]) == 0)
  930. break;
  931. if (j == enic->uc_count)
  932. enic_dev_add_addr(enic, uc_addr[i]);
  933. }
  934. /* Save the list to compare against next time
  935. */
  936. for (i = 0; i < uc_count; i++)
  937. memcpy(enic->uc_addr[i], uc_addr[i], ETH_ALEN);
  938. enic->uc_count = uc_count;
  939. }
  940. /* netif_tx_lock held, BHs disabled */
  941. static void enic_set_rx_mode(struct net_device *netdev)
  942. {
  943. struct enic *enic = netdev_priv(netdev);
  944. int directed = 1;
  945. int multicast = (netdev->flags & IFF_MULTICAST) ? 1 : 0;
  946. int broadcast = (netdev->flags & IFF_BROADCAST) ? 1 : 0;
  947. int promisc = (netdev->flags & IFF_PROMISC) ||
  948. netdev_uc_count(netdev) > ENIC_UNICAST_PERFECT_FILTERS;
  949. int allmulti = (netdev->flags & IFF_ALLMULTI) ||
  950. netdev_mc_count(netdev) > ENIC_MULTICAST_PERFECT_FILTERS;
  951. unsigned int flags = netdev->flags |
  952. (allmulti ? IFF_ALLMULTI : 0) |
  953. (promisc ? IFF_PROMISC : 0);
  954. if (enic->flags != flags) {
  955. enic->flags = flags;
  956. enic_dev_packet_filter(enic, directed,
  957. multicast, broadcast, promisc, allmulti);
  958. }
  959. if (!promisc) {
  960. enic_add_unicast_addr_list(enic);
  961. if (!allmulti)
  962. enic_add_multicast_addr_list(enic);
  963. }
  964. }
  965. /* rtnl lock is held */
  966. static void enic_vlan_rx_register(struct net_device *netdev,
  967. struct vlan_group *vlan_group)
  968. {
  969. struct enic *enic = netdev_priv(netdev);
  970. enic->vlan_group = vlan_group;
  971. }
  972. /* rtnl lock is held */
  973. static void enic_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
  974. {
  975. struct enic *enic = netdev_priv(netdev);
  976. spin_lock(&enic->devcmd_lock);
  977. enic_add_vlan(enic, vid);
  978. spin_unlock(&enic->devcmd_lock);
  979. }
  980. /* rtnl lock is held */
  981. static void enic_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
  982. {
  983. struct enic *enic = netdev_priv(netdev);
  984. spin_lock(&enic->devcmd_lock);
  985. enic_del_vlan(enic, vid);
  986. spin_unlock(&enic->devcmd_lock);
  987. }
  988. /* netif_tx_lock held, BHs disabled */
  989. static void enic_tx_timeout(struct net_device *netdev)
  990. {
  991. struct enic *enic = netdev_priv(netdev);
  992. schedule_work(&enic->reset);
  993. }
  994. static int enic_vnic_dev_deinit(struct enic *enic)
  995. {
  996. int err;
  997. spin_lock(&enic->devcmd_lock);
  998. err = vnic_dev_deinit(enic->vdev);
  999. spin_unlock(&enic->devcmd_lock);
  1000. return err;
  1001. }
  1002. static int enic_dev_init_prov(struct enic *enic, struct vic_provinfo *vp)
  1003. {
  1004. int err;
  1005. spin_lock(&enic->devcmd_lock);
  1006. err = vnic_dev_init_prov(enic->vdev,
  1007. (u8 *)vp, vic_provinfo_size(vp));
  1008. spin_unlock(&enic->devcmd_lock);
  1009. return err;
  1010. }
  1011. static int enic_dev_init_done(struct enic *enic, int *done, int *error)
  1012. {
  1013. int err;
  1014. spin_lock(&enic->devcmd_lock);
  1015. err = vnic_dev_init_done(enic->vdev, done, error);
  1016. spin_unlock(&enic->devcmd_lock);
  1017. return err;
  1018. }
  1019. static int enic_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
  1020. {
  1021. struct enic *enic = netdev_priv(netdev);
  1022. if (vf != PORT_SELF_VF)
  1023. return -EOPNOTSUPP;
  1024. /* Ignore the vf argument for now. We can assume the request
  1025. * is coming on a vf.
  1026. */
  1027. if (is_valid_ether_addr(mac)) {
  1028. memcpy(enic->pp.vf_mac, mac, ETH_ALEN);
  1029. return 0;
  1030. } else
  1031. return -EINVAL;
  1032. }
  1033. static int enic_set_port_profile(struct enic *enic, u8 *mac)
  1034. {
  1035. struct vic_provinfo *vp;
  1036. u8 oui[3] = VIC_PROVINFO_CISCO_OUI;
  1037. u16 os_type = VIC_GENERIC_PROV_OS_TYPE_LINUX;
  1038. char uuid_str[38];
  1039. char client_mac_str[18];
  1040. u8 *client_mac;
  1041. int err;
  1042. err = enic_vnic_dev_deinit(enic);
  1043. if (err)
  1044. return err;
  1045. switch (enic->pp.request) {
  1046. case PORT_REQUEST_ASSOCIATE:
  1047. if (!(enic->pp.set & ENIC_SET_NAME) || !strlen(enic->pp.name))
  1048. return -EINVAL;
  1049. if (!is_valid_ether_addr(mac))
  1050. return -EADDRNOTAVAIL;
  1051. vp = vic_provinfo_alloc(GFP_KERNEL, oui,
  1052. VIC_PROVINFO_GENERIC_TYPE);
  1053. if (!vp)
  1054. return -ENOMEM;
  1055. vic_provinfo_add_tlv(vp,
  1056. VIC_GENERIC_PROV_TLV_PORT_PROFILE_NAME_STR,
  1057. strlen(enic->pp.name) + 1, enic->pp.name);
  1058. if (!is_zero_ether_addr(enic->pp.mac_addr))
  1059. client_mac = enic->pp.mac_addr;
  1060. else
  1061. client_mac = mac;
  1062. vic_provinfo_add_tlv(vp,
  1063. VIC_GENERIC_PROV_TLV_CLIENT_MAC_ADDR,
  1064. ETH_ALEN, client_mac);
  1065. sprintf(client_mac_str, "%pM", client_mac);
  1066. vic_provinfo_add_tlv(vp,
  1067. VIC_GENERIC_PROV_TLV_CLUSTER_PORT_UUID_STR,
  1068. sizeof(client_mac_str), client_mac_str);
  1069. if (enic->pp.set & ENIC_SET_INSTANCE) {
  1070. sprintf(uuid_str, "%pUB", enic->pp.instance_uuid);
  1071. vic_provinfo_add_tlv(vp,
  1072. VIC_GENERIC_PROV_TLV_CLIENT_UUID_STR,
  1073. sizeof(uuid_str), uuid_str);
  1074. }
  1075. if (enic->pp.set & ENIC_SET_HOST) {
  1076. sprintf(uuid_str, "%pUB", enic->pp.host_uuid);
  1077. vic_provinfo_add_tlv(vp,
  1078. VIC_GENERIC_PROV_TLV_HOST_UUID_STR,
  1079. sizeof(uuid_str), uuid_str);
  1080. }
  1081. os_type = htons(os_type);
  1082. vic_provinfo_add_tlv(vp,
  1083. VIC_GENERIC_PROV_TLV_OS_TYPE,
  1084. sizeof(os_type), &os_type);
  1085. err = enic_dev_init_prov(enic, vp);
  1086. vic_provinfo_free(vp);
  1087. if (err)
  1088. return err;
  1089. enic->pp.set |= ENIC_SET_APPLIED;
  1090. break;
  1091. case PORT_REQUEST_DISASSOCIATE:
  1092. enic->pp.set &= ~ENIC_SET_APPLIED;
  1093. break;
  1094. default:
  1095. return -EINVAL;
  1096. }
  1097. return 0;
  1098. }
  1099. static int enic_set_vf_port(struct net_device *netdev, int vf,
  1100. struct nlattr *port[])
  1101. {
  1102. struct enic *enic = netdev_priv(netdev);
  1103. struct enic_port_profile new_pp;
  1104. int err = 0;
  1105. memset(&new_pp, 0, sizeof(new_pp));
  1106. if (port[IFLA_PORT_REQUEST]) {
  1107. new_pp.set |= ENIC_SET_REQUEST;
  1108. new_pp.request = nla_get_u8(port[IFLA_PORT_REQUEST]);
  1109. }
  1110. if (port[IFLA_PORT_PROFILE]) {
  1111. new_pp.set |= ENIC_SET_NAME;
  1112. memcpy(new_pp.name, nla_data(port[IFLA_PORT_PROFILE]),
  1113. PORT_PROFILE_MAX);
  1114. }
  1115. if (port[IFLA_PORT_INSTANCE_UUID]) {
  1116. new_pp.set |= ENIC_SET_INSTANCE;
  1117. memcpy(new_pp.instance_uuid,
  1118. nla_data(port[IFLA_PORT_INSTANCE_UUID]), PORT_UUID_MAX);
  1119. }
  1120. if (port[IFLA_PORT_HOST_UUID]) {
  1121. new_pp.set |= ENIC_SET_HOST;
  1122. memcpy(new_pp.host_uuid,
  1123. nla_data(port[IFLA_PORT_HOST_UUID]), PORT_UUID_MAX);
  1124. }
  1125. /* don't support VFs, yet */
  1126. if (vf != PORT_SELF_VF)
  1127. return -EOPNOTSUPP;
  1128. if (!(new_pp.set & ENIC_SET_REQUEST))
  1129. return -EOPNOTSUPP;
  1130. if (new_pp.request == PORT_REQUEST_ASSOCIATE) {
  1131. /* Special case handling */
  1132. if (!is_zero_ether_addr(enic->pp.vf_mac))
  1133. memcpy(new_pp.mac_addr, enic->pp.vf_mac, ETH_ALEN);
  1134. if (is_zero_ether_addr(netdev->dev_addr))
  1135. random_ether_addr(netdev->dev_addr);
  1136. } else if (new_pp.request == PORT_REQUEST_DISASSOCIATE) {
  1137. if (!is_zero_ether_addr(enic->pp.mac_addr))
  1138. enic_dev_del_addr(enic, enic->pp.mac_addr);
  1139. }
  1140. memcpy(&enic->pp, &new_pp, sizeof(struct enic_port_profile));
  1141. err = enic_set_port_profile(enic, netdev->dev_addr);
  1142. if (err)
  1143. goto set_port_profile_cleanup;
  1144. if (!is_zero_ether_addr(enic->pp.mac_addr))
  1145. enic_dev_add_addr(enic, enic->pp.mac_addr);
  1146. set_port_profile_cleanup:
  1147. memset(enic->pp.vf_mac, 0, ETH_ALEN);
  1148. if (err || enic->pp.request == PORT_REQUEST_DISASSOCIATE) {
  1149. memset(netdev->dev_addr, 0, ETH_ALEN);
  1150. memset(enic->pp.mac_addr, 0, ETH_ALEN);
  1151. }
  1152. return err;
  1153. }
  1154. static int enic_get_vf_port(struct net_device *netdev, int vf,
  1155. struct sk_buff *skb)
  1156. {
  1157. struct enic *enic = netdev_priv(netdev);
  1158. int err, error, done;
  1159. u16 response = PORT_PROFILE_RESPONSE_SUCCESS;
  1160. if (!(enic->pp.set & ENIC_SET_APPLIED))
  1161. return -ENODATA;
  1162. err = enic_dev_init_done(enic, &done, &error);
  1163. if (err)
  1164. error = err;
  1165. switch (error) {
  1166. case ERR_SUCCESS:
  1167. if (!done)
  1168. response = PORT_PROFILE_RESPONSE_INPROGRESS;
  1169. break;
  1170. case ERR_EINVAL:
  1171. response = PORT_PROFILE_RESPONSE_INVALID;
  1172. break;
  1173. case ERR_EBADSTATE:
  1174. response = PORT_PROFILE_RESPONSE_BADSTATE;
  1175. break;
  1176. case ERR_ENOMEM:
  1177. response = PORT_PROFILE_RESPONSE_INSUFFICIENT_RESOURCES;
  1178. break;
  1179. default:
  1180. response = PORT_PROFILE_RESPONSE_ERROR;
  1181. break;
  1182. }
  1183. NLA_PUT_U16(skb, IFLA_PORT_REQUEST, enic->pp.request);
  1184. NLA_PUT_U16(skb, IFLA_PORT_RESPONSE, response);
  1185. if (enic->pp.set & ENIC_SET_NAME)
  1186. NLA_PUT(skb, IFLA_PORT_PROFILE, PORT_PROFILE_MAX,
  1187. enic->pp.name);
  1188. if (enic->pp.set & ENIC_SET_INSTANCE)
  1189. NLA_PUT(skb, IFLA_PORT_INSTANCE_UUID, PORT_UUID_MAX,
  1190. enic->pp.instance_uuid);
  1191. if (enic->pp.set & ENIC_SET_HOST)
  1192. NLA_PUT(skb, IFLA_PORT_HOST_UUID, PORT_UUID_MAX,
  1193. enic->pp.host_uuid);
  1194. return 0;
  1195. nla_put_failure:
  1196. return -EMSGSIZE;
  1197. }
  1198. static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
  1199. {
  1200. struct enic *enic = vnic_dev_priv(rq->vdev);
  1201. if (!buf->os_buf)
  1202. return;
  1203. pci_unmap_single(enic->pdev, buf->dma_addr,
  1204. buf->len, PCI_DMA_FROMDEVICE);
  1205. dev_kfree_skb_any(buf->os_buf);
  1206. }
  1207. static int enic_rq_alloc_buf(struct vnic_rq *rq)
  1208. {
  1209. struct enic *enic = vnic_dev_priv(rq->vdev);
  1210. struct net_device *netdev = enic->netdev;
  1211. struct sk_buff *skb;
  1212. unsigned int len = netdev->mtu + VLAN_ETH_HLEN;
  1213. unsigned int os_buf_index = 0;
  1214. dma_addr_t dma_addr;
  1215. skb = netdev_alloc_skb_ip_align(netdev, len);
  1216. if (!skb)
  1217. return -ENOMEM;
  1218. dma_addr = pci_map_single(enic->pdev, skb->data,
  1219. len, PCI_DMA_FROMDEVICE);
  1220. enic_queue_rq_desc(rq, skb, os_buf_index,
  1221. dma_addr, len);
  1222. return 0;
  1223. }
  1224. static int enic_rq_alloc_buf_a1(struct vnic_rq *rq)
  1225. {
  1226. struct rq_enet_desc *desc = vnic_rq_next_desc(rq);
  1227. if (vnic_rq_posting_soon(rq)) {
  1228. /* SW workaround for A0 HW erratum: if we're just about
  1229. * to write posted_index, insert a dummy desc
  1230. * of type resvd
  1231. */
  1232. rq_enet_desc_enc(desc, 0, RQ_ENET_TYPE_RESV2, 0);
  1233. vnic_rq_post(rq, 0, 0, 0, 0);
  1234. } else {
  1235. return enic_rq_alloc_buf(rq);
  1236. }
  1237. return 0;
  1238. }
  1239. static int enic_dev_hw_version(struct enic *enic,
  1240. enum vnic_dev_hw_version *hw_ver)
  1241. {
  1242. int err;
  1243. spin_lock(&enic->devcmd_lock);
  1244. err = vnic_dev_hw_version(enic->vdev, hw_ver);
  1245. spin_unlock(&enic->devcmd_lock);
  1246. return err;
  1247. }
  1248. static int enic_set_rq_alloc_buf(struct enic *enic)
  1249. {
  1250. enum vnic_dev_hw_version hw_ver;
  1251. int err;
  1252. err = enic_dev_hw_version(enic, &hw_ver);
  1253. if (err)
  1254. return err;
  1255. switch (hw_ver) {
  1256. case VNIC_DEV_HW_VER_A1:
  1257. enic->rq_alloc_buf = enic_rq_alloc_buf_a1;
  1258. break;
  1259. case VNIC_DEV_HW_VER_A2:
  1260. case VNIC_DEV_HW_VER_UNKNOWN:
  1261. enic->rq_alloc_buf = enic_rq_alloc_buf;
  1262. break;
  1263. default:
  1264. return -ENODEV;
  1265. }
  1266. return 0;
  1267. }
  1268. static void enic_rq_indicate_buf(struct vnic_rq *rq,
  1269. struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
  1270. int skipped, void *opaque)
  1271. {
  1272. struct enic *enic = vnic_dev_priv(rq->vdev);
  1273. struct net_device *netdev = enic->netdev;
  1274. struct sk_buff *skb;
  1275. u8 type, color, eop, sop, ingress_port, vlan_stripped;
  1276. u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
  1277. u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
  1278. u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
  1279. u8 packet_error;
  1280. u16 q_number, completed_index, bytes_written, vlan_tci, checksum;
  1281. u32 rss_hash;
  1282. if (skipped)
  1283. return;
  1284. skb = buf->os_buf;
  1285. prefetch(skb->data - NET_IP_ALIGN);
  1286. pci_unmap_single(enic->pdev, buf->dma_addr,
  1287. buf->len, PCI_DMA_FROMDEVICE);
  1288. cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
  1289. &type, &color, &q_number, &completed_index,
  1290. &ingress_port, &fcoe, &eop, &sop, &rss_type,
  1291. &csum_not_calc, &rss_hash, &bytes_written,
  1292. &packet_error, &vlan_stripped, &vlan_tci, &checksum,
  1293. &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
  1294. &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
  1295. &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
  1296. &fcs_ok);
  1297. if (packet_error) {
  1298. if (!fcs_ok) {
  1299. if (bytes_written > 0)
  1300. enic->rq_bad_fcs++;
  1301. else if (bytes_written == 0)
  1302. enic->rq_truncated_pkts++;
  1303. }
  1304. dev_kfree_skb_any(skb);
  1305. return;
  1306. }
  1307. if (eop && bytes_written > 0) {
  1308. /* Good receive
  1309. */
  1310. skb_put(skb, bytes_written);
  1311. skb->protocol = eth_type_trans(skb, netdev);
  1312. if (enic->csum_rx_enabled && !csum_not_calc) {
  1313. skb->csum = htons(checksum);
  1314. skb->ip_summed = CHECKSUM_COMPLETE;
  1315. }
  1316. skb->dev = netdev;
  1317. if (enic->vlan_group && vlan_stripped &&
  1318. (vlan_tci & CQ_ENET_RQ_DESC_VLAN_TCI_VLAN_MASK)) {
  1319. if (netdev->features & NETIF_F_GRO)
  1320. vlan_gro_receive(&enic->napi[q_number],
  1321. enic->vlan_group, vlan_tci, skb);
  1322. else
  1323. vlan_hwaccel_receive_skb(skb,
  1324. enic->vlan_group, vlan_tci);
  1325. } else {
  1326. if (netdev->features & NETIF_F_GRO)
  1327. napi_gro_receive(&enic->napi[q_number], skb);
  1328. else
  1329. netif_receive_skb(skb);
  1330. }
  1331. } else {
  1332. /* Buffer overflow
  1333. */
  1334. dev_kfree_skb_any(skb);
  1335. }
  1336. }
  1337. static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
  1338. u8 type, u16 q_number, u16 completed_index, void *opaque)
  1339. {
  1340. struct enic *enic = vnic_dev_priv(vdev);
  1341. vnic_rq_service(&enic->rq[q_number], cq_desc,
  1342. completed_index, VNIC_RQ_RETURN_DESC,
  1343. enic_rq_indicate_buf, opaque);
  1344. return 0;
  1345. }
  1346. static int enic_poll(struct napi_struct *napi, int budget)
  1347. {
  1348. struct net_device *netdev = napi->dev;
  1349. struct enic *enic = netdev_priv(netdev);
  1350. unsigned int cq_rq = enic_cq_rq(enic, 0);
  1351. unsigned int cq_wq = enic_cq_wq(enic, 0);
  1352. unsigned int intr = enic_legacy_io_intr();
  1353. unsigned int rq_work_to_do = budget;
  1354. unsigned int wq_work_to_do = -1; /* no limit */
  1355. unsigned int work_done, rq_work_done, wq_work_done;
  1356. int err;
  1357. /* Service RQ (first) and WQ
  1358. */
  1359. rq_work_done = vnic_cq_service(&enic->cq[cq_rq],
  1360. rq_work_to_do, enic_rq_service, NULL);
  1361. wq_work_done = vnic_cq_service(&enic->cq[cq_wq],
  1362. wq_work_to_do, enic_wq_service, NULL);
  1363. /* Accumulate intr event credits for this polling
  1364. * cycle. An intr event is the completion of a
  1365. * a WQ or RQ packet.
  1366. */
  1367. work_done = rq_work_done + wq_work_done;
  1368. if (work_done > 0)
  1369. vnic_intr_return_credits(&enic->intr[intr],
  1370. work_done,
  1371. 0 /* don't unmask intr */,
  1372. 0 /* don't reset intr timer */);
  1373. err = vnic_rq_fill(&enic->rq[0], enic->rq_alloc_buf);
  1374. /* Buffer allocation failed. Stay in polling
  1375. * mode so we can try to fill the ring again.
  1376. */
  1377. if (err)
  1378. rq_work_done = rq_work_to_do;
  1379. if (rq_work_done < rq_work_to_do) {
  1380. /* Some work done, but not enough to stay in polling,
  1381. * exit polling
  1382. */
  1383. napi_complete(napi);
  1384. vnic_intr_unmask(&enic->intr[intr]);
  1385. }
  1386. return rq_work_done;
  1387. }
  1388. static int enic_poll_msix(struct napi_struct *napi, int budget)
  1389. {
  1390. struct net_device *netdev = napi->dev;
  1391. struct enic *enic = netdev_priv(netdev);
  1392. unsigned int rq = (napi - &enic->napi[0]);
  1393. unsigned int cq = enic_cq_rq(enic, rq);
  1394. unsigned int intr = enic_msix_rq_intr(enic, rq);
  1395. unsigned int work_to_do = budget;
  1396. unsigned int work_done;
  1397. int err;
  1398. /* Service RQ
  1399. */
  1400. work_done = vnic_cq_service(&enic->cq[cq],
  1401. work_to_do, enic_rq_service, NULL);
  1402. /* Return intr event credits for this polling
  1403. * cycle. An intr event is the completion of a
  1404. * RQ packet.
  1405. */
  1406. if (work_done > 0)
  1407. vnic_intr_return_credits(&enic->intr[intr],
  1408. work_done,
  1409. 0 /* don't unmask intr */,
  1410. 0 /* don't reset intr timer */);
  1411. err = vnic_rq_fill(&enic->rq[rq], enic->rq_alloc_buf);
  1412. /* Buffer allocation failed. Stay in polling mode
  1413. * so we can try to fill the ring again.
  1414. */
  1415. if (err)
  1416. work_done = work_to_do;
  1417. if (work_done < work_to_do) {
  1418. /* Some work done, but not enough to stay in polling,
  1419. * exit polling
  1420. */
  1421. napi_complete(napi);
  1422. vnic_intr_unmask(&enic->intr[intr]);
  1423. }
  1424. return work_done;
  1425. }
  1426. static void enic_notify_timer(unsigned long data)
  1427. {
  1428. struct enic *enic = (struct enic *)data;
  1429. enic_notify_check(enic);
  1430. mod_timer(&enic->notify_timer,
  1431. round_jiffies(jiffies + ENIC_NOTIFY_TIMER_PERIOD));
  1432. }
  1433. static void enic_free_intr(struct enic *enic)
  1434. {
  1435. struct net_device *netdev = enic->netdev;
  1436. unsigned int i;
  1437. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  1438. case VNIC_DEV_INTR_MODE_INTX:
  1439. free_irq(enic->pdev->irq, netdev);
  1440. break;
  1441. case VNIC_DEV_INTR_MODE_MSI:
  1442. free_irq(enic->pdev->irq, enic);
  1443. break;
  1444. case VNIC_DEV_INTR_MODE_MSIX:
  1445. for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
  1446. if (enic->msix[i].requested)
  1447. free_irq(enic->msix_entry[i].vector,
  1448. enic->msix[i].devid);
  1449. break;
  1450. default:
  1451. break;
  1452. }
  1453. }
  1454. static int enic_request_intr(struct enic *enic)
  1455. {
  1456. struct net_device *netdev = enic->netdev;
  1457. unsigned int i, intr;
  1458. int err = 0;
  1459. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  1460. case VNIC_DEV_INTR_MODE_INTX:
  1461. err = request_irq(enic->pdev->irq, enic_isr_legacy,
  1462. IRQF_SHARED, netdev->name, netdev);
  1463. break;
  1464. case VNIC_DEV_INTR_MODE_MSI:
  1465. err = request_irq(enic->pdev->irq, enic_isr_msi,
  1466. 0, netdev->name, enic);
  1467. break;
  1468. case VNIC_DEV_INTR_MODE_MSIX:
  1469. for (i = 0; i < enic->rq_count; i++) {
  1470. intr = enic_msix_rq_intr(enic, i);
  1471. sprintf(enic->msix[intr].devname,
  1472. "%.11s-rx-%d", netdev->name, i);
  1473. enic->msix[intr].isr = enic_isr_msix_rq;
  1474. enic->msix[intr].devid = &enic->napi[i];
  1475. }
  1476. for (i = 0; i < enic->wq_count; i++) {
  1477. intr = enic_msix_wq_intr(enic, i);
  1478. sprintf(enic->msix[intr].devname,
  1479. "%.11s-tx-%d", netdev->name, i);
  1480. enic->msix[intr].isr = enic_isr_msix_wq;
  1481. enic->msix[intr].devid = enic;
  1482. }
  1483. intr = enic_msix_err_intr(enic);
  1484. sprintf(enic->msix[intr].devname,
  1485. "%.11s-err", netdev->name);
  1486. enic->msix[intr].isr = enic_isr_msix_err;
  1487. enic->msix[intr].devid = enic;
  1488. intr = enic_msix_notify_intr(enic);
  1489. sprintf(enic->msix[intr].devname,
  1490. "%.11s-notify", netdev->name);
  1491. enic->msix[intr].isr = enic_isr_msix_notify;
  1492. enic->msix[intr].devid = enic;
  1493. for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
  1494. enic->msix[i].requested = 0;
  1495. for (i = 0; i < enic->intr_count; i++) {
  1496. err = request_irq(enic->msix_entry[i].vector,
  1497. enic->msix[i].isr, 0,
  1498. enic->msix[i].devname,
  1499. enic->msix[i].devid);
  1500. if (err) {
  1501. enic_free_intr(enic);
  1502. break;
  1503. }
  1504. enic->msix[i].requested = 1;
  1505. }
  1506. break;
  1507. default:
  1508. break;
  1509. }
  1510. return err;
  1511. }
  1512. static void enic_synchronize_irqs(struct enic *enic)
  1513. {
  1514. unsigned int i;
  1515. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  1516. case VNIC_DEV_INTR_MODE_INTX:
  1517. case VNIC_DEV_INTR_MODE_MSI:
  1518. synchronize_irq(enic->pdev->irq);
  1519. break;
  1520. case VNIC_DEV_INTR_MODE_MSIX:
  1521. for (i = 0; i < enic->intr_count; i++)
  1522. synchronize_irq(enic->msix_entry[i].vector);
  1523. break;
  1524. default:
  1525. break;
  1526. }
  1527. }
  1528. static int enic_dev_notify_set(struct enic *enic)
  1529. {
  1530. int err;
  1531. spin_lock(&enic->devcmd_lock);
  1532. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  1533. case VNIC_DEV_INTR_MODE_INTX:
  1534. err = vnic_dev_notify_set(enic->vdev,
  1535. enic_legacy_notify_intr());
  1536. break;
  1537. case VNIC_DEV_INTR_MODE_MSIX:
  1538. err = vnic_dev_notify_set(enic->vdev,
  1539. enic_msix_notify_intr(enic));
  1540. break;
  1541. default:
  1542. err = vnic_dev_notify_set(enic->vdev, -1 /* no intr */);
  1543. break;
  1544. }
  1545. spin_unlock(&enic->devcmd_lock);
  1546. return err;
  1547. }
  1548. static int enic_dev_notify_unset(struct enic *enic)
  1549. {
  1550. int err;
  1551. spin_lock(&enic->devcmd_lock);
  1552. err = vnic_dev_notify_unset(enic->vdev);
  1553. spin_unlock(&enic->devcmd_lock);
  1554. return err;
  1555. }
  1556. static int enic_dev_enable(struct enic *enic)
  1557. {
  1558. int err;
  1559. spin_lock(&enic->devcmd_lock);
  1560. err = vnic_dev_enable_wait(enic->vdev);
  1561. spin_unlock(&enic->devcmd_lock);
  1562. return err;
  1563. }
  1564. static int enic_dev_disable(struct enic *enic)
  1565. {
  1566. int err;
  1567. spin_lock(&enic->devcmd_lock);
  1568. err = vnic_dev_disable(enic->vdev);
  1569. spin_unlock(&enic->devcmd_lock);
  1570. return err;
  1571. }
  1572. static void enic_notify_timer_start(struct enic *enic)
  1573. {
  1574. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  1575. case VNIC_DEV_INTR_MODE_MSI:
  1576. mod_timer(&enic->notify_timer, jiffies);
  1577. break;
  1578. default:
  1579. /* Using intr for notification for INTx/MSI-X */
  1580. break;
  1581. };
  1582. }
  1583. /* rtnl lock is held, process context */
  1584. static int enic_open(struct net_device *netdev)
  1585. {
  1586. struct enic *enic = netdev_priv(netdev);
  1587. unsigned int i;
  1588. int err;
  1589. err = enic_request_intr(enic);
  1590. if (err) {
  1591. netdev_err(netdev, "Unable to request irq.\n");
  1592. return err;
  1593. }
  1594. err = enic_dev_notify_set(enic);
  1595. if (err) {
  1596. netdev_err(netdev,
  1597. "Failed to alloc notify buffer, aborting.\n");
  1598. goto err_out_free_intr;
  1599. }
  1600. for (i = 0; i < enic->rq_count; i++) {
  1601. vnic_rq_fill(&enic->rq[i], enic->rq_alloc_buf);
  1602. /* Need at least one buffer on ring to get going */
  1603. if (vnic_rq_desc_used(&enic->rq[i]) == 0) {
  1604. netdev_err(netdev, "Unable to alloc receive buffers\n");
  1605. err = -ENOMEM;
  1606. goto err_out_notify_unset;
  1607. }
  1608. }
  1609. for (i = 0; i < enic->wq_count; i++)
  1610. vnic_wq_enable(&enic->wq[i]);
  1611. for (i = 0; i < enic->rq_count; i++)
  1612. vnic_rq_enable(&enic->rq[i]);
  1613. if (enic_is_dynamic(enic) && !is_zero_ether_addr(enic->pp.mac_addr))
  1614. enic_dev_add_addr(enic, enic->pp.mac_addr);
  1615. else
  1616. enic_dev_add_station_addr(enic);
  1617. enic_set_rx_mode(netdev);
  1618. netif_wake_queue(netdev);
  1619. for (i = 0; i < enic->rq_count; i++)
  1620. napi_enable(&enic->napi[i]);
  1621. enic_dev_enable(enic);
  1622. for (i = 0; i < enic->intr_count; i++)
  1623. vnic_intr_unmask(&enic->intr[i]);
  1624. enic_notify_timer_start(enic);
  1625. return 0;
  1626. err_out_notify_unset:
  1627. enic_dev_notify_unset(enic);
  1628. err_out_free_intr:
  1629. enic_free_intr(enic);
  1630. return err;
  1631. }
  1632. /* rtnl lock is held, process context */
  1633. static int enic_stop(struct net_device *netdev)
  1634. {
  1635. struct enic *enic = netdev_priv(netdev);
  1636. unsigned int i;
  1637. int err;
  1638. for (i = 0; i < enic->intr_count; i++) {
  1639. vnic_intr_mask(&enic->intr[i]);
  1640. (void)vnic_intr_masked(&enic->intr[i]); /* flush write */
  1641. }
  1642. enic_synchronize_irqs(enic);
  1643. del_timer_sync(&enic->notify_timer);
  1644. enic_dev_disable(enic);
  1645. for (i = 0; i < enic->rq_count; i++)
  1646. napi_disable(&enic->napi[i]);
  1647. netif_carrier_off(netdev);
  1648. netif_tx_disable(netdev);
  1649. if (enic_is_dynamic(enic) && !is_zero_ether_addr(enic->pp.mac_addr))
  1650. enic_dev_del_addr(enic, enic->pp.mac_addr);
  1651. else
  1652. enic_dev_del_station_addr(enic);
  1653. for (i = 0; i < enic->wq_count; i++) {
  1654. err = vnic_wq_disable(&enic->wq[i]);
  1655. if (err)
  1656. return err;
  1657. }
  1658. for (i = 0; i < enic->rq_count; i++) {
  1659. err = vnic_rq_disable(&enic->rq[i]);
  1660. if (err)
  1661. return err;
  1662. }
  1663. enic_dev_notify_unset(enic);
  1664. enic_free_intr(enic);
  1665. for (i = 0; i < enic->wq_count; i++)
  1666. vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
  1667. for (i = 0; i < enic->rq_count; i++)
  1668. vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
  1669. for (i = 0; i < enic->cq_count; i++)
  1670. vnic_cq_clean(&enic->cq[i]);
  1671. for (i = 0; i < enic->intr_count; i++)
  1672. vnic_intr_clean(&enic->intr[i]);
  1673. return 0;
  1674. }
  1675. static int enic_change_mtu(struct net_device *netdev, int new_mtu)
  1676. {
  1677. struct enic *enic = netdev_priv(netdev);
  1678. int running = netif_running(netdev);
  1679. if (new_mtu < ENIC_MIN_MTU || new_mtu > ENIC_MAX_MTU)
  1680. return -EINVAL;
  1681. if (running)
  1682. enic_stop(netdev);
  1683. netdev->mtu = new_mtu;
  1684. if (netdev->mtu > enic->port_mtu)
  1685. netdev_warn(netdev,
  1686. "interface MTU (%d) set higher than port MTU (%d)\n",
  1687. netdev->mtu, enic->port_mtu);
  1688. if (running)
  1689. enic_open(netdev);
  1690. return 0;
  1691. }
  1692. #ifdef CONFIG_NET_POLL_CONTROLLER
  1693. static void enic_poll_controller(struct net_device *netdev)
  1694. {
  1695. struct enic *enic = netdev_priv(netdev);
  1696. struct vnic_dev *vdev = enic->vdev;
  1697. unsigned int i, intr;
  1698. switch (vnic_dev_get_intr_mode(vdev)) {
  1699. case VNIC_DEV_INTR_MODE_MSIX:
  1700. for (i = 0; i < enic->rq_count; i++) {
  1701. intr = enic_msix_rq_intr(enic, i);
  1702. enic_isr_msix_rq(enic->msix_entry[intr].vector,
  1703. &enic->napi[i]);
  1704. }
  1705. intr = enic_msix_wq_intr(enic, i);
  1706. enic_isr_msix_wq(enic->msix_entry[intr].vector, enic);
  1707. break;
  1708. case VNIC_DEV_INTR_MODE_MSI:
  1709. enic_isr_msi(enic->pdev->irq, enic);
  1710. break;
  1711. case VNIC_DEV_INTR_MODE_INTX:
  1712. enic_isr_legacy(enic->pdev->irq, netdev);
  1713. break;
  1714. default:
  1715. break;
  1716. }
  1717. }
  1718. #endif
  1719. static int enic_dev_wait(struct vnic_dev *vdev,
  1720. int (*start)(struct vnic_dev *, int),
  1721. int (*finished)(struct vnic_dev *, int *),
  1722. int arg)
  1723. {
  1724. unsigned long time;
  1725. int done;
  1726. int err;
  1727. BUG_ON(in_interrupt());
  1728. err = start(vdev, arg);
  1729. if (err)
  1730. return err;
  1731. /* Wait for func to complete...2 seconds max
  1732. */
  1733. time = jiffies + (HZ * 2);
  1734. do {
  1735. err = finished(vdev, &done);
  1736. if (err)
  1737. return err;
  1738. if (done)
  1739. return 0;
  1740. schedule_timeout_uninterruptible(HZ / 10);
  1741. } while (time_after(time, jiffies));
  1742. return -ETIMEDOUT;
  1743. }
  1744. static int enic_dev_open(struct enic *enic)
  1745. {
  1746. int err;
  1747. err = enic_dev_wait(enic->vdev, vnic_dev_open,
  1748. vnic_dev_open_done, 0);
  1749. if (err)
  1750. dev_err(enic_get_dev(enic), "vNIC device open failed, err %d\n",
  1751. err);
  1752. return err;
  1753. }
  1754. static int enic_dev_hang_reset(struct enic *enic)
  1755. {
  1756. int err;
  1757. err = enic_dev_wait(enic->vdev, vnic_dev_hang_reset,
  1758. vnic_dev_hang_reset_done, 0);
  1759. if (err)
  1760. netdev_err(enic->netdev, "vNIC hang reset failed, err %d\n",
  1761. err);
  1762. return err;
  1763. }
  1764. static int enic_set_rsskey(struct enic *enic)
  1765. {
  1766. dma_addr_t rss_key_buf_pa;
  1767. union vnic_rss_key *rss_key_buf_va = NULL;
  1768. union vnic_rss_key rss_key = {
  1769. .key[0].b = {85, 67, 83, 97, 119, 101, 115, 111, 109, 101},
  1770. .key[1].b = {80, 65, 76, 79, 117, 110, 105, 113, 117, 101},
  1771. .key[2].b = {76, 73, 78, 85, 88, 114, 111, 99, 107, 115},
  1772. .key[3].b = {69, 78, 73, 67, 105, 115, 99, 111, 111, 108},
  1773. };
  1774. int err;
  1775. rss_key_buf_va = pci_alloc_consistent(enic->pdev,
  1776. sizeof(union vnic_rss_key), &rss_key_buf_pa);
  1777. if (!rss_key_buf_va)
  1778. return -ENOMEM;
  1779. memcpy(rss_key_buf_va, &rss_key, sizeof(union vnic_rss_key));
  1780. spin_lock(&enic->devcmd_lock);
  1781. err = enic_set_rss_key(enic,
  1782. rss_key_buf_pa,
  1783. sizeof(union vnic_rss_key));
  1784. spin_unlock(&enic->devcmd_lock);
  1785. pci_free_consistent(enic->pdev, sizeof(union vnic_rss_key),
  1786. rss_key_buf_va, rss_key_buf_pa);
  1787. return err;
  1788. }
  1789. static int enic_set_rsscpu(struct enic *enic, u8 rss_hash_bits)
  1790. {
  1791. dma_addr_t rss_cpu_buf_pa;
  1792. union vnic_rss_cpu *rss_cpu_buf_va = NULL;
  1793. unsigned int i;
  1794. int err;
  1795. rss_cpu_buf_va = pci_alloc_consistent(enic->pdev,
  1796. sizeof(union vnic_rss_cpu), &rss_cpu_buf_pa);
  1797. if (!rss_cpu_buf_va)
  1798. return -ENOMEM;
  1799. for (i = 0; i < (1 << rss_hash_bits); i++)
  1800. (*rss_cpu_buf_va).cpu[i/4].b[i%4] = i % enic->rq_count;
  1801. spin_lock(&enic->devcmd_lock);
  1802. err = enic_set_rss_cpu(enic,
  1803. rss_cpu_buf_pa,
  1804. sizeof(union vnic_rss_cpu));
  1805. spin_unlock(&enic->devcmd_lock);
  1806. pci_free_consistent(enic->pdev, sizeof(union vnic_rss_cpu),
  1807. rss_cpu_buf_va, rss_cpu_buf_pa);
  1808. return err;
  1809. }
  1810. static int enic_set_niccfg(struct enic *enic, u8 rss_default_cpu,
  1811. u8 rss_hash_type, u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable)
  1812. {
  1813. const u8 tso_ipid_split_en = 0;
  1814. const u8 ig_vlan_strip_en = 1;
  1815. int err;
  1816. /* Enable VLAN tag stripping.
  1817. */
  1818. spin_lock(&enic->devcmd_lock);
  1819. err = enic_set_nic_cfg(enic,
  1820. rss_default_cpu, rss_hash_type,
  1821. rss_hash_bits, rss_base_cpu,
  1822. rss_enable, tso_ipid_split_en,
  1823. ig_vlan_strip_en);
  1824. spin_unlock(&enic->devcmd_lock);
  1825. return err;
  1826. }
  1827. static int enic_set_rss_nic_cfg(struct enic *enic)
  1828. {
  1829. struct device *dev = enic_get_dev(enic);
  1830. const u8 rss_default_cpu = 0;
  1831. const u8 rss_hash_type = NIC_CFG_RSS_HASH_TYPE_IPV4 |
  1832. NIC_CFG_RSS_HASH_TYPE_TCP_IPV4 |
  1833. NIC_CFG_RSS_HASH_TYPE_IPV6 |
  1834. NIC_CFG_RSS_HASH_TYPE_TCP_IPV6;
  1835. const u8 rss_hash_bits = 7;
  1836. const u8 rss_base_cpu = 0;
  1837. u8 rss_enable = ENIC_SETTING(enic, RSS) && (enic->rq_count > 1);
  1838. if (rss_enable) {
  1839. if (!enic_set_rsskey(enic)) {
  1840. if (enic_set_rsscpu(enic, rss_hash_bits)) {
  1841. rss_enable = 0;
  1842. dev_warn(dev, "RSS disabled, "
  1843. "Failed to set RSS cpu indirection table.");
  1844. }
  1845. } else {
  1846. rss_enable = 0;
  1847. dev_warn(dev, "RSS disabled, Failed to set RSS key.\n");
  1848. }
  1849. }
  1850. return enic_set_niccfg(enic, rss_default_cpu, rss_hash_type,
  1851. rss_hash_bits, rss_base_cpu, rss_enable);
  1852. }
  1853. static int enic_dev_hang_notify(struct enic *enic)
  1854. {
  1855. int err;
  1856. spin_lock(&enic->devcmd_lock);
  1857. err = vnic_dev_hang_notify(enic->vdev);
  1858. spin_unlock(&enic->devcmd_lock);
  1859. return err;
  1860. }
  1861. static int enic_dev_set_ig_vlan_rewrite_mode(struct enic *enic)
  1862. {
  1863. int err;
  1864. spin_lock(&enic->devcmd_lock);
  1865. err = vnic_dev_set_ig_vlan_rewrite_mode(enic->vdev,
  1866. IG_VLAN_REWRITE_MODE_PRIORITY_TAG_DEFAULT_VLAN);
  1867. spin_unlock(&enic->devcmd_lock);
  1868. return err;
  1869. }
  1870. static void enic_reset(struct work_struct *work)
  1871. {
  1872. struct enic *enic = container_of(work, struct enic, reset);
  1873. if (!netif_running(enic->netdev))
  1874. return;
  1875. rtnl_lock();
  1876. enic_dev_hang_notify(enic);
  1877. enic_stop(enic->netdev);
  1878. enic_dev_hang_reset(enic);
  1879. enic_reset_multicast_list(enic);
  1880. enic_init_vnic_resources(enic);
  1881. enic_set_rss_nic_cfg(enic);
  1882. enic_dev_set_ig_vlan_rewrite_mode(enic);
  1883. enic_open(enic->netdev);
  1884. rtnl_unlock();
  1885. }
  1886. static int enic_set_intr_mode(struct enic *enic)
  1887. {
  1888. unsigned int n = min_t(unsigned int, enic->rq_count, ENIC_RQ_MAX);
  1889. unsigned int m = 1;
  1890. unsigned int i;
  1891. /* Set interrupt mode (INTx, MSI, MSI-X) depending
  1892. * on system capabilities.
  1893. *
  1894. * Try MSI-X first
  1895. *
  1896. * We need n RQs, m WQs, n+m CQs, and n+m+2 INTRs
  1897. * (the second to last INTR is used for WQ/RQ errors)
  1898. * (the last INTR is used for notifications)
  1899. */
  1900. BUG_ON(ARRAY_SIZE(enic->msix_entry) < n + m + 2);
  1901. for (i = 0; i < n + m + 2; i++)
  1902. enic->msix_entry[i].entry = i;
  1903. /* Use multiple RQs if RSS is enabled
  1904. */
  1905. if (ENIC_SETTING(enic, RSS) &&
  1906. enic->config.intr_mode < 1 &&
  1907. enic->rq_count >= n &&
  1908. enic->wq_count >= m &&
  1909. enic->cq_count >= n + m &&
  1910. enic->intr_count >= n + m + 2) {
  1911. if (!pci_enable_msix(enic->pdev, enic->msix_entry, n + m + 2)) {
  1912. enic->rq_count = n;
  1913. enic->wq_count = m;
  1914. enic->cq_count = n + m;
  1915. enic->intr_count = n + m + 2;
  1916. vnic_dev_set_intr_mode(enic->vdev,
  1917. VNIC_DEV_INTR_MODE_MSIX);
  1918. return 0;
  1919. }
  1920. }
  1921. if (enic->config.intr_mode < 1 &&
  1922. enic->rq_count >= 1 &&
  1923. enic->wq_count >= m &&
  1924. enic->cq_count >= 1 + m &&
  1925. enic->intr_count >= 1 + m + 2) {
  1926. if (!pci_enable_msix(enic->pdev, enic->msix_entry, 1 + m + 2)) {
  1927. enic->rq_count = 1;
  1928. enic->wq_count = m;
  1929. enic->cq_count = 1 + m;
  1930. enic->intr_count = 1 + m + 2;
  1931. vnic_dev_set_intr_mode(enic->vdev,
  1932. VNIC_DEV_INTR_MODE_MSIX);
  1933. return 0;
  1934. }
  1935. }
  1936. /* Next try MSI
  1937. *
  1938. * We need 1 RQ, 1 WQ, 2 CQs, and 1 INTR
  1939. */
  1940. if (enic->config.intr_mode < 2 &&
  1941. enic->rq_count >= 1 &&
  1942. enic->wq_count >= 1 &&
  1943. enic->cq_count >= 2 &&
  1944. enic->intr_count >= 1 &&
  1945. !pci_enable_msi(enic->pdev)) {
  1946. enic->rq_count = 1;
  1947. enic->wq_count = 1;
  1948. enic->cq_count = 2;
  1949. enic->intr_count = 1;
  1950. vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSI);
  1951. return 0;
  1952. }
  1953. /* Next try INTx
  1954. *
  1955. * We need 1 RQ, 1 WQ, 2 CQs, and 3 INTRs
  1956. * (the first INTR is used for WQ/RQ)
  1957. * (the second INTR is used for WQ/RQ errors)
  1958. * (the last INTR is used for notifications)
  1959. */
  1960. if (enic->config.intr_mode < 3 &&
  1961. enic->rq_count >= 1 &&
  1962. enic->wq_count >= 1 &&
  1963. enic->cq_count >= 2 &&
  1964. enic->intr_count >= 3) {
  1965. enic->rq_count = 1;
  1966. enic->wq_count = 1;
  1967. enic->cq_count = 2;
  1968. enic->intr_count = 3;
  1969. vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_INTX);
  1970. return 0;
  1971. }
  1972. vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
  1973. return -EINVAL;
  1974. }
  1975. static void enic_clear_intr_mode(struct enic *enic)
  1976. {
  1977. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  1978. case VNIC_DEV_INTR_MODE_MSIX:
  1979. pci_disable_msix(enic->pdev);
  1980. break;
  1981. case VNIC_DEV_INTR_MODE_MSI:
  1982. pci_disable_msi(enic->pdev);
  1983. break;
  1984. default:
  1985. break;
  1986. }
  1987. vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
  1988. }
  1989. static const struct net_device_ops enic_netdev_dynamic_ops = {
  1990. .ndo_open = enic_open,
  1991. .ndo_stop = enic_stop,
  1992. .ndo_start_xmit = enic_hard_start_xmit,
  1993. .ndo_get_stats = enic_get_stats,
  1994. .ndo_validate_addr = eth_validate_addr,
  1995. .ndo_set_rx_mode = enic_set_rx_mode,
  1996. .ndo_set_multicast_list = enic_set_rx_mode,
  1997. .ndo_set_mac_address = enic_set_mac_address_dynamic,
  1998. .ndo_change_mtu = enic_change_mtu,
  1999. .ndo_vlan_rx_register = enic_vlan_rx_register,
  2000. .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
  2001. .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
  2002. .ndo_tx_timeout = enic_tx_timeout,
  2003. .ndo_set_vf_port = enic_set_vf_port,
  2004. .ndo_get_vf_port = enic_get_vf_port,
  2005. #ifdef IFLA_VF_MAX
  2006. .ndo_set_vf_mac = enic_set_vf_mac,
  2007. #endif
  2008. #ifdef CONFIG_NET_POLL_CONTROLLER
  2009. .ndo_poll_controller = enic_poll_controller,
  2010. #endif
  2011. };
  2012. static const struct net_device_ops enic_netdev_ops = {
  2013. .ndo_open = enic_open,
  2014. .ndo_stop = enic_stop,
  2015. .ndo_start_xmit = enic_hard_start_xmit,
  2016. .ndo_get_stats = enic_get_stats,
  2017. .ndo_validate_addr = eth_validate_addr,
  2018. .ndo_set_mac_address = enic_set_mac_address,
  2019. .ndo_set_rx_mode = enic_set_rx_mode,
  2020. .ndo_set_multicast_list = enic_set_rx_mode,
  2021. .ndo_change_mtu = enic_change_mtu,
  2022. .ndo_vlan_rx_register = enic_vlan_rx_register,
  2023. .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
  2024. .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
  2025. .ndo_tx_timeout = enic_tx_timeout,
  2026. #ifdef CONFIG_NET_POLL_CONTROLLER
  2027. .ndo_poll_controller = enic_poll_controller,
  2028. #endif
  2029. };
  2030. static void enic_dev_deinit(struct enic *enic)
  2031. {
  2032. unsigned int i;
  2033. for (i = 0; i < enic->rq_count; i++)
  2034. netif_napi_del(&enic->napi[i]);
  2035. enic_free_vnic_resources(enic);
  2036. enic_clear_intr_mode(enic);
  2037. }
  2038. static int enic_dev_init(struct enic *enic)
  2039. {
  2040. struct device *dev = enic_get_dev(enic);
  2041. struct net_device *netdev = enic->netdev;
  2042. unsigned int i;
  2043. int err;
  2044. /* Get vNIC configuration
  2045. */
  2046. err = enic_get_vnic_config(enic);
  2047. if (err) {
  2048. dev_err(dev, "Get vNIC configuration failed, aborting\n");
  2049. return err;
  2050. }
  2051. /* Get available resource counts
  2052. */
  2053. enic_get_res_counts(enic);
  2054. /* Set interrupt mode based on resource counts and system
  2055. * capabilities
  2056. */
  2057. err = enic_set_intr_mode(enic);
  2058. if (err) {
  2059. dev_err(dev, "Failed to set intr mode based on resource "
  2060. "counts and system capabilities, aborting\n");
  2061. return err;
  2062. }
  2063. /* Allocate and configure vNIC resources
  2064. */
  2065. err = enic_alloc_vnic_resources(enic);
  2066. if (err) {
  2067. dev_err(dev, "Failed to alloc vNIC resources, aborting\n");
  2068. goto err_out_free_vnic_resources;
  2069. }
  2070. enic_init_vnic_resources(enic);
  2071. err = enic_set_rq_alloc_buf(enic);
  2072. if (err) {
  2073. dev_err(dev, "Failed to set RQ buffer allocator, aborting\n");
  2074. goto err_out_free_vnic_resources;
  2075. }
  2076. err = enic_set_rss_nic_cfg(enic);
  2077. if (err) {
  2078. dev_err(dev, "Failed to config nic, aborting\n");
  2079. goto err_out_free_vnic_resources;
  2080. }
  2081. err = enic_dev_set_ig_vlan_rewrite_mode(enic);
  2082. if (err) {
  2083. dev_err(dev,
  2084. "Failed to set ingress vlan rewrite mode, aborting.\n");
  2085. goto err_out_free_vnic_resources;
  2086. }
  2087. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  2088. default:
  2089. netif_napi_add(netdev, &enic->napi[0], enic_poll, 64);
  2090. break;
  2091. case VNIC_DEV_INTR_MODE_MSIX:
  2092. for (i = 0; i < enic->rq_count; i++)
  2093. netif_napi_add(netdev, &enic->napi[i],
  2094. enic_poll_msix, 64);
  2095. break;
  2096. }
  2097. return 0;
  2098. err_out_free_vnic_resources:
  2099. enic_clear_intr_mode(enic);
  2100. enic_free_vnic_resources(enic);
  2101. return err;
  2102. }
  2103. static void enic_iounmap(struct enic *enic)
  2104. {
  2105. unsigned int i;
  2106. for (i = 0; i < ARRAY_SIZE(enic->bar); i++)
  2107. if (enic->bar[i].vaddr)
  2108. iounmap(enic->bar[i].vaddr);
  2109. }
  2110. static int __devinit enic_probe(struct pci_dev *pdev,
  2111. const struct pci_device_id *ent)
  2112. {
  2113. struct device *dev = &pdev->dev;
  2114. struct net_device *netdev;
  2115. struct enic *enic;
  2116. int using_dac = 0;
  2117. unsigned int i;
  2118. int err;
  2119. /* Allocate net device structure and initialize. Private
  2120. * instance data is initialized to zero.
  2121. */
  2122. netdev = alloc_etherdev(sizeof(struct enic));
  2123. if (!netdev) {
  2124. pr_err("Etherdev alloc failed, aborting\n");
  2125. return -ENOMEM;
  2126. }
  2127. pci_set_drvdata(pdev, netdev);
  2128. SET_NETDEV_DEV(netdev, &pdev->dev);
  2129. enic = netdev_priv(netdev);
  2130. enic->netdev = netdev;
  2131. enic->pdev = pdev;
  2132. /* Setup PCI resources
  2133. */
  2134. err = pci_enable_device_mem(pdev);
  2135. if (err) {
  2136. dev_err(dev, "Cannot enable PCI device, aborting\n");
  2137. goto err_out_free_netdev;
  2138. }
  2139. err = pci_request_regions(pdev, DRV_NAME);
  2140. if (err) {
  2141. dev_err(dev, "Cannot request PCI regions, aborting\n");
  2142. goto err_out_disable_device;
  2143. }
  2144. pci_set_master(pdev);
  2145. /* Query PCI controller on system for DMA addressing
  2146. * limitation for the device. Try 40-bit first, and
  2147. * fail to 32-bit.
  2148. */
  2149. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40));
  2150. if (err) {
  2151. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  2152. if (err) {
  2153. dev_err(dev, "No usable DMA configuration, aborting\n");
  2154. goto err_out_release_regions;
  2155. }
  2156. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  2157. if (err) {
  2158. dev_err(dev, "Unable to obtain %u-bit DMA "
  2159. "for consistent allocations, aborting\n", 32);
  2160. goto err_out_release_regions;
  2161. }
  2162. } else {
  2163. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
  2164. if (err) {
  2165. dev_err(dev, "Unable to obtain %u-bit DMA "
  2166. "for consistent allocations, aborting\n", 40);
  2167. goto err_out_release_regions;
  2168. }
  2169. using_dac = 1;
  2170. }
  2171. /* Map vNIC resources from BAR0-5
  2172. */
  2173. for (i = 0; i < ARRAY_SIZE(enic->bar); i++) {
  2174. if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
  2175. continue;
  2176. enic->bar[i].len = pci_resource_len(pdev, i);
  2177. enic->bar[i].vaddr = pci_iomap(pdev, i, enic->bar[i].len);
  2178. if (!enic->bar[i].vaddr) {
  2179. dev_err(dev, "Cannot memory-map BAR %d, aborting\n", i);
  2180. err = -ENODEV;
  2181. goto err_out_iounmap;
  2182. }
  2183. enic->bar[i].bus_addr = pci_resource_start(pdev, i);
  2184. }
  2185. /* Register vNIC device
  2186. */
  2187. enic->vdev = vnic_dev_register(NULL, enic, pdev, enic->bar,
  2188. ARRAY_SIZE(enic->bar));
  2189. if (!enic->vdev) {
  2190. dev_err(dev, "vNIC registration failed, aborting\n");
  2191. err = -ENODEV;
  2192. goto err_out_iounmap;
  2193. }
  2194. /* Issue device open to get device in known state
  2195. */
  2196. err = enic_dev_open(enic);
  2197. if (err) {
  2198. dev_err(dev, "vNIC dev open failed, aborting\n");
  2199. goto err_out_vnic_unregister;
  2200. }
  2201. /* Issue device init to initialize the vnic-to-switch link.
  2202. * We'll start with carrier off and wait for link UP
  2203. * notification later to turn on carrier. We don't need
  2204. * to wait here for the vnic-to-switch link initialization
  2205. * to complete; link UP notification is the indication that
  2206. * the process is complete.
  2207. */
  2208. netif_carrier_off(netdev);
  2209. /* Do not call dev_init for a dynamic vnic.
  2210. * For a dynamic vnic, init_prov_info will be
  2211. * called later by an upper layer.
  2212. */
  2213. if (!enic_is_dynamic(enic)) {
  2214. err = vnic_dev_init(enic->vdev, 0);
  2215. if (err) {
  2216. dev_err(dev, "vNIC dev init failed, aborting\n");
  2217. goto err_out_dev_close;
  2218. }
  2219. }
  2220. /* Setup devcmd lock
  2221. */
  2222. spin_lock_init(&enic->devcmd_lock);
  2223. err = enic_dev_init(enic);
  2224. if (err) {
  2225. dev_err(dev, "Device initialization failed, aborting\n");
  2226. goto err_out_dev_close;
  2227. }
  2228. /* Setup notification timer, HW reset task, and wq locks
  2229. */
  2230. init_timer(&enic->notify_timer);
  2231. enic->notify_timer.function = enic_notify_timer;
  2232. enic->notify_timer.data = (unsigned long)enic;
  2233. INIT_WORK(&enic->reset, enic_reset);
  2234. for (i = 0; i < enic->wq_count; i++)
  2235. spin_lock_init(&enic->wq_lock[i]);
  2236. /* Register net device
  2237. */
  2238. enic->port_mtu = enic->config.mtu;
  2239. (void)enic_change_mtu(netdev, enic->port_mtu);
  2240. err = enic_set_mac_addr(netdev, enic->mac_addr);
  2241. if (err) {
  2242. dev_err(dev, "Invalid MAC address, aborting\n");
  2243. goto err_out_dev_deinit;
  2244. }
  2245. enic->tx_coalesce_usecs = enic->config.intr_timer_usec;
  2246. enic->rx_coalesce_usecs = enic->tx_coalesce_usecs;
  2247. if (enic_is_dynamic(enic))
  2248. netdev->netdev_ops = &enic_netdev_dynamic_ops;
  2249. else
  2250. netdev->netdev_ops = &enic_netdev_ops;
  2251. netdev->watchdog_timeo = 2 * HZ;
  2252. netdev->ethtool_ops = &enic_ethtool_ops;
  2253. netdev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  2254. if (ENIC_SETTING(enic, LOOP)) {
  2255. netdev->features &= ~NETIF_F_HW_VLAN_TX;
  2256. enic->loop_enable = 1;
  2257. enic->loop_tag = enic->config.loop_tag;
  2258. dev_info(dev, "loopback tag=0x%04x\n", enic->loop_tag);
  2259. }
  2260. if (ENIC_SETTING(enic, TXCSUM))
  2261. netdev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
  2262. if (ENIC_SETTING(enic, TSO))
  2263. netdev->features |= NETIF_F_TSO |
  2264. NETIF_F_TSO6 | NETIF_F_TSO_ECN;
  2265. if (ENIC_SETTING(enic, LRO))
  2266. netdev->features |= NETIF_F_GRO;
  2267. if (using_dac)
  2268. netdev->features |= NETIF_F_HIGHDMA;
  2269. enic->csum_rx_enabled = ENIC_SETTING(enic, RXCSUM);
  2270. err = register_netdev(netdev);
  2271. if (err) {
  2272. dev_err(dev, "Cannot register net device, aborting\n");
  2273. goto err_out_dev_deinit;
  2274. }
  2275. return 0;
  2276. err_out_dev_deinit:
  2277. enic_dev_deinit(enic);
  2278. err_out_dev_close:
  2279. vnic_dev_close(enic->vdev);
  2280. err_out_vnic_unregister:
  2281. vnic_dev_unregister(enic->vdev);
  2282. err_out_iounmap:
  2283. enic_iounmap(enic);
  2284. err_out_release_regions:
  2285. pci_release_regions(pdev);
  2286. err_out_disable_device:
  2287. pci_disable_device(pdev);
  2288. err_out_free_netdev:
  2289. pci_set_drvdata(pdev, NULL);
  2290. free_netdev(netdev);
  2291. return err;
  2292. }
  2293. static void __devexit enic_remove(struct pci_dev *pdev)
  2294. {
  2295. struct net_device *netdev = pci_get_drvdata(pdev);
  2296. if (netdev) {
  2297. struct enic *enic = netdev_priv(netdev);
  2298. cancel_work_sync(&enic->reset);
  2299. unregister_netdev(netdev);
  2300. enic_dev_deinit(enic);
  2301. vnic_dev_close(enic->vdev);
  2302. vnic_dev_unregister(enic->vdev);
  2303. enic_iounmap(enic);
  2304. pci_release_regions(pdev);
  2305. pci_disable_device(pdev);
  2306. pci_set_drvdata(pdev, NULL);
  2307. free_netdev(netdev);
  2308. }
  2309. }
  2310. static struct pci_driver enic_driver = {
  2311. .name = DRV_NAME,
  2312. .id_table = enic_id_table,
  2313. .probe = enic_probe,
  2314. .remove = __devexit_p(enic_remove),
  2315. };
  2316. static int __init enic_init_module(void)
  2317. {
  2318. pr_info("%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
  2319. return pci_register_driver(&enic_driver);
  2320. }
  2321. static void __exit enic_cleanup_module(void)
  2322. {
  2323. pci_unregister_driver(&enic_driver);
  2324. }
  2325. module_init(enic_init_module);
  2326. module_exit(enic_cleanup_module);