You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mtnic.c 40KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758
  1. /*
  2. * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <stdio.h>
  34. #include <strings.h>
  35. #include <errno.h>
  36. #include <gpxe/malloc.h>
  37. #include <gpxe/umalloc.h>
  38. #include <bits/byteswap.h>
  39. #include <little_bswap.h>
  40. #include <timer.h>
  41. #include <gpxe/pci.h>
  42. #include <gpxe/ethernet.h>
  43. #include <gpxe/netdevice.h>
  44. #include <gpxe/iobuf.h>
  45. #include "mtnic.h"
  46. /*
  47. mtnic.c - gPXE driver for Mellanox 10Gig ConnectX EN
  48. */
  49. /** Set port number to use
  50. *
  51. * 0 - port 1
  52. * 1 - port 2
  53. */
  54. #define MTNIC_PORT_NUM 0
  55. /* Note: for verbose printing do Make ... DEBUG=mtnic */
  56. /********************************************************************
  57. *
  58. * MTNIC allocation functions
  59. *
  60. *********************************************************************/
  61. /**
  62. * mtnic_alloc_aligned
  63. *
  64. * @v unsigned int size size
  65. * @v void **va virtual address
  66. * @v u32 *pa physical address
  67. * @v u32 aligment aligment
  68. *
  69. * Function allocate aligned buffer and put it's virtual address in 'va'
  70. * and it's physical aligned address in 'pa'
  71. */
  72. static int
  73. mtnic_alloc_aligned(unsigned int size, void **va, u32 *pa, unsigned int alignment)
  74. {
  75. *va = alloc_memblock(size, alignment);
  76. if (!*va) {
  77. return MTNIC_ERROR;
  78. }
  79. *pa = (u32)virt_to_bus(*va);
  80. return 0;
  81. }
  82. /**
  83. *
  84. * mtnic alloc command interface
  85. *
  86. */
  87. static int
  88. mtnic_alloc_cmdif(struct mtnic_priv *priv)
  89. {
  90. u32 bar = mtnic_pci_dev.dev.bar[0];
  91. priv->hcr = ioremap(bar + MTNIC_HCR_BASE, MTNIC_HCR_SIZE);
  92. if (!priv->hcr) {
  93. eprintf("Couldn't map command register.");
  94. return MTNIC_ERROR;
  95. }
  96. mtnic_alloc_aligned(PAGE_SIZE, (void *)&priv->cmd.buf, &priv->cmd.mapping, PAGE_SIZE);
  97. if (!priv->cmd.buf) {
  98. eprintf("Error in allocating buffer for command interface\n");
  99. return MTNIC_ERROR;
  100. }
  101. return 0;
  102. }
  103. /**
  104. * Free RX io buffers
  105. */
  106. static void
  107. mtnic_free_io_buffers(struct mtnic_ring *ring)
  108. {
  109. int index;
  110. for (; ring->cons <= ring->prod; ++ring->cons) {
  111. index = ring->cons & ring->size_mask;
  112. if (ring->iobuf[index])
  113. free_iob(ring->iobuf[index]);
  114. }
  115. }
  116. /**
  117. *
  118. * mtnic alloc and attach io buffers
  119. *
  120. */
  121. static int
  122. mtnic_alloc_iobuf(struct mtnic_priv *priv, struct mtnic_ring *ring,
  123. unsigned int size)
  124. {
  125. struct mtnic_rx_desc *rx_desc_ptr = ring->buf;
  126. u32 index;
  127. while ((u32)(ring->prod - ring->cons) < UNITS_BUFFER_SIZE) {
  128. index = ring->prod & ring->size_mask;
  129. ring->iobuf[index] = alloc_iob(size);
  130. if (!&ring->iobuf[index]) {
  131. if (ring->prod <= (ring->cons + 1)) {
  132. eprintf("Error allocating Rx io "
  133. "buffer number %lx", index);
  134. /* In case of error freeing io buffer */
  135. mtnic_free_io_buffers(ring);
  136. return MTNIC_ERROR;
  137. }
  138. break;
  139. }
  140. /* Attach io_buffer to descriptor */
  141. rx_desc_ptr = ring->buf +
  142. (sizeof(struct mtnic_rx_desc) * index);
  143. rx_desc_ptr->data.count = cpu_to_be32(size);
  144. rx_desc_ptr->data.mem_type = priv->fw.mem_type_snoop_be;
  145. rx_desc_ptr->data.addr_l = cpu_to_be32(
  146. virt_to_bus(ring->iobuf[index]->data));
  147. ++ ring->prod;
  148. }
  149. /* Update RX producer index (PI) */
  150. ring->db->count = cpu_to_be32(ring->prod & 0xffff);
  151. return 0;
  152. }
  153. /**
  154. * mtnic alloc ring
  155. *
  156. * Alloc and configure TX or RX ring
  157. *
  158. */
  159. static int
  160. mtnic_alloc_ring(struct mtnic_priv *priv, struct mtnic_ring *ring,
  161. u32 size, u16 stride, u16 cq, u8 is_rx)
  162. {
  163. unsigned int i;
  164. int err;
  165. struct mtnic_rx_desc *rx_desc;
  166. struct mtnic_tx_desc *tx_desc;
  167. ring->size = size; /* Number of descriptors */
  168. ring->size_mask = size - 1;
  169. ring->stride = stride; /* Size of each entry */
  170. ring->cq = cq; /* CQ number associated with this ring */
  171. ring->cons = 0;
  172. ring->prod = 0;
  173. /* Alloc descriptors buffer */
  174. ring->buf_size = ring->size * ((is_rx) ? sizeof(struct mtnic_rx_desc) :
  175. sizeof(struct mtnic_tx_desc));
  176. err = mtnic_alloc_aligned(ring->buf_size, (void *)&ring->buf,
  177. &ring->dma, PAGE_SIZE);
  178. if (err) {
  179. eprintf("Failed allocating descriptor ring sizeof %lx\n",
  180. ring->buf_size);
  181. return MTNIC_ERROR;
  182. }
  183. memset(ring->buf, 0, ring->buf_size);
  184. DBG("Allocated %s ring (addr:%p) - buf:%p size:%lx"
  185. "buf_size:%lx dma:%lx\n",
  186. is_rx ? "Rx" : "Tx", ring, ring->buf, ring->size,
  187. ring->buf_size, ring->dma);
  188. if (is_rx) { /* RX ring */
  189. /* Alloc doorbell */
  190. err = mtnic_alloc_aligned(sizeof(struct mtnic_cq_db_record),
  191. (void *)&ring->db, &ring->db_dma, 32);
  192. if (err) {
  193. eprintf("Failed allocating Rx ring doorbell record\n");
  194. free(ring->buf);
  195. return MTNIC_ERROR;
  196. }
  197. /* ==- Configure Descriptor -== */
  198. /* Init ctrl seg of rx desc */
  199. for (i = 0; i < UNITS_BUFFER_SIZE; ++i) {
  200. rx_desc = ring->buf +
  201. (sizeof(struct mtnic_rx_desc) * i);
  202. /* Pre-link descriptor */
  203. rx_desc->next = cpu_to_be16(i + 1);
  204. }
  205. /*The last ctrl descriptor is '0' and points to the first one*/
  206. /* Alloc IO_BUFFERS */
  207. err = mtnic_alloc_iobuf(priv, ring, DEF_IOBUF_SIZE);
  208. if (err) {
  209. eprintf("ERROR Allocating io buffer");
  210. free(ring->buf);
  211. return MTNIC_ERROR;
  212. }
  213. } else { /* TX ring */
  214. /* Set initial ownership of all Tx Desc' to SW (1) */
  215. for (i = 0; i < ring->size; i++) {
  216. tx_desc = ring->buf + ring->stride * i;
  217. tx_desc->ctrl.op_own = cpu_to_be32(MTNIC_BIT_DESC_OWN);
  218. }
  219. /* DB */
  220. ring->db_offset = cpu_to_be32(
  221. ((u32) priv->fw.tx_offset[priv->port]) << 8);
  222. /* Map Tx+CQ doorbells */
  223. DBG("Mapping TxCQ doorbell at offset:0x%lx\n",
  224. priv->fw.txcq_db_offset);
  225. ring->txcq_db = ioremap(mtnic_pci_dev.dev.bar[2] +
  226. priv->fw.txcq_db_offset, PAGE_SIZE);
  227. if (!ring->txcq_db) {
  228. eprintf("Couldn't map txcq doorbell, aborting...\n");
  229. free(ring->buf);
  230. return MTNIC_ERROR;
  231. }
  232. }
  233. return 0;
  234. }
  235. /**
  236. * mtnic alloc CQ
  237. *
  238. * Alloc and configure CQ.
  239. *
  240. */
  241. static int
  242. mtnic_alloc_cq(struct net_device *dev, int num, struct mtnic_cq *cq,
  243. u8 is_rx, u32 size, u32 offset_ind)
  244. {
  245. int err ;
  246. unsigned int i;
  247. cq->num = num;
  248. cq->dev = dev;
  249. cq->size = size;
  250. cq->last = 0;
  251. cq->is_rx = is_rx;
  252. cq->offset_ind = offset_ind;
  253. /* Alloc doorbell */
  254. err = mtnic_alloc_aligned(sizeof(struct mtnic_cq_db_record),
  255. (void *)&cq->db, &cq->db_dma, 32);
  256. if (err) {
  257. eprintf("Failed allocating CQ doorbell record\n");
  258. return MTNIC_ERROR;
  259. }
  260. memset(cq->db, 0, sizeof(struct mtnic_cq_db_record));
  261. /* Alloc CQEs buffer */
  262. cq->buf_size = size * sizeof(struct mtnic_cqe);
  263. err = mtnic_alloc_aligned(cq->buf_size,
  264. (void *)&cq->buf, &cq->dma, PAGE_SIZE);
  265. if (err) {
  266. eprintf("Failed allocating CQ buffer\n");
  267. free(cq->db);
  268. return MTNIC_ERROR;
  269. }
  270. memset(cq->buf, 0, cq->buf_size);
  271. DBG("Allocated CQ (addr:%p) - size:%lx buf:%p buf_size:%lx "
  272. "dma:%lx db:%p db_dma:%lx\n"
  273. "cqn offset:%lx \n", cq, cq->size, cq->buf,
  274. cq->buf_size, cq->dma, cq->db,
  275. cq->db_dma, offset_ind);
  276. /* Set ownership of all CQEs to HW */
  277. DBG("Setting HW ownership for CQ:%d\n", num);
  278. for (i = 0; i < cq->size; i++) {
  279. /* Initial HW ownership is 1 */
  280. cq->buf[i].op_tr_own = MTNIC_BIT_CQ_OWN;
  281. }
  282. return 0;
  283. }
  284. /**
  285. * mtnic_alloc_resources
  286. *
  287. * Alloc and configure CQs, Tx, Rx
  288. */
  289. unsigned int
  290. mtnic_alloc_resources(struct net_device *dev)
  291. {
  292. struct mtnic_priv *priv = netdev_priv(dev);
  293. int err;
  294. int cq_ind = 0;
  295. int cq_offset = priv->fw.cq_offset;
  296. /* Alloc 1st CQ */
  297. err = mtnic_alloc_cq(dev, cq_ind, &priv->cq[cq_ind], 1 /* RX */,
  298. UNITS_BUFFER_SIZE, cq_offset + cq_ind);
  299. if (err) {
  300. eprintf("Failed allocating Rx CQ\n");
  301. return MTNIC_ERROR;
  302. }
  303. /* Alloc RX */
  304. err = mtnic_alloc_ring(priv, &priv->rx_ring, UNITS_BUFFER_SIZE,
  305. sizeof(struct mtnic_rx_desc), cq_ind, /* RX */1);
  306. if (err) {
  307. eprintf("Failed allocating Rx Ring\n");
  308. goto cq0_error;
  309. }
  310. ++cq_ind;
  311. /* alloc 2nd CQ */
  312. err = mtnic_alloc_cq(dev, cq_ind, &priv->cq[cq_ind], 0 /* TX */,
  313. UNITS_BUFFER_SIZE, cq_offset + cq_ind);
  314. if (err) {
  315. eprintf("Failed allocating Tx CQ\n");
  316. goto rx_error;
  317. }
  318. /* Alloc TX */
  319. err = mtnic_alloc_ring(priv, &priv->tx_ring, UNITS_BUFFER_SIZE,
  320. sizeof(struct mtnic_tx_desc), cq_ind, /* TX */ 0);
  321. if (err) {
  322. eprintf("Failed allocating Tx ring\n");
  323. goto cq1_error;
  324. }
  325. return 0;
  326. cq1_error:
  327. free(priv->cq[1].buf);
  328. free(priv->cq[1].db);
  329. rx_error:
  330. free(priv->rx_ring.buf);
  331. free(priv->rx_ring.db);
  332. mtnic_free_io_buffers(&priv->rx_ring);
  333. cq0_error:
  334. free(priv->cq[0].buf);
  335. free(priv->cq[0].db);
  336. return MTNIC_ERROR;
  337. }
  338. /**
  339. * mtnic alloc_eq
  340. *
  341. * Note: EQ is not used by the driver but must be allocated
  342. */
  343. static int
  344. mtnic_alloc_eq(struct mtnic_priv *priv)
  345. {
  346. int err;
  347. unsigned int i;
  348. struct mtnic_eqe *eqe_desc = NULL;
  349. /* Allocating doorbell */
  350. priv->eq_db = ioremap(mtnic_pci_dev.dev.bar[2] +
  351. priv->fw.eq_db_offset, sizeof(u32));
  352. if (!priv->eq_db) {
  353. eprintf("Couldn't map EQ doorbell, aborting...\n");
  354. return MTNIC_ERROR;
  355. }
  356. /* Allocating buffer */
  357. priv->eq.size = NUM_EQES;
  358. priv->eq.buf_size = priv->eq.size * sizeof(struct mtnic_eqe);
  359. err = mtnic_alloc_aligned(priv->eq.buf_size, (void *)&priv->eq.buf,
  360. &priv->eq.dma, PAGE_SIZE);
  361. if (err) {
  362. eprintf("Failed allocating EQ buffer\n");
  363. iounmap(priv->eq_db);
  364. return MTNIC_ERROR;
  365. }
  366. memset(priv->eq.buf, 0, priv->eq.buf_size);
  367. for (i = 0; i < priv->eq.size; i++)
  368. eqe_desc = priv->eq.buf + (sizeof(struct mtnic_eqe) * i);
  369. eqe_desc->own |= MTNIC_BIT_EQE_OWN;
  370. mdelay(20);
  371. return 0;
  372. }
  373. /********************************************************************
  374. *
  375. * Mtnic commands functions
  376. * -=-=-=-=-=-=-=-=-=-=-=-=
  377. *
  378. *
  379. *
  380. *********************************************************************/
  381. static inline int
  382. cmdif_go_bit(struct mtnic_priv *priv)
  383. {
  384. struct mtnic_if_cmd_reg *hcr = priv->hcr;
  385. u32 status;
  386. int i;
  387. for (i = 0; i < TBIT_RETRIES; i++) {
  388. status = be32_to_cpu(readl(&hcr->status_go_opcode));
  389. if ((status & MTNIC_BC_MASK(MTNIC_MASK_CMD_REG_T_BIT)) ==
  390. (priv->cmd.tbit << MTNIC_BC_OFF(MTNIC_MASK_CMD_REG_T_BIT))) {
  391. /* Read expected t-bit - now return go-bit value */
  392. return status & MTNIC_BC_MASK(MTNIC_MASK_CMD_REG_GO_BIT);
  393. }
  394. }
  395. eprintf("Invalid tbit after %d retries!\n", TBIT_RETRIES);
  396. return 1; /* Return busy... */
  397. }
  398. /* Base Command interface */
  399. static int
  400. mtnic_cmd(struct mtnic_priv *priv, void *in_imm,
  401. void *out_imm, u32 in_modifier, u16 op)
  402. {
  403. struct mtnic_if_cmd_reg *hcr = priv->hcr;
  404. int err = 0;
  405. u32 out_param_h = 0;
  406. u32 out_param_l = 0;
  407. u32 in_param_h = 0;
  408. u32 in_param_l = 0;
  409. static u16 token = 0x8000;
  410. u32 status;
  411. unsigned int timeout = 0;
  412. token++;
  413. if (cmdif_go_bit(priv)) {
  414. eprintf("GO BIT BUSY:%p.\n", hcr + 6);
  415. err = MTNIC_ERROR;
  416. goto out;
  417. }
  418. if (in_imm) {
  419. in_param_h = *((u32*)in_imm);
  420. in_param_l = *((u32*)in_imm + 1);
  421. } else {
  422. in_param_l = cpu_to_be32(priv->cmd.mapping);
  423. }
  424. out_param_l = cpu_to_be32(priv->cmd.mapping);
  425. /* writing to MCR */
  426. writel(in_param_h, &hcr->in_param_h);
  427. writel(in_param_l, &hcr->in_param_l);
  428. writel((u32) cpu_to_be32(in_modifier), &hcr->input_modifier);
  429. writel(out_param_h, &hcr->out_param_h);
  430. writel(out_param_l, &hcr->out_param_l);
  431. writel((u32)cpu_to_be32(token << 16), &hcr->token);
  432. wmb();
  433. /* flip toggle bit before each write to the HCR */
  434. priv->cmd.tbit = !priv->cmd.tbit;
  435. writel((u32)
  436. cpu_to_be32(MTNIC_BC_MASK(MTNIC_MASK_CMD_REG_GO_BIT) |
  437. (priv->cmd.tbit << MTNIC_BC_OFF(MTNIC_MASK_CMD_REG_T_BIT)) | op),
  438. &hcr->status_go_opcode);
  439. while (cmdif_go_bit(priv) && (timeout <= GO_BIT_TIMEOUT)) {
  440. mdelay(1);
  441. ++timeout;
  442. }
  443. if (cmdif_go_bit(priv)) {
  444. eprintf("Command opcode:0x%x token:0x%x TIMEOUT.\n", op, token);
  445. err = MTNIC_ERROR;
  446. goto out;
  447. }
  448. if (out_imm) {
  449. *((u32 *)out_imm) = readl(&hcr->out_param_h);
  450. *((u32 *)out_imm + 1) = readl(&hcr->out_param_l);
  451. }
  452. status = be32_to_cpu((u32)readl(&hcr->status_go_opcode)) >> 24;
  453. /*DBG("Command opcode:0x%x token:0x%x returned:0x%lx\n",
  454. op, token, status);*/
  455. if (status) {
  456. return status;
  457. }
  458. out:
  459. return err;
  460. }
  461. /* MAP PAGES wrapper */
  462. static int
  463. mtnic_map_cmd(struct mtnic_priv *priv, u16 op, struct mtnic_pages pages)
  464. {
  465. unsigned int j;
  466. u32 addr;
  467. unsigned int len;
  468. u32 *page_arr = priv->cmd.buf;
  469. int nent = 0;
  470. int err = 0;
  471. memset(page_arr, 0, PAGE_SIZE);
  472. len = PAGE_SIZE * pages.num;
  473. pages.buf = (u32 *)umalloc(PAGE_SIZE * (pages.num + 1));
  474. addr = PAGE_SIZE + ((virt_to_bus(pages.buf) & 0xfffff000) + PAGE_SIZE);
  475. DBG("Mapping pages: size: %lx address: %p\n", pages.num, pages.buf);
  476. if (addr & (PAGE_MASK)) {
  477. eprintf("Got FW area not aligned to %d (%llx/%x)\n",
  478. PAGE_SIZE, (u64) addr, len);
  479. return MTNIC_ERROR;
  480. }
  481. /* Function maps each PAGE seperately */
  482. for (j = 0; j < len; j+= PAGE_SIZE) {
  483. page_arr[nent * 4 + 3] = cpu_to_be32(addr + j);
  484. if (++nent == MTNIC_MAILBOX_SIZE / 16) {
  485. err = mtnic_cmd(priv, NULL, NULL, nent, op);
  486. if (err)
  487. return MTNIC_ERROR;
  488. nent = 0;
  489. }
  490. }
  491. if (nent)
  492. err = mtnic_cmd(priv, NULL, NULL, nent, op);
  493. return err;
  494. }
  495. /*
  496. * Query FW
  497. */
  498. static int
  499. mtnic_QUERY_FW(struct mtnic_priv *priv)
  500. {
  501. int err;
  502. struct mtnic_if_query_fw_out_mbox *cmd = priv->cmd.buf;
  503. err = mtnic_cmd(priv, NULL, NULL, 0, MTNIC_IF_CMD_QUERY_FW);
  504. if (err)
  505. return MTNIC_ERROR;
  506. /* Get FW and interface versions */
  507. priv->fw_ver = ((u64) be16_to_cpu(cmd->rev_maj) << 32) |
  508. ((u64) be16_to_cpu(cmd->rev_min) << 16) |
  509. (u64) be16_to_cpu(cmd->rev_smin);
  510. priv->fw.ifc_rev = be16_to_cpu(cmd->ifc_rev);
  511. /* Get offset for internal error reports (debug) */
  512. priv->fw.err_buf.offset = be64_to_cpu(cmd->err_buf_start);
  513. priv->fw.err_buf.size = be32_to_cpu(cmd->err_buf_size);
  514. DBG("Error buf offset is %llx\n", priv->fw.err_buf.offset);
  515. /* Get number of required FW (4k) pages */
  516. priv->fw.fw_pages.num = be16_to_cpu(cmd->fw_pages);
  517. return 0;
  518. }
  519. static int
  520. mtnic_OPEN_NIC(struct mtnic_priv *priv)
  521. {
  522. struct mtnic_if_open_nic_in_mbox *open_nic = priv->cmd.buf;
  523. u32 extra_pages[2] = {0};
  524. int err;
  525. memset(open_nic, 0, sizeof *open_nic);
  526. /* port 1 */
  527. open_nic->log_rx_p1 = 0;
  528. open_nic->log_cq_p1 = 1;
  529. open_nic->log_tx_p1 = 0;
  530. open_nic->steer_p1 = MTNIC_IF_STEER_RSS;
  531. /* MAC + VLAN - leave reserved */
  532. /* port 2 */
  533. open_nic->log_rx_p2 = 0;
  534. open_nic->log_cq_p2 = 1;
  535. open_nic->log_tx_p2 = 0;
  536. open_nic->steer_p2 = MTNIC_IF_STEER_RSS;
  537. /* MAC + VLAN - leave reserved */
  538. err = mtnic_cmd(priv, NULL, extra_pages, 0, MTNIC_IF_CMD_OPEN_NIC);
  539. priv->fw.extra_pages.num = be32_to_cpu(*(extra_pages+1));
  540. DBG("Extra pages num is %lx\n", priv->fw.extra_pages.num);
  541. return err;
  542. }
  543. static int
  544. mtnic_CONFIG_RX(struct mtnic_priv *priv)
  545. {
  546. struct mtnic_if_config_rx_in_imm config_rx;
  547. memset(&config_rx, 0, sizeof config_rx);
  548. return mtnic_cmd(priv, &config_rx, NULL, 0, MTNIC_IF_CMD_CONFIG_RX);
  549. }
  550. static int
  551. mtnic_CONFIG_TX(struct mtnic_priv *priv)
  552. {
  553. struct mtnic_if_config_send_in_imm config_tx;
  554. config_tx.enph_gpf = 0;
  555. return mtnic_cmd(priv, &config_tx, NULL, 0, MTNIC_IF_CMD_CONFIG_TX);
  556. }
  557. static int
  558. mtnic_HEART_BEAT(struct mtnic_priv *priv, u32 *link_state)
  559. {
  560. struct mtnic_if_heart_beat_out_imm heart_beat;
  561. int err;
  562. u32 flags;
  563. err = mtnic_cmd(priv, NULL, &heart_beat, 0, MTNIC_IF_CMD_HEART_BEAT);
  564. if (!err) {
  565. flags = be32_to_cpu(heart_beat.flags);
  566. if (flags & MTNIC_BC_MASK(MTNIC_MASK_HEAR_BEAT_INT_ERROR)) {
  567. eprintf("Internal error detected\n");
  568. return MTNIC_ERROR;
  569. }
  570. *link_state = flags &
  571. ~((u32) MTNIC_BC_MASK(MTNIC_MASK_HEAR_BEAT_INT_ERROR));
  572. }
  573. return err;
  574. }
  575. /*
  576. * Port commands
  577. */
  578. static int
  579. mtnic_SET_PORT_DEFAULT_RING(struct mtnic_priv *priv, u8 port, u16 ring)
  580. {
  581. struct mtnic_if_set_port_default_ring_in_imm def_ring;
  582. memset(&def_ring, 0, sizeof(def_ring));
  583. def_ring.ring = ring;
  584. return mtnic_cmd(priv, &def_ring, NULL, port + 1,
  585. MTNIC_IF_CMD_SET_PORT_DEFAULT_RING);
  586. }
  587. static int
  588. mtnic_CONFIG_PORT_RSS_STEER(struct mtnic_priv *priv, int port)
  589. {
  590. memset(priv->cmd.buf, 0, PAGE_SIZE);
  591. return mtnic_cmd(priv, NULL, NULL, port + 1,
  592. MTNIC_IF_CMD_CONFIG_PORT_RSS_STEER);
  593. }
  594. static int
  595. mtnic_SET_PORT_RSS_INDIRECTION(struct mtnic_priv *priv, int port)
  596. {
  597. memset(priv->cmd.buf, 0, PAGE_SIZE);
  598. return mtnic_cmd(priv, NULL, NULL, port + 1,
  599. MTNIC_IF_CMD_SET_PORT_RSS_INDIRECTION);
  600. }
  601. /*
  602. * Config commands
  603. */
  604. static int
  605. mtnic_CONFIG_CQ(struct mtnic_priv *priv, int port,
  606. u16 cq_ind, struct mtnic_cq *cq)
  607. {
  608. struct mtnic_if_config_cq_in_mbox *config_cq = priv->cmd.buf;
  609. memset(config_cq, 0, sizeof *config_cq);
  610. config_cq->cq = cq_ind;
  611. config_cq->size = fls(UNITS_BUFFER_SIZE - 1);
  612. config_cq->offset = ((cq->dma) & (PAGE_MASK)) >> 6;
  613. config_cq->db_record_addr_l = cpu_to_be32(cq->db_dma);
  614. config_cq->page_address[1] = cpu_to_be32(cq->dma);
  615. DBG("config cq address: %lx dma_address: %lx"
  616. "offset: %d size %d index: %d "
  617. , config_cq->page_address[1],cq->dma,
  618. config_cq->offset, config_cq->size, config_cq->cq );
  619. return mtnic_cmd(priv, NULL, NULL, port + 1,
  620. MTNIC_IF_CMD_CONFIG_CQ);
  621. }
  622. static int
  623. mtnic_CONFIG_TX_RING(struct mtnic_priv *priv, u8 port,
  624. u16 ring_ind, struct mtnic_ring *ring)
  625. {
  626. struct mtnic_if_config_send_ring_in_mbox *config_tx_ring = priv->cmd.buf;
  627. memset(config_tx_ring, 0, sizeof *config_tx_ring);
  628. config_tx_ring->ring = cpu_to_be16(ring_ind);
  629. config_tx_ring->size = fls(UNITS_BUFFER_SIZE - 1);
  630. config_tx_ring->cq = cpu_to_be16(ring->cq);
  631. config_tx_ring->page_address[1] = cpu_to_be32(ring->dma);
  632. return mtnic_cmd(priv, NULL, NULL, port + 1,
  633. MTNIC_IF_CMD_CONFIG_TX_RING);
  634. }
  635. static int
  636. mtnic_CONFIG_RX_RING(struct mtnic_priv *priv, u8 port,
  637. u16 ring_ind, struct mtnic_ring *ring)
  638. {
  639. struct mtnic_if_config_rx_ring_in_mbox *config_rx_ring = priv->cmd.buf;
  640. memset(config_rx_ring, 0, sizeof *config_rx_ring);
  641. config_rx_ring->ring = ring_ind;
  642. MTNIC_BC_PUT(config_rx_ring->stride_size, fls(UNITS_BUFFER_SIZE - 1),
  643. MTNIC_MASK_CONFIG_RX_RING_SIZE);
  644. MTNIC_BC_PUT(config_rx_ring->stride_size, 1,
  645. MTNIC_MASK_CONFIG_RX_RING_STRIDE);
  646. config_rx_ring->cq = cpu_to_be16(ring->cq);
  647. config_rx_ring->db_record_addr_l = cpu_to_be32(ring->db_dma);
  648. DBG("Config RX ring starting at address:%lx\n", ring->dma);
  649. config_rx_ring->page_address[1] = cpu_to_be32(ring->dma);
  650. return mtnic_cmd(priv, NULL, NULL, port + 1,
  651. MTNIC_IF_CMD_CONFIG_RX_RING);
  652. }
  653. static int
  654. mtnic_CONFIG_EQ(struct mtnic_priv *priv)
  655. {
  656. struct mtnic_if_config_eq_in_mbox *eq = priv->cmd.buf;
  657. if (priv->eq.dma & (PAGE_MASK)) {
  658. eprintf("misalligned eq buffer:%lx\n",
  659. priv->eq.dma);
  660. return MTNIC_ERROR;
  661. }
  662. memset(eq, 0, sizeof *eq);
  663. MTNIC_BC_PUT(eq->offset, priv->eq.dma >> 6, MTNIC_MASK_CONFIG_EQ_OFFSET);
  664. MTNIC_BC_PUT(eq->size, fls(priv->eq.size - 1) - 1, MTNIC_MASK_CONFIG_EQ_SIZE);
  665. MTNIC_BC_PUT(eq->int_vector, 0, MTNIC_MASK_CONFIG_EQ_INT_VEC);
  666. eq->page_address[1] = cpu_to_be32(priv->eq.dma);
  667. return mtnic_cmd(priv, NULL, NULL, 0, MTNIC_IF_CMD_CONFIG_EQ);
  668. }
  669. static int
  670. mtnic_SET_RX_RING_ADDR(struct mtnic_priv *priv, u8 port, u64* mac)
  671. {
  672. struct mtnic_if_set_rx_ring_addr_in_imm ring_addr;
  673. u32 modifier = ((u32) port + 1) << 16;
  674. memset(&ring_addr, 0, sizeof(ring_addr));
  675. ring_addr.mac_31_0 = cpu_to_be32(*mac & 0xffffffff);
  676. ring_addr.mac_47_32 = cpu_to_be16((*mac >> 32) & 0xffff);
  677. ring_addr.flags_vlan_id |= cpu_to_be16(
  678. MTNIC_BC_MASK(MTNIC_MASK_SET_RX_RING_ADDR_BY_MAC));
  679. return mtnic_cmd(priv, &ring_addr, NULL, modifier, MTNIC_IF_CMD_SET_RX_RING_ADDR);
  680. }
  681. static int
  682. mtnic_SET_PORT_STATE(struct mtnic_priv *priv, u8 port, u8 state)
  683. {
  684. struct mtnic_if_set_port_state_in_imm port_state;
  685. port_state.state = state ? cpu_to_be32(
  686. MTNIC_BC_MASK(MTNIC_MASK_CONFIG_PORT_STATE)) : 0;
  687. port_state.reserved = 0;
  688. return mtnic_cmd(priv, &port_state, NULL, port + 1,
  689. MTNIC_IF_CMD_SET_PORT_STATE);
  690. }
  691. static int
  692. mtnic_SET_PORT_MTU(struct mtnic_priv *priv, u8 port, u16 mtu)
  693. {
  694. struct mtnic_if_set_port_mtu_in_imm set_mtu;
  695. memset(&set_mtu, 0, sizeof(set_mtu));
  696. set_mtu.mtu = cpu_to_be16(mtu);
  697. return mtnic_cmd(priv, &set_mtu, NULL, port + 1,
  698. MTNIC_IF_CMD_SET_PORT_MTU);
  699. }
  700. static int
  701. mtnic_CONFIG_PORT_VLAN_FILTER(struct mtnic_priv *priv, int port)
  702. {
  703. struct mtnic_if_config_port_vlan_filter_in_mbox *vlan_filter = priv->cmd.buf;
  704. /* When no vlans are configured we disable the filter
  705. * (i.e., pass all vlans) because we ignore them anyhow */
  706. memset(vlan_filter, 0xff, sizeof(*vlan_filter));
  707. return mtnic_cmd(priv, NULL, NULL, port + 1,
  708. MTNIC_IF_CMD_CONFIG_PORT_VLAN_FILTER);
  709. }
  710. static int
  711. mtnic_RELEASE_RESOURCE(struct mtnic_priv *priv, u8 port, u8 type, u8 index)
  712. {
  713. struct mtnic_if_release_resource_in_imm rel;
  714. memset(&rel, 0, sizeof rel);
  715. rel.index = index;
  716. rel.type = type;
  717. return mtnic_cmd(priv,
  718. &rel, NULL, (type == MTNIC_IF_RESOURCE_TYPE_EQ) ?
  719. 0 : port + 1, MTNIC_IF_CMD_RELEASE_RESOURCE);
  720. }
  721. static int
  722. mtnic_QUERY_CAP(struct mtnic_priv *priv, u8 index, u8 mod, u64 *result)
  723. {
  724. struct mtnic_if_query_cap_in_imm cap;
  725. u32 out_imm[2];
  726. int err;
  727. memset(&cap, 0, sizeof cap);
  728. cap.cap_index = index;
  729. cap.cap_modifier = mod;
  730. err = mtnic_cmd(priv, &cap, &out_imm, 0, MTNIC_IF_CMD_QUERY_CAP);
  731. *((u32*)result) = be32_to_cpu(*(out_imm+1));
  732. *((u32*)result + 1) = be32_to_cpu(*out_imm);
  733. DBG("Called Query cap with index:0x%x mod:%d result:0x%llx"
  734. " error:%d\n", index, mod, *result, err);
  735. return err;
  736. }
  737. #define DO_QUERY_CAP(cap, mod, var) \
  738. err = mtnic_QUERY_CAP(priv, cap, mod, &result); \
  739. if (err) \
  740. return err; \
  741. (var) = result
  742. static int
  743. mtnic_query_cap(struct mtnic_priv *priv)
  744. {
  745. int err = 0;
  746. int i;
  747. u64 result;
  748. DO_QUERY_CAP(MTNIC_IF_CAP_NUM_PORTS, 0, priv->fw.num_ports);
  749. for (i = 0; i < priv->fw.num_ports; i++) {
  750. DO_QUERY_CAP(MTNIC_IF_CAP_DEFAULT_MAC, i + 1, priv->fw.mac[i]);
  751. }
  752. return 0;
  753. }
  754. static int
  755. mtnic_query_offsets(struct mtnic_priv *priv)
  756. {
  757. int err;
  758. int i;
  759. u64 result;
  760. DO_QUERY_CAP(MTNIC_IF_CAP_MEM_KEY,
  761. MTNIC_IF_MEM_TYPE_SNOOP,
  762. priv->fw.mem_type_snoop_be);
  763. priv->fw.mem_type_snoop_be = cpu_to_be32(priv->fw.mem_type_snoop_be);
  764. DO_QUERY_CAP(MTNIC_IF_CAP_TX_CQ_DB_OFFSET, 0, priv->fw.txcq_db_offset);
  765. DO_QUERY_CAP(MTNIC_IF_CAP_EQ_DB_OFFSET, 0, priv->fw.eq_db_offset);
  766. for (i = 0; i < priv->fw.num_ports; i++) {
  767. DO_QUERY_CAP(MTNIC_IF_CAP_CQ_OFFSET, i + 1, priv->fw.cq_offset);
  768. DO_QUERY_CAP(MTNIC_IF_CAP_TX_OFFSET, i + 1, priv->fw.tx_offset[i]);
  769. DO_QUERY_CAP(MTNIC_IF_CAP_RX_OFFSET, i + 1, priv->fw.rx_offset[i]);
  770. DBG("--> Port %d CQ offset:0x%x\n", i, priv->fw.cq_offset);
  771. DBG("--> Port %d Tx offset:0x%x\n", i, priv->fw.tx_offset[i]);
  772. DBG("--> Port %d Rx offset:0x%x\n", i, priv->fw.rx_offset[i]);
  773. }
  774. mdelay(20);
  775. return 0;
  776. }
  777. /********************************************************************
  778. *
  779. * MTNIC initalization functions
  780. *
  781. *
  782. *
  783. *
  784. *********************************************************************/
  785. /**
  786. * Reset device
  787. */
  788. void
  789. mtnic_reset(void)
  790. {
  791. void *reset = ioremap(mtnic_pci_dev.dev.bar[0] + MTNIC_RESET_OFFSET, 4);
  792. writel(cpu_to_be32(1), reset);
  793. iounmap(reset);
  794. }
  795. /**
  796. * Restore PCI config
  797. */
  798. static int
  799. restore_config(void)
  800. {
  801. int i;
  802. int rc;
  803. for (i = 0; i < 64; ++i) {
  804. if (i != 22 && i != 23) {
  805. rc = pci_write_config_dword(mtnic_pci_dev.dev.dev,
  806. i << 2,
  807. mtnic_pci_dev.dev.
  808. dev_config_space[i]);
  809. if (rc)
  810. return rc;
  811. }
  812. }
  813. return 0;
  814. }
  815. /**
  816. * Init PCI configuration
  817. */
  818. static int
  819. mtnic_init_pci(struct pci_device *dev)
  820. {
  821. int i;
  822. int err;
  823. /* save bars */
  824. DBG("bus=%d devfn=0x%x", dev->bus, dev->devfn);
  825. for (i = 0; i < 6; ++i) {
  826. mtnic_pci_dev.dev.bar[i] =
  827. pci_bar_start(dev, PCI_BASE_ADDRESS_0 + (i << 2));
  828. DBG("bar[%d]= 0x%08lx \n", i, mtnic_pci_dev.dev.bar[i]);
  829. }
  830. /* save config space */
  831. for (i = 0; i < 64; ++i) {
  832. err = pci_read_config_dword(dev, i << 2,
  833. &mtnic_pci_dev.dev.
  834. dev_config_space[i]);
  835. if (err) {
  836. eprintf("Can not save configuration space");
  837. return err;
  838. }
  839. }
  840. mtnic_pci_dev.dev.dev = dev;
  841. return 0;
  842. }
  843. /**
  844. * Initial hardware
  845. */
  846. static inline
  847. int mtnic_init_card(struct net_device *dev)
  848. {
  849. struct mtnic_priv *priv = netdev_priv(dev);
  850. int err = 0;
  851. /* Set state */
  852. priv->state = CARD_DOWN;
  853. /* Set port */
  854. priv->port = MTNIC_PORT_NUM;
  855. /* Alloc command interface */
  856. err = mtnic_alloc_cmdif(priv);
  857. if (err) {
  858. eprintf("Failed to init command interface, aborting.\n");
  859. return MTNIC_ERROR;
  860. }
  861. /**
  862. * Bring up HW
  863. */
  864. err = mtnic_QUERY_FW(priv);
  865. if (err) {
  866. eprintf("QUERY_FW command failed, aborting.\n");
  867. goto cmd_error;
  868. }
  869. DBG("Command interface revision:%d\n", priv->fw.ifc_rev);
  870. /* Allocate memory for FW and start it */
  871. err = mtnic_map_cmd(priv, MTNIC_IF_CMD_MAP_FW, priv->fw.fw_pages);
  872. if (err) {
  873. eprintf("Eror In MAP_FW\n");
  874. if (priv->fw.fw_pages.buf)
  875. free(priv->fw.fw_pages.buf);
  876. goto cmd_error;
  877. }
  878. /* Run firmware */
  879. err = mtnic_cmd(priv, NULL, NULL, 0, MTNIC_IF_CMD_RUN_FW);
  880. if (err) {
  881. eprintf("Eror In RUN FW\n");
  882. goto map_fw_error;
  883. }
  884. DBG("FW version:%d.%d.%d\n",
  885. (u16) (priv->fw_ver >> 32),
  886. (u16) ((priv->fw_ver >> 16) & 0xffff),
  887. (u16) (priv->fw_ver & 0xffff));
  888. /* Get device information */
  889. err = mtnic_query_cap(priv);
  890. if (err) {
  891. eprintf("Insufficient resources, aborting.\n");
  892. goto map_fw_error;
  893. }
  894. /* Open NIC */
  895. err = mtnic_OPEN_NIC(priv);
  896. if (err) {
  897. eprintf("Failed opening NIC, aborting.\n");
  898. goto map_fw_error;
  899. }
  900. /* Allocate and map pages worksace */
  901. err = mtnic_map_cmd(priv, MTNIC_IF_CMD_MAP_PAGES, priv->fw.extra_pages);
  902. if (err) {
  903. eprintf("Couldn't allocate %lx FW extra pages, aborting.\n",
  904. priv->fw.extra_pages.num);
  905. if (priv->fw.extra_pages.buf)
  906. free(priv->fw.extra_pages.buf);
  907. goto map_fw_error;
  908. }
  909. /* Get device offsets */
  910. err = mtnic_query_offsets(priv);
  911. if (err) {
  912. eprintf("Failed retrieving resource offests, aborting.\n");
  913. free(priv->fw.extra_pages.buf);
  914. goto map_extra_error;
  915. }
  916. /* Alloc EQ */
  917. err = mtnic_alloc_eq(priv);
  918. if (err) {
  919. eprintf("Failed init shared resources. error: %d\n", err);
  920. goto map_extra_error;
  921. }
  922. /* Configure HW */
  923. err = mtnic_CONFIG_EQ(priv);
  924. if (err) {
  925. eprintf("Failed configuring EQ\n");
  926. goto eq_error;
  927. }
  928. err = mtnic_CONFIG_RX(priv);
  929. if (err) {
  930. eprintf("Failed Rx configuration\n");
  931. goto eq_error;
  932. }
  933. err = mtnic_CONFIG_TX(priv);
  934. if (err) {
  935. eprintf("Failed Tx configuration\n");
  936. goto eq_error;
  937. }
  938. DBG("Activating port:%d\n", MTNIC_PORT_NUM + 1);
  939. priv->state = CARD_INITIALIZED;
  940. return 0;
  941. eq_error:
  942. iounmap(priv->eq_db);
  943. free(priv->eq.buf);
  944. map_extra_error:
  945. free(priv->fw.extra_pages.buf);
  946. map_fw_error:
  947. free(priv->fw.fw_pages.buf);
  948. cmd_error:
  949. iounmap(priv->hcr);
  950. free(priv->cmd.buf);
  951. free(priv);
  952. return MTNIC_ERROR;
  953. }
  954. /*******************************************************************
  955. *
  956. * Process functions
  957. *
  958. * process compliations of TX and RX
  959. *
  960. *
  961. ********************************************************************/
  962. void mtnic_process_tx_cq(struct mtnic_priv *priv, struct net_device *dev,
  963. struct mtnic_cq *cq)
  964. {
  965. struct mtnic_cqe *cqe = cq->buf;
  966. struct mtnic_ring *ring = &priv->tx_ring;
  967. u16 index;
  968. index = cq->last & (cq->size-1);
  969. cqe = &cq->buf[index];
  970. /* Owner bit changes every round */
  971. while (XNOR(cqe->op_tr_own & MTNIC_BIT_CQ_OWN, cq->last & cq->size)) {
  972. netdev_tx_complete (dev, ring->iobuf[index]);
  973. ++cq->last;
  974. index = cq->last & (cq->size-1);
  975. cqe = &cq->buf[index];
  976. }
  977. /* Update consumer index */
  978. cq->db->update_ci = cpu_to_be32(cq->last & 0xffffff);
  979. wmb(); /* ensure HW sees CQ consumer before we post new buffers */
  980. ring->cons = cq->last;
  981. }
  982. int mtnic_process_rx_cq(struct mtnic_priv *priv, struct net_device *dev, struct mtnic_cq *cq)
  983. {
  984. struct mtnic_cqe *cqe;
  985. struct mtnic_ring *ring = &priv->rx_ring;
  986. int index;
  987. int err;
  988. struct io_buffer *rx_iob;
  989. /* We assume a 1:1 mapping between CQEs and Rx descriptors, so Rx
  990. * descriptor offset can be deduced from the CQE index instead of
  991. * reading 'cqe->index' */
  992. index = cq->last & (cq->size-1);
  993. cqe = &cq->buf[index];
  994. /* Process all completed CQEs */
  995. while (XNOR(cqe->op_tr_own & MTNIC_BIT_CQ_OWN, cq->last & cq->size)) {
  996. /* Drop packet on bad receive or bad checksum */
  997. if ((cqe->op_tr_own & 0x1f) == MTNIC_OPCODE_ERROR) {
  998. DBG("CQE completed with error - vendor \n");
  999. free_iob(ring->iobuf[index]);
  1000. goto next;
  1001. }
  1002. if (cqe->enc_bf & MTNIC_BIT_BAD_FCS) {
  1003. DBG("Accepted packet with bad FCS\n");
  1004. free_iob(ring->iobuf[index]);
  1005. goto next;
  1006. }
  1007. /*
  1008. * Packet is OK - process it.
  1009. */
  1010. rx_iob = ring->iobuf[index];
  1011. iob_put(rx_iob, DEF_IOBUF_SIZE);
  1012. /* Add this packet to the receive queue. */
  1013. netdev_rx(dev, rx_iob);
  1014. ring->iobuf[index] = NULL;
  1015. next:
  1016. ++cq->last;
  1017. index = cq->last & (cq->size-1);
  1018. cqe = &cq->buf[index];
  1019. }
  1020. /* Update consumer index */
  1021. cq->db->update_ci = cpu_to_be32(cq->last & 0xffffff);
  1022. wmb(); /* ensure HW sees CQ consumer before we post new buffers */
  1023. ring->cons = cq->last;
  1024. if (ring->prod - ring->cons < (MAX_GAP_PROD_CONS)) {
  1025. err = mtnic_alloc_iobuf(priv, &priv->rx_ring, DEF_IOBUF_SIZE);
  1026. if (err) {
  1027. eprintf("ERROR Allocating io buffer");
  1028. return MTNIC_ERROR;
  1029. }
  1030. }
  1031. return 0;
  1032. }
  1033. /********************************************************************
  1034. *
  1035. * net_device functions
  1036. *
  1037. *
  1038. * open, poll, close, probe, disable, irq
  1039. *
  1040. *********************************************************************/
  1041. static int
  1042. mtnic_open(struct net_device *dev)
  1043. {
  1044. struct mtnic_priv *priv = netdev_priv(dev);
  1045. int err = 0;
  1046. struct mtnic_ring *ring;
  1047. struct mtnic_cq *cq;
  1048. int cq_ind = 0;
  1049. u32 dev_link_state;
  1050. DBG("starting port:%d", priv->port);
  1051. /* Alloc and configure CQs, TX, RX */
  1052. err = mtnic_alloc_resources(dev);
  1053. if (err) {
  1054. eprintf("Error allocating resources\n");
  1055. return MTNIC_ERROR;
  1056. }
  1057. /* Pass CQs configuration to HW */
  1058. for (cq_ind = 0; cq_ind < NUM_CQS; ++cq_ind) {
  1059. cq = &priv->cq[cq_ind];
  1060. err = mtnic_CONFIG_CQ(priv, priv->port, cq_ind, cq);
  1061. if (err) {
  1062. eprintf("Failed configuring CQ:%d error %d\n",
  1063. cq_ind, err);
  1064. if (cq_ind)
  1065. goto cq_error;
  1066. else
  1067. return MTNIC_ERROR;
  1068. }
  1069. /* Update consumer index */
  1070. cq->db->update_ci = cpu_to_be32(cq->last & 0xffffff);
  1071. }
  1072. /* Pass Tx configuration to HW */
  1073. ring = &priv->tx_ring;
  1074. err = mtnic_CONFIG_TX_RING(priv, priv->port, 0, ring);
  1075. if (err) {
  1076. eprintf("Failed configuring Tx ring:0\n");
  1077. goto cq_error;
  1078. }
  1079. /* Pass RX configuration to HW */
  1080. ring = &priv->rx_ring;
  1081. err = mtnic_CONFIG_RX_RING(priv, priv->port, 0, ring);
  1082. if (err) {
  1083. eprintf("Failed configuring Rx ring:0\n");
  1084. goto tx_error;
  1085. }
  1086. /* Configure Rx steering */
  1087. err = mtnic_CONFIG_PORT_RSS_STEER(priv, priv->port);
  1088. if (!err)
  1089. err = mtnic_SET_PORT_RSS_INDIRECTION(priv, priv->port);
  1090. if (err) {
  1091. eprintf("Failed configuring RSS steering\n");
  1092. goto rx_error;
  1093. }
  1094. /* Set the port default ring to ring 0 */
  1095. err = mtnic_SET_PORT_DEFAULT_RING(priv, priv->port, 0);
  1096. if (err) {
  1097. eprintf("Failed setting default ring\n");
  1098. goto rx_error;
  1099. }
  1100. /* Set Mac address */
  1101. err = mtnic_SET_RX_RING_ADDR(priv, priv->port, &priv->fw.mac[priv->port]);
  1102. if (err) {
  1103. eprintf("Failed setting default MAC address\n");
  1104. goto rx_error;
  1105. }
  1106. /* Set MTU */
  1107. err = mtnic_SET_PORT_MTU(priv, priv->port, DEF_MTU);
  1108. if (err) {
  1109. eprintf("Failed setting MTU\n");
  1110. goto rx_error;
  1111. }
  1112. /* Configure VLAN filter */
  1113. err = mtnic_CONFIG_PORT_VLAN_FILTER(priv, priv->port);
  1114. if (err) {
  1115. eprintf("Failed configuring VLAN filter\n");
  1116. goto rx_error;
  1117. }
  1118. /* Bring up physical link */
  1119. err = mtnic_SET_PORT_STATE(priv, priv->port, 1);
  1120. if (err) {
  1121. eprintf("Failed bringing up port\n");
  1122. goto rx_error;
  1123. }
  1124. mdelay(300); /* Let link state stabilize if cable was connected */
  1125. priv->state = CARD_UP;
  1126. err = mtnic_HEART_BEAT(priv, &dev_link_state);
  1127. if (err) {
  1128. eprintf("Failed getting device link state\n");
  1129. return MTNIC_ERROR;
  1130. }
  1131. if (!(dev_link_state & 0x3)) {
  1132. eprintf("Link down, check cables and restart\n");
  1133. return MTNIC_ERROR;
  1134. }
  1135. return 0;
  1136. rx_error:
  1137. err = mtnic_RELEASE_RESOURCE(priv, priv->port,
  1138. MTNIC_IF_RESOURCE_TYPE_RX_RING, 0);
  1139. tx_error:
  1140. err |= mtnic_RELEASE_RESOURCE(priv, priv->port,
  1141. MTNIC_IF_RESOURCE_TYPE_TX_RING, 0);
  1142. cq_error:
  1143. while (cq_ind) {
  1144. err |= mtnic_RELEASE_RESOURCE(priv, priv->port,
  1145. MTNIC_IF_RESOURCE_TYPE_CQ, --cq_ind);
  1146. }
  1147. if (err)
  1148. DBG("Eror Releasing resources\n");
  1149. return MTNIC_ERROR;
  1150. }
  1151. /** Check if we got completion for receive and transmit and
  1152. * check the line with heart_bit command */
  1153. static void
  1154. mtnic_poll(struct net_device *dev)
  1155. {
  1156. struct mtnic_priv *priv = netdev_priv(dev);
  1157. struct mtnic_cq *cq;
  1158. u32 dev_link_state;
  1159. int err;
  1160. unsigned int i;
  1161. /* In case of an old error then return */
  1162. if (priv->state != CARD_UP)
  1163. return;
  1164. /* We do not check the device every call _poll call,
  1165. since it will slow it down */
  1166. if ((priv->poll_counter % ROUND_TO_CHECK) == 0) {
  1167. /* Check device */
  1168. err = mtnic_HEART_BEAT(priv, &dev_link_state);
  1169. if (err) {
  1170. eprintf("Device has internal error\n");
  1171. priv->state = CARD_DOWN;
  1172. return;
  1173. }
  1174. if (!(dev_link_state & 0x3)) {
  1175. eprintf("Link down, check cables and restart\n");
  1176. priv->state = CARD_DOWN;
  1177. return;
  1178. }
  1179. }
  1180. /* Polling CQ */
  1181. for (i = 0; i < NUM_CQS; i++) {
  1182. cq = &priv->cq[i]; //Passing on the 2 cqs.
  1183. if (cq->is_rx) {
  1184. err = mtnic_process_rx_cq(priv, cq->dev, cq);
  1185. if (err) {
  1186. priv->state = CARD_DOWN;
  1187. eprintf(" Error allocating RX buffers\n");
  1188. return;
  1189. }
  1190. } else {
  1191. mtnic_process_tx_cq(priv, cq->dev, cq);
  1192. }
  1193. }
  1194. ++ priv->poll_counter;
  1195. }
  1196. static int
  1197. mtnic_transmit( struct net_device *dev, struct io_buffer *iobuf )
  1198. {
  1199. struct mtnic_priv *priv = netdev_priv(dev);
  1200. struct mtnic_ring *ring;
  1201. struct mtnic_tx_desc *tx_desc;
  1202. struct mtnic_data_seg *data;
  1203. u32 index;
  1204. /* In case of an error then return */
  1205. if (priv->state != CARD_UP)
  1206. return MTNIC_ERROR;
  1207. ring = &priv->tx_ring;
  1208. index = ring->prod & ring->size_mask;
  1209. if ((ring->prod - ring->cons) >= ring->size) {
  1210. DBG("No space left for descriptors!!! cons: %lx prod: %lx\n",
  1211. ring->cons, ring->prod);
  1212. mdelay(5);
  1213. return MTNIC_ERROR;/* no space left */
  1214. }
  1215. /* get current descriptor */
  1216. tx_desc = ring->buf + (index * sizeof(struct mtnic_tx_desc));
  1217. /* Prepare ctrl segement */
  1218. tx_desc->ctrl.size_vlan = cpu_to_be32(2);
  1219. tx_desc->ctrl.flags = cpu_to_be32(MTNIC_BIT_TX_COMP |
  1220. MTNIC_BIT_NO_ICRC);
  1221. tx_desc->ctrl.op_own = cpu_to_be32(MTNIC_OPCODE_SEND) |
  1222. ((ring->prod & ring->size) ?
  1223. cpu_to_be32(MTNIC_BIT_DESC_OWN) : 0);
  1224. /* Prepare Data Seg */
  1225. data = &tx_desc->data;
  1226. data->addr_l = cpu_to_be32((u32)virt_to_bus(iobuf->data));
  1227. data->count = cpu_to_be32(iob_len(iobuf));
  1228. data->mem_type = priv->fw.mem_type_snoop_be;
  1229. /* Attach io_buffer */
  1230. ring->iobuf[index] = iobuf;
  1231. /* Update producer index */
  1232. ++ring->prod;
  1233. /* Ring doorbell! */
  1234. wmb();
  1235. writel((u32) ring->db_offset, &ring->txcq_db->send_db);
  1236. return 0;
  1237. }
  1238. static void
  1239. mtnic_close(struct net_device *dev)
  1240. {
  1241. struct mtnic_priv *priv = netdev_priv(dev);
  1242. int err = 0;
  1243. DBG("Close called for port:%d\n", priv->port);
  1244. if (priv->state == CARD_UP) {
  1245. /* Disable port */
  1246. err |= mtnic_SET_PORT_STATE(priv, priv->port, 0);
  1247. /*
  1248. * Stop HW associated with this port
  1249. */
  1250. mdelay(5);
  1251. /* Stop RX */
  1252. err |= mtnic_RELEASE_RESOURCE(priv, priv->port,
  1253. MTNIC_IF_RESOURCE_TYPE_RX_RING, 0);
  1254. /* Stop TX */
  1255. err |= mtnic_RELEASE_RESOURCE(priv, priv->port,
  1256. MTNIC_IF_RESOURCE_TYPE_TX_RING, 0);
  1257. /* Stop CQs */
  1258. err |= mtnic_RELEASE_RESOURCE(priv, priv->port,
  1259. MTNIC_IF_RESOURCE_TYPE_CQ, 0);
  1260. err |= mtnic_RELEASE_RESOURCE(priv, priv->port,
  1261. MTNIC_IF_RESOURCE_TYPE_CQ, 1);
  1262. if (err) {
  1263. DBG("Close reported error %d", err);
  1264. }
  1265. /* Free memory */
  1266. free(priv->tx_ring.buf);
  1267. iounmap(priv->tx_ring.txcq_db);
  1268. free(priv->cq[1].buf);
  1269. free(priv->cq[1].db);
  1270. /* Free RX buffers */
  1271. mtnic_free_io_buffers(&priv->rx_ring);
  1272. free(priv->rx_ring.buf);
  1273. free(priv->rx_ring.db);
  1274. free(priv->cq[0].buf);
  1275. free(priv->cq[0].db);
  1276. priv->state = CARD_INITIALIZED;
  1277. }
  1278. }
  1279. static void
  1280. mtnic_disable(struct pci_device *pci)
  1281. {
  1282. int err;
  1283. struct net_device *dev = pci_get_drvdata(pci);
  1284. struct mtnic_priv *priv = netdev_priv(dev);
  1285. /* Should NOT happen! but just in case */
  1286. if (priv->state == CARD_UP)
  1287. mtnic_close(dev);
  1288. if (priv->state == CARD_INITIALIZED) {
  1289. err = mtnic_RELEASE_RESOURCE(priv, 0,
  1290. MTNIC_IF_RESOURCE_TYPE_EQ, 0);
  1291. DBG("Calling MTNIC_CLOSE command\n");
  1292. err |= mtnic_cmd(priv, NULL, NULL, 0,
  1293. MTNIC_IF_CMD_CLOSE_NIC);
  1294. if (err) {
  1295. DBG("Error Releasing resources %d\n", err);
  1296. }
  1297. free(priv->cmd.buf);
  1298. iounmap(priv->hcr);
  1299. ufree((u32)priv->fw.fw_pages.buf);
  1300. ufree((u32)priv->fw.extra_pages.buf);
  1301. free(priv->eq.buf);
  1302. iounmap(priv->eq_db);
  1303. priv->state = CARD_DOWN;
  1304. }
  1305. unregister_netdev(dev);
  1306. netdev_nullify(dev);
  1307. netdev_put(dev);
  1308. }
  1309. static void
  1310. mtnic_irq(struct net_device *netdev __unused, int enable __unused)
  1311. {
  1312. /* Not implemented */
  1313. }
  1314. /** mtnic net device operations */
  1315. static struct net_device_operations mtnic_operations = {
  1316. .open = mtnic_open,
  1317. .close = mtnic_close,
  1318. .transmit = mtnic_transmit,
  1319. .poll = mtnic_poll,
  1320. .irq = mtnic_irq,
  1321. };
  1322. static int
  1323. mtnic_probe(struct pci_device *pci,
  1324. const struct pci_device_id *id __unused)
  1325. {
  1326. struct net_device *dev;
  1327. struct mtnic_priv *priv;
  1328. int err;
  1329. u64 mac;
  1330. u32 result = 0;
  1331. void *dev_id;
  1332. int i;
  1333. if (pci->vendor != MELLANOX_VENDOR_ID) {
  1334. eprintf("");
  1335. return 0;
  1336. }
  1337. printf("\nMellanox Technologies LTD - Boot over MTNIC implementaion\n");
  1338. adjust_pci_device(pci);
  1339. err = mtnic_init_pci(pci);
  1340. if (err) {
  1341. eprintf("Error in pci_init\n");
  1342. return MTNIC_ERROR;
  1343. }
  1344. mtnic_reset();
  1345. mdelay(1000);
  1346. err = restore_config();
  1347. if (err) {
  1348. eprintf("");
  1349. return err;
  1350. }
  1351. /* Checking MTNIC device ID */
  1352. dev_id = ioremap(mtnic_pci_dev.dev.bar[0] +
  1353. MTNIC_DEVICE_ID_OFFSET, 4);
  1354. result = ntohl(readl(dev_id));
  1355. iounmap(dev_id);
  1356. if (result != MTNIC_DEVICE_ID) {
  1357. eprintf("Wrong Devie ID (0x%lx) !!!", result);
  1358. return MTNIC_ERROR;
  1359. }
  1360. /* Initializing net device */
  1361. dev = alloc_etherdev(sizeof(struct mtnic_priv));
  1362. if (dev == NULL) {
  1363. eprintf("Net device allocation failed\n");
  1364. return MTNIC_ERROR;
  1365. }
  1366. /*
  1367. * Initialize driver private data
  1368. */
  1369. priv = netdev_priv(dev);
  1370. memset(priv, 0, sizeof(struct mtnic_priv));
  1371. priv->dev = dev;
  1372. priv->pdev = pci;
  1373. priv->dev->dev = &pci->dev;
  1374. /* Attach pci device */
  1375. pci_set_drvdata(pci, priv->dev);
  1376. netdev_init(dev, &mtnic_operations);
  1377. /* Initialize hardware */
  1378. err = mtnic_init_card(dev);
  1379. if (err) {
  1380. eprintf("Error in init_card\n");
  1381. return MTNIC_ERROR;
  1382. }
  1383. /* Program the MAC address */
  1384. mac = priv->fw.mac[priv->port];
  1385. printf("Port %d Mac address: 0x%12llx\n", MTNIC_PORT_NUM + 1, mac);
  1386. for (i = 0;i < MAC_ADDRESS_SIZE; ++i) {
  1387. dev->ll_addr[MAC_ADDRESS_SIZE - i - 1] = mac & 0xFF;
  1388. mac = mac >> 8;
  1389. }
  1390. if (register_netdev(dev)) {
  1391. eprintf("Netdev registration failed\n");
  1392. return MTNIC_ERROR;
  1393. }
  1394. return 0;
  1395. }
  1396. static struct pci_device_id mtnic_nics[] = {
  1397. PCI_ROM(0x15b3, 0x6368, "mtnic", "Mellanox MTNIC driver"),
  1398. };
  1399. struct pci_driver mtnic_driver __pci_driver = {
  1400. .ids = mtnic_nics,
  1401. .id_count = sizeof(mtnic_nics) / sizeof(mtnic_nics[0]),
  1402. .probe = mtnic_probe,
  1403. .remove = mtnic_disable,
  1404. };