device.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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_show_class_id - Displays class id of I2O device
  123. * @dev: device of which the class id should be displayed
  124. * @attr: pointer to device attribute
  125. * @buf: buffer into which the class id should be printed
  126. *
  127. * Returns the number of bytes which are printed into the buffer.
  128. */
  129. static ssize_t i2o_device_show_class_id(struct device *dev,
  130. struct device_attribute *attr,
  131. char *buf)
  132. {
  133. struct i2o_device *i2o_dev = to_i2o_device(dev);
  134. sprintf(buf, "0x%03x\n", i2o_dev->lct_data.class_id);
  135. return strlen(buf) + 1;
  136. }
  137. /**
  138. * i2o_device_show_tid - Displays TID of I2O device
  139. * @dev: device of which the TID should be displayed
  140. * @attr: pointer to device attribute
  141. * @buf: buffer into which the TID should be printed
  142. *
  143. * Returns the number of bytes which are printed into the buffer.
  144. */
  145. static ssize_t i2o_device_show_tid(struct device *dev,
  146. struct device_attribute *attr, char *buf)
  147. {
  148. struct i2o_device *i2o_dev = to_i2o_device(dev);
  149. sprintf(buf, "0x%03x\n", i2o_dev->lct_data.tid);
  150. return strlen(buf) + 1;
  151. }
  152. /* I2O device attributes */
  153. struct device_attribute i2o_device_attrs[] = {
  154. __ATTR(class_id, S_IRUGO, i2o_device_show_class_id, NULL),
  155. __ATTR(tid, S_IRUGO, i2o_device_show_tid, NULL),
  156. __ATTR_NULL
  157. };
  158. /**
  159. * i2o_device_alloc - Allocate a I2O device and initialize it
  160. *
  161. * Allocate the memory for a I2O device and initialize locks and lists
  162. *
  163. * Returns the allocated I2O device or a negative error code if the device
  164. * could not be allocated.
  165. */
  166. static struct i2o_device *i2o_device_alloc(void)
  167. {
  168. struct i2o_device *dev;
  169. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  170. if (!dev)
  171. return ERR_PTR(-ENOMEM);
  172. INIT_LIST_HEAD(&dev->list);
  173. init_MUTEX(&dev->lock);
  174. dev->device.bus = &i2o_bus_type;
  175. dev->device.release = &i2o_device_release;
  176. return dev;
  177. }
  178. /**
  179. * i2o_device_add - allocate a new I2O device and add it to the IOP
  180. * @iop: I2O controller where the device is on
  181. * @entry: LCT entry of the I2O device
  182. *
  183. * Allocate a new I2O device and initialize it with the LCT entry. The
  184. * device is appended to the device list of the controller.
  185. *
  186. * Returns a pointer to the I2O device on success or negative error code
  187. * on failure.
  188. */
  189. static struct i2o_device *i2o_device_add(struct i2o_controller *c,
  190. i2o_lct_entry * entry)
  191. {
  192. struct i2o_device *i2o_dev, *tmp;
  193. i2o_dev = i2o_device_alloc();
  194. if (IS_ERR(i2o_dev)) {
  195. printk(KERN_ERR "i2o: unable to allocate i2o device\n");
  196. return i2o_dev;
  197. }
  198. i2o_dev->lct_data = *entry;
  199. snprintf(i2o_dev->device.bus_id, BUS_ID_SIZE, "%d:%03x", c->unit,
  200. i2o_dev->lct_data.tid);
  201. i2o_dev->iop = c;
  202. i2o_dev->device.parent = &c->device;
  203. device_register(&i2o_dev->device);
  204. list_add_tail(&i2o_dev->list, &c->devices);
  205. /* create user entries for this device */
  206. tmp = i2o_iop_find_device(i2o_dev->iop, i2o_dev->lct_data.user_tid);
  207. if (tmp && (tmp != i2o_dev))
  208. sysfs_create_link(&i2o_dev->device.kobj, &tmp->device.kobj,
  209. "user");
  210. /* create user entries refering to this device */
  211. list_for_each_entry(tmp, &c->devices, list)
  212. if ((tmp->lct_data.user_tid == i2o_dev->lct_data.tid)
  213. && (tmp != i2o_dev))
  214. sysfs_create_link(&tmp->device.kobj,
  215. &i2o_dev->device.kobj, "user");
  216. /* create parent entries for this device */
  217. tmp = i2o_iop_find_device(i2o_dev->iop, i2o_dev->lct_data.parent_tid);
  218. if (tmp && (tmp != i2o_dev))
  219. sysfs_create_link(&i2o_dev->device.kobj, &tmp->device.kobj,
  220. "parent");
  221. /* create parent entries refering to this device */
  222. list_for_each_entry(tmp, &c->devices, list)
  223. if ((tmp->lct_data.parent_tid == i2o_dev->lct_data.tid)
  224. && (tmp != i2o_dev))
  225. sysfs_create_link(&tmp->device.kobj,
  226. &i2o_dev->device.kobj, "parent");
  227. i2o_driver_notify_device_add_all(i2o_dev);
  228. pr_debug("i2o: device %s added\n", i2o_dev->device.bus_id);
  229. return i2o_dev;
  230. }
  231. /**
  232. * i2o_device_remove - remove an I2O device from the I2O core
  233. * @dev: I2O device which should be released
  234. *
  235. * Is used on I2O controller removal or LCT modification, when the device
  236. * is removed from the system. Note that the device could still hang
  237. * around until the refcount reaches 0.
  238. */
  239. void i2o_device_remove(struct i2o_device *i2o_dev)
  240. {
  241. struct i2o_device *tmp;
  242. struct i2o_controller *c = i2o_dev->iop;
  243. i2o_driver_notify_device_remove_all(i2o_dev);
  244. sysfs_remove_link(&i2o_dev->device.kobj, "parent");
  245. sysfs_remove_link(&i2o_dev->device.kobj, "user");
  246. list_for_each_entry(tmp, &c->devices, list) {
  247. if (tmp->lct_data.parent_tid == i2o_dev->lct_data.tid)
  248. sysfs_remove_link(&tmp->device.kobj, "parent");
  249. if (tmp->lct_data.user_tid == i2o_dev->lct_data.tid)
  250. sysfs_remove_link(&tmp->device.kobj, "user");
  251. }
  252. list_del(&i2o_dev->list);
  253. device_unregister(&i2o_dev->device);
  254. }
  255. /**
  256. * i2o_device_parse_lct - Parse a previously fetched LCT and create devices
  257. * @c: I2O controller from which the LCT should be parsed.
  258. *
  259. * The Logical Configuration Table tells us what we can talk to on the
  260. * board. For every entry we create an I2O device, which is registered in
  261. * the I2O core.
  262. *
  263. * Returns 0 on success or negative error code on failure.
  264. */
  265. int i2o_device_parse_lct(struct i2o_controller *c)
  266. {
  267. struct i2o_device *dev, *tmp;
  268. i2o_lct *lct;
  269. u32 *dlct = c->dlct.virt;
  270. int max = 0, i = 0;
  271. u16 table_size;
  272. u32 buf;
  273. down(&c->lct_lock);
  274. kfree(c->lct);
  275. buf = le32_to_cpu(*dlct++);
  276. table_size = buf & 0xffff;
  277. lct = c->lct = kmalloc(table_size * 4, GFP_KERNEL);
  278. if (!lct) {
  279. up(&c->lct_lock);
  280. return -ENOMEM;
  281. }
  282. lct->lct_ver = buf >> 28;
  283. lct->boot_tid = buf >> 16 & 0xfff;
  284. lct->table_size = table_size;
  285. lct->change_ind = le32_to_cpu(*dlct++);
  286. lct->iop_flags = le32_to_cpu(*dlct++);
  287. table_size -= 3;
  288. pr_debug("%s: LCT has %d entries (LCT size: %d)\n", c->name, max,
  289. lct->table_size);
  290. while (table_size > 0) {
  291. i2o_lct_entry *entry = &lct->lct_entry[max];
  292. int found = 0;
  293. buf = le32_to_cpu(*dlct++);
  294. entry->entry_size = buf & 0xffff;
  295. entry->tid = buf >> 16 & 0xfff;
  296. entry->change_ind = le32_to_cpu(*dlct++);
  297. entry->device_flags = le32_to_cpu(*dlct++);
  298. buf = le32_to_cpu(*dlct++);
  299. entry->class_id = buf & 0xfff;
  300. entry->version = buf >> 12 & 0xf;
  301. entry->vendor_id = buf >> 16;
  302. entry->sub_class = le32_to_cpu(*dlct++);
  303. buf = le32_to_cpu(*dlct++);
  304. entry->user_tid = buf & 0xfff;
  305. entry->parent_tid = buf >> 12 & 0xfff;
  306. entry->bios_info = buf >> 24;
  307. memcpy(&entry->identity_tag, dlct, 8);
  308. dlct += 2;
  309. entry->event_capabilities = le32_to_cpu(*dlct++);
  310. /* add new devices, which are new in the LCT */
  311. list_for_each_entry_safe(dev, tmp, &c->devices, list) {
  312. if (entry->tid == dev->lct_data.tid) {
  313. found = 1;
  314. break;
  315. }
  316. }
  317. if (!found)
  318. i2o_device_add(c, entry);
  319. table_size -= 9;
  320. max++;
  321. }
  322. /* remove devices, which are not in the LCT anymore */
  323. list_for_each_entry_safe(dev, tmp, &c->devices, list) {
  324. int found = 0;
  325. for (i = 0; i < max; i++) {
  326. if (lct->lct_entry[i].tid == dev->lct_data.tid) {
  327. found = 1;
  328. break;
  329. }
  330. }
  331. if (!found)
  332. i2o_device_remove(dev);
  333. }
  334. up(&c->lct_lock);
  335. return 0;
  336. }
  337. /*
  338. * Run time support routines
  339. */
  340. /* Issue UTIL_PARAMS_GET or UTIL_PARAMS_SET
  341. *
  342. * This function can be used for all UtilParamsGet/Set operations.
  343. * The OperationList is given in oplist-buffer,
  344. * and results are returned in reslist-buffer.
  345. * Note that the minimum sized reslist is 8 bytes and contains
  346. * ResultCount, ErrorInfoSize, BlockStatus and BlockSize.
  347. */
  348. int i2o_parm_issue(struct i2o_device *i2o_dev, int cmd, void *oplist,
  349. int oplen, void *reslist, int reslen)
  350. {
  351. struct i2o_message *msg;
  352. int i = 0;
  353. int rc;
  354. struct i2o_dma res;
  355. struct i2o_controller *c = i2o_dev->iop;
  356. struct device *dev = &c->pdev->dev;
  357. res.virt = NULL;
  358. if (i2o_dma_alloc(dev, &res, reslen, GFP_KERNEL))
  359. return -ENOMEM;
  360. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  361. if (IS_ERR(msg)) {
  362. i2o_dma_free(dev, &res);
  363. return PTR_ERR(msg);
  364. }
  365. i = 0;
  366. msg->u.head[1] =
  367. cpu_to_le32(cmd << 24 | HOST_TID << 12 | i2o_dev->lct_data.tid);
  368. msg->body[i++] = cpu_to_le32(0x00000000);
  369. msg->body[i++] = cpu_to_le32(0x4C000000 | oplen); /* OperationList */
  370. memcpy(&msg->body[i], oplist, oplen);
  371. i += (oplen / 4 + (oplen % 4 ? 1 : 0));
  372. msg->body[i++] = cpu_to_le32(0xD0000000 | res.len); /* ResultList */
  373. msg->body[i++] = cpu_to_le32(res.phys);
  374. msg->u.head[0] =
  375. cpu_to_le32(I2O_MESSAGE_SIZE(i + sizeof(struct i2o_message) / 4) |
  376. SGL_OFFSET_5);
  377. rc = i2o_msg_post_wait_mem(c, msg, 10, &res);
  378. /* This only looks like a memory leak - don't "fix" it. */
  379. if (rc == -ETIMEDOUT)
  380. return rc;
  381. memcpy(reslist, res.virt, res.len);
  382. i2o_dma_free(dev, &res);
  383. return rc;
  384. }
  385. /*
  386. * Query one field group value or a whole scalar group.
  387. */
  388. int i2o_parm_field_get(struct i2o_device *i2o_dev, int group, int field,
  389. void *buf, int buflen)
  390. {
  391. u32 opblk[] = { cpu_to_le32(0x00000001),
  392. cpu_to_le32((u16) group << 16 | I2O_PARAMS_FIELD_GET),
  393. cpu_to_le32((s16) field << 16 | 0x00000001)
  394. };
  395. u8 *resblk; /* 8 bytes for header */
  396. int rc;
  397. resblk = kmalloc(buflen + 8, GFP_KERNEL | GFP_ATOMIC);
  398. if (!resblk)
  399. return -ENOMEM;
  400. rc = i2o_parm_issue(i2o_dev, I2O_CMD_UTIL_PARAMS_GET, opblk,
  401. sizeof(opblk), resblk, buflen + 8);
  402. memcpy(buf, resblk + 8, buflen); /* cut off header */
  403. kfree(resblk);
  404. return rc;
  405. }
  406. /*
  407. * if oper == I2O_PARAMS_TABLE_GET, get from all rows
  408. * if fieldcount == -1 return all fields
  409. * ibuf and ibuflen are unused (use NULL, 0)
  410. * else return specific fields
  411. * ibuf contains fieldindexes
  412. *
  413. * if oper == I2O_PARAMS_LIST_GET, get from specific rows
  414. * if fieldcount == -1 return all fields
  415. * ibuf contains rowcount, keyvalues
  416. * else return specific fields
  417. * fieldcount is # of fieldindexes
  418. * ibuf contains fieldindexes, rowcount, keyvalues
  419. *
  420. * You could also use directly function i2o_issue_params().
  421. */
  422. int i2o_parm_table_get(struct i2o_device *dev, int oper, int group,
  423. int fieldcount, void *ibuf, int ibuflen, void *resblk,
  424. int reslen)
  425. {
  426. u16 *opblk;
  427. int size;
  428. size = 10 + ibuflen;
  429. if (size % 4)
  430. size += 4 - size % 4;
  431. opblk = kmalloc(size, GFP_KERNEL);
  432. if (opblk == NULL) {
  433. printk(KERN_ERR "i2o: no memory for query buffer.\n");
  434. return -ENOMEM;
  435. }
  436. opblk[0] = 1; /* operation count */
  437. opblk[1] = 0; /* pad */
  438. opblk[2] = oper;
  439. opblk[3] = group;
  440. opblk[4] = fieldcount;
  441. memcpy(opblk + 5, ibuf, ibuflen); /* other params */
  442. size = i2o_parm_issue(dev, I2O_CMD_UTIL_PARAMS_GET, opblk,
  443. size, resblk, reslen);
  444. kfree(opblk);
  445. if (size > reslen)
  446. return reslen;
  447. return size;
  448. }
  449. EXPORT_SYMBOL(i2o_device_claim);
  450. EXPORT_SYMBOL(i2o_device_claim_release);
  451. EXPORT_SYMBOL(i2o_parm_field_get);
  452. EXPORT_SYMBOL(i2o_parm_table_get);
  453. EXPORT_SYMBOL(i2o_parm_issue);