1 /*
2  * Copyright (C) 2005 - 2014 Emulex
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License version 2
7  * as published by the Free Software Foundation. The full GNU General
8  * Public License is included in this distribution in the file called COPYING.
9  *
10  * Contact Information:
11  * linux-drivers@emulex.com
12  *
13  * Emulex
14  * 3333 Susan Street
15  * Costa Mesa, CA 92626
16  */
17 
18 #include <linux/mutex.h>
19 #include <linux/list.h>
20 #include <linux/netdevice.h>
21 #include <linux/module.h>
22 
23 #include "be.h"
24 #include "be_cmds.h"
25 
26 static struct ocrdma_driver *ocrdma_drv;
27 static LIST_HEAD(be_adapter_list);
28 static DEFINE_MUTEX(be_adapter_list_lock);
29 
30 static void _be_roce_dev_add(struct be_adapter *adapter)
31 {
32 	struct be_dev_info dev_info;
33 	int i, num_vec;
34 	struct pci_dev *pdev = adapter->pdev;
35 
36 	if (!ocrdma_drv)
37 		return;
38 
39 	if (ocrdma_drv->be_abi_version != BE_ROCE_ABI_VERSION) {
40 		dev_warn(&pdev->dev, "Cannot initialize RoCE due to ocrdma ABI mismatch\n");
41 		return;
42 	}
43 
44 	if (pdev->device == OC_DEVICE_ID5) {
45 		/* only msix is supported on these devices */
46 		if (!msix_enabled(adapter))
47 			return;
48 		/* DPP region address and length */
49 		dev_info.dpp_unmapped_addr = pci_resource_start(pdev, 2);
50 		dev_info.dpp_unmapped_len = pci_resource_len(pdev, 2);
51 	} else {
52 		dev_info.dpp_unmapped_addr = 0;
53 		dev_info.dpp_unmapped_len = 0;
54 	}
55 	dev_info.pdev = adapter->pdev;
56 	dev_info.db = adapter->db;
57 	dev_info.unmapped_db = adapter->roce_db.io_addr;
58 	dev_info.db_page_size = adapter->roce_db.size;
59 	dev_info.db_total_size = adapter->roce_db.total_size;
60 	dev_info.netdev = adapter->netdev;
61 	memcpy(dev_info.mac_addr, adapter->netdev->dev_addr, ETH_ALEN);
62 	dev_info.dev_family = adapter->sli_family;
63 	if (msix_enabled(adapter)) {
64 		/* provide all the vectors, so that EQ creation response
65 		 * can decide which one to use.
66 		 */
67 		num_vec = adapter->num_msix_vec + adapter->num_msix_roce_vec;
68 		dev_info.intr_mode = BE_INTERRUPT_MODE_MSIX;
69 		dev_info.msix.num_vectors = min(num_vec, MAX_MSIX_VECTORS);
70 		/* provide start index of the vector,
71 		 * so in case of linear usage,
72 		 * it can use the base as starting point.
73 		 */
74 		dev_info.msix.start_vector = adapter->num_evt_qs;
75 		for (i = 0; i < dev_info.msix.num_vectors; i++) {
76 			dev_info.msix.vector_list[i] =
77 			    adapter->msix_entries[i].vector;
78 		}
79 	} else {
80 		dev_info.msix.num_vectors = 0;
81 		dev_info.intr_mode = BE_INTERRUPT_MODE_INTX;
82 	}
83 	adapter->ocrdma_dev = ocrdma_drv->add(&dev_info);
84 }
85 
86 void be_roce_dev_add(struct be_adapter *adapter)
87 {
88 	if (be_roce_supported(adapter)) {
89 		INIT_LIST_HEAD(&adapter->entry);
90 		mutex_lock(&be_adapter_list_lock);
91 		list_add_tail(&adapter->entry, &be_adapter_list);
92 
93 		/* invoke add() routine of roce driver only if
94 		 * valid driver registered with add method and add() is not yet
95 		 * invoked on a given adapter.
96 		 */
97 		_be_roce_dev_add(adapter);
98 		mutex_unlock(&be_adapter_list_lock);
99 	}
100 }
101 
102 static void _be_roce_dev_remove(struct be_adapter *adapter)
103 {
104 	if (ocrdma_drv && ocrdma_drv->remove && adapter->ocrdma_dev)
105 		ocrdma_drv->remove(adapter->ocrdma_dev);
106 	adapter->ocrdma_dev = NULL;
107 }
108 
109 void be_roce_dev_remove(struct be_adapter *adapter)
110 {
111 	if (be_roce_supported(adapter)) {
112 		mutex_lock(&be_adapter_list_lock);
113 		_be_roce_dev_remove(adapter);
114 		list_del(&adapter->entry);
115 		mutex_unlock(&be_adapter_list_lock);
116 	}
117 }
118 
119 static void _be_roce_dev_open(struct be_adapter *adapter)
120 {
121 	if (ocrdma_drv && adapter->ocrdma_dev &&
122 	    ocrdma_drv->state_change_handler)
123 		ocrdma_drv->state_change_handler(adapter->ocrdma_dev,
124 						 BE_DEV_UP);
125 }
126 
127 void be_roce_dev_open(struct be_adapter *adapter)
128 {
129 	if (be_roce_supported(adapter)) {
130 		mutex_lock(&be_adapter_list_lock);
131 		_be_roce_dev_open(adapter);
132 		mutex_unlock(&be_adapter_list_lock);
133 	}
134 }
135 
136 static void _be_roce_dev_close(struct be_adapter *adapter)
137 {
138 	if (ocrdma_drv && adapter->ocrdma_dev &&
139 	    ocrdma_drv->state_change_handler)
140 		ocrdma_drv->state_change_handler(adapter->ocrdma_dev,
141 						 BE_DEV_DOWN);
142 }
143 
144 void be_roce_dev_close(struct be_adapter *adapter)
145 {
146 	if (be_roce_supported(adapter)) {
147 		mutex_lock(&be_adapter_list_lock);
148 		_be_roce_dev_close(adapter);
149 		mutex_unlock(&be_adapter_list_lock);
150 	}
151 }
152 
153 void be_roce_dev_shutdown(struct be_adapter *adapter)
154 {
155 	if (be_roce_supported(adapter)) {
156 		mutex_lock(&be_adapter_list_lock);
157 		if (ocrdma_drv && adapter->ocrdma_dev &&
158 		    ocrdma_drv->state_change_handler)
159 			ocrdma_drv->state_change_handler(adapter->ocrdma_dev,
160 							 BE_DEV_SHUTDOWN);
161 		mutex_unlock(&be_adapter_list_lock);
162 	}
163 }
164 
165 int be_roce_register_driver(struct ocrdma_driver *drv)
166 {
167 	struct be_adapter *dev;
168 
169 	mutex_lock(&be_adapter_list_lock);
170 	if (ocrdma_drv) {
171 		mutex_unlock(&be_adapter_list_lock);
172 		return -EINVAL;
173 	}
174 	ocrdma_drv = drv;
175 	list_for_each_entry(dev, &be_adapter_list, entry) {
176 		struct net_device *netdev;
177 
178 		_be_roce_dev_add(dev);
179 		netdev = dev->netdev;
180 		if (netif_running(netdev) && netif_oper_up(netdev))
181 			_be_roce_dev_open(dev);
182 	}
183 	mutex_unlock(&be_adapter_list_lock);
184 	return 0;
185 }
186 EXPORT_SYMBOL(be_roce_register_driver);
187 
188 void be_roce_unregister_driver(struct ocrdma_driver *drv)
189 {
190 	struct be_adapter *dev;
191 
192 	mutex_lock(&be_adapter_list_lock);
193 	list_for_each_entry(dev, &be_adapter_list, entry) {
194 		if (dev->ocrdma_dev)
195 			_be_roce_dev_remove(dev);
196 	}
197 	ocrdma_drv = NULL;
198 	mutex_unlock(&be_adapter_list_lock);
199 }
200 EXPORT_SYMBOL(be_roce_unregister_driver);
201