nodemgr.c 51 KB

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