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@redhat.com>:
  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 "core.h"
  32. #define OSM_NAME "i2o"
  33. #define OSM_VERSION "1.325"
  34. #define OSM_DESCRIPTION "I2O subsystem"
  35. /* global I2O controller list */
  36. LIST_HEAD(i2o_controllers);
  37. /*
  38. * global I2O System Table. Contains information about all the IOPs in the
  39. * system. Used to inform IOPs about each others existence.
  40. */
  41. static struct i2o_dma i2o_systab;
  42. static int i2o_hrt_get(struct i2o_controller *c);
  43. /**
  44. * i2o_msg_get_wait - obtain an I2O message from the IOP
  45. * @c: I2O controller
  46. * @msg: pointer to a I2O message pointer
  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 succes 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 succes 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 ammount 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 = 1 + res->end - res->start;
  575. sb->current_mem_base = res->start;
  576. osm_info("%s: allocated %ld bytes of PCI memory at "
  577. "0x%08lX.\n", c->name,
  578. 1 + res->end - res->start, res->start);
  579. }
  580. }
  581. if (sb->current_io_size < sb->desired_io_size) {
  582. struct resource *res = &c->io_resource;
  583. res->name = c->pdev->bus->name;
  584. res->flags = IORESOURCE_IO;
  585. res->start = 0;
  586. res->end = 0;
  587. osm_info("%s: requires private memory resources.\n", c->name);
  588. root = pci_find_parent_resource(c->pdev, res);
  589. if (root == NULL)
  590. osm_warn("%s: Can't find parent resource!\n", c->name);
  591. 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 */
  592. NULL, NULL) >= 0) {
  593. c->io_alloc = 1;
  594. sb->current_io_size = 1 + res->end - res->start;
  595. sb->current_mem_base = res->start;
  596. osm_info("%s: allocated %ld bytes of PCI I/O at 0x%08lX"
  597. ".\n", c->name, 1 + res->end - res->start,
  598. res->start);
  599. }
  600. }
  601. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  602. if (IS_ERR(msg))
  603. return PTR_ERR(msg);
  604. i2o_systab.phys = dma_map_single(dev, i2o_systab.virt, i2o_systab.len,
  605. PCI_DMA_TODEVICE);
  606. if (!i2o_systab.phys) {
  607. i2o_msg_nop(c, msg);
  608. return -ENOMEM;
  609. }
  610. msg->u.head[0] = cpu_to_le32(I2O_MESSAGE_SIZE(12) | SGL_OFFSET_6);
  611. msg->u.head[1] =
  612. cpu_to_le32(I2O_CMD_SYS_TAB_SET << 24 | HOST_TID << 12 |
  613. ADAPTER_TID);
  614. /*
  615. * Provide three SGL-elements:
  616. * System table (SysTab), Private memory space declaration and
  617. * Private i/o space declaration
  618. */
  619. msg->body[0] = cpu_to_le32(c->unit + 2);
  620. msg->body[1] = cpu_to_le32(0x00000000);
  621. msg->body[2] = cpu_to_le32(0x54000000 | i2o_systab.len);
  622. msg->body[3] = cpu_to_le32(i2o_systab.phys);
  623. msg->body[4] = cpu_to_le32(0x54000000 | sb->current_mem_size);
  624. msg->body[5] = cpu_to_le32(sb->current_mem_base);
  625. msg->body[6] = cpu_to_le32(0xd4000000 | sb->current_io_size);
  626. msg->body[6] = cpu_to_le32(sb->current_io_base);
  627. rc = i2o_msg_post_wait(c, msg, 120);
  628. dma_unmap_single(dev, i2o_systab.phys, i2o_systab.len,
  629. PCI_DMA_TODEVICE);
  630. if (rc < 0)
  631. osm_err("%s: Unable to set SysTab (status=%#x).\n", c->name,
  632. -rc);
  633. else
  634. osm_debug("%s: SysTab set.\n", c->name);
  635. return rc;
  636. }
  637. /**
  638. * i2o_iop_online - Bring a controller online into OPERATIONAL state.
  639. * @c: I2O controller
  640. *
  641. * Send the system table and enable the I2O controller.
  642. *
  643. * Returns 0 on success or negative error code on failure.
  644. */
  645. static int i2o_iop_online(struct i2o_controller *c)
  646. {
  647. int rc;
  648. rc = i2o_iop_systab_set(c);
  649. if (rc)
  650. return rc;
  651. /* In READY state */
  652. osm_debug("%s: Attempting to enable...\n", c->name);
  653. rc = i2o_iop_enable(c);
  654. if (rc)
  655. return rc;
  656. return 0;
  657. };
  658. /**
  659. * i2o_iop_remove - Remove the I2O controller from the I2O core
  660. * @c: I2O controller
  661. *
  662. * Remove the I2O controller from the I2O core. If devices are attached to
  663. * the controller remove these also and finally reset the controller.
  664. */
  665. void i2o_iop_remove(struct i2o_controller *c)
  666. {
  667. struct i2o_device *dev, *tmp;
  668. osm_debug("%s: deleting controller\n", c->name);
  669. i2o_driver_notify_controller_remove_all(c);
  670. list_del(&c->list);
  671. list_for_each_entry_safe(dev, tmp, &c->devices, list)
  672. i2o_device_remove(dev);
  673. device_del(&c->device);
  674. /* Ask the IOP to switch to RESET state */
  675. i2o_iop_reset(c);
  676. put_device(&c->device);
  677. }
  678. /**
  679. * i2o_systab_build - Build system table
  680. *
  681. * The system table contains information about all the IOPs in the system
  682. * (duh) and is used by the Executives on the IOPs to establish peer2peer
  683. * connections. We're not supporting peer2peer at the moment, but this
  684. * will be needed down the road for things like lan2lan forwarding.
  685. *
  686. * Returns 0 on success or negative error code on failure.
  687. */
  688. static int i2o_systab_build(void)
  689. {
  690. struct i2o_controller *c, *tmp;
  691. int num_controllers = 0;
  692. u32 change_ind = 0;
  693. int count = 0;
  694. struct i2o_sys_tbl *systab = i2o_systab.virt;
  695. list_for_each_entry_safe(c, tmp, &i2o_controllers, list)
  696. num_controllers++;
  697. if (systab) {
  698. change_ind = systab->change_ind;
  699. kfree(i2o_systab.virt);
  700. }
  701. /* Header + IOPs */
  702. i2o_systab.len = sizeof(struct i2o_sys_tbl) + num_controllers *
  703. sizeof(struct i2o_sys_tbl_entry);
  704. systab = i2o_systab.virt = kzalloc(i2o_systab.len, GFP_KERNEL);
  705. if (!systab) {
  706. osm_err("unable to allocate memory for System Table\n");
  707. return -ENOMEM;
  708. }
  709. systab->version = I2OVERSION;
  710. systab->change_ind = change_ind + 1;
  711. list_for_each_entry_safe(c, tmp, &i2o_controllers, list) {
  712. i2o_status_block *sb;
  713. if (count >= num_controllers) {
  714. osm_err("controller added while building system table"
  715. "\n");
  716. break;
  717. }
  718. sb = c->status_block.virt;
  719. /*
  720. * Get updated IOP state so we have the latest information
  721. *
  722. * We should delete the controller at this point if it
  723. * doesn't respond since if it's not on the system table
  724. * it is techninically not part of the I2O subsystem...
  725. */
  726. if (unlikely(i2o_status_get(c))) {
  727. osm_err("%s: Deleting b/c could not get status while "
  728. "attempting to build system table\n", c->name);
  729. i2o_iop_remove(c);
  730. continue; // try the next one
  731. }
  732. systab->iops[count].org_id = sb->org_id;
  733. systab->iops[count].iop_id = c->unit + 2;
  734. systab->iops[count].seg_num = 0;
  735. systab->iops[count].i2o_version = sb->i2o_version;
  736. systab->iops[count].iop_state = sb->iop_state;
  737. systab->iops[count].msg_type = sb->msg_type;
  738. systab->iops[count].frame_size = sb->inbound_frame_size;
  739. systab->iops[count].last_changed = change_ind;
  740. systab->iops[count].iop_capabilities = sb->iop_capabilities;
  741. systab->iops[count].inbound_low =
  742. i2o_dma_low(c->base.phys + I2O_IN_PORT);
  743. systab->iops[count].inbound_high =
  744. i2o_dma_high(c->base.phys + I2O_IN_PORT);
  745. count++;
  746. }
  747. systab->num_entries = count;
  748. return 0;
  749. };
  750. /**
  751. * i2o_parse_hrt - Parse the hardware resource table.
  752. * @c: I2O controller
  753. *
  754. * We don't do anything with it except dumping it (in debug mode).
  755. *
  756. * Returns 0.
  757. */
  758. static int i2o_parse_hrt(struct i2o_controller *c)
  759. {
  760. i2o_dump_hrt(c);
  761. return 0;
  762. };
  763. /**
  764. * i2o_status_get - Get the status block from the I2O controller
  765. * @c: I2O controller
  766. *
  767. * Issue a status query on the controller. This updates the attached
  768. * status block. The status block could then be accessed through
  769. * c->status_block.
  770. *
  771. * Returns 0 on sucess or negative error code on failure.
  772. */
  773. int i2o_status_get(struct i2o_controller *c)
  774. {
  775. struct i2o_message *msg;
  776. volatile u8 *status_block;
  777. unsigned long timeout;
  778. status_block = (u8 *) c->status_block.virt;
  779. memset(c->status_block.virt, 0, sizeof(i2o_status_block));
  780. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  781. if (IS_ERR(msg))
  782. return PTR_ERR(msg);
  783. msg->u.head[0] = cpu_to_le32(NINE_WORD_MSG_SIZE | SGL_OFFSET_0);
  784. msg->u.head[1] =
  785. cpu_to_le32(I2O_CMD_STATUS_GET << 24 | HOST_TID << 12 |
  786. ADAPTER_TID);
  787. msg->u.s.icntxt = cpu_to_le32(i2o_exec_driver.context);
  788. msg->u.s.tcntxt = cpu_to_le32(0x00000000);
  789. msg->body[0] = cpu_to_le32(0x00000000);
  790. msg->body[1] = cpu_to_le32(0x00000000);
  791. msg->body[2] = cpu_to_le32(i2o_dma_low(c->status_block.phys));
  792. msg->body[3] = cpu_to_le32(i2o_dma_high(c->status_block.phys));
  793. msg->body[4] = cpu_to_le32(sizeof(i2o_status_block)); /* always 88 bytes */
  794. i2o_msg_post(c, msg);
  795. /* Wait for a reply */
  796. timeout = jiffies + I2O_TIMEOUT_STATUS_GET * HZ;
  797. while (status_block[87] != 0xFF) {
  798. if (time_after(jiffies, timeout)) {
  799. osm_err("%s: Get status timeout.\n", c->name);
  800. return -ETIMEDOUT;
  801. }
  802. schedule_timeout_uninterruptible(1);
  803. }
  804. #ifdef DEBUG
  805. i2o_debug_state(c);
  806. #endif
  807. return 0;
  808. }
  809. /*
  810. * i2o_hrt_get - Get the Hardware Resource Table from the I2O controller
  811. * @c: I2O controller from which the HRT should be fetched
  812. *
  813. * The HRT contains information about possible hidden devices but is
  814. * mostly useless to us.
  815. *
  816. * Returns 0 on success or negative error code on failure.
  817. */
  818. static int i2o_hrt_get(struct i2o_controller *c)
  819. {
  820. int rc;
  821. int i;
  822. i2o_hrt *hrt = c->hrt.virt;
  823. u32 size = sizeof(i2o_hrt);
  824. struct device *dev = &c->pdev->dev;
  825. for (i = 0; i < I2O_HRT_GET_TRIES; i++) {
  826. struct i2o_message *msg;
  827. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  828. if (IS_ERR(msg))
  829. return PTR_ERR(msg);
  830. msg->u.head[0] = cpu_to_le32(SIX_WORD_MSG_SIZE | SGL_OFFSET_4);
  831. msg->u.head[1] =
  832. cpu_to_le32(I2O_CMD_HRT_GET << 24 | HOST_TID << 12 |
  833. ADAPTER_TID);
  834. msg->body[0] = cpu_to_le32(0xd0000000 | c->hrt.len);
  835. msg->body[1] = cpu_to_le32(c->hrt.phys);
  836. rc = i2o_msg_post_wait_mem(c, msg, 20, &c->hrt);
  837. if (rc < 0) {
  838. osm_err("%s: Unable to get HRT (status=%#x)\n", c->name,
  839. -rc);
  840. return rc;
  841. }
  842. size = hrt->num_entries * hrt->entry_len << 2;
  843. if (size > c->hrt.len) {
  844. if (i2o_dma_realloc(dev, &c->hrt, size, GFP_KERNEL))
  845. return -ENOMEM;
  846. else
  847. hrt = c->hrt.virt;
  848. } else
  849. return i2o_parse_hrt(c);
  850. }
  851. osm_err("%s: Unable to get HRT after %d tries, giving up\n", c->name,
  852. I2O_HRT_GET_TRIES);
  853. return -EBUSY;
  854. }
  855. /**
  856. * i2o_iop_release - release the memory for a I2O controller
  857. * @dev: I2O controller which should be released
  858. *
  859. * Release the allocated memory. This function is called if refcount of
  860. * device reaches 0 automatically.
  861. */
  862. static void i2o_iop_release(struct device *dev)
  863. {
  864. struct i2o_controller *c = to_i2o_controller(dev);
  865. i2o_iop_free(c);
  866. };
  867. /**
  868. * i2o_iop_alloc - Allocate and initialize a i2o_controller struct
  869. *
  870. * Allocate the necessary memory for a i2o_controller struct and
  871. * initialize the lists and message mempool.
  872. *
  873. * Returns a pointer to the I2O controller or a negative error code on
  874. * failure.
  875. */
  876. struct i2o_controller *i2o_iop_alloc(void)
  877. {
  878. static int unit = 0; /* 0 and 1 are NULL IOP and Local Host */
  879. struct i2o_controller *c;
  880. char poolname[32];
  881. c = kzalloc(sizeof(*c), GFP_KERNEL);
  882. if (!c) {
  883. osm_err("i2o: Insufficient memory to allocate a I2O controller."
  884. "\n");
  885. return ERR_PTR(-ENOMEM);
  886. }
  887. c->unit = unit++;
  888. sprintf(c->name, "iop%d", c->unit);
  889. snprintf(poolname, sizeof(poolname), "i2o_%s_msg_inpool", c->name);
  890. if (i2o_pool_alloc
  891. (&c->in_msg, poolname, I2O_INBOUND_MSG_FRAME_SIZE * 4,
  892. I2O_MSG_INPOOL_MIN)) {
  893. kfree(c);
  894. return ERR_PTR(-ENOMEM);
  895. };
  896. INIT_LIST_HEAD(&c->devices);
  897. spin_lock_init(&c->lock);
  898. init_MUTEX(&c->lct_lock);
  899. device_initialize(&c->device);
  900. c->device.release = &i2o_iop_release;
  901. snprintf(c->device.bus_id, BUS_ID_SIZE, "iop%d", c->unit);
  902. #if BITS_PER_LONG == 64
  903. spin_lock_init(&c->context_list_lock);
  904. atomic_set(&c->context_list_counter, 0);
  905. INIT_LIST_HEAD(&c->context_list);
  906. #endif
  907. return c;
  908. };
  909. /**
  910. * i2o_iop_add - Initialize the I2O controller and add him to the I2O core
  911. * @c: controller
  912. *
  913. * Initialize the I2O controller and if no error occurs add him to the I2O
  914. * core.
  915. *
  916. * Returns 0 on success or negative error code on failure.
  917. */
  918. int i2o_iop_add(struct i2o_controller *c)
  919. {
  920. int rc;
  921. if ((rc = device_add(&c->device))) {
  922. osm_err("%s: could not add controller\n", c->name);
  923. goto iop_reset;
  924. }
  925. osm_info("%s: Activating I2O controller...\n", c->name);
  926. osm_info("%s: This may take a few minutes if there are many devices\n",
  927. c->name);
  928. if ((rc = i2o_iop_activate(c))) {
  929. osm_err("%s: could not activate controller\n", c->name);
  930. goto device_del;
  931. }
  932. osm_debug("%s: building sys table...\n", c->name);
  933. if ((rc = i2o_systab_build()))
  934. goto device_del;
  935. osm_debug("%s: online controller...\n", c->name);
  936. if ((rc = i2o_iop_online(c)))
  937. goto device_del;
  938. osm_debug("%s: getting LCT...\n", c->name);
  939. if ((rc = i2o_exec_lct_get(c)))
  940. goto device_del;
  941. list_add(&c->list, &i2o_controllers);
  942. i2o_driver_notify_controller_add_all(c);
  943. osm_info("%s: Controller added\n", c->name);
  944. return 0;
  945. device_del:
  946. device_del(&c->device);
  947. iop_reset:
  948. i2o_iop_reset(c);
  949. return rc;
  950. };
  951. /**
  952. * i2o_event_register - Turn on/off event notification for a I2O device
  953. * @dev: I2O device which should receive the event registration request
  954. * @drv: driver which want to get notified
  955. * @tcntxt: transaction context to use with this notifier
  956. * @evt_mask: mask of events
  957. *
  958. * Create and posts an event registration message to the task. No reply
  959. * is waited for, or expected. If you do not want further notifications,
  960. * call the i2o_event_register again with a evt_mask of 0.
  961. *
  962. * Returns 0 on success or negative error code on failure.
  963. */
  964. int i2o_event_register(struct i2o_device *dev, struct i2o_driver *drv,
  965. int tcntxt, u32 evt_mask)
  966. {
  967. struct i2o_controller *c = dev->iop;
  968. struct i2o_message *msg;
  969. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  970. if (IS_ERR(msg))
  971. return PTR_ERR(msg);
  972. msg->u.head[0] = cpu_to_le32(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0);
  973. msg->u.head[1] =
  974. cpu_to_le32(I2O_CMD_UTIL_EVT_REGISTER << 24 | HOST_TID << 12 | dev->
  975. lct_data.tid);
  976. msg->u.s.icntxt = cpu_to_le32(drv->context);
  977. msg->u.s.tcntxt = cpu_to_le32(tcntxt);
  978. msg->body[0] = cpu_to_le32(evt_mask);
  979. i2o_msg_post(c, msg);
  980. return 0;
  981. };
  982. /**
  983. * i2o_iop_init - I2O main initialization function
  984. *
  985. * Initialize the I2O drivers (OSM) functions, register the Executive OSM,
  986. * initialize the I2O PCI part and finally initialize I2O device stuff.
  987. *
  988. * Returns 0 on success or negative error code on failure.
  989. */
  990. static int __init i2o_iop_init(void)
  991. {
  992. int rc = 0;
  993. printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");
  994. if ((rc = i2o_driver_init()))
  995. goto exit;
  996. if ((rc = i2o_exec_init()))
  997. goto driver_exit;
  998. if ((rc = i2o_pci_init()))
  999. goto exec_exit;
  1000. return 0;
  1001. exec_exit:
  1002. i2o_exec_exit();
  1003. driver_exit:
  1004. i2o_driver_exit();
  1005. exit:
  1006. return rc;
  1007. }
  1008. /**
  1009. * i2o_iop_exit - I2O main exit function
  1010. *
  1011. * Removes I2O controllers from PCI subsystem and shut down OSMs.
  1012. */
  1013. static void __exit i2o_iop_exit(void)
  1014. {
  1015. i2o_pci_exit();
  1016. i2o_exec_exit();
  1017. i2o_driver_exit();
  1018. };
  1019. module_init(i2o_iop_init);
  1020. module_exit(i2o_iop_exit);
  1021. MODULE_AUTHOR("Red Hat Software");
  1022. MODULE_LICENSE("GPL");
  1023. MODULE_DESCRIPTION(OSM_DESCRIPTION);
  1024. MODULE_VERSION(OSM_VERSION);
  1025. #if BITS_PER_LONG == 64
  1026. EXPORT_SYMBOL(i2o_cntxt_list_add);
  1027. EXPORT_SYMBOL(i2o_cntxt_list_get);
  1028. EXPORT_SYMBOL(i2o_cntxt_list_remove);
  1029. EXPORT_SYMBOL(i2o_cntxt_list_get_ptr);
  1030. #endif
  1031. EXPORT_SYMBOL(i2o_msg_get_wait);
  1032. EXPORT_SYMBOL(i2o_msg_nop);
  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);