aboutsummaryrefslogtreecommitdiff
path: root/drivers/scsi/fcoe/fc_transport_fcoe.c
blob: bf7fe6fc0820491ce90aebbbbecc16b565aacce1 (plain)
1
2
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
 * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Maintained at www.Open-FCoE.org
 */

#include <linux/pci.h>
#include <scsi/libfcoe.h>
#include <scsi/fc_transport_fcoe.h>

/* internal fcoe transport */
struct fcoe_transport_internal {
	struct fcoe_transport *t;
	struct net_device *netdev;
	struct list_head list;
};

/* fcoe transports list and its lock */
static LIST_HEAD(fcoe_transports);
static DEFINE_MUTEX(fcoe_transports_lock);

/**
 * fcoe_transport_default - returns ptr to the default transport fcoe_sw
 **/
struct fcoe_transport *fcoe_transport_default(void)
{
	return &fcoe_sw_transport;
}

/**
 * fcoe_transport_to_pcidev - get the pci dev from a netdev
 * @netdev: the netdev that pci dev will be retrived from
 *
 * Returns: NULL or the corrsponding pci_dev
 **/
struct pci_dev *fcoe_transport_pcidev(const struct net_device *netdev)
{
	if (!netdev->dev.parent)
		return NULL;
	return to_pci_dev(netdev->dev.parent);
}

/**
 * fcoe_transport_device_lookup - find out netdev is managed by the
 * transport
 * assign a transport to a device
 * @netdev: the netdev the transport to be attached to
 *
 * This will look for existing offload driver, if not found, it falls back to
 * the default sw hba (fcoe_sw) as its fcoe transport.
 *
 * Returns: 0 for success
 **/
static struct fcoe_transport_internal *fcoe_transport_device_lookup(
	struct fcoe_transport *t, struct net_device *netdev)
{
	struct fcoe_transport_internal *ti;

	/* assign the transpor to this device */
	mutex_lock(&t->devlock);
	list_for_each_entry(ti, &t->devlist, list) {
		if (ti->netdev == netdev) {
			mutex_unlock(&t->devlock);
			return ti;
		}
	}
	mutex_unlock(&t->devlock);
	return NULL;
}
/**
 * fcoe_transport_device_add - assign a transport to a device
 * @netdev: the netdev the transport to be attached to
 *
 * This will look for existing offload driver, if not found, it falls back to
 * the default sw hba (fcoe_sw) as its fcoe transport.
 *
 * Returns: 0 for success
 **/
static int fcoe_transport_device_add(struct fcoe_transport *t,
				     struct net_device *netdev)
{
	struct fcoe_transport_internal *ti;

	ti = fcoe_transport_device_lookup(t, netdev);
	if (ti) {
		printk(KERN_DEBUG "fcoe_transport_device_add:"
		       "device %s is already added to transport %s\n",
		       netdev->name, t->name);
		return -EEXIST;
	}
	/* allocate an internal struct to host the netdev and the list */
	ti = kzalloc(sizeof(*ti), GFP_KERNEL);
	if (!ti)
		return -ENOMEM;

	ti->t = t;
	ti->netdev = netdev;
	INIT_LIST_HEAD(&ti->list);
	dev_hold(ti->netdev);

	mutex_lock(&t->devlock);
	list_add(&ti->list, &t->devlist);
	mutex_unlock(&t->devlock);

	printk(KERN_DEBUG "fcoe_transport_device_add:"
		       "device %s added to transport %s\n",
		       netdev->name, t->name);

	return 0;
}

/**
 * fcoe_transport_device_remove - remove a device from its transport
 * @netdev: the netdev the transport to be attached to
 *
 * this removes the device from the transport so the given transport will
 * not manage this device any more
 *
 * Returns: 0 for success
 **/
static int fcoe_transport_device_remove(struct fcoe_transport *t,
					struct net_device *netdev)
{
	struct fcoe_transport_internal *ti;

	ti = fcoe_transport_device_lookup(t, netdev);
	if (!ti) {
		printk(KERN_DEBUG "fcoe_transport_device_remove:"
		       "device %s is not managed by transport %s\n",
		       netdev->name, t->name);
		return -ENODEV;
	}
	mutex_lock(&t->devlock);
	list_del(&ti->list);
	mutex_unlock(&t->devlock);
	printk(KERN_DEBUG "fcoe_transport_device_remove:"
	       "device %s removed from transport %s\n",
	       netdev->name, t->name);
	dev_put(ti->netdev);
	kfree(ti);
	return 0;
}

/**
 * fcoe_transport_device_remove_all - remove all from transport devlist
 *
 * this removes the device from the transport so the given transport will
 * not manage this device any more
 *
 * Returns: 0 for success
 **/
static void fcoe_transport_device_remove_all(struct fcoe_transport *t)
{
	struct fcoe_transport_internal *ti, *tmp;

	mutex_lock(&t->devlock);
	list_for_each_entry_safe(ti, tmp, &t->devlist, list) {
		list_del(&ti->list);
		kfree(ti);
	}
	mutex_unlock(&t->devlock);
}

/**
 * fcoe_transport_match - use the bus device match function to match the hw
 * @t: the fcoe transport
 * @netdev:
 *
 * This function is used to check if the givne transport wants to manage the
 * input netdev. if the transports implements the match function, it will be
 * called, o.w. we just compare the pci vendor and device id.
 *
 * Returns: true for match up
 **/
static bool fcoe_transport_match(struct fcoe_transport *t,
				struct net_device *netdev)
{
	/* match transport by vendor and device id */
	struct pci_dev *pci;

	pci = fcoe_transport_pcidev(netdev);

	if (pci) {
		printk(KERN_DEBUG "fcoe_transport_match:"
		       "%s:%x:%x -- %s:%x:%x\n",
		       t->name, t->vendor, t->device,
		       netdev->name, pci->vendor, pci->device);

		/* if transport supports match */
		if (t->match)
			return t->match(netdev);

		/* else just compare the vendor and device id: pci only */
		return (t->vendor == pci->vendor) && (t->device == pci->device);
	}
	return false;
}

/**
 * fcoe_transport_lookup - check if the transport is already registered
 * @t: the transport to be looked up
 *
 * This compares the parent device (pci) vendor and device id
 *
 * Returns: NULL if not found
 *
 * TODO - return default sw transport if no other transport is found
 **/
static struct fcoe_transport *fcoe_transport_lookup(
	struct net_device *netdev)
{
	struct fcoe_transport *t;

	mutex_lock(&fcoe_transports_lock);
	list_for_each_entry(t, &fcoe_transports, list) {
		if (fcoe_transport_match(t, netdev)) {
			mutex_unlock(&fcoe_transports_lock);
			return t;
		}
	}
	mutex_unlock(&fcoe_transports_lock);

	printk(KERN_DEBUG "fcoe_transport_lookup:"
	       "use default transport for %s\n", netdev->name);
	return fcoe_transport_default();
}

/**
 * fcoe_transport_register - adds a fcoe transport to the fcoe transports list
 * @t: ptr to the fcoe transport to be added
 *
 * Returns: 0 for success
 **/
int fcoe_transport_register(struct fcoe_transport *t)
{
	struct fcoe_transport *tt;

	/* TODO - add fcoe_transport specific initialization here */
	mutex_lock(&fcoe_transports_lock);
	list_for_each_entry(tt, &fcoe_transports, list) {
		if (tt == t) {
			mutex_unlock(&fcoe_transports_lock);
			return -EEXIST;
		}
	}
	list_add_tail(&t->list, &fcoe_transports);
	mutex_unlock(&fcoe_transports_lock);

	mutex_init(&t->devlock);
	INIT_LIST_HEAD(&t->devlist);

	printk(KERN_DEBUG "fcoe_transport_register:%s\n", t->name);

	return 0;
}
EXPORT_SYMBOL_GPL(fcoe_transport_register);

/**
 * fcoe_transport_unregister - remove the tranport fro the fcoe transports list
 * @t: ptr to the fcoe transport to be removed
 *
 * Returns: 0 for success
 **/
int fcoe_transport_unregister(struct fcoe_transport *t)
{
	struct fcoe_transport *tt, *tmp;

	mutex_lock(&fcoe_transports_lock);
	list_for_each_entry_safe(tt, tmp, &fcoe_transports, list) {
		if (tt == t) {
			list_del(&t->list);
			mutex_unlock(&fcoe_transports_lock);
			fcoe_transport_device_remove_all(t);
			printk(KERN_DEBUG "fcoe_transport_unregister:%s\n",
			       t->name);
			return 0;
		}
	}
	mutex_unlock(&fcoe_transports_lock);
	return -ENODEV;
}
EXPORT_SYMBOL_GPL(fcoe_transport_unregister);

/*
 * fcoe_load_transport_driver - load an offload driver by alias name
 * @netdev: the target net device
 *
 * Requests for an offload driver module as the fcoe transport, if fails, it
 * falls back to use the SW HBA (fcoe_sw) as its transport
 *
 * TODO -
 * 	1. supports only PCI device
 * 	2. needs fix for VLAn and bonding
 * 	3. pure hw fcoe hba may not have netdev
 *
 * Returns: 0 for success
 **/
int fcoe_load_transport_driver(struct net_device *netdev)
{
	struct pci_dev *pci;
	struct device *dev = netdev->dev.parent;

	if (fcoe_transport_lookup(netdev)) {
		/* load default transport */
		printk(KERN_DEBUG "fcoe: already loaded transport for %s\n",
		       netdev->name);
		return -EEXIST;
	}

	pci = to_pci_dev(dev);
	if (dev->bus != &pci_bus_type) {
		printk(KERN_DEBUG "fcoe: support noly PCI device\n");
		return -ENODEV;
	}
	printk(KERN_DEBUG "fcoe: loading driver fcoe-pci-0x%04x-0x%04x\n",
	       pci->vendor, pci->device);

	return request_module("fcoe-pci-0x%04x-0x%04x",
			      pci->vendor, pci->device);

}
EXPORT_SYMBOL_GPL(fcoe_load_transport_driver);

/**
 * fcoe_transport_attach - load transport to fcoe
 * @netdev: the netdev the transport to be attached to
 *
 * This will look for existing offload driver, if not found, it falls back to
 * the default sw hba (fcoe_sw) as its fcoe transport.
 *
 * Returns: 0 for success
 **/
int fcoe_transport_attach(struct net_device *netdev)
{
	struct fcoe_transport *t;

	/* find the corresponding transport */
	t = fcoe_transport_lookup(netdev);
	if (!t) {
		printk(KERN_DEBUG "fcoe_transport_attach"
		       ":no transport for %s:use %s\n",
		       netdev->name, t->name);
		return -ENODEV;
	}
	/* add to the transport */
	if (fcoe_transport_device_add(t, netdev)) {
		printk(KERN_DEBUG "fcoe_transport_attach"
		       ":failed to add %s to tramsport %s\n",
		       netdev->name, t->name);
		return -EIO;
	}
	/* transport create function */
	if (t->create)
		t->create(netdev);

	printk(KERN_DEBUG "fcoe_transport_attach:transport %s for %s\n",
	       t->name, netdev->name);
	return 0;
}
EXPORT_SYMBOL_GPL(fcoe_transport_attach);

/**
 * fcoe_transport_release - unload transport from fcoe
 * @netdev: the net device on which fcoe is to be released
 *
 * Returns: 0 for success
 **/
int fcoe_transport_release(struct net_device *netdev)
{
	struct fcoe_transport *t;

	/* find the corresponding transport */
	t = fcoe_transport_lookup(netdev);
	if (!t) {
		printk(KERN_DEBUG "fcoe_transport_release:"
		       "no transport for %s:use %s\n",
		       netdev->name, t->name);
		return -ENODEV;
	}
	/* remove the device from the transport */
	if (fcoe_transport_device_remove(t, netdev)) {
		printk(KERN_DEBUG "fcoe_transport_release:"
		       "failed to add %s to tramsport %s\n",
		       netdev->name, t->name);
		return -EIO;
	}
	/* transport destroy function */
	if (t->destroy)
		t->destroy(netdev);

	printk(KERN_DEBUG "fcoe_transport_release:"
	       "device %s dettached from transport %s\n",
	       netdev->name, t->name);

	return 0;
}
EXPORT_SYMBOL_GPL(fcoe_transport_release);

/**
 * fcoe_transport_init - initializes fcoe transport layer
 *
 * This prepares for the fcoe transport layer
 *
 * Returns: none
 **/
int __init fcoe_transport_init(void)
{
	INIT_LIST_HEAD(&fcoe_transports);
	mutex_init(&fcoe_transports_lock);
	return 0;
}

/**
 * fcoe_transport_exit - cleans up the fcoe transport layer
 * This cleans up the fcoe transport layer. removing any transport on the list,
 * note that the transport destroy func is not called here.
 *
 * Returns: none
 **/
int __exit fcoe_transport_exit(void)
{
	struct fcoe_transport *t, *tmp;

	mutex_lock(&fcoe_transports_lock);
	list_for_each_entry_safe(t, tmp, &fcoe_transports, list) {
		list_del(&t->list);
		mutex_unlock(&fcoe_transports_lock);
		fcoe_transport_device_remove_all(t);
		mutex_lock(&fcoe_transports_lock);
	}
	mutex_unlock(&fcoe_transports_lock);
	return 0;
}