enic_main.c 61 KB

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