myri10ge.c 92 KB

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