enic_main.c 59 KB

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