nodemgr.c 52 KB

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