nodemgr.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731
  1. /*
  2. * Node information (ConfigROM) collection and management.
  3. *
  4. * Copyright (C) 2000 Andreas E. Bombe
  5. * 2001-2003 Ben Collins <bcollins@debian.net>
  6. *
  7. * This code is licensed under the GPL. See the file COPYING in the root
  8. * directory of the kernel sources for details.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/config.h>
  12. #include <linux/list.h>
  13. #include <linux/slab.h>
  14. #include <linux/smp_lock.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/kmod.h>
  17. #include <linux/completion.h>
  18. #include <linux/delay.h>
  19. #include <linux/pci.h>
  20. #include <linux/moduleparam.h>
  21. #include <asm/atomic.h>
  22. #include "ieee1394_types.h"
  23. #include "ieee1394.h"
  24. #include "ieee1394_core.h"
  25. #include "hosts.h"
  26. #include "ieee1394_transactions.h"
  27. #include "highlevel.h"
  28. #include "csr.h"
  29. #include "nodemgr.h"
  30. static int ignore_drivers = 0;
  31. module_param(ignore_drivers, int, 0444);
  32. MODULE_PARM_DESC(ignore_drivers, "Disable automatic probing for drivers.");
  33. struct nodemgr_csr_info {
  34. struct hpsb_host *host;
  35. nodeid_t nodeid;
  36. unsigned int generation;
  37. };
  38. static char *nodemgr_find_oui_name(int oui)
  39. {
  40. #ifdef CONFIG_IEEE1394_OUI_DB
  41. extern struct oui_list_struct {
  42. int oui;
  43. char *name;
  44. } oui_list[];
  45. int i;
  46. for (i = 0; oui_list[i].name; i++)
  47. if (oui_list[i].oui == oui)
  48. return oui_list[i].name;
  49. #endif
  50. return NULL;
  51. }
  52. static int nodemgr_bus_read(struct csr1212_csr *csr, u64 addr, u16 length,
  53. void *buffer, void *__ci)
  54. {
  55. struct nodemgr_csr_info *ci = (struct nodemgr_csr_info*)__ci;
  56. int i, ret = 0;
  57. for (i = 0; i < 3; i++) {
  58. ret = hpsb_read(ci->host, ci->nodeid, ci->generation, addr,
  59. buffer, length);
  60. if (!ret)
  61. break;
  62. if (msleep_interruptible(334))
  63. return -EINTR;
  64. }
  65. return ret;
  66. }
  67. static int nodemgr_get_max_rom(quadlet_t *bus_info_data, void *__ci)
  68. {
  69. return (CSR1212_BE32_TO_CPU(bus_info_data[2]) >> 8) & 0x3;
  70. }
  71. static struct csr1212_bus_ops nodemgr_csr_ops = {
  72. .bus_read = nodemgr_bus_read,
  73. .get_max_rom = nodemgr_get_max_rom
  74. };
  75. /*
  76. * Basically what we do here is start off retrieving the bus_info block.
  77. * From there will fill in some info about the node, verify it is of IEEE
  78. * 1394 type, and that the crc checks out ok. After that we start off with
  79. * the root directory, and subdirectories. To do this, we retrieve the
  80. * quadlet header for a directory, find out the length, and retrieve the
  81. * complete directory entry (be it a leaf or a directory). We then process
  82. * it and add the info to our structure for that particular node.
  83. *
  84. * We verify CRC's along the way for each directory/block/leaf. The entire
  85. * node structure is generic, and simply stores the information in a way
  86. * that's easy to parse by the protocol interface.
  87. */
  88. /*
  89. * The nodemgr relies heavily on the Driver Model for device callbacks and
  90. * driver/device mappings. The old nodemgr used to handle all this itself,
  91. * but now we are much simpler because of the LDM.
  92. */
  93. static DECLARE_MUTEX(nodemgr_serialize);
  94. struct host_info {
  95. struct hpsb_host *host;
  96. struct list_head list;
  97. struct completion exited;
  98. struct semaphore reset_sem;
  99. int pid;
  100. char daemon_name[15];
  101. int kill_me;
  102. };
  103. static int nodemgr_bus_match(struct device * dev, struct device_driver * drv);
  104. static int nodemgr_hotplug(struct class_device *cdev, char **envp, int num_envp,
  105. char *buffer, int buffer_size);
  106. static void nodemgr_resume_ne(struct node_entry *ne);
  107. static void nodemgr_remove_ne(struct node_entry *ne);
  108. static struct node_entry *find_entry_by_guid(u64 guid);
  109. struct bus_type ieee1394_bus_type = {
  110. .name = "ieee1394",
  111. .match = nodemgr_bus_match,
  112. };
  113. static void host_cls_release(struct class_device *class_dev)
  114. {
  115. put_device(&container_of((class_dev), struct hpsb_host, class_dev)->device);
  116. }
  117. struct class hpsb_host_class = {
  118. .name = "ieee1394_host",
  119. .release = host_cls_release,
  120. };
  121. static void ne_cls_release(struct class_device *class_dev)
  122. {
  123. put_device(&container_of((class_dev), struct node_entry, class_dev)->device);
  124. }
  125. static struct class nodemgr_ne_class = {
  126. .name = "ieee1394_node",
  127. .release = ne_cls_release,
  128. };
  129. static void ud_cls_release(struct class_device *class_dev)
  130. {
  131. put_device(&container_of((class_dev), struct unit_directory, class_dev)->device);
  132. }
  133. /* The name here is only so that unit directory hotplug works with old
  134. * style hotplug, which only ever did unit directories anyway. */
  135. static struct class nodemgr_ud_class = {
  136. .name = "ieee1394",
  137. .release = ud_cls_release,
  138. .hotplug = nodemgr_hotplug,
  139. };
  140. static struct hpsb_highlevel nodemgr_highlevel;
  141. static void nodemgr_release_ud(struct device *dev)
  142. {
  143. struct unit_directory *ud = container_of(dev, struct unit_directory, device);
  144. if (ud->vendor_name_kv)
  145. csr1212_release_keyval(ud->vendor_name_kv);
  146. if (ud->model_name_kv)
  147. csr1212_release_keyval(ud->model_name_kv);
  148. kfree(ud);
  149. }
  150. static void nodemgr_release_ne(struct device *dev)
  151. {
  152. struct node_entry *ne = container_of(dev, struct node_entry, device);
  153. if (ne->vendor_name_kv)
  154. csr1212_release_keyval(ne->vendor_name_kv);
  155. kfree(ne);
  156. }
  157. static void nodemgr_release_host(struct device *dev)
  158. {
  159. struct hpsb_host *host = container_of(dev, struct hpsb_host, device);
  160. csr1212_destroy_csr(host->csr.rom);
  161. kfree(host);
  162. }
  163. static int nodemgr_ud_platform_data;
  164. static struct device nodemgr_dev_template_ud = {
  165. .bus = &ieee1394_bus_type,
  166. .release = nodemgr_release_ud,
  167. .platform_data = &nodemgr_ud_platform_data,
  168. };
  169. static struct device nodemgr_dev_template_ne = {
  170. .bus = &ieee1394_bus_type,
  171. .release = nodemgr_release_ne,
  172. };
  173. struct device nodemgr_dev_template_host = {
  174. .bus = &ieee1394_bus_type,
  175. .release = nodemgr_release_host,
  176. };
  177. #define fw_attr(class, class_type, field, type, format_string) \
  178. static ssize_t fw_show_##class##_##field (struct device *dev, char *buf)\
  179. { \
  180. class_type *class; \
  181. class = container_of(dev, class_type, device); \
  182. return sprintf(buf, format_string, (type)class->field); \
  183. } \
  184. static struct device_attribute dev_attr_##class##_##field = { \
  185. .attr = {.name = __stringify(field), .mode = S_IRUGO }, \
  186. .show = fw_show_##class##_##field, \
  187. };
  188. #define fw_attr_td(class, class_type, td_kv) \
  189. static ssize_t fw_show_##class##_##td_kv (struct device *dev, char *buf)\
  190. { \
  191. int len; \
  192. class_type *class = container_of(dev, class_type, device); \
  193. len = (class->td_kv->value.leaf.len - 2) * sizeof(quadlet_t); \
  194. memcpy(buf, \
  195. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(class->td_kv), \
  196. len); \
  197. while ((buf + len - 1) == '\0') \
  198. len--; \
  199. buf[len++] = '\n'; \
  200. buf[len] = '\0'; \
  201. return len; \
  202. } \
  203. static struct device_attribute dev_attr_##class##_##td_kv = { \
  204. .attr = {.name = __stringify(td_kv), .mode = S_IRUGO }, \
  205. .show = fw_show_##class##_##td_kv, \
  206. };
  207. #define fw_drv_attr(field, type, format_string) \
  208. static ssize_t fw_drv_show_##field (struct device_driver *drv, char *buf) \
  209. { \
  210. struct hpsb_protocol_driver *driver; \
  211. driver = container_of(drv, struct hpsb_protocol_driver, driver); \
  212. return sprintf(buf, format_string, (type)driver->field);\
  213. } \
  214. static struct driver_attribute driver_attr_drv_##field = { \
  215. .attr = {.name = __stringify(field), .mode = S_IRUGO }, \
  216. .show = fw_drv_show_##field, \
  217. };
  218. static ssize_t fw_show_ne_bus_options(struct device *dev, char *buf)
  219. {
  220. struct node_entry *ne = container_of(dev, struct node_entry, device);
  221. return sprintf(buf, "IRMC(%d) CMC(%d) ISC(%d) BMC(%d) PMC(%d) GEN(%d) "
  222. "LSPD(%d) MAX_REC(%d) MAX_ROM(%d) CYC_CLK_ACC(%d)\n",
  223. ne->busopt.irmc,
  224. ne->busopt.cmc, ne->busopt.isc, ne->busopt.bmc,
  225. ne->busopt.pmc, ne->busopt.generation, ne->busopt.lnkspd,
  226. ne->busopt.max_rec,
  227. ne->busopt.max_rom,
  228. ne->busopt.cyc_clk_acc);
  229. }
  230. static DEVICE_ATTR(bus_options,S_IRUGO,fw_show_ne_bus_options,NULL);
  231. static ssize_t fw_show_ne_tlabels_free(struct device *dev, char *buf)
  232. {
  233. struct node_entry *ne = container_of(dev, struct node_entry, device);
  234. return sprintf(buf, "%d\n", atomic_read(&ne->tpool->count.count) + 1);
  235. }
  236. static DEVICE_ATTR(tlabels_free,S_IRUGO,fw_show_ne_tlabels_free,NULL);
  237. static ssize_t fw_show_ne_tlabels_allocations(struct device *dev, char *buf)
  238. {
  239. struct node_entry *ne = container_of(dev, struct node_entry, device);
  240. return sprintf(buf, "%u\n", ne->tpool->allocations);
  241. }
  242. static DEVICE_ATTR(tlabels_allocations,S_IRUGO,fw_show_ne_tlabels_allocations,NULL);
  243. static ssize_t fw_show_ne_tlabels_mask(struct device *dev, char *buf)
  244. {
  245. struct node_entry *ne = container_of(dev, struct node_entry, device);
  246. #if (BITS_PER_LONG <= 32)
  247. return sprintf(buf, "0x%08lx%08lx\n", ne->tpool->pool[0], ne->tpool->pool[1]);
  248. #else
  249. return sprintf(buf, "0x%016lx\n", ne->tpool->pool[0]);
  250. #endif
  251. }
  252. static DEVICE_ATTR(tlabels_mask, S_IRUGO, fw_show_ne_tlabels_mask, NULL);
  253. static ssize_t fw_set_ignore_driver(struct device *dev, const char *buf, size_t count)
  254. {
  255. struct unit_directory *ud = container_of(dev, struct unit_directory, device);
  256. int state = simple_strtoul(buf, NULL, 10);
  257. if (state == 1) {
  258. down_write(&dev->bus->subsys.rwsem);
  259. device_release_driver(dev);
  260. ud->ignore_driver = 1;
  261. up_write(&dev->bus->subsys.rwsem);
  262. } else if (!state)
  263. ud->ignore_driver = 0;
  264. return count;
  265. }
  266. static ssize_t fw_get_ignore_driver(struct device *dev, char *buf)
  267. {
  268. struct unit_directory *ud = container_of(dev, struct unit_directory, device);
  269. return sprintf(buf, "%d\n", ud->ignore_driver);
  270. }
  271. static DEVICE_ATTR(ignore_driver, S_IWUSR | S_IRUGO, fw_get_ignore_driver, fw_set_ignore_driver);
  272. static ssize_t fw_set_destroy_node(struct bus_type *bus, const char *buf, size_t count)
  273. {
  274. struct node_entry *ne;
  275. u64 guid = (u64)simple_strtoull(buf, NULL, 16);
  276. ne = find_entry_by_guid(guid);
  277. if (ne == NULL || !ne->in_limbo)
  278. return -EINVAL;
  279. nodemgr_remove_ne(ne);
  280. return count;
  281. }
  282. static ssize_t fw_get_destroy_node(struct bus_type *bus, char *buf)
  283. {
  284. return sprintf(buf, "You can destroy in_limbo nodes by writing their GUID to this file\n");
  285. }
  286. static BUS_ATTR(destroy_node, S_IWUSR | S_IRUGO, fw_get_destroy_node, fw_set_destroy_node);
  287. static int nodemgr_rescan_bus_thread(void *__unused)
  288. {
  289. /* No userlevel access needed */
  290. daemonize("kfwrescan");
  291. bus_rescan_devices(&ieee1394_bus_type);
  292. return 0;
  293. }
  294. static ssize_t fw_set_rescan(struct bus_type *bus, const char *buf, size_t count)
  295. {
  296. int state = simple_strtoul(buf, NULL, 10);
  297. /* Don't wait for this, or care about errors. Root could do
  298. * something stupid and spawn this a lot of times, but that's
  299. * root's fault. */
  300. if (state == 1)
  301. kernel_thread(nodemgr_rescan_bus_thread, NULL, CLONE_KERNEL);
  302. return count;
  303. }
  304. static ssize_t fw_get_rescan(struct bus_type *bus, char *buf)
  305. {
  306. return sprintf(buf, "You can force a rescan of the bus for "
  307. "drivers by writing a 1 to this file\n");
  308. }
  309. static BUS_ATTR(rescan, S_IWUSR | S_IRUGO, fw_get_rescan, fw_set_rescan);
  310. static ssize_t fw_set_ignore_drivers(struct bus_type *bus, const char *buf, size_t count)
  311. {
  312. int state = simple_strtoul(buf, NULL, 10);
  313. if (state == 1)
  314. ignore_drivers = 1;
  315. else if (!state)
  316. ignore_drivers = 0;
  317. return count;
  318. }
  319. static ssize_t fw_get_ignore_drivers(struct bus_type *bus, char *buf)
  320. {
  321. return sprintf(buf, "%d\n", ignore_drivers);
  322. }
  323. static BUS_ATTR(ignore_drivers, S_IWUSR | S_IRUGO, fw_get_ignore_drivers, fw_set_ignore_drivers);
  324. struct bus_attribute *const fw_bus_attrs[] = {
  325. &bus_attr_destroy_node,
  326. &bus_attr_rescan,
  327. &bus_attr_ignore_drivers,
  328. NULL
  329. };
  330. fw_attr(ne, struct node_entry, capabilities, unsigned int, "0x%06x\n")
  331. fw_attr(ne, struct node_entry, nodeid, unsigned int, "0x%04x\n")
  332. fw_attr(ne, struct node_entry, vendor_id, unsigned int, "0x%06x\n")
  333. fw_attr_td(ne, struct node_entry, vendor_name_kv)
  334. fw_attr(ne, struct node_entry, vendor_oui, const char *, "%s\n")
  335. fw_attr(ne, struct node_entry, guid, unsigned long long, "0x%016Lx\n")
  336. fw_attr(ne, struct node_entry, guid_vendor_id, unsigned int, "0x%06x\n")
  337. fw_attr(ne, struct node_entry, guid_vendor_oui, const char *, "%s\n")
  338. fw_attr(ne, struct node_entry, in_limbo, int, "%d\n");
  339. static struct device_attribute *const fw_ne_attrs[] = {
  340. &dev_attr_ne_guid,
  341. &dev_attr_ne_guid_vendor_id,
  342. &dev_attr_ne_capabilities,
  343. &dev_attr_ne_vendor_id,
  344. &dev_attr_ne_nodeid,
  345. &dev_attr_bus_options,
  346. &dev_attr_tlabels_free,
  347. &dev_attr_tlabels_allocations,
  348. &dev_attr_tlabels_mask,
  349. };
  350. fw_attr(ud, struct unit_directory, address, unsigned long long, "0x%016Lx\n")
  351. fw_attr(ud, struct unit_directory, length, int, "%d\n")
  352. /* These are all dependent on the value being provided */
  353. fw_attr(ud, struct unit_directory, vendor_id, unsigned int, "0x%06x\n")
  354. fw_attr(ud, struct unit_directory, model_id, unsigned int, "0x%06x\n")
  355. fw_attr(ud, struct unit_directory, specifier_id, unsigned int, "0x%06x\n")
  356. fw_attr(ud, struct unit_directory, version, unsigned int, "0x%06x\n")
  357. fw_attr_td(ud, struct unit_directory, vendor_name_kv)
  358. fw_attr(ud, struct unit_directory, vendor_oui, const char *, "%s\n")
  359. fw_attr_td(ud, struct unit_directory, model_name_kv)
  360. static struct device_attribute *const fw_ud_attrs[] = {
  361. &dev_attr_ud_address,
  362. &dev_attr_ud_length,
  363. &dev_attr_ignore_driver,
  364. };
  365. fw_attr(host, struct hpsb_host, node_count, int, "%d\n")
  366. fw_attr(host, struct hpsb_host, selfid_count, int, "%d\n")
  367. fw_attr(host, struct hpsb_host, nodes_active, int, "%d\n")
  368. fw_attr(host, struct hpsb_host, in_bus_reset, int, "%d\n")
  369. fw_attr(host, struct hpsb_host, is_root, int, "%d\n")
  370. fw_attr(host, struct hpsb_host, is_cycmst, int, "%d\n")
  371. fw_attr(host, struct hpsb_host, is_irm, int, "%d\n")
  372. fw_attr(host, struct hpsb_host, is_busmgr, int, "%d\n")
  373. static struct device_attribute *const fw_host_attrs[] = {
  374. &dev_attr_host_node_count,
  375. &dev_attr_host_selfid_count,
  376. &dev_attr_host_nodes_active,
  377. &dev_attr_host_in_bus_reset,
  378. &dev_attr_host_is_root,
  379. &dev_attr_host_is_cycmst,
  380. &dev_attr_host_is_irm,
  381. &dev_attr_host_is_busmgr,
  382. };
  383. static ssize_t fw_show_drv_device_ids(struct device_driver *drv, char *buf)
  384. {
  385. struct hpsb_protocol_driver *driver;
  386. struct ieee1394_device_id *id;
  387. int length = 0;
  388. char *scratch = buf;
  389. driver = container_of(drv, struct hpsb_protocol_driver, driver);
  390. for (id = driver->id_table; id->match_flags != 0; id++) {
  391. int need_coma = 0;
  392. if (id->match_flags & IEEE1394_MATCH_VENDOR_ID) {
  393. length += sprintf(scratch, "vendor_id=0x%06x", id->vendor_id);
  394. scratch = buf + length;
  395. need_coma++;
  396. }
  397. if (id->match_flags & IEEE1394_MATCH_MODEL_ID) {
  398. length += sprintf(scratch, "%smodel_id=0x%06x",
  399. need_coma++ ? "," : "",
  400. id->model_id);
  401. scratch = buf + length;
  402. }
  403. if (id->match_flags & IEEE1394_MATCH_SPECIFIER_ID) {
  404. length += sprintf(scratch, "%sspecifier_id=0x%06x",
  405. need_coma++ ? "," : "",
  406. id->specifier_id);
  407. scratch = buf + length;
  408. }
  409. if (id->match_flags & IEEE1394_MATCH_VERSION) {
  410. length += sprintf(scratch, "%sversion=0x%06x",
  411. need_coma++ ? "," : "",
  412. id->version);
  413. scratch = buf + length;
  414. }
  415. if (need_coma) {
  416. *scratch++ = '\n';
  417. length++;
  418. }
  419. }
  420. return length;
  421. }
  422. static DRIVER_ATTR(device_ids,S_IRUGO,fw_show_drv_device_ids,NULL);
  423. fw_drv_attr(name, const char *, "%s\n")
  424. static struct driver_attribute *const fw_drv_attrs[] = {
  425. &driver_attr_drv_name,
  426. &driver_attr_device_ids,
  427. };
  428. static void nodemgr_create_drv_files(struct hpsb_protocol_driver *driver)
  429. {
  430. struct device_driver *drv = &driver->driver;
  431. int i;
  432. for (i = 0; i < ARRAY_SIZE(fw_drv_attrs); i++)
  433. driver_create_file(drv, fw_drv_attrs[i]);
  434. }
  435. static void nodemgr_remove_drv_files(struct hpsb_protocol_driver *driver)
  436. {
  437. struct device_driver *drv = &driver->driver;
  438. int i;
  439. for (i = 0; i < ARRAY_SIZE(fw_drv_attrs); i++)
  440. driver_remove_file(drv, fw_drv_attrs[i]);
  441. }
  442. static void nodemgr_create_ne_dev_files(struct node_entry *ne)
  443. {
  444. struct device *dev = &ne->device;
  445. int i;
  446. for (i = 0; i < ARRAY_SIZE(fw_ne_attrs); i++)
  447. device_create_file(dev, fw_ne_attrs[i]);
  448. }
  449. static void nodemgr_create_host_dev_files(struct hpsb_host *host)
  450. {
  451. struct device *dev = &host->device;
  452. int i;
  453. for (i = 0; i < ARRAY_SIZE(fw_host_attrs); i++)
  454. device_create_file(dev, fw_host_attrs[i]);
  455. }
  456. static struct node_entry *find_entry_by_nodeid(struct hpsb_host *host, nodeid_t nodeid);
  457. static void nodemgr_update_host_dev_links(struct hpsb_host *host)
  458. {
  459. struct device *dev = &host->device;
  460. struct node_entry *ne;
  461. sysfs_remove_link(&dev->kobj, "irm_id");
  462. sysfs_remove_link(&dev->kobj, "busmgr_id");
  463. sysfs_remove_link(&dev->kobj, "host_id");
  464. if ((ne = find_entry_by_nodeid(host, host->irm_id)))
  465. sysfs_create_link(&dev->kobj, &ne->device.kobj, "irm_id");
  466. if ((ne = find_entry_by_nodeid(host, host->busmgr_id)))
  467. sysfs_create_link(&dev->kobj, &ne->device.kobj, "busmgr_id");
  468. if ((ne = find_entry_by_nodeid(host, host->node_id)))
  469. sysfs_create_link(&dev->kobj, &ne->device.kobj, "host_id");
  470. }
  471. static void nodemgr_create_ud_dev_files(struct unit_directory *ud)
  472. {
  473. struct device *dev = &ud->device;
  474. int i;
  475. for (i = 0; i < ARRAY_SIZE(fw_ud_attrs); i++)
  476. device_create_file(dev, fw_ud_attrs[i]);
  477. if (ud->flags & UNIT_DIRECTORY_SPECIFIER_ID)
  478. device_create_file(dev, &dev_attr_ud_specifier_id);
  479. if (ud->flags & UNIT_DIRECTORY_VERSION)
  480. device_create_file(dev, &dev_attr_ud_version);
  481. if (ud->flags & UNIT_DIRECTORY_VENDOR_ID) {
  482. device_create_file(dev, &dev_attr_ud_vendor_id);
  483. if (ud->vendor_name_kv)
  484. device_create_file(dev, &dev_attr_ud_vendor_name_kv);
  485. }
  486. if (ud->flags & UNIT_DIRECTORY_MODEL_ID) {
  487. device_create_file(dev, &dev_attr_ud_model_id);
  488. if (ud->model_name_kv)
  489. device_create_file(dev, &dev_attr_ud_model_name_kv);
  490. }
  491. }
  492. static int nodemgr_bus_match(struct device * dev, struct device_driver * drv)
  493. {
  494. struct hpsb_protocol_driver *driver;
  495. struct unit_directory *ud;
  496. struct ieee1394_device_id *id;
  497. /* We only match unit directories */
  498. if (dev->platform_data != &nodemgr_ud_platform_data)
  499. return 0;
  500. ud = container_of(dev, struct unit_directory, device);
  501. driver = container_of(drv, struct hpsb_protocol_driver, driver);
  502. if (ud->ne->in_limbo || ud->ignore_driver)
  503. return 0;
  504. for (id = driver->id_table; id->match_flags != 0; id++) {
  505. if ((id->match_flags & IEEE1394_MATCH_VENDOR_ID) &&
  506. id->vendor_id != ud->vendor_id)
  507. continue;
  508. if ((id->match_flags & IEEE1394_MATCH_MODEL_ID) &&
  509. id->model_id != ud->model_id)
  510. continue;
  511. if ((id->match_flags & IEEE1394_MATCH_SPECIFIER_ID) &&
  512. id->specifier_id != ud->specifier_id)
  513. continue;
  514. if ((id->match_flags & IEEE1394_MATCH_VERSION) &&
  515. id->version != ud->version)
  516. continue;
  517. return 1;
  518. }
  519. return 0;
  520. }
  521. static void nodemgr_remove_uds(struct node_entry *ne)
  522. {
  523. struct class_device *cdev, *next;
  524. struct unit_directory *ud;
  525. list_for_each_entry_safe(cdev, next, &nodemgr_ud_class.children, node) {
  526. ud = container_of(cdev, struct unit_directory, class_dev);
  527. if (ud->ne != ne)
  528. continue;
  529. class_device_unregister(&ud->class_dev);
  530. device_unregister(&ud->device);
  531. }
  532. }
  533. static void nodemgr_remove_ne(struct node_entry *ne)
  534. {
  535. struct device *dev = &ne->device;
  536. dev = get_device(&ne->device);
  537. if (!dev)
  538. return;
  539. HPSB_DEBUG("Node removed: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
  540. NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
  541. nodemgr_remove_uds(ne);
  542. class_device_unregister(&ne->class_dev);
  543. device_unregister(dev);
  544. put_device(dev);
  545. }
  546. static void nodemgr_remove_host_dev(struct device *dev)
  547. {
  548. struct device *ne_dev, *next;
  549. list_for_each_entry_safe(ne_dev, next, &dev->children, node)
  550. nodemgr_remove_ne(container_of(ne_dev, struct node_entry, device));
  551. sysfs_remove_link(&dev->kobj, "irm_id");
  552. sysfs_remove_link(&dev->kobj, "busmgr_id");
  553. sysfs_remove_link(&dev->kobj, "host_id");
  554. }
  555. static void nodemgr_update_bus_options(struct node_entry *ne)
  556. {
  557. #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
  558. static const u16 mr[] = { 4, 64, 1024, 0};
  559. #endif
  560. quadlet_t busoptions = be32_to_cpu(ne->csr->bus_info_data[2]);
  561. ne->busopt.irmc = (busoptions >> 31) & 1;
  562. ne->busopt.cmc = (busoptions >> 30) & 1;
  563. ne->busopt.isc = (busoptions >> 29) & 1;
  564. ne->busopt.bmc = (busoptions >> 28) & 1;
  565. ne->busopt.pmc = (busoptions >> 27) & 1;
  566. ne->busopt.cyc_clk_acc = (busoptions >> 16) & 0xff;
  567. ne->busopt.max_rec = 1 << (((busoptions >> 12) & 0xf) + 1);
  568. ne->busopt.max_rom = (busoptions >> 8) & 0x3;
  569. ne->busopt.generation = (busoptions >> 4) & 0xf;
  570. ne->busopt.lnkspd = busoptions & 0x7;
  571. HPSB_VERBOSE("NodeMgr: raw=0x%08x irmc=%d cmc=%d isc=%d bmc=%d pmc=%d "
  572. "cyc_clk_acc=%d max_rec=%d max_rom=%d gen=%d lspd=%d",
  573. busoptions, ne->busopt.irmc, ne->busopt.cmc,
  574. ne->busopt.isc, ne->busopt.bmc, ne->busopt.pmc,
  575. ne->busopt.cyc_clk_acc, ne->busopt.max_rec,
  576. mr[ne->busopt.max_rom],
  577. ne->busopt.generation, ne->busopt.lnkspd);
  578. }
  579. static struct node_entry *nodemgr_create_node(octlet_t guid, struct csr1212_csr *csr,
  580. struct host_info *hi, nodeid_t nodeid,
  581. unsigned int generation)
  582. {
  583. struct hpsb_host *host = hi->host;
  584. struct node_entry *ne;
  585. ne = kmalloc(sizeof(struct node_entry), GFP_KERNEL);
  586. if (!ne) return NULL;
  587. memset(ne, 0, sizeof(struct node_entry));
  588. ne->tpool = &host->tpool[nodeid & NODE_MASK];
  589. ne->host = host;
  590. ne->nodeid = nodeid;
  591. ne->generation = generation;
  592. ne->needs_probe = 1;
  593. ne->guid = guid;
  594. ne->guid_vendor_id = (guid >> 40) & 0xffffff;
  595. ne->guid_vendor_oui = nodemgr_find_oui_name(ne->guid_vendor_id);
  596. ne->csr = csr;
  597. memcpy(&ne->device, &nodemgr_dev_template_ne,
  598. sizeof(ne->device));
  599. ne->device.parent = &host->device;
  600. snprintf(ne->device.bus_id, BUS_ID_SIZE, "%016Lx",
  601. (unsigned long long)(ne->guid));
  602. ne->class_dev.dev = &ne->device;
  603. ne->class_dev.class = &nodemgr_ne_class;
  604. snprintf(ne->class_dev.class_id, BUS_ID_SIZE, "%016Lx",
  605. (unsigned long long)(ne->guid));
  606. device_register(&ne->device);
  607. class_device_register(&ne->class_dev);
  608. get_device(&ne->device);
  609. if (ne->guid_vendor_oui)
  610. device_create_file(&ne->device, &dev_attr_ne_guid_vendor_oui);
  611. nodemgr_create_ne_dev_files(ne);
  612. nodemgr_update_bus_options(ne);
  613. HPSB_DEBUG("%s added: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
  614. (host->node_id == nodeid) ? "Host" : "Node",
  615. NODE_BUS_ARGS(host, nodeid), (unsigned long long)guid);
  616. return ne;
  617. }
  618. static struct node_entry *find_entry_by_guid(u64 guid)
  619. {
  620. struct class *class = &nodemgr_ne_class;
  621. struct class_device *cdev;
  622. struct node_entry *ne, *ret_ne = NULL;
  623. down_read(&class->subsys.rwsem);
  624. list_for_each_entry(cdev, &class->children, node) {
  625. ne = container_of(cdev, struct node_entry, class_dev);
  626. if (ne->guid == guid) {
  627. ret_ne = ne;
  628. break;
  629. }
  630. }
  631. up_read(&class->subsys.rwsem);
  632. return ret_ne;
  633. }
  634. static struct node_entry *find_entry_by_nodeid(struct hpsb_host *host, nodeid_t nodeid)
  635. {
  636. struct class *class = &nodemgr_ne_class;
  637. struct class_device *cdev;
  638. struct node_entry *ne, *ret_ne = NULL;
  639. down_read(&class->subsys.rwsem);
  640. list_for_each_entry(cdev, &class->children, node) {
  641. ne = container_of(cdev, struct node_entry, class_dev);
  642. if (ne->host == host && ne->nodeid == nodeid) {
  643. ret_ne = ne;
  644. break;
  645. }
  646. }
  647. up_read(&class->subsys.rwsem);
  648. return ret_ne;
  649. }
  650. static void nodemgr_register_device(struct node_entry *ne,
  651. struct unit_directory *ud, struct device *parent)
  652. {
  653. memcpy(&ud->device, &nodemgr_dev_template_ud,
  654. sizeof(ud->device));
  655. ud->device.parent = parent;
  656. snprintf(ud->device.bus_id, BUS_ID_SIZE, "%s-%u",
  657. ne->device.bus_id, ud->id);
  658. ud->class_dev.dev = &ud->device;
  659. ud->class_dev.class = &nodemgr_ud_class;
  660. snprintf(ud->class_dev.class_id, BUS_ID_SIZE, "%s-%u",
  661. ne->device.bus_id, ud->id);
  662. device_register(&ud->device);
  663. class_device_register(&ud->class_dev);
  664. get_device(&ud->device);
  665. if (ud->vendor_oui)
  666. device_create_file(&ud->device, &dev_attr_ud_vendor_oui);
  667. nodemgr_create_ud_dev_files(ud);
  668. }
  669. /* This implementation currently only scans the config rom and its
  670. * immediate unit directories looking for software_id and
  671. * software_version entries, in order to get driver autoloading working. */
  672. static struct unit_directory *nodemgr_process_unit_directory
  673. (struct host_info *hi, struct node_entry *ne, struct csr1212_keyval *ud_kv,
  674. unsigned int *id, struct unit_directory *parent)
  675. {
  676. struct unit_directory *ud;
  677. struct unit_directory *ud_child = NULL;
  678. struct csr1212_dentry *dentry;
  679. struct csr1212_keyval *kv;
  680. u8 last_key_id = 0;
  681. ud = kmalloc(sizeof(struct unit_directory), GFP_KERNEL);
  682. if (!ud)
  683. goto unit_directory_error;
  684. memset (ud, 0, sizeof(struct unit_directory));
  685. ud->ne = ne;
  686. ud->ignore_driver = ignore_drivers;
  687. ud->address = ud_kv->offset + CSR1212_CONFIG_ROM_SPACE_BASE;
  688. ud->ud_kv = ud_kv;
  689. ud->id = (*id)++;
  690. csr1212_for_each_dir_entry(ne->csr, kv, ud_kv, dentry) {
  691. switch (kv->key.id) {
  692. case CSR1212_KV_ID_VENDOR:
  693. if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
  694. ud->vendor_id = kv->value.immediate;
  695. ud->flags |= UNIT_DIRECTORY_VENDOR_ID;
  696. if (ud->vendor_id)
  697. ud->vendor_oui = nodemgr_find_oui_name(ud->vendor_id);
  698. }
  699. break;
  700. case CSR1212_KV_ID_MODEL:
  701. ud->model_id = kv->value.immediate;
  702. ud->flags |= UNIT_DIRECTORY_MODEL_ID;
  703. break;
  704. case CSR1212_KV_ID_SPECIFIER_ID:
  705. ud->specifier_id = kv->value.immediate;
  706. ud->flags |= UNIT_DIRECTORY_SPECIFIER_ID;
  707. break;
  708. case CSR1212_KV_ID_VERSION:
  709. ud->version = kv->value.immediate;
  710. ud->flags |= UNIT_DIRECTORY_VERSION;
  711. break;
  712. case CSR1212_KV_ID_DESCRIPTOR:
  713. if (kv->key.type == CSR1212_KV_TYPE_LEAF &&
  714. CSR1212_DESCRIPTOR_LEAF_TYPE(kv) == 0 &&
  715. CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) == 0 &&
  716. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) == 0 &&
  717. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) == 0 &&
  718. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) == 0) {
  719. switch (last_key_id) {
  720. case CSR1212_KV_ID_VENDOR:
  721. ud->vendor_name_kv = kv;
  722. csr1212_keep_keyval(kv);
  723. break;
  724. case CSR1212_KV_ID_MODEL:
  725. ud->model_name_kv = kv;
  726. csr1212_keep_keyval(kv);
  727. break;
  728. }
  729. } /* else if (kv->key.type == CSR1212_KV_TYPE_DIRECTORY) ... */
  730. break;
  731. case CSR1212_KV_ID_DEPENDENT_INFO:
  732. /* Logical Unit Number */
  733. if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
  734. if (ud->flags & UNIT_DIRECTORY_HAS_LUN) {
  735. ud_child = kmalloc(sizeof(struct unit_directory), GFP_KERNEL);
  736. if (!ud_child)
  737. goto unit_directory_error;
  738. memcpy(ud_child, ud, sizeof(struct unit_directory));
  739. nodemgr_register_device(ne, ud_child, &ne->device);
  740. ud_child = NULL;
  741. ud->id = (*id)++;
  742. }
  743. ud->lun = kv->value.immediate;
  744. ud->flags |= UNIT_DIRECTORY_HAS_LUN;
  745. /* Logical Unit Directory */
  746. } else if (kv->key.type == CSR1212_KV_TYPE_DIRECTORY) {
  747. /* This should really be done in SBP2 as this is
  748. * doing SBP2 specific parsing.
  749. */
  750. /* first register the parent unit */
  751. ud->flags |= UNIT_DIRECTORY_HAS_LUN_DIRECTORY;
  752. if (ud->device.bus != &ieee1394_bus_type)
  753. nodemgr_register_device(ne, ud, &ne->device);
  754. /* process the child unit */
  755. ud_child = nodemgr_process_unit_directory(hi, ne, kv, id, ud);
  756. if (ud_child == NULL)
  757. break;
  758. /* inherit unspecified values so hotplug picks it up */
  759. if ((ud->flags & UNIT_DIRECTORY_MODEL_ID) &&
  760. !(ud_child->flags & UNIT_DIRECTORY_MODEL_ID))
  761. {
  762. ud_child->flags |= UNIT_DIRECTORY_MODEL_ID;
  763. ud_child->model_id = ud->model_id;
  764. }
  765. if ((ud->flags & UNIT_DIRECTORY_SPECIFIER_ID) &&
  766. !(ud_child->flags & UNIT_DIRECTORY_SPECIFIER_ID))
  767. {
  768. ud_child->flags |= UNIT_DIRECTORY_SPECIFIER_ID;
  769. ud_child->specifier_id = ud->specifier_id;
  770. }
  771. if ((ud->flags & UNIT_DIRECTORY_VERSION) &&
  772. !(ud_child->flags & UNIT_DIRECTORY_VERSION))
  773. {
  774. ud_child->flags |= UNIT_DIRECTORY_VERSION;
  775. ud_child->version = ud->version;
  776. }
  777. /* register the child unit */
  778. ud_child->flags |= UNIT_DIRECTORY_LUN_DIRECTORY;
  779. nodemgr_register_device(ne, ud_child, &ud->device);
  780. }
  781. break;
  782. default:
  783. break;
  784. }
  785. last_key_id = kv->key.id;
  786. }
  787. /* do not process child units here and only if not already registered */
  788. if (!parent && ud->device.bus != &ieee1394_bus_type)
  789. nodemgr_register_device(ne, ud, &ne->device);
  790. return ud;
  791. unit_directory_error:
  792. kfree(ud);
  793. return NULL;
  794. }
  795. static void nodemgr_process_root_directory(struct host_info *hi, struct node_entry *ne)
  796. {
  797. unsigned int ud_id = 0;
  798. struct csr1212_dentry *dentry;
  799. struct csr1212_keyval *kv;
  800. u8 last_key_id = 0;
  801. ne->needs_probe = 0;
  802. csr1212_for_each_dir_entry(ne->csr, kv, ne->csr->root_kv, dentry) {
  803. switch (kv->key.id) {
  804. case CSR1212_KV_ID_VENDOR:
  805. ne->vendor_id = kv->value.immediate;
  806. if (ne->vendor_id)
  807. ne->vendor_oui = nodemgr_find_oui_name(ne->vendor_id);
  808. break;
  809. case CSR1212_KV_ID_NODE_CAPABILITIES:
  810. ne->capabilities = kv->value.immediate;
  811. break;
  812. case CSR1212_KV_ID_UNIT:
  813. nodemgr_process_unit_directory(hi, ne, kv, &ud_id, NULL);
  814. break;
  815. case CSR1212_KV_ID_DESCRIPTOR:
  816. if (last_key_id == CSR1212_KV_ID_VENDOR) {
  817. if (kv->key.type == CSR1212_KV_TYPE_LEAF &&
  818. CSR1212_DESCRIPTOR_LEAF_TYPE(kv) == 0 &&
  819. CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) == 0 &&
  820. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) == 0 &&
  821. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) == 0 &&
  822. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) == 0) {
  823. ne->vendor_name_kv = kv;
  824. csr1212_keep_keyval(kv);
  825. }
  826. }
  827. break;
  828. }
  829. last_key_id = kv->key.id;
  830. }
  831. if (ne->vendor_oui)
  832. device_create_file(&ne->device, &dev_attr_ne_vendor_oui);
  833. if (ne->vendor_name_kv)
  834. device_create_file(&ne->device, &dev_attr_ne_vendor_name_kv);
  835. }
  836. #ifdef CONFIG_HOTPLUG
  837. static int nodemgr_hotplug(struct class_device *cdev, char **envp, int num_envp,
  838. char *buffer, int buffer_size)
  839. {
  840. struct unit_directory *ud;
  841. int i = 0;
  842. int length = 0;
  843. if (!cdev)
  844. return -ENODEV;
  845. ud = container_of(cdev, struct unit_directory, class_dev);
  846. if (ud->ne->in_limbo || ud->ignore_driver)
  847. return -ENODEV;
  848. #define PUT_ENVP(fmt,val) \
  849. do { \
  850. int printed; \
  851. envp[i++] = buffer; \
  852. printed = snprintf(buffer, buffer_size - length, \
  853. fmt, val); \
  854. if ((buffer_size - (length+printed) <= 0) || (i >= num_envp)) \
  855. return -ENOMEM; \
  856. length += printed+1; \
  857. buffer += printed+1; \
  858. } while (0)
  859. PUT_ENVP("VENDOR_ID=%06x", ud->vendor_id);
  860. PUT_ENVP("MODEL_ID=%06x", ud->model_id);
  861. PUT_ENVP("GUID=%016Lx", (unsigned long long)ud->ne->guid);
  862. PUT_ENVP("SPECIFIER_ID=%06x", ud->specifier_id);
  863. PUT_ENVP("VERSION=%06x", ud->version);
  864. #undef PUT_ENVP
  865. envp[i] = NULL;
  866. return 0;
  867. }
  868. #else
  869. static int nodemgr_hotplug(struct class_device *cdev, char **envp, int num_envp,
  870. char *buffer, int buffer_size)
  871. {
  872. return -ENODEV;
  873. }
  874. #endif /* CONFIG_HOTPLUG */
  875. int hpsb_register_protocol(struct hpsb_protocol_driver *driver)
  876. {
  877. int ret;
  878. /* This will cause a probe for devices */
  879. ret = driver_register(&driver->driver);
  880. if (!ret)
  881. nodemgr_create_drv_files(driver);
  882. return ret;
  883. }
  884. void hpsb_unregister_protocol(struct hpsb_protocol_driver *driver)
  885. {
  886. nodemgr_remove_drv_files(driver);
  887. /* This will subsequently disconnect all devices that our driver
  888. * is attached to. */
  889. driver_unregister(&driver->driver);
  890. }
  891. /*
  892. * This function updates nodes that were present on the bus before the
  893. * reset and still are after the reset. The nodeid and the config rom
  894. * may have changed, and the drivers managing this device must be
  895. * informed that this device just went through a bus reset, to allow
  896. * the to take whatever actions required.
  897. */
  898. static void nodemgr_update_node(struct node_entry *ne, struct csr1212_csr *csr,
  899. struct host_info *hi, nodeid_t nodeid,
  900. unsigned int generation)
  901. {
  902. if (ne->nodeid != nodeid) {
  903. HPSB_DEBUG("Node changed: " NODE_BUS_FMT " -> " NODE_BUS_FMT,
  904. NODE_BUS_ARGS(ne->host, ne->nodeid),
  905. NODE_BUS_ARGS(ne->host, nodeid));
  906. ne->nodeid = nodeid;
  907. }
  908. if (ne->busopt.generation != ((be32_to_cpu(csr->bus_info_data[2]) >> 4) & 0xf)) {
  909. kfree(ne->csr->private);
  910. csr1212_destroy_csr(ne->csr);
  911. ne->csr = csr;
  912. /* If the node's configrom generation has changed, we
  913. * unregister all the unit directories. */
  914. nodemgr_remove_uds(ne);
  915. nodemgr_update_bus_options(ne);
  916. /* Mark the node as new, so it gets re-probed */
  917. ne->needs_probe = 1;
  918. } else {
  919. /* old cache is valid, so update its generation */
  920. struct nodemgr_csr_info *ci = ne->csr->private;
  921. ci->generation = generation;
  922. /* free the partially filled now unneeded new cache */
  923. kfree(csr->private);
  924. csr1212_destroy_csr(csr);
  925. }
  926. if (ne->in_limbo)
  927. nodemgr_resume_ne(ne);
  928. /* Mark the node current */
  929. ne->generation = generation;
  930. }
  931. static void nodemgr_node_scan_one(struct host_info *hi,
  932. nodeid_t nodeid, int generation)
  933. {
  934. struct hpsb_host *host = hi->host;
  935. struct node_entry *ne;
  936. octlet_t guid;
  937. struct csr1212_csr *csr;
  938. struct nodemgr_csr_info *ci;
  939. ci = kmalloc(sizeof(struct nodemgr_csr_info), GFP_KERNEL);
  940. if (!ci)
  941. return;
  942. ci->host = host;
  943. ci->nodeid = nodeid;
  944. ci->generation = generation;
  945. /* We need to detect when the ConfigROM's generation has changed,
  946. * so we only update the node's info when it needs to be. */
  947. csr = csr1212_create_csr(&nodemgr_csr_ops, 5 * sizeof(quadlet_t), ci);
  948. if (!csr || csr1212_parse_csr(csr) != CSR1212_SUCCESS) {
  949. HPSB_ERR("Error parsing configrom for node " NODE_BUS_FMT,
  950. NODE_BUS_ARGS(host, nodeid));
  951. if (csr)
  952. csr1212_destroy_csr(csr);
  953. kfree(ci);
  954. return;
  955. }
  956. if (csr->bus_info_data[1] != IEEE1394_BUSID_MAGIC) {
  957. /* This isn't a 1394 device, but we let it slide. There
  958. * was a report of a device with broken firmware which
  959. * reported '2394' instead of '1394', which is obviously a
  960. * mistake. One would hope that a non-1394 device never
  961. * gets connected to Firewire bus. If someone does, we
  962. * shouldn't be held responsible, so we'll allow it with a
  963. * warning. */
  964. HPSB_WARN("Node " NODE_BUS_FMT " has invalid busID magic [0x%08x]",
  965. NODE_BUS_ARGS(host, nodeid), csr->bus_info_data[1]);
  966. }
  967. guid = ((u64)be32_to_cpu(csr->bus_info_data[3]) << 32) | be32_to_cpu(csr->bus_info_data[4]);
  968. ne = find_entry_by_guid(guid);
  969. if (ne && ne->host != host && ne->in_limbo) {
  970. /* Must have moved this device from one host to another */
  971. nodemgr_remove_ne(ne);
  972. ne = NULL;
  973. }
  974. if (!ne)
  975. nodemgr_create_node(guid, csr, hi, nodeid, generation);
  976. else
  977. nodemgr_update_node(ne, csr, hi, nodeid, generation);
  978. return;
  979. }
  980. static void nodemgr_node_scan(struct host_info *hi, int generation)
  981. {
  982. int count;
  983. struct hpsb_host *host = hi->host;
  984. struct selfid *sid = (struct selfid *)host->topology_map;
  985. nodeid_t nodeid = LOCAL_BUS;
  986. /* Scan each node on the bus */
  987. for (count = host->selfid_count; count; count--, sid++) {
  988. if (sid->extended)
  989. continue;
  990. if (!sid->link_active) {
  991. nodeid++;
  992. continue;
  993. }
  994. nodemgr_node_scan_one(hi, nodeid++, generation);
  995. }
  996. }
  997. static void nodemgr_suspend_ne(struct node_entry *ne)
  998. {
  999. struct class_device *cdev;
  1000. struct unit_directory *ud;
  1001. HPSB_DEBUG("Node suspended: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
  1002. NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
  1003. ne->in_limbo = 1;
  1004. device_create_file(&ne->device, &dev_attr_ne_in_limbo);
  1005. down_write(&ne->device.bus->subsys.rwsem);
  1006. list_for_each_entry(cdev, &nodemgr_ud_class.children, node) {
  1007. ud = container_of(cdev, struct unit_directory, class_dev);
  1008. if (ud->ne != ne)
  1009. continue;
  1010. if (ud->device.driver &&
  1011. (!ud->device.driver->suspend ||
  1012. ud->device.driver->suspend(&ud->device, PMSG_SUSPEND, 0)))
  1013. device_release_driver(&ud->device);
  1014. }
  1015. up_write(&ne->device.bus->subsys.rwsem);
  1016. }
  1017. static void nodemgr_resume_ne(struct node_entry *ne)
  1018. {
  1019. struct class_device *cdev;
  1020. struct unit_directory *ud;
  1021. ne->in_limbo = 0;
  1022. device_remove_file(&ne->device, &dev_attr_ne_in_limbo);
  1023. down_read(&ne->device.bus->subsys.rwsem);
  1024. list_for_each_entry(cdev, &nodemgr_ud_class.children, node) {
  1025. ud = container_of(cdev, struct unit_directory, class_dev);
  1026. if (ud->ne != ne)
  1027. continue;
  1028. if (ud->device.driver && ud->device.driver->resume)
  1029. ud->device.driver->resume(&ud->device, 0);
  1030. }
  1031. up_read(&ne->device.bus->subsys.rwsem);
  1032. HPSB_DEBUG("Node resumed: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
  1033. NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
  1034. }
  1035. static void nodemgr_update_pdrv(struct node_entry *ne)
  1036. {
  1037. struct unit_directory *ud;
  1038. struct hpsb_protocol_driver *pdrv;
  1039. struct class *class = &nodemgr_ud_class;
  1040. struct class_device *cdev;
  1041. down_read(&class->subsys.rwsem);
  1042. list_for_each_entry(cdev, &class->children, node) {
  1043. ud = container_of(cdev, struct unit_directory, class_dev);
  1044. if (ud->ne != ne || !ud->device.driver)
  1045. continue;
  1046. pdrv = container_of(ud->device.driver, struct hpsb_protocol_driver, driver);
  1047. if (pdrv->update && pdrv->update(ud)) {
  1048. down_write(&ud->device.bus->subsys.rwsem);
  1049. device_release_driver(&ud->device);
  1050. up_write(&ud->device.bus->subsys.rwsem);
  1051. }
  1052. }
  1053. up_read(&class->subsys.rwsem);
  1054. }
  1055. static void nodemgr_probe_ne(struct host_info *hi, struct node_entry *ne, int generation)
  1056. {
  1057. struct device *dev;
  1058. if (ne->host != hi->host || ne->in_limbo)
  1059. return;
  1060. dev = get_device(&ne->device);
  1061. if (!dev)
  1062. return;
  1063. /* If "needs_probe", then this is either a new or changed node we
  1064. * rescan totally. If the generation matches for an existing node
  1065. * (one that existed prior to the bus reset) we send update calls
  1066. * down to the drivers. Otherwise, this is a dead node and we
  1067. * suspend it. */
  1068. if (ne->needs_probe)
  1069. nodemgr_process_root_directory(hi, ne);
  1070. else if (ne->generation == generation)
  1071. nodemgr_update_pdrv(ne);
  1072. else
  1073. nodemgr_suspend_ne(ne);
  1074. put_device(dev);
  1075. }
  1076. static void nodemgr_node_probe(struct host_info *hi, int generation)
  1077. {
  1078. struct hpsb_host *host = hi->host;
  1079. struct class *class = &nodemgr_ne_class;
  1080. struct class_device *cdev;
  1081. /* Do some processing of the nodes we've probed. This pulls them
  1082. * into the sysfs layer if needed, and can result in processing of
  1083. * unit-directories, or just updating the node and it's
  1084. * unit-directories. */
  1085. down_read(&class->subsys.rwsem);
  1086. list_for_each_entry(cdev, &class->children, node)
  1087. nodemgr_probe_ne(hi, container_of(cdev, struct node_entry, class_dev), generation);
  1088. up_read(&class->subsys.rwsem);
  1089. /* If we had a bus reset while we were scanning the bus, it is
  1090. * possible that we did not probe all nodes. In that case, we
  1091. * skip the clean up for now, since we could remove nodes that
  1092. * were still on the bus. The bus reset increased hi->reset_sem,
  1093. * so there's a bus scan pending which will do the clean up
  1094. * eventually.
  1095. *
  1096. * Now let's tell the bus to rescan our devices. This may seem
  1097. * like overhead, but the driver-model core will only scan a
  1098. * device for a driver when either the device is added, or when a
  1099. * new driver is added. A bus reset is a good reason to rescan
  1100. * devices that were there before. For example, an sbp2 device
  1101. * may become available for login, if the host that held it was
  1102. * just removed. */
  1103. if (generation == get_hpsb_generation(host))
  1104. bus_rescan_devices(&ieee1394_bus_type);
  1105. return;
  1106. }
  1107. /* Because we are a 1394a-2000 compliant IRM, we need to inform all the other
  1108. * nodes of the broadcast channel. (Really we're only setting the validity
  1109. * bit). Other IRM responsibilities go in here as well. */
  1110. static int nodemgr_do_irm_duties(struct hpsb_host *host, int cycles)
  1111. {
  1112. quadlet_t bc;
  1113. /* if irm_id == -1 then there is no IRM on this bus */
  1114. if (!host->is_irm || host->irm_id == (nodeid_t)-1)
  1115. return 1;
  1116. host->csr.broadcast_channel |= 0x40000000; /* set validity bit */
  1117. bc = cpu_to_be32(host->csr.broadcast_channel);
  1118. hpsb_write(host, LOCAL_BUS | ALL_NODES, get_hpsb_generation(host),
  1119. (CSR_REGISTER_BASE | CSR_BROADCAST_CHANNEL),
  1120. &bc, sizeof(quadlet_t));
  1121. /* If there is no bus manager then we should set the root node's
  1122. * force_root bit to promote bus stability per the 1394
  1123. * spec. (8.4.2.6) */
  1124. if (host->busmgr_id == 0xffff && host->node_count > 1)
  1125. {
  1126. u16 root_node = host->node_count - 1;
  1127. struct node_entry *ne = find_entry_by_nodeid(host, root_node | LOCAL_BUS);
  1128. if (ne && ne->busopt.cmc)
  1129. hpsb_send_phy_config(host, root_node, -1);
  1130. else {
  1131. HPSB_DEBUG("The root node is not cycle master capable; "
  1132. "selecting a new root node and resetting...");
  1133. if (cycles >= 5) {
  1134. /* Oh screw it! Just leave the bus as it is */
  1135. HPSB_DEBUG("Stopping reset loop for IRM sanity");
  1136. return 1;
  1137. }
  1138. hpsb_send_phy_config(host, NODEID_TO_NODE(host->node_id), -1);
  1139. hpsb_reset_bus(host, LONG_RESET_FORCE_ROOT);
  1140. return 0;
  1141. }
  1142. }
  1143. return 1;
  1144. }
  1145. /* We need to ensure that if we are not the IRM, that the IRM node is capable of
  1146. * everything we can do, otherwise issue a bus reset and try to become the IRM
  1147. * ourselves. */
  1148. static int nodemgr_check_irm_capability(struct hpsb_host *host, int cycles)
  1149. {
  1150. quadlet_t bc;
  1151. int status;
  1152. if (hpsb_disable_irm || host->is_irm)
  1153. return 1;
  1154. status = hpsb_read(host, LOCAL_BUS | (host->irm_id),
  1155. get_hpsb_generation(host),
  1156. (CSR_REGISTER_BASE | CSR_BROADCAST_CHANNEL),
  1157. &bc, sizeof(quadlet_t));
  1158. if (status < 0 || !(be32_to_cpu(bc) & 0x80000000)) {
  1159. /* The current irm node does not have a valid BROADCAST_CHANNEL
  1160. * register and we do, so reset the bus with force_root set */
  1161. HPSB_DEBUG("Current remote IRM is not 1394a-2000 compliant, resetting...");
  1162. if (cycles >= 5) {
  1163. /* Oh screw it! Just leave the bus as it is */
  1164. HPSB_DEBUG("Stopping reset loop for IRM sanity");
  1165. return 1;
  1166. }
  1167. hpsb_send_phy_config(host, NODEID_TO_NODE(host->node_id), -1);
  1168. hpsb_reset_bus(host, LONG_RESET_FORCE_ROOT);
  1169. return 0;
  1170. }
  1171. return 1;
  1172. }
  1173. static int nodemgr_host_thread(void *__hi)
  1174. {
  1175. struct host_info *hi = (struct host_info *)__hi;
  1176. struct hpsb_host *host = hi->host;
  1177. int reset_cycles = 0;
  1178. /* No userlevel access needed */
  1179. daemonize(hi->daemon_name);
  1180. /* Setup our device-model entries */
  1181. nodemgr_create_host_dev_files(host);
  1182. /* Sit and wait for a signal to probe the nodes on the bus. This
  1183. * happens when we get a bus reset. */
  1184. while (1) {
  1185. unsigned int generation = 0;
  1186. int i;
  1187. if (down_interruptible(&hi->reset_sem) ||
  1188. down_interruptible(&nodemgr_serialize)) {
  1189. if (try_to_freeze(PF_FREEZE))
  1190. continue;
  1191. printk("NodeMgr: received unexpected signal?!\n" );
  1192. break;
  1193. }
  1194. if (hi->kill_me) {
  1195. up(&nodemgr_serialize);
  1196. break;
  1197. }
  1198. /* Pause for 1/4 second in 1/16 second intervals,
  1199. * to make sure things settle down. */
  1200. for (i = 0; i < 4 ; i++) {
  1201. set_current_state(TASK_INTERRUPTIBLE);
  1202. if (msleep_interruptible(63)) {
  1203. up(&nodemgr_serialize);
  1204. goto caught_signal;
  1205. }
  1206. /* Now get the generation in which the node ID's we collect
  1207. * are valid. During the bus scan we will use this generation
  1208. * for the read transactions, so that if another reset occurs
  1209. * during the scan the transactions will fail instead of
  1210. * returning bogus data. */
  1211. generation = get_hpsb_generation(host);
  1212. /* If we get a reset before we are done waiting, then
  1213. * start the the waiting over again */
  1214. while (!down_trylock(&hi->reset_sem))
  1215. i = 0;
  1216. /* Check the kill_me again */
  1217. if (hi->kill_me) {
  1218. up(&nodemgr_serialize);
  1219. goto caught_signal;
  1220. }
  1221. }
  1222. if (!nodemgr_check_irm_capability(host, reset_cycles)) {
  1223. reset_cycles++;
  1224. up(&nodemgr_serialize);
  1225. continue;
  1226. }
  1227. /* Scan our nodes to get the bus options and create node
  1228. * entries. This does not do the sysfs stuff, since that
  1229. * would trigger hotplug callbacks and such, which is a
  1230. * bad idea at this point. */
  1231. nodemgr_node_scan(hi, generation);
  1232. if (!nodemgr_do_irm_duties(host, reset_cycles)) {
  1233. reset_cycles++;
  1234. up(&nodemgr_serialize);
  1235. continue;
  1236. }
  1237. reset_cycles = 0;
  1238. /* This actually does the full probe, with sysfs
  1239. * registration. */
  1240. nodemgr_node_probe(hi, generation);
  1241. /* Update some of our sysfs symlinks */
  1242. nodemgr_update_host_dev_links(host);
  1243. up(&nodemgr_serialize);
  1244. }
  1245. caught_signal:
  1246. HPSB_VERBOSE("NodeMgr: Exiting thread");
  1247. complete_and_exit(&hi->exited, 0);
  1248. }
  1249. int nodemgr_for_each_host(void *__data, int (*cb)(struct hpsb_host *, void *))
  1250. {
  1251. struct class *class = &hpsb_host_class;
  1252. struct class_device *cdev;
  1253. struct hpsb_host *host;
  1254. int error = 0;
  1255. down_read(&class->subsys.rwsem);
  1256. list_for_each_entry(cdev, &class->children, node) {
  1257. host = container_of(cdev, struct hpsb_host, class_dev);
  1258. if ((error = cb(host, __data)))
  1259. break;
  1260. }
  1261. up_read(&class->subsys.rwsem);
  1262. return error;
  1263. }
  1264. /* The following four convenience functions use a struct node_entry
  1265. * for addressing a node on the bus. They are intended for use by any
  1266. * process context, not just the nodemgr thread, so we need to be a
  1267. * little careful when reading out the node ID and generation. The
  1268. * thing that can go wrong is that we get the node ID, then a bus
  1269. * reset occurs, and then we read the generation. The node ID is
  1270. * possibly invalid, but the generation is current, and we end up
  1271. * sending a packet to a the wrong node.
  1272. *
  1273. * The solution is to make sure we read the generation first, so that
  1274. * if a reset occurs in the process, we end up with a stale generation
  1275. * and the transactions will fail instead of silently using wrong node
  1276. * ID's.
  1277. */
  1278. void hpsb_node_fill_packet(struct node_entry *ne, struct hpsb_packet *pkt)
  1279. {
  1280. pkt->host = ne->host;
  1281. pkt->generation = ne->generation;
  1282. barrier();
  1283. pkt->node_id = ne->nodeid;
  1284. }
  1285. int hpsb_node_write(struct node_entry *ne, u64 addr,
  1286. quadlet_t *buffer, size_t length)
  1287. {
  1288. unsigned int generation = ne->generation;
  1289. barrier();
  1290. return hpsb_write(ne->host, ne->nodeid, generation,
  1291. addr, buffer, length);
  1292. }
  1293. static void nodemgr_add_host(struct hpsb_host *host)
  1294. {
  1295. struct host_info *hi;
  1296. hi = hpsb_create_hostinfo(&nodemgr_highlevel, host, sizeof(*hi));
  1297. if (!hi) {
  1298. HPSB_ERR ("NodeMgr: out of memory in add host");
  1299. return;
  1300. }
  1301. hi->host = host;
  1302. init_completion(&hi->exited);
  1303. sema_init(&hi->reset_sem, 0);
  1304. sprintf(hi->daemon_name, "knodemgrd_%d", host->id);
  1305. hi->pid = kernel_thread(nodemgr_host_thread, hi, CLONE_KERNEL);
  1306. if (hi->pid < 0) {
  1307. HPSB_ERR ("NodeMgr: failed to start %s thread for %s",
  1308. hi->daemon_name, host->driver->name);
  1309. hpsb_destroy_hostinfo(&nodemgr_highlevel, host);
  1310. return;
  1311. }
  1312. return;
  1313. }
  1314. static void nodemgr_host_reset(struct hpsb_host *host)
  1315. {
  1316. struct host_info *hi = hpsb_get_hostinfo(&nodemgr_highlevel, host);
  1317. if (hi != NULL) {
  1318. HPSB_VERBOSE("NodeMgr: Processing host reset for %s", hi->daemon_name);
  1319. up(&hi->reset_sem);
  1320. } else
  1321. HPSB_ERR ("NodeMgr: could not process reset of unused host");
  1322. return;
  1323. }
  1324. static void nodemgr_remove_host(struct hpsb_host *host)
  1325. {
  1326. struct host_info *hi = hpsb_get_hostinfo(&nodemgr_highlevel, host);
  1327. if (hi) {
  1328. if (hi->pid >= 0) {
  1329. hi->kill_me = 1;
  1330. mb();
  1331. up(&hi->reset_sem);
  1332. wait_for_completion(&hi->exited);
  1333. nodemgr_remove_host_dev(&host->device);
  1334. }
  1335. } else
  1336. HPSB_ERR("NodeMgr: host %s does not exist, cannot remove",
  1337. host->driver->name);
  1338. return;
  1339. }
  1340. static struct hpsb_highlevel nodemgr_highlevel = {
  1341. .name = "Node manager",
  1342. .add_host = nodemgr_add_host,
  1343. .host_reset = nodemgr_host_reset,
  1344. .remove_host = nodemgr_remove_host,
  1345. };
  1346. int init_ieee1394_nodemgr(void)
  1347. {
  1348. int ret;
  1349. ret = class_register(&nodemgr_ne_class);
  1350. if (ret < 0)
  1351. return ret;
  1352. ret = class_register(&nodemgr_ud_class);
  1353. if (ret < 0) {
  1354. class_unregister(&nodemgr_ne_class);
  1355. return ret;
  1356. }
  1357. hpsb_register_highlevel(&nodemgr_highlevel);
  1358. return 0;
  1359. }
  1360. void cleanup_ieee1394_nodemgr(void)
  1361. {
  1362. hpsb_unregister_highlevel(&nodemgr_highlevel);
  1363. class_unregister(&nodemgr_ud_class);
  1364. class_unregister(&nodemgr_ne_class);
  1365. }