enic_main.c 52 KB

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