device.c 16 KB

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