device.c 15 KB

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