myri10ge.c 85 KB

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