mtnic.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
00003  *
00004  * This software is available to you under a choice of one of two
00005  * licenses.  You may choose to be licensed under the terms of the GNU
00006  * General Public License (GPL) Version 2, available from the file
00007  * COPYING in the main directory of this source tree, or the
00008  * OpenIB.org BSD license below:
00009  *
00010  *     Redistribution and use in source and binary forms, with or
00011  *     without modification, are permitted provided that the following
00012  *     conditions are met:
00013  *
00014  *      - Redistributions of source code must retain the above
00015  *        copyright notice, this list of conditions and the following
00016  *        disclaimer.
00017  *
00018  *      - Redistributions in binary form must reproduce the above
00019  *        copyright notice, this list of conditions and the following
00020  *        disclaimer in the documentation and/or other materials
00021  *        provided with the distribution.
00022  *
00023  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00024  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00025  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00026  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
00027  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
00028  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00029  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00030  * SOFTWARE.
00031  *
00032  */
00033 
00034 FILE_LICENCE ( GPL2_ONLY );
00035 
00036 #include <strings.h>
00037 #include <errno.h>
00038 #include <gpxe/malloc.h>
00039 #include <gpxe/umalloc.h>
00040 #include <byteswap.h>
00041 #include <unistd.h>
00042 #include <gpxe/io.h>
00043 #include <gpxe/pci.h>
00044 #include <gpxe/ethernet.h>
00045 #include <gpxe/netdevice.h>
00046 #include <gpxe/iobuf.h>
00047 #include "mtnic.h"
00048 
00049 
00050 /*
00051 
00052 
00053     mtnic.c - gPXE driver for Mellanox 10Gig ConnectX EN
00054 
00055 
00056 */
00057 
00058 
00059 
00060 /********************************************************************
00061 *
00062 *       MTNIC allocation functions
00063 *
00064 *********************************************************************/
00065 /**
00066 * mtnic_alloc_aligned
00067 *
00068 * @v    unsigned int size       size
00069 * @v    void **va               virtual address
00070 * @v    u32 *pa                 physical address
00071 * @v    u32 aligment            aligment
00072 *
00073 * Function allocate aligned buffer and put it's virtual address in 'va'
00074 * and it's physical aligned address in 'pa'
00075 */
00076 static int
00077 mtnic_alloc_aligned(unsigned int size, void **va, unsigned long *pa, unsigned int alignment)
00078 {
00079         *va = alloc_memblock(size, alignment);
00080         if (!*va) {
00081                 return -EADDRINUSE;
00082         }
00083         *pa = (u32)virt_to_bus(*va);
00084         return 0;
00085 }
00086 
00087 
00088 
00089 /**
00090  *
00091  * mtnic alloc command interface
00092  *
00093  */
00094 static int
00095 mtnic_alloc_cmdif(struct mtnic *mtnic)
00096 {
00097         u32 bar = mtnic_pci_dev.dev.bar[0];
00098 
00099         mtnic->hcr = ioremap(bar + MTNIC_HCR_BASE, MTNIC_HCR_SIZE);
00100         if ( !mtnic->hcr ) {
00101                 DBG("Couldn't map command register\n");
00102                 return -EADDRINUSE;
00103         }
00104         mtnic_alloc_aligned(PAGE_SIZE, (void *)&mtnic->cmd.buf, &mtnic->cmd.mapping, PAGE_SIZE);
00105         if ( !mtnic->cmd.buf ) {
00106                 DBG("Error in allocating buffer for command interface\n");
00107                 return -EADDRINUSE;
00108         }
00109         return 0;
00110 }
00111 
00112 /**
00113  * Free RX io buffers
00114  */
00115 static void
00116 mtnic_free_io_buffers(struct mtnic_ring *ring)
00117 {
00118         int index;
00119 
00120         for (; ring->cons <= ring->prod; ++ring->cons) {
00121                 index = ring->cons & ring->size_mask;
00122                 if ( ring->iobuf[index] ) {
00123                         free_iob(ring->iobuf[index]);
00124                 }
00125         }
00126 }
00127 
00128 
00129 
00130 /**
00131  *
00132  * mtnic alloc and attach io buffers
00133  *
00134  */
00135 static int
00136 mtnic_alloc_iobuf(struct mtnic_port *priv, struct mtnic_ring *ring,
00137                   unsigned int size)
00138 {
00139         struct mtnic_rx_desc *rx_desc_ptr = ring->buf;
00140         u32 index;
00141 
00142         while ((u32)(ring->prod - ring->cons) < UNITS_BUFFER_SIZE) {
00143                 index = ring->prod & ring->size_mask;
00144                 ring->iobuf[index] = alloc_iob(size);
00145                 if (!ring->iobuf[index]) {
00146                         if (ring->prod <= (ring->cons + 1)) {
00147                                 DBG ( "Dropping packet, buffer is full\n" );
00148                         }
00149                         break;
00150                 }
00151 
00152                 /* Attach io_buffer to descriptor */
00153                 rx_desc_ptr = ring->buf +
00154                               (sizeof(struct mtnic_rx_desc) * index);
00155                 rx_desc_ptr->data.count = cpu_to_be32(size);
00156                 rx_desc_ptr->data.mem_type = priv->mtnic->fw.mem_type_snoop_be;
00157                 rx_desc_ptr->data.addr_l = cpu_to_be32(
00158                                                       virt_to_bus(ring->iobuf[index]->data));
00159 
00160                 ++ ring->prod;
00161         }
00162 
00163         /* Update RX producer index (PI) */
00164         ring->db->count = cpu_to_be32(ring->prod & 0xffff);
00165         return 0;
00166 }
00167 
00168 
00169 /**
00170  * mtnic alloc ring
00171  *
00172  *      Alloc and configure TX or RX ring
00173  *
00174  */
00175 static int
00176 mtnic_alloc_ring(struct mtnic_port *priv, struct mtnic_ring *ring,
00177                  u32 size, u16 stride, u16 cq, u8 is_rx)
00178 {
00179         unsigned int i;
00180         int err;
00181         struct mtnic_rx_desc *rx_desc;
00182         struct mtnic_tx_desc *tx_desc;
00183 
00184         ring->size = size; /* Number of descriptors */
00185         ring->size_mask = size - 1;
00186         ring->stride = stride; /* Size of each entry */
00187         ring->cq = cq; /* CQ number associated with this ring */
00188         ring->cons = 0;
00189         ring->prod = 0;
00190 
00191         /* Alloc descriptors buffer */
00192         ring->buf_size = ring->size * ((is_rx) ? sizeof(struct mtnic_rx_desc) :
00193                                        sizeof(struct mtnic_tx_desc));
00194         err = mtnic_alloc_aligned(ring->buf_size, (void *)&ring->buf,
00195                                   &ring->dma, PAGE_SIZE);
00196         if (err) {
00197                 DBG("Failed allocating descriptor ring sizeof %x\n",
00198                     ring->buf_size);
00199                 return -EADDRINUSE;
00200         }
00201         memset(ring->buf, 0, ring->buf_size);
00202 
00203         DBG("Allocated %s ring (addr:%p) - buf:%p size:%x"
00204             "buf_size:%x dma:%lx\n",
00205             is_rx ? "Rx" : "Tx", ring, ring->buf, ring->size,
00206             ring->buf_size, ring->dma);
00207 
00208 
00209         if (is_rx) { /* RX ring */
00210                 /* Alloc doorbell */
00211                 err = mtnic_alloc_aligned(sizeof(struct mtnic_cq_db_record),
00212                                           (void *)&ring->db, &ring->db_dma, 32);
00213                 if (err) {
00214                         DBG("Failed allocating Rx ring doorbell record\n");
00215                         free_memblock(ring->buf, ring->buf_size);
00216                         return -EADDRINUSE;
00217                 }
00218 
00219                 /* ==- Configure Descriptor -== */
00220                 /* Init ctrl seg of rx desc */
00221                 for (i = 0; i < UNITS_BUFFER_SIZE; ++i) {
00222                         rx_desc = ring->buf +
00223                                   (sizeof(struct mtnic_rx_desc) * i);
00224                         /* Pre-link descriptor */
00225                         rx_desc->next = cpu_to_be16(i + 1);
00226                 }
00227                 /*The last ctrl descriptor is '0' and points to the first one*/
00228 
00229                 /* Alloc IO_BUFFERS */
00230                 err = mtnic_alloc_iobuf ( priv, ring, DEF_IOBUF_SIZE );
00231                 if (err) {
00232                         DBG("ERROR Allocating io buffer\n");
00233                         free_memblock(ring->buf, ring->buf_size);
00234                         return -EADDRINUSE;
00235                 }
00236 
00237         } else { /* TX ring */
00238                 /* Set initial ownership of all Tx Desc' to SW (1) */
00239                 for (i = 0; i < ring->size; i++) {
00240                         tx_desc = ring->buf + ring->stride * i;
00241                         tx_desc->ctrl.op_own = cpu_to_be32(MTNIC_BIT_DESC_OWN);
00242                 }
00243                 /* DB */
00244                 ring->db_offset = cpu_to_be32(
00245                                              ((u32) priv->mtnic->fw.tx_offset[priv->port]) << 8);
00246 
00247                 /* Map Tx+CQ doorbells */
00248                 DBG("Mapping TxCQ doorbell at offset:0x%x\n",
00249                     priv->mtnic->fw.txcq_db_offset);
00250                 ring->txcq_db = ioremap(mtnic_pci_dev.dev.bar[2] +
00251                                         priv->mtnic->fw.txcq_db_offset, PAGE_SIZE);
00252                 if (!ring->txcq_db) {
00253                         DBG("Couldn't map txcq doorbell, aborting...\n");
00254                         free_memblock(ring->buf, ring->buf_size);
00255                         return -EADDRINUSE;
00256                 }
00257         }
00258 
00259         return 0;
00260 }
00261 
00262 
00263 
00264 /**
00265  * mtnic alloc CQ
00266  *
00267  *      Alloc and configure CQ.
00268  *
00269  */
00270 static int
00271 mtnic_alloc_cq(struct net_device *dev, int num, struct mtnic_cq *cq,
00272                u8 is_rx, u32 size, u32 offset_ind)
00273 {
00274         int err ;
00275         unsigned int i;
00276 
00277         cq->num = num;
00278         cq->dev = dev;
00279         cq->size = size;
00280         cq->last = 0;
00281         cq->is_rx = is_rx;
00282         cq->offset_ind = offset_ind;
00283 
00284         /* Alloc doorbell */
00285         err = mtnic_alloc_aligned(sizeof(struct mtnic_cq_db_record),
00286                                   (void *)&cq->db, &cq->db_dma, 32);
00287         if (err) {
00288                 DBG("Failed allocating CQ doorbell record\n");
00289                 return -EADDRINUSE;
00290         }
00291         memset(cq->db, 0, sizeof(struct mtnic_cq_db_record));
00292 
00293         /* Alloc CQEs buffer */
00294         cq->buf_size = size * sizeof(struct mtnic_cqe);
00295         err = mtnic_alloc_aligned(cq->buf_size,
00296                                   (void *)&cq->buf, &cq->dma, PAGE_SIZE);
00297         if (err) {
00298                 DBG("Failed allocating CQ buffer\n");
00299                 free_memblock(cq->db, sizeof(struct mtnic_cq_db_record));
00300                 return -EADDRINUSE;
00301         }
00302         memset(cq->buf, 0, cq->buf_size);
00303         DBG("Allocated CQ (addr:%p) - size:%x buf:%p buf_size:%x "
00304             "dma:%lx db:%p db_dma:%lx\n"
00305             "cqn offset:%x \n", cq, cq->size, cq->buf,
00306             cq->buf_size, cq->dma, cq->db,
00307             cq->db_dma, offset_ind);
00308 
00309 
00310         /* Set ownership of all CQEs to HW */
00311         DBG("Setting HW ownership for CQ:%d\n", num);
00312         for (i = 0; i < cq->size; i++) {
00313                 /* Initial HW ownership is 1 */
00314                 cq->buf[i].op_tr_own = MTNIC_BIT_CQ_OWN;
00315         }
00316         return 0;
00317 }
00318 
00319 
00320 
00321 /**
00322  * mtnic_alloc_resources
00323  *
00324  *      Alloc and configure CQs, Tx, Rx
00325  */
00326 unsigned int
00327 mtnic_alloc_resources(struct net_device *dev)
00328 {
00329         struct mtnic_port *priv = netdev_priv(dev);
00330         int err;
00331         int cq_ind = 0;
00332         int cq_offset = priv->mtnic->fw.cq_offset;
00333 
00334         /* Alloc 1st CQ */
00335         err = mtnic_alloc_cq(dev, cq_ind, &priv->cq[cq_ind], 1 /* RX */,
00336                              UNITS_BUFFER_SIZE, cq_offset + cq_ind);
00337         if (err) {
00338                 DBG("Failed allocating Rx CQ\n");
00339                 return -EADDRINUSE;
00340         }
00341 
00342 
00343         /* Alloc RX */
00344         err = mtnic_alloc_ring(priv, &priv->rx_ring, UNITS_BUFFER_SIZE,
00345                                sizeof(struct mtnic_rx_desc), cq_ind, /* RX */1);
00346         if (err) {
00347                 DBG("Failed allocating Rx Ring\n");
00348                 goto cq0_error;
00349         }
00350 
00351 
00352         ++cq_ind;
00353 
00354         /* alloc 2nd CQ */
00355         err = mtnic_alloc_cq(dev, cq_ind, &priv->cq[cq_ind], 0 /* TX */,
00356                              UNITS_BUFFER_SIZE, cq_offset + cq_ind);
00357         if (err) {
00358                 DBG("Failed allocating Tx CQ\n");
00359                 goto rx_error;
00360         }
00361 
00362         /* Alloc TX */
00363         err = mtnic_alloc_ring(priv, &priv->tx_ring, UNITS_BUFFER_SIZE,
00364                                sizeof(struct mtnic_tx_desc), cq_ind, /* TX */ 0);
00365         if (err) {
00366                 DBG("Failed allocating Tx ring\n");
00367                 goto cq1_error;
00368         }
00369 
00370         return 0;
00371 
00372 cq1_error:
00373         free_memblock(priv->cq[1].buf, priv->cq[1].buf_size);
00374         free_memblock(priv->cq[1].db, sizeof(struct mtnic_cq_db_record));
00375 
00376 rx_error:
00377         free_memblock(priv->rx_ring.buf, priv->rx_ring.buf_size);
00378         free_memblock(priv->rx_ring.db, sizeof(struct mtnic_cq_db_record));
00379         mtnic_free_io_buffers(&priv->rx_ring);
00380 cq0_error:
00381         free_memblock(priv->cq[0].buf, priv->cq[0].buf_size);
00382         free_memblock(priv->cq[0].db, sizeof(struct mtnic_cq_db_record));
00383 
00384         return -EADDRINUSE;
00385 }
00386 
00387 
00388 /**
00389  *  mtnic alloc_eq
00390  *
00391  * Note: EQ is not used by the driver but must be allocated
00392  */
00393 static int
00394 mtnic_alloc_eq(struct mtnic *mtnic)
00395 {
00396         int err;
00397         unsigned int i;
00398         struct mtnic_eqe *eqe_desc = NULL;
00399 
00400         /* Allocating doorbell */
00401         mtnic->eq_db = ioremap(mtnic_pci_dev.dev.bar[2] +
00402                                mtnic->fw.eq_db_offset, sizeof(u32));
00403         if (!mtnic->eq_db) {
00404                 DBG("Couldn't map EQ doorbell, aborting...\n");
00405                 return -EADDRINUSE;
00406         }
00407 
00408         /* Allocating buffer */
00409         mtnic->eq.size = NUM_EQES;
00410         mtnic->eq.buf_size = mtnic->eq.size * sizeof(struct mtnic_eqe);
00411         err = mtnic_alloc_aligned(mtnic->eq.buf_size, (void *)&mtnic->eq.buf,
00412                                   &mtnic->eq.dma, PAGE_SIZE);
00413         if (err) {
00414                 DBG("Failed allocating EQ buffer\n");
00415                 iounmap(mtnic->eq_db);
00416                 return -EADDRINUSE;
00417         }
00418         memset(mtnic->eq.buf, 0, mtnic->eq.buf_size);
00419 
00420         for (i = 0; i < mtnic->eq.size; i++)
00421                 eqe_desc = mtnic->eq.buf + (sizeof(struct mtnic_eqe) * i);
00422         eqe_desc->own |= MTNIC_BIT_EQE_OWN;
00423 
00424         mdelay(20);
00425         return 0;
00426 }
00427 
00428 
00429 
00430 
00431 
00432 
00433 
00434 
00435 
00436 
00437 
00438 /********************************************************************
00439 *
00440 * Mtnic commands functions
00441 * -=-=-=-=-=-=-=-=-=-=-=-=
00442 *
00443 *
00444 *
00445 *********************************************************************/
00446 static inline int
00447 cmdif_go_bit(struct mtnic *mtnic)
00448 {
00449         struct mtnic_if_cmd_reg *hcr = mtnic->hcr;
00450         u32 status;
00451         int i;
00452 
00453         for (i = 0; i < TBIT_RETRIES; i++) {
00454                 status = be32_to_cpu(readl(&hcr->status_go_opcode));
00455                 if ((status & MTNIC_BC_MASK(MTNIC_MASK_CMD_REG_T_BIT)) ==
00456                     (mtnic->cmd.tbit << MTNIC_BC_OFF(MTNIC_MASK_CMD_REG_T_BIT))) {
00457                         /* Read expected t-bit - now return go-bit value */
00458                         return status & MTNIC_BC_MASK(MTNIC_MASK_CMD_REG_GO_BIT);
00459                 }
00460         }
00461 
00462         DBG("Invalid tbit after %d retries!\n", TBIT_RETRIES);
00463         return -EBUSY; /* Return busy... */
00464 }
00465 
00466 /* Base Command interface */
00467 static int
00468 mtnic_cmd(struct mtnic *mtnic, void *in_imm,
00469           void *out_imm, u32 in_modifier, u16 op)
00470 {
00471 
00472         struct mtnic_if_cmd_reg *hcr = mtnic->hcr;
00473         int err = 0;
00474         u32 out_param_h = 0;
00475         u32 out_param_l = 0;
00476         u32 in_param_h = 0;
00477         u32 in_param_l = 0;
00478 
00479 
00480         static u16 token = 0x8000;
00481         u32 status;
00482         unsigned int timeout = 0;
00483 
00484         token++;
00485 
00486         if ( cmdif_go_bit ( mtnic ) ) {
00487                 DBG("GO BIT BUSY:%p.\n", hcr + 6);
00488                 err = -EBUSY;
00489                 goto out;
00490         }
00491         if (in_imm) {
00492                 in_param_h = *((u32*)in_imm);
00493                 in_param_l = *((u32*)in_imm + 1);
00494         } else {
00495                 in_param_l = cpu_to_be32(mtnic->cmd.mapping);
00496         }
00497         out_param_l = cpu_to_be32(mtnic->cmd.mapping);
00498 
00499         /* writing to MCR */
00500         writel(in_param_h,          &hcr->in_param_h);
00501         writel(in_param_l,          &hcr->in_param_l);
00502         writel((u32) cpu_to_be32(in_modifier),  &hcr->input_modifier);
00503         writel(out_param_h,         &hcr->out_param_h);
00504         writel(out_param_l,         &hcr->out_param_l);
00505         writel((u32)cpu_to_be32(token << 16),   &hcr->token);
00506         wmb();
00507 
00508         /* flip toggle bit before each write to the HCR */
00509         mtnic->cmd.tbit = !mtnic->cmd.tbit;
00510         writel( ( u32 )
00511                 cpu_to_be32(MTNIC_BC_MASK(MTNIC_MASK_CMD_REG_GO_BIT) |
00512                             ( mtnic->cmd.tbit << MTNIC_BC_OFF ( MTNIC_MASK_CMD_REG_T_BIT ) ) | op ),
00513                 &hcr->status_go_opcode);
00514 
00515         while ( cmdif_go_bit ( mtnic ) && ( timeout <= GO_BIT_TIMEOUT ) ) {
00516                 mdelay ( 1 );
00517                 ++timeout;
00518         }
00519 
00520         if ( cmdif_go_bit ( mtnic ) ) {
00521                 DBG("Command opcode:0x%x token:0x%x TIMEOUT.\n", op, token);
00522                 err = -EBUSY;
00523                 goto out;
00524         }
00525 
00526         if (out_imm) {
00527                 *((u32 *)out_imm) = readl(&hcr->out_param_h);
00528                 *((u32 *)out_imm + 1) = readl(&hcr->out_param_l);
00529         }
00530 
00531         status = be32_to_cpu((u32)readl(&hcr->status_go_opcode)) >> 24;
00532 
00533         if (status) {
00534                 DBG("Command opcode:0x%x token:0x%x returned:0x%x\n",
00535                     op, token, status);
00536                 return status;
00537         }
00538 
00539 out:
00540         return err;
00541 }
00542 
00543 /* MAP PAGES wrapper */
00544 static int
00545 mtnic_map_cmd(struct mtnic *mtnic, u16 op, struct mtnic_pages pages)
00546 {
00547         unsigned int j;
00548         u32 addr;
00549         unsigned int len;
00550         u32 *page_arr = mtnic->cmd.buf;
00551         int nent = 0;
00552         int err = 0;
00553 
00554         memset(page_arr, 0, PAGE_SIZE);
00555 
00556         len = PAGE_SIZE * pages.num;
00557         pages.buf = (u32 *)umalloc(PAGE_SIZE * (pages.num + 1));
00558         addr = PAGE_SIZE + ((virt_to_bus(pages.buf) & 0xfffff000) + PAGE_SIZE);
00559         DBG("Mapping pages: size: %x address: %p\n", pages.num, pages.buf);
00560 
00561         if (addr & (PAGE_MASK)) {
00562                 DBG("Got FW area not aligned to %d (%llx/%x)\n",
00563                     PAGE_SIZE, (u64) addr, len);
00564                 return -EADDRINUSE;
00565         }
00566 
00567         /* Function maps each PAGE seperately */
00568         for (j = 0; j < len; j+= PAGE_SIZE) {
00569                 page_arr[nent * 4 + 3] = cpu_to_be32(addr + j);
00570                 if (++nent == MTNIC_MAILBOX_SIZE / 16) {
00571                         err = mtnic_cmd(mtnic, NULL, NULL, nent, op);
00572                         if (err)
00573                                 return -EIO;
00574                         nent = 0;
00575                 }
00576         }
00577 
00578         if (nent) {
00579                 err = mtnic_cmd(mtnic, NULL, NULL, nent, op);
00580         }
00581         return err;
00582 }
00583 
00584 
00585 
00586 /*
00587  * Query FW
00588  */
00589 static int
00590 mtnic_QUERY_FW ( struct mtnic *mtnic )
00591 {
00592         int err;
00593         struct mtnic_if_query_fw_out_mbox *cmd = mtnic->cmd.buf;
00594 
00595         err = mtnic_cmd(mtnic, NULL, NULL, 0, MTNIC_IF_CMD_QUERY_FW);
00596         if (err)
00597                 return -EIO;
00598 
00599         /* Get FW and interface versions */
00600         mtnic->fw_ver = ((u64) be16_to_cpu(cmd->rev_maj) << 32) |
00601                         ((u64) be16_to_cpu(cmd->rev_min) << 16) |
00602                         (u64) be16_to_cpu(cmd->rev_smin);
00603         mtnic->fw.ifc_rev = be16_to_cpu(cmd->ifc_rev);
00604 
00605         /* Get offset for internal error reports (debug) */
00606         mtnic->fw.err_buf.offset = be64_to_cpu(cmd->err_buf_start);
00607         mtnic->fw.err_buf.size = be32_to_cpu(cmd->err_buf_size);
00608 
00609         DBG("Error buf offset is %llx\n", mtnic->fw.err_buf.offset);
00610 
00611         /* Get number of required FW (4k) pages */
00612         mtnic->fw.fw_pages.num = be16_to_cpu(cmd->fw_pages);
00613 
00614         return 0;
00615 }
00616 
00617 
00618 static int
00619 mtnic_OPEN_NIC(struct mtnic *mtnic)
00620 {
00621         struct mtnic_if_open_nic_in_mbox *open_nic = mtnic->cmd.buf;
00622         u32 extra_pages[2] = {0};
00623         int err;
00624 
00625         memset(open_nic, 0, sizeof *open_nic);
00626 
00627         /* port 1 */
00628         open_nic->log_rx_p1 = 0;
00629         open_nic->log_cq_p1 = 1;
00630 
00631         open_nic->log_tx_p1 = 0;
00632         open_nic->steer_p1 = MTNIC_IF_STEER_RSS;
00633         /* MAC + VLAN - leave reserved */
00634 
00635         /* port 2 */
00636         open_nic->log_rx_p2 = 0;
00637         open_nic->log_cq_p2 = 1;
00638 
00639         open_nic->log_tx_p2 = 0;
00640         open_nic->steer_p2 = MTNIC_IF_STEER_RSS;
00641         /* MAC + VLAN - leave reserved */
00642 
00643         err = mtnic_cmd(mtnic, NULL, extra_pages, 0, MTNIC_IF_CMD_OPEN_NIC);
00644 
00645         mtnic->fw.extra_pages.num = be32_to_cpu(*(extra_pages+1));
00646         DBG("Extra pages num is %x\n", mtnic->fw.extra_pages.num);
00647         return err;
00648 }
00649 
00650 static int
00651 mtnic_CONFIG_RX(struct mtnic *mtnic)
00652 {
00653         struct mtnic_if_config_rx_in_imm config_rx;
00654 
00655         memset(&config_rx, 0, sizeof config_rx);
00656         return mtnic_cmd(mtnic, &config_rx, NULL, 0, MTNIC_IF_CMD_CONFIG_RX);
00657 }
00658 
00659 static int
00660 mtnic_CONFIG_TX(struct mtnic *mtnic)
00661 {
00662         struct mtnic_if_config_send_in_imm config_tx;
00663 
00664         config_tx.enph_gpf = 0;
00665         return mtnic_cmd(mtnic, &config_tx, NULL, 0, MTNIC_IF_CMD_CONFIG_TX);
00666 }
00667 
00668 static int
00669 mtnic_HEART_BEAT(struct mtnic_port *priv, u32 *link_state)
00670 {
00671         struct mtnic_if_heart_beat_out_imm heart_beat;
00672 
00673         int err;
00674         u32 flags;
00675         err = mtnic_cmd(priv->mtnic, NULL, &heart_beat, 0, MTNIC_IF_CMD_HEART_BEAT);
00676         if (!err) {
00677                 flags = be32_to_cpu(heart_beat.flags);
00678                 if (flags & MTNIC_BC_MASK(MTNIC_MASK_HEAR_BEAT_INT_ERROR)) {
00679                         DBG("Internal error detected\n");
00680                         return -EIO;
00681                 }
00682                 *link_state = flags &
00683                               ~((u32) MTNIC_BC_MASK(MTNIC_MASK_HEAR_BEAT_INT_ERROR));
00684         }
00685         return err;
00686 }
00687 
00688 
00689 /*
00690  * Port commands
00691  */
00692 
00693 static int
00694 mtnic_SET_PORT_DEFAULT_RING(struct mtnic_port *priv, u8 port, u16 ring)
00695 {
00696         struct mtnic_if_set_port_default_ring_in_imm def_ring;
00697 
00698         memset(&def_ring, 0, sizeof(def_ring));
00699         def_ring.ring = ring;
00700         return mtnic_cmd(priv->mtnic, &def_ring, NULL, port + 1,
00701                          MTNIC_IF_CMD_SET_PORT_DEFAULT_RING);
00702 }
00703 
00704 static int
00705 mtnic_CONFIG_PORT_RSS_STEER(struct mtnic_port *priv, int port)
00706 {
00707         memset(priv->mtnic->cmd.buf, 0, PAGE_SIZE);
00708         return  mtnic_cmd(priv->mtnic, NULL, NULL, port + 1,
00709                           MTNIC_IF_CMD_CONFIG_PORT_RSS_STEER);
00710 }
00711 
00712 static int
00713 mtnic_SET_PORT_RSS_INDIRECTION(struct mtnic_port *priv, int port)
00714 
00715 {
00716         memset(priv->mtnic->cmd.buf, 0, PAGE_SIZE);
00717         return mtnic_cmd(priv->mtnic, NULL, NULL, port + 1,
00718                          MTNIC_IF_CMD_SET_PORT_RSS_INDIRECTION);
00719 }
00720 
00721 
00722 /*
00723  * Config commands
00724  */
00725 static int
00726 mtnic_CONFIG_CQ(struct mtnic_port *priv, int port,
00727                 u16 cq_ind, struct mtnic_cq *cq)
00728 {
00729         struct mtnic_if_config_cq_in_mbox *config_cq = priv->mtnic->cmd.buf;
00730 
00731         memset(config_cq, 0, sizeof *config_cq);
00732         config_cq->cq = cq_ind;
00733         config_cq->size = fls(UNITS_BUFFER_SIZE - 1);
00734         config_cq->offset = ((cq->dma) & (PAGE_MASK)) >> 6;
00735         config_cq->db_record_addr_l = cpu_to_be32(cq->db_dma);
00736         config_cq->page_address[1] = cpu_to_be32(cq->dma);
00737         DBG("config cq address: %x dma_address: %lx"
00738             "offset: %d size %d index: %d\n"
00739             , config_cq->page_address[1],cq->dma,
00740             config_cq->offset, config_cq->size, config_cq->cq );
00741 
00742         return mtnic_cmd(priv->mtnic, NULL, NULL, port + 1,
00743                          MTNIC_IF_CMD_CONFIG_CQ);
00744 }
00745 
00746 
00747 static int
00748 mtnic_CONFIG_TX_RING(struct mtnic_port *priv, u8 port,
00749                      u16 ring_ind, struct mtnic_ring *ring)
00750 {
00751         struct mtnic_if_config_send_ring_in_mbox *config_tx_ring = priv->mtnic->cmd.buf;
00752         memset(config_tx_ring, 0, sizeof *config_tx_ring);
00753         config_tx_ring->ring = cpu_to_be16(ring_ind);
00754         config_tx_ring->size = fls(UNITS_BUFFER_SIZE - 1);
00755         config_tx_ring->cq = cpu_to_be16(ring->cq);
00756         config_tx_ring->page_address[1] = cpu_to_be32(ring->dma);
00757 
00758         return mtnic_cmd(priv->mtnic, NULL, NULL, port + 1,
00759                          MTNIC_IF_CMD_CONFIG_TX_RING);
00760 }
00761 
00762 static int
00763 mtnic_CONFIG_RX_RING(struct mtnic_port *priv, u8 port,
00764                      u16 ring_ind, struct mtnic_ring *ring)
00765 {
00766         struct mtnic_if_config_rx_ring_in_mbox *config_rx_ring = priv->mtnic->cmd.buf;
00767         memset(config_rx_ring, 0, sizeof *config_rx_ring);
00768         config_rx_ring->ring = ring_ind;
00769         MTNIC_BC_PUT(config_rx_ring->stride_size, fls(UNITS_BUFFER_SIZE - 1),
00770                      MTNIC_MASK_CONFIG_RX_RING_SIZE);
00771         MTNIC_BC_PUT(config_rx_ring->stride_size, 1,
00772                      MTNIC_MASK_CONFIG_RX_RING_STRIDE);
00773         config_rx_ring->cq = cpu_to_be16(ring->cq);
00774         config_rx_ring->db_record_addr_l = cpu_to_be32(ring->db_dma);
00775 
00776         DBG("Config RX ring starting at address:%lx\n", ring->dma);
00777 
00778         config_rx_ring->page_address[1] = cpu_to_be32(ring->dma);
00779 
00780         return mtnic_cmd(priv->mtnic, NULL, NULL, port + 1,
00781                          MTNIC_IF_CMD_CONFIG_RX_RING);
00782 }
00783 
00784 static int
00785 mtnic_CONFIG_EQ(struct mtnic *mtnic)
00786 {
00787         struct mtnic_if_config_eq_in_mbox *eq = mtnic->cmd.buf;
00788 
00789         if (mtnic->eq.dma & (PAGE_MASK)) {
00790                 DBG("misalligned eq buffer:%lx\n",
00791                     mtnic->eq.dma);
00792                 return -EADDRINUSE;
00793         }
00794 
00795         memset(eq, 0, sizeof *eq);
00796         MTNIC_BC_PUT(eq->offset, mtnic->eq.dma >> 6, MTNIC_MASK_CONFIG_EQ_OFFSET);
00797         MTNIC_BC_PUT(eq->size, fls(mtnic->eq.size - 1) - 1, MTNIC_MASK_CONFIG_EQ_SIZE);
00798         MTNIC_BC_PUT(eq->int_vector, 0, MTNIC_MASK_CONFIG_EQ_INT_VEC);
00799         eq->page_address[1] = cpu_to_be32(mtnic->eq.dma);
00800 
00801         return mtnic_cmd(mtnic, NULL, NULL, 0, MTNIC_IF_CMD_CONFIG_EQ);
00802 }
00803 
00804 
00805 
00806 
00807 static int
00808 mtnic_SET_RX_RING_ADDR(struct mtnic_port *priv, u8 port, u64* mac)
00809 {
00810         struct mtnic_if_set_rx_ring_addr_in_imm ring_addr;
00811         u32 modifier = ((u32) port + 1) << 16;
00812 
00813         memset(&ring_addr, 0, sizeof(ring_addr));
00814 
00815         ring_addr.mac_31_0 = cpu_to_be32(*mac & 0xffffffff);
00816         ring_addr.mac_47_32 = cpu_to_be16((*mac >> 32) & 0xffff);
00817         ring_addr.flags_vlan_id |= cpu_to_be16(
00818                                               MTNIC_BC_MASK(MTNIC_MASK_SET_RX_RING_ADDR_BY_MAC));
00819 
00820         return mtnic_cmd(priv->mtnic, &ring_addr, NULL, modifier, MTNIC_IF_CMD_SET_RX_RING_ADDR);
00821 }
00822 
00823 static int
00824 mtnic_SET_PORT_STATE(struct mtnic_port *priv, u8 port, u8 state)
00825 {
00826         struct mtnic_if_set_port_state_in_imm port_state;
00827 
00828         port_state.state = state ? cpu_to_be32(
00829                                               MTNIC_BC_MASK(MTNIC_MASK_CONFIG_PORT_STATE)) : 0;
00830         port_state.reserved = 0;
00831         return mtnic_cmd(priv->mtnic, &port_state, NULL, port + 1,
00832                          MTNIC_IF_CMD_SET_PORT_STATE);
00833 }
00834 
00835 static int
00836 mtnic_SET_PORT_MTU(struct mtnic_port *priv, u8 port, u16 mtu)
00837 {
00838         struct mtnic_if_set_port_mtu_in_imm set_mtu;
00839 
00840         memset(&set_mtu, 0, sizeof(set_mtu));
00841         set_mtu.mtu = cpu_to_be16(mtu);
00842         return mtnic_cmd(priv->mtnic, &set_mtu, NULL, port + 1,
00843                          MTNIC_IF_CMD_SET_PORT_MTU);
00844 }
00845 
00846 /*
00847 static int
00848 mtnic_CONFIG_PORT_VLAN_FILTER(struct mtnic_port *priv, int port)
00849 {
00850         struct mtnic_if_config_port_vlan_filter_in_mbox *vlan_filter = priv->mtnic->cmd.buf;
00851 
00852         // When no vlans are configured we disable the filter
00853         // (i.e., pass all vlans) because we ignore them anyhow
00854         memset(vlan_filter, 0xff, sizeof(*vlan_filter));
00855         return mtnic_cmd(priv->mtnic, NULL, NULL, port + 1,
00856                          MTNIC_IF_CMD_CONFIG_PORT_VLAN_FILTER);
00857 }
00858 */
00859 
00860 
00861 static int
00862 mtnic_RELEASE_RESOURCE(struct mtnic_port *priv, u8 port, u8 type, u8 index)
00863 {
00864         struct mtnic_if_release_resource_in_imm rel;
00865         memset(&rel, 0, sizeof rel);
00866         rel.index = index;
00867         rel.type = type;
00868         return mtnic_cmd ( priv->mtnic,
00869                            &rel, NULL, ( type == MTNIC_IF_RESOURCE_TYPE_EQ ) ?
00870                            0 : port + 1, MTNIC_IF_CMD_RELEASE_RESOURCE );
00871 }
00872 
00873 
00874 static int
00875 mtnic_QUERY_CAP(struct mtnic *mtnic, u8 index, u8 mod, u64 *result)
00876 {
00877         struct mtnic_if_query_cap_in_imm cap;
00878         u32 out_imm[2];
00879         int err;
00880 
00881         memset(&cap, 0, sizeof cap);
00882         cap.cap_index = index;
00883         cap.cap_modifier = mod;
00884         err = mtnic_cmd(mtnic, &cap, &out_imm, 0, MTNIC_IF_CMD_QUERY_CAP);
00885 
00886         *((u32*)result) = be32_to_cpu(*(out_imm+1));
00887         *((u32*)result + 1) = be32_to_cpu(*out_imm);
00888 
00889         DBG("Called Query cap with index:0x%x mod:%d result:0x%llx"
00890             " error:%d\n", index, mod, *result, err);
00891         return err;
00892 }
00893 
00894 
00895 #define DO_QUERY_CAP(cap, mod, var)                             \
00896                 err = mtnic_QUERY_CAP(mtnic, cap, mod, &result);\
00897                 if (err)                                        \
00898                         return err;                             \
00899                 (var) = result
00900 
00901 static int
00902 mtnic_query_num_ports(struct mtnic *mtnic)
00903 {
00904         int err = 0;
00905         u64 result;
00906 
00907         DO_QUERY_CAP(MTNIC_IF_CAP_NUM_PORTS, 0, mtnic->fw.num_ports);
00908 
00909         return 0;
00910 }
00911 
00912 static int
00913 mtnic_query_mac(struct mtnic *mtnic)
00914 {
00915         int err = 0;
00916         int i;
00917         u64 result;
00918 
00919         for (i = 0; i < mtnic->fw.num_ports; i++) {
00920                 DO_QUERY_CAP(MTNIC_IF_CAP_DEFAULT_MAC, i + 1, mtnic->fw.mac[i]);
00921         }
00922 
00923         return 0;
00924 }
00925 
00926 static int
00927 mtnic_query_offsets(struct mtnic *mtnic)
00928 {
00929         int err;
00930         int i;
00931         u64 result;
00932 
00933         DO_QUERY_CAP(MTNIC_IF_CAP_MEM_KEY,
00934                      MTNIC_IF_MEM_TYPE_SNOOP,
00935                      mtnic->fw.mem_type_snoop_be);
00936         mtnic->fw.mem_type_snoop_be = cpu_to_be32(mtnic->fw.mem_type_snoop_be);
00937         DO_QUERY_CAP(MTNIC_IF_CAP_TX_CQ_DB_OFFSET, 0, mtnic->fw.txcq_db_offset);
00938         DO_QUERY_CAP(MTNIC_IF_CAP_EQ_DB_OFFSET, 0, mtnic->fw.eq_db_offset);
00939 
00940         for (i = 0; i < mtnic->fw.num_ports; i++) {
00941                 DO_QUERY_CAP(MTNIC_IF_CAP_CQ_OFFSET, i + 1, mtnic->fw.cq_offset);
00942                 DO_QUERY_CAP(MTNIC_IF_CAP_TX_OFFSET, i + 1, mtnic->fw.tx_offset[i]);
00943                 DO_QUERY_CAP(MTNIC_IF_CAP_RX_OFFSET, i + 1, mtnic->fw.rx_offset[i]);
00944                 DBG("--> Port %d CQ offset:0x%x\n", i, mtnic->fw.cq_offset);
00945                 DBG("--> Port %d Tx offset:0x%x\n", i, mtnic->fw.tx_offset[i]);
00946                 DBG("--> Port %d Rx offset:0x%x\n", i, mtnic->fw.rx_offset[i]);
00947         }
00948 
00949         mdelay(20);
00950         return 0;
00951 }
00952 
00953 
00954 
00955 
00956 
00957 
00958 
00959 
00960 
00961 
00962 
00963 /********************************************************************
00964 *
00965 *       MTNIC initalization functions
00966 *
00967 *
00968 *
00969 *
00970 *********************************************************************/
00971 
00972 /**
00973  * Reset device
00974  */
00975 void
00976 mtnic_reset ( void )
00977 {
00978         void *reset = ioremap ( mtnic_pci_dev.dev.bar[0] + MTNIC_RESET_OFFSET,
00979                                 4 );
00980         writel ( cpu_to_be32 ( 1 ), reset );
00981         iounmap ( reset );
00982 }
00983 
00984 
00985 /**
00986  * Restore PCI config
00987  */
00988 static int
00989 restore_config(void)
00990 {
00991         int i;
00992         int rc;
00993 
00994         for (i = 0; i < 64; ++i) {
00995                 if (i != 22 && i != 23) {
00996                         rc = pci_write_config_dword(mtnic_pci_dev.dev.dev,
00997                                                     i << 2,
00998                                                     mtnic_pci_dev.dev.
00999                                                     dev_config_space[i]);
01000                         if (rc)
01001                                 return rc;
01002                 }
01003         }
01004         return 0;
01005 }
01006 
01007 
01008 
01009 /**
01010  * Init PCI configuration
01011  */
01012 static int
01013 mtnic_init_pci(struct pci_device *dev)
01014 {
01015         int i;
01016         int err;
01017 
01018         /* save bars */
01019         DBG("bus=%d devfn=0x%x\n", dev->bus, dev->devfn);
01020         for (i = 0; i < 6; ++i) {
01021                 mtnic_pci_dev.dev.bar[i] =
01022                 pci_bar_start(dev, PCI_BASE_ADDRESS_0 + (i << 2));
01023                 DBG("bar[%d]= 0x%08lx \n", i, mtnic_pci_dev.dev.bar[i]);
01024         }
01025 
01026         /* save config space */
01027         for (i = 0; i < 64; ++i) {
01028                 err = pci_read_config_dword(dev, i << 2,
01029                                             &mtnic_pci_dev.dev.
01030                                             dev_config_space[i]);
01031                 if (err) {
01032                         DBG("Can not save configuration space");
01033                         return err;
01034                 }
01035         }
01036 
01037         mtnic_pci_dev.dev.dev = dev;
01038 
01039         return 0;
01040 }
01041 
01042 /**
01043  *  Initial hardware
01044  */
01045 static inline
01046 int mtnic_init_card(struct mtnic *mtnic)
01047 {
01048         int err = 0;
01049 
01050 
01051         /* Alloc command interface */
01052         err = mtnic_alloc_cmdif ( mtnic );
01053         if (err) {
01054                 DBG("Failed to init command interface, aborting\n");
01055                 return -EADDRINUSE;
01056         }
01057 
01058 
01059         /**
01060         * Bring up HW
01061         */
01062         err = mtnic_QUERY_FW ( mtnic );
01063         if (err) {
01064                 DBG("QUERY_FW command failed, aborting\n");
01065                 goto cmd_error;
01066         }
01067         DBG("Command interface revision:%d\n", mtnic->fw.ifc_rev);
01068 
01069         /* Allocate memory for FW and start it */
01070         err = mtnic_map_cmd(mtnic, MTNIC_IF_CMD_MAP_FW, mtnic->fw.fw_pages);
01071         if (err) {
01072                 DBG("Eror In MAP_FW\n");
01073                 if (mtnic->fw.fw_pages.buf)
01074                         ufree((intptr_t)mtnic->fw.fw_pages.buf);
01075                 goto cmd_error;
01076         }
01077 
01078         /* Run firmware */
01079         err = mtnic_cmd(mtnic, NULL, NULL, 0, MTNIC_IF_CMD_RUN_FW);
01080         if (err) {
01081                 DBG("Eror In RUN FW\n");
01082                 goto map_fw_error;
01083         }
01084 
01085         DBG("FW version:%d.%d.%d\n",
01086             (u16) (mtnic->fw_ver >> 32),
01087             (u16) ((mtnic->fw_ver >> 16) & 0xffff),
01088             (u16) (mtnic->fw_ver & 0xffff));
01089 
01090 
01091         /* Query num ports */
01092         err = mtnic_query_num_ports(mtnic);
01093         if (err) {
01094                 DBG("Insufficient resources, aborting\n");
01095                 goto map_fw_error;
01096         }
01097 
01098         /* Open NIC */
01099         err = mtnic_OPEN_NIC(mtnic);
01100         if (err) {
01101                 DBG("Failed opening NIC, aborting\n");
01102                 goto map_fw_error;
01103         }
01104 
01105         /* Allocate and map pages worksace */
01106         err = mtnic_map_cmd(mtnic, MTNIC_IF_CMD_MAP_PAGES, mtnic->fw.extra_pages);
01107         if (err) {
01108                 DBG("Couldn't allocate %x FW extra pages, aborting\n",
01109                     mtnic->fw.extra_pages.num);
01110                 if (mtnic->fw.extra_pages.buf)
01111                         ufree((intptr_t)mtnic->fw.extra_pages.buf);
01112                 goto map_fw_error;
01113         }
01114 
01115 
01116         /* Get device information */
01117         err = mtnic_query_mac(mtnic);
01118         if (err) {
01119                 DBG("Insufficient resources in quesry mac, aborting\n");
01120                 goto map_fw_error;
01121         }
01122 
01123         /* Get device offsets */
01124         err = mtnic_query_offsets(mtnic);
01125         if (err) {
01126                 DBG("Failed retrieving resource offests, aborting\n");
01127                 ufree((intptr_t)mtnic->fw.extra_pages.buf);
01128                 goto map_extra_error;
01129         }
01130 
01131 
01132         /* Alloc EQ */
01133         err = mtnic_alloc_eq(mtnic);
01134         if (err) {
01135                 DBG("Failed init shared resources. error: %d\n", err);
01136                 goto map_extra_error;
01137         }
01138 
01139         /* Configure HW */
01140         err = mtnic_CONFIG_EQ(mtnic);
01141         if (err) {
01142                 DBG("Failed configuring EQ\n");
01143                 goto eq_error;
01144         }
01145         err = mtnic_CONFIG_RX(mtnic);
01146         if (err) {
01147                 DBG("Failed Rx configuration\n");
01148                 goto eq_error;
01149         }
01150         err = mtnic_CONFIG_TX(mtnic);
01151         if (err) {
01152                 DBG("Failed Tx configuration\n");
01153                 goto eq_error;
01154         }
01155 
01156 
01157         return 0;
01158 
01159 
01160 eq_error:
01161         iounmap(mtnic->eq_db);
01162         free_memblock(mtnic->eq.buf, mtnic->eq.buf_size);
01163 map_extra_error:
01164         ufree((intptr_t)mtnic->fw.extra_pages.buf);
01165 map_fw_error:
01166         ufree((intptr_t)mtnic->fw.fw_pages.buf);
01167 
01168 cmd_error:
01169         iounmap(mtnic->hcr);
01170         free_memblock(mtnic->cmd.buf, PAGE_SIZE);
01171 
01172         return -EADDRINUSE;
01173 }
01174 
01175 
01176 
01177 
01178 
01179 
01180 
01181 
01182 
01183 
01184 /*******************************************************************
01185 *
01186 * Process functions
01187 *
01188 *       process compliations of TX and RX
01189 *
01190 *
01191 ********************************************************************/
01192 void mtnic_process_tx_cq(struct mtnic_port *priv, struct net_device *dev,
01193                          struct mtnic_cq *cq)
01194 {
01195         struct mtnic_cqe *cqe = cq->buf;
01196         struct mtnic_ring *ring = &priv->tx_ring;
01197         u16 index;
01198 
01199 
01200         index = cq->last & (cq->size-1);
01201         cqe = &cq->buf[index];
01202 
01203         /* Owner bit changes every round */
01204         while (XNOR(cqe->op_tr_own & MTNIC_BIT_CQ_OWN, cq->last & cq->size)) {
01205                 netdev_tx_complete (dev, ring->iobuf[index]);
01206                 ++cq->last;
01207                 index = cq->last & (cq->size-1);
01208                 cqe = &cq->buf[index];
01209         }
01210 
01211         /* Update consumer index */
01212         cq->db->update_ci = cpu_to_be32(cq->last & 0xffffff);
01213         wmb(); /* ensure HW sees CQ consumer before we post new buffers */
01214         ring->cons = cq->last;
01215 }
01216 
01217 
01218 int mtnic_process_rx_cq(struct mtnic_port *priv,
01219                         struct net_device *dev,
01220                         struct mtnic_cq *cq)
01221 {
01222         struct mtnic_cqe *cqe;
01223         struct mtnic_ring *ring = &priv->rx_ring;
01224         int index;
01225         int err;
01226         struct io_buffer *rx_iob;
01227         unsigned int length;
01228 
01229 
01230         /* We assume a 1:1 mapping between CQEs and Rx descriptors, so Rx
01231          * descriptor offset can be deduced from the CQE index instead of
01232          * reading 'cqe->index' */
01233         index = cq->last & (cq->size-1);
01234         cqe = &cq->buf[index];
01235 
01236         /* Process all completed CQEs */
01237         while (XNOR(cqe->op_tr_own & MTNIC_BIT_CQ_OWN, cq->last & cq->size)) {
01238                 /* Drop packet on bad receive or bad checksum */
01239                 if ((cqe->op_tr_own & 0x1f) == MTNIC_OPCODE_ERROR) {
01240                         DBG("CQE completed with error - vendor \n");
01241                         free_iob(ring->iobuf[index]);
01242                         goto next;
01243                 }
01244                 if (cqe->enc_bf & MTNIC_BIT_BAD_FCS) {
01245                         DBG("Accepted packet with bad FCS\n");
01246                         free_iob(ring->iobuf[index]);
01247                         goto next;
01248                 }
01249 
01250                 /*
01251                  * Packet is OK - process it.
01252                  */
01253                 length = be32_to_cpu(cqe->byte_cnt);
01254                 rx_iob = ring->iobuf[index];
01255                 iob_put(rx_iob, length);
01256 
01257                 /* Add this packet to the receive queue. */
01258                 netdev_rx(dev, rx_iob);
01259                 ring->iobuf[index] = NULL;
01260 
01261 next:
01262                 ++cq->last;
01263                 index = cq->last & (cq->size-1);
01264                 cqe = &cq->buf[index];
01265 
01266 
01267 
01268         }
01269 
01270         /* Update consumer index */
01271         cq->db->update_ci = cpu_to_be32(cq->last & 0xffffff);
01272         wmb(); /* ensure HW sees CQ consumer before we post new buffers */
01273         ring->cons = cq->last;
01274 
01275         if (ring->prod - ring->cons < (MAX_GAP_PROD_CONS)) {
01276                 err = mtnic_alloc_iobuf(priv, &priv->rx_ring, DEF_IOBUF_SIZE);
01277                 if (err) {
01278                         DBG("ERROR Allocating io buffer");
01279                         return -EADDRINUSE;
01280                 }
01281         }
01282 
01283         return 0;
01284 }
01285 
01286 
01287 
01288 
01289 
01290 
01291 
01292 
01293 
01294 
01295 
01296 
01297 
01298 
01299 
01300 
01301 /********************************************************************
01302 *
01303 * net_device functions
01304 *
01305 *
01306 *       open, poll, close, probe, disable, irq
01307 *
01308 *********************************************************************/
01309 static int
01310 mtnic_open(struct net_device *dev)
01311 {
01312         struct mtnic_port *priv = netdev_priv(dev);
01313 
01314         int err = 0;
01315         struct mtnic_ring *ring;
01316         struct mtnic_cq *cq;
01317         int cq_ind = 0;
01318         u32 dev_link_state;
01319         int link_check;
01320 
01321         DBG("starting port:%d, MAC Address: 0x%12llx\n",
01322             priv->port, priv->mtnic->fw.mac[priv->port]);
01323 
01324         /* Alloc and configure CQs, TX, RX */
01325         err = mtnic_alloc_resources ( dev );
01326         if (err) {
01327                 DBG("Error allocating resources\n");
01328                 return -EADDRINUSE;
01329         }
01330 
01331         /* Pass CQs configuration to HW */
01332         for (cq_ind = 0; cq_ind < NUM_CQS; ++cq_ind) {
01333                 cq = &priv->cq[cq_ind];
01334                 err = mtnic_CONFIG_CQ(priv, priv->port, cq_ind, cq);
01335                 if (err) {
01336                         DBG("Failed configuring CQ:%d error %d\n",
01337                             cq_ind, err);
01338                         if (cq_ind)
01339                                 goto cq_error;
01340                         else
01341                                 goto allocation_error;
01342                 }
01343                 /* Update consumer index */
01344                 cq->db->update_ci = cpu_to_be32(cq->last & 0xffffff);
01345         }
01346 
01347 
01348 
01349         /* Pass Tx configuration to HW */
01350         ring = &priv->tx_ring;
01351         err = mtnic_CONFIG_TX_RING(priv, priv->port, 0, ring);
01352         if (err) {
01353                 DBG("Failed configuring Tx ring:0\n");
01354                 goto cq_error;
01355         }
01356 
01357         /* Pass RX configuration to HW */
01358         ring = &priv->rx_ring;
01359         err = mtnic_CONFIG_RX_RING(priv, priv->port, 0, ring);
01360         if (err) {
01361                 DBG("Failed configuring Rx ring:0\n");
01362                 goto tx_error;
01363         }
01364 
01365         /* Configure Rx steering */
01366         err = mtnic_CONFIG_PORT_RSS_STEER(priv, priv->port);
01367         if (!err)
01368                 err = mtnic_SET_PORT_RSS_INDIRECTION(priv, priv->port);
01369         if (err) {
01370                 DBG("Failed configuring RSS steering\n");
01371                 goto rx_error;
01372         }
01373 
01374 
01375         /* Set the port default ring to ring 0 */
01376         err = mtnic_SET_PORT_DEFAULT_RING(priv, priv->port, 0);
01377         if (err) {
01378                 DBG("Failed setting default ring\n");
01379                 goto rx_error;
01380         }
01381 
01382         /* Set Mac address */
01383         err = mtnic_SET_RX_RING_ADDR(priv, priv->port, &priv->mtnic->fw.mac[priv->port]);
01384         if (err) {
01385                 DBG("Failed setting default MAC address\n");
01386                 goto rx_error;
01387         }
01388 
01389         /* Set MTU  */
01390         err = mtnic_SET_PORT_MTU(priv, priv->port, DEF_MTU);
01391         if (err) {
01392                 DBG("Failed setting MTU\n");
01393                 goto rx_error;
01394         }
01395 
01396         /* Configure VLAN filter */
01397         /* By adding this function, The second port won't accept packets
01398         err = mtnic_CONFIG_PORT_VLAN_FILTER(priv, priv->port);
01399         if (err) {
01400                 DBG("Failed configuring VLAN filter\n");
01401                 goto rx_error;
01402         }
01403         */
01404 
01405 
01406         /* Bring up physical link */
01407         err = mtnic_SET_PORT_STATE(priv, priv->port, 1);
01408         if (err) {
01409                 DBG("Failed bringing up port\n");
01410                 goto rx_error;
01411         }
01412 
01413         /* PORT IS UP */
01414         priv->state = CARD_UP;
01415 
01416 
01417         /* Checking Link is up */
01418         DBG ( "Checking if link is up\n" );
01419 
01420 
01421         for ( link_check = 0; link_check < CHECK_LINK_TIMES; link_check ++ ) {
01422                 /* Let link state stabilize if cable was connected */
01423                 mdelay ( DELAY_LINK_CHECK );
01424 
01425                 err = mtnic_HEART_BEAT(priv, &dev_link_state);
01426                 if (err) {
01427                         DBG("Failed getting device link state\n");
01428                         return -ENETDOWN;
01429                 }
01430 
01431                 if ( dev_link_state & priv->port ) {
01432                         /* Link is up */
01433                         break;
01434                 }
01435         }
01436 
01437 
01438         if ( ! ( dev_link_state & 0x3 ) ) {
01439                 DBG("Link down, check cables and restart\n");
01440                 netdev_link_down ( dev );
01441                 return -ENETDOWN;
01442         }
01443 
01444         DBG ( "Link is up!\n" );
01445 
01446         /* Mark as link up */
01447         netdev_link_up ( dev );
01448 
01449         return 0;
01450 
01451 rx_error:
01452         err = mtnic_RELEASE_RESOURCE(priv, priv->port,
01453                                      MTNIC_IF_RESOURCE_TYPE_RX_RING, 0);
01454 tx_error:
01455         err |= mtnic_RELEASE_RESOURCE(priv, priv->port,
01456                                       MTNIC_IF_RESOURCE_TYPE_TX_RING, 0);
01457 
01458 cq_error:
01459         while (cq_ind) {
01460                 err |= mtnic_RELEASE_RESOURCE(priv, priv->port,
01461                                               MTNIC_IF_RESOURCE_TYPE_CQ, --cq_ind);
01462         }
01463         if (err)
01464                 DBG("Eror Releasing resources\n");
01465 
01466 allocation_error:
01467 
01468         free_memblock(priv->tx_ring.buf, priv->tx_ring.buf_size);
01469         iounmap(priv->tx_ring.txcq_db);
01470         free_memblock(priv->cq[1].buf, priv->cq[1].buf_size);
01471         free_memblock(priv->cq[1].db, sizeof(struct mtnic_cq_db_record));
01472         free_memblock(priv->rx_ring.buf, priv->rx_ring.buf_size);
01473         free_memblock(priv->rx_ring.db, sizeof(struct mtnic_cq_db_record));
01474         free_memblock(priv->cq[0].buf, priv->cq[0].buf_size);
01475         free_memblock(priv->cq[0].db, sizeof(struct mtnic_cq_db_record));
01476 
01477         mtnic_free_io_buffers(&priv->rx_ring);
01478 
01479         return -ENETDOWN;
01480 }
01481 
01482 
01483 
01484 
01485 /** Check if we got completion for receive and transmit and
01486  * check the line with heart_bit command */
01487 static void
01488 mtnic_poll ( struct net_device *dev )
01489 {
01490         struct mtnic_port *priv = netdev_priv(dev);
01491         struct mtnic_cq *cq;
01492         u32 dev_link_state;
01493         int err;
01494         unsigned int i;
01495 
01496         /* In case of an old error then return */
01497         if (priv->state != CARD_UP)
01498                 return;
01499 
01500         /* We do not check the device every call _poll call,
01501             since it will slow it down */
01502         if ((priv->poll_counter % ROUND_TO_CHECK) == 0) {
01503                 /* Check device */
01504                 err = mtnic_HEART_BEAT(priv, &dev_link_state);
01505                 if (err) {
01506                         DBG("Device has internal error\n");
01507                         priv->state = CARD_LINK_DOWN;
01508                         return;
01509                 }
01510                 if (!(dev_link_state & 0x3)) {
01511                         DBG("Link down, check cables and restart\n");
01512                         priv->state = CARD_LINK_DOWN;
01513                         return;
01514                 }
01515         }
01516         /* Polling CQ */
01517         for (i = 0; i < NUM_CQS; i++) {
01518                 cq = &priv->cq[i]; //Passing on the 2 cqs.
01519 
01520                 if (cq->is_rx) {
01521                         err = mtnic_process_rx_cq(priv, cq->dev, cq);
01522                         if (err) {
01523                                 priv->state = CARD_LINK_DOWN;
01524                                 DBG(" Error allocating RX buffers\n");
01525                                 return;
01526                         }
01527                 } else {
01528                         mtnic_process_tx_cq(priv, cq->dev, cq);
01529                 }
01530         }
01531         ++ priv->poll_counter;
01532 }
01533 
01534 
01535 
01536 static int
01537 mtnic_transmit( struct net_device *dev, struct io_buffer *iobuf )
01538 {
01539 
01540         struct mtnic_port *priv = netdev_priv(dev);
01541         struct mtnic_ring *ring;
01542         struct mtnic_tx_desc *tx_desc;
01543         struct mtnic_data_seg *data;
01544         u32 index;
01545 
01546         /* In case of an error then return */
01547         if (priv->state != CARD_UP)
01548                 return -ENETDOWN;
01549 
01550         ring = &priv->tx_ring;
01551 
01552         index = ring->prod & ring->size_mask;
01553         if ((ring->prod - ring->cons) >= ring->size) {
01554                 DBG("No space left for descriptors!!! cons: %x prod: %x\n",
01555                     ring->cons, ring->prod);
01556                 mdelay(5);
01557                 return -EAGAIN;/* no space left */
01558         }
01559 
01560         /* get current descriptor */
01561         tx_desc = ring->buf + (index * sizeof(struct mtnic_tx_desc));
01562 
01563         /* Prepare Data Seg */
01564         data = &tx_desc->data;
01565         data->addr_l = cpu_to_be32((u32)virt_to_bus(iobuf->data));
01566         data->count = cpu_to_be32(iob_len(iobuf));
01567         data->mem_type = priv->mtnic->fw.mem_type_snoop_be;
01568 
01569         /* Prepare ctrl segement */
01570         tx_desc->ctrl.size_vlan = cpu_to_be32(2);
01571         tx_desc->ctrl.flags = cpu_to_be32(MTNIC_BIT_TX_COMP |
01572                                           MTNIC_BIT_NO_ICRC);
01573         tx_desc->ctrl.op_own = cpu_to_be32(MTNIC_OPCODE_SEND) |
01574                                ((ring->prod & ring->size) ?
01575                                 cpu_to_be32(MTNIC_BIT_DESC_OWN) : 0);
01576 
01577         /* Attach io_buffer */
01578         ring->iobuf[index] = iobuf;
01579 
01580         /* Update producer index */
01581         ++ring->prod;
01582 
01583         /* Ring doorbell! */
01584         wmb();
01585         writel((u32) ring->db_offset, &ring->txcq_db->send_db);
01586 
01587         return 0;
01588 }
01589 
01590 
01591 static void
01592 mtnic_close(struct net_device *dev)
01593 {
01594         struct mtnic_port *priv = netdev_priv(dev);
01595         int err = 0;
01596         DBG("Close called for port:%d\n", priv->port);
01597 
01598         if ( ( priv->state == CARD_UP ) ||
01599              ( priv->state == CARD_LINK_DOWN ) ) {
01600 
01601                 /* Disable port */
01602                 err |= mtnic_SET_PORT_STATE(priv, priv->port, 0);
01603                 /*
01604                  * Stop HW associated with this port
01605                  */
01606                 mdelay(5);
01607 
01608                 /* Stop RX */
01609                 err |= mtnic_RELEASE_RESOURCE(priv, priv->port,
01610                                               MTNIC_IF_RESOURCE_TYPE_RX_RING, 0);
01611 
01612                 /* Stop TX */
01613                 err |= mtnic_RELEASE_RESOURCE(priv, priv->port,
01614                                               MTNIC_IF_RESOURCE_TYPE_TX_RING, 0);
01615 
01616                 /* Stop CQs */
01617                 err |= mtnic_RELEASE_RESOURCE(priv, priv->port,
01618                                               MTNIC_IF_RESOURCE_TYPE_CQ, 0);
01619                 err |= mtnic_RELEASE_RESOURCE(priv, priv->port,
01620                                               MTNIC_IF_RESOURCE_TYPE_CQ, 1);
01621                 if (err) {
01622                         DBG("Close reported error %d\n", err);
01623                 }
01624 
01625                 mdelay ( 10 );
01626 
01627                 /* free memory */
01628                 free_memblock(priv->tx_ring.buf, priv->tx_ring.buf_size);
01629                 iounmap(priv->tx_ring.txcq_db);
01630                 free_memblock(priv->cq[1].buf, priv->cq[1].buf_size);
01631                 free_memblock(priv->cq[1].db, sizeof(struct mtnic_cq_db_record));
01632                 free_memblock(priv->rx_ring.buf, priv->rx_ring.buf_size);
01633                 free_memblock(priv->rx_ring.db, sizeof(struct mtnic_cq_db_record));
01634                 free_memblock(priv->cq[0].buf, priv->cq[0].buf_size);
01635                 free_memblock(priv->cq[0].db, sizeof(struct mtnic_cq_db_record));
01636 
01637                 /* Free RX buffers */
01638                 mtnic_free_io_buffers(&priv->rx_ring);
01639 
01640 
01641 
01642         }
01643 
01644         priv->state = CARD_INITIALIZED;
01645 
01646 }
01647 
01648 
01649 static void
01650 mtnic_disable(struct pci_device *pci)
01651 {
01652 
01653         int err;
01654         int i;
01655         struct mtnic *mtnic = pci_get_drvdata(pci);
01656 
01657 
01658         struct net_device *dev;
01659         struct mtnic_port *priv;
01660 
01661         for ( i = ( mtnic->fw.num_ports - 1 ); i >= 0; i-- ) {
01662 
01663                 dev = mtnic->netdev[i];
01664 
01665                 priv = netdev_priv(dev);
01666 
01667                 /* Just in case */
01668                 if ( ( priv->state == CARD_UP ) ||
01669                      ( priv->state == CARD_LINK_DOWN ) )
01670                         mtnic_close ( dev );
01671         }
01672 
01673         /* Releasing EQ */
01674         priv = netdev_priv ( mtnic->netdev[0] );
01675         err = mtnic_RELEASE_RESOURCE(priv, 1,
01676                                      MTNIC_IF_RESOURCE_TYPE_EQ, 0);
01677 
01678         DBG("Calling MTNIC_CLOSE command\n");
01679         err |= mtnic_cmd(mtnic, NULL, NULL, 0,
01680                          MTNIC_IF_CMD_CLOSE_NIC);
01681         if (err) {
01682                 DBG("Error Releasing resources %d\n", err);
01683         }
01684 
01685         free_memblock(mtnic->cmd.buf, PAGE_SIZE);
01686         iounmap(mtnic->hcr);
01687         ufree((intptr_t)mtnic->fw.fw_pages.buf);
01688         ufree((intptr_t)mtnic->fw.extra_pages.buf);
01689         free_memblock(mtnic->eq.buf, mtnic->eq.buf_size);
01690         iounmap(mtnic->eq_db);
01691 
01692 
01693         for ( i = ( mtnic->fw.num_ports - 1 ); i >= 0; i-- ) {
01694                 dev = mtnic->netdev[i];
01695                 unregister_netdev ( dev );
01696                 netdev_nullify ( dev );
01697                 netdev_put ( dev );
01698         }
01699 
01700         free ( mtnic );
01701 
01702 
01703         mtnic_reset ();
01704         mdelay ( 1000 );
01705         /* Restore config, if we would like to retry booting */
01706         restore_config ();
01707 
01708 
01709 }
01710 
01711 
01712 
01713 static void
01714 mtnic_irq(struct net_device *netdev __unused, int enable __unused)
01715 {
01716         /* Not implemented */
01717 }
01718 
01719 
01720 
01721 /** mtnic net device operations */
01722 static struct net_device_operations mtnic_operations = {
01723         .open       = mtnic_open,
01724         .close      = mtnic_close,
01725         .transmit   = mtnic_transmit,
01726         .poll       = mtnic_poll,
01727         .irq        = mtnic_irq,
01728 };
01729 
01730 
01731 
01732 
01733 
01734 
01735 
01736 static int
01737 mtnic_probe(struct pci_device *pci,
01738             const struct pci_device_id *id __unused)
01739 {
01740         struct mtnic_port *priv;
01741         struct mtnic *mtnic;
01742         int err;
01743         u64 mac;
01744         int port_index;
01745 
01746 
01747         adjust_pci_device(pci);
01748 
01749         err = mtnic_init_pci(pci);
01750         if (err) {
01751                 DBG("Error in pci_init\n");
01752                 return -EIO;
01753         }
01754 
01755         mtnic_reset();
01756         mdelay(1000);
01757 
01758         err = restore_config();
01759         if (err) {
01760                 DBG("Error in restoring config\n");
01761                 return err;
01762         }
01763 
01764         mtnic = zalloc ( sizeof ( *mtnic ) );
01765         if ( ! mtnic ) {
01766                 DBG ( "Error Allocating mtnic buffer\n" );
01767                 return -EADDRINUSE;
01768         }
01769 
01770         pci_set_drvdata(pci, mtnic);
01771 
01772         mtnic->pdev = pci;
01773 
01774 
01775         /* Initialize hardware */
01776         err = mtnic_init_card ( mtnic );
01777         if (err) {
01778                 DBG("Error in init_card\n");
01779                 goto err_init_card;
01780         }
01781 
01782         for ( port_index = 0; port_index < mtnic->fw.num_ports; port_index ++ ) {
01783                 /* Initializing net device */
01784                 mtnic->netdev[port_index] = alloc_etherdev( sizeof ( struct mtnic_port ) );
01785                 if ( mtnic->netdev[port_index] == NULL ) {
01786                         DBG("Net device allocation failed\n");
01787                         goto err_alloc_mtnic;
01788                 }
01789 
01790                 /*
01791                  * Initialize driver private data
01792                  */
01793 
01794                 mtnic->netdev[port_index]->dev = &pci->dev;
01795                 priv = netdev_priv ( mtnic->netdev[port_index] );
01796                 memset ( priv, 0, sizeof ( struct mtnic_port ) );
01797                 priv->mtnic = mtnic;
01798                 priv->netdev = mtnic->netdev[port_index];
01799 
01800                 /* Attach pci device */
01801                 netdev_init(mtnic->netdev[port_index], &mtnic_operations);
01802 
01803                 /* Set port number */
01804                 priv->port = port_index;
01805 
01806                 /* Set state */
01807                 priv->state = CARD_DOWN;
01808         }
01809 
01810 
01811         int mac_idx;
01812         for ( port_index = 0; port_index < mtnic->fw.num_ports; port_index ++ ) {
01813                 priv = netdev_priv ( mtnic->netdev[port_index] );
01814                 /* Program the MAC address */
01815                 mac = priv->mtnic->fw.mac[port_index];
01816                 for (mac_idx = 0; mac_idx < MAC_ADDRESS_SIZE; ++mac_idx) {
01817                         mtnic->netdev[port_index]->hw_addr[MAC_ADDRESS_SIZE - mac_idx - 1] = mac & 0xFF;
01818                         mac = mac >> 8;
01819                 }
01820 
01821                 if ( register_netdev ( mtnic->netdev[port_index] ) ) {
01822                         DBG("Netdev registration failed\n");
01823                         priv->state = CARD_INITIALIZED;
01824                         goto err_alloc_mtnic;
01825                 }
01826         }
01827 
01828 
01829         return 0;
01830 
01831 err_alloc_mtnic:
01832         free ( mtnic );
01833 err_init_card:
01834         return -EIO;
01835 }
01836 
01837 
01838 
01839 
01840 static struct pci_device_id mtnic_nics[] = {
01841         PCI_ROM ( 0x15b3, 0x6368, "mt25448", "Mellanox ConnectX EN driver", 0 ),
01842         PCI_ROM ( 0x15b3, 0x6372, "mt25458", "Mellanox ConnectX ENt driver", 0 ),
01843         PCI_ROM ( 0x15b3, 0x6750, "mt26448", "Mellanox ConnectX EN GEN2 driver", 0 ),
01844         PCI_ROM ( 0x15b3, 0x675a, "mt26458", "Mellanox ConnectX ENt GEN2 driver", 0 ),
01845 };
01846 
01847 struct pci_driver mtnic_driver __pci_driver = {
01848         .ids = mtnic_nics,
01849         .id_count = sizeof(mtnic_nics) / sizeof(mtnic_nics[0]),
01850                     .probe = mtnic_probe,
01851         .remove = mtnic_disable,
01852 };
01853 

Generated on Tue Apr 6 20:01:01 2010 for gPXE by  doxygen 1.5.7.1