2 * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 * Maintained at www.Open-FCoE.org
20 #include <linux/pci.h>
21 #include <scsi/libfcoe.h>
22 #include <scsi/fc_transport_fcoe.h>
24 /* internal fcoe transport */
25 struct fcoe_transport_internal {
26 struct fcoe_transport *t;
27 struct net_device *netdev;
28 struct list_head list;
31 /* fcoe transports list and its lock */
32 static LIST_HEAD(fcoe_transports);
33 static DEFINE_MUTEX(fcoe_transports_lock);
36 * fcoe_transport_default() - Returns ptr to the default transport fcoe_sw
38 struct fcoe_transport *fcoe_transport_default(void)
40 return &fcoe_sw_transport;
44 * fcoe_transport_to_pcidev() - get the pci dev from a netdev
45 * @netdev: the netdev that pci dev will be retrived from
47 * Returns: NULL or the corrsponding pci_dev
49 struct pci_dev *fcoe_transport_pcidev(const struct net_device *netdev)
51 if (!netdev->dev.parent)
53 return to_pci_dev(netdev->dev.parent);
57 * fcoe_transport_device_lookup() - Lookup a transport
58 * @netdev: the netdev the transport to be attached to
60 * This will look for existing offload driver, if not found, it falls back to
61 * the default sw hba (fcoe_sw) as its fcoe transport.
63 * Returns: 0 for success
65 static struct fcoe_transport_internal *
66 fcoe_transport_device_lookup(struct fcoe_transport *t,
67 struct net_device *netdev)
69 struct fcoe_transport_internal *ti;
71 /* assign the transpor to this device */
72 mutex_lock(&t->devlock);
73 list_for_each_entry(ti, &t->devlist, list) {
74 if (ti->netdev == netdev) {
75 mutex_unlock(&t->devlock);
79 mutex_unlock(&t->devlock);
83 * fcoe_transport_device_add() - Assign a transport to a device
84 * @netdev: the netdev the transport to be attached to
86 * This will look for existing offload driver, if not found, it falls back to
87 * the default sw hba (fcoe_sw) as its fcoe transport.
89 * Returns: 0 for success
91 static int fcoe_transport_device_add(struct fcoe_transport *t,
92 struct net_device *netdev)
94 struct fcoe_transport_internal *ti;
96 ti = fcoe_transport_device_lookup(t, netdev);
98 printk(KERN_DEBUG "fcoe_transport_device_add:"
99 "device %s is already added to transport %s\n",
100 netdev->name, t->name);
103 /* allocate an internal struct to host the netdev and the list */
104 ti = kzalloc(sizeof(*ti), GFP_KERNEL);
110 INIT_LIST_HEAD(&ti->list);
111 dev_hold(ti->netdev);
113 mutex_lock(&t->devlock);
114 list_add(&ti->list, &t->devlist);
115 mutex_unlock(&t->devlock);
117 printk(KERN_DEBUG "fcoe_transport_device_add:"
118 "device %s added to transport %s\n",
119 netdev->name, t->name);
125 * fcoe_transport_device_remove() - Remove a device from its transport
126 * @netdev: the netdev the transport to be attached to
128 * This removes the device from the transport so the given transport will
129 * not manage this device any more
131 * Returns: 0 for success
133 static int fcoe_transport_device_remove(struct fcoe_transport *t,
134 struct net_device *netdev)
136 struct fcoe_transport_internal *ti;
138 ti = fcoe_transport_device_lookup(t, netdev);
140 printk(KERN_DEBUG "fcoe_transport_device_remove:"
141 "device %s is not managed by transport %s\n",
142 netdev->name, t->name);
145 mutex_lock(&t->devlock);
147 mutex_unlock(&t->devlock);
148 printk(KERN_DEBUG "fcoe_transport_device_remove:"
149 "device %s removed from transport %s\n",
150 netdev->name, t->name);
157 * fcoe_transport_device_remove_all() - Remove all from transport devlist
159 * This removes the device from the transport so the given transport will
160 * not manage this device any more
162 * Returns: 0 for success
164 static void fcoe_transport_device_remove_all(struct fcoe_transport *t)
166 struct fcoe_transport_internal *ti, *tmp;
168 mutex_lock(&t->devlock);
169 list_for_each_entry_safe(ti, tmp, &t->devlist, list) {
173 mutex_unlock(&t->devlock);
177 * fcoe_transport_match() - Use the bus device match function to match the hw
178 * @t: The fcoe transport to check
179 * @netdev: The netdev to match against
181 * This function is used to check if the given transport wants to manage the
182 * input netdev. if the transports implements the match function, it will be
183 * called, o.w. we just compare the pci vendor and device id.
185 * Returns: true for match up
187 static bool fcoe_transport_match(struct fcoe_transport *t,
188 struct net_device *netdev)
190 /* match transport by vendor and device id */
193 pci = fcoe_transport_pcidev(netdev);
196 printk(KERN_DEBUG "fcoe_transport_match:"
197 "%s:%x:%x -- %s:%x:%x\n",
198 t->name, t->vendor, t->device,
199 netdev->name, pci->vendor, pci->device);
201 /* if transport supports match */
203 return t->match(netdev);
205 /* else just compare the vendor and device id: pci only */
206 return (t->vendor == pci->vendor) && (t->device == pci->device);
212 * fcoe_transport_lookup() - Check if the transport is already registered
213 * @t: the transport to be looked up
215 * This compares the parent device (pci) vendor and device id
217 * Returns: NULL if not found
219 * TODO: return default sw transport if no other transport is found
221 static struct fcoe_transport *
222 fcoe_transport_lookup(struct net_device *netdev)
224 struct fcoe_transport *t;
226 mutex_lock(&fcoe_transports_lock);
227 list_for_each_entry(t, &fcoe_transports, list) {
228 if (fcoe_transport_match(t, netdev)) {
229 mutex_unlock(&fcoe_transports_lock);
233 mutex_unlock(&fcoe_transports_lock);
235 printk(KERN_DEBUG "fcoe_transport_lookup:"
236 "use default transport for %s\n", netdev->name);
237 return fcoe_transport_default();
241 * fcoe_transport_register() - Adds a fcoe transport to the fcoe transports list
242 * @t: ptr to the fcoe transport to be added
244 * Returns: 0 for success
246 int fcoe_transport_register(struct fcoe_transport *t)
248 struct fcoe_transport *tt;
250 /* TODO - add fcoe_transport specific initialization here */
251 mutex_lock(&fcoe_transports_lock);
252 list_for_each_entry(tt, &fcoe_transports, list) {
254 mutex_unlock(&fcoe_transports_lock);
258 list_add_tail(&t->list, &fcoe_transports);
259 mutex_unlock(&fcoe_transports_lock);
261 mutex_init(&t->devlock);
262 INIT_LIST_HEAD(&t->devlist);
264 printk(KERN_DEBUG "fcoe_transport_register:%s\n", t->name);
268 EXPORT_SYMBOL_GPL(fcoe_transport_register);
271 * fcoe_transport_unregister() - Remove the tranport fro the fcoe transports list
272 * @t: ptr to the fcoe transport to be removed
274 * Returns: 0 for success
276 int fcoe_transport_unregister(struct fcoe_transport *t)
278 struct fcoe_transport *tt, *tmp;
280 mutex_lock(&fcoe_transports_lock);
281 list_for_each_entry_safe(tt, tmp, &fcoe_transports, list) {
284 mutex_unlock(&fcoe_transports_lock);
285 fcoe_transport_device_remove_all(t);
286 printk(KERN_DEBUG "fcoe_transport_unregister:%s\n",
291 mutex_unlock(&fcoe_transports_lock);
294 EXPORT_SYMBOL_GPL(fcoe_transport_unregister);
297 * fcoe_load_transport_driver() - Load an offload driver by alias name
298 * @netdev: the target net device
300 * Requests for an offload driver module as the fcoe transport, if fails, it
301 * falls back to use the SW HBA (fcoe_sw) as its transport
304 * 1. supports only PCI device
305 * 2. needs fix for VLAn and bonding
306 * 3. pure hw fcoe hba may not have netdev
308 * Returns: 0 for success
310 int fcoe_load_transport_driver(struct net_device *netdev)
313 struct device *dev = netdev->dev.parent;
315 if (fcoe_transport_lookup(netdev)) {
316 /* load default transport */
317 printk(KERN_DEBUG "fcoe: already loaded transport for %s\n",
322 pci = to_pci_dev(dev);
323 if (dev->bus != &pci_bus_type) {
324 printk(KERN_DEBUG "fcoe: support noly PCI device\n");
327 printk(KERN_DEBUG "fcoe: loading driver fcoe-pci-0x%04x-0x%04x\n",
328 pci->vendor, pci->device);
330 return request_module("fcoe-pci-0x%04x-0x%04x",
331 pci->vendor, pci->device);
334 EXPORT_SYMBOL_GPL(fcoe_load_transport_driver);
337 * fcoe_transport_attach() - Load transport to fcoe
338 * @netdev: the netdev the transport to be attached to
340 * This will look for existing offload driver, if not found, it falls back to
341 * the default sw hba (fcoe_sw) as its fcoe transport.
343 * Returns: 0 for success
345 int fcoe_transport_attach(struct net_device *netdev)
347 struct fcoe_transport *t;
349 /* find the corresponding transport */
350 t = fcoe_transport_lookup(netdev);
352 printk(KERN_DEBUG "fcoe_transport_attach"
353 ":no transport for %s:use %s\n",
354 netdev->name, t->name);
357 /* add to the transport */
358 if (fcoe_transport_device_add(t, netdev)) {
359 printk(KERN_DEBUG "fcoe_transport_attach"
360 ":failed to add %s to tramsport %s\n",
361 netdev->name, t->name);
364 /* transport create function */
368 printk(KERN_DEBUG "fcoe_transport_attach:transport %s for %s\n",
369 t->name, netdev->name);
372 EXPORT_SYMBOL_GPL(fcoe_transport_attach);
375 * fcoe_transport_release() - Unload transport from fcoe
376 * @netdev: the net device on which fcoe is to be released
378 * Returns: 0 for success
380 int fcoe_transport_release(struct net_device *netdev)
382 struct fcoe_transport *t;
384 /* find the corresponding transport */
385 t = fcoe_transport_lookup(netdev);
387 printk(KERN_DEBUG "fcoe_transport_release:"
388 "no transport for %s:use %s\n",
389 netdev->name, t->name);
392 /* remove the device from the transport */
393 if (fcoe_transport_device_remove(t, netdev)) {
394 printk(KERN_DEBUG "fcoe_transport_release:"
395 "failed to add %s to tramsport %s\n",
396 netdev->name, t->name);
399 /* transport destroy function */
403 printk(KERN_DEBUG "fcoe_transport_release:"
404 "device %s dettached from transport %s\n",
405 netdev->name, t->name);
409 EXPORT_SYMBOL_GPL(fcoe_transport_release);
412 * fcoe_transport_init() - Initializes fcoe transport layer
414 * This prepares for the fcoe transport layer
418 int __init fcoe_transport_init(void)
420 INIT_LIST_HEAD(&fcoe_transports);
421 mutex_init(&fcoe_transports_lock);
426 * fcoe_transport_exit() - Cleans up the fcoe transport layer
428 * This cleans up the fcoe transport layer. removing any transport on the list,
429 * note that the transport destroy func is not called here.
433 int __exit fcoe_transport_exit(void)
435 struct fcoe_transport *t, *tmp;
437 mutex_lock(&fcoe_transports_lock);
438 list_for_each_entry_safe(t, tmp, &fcoe_transports, list) {
440 mutex_unlock(&fcoe_transports_lock);
441 fcoe_transport_device_remove_all(t);
442 mutex_lock(&fcoe_transports_lock);
444 mutex_unlock(&fcoe_transports_lock);