nodemgr.c 52 KB

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