enic_main.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950
  1. /*
  2. * Copyright 2008 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 <net/ip6_checksum.h>
  37. #include "cq_enet_desc.h"
  38. #include "vnic_dev.h"
  39. #include "vnic_intr.h"
  40. #include "vnic_stats.h"
  41. #include "enic_res.h"
  42. #include "enic.h"
  43. #define ENIC_NOTIFY_TIMER_PERIOD (2 * HZ)
  44. /* Supported devices */
  45. static struct pci_device_id enic_id_table[] = {
  46. { PCI_VDEVICE(CISCO, 0x0043) },
  47. { 0, } /* end of table */
  48. };
  49. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  50. MODULE_AUTHOR("Scott Feldman <scofeldm@cisco.com>");
  51. MODULE_LICENSE("GPL");
  52. MODULE_VERSION(DRV_VERSION);
  53. MODULE_DEVICE_TABLE(pci, enic_id_table);
  54. struct enic_stat {
  55. char name[ETH_GSTRING_LEN];
  56. unsigned int offset;
  57. };
  58. #define ENIC_TX_STAT(stat) \
  59. { .name = #stat, .offset = offsetof(struct vnic_tx_stats, stat) / 8 }
  60. #define ENIC_RX_STAT(stat) \
  61. { .name = #stat, .offset = offsetof(struct vnic_rx_stats, stat) / 8 }
  62. static const struct enic_stat enic_tx_stats[] = {
  63. ENIC_TX_STAT(tx_frames_ok),
  64. ENIC_TX_STAT(tx_unicast_frames_ok),
  65. ENIC_TX_STAT(tx_multicast_frames_ok),
  66. ENIC_TX_STAT(tx_broadcast_frames_ok),
  67. ENIC_TX_STAT(tx_bytes_ok),
  68. ENIC_TX_STAT(tx_unicast_bytes_ok),
  69. ENIC_TX_STAT(tx_multicast_bytes_ok),
  70. ENIC_TX_STAT(tx_broadcast_bytes_ok),
  71. ENIC_TX_STAT(tx_drops),
  72. ENIC_TX_STAT(tx_errors),
  73. ENIC_TX_STAT(tx_tso),
  74. };
  75. static const struct enic_stat enic_rx_stats[] = {
  76. ENIC_RX_STAT(rx_frames_ok),
  77. ENIC_RX_STAT(rx_frames_total),
  78. ENIC_RX_STAT(rx_unicast_frames_ok),
  79. ENIC_RX_STAT(rx_multicast_frames_ok),
  80. ENIC_RX_STAT(rx_broadcast_frames_ok),
  81. ENIC_RX_STAT(rx_bytes_ok),
  82. ENIC_RX_STAT(rx_unicast_bytes_ok),
  83. ENIC_RX_STAT(rx_multicast_bytes_ok),
  84. ENIC_RX_STAT(rx_broadcast_bytes_ok),
  85. ENIC_RX_STAT(rx_drop),
  86. ENIC_RX_STAT(rx_no_bufs),
  87. ENIC_RX_STAT(rx_errors),
  88. ENIC_RX_STAT(rx_rss),
  89. ENIC_RX_STAT(rx_crc_errors),
  90. ENIC_RX_STAT(rx_frames_64),
  91. ENIC_RX_STAT(rx_frames_127),
  92. ENIC_RX_STAT(rx_frames_255),
  93. ENIC_RX_STAT(rx_frames_511),
  94. ENIC_RX_STAT(rx_frames_1023),
  95. ENIC_RX_STAT(rx_frames_1518),
  96. ENIC_RX_STAT(rx_frames_to_max),
  97. };
  98. static const unsigned int enic_n_tx_stats = ARRAY_SIZE(enic_tx_stats);
  99. static const unsigned int enic_n_rx_stats = ARRAY_SIZE(enic_rx_stats);
  100. static int enic_get_settings(struct net_device *netdev,
  101. struct ethtool_cmd *ecmd)
  102. {
  103. struct enic *enic = netdev_priv(netdev);
  104. ecmd->supported = (SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE);
  105. ecmd->advertising = (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
  106. ecmd->port = PORT_FIBRE;
  107. ecmd->transceiver = XCVR_EXTERNAL;
  108. if (netif_carrier_ok(netdev)) {
  109. ecmd->speed = vnic_dev_port_speed(enic->vdev);
  110. ecmd->duplex = DUPLEX_FULL;
  111. } else {
  112. ecmd->speed = -1;
  113. ecmd->duplex = -1;
  114. }
  115. ecmd->autoneg = AUTONEG_DISABLE;
  116. return 0;
  117. }
  118. static void enic_get_drvinfo(struct net_device *netdev,
  119. struct ethtool_drvinfo *drvinfo)
  120. {
  121. struct enic *enic = netdev_priv(netdev);
  122. struct vnic_devcmd_fw_info *fw_info;
  123. spin_lock(&enic->devcmd_lock);
  124. vnic_dev_fw_info(enic->vdev, &fw_info);
  125. spin_unlock(&enic->devcmd_lock);
  126. strncpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
  127. strncpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version));
  128. strncpy(drvinfo->fw_version, fw_info->fw_version,
  129. sizeof(drvinfo->fw_version));
  130. strncpy(drvinfo->bus_info, pci_name(enic->pdev),
  131. sizeof(drvinfo->bus_info));
  132. }
  133. static void enic_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
  134. {
  135. unsigned int i;
  136. switch (stringset) {
  137. case ETH_SS_STATS:
  138. for (i = 0; i < enic_n_tx_stats; i++) {
  139. memcpy(data, enic_tx_stats[i].name, ETH_GSTRING_LEN);
  140. data += ETH_GSTRING_LEN;
  141. }
  142. for (i = 0; i < enic_n_rx_stats; i++) {
  143. memcpy(data, enic_rx_stats[i].name, ETH_GSTRING_LEN);
  144. data += ETH_GSTRING_LEN;
  145. }
  146. break;
  147. }
  148. }
  149. static int enic_get_sset_count(struct net_device *netdev, int sset)
  150. {
  151. switch (sset) {
  152. case ETH_SS_STATS:
  153. return enic_n_tx_stats + enic_n_rx_stats;
  154. default:
  155. return -EOPNOTSUPP;
  156. }
  157. }
  158. static void enic_get_ethtool_stats(struct net_device *netdev,
  159. struct ethtool_stats *stats, u64 *data)
  160. {
  161. struct enic *enic = netdev_priv(netdev);
  162. struct vnic_stats *vstats;
  163. unsigned int i;
  164. spin_lock(&enic->devcmd_lock);
  165. vnic_dev_stats_dump(enic->vdev, &vstats);
  166. spin_unlock(&enic->devcmd_lock);
  167. for (i = 0; i < enic_n_tx_stats; i++)
  168. *(data++) = ((u64 *)&vstats->tx)[enic_tx_stats[i].offset];
  169. for (i = 0; i < enic_n_rx_stats; i++)
  170. *(data++) = ((u64 *)&vstats->rx)[enic_rx_stats[i].offset];
  171. }
  172. static u32 enic_get_rx_csum(struct net_device *netdev)
  173. {
  174. struct enic *enic = netdev_priv(netdev);
  175. return enic->csum_rx_enabled;
  176. }
  177. static int enic_set_rx_csum(struct net_device *netdev, u32 data)
  178. {
  179. struct enic *enic = netdev_priv(netdev);
  180. if (data && !ENIC_SETTING(enic, RXCSUM))
  181. return -EINVAL;
  182. enic->csum_rx_enabled = !!data;
  183. return 0;
  184. }
  185. static int enic_set_tx_csum(struct net_device *netdev, u32 data)
  186. {
  187. struct enic *enic = netdev_priv(netdev);
  188. if (data && !ENIC_SETTING(enic, TXCSUM))
  189. return -EINVAL;
  190. if (data)
  191. netdev->features |= NETIF_F_HW_CSUM;
  192. else
  193. netdev->features &= ~NETIF_F_HW_CSUM;
  194. return 0;
  195. }
  196. static int enic_set_tso(struct net_device *netdev, u32 data)
  197. {
  198. struct enic *enic = netdev_priv(netdev);
  199. if (data && !ENIC_SETTING(enic, TSO))
  200. return -EINVAL;
  201. if (data)
  202. netdev->features |=
  203. NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN;
  204. else
  205. netdev->features &=
  206. ~(NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN);
  207. return 0;
  208. }
  209. static u32 enic_get_msglevel(struct net_device *netdev)
  210. {
  211. struct enic *enic = netdev_priv(netdev);
  212. return enic->msg_enable;
  213. }
  214. static void enic_set_msglevel(struct net_device *netdev, u32 value)
  215. {
  216. struct enic *enic = netdev_priv(netdev);
  217. enic->msg_enable = value;
  218. }
  219. static struct ethtool_ops enic_ethtool_ops = {
  220. .get_settings = enic_get_settings,
  221. .get_drvinfo = enic_get_drvinfo,
  222. .get_msglevel = enic_get_msglevel,
  223. .set_msglevel = enic_set_msglevel,
  224. .get_link = ethtool_op_get_link,
  225. .get_strings = enic_get_strings,
  226. .get_sset_count = enic_get_sset_count,
  227. .get_ethtool_stats = enic_get_ethtool_stats,
  228. .get_rx_csum = enic_get_rx_csum,
  229. .set_rx_csum = enic_set_rx_csum,
  230. .get_tx_csum = ethtool_op_get_tx_csum,
  231. .set_tx_csum = enic_set_tx_csum,
  232. .get_sg = ethtool_op_get_sg,
  233. .set_sg = ethtool_op_set_sg,
  234. .get_tso = ethtool_op_get_tso,
  235. .set_tso = enic_set_tso,
  236. .get_flags = ethtool_op_get_flags,
  237. .set_flags = ethtool_op_set_flags,
  238. };
  239. static void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
  240. {
  241. struct enic *enic = vnic_dev_priv(wq->vdev);
  242. if (buf->sop)
  243. pci_unmap_single(enic->pdev, buf->dma_addr,
  244. buf->len, PCI_DMA_TODEVICE);
  245. else
  246. pci_unmap_page(enic->pdev, buf->dma_addr,
  247. buf->len, PCI_DMA_TODEVICE);
  248. if (buf->os_buf)
  249. dev_kfree_skb_any(buf->os_buf);
  250. }
  251. static void enic_wq_free_buf(struct vnic_wq *wq,
  252. struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque)
  253. {
  254. enic_free_wq_buf(wq, buf);
  255. }
  256. static int enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
  257. u8 type, u16 q_number, u16 completed_index, void *opaque)
  258. {
  259. struct enic *enic = vnic_dev_priv(vdev);
  260. spin_lock(&enic->wq_lock[q_number]);
  261. vnic_wq_service(&enic->wq[q_number], cq_desc,
  262. completed_index, enic_wq_free_buf,
  263. opaque);
  264. if (netif_queue_stopped(enic->netdev) &&
  265. vnic_wq_desc_avail(&enic->wq[q_number]) >= MAX_SKB_FRAGS + 1)
  266. netif_wake_queue(enic->netdev);
  267. spin_unlock(&enic->wq_lock[q_number]);
  268. return 0;
  269. }
  270. static void enic_log_q_error(struct enic *enic)
  271. {
  272. unsigned int i;
  273. u32 error_status;
  274. for (i = 0; i < enic->wq_count; i++) {
  275. error_status = vnic_wq_error_status(&enic->wq[i]);
  276. if (error_status)
  277. printk(KERN_ERR PFX "%s: WQ[%d] error_status %d\n",
  278. enic->netdev->name, i, error_status);
  279. }
  280. for (i = 0; i < enic->rq_count; i++) {
  281. error_status = vnic_rq_error_status(&enic->rq[i]);
  282. if (error_status)
  283. printk(KERN_ERR PFX "%s: RQ[%d] error_status %d\n",
  284. enic->netdev->name, i, error_status);
  285. }
  286. }
  287. static void enic_link_check(struct enic *enic)
  288. {
  289. int link_status = vnic_dev_link_status(enic->vdev);
  290. int carrier_ok = netif_carrier_ok(enic->netdev);
  291. if (link_status && !carrier_ok) {
  292. printk(KERN_INFO PFX "%s: Link UP\n", enic->netdev->name);
  293. netif_carrier_on(enic->netdev);
  294. } else if (!link_status && carrier_ok) {
  295. printk(KERN_INFO PFX "%s: Link DOWN\n", enic->netdev->name);
  296. netif_carrier_off(enic->netdev);
  297. }
  298. }
  299. static void enic_mtu_check(struct enic *enic)
  300. {
  301. u32 mtu = vnic_dev_mtu(enic->vdev);
  302. if (mtu != enic->port_mtu) {
  303. if (mtu < enic->netdev->mtu)
  304. printk(KERN_WARNING PFX
  305. "%s: interface MTU (%d) set higher "
  306. "than switch port MTU (%d)\n",
  307. enic->netdev->name, enic->netdev->mtu, mtu);
  308. enic->port_mtu = mtu;
  309. }
  310. }
  311. static void enic_msglvl_check(struct enic *enic)
  312. {
  313. u32 msg_enable = vnic_dev_msg_lvl(enic->vdev);
  314. if (msg_enable != enic->msg_enable) {
  315. printk(KERN_INFO PFX "%s: msg lvl changed from 0x%x to 0x%x\n",
  316. enic->netdev->name, enic->msg_enable, msg_enable);
  317. enic->msg_enable = msg_enable;
  318. }
  319. }
  320. static void enic_notify_check(struct enic *enic)
  321. {
  322. enic_msglvl_check(enic);
  323. enic_mtu_check(enic);
  324. enic_link_check(enic);
  325. }
  326. #define ENIC_TEST_INTR(pba, i) (pba & (1 << i))
  327. static irqreturn_t enic_isr_legacy(int irq, void *data)
  328. {
  329. struct net_device *netdev = data;
  330. struct enic *enic = netdev_priv(netdev);
  331. u32 pba;
  332. vnic_intr_mask(&enic->intr[ENIC_INTX_WQ_RQ]);
  333. pba = vnic_intr_legacy_pba(enic->legacy_pba);
  334. if (!pba) {
  335. vnic_intr_unmask(&enic->intr[ENIC_INTX_WQ_RQ]);
  336. return IRQ_NONE; /* not our interrupt */
  337. }
  338. if (ENIC_TEST_INTR(pba, ENIC_INTX_NOTIFY)) {
  339. vnic_intr_return_all_credits(&enic->intr[ENIC_INTX_NOTIFY]);
  340. enic_notify_check(enic);
  341. }
  342. if (ENIC_TEST_INTR(pba, ENIC_INTX_ERR)) {
  343. vnic_intr_return_all_credits(&enic->intr[ENIC_INTX_ERR]);
  344. enic_log_q_error(enic);
  345. /* schedule recovery from WQ/RQ error */
  346. schedule_work(&enic->reset);
  347. return IRQ_HANDLED;
  348. }
  349. if (ENIC_TEST_INTR(pba, ENIC_INTX_WQ_RQ)) {
  350. if (napi_schedule_prep(&enic->napi))
  351. __napi_schedule(&enic->napi);
  352. } else {
  353. vnic_intr_unmask(&enic->intr[ENIC_INTX_WQ_RQ]);
  354. }
  355. return IRQ_HANDLED;
  356. }
  357. static irqreturn_t enic_isr_msi(int irq, void *data)
  358. {
  359. struct enic *enic = data;
  360. /* With MSI, there is no sharing of interrupts, so this is
  361. * our interrupt and there is no need to ack it. The device
  362. * is not providing per-vector masking, so the OS will not
  363. * write to PCI config space to mask/unmask the interrupt.
  364. * We're using mask_on_assertion for MSI, so the device
  365. * automatically masks the interrupt when the interrupt is
  366. * generated. Later, when exiting polling, the interrupt
  367. * will be unmasked (see enic_poll).
  368. *
  369. * Also, the device uses the same PCIe Traffic Class (TC)
  370. * for Memory Write data and MSI, so there are no ordering
  371. * issues; the MSI will always arrive at the Root Complex
  372. * _after_ corresponding Memory Writes (i.e. descriptor
  373. * writes).
  374. */
  375. napi_schedule(&enic->napi);
  376. return IRQ_HANDLED;
  377. }
  378. static irqreturn_t enic_isr_msix_rq(int irq, void *data)
  379. {
  380. struct enic *enic = data;
  381. /* schedule NAPI polling for RQ cleanup */
  382. napi_schedule(&enic->napi);
  383. return IRQ_HANDLED;
  384. }
  385. static irqreturn_t enic_isr_msix_wq(int irq, void *data)
  386. {
  387. struct enic *enic = data;
  388. unsigned int wq_work_to_do = -1; /* no limit */
  389. unsigned int wq_work_done;
  390. wq_work_done = vnic_cq_service(&enic->cq[ENIC_CQ_WQ],
  391. wq_work_to_do, enic_wq_service, NULL);
  392. vnic_intr_return_credits(&enic->intr[ENIC_MSIX_WQ],
  393. wq_work_done,
  394. 1 /* unmask intr */,
  395. 1 /* reset intr timer */);
  396. return IRQ_HANDLED;
  397. }
  398. static irqreturn_t enic_isr_msix_err(int irq, void *data)
  399. {
  400. struct enic *enic = data;
  401. vnic_intr_return_all_credits(&enic->intr[ENIC_MSIX_ERR]);
  402. enic_log_q_error(enic);
  403. /* schedule recovery from WQ/RQ error */
  404. schedule_work(&enic->reset);
  405. return IRQ_HANDLED;
  406. }
  407. static irqreturn_t enic_isr_msix_notify(int irq, void *data)
  408. {
  409. struct enic *enic = data;
  410. vnic_intr_return_all_credits(&enic->intr[ENIC_MSIX_NOTIFY]);
  411. enic_notify_check(enic);
  412. return IRQ_HANDLED;
  413. }
  414. static inline void enic_queue_wq_skb_cont(struct enic *enic,
  415. struct vnic_wq *wq, struct sk_buff *skb,
  416. unsigned int len_left)
  417. {
  418. skb_frag_t *frag;
  419. /* Queue additional data fragments */
  420. for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
  421. len_left -= frag->size;
  422. enic_queue_wq_desc_cont(wq, skb,
  423. pci_map_page(enic->pdev, frag->page,
  424. frag->page_offset, frag->size,
  425. PCI_DMA_TODEVICE),
  426. frag->size,
  427. (len_left == 0)); /* EOP? */
  428. }
  429. }
  430. static inline void enic_queue_wq_skb_vlan(struct enic *enic,
  431. struct vnic_wq *wq, struct sk_buff *skb,
  432. int vlan_tag_insert, unsigned int vlan_tag)
  433. {
  434. unsigned int head_len = skb_headlen(skb);
  435. unsigned int len_left = skb->len - head_len;
  436. int eop = (len_left == 0);
  437. /* Queue the main skb fragment */
  438. enic_queue_wq_desc(wq, skb,
  439. pci_map_single(enic->pdev, skb->data,
  440. head_len, PCI_DMA_TODEVICE),
  441. head_len,
  442. vlan_tag_insert, vlan_tag,
  443. eop);
  444. if (!eop)
  445. enic_queue_wq_skb_cont(enic, wq, skb, len_left);
  446. }
  447. static inline void enic_queue_wq_skb_csum_l4(struct enic *enic,
  448. struct vnic_wq *wq, struct sk_buff *skb,
  449. int vlan_tag_insert, unsigned int vlan_tag)
  450. {
  451. unsigned int head_len = skb_headlen(skb);
  452. unsigned int len_left = skb->len - head_len;
  453. unsigned int hdr_len = skb_transport_offset(skb);
  454. unsigned int csum_offset = hdr_len + skb->csum_offset;
  455. int eop = (len_left == 0);
  456. /* Queue the main skb fragment */
  457. enic_queue_wq_desc_csum_l4(wq, skb,
  458. pci_map_single(enic->pdev, skb->data,
  459. head_len, PCI_DMA_TODEVICE),
  460. head_len,
  461. csum_offset,
  462. hdr_len,
  463. vlan_tag_insert, vlan_tag,
  464. eop);
  465. if (!eop)
  466. enic_queue_wq_skb_cont(enic, wq, skb, len_left);
  467. }
  468. static inline void enic_queue_wq_skb_tso(struct enic *enic,
  469. struct vnic_wq *wq, struct sk_buff *skb, unsigned int mss,
  470. int vlan_tag_insert, unsigned int vlan_tag)
  471. {
  472. unsigned int head_len = skb_headlen(skb);
  473. unsigned int len_left = skb->len - head_len;
  474. unsigned int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  475. int eop = (len_left == 0);
  476. /* Preload TCP csum field with IP pseudo hdr calculated
  477. * with IP length set to zero. HW will later add in length
  478. * to each TCP segment resulting from the TSO.
  479. */
  480. if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
  481. ip_hdr(skb)->check = 0;
  482. tcp_hdr(skb)->check = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  483. ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  484. } else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
  485. tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  486. &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  487. }
  488. /* Queue the main skb fragment */
  489. enic_queue_wq_desc_tso(wq, skb,
  490. pci_map_single(enic->pdev, skb->data,
  491. head_len, PCI_DMA_TODEVICE),
  492. head_len,
  493. mss, hdr_len,
  494. vlan_tag_insert, vlan_tag,
  495. eop);
  496. if (!eop)
  497. enic_queue_wq_skb_cont(enic, wq, skb, len_left);
  498. }
  499. static inline void enic_queue_wq_skb(struct enic *enic,
  500. struct vnic_wq *wq, struct sk_buff *skb)
  501. {
  502. unsigned int mss = skb_shinfo(skb)->gso_size;
  503. unsigned int vlan_tag = 0;
  504. int vlan_tag_insert = 0;
  505. if (enic->vlan_group && vlan_tx_tag_present(skb)) {
  506. /* VLAN tag from trunking driver */
  507. vlan_tag_insert = 1;
  508. vlan_tag = vlan_tx_tag_get(skb);
  509. }
  510. if (mss)
  511. enic_queue_wq_skb_tso(enic, wq, skb, mss,
  512. vlan_tag_insert, vlan_tag);
  513. else if (skb->ip_summed == CHECKSUM_PARTIAL)
  514. enic_queue_wq_skb_csum_l4(enic, wq, skb,
  515. vlan_tag_insert, vlan_tag);
  516. else
  517. enic_queue_wq_skb_vlan(enic, wq, skb,
  518. vlan_tag_insert, vlan_tag);
  519. }
  520. /* netif_tx_lock held, process context with BHs disabled, or BH */
  521. static int enic_hard_start_xmit(struct sk_buff *skb, struct net_device *netdev)
  522. {
  523. struct enic *enic = netdev_priv(netdev);
  524. struct vnic_wq *wq = &enic->wq[0];
  525. unsigned long flags;
  526. if (skb->len <= 0) {
  527. dev_kfree_skb(skb);
  528. return NETDEV_TX_OK;
  529. }
  530. /* Non-TSO sends must fit within ENIC_NON_TSO_MAX_DESC descs,
  531. * which is very likely. In the off chance it's going to take
  532. * more than * ENIC_NON_TSO_MAX_DESC, linearize the skb.
  533. */
  534. if (skb_shinfo(skb)->gso_size == 0 &&
  535. skb_shinfo(skb)->nr_frags + 1 > ENIC_NON_TSO_MAX_DESC &&
  536. skb_linearize(skb)) {
  537. dev_kfree_skb(skb);
  538. return NETDEV_TX_OK;
  539. }
  540. spin_lock_irqsave(&enic->wq_lock[0], flags);
  541. if (vnic_wq_desc_avail(wq) < skb_shinfo(skb)->nr_frags + 1) {
  542. netif_stop_queue(netdev);
  543. /* This is a hard error, log it */
  544. printk(KERN_ERR PFX "%s: BUG! Tx ring full when "
  545. "queue awake!\n", netdev->name);
  546. spin_unlock_irqrestore(&enic->wq_lock[0], flags);
  547. return NETDEV_TX_BUSY;
  548. }
  549. enic_queue_wq_skb(enic, wq, skb);
  550. if (vnic_wq_desc_avail(wq) < MAX_SKB_FRAGS + 1)
  551. netif_stop_queue(netdev);
  552. netdev->trans_start = jiffies;
  553. spin_unlock_irqrestore(&enic->wq_lock[0], flags);
  554. return NETDEV_TX_OK;
  555. }
  556. /* dev_base_lock rwlock held, nominally process context */
  557. static struct net_device_stats *enic_get_stats(struct net_device *netdev)
  558. {
  559. struct enic *enic = netdev_priv(netdev);
  560. struct net_device_stats *net_stats = &netdev->stats;
  561. struct vnic_stats *stats;
  562. spin_lock(&enic->devcmd_lock);
  563. vnic_dev_stats_dump(enic->vdev, &stats);
  564. spin_unlock(&enic->devcmd_lock);
  565. net_stats->tx_packets = stats->tx.tx_frames_ok;
  566. net_stats->tx_bytes = stats->tx.tx_bytes_ok;
  567. net_stats->tx_errors = stats->tx.tx_errors;
  568. net_stats->tx_dropped = stats->tx.tx_drops;
  569. net_stats->rx_packets = stats->rx.rx_frames_ok;
  570. net_stats->rx_bytes = stats->rx.rx_bytes_ok;
  571. net_stats->rx_errors = stats->rx.rx_errors;
  572. net_stats->multicast = stats->rx.rx_multicast_frames_ok;
  573. net_stats->rx_crc_errors = enic->rq_bad_fcs;
  574. net_stats->rx_dropped = stats->rx.rx_no_bufs;
  575. return net_stats;
  576. }
  577. static void enic_reset_mcaddrs(struct enic *enic)
  578. {
  579. enic->mc_count = 0;
  580. }
  581. static int enic_set_mac_addr(struct net_device *netdev, char *addr)
  582. {
  583. if (!is_valid_ether_addr(addr))
  584. return -EADDRNOTAVAIL;
  585. memcpy(netdev->dev_addr, addr, netdev->addr_len);
  586. return 0;
  587. }
  588. /* netif_tx_lock held, BHs disabled */
  589. static void enic_set_multicast_list(struct net_device *netdev)
  590. {
  591. struct enic *enic = netdev_priv(netdev);
  592. struct dev_mc_list *list = netdev->mc_list;
  593. int directed = 1;
  594. int multicast = (netdev->flags & IFF_MULTICAST) ? 1 : 0;
  595. int broadcast = (netdev->flags & IFF_BROADCAST) ? 1 : 0;
  596. int promisc = (netdev->flags & IFF_PROMISC) ? 1 : 0;
  597. int allmulti = (netdev->flags & IFF_ALLMULTI) ||
  598. (netdev->mc_count > ENIC_MULTICAST_PERFECT_FILTERS);
  599. u8 mc_addr[ENIC_MULTICAST_PERFECT_FILTERS][ETH_ALEN];
  600. unsigned int mc_count = netdev->mc_count;
  601. unsigned int i, j;
  602. if (mc_count > ENIC_MULTICAST_PERFECT_FILTERS)
  603. mc_count = ENIC_MULTICAST_PERFECT_FILTERS;
  604. spin_lock(&enic->devcmd_lock);
  605. vnic_dev_packet_filter(enic->vdev, directed,
  606. multicast, broadcast, promisc, allmulti);
  607. /* Is there an easier way? Trying to minimize to
  608. * calls to add/del multicast addrs. We keep the
  609. * addrs from the last call in enic->mc_addr and
  610. * look for changes to add/del.
  611. */
  612. for (i = 0; list && i < mc_count; i++) {
  613. memcpy(mc_addr[i], list->dmi_addr, ETH_ALEN);
  614. list = list->next;
  615. }
  616. for (i = 0; i < enic->mc_count; i++) {
  617. for (j = 0; j < mc_count; j++)
  618. if (compare_ether_addr(enic->mc_addr[i],
  619. mc_addr[j]) == 0)
  620. break;
  621. if (j == mc_count)
  622. enic_del_multicast_addr(enic, enic->mc_addr[i]);
  623. }
  624. for (i = 0; i < mc_count; i++) {
  625. for (j = 0; j < enic->mc_count; j++)
  626. if (compare_ether_addr(mc_addr[i],
  627. enic->mc_addr[j]) == 0)
  628. break;
  629. if (j == enic->mc_count)
  630. enic_add_multicast_addr(enic, mc_addr[i]);
  631. }
  632. /* Save the list to compare against next time
  633. */
  634. for (i = 0; i < mc_count; i++)
  635. memcpy(enic->mc_addr[i], mc_addr[i], ETH_ALEN);
  636. enic->mc_count = mc_count;
  637. spin_unlock(&enic->devcmd_lock);
  638. }
  639. /* rtnl lock is held */
  640. static void enic_vlan_rx_register(struct net_device *netdev,
  641. struct vlan_group *vlan_group)
  642. {
  643. struct enic *enic = netdev_priv(netdev);
  644. enic->vlan_group = vlan_group;
  645. }
  646. /* rtnl lock is held */
  647. static void enic_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
  648. {
  649. struct enic *enic = netdev_priv(netdev);
  650. spin_lock(&enic->devcmd_lock);
  651. enic_add_vlan(enic, vid);
  652. spin_unlock(&enic->devcmd_lock);
  653. }
  654. /* rtnl lock is held */
  655. static void enic_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
  656. {
  657. struct enic *enic = netdev_priv(netdev);
  658. spin_lock(&enic->devcmd_lock);
  659. enic_del_vlan(enic, vid);
  660. spin_unlock(&enic->devcmd_lock);
  661. }
  662. /* netif_tx_lock held, BHs disabled */
  663. static void enic_tx_timeout(struct net_device *netdev)
  664. {
  665. struct enic *enic = netdev_priv(netdev);
  666. schedule_work(&enic->reset);
  667. }
  668. static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
  669. {
  670. struct enic *enic = vnic_dev_priv(rq->vdev);
  671. if (!buf->os_buf)
  672. return;
  673. pci_unmap_single(enic->pdev, buf->dma_addr,
  674. buf->len, PCI_DMA_FROMDEVICE);
  675. dev_kfree_skb_any(buf->os_buf);
  676. }
  677. static inline struct sk_buff *enic_rq_alloc_skb(unsigned int size)
  678. {
  679. struct sk_buff *skb;
  680. skb = dev_alloc_skb(size + NET_IP_ALIGN);
  681. if (skb)
  682. skb_reserve(skb, NET_IP_ALIGN);
  683. return skb;
  684. }
  685. static int enic_rq_alloc_buf(struct vnic_rq *rq)
  686. {
  687. struct enic *enic = vnic_dev_priv(rq->vdev);
  688. struct sk_buff *skb;
  689. unsigned int len = enic->netdev->mtu + ETH_HLEN;
  690. unsigned int os_buf_index = 0;
  691. dma_addr_t dma_addr;
  692. skb = enic_rq_alloc_skb(len);
  693. if (!skb)
  694. return -ENOMEM;
  695. dma_addr = pci_map_single(enic->pdev, skb->data,
  696. len, PCI_DMA_FROMDEVICE);
  697. enic_queue_rq_desc(rq, skb, os_buf_index,
  698. dma_addr, len);
  699. return 0;
  700. }
  701. static int enic_get_skb_header(struct sk_buff *skb, void **iphdr,
  702. void **tcph, u64 *hdr_flags, void *priv)
  703. {
  704. struct cq_enet_rq_desc *cq_desc = priv;
  705. unsigned int ip_len;
  706. struct iphdr *iph;
  707. u8 type, color, eop, sop, ingress_port, vlan_stripped;
  708. u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
  709. u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
  710. u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
  711. u8 packet_error;
  712. u16 q_number, completed_index, bytes_written, vlan, checksum;
  713. u32 rss_hash;
  714. cq_enet_rq_desc_dec(cq_desc,
  715. &type, &color, &q_number, &completed_index,
  716. &ingress_port, &fcoe, &eop, &sop, &rss_type,
  717. &csum_not_calc, &rss_hash, &bytes_written,
  718. &packet_error, &vlan_stripped, &vlan, &checksum,
  719. &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
  720. &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
  721. &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
  722. &fcs_ok);
  723. if (!(ipv4 && tcp && !ipv4_fragment))
  724. return -1;
  725. skb_reset_network_header(skb);
  726. iph = ip_hdr(skb);
  727. ip_len = ip_hdrlen(skb);
  728. skb_set_transport_header(skb, ip_len);
  729. /* check if ip header and tcp header are complete */
  730. if (ntohs(iph->tot_len) < ip_len + tcp_hdrlen(skb))
  731. return -1;
  732. *hdr_flags = LRO_IPV4 | LRO_TCP;
  733. *tcph = tcp_hdr(skb);
  734. *iphdr = iph;
  735. return 0;
  736. }
  737. static void enic_rq_indicate_buf(struct vnic_rq *rq,
  738. struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
  739. int skipped, void *opaque)
  740. {
  741. struct enic *enic = vnic_dev_priv(rq->vdev);
  742. struct net_device *netdev = enic->netdev;
  743. struct sk_buff *skb;
  744. u8 type, color, eop, sop, ingress_port, vlan_stripped;
  745. u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
  746. u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
  747. u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
  748. u8 packet_error;
  749. u16 q_number, completed_index, bytes_written, vlan, checksum;
  750. u32 rss_hash;
  751. if (skipped)
  752. return;
  753. skb = buf->os_buf;
  754. prefetch(skb->data - NET_IP_ALIGN);
  755. pci_unmap_single(enic->pdev, buf->dma_addr,
  756. buf->len, PCI_DMA_FROMDEVICE);
  757. cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
  758. &type, &color, &q_number, &completed_index,
  759. &ingress_port, &fcoe, &eop, &sop, &rss_type,
  760. &csum_not_calc, &rss_hash, &bytes_written,
  761. &packet_error, &vlan_stripped, &vlan, &checksum,
  762. &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
  763. &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
  764. &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
  765. &fcs_ok);
  766. if (packet_error) {
  767. if (bytes_written > 0 && !fcs_ok)
  768. enic->rq_bad_fcs++;
  769. dev_kfree_skb_any(skb);
  770. return;
  771. }
  772. if (eop && bytes_written > 0) {
  773. /* Good receive
  774. */
  775. skb_put(skb, bytes_written);
  776. skb->protocol = eth_type_trans(skb, netdev);
  777. if (enic->csum_rx_enabled && !csum_not_calc) {
  778. skb->csum = htons(checksum);
  779. skb->ip_summed = CHECKSUM_COMPLETE;
  780. }
  781. skb->dev = netdev;
  782. if (enic->vlan_group && vlan_stripped) {
  783. if ((netdev->features & NETIF_F_LRO) && ipv4)
  784. lro_vlan_hwaccel_receive_skb(&enic->lro_mgr,
  785. skb, enic->vlan_group,
  786. vlan, cq_desc);
  787. else
  788. vlan_hwaccel_receive_skb(skb,
  789. enic->vlan_group, vlan);
  790. } else {
  791. if ((netdev->features & NETIF_F_LRO) && ipv4)
  792. lro_receive_skb(&enic->lro_mgr, skb, cq_desc);
  793. else
  794. netif_receive_skb(skb);
  795. }
  796. } else {
  797. /* Buffer overflow
  798. */
  799. dev_kfree_skb_any(skb);
  800. }
  801. }
  802. static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
  803. u8 type, u16 q_number, u16 completed_index, void *opaque)
  804. {
  805. struct enic *enic = vnic_dev_priv(vdev);
  806. vnic_rq_service(&enic->rq[q_number], cq_desc,
  807. completed_index, VNIC_RQ_RETURN_DESC,
  808. enic_rq_indicate_buf, opaque);
  809. return 0;
  810. }
  811. static void enic_rq_drop_buf(struct vnic_rq *rq,
  812. struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
  813. int skipped, void *opaque)
  814. {
  815. struct enic *enic = vnic_dev_priv(rq->vdev);
  816. struct sk_buff *skb = buf->os_buf;
  817. if (skipped)
  818. return;
  819. pci_unmap_single(enic->pdev, buf->dma_addr,
  820. buf->len, PCI_DMA_FROMDEVICE);
  821. dev_kfree_skb_any(skb);
  822. }
  823. static int enic_rq_service_drop(struct vnic_dev *vdev, struct cq_desc *cq_desc,
  824. u8 type, u16 q_number, u16 completed_index, void *opaque)
  825. {
  826. struct enic *enic = vnic_dev_priv(vdev);
  827. vnic_rq_service(&enic->rq[q_number], cq_desc,
  828. completed_index, VNIC_RQ_RETURN_DESC,
  829. enic_rq_drop_buf, opaque);
  830. return 0;
  831. }
  832. static int enic_poll(struct napi_struct *napi, int budget)
  833. {
  834. struct enic *enic = container_of(napi, struct enic, napi);
  835. struct net_device *netdev = enic->netdev;
  836. unsigned int rq_work_to_do = budget;
  837. unsigned int wq_work_to_do = -1; /* no limit */
  838. unsigned int work_done, rq_work_done, wq_work_done;
  839. /* Service RQ (first) and WQ
  840. */
  841. rq_work_done = vnic_cq_service(&enic->cq[ENIC_CQ_RQ],
  842. rq_work_to_do, enic_rq_service, NULL);
  843. wq_work_done = vnic_cq_service(&enic->cq[ENIC_CQ_WQ],
  844. wq_work_to_do, enic_wq_service, NULL);
  845. /* Accumulate intr event credits for this polling
  846. * cycle. An intr event is the completion of a
  847. * a WQ or RQ packet.
  848. */
  849. work_done = rq_work_done + wq_work_done;
  850. if (work_done > 0)
  851. vnic_intr_return_credits(&enic->intr[ENIC_INTX_WQ_RQ],
  852. work_done,
  853. 0 /* don't unmask intr */,
  854. 0 /* don't reset intr timer */);
  855. if (rq_work_done > 0) {
  856. /* Replenish RQ
  857. */
  858. vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
  859. } else {
  860. /* If no work done, flush all LROs and exit polling
  861. */
  862. if (netdev->features & NETIF_F_LRO)
  863. lro_flush_all(&enic->lro_mgr);
  864. napi_complete(napi);
  865. vnic_intr_unmask(&enic->intr[ENIC_INTX_WQ_RQ]);
  866. }
  867. return rq_work_done;
  868. }
  869. static int enic_poll_msix(struct napi_struct *napi, int budget)
  870. {
  871. struct enic *enic = container_of(napi, struct enic, napi);
  872. struct net_device *netdev = enic->netdev;
  873. unsigned int work_to_do = budget;
  874. unsigned int work_done;
  875. /* Service RQ
  876. */
  877. work_done = vnic_cq_service(&enic->cq[ENIC_CQ_RQ],
  878. work_to_do, enic_rq_service, NULL);
  879. if (work_done > 0) {
  880. /* Replenish RQ
  881. */
  882. vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
  883. /* Return intr event credits for this polling
  884. * cycle. An intr event is the completion of a
  885. * RQ packet.
  886. */
  887. vnic_intr_return_credits(&enic->intr[ENIC_MSIX_RQ],
  888. work_done,
  889. 0 /* don't unmask intr */,
  890. 0 /* don't reset intr timer */);
  891. } else {
  892. /* If no work done, flush all LROs and exit polling
  893. */
  894. if (netdev->features & NETIF_F_LRO)
  895. lro_flush_all(&enic->lro_mgr);
  896. napi_complete(napi);
  897. vnic_intr_unmask(&enic->intr[ENIC_MSIX_RQ]);
  898. }
  899. return work_done;
  900. }
  901. static void enic_notify_timer(unsigned long data)
  902. {
  903. struct enic *enic = (struct enic *)data;
  904. enic_notify_check(enic);
  905. mod_timer(&enic->notify_timer,
  906. round_jiffies(jiffies + ENIC_NOTIFY_TIMER_PERIOD));
  907. }
  908. static void enic_free_intr(struct enic *enic)
  909. {
  910. struct net_device *netdev = enic->netdev;
  911. unsigned int i;
  912. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  913. case VNIC_DEV_INTR_MODE_INTX:
  914. free_irq(enic->pdev->irq, netdev);
  915. break;
  916. case VNIC_DEV_INTR_MODE_MSI:
  917. free_irq(enic->pdev->irq, enic);
  918. break;
  919. case VNIC_DEV_INTR_MODE_MSIX:
  920. for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
  921. if (enic->msix[i].requested)
  922. free_irq(enic->msix_entry[i].vector,
  923. enic->msix[i].devid);
  924. break;
  925. default:
  926. break;
  927. }
  928. }
  929. static int enic_request_intr(struct enic *enic)
  930. {
  931. struct net_device *netdev = enic->netdev;
  932. unsigned int i;
  933. int err = 0;
  934. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  935. case VNIC_DEV_INTR_MODE_INTX:
  936. err = request_irq(enic->pdev->irq, enic_isr_legacy,
  937. IRQF_SHARED, netdev->name, netdev);
  938. break;
  939. case VNIC_DEV_INTR_MODE_MSI:
  940. err = request_irq(enic->pdev->irq, enic_isr_msi,
  941. 0, netdev->name, enic);
  942. break;
  943. case VNIC_DEV_INTR_MODE_MSIX:
  944. sprintf(enic->msix[ENIC_MSIX_RQ].devname,
  945. "%.11s-rx-0", netdev->name);
  946. enic->msix[ENIC_MSIX_RQ].isr = enic_isr_msix_rq;
  947. enic->msix[ENIC_MSIX_RQ].devid = enic;
  948. sprintf(enic->msix[ENIC_MSIX_WQ].devname,
  949. "%.11s-tx-0", netdev->name);
  950. enic->msix[ENIC_MSIX_WQ].isr = enic_isr_msix_wq;
  951. enic->msix[ENIC_MSIX_WQ].devid = enic;
  952. sprintf(enic->msix[ENIC_MSIX_ERR].devname,
  953. "%.11s-err", netdev->name);
  954. enic->msix[ENIC_MSIX_ERR].isr = enic_isr_msix_err;
  955. enic->msix[ENIC_MSIX_ERR].devid = enic;
  956. sprintf(enic->msix[ENIC_MSIX_NOTIFY].devname,
  957. "%.11s-notify", netdev->name);
  958. enic->msix[ENIC_MSIX_NOTIFY].isr = enic_isr_msix_notify;
  959. enic->msix[ENIC_MSIX_NOTIFY].devid = enic;
  960. for (i = 0; i < ARRAY_SIZE(enic->msix); i++) {
  961. err = request_irq(enic->msix_entry[i].vector,
  962. enic->msix[i].isr, 0,
  963. enic->msix[i].devname,
  964. enic->msix[i].devid);
  965. if (err) {
  966. enic_free_intr(enic);
  967. break;
  968. }
  969. enic->msix[i].requested = 1;
  970. }
  971. break;
  972. default:
  973. break;
  974. }
  975. return err;
  976. }
  977. static int enic_notify_set(struct enic *enic)
  978. {
  979. int err;
  980. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  981. case VNIC_DEV_INTR_MODE_INTX:
  982. err = vnic_dev_notify_set(enic->vdev, ENIC_INTX_NOTIFY);
  983. break;
  984. case VNIC_DEV_INTR_MODE_MSIX:
  985. err = vnic_dev_notify_set(enic->vdev, ENIC_MSIX_NOTIFY);
  986. break;
  987. default:
  988. err = vnic_dev_notify_set(enic->vdev, -1 /* no intr */);
  989. break;
  990. }
  991. return err;
  992. }
  993. static void enic_notify_timer_start(struct enic *enic)
  994. {
  995. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  996. case VNIC_DEV_INTR_MODE_MSI:
  997. mod_timer(&enic->notify_timer, jiffies);
  998. break;
  999. default:
  1000. /* Using intr for notification for INTx/MSI-X */
  1001. break;
  1002. };
  1003. }
  1004. /* rtnl lock is held, process context */
  1005. static int enic_open(struct net_device *netdev)
  1006. {
  1007. struct enic *enic = netdev_priv(netdev);
  1008. unsigned int i;
  1009. int err;
  1010. err = enic_request_intr(enic);
  1011. if (err) {
  1012. printk(KERN_ERR PFX "%s: Unable to request irq.\n",
  1013. netdev->name);
  1014. return err;
  1015. }
  1016. err = enic_notify_set(enic);
  1017. if (err) {
  1018. printk(KERN_ERR PFX
  1019. "%s: Failed to alloc notify buffer, aborting.\n",
  1020. netdev->name);
  1021. goto err_out_free_intr;
  1022. }
  1023. for (i = 0; i < enic->rq_count; i++) {
  1024. err = vnic_rq_fill(&enic->rq[i], enic_rq_alloc_buf);
  1025. if (err) {
  1026. printk(KERN_ERR PFX
  1027. "%s: Unable to alloc receive buffers.\n",
  1028. netdev->name);
  1029. goto err_out_notify_unset;
  1030. }
  1031. }
  1032. for (i = 0; i < enic->wq_count; i++)
  1033. vnic_wq_enable(&enic->wq[i]);
  1034. for (i = 0; i < enic->rq_count; i++)
  1035. vnic_rq_enable(&enic->rq[i]);
  1036. enic_add_station_addr(enic);
  1037. enic_set_multicast_list(netdev);
  1038. netif_wake_queue(netdev);
  1039. napi_enable(&enic->napi);
  1040. vnic_dev_enable(enic->vdev);
  1041. for (i = 0; i < enic->intr_count; i++)
  1042. vnic_intr_unmask(&enic->intr[i]);
  1043. enic_notify_timer_start(enic);
  1044. return 0;
  1045. err_out_notify_unset:
  1046. vnic_dev_notify_unset(enic->vdev);
  1047. err_out_free_intr:
  1048. enic_free_intr(enic);
  1049. return err;
  1050. }
  1051. /* rtnl lock is held, process context */
  1052. static int enic_stop(struct net_device *netdev)
  1053. {
  1054. struct enic *enic = netdev_priv(netdev);
  1055. unsigned int i;
  1056. int err;
  1057. del_timer_sync(&enic->notify_timer);
  1058. vnic_dev_disable(enic->vdev);
  1059. napi_disable(&enic->napi);
  1060. netif_stop_queue(netdev);
  1061. for (i = 0; i < enic->intr_count; i++)
  1062. vnic_intr_mask(&enic->intr[i]);
  1063. for (i = 0; i < enic->wq_count; i++) {
  1064. err = vnic_wq_disable(&enic->wq[i]);
  1065. if (err)
  1066. return err;
  1067. }
  1068. for (i = 0; i < enic->rq_count; i++) {
  1069. err = vnic_rq_disable(&enic->rq[i]);
  1070. if (err)
  1071. return err;
  1072. }
  1073. vnic_dev_notify_unset(enic->vdev);
  1074. enic_free_intr(enic);
  1075. (void)vnic_cq_service(&enic->cq[ENIC_CQ_RQ],
  1076. -1, enic_rq_service_drop, NULL);
  1077. (void)vnic_cq_service(&enic->cq[ENIC_CQ_WQ],
  1078. -1, enic_wq_service, NULL);
  1079. for (i = 0; i < enic->wq_count; i++)
  1080. vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
  1081. for (i = 0; i < enic->rq_count; i++)
  1082. vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
  1083. for (i = 0; i < enic->cq_count; i++)
  1084. vnic_cq_clean(&enic->cq[i]);
  1085. for (i = 0; i < enic->intr_count; i++)
  1086. vnic_intr_clean(&enic->intr[i]);
  1087. return 0;
  1088. }
  1089. static int enic_change_mtu(struct net_device *netdev, int new_mtu)
  1090. {
  1091. struct enic *enic = netdev_priv(netdev);
  1092. int running = netif_running(netdev);
  1093. if (new_mtu < ENIC_MIN_MTU || new_mtu > ENIC_MAX_MTU)
  1094. return -EINVAL;
  1095. if (running)
  1096. enic_stop(netdev);
  1097. netdev->mtu = new_mtu;
  1098. if (netdev->mtu > enic->port_mtu)
  1099. printk(KERN_WARNING PFX
  1100. "%s: interface MTU (%d) set higher "
  1101. "than port MTU (%d)\n",
  1102. netdev->name, netdev->mtu, enic->port_mtu);
  1103. if (running)
  1104. enic_open(netdev);
  1105. return 0;
  1106. }
  1107. #ifdef CONFIG_NET_POLL_CONTROLLER
  1108. static void enic_poll_controller(struct net_device *netdev)
  1109. {
  1110. struct enic *enic = netdev_priv(netdev);
  1111. struct vnic_dev *vdev = enic->vdev;
  1112. switch (vnic_dev_get_intr_mode(vdev)) {
  1113. case VNIC_DEV_INTR_MODE_MSIX:
  1114. enic_isr_msix_rq(enic->pdev->irq, enic);
  1115. enic_isr_msix_wq(enic->pdev->irq, enic);
  1116. break;
  1117. case VNIC_DEV_INTR_MODE_MSI:
  1118. enic_isr_msi(enic->pdev->irq, enic);
  1119. break;
  1120. case VNIC_DEV_INTR_MODE_INTX:
  1121. enic_isr_legacy(enic->pdev->irq, netdev);
  1122. break;
  1123. default:
  1124. break;
  1125. }
  1126. }
  1127. #endif
  1128. static int enic_dev_wait(struct vnic_dev *vdev,
  1129. int (*start)(struct vnic_dev *, int),
  1130. int (*finished)(struct vnic_dev *, int *),
  1131. int arg)
  1132. {
  1133. unsigned long time;
  1134. int done;
  1135. int err;
  1136. BUG_ON(in_interrupt());
  1137. err = start(vdev, arg);
  1138. if (err)
  1139. return err;
  1140. /* Wait for func to complete...2 seconds max
  1141. */
  1142. time = jiffies + (HZ * 2);
  1143. do {
  1144. err = finished(vdev, &done);
  1145. if (err)
  1146. return err;
  1147. if (done)
  1148. return 0;
  1149. schedule_timeout_uninterruptible(HZ / 10);
  1150. } while (time_after(time, jiffies));
  1151. return -ETIMEDOUT;
  1152. }
  1153. static int enic_dev_open(struct enic *enic)
  1154. {
  1155. int err;
  1156. err = enic_dev_wait(enic->vdev, vnic_dev_open,
  1157. vnic_dev_open_done, 0);
  1158. if (err)
  1159. printk(KERN_ERR PFX
  1160. "vNIC device open failed, err %d.\n", err);
  1161. return err;
  1162. }
  1163. static int enic_dev_soft_reset(struct enic *enic)
  1164. {
  1165. int err;
  1166. err = enic_dev_wait(enic->vdev, vnic_dev_soft_reset,
  1167. vnic_dev_soft_reset_done, 0);
  1168. if (err)
  1169. printk(KERN_ERR PFX
  1170. "vNIC soft reset failed, err %d.\n", err);
  1171. return err;
  1172. }
  1173. static int enic_set_niccfg(struct enic *enic)
  1174. {
  1175. const u8 rss_default_cpu = 0;
  1176. const u8 rss_hash_type = 0;
  1177. const u8 rss_hash_bits = 0;
  1178. const u8 rss_base_cpu = 0;
  1179. const u8 rss_enable = 0;
  1180. const u8 tso_ipid_split_en = 0;
  1181. const u8 ig_vlan_strip_en = 1;
  1182. /* Enable VLAN tag stripping. RSS not enabled (yet).
  1183. */
  1184. return enic_set_nic_cfg(enic,
  1185. rss_default_cpu, rss_hash_type,
  1186. rss_hash_bits, rss_base_cpu,
  1187. rss_enable, tso_ipid_split_en,
  1188. ig_vlan_strip_en);
  1189. }
  1190. static void enic_reset(struct work_struct *work)
  1191. {
  1192. struct enic *enic = container_of(work, struct enic, reset);
  1193. if (!netif_running(enic->netdev))
  1194. return;
  1195. rtnl_lock();
  1196. spin_lock(&enic->devcmd_lock);
  1197. vnic_dev_hang_notify(enic->vdev);
  1198. spin_unlock(&enic->devcmd_lock);
  1199. enic_stop(enic->netdev);
  1200. enic_dev_soft_reset(enic);
  1201. vnic_dev_init(enic->vdev, 0);
  1202. enic_reset_mcaddrs(enic);
  1203. enic_init_vnic_resources(enic);
  1204. enic_set_niccfg(enic);
  1205. enic_open(enic->netdev);
  1206. rtnl_unlock();
  1207. }
  1208. static int enic_set_intr_mode(struct enic *enic)
  1209. {
  1210. unsigned int n = ARRAY_SIZE(enic->rq);
  1211. unsigned int m = ARRAY_SIZE(enic->wq);
  1212. unsigned int i;
  1213. /* Set interrupt mode (INTx, MSI, MSI-X) depending
  1214. * system capabilities.
  1215. *
  1216. * Try MSI-X first
  1217. *
  1218. * We need n RQs, m WQs, n+m CQs, and n+m+2 INTRs
  1219. * (the second to last INTR is used for WQ/RQ errors)
  1220. * (the last INTR is used for notifications)
  1221. */
  1222. BUG_ON(ARRAY_SIZE(enic->msix_entry) < n + m + 2);
  1223. for (i = 0; i < n + m + 2; i++)
  1224. enic->msix_entry[i].entry = i;
  1225. if (enic->config.intr_mode < 1 &&
  1226. enic->rq_count >= n &&
  1227. enic->wq_count >= m &&
  1228. enic->cq_count >= n + m &&
  1229. enic->intr_count >= n + m + 2 &&
  1230. !pci_enable_msix(enic->pdev, enic->msix_entry, n + m + 2)) {
  1231. enic->rq_count = n;
  1232. enic->wq_count = m;
  1233. enic->cq_count = n + m;
  1234. enic->intr_count = n + m + 2;
  1235. vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSIX);
  1236. return 0;
  1237. }
  1238. /* Next try MSI
  1239. *
  1240. * We need 1 RQ, 1 WQ, 2 CQs, and 1 INTR
  1241. */
  1242. if (enic->config.intr_mode < 2 &&
  1243. enic->rq_count >= 1 &&
  1244. enic->wq_count >= 1 &&
  1245. enic->cq_count >= 2 &&
  1246. enic->intr_count >= 1 &&
  1247. !pci_enable_msi(enic->pdev)) {
  1248. enic->rq_count = 1;
  1249. enic->wq_count = 1;
  1250. enic->cq_count = 2;
  1251. enic->intr_count = 1;
  1252. vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSI);
  1253. return 0;
  1254. }
  1255. /* Next try INTx
  1256. *
  1257. * We need 1 RQ, 1 WQ, 2 CQs, and 3 INTRs
  1258. * (the first INTR is used for WQ/RQ)
  1259. * (the second INTR is used for WQ/RQ errors)
  1260. * (the last INTR is used for notifications)
  1261. */
  1262. if (enic->config.intr_mode < 3 &&
  1263. enic->rq_count >= 1 &&
  1264. enic->wq_count >= 1 &&
  1265. enic->cq_count >= 2 &&
  1266. enic->intr_count >= 3) {
  1267. enic->rq_count = 1;
  1268. enic->wq_count = 1;
  1269. enic->cq_count = 2;
  1270. enic->intr_count = 3;
  1271. vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_INTX);
  1272. return 0;
  1273. }
  1274. vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
  1275. return -EINVAL;
  1276. }
  1277. static void enic_clear_intr_mode(struct enic *enic)
  1278. {
  1279. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  1280. case VNIC_DEV_INTR_MODE_MSIX:
  1281. pci_disable_msix(enic->pdev);
  1282. break;
  1283. case VNIC_DEV_INTR_MODE_MSI:
  1284. pci_disable_msi(enic->pdev);
  1285. break;
  1286. default:
  1287. break;
  1288. }
  1289. vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
  1290. }
  1291. static void enic_iounmap(struct enic *enic)
  1292. {
  1293. if (enic->bar0.vaddr)
  1294. iounmap(enic->bar0.vaddr);
  1295. }
  1296. static const struct net_device_ops enic_netdev_ops = {
  1297. .ndo_open = enic_open,
  1298. .ndo_stop = enic_stop,
  1299. .ndo_start_xmit = enic_hard_start_xmit,
  1300. .ndo_get_stats = enic_get_stats,
  1301. .ndo_validate_addr = eth_validate_addr,
  1302. .ndo_set_mac_address = eth_mac_addr,
  1303. .ndo_set_multicast_list = enic_set_multicast_list,
  1304. .ndo_change_mtu = enic_change_mtu,
  1305. .ndo_vlan_rx_register = enic_vlan_rx_register,
  1306. .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
  1307. .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
  1308. .ndo_tx_timeout = enic_tx_timeout,
  1309. #ifdef CONFIG_NET_POLL_CONTROLLER
  1310. .ndo_poll_controller = enic_poll_controller,
  1311. #endif
  1312. };
  1313. static int __devinit enic_probe(struct pci_dev *pdev,
  1314. const struct pci_device_id *ent)
  1315. {
  1316. struct net_device *netdev;
  1317. struct enic *enic;
  1318. int using_dac = 0;
  1319. unsigned int i;
  1320. int err;
  1321. /* Allocate net device structure and initialize. Private
  1322. * instance data is initialized to zero.
  1323. */
  1324. netdev = alloc_etherdev(sizeof(struct enic));
  1325. if (!netdev) {
  1326. printk(KERN_ERR PFX "Etherdev alloc failed, aborting.\n");
  1327. return -ENOMEM;
  1328. }
  1329. pci_set_drvdata(pdev, netdev);
  1330. SET_NETDEV_DEV(netdev, &pdev->dev);
  1331. enic = netdev_priv(netdev);
  1332. enic->netdev = netdev;
  1333. enic->pdev = pdev;
  1334. /* Setup PCI resources
  1335. */
  1336. err = pci_enable_device(pdev);
  1337. if (err) {
  1338. printk(KERN_ERR PFX
  1339. "Cannot enable PCI device, aborting.\n");
  1340. goto err_out_free_netdev;
  1341. }
  1342. err = pci_request_regions(pdev, DRV_NAME);
  1343. if (err) {
  1344. printk(KERN_ERR PFX
  1345. "Cannot request PCI regions, aborting.\n");
  1346. goto err_out_disable_device;
  1347. }
  1348. pci_set_master(pdev);
  1349. /* Query PCI controller on system for DMA addressing
  1350. * limitation for the device. Try 40-bit first, and
  1351. * fail to 32-bit.
  1352. */
  1353. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40));
  1354. if (err) {
  1355. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1356. if (err) {
  1357. printk(KERN_ERR PFX
  1358. "No usable DMA configuration, aborting.\n");
  1359. goto err_out_release_regions;
  1360. }
  1361. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  1362. if (err) {
  1363. printk(KERN_ERR PFX
  1364. "Unable to obtain 32-bit DMA "
  1365. "for consistent allocations, aborting.\n");
  1366. goto err_out_release_regions;
  1367. }
  1368. } else {
  1369. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
  1370. if (err) {
  1371. printk(KERN_ERR PFX
  1372. "Unable to obtain 40-bit DMA "
  1373. "for consistent allocations, aborting.\n");
  1374. goto err_out_release_regions;
  1375. }
  1376. using_dac = 1;
  1377. }
  1378. /* Map vNIC resources from BAR0
  1379. */
  1380. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  1381. printk(KERN_ERR PFX
  1382. "BAR0 not memory-map'able, aborting.\n");
  1383. err = -ENODEV;
  1384. goto err_out_release_regions;
  1385. }
  1386. enic->bar0.vaddr = pci_iomap(pdev, 0, enic->bar0.len);
  1387. enic->bar0.bus_addr = pci_resource_start(pdev, 0);
  1388. enic->bar0.len = pci_resource_len(pdev, 0);
  1389. if (!enic->bar0.vaddr) {
  1390. printk(KERN_ERR PFX
  1391. "Cannot memory-map BAR0 res hdr, aborting.\n");
  1392. err = -ENODEV;
  1393. goto err_out_release_regions;
  1394. }
  1395. /* Register vNIC device
  1396. */
  1397. enic->vdev = vnic_dev_register(NULL, enic, pdev, &enic->bar0);
  1398. if (!enic->vdev) {
  1399. printk(KERN_ERR PFX
  1400. "vNIC registration failed, aborting.\n");
  1401. err = -ENODEV;
  1402. goto err_out_iounmap;
  1403. }
  1404. /* Issue device open to get device in known state
  1405. */
  1406. err = enic_dev_open(enic);
  1407. if (err) {
  1408. printk(KERN_ERR PFX
  1409. "vNIC dev open failed, aborting.\n");
  1410. goto err_out_vnic_unregister;
  1411. }
  1412. /* Issue device init to initialize the vnic-to-switch link.
  1413. * We'll start with carrier off and wait for link UP
  1414. * notification later to turn on carrier. We don't need
  1415. * to wait here for the vnic-to-switch link initialization
  1416. * to complete; link UP notification is the indication that
  1417. * the process is complete.
  1418. */
  1419. netif_carrier_off(netdev);
  1420. err = vnic_dev_init(enic->vdev, 0);
  1421. if (err) {
  1422. printk(KERN_ERR PFX
  1423. "vNIC dev init failed, aborting.\n");
  1424. goto err_out_dev_close;
  1425. }
  1426. /* Get vNIC configuration
  1427. */
  1428. err = enic_get_vnic_config(enic);
  1429. if (err) {
  1430. printk(KERN_ERR PFX
  1431. "Get vNIC configuration failed, aborting.\n");
  1432. goto err_out_dev_close;
  1433. }
  1434. /* Get available resource counts
  1435. */
  1436. enic_get_res_counts(enic);
  1437. /* Set interrupt mode based on resource counts and system
  1438. * capabilities
  1439. */
  1440. err = enic_set_intr_mode(enic);
  1441. if (err) {
  1442. printk(KERN_ERR PFX
  1443. "Failed to set intr mode, aborting.\n");
  1444. goto err_out_dev_close;
  1445. }
  1446. /* Allocate and configure vNIC resources
  1447. */
  1448. err = enic_alloc_vnic_resources(enic);
  1449. if (err) {
  1450. printk(KERN_ERR PFX
  1451. "Failed to alloc vNIC resources, aborting.\n");
  1452. goto err_out_free_vnic_resources;
  1453. }
  1454. enic_init_vnic_resources(enic);
  1455. err = enic_set_niccfg(enic);
  1456. if (err) {
  1457. printk(KERN_ERR PFX
  1458. "Failed to config nic, aborting.\n");
  1459. goto err_out_free_vnic_resources;
  1460. }
  1461. /* Setup notification timer, HW reset task, and locks
  1462. */
  1463. init_timer(&enic->notify_timer);
  1464. enic->notify_timer.function = enic_notify_timer;
  1465. enic->notify_timer.data = (unsigned long)enic;
  1466. INIT_WORK(&enic->reset, enic_reset);
  1467. for (i = 0; i < enic->wq_count; i++)
  1468. spin_lock_init(&enic->wq_lock[i]);
  1469. spin_lock_init(&enic->devcmd_lock);
  1470. /* Register net device
  1471. */
  1472. enic->port_mtu = enic->config.mtu;
  1473. (void)enic_change_mtu(netdev, enic->port_mtu);
  1474. err = enic_set_mac_addr(netdev, enic->mac_addr);
  1475. if (err) {
  1476. printk(KERN_ERR PFX
  1477. "Invalid MAC address, aborting.\n");
  1478. goto err_out_free_vnic_resources;
  1479. }
  1480. netdev->netdev_ops = &enic_netdev_ops;
  1481. netdev->watchdog_timeo = 2 * HZ;
  1482. netdev->ethtool_ops = &enic_ethtool_ops;
  1483. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  1484. default:
  1485. netif_napi_add(netdev, &enic->napi, enic_poll, 64);
  1486. break;
  1487. case VNIC_DEV_INTR_MODE_MSIX:
  1488. netif_napi_add(netdev, &enic->napi, enic_poll_msix, 64);
  1489. break;
  1490. }
  1491. netdev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  1492. if (ENIC_SETTING(enic, TXCSUM))
  1493. netdev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
  1494. if (ENIC_SETTING(enic, TSO))
  1495. netdev->features |= NETIF_F_TSO |
  1496. NETIF_F_TSO6 | NETIF_F_TSO_ECN;
  1497. if (ENIC_SETTING(enic, LRO))
  1498. netdev->features |= NETIF_F_LRO;
  1499. if (using_dac)
  1500. netdev->features |= NETIF_F_HIGHDMA;
  1501. enic->csum_rx_enabled = ENIC_SETTING(enic, RXCSUM);
  1502. enic->lro_mgr.max_aggr = ENIC_LRO_MAX_AGGR;
  1503. enic->lro_mgr.max_desc = ENIC_LRO_MAX_DESC;
  1504. enic->lro_mgr.lro_arr = enic->lro_desc;
  1505. enic->lro_mgr.get_skb_header = enic_get_skb_header;
  1506. enic->lro_mgr.features = LRO_F_NAPI | LRO_F_EXTRACT_VLAN_ID;
  1507. enic->lro_mgr.dev = netdev;
  1508. enic->lro_mgr.ip_summed = CHECKSUM_COMPLETE;
  1509. enic->lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
  1510. err = register_netdev(netdev);
  1511. if (err) {
  1512. printk(KERN_ERR PFX
  1513. "Cannot register net device, aborting.\n");
  1514. goto err_out_free_vnic_resources;
  1515. }
  1516. return 0;
  1517. err_out_free_vnic_resources:
  1518. enic_free_vnic_resources(enic);
  1519. err_out_dev_close:
  1520. vnic_dev_close(enic->vdev);
  1521. err_out_vnic_unregister:
  1522. enic_clear_intr_mode(enic);
  1523. vnic_dev_unregister(enic->vdev);
  1524. err_out_iounmap:
  1525. enic_iounmap(enic);
  1526. err_out_release_regions:
  1527. pci_release_regions(pdev);
  1528. err_out_disable_device:
  1529. pci_disable_device(pdev);
  1530. err_out_free_netdev:
  1531. pci_set_drvdata(pdev, NULL);
  1532. free_netdev(netdev);
  1533. return err;
  1534. }
  1535. static void __devexit enic_remove(struct pci_dev *pdev)
  1536. {
  1537. struct net_device *netdev = pci_get_drvdata(pdev);
  1538. if (netdev) {
  1539. struct enic *enic = netdev_priv(netdev);
  1540. flush_scheduled_work();
  1541. unregister_netdev(netdev);
  1542. enic_free_vnic_resources(enic);
  1543. vnic_dev_close(enic->vdev);
  1544. enic_clear_intr_mode(enic);
  1545. vnic_dev_unregister(enic->vdev);
  1546. enic_iounmap(enic);
  1547. pci_release_regions(pdev);
  1548. pci_disable_device(pdev);
  1549. pci_set_drvdata(pdev, NULL);
  1550. free_netdev(netdev);
  1551. }
  1552. }
  1553. static struct pci_driver enic_driver = {
  1554. .name = DRV_NAME,
  1555. .id_table = enic_id_table,
  1556. .probe = enic_probe,
  1557. .remove = __devexit_p(enic_remove),
  1558. };
  1559. static int __init enic_init_module(void)
  1560. {
  1561. printk(KERN_INFO PFX "%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
  1562. return pci_register_driver(&enic_driver);
  1563. }
  1564. static void __exit enic_cleanup_module(void)
  1565. {
  1566. pci_unregister_driver(&enic_driver);
  1567. }
  1568. module_init(enic_init_module);
  1569. module_exit(enic_cleanup_module);