enic_main.c 66 KB

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