iscsi_ibft.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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, NIPQUAD_FMT, ip[12],
  264. ip[13], ip[14], ip[15]);
  265. } else {
  266. /*
  267. * IPv6
  268. */
  269. str += sprintf(str, NIP6_FMT, ntohs(ip[0]), ntohs(ip[1]),
  270. ntohs(ip[2]), ntohs(ip[3]), ntohs(ip[4]),
  271. ntohs(ip[5]), ntohs(ip[6]), ntohs(ip[7]));
  272. }
  273. str += sprintf(str, "\n");
  274. return str - buf;
  275. }
  276. static ssize_t sprintf_string(char *str, int len, char *buf)
  277. {
  278. return sprintf(str, "%.*s\n", len, buf);
  279. }
  280. /*
  281. * Helper function to verify the IBFT header.
  282. */
  283. static int ibft_verify_hdr(char *t, struct ibft_hdr *hdr, int id, int length)
  284. {
  285. if (hdr->id != id) {
  286. printk(KERN_ERR "iBFT error: We expected the " \
  287. "field header.id to have %d but " \
  288. "found %d instead!\n", id, hdr->id);
  289. return -ENODEV;
  290. }
  291. if (hdr->length != length) {
  292. printk(KERN_ERR "iBFT error: We expected the " \
  293. "field header.length to have %d but " \
  294. "found %d instead!\n", length, hdr->length);
  295. return -ENODEV;
  296. }
  297. return 0;
  298. }
  299. static void ibft_release(struct kobject *kobj)
  300. {
  301. struct ibft_kobject *ibft =
  302. container_of(kobj, struct ibft_kobject, kobj);
  303. kfree(ibft);
  304. }
  305. /*
  306. * Routines for parsing the iBFT data to be human readable.
  307. */
  308. ssize_t ibft_attr_show_initiator(struct ibft_kobject *entry,
  309. struct ibft_attribute *attr,
  310. char *buf)
  311. {
  312. struct ibft_initiator *initiator = entry->initiator;
  313. void *ibft_loc = entry->header;
  314. char *str = buf;
  315. if (!initiator)
  316. return 0;
  317. switch (attr->type) {
  318. case ibft_init_index:
  319. str += sprintf(str, "%d\n", initiator->hdr.index);
  320. break;
  321. case ibft_init_flags:
  322. str += sprintf(str, "%d\n", initiator->hdr.flags);
  323. break;
  324. case ibft_init_isns_server:
  325. str += sprintf_ipaddr(str, initiator->isns_server);
  326. break;
  327. case ibft_init_slp_server:
  328. str += sprintf_ipaddr(str, initiator->slp_server);
  329. break;
  330. case ibft_init_pri_radius_server:
  331. str += sprintf_ipaddr(str, initiator->pri_radius_server);
  332. break;
  333. case ibft_init_sec_radius_server:
  334. str += sprintf_ipaddr(str, initiator->sec_radius_server);
  335. break;
  336. case ibft_init_initiator_name:
  337. str += sprintf_string(str, initiator->initiator_name_len,
  338. (char *)ibft_loc +
  339. initiator->initiator_name_off);
  340. break;
  341. default:
  342. break;
  343. }
  344. return str - buf;
  345. }
  346. ssize_t ibft_attr_show_nic(struct ibft_kobject *entry,
  347. struct ibft_attribute *attr,
  348. char *buf)
  349. {
  350. struct ibft_nic *nic = entry->nic;
  351. void *ibft_loc = entry->header;
  352. char *str = buf;
  353. char *mac;
  354. int val;
  355. if (!nic)
  356. return 0;
  357. switch (attr->type) {
  358. case ibft_eth_index:
  359. str += sprintf(str, "%d\n", nic->hdr.index);
  360. break;
  361. case ibft_eth_flags:
  362. str += sprintf(str, "%d\n", nic->hdr.flags);
  363. break;
  364. case ibft_eth_ip_addr:
  365. str += sprintf_ipaddr(str, nic->ip_addr);
  366. break;
  367. case ibft_eth_subnet_mask:
  368. val = ~((1 << (32-nic->subnet_mask_prefix))-1);
  369. str += sprintf(str, NIPQUAD_FMT,
  370. (u8)(val >> 24), (u8)(val >> 16),
  371. (u8)(val >> 8), (u8)(val));
  372. break;
  373. case ibft_eth_origin:
  374. str += sprintf(str, "%d\n", nic->origin);
  375. break;
  376. case ibft_eth_gateway:
  377. str += sprintf_ipaddr(str, nic->gateway);
  378. break;
  379. case ibft_eth_primary_dns:
  380. str += sprintf_ipaddr(str, nic->primary_dns);
  381. break;
  382. case ibft_eth_secondary_dns:
  383. str += sprintf_ipaddr(str, nic->secondary_dns);
  384. break;
  385. case ibft_eth_dhcp:
  386. str += sprintf_ipaddr(str, nic->dhcp);
  387. break;
  388. case ibft_eth_vlan:
  389. str += sprintf(str, "%d\n", nic->vlan);
  390. break;
  391. case ibft_eth_mac:
  392. mac = nic->mac;
  393. str += sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x\n",
  394. (u8)mac[0], (u8)mac[1], (u8)mac[2],
  395. (u8)mac[3], (u8)mac[4], (u8)mac[5]);
  396. break;
  397. case ibft_eth_hostname:
  398. str += sprintf_string(str, nic->hostname_len,
  399. (char *)ibft_loc + nic->hostname_off);
  400. break;
  401. default:
  402. break;
  403. }
  404. return str - buf;
  405. };
  406. ssize_t ibft_attr_show_target(struct ibft_kobject *entry,
  407. struct ibft_attribute *attr,
  408. char *buf)
  409. {
  410. struct ibft_tgt *tgt = entry->tgt;
  411. void *ibft_loc = entry->header;
  412. char *str = buf;
  413. int i;
  414. if (!tgt)
  415. return 0;
  416. switch (attr->type) {
  417. case ibft_tgt_index:
  418. str += sprintf(str, "%d\n", tgt->hdr.index);
  419. break;
  420. case ibft_tgt_flags:
  421. str += sprintf(str, "%d\n", tgt->hdr.flags);
  422. break;
  423. case ibft_tgt_ip_addr:
  424. str += sprintf_ipaddr(str, tgt->ip_addr);
  425. break;
  426. case ibft_tgt_port:
  427. str += sprintf(str, "%d\n", tgt->port);
  428. break;
  429. case ibft_tgt_lun:
  430. for (i = 0; i < 8; i++)
  431. str += sprintf(str, "%x", (u8)tgt->lun[i]);
  432. str += sprintf(str, "\n");
  433. break;
  434. case ibft_tgt_nic_assoc:
  435. str += sprintf(str, "%d\n", tgt->nic_assoc);
  436. break;
  437. case ibft_tgt_chap_type:
  438. str += sprintf(str, "%d\n", tgt->chap_type);
  439. break;
  440. case ibft_tgt_name:
  441. str += sprintf_string(str, tgt->tgt_name_len,
  442. (char *)ibft_loc + tgt->tgt_name_off);
  443. break;
  444. case ibft_tgt_chap_name:
  445. str += sprintf_string(str, tgt->chap_name_len,
  446. (char *)ibft_loc + tgt->chap_name_off);
  447. break;
  448. case ibft_tgt_chap_secret:
  449. str += sprintf_string(str, tgt->chap_secret_len,
  450. (char *)ibft_loc + tgt->chap_secret_off);
  451. break;
  452. case ibft_tgt_rev_chap_name:
  453. str += sprintf_string(str, tgt->rev_chap_name_len,
  454. (char *)ibft_loc +
  455. tgt->rev_chap_name_off);
  456. break;
  457. case ibft_tgt_rev_chap_secret:
  458. str += sprintf_string(str, tgt->rev_chap_secret_len,
  459. (char *)ibft_loc +
  460. tgt->rev_chap_secret_off);
  461. break;
  462. default:
  463. break;
  464. }
  465. return str - buf;
  466. }
  467. /*
  468. * The routine called for all sysfs attributes.
  469. */
  470. static ssize_t ibft_show_attribute(struct kobject *kobj,
  471. struct attribute *attr,
  472. char *buf)
  473. {
  474. struct ibft_kobject *dev =
  475. container_of(kobj, struct ibft_kobject, kobj);
  476. struct ibft_attribute *ibft_attr =
  477. container_of(attr, struct ibft_attribute, attr);
  478. ssize_t ret = -EIO;
  479. char *str = buf;
  480. if (!capable(CAP_SYS_ADMIN))
  481. return -EACCES;
  482. if (ibft_attr->show)
  483. ret = ibft_attr->show(dev, ibft_attr, str);
  484. return ret;
  485. }
  486. static struct sysfs_ops ibft_attr_ops = {
  487. .show = ibft_show_attribute,
  488. };
  489. static struct kobj_type ibft_ktype = {
  490. .release = ibft_release,
  491. .sysfs_ops = &ibft_attr_ops,
  492. };
  493. static struct kset *ibft_kset;
  494. static int __init ibft_check_device(void)
  495. {
  496. int len;
  497. u8 *pos;
  498. u8 csum = 0;
  499. len = ibft_addr->length;
  500. /* Sanity checking of iBFT. */
  501. if (ibft_addr->revision != 1) {
  502. printk(KERN_ERR "iBFT module supports only revision 1, " \
  503. "while this is %d.\n", ibft_addr->revision);
  504. return -ENOENT;
  505. }
  506. for (pos = (u8 *)ibft_addr; pos < (u8 *)ibft_addr + len; pos++)
  507. csum += *pos;
  508. if (csum) {
  509. printk(KERN_ERR "iBFT has incorrect checksum (0x%x)!\n", csum);
  510. return -ENOENT;
  511. }
  512. return 0;
  513. }
  514. /*
  515. * Helper function for ibft_register_kobjects.
  516. */
  517. static int __init ibft_create_kobject(struct ibft_table_header *header,
  518. struct ibft_hdr *hdr,
  519. struct list_head *list)
  520. {
  521. struct ibft_kobject *ibft_kobj = NULL;
  522. struct ibft_nic *nic = (struct ibft_nic *)hdr;
  523. struct pci_dev *pci_dev;
  524. int rc = 0;
  525. ibft_kobj = kzalloc(sizeof(*ibft_kobj), GFP_KERNEL);
  526. if (!ibft_kobj)
  527. return -ENOMEM;
  528. ibft_kobj->header = header;
  529. ibft_kobj->hdr = hdr;
  530. switch (hdr->id) {
  531. case id_initiator:
  532. rc = ibft_verify_hdr("initiator", hdr, id_initiator,
  533. sizeof(*ibft_kobj->initiator));
  534. break;
  535. case id_nic:
  536. rc = ibft_verify_hdr("ethernet", hdr, id_nic,
  537. sizeof(*ibft_kobj->nic));
  538. break;
  539. case id_target:
  540. rc = ibft_verify_hdr("target", hdr, id_target,
  541. sizeof(*ibft_kobj->tgt));
  542. break;
  543. case id_reserved:
  544. case id_control:
  545. case id_extensions:
  546. /* Fields which we don't support. Ignore them */
  547. rc = 1;
  548. break;
  549. default:
  550. printk(KERN_ERR "iBFT has unknown structure type (%d). " \
  551. "Report this bug to %.6s!\n", hdr->id,
  552. header->oem_id);
  553. rc = 1;
  554. break;
  555. }
  556. if (rc) {
  557. /* Skip adding this kobject, but exit with non-fatal error. */
  558. kfree(ibft_kobj);
  559. goto out_invalid_struct;
  560. }
  561. ibft_kobj->kobj.kset = ibft_kset;
  562. rc = kobject_init_and_add(&ibft_kobj->kobj, &ibft_ktype,
  563. NULL, ibft_id_names[hdr->id], hdr->index);
  564. if (rc) {
  565. kfree(ibft_kobj);
  566. goto out;
  567. }
  568. kobject_uevent(&ibft_kobj->kobj, KOBJ_ADD);
  569. if (hdr->id == id_nic) {
  570. /*
  571. * We don't search for the device in other domains than
  572. * zero. This is because on x86 platforms the BIOS
  573. * executes only devices which are in domain 0. Furthermore, the
  574. * iBFT spec doesn't have a domain id field :-(
  575. */
  576. pci_dev = pci_get_bus_and_slot((nic->pci_bdf & 0xff00) >> 8,
  577. (nic->pci_bdf & 0xff));
  578. if (pci_dev) {
  579. rc = sysfs_create_link(&ibft_kobj->kobj,
  580. &pci_dev->dev.kobj, "device");
  581. pci_dev_put(pci_dev);
  582. }
  583. }
  584. /* Nothing broke so lets add it to the list. */
  585. list_add_tail(&ibft_kobj->node, list);
  586. out:
  587. return rc;
  588. out_invalid_struct:
  589. /* Unsupported structs are skipped. */
  590. return 0;
  591. }
  592. /*
  593. * Scan the IBFT table structure for the NIC and Target fields. When
  594. * found add them on the passed-in list. We do not support the other
  595. * fields at this point, so they are skipped.
  596. */
  597. static int __init ibft_register_kobjects(struct ibft_table_header *header,
  598. struct list_head *list)
  599. {
  600. struct ibft_control *control = NULL;
  601. void *ptr, *end;
  602. int rc = 0;
  603. u16 offset;
  604. u16 eot_offset;
  605. control = (void *)header + sizeof(*header);
  606. end = (void *)control + control->hdr.length;
  607. eot_offset = (void *)header + header->length - (void *)control;
  608. rc = ibft_verify_hdr("control", (struct ibft_hdr *)control, id_control,
  609. sizeof(*control));
  610. /* iBFT table safety checking */
  611. rc |= ((control->hdr.index) ? -ENODEV : 0);
  612. if (rc) {
  613. printk(KERN_ERR "iBFT error: Control header is invalid!\n");
  614. return rc;
  615. }
  616. for (ptr = &control->initiator_off; ptr < end; ptr += sizeof(u16)) {
  617. offset = *(u16 *)ptr;
  618. if (offset && offset < header->length && offset < eot_offset) {
  619. rc = ibft_create_kobject(header,
  620. (void *)header + offset,
  621. list);
  622. if (rc)
  623. break;
  624. }
  625. }
  626. return rc;
  627. }
  628. static void ibft_unregister(struct list_head *attr_list,
  629. struct list_head *kobj_list)
  630. {
  631. struct ibft_kobject *data = NULL, *n;
  632. struct ibft_attribute *attr = NULL, *m;
  633. list_for_each_entry_safe(attr, m, attr_list, node) {
  634. sysfs_remove_file(attr->kobj, &attr->attr);
  635. list_del(&attr->node);
  636. kfree(attr);
  637. };
  638. list_del_init(attr_list);
  639. list_for_each_entry_safe(data, n, kobj_list, node) {
  640. list_del(&data->node);
  641. if (data->hdr->id == id_nic)
  642. sysfs_remove_link(&data->kobj, "device");
  643. kobject_put(&data->kobj);
  644. };
  645. list_del_init(kobj_list);
  646. }
  647. static int __init ibft_create_attribute(struct ibft_kobject *kobj_data,
  648. int type,
  649. const char *name,
  650. ssize_t (*show)(struct ibft_kobject *,
  651. struct ibft_attribute*,
  652. char *buf),
  653. struct list_head *list)
  654. {
  655. struct ibft_attribute *attr = NULL;
  656. struct ibft_hdr *hdr = kobj_data->hdr;
  657. attr = kmalloc(sizeof(*attr), GFP_KERNEL);
  658. if (!attr)
  659. return -ENOMEM;
  660. attr->attr.name = name;
  661. attr->attr.mode = S_IRUSR;
  662. attr->attr.owner = THIS_MODULE;
  663. attr->hdr = hdr;
  664. attr->show = show;
  665. attr->kobj = &kobj_data->kobj;
  666. attr->type = type;
  667. list_add_tail(&attr->node, list);
  668. return 0;
  669. }
  670. /*
  671. * Helper routiners to check to determine if the entry is valid
  672. * in the proper iBFT structure.
  673. */
  674. static int __init ibft_check_nic_for(struct ibft_nic *nic, int entry)
  675. {
  676. int rc = 0;
  677. switch (entry) {
  678. case ibft_eth_index:
  679. case ibft_eth_flags:
  680. rc = 1;
  681. break;
  682. case ibft_eth_ip_addr:
  683. if (!memcmp(nic->dhcp, nulls, sizeof(nic->dhcp)))
  684. rc = 1;
  685. break;
  686. case ibft_eth_subnet_mask:
  687. if (!memcmp(nic->dhcp, nulls, sizeof(nic->dhcp)))
  688. rc = 1;
  689. break;
  690. case ibft_eth_origin:
  691. rc = 1;
  692. break;
  693. case ibft_eth_gateway:
  694. if (memcmp(nic->gateway, nulls, sizeof(nic->gateway)))
  695. rc = 1;
  696. break;
  697. case ibft_eth_primary_dns:
  698. if (memcmp(nic->primary_dns, nulls,
  699. sizeof(nic->primary_dns)))
  700. rc = 1;
  701. break;
  702. case ibft_eth_secondary_dns:
  703. if (memcmp(nic->secondary_dns, nulls,
  704. sizeof(nic->secondary_dns)))
  705. rc = 1;
  706. break;
  707. case ibft_eth_dhcp:
  708. if (memcmp(nic->dhcp, nulls, sizeof(nic->dhcp)))
  709. rc = 1;
  710. break;
  711. case ibft_eth_vlan:
  712. case ibft_eth_mac:
  713. rc = 1;
  714. break;
  715. case ibft_eth_hostname:
  716. if (nic->hostname_off)
  717. rc = 1;
  718. break;
  719. default:
  720. break;
  721. }
  722. return rc;
  723. }
  724. static int __init ibft_check_tgt_for(struct ibft_tgt *tgt, int entry)
  725. {
  726. int rc = 0;
  727. switch (entry) {
  728. case ibft_tgt_index:
  729. case ibft_tgt_flags:
  730. case ibft_tgt_ip_addr:
  731. case ibft_tgt_port:
  732. case ibft_tgt_lun:
  733. case ibft_tgt_nic_assoc:
  734. case ibft_tgt_chap_type:
  735. rc = 1;
  736. case ibft_tgt_name:
  737. if (tgt->tgt_name_len)
  738. rc = 1;
  739. break;
  740. case ibft_tgt_chap_name:
  741. case ibft_tgt_chap_secret:
  742. if (tgt->chap_name_len)
  743. rc = 1;
  744. break;
  745. case ibft_tgt_rev_chap_name:
  746. case ibft_tgt_rev_chap_secret:
  747. if (tgt->rev_chap_name_len)
  748. rc = 1;
  749. break;
  750. default:
  751. break;
  752. }
  753. return rc;
  754. }
  755. static int __init ibft_check_initiator_for(struct ibft_initiator *init,
  756. int entry)
  757. {
  758. int rc = 0;
  759. switch (entry) {
  760. case ibft_init_index:
  761. case ibft_init_flags:
  762. rc = 1;
  763. break;
  764. case ibft_init_isns_server:
  765. if (memcmp(init->isns_server, nulls,
  766. sizeof(init->isns_server)))
  767. rc = 1;
  768. break;
  769. case ibft_init_slp_server:
  770. if (memcmp(init->slp_server, nulls,
  771. sizeof(init->slp_server)))
  772. rc = 1;
  773. break;
  774. case ibft_init_pri_radius_server:
  775. if (memcmp(init->pri_radius_server, nulls,
  776. sizeof(init->pri_radius_server)))
  777. rc = 1;
  778. break;
  779. case ibft_init_sec_radius_server:
  780. if (memcmp(init->sec_radius_server, nulls,
  781. sizeof(init->sec_radius_server)))
  782. rc = 1;
  783. break;
  784. case ibft_init_initiator_name:
  785. if (init->initiator_name_len)
  786. rc = 1;
  787. break;
  788. default:
  789. break;
  790. }
  791. return rc;
  792. }
  793. /*
  794. * Register the attributes for all of the kobjects.
  795. */
  796. static int __init ibft_register_attributes(struct list_head *kobject_list,
  797. struct list_head *attr_list)
  798. {
  799. int rc = 0, i = 0;
  800. struct ibft_kobject *data = NULL;
  801. struct ibft_attribute *attr = NULL, *m;
  802. list_for_each_entry(data, kobject_list, node) {
  803. switch (data->hdr->id) {
  804. case id_nic:
  805. for (i = 0; i < ibft_eth_end_marker && !rc; i++)
  806. if (ibft_check_nic_for(data->nic, i))
  807. rc = ibft_create_attribute(data, i,
  808. ibft_eth_properties[i],
  809. ibft_attr_show_nic, attr_list);
  810. break;
  811. case id_target:
  812. for (i = 0; i < ibft_tgt_end_marker && !rc; i++)
  813. if (ibft_check_tgt_for(data->tgt, i))
  814. rc = ibft_create_attribute(data, i,
  815. ibft_tgt_properties[i],
  816. ibft_attr_show_target,
  817. attr_list);
  818. break;
  819. case id_initiator:
  820. for (i = 0; i < ibft_init_end_marker && !rc; i++)
  821. if (ibft_check_initiator_for(
  822. data->initiator, i))
  823. rc = ibft_create_attribute(data, i,
  824. ibft_initiator_properties[i],
  825. ibft_attr_show_initiator,
  826. attr_list);
  827. break;
  828. default:
  829. break;
  830. }
  831. if (rc)
  832. break;
  833. }
  834. list_for_each_entry_safe(attr, m, attr_list, node) {
  835. rc = sysfs_create_file(attr->kobj, &attr->attr);
  836. if (rc) {
  837. list_del(&attr->node);
  838. kfree(attr);
  839. break;
  840. }
  841. }
  842. return rc;
  843. }
  844. /*
  845. * ibft_init() - creates sysfs tree entries for the iBFT data.
  846. */
  847. static int __init ibft_init(void)
  848. {
  849. int rc = 0;
  850. ibft_kset = kset_create_and_add("ibft", NULL, firmware_kobj);
  851. if (!ibft_kset)
  852. return -ENOMEM;
  853. if (ibft_addr) {
  854. printk(KERN_INFO "iBFT detected at 0x%lx.\n",
  855. virt_to_phys((void *)ibft_addr));
  856. rc = ibft_check_device();
  857. if (rc)
  858. goto out_firmware_unregister;
  859. /* Scan the IBFT for data and register the kobjects. */
  860. rc = ibft_register_kobjects(ibft_addr, &ibft_kobject_list);
  861. if (rc)
  862. goto out_free;
  863. /* Register the attributes */
  864. rc = ibft_register_attributes(&ibft_kobject_list,
  865. &ibft_attr_list);
  866. if (rc)
  867. goto out_free;
  868. } else
  869. printk(KERN_INFO "No iBFT detected.\n");
  870. return 0;
  871. out_free:
  872. ibft_unregister(&ibft_attr_list, &ibft_kobject_list);
  873. out_firmware_unregister:
  874. kset_unregister(ibft_kset);
  875. return rc;
  876. }
  877. static void __exit ibft_exit(void)
  878. {
  879. ibft_unregister(&ibft_attr_list, &ibft_kobject_list);
  880. kset_unregister(ibft_kset);
  881. }
  882. module_init(ibft_init);
  883. module_exit(ibft_exit);