blob: 1900bf7e67d1297dc9b24648e99d0ec50f779305 [file] [log] [blame]
Ram Amranicee9fbd2016-10-01 21:59:56 +03001/* QLogic qedr NIC Driver
Mintz, Yuvale8f1cb52017-01-01 13:57:00 +02002 * Copyright (c) 2015-2017 QLogic Corporation
Ram Amranicee9fbd2016-10-01 21:59:56 +03003 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and /or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32#include <linux/pci.h>
33#include <linux/netdevice.h>
34#include <linux/list.h>
35#include <linux/mutex.h>
Michal Kalderonb262a062017-06-20 16:00:03 +030036#include <linux/qed/qede_rdma.h>
Ram Amranicee9fbd2016-10-01 21:59:56 +030037#include "qede.h"
38
39static struct qedr_driver *qedr_drv;
40static LIST_HEAD(qedr_dev_list);
41static DEFINE_MUTEX(qedr_dev_list_lock);
42
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +030043bool qede_rdma_supported(struct qede_dev *dev)
Ram Amranicee9fbd2016-10-01 21:59:56 +030044{
45 return dev->dev_info.common.rdma_supported;
46}
47
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +030048static void _qede_rdma_dev_add(struct qede_dev *edev)
Ram Amranicee9fbd2016-10-01 21:59:56 +030049{
50 if (!qedr_drv)
51 return;
52
53 edev->rdma_info.qedr_dev = qedr_drv->add(edev->cdev, edev->pdev,
54 edev->ndev);
55}
56
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +030057static int qede_rdma_create_wq(struct qede_dev *edev)
Ram Amranicee9fbd2016-10-01 21:59:56 +030058{
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +030059 INIT_LIST_HEAD(&edev->rdma_info.rdma_event_list);
60 edev->rdma_info.rdma_wq = create_singlethread_workqueue("rdma_wq");
61 if (!edev->rdma_info.rdma_wq) {
Ram Amranicee9fbd2016-10-01 21:59:56 +030062 DP_NOTICE(edev, "qedr: Could not create workqueue\n");
63 return -ENOMEM;
64 }
65
66 return 0;
67}
68
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +030069static void qede_rdma_cleanup_event(struct qede_dev *edev)
Ram Amranicee9fbd2016-10-01 21:59:56 +030070{
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +030071 struct list_head *head = &edev->rdma_info.rdma_event_list;
72 struct qede_rdma_event_work *event_node;
Ram Amranicee9fbd2016-10-01 21:59:56 +030073
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +030074 flush_workqueue(edev->rdma_info.rdma_wq);
Ram Amranicee9fbd2016-10-01 21:59:56 +030075 while (!list_empty(head)) {
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +030076 event_node = list_entry(head->next, struct qede_rdma_event_work,
Ram Amranicee9fbd2016-10-01 21:59:56 +030077 list);
78 cancel_work_sync(&event_node->work);
79 list_del(&event_node->list);
80 kfree(event_node);
81 }
82}
83
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +030084static void qede_rdma_destroy_wq(struct qede_dev *edev)
Ram Amranicee9fbd2016-10-01 21:59:56 +030085{
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +030086 qede_rdma_cleanup_event(edev);
87 destroy_workqueue(edev->rdma_info.rdma_wq);
Ram Amranicee9fbd2016-10-01 21:59:56 +030088}
89
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +030090int qede_rdma_dev_add(struct qede_dev *edev)
Ram Amranicee9fbd2016-10-01 21:59:56 +030091{
92 int rc = 0;
93
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +030094 if (qede_rdma_supported(edev)) {
95 rc = qede_rdma_create_wq(edev);
Ram Amranicee9fbd2016-10-01 21:59:56 +030096 if (rc)
97 return rc;
98
99 INIT_LIST_HEAD(&edev->rdma_info.entry);
100 mutex_lock(&qedr_dev_list_lock);
101 list_add_tail(&edev->rdma_info.entry, &qedr_dev_list);
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300102 _qede_rdma_dev_add(edev);
Ram Amranicee9fbd2016-10-01 21:59:56 +0300103 mutex_unlock(&qedr_dev_list_lock);
104 }
105
106 return rc;
107}
108
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300109static void _qede_rdma_dev_remove(struct qede_dev *edev)
Ram Amranicee9fbd2016-10-01 21:59:56 +0300110{
111 if (qedr_drv && qedr_drv->remove && edev->rdma_info.qedr_dev)
112 qedr_drv->remove(edev->rdma_info.qedr_dev);
113 edev->rdma_info.qedr_dev = NULL;
114}
115
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300116void qede_rdma_dev_remove(struct qede_dev *edev)
Ram Amranicee9fbd2016-10-01 21:59:56 +0300117{
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300118 if (!qede_rdma_supported(edev))
Ram Amranicee9fbd2016-10-01 21:59:56 +0300119 return;
120
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300121 qede_rdma_destroy_wq(edev);
Ram Amranicee9fbd2016-10-01 21:59:56 +0300122 mutex_lock(&qedr_dev_list_lock);
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300123 _qede_rdma_dev_remove(edev);
Ram Amranicee9fbd2016-10-01 21:59:56 +0300124 list_del(&edev->rdma_info.entry);
125 mutex_unlock(&qedr_dev_list_lock);
126}
127
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300128static void _qede_rdma_dev_open(struct qede_dev *edev)
Ram Amranicee9fbd2016-10-01 21:59:56 +0300129{
130 if (qedr_drv && edev->rdma_info.qedr_dev && qedr_drv->notify)
131 qedr_drv->notify(edev->rdma_info.qedr_dev, QEDE_UP);
132}
133
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300134static void qede_rdma_dev_open(struct qede_dev *edev)
Ram Amranicee9fbd2016-10-01 21:59:56 +0300135{
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300136 if (!qede_rdma_supported(edev))
Ram Amranicee9fbd2016-10-01 21:59:56 +0300137 return;
138
139 mutex_lock(&qedr_dev_list_lock);
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300140 _qede_rdma_dev_open(edev);
Ram Amranicee9fbd2016-10-01 21:59:56 +0300141 mutex_unlock(&qedr_dev_list_lock);
142}
143
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300144static void _qede_rdma_dev_close(struct qede_dev *edev)
Ram Amranicee9fbd2016-10-01 21:59:56 +0300145{
146 if (qedr_drv && edev->rdma_info.qedr_dev && qedr_drv->notify)
147 qedr_drv->notify(edev->rdma_info.qedr_dev, QEDE_DOWN);
148}
149
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300150static void qede_rdma_dev_close(struct qede_dev *edev)
Ram Amranicee9fbd2016-10-01 21:59:56 +0300151{
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300152 if (!qede_rdma_supported(edev))
Ram Amranicee9fbd2016-10-01 21:59:56 +0300153 return;
154
155 mutex_lock(&qedr_dev_list_lock);
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300156 _qede_rdma_dev_close(edev);
Ram Amranicee9fbd2016-10-01 21:59:56 +0300157 mutex_unlock(&qedr_dev_list_lock);
158}
159
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300160static void qede_rdma_dev_shutdown(struct qede_dev *edev)
Ram Amranicee9fbd2016-10-01 21:59:56 +0300161{
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300162 if (!qede_rdma_supported(edev))
Ram Amranicee9fbd2016-10-01 21:59:56 +0300163 return;
164
165 mutex_lock(&qedr_dev_list_lock);
166 if (qedr_drv && edev->rdma_info.qedr_dev && qedr_drv->notify)
167 qedr_drv->notify(edev->rdma_info.qedr_dev, QEDE_CLOSE);
168 mutex_unlock(&qedr_dev_list_lock);
169}
170
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300171int qede_rdma_register_driver(struct qedr_driver *drv)
Ram Amranicee9fbd2016-10-01 21:59:56 +0300172{
173 struct qede_dev *edev;
174 u8 qedr_counter = 0;
175
176 mutex_lock(&qedr_dev_list_lock);
177 if (qedr_drv) {
178 mutex_unlock(&qedr_dev_list_lock);
179 return -EINVAL;
180 }
181 qedr_drv = drv;
182
183 list_for_each_entry(edev, &qedr_dev_list, rdma_info.entry) {
184 struct net_device *ndev;
185
186 qedr_counter++;
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300187 _qede_rdma_dev_add(edev);
Ram Amranicee9fbd2016-10-01 21:59:56 +0300188 ndev = edev->ndev;
189 if (netif_running(ndev) && netif_oper_up(ndev))
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300190 _qede_rdma_dev_open(edev);
Ram Amranicee9fbd2016-10-01 21:59:56 +0300191 }
192 mutex_unlock(&qedr_dev_list_lock);
193
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300194 pr_notice("qedr: discovered and registered %d RDMA funcs\n",
Amrani, Ram22b1ae62016-11-23 08:03:04 +0000195 qedr_counter);
Ram Amranicee9fbd2016-10-01 21:59:56 +0300196
197 return 0;
198}
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300199EXPORT_SYMBOL(qede_rdma_register_driver);
Ram Amranicee9fbd2016-10-01 21:59:56 +0300200
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300201void qede_rdma_unregister_driver(struct qedr_driver *drv)
Ram Amranicee9fbd2016-10-01 21:59:56 +0300202{
203 struct qede_dev *edev;
204
205 mutex_lock(&qedr_dev_list_lock);
206 list_for_each_entry(edev, &qedr_dev_list, rdma_info.entry) {
207 if (edev->rdma_info.qedr_dev)
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300208 _qede_rdma_dev_remove(edev);
Ram Amranicee9fbd2016-10-01 21:59:56 +0300209 }
210 qedr_drv = NULL;
211 mutex_unlock(&qedr_dev_list_lock);
212}
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300213EXPORT_SYMBOL(qede_rdma_unregister_driver);
Ram Amranicee9fbd2016-10-01 21:59:56 +0300214
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300215static void qede_rdma_changeaddr(struct qede_dev *edev)
Ram Amranicee9fbd2016-10-01 21:59:56 +0300216{
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300217 if (!qede_rdma_supported(edev))
Ram Amranicee9fbd2016-10-01 21:59:56 +0300218 return;
219
220 if (qedr_drv && edev->rdma_info.qedr_dev && qedr_drv->notify)
221 qedr_drv->notify(edev->rdma_info.qedr_dev, QEDE_CHANGE_ADDR);
222}
223
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300224static struct qede_rdma_event_work *
225qede_rdma_get_free_event_node(struct qede_dev *edev)
Ram Amranicee9fbd2016-10-01 21:59:56 +0300226{
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300227 struct qede_rdma_event_work *event_node = NULL;
Ram Amranicee9fbd2016-10-01 21:59:56 +0300228 struct list_head *list_node = NULL;
229 bool found = false;
230
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300231 list_for_each(list_node, &edev->rdma_info.rdma_event_list) {
232 event_node = list_entry(list_node, struct qede_rdma_event_work,
Ram Amranicee9fbd2016-10-01 21:59:56 +0300233 list);
234 if (!work_pending(&event_node->work)) {
235 found = true;
236 break;
237 }
238 }
239
240 if (!found) {
Michal Kalderon090477e2018-05-08 21:29:19 +0300241 event_node = kzalloc(sizeof(*event_node), GFP_ATOMIC);
Ram Amranicee9fbd2016-10-01 21:59:56 +0300242 if (!event_node) {
243 DP_NOTICE(edev,
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300244 "qedr: Could not allocate memory for rdma work\n");
Ram Amranicee9fbd2016-10-01 21:59:56 +0300245 return NULL;
246 }
247 list_add_tail(&event_node->list,
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300248 &edev->rdma_info.rdma_event_list);
Ram Amranicee9fbd2016-10-01 21:59:56 +0300249 }
250
251 return event_node;
252}
253
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300254static void qede_rdma_handle_event(struct work_struct *work)
Ram Amranicee9fbd2016-10-01 21:59:56 +0300255{
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300256 struct qede_rdma_event_work *event_node;
257 enum qede_rdma_event event;
Ram Amranicee9fbd2016-10-01 21:59:56 +0300258 struct qede_dev *edev;
259
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300260 event_node = container_of(work, struct qede_rdma_event_work, work);
Ram Amranicee9fbd2016-10-01 21:59:56 +0300261 event = event_node->event;
262 edev = event_node->ptr;
263
264 switch (event) {
265 case QEDE_UP:
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300266 qede_rdma_dev_open(edev);
Ram Amranicee9fbd2016-10-01 21:59:56 +0300267 break;
268 case QEDE_DOWN:
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300269 qede_rdma_dev_close(edev);
Ram Amranicee9fbd2016-10-01 21:59:56 +0300270 break;
271 case QEDE_CLOSE:
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300272 qede_rdma_dev_shutdown(edev);
Ram Amranicee9fbd2016-10-01 21:59:56 +0300273 break;
274 case QEDE_CHANGE_ADDR:
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300275 qede_rdma_changeaddr(edev);
Ram Amranicee9fbd2016-10-01 21:59:56 +0300276 break;
277 default:
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300278 DP_NOTICE(edev, "Invalid rdma event %d", event);
Ram Amranicee9fbd2016-10-01 21:59:56 +0300279 }
280}
281
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300282static void qede_rdma_add_event(struct qede_dev *edev,
283 enum qede_rdma_event event)
Ram Amranicee9fbd2016-10-01 21:59:56 +0300284{
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300285 struct qede_rdma_event_work *event_node;
Ram Amranicee9fbd2016-10-01 21:59:56 +0300286
287 if (!edev->rdma_info.qedr_dev)
288 return;
289
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300290 event_node = qede_rdma_get_free_event_node(edev);
Ram Amranicee9fbd2016-10-01 21:59:56 +0300291 if (!event_node)
292 return;
293
294 event_node->event = event;
295 event_node->ptr = edev;
296
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300297 INIT_WORK(&event_node->work, qede_rdma_handle_event);
298 queue_work(edev->rdma_info.rdma_wq, &event_node->work);
Ram Amranicee9fbd2016-10-01 21:59:56 +0300299}
300
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300301void qede_rdma_dev_event_open(struct qede_dev *edev)
Ram Amranicee9fbd2016-10-01 21:59:56 +0300302{
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300303 qede_rdma_add_event(edev, QEDE_UP);
Ram Amranicee9fbd2016-10-01 21:59:56 +0300304}
305
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300306void qede_rdma_dev_event_close(struct qede_dev *edev)
Ram Amranicee9fbd2016-10-01 21:59:56 +0300307{
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300308 qede_rdma_add_event(edev, QEDE_DOWN);
Ram Amranicee9fbd2016-10-01 21:59:56 +0300309}
310
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300311void qede_rdma_event_changeaddr(struct qede_dev *edev)
Ram Amranicee9fbd2016-10-01 21:59:56 +0300312{
Michal Kalderonbbfcd1e2017-06-20 16:00:04 +0300313 qede_rdma_add_event(edev, QEDE_CHANGE_ADDR);
Ram Amranicee9fbd2016-10-01 21:59:56 +0300314}