device.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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 __iomem *msg;
  37. u32 m;
  38. m = i2o_msg_get_wait(dev->iop, &msg, I2O_TIMEOUT_MESSAGE_GET);
  39. if (m == I2O_QUEUE_EMPTY)
  40. return -ETIMEDOUT;
  41. writel(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
  42. writel(cmd << 24 | HOST_TID << 12 | dev->lct_data.tid, &msg->u.head[1]);
  43. writel(type, &msg->body[0]);
  44. return i2o_msg_post_wait(dev->iop, m, 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. int i;
  289. int max;
  290. down(&c->lct_lock);
  291. kfree(c->lct);
  292. lct = c->dlct.virt;
  293. c->lct = kmalloc(lct->table_size * 4, GFP_KERNEL);
  294. if (!c->lct) {
  295. up(&c->lct_lock);
  296. return -ENOMEM;
  297. }
  298. if (lct->table_size * 4 > c->dlct.len) {
  299. memcpy(c->lct, c->dlct.virt, c->dlct.len);
  300. up(&c->lct_lock);
  301. return -EAGAIN;
  302. }
  303. memcpy(c->lct, c->dlct.virt, lct->table_size * 4);
  304. lct = c->lct;
  305. max = (lct->table_size - 3) / 9;
  306. pr_debug("%s: LCT has %d entries (LCT size: %d)\n", c->name, max,
  307. lct->table_size);
  308. /* remove devices, which are not in the LCT anymore */
  309. list_for_each_entry_safe(dev, tmp, &c->devices, list) {
  310. int found = 0;
  311. for (i = 0; i < max; i++) {
  312. if (lct->lct_entry[i].tid == dev->lct_data.tid) {
  313. found = 1;
  314. break;
  315. }
  316. }
  317. if (!found)
  318. i2o_device_remove(dev);
  319. }
  320. /* add new devices, which are new in the LCT */
  321. for (i = 0; i < max; i++) {
  322. int found = 0;
  323. list_for_each_entry_safe(dev, tmp, &c->devices, list) {
  324. if (lct->lct_entry[i].tid == dev->lct_data.tid) {
  325. found = 1;
  326. break;
  327. }
  328. }
  329. if (!found)
  330. i2o_device_add(c, &lct->lct_entry[i]);
  331. }
  332. up(&c->lct_lock);
  333. return 0;
  334. }
  335. /*
  336. * Run time support routines
  337. */
  338. /* Issue UTIL_PARAMS_GET or UTIL_PARAMS_SET
  339. *
  340. * This function can be used for all UtilParamsGet/Set operations.
  341. * The OperationList is given in oplist-buffer,
  342. * and results are returned in reslist-buffer.
  343. * Note that the minimum sized reslist is 8 bytes and contains
  344. * ResultCount, ErrorInfoSize, BlockStatus and BlockSize.
  345. */
  346. int i2o_parm_issue(struct i2o_device *i2o_dev, int cmd, void *oplist,
  347. int oplen, void *reslist, int reslen)
  348. {
  349. struct i2o_message __iomem *msg;
  350. u32 m;
  351. u32 *res32 = (u32 *) reslist;
  352. u32 *restmp = (u32 *) reslist;
  353. int len = 0;
  354. int i = 0;
  355. int rc;
  356. struct i2o_dma res;
  357. struct i2o_controller *c = i2o_dev->iop;
  358. struct device *dev = &c->pdev->dev;
  359. res.virt = NULL;
  360. if (i2o_dma_alloc(dev, &res, reslen, GFP_KERNEL))
  361. return -ENOMEM;
  362. m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
  363. if (m == I2O_QUEUE_EMPTY) {
  364. i2o_dma_free(dev, &res);
  365. return -ETIMEDOUT;
  366. }
  367. i = 0;
  368. writel(cmd << 24 | HOST_TID << 12 | i2o_dev->lct_data.tid,
  369. &msg->u.head[1]);
  370. writel(0, &msg->body[i++]);
  371. writel(0x4C000000 | oplen, &msg->body[i++]); /* OperationList */
  372. memcpy_toio(&msg->body[i], oplist, oplen);
  373. i += (oplen / 4 + (oplen % 4 ? 1 : 0));
  374. writel(0xD0000000 | res.len, &msg->body[i++]); /* ResultList */
  375. writel(res.phys, &msg->body[i++]);
  376. writel(I2O_MESSAGE_SIZE(i + sizeof(struct i2o_message) / 4) |
  377. SGL_OFFSET_5, &msg->u.head[0]);
  378. rc = i2o_msg_post_wait_mem(c, m, 10, &res);
  379. /* This only looks like a memory leak - don't "fix" it. */
  380. if (rc == -ETIMEDOUT)
  381. return rc;
  382. memcpy(reslist, res.virt, res.len);
  383. i2o_dma_free(dev, &res);
  384. /* Query failed */
  385. if (rc)
  386. return rc;
  387. /*
  388. * Calculate number of bytes of Result LIST
  389. * We need to loop through each Result BLOCK and grab the length
  390. */
  391. restmp = res32 + 1;
  392. len = 1;
  393. for (i = 0; i < (res32[0] & 0X0000FFFF); i++) {
  394. if (restmp[0] & 0x00FF0000) { /* BlockStatus != SUCCESS */
  395. printk(KERN_WARNING
  396. "%s - Error:\n ErrorInfoSize = 0x%02x, "
  397. "BlockStatus = 0x%02x, BlockSize = 0x%04x\n",
  398. (cmd ==
  399. I2O_CMD_UTIL_PARAMS_SET) ? "PARAMS_SET" :
  400. "PARAMS_GET", res32[1] >> 24,
  401. (res32[1] >> 16) & 0xFF, res32[1] & 0xFFFF);
  402. /*
  403. * If this is the only request,than we return an error
  404. */
  405. if ((res32[0] & 0x0000FFFF) == 1) {
  406. return -((res32[1] >> 16) & 0xFF); /* -BlockStatus */
  407. }
  408. }
  409. len += restmp[0] & 0x0000FFFF; /* Length of res BLOCK */
  410. restmp += restmp[0] & 0x0000FFFF; /* Skip to next BLOCK */
  411. }
  412. return (len << 2); /* bytes used by result list */
  413. }
  414. /*
  415. * Query one field group value or a whole scalar group.
  416. */
  417. int i2o_parm_field_get(struct i2o_device *i2o_dev, int group, int field,
  418. void *buf, int buflen)
  419. {
  420. u16 opblk[] = { 1, 0, I2O_PARAMS_FIELD_GET, group, 1, field };
  421. u8 *resblk; /* 8 bytes for header */
  422. int size;
  423. if (field == -1) /* whole group */
  424. opblk[4] = -1;
  425. resblk = kmalloc(buflen + 8, GFP_KERNEL | GFP_ATOMIC);
  426. if (!resblk)
  427. return -ENOMEM;
  428. size = i2o_parm_issue(i2o_dev, I2O_CMD_UTIL_PARAMS_GET, opblk,
  429. sizeof(opblk), resblk, buflen + 8);
  430. memcpy(buf, resblk + 8, buflen); /* cut off header */
  431. kfree(resblk);
  432. if (size > buflen)
  433. return buflen;
  434. return size;
  435. }
  436. /*
  437. * if oper == I2O_PARAMS_TABLE_GET, get from all rows
  438. * if fieldcount == -1 return all fields
  439. * ibuf and ibuflen are unused (use NULL, 0)
  440. * else return specific fields
  441. * ibuf contains fieldindexes
  442. *
  443. * if oper == I2O_PARAMS_LIST_GET, get from specific rows
  444. * if fieldcount == -1 return all fields
  445. * ibuf contains rowcount, keyvalues
  446. * else return specific fields
  447. * fieldcount is # of fieldindexes
  448. * ibuf contains fieldindexes, rowcount, keyvalues
  449. *
  450. * You could also use directly function i2o_issue_params().
  451. */
  452. int i2o_parm_table_get(struct i2o_device *dev, int oper, int group,
  453. int fieldcount, void *ibuf, int ibuflen, void *resblk,
  454. int reslen)
  455. {
  456. u16 *opblk;
  457. int size;
  458. size = 10 + ibuflen;
  459. if (size % 4)
  460. size += 4 - size % 4;
  461. opblk = kmalloc(size, GFP_KERNEL);
  462. if (opblk == NULL) {
  463. printk(KERN_ERR "i2o: no memory for query buffer.\n");
  464. return -ENOMEM;
  465. }
  466. opblk[0] = 1; /* operation count */
  467. opblk[1] = 0; /* pad */
  468. opblk[2] = oper;
  469. opblk[3] = group;
  470. opblk[4] = fieldcount;
  471. memcpy(opblk + 5, ibuf, ibuflen); /* other params */
  472. size = i2o_parm_issue(dev, I2O_CMD_UTIL_PARAMS_GET, opblk,
  473. size, resblk, reslen);
  474. kfree(opblk);
  475. if (size > reslen)
  476. return reslen;
  477. return size;
  478. }
  479. EXPORT_SYMBOL(i2o_device_claim);
  480. EXPORT_SYMBOL(i2o_device_claim_release);
  481. EXPORT_SYMBOL(i2o_parm_field_get);
  482. EXPORT_SYMBOL(i2o_parm_table_get);
  483. EXPORT_SYMBOL(i2o_parm_issue);