device.c 15 KB

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