cxgb4vf_main.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889
  1. /*
  2. * This file is part of the Chelsio T4 PCI-E SR-IOV Virtual Function Ethernet
  3. * driver for Linux.
  4. *
  5. * Copyright (c) 2009-2010 Chelsio Communications, Inc. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the
  11. * OpenIB.org BSD license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials
  24. * provided with the distribution.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. * SOFTWARE.
  34. */
  35. #include <linux/version.h>
  36. #include <linux/module.h>
  37. #include <linux/moduleparam.h>
  38. #include <linux/init.h>
  39. #include <linux/pci.h>
  40. #include <linux/dma-mapping.h>
  41. #include <linux/netdevice.h>
  42. #include <linux/etherdevice.h>
  43. #include <linux/debugfs.h>
  44. #include <linux/ethtool.h>
  45. #include "t4vf_common.h"
  46. #include "t4vf_defs.h"
  47. #include "../cxgb4/t4_regs.h"
  48. #include "../cxgb4/t4_msg.h"
  49. /*
  50. * Generic information about the driver.
  51. */
  52. #define DRV_VERSION "1.0.0"
  53. #define DRV_DESC "Chelsio T4 Virtual Function (VF) Network Driver"
  54. /*
  55. * Module Parameters.
  56. * ==================
  57. */
  58. /*
  59. * Default ethtool "message level" for adapters.
  60. */
  61. #define DFLT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK | \
  62. NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP |\
  63. NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)
  64. static int dflt_msg_enable = DFLT_MSG_ENABLE;
  65. module_param(dflt_msg_enable, int, 0644);
  66. MODULE_PARM_DESC(dflt_msg_enable,
  67. "default adapter ethtool message level bitmap");
  68. /*
  69. * The driver uses the best interrupt scheme available on a platform in the
  70. * order MSI-X then MSI. This parameter determines which of these schemes the
  71. * driver may consider as follows:
  72. *
  73. * msi = 2: choose from among MSI-X and MSI
  74. * msi = 1: only consider MSI interrupts
  75. *
  76. * Note that unlike the Physical Function driver, this Virtual Function driver
  77. * does _not_ support legacy INTx interrupts (this limitation is mandated by
  78. * the PCI-E SR-IOV standard).
  79. */
  80. #define MSI_MSIX 2
  81. #define MSI_MSI 1
  82. #define MSI_DEFAULT MSI_MSIX
  83. static int msi = MSI_DEFAULT;
  84. module_param(msi, int, 0644);
  85. MODULE_PARM_DESC(msi, "whether to use MSI-X or MSI");
  86. /*
  87. * Fundamental constants.
  88. * ======================
  89. */
  90. enum {
  91. MAX_TXQ_ENTRIES = 16384,
  92. MAX_RSPQ_ENTRIES = 16384,
  93. MAX_RX_BUFFERS = 16384,
  94. MIN_TXQ_ENTRIES = 32,
  95. MIN_RSPQ_ENTRIES = 128,
  96. MIN_FL_ENTRIES = 16,
  97. /*
  98. * For purposes of manipulating the Free List size we need to
  99. * recognize that Free Lists are actually Egress Queues (the host
  100. * produces free buffers which the hardware consumes), Egress Queues
  101. * indices are all in units of Egress Context Units bytes, and free
  102. * list entries are 64-bit PCI DMA addresses. And since the state of
  103. * the Producer Index == the Consumer Index implies an EMPTY list, we
  104. * always have at least one Egress Unit's worth of Free List entries
  105. * unused. See sge.c for more details ...
  106. */
  107. EQ_UNIT = SGE_EQ_IDXSIZE,
  108. FL_PER_EQ_UNIT = EQ_UNIT / sizeof(__be64),
  109. MIN_FL_RESID = FL_PER_EQ_UNIT,
  110. };
  111. /*
  112. * Global driver state.
  113. * ====================
  114. */
  115. static struct dentry *cxgb4vf_debugfs_root;
  116. /*
  117. * OS "Callback" functions.
  118. * ========================
  119. */
  120. /*
  121. * The link status has changed on the indicated "port" (Virtual Interface).
  122. */
  123. void t4vf_os_link_changed(struct adapter *adapter, int pidx, int link_ok)
  124. {
  125. struct net_device *dev = adapter->port[pidx];
  126. /*
  127. * If the port is disabled or the current recorded "link up"
  128. * status matches the new status, just return.
  129. */
  130. if (!netif_running(dev) || link_ok == netif_carrier_ok(dev))
  131. return;
  132. /*
  133. * Tell the OS that the link status has changed and print a short
  134. * informative message on the console about the event.
  135. */
  136. if (link_ok) {
  137. const char *s;
  138. const char *fc;
  139. const struct port_info *pi = netdev_priv(dev);
  140. netif_carrier_on(dev);
  141. switch (pi->link_cfg.speed) {
  142. case SPEED_10000:
  143. s = "10Gbps";
  144. break;
  145. case SPEED_1000:
  146. s = "1000Mbps";
  147. break;
  148. case SPEED_100:
  149. s = "100Mbps";
  150. break;
  151. default:
  152. s = "unknown";
  153. break;
  154. }
  155. switch (pi->link_cfg.fc) {
  156. case PAUSE_RX:
  157. fc = "RX";
  158. break;
  159. case PAUSE_TX:
  160. fc = "TX";
  161. break;
  162. case PAUSE_RX|PAUSE_TX:
  163. fc = "RX/TX";
  164. break;
  165. default:
  166. fc = "no";
  167. break;
  168. }
  169. printk(KERN_INFO "%s: link up, %s, full-duplex, %s PAUSE\n",
  170. dev->name, s, fc);
  171. } else {
  172. netif_carrier_off(dev);
  173. printk(KERN_INFO "%s: link down\n", dev->name);
  174. }
  175. }
  176. /*
  177. * Net device operations.
  178. * ======================
  179. */
  180. /*
  181. * Record our new VLAN Group and enable/disable hardware VLAN Tag extraction
  182. * based on whether the specified VLAN Group pointer is NULL or not.
  183. */
  184. static void cxgb4vf_vlan_rx_register(struct net_device *dev,
  185. struct vlan_group *grp)
  186. {
  187. struct port_info *pi = netdev_priv(dev);
  188. pi->vlan_grp = grp;
  189. t4vf_set_rxmode(pi->adapter, pi->viid, -1, -1, -1, -1, grp != NULL, 0);
  190. }
  191. /*
  192. * Perform the MAC and PHY actions needed to enable a "port" (Virtual
  193. * Interface).
  194. */
  195. static int link_start(struct net_device *dev)
  196. {
  197. int ret;
  198. struct port_info *pi = netdev_priv(dev);
  199. /*
  200. * We do not set address filters and promiscuity here, the stack does
  201. * that step explicitly.
  202. */
  203. ret = t4vf_set_rxmode(pi->adapter, pi->viid, dev->mtu, -1, -1, -1, -1,
  204. true);
  205. if (ret == 0) {
  206. ret = t4vf_change_mac(pi->adapter, pi->viid,
  207. pi->xact_addr_filt, dev->dev_addr, true);
  208. if (ret >= 0) {
  209. pi->xact_addr_filt = ret;
  210. ret = 0;
  211. }
  212. }
  213. /*
  214. * We don't need to actually "start the link" itself since the
  215. * firmware will do that for us when the first Virtual Interface
  216. * is enabled on a port.
  217. */
  218. if (ret == 0)
  219. ret = t4vf_enable_vi(pi->adapter, pi->viid, true, true);
  220. return ret;
  221. }
  222. /*
  223. * Name the MSI-X interrupts.
  224. */
  225. static void name_msix_vecs(struct adapter *adapter)
  226. {
  227. int namelen = sizeof(adapter->msix_info[0].desc) - 1;
  228. int pidx;
  229. /*
  230. * Firmware events.
  231. */
  232. snprintf(adapter->msix_info[MSIX_FW].desc, namelen,
  233. "%s-FWeventq", adapter->name);
  234. adapter->msix_info[MSIX_FW].desc[namelen] = 0;
  235. /*
  236. * Ethernet queues.
  237. */
  238. for_each_port(adapter, pidx) {
  239. struct net_device *dev = adapter->port[pidx];
  240. const struct port_info *pi = netdev_priv(dev);
  241. int qs, msi;
  242. for (qs = 0, msi = MSIX_NIQFLINT;
  243. qs < pi->nqsets;
  244. qs++, msi++) {
  245. snprintf(adapter->msix_info[msi].desc, namelen,
  246. "%s-%d", dev->name, qs);
  247. adapter->msix_info[msi].desc[namelen] = 0;
  248. }
  249. }
  250. }
  251. /*
  252. * Request all of our MSI-X resources.
  253. */
  254. static int request_msix_queue_irqs(struct adapter *adapter)
  255. {
  256. struct sge *s = &adapter->sge;
  257. int rxq, msi, err;
  258. /*
  259. * Firmware events.
  260. */
  261. err = request_irq(adapter->msix_info[MSIX_FW].vec, t4vf_sge_intr_msix,
  262. 0, adapter->msix_info[MSIX_FW].desc, &s->fw_evtq);
  263. if (err)
  264. return err;
  265. /*
  266. * Ethernet queues.
  267. */
  268. msi = MSIX_NIQFLINT;
  269. for_each_ethrxq(s, rxq) {
  270. err = request_irq(adapter->msix_info[msi].vec,
  271. t4vf_sge_intr_msix, 0,
  272. adapter->msix_info[msi].desc,
  273. &s->ethrxq[rxq].rspq);
  274. if (err)
  275. goto err_free_irqs;
  276. msi++;
  277. }
  278. return 0;
  279. err_free_irqs:
  280. while (--rxq >= 0)
  281. free_irq(adapter->msix_info[--msi].vec, &s->ethrxq[rxq].rspq);
  282. free_irq(adapter->msix_info[MSIX_FW].vec, &s->fw_evtq);
  283. return err;
  284. }
  285. /*
  286. * Free our MSI-X resources.
  287. */
  288. static void free_msix_queue_irqs(struct adapter *adapter)
  289. {
  290. struct sge *s = &adapter->sge;
  291. int rxq, msi;
  292. free_irq(adapter->msix_info[MSIX_FW].vec, &s->fw_evtq);
  293. msi = MSIX_NIQFLINT;
  294. for_each_ethrxq(s, rxq)
  295. free_irq(adapter->msix_info[msi++].vec,
  296. &s->ethrxq[rxq].rspq);
  297. }
  298. /*
  299. * Turn on NAPI and start up interrupts on a response queue.
  300. */
  301. static void qenable(struct sge_rspq *rspq)
  302. {
  303. napi_enable(&rspq->napi);
  304. /*
  305. * 0-increment the Going To Sleep register to start the timer and
  306. * enable interrupts.
  307. */
  308. t4_write_reg(rspq->adapter, T4VF_SGE_BASE_ADDR + SGE_VF_GTS,
  309. CIDXINC(0) |
  310. SEINTARM(rspq->intr_params) |
  311. INGRESSQID(rspq->cntxt_id));
  312. }
  313. /*
  314. * Enable NAPI scheduling and interrupt generation for all Receive Queues.
  315. */
  316. static void enable_rx(struct adapter *adapter)
  317. {
  318. int rxq;
  319. struct sge *s = &adapter->sge;
  320. for_each_ethrxq(s, rxq)
  321. qenable(&s->ethrxq[rxq].rspq);
  322. qenable(&s->fw_evtq);
  323. /*
  324. * The interrupt queue doesn't use NAPI so we do the 0-increment of
  325. * its Going To Sleep register here to get it started.
  326. */
  327. if (adapter->flags & USING_MSI)
  328. t4_write_reg(adapter, T4VF_SGE_BASE_ADDR + SGE_VF_GTS,
  329. CIDXINC(0) |
  330. SEINTARM(s->intrq.intr_params) |
  331. INGRESSQID(s->intrq.cntxt_id));
  332. }
  333. /*
  334. * Wait until all NAPI handlers are descheduled.
  335. */
  336. static void quiesce_rx(struct adapter *adapter)
  337. {
  338. struct sge *s = &adapter->sge;
  339. int rxq;
  340. for_each_ethrxq(s, rxq)
  341. napi_disable(&s->ethrxq[rxq].rspq.napi);
  342. napi_disable(&s->fw_evtq.napi);
  343. }
  344. /*
  345. * Response queue handler for the firmware event queue.
  346. */
  347. static int fwevtq_handler(struct sge_rspq *rspq, const __be64 *rsp,
  348. const struct pkt_gl *gl)
  349. {
  350. /*
  351. * Extract response opcode and get pointer to CPL message body.
  352. */
  353. struct adapter *adapter = rspq->adapter;
  354. u8 opcode = ((const struct rss_header *)rsp)->opcode;
  355. void *cpl = (void *)(rsp + 1);
  356. switch (opcode) {
  357. case CPL_FW6_MSG: {
  358. /*
  359. * We've received an asynchronous message from the firmware.
  360. */
  361. const struct cpl_fw6_msg *fw_msg = cpl;
  362. if (fw_msg->type == FW6_TYPE_CMD_RPL)
  363. t4vf_handle_fw_rpl(adapter, fw_msg->data);
  364. break;
  365. }
  366. case CPL_SGE_EGR_UPDATE: {
  367. /*
  368. * We've received an Egress Queue Status Update message. We
  369. * get these, if the SGE is configured to send these when the
  370. * firmware passes certain points in processing our TX
  371. * Ethernet Queue or if we make an explicit request for one.
  372. * We use these updates to determine when we may need to
  373. * restart a TX Ethernet Queue which was stopped for lack of
  374. * free TX Queue Descriptors ...
  375. */
  376. const struct cpl_sge_egr_update *p = (void *)cpl;
  377. unsigned int qid = EGR_QID(be32_to_cpu(p->opcode_qid));
  378. struct sge *s = &adapter->sge;
  379. struct sge_txq *tq;
  380. struct sge_eth_txq *txq;
  381. unsigned int eq_idx;
  382. /*
  383. * Perform sanity checking on the Queue ID to make sure it
  384. * really refers to one of our TX Ethernet Egress Queues which
  385. * is active and matches the queue's ID. None of these error
  386. * conditions should ever happen so we may want to either make
  387. * them fatal and/or conditionalized under DEBUG.
  388. */
  389. eq_idx = EQ_IDX(s, qid);
  390. if (unlikely(eq_idx >= MAX_EGRQ)) {
  391. dev_err(adapter->pdev_dev,
  392. "Egress Update QID %d out of range\n", qid);
  393. break;
  394. }
  395. tq = s->egr_map[eq_idx];
  396. if (unlikely(tq == NULL)) {
  397. dev_err(adapter->pdev_dev,
  398. "Egress Update QID %d TXQ=NULL\n", qid);
  399. break;
  400. }
  401. txq = container_of(tq, struct sge_eth_txq, q);
  402. if (unlikely(tq->abs_id != qid)) {
  403. dev_err(adapter->pdev_dev,
  404. "Egress Update QID %d refers to TXQ %d\n",
  405. qid, tq->abs_id);
  406. break;
  407. }
  408. /*
  409. * Restart a stopped TX Queue which has less than half of its
  410. * TX ring in use ...
  411. */
  412. txq->q.restarts++;
  413. netif_tx_wake_queue(txq->txq);
  414. break;
  415. }
  416. default:
  417. dev_err(adapter->pdev_dev,
  418. "unexpected CPL %#x on FW event queue\n", opcode);
  419. }
  420. return 0;
  421. }
  422. /*
  423. * Allocate SGE TX/RX response queues. Determine how many sets of SGE queues
  424. * to use and initializes them. We support multiple "Queue Sets" per port if
  425. * we have MSI-X, otherwise just one queue set per port.
  426. */
  427. static int setup_sge_queues(struct adapter *adapter)
  428. {
  429. struct sge *s = &adapter->sge;
  430. int err, pidx, msix;
  431. /*
  432. * Clear "Queue Set" Free List Starving and TX Queue Mapping Error
  433. * state.
  434. */
  435. bitmap_zero(s->starving_fl, MAX_EGRQ);
  436. /*
  437. * If we're using MSI interrupt mode we need to set up a "forwarded
  438. * interrupt" queue which we'll set up with our MSI vector. The rest
  439. * of the ingress queues will be set up to forward their interrupts to
  440. * this queue ... This must be first since t4vf_sge_alloc_rxq() uses
  441. * the intrq's queue ID as the interrupt forwarding queue for the
  442. * subsequent calls ...
  443. */
  444. if (adapter->flags & USING_MSI) {
  445. err = t4vf_sge_alloc_rxq(adapter, &s->intrq, false,
  446. adapter->port[0], 0, NULL, NULL);
  447. if (err)
  448. goto err_free_queues;
  449. }
  450. /*
  451. * Allocate our ingress queue for asynchronous firmware messages.
  452. */
  453. err = t4vf_sge_alloc_rxq(adapter, &s->fw_evtq, true, adapter->port[0],
  454. MSIX_FW, NULL, fwevtq_handler);
  455. if (err)
  456. goto err_free_queues;
  457. /*
  458. * Allocate each "port"'s initial Queue Sets. These can be changed
  459. * later on ... up to the point where any interface on the adapter is
  460. * brought up at which point lots of things get nailed down
  461. * permanently ...
  462. */
  463. msix = MSIX_NIQFLINT;
  464. for_each_port(adapter, pidx) {
  465. struct net_device *dev = adapter->port[pidx];
  466. struct port_info *pi = netdev_priv(dev);
  467. struct sge_eth_rxq *rxq = &s->ethrxq[pi->first_qset];
  468. struct sge_eth_txq *txq = &s->ethtxq[pi->first_qset];
  469. int qs;
  470. for (qs = 0; qs < pi->nqsets; qs++, rxq++, txq++) {
  471. err = t4vf_sge_alloc_rxq(adapter, &rxq->rspq, false,
  472. dev, msix++,
  473. &rxq->fl, t4vf_ethrx_handler);
  474. if (err)
  475. goto err_free_queues;
  476. err = t4vf_sge_alloc_eth_txq(adapter, txq, dev,
  477. netdev_get_tx_queue(dev, qs),
  478. s->fw_evtq.cntxt_id);
  479. if (err)
  480. goto err_free_queues;
  481. rxq->rspq.idx = qs;
  482. memset(&rxq->stats, 0, sizeof(rxq->stats));
  483. }
  484. }
  485. /*
  486. * Create the reverse mappings for the queues.
  487. */
  488. s->egr_base = s->ethtxq[0].q.abs_id - s->ethtxq[0].q.cntxt_id;
  489. s->ingr_base = s->ethrxq[0].rspq.abs_id - s->ethrxq[0].rspq.cntxt_id;
  490. IQ_MAP(s, s->fw_evtq.abs_id) = &s->fw_evtq;
  491. for_each_port(adapter, pidx) {
  492. struct net_device *dev = adapter->port[pidx];
  493. struct port_info *pi = netdev_priv(dev);
  494. struct sge_eth_rxq *rxq = &s->ethrxq[pi->first_qset];
  495. struct sge_eth_txq *txq = &s->ethtxq[pi->first_qset];
  496. int qs;
  497. for (qs = 0; qs < pi->nqsets; qs++, rxq++, txq++) {
  498. IQ_MAP(s, rxq->rspq.abs_id) = &rxq->rspq;
  499. EQ_MAP(s, txq->q.abs_id) = &txq->q;
  500. /*
  501. * The FW_IQ_CMD doesn't return the Absolute Queue IDs
  502. * for Free Lists but since all of the Egress Queues
  503. * (including Free Lists) have Relative Queue IDs
  504. * which are computed as Absolute - Base Queue ID, we
  505. * can synthesize the Absolute Queue IDs for the Free
  506. * Lists. This is useful for debugging purposes when
  507. * we want to dump Queue Contexts via the PF Driver.
  508. */
  509. rxq->fl.abs_id = rxq->fl.cntxt_id + s->egr_base;
  510. EQ_MAP(s, rxq->fl.abs_id) = &rxq->fl;
  511. }
  512. }
  513. return 0;
  514. err_free_queues:
  515. t4vf_free_sge_resources(adapter);
  516. return err;
  517. }
  518. /*
  519. * Set up Receive Side Scaling (RSS) to distribute packets to multiple receive
  520. * queues. We configure the RSS CPU lookup table to distribute to the number
  521. * of HW receive queues, and the response queue lookup table to narrow that
  522. * down to the response queues actually configured for each "port" (Virtual
  523. * Interface). We always configure the RSS mapping for all ports since the
  524. * mapping table has plenty of entries.
  525. */
  526. static int setup_rss(struct adapter *adapter)
  527. {
  528. int pidx;
  529. for_each_port(adapter, pidx) {
  530. struct port_info *pi = adap2pinfo(adapter, pidx);
  531. struct sge_eth_rxq *rxq = &adapter->sge.ethrxq[pi->first_qset];
  532. u16 rss[MAX_PORT_QSETS];
  533. int qs, err;
  534. for (qs = 0; qs < pi->nqsets; qs++)
  535. rss[qs] = rxq[qs].rspq.abs_id;
  536. err = t4vf_config_rss_range(adapter, pi->viid,
  537. 0, pi->rss_size, rss, pi->nqsets);
  538. if (err)
  539. return err;
  540. /*
  541. * Perform Global RSS Mode-specific initialization.
  542. */
  543. switch (adapter->params.rss.mode) {
  544. case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL:
  545. /*
  546. * If Tunnel All Lookup isn't specified in the global
  547. * RSS Configuration, then we need to specify a
  548. * default Ingress Queue for any ingress packets which
  549. * aren't hashed. We'll use our first ingress queue
  550. * ...
  551. */
  552. if (!adapter->params.rss.u.basicvirtual.tnlalllookup) {
  553. union rss_vi_config config;
  554. err = t4vf_read_rss_vi_config(adapter,
  555. pi->viid,
  556. &config);
  557. if (err)
  558. return err;
  559. config.basicvirtual.defaultq =
  560. rxq[0].rspq.abs_id;
  561. err = t4vf_write_rss_vi_config(adapter,
  562. pi->viid,
  563. &config);
  564. if (err)
  565. return err;
  566. }
  567. break;
  568. }
  569. }
  570. return 0;
  571. }
  572. /*
  573. * Bring the adapter up. Called whenever we go from no "ports" open to having
  574. * one open. This function performs the actions necessary to make an adapter
  575. * operational, such as completing the initialization of HW modules, and
  576. * enabling interrupts. Must be called with the rtnl lock held. (Note that
  577. * this is called "cxgb_up" in the PF Driver.)
  578. */
  579. static int adapter_up(struct adapter *adapter)
  580. {
  581. int err;
  582. /*
  583. * If this is the first time we've been called, perform basic
  584. * adapter setup. Once we've done this, many of our adapter
  585. * parameters can no longer be changed ...
  586. */
  587. if ((adapter->flags & FULL_INIT_DONE) == 0) {
  588. err = setup_sge_queues(adapter);
  589. if (err)
  590. return err;
  591. err = setup_rss(adapter);
  592. if (err) {
  593. t4vf_free_sge_resources(adapter);
  594. return err;
  595. }
  596. if (adapter->flags & USING_MSIX)
  597. name_msix_vecs(adapter);
  598. adapter->flags |= FULL_INIT_DONE;
  599. }
  600. /*
  601. * Acquire our interrupt resources. We only support MSI-X and MSI.
  602. */
  603. BUG_ON((adapter->flags & (USING_MSIX|USING_MSI)) == 0);
  604. if (adapter->flags & USING_MSIX)
  605. err = request_msix_queue_irqs(adapter);
  606. else
  607. err = request_irq(adapter->pdev->irq,
  608. t4vf_intr_handler(adapter), 0,
  609. adapter->name, adapter);
  610. if (err) {
  611. dev_err(adapter->pdev_dev, "request_irq failed, err %d\n",
  612. err);
  613. return err;
  614. }
  615. /*
  616. * Enable NAPI ingress processing and return success.
  617. */
  618. enable_rx(adapter);
  619. t4vf_sge_start(adapter);
  620. return 0;
  621. }
  622. /*
  623. * Bring the adapter down. Called whenever the last "port" (Virtual
  624. * Interface) closed. (Note that this routine is called "cxgb_down" in the PF
  625. * Driver.)
  626. */
  627. static void adapter_down(struct adapter *adapter)
  628. {
  629. /*
  630. * Free interrupt resources.
  631. */
  632. if (adapter->flags & USING_MSIX)
  633. free_msix_queue_irqs(adapter);
  634. else
  635. free_irq(adapter->pdev->irq, adapter);
  636. /*
  637. * Wait for NAPI handlers to finish.
  638. */
  639. quiesce_rx(adapter);
  640. }
  641. /*
  642. * Start up a net device.
  643. */
  644. static int cxgb4vf_open(struct net_device *dev)
  645. {
  646. int err;
  647. struct port_info *pi = netdev_priv(dev);
  648. struct adapter *adapter = pi->adapter;
  649. /*
  650. * If this is the first interface that we're opening on the "adapter",
  651. * bring the "adapter" up now.
  652. */
  653. if (adapter->open_device_map == 0) {
  654. err = adapter_up(adapter);
  655. if (err)
  656. return err;
  657. }
  658. /*
  659. * Note that this interface is up and start everything up ...
  660. */
  661. dev->real_num_tx_queues = pi->nqsets;
  662. set_bit(pi->port_id, &adapter->open_device_map);
  663. link_start(dev);
  664. netif_tx_start_all_queues(dev);
  665. return 0;
  666. }
  667. /*
  668. * Shut down a net device. This routine is called "cxgb_close" in the PF
  669. * Driver ...
  670. */
  671. static int cxgb4vf_stop(struct net_device *dev)
  672. {
  673. int ret;
  674. struct port_info *pi = netdev_priv(dev);
  675. struct adapter *adapter = pi->adapter;
  676. netif_tx_stop_all_queues(dev);
  677. netif_carrier_off(dev);
  678. ret = t4vf_enable_vi(adapter, pi->viid, false, false);
  679. pi->link_cfg.link_ok = 0;
  680. clear_bit(pi->port_id, &adapter->open_device_map);
  681. if (adapter->open_device_map == 0)
  682. adapter_down(adapter);
  683. return 0;
  684. }
  685. /*
  686. * Translate our basic statistics into the standard "ifconfig" statistics.
  687. */
  688. static struct net_device_stats *cxgb4vf_get_stats(struct net_device *dev)
  689. {
  690. struct t4vf_port_stats stats;
  691. struct port_info *pi = netdev2pinfo(dev);
  692. struct adapter *adapter = pi->adapter;
  693. struct net_device_stats *ns = &dev->stats;
  694. int err;
  695. spin_lock(&adapter->stats_lock);
  696. err = t4vf_get_port_stats(adapter, pi->pidx, &stats);
  697. spin_unlock(&adapter->stats_lock);
  698. memset(ns, 0, sizeof(*ns));
  699. if (err)
  700. return ns;
  701. ns->tx_bytes = (stats.tx_bcast_bytes + stats.tx_mcast_bytes +
  702. stats.tx_ucast_bytes + stats.tx_offload_bytes);
  703. ns->tx_packets = (stats.tx_bcast_frames + stats.tx_mcast_frames +
  704. stats.tx_ucast_frames + stats.tx_offload_frames);
  705. ns->rx_bytes = (stats.rx_bcast_bytes + stats.rx_mcast_bytes +
  706. stats.rx_ucast_bytes);
  707. ns->rx_packets = (stats.rx_bcast_frames + stats.rx_mcast_frames +
  708. stats.rx_ucast_frames);
  709. ns->multicast = stats.rx_mcast_frames;
  710. ns->tx_errors = stats.tx_drop_frames;
  711. ns->rx_errors = stats.rx_err_frames;
  712. return ns;
  713. }
  714. /*
  715. * Collect up to maxaddrs worth of a netdevice's unicast addresses into an
  716. * array of addrss pointers and return the number collected.
  717. */
  718. static inline int collect_netdev_uc_list_addrs(const struct net_device *dev,
  719. const u8 **addr,
  720. unsigned int maxaddrs)
  721. {
  722. unsigned int naddr = 0;
  723. const struct netdev_hw_addr *ha;
  724. for_each_dev_addr(dev, ha) {
  725. addr[naddr++] = ha->addr;
  726. if (naddr >= maxaddrs)
  727. break;
  728. }
  729. return naddr;
  730. }
  731. /*
  732. * Collect up to maxaddrs worth of a netdevice's multicast addresses into an
  733. * array of addrss pointers and return the number collected.
  734. */
  735. static inline int collect_netdev_mc_list_addrs(const struct net_device *dev,
  736. const u8 **addr,
  737. unsigned int maxaddrs)
  738. {
  739. unsigned int naddr = 0;
  740. const struct netdev_hw_addr *ha;
  741. netdev_for_each_mc_addr(ha, dev) {
  742. addr[naddr++] = ha->addr;
  743. if (naddr >= maxaddrs)
  744. break;
  745. }
  746. return naddr;
  747. }
  748. /*
  749. * Configure the exact and hash address filters to handle a port's multicast
  750. * and secondary unicast MAC addresses.
  751. */
  752. static int set_addr_filters(const struct net_device *dev, bool sleep)
  753. {
  754. u64 mhash = 0;
  755. u64 uhash = 0;
  756. bool free = true;
  757. u16 filt_idx[7];
  758. const u8 *addr[7];
  759. int ret, naddr = 0;
  760. const struct port_info *pi = netdev_priv(dev);
  761. /* first do the secondary unicast addresses */
  762. naddr = collect_netdev_uc_list_addrs(dev, addr, ARRAY_SIZE(addr));
  763. if (naddr > 0) {
  764. ret = t4vf_alloc_mac_filt(pi->adapter, pi->viid, free,
  765. naddr, addr, filt_idx, &uhash, sleep);
  766. if (ret < 0)
  767. return ret;
  768. free = false;
  769. }
  770. /* next set up the multicast addresses */
  771. naddr = collect_netdev_mc_list_addrs(dev, addr, ARRAY_SIZE(addr));
  772. if (naddr > 0) {
  773. ret = t4vf_alloc_mac_filt(pi->adapter, pi->viid, free,
  774. naddr, addr, filt_idx, &mhash, sleep);
  775. if (ret < 0)
  776. return ret;
  777. }
  778. return t4vf_set_addr_hash(pi->adapter, pi->viid, uhash != 0,
  779. uhash | mhash, sleep);
  780. }
  781. /*
  782. * Set RX properties of a port, such as promiscruity, address filters, and MTU.
  783. * If @mtu is -1 it is left unchanged.
  784. */
  785. static int set_rxmode(struct net_device *dev, int mtu, bool sleep_ok)
  786. {
  787. int ret;
  788. struct port_info *pi = netdev_priv(dev);
  789. ret = set_addr_filters(dev, sleep_ok);
  790. if (ret == 0)
  791. ret = t4vf_set_rxmode(pi->adapter, pi->viid, -1,
  792. (dev->flags & IFF_PROMISC) != 0,
  793. (dev->flags & IFF_ALLMULTI) != 0,
  794. 1, -1, sleep_ok);
  795. return ret;
  796. }
  797. /*
  798. * Set the current receive modes on the device.
  799. */
  800. static void cxgb4vf_set_rxmode(struct net_device *dev)
  801. {
  802. /* unfortunately we can't return errors to the stack */
  803. set_rxmode(dev, -1, false);
  804. }
  805. /*
  806. * Find the entry in the interrupt holdoff timer value array which comes
  807. * closest to the specified interrupt holdoff value.
  808. */
  809. static int closest_timer(const struct sge *s, int us)
  810. {
  811. int i, timer_idx = 0, min_delta = INT_MAX;
  812. for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
  813. int delta = us - s->timer_val[i];
  814. if (delta < 0)
  815. delta = -delta;
  816. if (delta < min_delta) {
  817. min_delta = delta;
  818. timer_idx = i;
  819. }
  820. }
  821. return timer_idx;
  822. }
  823. static int closest_thres(const struct sge *s, int thres)
  824. {
  825. int i, delta, pktcnt_idx = 0, min_delta = INT_MAX;
  826. for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
  827. delta = thres - s->counter_val[i];
  828. if (delta < 0)
  829. delta = -delta;
  830. if (delta < min_delta) {
  831. min_delta = delta;
  832. pktcnt_idx = i;
  833. }
  834. }
  835. return pktcnt_idx;
  836. }
  837. /*
  838. * Return a queue's interrupt hold-off time in us. 0 means no timer.
  839. */
  840. static unsigned int qtimer_val(const struct adapter *adapter,
  841. const struct sge_rspq *rspq)
  842. {
  843. unsigned int timer_idx = QINTR_TIMER_IDX_GET(rspq->intr_params);
  844. return timer_idx < SGE_NTIMERS
  845. ? adapter->sge.timer_val[timer_idx]
  846. : 0;
  847. }
  848. /**
  849. * set_rxq_intr_params - set a queue's interrupt holdoff parameters
  850. * @adapter: the adapter
  851. * @rspq: the RX response queue
  852. * @us: the hold-off time in us, or 0 to disable timer
  853. * @cnt: the hold-off packet count, or 0 to disable counter
  854. *
  855. * Sets an RX response queue's interrupt hold-off time and packet count.
  856. * At least one of the two needs to be enabled for the queue to generate
  857. * interrupts.
  858. */
  859. static int set_rxq_intr_params(struct adapter *adapter, struct sge_rspq *rspq,
  860. unsigned int us, unsigned int cnt)
  861. {
  862. unsigned int timer_idx;
  863. /*
  864. * If both the interrupt holdoff timer and count are specified as
  865. * zero, default to a holdoff count of 1 ...
  866. */
  867. if ((us | cnt) == 0)
  868. cnt = 1;
  869. /*
  870. * If an interrupt holdoff count has been specified, then find the
  871. * closest configured holdoff count and use that. If the response
  872. * queue has already been created, then update its queue context
  873. * parameters ...
  874. */
  875. if (cnt) {
  876. int err;
  877. u32 v, pktcnt_idx;
  878. pktcnt_idx = closest_thres(&adapter->sge, cnt);
  879. if (rspq->desc && rspq->pktcnt_idx != pktcnt_idx) {
  880. v = FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) |
  881. FW_PARAMS_PARAM_X(
  882. FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
  883. FW_PARAMS_PARAM_YZ(rspq->cntxt_id);
  884. err = t4vf_set_params(adapter, 1, &v, &pktcnt_idx);
  885. if (err)
  886. return err;
  887. }
  888. rspq->pktcnt_idx = pktcnt_idx;
  889. }
  890. /*
  891. * Compute the closest holdoff timer index from the supplied holdoff
  892. * timer value.
  893. */
  894. timer_idx = (us == 0
  895. ? SGE_TIMER_RSTRT_CNTR
  896. : closest_timer(&adapter->sge, us));
  897. /*
  898. * Update the response queue's interrupt coalescing parameters and
  899. * return success.
  900. */
  901. rspq->intr_params = (QINTR_TIMER_IDX(timer_idx) |
  902. (cnt > 0 ? QINTR_CNT_EN : 0));
  903. return 0;
  904. }
  905. /*
  906. * Return a version number to identify the type of adapter. The scheme is:
  907. * - bits 0..9: chip version
  908. * - bits 10..15: chip revision
  909. */
  910. static inline unsigned int mk_adap_vers(const struct adapter *adapter)
  911. {
  912. /*
  913. * Chip version 4, revision 0x3f (cxgb4vf).
  914. */
  915. return 4 | (0x3f << 10);
  916. }
  917. /*
  918. * Execute the specified ioctl command.
  919. */
  920. static int cxgb4vf_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  921. {
  922. int ret = 0;
  923. switch (cmd) {
  924. /*
  925. * The VF Driver doesn't have access to any of the other
  926. * common Ethernet device ioctl()'s (like reading/writing
  927. * PHY registers, etc.
  928. */
  929. default:
  930. ret = -EOPNOTSUPP;
  931. break;
  932. }
  933. return ret;
  934. }
  935. /*
  936. * Change the device's MTU.
  937. */
  938. static int cxgb4vf_change_mtu(struct net_device *dev, int new_mtu)
  939. {
  940. int ret;
  941. struct port_info *pi = netdev_priv(dev);
  942. /* accommodate SACK */
  943. if (new_mtu < 81)
  944. return -EINVAL;
  945. ret = t4vf_set_rxmode(pi->adapter, pi->viid, new_mtu,
  946. -1, -1, -1, -1, true);
  947. if (!ret)
  948. dev->mtu = new_mtu;
  949. return ret;
  950. }
  951. /*
  952. * Change the devices MAC address.
  953. */
  954. static int cxgb4vf_set_mac_addr(struct net_device *dev, void *_addr)
  955. {
  956. int ret;
  957. struct sockaddr *addr = _addr;
  958. struct port_info *pi = netdev_priv(dev);
  959. if (!is_valid_ether_addr(addr->sa_data))
  960. return -EINVAL;
  961. ret = t4vf_change_mac(pi->adapter, pi->viid, pi->xact_addr_filt,
  962. addr->sa_data, true);
  963. if (ret < 0)
  964. return ret;
  965. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  966. pi->xact_addr_filt = ret;
  967. return 0;
  968. }
  969. /*
  970. * Return a TX Queue on which to send the specified skb.
  971. */
  972. static u16 cxgb4vf_select_queue(struct net_device *dev, struct sk_buff *skb)
  973. {
  974. /*
  975. * XXX For now just use the default hash but we probably want to
  976. * XXX look at other possibilities ...
  977. */
  978. return skb_tx_hash(dev, skb);
  979. }
  980. #ifdef CONFIG_NET_POLL_CONTROLLER
  981. /*
  982. * Poll all of our receive queues. This is called outside of normal interrupt
  983. * context.
  984. */
  985. static void cxgb4vf_poll_controller(struct net_device *dev)
  986. {
  987. struct port_info *pi = netdev_priv(dev);
  988. struct adapter *adapter = pi->adapter;
  989. if (adapter->flags & USING_MSIX) {
  990. struct sge_eth_rxq *rxq;
  991. int nqsets;
  992. rxq = &adapter->sge.ethrxq[pi->first_qset];
  993. for (nqsets = pi->nqsets; nqsets; nqsets--) {
  994. t4vf_sge_intr_msix(0, &rxq->rspq);
  995. rxq++;
  996. }
  997. } else
  998. t4vf_intr_handler(adapter)(0, adapter);
  999. }
  1000. #endif
  1001. /*
  1002. * Ethtool operations.
  1003. * ===================
  1004. *
  1005. * Note that we don't support any ethtool operations which change the physical
  1006. * state of the port to which we're linked.
  1007. */
  1008. /*
  1009. * Return current port link settings.
  1010. */
  1011. static int cxgb4vf_get_settings(struct net_device *dev,
  1012. struct ethtool_cmd *cmd)
  1013. {
  1014. const struct port_info *pi = netdev_priv(dev);
  1015. cmd->supported = pi->link_cfg.supported;
  1016. cmd->advertising = pi->link_cfg.advertising;
  1017. cmd->speed = netif_carrier_ok(dev) ? pi->link_cfg.speed : -1;
  1018. cmd->duplex = DUPLEX_FULL;
  1019. cmd->port = (cmd->supported & SUPPORTED_TP) ? PORT_TP : PORT_FIBRE;
  1020. cmd->phy_address = pi->port_id;
  1021. cmd->transceiver = XCVR_EXTERNAL;
  1022. cmd->autoneg = pi->link_cfg.autoneg;
  1023. cmd->maxtxpkt = 0;
  1024. cmd->maxrxpkt = 0;
  1025. return 0;
  1026. }
  1027. /*
  1028. * Return our driver information.
  1029. */
  1030. static void cxgb4vf_get_drvinfo(struct net_device *dev,
  1031. struct ethtool_drvinfo *drvinfo)
  1032. {
  1033. struct adapter *adapter = netdev2adap(dev);
  1034. strcpy(drvinfo->driver, KBUILD_MODNAME);
  1035. strcpy(drvinfo->version, DRV_VERSION);
  1036. strcpy(drvinfo->bus_info, pci_name(to_pci_dev(dev->dev.parent)));
  1037. snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
  1038. "%u.%u.%u.%u, TP %u.%u.%u.%u",
  1039. FW_HDR_FW_VER_MAJOR_GET(adapter->params.dev.fwrev),
  1040. FW_HDR_FW_VER_MINOR_GET(adapter->params.dev.fwrev),
  1041. FW_HDR_FW_VER_MICRO_GET(adapter->params.dev.fwrev),
  1042. FW_HDR_FW_VER_BUILD_GET(adapter->params.dev.fwrev),
  1043. FW_HDR_FW_VER_MAJOR_GET(adapter->params.dev.tprev),
  1044. FW_HDR_FW_VER_MINOR_GET(adapter->params.dev.tprev),
  1045. FW_HDR_FW_VER_MICRO_GET(adapter->params.dev.tprev),
  1046. FW_HDR_FW_VER_BUILD_GET(adapter->params.dev.tprev));
  1047. }
  1048. /*
  1049. * Return current adapter message level.
  1050. */
  1051. static u32 cxgb4vf_get_msglevel(struct net_device *dev)
  1052. {
  1053. return netdev2adap(dev)->msg_enable;
  1054. }
  1055. /*
  1056. * Set current adapter message level.
  1057. */
  1058. static void cxgb4vf_set_msglevel(struct net_device *dev, u32 msglevel)
  1059. {
  1060. netdev2adap(dev)->msg_enable = msglevel;
  1061. }
  1062. /*
  1063. * Return the device's current Queue Set ring size parameters along with the
  1064. * allowed maximum values. Since ethtool doesn't understand the concept of
  1065. * multi-queue devices, we just return the current values associated with the
  1066. * first Queue Set.
  1067. */
  1068. static void cxgb4vf_get_ringparam(struct net_device *dev,
  1069. struct ethtool_ringparam *rp)
  1070. {
  1071. const struct port_info *pi = netdev_priv(dev);
  1072. const struct sge *s = &pi->adapter->sge;
  1073. rp->rx_max_pending = MAX_RX_BUFFERS;
  1074. rp->rx_mini_max_pending = MAX_RSPQ_ENTRIES;
  1075. rp->rx_jumbo_max_pending = 0;
  1076. rp->tx_max_pending = MAX_TXQ_ENTRIES;
  1077. rp->rx_pending = s->ethrxq[pi->first_qset].fl.size - MIN_FL_RESID;
  1078. rp->rx_mini_pending = s->ethrxq[pi->first_qset].rspq.size;
  1079. rp->rx_jumbo_pending = 0;
  1080. rp->tx_pending = s->ethtxq[pi->first_qset].q.size;
  1081. }
  1082. /*
  1083. * Set the Queue Set ring size parameters for the device. Again, since
  1084. * ethtool doesn't allow for the concept of multiple queues per device, we'll
  1085. * apply these new values across all of the Queue Sets associated with the
  1086. * device -- after vetting them of course!
  1087. */
  1088. static int cxgb4vf_set_ringparam(struct net_device *dev,
  1089. struct ethtool_ringparam *rp)
  1090. {
  1091. const struct port_info *pi = netdev_priv(dev);
  1092. struct adapter *adapter = pi->adapter;
  1093. struct sge *s = &adapter->sge;
  1094. int qs;
  1095. if (rp->rx_pending > MAX_RX_BUFFERS ||
  1096. rp->rx_jumbo_pending ||
  1097. rp->tx_pending > MAX_TXQ_ENTRIES ||
  1098. rp->rx_mini_pending > MAX_RSPQ_ENTRIES ||
  1099. rp->rx_mini_pending < MIN_RSPQ_ENTRIES ||
  1100. rp->rx_pending < MIN_FL_ENTRIES ||
  1101. rp->tx_pending < MIN_TXQ_ENTRIES)
  1102. return -EINVAL;
  1103. if (adapter->flags & FULL_INIT_DONE)
  1104. return -EBUSY;
  1105. for (qs = pi->first_qset; qs < pi->first_qset + pi->nqsets; qs++) {
  1106. s->ethrxq[qs].fl.size = rp->rx_pending + MIN_FL_RESID;
  1107. s->ethrxq[qs].rspq.size = rp->rx_mini_pending;
  1108. s->ethtxq[qs].q.size = rp->tx_pending;
  1109. }
  1110. return 0;
  1111. }
  1112. /*
  1113. * Return the interrupt holdoff timer and count for the first Queue Set on the
  1114. * device. Our extension ioctl() (the cxgbtool interface) allows the
  1115. * interrupt holdoff timer to be read on all of the device's Queue Sets.
  1116. */
  1117. static int cxgb4vf_get_coalesce(struct net_device *dev,
  1118. struct ethtool_coalesce *coalesce)
  1119. {
  1120. const struct port_info *pi = netdev_priv(dev);
  1121. const struct adapter *adapter = pi->adapter;
  1122. const struct sge_rspq *rspq = &adapter->sge.ethrxq[pi->first_qset].rspq;
  1123. coalesce->rx_coalesce_usecs = qtimer_val(adapter, rspq);
  1124. coalesce->rx_max_coalesced_frames =
  1125. ((rspq->intr_params & QINTR_CNT_EN)
  1126. ? adapter->sge.counter_val[rspq->pktcnt_idx]
  1127. : 0);
  1128. return 0;
  1129. }
  1130. /*
  1131. * Set the RX interrupt holdoff timer and count for the first Queue Set on the
  1132. * interface. Our extension ioctl() (the cxgbtool interface) allows us to set
  1133. * the interrupt holdoff timer on any of the device's Queue Sets.
  1134. */
  1135. static int cxgb4vf_set_coalesce(struct net_device *dev,
  1136. struct ethtool_coalesce *coalesce)
  1137. {
  1138. const struct port_info *pi = netdev_priv(dev);
  1139. struct adapter *adapter = pi->adapter;
  1140. return set_rxq_intr_params(adapter,
  1141. &adapter->sge.ethrxq[pi->first_qset].rspq,
  1142. coalesce->rx_coalesce_usecs,
  1143. coalesce->rx_max_coalesced_frames);
  1144. }
  1145. /*
  1146. * Report current port link pause parameter settings.
  1147. */
  1148. static void cxgb4vf_get_pauseparam(struct net_device *dev,
  1149. struct ethtool_pauseparam *pauseparam)
  1150. {
  1151. struct port_info *pi = netdev_priv(dev);
  1152. pauseparam->autoneg = (pi->link_cfg.requested_fc & PAUSE_AUTONEG) != 0;
  1153. pauseparam->rx_pause = (pi->link_cfg.fc & PAUSE_RX) != 0;
  1154. pauseparam->tx_pause = (pi->link_cfg.fc & PAUSE_TX) != 0;
  1155. }
  1156. /*
  1157. * Return whether RX Checksum Offloading is currently enabled for the device.
  1158. */
  1159. static u32 cxgb4vf_get_rx_csum(struct net_device *dev)
  1160. {
  1161. struct port_info *pi = netdev_priv(dev);
  1162. return (pi->rx_offload & RX_CSO) != 0;
  1163. }
  1164. /*
  1165. * Turn RX Checksum Offloading on or off for the device.
  1166. */
  1167. static int cxgb4vf_set_rx_csum(struct net_device *dev, u32 csum)
  1168. {
  1169. struct port_info *pi = netdev_priv(dev);
  1170. if (csum)
  1171. pi->rx_offload |= RX_CSO;
  1172. else
  1173. pi->rx_offload &= ~RX_CSO;
  1174. return 0;
  1175. }
  1176. /*
  1177. * Identify the port by blinking the port's LED.
  1178. */
  1179. static int cxgb4vf_phys_id(struct net_device *dev, u32 id)
  1180. {
  1181. struct port_info *pi = netdev_priv(dev);
  1182. return t4vf_identify_port(pi->adapter, pi->viid, 5);
  1183. }
  1184. /*
  1185. * Port stats maintained per queue of the port.
  1186. */
  1187. struct queue_port_stats {
  1188. u64 tso;
  1189. u64 tx_csum;
  1190. u64 rx_csum;
  1191. u64 vlan_ex;
  1192. u64 vlan_ins;
  1193. };
  1194. /*
  1195. * Strings for the ETH_SS_STATS statistics set ("ethtool -S"). Note that
  1196. * these need to match the order of statistics returned by
  1197. * t4vf_get_port_stats().
  1198. */
  1199. static const char stats_strings[][ETH_GSTRING_LEN] = {
  1200. /*
  1201. * These must match the layout of the t4vf_port_stats structure.
  1202. */
  1203. "TxBroadcastBytes ",
  1204. "TxBroadcastFrames ",
  1205. "TxMulticastBytes ",
  1206. "TxMulticastFrames ",
  1207. "TxUnicastBytes ",
  1208. "TxUnicastFrames ",
  1209. "TxDroppedFrames ",
  1210. "TxOffloadBytes ",
  1211. "TxOffloadFrames ",
  1212. "RxBroadcastBytes ",
  1213. "RxBroadcastFrames ",
  1214. "RxMulticastBytes ",
  1215. "RxMulticastFrames ",
  1216. "RxUnicastBytes ",
  1217. "RxUnicastFrames ",
  1218. "RxErrorFrames ",
  1219. /*
  1220. * These are accumulated per-queue statistics and must match the
  1221. * order of the fields in the queue_port_stats structure.
  1222. */
  1223. "TSO ",
  1224. "TxCsumOffload ",
  1225. "RxCsumGood ",
  1226. "VLANextractions ",
  1227. "VLANinsertions ",
  1228. };
  1229. /*
  1230. * Return the number of statistics in the specified statistics set.
  1231. */
  1232. static int cxgb4vf_get_sset_count(struct net_device *dev, int sset)
  1233. {
  1234. switch (sset) {
  1235. case ETH_SS_STATS:
  1236. return ARRAY_SIZE(stats_strings);
  1237. default:
  1238. return -EOPNOTSUPP;
  1239. }
  1240. /*NOTREACHED*/
  1241. }
  1242. /*
  1243. * Return the strings for the specified statistics set.
  1244. */
  1245. static void cxgb4vf_get_strings(struct net_device *dev,
  1246. u32 sset,
  1247. u8 *data)
  1248. {
  1249. switch (sset) {
  1250. case ETH_SS_STATS:
  1251. memcpy(data, stats_strings, sizeof(stats_strings));
  1252. break;
  1253. }
  1254. }
  1255. /*
  1256. * Small utility routine to accumulate queue statistics across the queues of
  1257. * a "port".
  1258. */
  1259. static void collect_sge_port_stats(const struct adapter *adapter,
  1260. const struct port_info *pi,
  1261. struct queue_port_stats *stats)
  1262. {
  1263. const struct sge_eth_txq *txq = &adapter->sge.ethtxq[pi->first_qset];
  1264. const struct sge_eth_rxq *rxq = &adapter->sge.ethrxq[pi->first_qset];
  1265. int qs;
  1266. memset(stats, 0, sizeof(*stats));
  1267. for (qs = 0; qs < pi->nqsets; qs++, rxq++, txq++) {
  1268. stats->tso += txq->tso;
  1269. stats->tx_csum += txq->tx_cso;
  1270. stats->rx_csum += rxq->stats.rx_cso;
  1271. stats->vlan_ex += rxq->stats.vlan_ex;
  1272. stats->vlan_ins += txq->vlan_ins;
  1273. }
  1274. }
  1275. /*
  1276. * Return the ETH_SS_STATS statistics set.
  1277. */
  1278. static void cxgb4vf_get_ethtool_stats(struct net_device *dev,
  1279. struct ethtool_stats *stats,
  1280. u64 *data)
  1281. {
  1282. struct port_info *pi = netdev2pinfo(dev);
  1283. struct adapter *adapter = pi->adapter;
  1284. int err = t4vf_get_port_stats(adapter, pi->pidx,
  1285. (struct t4vf_port_stats *)data);
  1286. if (err)
  1287. memset(data, 0, sizeof(struct t4vf_port_stats));
  1288. data += sizeof(struct t4vf_port_stats) / sizeof(u64);
  1289. collect_sge_port_stats(adapter, pi, (struct queue_port_stats *)data);
  1290. }
  1291. /*
  1292. * Return the size of our register map.
  1293. */
  1294. static int cxgb4vf_get_regs_len(struct net_device *dev)
  1295. {
  1296. return T4VF_REGMAP_SIZE;
  1297. }
  1298. /*
  1299. * Dump a block of registers, start to end inclusive, into a buffer.
  1300. */
  1301. static void reg_block_dump(struct adapter *adapter, void *regbuf,
  1302. unsigned int start, unsigned int end)
  1303. {
  1304. u32 *bp = regbuf + start - T4VF_REGMAP_START;
  1305. for ( ; start <= end; start += sizeof(u32)) {
  1306. /*
  1307. * Avoid reading the Mailbox Control register since that
  1308. * can trigger a Mailbox Ownership Arbitration cycle and
  1309. * interfere with communication with the firmware.
  1310. */
  1311. if (start == T4VF_CIM_BASE_ADDR + CIM_VF_EXT_MAILBOX_CTRL)
  1312. *bp++ = 0xffff;
  1313. else
  1314. *bp++ = t4_read_reg(adapter, start);
  1315. }
  1316. }
  1317. /*
  1318. * Copy our entire register map into the provided buffer.
  1319. */
  1320. static void cxgb4vf_get_regs(struct net_device *dev,
  1321. struct ethtool_regs *regs,
  1322. void *regbuf)
  1323. {
  1324. struct adapter *adapter = netdev2adap(dev);
  1325. regs->version = mk_adap_vers(adapter);
  1326. /*
  1327. * Fill in register buffer with our register map.
  1328. */
  1329. memset(regbuf, 0, T4VF_REGMAP_SIZE);
  1330. reg_block_dump(adapter, regbuf,
  1331. T4VF_SGE_BASE_ADDR + T4VF_MOD_MAP_SGE_FIRST,
  1332. T4VF_SGE_BASE_ADDR + T4VF_MOD_MAP_SGE_LAST);
  1333. reg_block_dump(adapter, regbuf,
  1334. T4VF_MPS_BASE_ADDR + T4VF_MOD_MAP_MPS_FIRST,
  1335. T4VF_MPS_BASE_ADDR + T4VF_MOD_MAP_MPS_LAST);
  1336. reg_block_dump(adapter, regbuf,
  1337. T4VF_PL_BASE_ADDR + T4VF_MOD_MAP_PL_FIRST,
  1338. T4VF_PL_BASE_ADDR + T4VF_MOD_MAP_PL_LAST);
  1339. reg_block_dump(adapter, regbuf,
  1340. T4VF_CIM_BASE_ADDR + T4VF_MOD_MAP_CIM_FIRST,
  1341. T4VF_CIM_BASE_ADDR + T4VF_MOD_MAP_CIM_LAST);
  1342. reg_block_dump(adapter, regbuf,
  1343. T4VF_MBDATA_BASE_ADDR + T4VF_MBDATA_FIRST,
  1344. T4VF_MBDATA_BASE_ADDR + T4VF_MBDATA_LAST);
  1345. }
  1346. /*
  1347. * Report current Wake On LAN settings.
  1348. */
  1349. static void cxgb4vf_get_wol(struct net_device *dev,
  1350. struct ethtool_wolinfo *wol)
  1351. {
  1352. wol->supported = 0;
  1353. wol->wolopts = 0;
  1354. memset(&wol->sopass, 0, sizeof(wol->sopass));
  1355. }
  1356. /*
  1357. * Set TCP Segmentation Offloading feature capabilities.
  1358. */
  1359. static int cxgb4vf_set_tso(struct net_device *dev, u32 tso)
  1360. {
  1361. if (tso)
  1362. dev->features |= NETIF_F_TSO | NETIF_F_TSO6;
  1363. else
  1364. dev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
  1365. return 0;
  1366. }
  1367. static struct ethtool_ops cxgb4vf_ethtool_ops = {
  1368. .get_settings = cxgb4vf_get_settings,
  1369. .get_drvinfo = cxgb4vf_get_drvinfo,
  1370. .get_msglevel = cxgb4vf_get_msglevel,
  1371. .set_msglevel = cxgb4vf_set_msglevel,
  1372. .get_ringparam = cxgb4vf_get_ringparam,
  1373. .set_ringparam = cxgb4vf_set_ringparam,
  1374. .get_coalesce = cxgb4vf_get_coalesce,
  1375. .set_coalesce = cxgb4vf_set_coalesce,
  1376. .get_pauseparam = cxgb4vf_get_pauseparam,
  1377. .get_rx_csum = cxgb4vf_get_rx_csum,
  1378. .set_rx_csum = cxgb4vf_set_rx_csum,
  1379. .set_tx_csum = ethtool_op_set_tx_ipv6_csum,
  1380. .set_sg = ethtool_op_set_sg,
  1381. .get_link = ethtool_op_get_link,
  1382. .get_strings = cxgb4vf_get_strings,
  1383. .phys_id = cxgb4vf_phys_id,
  1384. .get_sset_count = cxgb4vf_get_sset_count,
  1385. .get_ethtool_stats = cxgb4vf_get_ethtool_stats,
  1386. .get_regs_len = cxgb4vf_get_regs_len,
  1387. .get_regs = cxgb4vf_get_regs,
  1388. .get_wol = cxgb4vf_get_wol,
  1389. .set_tso = cxgb4vf_set_tso,
  1390. };
  1391. /*
  1392. * /sys/kernel/debug/cxgb4vf support code and data.
  1393. * ================================================
  1394. */
  1395. /*
  1396. * Show SGE Queue Set information. We display QPL Queues Sets per line.
  1397. */
  1398. #define QPL 4
  1399. static int sge_qinfo_show(struct seq_file *seq, void *v)
  1400. {
  1401. struct adapter *adapter = seq->private;
  1402. int eth_entries = DIV_ROUND_UP(adapter->sge.ethqsets, QPL);
  1403. int qs, r = (uintptr_t)v - 1;
  1404. if (r)
  1405. seq_putc(seq, '\n');
  1406. #define S3(fmt_spec, s, v) \
  1407. do {\
  1408. seq_printf(seq, "%-12s", s); \
  1409. for (qs = 0; qs < n; ++qs) \
  1410. seq_printf(seq, " %16" fmt_spec, v); \
  1411. seq_putc(seq, '\n'); \
  1412. } while (0)
  1413. #define S(s, v) S3("s", s, v)
  1414. #define T(s, v) S3("u", s, txq[qs].v)
  1415. #define R(s, v) S3("u", s, rxq[qs].v)
  1416. if (r < eth_entries) {
  1417. const struct sge_eth_rxq *rxq = &adapter->sge.ethrxq[r * QPL];
  1418. const struct sge_eth_txq *txq = &adapter->sge.ethtxq[r * QPL];
  1419. int n = min(QPL, adapter->sge.ethqsets - QPL * r);
  1420. S("QType:", "Ethernet");
  1421. S("Interface:",
  1422. (rxq[qs].rspq.netdev
  1423. ? rxq[qs].rspq.netdev->name
  1424. : "N/A"));
  1425. S3("d", "Port:",
  1426. (rxq[qs].rspq.netdev
  1427. ? ((struct port_info *)
  1428. netdev_priv(rxq[qs].rspq.netdev))->port_id
  1429. : -1));
  1430. T("TxQ ID:", q.abs_id);
  1431. T("TxQ size:", q.size);
  1432. T("TxQ inuse:", q.in_use);
  1433. T("TxQ PIdx:", q.pidx);
  1434. T("TxQ CIdx:", q.cidx);
  1435. R("RspQ ID:", rspq.abs_id);
  1436. R("RspQ size:", rspq.size);
  1437. R("RspQE size:", rspq.iqe_len);
  1438. S3("u", "Intr delay:", qtimer_val(adapter, &rxq[qs].rspq));
  1439. S3("u", "Intr pktcnt:",
  1440. adapter->sge.counter_val[rxq[qs].rspq.pktcnt_idx]);
  1441. R("RspQ CIdx:", rspq.cidx);
  1442. R("RspQ Gen:", rspq.gen);
  1443. R("FL ID:", fl.abs_id);
  1444. R("FL size:", fl.size - MIN_FL_RESID);
  1445. R("FL avail:", fl.avail);
  1446. R("FL PIdx:", fl.pidx);
  1447. R("FL CIdx:", fl.cidx);
  1448. return 0;
  1449. }
  1450. r -= eth_entries;
  1451. if (r == 0) {
  1452. const struct sge_rspq *evtq = &adapter->sge.fw_evtq;
  1453. seq_printf(seq, "%-12s %16s\n", "QType:", "FW event queue");
  1454. seq_printf(seq, "%-12s %16u\n", "RspQ ID:", evtq->abs_id);
  1455. seq_printf(seq, "%-12s %16u\n", "Intr delay:",
  1456. qtimer_val(adapter, evtq));
  1457. seq_printf(seq, "%-12s %16u\n", "Intr pktcnt:",
  1458. adapter->sge.counter_val[evtq->pktcnt_idx]);
  1459. seq_printf(seq, "%-12s %16u\n", "RspQ Cidx:", evtq->cidx);
  1460. seq_printf(seq, "%-12s %16u\n", "RspQ Gen:", evtq->gen);
  1461. } else if (r == 1) {
  1462. const struct sge_rspq *intrq = &adapter->sge.intrq;
  1463. seq_printf(seq, "%-12s %16s\n", "QType:", "Interrupt Queue");
  1464. seq_printf(seq, "%-12s %16u\n", "RspQ ID:", intrq->abs_id);
  1465. seq_printf(seq, "%-12s %16u\n", "Intr delay:",
  1466. qtimer_val(adapter, intrq));
  1467. seq_printf(seq, "%-12s %16u\n", "Intr pktcnt:",
  1468. adapter->sge.counter_val[intrq->pktcnt_idx]);
  1469. seq_printf(seq, "%-12s %16u\n", "RspQ Cidx:", intrq->cidx);
  1470. seq_printf(seq, "%-12s %16u\n", "RspQ Gen:", intrq->gen);
  1471. }
  1472. #undef R
  1473. #undef T
  1474. #undef S
  1475. #undef S3
  1476. return 0;
  1477. }
  1478. /*
  1479. * Return the number of "entries" in our "file". We group the multi-Queue
  1480. * sections with QPL Queue Sets per "entry". The sections of the output are:
  1481. *
  1482. * Ethernet RX/TX Queue Sets
  1483. * Firmware Event Queue
  1484. * Forwarded Interrupt Queue (if in MSI mode)
  1485. */
  1486. static int sge_queue_entries(const struct adapter *adapter)
  1487. {
  1488. return DIV_ROUND_UP(adapter->sge.ethqsets, QPL) + 1 +
  1489. ((adapter->flags & USING_MSI) != 0);
  1490. }
  1491. static void *sge_queue_start(struct seq_file *seq, loff_t *pos)
  1492. {
  1493. int entries = sge_queue_entries(seq->private);
  1494. return *pos < entries ? (void *)((uintptr_t)*pos + 1) : NULL;
  1495. }
  1496. static void sge_queue_stop(struct seq_file *seq, void *v)
  1497. {
  1498. }
  1499. static void *sge_queue_next(struct seq_file *seq, void *v, loff_t *pos)
  1500. {
  1501. int entries = sge_queue_entries(seq->private);
  1502. ++*pos;
  1503. return *pos < entries ? (void *)((uintptr_t)*pos + 1) : NULL;
  1504. }
  1505. static const struct seq_operations sge_qinfo_seq_ops = {
  1506. .start = sge_queue_start,
  1507. .next = sge_queue_next,
  1508. .stop = sge_queue_stop,
  1509. .show = sge_qinfo_show
  1510. };
  1511. static int sge_qinfo_open(struct inode *inode, struct file *file)
  1512. {
  1513. int res = seq_open(file, &sge_qinfo_seq_ops);
  1514. if (!res) {
  1515. struct seq_file *seq = file->private_data;
  1516. seq->private = inode->i_private;
  1517. }
  1518. return res;
  1519. }
  1520. static const struct file_operations sge_qinfo_debugfs_fops = {
  1521. .owner = THIS_MODULE,
  1522. .open = sge_qinfo_open,
  1523. .read = seq_read,
  1524. .llseek = seq_lseek,
  1525. .release = seq_release,
  1526. };
  1527. /*
  1528. * Show SGE Queue Set statistics. We display QPL Queues Sets per line.
  1529. */
  1530. #define QPL 4
  1531. static int sge_qstats_show(struct seq_file *seq, void *v)
  1532. {
  1533. struct adapter *adapter = seq->private;
  1534. int eth_entries = DIV_ROUND_UP(adapter->sge.ethqsets, QPL);
  1535. int qs, r = (uintptr_t)v - 1;
  1536. if (r)
  1537. seq_putc(seq, '\n');
  1538. #define S3(fmt, s, v) \
  1539. do { \
  1540. seq_printf(seq, "%-16s", s); \
  1541. for (qs = 0; qs < n; ++qs) \
  1542. seq_printf(seq, " %8" fmt, v); \
  1543. seq_putc(seq, '\n'); \
  1544. } while (0)
  1545. #define S(s, v) S3("s", s, v)
  1546. #define T3(fmt, s, v) S3(fmt, s, txq[qs].v)
  1547. #define T(s, v) T3("lu", s, v)
  1548. #define R3(fmt, s, v) S3(fmt, s, rxq[qs].v)
  1549. #define R(s, v) R3("lu", s, v)
  1550. if (r < eth_entries) {
  1551. const struct sge_eth_rxq *rxq = &adapter->sge.ethrxq[r * QPL];
  1552. const struct sge_eth_txq *txq = &adapter->sge.ethtxq[r * QPL];
  1553. int n = min(QPL, adapter->sge.ethqsets - QPL * r);
  1554. S("QType:", "Ethernet");
  1555. S("Interface:",
  1556. (rxq[qs].rspq.netdev
  1557. ? rxq[qs].rspq.netdev->name
  1558. : "N/A"));
  1559. R3("u", "RspQNullInts:", rspq.unhandled_irqs);
  1560. R("RxPackets:", stats.pkts);
  1561. R("RxCSO:", stats.rx_cso);
  1562. R("VLANxtract:", stats.vlan_ex);
  1563. R("LROmerged:", stats.lro_merged);
  1564. R("LROpackets:", stats.lro_pkts);
  1565. R("RxDrops:", stats.rx_drops);
  1566. T("TSO:", tso);
  1567. T("TxCSO:", tx_cso);
  1568. T("VLANins:", vlan_ins);
  1569. T("TxQFull:", q.stops);
  1570. T("TxQRestarts:", q.restarts);
  1571. T("TxMapErr:", mapping_err);
  1572. R("FLAllocErr:", fl.alloc_failed);
  1573. R("FLLrgAlcErr:", fl.large_alloc_failed);
  1574. R("FLStarving:", fl.starving);
  1575. return 0;
  1576. }
  1577. r -= eth_entries;
  1578. if (r == 0) {
  1579. const struct sge_rspq *evtq = &adapter->sge.fw_evtq;
  1580. seq_printf(seq, "%-8s %16s\n", "QType:", "FW event queue");
  1581. seq_printf(seq, "%-16s %8u\n", "RspQNullInts:",
  1582. evtq->unhandled_irqs);
  1583. seq_printf(seq, "%-16s %8u\n", "RspQ CIdx:", evtq->cidx);
  1584. seq_printf(seq, "%-16s %8u\n", "RspQ Gen:", evtq->gen);
  1585. } else if (r == 1) {
  1586. const struct sge_rspq *intrq = &adapter->sge.intrq;
  1587. seq_printf(seq, "%-8s %16s\n", "QType:", "Interrupt Queue");
  1588. seq_printf(seq, "%-16s %8u\n", "RspQNullInts:",
  1589. intrq->unhandled_irqs);
  1590. seq_printf(seq, "%-16s %8u\n", "RspQ CIdx:", intrq->cidx);
  1591. seq_printf(seq, "%-16s %8u\n", "RspQ Gen:", intrq->gen);
  1592. }
  1593. #undef R
  1594. #undef T
  1595. #undef S
  1596. #undef R3
  1597. #undef T3
  1598. #undef S3
  1599. return 0;
  1600. }
  1601. /*
  1602. * Return the number of "entries" in our "file". We group the multi-Queue
  1603. * sections with QPL Queue Sets per "entry". The sections of the output are:
  1604. *
  1605. * Ethernet RX/TX Queue Sets
  1606. * Firmware Event Queue
  1607. * Forwarded Interrupt Queue (if in MSI mode)
  1608. */
  1609. static int sge_qstats_entries(const struct adapter *adapter)
  1610. {
  1611. return DIV_ROUND_UP(adapter->sge.ethqsets, QPL) + 1 +
  1612. ((adapter->flags & USING_MSI) != 0);
  1613. }
  1614. static void *sge_qstats_start(struct seq_file *seq, loff_t *pos)
  1615. {
  1616. int entries = sge_qstats_entries(seq->private);
  1617. return *pos < entries ? (void *)((uintptr_t)*pos + 1) : NULL;
  1618. }
  1619. static void sge_qstats_stop(struct seq_file *seq, void *v)
  1620. {
  1621. }
  1622. static void *sge_qstats_next(struct seq_file *seq, void *v, loff_t *pos)
  1623. {
  1624. int entries = sge_qstats_entries(seq->private);
  1625. (*pos)++;
  1626. return *pos < entries ? (void *)((uintptr_t)*pos + 1) : NULL;
  1627. }
  1628. static const struct seq_operations sge_qstats_seq_ops = {
  1629. .start = sge_qstats_start,
  1630. .next = sge_qstats_next,
  1631. .stop = sge_qstats_stop,
  1632. .show = sge_qstats_show
  1633. };
  1634. static int sge_qstats_open(struct inode *inode, struct file *file)
  1635. {
  1636. int res = seq_open(file, &sge_qstats_seq_ops);
  1637. if (res == 0) {
  1638. struct seq_file *seq = file->private_data;
  1639. seq->private = inode->i_private;
  1640. }
  1641. return res;
  1642. }
  1643. static const struct file_operations sge_qstats_proc_fops = {
  1644. .owner = THIS_MODULE,
  1645. .open = sge_qstats_open,
  1646. .read = seq_read,
  1647. .llseek = seq_lseek,
  1648. .release = seq_release,
  1649. };
  1650. /*
  1651. * Show PCI-E SR-IOV Virtual Function Resource Limits.
  1652. */
  1653. static int resources_show(struct seq_file *seq, void *v)
  1654. {
  1655. struct adapter *adapter = seq->private;
  1656. struct vf_resources *vfres = &adapter->params.vfres;
  1657. #define S(desc, fmt, var) \
  1658. seq_printf(seq, "%-60s " fmt "\n", \
  1659. desc " (" #var "):", vfres->var)
  1660. S("Virtual Interfaces", "%d", nvi);
  1661. S("Egress Queues", "%d", neq);
  1662. S("Ethernet Control", "%d", nethctrl);
  1663. S("Ingress Queues/w Free Lists/Interrupts", "%d", niqflint);
  1664. S("Ingress Queues", "%d", niq);
  1665. S("Traffic Class", "%d", tc);
  1666. S("Port Access Rights Mask", "%#x", pmask);
  1667. S("MAC Address Filters", "%d", nexactf);
  1668. S("Firmware Command Read Capabilities", "%#x", r_caps);
  1669. S("Firmware Command Write/Execute Capabilities", "%#x", wx_caps);
  1670. #undef S
  1671. return 0;
  1672. }
  1673. static int resources_open(struct inode *inode, struct file *file)
  1674. {
  1675. return single_open(file, resources_show, inode->i_private);
  1676. }
  1677. static const struct file_operations resources_proc_fops = {
  1678. .owner = THIS_MODULE,
  1679. .open = resources_open,
  1680. .read = seq_read,
  1681. .llseek = seq_lseek,
  1682. .release = single_release,
  1683. };
  1684. /*
  1685. * Show Virtual Interfaces.
  1686. */
  1687. static int interfaces_show(struct seq_file *seq, void *v)
  1688. {
  1689. if (v == SEQ_START_TOKEN) {
  1690. seq_puts(seq, "Interface Port VIID\n");
  1691. } else {
  1692. struct adapter *adapter = seq->private;
  1693. int pidx = (uintptr_t)v - 2;
  1694. struct net_device *dev = adapter->port[pidx];
  1695. struct port_info *pi = netdev_priv(dev);
  1696. seq_printf(seq, "%9s %4d %#5x\n",
  1697. dev->name, pi->port_id, pi->viid);
  1698. }
  1699. return 0;
  1700. }
  1701. static inline void *interfaces_get_idx(struct adapter *adapter, loff_t pos)
  1702. {
  1703. return pos <= adapter->params.nports
  1704. ? (void *)(uintptr_t)(pos + 1)
  1705. : NULL;
  1706. }
  1707. static void *interfaces_start(struct seq_file *seq, loff_t *pos)
  1708. {
  1709. return *pos
  1710. ? interfaces_get_idx(seq->private, *pos)
  1711. : SEQ_START_TOKEN;
  1712. }
  1713. static void *interfaces_next(struct seq_file *seq, void *v, loff_t *pos)
  1714. {
  1715. (*pos)++;
  1716. return interfaces_get_idx(seq->private, *pos);
  1717. }
  1718. static void interfaces_stop(struct seq_file *seq, void *v)
  1719. {
  1720. }
  1721. static const struct seq_operations interfaces_seq_ops = {
  1722. .start = interfaces_start,
  1723. .next = interfaces_next,
  1724. .stop = interfaces_stop,
  1725. .show = interfaces_show
  1726. };
  1727. static int interfaces_open(struct inode *inode, struct file *file)
  1728. {
  1729. int res = seq_open(file, &interfaces_seq_ops);
  1730. if (res == 0) {
  1731. struct seq_file *seq = file->private_data;
  1732. seq->private = inode->i_private;
  1733. }
  1734. return res;
  1735. }
  1736. static const struct file_operations interfaces_proc_fops = {
  1737. .owner = THIS_MODULE,
  1738. .open = interfaces_open,
  1739. .read = seq_read,
  1740. .llseek = seq_lseek,
  1741. .release = seq_release,
  1742. };
  1743. /*
  1744. * /sys/kernel/debugfs/cxgb4vf/ files list.
  1745. */
  1746. struct cxgb4vf_debugfs_entry {
  1747. const char *name; /* name of debugfs node */
  1748. mode_t mode; /* file system mode */
  1749. const struct file_operations *fops;
  1750. };
  1751. static struct cxgb4vf_debugfs_entry debugfs_files[] = {
  1752. { "sge_qinfo", S_IRUGO, &sge_qinfo_debugfs_fops },
  1753. { "sge_qstats", S_IRUGO, &sge_qstats_proc_fops },
  1754. { "resources", S_IRUGO, &resources_proc_fops },
  1755. { "interfaces", S_IRUGO, &interfaces_proc_fops },
  1756. };
  1757. /*
  1758. * Module and device initialization and cleanup code.
  1759. * ==================================================
  1760. */
  1761. /*
  1762. * Set up out /sys/kernel/debug/cxgb4vf sub-nodes. We assume that the
  1763. * directory (debugfs_root) has already been set up.
  1764. */
  1765. static int __devinit setup_debugfs(struct adapter *adapter)
  1766. {
  1767. int i;
  1768. BUG_ON(adapter->debugfs_root == NULL);
  1769. /*
  1770. * Debugfs support is best effort.
  1771. */
  1772. for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
  1773. (void)debugfs_create_file(debugfs_files[i].name,
  1774. debugfs_files[i].mode,
  1775. adapter->debugfs_root,
  1776. (void *)adapter,
  1777. debugfs_files[i].fops);
  1778. return 0;
  1779. }
  1780. /*
  1781. * Tear down the /sys/kernel/debug/cxgb4vf sub-nodes created above. We leave
  1782. * it to our caller to tear down the directory (debugfs_root).
  1783. */
  1784. static void __devexit cleanup_debugfs(struct adapter *adapter)
  1785. {
  1786. BUG_ON(adapter->debugfs_root == NULL);
  1787. /*
  1788. * Unlike our sister routine cleanup_proc(), we don't need to remove
  1789. * individual entries because a call will be made to
  1790. * debugfs_remove_recursive(). We just need to clean up any ancillary
  1791. * persistent state.
  1792. */
  1793. /* nothing to do */
  1794. }
  1795. /*
  1796. * Perform early "adapter" initialization. This is where we discover what
  1797. * adapter parameters we're going to be using and initialize basic adapter
  1798. * hardware support.
  1799. */
  1800. static int adap_init0(struct adapter *adapter)
  1801. {
  1802. struct vf_resources *vfres = &adapter->params.vfres;
  1803. struct sge_params *sge_params = &adapter->params.sge;
  1804. struct sge *s = &adapter->sge;
  1805. unsigned int ethqsets;
  1806. int err;
  1807. /*
  1808. * Wait for the device to become ready before proceeding ...
  1809. */
  1810. err = t4vf_wait_dev_ready(adapter);
  1811. if (err) {
  1812. dev_err(adapter->pdev_dev, "device didn't become ready:"
  1813. " err=%d\n", err);
  1814. return err;
  1815. }
  1816. /*
  1817. * Grab basic operational parameters. These will predominantly have
  1818. * been set up by the Physical Function Driver or will be hard coded
  1819. * into the adapter. We just have to live with them ... Note that
  1820. * we _must_ get our VPD parameters before our SGE parameters because
  1821. * we need to know the adapter's core clock from the VPD in order to
  1822. * properly decode the SGE Timer Values.
  1823. */
  1824. err = t4vf_get_dev_params(adapter);
  1825. if (err) {
  1826. dev_err(adapter->pdev_dev, "unable to retrieve adapter"
  1827. " device parameters: err=%d\n", err);
  1828. return err;
  1829. }
  1830. err = t4vf_get_vpd_params(adapter);
  1831. if (err) {
  1832. dev_err(adapter->pdev_dev, "unable to retrieve adapter"
  1833. " VPD parameters: err=%d\n", err);
  1834. return err;
  1835. }
  1836. err = t4vf_get_sge_params(adapter);
  1837. if (err) {
  1838. dev_err(adapter->pdev_dev, "unable to retrieve adapter"
  1839. " SGE parameters: err=%d\n", err);
  1840. return err;
  1841. }
  1842. err = t4vf_get_rss_glb_config(adapter);
  1843. if (err) {
  1844. dev_err(adapter->pdev_dev, "unable to retrieve adapter"
  1845. " RSS parameters: err=%d\n", err);
  1846. return err;
  1847. }
  1848. if (adapter->params.rss.mode !=
  1849. FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL) {
  1850. dev_err(adapter->pdev_dev, "unable to operate with global RSS"
  1851. " mode %d\n", adapter->params.rss.mode);
  1852. return -EINVAL;
  1853. }
  1854. err = t4vf_sge_init(adapter);
  1855. if (err) {
  1856. dev_err(adapter->pdev_dev, "unable to use adapter parameters:"
  1857. " err=%d\n", err);
  1858. return err;
  1859. }
  1860. /*
  1861. * Retrieve our RX interrupt holdoff timer values and counter
  1862. * threshold values from the SGE parameters.
  1863. */
  1864. s->timer_val[0] = core_ticks_to_us(adapter,
  1865. TIMERVALUE0_GET(sge_params->sge_timer_value_0_and_1));
  1866. s->timer_val[1] = core_ticks_to_us(adapter,
  1867. TIMERVALUE1_GET(sge_params->sge_timer_value_0_and_1));
  1868. s->timer_val[2] = core_ticks_to_us(adapter,
  1869. TIMERVALUE0_GET(sge_params->sge_timer_value_2_and_3));
  1870. s->timer_val[3] = core_ticks_to_us(adapter,
  1871. TIMERVALUE1_GET(sge_params->sge_timer_value_2_and_3));
  1872. s->timer_val[4] = core_ticks_to_us(adapter,
  1873. TIMERVALUE0_GET(sge_params->sge_timer_value_4_and_5));
  1874. s->timer_val[5] = core_ticks_to_us(adapter,
  1875. TIMERVALUE1_GET(sge_params->sge_timer_value_4_and_5));
  1876. s->counter_val[0] =
  1877. THRESHOLD_0_GET(sge_params->sge_ingress_rx_threshold);
  1878. s->counter_val[1] =
  1879. THRESHOLD_1_GET(sge_params->sge_ingress_rx_threshold);
  1880. s->counter_val[2] =
  1881. THRESHOLD_2_GET(sge_params->sge_ingress_rx_threshold);
  1882. s->counter_val[3] =
  1883. THRESHOLD_3_GET(sge_params->sge_ingress_rx_threshold);
  1884. /*
  1885. * Grab our Virtual Interface resource allocation, extract the
  1886. * features that we're interested in and do a bit of sanity testing on
  1887. * what we discover.
  1888. */
  1889. err = t4vf_get_vfres(adapter);
  1890. if (err) {
  1891. dev_err(adapter->pdev_dev, "unable to get virtual interface"
  1892. " resources: err=%d\n", err);
  1893. return err;
  1894. }
  1895. /*
  1896. * The number of "ports" which we support is equal to the number of
  1897. * Virtual Interfaces with which we've been provisioned.
  1898. */
  1899. adapter->params.nports = vfres->nvi;
  1900. if (adapter->params.nports > MAX_NPORTS) {
  1901. dev_warn(adapter->pdev_dev, "only using %d of %d allowed"
  1902. " virtual interfaces\n", MAX_NPORTS,
  1903. adapter->params.nports);
  1904. adapter->params.nports = MAX_NPORTS;
  1905. }
  1906. /*
  1907. * We need to reserve a number of the ingress queues with Free List
  1908. * and Interrupt capabilities for special interrupt purposes (like
  1909. * asynchronous firmware messages, or forwarded interrupts if we're
  1910. * using MSI). The rest of the FL/Intr-capable ingress queues will be
  1911. * matched up one-for-one with Ethernet/Control egress queues in order
  1912. * to form "Queue Sets" which will be aportioned between the "ports".
  1913. * For each Queue Set, we'll need the ability to allocate two Egress
  1914. * Contexts -- one for the Ingress Queue Free List and one for the TX
  1915. * Ethernet Queue.
  1916. */
  1917. ethqsets = vfres->niqflint - INGQ_EXTRAS;
  1918. if (vfres->nethctrl != ethqsets) {
  1919. dev_warn(adapter->pdev_dev, "unequal number of [available]"
  1920. " ingress/egress queues (%d/%d); using minimum for"
  1921. " number of Queue Sets\n", ethqsets, vfres->nethctrl);
  1922. ethqsets = min(vfres->nethctrl, ethqsets);
  1923. }
  1924. if (vfres->neq < ethqsets*2) {
  1925. dev_warn(adapter->pdev_dev, "Not enough Egress Contexts (%d)"
  1926. " to support Queue Sets (%d); reducing allowed Queue"
  1927. " Sets\n", vfres->neq, ethqsets);
  1928. ethqsets = vfres->neq/2;
  1929. }
  1930. if (ethqsets > MAX_ETH_QSETS) {
  1931. dev_warn(adapter->pdev_dev, "only using %d of %d allowed Queue"
  1932. " Sets\n", MAX_ETH_QSETS, adapter->sge.max_ethqsets);
  1933. ethqsets = MAX_ETH_QSETS;
  1934. }
  1935. if (vfres->niq != 0 || vfres->neq > ethqsets*2) {
  1936. dev_warn(adapter->pdev_dev, "unused resources niq/neq (%d/%d)"
  1937. " ignored\n", vfres->niq, vfres->neq - ethqsets*2);
  1938. }
  1939. adapter->sge.max_ethqsets = ethqsets;
  1940. /*
  1941. * Check for various parameter sanity issues. Most checks simply
  1942. * result in us using fewer resources than our provissioning but we
  1943. * do need at least one "port" with which to work ...
  1944. */
  1945. if (adapter->sge.max_ethqsets < adapter->params.nports) {
  1946. dev_warn(adapter->pdev_dev, "only using %d of %d available"
  1947. " virtual interfaces (too few Queue Sets)\n",
  1948. adapter->sge.max_ethqsets, adapter->params.nports);
  1949. adapter->params.nports = adapter->sge.max_ethqsets;
  1950. }
  1951. if (adapter->params.nports == 0) {
  1952. dev_err(adapter->pdev_dev, "no virtual interfaces configured/"
  1953. "usable!\n");
  1954. return -EINVAL;
  1955. }
  1956. return 0;
  1957. }
  1958. static inline void init_rspq(struct sge_rspq *rspq, u8 timer_idx,
  1959. u8 pkt_cnt_idx, unsigned int size,
  1960. unsigned int iqe_size)
  1961. {
  1962. rspq->intr_params = (QINTR_TIMER_IDX(timer_idx) |
  1963. (pkt_cnt_idx < SGE_NCOUNTERS ? QINTR_CNT_EN : 0));
  1964. rspq->pktcnt_idx = (pkt_cnt_idx < SGE_NCOUNTERS
  1965. ? pkt_cnt_idx
  1966. : 0);
  1967. rspq->iqe_len = iqe_size;
  1968. rspq->size = size;
  1969. }
  1970. /*
  1971. * Perform default configuration of DMA queues depending on the number and
  1972. * type of ports we found and the number of available CPUs. Most settings can
  1973. * be modified by the admin via ethtool and cxgbtool prior to the adapter
  1974. * being brought up for the first time.
  1975. */
  1976. static void __devinit cfg_queues(struct adapter *adapter)
  1977. {
  1978. struct sge *s = &adapter->sge;
  1979. int q10g, n10g, qidx, pidx, qs;
  1980. /*
  1981. * We should not be called till we know how many Queue Sets we can
  1982. * support. In particular, this means that we need to know what kind
  1983. * of interrupts we'll be using ...
  1984. */
  1985. BUG_ON((adapter->flags & (USING_MSIX|USING_MSI)) == 0);
  1986. /*
  1987. * Count the number of 10GbE Virtual Interfaces that we have.
  1988. */
  1989. n10g = 0;
  1990. for_each_port(adapter, pidx)
  1991. n10g += is_10g_port(&adap2pinfo(adapter, pidx)->link_cfg);
  1992. /*
  1993. * We default to 1 queue per non-10G port and up to # of cores queues
  1994. * per 10G port.
  1995. */
  1996. if (n10g == 0)
  1997. q10g = 0;
  1998. else {
  1999. int n1g = (adapter->params.nports - n10g);
  2000. q10g = (adapter->sge.max_ethqsets - n1g) / n10g;
  2001. if (q10g > num_online_cpus())
  2002. q10g = num_online_cpus();
  2003. }
  2004. /*
  2005. * Allocate the "Queue Sets" to the various Virtual Interfaces.
  2006. * The layout will be established in setup_sge_queues() when the
  2007. * adapter is brough up for the first time.
  2008. */
  2009. qidx = 0;
  2010. for_each_port(adapter, pidx) {
  2011. struct port_info *pi = adap2pinfo(adapter, pidx);
  2012. pi->first_qset = qidx;
  2013. pi->nqsets = is_10g_port(&pi->link_cfg) ? q10g : 1;
  2014. qidx += pi->nqsets;
  2015. }
  2016. s->ethqsets = qidx;
  2017. /*
  2018. * Set up default Queue Set parameters ... Start off with the
  2019. * shortest interrupt holdoff timer.
  2020. */
  2021. for (qs = 0; qs < s->max_ethqsets; qs++) {
  2022. struct sge_eth_rxq *rxq = &s->ethrxq[qs];
  2023. struct sge_eth_txq *txq = &s->ethtxq[qs];
  2024. init_rspq(&rxq->rspq, 0, 0, 1024, L1_CACHE_BYTES);
  2025. rxq->fl.size = 72;
  2026. txq->q.size = 1024;
  2027. }
  2028. /*
  2029. * The firmware event queue is used for link state changes and
  2030. * notifications of TX DMA completions.
  2031. */
  2032. init_rspq(&s->fw_evtq, SGE_TIMER_RSTRT_CNTR, 0, 512,
  2033. L1_CACHE_BYTES);
  2034. /*
  2035. * The forwarded interrupt queue is used when we're in MSI interrupt
  2036. * mode. In this mode all interrupts associated with RX queues will
  2037. * be forwarded to a single queue which we'll associate with our MSI
  2038. * interrupt vector. The messages dropped in the forwarded interrupt
  2039. * queue will indicate which ingress queue needs servicing ... This
  2040. * queue needs to be large enough to accommodate all of the ingress
  2041. * queues which are forwarding their interrupt (+1 to prevent the PIDX
  2042. * from equalling the CIDX if every ingress queue has an outstanding
  2043. * interrupt). The queue doesn't need to be any larger because no
  2044. * ingress queue will ever have more than one outstanding interrupt at
  2045. * any time ...
  2046. */
  2047. init_rspq(&s->intrq, SGE_TIMER_RSTRT_CNTR, 0, MSIX_ENTRIES + 1,
  2048. L1_CACHE_BYTES);
  2049. }
  2050. /*
  2051. * Reduce the number of Ethernet queues across all ports to at most n.
  2052. * n provides at least one queue per port.
  2053. */
  2054. static void __devinit reduce_ethqs(struct adapter *adapter, int n)
  2055. {
  2056. int i;
  2057. struct port_info *pi;
  2058. /*
  2059. * While we have too many active Ether Queue Sets, interate across the
  2060. * "ports" and reduce their individual Queue Set allocations.
  2061. */
  2062. BUG_ON(n < adapter->params.nports);
  2063. while (n < adapter->sge.ethqsets)
  2064. for_each_port(adapter, i) {
  2065. pi = adap2pinfo(adapter, i);
  2066. if (pi->nqsets > 1) {
  2067. pi->nqsets--;
  2068. adapter->sge.ethqsets--;
  2069. if (adapter->sge.ethqsets <= n)
  2070. break;
  2071. }
  2072. }
  2073. /*
  2074. * Reassign the starting Queue Sets for each of the "ports" ...
  2075. */
  2076. n = 0;
  2077. for_each_port(adapter, i) {
  2078. pi = adap2pinfo(adapter, i);
  2079. pi->first_qset = n;
  2080. n += pi->nqsets;
  2081. }
  2082. }
  2083. /*
  2084. * We need to grab enough MSI-X vectors to cover our interrupt needs. Ideally
  2085. * we get a separate MSI-X vector for every "Queue Set" plus any extras we
  2086. * need. Minimally we need one for every Virtual Interface plus those needed
  2087. * for our "extras". Note that this process may lower the maximum number of
  2088. * allowed Queue Sets ...
  2089. */
  2090. static int __devinit enable_msix(struct adapter *adapter)
  2091. {
  2092. int i, err, want, need;
  2093. struct msix_entry entries[MSIX_ENTRIES];
  2094. struct sge *s = &adapter->sge;
  2095. for (i = 0; i < MSIX_ENTRIES; ++i)
  2096. entries[i].entry = i;
  2097. /*
  2098. * We _want_ enough MSI-X interrupts to cover all of our "Queue Sets"
  2099. * plus those needed for our "extras" (for example, the firmware
  2100. * message queue). We _need_ at least one "Queue Set" per Virtual
  2101. * Interface plus those needed for our "extras". So now we get to see
  2102. * if the song is right ...
  2103. */
  2104. want = s->max_ethqsets + MSIX_EXTRAS;
  2105. need = adapter->params.nports + MSIX_EXTRAS;
  2106. while ((err = pci_enable_msix(adapter->pdev, entries, want)) >= need)
  2107. want = err;
  2108. if (err == 0) {
  2109. int nqsets = want - MSIX_EXTRAS;
  2110. if (nqsets < s->max_ethqsets) {
  2111. dev_warn(adapter->pdev_dev, "only enough MSI-X vectors"
  2112. " for %d Queue Sets\n", nqsets);
  2113. s->max_ethqsets = nqsets;
  2114. if (nqsets < s->ethqsets)
  2115. reduce_ethqs(adapter, nqsets);
  2116. }
  2117. for (i = 0; i < want; ++i)
  2118. adapter->msix_info[i].vec = entries[i].vector;
  2119. } else if (err > 0) {
  2120. pci_disable_msix(adapter->pdev);
  2121. dev_info(adapter->pdev_dev, "only %d MSI-X vectors left,"
  2122. " not using MSI-X\n", err);
  2123. }
  2124. return err;
  2125. }
  2126. #ifdef HAVE_NET_DEVICE_OPS
  2127. static const struct net_device_ops cxgb4vf_netdev_ops = {
  2128. .ndo_open = cxgb4vf_open,
  2129. .ndo_stop = cxgb4vf_stop,
  2130. .ndo_start_xmit = t4vf_eth_xmit,
  2131. .ndo_get_stats = cxgb4vf_get_stats,
  2132. .ndo_set_rx_mode = cxgb4vf_set_rxmode,
  2133. .ndo_set_mac_address = cxgb4vf_set_mac_addr,
  2134. .ndo_select_queue = cxgb4vf_select_queue,
  2135. .ndo_validate_addr = eth_validate_addr,
  2136. .ndo_do_ioctl = cxgb4vf_do_ioctl,
  2137. .ndo_change_mtu = cxgb4vf_change_mtu,
  2138. .ndo_vlan_rx_register = cxgb4vf_vlan_rx_register,
  2139. #ifdef CONFIG_NET_POLL_CONTROLLER
  2140. .ndo_poll_controller = cxgb4vf_poll_controller,
  2141. #endif
  2142. };
  2143. #endif
  2144. /*
  2145. * "Probe" a device: initialize a device and construct all kernel and driver
  2146. * state needed to manage the device. This routine is called "init_one" in
  2147. * the PF Driver ...
  2148. */
  2149. static int __devinit cxgb4vf_pci_probe(struct pci_dev *pdev,
  2150. const struct pci_device_id *ent)
  2151. {
  2152. static int version_printed;
  2153. int pci_using_dac;
  2154. int err, pidx;
  2155. unsigned int pmask;
  2156. struct adapter *adapter;
  2157. struct port_info *pi;
  2158. struct net_device *netdev;
  2159. /*
  2160. * Vet our module parameters.
  2161. */
  2162. if (msi != MSI_MSIX && msi != MSI_MSI) {
  2163. dev_err(&pdev->dev, "bad module parameter msi=%d; must be %d"
  2164. " (MSI-X or MSI) or %d (MSI)\n", msi, MSI_MSIX,
  2165. MSI_MSI);
  2166. err = -EINVAL;
  2167. goto err_out;
  2168. }
  2169. /*
  2170. * Print our driver banner the first time we're called to initialize a
  2171. * device.
  2172. */
  2173. if (version_printed == 0) {
  2174. printk(KERN_INFO "%s - version %s\n", DRV_DESC, DRV_VERSION);
  2175. version_printed = 1;
  2176. }
  2177. /*
  2178. * Initialize generic PCI device state.
  2179. */
  2180. err = pci_enable_device(pdev);
  2181. if (err) {
  2182. dev_err(&pdev->dev, "cannot enable PCI device\n");
  2183. return err;
  2184. }
  2185. /*
  2186. * Reserve PCI resources for the device. If we can't get them some
  2187. * other driver may have already claimed the device ...
  2188. */
  2189. err = pci_request_regions(pdev, KBUILD_MODNAME);
  2190. if (err) {
  2191. dev_err(&pdev->dev, "cannot obtain PCI resources\n");
  2192. goto err_disable_device;
  2193. }
  2194. /*
  2195. * Set up our DMA mask: try for 64-bit address masking first and
  2196. * fall back to 32-bit if we can't get 64 bits ...
  2197. */
  2198. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  2199. if (err == 0) {
  2200. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  2201. if (err) {
  2202. dev_err(&pdev->dev, "unable to obtain 64-bit DMA for"
  2203. " coherent allocations\n");
  2204. goto err_release_regions;
  2205. }
  2206. pci_using_dac = 1;
  2207. } else {
  2208. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  2209. if (err != 0) {
  2210. dev_err(&pdev->dev, "no usable DMA configuration\n");
  2211. goto err_release_regions;
  2212. }
  2213. pci_using_dac = 0;
  2214. }
  2215. /*
  2216. * Enable bus mastering for the device ...
  2217. */
  2218. pci_set_master(pdev);
  2219. /*
  2220. * Allocate our adapter data structure and attach it to the device.
  2221. */
  2222. adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
  2223. if (!adapter) {
  2224. err = -ENOMEM;
  2225. goto err_release_regions;
  2226. }
  2227. pci_set_drvdata(pdev, adapter);
  2228. adapter->pdev = pdev;
  2229. adapter->pdev_dev = &pdev->dev;
  2230. /*
  2231. * Initialize SMP data synchronization resources.
  2232. */
  2233. spin_lock_init(&adapter->stats_lock);
  2234. /*
  2235. * Map our I/O registers in BAR0.
  2236. */
  2237. adapter->regs = pci_ioremap_bar(pdev, 0);
  2238. if (!adapter->regs) {
  2239. dev_err(&pdev->dev, "cannot map device registers\n");
  2240. err = -ENOMEM;
  2241. goto err_free_adapter;
  2242. }
  2243. /*
  2244. * Initialize adapter level features.
  2245. */
  2246. adapter->name = pci_name(pdev);
  2247. adapter->msg_enable = dflt_msg_enable;
  2248. err = adap_init0(adapter);
  2249. if (err)
  2250. goto err_unmap_bar;
  2251. /*
  2252. * Allocate our "adapter ports" and stitch everything together.
  2253. */
  2254. pmask = adapter->params.vfres.pmask;
  2255. for_each_port(adapter, pidx) {
  2256. int port_id, viid;
  2257. /*
  2258. * We simplistically allocate our virtual interfaces
  2259. * sequentially across the port numbers to which we have
  2260. * access rights. This should be configurable in some manner
  2261. * ...
  2262. */
  2263. if (pmask == 0)
  2264. break;
  2265. port_id = ffs(pmask) - 1;
  2266. pmask &= ~(1 << port_id);
  2267. viid = t4vf_alloc_vi(adapter, port_id);
  2268. if (viid < 0) {
  2269. dev_err(&pdev->dev, "cannot allocate VI for port %d:"
  2270. " err=%d\n", port_id, viid);
  2271. err = viid;
  2272. goto err_free_dev;
  2273. }
  2274. /*
  2275. * Allocate our network device and stitch things together.
  2276. */
  2277. netdev = alloc_etherdev_mq(sizeof(struct port_info),
  2278. MAX_PORT_QSETS);
  2279. if (netdev == NULL) {
  2280. dev_err(&pdev->dev, "cannot allocate netdev for"
  2281. " port %d\n", port_id);
  2282. t4vf_free_vi(adapter, viid);
  2283. err = -ENOMEM;
  2284. goto err_free_dev;
  2285. }
  2286. adapter->port[pidx] = netdev;
  2287. SET_NETDEV_DEV(netdev, &pdev->dev);
  2288. pi = netdev_priv(netdev);
  2289. pi->adapter = adapter;
  2290. pi->pidx = pidx;
  2291. pi->port_id = port_id;
  2292. pi->viid = viid;
  2293. /*
  2294. * Initialize the starting state of our "port" and register
  2295. * it.
  2296. */
  2297. pi->xact_addr_filt = -1;
  2298. pi->rx_offload = RX_CSO;
  2299. netif_carrier_off(netdev);
  2300. netif_tx_stop_all_queues(netdev);
  2301. netdev->irq = pdev->irq;
  2302. netdev->features = (NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 |
  2303. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  2304. NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
  2305. NETIF_F_GRO);
  2306. if (pci_using_dac)
  2307. netdev->features |= NETIF_F_HIGHDMA;
  2308. netdev->vlan_features =
  2309. (netdev->features &
  2310. ~(NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX));
  2311. #ifdef HAVE_NET_DEVICE_OPS
  2312. netdev->netdev_ops = &cxgb4vf_netdev_ops;
  2313. #else
  2314. netdev->vlan_rx_register = cxgb4vf_vlan_rx_register;
  2315. netdev->open = cxgb4vf_open;
  2316. netdev->stop = cxgb4vf_stop;
  2317. netdev->hard_start_xmit = t4vf_eth_xmit;
  2318. netdev->get_stats = cxgb4vf_get_stats;
  2319. netdev->set_rx_mode = cxgb4vf_set_rxmode;
  2320. netdev->do_ioctl = cxgb4vf_do_ioctl;
  2321. netdev->change_mtu = cxgb4vf_change_mtu;
  2322. netdev->set_mac_address = cxgb4vf_set_mac_addr;
  2323. netdev->select_queue = cxgb4vf_select_queue;
  2324. #ifdef CONFIG_NET_POLL_CONTROLLER
  2325. netdev->poll_controller = cxgb4vf_poll_controller;
  2326. #endif
  2327. #endif
  2328. SET_ETHTOOL_OPS(netdev, &cxgb4vf_ethtool_ops);
  2329. /*
  2330. * Initialize the hardware/software state for the port.
  2331. */
  2332. err = t4vf_port_init(adapter, pidx);
  2333. if (err) {
  2334. dev_err(&pdev->dev, "cannot initialize port %d\n",
  2335. pidx);
  2336. goto err_free_dev;
  2337. }
  2338. }
  2339. /*
  2340. * The "card" is now ready to go. If any errors occur during device
  2341. * registration we do not fail the whole "card" but rather proceed
  2342. * only with the ports we manage to register successfully. However we
  2343. * must register at least one net device.
  2344. */
  2345. for_each_port(adapter, pidx) {
  2346. netdev = adapter->port[pidx];
  2347. if (netdev == NULL)
  2348. continue;
  2349. err = register_netdev(netdev);
  2350. if (err) {
  2351. dev_warn(&pdev->dev, "cannot register net device %s,"
  2352. " skipping\n", netdev->name);
  2353. continue;
  2354. }
  2355. set_bit(pidx, &adapter->registered_device_map);
  2356. }
  2357. if (adapter->registered_device_map == 0) {
  2358. dev_err(&pdev->dev, "could not register any net devices\n");
  2359. goto err_free_dev;
  2360. }
  2361. /*
  2362. * Set up our debugfs entries.
  2363. */
  2364. if (cxgb4vf_debugfs_root) {
  2365. adapter->debugfs_root =
  2366. debugfs_create_dir(pci_name(pdev),
  2367. cxgb4vf_debugfs_root);
  2368. if (adapter->debugfs_root == NULL)
  2369. dev_warn(&pdev->dev, "could not create debugfs"
  2370. " directory");
  2371. else
  2372. setup_debugfs(adapter);
  2373. }
  2374. /*
  2375. * See what interrupts we'll be using. If we've been configured to
  2376. * use MSI-X interrupts, try to enable them but fall back to using
  2377. * MSI interrupts if we can't enable MSI-X interrupts. If we can't
  2378. * get MSI interrupts we bail with the error.
  2379. */
  2380. if (msi == MSI_MSIX && enable_msix(adapter) == 0)
  2381. adapter->flags |= USING_MSIX;
  2382. else {
  2383. err = pci_enable_msi(pdev);
  2384. if (err) {
  2385. dev_err(&pdev->dev, "Unable to allocate %s interrupts;"
  2386. " err=%d\n",
  2387. msi == MSI_MSIX ? "MSI-X or MSI" : "MSI", err);
  2388. goto err_free_debugfs;
  2389. }
  2390. adapter->flags |= USING_MSI;
  2391. }
  2392. /*
  2393. * Now that we know how many "ports" we have and what their types are,
  2394. * and how many Queue Sets we can support, we can configure our queue
  2395. * resources.
  2396. */
  2397. cfg_queues(adapter);
  2398. /*
  2399. * Print a short notice on the existance and configuration of the new
  2400. * VF network device ...
  2401. */
  2402. for_each_port(adapter, pidx) {
  2403. dev_info(adapter->pdev_dev, "%s: Chelsio VF NIC PCIe %s\n",
  2404. adapter->port[pidx]->name,
  2405. (adapter->flags & USING_MSIX) ? "MSI-X" :
  2406. (adapter->flags & USING_MSI) ? "MSI" : "");
  2407. }
  2408. /*
  2409. * Return success!
  2410. */
  2411. return 0;
  2412. /*
  2413. * Error recovery and exit code. Unwind state that's been created
  2414. * so far and return the error.
  2415. */
  2416. err_free_debugfs:
  2417. if (adapter->debugfs_root) {
  2418. cleanup_debugfs(adapter);
  2419. debugfs_remove_recursive(adapter->debugfs_root);
  2420. }
  2421. err_free_dev:
  2422. for_each_port(adapter, pidx) {
  2423. netdev = adapter->port[pidx];
  2424. if (netdev == NULL)
  2425. continue;
  2426. pi = netdev_priv(netdev);
  2427. t4vf_free_vi(adapter, pi->viid);
  2428. if (test_bit(pidx, &adapter->registered_device_map))
  2429. unregister_netdev(netdev);
  2430. free_netdev(netdev);
  2431. }
  2432. err_unmap_bar:
  2433. iounmap(adapter->regs);
  2434. err_free_adapter:
  2435. kfree(adapter);
  2436. pci_set_drvdata(pdev, NULL);
  2437. err_release_regions:
  2438. pci_release_regions(pdev);
  2439. pci_set_drvdata(pdev, NULL);
  2440. pci_clear_master(pdev);
  2441. err_disable_device:
  2442. pci_disable_device(pdev);
  2443. err_out:
  2444. return err;
  2445. }
  2446. /*
  2447. * "Remove" a device: tear down all kernel and driver state created in the
  2448. * "probe" routine and quiesce the device (disable interrupts, etc.). (Note
  2449. * that this is called "remove_one" in the PF Driver.)
  2450. */
  2451. static void __devexit cxgb4vf_pci_remove(struct pci_dev *pdev)
  2452. {
  2453. struct adapter *adapter = pci_get_drvdata(pdev);
  2454. /*
  2455. * Tear down driver state associated with device.
  2456. */
  2457. if (adapter) {
  2458. int pidx;
  2459. /*
  2460. * Stop all of our activity. Unregister network port,
  2461. * disable interrupts, etc.
  2462. */
  2463. for_each_port(adapter, pidx)
  2464. if (test_bit(pidx, &adapter->registered_device_map))
  2465. unregister_netdev(adapter->port[pidx]);
  2466. t4vf_sge_stop(adapter);
  2467. if (adapter->flags & USING_MSIX) {
  2468. pci_disable_msix(adapter->pdev);
  2469. adapter->flags &= ~USING_MSIX;
  2470. } else if (adapter->flags & USING_MSI) {
  2471. pci_disable_msi(adapter->pdev);
  2472. adapter->flags &= ~USING_MSI;
  2473. }
  2474. /*
  2475. * Tear down our debugfs entries.
  2476. */
  2477. if (adapter->debugfs_root) {
  2478. cleanup_debugfs(adapter);
  2479. debugfs_remove_recursive(adapter->debugfs_root);
  2480. }
  2481. /*
  2482. * Free all of the various resources which we've acquired ...
  2483. */
  2484. t4vf_free_sge_resources(adapter);
  2485. for_each_port(adapter, pidx) {
  2486. struct net_device *netdev = adapter->port[pidx];
  2487. struct port_info *pi;
  2488. if (netdev == NULL)
  2489. continue;
  2490. pi = netdev_priv(netdev);
  2491. t4vf_free_vi(adapter, pi->viid);
  2492. free_netdev(netdev);
  2493. }
  2494. iounmap(adapter->regs);
  2495. kfree(adapter);
  2496. pci_set_drvdata(pdev, NULL);
  2497. }
  2498. /*
  2499. * Disable the device and release its PCI resources.
  2500. */
  2501. pci_disable_device(pdev);
  2502. pci_clear_master(pdev);
  2503. pci_release_regions(pdev);
  2504. }
  2505. /*
  2506. * PCI Device registration data structures.
  2507. */
  2508. #define CH_DEVICE(devid, idx) \
  2509. { PCI_VENDOR_ID_CHELSIO, devid, PCI_ANY_ID, PCI_ANY_ID, 0, 0, idx }
  2510. static struct pci_device_id cxgb4vf_pci_tbl[] = {
  2511. CH_DEVICE(0xb000, 0), /* PE10K FPGA */
  2512. CH_DEVICE(0x4800, 0), /* T440-dbg */
  2513. CH_DEVICE(0x4801, 0), /* T420-cr */
  2514. CH_DEVICE(0x4802, 0), /* T422-cr */
  2515. { 0, }
  2516. };
  2517. MODULE_DESCRIPTION(DRV_DESC);
  2518. MODULE_AUTHOR("Chelsio Communications");
  2519. MODULE_LICENSE("Dual BSD/GPL");
  2520. MODULE_VERSION(DRV_VERSION);
  2521. MODULE_DEVICE_TABLE(pci, cxgb4vf_pci_tbl);
  2522. static struct pci_driver cxgb4vf_driver = {
  2523. .name = KBUILD_MODNAME,
  2524. .id_table = cxgb4vf_pci_tbl,
  2525. .probe = cxgb4vf_pci_probe,
  2526. .remove = __devexit_p(cxgb4vf_pci_remove),
  2527. };
  2528. /*
  2529. * Initialize global driver state.
  2530. */
  2531. static int __init cxgb4vf_module_init(void)
  2532. {
  2533. int ret;
  2534. /* Debugfs support is optional, just warn if this fails */
  2535. cxgb4vf_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
  2536. if (!cxgb4vf_debugfs_root)
  2537. printk(KERN_WARNING KBUILD_MODNAME ": could not create"
  2538. " debugfs entry, continuing\n");
  2539. ret = pci_register_driver(&cxgb4vf_driver);
  2540. if (ret < 0)
  2541. debugfs_remove(cxgb4vf_debugfs_root);
  2542. return ret;
  2543. }
  2544. /*
  2545. * Tear down global driver state.
  2546. */
  2547. static void __exit cxgb4vf_module_exit(void)
  2548. {
  2549. pci_unregister_driver(&cxgb4vf_driver);
  2550. debugfs_remove(cxgb4vf_debugfs_root);
  2551. }
  2552. module_init(cxgb4vf_module_init);
  2553. module_exit(cxgb4vf_module_exit);