15a2cc190SJeff Kirsher /*
25a2cc190SJeff Kirsher  * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
35a2cc190SJeff Kirsher  *
45a2cc190SJeff Kirsher  * This software is available to you under a choice of one of two
55a2cc190SJeff Kirsher  * licenses.  You may choose to be licensed under the terms of the GNU
65a2cc190SJeff Kirsher  * General Public License (GPL) Version 2, available from the file
75a2cc190SJeff Kirsher  * COPYING in the main directory of this source tree, or the
85a2cc190SJeff Kirsher  * OpenIB.org BSD license below:
95a2cc190SJeff Kirsher  *
105a2cc190SJeff Kirsher  *     Redistribution and use in source and binary forms, with or
115a2cc190SJeff Kirsher  *     without modification, are permitted provided that the following
125a2cc190SJeff Kirsher  *     conditions are met:
135a2cc190SJeff Kirsher  *
145a2cc190SJeff Kirsher  *      - Redistributions of source code must retain the above
155a2cc190SJeff Kirsher  *        copyright notice, this list of conditions and the following
165a2cc190SJeff Kirsher  *        disclaimer.
175a2cc190SJeff Kirsher  *
185a2cc190SJeff Kirsher  *      - Redistributions in binary form must reproduce the above
195a2cc190SJeff Kirsher  *        copyright notice, this list of conditions and the following
205a2cc190SJeff Kirsher  *        disclaimer in the documentation and/or other materials
215a2cc190SJeff Kirsher  *        provided with the distribution.
225a2cc190SJeff Kirsher  *
235a2cc190SJeff Kirsher  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
245a2cc190SJeff Kirsher  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
255a2cc190SJeff Kirsher  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
265a2cc190SJeff Kirsher  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
275a2cc190SJeff Kirsher  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
285a2cc190SJeff Kirsher  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
295a2cc190SJeff Kirsher  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
305a2cc190SJeff Kirsher  * SOFTWARE.
315a2cc190SJeff Kirsher  *
325a2cc190SJeff Kirsher  */
335a2cc190SJeff Kirsher 
345a2cc190SJeff Kirsher #include <linux/mlx4/cq.h>
355a2cc190SJeff Kirsher #include <linux/mlx4/qp.h>
365a2cc190SJeff Kirsher #include <linux/mlx4/cmd.h>
375a2cc190SJeff Kirsher 
385a2cc190SJeff Kirsher #include "mlx4_en.h"
395a2cc190SJeff Kirsher 
405a2cc190SJeff Kirsher static void mlx4_en_cq_event(struct mlx4_cq *cq, enum mlx4_event event)
415a2cc190SJeff Kirsher {
425a2cc190SJeff Kirsher 	return;
435a2cc190SJeff Kirsher }
445a2cc190SJeff Kirsher 
455a2cc190SJeff Kirsher 
465a2cc190SJeff Kirsher int mlx4_en_create_cq(struct mlx4_en_priv *priv,
475a2cc190SJeff Kirsher 		      struct mlx4_en_cq *cq,
485a2cc190SJeff Kirsher 		      int entries, int ring, enum cq_type mode)
495a2cc190SJeff Kirsher {
505a2cc190SJeff Kirsher 	struct mlx4_en_dev *mdev = priv->mdev;
515a2cc190SJeff Kirsher 	int err;
525a2cc190SJeff Kirsher 
535a2cc190SJeff Kirsher 	cq->size = entries;
545a2cc190SJeff Kirsher 	cq->buf_size = cq->size * sizeof(struct mlx4_cqe);
555a2cc190SJeff Kirsher 
565a2cc190SJeff Kirsher 	cq->ring = ring;
575a2cc190SJeff Kirsher 	cq->is_tx = mode;
585a2cc190SJeff Kirsher 	spin_lock_init(&cq->lock);
595a2cc190SJeff Kirsher 
605a2cc190SJeff Kirsher 	err = mlx4_alloc_hwq_res(mdev->dev, &cq->wqres,
615a2cc190SJeff Kirsher 				cq->buf_size, 2 * PAGE_SIZE);
625a2cc190SJeff Kirsher 	if (err)
635a2cc190SJeff Kirsher 		return err;
645a2cc190SJeff Kirsher 
655a2cc190SJeff Kirsher 	err = mlx4_en_map_buffer(&cq->wqres.buf);
665a2cc190SJeff Kirsher 	if (err)
675a2cc190SJeff Kirsher 		mlx4_free_hwq_res(mdev->dev, &cq->wqres, cq->buf_size);
685a2cc190SJeff Kirsher 	else
695a2cc190SJeff Kirsher 		cq->buf = (struct mlx4_cqe *) cq->wqres.buf.direct.buf;
705a2cc190SJeff Kirsher 
715a2cc190SJeff Kirsher 	return err;
725a2cc190SJeff Kirsher }
735a2cc190SJeff Kirsher 
7476532d0cSAlexander Guller int mlx4_en_activate_cq(struct mlx4_en_priv *priv, struct mlx4_en_cq *cq,
7576532d0cSAlexander Guller 			int cq_idx)
765a2cc190SJeff Kirsher {
775a2cc190SJeff Kirsher 	struct mlx4_en_dev *mdev = priv->mdev;
785a2cc190SJeff Kirsher 	int err = 0;
795a2cc190SJeff Kirsher 	char name[25];
805a2cc190SJeff Kirsher 
815a2cc190SJeff Kirsher 	cq->dev = mdev->pndev[priv->port];
825a2cc190SJeff Kirsher 	cq->mcq.set_ci_db  = cq->wqres.db.db;
835a2cc190SJeff Kirsher 	cq->mcq.arm_db     = cq->wqres.db.db + 1;
845a2cc190SJeff Kirsher 	*cq->mcq.set_ci_db = 0;
855a2cc190SJeff Kirsher 	*cq->mcq.arm_db    = 0;
865a2cc190SJeff Kirsher 	memset(cq->buf, 0, cq->buf_size);
875a2cc190SJeff Kirsher 
885a2cc190SJeff Kirsher 	if (cq->is_tx == RX) {
895a2cc190SJeff Kirsher 		if (mdev->dev->caps.comp_pool) {
905a2cc190SJeff Kirsher 			if (!cq->vector) {
9176532d0cSAlexander Guller 				sprintf(name, "%s-%d", priv->dev->name,
9276532d0cSAlexander Guller 					cq->ring);
9376532d0cSAlexander Guller 				/* Set IRQ for specific name (per ring) */
94d9236c3fSAmir Vadai 				if (mlx4_assign_eq(mdev->dev, name, NULL,
95d9236c3fSAmir Vadai 						   &cq->vector)) {
9676532d0cSAlexander Guller 					cq->vector = (cq->ring + 1 + priv->port)
9776532d0cSAlexander Guller 					    % mdev->dev->caps.num_comp_vectors;
985a2cc190SJeff Kirsher 					mlx4_warn(mdev, "Failed Assigning an EQ to "
9976532d0cSAlexander Guller 						  "%s ,Falling back to legacy EQ's\n",
10076532d0cSAlexander Guller 						  name);
1015a2cc190SJeff Kirsher 				}
1025a2cc190SJeff Kirsher 			}
1035a2cc190SJeff Kirsher 		} else {
1045a2cc190SJeff Kirsher 			cq->vector = (cq->ring + 1 + priv->port) %
1055a2cc190SJeff Kirsher 				mdev->dev->caps.num_comp_vectors;
1065a2cc190SJeff Kirsher 		}
1075a2cc190SJeff Kirsher 	} else {
10876532d0cSAlexander Guller 		/* For TX we use the same irq per
10976532d0cSAlexander Guller 		ring we assigned for the RX    */
11076532d0cSAlexander Guller 		struct mlx4_en_cq *rx_cq;
11176532d0cSAlexander Guller 
11276532d0cSAlexander Guller 		cq_idx = cq_idx % priv->rx_ring_num;
11376532d0cSAlexander Guller 		rx_cq = &priv->rx_cq[cq_idx];
11476532d0cSAlexander Guller 		cq->vector = rx_cq->vector;
1155a2cc190SJeff Kirsher 	}
1165a2cc190SJeff Kirsher 
1175a2cc190SJeff Kirsher 	if (!cq->is_tx)
1185a2cc190SJeff Kirsher 		cq->size = priv->rx_ring[cq->ring].actual_size;
1195a2cc190SJeff Kirsher 
1205a2cc190SJeff Kirsher 	err = mlx4_cq_alloc(mdev->dev, cq->size, &cq->wqres.mtt, &mdev->priv_uar,
121f0ab34f0SYevgeny Petrilin 			    cq->wqres.db.dma, &cq->mcq, cq->vector, 0);
1225a2cc190SJeff Kirsher 	if (err)
1235a2cc190SJeff Kirsher 		return err;
1245a2cc190SJeff Kirsher 
1255a2cc190SJeff Kirsher 	cq->mcq.comp  = cq->is_tx ? mlx4_en_tx_irq : mlx4_en_rx_irq;
1265a2cc190SJeff Kirsher 	cq->mcq.event = mlx4_en_cq_event;
1275a2cc190SJeff Kirsher 
128e22979d9SYevgeny Petrilin 	if (!cq->is_tx) {
1295a2cc190SJeff Kirsher 		netif_napi_add(cq->dev, &cq->napi, mlx4_en_poll_rx_cq, 64);
1305a2cc190SJeff Kirsher 		napi_enable(&cq->napi);
1315a2cc190SJeff Kirsher 	}
1325a2cc190SJeff Kirsher 
1335a2cc190SJeff Kirsher 	return 0;
1345a2cc190SJeff Kirsher }
1355a2cc190SJeff Kirsher 
136fe0af03cSAlexander Guller void mlx4_en_destroy_cq(struct mlx4_en_priv *priv, struct mlx4_en_cq *cq)
1375a2cc190SJeff Kirsher {
1385a2cc190SJeff Kirsher 	struct mlx4_en_dev *mdev = priv->mdev;
1395a2cc190SJeff Kirsher 
1405a2cc190SJeff Kirsher 	mlx4_en_unmap_buffer(&cq->wqres.buf);
1415a2cc190SJeff Kirsher 	mlx4_free_hwq_res(mdev->dev, &cq->wqres, cq->buf_size);
142fe0af03cSAlexander Guller 	if (priv->mdev->dev->caps.comp_pool && cq->vector)
1435a2cc190SJeff Kirsher 		mlx4_release_eq(priv->mdev->dev, cq->vector);
144cd3109d2SYevgeny Petrilin 	cq->vector = 0;
1455a2cc190SJeff Kirsher 	cq->buf_size = 0;
1465a2cc190SJeff Kirsher 	cq->buf = NULL;
1475a2cc190SJeff Kirsher }
1485a2cc190SJeff Kirsher 
1495a2cc190SJeff Kirsher void mlx4_en_deactivate_cq(struct mlx4_en_priv *priv, struct mlx4_en_cq *cq)
1505a2cc190SJeff Kirsher {
151e22979d9SYevgeny Petrilin 	if (!cq->is_tx) {
1525a2cc190SJeff Kirsher 		napi_disable(&cq->napi);
1535a2cc190SJeff Kirsher 		netif_napi_del(&cq->napi);
1545a2cc190SJeff Kirsher 	}
1555a2cc190SJeff Kirsher 
156e22979d9SYevgeny Petrilin 	mlx4_cq_free(priv->mdev->dev, &cq->mcq);
1575a2cc190SJeff Kirsher }
1585a2cc190SJeff Kirsher 
1595a2cc190SJeff Kirsher /* Set rx cq moderation parameters */
1605a2cc190SJeff Kirsher int mlx4_en_set_cq_moder(struct mlx4_en_priv *priv, struct mlx4_en_cq *cq)
1615a2cc190SJeff Kirsher {
1625a2cc190SJeff Kirsher 	return mlx4_cq_modify(priv->mdev->dev, &cq->mcq,
1635a2cc190SJeff Kirsher 			      cq->moder_cnt, cq->moder_time);
1645a2cc190SJeff Kirsher }
1655a2cc190SJeff Kirsher 
1665a2cc190SJeff Kirsher int mlx4_en_arm_cq(struct mlx4_en_priv *priv, struct mlx4_en_cq *cq)
1675a2cc190SJeff Kirsher {
1685a2cc190SJeff Kirsher 	mlx4_cq_arm(&cq->mcq, MLX4_CQ_DB_REQ_NOT, priv->mdev->uar_map,
1695a2cc190SJeff Kirsher 		    &priv->mdev->uar_lock);
1705a2cc190SJeff Kirsher 
1715a2cc190SJeff Kirsher 	return 0;
1725a2cc190SJeff Kirsher }
1735a2cc190SJeff Kirsher 
1745a2cc190SJeff Kirsher 
175