nodemgr.c 52 KB

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