qede_rdma.c 7.8 KB
Newer Older
R
Ram Amrani 已提交
1
/* QLogic qedr NIC Driver
M
Mintz, Yuval 已提交
2
 * Copyright (c) 2015-2017  QLogic Corporation
R
Ram Amrani 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and /or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include <linux/pci.h>
#include <linux/netdevice.h>
#include <linux/list.h>
#include <linux/mutex.h>
36
#include <linux/qed/qede_rdma.h>
R
Ram Amrani 已提交
37 38 39 40 41 42
#include "qede.h"

static struct qedr_driver *qedr_drv;
static LIST_HEAD(qedr_dev_list);
static DEFINE_MUTEX(qedr_dev_list_lock);

43
bool qede_rdma_supported(struct qede_dev *dev)
R
Ram Amrani 已提交
44 45 46 47
{
	return dev->dev_info.common.rdma_supported;
}

48
static void _qede_rdma_dev_add(struct qede_dev *edev)
R
Ram Amrani 已提交
49 50 51 52 53 54 55 56
{
	if (!qedr_drv)
		return;

	edev->rdma_info.qedr_dev = qedr_drv->add(edev->cdev, edev->pdev,
						 edev->ndev);
}

57
static int qede_rdma_create_wq(struct qede_dev *edev)
R
Ram Amrani 已提交
58
{
59 60 61
	INIT_LIST_HEAD(&edev->rdma_info.rdma_event_list);
	edev->rdma_info.rdma_wq = create_singlethread_workqueue("rdma_wq");
	if (!edev->rdma_info.rdma_wq) {
R
Ram Amrani 已提交
62 63 64 65 66 67 68
		DP_NOTICE(edev, "qedr: Could not create workqueue\n");
		return -ENOMEM;
	}

	return 0;
}

69
static void qede_rdma_cleanup_event(struct qede_dev *edev)
R
Ram Amrani 已提交
70
{
71 72
	struct list_head *head = &edev->rdma_info.rdma_event_list;
	struct qede_rdma_event_work *event_node;
R
Ram Amrani 已提交
73

74
	flush_workqueue(edev->rdma_info.rdma_wq);
R
Ram Amrani 已提交
75
	while (!list_empty(head)) {
76
		event_node = list_entry(head->next, struct qede_rdma_event_work,
R
Ram Amrani 已提交
77 78 79 80 81 82 83
					list);
		cancel_work_sync(&event_node->work);
		list_del(&event_node->list);
		kfree(event_node);
	}
}

84
static void qede_rdma_destroy_wq(struct qede_dev *edev)
R
Ram Amrani 已提交
85
{
86 87
	qede_rdma_cleanup_event(edev);
	destroy_workqueue(edev->rdma_info.rdma_wq);
R
Ram Amrani 已提交
88 89
}

90
int qede_rdma_dev_add(struct qede_dev *edev)
R
Ram Amrani 已提交
91
{
92
	int rc = 0;
R
Ram Amrani 已提交
93

94 95 96 97
	if (qede_rdma_supported(edev)) {
		rc = qede_rdma_create_wq(edev);
		if (rc)
			return rc;
T
Tomer Tayar 已提交
98

99 100 101 102 103 104
		INIT_LIST_HEAD(&edev->rdma_info.entry);
		mutex_lock(&qedr_dev_list_lock);
		list_add_tail(&edev->rdma_info.entry, &qedr_dev_list);
		_qede_rdma_dev_add(edev);
		mutex_unlock(&qedr_dev_list_lock);
	}
R
Ram Amrani 已提交
105 106 107 108

	return rc;
}

109
static void _qede_rdma_dev_remove(struct qede_dev *edev)
R
Ram Amrani 已提交
110 111 112
{
	if (qedr_drv && qedr_drv->remove && edev->rdma_info.qedr_dev)
		qedr_drv->remove(edev->rdma_info.qedr_dev);
113
	edev->rdma_info.qedr_dev = NULL;
R
Ram Amrani 已提交
114 115
}

116
void qede_rdma_dev_remove(struct qede_dev *edev)
R
Ram Amrani 已提交
117
{
118
	if (!qede_rdma_supported(edev))
R
Ram Amrani 已提交
119 120
		return;

121 122 123 124 125
	qede_rdma_destroy_wq(edev);
	mutex_lock(&qedr_dev_list_lock);
	_qede_rdma_dev_remove(edev);
	list_del(&edev->rdma_info.entry);
	mutex_unlock(&qedr_dev_list_lock);
R
Ram Amrani 已提交
126 127
}

128
static void _qede_rdma_dev_open(struct qede_dev *edev)
R
Ram Amrani 已提交
129 130 131 132 133
{
	if (qedr_drv && edev->rdma_info.qedr_dev && qedr_drv->notify)
		qedr_drv->notify(edev->rdma_info.qedr_dev, QEDE_UP);
}

134
static void qede_rdma_dev_open(struct qede_dev *edev)
R
Ram Amrani 已提交
135
{
136
	if (!qede_rdma_supported(edev))
R
Ram Amrani 已提交
137 138 139
		return;

	mutex_lock(&qedr_dev_list_lock);
140
	_qede_rdma_dev_open(edev);
R
Ram Amrani 已提交
141 142 143
	mutex_unlock(&qedr_dev_list_lock);
}

144
static void _qede_rdma_dev_close(struct qede_dev *edev)
R
Ram Amrani 已提交
145 146 147 148 149
{
	if (qedr_drv && edev->rdma_info.qedr_dev && qedr_drv->notify)
		qedr_drv->notify(edev->rdma_info.qedr_dev, QEDE_DOWN);
}

150
static void qede_rdma_dev_close(struct qede_dev *edev)
R
Ram Amrani 已提交
151
{
152
	if (!qede_rdma_supported(edev))
R
Ram Amrani 已提交
153 154 155
		return;

	mutex_lock(&qedr_dev_list_lock);
156
	_qede_rdma_dev_close(edev);
R
Ram Amrani 已提交
157 158 159
	mutex_unlock(&qedr_dev_list_lock);
}

160
static void qede_rdma_dev_shutdown(struct qede_dev *edev)
R
Ram Amrani 已提交
161
{
162
	if (!qede_rdma_supported(edev))
R
Ram Amrani 已提交
163 164 165 166 167 168 169 170
		return;

	mutex_lock(&qedr_dev_list_lock);
	if (qedr_drv && edev->rdma_info.qedr_dev && qedr_drv->notify)
		qedr_drv->notify(edev->rdma_info.qedr_dev, QEDE_CLOSE);
	mutex_unlock(&qedr_dev_list_lock);
}

171
int qede_rdma_register_driver(struct qedr_driver *drv)
R
Ram Amrani 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
{
	struct qede_dev *edev;
	u8 qedr_counter = 0;

	mutex_lock(&qedr_dev_list_lock);
	if (qedr_drv) {
		mutex_unlock(&qedr_dev_list_lock);
		return -EINVAL;
	}
	qedr_drv = drv;

	list_for_each_entry(edev, &qedr_dev_list, rdma_info.entry) {
		struct net_device *ndev;

		qedr_counter++;
187
		_qede_rdma_dev_add(edev);
R
Ram Amrani 已提交
188 189
		ndev = edev->ndev;
		if (netif_running(ndev) && netif_oper_up(ndev))
190
			_qede_rdma_dev_open(edev);
R
Ram Amrani 已提交
191 192 193
	}
	mutex_unlock(&qedr_dev_list_lock);

194
	pr_notice("qedr: discovered and registered %d RDMA funcs\n",
195
		  qedr_counter);
R
Ram Amrani 已提交
196 197 198

	return 0;
}
199
EXPORT_SYMBOL(qede_rdma_register_driver);
R
Ram Amrani 已提交
200

201
void qede_rdma_unregister_driver(struct qedr_driver *drv)
R
Ram Amrani 已提交
202 203 204 205 206
{
	struct qede_dev *edev;

	mutex_lock(&qedr_dev_list_lock);
	list_for_each_entry(edev, &qedr_dev_list, rdma_info.entry) {
207
		if (edev->rdma_info.qedr_dev)
208
			_qede_rdma_dev_remove(edev);
R
Ram Amrani 已提交
209 210 211 212
	}
	qedr_drv = NULL;
	mutex_unlock(&qedr_dev_list_lock);
}
213
EXPORT_SYMBOL(qede_rdma_unregister_driver);
R
Ram Amrani 已提交
214

215
static void qede_rdma_changeaddr(struct qede_dev *edev)
R
Ram Amrani 已提交
216
{
217
	if (!qede_rdma_supported(edev))
R
Ram Amrani 已提交
218 219 220 221 222 223
		return;

	if (qedr_drv && edev->rdma_info.qedr_dev && qedr_drv->notify)
		qedr_drv->notify(edev->rdma_info.qedr_dev, QEDE_CHANGE_ADDR);
}

224 225
static struct qede_rdma_event_work *
qede_rdma_get_free_event_node(struct qede_dev *edev)
R
Ram Amrani 已提交
226
{
227
	struct qede_rdma_event_work *event_node = NULL;
R
Ram Amrani 已提交
228 229 230
	struct list_head *list_node = NULL;
	bool found = false;

231 232
	list_for_each(list_node, &edev->rdma_info.rdma_event_list) {
		event_node = list_entry(list_node, struct qede_rdma_event_work,
R
Ram Amrani 已提交
233 234 235 236 237 238 239 240
					list);
		if (!work_pending(&event_node->work)) {
			found = true;
			break;
		}
	}

	if (!found) {
241
		event_node = kzalloc(sizeof(*event_node), GFP_ATOMIC);
R
Ram Amrani 已提交
242 243
		if (!event_node) {
			DP_NOTICE(edev,
244
				  "qedr: Could not allocate memory for rdma work\n");
R
Ram Amrani 已提交
245 246 247
			return NULL;
		}
		list_add_tail(&event_node->list,
248
			      &edev->rdma_info.rdma_event_list);
R
Ram Amrani 已提交
249 250 251 252 253
	}

	return event_node;
}

254
static void qede_rdma_handle_event(struct work_struct *work)
R
Ram Amrani 已提交
255
{
256 257
	struct qede_rdma_event_work *event_node;
	enum qede_rdma_event event;
R
Ram Amrani 已提交
258 259
	struct qede_dev *edev;

260
	event_node = container_of(work, struct qede_rdma_event_work, work);
R
Ram Amrani 已提交
261 262 263 264 265
	event = event_node->event;
	edev = event_node->ptr;

	switch (event) {
	case QEDE_UP:
266
		qede_rdma_dev_open(edev);
R
Ram Amrani 已提交
267 268
		break;
	case QEDE_DOWN:
269
		qede_rdma_dev_close(edev);
R
Ram Amrani 已提交
270 271
		break;
	case QEDE_CLOSE:
272
		qede_rdma_dev_shutdown(edev);
R
Ram Amrani 已提交
273 274
		break;
	case QEDE_CHANGE_ADDR:
275
		qede_rdma_changeaddr(edev);
R
Ram Amrani 已提交
276 277
		break;
	default:
278
		DP_NOTICE(edev, "Invalid rdma event %d", event);
R
Ram Amrani 已提交
279 280 281
	}
}

282 283
static void qede_rdma_add_event(struct qede_dev *edev,
				enum qede_rdma_event event)
R
Ram Amrani 已提交
284
{
285
	struct qede_rdma_event_work *event_node;
R
Ram Amrani 已提交
286 287 288 289

	if (!edev->rdma_info.qedr_dev)
		return;

290
	event_node = qede_rdma_get_free_event_node(edev);
R
Ram Amrani 已提交
291 292 293 294 295 296
	if (!event_node)
		return;

	event_node->event = event;
	event_node->ptr = edev;

297 298
	INIT_WORK(&event_node->work, qede_rdma_handle_event);
	queue_work(edev->rdma_info.rdma_wq, &event_node->work);
R
Ram Amrani 已提交
299 300
}

301
void qede_rdma_dev_event_open(struct qede_dev *edev)
R
Ram Amrani 已提交
302
{
303
	qede_rdma_add_event(edev, QEDE_UP);
R
Ram Amrani 已提交
304 305
}

306
void qede_rdma_dev_event_close(struct qede_dev *edev)
R
Ram Amrani 已提交
307
{
308
	qede_rdma_add_event(edev, QEDE_DOWN);
R
Ram Amrani 已提交
309 310
}

311
void qede_rdma_event_changeaddr(struct qede_dev *edev)
R
Ram Amrani 已提交
312
{
313
	qede_rdma_add_event(edev, QEDE_CHANGE_ADDR);
R
Ram Amrani 已提交
314
}