device.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*
  2. * Functions to handle I2O devices
  3. *
  4. * Copyright (C) 2004 Markus Lidel <Markus.Lidel@shadowconnect.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * Fixes/additions:
  12. * Markus Lidel <Markus.Lidel@shadowconnect.com>
  13. * initial version.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/i2o.h>
  17. #include <linux/delay.h>
  18. #include <linux/string.h>
  19. #include <linux/slab.h>
  20. #include "core.h"
  21. /**
  22. * i2o_device_issue_claim - claim or release a device
  23. * @dev: I2O device to claim or release
  24. * @cmd: claim or release command
  25. * @type: type of claim
  26. *
  27. * Issue I2O UTIL_CLAIM or UTIL_RELEASE messages. The message to be sent
  28. * is set by cmd. dev is the I2O device which should be claim or
  29. * released and the type is the claim type (see the I2O spec).
  30. *
  31. * Returs 0 on success or negative error code on failure.
  32. */
  33. static inline int i2o_device_issue_claim(struct i2o_device *dev, u32 cmd,
  34. u32 type)
  35. {
  36. struct i2o_message *msg;
  37. msg = i2o_msg_get_wait(dev->iop, I2O_TIMEOUT_MESSAGE_GET);
  38. if (IS_ERR(msg))
  39. return PTR_ERR(msg);
  40. msg->u.head[0] = cpu_to_le32(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0);
  41. msg->u.head[1] =
  42. cpu_to_le32(cmd << 24 | HOST_TID << 12 | dev->lct_data.tid);
  43. msg->body[0] = cpu_to_le32(type);
  44. return i2o_msg_post_wait(dev->iop, msg, 60);
  45. }
  46. /**
  47. * i2o_device_claim - claim a device for use by an OSM
  48. * @dev: I2O device to claim
  49. * @drv: I2O driver which wants to claim the device
  50. *
  51. * Do the leg work to assign a device to a given OSM. If the claim succeed
  52. * the owner of the rimary. If the attempt fails a negative errno code
  53. * is returned. On success zero is returned.
  54. */
  55. int i2o_device_claim(struct i2o_device *dev)
  56. {
  57. int rc = 0;
  58. down(&dev->lock);
  59. rc = i2o_device_issue_claim(dev, I2O_CMD_UTIL_CLAIM, I2O_CLAIM_PRIMARY);
  60. if (!rc)
  61. pr_debug("i2o: claim of device %d succeded\n",
  62. dev->lct_data.tid);
  63. else
  64. pr_debug("i2o: claim of device %d failed %d\n",
  65. dev->lct_data.tid, rc);
  66. up(&dev->lock);
  67. return rc;
  68. }
  69. /**
  70. * i2o_device_claim_release - release a device that the OSM is using
  71. * @dev: device to release
  72. * @drv: driver which claimed the device
  73. *
  74. * Drop a claim by an OSM on a given I2O device.
  75. *
  76. * AC - some devices seem to want to refuse an unclaim until they have
  77. * finished internal processing. It makes sense since you don't want a
  78. * new device to go reconfiguring the entire system until you are done.
  79. * Thus we are prepared to wait briefly.
  80. *
  81. * Returns 0 on success or negative error code on failure.
  82. */
  83. int i2o_device_claim_release(struct i2o_device *dev)
  84. {
  85. int tries;
  86. int rc = 0;
  87. down(&dev->lock);
  88. /*
  89. * If the controller takes a nonblocking approach to
  90. * releases we have to sleep/poll for a few times.
  91. */
  92. for (tries = 0; tries < 10; tries++) {
  93. rc = i2o_device_issue_claim(dev, I2O_CMD_UTIL_RELEASE,
  94. I2O_CLAIM_PRIMARY);
  95. if (!rc)
  96. break;
  97. ssleep(1);
  98. }
  99. if (!rc)
  100. pr_debug("i2o: claim release of device %d succeded\n",
  101. dev->lct_data.tid);
  102. else
  103. pr_debug("i2o: claim release of device %d failed %d\n",
  104. dev->lct_data.tid, rc);
  105. up(&dev->lock);
  106. return rc;
  107. }
  108. /**
  109. * i2o_device_release - release the memory for a I2O device
  110. * @dev: I2O device which should be released
  111. *
  112. * Release the allocated memory. This function is called if refcount of
  113. * device reaches 0 automatically.
  114. */
  115. static void i2o_device_release(struct device *dev)
  116. {
  117. struct i2o_device *i2o_dev = to_i2o_device(dev);
  118. pr_debug("i2o: device %s released\n", dev->bus_id);
  119. kfree(i2o_dev);
  120. }
  121. /**
  122. * i2o_device_class_show_class_id - Displays class id of I2O device
  123. * @cd: class device of which the class id should be displayed
  124. * @buf: buffer into which the class id should be printed
  125. *
  126. * Returns the number of bytes which are printed into the buffer.
  127. */
  128. static ssize_t i2o_device_show_class_id(struct device *dev,
  129. struct device_attribute *attr,
  130. char *buf)
  131. {
  132. struct i2o_device *i2o_dev = to_i2o_device(dev);
  133. sprintf(buf, "0x%03x\n", i2o_dev->lct_data.class_id);
  134. return strlen(buf) + 1;
  135. }
  136. /**
  137. * i2o_device_class_show_tid - Displays TID of I2O device
  138. * @cd: class device of which the TID should be displayed
  139. * @buf: buffer into which the class id should be printed
  140. *
  141. * Returns the number of bytes which are printed into the buffer.
  142. */
  143. static ssize_t i2o_device_show_tid(struct device *dev,
  144. struct device_attribute *attr,
  145. char *buf)
  146. {
  147. struct i2o_device *i2o_dev = to_i2o_device(dev);
  148. sprintf(buf, "0x%03x\n", i2o_dev->lct_data.tid);
  149. return strlen(buf) + 1;
  150. }
  151. struct device_attribute i2o_device_attrs[] = {
  152. __ATTR(class_id, S_IRUGO, i2o_device_show_class_id, NULL),
  153. __ATTR(tid, S_IRUGO, i2o_device_show_tid, NULL),
  154. __ATTR_NULL
  155. };
  156. /**
  157. * i2o_device_alloc - Allocate a I2O device and initialize it
  158. *
  159. * Allocate the memory for a I2O device and initialize locks and lists
  160. *
  161. * Returns the allocated I2O device or a negative error code if the device
  162. * could not be allocated.
  163. */
  164. static struct i2o_device *i2o_device_alloc(void)
  165. {
  166. struct i2o_device *dev;
  167. dev = kmalloc(sizeof(*dev), GFP_KERNEL);
  168. if (!dev)
  169. return ERR_PTR(-ENOMEM);
  170. memset(dev, 0, sizeof(*dev));
  171. INIT_LIST_HEAD(&dev->list);
  172. init_MUTEX(&dev->lock);
  173. dev->device.bus = &i2o_bus_type;
  174. dev->device.release = &i2o_device_release;
  175. return dev;
  176. }
  177. /**
  178. * i2o_setup_sysfs_links - Adds attributes to the I2O device
  179. * @cd: I2O class device which is added to the I2O device class
  180. *
  181. * This function get called when a I2O device is added to the class. It
  182. * creates the attributes for each device and creates user/parent symlink
  183. * if necessary.
  184. *
  185. * Returns 0 on success or negative error code on failure.
  186. */
  187. static void i2o_setup_sysfs_links(struct i2o_device *i2o_dev)
  188. {
  189. struct i2o_controller *c = i2o_dev->iop;
  190. struct i2o_device *tmp;
  191. /* create user entries for this device */
  192. tmp = i2o_iop_find_device(i2o_dev->iop, i2o_dev->lct_data.user_tid);
  193. if (tmp && tmp != i2o_dev)
  194. sysfs_create_link(&i2o_dev->device.kobj,
  195. &tmp->device.kobj, "user");
  196. /* create user entries refering to this device */
  197. list_for_each_entry(tmp, &c->devices, list)
  198. if (tmp->lct_data.user_tid == i2o_dev->lct_data.tid &&
  199. tmp != i2o_dev)
  200. sysfs_create_link(&tmp->device.kobj,
  201. &i2o_dev->device.kobj, "user");
  202. /* create parent entries for this device */
  203. tmp = i2o_iop_find_device(i2o_dev->iop, i2o_dev->lct_data.parent_tid);
  204. if (tmp && tmp != i2o_dev)
  205. sysfs_create_link(&i2o_dev->device.kobj,
  206. &tmp->device.kobj, "parent");
  207. /* create parent entries refering to this device */
  208. list_for_each_entry(tmp, &c->devices, list)
  209. if (tmp->lct_data.parent_tid == i2o_dev->lct_data.tid &&
  210. tmp != i2o_dev)
  211. sysfs_create_link(&tmp->device.kobj,
  212. &i2o_dev->device.kobj, "parent");
  213. }
  214. static void i2o_remove_sysfs_links(struct i2o_device *i2o_dev)
  215. {
  216. struct i2o_controller *c = i2o_dev->iop;
  217. struct i2o_device *tmp;
  218. sysfs_remove_link(&i2o_dev->device.kobj, "parent");
  219. sysfs_remove_link(&i2o_dev->device.kobj, "user");
  220. list_for_each_entry(tmp, &c->devices, list) {
  221. if (tmp->lct_data.parent_tid == i2o_dev->lct_data.tid)
  222. sysfs_remove_link(&tmp->device.kobj, "parent");
  223. if (tmp->lct_data.user_tid == i2o_dev->lct_data.tid)
  224. sysfs_remove_link(&tmp->device.kobj, "user");
  225. }
  226. }
  227. /**
  228. * i2o_device_add - allocate a new I2O device and add it to the IOP
  229. * @iop: I2O controller where the device is on
  230. * @entry: LCT entry of the I2O device
  231. *
  232. * Allocate a new I2O device and initialize it with the LCT entry. The
  233. * device is appended to the device list of the controller.
  234. *
  235. * Returns a pointer to the I2O device on success or negative error code
  236. * on failure.
  237. */
  238. static struct i2o_device *i2o_device_add(struct i2o_controller *c,
  239. i2o_lct_entry * entry)
  240. {
  241. struct i2o_device *dev;
  242. dev = i2o_device_alloc();
  243. if (IS_ERR(dev)) {
  244. printk(KERN_ERR "i2o: unable to allocate i2o device\n");
  245. return dev;
  246. }
  247. dev->lct_data = *entry;
  248. dev->iop = c;
  249. snprintf(dev->device.bus_id, BUS_ID_SIZE, "%d:%03x", c->unit,
  250. dev->lct_data.tid);
  251. dev->device.parent = &c->device;
  252. device_register(&dev->device);
  253. list_add_tail(&dev->list, &c->devices);
  254. i2o_setup_sysfs_links(dev);
  255. i2o_driver_notify_device_add_all(dev);
  256. pr_debug("i2o: device %s added\n", dev->device.bus_id);
  257. return dev;
  258. }
  259. /**
  260. * i2o_device_remove - remove an I2O device from the I2O core
  261. * @dev: I2O device which should be released
  262. *
  263. * Is used on I2O controller removal or LCT modification, when the device
  264. * is removed from the system. Note that the device could still hang
  265. * around until the refcount reaches 0.
  266. */
  267. void i2o_device_remove(struct i2o_device *i2o_dev)
  268. {
  269. i2o_driver_notify_device_remove_all(i2o_dev);
  270. i2o_remove_sysfs_links(i2o_dev);
  271. list_del(&i2o_dev->list);
  272. device_unregister(&i2o_dev->device);
  273. }
  274. /**
  275. * i2o_device_parse_lct - Parse a previously fetched LCT and create devices
  276. * @c: I2O controller from which the LCT should be parsed.
  277. *
  278. * The Logical Configuration Table tells us what we can talk to on the
  279. * board. For every entry we create an I2O device, which is registered in
  280. * the I2O core.
  281. *
  282. * Returns 0 on success or negative error code on failure.
  283. */
  284. int i2o_device_parse_lct(struct i2o_controller *c)
  285. {
  286. struct i2o_device *dev, *tmp;
  287. i2o_lct *lct;
  288. u32 *dlct = c->dlct.virt;
  289. int max = 0, i = 0;
  290. u16 table_size;
  291. u32 buf;
  292. down(&c->lct_lock);
  293. kfree(c->lct);
  294. buf = le32_to_cpu(*dlct++);
  295. table_size = buf & 0xffff;
  296. lct = c->lct = kmalloc(table_size * 4, GFP_KERNEL);
  297. if (!lct) {
  298. up(&c->lct_lock);
  299. return -ENOMEM;
  300. }
  301. lct->lct_ver = buf >> 28;
  302. lct->boot_tid = buf >> 16 & 0xfff;
  303. lct->table_size = table_size;
  304. lct->change_ind = le32_to_cpu(*dlct++);
  305. lct->iop_flags = le32_to_cpu(*dlct++);
  306. table_size -= 3;
  307. pr_debug("%s: LCT has %d entries (LCT size: %d)\n", c->name, max,
  308. lct->table_size);
  309. while (table_size > 0) {
  310. i2o_lct_entry *entry = &lct->lct_entry[max];
  311. int found = 0;
  312. buf = le32_to_cpu(*dlct++);
  313. entry->entry_size = buf & 0xffff;
  314. entry->tid = buf >> 16 & 0xfff;
  315. entry->change_ind = le32_to_cpu(*dlct++);
  316. entry->device_flags = le32_to_cpu(*dlct++);
  317. buf = le32_to_cpu(*dlct++);
  318. entry->class_id = buf & 0xfff;
  319. entry->version = buf >> 12 & 0xf;
  320. entry->vendor_id = buf >> 16;
  321. entry->sub_class = le32_to_cpu(*dlct++);
  322. buf = le32_to_cpu(*dlct++);
  323. entry->user_tid = buf & 0xfff;
  324. entry->parent_tid = buf >> 12 & 0xfff;
  325. entry->bios_info = buf >> 24;
  326. memcpy(&entry->identity_tag, dlct, 8);
  327. dlct += 2;
  328. entry->event_capabilities = le32_to_cpu(*dlct++);
  329. /* add new devices, which are new in the LCT */
  330. list_for_each_entry_safe(dev, tmp, &c->devices, list) {
  331. if (entry->tid == dev->lct_data.tid) {
  332. found = 1;
  333. break;
  334. }
  335. }
  336. if (!found)
  337. i2o_device_add(c, entry);
  338. table_size -= 9;
  339. max++;
  340. }
  341. /* remove devices, which are not in the LCT anymore */
  342. list_for_each_entry_safe(dev, tmp, &c->devices, list) {
  343. int found = 0;
  344. for (i = 0; i < max; i++) {
  345. if (lct->lct_entry[i].tid == dev->lct_data.tid) {
  346. found = 1;
  347. break;
  348. }
  349. }
  350. if (!found)
  351. i2o_device_remove(dev);
  352. }
  353. up(&c->lct_lock);
  354. return 0;
  355. }
  356. /*
  357. * Run time support routines
  358. */
  359. /* Issue UTIL_PARAMS_GET or UTIL_PARAMS_SET
  360. *
  361. * This function can be used for all UtilParamsGet/Set operations.
  362. * The OperationList is given in oplist-buffer,
  363. * and results are returned in reslist-buffer.
  364. * Note that the minimum sized reslist is 8 bytes and contains
  365. * ResultCount, ErrorInfoSize, BlockStatus and BlockSize.
  366. */
  367. int i2o_parm_issue(struct i2o_device *i2o_dev, int cmd, void *oplist,
  368. int oplen, void *reslist, int reslen)
  369. {
  370. struct i2o_message *msg;
  371. int i = 0;
  372. int rc;
  373. struct i2o_dma res;
  374. struct i2o_controller *c = i2o_dev->iop;
  375. struct device *dev = &c->pdev->dev;
  376. res.virt = NULL;
  377. if (i2o_dma_alloc(dev, &res, reslen, GFP_KERNEL))
  378. return -ENOMEM;
  379. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  380. if (IS_ERR(msg)) {
  381. i2o_dma_free(dev, &res);
  382. return PTR_ERR(msg);
  383. }
  384. i = 0;
  385. msg->u.head[1] =
  386. cpu_to_le32(cmd << 24 | HOST_TID << 12 | i2o_dev->lct_data.tid);
  387. msg->body[i++] = cpu_to_le32(0x00000000);
  388. msg->body[i++] = cpu_to_le32(0x4C000000 | oplen); /* OperationList */
  389. memcpy(&msg->body[i], oplist, oplen);
  390. i += (oplen / 4 + (oplen % 4 ? 1 : 0));
  391. msg->body[i++] = cpu_to_le32(0xD0000000 | res.len); /* ResultList */
  392. msg->body[i++] = cpu_to_le32(res.phys);
  393. msg->u.head[0] =
  394. cpu_to_le32(I2O_MESSAGE_SIZE(i + sizeof(struct i2o_message) / 4) |
  395. SGL_OFFSET_5);
  396. rc = i2o_msg_post_wait_mem(c, msg, 10, &res);
  397. /* This only looks like a memory leak - don't "fix" it. */
  398. if (rc == -ETIMEDOUT)
  399. return rc;
  400. memcpy(reslist, res.virt, res.len);
  401. i2o_dma_free(dev, &res);
  402. return rc;
  403. }
  404. /*
  405. * Query one field group value or a whole scalar group.
  406. */
  407. int i2o_parm_field_get(struct i2o_device *i2o_dev, int group, int field,
  408. void *buf, int buflen)
  409. {
  410. u32 opblk[] = { cpu_to_le32(0x00000001),
  411. cpu_to_le32((u16) group << 16 | I2O_PARAMS_FIELD_GET),
  412. cpu_to_le32((s16) field << 16 | 0x00000001)
  413. };
  414. u8 *resblk; /* 8 bytes for header */
  415. int rc;
  416. resblk = kmalloc(buflen + 8, GFP_KERNEL | GFP_ATOMIC);
  417. if (!resblk)
  418. return -ENOMEM;
  419. rc = i2o_parm_issue(i2o_dev, I2O_CMD_UTIL_PARAMS_GET, opblk,
  420. sizeof(opblk), resblk, buflen + 8);
  421. memcpy(buf, resblk + 8, buflen); /* cut off header */
  422. kfree(resblk);
  423. return rc;
  424. }
  425. /*
  426. * if oper == I2O_PARAMS_TABLE_GET, get from all rows
  427. * if fieldcount == -1 return all fields
  428. * ibuf and ibuflen are unused (use NULL, 0)
  429. * else return specific fields
  430. * ibuf contains fieldindexes
  431. *
  432. * if oper == I2O_PARAMS_LIST_GET, get from specific rows
  433. * if fieldcount == -1 return all fields
  434. * ibuf contains rowcount, keyvalues
  435. * else return specific fields
  436. * fieldcount is # of fieldindexes
  437. * ibuf contains fieldindexes, rowcount, keyvalues
  438. *
  439. * You could also use directly function i2o_issue_params().
  440. */
  441. int i2o_parm_table_get(struct i2o_device *dev, int oper, int group,
  442. int fieldcount, void *ibuf, int ibuflen, void *resblk,
  443. int reslen)
  444. {
  445. u16 *opblk;
  446. int size;
  447. size = 10 + ibuflen;
  448. if (size % 4)
  449. size += 4 - size % 4;
  450. opblk = kmalloc(size, GFP_KERNEL);
  451. if (opblk == NULL) {
  452. printk(KERN_ERR "i2o: no memory for query buffer.\n");
  453. return -ENOMEM;
  454. }
  455. opblk[0] = 1; /* operation count */
  456. opblk[1] = 0; /* pad */
  457. opblk[2] = oper;
  458. opblk[3] = group;
  459. opblk[4] = fieldcount;
  460. memcpy(opblk + 5, ibuf, ibuflen); /* other params */
  461. size = i2o_parm_issue(dev, I2O_CMD_UTIL_PARAMS_GET, opblk,
  462. size, resblk, reslen);
  463. kfree(opblk);
  464. if (size > reslen)
  465. return reslen;
  466. return size;
  467. }
  468. EXPORT_SYMBOL(i2o_device_claim);
  469. EXPORT_SYMBOL(i2o_device_claim_release);
  470. EXPORT_SYMBOL(i2o_parm_field_get);
  471. EXPORT_SYMBOL(i2o_parm_table_get);
  472. EXPORT_SYMBOL(i2o_parm_issue);