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 "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. * @wait: how long to wait until timeout
  47. *
  48. * This function waits up to wait seconds for a message slot to be
  49. * available.
  50. *
  51. * On a success the message is returned and the pointer to the message is
  52. * set in msg. The returned message is the physical page frame offset
  53. * address from the read port (see the i2o spec). If no message is
  54. * available returns I2O_QUEUE_EMPTY and msg is leaved untouched.
  55. */
  56. struct i2o_message *i2o_msg_get_wait(struct i2o_controller *c, int wait)
  57. {
  58. unsigned long timeout = jiffies + wait * HZ;
  59. struct i2o_message *msg;
  60. while (IS_ERR(msg = i2o_msg_get(c))) {
  61. if (time_after(jiffies, timeout)) {
  62. osm_debug("%s: Timeout waiting for message frame.\n",
  63. c->name);
  64. return ERR_PTR(-ETIMEDOUT);
  65. }
  66. schedule_timeout_uninterruptible(1);
  67. }
  68. return msg;
  69. };
  70. #if BITS_PER_LONG == 64
  71. /**
  72. * i2o_cntxt_list_add - Append a pointer to context list and return a id
  73. * @c: controller to which the context list belong
  74. * @ptr: pointer to add to the context list
  75. *
  76. * Because the context field in I2O is only 32-bit large, on 64-bit the
  77. * pointer is to large to fit in the context field. The i2o_cntxt_list
  78. * functions therefore map pointers to context fields.
  79. *
  80. * Returns context id > 0 on success or 0 on failure.
  81. */
  82. u32 i2o_cntxt_list_add(struct i2o_controller * c, void *ptr)
  83. {
  84. struct i2o_context_list_element *entry;
  85. unsigned long flags;
  86. if (!ptr)
  87. osm_err("%s: couldn't add NULL pointer to context list!\n",
  88. c->name);
  89. entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
  90. if (!entry) {
  91. osm_err("%s: Could not allocate memory for context list element"
  92. "\n", c->name);
  93. return 0;
  94. }
  95. entry->ptr = ptr;
  96. entry->timestamp = jiffies;
  97. INIT_LIST_HEAD(&entry->list);
  98. spin_lock_irqsave(&c->context_list_lock, flags);
  99. if (unlikely(atomic_inc_and_test(&c->context_list_counter)))
  100. atomic_inc(&c->context_list_counter);
  101. entry->context = atomic_read(&c->context_list_counter);
  102. list_add(&entry->list, &c->context_list);
  103. spin_unlock_irqrestore(&c->context_list_lock, flags);
  104. osm_debug("%s: Add context to list %p -> %d\n", c->name, ptr, context);
  105. return entry->context;
  106. };
  107. /**
  108. * i2o_cntxt_list_remove - Remove a pointer from the context list
  109. * @c: controller to which the context list belong
  110. * @ptr: pointer which should be removed from the context list
  111. *
  112. * Removes a previously added pointer from the context list and returns
  113. * the matching context id.
  114. *
  115. * Returns context id on succes or 0 on failure.
  116. */
  117. u32 i2o_cntxt_list_remove(struct i2o_controller * c, void *ptr)
  118. {
  119. struct i2o_context_list_element *entry;
  120. u32 context = 0;
  121. unsigned long flags;
  122. spin_lock_irqsave(&c->context_list_lock, flags);
  123. list_for_each_entry(entry, &c->context_list, list)
  124. if (entry->ptr == ptr) {
  125. list_del(&entry->list);
  126. context = entry->context;
  127. kfree(entry);
  128. break;
  129. }
  130. spin_unlock_irqrestore(&c->context_list_lock, flags);
  131. if (!context)
  132. osm_warn("%s: Could not remove nonexistent ptr %p\n", c->name,
  133. ptr);
  134. osm_debug("%s: remove ptr from context list %d -> %p\n", c->name,
  135. context, ptr);
  136. return context;
  137. };
  138. /**
  139. * i2o_cntxt_list_get - Get a pointer from the context list and remove it
  140. * @c: controller to which the context list belong
  141. * @context: context id to which the pointer belong
  142. *
  143. * Returns pointer to the matching context id on success or NULL on
  144. * failure.
  145. */
  146. void *i2o_cntxt_list_get(struct i2o_controller *c, u32 context)
  147. {
  148. struct i2o_context_list_element *entry;
  149. unsigned long flags;
  150. void *ptr = NULL;
  151. spin_lock_irqsave(&c->context_list_lock, flags);
  152. list_for_each_entry(entry, &c->context_list, list)
  153. if (entry->context == context) {
  154. list_del(&entry->list);
  155. ptr = entry->ptr;
  156. kfree(entry);
  157. break;
  158. }
  159. spin_unlock_irqrestore(&c->context_list_lock, flags);
  160. if (!ptr)
  161. osm_warn("%s: context id %d not found\n", c->name, context);
  162. osm_debug("%s: get ptr from context list %d -> %p\n", c->name, context,
  163. ptr);
  164. return ptr;
  165. };
  166. /**
  167. * i2o_cntxt_list_get_ptr - Get a context id from the context list
  168. * @c: controller to which the context list belong
  169. * @ptr: pointer to which the context id should be fetched
  170. *
  171. * Returns context id which matches to the pointer on succes or 0 on
  172. * failure.
  173. */
  174. u32 i2o_cntxt_list_get_ptr(struct i2o_controller * c, void *ptr)
  175. {
  176. struct i2o_context_list_element *entry;
  177. u32 context = 0;
  178. unsigned long flags;
  179. spin_lock_irqsave(&c->context_list_lock, flags);
  180. list_for_each_entry(entry, &c->context_list, list)
  181. if (entry->ptr == ptr) {
  182. context = entry->context;
  183. break;
  184. }
  185. spin_unlock_irqrestore(&c->context_list_lock, flags);
  186. if (!context)
  187. osm_warn("%s: Could not find nonexistent ptr %p\n", c->name,
  188. ptr);
  189. osm_debug("%s: get context id from context list %p -> %d\n", c->name,
  190. ptr, context);
  191. return context;
  192. };
  193. #endif
  194. /**
  195. * i2o_iop_find - Find an I2O controller by id
  196. * @unit: unit number of the I2O controller to search for
  197. *
  198. * Lookup the I2O controller on the controller list.
  199. *
  200. * Returns pointer to the I2O controller on success or NULL if not found.
  201. */
  202. struct i2o_controller *i2o_find_iop(int unit)
  203. {
  204. struct i2o_controller *c;
  205. list_for_each_entry(c, &i2o_controllers, list) {
  206. if (c->unit == unit)
  207. return c;
  208. }
  209. return NULL;
  210. };
  211. /**
  212. * i2o_iop_find_device - Find a I2O device on an I2O controller
  213. * @c: I2O controller where the I2O device hangs on
  214. * @tid: TID of the I2O device to search for
  215. *
  216. * Searches the devices of the I2O controller for a device with TID tid and
  217. * returns it.
  218. *
  219. * Returns a pointer to the I2O device if found, otherwise NULL.
  220. */
  221. struct i2o_device *i2o_iop_find_device(struct i2o_controller *c, u16 tid)
  222. {
  223. struct i2o_device *dev;
  224. list_for_each_entry(dev, &c->devices, list)
  225. if (dev->lct_data.tid == tid)
  226. return dev;
  227. return NULL;
  228. };
  229. /**
  230. * i2o_quiesce_controller - quiesce controller
  231. * @c: controller
  232. *
  233. * Quiesce an IOP. Causes IOP to make external operation quiescent
  234. * (i2o 'READY' state). Internal operation of the IOP continues normally.
  235. *
  236. * Returns 0 on success or negative error code on failure.
  237. */
  238. static int i2o_iop_quiesce(struct i2o_controller *c)
  239. {
  240. struct i2o_message *msg;
  241. i2o_status_block *sb = c->status_block.virt;
  242. int rc;
  243. i2o_status_get(c);
  244. /* SysQuiesce discarded if IOP not in READY or OPERATIONAL state */
  245. if ((sb->iop_state != ADAPTER_STATE_READY) &&
  246. (sb->iop_state != ADAPTER_STATE_OPERATIONAL))
  247. return 0;
  248. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  249. if (IS_ERR(msg))
  250. return PTR_ERR(msg);
  251. msg->u.head[0] = cpu_to_le32(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0);
  252. msg->u.head[1] =
  253. cpu_to_le32(I2O_CMD_SYS_QUIESCE << 24 | HOST_TID << 12 |
  254. ADAPTER_TID);
  255. /* Long timeout needed for quiesce if lots of devices */
  256. if ((rc = i2o_msg_post_wait(c, msg, 240)))
  257. osm_info("%s: Unable to quiesce (status=%#x).\n", c->name, -rc);
  258. else
  259. osm_debug("%s: Quiesced.\n", c->name);
  260. i2o_status_get(c); // Entered READY state
  261. return rc;
  262. };
  263. /**
  264. * i2o_iop_enable - move controller from ready to OPERATIONAL
  265. * @c: I2O controller
  266. *
  267. * Enable IOP. This allows the IOP to resume external operations and
  268. * reverses the effect of a quiesce. Returns zero or an error code if
  269. * an error occurs.
  270. */
  271. static int i2o_iop_enable(struct i2o_controller *c)
  272. {
  273. struct i2o_message *msg;
  274. i2o_status_block *sb = c->status_block.virt;
  275. int rc;
  276. i2o_status_get(c);
  277. /* Enable only allowed on READY state */
  278. if (sb->iop_state != ADAPTER_STATE_READY)
  279. return -EINVAL;
  280. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  281. if (IS_ERR(msg))
  282. return PTR_ERR(msg);
  283. msg->u.head[0] = cpu_to_le32(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0);
  284. msg->u.head[1] =
  285. cpu_to_le32(I2O_CMD_SYS_ENABLE << 24 | HOST_TID << 12 |
  286. ADAPTER_TID);
  287. /* How long of a timeout do we need? */
  288. if ((rc = i2o_msg_post_wait(c, msg, 240)))
  289. osm_err("%s: Could not enable (status=%#x).\n", c->name, -rc);
  290. else
  291. osm_debug("%s: Enabled.\n", c->name);
  292. i2o_status_get(c); // entered OPERATIONAL state
  293. return rc;
  294. };
  295. /**
  296. * i2o_iop_quiesce_all - Quiesce all I2O controllers on the system
  297. *
  298. * Quiesce all I2O controllers which are connected to the system.
  299. */
  300. static inline void i2o_iop_quiesce_all(void)
  301. {
  302. struct i2o_controller *c, *tmp;
  303. list_for_each_entry_safe(c, tmp, &i2o_controllers, list) {
  304. if (!c->no_quiesce)
  305. i2o_iop_quiesce(c);
  306. }
  307. };
  308. /**
  309. * i2o_iop_enable_all - Enables all controllers on the system
  310. *
  311. * Enables all I2O controllers which are connected to the system.
  312. */
  313. static inline void i2o_iop_enable_all(void)
  314. {
  315. struct i2o_controller *c, *tmp;
  316. list_for_each_entry_safe(c, tmp, &i2o_controllers, list)
  317. i2o_iop_enable(c);
  318. };
  319. /**
  320. * i2o_clear_controller - Bring I2O controller into HOLD state
  321. * @c: controller
  322. *
  323. * Clear an IOP to HOLD state, ie. terminate external operations, clear all
  324. * input queues and prepare for a system restart. IOP's internal operation
  325. * continues normally and the outbound queue is alive. The IOP is not
  326. * expected to rebuild its LCT.
  327. *
  328. * Returns 0 on success or negative error code on failure.
  329. */
  330. static int i2o_iop_clear(struct i2o_controller *c)
  331. {
  332. struct i2o_message *msg;
  333. int rc;
  334. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  335. if (IS_ERR(msg))
  336. return PTR_ERR(msg);
  337. /* Quiesce all IOPs first */
  338. i2o_iop_quiesce_all();
  339. msg->u.head[0] = cpu_to_le32(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0);
  340. msg->u.head[1] =
  341. cpu_to_le32(I2O_CMD_ADAPTER_CLEAR << 24 | HOST_TID << 12 |
  342. ADAPTER_TID);
  343. if ((rc = i2o_msg_post_wait(c, msg, 30)))
  344. osm_info("%s: Unable to clear (status=%#x).\n", c->name, -rc);
  345. else
  346. osm_debug("%s: Cleared.\n", c->name);
  347. /* Enable all IOPs */
  348. i2o_iop_enable_all();
  349. return rc;
  350. }
  351. /**
  352. * i2o_iop_init_outbound_queue - setup the outbound message queue
  353. * @c: I2O controller
  354. *
  355. * Clear and (re)initialize IOP's outbound queue and post the message
  356. * frames to the IOP.
  357. *
  358. * Returns 0 on success or negative error code on failure.
  359. */
  360. static int i2o_iop_init_outbound_queue(struct i2o_controller *c)
  361. {
  362. u32 m;
  363. volatile u8 *status = c->status.virt;
  364. struct i2o_message *msg;
  365. ulong timeout;
  366. int i;
  367. osm_debug("%s: Initializing Outbound Queue...\n", c->name);
  368. memset(c->status.virt, 0, 4);
  369. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  370. if (IS_ERR(msg))
  371. return PTR_ERR(msg);
  372. msg->u.head[0] = cpu_to_le32(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_6);
  373. msg->u.head[1] =
  374. cpu_to_le32(I2O_CMD_OUTBOUND_INIT << 24 | HOST_TID << 12 |
  375. ADAPTER_TID);
  376. msg->u.s.icntxt = cpu_to_le32(i2o_exec_driver.context);
  377. msg->u.s.tcntxt = cpu_to_le32(0x00000000);
  378. msg->body[0] = cpu_to_le32(PAGE_SIZE);
  379. /* Outbound msg frame size in words and Initcode */
  380. msg->body[1] = cpu_to_le32(I2O_OUTBOUND_MSG_FRAME_SIZE << 16 | 0x80);
  381. msg->body[2] = cpu_to_le32(0xd0000004);
  382. msg->body[3] = cpu_to_le32(i2o_dma_low(c->status.phys));
  383. msg->body[4] = cpu_to_le32(i2o_dma_high(c->status.phys));
  384. i2o_msg_post(c, msg);
  385. timeout = jiffies + I2O_TIMEOUT_INIT_OUTBOUND_QUEUE * HZ;
  386. while (*status <= I2O_CMD_IN_PROGRESS) {
  387. if (time_after(jiffies, timeout)) {
  388. osm_warn("%s: Timeout Initializing\n", c->name);
  389. return -ETIMEDOUT;
  390. }
  391. schedule_timeout_uninterruptible(1);
  392. }
  393. m = c->out_queue.phys;
  394. /* Post frames */
  395. for (i = 0; i < I2O_MAX_OUTBOUND_MSG_FRAMES; i++) {
  396. i2o_flush_reply(c, m);
  397. udelay(1); /* Promise */
  398. m += I2O_OUTBOUND_MSG_FRAME_SIZE * sizeof(u32);
  399. }
  400. return 0;
  401. }
  402. /**
  403. * i2o_iop_reset - reset an I2O controller
  404. * @c: controller to reset
  405. *
  406. * Reset the IOP into INIT state and wait until IOP gets into RESET state.
  407. * Terminate all external operations, clear IOP's inbound and outbound
  408. * queues, terminate all DDMs, and reload the IOP's operating environment
  409. * and all local DDMs. The IOP rebuilds its LCT.
  410. */
  411. static int i2o_iop_reset(struct i2o_controller *c)
  412. {
  413. volatile u8 *status = c->status.virt;
  414. struct i2o_message *msg;
  415. unsigned long timeout;
  416. i2o_status_block *sb = c->status_block.virt;
  417. int rc = 0;
  418. osm_debug("%s: Resetting controller\n", c->name);
  419. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  420. if (IS_ERR(msg))
  421. return PTR_ERR(msg);
  422. memset(c->status_block.virt, 0, 8);
  423. /* Quiesce all IOPs first */
  424. i2o_iop_quiesce_all();
  425. msg->u.head[0] = cpu_to_le32(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_0);
  426. msg->u.head[1] =
  427. cpu_to_le32(I2O_CMD_ADAPTER_RESET << 24 | HOST_TID << 12 |
  428. ADAPTER_TID);
  429. msg->u.s.icntxt = cpu_to_le32(i2o_exec_driver.context);
  430. msg->u.s.tcntxt = cpu_to_le32(0x00000000);
  431. msg->body[0] = cpu_to_le32(0x00000000);
  432. msg->body[1] = cpu_to_le32(0x00000000);
  433. msg->body[2] = cpu_to_le32(i2o_dma_low(c->status.phys));
  434. msg->body[3] = cpu_to_le32(i2o_dma_high(c->status.phys));
  435. i2o_msg_post(c, msg);
  436. /* Wait for a reply */
  437. timeout = jiffies + I2O_TIMEOUT_RESET * HZ;
  438. while (!*status) {
  439. if (time_after(jiffies, timeout))
  440. break;
  441. schedule_timeout_uninterruptible(1);
  442. }
  443. switch (*status) {
  444. case I2O_CMD_REJECTED:
  445. osm_warn("%s: IOP reset rejected\n", c->name);
  446. rc = -EPERM;
  447. break;
  448. case I2O_CMD_IN_PROGRESS:
  449. /*
  450. * Once the reset is sent, the IOP goes into the INIT state
  451. * which is indeterminate. We need to wait until the IOP has
  452. * rebooted before we can let the system talk to it. We read
  453. * the inbound Free_List until a message is available. If we
  454. * can't read one in the given ammount of time, we assume the
  455. * IOP could not reboot properly.
  456. */
  457. osm_debug("%s: Reset in progress, waiting for reboot...\n",
  458. c->name);
  459. while (IS_ERR(msg = i2o_msg_get_wait(c, I2O_TIMEOUT_RESET))) {
  460. if (time_after(jiffies, timeout)) {
  461. osm_err("%s: IOP reset timeout.\n", c->name);
  462. rc = PTR_ERR(msg);
  463. goto exit;
  464. }
  465. schedule_timeout_uninterruptible(1);
  466. }
  467. i2o_msg_nop(c, msg);
  468. /* from here all quiesce commands are safe */
  469. c->no_quiesce = 0;
  470. /* verify if controller is in state RESET */
  471. i2o_status_get(c);
  472. if (!c->promise && (sb->iop_state != ADAPTER_STATE_RESET))
  473. osm_warn("%s: reset completed, but adapter not in RESET"
  474. " state.\n", c->name);
  475. else
  476. osm_debug("%s: reset completed.\n", c->name);
  477. break;
  478. default:
  479. osm_err("%s: IOP reset timeout.\n", c->name);
  480. rc = -ETIMEDOUT;
  481. break;
  482. }
  483. exit:
  484. /* Enable all IOPs */
  485. i2o_iop_enable_all();
  486. return rc;
  487. };
  488. /**
  489. * i2o_iop_activate - Bring controller up to HOLD
  490. * @c: controller
  491. *
  492. * This function brings an I2O controller into HOLD state. The adapter
  493. * is reset if necessary and then the queues and resource table are read.
  494. *
  495. * Returns 0 on success or negative error code on failure.
  496. */
  497. static int i2o_iop_activate(struct i2o_controller *c)
  498. {
  499. i2o_status_block *sb = c->status_block.virt;
  500. int rc;
  501. int state;
  502. /* In INIT state, Wait Inbound Q to initialize (in i2o_status_get) */
  503. /* In READY state, Get status */
  504. rc = i2o_status_get(c);
  505. if (rc) {
  506. osm_info("%s: Unable to obtain status, attempting a reset.\n",
  507. c->name);
  508. rc = i2o_iop_reset(c);
  509. if (rc)
  510. return rc;
  511. }
  512. if (sb->i2o_version > I2OVER15) {
  513. osm_err("%s: Not running version 1.5 of the I2O Specification."
  514. "\n", c->name);
  515. return -ENODEV;
  516. }
  517. switch (sb->iop_state) {
  518. case ADAPTER_STATE_FAULTED:
  519. osm_err("%s: hardware fault\n", c->name);
  520. return -EFAULT;
  521. case ADAPTER_STATE_READY:
  522. case ADAPTER_STATE_OPERATIONAL:
  523. case ADAPTER_STATE_HOLD:
  524. case ADAPTER_STATE_FAILED:
  525. osm_debug("%s: already running, trying to reset...\n", c->name);
  526. rc = i2o_iop_reset(c);
  527. if (rc)
  528. return rc;
  529. }
  530. /* preserve state */
  531. state = sb->iop_state;
  532. rc = i2o_iop_init_outbound_queue(c);
  533. if (rc)
  534. return rc;
  535. /* if adapter was not in RESET state clear now */
  536. if (state != ADAPTER_STATE_RESET)
  537. i2o_iop_clear(c);
  538. i2o_status_get(c);
  539. if (sb->iop_state != ADAPTER_STATE_HOLD) {
  540. osm_err("%s: failed to bring IOP into HOLD state\n", c->name);
  541. return -EIO;
  542. }
  543. return i2o_hrt_get(c);
  544. };
  545. /**
  546. * i2o_iop_systab_set - Set the I2O System Table of the specified IOP
  547. * @c: I2O controller to which the system table should be send
  548. *
  549. * Before the systab could be set i2o_systab_build() must be called.
  550. *
  551. * Returns 0 on success or negative error code on failure.
  552. */
  553. static int i2o_iop_systab_set(struct i2o_controller *c)
  554. {
  555. struct i2o_message *msg;
  556. i2o_status_block *sb = c->status_block.virt;
  557. struct device *dev = &c->pdev->dev;
  558. struct resource *root;
  559. int rc;
  560. if (sb->current_mem_size < sb->desired_mem_size) {
  561. struct resource *res = &c->mem_resource;
  562. res->name = c->pdev->bus->name;
  563. res->flags = IORESOURCE_MEM;
  564. res->start = 0;
  565. res->end = 0;
  566. osm_info("%s: requires private memory resources.\n", c->name);
  567. root = pci_find_parent_resource(c->pdev, res);
  568. if (root == NULL)
  569. osm_warn("%s: Can't find parent resource!\n", c->name);
  570. 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 */
  571. NULL, NULL) >= 0) {
  572. c->mem_alloc = 1;
  573. sb->current_mem_size = 1 + res->end - res->start;
  574. sb->current_mem_base = res->start;
  575. osm_info("%s: allocated %llu bytes of PCI memory at "
  576. "0x%016llX.\n", c->name,
  577. (unsigned long long)(1 + res->end - res->start),
  578. (unsigned long long)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 %llu bytes of PCI I/O at "
  597. "0x%016llX.\n", c->name,
  598. (unsigned long long)(1 + res->end - res->start),
  599. (unsigned long long)res->start);
  600. }
  601. }
  602. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  603. if (IS_ERR(msg))
  604. return PTR_ERR(msg);
  605. i2o_systab.phys = dma_map_single(dev, i2o_systab.virt, i2o_systab.len,
  606. PCI_DMA_TODEVICE);
  607. if (!i2o_systab.phys) {
  608. i2o_msg_nop(c, msg);
  609. return -ENOMEM;
  610. }
  611. msg->u.head[0] = cpu_to_le32(I2O_MESSAGE_SIZE(12) | SGL_OFFSET_6);
  612. msg->u.head[1] =
  613. cpu_to_le32(I2O_CMD_SYS_TAB_SET << 24 | HOST_TID << 12 |
  614. ADAPTER_TID);
  615. /*
  616. * Provide three SGL-elements:
  617. * System table (SysTab), Private memory space declaration and
  618. * Private i/o space declaration
  619. */
  620. msg->body[0] = cpu_to_le32(c->unit + 2);
  621. msg->body[1] = cpu_to_le32(0x00000000);
  622. msg->body[2] = cpu_to_le32(0x54000000 | i2o_systab.len);
  623. msg->body[3] = cpu_to_le32(i2o_systab.phys);
  624. msg->body[4] = cpu_to_le32(0x54000000 | sb->current_mem_size);
  625. msg->body[5] = cpu_to_le32(sb->current_mem_base);
  626. msg->body[6] = cpu_to_le32(0xd4000000 | sb->current_io_size);
  627. msg->body[6] = cpu_to_le32(sb->current_io_base);
  628. rc = i2o_msg_post_wait(c, msg, 120);
  629. dma_unmap_single(dev, i2o_systab.phys, i2o_systab.len,
  630. PCI_DMA_TODEVICE);
  631. if (rc < 0)
  632. osm_err("%s: Unable to set SysTab (status=%#x).\n", c->name,
  633. -rc);
  634. else
  635. osm_debug("%s: SysTab set.\n", c->name);
  636. return rc;
  637. }
  638. /**
  639. * i2o_iop_online - Bring a controller online into OPERATIONAL state.
  640. * @c: I2O controller
  641. *
  642. * Send the system table and enable the I2O controller.
  643. *
  644. * Returns 0 on success or negative error code on failure.
  645. */
  646. static int i2o_iop_online(struct i2o_controller *c)
  647. {
  648. int rc;
  649. rc = i2o_iop_systab_set(c);
  650. if (rc)
  651. return rc;
  652. /* In READY state */
  653. osm_debug("%s: Attempting to enable...\n", c->name);
  654. rc = i2o_iop_enable(c);
  655. if (rc)
  656. return rc;
  657. return 0;
  658. };
  659. /**
  660. * i2o_iop_remove - Remove the I2O controller from the I2O core
  661. * @c: I2O controller
  662. *
  663. * Remove the I2O controller from the I2O core. If devices are attached to
  664. * the controller remove these also and finally reset the controller.
  665. */
  666. void i2o_iop_remove(struct i2o_controller *c)
  667. {
  668. struct i2o_device *dev, *tmp;
  669. osm_debug("%s: deleting controller\n", c->name);
  670. i2o_driver_notify_controller_remove_all(c);
  671. list_del(&c->list);
  672. list_for_each_entry_safe(dev, tmp, &c->devices, list)
  673. i2o_device_remove(dev);
  674. device_del(&c->device);
  675. /* Ask the IOP to switch to RESET state */
  676. i2o_iop_reset(c);
  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 success 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))
  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 + sizeof(u32),
  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. mutex_init(&c->lct_lock);
  899. device_initialize(&c->device);
  900. c->device.release = &i2o_iop_release;
  901. dev_set_name(&c->device, "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_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);