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