iscsi_ibft.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /*
  2. * Copyright 2007 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. * 14 Mar 2008 - Konrad Rzeszutek <ketuzsezr@darnok.org>
  23. * Updated comments and copyrights. (v0.4.9)
  24. *
  25. * 11 Feb 2008 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
  26. * Converted to using ibft_addr. (v0.4.8)
  27. *
  28. * 8 Feb 2008 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
  29. * Combined two functions in one: reserve_ibft_region. (v0.4.7)
  30. *
  31. * 30 Jan 2008 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
  32. * Added logic to handle IPv6 addresses. (v0.4.6)
  33. *
  34. * 25 Jan 2008 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
  35. * Added logic to handle badly not-to-spec iBFT. (v0.4.5)
  36. *
  37. * 4 Jan 2008 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
  38. * Added __init to function declarations. (v0.4.4)
  39. *
  40. * 21 Dec 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
  41. * Updated kobject registration, combined unregister functions in one
  42. * and code and style cleanup. (v0.4.3)
  43. *
  44. * 5 Dec 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
  45. * Added end-markers to enums and re-organized kobject registration. (v0.4.2)
  46. *
  47. * 4 Dec 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
  48. * Created 'device' sysfs link to the NIC and style cleanup. (v0.4.1)
  49. *
  50. * 28 Nov 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
  51. * Added sysfs-ibft documentation, moved 'find_ibft' function to
  52. * in its own file and added text attributes for every struct field. (v0.4)
  53. *
  54. * 21 Nov 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
  55. * Added text attributes emulating OpenFirmware /proc/device-tree naming.
  56. * Removed binary /sysfs interface (v0.3)
  57. *
  58. * 29 Aug 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
  59. * Added functionality in setup.c to reserve iBFT region. (v0.2)
  60. *
  61. * 27 Aug 2007 - Konrad Rzeszutek <konradr@linux.vnet.ibm.com>
  62. * First version exposing iBFT data via a binary /sysfs. (v0.1)
  63. *
  64. */
  65. #include <linux/blkdev.h>
  66. #include <linux/capability.h>
  67. #include <linux/ctype.h>
  68. #include <linux/device.h>
  69. #include <linux/err.h>
  70. #include <linux/init.h>
  71. #include <linux/iscsi_ibft.h>
  72. #include <linux/limits.h>
  73. #include <linux/module.h>
  74. #include <linux/pci.h>
  75. #include <linux/slab.h>
  76. #include <linux/stat.h>
  77. #include <linux/string.h>
  78. #include <linux/types.h>
  79. #define IBFT_ISCSI_VERSION "0.4.9"
  80. #define IBFT_ISCSI_DATE "2008-Mar-14"
  81. MODULE_AUTHOR("Peter Jones <pjones@redhat.com> and \
  82. Konrad Rzeszutek <ketuzsezr@darnok.org>");
  83. MODULE_DESCRIPTION("sysfs interface to BIOS iBFT information");
  84. MODULE_LICENSE("GPL");
  85. MODULE_VERSION(IBFT_ISCSI_VERSION);
  86. struct ibft_hdr {
  87. u8 id;
  88. u8 version;
  89. u16 length;
  90. u8 index;
  91. u8 flags;
  92. } __attribute__((__packed__));
  93. struct ibft_control {
  94. struct ibft_hdr hdr;
  95. u16 extensions;
  96. u16 initiator_off;
  97. u16 nic0_off;
  98. u16 tgt0_off;
  99. u16 nic1_off;
  100. u16 tgt1_off;
  101. } __attribute__((__packed__));
  102. struct ibft_initiator {
  103. struct ibft_hdr hdr;
  104. char isns_server[16];
  105. char slp_server[16];
  106. char pri_radius_server[16];
  107. char sec_radius_server[16];
  108. u16 initiator_name_len;
  109. u16 initiator_name_off;
  110. } __attribute__((__packed__));
  111. struct ibft_nic {
  112. struct ibft_hdr hdr;
  113. char ip_addr[16];
  114. u8 subnet_mask_prefix;
  115. u8 origin;
  116. char gateway[16];
  117. char primary_dns[16];
  118. char secondary_dns[16];
  119. char dhcp[16];
  120. u16 vlan;
  121. char mac[6];
  122. u16 pci_bdf;
  123. u16 hostname_len;
  124. u16 hostname_off;
  125. } __attribute__((__packed__));
  126. struct ibft_tgt {
  127. struct ibft_hdr hdr;
  128. char ip_addr[16];
  129. u16 port;
  130. char lun[8];
  131. u8 chap_type;
  132. u8 nic_assoc;
  133. u16 tgt_name_len;
  134. u16 tgt_name_off;
  135. u16 chap_name_len;
  136. u16 chap_name_off;
  137. u16 chap_secret_len;
  138. u16 chap_secret_off;
  139. u16 rev_chap_name_len;
  140. u16 rev_chap_name_off;
  141. u16 rev_chap_secret_len;
  142. u16 rev_chap_secret_off;
  143. } __attribute__((__packed__));
  144. /*
  145. * The kobject different types and its names.
  146. *
  147. */
  148. enum ibft_id {
  149. id_reserved = 0, /* We don't support. */
  150. id_control = 1, /* Should show up only once and is not exported. */
  151. id_initiator = 2,
  152. id_nic = 3,
  153. id_target = 4,
  154. id_extensions = 5, /* We don't support. */
  155. id_end_marker,
  156. };
  157. /*
  158. * We do not support the other types, hence the usage of NULL.
  159. * This maps to the enum ibft_id.
  160. */
  161. static const char *ibft_id_names[] =
  162. {NULL, NULL, "initiator", "ethernet%d", "target%d", NULL, NULL};
  163. /*
  164. * The text attributes names for each of the kobjects.
  165. */
  166. enum ibft_eth_properties_enum {
  167. ibft_eth_index,
  168. ibft_eth_flags,
  169. ibft_eth_ip_addr,
  170. ibft_eth_subnet_mask,
  171. ibft_eth_origin,
  172. ibft_eth_gateway,
  173. ibft_eth_primary_dns,
  174. ibft_eth_secondary_dns,
  175. ibft_eth_dhcp,
  176. ibft_eth_vlan,
  177. ibft_eth_mac,
  178. /* ibft_eth_pci_bdf - this is replaced by link to the device itself. */
  179. ibft_eth_hostname,
  180. ibft_eth_end_marker,
  181. };
  182. static const char *ibft_eth_properties[] =
  183. {"index", "flags", "ip-addr", "subnet-mask", "origin", "gateway",
  184. "primary-dns", "secondary-dns", "dhcp", "vlan", "mac", "hostname",
  185. NULL};
  186. enum ibft_tgt_properties_enum {
  187. ibft_tgt_index,
  188. ibft_tgt_flags,
  189. ibft_tgt_ip_addr,
  190. ibft_tgt_port,
  191. ibft_tgt_lun,
  192. ibft_tgt_chap_type,
  193. ibft_tgt_nic_assoc,
  194. ibft_tgt_name,
  195. ibft_tgt_chap_name,
  196. ibft_tgt_chap_secret,
  197. ibft_tgt_rev_chap_name,
  198. ibft_tgt_rev_chap_secret,
  199. ibft_tgt_end_marker,
  200. };
  201. static const char *ibft_tgt_properties[] =
  202. {"index", "flags", "ip-addr", "port", "lun", "chap-type", "nic-assoc",
  203. "target-name", "chap-name", "chap-secret", "rev-chap-name",
  204. "rev-chap-name-secret", NULL};
  205. enum ibft_initiator_properties_enum {
  206. ibft_init_index,
  207. ibft_init_flags,
  208. ibft_init_isns_server,
  209. ibft_init_slp_server,
  210. ibft_init_pri_radius_server,
  211. ibft_init_sec_radius_server,
  212. ibft_init_initiator_name,
  213. ibft_init_end_marker,
  214. };
  215. static const char *ibft_initiator_properties[] =
  216. {"index", "flags", "isns-server", "slp-server", "pri-radius-server",
  217. "sec-radius-server", "initiator-name", NULL};
  218. /*
  219. * The kobject and attribute structures.
  220. */
  221. struct ibft_kobject {
  222. struct ibft_table_header *header;
  223. union {
  224. struct ibft_initiator *initiator;
  225. struct ibft_nic *nic;
  226. struct ibft_tgt *tgt;
  227. struct ibft_hdr *hdr;
  228. };
  229. struct kobject kobj;
  230. struct list_head node;
  231. };
  232. struct ibft_attribute {
  233. struct attribute attr;
  234. ssize_t (*show) (struct ibft_kobject *entry,
  235. struct ibft_attribute *attr, char *buf);
  236. union {
  237. struct ibft_initiator *initiator;
  238. struct ibft_nic *nic;
  239. struct ibft_tgt *tgt;
  240. struct ibft_hdr *hdr;
  241. };
  242. struct kobject *kobj;
  243. int type; /* The enum of the type. This can be any value of:
  244. ibft_eth_properties_enum, ibft_tgt_properties_enum,
  245. or ibft_initiator_properties_enum. */
  246. struct list_head node;
  247. };
  248. static LIST_HEAD(ibft_attr_list);
  249. static LIST_HEAD(ibft_kobject_list);
  250. static const char nulls[16];
  251. /*
  252. * Helper functions to parse data properly.
  253. */
  254. static ssize_t sprintf_ipaddr(char *buf, u8 *ip)
  255. {
  256. char *str = buf;
  257. if (ip[0] == 0 && ip[1] == 0 && ip[2] == 0 && ip[3] == 0 &&
  258. ip[4] == 0 && ip[5] == 0 && ip[6] == 0 && ip[7] == 0 &&
  259. ip[8] == 0 && ip[9] == 0 && ip[10] == 0xff && ip[11] == 0xff) {
  260. /*
  261. * IPV4
  262. */
  263. str += sprintf(buf, "%pI4", ip + 12);
  264. } else {
  265. /*
  266. * IPv6
  267. */
  268. str += sprintf(str, "%pI6", ip);
  269. }
  270. str += sprintf(str, "\n");
  271. return str - buf;
  272. }
  273. static ssize_t sprintf_string(char *str, int len, char *buf)
  274. {
  275. return sprintf(str, "%.*s\n", len, buf);
  276. }
  277. /*
  278. * Helper function to verify the IBFT header.
  279. */
  280. static int ibft_verify_hdr(char *t, struct ibft_hdr *hdr, int id, int length)
  281. {
  282. if (hdr->id != id) {
  283. printk(KERN_ERR "iBFT error: We expected the " \
  284. "field header.id to have %d but " \
  285. "found %d instead!\n", id, hdr->id);
  286. return -ENODEV;
  287. }
  288. if (hdr->length != length) {
  289. printk(KERN_ERR "iBFT error: We expected the " \
  290. "field header.length to have %d but " \
  291. "found %d instead!\n", length, hdr->length);
  292. return -ENODEV;
  293. }
  294. return 0;
  295. }
  296. static void ibft_release(struct kobject *kobj)
  297. {
  298. struct ibft_kobject *ibft =
  299. container_of(kobj, struct ibft_kobject, kobj);
  300. kfree(ibft);
  301. }
  302. /*
  303. * Routines for parsing the iBFT data to be human readable.
  304. */
  305. static ssize_t ibft_attr_show_initiator(struct ibft_kobject *entry,
  306. struct ibft_attribute *attr,
  307. char *buf)
  308. {
  309. struct ibft_initiator *initiator = entry->initiator;
  310. void *ibft_loc = entry->header;
  311. char *str = buf;
  312. if (!initiator)
  313. return 0;
  314. switch (attr->type) {
  315. case ibft_init_index:
  316. str += sprintf(str, "%d\n", initiator->hdr.index);
  317. break;
  318. case ibft_init_flags:
  319. str += sprintf(str, "%d\n", initiator->hdr.flags);
  320. break;
  321. case ibft_init_isns_server:
  322. str += sprintf_ipaddr(str, initiator->isns_server);
  323. break;
  324. case ibft_init_slp_server:
  325. str += sprintf_ipaddr(str, initiator->slp_server);
  326. break;
  327. case ibft_init_pri_radius_server:
  328. str += sprintf_ipaddr(str, initiator->pri_radius_server);
  329. break;
  330. case ibft_init_sec_radius_server:
  331. str += sprintf_ipaddr(str, initiator->sec_radius_server);
  332. break;
  333. case ibft_init_initiator_name:
  334. str += sprintf_string(str, initiator->initiator_name_len,
  335. (char *)ibft_loc +
  336. initiator->initiator_name_off);
  337. break;
  338. default:
  339. break;
  340. }
  341. return str - buf;
  342. }
  343. static ssize_t ibft_attr_show_nic(struct ibft_kobject *entry,
  344. struct ibft_attribute *attr,
  345. char *buf)
  346. {
  347. struct ibft_nic *nic = entry->nic;
  348. void *ibft_loc = entry->header;
  349. char *str = buf;
  350. __be32 val;
  351. if (!nic)
  352. return 0;
  353. switch (attr->type) {
  354. case ibft_eth_index:
  355. str += sprintf(str, "%d\n", nic->hdr.index);
  356. break;
  357. case ibft_eth_flags:
  358. str += sprintf(str, "%d\n", nic->hdr.flags);
  359. break;
  360. case ibft_eth_ip_addr:
  361. str += sprintf_ipaddr(str, nic->ip_addr);
  362. break;
  363. case ibft_eth_subnet_mask:
  364. val = cpu_to_be32(~((1 << (32-nic->subnet_mask_prefix))-1));
  365. str += sprintf(str, "%pI4", &val);
  366. break;
  367. case ibft_eth_origin:
  368. str += sprintf(str, "%d\n", nic->origin);
  369. break;
  370. case ibft_eth_gateway:
  371. str += sprintf_ipaddr(str, nic->gateway);
  372. break;
  373. case ibft_eth_primary_dns:
  374. str += sprintf_ipaddr(str, nic->primary_dns);
  375. break;
  376. case ibft_eth_secondary_dns:
  377. str += sprintf_ipaddr(str, nic->secondary_dns);
  378. break;
  379. case ibft_eth_dhcp:
  380. str += sprintf_ipaddr(str, nic->dhcp);
  381. break;
  382. case ibft_eth_vlan:
  383. str += sprintf(str, "%d\n", nic->vlan);
  384. break;
  385. case ibft_eth_mac:
  386. str += sprintf(str, "%pM\n", nic->mac);
  387. break;
  388. case ibft_eth_hostname:
  389. str += sprintf_string(str, nic->hostname_len,
  390. (char *)ibft_loc + nic->hostname_off);
  391. break;
  392. default:
  393. break;
  394. }
  395. return str - buf;
  396. };
  397. static ssize_t ibft_attr_show_target(struct ibft_kobject *entry,
  398. struct ibft_attribute *attr,
  399. char *buf)
  400. {
  401. struct ibft_tgt *tgt = entry->tgt;
  402. void *ibft_loc = entry->header;
  403. char *str = buf;
  404. int i;
  405. if (!tgt)
  406. return 0;
  407. switch (attr->type) {
  408. case ibft_tgt_index:
  409. str += sprintf(str, "%d\n", tgt->hdr.index);
  410. break;
  411. case ibft_tgt_flags:
  412. str += sprintf(str, "%d\n", tgt->hdr.flags);
  413. break;
  414. case ibft_tgt_ip_addr:
  415. str += sprintf_ipaddr(str, tgt->ip_addr);
  416. break;
  417. case ibft_tgt_port:
  418. str += sprintf(str, "%d\n", tgt->port);
  419. break;
  420. case ibft_tgt_lun:
  421. for (i = 0; i < 8; i++)
  422. str += sprintf(str, "%x", (u8)tgt->lun[i]);
  423. str += sprintf(str, "\n");
  424. break;
  425. case ibft_tgt_nic_assoc:
  426. str += sprintf(str, "%d\n", tgt->nic_assoc);
  427. break;
  428. case ibft_tgt_chap_type:
  429. str += sprintf(str, "%d\n", tgt->chap_type);
  430. break;
  431. case ibft_tgt_name:
  432. str += sprintf_string(str, tgt->tgt_name_len,
  433. (char *)ibft_loc + tgt->tgt_name_off);
  434. break;
  435. case ibft_tgt_chap_name:
  436. str += sprintf_string(str, tgt->chap_name_len,
  437. (char *)ibft_loc + tgt->chap_name_off);
  438. break;
  439. case ibft_tgt_chap_secret:
  440. str += sprintf_string(str, tgt->chap_secret_len,
  441. (char *)ibft_loc + tgt->chap_secret_off);
  442. break;
  443. case ibft_tgt_rev_chap_name:
  444. str += sprintf_string(str, tgt->rev_chap_name_len,
  445. (char *)ibft_loc +
  446. tgt->rev_chap_name_off);
  447. break;
  448. case ibft_tgt_rev_chap_secret:
  449. str += sprintf_string(str, tgt->rev_chap_secret_len,
  450. (char *)ibft_loc +
  451. tgt->rev_chap_secret_off);
  452. break;
  453. default:
  454. break;
  455. }
  456. return str - buf;
  457. }
  458. /*
  459. * The routine called for all sysfs attributes.
  460. */
  461. static ssize_t ibft_show_attribute(struct kobject *kobj,
  462. struct attribute *attr,
  463. char *buf)
  464. {
  465. struct ibft_kobject *dev =
  466. container_of(kobj, struct ibft_kobject, kobj);
  467. struct ibft_attribute *ibft_attr =
  468. container_of(attr, struct ibft_attribute, attr);
  469. ssize_t ret = -EIO;
  470. char *str = buf;
  471. if (!capable(CAP_SYS_ADMIN))
  472. return -EACCES;
  473. if (ibft_attr->show)
  474. ret = ibft_attr->show(dev, ibft_attr, str);
  475. return ret;
  476. }
  477. static const struct sysfs_ops ibft_attr_ops = {
  478. .show = ibft_show_attribute,
  479. };
  480. static struct kobj_type ibft_ktype = {
  481. .release = ibft_release,
  482. .sysfs_ops = &ibft_attr_ops,
  483. };
  484. static struct kset *ibft_kset;
  485. static int __init ibft_check_device(void)
  486. {
  487. int len;
  488. u8 *pos;
  489. u8 csum = 0;
  490. len = ibft_addr->length;
  491. /* Sanity checking of iBFT. */
  492. if (ibft_addr->revision != 1) {
  493. printk(KERN_ERR "iBFT module supports only revision 1, " \
  494. "while this is %d.\n", ibft_addr->revision);
  495. return -ENOENT;
  496. }
  497. for (pos = (u8 *)ibft_addr; pos < (u8 *)ibft_addr + len; pos++)
  498. csum += *pos;
  499. if (csum) {
  500. printk(KERN_ERR "iBFT has incorrect checksum (0x%x)!\n", csum);
  501. return -ENOENT;
  502. }
  503. return 0;
  504. }
  505. /*
  506. * Helper function for ibft_register_kobjects.
  507. */
  508. static int __init ibft_create_kobject(struct ibft_table_header *header,
  509. struct ibft_hdr *hdr,
  510. struct list_head *list)
  511. {
  512. struct ibft_kobject *ibft_kobj = NULL;
  513. struct ibft_nic *nic = (struct ibft_nic *)hdr;
  514. struct pci_dev *pci_dev;
  515. int rc = 0;
  516. ibft_kobj = kzalloc(sizeof(*ibft_kobj), GFP_KERNEL);
  517. if (!ibft_kobj)
  518. return -ENOMEM;
  519. ibft_kobj->header = header;
  520. ibft_kobj->hdr = hdr;
  521. switch (hdr->id) {
  522. case id_initiator:
  523. rc = ibft_verify_hdr("initiator", hdr, id_initiator,
  524. sizeof(*ibft_kobj->initiator));
  525. break;
  526. case id_nic:
  527. rc = ibft_verify_hdr("ethernet", hdr, id_nic,
  528. sizeof(*ibft_kobj->nic));
  529. break;
  530. case id_target:
  531. rc = ibft_verify_hdr("target", hdr, id_target,
  532. sizeof(*ibft_kobj->tgt));
  533. break;
  534. case id_reserved:
  535. case id_control:
  536. case id_extensions:
  537. /* Fields which we don't support. Ignore them */
  538. rc = 1;
  539. break;
  540. default:
  541. printk(KERN_ERR "iBFT has unknown structure type (%d). " \
  542. "Report this bug to %.6s!\n", hdr->id,
  543. header->oem_id);
  544. rc = 1;
  545. break;
  546. }
  547. if (rc) {
  548. /* Skip adding this kobject, but exit with non-fatal error. */
  549. kfree(ibft_kobj);
  550. goto out_invalid_struct;
  551. }
  552. ibft_kobj->kobj.kset = ibft_kset;
  553. rc = kobject_init_and_add(&ibft_kobj->kobj, &ibft_ktype,
  554. NULL, ibft_id_names[hdr->id], hdr->index);
  555. if (rc) {
  556. kfree(ibft_kobj);
  557. goto out;
  558. }
  559. kobject_uevent(&ibft_kobj->kobj, KOBJ_ADD);
  560. if (hdr->id == id_nic) {
  561. /*
  562. * We don't search for the device in other domains than
  563. * zero. This is because on x86 platforms the BIOS
  564. * executes only devices which are in domain 0. Furthermore, the
  565. * iBFT spec doesn't have a domain id field :-(
  566. */
  567. pci_dev = pci_get_bus_and_slot((nic->pci_bdf & 0xff00) >> 8,
  568. (nic->pci_bdf & 0xff));
  569. if (pci_dev) {
  570. rc = sysfs_create_link(&ibft_kobj->kobj,
  571. &pci_dev->dev.kobj, "device");
  572. pci_dev_put(pci_dev);
  573. }
  574. }
  575. /* Nothing broke so lets add it to the list. */
  576. list_add_tail(&ibft_kobj->node, list);
  577. out:
  578. return rc;
  579. out_invalid_struct:
  580. /* Unsupported structs are skipped. */
  581. return 0;
  582. }
  583. /*
  584. * Scan the IBFT table structure for the NIC and Target fields. When
  585. * found add them on the passed-in list. We do not support the other
  586. * fields at this point, so they are skipped.
  587. */
  588. static int __init ibft_register_kobjects(struct ibft_table_header *header,
  589. struct list_head *list)
  590. {
  591. struct ibft_control *control = NULL;
  592. void *ptr, *end;
  593. int rc = 0;
  594. u16 offset;
  595. u16 eot_offset;
  596. control = (void *)header + sizeof(*header);
  597. end = (void *)control + control->hdr.length;
  598. eot_offset = (void *)header + header->length - (void *)control;
  599. rc = ibft_verify_hdr("control", (struct ibft_hdr *)control, id_control,
  600. sizeof(*control));
  601. /* iBFT table safety checking */
  602. rc |= ((control->hdr.index) ? -ENODEV : 0);
  603. if (rc) {
  604. printk(KERN_ERR "iBFT error: Control header is invalid!\n");
  605. return rc;
  606. }
  607. for (ptr = &control->initiator_off; ptr < end; ptr += sizeof(u16)) {
  608. offset = *(u16 *)ptr;
  609. if (offset && offset < header->length && offset < eot_offset) {
  610. rc = ibft_create_kobject(header,
  611. (void *)header + offset,
  612. list);
  613. if (rc)
  614. break;
  615. }
  616. }
  617. return rc;
  618. }
  619. static void ibft_unregister(struct list_head *attr_list,
  620. struct list_head *kobj_list)
  621. {
  622. struct ibft_kobject *data = NULL, *n;
  623. struct ibft_attribute *attr = NULL, *m;
  624. list_for_each_entry_safe(attr, m, attr_list, node) {
  625. sysfs_remove_file(attr->kobj, &attr->attr);
  626. list_del(&attr->node);
  627. kfree(attr);
  628. };
  629. list_del_init(attr_list);
  630. list_for_each_entry_safe(data, n, kobj_list, node) {
  631. list_del(&data->node);
  632. if (data->hdr->id == id_nic)
  633. sysfs_remove_link(&data->kobj, "device");
  634. kobject_put(&data->kobj);
  635. };
  636. list_del_init(kobj_list);
  637. }
  638. static int __init ibft_create_attribute(struct ibft_kobject *kobj_data,
  639. int type,
  640. const char *name,
  641. ssize_t (*show)(struct ibft_kobject *,
  642. struct ibft_attribute*,
  643. char *buf),
  644. struct list_head *list)
  645. {
  646. struct ibft_attribute *attr = NULL;
  647. struct ibft_hdr *hdr = kobj_data->hdr;
  648. attr = kmalloc(sizeof(*attr), GFP_KERNEL);
  649. if (!attr)
  650. return -ENOMEM;
  651. attr->attr.name = name;
  652. attr->attr.mode = S_IRUSR;
  653. attr->hdr = hdr;
  654. attr->show = show;
  655. attr->kobj = &kobj_data->kobj;
  656. attr->type = type;
  657. list_add_tail(&attr->node, list);
  658. return 0;
  659. }
  660. /*
  661. * Helper routiners to check to determine if the entry is valid
  662. * in the proper iBFT structure.
  663. */
  664. static int __init ibft_check_nic_for(struct ibft_nic *nic, int entry)
  665. {
  666. int rc = 0;
  667. switch (entry) {
  668. case ibft_eth_index:
  669. case ibft_eth_flags:
  670. rc = 1;
  671. break;
  672. case ibft_eth_ip_addr:
  673. if (memcmp(nic->ip_addr, nulls, sizeof(nic->ip_addr)))
  674. rc = 1;
  675. break;
  676. case ibft_eth_subnet_mask:
  677. if (nic->subnet_mask_prefix)
  678. rc = 1;
  679. break;
  680. case ibft_eth_origin:
  681. rc = 1;
  682. break;
  683. case ibft_eth_gateway:
  684. if (memcmp(nic->gateway, nulls, sizeof(nic->gateway)))
  685. rc = 1;
  686. break;
  687. case ibft_eth_primary_dns:
  688. if (memcmp(nic->primary_dns, nulls,
  689. sizeof(nic->primary_dns)))
  690. rc = 1;
  691. break;
  692. case ibft_eth_secondary_dns:
  693. if (memcmp(nic->secondary_dns, nulls,
  694. sizeof(nic->secondary_dns)))
  695. rc = 1;
  696. break;
  697. case ibft_eth_dhcp:
  698. if (memcmp(nic->dhcp, nulls, sizeof(nic->dhcp)))
  699. rc = 1;
  700. break;
  701. case ibft_eth_vlan:
  702. case ibft_eth_mac:
  703. rc = 1;
  704. break;
  705. case ibft_eth_hostname:
  706. if (nic->hostname_off)
  707. rc = 1;
  708. break;
  709. default:
  710. break;
  711. }
  712. return rc;
  713. }
  714. static int __init ibft_check_tgt_for(struct ibft_tgt *tgt, int entry)
  715. {
  716. int rc = 0;
  717. switch (entry) {
  718. case ibft_tgt_index:
  719. case ibft_tgt_flags:
  720. case ibft_tgt_ip_addr:
  721. case ibft_tgt_port:
  722. case ibft_tgt_lun:
  723. case ibft_tgt_nic_assoc:
  724. case ibft_tgt_chap_type:
  725. rc = 1;
  726. case ibft_tgt_name:
  727. if (tgt->tgt_name_len)
  728. rc = 1;
  729. break;
  730. case ibft_tgt_chap_name:
  731. case ibft_tgt_chap_secret:
  732. if (tgt->chap_name_len)
  733. rc = 1;
  734. break;
  735. case ibft_tgt_rev_chap_name:
  736. case ibft_tgt_rev_chap_secret:
  737. if (tgt->rev_chap_name_len)
  738. rc = 1;
  739. break;
  740. default:
  741. break;
  742. }
  743. return rc;
  744. }
  745. static int __init ibft_check_initiator_for(struct ibft_initiator *init,
  746. int entry)
  747. {
  748. int rc = 0;
  749. switch (entry) {
  750. case ibft_init_index:
  751. case ibft_init_flags:
  752. rc = 1;
  753. break;
  754. case ibft_init_isns_server:
  755. if (memcmp(init->isns_server, nulls,
  756. sizeof(init->isns_server)))
  757. rc = 1;
  758. break;
  759. case ibft_init_slp_server:
  760. if (memcmp(init->slp_server, nulls,
  761. sizeof(init->slp_server)))
  762. rc = 1;
  763. break;
  764. case ibft_init_pri_radius_server:
  765. if (memcmp(init->pri_radius_server, nulls,
  766. sizeof(init->pri_radius_server)))
  767. rc = 1;
  768. break;
  769. case ibft_init_sec_radius_server:
  770. if (memcmp(init->sec_radius_server, nulls,
  771. sizeof(init->sec_radius_server)))
  772. rc = 1;
  773. break;
  774. case ibft_init_initiator_name:
  775. if (init->initiator_name_len)
  776. rc = 1;
  777. break;
  778. default:
  779. break;
  780. }
  781. return rc;
  782. }
  783. /*
  784. * Register the attributes for all of the kobjects.
  785. */
  786. static int __init ibft_register_attributes(struct list_head *kobject_list,
  787. struct list_head *attr_list)
  788. {
  789. int rc = 0, i = 0;
  790. struct ibft_kobject *data = NULL;
  791. struct ibft_attribute *attr = NULL, *m;
  792. list_for_each_entry(data, kobject_list, node) {
  793. switch (data->hdr->id) {
  794. case id_nic:
  795. for (i = 0; i < ibft_eth_end_marker && !rc; i++)
  796. if (ibft_check_nic_for(data->nic, i))
  797. rc = ibft_create_attribute(data, i,
  798. ibft_eth_properties[i],
  799. ibft_attr_show_nic, attr_list);
  800. break;
  801. case id_target:
  802. for (i = 0; i < ibft_tgt_end_marker && !rc; i++)
  803. if (ibft_check_tgt_for(data->tgt, i))
  804. rc = ibft_create_attribute(data, i,
  805. ibft_tgt_properties[i],
  806. ibft_attr_show_target,
  807. attr_list);
  808. break;
  809. case id_initiator:
  810. for (i = 0; i < ibft_init_end_marker && !rc; i++)
  811. if (ibft_check_initiator_for(
  812. data->initiator, i))
  813. rc = ibft_create_attribute(data, i,
  814. ibft_initiator_properties[i],
  815. ibft_attr_show_initiator,
  816. attr_list);
  817. break;
  818. default:
  819. break;
  820. }
  821. if (rc)
  822. break;
  823. }
  824. list_for_each_entry_safe(attr, m, attr_list, node) {
  825. rc = sysfs_create_file(attr->kobj, &attr->attr);
  826. if (rc) {
  827. list_del(&attr->node);
  828. kfree(attr);
  829. break;
  830. }
  831. }
  832. return rc;
  833. }
  834. /*
  835. * ibft_init() - creates sysfs tree entries for the iBFT data.
  836. */
  837. static int __init ibft_init(void)
  838. {
  839. int rc = 0;
  840. ibft_kset = kset_create_and_add("ibft", NULL, firmware_kobj);
  841. if (!ibft_kset)
  842. return -ENOMEM;
  843. if (ibft_addr) {
  844. printk(KERN_INFO "iBFT detected at 0x%llx.\n",
  845. (u64)isa_virt_to_bus(ibft_addr));
  846. rc = ibft_check_device();
  847. if (rc)
  848. goto out_firmware_unregister;
  849. /* Scan the IBFT for data and register the kobjects. */
  850. rc = ibft_register_kobjects(ibft_addr, &ibft_kobject_list);
  851. if (rc)
  852. goto out_free;
  853. /* Register the attributes */
  854. rc = ibft_register_attributes(&ibft_kobject_list,
  855. &ibft_attr_list);
  856. if (rc)
  857. goto out_free;
  858. } else
  859. printk(KERN_INFO "No iBFT detected.\n");
  860. return 0;
  861. out_free:
  862. ibft_unregister(&ibft_attr_list, &ibft_kobject_list);
  863. out_firmware_unregister:
  864. kset_unregister(ibft_kset);
  865. return rc;
  866. }
  867. static void __exit ibft_exit(void)
  868. {
  869. ibft_unregister(&ibft_attr_list, &ibft_kobject_list);
  870. kset_unregister(ibft_kset);
  871. }
  872. module_init(ibft_init);
  873. module_exit(ibft_exit);