myri10ge.c 84 KB

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