iop.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. /*
  2. * Functions to handle I2O controllers and I2O message handling
  3. *
  4. * Copyright (C) 1999-2002 Red Hat Software
  5. *
  6. * Written by Alan Cox, Building Number Three Ltd
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * A lot of the I2O message side code from this is taken from the
  14. * Red Creek RCPCI45 adapter driver by Red Creek Communications
  15. *
  16. * Fixes/additions:
  17. * Philipp Rumpf
  18. * Juha Sievänen <Juha.Sievanen@cs.Helsinki.FI>
  19. * Auvo Häkkinen <Auvo.Hakkinen@cs.Helsinki.FI>
  20. * Deepak Saxena <deepak@plexity.net>
  21. * Boji T Kannanthanam <boji.t.kannanthanam@intel.com>
  22. * Alan Cox <alan@lxorguk.ukuu.org.uk>:
  23. * Ported to Linux 2.5.
  24. * Markus Lidel <Markus.Lidel@shadowconnect.com>:
  25. * Minor fixes for 2.6.
  26. */
  27. #include <linux/module.h>
  28. #include <linux/i2o.h>
  29. #include <linux/delay.h>
  30. #include <linux/sched.h>
  31. #include <linux/slab.h>
  32. #include "core.h"
  33. #define OSM_NAME "i2o"
  34. #define OSM_VERSION "1.325"
  35. #define OSM_DESCRIPTION "I2O subsystem"
  36. /* global I2O controller list */
  37. LIST_HEAD(i2o_controllers);
  38. /*
  39. * global I2O System Table. Contains information about all the IOPs in the
  40. * system. Used to inform IOPs about each others existence.
  41. */
  42. static struct i2o_dma i2o_systab;
  43. static int i2o_hrt_get(struct i2o_controller *c);
  44. /**
  45. * i2o_msg_get_wait - obtain an I2O message from the IOP
  46. * @c: I2O controller
  47. * @wait: how long to wait until timeout
  48. *
  49. * This function waits up to wait seconds for a message slot to be
  50. * available.
  51. *
  52. * On a success the message is returned and the pointer to the message is
  53. * set in msg. The returned message is the physical page frame offset
  54. * address from the read port (see the i2o spec). If no message is
  55. * available returns I2O_QUEUE_EMPTY and msg is leaved untouched.
  56. */
  57. struct i2o_message *i2o_msg_get_wait(struct i2o_controller *c, int wait)
  58. {
  59. unsigned long timeout = jiffies + wait * HZ;
  60. struct i2o_message *msg;
  61. while (IS_ERR(msg = i2o_msg_get(c))) {
  62. if (time_after(jiffies, timeout)) {
  63. osm_debug("%s: Timeout waiting for message frame.\n",
  64. c->name);
  65. return ERR_PTR(-ETIMEDOUT);
  66. }
  67. schedule_timeout_uninterruptible(1);
  68. }
  69. return msg;
  70. };
  71. #if BITS_PER_LONG == 64
  72. /**
  73. * i2o_cntxt_list_add - Append a pointer to context list and return a id
  74. * @c: controller to which the context list belong
  75. * @ptr: pointer to add to the context list
  76. *
  77. * Because the context field in I2O is only 32-bit large, on 64-bit the
  78. * pointer is to large to fit in the context field. The i2o_cntxt_list
  79. * functions therefore map pointers to context fields.
  80. *
  81. * Returns context id > 0 on success or 0 on failure.
  82. */
  83. u32 i2o_cntxt_list_add(struct i2o_controller * c, void *ptr)
  84. {
  85. struct i2o_context_list_element *entry;
  86. unsigned long flags;
  87. if (!ptr)
  88. osm_err("%s: couldn't add NULL pointer to context list!\n",
  89. c->name);
  90. entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
  91. if (!entry) {
  92. osm_err("%s: Could not allocate memory for context list element"
  93. "\n", c->name);
  94. return 0;
  95. }
  96. entry->ptr = ptr;
  97. entry->timestamp = jiffies;
  98. INIT_LIST_HEAD(&entry->list);
  99. spin_lock_irqsave(&c->context_list_lock, flags);
  100. if (unlikely(atomic_inc_and_test(&c->context_list_counter)))
  101. atomic_inc(&c->context_list_counter);
  102. entry->context = atomic_read(&c->context_list_counter);
  103. list_add(&entry->list, &c->context_list);
  104. spin_unlock_irqrestore(&c->context_list_lock, flags);
  105. osm_debug("%s: Add context to list %p -> %d\n", c->name, ptr, context);
  106. return entry->context;
  107. };
  108. /**
  109. * i2o_cntxt_list_remove - Remove a pointer from the context list
  110. * @c: controller to which the context list belong
  111. * @ptr: pointer which should be removed from the context list
  112. *
  113. * Removes a previously added pointer from the context list and returns
  114. * the matching context id.
  115. *
  116. * Returns context id on success or 0 on failure.
  117. */
  118. u32 i2o_cntxt_list_remove(struct i2o_controller * c, void *ptr)
  119. {
  120. struct i2o_context_list_element *entry;
  121. u32 context = 0;
  122. unsigned long flags;
  123. spin_lock_irqsave(&c->context_list_lock, flags);
  124. list_for_each_entry(entry, &c->context_list, list)
  125. if (entry->ptr == ptr) {
  126. list_del(&entry->list);
  127. context = entry->context;
  128. kfree(entry);
  129. break;
  130. }
  131. spin_unlock_irqrestore(&c->context_list_lock, flags);
  132. if (!context)
  133. osm_warn("%s: Could not remove nonexistent ptr %p\n", c->name,
  134. ptr);
  135. osm_debug("%s: remove ptr from context list %d -> %p\n", c->name,
  136. context, ptr);
  137. return context;
  138. };
  139. /**
  140. * i2o_cntxt_list_get - Get a pointer from the context list and remove it
  141. * @c: controller to which the context list belong
  142. * @context: context id to which the pointer belong
  143. *
  144. * Returns pointer to the matching context id on success or NULL on
  145. * failure.
  146. */
  147. void *i2o_cntxt_list_get(struct i2o_controller *c, u32 context)
  148. {
  149. struct i2o_context_list_element *entry;
  150. unsigned long flags;
  151. void *ptr = NULL;
  152. spin_lock_irqsave(&c->context_list_lock, flags);
  153. list_for_each_entry(entry, &c->context_list, list)
  154. if (entry->context == context) {
  155. list_del(&entry->list);
  156. ptr = entry->ptr;
  157. kfree(entry);
  158. break;
  159. }
  160. spin_unlock_irqrestore(&c->context_list_lock, flags);
  161. if (!ptr)
  162. osm_warn("%s: context id %d not found\n", c->name, context);
  163. osm_debug("%s: get ptr from context list %d -> %p\n", c->name, context,
  164. ptr);
  165. return ptr;
  166. };
  167. /**
  168. * i2o_cntxt_list_get_ptr - Get a context id from the context list
  169. * @c: controller to which the context list belong
  170. * @ptr: pointer to which the context id should be fetched
  171. *
  172. * Returns context id which matches to the pointer on success or 0 on
  173. * failure.
  174. */
  175. u32 i2o_cntxt_list_get_ptr(struct i2o_controller * c, void *ptr)
  176. {
  177. struct i2o_context_list_element *entry;
  178. u32 context = 0;
  179. unsigned long flags;
  180. spin_lock_irqsave(&c->context_list_lock, flags);
  181. list_for_each_entry(entry, &c->context_list, list)
  182. if (entry->ptr == ptr) {
  183. context = entry->context;
  184. break;
  185. }
  186. spin_unlock_irqrestore(&c->context_list_lock, flags);
  187. if (!context)
  188. osm_warn("%s: Could not find nonexistent ptr %p\n", c->name,
  189. ptr);
  190. osm_debug("%s: get context id from context list %p -> %d\n", c->name,
  191. ptr, context);
  192. return context;
  193. };
  194. #endif
  195. /**
  196. * i2o_iop_find - Find an I2O controller by id
  197. * @unit: unit number of the I2O controller to search for
  198. *
  199. * Lookup the I2O controller on the controller list.
  200. *
  201. * Returns pointer to the I2O controller on success or NULL if not found.
  202. */
  203. struct i2o_controller *i2o_find_iop(int unit)
  204. {
  205. struct i2o_controller *c;
  206. list_for_each_entry(c, &i2o_controllers, list) {
  207. if (c->unit == unit)
  208. return c;
  209. }
  210. return NULL;
  211. };
  212. /**
  213. * i2o_iop_find_device - Find a I2O device on an I2O controller
  214. * @c: I2O controller where the I2O device hangs on
  215. * @tid: TID of the I2O device to search for
  216. *
  217. * Searches the devices of the I2O controller for a device with TID tid and
  218. * returns it.
  219. *
  220. * Returns a pointer to the I2O device if found, otherwise NULL.
  221. */
  222. struct i2o_device *i2o_iop_find_device(struct i2o_controller *c, u16 tid)
  223. {
  224. struct i2o_device *dev;
  225. list_for_each_entry(dev, &c->devices, list)
  226. if (dev->lct_data.tid == tid)
  227. return dev;
  228. return NULL;
  229. };
  230. /**
  231. * i2o_quiesce_controller - quiesce controller
  232. * @c: controller
  233. *
  234. * Quiesce an IOP. Causes IOP to make external operation quiescent
  235. * (i2o 'READY' state). Internal operation of the IOP continues normally.
  236. *
  237. * Returns 0 on success or negative error code on failure.
  238. */
  239. static int i2o_iop_quiesce(struct i2o_controller *c)
  240. {
  241. struct i2o_message *msg;
  242. i2o_status_block *sb = c->status_block.virt;
  243. int rc;
  244. i2o_status_get(c);
  245. /* SysQuiesce discarded if IOP not in READY or OPERATIONAL state */
  246. if ((sb->iop_state != ADAPTER_STATE_READY) &&
  247. (sb->iop_state != ADAPTER_STATE_OPERATIONAL))
  248. return 0;
  249. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  250. if (IS_ERR(msg))
  251. return PTR_ERR(msg);
  252. msg->u.head[0] = cpu_to_le32(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0);
  253. msg->u.head[1] =
  254. cpu_to_le32(I2O_CMD_SYS_QUIESCE << 24 | HOST_TID << 12 |
  255. ADAPTER_TID);
  256. /* Long timeout needed for quiesce if lots of devices */
  257. if ((rc = i2o_msg_post_wait(c, msg, 240)))
  258. osm_info("%s: Unable to quiesce (status=%#x).\n", c->name, -rc);
  259. else
  260. osm_debug("%s: Quiesced.\n", c->name);
  261. i2o_status_get(c); // Entered READY state
  262. return rc;
  263. };
  264. /**
  265. * i2o_iop_enable - move controller from ready to OPERATIONAL
  266. * @c: I2O controller
  267. *
  268. * Enable IOP. This allows the IOP to resume external operations and
  269. * reverses the effect of a quiesce. Returns zero or an error code if
  270. * an error occurs.
  271. */
  272. static int i2o_iop_enable(struct i2o_controller *c)
  273. {
  274. struct i2o_message *msg;
  275. i2o_status_block *sb = c->status_block.virt;
  276. int rc;
  277. i2o_status_get(c);
  278. /* Enable only allowed on READY state */
  279. if (sb->iop_state != ADAPTER_STATE_READY)
  280. return -EINVAL;
  281. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  282. if (IS_ERR(msg))
  283. return PTR_ERR(msg);
  284. msg->u.head[0] = cpu_to_le32(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0);
  285. msg->u.head[1] =
  286. cpu_to_le32(I2O_CMD_SYS_ENABLE << 24 | HOST_TID << 12 |
  287. ADAPTER_TID);
  288. /* How long of a timeout do we need? */
  289. if ((rc = i2o_msg_post_wait(c, msg, 240)))
  290. osm_err("%s: Could not enable (status=%#x).\n", c->name, -rc);
  291. else
  292. osm_debug("%s: Enabled.\n", c->name);
  293. i2o_status_get(c); // entered OPERATIONAL state
  294. return rc;
  295. };
  296. /**
  297. * i2o_iop_quiesce_all - Quiesce all I2O controllers on the system
  298. *
  299. * Quiesce all I2O controllers which are connected to the system.
  300. */
  301. static inline void i2o_iop_quiesce_all(void)
  302. {
  303. struct i2o_controller *c, *tmp;
  304. list_for_each_entry_safe(c, tmp, &i2o_controllers, list) {
  305. if (!c->no_quiesce)
  306. i2o_iop_quiesce(c);
  307. }
  308. };
  309. /**
  310. * i2o_iop_enable_all - Enables all controllers on the system
  311. *
  312. * Enables all I2O controllers which are connected to the system.
  313. */
  314. static inline void i2o_iop_enable_all(void)
  315. {
  316. struct i2o_controller *c, *tmp;
  317. list_for_each_entry_safe(c, tmp, &i2o_controllers, list)
  318. i2o_iop_enable(c);
  319. };
  320. /**
  321. * i2o_clear_controller - Bring I2O controller into HOLD state
  322. * @c: controller
  323. *
  324. * Clear an IOP to HOLD state, ie. terminate external operations, clear all
  325. * input queues and prepare for a system restart. IOP's internal operation
  326. * continues normally and the outbound queue is alive. The IOP is not
  327. * expected to rebuild its LCT.
  328. *
  329. * Returns 0 on success or negative error code on failure.
  330. */
  331. static int i2o_iop_clear(struct i2o_controller *c)
  332. {
  333. struct i2o_message *msg;
  334. int rc;
  335. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  336. if (IS_ERR(msg))
  337. return PTR_ERR(msg);
  338. /* Quiesce all IOPs first */
  339. i2o_iop_quiesce_all();
  340. msg->u.head[0] = cpu_to_le32(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0);
  341. msg->u.head[1] =
  342. cpu_to_le32(I2O_CMD_ADAPTER_CLEAR << 24 | HOST_TID << 12 |
  343. ADAPTER_TID);
  344. if ((rc = i2o_msg_post_wait(c, msg, 30)))
  345. osm_info("%s: Unable to clear (status=%#x).\n", c->name, -rc);
  346. else
  347. osm_debug("%s: Cleared.\n", c->name);
  348. /* Enable all IOPs */
  349. i2o_iop_enable_all();
  350. return rc;
  351. }
  352. /**
  353. * i2o_iop_init_outbound_queue - setup the outbound message queue
  354. * @c: I2O controller
  355. *
  356. * Clear and (re)initialize IOP's outbound queue and post the message
  357. * frames to the IOP.
  358. *
  359. * Returns 0 on success or negative error code on failure.
  360. */
  361. static int i2o_iop_init_outbound_queue(struct i2o_controller *c)
  362. {
  363. u32 m;
  364. volatile u8 *status = c->status.virt;
  365. struct i2o_message *msg;
  366. ulong timeout;
  367. int i;
  368. osm_debug("%s: Initializing Outbound Queue...\n", c->name);
  369. memset(c->status.virt, 0, 4);
  370. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  371. if (IS_ERR(msg))
  372. return PTR_ERR(msg);
  373. msg->u.head[0] = cpu_to_le32(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_6);
  374. msg->u.head[1] =
  375. cpu_to_le32(I2O_CMD_OUTBOUND_INIT << 24 | HOST_TID << 12 |
  376. ADAPTER_TID);
  377. msg->u.s.icntxt = cpu_to_le32(i2o_exec_driver.context);
  378. msg->u.s.tcntxt = cpu_to_le32(0x00000000);
  379. msg->body[0] = cpu_to_le32(PAGE_SIZE);
  380. /* Outbound msg frame size in words and Initcode */
  381. msg->body[1] = cpu_to_le32(I2O_OUTBOUND_MSG_FRAME_SIZE << 16 | 0x80);
  382. msg->body[2] = cpu_to_le32(0xd0000004);
  383. msg->body[3] = cpu_to_le32(i2o_dma_low(c->status.phys));
  384. msg->body[4] = cpu_to_le32(i2o_dma_high(c->status.phys));
  385. i2o_msg_post(c, msg);
  386. timeout = jiffies + I2O_TIMEOUT_INIT_OUTBOUND_QUEUE * HZ;
  387. while (*status <= I2O_CMD_IN_PROGRESS) {
  388. if (time_after(jiffies, timeout)) {
  389. osm_warn("%s: Timeout Initializing\n", c->name);
  390. return -ETIMEDOUT;
  391. }
  392. schedule_timeout_uninterruptible(1);
  393. }
  394. m = c->out_queue.phys;
  395. /* Post frames */
  396. for (i = 0; i < I2O_MAX_OUTBOUND_MSG_FRAMES; i++) {
  397. i2o_flush_reply(c, m);
  398. udelay(1); /* Promise */
  399. m += I2O_OUTBOUND_MSG_FRAME_SIZE * sizeof(u32);
  400. }
  401. return 0;
  402. }
  403. /**
  404. * i2o_iop_reset - reset an I2O controller
  405. * @c: controller to reset
  406. *
  407. * Reset the IOP into INIT state and wait until IOP gets into RESET state.
  408. * Terminate all external operations, clear IOP's inbound and outbound
  409. * queues, terminate all DDMs, and reload the IOP's operating environment
  410. * and all local DDMs. The IOP rebuilds its LCT.
  411. */
  412. static int i2o_iop_reset(struct i2o_controller *c)
  413. {
  414. volatile u8 *status = c->status.virt;
  415. struct i2o_message *msg;
  416. unsigned long timeout;
  417. i2o_status_block *sb = c->status_block.virt;
  418. int rc = 0;
  419. osm_debug("%s: Resetting controller\n", c->name);
  420. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  421. if (IS_ERR(msg))
  422. return PTR_ERR(msg);
  423. memset(c->status_block.virt, 0, 8);
  424. /* Quiesce all IOPs first */
  425. i2o_iop_quiesce_all();
  426. msg->u.head[0] = cpu_to_le32(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_0);
  427. msg->u.head[1] =
  428. cpu_to_le32(I2O_CMD_ADAPTER_RESET << 24 | HOST_TID << 12 |
  429. ADAPTER_TID);
  430. msg->u.s.icntxt = cpu_to_le32(i2o_exec_driver.context);
  431. msg->u.s.tcntxt = cpu_to_le32(0x00000000);
  432. msg->body[0] = cpu_to_le32(0x00000000);
  433. msg->body[1] = cpu_to_le32(0x00000000);
  434. msg->body[2] = cpu_to_le32(i2o_dma_low(c->status.phys));
  435. msg->body[3] = cpu_to_le32(i2o_dma_high(c->status.phys));
  436. i2o_msg_post(c, msg);
  437. /* Wait for a reply */
  438. timeout = jiffies + I2O_TIMEOUT_RESET * HZ;
  439. while (!*status) {
  440. if (time_after(jiffies, timeout))
  441. break;
  442. schedule_timeout_uninterruptible(1);
  443. }
  444. switch (*status) {
  445. case I2O_CMD_REJECTED:
  446. osm_warn("%s: IOP reset rejected\n", c->name);
  447. rc = -EPERM;
  448. break;
  449. case I2O_CMD_IN_PROGRESS:
  450. /*
  451. * Once the reset is sent, the IOP goes into the INIT state
  452. * which is indeterminate. We need to wait until the IOP has
  453. * rebooted before we can let the system talk to it. We read
  454. * the inbound Free_List until a message is available. If we
  455. * can't read one in the given amount of time, we assume the
  456. * IOP could not reboot properly.
  457. */
  458. osm_debug("%s: Reset in progress, waiting for reboot...\n",
  459. c->name);
  460. while (IS_ERR(msg = i2o_msg_get_wait(c, I2O_TIMEOUT_RESET))) {
  461. if (time_after(jiffies, timeout)) {
  462. osm_err("%s: IOP reset timeout.\n", c->name);
  463. rc = PTR_ERR(msg);
  464. goto exit;
  465. }
  466. schedule_timeout_uninterruptible(1);
  467. }
  468. i2o_msg_nop(c, msg);
  469. /* from here all quiesce commands are safe */
  470. c->no_quiesce = 0;
  471. /* verify if controller is in state RESET */
  472. i2o_status_get(c);
  473. if (!c->promise && (sb->iop_state != ADAPTER_STATE_RESET))
  474. osm_warn("%s: reset completed, but adapter not in RESET"
  475. " state.\n", c->name);
  476. else
  477. osm_debug("%s: reset completed.\n", c->name);
  478. break;
  479. default:
  480. osm_err("%s: IOP reset timeout.\n", c->name);
  481. rc = -ETIMEDOUT;
  482. break;
  483. }
  484. exit:
  485. /* Enable all IOPs */
  486. i2o_iop_enable_all();
  487. return rc;
  488. };
  489. /**
  490. * i2o_iop_activate - Bring controller up to HOLD
  491. * @c: controller
  492. *
  493. * This function brings an I2O controller into HOLD state. The adapter
  494. * is reset if necessary and then the queues and resource table are read.
  495. *
  496. * Returns 0 on success or negative error code on failure.
  497. */
  498. static int i2o_iop_activate(struct i2o_controller *c)
  499. {
  500. i2o_status_block *sb = c->status_block.virt;
  501. int rc;
  502. int state;
  503. /* In INIT state, Wait Inbound Q to initialize (in i2o_status_get) */
  504. /* In READY state, Get status */
  505. rc = i2o_status_get(c);
  506. if (rc) {
  507. osm_info("%s: Unable to obtain status, attempting a reset.\n",
  508. c->name);
  509. rc = i2o_iop_reset(c);
  510. if (rc)
  511. return rc;
  512. }
  513. if (sb->i2o_version > I2OVER15) {
  514. osm_err("%s: Not running version 1.5 of the I2O Specification."
  515. "\n", c->name);
  516. return -ENODEV;
  517. }
  518. switch (sb->iop_state) {
  519. case ADAPTER_STATE_FAULTED:
  520. osm_err("%s: hardware fault\n", c->name);
  521. return -EFAULT;
  522. case ADAPTER_STATE_READY:
  523. case ADAPTER_STATE_OPERATIONAL:
  524. case ADAPTER_STATE_HOLD:
  525. case ADAPTER_STATE_FAILED:
  526. osm_debug("%s: already running, trying to reset...\n", c->name);
  527. rc = i2o_iop_reset(c);
  528. if (rc)
  529. return rc;
  530. }
  531. /* preserve state */
  532. state = sb->iop_state;
  533. rc = i2o_iop_init_outbound_queue(c);
  534. if (rc)
  535. return rc;
  536. /* if adapter was not in RESET state clear now */
  537. if (state != ADAPTER_STATE_RESET)
  538. i2o_iop_clear(c);
  539. i2o_status_get(c);
  540. if (sb->iop_state != ADAPTER_STATE_HOLD) {
  541. osm_err("%s: failed to bring IOP into HOLD state\n", c->name);
  542. return -EIO;
  543. }
  544. return i2o_hrt_get(c);
  545. };
  546. /**
  547. * i2o_iop_systab_set - Set the I2O System Table of the specified IOP
  548. * @c: I2O controller to which the system table should be send
  549. *
  550. * Before the systab could be set i2o_systab_build() must be called.
  551. *
  552. * Returns 0 on success or negative error code on failure.
  553. */
  554. static int i2o_iop_systab_set(struct i2o_controller *c)
  555. {
  556. struct i2o_message *msg;
  557. i2o_status_block *sb = c->status_block.virt;
  558. struct device *dev = &c->pdev->dev;
  559. struct resource *root;
  560. int rc;
  561. if (sb->current_mem_size < sb->desired_mem_size) {
  562. struct resource *res = &c->mem_resource;
  563. res->name = c->pdev->bus->name;
  564. res->flags = IORESOURCE_MEM;
  565. res->start = 0;
  566. res->end = 0;
  567. osm_info("%s: requires private memory resources.\n", c->name);
  568. root = pci_find_parent_resource(c->pdev, res);
  569. if (root == NULL)
  570. osm_warn("%s: Can't find parent resource!\n", c->name);
  571. if (root && allocate_resource(root, res, sb->desired_mem_size, sb->desired_mem_size, sb->desired_mem_size, 1 << 20, /* Unspecified, so use 1Mb and play safe */
  572. NULL, NULL) >= 0) {
  573. c->mem_alloc = 1;
  574. sb->current_mem_size = resource_size(res);
  575. sb->current_mem_base = res->start;
  576. osm_info("%s: allocated %llu bytes of PCI memory at "
  577. "0x%016llX.\n", c->name,
  578. (unsigned long long)resource_size(res),
  579. (unsigned long long)res->start);
  580. }
  581. }
  582. if (sb->current_io_size < sb->desired_io_size) {
  583. struct resource *res = &c->io_resource;
  584. res->name = c->pdev->bus->name;
  585. res->flags = IORESOURCE_IO;
  586. res->start = 0;
  587. res->end = 0;
  588. osm_info("%s: requires private memory resources.\n", c->name);
  589. root = pci_find_parent_resource(c->pdev, res);
  590. if (root == NULL)
  591. osm_warn("%s: Can't find parent resource!\n", c->name);
  592. if (root && allocate_resource(root, res, sb->desired_io_size, sb->desired_io_size, sb->desired_io_size, 1 << 20, /* Unspecified, so use 1Mb and play safe */
  593. NULL, NULL) >= 0) {
  594. c->io_alloc = 1;
  595. sb->current_io_size = resource_size(res);
  596. sb->current_mem_base = res->start;
  597. osm_info("%s: allocated %llu bytes of PCI I/O at "
  598. "0x%016llX.\n", c->name,
  599. (unsigned long long)resource_size(res),
  600. (unsigned long long)res->start);
  601. }
  602. }
  603. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  604. if (IS_ERR(msg))
  605. return PTR_ERR(msg);
  606. i2o_systab.phys = dma_map_single(dev, i2o_systab.virt, i2o_systab.len,
  607. PCI_DMA_TODEVICE);
  608. if (!i2o_systab.phys) {
  609. i2o_msg_nop(c, msg);
  610. return -ENOMEM;
  611. }
  612. msg->u.head[0] = cpu_to_le32(I2O_MESSAGE_SIZE(12) | SGL_OFFSET_6);
  613. msg->u.head[1] =
  614. cpu_to_le32(I2O_CMD_SYS_TAB_SET << 24 | HOST_TID << 12 |
  615. ADAPTER_TID);
  616. /*
  617. * Provide three SGL-elements:
  618. * System table (SysTab), Private memory space declaration and
  619. * Private i/o space declaration
  620. */
  621. msg->body[0] = cpu_to_le32(c->unit + 2);
  622. msg->body[1] = cpu_to_le32(0x00000000);
  623. msg->body[2] = cpu_to_le32(0x54000000 | i2o_systab.len);
  624. msg->body[3] = cpu_to_le32(i2o_systab.phys);
  625. msg->body[4] = cpu_to_le32(0x54000000 | sb->current_mem_size);
  626. msg->body[5] = cpu_to_le32(sb->current_mem_base);
  627. msg->body[6] = cpu_to_le32(0xd4000000 | sb->current_io_size);
  628. msg->body[6] = cpu_to_le32(sb->current_io_base);
  629. rc = i2o_msg_post_wait(c, msg, 120);
  630. dma_unmap_single(dev, i2o_systab.phys, i2o_systab.len,
  631. PCI_DMA_TODEVICE);
  632. if (rc < 0)
  633. osm_err("%s: Unable to set SysTab (status=%#x).\n", c->name,
  634. -rc);
  635. else
  636. osm_debug("%s: SysTab set.\n", c->name);
  637. return rc;
  638. }
  639. /**
  640. * i2o_iop_online - Bring a controller online into OPERATIONAL state.
  641. * @c: I2O controller
  642. *
  643. * Send the system table and enable the I2O controller.
  644. *
  645. * Returns 0 on success or negative error code on failure.
  646. */
  647. static int i2o_iop_online(struct i2o_controller *c)
  648. {
  649. int rc;
  650. rc = i2o_iop_systab_set(c);
  651. if (rc)
  652. return rc;
  653. /* In READY state */
  654. osm_debug("%s: Attempting to enable...\n", c->name);
  655. rc = i2o_iop_enable(c);
  656. if (rc)
  657. return rc;
  658. return 0;
  659. };
  660. /**
  661. * i2o_iop_remove - Remove the I2O controller from the I2O core
  662. * @c: I2O controller
  663. *
  664. * Remove the I2O controller from the I2O core. If devices are attached to
  665. * the controller remove these also and finally reset the controller.
  666. */
  667. void i2o_iop_remove(struct i2o_controller *c)
  668. {
  669. struct i2o_device *dev, *tmp;
  670. osm_debug("%s: deleting controller\n", c->name);
  671. i2o_driver_notify_controller_remove_all(c);
  672. list_del(&c->list);
  673. list_for_each_entry_safe(dev, tmp, &c->devices, list)
  674. i2o_device_remove(dev);
  675. device_del(&c->device);
  676. /* Ask the IOP to switch to RESET state */
  677. i2o_iop_reset(c);
  678. }
  679. /**
  680. * i2o_systab_build - Build system table
  681. *
  682. * The system table contains information about all the IOPs in the system
  683. * (duh) and is used by the Executives on the IOPs to establish peer2peer
  684. * connections. We're not supporting peer2peer at the moment, but this
  685. * will be needed down the road for things like lan2lan forwarding.
  686. *
  687. * Returns 0 on success or negative error code on failure.
  688. */
  689. static int i2o_systab_build(void)
  690. {
  691. struct i2o_controller *c, *tmp;
  692. int num_controllers = 0;
  693. u32 change_ind = 0;
  694. int count = 0;
  695. struct i2o_sys_tbl *systab = i2o_systab.virt;
  696. list_for_each_entry_safe(c, tmp, &i2o_controllers, list)
  697. num_controllers++;
  698. if (systab) {
  699. change_ind = systab->change_ind;
  700. kfree(i2o_systab.virt);
  701. }
  702. /* Header + IOPs */
  703. i2o_systab.len = sizeof(struct i2o_sys_tbl) + num_controllers *
  704. sizeof(struct i2o_sys_tbl_entry);
  705. systab = i2o_systab.virt = kzalloc(i2o_systab.len, GFP_KERNEL);
  706. if (!systab) {
  707. osm_err("unable to allocate memory for System Table\n");
  708. return -ENOMEM;
  709. }
  710. systab->version = I2OVERSION;
  711. systab->change_ind = change_ind + 1;
  712. list_for_each_entry_safe(c, tmp, &i2o_controllers, list) {
  713. i2o_status_block *sb;
  714. if (count >= num_controllers) {
  715. osm_err("controller added while building system table"
  716. "\n");
  717. break;
  718. }
  719. sb = c->status_block.virt;
  720. /*
  721. * Get updated IOP state so we have the latest information
  722. *
  723. * We should delete the controller at this point if it
  724. * doesn't respond since if it's not on the system table
  725. * it is techninically not part of the I2O subsystem...
  726. */
  727. if (unlikely(i2o_status_get(c))) {
  728. osm_err("%s: Deleting b/c could not get status while "
  729. "attempting to build system table\n", c->name);
  730. i2o_iop_remove(c);
  731. continue; // try the next one
  732. }
  733. systab->iops[count].org_id = sb->org_id;
  734. systab->iops[count].iop_id = c->unit + 2;
  735. systab->iops[count].seg_num = 0;
  736. systab->iops[count].i2o_version = sb->i2o_version;
  737. systab->iops[count].iop_state = sb->iop_state;
  738. systab->iops[count].msg_type = sb->msg_type;
  739. systab->iops[count].frame_size = sb->inbound_frame_size;
  740. systab->iops[count].last_changed = change_ind;
  741. systab->iops[count].iop_capabilities = sb->iop_capabilities;
  742. systab->iops[count].inbound_low =
  743. i2o_dma_low(c->base.phys + I2O_IN_PORT);
  744. systab->iops[count].inbound_high =
  745. i2o_dma_high(c->base.phys + I2O_IN_PORT);
  746. count++;
  747. }
  748. systab->num_entries = count;
  749. return 0;
  750. };
  751. /**
  752. * i2o_parse_hrt - Parse the hardware resource table.
  753. * @c: I2O controller
  754. *
  755. * We don't do anything with it except dumping it (in debug mode).
  756. *
  757. * Returns 0.
  758. */
  759. static int i2o_parse_hrt(struct i2o_controller *c)
  760. {
  761. i2o_dump_hrt(c);
  762. return 0;
  763. };
  764. /**
  765. * i2o_status_get - Get the status block from the I2O controller
  766. * @c: I2O controller
  767. *
  768. * Issue a status query on the controller. This updates the attached
  769. * status block. The status block could then be accessed through
  770. * c->status_block.
  771. *
  772. * Returns 0 on success or negative error code on failure.
  773. */
  774. int i2o_status_get(struct i2o_controller *c)
  775. {
  776. struct i2o_message *msg;
  777. volatile u8 *status_block;
  778. unsigned long timeout;
  779. status_block = (u8 *) c->status_block.virt;
  780. memset(c->status_block.virt, 0, sizeof(i2o_status_block));
  781. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  782. if (IS_ERR(msg))
  783. return PTR_ERR(msg);
  784. msg->u.head[0] = cpu_to_le32(NINE_WORD_MSG_SIZE | SGL_OFFSET_0);
  785. msg->u.head[1] =
  786. cpu_to_le32(I2O_CMD_STATUS_GET << 24 | HOST_TID << 12 |
  787. ADAPTER_TID);
  788. msg->u.s.icntxt = cpu_to_le32(i2o_exec_driver.context);
  789. msg->u.s.tcntxt = cpu_to_le32(0x00000000);
  790. msg->body[0] = cpu_to_le32(0x00000000);
  791. msg->body[1] = cpu_to_le32(0x00000000);
  792. msg->body[2] = cpu_to_le32(i2o_dma_low(c->status_block.phys));
  793. msg->body[3] = cpu_to_le32(i2o_dma_high(c->status_block.phys));
  794. msg->body[4] = cpu_to_le32(sizeof(i2o_status_block)); /* always 88 bytes */
  795. i2o_msg_post(c, msg);
  796. /* Wait for a reply */
  797. timeout = jiffies + I2O_TIMEOUT_STATUS_GET * HZ;
  798. while (status_block[87] != 0xFF) {
  799. if (time_after(jiffies, timeout)) {
  800. osm_err("%s: Get status timeout.\n", c->name);
  801. return -ETIMEDOUT;
  802. }
  803. schedule_timeout_uninterruptible(1);
  804. }
  805. #ifdef DEBUG
  806. i2o_debug_state(c);
  807. #endif
  808. return 0;
  809. }
  810. /*
  811. * i2o_hrt_get - Get the Hardware Resource Table from the I2O controller
  812. * @c: I2O controller from which the HRT should be fetched
  813. *
  814. * The HRT contains information about possible hidden devices but is
  815. * mostly useless to us.
  816. *
  817. * Returns 0 on success or negative error code on failure.
  818. */
  819. static int i2o_hrt_get(struct i2o_controller *c)
  820. {
  821. int rc;
  822. int i;
  823. i2o_hrt *hrt = c->hrt.virt;
  824. u32 size = sizeof(i2o_hrt);
  825. struct device *dev = &c->pdev->dev;
  826. for (i = 0; i < I2O_HRT_GET_TRIES; i++) {
  827. struct i2o_message *msg;
  828. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  829. if (IS_ERR(msg))
  830. return PTR_ERR(msg);
  831. msg->u.head[0] = cpu_to_le32(SIX_WORD_MSG_SIZE | SGL_OFFSET_4);
  832. msg->u.head[1] =
  833. cpu_to_le32(I2O_CMD_HRT_GET << 24 | HOST_TID << 12 |
  834. ADAPTER_TID);
  835. msg->body[0] = cpu_to_le32(0xd0000000 | c->hrt.len);
  836. msg->body[1] = cpu_to_le32(c->hrt.phys);
  837. rc = i2o_msg_post_wait_mem(c, msg, 20, &c->hrt);
  838. if (rc < 0) {
  839. osm_err("%s: Unable to get HRT (status=%#x)\n", c->name,
  840. -rc);
  841. return rc;
  842. }
  843. size = hrt->num_entries * hrt->entry_len << 2;
  844. if (size > c->hrt.len) {
  845. if (i2o_dma_realloc(dev, &c->hrt, size))
  846. return -ENOMEM;
  847. else
  848. hrt = c->hrt.virt;
  849. } else
  850. return i2o_parse_hrt(c);
  851. }
  852. osm_err("%s: Unable to get HRT after %d tries, giving up\n", c->name,
  853. I2O_HRT_GET_TRIES);
  854. return -EBUSY;
  855. }
  856. /**
  857. * i2o_iop_release - release the memory for a I2O controller
  858. * @dev: I2O controller which should be released
  859. *
  860. * Release the allocated memory. This function is called if refcount of
  861. * device reaches 0 automatically.
  862. */
  863. static void i2o_iop_release(struct device *dev)
  864. {
  865. struct i2o_controller *c = to_i2o_controller(dev);
  866. i2o_iop_free(c);
  867. };
  868. /**
  869. * i2o_iop_alloc - Allocate and initialize a i2o_controller struct
  870. *
  871. * Allocate the necessary memory for a i2o_controller struct and
  872. * initialize the lists and message mempool.
  873. *
  874. * Returns a pointer to the I2O controller or a negative error code on
  875. * failure.
  876. */
  877. struct i2o_controller *i2o_iop_alloc(void)
  878. {
  879. static int unit = 0; /* 0 and 1 are NULL IOP and Local Host */
  880. struct i2o_controller *c;
  881. char poolname[32];
  882. c = kzalloc(sizeof(*c), GFP_KERNEL);
  883. if (!c) {
  884. osm_err("i2o: Insufficient memory to allocate a I2O controller."
  885. "\n");
  886. return ERR_PTR(-ENOMEM);
  887. }
  888. c->unit = unit++;
  889. sprintf(c->name, "iop%d", c->unit);
  890. snprintf(poolname, sizeof(poolname), "i2o_%s_msg_inpool", c->name);
  891. if (i2o_pool_alloc
  892. (&c->in_msg, poolname, I2O_INBOUND_MSG_FRAME_SIZE * 4 + sizeof(u32),
  893. I2O_MSG_INPOOL_MIN)) {
  894. kfree(c);
  895. return ERR_PTR(-ENOMEM);
  896. };
  897. INIT_LIST_HEAD(&c->devices);
  898. spin_lock_init(&c->lock);
  899. mutex_init(&c->lct_lock);
  900. device_initialize(&c->device);
  901. c->device.release = &i2o_iop_release;
  902. dev_set_name(&c->device, "iop%d", c->unit);
  903. #if BITS_PER_LONG == 64
  904. spin_lock_init(&c->context_list_lock);
  905. atomic_set(&c->context_list_counter, 0);
  906. INIT_LIST_HEAD(&c->context_list);
  907. #endif
  908. return c;
  909. };
  910. /**
  911. * i2o_iop_add - Initialize the I2O controller and add him to the I2O core
  912. * @c: controller
  913. *
  914. * Initialize the I2O controller and if no error occurs add him to the I2O
  915. * core.
  916. *
  917. * Returns 0 on success or negative error code on failure.
  918. */
  919. int i2o_iop_add(struct i2o_controller *c)
  920. {
  921. int rc;
  922. if ((rc = device_add(&c->device))) {
  923. osm_err("%s: could not add controller\n", c->name);
  924. goto iop_reset;
  925. }
  926. osm_info("%s: Activating I2O controller...\n", c->name);
  927. osm_info("%s: This may take a few minutes if there are many devices\n",
  928. c->name);
  929. if ((rc = i2o_iop_activate(c))) {
  930. osm_err("%s: could not activate controller\n", c->name);
  931. goto device_del;
  932. }
  933. osm_debug("%s: building sys table...\n", c->name);
  934. if ((rc = i2o_systab_build()))
  935. goto device_del;
  936. osm_debug("%s: online controller...\n", c->name);
  937. if ((rc = i2o_iop_online(c)))
  938. goto device_del;
  939. osm_debug("%s: getting LCT...\n", c->name);
  940. if ((rc = i2o_exec_lct_get(c)))
  941. goto device_del;
  942. list_add(&c->list, &i2o_controllers);
  943. i2o_driver_notify_controller_add_all(c);
  944. osm_info("%s: Controller added\n", c->name);
  945. return 0;
  946. device_del:
  947. device_del(&c->device);
  948. iop_reset:
  949. i2o_iop_reset(c);
  950. return rc;
  951. };
  952. /**
  953. * i2o_event_register - Turn on/off event notification for a I2O device
  954. * @dev: I2O device which should receive the event registration request
  955. * @drv: driver which want to get notified
  956. * @tcntxt: transaction context to use with this notifier
  957. * @evt_mask: mask of events
  958. *
  959. * Create and posts an event registration message to the task. No reply
  960. * is waited for, or expected. If you do not want further notifications,
  961. * call the i2o_event_register again with a evt_mask of 0.
  962. *
  963. * Returns 0 on success or negative error code on failure.
  964. */
  965. int i2o_event_register(struct i2o_device *dev, struct i2o_driver *drv,
  966. int tcntxt, u32 evt_mask)
  967. {
  968. struct i2o_controller *c = dev->iop;
  969. struct i2o_message *msg;
  970. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  971. if (IS_ERR(msg))
  972. return PTR_ERR(msg);
  973. msg->u.head[0] = cpu_to_le32(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0);
  974. msg->u.head[1] =
  975. cpu_to_le32(I2O_CMD_UTIL_EVT_REGISTER << 24 | HOST_TID << 12 | dev->
  976. lct_data.tid);
  977. msg->u.s.icntxt = cpu_to_le32(drv->context);
  978. msg->u.s.tcntxt = cpu_to_le32(tcntxt);
  979. msg->body[0] = cpu_to_le32(evt_mask);
  980. i2o_msg_post(c, msg);
  981. return 0;
  982. };
  983. /**
  984. * i2o_iop_init - I2O main initialization function
  985. *
  986. * Initialize the I2O drivers (OSM) functions, register the Executive OSM,
  987. * initialize the I2O PCI part and finally initialize I2O device stuff.
  988. *
  989. * Returns 0 on success or negative error code on failure.
  990. */
  991. static int __init i2o_iop_init(void)
  992. {
  993. int rc = 0;
  994. printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");
  995. if ((rc = i2o_driver_init()))
  996. goto exit;
  997. if ((rc = i2o_exec_init()))
  998. goto driver_exit;
  999. if ((rc = i2o_pci_init()))
  1000. goto exec_exit;
  1001. return 0;
  1002. exec_exit:
  1003. i2o_exec_exit();
  1004. driver_exit:
  1005. i2o_driver_exit();
  1006. exit:
  1007. return rc;
  1008. }
  1009. /**
  1010. * i2o_iop_exit - I2O main exit function
  1011. *
  1012. * Removes I2O controllers from PCI subsystem and shut down OSMs.
  1013. */
  1014. static void __exit i2o_iop_exit(void)
  1015. {
  1016. i2o_pci_exit();
  1017. i2o_exec_exit();
  1018. i2o_driver_exit();
  1019. };
  1020. module_init(i2o_iop_init);
  1021. module_exit(i2o_iop_exit);
  1022. MODULE_AUTHOR("Red Hat Software");
  1023. MODULE_LICENSE("GPL");
  1024. MODULE_DESCRIPTION(OSM_DESCRIPTION);
  1025. MODULE_VERSION(OSM_VERSION);
  1026. #if BITS_PER_LONG == 64
  1027. EXPORT_SYMBOL(i2o_cntxt_list_add);
  1028. EXPORT_SYMBOL(i2o_cntxt_list_get);
  1029. EXPORT_SYMBOL(i2o_cntxt_list_remove);
  1030. EXPORT_SYMBOL(i2o_cntxt_list_get_ptr);
  1031. #endif
  1032. EXPORT_SYMBOL(i2o_msg_get_wait);
  1033. EXPORT_SYMBOL(i2o_find_iop);
  1034. EXPORT_SYMBOL(i2o_iop_find_device);
  1035. EXPORT_SYMBOL(i2o_event_register);
  1036. EXPORT_SYMBOL(i2o_status_get);
  1037. EXPORT_SYMBOL(i2o_controllers);