blob: 9c74374b53cda258fbd37a29091754aaa17dea78 [file] [log] [blame]
Robert Love9a74e882012-05-22 19:06:21 -07001/*
2 * Copyright(c) 2011 - 2012 Intel Corporation. All rights reserved.
3 *
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.
7 *
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
11 * more details.
12 *
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.
16 *
17 * Maintained at www.Open-FCoE.org
18 */
19
20#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/kernel.h>
23#include <linux/etherdevice.h>
24
25#include <scsi/fcoe_sysfs.h>
26
Robert Love3993de62012-11-27 06:53:24 +000027/*
28 * OK to include local libfcoe.h for debug_logging, but cannot include
29 * <scsi/libfcoe.h> otherwise non-netdev based fcoe solutions would have
30 * have to include more than fcoe_sysfs.h.
31 */
32#include "libfcoe.h"
33
Robert Love9a74e882012-05-22 19:06:21 -070034static atomic_t ctlr_num;
35static atomic_t fcf_num;
36
37/*
38 * fcoe_fcf_dev_loss_tmo: the default number of seconds that fcoe sysfs
39 * should insulate the loss of a fcf.
40 */
41static unsigned int fcoe_fcf_dev_loss_tmo = 1800; /* seconds */
42
43module_param_named(fcf_dev_loss_tmo, fcoe_fcf_dev_loss_tmo,
44 uint, S_IRUGO|S_IWUSR);
45MODULE_PARM_DESC(fcf_dev_loss_tmo,
46 "Maximum number of seconds that libfcoe should"
47 " insulate the loss of a fcf. Once this value is"
48 " exceeded, the fcf is removed.");
49
50/*
51 * These are used by the fcoe_*_show_function routines, they
52 * are intentionally placed in the .c file as they're not intended
53 * for use throughout the code.
54 */
55#define fcoe_ctlr_id(x) \
56 ((x)->id)
57#define fcoe_ctlr_work_q_name(x) \
58 ((x)->work_q_name)
59#define fcoe_ctlr_work_q(x) \
60 ((x)->work_q)
61#define fcoe_ctlr_devloss_work_q_name(x) \
62 ((x)->devloss_work_q_name)
63#define fcoe_ctlr_devloss_work_q(x) \
64 ((x)->devloss_work_q)
65#define fcoe_ctlr_mode(x) \
66 ((x)->mode)
67#define fcoe_ctlr_fcf_dev_loss_tmo(x) \
68 ((x)->fcf_dev_loss_tmo)
69#define fcoe_ctlr_link_fail(x) \
70 ((x)->lesb.lesb_link_fail)
71#define fcoe_ctlr_vlink_fail(x) \
72 ((x)->lesb.lesb_vlink_fail)
73#define fcoe_ctlr_miss_fka(x) \
74 ((x)->lesb.lesb_miss_fka)
75#define fcoe_ctlr_symb_err(x) \
76 ((x)->lesb.lesb_symb_err)
77#define fcoe_ctlr_err_block(x) \
78 ((x)->lesb.lesb_err_block)
79#define fcoe_ctlr_fcs_error(x) \
80 ((x)->lesb.lesb_fcs_error)
81#define fcoe_fcf_state(x) \
82 ((x)->state)
83#define fcoe_fcf_fabric_name(x) \
84 ((x)->fabric_name)
85#define fcoe_fcf_switch_name(x) \
86 ((x)->switch_name)
87#define fcoe_fcf_fc_map(x) \
88 ((x)->fc_map)
89#define fcoe_fcf_vfid(x) \
90 ((x)->vfid)
91#define fcoe_fcf_mac(x) \
92 ((x)->mac)
93#define fcoe_fcf_priority(x) \
94 ((x)->priority)
95#define fcoe_fcf_fka_period(x) \
96 ((x)->fka_period)
97#define fcoe_fcf_dev_loss_tmo(x) \
98 ((x)->dev_loss_tmo)
99#define fcoe_fcf_selected(x) \
100 ((x)->selected)
101#define fcoe_fcf_vlan_id(x) \
102 ((x)->vlan_id)
103
104/*
105 * dev_loss_tmo attribute
106 */
107static int fcoe_str_to_dev_loss(const char *buf, unsigned long *val)
108{
109 int ret;
110
111 ret = kstrtoul(buf, 0, val);
Robert Love902a45a2012-07-06 10:40:20 -0700112 if (ret)
Robert Love9a74e882012-05-22 19:06:21 -0700113 return -EINVAL;
114 /*
115 * Check for overflow; dev_loss_tmo is u32
116 */
117 if (*val > UINT_MAX)
118 return -EINVAL;
119
120 return 0;
121}
122
123static int fcoe_fcf_set_dev_loss_tmo(struct fcoe_fcf_device *fcf,
124 unsigned long val)
125{
126 if ((fcf->state == FCOE_FCF_STATE_UNKNOWN) ||
127 (fcf->state == FCOE_FCF_STATE_DISCONNECTED) ||
128 (fcf->state == FCOE_FCF_STATE_DELETED))
129 return -EBUSY;
130 /*
131 * Check for overflow; dev_loss_tmo is u32
132 */
133 if (val > UINT_MAX)
134 return -EINVAL;
135
136 fcoe_fcf_dev_loss_tmo(fcf) = val;
137 return 0;
138}
139
140#define FCOE_DEVICE_ATTR(_prefix, _name, _mode, _show, _store) \
141struct device_attribute device_attr_fcoe_##_prefix##_##_name = \
142 __ATTR(_name, _mode, _show, _store)
143
144#define fcoe_ctlr_show_function(field, format_string, sz, cast) \
145static ssize_t show_fcoe_ctlr_device_##field(struct device *dev, \
146 struct device_attribute *attr, \
147 char *buf) \
148{ \
149 struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev); \
150 if (ctlr->f->get_fcoe_ctlr_##field) \
151 ctlr->f->get_fcoe_ctlr_##field(ctlr); \
152 return snprintf(buf, sz, format_string, \
153 cast fcoe_ctlr_##field(ctlr)); \
154}
155
156#define fcoe_fcf_show_function(field, format_string, sz, cast) \
157static ssize_t show_fcoe_fcf_device_##field(struct device *dev, \
158 struct device_attribute *attr, \
159 char *buf) \
160{ \
161 struct fcoe_fcf_device *fcf = dev_to_fcf(dev); \
162 struct fcoe_ctlr_device *ctlr = fcoe_fcf_dev_to_ctlr_dev(fcf); \
163 if (ctlr->f->get_fcoe_fcf_##field) \
164 ctlr->f->get_fcoe_fcf_##field(fcf); \
165 return snprintf(buf, sz, format_string, \
166 cast fcoe_fcf_##field(fcf)); \
167}
168
169#define fcoe_ctlr_private_show_function(field, format_string, sz, cast) \
170static ssize_t show_fcoe_ctlr_device_##field(struct device *dev, \
171 struct device_attribute *attr, \
172 char *buf) \
173{ \
174 struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev); \
175 return snprintf(buf, sz, format_string, cast fcoe_ctlr_##field(ctlr)); \
176}
177
178#define fcoe_fcf_private_show_function(field, format_string, sz, cast) \
179static ssize_t show_fcoe_fcf_device_##field(struct device *dev, \
180 struct device_attribute *attr, \
181 char *buf) \
182{ \
183 struct fcoe_fcf_device *fcf = dev_to_fcf(dev); \
184 return snprintf(buf, sz, format_string, cast fcoe_fcf_##field(fcf)); \
185}
186
187#define fcoe_ctlr_private_rd_attr(field, format_string, sz) \
188 fcoe_ctlr_private_show_function(field, format_string, sz, ) \
189 static FCOE_DEVICE_ATTR(ctlr, field, S_IRUGO, \
190 show_fcoe_ctlr_device_##field, NULL)
191
192#define fcoe_ctlr_rd_attr(field, format_string, sz) \
193 fcoe_ctlr_show_function(field, format_string, sz, ) \
194 static FCOE_DEVICE_ATTR(ctlr, field, S_IRUGO, \
195 show_fcoe_ctlr_device_##field, NULL)
196
197#define fcoe_fcf_rd_attr(field, format_string, sz) \
198 fcoe_fcf_show_function(field, format_string, sz, ) \
199 static FCOE_DEVICE_ATTR(fcf, field, S_IRUGO, \
200 show_fcoe_fcf_device_##field, NULL)
201
202#define fcoe_fcf_private_rd_attr(field, format_string, sz) \
203 fcoe_fcf_private_show_function(field, format_string, sz, ) \
204 static FCOE_DEVICE_ATTR(fcf, field, S_IRUGO, \
205 show_fcoe_fcf_device_##field, NULL)
206
207#define fcoe_ctlr_private_rd_attr_cast(field, format_string, sz, cast) \
208 fcoe_ctlr_private_show_function(field, format_string, sz, (cast)) \
209 static FCOE_DEVICE_ATTR(ctlr, field, S_IRUGO, \
210 show_fcoe_ctlr_device_##field, NULL)
211
212#define fcoe_fcf_private_rd_attr_cast(field, format_string, sz, cast) \
213 fcoe_fcf_private_show_function(field, format_string, sz, (cast)) \
214 static FCOE_DEVICE_ATTR(fcf, field, S_IRUGO, \
215 show_fcoe_fcf_device_##field, NULL)
216
217#define fcoe_enum_name_search(title, table_type, table) \
218static const char *get_fcoe_##title##_name(enum table_type table_key) \
219{ \
Robert Loveef60f672012-11-27 06:53:19 +0000220 if (table_key < 0 || table_key >= ARRAY_SIZE(table)) \
221 return NULL; \
222 return table[table_key]; \
Robert Love9a74e882012-05-22 19:06:21 -0700223}
224
Robert Loveef60f672012-11-27 06:53:19 +0000225static char *fip_conn_type_names[] = {
226 [ FIP_CONN_TYPE_UNKNOWN ] = "Unknown",
227 [ FIP_CONN_TYPE_FABRIC ] = "Fabric",
228 [ FIP_CONN_TYPE_VN2VN ] = "VN2VN",
229};
230fcoe_enum_name_search(ctlr_mode, fip_conn_type, fip_conn_type_names)
231#define FCOE_CTLR_MODE_MAX_NAMELEN 50
232
233static char *fcf_state_names[] = {
234 [ FCOE_FCF_STATE_UNKNOWN ] = "Unknown",
235 [ FCOE_FCF_STATE_DISCONNECTED ] = "Disconnected",
236 [ FCOE_FCF_STATE_CONNECTED ] = "Connected",
Robert Love9a74e882012-05-22 19:06:21 -0700237};
238fcoe_enum_name_search(fcf_state, fcf_state, fcf_state_names)
239#define FCOE_FCF_STATE_MAX_NAMELEN 50
240
241static ssize_t show_fcf_state(struct device *dev,
242 struct device_attribute *attr,
243 char *buf)
244{
245 struct fcoe_fcf_device *fcf = dev_to_fcf(dev);
246 const char *name;
247 name = get_fcoe_fcf_state_name(fcf->state);
248 if (!name)
249 return -EINVAL;
250 return snprintf(buf, FCOE_FCF_STATE_MAX_NAMELEN, "%s\n", name);
251}
252static FCOE_DEVICE_ATTR(fcf, state, S_IRUGO, show_fcf_state, NULL);
253
Robert Loveef60f672012-11-27 06:53:19 +0000254#define FCOE_MAX_MODENAME_LEN 20
Robert Love9a74e882012-05-22 19:06:21 -0700255static ssize_t show_ctlr_mode(struct device *dev,
256 struct device_attribute *attr,
257 char *buf)
258{
259 struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
260 const char *name;
261
262 if (ctlr->f->get_fcoe_ctlr_mode)
263 ctlr->f->get_fcoe_ctlr_mode(ctlr);
264
265 name = get_fcoe_ctlr_mode_name(ctlr->mode);
266 if (!name)
267 return -EINVAL;
268 return snprintf(buf, FCOE_CTLR_MODE_MAX_NAMELEN,
269 "%s\n", name);
270}
271static FCOE_DEVICE_ATTR(ctlr, mode, S_IRUGO,
272 show_ctlr_mode, NULL);
273
274static ssize_t
275store_private_fcoe_ctlr_fcf_dev_loss_tmo(struct device *dev,
276 struct device_attribute *attr,
277 const char *buf, size_t count)
278{
279 struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
280 struct fcoe_fcf_device *fcf;
281 unsigned long val;
282 int rc;
283
284 rc = fcoe_str_to_dev_loss(buf, &val);
285 if (rc)
286 return rc;
287
288 fcoe_ctlr_fcf_dev_loss_tmo(ctlr) = val;
289 mutex_lock(&ctlr->lock);
290 list_for_each_entry(fcf, &ctlr->fcfs, peers)
291 fcoe_fcf_set_dev_loss_tmo(fcf, val);
292 mutex_unlock(&ctlr->lock);
293 return count;
294}
295fcoe_ctlr_private_show_function(fcf_dev_loss_tmo, "%d\n", 20, );
296static FCOE_DEVICE_ATTR(ctlr, fcf_dev_loss_tmo, S_IRUGO | S_IWUSR,
297 show_fcoe_ctlr_device_fcf_dev_loss_tmo,
298 store_private_fcoe_ctlr_fcf_dev_loss_tmo);
299
300/* Link Error Status Block (LESB) */
301fcoe_ctlr_rd_attr(link_fail, "%u\n", 20);
302fcoe_ctlr_rd_attr(vlink_fail, "%u\n", 20);
303fcoe_ctlr_rd_attr(miss_fka, "%u\n", 20);
304fcoe_ctlr_rd_attr(symb_err, "%u\n", 20);
305fcoe_ctlr_rd_attr(err_block, "%u\n", 20);
306fcoe_ctlr_rd_attr(fcs_error, "%u\n", 20);
307
308fcoe_fcf_private_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
309fcoe_fcf_private_rd_attr_cast(switch_name, "0x%llx\n", 20, unsigned long long);
310fcoe_fcf_private_rd_attr(priority, "%u\n", 20);
311fcoe_fcf_private_rd_attr(fc_map, "0x%x\n", 20);
312fcoe_fcf_private_rd_attr(vfid, "%u\n", 20);
313fcoe_fcf_private_rd_attr(mac, "%pM\n", 20);
314fcoe_fcf_private_rd_attr(fka_period, "%u\n", 20);
315fcoe_fcf_rd_attr(selected, "%u\n", 20);
316fcoe_fcf_rd_attr(vlan_id, "%u\n", 20);
317
318fcoe_fcf_private_show_function(dev_loss_tmo, "%d\n", 20, )
319static ssize_t
320store_fcoe_fcf_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
321 const char *buf, size_t count)
322{
323 struct fcoe_fcf_device *fcf = dev_to_fcf(dev);
324 unsigned long val;
325 int rc;
326
327 rc = fcoe_str_to_dev_loss(buf, &val);
328 if (rc)
329 return rc;
330
331 rc = fcoe_fcf_set_dev_loss_tmo(fcf, val);
332 if (rc)
333 return rc;
334 return count;
335}
336static FCOE_DEVICE_ATTR(fcf, dev_loss_tmo, S_IRUGO | S_IWUSR,
337 show_fcoe_fcf_device_dev_loss_tmo,
338 store_fcoe_fcf_dev_loss_tmo);
339
340static struct attribute *fcoe_ctlr_lesb_attrs[] = {
341 &device_attr_fcoe_ctlr_link_fail.attr,
342 &device_attr_fcoe_ctlr_vlink_fail.attr,
343 &device_attr_fcoe_ctlr_miss_fka.attr,
344 &device_attr_fcoe_ctlr_symb_err.attr,
345 &device_attr_fcoe_ctlr_err_block.attr,
346 &device_attr_fcoe_ctlr_fcs_error.attr,
347 NULL,
348};
349
350static struct attribute_group fcoe_ctlr_lesb_attr_group = {
351 .name = "lesb",
352 .attrs = fcoe_ctlr_lesb_attrs,
353};
354
355static struct attribute *fcoe_ctlr_attrs[] = {
356 &device_attr_fcoe_ctlr_fcf_dev_loss_tmo.attr,
357 &device_attr_fcoe_ctlr_mode.attr,
358 NULL,
359};
360
361static struct attribute_group fcoe_ctlr_attr_group = {
362 .attrs = fcoe_ctlr_attrs,
363};
364
365static const struct attribute_group *fcoe_ctlr_attr_groups[] = {
366 &fcoe_ctlr_attr_group,
367 &fcoe_ctlr_lesb_attr_group,
368 NULL,
369};
370
371static struct attribute *fcoe_fcf_attrs[] = {
372 &device_attr_fcoe_fcf_fabric_name.attr,
373 &device_attr_fcoe_fcf_switch_name.attr,
374 &device_attr_fcoe_fcf_dev_loss_tmo.attr,
375 &device_attr_fcoe_fcf_fc_map.attr,
376 &device_attr_fcoe_fcf_vfid.attr,
377 &device_attr_fcoe_fcf_mac.attr,
378 &device_attr_fcoe_fcf_priority.attr,
379 &device_attr_fcoe_fcf_fka_period.attr,
380 &device_attr_fcoe_fcf_state.attr,
381 &device_attr_fcoe_fcf_selected.attr,
382 &device_attr_fcoe_fcf_vlan_id.attr,
383 NULL
384};
385
386static struct attribute_group fcoe_fcf_attr_group = {
387 .attrs = fcoe_fcf_attrs,
388};
389
390static const struct attribute_group *fcoe_fcf_attr_groups[] = {
391 &fcoe_fcf_attr_group,
392 NULL,
393};
394
395struct bus_type fcoe_bus_type;
396
397static int fcoe_bus_match(struct device *dev,
398 struct device_driver *drv)
399{
400 if (dev->bus == &fcoe_bus_type)
401 return 1;
402 return 0;
403}
404
405/**
406 * fcoe_ctlr_device_release() - Release the FIP ctlr memory
407 * @dev: Pointer to the FIP ctlr's embedded device
408 *
409 * Called when the last FIP ctlr reference is released.
410 */
411static void fcoe_ctlr_device_release(struct device *dev)
412{
413 struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
414 kfree(ctlr);
415}
416
417/**
418 * fcoe_fcf_device_release() - Release the FIP fcf memory
419 * @dev: Pointer to the fcf's embedded device
420 *
421 * Called when the last FIP fcf reference is released.
422 */
423static void fcoe_fcf_device_release(struct device *dev)
424{
425 struct fcoe_fcf_device *fcf = dev_to_fcf(dev);
426 kfree(fcf);
427}
428
429struct device_type fcoe_ctlr_device_type = {
430 .name = "fcoe_ctlr",
431 .groups = fcoe_ctlr_attr_groups,
432 .release = fcoe_ctlr_device_release,
433};
434
435struct device_type fcoe_fcf_device_type = {
436 .name = "fcoe_fcf",
437 .groups = fcoe_fcf_attr_groups,
438 .release = fcoe_fcf_device_release,
439};
440
441struct bus_type fcoe_bus_type = {
442 .name = "fcoe",
443 .match = &fcoe_bus_match,
444};
445
446/**
447 * fcoe_ctlr_device_flush_work() - Flush a FIP ctlr's workqueue
448 * @ctlr: Pointer to the FIP ctlr whose workqueue is to be flushed
449 */
450void fcoe_ctlr_device_flush_work(struct fcoe_ctlr_device *ctlr)
451{
452 if (!fcoe_ctlr_work_q(ctlr)) {
453 printk(KERN_ERR
454 "ERROR: FIP Ctlr '%d' attempted to flush work, "
455 "when no workqueue created.\n", ctlr->id);
456 dump_stack();
457 return;
458 }
459
460 flush_workqueue(fcoe_ctlr_work_q(ctlr));
461}
462
463/**
464 * fcoe_ctlr_device_queue_work() - Schedule work for a FIP ctlr's workqueue
465 * @ctlr: Pointer to the FIP ctlr who owns the devloss workqueue
466 * @work: Work to queue for execution
467 *
468 * Return value:
469 * 1 on success / 0 already queued / < 0 for error
470 */
471int fcoe_ctlr_device_queue_work(struct fcoe_ctlr_device *ctlr,
472 struct work_struct *work)
473{
474 if (unlikely(!fcoe_ctlr_work_q(ctlr))) {
475 printk(KERN_ERR
476 "ERROR: FIP Ctlr '%d' attempted to queue work, "
477 "when no workqueue created.\n", ctlr->id);
478 dump_stack();
479
480 return -EINVAL;
481 }
482
483 return queue_work(fcoe_ctlr_work_q(ctlr), work);
484}
485
486/**
487 * fcoe_ctlr_device_flush_devloss() - Flush a FIP ctlr's devloss workqueue
488 * @ctlr: Pointer to FIP ctlr whose workqueue is to be flushed
489 */
490void fcoe_ctlr_device_flush_devloss(struct fcoe_ctlr_device *ctlr)
491{
492 if (!fcoe_ctlr_devloss_work_q(ctlr)) {
493 printk(KERN_ERR
494 "ERROR: FIP Ctlr '%d' attempted to flush work, "
495 "when no workqueue created.\n", ctlr->id);
496 dump_stack();
497 return;
498 }
499
500 flush_workqueue(fcoe_ctlr_devloss_work_q(ctlr));
501}
502
503/**
504 * fcoe_ctlr_device_queue_devloss_work() - Schedule work for a FIP ctlr's devloss workqueue
505 * @ctlr: Pointer to the FIP ctlr who owns the devloss workqueue
506 * @work: Work to queue for execution
507 * @delay: jiffies to delay the work queuing
508 *
509 * Return value:
510 * 1 on success / 0 already queued / < 0 for error
511 */
512int fcoe_ctlr_device_queue_devloss_work(struct fcoe_ctlr_device *ctlr,
513 struct delayed_work *work,
514 unsigned long delay)
515{
516 if (unlikely(!fcoe_ctlr_devloss_work_q(ctlr))) {
517 printk(KERN_ERR
518 "ERROR: FIP Ctlr '%d' attempted to queue work, "
519 "when no workqueue created.\n", ctlr->id);
520 dump_stack();
521
522 return -EINVAL;
523 }
524
525 return queue_delayed_work(fcoe_ctlr_devloss_work_q(ctlr), work, delay);
526}
527
528static int fcoe_fcf_device_match(struct fcoe_fcf_device *new,
529 struct fcoe_fcf_device *old)
530{
531 if (new->switch_name == old->switch_name &&
532 new->fabric_name == old->fabric_name &&
533 new->fc_map == old->fc_map &&
534 compare_ether_addr(new->mac, old->mac) == 0)
535 return 1;
536 return 0;
537}
538
539/**
540 * fcoe_ctlr_device_add() - Add a FIP ctlr to sysfs
541 * @parent: The parent device to which the fcoe_ctlr instance
542 * should be attached
543 * @f: The LLD's FCoE sysfs function template pointer
544 * @priv_size: Size to be allocated with the fcoe_ctlr_device for the LLD
545 *
546 * This routine allocates a FIP ctlr object with some additional memory
547 * for the LLD. The FIP ctlr is initialized, added to sysfs and then
548 * attributes are added to it.
549 */
550struct fcoe_ctlr_device *fcoe_ctlr_device_add(struct device *parent,
551 struct fcoe_sysfs_function_template *f,
552 int priv_size)
553{
554 struct fcoe_ctlr_device *ctlr;
555 int error = 0;
556
557 ctlr = kzalloc(sizeof(struct fcoe_ctlr_device) + priv_size,
558 GFP_KERNEL);
559 if (!ctlr)
560 goto out;
561
562 ctlr->id = atomic_inc_return(&ctlr_num) - 1;
563 ctlr->f = f;
564 INIT_LIST_HEAD(&ctlr->fcfs);
565 mutex_init(&ctlr->lock);
566 ctlr->dev.parent = parent;
567 ctlr->dev.bus = &fcoe_bus_type;
568 ctlr->dev.type = &fcoe_ctlr_device_type;
569
570 ctlr->fcf_dev_loss_tmo = fcoe_fcf_dev_loss_tmo;
571
572 snprintf(ctlr->work_q_name, sizeof(ctlr->work_q_name),
573 "ctlr_wq_%d", ctlr->id);
574 ctlr->work_q = create_singlethread_workqueue(
575 ctlr->work_q_name);
576 if (!ctlr->work_q)
577 goto out_del;
578
579 snprintf(ctlr->devloss_work_q_name,
580 sizeof(ctlr->devloss_work_q_name),
581 "ctlr_dl_wq_%d", ctlr->id);
582 ctlr->devloss_work_q = create_singlethread_workqueue(
583 ctlr->devloss_work_q_name);
584 if (!ctlr->devloss_work_q)
585 goto out_del_q;
586
587 dev_set_name(&ctlr->dev, "ctlr_%d", ctlr->id);
588 error = device_register(&ctlr->dev);
589 if (error)
590 goto out_del_q2;
591
592 return ctlr;
593
594out_del_q2:
595 destroy_workqueue(ctlr->devloss_work_q);
596 ctlr->devloss_work_q = NULL;
597out_del_q:
598 destroy_workqueue(ctlr->work_q);
599 ctlr->work_q = NULL;
600out_del:
601 kfree(ctlr);
602out:
603 return NULL;
604}
605EXPORT_SYMBOL_GPL(fcoe_ctlr_device_add);
606
607/**
608 * fcoe_ctlr_device_delete() - Delete a FIP ctlr and its subtree from sysfs
609 * @ctlr: A pointer to the ctlr to be deleted
610 *
611 * Deletes a FIP ctlr and any fcfs attached
612 * to it. Deleting fcfs will cause their childen
613 * to be deleted as well.
614 *
615 * The ctlr is detached from sysfs and it's resources
616 * are freed (work q), but the memory is not freed
617 * until its last reference is released.
618 *
619 * This routine expects no locks to be held before
620 * calling.
621 *
622 * TODO: Currently there are no callbacks to clean up LLD data
623 * for a fcoe_fcf_device. LLDs must keep this in mind as they need
624 * to clean up each of their LLD data for all fcoe_fcf_device before
625 * calling fcoe_ctlr_device_delete.
626 */
627void fcoe_ctlr_device_delete(struct fcoe_ctlr_device *ctlr)
628{
629 struct fcoe_fcf_device *fcf, *next;
630 /* Remove any attached fcfs */
631 mutex_lock(&ctlr->lock);
632 list_for_each_entry_safe(fcf, next,
633 &ctlr->fcfs, peers) {
634 list_del(&fcf->peers);
635 fcf->state = FCOE_FCF_STATE_DELETED;
636 fcoe_ctlr_device_queue_work(ctlr, &fcf->delete_work);
637 }
638 mutex_unlock(&ctlr->lock);
639
640 fcoe_ctlr_device_flush_work(ctlr);
641
642 destroy_workqueue(ctlr->devloss_work_q);
643 ctlr->devloss_work_q = NULL;
644 destroy_workqueue(ctlr->work_q);
645 ctlr->work_q = NULL;
646
647 device_unregister(&ctlr->dev);
648}
649EXPORT_SYMBOL_GPL(fcoe_ctlr_device_delete);
650
651/**
652 * fcoe_fcf_device_final_delete() - Final delete routine
653 * @work: The FIP fcf's embedded work struct
654 *
655 * It is expected that the fcf has been removed from
656 * the FIP ctlr's list before calling this routine.
657 */
658static void fcoe_fcf_device_final_delete(struct work_struct *work)
659{
660 struct fcoe_fcf_device *fcf =
661 container_of(work, struct fcoe_fcf_device, delete_work);
662 struct fcoe_ctlr_device *ctlr = fcoe_fcf_dev_to_ctlr_dev(fcf);
663
664 /*
665 * Cancel any outstanding timers. These should really exist
666 * only when rmmod'ing the LLDD and we're asking for
667 * immediate termination of the rports
668 */
669 if (!cancel_delayed_work(&fcf->dev_loss_work))
670 fcoe_ctlr_device_flush_devloss(ctlr);
671
672 device_unregister(&fcf->dev);
673}
674
675/**
676 * fip_timeout_deleted_fcf() - Delete a fcf when the devloss timer fires
677 * @work: The FIP fcf's embedded work struct
678 *
679 * Removes the fcf from the FIP ctlr's list of fcfs and
680 * queues the final deletion.
681 */
682static void fip_timeout_deleted_fcf(struct work_struct *work)
683{
684 struct fcoe_fcf_device *fcf =
685 container_of(work, struct fcoe_fcf_device, dev_loss_work.work);
686 struct fcoe_ctlr_device *ctlr = fcoe_fcf_dev_to_ctlr_dev(fcf);
687
688 mutex_lock(&ctlr->lock);
689
690 /*
691 * If the fcf is deleted or reconnected before the timer
692 * fires the devloss queue will be flushed, but the state will
693 * either be CONNECTED or DELETED. If that is the case we
694 * cancel deleting the fcf.
695 */
696 if (fcf->state != FCOE_FCF_STATE_DISCONNECTED)
697 goto out;
698
699 dev_printk(KERN_ERR, &fcf->dev,
700 "FIP fcf connection time out: removing fcf\n");
701
702 list_del(&fcf->peers);
703 fcf->state = FCOE_FCF_STATE_DELETED;
704 fcoe_ctlr_device_queue_work(ctlr, &fcf->delete_work);
705
706out:
707 mutex_unlock(&ctlr->lock);
708}
709
710/**
711 * fcoe_fcf_device_delete() - Delete a FIP fcf
712 * @fcf: Pointer to the fcf which is to be deleted
713 *
714 * Queues the FIP fcf on the devloss workqueue
715 *
716 * Expects the ctlr_attrs mutex to be held for fcf
717 * state change.
718 */
719void fcoe_fcf_device_delete(struct fcoe_fcf_device *fcf)
720{
721 struct fcoe_ctlr_device *ctlr = fcoe_fcf_dev_to_ctlr_dev(fcf);
722 int timeout = fcf->dev_loss_tmo;
723
724 if (fcf->state != FCOE_FCF_STATE_CONNECTED)
725 return;
726
727 fcf->state = FCOE_FCF_STATE_DISCONNECTED;
728
729 /*
730 * FCF will only be re-connected by the LLD calling
731 * fcoe_fcf_device_add, and it should be setting up
732 * priv then.
733 */
734 fcf->priv = NULL;
735
736 fcoe_ctlr_device_queue_devloss_work(ctlr, &fcf->dev_loss_work,
737 timeout * HZ);
738}
739EXPORT_SYMBOL_GPL(fcoe_fcf_device_delete);
740
741/**
742 * fcoe_fcf_device_add() - Add a FCoE sysfs fcoe_fcf_device to the system
743 * @ctlr: The fcoe_ctlr_device that will be the fcoe_fcf_device parent
744 * @new_fcf: A temporary FCF used for lookups on the current list of fcfs
745 *
746 * Expects to be called with the ctlr->lock held
747 */
748struct fcoe_fcf_device *fcoe_fcf_device_add(struct fcoe_ctlr_device *ctlr,
749 struct fcoe_fcf_device *new_fcf)
750{
751 struct fcoe_fcf_device *fcf;
752 int error = 0;
753
754 list_for_each_entry(fcf, &ctlr->fcfs, peers) {
755 if (fcoe_fcf_device_match(new_fcf, fcf)) {
756 if (fcf->state == FCOE_FCF_STATE_CONNECTED)
757 return fcf;
758
759 fcf->state = FCOE_FCF_STATE_CONNECTED;
760
761 if (!cancel_delayed_work(&fcf->dev_loss_work))
762 fcoe_ctlr_device_flush_devloss(ctlr);
763
764 return fcf;
765 }
766 }
767
768 fcf = kzalloc(sizeof(struct fcoe_fcf_device), GFP_ATOMIC);
769 if (unlikely(!fcf))
770 goto out;
771
772 INIT_WORK(&fcf->delete_work, fcoe_fcf_device_final_delete);
773 INIT_DELAYED_WORK(&fcf->dev_loss_work, fip_timeout_deleted_fcf);
774
775 fcf->dev.parent = &ctlr->dev;
776 fcf->dev.bus = &fcoe_bus_type;
777 fcf->dev.type = &fcoe_fcf_device_type;
778 fcf->id = atomic_inc_return(&fcf_num) - 1;
779 fcf->state = FCOE_FCF_STATE_UNKNOWN;
780
781 fcf->dev_loss_tmo = ctlr->fcf_dev_loss_tmo;
782
783 dev_set_name(&fcf->dev, "fcf_%d", fcf->id);
784
785 fcf->fabric_name = new_fcf->fabric_name;
786 fcf->switch_name = new_fcf->switch_name;
787 fcf->fc_map = new_fcf->fc_map;
788 fcf->vfid = new_fcf->vfid;
789 memcpy(fcf->mac, new_fcf->mac, ETH_ALEN);
790 fcf->priority = new_fcf->priority;
791 fcf->fka_period = new_fcf->fka_period;
792 fcf->selected = new_fcf->selected;
793
794 error = device_register(&fcf->dev);
795 if (error)
796 goto out_del;
797
798 fcf->state = FCOE_FCF_STATE_CONNECTED;
799 list_add_tail(&fcf->peers, &ctlr->fcfs);
800
801 return fcf;
802
803out_del:
804 kfree(fcf);
805out:
806 return NULL;
807}
808EXPORT_SYMBOL_GPL(fcoe_fcf_device_add);
809
810int __init fcoe_sysfs_setup(void)
811{
812 int error;
813
814 atomic_set(&ctlr_num, 0);
815 atomic_set(&fcf_num, 0);
816
817 error = bus_register(&fcoe_bus_type);
818 if (error)
819 return error;
820
821 return 0;
822}
823
824void __exit fcoe_sysfs_teardown(void)
825{
826 bus_unregister(&fcoe_bus_type);
827}