iscsi_ibft.c 18.6 KB
Newer Older
1
/*
2
 *  Copyright 2007-2010 Red Hat, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *  by Peter Jones <pjones@redhat.com>
 *  Copyright 2008 IBM, Inc.
 *  by Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
 *  Copyright 2008
 *  by Konrad Rzeszutek <ketuzsezr@darnok.org>
 *
 * This code exposes the iSCSI Boot Format Table to userland via sysfs.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License v2.0 as published by
 * the Free Software Foundation
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Changelog:
 *
22 23 24
 *  06 Jan 2010 - Peter Jones <pjones@redhat.com>
 *    New changelog entries are in the git log from now on.  Not here.
 *
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
 *  14 Mar 2008 - Konrad Rzeszutek <ketuzsezr@darnok.org>
 *    Updated comments and copyrights. (v0.4.9)
 *
 *  11 Feb 2008 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
 *    Converted to using ibft_addr. (v0.4.8)
 *
 *   8 Feb 2008 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
 *    Combined two functions in one: reserve_ibft_region. (v0.4.7)
 *
 *  30 Jan 2008 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
 *   Added logic to handle IPv6 addresses. (v0.4.6)
 *
 *  25 Jan 2008 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
 *   Added logic to handle badly not-to-spec iBFT. (v0.4.5)
 *
 *   4 Jan 2008 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
 *   Added __init to function declarations. (v0.4.4)
 *
 *  21 Dec 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
 *   Updated kobject registration, combined unregister functions in one
 *   and code and style cleanup. (v0.4.3)
 *
 *   5 Dec 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
 *   Added end-markers to enums and re-organized kobject registration. (v0.4.2)
 *
 *   4 Dec 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
 *   Created 'device' sysfs link to the NIC and style cleanup. (v0.4.1)
 *
 *  28 Nov 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
 *   Added sysfs-ibft documentation, moved 'find_ibft' function to
 *   in its own file and added text attributes for every struct field.  (v0.4)
 *
 *  21 Nov 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
 *   Added text attributes emulating OpenFirmware /proc/device-tree naming.
 *   Removed binary /sysfs interface (v0.3)
 *
 *  29 Aug 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
 *   Added functionality in setup.c to reserve iBFT region. (v0.2)
 *
 *  27 Aug 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
 *   First version exposing iBFT data via a binary /sysfs. (v0.1)
 *
 */


#include <linux/blkdev.h>
#include <linux/capability.h>
#include <linux/ctype.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/init.h>
#include <linux/iscsi_ibft.h>
#include <linux/limits.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/slab.h>
#include <linux/stat.h>
#include <linux/string.h>
#include <linux/types.h>
84
#include <linux/acpi.h>
85
#include <linux/iscsi_boot_sysfs.h>
86

87 88
#define IBFT_ISCSI_VERSION "0.5.0"
#define IBFT_ISCSI_DATE "2010-Feb-25"
89

90 91
MODULE_AUTHOR("Peter Jones <pjones@redhat.com> and "
	      "Konrad Rzeszutek <ketuzsezr@darnok.org>");
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
MODULE_DESCRIPTION("sysfs interface to BIOS iBFT information");
MODULE_LICENSE("GPL");
MODULE_VERSION(IBFT_ISCSI_VERSION);

struct ibft_hdr {
	u8 id;
	u8 version;
	u16 length;
	u8 index;
	u8 flags;
} __attribute__((__packed__));

struct ibft_control {
	struct ibft_hdr hdr;
	u16 extensions;
	u16 initiator_off;
	u16 nic0_off;
	u16 tgt0_off;
	u16 nic1_off;
	u16 tgt1_off;
} __attribute__((__packed__));

struct ibft_initiator {
	struct ibft_hdr hdr;
	char isns_server[16];
	char slp_server[16];
	char pri_radius_server[16];
	char sec_radius_server[16];
	u16 initiator_name_len;
	u16 initiator_name_off;
} __attribute__((__packed__));

struct ibft_nic {
	struct ibft_hdr hdr;
	char ip_addr[16];
	u8 subnet_mask_prefix;
	u8 origin;
	char gateway[16];
	char primary_dns[16];
	char secondary_dns[16];
	char dhcp[16];
	u16 vlan;
	char mac[6];
	u16 pci_bdf;
	u16 hostname_len;
	u16 hostname_off;
} __attribute__((__packed__));

struct ibft_tgt {
	struct ibft_hdr hdr;
	char ip_addr[16];
	u16 port;
	char lun[8];
	u8 chap_type;
	u8 nic_assoc;
	u16 tgt_name_len;
	u16 tgt_name_off;
	u16 chap_name_len;
	u16 chap_name_off;
	u16 chap_secret_len;
	u16 chap_secret_off;
	u16 rev_chap_name_len;
	u16 rev_chap_name_off;
	u16 rev_chap_secret_len;
	u16 rev_chap_secret_off;
} __attribute__((__packed__));

/*
 * The kobject different types and its names.
 *
*/
enum ibft_id {
	id_reserved = 0, /* We don't support. */
	id_control = 1, /* Should show up only once and is not exported. */
	id_initiator = 2,
	id_nic = 3,
	id_target = 4,
	id_extensions = 5, /* We don't support. */
	id_end_marker,
};

/*
 * The kobject and attribute structures.
 */

struct ibft_kobject {
178
	struct acpi_table_ibft *header;
179 180 181 182 183 184 185 186
	union {
		struct ibft_initiator *initiator;
		struct ibft_nic *nic;
		struct ibft_tgt *tgt;
		struct ibft_hdr *hdr;
	};
};

187
static struct iscsi_boot_kset *boot_kset;
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

static const char nulls[16];

/*
 * Helper functions to parse data properly.
 */
static ssize_t sprintf_ipaddr(char *buf, u8 *ip)
{
	char *str = buf;

	if (ip[0] == 0 && ip[1] == 0 && ip[2] == 0 && ip[3] == 0 &&
	    ip[4] == 0 && ip[5] == 0 && ip[6] == 0 && ip[7] == 0 &&
	    ip[8] == 0 && ip[9] == 0 && ip[10] == 0xff && ip[11] == 0xff) {
		/*
		 * IPV4
		 */
H
Harvey Harrison 已提交
204
		str += sprintf(buf, "%pI4", ip + 12);
205 206 207 208
	} else {
		/*
		 * IPv6
		 */
H
Harvey Harrison 已提交
209
		str += sprintf(str, "%pI6", ip);
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
	}
	str += sprintf(str, "\n");
	return str - buf;
}

static ssize_t sprintf_string(char *str, int len, char *buf)
{
	return sprintf(str, "%.*s\n", len, buf);
}

/*
 * Helper function to verify the IBFT header.
 */
static int ibft_verify_hdr(char *t, struct ibft_hdr *hdr, int id, int length)
{
	if (hdr->id != id) {
226
		printk(KERN_ERR "iBFT error: We expected the %s " \
227
				"field header.id to have %d but " \
228
				"found %d instead!\n", t, id, hdr->id);
229 230 231
		return -ENODEV;
	}
	if (hdr->length != length) {
232
		printk(KERN_ERR "iBFT error: We expected the %s " \
233
				"field header.length to have %d but " \
234
				"found %d instead!\n", t, length, hdr->length);
235 236 237 238 239 240 241 242 243
		return -ENODEV;
	}

	return 0;
}

/*
 *  Routines for parsing the iBFT data to be human readable.
 */
244
static ssize_t ibft_attr_show_initiator(void *data, int type, char *buf)
245
{
246
	struct ibft_kobject *entry = data;
247 248 249 250 251 252 253
	struct ibft_initiator *initiator = entry->initiator;
	void *ibft_loc = entry->header;
	char *str = buf;

	if (!initiator)
		return 0;

254 255
	switch (type) {
	case ISCSI_BOOT_INI_INDEX:
256 257
		str += sprintf(str, "%d\n", initiator->hdr.index);
		break;
258
	case ISCSI_BOOT_INI_FLAGS:
259 260
		str += sprintf(str, "%d\n", initiator->hdr.flags);
		break;
261
	case ISCSI_BOOT_INI_ISNS_SERVER:
262 263
		str += sprintf_ipaddr(str, initiator->isns_server);
		break;
264
	case ISCSI_BOOT_INI_SLP_SERVER:
265 266
		str += sprintf_ipaddr(str, initiator->slp_server);
		break;
267
	case ISCSI_BOOT_INI_PRI_RADIUS_SERVER:
268 269
		str += sprintf_ipaddr(str, initiator->pri_radius_server);
		break;
270
	case ISCSI_BOOT_INI_SEC_RADIUS_SERVER:
271 272
		str += sprintf_ipaddr(str, initiator->sec_radius_server);
		break;
273
	case ISCSI_BOOT_INI_INITIATOR_NAME:
274 275 276 277 278 279 280 281 282 283 284
		str += sprintf_string(str, initiator->initiator_name_len,
				      (char *)ibft_loc +
				      initiator->initiator_name_off);
		break;
	default:
		break;
	}

	return str - buf;
}

285
static ssize_t ibft_attr_show_nic(void *data, int type, char *buf)
286
{
287
	struct ibft_kobject *entry = data;
288 289 290
	struct ibft_nic *nic = entry->nic;
	void *ibft_loc = entry->header;
	char *str = buf;
291
	__be32 val;
292 293 294 295

	if (!nic)
		return 0;

296 297
	switch (type) {
	case ISCSI_BOOT_ETH_INDEX:
298 299
		str += sprintf(str, "%d\n", nic->hdr.index);
		break;
300
	case ISCSI_BOOT_ETH_FLAGS:
301 302
		str += sprintf(str, "%d\n", nic->hdr.flags);
		break;
303
	case ISCSI_BOOT_ETH_IP_ADDR:
304 305
		str += sprintf_ipaddr(str, nic->ip_addr);
		break;
306
	case ISCSI_BOOT_ETH_SUBNET_MASK:
307 308
		val = cpu_to_be32(~((1 << (32-nic->subnet_mask_prefix))-1));
		str += sprintf(str, "%pI4", &val);
309
		break;
310
	case ISCSI_BOOT_ETH_ORIGIN:
311 312
		str += sprintf(str, "%d\n", nic->origin);
		break;
313
	case ISCSI_BOOT_ETH_GATEWAY:
314 315
		str += sprintf_ipaddr(str, nic->gateway);
		break;
316
	case ISCSI_BOOT_ETH_PRIMARY_DNS:
317 318
		str += sprintf_ipaddr(str, nic->primary_dns);
		break;
319
	case ISCSI_BOOT_ETH_SECONDARY_DNS:
320 321
		str += sprintf_ipaddr(str, nic->secondary_dns);
		break;
322
	case ISCSI_BOOT_ETH_DHCP:
323 324
		str += sprintf_ipaddr(str, nic->dhcp);
		break;
325
	case ISCSI_BOOT_ETH_VLAN:
326 327
		str += sprintf(str, "%d\n", nic->vlan);
		break;
328
	case ISCSI_BOOT_ETH_MAC:
329
		str += sprintf(str, "%pM\n", nic->mac);
330
		break;
331
	case ISCSI_BOOT_ETH_HOSTNAME:
332 333 334 335 336 337 338 339 340 341
		str += sprintf_string(str, nic->hostname_len,
				      (char *)ibft_loc + nic->hostname_off);
		break;
	default:
		break;
	}

	return str - buf;
};

342
static ssize_t ibft_attr_show_target(void *data, int type, char *buf)
343
{
344
	struct ibft_kobject *entry = data;
345 346 347 348 349 350 351 352
	struct ibft_tgt *tgt = entry->tgt;
	void *ibft_loc = entry->header;
	char *str = buf;
	int i;

	if (!tgt)
		return 0;

353 354
	switch (type) {
	case ISCSI_BOOT_TGT_INDEX:
355 356
		str += sprintf(str, "%d\n", tgt->hdr.index);
		break;
357
	case ISCSI_BOOT_TGT_FLAGS:
358 359
		str += sprintf(str, "%d\n", tgt->hdr.flags);
		break;
360
	case ISCSI_BOOT_TGT_IP_ADDR:
361 362
		str += sprintf_ipaddr(str, tgt->ip_addr);
		break;
363
	case ISCSI_BOOT_TGT_PORT:
364 365
		str += sprintf(str, "%d\n", tgt->port);
		break;
366
	case ISCSI_BOOT_TGT_LUN:
367 368 369 370
		for (i = 0; i < 8; i++)
			str += sprintf(str, "%x", (u8)tgt->lun[i]);
		str += sprintf(str, "\n");
		break;
371
	case ISCSI_BOOT_TGT_NIC_ASSOC:
372 373
		str += sprintf(str, "%d\n", tgt->nic_assoc);
		break;
374
	case ISCSI_BOOT_TGT_CHAP_TYPE:
375 376
		str += sprintf(str, "%d\n", tgt->chap_type);
		break;
377
	case ISCSI_BOOT_TGT_NAME:
378 379 380
		str += sprintf_string(str, tgt->tgt_name_len,
				      (char *)ibft_loc + tgt->tgt_name_off);
		break;
381
	case ISCSI_BOOT_TGT_CHAP_NAME:
382 383 384
		str += sprintf_string(str, tgt->chap_name_len,
				      (char *)ibft_loc + tgt->chap_name_off);
		break;
385
	case ISCSI_BOOT_TGT_CHAP_SECRET:
386 387 388
		str += sprintf_string(str, tgt->chap_secret_len,
				      (char *)ibft_loc + tgt->chap_secret_off);
		break;
389
	case ISCSI_BOOT_TGT_REV_CHAP_NAME:
390 391 392 393
		str += sprintf_string(str, tgt->rev_chap_name_len,
				      (char *)ibft_loc +
				      tgt->rev_chap_name_off);
		break;
394
	case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
		str += sprintf_string(str, tgt->rev_chap_secret_len,
				      (char *)ibft_loc +
				      tgt->rev_chap_secret_off);
		break;
	default:
		break;
	}

	return str - buf;
}

static int __init ibft_check_device(void)
{
	int len;
	u8 *pos;
	u8 csum = 0;

412
	len = ibft_addr->header.length;
413 414

	/* Sanity checking of iBFT. */
415
	if (ibft_addr->header.revision != 1) {
416
		printk(KERN_ERR "iBFT module supports only revision 1, " \
417 418
				"while this is %d.\n",
				ibft_addr->header.revision);
419 420 421 422 423 424 425 426 427 428 429 430 431
		return -ENOENT;
	}
	for (pos = (u8 *)ibft_addr; pos < (u8 *)ibft_addr + len; pos++)
		csum += *pos;

	if (csum) {
		printk(KERN_ERR "iBFT has incorrect checksum (0x%x)!\n", csum);
		return -ENOENT;
	}

	return 0;
}

432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
/*
 * Helper routiners to check to determine if the entry is valid
 * in the proper iBFT structure.
 */
static mode_t ibft_check_nic_for(void *data, int type)
{
	struct ibft_kobject *entry = data;
	struct ibft_nic *nic = entry->nic;
	mode_t rc = 0;

	switch (type) {
	case ISCSI_BOOT_ETH_INDEX:
	case ISCSI_BOOT_ETH_FLAGS:
		rc = S_IRUGO;
		break;
	case ISCSI_BOOT_ETH_IP_ADDR:
		if (memcmp(nic->ip_addr, nulls, sizeof(nic->ip_addr)))
			rc = S_IRUGO;
		break;
	case ISCSI_BOOT_ETH_SUBNET_MASK:
		if (nic->subnet_mask_prefix)
			rc = S_IRUGO;
		break;
	case ISCSI_BOOT_ETH_ORIGIN:
		rc = S_IRUGO;
		break;
	case ISCSI_BOOT_ETH_GATEWAY:
		if (memcmp(nic->gateway, nulls, sizeof(nic->gateway)))
			rc = S_IRUGO;
		break;
	case ISCSI_BOOT_ETH_PRIMARY_DNS:
		if (memcmp(nic->primary_dns, nulls,
			   sizeof(nic->primary_dns)))
			rc = S_IRUGO;
		break;
	case ISCSI_BOOT_ETH_SECONDARY_DNS:
		if (memcmp(nic->secondary_dns, nulls,
			   sizeof(nic->secondary_dns)))
			rc = S_IRUGO;
		break;
	case ISCSI_BOOT_ETH_DHCP:
		if (memcmp(nic->dhcp, nulls, sizeof(nic->dhcp)))
			rc = S_IRUGO;
		break;
	case ISCSI_BOOT_ETH_VLAN:
	case ISCSI_BOOT_ETH_MAC:
		rc = S_IRUGO;
		break;
	case ISCSI_BOOT_ETH_HOSTNAME:
		if (nic->hostname_off)
			rc = S_IRUGO;
		break;
	default:
		break;
	}

	return rc;
}

static mode_t __init ibft_check_tgt_for(void *data, int type)
{
	struct ibft_kobject *entry = data;
	struct ibft_tgt *tgt = entry->tgt;
	mode_t rc = 0;

	switch (type) {
	case ISCSI_BOOT_TGT_INDEX:
	case ISCSI_BOOT_TGT_FLAGS:
	case ISCSI_BOOT_TGT_IP_ADDR:
	case ISCSI_BOOT_TGT_PORT:
	case ISCSI_BOOT_TGT_LUN:
	case ISCSI_BOOT_TGT_NIC_ASSOC:
	case ISCSI_BOOT_TGT_CHAP_TYPE:
		rc = S_IRUGO;
	case ISCSI_BOOT_TGT_NAME:
		if (tgt->tgt_name_len)
			rc = S_IRUGO;
		break;
	case ISCSI_BOOT_TGT_CHAP_NAME:
	case ISCSI_BOOT_TGT_CHAP_SECRET:
		if (tgt->chap_name_len)
			rc = S_IRUGO;
		break;
	case ISCSI_BOOT_TGT_REV_CHAP_NAME:
	case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
		if (tgt->rev_chap_name_len)
			rc = S_IRUGO;
		break;
	default:
		break;
	}

	return rc;
}

static mode_t __init ibft_check_initiator_for(void *data, int type)
{
	struct ibft_kobject *entry = data;
	struct ibft_initiator *init = entry->initiator;
	mode_t rc = 0;

	switch (type) {
	case ISCSI_BOOT_INI_INDEX:
	case ISCSI_BOOT_INI_FLAGS:
		rc = S_IRUGO;
		break;
	case ISCSI_BOOT_INI_ISNS_SERVER:
		if (memcmp(init->isns_server, nulls,
			   sizeof(init->isns_server)))
			rc = S_IRUGO;
		break;
	case ISCSI_BOOT_INI_SLP_SERVER:
		if (memcmp(init->slp_server, nulls,
			   sizeof(init->slp_server)))
			rc = S_IRUGO;
		break;
	case ISCSI_BOOT_INI_PRI_RADIUS_SERVER:
		if (memcmp(init->pri_radius_server, nulls,
			   sizeof(init->pri_radius_server)))
			rc = S_IRUGO;
		break;
	case ISCSI_BOOT_INI_SEC_RADIUS_SERVER:
		if (memcmp(init->sec_radius_server, nulls,
			   sizeof(init->sec_radius_server)))
			rc = S_IRUGO;
		break;
	case ISCSI_BOOT_INI_INITIATOR_NAME:
		if (init->initiator_name_len)
			rc = S_IRUGO;
		break;
	default:
		break;
	}

	return rc;
}

569 570 571 572 573
static void ibft_kobj_release(void *data)
{
	kfree(data);
}

574 575 576
/*
 * Helper function for ibft_register_kobjects.
 */
577
static int __init ibft_create_kobject(struct acpi_table_ibft *header,
578
				      struct ibft_hdr *hdr)
579
{
580
	struct iscsi_boot_kobj *boot_kobj = NULL;
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
	struct ibft_kobject *ibft_kobj = NULL;
	struct ibft_nic *nic = (struct ibft_nic *)hdr;
	struct pci_dev *pci_dev;
	int rc = 0;

	ibft_kobj = kzalloc(sizeof(*ibft_kobj), GFP_KERNEL);
	if (!ibft_kobj)
		return -ENOMEM;

	ibft_kobj->header = header;
	ibft_kobj->hdr = hdr;

	switch (hdr->id) {
	case id_initiator:
		rc = ibft_verify_hdr("initiator", hdr, id_initiator,
				     sizeof(*ibft_kobj->initiator));
597 598 599 600 601 602
		if (rc)
			break;

		boot_kobj = iscsi_boot_create_initiator(boot_kset, hdr->index,
						ibft_kobj,
						ibft_attr_show_initiator,
603 604
						ibft_check_initiator_for,
						ibft_kobj_release);
605 606 607 608
		if (!boot_kobj) {
			rc = -ENOMEM;
			goto free_ibft_obj;
		}
609 610 611 612
		break;
	case id_nic:
		rc = ibft_verify_hdr("ethernet", hdr, id_nic,
				     sizeof(*ibft_kobj->nic));
613 614 615 616 617 618
		if (rc)
			break;

		boot_kobj = iscsi_boot_create_ethernet(boot_kset, hdr->index,
						       ibft_kobj,
						       ibft_attr_show_nic,
619 620
						       ibft_check_nic_for,
						       ibft_kobj_release);
621 622 623 624
		if (!boot_kobj) {
			rc = -ENOMEM;
			goto free_ibft_obj;
		}
625 626 627 628
		break;
	case id_target:
		rc = ibft_verify_hdr("target", hdr, id_target,
				     sizeof(*ibft_kobj->tgt));
629 630 631 632 633 634
		if (rc)
			break;

		boot_kobj = iscsi_boot_create_target(boot_kset, hdr->index,
						     ibft_kobj,
						     ibft_attr_show_target,
635 636
						     ibft_check_tgt_for,
						     ibft_kobj_release);
637 638 639 640
		if (!boot_kobj) {
			rc = -ENOMEM;
			goto free_ibft_obj;
		}
641 642 643 644 645 646 647 648 649 650
		break;
	case id_reserved:
	case id_control:
	case id_extensions:
		/* Fields which we don't support. Ignore them */
		rc = 1;
		break;
	default:
		printk(KERN_ERR "iBFT has unknown structure type (%d). " \
				"Report this bug to %.6s!\n", hdr->id,
651
				header->header.oem_id);
652 653 654 655 656 657
		rc = 1;
		break;
	}

	if (rc) {
		/* Skip adding this kobject, but exit with non-fatal error. */
658 659
		rc = 0;
		goto free_ibft_obj;
660 661 662 663 664 665 666 667 668 669 670 671
	}

	if (hdr->id == id_nic) {
		/*
		* We don't search for the device in other domains than
		* zero. This is because on x86 platforms the BIOS
		* executes only devices which are in domain 0. Furthermore, the
		* iBFT spec doesn't have a domain id field :-(
		*/
		pci_dev = pci_get_bus_and_slot((nic->pci_bdf & 0xff00) >> 8,
					       (nic->pci_bdf & 0xff));
		if (pci_dev) {
672
			rc = sysfs_create_link(&boot_kobj->kobj,
673 674 675 676
					       &pci_dev->dev.kobj, "device");
			pci_dev_put(pci_dev);
		}
	}
677
	return 0;
678

679 680
free_ibft_obj:
	kfree(ibft_kobj);
681 682 683 684 685 686 687 688
	return rc;
}

/*
 * Scan the IBFT table structure for the NIC and Target fields. When
 * found add them on the passed-in list. We do not support the other
 * fields at this point, so they are skipped.
 */
689
static int __init ibft_register_kobjects(struct acpi_table_ibft *header)
690 691 692 693 694 695 696 697 698
{
	struct ibft_control *control = NULL;
	void *ptr, *end;
	int rc = 0;
	u16 offset;
	u16 eot_offset;

	control = (void *)header + sizeof(*header);
	end = (void *)control + control->hdr.length;
699
	eot_offset = (void *)header + header->header.length - (void *)control;
700 701 702 703 704 705 706 707 708 709 710
	rc = ibft_verify_hdr("control", (struct ibft_hdr *)control, id_control,
			     sizeof(*control));

	/* iBFT table safety checking */
	rc |= ((control->hdr.index) ? -ENODEV : 0);
	if (rc) {
		printk(KERN_ERR "iBFT error: Control header is invalid!\n");
		return rc;
	}
	for (ptr = &control->initiator_off; ptr < end; ptr += sizeof(u16)) {
		offset = *(u16 *)ptr;
711 712
		if (offset && offset < header->header.length &&
						offset < eot_offset) {
713
			rc = ibft_create_kobject(header,
714
						 (void *)header + offset);
715 716 717 718 719 720 721 722
			if (rc)
				break;
		}
	}

	return rc;
}

723
static void ibft_unregister(void)
724
{
725 726 727 728 729 730 731 732
	struct iscsi_boot_kobj *boot_kobj, *tmp_kobj;
	struct ibft_kobject *ibft_kobj;

	list_for_each_entry_safe(boot_kobj, tmp_kobj,
				 &boot_kset->kobj_list, list) {
		ibft_kobj = boot_kobj->data;
		if (ibft_kobj->hdr->id == id_nic)
			sysfs_remove_link(&boot_kobj->kobj, "device");
733 734 735
	};
}

736
static void ibft_cleanup(void)
737
{
738 739 740 741
	if (boot_kset) {
		ibft_unregister();
		iscsi_boot_destroy_kset(boot_kset);
	}
742 743
}

744
static void __exit ibft_exit(void)
745
{
746
	ibft_cleanup();
747 748 749 750 751 752 753 754 755 756
}

/*
 * ibft_init() - creates sysfs tree entries for the iBFT data.
 */
static int __init ibft_init(void)
{
	int rc = 0;

	if (ibft_addr) {
757
		printk(KERN_INFO "iBFT detected at 0x%llx.\n",
758
		       (u64)isa_virt_to_bus(ibft_addr));
759 760 761

		rc = ibft_check_device();
		if (rc)
762
			return rc;
763

764 765 766
		boot_kset = iscsi_boot_create_kset("ibft");
		if (!boot_kset)
			return -ENOMEM;
767

768 769
		/* Scan the IBFT for data and register the kobjects. */
		rc = ibft_register_kobjects(ibft_addr);
770 771 772 773 774 775 776 777
		if (rc)
			goto out_free;
	} else
		printk(KERN_INFO "No iBFT detected.\n");

	return 0;

out_free:
778
	ibft_cleanup();
779 780 781 782 783
	return rc;
}

module_init(ibft_init);
module_exit(ibft_exit);