myri10ge.c 90 KB

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