myri10ge.c 93 KB

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