1/*
2 *  Copyright 2007-2010 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 *  06 Jan 2010 - Peter Jones <pjones@redhat.com>
23 *    New changelog entries are in the git log from now on.  Not here.
24 *
25 *  14 Mar 2008 - Konrad Rzeszutek <ketuzsezr@darnok.org>
26 *    Updated comments and copyrights. (v0.4.9)
27 *
28 *  11 Feb 2008 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
29 *    Converted to using ibft_addr. (v0.4.8)
30 *
31 *   8 Feb 2008 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
32 *    Combined two functions in one: reserve_ibft_region. (v0.4.7)
33 *
34 *  30 Jan 2008 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
35 *   Added logic to handle IPv6 addresses. (v0.4.6)
36 *
37 *  25 Jan 2008 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
38 *   Added logic to handle badly not-to-spec iBFT. (v0.4.5)
39 *
40 *   4 Jan 2008 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
41 *   Added __init to function declarations. (v0.4.4)
42 *
43 *  21 Dec 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
44 *   Updated kobject registration, combined unregister functions in one
45 *   and code and style cleanup. (v0.4.3)
46 *
47 *   5 Dec 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
48 *   Added end-markers to enums and re-organized kobject registration. (v0.4.2)
49 *
50 *   4 Dec 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
51 *   Created 'device' sysfs link to the NIC and style cleanup. (v0.4.1)
52 *
53 *  28 Nov 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
54 *   Added sysfs-ibft documentation, moved 'find_ibft' function to
55 *   in its own file and added text attributes for every struct field.  (v0.4)
56 *
57 *  21 Nov 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
58 *   Added text attributes emulating OpenFirmware /proc/device-tree naming.
59 *   Removed binary /sysfs interface (v0.3)
60 *
61 *  29 Aug 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
62 *   Added functionality in setup.c to reserve iBFT region. (v0.2)
63 *
64 *  27 Aug 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
65 *   First version exposing iBFT data via a binary /sysfs. (v0.1)
66 *
67 */
68
69
70#include <linux/blkdev.h>
71#include <linux/capability.h>
72#include <linux/ctype.h>
73#include <linux/device.h>
74#include <linux/err.h>
75#include <linux/init.h>
76#include <linux/iscsi_ibft.h>
77#include <linux/limits.h>
78#include <linux/module.h>
79#include <linux/pci.h>
80#include <linux/slab.h>
81#include <linux/stat.h>
82#include <linux/string.h>
83#include <linux/types.h>
84#include <linux/acpi.h>
85#include <linux/iscsi_boot_sysfs.h>
86
87#define IBFT_ISCSI_VERSION "0.5.0"
88#define IBFT_ISCSI_DATE "2010-Feb-25"
89
90MODULE_AUTHOR("Peter Jones <pjones@redhat.com> and "
91	      "Konrad Rzeszutek <ketuzsezr@darnok.org>");
92MODULE_DESCRIPTION("sysfs interface to BIOS iBFT information");
93MODULE_LICENSE("GPL");
94MODULE_VERSION(IBFT_ISCSI_VERSION);
95
96struct ibft_hdr {
97	u8 id;
98	u8 version;
99	u16 length;
100	u8 index;
101	u8 flags;
102} __attribute__((__packed__));
103
104struct ibft_control {
105	struct ibft_hdr hdr;
106	u16 extensions;
107	u16 initiator_off;
108	u16 nic0_off;
109	u16 tgt0_off;
110	u16 nic1_off;
111	u16 tgt1_off;
112} __attribute__((__packed__));
113
114struct ibft_initiator {
115	struct ibft_hdr hdr;
116	char isns_server[16];
117	char slp_server[16];
118	char pri_radius_server[16];
119	char sec_radius_server[16];
120	u16 initiator_name_len;
121	u16 initiator_name_off;
122} __attribute__((__packed__));
123
124struct ibft_nic {
125	struct ibft_hdr hdr;
126	char ip_addr[16];
127	u8 subnet_mask_prefix;
128	u8 origin;
129	char gateway[16];
130	char primary_dns[16];
131	char secondary_dns[16];
132	char dhcp[16];
133	u16 vlan;
134	char mac[6];
135	u16 pci_bdf;
136	u16 hostname_len;
137	u16 hostname_off;
138} __attribute__((__packed__));
139
140struct ibft_tgt {
141	struct ibft_hdr hdr;
142	char ip_addr[16];
143	u16 port;
144	char lun[8];
145	u8 chap_type;
146	u8 nic_assoc;
147	u16 tgt_name_len;
148	u16 tgt_name_off;
149	u16 chap_name_len;
150	u16 chap_name_off;
151	u16 chap_secret_len;
152	u16 chap_secret_off;
153	u16 rev_chap_name_len;
154	u16 rev_chap_name_off;
155	u16 rev_chap_secret_len;
156	u16 rev_chap_secret_off;
157} __attribute__((__packed__));
158
159/*
160 * The kobject different types and its names.
161 *
162*/
163enum ibft_id {
164	id_reserved = 0, /* We don't support. */
165	id_control = 1, /* Should show up only once and is not exported. */
166	id_initiator = 2,
167	id_nic = 3,
168	id_target = 4,
169	id_extensions = 5, /* We don't support. */
170	id_end_marker,
171};
172
173/*
174 * The kobject and attribute structures.
175 */
176
177struct ibft_kobject {
178	struct acpi_table_ibft *header;
179	union {
180		struct ibft_initiator *initiator;
181		struct ibft_nic *nic;
182		struct ibft_tgt *tgt;
183		struct ibft_hdr *hdr;
184	};
185};
186
187static struct iscsi_boot_kset *boot_kset;
188
189/* fully null address */
190static const char nulls[16];
191
192/* IPv4-mapped IPv6 ::ffff:0.0.0.0 */
193static const char mapped_nulls[16] = { 0x00, 0x00, 0x00, 0x00,
194                                       0x00, 0x00, 0x00, 0x00,
195                                       0x00, 0x00, 0xff, 0xff,
196                                       0x00, 0x00, 0x00, 0x00 };
197
198static int address_not_null(u8 *ip)
199{
200	return (memcmp(ip, nulls, 16) && memcmp(ip, mapped_nulls, 16));
201}
202
203/*
204 * Helper functions to parse data properly.
205 */
206static ssize_t sprintf_ipaddr(char *buf, u8 *ip)
207{
208	char *str = buf;
209
210	if (ip[0] == 0 && ip[1] == 0 && ip[2] == 0 && ip[3] == 0 &&
211	    ip[4] == 0 && ip[5] == 0 && ip[6] == 0 && ip[7] == 0 &&
212	    ip[8] == 0 && ip[9] == 0 && ip[10] == 0xff && ip[11] == 0xff) {
213		/*
214		 * IPV4
215		 */
216		str += sprintf(buf, "%pI4", ip + 12);
217	} else {
218		/*
219		 * IPv6
220		 */
221		str += sprintf(str, "%pI6", ip);
222	}
223	str += sprintf(str, "\n");
224	return str - buf;
225}
226
227static ssize_t sprintf_string(char *str, int len, char *buf)
228{
229	return sprintf(str, "%.*s\n", len, buf);
230}
231
232/*
233 * Helper function to verify the IBFT header.
234 */
235static int ibft_verify_hdr(char *t, struct ibft_hdr *hdr, int id, int length)
236{
237	if (hdr->id != id) {
238		printk(KERN_ERR "iBFT error: We expected the %s " \
239				"field header.id to have %d but " \
240				"found %d instead!\n", t, id, hdr->id);
241		return -ENODEV;
242	}
243	if (hdr->length != length) {
244		printk(KERN_ERR "iBFT error: We expected the %s " \
245				"field header.length to have %d but " \
246				"found %d instead!\n", t, length, hdr->length);
247		return -ENODEV;
248	}
249
250	return 0;
251}
252
253/*
254 *  Routines for parsing the iBFT data to be human readable.
255 */
256static ssize_t ibft_attr_show_initiator(void *data, int type, char *buf)
257{
258	struct ibft_kobject *entry = data;
259	struct ibft_initiator *initiator = entry->initiator;
260	void *ibft_loc = entry->header;
261	char *str = buf;
262
263	if (!initiator)
264		return 0;
265
266	switch (type) {
267	case ISCSI_BOOT_INI_INDEX:
268		str += sprintf(str, "%d\n", initiator->hdr.index);
269		break;
270	case ISCSI_BOOT_INI_FLAGS:
271		str += sprintf(str, "%d\n", initiator->hdr.flags);
272		break;
273	case ISCSI_BOOT_INI_ISNS_SERVER:
274		str += sprintf_ipaddr(str, initiator->isns_server);
275		break;
276	case ISCSI_BOOT_INI_SLP_SERVER:
277		str += sprintf_ipaddr(str, initiator->slp_server);
278		break;
279	case ISCSI_BOOT_INI_PRI_RADIUS_SERVER:
280		str += sprintf_ipaddr(str, initiator->pri_radius_server);
281		break;
282	case ISCSI_BOOT_INI_SEC_RADIUS_SERVER:
283		str += sprintf_ipaddr(str, initiator->sec_radius_server);
284		break;
285	case ISCSI_BOOT_INI_INITIATOR_NAME:
286		str += sprintf_string(str, initiator->initiator_name_len,
287				      (char *)ibft_loc +
288				      initiator->initiator_name_off);
289		break;
290	default:
291		break;
292	}
293
294	return str - buf;
295}
296
297static ssize_t ibft_attr_show_nic(void *data, int type, char *buf)
298{
299	struct ibft_kobject *entry = data;
300	struct ibft_nic *nic = entry->nic;
301	void *ibft_loc = entry->header;
302	char *str = buf;
303	__be32 val;
304
305	if (!nic)
306		return 0;
307
308	switch (type) {
309	case ISCSI_BOOT_ETH_INDEX:
310		str += sprintf(str, "%d\n", nic->hdr.index);
311		break;
312	case ISCSI_BOOT_ETH_FLAGS:
313		str += sprintf(str, "%d\n", nic->hdr.flags);
314		break;
315	case ISCSI_BOOT_ETH_IP_ADDR:
316		str += sprintf_ipaddr(str, nic->ip_addr);
317		break;
318	case ISCSI_BOOT_ETH_SUBNET_MASK:
319		val = cpu_to_be32(~((1 << (32-nic->subnet_mask_prefix))-1));
320		str += sprintf(str, "%pI4", &val);
321		break;
322	case ISCSI_BOOT_ETH_ORIGIN:
323		str += sprintf(str, "%d\n", nic->origin);
324		break;
325	case ISCSI_BOOT_ETH_GATEWAY:
326		str += sprintf_ipaddr(str, nic->gateway);
327		break;
328	case ISCSI_BOOT_ETH_PRIMARY_DNS:
329		str += sprintf_ipaddr(str, nic->primary_dns);
330		break;
331	case ISCSI_BOOT_ETH_SECONDARY_DNS:
332		str += sprintf_ipaddr(str, nic->secondary_dns);
333		break;
334	case ISCSI_BOOT_ETH_DHCP:
335		str += sprintf_ipaddr(str, nic->dhcp);
336		break;
337	case ISCSI_BOOT_ETH_VLAN:
338		str += sprintf(str, "%d\n", nic->vlan);
339		break;
340	case ISCSI_BOOT_ETH_MAC:
341		str += sprintf(str, "%pM\n", nic->mac);
342		break;
343	case ISCSI_BOOT_ETH_HOSTNAME:
344		str += sprintf_string(str, nic->hostname_len,
345				      (char *)ibft_loc + nic->hostname_off);
346		break;
347	default:
348		break;
349	}
350
351	return str - buf;
352};
353
354static ssize_t ibft_attr_show_target(void *data, int type, char *buf)
355{
356	struct ibft_kobject *entry = data;
357	struct ibft_tgt *tgt = entry->tgt;
358	void *ibft_loc = entry->header;
359	char *str = buf;
360	int i;
361
362	if (!tgt)
363		return 0;
364
365	switch (type) {
366	case ISCSI_BOOT_TGT_INDEX:
367		str += sprintf(str, "%d\n", tgt->hdr.index);
368		break;
369	case ISCSI_BOOT_TGT_FLAGS:
370		str += sprintf(str, "%d\n", tgt->hdr.flags);
371		break;
372	case ISCSI_BOOT_TGT_IP_ADDR:
373		str += sprintf_ipaddr(str, tgt->ip_addr);
374		break;
375	case ISCSI_BOOT_TGT_PORT:
376		str += sprintf(str, "%d\n", tgt->port);
377		break;
378	case ISCSI_BOOT_TGT_LUN:
379		for (i = 0; i < 8; i++)
380			str += sprintf(str, "%x", (u8)tgt->lun[i]);
381		str += sprintf(str, "\n");
382		break;
383	case ISCSI_BOOT_TGT_NIC_ASSOC:
384		str += sprintf(str, "%d\n", tgt->nic_assoc);
385		break;
386	case ISCSI_BOOT_TGT_CHAP_TYPE:
387		str += sprintf(str, "%d\n", tgt->chap_type);
388		break;
389	case ISCSI_BOOT_TGT_NAME:
390		str += sprintf_string(str, tgt->tgt_name_len,
391				      (char *)ibft_loc + tgt->tgt_name_off);
392		break;
393	case ISCSI_BOOT_TGT_CHAP_NAME:
394		str += sprintf_string(str, tgt->chap_name_len,
395				      (char *)ibft_loc + tgt->chap_name_off);
396		break;
397	case ISCSI_BOOT_TGT_CHAP_SECRET:
398		str += sprintf_string(str, tgt->chap_secret_len,
399				      (char *)ibft_loc + tgt->chap_secret_off);
400		break;
401	case ISCSI_BOOT_TGT_REV_CHAP_NAME:
402		str += sprintf_string(str, tgt->rev_chap_name_len,
403				      (char *)ibft_loc +
404				      tgt->rev_chap_name_off);
405		break;
406	case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
407		str += sprintf_string(str, tgt->rev_chap_secret_len,
408				      (char *)ibft_loc +
409				      tgt->rev_chap_secret_off);
410		break;
411	default:
412		break;
413	}
414
415	return str - buf;
416}
417
418static int __init ibft_check_device(void)
419{
420	int len;
421	u8 *pos;
422	u8 csum = 0;
423
424	len = ibft_addr->header.length;
425
426	/* Sanity checking of iBFT. */
427	if (ibft_addr->header.revision != 1) {
428		printk(KERN_ERR "iBFT module supports only revision 1, " \
429				"while this is %d.\n",
430				ibft_addr->header.revision);
431		return -ENOENT;
432	}
433	for (pos = (u8 *)ibft_addr; pos < (u8 *)ibft_addr + len; pos++)
434		csum += *pos;
435
436	if (csum) {
437		printk(KERN_ERR "iBFT has incorrect checksum (0x%x)!\n", csum);
438		return -ENOENT;
439	}
440
441	return 0;
442}
443
444/*
445 * Helper routiners to check to determine if the entry is valid
446 * in the proper iBFT structure.
447 */
448static umode_t ibft_check_nic_for(void *data, int type)
449{
450	struct ibft_kobject *entry = data;
451	struct ibft_nic *nic = entry->nic;
452	umode_t rc = 0;
453
454	switch (type) {
455	case ISCSI_BOOT_ETH_INDEX:
456	case ISCSI_BOOT_ETH_FLAGS:
457		rc = S_IRUGO;
458		break;
459	case ISCSI_BOOT_ETH_IP_ADDR:
460		if (address_not_null(nic->ip_addr))
461			rc = S_IRUGO;
462		break;
463	case ISCSI_BOOT_ETH_SUBNET_MASK:
464		if (nic->subnet_mask_prefix)
465			rc = S_IRUGO;
466		break;
467	case ISCSI_BOOT_ETH_ORIGIN:
468		rc = S_IRUGO;
469		break;
470	case ISCSI_BOOT_ETH_GATEWAY:
471		if (address_not_null(nic->gateway))
472			rc = S_IRUGO;
473		break;
474	case ISCSI_BOOT_ETH_PRIMARY_DNS:
475		if (address_not_null(nic->primary_dns))
476			rc = S_IRUGO;
477		break;
478	case ISCSI_BOOT_ETH_SECONDARY_DNS:
479		if (address_not_null(nic->secondary_dns))
480			rc = S_IRUGO;
481		break;
482	case ISCSI_BOOT_ETH_DHCP:
483		if (address_not_null(nic->dhcp))
484			rc = S_IRUGO;
485		break;
486	case ISCSI_BOOT_ETH_VLAN:
487	case ISCSI_BOOT_ETH_MAC:
488		rc = S_IRUGO;
489		break;
490	case ISCSI_BOOT_ETH_HOSTNAME:
491		if (nic->hostname_off)
492			rc = S_IRUGO;
493		break;
494	default:
495		break;
496	}
497
498	return rc;
499}
500
501static umode_t __init ibft_check_tgt_for(void *data, int type)
502{
503	struct ibft_kobject *entry = data;
504	struct ibft_tgt *tgt = entry->tgt;
505	umode_t rc = 0;
506
507	switch (type) {
508	case ISCSI_BOOT_TGT_INDEX:
509	case ISCSI_BOOT_TGT_FLAGS:
510	case ISCSI_BOOT_TGT_IP_ADDR:
511	case ISCSI_BOOT_TGT_PORT:
512	case ISCSI_BOOT_TGT_LUN:
513	case ISCSI_BOOT_TGT_NIC_ASSOC:
514	case ISCSI_BOOT_TGT_CHAP_TYPE:
515		rc = S_IRUGO;
516	case ISCSI_BOOT_TGT_NAME:
517		if (tgt->tgt_name_len)
518			rc = S_IRUGO;
519		break;
520	case ISCSI_BOOT_TGT_CHAP_NAME:
521	case ISCSI_BOOT_TGT_CHAP_SECRET:
522		if (tgt->chap_name_len)
523			rc = S_IRUGO;
524		break;
525	case ISCSI_BOOT_TGT_REV_CHAP_NAME:
526	case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
527		if (tgt->rev_chap_name_len)
528			rc = S_IRUGO;
529		break;
530	default:
531		break;
532	}
533
534	return rc;
535}
536
537static umode_t __init ibft_check_initiator_for(void *data, int type)
538{
539	struct ibft_kobject *entry = data;
540	struct ibft_initiator *init = entry->initiator;
541	umode_t rc = 0;
542
543	switch (type) {
544	case ISCSI_BOOT_INI_INDEX:
545	case ISCSI_BOOT_INI_FLAGS:
546		rc = S_IRUGO;
547		break;
548	case ISCSI_BOOT_INI_ISNS_SERVER:
549		if (address_not_null(init->isns_server))
550			rc = S_IRUGO;
551		break;
552	case ISCSI_BOOT_INI_SLP_SERVER:
553		if (address_not_null(init->slp_server))
554			rc = S_IRUGO;
555		break;
556	case ISCSI_BOOT_INI_PRI_RADIUS_SERVER:
557		if (address_not_null(init->pri_radius_server))
558			rc = S_IRUGO;
559		break;
560	case ISCSI_BOOT_INI_SEC_RADIUS_SERVER:
561		if (address_not_null(init->sec_radius_server))
562			rc = S_IRUGO;
563		break;
564	case ISCSI_BOOT_INI_INITIATOR_NAME:
565		if (init->initiator_name_len)
566			rc = S_IRUGO;
567		break;
568	default:
569		break;
570	}
571
572	return rc;
573}
574
575static void ibft_kobj_release(void *data)
576{
577	kfree(data);
578}
579
580/*
581 * Helper function for ibft_register_kobjects.
582 */
583static int __init ibft_create_kobject(struct acpi_table_ibft *header,
584				      struct ibft_hdr *hdr)
585{
586	struct iscsi_boot_kobj *boot_kobj = NULL;
587	struct ibft_kobject *ibft_kobj = NULL;
588	struct ibft_nic *nic = (struct ibft_nic *)hdr;
589	struct pci_dev *pci_dev;
590	int rc = 0;
591
592	ibft_kobj = kzalloc(sizeof(*ibft_kobj), GFP_KERNEL);
593	if (!ibft_kobj)
594		return -ENOMEM;
595
596	ibft_kobj->header = header;
597	ibft_kobj->hdr = hdr;
598
599	switch (hdr->id) {
600	case id_initiator:
601		rc = ibft_verify_hdr("initiator", hdr, id_initiator,
602				     sizeof(*ibft_kobj->initiator));
603		if (rc)
604			break;
605
606		boot_kobj = iscsi_boot_create_initiator(boot_kset, hdr->index,
607						ibft_kobj,
608						ibft_attr_show_initiator,
609						ibft_check_initiator_for,
610						ibft_kobj_release);
611		if (!boot_kobj) {
612			rc = -ENOMEM;
613			goto free_ibft_obj;
614		}
615		break;
616	case id_nic:
617		rc = ibft_verify_hdr("ethernet", hdr, id_nic,
618				     sizeof(*ibft_kobj->nic));
619		if (rc)
620			break;
621
622		boot_kobj = iscsi_boot_create_ethernet(boot_kset, hdr->index,
623						       ibft_kobj,
624						       ibft_attr_show_nic,
625						       ibft_check_nic_for,
626						       ibft_kobj_release);
627		if (!boot_kobj) {
628			rc = -ENOMEM;
629			goto free_ibft_obj;
630		}
631		break;
632	case id_target:
633		rc = ibft_verify_hdr("target", hdr, id_target,
634				     sizeof(*ibft_kobj->tgt));
635		if (rc)
636			break;
637
638		boot_kobj = iscsi_boot_create_target(boot_kset, hdr->index,
639						     ibft_kobj,
640						     ibft_attr_show_target,
641						     ibft_check_tgt_for,
642						     ibft_kobj_release);
643		if (!boot_kobj) {
644			rc = -ENOMEM;
645			goto free_ibft_obj;
646		}
647		break;
648	case id_reserved:
649	case id_control:
650	case id_extensions:
651		/* Fields which we don't support. Ignore them */
652		rc = 1;
653		break;
654	default:
655		printk(KERN_ERR "iBFT has unknown structure type (%d). " \
656				"Report this bug to %.6s!\n", hdr->id,
657				header->header.oem_id);
658		rc = 1;
659		break;
660	}
661
662	if (rc) {
663		/* Skip adding this kobject, but exit with non-fatal error. */
664		rc = 0;
665		goto free_ibft_obj;
666	}
667
668	if (hdr->id == id_nic) {
669		/*
670		* We don't search for the device in other domains than
671		* zero. This is because on x86 platforms the BIOS
672		* executes only devices which are in domain 0. Furthermore, the
673		* iBFT spec doesn't have a domain id field :-(
674		*/
675		pci_dev = pci_get_bus_and_slot((nic->pci_bdf & 0xff00) >> 8,
676					       (nic->pci_bdf & 0xff));
677		if (pci_dev) {
678			rc = sysfs_create_link(&boot_kobj->kobj,
679					       &pci_dev->dev.kobj, "device");
680			pci_dev_put(pci_dev);
681		}
682	}
683	return 0;
684
685free_ibft_obj:
686	kfree(ibft_kobj);
687	return rc;
688}
689
690/*
691 * Scan the IBFT table structure for the NIC and Target fields. When
692 * found add them on the passed-in list. We do not support the other
693 * fields at this point, so they are skipped.
694 */
695static int __init ibft_register_kobjects(struct acpi_table_ibft *header)
696{
697	struct ibft_control *control = NULL;
698	void *ptr, *end;
699	int rc = 0;
700	u16 offset;
701	u16 eot_offset;
702
703	control = (void *)header + sizeof(*header);
704	end = (void *)control + control->hdr.length;
705	eot_offset = (void *)header + header->header.length - (void *)control;
706	rc = ibft_verify_hdr("control", (struct ibft_hdr *)control, id_control,
707			     sizeof(*control));
708
709	/* iBFT table safety checking */
710	rc |= ((control->hdr.index) ? -ENODEV : 0);
711	if (rc) {
712		printk(KERN_ERR "iBFT error: Control header is invalid!\n");
713		return rc;
714	}
715	for (ptr = &control->initiator_off; ptr < end; ptr += sizeof(u16)) {
716		offset = *(u16 *)ptr;
717		if (offset && offset < header->header.length &&
718						offset < eot_offset) {
719			rc = ibft_create_kobject(header,
720						 (void *)header + offset);
721			if (rc)
722				break;
723		}
724	}
725
726	return rc;
727}
728
729static void ibft_unregister(void)
730{
731	struct iscsi_boot_kobj *boot_kobj, *tmp_kobj;
732	struct ibft_kobject *ibft_kobj;
733
734	list_for_each_entry_safe(boot_kobj, tmp_kobj,
735				 &boot_kset->kobj_list, list) {
736		ibft_kobj = boot_kobj->data;
737		if (ibft_kobj->hdr->id == id_nic)
738			sysfs_remove_link(&boot_kobj->kobj, "device");
739	};
740}
741
742static void ibft_cleanup(void)
743{
744	if (boot_kset) {
745		ibft_unregister();
746		iscsi_boot_destroy_kset(boot_kset);
747	}
748}
749
750static void __exit ibft_exit(void)
751{
752	ibft_cleanup();
753}
754
755#ifdef CONFIG_ACPI
756static const struct {
757	char *sign;
758} ibft_signs[] = {
759	/*
760	 * One spec says "IBFT", the other says "iBFT". We have to check
761	 * for both.
762	 */
763	{ ACPI_SIG_IBFT },
764	{ "iBFT" },
765	{ "BIFT" },	/* Broadcom iSCSI Offload */
766};
767
768static void __init acpi_find_ibft_region(void)
769{
770	int i;
771	struct acpi_table_header *table = NULL;
772
773	if (acpi_disabled)
774		return;
775
776	for (i = 0; i < ARRAY_SIZE(ibft_signs) && !ibft_addr; i++) {
777		acpi_get_table(ibft_signs[i].sign, 0, &table);
778		ibft_addr = (struct acpi_table_ibft *)table;
779	}
780}
781#else
782static void __init acpi_find_ibft_region(void)
783{
784}
785#endif
786
787/*
788 * ibft_init() - creates sysfs tree entries for the iBFT data.
789 */
790static int __init ibft_init(void)
791{
792	int rc = 0;
793
794	/*
795	   As on UEFI systems the setup_arch()/find_ibft_region()
796	   is called before ACPI tables are parsed and it only does
797	   legacy finding.
798	*/
799	if (!ibft_addr)
800		acpi_find_ibft_region();
801
802	if (ibft_addr) {
803		pr_info("iBFT detected.\n");
804
805		rc = ibft_check_device();
806		if (rc)
807			return rc;
808
809		boot_kset = iscsi_boot_create_kset("ibft");
810		if (!boot_kset)
811			return -ENOMEM;
812
813		/* Scan the IBFT for data and register the kobjects. */
814		rc = ibft_register_kobjects(ibft_addr);
815		if (rc)
816			goto out_free;
817	} else
818		printk(KERN_INFO "No iBFT detected.\n");
819
820	return 0;
821
822out_free:
823	ibft_cleanup();
824	return rc;
825}
826
827module_init(ibft_init);
828module_exit(ibft_exit);
829