iscsi_ibft.c 19 KB

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