nodemgr.c 51 KB

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