enic_main.c 59 KB

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