nodemgr.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740
  1. /*
  2. * Node information (ConfigROM) collection and management.
  3. *
  4. * Copyright (C) 2000 Andreas E. Bombe
  5. * 2001-2003 Ben Collins <bcollins@debian.net>
  6. *
  7. * This code is licensed under the GPL. See the file COPYING in the root
  8. * directory of the kernel sources for details.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/config.h>
  12. #include <linux/list.h>
  13. #include <linux/slab.h>
  14. #include <linux/smp_lock.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/kmod.h>
  17. #include <linux/completion.h>
  18. #include <linux/delay.h>
  19. #include <linux/pci.h>
  20. #include <linux/moduleparam.h>
  21. #include <asm/atomic.h>
  22. #include "ieee1394_types.h"
  23. #include "ieee1394.h"
  24. #include "ieee1394_core.h"
  25. #include "hosts.h"
  26. #include "ieee1394_transactions.h"
  27. #include "highlevel.h"
  28. #include "csr.h"
  29. #include "nodemgr.h"
  30. static int ignore_drivers;
  31. module_param(ignore_drivers, int, 0444);
  32. MODULE_PARM_DESC(ignore_drivers, "Disable automatic probing for drivers.");
  33. struct nodemgr_csr_info {
  34. struct hpsb_host *host;
  35. nodeid_t nodeid;
  36. unsigned int generation;
  37. };
  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, struct device_attribute *attr, 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, struct device_attribute *attr, 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, struct device_attribute *attr, 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, struct device_attribute *attr, 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, struct device_attribute *attr, 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, struct device_attribute *attr, 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, struct device_attribute *attr, 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, struct device_attribute *attr, 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 int __nodemgr_remove_host_dev(struct device *dev, void *data)
  547. {
  548. nodemgr_remove_ne(container_of(dev, struct node_entry, device));
  549. return 0;
  550. }
  551. static void nodemgr_remove_host_dev(struct device *dev)
  552. {
  553. device_for_each_child(dev, NULL, __nodemgr_remove_host_dev);
  554. sysfs_remove_link(&dev->kobj, "irm_id");
  555. sysfs_remove_link(&dev->kobj, "busmgr_id");
  556. sysfs_remove_link(&dev->kobj, "host_id");
  557. }
  558. static void nodemgr_update_bus_options(struct node_entry *ne)
  559. {
  560. #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
  561. static const u16 mr[] = { 4, 64, 1024, 0};
  562. #endif
  563. quadlet_t busoptions = be32_to_cpu(ne->csr->bus_info_data[2]);
  564. ne->busopt.irmc = (busoptions >> 31) & 1;
  565. ne->busopt.cmc = (busoptions >> 30) & 1;
  566. ne->busopt.isc = (busoptions >> 29) & 1;
  567. ne->busopt.bmc = (busoptions >> 28) & 1;
  568. ne->busopt.pmc = (busoptions >> 27) & 1;
  569. ne->busopt.cyc_clk_acc = (busoptions >> 16) & 0xff;
  570. ne->busopt.max_rec = 1 << (((busoptions >> 12) & 0xf) + 1);
  571. ne->busopt.max_rom = (busoptions >> 8) & 0x3;
  572. ne->busopt.generation = (busoptions >> 4) & 0xf;
  573. ne->busopt.lnkspd = busoptions & 0x7;
  574. HPSB_VERBOSE("NodeMgr: raw=0x%08x irmc=%d cmc=%d isc=%d bmc=%d pmc=%d "
  575. "cyc_clk_acc=%d max_rec=%d max_rom=%d gen=%d lspd=%d",
  576. busoptions, ne->busopt.irmc, ne->busopt.cmc,
  577. ne->busopt.isc, ne->busopt.bmc, ne->busopt.pmc,
  578. ne->busopt.cyc_clk_acc, ne->busopt.max_rec,
  579. mr[ne->busopt.max_rom],
  580. ne->busopt.generation, ne->busopt.lnkspd);
  581. }
  582. static struct node_entry *nodemgr_create_node(octlet_t guid, struct csr1212_csr *csr,
  583. struct host_info *hi, nodeid_t nodeid,
  584. unsigned int generation)
  585. {
  586. struct hpsb_host *host = hi->host;
  587. struct node_entry *ne;
  588. ne = kmalloc(sizeof(struct node_entry), GFP_KERNEL);
  589. if (!ne) return NULL;
  590. memset(ne, 0, sizeof(struct node_entry));
  591. ne->tpool = &host->tpool[nodeid & NODE_MASK];
  592. ne->host = host;
  593. ne->nodeid = nodeid;
  594. ne->generation = generation;
  595. ne->needs_probe = 1;
  596. ne->guid = guid;
  597. ne->guid_vendor_id = (guid >> 40) & 0xffffff;
  598. ne->guid_vendor_oui = nodemgr_find_oui_name(ne->guid_vendor_id);
  599. ne->csr = csr;
  600. memcpy(&ne->device, &nodemgr_dev_template_ne,
  601. sizeof(ne->device));
  602. ne->device.parent = &host->device;
  603. snprintf(ne->device.bus_id, BUS_ID_SIZE, "%016Lx",
  604. (unsigned long long)(ne->guid));
  605. ne->class_dev.dev = &ne->device;
  606. ne->class_dev.class = &nodemgr_ne_class;
  607. snprintf(ne->class_dev.class_id, BUS_ID_SIZE, "%016Lx",
  608. (unsigned long long)(ne->guid));
  609. device_register(&ne->device);
  610. class_device_register(&ne->class_dev);
  611. get_device(&ne->device);
  612. if (ne->guid_vendor_oui)
  613. device_create_file(&ne->device, &dev_attr_ne_guid_vendor_oui);
  614. nodemgr_create_ne_dev_files(ne);
  615. nodemgr_update_bus_options(ne);
  616. HPSB_DEBUG("%s added: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
  617. (host->node_id == nodeid) ? "Host" : "Node",
  618. NODE_BUS_ARGS(host, nodeid), (unsigned long long)guid);
  619. return ne;
  620. }
  621. static struct node_entry *find_entry_by_guid(u64 guid)
  622. {
  623. struct class *class = &nodemgr_ne_class;
  624. struct class_device *cdev;
  625. struct node_entry *ne, *ret_ne = NULL;
  626. down_read(&class->subsys.rwsem);
  627. list_for_each_entry(cdev, &class->children, node) {
  628. ne = container_of(cdev, struct node_entry, class_dev);
  629. if (ne->guid == guid) {
  630. ret_ne = ne;
  631. break;
  632. }
  633. }
  634. up_read(&class->subsys.rwsem);
  635. return ret_ne;
  636. }
  637. static struct node_entry *find_entry_by_nodeid(struct hpsb_host *host, nodeid_t nodeid)
  638. {
  639. struct class *class = &nodemgr_ne_class;
  640. struct class_device *cdev;
  641. struct node_entry *ne, *ret_ne = NULL;
  642. down_read(&class->subsys.rwsem);
  643. list_for_each_entry(cdev, &class->children, node) {
  644. ne = container_of(cdev, struct node_entry, class_dev);
  645. if (ne->host == host && ne->nodeid == nodeid) {
  646. ret_ne = ne;
  647. break;
  648. }
  649. }
  650. up_read(&class->subsys.rwsem);
  651. return ret_ne;
  652. }
  653. static void nodemgr_register_device(struct node_entry *ne,
  654. struct unit_directory *ud, struct device *parent)
  655. {
  656. memcpy(&ud->device, &nodemgr_dev_template_ud,
  657. sizeof(ud->device));
  658. ud->device.parent = parent;
  659. snprintf(ud->device.bus_id, BUS_ID_SIZE, "%s-%u",
  660. ne->device.bus_id, ud->id);
  661. ud->class_dev.dev = &ud->device;
  662. ud->class_dev.class = &nodemgr_ud_class;
  663. snprintf(ud->class_dev.class_id, BUS_ID_SIZE, "%s-%u",
  664. ne->device.bus_id, ud->id);
  665. device_register(&ud->device);
  666. class_device_register(&ud->class_dev);
  667. get_device(&ud->device);
  668. if (ud->vendor_oui)
  669. device_create_file(&ud->device, &dev_attr_ud_vendor_oui);
  670. nodemgr_create_ud_dev_files(ud);
  671. }
  672. /* This implementation currently only scans the config rom and its
  673. * immediate unit directories looking for software_id and
  674. * software_version entries, in order to get driver autoloading working. */
  675. static struct unit_directory *nodemgr_process_unit_directory
  676. (struct host_info *hi, struct node_entry *ne, struct csr1212_keyval *ud_kv,
  677. unsigned int *id, struct unit_directory *parent)
  678. {
  679. struct unit_directory *ud;
  680. struct unit_directory *ud_child = NULL;
  681. struct csr1212_dentry *dentry;
  682. struct csr1212_keyval *kv;
  683. u8 last_key_id = 0;
  684. ud = kmalloc(sizeof(struct unit_directory), GFP_KERNEL);
  685. if (!ud)
  686. goto unit_directory_error;
  687. memset (ud, 0, sizeof(struct unit_directory));
  688. ud->ne = ne;
  689. ud->ignore_driver = ignore_drivers;
  690. ud->address = ud_kv->offset + CSR1212_CONFIG_ROM_SPACE_BASE;
  691. ud->ud_kv = ud_kv;
  692. ud->id = (*id)++;
  693. csr1212_for_each_dir_entry(ne->csr, kv, ud_kv, dentry) {
  694. switch (kv->key.id) {
  695. case CSR1212_KV_ID_VENDOR:
  696. if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
  697. ud->vendor_id = kv->value.immediate;
  698. ud->flags |= UNIT_DIRECTORY_VENDOR_ID;
  699. if (ud->vendor_id)
  700. ud->vendor_oui = nodemgr_find_oui_name(ud->vendor_id);
  701. }
  702. break;
  703. case CSR1212_KV_ID_MODEL:
  704. ud->model_id = kv->value.immediate;
  705. ud->flags |= UNIT_DIRECTORY_MODEL_ID;
  706. break;
  707. case CSR1212_KV_ID_SPECIFIER_ID:
  708. ud->specifier_id = kv->value.immediate;
  709. ud->flags |= UNIT_DIRECTORY_SPECIFIER_ID;
  710. break;
  711. case CSR1212_KV_ID_VERSION:
  712. ud->version = kv->value.immediate;
  713. ud->flags |= UNIT_DIRECTORY_VERSION;
  714. break;
  715. case CSR1212_KV_ID_DESCRIPTOR:
  716. if (kv->key.type == CSR1212_KV_TYPE_LEAF &&
  717. CSR1212_DESCRIPTOR_LEAF_TYPE(kv) == 0 &&
  718. CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) == 0 &&
  719. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) == 0 &&
  720. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) == 0 &&
  721. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) == 0) {
  722. switch (last_key_id) {
  723. case CSR1212_KV_ID_VENDOR:
  724. ud->vendor_name_kv = kv;
  725. csr1212_keep_keyval(kv);
  726. break;
  727. case CSR1212_KV_ID_MODEL:
  728. ud->model_name_kv = kv;
  729. csr1212_keep_keyval(kv);
  730. break;
  731. }
  732. } /* else if (kv->key.type == CSR1212_KV_TYPE_DIRECTORY) ... */
  733. break;
  734. case CSR1212_KV_ID_DEPENDENT_INFO:
  735. /* Logical Unit Number */
  736. if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
  737. if (ud->flags & UNIT_DIRECTORY_HAS_LUN) {
  738. ud_child = kmalloc(sizeof(struct unit_directory), GFP_KERNEL);
  739. if (!ud_child)
  740. goto unit_directory_error;
  741. memcpy(ud_child, ud, sizeof(struct unit_directory));
  742. nodemgr_register_device(ne, ud_child, &ne->device);
  743. ud_child = NULL;
  744. ud->id = (*id)++;
  745. }
  746. ud->lun = kv->value.immediate;
  747. ud->flags |= UNIT_DIRECTORY_HAS_LUN;
  748. /* Logical Unit Directory */
  749. } else if (kv->key.type == CSR1212_KV_TYPE_DIRECTORY) {
  750. /* This should really be done in SBP2 as this is
  751. * doing SBP2 specific parsing.
  752. */
  753. /* first register the parent unit */
  754. ud->flags |= UNIT_DIRECTORY_HAS_LUN_DIRECTORY;
  755. if (ud->device.bus != &ieee1394_bus_type)
  756. nodemgr_register_device(ne, ud, &ne->device);
  757. /* process the child unit */
  758. ud_child = nodemgr_process_unit_directory(hi, ne, kv, id, ud);
  759. if (ud_child == NULL)
  760. break;
  761. /* inherit unspecified values so hotplug picks it up */
  762. if ((ud->flags & UNIT_DIRECTORY_MODEL_ID) &&
  763. !(ud_child->flags & UNIT_DIRECTORY_MODEL_ID))
  764. {
  765. ud_child->flags |= UNIT_DIRECTORY_MODEL_ID;
  766. ud_child->model_id = ud->model_id;
  767. }
  768. if ((ud->flags & UNIT_DIRECTORY_SPECIFIER_ID) &&
  769. !(ud_child->flags & UNIT_DIRECTORY_SPECIFIER_ID))
  770. {
  771. ud_child->flags |= UNIT_DIRECTORY_SPECIFIER_ID;
  772. ud_child->specifier_id = ud->specifier_id;
  773. }
  774. if ((ud->flags & UNIT_DIRECTORY_VERSION) &&
  775. !(ud_child->flags & UNIT_DIRECTORY_VERSION))
  776. {
  777. ud_child->flags |= UNIT_DIRECTORY_VERSION;
  778. ud_child->version = ud->version;
  779. }
  780. /* register the child unit */
  781. ud_child->flags |= UNIT_DIRECTORY_LUN_DIRECTORY;
  782. nodemgr_register_device(ne, ud_child, &ud->device);
  783. }
  784. break;
  785. default:
  786. break;
  787. }
  788. last_key_id = kv->key.id;
  789. }
  790. /* do not process child units here and only if not already registered */
  791. if (!parent && ud->device.bus != &ieee1394_bus_type)
  792. nodemgr_register_device(ne, ud, &ne->device);
  793. return ud;
  794. unit_directory_error:
  795. kfree(ud);
  796. return NULL;
  797. }
  798. static void nodemgr_process_root_directory(struct host_info *hi, struct node_entry *ne)
  799. {
  800. unsigned int ud_id = 0;
  801. struct csr1212_dentry *dentry;
  802. struct csr1212_keyval *kv;
  803. u8 last_key_id = 0;
  804. ne->needs_probe = 0;
  805. csr1212_for_each_dir_entry(ne->csr, kv, ne->csr->root_kv, dentry) {
  806. switch (kv->key.id) {
  807. case CSR1212_KV_ID_VENDOR:
  808. ne->vendor_id = kv->value.immediate;
  809. if (ne->vendor_id)
  810. ne->vendor_oui = nodemgr_find_oui_name(ne->vendor_id);
  811. break;
  812. case CSR1212_KV_ID_NODE_CAPABILITIES:
  813. ne->capabilities = kv->value.immediate;
  814. break;
  815. case CSR1212_KV_ID_UNIT:
  816. nodemgr_process_unit_directory(hi, ne, kv, &ud_id, NULL);
  817. break;
  818. case CSR1212_KV_ID_DESCRIPTOR:
  819. if (last_key_id == CSR1212_KV_ID_VENDOR) {
  820. if (kv->key.type == CSR1212_KV_TYPE_LEAF &&
  821. CSR1212_DESCRIPTOR_LEAF_TYPE(kv) == 0 &&
  822. CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) == 0 &&
  823. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) == 0 &&
  824. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) == 0 &&
  825. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) == 0) {
  826. ne->vendor_name_kv = kv;
  827. csr1212_keep_keyval(kv);
  828. }
  829. }
  830. break;
  831. }
  832. last_key_id = kv->key.id;
  833. }
  834. if (ne->vendor_oui)
  835. device_create_file(&ne->device, &dev_attr_ne_vendor_oui);
  836. if (ne->vendor_name_kv)
  837. device_create_file(&ne->device, &dev_attr_ne_vendor_name_kv);
  838. }
  839. #ifdef CONFIG_HOTPLUG
  840. static int nodemgr_hotplug(struct class_device *cdev, char **envp, int num_envp,
  841. char *buffer, int buffer_size)
  842. {
  843. struct unit_directory *ud;
  844. int i = 0;
  845. int length = 0;
  846. /* ieee1394:venNmoNspNverN */
  847. char buf[8 + 1 + 3 + 8 + 2 + 8 + 2 + 8 + 3 + 8 + 1];
  848. if (!cdev)
  849. return -ENODEV;
  850. ud = container_of(cdev, struct unit_directory, class_dev);
  851. if (ud->ne->in_limbo || ud->ignore_driver)
  852. return -ENODEV;
  853. #define PUT_ENVP(fmt,val) \
  854. do { \
  855. int printed; \
  856. envp[i++] = buffer; \
  857. printed = snprintf(buffer, buffer_size - length, \
  858. fmt, val); \
  859. if ((buffer_size - (length+printed) <= 0) || (i >= num_envp)) \
  860. return -ENOMEM; \
  861. length += printed+1; \
  862. buffer += printed+1; \
  863. } while (0)
  864. PUT_ENVP("VENDOR_ID=%06x", ud->vendor_id);
  865. PUT_ENVP("MODEL_ID=%06x", ud->model_id);
  866. PUT_ENVP("GUID=%016Lx", (unsigned long long)ud->ne->guid);
  867. PUT_ENVP("SPECIFIER_ID=%06x", ud->specifier_id);
  868. PUT_ENVP("VERSION=%06x", ud->version);
  869. snprintf(buf, sizeof(buf), "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
  870. ud->vendor_id,
  871. ud->model_id,
  872. ud->specifier_id,
  873. ud->version);
  874. PUT_ENVP("MODALIAS=%s", buf);
  875. #undef PUT_ENVP
  876. envp[i] = NULL;
  877. return 0;
  878. }
  879. #else
  880. static int nodemgr_hotplug(struct class_device *cdev, char **envp, int num_envp,
  881. char *buffer, int buffer_size)
  882. {
  883. return -ENODEV;
  884. }
  885. #endif /* CONFIG_HOTPLUG */
  886. int hpsb_register_protocol(struct hpsb_protocol_driver *driver)
  887. {
  888. int ret;
  889. /* This will cause a probe for devices */
  890. ret = driver_register(&driver->driver);
  891. if (!ret)
  892. nodemgr_create_drv_files(driver);
  893. return ret;
  894. }
  895. void hpsb_unregister_protocol(struct hpsb_protocol_driver *driver)
  896. {
  897. nodemgr_remove_drv_files(driver);
  898. /* This will subsequently disconnect all devices that our driver
  899. * is attached to. */
  900. driver_unregister(&driver->driver);
  901. }
  902. /*
  903. * This function updates nodes that were present on the bus before the
  904. * reset and still are after the reset. The nodeid and the config rom
  905. * may have changed, and the drivers managing this device must be
  906. * informed that this device just went through a bus reset, to allow
  907. * the to take whatever actions required.
  908. */
  909. static void nodemgr_update_node(struct node_entry *ne, struct csr1212_csr *csr,
  910. struct host_info *hi, nodeid_t nodeid,
  911. unsigned int generation)
  912. {
  913. if (ne->nodeid != nodeid) {
  914. HPSB_DEBUG("Node changed: " NODE_BUS_FMT " -> " NODE_BUS_FMT,
  915. NODE_BUS_ARGS(ne->host, ne->nodeid),
  916. NODE_BUS_ARGS(ne->host, nodeid));
  917. ne->nodeid = nodeid;
  918. }
  919. if (ne->busopt.generation != ((be32_to_cpu(csr->bus_info_data[2]) >> 4) & 0xf)) {
  920. kfree(ne->csr->private);
  921. csr1212_destroy_csr(ne->csr);
  922. ne->csr = csr;
  923. /* If the node's configrom generation has changed, we
  924. * unregister all the unit directories. */
  925. nodemgr_remove_uds(ne);
  926. nodemgr_update_bus_options(ne);
  927. /* Mark the node as new, so it gets re-probed */
  928. ne->needs_probe = 1;
  929. } else {
  930. /* old cache is valid, so update its generation */
  931. struct nodemgr_csr_info *ci = ne->csr->private;
  932. ci->generation = generation;
  933. /* free the partially filled now unneeded new cache */
  934. kfree(csr->private);
  935. csr1212_destroy_csr(csr);
  936. }
  937. if (ne->in_limbo)
  938. nodemgr_resume_ne(ne);
  939. /* Mark the node current */
  940. ne->generation = generation;
  941. }
  942. static void nodemgr_node_scan_one(struct host_info *hi,
  943. nodeid_t nodeid, int generation)
  944. {
  945. struct hpsb_host *host = hi->host;
  946. struct node_entry *ne;
  947. octlet_t guid;
  948. struct csr1212_csr *csr;
  949. struct nodemgr_csr_info *ci;
  950. ci = kmalloc(sizeof(struct nodemgr_csr_info), GFP_KERNEL);
  951. if (!ci)
  952. return;
  953. ci->host = host;
  954. ci->nodeid = nodeid;
  955. ci->generation = generation;
  956. /* We need to detect when the ConfigROM's generation has changed,
  957. * so we only update the node's info when it needs to be. */
  958. csr = csr1212_create_csr(&nodemgr_csr_ops, 5 * sizeof(quadlet_t), ci);
  959. if (!csr || csr1212_parse_csr(csr) != CSR1212_SUCCESS) {
  960. HPSB_ERR("Error parsing configrom for node " NODE_BUS_FMT,
  961. NODE_BUS_ARGS(host, nodeid));
  962. if (csr)
  963. csr1212_destroy_csr(csr);
  964. kfree(ci);
  965. return;
  966. }
  967. if (csr->bus_info_data[1] != IEEE1394_BUSID_MAGIC) {
  968. /* This isn't a 1394 device, but we let it slide. There
  969. * was a report of a device with broken firmware which
  970. * reported '2394' instead of '1394', which is obviously a
  971. * mistake. One would hope that a non-1394 device never
  972. * gets connected to Firewire bus. If someone does, we
  973. * shouldn't be held responsible, so we'll allow it with a
  974. * warning. */
  975. HPSB_WARN("Node " NODE_BUS_FMT " has invalid busID magic [0x%08x]",
  976. NODE_BUS_ARGS(host, nodeid), csr->bus_info_data[1]);
  977. }
  978. guid = ((u64)be32_to_cpu(csr->bus_info_data[3]) << 32) | be32_to_cpu(csr->bus_info_data[4]);
  979. ne = find_entry_by_guid(guid);
  980. if (ne && ne->host != host && ne->in_limbo) {
  981. /* Must have moved this device from one host to another */
  982. nodemgr_remove_ne(ne);
  983. ne = NULL;
  984. }
  985. if (!ne)
  986. nodemgr_create_node(guid, csr, hi, nodeid, generation);
  987. else
  988. nodemgr_update_node(ne, csr, hi, nodeid, generation);
  989. return;
  990. }
  991. static void nodemgr_node_scan(struct host_info *hi, int generation)
  992. {
  993. int count;
  994. struct hpsb_host *host = hi->host;
  995. struct selfid *sid = (struct selfid *)host->topology_map;
  996. nodeid_t nodeid = LOCAL_BUS;
  997. /* Scan each node on the bus */
  998. for (count = host->selfid_count; count; count--, sid++) {
  999. if (sid->extended)
  1000. continue;
  1001. if (!sid->link_active) {
  1002. nodeid++;
  1003. continue;
  1004. }
  1005. nodemgr_node_scan_one(hi, nodeid++, generation);
  1006. }
  1007. }
  1008. static void nodemgr_suspend_ne(struct node_entry *ne)
  1009. {
  1010. struct class_device *cdev;
  1011. struct unit_directory *ud;
  1012. HPSB_DEBUG("Node suspended: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
  1013. NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
  1014. ne->in_limbo = 1;
  1015. device_create_file(&ne->device, &dev_attr_ne_in_limbo);
  1016. down_write(&ne->device.bus->subsys.rwsem);
  1017. list_for_each_entry(cdev, &nodemgr_ud_class.children, node) {
  1018. ud = container_of(cdev, struct unit_directory, class_dev);
  1019. if (ud->ne != ne)
  1020. continue;
  1021. if (ud->device.driver &&
  1022. (!ud->device.driver->suspend ||
  1023. ud->device.driver->suspend(&ud->device, PMSG_SUSPEND, 0)))
  1024. device_release_driver(&ud->device);
  1025. }
  1026. up_write(&ne->device.bus->subsys.rwsem);
  1027. }
  1028. static void nodemgr_resume_ne(struct node_entry *ne)
  1029. {
  1030. struct class_device *cdev;
  1031. struct unit_directory *ud;
  1032. ne->in_limbo = 0;
  1033. device_remove_file(&ne->device, &dev_attr_ne_in_limbo);
  1034. down_read(&ne->device.bus->subsys.rwsem);
  1035. list_for_each_entry(cdev, &nodemgr_ud_class.children, node) {
  1036. ud = container_of(cdev, struct unit_directory, class_dev);
  1037. if (ud->ne != ne)
  1038. continue;
  1039. if (ud->device.driver && ud->device.driver->resume)
  1040. ud->device.driver->resume(&ud->device, 0);
  1041. }
  1042. up_read(&ne->device.bus->subsys.rwsem);
  1043. HPSB_DEBUG("Node resumed: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
  1044. NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
  1045. }
  1046. static void nodemgr_update_pdrv(struct node_entry *ne)
  1047. {
  1048. struct unit_directory *ud;
  1049. struct hpsb_protocol_driver *pdrv;
  1050. struct class *class = &nodemgr_ud_class;
  1051. struct class_device *cdev;
  1052. down_read(&class->subsys.rwsem);
  1053. list_for_each_entry(cdev, &class->children, node) {
  1054. ud = container_of(cdev, struct unit_directory, class_dev);
  1055. if (ud->ne != ne || !ud->device.driver)
  1056. continue;
  1057. pdrv = container_of(ud->device.driver, struct hpsb_protocol_driver, driver);
  1058. if (pdrv->update && pdrv->update(ud)) {
  1059. down_write(&ud->device.bus->subsys.rwsem);
  1060. device_release_driver(&ud->device);
  1061. up_write(&ud->device.bus->subsys.rwsem);
  1062. }
  1063. }
  1064. up_read(&class->subsys.rwsem);
  1065. }
  1066. static void nodemgr_probe_ne(struct host_info *hi, struct node_entry *ne, int generation)
  1067. {
  1068. struct device *dev;
  1069. if (ne->host != hi->host || ne->in_limbo)
  1070. return;
  1071. dev = get_device(&ne->device);
  1072. if (!dev)
  1073. return;
  1074. /* If "needs_probe", then this is either a new or changed node we
  1075. * rescan totally. If the generation matches for an existing node
  1076. * (one that existed prior to the bus reset) we send update calls
  1077. * down to the drivers. Otherwise, this is a dead node and we
  1078. * suspend it. */
  1079. if (ne->needs_probe)
  1080. nodemgr_process_root_directory(hi, ne);
  1081. else if (ne->generation == generation)
  1082. nodemgr_update_pdrv(ne);
  1083. else
  1084. nodemgr_suspend_ne(ne);
  1085. put_device(dev);
  1086. }
  1087. static void nodemgr_node_probe(struct host_info *hi, int generation)
  1088. {
  1089. struct hpsb_host *host = hi->host;
  1090. struct class *class = &nodemgr_ne_class;
  1091. struct class_device *cdev;
  1092. /* Do some processing of the nodes we've probed. This pulls them
  1093. * into the sysfs layer if needed, and can result in processing of
  1094. * unit-directories, or just updating the node and it's
  1095. * unit-directories. */
  1096. down_read(&class->subsys.rwsem);
  1097. list_for_each_entry(cdev, &class->children, node)
  1098. nodemgr_probe_ne(hi, container_of(cdev, struct node_entry, class_dev), generation);
  1099. up_read(&class->subsys.rwsem);
  1100. /* If we had a bus reset while we were scanning the bus, it is
  1101. * possible that we did not probe all nodes. In that case, we
  1102. * skip the clean up for now, since we could remove nodes that
  1103. * were still on the bus. The bus reset increased hi->reset_sem,
  1104. * so there's a bus scan pending which will do the clean up
  1105. * eventually.
  1106. *
  1107. * Now let's tell the bus to rescan our devices. This may seem
  1108. * like overhead, but the driver-model core will only scan a
  1109. * device for a driver when either the device is added, or when a
  1110. * new driver is added. A bus reset is a good reason to rescan
  1111. * devices that were there before. For example, an sbp2 device
  1112. * may become available for login, if the host that held it was
  1113. * just removed. */
  1114. if (generation == get_hpsb_generation(host))
  1115. bus_rescan_devices(&ieee1394_bus_type);
  1116. return;
  1117. }
  1118. /* Because we are a 1394a-2000 compliant IRM, we need to inform all the other
  1119. * nodes of the broadcast channel. (Really we're only setting the validity
  1120. * bit). Other IRM responsibilities go in here as well. */
  1121. static int nodemgr_do_irm_duties(struct hpsb_host *host, int cycles)
  1122. {
  1123. quadlet_t bc;
  1124. /* if irm_id == -1 then there is no IRM on this bus */
  1125. if (!host->is_irm || host->irm_id == (nodeid_t)-1)
  1126. return 1;
  1127. host->csr.broadcast_channel |= 0x40000000; /* set validity bit */
  1128. bc = cpu_to_be32(host->csr.broadcast_channel);
  1129. hpsb_write(host, LOCAL_BUS | ALL_NODES, get_hpsb_generation(host),
  1130. (CSR_REGISTER_BASE | CSR_BROADCAST_CHANNEL),
  1131. &bc, sizeof(quadlet_t));
  1132. /* If there is no bus manager then we should set the root node's
  1133. * force_root bit to promote bus stability per the 1394
  1134. * spec. (8.4.2.6) */
  1135. if (host->busmgr_id == 0xffff && host->node_count > 1)
  1136. {
  1137. u16 root_node = host->node_count - 1;
  1138. struct node_entry *ne = find_entry_by_nodeid(host, root_node | LOCAL_BUS);
  1139. if (ne && ne->busopt.cmc)
  1140. hpsb_send_phy_config(host, root_node, -1);
  1141. else {
  1142. HPSB_DEBUG("The root node is not cycle master capable; "
  1143. "selecting a new root node and resetting...");
  1144. if (cycles >= 5) {
  1145. /* Oh screw it! Just leave the bus as it is */
  1146. HPSB_DEBUG("Stopping reset loop for IRM sanity");
  1147. return 1;
  1148. }
  1149. hpsb_send_phy_config(host, NODEID_TO_NODE(host->node_id), -1);
  1150. hpsb_reset_bus(host, LONG_RESET_FORCE_ROOT);
  1151. return 0;
  1152. }
  1153. }
  1154. return 1;
  1155. }
  1156. /* We need to ensure that if we are not the IRM, that the IRM node is capable of
  1157. * everything we can do, otherwise issue a bus reset and try to become the IRM
  1158. * ourselves. */
  1159. static int nodemgr_check_irm_capability(struct hpsb_host *host, int cycles)
  1160. {
  1161. quadlet_t bc;
  1162. int status;
  1163. if (hpsb_disable_irm || host->is_irm)
  1164. return 1;
  1165. status = hpsb_read(host, LOCAL_BUS | (host->irm_id),
  1166. get_hpsb_generation(host),
  1167. (CSR_REGISTER_BASE | CSR_BROADCAST_CHANNEL),
  1168. &bc, sizeof(quadlet_t));
  1169. if (status < 0 || !(be32_to_cpu(bc) & 0x80000000)) {
  1170. /* The current irm node does not have a valid BROADCAST_CHANNEL
  1171. * register and we do, so reset the bus with force_root set */
  1172. HPSB_DEBUG("Current remote IRM is not 1394a-2000 compliant, resetting...");
  1173. if (cycles >= 5) {
  1174. /* Oh screw it! Just leave the bus as it is */
  1175. HPSB_DEBUG("Stopping reset loop for IRM sanity");
  1176. return 1;
  1177. }
  1178. hpsb_send_phy_config(host, NODEID_TO_NODE(host->node_id), -1);
  1179. hpsb_reset_bus(host, LONG_RESET_FORCE_ROOT);
  1180. return 0;
  1181. }
  1182. return 1;
  1183. }
  1184. static int nodemgr_host_thread(void *__hi)
  1185. {
  1186. struct host_info *hi = (struct host_info *)__hi;
  1187. struct hpsb_host *host = hi->host;
  1188. int reset_cycles = 0;
  1189. /* No userlevel access needed */
  1190. daemonize(hi->daemon_name);
  1191. /* Setup our device-model entries */
  1192. nodemgr_create_host_dev_files(host);
  1193. /* Sit and wait for a signal to probe the nodes on the bus. This
  1194. * happens when we get a bus reset. */
  1195. while (1) {
  1196. unsigned int generation = 0;
  1197. int i;
  1198. if (down_interruptible(&hi->reset_sem) ||
  1199. down_interruptible(&nodemgr_serialize)) {
  1200. if (try_to_freeze())
  1201. continue;
  1202. printk("NodeMgr: received unexpected signal?!\n" );
  1203. break;
  1204. }
  1205. if (hi->kill_me) {
  1206. up(&nodemgr_serialize);
  1207. break;
  1208. }
  1209. /* Pause for 1/4 second in 1/16 second intervals,
  1210. * to make sure things settle down. */
  1211. for (i = 0; i < 4 ; i++) {
  1212. set_current_state(TASK_INTERRUPTIBLE);
  1213. if (msleep_interruptible(63)) {
  1214. up(&nodemgr_serialize);
  1215. goto caught_signal;
  1216. }
  1217. /* Now get the generation in which the node ID's we collect
  1218. * are valid. During the bus scan we will use this generation
  1219. * for the read transactions, so that if another reset occurs
  1220. * during the scan the transactions will fail instead of
  1221. * returning bogus data. */
  1222. generation = get_hpsb_generation(host);
  1223. /* If we get a reset before we are done waiting, then
  1224. * start the the waiting over again */
  1225. while (!down_trylock(&hi->reset_sem))
  1226. i = 0;
  1227. /* Check the kill_me again */
  1228. if (hi->kill_me) {
  1229. up(&nodemgr_serialize);
  1230. goto caught_signal;
  1231. }
  1232. }
  1233. if (!nodemgr_check_irm_capability(host, reset_cycles)) {
  1234. reset_cycles++;
  1235. up(&nodemgr_serialize);
  1236. continue;
  1237. }
  1238. /* Scan our nodes to get the bus options and create node
  1239. * entries. This does not do the sysfs stuff, since that
  1240. * would trigger hotplug callbacks and such, which is a
  1241. * bad idea at this point. */
  1242. nodemgr_node_scan(hi, generation);
  1243. if (!nodemgr_do_irm_duties(host, reset_cycles)) {
  1244. reset_cycles++;
  1245. up(&nodemgr_serialize);
  1246. continue;
  1247. }
  1248. reset_cycles = 0;
  1249. /* This actually does the full probe, with sysfs
  1250. * registration. */
  1251. nodemgr_node_probe(hi, generation);
  1252. /* Update some of our sysfs symlinks */
  1253. nodemgr_update_host_dev_links(host);
  1254. up(&nodemgr_serialize);
  1255. }
  1256. caught_signal:
  1257. HPSB_VERBOSE("NodeMgr: Exiting thread");
  1258. complete_and_exit(&hi->exited, 0);
  1259. }
  1260. int nodemgr_for_each_host(void *__data, int (*cb)(struct hpsb_host *, void *))
  1261. {
  1262. struct class *class = &hpsb_host_class;
  1263. struct class_device *cdev;
  1264. struct hpsb_host *host;
  1265. int error = 0;
  1266. down_read(&class->subsys.rwsem);
  1267. list_for_each_entry(cdev, &class->children, node) {
  1268. host = container_of(cdev, struct hpsb_host, class_dev);
  1269. if ((error = cb(host, __data)))
  1270. break;
  1271. }
  1272. up_read(&class->subsys.rwsem);
  1273. return error;
  1274. }
  1275. /* The following four convenience functions use a struct node_entry
  1276. * for addressing a node on the bus. They are intended for use by any
  1277. * process context, not just the nodemgr thread, so we need to be a
  1278. * little careful when reading out the node ID and generation. The
  1279. * thing that can go wrong is that we get the node ID, then a bus
  1280. * reset occurs, and then we read the generation. The node ID is
  1281. * possibly invalid, but the generation is current, and we end up
  1282. * sending a packet to a the wrong node.
  1283. *
  1284. * The solution is to make sure we read the generation first, so that
  1285. * if a reset occurs in the process, we end up with a stale generation
  1286. * and the transactions will fail instead of silently using wrong node
  1287. * ID's.
  1288. */
  1289. void hpsb_node_fill_packet(struct node_entry *ne, struct hpsb_packet *pkt)
  1290. {
  1291. pkt->host = ne->host;
  1292. pkt->generation = ne->generation;
  1293. barrier();
  1294. pkt->node_id = ne->nodeid;
  1295. }
  1296. int hpsb_node_write(struct node_entry *ne, u64 addr,
  1297. quadlet_t *buffer, size_t length)
  1298. {
  1299. unsigned int generation = ne->generation;
  1300. barrier();
  1301. return hpsb_write(ne->host, ne->nodeid, generation,
  1302. addr, buffer, length);
  1303. }
  1304. static void nodemgr_add_host(struct hpsb_host *host)
  1305. {
  1306. struct host_info *hi;
  1307. hi = hpsb_create_hostinfo(&nodemgr_highlevel, host, sizeof(*hi));
  1308. if (!hi) {
  1309. HPSB_ERR ("NodeMgr: out of memory in add host");
  1310. return;
  1311. }
  1312. hi->host = host;
  1313. init_completion(&hi->exited);
  1314. sema_init(&hi->reset_sem, 0);
  1315. sprintf(hi->daemon_name, "knodemgrd_%d", host->id);
  1316. hi->pid = kernel_thread(nodemgr_host_thread, hi, CLONE_KERNEL);
  1317. if (hi->pid < 0) {
  1318. HPSB_ERR ("NodeMgr: failed to start %s thread for %s",
  1319. hi->daemon_name, host->driver->name);
  1320. hpsb_destroy_hostinfo(&nodemgr_highlevel, host);
  1321. return;
  1322. }
  1323. return;
  1324. }
  1325. static void nodemgr_host_reset(struct hpsb_host *host)
  1326. {
  1327. struct host_info *hi = hpsb_get_hostinfo(&nodemgr_highlevel, host);
  1328. if (hi != NULL) {
  1329. HPSB_VERBOSE("NodeMgr: Processing host reset for %s", hi->daemon_name);
  1330. up(&hi->reset_sem);
  1331. } else
  1332. HPSB_ERR ("NodeMgr: could not process reset of unused host");
  1333. return;
  1334. }
  1335. static void nodemgr_remove_host(struct hpsb_host *host)
  1336. {
  1337. struct host_info *hi = hpsb_get_hostinfo(&nodemgr_highlevel, host);
  1338. if (hi) {
  1339. if (hi->pid >= 0) {
  1340. hi->kill_me = 1;
  1341. mb();
  1342. up(&hi->reset_sem);
  1343. wait_for_completion(&hi->exited);
  1344. nodemgr_remove_host_dev(&host->device);
  1345. }
  1346. } else
  1347. HPSB_ERR("NodeMgr: host %s does not exist, cannot remove",
  1348. host->driver->name);
  1349. return;
  1350. }
  1351. static struct hpsb_highlevel nodemgr_highlevel = {
  1352. .name = "Node manager",
  1353. .add_host = nodemgr_add_host,
  1354. .host_reset = nodemgr_host_reset,
  1355. .remove_host = nodemgr_remove_host,
  1356. };
  1357. int init_ieee1394_nodemgr(void)
  1358. {
  1359. int ret;
  1360. ret = class_register(&nodemgr_ne_class);
  1361. if (ret < 0)
  1362. return ret;
  1363. ret = class_register(&nodemgr_ud_class);
  1364. if (ret < 0) {
  1365. class_unregister(&nodemgr_ne_class);
  1366. return ret;
  1367. }
  1368. hpsb_register_highlevel(&nodemgr_highlevel);
  1369. return 0;
  1370. }
  1371. void cleanup_ieee1394_nodemgr(void)
  1372. {
  1373. hpsb_unregister_highlevel(&nodemgr_highlevel);
  1374. class_unregister(&nodemgr_ud_class);
  1375. class_unregister(&nodemgr_ne_class);
  1376. }