enic_main.c 63 KB

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