myri10ge.c 111 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096
  1. /*************************************************************************
  2. * myri10ge.c: Myricom Myri-10G Ethernet driver.
  3. *
  4. * Copyright (C) 2005 - 2011 Myricom, Inc.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of Myricom, Inc. nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. *
  32. * If the eeprom on your board is not recent enough, you will need to get a
  33. * newer firmware image at:
  34. * http://www.myri.com/scs/download-Myri10GE.html
  35. *
  36. * Contact Information:
  37. * <help@myri.com>
  38. * Myricom, Inc., 325N Santa Anita Avenue, Arcadia, CA 91006
  39. *************************************************************************/
  40. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  41. #include <linux/tcp.h>
  42. #include <linux/netdevice.h>
  43. #include <linux/skbuff.h>
  44. #include <linux/string.h>
  45. #include <linux/module.h>
  46. #include <linux/pci.h>
  47. #include <linux/dma-mapping.h>
  48. #include <linux/etherdevice.h>
  49. #include <linux/if_ether.h>
  50. #include <linux/if_vlan.h>
  51. #include <linux/dca.h>
  52. #include <linux/ip.h>
  53. #include <linux/inet.h>
  54. #include <linux/in.h>
  55. #include <linux/ethtool.h>
  56. #include <linux/firmware.h>
  57. #include <linux/delay.h>
  58. #include <linux/timer.h>
  59. #include <linux/vmalloc.h>
  60. #include <linux/crc32.h>
  61. #include <linux/moduleparam.h>
  62. #include <linux/io.h>
  63. #include <linux/log2.h>
  64. #include <linux/slab.h>
  65. #include <linux/prefetch.h>
  66. #include <net/checksum.h>
  67. #include <net/ip.h>
  68. #include <net/tcp.h>
  69. #include <asm/byteorder.h>
  70. #include <asm/processor.h>
  71. #include <net/busy_poll.h>
  72. #include "myri10ge_mcp.h"
  73. #include "myri10ge_mcp_gen_header.h"
  74. #define MYRI10GE_VERSION_STR "1.5.3-1.534"
  75. MODULE_DESCRIPTION("Myricom 10G driver (10GbE)");
  76. MODULE_AUTHOR("Maintainer: help@myri.com");
  77. MODULE_VERSION(MYRI10GE_VERSION_STR);
  78. MODULE_LICENSE("Dual BSD/GPL");
  79. #define MYRI10GE_MAX_ETHER_MTU 9014
  80. #define MYRI10GE_ETH_STOPPED 0
  81. #define MYRI10GE_ETH_STOPPING 1
  82. #define MYRI10GE_ETH_STARTING 2
  83. #define MYRI10GE_ETH_RUNNING 3
  84. #define MYRI10GE_ETH_OPEN_FAILED 4
  85. #define MYRI10GE_EEPROM_STRINGS_SIZE 256
  86. #define MYRI10GE_MAX_SEND_DESC_TSO ((65536 / 2048) * 2)
  87. #define MYRI10GE_NO_CONFIRM_DATA htonl(0xffffffff)
  88. #define MYRI10GE_NO_RESPONSE_RESULT 0xffffffff
  89. #define MYRI10GE_ALLOC_ORDER 0
  90. #define MYRI10GE_ALLOC_SIZE ((1 << MYRI10GE_ALLOC_ORDER) * PAGE_SIZE)
  91. #define MYRI10GE_MAX_FRAGS_PER_FRAME (MYRI10GE_MAX_ETHER_MTU/MYRI10GE_ALLOC_SIZE + 1)
  92. #define MYRI10GE_MAX_SLICES 32
  93. struct myri10ge_rx_buffer_state {
  94. struct page *page;
  95. int page_offset;
  96. DEFINE_DMA_UNMAP_ADDR(bus);
  97. DEFINE_DMA_UNMAP_LEN(len);
  98. };
  99. struct myri10ge_tx_buffer_state {
  100. struct sk_buff *skb;
  101. int last;
  102. DEFINE_DMA_UNMAP_ADDR(bus);
  103. DEFINE_DMA_UNMAP_LEN(len);
  104. };
  105. struct myri10ge_cmd {
  106. u32 data0;
  107. u32 data1;
  108. u32 data2;
  109. };
  110. struct myri10ge_rx_buf {
  111. struct mcp_kreq_ether_recv __iomem *lanai; /* lanai ptr for recv ring */
  112. struct mcp_kreq_ether_recv *shadow; /* host shadow of recv ring */
  113. struct myri10ge_rx_buffer_state *info;
  114. struct page *page;
  115. dma_addr_t bus;
  116. int page_offset;
  117. int cnt;
  118. int fill_cnt;
  119. int alloc_fail;
  120. int mask; /* number of rx slots -1 */
  121. int watchdog_needed;
  122. };
  123. struct myri10ge_tx_buf {
  124. struct mcp_kreq_ether_send __iomem *lanai; /* lanai ptr for sendq */
  125. __be32 __iomem *send_go; /* "go" doorbell ptr */
  126. __be32 __iomem *send_stop; /* "stop" doorbell ptr */
  127. struct mcp_kreq_ether_send *req_list; /* host shadow of sendq */
  128. char *req_bytes;
  129. struct myri10ge_tx_buffer_state *info;
  130. int mask; /* number of transmit slots -1 */
  131. int req ____cacheline_aligned; /* transmit slots submitted */
  132. int pkt_start; /* packets started */
  133. int stop_queue;
  134. int linearized;
  135. int done ____cacheline_aligned; /* transmit slots completed */
  136. int pkt_done; /* packets completed */
  137. int wake_queue;
  138. int queue_active;
  139. };
  140. struct myri10ge_rx_done {
  141. struct mcp_slot *entry;
  142. dma_addr_t bus;
  143. int cnt;
  144. int idx;
  145. };
  146. struct myri10ge_slice_netstats {
  147. unsigned long rx_packets;
  148. unsigned long tx_packets;
  149. unsigned long rx_bytes;
  150. unsigned long tx_bytes;
  151. unsigned long rx_dropped;
  152. unsigned long tx_dropped;
  153. };
  154. struct myri10ge_slice_state {
  155. struct myri10ge_tx_buf tx; /* transmit ring */
  156. struct myri10ge_rx_buf rx_small;
  157. struct myri10ge_rx_buf rx_big;
  158. struct myri10ge_rx_done rx_done;
  159. struct net_device *dev;
  160. struct napi_struct napi;
  161. struct myri10ge_priv *mgp;
  162. struct myri10ge_slice_netstats stats;
  163. __be32 __iomem *irq_claim;
  164. struct mcp_irq_data *fw_stats;
  165. dma_addr_t fw_stats_bus;
  166. int watchdog_tx_done;
  167. int watchdog_tx_req;
  168. int watchdog_rx_done;
  169. int stuck;
  170. #ifdef CONFIG_MYRI10GE_DCA
  171. int cached_dca_tag;
  172. int cpu;
  173. __be32 __iomem *dca_tag;
  174. #endif
  175. char irq_desc[32];
  176. };
  177. struct myri10ge_priv {
  178. struct myri10ge_slice_state *ss;
  179. int tx_boundary; /* boundary transmits cannot cross */
  180. int num_slices;
  181. int running; /* running? */
  182. int small_bytes;
  183. int big_bytes;
  184. int max_intr_slots;
  185. struct net_device *dev;
  186. u8 __iomem *sram;
  187. int sram_size;
  188. unsigned long board_span;
  189. unsigned long iomem_base;
  190. __be32 __iomem *irq_deassert;
  191. char *mac_addr_string;
  192. struct mcp_cmd_response *cmd;
  193. dma_addr_t cmd_bus;
  194. struct pci_dev *pdev;
  195. int msi_enabled;
  196. int msix_enabled;
  197. struct msix_entry *msix_vectors;
  198. #ifdef CONFIG_MYRI10GE_DCA
  199. int dca_enabled;
  200. int relaxed_order;
  201. #endif
  202. u32 link_state;
  203. unsigned int rdma_tags_available;
  204. int intr_coal_delay;
  205. __be32 __iomem *intr_coal_delay_ptr;
  206. int wc_cookie;
  207. int down_cnt;
  208. wait_queue_head_t down_wq;
  209. struct work_struct watchdog_work;
  210. struct timer_list watchdog_timer;
  211. int watchdog_resets;
  212. int watchdog_pause;
  213. int pause;
  214. bool fw_name_allocated;
  215. char *fw_name;
  216. char eeprom_strings[MYRI10GE_EEPROM_STRINGS_SIZE];
  217. char *product_code_string;
  218. char fw_version[128];
  219. int fw_ver_major;
  220. int fw_ver_minor;
  221. int fw_ver_tiny;
  222. int adopted_rx_filter_bug;
  223. u8 mac_addr[ETH_ALEN]; /* eeprom mac address */
  224. unsigned long serial_number;
  225. int vendor_specific_offset;
  226. int fw_multicast_support;
  227. u32 features;
  228. u32 max_tso6;
  229. u32 read_dma;
  230. u32 write_dma;
  231. u32 read_write_dma;
  232. u32 link_changes;
  233. u32 msg_enable;
  234. unsigned int board_number;
  235. int rebooted;
  236. };
  237. static char *myri10ge_fw_unaligned = "myri10ge_ethp_z8e.dat";
  238. static char *myri10ge_fw_aligned = "myri10ge_eth_z8e.dat";
  239. static char *myri10ge_fw_rss_unaligned = "myri10ge_rss_ethp_z8e.dat";
  240. static char *myri10ge_fw_rss_aligned = "myri10ge_rss_eth_z8e.dat";
  241. MODULE_FIRMWARE("myri10ge_ethp_z8e.dat");
  242. MODULE_FIRMWARE("myri10ge_eth_z8e.dat");
  243. MODULE_FIRMWARE("myri10ge_rss_ethp_z8e.dat");
  244. MODULE_FIRMWARE("myri10ge_rss_eth_z8e.dat");
  245. /* Careful: must be accessed under kernel_param_lock() */
  246. static char *myri10ge_fw_name = NULL;
  247. module_param(myri10ge_fw_name, charp, 0644);
  248. MODULE_PARM_DESC(myri10ge_fw_name, "Firmware image name");
  249. #define MYRI10GE_MAX_BOARDS 8
  250. static char *myri10ge_fw_names[MYRI10GE_MAX_BOARDS] =
  251. {[0 ... (MYRI10GE_MAX_BOARDS - 1)] = NULL };
  252. module_param_array_named(myri10ge_fw_names, myri10ge_fw_names, charp, NULL,
  253. 0444);
  254. MODULE_PARM_DESC(myri10ge_fw_names, "Firmware image names per board");
  255. static int myri10ge_ecrc_enable = 1;
  256. module_param(myri10ge_ecrc_enable, int, 0444);
  257. MODULE_PARM_DESC(myri10ge_ecrc_enable, "Enable Extended CRC on PCI-E");
  258. static int myri10ge_small_bytes = -1; /* -1 == auto */
  259. module_param(myri10ge_small_bytes, int, 0644);
  260. MODULE_PARM_DESC(myri10ge_small_bytes, "Threshold of small packets");
  261. static int myri10ge_msi = 1; /* enable msi by default */
  262. module_param(myri10ge_msi, int, 0644);
  263. MODULE_PARM_DESC(myri10ge_msi, "Enable Message Signalled Interrupts");
  264. static int myri10ge_intr_coal_delay = 75;
  265. module_param(myri10ge_intr_coal_delay, int, 0444);
  266. MODULE_PARM_DESC(myri10ge_intr_coal_delay, "Interrupt coalescing delay");
  267. static int myri10ge_flow_control = 1;
  268. module_param(myri10ge_flow_control, int, 0444);
  269. MODULE_PARM_DESC(myri10ge_flow_control, "Pause parameter");
  270. static int myri10ge_deassert_wait = 1;
  271. module_param(myri10ge_deassert_wait, int, 0644);
  272. MODULE_PARM_DESC(myri10ge_deassert_wait,
  273. "Wait when deasserting legacy interrupts");
  274. static int myri10ge_force_firmware = 0;
  275. module_param(myri10ge_force_firmware, int, 0444);
  276. MODULE_PARM_DESC(myri10ge_force_firmware,
  277. "Force firmware to assume aligned completions");
  278. static int myri10ge_initial_mtu = MYRI10GE_MAX_ETHER_MTU - ETH_HLEN;
  279. module_param(myri10ge_initial_mtu, int, 0444);
  280. MODULE_PARM_DESC(myri10ge_initial_mtu, "Initial MTU");
  281. static int myri10ge_napi_weight = 64;
  282. module_param(myri10ge_napi_weight, int, 0444);
  283. MODULE_PARM_DESC(myri10ge_napi_weight, "Set NAPI weight");
  284. static int myri10ge_watchdog_timeout = 1;
  285. module_param(myri10ge_watchdog_timeout, int, 0444);
  286. MODULE_PARM_DESC(myri10ge_watchdog_timeout, "Set watchdog timeout");
  287. static int myri10ge_max_irq_loops = 1048576;
  288. module_param(myri10ge_max_irq_loops, int, 0444);
  289. MODULE_PARM_DESC(myri10ge_max_irq_loops,
  290. "Set stuck legacy IRQ detection threshold");
  291. #define MYRI10GE_MSG_DEFAULT NETIF_MSG_LINK
  292. static int myri10ge_debug = -1; /* defaults above */
  293. module_param(myri10ge_debug, int, 0);
  294. MODULE_PARM_DESC(myri10ge_debug, "Debug level (0=none,...,16=all)");
  295. static int myri10ge_fill_thresh = 256;
  296. module_param(myri10ge_fill_thresh, int, 0644);
  297. MODULE_PARM_DESC(myri10ge_fill_thresh, "Number of empty rx slots allowed");
  298. static int myri10ge_reset_recover = 1;
  299. static int myri10ge_max_slices = 1;
  300. module_param(myri10ge_max_slices, int, 0444);
  301. MODULE_PARM_DESC(myri10ge_max_slices, "Max tx/rx queues");
  302. static int myri10ge_rss_hash = MXGEFW_RSS_HASH_TYPE_SRC_DST_PORT;
  303. module_param(myri10ge_rss_hash, int, 0444);
  304. MODULE_PARM_DESC(myri10ge_rss_hash, "Type of RSS hashing to do");
  305. static int myri10ge_dca = 1;
  306. module_param(myri10ge_dca, int, 0444);
  307. MODULE_PARM_DESC(myri10ge_dca, "Enable DCA if possible");
  308. #define MYRI10GE_FW_OFFSET 1024*1024
  309. #define MYRI10GE_HIGHPART_TO_U32(X) \
  310. (sizeof (X) == 8) ? ((u32)((u64)(X) >> 32)) : (0)
  311. #define MYRI10GE_LOWPART_TO_U32(X) ((u32)(X))
  312. #define myri10ge_pio_copy(to,from,size) __iowrite64_copy(to,from,size/8)
  313. static void myri10ge_set_multicast_list(struct net_device *dev);
  314. static netdev_tx_t myri10ge_sw_tso(struct sk_buff *skb,
  315. struct net_device *dev);
  316. static inline void put_be32(__be32 val, __be32 __iomem * p)
  317. {
  318. __raw_writel((__force __u32) val, (__force void __iomem *)p);
  319. }
  320. static void myri10ge_get_stats(struct net_device *dev,
  321. struct rtnl_link_stats64 *stats);
  322. static void set_fw_name(struct myri10ge_priv *mgp, char *name, bool allocated)
  323. {
  324. if (mgp->fw_name_allocated)
  325. kfree(mgp->fw_name);
  326. mgp->fw_name = name;
  327. mgp->fw_name_allocated = allocated;
  328. }
  329. static int
  330. myri10ge_send_cmd(struct myri10ge_priv *mgp, u32 cmd,
  331. struct myri10ge_cmd *data, int atomic)
  332. {
  333. struct mcp_cmd *buf;
  334. char buf_bytes[sizeof(*buf) + 8];
  335. struct mcp_cmd_response *response = mgp->cmd;
  336. char __iomem *cmd_addr = mgp->sram + MXGEFW_ETH_CMD;
  337. u32 dma_low, dma_high, result, value;
  338. int sleep_total = 0;
  339. /* ensure buf is aligned to 8 bytes */
  340. buf = (struct mcp_cmd *)ALIGN((unsigned long)buf_bytes, 8);
  341. buf->data0 = htonl(data->data0);
  342. buf->data1 = htonl(data->data1);
  343. buf->data2 = htonl(data->data2);
  344. buf->cmd = htonl(cmd);
  345. dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
  346. dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
  347. buf->response_addr.low = htonl(dma_low);
  348. buf->response_addr.high = htonl(dma_high);
  349. response->result = htonl(MYRI10GE_NO_RESPONSE_RESULT);
  350. mb();
  351. myri10ge_pio_copy(cmd_addr, buf, sizeof(*buf));
  352. /* wait up to 15ms. Longest command is the DMA benchmark,
  353. * which is capped at 5ms, but runs from a timeout handler
  354. * that runs every 7.8ms. So a 15ms timeout leaves us with
  355. * a 2.2ms margin
  356. */
  357. if (atomic) {
  358. /* if atomic is set, do not sleep,
  359. * and try to get the completion quickly
  360. * (1ms will be enough for those commands) */
  361. for (sleep_total = 0;
  362. sleep_total < 1000 &&
  363. response->result == htonl(MYRI10GE_NO_RESPONSE_RESULT);
  364. sleep_total += 10) {
  365. udelay(10);
  366. mb();
  367. }
  368. } else {
  369. /* use msleep for most command */
  370. for (sleep_total = 0;
  371. sleep_total < 15 &&
  372. response->result == htonl(MYRI10GE_NO_RESPONSE_RESULT);
  373. sleep_total++)
  374. msleep(1);
  375. }
  376. result = ntohl(response->result);
  377. value = ntohl(response->data);
  378. if (result != MYRI10GE_NO_RESPONSE_RESULT) {
  379. if (result == 0) {
  380. data->data0 = value;
  381. return 0;
  382. } else if (result == MXGEFW_CMD_UNKNOWN) {
  383. return -ENOSYS;
  384. } else if (result == MXGEFW_CMD_ERROR_UNALIGNED) {
  385. return -E2BIG;
  386. } else if (result == MXGEFW_CMD_ERROR_RANGE &&
  387. cmd == MXGEFW_CMD_ENABLE_RSS_QUEUES &&
  388. (data->
  389. data1 & MXGEFW_SLICE_ENABLE_MULTIPLE_TX_QUEUES) !=
  390. 0) {
  391. return -ERANGE;
  392. } else {
  393. dev_err(&mgp->pdev->dev,
  394. "command %d failed, result = %d\n",
  395. cmd, result);
  396. return -ENXIO;
  397. }
  398. }
  399. dev_err(&mgp->pdev->dev, "command %d timed out, result = %d\n",
  400. cmd, result);
  401. return -EAGAIN;
  402. }
  403. /*
  404. * The eeprom strings on the lanaiX have the format
  405. * SN=x\0
  406. * MAC=x:x:x:x:x:x\0
  407. * PT:ddd mmm xx xx:xx:xx xx\0
  408. * PV:ddd mmm xx xx:xx:xx xx\0
  409. */
  410. static int myri10ge_read_mac_addr(struct myri10ge_priv *mgp)
  411. {
  412. char *ptr, *limit;
  413. int i;
  414. ptr = mgp->eeprom_strings;
  415. limit = mgp->eeprom_strings + MYRI10GE_EEPROM_STRINGS_SIZE;
  416. while (*ptr != '\0' && ptr < limit) {
  417. if (memcmp(ptr, "MAC=", 4) == 0) {
  418. ptr += 4;
  419. mgp->mac_addr_string = ptr;
  420. for (i = 0; i < 6; i++) {
  421. if ((ptr + 2) > limit)
  422. goto abort;
  423. mgp->mac_addr[i] =
  424. simple_strtoul(ptr, &ptr, 16);
  425. ptr += 1;
  426. }
  427. }
  428. if (memcmp(ptr, "PC=", 3) == 0) {
  429. ptr += 3;
  430. mgp->product_code_string = ptr;
  431. }
  432. if (memcmp((const void *)ptr, "SN=", 3) == 0) {
  433. ptr += 3;
  434. mgp->serial_number = simple_strtoul(ptr, &ptr, 10);
  435. }
  436. while (ptr < limit && *ptr++) ;
  437. }
  438. return 0;
  439. abort:
  440. dev_err(&mgp->pdev->dev, "failed to parse eeprom_strings\n");
  441. return -ENXIO;
  442. }
  443. /*
  444. * Enable or disable periodic RDMAs from the host to make certain
  445. * chipsets resend dropped PCIe messages
  446. */
  447. static void myri10ge_dummy_rdma(struct myri10ge_priv *mgp, int enable)
  448. {
  449. char __iomem *submit;
  450. __be32 buf[16] __attribute__ ((__aligned__(8)));
  451. u32 dma_low, dma_high;
  452. int i;
  453. /* clear confirmation addr */
  454. mgp->cmd->data = 0;
  455. mb();
  456. /* send a rdma command to the PCIe engine, and wait for the
  457. * response in the confirmation address. The firmware should
  458. * write a -1 there to indicate it is alive and well
  459. */
  460. dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
  461. dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
  462. buf[0] = htonl(dma_high); /* confirm addr MSW */
  463. buf[1] = htonl(dma_low); /* confirm addr LSW */
  464. buf[2] = MYRI10GE_NO_CONFIRM_DATA; /* confirm data */
  465. buf[3] = htonl(dma_high); /* dummy addr MSW */
  466. buf[4] = htonl(dma_low); /* dummy addr LSW */
  467. buf[5] = htonl(enable); /* enable? */
  468. submit = mgp->sram + MXGEFW_BOOT_DUMMY_RDMA;
  469. myri10ge_pio_copy(submit, &buf, sizeof(buf));
  470. for (i = 0; mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA && i < 20; i++)
  471. msleep(1);
  472. if (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA)
  473. dev_err(&mgp->pdev->dev, "dummy rdma %s failed\n",
  474. (enable ? "enable" : "disable"));
  475. }
  476. static int
  477. myri10ge_validate_firmware(struct myri10ge_priv *mgp,
  478. struct mcp_gen_header *hdr)
  479. {
  480. struct device *dev = &mgp->pdev->dev;
  481. /* check firmware type */
  482. if (ntohl(hdr->mcp_type) != MCP_TYPE_ETH) {
  483. dev_err(dev, "Bad firmware type: 0x%x\n", ntohl(hdr->mcp_type));
  484. return -EINVAL;
  485. }
  486. /* save firmware version for ethtool */
  487. strncpy(mgp->fw_version, hdr->version, sizeof(mgp->fw_version));
  488. mgp->fw_version[sizeof(mgp->fw_version) - 1] = '\0';
  489. sscanf(mgp->fw_version, "%d.%d.%d", &mgp->fw_ver_major,
  490. &mgp->fw_ver_minor, &mgp->fw_ver_tiny);
  491. if (!(mgp->fw_ver_major == MXGEFW_VERSION_MAJOR &&
  492. mgp->fw_ver_minor == MXGEFW_VERSION_MINOR)) {
  493. dev_err(dev, "Found firmware version %s\n", mgp->fw_version);
  494. dev_err(dev, "Driver needs %d.%d\n", MXGEFW_VERSION_MAJOR,
  495. MXGEFW_VERSION_MINOR);
  496. return -EINVAL;
  497. }
  498. return 0;
  499. }
  500. static int myri10ge_load_hotplug_firmware(struct myri10ge_priv *mgp, u32 * size)
  501. {
  502. unsigned crc, reread_crc;
  503. const struct firmware *fw;
  504. struct device *dev = &mgp->pdev->dev;
  505. unsigned char *fw_readback;
  506. struct mcp_gen_header *hdr;
  507. size_t hdr_offset;
  508. int status;
  509. unsigned i;
  510. if ((status = request_firmware(&fw, mgp->fw_name, dev)) < 0) {
  511. dev_err(dev, "Unable to load %s firmware image via hotplug\n",
  512. mgp->fw_name);
  513. status = -EINVAL;
  514. goto abort_with_nothing;
  515. }
  516. /* check size */
  517. if (fw->size >= mgp->sram_size - MYRI10GE_FW_OFFSET ||
  518. fw->size < MCP_HEADER_PTR_OFFSET + 4) {
  519. dev_err(dev, "Firmware size invalid:%d\n", (int)fw->size);
  520. status = -EINVAL;
  521. goto abort_with_fw;
  522. }
  523. /* check id */
  524. hdr_offset = ntohl(*(__be32 *) (fw->data + MCP_HEADER_PTR_OFFSET));
  525. if ((hdr_offset & 3) || hdr_offset + sizeof(*hdr) > fw->size) {
  526. dev_err(dev, "Bad firmware file\n");
  527. status = -EINVAL;
  528. goto abort_with_fw;
  529. }
  530. hdr = (void *)(fw->data + hdr_offset);
  531. status = myri10ge_validate_firmware(mgp, hdr);
  532. if (status != 0)
  533. goto abort_with_fw;
  534. crc = crc32(~0, fw->data, fw->size);
  535. for (i = 0; i < fw->size; i += 256) {
  536. myri10ge_pio_copy(mgp->sram + MYRI10GE_FW_OFFSET + i,
  537. fw->data + i,
  538. min(256U, (unsigned)(fw->size - i)));
  539. mb();
  540. readb(mgp->sram);
  541. }
  542. fw_readback = vmalloc(fw->size);
  543. if (!fw_readback) {
  544. status = -ENOMEM;
  545. goto abort_with_fw;
  546. }
  547. /* corruption checking is good for parity recovery and buggy chipset */
  548. memcpy_fromio(fw_readback, mgp->sram + MYRI10GE_FW_OFFSET, fw->size);
  549. reread_crc = crc32(~0, fw_readback, fw->size);
  550. vfree(fw_readback);
  551. if (crc != reread_crc) {
  552. dev_err(dev, "CRC failed(fw-len=%u), got 0x%x (expect 0x%x)\n",
  553. (unsigned)fw->size, reread_crc, crc);
  554. status = -EIO;
  555. goto abort_with_fw;
  556. }
  557. *size = (u32) fw->size;
  558. abort_with_fw:
  559. release_firmware(fw);
  560. abort_with_nothing:
  561. return status;
  562. }
  563. static int myri10ge_adopt_running_firmware(struct myri10ge_priv *mgp)
  564. {
  565. struct mcp_gen_header *hdr;
  566. struct device *dev = &mgp->pdev->dev;
  567. const size_t bytes = sizeof(struct mcp_gen_header);
  568. size_t hdr_offset;
  569. int status;
  570. /* find running firmware header */
  571. hdr_offset = swab32(readl(mgp->sram + MCP_HEADER_PTR_OFFSET));
  572. if ((hdr_offset & 3) || hdr_offset + sizeof(*hdr) > mgp->sram_size) {
  573. dev_err(dev, "Running firmware has bad header offset (%d)\n",
  574. (int)hdr_offset);
  575. return -EIO;
  576. }
  577. /* copy header of running firmware from SRAM to host memory to
  578. * validate firmware */
  579. hdr = kmalloc(bytes, GFP_KERNEL);
  580. if (hdr == NULL)
  581. return -ENOMEM;
  582. memcpy_fromio(hdr, mgp->sram + hdr_offset, bytes);
  583. status = myri10ge_validate_firmware(mgp, hdr);
  584. kfree(hdr);
  585. /* check to see if adopted firmware has bug where adopting
  586. * it will cause broadcasts to be filtered unless the NIC
  587. * is kept in ALLMULTI mode */
  588. if (mgp->fw_ver_major == 1 && mgp->fw_ver_minor == 4 &&
  589. mgp->fw_ver_tiny >= 4 && mgp->fw_ver_tiny <= 11) {
  590. mgp->adopted_rx_filter_bug = 1;
  591. dev_warn(dev, "Adopting fw %d.%d.%d: "
  592. "working around rx filter bug\n",
  593. mgp->fw_ver_major, mgp->fw_ver_minor,
  594. mgp->fw_ver_tiny);
  595. }
  596. return status;
  597. }
  598. static int myri10ge_get_firmware_capabilities(struct myri10ge_priv *mgp)
  599. {
  600. struct myri10ge_cmd cmd;
  601. int status;
  602. /* probe for IPv6 TSO support */
  603. mgp->features = NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_TSO;
  604. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_TSO6_HDR_SIZE,
  605. &cmd, 0);
  606. if (status == 0) {
  607. mgp->max_tso6 = cmd.data0;
  608. mgp->features |= NETIF_F_TSO6;
  609. }
  610. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_RX_RING_SIZE, &cmd, 0);
  611. if (status != 0) {
  612. dev_err(&mgp->pdev->dev,
  613. "failed MXGEFW_CMD_GET_RX_RING_SIZE\n");
  614. return -ENXIO;
  615. }
  616. mgp->max_intr_slots = 2 * (cmd.data0 / sizeof(struct mcp_dma_addr));
  617. return 0;
  618. }
  619. static int myri10ge_load_firmware(struct myri10ge_priv *mgp, int adopt)
  620. {
  621. char __iomem *submit;
  622. __be32 buf[16] __attribute__ ((__aligned__(8)));
  623. u32 dma_low, dma_high, size;
  624. int status, i;
  625. size = 0;
  626. status = myri10ge_load_hotplug_firmware(mgp, &size);
  627. if (status) {
  628. if (!adopt)
  629. return status;
  630. dev_warn(&mgp->pdev->dev, "hotplug firmware loading failed\n");
  631. /* Do not attempt to adopt firmware if there
  632. * was a bad crc */
  633. if (status == -EIO)
  634. return status;
  635. status = myri10ge_adopt_running_firmware(mgp);
  636. if (status != 0) {
  637. dev_err(&mgp->pdev->dev,
  638. "failed to adopt running firmware\n");
  639. return status;
  640. }
  641. dev_info(&mgp->pdev->dev,
  642. "Successfully adopted running firmware\n");
  643. if (mgp->tx_boundary == 4096) {
  644. dev_warn(&mgp->pdev->dev,
  645. "Using firmware currently running on NIC"
  646. ". For optimal\n");
  647. dev_warn(&mgp->pdev->dev,
  648. "performance consider loading optimized "
  649. "firmware\n");
  650. dev_warn(&mgp->pdev->dev, "via hotplug\n");
  651. }
  652. set_fw_name(mgp, "adopted", false);
  653. mgp->tx_boundary = 2048;
  654. myri10ge_dummy_rdma(mgp, 1);
  655. status = myri10ge_get_firmware_capabilities(mgp);
  656. return status;
  657. }
  658. /* clear confirmation addr */
  659. mgp->cmd->data = 0;
  660. mb();
  661. /* send a reload command to the bootstrap MCP, and wait for the
  662. * response in the confirmation address. The firmware should
  663. * write a -1 there to indicate it is alive and well
  664. */
  665. dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
  666. dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
  667. buf[0] = htonl(dma_high); /* confirm addr MSW */
  668. buf[1] = htonl(dma_low); /* confirm addr LSW */
  669. buf[2] = MYRI10GE_NO_CONFIRM_DATA; /* confirm data */
  670. /* FIX: All newest firmware should un-protect the bottom of
  671. * the sram before handoff. However, the very first interfaces
  672. * do not. Therefore the handoff copy must skip the first 8 bytes
  673. */
  674. buf[3] = htonl(MYRI10GE_FW_OFFSET + 8); /* where the code starts */
  675. buf[4] = htonl(size - 8); /* length of code */
  676. buf[5] = htonl(8); /* where to copy to */
  677. buf[6] = htonl(0); /* where to jump to */
  678. submit = mgp->sram + MXGEFW_BOOT_HANDOFF;
  679. myri10ge_pio_copy(submit, &buf, sizeof(buf));
  680. mb();
  681. msleep(1);
  682. mb();
  683. i = 0;
  684. while (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA && i < 9) {
  685. msleep(1 << i);
  686. i++;
  687. }
  688. if (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA) {
  689. dev_err(&mgp->pdev->dev, "handoff failed\n");
  690. return -ENXIO;
  691. }
  692. myri10ge_dummy_rdma(mgp, 1);
  693. status = myri10ge_get_firmware_capabilities(mgp);
  694. return status;
  695. }
  696. static int myri10ge_update_mac_address(struct myri10ge_priv *mgp, u8 * addr)
  697. {
  698. struct myri10ge_cmd cmd;
  699. int status;
  700. cmd.data0 = ((addr[0] << 24) | (addr[1] << 16)
  701. | (addr[2] << 8) | addr[3]);
  702. cmd.data1 = ((addr[4] << 8) | (addr[5]));
  703. status = myri10ge_send_cmd(mgp, MXGEFW_SET_MAC_ADDRESS, &cmd, 0);
  704. return status;
  705. }
  706. static int myri10ge_change_pause(struct myri10ge_priv *mgp, int pause)
  707. {
  708. struct myri10ge_cmd cmd;
  709. int status, ctl;
  710. ctl = pause ? MXGEFW_ENABLE_FLOW_CONTROL : MXGEFW_DISABLE_FLOW_CONTROL;
  711. status = myri10ge_send_cmd(mgp, ctl, &cmd, 0);
  712. if (status) {
  713. netdev_err(mgp->dev, "Failed to set flow control mode\n");
  714. return status;
  715. }
  716. mgp->pause = pause;
  717. return 0;
  718. }
  719. static void
  720. myri10ge_change_promisc(struct myri10ge_priv *mgp, int promisc, int atomic)
  721. {
  722. struct myri10ge_cmd cmd;
  723. int status, ctl;
  724. ctl = promisc ? MXGEFW_ENABLE_PROMISC : MXGEFW_DISABLE_PROMISC;
  725. status = myri10ge_send_cmd(mgp, ctl, &cmd, atomic);
  726. if (status)
  727. netdev_err(mgp->dev, "Failed to set promisc mode\n");
  728. }
  729. static int myri10ge_dma_test(struct myri10ge_priv *mgp, int test_type)
  730. {
  731. struct myri10ge_cmd cmd;
  732. int status;
  733. u32 len;
  734. struct page *dmatest_page;
  735. dma_addr_t dmatest_bus;
  736. char *test = " ";
  737. dmatest_page = alloc_page(GFP_KERNEL);
  738. if (!dmatest_page)
  739. return -ENOMEM;
  740. dmatest_bus = pci_map_page(mgp->pdev, dmatest_page, 0, PAGE_SIZE,
  741. DMA_BIDIRECTIONAL);
  742. if (unlikely(pci_dma_mapping_error(mgp->pdev, dmatest_bus))) {
  743. __free_page(dmatest_page);
  744. return -ENOMEM;
  745. }
  746. /* Run a small DMA test.
  747. * The magic multipliers to the length tell the firmware
  748. * to do DMA read, write, or read+write tests. The
  749. * results are returned in cmd.data0. The upper 16
  750. * bits or the return is the number of transfers completed.
  751. * The lower 16 bits is the time in 0.5us ticks that the
  752. * transfers took to complete.
  753. */
  754. len = mgp->tx_boundary;
  755. cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
  756. cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
  757. cmd.data2 = len * 0x10000;
  758. status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
  759. if (status != 0) {
  760. test = "read";
  761. goto abort;
  762. }
  763. mgp->read_dma = ((cmd.data0 >> 16) * len * 2) / (cmd.data0 & 0xffff);
  764. cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
  765. cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
  766. cmd.data2 = len * 0x1;
  767. status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
  768. if (status != 0) {
  769. test = "write";
  770. goto abort;
  771. }
  772. mgp->write_dma = ((cmd.data0 >> 16) * len * 2) / (cmd.data0 & 0xffff);
  773. cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
  774. cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
  775. cmd.data2 = len * 0x10001;
  776. status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
  777. if (status != 0) {
  778. test = "read/write";
  779. goto abort;
  780. }
  781. mgp->read_write_dma = ((cmd.data0 >> 16) * len * 2 * 2) /
  782. (cmd.data0 & 0xffff);
  783. abort:
  784. pci_unmap_page(mgp->pdev, dmatest_bus, PAGE_SIZE, DMA_BIDIRECTIONAL);
  785. put_page(dmatest_page);
  786. if (status != 0 && test_type != MXGEFW_CMD_UNALIGNED_TEST)
  787. dev_warn(&mgp->pdev->dev, "DMA %s benchmark failed: %d\n",
  788. test, status);
  789. return status;
  790. }
  791. static int myri10ge_reset(struct myri10ge_priv *mgp)
  792. {
  793. struct myri10ge_cmd cmd;
  794. struct myri10ge_slice_state *ss;
  795. int i, status;
  796. size_t bytes;
  797. #ifdef CONFIG_MYRI10GE_DCA
  798. unsigned long dca_tag_off;
  799. #endif
  800. /* try to send a reset command to the card to see if it
  801. * is alive */
  802. memset(&cmd, 0, sizeof(cmd));
  803. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_RESET, &cmd, 0);
  804. if (status != 0) {
  805. dev_err(&mgp->pdev->dev, "failed reset\n");
  806. return -ENXIO;
  807. }
  808. (void)myri10ge_dma_test(mgp, MXGEFW_DMA_TEST);
  809. /*
  810. * Use non-ndis mcp_slot (eg, 4 bytes total,
  811. * no toeplitz hash value returned. Older firmware will
  812. * not understand this command, but will use the correct
  813. * sized mcp_slot, so we ignore error returns
  814. */
  815. cmd.data0 = MXGEFW_RSS_MCP_SLOT_TYPE_MIN;
  816. (void)myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_RSS_MCP_SLOT_TYPE, &cmd, 0);
  817. /* Now exchange information about interrupts */
  818. bytes = mgp->max_intr_slots * sizeof(*mgp->ss[0].rx_done.entry);
  819. cmd.data0 = (u32) bytes;
  820. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_SIZE, &cmd, 0);
  821. /*
  822. * Even though we already know how many slices are supported
  823. * via myri10ge_probe_slices() MXGEFW_CMD_GET_MAX_RSS_QUEUES
  824. * has magic side effects, and must be called after a reset.
  825. * It must be called prior to calling any RSS related cmds,
  826. * including assigning an interrupt queue for anything but
  827. * slice 0. It must also be called *after*
  828. * MXGEFW_CMD_SET_INTRQ_SIZE, since the intrq size is used by
  829. * the firmware to compute offsets.
  830. */
  831. if (mgp->num_slices > 1) {
  832. /* ask the maximum number of slices it supports */
  833. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_RSS_QUEUES,
  834. &cmd, 0);
  835. if (status != 0) {
  836. dev_err(&mgp->pdev->dev,
  837. "failed to get number of slices\n");
  838. }
  839. /*
  840. * MXGEFW_CMD_ENABLE_RSS_QUEUES must be called prior
  841. * to setting up the interrupt queue DMA
  842. */
  843. cmd.data0 = mgp->num_slices;
  844. cmd.data1 = MXGEFW_SLICE_INTR_MODE_ONE_PER_SLICE;
  845. if (mgp->dev->real_num_tx_queues > 1)
  846. cmd.data1 |= MXGEFW_SLICE_ENABLE_MULTIPLE_TX_QUEUES;
  847. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ENABLE_RSS_QUEUES,
  848. &cmd, 0);
  849. /* Firmware older than 1.4.32 only supports multiple
  850. * RX queues, so if we get an error, first retry using a
  851. * single TX queue before giving up */
  852. if (status != 0 && mgp->dev->real_num_tx_queues > 1) {
  853. netif_set_real_num_tx_queues(mgp->dev, 1);
  854. cmd.data0 = mgp->num_slices;
  855. cmd.data1 = MXGEFW_SLICE_INTR_MODE_ONE_PER_SLICE;
  856. status = myri10ge_send_cmd(mgp,
  857. MXGEFW_CMD_ENABLE_RSS_QUEUES,
  858. &cmd, 0);
  859. }
  860. if (status != 0) {
  861. dev_err(&mgp->pdev->dev,
  862. "failed to set number of slices\n");
  863. return status;
  864. }
  865. }
  866. for (i = 0; i < mgp->num_slices; i++) {
  867. ss = &mgp->ss[i];
  868. cmd.data0 = MYRI10GE_LOWPART_TO_U32(ss->rx_done.bus);
  869. cmd.data1 = MYRI10GE_HIGHPART_TO_U32(ss->rx_done.bus);
  870. cmd.data2 = i;
  871. status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_DMA,
  872. &cmd, 0);
  873. }
  874. status |=
  875. myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_IRQ_ACK_OFFSET, &cmd, 0);
  876. for (i = 0; i < mgp->num_slices; i++) {
  877. ss = &mgp->ss[i];
  878. ss->irq_claim =
  879. (__iomem __be32 *) (mgp->sram + cmd.data0 + 8 * i);
  880. }
  881. status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_IRQ_DEASSERT_OFFSET,
  882. &cmd, 0);
  883. mgp->irq_deassert = (__iomem __be32 *) (mgp->sram + cmd.data0);
  884. status |= myri10ge_send_cmd
  885. (mgp, MXGEFW_CMD_GET_INTR_COAL_DELAY_OFFSET, &cmd, 0);
  886. mgp->intr_coal_delay_ptr = (__iomem __be32 *) (mgp->sram + cmd.data0);
  887. if (status != 0) {
  888. dev_err(&mgp->pdev->dev, "failed set interrupt parameters\n");
  889. return status;
  890. }
  891. put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
  892. #ifdef CONFIG_MYRI10GE_DCA
  893. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_DCA_OFFSET, &cmd, 0);
  894. dca_tag_off = cmd.data0;
  895. for (i = 0; i < mgp->num_slices; i++) {
  896. ss = &mgp->ss[i];
  897. if (status == 0) {
  898. ss->dca_tag = (__iomem __be32 *)
  899. (mgp->sram + dca_tag_off + 4 * i);
  900. } else {
  901. ss->dca_tag = NULL;
  902. }
  903. }
  904. #endif /* CONFIG_MYRI10GE_DCA */
  905. /* reset mcp/driver shared state back to 0 */
  906. mgp->link_changes = 0;
  907. for (i = 0; i < mgp->num_slices; i++) {
  908. ss = &mgp->ss[i];
  909. memset(ss->rx_done.entry, 0, bytes);
  910. ss->tx.req = 0;
  911. ss->tx.done = 0;
  912. ss->tx.pkt_start = 0;
  913. ss->tx.pkt_done = 0;
  914. ss->rx_big.cnt = 0;
  915. ss->rx_small.cnt = 0;
  916. ss->rx_done.idx = 0;
  917. ss->rx_done.cnt = 0;
  918. ss->tx.wake_queue = 0;
  919. ss->tx.stop_queue = 0;
  920. }
  921. status = myri10ge_update_mac_address(mgp, mgp->dev->dev_addr);
  922. myri10ge_change_pause(mgp, mgp->pause);
  923. myri10ge_set_multicast_list(mgp->dev);
  924. return status;
  925. }
  926. #ifdef CONFIG_MYRI10GE_DCA
  927. static int myri10ge_toggle_relaxed(struct pci_dev *pdev, int on)
  928. {
  929. int ret;
  930. u16 ctl;
  931. pcie_capability_read_word(pdev, PCI_EXP_DEVCTL, &ctl);
  932. ret = (ctl & PCI_EXP_DEVCTL_RELAX_EN) >> 4;
  933. if (ret != on) {
  934. ctl &= ~PCI_EXP_DEVCTL_RELAX_EN;
  935. ctl |= (on << 4);
  936. pcie_capability_write_word(pdev, PCI_EXP_DEVCTL, ctl);
  937. }
  938. return ret;
  939. }
  940. static void
  941. myri10ge_write_dca(struct myri10ge_slice_state *ss, int cpu, int tag)
  942. {
  943. ss->cached_dca_tag = tag;
  944. put_be32(htonl(tag), ss->dca_tag);
  945. }
  946. static inline void myri10ge_update_dca(struct myri10ge_slice_state *ss)
  947. {
  948. int cpu = get_cpu();
  949. int tag;
  950. if (cpu != ss->cpu) {
  951. tag = dca3_get_tag(&ss->mgp->pdev->dev, cpu);
  952. if (ss->cached_dca_tag != tag)
  953. myri10ge_write_dca(ss, cpu, tag);
  954. ss->cpu = cpu;
  955. }
  956. put_cpu();
  957. }
  958. static void myri10ge_setup_dca(struct myri10ge_priv *mgp)
  959. {
  960. int err, i;
  961. struct pci_dev *pdev = mgp->pdev;
  962. if (mgp->ss[0].dca_tag == NULL || mgp->dca_enabled)
  963. return;
  964. if (!myri10ge_dca) {
  965. dev_err(&pdev->dev, "dca disabled by administrator\n");
  966. return;
  967. }
  968. err = dca_add_requester(&pdev->dev);
  969. if (err) {
  970. if (err != -ENODEV)
  971. dev_err(&pdev->dev,
  972. "dca_add_requester() failed, err=%d\n", err);
  973. return;
  974. }
  975. mgp->relaxed_order = myri10ge_toggle_relaxed(pdev, 0);
  976. mgp->dca_enabled = 1;
  977. for (i = 0; i < mgp->num_slices; i++) {
  978. mgp->ss[i].cpu = -1;
  979. mgp->ss[i].cached_dca_tag = -1;
  980. myri10ge_update_dca(&mgp->ss[i]);
  981. }
  982. }
  983. static void myri10ge_teardown_dca(struct myri10ge_priv *mgp)
  984. {
  985. struct pci_dev *pdev = mgp->pdev;
  986. if (!mgp->dca_enabled)
  987. return;
  988. mgp->dca_enabled = 0;
  989. if (mgp->relaxed_order)
  990. myri10ge_toggle_relaxed(pdev, 1);
  991. dca_remove_requester(&pdev->dev);
  992. }
  993. static int myri10ge_notify_dca_device(struct device *dev, void *data)
  994. {
  995. struct myri10ge_priv *mgp;
  996. unsigned long event;
  997. mgp = dev_get_drvdata(dev);
  998. event = *(unsigned long *)data;
  999. if (event == DCA_PROVIDER_ADD)
  1000. myri10ge_setup_dca(mgp);
  1001. else if (event == DCA_PROVIDER_REMOVE)
  1002. myri10ge_teardown_dca(mgp);
  1003. return 0;
  1004. }
  1005. #endif /* CONFIG_MYRI10GE_DCA */
  1006. static inline void
  1007. myri10ge_submit_8rx(struct mcp_kreq_ether_recv __iomem * dst,
  1008. struct mcp_kreq_ether_recv *src)
  1009. {
  1010. __be32 low;
  1011. low = src->addr_low;
  1012. src->addr_low = htonl(DMA_BIT_MASK(32));
  1013. myri10ge_pio_copy(dst, src, 4 * sizeof(*src));
  1014. mb();
  1015. myri10ge_pio_copy(dst + 4, src + 4, 4 * sizeof(*src));
  1016. mb();
  1017. src->addr_low = low;
  1018. put_be32(low, &dst->addr_low);
  1019. mb();
  1020. }
  1021. static inline void myri10ge_vlan_ip_csum(struct sk_buff *skb, __wsum hw_csum)
  1022. {
  1023. struct vlan_hdr *vh = (struct vlan_hdr *)(skb->data);
  1024. if ((skb->protocol == htons(ETH_P_8021Q)) &&
  1025. (vh->h_vlan_encapsulated_proto == htons(ETH_P_IP) ||
  1026. vh->h_vlan_encapsulated_proto == htons(ETH_P_IPV6))) {
  1027. skb->csum = hw_csum;
  1028. skb->ip_summed = CHECKSUM_COMPLETE;
  1029. }
  1030. }
  1031. static void
  1032. myri10ge_alloc_rx_pages(struct myri10ge_priv *mgp, struct myri10ge_rx_buf *rx,
  1033. int bytes, int watchdog)
  1034. {
  1035. struct page *page;
  1036. dma_addr_t bus;
  1037. int idx;
  1038. #if MYRI10GE_ALLOC_SIZE > 4096
  1039. int end_offset;
  1040. #endif
  1041. if (unlikely(rx->watchdog_needed && !watchdog))
  1042. return;
  1043. /* try to refill entire ring */
  1044. while (rx->fill_cnt != (rx->cnt + rx->mask + 1)) {
  1045. idx = rx->fill_cnt & rx->mask;
  1046. if (rx->page_offset + bytes <= MYRI10GE_ALLOC_SIZE) {
  1047. /* we can use part of previous page */
  1048. get_page(rx->page);
  1049. } else {
  1050. /* we need a new page */
  1051. page =
  1052. alloc_pages(GFP_ATOMIC | __GFP_COMP,
  1053. MYRI10GE_ALLOC_ORDER);
  1054. if (unlikely(page == NULL)) {
  1055. if (rx->fill_cnt - rx->cnt < 16)
  1056. rx->watchdog_needed = 1;
  1057. return;
  1058. }
  1059. bus = pci_map_page(mgp->pdev, page, 0,
  1060. MYRI10GE_ALLOC_SIZE,
  1061. PCI_DMA_FROMDEVICE);
  1062. if (unlikely(pci_dma_mapping_error(mgp->pdev, bus))) {
  1063. __free_pages(page, MYRI10GE_ALLOC_ORDER);
  1064. if (rx->fill_cnt - rx->cnt < 16)
  1065. rx->watchdog_needed = 1;
  1066. return;
  1067. }
  1068. rx->page = page;
  1069. rx->page_offset = 0;
  1070. rx->bus = bus;
  1071. }
  1072. rx->info[idx].page = rx->page;
  1073. rx->info[idx].page_offset = rx->page_offset;
  1074. /* note that this is the address of the start of the
  1075. * page */
  1076. dma_unmap_addr_set(&rx->info[idx], bus, rx->bus);
  1077. rx->shadow[idx].addr_low =
  1078. htonl(MYRI10GE_LOWPART_TO_U32(rx->bus) + rx->page_offset);
  1079. rx->shadow[idx].addr_high =
  1080. htonl(MYRI10GE_HIGHPART_TO_U32(rx->bus));
  1081. /* start next packet on a cacheline boundary */
  1082. rx->page_offset += SKB_DATA_ALIGN(bytes);
  1083. #if MYRI10GE_ALLOC_SIZE > 4096
  1084. /* don't cross a 4KB boundary */
  1085. end_offset = rx->page_offset + bytes - 1;
  1086. if ((unsigned)(rx->page_offset ^ end_offset) > 4095)
  1087. rx->page_offset = end_offset & ~4095;
  1088. #endif
  1089. rx->fill_cnt++;
  1090. /* copy 8 descriptors to the firmware at a time */
  1091. if ((idx & 7) == 7) {
  1092. myri10ge_submit_8rx(&rx->lanai[idx - 7],
  1093. &rx->shadow[idx - 7]);
  1094. }
  1095. }
  1096. }
  1097. static inline void
  1098. myri10ge_unmap_rx_page(struct pci_dev *pdev,
  1099. struct myri10ge_rx_buffer_state *info, int bytes)
  1100. {
  1101. /* unmap the recvd page if we're the only or last user of it */
  1102. if (bytes >= MYRI10GE_ALLOC_SIZE / 2 ||
  1103. (info->page_offset + 2 * bytes) > MYRI10GE_ALLOC_SIZE) {
  1104. pci_unmap_page(pdev, (dma_unmap_addr(info, bus)
  1105. & ~(MYRI10GE_ALLOC_SIZE - 1)),
  1106. MYRI10GE_ALLOC_SIZE, PCI_DMA_FROMDEVICE);
  1107. }
  1108. }
  1109. /*
  1110. * GRO does not support acceleration of tagged vlan frames, and
  1111. * this NIC does not support vlan tag offload, so we must pop
  1112. * the tag ourselves to be able to achieve GRO performance that
  1113. * is comparable to LRO.
  1114. */
  1115. static inline void
  1116. myri10ge_vlan_rx(struct net_device *dev, void *addr, struct sk_buff *skb)
  1117. {
  1118. u8 *va;
  1119. struct vlan_ethhdr *veh;
  1120. struct skb_frag_struct *frag;
  1121. __wsum vsum;
  1122. va = addr;
  1123. va += MXGEFW_PAD;
  1124. veh = (struct vlan_ethhdr *)va;
  1125. if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) ==
  1126. NETIF_F_HW_VLAN_CTAG_RX &&
  1127. veh->h_vlan_proto == htons(ETH_P_8021Q)) {
  1128. /* fixup csum if needed */
  1129. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  1130. vsum = csum_partial(va + ETH_HLEN, VLAN_HLEN, 0);
  1131. skb->csum = csum_sub(skb->csum, vsum);
  1132. }
  1133. /* pop tag */
  1134. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(veh->h_vlan_TCI));
  1135. memmove(va + VLAN_HLEN, va, 2 * ETH_ALEN);
  1136. skb->len -= VLAN_HLEN;
  1137. skb->data_len -= VLAN_HLEN;
  1138. frag = skb_shinfo(skb)->frags;
  1139. frag->page_offset += VLAN_HLEN;
  1140. skb_frag_size_set(frag, skb_frag_size(frag) - VLAN_HLEN);
  1141. }
  1142. }
  1143. #define MYRI10GE_HLEN 64 /* Bytes to copy from page to skb linear memory */
  1144. static inline int
  1145. myri10ge_rx_done(struct myri10ge_slice_state *ss, int len, __wsum csum)
  1146. {
  1147. struct myri10ge_priv *mgp = ss->mgp;
  1148. struct sk_buff *skb;
  1149. struct skb_frag_struct *rx_frags;
  1150. struct myri10ge_rx_buf *rx;
  1151. int i, idx, remainder, bytes;
  1152. struct pci_dev *pdev = mgp->pdev;
  1153. struct net_device *dev = mgp->dev;
  1154. u8 *va;
  1155. if (len <= mgp->small_bytes) {
  1156. rx = &ss->rx_small;
  1157. bytes = mgp->small_bytes;
  1158. } else {
  1159. rx = &ss->rx_big;
  1160. bytes = mgp->big_bytes;
  1161. }
  1162. len += MXGEFW_PAD;
  1163. idx = rx->cnt & rx->mask;
  1164. va = page_address(rx->info[idx].page) + rx->info[idx].page_offset;
  1165. prefetch(va);
  1166. skb = napi_get_frags(&ss->napi);
  1167. if (unlikely(skb == NULL)) {
  1168. ss->stats.rx_dropped++;
  1169. for (i = 0, remainder = len; remainder > 0; i++) {
  1170. myri10ge_unmap_rx_page(pdev, &rx->info[idx], bytes);
  1171. put_page(rx->info[idx].page);
  1172. rx->cnt++;
  1173. idx = rx->cnt & rx->mask;
  1174. remainder -= MYRI10GE_ALLOC_SIZE;
  1175. }
  1176. return 0;
  1177. }
  1178. rx_frags = skb_shinfo(skb)->frags;
  1179. /* Fill skb_frag_struct(s) with data from our receive */
  1180. for (i = 0, remainder = len; remainder > 0; i++) {
  1181. myri10ge_unmap_rx_page(pdev, &rx->info[idx], bytes);
  1182. skb_fill_page_desc(skb, i, rx->info[idx].page,
  1183. rx->info[idx].page_offset,
  1184. remainder < MYRI10GE_ALLOC_SIZE ?
  1185. remainder : MYRI10GE_ALLOC_SIZE);
  1186. rx->cnt++;
  1187. idx = rx->cnt & rx->mask;
  1188. remainder -= MYRI10GE_ALLOC_SIZE;
  1189. }
  1190. /* remove padding */
  1191. rx_frags[0].page_offset += MXGEFW_PAD;
  1192. rx_frags[0].size -= MXGEFW_PAD;
  1193. len -= MXGEFW_PAD;
  1194. skb->len = len;
  1195. skb->data_len = len;
  1196. skb->truesize += len;
  1197. if (dev->features & NETIF_F_RXCSUM) {
  1198. skb->ip_summed = CHECKSUM_COMPLETE;
  1199. skb->csum = csum;
  1200. }
  1201. myri10ge_vlan_rx(mgp->dev, va, skb);
  1202. skb_record_rx_queue(skb, ss - &mgp->ss[0]);
  1203. napi_gro_frags(&ss->napi);
  1204. return 1;
  1205. }
  1206. static inline void
  1207. myri10ge_tx_done(struct myri10ge_slice_state *ss, int mcp_index)
  1208. {
  1209. struct pci_dev *pdev = ss->mgp->pdev;
  1210. struct myri10ge_tx_buf *tx = &ss->tx;
  1211. struct netdev_queue *dev_queue;
  1212. struct sk_buff *skb;
  1213. int idx, len;
  1214. while (tx->pkt_done != mcp_index) {
  1215. idx = tx->done & tx->mask;
  1216. skb = tx->info[idx].skb;
  1217. /* Mark as free */
  1218. tx->info[idx].skb = NULL;
  1219. if (tx->info[idx].last) {
  1220. tx->pkt_done++;
  1221. tx->info[idx].last = 0;
  1222. }
  1223. tx->done++;
  1224. len = dma_unmap_len(&tx->info[idx], len);
  1225. dma_unmap_len_set(&tx->info[idx], len, 0);
  1226. if (skb) {
  1227. ss->stats.tx_bytes += skb->len;
  1228. ss->stats.tx_packets++;
  1229. dev_kfree_skb_irq(skb);
  1230. if (len)
  1231. pci_unmap_single(pdev,
  1232. dma_unmap_addr(&tx->info[idx],
  1233. bus), len,
  1234. PCI_DMA_TODEVICE);
  1235. } else {
  1236. if (len)
  1237. pci_unmap_page(pdev,
  1238. dma_unmap_addr(&tx->info[idx],
  1239. bus), len,
  1240. PCI_DMA_TODEVICE);
  1241. }
  1242. }
  1243. dev_queue = netdev_get_tx_queue(ss->dev, ss - ss->mgp->ss);
  1244. /*
  1245. * Make a minimal effort to prevent the NIC from polling an
  1246. * idle tx queue. If we can't get the lock we leave the queue
  1247. * active. In this case, either a thread was about to start
  1248. * using the queue anyway, or we lost a race and the NIC will
  1249. * waste some of its resources polling an inactive queue for a
  1250. * while.
  1251. */
  1252. if ((ss->mgp->dev->real_num_tx_queues > 1) &&
  1253. __netif_tx_trylock(dev_queue)) {
  1254. if (tx->req == tx->done) {
  1255. tx->queue_active = 0;
  1256. put_be32(htonl(1), tx->send_stop);
  1257. mb();
  1258. mmiowb();
  1259. }
  1260. __netif_tx_unlock(dev_queue);
  1261. }
  1262. /* start the queue if we've stopped it */
  1263. if (netif_tx_queue_stopped(dev_queue) &&
  1264. tx->req - tx->done < (tx->mask >> 1) &&
  1265. ss->mgp->running == MYRI10GE_ETH_RUNNING) {
  1266. tx->wake_queue++;
  1267. netif_tx_wake_queue(dev_queue);
  1268. }
  1269. }
  1270. static inline int
  1271. myri10ge_clean_rx_done(struct myri10ge_slice_state *ss, int budget)
  1272. {
  1273. struct myri10ge_rx_done *rx_done = &ss->rx_done;
  1274. struct myri10ge_priv *mgp = ss->mgp;
  1275. unsigned long rx_bytes = 0;
  1276. unsigned long rx_packets = 0;
  1277. unsigned long rx_ok;
  1278. int idx = rx_done->idx;
  1279. int cnt = rx_done->cnt;
  1280. int work_done = 0;
  1281. u16 length;
  1282. __wsum checksum;
  1283. while (rx_done->entry[idx].length != 0 && work_done < budget) {
  1284. length = ntohs(rx_done->entry[idx].length);
  1285. rx_done->entry[idx].length = 0;
  1286. checksum = csum_unfold(rx_done->entry[idx].checksum);
  1287. rx_ok = myri10ge_rx_done(ss, length, checksum);
  1288. rx_packets += rx_ok;
  1289. rx_bytes += rx_ok * (unsigned long)length;
  1290. cnt++;
  1291. idx = cnt & (mgp->max_intr_slots - 1);
  1292. work_done++;
  1293. }
  1294. rx_done->idx = idx;
  1295. rx_done->cnt = cnt;
  1296. ss->stats.rx_packets += rx_packets;
  1297. ss->stats.rx_bytes += rx_bytes;
  1298. /* restock receive rings if needed */
  1299. if (ss->rx_small.fill_cnt - ss->rx_small.cnt < myri10ge_fill_thresh)
  1300. myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
  1301. mgp->small_bytes + MXGEFW_PAD, 0);
  1302. if (ss->rx_big.fill_cnt - ss->rx_big.cnt < myri10ge_fill_thresh)
  1303. myri10ge_alloc_rx_pages(mgp, &ss->rx_big, mgp->big_bytes, 0);
  1304. return work_done;
  1305. }
  1306. static inline void myri10ge_check_statblock(struct myri10ge_priv *mgp)
  1307. {
  1308. struct mcp_irq_data *stats = mgp->ss[0].fw_stats;
  1309. if (unlikely(stats->stats_updated)) {
  1310. unsigned link_up = ntohl(stats->link_up);
  1311. if (mgp->link_state != link_up) {
  1312. mgp->link_state = link_up;
  1313. if (mgp->link_state == MXGEFW_LINK_UP) {
  1314. netif_info(mgp, link, mgp->dev, "link up\n");
  1315. netif_carrier_on(mgp->dev);
  1316. mgp->link_changes++;
  1317. } else {
  1318. netif_info(mgp, link, mgp->dev, "link %s\n",
  1319. (link_up == MXGEFW_LINK_MYRINET ?
  1320. "mismatch (Myrinet detected)" :
  1321. "down"));
  1322. netif_carrier_off(mgp->dev);
  1323. mgp->link_changes++;
  1324. }
  1325. }
  1326. if (mgp->rdma_tags_available !=
  1327. ntohl(stats->rdma_tags_available)) {
  1328. mgp->rdma_tags_available =
  1329. ntohl(stats->rdma_tags_available);
  1330. netdev_warn(mgp->dev, "RDMA timed out! %d tags left\n",
  1331. mgp->rdma_tags_available);
  1332. }
  1333. mgp->down_cnt += stats->link_down;
  1334. if (stats->link_down)
  1335. wake_up(&mgp->down_wq);
  1336. }
  1337. }
  1338. static int myri10ge_poll(struct napi_struct *napi, int budget)
  1339. {
  1340. struct myri10ge_slice_state *ss =
  1341. container_of(napi, struct myri10ge_slice_state, napi);
  1342. int work_done;
  1343. #ifdef CONFIG_MYRI10GE_DCA
  1344. if (ss->mgp->dca_enabled)
  1345. myri10ge_update_dca(ss);
  1346. #endif
  1347. /* process as many rx events as NAPI will allow */
  1348. work_done = myri10ge_clean_rx_done(ss, budget);
  1349. if (work_done < budget) {
  1350. napi_complete_done(napi, work_done);
  1351. put_be32(htonl(3), ss->irq_claim);
  1352. }
  1353. return work_done;
  1354. }
  1355. static irqreturn_t myri10ge_intr(int irq, void *arg)
  1356. {
  1357. struct myri10ge_slice_state *ss = arg;
  1358. struct myri10ge_priv *mgp = ss->mgp;
  1359. struct mcp_irq_data *stats = ss->fw_stats;
  1360. struct myri10ge_tx_buf *tx = &ss->tx;
  1361. u32 send_done_count;
  1362. int i;
  1363. /* an interrupt on a non-zero receive-only slice is implicitly
  1364. * valid since MSI-X irqs are not shared */
  1365. if ((mgp->dev->real_num_tx_queues == 1) && (ss != mgp->ss)) {
  1366. napi_schedule(&ss->napi);
  1367. return IRQ_HANDLED;
  1368. }
  1369. /* make sure it is our IRQ, and that the DMA has finished */
  1370. if (unlikely(!stats->valid))
  1371. return IRQ_NONE;
  1372. /* low bit indicates receives are present, so schedule
  1373. * napi poll handler */
  1374. if (stats->valid & 1)
  1375. napi_schedule(&ss->napi);
  1376. if (!mgp->msi_enabled && !mgp->msix_enabled) {
  1377. put_be32(0, mgp->irq_deassert);
  1378. if (!myri10ge_deassert_wait)
  1379. stats->valid = 0;
  1380. mb();
  1381. } else
  1382. stats->valid = 0;
  1383. /* Wait for IRQ line to go low, if using INTx */
  1384. i = 0;
  1385. while (1) {
  1386. i++;
  1387. /* check for transmit completes and receives */
  1388. send_done_count = ntohl(stats->send_done_count);
  1389. if (send_done_count != tx->pkt_done)
  1390. myri10ge_tx_done(ss, (int)send_done_count);
  1391. if (unlikely(i > myri10ge_max_irq_loops)) {
  1392. netdev_warn(mgp->dev, "irq stuck?\n");
  1393. stats->valid = 0;
  1394. schedule_work(&mgp->watchdog_work);
  1395. }
  1396. if (likely(stats->valid == 0))
  1397. break;
  1398. cpu_relax();
  1399. barrier();
  1400. }
  1401. /* Only slice 0 updates stats */
  1402. if (ss == mgp->ss)
  1403. myri10ge_check_statblock(mgp);
  1404. put_be32(htonl(3), ss->irq_claim + 1);
  1405. return IRQ_HANDLED;
  1406. }
  1407. static int
  1408. myri10ge_get_link_ksettings(struct net_device *netdev,
  1409. struct ethtool_link_ksettings *cmd)
  1410. {
  1411. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1412. char *ptr;
  1413. int i;
  1414. cmd->base.autoneg = AUTONEG_DISABLE;
  1415. cmd->base.speed = SPEED_10000;
  1416. cmd->base.duplex = DUPLEX_FULL;
  1417. /*
  1418. * parse the product code to deterimine the interface type
  1419. * (CX4, XFP, Quad Ribbon Fiber) by looking at the character
  1420. * after the 3rd dash in the driver's cached copy of the
  1421. * EEPROM's product code string.
  1422. */
  1423. ptr = mgp->product_code_string;
  1424. if (ptr == NULL) {
  1425. netdev_err(netdev, "Missing product code\n");
  1426. return 0;
  1427. }
  1428. for (i = 0; i < 3; i++, ptr++) {
  1429. ptr = strchr(ptr, '-');
  1430. if (ptr == NULL) {
  1431. netdev_err(netdev, "Invalid product code %s\n",
  1432. mgp->product_code_string);
  1433. return 0;
  1434. }
  1435. }
  1436. if (*ptr == '2')
  1437. ptr++;
  1438. if (*ptr == 'R' || *ptr == 'Q' || *ptr == 'S') {
  1439. /* We've found either an XFP, quad ribbon fiber, or SFP+ */
  1440. cmd->base.port = PORT_FIBRE;
  1441. ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
  1442. ethtool_link_ksettings_add_link_mode(cmd, advertising, FIBRE);
  1443. } else {
  1444. cmd->base.port = PORT_OTHER;
  1445. }
  1446. return 0;
  1447. }
  1448. static void
  1449. myri10ge_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
  1450. {
  1451. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1452. strlcpy(info->driver, "myri10ge", sizeof(info->driver));
  1453. strlcpy(info->version, MYRI10GE_VERSION_STR, sizeof(info->version));
  1454. strlcpy(info->fw_version, mgp->fw_version, sizeof(info->fw_version));
  1455. strlcpy(info->bus_info, pci_name(mgp->pdev), sizeof(info->bus_info));
  1456. }
  1457. static int
  1458. myri10ge_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *coal)
  1459. {
  1460. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1461. coal->rx_coalesce_usecs = mgp->intr_coal_delay;
  1462. return 0;
  1463. }
  1464. static int
  1465. myri10ge_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *coal)
  1466. {
  1467. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1468. mgp->intr_coal_delay = coal->rx_coalesce_usecs;
  1469. put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
  1470. return 0;
  1471. }
  1472. static void
  1473. myri10ge_get_pauseparam(struct net_device *netdev,
  1474. struct ethtool_pauseparam *pause)
  1475. {
  1476. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1477. pause->autoneg = 0;
  1478. pause->rx_pause = mgp->pause;
  1479. pause->tx_pause = mgp->pause;
  1480. }
  1481. static int
  1482. myri10ge_set_pauseparam(struct net_device *netdev,
  1483. struct ethtool_pauseparam *pause)
  1484. {
  1485. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1486. if (pause->tx_pause != mgp->pause)
  1487. return myri10ge_change_pause(mgp, pause->tx_pause);
  1488. if (pause->rx_pause != mgp->pause)
  1489. return myri10ge_change_pause(mgp, pause->rx_pause);
  1490. if (pause->autoneg != 0)
  1491. return -EINVAL;
  1492. return 0;
  1493. }
  1494. static void
  1495. myri10ge_get_ringparam(struct net_device *netdev,
  1496. struct ethtool_ringparam *ring)
  1497. {
  1498. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1499. ring->rx_mini_max_pending = mgp->ss[0].rx_small.mask + 1;
  1500. ring->rx_max_pending = mgp->ss[0].rx_big.mask + 1;
  1501. ring->rx_jumbo_max_pending = 0;
  1502. ring->tx_max_pending = mgp->ss[0].tx.mask + 1;
  1503. ring->rx_mini_pending = ring->rx_mini_max_pending;
  1504. ring->rx_pending = ring->rx_max_pending;
  1505. ring->rx_jumbo_pending = ring->rx_jumbo_max_pending;
  1506. ring->tx_pending = ring->tx_max_pending;
  1507. }
  1508. static const char myri10ge_gstrings_main_stats[][ETH_GSTRING_LEN] = {
  1509. "rx_packets", "tx_packets", "rx_bytes", "tx_bytes", "rx_errors",
  1510. "tx_errors", "rx_dropped", "tx_dropped", "multicast", "collisions",
  1511. "rx_length_errors", "rx_over_errors", "rx_crc_errors",
  1512. "rx_frame_errors", "rx_fifo_errors", "rx_missed_errors",
  1513. "tx_aborted_errors", "tx_carrier_errors", "tx_fifo_errors",
  1514. "tx_heartbeat_errors", "tx_window_errors",
  1515. /* device-specific stats */
  1516. "tx_boundary", "irq", "MSI", "MSIX",
  1517. "read_dma_bw_MBs", "write_dma_bw_MBs", "read_write_dma_bw_MBs",
  1518. "serial_number", "watchdog_resets",
  1519. #ifdef CONFIG_MYRI10GE_DCA
  1520. "dca_capable_firmware", "dca_device_present",
  1521. #endif
  1522. "link_changes", "link_up", "dropped_link_overflow",
  1523. "dropped_link_error_or_filtered",
  1524. "dropped_pause", "dropped_bad_phy", "dropped_bad_crc32",
  1525. "dropped_unicast_filtered", "dropped_multicast_filtered",
  1526. "dropped_runt", "dropped_overrun", "dropped_no_small_buffer",
  1527. "dropped_no_big_buffer"
  1528. };
  1529. static const char myri10ge_gstrings_slice_stats[][ETH_GSTRING_LEN] = {
  1530. "----------- slice ---------",
  1531. "tx_pkt_start", "tx_pkt_done", "tx_req", "tx_done",
  1532. "rx_small_cnt", "rx_big_cnt",
  1533. "wake_queue", "stop_queue", "tx_linearized",
  1534. };
  1535. #define MYRI10GE_NET_STATS_LEN 21
  1536. #define MYRI10GE_MAIN_STATS_LEN ARRAY_SIZE(myri10ge_gstrings_main_stats)
  1537. #define MYRI10GE_SLICE_STATS_LEN ARRAY_SIZE(myri10ge_gstrings_slice_stats)
  1538. static void
  1539. myri10ge_get_strings(struct net_device *netdev, u32 stringset, u8 * data)
  1540. {
  1541. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1542. int i;
  1543. switch (stringset) {
  1544. case ETH_SS_STATS:
  1545. memcpy(data, *myri10ge_gstrings_main_stats,
  1546. sizeof(myri10ge_gstrings_main_stats));
  1547. data += sizeof(myri10ge_gstrings_main_stats);
  1548. for (i = 0; i < mgp->num_slices; i++) {
  1549. memcpy(data, *myri10ge_gstrings_slice_stats,
  1550. sizeof(myri10ge_gstrings_slice_stats));
  1551. data += sizeof(myri10ge_gstrings_slice_stats);
  1552. }
  1553. break;
  1554. }
  1555. }
  1556. static int myri10ge_get_sset_count(struct net_device *netdev, int sset)
  1557. {
  1558. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1559. switch (sset) {
  1560. case ETH_SS_STATS:
  1561. return MYRI10GE_MAIN_STATS_LEN +
  1562. mgp->num_slices * MYRI10GE_SLICE_STATS_LEN;
  1563. default:
  1564. return -EOPNOTSUPP;
  1565. }
  1566. }
  1567. static void
  1568. myri10ge_get_ethtool_stats(struct net_device *netdev,
  1569. struct ethtool_stats *stats, u64 * data)
  1570. {
  1571. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1572. struct myri10ge_slice_state *ss;
  1573. struct rtnl_link_stats64 link_stats;
  1574. int slice;
  1575. int i;
  1576. /* force stats update */
  1577. memset(&link_stats, 0, sizeof(link_stats));
  1578. (void)myri10ge_get_stats(netdev, &link_stats);
  1579. for (i = 0; i < MYRI10GE_NET_STATS_LEN; i++)
  1580. data[i] = ((u64 *)&link_stats)[i];
  1581. data[i++] = (unsigned int)mgp->tx_boundary;
  1582. data[i++] = (unsigned int)mgp->pdev->irq;
  1583. data[i++] = (unsigned int)mgp->msi_enabled;
  1584. data[i++] = (unsigned int)mgp->msix_enabled;
  1585. data[i++] = (unsigned int)mgp->read_dma;
  1586. data[i++] = (unsigned int)mgp->write_dma;
  1587. data[i++] = (unsigned int)mgp->read_write_dma;
  1588. data[i++] = (unsigned int)mgp->serial_number;
  1589. data[i++] = (unsigned int)mgp->watchdog_resets;
  1590. #ifdef CONFIG_MYRI10GE_DCA
  1591. data[i++] = (unsigned int)(mgp->ss[0].dca_tag != NULL);
  1592. data[i++] = (unsigned int)(mgp->dca_enabled);
  1593. #endif
  1594. data[i++] = (unsigned int)mgp->link_changes;
  1595. /* firmware stats are useful only in the first slice */
  1596. ss = &mgp->ss[0];
  1597. data[i++] = (unsigned int)ntohl(ss->fw_stats->link_up);
  1598. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_link_overflow);
  1599. data[i++] =
  1600. (unsigned int)ntohl(ss->fw_stats->dropped_link_error_or_filtered);
  1601. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_pause);
  1602. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_bad_phy);
  1603. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_bad_crc32);
  1604. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_unicast_filtered);
  1605. data[i++] =
  1606. (unsigned int)ntohl(ss->fw_stats->dropped_multicast_filtered);
  1607. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_runt);
  1608. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_overrun);
  1609. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_no_small_buffer);
  1610. data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_no_big_buffer);
  1611. for (slice = 0; slice < mgp->num_slices; slice++) {
  1612. ss = &mgp->ss[slice];
  1613. data[i++] = slice;
  1614. data[i++] = (unsigned int)ss->tx.pkt_start;
  1615. data[i++] = (unsigned int)ss->tx.pkt_done;
  1616. data[i++] = (unsigned int)ss->tx.req;
  1617. data[i++] = (unsigned int)ss->tx.done;
  1618. data[i++] = (unsigned int)ss->rx_small.cnt;
  1619. data[i++] = (unsigned int)ss->rx_big.cnt;
  1620. data[i++] = (unsigned int)ss->tx.wake_queue;
  1621. data[i++] = (unsigned int)ss->tx.stop_queue;
  1622. data[i++] = (unsigned int)ss->tx.linearized;
  1623. }
  1624. }
  1625. static void myri10ge_set_msglevel(struct net_device *netdev, u32 value)
  1626. {
  1627. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1628. mgp->msg_enable = value;
  1629. }
  1630. static u32 myri10ge_get_msglevel(struct net_device *netdev)
  1631. {
  1632. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1633. return mgp->msg_enable;
  1634. }
  1635. /*
  1636. * Use a low-level command to change the LED behavior. Rather than
  1637. * blinking (which is the normal case), when identify is used, the
  1638. * yellow LED turns solid.
  1639. */
  1640. static int myri10ge_led(struct myri10ge_priv *mgp, int on)
  1641. {
  1642. struct mcp_gen_header *hdr;
  1643. struct device *dev = &mgp->pdev->dev;
  1644. size_t hdr_off, pattern_off, hdr_len;
  1645. u32 pattern = 0xfffffffe;
  1646. /* find running firmware header */
  1647. hdr_off = swab32(readl(mgp->sram + MCP_HEADER_PTR_OFFSET));
  1648. if ((hdr_off & 3) || hdr_off + sizeof(*hdr) > mgp->sram_size) {
  1649. dev_err(dev, "Running firmware has bad header offset (%d)\n",
  1650. (int)hdr_off);
  1651. return -EIO;
  1652. }
  1653. hdr_len = swab32(readl(mgp->sram + hdr_off +
  1654. offsetof(struct mcp_gen_header, header_length)));
  1655. pattern_off = hdr_off + offsetof(struct mcp_gen_header, led_pattern);
  1656. if (pattern_off >= (hdr_len + hdr_off)) {
  1657. dev_info(dev, "Firmware does not support LED identification\n");
  1658. return -EINVAL;
  1659. }
  1660. if (!on)
  1661. pattern = swab32(readl(mgp->sram + pattern_off + 4));
  1662. writel(swab32(pattern), mgp->sram + pattern_off);
  1663. return 0;
  1664. }
  1665. static int
  1666. myri10ge_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
  1667. {
  1668. struct myri10ge_priv *mgp = netdev_priv(netdev);
  1669. int rc;
  1670. switch (state) {
  1671. case ETHTOOL_ID_ACTIVE:
  1672. rc = myri10ge_led(mgp, 1);
  1673. break;
  1674. case ETHTOOL_ID_INACTIVE:
  1675. rc = myri10ge_led(mgp, 0);
  1676. break;
  1677. default:
  1678. rc = -EINVAL;
  1679. }
  1680. return rc;
  1681. }
  1682. static const struct ethtool_ops myri10ge_ethtool_ops = {
  1683. .get_drvinfo = myri10ge_get_drvinfo,
  1684. .get_coalesce = myri10ge_get_coalesce,
  1685. .set_coalesce = myri10ge_set_coalesce,
  1686. .get_pauseparam = myri10ge_get_pauseparam,
  1687. .set_pauseparam = myri10ge_set_pauseparam,
  1688. .get_ringparam = myri10ge_get_ringparam,
  1689. .get_link = ethtool_op_get_link,
  1690. .get_strings = myri10ge_get_strings,
  1691. .get_sset_count = myri10ge_get_sset_count,
  1692. .get_ethtool_stats = myri10ge_get_ethtool_stats,
  1693. .set_msglevel = myri10ge_set_msglevel,
  1694. .get_msglevel = myri10ge_get_msglevel,
  1695. .set_phys_id = myri10ge_phys_id,
  1696. .get_link_ksettings = myri10ge_get_link_ksettings,
  1697. };
  1698. static int myri10ge_allocate_rings(struct myri10ge_slice_state *ss)
  1699. {
  1700. struct myri10ge_priv *mgp = ss->mgp;
  1701. struct myri10ge_cmd cmd;
  1702. struct net_device *dev = mgp->dev;
  1703. int tx_ring_size, rx_ring_size;
  1704. int tx_ring_entries, rx_ring_entries;
  1705. int i, slice, status;
  1706. size_t bytes;
  1707. /* get ring sizes */
  1708. slice = ss - mgp->ss;
  1709. cmd.data0 = slice;
  1710. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_RING_SIZE, &cmd, 0);
  1711. tx_ring_size = cmd.data0;
  1712. cmd.data0 = slice;
  1713. status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_RX_RING_SIZE, &cmd, 0);
  1714. if (status != 0)
  1715. return status;
  1716. rx_ring_size = cmd.data0;
  1717. tx_ring_entries = tx_ring_size / sizeof(struct mcp_kreq_ether_send);
  1718. rx_ring_entries = rx_ring_size / sizeof(struct mcp_dma_addr);
  1719. ss->tx.mask = tx_ring_entries - 1;
  1720. ss->rx_small.mask = ss->rx_big.mask = rx_ring_entries - 1;
  1721. status = -ENOMEM;
  1722. /* allocate the host shadow rings */
  1723. bytes = 8 + (MYRI10GE_MAX_SEND_DESC_TSO + 4)
  1724. * sizeof(*ss->tx.req_list);
  1725. ss->tx.req_bytes = kzalloc(bytes, GFP_KERNEL);
  1726. if (ss->tx.req_bytes == NULL)
  1727. goto abort_with_nothing;
  1728. /* ensure req_list entries are aligned to 8 bytes */
  1729. ss->tx.req_list = (struct mcp_kreq_ether_send *)
  1730. ALIGN((unsigned long)ss->tx.req_bytes, 8);
  1731. ss->tx.queue_active = 0;
  1732. bytes = rx_ring_entries * sizeof(*ss->rx_small.shadow);
  1733. ss->rx_small.shadow = kzalloc(bytes, GFP_KERNEL);
  1734. if (ss->rx_small.shadow == NULL)
  1735. goto abort_with_tx_req_bytes;
  1736. bytes = rx_ring_entries * sizeof(*ss->rx_big.shadow);
  1737. ss->rx_big.shadow = kzalloc(bytes, GFP_KERNEL);
  1738. if (ss->rx_big.shadow == NULL)
  1739. goto abort_with_rx_small_shadow;
  1740. /* allocate the host info rings */
  1741. bytes = tx_ring_entries * sizeof(*ss->tx.info);
  1742. ss->tx.info = kzalloc(bytes, GFP_KERNEL);
  1743. if (ss->tx.info == NULL)
  1744. goto abort_with_rx_big_shadow;
  1745. bytes = rx_ring_entries * sizeof(*ss->rx_small.info);
  1746. ss->rx_small.info = kzalloc(bytes, GFP_KERNEL);
  1747. if (ss->rx_small.info == NULL)
  1748. goto abort_with_tx_info;
  1749. bytes = rx_ring_entries * sizeof(*ss->rx_big.info);
  1750. ss->rx_big.info = kzalloc(bytes, GFP_KERNEL);
  1751. if (ss->rx_big.info == NULL)
  1752. goto abort_with_rx_small_info;
  1753. /* Fill the receive rings */
  1754. ss->rx_big.cnt = 0;
  1755. ss->rx_small.cnt = 0;
  1756. ss->rx_big.fill_cnt = 0;
  1757. ss->rx_small.fill_cnt = 0;
  1758. ss->rx_small.page_offset = MYRI10GE_ALLOC_SIZE;
  1759. ss->rx_big.page_offset = MYRI10GE_ALLOC_SIZE;
  1760. ss->rx_small.watchdog_needed = 0;
  1761. ss->rx_big.watchdog_needed = 0;
  1762. if (mgp->small_bytes == 0) {
  1763. ss->rx_small.fill_cnt = ss->rx_small.mask + 1;
  1764. } else {
  1765. myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
  1766. mgp->small_bytes + MXGEFW_PAD, 0);
  1767. }
  1768. if (ss->rx_small.fill_cnt < ss->rx_small.mask + 1) {
  1769. netdev_err(dev, "slice-%d: alloced only %d small bufs\n",
  1770. slice, ss->rx_small.fill_cnt);
  1771. goto abort_with_rx_small_ring;
  1772. }
  1773. myri10ge_alloc_rx_pages(mgp, &ss->rx_big, mgp->big_bytes, 0);
  1774. if (ss->rx_big.fill_cnt < ss->rx_big.mask + 1) {
  1775. netdev_err(dev, "slice-%d: alloced only %d big bufs\n",
  1776. slice, ss->rx_big.fill_cnt);
  1777. goto abort_with_rx_big_ring;
  1778. }
  1779. return 0;
  1780. abort_with_rx_big_ring:
  1781. for (i = ss->rx_big.cnt; i < ss->rx_big.fill_cnt; i++) {
  1782. int idx = i & ss->rx_big.mask;
  1783. myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_big.info[idx],
  1784. mgp->big_bytes);
  1785. put_page(ss->rx_big.info[idx].page);
  1786. }
  1787. abort_with_rx_small_ring:
  1788. if (mgp->small_bytes == 0)
  1789. ss->rx_small.fill_cnt = ss->rx_small.cnt;
  1790. for (i = ss->rx_small.cnt; i < ss->rx_small.fill_cnt; i++) {
  1791. int idx = i & ss->rx_small.mask;
  1792. myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_small.info[idx],
  1793. mgp->small_bytes + MXGEFW_PAD);
  1794. put_page(ss->rx_small.info[idx].page);
  1795. }
  1796. kfree(ss->rx_big.info);
  1797. abort_with_rx_small_info:
  1798. kfree(ss->rx_small.info);
  1799. abort_with_tx_info:
  1800. kfree(ss->tx.info);
  1801. abort_with_rx_big_shadow:
  1802. kfree(ss->rx_big.shadow);
  1803. abort_with_rx_small_shadow:
  1804. kfree(ss->rx_small.shadow);
  1805. abort_with_tx_req_bytes:
  1806. kfree(ss->tx.req_bytes);
  1807. ss->tx.req_bytes = NULL;
  1808. ss->tx.req_list = NULL;
  1809. abort_with_nothing:
  1810. return status;
  1811. }
  1812. static void myri10ge_free_rings(struct myri10ge_slice_state *ss)
  1813. {
  1814. struct myri10ge_priv *mgp = ss->mgp;
  1815. struct sk_buff *skb;
  1816. struct myri10ge_tx_buf *tx;
  1817. int i, len, idx;
  1818. /* If not allocated, skip it */
  1819. if (ss->tx.req_list == NULL)
  1820. return;
  1821. for (i = ss->rx_big.cnt; i < ss->rx_big.fill_cnt; i++) {
  1822. idx = i & ss->rx_big.mask;
  1823. if (i == ss->rx_big.fill_cnt - 1)
  1824. ss->rx_big.info[idx].page_offset = MYRI10GE_ALLOC_SIZE;
  1825. myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_big.info[idx],
  1826. mgp->big_bytes);
  1827. put_page(ss->rx_big.info[idx].page);
  1828. }
  1829. if (mgp->small_bytes == 0)
  1830. ss->rx_small.fill_cnt = ss->rx_small.cnt;
  1831. for (i = ss->rx_small.cnt; i < ss->rx_small.fill_cnt; i++) {
  1832. idx = i & ss->rx_small.mask;
  1833. if (i == ss->rx_small.fill_cnt - 1)
  1834. ss->rx_small.info[idx].page_offset =
  1835. MYRI10GE_ALLOC_SIZE;
  1836. myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_small.info[idx],
  1837. mgp->small_bytes + MXGEFW_PAD);
  1838. put_page(ss->rx_small.info[idx].page);
  1839. }
  1840. tx = &ss->tx;
  1841. while (tx->done != tx->req) {
  1842. idx = tx->done & tx->mask;
  1843. skb = tx->info[idx].skb;
  1844. /* Mark as free */
  1845. tx->info[idx].skb = NULL;
  1846. tx->done++;
  1847. len = dma_unmap_len(&tx->info[idx], len);
  1848. dma_unmap_len_set(&tx->info[idx], len, 0);
  1849. if (skb) {
  1850. ss->stats.tx_dropped++;
  1851. dev_kfree_skb_any(skb);
  1852. if (len)
  1853. pci_unmap_single(mgp->pdev,
  1854. dma_unmap_addr(&tx->info[idx],
  1855. bus), len,
  1856. PCI_DMA_TODEVICE);
  1857. } else {
  1858. if (len)
  1859. pci_unmap_page(mgp->pdev,
  1860. dma_unmap_addr(&tx->info[idx],
  1861. bus), len,
  1862. PCI_DMA_TODEVICE);
  1863. }
  1864. }
  1865. kfree(ss->rx_big.info);
  1866. kfree(ss->rx_small.info);
  1867. kfree(ss->tx.info);
  1868. kfree(ss->rx_big.shadow);
  1869. kfree(ss->rx_small.shadow);
  1870. kfree(ss->tx.req_bytes);
  1871. ss->tx.req_bytes = NULL;
  1872. ss->tx.req_list = NULL;
  1873. }
  1874. static int myri10ge_request_irq(struct myri10ge_priv *mgp)
  1875. {
  1876. struct pci_dev *pdev = mgp->pdev;
  1877. struct myri10ge_slice_state *ss;
  1878. struct net_device *netdev = mgp->dev;
  1879. int i;
  1880. int status;
  1881. mgp->msi_enabled = 0;
  1882. mgp->msix_enabled = 0;
  1883. status = 0;
  1884. if (myri10ge_msi) {
  1885. if (mgp->num_slices > 1) {
  1886. status = pci_enable_msix_range(pdev, mgp->msix_vectors,
  1887. mgp->num_slices, mgp->num_slices);
  1888. if (status < 0) {
  1889. dev_err(&pdev->dev,
  1890. "Error %d setting up MSI-X\n", status);
  1891. return status;
  1892. }
  1893. mgp->msix_enabled = 1;
  1894. }
  1895. if (mgp->msix_enabled == 0) {
  1896. status = pci_enable_msi(pdev);
  1897. if (status != 0) {
  1898. dev_err(&pdev->dev,
  1899. "Error %d setting up MSI; falling back to xPIC\n",
  1900. status);
  1901. } else {
  1902. mgp->msi_enabled = 1;
  1903. }
  1904. }
  1905. }
  1906. if (mgp->msix_enabled) {
  1907. for (i = 0; i < mgp->num_slices; i++) {
  1908. ss = &mgp->ss[i];
  1909. snprintf(ss->irq_desc, sizeof(ss->irq_desc),
  1910. "%s:slice-%d", netdev->name, i);
  1911. status = request_irq(mgp->msix_vectors[i].vector,
  1912. myri10ge_intr, 0, ss->irq_desc,
  1913. ss);
  1914. if (status != 0) {
  1915. dev_err(&pdev->dev,
  1916. "slice %d failed to allocate IRQ\n", i);
  1917. i--;
  1918. while (i >= 0) {
  1919. free_irq(mgp->msix_vectors[i].vector,
  1920. &mgp->ss[i]);
  1921. i--;
  1922. }
  1923. pci_disable_msix(pdev);
  1924. return status;
  1925. }
  1926. }
  1927. } else {
  1928. status = request_irq(pdev->irq, myri10ge_intr, IRQF_SHARED,
  1929. mgp->dev->name, &mgp->ss[0]);
  1930. if (status != 0) {
  1931. dev_err(&pdev->dev, "failed to allocate IRQ\n");
  1932. if (mgp->msi_enabled)
  1933. pci_disable_msi(pdev);
  1934. }
  1935. }
  1936. return status;
  1937. }
  1938. static void myri10ge_free_irq(struct myri10ge_priv *mgp)
  1939. {
  1940. struct pci_dev *pdev = mgp->pdev;
  1941. int i;
  1942. if (mgp->msix_enabled) {
  1943. for (i = 0; i < mgp->num_slices; i++)
  1944. free_irq(mgp->msix_vectors[i].vector, &mgp->ss[i]);
  1945. } else {
  1946. free_irq(pdev->irq, &mgp->ss[0]);
  1947. }
  1948. if (mgp->msi_enabled)
  1949. pci_disable_msi(pdev);
  1950. if (mgp->msix_enabled)
  1951. pci_disable_msix(pdev);
  1952. }
  1953. static int myri10ge_get_txrx(struct myri10ge_priv *mgp, int slice)
  1954. {
  1955. struct myri10ge_cmd cmd;
  1956. struct myri10ge_slice_state *ss;
  1957. int status;
  1958. ss = &mgp->ss[slice];
  1959. status = 0;
  1960. if (slice == 0 || (mgp->dev->real_num_tx_queues > 1)) {
  1961. cmd.data0 = slice;
  1962. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_OFFSET,
  1963. &cmd, 0);
  1964. ss->tx.lanai = (struct mcp_kreq_ether_send __iomem *)
  1965. (mgp->sram + cmd.data0);
  1966. }
  1967. cmd.data0 = slice;
  1968. status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SMALL_RX_OFFSET,
  1969. &cmd, 0);
  1970. ss->rx_small.lanai = (struct mcp_kreq_ether_recv __iomem *)
  1971. (mgp->sram + cmd.data0);
  1972. cmd.data0 = slice;
  1973. status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_BIG_RX_OFFSET, &cmd, 0);
  1974. ss->rx_big.lanai = (struct mcp_kreq_ether_recv __iomem *)
  1975. (mgp->sram + cmd.data0);
  1976. ss->tx.send_go = (__iomem __be32 *)
  1977. (mgp->sram + MXGEFW_ETH_SEND_GO + 64 * slice);
  1978. ss->tx.send_stop = (__iomem __be32 *)
  1979. (mgp->sram + MXGEFW_ETH_SEND_STOP + 64 * slice);
  1980. return status;
  1981. }
  1982. static int myri10ge_set_stats(struct myri10ge_priv *mgp, int slice)
  1983. {
  1984. struct myri10ge_cmd cmd;
  1985. struct myri10ge_slice_state *ss;
  1986. int status;
  1987. ss = &mgp->ss[slice];
  1988. cmd.data0 = MYRI10GE_LOWPART_TO_U32(ss->fw_stats_bus);
  1989. cmd.data1 = MYRI10GE_HIGHPART_TO_U32(ss->fw_stats_bus);
  1990. cmd.data2 = sizeof(struct mcp_irq_data) | (slice << 16);
  1991. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_STATS_DMA_V2, &cmd, 0);
  1992. if (status == -ENOSYS) {
  1993. dma_addr_t bus = ss->fw_stats_bus;
  1994. if (slice != 0)
  1995. return -EINVAL;
  1996. bus += offsetof(struct mcp_irq_data, send_done_count);
  1997. cmd.data0 = MYRI10GE_LOWPART_TO_U32(bus);
  1998. cmd.data1 = MYRI10GE_HIGHPART_TO_U32(bus);
  1999. status = myri10ge_send_cmd(mgp,
  2000. MXGEFW_CMD_SET_STATS_DMA_OBSOLETE,
  2001. &cmd, 0);
  2002. /* Firmware cannot support multicast without STATS_DMA_V2 */
  2003. mgp->fw_multicast_support = 0;
  2004. } else {
  2005. mgp->fw_multicast_support = 1;
  2006. }
  2007. return 0;
  2008. }
  2009. static int myri10ge_open(struct net_device *dev)
  2010. {
  2011. struct myri10ge_slice_state *ss;
  2012. struct myri10ge_priv *mgp = netdev_priv(dev);
  2013. struct myri10ge_cmd cmd;
  2014. int i, status, big_pow2, slice;
  2015. u8 __iomem *itable;
  2016. if (mgp->running != MYRI10GE_ETH_STOPPED)
  2017. return -EBUSY;
  2018. mgp->running = MYRI10GE_ETH_STARTING;
  2019. status = myri10ge_reset(mgp);
  2020. if (status != 0) {
  2021. netdev_err(dev, "failed reset\n");
  2022. goto abort_with_nothing;
  2023. }
  2024. if (mgp->num_slices > 1) {
  2025. cmd.data0 = mgp->num_slices;
  2026. cmd.data1 = MXGEFW_SLICE_INTR_MODE_ONE_PER_SLICE;
  2027. if (mgp->dev->real_num_tx_queues > 1)
  2028. cmd.data1 |= MXGEFW_SLICE_ENABLE_MULTIPLE_TX_QUEUES;
  2029. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ENABLE_RSS_QUEUES,
  2030. &cmd, 0);
  2031. if (status != 0) {
  2032. netdev_err(dev, "failed to set number of slices\n");
  2033. goto abort_with_nothing;
  2034. }
  2035. /* setup the indirection table */
  2036. cmd.data0 = mgp->num_slices;
  2037. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_RSS_TABLE_SIZE,
  2038. &cmd, 0);
  2039. status |= myri10ge_send_cmd(mgp,
  2040. MXGEFW_CMD_GET_RSS_TABLE_OFFSET,
  2041. &cmd, 0);
  2042. if (status != 0) {
  2043. netdev_err(dev, "failed to setup rss tables\n");
  2044. goto abort_with_nothing;
  2045. }
  2046. /* just enable an identity mapping */
  2047. itable = mgp->sram + cmd.data0;
  2048. for (i = 0; i < mgp->num_slices; i++)
  2049. __raw_writeb(i, &itable[i]);
  2050. cmd.data0 = 1;
  2051. cmd.data1 = myri10ge_rss_hash;
  2052. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_RSS_ENABLE,
  2053. &cmd, 0);
  2054. if (status != 0) {
  2055. netdev_err(dev, "failed to enable slices\n");
  2056. goto abort_with_nothing;
  2057. }
  2058. }
  2059. status = myri10ge_request_irq(mgp);
  2060. if (status != 0)
  2061. goto abort_with_nothing;
  2062. /* decide what small buffer size to use. For good TCP rx
  2063. * performance, it is important to not receive 1514 byte
  2064. * frames into jumbo buffers, as it confuses the socket buffer
  2065. * accounting code, leading to drops and erratic performance.
  2066. */
  2067. if (dev->mtu <= ETH_DATA_LEN)
  2068. /* enough for a TCP header */
  2069. mgp->small_bytes = (128 > SMP_CACHE_BYTES)
  2070. ? (128 - MXGEFW_PAD)
  2071. : (SMP_CACHE_BYTES - MXGEFW_PAD);
  2072. else
  2073. /* enough for a vlan encapsulated ETH_DATA_LEN frame */
  2074. mgp->small_bytes = VLAN_ETH_FRAME_LEN;
  2075. /* Override the small buffer size? */
  2076. if (myri10ge_small_bytes >= 0)
  2077. mgp->small_bytes = myri10ge_small_bytes;
  2078. /* Firmware needs the big buff size as a power of 2. Lie and
  2079. * tell him the buffer is larger, because we only use 1
  2080. * buffer/pkt, and the mtu will prevent overruns.
  2081. */
  2082. big_pow2 = dev->mtu + ETH_HLEN + VLAN_HLEN + MXGEFW_PAD;
  2083. if (big_pow2 < MYRI10GE_ALLOC_SIZE / 2) {
  2084. while (!is_power_of_2(big_pow2))
  2085. big_pow2++;
  2086. mgp->big_bytes = dev->mtu + ETH_HLEN + VLAN_HLEN + MXGEFW_PAD;
  2087. } else {
  2088. big_pow2 = MYRI10GE_ALLOC_SIZE;
  2089. mgp->big_bytes = big_pow2;
  2090. }
  2091. /* setup the per-slice data structures */
  2092. for (slice = 0; slice < mgp->num_slices; slice++) {
  2093. ss = &mgp->ss[slice];
  2094. status = myri10ge_get_txrx(mgp, slice);
  2095. if (status != 0) {
  2096. netdev_err(dev, "failed to get ring sizes or locations\n");
  2097. goto abort_with_rings;
  2098. }
  2099. status = myri10ge_allocate_rings(ss);
  2100. if (status != 0)
  2101. goto abort_with_rings;
  2102. /* only firmware which supports multiple TX queues
  2103. * supports setting up the tx stats on non-zero
  2104. * slices */
  2105. if (slice == 0 || mgp->dev->real_num_tx_queues > 1)
  2106. status = myri10ge_set_stats(mgp, slice);
  2107. if (status) {
  2108. netdev_err(dev, "Couldn't set stats DMA\n");
  2109. goto abort_with_rings;
  2110. }
  2111. /* must happen prior to any irq */
  2112. napi_enable(&(ss)->napi);
  2113. }
  2114. /* now give firmware buffers sizes, and MTU */
  2115. cmd.data0 = dev->mtu + ETH_HLEN + VLAN_HLEN;
  2116. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_MTU, &cmd, 0);
  2117. cmd.data0 = mgp->small_bytes;
  2118. status |=
  2119. myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_SMALL_BUFFER_SIZE, &cmd, 0);
  2120. cmd.data0 = big_pow2;
  2121. status |=
  2122. myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_BIG_BUFFER_SIZE, &cmd, 0);
  2123. if (status) {
  2124. netdev_err(dev, "Couldn't set buffer sizes\n");
  2125. goto abort_with_rings;
  2126. }
  2127. /*
  2128. * Set Linux style TSO mode; this is needed only on newer
  2129. * firmware versions. Older versions default to Linux
  2130. * style TSO
  2131. */
  2132. cmd.data0 = 0;
  2133. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_TSO_MODE, &cmd, 0);
  2134. if (status && status != -ENOSYS) {
  2135. netdev_err(dev, "Couldn't set TSO mode\n");
  2136. goto abort_with_rings;
  2137. }
  2138. mgp->link_state = ~0U;
  2139. mgp->rdma_tags_available = 15;
  2140. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_UP, &cmd, 0);
  2141. if (status) {
  2142. netdev_err(dev, "Couldn't bring up link\n");
  2143. goto abort_with_rings;
  2144. }
  2145. mgp->running = MYRI10GE_ETH_RUNNING;
  2146. mgp->watchdog_timer.expires = jiffies + myri10ge_watchdog_timeout * HZ;
  2147. add_timer(&mgp->watchdog_timer);
  2148. netif_tx_wake_all_queues(dev);
  2149. return 0;
  2150. abort_with_rings:
  2151. while (slice) {
  2152. slice--;
  2153. napi_disable(&mgp->ss[slice].napi);
  2154. }
  2155. for (i = 0; i < mgp->num_slices; i++)
  2156. myri10ge_free_rings(&mgp->ss[i]);
  2157. myri10ge_free_irq(mgp);
  2158. abort_with_nothing:
  2159. mgp->running = MYRI10GE_ETH_STOPPED;
  2160. return -ENOMEM;
  2161. }
  2162. static int myri10ge_close(struct net_device *dev)
  2163. {
  2164. struct myri10ge_priv *mgp = netdev_priv(dev);
  2165. struct myri10ge_cmd cmd;
  2166. int status, old_down_cnt;
  2167. int i;
  2168. if (mgp->running != MYRI10GE_ETH_RUNNING)
  2169. return 0;
  2170. if (mgp->ss[0].tx.req_bytes == NULL)
  2171. return 0;
  2172. del_timer_sync(&mgp->watchdog_timer);
  2173. mgp->running = MYRI10GE_ETH_STOPPING;
  2174. for (i = 0; i < mgp->num_slices; i++)
  2175. napi_disable(&mgp->ss[i].napi);
  2176. netif_carrier_off(dev);
  2177. netif_tx_stop_all_queues(dev);
  2178. if (mgp->rebooted == 0) {
  2179. old_down_cnt = mgp->down_cnt;
  2180. mb();
  2181. status =
  2182. myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_DOWN, &cmd, 0);
  2183. if (status)
  2184. netdev_err(dev, "Couldn't bring down link\n");
  2185. wait_event_timeout(mgp->down_wq, old_down_cnt != mgp->down_cnt,
  2186. HZ);
  2187. if (old_down_cnt == mgp->down_cnt)
  2188. netdev_err(dev, "never got down irq\n");
  2189. }
  2190. netif_tx_disable(dev);
  2191. myri10ge_free_irq(mgp);
  2192. for (i = 0; i < mgp->num_slices; i++)
  2193. myri10ge_free_rings(&mgp->ss[i]);
  2194. mgp->running = MYRI10GE_ETH_STOPPED;
  2195. return 0;
  2196. }
  2197. /* copy an array of struct mcp_kreq_ether_send's to the mcp. Copy
  2198. * backwards one at a time and handle ring wraps */
  2199. static inline void
  2200. myri10ge_submit_req_backwards(struct myri10ge_tx_buf *tx,
  2201. struct mcp_kreq_ether_send *src, int cnt)
  2202. {
  2203. int idx, starting_slot;
  2204. starting_slot = tx->req;
  2205. while (cnt > 1) {
  2206. cnt--;
  2207. idx = (starting_slot + cnt) & tx->mask;
  2208. myri10ge_pio_copy(&tx->lanai[idx], &src[cnt], sizeof(*src));
  2209. mb();
  2210. }
  2211. }
  2212. /*
  2213. * copy an array of struct mcp_kreq_ether_send's to the mcp. Copy
  2214. * at most 32 bytes at a time, so as to avoid involving the software
  2215. * pio handler in the nic. We re-write the first segment's flags
  2216. * to mark them valid only after writing the entire chain.
  2217. */
  2218. static inline void
  2219. myri10ge_submit_req(struct myri10ge_tx_buf *tx, struct mcp_kreq_ether_send *src,
  2220. int cnt)
  2221. {
  2222. int idx, i;
  2223. struct mcp_kreq_ether_send __iomem *dstp, *dst;
  2224. struct mcp_kreq_ether_send *srcp;
  2225. u8 last_flags;
  2226. idx = tx->req & tx->mask;
  2227. last_flags = src->flags;
  2228. src->flags = 0;
  2229. mb();
  2230. dst = dstp = &tx->lanai[idx];
  2231. srcp = src;
  2232. if ((idx + cnt) < tx->mask) {
  2233. for (i = 0; i < (cnt - 1); i += 2) {
  2234. myri10ge_pio_copy(dstp, srcp, 2 * sizeof(*src));
  2235. mb(); /* force write every 32 bytes */
  2236. srcp += 2;
  2237. dstp += 2;
  2238. }
  2239. } else {
  2240. /* submit all but the first request, and ensure
  2241. * that it is submitted below */
  2242. myri10ge_submit_req_backwards(tx, src, cnt);
  2243. i = 0;
  2244. }
  2245. if (i < cnt) {
  2246. /* submit the first request */
  2247. myri10ge_pio_copy(dstp, srcp, sizeof(*src));
  2248. mb(); /* barrier before setting valid flag */
  2249. }
  2250. /* re-write the last 32-bits with the valid flags */
  2251. src->flags = last_flags;
  2252. put_be32(*((__be32 *) src + 3), (__be32 __iomem *) dst + 3);
  2253. tx->req += cnt;
  2254. mb();
  2255. }
  2256. static void myri10ge_unmap_tx_dma(struct myri10ge_priv *mgp,
  2257. struct myri10ge_tx_buf *tx, int idx)
  2258. {
  2259. unsigned int len;
  2260. int last_idx;
  2261. /* Free any DMA resources we've alloced and clear out the skb slot */
  2262. last_idx = (idx + 1) & tx->mask;
  2263. idx = tx->req & tx->mask;
  2264. do {
  2265. len = dma_unmap_len(&tx->info[idx], len);
  2266. if (len) {
  2267. if (tx->info[idx].skb != NULL)
  2268. pci_unmap_single(mgp->pdev,
  2269. dma_unmap_addr(&tx->info[idx],
  2270. bus), len,
  2271. PCI_DMA_TODEVICE);
  2272. else
  2273. pci_unmap_page(mgp->pdev,
  2274. dma_unmap_addr(&tx->info[idx],
  2275. bus), len,
  2276. PCI_DMA_TODEVICE);
  2277. dma_unmap_len_set(&tx->info[idx], len, 0);
  2278. tx->info[idx].skb = NULL;
  2279. }
  2280. idx = (idx + 1) & tx->mask;
  2281. } while (idx != last_idx);
  2282. }
  2283. /*
  2284. * Transmit a packet. We need to split the packet so that a single
  2285. * segment does not cross myri10ge->tx_boundary, so this makes segment
  2286. * counting tricky. So rather than try to count segments up front, we
  2287. * just give up if there are too few segments to hold a reasonably
  2288. * fragmented packet currently available. If we run
  2289. * out of segments while preparing a packet for DMA, we just linearize
  2290. * it and try again.
  2291. */
  2292. static netdev_tx_t myri10ge_xmit(struct sk_buff *skb,
  2293. struct net_device *dev)
  2294. {
  2295. struct myri10ge_priv *mgp = netdev_priv(dev);
  2296. struct myri10ge_slice_state *ss;
  2297. struct mcp_kreq_ether_send *req;
  2298. struct myri10ge_tx_buf *tx;
  2299. struct skb_frag_struct *frag;
  2300. struct netdev_queue *netdev_queue;
  2301. dma_addr_t bus;
  2302. u32 low;
  2303. __be32 high_swapped;
  2304. unsigned int len;
  2305. int idx, avail, frag_cnt, frag_idx, count, mss, max_segments;
  2306. u16 pseudo_hdr_offset, cksum_offset, queue;
  2307. int cum_len, seglen, boundary, rdma_count;
  2308. u8 flags, odd_flag;
  2309. queue = skb_get_queue_mapping(skb);
  2310. ss = &mgp->ss[queue];
  2311. netdev_queue = netdev_get_tx_queue(mgp->dev, queue);
  2312. tx = &ss->tx;
  2313. again:
  2314. req = tx->req_list;
  2315. avail = tx->mask - 1 - (tx->req - tx->done);
  2316. mss = 0;
  2317. max_segments = MXGEFW_MAX_SEND_DESC;
  2318. if (skb_is_gso(skb)) {
  2319. mss = skb_shinfo(skb)->gso_size;
  2320. max_segments = MYRI10GE_MAX_SEND_DESC_TSO;
  2321. }
  2322. if ((unlikely(avail < max_segments))) {
  2323. /* we are out of transmit resources */
  2324. tx->stop_queue++;
  2325. netif_tx_stop_queue(netdev_queue);
  2326. return NETDEV_TX_BUSY;
  2327. }
  2328. /* Setup checksum offloading, if needed */
  2329. cksum_offset = 0;
  2330. pseudo_hdr_offset = 0;
  2331. odd_flag = 0;
  2332. flags = (MXGEFW_FLAGS_NO_TSO | MXGEFW_FLAGS_FIRST);
  2333. if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
  2334. cksum_offset = skb_checksum_start_offset(skb);
  2335. pseudo_hdr_offset = cksum_offset + skb->csum_offset;
  2336. /* If the headers are excessively large, then we must
  2337. * fall back to a software checksum */
  2338. if (unlikely(!mss && (cksum_offset > 255 ||
  2339. pseudo_hdr_offset > 127))) {
  2340. if (skb_checksum_help(skb))
  2341. goto drop;
  2342. cksum_offset = 0;
  2343. pseudo_hdr_offset = 0;
  2344. } else {
  2345. odd_flag = MXGEFW_FLAGS_ALIGN_ODD;
  2346. flags |= MXGEFW_FLAGS_CKSUM;
  2347. }
  2348. }
  2349. cum_len = 0;
  2350. if (mss) { /* TSO */
  2351. /* this removes any CKSUM flag from before */
  2352. flags = (MXGEFW_FLAGS_TSO_HDR | MXGEFW_FLAGS_FIRST);
  2353. /* negative cum_len signifies to the
  2354. * send loop that we are still in the
  2355. * header portion of the TSO packet.
  2356. * TSO header can be at most 1KB long */
  2357. cum_len = -(skb_transport_offset(skb) + tcp_hdrlen(skb));
  2358. /* for IPv6 TSO, the checksum offset stores the
  2359. * TCP header length, to save the firmware from
  2360. * the need to parse the headers */
  2361. if (skb_is_gso_v6(skb)) {
  2362. cksum_offset = tcp_hdrlen(skb);
  2363. /* Can only handle headers <= max_tso6 long */
  2364. if (unlikely(-cum_len > mgp->max_tso6))
  2365. return myri10ge_sw_tso(skb, dev);
  2366. }
  2367. /* for TSO, pseudo_hdr_offset holds mss.
  2368. * The firmware figures out where to put
  2369. * the checksum by parsing the header. */
  2370. pseudo_hdr_offset = mss;
  2371. } else
  2372. /* Mark small packets, and pad out tiny packets */
  2373. if (skb->len <= MXGEFW_SEND_SMALL_SIZE) {
  2374. flags |= MXGEFW_FLAGS_SMALL;
  2375. /* pad frames to at least ETH_ZLEN bytes */
  2376. if (eth_skb_pad(skb)) {
  2377. /* The packet is gone, so we must
  2378. * return 0 */
  2379. ss->stats.tx_dropped += 1;
  2380. return NETDEV_TX_OK;
  2381. }
  2382. }
  2383. /* map the skb for DMA */
  2384. len = skb_headlen(skb);
  2385. bus = pci_map_single(mgp->pdev, skb->data, len, PCI_DMA_TODEVICE);
  2386. if (unlikely(pci_dma_mapping_error(mgp->pdev, bus)))
  2387. goto drop;
  2388. idx = tx->req & tx->mask;
  2389. tx->info[idx].skb = skb;
  2390. dma_unmap_addr_set(&tx->info[idx], bus, bus);
  2391. dma_unmap_len_set(&tx->info[idx], len, len);
  2392. frag_cnt = skb_shinfo(skb)->nr_frags;
  2393. frag_idx = 0;
  2394. count = 0;
  2395. rdma_count = 0;
  2396. /* "rdma_count" is the number of RDMAs belonging to the
  2397. * current packet BEFORE the current send request. For
  2398. * non-TSO packets, this is equal to "count".
  2399. * For TSO packets, rdma_count needs to be reset
  2400. * to 0 after a segment cut.
  2401. *
  2402. * The rdma_count field of the send request is
  2403. * the number of RDMAs of the packet starting at
  2404. * that request. For TSO send requests with one ore more cuts
  2405. * in the middle, this is the number of RDMAs starting
  2406. * after the last cut in the request. All previous
  2407. * segments before the last cut implicitly have 1 RDMA.
  2408. *
  2409. * Since the number of RDMAs is not known beforehand,
  2410. * it must be filled-in retroactively - after each
  2411. * segmentation cut or at the end of the entire packet.
  2412. */
  2413. while (1) {
  2414. /* Break the SKB or Fragment up into pieces which
  2415. * do not cross mgp->tx_boundary */
  2416. low = MYRI10GE_LOWPART_TO_U32(bus);
  2417. high_swapped = htonl(MYRI10GE_HIGHPART_TO_U32(bus));
  2418. while (len) {
  2419. u8 flags_next;
  2420. int cum_len_next;
  2421. if (unlikely(count == max_segments))
  2422. goto abort_linearize;
  2423. boundary =
  2424. (low + mgp->tx_boundary) & ~(mgp->tx_boundary - 1);
  2425. seglen = boundary - low;
  2426. if (seglen > len)
  2427. seglen = len;
  2428. flags_next = flags & ~MXGEFW_FLAGS_FIRST;
  2429. cum_len_next = cum_len + seglen;
  2430. if (mss) { /* TSO */
  2431. (req - rdma_count)->rdma_count = rdma_count + 1;
  2432. if (likely(cum_len >= 0)) { /* payload */
  2433. int next_is_first, chop;
  2434. chop = (cum_len_next > mss);
  2435. cum_len_next = cum_len_next % mss;
  2436. next_is_first = (cum_len_next == 0);
  2437. flags |= chop * MXGEFW_FLAGS_TSO_CHOP;
  2438. flags_next |= next_is_first *
  2439. MXGEFW_FLAGS_FIRST;
  2440. rdma_count |= -(chop | next_is_first);
  2441. rdma_count += chop & ~next_is_first;
  2442. } else if (likely(cum_len_next >= 0)) { /* header ends */
  2443. int small;
  2444. rdma_count = -1;
  2445. cum_len_next = 0;
  2446. seglen = -cum_len;
  2447. small = (mss <= MXGEFW_SEND_SMALL_SIZE);
  2448. flags_next = MXGEFW_FLAGS_TSO_PLD |
  2449. MXGEFW_FLAGS_FIRST |
  2450. (small * MXGEFW_FLAGS_SMALL);
  2451. }
  2452. }
  2453. req->addr_high = high_swapped;
  2454. req->addr_low = htonl(low);
  2455. req->pseudo_hdr_offset = htons(pseudo_hdr_offset);
  2456. req->pad = 0; /* complete solid 16-byte block; does this matter? */
  2457. req->rdma_count = 1;
  2458. req->length = htons(seglen);
  2459. req->cksum_offset = cksum_offset;
  2460. req->flags = flags | ((cum_len & 1) * odd_flag);
  2461. low += seglen;
  2462. len -= seglen;
  2463. cum_len = cum_len_next;
  2464. flags = flags_next;
  2465. req++;
  2466. count++;
  2467. rdma_count++;
  2468. if (cksum_offset != 0 && !(mss && skb_is_gso_v6(skb))) {
  2469. if (unlikely(cksum_offset > seglen))
  2470. cksum_offset -= seglen;
  2471. else
  2472. cksum_offset = 0;
  2473. }
  2474. }
  2475. if (frag_idx == frag_cnt)
  2476. break;
  2477. /* map next fragment for DMA */
  2478. frag = &skb_shinfo(skb)->frags[frag_idx];
  2479. frag_idx++;
  2480. len = skb_frag_size(frag);
  2481. bus = skb_frag_dma_map(&mgp->pdev->dev, frag, 0, len,
  2482. DMA_TO_DEVICE);
  2483. if (unlikely(pci_dma_mapping_error(mgp->pdev, bus))) {
  2484. myri10ge_unmap_tx_dma(mgp, tx, idx);
  2485. goto drop;
  2486. }
  2487. idx = (count + tx->req) & tx->mask;
  2488. dma_unmap_addr_set(&tx->info[idx], bus, bus);
  2489. dma_unmap_len_set(&tx->info[idx], len, len);
  2490. }
  2491. (req - rdma_count)->rdma_count = rdma_count;
  2492. if (mss)
  2493. do {
  2494. req--;
  2495. req->flags |= MXGEFW_FLAGS_TSO_LAST;
  2496. } while (!(req->flags & (MXGEFW_FLAGS_TSO_CHOP |
  2497. MXGEFW_FLAGS_FIRST)));
  2498. idx = ((count - 1) + tx->req) & tx->mask;
  2499. tx->info[idx].last = 1;
  2500. myri10ge_submit_req(tx, tx->req_list, count);
  2501. /* if using multiple tx queues, make sure NIC polls the
  2502. * current slice */
  2503. if ((mgp->dev->real_num_tx_queues > 1) && tx->queue_active == 0) {
  2504. tx->queue_active = 1;
  2505. put_be32(htonl(1), tx->send_go);
  2506. mb();
  2507. mmiowb();
  2508. }
  2509. tx->pkt_start++;
  2510. if ((avail - count) < MXGEFW_MAX_SEND_DESC) {
  2511. tx->stop_queue++;
  2512. netif_tx_stop_queue(netdev_queue);
  2513. }
  2514. return NETDEV_TX_OK;
  2515. abort_linearize:
  2516. myri10ge_unmap_tx_dma(mgp, tx, idx);
  2517. if (skb_is_gso(skb)) {
  2518. netdev_err(mgp->dev, "TSO but wanted to linearize?!?!?\n");
  2519. goto drop;
  2520. }
  2521. if (skb_linearize(skb))
  2522. goto drop;
  2523. tx->linearized++;
  2524. goto again;
  2525. drop:
  2526. dev_kfree_skb_any(skb);
  2527. ss->stats.tx_dropped += 1;
  2528. return NETDEV_TX_OK;
  2529. }
  2530. static netdev_tx_t myri10ge_sw_tso(struct sk_buff *skb,
  2531. struct net_device *dev)
  2532. {
  2533. struct sk_buff *segs, *curr;
  2534. struct myri10ge_priv *mgp = netdev_priv(dev);
  2535. struct myri10ge_slice_state *ss;
  2536. netdev_tx_t status;
  2537. segs = skb_gso_segment(skb, dev->features & ~NETIF_F_TSO6);
  2538. if (IS_ERR(segs))
  2539. goto drop;
  2540. while (segs) {
  2541. curr = segs;
  2542. segs = segs->next;
  2543. curr->next = NULL;
  2544. status = myri10ge_xmit(curr, dev);
  2545. if (status != 0) {
  2546. dev_kfree_skb_any(curr);
  2547. if (segs != NULL) {
  2548. curr = segs;
  2549. segs = segs->next;
  2550. curr->next = NULL;
  2551. dev_kfree_skb_any(segs);
  2552. }
  2553. goto drop;
  2554. }
  2555. }
  2556. dev_kfree_skb_any(skb);
  2557. return NETDEV_TX_OK;
  2558. drop:
  2559. ss = &mgp->ss[skb_get_queue_mapping(skb)];
  2560. dev_kfree_skb_any(skb);
  2561. ss->stats.tx_dropped += 1;
  2562. return NETDEV_TX_OK;
  2563. }
  2564. static void myri10ge_get_stats(struct net_device *dev,
  2565. struct rtnl_link_stats64 *stats)
  2566. {
  2567. const struct myri10ge_priv *mgp = netdev_priv(dev);
  2568. const struct myri10ge_slice_netstats *slice_stats;
  2569. int i;
  2570. for (i = 0; i < mgp->num_slices; i++) {
  2571. slice_stats = &mgp->ss[i].stats;
  2572. stats->rx_packets += slice_stats->rx_packets;
  2573. stats->tx_packets += slice_stats->tx_packets;
  2574. stats->rx_bytes += slice_stats->rx_bytes;
  2575. stats->tx_bytes += slice_stats->tx_bytes;
  2576. stats->rx_dropped += slice_stats->rx_dropped;
  2577. stats->tx_dropped += slice_stats->tx_dropped;
  2578. }
  2579. }
  2580. static void myri10ge_set_multicast_list(struct net_device *dev)
  2581. {
  2582. struct myri10ge_priv *mgp = netdev_priv(dev);
  2583. struct myri10ge_cmd cmd;
  2584. struct netdev_hw_addr *ha;
  2585. __be32 data[2] = { 0, 0 };
  2586. int err;
  2587. /* can be called from atomic contexts,
  2588. * pass 1 to force atomicity in myri10ge_send_cmd() */
  2589. myri10ge_change_promisc(mgp, dev->flags & IFF_PROMISC, 1);
  2590. /* This firmware is known to not support multicast */
  2591. if (!mgp->fw_multicast_support)
  2592. return;
  2593. /* Disable multicast filtering */
  2594. err = myri10ge_send_cmd(mgp, MXGEFW_ENABLE_ALLMULTI, &cmd, 1);
  2595. if (err != 0) {
  2596. netdev_err(dev, "Failed MXGEFW_ENABLE_ALLMULTI, error status: %d\n",
  2597. err);
  2598. goto abort;
  2599. }
  2600. if ((dev->flags & IFF_ALLMULTI) || mgp->adopted_rx_filter_bug) {
  2601. /* request to disable multicast filtering, so quit here */
  2602. return;
  2603. }
  2604. /* Flush the filters */
  2605. err = myri10ge_send_cmd(mgp, MXGEFW_LEAVE_ALL_MULTICAST_GROUPS,
  2606. &cmd, 1);
  2607. if (err != 0) {
  2608. netdev_err(dev, "Failed MXGEFW_LEAVE_ALL_MULTICAST_GROUPS, error status: %d\n",
  2609. err);
  2610. goto abort;
  2611. }
  2612. /* Walk the multicast list, and add each address */
  2613. netdev_for_each_mc_addr(ha, dev) {
  2614. memcpy(data, &ha->addr, ETH_ALEN);
  2615. cmd.data0 = ntohl(data[0]);
  2616. cmd.data1 = ntohl(data[1]);
  2617. err = myri10ge_send_cmd(mgp, MXGEFW_JOIN_MULTICAST_GROUP,
  2618. &cmd, 1);
  2619. if (err != 0) {
  2620. netdev_err(dev, "Failed MXGEFW_JOIN_MULTICAST_GROUP, error status:%d %pM\n",
  2621. err, ha->addr);
  2622. goto abort;
  2623. }
  2624. }
  2625. /* Enable multicast filtering */
  2626. err = myri10ge_send_cmd(mgp, MXGEFW_DISABLE_ALLMULTI, &cmd, 1);
  2627. if (err != 0) {
  2628. netdev_err(dev, "Failed MXGEFW_DISABLE_ALLMULTI, error status: %d\n",
  2629. err);
  2630. goto abort;
  2631. }
  2632. return;
  2633. abort:
  2634. return;
  2635. }
  2636. static int myri10ge_set_mac_address(struct net_device *dev, void *addr)
  2637. {
  2638. struct sockaddr *sa = addr;
  2639. struct myri10ge_priv *mgp = netdev_priv(dev);
  2640. int status;
  2641. if (!is_valid_ether_addr(sa->sa_data))
  2642. return -EADDRNOTAVAIL;
  2643. status = myri10ge_update_mac_address(mgp, sa->sa_data);
  2644. if (status != 0) {
  2645. netdev_err(dev, "changing mac address failed with %d\n",
  2646. status);
  2647. return status;
  2648. }
  2649. /* change the dev structure */
  2650. memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
  2651. return 0;
  2652. }
  2653. static int myri10ge_change_mtu(struct net_device *dev, int new_mtu)
  2654. {
  2655. struct myri10ge_priv *mgp = netdev_priv(dev);
  2656. int error = 0;
  2657. netdev_info(dev, "changing mtu from %d to %d\n", dev->mtu, new_mtu);
  2658. if (mgp->running) {
  2659. /* if we change the mtu on an active device, we must
  2660. * reset the device so the firmware sees the change */
  2661. myri10ge_close(dev);
  2662. dev->mtu = new_mtu;
  2663. myri10ge_open(dev);
  2664. } else
  2665. dev->mtu = new_mtu;
  2666. return error;
  2667. }
  2668. /*
  2669. * Enable ECRC to align PCI-E Completion packets on an 8-byte boundary.
  2670. * Only do it if the bridge is a root port since we don't want to disturb
  2671. * any other device, except if forced with myri10ge_ecrc_enable > 1.
  2672. */
  2673. static void myri10ge_enable_ecrc(struct myri10ge_priv *mgp)
  2674. {
  2675. struct pci_dev *bridge = mgp->pdev->bus->self;
  2676. struct device *dev = &mgp->pdev->dev;
  2677. int cap;
  2678. unsigned err_cap;
  2679. int ret;
  2680. if (!myri10ge_ecrc_enable || !bridge)
  2681. return;
  2682. /* check that the bridge is a root port */
  2683. if (pci_pcie_type(bridge) != PCI_EXP_TYPE_ROOT_PORT) {
  2684. if (myri10ge_ecrc_enable > 1) {
  2685. struct pci_dev *prev_bridge, *old_bridge = bridge;
  2686. /* Walk the hierarchy up to the root port
  2687. * where ECRC has to be enabled */
  2688. do {
  2689. prev_bridge = bridge;
  2690. bridge = bridge->bus->self;
  2691. if (!bridge || prev_bridge == bridge) {
  2692. dev_err(dev,
  2693. "Failed to find root port"
  2694. " to force ECRC\n");
  2695. return;
  2696. }
  2697. } while (pci_pcie_type(bridge) !=
  2698. PCI_EXP_TYPE_ROOT_PORT);
  2699. dev_info(dev,
  2700. "Forcing ECRC on non-root port %s"
  2701. " (enabling on root port %s)\n",
  2702. pci_name(old_bridge), pci_name(bridge));
  2703. } else {
  2704. dev_err(dev,
  2705. "Not enabling ECRC on non-root port %s\n",
  2706. pci_name(bridge));
  2707. return;
  2708. }
  2709. }
  2710. cap = pci_find_ext_capability(bridge, PCI_EXT_CAP_ID_ERR);
  2711. if (!cap)
  2712. return;
  2713. ret = pci_read_config_dword(bridge, cap + PCI_ERR_CAP, &err_cap);
  2714. if (ret) {
  2715. dev_err(dev, "failed reading ext-conf-space of %s\n",
  2716. pci_name(bridge));
  2717. dev_err(dev, "\t pci=nommconf in use? "
  2718. "or buggy/incomplete/absent ACPI MCFG attr?\n");
  2719. return;
  2720. }
  2721. if (!(err_cap & PCI_ERR_CAP_ECRC_GENC))
  2722. return;
  2723. err_cap |= PCI_ERR_CAP_ECRC_GENE;
  2724. pci_write_config_dword(bridge, cap + PCI_ERR_CAP, err_cap);
  2725. dev_info(dev, "Enabled ECRC on upstream bridge %s\n", pci_name(bridge));
  2726. }
  2727. /*
  2728. * The Lanai Z8E PCI-E interface achieves higher Read-DMA throughput
  2729. * when the PCI-E Completion packets are aligned on an 8-byte
  2730. * boundary. Some PCI-E chip sets always align Completion packets; on
  2731. * the ones that do not, the alignment can be enforced by enabling
  2732. * ECRC generation (if supported).
  2733. *
  2734. * When PCI-E Completion packets are not aligned, it is actually more
  2735. * efficient to limit Read-DMA transactions to 2KB, rather than 4KB.
  2736. *
  2737. * If the driver can neither enable ECRC nor verify that it has
  2738. * already been enabled, then it must use a firmware image which works
  2739. * around unaligned completion packets (myri10ge_rss_ethp_z8e.dat), and it
  2740. * should also ensure that it never gives the device a Read-DMA which is
  2741. * larger than 2KB by setting the tx_boundary to 2KB. If ECRC is
  2742. * enabled, then the driver should use the aligned (myri10ge_rss_eth_z8e.dat)
  2743. * firmware image, and set tx_boundary to 4KB.
  2744. */
  2745. static void myri10ge_firmware_probe(struct myri10ge_priv *mgp)
  2746. {
  2747. struct pci_dev *pdev = mgp->pdev;
  2748. struct device *dev = &pdev->dev;
  2749. int status;
  2750. mgp->tx_boundary = 4096;
  2751. /*
  2752. * Verify the max read request size was set to 4KB
  2753. * before trying the test with 4KB.
  2754. */
  2755. status = pcie_get_readrq(pdev);
  2756. if (status < 0) {
  2757. dev_err(dev, "Couldn't read max read req size: %d\n", status);
  2758. goto abort;
  2759. }
  2760. if (status != 4096) {
  2761. dev_warn(dev, "Max Read Request size != 4096 (%d)\n", status);
  2762. mgp->tx_boundary = 2048;
  2763. }
  2764. /*
  2765. * load the optimized firmware (which assumes aligned PCIe
  2766. * completions) in order to see if it works on this host.
  2767. */
  2768. set_fw_name(mgp, myri10ge_fw_aligned, false);
  2769. status = myri10ge_load_firmware(mgp, 1);
  2770. if (status != 0) {
  2771. goto abort;
  2772. }
  2773. /*
  2774. * Enable ECRC if possible
  2775. */
  2776. myri10ge_enable_ecrc(mgp);
  2777. /*
  2778. * Run a DMA test which watches for unaligned completions and
  2779. * aborts on the first one seen.
  2780. */
  2781. status = myri10ge_dma_test(mgp, MXGEFW_CMD_UNALIGNED_TEST);
  2782. if (status == 0)
  2783. return; /* keep the aligned firmware */
  2784. if (status != -E2BIG)
  2785. dev_warn(dev, "DMA test failed: %d\n", status);
  2786. if (status == -ENOSYS)
  2787. dev_warn(dev, "Falling back to ethp! "
  2788. "Please install up to date fw\n");
  2789. abort:
  2790. /* fall back to using the unaligned firmware */
  2791. mgp->tx_boundary = 2048;
  2792. set_fw_name(mgp, myri10ge_fw_unaligned, false);
  2793. }
  2794. static void myri10ge_select_firmware(struct myri10ge_priv *mgp)
  2795. {
  2796. int overridden = 0;
  2797. if (myri10ge_force_firmware == 0) {
  2798. int link_width;
  2799. u16 lnk;
  2800. pcie_capability_read_word(mgp->pdev, PCI_EXP_LNKSTA, &lnk);
  2801. link_width = (lnk >> 4) & 0x3f;
  2802. /* Check to see if Link is less than 8 or if the
  2803. * upstream bridge is known to provide aligned
  2804. * completions */
  2805. if (link_width < 8) {
  2806. dev_info(&mgp->pdev->dev, "PCIE x%d Link\n",
  2807. link_width);
  2808. mgp->tx_boundary = 4096;
  2809. set_fw_name(mgp, myri10ge_fw_aligned, false);
  2810. } else {
  2811. myri10ge_firmware_probe(mgp);
  2812. }
  2813. } else {
  2814. if (myri10ge_force_firmware == 1) {
  2815. dev_info(&mgp->pdev->dev,
  2816. "Assuming aligned completions (forced)\n");
  2817. mgp->tx_boundary = 4096;
  2818. set_fw_name(mgp, myri10ge_fw_aligned, false);
  2819. } else {
  2820. dev_info(&mgp->pdev->dev,
  2821. "Assuming unaligned completions (forced)\n");
  2822. mgp->tx_boundary = 2048;
  2823. set_fw_name(mgp, myri10ge_fw_unaligned, false);
  2824. }
  2825. }
  2826. kernel_param_lock(THIS_MODULE);
  2827. if (myri10ge_fw_name != NULL) {
  2828. char *fw_name = kstrdup(myri10ge_fw_name, GFP_KERNEL);
  2829. if (fw_name) {
  2830. overridden = 1;
  2831. set_fw_name(mgp, fw_name, true);
  2832. }
  2833. }
  2834. kernel_param_unlock(THIS_MODULE);
  2835. if (mgp->board_number < MYRI10GE_MAX_BOARDS &&
  2836. myri10ge_fw_names[mgp->board_number] != NULL &&
  2837. strlen(myri10ge_fw_names[mgp->board_number])) {
  2838. set_fw_name(mgp, myri10ge_fw_names[mgp->board_number], false);
  2839. overridden = 1;
  2840. }
  2841. if (overridden)
  2842. dev_info(&mgp->pdev->dev, "overriding firmware to %s\n",
  2843. mgp->fw_name);
  2844. }
  2845. static void myri10ge_mask_surprise_down(struct pci_dev *pdev)
  2846. {
  2847. struct pci_dev *bridge = pdev->bus->self;
  2848. int cap;
  2849. u32 mask;
  2850. if (bridge == NULL)
  2851. return;
  2852. cap = pci_find_ext_capability(bridge, PCI_EXT_CAP_ID_ERR);
  2853. if (cap) {
  2854. /* a sram parity error can cause a surprise link
  2855. * down; since we expect and can recover from sram
  2856. * parity errors, mask surprise link down events */
  2857. pci_read_config_dword(bridge, cap + PCI_ERR_UNCOR_MASK, &mask);
  2858. mask |= 0x20;
  2859. pci_write_config_dword(bridge, cap + PCI_ERR_UNCOR_MASK, mask);
  2860. }
  2861. }
  2862. #ifdef CONFIG_PM
  2863. static int myri10ge_suspend(struct pci_dev *pdev, pm_message_t state)
  2864. {
  2865. struct myri10ge_priv *mgp;
  2866. struct net_device *netdev;
  2867. mgp = pci_get_drvdata(pdev);
  2868. if (mgp == NULL)
  2869. return -EINVAL;
  2870. netdev = mgp->dev;
  2871. netif_device_detach(netdev);
  2872. if (netif_running(netdev)) {
  2873. netdev_info(netdev, "closing\n");
  2874. rtnl_lock();
  2875. myri10ge_close(netdev);
  2876. rtnl_unlock();
  2877. }
  2878. myri10ge_dummy_rdma(mgp, 0);
  2879. pci_save_state(pdev);
  2880. pci_disable_device(pdev);
  2881. return pci_set_power_state(pdev, pci_choose_state(pdev, state));
  2882. }
  2883. static int myri10ge_resume(struct pci_dev *pdev)
  2884. {
  2885. struct myri10ge_priv *mgp;
  2886. struct net_device *netdev;
  2887. int status;
  2888. u16 vendor;
  2889. mgp = pci_get_drvdata(pdev);
  2890. if (mgp == NULL)
  2891. return -EINVAL;
  2892. netdev = mgp->dev;
  2893. pci_set_power_state(pdev, PCI_D0); /* zeros conf space as a side effect */
  2894. msleep(5); /* give card time to respond */
  2895. pci_read_config_word(mgp->pdev, PCI_VENDOR_ID, &vendor);
  2896. if (vendor == 0xffff) {
  2897. netdev_err(mgp->dev, "device disappeared!\n");
  2898. return -EIO;
  2899. }
  2900. pci_restore_state(pdev);
  2901. status = pci_enable_device(pdev);
  2902. if (status) {
  2903. dev_err(&pdev->dev, "failed to enable device\n");
  2904. return status;
  2905. }
  2906. pci_set_master(pdev);
  2907. myri10ge_reset(mgp);
  2908. myri10ge_dummy_rdma(mgp, 1);
  2909. /* Save configuration space to be restored if the
  2910. * nic resets due to a parity error */
  2911. pci_save_state(pdev);
  2912. if (netif_running(netdev)) {
  2913. rtnl_lock();
  2914. status = myri10ge_open(netdev);
  2915. rtnl_unlock();
  2916. if (status != 0)
  2917. goto abort_with_enabled;
  2918. }
  2919. netif_device_attach(netdev);
  2920. return 0;
  2921. abort_with_enabled:
  2922. pci_disable_device(pdev);
  2923. return -EIO;
  2924. }
  2925. #endif /* CONFIG_PM */
  2926. static u32 myri10ge_read_reboot(struct myri10ge_priv *mgp)
  2927. {
  2928. struct pci_dev *pdev = mgp->pdev;
  2929. int vs = mgp->vendor_specific_offset;
  2930. u32 reboot;
  2931. /*enter read32 mode */
  2932. pci_write_config_byte(pdev, vs + 0x10, 0x3);
  2933. /*read REBOOT_STATUS (0xfffffff0) */
  2934. pci_write_config_dword(pdev, vs + 0x18, 0xfffffff0);
  2935. pci_read_config_dword(pdev, vs + 0x14, &reboot);
  2936. return reboot;
  2937. }
  2938. static void
  2939. myri10ge_check_slice(struct myri10ge_slice_state *ss, int *reset_needed,
  2940. int *busy_slice_cnt, u32 rx_pause_cnt)
  2941. {
  2942. struct myri10ge_priv *mgp = ss->mgp;
  2943. int slice = ss - mgp->ss;
  2944. if (ss->tx.req != ss->tx.done &&
  2945. ss->tx.done == ss->watchdog_tx_done &&
  2946. ss->watchdog_tx_req != ss->watchdog_tx_done) {
  2947. /* nic seems like it might be stuck.. */
  2948. if (rx_pause_cnt != mgp->watchdog_pause) {
  2949. if (net_ratelimit())
  2950. netdev_warn(mgp->dev, "slice %d: TX paused, "
  2951. "check link partner\n", slice);
  2952. } else {
  2953. netdev_warn(mgp->dev,
  2954. "slice %d: TX stuck %d %d %d %d %d %d\n",
  2955. slice, ss->tx.queue_active, ss->tx.req,
  2956. ss->tx.done, ss->tx.pkt_start,
  2957. ss->tx.pkt_done,
  2958. (int)ntohl(mgp->ss[slice].fw_stats->
  2959. send_done_count));
  2960. *reset_needed = 1;
  2961. ss->stuck = 1;
  2962. }
  2963. }
  2964. if (ss->watchdog_tx_done != ss->tx.done ||
  2965. ss->watchdog_rx_done != ss->rx_done.cnt) {
  2966. *busy_slice_cnt += 1;
  2967. }
  2968. ss->watchdog_tx_done = ss->tx.done;
  2969. ss->watchdog_tx_req = ss->tx.req;
  2970. ss->watchdog_rx_done = ss->rx_done.cnt;
  2971. }
  2972. /*
  2973. * This watchdog is used to check whether the board has suffered
  2974. * from a parity error and needs to be recovered.
  2975. */
  2976. static void myri10ge_watchdog(struct work_struct *work)
  2977. {
  2978. struct myri10ge_priv *mgp =
  2979. container_of(work, struct myri10ge_priv, watchdog_work);
  2980. struct myri10ge_slice_state *ss;
  2981. u32 reboot, rx_pause_cnt;
  2982. int status, rebooted;
  2983. int i;
  2984. int reset_needed = 0;
  2985. int busy_slice_cnt = 0;
  2986. u16 cmd, vendor;
  2987. mgp->watchdog_resets++;
  2988. pci_read_config_word(mgp->pdev, PCI_COMMAND, &cmd);
  2989. rebooted = 0;
  2990. if ((cmd & PCI_COMMAND_MASTER) == 0) {
  2991. /* Bus master DMA disabled? Check to see
  2992. * if the card rebooted due to a parity error
  2993. * For now, just report it */
  2994. reboot = myri10ge_read_reboot(mgp);
  2995. netdev_err(mgp->dev, "NIC rebooted (0x%x),%s resetting\n",
  2996. reboot, myri10ge_reset_recover ? "" : " not");
  2997. if (myri10ge_reset_recover == 0)
  2998. return;
  2999. rtnl_lock();
  3000. mgp->rebooted = 1;
  3001. rebooted = 1;
  3002. myri10ge_close(mgp->dev);
  3003. myri10ge_reset_recover--;
  3004. mgp->rebooted = 0;
  3005. /*
  3006. * A rebooted nic will come back with config space as
  3007. * it was after power was applied to PCIe bus.
  3008. * Attempt to restore config space which was saved
  3009. * when the driver was loaded, or the last time the
  3010. * nic was resumed from power saving mode.
  3011. */
  3012. pci_restore_state(mgp->pdev);
  3013. /* save state again for accounting reasons */
  3014. pci_save_state(mgp->pdev);
  3015. } else {
  3016. /* if we get back -1's from our slot, perhaps somebody
  3017. * powered off our card. Don't try to reset it in
  3018. * this case */
  3019. if (cmd == 0xffff) {
  3020. pci_read_config_word(mgp->pdev, PCI_VENDOR_ID, &vendor);
  3021. if (vendor == 0xffff) {
  3022. netdev_err(mgp->dev, "device disappeared!\n");
  3023. return;
  3024. }
  3025. }
  3026. /* Perhaps it is a software error. See if stuck slice
  3027. * has recovered, reset if not */
  3028. rx_pause_cnt = ntohl(mgp->ss[0].fw_stats->dropped_pause);
  3029. for (i = 0; i < mgp->num_slices; i++) {
  3030. ss = mgp->ss;
  3031. if (ss->stuck) {
  3032. myri10ge_check_slice(ss, &reset_needed,
  3033. &busy_slice_cnt,
  3034. rx_pause_cnt);
  3035. ss->stuck = 0;
  3036. }
  3037. }
  3038. if (!reset_needed) {
  3039. netdev_dbg(mgp->dev, "not resetting\n");
  3040. return;
  3041. }
  3042. netdev_err(mgp->dev, "device timeout, resetting\n");
  3043. }
  3044. if (!rebooted) {
  3045. rtnl_lock();
  3046. myri10ge_close(mgp->dev);
  3047. }
  3048. status = myri10ge_load_firmware(mgp, 1);
  3049. if (status != 0)
  3050. netdev_err(mgp->dev, "failed to load firmware\n");
  3051. else
  3052. myri10ge_open(mgp->dev);
  3053. rtnl_unlock();
  3054. }
  3055. /*
  3056. * We use our own timer routine rather than relying upon
  3057. * netdev->tx_timeout because we have a very large hardware transmit
  3058. * queue. Due to the large queue, the netdev->tx_timeout function
  3059. * cannot detect a NIC with a parity error in a timely fashion if the
  3060. * NIC is lightly loaded.
  3061. */
  3062. static void myri10ge_watchdog_timer(struct timer_list *t)
  3063. {
  3064. struct myri10ge_priv *mgp;
  3065. struct myri10ge_slice_state *ss;
  3066. int i, reset_needed, busy_slice_cnt;
  3067. u32 rx_pause_cnt;
  3068. u16 cmd;
  3069. mgp = from_timer(mgp, t, watchdog_timer);
  3070. rx_pause_cnt = ntohl(mgp->ss[0].fw_stats->dropped_pause);
  3071. busy_slice_cnt = 0;
  3072. for (i = 0, reset_needed = 0;
  3073. i < mgp->num_slices && reset_needed == 0; ++i) {
  3074. ss = &mgp->ss[i];
  3075. if (ss->rx_small.watchdog_needed) {
  3076. myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
  3077. mgp->small_bytes + MXGEFW_PAD,
  3078. 1);
  3079. if (ss->rx_small.fill_cnt - ss->rx_small.cnt >=
  3080. myri10ge_fill_thresh)
  3081. ss->rx_small.watchdog_needed = 0;
  3082. }
  3083. if (ss->rx_big.watchdog_needed) {
  3084. myri10ge_alloc_rx_pages(mgp, &ss->rx_big,
  3085. mgp->big_bytes, 1);
  3086. if (ss->rx_big.fill_cnt - ss->rx_big.cnt >=
  3087. myri10ge_fill_thresh)
  3088. ss->rx_big.watchdog_needed = 0;
  3089. }
  3090. myri10ge_check_slice(ss, &reset_needed, &busy_slice_cnt,
  3091. rx_pause_cnt);
  3092. }
  3093. /* if we've sent or received no traffic, poll the NIC to
  3094. * ensure it is still there. Otherwise, we risk not noticing
  3095. * an error in a timely fashion */
  3096. if (busy_slice_cnt == 0) {
  3097. pci_read_config_word(mgp->pdev, PCI_COMMAND, &cmd);
  3098. if ((cmd & PCI_COMMAND_MASTER) == 0) {
  3099. reset_needed = 1;
  3100. }
  3101. }
  3102. mgp->watchdog_pause = rx_pause_cnt;
  3103. if (reset_needed) {
  3104. schedule_work(&mgp->watchdog_work);
  3105. } else {
  3106. /* rearm timer */
  3107. mod_timer(&mgp->watchdog_timer,
  3108. jiffies + myri10ge_watchdog_timeout * HZ);
  3109. }
  3110. }
  3111. static void myri10ge_free_slices(struct myri10ge_priv *mgp)
  3112. {
  3113. struct myri10ge_slice_state *ss;
  3114. struct pci_dev *pdev = mgp->pdev;
  3115. size_t bytes;
  3116. int i;
  3117. if (mgp->ss == NULL)
  3118. return;
  3119. for (i = 0; i < mgp->num_slices; i++) {
  3120. ss = &mgp->ss[i];
  3121. if (ss->rx_done.entry != NULL) {
  3122. bytes = mgp->max_intr_slots *
  3123. sizeof(*ss->rx_done.entry);
  3124. dma_free_coherent(&pdev->dev, bytes,
  3125. ss->rx_done.entry, ss->rx_done.bus);
  3126. ss->rx_done.entry = NULL;
  3127. }
  3128. if (ss->fw_stats != NULL) {
  3129. bytes = sizeof(*ss->fw_stats);
  3130. dma_free_coherent(&pdev->dev, bytes,
  3131. ss->fw_stats, ss->fw_stats_bus);
  3132. ss->fw_stats = NULL;
  3133. }
  3134. napi_hash_del(&ss->napi);
  3135. netif_napi_del(&ss->napi);
  3136. }
  3137. /* Wait till napi structs are no longer used, and then free ss. */
  3138. synchronize_rcu();
  3139. kfree(mgp->ss);
  3140. mgp->ss = NULL;
  3141. }
  3142. static int myri10ge_alloc_slices(struct myri10ge_priv *mgp)
  3143. {
  3144. struct myri10ge_slice_state *ss;
  3145. struct pci_dev *pdev = mgp->pdev;
  3146. size_t bytes;
  3147. int i;
  3148. bytes = sizeof(*mgp->ss) * mgp->num_slices;
  3149. mgp->ss = kzalloc(bytes, GFP_KERNEL);
  3150. if (mgp->ss == NULL) {
  3151. return -ENOMEM;
  3152. }
  3153. for (i = 0; i < mgp->num_slices; i++) {
  3154. ss = &mgp->ss[i];
  3155. bytes = mgp->max_intr_slots * sizeof(*ss->rx_done.entry);
  3156. ss->rx_done.entry = dma_zalloc_coherent(&pdev->dev, bytes,
  3157. &ss->rx_done.bus,
  3158. GFP_KERNEL);
  3159. if (ss->rx_done.entry == NULL)
  3160. goto abort;
  3161. bytes = sizeof(*ss->fw_stats);
  3162. ss->fw_stats = dma_alloc_coherent(&pdev->dev, bytes,
  3163. &ss->fw_stats_bus,
  3164. GFP_KERNEL);
  3165. if (ss->fw_stats == NULL)
  3166. goto abort;
  3167. ss->mgp = mgp;
  3168. ss->dev = mgp->dev;
  3169. netif_napi_add(ss->dev, &ss->napi, myri10ge_poll,
  3170. myri10ge_napi_weight);
  3171. }
  3172. return 0;
  3173. abort:
  3174. myri10ge_free_slices(mgp);
  3175. return -ENOMEM;
  3176. }
  3177. /*
  3178. * This function determines the number of slices supported.
  3179. * The number slices is the minimum of the number of CPUS,
  3180. * the number of MSI-X irqs supported, the number of slices
  3181. * supported by the firmware
  3182. */
  3183. static void myri10ge_probe_slices(struct myri10ge_priv *mgp)
  3184. {
  3185. struct myri10ge_cmd cmd;
  3186. struct pci_dev *pdev = mgp->pdev;
  3187. char *old_fw;
  3188. bool old_allocated;
  3189. int i, status, ncpus;
  3190. mgp->num_slices = 1;
  3191. ncpus = netif_get_num_default_rss_queues();
  3192. if (myri10ge_max_slices == 1 || !pdev->msix_cap ||
  3193. (myri10ge_max_slices == -1 && ncpus < 2))
  3194. return;
  3195. /* try to load the slice aware rss firmware */
  3196. old_fw = mgp->fw_name;
  3197. old_allocated = mgp->fw_name_allocated;
  3198. /* don't free old_fw if we override it. */
  3199. mgp->fw_name_allocated = false;
  3200. if (myri10ge_fw_name != NULL) {
  3201. dev_info(&mgp->pdev->dev, "overriding rss firmware to %s\n",
  3202. myri10ge_fw_name);
  3203. set_fw_name(mgp, myri10ge_fw_name, false);
  3204. } else if (old_fw == myri10ge_fw_aligned)
  3205. set_fw_name(mgp, myri10ge_fw_rss_aligned, false);
  3206. else
  3207. set_fw_name(mgp, myri10ge_fw_rss_unaligned, false);
  3208. status = myri10ge_load_firmware(mgp, 0);
  3209. if (status != 0) {
  3210. dev_info(&pdev->dev, "Rss firmware not found\n");
  3211. if (old_allocated)
  3212. kfree(old_fw);
  3213. return;
  3214. }
  3215. /* hit the board with a reset to ensure it is alive */
  3216. memset(&cmd, 0, sizeof(cmd));
  3217. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_RESET, &cmd, 0);
  3218. if (status != 0) {
  3219. dev_err(&mgp->pdev->dev, "failed reset\n");
  3220. goto abort_with_fw;
  3221. }
  3222. mgp->max_intr_slots = cmd.data0 / sizeof(struct mcp_slot);
  3223. /* tell it the size of the interrupt queues */
  3224. cmd.data0 = mgp->max_intr_slots * sizeof(struct mcp_slot);
  3225. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_SIZE, &cmd, 0);
  3226. if (status != 0) {
  3227. dev_err(&mgp->pdev->dev, "failed MXGEFW_CMD_SET_INTRQ_SIZE\n");
  3228. goto abort_with_fw;
  3229. }
  3230. /* ask the maximum number of slices it supports */
  3231. status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_RSS_QUEUES, &cmd, 0);
  3232. if (status != 0)
  3233. goto abort_with_fw;
  3234. else
  3235. mgp->num_slices = cmd.data0;
  3236. /* Only allow multiple slices if MSI-X is usable */
  3237. if (!myri10ge_msi) {
  3238. goto abort_with_fw;
  3239. }
  3240. /* if the admin did not specify a limit to how many
  3241. * slices we should use, cap it automatically to the
  3242. * number of CPUs currently online */
  3243. if (myri10ge_max_slices == -1)
  3244. myri10ge_max_slices = ncpus;
  3245. if (mgp->num_slices > myri10ge_max_slices)
  3246. mgp->num_slices = myri10ge_max_slices;
  3247. /* Now try to allocate as many MSI-X vectors as we have
  3248. * slices. We give up on MSI-X if we can only get a single
  3249. * vector. */
  3250. mgp->msix_vectors = kcalloc(mgp->num_slices, sizeof(*mgp->msix_vectors),
  3251. GFP_KERNEL);
  3252. if (mgp->msix_vectors == NULL)
  3253. goto no_msix;
  3254. for (i = 0; i < mgp->num_slices; i++) {
  3255. mgp->msix_vectors[i].entry = i;
  3256. }
  3257. while (mgp->num_slices > 1) {
  3258. mgp->num_slices = rounddown_pow_of_two(mgp->num_slices);
  3259. if (mgp->num_slices == 1)
  3260. goto no_msix;
  3261. status = pci_enable_msix_range(pdev,
  3262. mgp->msix_vectors,
  3263. mgp->num_slices,
  3264. mgp->num_slices);
  3265. if (status < 0)
  3266. goto no_msix;
  3267. pci_disable_msix(pdev);
  3268. if (status == mgp->num_slices) {
  3269. if (old_allocated)
  3270. kfree(old_fw);
  3271. return;
  3272. } else {
  3273. mgp->num_slices = status;
  3274. }
  3275. }
  3276. no_msix:
  3277. if (mgp->msix_vectors != NULL) {
  3278. kfree(mgp->msix_vectors);
  3279. mgp->msix_vectors = NULL;
  3280. }
  3281. abort_with_fw:
  3282. mgp->num_slices = 1;
  3283. set_fw_name(mgp, old_fw, old_allocated);
  3284. myri10ge_load_firmware(mgp, 0);
  3285. }
  3286. static const struct net_device_ops myri10ge_netdev_ops = {
  3287. .ndo_open = myri10ge_open,
  3288. .ndo_stop = myri10ge_close,
  3289. .ndo_start_xmit = myri10ge_xmit,
  3290. .ndo_get_stats64 = myri10ge_get_stats,
  3291. .ndo_validate_addr = eth_validate_addr,
  3292. .ndo_change_mtu = myri10ge_change_mtu,
  3293. .ndo_set_rx_mode = myri10ge_set_multicast_list,
  3294. .ndo_set_mac_address = myri10ge_set_mac_address,
  3295. };
  3296. static int myri10ge_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  3297. {
  3298. struct net_device *netdev;
  3299. struct myri10ge_priv *mgp;
  3300. struct device *dev = &pdev->dev;
  3301. int i;
  3302. int status = -ENXIO;
  3303. int dac_enabled;
  3304. unsigned hdr_offset, ss_offset;
  3305. static int board_number;
  3306. netdev = alloc_etherdev_mq(sizeof(*mgp), MYRI10GE_MAX_SLICES);
  3307. if (netdev == NULL)
  3308. return -ENOMEM;
  3309. SET_NETDEV_DEV(netdev, &pdev->dev);
  3310. mgp = netdev_priv(netdev);
  3311. mgp->dev = netdev;
  3312. mgp->pdev = pdev;
  3313. mgp->pause = myri10ge_flow_control;
  3314. mgp->intr_coal_delay = myri10ge_intr_coal_delay;
  3315. mgp->msg_enable = netif_msg_init(myri10ge_debug, MYRI10GE_MSG_DEFAULT);
  3316. mgp->board_number = board_number;
  3317. init_waitqueue_head(&mgp->down_wq);
  3318. if (pci_enable_device(pdev)) {
  3319. dev_err(&pdev->dev, "pci_enable_device call failed\n");
  3320. status = -ENODEV;
  3321. goto abort_with_netdev;
  3322. }
  3323. /* Find the vendor-specific cap so we can check
  3324. * the reboot register later on */
  3325. mgp->vendor_specific_offset
  3326. = pci_find_capability(pdev, PCI_CAP_ID_VNDR);
  3327. /* Set our max read request to 4KB */
  3328. status = pcie_set_readrq(pdev, 4096);
  3329. if (status != 0) {
  3330. dev_err(&pdev->dev, "Error %d writing PCI_EXP_DEVCTL\n",
  3331. status);
  3332. goto abort_with_enabled;
  3333. }
  3334. myri10ge_mask_surprise_down(pdev);
  3335. pci_set_master(pdev);
  3336. dac_enabled = 1;
  3337. status = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  3338. if (status != 0) {
  3339. dac_enabled = 0;
  3340. dev_err(&pdev->dev,
  3341. "64-bit pci address mask was refused, "
  3342. "trying 32-bit\n");
  3343. status = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  3344. }
  3345. if (status != 0) {
  3346. dev_err(&pdev->dev, "Error %d setting DMA mask\n", status);
  3347. goto abort_with_enabled;
  3348. }
  3349. (void)pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  3350. mgp->cmd = dma_alloc_coherent(&pdev->dev, sizeof(*mgp->cmd),
  3351. &mgp->cmd_bus, GFP_KERNEL);
  3352. if (!mgp->cmd) {
  3353. status = -ENOMEM;
  3354. goto abort_with_enabled;
  3355. }
  3356. mgp->board_span = pci_resource_len(pdev, 0);
  3357. mgp->iomem_base = pci_resource_start(pdev, 0);
  3358. mgp->wc_cookie = arch_phys_wc_add(mgp->iomem_base, mgp->board_span);
  3359. mgp->sram = ioremap_wc(mgp->iomem_base, mgp->board_span);
  3360. if (mgp->sram == NULL) {
  3361. dev_err(&pdev->dev, "ioremap failed for %ld bytes at 0x%lx\n",
  3362. mgp->board_span, mgp->iomem_base);
  3363. status = -ENXIO;
  3364. goto abort_with_mtrr;
  3365. }
  3366. hdr_offset =
  3367. swab32(readl(mgp->sram + MCP_HEADER_PTR_OFFSET)) & 0xffffc;
  3368. ss_offset = hdr_offset + offsetof(struct mcp_gen_header, string_specs);
  3369. mgp->sram_size = swab32(readl(mgp->sram + ss_offset));
  3370. if (mgp->sram_size > mgp->board_span ||
  3371. mgp->sram_size <= MYRI10GE_FW_OFFSET) {
  3372. dev_err(&pdev->dev,
  3373. "invalid sram_size %dB or board span %ldB\n",
  3374. mgp->sram_size, mgp->board_span);
  3375. goto abort_with_ioremap;
  3376. }
  3377. memcpy_fromio(mgp->eeprom_strings,
  3378. mgp->sram + mgp->sram_size, MYRI10GE_EEPROM_STRINGS_SIZE);
  3379. memset(mgp->eeprom_strings + MYRI10GE_EEPROM_STRINGS_SIZE - 2, 0, 2);
  3380. status = myri10ge_read_mac_addr(mgp);
  3381. if (status)
  3382. goto abort_with_ioremap;
  3383. for (i = 0; i < ETH_ALEN; i++)
  3384. netdev->dev_addr[i] = mgp->mac_addr[i];
  3385. myri10ge_select_firmware(mgp);
  3386. status = myri10ge_load_firmware(mgp, 1);
  3387. if (status != 0) {
  3388. dev_err(&pdev->dev, "failed to load firmware\n");
  3389. goto abort_with_ioremap;
  3390. }
  3391. myri10ge_probe_slices(mgp);
  3392. status = myri10ge_alloc_slices(mgp);
  3393. if (status != 0) {
  3394. dev_err(&pdev->dev, "failed to alloc slice state\n");
  3395. goto abort_with_firmware;
  3396. }
  3397. netif_set_real_num_tx_queues(netdev, mgp->num_slices);
  3398. netif_set_real_num_rx_queues(netdev, mgp->num_slices);
  3399. status = myri10ge_reset(mgp);
  3400. if (status != 0) {
  3401. dev_err(&pdev->dev, "failed reset\n");
  3402. goto abort_with_slices;
  3403. }
  3404. #ifdef CONFIG_MYRI10GE_DCA
  3405. myri10ge_setup_dca(mgp);
  3406. #endif
  3407. pci_set_drvdata(pdev, mgp);
  3408. /* MTU range: 68 - 9000 */
  3409. netdev->min_mtu = ETH_MIN_MTU;
  3410. netdev->max_mtu = MYRI10GE_MAX_ETHER_MTU - ETH_HLEN;
  3411. if (myri10ge_initial_mtu > netdev->max_mtu)
  3412. myri10ge_initial_mtu = netdev->max_mtu;
  3413. if (myri10ge_initial_mtu < netdev->min_mtu)
  3414. myri10ge_initial_mtu = netdev->min_mtu;
  3415. netdev->mtu = myri10ge_initial_mtu;
  3416. netdev->netdev_ops = &myri10ge_netdev_ops;
  3417. netdev->hw_features = mgp->features | NETIF_F_RXCSUM;
  3418. /* fake NETIF_F_HW_VLAN_CTAG_RX for good GRO performance */
  3419. netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
  3420. netdev->features = netdev->hw_features;
  3421. if (dac_enabled)
  3422. netdev->features |= NETIF_F_HIGHDMA;
  3423. netdev->vlan_features |= mgp->features;
  3424. if (mgp->fw_ver_tiny < 37)
  3425. netdev->vlan_features &= ~NETIF_F_TSO6;
  3426. if (mgp->fw_ver_tiny < 32)
  3427. netdev->vlan_features &= ~NETIF_F_TSO;
  3428. /* make sure we can get an irq, and that MSI can be
  3429. * setup (if available). */
  3430. status = myri10ge_request_irq(mgp);
  3431. if (status != 0)
  3432. goto abort_with_slices;
  3433. myri10ge_free_irq(mgp);
  3434. /* Save configuration space to be restored if the
  3435. * nic resets due to a parity error */
  3436. pci_save_state(pdev);
  3437. /* Setup the watchdog timer */
  3438. timer_setup(&mgp->watchdog_timer, myri10ge_watchdog_timer, 0);
  3439. netdev->ethtool_ops = &myri10ge_ethtool_ops;
  3440. INIT_WORK(&mgp->watchdog_work, myri10ge_watchdog);
  3441. status = register_netdev(netdev);
  3442. if (status != 0) {
  3443. dev_err(&pdev->dev, "register_netdev failed: %d\n", status);
  3444. goto abort_with_state;
  3445. }
  3446. if (mgp->msix_enabled)
  3447. dev_info(dev, "%d MSI-X IRQs, tx bndry %d, fw %s, MTRR %s, WC Enabled\n",
  3448. mgp->num_slices, mgp->tx_boundary, mgp->fw_name,
  3449. (mgp->wc_cookie > 0 ? "Enabled" : "Disabled"));
  3450. else
  3451. dev_info(dev, "%s IRQ %d, tx bndry %d, fw %s, MTRR %s, WC Enabled\n",
  3452. mgp->msi_enabled ? "MSI" : "xPIC",
  3453. pdev->irq, mgp->tx_boundary, mgp->fw_name,
  3454. (mgp->wc_cookie > 0 ? "Enabled" : "Disabled"));
  3455. board_number++;
  3456. return 0;
  3457. abort_with_state:
  3458. pci_restore_state(pdev);
  3459. abort_with_slices:
  3460. myri10ge_free_slices(mgp);
  3461. abort_with_firmware:
  3462. myri10ge_dummy_rdma(mgp, 0);
  3463. abort_with_ioremap:
  3464. if (mgp->mac_addr_string != NULL)
  3465. dev_err(&pdev->dev,
  3466. "myri10ge_probe() failed: MAC=%s, SN=%ld\n",
  3467. mgp->mac_addr_string, mgp->serial_number);
  3468. iounmap(mgp->sram);
  3469. abort_with_mtrr:
  3470. arch_phys_wc_del(mgp->wc_cookie);
  3471. dma_free_coherent(&pdev->dev, sizeof(*mgp->cmd),
  3472. mgp->cmd, mgp->cmd_bus);
  3473. abort_with_enabled:
  3474. pci_disable_device(pdev);
  3475. abort_with_netdev:
  3476. set_fw_name(mgp, NULL, false);
  3477. free_netdev(netdev);
  3478. return status;
  3479. }
  3480. /*
  3481. * myri10ge_remove
  3482. *
  3483. * Does what is necessary to shutdown one Myrinet device. Called
  3484. * once for each Myrinet card by the kernel when a module is
  3485. * unloaded.
  3486. */
  3487. static void myri10ge_remove(struct pci_dev *pdev)
  3488. {
  3489. struct myri10ge_priv *mgp;
  3490. struct net_device *netdev;
  3491. mgp = pci_get_drvdata(pdev);
  3492. if (mgp == NULL)
  3493. return;
  3494. cancel_work_sync(&mgp->watchdog_work);
  3495. netdev = mgp->dev;
  3496. unregister_netdev(netdev);
  3497. #ifdef CONFIG_MYRI10GE_DCA
  3498. myri10ge_teardown_dca(mgp);
  3499. #endif
  3500. myri10ge_dummy_rdma(mgp, 0);
  3501. /* avoid a memory leak */
  3502. pci_restore_state(pdev);
  3503. iounmap(mgp->sram);
  3504. arch_phys_wc_del(mgp->wc_cookie);
  3505. myri10ge_free_slices(mgp);
  3506. kfree(mgp->msix_vectors);
  3507. dma_free_coherent(&pdev->dev, sizeof(*mgp->cmd),
  3508. mgp->cmd, mgp->cmd_bus);
  3509. set_fw_name(mgp, NULL, false);
  3510. free_netdev(netdev);
  3511. pci_disable_device(pdev);
  3512. }
  3513. #define PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E 0x0008
  3514. #define PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E_9 0x0009
  3515. static const struct pci_device_id myri10ge_pci_tbl[] = {
  3516. {PCI_DEVICE(PCI_VENDOR_ID_MYRICOM, PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E)},
  3517. {PCI_DEVICE
  3518. (PCI_VENDOR_ID_MYRICOM, PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E_9)},
  3519. {0},
  3520. };
  3521. MODULE_DEVICE_TABLE(pci, myri10ge_pci_tbl);
  3522. static struct pci_driver myri10ge_driver = {
  3523. .name = "myri10ge",
  3524. .probe = myri10ge_probe,
  3525. .remove = myri10ge_remove,
  3526. .id_table = myri10ge_pci_tbl,
  3527. #ifdef CONFIG_PM
  3528. .suspend = myri10ge_suspend,
  3529. .resume = myri10ge_resume,
  3530. #endif
  3531. };
  3532. #ifdef CONFIG_MYRI10GE_DCA
  3533. static int
  3534. myri10ge_notify_dca(struct notifier_block *nb, unsigned long event, void *p)
  3535. {
  3536. int err = driver_for_each_device(&myri10ge_driver.driver,
  3537. NULL, &event,
  3538. myri10ge_notify_dca_device);
  3539. if (err)
  3540. return NOTIFY_BAD;
  3541. return NOTIFY_DONE;
  3542. }
  3543. static struct notifier_block myri10ge_dca_notifier = {
  3544. .notifier_call = myri10ge_notify_dca,
  3545. .next = NULL,
  3546. .priority = 0,
  3547. };
  3548. #endif /* CONFIG_MYRI10GE_DCA */
  3549. static __init int myri10ge_init_module(void)
  3550. {
  3551. pr_info("Version %s\n", MYRI10GE_VERSION_STR);
  3552. if (myri10ge_rss_hash > MXGEFW_RSS_HASH_TYPE_MAX) {
  3553. pr_err("Illegal rssh hash type %d, defaulting to source port\n",
  3554. myri10ge_rss_hash);
  3555. myri10ge_rss_hash = MXGEFW_RSS_HASH_TYPE_SRC_PORT;
  3556. }
  3557. #ifdef CONFIG_MYRI10GE_DCA
  3558. dca_register_notify(&myri10ge_dca_notifier);
  3559. #endif
  3560. if (myri10ge_max_slices > MYRI10GE_MAX_SLICES)
  3561. myri10ge_max_slices = MYRI10GE_MAX_SLICES;
  3562. return pci_register_driver(&myri10ge_driver);
  3563. }
  3564. module_init(myri10ge_init_module);
  3565. static __exit void myri10ge_cleanup_module(void)
  3566. {
  3567. #ifdef CONFIG_MYRI10GE_DCA
  3568. dca_unregister_notify(&myri10ge_dca_notifier);
  3569. #endif
  3570. pci_unregister_driver(&myri10ge_driver);
  3571. }
  3572. module_exit(myri10ge_cleanup_module);