enic_main.c 63 KB

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