blob: 5aeb3b541c804a04db0c696eefd3369b8215f444 [file] [log] [blame]
Konrad Rzeszutek138fe4e2008-04-09 19:50:41 -07001/*
2 * Copyright 2007 Red Hat, Inc.
3 * by Peter Jones <pjones@redhat.com>
4 * Copyright 2008 IBM, Inc.
5 * by Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
6 * Copyright 2008
7 * by Konrad Rzeszutek <ketuzsezr@darnok.org>
8 *
9 * This code exposes the iSCSI Boot Format Table to userland via sysfs.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License v2.0 as published by
13 * the Free Software Foundation
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * Changelog:
21 *
22 * 14 Mar 2008 - Konrad Rzeszutek <ketuzsezr@darnok.org>
23 * Updated comments and copyrights. (v0.4.9)
24 *
25 * 11 Feb 2008 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
26 * Converted to using ibft_addr. (v0.4.8)
27 *
28 * 8 Feb 2008 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
29 * Combined two functions in one: reserve_ibft_region. (v0.4.7)
30 *
31 * 30 Jan 2008 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
32 * Added logic to handle IPv6 addresses. (v0.4.6)
33 *
34 * 25 Jan 2008 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
35 * Added logic to handle badly not-to-spec iBFT. (v0.4.5)
36 *
37 * 4 Jan 2008 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
38 * Added __init to function declarations. (v0.4.4)
39 *
40 * 21 Dec 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
41 * Updated kobject registration, combined unregister functions in one
42 * and code and style cleanup. (v0.4.3)
43 *
44 * 5 Dec 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
45 * Added end-markers to enums and re-organized kobject registration. (v0.4.2)
46 *
47 * 4 Dec 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
48 * Created 'device' sysfs link to the NIC and style cleanup. (v0.4.1)
49 *
50 * 28 Nov 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
51 * Added sysfs-ibft documentation, moved 'find_ibft' function to
52 * in its own file and added text attributes for every struct field. (v0.4)
53 *
54 * 21 Nov 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
55 * Added text attributes emulating OpenFirmware /proc/device-tree naming.
56 * Removed binary /sysfs interface (v0.3)
57 *
58 * 29 Aug 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
59 * Added functionality in setup.c to reserve iBFT region. (v0.2)
60 *
61 * 27 Aug 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
62 * First version exposing iBFT data via a binary /sysfs. (v0.1)
63 *
64 */
65
66
67#include <linux/blkdev.h>
68#include <linux/capability.h>
69#include <linux/ctype.h>
70#include <linux/device.h>
71#include <linux/err.h>
72#include <linux/init.h>
73#include <linux/iscsi_ibft.h>
74#include <linux/limits.h>
75#include <linux/module.h>
76#include <linux/pci.h>
77#include <linux/slab.h>
78#include <linux/stat.h>
79#include <linux/string.h>
80#include <linux/types.h>
81
82#define IBFT_ISCSI_VERSION "0.4.9"
83#define IBFT_ISCSI_DATE "2008-Mar-14"
84
85MODULE_AUTHOR("Peter Jones <pjones@redhat.com> and \
86Konrad Rzeszutek <ketuzsezr@darnok.org>");
87MODULE_DESCRIPTION("sysfs interface to BIOS iBFT information");
88MODULE_LICENSE("GPL");
89MODULE_VERSION(IBFT_ISCSI_VERSION);
90
91struct ibft_hdr {
92 u8 id;
93 u8 version;
94 u16 length;
95 u8 index;
96 u8 flags;
97} __attribute__((__packed__));
98
99struct ibft_control {
100 struct ibft_hdr hdr;
101 u16 extensions;
102 u16 initiator_off;
103 u16 nic0_off;
104 u16 tgt0_off;
105 u16 nic1_off;
106 u16 tgt1_off;
107} __attribute__((__packed__));
108
109struct ibft_initiator {
110 struct ibft_hdr hdr;
111 char isns_server[16];
112 char slp_server[16];
113 char pri_radius_server[16];
114 char sec_radius_server[16];
115 u16 initiator_name_len;
116 u16 initiator_name_off;
117} __attribute__((__packed__));
118
119struct ibft_nic {
120 struct ibft_hdr hdr;
121 char ip_addr[16];
122 u8 subnet_mask_prefix;
123 u8 origin;
124 char gateway[16];
125 char primary_dns[16];
126 char secondary_dns[16];
127 char dhcp[16];
128 u16 vlan;
129 char mac[6];
130 u16 pci_bdf;
131 u16 hostname_len;
132 u16 hostname_off;
133} __attribute__((__packed__));
134
135struct ibft_tgt {
136 struct ibft_hdr hdr;
137 char ip_addr[16];
138 u16 port;
139 char lun[8];
140 u8 chap_type;
141 u8 nic_assoc;
142 u16 tgt_name_len;
143 u16 tgt_name_off;
144 u16 chap_name_len;
145 u16 chap_name_off;
146 u16 chap_secret_len;
147 u16 chap_secret_off;
148 u16 rev_chap_name_len;
149 u16 rev_chap_name_off;
150 u16 rev_chap_secret_len;
151 u16 rev_chap_secret_off;
152} __attribute__((__packed__));
153
154/*
155 * The kobject different types and its names.
156 *
157*/
158enum ibft_id {
159 id_reserved = 0, /* We don't support. */
160 id_control = 1, /* Should show up only once and is not exported. */
161 id_initiator = 2,
162 id_nic = 3,
163 id_target = 4,
164 id_extensions = 5, /* We don't support. */
165 id_end_marker,
166};
167
168/*
169 * We do not support the other types, hence the usage of NULL.
170 * This maps to the enum ibft_id.
171 */
172static const char *ibft_id_names[] =
173 {NULL, NULL, "initiator", "ethernet%d", "target%d", NULL, NULL};
174
175/*
176 * The text attributes names for each of the kobjects.
177*/
178enum ibft_eth_properties_enum {
179 ibft_eth_index,
180 ibft_eth_flags,
181 ibft_eth_ip_addr,
182 ibft_eth_subnet_mask,
183 ibft_eth_origin,
184 ibft_eth_gateway,
185 ibft_eth_primary_dns,
186 ibft_eth_secondary_dns,
187 ibft_eth_dhcp,
188 ibft_eth_vlan,
189 ibft_eth_mac,
190 /* ibft_eth_pci_bdf - this is replaced by link to the device itself. */
191 ibft_eth_hostname,
192 ibft_eth_end_marker,
193};
194
195static const char *ibft_eth_properties[] =
196 {"index", "flags", "ip-addr", "subnet-mask", "origin", "gateway",
197 "primary-dns", "secondary-dns", "dhcp", "vlan", "mac", "hostname",
198 NULL};
199
200enum ibft_tgt_properties_enum {
201 ibft_tgt_index,
202 ibft_tgt_flags,
203 ibft_tgt_ip_addr,
204 ibft_tgt_port,
205 ibft_tgt_lun,
206 ibft_tgt_chap_type,
207 ibft_tgt_nic_assoc,
208 ibft_tgt_name,
209 ibft_tgt_chap_name,
210 ibft_tgt_chap_secret,
211 ibft_tgt_rev_chap_name,
212 ibft_tgt_rev_chap_secret,
213 ibft_tgt_end_marker,
214};
215
216static const char *ibft_tgt_properties[] =
217 {"index", "flags", "ip-addr", "port", "lun", "chap-type", "nic-assoc",
218 "target-name", "chap-name", "chap-secret", "rev-chap-name",
219 "rev-chap-name-secret", NULL};
220
221enum ibft_initiator_properties_enum {
222 ibft_init_index,
223 ibft_init_flags,
224 ibft_init_isns_server,
225 ibft_init_slp_server,
226 ibft_init_pri_radius_server,
227 ibft_init_sec_radius_server,
228 ibft_init_initiator_name,
229 ibft_init_end_marker,
230};
231
232static const char *ibft_initiator_properties[] =
233 {"index", "flags", "isns-server", "slp-server", "pri-radius-server",
234 "sec-radius-server", "initiator-name", NULL};
235
236/*
237 * The kobject and attribute structures.
238 */
239
240struct ibft_kobject {
241 struct ibft_table_header *header;
242 union {
243 struct ibft_initiator *initiator;
244 struct ibft_nic *nic;
245 struct ibft_tgt *tgt;
246 struct ibft_hdr *hdr;
247 };
248 struct kobject kobj;
249 struct list_head node;
250};
251
252struct ibft_attribute {
253 struct attribute attr;
254 ssize_t (*show) (struct ibft_kobject *entry,
255 struct ibft_attribute *attr, char *buf);
256 union {
257 struct ibft_initiator *initiator;
258 struct ibft_nic *nic;
259 struct ibft_tgt *tgt;
260 struct ibft_hdr *hdr;
261 };
262 struct kobject *kobj;
263 int type; /* The enum of the type. This can be any value of:
264 ibft_eth_properties_enum, ibft_tgt_properties_enum,
265 or ibft_initiator_properties_enum. */
266 struct list_head node;
267};
268
269static LIST_HEAD(ibft_attr_list);
270static LIST_HEAD(ibft_kobject_list);
271
272static const char nulls[16];
273
274/*
275 * Helper functions to parse data properly.
276 */
277static ssize_t sprintf_ipaddr(char *buf, u8 *ip)
278{
279 char *str = buf;
280
281 if (ip[0] == 0 && ip[1] == 0 && ip[2] == 0 && ip[3] == 0 &&
282 ip[4] == 0 && ip[5] == 0 && ip[6] == 0 && ip[7] == 0 &&
283 ip[8] == 0 && ip[9] == 0 && ip[10] == 0xff && ip[11] == 0xff) {
284 /*
285 * IPV4
286 */
Harvey Harrison63779432008-10-31 00:56:00 -0700287 str += sprintf(buf, "%pI4", ip + 12);
Konrad Rzeszutek138fe4e2008-04-09 19:50:41 -0700288 } else {
289 /*
290 * IPv6
291 */
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700292 str += sprintf(str, "%pI6", ip);
Konrad Rzeszutek138fe4e2008-04-09 19:50:41 -0700293 }
294 str += sprintf(str, "\n");
295 return str - buf;
296}
297
298static ssize_t sprintf_string(char *str, int len, char *buf)
299{
300 return sprintf(str, "%.*s\n", len, buf);
301}
302
303/*
304 * Helper function to verify the IBFT header.
305 */
306static int ibft_verify_hdr(char *t, struct ibft_hdr *hdr, int id, int length)
307{
308 if (hdr->id != id) {
309 printk(KERN_ERR "iBFT error: We expected the " \
310 "field header.id to have %d but " \
311 "found %d instead!\n", id, hdr->id);
312 return -ENODEV;
313 }
314 if (hdr->length != length) {
315 printk(KERN_ERR "iBFT error: We expected the " \
316 "field header.length to have %d but " \
317 "found %d instead!\n", length, hdr->length);
318 return -ENODEV;
319 }
320
321 return 0;
322}
323
324static void ibft_release(struct kobject *kobj)
325{
326 struct ibft_kobject *ibft =
327 container_of(kobj, struct ibft_kobject, kobj);
328 kfree(ibft);
329}
330
331/*
332 * Routines for parsing the iBFT data to be human readable.
333 */
Adrian Bunk9cf899d2008-08-12 15:43:59 -0400334static ssize_t ibft_attr_show_initiator(struct ibft_kobject *entry,
335 struct ibft_attribute *attr,
336 char *buf)
Konrad Rzeszutek138fe4e2008-04-09 19:50:41 -0700337{
338 struct ibft_initiator *initiator = entry->initiator;
339 void *ibft_loc = entry->header;
340 char *str = buf;
341
342 if (!initiator)
343 return 0;
344
345 switch (attr->type) {
346 case ibft_init_index:
347 str += sprintf(str, "%d\n", initiator->hdr.index);
348 break;
349 case ibft_init_flags:
350 str += sprintf(str, "%d\n", initiator->hdr.flags);
351 break;
352 case ibft_init_isns_server:
353 str += sprintf_ipaddr(str, initiator->isns_server);
354 break;
355 case ibft_init_slp_server:
356 str += sprintf_ipaddr(str, initiator->slp_server);
357 break;
358 case ibft_init_pri_radius_server:
359 str += sprintf_ipaddr(str, initiator->pri_radius_server);
360 break;
361 case ibft_init_sec_radius_server:
362 str += sprintf_ipaddr(str, initiator->sec_radius_server);
363 break;
364 case ibft_init_initiator_name:
365 str += sprintf_string(str, initiator->initiator_name_len,
366 (char *)ibft_loc +
367 initiator->initiator_name_off);
368 break;
369 default:
370 break;
371 }
372
373 return str - buf;
374}
375
Adrian Bunk9cf899d2008-08-12 15:43:59 -0400376static ssize_t ibft_attr_show_nic(struct ibft_kobject *entry,
377 struct ibft_attribute *attr,
378 char *buf)
Konrad Rzeszutek138fe4e2008-04-09 19:50:41 -0700379{
380 struct ibft_nic *nic = entry->nic;
381 void *ibft_loc = entry->header;
382 char *str = buf;
Konrad Rzeszutek138fe4e2008-04-09 19:50:41 -0700383 int val;
384
385 if (!nic)
386 return 0;
387
388 switch (attr->type) {
389 case ibft_eth_index:
390 str += sprintf(str, "%d\n", nic->hdr.index);
391 break;
392 case ibft_eth_flags:
393 str += sprintf(str, "%d\n", nic->hdr.flags);
394 break;
395 case ibft_eth_ip_addr:
396 str += sprintf_ipaddr(str, nic->ip_addr);
397 break;
398 case ibft_eth_subnet_mask:
399 val = ~((1 << (32-nic->subnet_mask_prefix))-1);
400 str += sprintf(str, NIPQUAD_FMT,
401 (u8)(val >> 24), (u8)(val >> 16),
402 (u8)(val >> 8), (u8)(val));
403 break;
404 case ibft_eth_origin:
405 str += sprintf(str, "%d\n", nic->origin);
406 break;
407 case ibft_eth_gateway:
408 str += sprintf_ipaddr(str, nic->gateway);
409 break;
410 case ibft_eth_primary_dns:
411 str += sprintf_ipaddr(str, nic->primary_dns);
412 break;
413 case ibft_eth_secondary_dns:
414 str += sprintf_ipaddr(str, nic->secondary_dns);
415 break;
416 case ibft_eth_dhcp:
417 str += sprintf_ipaddr(str, nic->dhcp);
418 break;
419 case ibft_eth_vlan:
420 str += sprintf(str, "%d\n", nic->vlan);
421 break;
422 case ibft_eth_mac:
hartleys2c352942010-01-05 06:37:43 +0000423 str += sprintf(str, "%pM\n", nic->mac);
Konrad Rzeszutek138fe4e2008-04-09 19:50:41 -0700424 break;
425 case ibft_eth_hostname:
426 str += sprintf_string(str, nic->hostname_len,
427 (char *)ibft_loc + nic->hostname_off);
428 break;
429 default:
430 break;
431 }
432
433 return str - buf;
434};
435
Adrian Bunk9cf899d2008-08-12 15:43:59 -0400436static ssize_t ibft_attr_show_target(struct ibft_kobject *entry,
437 struct ibft_attribute *attr,
438 char *buf)
Konrad Rzeszutek138fe4e2008-04-09 19:50:41 -0700439{
440 struct ibft_tgt *tgt = entry->tgt;
441 void *ibft_loc = entry->header;
442 char *str = buf;
443 int i;
444
445 if (!tgt)
446 return 0;
447
448 switch (attr->type) {
449 case ibft_tgt_index:
450 str += sprintf(str, "%d\n", tgt->hdr.index);
451 break;
452 case ibft_tgt_flags:
453 str += sprintf(str, "%d\n", tgt->hdr.flags);
454 break;
455 case ibft_tgt_ip_addr:
456 str += sprintf_ipaddr(str, tgt->ip_addr);
457 break;
458 case ibft_tgt_port:
459 str += sprintf(str, "%d\n", tgt->port);
460 break;
461 case ibft_tgt_lun:
462 for (i = 0; i < 8; i++)
463 str += sprintf(str, "%x", (u8)tgt->lun[i]);
464 str += sprintf(str, "\n");
465 break;
466 case ibft_tgt_nic_assoc:
467 str += sprintf(str, "%d\n", tgt->nic_assoc);
468 break;
469 case ibft_tgt_chap_type:
470 str += sprintf(str, "%d\n", tgt->chap_type);
471 break;
472 case ibft_tgt_name:
473 str += sprintf_string(str, tgt->tgt_name_len,
474 (char *)ibft_loc + tgt->tgt_name_off);
475 break;
476 case ibft_tgt_chap_name:
477 str += sprintf_string(str, tgt->chap_name_len,
478 (char *)ibft_loc + tgt->chap_name_off);
479 break;
480 case ibft_tgt_chap_secret:
481 str += sprintf_string(str, tgt->chap_secret_len,
482 (char *)ibft_loc + tgt->chap_secret_off);
483 break;
484 case ibft_tgt_rev_chap_name:
485 str += sprintf_string(str, tgt->rev_chap_name_len,
486 (char *)ibft_loc +
487 tgt->rev_chap_name_off);
488 break;
489 case ibft_tgt_rev_chap_secret:
490 str += sprintf_string(str, tgt->rev_chap_secret_len,
491 (char *)ibft_loc +
492 tgt->rev_chap_secret_off);
493 break;
494 default:
495 break;
496 }
497
498 return str - buf;
499}
500
501/*
502 * The routine called for all sysfs attributes.
503 */
504static ssize_t ibft_show_attribute(struct kobject *kobj,
505 struct attribute *attr,
506 char *buf)
507{
508 struct ibft_kobject *dev =
509 container_of(kobj, struct ibft_kobject, kobj);
510 struct ibft_attribute *ibft_attr =
511 container_of(attr, struct ibft_attribute, attr);
512 ssize_t ret = -EIO;
513 char *str = buf;
514
515 if (!capable(CAP_SYS_ADMIN))
516 return -EACCES;
517
518 if (ibft_attr->show)
519 ret = ibft_attr->show(dev, ibft_attr, str);
520
521 return ret;
522}
523
524static struct sysfs_ops ibft_attr_ops = {
525 .show = ibft_show_attribute,
526};
527
528static struct kobj_type ibft_ktype = {
529 .release = ibft_release,
530 .sysfs_ops = &ibft_attr_ops,
531};
532
533static struct kset *ibft_kset;
534
535static int __init ibft_check_device(void)
536{
537 int len;
538 u8 *pos;
539 u8 csum = 0;
540
541 len = ibft_addr->length;
542
543 /* Sanity checking of iBFT. */
544 if (ibft_addr->revision != 1) {
545 printk(KERN_ERR "iBFT module supports only revision 1, " \
546 "while this is %d.\n", ibft_addr->revision);
547 return -ENOENT;
548 }
549 for (pos = (u8 *)ibft_addr; pos < (u8 *)ibft_addr + len; pos++)
550 csum += *pos;
551
552 if (csum) {
553 printk(KERN_ERR "iBFT has incorrect checksum (0x%x)!\n", csum);
554 return -ENOENT;
555 }
556
557 return 0;
558}
559
560/*
561 * Helper function for ibft_register_kobjects.
562 */
563static int __init ibft_create_kobject(struct ibft_table_header *header,
564 struct ibft_hdr *hdr,
565 struct list_head *list)
566{
567 struct ibft_kobject *ibft_kobj = NULL;
568 struct ibft_nic *nic = (struct ibft_nic *)hdr;
569 struct pci_dev *pci_dev;
570 int rc = 0;
571
572 ibft_kobj = kzalloc(sizeof(*ibft_kobj), GFP_KERNEL);
573 if (!ibft_kobj)
574 return -ENOMEM;
575
576 ibft_kobj->header = header;
577 ibft_kobj->hdr = hdr;
578
579 switch (hdr->id) {
580 case id_initiator:
581 rc = ibft_verify_hdr("initiator", hdr, id_initiator,
582 sizeof(*ibft_kobj->initiator));
583 break;
584 case id_nic:
585 rc = ibft_verify_hdr("ethernet", hdr, id_nic,
586 sizeof(*ibft_kobj->nic));
587 break;
588 case id_target:
589 rc = ibft_verify_hdr("target", hdr, id_target,
590 sizeof(*ibft_kobj->tgt));
591 break;
592 case id_reserved:
593 case id_control:
594 case id_extensions:
595 /* Fields which we don't support. Ignore them */
596 rc = 1;
597 break;
598 default:
599 printk(KERN_ERR "iBFT has unknown structure type (%d). " \
600 "Report this bug to %.6s!\n", hdr->id,
601 header->oem_id);
602 rc = 1;
603 break;
604 }
605
606 if (rc) {
607 /* Skip adding this kobject, but exit with non-fatal error. */
608 kfree(ibft_kobj);
609 goto out_invalid_struct;
610 }
611
612 ibft_kobj->kobj.kset = ibft_kset;
613
614 rc = kobject_init_and_add(&ibft_kobj->kobj, &ibft_ktype,
615 NULL, ibft_id_names[hdr->id], hdr->index);
616
617 if (rc) {
618 kfree(ibft_kobj);
619 goto out;
620 }
621
622 kobject_uevent(&ibft_kobj->kobj, KOBJ_ADD);
623
624 if (hdr->id == id_nic) {
625 /*
626 * We don't search for the device in other domains than
627 * zero. This is because on x86 platforms the BIOS
628 * executes only devices which are in domain 0. Furthermore, the
629 * iBFT spec doesn't have a domain id field :-(
630 */
631 pci_dev = pci_get_bus_and_slot((nic->pci_bdf & 0xff00) >> 8,
632 (nic->pci_bdf & 0xff));
633 if (pci_dev) {
634 rc = sysfs_create_link(&ibft_kobj->kobj,
635 &pci_dev->dev.kobj, "device");
636 pci_dev_put(pci_dev);
637 }
638 }
639
640 /* Nothing broke so lets add it to the list. */
641 list_add_tail(&ibft_kobj->node, list);
642out:
643 return rc;
644out_invalid_struct:
645 /* Unsupported structs are skipped. */
646 return 0;
647}
648
649/*
650 * Scan the IBFT table structure for the NIC and Target fields. When
651 * found add them on the passed-in list. We do not support the other
652 * fields at this point, so they are skipped.
653 */
654static int __init ibft_register_kobjects(struct ibft_table_header *header,
655 struct list_head *list)
656{
657 struct ibft_control *control = NULL;
658 void *ptr, *end;
659 int rc = 0;
660 u16 offset;
661 u16 eot_offset;
662
663 control = (void *)header + sizeof(*header);
664 end = (void *)control + control->hdr.length;
Mike Christiebb8fb4e2008-09-02 14:36:07 -0700665 eot_offset = (void *)header + header->length - (void *)control;
Konrad Rzeszutek138fe4e2008-04-09 19:50:41 -0700666 rc = ibft_verify_hdr("control", (struct ibft_hdr *)control, id_control,
667 sizeof(*control));
668
669 /* iBFT table safety checking */
670 rc |= ((control->hdr.index) ? -ENODEV : 0);
671 if (rc) {
672 printk(KERN_ERR "iBFT error: Control header is invalid!\n");
673 return rc;
674 }
675 for (ptr = &control->initiator_off; ptr < end; ptr += sizeof(u16)) {
676 offset = *(u16 *)ptr;
677 if (offset && offset < header->length && offset < eot_offset) {
678 rc = ibft_create_kobject(header,
679 (void *)header + offset,
680 list);
681 if (rc)
682 break;
683 }
684 }
685
686 return rc;
687}
688
689static void ibft_unregister(struct list_head *attr_list,
690 struct list_head *kobj_list)
691{
692 struct ibft_kobject *data = NULL, *n;
693 struct ibft_attribute *attr = NULL, *m;
694
695 list_for_each_entry_safe(attr, m, attr_list, node) {
696 sysfs_remove_file(attr->kobj, &attr->attr);
697 list_del(&attr->node);
698 kfree(attr);
699 };
700 list_del_init(attr_list);
701
702 list_for_each_entry_safe(data, n, kobj_list, node) {
703 list_del(&data->node);
704 if (data->hdr->id == id_nic)
705 sysfs_remove_link(&data->kobj, "device");
706 kobject_put(&data->kobj);
707 };
708 list_del_init(kobj_list);
709}
710
711static int __init ibft_create_attribute(struct ibft_kobject *kobj_data,
712 int type,
713 const char *name,
714 ssize_t (*show)(struct ibft_kobject *,
715 struct ibft_attribute*,
716 char *buf),
717 struct list_head *list)
718{
719 struct ibft_attribute *attr = NULL;
720 struct ibft_hdr *hdr = kobj_data->hdr;
721
722 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
723 if (!attr)
724 return -ENOMEM;
725
726 attr->attr.name = name;
727 attr->attr.mode = S_IRUSR;
Konrad Rzeszutek138fe4e2008-04-09 19:50:41 -0700728
729 attr->hdr = hdr;
730 attr->show = show;
731 attr->kobj = &kobj_data->kobj;
732 attr->type = type;
733
734 list_add_tail(&attr->node, list);
735
736 return 0;
737}
738
739/*
740 * Helper routiners to check to determine if the entry is valid
741 * in the proper iBFT structure.
742 */
743static int __init ibft_check_nic_for(struct ibft_nic *nic, int entry)
744{
745 int rc = 0;
746
747 switch (entry) {
748 case ibft_eth_index:
749 case ibft_eth_flags:
750 rc = 1;
751 break;
752 case ibft_eth_ip_addr:
Ashutosh Naik65fd2102009-04-30 15:08:58 -0700753 if (memcmp(nic->ip_addr, nulls, sizeof(nic->ip_addr)))
Konrad Rzeszutek138fe4e2008-04-09 19:50:41 -0700754 rc = 1;
755 break;
756 case ibft_eth_subnet_mask:
Ashutosh Naik65fd2102009-04-30 15:08:58 -0700757 if (nic->subnet_mask_prefix)
Konrad Rzeszutek138fe4e2008-04-09 19:50:41 -0700758 rc = 1;
759 break;
760 case ibft_eth_origin:
761 rc = 1;
762 break;
763 case ibft_eth_gateway:
764 if (memcmp(nic->gateway, nulls, sizeof(nic->gateway)))
765 rc = 1;
766 break;
767 case ibft_eth_primary_dns:
768 if (memcmp(nic->primary_dns, nulls,
769 sizeof(nic->primary_dns)))
770 rc = 1;
771 break;
772 case ibft_eth_secondary_dns:
773 if (memcmp(nic->secondary_dns, nulls,
774 sizeof(nic->secondary_dns)))
775 rc = 1;
776 break;
777 case ibft_eth_dhcp:
778 if (memcmp(nic->dhcp, nulls, sizeof(nic->dhcp)))
779 rc = 1;
780 break;
781 case ibft_eth_vlan:
782 case ibft_eth_mac:
783 rc = 1;
784 break;
785 case ibft_eth_hostname:
786 if (nic->hostname_off)
787 rc = 1;
788 break;
789 default:
790 break;
791 }
792
793 return rc;
794}
795
796static int __init ibft_check_tgt_for(struct ibft_tgt *tgt, int entry)
797{
798 int rc = 0;
799
800 switch (entry) {
801 case ibft_tgt_index:
802 case ibft_tgt_flags:
803 case ibft_tgt_ip_addr:
804 case ibft_tgt_port:
805 case ibft_tgt_lun:
806 case ibft_tgt_nic_assoc:
807 case ibft_tgt_chap_type:
808 rc = 1;
809 case ibft_tgt_name:
810 if (tgt->tgt_name_len)
811 rc = 1;
812 break;
813 case ibft_tgt_chap_name:
814 case ibft_tgt_chap_secret:
815 if (tgt->chap_name_len)
816 rc = 1;
817 break;
818 case ibft_tgt_rev_chap_name:
819 case ibft_tgt_rev_chap_secret:
820 if (tgt->rev_chap_name_len)
821 rc = 1;
822 break;
823 default:
824 break;
825 }
826
827 return rc;
828}
829
830static int __init ibft_check_initiator_for(struct ibft_initiator *init,
831 int entry)
832{
833 int rc = 0;
834
835 switch (entry) {
836 case ibft_init_index:
837 case ibft_init_flags:
838 rc = 1;
839 break;
840 case ibft_init_isns_server:
841 if (memcmp(init->isns_server, nulls,
842 sizeof(init->isns_server)))
843 rc = 1;
844 break;
845 case ibft_init_slp_server:
846 if (memcmp(init->slp_server, nulls,
847 sizeof(init->slp_server)))
848 rc = 1;
849 break;
850 case ibft_init_pri_radius_server:
851 if (memcmp(init->pri_radius_server, nulls,
852 sizeof(init->pri_radius_server)))
853 rc = 1;
854 break;
855 case ibft_init_sec_radius_server:
856 if (memcmp(init->sec_radius_server, nulls,
857 sizeof(init->sec_radius_server)))
858 rc = 1;
859 break;
860 case ibft_init_initiator_name:
861 if (init->initiator_name_len)
862 rc = 1;
863 break;
864 default:
865 break;
866 }
867
868 return rc;
869}
870
871/*
872 * Register the attributes for all of the kobjects.
873 */
874static int __init ibft_register_attributes(struct list_head *kobject_list,
875 struct list_head *attr_list)
876{
877 int rc = 0, i = 0;
878 struct ibft_kobject *data = NULL;
879 struct ibft_attribute *attr = NULL, *m;
880
881 list_for_each_entry(data, kobject_list, node) {
882 switch (data->hdr->id) {
883 case id_nic:
884 for (i = 0; i < ibft_eth_end_marker && !rc; i++)
885 if (ibft_check_nic_for(data->nic, i))
886 rc = ibft_create_attribute(data, i,
887 ibft_eth_properties[i],
888 ibft_attr_show_nic, attr_list);
889 break;
890 case id_target:
891 for (i = 0; i < ibft_tgt_end_marker && !rc; i++)
892 if (ibft_check_tgt_for(data->tgt, i))
893 rc = ibft_create_attribute(data, i,
894 ibft_tgt_properties[i],
895 ibft_attr_show_target,
896 attr_list);
897 break;
898 case id_initiator:
899 for (i = 0; i < ibft_init_end_marker && !rc; i++)
900 if (ibft_check_initiator_for(
901 data->initiator, i))
902 rc = ibft_create_attribute(data, i,
903 ibft_initiator_properties[i],
904 ibft_attr_show_initiator,
905 attr_list);
906 break;
907 default:
908 break;
909 }
910 if (rc)
911 break;
912 }
913 list_for_each_entry_safe(attr, m, attr_list, node) {
914 rc = sysfs_create_file(attr->kobj, &attr->attr);
915 if (rc) {
916 list_del(&attr->node);
917 kfree(attr);
918 break;
919 }
920 }
921
922 return rc;
923}
924
925/*
926 * ibft_init() - creates sysfs tree entries for the iBFT data.
927 */
928static int __init ibft_init(void)
929{
930 int rc = 0;
931
932 ibft_kset = kset_create_and_add("ibft", NULL, firmware_kobj);
933 if (!ibft_kset)
934 return -ENOMEM;
935
936 if (ibft_addr) {
Jaswinder Singh Rajputf537a532009-02-11 23:51:34 +0530937 printk(KERN_INFO "iBFT detected at 0x%llx.\n",
Jan Beuliched3c6612009-10-02 16:12:39 +0100938 (u64)isa_virt_to_bus(ibft_addr));
Konrad Rzeszutek138fe4e2008-04-09 19:50:41 -0700939
940 rc = ibft_check_device();
941 if (rc)
942 goto out_firmware_unregister;
943
944 /* Scan the IBFT for data and register the kobjects. */
945 rc = ibft_register_kobjects(ibft_addr, &ibft_kobject_list);
946 if (rc)
947 goto out_free;
948
949 /* Register the attributes */
950 rc = ibft_register_attributes(&ibft_kobject_list,
951 &ibft_attr_list);
952 if (rc)
953 goto out_free;
954 } else
955 printk(KERN_INFO "No iBFT detected.\n");
956
957 return 0;
958
959out_free:
960 ibft_unregister(&ibft_attr_list, &ibft_kobject_list);
961out_firmware_unregister:
962 kset_unregister(ibft_kset);
963 return rc;
964}
965
966static void __exit ibft_exit(void)
967{
968 ibft_unregister(&ibft_attr_list, &ibft_kobject_list);
969 kset_unregister(ibft_kset);
970}
971
972module_init(ibft_init);
973module_exit(ibft_exit);