myri10ge.c 94 KB

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