nodemgr.c 53 KB

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