myri10ge.c 109 KB

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