t4vf_hw.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247
  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/pci.h>
  36. #include "t4vf_common.h"
  37. #include "t4vf_defs.h"
  38. #include "../cxgb4/t4_regs.h"
  39. #include "../cxgb4/t4_values.h"
  40. #include "../cxgb4/t4fw_api.h"
  41. /*
  42. * Wait for the device to become ready (signified by our "who am I" register
  43. * returning a value other than all 1's). Return an error if it doesn't
  44. * become ready ...
  45. */
  46. int t4vf_wait_dev_ready(struct adapter *adapter)
  47. {
  48. const u32 whoami = T4VF_PL_BASE_ADDR + PL_VF_WHOAMI;
  49. const u32 notready1 = 0xffffffff;
  50. const u32 notready2 = 0xeeeeeeee;
  51. u32 val;
  52. val = t4_read_reg(adapter, whoami);
  53. if (val != notready1 && val != notready2)
  54. return 0;
  55. msleep(500);
  56. val = t4_read_reg(adapter, whoami);
  57. if (val != notready1 && val != notready2)
  58. return 0;
  59. else
  60. return -EIO;
  61. }
  62. /*
  63. * Get the reply to a mailbox command and store it in @rpl in big-endian order
  64. * (since the firmware data structures are specified in a big-endian layout).
  65. */
  66. static void get_mbox_rpl(struct adapter *adapter, __be64 *rpl, int size,
  67. u32 mbox_data)
  68. {
  69. for ( ; size; size -= 8, mbox_data += 8)
  70. *rpl++ = cpu_to_be64(t4_read_reg64(adapter, mbox_data));
  71. }
  72. /**
  73. * t4vf_record_mbox - record a Firmware Mailbox Command/Reply in the log
  74. * @adapter: the adapter
  75. * @cmd: the Firmware Mailbox Command or Reply
  76. * @size: command length in bytes
  77. * @access: the time (ms) needed to access the Firmware Mailbox
  78. * @execute: the time (ms) the command spent being executed
  79. */
  80. static void t4vf_record_mbox(struct adapter *adapter, const __be64 *cmd,
  81. int size, int access, int execute)
  82. {
  83. struct mbox_cmd_log *log = adapter->mbox_log;
  84. struct mbox_cmd *entry;
  85. int i;
  86. entry = mbox_cmd_log_entry(log, log->cursor++);
  87. if (log->cursor == log->size)
  88. log->cursor = 0;
  89. for (i = 0; i < size / 8; i++)
  90. entry->cmd[i] = be64_to_cpu(cmd[i]);
  91. while (i < MBOX_LEN / 8)
  92. entry->cmd[i++] = 0;
  93. entry->timestamp = jiffies;
  94. entry->seqno = log->seqno++;
  95. entry->access = access;
  96. entry->execute = execute;
  97. }
  98. /**
  99. * t4vf_wr_mbox_core - send a command to FW through the mailbox
  100. * @adapter: the adapter
  101. * @cmd: the command to write
  102. * @size: command length in bytes
  103. * @rpl: where to optionally store the reply
  104. * @sleep_ok: if true we may sleep while awaiting command completion
  105. *
  106. * Sends the given command to FW through the mailbox and waits for the
  107. * FW to execute the command. If @rpl is not %NULL it is used to store
  108. * the FW's reply to the command. The command and its optional reply
  109. * are of the same length. FW can take up to 500 ms to respond.
  110. * @sleep_ok determines whether we may sleep while awaiting the response.
  111. * If sleeping is allowed we use progressive backoff otherwise we spin.
  112. *
  113. * The return value is 0 on success or a negative errno on failure. A
  114. * failure can happen either because we are not able to execute the
  115. * command or FW executes it but signals an error. In the latter case
  116. * the return value is the error code indicated by FW (negated).
  117. */
  118. int t4vf_wr_mbox_core(struct adapter *adapter, const void *cmd, int size,
  119. void *rpl, bool sleep_ok)
  120. {
  121. static const int delay[] = {
  122. 1, 1, 3, 5, 10, 10, 20, 50, 100
  123. };
  124. u16 access = 0, execute = 0;
  125. u32 v, mbox_data;
  126. int i, ms, delay_idx, ret;
  127. const __be64 *p;
  128. u32 mbox_ctl = T4VF_CIM_BASE_ADDR + CIM_VF_EXT_MAILBOX_CTRL;
  129. u32 cmd_op = FW_CMD_OP_G(be32_to_cpu(((struct fw_cmd_hdr *)cmd)->hi));
  130. __be64 cmd_rpl[MBOX_LEN / 8];
  131. struct mbox_list entry;
  132. /* In T6, mailbox size is changed to 128 bytes to avoid
  133. * invalidating the entire prefetch buffer.
  134. */
  135. if (CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5)
  136. mbox_data = T4VF_MBDATA_BASE_ADDR;
  137. else
  138. mbox_data = T6VF_MBDATA_BASE_ADDR;
  139. /*
  140. * Commands must be multiples of 16 bytes in length and may not be
  141. * larger than the size of the Mailbox Data register array.
  142. */
  143. if ((size % 16) != 0 ||
  144. size > NUM_CIM_VF_MAILBOX_DATA_INSTANCES * 4)
  145. return -EINVAL;
  146. /* Queue ourselves onto the mailbox access list. When our entry is at
  147. * the front of the list, we have rights to access the mailbox. So we
  148. * wait [for a while] till we're at the front [or bail out with an
  149. * EBUSY] ...
  150. */
  151. spin_lock(&adapter->mbox_lock);
  152. list_add_tail(&entry.list, &adapter->mlist.list);
  153. spin_unlock(&adapter->mbox_lock);
  154. delay_idx = 0;
  155. ms = delay[0];
  156. for (i = 0; ; i += ms) {
  157. /* If we've waited too long, return a busy indication. This
  158. * really ought to be based on our initial position in the
  159. * mailbox access list but this is a start. We very rearely
  160. * contend on access to the mailbox ...
  161. */
  162. if (i > FW_CMD_MAX_TIMEOUT) {
  163. spin_lock(&adapter->mbox_lock);
  164. list_del(&entry.list);
  165. spin_unlock(&adapter->mbox_lock);
  166. ret = -EBUSY;
  167. t4vf_record_mbox(adapter, cmd, size, access, ret);
  168. return ret;
  169. }
  170. /* If we're at the head, break out and start the mailbox
  171. * protocol.
  172. */
  173. if (list_first_entry(&adapter->mlist.list, struct mbox_list,
  174. list) == &entry)
  175. break;
  176. /* Delay for a bit before checking again ... */
  177. if (sleep_ok) {
  178. ms = delay[delay_idx]; /* last element may repeat */
  179. if (delay_idx < ARRAY_SIZE(delay) - 1)
  180. delay_idx++;
  181. msleep(ms);
  182. } else {
  183. mdelay(ms);
  184. }
  185. }
  186. /*
  187. * Loop trying to get ownership of the mailbox. Return an error
  188. * if we can't gain ownership.
  189. */
  190. v = MBOWNER_G(t4_read_reg(adapter, mbox_ctl));
  191. for (i = 0; v == MBOX_OWNER_NONE && i < 3; i++)
  192. v = MBOWNER_G(t4_read_reg(adapter, mbox_ctl));
  193. if (v != MBOX_OWNER_DRV) {
  194. spin_lock(&adapter->mbox_lock);
  195. list_del(&entry.list);
  196. spin_unlock(&adapter->mbox_lock);
  197. ret = (v == MBOX_OWNER_FW) ? -EBUSY : -ETIMEDOUT;
  198. t4vf_record_mbox(adapter, cmd, size, access, ret);
  199. return ret;
  200. }
  201. /*
  202. * Write the command array into the Mailbox Data register array and
  203. * transfer ownership of the mailbox to the firmware.
  204. *
  205. * For the VFs, the Mailbox Data "registers" are actually backed by
  206. * T4's "MA" interface rather than PL Registers (as is the case for
  207. * the PFs). Because these are in different coherency domains, the
  208. * write to the VF's PL-register-backed Mailbox Control can race in
  209. * front of the writes to the MA-backed VF Mailbox Data "registers".
  210. * So we need to do a read-back on at least one byte of the VF Mailbox
  211. * Data registers before doing the write to the VF Mailbox Control
  212. * register.
  213. */
  214. if (cmd_op != FW_VI_STATS_CMD)
  215. t4vf_record_mbox(adapter, cmd, size, access, 0);
  216. for (i = 0, p = cmd; i < size; i += 8)
  217. t4_write_reg64(adapter, mbox_data + i, be64_to_cpu(*p++));
  218. t4_read_reg(adapter, mbox_data); /* flush write */
  219. t4_write_reg(adapter, mbox_ctl,
  220. MBMSGVALID_F | MBOWNER_V(MBOX_OWNER_FW));
  221. t4_read_reg(adapter, mbox_ctl); /* flush write */
  222. /*
  223. * Spin waiting for firmware to acknowledge processing our command.
  224. */
  225. delay_idx = 0;
  226. ms = delay[0];
  227. for (i = 0; i < FW_CMD_MAX_TIMEOUT; i += ms) {
  228. if (sleep_ok) {
  229. ms = delay[delay_idx];
  230. if (delay_idx < ARRAY_SIZE(delay) - 1)
  231. delay_idx++;
  232. msleep(ms);
  233. } else
  234. mdelay(ms);
  235. /*
  236. * If we're the owner, see if this is the reply we wanted.
  237. */
  238. v = t4_read_reg(adapter, mbox_ctl);
  239. if (MBOWNER_G(v) == MBOX_OWNER_DRV) {
  240. /*
  241. * If the Message Valid bit isn't on, revoke ownership
  242. * of the mailbox and continue waiting for our reply.
  243. */
  244. if ((v & MBMSGVALID_F) == 0) {
  245. t4_write_reg(adapter, mbox_ctl,
  246. MBOWNER_V(MBOX_OWNER_NONE));
  247. continue;
  248. }
  249. /*
  250. * We now have our reply. Extract the command return
  251. * value, copy the reply back to our caller's buffer
  252. * (if specified) and revoke ownership of the mailbox.
  253. * We return the (negated) firmware command return
  254. * code (this depends on FW_SUCCESS == 0).
  255. */
  256. get_mbox_rpl(adapter, cmd_rpl, size, mbox_data);
  257. /* return value in low-order little-endian word */
  258. v = be64_to_cpu(cmd_rpl[0]);
  259. if (rpl) {
  260. /* request bit in high-order BE word */
  261. WARN_ON((be32_to_cpu(*(const __be32 *)cmd)
  262. & FW_CMD_REQUEST_F) == 0);
  263. memcpy(rpl, cmd_rpl, size);
  264. WARN_ON((be32_to_cpu(*(__be32 *)rpl)
  265. & FW_CMD_REQUEST_F) != 0);
  266. }
  267. t4_write_reg(adapter, mbox_ctl,
  268. MBOWNER_V(MBOX_OWNER_NONE));
  269. execute = i + ms;
  270. if (cmd_op != FW_VI_STATS_CMD)
  271. t4vf_record_mbox(adapter, cmd_rpl, size, access,
  272. execute);
  273. spin_lock(&adapter->mbox_lock);
  274. list_del(&entry.list);
  275. spin_unlock(&adapter->mbox_lock);
  276. return -FW_CMD_RETVAL_G(v);
  277. }
  278. }
  279. /* We timed out. Return the error ... */
  280. ret = -ETIMEDOUT;
  281. t4vf_record_mbox(adapter, cmd, size, access, ret);
  282. spin_lock(&adapter->mbox_lock);
  283. list_del(&entry.list);
  284. spin_unlock(&adapter->mbox_lock);
  285. return ret;
  286. }
  287. #define ADVERT_MASK (FW_PORT_CAP32_SPEED_V(FW_PORT_CAP32_SPEED_M) | \
  288. FW_PORT_CAP32_ANEG)
  289. /**
  290. * fwcaps16_to_caps32 - convert 16-bit Port Capabilities to 32-bits
  291. * @caps16: a 16-bit Port Capabilities value
  292. *
  293. * Returns the equivalent 32-bit Port Capabilities value.
  294. */
  295. static fw_port_cap32_t fwcaps16_to_caps32(fw_port_cap16_t caps16)
  296. {
  297. fw_port_cap32_t caps32 = 0;
  298. #define CAP16_TO_CAP32(__cap) \
  299. do { \
  300. if (caps16 & FW_PORT_CAP_##__cap) \
  301. caps32 |= FW_PORT_CAP32_##__cap; \
  302. } while (0)
  303. CAP16_TO_CAP32(SPEED_100M);
  304. CAP16_TO_CAP32(SPEED_1G);
  305. CAP16_TO_CAP32(SPEED_25G);
  306. CAP16_TO_CAP32(SPEED_10G);
  307. CAP16_TO_CAP32(SPEED_40G);
  308. CAP16_TO_CAP32(SPEED_100G);
  309. CAP16_TO_CAP32(FC_RX);
  310. CAP16_TO_CAP32(FC_TX);
  311. CAP16_TO_CAP32(ANEG);
  312. CAP16_TO_CAP32(MDIAUTO);
  313. CAP16_TO_CAP32(MDISTRAIGHT);
  314. CAP16_TO_CAP32(FEC_RS);
  315. CAP16_TO_CAP32(FEC_BASER_RS);
  316. CAP16_TO_CAP32(802_3_PAUSE);
  317. CAP16_TO_CAP32(802_3_ASM_DIR);
  318. #undef CAP16_TO_CAP32
  319. return caps32;
  320. }
  321. /* Translate Firmware Pause specification to Common Code */
  322. static inline enum cc_pause fwcap_to_cc_pause(fw_port_cap32_t fw_pause)
  323. {
  324. enum cc_pause cc_pause = 0;
  325. if (fw_pause & FW_PORT_CAP32_FC_RX)
  326. cc_pause |= PAUSE_RX;
  327. if (fw_pause & FW_PORT_CAP32_FC_TX)
  328. cc_pause |= PAUSE_TX;
  329. return cc_pause;
  330. }
  331. /* Translate Firmware Forward Error Correction specification to Common Code */
  332. static inline enum cc_fec fwcap_to_cc_fec(fw_port_cap32_t fw_fec)
  333. {
  334. enum cc_fec cc_fec = 0;
  335. if (fw_fec & FW_PORT_CAP32_FEC_RS)
  336. cc_fec |= FEC_RS;
  337. if (fw_fec & FW_PORT_CAP32_FEC_BASER_RS)
  338. cc_fec |= FEC_BASER_RS;
  339. return cc_fec;
  340. }
  341. /**
  342. * Return the highest speed set in the port capabilities, in Mb/s.
  343. */
  344. static unsigned int fwcap_to_speed(fw_port_cap32_t caps)
  345. {
  346. #define TEST_SPEED_RETURN(__caps_speed, __speed) \
  347. do { \
  348. if (caps & FW_PORT_CAP32_SPEED_##__caps_speed) \
  349. return __speed; \
  350. } while (0)
  351. TEST_SPEED_RETURN(400G, 400000);
  352. TEST_SPEED_RETURN(200G, 200000);
  353. TEST_SPEED_RETURN(100G, 100000);
  354. TEST_SPEED_RETURN(50G, 50000);
  355. TEST_SPEED_RETURN(40G, 40000);
  356. TEST_SPEED_RETURN(25G, 25000);
  357. TEST_SPEED_RETURN(10G, 10000);
  358. TEST_SPEED_RETURN(1G, 1000);
  359. TEST_SPEED_RETURN(100M, 100);
  360. #undef TEST_SPEED_RETURN
  361. return 0;
  362. }
  363. /**
  364. * fwcap_to_fwspeed - return highest speed in Port Capabilities
  365. * @acaps: advertised Port Capabilities
  366. *
  367. * Get the highest speed for the port from the advertised Port
  368. * Capabilities. It will be either the highest speed from the list of
  369. * speeds or whatever user has set using ethtool.
  370. */
  371. static fw_port_cap32_t fwcap_to_fwspeed(fw_port_cap32_t acaps)
  372. {
  373. #define TEST_SPEED_RETURN(__caps_speed) \
  374. do { \
  375. if (acaps & FW_PORT_CAP32_SPEED_##__caps_speed) \
  376. return FW_PORT_CAP32_SPEED_##__caps_speed; \
  377. } while (0)
  378. TEST_SPEED_RETURN(400G);
  379. TEST_SPEED_RETURN(200G);
  380. TEST_SPEED_RETURN(100G);
  381. TEST_SPEED_RETURN(50G);
  382. TEST_SPEED_RETURN(40G);
  383. TEST_SPEED_RETURN(25G);
  384. TEST_SPEED_RETURN(10G);
  385. TEST_SPEED_RETURN(1G);
  386. TEST_SPEED_RETURN(100M);
  387. #undef TEST_SPEED_RETURN
  388. return 0;
  389. }
  390. /*
  391. * init_link_config - initialize a link's SW state
  392. * @lc: structure holding the link state
  393. * @pcaps: link Port Capabilities
  394. * @acaps: link current Advertised Port Capabilities
  395. *
  396. * Initializes the SW state maintained for each link, including the link's
  397. * capabilities and default speed/flow-control/autonegotiation settings.
  398. */
  399. static void init_link_config(struct link_config *lc,
  400. fw_port_cap32_t pcaps,
  401. fw_port_cap32_t acaps)
  402. {
  403. lc->pcaps = pcaps;
  404. lc->lpacaps = 0;
  405. lc->speed_caps = 0;
  406. lc->speed = 0;
  407. lc->requested_fc = lc->fc = PAUSE_RX | PAUSE_TX;
  408. /* For Forward Error Control, we default to whatever the Firmware
  409. * tells us the Link is currently advertising.
  410. */
  411. lc->auto_fec = fwcap_to_cc_fec(acaps);
  412. lc->requested_fec = FEC_AUTO;
  413. lc->fec = lc->auto_fec;
  414. /* If the Port is capable of Auto-Negtotiation, initialize it as
  415. * "enabled" and copy over all of the Physical Port Capabilities
  416. * to the Advertised Port Capabilities. Otherwise mark it as
  417. * Auto-Negotiate disabled and select the highest supported speed
  418. * for the link. Note parallel structure in t4_link_l1cfg_core()
  419. * and t4_handle_get_port_info().
  420. */
  421. if (lc->pcaps & FW_PORT_CAP32_ANEG) {
  422. lc->acaps = acaps & ADVERT_MASK;
  423. lc->autoneg = AUTONEG_ENABLE;
  424. lc->requested_fc |= PAUSE_AUTONEG;
  425. } else {
  426. lc->acaps = 0;
  427. lc->autoneg = AUTONEG_DISABLE;
  428. lc->speed_caps = fwcap_to_fwspeed(acaps);
  429. }
  430. }
  431. /**
  432. * t4vf_port_init - initialize port hardware/software state
  433. * @adapter: the adapter
  434. * @pidx: the adapter port index
  435. */
  436. int t4vf_port_init(struct adapter *adapter, int pidx)
  437. {
  438. struct port_info *pi = adap2pinfo(adapter, pidx);
  439. unsigned int fw_caps = adapter->params.fw_caps_support;
  440. struct fw_vi_cmd vi_cmd, vi_rpl;
  441. struct fw_port_cmd port_cmd, port_rpl;
  442. enum fw_port_type port_type;
  443. int mdio_addr;
  444. fw_port_cap32_t pcaps, acaps;
  445. int ret;
  446. /* If we haven't yet determined whether we're talking to Firmware
  447. * which knows the new 32-bit Port Capabilities, it's time to find
  448. * out now. This will also tell new Firmware to send us Port Status
  449. * Updates using the new 32-bit Port Capabilities version of the
  450. * Port Information message.
  451. */
  452. if (fw_caps == FW_CAPS_UNKNOWN) {
  453. u32 param, val;
  454. param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) |
  455. FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_PORT_CAPS32));
  456. val = 1;
  457. ret = t4vf_set_params(adapter, 1, &param, &val);
  458. fw_caps = (ret == 0 ? FW_CAPS32 : FW_CAPS16);
  459. adapter->params.fw_caps_support = fw_caps;
  460. }
  461. /*
  462. * Execute a VI Read command to get our Virtual Interface information
  463. * like MAC address, etc.
  464. */
  465. memset(&vi_cmd, 0, sizeof(vi_cmd));
  466. vi_cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_VI_CMD) |
  467. FW_CMD_REQUEST_F |
  468. FW_CMD_READ_F);
  469. vi_cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(vi_cmd));
  470. vi_cmd.type_viid = cpu_to_be16(FW_VI_CMD_VIID_V(pi->viid));
  471. ret = t4vf_wr_mbox(adapter, &vi_cmd, sizeof(vi_cmd), &vi_rpl);
  472. if (ret != FW_SUCCESS)
  473. return ret;
  474. BUG_ON(pi->port_id != FW_VI_CMD_PORTID_G(vi_rpl.portid_pkd));
  475. pi->rss_size = FW_VI_CMD_RSSSIZE_G(be16_to_cpu(vi_rpl.rsssize_pkd));
  476. t4_os_set_hw_addr(adapter, pidx, vi_rpl.mac);
  477. /*
  478. * If we don't have read access to our port information, we're done
  479. * now. Otherwise, execute a PORT Read command to get it ...
  480. */
  481. if (!(adapter->params.vfres.r_caps & FW_CMD_CAP_PORT))
  482. return 0;
  483. memset(&port_cmd, 0, sizeof(port_cmd));
  484. port_cmd.op_to_portid = cpu_to_be32(FW_CMD_OP_V(FW_PORT_CMD) |
  485. FW_CMD_REQUEST_F |
  486. FW_CMD_READ_F |
  487. FW_PORT_CMD_PORTID_V(pi->port_id));
  488. port_cmd.action_to_len16 = cpu_to_be32(
  489. FW_PORT_CMD_ACTION_V(fw_caps == FW_CAPS16
  490. ? FW_PORT_ACTION_GET_PORT_INFO
  491. : FW_PORT_ACTION_GET_PORT_INFO32) |
  492. FW_LEN16(port_cmd));
  493. ret = t4vf_wr_mbox(adapter, &port_cmd, sizeof(port_cmd), &port_rpl);
  494. if (ret != FW_SUCCESS)
  495. return ret;
  496. /* Extract the various fields from the Port Information message. */
  497. if (fw_caps == FW_CAPS16) {
  498. u32 lstatus = be32_to_cpu(port_rpl.u.info.lstatus_to_modtype);
  499. port_type = FW_PORT_CMD_PTYPE_G(lstatus);
  500. mdio_addr = ((lstatus & FW_PORT_CMD_MDIOCAP_F)
  501. ? FW_PORT_CMD_MDIOADDR_G(lstatus)
  502. : -1);
  503. pcaps = fwcaps16_to_caps32(be16_to_cpu(port_rpl.u.info.pcap));
  504. acaps = fwcaps16_to_caps32(be16_to_cpu(port_rpl.u.info.acap));
  505. } else {
  506. u32 lstatus32 =
  507. be32_to_cpu(port_rpl.u.info32.lstatus32_to_cbllen32);
  508. port_type = FW_PORT_CMD_PORTTYPE32_G(lstatus32);
  509. mdio_addr = ((lstatus32 & FW_PORT_CMD_MDIOCAP32_F)
  510. ? FW_PORT_CMD_MDIOADDR32_G(lstatus32)
  511. : -1);
  512. pcaps = be32_to_cpu(port_rpl.u.info32.pcaps32);
  513. acaps = be32_to_cpu(port_rpl.u.info32.acaps32);
  514. }
  515. pi->port_type = port_type;
  516. pi->mdio_addr = mdio_addr;
  517. pi->mod_type = FW_PORT_MOD_TYPE_NA;
  518. init_link_config(&pi->link_cfg, pcaps, acaps);
  519. return 0;
  520. }
  521. /**
  522. * t4vf_fw_reset - issue a reset to FW
  523. * @adapter: the adapter
  524. *
  525. * Issues a reset command to FW. For a Physical Function this would
  526. * result in the Firmware resetting all of its state. For a Virtual
  527. * Function this just resets the state associated with the VF.
  528. */
  529. int t4vf_fw_reset(struct adapter *adapter)
  530. {
  531. struct fw_reset_cmd cmd;
  532. memset(&cmd, 0, sizeof(cmd));
  533. cmd.op_to_write = cpu_to_be32(FW_CMD_OP_V(FW_RESET_CMD) |
  534. FW_CMD_WRITE_F);
  535. cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
  536. return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
  537. }
  538. /**
  539. * t4vf_query_params - query FW or device parameters
  540. * @adapter: the adapter
  541. * @nparams: the number of parameters
  542. * @params: the parameter names
  543. * @vals: the parameter values
  544. *
  545. * Reads the values of firmware or device parameters. Up to 7 parameters
  546. * can be queried at once.
  547. */
  548. static int t4vf_query_params(struct adapter *adapter, unsigned int nparams,
  549. const u32 *params, u32 *vals)
  550. {
  551. int i, ret;
  552. struct fw_params_cmd cmd, rpl;
  553. struct fw_params_param *p;
  554. size_t len16;
  555. if (nparams > 7)
  556. return -EINVAL;
  557. memset(&cmd, 0, sizeof(cmd));
  558. cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_PARAMS_CMD) |
  559. FW_CMD_REQUEST_F |
  560. FW_CMD_READ_F);
  561. len16 = DIV_ROUND_UP(offsetof(struct fw_params_cmd,
  562. param[nparams].mnem), 16);
  563. cmd.retval_len16 = cpu_to_be32(FW_CMD_LEN16_V(len16));
  564. for (i = 0, p = &cmd.param[0]; i < nparams; i++, p++)
  565. p->mnem = htonl(*params++);
  566. ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
  567. if (ret == 0)
  568. for (i = 0, p = &rpl.param[0]; i < nparams; i++, p++)
  569. *vals++ = be32_to_cpu(p->val);
  570. return ret;
  571. }
  572. /**
  573. * t4vf_set_params - sets FW or device parameters
  574. * @adapter: the adapter
  575. * @nparams: the number of parameters
  576. * @params: the parameter names
  577. * @vals: the parameter values
  578. *
  579. * Sets the values of firmware or device parameters. Up to 7 parameters
  580. * can be specified at once.
  581. */
  582. int t4vf_set_params(struct adapter *adapter, unsigned int nparams,
  583. const u32 *params, const u32 *vals)
  584. {
  585. int i;
  586. struct fw_params_cmd cmd;
  587. struct fw_params_param *p;
  588. size_t len16;
  589. if (nparams > 7)
  590. return -EINVAL;
  591. memset(&cmd, 0, sizeof(cmd));
  592. cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_PARAMS_CMD) |
  593. FW_CMD_REQUEST_F |
  594. FW_CMD_WRITE_F);
  595. len16 = DIV_ROUND_UP(offsetof(struct fw_params_cmd,
  596. param[nparams]), 16);
  597. cmd.retval_len16 = cpu_to_be32(FW_CMD_LEN16_V(len16));
  598. for (i = 0, p = &cmd.param[0]; i < nparams; i++, p++) {
  599. p->mnem = cpu_to_be32(*params++);
  600. p->val = cpu_to_be32(*vals++);
  601. }
  602. return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
  603. }
  604. /**
  605. * t4vf_fl_pkt_align - return the fl packet alignment
  606. * @adapter: the adapter
  607. *
  608. * T4 has a single field to specify the packing and padding boundary.
  609. * T5 onwards has separate fields for this and hence the alignment for
  610. * next packet offset is maximum of these two. And T6 changes the
  611. * Ingress Padding Boundary Shift, so it's all a mess and it's best
  612. * if we put this in low-level Common Code ...
  613. *
  614. */
  615. int t4vf_fl_pkt_align(struct adapter *adapter)
  616. {
  617. u32 sge_control, sge_control2;
  618. unsigned int ingpadboundary, ingpackboundary, fl_align, ingpad_shift;
  619. sge_control = adapter->params.sge.sge_control;
  620. /* T4 uses a single control field to specify both the PCIe Padding and
  621. * Packing Boundary. T5 introduced the ability to specify these
  622. * separately. The actual Ingress Packet Data alignment boundary
  623. * within Packed Buffer Mode is the maximum of these two
  624. * specifications. (Note that it makes no real practical sense to
  625. * have the Pading Boudary be larger than the Packing Boundary but you
  626. * could set the chip up that way and, in fact, legacy T4 code would
  627. * end doing this because it would initialize the Padding Boundary and
  628. * leave the Packing Boundary initialized to 0 (16 bytes).)
  629. * Padding Boundary values in T6 starts from 8B,
  630. * where as it is 32B for T4 and T5.
  631. */
  632. if (CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5)
  633. ingpad_shift = INGPADBOUNDARY_SHIFT_X;
  634. else
  635. ingpad_shift = T6_INGPADBOUNDARY_SHIFT_X;
  636. ingpadboundary = 1 << (INGPADBOUNDARY_G(sge_control) + ingpad_shift);
  637. fl_align = ingpadboundary;
  638. if (!is_t4(adapter->params.chip)) {
  639. /* T5 has a different interpretation of one of the PCIe Packing
  640. * Boundary values.
  641. */
  642. sge_control2 = adapter->params.sge.sge_control2;
  643. ingpackboundary = INGPACKBOUNDARY_G(sge_control2);
  644. if (ingpackboundary == INGPACKBOUNDARY_16B_X)
  645. ingpackboundary = 16;
  646. else
  647. ingpackboundary = 1 << (ingpackboundary +
  648. INGPACKBOUNDARY_SHIFT_X);
  649. fl_align = max(ingpadboundary, ingpackboundary);
  650. }
  651. return fl_align;
  652. }
  653. /**
  654. * t4vf_bar2_sge_qregs - return BAR2 SGE Queue register information
  655. * @adapter: the adapter
  656. * @qid: the Queue ID
  657. * @qtype: the Ingress or Egress type for @qid
  658. * @pbar2_qoffset: BAR2 Queue Offset
  659. * @pbar2_qid: BAR2 Queue ID or 0 for Queue ID inferred SGE Queues
  660. *
  661. * Returns the BAR2 SGE Queue Registers information associated with the
  662. * indicated Absolute Queue ID. These are passed back in return value
  663. * pointers. @qtype should be T4_BAR2_QTYPE_EGRESS for Egress Queue
  664. * and T4_BAR2_QTYPE_INGRESS for Ingress Queues.
  665. *
  666. * This may return an error which indicates that BAR2 SGE Queue
  667. * registers aren't available. If an error is not returned, then the
  668. * following values are returned:
  669. *
  670. * *@pbar2_qoffset: the BAR2 Offset of the @qid Registers
  671. * *@pbar2_qid: the BAR2 SGE Queue ID or 0 of @qid
  672. *
  673. * If the returned BAR2 Queue ID is 0, then BAR2 SGE registers which
  674. * require the "Inferred Queue ID" ability may be used. E.g. the
  675. * Write Combining Doorbell Buffer. If the BAR2 Queue ID is not 0,
  676. * then these "Inferred Queue ID" register may not be used.
  677. */
  678. int t4vf_bar2_sge_qregs(struct adapter *adapter,
  679. unsigned int qid,
  680. enum t4_bar2_qtype qtype,
  681. u64 *pbar2_qoffset,
  682. unsigned int *pbar2_qid)
  683. {
  684. unsigned int page_shift, page_size, qpp_shift, qpp_mask;
  685. u64 bar2_page_offset, bar2_qoffset;
  686. unsigned int bar2_qid, bar2_qid_offset, bar2_qinferred;
  687. /* T4 doesn't support BAR2 SGE Queue registers.
  688. */
  689. if (is_t4(adapter->params.chip))
  690. return -EINVAL;
  691. /* Get our SGE Page Size parameters.
  692. */
  693. page_shift = adapter->params.sge.sge_vf_hps + 10;
  694. page_size = 1 << page_shift;
  695. /* Get the right Queues per Page parameters for our Queue.
  696. */
  697. qpp_shift = (qtype == T4_BAR2_QTYPE_EGRESS
  698. ? adapter->params.sge.sge_vf_eq_qpp
  699. : adapter->params.sge.sge_vf_iq_qpp);
  700. qpp_mask = (1 << qpp_shift) - 1;
  701. /* Calculate the basics of the BAR2 SGE Queue register area:
  702. * o The BAR2 page the Queue registers will be in.
  703. * o The BAR2 Queue ID.
  704. * o The BAR2 Queue ID Offset into the BAR2 page.
  705. */
  706. bar2_page_offset = ((u64)(qid >> qpp_shift) << page_shift);
  707. bar2_qid = qid & qpp_mask;
  708. bar2_qid_offset = bar2_qid * SGE_UDB_SIZE;
  709. /* If the BAR2 Queue ID Offset is less than the Page Size, then the
  710. * hardware will infer the Absolute Queue ID simply from the writes to
  711. * the BAR2 Queue ID Offset within the BAR2 Page (and we need to use a
  712. * BAR2 Queue ID of 0 for those writes). Otherwise, we'll simply
  713. * write to the first BAR2 SGE Queue Area within the BAR2 Page with
  714. * the BAR2 Queue ID and the hardware will infer the Absolute Queue ID
  715. * from the BAR2 Page and BAR2 Queue ID.
  716. *
  717. * One important censequence of this is that some BAR2 SGE registers
  718. * have a "Queue ID" field and we can write the BAR2 SGE Queue ID
  719. * there. But other registers synthesize the SGE Queue ID purely
  720. * from the writes to the registers -- the Write Combined Doorbell
  721. * Buffer is a good example. These BAR2 SGE Registers are only
  722. * available for those BAR2 SGE Register areas where the SGE Absolute
  723. * Queue ID can be inferred from simple writes.
  724. */
  725. bar2_qoffset = bar2_page_offset;
  726. bar2_qinferred = (bar2_qid_offset < page_size);
  727. if (bar2_qinferred) {
  728. bar2_qoffset += bar2_qid_offset;
  729. bar2_qid = 0;
  730. }
  731. *pbar2_qoffset = bar2_qoffset;
  732. *pbar2_qid = bar2_qid;
  733. return 0;
  734. }
  735. unsigned int t4vf_get_pf_from_vf(struct adapter *adapter)
  736. {
  737. u32 whoami;
  738. whoami = t4_read_reg(adapter, T4VF_PL_BASE_ADDR + PL_VF_WHOAMI_A);
  739. return (CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5 ?
  740. SOURCEPF_G(whoami) : T6_SOURCEPF_G(whoami));
  741. }
  742. /**
  743. * t4vf_get_sge_params - retrieve adapter Scatter gather Engine parameters
  744. * @adapter: the adapter
  745. *
  746. * Retrieves various core SGE parameters in the form of hardware SGE
  747. * register values. The caller is responsible for decoding these as
  748. * needed. The SGE parameters are stored in @adapter->params.sge.
  749. */
  750. int t4vf_get_sge_params(struct adapter *adapter)
  751. {
  752. struct sge_params *sge_params = &adapter->params.sge;
  753. u32 params[7], vals[7];
  754. int v;
  755. params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
  756. FW_PARAMS_PARAM_XYZ_V(SGE_CONTROL_A));
  757. params[1] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
  758. FW_PARAMS_PARAM_XYZ_V(SGE_HOST_PAGE_SIZE_A));
  759. params[2] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
  760. FW_PARAMS_PARAM_XYZ_V(SGE_FL_BUFFER_SIZE0_A));
  761. params[3] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
  762. FW_PARAMS_PARAM_XYZ_V(SGE_FL_BUFFER_SIZE1_A));
  763. params[4] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
  764. FW_PARAMS_PARAM_XYZ_V(SGE_TIMER_VALUE_0_AND_1_A));
  765. params[5] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
  766. FW_PARAMS_PARAM_XYZ_V(SGE_TIMER_VALUE_2_AND_3_A));
  767. params[6] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
  768. FW_PARAMS_PARAM_XYZ_V(SGE_TIMER_VALUE_4_AND_5_A));
  769. v = t4vf_query_params(adapter, 7, params, vals);
  770. if (v)
  771. return v;
  772. sge_params->sge_control = vals[0];
  773. sge_params->sge_host_page_size = vals[1];
  774. sge_params->sge_fl_buffer_size[0] = vals[2];
  775. sge_params->sge_fl_buffer_size[1] = vals[3];
  776. sge_params->sge_timer_value_0_and_1 = vals[4];
  777. sge_params->sge_timer_value_2_and_3 = vals[5];
  778. sge_params->sge_timer_value_4_and_5 = vals[6];
  779. /* T4 uses a single control field to specify both the PCIe Padding and
  780. * Packing Boundary. T5 introduced the ability to specify these
  781. * separately with the Padding Boundary in SGE_CONTROL and and Packing
  782. * Boundary in SGE_CONTROL2. So for T5 and later we need to grab
  783. * SGE_CONTROL in order to determine how ingress packet data will be
  784. * laid out in Packed Buffer Mode. Unfortunately, older versions of
  785. * the firmware won't let us retrieve SGE_CONTROL2 so if we get a
  786. * failure grabbing it we throw an error since we can't figure out the
  787. * right value.
  788. */
  789. if (!is_t4(adapter->params.chip)) {
  790. params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
  791. FW_PARAMS_PARAM_XYZ_V(SGE_CONTROL2_A));
  792. v = t4vf_query_params(adapter, 1, params, vals);
  793. if (v != FW_SUCCESS) {
  794. dev_err(adapter->pdev_dev,
  795. "Unable to get SGE Control2; "
  796. "probably old firmware.\n");
  797. return v;
  798. }
  799. sge_params->sge_control2 = vals[0];
  800. }
  801. params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
  802. FW_PARAMS_PARAM_XYZ_V(SGE_INGRESS_RX_THRESHOLD_A));
  803. params[1] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
  804. FW_PARAMS_PARAM_XYZ_V(SGE_CONM_CTRL_A));
  805. v = t4vf_query_params(adapter, 2, params, vals);
  806. if (v)
  807. return v;
  808. sge_params->sge_ingress_rx_threshold = vals[0];
  809. sge_params->sge_congestion_control = vals[1];
  810. /* For T5 and later we want to use the new BAR2 Doorbells.
  811. * Unfortunately, older firmware didn't allow the this register to be
  812. * read.
  813. */
  814. if (!is_t4(adapter->params.chip)) {
  815. unsigned int pf, s_hps, s_qpp;
  816. params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
  817. FW_PARAMS_PARAM_XYZ_V(
  818. SGE_EGRESS_QUEUES_PER_PAGE_VF_A));
  819. params[1] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
  820. FW_PARAMS_PARAM_XYZ_V(
  821. SGE_INGRESS_QUEUES_PER_PAGE_VF_A));
  822. v = t4vf_query_params(adapter, 2, params, vals);
  823. if (v != FW_SUCCESS) {
  824. dev_warn(adapter->pdev_dev,
  825. "Unable to get VF SGE Queues/Page; "
  826. "probably old firmware.\n");
  827. return v;
  828. }
  829. sge_params->sge_egress_queues_per_page = vals[0];
  830. sge_params->sge_ingress_queues_per_page = vals[1];
  831. /* We need the Queues/Page for our VF. This is based on the
  832. * PF from which we're instantiated and is indexed in the
  833. * register we just read. Do it once here so other code in
  834. * the driver can just use it.
  835. */
  836. pf = t4vf_get_pf_from_vf(adapter);
  837. s_hps = (HOSTPAGESIZEPF0_S +
  838. (HOSTPAGESIZEPF1_S - HOSTPAGESIZEPF0_S) * pf);
  839. sge_params->sge_vf_hps =
  840. ((sge_params->sge_host_page_size >> s_hps)
  841. & HOSTPAGESIZEPF0_M);
  842. s_qpp = (QUEUESPERPAGEPF0_S +
  843. (QUEUESPERPAGEPF1_S - QUEUESPERPAGEPF0_S) * pf);
  844. sge_params->sge_vf_eq_qpp =
  845. ((sge_params->sge_egress_queues_per_page >> s_qpp)
  846. & QUEUESPERPAGEPF0_M);
  847. sge_params->sge_vf_iq_qpp =
  848. ((sge_params->sge_ingress_queues_per_page >> s_qpp)
  849. & QUEUESPERPAGEPF0_M);
  850. }
  851. return 0;
  852. }
  853. /**
  854. * t4vf_get_vpd_params - retrieve device VPD paremeters
  855. * @adapter: the adapter
  856. *
  857. * Retrives various device Vital Product Data parameters. The parameters
  858. * are stored in @adapter->params.vpd.
  859. */
  860. int t4vf_get_vpd_params(struct adapter *adapter)
  861. {
  862. struct vpd_params *vpd_params = &adapter->params.vpd;
  863. u32 params[7], vals[7];
  864. int v;
  865. params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
  866. FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CCLK));
  867. v = t4vf_query_params(adapter, 1, params, vals);
  868. if (v)
  869. return v;
  870. vpd_params->cclk = vals[0];
  871. return 0;
  872. }
  873. /**
  874. * t4vf_get_dev_params - retrieve device paremeters
  875. * @adapter: the adapter
  876. *
  877. * Retrives various device parameters. The parameters are stored in
  878. * @adapter->params.dev.
  879. */
  880. int t4vf_get_dev_params(struct adapter *adapter)
  881. {
  882. struct dev_params *dev_params = &adapter->params.dev;
  883. u32 params[7], vals[7];
  884. int v;
  885. params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
  886. FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_FWREV));
  887. params[1] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
  888. FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_TPREV));
  889. v = t4vf_query_params(adapter, 2, params, vals);
  890. if (v)
  891. return v;
  892. dev_params->fwrev = vals[0];
  893. dev_params->tprev = vals[1];
  894. return 0;
  895. }
  896. /**
  897. * t4vf_get_rss_glb_config - retrieve adapter RSS Global Configuration
  898. * @adapter: the adapter
  899. *
  900. * Retrieves global RSS mode and parameters with which we have to live
  901. * and stores them in the @adapter's RSS parameters.
  902. */
  903. int t4vf_get_rss_glb_config(struct adapter *adapter)
  904. {
  905. struct rss_params *rss = &adapter->params.rss;
  906. struct fw_rss_glb_config_cmd cmd, rpl;
  907. int v;
  908. /*
  909. * Execute an RSS Global Configuration read command to retrieve
  910. * our RSS configuration.
  911. */
  912. memset(&cmd, 0, sizeof(cmd));
  913. cmd.op_to_write = cpu_to_be32(FW_CMD_OP_V(FW_RSS_GLB_CONFIG_CMD) |
  914. FW_CMD_REQUEST_F |
  915. FW_CMD_READ_F);
  916. cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
  917. v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
  918. if (v)
  919. return v;
  920. /*
  921. * Transate the big-endian RSS Global Configuration into our
  922. * cpu-endian format based on the RSS mode. We also do first level
  923. * filtering at this point to weed out modes which don't support
  924. * VF Drivers ...
  925. */
  926. rss->mode = FW_RSS_GLB_CONFIG_CMD_MODE_G(
  927. be32_to_cpu(rpl.u.manual.mode_pkd));
  928. switch (rss->mode) {
  929. case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL: {
  930. u32 word = be32_to_cpu(
  931. rpl.u.basicvirtual.synmapen_to_hashtoeplitz);
  932. rss->u.basicvirtual.synmapen =
  933. ((word & FW_RSS_GLB_CONFIG_CMD_SYNMAPEN_F) != 0);
  934. rss->u.basicvirtual.syn4tupenipv6 =
  935. ((word & FW_RSS_GLB_CONFIG_CMD_SYN4TUPENIPV6_F) != 0);
  936. rss->u.basicvirtual.syn2tupenipv6 =
  937. ((word & FW_RSS_GLB_CONFIG_CMD_SYN2TUPENIPV6_F) != 0);
  938. rss->u.basicvirtual.syn4tupenipv4 =
  939. ((word & FW_RSS_GLB_CONFIG_CMD_SYN4TUPENIPV4_F) != 0);
  940. rss->u.basicvirtual.syn2tupenipv4 =
  941. ((word & FW_RSS_GLB_CONFIG_CMD_SYN2TUPENIPV4_F) != 0);
  942. rss->u.basicvirtual.ofdmapen =
  943. ((word & FW_RSS_GLB_CONFIG_CMD_OFDMAPEN_F) != 0);
  944. rss->u.basicvirtual.tnlmapen =
  945. ((word & FW_RSS_GLB_CONFIG_CMD_TNLMAPEN_F) != 0);
  946. rss->u.basicvirtual.tnlalllookup =
  947. ((word & FW_RSS_GLB_CONFIG_CMD_TNLALLLKP_F) != 0);
  948. rss->u.basicvirtual.hashtoeplitz =
  949. ((word & FW_RSS_GLB_CONFIG_CMD_HASHTOEPLITZ_F) != 0);
  950. /* we need at least Tunnel Map Enable to be set */
  951. if (!rss->u.basicvirtual.tnlmapen)
  952. return -EINVAL;
  953. break;
  954. }
  955. default:
  956. /* all unknown/unsupported RSS modes result in an error */
  957. return -EINVAL;
  958. }
  959. return 0;
  960. }
  961. /**
  962. * t4vf_get_vfres - retrieve VF resource limits
  963. * @adapter: the adapter
  964. *
  965. * Retrieves configured resource limits and capabilities for a virtual
  966. * function. The results are stored in @adapter->vfres.
  967. */
  968. int t4vf_get_vfres(struct adapter *adapter)
  969. {
  970. struct vf_resources *vfres = &adapter->params.vfres;
  971. struct fw_pfvf_cmd cmd, rpl;
  972. int v;
  973. u32 word;
  974. /*
  975. * Execute PFVF Read command to get VF resource limits; bail out early
  976. * with error on command failure.
  977. */
  978. memset(&cmd, 0, sizeof(cmd));
  979. cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_PFVF_CMD) |
  980. FW_CMD_REQUEST_F |
  981. FW_CMD_READ_F);
  982. cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
  983. v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
  984. if (v)
  985. return v;
  986. /*
  987. * Extract VF resource limits and return success.
  988. */
  989. word = be32_to_cpu(rpl.niqflint_niq);
  990. vfres->niqflint = FW_PFVF_CMD_NIQFLINT_G(word);
  991. vfres->niq = FW_PFVF_CMD_NIQ_G(word);
  992. word = be32_to_cpu(rpl.type_to_neq);
  993. vfres->neq = FW_PFVF_CMD_NEQ_G(word);
  994. vfres->pmask = FW_PFVF_CMD_PMASK_G(word);
  995. word = be32_to_cpu(rpl.tc_to_nexactf);
  996. vfres->tc = FW_PFVF_CMD_TC_G(word);
  997. vfres->nvi = FW_PFVF_CMD_NVI_G(word);
  998. vfres->nexactf = FW_PFVF_CMD_NEXACTF_G(word);
  999. word = be32_to_cpu(rpl.r_caps_to_nethctrl);
  1000. vfres->r_caps = FW_PFVF_CMD_R_CAPS_G(word);
  1001. vfres->wx_caps = FW_PFVF_CMD_WX_CAPS_G(word);
  1002. vfres->nethctrl = FW_PFVF_CMD_NETHCTRL_G(word);
  1003. return 0;
  1004. }
  1005. /**
  1006. * t4vf_read_rss_vi_config - read a VI's RSS configuration
  1007. * @adapter: the adapter
  1008. * @viid: Virtual Interface ID
  1009. * @config: pointer to host-native VI RSS Configuration buffer
  1010. *
  1011. * Reads the Virtual Interface's RSS configuration information and
  1012. * translates it into CPU-native format.
  1013. */
  1014. int t4vf_read_rss_vi_config(struct adapter *adapter, unsigned int viid,
  1015. union rss_vi_config *config)
  1016. {
  1017. struct fw_rss_vi_config_cmd cmd, rpl;
  1018. int v;
  1019. memset(&cmd, 0, sizeof(cmd));
  1020. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_RSS_VI_CONFIG_CMD) |
  1021. FW_CMD_REQUEST_F |
  1022. FW_CMD_READ_F |
  1023. FW_RSS_VI_CONFIG_CMD_VIID(viid));
  1024. cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
  1025. v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
  1026. if (v)
  1027. return v;
  1028. switch (adapter->params.rss.mode) {
  1029. case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL: {
  1030. u32 word = be32_to_cpu(rpl.u.basicvirtual.defaultq_to_udpen);
  1031. config->basicvirtual.ip6fourtupen =
  1032. ((word & FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F) != 0);
  1033. config->basicvirtual.ip6twotupen =
  1034. ((word & FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F) != 0);
  1035. config->basicvirtual.ip4fourtupen =
  1036. ((word & FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F) != 0);
  1037. config->basicvirtual.ip4twotupen =
  1038. ((word & FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F) != 0);
  1039. config->basicvirtual.udpen =
  1040. ((word & FW_RSS_VI_CONFIG_CMD_UDPEN_F) != 0);
  1041. config->basicvirtual.defaultq =
  1042. FW_RSS_VI_CONFIG_CMD_DEFAULTQ_G(word);
  1043. break;
  1044. }
  1045. default:
  1046. return -EINVAL;
  1047. }
  1048. return 0;
  1049. }
  1050. /**
  1051. * t4vf_write_rss_vi_config - write a VI's RSS configuration
  1052. * @adapter: the adapter
  1053. * @viid: Virtual Interface ID
  1054. * @config: pointer to host-native VI RSS Configuration buffer
  1055. *
  1056. * Write the Virtual Interface's RSS configuration information
  1057. * (translating it into firmware-native format before writing).
  1058. */
  1059. int t4vf_write_rss_vi_config(struct adapter *adapter, unsigned int viid,
  1060. union rss_vi_config *config)
  1061. {
  1062. struct fw_rss_vi_config_cmd cmd, rpl;
  1063. memset(&cmd, 0, sizeof(cmd));
  1064. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_RSS_VI_CONFIG_CMD) |
  1065. FW_CMD_REQUEST_F |
  1066. FW_CMD_WRITE_F |
  1067. FW_RSS_VI_CONFIG_CMD_VIID(viid));
  1068. cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
  1069. switch (adapter->params.rss.mode) {
  1070. case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL: {
  1071. u32 word = 0;
  1072. if (config->basicvirtual.ip6fourtupen)
  1073. word |= FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F;
  1074. if (config->basicvirtual.ip6twotupen)
  1075. word |= FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F;
  1076. if (config->basicvirtual.ip4fourtupen)
  1077. word |= FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F;
  1078. if (config->basicvirtual.ip4twotupen)
  1079. word |= FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F;
  1080. if (config->basicvirtual.udpen)
  1081. word |= FW_RSS_VI_CONFIG_CMD_UDPEN_F;
  1082. word |= FW_RSS_VI_CONFIG_CMD_DEFAULTQ_V(
  1083. config->basicvirtual.defaultq);
  1084. cmd.u.basicvirtual.defaultq_to_udpen = cpu_to_be32(word);
  1085. break;
  1086. }
  1087. default:
  1088. return -EINVAL;
  1089. }
  1090. return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
  1091. }
  1092. /**
  1093. * t4vf_config_rss_range - configure a portion of the RSS mapping table
  1094. * @adapter: the adapter
  1095. * @viid: Virtual Interface of RSS Table Slice
  1096. * @start: starting entry in the table to write
  1097. * @n: how many table entries to write
  1098. * @rspq: values for the "Response Queue" (Ingress Queue) lookup table
  1099. * @nrspq: number of values in @rspq
  1100. *
  1101. * Programs the selected part of the VI's RSS mapping table with the
  1102. * provided values. If @nrspq < @n the supplied values are used repeatedly
  1103. * until the full table range is populated.
  1104. *
  1105. * The caller must ensure the values in @rspq are in the range 0..1023.
  1106. */
  1107. int t4vf_config_rss_range(struct adapter *adapter, unsigned int viid,
  1108. int start, int n, const u16 *rspq, int nrspq)
  1109. {
  1110. const u16 *rsp = rspq;
  1111. const u16 *rsp_end = rspq+nrspq;
  1112. struct fw_rss_ind_tbl_cmd cmd;
  1113. /*
  1114. * Initialize firmware command template to write the RSS table.
  1115. */
  1116. memset(&cmd, 0, sizeof(cmd));
  1117. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_RSS_IND_TBL_CMD) |
  1118. FW_CMD_REQUEST_F |
  1119. FW_CMD_WRITE_F |
  1120. FW_RSS_IND_TBL_CMD_VIID_V(viid));
  1121. cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
  1122. /*
  1123. * Each firmware RSS command can accommodate up to 32 RSS Ingress
  1124. * Queue Identifiers. These Ingress Queue IDs are packed three to
  1125. * a 32-bit word as 10-bit values with the upper remaining 2 bits
  1126. * reserved.
  1127. */
  1128. while (n > 0) {
  1129. __be32 *qp = &cmd.iq0_to_iq2;
  1130. int nq = min(n, 32);
  1131. int ret;
  1132. /*
  1133. * Set up the firmware RSS command header to send the next
  1134. * "nq" Ingress Queue IDs to the firmware.
  1135. */
  1136. cmd.niqid = cpu_to_be16(nq);
  1137. cmd.startidx = cpu_to_be16(start);
  1138. /*
  1139. * "nq" more done for the start of the next loop.
  1140. */
  1141. start += nq;
  1142. n -= nq;
  1143. /*
  1144. * While there are still Ingress Queue IDs to stuff into the
  1145. * current firmware RSS command, retrieve them from the
  1146. * Ingress Queue ID array and insert them into the command.
  1147. */
  1148. while (nq > 0) {
  1149. /*
  1150. * Grab up to the next 3 Ingress Queue IDs (wrapping
  1151. * around the Ingress Queue ID array if necessary) and
  1152. * insert them into the firmware RSS command at the
  1153. * current 3-tuple position within the commad.
  1154. */
  1155. u16 qbuf[3];
  1156. u16 *qbp = qbuf;
  1157. int nqbuf = min(3, nq);
  1158. nq -= nqbuf;
  1159. qbuf[0] = qbuf[1] = qbuf[2] = 0;
  1160. while (nqbuf) {
  1161. nqbuf--;
  1162. *qbp++ = *rsp++;
  1163. if (rsp >= rsp_end)
  1164. rsp = rspq;
  1165. }
  1166. *qp++ = cpu_to_be32(FW_RSS_IND_TBL_CMD_IQ0_V(qbuf[0]) |
  1167. FW_RSS_IND_TBL_CMD_IQ1_V(qbuf[1]) |
  1168. FW_RSS_IND_TBL_CMD_IQ2_V(qbuf[2]));
  1169. }
  1170. /*
  1171. * Send this portion of the RRS table update to the firmware;
  1172. * bail out on any errors.
  1173. */
  1174. ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
  1175. if (ret)
  1176. return ret;
  1177. }
  1178. return 0;
  1179. }
  1180. /**
  1181. * t4vf_alloc_vi - allocate a virtual interface on a port
  1182. * @adapter: the adapter
  1183. * @port_id: physical port associated with the VI
  1184. *
  1185. * Allocate a new Virtual Interface and bind it to the indicated
  1186. * physical port. Return the new Virtual Interface Identifier on
  1187. * success, or a [negative] error number on failure.
  1188. */
  1189. int t4vf_alloc_vi(struct adapter *adapter, int port_id)
  1190. {
  1191. struct fw_vi_cmd cmd, rpl;
  1192. int v;
  1193. /*
  1194. * Execute a VI command to allocate Virtual Interface and return its
  1195. * VIID.
  1196. */
  1197. memset(&cmd, 0, sizeof(cmd));
  1198. cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_VI_CMD) |
  1199. FW_CMD_REQUEST_F |
  1200. FW_CMD_WRITE_F |
  1201. FW_CMD_EXEC_F);
  1202. cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(cmd) |
  1203. FW_VI_CMD_ALLOC_F);
  1204. cmd.portid_pkd = FW_VI_CMD_PORTID_V(port_id);
  1205. v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
  1206. if (v)
  1207. return v;
  1208. return FW_VI_CMD_VIID_G(be16_to_cpu(rpl.type_viid));
  1209. }
  1210. /**
  1211. * t4vf_free_vi -- free a virtual interface
  1212. * @adapter: the adapter
  1213. * @viid: the virtual interface identifier
  1214. *
  1215. * Free a previously allocated Virtual Interface. Return an error on
  1216. * failure.
  1217. */
  1218. int t4vf_free_vi(struct adapter *adapter, int viid)
  1219. {
  1220. struct fw_vi_cmd cmd;
  1221. /*
  1222. * Execute a VI command to free the Virtual Interface.
  1223. */
  1224. memset(&cmd, 0, sizeof(cmd));
  1225. cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_VI_CMD) |
  1226. FW_CMD_REQUEST_F |
  1227. FW_CMD_EXEC_F);
  1228. cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(cmd) |
  1229. FW_VI_CMD_FREE_F);
  1230. cmd.type_viid = cpu_to_be16(FW_VI_CMD_VIID_V(viid));
  1231. return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
  1232. }
  1233. /**
  1234. * t4vf_enable_vi - enable/disable a virtual interface
  1235. * @adapter: the adapter
  1236. * @viid: the Virtual Interface ID
  1237. * @rx_en: 1=enable Rx, 0=disable Rx
  1238. * @tx_en: 1=enable Tx, 0=disable Tx
  1239. *
  1240. * Enables/disables a virtual interface.
  1241. */
  1242. int t4vf_enable_vi(struct adapter *adapter, unsigned int viid,
  1243. bool rx_en, bool tx_en)
  1244. {
  1245. struct fw_vi_enable_cmd cmd;
  1246. memset(&cmd, 0, sizeof(cmd));
  1247. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_VI_ENABLE_CMD) |
  1248. FW_CMD_REQUEST_F |
  1249. FW_CMD_EXEC_F |
  1250. FW_VI_ENABLE_CMD_VIID_V(viid));
  1251. cmd.ien_to_len16 = cpu_to_be32(FW_VI_ENABLE_CMD_IEN_V(rx_en) |
  1252. FW_VI_ENABLE_CMD_EEN_V(tx_en) |
  1253. FW_LEN16(cmd));
  1254. return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
  1255. }
  1256. /**
  1257. * t4vf_enable_pi - enable/disable a Port's virtual interface
  1258. * @adapter: the adapter
  1259. * @pi: the Port Information structure
  1260. * @rx_en: 1=enable Rx, 0=disable Rx
  1261. * @tx_en: 1=enable Tx, 0=disable Tx
  1262. *
  1263. * Enables/disables a Port's virtual interface. If the Virtual
  1264. * Interface enable/disable operation is successful, we notify the
  1265. * OS-specific code of a potential Link Status change via the OS Contract
  1266. * API t4vf_os_link_changed().
  1267. */
  1268. int t4vf_enable_pi(struct adapter *adapter, struct port_info *pi,
  1269. bool rx_en, bool tx_en)
  1270. {
  1271. int ret = t4vf_enable_vi(adapter, pi->viid, rx_en, tx_en);
  1272. if (ret)
  1273. return ret;
  1274. t4vf_os_link_changed(adapter, pi->pidx,
  1275. rx_en && tx_en && pi->link_cfg.link_ok);
  1276. return 0;
  1277. }
  1278. /**
  1279. * t4vf_identify_port - identify a VI's port by blinking its LED
  1280. * @adapter: the adapter
  1281. * @viid: the Virtual Interface ID
  1282. * @nblinks: how many times to blink LED at 2.5 Hz
  1283. *
  1284. * Identifies a VI's port by blinking its LED.
  1285. */
  1286. int t4vf_identify_port(struct adapter *adapter, unsigned int viid,
  1287. unsigned int nblinks)
  1288. {
  1289. struct fw_vi_enable_cmd cmd;
  1290. memset(&cmd, 0, sizeof(cmd));
  1291. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_VI_ENABLE_CMD) |
  1292. FW_CMD_REQUEST_F |
  1293. FW_CMD_EXEC_F |
  1294. FW_VI_ENABLE_CMD_VIID_V(viid));
  1295. cmd.ien_to_len16 = cpu_to_be32(FW_VI_ENABLE_CMD_LED_F |
  1296. FW_LEN16(cmd));
  1297. cmd.blinkdur = cpu_to_be16(nblinks);
  1298. return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
  1299. }
  1300. /**
  1301. * t4vf_set_rxmode - set Rx properties of a virtual interface
  1302. * @adapter: the adapter
  1303. * @viid: the VI id
  1304. * @mtu: the new MTU or -1 for no change
  1305. * @promisc: 1 to enable promiscuous mode, 0 to disable it, -1 no change
  1306. * @all_multi: 1 to enable all-multi mode, 0 to disable it, -1 no change
  1307. * @bcast: 1 to enable broadcast Rx, 0 to disable it, -1 no change
  1308. * @vlanex: 1 to enable hardware VLAN Tag extraction, 0 to disable it,
  1309. * -1 no change
  1310. *
  1311. * Sets Rx properties of a virtual interface.
  1312. */
  1313. int t4vf_set_rxmode(struct adapter *adapter, unsigned int viid,
  1314. int mtu, int promisc, int all_multi, int bcast, int vlanex,
  1315. bool sleep_ok)
  1316. {
  1317. struct fw_vi_rxmode_cmd cmd;
  1318. /* convert to FW values */
  1319. if (mtu < 0)
  1320. mtu = FW_VI_RXMODE_CMD_MTU_M;
  1321. if (promisc < 0)
  1322. promisc = FW_VI_RXMODE_CMD_PROMISCEN_M;
  1323. if (all_multi < 0)
  1324. all_multi = FW_VI_RXMODE_CMD_ALLMULTIEN_M;
  1325. if (bcast < 0)
  1326. bcast = FW_VI_RXMODE_CMD_BROADCASTEN_M;
  1327. if (vlanex < 0)
  1328. vlanex = FW_VI_RXMODE_CMD_VLANEXEN_M;
  1329. memset(&cmd, 0, sizeof(cmd));
  1330. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_VI_RXMODE_CMD) |
  1331. FW_CMD_REQUEST_F |
  1332. FW_CMD_WRITE_F |
  1333. FW_VI_RXMODE_CMD_VIID_V(viid));
  1334. cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
  1335. cmd.mtu_to_vlanexen =
  1336. cpu_to_be32(FW_VI_RXMODE_CMD_MTU_V(mtu) |
  1337. FW_VI_RXMODE_CMD_PROMISCEN_V(promisc) |
  1338. FW_VI_RXMODE_CMD_ALLMULTIEN_V(all_multi) |
  1339. FW_VI_RXMODE_CMD_BROADCASTEN_V(bcast) |
  1340. FW_VI_RXMODE_CMD_VLANEXEN_V(vlanex));
  1341. return t4vf_wr_mbox_core(adapter, &cmd, sizeof(cmd), NULL, sleep_ok);
  1342. }
  1343. /**
  1344. * t4vf_alloc_mac_filt - allocates exact-match filters for MAC addresses
  1345. * @adapter: the adapter
  1346. * @viid: the Virtual Interface Identifier
  1347. * @free: if true any existing filters for this VI id are first removed
  1348. * @naddr: the number of MAC addresses to allocate filters for (up to 7)
  1349. * @addr: the MAC address(es)
  1350. * @idx: where to store the index of each allocated filter
  1351. * @hash: pointer to hash address filter bitmap
  1352. * @sleep_ok: call is allowed to sleep
  1353. *
  1354. * Allocates an exact-match filter for each of the supplied addresses and
  1355. * sets it to the corresponding address. If @idx is not %NULL it should
  1356. * have at least @naddr entries, each of which will be set to the index of
  1357. * the filter allocated for the corresponding MAC address. If a filter
  1358. * could not be allocated for an address its index is set to 0xffff.
  1359. * If @hash is not %NULL addresses that fail to allocate an exact filter
  1360. * are hashed and update the hash filter bitmap pointed at by @hash.
  1361. *
  1362. * Returns a negative error number or the number of filters allocated.
  1363. */
  1364. int t4vf_alloc_mac_filt(struct adapter *adapter, unsigned int viid, bool free,
  1365. unsigned int naddr, const u8 **addr, u16 *idx,
  1366. u64 *hash, bool sleep_ok)
  1367. {
  1368. int offset, ret = 0;
  1369. unsigned nfilters = 0;
  1370. unsigned int rem = naddr;
  1371. struct fw_vi_mac_cmd cmd, rpl;
  1372. unsigned int max_naddr = adapter->params.arch.mps_tcam_size;
  1373. if (naddr > max_naddr)
  1374. return -EINVAL;
  1375. for (offset = 0; offset < naddr; /**/) {
  1376. unsigned int fw_naddr = (rem < ARRAY_SIZE(cmd.u.exact)
  1377. ? rem
  1378. : ARRAY_SIZE(cmd.u.exact));
  1379. size_t len16 = DIV_ROUND_UP(offsetof(struct fw_vi_mac_cmd,
  1380. u.exact[fw_naddr]), 16);
  1381. struct fw_vi_mac_exact *p;
  1382. int i;
  1383. memset(&cmd, 0, sizeof(cmd));
  1384. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_VI_MAC_CMD) |
  1385. FW_CMD_REQUEST_F |
  1386. FW_CMD_WRITE_F |
  1387. (free ? FW_CMD_EXEC_F : 0) |
  1388. FW_VI_MAC_CMD_VIID_V(viid));
  1389. cmd.freemacs_to_len16 =
  1390. cpu_to_be32(FW_VI_MAC_CMD_FREEMACS_V(free) |
  1391. FW_CMD_LEN16_V(len16));
  1392. for (i = 0, p = cmd.u.exact; i < fw_naddr; i++, p++) {
  1393. p->valid_to_idx = cpu_to_be16(
  1394. FW_VI_MAC_CMD_VALID_F |
  1395. FW_VI_MAC_CMD_IDX_V(FW_VI_MAC_ADD_MAC));
  1396. memcpy(p->macaddr, addr[offset+i], sizeof(p->macaddr));
  1397. }
  1398. ret = t4vf_wr_mbox_core(adapter, &cmd, sizeof(cmd), &rpl,
  1399. sleep_ok);
  1400. if (ret && ret != -ENOMEM)
  1401. break;
  1402. for (i = 0, p = rpl.u.exact; i < fw_naddr; i++, p++) {
  1403. u16 index = FW_VI_MAC_CMD_IDX_G(
  1404. be16_to_cpu(p->valid_to_idx));
  1405. if (idx)
  1406. idx[offset+i] =
  1407. (index >= max_naddr
  1408. ? 0xffff
  1409. : index);
  1410. if (index < max_naddr)
  1411. nfilters++;
  1412. else if (hash)
  1413. *hash |= (1ULL << hash_mac_addr(addr[offset+i]));
  1414. }
  1415. free = false;
  1416. offset += fw_naddr;
  1417. rem -= fw_naddr;
  1418. }
  1419. /*
  1420. * If there were no errors or we merely ran out of room in our MAC
  1421. * address arena, return the number of filters actually written.
  1422. */
  1423. if (ret == 0 || ret == -ENOMEM)
  1424. ret = nfilters;
  1425. return ret;
  1426. }
  1427. /**
  1428. * t4vf_free_mac_filt - frees exact-match filters of given MAC addresses
  1429. * @adapter: the adapter
  1430. * @viid: the VI id
  1431. * @naddr: the number of MAC addresses to allocate filters for (up to 7)
  1432. * @addr: the MAC address(es)
  1433. * @sleep_ok: call is allowed to sleep
  1434. *
  1435. * Frees the exact-match filter for each of the supplied addresses
  1436. *
  1437. * Returns a negative error number or the number of filters freed.
  1438. */
  1439. int t4vf_free_mac_filt(struct adapter *adapter, unsigned int viid,
  1440. unsigned int naddr, const u8 **addr, bool sleep_ok)
  1441. {
  1442. int offset, ret = 0;
  1443. struct fw_vi_mac_cmd cmd;
  1444. unsigned int nfilters = 0;
  1445. unsigned int max_naddr = adapter->params.arch.mps_tcam_size;
  1446. unsigned int rem = naddr;
  1447. if (naddr > max_naddr)
  1448. return -EINVAL;
  1449. for (offset = 0; offset < (int)naddr ; /**/) {
  1450. unsigned int fw_naddr = (rem < ARRAY_SIZE(cmd.u.exact) ?
  1451. rem : ARRAY_SIZE(cmd.u.exact));
  1452. size_t len16 = DIV_ROUND_UP(offsetof(struct fw_vi_mac_cmd,
  1453. u.exact[fw_naddr]), 16);
  1454. struct fw_vi_mac_exact *p;
  1455. int i;
  1456. memset(&cmd, 0, sizeof(cmd));
  1457. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_VI_MAC_CMD) |
  1458. FW_CMD_REQUEST_F |
  1459. FW_CMD_WRITE_F |
  1460. FW_CMD_EXEC_V(0) |
  1461. FW_VI_MAC_CMD_VIID_V(viid));
  1462. cmd.freemacs_to_len16 =
  1463. cpu_to_be32(FW_VI_MAC_CMD_FREEMACS_V(0) |
  1464. FW_CMD_LEN16_V(len16));
  1465. for (i = 0, p = cmd.u.exact; i < (int)fw_naddr; i++, p++) {
  1466. p->valid_to_idx = cpu_to_be16(
  1467. FW_VI_MAC_CMD_VALID_F |
  1468. FW_VI_MAC_CMD_IDX_V(FW_VI_MAC_MAC_BASED_FREE));
  1469. memcpy(p->macaddr, addr[offset+i], sizeof(p->macaddr));
  1470. }
  1471. ret = t4vf_wr_mbox_core(adapter, &cmd, sizeof(cmd), &cmd,
  1472. sleep_ok);
  1473. if (ret)
  1474. break;
  1475. for (i = 0, p = cmd.u.exact; i < fw_naddr; i++, p++) {
  1476. u16 index = FW_VI_MAC_CMD_IDX_G(
  1477. be16_to_cpu(p->valid_to_idx));
  1478. if (index < max_naddr)
  1479. nfilters++;
  1480. }
  1481. offset += fw_naddr;
  1482. rem -= fw_naddr;
  1483. }
  1484. if (ret == 0)
  1485. ret = nfilters;
  1486. return ret;
  1487. }
  1488. /**
  1489. * t4vf_change_mac - modifies the exact-match filter for a MAC address
  1490. * @adapter: the adapter
  1491. * @viid: the Virtual Interface ID
  1492. * @idx: index of existing filter for old value of MAC address, or -1
  1493. * @addr: the new MAC address value
  1494. * @persist: if idx < 0, the new MAC allocation should be persistent
  1495. *
  1496. * Modifies an exact-match filter and sets it to the new MAC address.
  1497. * Note that in general it is not possible to modify the value of a given
  1498. * filter so the generic way to modify an address filter is to free the
  1499. * one being used by the old address value and allocate a new filter for
  1500. * the new address value. @idx can be -1 if the address is a new
  1501. * addition.
  1502. *
  1503. * Returns a negative error number or the index of the filter with the new
  1504. * MAC value.
  1505. */
  1506. int t4vf_change_mac(struct adapter *adapter, unsigned int viid,
  1507. int idx, const u8 *addr, bool persist)
  1508. {
  1509. int ret;
  1510. struct fw_vi_mac_cmd cmd, rpl;
  1511. struct fw_vi_mac_exact *p = &cmd.u.exact[0];
  1512. size_t len16 = DIV_ROUND_UP(offsetof(struct fw_vi_mac_cmd,
  1513. u.exact[1]), 16);
  1514. unsigned int max_mac_addr = adapter->params.arch.mps_tcam_size;
  1515. /*
  1516. * If this is a new allocation, determine whether it should be
  1517. * persistent (across a "freemacs" operation) or not.
  1518. */
  1519. if (idx < 0)
  1520. idx = persist ? FW_VI_MAC_ADD_PERSIST_MAC : FW_VI_MAC_ADD_MAC;
  1521. memset(&cmd, 0, sizeof(cmd));
  1522. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_VI_MAC_CMD) |
  1523. FW_CMD_REQUEST_F |
  1524. FW_CMD_WRITE_F |
  1525. FW_VI_MAC_CMD_VIID_V(viid));
  1526. cmd.freemacs_to_len16 = cpu_to_be32(FW_CMD_LEN16_V(len16));
  1527. p->valid_to_idx = cpu_to_be16(FW_VI_MAC_CMD_VALID_F |
  1528. FW_VI_MAC_CMD_IDX_V(idx));
  1529. memcpy(p->macaddr, addr, sizeof(p->macaddr));
  1530. ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
  1531. if (ret == 0) {
  1532. p = &rpl.u.exact[0];
  1533. ret = FW_VI_MAC_CMD_IDX_G(be16_to_cpu(p->valid_to_idx));
  1534. if (ret >= max_mac_addr)
  1535. ret = -ENOMEM;
  1536. }
  1537. return ret;
  1538. }
  1539. /**
  1540. * t4vf_set_addr_hash - program the MAC inexact-match hash filter
  1541. * @adapter: the adapter
  1542. * @viid: the Virtual Interface Identifier
  1543. * @ucast: whether the hash filter should also match unicast addresses
  1544. * @vec: the value to be written to the hash filter
  1545. * @sleep_ok: call is allowed to sleep
  1546. *
  1547. * Sets the 64-bit inexact-match hash filter for a virtual interface.
  1548. */
  1549. int t4vf_set_addr_hash(struct adapter *adapter, unsigned int viid,
  1550. bool ucast, u64 vec, bool sleep_ok)
  1551. {
  1552. struct fw_vi_mac_cmd cmd;
  1553. size_t len16 = DIV_ROUND_UP(offsetof(struct fw_vi_mac_cmd,
  1554. u.exact[0]), 16);
  1555. memset(&cmd, 0, sizeof(cmd));
  1556. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_VI_MAC_CMD) |
  1557. FW_CMD_REQUEST_F |
  1558. FW_CMD_WRITE_F |
  1559. FW_VI_ENABLE_CMD_VIID_V(viid));
  1560. cmd.freemacs_to_len16 = cpu_to_be32(FW_VI_MAC_CMD_HASHVECEN_F |
  1561. FW_VI_MAC_CMD_HASHUNIEN_V(ucast) |
  1562. FW_CMD_LEN16_V(len16));
  1563. cmd.u.hash.hashvec = cpu_to_be64(vec);
  1564. return t4vf_wr_mbox_core(adapter, &cmd, sizeof(cmd), NULL, sleep_ok);
  1565. }
  1566. /**
  1567. * t4vf_get_port_stats - collect "port" statistics
  1568. * @adapter: the adapter
  1569. * @pidx: the port index
  1570. * @s: the stats structure to fill
  1571. *
  1572. * Collect statistics for the "port"'s Virtual Interface.
  1573. */
  1574. int t4vf_get_port_stats(struct adapter *adapter, int pidx,
  1575. struct t4vf_port_stats *s)
  1576. {
  1577. struct port_info *pi = adap2pinfo(adapter, pidx);
  1578. struct fw_vi_stats_vf fwstats;
  1579. unsigned int rem = VI_VF_NUM_STATS;
  1580. __be64 *fwsp = (__be64 *)&fwstats;
  1581. /*
  1582. * Grab the Virtual Interface statistics a chunk at a time via mailbox
  1583. * commands. We could use a Work Request and get all of them at once
  1584. * but that's an asynchronous interface which is awkward to use.
  1585. */
  1586. while (rem) {
  1587. unsigned int ix = VI_VF_NUM_STATS - rem;
  1588. unsigned int nstats = min(6U, rem);
  1589. struct fw_vi_stats_cmd cmd, rpl;
  1590. size_t len = (offsetof(struct fw_vi_stats_cmd, u) +
  1591. sizeof(struct fw_vi_stats_ctl));
  1592. size_t len16 = DIV_ROUND_UP(len, 16);
  1593. int ret;
  1594. memset(&cmd, 0, sizeof(cmd));
  1595. cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_VI_STATS_CMD) |
  1596. FW_VI_STATS_CMD_VIID_V(pi->viid) |
  1597. FW_CMD_REQUEST_F |
  1598. FW_CMD_READ_F);
  1599. cmd.retval_len16 = cpu_to_be32(FW_CMD_LEN16_V(len16));
  1600. cmd.u.ctl.nstats_ix =
  1601. cpu_to_be16(FW_VI_STATS_CMD_IX_V(ix) |
  1602. FW_VI_STATS_CMD_NSTATS_V(nstats));
  1603. ret = t4vf_wr_mbox_ns(adapter, &cmd, len, &rpl);
  1604. if (ret)
  1605. return ret;
  1606. memcpy(fwsp, &rpl.u.ctl.stat0, sizeof(__be64) * nstats);
  1607. rem -= nstats;
  1608. fwsp += nstats;
  1609. }
  1610. /*
  1611. * Translate firmware statistics into host native statistics.
  1612. */
  1613. s->tx_bcast_bytes = be64_to_cpu(fwstats.tx_bcast_bytes);
  1614. s->tx_bcast_frames = be64_to_cpu(fwstats.tx_bcast_frames);
  1615. s->tx_mcast_bytes = be64_to_cpu(fwstats.tx_mcast_bytes);
  1616. s->tx_mcast_frames = be64_to_cpu(fwstats.tx_mcast_frames);
  1617. s->tx_ucast_bytes = be64_to_cpu(fwstats.tx_ucast_bytes);
  1618. s->tx_ucast_frames = be64_to_cpu(fwstats.tx_ucast_frames);
  1619. s->tx_drop_frames = be64_to_cpu(fwstats.tx_drop_frames);
  1620. s->tx_offload_bytes = be64_to_cpu(fwstats.tx_offload_bytes);
  1621. s->tx_offload_frames = be64_to_cpu(fwstats.tx_offload_frames);
  1622. s->rx_bcast_bytes = be64_to_cpu(fwstats.rx_bcast_bytes);
  1623. s->rx_bcast_frames = be64_to_cpu(fwstats.rx_bcast_frames);
  1624. s->rx_mcast_bytes = be64_to_cpu(fwstats.rx_mcast_bytes);
  1625. s->rx_mcast_frames = be64_to_cpu(fwstats.rx_mcast_frames);
  1626. s->rx_ucast_bytes = be64_to_cpu(fwstats.rx_ucast_bytes);
  1627. s->rx_ucast_frames = be64_to_cpu(fwstats.rx_ucast_frames);
  1628. s->rx_err_frames = be64_to_cpu(fwstats.rx_err_frames);
  1629. return 0;
  1630. }
  1631. /**
  1632. * t4vf_iq_free - free an ingress queue and its free lists
  1633. * @adapter: the adapter
  1634. * @iqtype: the ingress queue type (FW_IQ_TYPE_FL_INT_CAP, etc.)
  1635. * @iqid: ingress queue ID
  1636. * @fl0id: FL0 queue ID or 0xffff if no attached FL0
  1637. * @fl1id: FL1 queue ID or 0xffff if no attached FL1
  1638. *
  1639. * Frees an ingress queue and its associated free lists, if any.
  1640. */
  1641. int t4vf_iq_free(struct adapter *adapter, unsigned int iqtype,
  1642. unsigned int iqid, unsigned int fl0id, unsigned int fl1id)
  1643. {
  1644. struct fw_iq_cmd cmd;
  1645. memset(&cmd, 0, sizeof(cmd));
  1646. cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_IQ_CMD) |
  1647. FW_CMD_REQUEST_F |
  1648. FW_CMD_EXEC_F);
  1649. cmd.alloc_to_len16 = cpu_to_be32(FW_IQ_CMD_FREE_F |
  1650. FW_LEN16(cmd));
  1651. cmd.type_to_iqandstindex =
  1652. cpu_to_be32(FW_IQ_CMD_TYPE_V(iqtype));
  1653. cmd.iqid = cpu_to_be16(iqid);
  1654. cmd.fl0id = cpu_to_be16(fl0id);
  1655. cmd.fl1id = cpu_to_be16(fl1id);
  1656. return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
  1657. }
  1658. /**
  1659. * t4vf_eth_eq_free - free an Ethernet egress queue
  1660. * @adapter: the adapter
  1661. * @eqid: egress queue ID
  1662. *
  1663. * Frees an Ethernet egress queue.
  1664. */
  1665. int t4vf_eth_eq_free(struct adapter *adapter, unsigned int eqid)
  1666. {
  1667. struct fw_eq_eth_cmd cmd;
  1668. memset(&cmd, 0, sizeof(cmd));
  1669. cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_EQ_ETH_CMD) |
  1670. FW_CMD_REQUEST_F |
  1671. FW_CMD_EXEC_F);
  1672. cmd.alloc_to_len16 = cpu_to_be32(FW_EQ_ETH_CMD_FREE_F |
  1673. FW_LEN16(cmd));
  1674. cmd.eqid_pkd = cpu_to_be32(FW_EQ_ETH_CMD_EQID_V(eqid));
  1675. return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
  1676. }
  1677. /**
  1678. * t4vf_link_down_rc_str - return a string for a Link Down Reason Code
  1679. * @link_down_rc: Link Down Reason Code
  1680. *
  1681. * Returns a string representation of the Link Down Reason Code.
  1682. */
  1683. static const char *t4vf_link_down_rc_str(unsigned char link_down_rc)
  1684. {
  1685. static const char * const reason[] = {
  1686. "Link Down",
  1687. "Remote Fault",
  1688. "Auto-negotiation Failure",
  1689. "Reserved",
  1690. "Insufficient Airflow",
  1691. "Unable To Determine Reason",
  1692. "No RX Signal Detected",
  1693. "Reserved",
  1694. };
  1695. if (link_down_rc >= ARRAY_SIZE(reason))
  1696. return "Bad Reason Code";
  1697. return reason[link_down_rc];
  1698. }
  1699. /**
  1700. * t4vf_handle_get_port_info - process a FW reply message
  1701. * @pi: the port info
  1702. * @rpl: start of the FW message
  1703. *
  1704. * Processes a GET_PORT_INFO FW reply message.
  1705. */
  1706. static void t4vf_handle_get_port_info(struct port_info *pi,
  1707. const struct fw_port_cmd *cmd)
  1708. {
  1709. int action = FW_PORT_CMD_ACTION_G(be32_to_cpu(cmd->action_to_len16));
  1710. struct adapter *adapter = pi->adapter;
  1711. struct link_config *lc = &pi->link_cfg;
  1712. int link_ok, linkdnrc;
  1713. enum fw_port_type port_type;
  1714. enum fw_port_module_type mod_type;
  1715. unsigned int speed, fc, fec;
  1716. fw_port_cap32_t pcaps, acaps, lpacaps, linkattr;
  1717. /* Extract the various fields from the Port Information message. */
  1718. switch (action) {
  1719. case FW_PORT_ACTION_GET_PORT_INFO: {
  1720. u32 lstatus = be32_to_cpu(cmd->u.info.lstatus_to_modtype);
  1721. link_ok = (lstatus & FW_PORT_CMD_LSTATUS_F) != 0;
  1722. linkdnrc = FW_PORT_CMD_LINKDNRC_G(lstatus);
  1723. port_type = FW_PORT_CMD_PTYPE_G(lstatus);
  1724. mod_type = FW_PORT_CMD_MODTYPE_G(lstatus);
  1725. pcaps = fwcaps16_to_caps32(be16_to_cpu(cmd->u.info.pcap));
  1726. acaps = fwcaps16_to_caps32(be16_to_cpu(cmd->u.info.acap));
  1727. lpacaps = fwcaps16_to_caps32(be16_to_cpu(cmd->u.info.lpacap));
  1728. /* Unfortunately the format of the Link Status in the old
  1729. * 16-bit Port Information message isn't the same as the
  1730. * 16-bit Port Capabilities bitfield used everywhere else ...
  1731. */
  1732. linkattr = 0;
  1733. if (lstatus & FW_PORT_CMD_RXPAUSE_F)
  1734. linkattr |= FW_PORT_CAP32_FC_RX;
  1735. if (lstatus & FW_PORT_CMD_TXPAUSE_F)
  1736. linkattr |= FW_PORT_CAP32_FC_TX;
  1737. if (lstatus & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_100M))
  1738. linkattr |= FW_PORT_CAP32_SPEED_100M;
  1739. if (lstatus & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_1G))
  1740. linkattr |= FW_PORT_CAP32_SPEED_1G;
  1741. if (lstatus & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_10G))
  1742. linkattr |= FW_PORT_CAP32_SPEED_10G;
  1743. if (lstatus & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_25G))
  1744. linkattr |= FW_PORT_CAP32_SPEED_25G;
  1745. if (lstatus & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_40G))
  1746. linkattr |= FW_PORT_CAP32_SPEED_40G;
  1747. if (lstatus & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_100G))
  1748. linkattr |= FW_PORT_CAP32_SPEED_100G;
  1749. break;
  1750. }
  1751. case FW_PORT_ACTION_GET_PORT_INFO32: {
  1752. u32 lstatus32;
  1753. lstatus32 = be32_to_cpu(cmd->u.info32.lstatus32_to_cbllen32);
  1754. link_ok = (lstatus32 & FW_PORT_CMD_LSTATUS32_F) != 0;
  1755. linkdnrc = FW_PORT_CMD_LINKDNRC32_G(lstatus32);
  1756. port_type = FW_PORT_CMD_PORTTYPE32_G(lstatus32);
  1757. mod_type = FW_PORT_CMD_MODTYPE32_G(lstatus32);
  1758. pcaps = be32_to_cpu(cmd->u.info32.pcaps32);
  1759. acaps = be32_to_cpu(cmd->u.info32.acaps32);
  1760. lpacaps = be32_to_cpu(cmd->u.info32.lpacaps32);
  1761. linkattr = be32_to_cpu(cmd->u.info32.linkattr32);
  1762. break;
  1763. }
  1764. default:
  1765. dev_err(adapter->pdev_dev, "Handle Port Information: Bad Command/Action %#x\n",
  1766. be32_to_cpu(cmd->action_to_len16));
  1767. return;
  1768. }
  1769. fec = fwcap_to_cc_fec(acaps);
  1770. fc = fwcap_to_cc_pause(linkattr);
  1771. speed = fwcap_to_speed(linkattr);
  1772. if (mod_type != pi->mod_type) {
  1773. /* When a new Transceiver Module is inserted, the Firmware
  1774. * will examine any Forward Error Correction parameters
  1775. * present in the Transceiver Module i2c EPROM and determine
  1776. * the supported and recommended FEC settings from those
  1777. * based on IEEE 802.3 standards. We always record the
  1778. * IEEE 802.3 recommended "automatic" settings.
  1779. */
  1780. lc->auto_fec = fec;
  1781. /* Some versions of the early T6 Firmware "cheated" when
  1782. * handling different Transceiver Modules by changing the
  1783. * underlaying Port Type reported to the Host Drivers. As
  1784. * such we need to capture whatever Port Type the Firmware
  1785. * sends us and record it in case it's different from what we
  1786. * were told earlier. Unfortunately, since Firmware is
  1787. * forever, we'll need to keep this code here forever, but in
  1788. * later T6 Firmware it should just be an assignment of the
  1789. * same value already recorded.
  1790. */
  1791. pi->port_type = port_type;
  1792. pi->mod_type = mod_type;
  1793. t4vf_os_portmod_changed(adapter, pi->pidx);
  1794. }
  1795. if (link_ok != lc->link_ok || speed != lc->speed ||
  1796. fc != lc->fc || fec != lc->fec) { /* something changed */
  1797. if (!link_ok && lc->link_ok) {
  1798. lc->link_down_rc = linkdnrc;
  1799. dev_warn(adapter->pdev_dev, "Port %d link down, reason: %s\n",
  1800. pi->port_id, t4vf_link_down_rc_str(linkdnrc));
  1801. }
  1802. lc->link_ok = link_ok;
  1803. lc->speed = speed;
  1804. lc->fc = fc;
  1805. lc->fec = fec;
  1806. lc->pcaps = pcaps;
  1807. lc->lpacaps = lpacaps;
  1808. lc->acaps = acaps & ADVERT_MASK;
  1809. /* If we're not physically capable of Auto-Negotiation, note
  1810. * this as Auto-Negotiation disabled. Otherwise, we track
  1811. * what Auto-Negotiation settings we have. Note parallel
  1812. * structure in init_link_config().
  1813. */
  1814. if (!(lc->pcaps & FW_PORT_CAP32_ANEG)) {
  1815. lc->autoneg = AUTONEG_DISABLE;
  1816. } else if (lc->acaps & FW_PORT_CAP32_ANEG) {
  1817. lc->autoneg = AUTONEG_ENABLE;
  1818. } else {
  1819. /* When Autoneg is disabled, user needs to set
  1820. * single speed.
  1821. * Similar to cxgb4_ethtool.c: set_link_ksettings
  1822. */
  1823. lc->acaps = 0;
  1824. lc->speed_caps = fwcap_to_speed(acaps);
  1825. lc->autoneg = AUTONEG_DISABLE;
  1826. }
  1827. t4vf_os_link_changed(adapter, pi->pidx, link_ok);
  1828. }
  1829. }
  1830. /**
  1831. * t4vf_update_port_info - retrieve and update port information if changed
  1832. * @pi: the port_info
  1833. *
  1834. * We issue a Get Port Information Command to the Firmware and, if
  1835. * successful, we check to see if anything is different from what we
  1836. * last recorded and update things accordingly.
  1837. */
  1838. int t4vf_update_port_info(struct port_info *pi)
  1839. {
  1840. unsigned int fw_caps = pi->adapter->params.fw_caps_support;
  1841. struct fw_port_cmd port_cmd;
  1842. int ret;
  1843. memset(&port_cmd, 0, sizeof(port_cmd));
  1844. port_cmd.op_to_portid = cpu_to_be32(FW_CMD_OP_V(FW_PORT_CMD) |
  1845. FW_CMD_REQUEST_F | FW_CMD_READ_F |
  1846. FW_PORT_CMD_PORTID_V(pi->port_id));
  1847. port_cmd.action_to_len16 = cpu_to_be32(
  1848. FW_PORT_CMD_ACTION_V(fw_caps == FW_CAPS16
  1849. ? FW_PORT_ACTION_GET_PORT_INFO
  1850. : FW_PORT_ACTION_GET_PORT_INFO32) |
  1851. FW_LEN16(port_cmd));
  1852. ret = t4vf_wr_mbox(pi->adapter, &port_cmd, sizeof(port_cmd),
  1853. &port_cmd);
  1854. if (ret)
  1855. return ret;
  1856. t4vf_handle_get_port_info(pi, &port_cmd);
  1857. return 0;
  1858. }
  1859. /**
  1860. * t4vf_handle_fw_rpl - process a firmware reply message
  1861. * @adapter: the adapter
  1862. * @rpl: start of the firmware message
  1863. *
  1864. * Processes a firmware message, such as link state change messages.
  1865. */
  1866. int t4vf_handle_fw_rpl(struct adapter *adapter, const __be64 *rpl)
  1867. {
  1868. const struct fw_cmd_hdr *cmd_hdr = (const struct fw_cmd_hdr *)rpl;
  1869. u8 opcode = FW_CMD_OP_G(be32_to_cpu(cmd_hdr->hi));
  1870. switch (opcode) {
  1871. case FW_PORT_CMD: {
  1872. /*
  1873. * Link/module state change message.
  1874. */
  1875. const struct fw_port_cmd *port_cmd =
  1876. (const struct fw_port_cmd *)rpl;
  1877. int action = FW_PORT_CMD_ACTION_G(
  1878. be32_to_cpu(port_cmd->action_to_len16));
  1879. int port_id, pidx;
  1880. if (action != FW_PORT_ACTION_GET_PORT_INFO &&
  1881. action != FW_PORT_ACTION_GET_PORT_INFO32) {
  1882. dev_err(adapter->pdev_dev,
  1883. "Unknown firmware PORT reply action %x\n",
  1884. action);
  1885. break;
  1886. }
  1887. port_id = FW_PORT_CMD_PORTID_G(
  1888. be32_to_cpu(port_cmd->op_to_portid));
  1889. for_each_port(adapter, pidx) {
  1890. struct port_info *pi = adap2pinfo(adapter, pidx);
  1891. if (pi->port_id != port_id)
  1892. continue;
  1893. t4vf_handle_get_port_info(pi, port_cmd);
  1894. }
  1895. break;
  1896. }
  1897. default:
  1898. dev_err(adapter->pdev_dev, "Unknown firmware reply %X\n",
  1899. opcode);
  1900. }
  1901. return 0;
  1902. }
  1903. /**
  1904. */
  1905. int t4vf_prep_adapter(struct adapter *adapter)
  1906. {
  1907. int err;
  1908. unsigned int chipid;
  1909. /* Wait for the device to become ready before proceeding ...
  1910. */
  1911. err = t4vf_wait_dev_ready(adapter);
  1912. if (err)
  1913. return err;
  1914. /* Default port and clock for debugging in case we can't reach
  1915. * firmware.
  1916. */
  1917. adapter->params.nports = 1;
  1918. adapter->params.vfres.pmask = 1;
  1919. adapter->params.vpd.cclk = 50000;
  1920. adapter->params.chip = 0;
  1921. switch (CHELSIO_PCI_ID_VER(adapter->pdev->device)) {
  1922. case CHELSIO_T4:
  1923. adapter->params.chip |= CHELSIO_CHIP_CODE(CHELSIO_T4, 0);
  1924. adapter->params.arch.sge_fl_db = DBPRIO_F;
  1925. adapter->params.arch.mps_tcam_size =
  1926. NUM_MPS_CLS_SRAM_L_INSTANCES;
  1927. break;
  1928. case CHELSIO_T5:
  1929. chipid = REV_G(t4_read_reg(adapter, PL_VF_REV_A));
  1930. adapter->params.chip |= CHELSIO_CHIP_CODE(CHELSIO_T5, chipid);
  1931. adapter->params.arch.sge_fl_db = DBPRIO_F | DBTYPE_F;
  1932. adapter->params.arch.mps_tcam_size =
  1933. NUM_MPS_T5_CLS_SRAM_L_INSTANCES;
  1934. break;
  1935. case CHELSIO_T6:
  1936. chipid = REV_G(t4_read_reg(adapter, PL_VF_REV_A));
  1937. adapter->params.chip |= CHELSIO_CHIP_CODE(CHELSIO_T6, chipid);
  1938. adapter->params.arch.sge_fl_db = 0;
  1939. adapter->params.arch.mps_tcam_size =
  1940. NUM_MPS_T5_CLS_SRAM_L_INSTANCES;
  1941. break;
  1942. }
  1943. return 0;
  1944. }
  1945. /**
  1946. * t4vf_get_vf_mac_acl - Get the MAC address to be set to
  1947. * the VI of this VF.
  1948. * @adapter: The adapter
  1949. * @pf: The pf associated with vf
  1950. * @naddr: the number of ACL MAC addresses returned in addr
  1951. * @addr: Placeholder for MAC addresses
  1952. *
  1953. * Find the MAC address to be set to the VF's VI. The requested MAC address
  1954. * is from the host OS via callback in the PF driver.
  1955. */
  1956. int t4vf_get_vf_mac_acl(struct adapter *adapter, unsigned int pf,
  1957. unsigned int *naddr, u8 *addr)
  1958. {
  1959. struct fw_acl_mac_cmd cmd;
  1960. int ret;
  1961. memset(&cmd, 0, sizeof(cmd));
  1962. cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_ACL_MAC_CMD) |
  1963. FW_CMD_REQUEST_F |
  1964. FW_CMD_READ_F);
  1965. cmd.en_to_len16 = cpu_to_be32((unsigned int)FW_LEN16(cmd));
  1966. ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &cmd);
  1967. if (ret)
  1968. return ret;
  1969. if (cmd.nmac < *naddr)
  1970. *naddr = cmd.nmac;
  1971. switch (pf) {
  1972. case 3:
  1973. memcpy(addr, cmd.macaddr3, sizeof(cmd.macaddr3));
  1974. break;
  1975. case 2:
  1976. memcpy(addr, cmd.macaddr2, sizeof(cmd.macaddr2));
  1977. break;
  1978. case 1:
  1979. memcpy(addr, cmd.macaddr1, sizeof(cmd.macaddr1));
  1980. break;
  1981. case 0:
  1982. memcpy(addr, cmd.macaddr0, sizeof(cmd.macaddr0));
  1983. break;
  1984. }
  1985. return ret;
  1986. }
  1987. /**
  1988. * t4vf_get_vf_vlan_acl - Get the VLAN ID to be set to
  1989. * the VI of this VF.
  1990. * @adapter: The adapter
  1991. *
  1992. * Find the VLAN ID to be set to the VF's VI. The requested VLAN ID
  1993. * is from the host OS via callback in the PF driver.
  1994. */
  1995. int t4vf_get_vf_vlan_acl(struct adapter *adapter)
  1996. {
  1997. struct fw_acl_vlan_cmd cmd;
  1998. int vlan = 0;
  1999. int ret = 0;
  2000. cmd.op_to_vfn = htonl(FW_CMD_OP_V(FW_ACL_VLAN_CMD) |
  2001. FW_CMD_REQUEST_F | FW_CMD_READ_F);
  2002. /* Note: Do not enable the ACL */
  2003. cmd.en_to_len16 = cpu_to_be32((unsigned int)FW_LEN16(cmd));
  2004. ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &cmd);
  2005. if (!ret)
  2006. vlan = be16_to_cpu(cmd.vlanid[0]);
  2007. return vlan;
  2008. }