enic_main.c 70 KB

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