iscsi_ibft.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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. break;
  466. case ISCSI_BOOT_TGT_NAME:
  467. if (tgt->tgt_name_len)
  468. rc = S_IRUGO;
  469. break;
  470. case ISCSI_BOOT_TGT_CHAP_NAME:
  471. case ISCSI_BOOT_TGT_CHAP_SECRET:
  472. if (tgt->chap_name_len)
  473. rc = S_IRUGO;
  474. break;
  475. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  476. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  477. if (tgt->rev_chap_name_len)
  478. rc = S_IRUGO;
  479. break;
  480. default:
  481. break;
  482. }
  483. return rc;
  484. }
  485. static umode_t __init ibft_check_initiator_for(void *data, int type)
  486. {
  487. struct ibft_kobject *entry = data;
  488. struct ibft_initiator *init = entry->initiator;
  489. umode_t rc = 0;
  490. switch (type) {
  491. case ISCSI_BOOT_INI_INDEX:
  492. case ISCSI_BOOT_INI_FLAGS:
  493. rc = S_IRUGO;
  494. break;
  495. case ISCSI_BOOT_INI_ISNS_SERVER:
  496. if (memcmp(init->isns_server, nulls,
  497. sizeof(init->isns_server)))
  498. rc = S_IRUGO;
  499. break;
  500. case ISCSI_BOOT_INI_SLP_SERVER:
  501. if (memcmp(init->slp_server, nulls,
  502. sizeof(init->slp_server)))
  503. rc = S_IRUGO;
  504. break;
  505. case ISCSI_BOOT_INI_PRI_RADIUS_SERVER:
  506. if (memcmp(init->pri_radius_server, nulls,
  507. sizeof(init->pri_radius_server)))
  508. rc = S_IRUGO;
  509. break;
  510. case ISCSI_BOOT_INI_SEC_RADIUS_SERVER:
  511. if (memcmp(init->sec_radius_server, nulls,
  512. sizeof(init->sec_radius_server)))
  513. rc = S_IRUGO;
  514. break;
  515. case ISCSI_BOOT_INI_INITIATOR_NAME:
  516. if (init->initiator_name_len)
  517. rc = S_IRUGO;
  518. break;
  519. default:
  520. break;
  521. }
  522. return rc;
  523. }
  524. static void ibft_kobj_release(void *data)
  525. {
  526. kfree(data);
  527. }
  528. /*
  529. * Helper function for ibft_register_kobjects.
  530. */
  531. static int __init ibft_create_kobject(struct acpi_table_ibft *header,
  532. struct ibft_hdr *hdr)
  533. {
  534. struct iscsi_boot_kobj *boot_kobj = NULL;
  535. struct ibft_kobject *ibft_kobj = NULL;
  536. struct ibft_nic *nic = (struct ibft_nic *)hdr;
  537. struct pci_dev *pci_dev;
  538. int rc = 0;
  539. ibft_kobj = kzalloc(sizeof(*ibft_kobj), GFP_KERNEL);
  540. if (!ibft_kobj)
  541. return -ENOMEM;
  542. ibft_kobj->header = header;
  543. ibft_kobj->hdr = hdr;
  544. switch (hdr->id) {
  545. case id_initiator:
  546. rc = ibft_verify_hdr("initiator", hdr, id_initiator,
  547. sizeof(*ibft_kobj->initiator));
  548. if (rc)
  549. break;
  550. boot_kobj = iscsi_boot_create_initiator(boot_kset, hdr->index,
  551. ibft_kobj,
  552. ibft_attr_show_initiator,
  553. ibft_check_initiator_for,
  554. ibft_kobj_release);
  555. if (!boot_kobj) {
  556. rc = -ENOMEM;
  557. goto free_ibft_obj;
  558. }
  559. break;
  560. case id_nic:
  561. rc = ibft_verify_hdr("ethernet", hdr, id_nic,
  562. sizeof(*ibft_kobj->nic));
  563. if (rc)
  564. break;
  565. boot_kobj = iscsi_boot_create_ethernet(boot_kset, hdr->index,
  566. ibft_kobj,
  567. ibft_attr_show_nic,
  568. ibft_check_nic_for,
  569. ibft_kobj_release);
  570. if (!boot_kobj) {
  571. rc = -ENOMEM;
  572. goto free_ibft_obj;
  573. }
  574. break;
  575. case id_target:
  576. rc = ibft_verify_hdr("target", hdr, id_target,
  577. sizeof(*ibft_kobj->tgt));
  578. if (rc)
  579. break;
  580. boot_kobj = iscsi_boot_create_target(boot_kset, hdr->index,
  581. ibft_kobj,
  582. ibft_attr_show_target,
  583. ibft_check_tgt_for,
  584. ibft_kobj_release);
  585. if (!boot_kobj) {
  586. rc = -ENOMEM;
  587. goto free_ibft_obj;
  588. }
  589. break;
  590. case id_reserved:
  591. case id_control:
  592. case id_extensions:
  593. /* Fields which we don't support. Ignore them */
  594. rc = 1;
  595. break;
  596. default:
  597. printk(KERN_ERR "iBFT has unknown structure type (%d). " \
  598. "Report this bug to %.6s!\n", hdr->id,
  599. header->header.oem_id);
  600. rc = 1;
  601. break;
  602. }
  603. if (rc) {
  604. /* Skip adding this kobject, but exit with non-fatal error. */
  605. rc = 0;
  606. goto free_ibft_obj;
  607. }
  608. if (hdr->id == id_nic) {
  609. /*
  610. * We don't search for the device in other domains than
  611. * zero. This is because on x86 platforms the BIOS
  612. * executes only devices which are in domain 0. Furthermore, the
  613. * iBFT spec doesn't have a domain id field :-(
  614. */
  615. pci_dev = pci_get_bus_and_slot((nic->pci_bdf & 0xff00) >> 8,
  616. (nic->pci_bdf & 0xff));
  617. if (pci_dev) {
  618. rc = sysfs_create_link(&boot_kobj->kobj,
  619. &pci_dev->dev.kobj, "device");
  620. pci_dev_put(pci_dev);
  621. }
  622. }
  623. return 0;
  624. free_ibft_obj:
  625. kfree(ibft_kobj);
  626. return rc;
  627. }
  628. /*
  629. * Scan the IBFT table structure for the NIC and Target fields. When
  630. * found add them on the passed-in list. We do not support the other
  631. * fields at this point, so they are skipped.
  632. */
  633. static int __init ibft_register_kobjects(struct acpi_table_ibft *header)
  634. {
  635. struct ibft_control *control = NULL;
  636. void *ptr, *end;
  637. int rc = 0;
  638. u16 offset;
  639. u16 eot_offset;
  640. control = (void *)header + sizeof(*header);
  641. end = (void *)control + control->hdr.length;
  642. eot_offset = (void *)header + header->header.length - (void *)control;
  643. rc = ibft_verify_hdr("control", (struct ibft_hdr *)control, id_control,
  644. sizeof(*control));
  645. /* iBFT table safety checking */
  646. rc |= ((control->hdr.index) ? -ENODEV : 0);
  647. if (rc) {
  648. printk(KERN_ERR "iBFT error: Control header is invalid!\n");
  649. return rc;
  650. }
  651. for (ptr = &control->initiator_off; ptr < end; ptr += sizeof(u16)) {
  652. offset = *(u16 *)ptr;
  653. if (offset && offset < header->header.length &&
  654. offset < eot_offset) {
  655. rc = ibft_create_kobject(header,
  656. (void *)header + offset);
  657. if (rc)
  658. break;
  659. }
  660. }
  661. return rc;
  662. }
  663. static void ibft_unregister(void)
  664. {
  665. struct iscsi_boot_kobj *boot_kobj, *tmp_kobj;
  666. struct ibft_kobject *ibft_kobj;
  667. list_for_each_entry_safe(boot_kobj, tmp_kobj,
  668. &boot_kset->kobj_list, list) {
  669. ibft_kobj = boot_kobj->data;
  670. if (ibft_kobj->hdr->id == id_nic)
  671. sysfs_remove_link(&boot_kobj->kobj, "device");
  672. };
  673. }
  674. static void ibft_cleanup(void)
  675. {
  676. if (boot_kset) {
  677. ibft_unregister();
  678. iscsi_boot_destroy_kset(boot_kset);
  679. }
  680. }
  681. static void __exit ibft_exit(void)
  682. {
  683. ibft_cleanup();
  684. }
  685. #ifdef CONFIG_ACPI
  686. static const struct {
  687. char *sign;
  688. } ibft_signs[] = {
  689. /*
  690. * One spec says "IBFT", the other says "iBFT". We have to check
  691. * for both.
  692. */
  693. { ACPI_SIG_IBFT },
  694. { "iBFT" },
  695. };
  696. static void __init acpi_find_ibft_region(void)
  697. {
  698. int i;
  699. struct acpi_table_header *table = NULL;
  700. if (acpi_disabled)
  701. return;
  702. for (i = 0; i < ARRAY_SIZE(ibft_signs) && !ibft_addr; i++) {
  703. acpi_get_table(ibft_signs[i].sign, 0, &table);
  704. ibft_addr = (struct acpi_table_ibft *)table;
  705. }
  706. }
  707. #else
  708. static void __init acpi_find_ibft_region(void)
  709. {
  710. }
  711. #endif
  712. /*
  713. * ibft_init() - creates sysfs tree entries for the iBFT data.
  714. */
  715. static int __init ibft_init(void)
  716. {
  717. int rc = 0;
  718. /*
  719. As on UEFI systems the setup_arch()/find_ibft_region()
  720. is called before ACPI tables are parsed and it only does
  721. legacy finding.
  722. */
  723. if (!ibft_addr)
  724. acpi_find_ibft_region();
  725. if (ibft_addr) {
  726. pr_info("iBFT detected.\n");
  727. rc = ibft_check_device();
  728. if (rc)
  729. return rc;
  730. boot_kset = iscsi_boot_create_kset("ibft");
  731. if (!boot_kset)
  732. return -ENOMEM;
  733. /* Scan the IBFT for data and register the kobjects. */
  734. rc = ibft_register_kobjects(ibft_addr);
  735. if (rc)
  736. goto out_free;
  737. } else
  738. printk(KERN_INFO "No iBFT detected.\n");
  739. return 0;
  740. out_free:
  741. ibft_cleanup();
  742. return rc;
  743. }
  744. module_init(ibft_init);
  745. module_exit(ibft_exit);