nodemgr.c 51 KB

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