nodemgr.c 53 KB

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