iscsi_ibft.c 19 KB

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