myri10ge.c 79 KB

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