nodemgr.c 51 KB

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