nodemgr.c 53 KB

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