enic_main.c 63 KB

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