enic_main.c 60 KB

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