enic_main.c 61 KB

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