enic_main.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536
  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. .set_flags = ethtool_op_set_flags,
  311. };
  312. static void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
  313. {
  314. struct enic *enic = vnic_dev_priv(wq->vdev);
  315. if (buf->sop)
  316. pci_unmap_single(enic->pdev, buf->dma_addr,
  317. buf->len, PCI_DMA_TODEVICE);
  318. else
  319. pci_unmap_page(enic->pdev, buf->dma_addr,
  320. buf->len, PCI_DMA_TODEVICE);
  321. if (buf->os_buf)
  322. dev_kfree_skb_any(buf->os_buf);
  323. }
  324. static void enic_wq_free_buf(struct vnic_wq *wq,
  325. struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque)
  326. {
  327. enic_free_wq_buf(wq, buf);
  328. }
  329. static int enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
  330. u8 type, u16 q_number, u16 completed_index, void *opaque)
  331. {
  332. struct enic *enic = vnic_dev_priv(vdev);
  333. spin_lock(&enic->wq_lock[q_number]);
  334. vnic_wq_service(&enic->wq[q_number], cq_desc,
  335. completed_index, enic_wq_free_buf,
  336. opaque);
  337. if (netif_queue_stopped(enic->netdev) &&
  338. vnic_wq_desc_avail(&enic->wq[q_number]) >=
  339. (MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS))
  340. netif_wake_queue(enic->netdev);
  341. spin_unlock(&enic->wq_lock[q_number]);
  342. return 0;
  343. }
  344. static void enic_log_q_error(struct enic *enic)
  345. {
  346. unsigned int i;
  347. u32 error_status;
  348. for (i = 0; i < enic->wq_count; i++) {
  349. error_status = vnic_wq_error_status(&enic->wq[i]);
  350. if (error_status)
  351. netdev_err(enic->netdev, "WQ[%d] error_status %d\n",
  352. i, error_status);
  353. }
  354. for (i = 0; i < enic->rq_count; i++) {
  355. error_status = vnic_rq_error_status(&enic->rq[i]);
  356. if (error_status)
  357. netdev_err(enic->netdev, "RQ[%d] error_status %d\n",
  358. i, error_status);
  359. }
  360. }
  361. static void enic_msglvl_check(struct enic *enic)
  362. {
  363. u32 msg_enable = vnic_dev_msg_lvl(enic->vdev);
  364. if (msg_enable != enic->msg_enable) {
  365. netdev_info(enic->netdev, "msg lvl changed from 0x%x to 0x%x\n",
  366. enic->msg_enable, msg_enable);
  367. enic->msg_enable = msg_enable;
  368. }
  369. }
  370. static void enic_mtu_check(struct enic *enic)
  371. {
  372. u32 mtu = vnic_dev_mtu(enic->vdev);
  373. struct net_device *netdev = enic->netdev;
  374. if (mtu && mtu != enic->port_mtu) {
  375. enic->port_mtu = mtu;
  376. if (mtu < netdev->mtu)
  377. netdev_warn(netdev,
  378. "interface MTU (%d) set higher "
  379. "than switch port MTU (%d)\n",
  380. netdev->mtu, mtu);
  381. }
  382. }
  383. static void enic_link_check(struct enic *enic)
  384. {
  385. int link_status = vnic_dev_link_status(enic->vdev);
  386. int carrier_ok = netif_carrier_ok(enic->netdev);
  387. if (link_status && !carrier_ok) {
  388. netdev_info(enic->netdev, "Link UP\n");
  389. netif_carrier_on(enic->netdev);
  390. } else if (!link_status && carrier_ok) {
  391. netdev_info(enic->netdev, "Link DOWN\n");
  392. netif_carrier_off(enic->netdev);
  393. }
  394. }
  395. static void enic_notify_check(struct enic *enic)
  396. {
  397. enic_msglvl_check(enic);
  398. enic_mtu_check(enic);
  399. enic_link_check(enic);
  400. }
  401. #define ENIC_TEST_INTR(pba, i) (pba & (1 << i))
  402. static irqreturn_t enic_isr_legacy(int irq, void *data)
  403. {
  404. struct net_device *netdev = data;
  405. struct enic *enic = netdev_priv(netdev);
  406. u32 pba;
  407. vnic_intr_mask(&enic->intr[ENIC_INTX_WQ_RQ]);
  408. pba = vnic_intr_legacy_pba(enic->legacy_pba);
  409. if (!pba) {
  410. vnic_intr_unmask(&enic->intr[ENIC_INTX_WQ_RQ]);
  411. return IRQ_NONE; /* not our interrupt */
  412. }
  413. if (ENIC_TEST_INTR(pba, ENIC_INTX_NOTIFY)) {
  414. vnic_intr_return_all_credits(&enic->intr[ENIC_INTX_NOTIFY]);
  415. enic_notify_check(enic);
  416. }
  417. if (ENIC_TEST_INTR(pba, ENIC_INTX_ERR)) {
  418. vnic_intr_return_all_credits(&enic->intr[ENIC_INTX_ERR]);
  419. enic_log_q_error(enic);
  420. /* schedule recovery from WQ/RQ error */
  421. schedule_work(&enic->reset);
  422. return IRQ_HANDLED;
  423. }
  424. if (ENIC_TEST_INTR(pba, ENIC_INTX_WQ_RQ)) {
  425. if (napi_schedule_prep(&enic->napi))
  426. __napi_schedule(&enic->napi);
  427. } else {
  428. vnic_intr_unmask(&enic->intr[ENIC_INTX_WQ_RQ]);
  429. }
  430. return IRQ_HANDLED;
  431. }
  432. static irqreturn_t enic_isr_msi(int irq, void *data)
  433. {
  434. struct enic *enic = data;
  435. /* With MSI, there is no sharing of interrupts, so this is
  436. * our interrupt and there is no need to ack it. The device
  437. * is not providing per-vector masking, so the OS will not
  438. * write to PCI config space to mask/unmask the interrupt.
  439. * We're using mask_on_assertion for MSI, so the device
  440. * automatically masks the interrupt when the interrupt is
  441. * generated. Later, when exiting polling, the interrupt
  442. * will be unmasked (see enic_poll).
  443. *
  444. * Also, the device uses the same PCIe Traffic Class (TC)
  445. * for Memory Write data and MSI, so there are no ordering
  446. * issues; the MSI will always arrive at the Root Complex
  447. * _after_ corresponding Memory Writes (i.e. descriptor
  448. * writes).
  449. */
  450. napi_schedule(&enic->napi);
  451. return IRQ_HANDLED;
  452. }
  453. static irqreturn_t enic_isr_msix_rq(int irq, void *data)
  454. {
  455. struct enic *enic = data;
  456. /* schedule NAPI polling for RQ cleanup */
  457. napi_schedule(&enic->napi);
  458. return IRQ_HANDLED;
  459. }
  460. static irqreturn_t enic_isr_msix_wq(int irq, void *data)
  461. {
  462. struct enic *enic = data;
  463. unsigned int wq_work_to_do = -1; /* no limit */
  464. unsigned int wq_work_done;
  465. wq_work_done = vnic_cq_service(&enic->cq[ENIC_CQ_WQ],
  466. wq_work_to_do, enic_wq_service, NULL);
  467. vnic_intr_return_credits(&enic->intr[ENIC_MSIX_WQ],
  468. wq_work_done,
  469. 1 /* unmask intr */,
  470. 1 /* reset intr timer */);
  471. return IRQ_HANDLED;
  472. }
  473. static irqreturn_t enic_isr_msix_err(int irq, void *data)
  474. {
  475. struct enic *enic = data;
  476. vnic_intr_return_all_credits(&enic->intr[ENIC_MSIX_ERR]);
  477. enic_log_q_error(enic);
  478. /* schedule recovery from WQ/RQ error */
  479. schedule_work(&enic->reset);
  480. return IRQ_HANDLED;
  481. }
  482. static irqreturn_t enic_isr_msix_notify(int irq, void *data)
  483. {
  484. struct enic *enic = data;
  485. vnic_intr_return_all_credits(&enic->intr[ENIC_MSIX_NOTIFY]);
  486. enic_notify_check(enic);
  487. return IRQ_HANDLED;
  488. }
  489. static inline void enic_queue_wq_skb_cont(struct enic *enic,
  490. struct vnic_wq *wq, struct sk_buff *skb,
  491. unsigned int len_left, int loopback)
  492. {
  493. skb_frag_t *frag;
  494. /* Queue additional data fragments */
  495. for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
  496. len_left -= frag->size;
  497. enic_queue_wq_desc_cont(wq, skb,
  498. pci_map_page(enic->pdev, frag->page,
  499. frag->page_offset, frag->size,
  500. PCI_DMA_TODEVICE),
  501. frag->size,
  502. (len_left == 0), /* EOP? */
  503. loopback);
  504. }
  505. }
  506. static inline void enic_queue_wq_skb_vlan(struct enic *enic,
  507. struct vnic_wq *wq, struct sk_buff *skb,
  508. int vlan_tag_insert, unsigned int vlan_tag, int loopback)
  509. {
  510. unsigned int head_len = skb_headlen(skb);
  511. unsigned int len_left = skb->len - head_len;
  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(wq, skb,
  519. pci_map_single(enic->pdev, skb->data,
  520. head_len, PCI_DMA_TODEVICE),
  521. head_len,
  522. vlan_tag_insert, vlan_tag,
  523. eop, loopback);
  524. if (!eop)
  525. enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
  526. }
  527. static inline void enic_queue_wq_skb_csum_l4(struct enic *enic,
  528. struct vnic_wq *wq, struct sk_buff *skb,
  529. int vlan_tag_insert, unsigned int vlan_tag, int loopback)
  530. {
  531. unsigned int head_len = skb_headlen(skb);
  532. unsigned int len_left = skb->len - head_len;
  533. unsigned int hdr_len = skb_transport_offset(skb);
  534. unsigned int csum_offset = hdr_len + skb->csum_offset;
  535. int eop = (len_left == 0);
  536. /* Queue the main skb fragment. The fragments are no larger
  537. * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
  538. * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
  539. * per fragment is queued.
  540. */
  541. enic_queue_wq_desc_csum_l4(wq, skb,
  542. pci_map_single(enic->pdev, skb->data,
  543. head_len, PCI_DMA_TODEVICE),
  544. head_len,
  545. csum_offset,
  546. hdr_len,
  547. vlan_tag_insert, vlan_tag,
  548. eop, loopback);
  549. if (!eop)
  550. enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
  551. }
  552. static inline void enic_queue_wq_skb_tso(struct enic *enic,
  553. struct vnic_wq *wq, struct sk_buff *skb, unsigned int mss,
  554. int vlan_tag_insert, unsigned int vlan_tag, int loopback)
  555. {
  556. unsigned int frag_len_left = skb_headlen(skb);
  557. unsigned int len_left = skb->len - frag_len_left;
  558. unsigned int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  559. int eop = (len_left == 0);
  560. unsigned int len;
  561. dma_addr_t dma_addr;
  562. unsigned int offset = 0;
  563. skb_frag_t *frag;
  564. /* Preload TCP csum field with IP pseudo hdr calculated
  565. * with IP length set to zero. HW will later add in length
  566. * to each TCP segment resulting from the TSO.
  567. */
  568. if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
  569. ip_hdr(skb)->check = 0;
  570. tcp_hdr(skb)->check = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  571. ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  572. } else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
  573. tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  574. &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  575. }
  576. /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
  577. * for the main skb fragment
  578. */
  579. while (frag_len_left) {
  580. len = min(frag_len_left, (unsigned int)WQ_ENET_MAX_DESC_LEN);
  581. dma_addr = pci_map_single(enic->pdev, skb->data + offset,
  582. len, PCI_DMA_TODEVICE);
  583. enic_queue_wq_desc_tso(wq, skb,
  584. dma_addr,
  585. len,
  586. mss, hdr_len,
  587. vlan_tag_insert, vlan_tag,
  588. eop && (len == frag_len_left), loopback);
  589. frag_len_left -= len;
  590. offset += len;
  591. }
  592. if (eop)
  593. return;
  594. /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
  595. * for additional data fragments
  596. */
  597. for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
  598. len_left -= frag->size;
  599. frag_len_left = frag->size;
  600. offset = frag->page_offset;
  601. while (frag_len_left) {
  602. len = min(frag_len_left,
  603. (unsigned int)WQ_ENET_MAX_DESC_LEN);
  604. dma_addr = pci_map_page(enic->pdev, frag->page,
  605. offset, len,
  606. PCI_DMA_TODEVICE);
  607. enic_queue_wq_desc_cont(wq, skb,
  608. dma_addr,
  609. len,
  610. (len_left == 0) &&
  611. (len == frag_len_left), /* EOP? */
  612. loopback);
  613. frag_len_left -= len;
  614. offset += len;
  615. }
  616. }
  617. }
  618. static inline void enic_queue_wq_skb(struct enic *enic,
  619. struct vnic_wq *wq, struct sk_buff *skb)
  620. {
  621. unsigned int mss = skb_shinfo(skb)->gso_size;
  622. unsigned int vlan_tag = 0;
  623. int vlan_tag_insert = 0;
  624. int loopback = 0;
  625. if (enic->vlan_group && vlan_tx_tag_present(skb)) {
  626. /* VLAN tag from trunking driver */
  627. vlan_tag_insert = 1;
  628. vlan_tag = vlan_tx_tag_get(skb);
  629. } else if (enic->loop_enable) {
  630. vlan_tag = enic->loop_tag;
  631. loopback = 1;
  632. }
  633. if (mss)
  634. enic_queue_wq_skb_tso(enic, wq, skb, mss,
  635. vlan_tag_insert, vlan_tag, loopback);
  636. else if (skb->ip_summed == CHECKSUM_PARTIAL)
  637. enic_queue_wq_skb_csum_l4(enic, wq, skb,
  638. vlan_tag_insert, vlan_tag, loopback);
  639. else
  640. enic_queue_wq_skb_vlan(enic, wq, skb,
  641. vlan_tag_insert, vlan_tag, loopback);
  642. }
  643. /* netif_tx_lock held, process context with BHs disabled, or BH */
  644. static netdev_tx_t enic_hard_start_xmit(struct sk_buff *skb,
  645. struct net_device *netdev)
  646. {
  647. struct enic *enic = netdev_priv(netdev);
  648. struct vnic_wq *wq = &enic->wq[0];
  649. unsigned long flags;
  650. if (skb->len <= 0) {
  651. dev_kfree_skb(skb);
  652. return NETDEV_TX_OK;
  653. }
  654. /* Non-TSO sends must fit within ENIC_NON_TSO_MAX_DESC descs,
  655. * which is very likely. In the off chance it's going to take
  656. * more than * ENIC_NON_TSO_MAX_DESC, linearize the skb.
  657. */
  658. if (skb_shinfo(skb)->gso_size == 0 &&
  659. skb_shinfo(skb)->nr_frags + 1 > ENIC_NON_TSO_MAX_DESC &&
  660. skb_linearize(skb)) {
  661. dev_kfree_skb(skb);
  662. return NETDEV_TX_OK;
  663. }
  664. spin_lock_irqsave(&enic->wq_lock[0], flags);
  665. if (vnic_wq_desc_avail(wq) <
  666. skb_shinfo(skb)->nr_frags + ENIC_DESC_MAX_SPLITS) {
  667. netif_stop_queue(netdev);
  668. /* This is a hard error, log it */
  669. netdev_err(netdev, "BUG! Tx ring full when queue awake!\n");
  670. spin_unlock_irqrestore(&enic->wq_lock[0], flags);
  671. return NETDEV_TX_BUSY;
  672. }
  673. enic_queue_wq_skb(enic, wq, skb);
  674. if (vnic_wq_desc_avail(wq) < MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS)
  675. netif_stop_queue(netdev);
  676. spin_unlock_irqrestore(&enic->wq_lock[0], flags);
  677. return NETDEV_TX_OK;
  678. }
  679. /* dev_base_lock rwlock held, nominally process context */
  680. static struct net_device_stats *enic_get_stats(struct net_device *netdev)
  681. {
  682. struct enic *enic = netdev_priv(netdev);
  683. struct net_device_stats *net_stats = &netdev->stats;
  684. struct vnic_stats *stats;
  685. enic_dev_stats_dump(enic, &stats);
  686. net_stats->tx_packets = stats->tx.tx_frames_ok;
  687. net_stats->tx_bytes = stats->tx.tx_bytes_ok;
  688. net_stats->tx_errors = stats->tx.tx_errors;
  689. net_stats->tx_dropped = stats->tx.tx_drops;
  690. net_stats->rx_packets = stats->rx.rx_frames_ok;
  691. net_stats->rx_bytes = stats->rx.rx_bytes_ok;
  692. net_stats->rx_errors = stats->rx.rx_errors;
  693. net_stats->multicast = stats->rx.rx_multicast_frames_ok;
  694. net_stats->rx_over_errors = enic->rq_truncated_pkts;
  695. net_stats->rx_crc_errors = enic->rq_bad_fcs;
  696. net_stats->rx_dropped = stats->rx.rx_no_bufs + stats->rx.rx_drop;
  697. return net_stats;
  698. }
  699. static void enic_reset_multicast_list(struct enic *enic)
  700. {
  701. enic->mc_count = 0;
  702. enic->flags = 0;
  703. }
  704. static int enic_set_mac_addr(struct net_device *netdev, char *addr)
  705. {
  706. struct enic *enic = netdev_priv(netdev);
  707. if (enic_is_dynamic(enic)) {
  708. if (!is_valid_ether_addr(addr) && !is_zero_ether_addr(addr))
  709. return -EADDRNOTAVAIL;
  710. } else {
  711. if (!is_valid_ether_addr(addr))
  712. return -EADDRNOTAVAIL;
  713. }
  714. memcpy(netdev->dev_addr, addr, netdev->addr_len);
  715. return 0;
  716. }
  717. static int enic_dev_add_station_addr(struct enic *enic)
  718. {
  719. int err = 0;
  720. if (is_valid_ether_addr(enic->netdev->dev_addr)) {
  721. spin_lock(&enic->devcmd_lock);
  722. err = vnic_dev_add_addr(enic->vdev, enic->netdev->dev_addr);
  723. spin_unlock(&enic->devcmd_lock);
  724. }
  725. return err;
  726. }
  727. static int enic_dev_del_station_addr(struct enic *enic)
  728. {
  729. int err = 0;
  730. if (is_valid_ether_addr(enic->netdev->dev_addr)) {
  731. spin_lock(&enic->devcmd_lock);
  732. err = vnic_dev_del_addr(enic->vdev, enic->netdev->dev_addr);
  733. spin_unlock(&enic->devcmd_lock);
  734. }
  735. return err;
  736. }
  737. static int enic_set_mac_address_dynamic(struct net_device *netdev, void *p)
  738. {
  739. struct enic *enic = netdev_priv(netdev);
  740. struct sockaddr *saddr = p;
  741. char *addr = saddr->sa_data;
  742. int err;
  743. if (netif_running(enic->netdev)) {
  744. err = enic_dev_del_station_addr(enic);
  745. if (err)
  746. return err;
  747. }
  748. err = enic_set_mac_addr(netdev, addr);
  749. if (err)
  750. return err;
  751. if (netif_running(enic->netdev)) {
  752. err = enic_dev_add_station_addr(enic);
  753. if (err)
  754. return err;
  755. }
  756. return err;
  757. }
  758. static int enic_set_mac_address(struct net_device *netdev, void *p)
  759. {
  760. return -EOPNOTSUPP;
  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. u8 *uuid;
  902. char uuid_str[38];
  903. static char *uuid_fmt = "%02X%02X%02X%02X-%02X%02X-%02X%02X-"
  904. "%02X%02X-%02X%02X%02X%02X%0X%02X";
  905. int err;
  906. err = enic_vnic_dev_deinit(enic);
  907. if (err)
  908. return err;
  909. switch (enic->pp.request) {
  910. case PORT_REQUEST_ASSOCIATE:
  911. if (!(enic->pp.set & ENIC_SET_NAME) || !strlen(enic->pp.name))
  912. return -EINVAL;
  913. if (!is_valid_ether_addr(mac))
  914. return -EADDRNOTAVAIL;
  915. vp = vic_provinfo_alloc(GFP_KERNEL, oui,
  916. VIC_PROVINFO_LINUX_TYPE);
  917. if (!vp)
  918. return -ENOMEM;
  919. vic_provinfo_add_tlv(vp,
  920. VIC_LINUX_PROV_TLV_PORT_PROFILE_NAME_STR,
  921. strlen(enic->pp.name) + 1, enic->pp.name);
  922. vic_provinfo_add_tlv(vp,
  923. VIC_LINUX_PROV_TLV_CLIENT_MAC_ADDR,
  924. ETH_ALEN, mac);
  925. if (enic->pp.set & ENIC_SET_INSTANCE) {
  926. uuid = enic->pp.instance_uuid;
  927. sprintf(uuid_str, uuid_fmt,
  928. uuid[0], uuid[1], uuid[2], uuid[3],
  929. uuid[4], uuid[5], uuid[6], uuid[7],
  930. uuid[8], uuid[9], uuid[10], uuid[11],
  931. uuid[12], uuid[13], uuid[14], uuid[15]);
  932. vic_provinfo_add_tlv(vp,
  933. VIC_LINUX_PROV_TLV_CLIENT_UUID_STR,
  934. sizeof(uuid_str), uuid_str);
  935. }
  936. if (enic->pp.set & ENIC_SET_HOST) {
  937. uuid = enic->pp.host_uuid;
  938. sprintf(uuid_str, uuid_fmt,
  939. uuid[0], uuid[1], uuid[2], uuid[3],
  940. uuid[4], uuid[5], uuid[6], uuid[7],
  941. uuid[8], uuid[9], uuid[10], uuid[11],
  942. uuid[12], uuid[13], uuid[14], uuid[15]);
  943. vic_provinfo_add_tlv(vp,
  944. VIC_LINUX_PROV_TLV_HOST_UUID_STR,
  945. sizeof(uuid_str), uuid_str);
  946. }
  947. err = enic_dev_init_prov(enic, vp);
  948. vic_provinfo_free(vp);
  949. if (err)
  950. return err;
  951. break;
  952. case PORT_REQUEST_DISASSOCIATE:
  953. break;
  954. default:
  955. return -EINVAL;
  956. }
  957. enic->pp.set |= ENIC_SET_APPLIED;
  958. return 0;
  959. }
  960. static int enic_set_vf_port(struct net_device *netdev, int vf,
  961. struct nlattr *port[])
  962. {
  963. struct enic *enic = netdev_priv(netdev);
  964. memset(&enic->pp, 0, sizeof(enic->pp));
  965. if (port[IFLA_PORT_REQUEST]) {
  966. enic->pp.set |= ENIC_SET_REQUEST;
  967. enic->pp.request = nla_get_u8(port[IFLA_PORT_REQUEST]);
  968. }
  969. if (port[IFLA_PORT_PROFILE]) {
  970. enic->pp.set |= ENIC_SET_NAME;
  971. memcpy(enic->pp.name, nla_data(port[IFLA_PORT_PROFILE]),
  972. PORT_PROFILE_MAX);
  973. }
  974. if (port[IFLA_PORT_INSTANCE_UUID]) {
  975. enic->pp.set |= ENIC_SET_INSTANCE;
  976. memcpy(enic->pp.instance_uuid,
  977. nla_data(port[IFLA_PORT_INSTANCE_UUID]), PORT_UUID_MAX);
  978. }
  979. if (port[IFLA_PORT_HOST_UUID]) {
  980. enic->pp.set |= ENIC_SET_HOST;
  981. memcpy(enic->pp.host_uuid,
  982. nla_data(port[IFLA_PORT_HOST_UUID]), PORT_UUID_MAX);
  983. }
  984. /* don't support VFs, yet */
  985. if (vf != PORT_SELF_VF)
  986. return -EOPNOTSUPP;
  987. if (!(enic->pp.set & ENIC_SET_REQUEST))
  988. return -EOPNOTSUPP;
  989. if (enic->pp.request == PORT_REQUEST_ASSOCIATE) {
  990. /* If the interface mac addr hasn't been assigned,
  991. * assign a random mac addr before setting port-
  992. * profile.
  993. */
  994. if (is_zero_ether_addr(netdev->dev_addr))
  995. random_ether_addr(netdev->dev_addr);
  996. }
  997. return enic_set_port_profile(enic, netdev->dev_addr);
  998. }
  999. static int enic_get_vf_port(struct net_device *netdev, int vf,
  1000. struct sk_buff *skb)
  1001. {
  1002. struct enic *enic = netdev_priv(netdev);
  1003. int err, error, done;
  1004. u16 response = PORT_PROFILE_RESPONSE_SUCCESS;
  1005. if (!(enic->pp.set & ENIC_SET_APPLIED))
  1006. return -ENODATA;
  1007. err = enic_dev_init_done(enic, &done, &error);
  1008. if (err)
  1009. error = err;
  1010. switch (error) {
  1011. case ERR_SUCCESS:
  1012. if (!done)
  1013. response = PORT_PROFILE_RESPONSE_INPROGRESS;
  1014. break;
  1015. case ERR_EINVAL:
  1016. response = PORT_PROFILE_RESPONSE_INVALID;
  1017. break;
  1018. case ERR_EBADSTATE:
  1019. response = PORT_PROFILE_RESPONSE_BADSTATE;
  1020. break;
  1021. case ERR_ENOMEM:
  1022. response = PORT_PROFILE_RESPONSE_INSUFFICIENT_RESOURCES;
  1023. break;
  1024. default:
  1025. response = PORT_PROFILE_RESPONSE_ERROR;
  1026. break;
  1027. }
  1028. NLA_PUT_U16(skb, IFLA_PORT_REQUEST, enic->pp.request);
  1029. NLA_PUT_U16(skb, IFLA_PORT_RESPONSE, response);
  1030. if (enic->pp.set & ENIC_SET_NAME)
  1031. NLA_PUT(skb, IFLA_PORT_PROFILE, PORT_PROFILE_MAX,
  1032. enic->pp.name);
  1033. if (enic->pp.set & ENIC_SET_INSTANCE)
  1034. NLA_PUT(skb, IFLA_PORT_INSTANCE_UUID, PORT_UUID_MAX,
  1035. enic->pp.instance_uuid);
  1036. if (enic->pp.set & ENIC_SET_HOST)
  1037. NLA_PUT(skb, IFLA_PORT_HOST_UUID, PORT_UUID_MAX,
  1038. enic->pp.host_uuid);
  1039. return 0;
  1040. nla_put_failure:
  1041. return -EMSGSIZE;
  1042. }
  1043. static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
  1044. {
  1045. struct enic *enic = vnic_dev_priv(rq->vdev);
  1046. if (!buf->os_buf)
  1047. return;
  1048. pci_unmap_single(enic->pdev, buf->dma_addr,
  1049. buf->len, PCI_DMA_FROMDEVICE);
  1050. dev_kfree_skb_any(buf->os_buf);
  1051. }
  1052. static int enic_rq_alloc_buf(struct vnic_rq *rq)
  1053. {
  1054. struct enic *enic = vnic_dev_priv(rq->vdev);
  1055. struct net_device *netdev = enic->netdev;
  1056. struct sk_buff *skb;
  1057. unsigned int len = netdev->mtu + VLAN_ETH_HLEN;
  1058. unsigned int os_buf_index = 0;
  1059. dma_addr_t dma_addr;
  1060. skb = netdev_alloc_skb_ip_align(netdev, len);
  1061. if (!skb)
  1062. return -ENOMEM;
  1063. dma_addr = pci_map_single(enic->pdev, skb->data,
  1064. len, PCI_DMA_FROMDEVICE);
  1065. enic_queue_rq_desc(rq, skb, os_buf_index,
  1066. dma_addr, len);
  1067. return 0;
  1068. }
  1069. static int enic_rq_alloc_buf_a1(struct vnic_rq *rq)
  1070. {
  1071. struct rq_enet_desc *desc = vnic_rq_next_desc(rq);
  1072. if (vnic_rq_posting_soon(rq)) {
  1073. /* SW workaround for A0 HW erratum: if we're just about
  1074. * to write posted_index, insert a dummy desc
  1075. * of type resvd
  1076. */
  1077. rq_enet_desc_enc(desc, 0, RQ_ENET_TYPE_RESV2, 0);
  1078. vnic_rq_post(rq, 0, 0, 0, 0);
  1079. } else {
  1080. return enic_rq_alloc_buf(rq);
  1081. }
  1082. return 0;
  1083. }
  1084. static int enic_dev_hw_version(struct enic *enic,
  1085. enum vnic_dev_hw_version *hw_ver)
  1086. {
  1087. int err;
  1088. spin_lock(&enic->devcmd_lock);
  1089. err = vnic_dev_hw_version(enic->vdev, hw_ver);
  1090. spin_unlock(&enic->devcmd_lock);
  1091. return err;
  1092. }
  1093. static int enic_set_rq_alloc_buf(struct enic *enic)
  1094. {
  1095. enum vnic_dev_hw_version hw_ver;
  1096. int err;
  1097. err = enic_dev_hw_version(enic, &hw_ver);
  1098. if (err)
  1099. return err;
  1100. switch (hw_ver) {
  1101. case VNIC_DEV_HW_VER_A1:
  1102. enic->rq_alloc_buf = enic_rq_alloc_buf_a1;
  1103. break;
  1104. case VNIC_DEV_HW_VER_A2:
  1105. case VNIC_DEV_HW_VER_UNKNOWN:
  1106. enic->rq_alloc_buf = enic_rq_alloc_buf;
  1107. break;
  1108. default:
  1109. return -ENODEV;
  1110. }
  1111. return 0;
  1112. }
  1113. static void enic_rq_indicate_buf(struct vnic_rq *rq,
  1114. struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
  1115. int skipped, void *opaque)
  1116. {
  1117. struct enic *enic = vnic_dev_priv(rq->vdev);
  1118. struct net_device *netdev = enic->netdev;
  1119. struct sk_buff *skb;
  1120. u8 type, color, eop, sop, ingress_port, vlan_stripped;
  1121. u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
  1122. u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
  1123. u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
  1124. u8 packet_error;
  1125. u16 q_number, completed_index, bytes_written, vlan_tci, checksum;
  1126. u32 rss_hash;
  1127. if (skipped)
  1128. return;
  1129. skb = buf->os_buf;
  1130. prefetch(skb->data - NET_IP_ALIGN);
  1131. pci_unmap_single(enic->pdev, buf->dma_addr,
  1132. buf->len, PCI_DMA_FROMDEVICE);
  1133. cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
  1134. &type, &color, &q_number, &completed_index,
  1135. &ingress_port, &fcoe, &eop, &sop, &rss_type,
  1136. &csum_not_calc, &rss_hash, &bytes_written,
  1137. &packet_error, &vlan_stripped, &vlan_tci, &checksum,
  1138. &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
  1139. &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
  1140. &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
  1141. &fcs_ok);
  1142. if (packet_error) {
  1143. if (!fcs_ok) {
  1144. if (bytes_written > 0)
  1145. enic->rq_bad_fcs++;
  1146. else if (bytes_written == 0)
  1147. enic->rq_truncated_pkts++;
  1148. }
  1149. dev_kfree_skb_any(skb);
  1150. return;
  1151. }
  1152. if (eop && bytes_written > 0) {
  1153. /* Good receive
  1154. */
  1155. skb_put(skb, bytes_written);
  1156. skb->protocol = eth_type_trans(skb, netdev);
  1157. if (enic->csum_rx_enabled && !csum_not_calc) {
  1158. skb->csum = htons(checksum);
  1159. skb->ip_summed = CHECKSUM_COMPLETE;
  1160. }
  1161. skb->dev = netdev;
  1162. if (enic->vlan_group && vlan_stripped &&
  1163. (vlan_tci & CQ_ENET_RQ_DESC_VLAN_TCI_VLAN_MASK)) {
  1164. if (netdev->features & NETIF_F_GRO)
  1165. vlan_gro_receive(&enic->napi, enic->vlan_group,
  1166. vlan_tci, skb);
  1167. else
  1168. vlan_hwaccel_receive_skb(skb,
  1169. enic->vlan_group, vlan_tci);
  1170. } else {
  1171. if (netdev->features & NETIF_F_GRO)
  1172. napi_gro_receive(&enic->napi, skb);
  1173. else
  1174. netif_receive_skb(skb);
  1175. }
  1176. } else {
  1177. /* Buffer overflow
  1178. */
  1179. dev_kfree_skb_any(skb);
  1180. }
  1181. }
  1182. static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
  1183. u8 type, u16 q_number, u16 completed_index, void *opaque)
  1184. {
  1185. struct enic *enic = vnic_dev_priv(vdev);
  1186. vnic_rq_service(&enic->rq[q_number], cq_desc,
  1187. completed_index, VNIC_RQ_RETURN_DESC,
  1188. enic_rq_indicate_buf, opaque);
  1189. return 0;
  1190. }
  1191. static int enic_poll(struct napi_struct *napi, int budget)
  1192. {
  1193. struct enic *enic = container_of(napi, struct enic, napi);
  1194. unsigned int rq_work_to_do = budget;
  1195. unsigned int wq_work_to_do = -1; /* no limit */
  1196. unsigned int work_done, rq_work_done, wq_work_done;
  1197. int err;
  1198. /* Service RQ (first) and WQ
  1199. */
  1200. rq_work_done = vnic_cq_service(&enic->cq[ENIC_CQ_RQ],
  1201. rq_work_to_do, enic_rq_service, NULL);
  1202. wq_work_done = vnic_cq_service(&enic->cq[ENIC_CQ_WQ],
  1203. wq_work_to_do, enic_wq_service, NULL);
  1204. /* Accumulate intr event credits for this polling
  1205. * cycle. An intr event is the completion of a
  1206. * a WQ or RQ packet.
  1207. */
  1208. work_done = rq_work_done + wq_work_done;
  1209. if (work_done > 0)
  1210. vnic_intr_return_credits(&enic->intr[ENIC_INTX_WQ_RQ],
  1211. work_done,
  1212. 0 /* don't unmask intr */,
  1213. 0 /* don't reset intr timer */);
  1214. err = vnic_rq_fill(&enic->rq[0], enic->rq_alloc_buf);
  1215. /* Buffer allocation failed. Stay in polling
  1216. * mode so we can try to fill the ring again.
  1217. */
  1218. if (err)
  1219. rq_work_done = rq_work_to_do;
  1220. if (rq_work_done < rq_work_to_do) {
  1221. /* Some work done, but not enough to stay in polling,
  1222. * exit polling
  1223. */
  1224. napi_complete(napi);
  1225. vnic_intr_unmask(&enic->intr[ENIC_INTX_WQ_RQ]);
  1226. }
  1227. return rq_work_done;
  1228. }
  1229. static int enic_poll_msix(struct napi_struct *napi, int budget)
  1230. {
  1231. struct enic *enic = container_of(napi, struct enic, napi);
  1232. unsigned int work_to_do = budget;
  1233. unsigned int work_done;
  1234. int err;
  1235. /* Service RQ
  1236. */
  1237. work_done = vnic_cq_service(&enic->cq[ENIC_CQ_RQ],
  1238. work_to_do, enic_rq_service, NULL);
  1239. /* Return intr event credits for this polling
  1240. * cycle. An intr event is the completion of a
  1241. * RQ packet.
  1242. */
  1243. if (work_done > 0)
  1244. vnic_intr_return_credits(&enic->intr[ENIC_MSIX_RQ],
  1245. work_done,
  1246. 0 /* don't unmask intr */,
  1247. 0 /* don't reset intr timer */);
  1248. err = vnic_rq_fill(&enic->rq[0], enic->rq_alloc_buf);
  1249. /* Buffer allocation failed. Stay in polling mode
  1250. * so we can try to fill the ring again.
  1251. */
  1252. if (err)
  1253. work_done = work_to_do;
  1254. if (work_done < work_to_do) {
  1255. /* Some work done, but not enough to stay in polling,
  1256. * exit polling
  1257. */
  1258. napi_complete(napi);
  1259. vnic_intr_unmask(&enic->intr[ENIC_MSIX_RQ]);
  1260. }
  1261. return work_done;
  1262. }
  1263. static void enic_notify_timer(unsigned long data)
  1264. {
  1265. struct enic *enic = (struct enic *)data;
  1266. enic_notify_check(enic);
  1267. mod_timer(&enic->notify_timer,
  1268. round_jiffies(jiffies + ENIC_NOTIFY_TIMER_PERIOD));
  1269. }
  1270. static void enic_free_intr(struct enic *enic)
  1271. {
  1272. struct net_device *netdev = enic->netdev;
  1273. unsigned int i;
  1274. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  1275. case VNIC_DEV_INTR_MODE_INTX:
  1276. free_irq(enic->pdev->irq, netdev);
  1277. break;
  1278. case VNIC_DEV_INTR_MODE_MSI:
  1279. free_irq(enic->pdev->irq, enic);
  1280. break;
  1281. case VNIC_DEV_INTR_MODE_MSIX:
  1282. for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
  1283. if (enic->msix[i].requested)
  1284. free_irq(enic->msix_entry[i].vector,
  1285. enic->msix[i].devid);
  1286. break;
  1287. default:
  1288. break;
  1289. }
  1290. }
  1291. static int enic_request_intr(struct enic *enic)
  1292. {
  1293. struct net_device *netdev = enic->netdev;
  1294. unsigned int i;
  1295. int err = 0;
  1296. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  1297. case VNIC_DEV_INTR_MODE_INTX:
  1298. err = request_irq(enic->pdev->irq, enic_isr_legacy,
  1299. IRQF_SHARED, netdev->name, netdev);
  1300. break;
  1301. case VNIC_DEV_INTR_MODE_MSI:
  1302. err = request_irq(enic->pdev->irq, enic_isr_msi,
  1303. 0, netdev->name, enic);
  1304. break;
  1305. case VNIC_DEV_INTR_MODE_MSIX:
  1306. sprintf(enic->msix[ENIC_MSIX_RQ].devname,
  1307. "%.11s-rx-0", netdev->name);
  1308. enic->msix[ENIC_MSIX_RQ].isr = enic_isr_msix_rq;
  1309. enic->msix[ENIC_MSIX_RQ].devid = enic;
  1310. sprintf(enic->msix[ENIC_MSIX_WQ].devname,
  1311. "%.11s-tx-0", netdev->name);
  1312. enic->msix[ENIC_MSIX_WQ].isr = enic_isr_msix_wq;
  1313. enic->msix[ENIC_MSIX_WQ].devid = enic;
  1314. sprintf(enic->msix[ENIC_MSIX_ERR].devname,
  1315. "%.11s-err", netdev->name);
  1316. enic->msix[ENIC_MSIX_ERR].isr = enic_isr_msix_err;
  1317. enic->msix[ENIC_MSIX_ERR].devid = enic;
  1318. sprintf(enic->msix[ENIC_MSIX_NOTIFY].devname,
  1319. "%.11s-notify", netdev->name);
  1320. enic->msix[ENIC_MSIX_NOTIFY].isr = enic_isr_msix_notify;
  1321. enic->msix[ENIC_MSIX_NOTIFY].devid = enic;
  1322. for (i = 0; i < ARRAY_SIZE(enic->msix); i++) {
  1323. err = request_irq(enic->msix_entry[i].vector,
  1324. enic->msix[i].isr, 0,
  1325. enic->msix[i].devname,
  1326. enic->msix[i].devid);
  1327. if (err) {
  1328. enic_free_intr(enic);
  1329. break;
  1330. }
  1331. enic->msix[i].requested = 1;
  1332. }
  1333. break;
  1334. default:
  1335. break;
  1336. }
  1337. return err;
  1338. }
  1339. static void enic_synchronize_irqs(struct enic *enic)
  1340. {
  1341. unsigned int i;
  1342. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  1343. case VNIC_DEV_INTR_MODE_INTX:
  1344. case VNIC_DEV_INTR_MODE_MSI:
  1345. synchronize_irq(enic->pdev->irq);
  1346. break;
  1347. case VNIC_DEV_INTR_MODE_MSIX:
  1348. for (i = 0; i < enic->intr_count; i++)
  1349. synchronize_irq(enic->msix_entry[i].vector);
  1350. break;
  1351. default:
  1352. break;
  1353. }
  1354. }
  1355. static int enic_dev_notify_set(struct enic *enic)
  1356. {
  1357. int err;
  1358. spin_lock(&enic->devcmd_lock);
  1359. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  1360. case VNIC_DEV_INTR_MODE_INTX:
  1361. err = vnic_dev_notify_set(enic->vdev, ENIC_INTX_NOTIFY);
  1362. break;
  1363. case VNIC_DEV_INTR_MODE_MSIX:
  1364. err = vnic_dev_notify_set(enic->vdev, ENIC_MSIX_NOTIFY);
  1365. break;
  1366. default:
  1367. err = vnic_dev_notify_set(enic->vdev, -1 /* no intr */);
  1368. break;
  1369. }
  1370. spin_unlock(&enic->devcmd_lock);
  1371. return err;
  1372. }
  1373. static int enic_dev_notify_unset(struct enic *enic)
  1374. {
  1375. int err;
  1376. spin_lock(&enic->devcmd_lock);
  1377. err = vnic_dev_notify_unset(enic->vdev);
  1378. spin_unlock(&enic->devcmd_lock);
  1379. return err;
  1380. }
  1381. static int enic_dev_enable(struct enic *enic)
  1382. {
  1383. int err;
  1384. spin_lock(&enic->devcmd_lock);
  1385. err = vnic_dev_enable(enic->vdev);
  1386. spin_unlock(&enic->devcmd_lock);
  1387. return err;
  1388. }
  1389. static int enic_dev_disable(struct enic *enic)
  1390. {
  1391. int err;
  1392. spin_lock(&enic->devcmd_lock);
  1393. err = vnic_dev_disable(enic->vdev);
  1394. spin_unlock(&enic->devcmd_lock);
  1395. return err;
  1396. }
  1397. static void enic_notify_timer_start(struct enic *enic)
  1398. {
  1399. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  1400. case VNIC_DEV_INTR_MODE_MSI:
  1401. mod_timer(&enic->notify_timer, jiffies);
  1402. break;
  1403. default:
  1404. /* Using intr for notification for INTx/MSI-X */
  1405. break;
  1406. };
  1407. }
  1408. /* rtnl lock is held, process context */
  1409. static int enic_open(struct net_device *netdev)
  1410. {
  1411. struct enic *enic = netdev_priv(netdev);
  1412. unsigned int i;
  1413. int err;
  1414. err = enic_request_intr(enic);
  1415. if (err) {
  1416. netdev_err(netdev, "Unable to request irq.\n");
  1417. return err;
  1418. }
  1419. err = enic_dev_notify_set(enic);
  1420. if (err) {
  1421. netdev_err(netdev,
  1422. "Failed to alloc notify buffer, aborting.\n");
  1423. goto err_out_free_intr;
  1424. }
  1425. for (i = 0; i < enic->rq_count; i++) {
  1426. vnic_rq_fill(&enic->rq[i], enic->rq_alloc_buf);
  1427. /* Need at least one buffer on ring to get going */
  1428. if (vnic_rq_desc_used(&enic->rq[i]) == 0) {
  1429. netdev_err(netdev, "Unable to alloc receive buffers\n");
  1430. err = -ENOMEM;
  1431. goto err_out_notify_unset;
  1432. }
  1433. }
  1434. for (i = 0; i < enic->wq_count; i++)
  1435. vnic_wq_enable(&enic->wq[i]);
  1436. for (i = 0; i < enic->rq_count; i++)
  1437. vnic_rq_enable(&enic->rq[i]);
  1438. enic_dev_add_station_addr(enic);
  1439. enic_set_multicast_list(netdev);
  1440. netif_wake_queue(netdev);
  1441. napi_enable(&enic->napi);
  1442. enic_dev_enable(enic);
  1443. for (i = 0; i < enic->intr_count; i++)
  1444. vnic_intr_unmask(&enic->intr[i]);
  1445. enic_notify_timer_start(enic);
  1446. return 0;
  1447. err_out_notify_unset:
  1448. enic_dev_notify_unset(enic);
  1449. err_out_free_intr:
  1450. enic_free_intr(enic);
  1451. return err;
  1452. }
  1453. /* rtnl lock is held, process context */
  1454. static int enic_stop(struct net_device *netdev)
  1455. {
  1456. struct enic *enic = netdev_priv(netdev);
  1457. unsigned int i;
  1458. int err;
  1459. for (i = 0; i < enic->intr_count; i++) {
  1460. vnic_intr_mask(&enic->intr[i]);
  1461. (void)vnic_intr_masked(&enic->intr[i]); /* flush write */
  1462. }
  1463. enic_synchronize_irqs(enic);
  1464. del_timer_sync(&enic->notify_timer);
  1465. enic_dev_disable(enic);
  1466. napi_disable(&enic->napi);
  1467. netif_carrier_off(netdev);
  1468. netif_tx_disable(netdev);
  1469. enic_dev_del_station_addr(enic);
  1470. for (i = 0; i < enic->wq_count; i++) {
  1471. err = vnic_wq_disable(&enic->wq[i]);
  1472. if (err)
  1473. return err;
  1474. }
  1475. for (i = 0; i < enic->rq_count; i++) {
  1476. err = vnic_rq_disable(&enic->rq[i]);
  1477. if (err)
  1478. return err;
  1479. }
  1480. enic_dev_notify_unset(enic);
  1481. enic_free_intr(enic);
  1482. for (i = 0; i < enic->wq_count; i++)
  1483. vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
  1484. for (i = 0; i < enic->rq_count; i++)
  1485. vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
  1486. for (i = 0; i < enic->cq_count; i++)
  1487. vnic_cq_clean(&enic->cq[i]);
  1488. for (i = 0; i < enic->intr_count; i++)
  1489. vnic_intr_clean(&enic->intr[i]);
  1490. return 0;
  1491. }
  1492. static int enic_change_mtu(struct net_device *netdev, int new_mtu)
  1493. {
  1494. struct enic *enic = netdev_priv(netdev);
  1495. int running = netif_running(netdev);
  1496. if (new_mtu < ENIC_MIN_MTU || new_mtu > ENIC_MAX_MTU)
  1497. return -EINVAL;
  1498. if (running)
  1499. enic_stop(netdev);
  1500. netdev->mtu = new_mtu;
  1501. if (netdev->mtu > enic->port_mtu)
  1502. netdev_warn(netdev,
  1503. "interface MTU (%d) set higher than port MTU (%d)\n",
  1504. netdev->mtu, enic->port_mtu);
  1505. if (running)
  1506. enic_open(netdev);
  1507. return 0;
  1508. }
  1509. #ifdef CONFIG_NET_POLL_CONTROLLER
  1510. static void enic_poll_controller(struct net_device *netdev)
  1511. {
  1512. struct enic *enic = netdev_priv(netdev);
  1513. struct vnic_dev *vdev = enic->vdev;
  1514. switch (vnic_dev_get_intr_mode(vdev)) {
  1515. case VNIC_DEV_INTR_MODE_MSIX:
  1516. enic_isr_msix_rq(enic->pdev->irq, enic);
  1517. enic_isr_msix_wq(enic->pdev->irq, enic);
  1518. break;
  1519. case VNIC_DEV_INTR_MODE_MSI:
  1520. enic_isr_msi(enic->pdev->irq, enic);
  1521. break;
  1522. case VNIC_DEV_INTR_MODE_INTX:
  1523. enic_isr_legacy(enic->pdev->irq, netdev);
  1524. break;
  1525. default:
  1526. break;
  1527. }
  1528. }
  1529. #endif
  1530. static int enic_dev_wait(struct vnic_dev *vdev,
  1531. int (*start)(struct vnic_dev *, int),
  1532. int (*finished)(struct vnic_dev *, int *),
  1533. int arg)
  1534. {
  1535. unsigned long time;
  1536. int done;
  1537. int err;
  1538. BUG_ON(in_interrupt());
  1539. err = start(vdev, arg);
  1540. if (err)
  1541. return err;
  1542. /* Wait for func to complete...2 seconds max
  1543. */
  1544. time = jiffies + (HZ * 2);
  1545. do {
  1546. err = finished(vdev, &done);
  1547. if (err)
  1548. return err;
  1549. if (done)
  1550. return 0;
  1551. schedule_timeout_uninterruptible(HZ / 10);
  1552. } while (time_after(time, jiffies));
  1553. return -ETIMEDOUT;
  1554. }
  1555. static int enic_dev_open(struct enic *enic)
  1556. {
  1557. int err;
  1558. err = enic_dev_wait(enic->vdev, vnic_dev_open,
  1559. vnic_dev_open_done, 0);
  1560. if (err)
  1561. dev_err(enic_get_dev(enic), "vNIC device open failed, err %d\n",
  1562. err);
  1563. return err;
  1564. }
  1565. static int enic_dev_hang_reset(struct enic *enic)
  1566. {
  1567. int err;
  1568. err = enic_dev_wait(enic->vdev, vnic_dev_hang_reset,
  1569. vnic_dev_hang_reset_done, 0);
  1570. if (err)
  1571. netdev_err(enic->netdev, "vNIC hang reset failed, err %d\n",
  1572. err);
  1573. return err;
  1574. }
  1575. static int enic_set_niccfg(struct enic *enic)
  1576. {
  1577. const u8 rss_default_cpu = 0;
  1578. const u8 rss_hash_type = 0;
  1579. const u8 rss_hash_bits = 0;
  1580. const u8 rss_base_cpu = 0;
  1581. const u8 rss_enable = 0;
  1582. const u8 tso_ipid_split_en = 0;
  1583. const u8 ig_vlan_strip_en = 1;
  1584. int err;
  1585. /* Enable VLAN tag stripping. RSS not enabled (yet).
  1586. */
  1587. spin_lock(&enic->devcmd_lock);
  1588. err = enic_set_nic_cfg(enic,
  1589. rss_default_cpu, rss_hash_type,
  1590. rss_hash_bits, rss_base_cpu,
  1591. rss_enable, tso_ipid_split_en,
  1592. ig_vlan_strip_en);
  1593. spin_unlock(&enic->devcmd_lock);
  1594. return err;
  1595. }
  1596. static int enic_dev_hang_notify(struct enic *enic)
  1597. {
  1598. int err;
  1599. spin_lock(&enic->devcmd_lock);
  1600. err = vnic_dev_hang_notify(enic->vdev);
  1601. spin_unlock(&enic->devcmd_lock);
  1602. return err;
  1603. }
  1604. int enic_dev_set_ig_vlan_rewrite_mode(struct enic *enic)
  1605. {
  1606. int err;
  1607. spin_lock(&enic->devcmd_lock);
  1608. err = vnic_dev_set_ig_vlan_rewrite_mode(enic->vdev,
  1609. IG_VLAN_REWRITE_MODE_PRIORITY_TAG_DEFAULT_VLAN);
  1610. spin_unlock(&enic->devcmd_lock);
  1611. return err;
  1612. }
  1613. static void enic_reset(struct work_struct *work)
  1614. {
  1615. struct enic *enic = container_of(work, struct enic, reset);
  1616. if (!netif_running(enic->netdev))
  1617. return;
  1618. rtnl_lock();
  1619. enic_dev_hang_notify(enic);
  1620. enic_stop(enic->netdev);
  1621. enic_dev_hang_reset(enic);
  1622. enic_reset_multicast_list(enic);
  1623. enic_init_vnic_resources(enic);
  1624. enic_set_niccfg(enic);
  1625. enic_dev_set_ig_vlan_rewrite_mode(enic);
  1626. enic_open(enic->netdev);
  1627. rtnl_unlock();
  1628. }
  1629. static int enic_set_intr_mode(struct enic *enic)
  1630. {
  1631. unsigned int n = 1;
  1632. unsigned int m = 1;
  1633. unsigned int i;
  1634. /* Set interrupt mode (INTx, MSI, MSI-X) depending
  1635. * system capabilities.
  1636. *
  1637. * Try MSI-X first
  1638. *
  1639. * We need n RQs, m WQs, n+m CQs, and n+m+2 INTRs
  1640. * (the second to last INTR is used for WQ/RQ errors)
  1641. * (the last INTR is used for notifications)
  1642. */
  1643. BUG_ON(ARRAY_SIZE(enic->msix_entry) < n + m + 2);
  1644. for (i = 0; i < n + m + 2; i++)
  1645. enic->msix_entry[i].entry = i;
  1646. if (enic->config.intr_mode < 1 &&
  1647. enic->rq_count >= n &&
  1648. enic->wq_count >= m &&
  1649. enic->cq_count >= n + m &&
  1650. enic->intr_count >= n + m + 2 &&
  1651. !pci_enable_msix(enic->pdev, enic->msix_entry, n + m + 2)) {
  1652. enic->rq_count = n;
  1653. enic->wq_count = m;
  1654. enic->cq_count = n + m;
  1655. enic->intr_count = n + m + 2;
  1656. vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSIX);
  1657. return 0;
  1658. }
  1659. /* Next try MSI
  1660. *
  1661. * We need 1 RQ, 1 WQ, 2 CQs, and 1 INTR
  1662. */
  1663. if (enic->config.intr_mode < 2 &&
  1664. enic->rq_count >= 1 &&
  1665. enic->wq_count >= 1 &&
  1666. enic->cq_count >= 2 &&
  1667. enic->intr_count >= 1 &&
  1668. !pci_enable_msi(enic->pdev)) {
  1669. enic->rq_count = 1;
  1670. enic->wq_count = 1;
  1671. enic->cq_count = 2;
  1672. enic->intr_count = 1;
  1673. vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSI);
  1674. return 0;
  1675. }
  1676. /* Next try INTx
  1677. *
  1678. * We need 1 RQ, 1 WQ, 2 CQs, and 3 INTRs
  1679. * (the first INTR is used for WQ/RQ)
  1680. * (the second INTR is used for WQ/RQ errors)
  1681. * (the last INTR is used for notifications)
  1682. */
  1683. if (enic->config.intr_mode < 3 &&
  1684. enic->rq_count >= 1 &&
  1685. enic->wq_count >= 1 &&
  1686. enic->cq_count >= 2 &&
  1687. enic->intr_count >= 3) {
  1688. enic->rq_count = 1;
  1689. enic->wq_count = 1;
  1690. enic->cq_count = 2;
  1691. enic->intr_count = 3;
  1692. vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_INTX);
  1693. return 0;
  1694. }
  1695. vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
  1696. return -EINVAL;
  1697. }
  1698. static void enic_clear_intr_mode(struct enic *enic)
  1699. {
  1700. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  1701. case VNIC_DEV_INTR_MODE_MSIX:
  1702. pci_disable_msix(enic->pdev);
  1703. break;
  1704. case VNIC_DEV_INTR_MODE_MSI:
  1705. pci_disable_msi(enic->pdev);
  1706. break;
  1707. default:
  1708. break;
  1709. }
  1710. vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
  1711. }
  1712. static const struct net_device_ops enic_netdev_dynamic_ops = {
  1713. .ndo_open = enic_open,
  1714. .ndo_stop = enic_stop,
  1715. .ndo_start_xmit = enic_hard_start_xmit,
  1716. .ndo_get_stats = enic_get_stats,
  1717. .ndo_validate_addr = eth_validate_addr,
  1718. .ndo_set_multicast_list = enic_set_multicast_list,
  1719. .ndo_set_mac_address = enic_set_mac_address_dynamic,
  1720. .ndo_change_mtu = enic_change_mtu,
  1721. .ndo_vlan_rx_register = enic_vlan_rx_register,
  1722. .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
  1723. .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
  1724. .ndo_tx_timeout = enic_tx_timeout,
  1725. .ndo_set_vf_port = enic_set_vf_port,
  1726. .ndo_get_vf_port = enic_get_vf_port,
  1727. #ifdef CONFIG_NET_POLL_CONTROLLER
  1728. .ndo_poll_controller = enic_poll_controller,
  1729. #endif
  1730. };
  1731. static const struct net_device_ops enic_netdev_ops = {
  1732. .ndo_open = enic_open,
  1733. .ndo_stop = enic_stop,
  1734. .ndo_start_xmit = enic_hard_start_xmit,
  1735. .ndo_get_stats = enic_get_stats,
  1736. .ndo_validate_addr = eth_validate_addr,
  1737. .ndo_set_mac_address = enic_set_mac_address,
  1738. .ndo_set_multicast_list = enic_set_multicast_list,
  1739. .ndo_change_mtu = enic_change_mtu,
  1740. .ndo_vlan_rx_register = enic_vlan_rx_register,
  1741. .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
  1742. .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
  1743. .ndo_tx_timeout = enic_tx_timeout,
  1744. #ifdef CONFIG_NET_POLL_CONTROLLER
  1745. .ndo_poll_controller = enic_poll_controller,
  1746. #endif
  1747. };
  1748. void enic_dev_deinit(struct enic *enic)
  1749. {
  1750. netif_napi_del(&enic->napi);
  1751. enic_free_vnic_resources(enic);
  1752. enic_clear_intr_mode(enic);
  1753. }
  1754. static int enic_dev_stats_clear(struct enic *enic)
  1755. {
  1756. int err;
  1757. spin_lock(&enic->devcmd_lock);
  1758. err = vnic_dev_stats_clear(enic->vdev);
  1759. spin_unlock(&enic->devcmd_lock);
  1760. return err;
  1761. }
  1762. int enic_dev_init(struct enic *enic)
  1763. {
  1764. struct device *dev = enic_get_dev(enic);
  1765. struct net_device *netdev = enic->netdev;
  1766. int err;
  1767. /* Get vNIC configuration
  1768. */
  1769. err = enic_get_vnic_config(enic);
  1770. if (err) {
  1771. dev_err(dev, "Get vNIC configuration failed, aborting\n");
  1772. return err;
  1773. }
  1774. /* Get available resource counts
  1775. */
  1776. enic_get_res_counts(enic);
  1777. /* Set interrupt mode based on resource counts and system
  1778. * capabilities
  1779. */
  1780. err = enic_set_intr_mode(enic);
  1781. if (err) {
  1782. dev_err(dev, "Failed to set intr mode based on resource "
  1783. "counts and system capabilities, aborting\n");
  1784. return err;
  1785. }
  1786. /* Allocate and configure vNIC resources
  1787. */
  1788. err = enic_alloc_vnic_resources(enic);
  1789. if (err) {
  1790. dev_err(dev, "Failed to alloc vNIC resources, aborting\n");
  1791. goto err_out_free_vnic_resources;
  1792. }
  1793. enic_init_vnic_resources(enic);
  1794. /* Clear LIF stats
  1795. */
  1796. enic_dev_stats_clear(enic);
  1797. err = enic_set_rq_alloc_buf(enic);
  1798. if (err) {
  1799. dev_err(dev, "Failed to set RQ buffer allocator, aborting\n");
  1800. goto err_out_free_vnic_resources;
  1801. }
  1802. err = enic_set_niccfg(enic);
  1803. if (err) {
  1804. dev_err(dev, "Failed to config nic, aborting\n");
  1805. goto err_out_free_vnic_resources;
  1806. }
  1807. err = enic_dev_set_ig_vlan_rewrite_mode(enic);
  1808. if (err) {
  1809. netdev_err(netdev,
  1810. "Failed to set ingress vlan rewrite mode, aborting.\n");
  1811. goto err_out_free_vnic_resources;
  1812. }
  1813. switch (vnic_dev_get_intr_mode(enic->vdev)) {
  1814. default:
  1815. netif_napi_add(netdev, &enic->napi, enic_poll, 64);
  1816. break;
  1817. case VNIC_DEV_INTR_MODE_MSIX:
  1818. netif_napi_add(netdev, &enic->napi, enic_poll_msix, 64);
  1819. break;
  1820. }
  1821. return 0;
  1822. err_out_free_vnic_resources:
  1823. enic_clear_intr_mode(enic);
  1824. enic_free_vnic_resources(enic);
  1825. return err;
  1826. }
  1827. static void enic_iounmap(struct enic *enic)
  1828. {
  1829. unsigned int i;
  1830. for (i = 0; i < ARRAY_SIZE(enic->bar); i++)
  1831. if (enic->bar[i].vaddr)
  1832. iounmap(enic->bar[i].vaddr);
  1833. }
  1834. static int __devinit enic_probe(struct pci_dev *pdev,
  1835. const struct pci_device_id *ent)
  1836. {
  1837. struct device *dev = &pdev->dev;
  1838. struct net_device *netdev;
  1839. struct enic *enic;
  1840. int using_dac = 0;
  1841. unsigned int i;
  1842. int err;
  1843. /* Allocate net device structure and initialize. Private
  1844. * instance data is initialized to zero.
  1845. */
  1846. netdev = alloc_etherdev(sizeof(struct enic));
  1847. if (!netdev) {
  1848. pr_err("Etherdev alloc failed, aborting\n");
  1849. return -ENOMEM;
  1850. }
  1851. pci_set_drvdata(pdev, netdev);
  1852. SET_NETDEV_DEV(netdev, &pdev->dev);
  1853. enic = netdev_priv(netdev);
  1854. enic->netdev = netdev;
  1855. enic->pdev = pdev;
  1856. /* Setup PCI resources
  1857. */
  1858. err = pci_enable_device_mem(pdev);
  1859. if (err) {
  1860. dev_err(dev, "Cannot enable PCI device, aborting\n");
  1861. goto err_out_free_netdev;
  1862. }
  1863. err = pci_request_regions(pdev, DRV_NAME);
  1864. if (err) {
  1865. dev_err(dev, "Cannot request PCI regions, aborting\n");
  1866. goto err_out_disable_device;
  1867. }
  1868. pci_set_master(pdev);
  1869. /* Query PCI controller on system for DMA addressing
  1870. * limitation for the device. Try 40-bit first, and
  1871. * fail to 32-bit.
  1872. */
  1873. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40));
  1874. if (err) {
  1875. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1876. if (err) {
  1877. dev_err(dev, "No usable DMA configuration, aborting\n");
  1878. goto err_out_release_regions;
  1879. }
  1880. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  1881. if (err) {
  1882. dev_err(dev, "Unable to obtain %u-bit DMA "
  1883. "for consistent allocations, aborting\n", 32);
  1884. goto err_out_release_regions;
  1885. }
  1886. } else {
  1887. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
  1888. if (err) {
  1889. dev_err(dev, "Unable to obtain %u-bit DMA "
  1890. "for consistent allocations, aborting\n", 40);
  1891. goto err_out_release_regions;
  1892. }
  1893. using_dac = 1;
  1894. }
  1895. /* Map vNIC resources from BAR0-5
  1896. */
  1897. for (i = 0; i < ARRAY_SIZE(enic->bar); i++) {
  1898. if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
  1899. continue;
  1900. enic->bar[i].len = pci_resource_len(pdev, i);
  1901. enic->bar[i].vaddr = pci_iomap(pdev, i, enic->bar[i].len);
  1902. if (!enic->bar[i].vaddr) {
  1903. dev_err(dev, "Cannot memory-map BAR %d, aborting\n", i);
  1904. err = -ENODEV;
  1905. goto err_out_iounmap;
  1906. }
  1907. enic->bar[i].bus_addr = pci_resource_start(pdev, i);
  1908. }
  1909. /* Register vNIC device
  1910. */
  1911. enic->vdev = vnic_dev_register(NULL, enic, pdev, enic->bar,
  1912. ARRAY_SIZE(enic->bar));
  1913. if (!enic->vdev) {
  1914. dev_err(dev, "vNIC registration failed, aborting\n");
  1915. err = -ENODEV;
  1916. goto err_out_iounmap;
  1917. }
  1918. /* Issue device open to get device in known state
  1919. */
  1920. err = enic_dev_open(enic);
  1921. if (err) {
  1922. dev_err(dev, "vNIC dev open failed, aborting\n");
  1923. goto err_out_vnic_unregister;
  1924. }
  1925. /* Issue device init to initialize the vnic-to-switch link.
  1926. * We'll start with carrier off and wait for link UP
  1927. * notification later to turn on carrier. We don't need
  1928. * to wait here for the vnic-to-switch link initialization
  1929. * to complete; link UP notification is the indication that
  1930. * the process is complete.
  1931. */
  1932. netif_carrier_off(netdev);
  1933. /* Do not call dev_init for a dynamic vnic.
  1934. * For a dynamic vnic, init_prov_info will be
  1935. * called later by an upper layer.
  1936. */
  1937. if (!enic_is_dynamic(enic)) {
  1938. err = vnic_dev_init(enic->vdev, 0);
  1939. if (err) {
  1940. dev_err(dev, "vNIC dev init failed, aborting\n");
  1941. goto err_out_dev_close;
  1942. }
  1943. }
  1944. /* Setup devcmd lock
  1945. */
  1946. spin_lock_init(&enic->devcmd_lock);
  1947. err = enic_dev_init(enic);
  1948. if (err) {
  1949. dev_err(dev, "Device initialization failed, aborting\n");
  1950. goto err_out_dev_close;
  1951. }
  1952. /* Setup notification timer, HW reset task, and wq locks
  1953. */
  1954. init_timer(&enic->notify_timer);
  1955. enic->notify_timer.function = enic_notify_timer;
  1956. enic->notify_timer.data = (unsigned long)enic;
  1957. INIT_WORK(&enic->reset, enic_reset);
  1958. for (i = 0; i < enic->wq_count; i++)
  1959. spin_lock_init(&enic->wq_lock[i]);
  1960. /* Register net device
  1961. */
  1962. enic->port_mtu = enic->config.mtu;
  1963. (void)enic_change_mtu(netdev, enic->port_mtu);
  1964. err = enic_set_mac_addr(netdev, enic->mac_addr);
  1965. if (err) {
  1966. dev_err(dev, "Invalid MAC address, aborting\n");
  1967. goto err_out_dev_deinit;
  1968. }
  1969. enic->tx_coalesce_usecs = enic->config.intr_timer_usec;
  1970. enic->rx_coalesce_usecs = enic->tx_coalesce_usecs;
  1971. if (enic_is_dynamic(enic))
  1972. netdev->netdev_ops = &enic_netdev_dynamic_ops;
  1973. else
  1974. netdev->netdev_ops = &enic_netdev_ops;
  1975. netdev->watchdog_timeo = 2 * HZ;
  1976. netdev->ethtool_ops = &enic_ethtool_ops;
  1977. netdev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  1978. if (ENIC_SETTING(enic, LOOP)) {
  1979. netdev->features &= ~NETIF_F_HW_VLAN_TX;
  1980. enic->loop_enable = 1;
  1981. enic->loop_tag = enic->config.loop_tag;
  1982. dev_info(dev, "loopback tag=0x%04x\n", enic->loop_tag);
  1983. }
  1984. if (ENIC_SETTING(enic, TXCSUM))
  1985. netdev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
  1986. if (ENIC_SETTING(enic, TSO))
  1987. netdev->features |= NETIF_F_TSO |
  1988. NETIF_F_TSO6 | NETIF_F_TSO_ECN;
  1989. if (ENIC_SETTING(enic, LRO))
  1990. netdev->features |= NETIF_F_GRO;
  1991. if (using_dac)
  1992. netdev->features |= NETIF_F_HIGHDMA;
  1993. enic->csum_rx_enabled = ENIC_SETTING(enic, RXCSUM);
  1994. err = register_netdev(netdev);
  1995. if (err) {
  1996. dev_err(dev, "Cannot register net device, aborting\n");
  1997. goto err_out_dev_deinit;
  1998. }
  1999. return 0;
  2000. err_out_dev_deinit:
  2001. enic_dev_deinit(enic);
  2002. err_out_dev_close:
  2003. vnic_dev_close(enic->vdev);
  2004. err_out_vnic_unregister:
  2005. vnic_dev_unregister(enic->vdev);
  2006. err_out_iounmap:
  2007. enic_iounmap(enic);
  2008. err_out_release_regions:
  2009. pci_release_regions(pdev);
  2010. err_out_disable_device:
  2011. pci_disable_device(pdev);
  2012. err_out_free_netdev:
  2013. pci_set_drvdata(pdev, NULL);
  2014. free_netdev(netdev);
  2015. return err;
  2016. }
  2017. static void __devexit enic_remove(struct pci_dev *pdev)
  2018. {
  2019. struct net_device *netdev = pci_get_drvdata(pdev);
  2020. if (netdev) {
  2021. struct enic *enic = netdev_priv(netdev);
  2022. flush_scheduled_work();
  2023. unregister_netdev(netdev);
  2024. enic_dev_deinit(enic);
  2025. vnic_dev_close(enic->vdev);
  2026. vnic_dev_unregister(enic->vdev);
  2027. enic_iounmap(enic);
  2028. pci_release_regions(pdev);
  2029. pci_disable_device(pdev);
  2030. pci_set_drvdata(pdev, NULL);
  2031. free_netdev(netdev);
  2032. }
  2033. }
  2034. static struct pci_driver enic_driver = {
  2035. .name = DRV_NAME,
  2036. .id_table = enic_id_table,
  2037. .probe = enic_probe,
  2038. .remove = __devexit_p(enic_remove),
  2039. };
  2040. static int __init enic_init_module(void)
  2041. {
  2042. pr_info("%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
  2043. return pci_register_driver(&enic_driver);
  2044. }
  2045. static void __exit enic_cleanup_module(void)
  2046. {
  2047. pci_unregister_driver(&enic_driver);
  2048. }
  2049. module_init(enic_init_module);
  2050. module_exit(enic_cleanup_module);