cxgb3_main.c 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437
  1. /*
  2. * Copyright (c) 2003-2008 Chelsio, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  33. #include <linux/module.h>
  34. #include <linux/moduleparam.h>
  35. #include <linux/init.h>
  36. #include <linux/pci.h>
  37. #include <linux/dma-mapping.h>
  38. #include <linux/netdevice.h>
  39. #include <linux/etherdevice.h>
  40. #include <linux/if_vlan.h>
  41. #include <linux/mdio.h>
  42. #include <linux/sockios.h>
  43. #include <linux/workqueue.h>
  44. #include <linux/proc_fs.h>
  45. #include <linux/rtnetlink.h>
  46. #include <linux/firmware.h>
  47. #include <linux/log2.h>
  48. #include <linux/stringify.h>
  49. #include <linux/sched.h>
  50. #include <linux/slab.h>
  51. #include <linux/nospec.h>
  52. #include <asm/uaccess.h>
  53. #include "common.h"
  54. #include "cxgb3_ioctl.h"
  55. #include "regs.h"
  56. #include "cxgb3_offload.h"
  57. #include "version.h"
  58. #include "cxgb3_ctl_defs.h"
  59. #include "t3_cpl.h"
  60. #include "firmware_exports.h"
  61. enum {
  62. MAX_TXQ_ENTRIES = 16384,
  63. MAX_CTRL_TXQ_ENTRIES = 1024,
  64. MAX_RSPQ_ENTRIES = 16384,
  65. MAX_RX_BUFFERS = 16384,
  66. MAX_RX_JUMBO_BUFFERS = 16384,
  67. MIN_TXQ_ENTRIES = 4,
  68. MIN_CTRL_TXQ_ENTRIES = 4,
  69. MIN_RSPQ_ENTRIES = 32,
  70. MIN_FL_ENTRIES = 32
  71. };
  72. #define PORT_MASK ((1 << MAX_NPORTS) - 1)
  73. #define DFLT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK | \
  74. NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP |\
  75. NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)
  76. #define EEPROM_MAGIC 0x38E2F10C
  77. #define CH_DEVICE(devid, idx) \
  78. { PCI_VENDOR_ID_CHELSIO, devid, PCI_ANY_ID, PCI_ANY_ID, 0, 0, idx }
  79. static const struct pci_device_id cxgb3_pci_tbl[] = {
  80. CH_DEVICE(0x20, 0), /* PE9000 */
  81. CH_DEVICE(0x21, 1), /* T302E */
  82. CH_DEVICE(0x22, 2), /* T310E */
  83. CH_DEVICE(0x23, 3), /* T320X */
  84. CH_DEVICE(0x24, 1), /* T302X */
  85. CH_DEVICE(0x25, 3), /* T320E */
  86. CH_DEVICE(0x26, 2), /* T310X */
  87. CH_DEVICE(0x30, 2), /* T3B10 */
  88. CH_DEVICE(0x31, 3), /* T3B20 */
  89. CH_DEVICE(0x32, 1), /* T3B02 */
  90. CH_DEVICE(0x35, 6), /* T3C20-derived T3C10 */
  91. CH_DEVICE(0x36, 3), /* S320E-CR */
  92. CH_DEVICE(0x37, 7), /* N320E-G2 */
  93. {0,}
  94. };
  95. MODULE_DESCRIPTION(DRV_DESC);
  96. MODULE_AUTHOR("Chelsio Communications");
  97. MODULE_LICENSE("Dual BSD/GPL");
  98. MODULE_VERSION(DRV_VERSION);
  99. MODULE_DEVICE_TABLE(pci, cxgb3_pci_tbl);
  100. static int dflt_msg_enable = DFLT_MSG_ENABLE;
  101. module_param(dflt_msg_enable, int, 0644);
  102. MODULE_PARM_DESC(dflt_msg_enable, "Chelsio T3 default message enable bitmap");
  103. /*
  104. * The driver uses the best interrupt scheme available on a platform in the
  105. * order MSI-X, MSI, legacy pin interrupts. This parameter determines which
  106. * of these schemes the driver may consider as follows:
  107. *
  108. * msi = 2: choose from among all three options
  109. * msi = 1: only consider MSI and pin interrupts
  110. * msi = 0: force pin interrupts
  111. */
  112. static int msi = 2;
  113. module_param(msi, int, 0644);
  114. MODULE_PARM_DESC(msi, "whether to use MSI or MSI-X");
  115. /*
  116. * The driver enables offload as a default.
  117. * To disable it, use ofld_disable = 1.
  118. */
  119. static int ofld_disable = 0;
  120. module_param(ofld_disable, int, 0644);
  121. MODULE_PARM_DESC(ofld_disable, "whether to enable offload at init time or not");
  122. /*
  123. * We have work elements that we need to cancel when an interface is taken
  124. * down. Normally the work elements would be executed by keventd but that
  125. * can deadlock because of linkwatch. If our close method takes the rtnl
  126. * lock and linkwatch is ahead of our work elements in keventd, linkwatch
  127. * will block keventd as it needs the rtnl lock, and we'll deadlock waiting
  128. * for our work to complete. Get our own work queue to solve this.
  129. */
  130. struct workqueue_struct *cxgb3_wq;
  131. /**
  132. * link_report - show link status and link speed/duplex
  133. * @p: the port whose settings are to be reported
  134. *
  135. * Shows the link status, speed, and duplex of a port.
  136. */
  137. static void link_report(struct net_device *dev)
  138. {
  139. if (!netif_carrier_ok(dev))
  140. netdev_info(dev, "link down\n");
  141. else {
  142. const char *s = "10Mbps";
  143. const struct port_info *p = netdev_priv(dev);
  144. switch (p->link_config.speed) {
  145. case SPEED_10000:
  146. s = "10Gbps";
  147. break;
  148. case SPEED_1000:
  149. s = "1000Mbps";
  150. break;
  151. case SPEED_100:
  152. s = "100Mbps";
  153. break;
  154. }
  155. netdev_info(dev, "link up, %s, %s-duplex\n",
  156. s, p->link_config.duplex == DUPLEX_FULL
  157. ? "full" : "half");
  158. }
  159. }
  160. static void enable_tx_fifo_drain(struct adapter *adapter,
  161. struct port_info *pi)
  162. {
  163. t3_set_reg_field(adapter, A_XGM_TXFIFO_CFG + pi->mac.offset, 0,
  164. F_ENDROPPKT);
  165. t3_write_reg(adapter, A_XGM_RX_CTRL + pi->mac.offset, 0);
  166. t3_write_reg(adapter, A_XGM_TX_CTRL + pi->mac.offset, F_TXEN);
  167. t3_write_reg(adapter, A_XGM_RX_CTRL + pi->mac.offset, F_RXEN);
  168. }
  169. static void disable_tx_fifo_drain(struct adapter *adapter,
  170. struct port_info *pi)
  171. {
  172. t3_set_reg_field(adapter, A_XGM_TXFIFO_CFG + pi->mac.offset,
  173. F_ENDROPPKT, 0);
  174. }
  175. void t3_os_link_fault(struct adapter *adap, int port_id, int state)
  176. {
  177. struct net_device *dev = adap->port[port_id];
  178. struct port_info *pi = netdev_priv(dev);
  179. if (state == netif_carrier_ok(dev))
  180. return;
  181. if (state) {
  182. struct cmac *mac = &pi->mac;
  183. netif_carrier_on(dev);
  184. disable_tx_fifo_drain(adap, pi);
  185. /* Clear local faults */
  186. t3_xgm_intr_disable(adap, pi->port_id);
  187. t3_read_reg(adap, A_XGM_INT_STATUS +
  188. pi->mac.offset);
  189. t3_write_reg(adap,
  190. A_XGM_INT_CAUSE + pi->mac.offset,
  191. F_XGM_INT);
  192. t3_set_reg_field(adap,
  193. A_XGM_INT_ENABLE +
  194. pi->mac.offset,
  195. F_XGM_INT, F_XGM_INT);
  196. t3_xgm_intr_enable(adap, pi->port_id);
  197. t3_mac_enable(mac, MAC_DIRECTION_TX);
  198. } else {
  199. netif_carrier_off(dev);
  200. /* Flush TX FIFO */
  201. enable_tx_fifo_drain(adap, pi);
  202. }
  203. link_report(dev);
  204. }
  205. /**
  206. * t3_os_link_changed - handle link status changes
  207. * @adapter: the adapter associated with the link change
  208. * @port_id: the port index whose limk status has changed
  209. * @link_stat: the new status of the link
  210. * @speed: the new speed setting
  211. * @duplex: the new duplex setting
  212. * @pause: the new flow-control setting
  213. *
  214. * This is the OS-dependent handler for link status changes. The OS
  215. * neutral handler takes care of most of the processing for these events,
  216. * then calls this handler for any OS-specific processing.
  217. */
  218. void t3_os_link_changed(struct adapter *adapter, int port_id, int link_stat,
  219. int speed, int duplex, int pause)
  220. {
  221. struct net_device *dev = adapter->port[port_id];
  222. struct port_info *pi = netdev_priv(dev);
  223. struct cmac *mac = &pi->mac;
  224. /* Skip changes from disabled ports. */
  225. if (!netif_running(dev))
  226. return;
  227. if (link_stat != netif_carrier_ok(dev)) {
  228. if (link_stat) {
  229. disable_tx_fifo_drain(adapter, pi);
  230. t3_mac_enable(mac, MAC_DIRECTION_RX);
  231. /* Clear local faults */
  232. t3_xgm_intr_disable(adapter, pi->port_id);
  233. t3_read_reg(adapter, A_XGM_INT_STATUS +
  234. pi->mac.offset);
  235. t3_write_reg(adapter,
  236. A_XGM_INT_CAUSE + pi->mac.offset,
  237. F_XGM_INT);
  238. t3_set_reg_field(adapter,
  239. A_XGM_INT_ENABLE + pi->mac.offset,
  240. F_XGM_INT, F_XGM_INT);
  241. t3_xgm_intr_enable(adapter, pi->port_id);
  242. netif_carrier_on(dev);
  243. } else {
  244. netif_carrier_off(dev);
  245. t3_xgm_intr_disable(adapter, pi->port_id);
  246. t3_read_reg(adapter, A_XGM_INT_STATUS + pi->mac.offset);
  247. t3_set_reg_field(adapter,
  248. A_XGM_INT_ENABLE + pi->mac.offset,
  249. F_XGM_INT, 0);
  250. if (is_10G(adapter))
  251. pi->phy.ops->power_down(&pi->phy, 1);
  252. t3_read_reg(adapter, A_XGM_INT_STATUS + pi->mac.offset);
  253. t3_mac_disable(mac, MAC_DIRECTION_RX);
  254. t3_link_start(&pi->phy, mac, &pi->link_config);
  255. /* Flush TX FIFO */
  256. enable_tx_fifo_drain(adapter, pi);
  257. }
  258. link_report(dev);
  259. }
  260. }
  261. /**
  262. * t3_os_phymod_changed - handle PHY module changes
  263. * @phy: the PHY reporting the module change
  264. * @mod_type: new module type
  265. *
  266. * This is the OS-dependent handler for PHY module changes. It is
  267. * invoked when a PHY module is removed or inserted for any OS-specific
  268. * processing.
  269. */
  270. void t3_os_phymod_changed(struct adapter *adap, int port_id)
  271. {
  272. static const char *mod_str[] = {
  273. NULL, "SR", "LR", "LRM", "TWINAX", "TWINAX", "unknown"
  274. };
  275. const struct net_device *dev = adap->port[port_id];
  276. const struct port_info *pi = netdev_priv(dev);
  277. if (pi->phy.modtype == phy_modtype_none)
  278. netdev_info(dev, "PHY module unplugged\n");
  279. else
  280. netdev_info(dev, "%s PHY module inserted\n",
  281. mod_str[pi->phy.modtype]);
  282. }
  283. static void cxgb_set_rxmode(struct net_device *dev)
  284. {
  285. struct port_info *pi = netdev_priv(dev);
  286. t3_mac_set_rx_mode(&pi->mac, dev);
  287. }
  288. /**
  289. * link_start - enable a port
  290. * @dev: the device to enable
  291. *
  292. * Performs the MAC and PHY actions needed to enable a port.
  293. */
  294. static void link_start(struct net_device *dev)
  295. {
  296. struct port_info *pi = netdev_priv(dev);
  297. struct cmac *mac = &pi->mac;
  298. t3_mac_reset(mac);
  299. t3_mac_set_num_ucast(mac, MAX_MAC_IDX);
  300. t3_mac_set_mtu(mac, dev->mtu);
  301. t3_mac_set_address(mac, LAN_MAC_IDX, dev->dev_addr);
  302. t3_mac_set_address(mac, SAN_MAC_IDX, pi->iscsic.mac_addr);
  303. t3_mac_set_rx_mode(mac, dev);
  304. t3_link_start(&pi->phy, mac, &pi->link_config);
  305. t3_mac_enable(mac, MAC_DIRECTION_RX | MAC_DIRECTION_TX);
  306. }
  307. static inline void cxgb_disable_msi(struct adapter *adapter)
  308. {
  309. if (adapter->flags & USING_MSIX) {
  310. pci_disable_msix(adapter->pdev);
  311. adapter->flags &= ~USING_MSIX;
  312. } else if (adapter->flags & USING_MSI) {
  313. pci_disable_msi(adapter->pdev);
  314. adapter->flags &= ~USING_MSI;
  315. }
  316. }
  317. /*
  318. * Interrupt handler for asynchronous events used with MSI-X.
  319. */
  320. static irqreturn_t t3_async_intr_handler(int irq, void *cookie)
  321. {
  322. t3_slow_intr_handler(cookie);
  323. return IRQ_HANDLED;
  324. }
  325. /*
  326. * Name the MSI-X interrupts.
  327. */
  328. static void name_msix_vecs(struct adapter *adap)
  329. {
  330. int i, j, msi_idx = 1, n = sizeof(adap->msix_info[0].desc) - 1;
  331. snprintf(adap->msix_info[0].desc, n, "%s", adap->name);
  332. adap->msix_info[0].desc[n] = 0;
  333. for_each_port(adap, j) {
  334. struct net_device *d = adap->port[j];
  335. const struct port_info *pi = netdev_priv(d);
  336. for (i = 0; i < pi->nqsets; i++, msi_idx++) {
  337. snprintf(adap->msix_info[msi_idx].desc, n,
  338. "%s-%d", d->name, pi->first_qset + i);
  339. adap->msix_info[msi_idx].desc[n] = 0;
  340. }
  341. }
  342. }
  343. static int request_msix_data_irqs(struct adapter *adap)
  344. {
  345. int i, j, err, qidx = 0;
  346. for_each_port(adap, i) {
  347. int nqsets = adap2pinfo(adap, i)->nqsets;
  348. for (j = 0; j < nqsets; ++j) {
  349. err = request_irq(adap->msix_info[qidx + 1].vec,
  350. t3_intr_handler(adap,
  351. adap->sge.qs[qidx].
  352. rspq.polling), 0,
  353. adap->msix_info[qidx + 1].desc,
  354. &adap->sge.qs[qidx]);
  355. if (err) {
  356. while (--qidx >= 0)
  357. free_irq(adap->msix_info[qidx + 1].vec,
  358. &adap->sge.qs[qidx]);
  359. return err;
  360. }
  361. qidx++;
  362. }
  363. }
  364. return 0;
  365. }
  366. static void free_irq_resources(struct adapter *adapter)
  367. {
  368. if (adapter->flags & USING_MSIX) {
  369. int i, n = 0;
  370. free_irq(adapter->msix_info[0].vec, adapter);
  371. for_each_port(adapter, i)
  372. n += adap2pinfo(adapter, i)->nqsets;
  373. for (i = 0; i < n; ++i)
  374. free_irq(adapter->msix_info[i + 1].vec,
  375. &adapter->sge.qs[i]);
  376. } else
  377. free_irq(adapter->pdev->irq, adapter);
  378. }
  379. static int await_mgmt_replies(struct adapter *adap, unsigned long init_cnt,
  380. unsigned long n)
  381. {
  382. int attempts = 10;
  383. while (adap->sge.qs[0].rspq.offload_pkts < init_cnt + n) {
  384. if (!--attempts)
  385. return -ETIMEDOUT;
  386. msleep(10);
  387. }
  388. return 0;
  389. }
  390. static int init_tp_parity(struct adapter *adap)
  391. {
  392. int i;
  393. struct sk_buff *skb;
  394. struct cpl_set_tcb_field *greq;
  395. unsigned long cnt = adap->sge.qs[0].rspq.offload_pkts;
  396. t3_tp_set_offload_mode(adap, 1);
  397. for (i = 0; i < 16; i++) {
  398. struct cpl_smt_write_req *req;
  399. skb = alloc_skb(sizeof(*req), GFP_KERNEL);
  400. if (!skb)
  401. skb = adap->nofail_skb;
  402. if (!skb)
  403. goto alloc_skb_fail;
  404. req = (struct cpl_smt_write_req *)__skb_put(skb, sizeof(*req));
  405. memset(req, 0, sizeof(*req));
  406. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
  407. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SMT_WRITE_REQ, i));
  408. req->mtu_idx = NMTUS - 1;
  409. req->iff = i;
  410. t3_mgmt_tx(adap, skb);
  411. if (skb == adap->nofail_skb) {
  412. await_mgmt_replies(adap, cnt, i + 1);
  413. adap->nofail_skb = alloc_skb(sizeof(*greq), GFP_KERNEL);
  414. if (!adap->nofail_skb)
  415. goto alloc_skb_fail;
  416. }
  417. }
  418. for (i = 0; i < 2048; i++) {
  419. struct cpl_l2t_write_req *req;
  420. skb = alloc_skb(sizeof(*req), GFP_KERNEL);
  421. if (!skb)
  422. skb = adap->nofail_skb;
  423. if (!skb)
  424. goto alloc_skb_fail;
  425. req = (struct cpl_l2t_write_req *)__skb_put(skb, sizeof(*req));
  426. memset(req, 0, sizeof(*req));
  427. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
  428. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_L2T_WRITE_REQ, i));
  429. req->params = htonl(V_L2T_W_IDX(i));
  430. t3_mgmt_tx(adap, skb);
  431. if (skb == adap->nofail_skb) {
  432. await_mgmt_replies(adap, cnt, 16 + i + 1);
  433. adap->nofail_skb = alloc_skb(sizeof(*greq), GFP_KERNEL);
  434. if (!adap->nofail_skb)
  435. goto alloc_skb_fail;
  436. }
  437. }
  438. for (i = 0; i < 2048; i++) {
  439. struct cpl_rte_write_req *req;
  440. skb = alloc_skb(sizeof(*req), GFP_KERNEL);
  441. if (!skb)
  442. skb = adap->nofail_skb;
  443. if (!skb)
  444. goto alloc_skb_fail;
  445. req = (struct cpl_rte_write_req *)__skb_put(skb, sizeof(*req));
  446. memset(req, 0, sizeof(*req));
  447. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
  448. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_RTE_WRITE_REQ, i));
  449. req->l2t_idx = htonl(V_L2T_W_IDX(i));
  450. t3_mgmt_tx(adap, skb);
  451. if (skb == adap->nofail_skb) {
  452. await_mgmt_replies(adap, cnt, 16 + 2048 + i + 1);
  453. adap->nofail_skb = alloc_skb(sizeof(*greq), GFP_KERNEL);
  454. if (!adap->nofail_skb)
  455. goto alloc_skb_fail;
  456. }
  457. }
  458. skb = alloc_skb(sizeof(*greq), GFP_KERNEL);
  459. if (!skb)
  460. skb = adap->nofail_skb;
  461. if (!skb)
  462. goto alloc_skb_fail;
  463. greq = (struct cpl_set_tcb_field *)__skb_put(skb, sizeof(*greq));
  464. memset(greq, 0, sizeof(*greq));
  465. greq->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
  466. OPCODE_TID(greq) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, 0));
  467. greq->mask = cpu_to_be64(1);
  468. t3_mgmt_tx(adap, skb);
  469. i = await_mgmt_replies(adap, cnt, 16 + 2048 + 2048 + 1);
  470. if (skb == adap->nofail_skb) {
  471. i = await_mgmt_replies(adap, cnt, 16 + 2048 + 2048 + 1);
  472. adap->nofail_skb = alloc_skb(sizeof(*greq), GFP_KERNEL);
  473. }
  474. t3_tp_set_offload_mode(adap, 0);
  475. return i;
  476. alloc_skb_fail:
  477. t3_tp_set_offload_mode(adap, 0);
  478. return -ENOMEM;
  479. }
  480. /**
  481. * setup_rss - configure RSS
  482. * @adap: the adapter
  483. *
  484. * Sets up RSS to distribute packets to multiple receive queues. We
  485. * configure the RSS CPU lookup table to distribute to the number of HW
  486. * receive queues, and the response queue lookup table to narrow that
  487. * down to the response queues actually configured for each port.
  488. * We always configure the RSS mapping for two ports since the mapping
  489. * table has plenty of entries.
  490. */
  491. static void setup_rss(struct adapter *adap)
  492. {
  493. int i;
  494. unsigned int nq0 = adap2pinfo(adap, 0)->nqsets;
  495. unsigned int nq1 = adap->port[1] ? adap2pinfo(adap, 1)->nqsets : 1;
  496. u8 cpus[SGE_QSETS + 1];
  497. u16 rspq_map[RSS_TABLE_SIZE + 1];
  498. for (i = 0; i < SGE_QSETS; ++i)
  499. cpus[i] = i;
  500. cpus[SGE_QSETS] = 0xff; /* terminator */
  501. for (i = 0; i < RSS_TABLE_SIZE / 2; ++i) {
  502. rspq_map[i] = i % nq0;
  503. rspq_map[i + RSS_TABLE_SIZE / 2] = (i % nq1) + nq0;
  504. }
  505. rspq_map[RSS_TABLE_SIZE] = 0xffff; /* terminator */
  506. t3_config_rss(adap, F_RQFEEDBACKENABLE | F_TNLLKPEN | F_TNLMAPEN |
  507. F_TNLPRTEN | F_TNL2TUPEN | F_TNL4TUPEN |
  508. V_RRCPLCPUSIZE(6) | F_HASHTOEPLITZ, cpus, rspq_map);
  509. }
  510. static void ring_dbs(struct adapter *adap)
  511. {
  512. int i, j;
  513. for (i = 0; i < SGE_QSETS; i++) {
  514. struct sge_qset *qs = &adap->sge.qs[i];
  515. if (qs->adap)
  516. for (j = 0; j < SGE_TXQ_PER_SET; j++)
  517. t3_write_reg(adap, A_SG_KDOORBELL, F_SELEGRCNTX | V_EGRCNTX(qs->txq[j].cntxt_id));
  518. }
  519. }
  520. static void init_napi(struct adapter *adap)
  521. {
  522. int i;
  523. for (i = 0; i < SGE_QSETS; i++) {
  524. struct sge_qset *qs = &adap->sge.qs[i];
  525. if (qs->adap)
  526. netif_napi_add(qs->netdev, &qs->napi, qs->napi.poll,
  527. 64);
  528. }
  529. /*
  530. * netif_napi_add() can be called only once per napi_struct because it
  531. * adds each new napi_struct to a list. Be careful not to call it a
  532. * second time, e.g., during EEH recovery, by making a note of it.
  533. */
  534. adap->flags |= NAPI_INIT;
  535. }
  536. /*
  537. * Wait until all NAPI handlers are descheduled. This includes the handlers of
  538. * both netdevices representing interfaces and the dummy ones for the extra
  539. * queues.
  540. */
  541. static void quiesce_rx(struct adapter *adap)
  542. {
  543. int i;
  544. for (i = 0; i < SGE_QSETS; i++)
  545. if (adap->sge.qs[i].adap)
  546. napi_disable(&adap->sge.qs[i].napi);
  547. }
  548. static void enable_all_napi(struct adapter *adap)
  549. {
  550. int i;
  551. for (i = 0; i < SGE_QSETS; i++)
  552. if (adap->sge.qs[i].adap)
  553. napi_enable(&adap->sge.qs[i].napi);
  554. }
  555. /**
  556. * setup_sge_qsets - configure SGE Tx/Rx/response queues
  557. * @adap: the adapter
  558. *
  559. * Determines how many sets of SGE queues to use and initializes them.
  560. * We support multiple queue sets per port if we have MSI-X, otherwise
  561. * just one queue set per port.
  562. */
  563. static int setup_sge_qsets(struct adapter *adap)
  564. {
  565. int i, j, err, irq_idx = 0, qset_idx = 0;
  566. unsigned int ntxq = SGE_TXQ_PER_SET;
  567. if (adap->params.rev > 0 && !(adap->flags & USING_MSI))
  568. irq_idx = -1;
  569. for_each_port(adap, i) {
  570. struct net_device *dev = adap->port[i];
  571. struct port_info *pi = netdev_priv(dev);
  572. pi->qs = &adap->sge.qs[pi->first_qset];
  573. for (j = 0; j < pi->nqsets; ++j, ++qset_idx) {
  574. err = t3_sge_alloc_qset(adap, qset_idx, 1,
  575. (adap->flags & USING_MSIX) ? qset_idx + 1 :
  576. irq_idx,
  577. &adap->params.sge.qset[qset_idx], ntxq, dev,
  578. netdev_get_tx_queue(dev, j));
  579. if (err) {
  580. t3_free_sge_resources(adap);
  581. return err;
  582. }
  583. }
  584. }
  585. return 0;
  586. }
  587. static ssize_t attr_show(struct device *d, char *buf,
  588. ssize_t(*format) (struct net_device *, char *))
  589. {
  590. ssize_t len;
  591. /* Synchronize with ioctls that may shut down the device */
  592. rtnl_lock();
  593. len = (*format) (to_net_dev(d), buf);
  594. rtnl_unlock();
  595. return len;
  596. }
  597. static ssize_t attr_store(struct device *d,
  598. const char *buf, size_t len,
  599. ssize_t(*set) (struct net_device *, unsigned int),
  600. unsigned int min_val, unsigned int max_val)
  601. {
  602. ssize_t ret;
  603. unsigned int val;
  604. if (!capable(CAP_NET_ADMIN))
  605. return -EPERM;
  606. ret = kstrtouint(buf, 0, &val);
  607. if (ret)
  608. return ret;
  609. if (val < min_val || val > max_val)
  610. return -EINVAL;
  611. rtnl_lock();
  612. ret = (*set) (to_net_dev(d), val);
  613. if (!ret)
  614. ret = len;
  615. rtnl_unlock();
  616. return ret;
  617. }
  618. #define CXGB3_SHOW(name, val_expr) \
  619. static ssize_t format_##name(struct net_device *dev, char *buf) \
  620. { \
  621. struct port_info *pi = netdev_priv(dev); \
  622. struct adapter *adap = pi->adapter; \
  623. return sprintf(buf, "%u\n", val_expr); \
  624. } \
  625. static ssize_t show_##name(struct device *d, struct device_attribute *attr, \
  626. char *buf) \
  627. { \
  628. return attr_show(d, buf, format_##name); \
  629. }
  630. static ssize_t set_nfilters(struct net_device *dev, unsigned int val)
  631. {
  632. struct port_info *pi = netdev_priv(dev);
  633. struct adapter *adap = pi->adapter;
  634. int min_tids = is_offload(adap) ? MC5_MIN_TIDS : 0;
  635. if (adap->flags & FULL_INIT_DONE)
  636. return -EBUSY;
  637. if (val && adap->params.rev == 0)
  638. return -EINVAL;
  639. if (val > t3_mc5_size(&adap->mc5) - adap->params.mc5.nservers -
  640. min_tids)
  641. return -EINVAL;
  642. adap->params.mc5.nfilters = val;
  643. return 0;
  644. }
  645. static ssize_t store_nfilters(struct device *d, struct device_attribute *attr,
  646. const char *buf, size_t len)
  647. {
  648. return attr_store(d, buf, len, set_nfilters, 0, ~0);
  649. }
  650. static ssize_t set_nservers(struct net_device *dev, unsigned int val)
  651. {
  652. struct port_info *pi = netdev_priv(dev);
  653. struct adapter *adap = pi->adapter;
  654. if (adap->flags & FULL_INIT_DONE)
  655. return -EBUSY;
  656. if (val > t3_mc5_size(&adap->mc5) - adap->params.mc5.nfilters -
  657. MC5_MIN_TIDS)
  658. return -EINVAL;
  659. adap->params.mc5.nservers = val;
  660. return 0;
  661. }
  662. static ssize_t store_nservers(struct device *d, struct device_attribute *attr,
  663. const char *buf, size_t len)
  664. {
  665. return attr_store(d, buf, len, set_nservers, 0, ~0);
  666. }
  667. #define CXGB3_ATTR_R(name, val_expr) \
  668. CXGB3_SHOW(name, val_expr) \
  669. static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
  670. #define CXGB3_ATTR_RW(name, val_expr, store_method) \
  671. CXGB3_SHOW(name, val_expr) \
  672. static DEVICE_ATTR(name, S_IRUGO | S_IWUSR, show_##name, store_method)
  673. CXGB3_ATTR_R(cam_size, t3_mc5_size(&adap->mc5));
  674. CXGB3_ATTR_RW(nfilters, adap->params.mc5.nfilters, store_nfilters);
  675. CXGB3_ATTR_RW(nservers, adap->params.mc5.nservers, store_nservers);
  676. static struct attribute *cxgb3_attrs[] = {
  677. &dev_attr_cam_size.attr,
  678. &dev_attr_nfilters.attr,
  679. &dev_attr_nservers.attr,
  680. NULL
  681. };
  682. static struct attribute_group cxgb3_attr_group = {.attrs = cxgb3_attrs };
  683. static ssize_t tm_attr_show(struct device *d,
  684. char *buf, int sched)
  685. {
  686. struct port_info *pi = netdev_priv(to_net_dev(d));
  687. struct adapter *adap = pi->adapter;
  688. unsigned int v, addr, bpt, cpt;
  689. ssize_t len;
  690. addr = A_TP_TX_MOD_Q1_Q0_RATE_LIMIT - sched / 2;
  691. rtnl_lock();
  692. t3_write_reg(adap, A_TP_TM_PIO_ADDR, addr);
  693. v = t3_read_reg(adap, A_TP_TM_PIO_DATA);
  694. if (sched & 1)
  695. v >>= 16;
  696. bpt = (v >> 8) & 0xff;
  697. cpt = v & 0xff;
  698. if (!cpt)
  699. len = sprintf(buf, "disabled\n");
  700. else {
  701. v = (adap->params.vpd.cclk * 1000) / cpt;
  702. len = sprintf(buf, "%u Kbps\n", (v * bpt) / 125);
  703. }
  704. rtnl_unlock();
  705. return len;
  706. }
  707. static ssize_t tm_attr_store(struct device *d,
  708. const char *buf, size_t len, int sched)
  709. {
  710. struct port_info *pi = netdev_priv(to_net_dev(d));
  711. struct adapter *adap = pi->adapter;
  712. unsigned int val;
  713. ssize_t ret;
  714. if (!capable(CAP_NET_ADMIN))
  715. return -EPERM;
  716. ret = kstrtouint(buf, 0, &val);
  717. if (ret)
  718. return ret;
  719. if (val > 10000000)
  720. return -EINVAL;
  721. rtnl_lock();
  722. ret = t3_config_sched(adap, val, sched);
  723. if (!ret)
  724. ret = len;
  725. rtnl_unlock();
  726. return ret;
  727. }
  728. #define TM_ATTR(name, sched) \
  729. static ssize_t show_##name(struct device *d, struct device_attribute *attr, \
  730. char *buf) \
  731. { \
  732. return tm_attr_show(d, buf, sched); \
  733. } \
  734. static ssize_t store_##name(struct device *d, struct device_attribute *attr, \
  735. const char *buf, size_t len) \
  736. { \
  737. return tm_attr_store(d, buf, len, sched); \
  738. } \
  739. static DEVICE_ATTR(name, S_IRUGO | S_IWUSR, show_##name, store_##name)
  740. TM_ATTR(sched0, 0);
  741. TM_ATTR(sched1, 1);
  742. TM_ATTR(sched2, 2);
  743. TM_ATTR(sched3, 3);
  744. TM_ATTR(sched4, 4);
  745. TM_ATTR(sched5, 5);
  746. TM_ATTR(sched6, 6);
  747. TM_ATTR(sched7, 7);
  748. static struct attribute *offload_attrs[] = {
  749. &dev_attr_sched0.attr,
  750. &dev_attr_sched1.attr,
  751. &dev_attr_sched2.attr,
  752. &dev_attr_sched3.attr,
  753. &dev_attr_sched4.attr,
  754. &dev_attr_sched5.attr,
  755. &dev_attr_sched6.attr,
  756. &dev_attr_sched7.attr,
  757. NULL
  758. };
  759. static struct attribute_group offload_attr_group = {.attrs = offload_attrs };
  760. /*
  761. * Sends an sk_buff to an offload queue driver
  762. * after dealing with any active network taps.
  763. */
  764. static inline int offload_tx(struct t3cdev *tdev, struct sk_buff *skb)
  765. {
  766. int ret;
  767. local_bh_disable();
  768. ret = t3_offload_tx(tdev, skb);
  769. local_bh_enable();
  770. return ret;
  771. }
  772. static int write_smt_entry(struct adapter *adapter, int idx)
  773. {
  774. struct cpl_smt_write_req *req;
  775. struct port_info *pi = netdev_priv(adapter->port[idx]);
  776. struct sk_buff *skb = alloc_skb(sizeof(*req), GFP_KERNEL);
  777. if (!skb)
  778. return -ENOMEM;
  779. req = (struct cpl_smt_write_req *)__skb_put(skb, sizeof(*req));
  780. req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
  781. OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SMT_WRITE_REQ, idx));
  782. req->mtu_idx = NMTUS - 1; /* should be 0 but there's a T3 bug */
  783. req->iff = idx;
  784. memcpy(req->src_mac0, adapter->port[idx]->dev_addr, ETH_ALEN);
  785. memcpy(req->src_mac1, pi->iscsic.mac_addr, ETH_ALEN);
  786. skb->priority = 1;
  787. offload_tx(&adapter->tdev, skb);
  788. return 0;
  789. }
  790. static int init_smt(struct adapter *adapter)
  791. {
  792. int i;
  793. for_each_port(adapter, i)
  794. write_smt_entry(adapter, i);
  795. return 0;
  796. }
  797. static void init_port_mtus(struct adapter *adapter)
  798. {
  799. unsigned int mtus = adapter->port[0]->mtu;
  800. if (adapter->port[1])
  801. mtus |= adapter->port[1]->mtu << 16;
  802. t3_write_reg(adapter, A_TP_MTU_PORT_TABLE, mtus);
  803. }
  804. static int send_pktsched_cmd(struct adapter *adap, int sched, int qidx, int lo,
  805. int hi, int port)
  806. {
  807. struct sk_buff *skb;
  808. struct mngt_pktsched_wr *req;
  809. int ret;
  810. skb = alloc_skb(sizeof(*req), GFP_KERNEL);
  811. if (!skb)
  812. skb = adap->nofail_skb;
  813. if (!skb)
  814. return -ENOMEM;
  815. req = (struct mngt_pktsched_wr *)skb_put(skb, sizeof(*req));
  816. req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_MNGT));
  817. req->mngt_opcode = FW_MNGTOPCODE_PKTSCHED_SET;
  818. req->sched = sched;
  819. req->idx = qidx;
  820. req->min = lo;
  821. req->max = hi;
  822. req->binding = port;
  823. ret = t3_mgmt_tx(adap, skb);
  824. if (skb == adap->nofail_skb) {
  825. adap->nofail_skb = alloc_skb(sizeof(struct cpl_set_tcb_field),
  826. GFP_KERNEL);
  827. if (!adap->nofail_skb)
  828. ret = -ENOMEM;
  829. }
  830. return ret;
  831. }
  832. static int bind_qsets(struct adapter *adap)
  833. {
  834. int i, j, err = 0;
  835. for_each_port(adap, i) {
  836. const struct port_info *pi = adap2pinfo(adap, i);
  837. for (j = 0; j < pi->nqsets; ++j) {
  838. int ret = send_pktsched_cmd(adap, 1,
  839. pi->first_qset + j, -1,
  840. -1, i);
  841. if (ret)
  842. err = ret;
  843. }
  844. }
  845. return err;
  846. }
  847. /*(DEBLOBBED)*/
  848. #define FW_FNAME "/*(DEBLOBBED)*/"
  849. /*(DEBLOBBED)*/
  850. #define TPSRAM_NAME "/*(DEBLOBBED)*/"
  851. #define AEL2005_OPT_EDC_NAME "/*(DEBLOBBED)*/"
  852. #define AEL2005_TWX_EDC_NAME "/*(DEBLOBBED)*/"
  853. #define AEL2020_TWX_EDC_NAME "/*(DEBLOBBED)*/"
  854. /*(DEBLOBBED)*/
  855. static inline const char *get_edc_fw_name(int edc_idx)
  856. {
  857. const char *fw_name = NULL;
  858. switch (edc_idx) {
  859. case EDC_OPT_AEL2005:
  860. fw_name = AEL2005_OPT_EDC_NAME;
  861. break;
  862. case EDC_TWX_AEL2005:
  863. fw_name = AEL2005_TWX_EDC_NAME;
  864. break;
  865. case EDC_TWX_AEL2020:
  866. fw_name = AEL2020_TWX_EDC_NAME;
  867. break;
  868. }
  869. return fw_name;
  870. }
  871. int t3_get_edc_fw(struct cphy *phy, int edc_idx, int size)
  872. {
  873. struct adapter *adapter = phy->adapter;
  874. const struct firmware *fw;
  875. const char *fw_name;
  876. u32 csum;
  877. const __be32 *p;
  878. u16 *cache = phy->phy_cache;
  879. int i, ret = -EINVAL;
  880. fw_name = get_edc_fw_name(edc_idx);
  881. if (fw_name)
  882. ret = reject_firmware(&fw, fw_name, &adapter->pdev->dev);
  883. if (ret < 0) {
  884. dev_err(&adapter->pdev->dev,
  885. "could not upgrade firmware: unable to load %s\n",
  886. fw_name);
  887. return ret;
  888. }
  889. /* check size, take checksum in account */
  890. if (fw->size > size + 4) {
  891. CH_ERR(adapter, "firmware image too large %u, expected %d\n",
  892. (unsigned int)fw->size, size + 4);
  893. ret = -EINVAL;
  894. }
  895. /* compute checksum */
  896. p = (const __be32 *)fw->data;
  897. for (csum = 0, i = 0; i < fw->size / sizeof(csum); i++)
  898. csum += ntohl(p[i]);
  899. if (csum != 0xffffffff) {
  900. CH_ERR(adapter, "corrupted firmware image, checksum %u\n",
  901. csum);
  902. ret = -EINVAL;
  903. }
  904. for (i = 0; i < size / 4 ; i++) {
  905. *cache++ = (be32_to_cpu(p[i]) & 0xffff0000) >> 16;
  906. *cache++ = be32_to_cpu(p[i]) & 0xffff;
  907. }
  908. release_firmware(fw);
  909. return ret;
  910. }
  911. static int upgrade_fw(struct adapter *adap)
  912. {
  913. int ret;
  914. const struct firmware *fw;
  915. struct device *dev = &adap->pdev->dev;
  916. ret = reject_firmware(&fw, FW_FNAME, dev);
  917. if (ret < 0) {
  918. dev_err(dev, "could not upgrade firmware: unable to load %s\n",
  919. FW_FNAME);
  920. return ret;
  921. }
  922. ret = t3_load_fw(adap, fw->data, fw->size);
  923. release_firmware(fw);
  924. if (ret == 0)
  925. dev_info(dev, "successful upgrade to firmware %d.%d.%d\n",
  926. FW_VERSION_MAJOR, FW_VERSION_MINOR, FW_VERSION_MICRO);
  927. else
  928. dev_err(dev, "failed to upgrade to firmware %d.%d.%d\n",
  929. FW_VERSION_MAJOR, FW_VERSION_MINOR, FW_VERSION_MICRO);
  930. return ret;
  931. }
  932. static inline char t3rev2char(struct adapter *adapter)
  933. {
  934. char rev = 0;
  935. switch(adapter->params.rev) {
  936. case T3_REV_B:
  937. case T3_REV_B2:
  938. rev = 'b';
  939. break;
  940. case T3_REV_C:
  941. rev = 'c';
  942. break;
  943. }
  944. return rev;
  945. }
  946. static int update_tpsram(struct adapter *adap)
  947. {
  948. const struct firmware *tpsram;
  949. char buf[64];
  950. struct device *dev = &adap->pdev->dev;
  951. int ret;
  952. char rev;
  953. rev = t3rev2char(adap);
  954. if (!rev)
  955. return 0;
  956. snprintf(buf, sizeof(buf), TPSRAM_NAME, rev);
  957. ret = reject_firmware(&tpsram, buf, dev);
  958. if (ret < 0) {
  959. dev_err(dev, "could not load TP SRAM: unable to load %s\n",
  960. buf);
  961. return ret;
  962. }
  963. ret = t3_check_tpsram(adap, tpsram->data, tpsram->size);
  964. if (ret)
  965. goto release_tpsram;
  966. ret = t3_set_proto_sram(adap, tpsram->data);
  967. if (ret == 0)
  968. dev_info(dev,
  969. "successful update of protocol engine "
  970. "to %d.%d.%d\n",
  971. TP_VERSION_MAJOR, TP_VERSION_MINOR, TP_VERSION_MICRO);
  972. else
  973. dev_err(dev, "failed to update of protocol engine %d.%d.%d\n",
  974. TP_VERSION_MAJOR, TP_VERSION_MINOR, TP_VERSION_MICRO);
  975. if (ret)
  976. dev_err(dev, "loading protocol SRAM failed\n");
  977. release_tpsram:
  978. release_firmware(tpsram);
  979. return ret;
  980. }
  981. /**
  982. * t3_synchronize_rx - wait for current Rx processing on a port to complete
  983. * @adap: the adapter
  984. * @p: the port
  985. *
  986. * Ensures that current Rx processing on any of the queues associated with
  987. * the given port completes before returning. We do this by acquiring and
  988. * releasing the locks of the response queues associated with the port.
  989. */
  990. static void t3_synchronize_rx(struct adapter *adap, const struct port_info *p)
  991. {
  992. int i;
  993. for (i = p->first_qset; i < p->first_qset + p->nqsets; i++) {
  994. struct sge_rspq *q = &adap->sge.qs[i].rspq;
  995. spin_lock_irq(&q->lock);
  996. spin_unlock_irq(&q->lock);
  997. }
  998. }
  999. static void cxgb_vlan_mode(struct net_device *dev, netdev_features_t features)
  1000. {
  1001. struct port_info *pi = netdev_priv(dev);
  1002. struct adapter *adapter = pi->adapter;
  1003. if (adapter->params.rev > 0) {
  1004. t3_set_vlan_accel(adapter, 1 << pi->port_id,
  1005. features & NETIF_F_HW_VLAN_CTAG_RX);
  1006. } else {
  1007. /* single control for all ports */
  1008. unsigned int i, have_vlans = features & NETIF_F_HW_VLAN_CTAG_RX;
  1009. for_each_port(adapter, i)
  1010. have_vlans |=
  1011. adapter->port[i]->features &
  1012. NETIF_F_HW_VLAN_CTAG_RX;
  1013. t3_set_vlan_accel(adapter, 1, have_vlans);
  1014. }
  1015. t3_synchronize_rx(adapter, pi);
  1016. }
  1017. /**
  1018. * cxgb_up - enable the adapter
  1019. * @adapter: adapter being enabled
  1020. *
  1021. * Called when the first port is enabled, this function performs the
  1022. * actions necessary to make an adapter operational, such as completing
  1023. * the initialization of HW modules, and enabling interrupts.
  1024. *
  1025. * Must be called with the rtnl lock held.
  1026. */
  1027. static int cxgb_up(struct adapter *adap)
  1028. {
  1029. int i, err;
  1030. if (!(adap->flags & FULL_INIT_DONE)) {
  1031. err = t3_check_fw_version(adap);
  1032. if (err == -EINVAL) {
  1033. err = upgrade_fw(adap);
  1034. CH_WARN(adap, "FW upgrade to %d.%d.%d %s\n",
  1035. FW_VERSION_MAJOR, FW_VERSION_MINOR,
  1036. FW_VERSION_MICRO, err ? "failed" : "succeeded");
  1037. }
  1038. err = t3_check_tpsram_version(adap);
  1039. if (err == -EINVAL) {
  1040. err = update_tpsram(adap);
  1041. CH_WARN(adap, "TP upgrade to %d.%d.%d %s\n",
  1042. TP_VERSION_MAJOR, TP_VERSION_MINOR,
  1043. TP_VERSION_MICRO, err ? "failed" : "succeeded");
  1044. }
  1045. /*
  1046. * Clear interrupts now to catch errors if t3_init_hw fails.
  1047. * We clear them again later as initialization may trigger
  1048. * conditions that can interrupt.
  1049. */
  1050. t3_intr_clear(adap);
  1051. err = t3_init_hw(adap, 0);
  1052. if (err)
  1053. goto out;
  1054. t3_set_reg_field(adap, A_TP_PARA_REG5, 0, F_RXDDPOFFINIT);
  1055. t3_write_reg(adap, A_ULPRX_TDDP_PSZ, V_HPZ0(PAGE_SHIFT - 12));
  1056. err = setup_sge_qsets(adap);
  1057. if (err)
  1058. goto out;
  1059. for_each_port(adap, i)
  1060. cxgb_vlan_mode(adap->port[i], adap->port[i]->features);
  1061. setup_rss(adap);
  1062. if (!(adap->flags & NAPI_INIT))
  1063. init_napi(adap);
  1064. t3_start_sge_timers(adap);
  1065. adap->flags |= FULL_INIT_DONE;
  1066. }
  1067. t3_intr_clear(adap);
  1068. if (adap->flags & USING_MSIX) {
  1069. name_msix_vecs(adap);
  1070. err = request_irq(adap->msix_info[0].vec,
  1071. t3_async_intr_handler, 0,
  1072. adap->msix_info[0].desc, adap);
  1073. if (err)
  1074. goto irq_err;
  1075. err = request_msix_data_irqs(adap);
  1076. if (err) {
  1077. free_irq(adap->msix_info[0].vec, adap);
  1078. goto irq_err;
  1079. }
  1080. } else if ((err = request_irq(adap->pdev->irq,
  1081. t3_intr_handler(adap,
  1082. adap->sge.qs[0].rspq.
  1083. polling),
  1084. (adap->flags & USING_MSI) ?
  1085. 0 : IRQF_SHARED,
  1086. adap->name, adap)))
  1087. goto irq_err;
  1088. enable_all_napi(adap);
  1089. t3_sge_start(adap);
  1090. t3_intr_enable(adap);
  1091. if (adap->params.rev >= T3_REV_C && !(adap->flags & TP_PARITY_INIT) &&
  1092. is_offload(adap) && init_tp_parity(adap) == 0)
  1093. adap->flags |= TP_PARITY_INIT;
  1094. if (adap->flags & TP_PARITY_INIT) {
  1095. t3_write_reg(adap, A_TP_INT_CAUSE,
  1096. F_CMCACHEPERR | F_ARPLUTPERR);
  1097. t3_write_reg(adap, A_TP_INT_ENABLE, 0x7fbfffff);
  1098. }
  1099. if (!(adap->flags & QUEUES_BOUND)) {
  1100. int ret = bind_qsets(adap);
  1101. if (ret < 0) {
  1102. CH_ERR(adap, "failed to bind qsets, err %d\n", ret);
  1103. t3_intr_disable(adap);
  1104. free_irq_resources(adap);
  1105. err = ret;
  1106. goto out;
  1107. }
  1108. adap->flags |= QUEUES_BOUND;
  1109. }
  1110. out:
  1111. return err;
  1112. irq_err:
  1113. CH_ERR(adap, "request_irq failed, err %d\n", err);
  1114. goto out;
  1115. }
  1116. /*
  1117. * Release resources when all the ports and offloading have been stopped.
  1118. */
  1119. static void cxgb_down(struct adapter *adapter, int on_wq)
  1120. {
  1121. t3_sge_stop(adapter);
  1122. spin_lock_irq(&adapter->work_lock); /* sync with PHY intr task */
  1123. t3_intr_disable(adapter);
  1124. spin_unlock_irq(&adapter->work_lock);
  1125. free_irq_resources(adapter);
  1126. quiesce_rx(adapter);
  1127. t3_sge_stop(adapter);
  1128. if (!on_wq)
  1129. flush_workqueue(cxgb3_wq);/* wait for external IRQ handler */
  1130. }
  1131. static void schedule_chk_task(struct adapter *adap)
  1132. {
  1133. unsigned int timeo;
  1134. timeo = adap->params.linkpoll_period ?
  1135. (HZ * adap->params.linkpoll_period) / 10 :
  1136. adap->params.stats_update_period * HZ;
  1137. if (timeo)
  1138. queue_delayed_work(cxgb3_wq, &adap->adap_check_task, timeo);
  1139. }
  1140. static int offload_open(struct net_device *dev)
  1141. {
  1142. struct port_info *pi = netdev_priv(dev);
  1143. struct adapter *adapter = pi->adapter;
  1144. struct t3cdev *tdev = dev2t3cdev(dev);
  1145. int adap_up = adapter->open_device_map & PORT_MASK;
  1146. int err;
  1147. if (test_and_set_bit(OFFLOAD_DEVMAP_BIT, &adapter->open_device_map))
  1148. return 0;
  1149. if (!adap_up && (err = cxgb_up(adapter)) < 0)
  1150. goto out;
  1151. t3_tp_set_offload_mode(adapter, 1);
  1152. tdev->lldev = adapter->port[0];
  1153. err = cxgb3_offload_activate(adapter);
  1154. if (err)
  1155. goto out;
  1156. init_port_mtus(adapter);
  1157. t3_load_mtus(adapter, adapter->params.mtus, adapter->params.a_wnd,
  1158. adapter->params.b_wnd,
  1159. adapter->params.rev == 0 ?
  1160. adapter->port[0]->mtu : 0xffff);
  1161. init_smt(adapter);
  1162. if (sysfs_create_group(&tdev->lldev->dev.kobj, &offload_attr_group))
  1163. dev_dbg(&dev->dev, "cannot create sysfs group\n");
  1164. /* Call back all registered clients */
  1165. cxgb3_add_clients(tdev);
  1166. out:
  1167. /* restore them in case the offload module has changed them */
  1168. if (err) {
  1169. t3_tp_set_offload_mode(adapter, 0);
  1170. clear_bit(OFFLOAD_DEVMAP_BIT, &adapter->open_device_map);
  1171. cxgb3_set_dummy_ops(tdev);
  1172. }
  1173. return err;
  1174. }
  1175. static int offload_close(struct t3cdev *tdev)
  1176. {
  1177. struct adapter *adapter = tdev2adap(tdev);
  1178. struct t3c_data *td = T3C_DATA(tdev);
  1179. if (!test_bit(OFFLOAD_DEVMAP_BIT, &adapter->open_device_map))
  1180. return 0;
  1181. /* Call back all registered clients */
  1182. cxgb3_remove_clients(tdev);
  1183. sysfs_remove_group(&tdev->lldev->dev.kobj, &offload_attr_group);
  1184. /* Flush work scheduled while releasing TIDs */
  1185. flush_work(&td->tid_release_task);
  1186. tdev->lldev = NULL;
  1187. cxgb3_set_dummy_ops(tdev);
  1188. t3_tp_set_offload_mode(adapter, 0);
  1189. clear_bit(OFFLOAD_DEVMAP_BIT, &adapter->open_device_map);
  1190. if (!adapter->open_device_map)
  1191. cxgb_down(adapter, 0);
  1192. cxgb3_offload_deactivate(adapter);
  1193. return 0;
  1194. }
  1195. static int cxgb_open(struct net_device *dev)
  1196. {
  1197. struct port_info *pi = netdev_priv(dev);
  1198. struct adapter *adapter = pi->adapter;
  1199. int other_ports = adapter->open_device_map & PORT_MASK;
  1200. int err;
  1201. if (!adapter->open_device_map && (err = cxgb_up(adapter)) < 0)
  1202. return err;
  1203. set_bit(pi->port_id, &adapter->open_device_map);
  1204. if (is_offload(adapter) && !ofld_disable) {
  1205. err = offload_open(dev);
  1206. if (err)
  1207. pr_warn("Could not initialize offload capabilities\n");
  1208. }
  1209. netif_set_real_num_tx_queues(dev, pi->nqsets);
  1210. err = netif_set_real_num_rx_queues(dev, pi->nqsets);
  1211. if (err)
  1212. return err;
  1213. link_start(dev);
  1214. t3_port_intr_enable(adapter, pi->port_id);
  1215. netif_tx_start_all_queues(dev);
  1216. if (!other_ports)
  1217. schedule_chk_task(adapter);
  1218. cxgb3_event_notify(&adapter->tdev, OFFLOAD_PORT_UP, pi->port_id);
  1219. return 0;
  1220. }
  1221. static int __cxgb_close(struct net_device *dev, int on_wq)
  1222. {
  1223. struct port_info *pi = netdev_priv(dev);
  1224. struct adapter *adapter = pi->adapter;
  1225. if (!adapter->open_device_map)
  1226. return 0;
  1227. /* Stop link fault interrupts */
  1228. t3_xgm_intr_disable(adapter, pi->port_id);
  1229. t3_read_reg(adapter, A_XGM_INT_STATUS + pi->mac.offset);
  1230. t3_port_intr_disable(adapter, pi->port_id);
  1231. netif_tx_stop_all_queues(dev);
  1232. pi->phy.ops->power_down(&pi->phy, 1);
  1233. netif_carrier_off(dev);
  1234. t3_mac_disable(&pi->mac, MAC_DIRECTION_TX | MAC_DIRECTION_RX);
  1235. spin_lock_irq(&adapter->work_lock); /* sync with update task */
  1236. clear_bit(pi->port_id, &adapter->open_device_map);
  1237. spin_unlock_irq(&adapter->work_lock);
  1238. if (!(adapter->open_device_map & PORT_MASK))
  1239. cancel_delayed_work_sync(&adapter->adap_check_task);
  1240. if (!adapter->open_device_map)
  1241. cxgb_down(adapter, on_wq);
  1242. cxgb3_event_notify(&adapter->tdev, OFFLOAD_PORT_DOWN, pi->port_id);
  1243. return 0;
  1244. }
  1245. static int cxgb_close(struct net_device *dev)
  1246. {
  1247. return __cxgb_close(dev, 0);
  1248. }
  1249. static struct net_device_stats *cxgb_get_stats(struct net_device *dev)
  1250. {
  1251. struct port_info *pi = netdev_priv(dev);
  1252. struct adapter *adapter = pi->adapter;
  1253. struct net_device_stats *ns = &pi->netstats;
  1254. const struct mac_stats *pstats;
  1255. spin_lock(&adapter->stats_lock);
  1256. pstats = t3_mac_update_stats(&pi->mac);
  1257. spin_unlock(&adapter->stats_lock);
  1258. ns->tx_bytes = pstats->tx_octets;
  1259. ns->tx_packets = pstats->tx_frames;
  1260. ns->rx_bytes = pstats->rx_octets;
  1261. ns->rx_packets = pstats->rx_frames;
  1262. ns->multicast = pstats->rx_mcast_frames;
  1263. ns->tx_errors = pstats->tx_underrun;
  1264. ns->rx_errors = pstats->rx_symbol_errs + pstats->rx_fcs_errs +
  1265. pstats->rx_too_long + pstats->rx_jabber + pstats->rx_short +
  1266. pstats->rx_fifo_ovfl;
  1267. /* detailed rx_errors */
  1268. ns->rx_length_errors = pstats->rx_jabber + pstats->rx_too_long;
  1269. ns->rx_over_errors = 0;
  1270. ns->rx_crc_errors = pstats->rx_fcs_errs;
  1271. ns->rx_frame_errors = pstats->rx_symbol_errs;
  1272. ns->rx_fifo_errors = pstats->rx_fifo_ovfl;
  1273. ns->rx_missed_errors = pstats->rx_cong_drops;
  1274. /* detailed tx_errors */
  1275. ns->tx_aborted_errors = 0;
  1276. ns->tx_carrier_errors = 0;
  1277. ns->tx_fifo_errors = pstats->tx_underrun;
  1278. ns->tx_heartbeat_errors = 0;
  1279. ns->tx_window_errors = 0;
  1280. return ns;
  1281. }
  1282. static u32 get_msglevel(struct net_device *dev)
  1283. {
  1284. struct port_info *pi = netdev_priv(dev);
  1285. struct adapter *adapter = pi->adapter;
  1286. return adapter->msg_enable;
  1287. }
  1288. static void set_msglevel(struct net_device *dev, u32 val)
  1289. {
  1290. struct port_info *pi = netdev_priv(dev);
  1291. struct adapter *adapter = pi->adapter;
  1292. adapter->msg_enable = val;
  1293. }
  1294. static const char stats_strings[][ETH_GSTRING_LEN] = {
  1295. "TxOctetsOK ",
  1296. "TxFramesOK ",
  1297. "TxMulticastFramesOK",
  1298. "TxBroadcastFramesOK",
  1299. "TxPauseFrames ",
  1300. "TxUnderrun ",
  1301. "TxExtUnderrun ",
  1302. "TxFrames64 ",
  1303. "TxFrames65To127 ",
  1304. "TxFrames128To255 ",
  1305. "TxFrames256To511 ",
  1306. "TxFrames512To1023 ",
  1307. "TxFrames1024To1518 ",
  1308. "TxFrames1519ToMax ",
  1309. "RxOctetsOK ",
  1310. "RxFramesOK ",
  1311. "RxMulticastFramesOK",
  1312. "RxBroadcastFramesOK",
  1313. "RxPauseFrames ",
  1314. "RxFCSErrors ",
  1315. "RxSymbolErrors ",
  1316. "RxShortErrors ",
  1317. "RxJabberErrors ",
  1318. "RxLengthErrors ",
  1319. "RxFIFOoverflow ",
  1320. "RxFrames64 ",
  1321. "RxFrames65To127 ",
  1322. "RxFrames128To255 ",
  1323. "RxFrames256To511 ",
  1324. "RxFrames512To1023 ",
  1325. "RxFrames1024To1518 ",
  1326. "RxFrames1519ToMax ",
  1327. "PhyFIFOErrors ",
  1328. "TSO ",
  1329. "VLANextractions ",
  1330. "VLANinsertions ",
  1331. "TxCsumOffload ",
  1332. "RxCsumGood ",
  1333. "LroAggregated ",
  1334. "LroFlushed ",
  1335. "LroNoDesc ",
  1336. "RxDrops ",
  1337. "CheckTXEnToggled ",
  1338. "CheckResets ",
  1339. "LinkFaults ",
  1340. };
  1341. static int get_sset_count(struct net_device *dev, int sset)
  1342. {
  1343. switch (sset) {
  1344. case ETH_SS_STATS:
  1345. return ARRAY_SIZE(stats_strings);
  1346. default:
  1347. return -EOPNOTSUPP;
  1348. }
  1349. }
  1350. #define T3_REGMAP_SIZE (3 * 1024)
  1351. static int get_regs_len(struct net_device *dev)
  1352. {
  1353. return T3_REGMAP_SIZE;
  1354. }
  1355. static int get_eeprom_len(struct net_device *dev)
  1356. {
  1357. return EEPROMSIZE;
  1358. }
  1359. static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1360. {
  1361. struct port_info *pi = netdev_priv(dev);
  1362. struct adapter *adapter = pi->adapter;
  1363. u32 fw_vers = 0;
  1364. u32 tp_vers = 0;
  1365. spin_lock(&adapter->stats_lock);
  1366. t3_get_fw_version(adapter, &fw_vers);
  1367. t3_get_tp_version(adapter, &tp_vers);
  1368. spin_unlock(&adapter->stats_lock);
  1369. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  1370. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  1371. strlcpy(info->bus_info, pci_name(adapter->pdev),
  1372. sizeof(info->bus_info));
  1373. if (fw_vers)
  1374. snprintf(info->fw_version, sizeof(info->fw_version),
  1375. "%s %u.%u.%u TP %u.%u.%u",
  1376. G_FW_VERSION_TYPE(fw_vers) ? "T" : "N",
  1377. G_FW_VERSION_MAJOR(fw_vers),
  1378. G_FW_VERSION_MINOR(fw_vers),
  1379. G_FW_VERSION_MICRO(fw_vers),
  1380. G_TP_VERSION_MAJOR(tp_vers),
  1381. G_TP_VERSION_MINOR(tp_vers),
  1382. G_TP_VERSION_MICRO(tp_vers));
  1383. }
  1384. static void get_strings(struct net_device *dev, u32 stringset, u8 * data)
  1385. {
  1386. if (stringset == ETH_SS_STATS)
  1387. memcpy(data, stats_strings, sizeof(stats_strings));
  1388. }
  1389. static unsigned long collect_sge_port_stats(struct adapter *adapter,
  1390. struct port_info *p, int idx)
  1391. {
  1392. int i;
  1393. unsigned long tot = 0;
  1394. for (i = p->first_qset; i < p->first_qset + p->nqsets; ++i)
  1395. tot += adapter->sge.qs[i].port_stats[idx];
  1396. return tot;
  1397. }
  1398. static void get_stats(struct net_device *dev, struct ethtool_stats *stats,
  1399. u64 *data)
  1400. {
  1401. struct port_info *pi = netdev_priv(dev);
  1402. struct adapter *adapter = pi->adapter;
  1403. const struct mac_stats *s;
  1404. spin_lock(&adapter->stats_lock);
  1405. s = t3_mac_update_stats(&pi->mac);
  1406. spin_unlock(&adapter->stats_lock);
  1407. *data++ = s->tx_octets;
  1408. *data++ = s->tx_frames;
  1409. *data++ = s->tx_mcast_frames;
  1410. *data++ = s->tx_bcast_frames;
  1411. *data++ = s->tx_pause;
  1412. *data++ = s->tx_underrun;
  1413. *data++ = s->tx_fifo_urun;
  1414. *data++ = s->tx_frames_64;
  1415. *data++ = s->tx_frames_65_127;
  1416. *data++ = s->tx_frames_128_255;
  1417. *data++ = s->tx_frames_256_511;
  1418. *data++ = s->tx_frames_512_1023;
  1419. *data++ = s->tx_frames_1024_1518;
  1420. *data++ = s->tx_frames_1519_max;
  1421. *data++ = s->rx_octets;
  1422. *data++ = s->rx_frames;
  1423. *data++ = s->rx_mcast_frames;
  1424. *data++ = s->rx_bcast_frames;
  1425. *data++ = s->rx_pause;
  1426. *data++ = s->rx_fcs_errs;
  1427. *data++ = s->rx_symbol_errs;
  1428. *data++ = s->rx_short;
  1429. *data++ = s->rx_jabber;
  1430. *data++ = s->rx_too_long;
  1431. *data++ = s->rx_fifo_ovfl;
  1432. *data++ = s->rx_frames_64;
  1433. *data++ = s->rx_frames_65_127;
  1434. *data++ = s->rx_frames_128_255;
  1435. *data++ = s->rx_frames_256_511;
  1436. *data++ = s->rx_frames_512_1023;
  1437. *data++ = s->rx_frames_1024_1518;
  1438. *data++ = s->rx_frames_1519_max;
  1439. *data++ = pi->phy.fifo_errors;
  1440. *data++ = collect_sge_port_stats(adapter, pi, SGE_PSTAT_TSO);
  1441. *data++ = collect_sge_port_stats(adapter, pi, SGE_PSTAT_VLANEX);
  1442. *data++ = collect_sge_port_stats(adapter, pi, SGE_PSTAT_VLANINS);
  1443. *data++ = collect_sge_port_stats(adapter, pi, SGE_PSTAT_TX_CSUM);
  1444. *data++ = collect_sge_port_stats(adapter, pi, SGE_PSTAT_RX_CSUM_GOOD);
  1445. *data++ = 0;
  1446. *data++ = 0;
  1447. *data++ = 0;
  1448. *data++ = s->rx_cong_drops;
  1449. *data++ = s->num_toggled;
  1450. *data++ = s->num_resets;
  1451. *data++ = s->link_faults;
  1452. }
  1453. static inline void reg_block_dump(struct adapter *ap, void *buf,
  1454. unsigned int start, unsigned int end)
  1455. {
  1456. u32 *p = buf + start;
  1457. for (; start <= end; start += sizeof(u32))
  1458. *p++ = t3_read_reg(ap, start);
  1459. }
  1460. static void get_regs(struct net_device *dev, struct ethtool_regs *regs,
  1461. void *buf)
  1462. {
  1463. struct port_info *pi = netdev_priv(dev);
  1464. struct adapter *ap = pi->adapter;
  1465. /*
  1466. * Version scheme:
  1467. * bits 0..9: chip version
  1468. * bits 10..15: chip revision
  1469. * bit 31: set for PCIe cards
  1470. */
  1471. regs->version = 3 | (ap->params.rev << 10) | (is_pcie(ap) << 31);
  1472. /*
  1473. * We skip the MAC statistics registers because they are clear-on-read.
  1474. * Also reading multi-register stats would need to synchronize with the
  1475. * periodic mac stats accumulation. Hard to justify the complexity.
  1476. */
  1477. memset(buf, 0, T3_REGMAP_SIZE);
  1478. reg_block_dump(ap, buf, 0, A_SG_RSPQ_CREDIT_RETURN);
  1479. reg_block_dump(ap, buf, A_SG_HI_DRB_HI_THRSH, A_ULPRX_PBL_ULIMIT);
  1480. reg_block_dump(ap, buf, A_ULPTX_CONFIG, A_MPS_INT_CAUSE);
  1481. reg_block_dump(ap, buf, A_CPL_SWITCH_CNTRL, A_CPL_MAP_TBL_DATA);
  1482. reg_block_dump(ap, buf, A_SMB_GLOBAL_TIME_CFG, A_XGM_SERDES_STAT3);
  1483. reg_block_dump(ap, buf, A_XGM_SERDES_STATUS0,
  1484. XGM_REG(A_XGM_SERDES_STAT3, 1));
  1485. reg_block_dump(ap, buf, XGM_REG(A_XGM_SERDES_STATUS0, 1),
  1486. XGM_REG(A_XGM_RX_SPI4_SOP_EOP_CNT, 1));
  1487. }
  1488. static int restart_autoneg(struct net_device *dev)
  1489. {
  1490. struct port_info *p = netdev_priv(dev);
  1491. if (!netif_running(dev))
  1492. return -EAGAIN;
  1493. if (p->link_config.autoneg != AUTONEG_ENABLE)
  1494. return -EINVAL;
  1495. p->phy.ops->autoneg_restart(&p->phy);
  1496. return 0;
  1497. }
  1498. static int set_phys_id(struct net_device *dev,
  1499. enum ethtool_phys_id_state state)
  1500. {
  1501. struct port_info *pi = netdev_priv(dev);
  1502. struct adapter *adapter = pi->adapter;
  1503. switch (state) {
  1504. case ETHTOOL_ID_ACTIVE:
  1505. return 1; /* cycle on/off once per second */
  1506. case ETHTOOL_ID_OFF:
  1507. t3_set_reg_field(adapter, A_T3DBG_GPIO_EN, F_GPIO0_OUT_VAL, 0);
  1508. break;
  1509. case ETHTOOL_ID_ON:
  1510. case ETHTOOL_ID_INACTIVE:
  1511. t3_set_reg_field(adapter, A_T3DBG_GPIO_EN, F_GPIO0_OUT_VAL,
  1512. F_GPIO0_OUT_VAL);
  1513. }
  1514. return 0;
  1515. }
  1516. static int get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1517. {
  1518. struct port_info *p = netdev_priv(dev);
  1519. cmd->supported = p->link_config.supported;
  1520. cmd->advertising = p->link_config.advertising;
  1521. if (netif_carrier_ok(dev)) {
  1522. ethtool_cmd_speed_set(cmd, p->link_config.speed);
  1523. cmd->duplex = p->link_config.duplex;
  1524. } else {
  1525. ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
  1526. cmd->duplex = DUPLEX_UNKNOWN;
  1527. }
  1528. cmd->port = (cmd->supported & SUPPORTED_TP) ? PORT_TP : PORT_FIBRE;
  1529. cmd->phy_address = p->phy.mdio.prtad;
  1530. cmd->transceiver = XCVR_EXTERNAL;
  1531. cmd->autoneg = p->link_config.autoneg;
  1532. cmd->maxtxpkt = 0;
  1533. cmd->maxrxpkt = 0;
  1534. return 0;
  1535. }
  1536. static int speed_duplex_to_caps(int speed, int duplex)
  1537. {
  1538. int cap = 0;
  1539. switch (speed) {
  1540. case SPEED_10:
  1541. if (duplex == DUPLEX_FULL)
  1542. cap = SUPPORTED_10baseT_Full;
  1543. else
  1544. cap = SUPPORTED_10baseT_Half;
  1545. break;
  1546. case SPEED_100:
  1547. if (duplex == DUPLEX_FULL)
  1548. cap = SUPPORTED_100baseT_Full;
  1549. else
  1550. cap = SUPPORTED_100baseT_Half;
  1551. break;
  1552. case SPEED_1000:
  1553. if (duplex == DUPLEX_FULL)
  1554. cap = SUPPORTED_1000baseT_Full;
  1555. else
  1556. cap = SUPPORTED_1000baseT_Half;
  1557. break;
  1558. case SPEED_10000:
  1559. if (duplex == DUPLEX_FULL)
  1560. cap = SUPPORTED_10000baseT_Full;
  1561. }
  1562. return cap;
  1563. }
  1564. #define ADVERTISED_MASK (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full | \
  1565. ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full | \
  1566. ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full | \
  1567. ADVERTISED_10000baseT_Full)
  1568. static int set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1569. {
  1570. struct port_info *p = netdev_priv(dev);
  1571. struct link_config *lc = &p->link_config;
  1572. if (!(lc->supported & SUPPORTED_Autoneg)) {
  1573. /*
  1574. * PHY offers a single speed/duplex. See if that's what's
  1575. * being requested.
  1576. */
  1577. if (cmd->autoneg == AUTONEG_DISABLE) {
  1578. u32 speed = ethtool_cmd_speed(cmd);
  1579. int cap = speed_duplex_to_caps(speed, cmd->duplex);
  1580. if (lc->supported & cap)
  1581. return 0;
  1582. }
  1583. return -EINVAL;
  1584. }
  1585. if (cmd->autoneg == AUTONEG_DISABLE) {
  1586. u32 speed = ethtool_cmd_speed(cmd);
  1587. int cap = speed_duplex_to_caps(speed, cmd->duplex);
  1588. if (!(lc->supported & cap) || (speed == SPEED_1000))
  1589. return -EINVAL;
  1590. lc->requested_speed = speed;
  1591. lc->requested_duplex = cmd->duplex;
  1592. lc->advertising = 0;
  1593. } else {
  1594. cmd->advertising &= ADVERTISED_MASK;
  1595. cmd->advertising &= lc->supported;
  1596. if (!cmd->advertising)
  1597. return -EINVAL;
  1598. lc->requested_speed = SPEED_INVALID;
  1599. lc->requested_duplex = DUPLEX_INVALID;
  1600. lc->advertising = cmd->advertising | ADVERTISED_Autoneg;
  1601. }
  1602. lc->autoneg = cmd->autoneg;
  1603. if (netif_running(dev))
  1604. t3_link_start(&p->phy, &p->mac, lc);
  1605. return 0;
  1606. }
  1607. static void get_pauseparam(struct net_device *dev,
  1608. struct ethtool_pauseparam *epause)
  1609. {
  1610. struct port_info *p = netdev_priv(dev);
  1611. epause->autoneg = (p->link_config.requested_fc & PAUSE_AUTONEG) != 0;
  1612. epause->rx_pause = (p->link_config.fc & PAUSE_RX) != 0;
  1613. epause->tx_pause = (p->link_config.fc & PAUSE_TX) != 0;
  1614. }
  1615. static int set_pauseparam(struct net_device *dev,
  1616. struct ethtool_pauseparam *epause)
  1617. {
  1618. struct port_info *p = netdev_priv(dev);
  1619. struct link_config *lc = &p->link_config;
  1620. if (epause->autoneg == AUTONEG_DISABLE)
  1621. lc->requested_fc = 0;
  1622. else if (lc->supported & SUPPORTED_Autoneg)
  1623. lc->requested_fc = PAUSE_AUTONEG;
  1624. else
  1625. return -EINVAL;
  1626. if (epause->rx_pause)
  1627. lc->requested_fc |= PAUSE_RX;
  1628. if (epause->tx_pause)
  1629. lc->requested_fc |= PAUSE_TX;
  1630. if (lc->autoneg == AUTONEG_ENABLE) {
  1631. if (netif_running(dev))
  1632. t3_link_start(&p->phy, &p->mac, lc);
  1633. } else {
  1634. lc->fc = lc->requested_fc & (PAUSE_RX | PAUSE_TX);
  1635. if (netif_running(dev))
  1636. t3_mac_set_speed_duplex_fc(&p->mac, -1, -1, lc->fc);
  1637. }
  1638. return 0;
  1639. }
  1640. static void get_sge_param(struct net_device *dev, struct ethtool_ringparam *e)
  1641. {
  1642. struct port_info *pi = netdev_priv(dev);
  1643. struct adapter *adapter = pi->adapter;
  1644. const struct qset_params *q = &adapter->params.sge.qset[pi->first_qset];
  1645. e->rx_max_pending = MAX_RX_BUFFERS;
  1646. e->rx_jumbo_max_pending = MAX_RX_JUMBO_BUFFERS;
  1647. e->tx_max_pending = MAX_TXQ_ENTRIES;
  1648. e->rx_pending = q->fl_size;
  1649. e->rx_mini_pending = q->rspq_size;
  1650. e->rx_jumbo_pending = q->jumbo_size;
  1651. e->tx_pending = q->txq_size[0];
  1652. }
  1653. static int set_sge_param(struct net_device *dev, struct ethtool_ringparam *e)
  1654. {
  1655. struct port_info *pi = netdev_priv(dev);
  1656. struct adapter *adapter = pi->adapter;
  1657. struct qset_params *q;
  1658. int i;
  1659. if (e->rx_pending > MAX_RX_BUFFERS ||
  1660. e->rx_jumbo_pending > MAX_RX_JUMBO_BUFFERS ||
  1661. e->tx_pending > MAX_TXQ_ENTRIES ||
  1662. e->rx_mini_pending > MAX_RSPQ_ENTRIES ||
  1663. e->rx_mini_pending < MIN_RSPQ_ENTRIES ||
  1664. e->rx_pending < MIN_FL_ENTRIES ||
  1665. e->rx_jumbo_pending < MIN_FL_ENTRIES ||
  1666. e->tx_pending < adapter->params.nports * MIN_TXQ_ENTRIES)
  1667. return -EINVAL;
  1668. if (adapter->flags & FULL_INIT_DONE)
  1669. return -EBUSY;
  1670. q = &adapter->params.sge.qset[pi->first_qset];
  1671. for (i = 0; i < pi->nqsets; ++i, ++q) {
  1672. q->rspq_size = e->rx_mini_pending;
  1673. q->fl_size = e->rx_pending;
  1674. q->jumbo_size = e->rx_jumbo_pending;
  1675. q->txq_size[0] = e->tx_pending;
  1676. q->txq_size[1] = e->tx_pending;
  1677. q->txq_size[2] = e->tx_pending;
  1678. }
  1679. return 0;
  1680. }
  1681. static int set_coalesce(struct net_device *dev, struct ethtool_coalesce *c)
  1682. {
  1683. struct port_info *pi = netdev_priv(dev);
  1684. struct adapter *adapter = pi->adapter;
  1685. struct qset_params *qsp;
  1686. struct sge_qset *qs;
  1687. int i;
  1688. if (c->rx_coalesce_usecs * 10 > M_NEWTIMER)
  1689. return -EINVAL;
  1690. for (i = 0; i < pi->nqsets; i++) {
  1691. qsp = &adapter->params.sge.qset[i];
  1692. qs = &adapter->sge.qs[i];
  1693. qsp->coalesce_usecs = c->rx_coalesce_usecs;
  1694. t3_update_qset_coalesce(qs, qsp);
  1695. }
  1696. return 0;
  1697. }
  1698. static int get_coalesce(struct net_device *dev, struct ethtool_coalesce *c)
  1699. {
  1700. struct port_info *pi = netdev_priv(dev);
  1701. struct adapter *adapter = pi->adapter;
  1702. struct qset_params *q = adapter->params.sge.qset;
  1703. c->rx_coalesce_usecs = q->coalesce_usecs;
  1704. return 0;
  1705. }
  1706. static int get_eeprom(struct net_device *dev, struct ethtool_eeprom *e,
  1707. u8 * data)
  1708. {
  1709. struct port_info *pi = netdev_priv(dev);
  1710. struct adapter *adapter = pi->adapter;
  1711. int i, err = 0;
  1712. u8 *buf = kmalloc(EEPROMSIZE, GFP_KERNEL);
  1713. if (!buf)
  1714. return -ENOMEM;
  1715. e->magic = EEPROM_MAGIC;
  1716. for (i = e->offset & ~3; !err && i < e->offset + e->len; i += 4)
  1717. err = t3_seeprom_read(adapter, i, (__le32 *) & buf[i]);
  1718. if (!err)
  1719. memcpy(data, buf + e->offset, e->len);
  1720. kfree(buf);
  1721. return err;
  1722. }
  1723. static int set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
  1724. u8 * data)
  1725. {
  1726. struct port_info *pi = netdev_priv(dev);
  1727. struct adapter *adapter = pi->adapter;
  1728. u32 aligned_offset, aligned_len;
  1729. __le32 *p;
  1730. u8 *buf;
  1731. int err;
  1732. if (eeprom->magic != EEPROM_MAGIC)
  1733. return -EINVAL;
  1734. aligned_offset = eeprom->offset & ~3;
  1735. aligned_len = (eeprom->len + (eeprom->offset & 3) + 3) & ~3;
  1736. if (aligned_offset != eeprom->offset || aligned_len != eeprom->len) {
  1737. buf = kmalloc(aligned_len, GFP_KERNEL);
  1738. if (!buf)
  1739. return -ENOMEM;
  1740. err = t3_seeprom_read(adapter, aligned_offset, (__le32 *) buf);
  1741. if (!err && aligned_len > 4)
  1742. err = t3_seeprom_read(adapter,
  1743. aligned_offset + aligned_len - 4,
  1744. (__le32 *) & buf[aligned_len - 4]);
  1745. if (err)
  1746. goto out;
  1747. memcpy(buf + (eeprom->offset & 3), data, eeprom->len);
  1748. } else
  1749. buf = data;
  1750. err = t3_seeprom_wp(adapter, 0);
  1751. if (err)
  1752. goto out;
  1753. for (p = (__le32 *) buf; !err && aligned_len; aligned_len -= 4, p++) {
  1754. err = t3_seeprom_write(adapter, aligned_offset, *p);
  1755. aligned_offset += 4;
  1756. }
  1757. if (!err)
  1758. err = t3_seeprom_wp(adapter, 1);
  1759. out:
  1760. if (buf != data)
  1761. kfree(buf);
  1762. return err;
  1763. }
  1764. static void get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1765. {
  1766. wol->supported = 0;
  1767. wol->wolopts = 0;
  1768. memset(&wol->sopass, 0, sizeof(wol->sopass));
  1769. }
  1770. static const struct ethtool_ops cxgb_ethtool_ops = {
  1771. .get_settings = get_settings,
  1772. .set_settings = set_settings,
  1773. .get_drvinfo = get_drvinfo,
  1774. .get_msglevel = get_msglevel,
  1775. .set_msglevel = set_msglevel,
  1776. .get_ringparam = get_sge_param,
  1777. .set_ringparam = set_sge_param,
  1778. .get_coalesce = get_coalesce,
  1779. .set_coalesce = set_coalesce,
  1780. .get_eeprom_len = get_eeprom_len,
  1781. .get_eeprom = get_eeprom,
  1782. .set_eeprom = set_eeprom,
  1783. .get_pauseparam = get_pauseparam,
  1784. .set_pauseparam = set_pauseparam,
  1785. .get_link = ethtool_op_get_link,
  1786. .get_strings = get_strings,
  1787. .set_phys_id = set_phys_id,
  1788. .nway_reset = restart_autoneg,
  1789. .get_sset_count = get_sset_count,
  1790. .get_ethtool_stats = get_stats,
  1791. .get_regs_len = get_regs_len,
  1792. .get_regs = get_regs,
  1793. .get_wol = get_wol,
  1794. };
  1795. static int in_range(int val, int lo, int hi)
  1796. {
  1797. return val < 0 || (val <= hi && val >= lo);
  1798. }
  1799. static int cxgb_extension_ioctl(struct net_device *dev, void __user *useraddr)
  1800. {
  1801. struct port_info *pi = netdev_priv(dev);
  1802. struct adapter *adapter = pi->adapter;
  1803. u32 cmd;
  1804. int ret;
  1805. if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
  1806. return -EFAULT;
  1807. switch (cmd) {
  1808. case CHELSIO_SET_QSET_PARAMS:{
  1809. int i;
  1810. struct qset_params *q;
  1811. struct ch_qset_params t;
  1812. int q1 = pi->first_qset;
  1813. int nqsets = pi->nqsets;
  1814. if (!capable(CAP_NET_ADMIN))
  1815. return -EPERM;
  1816. if (copy_from_user(&t, useraddr, sizeof(t)))
  1817. return -EFAULT;
  1818. if (t.qset_idx >= SGE_QSETS)
  1819. return -EINVAL;
  1820. if (!in_range(t.intr_lat, 0, M_NEWTIMER) ||
  1821. !in_range(t.cong_thres, 0, 255) ||
  1822. !in_range(t.txq_size[0], MIN_TXQ_ENTRIES,
  1823. MAX_TXQ_ENTRIES) ||
  1824. !in_range(t.txq_size[1], MIN_TXQ_ENTRIES,
  1825. MAX_TXQ_ENTRIES) ||
  1826. !in_range(t.txq_size[2], MIN_CTRL_TXQ_ENTRIES,
  1827. MAX_CTRL_TXQ_ENTRIES) ||
  1828. !in_range(t.fl_size[0], MIN_FL_ENTRIES,
  1829. MAX_RX_BUFFERS) ||
  1830. !in_range(t.fl_size[1], MIN_FL_ENTRIES,
  1831. MAX_RX_JUMBO_BUFFERS) ||
  1832. !in_range(t.rspq_size, MIN_RSPQ_ENTRIES,
  1833. MAX_RSPQ_ENTRIES))
  1834. return -EINVAL;
  1835. if ((adapter->flags & FULL_INIT_DONE) &&
  1836. (t.rspq_size >= 0 || t.fl_size[0] >= 0 ||
  1837. t.fl_size[1] >= 0 || t.txq_size[0] >= 0 ||
  1838. t.txq_size[1] >= 0 || t.txq_size[2] >= 0 ||
  1839. t.polling >= 0 || t.cong_thres >= 0))
  1840. return -EBUSY;
  1841. /* Allow setting of any available qset when offload enabled */
  1842. if (test_bit(OFFLOAD_DEVMAP_BIT, &adapter->open_device_map)) {
  1843. q1 = 0;
  1844. for_each_port(adapter, i) {
  1845. pi = adap2pinfo(adapter, i);
  1846. nqsets += pi->first_qset + pi->nqsets;
  1847. }
  1848. }
  1849. if (t.qset_idx < q1)
  1850. return -EINVAL;
  1851. if (t.qset_idx > q1 + nqsets - 1)
  1852. return -EINVAL;
  1853. q = &adapter->params.sge.qset[t.qset_idx];
  1854. if (t.rspq_size >= 0)
  1855. q->rspq_size = t.rspq_size;
  1856. if (t.fl_size[0] >= 0)
  1857. q->fl_size = t.fl_size[0];
  1858. if (t.fl_size[1] >= 0)
  1859. q->jumbo_size = t.fl_size[1];
  1860. if (t.txq_size[0] >= 0)
  1861. q->txq_size[0] = t.txq_size[0];
  1862. if (t.txq_size[1] >= 0)
  1863. q->txq_size[1] = t.txq_size[1];
  1864. if (t.txq_size[2] >= 0)
  1865. q->txq_size[2] = t.txq_size[2];
  1866. if (t.cong_thres >= 0)
  1867. q->cong_thres = t.cong_thres;
  1868. if (t.intr_lat >= 0) {
  1869. struct sge_qset *qs =
  1870. &adapter->sge.qs[t.qset_idx];
  1871. q->coalesce_usecs = t.intr_lat;
  1872. t3_update_qset_coalesce(qs, q);
  1873. }
  1874. if (t.polling >= 0) {
  1875. if (adapter->flags & USING_MSIX)
  1876. q->polling = t.polling;
  1877. else {
  1878. /* No polling with INTx for T3A */
  1879. if (adapter->params.rev == 0 &&
  1880. !(adapter->flags & USING_MSI))
  1881. t.polling = 0;
  1882. for (i = 0; i < SGE_QSETS; i++) {
  1883. q = &adapter->params.sge.
  1884. qset[i];
  1885. q->polling = t.polling;
  1886. }
  1887. }
  1888. }
  1889. if (t.lro >= 0) {
  1890. if (t.lro)
  1891. dev->wanted_features |= NETIF_F_GRO;
  1892. else
  1893. dev->wanted_features &= ~NETIF_F_GRO;
  1894. netdev_update_features(dev);
  1895. }
  1896. break;
  1897. }
  1898. case CHELSIO_GET_QSET_PARAMS:{
  1899. struct qset_params *q;
  1900. struct ch_qset_params t;
  1901. int q1 = pi->first_qset;
  1902. int nqsets = pi->nqsets;
  1903. int i;
  1904. if (copy_from_user(&t, useraddr, sizeof(t)))
  1905. return -EFAULT;
  1906. /* Display qsets for all ports when offload enabled */
  1907. if (test_bit(OFFLOAD_DEVMAP_BIT, &adapter->open_device_map)) {
  1908. q1 = 0;
  1909. for_each_port(adapter, i) {
  1910. pi = adap2pinfo(adapter, i);
  1911. nqsets = pi->first_qset + pi->nqsets;
  1912. }
  1913. }
  1914. if (t.qset_idx >= nqsets)
  1915. return -EINVAL;
  1916. t.qset_idx = array_index_nospec(t.qset_idx, nqsets);
  1917. q = &adapter->params.sge.qset[q1 + t.qset_idx];
  1918. t.rspq_size = q->rspq_size;
  1919. t.txq_size[0] = q->txq_size[0];
  1920. t.txq_size[1] = q->txq_size[1];
  1921. t.txq_size[2] = q->txq_size[2];
  1922. t.fl_size[0] = q->fl_size;
  1923. t.fl_size[1] = q->jumbo_size;
  1924. t.polling = q->polling;
  1925. t.lro = !!(dev->features & NETIF_F_GRO);
  1926. t.intr_lat = q->coalesce_usecs;
  1927. t.cong_thres = q->cong_thres;
  1928. t.qnum = q1;
  1929. if (adapter->flags & USING_MSIX)
  1930. t.vector = adapter->msix_info[q1 + t.qset_idx + 1].vec;
  1931. else
  1932. t.vector = adapter->pdev->irq;
  1933. if (copy_to_user(useraddr, &t, sizeof(t)))
  1934. return -EFAULT;
  1935. break;
  1936. }
  1937. case CHELSIO_SET_QSET_NUM:{
  1938. struct ch_reg edata;
  1939. unsigned int i, first_qset = 0, other_qsets = 0;
  1940. if (!capable(CAP_NET_ADMIN))
  1941. return -EPERM;
  1942. if (adapter->flags & FULL_INIT_DONE)
  1943. return -EBUSY;
  1944. if (copy_from_user(&edata, useraddr, sizeof(edata)))
  1945. return -EFAULT;
  1946. if (edata.val < 1 ||
  1947. (edata.val > 1 && !(adapter->flags & USING_MSIX)))
  1948. return -EINVAL;
  1949. for_each_port(adapter, i)
  1950. if (adapter->port[i] && adapter->port[i] != dev)
  1951. other_qsets += adap2pinfo(adapter, i)->nqsets;
  1952. if (edata.val + other_qsets > SGE_QSETS)
  1953. return -EINVAL;
  1954. pi->nqsets = edata.val;
  1955. for_each_port(adapter, i)
  1956. if (adapter->port[i]) {
  1957. pi = adap2pinfo(adapter, i);
  1958. pi->first_qset = first_qset;
  1959. first_qset += pi->nqsets;
  1960. }
  1961. break;
  1962. }
  1963. case CHELSIO_GET_QSET_NUM:{
  1964. struct ch_reg edata;
  1965. memset(&edata, 0, sizeof(struct ch_reg));
  1966. edata.cmd = CHELSIO_GET_QSET_NUM;
  1967. edata.val = pi->nqsets;
  1968. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  1969. return -EFAULT;
  1970. break;
  1971. }
  1972. case CHELSIO_LOAD_FW:{
  1973. u8 *fw_data;
  1974. struct ch_mem_range t;
  1975. if (!capable(CAP_SYS_RAWIO))
  1976. return -EPERM;
  1977. if (copy_from_user(&t, useraddr, sizeof(t)))
  1978. return -EFAULT;
  1979. /* Check t.len sanity ? */
  1980. fw_data = memdup_user(useraddr + sizeof(t), t.len);
  1981. if (IS_ERR(fw_data))
  1982. return PTR_ERR(fw_data);
  1983. ret = t3_load_fw(adapter, fw_data, t.len);
  1984. kfree(fw_data);
  1985. if (ret)
  1986. return ret;
  1987. break;
  1988. }
  1989. case CHELSIO_SETMTUTAB:{
  1990. struct ch_mtus m;
  1991. int i;
  1992. if (!is_offload(adapter))
  1993. return -EOPNOTSUPP;
  1994. if (!capable(CAP_NET_ADMIN))
  1995. return -EPERM;
  1996. if (offload_running(adapter))
  1997. return -EBUSY;
  1998. if (copy_from_user(&m, useraddr, sizeof(m)))
  1999. return -EFAULT;
  2000. if (m.nmtus != NMTUS)
  2001. return -EINVAL;
  2002. if (m.mtus[0] < 81) /* accommodate SACK */
  2003. return -EINVAL;
  2004. /* MTUs must be in ascending order */
  2005. for (i = 1; i < NMTUS; ++i)
  2006. if (m.mtus[i] < m.mtus[i - 1])
  2007. return -EINVAL;
  2008. memcpy(adapter->params.mtus, m.mtus,
  2009. sizeof(adapter->params.mtus));
  2010. break;
  2011. }
  2012. case CHELSIO_GET_PM:{
  2013. struct tp_params *p = &adapter->params.tp;
  2014. struct ch_pm m = {.cmd = CHELSIO_GET_PM };
  2015. if (!is_offload(adapter))
  2016. return -EOPNOTSUPP;
  2017. m.tx_pg_sz = p->tx_pg_size;
  2018. m.tx_num_pg = p->tx_num_pgs;
  2019. m.rx_pg_sz = p->rx_pg_size;
  2020. m.rx_num_pg = p->rx_num_pgs;
  2021. m.pm_total = p->pmtx_size + p->chan_rx_size * p->nchan;
  2022. if (copy_to_user(useraddr, &m, sizeof(m)))
  2023. return -EFAULT;
  2024. break;
  2025. }
  2026. case CHELSIO_SET_PM:{
  2027. struct ch_pm m;
  2028. struct tp_params *p = &adapter->params.tp;
  2029. if (!is_offload(adapter))
  2030. return -EOPNOTSUPP;
  2031. if (!capable(CAP_NET_ADMIN))
  2032. return -EPERM;
  2033. if (adapter->flags & FULL_INIT_DONE)
  2034. return -EBUSY;
  2035. if (copy_from_user(&m, useraddr, sizeof(m)))
  2036. return -EFAULT;
  2037. if (!is_power_of_2(m.rx_pg_sz) ||
  2038. !is_power_of_2(m.tx_pg_sz))
  2039. return -EINVAL; /* not power of 2 */
  2040. if (!(m.rx_pg_sz & 0x14000))
  2041. return -EINVAL; /* not 16KB or 64KB */
  2042. if (!(m.tx_pg_sz & 0x1554000))
  2043. return -EINVAL;
  2044. if (m.tx_num_pg == -1)
  2045. m.tx_num_pg = p->tx_num_pgs;
  2046. if (m.rx_num_pg == -1)
  2047. m.rx_num_pg = p->rx_num_pgs;
  2048. if (m.tx_num_pg % 24 || m.rx_num_pg % 24)
  2049. return -EINVAL;
  2050. if (m.rx_num_pg * m.rx_pg_sz > p->chan_rx_size ||
  2051. m.tx_num_pg * m.tx_pg_sz > p->chan_tx_size)
  2052. return -EINVAL;
  2053. p->rx_pg_size = m.rx_pg_sz;
  2054. p->tx_pg_size = m.tx_pg_sz;
  2055. p->rx_num_pgs = m.rx_num_pg;
  2056. p->tx_num_pgs = m.tx_num_pg;
  2057. break;
  2058. }
  2059. case CHELSIO_GET_MEM:{
  2060. struct ch_mem_range t;
  2061. struct mc7 *mem;
  2062. u64 buf[32];
  2063. if (!is_offload(adapter))
  2064. return -EOPNOTSUPP;
  2065. if (!(adapter->flags & FULL_INIT_DONE))
  2066. return -EIO; /* need the memory controllers */
  2067. if (copy_from_user(&t, useraddr, sizeof(t)))
  2068. return -EFAULT;
  2069. if ((t.addr & 7) || (t.len & 7))
  2070. return -EINVAL;
  2071. if (t.mem_id == MEM_CM)
  2072. mem = &adapter->cm;
  2073. else if (t.mem_id == MEM_PMRX)
  2074. mem = &adapter->pmrx;
  2075. else if (t.mem_id == MEM_PMTX)
  2076. mem = &adapter->pmtx;
  2077. else
  2078. return -EINVAL;
  2079. /*
  2080. * Version scheme:
  2081. * bits 0..9: chip version
  2082. * bits 10..15: chip revision
  2083. */
  2084. t.version = 3 | (adapter->params.rev << 10);
  2085. if (copy_to_user(useraddr, &t, sizeof(t)))
  2086. return -EFAULT;
  2087. /*
  2088. * Read 256 bytes at a time as len can be large and we don't
  2089. * want to use huge intermediate buffers.
  2090. */
  2091. useraddr += sizeof(t); /* advance to start of buffer */
  2092. while (t.len) {
  2093. unsigned int chunk =
  2094. min_t(unsigned int, t.len, sizeof(buf));
  2095. ret =
  2096. t3_mc7_bd_read(mem, t.addr / 8, chunk / 8,
  2097. buf);
  2098. if (ret)
  2099. return ret;
  2100. if (copy_to_user(useraddr, buf, chunk))
  2101. return -EFAULT;
  2102. useraddr += chunk;
  2103. t.addr += chunk;
  2104. t.len -= chunk;
  2105. }
  2106. break;
  2107. }
  2108. case CHELSIO_SET_TRACE_FILTER:{
  2109. struct ch_trace t;
  2110. const struct trace_params *tp;
  2111. if (!capable(CAP_NET_ADMIN))
  2112. return -EPERM;
  2113. if (!offload_running(adapter))
  2114. return -EAGAIN;
  2115. if (copy_from_user(&t, useraddr, sizeof(t)))
  2116. return -EFAULT;
  2117. tp = (const struct trace_params *)&t.sip;
  2118. if (t.config_tx)
  2119. t3_config_trace_filter(adapter, tp, 0,
  2120. t.invert_match,
  2121. t.trace_tx);
  2122. if (t.config_rx)
  2123. t3_config_trace_filter(adapter, tp, 1,
  2124. t.invert_match,
  2125. t.trace_rx);
  2126. break;
  2127. }
  2128. default:
  2129. return -EOPNOTSUPP;
  2130. }
  2131. return 0;
  2132. }
  2133. static int cxgb_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
  2134. {
  2135. struct mii_ioctl_data *data = if_mii(req);
  2136. struct port_info *pi = netdev_priv(dev);
  2137. struct adapter *adapter = pi->adapter;
  2138. switch (cmd) {
  2139. case SIOCGMIIREG:
  2140. case SIOCSMIIREG:
  2141. /* Convert phy_id from older PRTAD/DEVAD format */
  2142. if (is_10G(adapter) &&
  2143. !mdio_phy_id_is_c45(data->phy_id) &&
  2144. (data->phy_id & 0x1f00) &&
  2145. !(data->phy_id & 0xe0e0))
  2146. data->phy_id = mdio_phy_id_c45(data->phy_id >> 8,
  2147. data->phy_id & 0x1f);
  2148. /* FALLTHRU */
  2149. case SIOCGMIIPHY:
  2150. return mdio_mii_ioctl(&pi->phy.mdio, data, cmd);
  2151. case SIOCCHIOCTL:
  2152. return cxgb_extension_ioctl(dev, req->ifr_data);
  2153. default:
  2154. return -EOPNOTSUPP;
  2155. }
  2156. }
  2157. static int cxgb_change_mtu(struct net_device *dev, int new_mtu)
  2158. {
  2159. struct port_info *pi = netdev_priv(dev);
  2160. struct adapter *adapter = pi->adapter;
  2161. int ret;
  2162. if (new_mtu < 81) /* accommodate SACK */
  2163. return -EINVAL;
  2164. if ((ret = t3_mac_set_mtu(&pi->mac, new_mtu)))
  2165. return ret;
  2166. dev->mtu = new_mtu;
  2167. init_port_mtus(adapter);
  2168. if (adapter->params.rev == 0 && offload_running(adapter))
  2169. t3_load_mtus(adapter, adapter->params.mtus,
  2170. adapter->params.a_wnd, adapter->params.b_wnd,
  2171. adapter->port[0]->mtu);
  2172. return 0;
  2173. }
  2174. static int cxgb_set_mac_addr(struct net_device *dev, void *p)
  2175. {
  2176. struct port_info *pi = netdev_priv(dev);
  2177. struct adapter *adapter = pi->adapter;
  2178. struct sockaddr *addr = p;
  2179. if (!is_valid_ether_addr(addr->sa_data))
  2180. return -EADDRNOTAVAIL;
  2181. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  2182. t3_mac_set_address(&pi->mac, LAN_MAC_IDX, dev->dev_addr);
  2183. if (offload_running(adapter))
  2184. write_smt_entry(adapter, pi->port_id);
  2185. return 0;
  2186. }
  2187. static netdev_features_t cxgb_fix_features(struct net_device *dev,
  2188. netdev_features_t features)
  2189. {
  2190. /*
  2191. * Since there is no support for separate rx/tx vlan accel
  2192. * enable/disable make sure tx flag is always in same state as rx.
  2193. */
  2194. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  2195. features |= NETIF_F_HW_VLAN_CTAG_TX;
  2196. else
  2197. features &= ~NETIF_F_HW_VLAN_CTAG_TX;
  2198. return features;
  2199. }
  2200. static int cxgb_set_features(struct net_device *dev, netdev_features_t features)
  2201. {
  2202. netdev_features_t changed = dev->features ^ features;
  2203. if (changed & NETIF_F_HW_VLAN_CTAG_RX)
  2204. cxgb_vlan_mode(dev, features);
  2205. return 0;
  2206. }
  2207. #ifdef CONFIG_NET_POLL_CONTROLLER
  2208. static void cxgb_netpoll(struct net_device *dev)
  2209. {
  2210. struct port_info *pi = netdev_priv(dev);
  2211. struct adapter *adapter = pi->adapter;
  2212. int qidx;
  2213. for (qidx = pi->first_qset; qidx < pi->first_qset + pi->nqsets; qidx++) {
  2214. struct sge_qset *qs = &adapter->sge.qs[qidx];
  2215. void *source;
  2216. if (adapter->flags & USING_MSIX)
  2217. source = qs;
  2218. else
  2219. source = adapter;
  2220. t3_intr_handler(adapter, qs->rspq.polling) (0, source);
  2221. }
  2222. }
  2223. #endif
  2224. /*
  2225. * Periodic accumulation of MAC statistics.
  2226. */
  2227. static void mac_stats_update(struct adapter *adapter)
  2228. {
  2229. int i;
  2230. for_each_port(adapter, i) {
  2231. struct net_device *dev = adapter->port[i];
  2232. struct port_info *p = netdev_priv(dev);
  2233. if (netif_running(dev)) {
  2234. spin_lock(&adapter->stats_lock);
  2235. t3_mac_update_stats(&p->mac);
  2236. spin_unlock(&adapter->stats_lock);
  2237. }
  2238. }
  2239. }
  2240. static void check_link_status(struct adapter *adapter)
  2241. {
  2242. int i;
  2243. for_each_port(adapter, i) {
  2244. struct net_device *dev = adapter->port[i];
  2245. struct port_info *p = netdev_priv(dev);
  2246. int link_fault;
  2247. spin_lock_irq(&adapter->work_lock);
  2248. link_fault = p->link_fault;
  2249. spin_unlock_irq(&adapter->work_lock);
  2250. if (link_fault) {
  2251. t3_link_fault(adapter, i);
  2252. continue;
  2253. }
  2254. if (!(p->phy.caps & SUPPORTED_IRQ) && netif_running(dev)) {
  2255. t3_xgm_intr_disable(adapter, i);
  2256. t3_read_reg(adapter, A_XGM_INT_STATUS + p->mac.offset);
  2257. t3_link_changed(adapter, i);
  2258. t3_xgm_intr_enable(adapter, i);
  2259. }
  2260. }
  2261. }
  2262. static void check_t3b2_mac(struct adapter *adapter)
  2263. {
  2264. int i;
  2265. if (!rtnl_trylock()) /* synchronize with ifdown */
  2266. return;
  2267. for_each_port(adapter, i) {
  2268. struct net_device *dev = adapter->port[i];
  2269. struct port_info *p = netdev_priv(dev);
  2270. int status;
  2271. if (!netif_running(dev))
  2272. continue;
  2273. status = 0;
  2274. if (netif_running(dev) && netif_carrier_ok(dev))
  2275. status = t3b2_mac_watchdog_task(&p->mac);
  2276. if (status == 1)
  2277. p->mac.stats.num_toggled++;
  2278. else if (status == 2) {
  2279. struct cmac *mac = &p->mac;
  2280. t3_mac_set_mtu(mac, dev->mtu);
  2281. t3_mac_set_address(mac, LAN_MAC_IDX, dev->dev_addr);
  2282. cxgb_set_rxmode(dev);
  2283. t3_link_start(&p->phy, mac, &p->link_config);
  2284. t3_mac_enable(mac, MAC_DIRECTION_RX | MAC_DIRECTION_TX);
  2285. t3_port_intr_enable(adapter, p->port_id);
  2286. p->mac.stats.num_resets++;
  2287. }
  2288. }
  2289. rtnl_unlock();
  2290. }
  2291. static void t3_adap_check_task(struct work_struct *work)
  2292. {
  2293. struct adapter *adapter = container_of(work, struct adapter,
  2294. adap_check_task.work);
  2295. const struct adapter_params *p = &adapter->params;
  2296. int port;
  2297. unsigned int v, status, reset;
  2298. adapter->check_task_cnt++;
  2299. check_link_status(adapter);
  2300. /* Accumulate MAC stats if needed */
  2301. if (!p->linkpoll_period ||
  2302. (adapter->check_task_cnt * p->linkpoll_period) / 10 >=
  2303. p->stats_update_period) {
  2304. mac_stats_update(adapter);
  2305. adapter->check_task_cnt = 0;
  2306. }
  2307. if (p->rev == T3_REV_B2)
  2308. check_t3b2_mac(adapter);
  2309. /*
  2310. * Scan the XGMAC's to check for various conditions which we want to
  2311. * monitor in a periodic polling manner rather than via an interrupt
  2312. * condition. This is used for conditions which would otherwise flood
  2313. * the system with interrupts and we only really need to know that the
  2314. * conditions are "happening" ... For each condition we count the
  2315. * detection of the condition and reset it for the next polling loop.
  2316. */
  2317. for_each_port(adapter, port) {
  2318. struct cmac *mac = &adap2pinfo(adapter, port)->mac;
  2319. u32 cause;
  2320. cause = t3_read_reg(adapter, A_XGM_INT_CAUSE + mac->offset);
  2321. reset = 0;
  2322. if (cause & F_RXFIFO_OVERFLOW) {
  2323. mac->stats.rx_fifo_ovfl++;
  2324. reset |= F_RXFIFO_OVERFLOW;
  2325. }
  2326. t3_write_reg(adapter, A_XGM_INT_CAUSE + mac->offset, reset);
  2327. }
  2328. /*
  2329. * We do the same as above for FL_EMPTY interrupts.
  2330. */
  2331. status = t3_read_reg(adapter, A_SG_INT_CAUSE);
  2332. reset = 0;
  2333. if (status & F_FLEMPTY) {
  2334. struct sge_qset *qs = &adapter->sge.qs[0];
  2335. int i = 0;
  2336. reset |= F_FLEMPTY;
  2337. v = (t3_read_reg(adapter, A_SG_RSPQ_FL_STATUS) >> S_FL0EMPTY) &
  2338. 0xffff;
  2339. while (v) {
  2340. qs->fl[i].empty += (v & 1);
  2341. if (i)
  2342. qs++;
  2343. i ^= 1;
  2344. v >>= 1;
  2345. }
  2346. }
  2347. t3_write_reg(adapter, A_SG_INT_CAUSE, reset);
  2348. /* Schedule the next check update if any port is active. */
  2349. spin_lock_irq(&adapter->work_lock);
  2350. if (adapter->open_device_map & PORT_MASK)
  2351. schedule_chk_task(adapter);
  2352. spin_unlock_irq(&adapter->work_lock);
  2353. }
  2354. static void db_full_task(struct work_struct *work)
  2355. {
  2356. struct adapter *adapter = container_of(work, struct adapter,
  2357. db_full_task);
  2358. cxgb3_event_notify(&adapter->tdev, OFFLOAD_DB_FULL, 0);
  2359. }
  2360. static void db_empty_task(struct work_struct *work)
  2361. {
  2362. struct adapter *adapter = container_of(work, struct adapter,
  2363. db_empty_task);
  2364. cxgb3_event_notify(&adapter->tdev, OFFLOAD_DB_EMPTY, 0);
  2365. }
  2366. static void db_drop_task(struct work_struct *work)
  2367. {
  2368. struct adapter *adapter = container_of(work, struct adapter,
  2369. db_drop_task);
  2370. unsigned long delay = 1000;
  2371. unsigned short r;
  2372. cxgb3_event_notify(&adapter->tdev, OFFLOAD_DB_DROP, 0);
  2373. /*
  2374. * Sleep a while before ringing the driver qset dbs.
  2375. * The delay is between 1000-2023 usecs.
  2376. */
  2377. get_random_bytes(&r, 2);
  2378. delay += r & 1023;
  2379. set_current_state(TASK_UNINTERRUPTIBLE);
  2380. schedule_timeout(usecs_to_jiffies(delay));
  2381. ring_dbs(adapter);
  2382. }
  2383. /*
  2384. * Processes external (PHY) interrupts in process context.
  2385. */
  2386. static void ext_intr_task(struct work_struct *work)
  2387. {
  2388. struct adapter *adapter = container_of(work, struct adapter,
  2389. ext_intr_handler_task);
  2390. int i;
  2391. /* Disable link fault interrupts */
  2392. for_each_port(adapter, i) {
  2393. struct net_device *dev = adapter->port[i];
  2394. struct port_info *p = netdev_priv(dev);
  2395. t3_xgm_intr_disable(adapter, i);
  2396. t3_read_reg(adapter, A_XGM_INT_STATUS + p->mac.offset);
  2397. }
  2398. /* Re-enable link fault interrupts */
  2399. t3_phy_intr_handler(adapter);
  2400. for_each_port(adapter, i)
  2401. t3_xgm_intr_enable(adapter, i);
  2402. /* Now reenable external interrupts */
  2403. spin_lock_irq(&adapter->work_lock);
  2404. if (adapter->slow_intr_mask) {
  2405. adapter->slow_intr_mask |= F_T3DBG;
  2406. t3_write_reg(adapter, A_PL_INT_CAUSE0, F_T3DBG);
  2407. t3_write_reg(adapter, A_PL_INT_ENABLE0,
  2408. adapter->slow_intr_mask);
  2409. }
  2410. spin_unlock_irq(&adapter->work_lock);
  2411. }
  2412. /*
  2413. * Interrupt-context handler for external (PHY) interrupts.
  2414. */
  2415. void t3_os_ext_intr_handler(struct adapter *adapter)
  2416. {
  2417. /*
  2418. * Schedule a task to handle external interrupts as they may be slow
  2419. * and we use a mutex to protect MDIO registers. We disable PHY
  2420. * interrupts in the meantime and let the task reenable them when
  2421. * it's done.
  2422. */
  2423. spin_lock(&adapter->work_lock);
  2424. if (adapter->slow_intr_mask) {
  2425. adapter->slow_intr_mask &= ~F_T3DBG;
  2426. t3_write_reg(adapter, A_PL_INT_ENABLE0,
  2427. adapter->slow_intr_mask);
  2428. queue_work(cxgb3_wq, &adapter->ext_intr_handler_task);
  2429. }
  2430. spin_unlock(&adapter->work_lock);
  2431. }
  2432. void t3_os_link_fault_handler(struct adapter *adapter, int port_id)
  2433. {
  2434. struct net_device *netdev = adapter->port[port_id];
  2435. struct port_info *pi = netdev_priv(netdev);
  2436. spin_lock(&adapter->work_lock);
  2437. pi->link_fault = 1;
  2438. spin_unlock(&adapter->work_lock);
  2439. }
  2440. static int t3_adapter_error(struct adapter *adapter, int reset, int on_wq)
  2441. {
  2442. int i, ret = 0;
  2443. if (is_offload(adapter) &&
  2444. test_bit(OFFLOAD_DEVMAP_BIT, &adapter->open_device_map)) {
  2445. cxgb3_event_notify(&adapter->tdev, OFFLOAD_STATUS_DOWN, 0);
  2446. offload_close(&adapter->tdev);
  2447. }
  2448. /* Stop all ports */
  2449. for_each_port(adapter, i) {
  2450. struct net_device *netdev = adapter->port[i];
  2451. if (netif_running(netdev))
  2452. __cxgb_close(netdev, on_wq);
  2453. }
  2454. /* Stop SGE timers */
  2455. t3_stop_sge_timers(adapter);
  2456. adapter->flags &= ~FULL_INIT_DONE;
  2457. if (reset)
  2458. ret = t3_reset_adapter(adapter);
  2459. pci_disable_device(adapter->pdev);
  2460. return ret;
  2461. }
  2462. static int t3_reenable_adapter(struct adapter *adapter)
  2463. {
  2464. if (pci_enable_device(adapter->pdev)) {
  2465. dev_err(&adapter->pdev->dev,
  2466. "Cannot re-enable PCI device after reset.\n");
  2467. goto err;
  2468. }
  2469. pci_set_master(adapter->pdev);
  2470. pci_restore_state(adapter->pdev);
  2471. pci_save_state(adapter->pdev);
  2472. /* Free sge resources */
  2473. t3_free_sge_resources(adapter);
  2474. if (t3_replay_prep_adapter(adapter))
  2475. goto err;
  2476. return 0;
  2477. err:
  2478. return -1;
  2479. }
  2480. static void t3_resume_ports(struct adapter *adapter)
  2481. {
  2482. int i;
  2483. /* Restart the ports */
  2484. for_each_port(adapter, i) {
  2485. struct net_device *netdev = adapter->port[i];
  2486. if (netif_running(netdev)) {
  2487. if (cxgb_open(netdev)) {
  2488. dev_err(&adapter->pdev->dev,
  2489. "can't bring device back up"
  2490. " after reset\n");
  2491. continue;
  2492. }
  2493. }
  2494. }
  2495. if (is_offload(adapter) && !ofld_disable)
  2496. cxgb3_event_notify(&adapter->tdev, OFFLOAD_STATUS_UP, 0);
  2497. }
  2498. /*
  2499. * processes a fatal error.
  2500. * Bring the ports down, reset the chip, bring the ports back up.
  2501. */
  2502. static void fatal_error_task(struct work_struct *work)
  2503. {
  2504. struct adapter *adapter = container_of(work, struct adapter,
  2505. fatal_error_handler_task);
  2506. int err = 0;
  2507. rtnl_lock();
  2508. err = t3_adapter_error(adapter, 1, 1);
  2509. if (!err)
  2510. err = t3_reenable_adapter(adapter);
  2511. if (!err)
  2512. t3_resume_ports(adapter);
  2513. CH_ALERT(adapter, "adapter reset %s\n", err ? "failed" : "succeeded");
  2514. rtnl_unlock();
  2515. }
  2516. void t3_fatal_err(struct adapter *adapter)
  2517. {
  2518. unsigned int fw_status[4];
  2519. if (adapter->flags & FULL_INIT_DONE) {
  2520. t3_sge_stop(adapter);
  2521. t3_write_reg(adapter, A_XGM_TX_CTRL, 0);
  2522. t3_write_reg(adapter, A_XGM_RX_CTRL, 0);
  2523. t3_write_reg(adapter, XGM_REG(A_XGM_TX_CTRL, 1), 0);
  2524. t3_write_reg(adapter, XGM_REG(A_XGM_RX_CTRL, 1), 0);
  2525. spin_lock(&adapter->work_lock);
  2526. t3_intr_disable(adapter);
  2527. queue_work(cxgb3_wq, &adapter->fatal_error_handler_task);
  2528. spin_unlock(&adapter->work_lock);
  2529. }
  2530. CH_ALERT(adapter, "encountered fatal error, operation suspended\n");
  2531. if (!t3_cim_ctl_blk_read(adapter, 0xa0, 4, fw_status))
  2532. CH_ALERT(adapter, "FW status: 0x%x, 0x%x, 0x%x, 0x%x\n",
  2533. fw_status[0], fw_status[1],
  2534. fw_status[2], fw_status[3]);
  2535. }
  2536. /**
  2537. * t3_io_error_detected - called when PCI error is detected
  2538. * @pdev: Pointer to PCI device
  2539. * @state: The current pci connection state
  2540. *
  2541. * This function is called after a PCI bus error affecting
  2542. * this device has been detected.
  2543. */
  2544. static pci_ers_result_t t3_io_error_detected(struct pci_dev *pdev,
  2545. pci_channel_state_t state)
  2546. {
  2547. struct adapter *adapter = pci_get_drvdata(pdev);
  2548. if (state == pci_channel_io_perm_failure)
  2549. return PCI_ERS_RESULT_DISCONNECT;
  2550. t3_adapter_error(adapter, 0, 0);
  2551. /* Request a slot reset. */
  2552. return PCI_ERS_RESULT_NEED_RESET;
  2553. }
  2554. /**
  2555. * t3_io_slot_reset - called after the pci bus has been reset.
  2556. * @pdev: Pointer to PCI device
  2557. *
  2558. * Restart the card from scratch, as if from a cold-boot.
  2559. */
  2560. static pci_ers_result_t t3_io_slot_reset(struct pci_dev *pdev)
  2561. {
  2562. struct adapter *adapter = pci_get_drvdata(pdev);
  2563. if (!t3_reenable_adapter(adapter))
  2564. return PCI_ERS_RESULT_RECOVERED;
  2565. return PCI_ERS_RESULT_DISCONNECT;
  2566. }
  2567. /**
  2568. * t3_io_resume - called when traffic can start flowing again.
  2569. * @pdev: Pointer to PCI device
  2570. *
  2571. * This callback is called when the error recovery driver tells us that
  2572. * its OK to resume normal operation.
  2573. */
  2574. static void t3_io_resume(struct pci_dev *pdev)
  2575. {
  2576. struct adapter *adapter = pci_get_drvdata(pdev);
  2577. CH_ALERT(adapter, "adapter recovering, PEX ERR 0x%x\n",
  2578. t3_read_reg(adapter, A_PCIE_PEX_ERR));
  2579. rtnl_lock();
  2580. t3_resume_ports(adapter);
  2581. rtnl_unlock();
  2582. }
  2583. static const struct pci_error_handlers t3_err_handler = {
  2584. .error_detected = t3_io_error_detected,
  2585. .slot_reset = t3_io_slot_reset,
  2586. .resume = t3_io_resume,
  2587. };
  2588. /*
  2589. * Set the number of qsets based on the number of CPUs and the number of ports,
  2590. * not to exceed the number of available qsets, assuming there are enough qsets
  2591. * per port in HW.
  2592. */
  2593. static void set_nqsets(struct adapter *adap)
  2594. {
  2595. int i, j = 0;
  2596. int num_cpus = netif_get_num_default_rss_queues();
  2597. int hwports = adap->params.nports;
  2598. int nqsets = adap->msix_nvectors - 1;
  2599. if (adap->params.rev > 0 && adap->flags & USING_MSIX) {
  2600. if (hwports == 2 &&
  2601. (hwports * nqsets > SGE_QSETS ||
  2602. num_cpus >= nqsets / hwports))
  2603. nqsets /= hwports;
  2604. if (nqsets > num_cpus)
  2605. nqsets = num_cpus;
  2606. if (nqsets < 1 || hwports == 4)
  2607. nqsets = 1;
  2608. } else
  2609. nqsets = 1;
  2610. for_each_port(adap, i) {
  2611. struct port_info *pi = adap2pinfo(adap, i);
  2612. pi->first_qset = j;
  2613. pi->nqsets = nqsets;
  2614. j = pi->first_qset + nqsets;
  2615. dev_info(&adap->pdev->dev,
  2616. "Port %d using %d queue sets.\n", i, nqsets);
  2617. }
  2618. }
  2619. static int cxgb_enable_msix(struct adapter *adap)
  2620. {
  2621. struct msix_entry entries[SGE_QSETS + 1];
  2622. int vectors;
  2623. int i;
  2624. vectors = ARRAY_SIZE(entries);
  2625. for (i = 0; i < vectors; ++i)
  2626. entries[i].entry = i;
  2627. vectors = pci_enable_msix_range(adap->pdev, entries,
  2628. adap->params.nports + 1, vectors);
  2629. if (vectors < 0)
  2630. return vectors;
  2631. for (i = 0; i < vectors; ++i)
  2632. adap->msix_info[i].vec = entries[i].vector;
  2633. adap->msix_nvectors = vectors;
  2634. return 0;
  2635. }
  2636. static void print_port_info(struct adapter *adap, const struct adapter_info *ai)
  2637. {
  2638. static const char *pci_variant[] = {
  2639. "PCI", "PCI-X", "PCI-X ECC", "PCI-X 266", "PCI Express"
  2640. };
  2641. int i;
  2642. char buf[80];
  2643. if (is_pcie(adap))
  2644. snprintf(buf, sizeof(buf), "%s x%d",
  2645. pci_variant[adap->params.pci.variant],
  2646. adap->params.pci.width);
  2647. else
  2648. snprintf(buf, sizeof(buf), "%s %dMHz/%d-bit",
  2649. pci_variant[adap->params.pci.variant],
  2650. adap->params.pci.speed, adap->params.pci.width);
  2651. for_each_port(adap, i) {
  2652. struct net_device *dev = adap->port[i];
  2653. const struct port_info *pi = netdev_priv(dev);
  2654. if (!test_bit(i, &adap->registered_device_map))
  2655. continue;
  2656. netdev_info(dev, "%s %s %sNIC (rev %d) %s%s\n",
  2657. ai->desc, pi->phy.desc,
  2658. is_offload(adap) ? "R" : "", adap->params.rev, buf,
  2659. (adap->flags & USING_MSIX) ? " MSI-X" :
  2660. (adap->flags & USING_MSI) ? " MSI" : "");
  2661. if (adap->name == dev->name && adap->params.vpd.mclk)
  2662. pr_info("%s: %uMB CM, %uMB PMTX, %uMB PMRX, S/N: %s\n",
  2663. adap->name, t3_mc7_size(&adap->cm) >> 20,
  2664. t3_mc7_size(&adap->pmtx) >> 20,
  2665. t3_mc7_size(&adap->pmrx) >> 20,
  2666. adap->params.vpd.sn);
  2667. }
  2668. }
  2669. static const struct net_device_ops cxgb_netdev_ops = {
  2670. .ndo_open = cxgb_open,
  2671. .ndo_stop = cxgb_close,
  2672. .ndo_start_xmit = t3_eth_xmit,
  2673. .ndo_get_stats = cxgb_get_stats,
  2674. .ndo_validate_addr = eth_validate_addr,
  2675. .ndo_set_rx_mode = cxgb_set_rxmode,
  2676. .ndo_do_ioctl = cxgb_ioctl,
  2677. .ndo_change_mtu = cxgb_change_mtu,
  2678. .ndo_set_mac_address = cxgb_set_mac_addr,
  2679. .ndo_fix_features = cxgb_fix_features,
  2680. .ndo_set_features = cxgb_set_features,
  2681. #ifdef CONFIG_NET_POLL_CONTROLLER
  2682. .ndo_poll_controller = cxgb_netpoll,
  2683. #endif
  2684. };
  2685. static void cxgb3_init_iscsi_mac(struct net_device *dev)
  2686. {
  2687. struct port_info *pi = netdev_priv(dev);
  2688. memcpy(pi->iscsic.mac_addr, dev->dev_addr, ETH_ALEN);
  2689. pi->iscsic.mac_addr[3] |= 0x80;
  2690. }
  2691. #define TSO_FLAGS (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN)
  2692. #define VLAN_FEAT (NETIF_F_SG | NETIF_F_IP_CSUM | TSO_FLAGS | \
  2693. NETIF_F_IPV6_CSUM | NETIF_F_HIGHDMA)
  2694. static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  2695. {
  2696. int i, err, pci_using_dac = 0;
  2697. resource_size_t mmio_start, mmio_len;
  2698. const struct adapter_info *ai;
  2699. struct adapter *adapter = NULL;
  2700. struct port_info *pi;
  2701. pr_info_once("%s - version %s\n", DRV_DESC, DRV_VERSION);
  2702. if (!cxgb3_wq) {
  2703. cxgb3_wq = create_singlethread_workqueue(DRV_NAME);
  2704. if (!cxgb3_wq) {
  2705. pr_err("cannot initialize work queue\n");
  2706. return -ENOMEM;
  2707. }
  2708. }
  2709. err = pci_enable_device(pdev);
  2710. if (err) {
  2711. dev_err(&pdev->dev, "cannot enable PCI device\n");
  2712. goto out;
  2713. }
  2714. err = pci_request_regions(pdev, DRV_NAME);
  2715. if (err) {
  2716. /* Just info, some other driver may have claimed the device. */
  2717. dev_info(&pdev->dev, "cannot obtain PCI resources\n");
  2718. goto out_disable_device;
  2719. }
  2720. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  2721. pci_using_dac = 1;
  2722. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  2723. if (err) {
  2724. dev_err(&pdev->dev, "unable to obtain 64-bit DMA for "
  2725. "coherent allocations\n");
  2726. goto out_release_regions;
  2727. }
  2728. } else if ((err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) != 0) {
  2729. dev_err(&pdev->dev, "no usable DMA configuration\n");
  2730. goto out_release_regions;
  2731. }
  2732. pci_set_master(pdev);
  2733. pci_save_state(pdev);
  2734. mmio_start = pci_resource_start(pdev, 0);
  2735. mmio_len = pci_resource_len(pdev, 0);
  2736. ai = t3_get_adapter_info(ent->driver_data);
  2737. adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
  2738. if (!adapter) {
  2739. err = -ENOMEM;
  2740. goto out_release_regions;
  2741. }
  2742. adapter->nofail_skb =
  2743. alloc_skb(sizeof(struct cpl_set_tcb_field), GFP_KERNEL);
  2744. if (!adapter->nofail_skb) {
  2745. dev_err(&pdev->dev, "cannot allocate nofail buffer\n");
  2746. err = -ENOMEM;
  2747. goto out_free_adapter;
  2748. }
  2749. adapter->regs = ioremap_nocache(mmio_start, mmio_len);
  2750. if (!adapter->regs) {
  2751. dev_err(&pdev->dev, "cannot map device registers\n");
  2752. err = -ENOMEM;
  2753. goto out_free_adapter;
  2754. }
  2755. adapter->pdev = pdev;
  2756. adapter->name = pci_name(pdev);
  2757. adapter->msg_enable = dflt_msg_enable;
  2758. adapter->mmio_len = mmio_len;
  2759. mutex_init(&adapter->mdio_lock);
  2760. spin_lock_init(&adapter->work_lock);
  2761. spin_lock_init(&adapter->stats_lock);
  2762. INIT_LIST_HEAD(&adapter->adapter_list);
  2763. INIT_WORK(&adapter->ext_intr_handler_task, ext_intr_task);
  2764. INIT_WORK(&adapter->fatal_error_handler_task, fatal_error_task);
  2765. INIT_WORK(&adapter->db_full_task, db_full_task);
  2766. INIT_WORK(&adapter->db_empty_task, db_empty_task);
  2767. INIT_WORK(&adapter->db_drop_task, db_drop_task);
  2768. INIT_DELAYED_WORK(&adapter->adap_check_task, t3_adap_check_task);
  2769. for (i = 0; i < ai->nports0 + ai->nports1; ++i) {
  2770. struct net_device *netdev;
  2771. netdev = alloc_etherdev_mq(sizeof(struct port_info), SGE_QSETS);
  2772. if (!netdev) {
  2773. err = -ENOMEM;
  2774. goto out_free_dev;
  2775. }
  2776. SET_NETDEV_DEV(netdev, &pdev->dev);
  2777. adapter->port[i] = netdev;
  2778. pi = netdev_priv(netdev);
  2779. pi->adapter = adapter;
  2780. pi->port_id = i;
  2781. netif_carrier_off(netdev);
  2782. netdev->irq = pdev->irq;
  2783. netdev->mem_start = mmio_start;
  2784. netdev->mem_end = mmio_start + mmio_len - 1;
  2785. netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
  2786. NETIF_F_TSO | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX;
  2787. netdev->features |= netdev->hw_features |
  2788. NETIF_F_HW_VLAN_CTAG_TX;
  2789. netdev->vlan_features |= netdev->features & VLAN_FEAT;
  2790. if (pci_using_dac)
  2791. netdev->features |= NETIF_F_HIGHDMA;
  2792. netdev->netdev_ops = &cxgb_netdev_ops;
  2793. netdev->ethtool_ops = &cxgb_ethtool_ops;
  2794. }
  2795. pci_set_drvdata(pdev, adapter);
  2796. if (t3_prep_adapter(adapter, ai, 1) < 0) {
  2797. err = -ENODEV;
  2798. goto out_free_dev;
  2799. }
  2800. /*
  2801. * The card is now ready to go. If any errors occur during device
  2802. * registration we do not fail the whole card but rather proceed only
  2803. * with the ports we manage to register successfully. However we must
  2804. * register at least one net device.
  2805. */
  2806. for_each_port(adapter, i) {
  2807. err = register_netdev(adapter->port[i]);
  2808. if (err)
  2809. dev_warn(&pdev->dev,
  2810. "cannot register net device %s, skipping\n",
  2811. adapter->port[i]->name);
  2812. else {
  2813. /*
  2814. * Change the name we use for messages to the name of
  2815. * the first successfully registered interface.
  2816. */
  2817. if (!adapter->registered_device_map)
  2818. adapter->name = adapter->port[i]->name;
  2819. __set_bit(i, &adapter->registered_device_map);
  2820. }
  2821. }
  2822. if (!adapter->registered_device_map) {
  2823. dev_err(&pdev->dev, "could not register any net devices\n");
  2824. goto out_free_dev;
  2825. }
  2826. for_each_port(adapter, i)
  2827. cxgb3_init_iscsi_mac(adapter->port[i]);
  2828. /* Driver's ready. Reflect it on LEDs */
  2829. t3_led_ready(adapter);
  2830. if (is_offload(adapter)) {
  2831. __set_bit(OFFLOAD_DEVMAP_BIT, &adapter->registered_device_map);
  2832. cxgb3_adapter_ofld(adapter);
  2833. }
  2834. /* See what interrupts we'll be using */
  2835. if (msi > 1 && cxgb_enable_msix(adapter) == 0)
  2836. adapter->flags |= USING_MSIX;
  2837. else if (msi > 0 && pci_enable_msi(pdev) == 0)
  2838. adapter->flags |= USING_MSI;
  2839. set_nqsets(adapter);
  2840. err = sysfs_create_group(&adapter->port[0]->dev.kobj,
  2841. &cxgb3_attr_group);
  2842. print_port_info(adapter, ai);
  2843. return 0;
  2844. out_free_dev:
  2845. iounmap(adapter->regs);
  2846. for (i = ai->nports0 + ai->nports1 - 1; i >= 0; --i)
  2847. if (adapter->port[i])
  2848. free_netdev(adapter->port[i]);
  2849. out_free_adapter:
  2850. kfree(adapter);
  2851. out_release_regions:
  2852. pci_release_regions(pdev);
  2853. out_disable_device:
  2854. pci_disable_device(pdev);
  2855. out:
  2856. return err;
  2857. }
  2858. static void remove_one(struct pci_dev *pdev)
  2859. {
  2860. struct adapter *adapter = pci_get_drvdata(pdev);
  2861. if (adapter) {
  2862. int i;
  2863. t3_sge_stop(adapter);
  2864. sysfs_remove_group(&adapter->port[0]->dev.kobj,
  2865. &cxgb3_attr_group);
  2866. if (is_offload(adapter)) {
  2867. cxgb3_adapter_unofld(adapter);
  2868. if (test_bit(OFFLOAD_DEVMAP_BIT,
  2869. &adapter->open_device_map))
  2870. offload_close(&adapter->tdev);
  2871. }
  2872. for_each_port(adapter, i)
  2873. if (test_bit(i, &adapter->registered_device_map))
  2874. unregister_netdev(adapter->port[i]);
  2875. t3_stop_sge_timers(adapter);
  2876. t3_free_sge_resources(adapter);
  2877. cxgb_disable_msi(adapter);
  2878. for_each_port(adapter, i)
  2879. if (adapter->port[i])
  2880. free_netdev(adapter->port[i]);
  2881. iounmap(adapter->regs);
  2882. if (adapter->nofail_skb)
  2883. kfree_skb(adapter->nofail_skb);
  2884. kfree(adapter);
  2885. pci_release_regions(pdev);
  2886. pci_disable_device(pdev);
  2887. }
  2888. }
  2889. static struct pci_driver driver = {
  2890. .name = DRV_NAME,
  2891. .id_table = cxgb3_pci_tbl,
  2892. .probe = init_one,
  2893. .remove = remove_one,
  2894. .err_handler = &t3_err_handler,
  2895. };
  2896. static int __init cxgb3_init_module(void)
  2897. {
  2898. int ret;
  2899. cxgb3_offload_init();
  2900. ret = pci_register_driver(&driver);
  2901. return ret;
  2902. }
  2903. static void __exit cxgb3_cleanup_module(void)
  2904. {
  2905. pci_unregister_driver(&driver);
  2906. if (cxgb3_wq)
  2907. destroy_workqueue(cxgb3_wq);
  2908. }
  2909. module_init(cxgb3_init_module);
  2910. module_exit(cxgb3_cleanup_module);