nodemgr.c 53 KB

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