device.c 16 KB

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