enic_main.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948
  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. spin_unlock_irqrestore(&enic->wq_lock[0], flags);
  553. return NETDEV_TX_OK;
  554. }
  555. /* dev_base_lock rwlock held, nominally process context */
  556. static struct net_device_stats *enic_get_stats(struct net_device *netdev)
  557. {
  558. struct enic *enic = netdev_priv(netdev);
  559. struct net_device_stats *net_stats = &netdev->stats;
  560. struct vnic_stats *stats;
  561. spin_lock(&enic->devcmd_lock);
  562. vnic_dev_stats_dump(enic->vdev, &stats);
  563. spin_unlock(&enic->devcmd_lock);
  564. net_stats->tx_packets = stats->tx.tx_frames_ok;
  565. net_stats->tx_bytes = stats->tx.tx_bytes_ok;
  566. net_stats->tx_errors = stats->tx.tx_errors;
  567. net_stats->tx_dropped = stats->tx.tx_drops;
  568. net_stats->rx_packets = stats->rx.rx_frames_ok;
  569. net_stats->rx_bytes = stats->rx.rx_bytes_ok;
  570. net_stats->rx_errors = stats->rx.rx_errors;
  571. net_stats->multicast = stats->rx.rx_multicast_frames_ok;
  572. net_stats->rx_crc_errors = enic->rq_bad_fcs;
  573. net_stats->rx_dropped = stats->rx.rx_no_bufs;
  574. return net_stats;
  575. }
  576. static void enic_reset_mcaddrs(struct enic *enic)
  577. {
  578. enic->mc_count = 0;
  579. }
  580. static int enic_set_mac_addr(struct net_device *netdev, char *addr)
  581. {
  582. if (!is_valid_ether_addr(addr))
  583. return -EADDRNOTAVAIL;
  584. memcpy(netdev->dev_addr, addr, netdev->addr_len);
  585. return 0;
  586. }
  587. /* netif_tx_lock held, BHs disabled */
  588. static void enic_set_multicast_list(struct net_device *netdev)
  589. {
  590. struct enic *enic = netdev_priv(netdev);
  591. struct dev_mc_list *list = netdev->mc_list;
  592. int directed = 1;
  593. int multicast = (netdev->flags & IFF_MULTICAST) ? 1 : 0;
  594. int broadcast = (netdev->flags & IFF_BROADCAST) ? 1 : 0;
  595. int promisc = (netdev->flags & IFF_PROMISC) ? 1 : 0;
  596. int allmulti = (netdev->flags & IFF_ALLMULTI) ||
  597. (netdev->mc_count > ENIC_MULTICAST_PERFECT_FILTERS);
  598. u8 mc_addr[ENIC_MULTICAST_PERFECT_FILTERS][ETH_ALEN];
  599. unsigned int mc_count = netdev->mc_count;
  600. unsigned int i, j;
  601. if (mc_count > ENIC_MULTICAST_PERFECT_FILTERS)
  602. mc_count = ENIC_MULTICAST_PERFECT_FILTERS;
  603. spin_lock(&enic->devcmd_lock);
  604. vnic_dev_packet_filter(enic->vdev, directed,
  605. multicast, broadcast, promisc, allmulti);
  606. /* Is there an easier way? Trying to minimize to
  607. * calls to add/del multicast addrs. We keep the
  608. * addrs from the last call in enic->mc_addr and
  609. * look for changes to add/del.
  610. */
  611. for (i = 0; list && i < mc_count; i++) {
  612. memcpy(mc_addr[i], list->dmi_addr, ETH_ALEN);
  613. list = list->next;
  614. }
  615. for (i = 0; i < enic->mc_count; i++) {
  616. for (j = 0; j < mc_count; j++)
  617. if (compare_ether_addr(enic->mc_addr[i],
  618. mc_addr[j]) == 0)
  619. break;
  620. if (j == mc_count)
  621. enic_del_multicast_addr(enic, enic->mc_addr[i]);
  622. }
  623. for (i = 0; i < mc_count; i++) {
  624. for (j = 0; j < enic->mc_count; j++)
  625. if (compare_ether_addr(mc_addr[i],
  626. enic->mc_addr[j]) == 0)
  627. break;
  628. if (j == enic->mc_count)
  629. enic_add_multicast_addr(enic, mc_addr[i]);
  630. }
  631. /* Save the list to compare against next time
  632. */
  633. for (i = 0; i < mc_count; i++)
  634. memcpy(enic->mc_addr[i], mc_addr[i], ETH_ALEN);
  635. enic->mc_count = mc_count;
  636. spin_unlock(&enic->devcmd_lock);
  637. }
  638. /* rtnl lock is held */
  639. static void enic_vlan_rx_register(struct net_device *netdev,
  640. struct vlan_group *vlan_group)
  641. {
  642. struct enic *enic = netdev_priv(netdev);
  643. enic->vlan_group = vlan_group;
  644. }
  645. /* rtnl lock is held */
  646. static void enic_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
  647. {
  648. struct enic *enic = netdev_priv(netdev);
  649. spin_lock(&enic->devcmd_lock);
  650. enic_add_vlan(enic, vid);
  651. spin_unlock(&enic->devcmd_lock);
  652. }
  653. /* rtnl lock is held */
  654. static void enic_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
  655. {
  656. struct enic *enic = netdev_priv(netdev);
  657. spin_lock(&enic->devcmd_lock);
  658. enic_del_vlan(enic, vid);
  659. spin_unlock(&enic->devcmd_lock);
  660. }
  661. /* netif_tx_lock held, BHs disabled */
  662. static void enic_tx_timeout(struct net_device *netdev)
  663. {
  664. struct enic *enic = netdev_priv(netdev);
  665. schedule_work(&enic->reset);
  666. }
  667. static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
  668. {
  669. struct enic *enic = vnic_dev_priv(rq->vdev);
  670. if (!buf->os_buf)
  671. return;
  672. pci_unmap_single(enic->pdev, buf->dma_addr,
  673. buf->len, PCI_DMA_FROMDEVICE);
  674. dev_kfree_skb_any(buf->os_buf);
  675. }
  676. static inline struct sk_buff *enic_rq_alloc_skb(unsigned int size)
  677. {
  678. struct sk_buff *skb;
  679. skb = dev_alloc_skb(size + NET_IP_ALIGN);
  680. if (skb)
  681. skb_reserve(skb, NET_IP_ALIGN);
  682. return skb;
  683. }
  684. static int enic_rq_alloc_buf(struct vnic_rq *rq)
  685. {
  686. struct enic *enic = vnic_dev_priv(rq->vdev);
  687. struct sk_buff *skb;
  688. unsigned int len = enic->netdev->mtu + ETH_HLEN;
  689. unsigned int os_buf_index = 0;
  690. dma_addr_t dma_addr;
  691. skb = enic_rq_alloc_skb(len);
  692. if (!skb)
  693. return -ENOMEM;
  694. dma_addr = pci_map_single(enic->pdev, skb->data,
  695. len, PCI_DMA_FROMDEVICE);
  696. enic_queue_rq_desc(rq, skb, os_buf_index,
  697. dma_addr, len);
  698. return 0;
  699. }
  700. static int enic_get_skb_header(struct sk_buff *skb, void **iphdr,
  701. void **tcph, u64 *hdr_flags, void *priv)
  702. {
  703. struct cq_enet_rq_desc *cq_desc = priv;
  704. unsigned int ip_len;
  705. struct iphdr *iph;
  706. u8 type, color, eop, sop, ingress_port, vlan_stripped;
  707. u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
  708. u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
  709. u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
  710. u8 packet_error;
  711. u16 q_number, completed_index, bytes_written, vlan, checksum;
  712. u32 rss_hash;
  713. cq_enet_rq_desc_dec(cq_desc,
  714. &type, &color, &q_number, &completed_index,
  715. &ingress_port, &fcoe, &eop, &sop, &rss_type,
  716. &csum_not_calc, &rss_hash, &bytes_written,
  717. &packet_error, &vlan_stripped, &vlan, &checksum,
  718. &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
  719. &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
  720. &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
  721. &fcs_ok);
  722. if (!(ipv4 && tcp && !ipv4_fragment))
  723. return -1;
  724. skb_reset_network_header(skb);
  725. iph = ip_hdr(skb);
  726. ip_len = ip_hdrlen(skb);
  727. skb_set_transport_header(skb, ip_len);
  728. /* check if ip header and tcp header are complete */
  729. if (ntohs(iph->tot_len) < ip_len + tcp_hdrlen(skb))
  730. return -1;
  731. *hdr_flags = LRO_IPV4 | LRO_TCP;
  732. *tcph = tcp_hdr(skb);
  733. *iphdr = iph;
  734. return 0;
  735. }
  736. static void enic_rq_indicate_buf(struct vnic_rq *rq,
  737. struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
  738. int skipped, void *opaque)
  739. {
  740. struct enic *enic = vnic_dev_priv(rq->vdev);
  741. struct net_device *netdev = enic->netdev;
  742. struct sk_buff *skb;
  743. u8 type, color, eop, sop, ingress_port, vlan_stripped;
  744. u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
  745. u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
  746. u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
  747. u8 packet_error;
  748. u16 q_number, completed_index, bytes_written, vlan, checksum;
  749. u32 rss_hash;
  750. if (skipped)
  751. return;
  752. skb = buf->os_buf;
  753. prefetch(skb->data - NET_IP_ALIGN);
  754. pci_unmap_single(enic->pdev, buf->dma_addr,
  755. buf->len, PCI_DMA_FROMDEVICE);
  756. cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
  757. &type, &color, &q_number, &completed_index,
  758. &ingress_port, &fcoe, &eop, &sop, &rss_type,
  759. &csum_not_calc, &rss_hash, &bytes_written,
  760. &packet_error, &vlan_stripped, &vlan, &checksum,
  761. &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
  762. &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
  763. &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
  764. &fcs_ok);
  765. if (packet_error) {
  766. if (bytes_written > 0 && !fcs_ok)
  767. enic->rq_bad_fcs++;
  768. dev_kfree_skb_any(skb);
  769. return;
  770. }
  771. if (eop && bytes_written > 0) {
  772. /* Good receive
  773. */
  774. skb_put(skb, bytes_written);
  775. skb->protocol = eth_type_trans(skb, netdev);
  776. if (enic->csum_rx_enabled && !csum_not_calc) {
  777. skb->csum = htons(checksum);
  778. skb->ip_summed = CHECKSUM_COMPLETE;
  779. }
  780. skb->dev = netdev;
  781. if (enic->vlan_group && vlan_stripped) {
  782. if ((netdev->features & NETIF_F_LRO) && ipv4)
  783. lro_vlan_hwaccel_receive_skb(&enic->lro_mgr,
  784. skb, enic->vlan_group,
  785. vlan, cq_desc);
  786. else
  787. vlan_hwaccel_receive_skb(skb,
  788. enic->vlan_group, vlan);
  789. } else {
  790. if ((netdev->features & NETIF_F_LRO) && ipv4)
  791. lro_receive_skb(&enic->lro_mgr, skb, cq_desc);
  792. else
  793. netif_receive_skb(skb);
  794. }
  795. } else {
  796. /* Buffer overflow
  797. */
  798. dev_kfree_skb_any(skb);
  799. }
  800. }
  801. static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
  802. u8 type, u16 q_number, u16 completed_index, void *opaque)
  803. {
  804. struct enic *enic = vnic_dev_priv(vdev);
  805. vnic_rq_service(&enic->rq[q_number], cq_desc,
  806. completed_index, VNIC_RQ_RETURN_DESC,
  807. enic_rq_indicate_buf, opaque);
  808. return 0;
  809. }
  810. static void enic_rq_drop_buf(struct vnic_rq *rq,
  811. struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
  812. int skipped, void *opaque)
  813. {
  814. struct enic *enic = vnic_dev_priv(rq->vdev);
  815. struct sk_buff *skb = buf->os_buf;
  816. if (skipped)
  817. return;
  818. pci_unmap_single(enic->pdev, buf->dma_addr,
  819. buf->len, PCI_DMA_FROMDEVICE);
  820. dev_kfree_skb_any(skb);
  821. }
  822. static int enic_rq_service_drop(struct vnic_dev *vdev, struct cq_desc *cq_desc,
  823. u8 type, u16 q_number, u16 completed_index, void *opaque)
  824. {
  825. struct enic *enic = vnic_dev_priv(vdev);
  826. vnic_rq_service(&enic->rq[q_number], cq_desc,
  827. completed_index, VNIC_RQ_RETURN_DESC,
  828. enic_rq_drop_buf, opaque);
  829. return 0;
  830. }
  831. static int enic_poll(struct napi_struct *napi, int budget)
  832. {
  833. struct enic *enic = container_of(napi, struct enic, napi);
  834. struct net_device *netdev = enic->netdev;
  835. unsigned int rq_work_to_do = budget;
  836. unsigned int wq_work_to_do = -1; /* no limit */
  837. unsigned int work_done, rq_work_done, wq_work_done;
  838. /* Service RQ (first) and WQ
  839. */
  840. rq_work_done = vnic_cq_service(&enic->cq[ENIC_CQ_RQ],
  841. rq_work_to_do, enic_rq_service, NULL);
  842. wq_work_done = vnic_cq_service(&enic->cq[ENIC_CQ_WQ],
  843. wq_work_to_do, enic_wq_service, NULL);
  844. /* Accumulate intr event credits for this polling
  845. * cycle. An intr event is the completion of a
  846. * a WQ or RQ packet.
  847. */
  848. work_done = rq_work_done + wq_work_done;
  849. if (work_done > 0)
  850. vnic_intr_return_credits(&enic->intr[ENIC_INTX_WQ_RQ],
  851. work_done,
  852. 0 /* don't unmask intr */,
  853. 0 /* don't reset intr timer */);
  854. if (rq_work_done > 0) {
  855. /* Replenish RQ
  856. */
  857. vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
  858. } else {
  859. /* If no work done, flush all LROs and exit polling
  860. */
  861. if (netdev->features & NETIF_F_LRO)
  862. lro_flush_all(&enic->lro_mgr);
  863. napi_complete(napi);
  864. vnic_intr_unmask(&enic->intr[ENIC_INTX_WQ_RQ]);
  865. }
  866. return rq_work_done;
  867. }
  868. static int enic_poll_msix(struct napi_struct *napi, int budget)
  869. {
  870. struct enic *enic = container_of(napi, struct enic, napi);
  871. struct net_device *netdev = enic->netdev;
  872. unsigned int work_to_do = budget;
  873. unsigned int work_done;
  874. /* Service RQ
  875. */
  876. work_done = vnic_cq_service(&enic->cq[ENIC_CQ_RQ],
  877. work_to_do, enic_rq_service, NULL);
  878. if (work_done > 0) {
  879. /* Replenish RQ
  880. */
  881. vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
  882. /* Return intr event credits for this polling
  883. * cycle. An intr event is the completion of a
  884. * RQ packet.
  885. */
  886. vnic_intr_return_credits(&enic->intr[ENIC_MSIX_RQ],
  887. work_done,
  888. 0 /* don't unmask intr */,
  889. 0 /* don't reset intr timer */);
  890. } else {
  891. /* If no work done, flush all LROs and exit polling
  892. */
  893. if (netdev->features & NETIF_F_LRO)
  894. lro_flush_all(&enic->lro_mgr);
  895. napi_complete(napi);
  896. vnic_intr_unmask(&enic->intr[ENIC_MSIX_RQ]);
  897. }
  898. return work_done;
  899. }
  900. static void enic_notify_timer(unsigned long data)
  901. {
  902. struct enic *enic = (struct enic *)data;
  903. enic_notify_check(enic);
  904. mod_timer(&enic->notify_timer,
  905. round_jiffies(jiffies + ENIC_NOTIFY_TIMER_PERIOD));
  906. }
  907. static void enic_free_intr(struct enic *enic)
  908. {
  909. struct net_device *netdev = enic->netdev;
  910. unsigned int i;
  911. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  912. case VNIC_DEV_INTR_MODE_INTX:
  913. free_irq(enic->pdev->irq, netdev);
  914. break;
  915. case VNIC_DEV_INTR_MODE_MSI:
  916. free_irq(enic->pdev->irq, enic);
  917. break;
  918. case VNIC_DEV_INTR_MODE_MSIX:
  919. for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
  920. if (enic->msix[i].requested)
  921. free_irq(enic->msix_entry[i].vector,
  922. enic->msix[i].devid);
  923. break;
  924. default:
  925. break;
  926. }
  927. }
  928. static int enic_request_intr(struct enic *enic)
  929. {
  930. struct net_device *netdev = enic->netdev;
  931. unsigned int i;
  932. int err = 0;
  933. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  934. case VNIC_DEV_INTR_MODE_INTX:
  935. err = request_irq(enic->pdev->irq, enic_isr_legacy,
  936. IRQF_SHARED, netdev->name, netdev);
  937. break;
  938. case VNIC_DEV_INTR_MODE_MSI:
  939. err = request_irq(enic->pdev->irq, enic_isr_msi,
  940. 0, netdev->name, enic);
  941. break;
  942. case VNIC_DEV_INTR_MODE_MSIX:
  943. sprintf(enic->msix[ENIC_MSIX_RQ].devname,
  944. "%.11s-rx-0", netdev->name);
  945. enic->msix[ENIC_MSIX_RQ].isr = enic_isr_msix_rq;
  946. enic->msix[ENIC_MSIX_RQ].devid = enic;
  947. sprintf(enic->msix[ENIC_MSIX_WQ].devname,
  948. "%.11s-tx-0", netdev->name);
  949. enic->msix[ENIC_MSIX_WQ].isr = enic_isr_msix_wq;
  950. enic->msix[ENIC_MSIX_WQ].devid = enic;
  951. sprintf(enic->msix[ENIC_MSIX_ERR].devname,
  952. "%.11s-err", netdev->name);
  953. enic->msix[ENIC_MSIX_ERR].isr = enic_isr_msix_err;
  954. enic->msix[ENIC_MSIX_ERR].devid = enic;
  955. sprintf(enic->msix[ENIC_MSIX_NOTIFY].devname,
  956. "%.11s-notify", netdev->name);
  957. enic->msix[ENIC_MSIX_NOTIFY].isr = enic_isr_msix_notify;
  958. enic->msix[ENIC_MSIX_NOTIFY].devid = enic;
  959. for (i = 0; i < ARRAY_SIZE(enic->msix); i++) {
  960. err = request_irq(enic->msix_entry[i].vector,
  961. enic->msix[i].isr, 0,
  962. enic->msix[i].devname,
  963. enic->msix[i].devid);
  964. if (err) {
  965. enic_free_intr(enic);
  966. break;
  967. }
  968. enic->msix[i].requested = 1;
  969. }
  970. break;
  971. default:
  972. break;
  973. }
  974. return err;
  975. }
  976. static int enic_notify_set(struct enic *enic)
  977. {
  978. int err;
  979. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  980. case VNIC_DEV_INTR_MODE_INTX:
  981. err = vnic_dev_notify_set(enic->vdev, ENIC_INTX_NOTIFY);
  982. break;
  983. case VNIC_DEV_INTR_MODE_MSIX:
  984. err = vnic_dev_notify_set(enic->vdev, ENIC_MSIX_NOTIFY);
  985. break;
  986. default:
  987. err = vnic_dev_notify_set(enic->vdev, -1 /* no intr */);
  988. break;
  989. }
  990. return err;
  991. }
  992. static void enic_notify_timer_start(struct enic *enic)
  993. {
  994. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  995. case VNIC_DEV_INTR_MODE_MSI:
  996. mod_timer(&enic->notify_timer, jiffies);
  997. break;
  998. default:
  999. /* Using intr for notification for INTx/MSI-X */
  1000. break;
  1001. };
  1002. }
  1003. /* rtnl lock is held, process context */
  1004. static int enic_open(struct net_device *netdev)
  1005. {
  1006. struct enic *enic = netdev_priv(netdev);
  1007. unsigned int i;
  1008. int err;
  1009. err = enic_request_intr(enic);
  1010. if (err) {
  1011. printk(KERN_ERR PFX "%s: Unable to request irq.\n",
  1012. netdev->name);
  1013. return err;
  1014. }
  1015. err = enic_notify_set(enic);
  1016. if (err) {
  1017. printk(KERN_ERR PFX
  1018. "%s: Failed to alloc notify buffer, aborting.\n",
  1019. netdev->name);
  1020. goto err_out_free_intr;
  1021. }
  1022. for (i = 0; i < enic->rq_count; i++) {
  1023. err = vnic_rq_fill(&enic->rq[i], enic_rq_alloc_buf);
  1024. if (err) {
  1025. printk(KERN_ERR PFX
  1026. "%s: Unable to alloc receive buffers.\n",
  1027. netdev->name);
  1028. goto err_out_notify_unset;
  1029. }
  1030. }
  1031. for (i = 0; i < enic->wq_count; i++)
  1032. vnic_wq_enable(&enic->wq[i]);
  1033. for (i = 0; i < enic->rq_count; i++)
  1034. vnic_rq_enable(&enic->rq[i]);
  1035. enic_add_station_addr(enic);
  1036. enic_set_multicast_list(netdev);
  1037. netif_wake_queue(netdev);
  1038. napi_enable(&enic->napi);
  1039. vnic_dev_enable(enic->vdev);
  1040. for (i = 0; i < enic->intr_count; i++)
  1041. vnic_intr_unmask(&enic->intr[i]);
  1042. enic_notify_timer_start(enic);
  1043. return 0;
  1044. err_out_notify_unset:
  1045. vnic_dev_notify_unset(enic->vdev);
  1046. err_out_free_intr:
  1047. enic_free_intr(enic);
  1048. return err;
  1049. }
  1050. /* rtnl lock is held, process context */
  1051. static int enic_stop(struct net_device *netdev)
  1052. {
  1053. struct enic *enic = netdev_priv(netdev);
  1054. unsigned int i;
  1055. int err;
  1056. del_timer_sync(&enic->notify_timer);
  1057. vnic_dev_disable(enic->vdev);
  1058. napi_disable(&enic->napi);
  1059. netif_stop_queue(netdev);
  1060. for (i = 0; i < enic->intr_count; i++)
  1061. vnic_intr_mask(&enic->intr[i]);
  1062. for (i = 0; i < enic->wq_count; i++) {
  1063. err = vnic_wq_disable(&enic->wq[i]);
  1064. if (err)
  1065. return err;
  1066. }
  1067. for (i = 0; i < enic->rq_count; i++) {
  1068. err = vnic_rq_disable(&enic->rq[i]);
  1069. if (err)
  1070. return err;
  1071. }
  1072. vnic_dev_notify_unset(enic->vdev);
  1073. enic_free_intr(enic);
  1074. (void)vnic_cq_service(&enic->cq[ENIC_CQ_RQ],
  1075. -1, enic_rq_service_drop, NULL);
  1076. (void)vnic_cq_service(&enic->cq[ENIC_CQ_WQ],
  1077. -1, enic_wq_service, NULL);
  1078. for (i = 0; i < enic->wq_count; i++)
  1079. vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
  1080. for (i = 0; i < enic->rq_count; i++)
  1081. vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
  1082. for (i = 0; i < enic->cq_count; i++)
  1083. vnic_cq_clean(&enic->cq[i]);
  1084. for (i = 0; i < enic->intr_count; i++)
  1085. vnic_intr_clean(&enic->intr[i]);
  1086. return 0;
  1087. }
  1088. static int enic_change_mtu(struct net_device *netdev, int new_mtu)
  1089. {
  1090. struct enic *enic = netdev_priv(netdev);
  1091. int running = netif_running(netdev);
  1092. if (new_mtu < ENIC_MIN_MTU || new_mtu > ENIC_MAX_MTU)
  1093. return -EINVAL;
  1094. if (running)
  1095. enic_stop(netdev);
  1096. netdev->mtu = new_mtu;
  1097. if (netdev->mtu > enic->port_mtu)
  1098. printk(KERN_WARNING PFX
  1099. "%s: interface MTU (%d) set higher "
  1100. "than port MTU (%d)\n",
  1101. netdev->name, netdev->mtu, enic->port_mtu);
  1102. if (running)
  1103. enic_open(netdev);
  1104. return 0;
  1105. }
  1106. #ifdef CONFIG_NET_POLL_CONTROLLER
  1107. static void enic_poll_controller(struct net_device *netdev)
  1108. {
  1109. struct enic *enic = netdev_priv(netdev);
  1110. struct vnic_dev *vdev = enic->vdev;
  1111. switch (vnic_dev_get_intr_mode(vdev)) {
  1112. case VNIC_DEV_INTR_MODE_MSIX:
  1113. enic_isr_msix_rq(enic->pdev->irq, enic);
  1114. enic_isr_msix_wq(enic->pdev->irq, enic);
  1115. break;
  1116. case VNIC_DEV_INTR_MODE_MSI:
  1117. enic_isr_msi(enic->pdev->irq, enic);
  1118. break;
  1119. case VNIC_DEV_INTR_MODE_INTX:
  1120. enic_isr_legacy(enic->pdev->irq, netdev);
  1121. break;
  1122. default:
  1123. break;
  1124. }
  1125. }
  1126. #endif
  1127. static int enic_dev_wait(struct vnic_dev *vdev,
  1128. int (*start)(struct vnic_dev *, int),
  1129. int (*finished)(struct vnic_dev *, int *),
  1130. int arg)
  1131. {
  1132. unsigned long time;
  1133. int done;
  1134. int err;
  1135. BUG_ON(in_interrupt());
  1136. err = start(vdev, arg);
  1137. if (err)
  1138. return err;
  1139. /* Wait for func to complete...2 seconds max
  1140. */
  1141. time = jiffies + (HZ * 2);
  1142. do {
  1143. err = finished(vdev, &done);
  1144. if (err)
  1145. return err;
  1146. if (done)
  1147. return 0;
  1148. schedule_timeout_uninterruptible(HZ / 10);
  1149. } while (time_after(time, jiffies));
  1150. return -ETIMEDOUT;
  1151. }
  1152. static int enic_dev_open(struct enic *enic)
  1153. {
  1154. int err;
  1155. err = enic_dev_wait(enic->vdev, vnic_dev_open,
  1156. vnic_dev_open_done, 0);
  1157. if (err)
  1158. printk(KERN_ERR PFX
  1159. "vNIC device open failed, err %d.\n", err);
  1160. return err;
  1161. }
  1162. static int enic_dev_soft_reset(struct enic *enic)
  1163. {
  1164. int err;
  1165. err = enic_dev_wait(enic->vdev, vnic_dev_soft_reset,
  1166. vnic_dev_soft_reset_done, 0);
  1167. if (err)
  1168. printk(KERN_ERR PFX
  1169. "vNIC soft reset failed, err %d.\n", err);
  1170. return err;
  1171. }
  1172. static int enic_set_niccfg(struct enic *enic)
  1173. {
  1174. const u8 rss_default_cpu = 0;
  1175. const u8 rss_hash_type = 0;
  1176. const u8 rss_hash_bits = 0;
  1177. const u8 rss_base_cpu = 0;
  1178. const u8 rss_enable = 0;
  1179. const u8 tso_ipid_split_en = 0;
  1180. const u8 ig_vlan_strip_en = 1;
  1181. /* Enable VLAN tag stripping. RSS not enabled (yet).
  1182. */
  1183. return enic_set_nic_cfg(enic,
  1184. rss_default_cpu, rss_hash_type,
  1185. rss_hash_bits, rss_base_cpu,
  1186. rss_enable, tso_ipid_split_en,
  1187. ig_vlan_strip_en);
  1188. }
  1189. static void enic_reset(struct work_struct *work)
  1190. {
  1191. struct enic *enic = container_of(work, struct enic, reset);
  1192. if (!netif_running(enic->netdev))
  1193. return;
  1194. rtnl_lock();
  1195. spin_lock(&enic->devcmd_lock);
  1196. vnic_dev_hang_notify(enic->vdev);
  1197. spin_unlock(&enic->devcmd_lock);
  1198. enic_stop(enic->netdev);
  1199. enic_dev_soft_reset(enic);
  1200. vnic_dev_init(enic->vdev, 0);
  1201. enic_reset_mcaddrs(enic);
  1202. enic_init_vnic_resources(enic);
  1203. enic_set_niccfg(enic);
  1204. enic_open(enic->netdev);
  1205. rtnl_unlock();
  1206. }
  1207. static int enic_set_intr_mode(struct enic *enic)
  1208. {
  1209. unsigned int n = ARRAY_SIZE(enic->rq);
  1210. unsigned int m = ARRAY_SIZE(enic->wq);
  1211. unsigned int i;
  1212. /* Set interrupt mode (INTx, MSI, MSI-X) depending
  1213. * system capabilities.
  1214. *
  1215. * Try MSI-X first
  1216. *
  1217. * We need n RQs, m WQs, n+m CQs, and n+m+2 INTRs
  1218. * (the second to last INTR is used for WQ/RQ errors)
  1219. * (the last INTR is used for notifications)
  1220. */
  1221. BUG_ON(ARRAY_SIZE(enic->msix_entry) < n + m + 2);
  1222. for (i = 0; i < n + m + 2; i++)
  1223. enic->msix_entry[i].entry = i;
  1224. if (enic->config.intr_mode < 1 &&
  1225. enic->rq_count >= n &&
  1226. enic->wq_count >= m &&
  1227. enic->cq_count >= n + m &&
  1228. enic->intr_count >= n + m + 2 &&
  1229. !pci_enable_msix(enic->pdev, enic->msix_entry, n + m + 2)) {
  1230. enic->rq_count = n;
  1231. enic->wq_count = m;
  1232. enic->cq_count = n + m;
  1233. enic->intr_count = n + m + 2;
  1234. vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSIX);
  1235. return 0;
  1236. }
  1237. /* Next try MSI
  1238. *
  1239. * We need 1 RQ, 1 WQ, 2 CQs, and 1 INTR
  1240. */
  1241. if (enic->config.intr_mode < 2 &&
  1242. enic->rq_count >= 1 &&
  1243. enic->wq_count >= 1 &&
  1244. enic->cq_count >= 2 &&
  1245. enic->intr_count >= 1 &&
  1246. !pci_enable_msi(enic->pdev)) {
  1247. enic->rq_count = 1;
  1248. enic->wq_count = 1;
  1249. enic->cq_count = 2;
  1250. enic->intr_count = 1;
  1251. vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSI);
  1252. return 0;
  1253. }
  1254. /* Next try INTx
  1255. *
  1256. * We need 1 RQ, 1 WQ, 2 CQs, and 3 INTRs
  1257. * (the first INTR is used for WQ/RQ)
  1258. * (the second INTR is used for WQ/RQ errors)
  1259. * (the last INTR is used for notifications)
  1260. */
  1261. if (enic->config.intr_mode < 3 &&
  1262. enic->rq_count >= 1 &&
  1263. enic->wq_count >= 1 &&
  1264. enic->cq_count >= 2 &&
  1265. enic->intr_count >= 3) {
  1266. enic->rq_count = 1;
  1267. enic->wq_count = 1;
  1268. enic->cq_count = 2;
  1269. enic->intr_count = 3;
  1270. vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_INTX);
  1271. return 0;
  1272. }
  1273. vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
  1274. return -EINVAL;
  1275. }
  1276. static void enic_clear_intr_mode(struct enic *enic)
  1277. {
  1278. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  1279. case VNIC_DEV_INTR_MODE_MSIX:
  1280. pci_disable_msix(enic->pdev);
  1281. break;
  1282. case VNIC_DEV_INTR_MODE_MSI:
  1283. pci_disable_msi(enic->pdev);
  1284. break;
  1285. default:
  1286. break;
  1287. }
  1288. vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
  1289. }
  1290. static void enic_iounmap(struct enic *enic)
  1291. {
  1292. if (enic->bar0.vaddr)
  1293. iounmap(enic->bar0.vaddr);
  1294. }
  1295. static const struct net_device_ops enic_netdev_ops = {
  1296. .ndo_open = enic_open,
  1297. .ndo_stop = enic_stop,
  1298. .ndo_start_xmit = enic_hard_start_xmit,
  1299. .ndo_get_stats = enic_get_stats,
  1300. .ndo_validate_addr = eth_validate_addr,
  1301. .ndo_set_mac_address = eth_mac_addr,
  1302. .ndo_set_multicast_list = enic_set_multicast_list,
  1303. .ndo_change_mtu = enic_change_mtu,
  1304. .ndo_vlan_rx_register = enic_vlan_rx_register,
  1305. .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
  1306. .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
  1307. .ndo_tx_timeout = enic_tx_timeout,
  1308. #ifdef CONFIG_NET_POLL_CONTROLLER
  1309. .ndo_poll_controller = enic_poll_controller,
  1310. #endif
  1311. };
  1312. static int __devinit enic_probe(struct pci_dev *pdev,
  1313. const struct pci_device_id *ent)
  1314. {
  1315. struct net_device *netdev;
  1316. struct enic *enic;
  1317. int using_dac = 0;
  1318. unsigned int i;
  1319. int err;
  1320. /* Allocate net device structure and initialize. Private
  1321. * instance data is initialized to zero.
  1322. */
  1323. netdev = alloc_etherdev(sizeof(struct enic));
  1324. if (!netdev) {
  1325. printk(KERN_ERR PFX "Etherdev alloc failed, aborting.\n");
  1326. return -ENOMEM;
  1327. }
  1328. pci_set_drvdata(pdev, netdev);
  1329. SET_NETDEV_DEV(netdev, &pdev->dev);
  1330. enic = netdev_priv(netdev);
  1331. enic->netdev = netdev;
  1332. enic->pdev = pdev;
  1333. /* Setup PCI resources
  1334. */
  1335. err = pci_enable_device(pdev);
  1336. if (err) {
  1337. printk(KERN_ERR PFX
  1338. "Cannot enable PCI device, aborting.\n");
  1339. goto err_out_free_netdev;
  1340. }
  1341. err = pci_request_regions(pdev, DRV_NAME);
  1342. if (err) {
  1343. printk(KERN_ERR PFX
  1344. "Cannot request PCI regions, aborting.\n");
  1345. goto err_out_disable_device;
  1346. }
  1347. pci_set_master(pdev);
  1348. /* Query PCI controller on system for DMA addressing
  1349. * limitation for the device. Try 40-bit first, and
  1350. * fail to 32-bit.
  1351. */
  1352. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40));
  1353. if (err) {
  1354. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1355. if (err) {
  1356. printk(KERN_ERR PFX
  1357. "No usable DMA configuration, aborting.\n");
  1358. goto err_out_release_regions;
  1359. }
  1360. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  1361. if (err) {
  1362. printk(KERN_ERR PFX
  1363. "Unable to obtain 32-bit DMA "
  1364. "for consistent allocations, aborting.\n");
  1365. goto err_out_release_regions;
  1366. }
  1367. } else {
  1368. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
  1369. if (err) {
  1370. printk(KERN_ERR PFX
  1371. "Unable to obtain 40-bit DMA "
  1372. "for consistent allocations, aborting.\n");
  1373. goto err_out_release_regions;
  1374. }
  1375. using_dac = 1;
  1376. }
  1377. /* Map vNIC resources from BAR0
  1378. */
  1379. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  1380. printk(KERN_ERR PFX
  1381. "BAR0 not memory-map'able, aborting.\n");
  1382. err = -ENODEV;
  1383. goto err_out_release_regions;
  1384. }
  1385. enic->bar0.vaddr = pci_iomap(pdev, 0, enic->bar0.len);
  1386. enic->bar0.bus_addr = pci_resource_start(pdev, 0);
  1387. enic->bar0.len = pci_resource_len(pdev, 0);
  1388. if (!enic->bar0.vaddr) {
  1389. printk(KERN_ERR PFX
  1390. "Cannot memory-map BAR0 res hdr, aborting.\n");
  1391. err = -ENODEV;
  1392. goto err_out_release_regions;
  1393. }
  1394. /* Register vNIC device
  1395. */
  1396. enic->vdev = vnic_dev_register(NULL, enic, pdev, &enic->bar0);
  1397. if (!enic->vdev) {
  1398. printk(KERN_ERR PFX
  1399. "vNIC registration failed, aborting.\n");
  1400. err = -ENODEV;
  1401. goto err_out_iounmap;
  1402. }
  1403. /* Issue device open to get device in known state
  1404. */
  1405. err = enic_dev_open(enic);
  1406. if (err) {
  1407. printk(KERN_ERR PFX
  1408. "vNIC dev open failed, aborting.\n");
  1409. goto err_out_vnic_unregister;
  1410. }
  1411. /* Issue device init to initialize the vnic-to-switch link.
  1412. * We'll start with carrier off and wait for link UP
  1413. * notification later to turn on carrier. We don't need
  1414. * to wait here for the vnic-to-switch link initialization
  1415. * to complete; link UP notification is the indication that
  1416. * the process is complete.
  1417. */
  1418. netif_carrier_off(netdev);
  1419. err = vnic_dev_init(enic->vdev, 0);
  1420. if (err) {
  1421. printk(KERN_ERR PFX
  1422. "vNIC dev init failed, aborting.\n");
  1423. goto err_out_dev_close;
  1424. }
  1425. /* Get vNIC configuration
  1426. */
  1427. err = enic_get_vnic_config(enic);
  1428. if (err) {
  1429. printk(KERN_ERR PFX
  1430. "Get vNIC configuration failed, aborting.\n");
  1431. goto err_out_dev_close;
  1432. }
  1433. /* Get available resource counts
  1434. */
  1435. enic_get_res_counts(enic);
  1436. /* Set interrupt mode based on resource counts and system
  1437. * capabilities
  1438. */
  1439. err = enic_set_intr_mode(enic);
  1440. if (err) {
  1441. printk(KERN_ERR PFX
  1442. "Failed to set intr mode, aborting.\n");
  1443. goto err_out_dev_close;
  1444. }
  1445. /* Allocate and configure vNIC resources
  1446. */
  1447. err = enic_alloc_vnic_resources(enic);
  1448. if (err) {
  1449. printk(KERN_ERR PFX
  1450. "Failed to alloc vNIC resources, aborting.\n");
  1451. goto err_out_free_vnic_resources;
  1452. }
  1453. enic_init_vnic_resources(enic);
  1454. err = enic_set_niccfg(enic);
  1455. if (err) {
  1456. printk(KERN_ERR PFX
  1457. "Failed to config nic, aborting.\n");
  1458. goto err_out_free_vnic_resources;
  1459. }
  1460. /* Setup notification timer, HW reset task, and locks
  1461. */
  1462. init_timer(&enic->notify_timer);
  1463. enic->notify_timer.function = enic_notify_timer;
  1464. enic->notify_timer.data = (unsigned long)enic;
  1465. INIT_WORK(&enic->reset, enic_reset);
  1466. for (i = 0; i < enic->wq_count; i++)
  1467. spin_lock_init(&enic->wq_lock[i]);
  1468. spin_lock_init(&enic->devcmd_lock);
  1469. /* Register net device
  1470. */
  1471. enic->port_mtu = enic->config.mtu;
  1472. (void)enic_change_mtu(netdev, enic->port_mtu);
  1473. err = enic_set_mac_addr(netdev, enic->mac_addr);
  1474. if (err) {
  1475. printk(KERN_ERR PFX
  1476. "Invalid MAC address, aborting.\n");
  1477. goto err_out_free_vnic_resources;
  1478. }
  1479. netdev->netdev_ops = &enic_netdev_ops;
  1480. netdev->watchdog_timeo = 2 * HZ;
  1481. netdev->ethtool_ops = &enic_ethtool_ops;
  1482. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  1483. default:
  1484. netif_napi_add(netdev, &enic->napi, enic_poll, 64);
  1485. break;
  1486. case VNIC_DEV_INTR_MODE_MSIX:
  1487. netif_napi_add(netdev, &enic->napi, enic_poll_msix, 64);
  1488. break;
  1489. }
  1490. netdev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  1491. if (ENIC_SETTING(enic, TXCSUM))
  1492. netdev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
  1493. if (ENIC_SETTING(enic, TSO))
  1494. netdev->features |= NETIF_F_TSO |
  1495. NETIF_F_TSO6 | NETIF_F_TSO_ECN;
  1496. if (ENIC_SETTING(enic, LRO))
  1497. netdev->features |= NETIF_F_LRO;
  1498. if (using_dac)
  1499. netdev->features |= NETIF_F_HIGHDMA;
  1500. enic->csum_rx_enabled = ENIC_SETTING(enic, RXCSUM);
  1501. enic->lro_mgr.max_aggr = ENIC_LRO_MAX_AGGR;
  1502. enic->lro_mgr.max_desc = ENIC_LRO_MAX_DESC;
  1503. enic->lro_mgr.lro_arr = enic->lro_desc;
  1504. enic->lro_mgr.get_skb_header = enic_get_skb_header;
  1505. enic->lro_mgr.features = LRO_F_NAPI | LRO_F_EXTRACT_VLAN_ID;
  1506. enic->lro_mgr.dev = netdev;
  1507. enic->lro_mgr.ip_summed = CHECKSUM_COMPLETE;
  1508. enic->lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
  1509. err = register_netdev(netdev);
  1510. if (err) {
  1511. printk(KERN_ERR PFX
  1512. "Cannot register net device, aborting.\n");
  1513. goto err_out_free_vnic_resources;
  1514. }
  1515. return 0;
  1516. err_out_free_vnic_resources:
  1517. enic_free_vnic_resources(enic);
  1518. err_out_dev_close:
  1519. vnic_dev_close(enic->vdev);
  1520. err_out_vnic_unregister:
  1521. enic_clear_intr_mode(enic);
  1522. vnic_dev_unregister(enic->vdev);
  1523. err_out_iounmap:
  1524. enic_iounmap(enic);
  1525. err_out_release_regions:
  1526. pci_release_regions(pdev);
  1527. err_out_disable_device:
  1528. pci_disable_device(pdev);
  1529. err_out_free_netdev:
  1530. pci_set_drvdata(pdev, NULL);
  1531. free_netdev(netdev);
  1532. return err;
  1533. }
  1534. static void __devexit enic_remove(struct pci_dev *pdev)
  1535. {
  1536. struct net_device *netdev = pci_get_drvdata(pdev);
  1537. if (netdev) {
  1538. struct enic *enic = netdev_priv(netdev);
  1539. flush_scheduled_work();
  1540. unregister_netdev(netdev);
  1541. enic_free_vnic_resources(enic);
  1542. vnic_dev_close(enic->vdev);
  1543. enic_clear_intr_mode(enic);
  1544. vnic_dev_unregister(enic->vdev);
  1545. enic_iounmap(enic);
  1546. pci_release_regions(pdev);
  1547. pci_disable_device(pdev);
  1548. pci_set_drvdata(pdev, NULL);
  1549. free_netdev(netdev);
  1550. }
  1551. }
  1552. static struct pci_driver enic_driver = {
  1553. .name = DRV_NAME,
  1554. .id_table = enic_id_table,
  1555. .probe = enic_probe,
  1556. .remove = __devexit_p(enic_remove),
  1557. };
  1558. static int __init enic_init_module(void)
  1559. {
  1560. printk(KERN_INFO PFX "%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
  1561. return pci_register_driver(&enic_driver);
  1562. }
  1563. static void __exit enic_cleanup_module(void)
  1564. {
  1565. pci_unregister_driver(&enic_driver);
  1566. }
  1567. module_init(enic_init_module);
  1568. module_exit(enic_cleanup_module);