nodemgr.c 47 KB

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