iop.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  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. }
  677. /**
  678. * i2o_systab_build - Build system table
  679. *
  680. * The system table contains information about all the IOPs in the system
  681. * (duh) and is used by the Executives on the IOPs to establish peer2peer
  682. * connections. We're not supporting peer2peer at the moment, but this
  683. * will be needed down the road for things like lan2lan forwarding.
  684. *
  685. * Returns 0 on success or negative error code on failure.
  686. */
  687. static int i2o_systab_build(void)
  688. {
  689. struct i2o_controller *c, *tmp;
  690. int num_controllers = 0;
  691. u32 change_ind = 0;
  692. int count = 0;
  693. struct i2o_sys_tbl *systab = i2o_systab.virt;
  694. list_for_each_entry_safe(c, tmp, &i2o_controllers, list)
  695. num_controllers++;
  696. if (systab) {
  697. change_ind = systab->change_ind;
  698. kfree(i2o_systab.virt);
  699. }
  700. /* Header + IOPs */
  701. i2o_systab.len = sizeof(struct i2o_sys_tbl) + num_controllers *
  702. sizeof(struct i2o_sys_tbl_entry);
  703. systab = i2o_systab.virt = kzalloc(i2o_systab.len, GFP_KERNEL);
  704. if (!systab) {
  705. osm_err("unable to allocate memory for System Table\n");
  706. return -ENOMEM;
  707. }
  708. systab->version = I2OVERSION;
  709. systab->change_ind = change_ind + 1;
  710. list_for_each_entry_safe(c, tmp, &i2o_controllers, list) {
  711. i2o_status_block *sb;
  712. if (count >= num_controllers) {
  713. osm_err("controller added while building system table"
  714. "\n");
  715. break;
  716. }
  717. sb = c->status_block.virt;
  718. /*
  719. * Get updated IOP state so we have the latest information
  720. *
  721. * We should delete the controller at this point if it
  722. * doesn't respond since if it's not on the system table
  723. * it is techninically not part of the I2O subsystem...
  724. */
  725. if (unlikely(i2o_status_get(c))) {
  726. osm_err("%s: Deleting b/c could not get status while "
  727. "attempting to build system table\n", c->name);
  728. i2o_iop_remove(c);
  729. continue; // try the next one
  730. }
  731. systab->iops[count].org_id = sb->org_id;
  732. systab->iops[count].iop_id = c->unit + 2;
  733. systab->iops[count].seg_num = 0;
  734. systab->iops[count].i2o_version = sb->i2o_version;
  735. systab->iops[count].iop_state = sb->iop_state;
  736. systab->iops[count].msg_type = sb->msg_type;
  737. systab->iops[count].frame_size = sb->inbound_frame_size;
  738. systab->iops[count].last_changed = change_ind;
  739. systab->iops[count].iop_capabilities = sb->iop_capabilities;
  740. systab->iops[count].inbound_low =
  741. i2o_dma_low(c->base.phys + I2O_IN_PORT);
  742. systab->iops[count].inbound_high =
  743. i2o_dma_high(c->base.phys + I2O_IN_PORT);
  744. count++;
  745. }
  746. systab->num_entries = count;
  747. return 0;
  748. };
  749. /**
  750. * i2o_parse_hrt - Parse the hardware resource table.
  751. * @c: I2O controller
  752. *
  753. * We don't do anything with it except dumping it (in debug mode).
  754. *
  755. * Returns 0.
  756. */
  757. static int i2o_parse_hrt(struct i2o_controller *c)
  758. {
  759. i2o_dump_hrt(c);
  760. return 0;
  761. };
  762. /**
  763. * i2o_status_get - Get the status block from the I2O controller
  764. * @c: I2O controller
  765. *
  766. * Issue a status query on the controller. This updates the attached
  767. * status block. The status block could then be accessed through
  768. * c->status_block.
  769. *
  770. * Returns 0 on sucess or negative error code on failure.
  771. */
  772. int i2o_status_get(struct i2o_controller *c)
  773. {
  774. struct i2o_message *msg;
  775. volatile u8 *status_block;
  776. unsigned long timeout;
  777. status_block = (u8 *) c->status_block.virt;
  778. memset(c->status_block.virt, 0, sizeof(i2o_status_block));
  779. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  780. if (IS_ERR(msg))
  781. return PTR_ERR(msg);
  782. msg->u.head[0] = cpu_to_le32(NINE_WORD_MSG_SIZE | SGL_OFFSET_0);
  783. msg->u.head[1] =
  784. cpu_to_le32(I2O_CMD_STATUS_GET << 24 | HOST_TID << 12 |
  785. ADAPTER_TID);
  786. msg->u.s.icntxt = cpu_to_le32(i2o_exec_driver.context);
  787. msg->u.s.tcntxt = cpu_to_le32(0x00000000);
  788. msg->body[0] = cpu_to_le32(0x00000000);
  789. msg->body[1] = cpu_to_le32(0x00000000);
  790. msg->body[2] = cpu_to_le32(i2o_dma_low(c->status_block.phys));
  791. msg->body[3] = cpu_to_le32(i2o_dma_high(c->status_block.phys));
  792. msg->body[4] = cpu_to_le32(sizeof(i2o_status_block)); /* always 88 bytes */
  793. i2o_msg_post(c, msg);
  794. /* Wait for a reply */
  795. timeout = jiffies + I2O_TIMEOUT_STATUS_GET * HZ;
  796. while (status_block[87] != 0xFF) {
  797. if (time_after(jiffies, timeout)) {
  798. osm_err("%s: Get status timeout.\n", c->name);
  799. return -ETIMEDOUT;
  800. }
  801. schedule_timeout_uninterruptible(1);
  802. }
  803. #ifdef DEBUG
  804. i2o_debug_state(c);
  805. #endif
  806. return 0;
  807. }
  808. /*
  809. * i2o_hrt_get - Get the Hardware Resource Table from the I2O controller
  810. * @c: I2O controller from which the HRT should be fetched
  811. *
  812. * The HRT contains information about possible hidden devices but is
  813. * mostly useless to us.
  814. *
  815. * Returns 0 on success or negative error code on failure.
  816. */
  817. static int i2o_hrt_get(struct i2o_controller *c)
  818. {
  819. int rc;
  820. int i;
  821. i2o_hrt *hrt = c->hrt.virt;
  822. u32 size = sizeof(i2o_hrt);
  823. struct device *dev = &c->pdev->dev;
  824. for (i = 0; i < I2O_HRT_GET_TRIES; i++) {
  825. struct i2o_message *msg;
  826. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  827. if (IS_ERR(msg))
  828. return PTR_ERR(msg);
  829. msg->u.head[0] = cpu_to_le32(SIX_WORD_MSG_SIZE | SGL_OFFSET_4);
  830. msg->u.head[1] =
  831. cpu_to_le32(I2O_CMD_HRT_GET << 24 | HOST_TID << 12 |
  832. ADAPTER_TID);
  833. msg->body[0] = cpu_to_le32(0xd0000000 | c->hrt.len);
  834. msg->body[1] = cpu_to_le32(c->hrt.phys);
  835. rc = i2o_msg_post_wait_mem(c, msg, 20, &c->hrt);
  836. if (rc < 0) {
  837. osm_err("%s: Unable to get HRT (status=%#x)\n", c->name,
  838. -rc);
  839. return rc;
  840. }
  841. size = hrt->num_entries * hrt->entry_len << 2;
  842. if (size > c->hrt.len) {
  843. if (i2o_dma_realloc(dev, &c->hrt, size, GFP_KERNEL))
  844. return -ENOMEM;
  845. else
  846. hrt = c->hrt.virt;
  847. } else
  848. return i2o_parse_hrt(c);
  849. }
  850. osm_err("%s: Unable to get HRT after %d tries, giving up\n", c->name,
  851. I2O_HRT_GET_TRIES);
  852. return -EBUSY;
  853. }
  854. /**
  855. * i2o_iop_release - release the memory for a I2O controller
  856. * @dev: I2O controller which should be released
  857. *
  858. * Release the allocated memory. This function is called if refcount of
  859. * device reaches 0 automatically.
  860. */
  861. static void i2o_iop_release(struct device *dev)
  862. {
  863. struct i2o_controller *c = to_i2o_controller(dev);
  864. i2o_iop_free(c);
  865. };
  866. /**
  867. * i2o_iop_alloc - Allocate and initialize a i2o_controller struct
  868. *
  869. * Allocate the necessary memory for a i2o_controller struct and
  870. * initialize the lists and message mempool.
  871. *
  872. * Returns a pointer to the I2O controller or a negative error code on
  873. * failure.
  874. */
  875. struct i2o_controller *i2o_iop_alloc(void)
  876. {
  877. static int unit = 0; /* 0 and 1 are NULL IOP and Local Host */
  878. struct i2o_controller *c;
  879. char poolname[32];
  880. c = kzalloc(sizeof(*c), GFP_KERNEL);
  881. if (!c) {
  882. osm_err("i2o: Insufficient memory to allocate a I2O controller."
  883. "\n");
  884. return ERR_PTR(-ENOMEM);
  885. }
  886. c->unit = unit++;
  887. sprintf(c->name, "iop%d", c->unit);
  888. snprintf(poolname, sizeof(poolname), "i2o_%s_msg_inpool", c->name);
  889. if (i2o_pool_alloc
  890. (&c->in_msg, poolname, I2O_INBOUND_MSG_FRAME_SIZE * 4 + sizeof(u32),
  891. I2O_MSG_INPOOL_MIN)) {
  892. kfree(c);
  893. return ERR_PTR(-ENOMEM);
  894. };
  895. INIT_LIST_HEAD(&c->devices);
  896. spin_lock_init(&c->lock);
  897. init_MUTEX(&c->lct_lock);
  898. device_initialize(&c->device);
  899. c->device.release = &i2o_iop_release;
  900. snprintf(c->device.bus_id, BUS_ID_SIZE, "iop%d", c->unit);
  901. #if BITS_PER_LONG == 64
  902. spin_lock_init(&c->context_list_lock);
  903. atomic_set(&c->context_list_counter, 0);
  904. INIT_LIST_HEAD(&c->context_list);
  905. #endif
  906. return c;
  907. };
  908. /**
  909. * i2o_iop_add - Initialize the I2O controller and add him to the I2O core
  910. * @c: controller
  911. *
  912. * Initialize the I2O controller and if no error occurs add him to the I2O
  913. * core.
  914. *
  915. * Returns 0 on success or negative error code on failure.
  916. */
  917. int i2o_iop_add(struct i2o_controller *c)
  918. {
  919. int rc;
  920. if ((rc = device_add(&c->device))) {
  921. osm_err("%s: could not add controller\n", c->name);
  922. goto iop_reset;
  923. }
  924. osm_info("%s: Activating I2O controller...\n", c->name);
  925. osm_info("%s: This may take a few minutes if there are many devices\n",
  926. c->name);
  927. if ((rc = i2o_iop_activate(c))) {
  928. osm_err("%s: could not activate controller\n", c->name);
  929. goto device_del;
  930. }
  931. osm_debug("%s: building sys table...\n", c->name);
  932. if ((rc = i2o_systab_build()))
  933. goto device_del;
  934. osm_debug("%s: online controller...\n", c->name);
  935. if ((rc = i2o_iop_online(c)))
  936. goto device_del;
  937. osm_debug("%s: getting LCT...\n", c->name);
  938. if ((rc = i2o_exec_lct_get(c)))
  939. goto device_del;
  940. list_add(&c->list, &i2o_controllers);
  941. i2o_driver_notify_controller_add_all(c);
  942. osm_info("%s: Controller added\n", c->name);
  943. return 0;
  944. device_del:
  945. device_del(&c->device);
  946. iop_reset:
  947. i2o_iop_reset(c);
  948. return rc;
  949. };
  950. /**
  951. * i2o_event_register - Turn on/off event notification for a I2O device
  952. * @dev: I2O device which should receive the event registration request
  953. * @drv: driver which want to get notified
  954. * @tcntxt: transaction context to use with this notifier
  955. * @evt_mask: mask of events
  956. *
  957. * Create and posts an event registration message to the task. No reply
  958. * is waited for, or expected. If you do not want further notifications,
  959. * call the i2o_event_register again with a evt_mask of 0.
  960. *
  961. * Returns 0 on success or negative error code on failure.
  962. */
  963. int i2o_event_register(struct i2o_device *dev, struct i2o_driver *drv,
  964. int tcntxt, u32 evt_mask)
  965. {
  966. struct i2o_controller *c = dev->iop;
  967. struct i2o_message *msg;
  968. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  969. if (IS_ERR(msg))
  970. return PTR_ERR(msg);
  971. msg->u.head[0] = cpu_to_le32(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0);
  972. msg->u.head[1] =
  973. cpu_to_le32(I2O_CMD_UTIL_EVT_REGISTER << 24 | HOST_TID << 12 | dev->
  974. lct_data.tid);
  975. msg->u.s.icntxt = cpu_to_le32(drv->context);
  976. msg->u.s.tcntxt = cpu_to_le32(tcntxt);
  977. msg->body[0] = cpu_to_le32(evt_mask);
  978. i2o_msg_post(c, msg);
  979. return 0;
  980. };
  981. /**
  982. * i2o_iop_init - I2O main initialization function
  983. *
  984. * Initialize the I2O drivers (OSM) functions, register the Executive OSM,
  985. * initialize the I2O PCI part and finally initialize I2O device stuff.
  986. *
  987. * Returns 0 on success or negative error code on failure.
  988. */
  989. static int __init i2o_iop_init(void)
  990. {
  991. int rc = 0;
  992. printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");
  993. if ((rc = i2o_driver_init()))
  994. goto exit;
  995. if ((rc = i2o_exec_init()))
  996. goto driver_exit;
  997. if ((rc = i2o_pci_init()))
  998. goto exec_exit;
  999. return 0;
  1000. exec_exit:
  1001. i2o_exec_exit();
  1002. driver_exit:
  1003. i2o_driver_exit();
  1004. exit:
  1005. return rc;
  1006. }
  1007. /**
  1008. * i2o_iop_exit - I2O main exit function
  1009. *
  1010. * Removes I2O controllers from PCI subsystem and shut down OSMs.
  1011. */
  1012. static void __exit i2o_iop_exit(void)
  1013. {
  1014. i2o_pci_exit();
  1015. i2o_exec_exit();
  1016. i2o_driver_exit();
  1017. };
  1018. module_init(i2o_iop_init);
  1019. module_exit(i2o_iop_exit);
  1020. MODULE_AUTHOR("Red Hat Software");
  1021. MODULE_LICENSE("GPL");
  1022. MODULE_DESCRIPTION(OSM_DESCRIPTION);
  1023. MODULE_VERSION(OSM_VERSION);
  1024. #if BITS_PER_LONG == 64
  1025. EXPORT_SYMBOL(i2o_cntxt_list_add);
  1026. EXPORT_SYMBOL(i2o_cntxt_list_get);
  1027. EXPORT_SYMBOL(i2o_cntxt_list_remove);
  1028. EXPORT_SYMBOL(i2o_cntxt_list_get_ptr);
  1029. #endif
  1030. EXPORT_SYMBOL(i2o_msg_get_wait);
  1031. EXPORT_SYMBOL(i2o_msg_nop);
  1032. EXPORT_SYMBOL(i2o_find_iop);
  1033. EXPORT_SYMBOL(i2o_iop_find_device);
  1034. EXPORT_SYMBOL(i2o_event_register);
  1035. EXPORT_SYMBOL(i2o_status_get);
  1036. EXPORT_SYMBOL(i2o_controllers);