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 printk(KERN_DEBUG "fcoe_transport_register:%s\n", t->name);
265 EXPORT_SYMBOL_GPL(fcoe_transport_register);
268 * fcoe_transport_unregister() - Remove the tranport fro the fcoe transports list
269 * @t: ptr to the fcoe transport to be removed
271 * Returns: 0 for success
273 int fcoe_transport_unregister(struct fcoe_transport *t)
275 struct fcoe_transport *tt, *tmp;
277 mutex_lock(&fcoe_transports_lock);
278 list_for_each_entry_safe(tt, tmp, &fcoe_transports, list) {
281 mutex_unlock(&fcoe_transports_lock);
282 fcoe_transport_device_remove_all(t);
283 printk(KERN_DEBUG "fcoe_transport_unregister:%s\n",
288 mutex_unlock(&fcoe_transports_lock);
291 EXPORT_SYMBOL_GPL(fcoe_transport_unregister);
294 * fcoe_load_transport_driver() - Load an offload driver by alias name
295 * @netdev: the target net device
297 * Requests for an offload driver module as the fcoe transport, if fails, it
298 * falls back to use the SW HBA (fcoe_sw) as its transport
301 * 1. supports only PCI device
302 * 2. needs fix for VLAn and bonding
303 * 3. pure hw fcoe hba may not have netdev
305 * Returns: 0 for success
307 int fcoe_load_transport_driver(struct net_device *netdev)
310 struct device *dev = netdev->dev.parent;
312 if (fcoe_transport_lookup(netdev)) {
313 /* load default transport */
314 printk(KERN_DEBUG "fcoe: already loaded transport for %s\n",
319 pci = to_pci_dev(dev);
320 if (dev->bus != &pci_bus_type) {
321 printk(KERN_DEBUG "fcoe: support noly PCI device\n");
324 printk(KERN_DEBUG "fcoe: loading driver fcoe-pci-0x%04x-0x%04x\n",
325 pci->vendor, pci->device);
327 return request_module("fcoe-pci-0x%04x-0x%04x",
328 pci->vendor, pci->device);
331 EXPORT_SYMBOL_GPL(fcoe_load_transport_driver);
334 * fcoe_transport_attach() - Load transport to fcoe
335 * @netdev: the netdev the transport to be attached to
337 * This will look for existing offload driver, if not found, it falls back to
338 * the default sw hba (fcoe_sw) as its fcoe transport.
340 * Returns: 0 for success
342 int fcoe_transport_attach(struct net_device *netdev)
344 struct fcoe_transport *t;
346 /* find the corresponding transport */
347 t = fcoe_transport_lookup(netdev);
349 printk(KERN_DEBUG "fcoe_transport_attach"
350 ":no transport for %s:use %s\n",
351 netdev->name, t->name);
354 /* add to the transport */
355 if (fcoe_transport_device_add(t, netdev)) {
356 printk(KERN_DEBUG "fcoe_transport_attach"
357 ":failed to add %s to tramsport %s\n",
358 netdev->name, t->name);
361 /* transport create function */
365 printk(KERN_DEBUG "fcoe_transport_attach:transport %s for %s\n",
366 t->name, netdev->name);
369 EXPORT_SYMBOL_GPL(fcoe_transport_attach);
372 * fcoe_transport_release() - Unload transport from fcoe
373 * @netdev: the net device on which fcoe is to be released
375 * Returns: 0 for success
377 int fcoe_transport_release(struct net_device *netdev)
379 struct fcoe_transport *t;
381 /* find the corresponding transport */
382 t = fcoe_transport_lookup(netdev);
384 printk(KERN_DEBUG "fcoe_transport_release:"
385 "no transport for %s:use %s\n",
386 netdev->name, t->name);
389 /* remove the device from the transport */
390 if (fcoe_transport_device_remove(t, netdev)) {
391 printk(KERN_DEBUG "fcoe_transport_release:"
392 "failed to add %s to tramsport %s\n",
393 netdev->name, t->name);
396 /* transport destroy function */
400 printk(KERN_DEBUG "fcoe_transport_release:"
401 "device %s dettached from transport %s\n",
402 netdev->name, t->name);
406 EXPORT_SYMBOL_GPL(fcoe_transport_release);
409 * fcoe_transport_init() - Initializes fcoe transport layer
411 * This prepares for the fcoe transport layer
415 int __init fcoe_transport_init(void)
417 INIT_LIST_HEAD(&fcoe_transports);
418 mutex_init(&fcoe_transports_lock);
423 * fcoe_transport_exit() - Cleans up the fcoe transport layer
425 * This cleans up the fcoe transport layer. removing any transport on the list,
426 * note that the transport destroy func is not called here.
430 int __exit fcoe_transport_exit(void)
432 struct fcoe_transport *t, *tmp;
434 mutex_lock(&fcoe_transports_lock);
435 list_for_each_entry_safe(t, tmp, &fcoe_transports, list) {
437 mutex_unlock(&fcoe_transports_lock);
438 fcoe_transport_device_remove_all(t);
439 mutex_lock(&fcoe_transports_lock);
441 mutex_unlock(&fcoe_transports_lock);