myri10ge.c 92 KB

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