blob: 0675fd128734e15b23513565d83f1c05be328102 [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>
Robert Love6a891b02012-11-27 06:53:30 +000024#include <linux/ctype.h>
Robert Love9a74e882012-05-22 19:06:21 -070025
26#include <scsi/fcoe_sysfs.h>
Robert Love6a891b02012-11-27 06:53:30 +000027#include <scsi/libfcoe.h>
Robert Love9a74e882012-05-22 19:06:21 -070028
Robert Love3993de62012-11-27 06:53:24 +000029/*
30 * OK to include local libfcoe.h for debug_logging, but cannot include
31 * <scsi/libfcoe.h> otherwise non-netdev based fcoe solutions would have
32 * have to include more than fcoe_sysfs.h.
33 */
34#include "libfcoe.h"
35
Robert Love9a74e882012-05-22 19:06:21 -070036static atomic_t ctlr_num;
37static atomic_t fcf_num;
38
39/*
40 * fcoe_fcf_dev_loss_tmo: the default number of seconds that fcoe sysfs
41 * should insulate the loss of a fcf.
42 */
43static unsigned int fcoe_fcf_dev_loss_tmo = 1800; /* seconds */
44
45module_param_named(fcf_dev_loss_tmo, fcoe_fcf_dev_loss_tmo,
46 uint, S_IRUGO|S_IWUSR);
47MODULE_PARM_DESC(fcf_dev_loss_tmo,
48 "Maximum number of seconds that libfcoe should"
49 " insulate the loss of a fcf. Once this value is"
50 " exceeded, the fcf is removed.");
51
52/*
53 * These are used by the fcoe_*_show_function routines, they
54 * are intentionally placed in the .c file as they're not intended
55 * for use throughout the code.
56 */
57#define fcoe_ctlr_id(x) \
58 ((x)->id)
59#define fcoe_ctlr_work_q_name(x) \
60 ((x)->work_q_name)
61#define fcoe_ctlr_work_q(x) \
62 ((x)->work_q)
63#define fcoe_ctlr_devloss_work_q_name(x) \
64 ((x)->devloss_work_q_name)
65#define fcoe_ctlr_devloss_work_q(x) \
66 ((x)->devloss_work_q)
67#define fcoe_ctlr_mode(x) \
68 ((x)->mode)
69#define fcoe_ctlr_fcf_dev_loss_tmo(x) \
70 ((x)->fcf_dev_loss_tmo)
71#define fcoe_ctlr_link_fail(x) \
72 ((x)->lesb.lesb_link_fail)
73#define fcoe_ctlr_vlink_fail(x) \
74 ((x)->lesb.lesb_vlink_fail)
75#define fcoe_ctlr_miss_fka(x) \
76 ((x)->lesb.lesb_miss_fka)
77#define fcoe_ctlr_symb_err(x) \
78 ((x)->lesb.lesb_symb_err)
79#define fcoe_ctlr_err_block(x) \
80 ((x)->lesb.lesb_err_block)
81#define fcoe_ctlr_fcs_error(x) \
82 ((x)->lesb.lesb_fcs_error)
Robert Love6a891b02012-11-27 06:53:30 +000083#define fcoe_ctlr_enabled(x) \
84 ((x)->enabled)
Robert Love9a74e882012-05-22 19:06:21 -070085#define fcoe_fcf_state(x) \
86 ((x)->state)
87#define fcoe_fcf_fabric_name(x) \
88 ((x)->fabric_name)
89#define fcoe_fcf_switch_name(x) \
90 ((x)->switch_name)
91#define fcoe_fcf_fc_map(x) \
92 ((x)->fc_map)
93#define fcoe_fcf_vfid(x) \
94 ((x)->vfid)
95#define fcoe_fcf_mac(x) \
96 ((x)->mac)
97#define fcoe_fcf_priority(x) \
98 ((x)->priority)
99#define fcoe_fcf_fka_period(x) \
100 ((x)->fka_period)
101#define fcoe_fcf_dev_loss_tmo(x) \
102 ((x)->dev_loss_tmo)
103#define fcoe_fcf_selected(x) \
104 ((x)->selected)
105#define fcoe_fcf_vlan_id(x) \
106 ((x)->vlan_id)
107
108/*
109 * dev_loss_tmo attribute
110 */
111static int fcoe_str_to_dev_loss(const char *buf, unsigned long *val)
112{
113 int ret;
114
115 ret = kstrtoul(buf, 0, val);
Robert Love902a45a2012-07-06 10:40:20 -0700116 if (ret)
Robert Love9a74e882012-05-22 19:06:21 -0700117 return -EINVAL;
118 /*
119 * Check for overflow; dev_loss_tmo is u32
120 */
121 if (*val > UINT_MAX)
122 return -EINVAL;
123
124 return 0;
125}
126
127static int fcoe_fcf_set_dev_loss_tmo(struct fcoe_fcf_device *fcf,
128 unsigned long val)
129{
130 if ((fcf->state == FCOE_FCF_STATE_UNKNOWN) ||
131 (fcf->state == FCOE_FCF_STATE_DISCONNECTED) ||
132 (fcf->state == FCOE_FCF_STATE_DELETED))
133 return -EBUSY;
134 /*
135 * Check for overflow; dev_loss_tmo is u32
136 */
137 if (val > UINT_MAX)
138 return -EINVAL;
139
140 fcoe_fcf_dev_loss_tmo(fcf) = val;
141 return 0;
142}
143
144#define FCOE_DEVICE_ATTR(_prefix, _name, _mode, _show, _store) \
145struct device_attribute device_attr_fcoe_##_prefix##_##_name = \
146 __ATTR(_name, _mode, _show, _store)
147
148#define fcoe_ctlr_show_function(field, format_string, sz, cast) \
149static ssize_t show_fcoe_ctlr_device_##field(struct device *dev, \
150 struct device_attribute *attr, \
151 char *buf) \
152{ \
153 struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev); \
154 if (ctlr->f->get_fcoe_ctlr_##field) \
155 ctlr->f->get_fcoe_ctlr_##field(ctlr); \
156 return snprintf(buf, sz, format_string, \
157 cast fcoe_ctlr_##field(ctlr)); \
158}
159
160#define fcoe_fcf_show_function(field, format_string, sz, cast) \
161static ssize_t show_fcoe_fcf_device_##field(struct device *dev, \
162 struct device_attribute *attr, \
163 char *buf) \
164{ \
165 struct fcoe_fcf_device *fcf = dev_to_fcf(dev); \
166 struct fcoe_ctlr_device *ctlr = fcoe_fcf_dev_to_ctlr_dev(fcf); \
167 if (ctlr->f->get_fcoe_fcf_##field) \
168 ctlr->f->get_fcoe_fcf_##field(fcf); \
169 return snprintf(buf, sz, format_string, \
170 cast fcoe_fcf_##field(fcf)); \
171}
172
173#define fcoe_ctlr_private_show_function(field, format_string, sz, cast) \
174static ssize_t show_fcoe_ctlr_device_##field(struct device *dev, \
175 struct device_attribute *attr, \
176 char *buf) \
177{ \
178 struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev); \
179 return snprintf(buf, sz, format_string, cast fcoe_ctlr_##field(ctlr)); \
180}
181
182#define fcoe_fcf_private_show_function(field, format_string, sz, cast) \
183static ssize_t show_fcoe_fcf_device_##field(struct device *dev, \
184 struct device_attribute *attr, \
185 char *buf) \
186{ \
187 struct fcoe_fcf_device *fcf = dev_to_fcf(dev); \
188 return snprintf(buf, sz, format_string, cast fcoe_fcf_##field(fcf)); \
189}
190
191#define fcoe_ctlr_private_rd_attr(field, format_string, sz) \
192 fcoe_ctlr_private_show_function(field, format_string, sz, ) \
193 static FCOE_DEVICE_ATTR(ctlr, field, S_IRUGO, \
194 show_fcoe_ctlr_device_##field, NULL)
195
196#define fcoe_ctlr_rd_attr(field, format_string, sz) \
197 fcoe_ctlr_show_function(field, format_string, sz, ) \
198 static FCOE_DEVICE_ATTR(ctlr, field, S_IRUGO, \
199 show_fcoe_ctlr_device_##field, NULL)
200
201#define fcoe_fcf_rd_attr(field, format_string, sz) \
202 fcoe_fcf_show_function(field, format_string, sz, ) \
203 static FCOE_DEVICE_ATTR(fcf, field, S_IRUGO, \
204 show_fcoe_fcf_device_##field, NULL)
205
206#define fcoe_fcf_private_rd_attr(field, format_string, sz) \
207 fcoe_fcf_private_show_function(field, format_string, sz, ) \
208 static FCOE_DEVICE_ATTR(fcf, field, S_IRUGO, \
209 show_fcoe_fcf_device_##field, NULL)
210
211#define fcoe_ctlr_private_rd_attr_cast(field, format_string, sz, cast) \
212 fcoe_ctlr_private_show_function(field, format_string, sz, (cast)) \
213 static FCOE_DEVICE_ATTR(ctlr, field, S_IRUGO, \
214 show_fcoe_ctlr_device_##field, NULL)
215
216#define fcoe_fcf_private_rd_attr_cast(field, format_string, sz, cast) \
217 fcoe_fcf_private_show_function(field, format_string, sz, (cast)) \
218 static FCOE_DEVICE_ATTR(fcf, field, S_IRUGO, \
219 show_fcoe_fcf_device_##field, NULL)
220
221#define fcoe_enum_name_search(title, table_type, table) \
222static const char *get_fcoe_##title##_name(enum table_type table_key) \
223{ \
Robert Loveef60f672012-11-27 06:53:19 +0000224 if (table_key < 0 || table_key >= ARRAY_SIZE(table)) \
225 return NULL; \
226 return table[table_key]; \
Robert Love9a74e882012-05-22 19:06:21 -0700227}
228
Robert Loveef60f672012-11-27 06:53:19 +0000229static char *fip_conn_type_names[] = {
230 [ FIP_CONN_TYPE_UNKNOWN ] = "Unknown",
231 [ FIP_CONN_TYPE_FABRIC ] = "Fabric",
232 [ FIP_CONN_TYPE_VN2VN ] = "VN2VN",
233};
234fcoe_enum_name_search(ctlr_mode, fip_conn_type, fip_conn_type_names)
Robert Love6a891b02012-11-27 06:53:30 +0000235
236static enum fip_conn_type fcoe_parse_mode(const char *buf)
237{
238 int i;
239
240 for (i = 0; i < ARRAY_SIZE(fip_conn_type_names); i++) {
241 if (strcasecmp(buf, fip_conn_type_names[i]) == 0)
242 return i;
243 }
244
245 return FIP_CONN_TYPE_UNKNOWN;
246}
Robert Loveef60f672012-11-27 06:53:19 +0000247
248static char *fcf_state_names[] = {
249 [ FCOE_FCF_STATE_UNKNOWN ] = "Unknown",
250 [ FCOE_FCF_STATE_DISCONNECTED ] = "Disconnected",
251 [ FCOE_FCF_STATE_CONNECTED ] = "Connected",
Robert Love9a74e882012-05-22 19:06:21 -0700252};
253fcoe_enum_name_search(fcf_state, fcf_state, fcf_state_names)
254#define FCOE_FCF_STATE_MAX_NAMELEN 50
255
256static ssize_t show_fcf_state(struct device *dev,
257 struct device_attribute *attr,
258 char *buf)
259{
260 struct fcoe_fcf_device *fcf = dev_to_fcf(dev);
261 const char *name;
262 name = get_fcoe_fcf_state_name(fcf->state);
263 if (!name)
264 return -EINVAL;
265 return snprintf(buf, FCOE_FCF_STATE_MAX_NAMELEN, "%s\n", name);
266}
267static FCOE_DEVICE_ATTR(fcf, state, S_IRUGO, show_fcf_state, NULL);
268
Robert Loveef60f672012-11-27 06:53:19 +0000269#define FCOE_MAX_MODENAME_LEN 20
Robert Love9a74e882012-05-22 19:06:21 -0700270static ssize_t show_ctlr_mode(struct device *dev,
271 struct device_attribute *attr,
272 char *buf)
273{
274 struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
275 const char *name;
276
Robert Love9a74e882012-05-22 19:06:21 -0700277 name = get_fcoe_ctlr_mode_name(ctlr->mode);
278 if (!name)
279 return -EINVAL;
Robert Love6a891b02012-11-27 06:53:30 +0000280 return snprintf(buf, FCOE_MAX_MODENAME_LEN,
Robert Love9a74e882012-05-22 19:06:21 -0700281 "%s\n", name);
282}
Robert Love6a891b02012-11-27 06:53:30 +0000283
284static ssize_t store_ctlr_mode(struct device *dev,
285 struct device_attribute *attr,
286 const char *buf, size_t count)
287{
288 struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
289 char mode[FCOE_MAX_MODENAME_LEN + 1];
290
291 if (count > FCOE_MAX_MODENAME_LEN)
292 return -EINVAL;
293
294 strncpy(mode, buf, count);
295
296 if (mode[count - 1] == '\n')
297 mode[count - 1] = '\0';
298 else
299 mode[count] = '\0';
300
301 switch (ctlr->enabled) {
302 case FCOE_CTLR_ENABLED:
Bart Van Assche465b87b2013-08-14 15:42:09 +0000303 LIBFCOE_SYSFS_DBG(ctlr, "Cannot change mode when enabled.\n");
Robert Love6a891b02012-11-27 06:53:30 +0000304 return -EBUSY;
305 case FCOE_CTLR_DISABLED:
306 if (!ctlr->f->set_fcoe_ctlr_mode) {
307 LIBFCOE_SYSFS_DBG(ctlr,
Bart Van Assche465b87b2013-08-14 15:42:09 +0000308 "Mode change not supported by LLD.\n");
Robert Love6a891b02012-11-27 06:53:30 +0000309 return -ENOTSUPP;
310 }
311
312 ctlr->mode = fcoe_parse_mode(mode);
313 if (ctlr->mode == FIP_CONN_TYPE_UNKNOWN) {
Bart Van Assche465b87b2013-08-14 15:42:09 +0000314 LIBFCOE_SYSFS_DBG(ctlr, "Unknown mode %s provided.\n",
315 buf);
Robert Love6a891b02012-11-27 06:53:30 +0000316 return -EINVAL;
317 }
318
319 ctlr->f->set_fcoe_ctlr_mode(ctlr);
Bart Van Assche465b87b2013-08-14 15:42:09 +0000320 LIBFCOE_SYSFS_DBG(ctlr, "Mode changed to %s.\n", buf);
Robert Love6a891b02012-11-27 06:53:30 +0000321
322 return count;
323 case FCOE_CTLR_UNUSED:
324 default:
Bart Van Assche465b87b2013-08-14 15:42:09 +0000325 LIBFCOE_SYSFS_DBG(ctlr, "Mode change not supported.\n");
Robert Love6a891b02012-11-27 06:53:30 +0000326 return -ENOTSUPP;
327 };
328}
329
330static FCOE_DEVICE_ATTR(ctlr, mode, S_IRUGO | S_IWUSR,
331 show_ctlr_mode, store_ctlr_mode);
332
333static ssize_t store_ctlr_enabled(struct device *dev,
334 struct device_attribute *attr,
335 const char *buf, size_t count)
336{
337 struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
338 int rc;
339
340 switch (ctlr->enabled) {
341 case FCOE_CTLR_ENABLED:
342 if (*buf == '1')
343 return count;
344 ctlr->enabled = FCOE_CTLR_DISABLED;
345 break;
346 case FCOE_CTLR_DISABLED:
347 if (*buf == '0')
348 return count;
349 ctlr->enabled = FCOE_CTLR_ENABLED;
350 break;
351 case FCOE_CTLR_UNUSED:
352 return -ENOTSUPP;
353 };
354
355 rc = ctlr->f->set_fcoe_ctlr_enabled(ctlr);
356 if (rc)
357 return rc;
358
359 return count;
360}
361
362static char *ctlr_enabled_state_names[] = {
363 [ FCOE_CTLR_ENABLED ] = "1",
364 [ FCOE_CTLR_DISABLED ] = "0",
365};
366fcoe_enum_name_search(ctlr_enabled_state, ctlr_enabled_state,
367 ctlr_enabled_state_names)
368#define FCOE_CTLR_ENABLED_MAX_NAMELEN 50
369
370static ssize_t show_ctlr_enabled_state(struct device *dev,
371 struct device_attribute *attr,
372 char *buf)
373{
374 struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
375 const char *name;
376
377 name = get_fcoe_ctlr_enabled_state_name(ctlr->enabled);
378 if (!name)
379 return -EINVAL;
380 return snprintf(buf, FCOE_CTLR_ENABLED_MAX_NAMELEN,
381 "%s\n", name);
382}
383
384static FCOE_DEVICE_ATTR(ctlr, enabled, S_IRUGO | S_IWUSR,
385 show_ctlr_enabled_state,
386 store_ctlr_enabled);
Robert Love9a74e882012-05-22 19:06:21 -0700387
Hannes Reinecke9a6cf882016-07-19 13:49:40 +0200388static ssize_t store_ctlr_fip_resp(struct device *dev,
389 struct device_attribute *attr,
390 const char *buf, size_t count)
391{
392 struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
393 struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr);
394
395 mutex_lock(&fip->ctlr_mutex);
396 if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
397 if (buf[0] == '1') {
398 fip->fip_resp = 1;
399 mutex_unlock(&fip->ctlr_mutex);
400 return count;
401 }
402 if (buf[0] == '0') {
403 fip->fip_resp = 0;
404 mutex_unlock(&fip->ctlr_mutex);
405 return count;
406 }
407 }
408 mutex_unlock(&fip->ctlr_mutex);
409 return -EINVAL;
410}
411
412static ssize_t show_ctlr_fip_resp(struct device *dev,
413 struct device_attribute *attr,
414 char *buf)
415{
416 struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
417 struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr);
418
419 return sprintf(buf, "%d\n", fip->fip_resp ? 1 : 0);
420}
421
422static FCOE_DEVICE_ATTR(ctlr, fip_vlan_responder, S_IRUGO | S_IWUSR,
423 show_ctlr_fip_resp,
424 store_ctlr_fip_resp);
425
Robert Love9a74e882012-05-22 19:06:21 -0700426static ssize_t
427store_private_fcoe_ctlr_fcf_dev_loss_tmo(struct device *dev,
428 struct device_attribute *attr,
429 const char *buf, size_t count)
430{
431 struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
432 struct fcoe_fcf_device *fcf;
433 unsigned long val;
434 int rc;
435
436 rc = fcoe_str_to_dev_loss(buf, &val);
437 if (rc)
438 return rc;
439
440 fcoe_ctlr_fcf_dev_loss_tmo(ctlr) = val;
441 mutex_lock(&ctlr->lock);
442 list_for_each_entry(fcf, &ctlr->fcfs, peers)
443 fcoe_fcf_set_dev_loss_tmo(fcf, val);
444 mutex_unlock(&ctlr->lock);
445 return count;
446}
447fcoe_ctlr_private_show_function(fcf_dev_loss_tmo, "%d\n", 20, );
448static FCOE_DEVICE_ATTR(ctlr, fcf_dev_loss_tmo, S_IRUGO | S_IWUSR,
449 show_fcoe_ctlr_device_fcf_dev_loss_tmo,
450 store_private_fcoe_ctlr_fcf_dev_loss_tmo);
451
452/* Link Error Status Block (LESB) */
453fcoe_ctlr_rd_attr(link_fail, "%u\n", 20);
454fcoe_ctlr_rd_attr(vlink_fail, "%u\n", 20);
455fcoe_ctlr_rd_attr(miss_fka, "%u\n", 20);
456fcoe_ctlr_rd_attr(symb_err, "%u\n", 20);
457fcoe_ctlr_rd_attr(err_block, "%u\n", 20);
458fcoe_ctlr_rd_attr(fcs_error, "%u\n", 20);
459
460fcoe_fcf_private_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
461fcoe_fcf_private_rd_attr_cast(switch_name, "0x%llx\n", 20, unsigned long long);
462fcoe_fcf_private_rd_attr(priority, "%u\n", 20);
463fcoe_fcf_private_rd_attr(fc_map, "0x%x\n", 20);
464fcoe_fcf_private_rd_attr(vfid, "%u\n", 20);
465fcoe_fcf_private_rd_attr(mac, "%pM\n", 20);
466fcoe_fcf_private_rd_attr(fka_period, "%u\n", 20);
467fcoe_fcf_rd_attr(selected, "%u\n", 20);
468fcoe_fcf_rd_attr(vlan_id, "%u\n", 20);
469
470fcoe_fcf_private_show_function(dev_loss_tmo, "%d\n", 20, )
471static ssize_t
472store_fcoe_fcf_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
473 const char *buf, size_t count)
474{
475 struct fcoe_fcf_device *fcf = dev_to_fcf(dev);
476 unsigned long val;
477 int rc;
478
479 rc = fcoe_str_to_dev_loss(buf, &val);
480 if (rc)
481 return rc;
482
483 rc = fcoe_fcf_set_dev_loss_tmo(fcf, val);
484 if (rc)
485 return rc;
486 return count;
487}
488static FCOE_DEVICE_ATTR(fcf, dev_loss_tmo, S_IRUGO | S_IWUSR,
489 show_fcoe_fcf_device_dev_loss_tmo,
490 store_fcoe_fcf_dev_loss_tmo);
491
492static struct attribute *fcoe_ctlr_lesb_attrs[] = {
493 &device_attr_fcoe_ctlr_link_fail.attr,
494 &device_attr_fcoe_ctlr_vlink_fail.attr,
495 &device_attr_fcoe_ctlr_miss_fka.attr,
496 &device_attr_fcoe_ctlr_symb_err.attr,
497 &device_attr_fcoe_ctlr_err_block.attr,
498 &device_attr_fcoe_ctlr_fcs_error.attr,
499 NULL,
500};
501
502static struct attribute_group fcoe_ctlr_lesb_attr_group = {
503 .name = "lesb",
504 .attrs = fcoe_ctlr_lesb_attrs,
505};
506
507static struct attribute *fcoe_ctlr_attrs[] = {
Hannes Reinecke9a6cf882016-07-19 13:49:40 +0200508 &device_attr_fcoe_ctlr_fip_vlan_responder.attr,
Robert Love9a74e882012-05-22 19:06:21 -0700509 &device_attr_fcoe_ctlr_fcf_dev_loss_tmo.attr,
Robert Love6a891b02012-11-27 06:53:30 +0000510 &device_attr_fcoe_ctlr_enabled.attr,
Robert Love9a74e882012-05-22 19:06:21 -0700511 &device_attr_fcoe_ctlr_mode.attr,
512 NULL,
513};
514
515static struct attribute_group fcoe_ctlr_attr_group = {
516 .attrs = fcoe_ctlr_attrs,
517};
518
519static const struct attribute_group *fcoe_ctlr_attr_groups[] = {
520 &fcoe_ctlr_attr_group,
521 &fcoe_ctlr_lesb_attr_group,
522 NULL,
523};
524
525static struct attribute *fcoe_fcf_attrs[] = {
526 &device_attr_fcoe_fcf_fabric_name.attr,
527 &device_attr_fcoe_fcf_switch_name.attr,
528 &device_attr_fcoe_fcf_dev_loss_tmo.attr,
529 &device_attr_fcoe_fcf_fc_map.attr,
530 &device_attr_fcoe_fcf_vfid.attr,
531 &device_attr_fcoe_fcf_mac.attr,
532 &device_attr_fcoe_fcf_priority.attr,
533 &device_attr_fcoe_fcf_fka_period.attr,
534 &device_attr_fcoe_fcf_state.attr,
535 &device_attr_fcoe_fcf_selected.attr,
536 &device_attr_fcoe_fcf_vlan_id.attr,
537 NULL
538};
539
540static struct attribute_group fcoe_fcf_attr_group = {
541 .attrs = fcoe_fcf_attrs,
542};
543
544static const struct attribute_group *fcoe_fcf_attr_groups[] = {
545 &fcoe_fcf_attr_group,
546 NULL,
547};
548
Bart Van Assche7a5ed752013-06-25 17:49:40 +0000549static struct bus_type fcoe_bus_type;
Robert Love9a74e882012-05-22 19:06:21 -0700550
551static int fcoe_bus_match(struct device *dev,
552 struct device_driver *drv)
553{
554 if (dev->bus == &fcoe_bus_type)
555 return 1;
556 return 0;
557}
558
559/**
560 * fcoe_ctlr_device_release() - Release the FIP ctlr memory
561 * @dev: Pointer to the FIP ctlr's embedded device
562 *
563 * Called when the last FIP ctlr reference is released.
564 */
565static void fcoe_ctlr_device_release(struct device *dev)
566{
567 struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
568 kfree(ctlr);
569}
570
571/**
572 * fcoe_fcf_device_release() - Release the FIP fcf memory
573 * @dev: Pointer to the fcf's embedded device
574 *
575 * Called when the last FIP fcf reference is released.
576 */
577static void fcoe_fcf_device_release(struct device *dev)
578{
579 struct fcoe_fcf_device *fcf = dev_to_fcf(dev);
580 kfree(fcf);
581}
582
Bart Van Assche7a5ed752013-06-25 17:49:40 +0000583static struct device_type fcoe_ctlr_device_type = {
Robert Love9a74e882012-05-22 19:06:21 -0700584 .name = "fcoe_ctlr",
585 .groups = fcoe_ctlr_attr_groups,
586 .release = fcoe_ctlr_device_release,
587};
588
Bart Van Assche7a5ed752013-06-25 17:49:40 +0000589static struct device_type fcoe_fcf_device_type = {
Robert Love9a74e882012-05-22 19:06:21 -0700590 .name = "fcoe_fcf",
591 .groups = fcoe_fcf_attr_groups,
592 .release = fcoe_fcf_device_release,
593};
594
Greg Kroah-Hartmanc88dc552013-08-23 14:24:32 -0700595static BUS_ATTR(ctlr_create, S_IWUSR, NULL, fcoe_ctlr_create_store);
596static BUS_ATTR(ctlr_destroy, S_IWUSR, NULL, fcoe_ctlr_destroy_store);
597
598static struct attribute *fcoe_bus_attrs[] = {
599 &bus_attr_ctlr_create.attr,
Greg Kroah-Hartman1296fc02013-09-26 16:15:02 -0700600 &bus_attr_ctlr_destroy.attr,
Greg Kroah-Hartmanc88dc552013-08-23 14:24:32 -0700601 NULL,
Robert Love6a891b02012-11-27 06:53:30 +0000602};
Greg Kroah-Hartmanc88dc552013-08-23 14:24:32 -0700603ATTRIBUTE_GROUPS(fcoe_bus);
Robert Love6a891b02012-11-27 06:53:30 +0000604
Bart Van Assche7a5ed752013-06-25 17:49:40 +0000605static struct bus_type fcoe_bus_type = {
Robert Love9a74e882012-05-22 19:06:21 -0700606 .name = "fcoe",
607 .match = &fcoe_bus_match,
Greg Kroah-Hartmanc88dc552013-08-23 14:24:32 -0700608 .bus_groups = fcoe_bus_groups,
Robert Love9a74e882012-05-22 19:06:21 -0700609};
610
611/**
612 * fcoe_ctlr_device_flush_work() - Flush a FIP ctlr's workqueue
613 * @ctlr: Pointer to the FIP ctlr whose workqueue is to be flushed
614 */
Bart Van Assche7a5ed752013-06-25 17:49:40 +0000615static void fcoe_ctlr_device_flush_work(struct fcoe_ctlr_device *ctlr)
Robert Love9a74e882012-05-22 19:06:21 -0700616{
617 if (!fcoe_ctlr_work_q(ctlr)) {
618 printk(KERN_ERR
619 "ERROR: FIP Ctlr '%d' attempted to flush work, "
620 "when no workqueue created.\n", ctlr->id);
621 dump_stack();
622 return;
623 }
624
625 flush_workqueue(fcoe_ctlr_work_q(ctlr));
626}
627
628/**
629 * fcoe_ctlr_device_queue_work() - Schedule work for a FIP ctlr's workqueue
630 * @ctlr: Pointer to the FIP ctlr who owns the devloss workqueue
631 * @work: Work to queue for execution
632 *
633 * Return value:
634 * 1 on success / 0 already queued / < 0 for error
635 */
Bart Van Assche7a5ed752013-06-25 17:49:40 +0000636static int fcoe_ctlr_device_queue_work(struct fcoe_ctlr_device *ctlr,
637 struct work_struct *work)
Robert Love9a74e882012-05-22 19:06:21 -0700638{
639 if (unlikely(!fcoe_ctlr_work_q(ctlr))) {
640 printk(KERN_ERR
641 "ERROR: FIP Ctlr '%d' attempted to queue work, "
642 "when no workqueue created.\n", ctlr->id);
643 dump_stack();
644
645 return -EINVAL;
646 }
647
648 return queue_work(fcoe_ctlr_work_q(ctlr), work);
649}
650
651/**
652 * fcoe_ctlr_device_flush_devloss() - Flush a FIP ctlr's devloss workqueue
653 * @ctlr: Pointer to FIP ctlr whose workqueue is to be flushed
654 */
Bart Van Assche7a5ed752013-06-25 17:49:40 +0000655static void fcoe_ctlr_device_flush_devloss(struct fcoe_ctlr_device *ctlr)
Robert Love9a74e882012-05-22 19:06:21 -0700656{
657 if (!fcoe_ctlr_devloss_work_q(ctlr)) {
658 printk(KERN_ERR
659 "ERROR: FIP Ctlr '%d' attempted to flush work, "
660 "when no workqueue created.\n", ctlr->id);
661 dump_stack();
662 return;
663 }
664
665 flush_workqueue(fcoe_ctlr_devloss_work_q(ctlr));
666}
667
668/**
669 * fcoe_ctlr_device_queue_devloss_work() - Schedule work for a FIP ctlr's devloss workqueue
670 * @ctlr: Pointer to the FIP ctlr who owns the devloss workqueue
671 * @work: Work to queue for execution
672 * @delay: jiffies to delay the work queuing
673 *
674 * Return value:
675 * 1 on success / 0 already queued / < 0 for error
676 */
Bart Van Assche7a5ed752013-06-25 17:49:40 +0000677static int fcoe_ctlr_device_queue_devloss_work(struct fcoe_ctlr_device *ctlr,
678 struct delayed_work *work,
679 unsigned long delay)
Robert Love9a74e882012-05-22 19:06:21 -0700680{
681 if (unlikely(!fcoe_ctlr_devloss_work_q(ctlr))) {
682 printk(KERN_ERR
683 "ERROR: FIP Ctlr '%d' attempted to queue work, "
684 "when no workqueue created.\n", ctlr->id);
685 dump_stack();
686
687 return -EINVAL;
688 }
689
690 return queue_delayed_work(fcoe_ctlr_devloss_work_q(ctlr), work, delay);
691}
692
693static int fcoe_fcf_device_match(struct fcoe_fcf_device *new,
694 struct fcoe_fcf_device *old)
695{
696 if (new->switch_name == old->switch_name &&
697 new->fabric_name == old->fabric_name &&
698 new->fc_map == old->fc_map &&
Joe Perches6942df72013-09-02 03:32:33 +0000699 ether_addr_equal(new->mac, old->mac))
Robert Love9a74e882012-05-22 19:06:21 -0700700 return 1;
701 return 0;
702}
703
704/**
705 * fcoe_ctlr_device_add() - Add a FIP ctlr to sysfs
706 * @parent: The parent device to which the fcoe_ctlr instance
707 * should be attached
708 * @f: The LLD's FCoE sysfs function template pointer
709 * @priv_size: Size to be allocated with the fcoe_ctlr_device for the LLD
710 *
711 * This routine allocates a FIP ctlr object with some additional memory
712 * for the LLD. The FIP ctlr is initialized, added to sysfs and then
713 * attributes are added to it.
714 */
715struct fcoe_ctlr_device *fcoe_ctlr_device_add(struct device *parent,
716 struct fcoe_sysfs_function_template *f,
717 int priv_size)
718{
719 struct fcoe_ctlr_device *ctlr;
720 int error = 0;
721
722 ctlr = kzalloc(sizeof(struct fcoe_ctlr_device) + priv_size,
723 GFP_KERNEL);
724 if (!ctlr)
725 goto out;
726
727 ctlr->id = atomic_inc_return(&ctlr_num) - 1;
728 ctlr->f = f;
Robert Love6a891b02012-11-27 06:53:30 +0000729 ctlr->mode = FIP_CONN_TYPE_FABRIC;
Robert Love9a74e882012-05-22 19:06:21 -0700730 INIT_LIST_HEAD(&ctlr->fcfs);
731 mutex_init(&ctlr->lock);
732 ctlr->dev.parent = parent;
733 ctlr->dev.bus = &fcoe_bus_type;
734 ctlr->dev.type = &fcoe_ctlr_device_type;
735
736 ctlr->fcf_dev_loss_tmo = fcoe_fcf_dev_loss_tmo;
737
738 snprintf(ctlr->work_q_name, sizeof(ctlr->work_q_name),
739 "ctlr_wq_%d", ctlr->id);
740 ctlr->work_q = create_singlethread_workqueue(
741 ctlr->work_q_name);
742 if (!ctlr->work_q)
743 goto out_del;
744
745 snprintf(ctlr->devloss_work_q_name,
746 sizeof(ctlr->devloss_work_q_name),
747 "ctlr_dl_wq_%d", ctlr->id);
748 ctlr->devloss_work_q = create_singlethread_workqueue(
749 ctlr->devloss_work_q_name);
750 if (!ctlr->devloss_work_q)
751 goto out_del_q;
752
753 dev_set_name(&ctlr->dev, "ctlr_%d", ctlr->id);
754 error = device_register(&ctlr->dev);
755 if (error)
756 goto out_del_q2;
757
758 return ctlr;
759
760out_del_q2:
761 destroy_workqueue(ctlr->devloss_work_q);
762 ctlr->devloss_work_q = NULL;
763out_del_q:
764 destroy_workqueue(ctlr->work_q);
765 ctlr->work_q = NULL;
766out_del:
767 kfree(ctlr);
768out:
769 return NULL;
770}
771EXPORT_SYMBOL_GPL(fcoe_ctlr_device_add);
772
773/**
774 * fcoe_ctlr_device_delete() - Delete a FIP ctlr and its subtree from sysfs
775 * @ctlr: A pointer to the ctlr to be deleted
776 *
777 * Deletes a FIP ctlr and any fcfs attached
778 * to it. Deleting fcfs will cause their childen
779 * to be deleted as well.
780 *
781 * The ctlr is detached from sysfs and it's resources
782 * are freed (work q), but the memory is not freed
783 * until its last reference is released.
784 *
785 * This routine expects no locks to be held before
786 * calling.
787 *
788 * TODO: Currently there are no callbacks to clean up LLD data
789 * for a fcoe_fcf_device. LLDs must keep this in mind as they need
790 * to clean up each of their LLD data for all fcoe_fcf_device before
791 * calling fcoe_ctlr_device_delete.
792 */
793void fcoe_ctlr_device_delete(struct fcoe_ctlr_device *ctlr)
794{
795 struct fcoe_fcf_device *fcf, *next;
796 /* Remove any attached fcfs */
797 mutex_lock(&ctlr->lock);
798 list_for_each_entry_safe(fcf, next,
799 &ctlr->fcfs, peers) {
800 list_del(&fcf->peers);
801 fcf->state = FCOE_FCF_STATE_DELETED;
802 fcoe_ctlr_device_queue_work(ctlr, &fcf->delete_work);
803 }
804 mutex_unlock(&ctlr->lock);
805
806 fcoe_ctlr_device_flush_work(ctlr);
807
808 destroy_workqueue(ctlr->devloss_work_q);
809 ctlr->devloss_work_q = NULL;
810 destroy_workqueue(ctlr->work_q);
811 ctlr->work_q = NULL;
812
813 device_unregister(&ctlr->dev);
814}
815EXPORT_SYMBOL_GPL(fcoe_ctlr_device_delete);
816
817/**
818 * fcoe_fcf_device_final_delete() - Final delete routine
819 * @work: The FIP fcf's embedded work struct
820 *
821 * It is expected that the fcf has been removed from
822 * the FIP ctlr's list before calling this routine.
823 */
824static void fcoe_fcf_device_final_delete(struct work_struct *work)
825{
826 struct fcoe_fcf_device *fcf =
827 container_of(work, struct fcoe_fcf_device, delete_work);
828 struct fcoe_ctlr_device *ctlr = fcoe_fcf_dev_to_ctlr_dev(fcf);
829
830 /*
831 * Cancel any outstanding timers. These should really exist
832 * only when rmmod'ing the LLDD and we're asking for
833 * immediate termination of the rports
834 */
835 if (!cancel_delayed_work(&fcf->dev_loss_work))
836 fcoe_ctlr_device_flush_devloss(ctlr);
837
838 device_unregister(&fcf->dev);
839}
840
841/**
842 * fip_timeout_deleted_fcf() - Delete a fcf when the devloss timer fires
843 * @work: The FIP fcf's embedded work struct
844 *
845 * Removes the fcf from the FIP ctlr's list of fcfs and
846 * queues the final deletion.
847 */
848static void fip_timeout_deleted_fcf(struct work_struct *work)
849{
850 struct fcoe_fcf_device *fcf =
851 container_of(work, struct fcoe_fcf_device, dev_loss_work.work);
852 struct fcoe_ctlr_device *ctlr = fcoe_fcf_dev_to_ctlr_dev(fcf);
853
854 mutex_lock(&ctlr->lock);
855
856 /*
857 * If the fcf is deleted or reconnected before the timer
858 * fires the devloss queue will be flushed, but the state will
859 * either be CONNECTED or DELETED. If that is the case we
860 * cancel deleting the fcf.
861 */
862 if (fcf->state != FCOE_FCF_STATE_DISCONNECTED)
863 goto out;
864
865 dev_printk(KERN_ERR, &fcf->dev,
866 "FIP fcf connection time out: removing fcf\n");
867
868 list_del(&fcf->peers);
869 fcf->state = FCOE_FCF_STATE_DELETED;
870 fcoe_ctlr_device_queue_work(ctlr, &fcf->delete_work);
871
872out:
873 mutex_unlock(&ctlr->lock);
874}
875
876/**
877 * fcoe_fcf_device_delete() - Delete a FIP fcf
878 * @fcf: Pointer to the fcf which is to be deleted
879 *
880 * Queues the FIP fcf on the devloss workqueue
881 *
882 * Expects the ctlr_attrs mutex to be held for fcf
883 * state change.
884 */
885void fcoe_fcf_device_delete(struct fcoe_fcf_device *fcf)
886{
887 struct fcoe_ctlr_device *ctlr = fcoe_fcf_dev_to_ctlr_dev(fcf);
888 int timeout = fcf->dev_loss_tmo;
889
890 if (fcf->state != FCOE_FCF_STATE_CONNECTED)
891 return;
892
893 fcf->state = FCOE_FCF_STATE_DISCONNECTED;
894
895 /*
896 * FCF will only be re-connected by the LLD calling
897 * fcoe_fcf_device_add, and it should be setting up
898 * priv then.
899 */
900 fcf->priv = NULL;
901
902 fcoe_ctlr_device_queue_devloss_work(ctlr, &fcf->dev_loss_work,
903 timeout * HZ);
904}
905EXPORT_SYMBOL_GPL(fcoe_fcf_device_delete);
906
907/**
908 * fcoe_fcf_device_add() - Add a FCoE sysfs fcoe_fcf_device to the system
909 * @ctlr: The fcoe_ctlr_device that will be the fcoe_fcf_device parent
910 * @new_fcf: A temporary FCF used for lookups on the current list of fcfs
911 *
912 * Expects to be called with the ctlr->lock held
913 */
914struct fcoe_fcf_device *fcoe_fcf_device_add(struct fcoe_ctlr_device *ctlr,
915 struct fcoe_fcf_device *new_fcf)
916{
917 struct fcoe_fcf_device *fcf;
918 int error = 0;
919
920 list_for_each_entry(fcf, &ctlr->fcfs, peers) {
921 if (fcoe_fcf_device_match(new_fcf, fcf)) {
922 if (fcf->state == FCOE_FCF_STATE_CONNECTED)
923 return fcf;
924
925 fcf->state = FCOE_FCF_STATE_CONNECTED;
926
927 if (!cancel_delayed_work(&fcf->dev_loss_work))
928 fcoe_ctlr_device_flush_devloss(ctlr);
929
930 return fcf;
931 }
932 }
933
934 fcf = kzalloc(sizeof(struct fcoe_fcf_device), GFP_ATOMIC);
935 if (unlikely(!fcf))
936 goto out;
937
938 INIT_WORK(&fcf->delete_work, fcoe_fcf_device_final_delete);
939 INIT_DELAYED_WORK(&fcf->dev_loss_work, fip_timeout_deleted_fcf);
940
941 fcf->dev.parent = &ctlr->dev;
942 fcf->dev.bus = &fcoe_bus_type;
943 fcf->dev.type = &fcoe_fcf_device_type;
944 fcf->id = atomic_inc_return(&fcf_num) - 1;
945 fcf->state = FCOE_FCF_STATE_UNKNOWN;
946
947 fcf->dev_loss_tmo = ctlr->fcf_dev_loss_tmo;
948
949 dev_set_name(&fcf->dev, "fcf_%d", fcf->id);
950
951 fcf->fabric_name = new_fcf->fabric_name;
952 fcf->switch_name = new_fcf->switch_name;
953 fcf->fc_map = new_fcf->fc_map;
954 fcf->vfid = new_fcf->vfid;
955 memcpy(fcf->mac, new_fcf->mac, ETH_ALEN);
956 fcf->priority = new_fcf->priority;
957 fcf->fka_period = new_fcf->fka_period;
958 fcf->selected = new_fcf->selected;
959
960 error = device_register(&fcf->dev);
961 if (error)
962 goto out_del;
963
964 fcf->state = FCOE_FCF_STATE_CONNECTED;
965 list_add_tail(&fcf->peers, &ctlr->fcfs);
966
967 return fcf;
968
969out_del:
970 kfree(fcf);
971out:
972 return NULL;
973}
974EXPORT_SYMBOL_GPL(fcoe_fcf_device_add);
975
976int __init fcoe_sysfs_setup(void)
977{
978 int error;
979
980 atomic_set(&ctlr_num, 0);
981 atomic_set(&fcf_num, 0);
982
983 error = bus_register(&fcoe_bus_type);
984 if (error)
985 return error;
986
987 return 0;
988}
989
990void __exit fcoe_sysfs_teardown(void)
991{
992 bus_unregister(&fcoe_bus_type);
993}