nodemgr.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905
  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 <linux/semaphore.h>
  21. #include <asm/atomic.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. #define OUI_FREECOM_TECHNOLOGIES_GMBH 0x0001db
  107. static int nodemgr_get_max_rom(quadlet_t *bus_info_data, void *__ci)
  108. {
  109. /* Freecom FireWire Hard Drive firmware bug */
  110. if (be32_to_cpu(bus_info_data[3]) >> 8 == OUI_FREECOM_TECHNOLOGIES_GMBH)
  111. return 0;
  112. return (be32_to_cpu(bus_info_data[2]) >> 8) & 0x3;
  113. }
  114. static struct csr1212_bus_ops nodemgr_csr_ops = {
  115. .bus_read = nodemgr_bus_read,
  116. .get_max_rom = nodemgr_get_max_rom
  117. };
  118. /*
  119. * Basically what we do here is start off retrieving the bus_info block.
  120. * From there will fill in some info about the node, verify it is of IEEE
  121. * 1394 type, and that the crc checks out ok. After that we start off with
  122. * the root directory, and subdirectories. To do this, we retrieve the
  123. * quadlet header for a directory, find out the length, and retrieve the
  124. * complete directory entry (be it a leaf or a directory). We then process
  125. * it and add the info to our structure for that particular node.
  126. *
  127. * We verify CRC's along the way for each directory/block/leaf. The entire
  128. * node structure is generic, and simply stores the information in a way
  129. * that's easy to parse by the protocol interface.
  130. */
  131. /*
  132. * The nodemgr relies heavily on the Driver Model for device callbacks and
  133. * driver/device mappings. The old nodemgr used to handle all this itself,
  134. * but now we are much simpler because of the LDM.
  135. */
  136. struct host_info {
  137. struct hpsb_host *host;
  138. struct list_head list;
  139. struct task_struct *thread;
  140. };
  141. static int nodemgr_bus_match(struct device * dev, struct device_driver * drv);
  142. static int nodemgr_uevent(struct device *dev, struct kobj_uevent_env *env);
  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_rescan(struct bus_type *bus, const char *buf,
  325. size_t count)
  326. {
  327. int error = 0;
  328. if (simple_strtoul(buf, NULL, 10) == 1)
  329. error = bus_rescan_devices(&ieee1394_bus_type);
  330. return error ? error : count;
  331. }
  332. static ssize_t fw_get_rescan(struct bus_type *bus, char *buf)
  333. {
  334. return sprintf(buf, "You can force a rescan of the bus for "
  335. "drivers by writing a 1 to this file\n");
  336. }
  337. static BUS_ATTR(rescan, S_IWUSR | S_IRUGO, fw_get_rescan, fw_set_rescan);
  338. static ssize_t fw_set_ignore_drivers(struct bus_type *bus, const char *buf, size_t count)
  339. {
  340. int state = simple_strtoul(buf, NULL, 10);
  341. if (state == 1)
  342. ignore_drivers = 1;
  343. else if (state == 0)
  344. ignore_drivers = 0;
  345. return count;
  346. }
  347. static ssize_t fw_get_ignore_drivers(struct bus_type *bus, char *buf)
  348. {
  349. return sprintf(buf, "%d\n", ignore_drivers);
  350. }
  351. static BUS_ATTR(ignore_drivers, S_IWUSR | S_IRUGO, fw_get_ignore_drivers, fw_set_ignore_drivers);
  352. struct bus_attribute *const fw_bus_attrs[] = {
  353. &bus_attr_rescan,
  354. &bus_attr_ignore_drivers,
  355. NULL
  356. };
  357. fw_attr(ne, struct node_entry, capabilities, unsigned int, "0x%06x\n")
  358. fw_attr(ne, struct node_entry, nodeid, unsigned int, "0x%04x\n")
  359. fw_attr(ne, struct node_entry, vendor_id, unsigned int, "0x%06x\n")
  360. fw_attr_td(ne, struct node_entry, vendor_name_kv)
  361. fw_attr(ne, struct node_entry, guid, unsigned long long, "0x%016Lx\n")
  362. fw_attr(ne, struct node_entry, guid_vendor_id, unsigned int, "0x%06x\n")
  363. fw_attr(ne, struct node_entry, in_limbo, int, "%d\n");
  364. static struct device_attribute *const fw_ne_attrs[] = {
  365. &dev_attr_ne_guid,
  366. &dev_attr_ne_guid_vendor_id,
  367. &dev_attr_ne_capabilities,
  368. &dev_attr_ne_vendor_id,
  369. &dev_attr_ne_nodeid,
  370. &dev_attr_bus_options,
  371. #ifdef HPSB_DEBUG_TLABELS
  372. &dev_attr_tlabels_free,
  373. &dev_attr_tlabels_mask,
  374. #endif
  375. };
  376. fw_attr(ud, struct unit_directory, address, unsigned long long, "0x%016Lx\n")
  377. fw_attr(ud, struct unit_directory, length, int, "%d\n")
  378. /* These are all dependent on the value being provided */
  379. fw_attr(ud, struct unit_directory, vendor_id, unsigned int, "0x%06x\n")
  380. fw_attr(ud, struct unit_directory, model_id, unsigned int, "0x%06x\n")
  381. fw_attr(ud, struct unit_directory, specifier_id, unsigned int, "0x%06x\n")
  382. fw_attr(ud, struct unit_directory, version, unsigned int, "0x%06x\n")
  383. fw_attr_td(ud, struct unit_directory, vendor_name_kv)
  384. fw_attr_td(ud, struct unit_directory, model_name_kv)
  385. static struct device_attribute *const fw_ud_attrs[] = {
  386. &dev_attr_ud_address,
  387. &dev_attr_ud_length,
  388. &dev_attr_ignore_driver,
  389. };
  390. fw_attr(host, struct hpsb_host, node_count, int, "%d\n")
  391. fw_attr(host, struct hpsb_host, selfid_count, int, "%d\n")
  392. fw_attr(host, struct hpsb_host, nodes_active, int, "%d\n")
  393. fw_attr(host, struct hpsb_host, in_bus_reset, int, "%d\n")
  394. fw_attr(host, struct hpsb_host, is_root, int, "%d\n")
  395. fw_attr(host, struct hpsb_host, is_cycmst, int, "%d\n")
  396. fw_attr(host, struct hpsb_host, is_irm, int, "%d\n")
  397. fw_attr(host, struct hpsb_host, is_busmgr, int, "%d\n")
  398. static struct device_attribute *const fw_host_attrs[] = {
  399. &dev_attr_host_node_count,
  400. &dev_attr_host_selfid_count,
  401. &dev_attr_host_nodes_active,
  402. &dev_attr_host_in_bus_reset,
  403. &dev_attr_host_is_root,
  404. &dev_attr_host_is_cycmst,
  405. &dev_attr_host_is_irm,
  406. &dev_attr_host_is_busmgr,
  407. };
  408. static ssize_t fw_show_drv_device_ids(struct device_driver *drv, char *buf)
  409. {
  410. struct hpsb_protocol_driver *driver;
  411. struct ieee1394_device_id *id;
  412. int length = 0;
  413. char *scratch = buf;
  414. driver = container_of(drv, struct hpsb_protocol_driver, driver);
  415. id = driver->id_table;
  416. if (!id)
  417. return 0;
  418. for (; id->match_flags != 0; id++) {
  419. int need_coma = 0;
  420. if (id->match_flags & IEEE1394_MATCH_VENDOR_ID) {
  421. length += sprintf(scratch, "vendor_id=0x%06x", id->vendor_id);
  422. scratch = buf + length;
  423. need_coma++;
  424. }
  425. if (id->match_flags & IEEE1394_MATCH_MODEL_ID) {
  426. length += sprintf(scratch, "%smodel_id=0x%06x",
  427. need_coma++ ? "," : "",
  428. id->model_id);
  429. scratch = buf + length;
  430. }
  431. if (id->match_flags & IEEE1394_MATCH_SPECIFIER_ID) {
  432. length += sprintf(scratch, "%sspecifier_id=0x%06x",
  433. need_coma++ ? "," : "",
  434. id->specifier_id);
  435. scratch = buf + length;
  436. }
  437. if (id->match_flags & IEEE1394_MATCH_VERSION) {
  438. length += sprintf(scratch, "%sversion=0x%06x",
  439. need_coma++ ? "," : "",
  440. id->version);
  441. scratch = buf + length;
  442. }
  443. if (need_coma) {
  444. *scratch++ = '\n';
  445. length++;
  446. }
  447. }
  448. return length;
  449. }
  450. static DRIVER_ATTR(device_ids,S_IRUGO,fw_show_drv_device_ids,NULL);
  451. fw_drv_attr(name, const char *, "%s\n")
  452. static struct driver_attribute *const fw_drv_attrs[] = {
  453. &driver_attr_drv_name,
  454. &driver_attr_device_ids,
  455. };
  456. static void nodemgr_create_drv_files(struct hpsb_protocol_driver *driver)
  457. {
  458. struct device_driver *drv = &driver->driver;
  459. int i;
  460. for (i = 0; i < ARRAY_SIZE(fw_drv_attrs); i++)
  461. if (driver_create_file(drv, fw_drv_attrs[i]))
  462. goto fail;
  463. return;
  464. fail:
  465. HPSB_ERR("Failed to add sysfs attribute");
  466. }
  467. static void nodemgr_remove_drv_files(struct hpsb_protocol_driver *driver)
  468. {
  469. struct device_driver *drv = &driver->driver;
  470. int i;
  471. for (i = 0; i < ARRAY_SIZE(fw_drv_attrs); i++)
  472. driver_remove_file(drv, fw_drv_attrs[i]);
  473. }
  474. static void nodemgr_create_ne_dev_files(struct node_entry *ne)
  475. {
  476. struct device *dev = &ne->device;
  477. int i;
  478. for (i = 0; i < ARRAY_SIZE(fw_ne_attrs); i++)
  479. if (device_create_file(dev, fw_ne_attrs[i]))
  480. goto fail;
  481. return;
  482. fail:
  483. HPSB_ERR("Failed to add sysfs attribute");
  484. }
  485. static void nodemgr_create_host_dev_files(struct hpsb_host *host)
  486. {
  487. struct device *dev = &host->device;
  488. int i;
  489. for (i = 0; i < ARRAY_SIZE(fw_host_attrs); i++)
  490. if (device_create_file(dev, fw_host_attrs[i]))
  491. goto fail;
  492. return;
  493. fail:
  494. HPSB_ERR("Failed to add sysfs attribute");
  495. }
  496. static struct node_entry *find_entry_by_nodeid(struct hpsb_host *host,
  497. nodeid_t nodeid);
  498. static void nodemgr_update_host_dev_links(struct hpsb_host *host)
  499. {
  500. struct device *dev = &host->device;
  501. struct node_entry *ne;
  502. sysfs_remove_link(&dev->kobj, "irm_id");
  503. sysfs_remove_link(&dev->kobj, "busmgr_id");
  504. sysfs_remove_link(&dev->kobj, "host_id");
  505. if ((ne = find_entry_by_nodeid(host, host->irm_id)) &&
  506. sysfs_create_link(&dev->kobj, &ne->device.kobj, "irm_id"))
  507. goto fail;
  508. if ((ne = find_entry_by_nodeid(host, host->busmgr_id)) &&
  509. sysfs_create_link(&dev->kobj, &ne->device.kobj, "busmgr_id"))
  510. goto fail;
  511. if ((ne = find_entry_by_nodeid(host, host->node_id)) &&
  512. sysfs_create_link(&dev->kobj, &ne->device.kobj, "host_id"))
  513. goto fail;
  514. return;
  515. fail:
  516. HPSB_ERR("Failed to update sysfs attributes for host %d", host->id);
  517. }
  518. static void nodemgr_create_ud_dev_files(struct unit_directory *ud)
  519. {
  520. struct device *dev = &ud->device;
  521. int i;
  522. for (i = 0; i < ARRAY_SIZE(fw_ud_attrs); i++)
  523. if (device_create_file(dev, fw_ud_attrs[i]))
  524. goto fail;
  525. if (ud->flags & UNIT_DIRECTORY_SPECIFIER_ID)
  526. if (device_create_file(dev, &dev_attr_ud_specifier_id))
  527. goto fail;
  528. if (ud->flags & UNIT_DIRECTORY_VERSION)
  529. if (device_create_file(dev, &dev_attr_ud_version))
  530. goto fail;
  531. if (ud->flags & UNIT_DIRECTORY_VENDOR_ID) {
  532. if (device_create_file(dev, &dev_attr_ud_vendor_id))
  533. goto fail;
  534. if (ud->vendor_name_kv &&
  535. device_create_file(dev, &dev_attr_ud_vendor_name_kv))
  536. goto fail;
  537. }
  538. if (ud->flags & UNIT_DIRECTORY_MODEL_ID) {
  539. if (device_create_file(dev, &dev_attr_ud_model_id))
  540. goto fail;
  541. if (ud->model_name_kv &&
  542. device_create_file(dev, &dev_attr_ud_model_name_kv))
  543. goto fail;
  544. }
  545. return;
  546. fail:
  547. HPSB_ERR("Failed to add sysfs attribute");
  548. }
  549. static int nodemgr_bus_match(struct device * dev, struct device_driver * drv)
  550. {
  551. struct hpsb_protocol_driver *driver;
  552. struct unit_directory *ud;
  553. struct ieee1394_device_id *id;
  554. /* We only match unit directories */
  555. if (dev->platform_data != &nodemgr_ud_platform_data)
  556. return 0;
  557. ud = container_of(dev, struct unit_directory, device);
  558. if (ud->ne->in_limbo || ud->ignore_driver)
  559. return 0;
  560. /* We only match drivers of type hpsb_protocol_driver */
  561. if (drv == &nodemgr_mid_layer_driver)
  562. return 0;
  563. driver = container_of(drv, struct hpsb_protocol_driver, driver);
  564. id = driver->id_table;
  565. if (!id)
  566. return 0;
  567. for (; id->match_flags != 0; id++) {
  568. if ((id->match_flags & IEEE1394_MATCH_VENDOR_ID) &&
  569. id->vendor_id != ud->vendor_id)
  570. continue;
  571. if ((id->match_flags & IEEE1394_MATCH_MODEL_ID) &&
  572. id->model_id != ud->model_id)
  573. continue;
  574. if ((id->match_flags & IEEE1394_MATCH_SPECIFIER_ID) &&
  575. id->specifier_id != ud->specifier_id)
  576. continue;
  577. if ((id->match_flags & IEEE1394_MATCH_VERSION) &&
  578. id->version != ud->version)
  579. continue;
  580. return 1;
  581. }
  582. return 0;
  583. }
  584. static DEFINE_MUTEX(nodemgr_serialize_remove_uds);
  585. static int match_ne(struct device *dev, void *data)
  586. {
  587. struct unit_directory *ud;
  588. struct node_entry *ne = data;
  589. ud = container_of(dev, struct unit_directory, unit_dev);
  590. return ud->ne == ne;
  591. }
  592. static void nodemgr_remove_uds(struct node_entry *ne)
  593. {
  594. struct device *dev;
  595. struct unit_directory *ud;
  596. /* Use class_find device to iterate the devices. Since this code
  597. * may be called from other contexts besides the knodemgrds,
  598. * protect it by nodemgr_serialize_remove_uds.
  599. */
  600. mutex_lock(&nodemgr_serialize_remove_uds);
  601. for (;;) {
  602. dev = class_find_device(&nodemgr_ud_class, NULL, ne, match_ne);
  603. if (!dev)
  604. break;
  605. ud = container_of(dev, struct unit_directory, unit_dev);
  606. put_device(dev);
  607. device_unregister(&ud->unit_dev);
  608. device_unregister(&ud->device);
  609. }
  610. mutex_unlock(&nodemgr_serialize_remove_uds);
  611. }
  612. static void nodemgr_remove_ne(struct node_entry *ne)
  613. {
  614. struct device *dev;
  615. dev = get_device(&ne->device);
  616. if (!dev)
  617. return;
  618. HPSB_DEBUG("Node removed: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
  619. NODE_BUS_ARGS(ne->host, ne->nodeid), (unsigned long long)ne->guid);
  620. nodemgr_remove_uds(ne);
  621. device_unregister(&ne->node_dev);
  622. device_unregister(dev);
  623. put_device(dev);
  624. }
  625. static int remove_host_dev(struct device *dev, void *data)
  626. {
  627. if (dev->bus == &ieee1394_bus_type)
  628. nodemgr_remove_ne(container_of(dev, struct node_entry,
  629. device));
  630. return 0;
  631. }
  632. static void nodemgr_remove_host_dev(struct device *dev)
  633. {
  634. device_for_each_child(dev, NULL, remove_host_dev);
  635. sysfs_remove_link(&dev->kobj, "irm_id");
  636. sysfs_remove_link(&dev->kobj, "busmgr_id");
  637. sysfs_remove_link(&dev->kobj, "host_id");
  638. }
  639. static void nodemgr_update_bus_options(struct node_entry *ne)
  640. {
  641. #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
  642. static const u16 mr[] = { 4, 64, 1024, 0};
  643. #endif
  644. quadlet_t busoptions = be32_to_cpu(ne->csr->bus_info_data[2]);
  645. ne->busopt.irmc = (busoptions >> 31) & 1;
  646. ne->busopt.cmc = (busoptions >> 30) & 1;
  647. ne->busopt.isc = (busoptions >> 29) & 1;
  648. ne->busopt.bmc = (busoptions >> 28) & 1;
  649. ne->busopt.pmc = (busoptions >> 27) & 1;
  650. ne->busopt.cyc_clk_acc = (busoptions >> 16) & 0xff;
  651. ne->busopt.max_rec = 1 << (((busoptions >> 12) & 0xf) + 1);
  652. ne->busopt.max_rom = (busoptions >> 8) & 0x3;
  653. ne->busopt.generation = (busoptions >> 4) & 0xf;
  654. ne->busopt.lnkspd = busoptions & 0x7;
  655. HPSB_VERBOSE("NodeMgr: raw=0x%08x irmc=%d cmc=%d isc=%d bmc=%d pmc=%d "
  656. "cyc_clk_acc=%d max_rec=%d max_rom=%d gen=%d lspd=%d",
  657. busoptions, ne->busopt.irmc, ne->busopt.cmc,
  658. ne->busopt.isc, ne->busopt.bmc, ne->busopt.pmc,
  659. ne->busopt.cyc_clk_acc, ne->busopt.max_rec,
  660. mr[ne->busopt.max_rom],
  661. ne->busopt.generation, ne->busopt.lnkspd);
  662. }
  663. static struct node_entry *nodemgr_create_node(octlet_t guid,
  664. struct csr1212_csr *csr, struct hpsb_host *host,
  665. nodeid_t nodeid, unsigned int generation)
  666. {
  667. struct node_entry *ne;
  668. ne = kzalloc(sizeof(*ne), GFP_KERNEL);
  669. if (!ne)
  670. goto fail_alloc;
  671. ne->host = host;
  672. ne->nodeid = nodeid;
  673. ne->generation = generation;
  674. ne->needs_probe = true;
  675. ne->guid = guid;
  676. ne->guid_vendor_id = (guid >> 40) & 0xffffff;
  677. ne->csr = csr;
  678. memcpy(&ne->device, &nodemgr_dev_template_ne,
  679. sizeof(ne->device));
  680. ne->device.parent = &host->device;
  681. dev_set_name(&ne->device, "%016Lx", (unsigned long long)(ne->guid));
  682. ne->node_dev.parent = &ne->device;
  683. ne->node_dev.class = &nodemgr_ne_class;
  684. dev_set_name(&ne->node_dev, "%016Lx", (unsigned long long)(ne->guid));
  685. if (device_register(&ne->device))
  686. goto fail_devreg;
  687. if (device_register(&ne->node_dev))
  688. goto fail_classdevreg;
  689. get_device(&ne->device);
  690. nodemgr_create_ne_dev_files(ne);
  691. nodemgr_update_bus_options(ne);
  692. HPSB_DEBUG("%s added: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
  693. (host->node_id == nodeid) ? "Host" : "Node",
  694. NODE_BUS_ARGS(host, nodeid), (unsigned long long)guid);
  695. return ne;
  696. fail_classdevreg:
  697. device_unregister(&ne->device);
  698. fail_devreg:
  699. kfree(ne);
  700. fail_alloc:
  701. HPSB_ERR("Failed to create node ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
  702. NODE_BUS_ARGS(host, nodeid), (unsigned long long)guid);
  703. return NULL;
  704. }
  705. static int match_ne_guid(struct device *dev, void *data)
  706. {
  707. struct node_entry *ne;
  708. u64 *guid = data;
  709. ne = container_of(dev, struct node_entry, node_dev);
  710. return ne->guid == *guid;
  711. }
  712. static struct node_entry *find_entry_by_guid(u64 guid)
  713. {
  714. struct device *dev;
  715. struct node_entry *ne;
  716. dev = class_find_device(&nodemgr_ne_class, NULL, &guid, match_ne_guid);
  717. if (!dev)
  718. return NULL;
  719. ne = container_of(dev, struct node_entry, node_dev);
  720. put_device(dev);
  721. return ne;
  722. }
  723. struct match_nodeid_parameter {
  724. struct hpsb_host *host;
  725. nodeid_t nodeid;
  726. };
  727. static int match_ne_nodeid(struct device *dev, void *data)
  728. {
  729. int found = 0;
  730. struct node_entry *ne;
  731. struct match_nodeid_parameter *p = data;
  732. if (!dev)
  733. goto ret;
  734. ne = container_of(dev, struct node_entry, node_dev);
  735. if (ne->host == p->host && ne->nodeid == p->nodeid)
  736. found = 1;
  737. ret:
  738. return found;
  739. }
  740. static struct node_entry *find_entry_by_nodeid(struct hpsb_host *host,
  741. nodeid_t nodeid)
  742. {
  743. struct device *dev;
  744. struct node_entry *ne;
  745. struct match_nodeid_parameter p;
  746. p.host = host;
  747. p.nodeid = nodeid;
  748. dev = class_find_device(&nodemgr_ne_class, NULL, &p, match_ne_nodeid);
  749. if (!dev)
  750. return NULL;
  751. ne = container_of(dev, struct node_entry, node_dev);
  752. put_device(dev);
  753. return ne;
  754. }
  755. static void nodemgr_register_device(struct node_entry *ne,
  756. struct unit_directory *ud, struct device *parent)
  757. {
  758. memcpy(&ud->device, &nodemgr_dev_template_ud,
  759. sizeof(ud->device));
  760. ud->device.parent = parent;
  761. dev_set_name(&ud->device, "%s-%u", dev_name(&ne->device), ud->id);
  762. ud->unit_dev.parent = &ud->device;
  763. ud->unit_dev.class = &nodemgr_ud_class;
  764. dev_set_name(&ud->unit_dev, "%s-%u", dev_name(&ne->device), 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", dev_name(&ud->device));
  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 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. csr1212_keep_keyval(kv);
  828. ud->vendor_name_kv = kv;
  829. break;
  830. case CSR1212_KV_ID_MODEL:
  831. csr1212_keep_keyval(kv);
  832. ud->model_name_kv = 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(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 node_entry *ne)
  904. {
  905. unsigned int ud_id = 0;
  906. struct csr1212_dentry *dentry;
  907. struct csr1212_keyval *kv, *vendor_name_kv = NULL;
  908. u8 last_key_id = 0;
  909. ne->needs_probe = false;
  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(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. csr1212_keep_keyval(kv);
  930. vendor_name_kv = kv;
  931. }
  932. }
  933. break;
  934. }
  935. last_key_id = kv->key.id;
  936. }
  937. if (ne->vendor_name_kv) {
  938. kv = ne->vendor_name_kv;
  939. ne->vendor_name_kv = vendor_name_kv;
  940. csr1212_release_keyval(kv);
  941. } else if (vendor_name_kv) {
  942. ne->vendor_name_kv = vendor_name_kv;
  943. if (device_create_file(&ne->device,
  944. &dev_attr_ne_vendor_name_kv) != 0)
  945. HPSB_ERR("Failed to add sysfs attribute");
  946. }
  947. }
  948. #ifdef CONFIG_HOTPLUG
  949. static int nodemgr_uevent(struct device *dev, struct kobj_uevent_env *env)
  950. {
  951. struct unit_directory *ud;
  952. int retval = 0;
  953. /* ieee1394:venNmoNspNverN */
  954. char buf[8 + 1 + 3 + 8 + 2 + 8 + 2 + 8 + 3 + 8 + 1];
  955. if (!dev)
  956. return -ENODEV;
  957. ud = container_of(dev, struct unit_directory, unit_dev);
  958. if (ud->ne->in_limbo || ud->ignore_driver)
  959. return -ENODEV;
  960. #define PUT_ENVP(fmt,val) \
  961. do { \
  962. retval = add_uevent_var(env, fmt, val); \
  963. if (retval) \
  964. return retval; \
  965. } while (0)
  966. PUT_ENVP("VENDOR_ID=%06x", ud->vendor_id);
  967. PUT_ENVP("MODEL_ID=%06x", ud->model_id);
  968. PUT_ENVP("GUID=%016Lx", (unsigned long long)ud->ne->guid);
  969. PUT_ENVP("SPECIFIER_ID=%06x", ud->specifier_id);
  970. PUT_ENVP("VERSION=%06x", ud->version);
  971. snprintf(buf, sizeof(buf), "ieee1394:ven%08Xmo%08Xsp%08Xver%08X",
  972. ud->vendor_id,
  973. ud->model_id,
  974. ud->specifier_id,
  975. ud->version);
  976. PUT_ENVP("MODALIAS=%s", buf);
  977. #undef PUT_ENVP
  978. return 0;
  979. }
  980. #else
  981. static int nodemgr_uevent(struct device *dev, struct kobj_uevent_env *env)
  982. {
  983. return -ENODEV;
  984. }
  985. #endif /* CONFIG_HOTPLUG */
  986. int __hpsb_register_protocol(struct hpsb_protocol_driver *drv,
  987. struct module *owner)
  988. {
  989. int error;
  990. drv->driver.bus = &ieee1394_bus_type;
  991. drv->driver.owner = owner;
  992. drv->driver.name = drv->name;
  993. /* This will cause a probe for devices */
  994. error = driver_register(&drv->driver);
  995. if (!error)
  996. nodemgr_create_drv_files(drv);
  997. return error;
  998. }
  999. void hpsb_unregister_protocol(struct hpsb_protocol_driver *driver)
  1000. {
  1001. nodemgr_remove_drv_files(driver);
  1002. /* This will subsequently disconnect all devices that our driver
  1003. * is attached to. */
  1004. driver_unregister(&driver->driver);
  1005. }
  1006. /*
  1007. * This function updates nodes that were present on the bus before the
  1008. * reset and still are after the reset. The nodeid and the config rom
  1009. * may have changed, and the drivers managing this device must be
  1010. * informed that this device just went through a bus reset, to allow
  1011. * the to take whatever actions required.
  1012. */
  1013. static void nodemgr_update_node(struct node_entry *ne, struct csr1212_csr *csr,
  1014. nodeid_t nodeid, unsigned int generation)
  1015. {
  1016. if (ne->nodeid != nodeid) {
  1017. HPSB_DEBUG("Node changed: " NODE_BUS_FMT " -> " NODE_BUS_FMT,
  1018. NODE_BUS_ARGS(ne->host, ne->nodeid),
  1019. NODE_BUS_ARGS(ne->host, nodeid));
  1020. ne->nodeid = nodeid;
  1021. }
  1022. if (ne->busopt.generation != ((be32_to_cpu(csr->bus_info_data[2]) >> 4) & 0xf)) {
  1023. kfree(ne->csr->private);
  1024. csr1212_destroy_csr(ne->csr);
  1025. ne->csr = csr;
  1026. /* If the node's configrom generation has changed, we
  1027. * unregister all the unit directories. */
  1028. nodemgr_remove_uds(ne);
  1029. nodemgr_update_bus_options(ne);
  1030. /* Mark the node as new, so it gets re-probed */
  1031. ne->needs_probe = true;
  1032. } else {
  1033. /* old cache is valid, so update its generation */
  1034. struct nodemgr_csr_info *ci = ne->csr->private;
  1035. ci->generation = generation;
  1036. /* free the partially filled now unneeded new cache */
  1037. kfree(csr->private);
  1038. csr1212_destroy_csr(csr);
  1039. }
  1040. /* Mark the node current */
  1041. ne->generation = generation;
  1042. if (ne->in_limbo) {
  1043. device_remove_file(&ne->device, &dev_attr_ne_in_limbo);
  1044. ne->in_limbo = false;
  1045. HPSB_DEBUG("Node reactivated: "
  1046. "ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
  1047. NODE_BUS_ARGS(ne->host, ne->nodeid),
  1048. (unsigned long long)ne->guid);
  1049. }
  1050. }
  1051. static void nodemgr_node_scan_one(struct hpsb_host *host,
  1052. nodeid_t nodeid, int generation)
  1053. {
  1054. struct node_entry *ne;
  1055. octlet_t guid;
  1056. struct csr1212_csr *csr;
  1057. struct nodemgr_csr_info *ci;
  1058. u8 *speed;
  1059. ci = kmalloc(sizeof(*ci), GFP_KERNEL);
  1060. if (!ci)
  1061. return;
  1062. ci->host = host;
  1063. ci->nodeid = nodeid;
  1064. ci->generation = generation;
  1065. /* Prepare for speed probe which occurs when reading the ROM */
  1066. speed = &(host->speed[NODEID_TO_NODE(nodeid)]);
  1067. if (*speed > host->csr.lnk_spd)
  1068. *speed = host->csr.lnk_spd;
  1069. ci->speed_unverified = *speed > IEEE1394_SPEED_100;
  1070. /* We need to detect when the ConfigROM's generation has changed,
  1071. * so we only update the node's info when it needs to be. */
  1072. csr = csr1212_create_csr(&nodemgr_csr_ops, 5 * sizeof(quadlet_t), ci);
  1073. if (!csr || csr1212_parse_csr(csr) != CSR1212_SUCCESS) {
  1074. HPSB_ERR("Error parsing configrom for node " NODE_BUS_FMT,
  1075. NODE_BUS_ARGS(host, nodeid));
  1076. if (csr)
  1077. csr1212_destroy_csr(csr);
  1078. kfree(ci);
  1079. return;
  1080. }
  1081. if (csr->bus_info_data[1] != IEEE1394_BUSID_MAGIC) {
  1082. /* This isn't a 1394 device, but we let it slide. There
  1083. * was a report of a device with broken firmware which
  1084. * reported '2394' instead of '1394', which is obviously a
  1085. * mistake. One would hope that a non-1394 device never
  1086. * gets connected to Firewire bus. If someone does, we
  1087. * shouldn't be held responsible, so we'll allow it with a
  1088. * warning. */
  1089. HPSB_WARN("Node " NODE_BUS_FMT " has invalid busID magic [0x%08x]",
  1090. NODE_BUS_ARGS(host, nodeid), csr->bus_info_data[1]);
  1091. }
  1092. guid = ((u64)be32_to_cpu(csr->bus_info_data[3]) << 32) | be32_to_cpu(csr->bus_info_data[4]);
  1093. ne = find_entry_by_guid(guid);
  1094. if (ne && ne->host != host && ne->in_limbo) {
  1095. /* Must have moved this device from one host to another */
  1096. nodemgr_remove_ne(ne);
  1097. ne = NULL;
  1098. }
  1099. if (!ne)
  1100. nodemgr_create_node(guid, csr, host, nodeid, generation);
  1101. else
  1102. nodemgr_update_node(ne, csr, nodeid, generation);
  1103. }
  1104. static void nodemgr_node_scan(struct hpsb_host *host, int generation)
  1105. {
  1106. int count;
  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(host, nodeid++, generation);
  1118. }
  1119. }
  1120. static void nodemgr_pause_ne(struct node_entry *ne)
  1121. {
  1122. HPSB_DEBUG("Node paused: ID:BUS[" NODE_BUS_FMT "] GUID[%016Lx]",
  1123. NODE_BUS_ARGS(ne->host, ne->nodeid),
  1124. (unsigned long long)ne->guid);
  1125. ne->in_limbo = true;
  1126. WARN_ON(device_create_file(&ne->device, &dev_attr_ne_in_limbo));
  1127. }
  1128. static int update_pdrv(struct device *dev, void *data)
  1129. {
  1130. struct unit_directory *ud;
  1131. struct device_driver *drv;
  1132. struct hpsb_protocol_driver *pdrv;
  1133. struct node_entry *ne = data;
  1134. int error;
  1135. ud = container_of(dev, struct unit_directory, unit_dev);
  1136. if (ud->ne == ne) {
  1137. drv = get_driver(ud->device.driver);
  1138. if (drv) {
  1139. error = 0;
  1140. pdrv = container_of(drv, struct hpsb_protocol_driver,
  1141. driver);
  1142. if (pdrv->update) {
  1143. down(&ud->device.sem);
  1144. error = pdrv->update(ud);
  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 void nodemgr_update_pdrv(struct node_entry *ne)
  1155. {
  1156. class_for_each_device(&nodemgr_ud_class, NULL, ne, update_pdrv);
  1157. }
  1158. /* Write the BROADCAST_CHANNEL as per IEEE1394a 8.3.2.3.11 and 8.4.2.3. This
  1159. * seems like an optional service but in the end it is practically mandatory
  1160. * as a consequence of these clauses.
  1161. *
  1162. * Note that we cannot do a broadcast write to all nodes at once because some
  1163. * pre-1394a devices would hang. */
  1164. static void nodemgr_irm_write_bc(struct node_entry *ne, int generation)
  1165. {
  1166. const u64 bc_addr = (CSR_REGISTER_BASE | CSR_BROADCAST_CHANNEL);
  1167. quadlet_t bc_remote, bc_local;
  1168. int error;
  1169. if (!ne->host->is_irm || ne->generation != generation ||
  1170. ne->nodeid == ne->host->node_id)
  1171. return;
  1172. bc_local = cpu_to_be32(ne->host->csr.broadcast_channel);
  1173. /* Check if the register is implemented and 1394a compliant. */
  1174. error = hpsb_read(ne->host, ne->nodeid, generation, bc_addr, &bc_remote,
  1175. sizeof(bc_remote));
  1176. if (!error && bc_remote & cpu_to_be32(0x80000000) &&
  1177. bc_remote != bc_local)
  1178. hpsb_node_write(ne, bc_addr, &bc_local, sizeof(bc_local));
  1179. }
  1180. static void nodemgr_probe_ne(struct hpsb_host *host, struct node_entry *ne,
  1181. int generation)
  1182. {
  1183. struct device *dev;
  1184. if (ne->host != host || ne->in_limbo)
  1185. return;
  1186. dev = get_device(&ne->device);
  1187. if (!dev)
  1188. return;
  1189. nodemgr_irm_write_bc(ne, generation);
  1190. /* If "needs_probe", then this is either a new or changed node we
  1191. * rescan totally. If the generation matches for an existing node
  1192. * (one that existed prior to the bus reset) we send update calls
  1193. * down to the drivers. Otherwise, this is a dead node and we
  1194. * suspend it. */
  1195. if (ne->needs_probe)
  1196. nodemgr_process_root_directory(ne);
  1197. else if (ne->generation == generation)
  1198. nodemgr_update_pdrv(ne);
  1199. else
  1200. nodemgr_pause_ne(ne);
  1201. put_device(dev);
  1202. }
  1203. struct node_probe_parameter {
  1204. struct hpsb_host *host;
  1205. int generation;
  1206. bool probe_now;
  1207. };
  1208. static int node_probe(struct device *dev, void *data)
  1209. {
  1210. struct node_probe_parameter *p = data;
  1211. struct node_entry *ne;
  1212. if (p->generation != get_hpsb_generation(p->host))
  1213. return -EAGAIN;
  1214. ne = container_of(dev, struct node_entry, node_dev);
  1215. if (ne->needs_probe == p->probe_now)
  1216. nodemgr_probe_ne(p->host, ne, p->generation);
  1217. return 0;
  1218. }
  1219. static int nodemgr_node_probe(struct hpsb_host *host, int generation)
  1220. {
  1221. struct node_probe_parameter p;
  1222. p.host = host;
  1223. p.generation = generation;
  1224. /*
  1225. * Do some processing of the nodes we've probed. This pulls them
  1226. * into the sysfs layer if needed, and can result in processing of
  1227. * unit-directories, or just updating the node and it's
  1228. * unit-directories.
  1229. *
  1230. * Run updates before probes. Usually, updates are time-critical
  1231. * while probes are time-consuming.
  1232. *
  1233. * Meanwhile, another bus reset may have happened. In this case we
  1234. * skip everything here and let the next bus scan handle it.
  1235. * Otherwise we may prematurely remove nodes which are still there.
  1236. */
  1237. p.probe_now = false;
  1238. if (class_for_each_device(&nodemgr_ne_class, NULL, &p, node_probe) != 0)
  1239. return 0;
  1240. p.probe_now = true;
  1241. if (class_for_each_device(&nodemgr_ne_class, NULL, &p, node_probe) != 0)
  1242. return 0;
  1243. /*
  1244. * Now let's tell the bus to rescan our devices. This may seem
  1245. * like overhead, but the driver-model core will only scan a
  1246. * device for a driver when either the device is added, or when a
  1247. * new driver is added. A bus reset is a good reason to rescan
  1248. * devices that were there before. For example, an sbp2 device
  1249. * may become available for login, if the host that held it was
  1250. * just removed.
  1251. */
  1252. if (bus_rescan_devices(&ieee1394_bus_type) != 0)
  1253. HPSB_DEBUG("bus_rescan_devices had an error");
  1254. return 1;
  1255. }
  1256. static int remove_nodes_in_limbo(struct device *dev, void *data)
  1257. {
  1258. struct node_entry *ne;
  1259. if (dev->bus != &ieee1394_bus_type)
  1260. return 0;
  1261. ne = container_of(dev, struct node_entry, device);
  1262. if (ne->in_limbo)
  1263. nodemgr_remove_ne(ne);
  1264. return 0;
  1265. }
  1266. static void nodemgr_remove_nodes_in_limbo(struct hpsb_host *host)
  1267. {
  1268. device_for_each_child(&host->device, NULL, remove_nodes_in_limbo);
  1269. }
  1270. static int nodemgr_send_resume_packet(struct hpsb_host *host)
  1271. {
  1272. struct hpsb_packet *packet;
  1273. int error = -ENOMEM;
  1274. packet = hpsb_make_phypacket(host,
  1275. EXTPHYPACKET_TYPE_RESUME |
  1276. NODEID_TO_NODE(host->node_id) << PHYPACKET_PORT_SHIFT);
  1277. if (packet) {
  1278. packet->no_waiter = 1;
  1279. packet->generation = get_hpsb_generation(host);
  1280. error = hpsb_send_packet(packet);
  1281. }
  1282. if (error)
  1283. HPSB_WARN("fw-host%d: Failed to broadcast resume packet",
  1284. host->id);
  1285. return error;
  1286. }
  1287. /* Perform a few high-level IRM responsibilities. */
  1288. static int nodemgr_do_irm_duties(struct hpsb_host *host, int cycles)
  1289. {
  1290. quadlet_t bc;
  1291. /* if irm_id == -1 then there is no IRM on this bus */
  1292. if (!host->is_irm || host->irm_id == (nodeid_t)-1)
  1293. return 1;
  1294. /* We are a 1394a-2000 compliant IRM. Set the validity bit. */
  1295. host->csr.broadcast_channel |= 0x40000000;
  1296. /* If there is no bus manager then we should set the root node's
  1297. * force_root bit to promote bus stability per the 1394
  1298. * spec. (8.4.2.6) */
  1299. if (host->busmgr_id == 0xffff && host->node_count > 1)
  1300. {
  1301. u16 root_node = host->node_count - 1;
  1302. /* get cycle master capability flag from root node */
  1303. if (host->is_cycmst ||
  1304. (!hpsb_read(host, LOCAL_BUS | root_node, get_hpsb_generation(host),
  1305. (CSR_REGISTER_BASE + CSR_CONFIG_ROM + 2 * sizeof(quadlet_t)),
  1306. &bc, sizeof(quadlet_t)) &&
  1307. be32_to_cpu(bc) & 1 << CSR_CMC_SHIFT))
  1308. hpsb_send_phy_config(host, root_node, -1);
  1309. else {
  1310. HPSB_DEBUG("The root node is not cycle master capable; "
  1311. "selecting a new root node and resetting...");
  1312. if (cycles >= 5) {
  1313. /* Oh screw it! Just leave the bus as it is */
  1314. HPSB_DEBUG("Stopping reset loop for IRM sanity");
  1315. return 1;
  1316. }
  1317. hpsb_send_phy_config(host, NODEID_TO_NODE(host->node_id), -1);
  1318. hpsb_reset_bus(host, LONG_RESET_FORCE_ROOT);
  1319. return 0;
  1320. }
  1321. }
  1322. /* Some devices suspend their ports while being connected to an inactive
  1323. * host adapter, i.e. if connected before the low-level driver is
  1324. * loaded. They become visible either when physically unplugged and
  1325. * replugged, or when receiving a resume packet. Send one once. */
  1326. if (!host->resume_packet_sent && !nodemgr_send_resume_packet(host))
  1327. host->resume_packet_sent = 1;
  1328. return 1;
  1329. }
  1330. /* We need to ensure that if we are not the IRM, that the IRM node is capable of
  1331. * everything we can do, otherwise issue a bus reset and try to become the IRM
  1332. * ourselves. */
  1333. static int nodemgr_check_irm_capability(struct hpsb_host *host, int cycles)
  1334. {
  1335. quadlet_t bc;
  1336. int status;
  1337. if (hpsb_disable_irm || host->is_irm)
  1338. return 1;
  1339. status = hpsb_read(host, LOCAL_BUS | (host->irm_id),
  1340. get_hpsb_generation(host),
  1341. (CSR_REGISTER_BASE | CSR_BROADCAST_CHANNEL),
  1342. &bc, sizeof(quadlet_t));
  1343. if (status < 0 || !(be32_to_cpu(bc) & 0x80000000)) {
  1344. /* The current irm node does not have a valid BROADCAST_CHANNEL
  1345. * register and we do, so reset the bus with force_root set */
  1346. HPSB_DEBUG("Current remote IRM is not 1394a-2000 compliant, resetting...");
  1347. if (cycles >= 5) {
  1348. /* Oh screw it! Just leave the bus as it is */
  1349. HPSB_DEBUG("Stopping reset loop for IRM sanity");
  1350. return 1;
  1351. }
  1352. hpsb_send_phy_config(host, NODEID_TO_NODE(host->node_id), -1);
  1353. hpsb_reset_bus(host, LONG_RESET_FORCE_ROOT);
  1354. return 0;
  1355. }
  1356. return 1;
  1357. }
  1358. static int nodemgr_host_thread(void *data)
  1359. {
  1360. struct hpsb_host *host = data;
  1361. unsigned int g, generation = 0;
  1362. int i, reset_cycles = 0;
  1363. set_freezable();
  1364. /* Setup our device-model entries */
  1365. nodemgr_create_host_dev_files(host);
  1366. for (;;) {
  1367. /* Sleep until next bus reset */
  1368. set_current_state(TASK_INTERRUPTIBLE);
  1369. if (get_hpsb_generation(host) == generation &&
  1370. !kthread_should_stop())
  1371. schedule();
  1372. __set_current_state(TASK_RUNNING);
  1373. /* Thread may have been woken up to freeze or to exit */
  1374. if (try_to_freeze())
  1375. continue;
  1376. if (kthread_should_stop())
  1377. goto exit;
  1378. /* Pause for 1/4 second in 1/16 second intervals,
  1379. * to make sure things settle down. */
  1380. g = get_hpsb_generation(host);
  1381. for (i = 0; i < 4 ; i++) {
  1382. msleep_interruptible(63);
  1383. try_to_freeze();
  1384. if (kthread_should_stop())
  1385. goto exit;
  1386. /* Now get the generation in which the node ID's we collect
  1387. * are valid. During the bus scan we will use this generation
  1388. * for the read transactions, so that if another reset occurs
  1389. * during the scan the transactions will fail instead of
  1390. * returning bogus data. */
  1391. generation = get_hpsb_generation(host);
  1392. /* If we get a reset before we are done waiting, then
  1393. * start the waiting over again */
  1394. if (generation != g)
  1395. g = generation, i = 0;
  1396. }
  1397. if (!nodemgr_check_irm_capability(host, reset_cycles) ||
  1398. !nodemgr_do_irm_duties(host, reset_cycles)) {
  1399. reset_cycles++;
  1400. continue;
  1401. }
  1402. reset_cycles = 0;
  1403. /* Scan our nodes to get the bus options and create node
  1404. * entries. This does not do the sysfs stuff, since that
  1405. * would trigger uevents and such, which is a bad idea at
  1406. * this point. */
  1407. nodemgr_node_scan(host, generation);
  1408. /* This actually does the full probe, with sysfs
  1409. * registration. */
  1410. if (!nodemgr_node_probe(host, generation))
  1411. continue;
  1412. /* Update some of our sysfs symlinks */
  1413. nodemgr_update_host_dev_links(host);
  1414. /* Sleep 3 seconds */
  1415. for (i = 3000/200; i; i--) {
  1416. msleep_interruptible(200);
  1417. try_to_freeze();
  1418. if (kthread_should_stop())
  1419. goto exit;
  1420. if (generation != get_hpsb_generation(host))
  1421. break;
  1422. }
  1423. /* Remove nodes which are gone, unless a bus reset happened */
  1424. if (!i)
  1425. nodemgr_remove_nodes_in_limbo(host);
  1426. }
  1427. exit:
  1428. HPSB_VERBOSE("NodeMgr: Exiting thread");
  1429. return 0;
  1430. }
  1431. struct per_host_parameter {
  1432. void *data;
  1433. int (*cb)(struct hpsb_host *, void *);
  1434. };
  1435. static int per_host(struct device *dev, void *data)
  1436. {
  1437. struct hpsb_host *host;
  1438. struct per_host_parameter *p = data;
  1439. host = container_of(dev, struct hpsb_host, host_dev);
  1440. return p->cb(host, p->data);
  1441. }
  1442. /**
  1443. * nodemgr_for_each_host - call a function for each IEEE 1394 host
  1444. * @data: an address to supply to the callback
  1445. * @cb: function to call for each host
  1446. *
  1447. * Iterate the hosts, calling a given function with supplied data for each host.
  1448. * If the callback fails on a host, i.e. if it returns a non-zero value, the
  1449. * iteration is stopped.
  1450. *
  1451. * Return value: 0 on success, non-zero on failure (same as returned by last run
  1452. * of the callback).
  1453. */
  1454. int nodemgr_for_each_host(void *data, int (*cb)(struct hpsb_host *, void *))
  1455. {
  1456. struct per_host_parameter p;
  1457. p.cb = cb;
  1458. p.data = data;
  1459. return class_for_each_device(&hpsb_host_class, NULL, &p, per_host);
  1460. }
  1461. /* The following two convenience functions use a struct node_entry
  1462. * for addressing a node on the bus. They are intended for use by any
  1463. * process context, not just the nodemgr thread, so we need to be a
  1464. * little careful when reading out the node ID and generation. The
  1465. * thing that can go wrong is that we get the node ID, then a bus
  1466. * reset occurs, and then we read the generation. The node ID is
  1467. * possibly invalid, but the generation is current, and we end up
  1468. * sending a packet to a the wrong node.
  1469. *
  1470. * The solution is to make sure we read the generation first, so that
  1471. * if a reset occurs in the process, we end up with a stale generation
  1472. * and the transactions will fail instead of silently using wrong node
  1473. * ID's.
  1474. */
  1475. /**
  1476. * hpsb_node_fill_packet - fill some destination information into a packet
  1477. * @ne: destination node
  1478. * @packet: packet to fill in
  1479. *
  1480. * This will fill in the given, pre-initialised hpsb_packet with the current
  1481. * information from the node entry (host, node ID, bus generation number).
  1482. */
  1483. void hpsb_node_fill_packet(struct node_entry *ne, struct hpsb_packet *packet)
  1484. {
  1485. packet->host = ne->host;
  1486. packet->generation = ne->generation;
  1487. barrier();
  1488. packet->node_id = ne->nodeid;
  1489. }
  1490. int hpsb_node_write(struct node_entry *ne, u64 addr,
  1491. quadlet_t *buffer, size_t length)
  1492. {
  1493. unsigned int generation = ne->generation;
  1494. barrier();
  1495. return hpsb_write(ne->host, ne->nodeid, generation,
  1496. addr, buffer, length);
  1497. }
  1498. static void nodemgr_add_host(struct hpsb_host *host)
  1499. {
  1500. struct host_info *hi;
  1501. hi = hpsb_create_hostinfo(&nodemgr_highlevel, host, sizeof(*hi));
  1502. if (!hi) {
  1503. HPSB_ERR("NodeMgr: out of memory in add host");
  1504. return;
  1505. }
  1506. hi->host = host;
  1507. hi->thread = kthread_run(nodemgr_host_thread, host, "knodemgrd_%d",
  1508. host->id);
  1509. if (IS_ERR(hi->thread)) {
  1510. HPSB_ERR("NodeMgr: cannot start thread for host %d", host->id);
  1511. hpsb_destroy_hostinfo(&nodemgr_highlevel, host);
  1512. }
  1513. }
  1514. static void nodemgr_host_reset(struct hpsb_host *host)
  1515. {
  1516. struct host_info *hi = hpsb_get_hostinfo(&nodemgr_highlevel, host);
  1517. if (hi) {
  1518. HPSB_VERBOSE("NodeMgr: Processing reset for host %d", host->id);
  1519. wake_up_process(hi->thread);
  1520. }
  1521. }
  1522. static void nodemgr_remove_host(struct hpsb_host *host)
  1523. {
  1524. struct host_info *hi = hpsb_get_hostinfo(&nodemgr_highlevel, host);
  1525. if (hi) {
  1526. kthread_stop(hi->thread);
  1527. nodemgr_remove_host_dev(&host->device);
  1528. }
  1529. }
  1530. static struct hpsb_highlevel nodemgr_highlevel = {
  1531. .name = "Node manager",
  1532. .add_host = nodemgr_add_host,
  1533. .host_reset = nodemgr_host_reset,
  1534. .remove_host = nodemgr_remove_host,
  1535. };
  1536. int init_ieee1394_nodemgr(void)
  1537. {
  1538. int error;
  1539. error = class_register(&nodemgr_ne_class);
  1540. if (error)
  1541. goto fail_ne;
  1542. error = class_register(&nodemgr_ud_class);
  1543. if (error)
  1544. goto fail_ud;
  1545. error = driver_register(&nodemgr_mid_layer_driver);
  1546. if (error)
  1547. goto fail_ml;
  1548. /* This driver is not used if nodemgr is off (disable_nodemgr=1). */
  1549. nodemgr_dev_template_host.driver = &nodemgr_mid_layer_driver;
  1550. hpsb_register_highlevel(&nodemgr_highlevel);
  1551. return 0;
  1552. fail_ml:
  1553. class_unregister(&nodemgr_ud_class);
  1554. fail_ud:
  1555. class_unregister(&nodemgr_ne_class);
  1556. fail_ne:
  1557. return error;
  1558. }
  1559. void cleanup_ieee1394_nodemgr(void)
  1560. {
  1561. hpsb_unregister_highlevel(&nodemgr_highlevel);
  1562. driver_unregister(&nodemgr_mid_layer_driver);
  1563. class_unregister(&nodemgr_ud_class);
  1564. class_unregister(&nodemgr_ne_class);
  1565. }