nodemgr.c 52 KB

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