cxgb4_main.c 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803
  1. /*
  2. * This file is part of the Chelsio T4 Ethernet driver for Linux.
  3. *
  4. * Copyright (c) 2003-2010 Chelsio Communications, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  35. #include <linux/bitmap.h>
  36. #include <linux/crc32.h>
  37. #include <linux/ctype.h>
  38. #include <linux/debugfs.h>
  39. #include <linux/err.h>
  40. #include <linux/etherdevice.h>
  41. #include <linux/firmware.h>
  42. #include <linux/if_vlan.h>
  43. #include <linux/init.h>
  44. #include <linux/log2.h>
  45. #include <linux/mdio.h>
  46. #include <linux/module.h>
  47. #include <linux/moduleparam.h>
  48. #include <linux/mutex.h>
  49. #include <linux/netdevice.h>
  50. #include <linux/pci.h>
  51. #include <linux/aer.h>
  52. #include <linux/rtnetlink.h>
  53. #include <linux/sched.h>
  54. #include <linux/seq_file.h>
  55. #include <linux/sockios.h>
  56. #include <linux/vmalloc.h>
  57. #include <linux/workqueue.h>
  58. #include <net/neighbour.h>
  59. #include <net/netevent.h>
  60. #include <asm/uaccess.h>
  61. #include "cxgb4.h"
  62. #include "t4_regs.h"
  63. #include "t4_msg.h"
  64. #include "t4fw_api.h"
  65. #include "l2t.h"
  66. #define DRV_VERSION "1.0.0-ko"
  67. #define DRV_DESC "Chelsio T4 Network Driver"
  68. /*
  69. * Max interrupt hold-off timer value in us. Queues fall back to this value
  70. * under extreme memory pressure so it's largish to give the system time to
  71. * recover.
  72. */
  73. #define MAX_SGE_TIMERVAL 200U
  74. #ifdef CONFIG_PCI_IOV
  75. /*
  76. * Virtual Function provisioning constants. We need two extra Ingress Queues
  77. * with Interrupt capability to serve as the VF's Firmware Event Queue and
  78. * Forwarded Interrupt Queue (when using MSI mode) -- neither will have Free
  79. * Lists associated with them). For each Ethernet/Control Egress Queue and
  80. * for each Free List, we need an Egress Context.
  81. */
  82. enum {
  83. VFRES_NPORTS = 1, /* # of "ports" per VF */
  84. VFRES_NQSETS = 2, /* # of "Queue Sets" per VF */
  85. VFRES_NVI = VFRES_NPORTS, /* # of Virtual Interfaces */
  86. VFRES_NETHCTRL = VFRES_NQSETS, /* # of EQs used for ETH or CTRL Qs */
  87. VFRES_NIQFLINT = VFRES_NQSETS+2,/* # of ingress Qs/w Free List(s)/intr */
  88. VFRES_NIQ = 0, /* # of non-fl/int ingress queues */
  89. VFRES_NEQ = VFRES_NQSETS*2, /* # of egress queues */
  90. VFRES_TC = 0, /* PCI-E traffic class */
  91. VFRES_NEXACTF = 16, /* # of exact MPS filters */
  92. VFRES_R_CAPS = FW_CMD_CAP_DMAQ|FW_CMD_CAP_VF|FW_CMD_CAP_PORT,
  93. VFRES_WX_CAPS = FW_CMD_CAP_DMAQ|FW_CMD_CAP_VF,
  94. };
  95. /*
  96. * Provide a Port Access Rights Mask for the specified PF/VF. This is very
  97. * static and likely not to be useful in the long run. We really need to
  98. * implement some form of persistent configuration which the firmware
  99. * controls.
  100. */
  101. static unsigned int pfvfres_pmask(struct adapter *adapter,
  102. unsigned int pf, unsigned int vf)
  103. {
  104. unsigned int portn, portvec;
  105. /*
  106. * Give PF's access to all of the ports.
  107. */
  108. if (vf == 0)
  109. return FW_PFVF_CMD_PMASK_MASK;
  110. /*
  111. * For VFs, we'll assign them access to the ports based purely on the
  112. * PF. We assign active ports in order, wrapping around if there are
  113. * fewer active ports than PFs: e.g. active port[pf % nports].
  114. * Unfortunately the adapter's port_info structs haven't been
  115. * initialized yet so we have to compute this.
  116. */
  117. if (adapter->params.nports == 0)
  118. return 0;
  119. portn = pf % adapter->params.nports;
  120. portvec = adapter->params.portvec;
  121. for (;;) {
  122. /*
  123. * Isolate the lowest set bit in the port vector. If we're at
  124. * the port number that we want, return that as the pmask.
  125. * otherwise mask that bit out of the port vector and
  126. * decrement our port number ...
  127. */
  128. unsigned int pmask = portvec ^ (portvec & (portvec-1));
  129. if (portn == 0)
  130. return pmask;
  131. portn--;
  132. portvec &= ~pmask;
  133. }
  134. /*NOTREACHED*/
  135. }
  136. #endif
  137. enum {
  138. MEMWIN0_APERTURE = 65536,
  139. MEMWIN0_BASE = 0x30000,
  140. MEMWIN1_APERTURE = 32768,
  141. MEMWIN1_BASE = 0x28000,
  142. MEMWIN2_APERTURE = 2048,
  143. MEMWIN2_BASE = 0x1b800,
  144. };
  145. enum {
  146. MAX_TXQ_ENTRIES = 16384,
  147. MAX_CTRL_TXQ_ENTRIES = 1024,
  148. MAX_RSPQ_ENTRIES = 16384,
  149. MAX_RX_BUFFERS = 16384,
  150. MIN_TXQ_ENTRIES = 32,
  151. MIN_CTRL_TXQ_ENTRIES = 32,
  152. MIN_RSPQ_ENTRIES = 128,
  153. MIN_FL_ENTRIES = 16
  154. };
  155. #define DFLT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK | \
  156. NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP |\
  157. NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)
  158. #define CH_DEVICE(devid) { PCI_VDEVICE(CHELSIO, devid), 0 }
  159. static DEFINE_PCI_DEVICE_TABLE(cxgb4_pci_tbl) = {
  160. CH_DEVICE(0xa000), /* PE10K */
  161. { 0, }
  162. };
  163. #define FW_FNAME "cxgb4/t4fw.bin"
  164. MODULE_DESCRIPTION(DRV_DESC);
  165. MODULE_AUTHOR("Chelsio Communications");
  166. MODULE_LICENSE("Dual BSD/GPL");
  167. MODULE_VERSION(DRV_VERSION);
  168. MODULE_DEVICE_TABLE(pci, cxgb4_pci_tbl);
  169. MODULE_FIRMWARE(FW_FNAME);
  170. static int dflt_msg_enable = DFLT_MSG_ENABLE;
  171. module_param(dflt_msg_enable, int, 0644);
  172. MODULE_PARM_DESC(dflt_msg_enable, "Chelsio T4 default message enable bitmap");
  173. /*
  174. * The driver uses the best interrupt scheme available on a platform in the
  175. * order MSI-X, MSI, legacy INTx interrupts. This parameter determines which
  176. * of these schemes the driver may consider as follows:
  177. *
  178. * msi = 2: choose from among all three options
  179. * msi = 1: only consider MSI and INTx interrupts
  180. * msi = 0: force INTx interrupts
  181. */
  182. static int msi = 2;
  183. module_param(msi, int, 0644);
  184. MODULE_PARM_DESC(msi, "whether to use INTx (0), MSI (1) or MSI-X (2)");
  185. /*
  186. * Queue interrupt hold-off timer values. Queues default to the first of these
  187. * upon creation.
  188. */
  189. static unsigned int intr_holdoff[SGE_NTIMERS - 1] = { 5, 10, 20, 50, 100 };
  190. module_param_array(intr_holdoff, uint, NULL, 0644);
  191. MODULE_PARM_DESC(intr_holdoff, "values for queue interrupt hold-off timers "
  192. "0..4 in microseconds");
  193. static unsigned int intr_cnt[SGE_NCOUNTERS - 1] = { 4, 8, 16 };
  194. module_param_array(intr_cnt, uint, NULL, 0644);
  195. MODULE_PARM_DESC(intr_cnt,
  196. "thresholds 1..3 for queue interrupt packet counters");
  197. static int vf_acls;
  198. #ifdef CONFIG_PCI_IOV
  199. module_param(vf_acls, bool, 0644);
  200. MODULE_PARM_DESC(vf_acls, "if set enable virtualization L2 ACL enforcement");
  201. static unsigned int num_vf[4];
  202. module_param_array(num_vf, uint, NULL, 0644);
  203. MODULE_PARM_DESC(num_vf, "number of VFs for each of PFs 0-3");
  204. #endif
  205. static struct dentry *cxgb4_debugfs_root;
  206. static LIST_HEAD(adapter_list);
  207. static DEFINE_MUTEX(uld_mutex);
  208. static struct cxgb4_uld_info ulds[CXGB4_ULD_MAX];
  209. static const char *uld_str[] = { "RDMA", "iSCSI" };
  210. static void link_report(struct net_device *dev)
  211. {
  212. if (!netif_carrier_ok(dev))
  213. netdev_info(dev, "link down\n");
  214. else {
  215. static const char *fc[] = { "no", "Rx", "Tx", "Tx/Rx" };
  216. const char *s = "10Mbps";
  217. const struct port_info *p = netdev_priv(dev);
  218. switch (p->link_cfg.speed) {
  219. case SPEED_10000:
  220. s = "10Gbps";
  221. break;
  222. case SPEED_1000:
  223. s = "1000Mbps";
  224. break;
  225. case SPEED_100:
  226. s = "100Mbps";
  227. break;
  228. }
  229. netdev_info(dev, "link up, %s, full-duplex, %s PAUSE\n", s,
  230. fc[p->link_cfg.fc]);
  231. }
  232. }
  233. void t4_os_link_changed(struct adapter *adapter, int port_id, int link_stat)
  234. {
  235. struct net_device *dev = adapter->port[port_id];
  236. /* Skip changes from disabled ports. */
  237. if (netif_running(dev) && link_stat != netif_carrier_ok(dev)) {
  238. if (link_stat)
  239. netif_carrier_on(dev);
  240. else
  241. netif_carrier_off(dev);
  242. link_report(dev);
  243. }
  244. }
  245. void t4_os_portmod_changed(const struct adapter *adap, int port_id)
  246. {
  247. static const char *mod_str[] = {
  248. NULL, "LR", "SR", "ER", "passive DA", "active DA", "LRM"
  249. };
  250. const struct net_device *dev = adap->port[port_id];
  251. const struct port_info *pi = netdev_priv(dev);
  252. if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
  253. netdev_info(dev, "port module unplugged\n");
  254. else if (pi->mod_type < ARRAY_SIZE(mod_str))
  255. netdev_info(dev, "%s module inserted\n", mod_str[pi->mod_type]);
  256. }
  257. /*
  258. * Configure the exact and hash address filters to handle a port's multicast
  259. * and secondary unicast MAC addresses.
  260. */
  261. static int set_addr_filters(const struct net_device *dev, bool sleep)
  262. {
  263. u64 mhash = 0;
  264. u64 uhash = 0;
  265. bool free = true;
  266. u16 filt_idx[7];
  267. const u8 *addr[7];
  268. int ret, naddr = 0;
  269. const struct netdev_hw_addr *ha;
  270. int uc_cnt = netdev_uc_count(dev);
  271. int mc_cnt = netdev_mc_count(dev);
  272. const struct port_info *pi = netdev_priv(dev);
  273. /* first do the secondary unicast addresses */
  274. netdev_for_each_uc_addr(ha, dev) {
  275. addr[naddr++] = ha->addr;
  276. if (--uc_cnt == 0 || naddr >= ARRAY_SIZE(addr)) {
  277. ret = t4_alloc_mac_filt(pi->adapter, 0, pi->viid, free,
  278. naddr, addr, filt_idx, &uhash, sleep);
  279. if (ret < 0)
  280. return ret;
  281. free = false;
  282. naddr = 0;
  283. }
  284. }
  285. /* next set up the multicast addresses */
  286. netdev_for_each_mc_addr(ha, dev) {
  287. addr[naddr++] = ha->addr;
  288. if (--mc_cnt == 0 || naddr >= ARRAY_SIZE(addr)) {
  289. ret = t4_alloc_mac_filt(pi->adapter, 0, pi->viid, free,
  290. naddr, addr, filt_idx, &mhash, sleep);
  291. if (ret < 0)
  292. return ret;
  293. free = false;
  294. naddr = 0;
  295. }
  296. }
  297. return t4_set_addr_hash(pi->adapter, 0, pi->viid, uhash != 0,
  298. uhash | mhash, sleep);
  299. }
  300. /*
  301. * Set Rx properties of a port, such as promiscruity, address filters, and MTU.
  302. * If @mtu is -1 it is left unchanged.
  303. */
  304. static int set_rxmode(struct net_device *dev, int mtu, bool sleep_ok)
  305. {
  306. int ret;
  307. struct port_info *pi = netdev_priv(dev);
  308. ret = set_addr_filters(dev, sleep_ok);
  309. if (ret == 0)
  310. ret = t4_set_rxmode(pi->adapter, 0, pi->viid, mtu,
  311. (dev->flags & IFF_PROMISC) ? 1 : 0,
  312. (dev->flags & IFF_ALLMULTI) ? 1 : 0, 1, -1,
  313. sleep_ok);
  314. return ret;
  315. }
  316. /**
  317. * link_start - enable a port
  318. * @dev: the port to enable
  319. *
  320. * Performs the MAC and PHY actions needed to enable a port.
  321. */
  322. static int link_start(struct net_device *dev)
  323. {
  324. int ret;
  325. struct port_info *pi = netdev_priv(dev);
  326. /*
  327. * We do not set address filters and promiscuity here, the stack does
  328. * that step explicitly.
  329. */
  330. ret = t4_set_rxmode(pi->adapter, 0, pi->viid, dev->mtu, -1, -1, -1,
  331. pi->vlan_grp != NULL, true);
  332. if (ret == 0) {
  333. ret = t4_change_mac(pi->adapter, 0, pi->viid,
  334. pi->xact_addr_filt, dev->dev_addr, true,
  335. true);
  336. if (ret >= 0) {
  337. pi->xact_addr_filt = ret;
  338. ret = 0;
  339. }
  340. }
  341. if (ret == 0)
  342. ret = t4_link_start(pi->adapter, 0, pi->tx_chan, &pi->link_cfg);
  343. if (ret == 0)
  344. ret = t4_enable_vi(pi->adapter, 0, pi->viid, true, true);
  345. return ret;
  346. }
  347. /*
  348. * Response queue handler for the FW event queue.
  349. */
  350. static int fwevtq_handler(struct sge_rspq *q, const __be64 *rsp,
  351. const struct pkt_gl *gl)
  352. {
  353. u8 opcode = ((const struct rss_header *)rsp)->opcode;
  354. rsp++; /* skip RSS header */
  355. if (likely(opcode == CPL_SGE_EGR_UPDATE)) {
  356. const struct cpl_sge_egr_update *p = (void *)rsp;
  357. unsigned int qid = EGR_QID(ntohl(p->opcode_qid));
  358. struct sge_txq *txq = q->adap->sge.egr_map[qid];
  359. txq->restarts++;
  360. if ((u8 *)txq < (u8 *)q->adap->sge.ethrxq) {
  361. struct sge_eth_txq *eq;
  362. eq = container_of(txq, struct sge_eth_txq, q);
  363. netif_tx_wake_queue(eq->txq);
  364. } else {
  365. struct sge_ofld_txq *oq;
  366. oq = container_of(txq, struct sge_ofld_txq, q);
  367. tasklet_schedule(&oq->qresume_tsk);
  368. }
  369. } else if (opcode == CPL_FW6_MSG || opcode == CPL_FW4_MSG) {
  370. const struct cpl_fw6_msg *p = (void *)rsp;
  371. if (p->type == 0)
  372. t4_handle_fw_rpl(q->adap, p->data);
  373. } else if (opcode == CPL_L2T_WRITE_RPL) {
  374. const struct cpl_l2t_write_rpl *p = (void *)rsp;
  375. do_l2t_write_rpl(q->adap, p);
  376. } else
  377. dev_err(q->adap->pdev_dev,
  378. "unexpected CPL %#x on FW event queue\n", opcode);
  379. return 0;
  380. }
  381. /**
  382. * uldrx_handler - response queue handler for ULD queues
  383. * @q: the response queue that received the packet
  384. * @rsp: the response queue descriptor holding the offload message
  385. * @gl: the gather list of packet fragments
  386. *
  387. * Deliver an ingress offload packet to a ULD. All processing is done by
  388. * the ULD, we just maintain statistics.
  389. */
  390. static int uldrx_handler(struct sge_rspq *q, const __be64 *rsp,
  391. const struct pkt_gl *gl)
  392. {
  393. struct sge_ofld_rxq *rxq = container_of(q, struct sge_ofld_rxq, rspq);
  394. if (ulds[q->uld].rx_handler(q->adap->uld_handle[q->uld], rsp, gl)) {
  395. rxq->stats.nomem++;
  396. return -1;
  397. }
  398. if (gl == NULL)
  399. rxq->stats.imm++;
  400. else if (gl == CXGB4_MSG_AN)
  401. rxq->stats.an++;
  402. else
  403. rxq->stats.pkts++;
  404. return 0;
  405. }
  406. static void disable_msi(struct adapter *adapter)
  407. {
  408. if (adapter->flags & USING_MSIX) {
  409. pci_disable_msix(adapter->pdev);
  410. adapter->flags &= ~USING_MSIX;
  411. } else if (adapter->flags & USING_MSI) {
  412. pci_disable_msi(adapter->pdev);
  413. adapter->flags &= ~USING_MSI;
  414. }
  415. }
  416. /*
  417. * Interrupt handler for non-data events used with MSI-X.
  418. */
  419. static irqreturn_t t4_nondata_intr(int irq, void *cookie)
  420. {
  421. struct adapter *adap = cookie;
  422. u32 v = t4_read_reg(adap, MYPF_REG(PL_PF_INT_CAUSE));
  423. if (v & PFSW) {
  424. adap->swintr = 1;
  425. t4_write_reg(adap, MYPF_REG(PL_PF_INT_CAUSE), v);
  426. }
  427. t4_slow_intr_handler(adap);
  428. return IRQ_HANDLED;
  429. }
  430. /*
  431. * Name the MSI-X interrupts.
  432. */
  433. static void name_msix_vecs(struct adapter *adap)
  434. {
  435. int i, j, msi_idx = 2, n = sizeof(adap->msix_info[0].desc) - 1;
  436. /* non-data interrupts */
  437. snprintf(adap->msix_info[0].desc, n, "%s", adap->name);
  438. adap->msix_info[0].desc[n] = 0;
  439. /* FW events */
  440. snprintf(adap->msix_info[1].desc, n, "%s-FWeventq", adap->name);
  441. adap->msix_info[1].desc[n] = 0;
  442. /* Ethernet queues */
  443. for_each_port(adap, j) {
  444. struct net_device *d = adap->port[j];
  445. const struct port_info *pi = netdev_priv(d);
  446. for (i = 0; i < pi->nqsets; i++, msi_idx++) {
  447. snprintf(adap->msix_info[msi_idx].desc, n, "%s-Rx%d",
  448. d->name, i);
  449. adap->msix_info[msi_idx].desc[n] = 0;
  450. }
  451. }
  452. /* offload queues */
  453. for_each_ofldrxq(&adap->sge, i) {
  454. snprintf(adap->msix_info[msi_idx].desc, n, "%s-ofld%d",
  455. adap->name, i);
  456. adap->msix_info[msi_idx++].desc[n] = 0;
  457. }
  458. for_each_rdmarxq(&adap->sge, i) {
  459. snprintf(adap->msix_info[msi_idx].desc, n, "%s-rdma%d",
  460. adap->name, i);
  461. adap->msix_info[msi_idx++].desc[n] = 0;
  462. }
  463. }
  464. static int request_msix_queue_irqs(struct adapter *adap)
  465. {
  466. struct sge *s = &adap->sge;
  467. int err, ethqidx, ofldqidx = 0, rdmaqidx = 0, msi = 2;
  468. err = request_irq(adap->msix_info[1].vec, t4_sge_intr_msix, 0,
  469. adap->msix_info[1].desc, &s->fw_evtq);
  470. if (err)
  471. return err;
  472. for_each_ethrxq(s, ethqidx) {
  473. err = request_irq(adap->msix_info[msi].vec, t4_sge_intr_msix, 0,
  474. adap->msix_info[msi].desc,
  475. &s->ethrxq[ethqidx].rspq);
  476. if (err)
  477. goto unwind;
  478. msi++;
  479. }
  480. for_each_ofldrxq(s, ofldqidx) {
  481. err = request_irq(adap->msix_info[msi].vec, t4_sge_intr_msix, 0,
  482. adap->msix_info[msi].desc,
  483. &s->ofldrxq[ofldqidx].rspq);
  484. if (err)
  485. goto unwind;
  486. msi++;
  487. }
  488. for_each_rdmarxq(s, rdmaqidx) {
  489. err = request_irq(adap->msix_info[msi].vec, t4_sge_intr_msix, 0,
  490. adap->msix_info[msi].desc,
  491. &s->rdmarxq[rdmaqidx].rspq);
  492. if (err)
  493. goto unwind;
  494. msi++;
  495. }
  496. return 0;
  497. unwind:
  498. while (--rdmaqidx >= 0)
  499. free_irq(adap->msix_info[--msi].vec,
  500. &s->rdmarxq[rdmaqidx].rspq);
  501. while (--ofldqidx >= 0)
  502. free_irq(adap->msix_info[--msi].vec,
  503. &s->ofldrxq[ofldqidx].rspq);
  504. while (--ethqidx >= 0)
  505. free_irq(adap->msix_info[--msi].vec, &s->ethrxq[ethqidx].rspq);
  506. free_irq(adap->msix_info[1].vec, &s->fw_evtq);
  507. return err;
  508. }
  509. static void free_msix_queue_irqs(struct adapter *adap)
  510. {
  511. int i, msi = 2;
  512. struct sge *s = &adap->sge;
  513. free_irq(adap->msix_info[1].vec, &s->fw_evtq);
  514. for_each_ethrxq(s, i)
  515. free_irq(adap->msix_info[msi++].vec, &s->ethrxq[i].rspq);
  516. for_each_ofldrxq(s, i)
  517. free_irq(adap->msix_info[msi++].vec, &s->ofldrxq[i].rspq);
  518. for_each_rdmarxq(s, i)
  519. free_irq(adap->msix_info[msi++].vec, &s->rdmarxq[i].rspq);
  520. }
  521. /**
  522. * write_rss - write the RSS table for a given port
  523. * @pi: the port
  524. * @queues: array of queue indices for RSS
  525. *
  526. * Sets up the portion of the HW RSS table for the port's VI to distribute
  527. * packets to the Rx queues in @queues.
  528. */
  529. static int write_rss(const struct port_info *pi, const u16 *queues)
  530. {
  531. u16 *rss;
  532. int i, err;
  533. const struct sge_eth_rxq *q = &pi->adapter->sge.ethrxq[pi->first_qset];
  534. rss = kmalloc(pi->rss_size * sizeof(u16), GFP_KERNEL);
  535. if (!rss)
  536. return -ENOMEM;
  537. /* map the queue indices to queue ids */
  538. for (i = 0; i < pi->rss_size; i++, queues++)
  539. rss[i] = q[*queues].rspq.abs_id;
  540. err = t4_config_rss_range(pi->adapter, 0, pi->viid, 0, pi->rss_size,
  541. rss, pi->rss_size);
  542. kfree(rss);
  543. return err;
  544. }
  545. /**
  546. * setup_rss - configure RSS
  547. * @adap: the adapter
  548. *
  549. * Sets up RSS for each port.
  550. */
  551. static int setup_rss(struct adapter *adap)
  552. {
  553. int i, err;
  554. for_each_port(adap, i) {
  555. const struct port_info *pi = adap2pinfo(adap, i);
  556. err = write_rss(pi, pi->rss);
  557. if (err)
  558. return err;
  559. }
  560. return 0;
  561. }
  562. /*
  563. * Wait until all NAPI handlers are descheduled.
  564. */
  565. static void quiesce_rx(struct adapter *adap)
  566. {
  567. int i;
  568. for (i = 0; i < ARRAY_SIZE(adap->sge.ingr_map); i++) {
  569. struct sge_rspq *q = adap->sge.ingr_map[i];
  570. if (q && q->handler)
  571. napi_disable(&q->napi);
  572. }
  573. }
  574. /*
  575. * Enable NAPI scheduling and interrupt generation for all Rx queues.
  576. */
  577. static void enable_rx(struct adapter *adap)
  578. {
  579. int i;
  580. for (i = 0; i < ARRAY_SIZE(adap->sge.ingr_map); i++) {
  581. struct sge_rspq *q = adap->sge.ingr_map[i];
  582. if (!q)
  583. continue;
  584. if (q->handler)
  585. napi_enable(&q->napi);
  586. /* 0-increment GTS to start the timer and enable interrupts */
  587. t4_write_reg(adap, MYPF_REG(SGE_PF_GTS),
  588. SEINTARM(q->intr_params) |
  589. INGRESSQID(q->cntxt_id));
  590. }
  591. }
  592. /**
  593. * setup_sge_queues - configure SGE Tx/Rx/response queues
  594. * @adap: the adapter
  595. *
  596. * Determines how many sets of SGE queues to use and initializes them.
  597. * We support multiple queue sets per port if we have MSI-X, otherwise
  598. * just one queue set per port.
  599. */
  600. static int setup_sge_queues(struct adapter *adap)
  601. {
  602. int err, msi_idx, i, j;
  603. struct sge *s = &adap->sge;
  604. bitmap_zero(s->starving_fl, MAX_EGRQ);
  605. bitmap_zero(s->txq_maperr, MAX_EGRQ);
  606. if (adap->flags & USING_MSIX)
  607. msi_idx = 1; /* vector 0 is for non-queue interrupts */
  608. else {
  609. err = t4_sge_alloc_rxq(adap, &s->intrq, false, adap->port[0], 0,
  610. NULL, NULL);
  611. if (err)
  612. return err;
  613. msi_idx = -((int)s->intrq.abs_id + 1);
  614. }
  615. err = t4_sge_alloc_rxq(adap, &s->fw_evtq, true, adap->port[0],
  616. msi_idx, NULL, fwevtq_handler);
  617. if (err) {
  618. freeout: t4_free_sge_resources(adap);
  619. return err;
  620. }
  621. for_each_port(adap, i) {
  622. struct net_device *dev = adap->port[i];
  623. struct port_info *pi = netdev_priv(dev);
  624. struct sge_eth_rxq *q = &s->ethrxq[pi->first_qset];
  625. struct sge_eth_txq *t = &s->ethtxq[pi->first_qset];
  626. for (j = 0; j < pi->nqsets; j++, q++) {
  627. if (msi_idx > 0)
  628. msi_idx++;
  629. err = t4_sge_alloc_rxq(adap, &q->rspq, false, dev,
  630. msi_idx, &q->fl,
  631. t4_ethrx_handler);
  632. if (err)
  633. goto freeout;
  634. q->rspq.idx = j;
  635. memset(&q->stats, 0, sizeof(q->stats));
  636. }
  637. for (j = 0; j < pi->nqsets; j++, t++) {
  638. err = t4_sge_alloc_eth_txq(adap, t, dev,
  639. netdev_get_tx_queue(dev, j),
  640. s->fw_evtq.cntxt_id);
  641. if (err)
  642. goto freeout;
  643. }
  644. }
  645. j = s->ofldqsets / adap->params.nports; /* ofld queues per channel */
  646. for_each_ofldrxq(s, i) {
  647. struct sge_ofld_rxq *q = &s->ofldrxq[i];
  648. struct net_device *dev = adap->port[i / j];
  649. if (msi_idx > 0)
  650. msi_idx++;
  651. err = t4_sge_alloc_rxq(adap, &q->rspq, false, dev, msi_idx,
  652. &q->fl, uldrx_handler);
  653. if (err)
  654. goto freeout;
  655. memset(&q->stats, 0, sizeof(q->stats));
  656. s->ofld_rxq[i] = q->rspq.abs_id;
  657. err = t4_sge_alloc_ofld_txq(adap, &s->ofldtxq[i], dev,
  658. s->fw_evtq.cntxt_id);
  659. if (err)
  660. goto freeout;
  661. }
  662. for_each_rdmarxq(s, i) {
  663. struct sge_ofld_rxq *q = &s->rdmarxq[i];
  664. if (msi_idx > 0)
  665. msi_idx++;
  666. err = t4_sge_alloc_rxq(adap, &q->rspq, false, adap->port[i],
  667. msi_idx, &q->fl, uldrx_handler);
  668. if (err)
  669. goto freeout;
  670. memset(&q->stats, 0, sizeof(q->stats));
  671. s->rdma_rxq[i] = q->rspq.abs_id;
  672. }
  673. for_each_port(adap, i) {
  674. /*
  675. * Note that ->rdmarxq[i].rspq.cntxt_id below is 0 if we don't
  676. * have RDMA queues, and that's the right value.
  677. */
  678. err = t4_sge_alloc_ctrl_txq(adap, &s->ctrlq[i], adap->port[i],
  679. s->fw_evtq.cntxt_id,
  680. s->rdmarxq[i].rspq.cntxt_id);
  681. if (err)
  682. goto freeout;
  683. }
  684. t4_write_reg(adap, MPS_TRC_RSS_CONTROL,
  685. RSSCONTROL(netdev2pinfo(adap->port[0])->tx_chan) |
  686. QUEUENUMBER(s->ethrxq[0].rspq.abs_id));
  687. return 0;
  688. }
  689. /*
  690. * Returns 0 if new FW was successfully loaded, a positive errno if a load was
  691. * started but failed, and a negative errno if flash load couldn't start.
  692. */
  693. static int upgrade_fw(struct adapter *adap)
  694. {
  695. int ret;
  696. u32 vers;
  697. const struct fw_hdr *hdr;
  698. const struct firmware *fw;
  699. struct device *dev = adap->pdev_dev;
  700. ret = request_firmware(&fw, FW_FNAME, dev);
  701. if (ret < 0) {
  702. dev_err(dev, "unable to load firmware image " FW_FNAME
  703. ", error %d\n", ret);
  704. return ret;
  705. }
  706. hdr = (const struct fw_hdr *)fw->data;
  707. vers = ntohl(hdr->fw_ver);
  708. if (FW_HDR_FW_VER_MAJOR_GET(vers) != FW_VERSION_MAJOR) {
  709. ret = -EINVAL; /* wrong major version, won't do */
  710. goto out;
  711. }
  712. /*
  713. * If the flash FW is unusable or we found something newer, load it.
  714. */
  715. if (FW_HDR_FW_VER_MAJOR_GET(adap->params.fw_vers) != FW_VERSION_MAJOR ||
  716. vers > adap->params.fw_vers) {
  717. ret = -t4_load_fw(adap, fw->data, fw->size);
  718. if (!ret)
  719. dev_info(dev, "firmware upgraded to version %pI4 from "
  720. FW_FNAME "\n", &hdr->fw_ver);
  721. }
  722. out: release_firmware(fw);
  723. return ret;
  724. }
  725. /*
  726. * Allocate a chunk of memory using kmalloc or, if that fails, vmalloc.
  727. * The allocated memory is cleared.
  728. */
  729. void *t4_alloc_mem(size_t size)
  730. {
  731. void *p = kmalloc(size, GFP_KERNEL);
  732. if (!p)
  733. p = vmalloc(size);
  734. if (p)
  735. memset(p, 0, size);
  736. return p;
  737. }
  738. /*
  739. * Free memory allocated through alloc_mem().
  740. */
  741. void t4_free_mem(void *addr)
  742. {
  743. if (is_vmalloc_addr(addr))
  744. vfree(addr);
  745. else
  746. kfree(addr);
  747. }
  748. static inline int is_offload(const struct adapter *adap)
  749. {
  750. return adap->params.offload;
  751. }
  752. /*
  753. * Implementation of ethtool operations.
  754. */
  755. static u32 get_msglevel(struct net_device *dev)
  756. {
  757. return netdev2adap(dev)->msg_enable;
  758. }
  759. static void set_msglevel(struct net_device *dev, u32 val)
  760. {
  761. netdev2adap(dev)->msg_enable = val;
  762. }
  763. static char stats_strings[][ETH_GSTRING_LEN] = {
  764. "TxOctetsOK ",
  765. "TxFramesOK ",
  766. "TxBroadcastFrames ",
  767. "TxMulticastFrames ",
  768. "TxUnicastFrames ",
  769. "TxErrorFrames ",
  770. "TxFrames64 ",
  771. "TxFrames65To127 ",
  772. "TxFrames128To255 ",
  773. "TxFrames256To511 ",
  774. "TxFrames512To1023 ",
  775. "TxFrames1024To1518 ",
  776. "TxFrames1519ToMax ",
  777. "TxFramesDropped ",
  778. "TxPauseFrames ",
  779. "TxPPP0Frames ",
  780. "TxPPP1Frames ",
  781. "TxPPP2Frames ",
  782. "TxPPP3Frames ",
  783. "TxPPP4Frames ",
  784. "TxPPP5Frames ",
  785. "TxPPP6Frames ",
  786. "TxPPP7Frames ",
  787. "RxOctetsOK ",
  788. "RxFramesOK ",
  789. "RxBroadcastFrames ",
  790. "RxMulticastFrames ",
  791. "RxUnicastFrames ",
  792. "RxFramesTooLong ",
  793. "RxJabberErrors ",
  794. "RxFCSErrors ",
  795. "RxLengthErrors ",
  796. "RxSymbolErrors ",
  797. "RxRuntFrames ",
  798. "RxFrames64 ",
  799. "RxFrames65To127 ",
  800. "RxFrames128To255 ",
  801. "RxFrames256To511 ",
  802. "RxFrames512To1023 ",
  803. "RxFrames1024To1518 ",
  804. "RxFrames1519ToMax ",
  805. "RxPauseFrames ",
  806. "RxPPP0Frames ",
  807. "RxPPP1Frames ",
  808. "RxPPP2Frames ",
  809. "RxPPP3Frames ",
  810. "RxPPP4Frames ",
  811. "RxPPP5Frames ",
  812. "RxPPP6Frames ",
  813. "RxPPP7Frames ",
  814. "RxBG0FramesDropped ",
  815. "RxBG1FramesDropped ",
  816. "RxBG2FramesDropped ",
  817. "RxBG3FramesDropped ",
  818. "RxBG0FramesTrunc ",
  819. "RxBG1FramesTrunc ",
  820. "RxBG2FramesTrunc ",
  821. "RxBG3FramesTrunc ",
  822. "TSO ",
  823. "TxCsumOffload ",
  824. "RxCsumGood ",
  825. "VLANextractions ",
  826. "VLANinsertions ",
  827. "GROpackets ",
  828. "GROmerged ",
  829. };
  830. static int get_sset_count(struct net_device *dev, int sset)
  831. {
  832. switch (sset) {
  833. case ETH_SS_STATS:
  834. return ARRAY_SIZE(stats_strings);
  835. default:
  836. return -EOPNOTSUPP;
  837. }
  838. }
  839. #define T4_REGMAP_SIZE (160 * 1024)
  840. static int get_regs_len(struct net_device *dev)
  841. {
  842. return T4_REGMAP_SIZE;
  843. }
  844. static int get_eeprom_len(struct net_device *dev)
  845. {
  846. return EEPROMSIZE;
  847. }
  848. static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  849. {
  850. struct adapter *adapter = netdev2adap(dev);
  851. strcpy(info->driver, KBUILD_MODNAME);
  852. strcpy(info->version, DRV_VERSION);
  853. strcpy(info->bus_info, pci_name(adapter->pdev));
  854. if (!adapter->params.fw_vers)
  855. strcpy(info->fw_version, "N/A");
  856. else
  857. snprintf(info->fw_version, sizeof(info->fw_version),
  858. "%u.%u.%u.%u, TP %u.%u.%u.%u",
  859. FW_HDR_FW_VER_MAJOR_GET(adapter->params.fw_vers),
  860. FW_HDR_FW_VER_MINOR_GET(adapter->params.fw_vers),
  861. FW_HDR_FW_VER_MICRO_GET(adapter->params.fw_vers),
  862. FW_HDR_FW_VER_BUILD_GET(adapter->params.fw_vers),
  863. FW_HDR_FW_VER_MAJOR_GET(adapter->params.tp_vers),
  864. FW_HDR_FW_VER_MINOR_GET(adapter->params.tp_vers),
  865. FW_HDR_FW_VER_MICRO_GET(adapter->params.tp_vers),
  866. FW_HDR_FW_VER_BUILD_GET(adapter->params.tp_vers));
  867. }
  868. static void get_strings(struct net_device *dev, u32 stringset, u8 *data)
  869. {
  870. if (stringset == ETH_SS_STATS)
  871. memcpy(data, stats_strings, sizeof(stats_strings));
  872. }
  873. /*
  874. * port stats maintained per queue of the port. They should be in the same
  875. * order as in stats_strings above.
  876. */
  877. struct queue_port_stats {
  878. u64 tso;
  879. u64 tx_csum;
  880. u64 rx_csum;
  881. u64 vlan_ex;
  882. u64 vlan_ins;
  883. u64 gro_pkts;
  884. u64 gro_merged;
  885. };
  886. static void collect_sge_port_stats(const struct adapter *adap,
  887. const struct port_info *p, struct queue_port_stats *s)
  888. {
  889. int i;
  890. const struct sge_eth_txq *tx = &adap->sge.ethtxq[p->first_qset];
  891. const struct sge_eth_rxq *rx = &adap->sge.ethrxq[p->first_qset];
  892. memset(s, 0, sizeof(*s));
  893. for (i = 0; i < p->nqsets; i++, rx++, tx++) {
  894. s->tso += tx->tso;
  895. s->tx_csum += tx->tx_cso;
  896. s->rx_csum += rx->stats.rx_cso;
  897. s->vlan_ex += rx->stats.vlan_ex;
  898. s->vlan_ins += tx->vlan_ins;
  899. s->gro_pkts += rx->stats.lro_pkts;
  900. s->gro_merged += rx->stats.lro_merged;
  901. }
  902. }
  903. static void get_stats(struct net_device *dev, struct ethtool_stats *stats,
  904. u64 *data)
  905. {
  906. struct port_info *pi = netdev_priv(dev);
  907. struct adapter *adapter = pi->adapter;
  908. t4_get_port_stats(adapter, pi->tx_chan, (struct port_stats *)data);
  909. data += sizeof(struct port_stats) / sizeof(u64);
  910. collect_sge_port_stats(adapter, pi, (struct queue_port_stats *)data);
  911. }
  912. /*
  913. * Return a version number to identify the type of adapter. The scheme is:
  914. * - bits 0..9: chip version
  915. * - bits 10..15: chip revision
  916. * - bits 16..23: register dump version
  917. */
  918. static inline unsigned int mk_adap_vers(const struct adapter *ap)
  919. {
  920. return 4 | (ap->params.rev << 10) | (1 << 16);
  921. }
  922. static void reg_block_dump(struct adapter *ap, void *buf, unsigned int start,
  923. unsigned int end)
  924. {
  925. u32 *p = buf + start;
  926. for ( ; start <= end; start += sizeof(u32))
  927. *p++ = t4_read_reg(ap, start);
  928. }
  929. static void get_regs(struct net_device *dev, struct ethtool_regs *regs,
  930. void *buf)
  931. {
  932. static const unsigned int reg_ranges[] = {
  933. 0x1008, 0x1108,
  934. 0x1180, 0x11b4,
  935. 0x11fc, 0x123c,
  936. 0x1300, 0x173c,
  937. 0x1800, 0x18fc,
  938. 0x3000, 0x30d8,
  939. 0x30e0, 0x5924,
  940. 0x5960, 0x59d4,
  941. 0x5a00, 0x5af8,
  942. 0x6000, 0x6098,
  943. 0x6100, 0x6150,
  944. 0x6200, 0x6208,
  945. 0x6240, 0x6248,
  946. 0x6280, 0x6338,
  947. 0x6370, 0x638c,
  948. 0x6400, 0x643c,
  949. 0x6500, 0x6524,
  950. 0x6a00, 0x6a38,
  951. 0x6a60, 0x6a78,
  952. 0x6b00, 0x6b84,
  953. 0x6bf0, 0x6c84,
  954. 0x6cf0, 0x6d84,
  955. 0x6df0, 0x6e84,
  956. 0x6ef0, 0x6f84,
  957. 0x6ff0, 0x7084,
  958. 0x70f0, 0x7184,
  959. 0x71f0, 0x7284,
  960. 0x72f0, 0x7384,
  961. 0x73f0, 0x7450,
  962. 0x7500, 0x7530,
  963. 0x7600, 0x761c,
  964. 0x7680, 0x76cc,
  965. 0x7700, 0x7798,
  966. 0x77c0, 0x77fc,
  967. 0x7900, 0x79fc,
  968. 0x7b00, 0x7c38,
  969. 0x7d00, 0x7efc,
  970. 0x8dc0, 0x8e1c,
  971. 0x8e30, 0x8e78,
  972. 0x8ea0, 0x8f6c,
  973. 0x8fc0, 0x9074,
  974. 0x90fc, 0x90fc,
  975. 0x9400, 0x9458,
  976. 0x9600, 0x96bc,
  977. 0x9800, 0x9808,
  978. 0x9820, 0x983c,
  979. 0x9850, 0x9864,
  980. 0x9c00, 0x9c6c,
  981. 0x9c80, 0x9cec,
  982. 0x9d00, 0x9d6c,
  983. 0x9d80, 0x9dec,
  984. 0x9e00, 0x9e6c,
  985. 0x9e80, 0x9eec,
  986. 0x9f00, 0x9f6c,
  987. 0x9f80, 0x9fec,
  988. 0xd004, 0xd03c,
  989. 0xdfc0, 0xdfe0,
  990. 0xe000, 0xea7c,
  991. 0xf000, 0x11190,
  992. 0x19040, 0x1906c,
  993. 0x19078, 0x19080,
  994. 0x1908c, 0x19124,
  995. 0x19150, 0x191b0,
  996. 0x191d0, 0x191e8,
  997. 0x19238, 0x1924c,
  998. 0x193f8, 0x19474,
  999. 0x19490, 0x194f8,
  1000. 0x19800, 0x19f30,
  1001. 0x1a000, 0x1a06c,
  1002. 0x1a0b0, 0x1a120,
  1003. 0x1a128, 0x1a138,
  1004. 0x1a190, 0x1a1c4,
  1005. 0x1a1fc, 0x1a1fc,
  1006. 0x1e040, 0x1e04c,
  1007. 0x1e284, 0x1e28c,
  1008. 0x1e2c0, 0x1e2c0,
  1009. 0x1e2e0, 0x1e2e0,
  1010. 0x1e300, 0x1e384,
  1011. 0x1e3c0, 0x1e3c8,
  1012. 0x1e440, 0x1e44c,
  1013. 0x1e684, 0x1e68c,
  1014. 0x1e6c0, 0x1e6c0,
  1015. 0x1e6e0, 0x1e6e0,
  1016. 0x1e700, 0x1e784,
  1017. 0x1e7c0, 0x1e7c8,
  1018. 0x1e840, 0x1e84c,
  1019. 0x1ea84, 0x1ea8c,
  1020. 0x1eac0, 0x1eac0,
  1021. 0x1eae0, 0x1eae0,
  1022. 0x1eb00, 0x1eb84,
  1023. 0x1ebc0, 0x1ebc8,
  1024. 0x1ec40, 0x1ec4c,
  1025. 0x1ee84, 0x1ee8c,
  1026. 0x1eec0, 0x1eec0,
  1027. 0x1eee0, 0x1eee0,
  1028. 0x1ef00, 0x1ef84,
  1029. 0x1efc0, 0x1efc8,
  1030. 0x1f040, 0x1f04c,
  1031. 0x1f284, 0x1f28c,
  1032. 0x1f2c0, 0x1f2c0,
  1033. 0x1f2e0, 0x1f2e0,
  1034. 0x1f300, 0x1f384,
  1035. 0x1f3c0, 0x1f3c8,
  1036. 0x1f440, 0x1f44c,
  1037. 0x1f684, 0x1f68c,
  1038. 0x1f6c0, 0x1f6c0,
  1039. 0x1f6e0, 0x1f6e0,
  1040. 0x1f700, 0x1f784,
  1041. 0x1f7c0, 0x1f7c8,
  1042. 0x1f840, 0x1f84c,
  1043. 0x1fa84, 0x1fa8c,
  1044. 0x1fac0, 0x1fac0,
  1045. 0x1fae0, 0x1fae0,
  1046. 0x1fb00, 0x1fb84,
  1047. 0x1fbc0, 0x1fbc8,
  1048. 0x1fc40, 0x1fc4c,
  1049. 0x1fe84, 0x1fe8c,
  1050. 0x1fec0, 0x1fec0,
  1051. 0x1fee0, 0x1fee0,
  1052. 0x1ff00, 0x1ff84,
  1053. 0x1ffc0, 0x1ffc8,
  1054. 0x20000, 0x2002c,
  1055. 0x20100, 0x2013c,
  1056. 0x20190, 0x201c8,
  1057. 0x20200, 0x20318,
  1058. 0x20400, 0x20528,
  1059. 0x20540, 0x20614,
  1060. 0x21000, 0x21040,
  1061. 0x2104c, 0x21060,
  1062. 0x210c0, 0x210ec,
  1063. 0x21200, 0x21268,
  1064. 0x21270, 0x21284,
  1065. 0x212fc, 0x21388,
  1066. 0x21400, 0x21404,
  1067. 0x21500, 0x21518,
  1068. 0x2152c, 0x2153c,
  1069. 0x21550, 0x21554,
  1070. 0x21600, 0x21600,
  1071. 0x21608, 0x21628,
  1072. 0x21630, 0x2163c,
  1073. 0x21700, 0x2171c,
  1074. 0x21780, 0x2178c,
  1075. 0x21800, 0x21c38,
  1076. 0x21c80, 0x21d7c,
  1077. 0x21e00, 0x21e04,
  1078. 0x22000, 0x2202c,
  1079. 0x22100, 0x2213c,
  1080. 0x22190, 0x221c8,
  1081. 0x22200, 0x22318,
  1082. 0x22400, 0x22528,
  1083. 0x22540, 0x22614,
  1084. 0x23000, 0x23040,
  1085. 0x2304c, 0x23060,
  1086. 0x230c0, 0x230ec,
  1087. 0x23200, 0x23268,
  1088. 0x23270, 0x23284,
  1089. 0x232fc, 0x23388,
  1090. 0x23400, 0x23404,
  1091. 0x23500, 0x23518,
  1092. 0x2352c, 0x2353c,
  1093. 0x23550, 0x23554,
  1094. 0x23600, 0x23600,
  1095. 0x23608, 0x23628,
  1096. 0x23630, 0x2363c,
  1097. 0x23700, 0x2371c,
  1098. 0x23780, 0x2378c,
  1099. 0x23800, 0x23c38,
  1100. 0x23c80, 0x23d7c,
  1101. 0x23e00, 0x23e04,
  1102. 0x24000, 0x2402c,
  1103. 0x24100, 0x2413c,
  1104. 0x24190, 0x241c8,
  1105. 0x24200, 0x24318,
  1106. 0x24400, 0x24528,
  1107. 0x24540, 0x24614,
  1108. 0x25000, 0x25040,
  1109. 0x2504c, 0x25060,
  1110. 0x250c0, 0x250ec,
  1111. 0x25200, 0x25268,
  1112. 0x25270, 0x25284,
  1113. 0x252fc, 0x25388,
  1114. 0x25400, 0x25404,
  1115. 0x25500, 0x25518,
  1116. 0x2552c, 0x2553c,
  1117. 0x25550, 0x25554,
  1118. 0x25600, 0x25600,
  1119. 0x25608, 0x25628,
  1120. 0x25630, 0x2563c,
  1121. 0x25700, 0x2571c,
  1122. 0x25780, 0x2578c,
  1123. 0x25800, 0x25c38,
  1124. 0x25c80, 0x25d7c,
  1125. 0x25e00, 0x25e04,
  1126. 0x26000, 0x2602c,
  1127. 0x26100, 0x2613c,
  1128. 0x26190, 0x261c8,
  1129. 0x26200, 0x26318,
  1130. 0x26400, 0x26528,
  1131. 0x26540, 0x26614,
  1132. 0x27000, 0x27040,
  1133. 0x2704c, 0x27060,
  1134. 0x270c0, 0x270ec,
  1135. 0x27200, 0x27268,
  1136. 0x27270, 0x27284,
  1137. 0x272fc, 0x27388,
  1138. 0x27400, 0x27404,
  1139. 0x27500, 0x27518,
  1140. 0x2752c, 0x2753c,
  1141. 0x27550, 0x27554,
  1142. 0x27600, 0x27600,
  1143. 0x27608, 0x27628,
  1144. 0x27630, 0x2763c,
  1145. 0x27700, 0x2771c,
  1146. 0x27780, 0x2778c,
  1147. 0x27800, 0x27c38,
  1148. 0x27c80, 0x27d7c,
  1149. 0x27e00, 0x27e04
  1150. };
  1151. int i;
  1152. struct adapter *ap = netdev2adap(dev);
  1153. regs->version = mk_adap_vers(ap);
  1154. memset(buf, 0, T4_REGMAP_SIZE);
  1155. for (i = 0; i < ARRAY_SIZE(reg_ranges); i += 2)
  1156. reg_block_dump(ap, buf, reg_ranges[i], reg_ranges[i + 1]);
  1157. }
  1158. static int restart_autoneg(struct net_device *dev)
  1159. {
  1160. struct port_info *p = netdev_priv(dev);
  1161. if (!netif_running(dev))
  1162. return -EAGAIN;
  1163. if (p->link_cfg.autoneg != AUTONEG_ENABLE)
  1164. return -EINVAL;
  1165. t4_restart_aneg(p->adapter, 0, p->tx_chan);
  1166. return 0;
  1167. }
  1168. static int identify_port(struct net_device *dev, u32 data)
  1169. {
  1170. if (data == 0)
  1171. data = 2; /* default to 2 seconds */
  1172. return t4_identify_port(netdev2adap(dev), 0, netdev2pinfo(dev)->viid,
  1173. data * 5);
  1174. }
  1175. static unsigned int from_fw_linkcaps(unsigned int type, unsigned int caps)
  1176. {
  1177. unsigned int v = 0;
  1178. if (type == FW_PORT_TYPE_BT_SGMII || type == FW_PORT_TYPE_BT_XFI ||
  1179. type == FW_PORT_TYPE_BT_XAUI) {
  1180. v |= SUPPORTED_TP;
  1181. if (caps & FW_PORT_CAP_SPEED_100M)
  1182. v |= SUPPORTED_100baseT_Full;
  1183. if (caps & FW_PORT_CAP_SPEED_1G)
  1184. v |= SUPPORTED_1000baseT_Full;
  1185. if (caps & FW_PORT_CAP_SPEED_10G)
  1186. v |= SUPPORTED_10000baseT_Full;
  1187. } else if (type == FW_PORT_TYPE_KX4 || type == FW_PORT_TYPE_KX) {
  1188. v |= SUPPORTED_Backplane;
  1189. if (caps & FW_PORT_CAP_SPEED_1G)
  1190. v |= SUPPORTED_1000baseKX_Full;
  1191. if (caps & FW_PORT_CAP_SPEED_10G)
  1192. v |= SUPPORTED_10000baseKX4_Full;
  1193. } else if (type == FW_PORT_TYPE_KR)
  1194. v |= SUPPORTED_Backplane | SUPPORTED_10000baseKR_Full;
  1195. else if (type == FW_PORT_TYPE_BP_AP)
  1196. v |= SUPPORTED_Backplane | SUPPORTED_10000baseR_FEC;
  1197. else if (type == FW_PORT_TYPE_FIBER_XFI ||
  1198. type == FW_PORT_TYPE_FIBER_XAUI || type == FW_PORT_TYPE_SFP)
  1199. v |= SUPPORTED_FIBRE;
  1200. if (caps & FW_PORT_CAP_ANEG)
  1201. v |= SUPPORTED_Autoneg;
  1202. return v;
  1203. }
  1204. static unsigned int to_fw_linkcaps(unsigned int caps)
  1205. {
  1206. unsigned int v = 0;
  1207. if (caps & ADVERTISED_100baseT_Full)
  1208. v |= FW_PORT_CAP_SPEED_100M;
  1209. if (caps & ADVERTISED_1000baseT_Full)
  1210. v |= FW_PORT_CAP_SPEED_1G;
  1211. if (caps & ADVERTISED_10000baseT_Full)
  1212. v |= FW_PORT_CAP_SPEED_10G;
  1213. return v;
  1214. }
  1215. static int get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1216. {
  1217. const struct port_info *p = netdev_priv(dev);
  1218. if (p->port_type == FW_PORT_TYPE_BT_SGMII ||
  1219. p->port_type == FW_PORT_TYPE_BT_XFI ||
  1220. p->port_type == FW_PORT_TYPE_BT_XAUI)
  1221. cmd->port = PORT_TP;
  1222. else if (p->port_type == FW_PORT_TYPE_FIBER_XFI ||
  1223. p->port_type == FW_PORT_TYPE_FIBER_XAUI)
  1224. cmd->port = PORT_FIBRE;
  1225. else if (p->port_type == FW_PORT_TYPE_SFP) {
  1226. if (p->mod_type == FW_PORT_MOD_TYPE_TWINAX_PASSIVE ||
  1227. p->mod_type == FW_PORT_MOD_TYPE_TWINAX_ACTIVE)
  1228. cmd->port = PORT_DA;
  1229. else
  1230. cmd->port = PORT_FIBRE;
  1231. } else
  1232. cmd->port = PORT_OTHER;
  1233. if (p->mdio_addr >= 0) {
  1234. cmd->phy_address = p->mdio_addr;
  1235. cmd->transceiver = XCVR_EXTERNAL;
  1236. cmd->mdio_support = p->port_type == FW_PORT_TYPE_BT_SGMII ?
  1237. MDIO_SUPPORTS_C22 : MDIO_SUPPORTS_C45;
  1238. } else {
  1239. cmd->phy_address = 0; /* not really, but no better option */
  1240. cmd->transceiver = XCVR_INTERNAL;
  1241. cmd->mdio_support = 0;
  1242. }
  1243. cmd->supported = from_fw_linkcaps(p->port_type, p->link_cfg.supported);
  1244. cmd->advertising = from_fw_linkcaps(p->port_type,
  1245. p->link_cfg.advertising);
  1246. cmd->speed = netif_carrier_ok(dev) ? p->link_cfg.speed : 0;
  1247. cmd->duplex = DUPLEX_FULL;
  1248. cmd->autoneg = p->link_cfg.autoneg;
  1249. cmd->maxtxpkt = 0;
  1250. cmd->maxrxpkt = 0;
  1251. return 0;
  1252. }
  1253. static unsigned int speed_to_caps(int speed)
  1254. {
  1255. if (speed == SPEED_100)
  1256. return FW_PORT_CAP_SPEED_100M;
  1257. if (speed == SPEED_1000)
  1258. return FW_PORT_CAP_SPEED_1G;
  1259. if (speed == SPEED_10000)
  1260. return FW_PORT_CAP_SPEED_10G;
  1261. return 0;
  1262. }
  1263. static int set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1264. {
  1265. unsigned int cap;
  1266. struct port_info *p = netdev_priv(dev);
  1267. struct link_config *lc = &p->link_cfg;
  1268. if (cmd->duplex != DUPLEX_FULL) /* only full-duplex supported */
  1269. return -EINVAL;
  1270. if (!(lc->supported & FW_PORT_CAP_ANEG)) {
  1271. /*
  1272. * PHY offers a single speed. See if that's what's
  1273. * being requested.
  1274. */
  1275. if (cmd->autoneg == AUTONEG_DISABLE &&
  1276. (lc->supported & speed_to_caps(cmd->speed)))
  1277. return 0;
  1278. return -EINVAL;
  1279. }
  1280. if (cmd->autoneg == AUTONEG_DISABLE) {
  1281. cap = speed_to_caps(cmd->speed);
  1282. if (!(lc->supported & cap) || cmd->speed == SPEED_1000 ||
  1283. cmd->speed == SPEED_10000)
  1284. return -EINVAL;
  1285. lc->requested_speed = cap;
  1286. lc->advertising = 0;
  1287. } else {
  1288. cap = to_fw_linkcaps(cmd->advertising);
  1289. if (!(lc->supported & cap))
  1290. return -EINVAL;
  1291. lc->requested_speed = 0;
  1292. lc->advertising = cap | FW_PORT_CAP_ANEG;
  1293. }
  1294. lc->autoneg = cmd->autoneg;
  1295. if (netif_running(dev))
  1296. return t4_link_start(p->adapter, 0, p->tx_chan, lc);
  1297. return 0;
  1298. }
  1299. static void get_pauseparam(struct net_device *dev,
  1300. struct ethtool_pauseparam *epause)
  1301. {
  1302. struct port_info *p = netdev_priv(dev);
  1303. epause->autoneg = (p->link_cfg.requested_fc & PAUSE_AUTONEG) != 0;
  1304. epause->rx_pause = (p->link_cfg.fc & PAUSE_RX) != 0;
  1305. epause->tx_pause = (p->link_cfg.fc & PAUSE_TX) != 0;
  1306. }
  1307. static int set_pauseparam(struct net_device *dev,
  1308. struct ethtool_pauseparam *epause)
  1309. {
  1310. struct port_info *p = netdev_priv(dev);
  1311. struct link_config *lc = &p->link_cfg;
  1312. if (epause->autoneg == AUTONEG_DISABLE)
  1313. lc->requested_fc = 0;
  1314. else if (lc->supported & FW_PORT_CAP_ANEG)
  1315. lc->requested_fc = PAUSE_AUTONEG;
  1316. else
  1317. return -EINVAL;
  1318. if (epause->rx_pause)
  1319. lc->requested_fc |= PAUSE_RX;
  1320. if (epause->tx_pause)
  1321. lc->requested_fc |= PAUSE_TX;
  1322. if (netif_running(dev))
  1323. return t4_link_start(p->adapter, 0, p->tx_chan, lc);
  1324. return 0;
  1325. }
  1326. static u32 get_rx_csum(struct net_device *dev)
  1327. {
  1328. struct port_info *p = netdev_priv(dev);
  1329. return p->rx_offload & RX_CSO;
  1330. }
  1331. static int set_rx_csum(struct net_device *dev, u32 data)
  1332. {
  1333. struct port_info *p = netdev_priv(dev);
  1334. if (data)
  1335. p->rx_offload |= RX_CSO;
  1336. else
  1337. p->rx_offload &= ~RX_CSO;
  1338. return 0;
  1339. }
  1340. static void get_sge_param(struct net_device *dev, struct ethtool_ringparam *e)
  1341. {
  1342. const struct port_info *pi = netdev_priv(dev);
  1343. const struct sge *s = &pi->adapter->sge;
  1344. e->rx_max_pending = MAX_RX_BUFFERS;
  1345. e->rx_mini_max_pending = MAX_RSPQ_ENTRIES;
  1346. e->rx_jumbo_max_pending = 0;
  1347. e->tx_max_pending = MAX_TXQ_ENTRIES;
  1348. e->rx_pending = s->ethrxq[pi->first_qset].fl.size - 8;
  1349. e->rx_mini_pending = s->ethrxq[pi->first_qset].rspq.size;
  1350. e->rx_jumbo_pending = 0;
  1351. e->tx_pending = s->ethtxq[pi->first_qset].q.size;
  1352. }
  1353. static int set_sge_param(struct net_device *dev, struct ethtool_ringparam *e)
  1354. {
  1355. int i;
  1356. const struct port_info *pi = netdev_priv(dev);
  1357. struct adapter *adapter = pi->adapter;
  1358. struct sge *s = &adapter->sge;
  1359. if (e->rx_pending > MAX_RX_BUFFERS || e->rx_jumbo_pending ||
  1360. e->tx_pending > MAX_TXQ_ENTRIES ||
  1361. e->rx_mini_pending > MAX_RSPQ_ENTRIES ||
  1362. e->rx_mini_pending < MIN_RSPQ_ENTRIES ||
  1363. e->rx_pending < MIN_FL_ENTRIES || e->tx_pending < MIN_TXQ_ENTRIES)
  1364. return -EINVAL;
  1365. if (adapter->flags & FULL_INIT_DONE)
  1366. return -EBUSY;
  1367. for (i = 0; i < pi->nqsets; ++i) {
  1368. s->ethtxq[pi->first_qset + i].q.size = e->tx_pending;
  1369. s->ethrxq[pi->first_qset + i].fl.size = e->rx_pending + 8;
  1370. s->ethrxq[pi->first_qset + i].rspq.size = e->rx_mini_pending;
  1371. }
  1372. return 0;
  1373. }
  1374. static int closest_timer(const struct sge *s, int time)
  1375. {
  1376. int i, delta, match = 0, min_delta = INT_MAX;
  1377. for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
  1378. delta = time - s->timer_val[i];
  1379. if (delta < 0)
  1380. delta = -delta;
  1381. if (delta < min_delta) {
  1382. min_delta = delta;
  1383. match = i;
  1384. }
  1385. }
  1386. return match;
  1387. }
  1388. static int closest_thres(const struct sge *s, int thres)
  1389. {
  1390. int i, delta, match = 0, min_delta = INT_MAX;
  1391. for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
  1392. delta = thres - s->counter_val[i];
  1393. if (delta < 0)
  1394. delta = -delta;
  1395. if (delta < min_delta) {
  1396. min_delta = delta;
  1397. match = i;
  1398. }
  1399. }
  1400. return match;
  1401. }
  1402. /*
  1403. * Return a queue's interrupt hold-off time in us. 0 means no timer.
  1404. */
  1405. static unsigned int qtimer_val(const struct adapter *adap,
  1406. const struct sge_rspq *q)
  1407. {
  1408. unsigned int idx = q->intr_params >> 1;
  1409. return idx < SGE_NTIMERS ? adap->sge.timer_val[idx] : 0;
  1410. }
  1411. /**
  1412. * set_rxq_intr_params - set a queue's interrupt holdoff parameters
  1413. * @adap: the adapter
  1414. * @q: the Rx queue
  1415. * @us: the hold-off time in us, or 0 to disable timer
  1416. * @cnt: the hold-off packet count, or 0 to disable counter
  1417. *
  1418. * Sets an Rx queue's interrupt hold-off time and packet count. At least
  1419. * one of the two needs to be enabled for the queue to generate interrupts.
  1420. */
  1421. static int set_rxq_intr_params(struct adapter *adap, struct sge_rspq *q,
  1422. unsigned int us, unsigned int cnt)
  1423. {
  1424. if ((us | cnt) == 0)
  1425. cnt = 1;
  1426. if (cnt) {
  1427. int err;
  1428. u32 v, new_idx;
  1429. new_idx = closest_thres(&adap->sge, cnt);
  1430. if (q->desc && q->pktcnt_idx != new_idx) {
  1431. /* the queue has already been created, update it */
  1432. v = FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) |
  1433. FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
  1434. FW_PARAMS_PARAM_YZ(q->cntxt_id);
  1435. err = t4_set_params(adap, 0, 0, 0, 1, &v, &new_idx);
  1436. if (err)
  1437. return err;
  1438. }
  1439. q->pktcnt_idx = new_idx;
  1440. }
  1441. us = us == 0 ? 6 : closest_timer(&adap->sge, us);
  1442. q->intr_params = QINTR_TIMER_IDX(us) | (cnt > 0 ? QINTR_CNT_EN : 0);
  1443. return 0;
  1444. }
  1445. static int set_coalesce(struct net_device *dev, struct ethtool_coalesce *c)
  1446. {
  1447. const struct port_info *pi = netdev_priv(dev);
  1448. struct adapter *adap = pi->adapter;
  1449. return set_rxq_intr_params(adap, &adap->sge.ethrxq[pi->first_qset].rspq,
  1450. c->rx_coalesce_usecs, c->rx_max_coalesced_frames);
  1451. }
  1452. static int get_coalesce(struct net_device *dev, struct ethtool_coalesce *c)
  1453. {
  1454. const struct port_info *pi = netdev_priv(dev);
  1455. const struct adapter *adap = pi->adapter;
  1456. const struct sge_rspq *rq = &adap->sge.ethrxq[pi->first_qset].rspq;
  1457. c->rx_coalesce_usecs = qtimer_val(adap, rq);
  1458. c->rx_max_coalesced_frames = (rq->intr_params & QINTR_CNT_EN) ?
  1459. adap->sge.counter_val[rq->pktcnt_idx] : 0;
  1460. return 0;
  1461. }
  1462. /*
  1463. * Translate a physical EEPROM address to virtual. The first 1K is accessed
  1464. * through virtual addresses starting at 31K, the rest is accessed through
  1465. * virtual addresses starting at 0. This mapping is correct only for PF0.
  1466. */
  1467. static int eeprom_ptov(unsigned int phys_addr)
  1468. {
  1469. if (phys_addr < 1024)
  1470. return phys_addr + (31 << 10);
  1471. if (phys_addr < EEPROMSIZE)
  1472. return phys_addr - 1024;
  1473. return -EINVAL;
  1474. }
  1475. /*
  1476. * The next two routines implement eeprom read/write from physical addresses.
  1477. * The physical->virtual translation is correct only for PF0.
  1478. */
  1479. static int eeprom_rd_phys(struct adapter *adap, unsigned int phys_addr, u32 *v)
  1480. {
  1481. int vaddr = eeprom_ptov(phys_addr);
  1482. if (vaddr >= 0)
  1483. vaddr = pci_read_vpd(adap->pdev, vaddr, sizeof(u32), v);
  1484. return vaddr < 0 ? vaddr : 0;
  1485. }
  1486. static int eeprom_wr_phys(struct adapter *adap, unsigned int phys_addr, u32 v)
  1487. {
  1488. int vaddr = eeprom_ptov(phys_addr);
  1489. if (vaddr >= 0)
  1490. vaddr = pci_write_vpd(adap->pdev, vaddr, sizeof(u32), &v);
  1491. return vaddr < 0 ? vaddr : 0;
  1492. }
  1493. #define EEPROM_MAGIC 0x38E2F10C
  1494. static int get_eeprom(struct net_device *dev, struct ethtool_eeprom *e,
  1495. u8 *data)
  1496. {
  1497. int i, err = 0;
  1498. struct adapter *adapter = netdev2adap(dev);
  1499. u8 *buf = kmalloc(EEPROMSIZE, GFP_KERNEL);
  1500. if (!buf)
  1501. return -ENOMEM;
  1502. e->magic = EEPROM_MAGIC;
  1503. for (i = e->offset & ~3; !err && i < e->offset + e->len; i += 4)
  1504. err = eeprom_rd_phys(adapter, i, (u32 *)&buf[i]);
  1505. if (!err)
  1506. memcpy(data, buf + e->offset, e->len);
  1507. kfree(buf);
  1508. return err;
  1509. }
  1510. static int set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
  1511. u8 *data)
  1512. {
  1513. u8 *buf;
  1514. int err = 0;
  1515. u32 aligned_offset, aligned_len, *p;
  1516. struct adapter *adapter = netdev2adap(dev);
  1517. if (eeprom->magic != EEPROM_MAGIC)
  1518. return -EINVAL;
  1519. aligned_offset = eeprom->offset & ~3;
  1520. aligned_len = (eeprom->len + (eeprom->offset & 3) + 3) & ~3;
  1521. if (aligned_offset != eeprom->offset || aligned_len != eeprom->len) {
  1522. /*
  1523. * RMW possibly needed for first or last words.
  1524. */
  1525. buf = kmalloc(aligned_len, GFP_KERNEL);
  1526. if (!buf)
  1527. return -ENOMEM;
  1528. err = eeprom_rd_phys(adapter, aligned_offset, (u32 *)buf);
  1529. if (!err && aligned_len > 4)
  1530. err = eeprom_rd_phys(adapter,
  1531. aligned_offset + aligned_len - 4,
  1532. (u32 *)&buf[aligned_len - 4]);
  1533. if (err)
  1534. goto out;
  1535. memcpy(buf + (eeprom->offset & 3), data, eeprom->len);
  1536. } else
  1537. buf = data;
  1538. err = t4_seeprom_wp(adapter, false);
  1539. if (err)
  1540. goto out;
  1541. for (p = (u32 *)buf; !err && aligned_len; aligned_len -= 4, p++) {
  1542. err = eeprom_wr_phys(adapter, aligned_offset, *p);
  1543. aligned_offset += 4;
  1544. }
  1545. if (!err)
  1546. err = t4_seeprom_wp(adapter, true);
  1547. out:
  1548. if (buf != data)
  1549. kfree(buf);
  1550. return err;
  1551. }
  1552. static int set_flash(struct net_device *netdev, struct ethtool_flash *ef)
  1553. {
  1554. int ret;
  1555. const struct firmware *fw;
  1556. struct adapter *adap = netdev2adap(netdev);
  1557. ef->data[sizeof(ef->data) - 1] = '\0';
  1558. ret = request_firmware(&fw, ef->data, adap->pdev_dev);
  1559. if (ret < 0)
  1560. return ret;
  1561. ret = t4_load_fw(adap, fw->data, fw->size);
  1562. release_firmware(fw);
  1563. if (!ret)
  1564. dev_info(adap->pdev_dev, "loaded firmware %s\n", ef->data);
  1565. return ret;
  1566. }
  1567. #define WOL_SUPPORTED (WAKE_BCAST | WAKE_MAGIC)
  1568. #define BCAST_CRC 0xa0ccc1a6
  1569. static void get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1570. {
  1571. wol->supported = WAKE_BCAST | WAKE_MAGIC;
  1572. wol->wolopts = netdev2adap(dev)->wol;
  1573. memset(&wol->sopass, 0, sizeof(wol->sopass));
  1574. }
  1575. static int set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1576. {
  1577. int err = 0;
  1578. struct port_info *pi = netdev_priv(dev);
  1579. if (wol->wolopts & ~WOL_SUPPORTED)
  1580. return -EINVAL;
  1581. t4_wol_magic_enable(pi->adapter, pi->tx_chan,
  1582. (wol->wolopts & WAKE_MAGIC) ? dev->dev_addr : NULL);
  1583. if (wol->wolopts & WAKE_BCAST) {
  1584. err = t4_wol_pat_enable(pi->adapter, pi->tx_chan, 0xfe, ~0ULL,
  1585. ~0ULL, 0, false);
  1586. if (!err)
  1587. err = t4_wol_pat_enable(pi->adapter, pi->tx_chan, 1,
  1588. ~6ULL, ~0ULL, BCAST_CRC, true);
  1589. } else
  1590. t4_wol_pat_enable(pi->adapter, pi->tx_chan, 0, 0, 0, 0, false);
  1591. return err;
  1592. }
  1593. static int set_tso(struct net_device *dev, u32 value)
  1594. {
  1595. if (value)
  1596. dev->features |= NETIF_F_TSO | NETIF_F_TSO6;
  1597. else
  1598. dev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
  1599. return 0;
  1600. }
  1601. static int set_flags(struct net_device *dev, u32 flags)
  1602. {
  1603. return ethtool_op_set_flags(dev, flags, ETH_FLAG_RXHASH);
  1604. }
  1605. static int get_rss_table(struct net_device *dev, struct ethtool_rxfh_indir *p)
  1606. {
  1607. const struct port_info *pi = netdev_priv(dev);
  1608. unsigned int n = min_t(unsigned int, p->size, pi->rss_size);
  1609. p->size = pi->rss_size;
  1610. while (n--)
  1611. p->ring_index[n] = pi->rss[n];
  1612. return 0;
  1613. }
  1614. static int set_rss_table(struct net_device *dev,
  1615. const struct ethtool_rxfh_indir *p)
  1616. {
  1617. unsigned int i;
  1618. struct port_info *pi = netdev_priv(dev);
  1619. if (p->size != pi->rss_size)
  1620. return -EINVAL;
  1621. for (i = 0; i < p->size; i++)
  1622. if (p->ring_index[i] >= pi->nqsets)
  1623. return -EINVAL;
  1624. for (i = 0; i < p->size; i++)
  1625. pi->rss[i] = p->ring_index[i];
  1626. if (pi->adapter->flags & FULL_INIT_DONE)
  1627. return write_rss(pi, pi->rss);
  1628. return 0;
  1629. }
  1630. static int get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
  1631. void *rules)
  1632. {
  1633. const struct port_info *pi = netdev_priv(dev);
  1634. switch (info->cmd) {
  1635. case ETHTOOL_GRXFH: {
  1636. unsigned int v = pi->rss_mode;
  1637. info->data = 0;
  1638. switch (info->flow_type) {
  1639. case TCP_V4_FLOW:
  1640. if (v & FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN)
  1641. info->data = RXH_IP_SRC | RXH_IP_DST |
  1642. RXH_L4_B_0_1 | RXH_L4_B_2_3;
  1643. else if (v & FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN)
  1644. info->data = RXH_IP_SRC | RXH_IP_DST;
  1645. break;
  1646. case UDP_V4_FLOW:
  1647. if ((v & FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN) &&
  1648. (v & FW_RSS_VI_CONFIG_CMD_UDPEN))
  1649. info->data = RXH_IP_SRC | RXH_IP_DST |
  1650. RXH_L4_B_0_1 | RXH_L4_B_2_3;
  1651. else if (v & FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN)
  1652. info->data = RXH_IP_SRC | RXH_IP_DST;
  1653. break;
  1654. case SCTP_V4_FLOW:
  1655. case AH_ESP_V4_FLOW:
  1656. case IPV4_FLOW:
  1657. if (v & FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN)
  1658. info->data = RXH_IP_SRC | RXH_IP_DST;
  1659. break;
  1660. case TCP_V6_FLOW:
  1661. if (v & FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN)
  1662. info->data = RXH_IP_SRC | RXH_IP_DST |
  1663. RXH_L4_B_0_1 | RXH_L4_B_2_3;
  1664. else if (v & FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN)
  1665. info->data = RXH_IP_SRC | RXH_IP_DST;
  1666. break;
  1667. case UDP_V6_FLOW:
  1668. if ((v & FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN) &&
  1669. (v & FW_RSS_VI_CONFIG_CMD_UDPEN))
  1670. info->data = RXH_IP_SRC | RXH_IP_DST |
  1671. RXH_L4_B_0_1 | RXH_L4_B_2_3;
  1672. else if (v & FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN)
  1673. info->data = RXH_IP_SRC | RXH_IP_DST;
  1674. break;
  1675. case SCTP_V6_FLOW:
  1676. case AH_ESP_V6_FLOW:
  1677. case IPV6_FLOW:
  1678. if (v & FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN)
  1679. info->data = RXH_IP_SRC | RXH_IP_DST;
  1680. break;
  1681. }
  1682. return 0;
  1683. }
  1684. case ETHTOOL_GRXRINGS:
  1685. info->data = pi->nqsets;
  1686. return 0;
  1687. }
  1688. return -EOPNOTSUPP;
  1689. }
  1690. static struct ethtool_ops cxgb_ethtool_ops = {
  1691. .get_settings = get_settings,
  1692. .set_settings = set_settings,
  1693. .get_drvinfo = get_drvinfo,
  1694. .get_msglevel = get_msglevel,
  1695. .set_msglevel = set_msglevel,
  1696. .get_ringparam = get_sge_param,
  1697. .set_ringparam = set_sge_param,
  1698. .get_coalesce = get_coalesce,
  1699. .set_coalesce = set_coalesce,
  1700. .get_eeprom_len = get_eeprom_len,
  1701. .get_eeprom = get_eeprom,
  1702. .set_eeprom = set_eeprom,
  1703. .get_pauseparam = get_pauseparam,
  1704. .set_pauseparam = set_pauseparam,
  1705. .get_rx_csum = get_rx_csum,
  1706. .set_rx_csum = set_rx_csum,
  1707. .set_tx_csum = ethtool_op_set_tx_ipv6_csum,
  1708. .set_sg = ethtool_op_set_sg,
  1709. .get_link = ethtool_op_get_link,
  1710. .get_strings = get_strings,
  1711. .phys_id = identify_port,
  1712. .nway_reset = restart_autoneg,
  1713. .get_sset_count = get_sset_count,
  1714. .get_ethtool_stats = get_stats,
  1715. .get_regs_len = get_regs_len,
  1716. .get_regs = get_regs,
  1717. .get_wol = get_wol,
  1718. .set_wol = set_wol,
  1719. .set_tso = set_tso,
  1720. .set_flags = set_flags,
  1721. .get_rxnfc = get_rxnfc,
  1722. .get_rxfh_indir = get_rss_table,
  1723. .set_rxfh_indir = set_rss_table,
  1724. .flash_device = set_flash,
  1725. };
  1726. /*
  1727. * debugfs support
  1728. */
  1729. static int mem_open(struct inode *inode, struct file *file)
  1730. {
  1731. file->private_data = inode->i_private;
  1732. return 0;
  1733. }
  1734. static ssize_t mem_read(struct file *file, char __user *buf, size_t count,
  1735. loff_t *ppos)
  1736. {
  1737. loff_t pos = *ppos;
  1738. loff_t avail = file->f_path.dentry->d_inode->i_size;
  1739. unsigned int mem = (uintptr_t)file->private_data & 3;
  1740. struct adapter *adap = file->private_data - mem;
  1741. if (pos < 0)
  1742. return -EINVAL;
  1743. if (pos >= avail)
  1744. return 0;
  1745. if (count > avail - pos)
  1746. count = avail - pos;
  1747. while (count) {
  1748. size_t len;
  1749. int ret, ofst;
  1750. __be32 data[16];
  1751. if (mem == MEM_MC)
  1752. ret = t4_mc_read(adap, pos, data, NULL);
  1753. else
  1754. ret = t4_edc_read(adap, mem, pos, data, NULL);
  1755. if (ret)
  1756. return ret;
  1757. ofst = pos % sizeof(data);
  1758. len = min(count, sizeof(data) - ofst);
  1759. if (copy_to_user(buf, (u8 *)data + ofst, len))
  1760. return -EFAULT;
  1761. buf += len;
  1762. pos += len;
  1763. count -= len;
  1764. }
  1765. count = pos - *ppos;
  1766. *ppos = pos;
  1767. return count;
  1768. }
  1769. static const struct file_operations mem_debugfs_fops = {
  1770. .owner = THIS_MODULE,
  1771. .open = mem_open,
  1772. .read = mem_read,
  1773. };
  1774. static void __devinit add_debugfs_mem(struct adapter *adap, const char *name,
  1775. unsigned int idx, unsigned int size_mb)
  1776. {
  1777. struct dentry *de;
  1778. de = debugfs_create_file(name, S_IRUSR, adap->debugfs_root,
  1779. (void *)adap + idx, &mem_debugfs_fops);
  1780. if (de && de->d_inode)
  1781. de->d_inode->i_size = size_mb << 20;
  1782. }
  1783. static int __devinit setup_debugfs(struct adapter *adap)
  1784. {
  1785. int i;
  1786. if (IS_ERR_OR_NULL(adap->debugfs_root))
  1787. return -1;
  1788. i = t4_read_reg(adap, MA_TARGET_MEM_ENABLE);
  1789. if (i & EDRAM0_ENABLE)
  1790. add_debugfs_mem(adap, "edc0", MEM_EDC0, 5);
  1791. if (i & EDRAM1_ENABLE)
  1792. add_debugfs_mem(adap, "edc1", MEM_EDC1, 5);
  1793. if (i & EXT_MEM_ENABLE)
  1794. add_debugfs_mem(adap, "mc", MEM_MC,
  1795. EXT_MEM_SIZE_GET(t4_read_reg(adap, MA_EXT_MEMORY_BAR)));
  1796. if (adap->l2t)
  1797. debugfs_create_file("l2t", S_IRUSR, adap->debugfs_root, adap,
  1798. &t4_l2t_fops);
  1799. return 0;
  1800. }
  1801. /*
  1802. * upper-layer driver support
  1803. */
  1804. /*
  1805. * Allocate an active-open TID and set it to the supplied value.
  1806. */
  1807. int cxgb4_alloc_atid(struct tid_info *t, void *data)
  1808. {
  1809. int atid = -1;
  1810. spin_lock_bh(&t->atid_lock);
  1811. if (t->afree) {
  1812. union aopen_entry *p = t->afree;
  1813. atid = p - t->atid_tab;
  1814. t->afree = p->next;
  1815. p->data = data;
  1816. t->atids_in_use++;
  1817. }
  1818. spin_unlock_bh(&t->atid_lock);
  1819. return atid;
  1820. }
  1821. EXPORT_SYMBOL(cxgb4_alloc_atid);
  1822. /*
  1823. * Release an active-open TID.
  1824. */
  1825. void cxgb4_free_atid(struct tid_info *t, unsigned int atid)
  1826. {
  1827. union aopen_entry *p = &t->atid_tab[atid];
  1828. spin_lock_bh(&t->atid_lock);
  1829. p->next = t->afree;
  1830. t->afree = p;
  1831. t->atids_in_use--;
  1832. spin_unlock_bh(&t->atid_lock);
  1833. }
  1834. EXPORT_SYMBOL(cxgb4_free_atid);
  1835. /*
  1836. * Allocate a server TID and set it to the supplied value.
  1837. */
  1838. int cxgb4_alloc_stid(struct tid_info *t, int family, void *data)
  1839. {
  1840. int stid;
  1841. spin_lock_bh(&t->stid_lock);
  1842. if (family == PF_INET) {
  1843. stid = find_first_zero_bit(t->stid_bmap, t->nstids);
  1844. if (stid < t->nstids)
  1845. __set_bit(stid, t->stid_bmap);
  1846. else
  1847. stid = -1;
  1848. } else {
  1849. stid = bitmap_find_free_region(t->stid_bmap, t->nstids, 2);
  1850. if (stid < 0)
  1851. stid = -1;
  1852. }
  1853. if (stid >= 0) {
  1854. t->stid_tab[stid].data = data;
  1855. stid += t->stid_base;
  1856. t->stids_in_use++;
  1857. }
  1858. spin_unlock_bh(&t->stid_lock);
  1859. return stid;
  1860. }
  1861. EXPORT_SYMBOL(cxgb4_alloc_stid);
  1862. /*
  1863. * Release a server TID.
  1864. */
  1865. void cxgb4_free_stid(struct tid_info *t, unsigned int stid, int family)
  1866. {
  1867. stid -= t->stid_base;
  1868. spin_lock_bh(&t->stid_lock);
  1869. if (family == PF_INET)
  1870. __clear_bit(stid, t->stid_bmap);
  1871. else
  1872. bitmap_release_region(t->stid_bmap, stid, 2);
  1873. t->stid_tab[stid].data = NULL;
  1874. t->stids_in_use--;
  1875. spin_unlock_bh(&t->stid_lock);
  1876. }
  1877. EXPORT_SYMBOL(cxgb4_free_stid);
  1878. /*
  1879. * Populate a TID_RELEASE WR. Caller must properly size the skb.
  1880. */
  1881. static void mk_tid_release(struct sk_buff *skb, unsigned int chan,
  1882. unsigned int tid)
  1883. {
  1884. struct cpl_tid_release *req;
  1885. set_wr_txq(skb, CPL_PRIORITY_SETUP, chan);
  1886. req = (struct cpl_tid_release *)__skb_put(skb, sizeof(*req));
  1887. INIT_TP_WR(req, tid);
  1888. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, tid));
  1889. }
  1890. /*
  1891. * Queue a TID release request and if necessary schedule a work queue to
  1892. * process it.
  1893. */
  1894. void cxgb4_queue_tid_release(struct tid_info *t, unsigned int chan,
  1895. unsigned int tid)
  1896. {
  1897. void **p = &t->tid_tab[tid];
  1898. struct adapter *adap = container_of(t, struct adapter, tids);
  1899. spin_lock_bh(&adap->tid_release_lock);
  1900. *p = adap->tid_release_head;
  1901. /* Low 2 bits encode the Tx channel number */
  1902. adap->tid_release_head = (void **)((uintptr_t)p | chan);
  1903. if (!adap->tid_release_task_busy) {
  1904. adap->tid_release_task_busy = true;
  1905. schedule_work(&adap->tid_release_task);
  1906. }
  1907. spin_unlock_bh(&adap->tid_release_lock);
  1908. }
  1909. EXPORT_SYMBOL(cxgb4_queue_tid_release);
  1910. /*
  1911. * Process the list of pending TID release requests.
  1912. */
  1913. static void process_tid_release_list(struct work_struct *work)
  1914. {
  1915. struct sk_buff *skb;
  1916. struct adapter *adap;
  1917. adap = container_of(work, struct adapter, tid_release_task);
  1918. spin_lock_bh(&adap->tid_release_lock);
  1919. while (adap->tid_release_head) {
  1920. void **p = adap->tid_release_head;
  1921. unsigned int chan = (uintptr_t)p & 3;
  1922. p = (void *)p - chan;
  1923. adap->tid_release_head = *p;
  1924. *p = NULL;
  1925. spin_unlock_bh(&adap->tid_release_lock);
  1926. while (!(skb = alloc_skb(sizeof(struct cpl_tid_release),
  1927. GFP_KERNEL)))
  1928. schedule_timeout_uninterruptible(1);
  1929. mk_tid_release(skb, chan, p - adap->tids.tid_tab);
  1930. t4_ofld_send(adap, skb);
  1931. spin_lock_bh(&adap->tid_release_lock);
  1932. }
  1933. adap->tid_release_task_busy = false;
  1934. spin_unlock_bh(&adap->tid_release_lock);
  1935. }
  1936. /*
  1937. * Release a TID and inform HW. If we are unable to allocate the release
  1938. * message we defer to a work queue.
  1939. */
  1940. void cxgb4_remove_tid(struct tid_info *t, unsigned int chan, unsigned int tid)
  1941. {
  1942. void *old;
  1943. struct sk_buff *skb;
  1944. struct adapter *adap = container_of(t, struct adapter, tids);
  1945. old = t->tid_tab[tid];
  1946. skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
  1947. if (likely(skb)) {
  1948. t->tid_tab[tid] = NULL;
  1949. mk_tid_release(skb, chan, tid);
  1950. t4_ofld_send(adap, skb);
  1951. } else
  1952. cxgb4_queue_tid_release(t, chan, tid);
  1953. if (old)
  1954. atomic_dec(&t->tids_in_use);
  1955. }
  1956. EXPORT_SYMBOL(cxgb4_remove_tid);
  1957. /*
  1958. * Allocate and initialize the TID tables. Returns 0 on success.
  1959. */
  1960. static int tid_init(struct tid_info *t)
  1961. {
  1962. size_t size;
  1963. unsigned int natids = t->natids;
  1964. size = t->ntids * sizeof(*t->tid_tab) + natids * sizeof(*t->atid_tab) +
  1965. t->nstids * sizeof(*t->stid_tab) +
  1966. BITS_TO_LONGS(t->nstids) * sizeof(long);
  1967. t->tid_tab = t4_alloc_mem(size);
  1968. if (!t->tid_tab)
  1969. return -ENOMEM;
  1970. t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
  1971. t->stid_tab = (struct serv_entry *)&t->atid_tab[natids];
  1972. t->stid_bmap = (unsigned long *)&t->stid_tab[t->nstids];
  1973. spin_lock_init(&t->stid_lock);
  1974. spin_lock_init(&t->atid_lock);
  1975. t->stids_in_use = 0;
  1976. t->afree = NULL;
  1977. t->atids_in_use = 0;
  1978. atomic_set(&t->tids_in_use, 0);
  1979. /* Setup the free list for atid_tab and clear the stid bitmap. */
  1980. if (natids) {
  1981. while (--natids)
  1982. t->atid_tab[natids - 1].next = &t->atid_tab[natids];
  1983. t->afree = t->atid_tab;
  1984. }
  1985. bitmap_zero(t->stid_bmap, t->nstids);
  1986. return 0;
  1987. }
  1988. /**
  1989. * cxgb4_create_server - create an IP server
  1990. * @dev: the device
  1991. * @stid: the server TID
  1992. * @sip: local IP address to bind server to
  1993. * @sport: the server's TCP port
  1994. * @queue: queue to direct messages from this server to
  1995. *
  1996. * Create an IP server for the given port and address.
  1997. * Returns <0 on error and one of the %NET_XMIT_* values on success.
  1998. */
  1999. int cxgb4_create_server(const struct net_device *dev, unsigned int stid,
  2000. __be32 sip, __be16 sport, unsigned int queue)
  2001. {
  2002. unsigned int chan;
  2003. struct sk_buff *skb;
  2004. struct adapter *adap;
  2005. struct cpl_pass_open_req *req;
  2006. skb = alloc_skb(sizeof(*req), GFP_KERNEL);
  2007. if (!skb)
  2008. return -ENOMEM;
  2009. adap = netdev2adap(dev);
  2010. req = (struct cpl_pass_open_req *)__skb_put(skb, sizeof(*req));
  2011. INIT_TP_WR(req, 0);
  2012. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, stid));
  2013. req->local_port = sport;
  2014. req->peer_port = htons(0);
  2015. req->local_ip = sip;
  2016. req->peer_ip = htonl(0);
  2017. chan = netdev2pinfo(adap->sge.ingr_map[queue]->netdev)->tx_chan;
  2018. req->opt0 = cpu_to_be64(TX_CHAN(chan));
  2019. req->opt1 = cpu_to_be64(CONN_POLICY_ASK |
  2020. SYN_RSS_ENABLE | SYN_RSS_QUEUE(queue));
  2021. return t4_mgmt_tx(adap, skb);
  2022. }
  2023. EXPORT_SYMBOL(cxgb4_create_server);
  2024. /**
  2025. * cxgb4_create_server6 - create an IPv6 server
  2026. * @dev: the device
  2027. * @stid: the server TID
  2028. * @sip: local IPv6 address to bind server to
  2029. * @sport: the server's TCP port
  2030. * @queue: queue to direct messages from this server to
  2031. *
  2032. * Create an IPv6 server for the given port and address.
  2033. * Returns <0 on error and one of the %NET_XMIT_* values on success.
  2034. */
  2035. int cxgb4_create_server6(const struct net_device *dev, unsigned int stid,
  2036. const struct in6_addr *sip, __be16 sport,
  2037. unsigned int queue)
  2038. {
  2039. unsigned int chan;
  2040. struct sk_buff *skb;
  2041. struct adapter *adap;
  2042. struct cpl_pass_open_req6 *req;
  2043. skb = alloc_skb(sizeof(*req), GFP_KERNEL);
  2044. if (!skb)
  2045. return -ENOMEM;
  2046. adap = netdev2adap(dev);
  2047. req = (struct cpl_pass_open_req6 *)__skb_put(skb, sizeof(*req));
  2048. INIT_TP_WR(req, 0);
  2049. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ6, stid));
  2050. req->local_port = sport;
  2051. req->peer_port = htons(0);
  2052. req->local_ip_hi = *(__be64 *)(sip->s6_addr);
  2053. req->local_ip_lo = *(__be64 *)(sip->s6_addr + 8);
  2054. req->peer_ip_hi = cpu_to_be64(0);
  2055. req->peer_ip_lo = cpu_to_be64(0);
  2056. chan = netdev2pinfo(adap->sge.ingr_map[queue]->netdev)->tx_chan;
  2057. req->opt0 = cpu_to_be64(TX_CHAN(chan));
  2058. req->opt1 = cpu_to_be64(CONN_POLICY_ASK |
  2059. SYN_RSS_ENABLE | SYN_RSS_QUEUE(queue));
  2060. return t4_mgmt_tx(adap, skb);
  2061. }
  2062. EXPORT_SYMBOL(cxgb4_create_server6);
  2063. /**
  2064. * cxgb4_best_mtu - find the entry in the MTU table closest to an MTU
  2065. * @mtus: the HW MTU table
  2066. * @mtu: the target MTU
  2067. * @idx: index of selected entry in the MTU table
  2068. *
  2069. * Returns the index and the value in the HW MTU table that is closest to
  2070. * but does not exceed @mtu, unless @mtu is smaller than any value in the
  2071. * table, in which case that smallest available value is selected.
  2072. */
  2073. unsigned int cxgb4_best_mtu(const unsigned short *mtus, unsigned short mtu,
  2074. unsigned int *idx)
  2075. {
  2076. unsigned int i = 0;
  2077. while (i < NMTUS - 1 && mtus[i + 1] <= mtu)
  2078. ++i;
  2079. if (idx)
  2080. *idx = i;
  2081. return mtus[i];
  2082. }
  2083. EXPORT_SYMBOL(cxgb4_best_mtu);
  2084. /**
  2085. * cxgb4_port_chan - get the HW channel of a port
  2086. * @dev: the net device for the port
  2087. *
  2088. * Return the HW Tx channel of the given port.
  2089. */
  2090. unsigned int cxgb4_port_chan(const struct net_device *dev)
  2091. {
  2092. return netdev2pinfo(dev)->tx_chan;
  2093. }
  2094. EXPORT_SYMBOL(cxgb4_port_chan);
  2095. /**
  2096. * cxgb4_port_viid - get the VI id of a port
  2097. * @dev: the net device for the port
  2098. *
  2099. * Return the VI id of the given port.
  2100. */
  2101. unsigned int cxgb4_port_viid(const struct net_device *dev)
  2102. {
  2103. return netdev2pinfo(dev)->viid;
  2104. }
  2105. EXPORT_SYMBOL(cxgb4_port_viid);
  2106. /**
  2107. * cxgb4_port_idx - get the index of a port
  2108. * @dev: the net device for the port
  2109. *
  2110. * Return the index of the given port.
  2111. */
  2112. unsigned int cxgb4_port_idx(const struct net_device *dev)
  2113. {
  2114. return netdev2pinfo(dev)->port_id;
  2115. }
  2116. EXPORT_SYMBOL(cxgb4_port_idx);
  2117. /**
  2118. * cxgb4_netdev_by_hwid - return the net device of a HW port
  2119. * @pdev: identifies the adapter
  2120. * @id: the HW port id
  2121. *
  2122. * Return the net device associated with the interface with the given HW
  2123. * id.
  2124. */
  2125. struct net_device *cxgb4_netdev_by_hwid(struct pci_dev *pdev, unsigned int id)
  2126. {
  2127. const struct adapter *adap = pci_get_drvdata(pdev);
  2128. if (!adap || id >= NCHAN)
  2129. return NULL;
  2130. id = adap->chan_map[id];
  2131. return id < MAX_NPORTS ? adap->port[id] : NULL;
  2132. }
  2133. EXPORT_SYMBOL(cxgb4_netdev_by_hwid);
  2134. void cxgb4_get_tcp_stats(struct pci_dev *pdev, struct tp_tcp_stats *v4,
  2135. struct tp_tcp_stats *v6)
  2136. {
  2137. struct adapter *adap = pci_get_drvdata(pdev);
  2138. spin_lock(&adap->stats_lock);
  2139. t4_tp_get_tcp_stats(adap, v4, v6);
  2140. spin_unlock(&adap->stats_lock);
  2141. }
  2142. EXPORT_SYMBOL(cxgb4_get_tcp_stats);
  2143. void cxgb4_iscsi_init(struct net_device *dev, unsigned int tag_mask,
  2144. const unsigned int *pgsz_order)
  2145. {
  2146. struct adapter *adap = netdev2adap(dev);
  2147. t4_write_reg(adap, ULP_RX_ISCSI_TAGMASK, tag_mask);
  2148. t4_write_reg(adap, ULP_RX_ISCSI_PSZ, HPZ0(pgsz_order[0]) |
  2149. HPZ1(pgsz_order[1]) | HPZ2(pgsz_order[2]) |
  2150. HPZ3(pgsz_order[3]));
  2151. }
  2152. EXPORT_SYMBOL(cxgb4_iscsi_init);
  2153. static struct pci_driver cxgb4_driver;
  2154. static void check_neigh_update(struct neighbour *neigh)
  2155. {
  2156. const struct device *parent;
  2157. const struct net_device *netdev = neigh->dev;
  2158. if (netdev->priv_flags & IFF_802_1Q_VLAN)
  2159. netdev = vlan_dev_real_dev(netdev);
  2160. parent = netdev->dev.parent;
  2161. if (parent && parent->driver == &cxgb4_driver.driver)
  2162. t4_l2t_update(dev_get_drvdata(parent), neigh);
  2163. }
  2164. static int netevent_cb(struct notifier_block *nb, unsigned long event,
  2165. void *data)
  2166. {
  2167. switch (event) {
  2168. case NETEVENT_NEIGH_UPDATE:
  2169. check_neigh_update(data);
  2170. break;
  2171. case NETEVENT_PMTU_UPDATE:
  2172. case NETEVENT_REDIRECT:
  2173. default:
  2174. break;
  2175. }
  2176. return 0;
  2177. }
  2178. static bool netevent_registered;
  2179. static struct notifier_block cxgb4_netevent_nb = {
  2180. .notifier_call = netevent_cb
  2181. };
  2182. static void uld_attach(struct adapter *adap, unsigned int uld)
  2183. {
  2184. void *handle;
  2185. struct cxgb4_lld_info lli;
  2186. lli.pdev = adap->pdev;
  2187. lli.l2t = adap->l2t;
  2188. lli.tids = &adap->tids;
  2189. lli.ports = adap->port;
  2190. lli.vr = &adap->vres;
  2191. lli.mtus = adap->params.mtus;
  2192. if (uld == CXGB4_ULD_RDMA) {
  2193. lli.rxq_ids = adap->sge.rdma_rxq;
  2194. lli.nrxq = adap->sge.rdmaqs;
  2195. } else if (uld == CXGB4_ULD_ISCSI) {
  2196. lli.rxq_ids = adap->sge.ofld_rxq;
  2197. lli.nrxq = adap->sge.ofldqsets;
  2198. }
  2199. lli.ntxq = adap->sge.ofldqsets;
  2200. lli.nchan = adap->params.nports;
  2201. lli.nports = adap->params.nports;
  2202. lli.wr_cred = adap->params.ofldq_wr_cred;
  2203. lli.adapter_type = adap->params.rev;
  2204. lli.iscsi_iolen = MAXRXDATA_GET(t4_read_reg(adap, TP_PARA_REG2));
  2205. lli.udb_density = 1 << QUEUESPERPAGEPF0_GET(
  2206. t4_read_reg(adap, SGE_EGRESS_QUEUES_PER_PAGE_PF));
  2207. lli.ucq_density = 1 << QUEUESPERPAGEPF0_GET(
  2208. t4_read_reg(adap, SGE_INGRESS_QUEUES_PER_PAGE_PF));
  2209. lli.gts_reg = adap->regs + MYPF_REG(SGE_PF_GTS);
  2210. lli.db_reg = adap->regs + MYPF_REG(SGE_PF_KDOORBELL);
  2211. lli.fw_vers = adap->params.fw_vers;
  2212. handle = ulds[uld].add(&lli);
  2213. if (IS_ERR(handle)) {
  2214. dev_warn(adap->pdev_dev,
  2215. "could not attach to the %s driver, error %ld\n",
  2216. uld_str[uld], PTR_ERR(handle));
  2217. return;
  2218. }
  2219. adap->uld_handle[uld] = handle;
  2220. if (!netevent_registered) {
  2221. register_netevent_notifier(&cxgb4_netevent_nb);
  2222. netevent_registered = true;
  2223. }
  2224. if (adap->flags & FULL_INIT_DONE)
  2225. ulds[uld].state_change(handle, CXGB4_STATE_UP);
  2226. }
  2227. static void attach_ulds(struct adapter *adap)
  2228. {
  2229. unsigned int i;
  2230. mutex_lock(&uld_mutex);
  2231. list_add_tail(&adap->list_node, &adapter_list);
  2232. for (i = 0; i < CXGB4_ULD_MAX; i++)
  2233. if (ulds[i].add)
  2234. uld_attach(adap, i);
  2235. mutex_unlock(&uld_mutex);
  2236. }
  2237. static void detach_ulds(struct adapter *adap)
  2238. {
  2239. unsigned int i;
  2240. mutex_lock(&uld_mutex);
  2241. list_del(&adap->list_node);
  2242. for (i = 0; i < CXGB4_ULD_MAX; i++)
  2243. if (adap->uld_handle[i]) {
  2244. ulds[i].state_change(adap->uld_handle[i],
  2245. CXGB4_STATE_DETACH);
  2246. adap->uld_handle[i] = NULL;
  2247. }
  2248. if (netevent_registered && list_empty(&adapter_list)) {
  2249. unregister_netevent_notifier(&cxgb4_netevent_nb);
  2250. netevent_registered = false;
  2251. }
  2252. mutex_unlock(&uld_mutex);
  2253. }
  2254. static void notify_ulds(struct adapter *adap, enum cxgb4_state new_state)
  2255. {
  2256. unsigned int i;
  2257. mutex_lock(&uld_mutex);
  2258. for (i = 0; i < CXGB4_ULD_MAX; i++)
  2259. if (adap->uld_handle[i])
  2260. ulds[i].state_change(adap->uld_handle[i], new_state);
  2261. mutex_unlock(&uld_mutex);
  2262. }
  2263. /**
  2264. * cxgb4_register_uld - register an upper-layer driver
  2265. * @type: the ULD type
  2266. * @p: the ULD methods
  2267. *
  2268. * Registers an upper-layer driver with this driver and notifies the ULD
  2269. * about any presently available devices that support its type. Returns
  2270. * %-EBUSY if a ULD of the same type is already registered.
  2271. */
  2272. int cxgb4_register_uld(enum cxgb4_uld type, const struct cxgb4_uld_info *p)
  2273. {
  2274. int ret = 0;
  2275. struct adapter *adap;
  2276. if (type >= CXGB4_ULD_MAX)
  2277. return -EINVAL;
  2278. mutex_lock(&uld_mutex);
  2279. if (ulds[type].add) {
  2280. ret = -EBUSY;
  2281. goto out;
  2282. }
  2283. ulds[type] = *p;
  2284. list_for_each_entry(adap, &adapter_list, list_node)
  2285. uld_attach(adap, type);
  2286. out: mutex_unlock(&uld_mutex);
  2287. return ret;
  2288. }
  2289. EXPORT_SYMBOL(cxgb4_register_uld);
  2290. /**
  2291. * cxgb4_unregister_uld - unregister an upper-layer driver
  2292. * @type: the ULD type
  2293. *
  2294. * Unregisters an existing upper-layer driver.
  2295. */
  2296. int cxgb4_unregister_uld(enum cxgb4_uld type)
  2297. {
  2298. struct adapter *adap;
  2299. if (type >= CXGB4_ULD_MAX)
  2300. return -EINVAL;
  2301. mutex_lock(&uld_mutex);
  2302. list_for_each_entry(adap, &adapter_list, list_node)
  2303. adap->uld_handle[type] = NULL;
  2304. ulds[type].add = NULL;
  2305. mutex_unlock(&uld_mutex);
  2306. return 0;
  2307. }
  2308. EXPORT_SYMBOL(cxgb4_unregister_uld);
  2309. /**
  2310. * cxgb_up - enable the adapter
  2311. * @adap: adapter being enabled
  2312. *
  2313. * Called when the first port is enabled, this function performs the
  2314. * actions necessary to make an adapter operational, such as completing
  2315. * the initialization of HW modules, and enabling interrupts.
  2316. *
  2317. * Must be called with the rtnl lock held.
  2318. */
  2319. static int cxgb_up(struct adapter *adap)
  2320. {
  2321. int err;
  2322. err = setup_sge_queues(adap);
  2323. if (err)
  2324. goto out;
  2325. err = setup_rss(adap);
  2326. if (err)
  2327. goto freeq;
  2328. if (adap->flags & USING_MSIX) {
  2329. name_msix_vecs(adap);
  2330. err = request_irq(adap->msix_info[0].vec, t4_nondata_intr, 0,
  2331. adap->msix_info[0].desc, adap);
  2332. if (err)
  2333. goto irq_err;
  2334. err = request_msix_queue_irqs(adap);
  2335. if (err) {
  2336. free_irq(adap->msix_info[0].vec, adap);
  2337. goto irq_err;
  2338. }
  2339. } else {
  2340. err = request_irq(adap->pdev->irq, t4_intr_handler(adap),
  2341. (adap->flags & USING_MSI) ? 0 : IRQF_SHARED,
  2342. adap->name, adap);
  2343. if (err)
  2344. goto irq_err;
  2345. }
  2346. enable_rx(adap);
  2347. t4_sge_start(adap);
  2348. t4_intr_enable(adap);
  2349. adap->flags |= FULL_INIT_DONE;
  2350. notify_ulds(adap, CXGB4_STATE_UP);
  2351. out:
  2352. return err;
  2353. irq_err:
  2354. dev_err(adap->pdev_dev, "request_irq failed, err %d\n", err);
  2355. freeq:
  2356. t4_free_sge_resources(adap);
  2357. goto out;
  2358. }
  2359. static void cxgb_down(struct adapter *adapter)
  2360. {
  2361. t4_intr_disable(adapter);
  2362. cancel_work_sync(&adapter->tid_release_task);
  2363. adapter->tid_release_task_busy = false;
  2364. adapter->tid_release_head = NULL;
  2365. if (adapter->flags & USING_MSIX) {
  2366. free_msix_queue_irqs(adapter);
  2367. free_irq(adapter->msix_info[0].vec, adapter);
  2368. } else
  2369. free_irq(adapter->pdev->irq, adapter);
  2370. quiesce_rx(adapter);
  2371. t4_sge_stop(adapter);
  2372. t4_free_sge_resources(adapter);
  2373. adapter->flags &= ~FULL_INIT_DONE;
  2374. }
  2375. /*
  2376. * net_device operations
  2377. */
  2378. static int cxgb_open(struct net_device *dev)
  2379. {
  2380. int err;
  2381. struct port_info *pi = netdev_priv(dev);
  2382. struct adapter *adapter = pi->adapter;
  2383. if (!(adapter->flags & FULL_INIT_DONE)) {
  2384. err = cxgb_up(adapter);
  2385. if (err < 0)
  2386. return err;
  2387. }
  2388. dev->real_num_tx_queues = pi->nqsets;
  2389. err = link_start(dev);
  2390. if (!err)
  2391. netif_tx_start_all_queues(dev);
  2392. return err;
  2393. }
  2394. static int cxgb_close(struct net_device *dev)
  2395. {
  2396. struct port_info *pi = netdev_priv(dev);
  2397. struct adapter *adapter = pi->adapter;
  2398. netif_tx_stop_all_queues(dev);
  2399. netif_carrier_off(dev);
  2400. return t4_enable_vi(adapter, 0, pi->viid, false, false);
  2401. }
  2402. static struct rtnl_link_stats64 *cxgb_get_stats(struct net_device *dev,
  2403. struct rtnl_link_stats64 *ns)
  2404. {
  2405. struct port_stats stats;
  2406. struct port_info *p = netdev_priv(dev);
  2407. struct adapter *adapter = p->adapter;
  2408. spin_lock(&adapter->stats_lock);
  2409. t4_get_port_stats(adapter, p->tx_chan, &stats);
  2410. spin_unlock(&adapter->stats_lock);
  2411. ns->tx_bytes = stats.tx_octets;
  2412. ns->tx_packets = stats.tx_frames;
  2413. ns->rx_bytes = stats.rx_octets;
  2414. ns->rx_packets = stats.rx_frames;
  2415. ns->multicast = stats.rx_mcast_frames;
  2416. /* detailed rx_errors */
  2417. ns->rx_length_errors = stats.rx_jabber + stats.rx_too_long +
  2418. stats.rx_runt;
  2419. ns->rx_over_errors = 0;
  2420. ns->rx_crc_errors = stats.rx_fcs_err;
  2421. ns->rx_frame_errors = stats.rx_symbol_err;
  2422. ns->rx_fifo_errors = stats.rx_ovflow0 + stats.rx_ovflow1 +
  2423. stats.rx_ovflow2 + stats.rx_ovflow3 +
  2424. stats.rx_trunc0 + stats.rx_trunc1 +
  2425. stats.rx_trunc2 + stats.rx_trunc3;
  2426. ns->rx_missed_errors = 0;
  2427. /* detailed tx_errors */
  2428. ns->tx_aborted_errors = 0;
  2429. ns->tx_carrier_errors = 0;
  2430. ns->tx_fifo_errors = 0;
  2431. ns->tx_heartbeat_errors = 0;
  2432. ns->tx_window_errors = 0;
  2433. ns->tx_errors = stats.tx_error_frames;
  2434. ns->rx_errors = stats.rx_symbol_err + stats.rx_fcs_err +
  2435. ns->rx_length_errors + stats.rx_len_err + ns->rx_fifo_errors;
  2436. return ns;
  2437. }
  2438. static int cxgb_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
  2439. {
  2440. int ret = 0, prtad, devad;
  2441. struct port_info *pi = netdev_priv(dev);
  2442. struct mii_ioctl_data *data = (struct mii_ioctl_data *)&req->ifr_data;
  2443. switch (cmd) {
  2444. case SIOCGMIIPHY:
  2445. if (pi->mdio_addr < 0)
  2446. return -EOPNOTSUPP;
  2447. data->phy_id = pi->mdio_addr;
  2448. break;
  2449. case SIOCGMIIREG:
  2450. case SIOCSMIIREG:
  2451. if (mdio_phy_id_is_c45(data->phy_id)) {
  2452. prtad = mdio_phy_id_prtad(data->phy_id);
  2453. devad = mdio_phy_id_devad(data->phy_id);
  2454. } else if (data->phy_id < 32) {
  2455. prtad = data->phy_id;
  2456. devad = 0;
  2457. data->reg_num &= 0x1f;
  2458. } else
  2459. return -EINVAL;
  2460. if (cmd == SIOCGMIIREG)
  2461. ret = t4_mdio_rd(pi->adapter, 0, prtad, devad,
  2462. data->reg_num, &data->val_out);
  2463. else
  2464. ret = t4_mdio_wr(pi->adapter, 0, prtad, devad,
  2465. data->reg_num, data->val_in);
  2466. break;
  2467. default:
  2468. return -EOPNOTSUPP;
  2469. }
  2470. return ret;
  2471. }
  2472. static void cxgb_set_rxmode(struct net_device *dev)
  2473. {
  2474. /* unfortunately we can't return errors to the stack */
  2475. set_rxmode(dev, -1, false);
  2476. }
  2477. static int cxgb_change_mtu(struct net_device *dev, int new_mtu)
  2478. {
  2479. int ret;
  2480. struct port_info *pi = netdev_priv(dev);
  2481. if (new_mtu < 81 || new_mtu > MAX_MTU) /* accommodate SACK */
  2482. return -EINVAL;
  2483. ret = t4_set_rxmode(pi->adapter, 0, pi->viid, new_mtu, -1, -1, -1, -1,
  2484. true);
  2485. if (!ret)
  2486. dev->mtu = new_mtu;
  2487. return ret;
  2488. }
  2489. static int cxgb_set_mac_addr(struct net_device *dev, void *p)
  2490. {
  2491. int ret;
  2492. struct sockaddr *addr = p;
  2493. struct port_info *pi = netdev_priv(dev);
  2494. if (!is_valid_ether_addr(addr->sa_data))
  2495. return -EINVAL;
  2496. ret = t4_change_mac(pi->adapter, 0, pi->viid, pi->xact_addr_filt,
  2497. addr->sa_data, true, true);
  2498. if (ret < 0)
  2499. return ret;
  2500. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  2501. pi->xact_addr_filt = ret;
  2502. return 0;
  2503. }
  2504. static void vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
  2505. {
  2506. struct port_info *pi = netdev_priv(dev);
  2507. pi->vlan_grp = grp;
  2508. t4_set_rxmode(pi->adapter, 0, pi->viid, -1, -1, -1, -1, grp != NULL,
  2509. true);
  2510. }
  2511. #ifdef CONFIG_NET_POLL_CONTROLLER
  2512. static void cxgb_netpoll(struct net_device *dev)
  2513. {
  2514. struct port_info *pi = netdev_priv(dev);
  2515. struct adapter *adap = pi->adapter;
  2516. if (adap->flags & USING_MSIX) {
  2517. int i;
  2518. struct sge_eth_rxq *rx = &adap->sge.ethrxq[pi->first_qset];
  2519. for (i = pi->nqsets; i; i--, rx++)
  2520. t4_sge_intr_msix(0, &rx->rspq);
  2521. } else
  2522. t4_intr_handler(adap)(0, adap);
  2523. }
  2524. #endif
  2525. static const struct net_device_ops cxgb4_netdev_ops = {
  2526. .ndo_open = cxgb_open,
  2527. .ndo_stop = cxgb_close,
  2528. .ndo_start_xmit = t4_eth_xmit,
  2529. .ndo_get_stats64 = cxgb_get_stats,
  2530. .ndo_set_rx_mode = cxgb_set_rxmode,
  2531. .ndo_set_mac_address = cxgb_set_mac_addr,
  2532. .ndo_validate_addr = eth_validate_addr,
  2533. .ndo_do_ioctl = cxgb_ioctl,
  2534. .ndo_change_mtu = cxgb_change_mtu,
  2535. .ndo_vlan_rx_register = vlan_rx_register,
  2536. #ifdef CONFIG_NET_POLL_CONTROLLER
  2537. .ndo_poll_controller = cxgb_netpoll,
  2538. #endif
  2539. };
  2540. void t4_fatal_err(struct adapter *adap)
  2541. {
  2542. t4_set_reg_field(adap, SGE_CONTROL, GLOBALENABLE, 0);
  2543. t4_intr_disable(adap);
  2544. dev_alert(adap->pdev_dev, "encountered fatal error, adapter stopped\n");
  2545. }
  2546. static void setup_memwin(struct adapter *adap)
  2547. {
  2548. u32 bar0;
  2549. bar0 = pci_resource_start(adap->pdev, 0); /* truncation intentional */
  2550. t4_write_reg(adap, PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN, 0),
  2551. (bar0 + MEMWIN0_BASE) | BIR(0) |
  2552. WINDOW(ilog2(MEMWIN0_APERTURE) - 10));
  2553. t4_write_reg(adap, PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN, 1),
  2554. (bar0 + MEMWIN1_BASE) | BIR(0) |
  2555. WINDOW(ilog2(MEMWIN1_APERTURE) - 10));
  2556. t4_write_reg(adap, PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN, 2),
  2557. (bar0 + MEMWIN2_BASE) | BIR(0) |
  2558. WINDOW(ilog2(MEMWIN2_APERTURE) - 10));
  2559. }
  2560. static int adap_init1(struct adapter *adap, struct fw_caps_config_cmd *c)
  2561. {
  2562. u32 v;
  2563. int ret;
  2564. /* get device capabilities */
  2565. memset(c, 0, sizeof(*c));
  2566. c->op_to_write = htonl(FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
  2567. FW_CMD_REQUEST | FW_CMD_READ);
  2568. c->retval_len16 = htonl(FW_LEN16(*c));
  2569. ret = t4_wr_mbox(adap, 0, c, sizeof(*c), c);
  2570. if (ret < 0)
  2571. return ret;
  2572. /* select capabilities we'll be using */
  2573. if (c->niccaps & htons(FW_CAPS_CONFIG_NIC_VM)) {
  2574. if (!vf_acls)
  2575. c->niccaps ^= htons(FW_CAPS_CONFIG_NIC_VM);
  2576. else
  2577. c->niccaps = htons(FW_CAPS_CONFIG_NIC_VM);
  2578. } else if (vf_acls) {
  2579. dev_err(adap->pdev_dev, "virtualization ACLs not supported");
  2580. return ret;
  2581. }
  2582. c->op_to_write = htonl(FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
  2583. FW_CMD_REQUEST | FW_CMD_WRITE);
  2584. ret = t4_wr_mbox(adap, 0, c, sizeof(*c), NULL);
  2585. if (ret < 0)
  2586. return ret;
  2587. ret = t4_config_glbl_rss(adap, 0,
  2588. FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL,
  2589. FW_RSS_GLB_CONFIG_CMD_TNLMAPEN |
  2590. FW_RSS_GLB_CONFIG_CMD_TNLALLLKP);
  2591. if (ret < 0)
  2592. return ret;
  2593. ret = t4_cfg_pfvf(adap, 0, 0, 0, MAX_EGRQ, 64, MAX_INGQ, 0, 0, 4,
  2594. 0xf, 0xf, 16, FW_CMD_CAP_PF, FW_CMD_CAP_PF);
  2595. if (ret < 0)
  2596. return ret;
  2597. t4_sge_init(adap);
  2598. /* get basic stuff going */
  2599. ret = t4_early_init(adap, 0);
  2600. if (ret < 0)
  2601. return ret;
  2602. /* tweak some settings */
  2603. t4_write_reg(adap, TP_SHIFT_CNT, 0x64f8849);
  2604. t4_write_reg(adap, ULP_RX_TDDP_PSZ, HPZ0(PAGE_SHIFT - 12));
  2605. t4_write_reg(adap, TP_PIO_ADDR, TP_INGRESS_CONFIG);
  2606. v = t4_read_reg(adap, TP_PIO_DATA);
  2607. t4_write_reg(adap, TP_PIO_DATA, v & ~CSUM_HAS_PSEUDO_HDR);
  2608. setup_memwin(adap);
  2609. return 0;
  2610. }
  2611. /*
  2612. * Max # of ATIDs. The absolute HW max is 16K but we keep it lower.
  2613. */
  2614. #define MAX_ATIDS 8192U
  2615. /*
  2616. * Phase 0 of initialization: contact FW, obtain config, perform basic init.
  2617. */
  2618. static int adap_init0(struct adapter *adap)
  2619. {
  2620. int ret;
  2621. u32 v, port_vec;
  2622. enum dev_state state;
  2623. u32 params[7], val[7];
  2624. struct fw_caps_config_cmd c;
  2625. ret = t4_check_fw_version(adap);
  2626. if (ret == -EINVAL || ret > 0) {
  2627. if (upgrade_fw(adap) >= 0) /* recache FW version */
  2628. ret = t4_check_fw_version(adap);
  2629. }
  2630. if (ret < 0)
  2631. return ret;
  2632. /* contact FW, request master */
  2633. ret = t4_fw_hello(adap, 0, 0, MASTER_MUST, &state);
  2634. if (ret < 0) {
  2635. dev_err(adap->pdev_dev, "could not connect to FW, error %d\n",
  2636. ret);
  2637. return ret;
  2638. }
  2639. /* reset device */
  2640. ret = t4_fw_reset(adap, 0, PIORSTMODE | PIORST);
  2641. if (ret < 0)
  2642. goto bye;
  2643. for (v = 0; v < SGE_NTIMERS - 1; v++)
  2644. adap->sge.timer_val[v] = min(intr_holdoff[v], MAX_SGE_TIMERVAL);
  2645. adap->sge.timer_val[SGE_NTIMERS - 1] = MAX_SGE_TIMERVAL;
  2646. adap->sge.counter_val[0] = 1;
  2647. for (v = 1; v < SGE_NCOUNTERS; v++)
  2648. adap->sge.counter_val[v] = min(intr_cnt[v - 1],
  2649. THRESHOLD_3_MASK);
  2650. #define FW_PARAM_DEV(param) \
  2651. (FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) | \
  2652. FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_##param))
  2653. params[0] = FW_PARAM_DEV(CCLK);
  2654. ret = t4_query_params(adap, 0, 0, 0, 1, params, val);
  2655. if (ret < 0)
  2656. goto bye;
  2657. adap->params.vpd.cclk = val[0];
  2658. ret = adap_init1(adap, &c);
  2659. if (ret < 0)
  2660. goto bye;
  2661. #define FW_PARAM_PFVF(param) \
  2662. (FW_PARAMS_MNEM(FW_PARAMS_MNEM_PFVF) | \
  2663. FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_PFVF_##param))
  2664. params[0] = FW_PARAM_DEV(PORTVEC);
  2665. params[1] = FW_PARAM_PFVF(L2T_START);
  2666. params[2] = FW_PARAM_PFVF(L2T_END);
  2667. params[3] = FW_PARAM_PFVF(FILTER_START);
  2668. params[4] = FW_PARAM_PFVF(FILTER_END);
  2669. ret = t4_query_params(adap, 0, 0, 0, 5, params, val);
  2670. if (ret < 0)
  2671. goto bye;
  2672. port_vec = val[0];
  2673. adap->tids.ftid_base = val[3];
  2674. adap->tids.nftids = val[4] - val[3] + 1;
  2675. if (c.ofldcaps) {
  2676. /* query offload-related parameters */
  2677. params[0] = FW_PARAM_DEV(NTID);
  2678. params[1] = FW_PARAM_PFVF(SERVER_START);
  2679. params[2] = FW_PARAM_PFVF(SERVER_END);
  2680. params[3] = FW_PARAM_PFVF(TDDP_START);
  2681. params[4] = FW_PARAM_PFVF(TDDP_END);
  2682. params[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
  2683. ret = t4_query_params(adap, 0, 0, 0, 6, params, val);
  2684. if (ret < 0)
  2685. goto bye;
  2686. adap->tids.ntids = val[0];
  2687. adap->tids.natids = min(adap->tids.ntids / 2, MAX_ATIDS);
  2688. adap->tids.stid_base = val[1];
  2689. adap->tids.nstids = val[2] - val[1] + 1;
  2690. adap->vres.ddp.start = val[3];
  2691. adap->vres.ddp.size = val[4] - val[3] + 1;
  2692. adap->params.ofldq_wr_cred = val[5];
  2693. adap->params.offload = 1;
  2694. }
  2695. if (c.rdmacaps) {
  2696. params[0] = FW_PARAM_PFVF(STAG_START);
  2697. params[1] = FW_PARAM_PFVF(STAG_END);
  2698. params[2] = FW_PARAM_PFVF(RQ_START);
  2699. params[3] = FW_PARAM_PFVF(RQ_END);
  2700. params[4] = FW_PARAM_PFVF(PBL_START);
  2701. params[5] = FW_PARAM_PFVF(PBL_END);
  2702. ret = t4_query_params(adap, 0, 0, 0, 6, params, val);
  2703. if (ret < 0)
  2704. goto bye;
  2705. adap->vres.stag.start = val[0];
  2706. adap->vres.stag.size = val[1] - val[0] + 1;
  2707. adap->vres.rq.start = val[2];
  2708. adap->vres.rq.size = val[3] - val[2] + 1;
  2709. adap->vres.pbl.start = val[4];
  2710. adap->vres.pbl.size = val[5] - val[4] + 1;
  2711. params[0] = FW_PARAM_PFVF(SQRQ_START);
  2712. params[1] = FW_PARAM_PFVF(SQRQ_END);
  2713. params[2] = FW_PARAM_PFVF(CQ_START);
  2714. params[3] = FW_PARAM_PFVF(CQ_END);
  2715. ret = t4_query_params(adap, 0, 0, 0, 4, params, val);
  2716. if (ret < 0)
  2717. goto bye;
  2718. adap->vres.qp.start = val[0];
  2719. adap->vres.qp.size = val[1] - val[0] + 1;
  2720. adap->vres.cq.start = val[2];
  2721. adap->vres.cq.size = val[3] - val[2] + 1;
  2722. }
  2723. if (c.iscsicaps) {
  2724. params[0] = FW_PARAM_PFVF(ISCSI_START);
  2725. params[1] = FW_PARAM_PFVF(ISCSI_END);
  2726. ret = t4_query_params(adap, 0, 0, 0, 2, params, val);
  2727. if (ret < 0)
  2728. goto bye;
  2729. adap->vres.iscsi.start = val[0];
  2730. adap->vres.iscsi.size = val[1] - val[0] + 1;
  2731. }
  2732. #undef FW_PARAM_PFVF
  2733. #undef FW_PARAM_DEV
  2734. adap->params.nports = hweight32(port_vec);
  2735. adap->params.portvec = port_vec;
  2736. adap->flags |= FW_OK;
  2737. /* These are finalized by FW initialization, load their values now */
  2738. v = t4_read_reg(adap, TP_TIMER_RESOLUTION);
  2739. adap->params.tp.tre = TIMERRESOLUTION_GET(v);
  2740. t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
  2741. t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
  2742. adap->params.b_wnd);
  2743. #ifdef CONFIG_PCI_IOV
  2744. /*
  2745. * Provision resource limits for Virtual Functions. We currently
  2746. * grant them all the same static resource limits except for the Port
  2747. * Access Rights Mask which we're assigning based on the PF. All of
  2748. * the static provisioning stuff for both the PF and VF really needs
  2749. * to be managed in a persistent manner for each device which the
  2750. * firmware controls.
  2751. */
  2752. {
  2753. int pf, vf;
  2754. for (pf = 0; pf < ARRAY_SIZE(num_vf); pf++) {
  2755. if (num_vf[pf] <= 0)
  2756. continue;
  2757. /* VF numbering starts at 1! */
  2758. for (vf = 1; vf <= num_vf[pf]; vf++) {
  2759. ret = t4_cfg_pfvf(adap, 0, pf, vf,
  2760. VFRES_NEQ, VFRES_NETHCTRL,
  2761. VFRES_NIQFLINT, VFRES_NIQ,
  2762. VFRES_TC, VFRES_NVI,
  2763. FW_PFVF_CMD_CMASK_MASK,
  2764. pfvfres_pmask(adap, pf, vf),
  2765. VFRES_NEXACTF,
  2766. VFRES_R_CAPS, VFRES_WX_CAPS);
  2767. if (ret < 0)
  2768. dev_warn(adap->pdev_dev, "failed to "
  2769. "provision pf/vf=%d/%d; "
  2770. "err=%d\n", pf, vf, ret);
  2771. }
  2772. }
  2773. }
  2774. #endif
  2775. return 0;
  2776. /*
  2777. * If a command timed out or failed with EIO FW does not operate within
  2778. * its spec or something catastrophic happened to HW/FW, stop issuing
  2779. * commands.
  2780. */
  2781. bye: if (ret != -ETIMEDOUT && ret != -EIO)
  2782. t4_fw_bye(adap, 0);
  2783. return ret;
  2784. }
  2785. /* EEH callbacks */
  2786. static pci_ers_result_t eeh_err_detected(struct pci_dev *pdev,
  2787. pci_channel_state_t state)
  2788. {
  2789. int i;
  2790. struct adapter *adap = pci_get_drvdata(pdev);
  2791. if (!adap)
  2792. goto out;
  2793. rtnl_lock();
  2794. adap->flags &= ~FW_OK;
  2795. notify_ulds(adap, CXGB4_STATE_START_RECOVERY);
  2796. for_each_port(adap, i) {
  2797. struct net_device *dev = adap->port[i];
  2798. netif_device_detach(dev);
  2799. netif_carrier_off(dev);
  2800. }
  2801. if (adap->flags & FULL_INIT_DONE)
  2802. cxgb_down(adap);
  2803. rtnl_unlock();
  2804. pci_disable_device(pdev);
  2805. out: return state == pci_channel_io_perm_failure ?
  2806. PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
  2807. }
  2808. static pci_ers_result_t eeh_slot_reset(struct pci_dev *pdev)
  2809. {
  2810. int i, ret;
  2811. struct fw_caps_config_cmd c;
  2812. struct adapter *adap = pci_get_drvdata(pdev);
  2813. if (!adap) {
  2814. pci_restore_state(pdev);
  2815. pci_save_state(pdev);
  2816. return PCI_ERS_RESULT_RECOVERED;
  2817. }
  2818. if (pci_enable_device(pdev)) {
  2819. dev_err(&pdev->dev, "cannot reenable PCI device after reset\n");
  2820. return PCI_ERS_RESULT_DISCONNECT;
  2821. }
  2822. pci_set_master(pdev);
  2823. pci_restore_state(pdev);
  2824. pci_save_state(pdev);
  2825. pci_cleanup_aer_uncorrect_error_status(pdev);
  2826. if (t4_wait_dev_ready(adap) < 0)
  2827. return PCI_ERS_RESULT_DISCONNECT;
  2828. if (t4_fw_hello(adap, 0, 0, MASTER_MUST, NULL))
  2829. return PCI_ERS_RESULT_DISCONNECT;
  2830. adap->flags |= FW_OK;
  2831. if (adap_init1(adap, &c))
  2832. return PCI_ERS_RESULT_DISCONNECT;
  2833. for_each_port(adap, i) {
  2834. struct port_info *p = adap2pinfo(adap, i);
  2835. ret = t4_alloc_vi(adap, 0, p->tx_chan, 0, 0, 1, NULL, NULL);
  2836. if (ret < 0)
  2837. return PCI_ERS_RESULT_DISCONNECT;
  2838. p->viid = ret;
  2839. p->xact_addr_filt = -1;
  2840. }
  2841. t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
  2842. adap->params.b_wnd);
  2843. if (cxgb_up(adap))
  2844. return PCI_ERS_RESULT_DISCONNECT;
  2845. return PCI_ERS_RESULT_RECOVERED;
  2846. }
  2847. static void eeh_resume(struct pci_dev *pdev)
  2848. {
  2849. int i;
  2850. struct adapter *adap = pci_get_drvdata(pdev);
  2851. if (!adap)
  2852. return;
  2853. rtnl_lock();
  2854. for_each_port(adap, i) {
  2855. struct net_device *dev = adap->port[i];
  2856. if (netif_running(dev)) {
  2857. link_start(dev);
  2858. cxgb_set_rxmode(dev);
  2859. }
  2860. netif_device_attach(dev);
  2861. }
  2862. rtnl_unlock();
  2863. }
  2864. static struct pci_error_handlers cxgb4_eeh = {
  2865. .error_detected = eeh_err_detected,
  2866. .slot_reset = eeh_slot_reset,
  2867. .resume = eeh_resume,
  2868. };
  2869. static inline bool is_10g_port(const struct link_config *lc)
  2870. {
  2871. return (lc->supported & FW_PORT_CAP_SPEED_10G) != 0;
  2872. }
  2873. static inline void init_rspq(struct sge_rspq *q, u8 timer_idx, u8 pkt_cnt_idx,
  2874. unsigned int size, unsigned int iqe_size)
  2875. {
  2876. q->intr_params = QINTR_TIMER_IDX(timer_idx) |
  2877. (pkt_cnt_idx < SGE_NCOUNTERS ? QINTR_CNT_EN : 0);
  2878. q->pktcnt_idx = pkt_cnt_idx < SGE_NCOUNTERS ? pkt_cnt_idx : 0;
  2879. q->iqe_len = iqe_size;
  2880. q->size = size;
  2881. }
  2882. /*
  2883. * Perform default configuration of DMA queues depending on the number and type
  2884. * of ports we found and the number of available CPUs. Most settings can be
  2885. * modified by the admin prior to actual use.
  2886. */
  2887. static void __devinit cfg_queues(struct adapter *adap)
  2888. {
  2889. struct sge *s = &adap->sge;
  2890. int i, q10g = 0, n10g = 0, qidx = 0;
  2891. for_each_port(adap, i)
  2892. n10g += is_10g_port(&adap2pinfo(adap, i)->link_cfg);
  2893. /*
  2894. * We default to 1 queue per non-10G port and up to # of cores queues
  2895. * per 10G port.
  2896. */
  2897. if (n10g)
  2898. q10g = (MAX_ETH_QSETS - (adap->params.nports - n10g)) / n10g;
  2899. if (q10g > num_online_cpus())
  2900. q10g = num_online_cpus();
  2901. for_each_port(adap, i) {
  2902. struct port_info *pi = adap2pinfo(adap, i);
  2903. pi->first_qset = qidx;
  2904. pi->nqsets = is_10g_port(&pi->link_cfg) ? q10g : 1;
  2905. qidx += pi->nqsets;
  2906. }
  2907. s->ethqsets = qidx;
  2908. s->max_ethqsets = qidx; /* MSI-X may lower it later */
  2909. if (is_offload(adap)) {
  2910. /*
  2911. * For offload we use 1 queue/channel if all ports are up to 1G,
  2912. * otherwise we divide all available queues amongst the channels
  2913. * capped by the number of available cores.
  2914. */
  2915. if (n10g) {
  2916. i = min_t(int, ARRAY_SIZE(s->ofldrxq),
  2917. num_online_cpus());
  2918. s->ofldqsets = roundup(i, adap->params.nports);
  2919. } else
  2920. s->ofldqsets = adap->params.nports;
  2921. /* For RDMA one Rx queue per channel suffices */
  2922. s->rdmaqs = adap->params.nports;
  2923. }
  2924. for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
  2925. struct sge_eth_rxq *r = &s->ethrxq[i];
  2926. init_rspq(&r->rspq, 0, 0, 1024, 64);
  2927. r->fl.size = 72;
  2928. }
  2929. for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
  2930. s->ethtxq[i].q.size = 1024;
  2931. for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++)
  2932. s->ctrlq[i].q.size = 512;
  2933. for (i = 0; i < ARRAY_SIZE(s->ofldtxq); i++)
  2934. s->ofldtxq[i].q.size = 1024;
  2935. for (i = 0; i < ARRAY_SIZE(s->ofldrxq); i++) {
  2936. struct sge_ofld_rxq *r = &s->ofldrxq[i];
  2937. init_rspq(&r->rspq, 0, 0, 1024, 64);
  2938. r->rspq.uld = CXGB4_ULD_ISCSI;
  2939. r->fl.size = 72;
  2940. }
  2941. for (i = 0; i < ARRAY_SIZE(s->rdmarxq); i++) {
  2942. struct sge_ofld_rxq *r = &s->rdmarxq[i];
  2943. init_rspq(&r->rspq, 0, 0, 511, 64);
  2944. r->rspq.uld = CXGB4_ULD_RDMA;
  2945. r->fl.size = 72;
  2946. }
  2947. init_rspq(&s->fw_evtq, 6, 0, 512, 64);
  2948. init_rspq(&s->intrq, 6, 0, 2 * MAX_INGQ, 64);
  2949. }
  2950. /*
  2951. * Reduce the number of Ethernet queues across all ports to at most n.
  2952. * n provides at least one queue per port.
  2953. */
  2954. static void __devinit reduce_ethqs(struct adapter *adap, int n)
  2955. {
  2956. int i;
  2957. struct port_info *pi;
  2958. while (n < adap->sge.ethqsets)
  2959. for_each_port(adap, i) {
  2960. pi = adap2pinfo(adap, i);
  2961. if (pi->nqsets > 1) {
  2962. pi->nqsets--;
  2963. adap->sge.ethqsets--;
  2964. if (adap->sge.ethqsets <= n)
  2965. break;
  2966. }
  2967. }
  2968. n = 0;
  2969. for_each_port(adap, i) {
  2970. pi = adap2pinfo(adap, i);
  2971. pi->first_qset = n;
  2972. n += pi->nqsets;
  2973. }
  2974. }
  2975. /* 2 MSI-X vectors needed for the FW queue and non-data interrupts */
  2976. #define EXTRA_VECS 2
  2977. static int __devinit enable_msix(struct adapter *adap)
  2978. {
  2979. int ofld_need = 0;
  2980. int i, err, want, need;
  2981. struct sge *s = &adap->sge;
  2982. unsigned int nchan = adap->params.nports;
  2983. struct msix_entry entries[MAX_INGQ + 1];
  2984. for (i = 0; i < ARRAY_SIZE(entries); ++i)
  2985. entries[i].entry = i;
  2986. want = s->max_ethqsets + EXTRA_VECS;
  2987. if (is_offload(adap)) {
  2988. want += s->rdmaqs + s->ofldqsets;
  2989. /* need nchan for each possible ULD */
  2990. ofld_need = 2 * nchan;
  2991. }
  2992. need = adap->params.nports + EXTRA_VECS + ofld_need;
  2993. while ((err = pci_enable_msix(adap->pdev, entries, want)) >= need)
  2994. want = err;
  2995. if (!err) {
  2996. /*
  2997. * Distribute available vectors to the various queue groups.
  2998. * Every group gets its minimum requirement and NIC gets top
  2999. * priority for leftovers.
  3000. */
  3001. i = want - EXTRA_VECS - ofld_need;
  3002. if (i < s->max_ethqsets) {
  3003. s->max_ethqsets = i;
  3004. if (i < s->ethqsets)
  3005. reduce_ethqs(adap, i);
  3006. }
  3007. if (is_offload(adap)) {
  3008. i = want - EXTRA_VECS - s->max_ethqsets;
  3009. i -= ofld_need - nchan;
  3010. s->ofldqsets = (i / nchan) * nchan; /* round down */
  3011. }
  3012. for (i = 0; i < want; ++i)
  3013. adap->msix_info[i].vec = entries[i].vector;
  3014. } else if (err > 0)
  3015. dev_info(adap->pdev_dev,
  3016. "only %d MSI-X vectors left, not using MSI-X\n", err);
  3017. return err;
  3018. }
  3019. #undef EXTRA_VECS
  3020. static int __devinit init_rss(struct adapter *adap)
  3021. {
  3022. unsigned int i, j;
  3023. for_each_port(adap, i) {
  3024. struct port_info *pi = adap2pinfo(adap, i);
  3025. pi->rss = kcalloc(pi->rss_size, sizeof(u16), GFP_KERNEL);
  3026. if (!pi->rss)
  3027. return -ENOMEM;
  3028. for (j = 0; j < pi->rss_size; j++)
  3029. pi->rss[j] = j % pi->nqsets;
  3030. }
  3031. return 0;
  3032. }
  3033. static void __devinit print_port_info(struct adapter *adap)
  3034. {
  3035. static const char *base[] = {
  3036. "R XFI", "R XAUI", "T SGMII", "T XFI", "T XAUI", "KX4", "CX4",
  3037. "KX", "KR", "KR SFP+", "KR FEC"
  3038. };
  3039. int i;
  3040. char buf[80];
  3041. const char *spd = "";
  3042. if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_2_5GB)
  3043. spd = " 2.5 GT/s";
  3044. else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_5_0GB)
  3045. spd = " 5 GT/s";
  3046. for_each_port(adap, i) {
  3047. struct net_device *dev = adap->port[i];
  3048. const struct port_info *pi = netdev_priv(dev);
  3049. char *bufp = buf;
  3050. if (!test_bit(i, &adap->registered_device_map))
  3051. continue;
  3052. if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_100M)
  3053. bufp += sprintf(bufp, "100/");
  3054. if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_1G)
  3055. bufp += sprintf(bufp, "1000/");
  3056. if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_10G)
  3057. bufp += sprintf(bufp, "10G/");
  3058. if (bufp != buf)
  3059. --bufp;
  3060. sprintf(bufp, "BASE-%s", base[pi->port_type]);
  3061. netdev_info(dev, "Chelsio %s rev %d %s %sNIC PCIe x%d%s%s\n",
  3062. adap->params.vpd.id, adap->params.rev,
  3063. buf, is_offload(adap) ? "R" : "",
  3064. adap->params.pci.width, spd,
  3065. (adap->flags & USING_MSIX) ? " MSI-X" :
  3066. (adap->flags & USING_MSI) ? " MSI" : "");
  3067. if (adap->name == dev->name)
  3068. netdev_info(dev, "S/N: %s, E/C: %s\n",
  3069. adap->params.vpd.sn, adap->params.vpd.ec);
  3070. }
  3071. }
  3072. /*
  3073. * Free the following resources:
  3074. * - memory used for tables
  3075. * - MSI/MSI-X
  3076. * - net devices
  3077. * - resources FW is holding for us
  3078. */
  3079. static void free_some_resources(struct adapter *adapter)
  3080. {
  3081. unsigned int i;
  3082. t4_free_mem(adapter->l2t);
  3083. t4_free_mem(adapter->tids.tid_tab);
  3084. disable_msi(adapter);
  3085. for_each_port(adapter, i)
  3086. if (adapter->port[i]) {
  3087. kfree(adap2pinfo(adapter, i)->rss);
  3088. free_netdev(adapter->port[i]);
  3089. }
  3090. if (adapter->flags & FW_OK)
  3091. t4_fw_bye(adapter, 0);
  3092. }
  3093. #define VLAN_FEAT (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO | NETIF_F_TSO6 |\
  3094. NETIF_F_IPV6_CSUM | NETIF_F_HIGHDMA)
  3095. static int __devinit init_one(struct pci_dev *pdev,
  3096. const struct pci_device_id *ent)
  3097. {
  3098. int func, i, err;
  3099. struct port_info *pi;
  3100. unsigned int highdma = 0;
  3101. struct adapter *adapter = NULL;
  3102. printk_once(KERN_INFO "%s - version %s\n", DRV_DESC, DRV_VERSION);
  3103. err = pci_request_regions(pdev, KBUILD_MODNAME);
  3104. if (err) {
  3105. /* Just info, some other driver may have claimed the device. */
  3106. dev_info(&pdev->dev, "cannot obtain PCI resources\n");
  3107. return err;
  3108. }
  3109. /* We control everything through PF 0 */
  3110. func = PCI_FUNC(pdev->devfn);
  3111. if (func > 0) {
  3112. pci_save_state(pdev); /* to restore SR-IOV later */
  3113. goto sriov;
  3114. }
  3115. err = pci_enable_device(pdev);
  3116. if (err) {
  3117. dev_err(&pdev->dev, "cannot enable PCI device\n");
  3118. goto out_release_regions;
  3119. }
  3120. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  3121. highdma = NETIF_F_HIGHDMA;
  3122. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  3123. if (err) {
  3124. dev_err(&pdev->dev, "unable to obtain 64-bit DMA for "
  3125. "coherent allocations\n");
  3126. goto out_disable_device;
  3127. }
  3128. } else {
  3129. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  3130. if (err) {
  3131. dev_err(&pdev->dev, "no usable DMA configuration\n");
  3132. goto out_disable_device;
  3133. }
  3134. }
  3135. pci_enable_pcie_error_reporting(pdev);
  3136. pci_set_master(pdev);
  3137. pci_save_state(pdev);
  3138. adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
  3139. if (!adapter) {
  3140. err = -ENOMEM;
  3141. goto out_disable_device;
  3142. }
  3143. adapter->regs = pci_ioremap_bar(pdev, 0);
  3144. if (!adapter->regs) {
  3145. dev_err(&pdev->dev, "cannot map device registers\n");
  3146. err = -ENOMEM;
  3147. goto out_free_adapter;
  3148. }
  3149. adapter->pdev = pdev;
  3150. adapter->pdev_dev = &pdev->dev;
  3151. adapter->name = pci_name(pdev);
  3152. adapter->msg_enable = dflt_msg_enable;
  3153. memset(adapter->chan_map, 0xff, sizeof(adapter->chan_map));
  3154. spin_lock_init(&adapter->stats_lock);
  3155. spin_lock_init(&adapter->tid_release_lock);
  3156. INIT_WORK(&adapter->tid_release_task, process_tid_release_list);
  3157. err = t4_prep_adapter(adapter);
  3158. if (err)
  3159. goto out_unmap_bar;
  3160. err = adap_init0(adapter);
  3161. if (err)
  3162. goto out_unmap_bar;
  3163. for_each_port(adapter, i) {
  3164. struct net_device *netdev;
  3165. netdev = alloc_etherdev_mq(sizeof(struct port_info),
  3166. MAX_ETH_QSETS);
  3167. if (!netdev) {
  3168. err = -ENOMEM;
  3169. goto out_free_dev;
  3170. }
  3171. SET_NETDEV_DEV(netdev, &pdev->dev);
  3172. adapter->port[i] = netdev;
  3173. pi = netdev_priv(netdev);
  3174. pi->adapter = adapter;
  3175. pi->xact_addr_filt = -1;
  3176. pi->rx_offload = RX_CSO;
  3177. pi->port_id = i;
  3178. netif_carrier_off(netdev);
  3179. netif_tx_stop_all_queues(netdev);
  3180. netdev->irq = pdev->irq;
  3181. netdev->features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6;
  3182. netdev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
  3183. netdev->features |= NETIF_F_GRO | NETIF_F_RXHASH | highdma;
  3184. netdev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  3185. netdev->vlan_features = netdev->features & VLAN_FEAT;
  3186. netdev->netdev_ops = &cxgb4_netdev_ops;
  3187. SET_ETHTOOL_OPS(netdev, &cxgb_ethtool_ops);
  3188. }
  3189. pci_set_drvdata(pdev, adapter);
  3190. if (adapter->flags & FW_OK) {
  3191. err = t4_port_init(adapter, 0, 0, 0);
  3192. if (err)
  3193. goto out_free_dev;
  3194. }
  3195. /*
  3196. * Configure queues and allocate tables now, they can be needed as
  3197. * soon as the first register_netdev completes.
  3198. */
  3199. cfg_queues(adapter);
  3200. adapter->l2t = t4_init_l2t();
  3201. if (!adapter->l2t) {
  3202. /* We tolerate a lack of L2T, giving up some functionality */
  3203. dev_warn(&pdev->dev, "could not allocate L2T, continuing\n");
  3204. adapter->params.offload = 0;
  3205. }
  3206. if (is_offload(adapter) && tid_init(&adapter->tids) < 0) {
  3207. dev_warn(&pdev->dev, "could not allocate TID table, "
  3208. "continuing\n");
  3209. adapter->params.offload = 0;
  3210. }
  3211. /* See what interrupts we'll be using */
  3212. if (msi > 1 && enable_msix(adapter) == 0)
  3213. adapter->flags |= USING_MSIX;
  3214. else if (msi > 0 && pci_enable_msi(pdev) == 0)
  3215. adapter->flags |= USING_MSI;
  3216. err = init_rss(adapter);
  3217. if (err)
  3218. goto out_free_dev;
  3219. /*
  3220. * The card is now ready to go. If any errors occur during device
  3221. * registration we do not fail the whole card but rather proceed only
  3222. * with the ports we manage to register successfully. However we must
  3223. * register at least one net device.
  3224. */
  3225. for_each_port(adapter, i) {
  3226. err = register_netdev(adapter->port[i]);
  3227. if (err)
  3228. dev_warn(&pdev->dev,
  3229. "cannot register net device %s, skipping\n",
  3230. adapter->port[i]->name);
  3231. else {
  3232. /*
  3233. * Change the name we use for messages to the name of
  3234. * the first successfully registered interface.
  3235. */
  3236. if (!adapter->registered_device_map)
  3237. adapter->name = adapter->port[i]->name;
  3238. __set_bit(i, &adapter->registered_device_map);
  3239. adapter->chan_map[adap2pinfo(adapter, i)->tx_chan] = i;
  3240. }
  3241. }
  3242. if (!adapter->registered_device_map) {
  3243. dev_err(&pdev->dev, "could not register any net devices\n");
  3244. goto out_free_dev;
  3245. }
  3246. if (cxgb4_debugfs_root) {
  3247. adapter->debugfs_root = debugfs_create_dir(pci_name(pdev),
  3248. cxgb4_debugfs_root);
  3249. setup_debugfs(adapter);
  3250. }
  3251. if (is_offload(adapter))
  3252. attach_ulds(adapter);
  3253. print_port_info(adapter);
  3254. sriov:
  3255. #ifdef CONFIG_PCI_IOV
  3256. if (func < ARRAY_SIZE(num_vf) && num_vf[func] > 0)
  3257. if (pci_enable_sriov(pdev, num_vf[func]) == 0)
  3258. dev_info(&pdev->dev,
  3259. "instantiated %u virtual functions\n",
  3260. num_vf[func]);
  3261. #endif
  3262. return 0;
  3263. out_free_dev:
  3264. free_some_resources(adapter);
  3265. out_unmap_bar:
  3266. iounmap(adapter->regs);
  3267. out_free_adapter:
  3268. kfree(adapter);
  3269. out_disable_device:
  3270. pci_disable_pcie_error_reporting(pdev);
  3271. pci_disable_device(pdev);
  3272. out_release_regions:
  3273. pci_release_regions(pdev);
  3274. pci_set_drvdata(pdev, NULL);
  3275. return err;
  3276. }
  3277. static void __devexit remove_one(struct pci_dev *pdev)
  3278. {
  3279. struct adapter *adapter = pci_get_drvdata(pdev);
  3280. pci_disable_sriov(pdev);
  3281. if (adapter) {
  3282. int i;
  3283. if (is_offload(adapter))
  3284. detach_ulds(adapter);
  3285. for_each_port(adapter, i)
  3286. if (test_bit(i, &adapter->registered_device_map))
  3287. unregister_netdev(adapter->port[i]);
  3288. if (adapter->debugfs_root)
  3289. debugfs_remove_recursive(adapter->debugfs_root);
  3290. if (adapter->flags & FULL_INIT_DONE)
  3291. cxgb_down(adapter);
  3292. free_some_resources(adapter);
  3293. iounmap(adapter->regs);
  3294. kfree(adapter);
  3295. pci_disable_pcie_error_reporting(pdev);
  3296. pci_disable_device(pdev);
  3297. pci_release_regions(pdev);
  3298. pci_set_drvdata(pdev, NULL);
  3299. } else if (PCI_FUNC(pdev->devfn) > 0)
  3300. pci_release_regions(pdev);
  3301. }
  3302. static struct pci_driver cxgb4_driver = {
  3303. .name = KBUILD_MODNAME,
  3304. .id_table = cxgb4_pci_tbl,
  3305. .probe = init_one,
  3306. .remove = __devexit_p(remove_one),
  3307. .err_handler = &cxgb4_eeh,
  3308. };
  3309. static int __init cxgb4_init_module(void)
  3310. {
  3311. int ret;
  3312. /* Debugfs support is optional, just warn if this fails */
  3313. cxgb4_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
  3314. if (!cxgb4_debugfs_root)
  3315. pr_warning("could not create debugfs entry, continuing\n");
  3316. ret = pci_register_driver(&cxgb4_driver);
  3317. if (ret < 0)
  3318. debugfs_remove(cxgb4_debugfs_root);
  3319. return ret;
  3320. }
  3321. static void __exit cxgb4_cleanup_module(void)
  3322. {
  3323. pci_unregister_driver(&cxgb4_driver);
  3324. debugfs_remove(cxgb4_debugfs_root); /* NULL ok */
  3325. }
  3326. module_init(cxgb4_init_module);
  3327. module_exit(cxgb4_cleanup_module);