myri10ge.c 88 KB

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