nodemgr.c 52 KB

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