myri10ge.c 114 KB

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