myri10ge.c 86 KB

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