myri10ge.c 85 KB

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