enic_main.c 64 KB

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