device.c 15 KB

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