iscsi_ibft.c 20 KB

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