nodemgr.c 51 KB

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