nodemgr.c 51 KB

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