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 	if (mode == RX)
555a2cc190SJeff Kirsher 		cq->buf_size = cq->size * sizeof(struct mlx4_cqe);
565a2cc190SJeff Kirsher 	else
575a2cc190SJeff Kirsher 		cq->buf_size = sizeof(struct mlx4_cqe);
585a2cc190SJeff Kirsher 
595a2cc190SJeff Kirsher 	cq->ring = ring;
605a2cc190SJeff Kirsher 	cq->is_tx = mode;
615a2cc190SJeff Kirsher 	spin_lock_init(&cq->lock);
625a2cc190SJeff Kirsher 
635a2cc190SJeff Kirsher 	err = mlx4_alloc_hwq_res(mdev->dev, &cq->wqres,
645a2cc190SJeff Kirsher 				cq->buf_size, 2 * PAGE_SIZE);
655a2cc190SJeff Kirsher 	if (err)
665a2cc190SJeff Kirsher 		return err;
675a2cc190SJeff Kirsher 
685a2cc190SJeff Kirsher 	err = mlx4_en_map_buffer(&cq->wqres.buf);
695a2cc190SJeff Kirsher 	if (err)
705a2cc190SJeff Kirsher 		mlx4_free_hwq_res(mdev->dev, &cq->wqres, cq->buf_size);
715a2cc190SJeff Kirsher 	else
725a2cc190SJeff Kirsher 		cq->buf = (struct mlx4_cqe *) cq->wqres.buf.direct.buf;
735a2cc190SJeff Kirsher 
745a2cc190SJeff Kirsher 	return err;
755a2cc190SJeff Kirsher }
765a2cc190SJeff Kirsher 
7776532d0cSAlexander Guller int mlx4_en_activate_cq(struct mlx4_en_priv *priv, struct mlx4_en_cq *cq,
7876532d0cSAlexander Guller 			int cq_idx)
795a2cc190SJeff Kirsher {
805a2cc190SJeff Kirsher 	struct mlx4_en_dev *mdev = priv->mdev;
815a2cc190SJeff Kirsher 	int err = 0;
825a2cc190SJeff Kirsher 	char name[25];
835a2cc190SJeff Kirsher 
845a2cc190SJeff Kirsher 	cq->dev = mdev->pndev[priv->port];
855a2cc190SJeff Kirsher 	cq->mcq.set_ci_db  = cq->wqres.db.db;
865a2cc190SJeff Kirsher 	cq->mcq.arm_db     = cq->wqres.db.db + 1;
875a2cc190SJeff Kirsher 	*cq->mcq.set_ci_db = 0;
885a2cc190SJeff Kirsher 	*cq->mcq.arm_db    = 0;
895a2cc190SJeff Kirsher 	memset(cq->buf, 0, cq->buf_size);
905a2cc190SJeff Kirsher 
915a2cc190SJeff Kirsher 	if (cq->is_tx == RX) {
925a2cc190SJeff Kirsher 		if (mdev->dev->caps.comp_pool) {
935a2cc190SJeff Kirsher 			if (!cq->vector) {
9476532d0cSAlexander Guller 				sprintf(name, "%s-%d", priv->dev->name,
9576532d0cSAlexander Guller 					cq->ring);
9676532d0cSAlexander Guller 				/* Set IRQ for specific name (per ring) */
975a2cc190SJeff Kirsher 				if (mlx4_assign_eq(mdev->dev, name, &cq->vector)) {
9876532d0cSAlexander Guller 					cq->vector = (cq->ring + 1 + priv->port)
9976532d0cSAlexander Guller 					    % mdev->dev->caps.num_comp_vectors;
1005a2cc190SJeff Kirsher 					mlx4_warn(mdev, "Failed Assigning an EQ to "
10176532d0cSAlexander Guller 						  "%s ,Falling back to legacy EQ's\n",
10276532d0cSAlexander Guller 						  name);
1035a2cc190SJeff Kirsher 				}
1045a2cc190SJeff Kirsher 			}
1055a2cc190SJeff Kirsher 		} else {
1065a2cc190SJeff Kirsher 			cq->vector = (cq->ring + 1 + priv->port) %
1075a2cc190SJeff Kirsher 				mdev->dev->caps.num_comp_vectors;
1085a2cc190SJeff Kirsher 		}
1095a2cc190SJeff Kirsher 	} else {
11076532d0cSAlexander Guller 		/* For TX we use the same irq per
11176532d0cSAlexander Guller 		ring we assigned for the RX    */
11276532d0cSAlexander Guller 		struct mlx4_en_cq *rx_cq;
11376532d0cSAlexander Guller 
11476532d0cSAlexander Guller 		cq_idx = cq_idx % priv->rx_ring_num;
11576532d0cSAlexander Guller 		rx_cq = &priv->rx_cq[cq_idx];
11676532d0cSAlexander Guller 		cq->vector = rx_cq->vector;
1175a2cc190SJeff Kirsher 	}
1185a2cc190SJeff Kirsher 
1195a2cc190SJeff Kirsher 	if (!cq->is_tx)
1205a2cc190SJeff Kirsher 		cq->size = priv->rx_ring[cq->ring].actual_size;
1215a2cc190SJeff Kirsher 
1225a2cc190SJeff Kirsher 	err = mlx4_cq_alloc(mdev->dev, cq->size, &cq->wqres.mtt, &mdev->priv_uar,
1235a2cc190SJeff Kirsher 			    cq->wqres.db.dma, &cq->mcq, cq->vector, cq->is_tx);
1245a2cc190SJeff Kirsher 	if (err)
1255a2cc190SJeff Kirsher 		return err;
1265a2cc190SJeff Kirsher 
1275a2cc190SJeff Kirsher 	cq->mcq.comp  = cq->is_tx ? mlx4_en_tx_irq : mlx4_en_rx_irq;
1285a2cc190SJeff Kirsher 	cq->mcq.event = mlx4_en_cq_event;
1295a2cc190SJeff Kirsher 
1305a2cc190SJeff Kirsher 	if (cq->is_tx) {
1315a2cc190SJeff Kirsher 		init_timer(&cq->timer);
1325a2cc190SJeff Kirsher 		cq->timer.function = mlx4_en_poll_tx_cq;
1335a2cc190SJeff Kirsher 		cq->timer.data = (unsigned long) cq;
1345a2cc190SJeff Kirsher 	} else {
1355a2cc190SJeff Kirsher 		netif_napi_add(cq->dev, &cq->napi, mlx4_en_poll_rx_cq, 64);
1365a2cc190SJeff Kirsher 		napi_enable(&cq->napi);
1375a2cc190SJeff Kirsher 	}
1385a2cc190SJeff Kirsher 
1395a2cc190SJeff Kirsher 	return 0;
1405a2cc190SJeff Kirsher }
1415a2cc190SJeff Kirsher 
142fe0af03cSAlexander Guller void mlx4_en_destroy_cq(struct mlx4_en_priv *priv, struct mlx4_en_cq *cq)
1435a2cc190SJeff Kirsher {
1445a2cc190SJeff Kirsher 	struct mlx4_en_dev *mdev = priv->mdev;
1455a2cc190SJeff Kirsher 
1465a2cc190SJeff Kirsher 	mlx4_en_unmap_buffer(&cq->wqres.buf);
1475a2cc190SJeff Kirsher 	mlx4_free_hwq_res(mdev->dev, &cq->wqres, cq->buf_size);
148fe0af03cSAlexander Guller 	if (priv->mdev->dev->caps.comp_pool && cq->vector)
1495a2cc190SJeff Kirsher 		mlx4_release_eq(priv->mdev->dev, cq->vector);
1505a2cc190SJeff Kirsher 	cq->buf_size = 0;
1515a2cc190SJeff Kirsher 	cq->buf = NULL;
1525a2cc190SJeff Kirsher }
1535a2cc190SJeff Kirsher 
1545a2cc190SJeff Kirsher void mlx4_en_deactivate_cq(struct mlx4_en_priv *priv, struct mlx4_en_cq *cq)
1555a2cc190SJeff Kirsher {
1565a2cc190SJeff Kirsher 	struct mlx4_en_dev *mdev = priv->mdev;
1575a2cc190SJeff Kirsher 
1585a2cc190SJeff Kirsher 	if (cq->is_tx)
1595a2cc190SJeff Kirsher 		del_timer(&cq->timer);
1605a2cc190SJeff Kirsher 	else {
1615a2cc190SJeff Kirsher 		napi_disable(&cq->napi);
1625a2cc190SJeff Kirsher 		netif_napi_del(&cq->napi);
1635a2cc190SJeff Kirsher 	}
1645a2cc190SJeff Kirsher 
1655a2cc190SJeff Kirsher 	mlx4_cq_free(mdev->dev, &cq->mcq);
1665a2cc190SJeff Kirsher }
1675a2cc190SJeff Kirsher 
1685a2cc190SJeff Kirsher /* Set rx cq moderation parameters */
1695a2cc190SJeff Kirsher int mlx4_en_set_cq_moder(struct mlx4_en_priv *priv, struct mlx4_en_cq *cq)
1705a2cc190SJeff Kirsher {
1715a2cc190SJeff Kirsher 	return mlx4_cq_modify(priv->mdev->dev, &cq->mcq,
1725a2cc190SJeff Kirsher 			      cq->moder_cnt, cq->moder_time);
1735a2cc190SJeff Kirsher }
1745a2cc190SJeff Kirsher 
1755a2cc190SJeff Kirsher int mlx4_en_arm_cq(struct mlx4_en_priv *priv, struct mlx4_en_cq *cq)
1765a2cc190SJeff Kirsher {
1775a2cc190SJeff Kirsher 	mlx4_cq_arm(&cq->mcq, MLX4_CQ_DB_REQ_NOT, priv->mdev->uar_map,
1785a2cc190SJeff Kirsher 		    &priv->mdev->uar_lock);
1795a2cc190SJeff Kirsher 
1805a2cc190SJeff Kirsher 	return 0;
1815a2cc190SJeff Kirsher }
1825a2cc190SJeff Kirsher 
1835a2cc190SJeff Kirsher 
184