csr.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. /*
  2. * IEEE 1394 for Linux
  3. *
  4. * CSR implementation, iso/bus manager implementation.
  5. *
  6. * Copyright (C) 1999 Andreas E. Bombe
  7. * 2002 Manfred Weihs <weihs@ict.tuwien.ac.at>
  8. *
  9. * This code is licensed under the GPL. See the file COPYING in the root
  10. * directory of the kernel sources for details.
  11. *
  12. *
  13. * Contributions:
  14. *
  15. * Manfred Weihs <weihs@ict.tuwien.ac.at>
  16. * configuration ROM manipulation
  17. *
  18. */
  19. #include <linux/string.h>
  20. #include <linux/module.h>
  21. #include <linux/moduleparam.h>
  22. #include <linux/param.h>
  23. #include <linux/spinlock.h>
  24. #include "csr1212.h"
  25. #include "ieee1394_types.h"
  26. #include "hosts.h"
  27. #include "ieee1394.h"
  28. #include "highlevel.h"
  29. #include "ieee1394_core.h"
  30. /* Module Parameters */
  31. /* this module parameter can be used to disable mapping of the FCP registers */
  32. static int fcp = 1;
  33. module_param(fcp, int, 0444);
  34. MODULE_PARM_DESC(fcp, "Map FCP registers (default = 1, disable = 0).");
  35. static struct csr1212_keyval *node_cap = NULL;
  36. static void add_host(struct hpsb_host *host);
  37. static void remove_host(struct hpsb_host *host);
  38. static void host_reset(struct hpsb_host *host);
  39. static int read_maps(struct hpsb_host *host, int nodeid, quadlet_t *buffer,
  40. u64 addr, size_t length, u16 fl);
  41. static int write_fcp(struct hpsb_host *host, int nodeid, int dest,
  42. quadlet_t *data, u64 addr, size_t length, u16 flags);
  43. static int read_regs(struct hpsb_host *host, int nodeid, quadlet_t *buf,
  44. u64 addr, size_t length, u16 flags);
  45. static int write_regs(struct hpsb_host *host, int nodeid, int destid,
  46. quadlet_t *data, u64 addr, size_t length, u16 flags);
  47. static int lock_regs(struct hpsb_host *host, int nodeid, quadlet_t *store,
  48. u64 addr, quadlet_t data, quadlet_t arg, int extcode, u16 fl);
  49. static int lock64_regs(struct hpsb_host *host, int nodeid, octlet_t * store,
  50. u64 addr, octlet_t data, octlet_t arg, int extcode, u16 fl);
  51. static int read_config_rom(struct hpsb_host *host, int nodeid, quadlet_t *buffer,
  52. u64 addr, size_t length, u16 fl);
  53. static u64 allocate_addr_range(u64 size, u32 alignment, void *__host);
  54. static void release_addr_range(u64 addr, void *__host);
  55. static struct hpsb_highlevel csr_highlevel = {
  56. .name = "standard registers",
  57. .add_host = add_host,
  58. .remove_host = remove_host,
  59. .host_reset = host_reset,
  60. };
  61. static struct hpsb_address_ops map_ops = {
  62. .read = read_maps,
  63. };
  64. static struct hpsb_address_ops fcp_ops = {
  65. .write = write_fcp,
  66. };
  67. static struct hpsb_address_ops reg_ops = {
  68. .read = read_regs,
  69. .write = write_regs,
  70. .lock = lock_regs,
  71. .lock64 = lock64_regs,
  72. };
  73. static struct hpsb_address_ops config_rom_ops = {
  74. .read = read_config_rom,
  75. };
  76. struct csr1212_bus_ops csr_bus_ops = {
  77. .allocate_addr_range = allocate_addr_range,
  78. .release_addr = release_addr_range,
  79. };
  80. static u16 csr_crc16(unsigned *data, int length)
  81. {
  82. int check=0, i;
  83. int shift, sum, next=0;
  84. for (i = length; i; i--) {
  85. for (next = check, shift = 28; shift >= 0; shift -= 4 ) {
  86. sum = ((next >> 12) ^ (be32_to_cpu(*data) >> shift)) & 0xf;
  87. next = (next << 4) ^ (sum << 12) ^ (sum << 5) ^ (sum);
  88. }
  89. check = next & 0xffff;
  90. data++;
  91. }
  92. return check;
  93. }
  94. static void host_reset(struct hpsb_host *host)
  95. {
  96. host->csr.state &= 0x300;
  97. host->csr.bus_manager_id = 0x3f;
  98. host->csr.bandwidth_available = 4915;
  99. host->csr.channels_available_hi = 0xfffffffe; /* pre-alloc ch 31 per 1394a-2000 */
  100. host->csr.channels_available_lo = ~0;
  101. host->csr.broadcast_channel = 0x80000000 | 31;
  102. if (host->is_irm) {
  103. if (host->driver->hw_csr_reg) {
  104. host->driver->hw_csr_reg(host, 2, 0xfffffffe, ~0);
  105. }
  106. }
  107. host->csr.node_ids = host->node_id << 16;
  108. if (!host->is_root) {
  109. /* clear cmstr bit */
  110. host->csr.state &= ~0x100;
  111. }
  112. host->csr.topology_map[1] =
  113. cpu_to_be32(be32_to_cpu(host->csr.topology_map[1]) + 1);
  114. host->csr.topology_map[2] = cpu_to_be32(host->node_count << 16
  115. | host->selfid_count);
  116. host->csr.topology_map[0] =
  117. cpu_to_be32((host->selfid_count + 2) << 16
  118. | csr_crc16(host->csr.topology_map + 1,
  119. host->selfid_count + 2));
  120. host->csr.speed_map[1] =
  121. cpu_to_be32(be32_to_cpu(host->csr.speed_map[1]) + 1);
  122. host->csr.speed_map[0] = cpu_to_be32(0x3f1 << 16
  123. | csr_crc16(host->csr.speed_map+1,
  124. 0x3f1));
  125. }
  126. /*
  127. * HI == seconds (bits 0:2)
  128. * LO == fraction units of 1/8000 of a second, as per 1394 (bits 19:31)
  129. *
  130. * Convert to units and then to HZ, for comparison to jiffies.
  131. *
  132. * By default this will end up being 800 units, or 100ms (125usec per
  133. * unit).
  134. *
  135. * NOTE: The spec says 1/8000, but also says we can compute based on 1/8192
  136. * like CSR specifies. Should make our math less complex.
  137. */
  138. static inline void calculate_expire(struct csr_control *csr)
  139. {
  140. unsigned long units;
  141. /* Take the seconds, and convert to units */
  142. units = (unsigned long)(csr->split_timeout_hi & 0x07) << 13;
  143. /* Add in the fractional units */
  144. units += (unsigned long)(csr->split_timeout_lo >> 19);
  145. /* Convert to jiffies */
  146. csr->expire = (unsigned long)(units * HZ) >> 13UL;
  147. /* Just to keep from rounding low */
  148. csr->expire++;
  149. HPSB_VERBOSE("CSR: setting expire to %lu, HZ=%u", csr->expire, HZ);
  150. }
  151. static void add_host(struct hpsb_host *host)
  152. {
  153. struct csr1212_keyval *root;
  154. quadlet_t bus_info[CSR_BUS_INFO_SIZE];
  155. hpsb_register_addrspace(&csr_highlevel, host, &reg_ops,
  156. CSR_REGISTER_BASE,
  157. CSR_REGISTER_BASE + CSR_CONFIG_ROM);
  158. hpsb_register_addrspace(&csr_highlevel, host, &config_rom_ops,
  159. CSR_REGISTER_BASE + CSR_CONFIG_ROM,
  160. CSR_REGISTER_BASE + CSR_CONFIG_ROM_END);
  161. if (fcp) {
  162. hpsb_register_addrspace(&csr_highlevel, host, &fcp_ops,
  163. CSR_REGISTER_BASE + CSR_FCP_COMMAND,
  164. CSR_REGISTER_BASE + CSR_FCP_END);
  165. }
  166. hpsb_register_addrspace(&csr_highlevel, host, &map_ops,
  167. CSR_REGISTER_BASE + CSR_TOPOLOGY_MAP,
  168. CSR_REGISTER_BASE + CSR_TOPOLOGY_MAP_END);
  169. hpsb_register_addrspace(&csr_highlevel, host, &map_ops,
  170. CSR_REGISTER_BASE + CSR_SPEED_MAP,
  171. CSR_REGISTER_BASE + CSR_SPEED_MAP_END);
  172. spin_lock_init(&host->csr.lock);
  173. host->csr.state = 0;
  174. host->csr.node_ids = 0;
  175. host->csr.split_timeout_hi = 0;
  176. host->csr.split_timeout_lo = 800 << 19;
  177. calculate_expire(&host->csr);
  178. host->csr.cycle_time = 0;
  179. host->csr.bus_time = 0;
  180. host->csr.bus_manager_id = 0x3f;
  181. host->csr.bandwidth_available = 4915;
  182. host->csr.channels_available_hi = 0xfffffffe; /* pre-alloc ch 31 per 1394a-2000 */
  183. host->csr.channels_available_lo = ~0;
  184. host->csr.broadcast_channel = 0x80000000 | 31;
  185. if (host->is_irm) {
  186. if (host->driver->hw_csr_reg) {
  187. host->driver->hw_csr_reg(host, 2, 0xfffffffe, ~0);
  188. }
  189. }
  190. if (host->csr.max_rec >= 9)
  191. host->csr.max_rom = 2;
  192. else if (host->csr.max_rec >= 5)
  193. host->csr.max_rom = 1;
  194. else
  195. host->csr.max_rom = 0;
  196. host->csr.generation = 2;
  197. bus_info[1] = __constant_cpu_to_be32(0x31333934);
  198. bus_info[2] = cpu_to_be32((hpsb_disable_irm ? 0 : 1 << CSR_IRMC_SHIFT) |
  199. (1 << CSR_CMC_SHIFT) |
  200. (1 << CSR_ISC_SHIFT) |
  201. (0 << CSR_BMC_SHIFT) |
  202. (0 << CSR_PMC_SHIFT) |
  203. (host->csr.cyc_clk_acc << CSR_CYC_CLK_ACC_SHIFT) |
  204. (host->csr.max_rec << CSR_MAX_REC_SHIFT) |
  205. (host->csr.max_rom << CSR_MAX_ROM_SHIFT) |
  206. (host->csr.generation << CSR_GENERATION_SHIFT) |
  207. host->csr.lnk_spd);
  208. bus_info[3] = cpu_to_be32(host->csr.guid_hi);
  209. bus_info[4] = cpu_to_be32(host->csr.guid_lo);
  210. /* The hardware copy of the bus info block will be set later when a
  211. * bus reset is issued. */
  212. csr1212_init_local_csr(host->csr.rom, bus_info, host->csr.max_rom);
  213. root = host->csr.rom->root_kv;
  214. if(csr1212_attach_keyval_to_directory(root, node_cap) != CSR1212_SUCCESS) {
  215. HPSB_ERR("Failed to attach Node Capabilities to root directory");
  216. }
  217. host->update_config_rom = 1;
  218. }
  219. static void remove_host(struct hpsb_host *host)
  220. {
  221. quadlet_t bus_info[CSR_BUS_INFO_SIZE];
  222. bus_info[1] = __constant_cpu_to_be32(0x31333934);
  223. bus_info[2] = cpu_to_be32((0 << CSR_IRMC_SHIFT) |
  224. (0 << CSR_CMC_SHIFT) |
  225. (0 << CSR_ISC_SHIFT) |
  226. (0 << CSR_BMC_SHIFT) |
  227. (0 << CSR_PMC_SHIFT) |
  228. (host->csr.cyc_clk_acc << CSR_CYC_CLK_ACC_SHIFT) |
  229. (host->csr.max_rec << CSR_MAX_REC_SHIFT) |
  230. (0 << CSR_MAX_ROM_SHIFT) |
  231. (0 << CSR_GENERATION_SHIFT) |
  232. host->csr.lnk_spd);
  233. bus_info[3] = cpu_to_be32(host->csr.guid_hi);
  234. bus_info[4] = cpu_to_be32(host->csr.guid_lo);
  235. csr1212_detach_keyval_from_directory(host->csr.rom->root_kv, node_cap);
  236. csr1212_init_local_csr(host->csr.rom, bus_info, 0);
  237. host->update_config_rom = 1;
  238. }
  239. int hpsb_update_config_rom(struct hpsb_host *host, const quadlet_t *new_rom,
  240. size_t buffersize, unsigned char rom_version)
  241. {
  242. unsigned long flags;
  243. int ret;
  244. HPSB_NOTICE("hpsb_update_config_rom() is deprecated");
  245. spin_lock_irqsave(&host->csr.lock, flags);
  246. if (rom_version != host->csr.generation)
  247. ret = -1;
  248. else if (buffersize > host->csr.rom->cache_head->size)
  249. ret = -2;
  250. else {
  251. /* Just overwrite the generated ConfigROM image with new data,
  252. * it can be regenerated later. */
  253. memcpy(host->csr.rom->cache_head->data, new_rom, buffersize);
  254. host->csr.rom->cache_head->len = buffersize;
  255. if (host->driver->set_hw_config_rom)
  256. host->driver->set_hw_config_rom(host, host->csr.rom->bus_info_data);
  257. /* Increment the generation number to keep some sort of sync
  258. * with the newer ConfigROM manipulation method. */
  259. host->csr.generation++;
  260. if (host->csr.generation > 0xf || host->csr.generation < 2)
  261. host->csr.generation = 2;
  262. ret=0;
  263. }
  264. spin_unlock_irqrestore(&host->csr.lock, flags);
  265. return ret;
  266. }
  267. /* Read topology / speed maps and configuration ROM */
  268. static int read_maps(struct hpsb_host *host, int nodeid, quadlet_t *buffer,
  269. u64 addr, size_t length, u16 fl)
  270. {
  271. unsigned long flags;
  272. int csraddr = addr - CSR_REGISTER_BASE;
  273. const char *src;
  274. spin_lock_irqsave(&host->csr.lock, flags);
  275. if (csraddr < CSR_SPEED_MAP) {
  276. src = ((char *)host->csr.topology_map) + csraddr
  277. - CSR_TOPOLOGY_MAP;
  278. } else {
  279. src = ((char *)host->csr.speed_map) + csraddr - CSR_SPEED_MAP;
  280. }
  281. memcpy(buffer, src, length);
  282. spin_unlock_irqrestore(&host->csr.lock, flags);
  283. return RCODE_COMPLETE;
  284. }
  285. #define out if (--length == 0) break
  286. static int read_regs(struct hpsb_host *host, int nodeid, quadlet_t *buf,
  287. u64 addr, size_t length, u16 flags)
  288. {
  289. int csraddr = addr - CSR_REGISTER_BASE;
  290. int oldcycle;
  291. quadlet_t ret;
  292. if ((csraddr | length) & 0x3)
  293. return RCODE_TYPE_ERROR;
  294. length /= 4;
  295. switch (csraddr) {
  296. case CSR_STATE_CLEAR:
  297. *(buf++) = cpu_to_be32(host->csr.state);
  298. out;
  299. case CSR_STATE_SET:
  300. *(buf++) = cpu_to_be32(host->csr.state);
  301. out;
  302. case CSR_NODE_IDS:
  303. *(buf++) = cpu_to_be32(host->csr.node_ids);
  304. out;
  305. case CSR_RESET_START:
  306. return RCODE_TYPE_ERROR;
  307. /* address gap - handled by default below */
  308. case CSR_SPLIT_TIMEOUT_HI:
  309. *(buf++) = cpu_to_be32(host->csr.split_timeout_hi);
  310. out;
  311. case CSR_SPLIT_TIMEOUT_LO:
  312. *(buf++) = cpu_to_be32(host->csr.split_timeout_lo);
  313. out;
  314. /* address gap */
  315. return RCODE_ADDRESS_ERROR;
  316. case CSR_CYCLE_TIME:
  317. oldcycle = host->csr.cycle_time;
  318. host->csr.cycle_time =
  319. host->driver->devctl(host, GET_CYCLE_COUNTER, 0);
  320. if (oldcycle > host->csr.cycle_time) {
  321. /* cycle time wrapped around */
  322. host->csr.bus_time += 1 << 7;
  323. }
  324. *(buf++) = cpu_to_be32(host->csr.cycle_time);
  325. out;
  326. case CSR_BUS_TIME:
  327. oldcycle = host->csr.cycle_time;
  328. host->csr.cycle_time =
  329. host->driver->devctl(host, GET_CYCLE_COUNTER, 0);
  330. if (oldcycle > host->csr.cycle_time) {
  331. /* cycle time wrapped around */
  332. host->csr.bus_time += (1 << 7);
  333. }
  334. *(buf++) = cpu_to_be32(host->csr.bus_time
  335. | (host->csr.cycle_time >> 25));
  336. out;
  337. /* address gap */
  338. return RCODE_ADDRESS_ERROR;
  339. case CSR_BUSY_TIMEOUT:
  340. /* not yet implemented */
  341. return RCODE_ADDRESS_ERROR;
  342. case CSR_BUS_MANAGER_ID:
  343. if (host->driver->hw_csr_reg)
  344. ret = host->driver->hw_csr_reg(host, 0, 0, 0);
  345. else
  346. ret = host->csr.bus_manager_id;
  347. *(buf++) = cpu_to_be32(ret);
  348. out;
  349. case CSR_BANDWIDTH_AVAILABLE:
  350. if (host->driver->hw_csr_reg)
  351. ret = host->driver->hw_csr_reg(host, 1, 0, 0);
  352. else
  353. ret = host->csr.bandwidth_available;
  354. *(buf++) = cpu_to_be32(ret);
  355. out;
  356. case CSR_CHANNELS_AVAILABLE_HI:
  357. if (host->driver->hw_csr_reg)
  358. ret = host->driver->hw_csr_reg(host, 2, 0, 0);
  359. else
  360. ret = host->csr.channels_available_hi;
  361. *(buf++) = cpu_to_be32(ret);
  362. out;
  363. case CSR_CHANNELS_AVAILABLE_LO:
  364. if (host->driver->hw_csr_reg)
  365. ret = host->driver->hw_csr_reg(host, 3, 0, 0);
  366. else
  367. ret = host->csr.channels_available_lo;
  368. *(buf++) = cpu_to_be32(ret);
  369. out;
  370. case CSR_BROADCAST_CHANNEL:
  371. *(buf++) = cpu_to_be32(host->csr.broadcast_channel);
  372. out;
  373. /* address gap to end - fall through to default */
  374. default:
  375. return RCODE_ADDRESS_ERROR;
  376. }
  377. return RCODE_COMPLETE;
  378. }
  379. static int write_regs(struct hpsb_host *host, int nodeid, int destid,
  380. quadlet_t *data, u64 addr, size_t length, u16 flags)
  381. {
  382. int csraddr = addr - CSR_REGISTER_BASE;
  383. if ((csraddr | length) & 0x3)
  384. return RCODE_TYPE_ERROR;
  385. length /= 4;
  386. switch (csraddr) {
  387. case CSR_STATE_CLEAR:
  388. /* FIXME FIXME FIXME */
  389. printk("doh, someone wants to mess with state clear\n");
  390. out;
  391. case CSR_STATE_SET:
  392. printk("doh, someone wants to mess with state set\n");
  393. out;
  394. case CSR_NODE_IDS:
  395. host->csr.node_ids &= NODE_MASK << 16;
  396. host->csr.node_ids |= be32_to_cpu(*(data++)) & (BUS_MASK << 16);
  397. host->node_id = host->csr.node_ids >> 16;
  398. host->driver->devctl(host, SET_BUS_ID, host->node_id >> 6);
  399. out;
  400. case CSR_RESET_START:
  401. /* FIXME - perform command reset */
  402. out;
  403. /* address gap */
  404. return RCODE_ADDRESS_ERROR;
  405. case CSR_SPLIT_TIMEOUT_HI:
  406. host->csr.split_timeout_hi =
  407. be32_to_cpu(*(data++)) & 0x00000007;
  408. calculate_expire(&host->csr);
  409. out;
  410. case CSR_SPLIT_TIMEOUT_LO:
  411. host->csr.split_timeout_lo =
  412. be32_to_cpu(*(data++)) & 0xfff80000;
  413. calculate_expire(&host->csr);
  414. out;
  415. /* address gap */
  416. return RCODE_ADDRESS_ERROR;
  417. case CSR_CYCLE_TIME:
  418. /* should only be set by cycle start packet, automatically */
  419. host->csr.cycle_time = be32_to_cpu(*data);
  420. host->driver->devctl(host, SET_CYCLE_COUNTER,
  421. be32_to_cpu(*(data++)));
  422. out;
  423. case CSR_BUS_TIME:
  424. host->csr.bus_time = be32_to_cpu(*(data++)) & 0xffffff80;
  425. out;
  426. /* address gap */
  427. return RCODE_ADDRESS_ERROR;
  428. case CSR_BUSY_TIMEOUT:
  429. /* not yet implemented */
  430. return RCODE_ADDRESS_ERROR;
  431. case CSR_BUS_MANAGER_ID:
  432. case CSR_BANDWIDTH_AVAILABLE:
  433. case CSR_CHANNELS_AVAILABLE_HI:
  434. case CSR_CHANNELS_AVAILABLE_LO:
  435. /* these are not writable, only lockable */
  436. return RCODE_TYPE_ERROR;
  437. case CSR_BROADCAST_CHANNEL:
  438. /* only the valid bit can be written */
  439. host->csr.broadcast_channel = (host->csr.broadcast_channel & ~0x40000000)
  440. | (be32_to_cpu(*data) & 0x40000000);
  441. out;
  442. /* address gap to end - fall through */
  443. default:
  444. return RCODE_ADDRESS_ERROR;
  445. }
  446. return RCODE_COMPLETE;
  447. }
  448. #undef out
  449. static int lock_regs(struct hpsb_host *host, int nodeid, quadlet_t *store,
  450. u64 addr, quadlet_t data, quadlet_t arg, int extcode, u16 fl)
  451. {
  452. int csraddr = addr - CSR_REGISTER_BASE;
  453. unsigned long flags;
  454. quadlet_t *regptr = NULL;
  455. if (csraddr & 0x3)
  456. return RCODE_TYPE_ERROR;
  457. if (csraddr < CSR_BUS_MANAGER_ID || csraddr > CSR_CHANNELS_AVAILABLE_LO
  458. || extcode != EXTCODE_COMPARE_SWAP)
  459. goto unsupported_lockreq;
  460. data = be32_to_cpu(data);
  461. arg = be32_to_cpu(arg);
  462. /* Is somebody releasing the broadcast_channel on us? */
  463. if (csraddr == CSR_CHANNELS_AVAILABLE_HI && (data & 0x1)) {
  464. /* Note: this is may not be the right way to handle
  465. * the problem, so we should look into the proper way
  466. * eventually. */
  467. HPSB_WARN("Node [" NODE_BUS_FMT "] wants to release "
  468. "broadcast channel 31. Ignoring.",
  469. NODE_BUS_ARGS(host, nodeid));
  470. data &= ~0x1; /* keep broadcast channel allocated */
  471. }
  472. if (host->driver->hw_csr_reg) {
  473. quadlet_t old;
  474. old = host->driver->
  475. hw_csr_reg(host, (csraddr - CSR_BUS_MANAGER_ID) >> 2,
  476. data, arg);
  477. *store = cpu_to_be32(old);
  478. return RCODE_COMPLETE;
  479. }
  480. spin_lock_irqsave(&host->csr.lock, flags);
  481. switch (csraddr) {
  482. case CSR_BUS_MANAGER_ID:
  483. regptr = &host->csr.bus_manager_id;
  484. *store = cpu_to_be32(*regptr);
  485. if (*regptr == arg)
  486. *regptr = data;
  487. break;
  488. case CSR_BANDWIDTH_AVAILABLE:
  489. {
  490. quadlet_t bandwidth;
  491. quadlet_t old;
  492. quadlet_t new;
  493. regptr = &host->csr.bandwidth_available;
  494. old = *regptr;
  495. /* bandwidth available algorithm adapted from IEEE 1394a-2000 spec */
  496. if (arg > 0x1fff) {
  497. *store = cpu_to_be32(old); /* change nothing */
  498. break;
  499. }
  500. data &= 0x1fff;
  501. if (arg >= data) {
  502. /* allocate bandwidth */
  503. bandwidth = arg - data;
  504. if (old >= bandwidth) {
  505. new = old - bandwidth;
  506. *store = cpu_to_be32(arg);
  507. *regptr = new;
  508. } else {
  509. *store = cpu_to_be32(old);
  510. }
  511. } else {
  512. /* deallocate bandwidth */
  513. bandwidth = data - arg;
  514. if (old + bandwidth < 0x2000) {
  515. new = old + bandwidth;
  516. *store = cpu_to_be32(arg);
  517. *regptr = new;
  518. } else {
  519. *store = cpu_to_be32(old);
  520. }
  521. }
  522. break;
  523. }
  524. case CSR_CHANNELS_AVAILABLE_HI:
  525. {
  526. /* Lock algorithm for CHANNELS_AVAILABLE as recommended by 1394a-2000 */
  527. quadlet_t affected_channels = arg ^ data;
  528. regptr = &host->csr.channels_available_hi;
  529. if ((arg & affected_channels) == (*regptr & affected_channels)) {
  530. *regptr ^= affected_channels;
  531. *store = cpu_to_be32(arg);
  532. } else {
  533. *store = cpu_to_be32(*regptr);
  534. }
  535. break;
  536. }
  537. case CSR_CHANNELS_AVAILABLE_LO:
  538. {
  539. /* Lock algorithm for CHANNELS_AVAILABLE as recommended by 1394a-2000 */
  540. quadlet_t affected_channels = arg ^ data;
  541. regptr = &host->csr.channels_available_lo;
  542. if ((arg & affected_channels) == (*regptr & affected_channels)) {
  543. *regptr ^= affected_channels;
  544. *store = cpu_to_be32(arg);
  545. } else {
  546. *store = cpu_to_be32(*regptr);
  547. }
  548. break;
  549. }
  550. }
  551. spin_unlock_irqrestore(&host->csr.lock, flags);
  552. return RCODE_COMPLETE;
  553. unsupported_lockreq:
  554. switch (csraddr) {
  555. case CSR_STATE_CLEAR:
  556. case CSR_STATE_SET:
  557. case CSR_RESET_START:
  558. case CSR_NODE_IDS:
  559. case CSR_SPLIT_TIMEOUT_HI:
  560. case CSR_SPLIT_TIMEOUT_LO:
  561. case CSR_CYCLE_TIME:
  562. case CSR_BUS_TIME:
  563. case CSR_BROADCAST_CHANNEL:
  564. return RCODE_TYPE_ERROR;
  565. case CSR_BUSY_TIMEOUT:
  566. /* not yet implemented - fall through */
  567. default:
  568. return RCODE_ADDRESS_ERROR;
  569. }
  570. }
  571. static int lock64_regs(struct hpsb_host *host, int nodeid, octlet_t * store,
  572. u64 addr, octlet_t data, octlet_t arg, int extcode, u16 fl)
  573. {
  574. int csraddr = addr - CSR_REGISTER_BASE;
  575. unsigned long flags;
  576. data = be64_to_cpu(data);
  577. arg = be64_to_cpu(arg);
  578. if (csraddr & 0x3)
  579. return RCODE_TYPE_ERROR;
  580. if (csraddr != CSR_CHANNELS_AVAILABLE
  581. || extcode != EXTCODE_COMPARE_SWAP)
  582. goto unsupported_lock64req;
  583. /* Is somebody releasing the broadcast_channel on us? */
  584. if (csraddr == CSR_CHANNELS_AVAILABLE_HI && (data & 0x100000000ULL)) {
  585. /* Note: this is may not be the right way to handle
  586. * the problem, so we should look into the proper way
  587. * eventually. */
  588. HPSB_WARN("Node [" NODE_BUS_FMT "] wants to release "
  589. "broadcast channel 31. Ignoring.",
  590. NODE_BUS_ARGS(host, nodeid));
  591. data &= ~0x100000000ULL; /* keep broadcast channel allocated */
  592. }
  593. if (host->driver->hw_csr_reg) {
  594. quadlet_t data_hi, data_lo;
  595. quadlet_t arg_hi, arg_lo;
  596. quadlet_t old_hi, old_lo;
  597. data_hi = data >> 32;
  598. data_lo = data & 0xFFFFFFFF;
  599. arg_hi = arg >> 32;
  600. arg_lo = arg & 0xFFFFFFFF;
  601. old_hi = host->driver->hw_csr_reg(host, (csraddr - CSR_BUS_MANAGER_ID) >> 2,
  602. data_hi, arg_hi);
  603. old_lo = host->driver->hw_csr_reg(host, ((csraddr + 4) - CSR_BUS_MANAGER_ID) >> 2,
  604. data_lo, arg_lo);
  605. *store = cpu_to_be64(((octlet_t)old_hi << 32) | old_lo);
  606. } else {
  607. octlet_t old;
  608. octlet_t affected_channels = arg ^ data;
  609. spin_lock_irqsave(&host->csr.lock, flags);
  610. old = ((octlet_t)host->csr.channels_available_hi << 32) | host->csr.channels_available_lo;
  611. if ((arg & affected_channels) == (old & affected_channels)) {
  612. host->csr.channels_available_hi ^= (affected_channels >> 32);
  613. host->csr.channels_available_lo ^= (affected_channels & 0xffffffff);
  614. *store = cpu_to_be64(arg);
  615. } else {
  616. *store = cpu_to_be64(old);
  617. }
  618. spin_unlock_irqrestore(&host->csr.lock, flags);
  619. }
  620. /* Is somebody erroneously releasing the broadcast_channel on us? */
  621. if (host->csr.channels_available_hi & 0x1)
  622. host->csr.channels_available_hi &= ~0x1;
  623. return RCODE_COMPLETE;
  624. unsupported_lock64req:
  625. switch (csraddr) {
  626. case CSR_STATE_CLEAR:
  627. case CSR_STATE_SET:
  628. case CSR_RESET_START:
  629. case CSR_NODE_IDS:
  630. case CSR_SPLIT_TIMEOUT_HI:
  631. case CSR_SPLIT_TIMEOUT_LO:
  632. case CSR_CYCLE_TIME:
  633. case CSR_BUS_TIME:
  634. case CSR_BUS_MANAGER_ID:
  635. case CSR_BROADCAST_CHANNEL:
  636. case CSR_BUSY_TIMEOUT:
  637. case CSR_BANDWIDTH_AVAILABLE:
  638. return RCODE_TYPE_ERROR;
  639. default:
  640. return RCODE_ADDRESS_ERROR;
  641. }
  642. }
  643. static int write_fcp(struct hpsb_host *host, int nodeid, int dest,
  644. quadlet_t *data, u64 addr, size_t length, u16 flags)
  645. {
  646. int csraddr = addr - CSR_REGISTER_BASE;
  647. if (length > 512)
  648. return RCODE_TYPE_ERROR;
  649. switch (csraddr) {
  650. case CSR_FCP_COMMAND:
  651. highlevel_fcp_request(host, nodeid, 0, (u8 *)data, length);
  652. break;
  653. case CSR_FCP_RESPONSE:
  654. highlevel_fcp_request(host, nodeid, 1, (u8 *)data, length);
  655. break;
  656. default:
  657. return RCODE_TYPE_ERROR;
  658. }
  659. return RCODE_COMPLETE;
  660. }
  661. static int read_config_rom(struct hpsb_host *host, int nodeid, quadlet_t *buffer,
  662. u64 addr, size_t length, u16 fl)
  663. {
  664. u32 offset = addr - CSR1212_REGISTER_SPACE_BASE;
  665. if (csr1212_read(host->csr.rom, offset, buffer, length) == CSR1212_SUCCESS)
  666. return RCODE_COMPLETE;
  667. else
  668. return RCODE_ADDRESS_ERROR;
  669. }
  670. static u64 allocate_addr_range(u64 size, u32 alignment, void *__host)
  671. {
  672. struct hpsb_host *host = (struct hpsb_host*)__host;
  673. return hpsb_allocate_and_register_addrspace(&csr_highlevel,
  674. host,
  675. &config_rom_ops,
  676. size, alignment,
  677. CSR1212_UNITS_SPACE_BASE,
  678. CSR1212_UNITS_SPACE_END);
  679. }
  680. static void release_addr_range(u64 addr, void *__host)
  681. {
  682. struct hpsb_host *host = (struct hpsb_host*)__host;
  683. hpsb_unregister_addrspace(&csr_highlevel, host, addr);
  684. }
  685. int init_csr(void)
  686. {
  687. node_cap = csr1212_new_immediate(CSR1212_KV_ID_NODE_CAPABILITIES, 0x0083c0);
  688. if (!node_cap) {
  689. HPSB_ERR("Failed to allocate memory for Node Capabilties ConfigROM entry!");
  690. return -ENOMEM;
  691. }
  692. hpsb_register_highlevel(&csr_highlevel);
  693. return 0;
  694. }
  695. void cleanup_csr(void)
  696. {
  697. if (node_cap)
  698. csr1212_release_keyval(node_cap);
  699. hpsb_unregister_highlevel(&csr_highlevel);
  700. }