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