myri10ge.c 79 KB

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