nodemgr.c 51 KB

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