device.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045
  1. /*
  2. * drivers/s390/cio/device.c
  3. * bus driver for ccw devices
  4. *
  5. * Copyright IBM Corp. 2002,2008
  6. * Author(s): Arnd Bergmann (arndb@de.ibm.com)
  7. * Cornelia Huck (cornelia.huck@de.ibm.com)
  8. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  9. */
  10. #define KMSG_COMPONENT "cio"
  11. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/errno.h>
  16. #include <linux/err.h>
  17. #include <linux/slab.h>
  18. #include <linux/list.h>
  19. #include <linux/device.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/timer.h>
  22. #include <asm/ccwdev.h>
  23. #include <asm/cio.h>
  24. #include <asm/param.h> /* HZ */
  25. #include <asm/cmb.h>
  26. #include <asm/isc.h>
  27. #include "chp.h"
  28. #include "cio.h"
  29. #include "cio_debug.h"
  30. #include "css.h"
  31. #include "device.h"
  32. #include "ioasm.h"
  33. #include "io_sch.h"
  34. #include "blacklist.h"
  35. static struct timer_list recovery_timer;
  36. static DEFINE_SPINLOCK(recovery_lock);
  37. static int recovery_phase;
  38. static const unsigned long recovery_delay[] = { 3, 30, 300 };
  39. /******************* bus type handling ***********************/
  40. /* The Linux driver model distinguishes between a bus type and
  41. * the bus itself. Of course we only have one channel
  42. * subsystem driver and one channel system per machine, but
  43. * we still use the abstraction. T.R. says it's a good idea. */
  44. static int
  45. ccw_bus_match (struct device * dev, struct device_driver * drv)
  46. {
  47. struct ccw_device *cdev = to_ccwdev(dev);
  48. struct ccw_driver *cdrv = to_ccwdrv(drv);
  49. const struct ccw_device_id *ids = cdrv->ids, *found;
  50. if (!ids)
  51. return 0;
  52. found = ccw_device_id_match(ids, &cdev->id);
  53. if (!found)
  54. return 0;
  55. cdev->id.driver_info = found->driver_info;
  56. return 1;
  57. }
  58. /* Store modalias string delimited by prefix/suffix string into buffer with
  59. * specified size. Return length of resulting string (excluding trailing '\0')
  60. * even if string doesn't fit buffer (snprintf semantics). */
  61. static int snprint_alias(char *buf, size_t size,
  62. struct ccw_device_id *id, const char *suffix)
  63. {
  64. int len;
  65. len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
  66. if (len > size)
  67. return len;
  68. buf += len;
  69. size -= len;
  70. if (id->dev_type != 0)
  71. len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
  72. id->dev_model, suffix);
  73. else
  74. len += snprintf(buf, size, "dtdm%s", suffix);
  75. return len;
  76. }
  77. /* Set up environment variables for ccw device uevent. Return 0 on success,
  78. * non-zero otherwise. */
  79. static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
  80. {
  81. struct ccw_device *cdev = to_ccwdev(dev);
  82. struct ccw_device_id *id = &(cdev->id);
  83. int ret;
  84. char modalias_buf[30];
  85. /* CU_TYPE= */
  86. ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
  87. if (ret)
  88. return ret;
  89. /* CU_MODEL= */
  90. ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
  91. if (ret)
  92. return ret;
  93. /* The next two can be zero, that's ok for us */
  94. /* DEV_TYPE= */
  95. ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
  96. if (ret)
  97. return ret;
  98. /* DEV_MODEL= */
  99. ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
  100. if (ret)
  101. return ret;
  102. /* MODALIAS= */
  103. snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
  104. ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
  105. return ret;
  106. }
  107. struct bus_type ccw_bus_type;
  108. static void io_subchannel_irq(struct subchannel *);
  109. static int io_subchannel_probe(struct subchannel *);
  110. static int io_subchannel_remove(struct subchannel *);
  111. static void io_subchannel_shutdown(struct subchannel *);
  112. static int io_subchannel_sch_event(struct subchannel *, int);
  113. static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
  114. int);
  115. static void recovery_func(unsigned long data);
  116. wait_queue_head_t ccw_device_init_wq;
  117. atomic_t ccw_device_init_count;
  118. static struct css_device_id io_subchannel_ids[] = {
  119. { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
  120. { /* end of list */ },
  121. };
  122. MODULE_DEVICE_TABLE(css, io_subchannel_ids);
  123. static int io_subchannel_prepare(struct subchannel *sch)
  124. {
  125. struct ccw_device *cdev;
  126. /*
  127. * Don't allow suspend while a ccw device registration
  128. * is still outstanding.
  129. */
  130. cdev = sch_get_cdev(sch);
  131. if (cdev && !device_is_registered(&cdev->dev))
  132. return -EAGAIN;
  133. return 0;
  134. }
  135. static int io_subchannel_settle(void)
  136. {
  137. int ret;
  138. ret = wait_event_interruptible(ccw_device_init_wq,
  139. atomic_read(&ccw_device_init_count) == 0);
  140. if (ret)
  141. return -EINTR;
  142. flush_workqueue(cio_work_q);
  143. return 0;
  144. }
  145. static struct css_driver io_subchannel_driver = {
  146. .owner = THIS_MODULE,
  147. .subchannel_type = io_subchannel_ids,
  148. .name = "io_subchannel",
  149. .irq = io_subchannel_irq,
  150. .sch_event = io_subchannel_sch_event,
  151. .chp_event = io_subchannel_chp_event,
  152. .probe = io_subchannel_probe,
  153. .remove = io_subchannel_remove,
  154. .shutdown = io_subchannel_shutdown,
  155. .prepare = io_subchannel_prepare,
  156. .settle = io_subchannel_settle,
  157. };
  158. int __init io_subchannel_init(void)
  159. {
  160. int ret;
  161. init_waitqueue_head(&ccw_device_init_wq);
  162. atomic_set(&ccw_device_init_count, 0);
  163. setup_timer(&recovery_timer, recovery_func, 0);
  164. ret = bus_register(&ccw_bus_type);
  165. if (ret)
  166. return ret;
  167. ret = css_driver_register(&io_subchannel_driver);
  168. if (ret)
  169. bus_unregister(&ccw_bus_type);
  170. return ret;
  171. }
  172. /************************ device handling **************************/
  173. /*
  174. * A ccw_device has some interfaces in sysfs in addition to the
  175. * standard ones.
  176. * The following entries are designed to export the information which
  177. * resided in 2.4 in /proc/subchannels. Subchannel and device number
  178. * are obvious, so they don't have an entry :)
  179. * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
  180. */
  181. static ssize_t
  182. chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
  183. {
  184. struct subchannel *sch = to_subchannel(dev);
  185. struct chsc_ssd_info *ssd = &sch->ssd_info;
  186. ssize_t ret = 0;
  187. int chp;
  188. int mask;
  189. for (chp = 0; chp < 8; chp++) {
  190. mask = 0x80 >> chp;
  191. if (ssd->path_mask & mask)
  192. ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
  193. else
  194. ret += sprintf(buf + ret, "00 ");
  195. }
  196. ret += sprintf (buf+ret, "\n");
  197. return min((ssize_t)PAGE_SIZE, ret);
  198. }
  199. static ssize_t
  200. pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
  201. {
  202. struct subchannel *sch = to_subchannel(dev);
  203. struct pmcw *pmcw = &sch->schib.pmcw;
  204. return sprintf (buf, "%02x %02x %02x\n",
  205. pmcw->pim, pmcw->pam, pmcw->pom);
  206. }
  207. static ssize_t
  208. devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
  209. {
  210. struct ccw_device *cdev = to_ccwdev(dev);
  211. struct ccw_device_id *id = &(cdev->id);
  212. if (id->dev_type != 0)
  213. return sprintf(buf, "%04x/%02x\n",
  214. id->dev_type, id->dev_model);
  215. else
  216. return sprintf(buf, "n/a\n");
  217. }
  218. static ssize_t
  219. cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
  220. {
  221. struct ccw_device *cdev = to_ccwdev(dev);
  222. struct ccw_device_id *id = &(cdev->id);
  223. return sprintf(buf, "%04x/%02x\n",
  224. id->cu_type, id->cu_model);
  225. }
  226. static ssize_t
  227. modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
  228. {
  229. struct ccw_device *cdev = to_ccwdev(dev);
  230. struct ccw_device_id *id = &(cdev->id);
  231. int len;
  232. len = snprint_alias(buf, PAGE_SIZE, id, "\n");
  233. return len > PAGE_SIZE ? PAGE_SIZE : len;
  234. }
  235. static ssize_t
  236. online_show (struct device *dev, struct device_attribute *attr, char *buf)
  237. {
  238. struct ccw_device *cdev = to_ccwdev(dev);
  239. return sprintf(buf, cdev->online ? "1\n" : "0\n");
  240. }
  241. int ccw_device_is_orphan(struct ccw_device *cdev)
  242. {
  243. return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
  244. }
  245. static void ccw_device_unregister(struct ccw_device *cdev)
  246. {
  247. if (device_is_registered(&cdev->dev)) {
  248. /* Undo device_add(). */
  249. device_del(&cdev->dev);
  250. }
  251. if (cdev->private->flags.initialized) {
  252. cdev->private->flags.initialized = 0;
  253. /* Release reference from device_initialize(). */
  254. put_device(&cdev->dev);
  255. }
  256. }
  257. static void io_subchannel_quiesce(struct subchannel *);
  258. /**
  259. * ccw_device_set_offline() - disable a ccw device for I/O
  260. * @cdev: target ccw device
  261. *
  262. * This function calls the driver's set_offline() function for @cdev, if
  263. * given, and then disables @cdev.
  264. * Returns:
  265. * %0 on success and a negative error value on failure.
  266. * Context:
  267. * enabled, ccw device lock not held
  268. */
  269. int ccw_device_set_offline(struct ccw_device *cdev)
  270. {
  271. struct subchannel *sch;
  272. int ret, state;
  273. if (!cdev)
  274. return -ENODEV;
  275. if (!cdev->online || !cdev->drv)
  276. return -EINVAL;
  277. if (cdev->drv->set_offline) {
  278. ret = cdev->drv->set_offline(cdev);
  279. if (ret != 0)
  280. return ret;
  281. }
  282. cdev->online = 0;
  283. spin_lock_irq(cdev->ccwlock);
  284. sch = to_subchannel(cdev->dev.parent);
  285. /* Wait until a final state or DISCONNECTED is reached */
  286. while (!dev_fsm_final_state(cdev) &&
  287. cdev->private->state != DEV_STATE_DISCONNECTED) {
  288. spin_unlock_irq(cdev->ccwlock);
  289. wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
  290. cdev->private->state == DEV_STATE_DISCONNECTED));
  291. spin_lock_irq(cdev->ccwlock);
  292. }
  293. do {
  294. ret = ccw_device_offline(cdev);
  295. if (!ret)
  296. break;
  297. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device "
  298. "0.%x.%04x\n", ret, cdev->private->dev_id.ssid,
  299. cdev->private->dev_id.devno);
  300. if (ret != -EBUSY)
  301. goto error;
  302. state = cdev->private->state;
  303. spin_unlock_irq(cdev->ccwlock);
  304. io_subchannel_quiesce(sch);
  305. spin_lock_irq(cdev->ccwlock);
  306. cdev->private->state = state;
  307. } while (ret == -EBUSY);
  308. spin_unlock_irq(cdev->ccwlock);
  309. wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
  310. cdev->private->state == DEV_STATE_DISCONNECTED));
  311. /* Inform the user if set offline failed. */
  312. if (cdev->private->state == DEV_STATE_BOXED) {
  313. pr_warning("%s: The device entered boxed state while "
  314. "being set offline\n", dev_name(&cdev->dev));
  315. } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
  316. pr_warning("%s: The device stopped operating while "
  317. "being set offline\n", dev_name(&cdev->dev));
  318. }
  319. /* Give up reference from ccw_device_set_online(). */
  320. put_device(&cdev->dev);
  321. return 0;
  322. error:
  323. cdev->private->state = DEV_STATE_OFFLINE;
  324. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  325. spin_unlock_irq(cdev->ccwlock);
  326. /* Give up reference from ccw_device_set_online(). */
  327. put_device(&cdev->dev);
  328. return -ENODEV;
  329. }
  330. /**
  331. * ccw_device_set_online() - enable a ccw device for I/O
  332. * @cdev: target ccw device
  333. *
  334. * This function first enables @cdev and then calls the driver's set_online()
  335. * function for @cdev, if given. If set_online() returns an error, @cdev is
  336. * disabled again.
  337. * Returns:
  338. * %0 on success and a negative error value on failure.
  339. * Context:
  340. * enabled, ccw device lock not held
  341. */
  342. int ccw_device_set_online(struct ccw_device *cdev)
  343. {
  344. int ret;
  345. int ret2;
  346. if (!cdev)
  347. return -ENODEV;
  348. if (cdev->online || !cdev->drv)
  349. return -EINVAL;
  350. /* Hold on to an extra reference while device is online. */
  351. if (!get_device(&cdev->dev))
  352. return -ENODEV;
  353. spin_lock_irq(cdev->ccwlock);
  354. ret = ccw_device_online(cdev);
  355. spin_unlock_irq(cdev->ccwlock);
  356. if (ret == 0)
  357. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  358. else {
  359. CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
  360. "device 0.%x.%04x\n",
  361. ret, cdev->private->dev_id.ssid,
  362. cdev->private->dev_id.devno);
  363. /* Give up online reference since onlining failed. */
  364. put_device(&cdev->dev);
  365. return ret;
  366. }
  367. spin_lock_irq(cdev->ccwlock);
  368. /* Check if online processing was successful */
  369. if ((cdev->private->state != DEV_STATE_ONLINE) &&
  370. (cdev->private->state != DEV_STATE_W4SENSE)) {
  371. spin_unlock_irq(cdev->ccwlock);
  372. /* Inform the user that set online failed. */
  373. if (cdev->private->state == DEV_STATE_BOXED) {
  374. pr_warning("%s: Setting the device online failed "
  375. "because it is boxed\n",
  376. dev_name(&cdev->dev));
  377. } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
  378. pr_warning("%s: Setting the device online failed "
  379. "because it is not operational\n",
  380. dev_name(&cdev->dev));
  381. }
  382. /* Give up online reference since onlining failed. */
  383. put_device(&cdev->dev);
  384. return -ENODEV;
  385. }
  386. spin_unlock_irq(cdev->ccwlock);
  387. if (cdev->drv->set_online)
  388. ret = cdev->drv->set_online(cdev);
  389. if (ret)
  390. goto rollback;
  391. cdev->online = 1;
  392. return 0;
  393. rollback:
  394. spin_lock_irq(cdev->ccwlock);
  395. /* Wait until a final state or DISCONNECTED is reached */
  396. while (!dev_fsm_final_state(cdev) &&
  397. cdev->private->state != DEV_STATE_DISCONNECTED) {
  398. spin_unlock_irq(cdev->ccwlock);
  399. wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
  400. cdev->private->state == DEV_STATE_DISCONNECTED));
  401. spin_lock_irq(cdev->ccwlock);
  402. }
  403. ret2 = ccw_device_offline(cdev);
  404. if (ret2)
  405. goto error;
  406. spin_unlock_irq(cdev->ccwlock);
  407. wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
  408. cdev->private->state == DEV_STATE_DISCONNECTED));
  409. /* Give up online reference since onlining failed. */
  410. put_device(&cdev->dev);
  411. return ret;
  412. error:
  413. CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
  414. "device 0.%x.%04x\n",
  415. ret2, cdev->private->dev_id.ssid,
  416. cdev->private->dev_id.devno);
  417. cdev->private->state = DEV_STATE_OFFLINE;
  418. spin_unlock_irq(cdev->ccwlock);
  419. /* Give up online reference since onlining failed. */
  420. put_device(&cdev->dev);
  421. return ret;
  422. }
  423. static int online_store_handle_offline(struct ccw_device *cdev)
  424. {
  425. if (cdev->private->state == DEV_STATE_DISCONNECTED) {
  426. spin_lock_irq(cdev->ccwlock);
  427. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
  428. spin_unlock_irq(cdev->ccwlock);
  429. } else if (cdev->online && cdev->drv && cdev->drv->set_offline)
  430. return ccw_device_set_offline(cdev);
  431. return 0;
  432. }
  433. static int online_store_recog_and_online(struct ccw_device *cdev)
  434. {
  435. /* Do device recognition, if needed. */
  436. if (cdev->private->state == DEV_STATE_BOXED) {
  437. spin_lock_irq(cdev->ccwlock);
  438. ccw_device_recognition(cdev);
  439. spin_unlock_irq(cdev->ccwlock);
  440. wait_event(cdev->private->wait_q,
  441. cdev->private->flags.recog_done);
  442. if (cdev->private->state != DEV_STATE_OFFLINE)
  443. /* recognition failed */
  444. return -EAGAIN;
  445. }
  446. if (cdev->drv && cdev->drv->set_online)
  447. ccw_device_set_online(cdev);
  448. return 0;
  449. }
  450. static int online_store_handle_online(struct ccw_device *cdev, int force)
  451. {
  452. int ret;
  453. ret = online_store_recog_and_online(cdev);
  454. if (ret && !force)
  455. return ret;
  456. if (force && cdev->private->state == DEV_STATE_BOXED) {
  457. ret = ccw_device_stlck(cdev);
  458. if (ret)
  459. return ret;
  460. if (cdev->id.cu_type == 0)
  461. cdev->private->state = DEV_STATE_NOT_OPER;
  462. ret = online_store_recog_and_online(cdev);
  463. if (ret)
  464. return ret;
  465. }
  466. return 0;
  467. }
  468. static ssize_t online_store (struct device *dev, struct device_attribute *attr,
  469. const char *buf, size_t count)
  470. {
  471. struct ccw_device *cdev = to_ccwdev(dev);
  472. int force, ret;
  473. unsigned long i;
  474. if (!dev_fsm_final_state(cdev) &&
  475. cdev->private->state != DEV_STATE_DISCONNECTED)
  476. return -EAGAIN;
  477. if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
  478. return -EAGAIN;
  479. if (cdev->drv && !try_module_get(cdev->drv->owner)) {
  480. atomic_set(&cdev->private->onoff, 0);
  481. return -EINVAL;
  482. }
  483. if (!strncmp(buf, "force\n", count)) {
  484. force = 1;
  485. i = 1;
  486. ret = 0;
  487. } else {
  488. force = 0;
  489. ret = strict_strtoul(buf, 16, &i);
  490. }
  491. if (ret)
  492. goto out;
  493. switch (i) {
  494. case 0:
  495. ret = online_store_handle_offline(cdev);
  496. break;
  497. case 1:
  498. ret = online_store_handle_online(cdev, force);
  499. break;
  500. default:
  501. ret = -EINVAL;
  502. }
  503. out:
  504. if (cdev->drv)
  505. module_put(cdev->drv->owner);
  506. atomic_set(&cdev->private->onoff, 0);
  507. return (ret < 0) ? ret : count;
  508. }
  509. static ssize_t
  510. available_show (struct device *dev, struct device_attribute *attr, char *buf)
  511. {
  512. struct ccw_device *cdev = to_ccwdev(dev);
  513. struct subchannel *sch;
  514. if (ccw_device_is_orphan(cdev))
  515. return sprintf(buf, "no device\n");
  516. switch (cdev->private->state) {
  517. case DEV_STATE_BOXED:
  518. return sprintf(buf, "boxed\n");
  519. case DEV_STATE_DISCONNECTED:
  520. case DEV_STATE_DISCONNECTED_SENSE_ID:
  521. case DEV_STATE_NOT_OPER:
  522. sch = to_subchannel(dev->parent);
  523. if (!sch->lpm)
  524. return sprintf(buf, "no path\n");
  525. else
  526. return sprintf(buf, "no device\n");
  527. default:
  528. /* All other states considered fine. */
  529. return sprintf(buf, "good\n");
  530. }
  531. }
  532. static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
  533. static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
  534. static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
  535. static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
  536. static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
  537. static DEVICE_ATTR(online, 0644, online_show, online_store);
  538. static DEVICE_ATTR(availability, 0444, available_show, NULL);
  539. static struct attribute *io_subchannel_attrs[] = {
  540. &dev_attr_chpids.attr,
  541. &dev_attr_pimpampom.attr,
  542. NULL,
  543. };
  544. static struct attribute_group io_subchannel_attr_group = {
  545. .attrs = io_subchannel_attrs,
  546. };
  547. static struct attribute * ccwdev_attrs[] = {
  548. &dev_attr_devtype.attr,
  549. &dev_attr_cutype.attr,
  550. &dev_attr_modalias.attr,
  551. &dev_attr_online.attr,
  552. &dev_attr_cmb_enable.attr,
  553. &dev_attr_availability.attr,
  554. NULL,
  555. };
  556. static struct attribute_group ccwdev_attr_group = {
  557. .attrs = ccwdev_attrs,
  558. };
  559. static const struct attribute_group *ccwdev_attr_groups[] = {
  560. &ccwdev_attr_group,
  561. NULL,
  562. };
  563. /* this is a simple abstraction for device_register that sets the
  564. * correct bus type and adds the bus specific files */
  565. static int ccw_device_register(struct ccw_device *cdev)
  566. {
  567. struct device *dev = &cdev->dev;
  568. int ret;
  569. dev->bus = &ccw_bus_type;
  570. ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
  571. cdev->private->dev_id.devno);
  572. if (ret)
  573. return ret;
  574. return device_add(dev);
  575. }
  576. static int match_dev_id(struct device *dev, void *data)
  577. {
  578. struct ccw_device *cdev = to_ccwdev(dev);
  579. struct ccw_dev_id *dev_id = data;
  580. return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
  581. }
  582. static struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
  583. {
  584. struct device *dev;
  585. dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
  586. return dev ? to_ccwdev(dev) : NULL;
  587. }
  588. static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
  589. {
  590. int ret;
  591. if (device_is_registered(&cdev->dev)) {
  592. device_release_driver(&cdev->dev);
  593. ret = device_attach(&cdev->dev);
  594. WARN_ON(ret == -ENODEV);
  595. }
  596. }
  597. static void
  598. ccw_device_release(struct device *dev)
  599. {
  600. struct ccw_device *cdev;
  601. cdev = to_ccwdev(dev);
  602. /* Release reference of parent subchannel. */
  603. put_device(cdev->dev.parent);
  604. kfree(cdev->private);
  605. kfree(cdev);
  606. }
  607. static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
  608. {
  609. struct ccw_device *cdev;
  610. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  611. if (cdev) {
  612. cdev->private = kzalloc(sizeof(struct ccw_device_private),
  613. GFP_KERNEL | GFP_DMA);
  614. if (cdev->private)
  615. return cdev;
  616. }
  617. kfree(cdev);
  618. return ERR_PTR(-ENOMEM);
  619. }
  620. static void ccw_device_todo(struct work_struct *work);
  621. static int io_subchannel_initialize_dev(struct subchannel *sch,
  622. struct ccw_device *cdev)
  623. {
  624. cdev->private->cdev = cdev;
  625. atomic_set(&cdev->private->onoff, 0);
  626. cdev->dev.parent = &sch->dev;
  627. cdev->dev.release = ccw_device_release;
  628. INIT_WORK(&cdev->private->todo_work, ccw_device_todo);
  629. cdev->dev.groups = ccwdev_attr_groups;
  630. /* Do first half of device_register. */
  631. device_initialize(&cdev->dev);
  632. if (!get_device(&sch->dev)) {
  633. /* Release reference from device_initialize(). */
  634. put_device(&cdev->dev);
  635. return -ENODEV;
  636. }
  637. cdev->private->flags.initialized = 1;
  638. return 0;
  639. }
  640. static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
  641. {
  642. struct ccw_device *cdev;
  643. int ret;
  644. cdev = io_subchannel_allocate_dev(sch);
  645. if (!IS_ERR(cdev)) {
  646. ret = io_subchannel_initialize_dev(sch, cdev);
  647. if (ret)
  648. cdev = ERR_PTR(ret);
  649. }
  650. return cdev;
  651. }
  652. static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
  653. static void sch_create_and_recog_new_device(struct subchannel *sch)
  654. {
  655. struct ccw_device *cdev;
  656. /* Need to allocate a new ccw device. */
  657. cdev = io_subchannel_create_ccwdev(sch);
  658. if (IS_ERR(cdev)) {
  659. /* OK, we did everything we could... */
  660. css_sch_device_unregister(sch);
  661. return;
  662. }
  663. /* Start recognition for the new ccw device. */
  664. io_subchannel_recog(cdev, sch);
  665. }
  666. /*
  667. * Register recognized device.
  668. */
  669. static void io_subchannel_register(struct ccw_device *cdev)
  670. {
  671. struct subchannel *sch;
  672. int ret;
  673. unsigned long flags;
  674. sch = to_subchannel(cdev->dev.parent);
  675. /*
  676. * Check if subchannel is still registered. It may have become
  677. * unregistered if a machine check hit us after finishing
  678. * device recognition but before the register work could be
  679. * queued.
  680. */
  681. if (!device_is_registered(&sch->dev))
  682. goto out_err;
  683. css_update_ssd_info(sch);
  684. /*
  685. * io_subchannel_register() will also be called after device
  686. * recognition has been done for a boxed device (which will already
  687. * be registered). We need to reprobe since we may now have sense id
  688. * information.
  689. */
  690. if (device_is_registered(&cdev->dev)) {
  691. if (!cdev->drv) {
  692. ret = device_reprobe(&cdev->dev);
  693. if (ret)
  694. /* We can't do much here. */
  695. CIO_MSG_EVENT(0, "device_reprobe() returned"
  696. " %d for 0.%x.%04x\n", ret,
  697. cdev->private->dev_id.ssid,
  698. cdev->private->dev_id.devno);
  699. }
  700. goto out;
  701. }
  702. /*
  703. * Now we know this subchannel will stay, we can throw
  704. * our delayed uevent.
  705. */
  706. dev_set_uevent_suppress(&sch->dev, 0);
  707. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  708. /* make it known to the system */
  709. ret = ccw_device_register(cdev);
  710. if (ret) {
  711. CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
  712. cdev->private->dev_id.ssid,
  713. cdev->private->dev_id.devno, ret);
  714. spin_lock_irqsave(sch->lock, flags);
  715. sch_set_cdev(sch, NULL);
  716. spin_unlock_irqrestore(sch->lock, flags);
  717. /* Release initial device reference. */
  718. put_device(&cdev->dev);
  719. goto out_err;
  720. }
  721. out:
  722. cdev->private->flags.recog_done = 1;
  723. wake_up(&cdev->private->wait_q);
  724. out_err:
  725. if (atomic_dec_and_test(&ccw_device_init_count))
  726. wake_up(&ccw_device_init_wq);
  727. }
  728. static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
  729. {
  730. struct subchannel *sch;
  731. /* Get subchannel reference for local processing. */
  732. if (!get_device(cdev->dev.parent))
  733. return;
  734. sch = to_subchannel(cdev->dev.parent);
  735. css_sch_device_unregister(sch);
  736. /* Release subchannel reference for local processing. */
  737. put_device(&sch->dev);
  738. }
  739. /*
  740. * subchannel recognition done. Called from the state machine.
  741. */
  742. void
  743. io_subchannel_recog_done(struct ccw_device *cdev)
  744. {
  745. if (css_init_done == 0) {
  746. cdev->private->flags.recog_done = 1;
  747. return;
  748. }
  749. switch (cdev->private->state) {
  750. case DEV_STATE_BOXED:
  751. /* Device did not respond in time. */
  752. case DEV_STATE_NOT_OPER:
  753. cdev->private->flags.recog_done = 1;
  754. /* Remove device found not operational. */
  755. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  756. if (atomic_dec_and_test(&ccw_device_init_count))
  757. wake_up(&ccw_device_init_wq);
  758. break;
  759. case DEV_STATE_OFFLINE:
  760. /*
  761. * We can't register the device in interrupt context so
  762. * we schedule a work item.
  763. */
  764. ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
  765. break;
  766. }
  767. }
  768. static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
  769. {
  770. struct ccw_device_private *priv;
  771. cdev->ccwlock = sch->lock;
  772. /* Init private data. */
  773. priv = cdev->private;
  774. priv->dev_id.devno = sch->schib.pmcw.dev;
  775. priv->dev_id.ssid = sch->schid.ssid;
  776. priv->schid = sch->schid;
  777. priv->state = DEV_STATE_NOT_OPER;
  778. INIT_LIST_HEAD(&priv->cmb_list);
  779. init_waitqueue_head(&priv->wait_q);
  780. init_timer(&priv->timer);
  781. /* Increase counter of devices currently in recognition. */
  782. atomic_inc(&ccw_device_init_count);
  783. /* Start async. device sensing. */
  784. spin_lock_irq(sch->lock);
  785. sch_set_cdev(sch, cdev);
  786. ccw_device_recognition(cdev);
  787. spin_unlock_irq(sch->lock);
  788. }
  789. static int ccw_device_move_to_sch(struct ccw_device *cdev,
  790. struct subchannel *sch)
  791. {
  792. struct subchannel *old_sch;
  793. int rc, old_enabled = 0;
  794. old_sch = to_subchannel(cdev->dev.parent);
  795. /* Obtain child reference for new parent. */
  796. if (!get_device(&sch->dev))
  797. return -ENODEV;
  798. if (!sch_is_pseudo_sch(old_sch)) {
  799. spin_lock_irq(old_sch->lock);
  800. old_enabled = old_sch->schib.pmcw.ena;
  801. rc = 0;
  802. if (old_enabled)
  803. rc = cio_disable_subchannel(old_sch);
  804. spin_unlock_irq(old_sch->lock);
  805. if (rc == -EBUSY) {
  806. /* Release child reference for new parent. */
  807. put_device(&sch->dev);
  808. return rc;
  809. }
  810. }
  811. mutex_lock(&sch->reg_mutex);
  812. rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
  813. mutex_unlock(&sch->reg_mutex);
  814. if (rc) {
  815. CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
  816. cdev->private->dev_id.ssid,
  817. cdev->private->dev_id.devno, sch->schid.ssid,
  818. sch->schib.pmcw.dev, rc);
  819. if (old_enabled) {
  820. /* Try to reenable the old subchannel. */
  821. spin_lock_irq(old_sch->lock);
  822. cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
  823. spin_unlock_irq(old_sch->lock);
  824. }
  825. /* Release child reference for new parent. */
  826. put_device(&sch->dev);
  827. return rc;
  828. }
  829. /* Clean up old subchannel. */
  830. if (!sch_is_pseudo_sch(old_sch)) {
  831. spin_lock_irq(old_sch->lock);
  832. sch_set_cdev(old_sch, NULL);
  833. spin_unlock_irq(old_sch->lock);
  834. css_schedule_eval(old_sch->schid);
  835. }
  836. /* Release child reference for old parent. */
  837. put_device(&old_sch->dev);
  838. /* Initialize new subchannel. */
  839. spin_lock_irq(sch->lock);
  840. cdev->private->schid = sch->schid;
  841. cdev->ccwlock = sch->lock;
  842. if (!sch_is_pseudo_sch(sch))
  843. sch_set_cdev(sch, cdev);
  844. spin_unlock_irq(sch->lock);
  845. if (!sch_is_pseudo_sch(sch))
  846. css_update_ssd_info(sch);
  847. return 0;
  848. }
  849. static int ccw_device_move_to_orph(struct ccw_device *cdev)
  850. {
  851. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  852. struct channel_subsystem *css = to_css(sch->dev.parent);
  853. return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
  854. }
  855. static void io_subchannel_irq(struct subchannel *sch)
  856. {
  857. struct ccw_device *cdev;
  858. cdev = sch_get_cdev(sch);
  859. CIO_TRACE_EVENT(6, "IRQ");
  860. CIO_TRACE_EVENT(6, dev_name(&sch->dev));
  861. if (cdev)
  862. dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
  863. }
  864. void io_subchannel_init_config(struct subchannel *sch)
  865. {
  866. memset(&sch->config, 0, sizeof(sch->config));
  867. sch->config.csense = 1;
  868. }
  869. static void io_subchannel_init_fields(struct subchannel *sch)
  870. {
  871. if (cio_is_console(sch->schid))
  872. sch->opm = 0xff;
  873. else
  874. sch->opm = chp_get_sch_opm(sch);
  875. sch->lpm = sch->schib.pmcw.pam & sch->opm;
  876. sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
  877. CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
  878. " - PIM = %02X, PAM = %02X, POM = %02X\n",
  879. sch->schib.pmcw.dev, sch->schid.ssid,
  880. sch->schid.sch_no, sch->schib.pmcw.pim,
  881. sch->schib.pmcw.pam, sch->schib.pmcw.pom);
  882. io_subchannel_init_config(sch);
  883. }
  884. /*
  885. * Note: We always return 0 so that we bind to the device even on error.
  886. * This is needed so that our remove function is called on unregister.
  887. */
  888. static int io_subchannel_probe(struct subchannel *sch)
  889. {
  890. struct ccw_device *cdev;
  891. int rc;
  892. if (cio_is_console(sch->schid)) {
  893. rc = sysfs_create_group(&sch->dev.kobj,
  894. &io_subchannel_attr_group);
  895. if (rc)
  896. CIO_MSG_EVENT(0, "Failed to create io subchannel "
  897. "attributes for subchannel "
  898. "0.%x.%04x (rc=%d)\n",
  899. sch->schid.ssid, sch->schid.sch_no, rc);
  900. /*
  901. * The console subchannel already has an associated ccw_device.
  902. * Throw the delayed uevent for the subchannel, register
  903. * the ccw_device and exit.
  904. */
  905. dev_set_uevent_suppress(&sch->dev, 0);
  906. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  907. cdev = sch_get_cdev(sch);
  908. cdev->dev.groups = ccwdev_attr_groups;
  909. device_initialize(&cdev->dev);
  910. cdev->private->flags.initialized = 1;
  911. ccw_device_register(cdev);
  912. /*
  913. * Check if the device is already online. If it is
  914. * the reference count needs to be corrected since we
  915. * didn't obtain a reference in ccw_device_set_online.
  916. */
  917. if (cdev->private->state != DEV_STATE_NOT_OPER &&
  918. cdev->private->state != DEV_STATE_OFFLINE &&
  919. cdev->private->state != DEV_STATE_BOXED)
  920. get_device(&cdev->dev);
  921. return 0;
  922. }
  923. io_subchannel_init_fields(sch);
  924. rc = cio_commit_config(sch);
  925. if (rc)
  926. goto out_schedule;
  927. rc = sysfs_create_group(&sch->dev.kobj,
  928. &io_subchannel_attr_group);
  929. if (rc)
  930. goto out_schedule;
  931. /* Allocate I/O subchannel private data. */
  932. sch->private = kzalloc(sizeof(struct io_subchannel_private),
  933. GFP_KERNEL | GFP_DMA);
  934. if (!sch->private)
  935. goto out_schedule;
  936. css_schedule_eval(sch->schid);
  937. return 0;
  938. out_schedule:
  939. spin_lock_irq(sch->lock);
  940. css_sched_sch_todo(sch, SCH_TODO_UNREG);
  941. spin_unlock_irq(sch->lock);
  942. return 0;
  943. }
  944. static int
  945. io_subchannel_remove (struct subchannel *sch)
  946. {
  947. struct ccw_device *cdev;
  948. cdev = sch_get_cdev(sch);
  949. if (!cdev)
  950. goto out_free;
  951. io_subchannel_quiesce(sch);
  952. /* Set ccw device to not operational and drop reference. */
  953. spin_lock_irq(cdev->ccwlock);
  954. sch_set_cdev(sch, NULL);
  955. cdev->private->state = DEV_STATE_NOT_OPER;
  956. spin_unlock_irq(cdev->ccwlock);
  957. ccw_device_unregister(cdev);
  958. out_free:
  959. kfree(sch->private);
  960. sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
  961. return 0;
  962. }
  963. static void io_subchannel_verify(struct subchannel *sch)
  964. {
  965. struct ccw_device *cdev;
  966. cdev = sch_get_cdev(sch);
  967. if (cdev)
  968. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  969. }
  970. static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
  971. {
  972. struct ccw_device *cdev;
  973. cdev = sch_get_cdev(sch);
  974. if (!cdev)
  975. return;
  976. if (cio_update_schib(sch))
  977. goto err;
  978. /* Check for I/O on path. */
  979. if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
  980. goto out;
  981. if (cdev->private->state == DEV_STATE_ONLINE) {
  982. ccw_device_kill_io(cdev);
  983. goto out;
  984. }
  985. if (cio_clear(sch))
  986. goto err;
  987. out:
  988. /* Trigger path verification. */
  989. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  990. return;
  991. err:
  992. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  993. }
  994. static int io_subchannel_chp_event(struct subchannel *sch,
  995. struct chp_link *link, int event)
  996. {
  997. int mask;
  998. mask = chp_ssd_get_mask(&sch->ssd_info, link);
  999. if (!mask)
  1000. return 0;
  1001. switch (event) {
  1002. case CHP_VARY_OFF:
  1003. sch->opm &= ~mask;
  1004. sch->lpm &= ~mask;
  1005. io_subchannel_terminate_path(sch, mask);
  1006. break;
  1007. case CHP_VARY_ON:
  1008. sch->opm |= mask;
  1009. sch->lpm |= mask;
  1010. io_subchannel_verify(sch);
  1011. break;
  1012. case CHP_OFFLINE:
  1013. if (cio_update_schib(sch))
  1014. return -ENODEV;
  1015. io_subchannel_terminate_path(sch, mask);
  1016. break;
  1017. case CHP_ONLINE:
  1018. if (cio_update_schib(sch))
  1019. return -ENODEV;
  1020. sch->lpm |= mask & sch->opm;
  1021. io_subchannel_verify(sch);
  1022. break;
  1023. }
  1024. return 0;
  1025. }
  1026. static void io_subchannel_quiesce(struct subchannel *sch)
  1027. {
  1028. struct ccw_device *cdev;
  1029. int ret;
  1030. spin_lock_irq(sch->lock);
  1031. cdev = sch_get_cdev(sch);
  1032. if (cio_is_console(sch->schid))
  1033. goto out_unlock;
  1034. if (!sch->schib.pmcw.ena)
  1035. goto out_unlock;
  1036. ret = cio_disable_subchannel(sch);
  1037. if (ret != -EBUSY)
  1038. goto out_unlock;
  1039. if (cdev->handler)
  1040. cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
  1041. while (ret == -EBUSY) {
  1042. cdev->private->state = DEV_STATE_QUIESCE;
  1043. ret = ccw_device_cancel_halt_clear(cdev);
  1044. if (ret == -EBUSY) {
  1045. ccw_device_set_timeout(cdev, HZ/10);
  1046. spin_unlock_irq(sch->lock);
  1047. wait_event(cdev->private->wait_q,
  1048. cdev->private->state != DEV_STATE_QUIESCE);
  1049. spin_lock_irq(sch->lock);
  1050. }
  1051. ret = cio_disable_subchannel(sch);
  1052. }
  1053. out_unlock:
  1054. spin_unlock_irq(sch->lock);
  1055. }
  1056. static void io_subchannel_shutdown(struct subchannel *sch)
  1057. {
  1058. io_subchannel_quiesce(sch);
  1059. }
  1060. static int device_is_disconnected(struct ccw_device *cdev)
  1061. {
  1062. if (!cdev)
  1063. return 0;
  1064. return (cdev->private->state == DEV_STATE_DISCONNECTED ||
  1065. cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
  1066. }
  1067. static int recovery_check(struct device *dev, void *data)
  1068. {
  1069. struct ccw_device *cdev = to_ccwdev(dev);
  1070. int *redo = data;
  1071. spin_lock_irq(cdev->ccwlock);
  1072. switch (cdev->private->state) {
  1073. case DEV_STATE_DISCONNECTED:
  1074. CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
  1075. cdev->private->dev_id.ssid,
  1076. cdev->private->dev_id.devno);
  1077. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1078. *redo = 1;
  1079. break;
  1080. case DEV_STATE_DISCONNECTED_SENSE_ID:
  1081. *redo = 1;
  1082. break;
  1083. }
  1084. spin_unlock_irq(cdev->ccwlock);
  1085. return 0;
  1086. }
  1087. static void recovery_work_func(struct work_struct *unused)
  1088. {
  1089. int redo = 0;
  1090. bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
  1091. if (redo) {
  1092. spin_lock_irq(&recovery_lock);
  1093. if (!timer_pending(&recovery_timer)) {
  1094. if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
  1095. recovery_phase++;
  1096. mod_timer(&recovery_timer, jiffies +
  1097. recovery_delay[recovery_phase] * HZ);
  1098. }
  1099. spin_unlock_irq(&recovery_lock);
  1100. } else
  1101. CIO_MSG_EVENT(4, "recovery: end\n");
  1102. }
  1103. static DECLARE_WORK(recovery_work, recovery_work_func);
  1104. static void recovery_func(unsigned long data)
  1105. {
  1106. /*
  1107. * We can't do our recovery in softirq context and it's not
  1108. * performance critical, so we schedule it.
  1109. */
  1110. schedule_work(&recovery_work);
  1111. }
  1112. static void ccw_device_schedule_recovery(void)
  1113. {
  1114. unsigned long flags;
  1115. CIO_MSG_EVENT(4, "recovery: schedule\n");
  1116. spin_lock_irqsave(&recovery_lock, flags);
  1117. if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
  1118. recovery_phase = 0;
  1119. mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
  1120. }
  1121. spin_unlock_irqrestore(&recovery_lock, flags);
  1122. }
  1123. static int purge_fn(struct device *dev, void *data)
  1124. {
  1125. struct ccw_device *cdev = to_ccwdev(dev);
  1126. struct ccw_dev_id *id = &cdev->private->dev_id;
  1127. spin_lock_irq(cdev->ccwlock);
  1128. if (is_blacklisted(id->ssid, id->devno) &&
  1129. (cdev->private->state == DEV_STATE_OFFLINE)) {
  1130. CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
  1131. id->devno);
  1132. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  1133. }
  1134. spin_unlock_irq(cdev->ccwlock);
  1135. /* Abort loop in case of pending signal. */
  1136. if (signal_pending(current))
  1137. return -EINTR;
  1138. return 0;
  1139. }
  1140. /**
  1141. * ccw_purge_blacklisted - purge unused, blacklisted devices
  1142. *
  1143. * Unregister all ccw devices that are offline and on the blacklist.
  1144. */
  1145. int ccw_purge_blacklisted(void)
  1146. {
  1147. CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
  1148. bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
  1149. return 0;
  1150. }
  1151. void ccw_device_set_disconnected(struct ccw_device *cdev)
  1152. {
  1153. if (!cdev)
  1154. return;
  1155. ccw_device_set_timeout(cdev, 0);
  1156. cdev->private->flags.fake_irb = 0;
  1157. cdev->private->state = DEV_STATE_DISCONNECTED;
  1158. if (cdev->online)
  1159. ccw_device_schedule_recovery();
  1160. }
  1161. void ccw_device_set_notoper(struct ccw_device *cdev)
  1162. {
  1163. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1164. CIO_TRACE_EVENT(2, "notoper");
  1165. CIO_TRACE_EVENT(2, dev_name(&sch->dev));
  1166. ccw_device_set_timeout(cdev, 0);
  1167. cio_disable_subchannel(sch);
  1168. cdev->private->state = DEV_STATE_NOT_OPER;
  1169. }
  1170. enum io_sch_action {
  1171. IO_SCH_UNREG,
  1172. IO_SCH_ORPH_UNREG,
  1173. IO_SCH_ATTACH,
  1174. IO_SCH_UNREG_ATTACH,
  1175. IO_SCH_ORPH_ATTACH,
  1176. IO_SCH_REPROBE,
  1177. IO_SCH_VERIFY,
  1178. IO_SCH_DISC,
  1179. IO_SCH_NOP,
  1180. };
  1181. static enum io_sch_action sch_get_action(struct subchannel *sch)
  1182. {
  1183. struct ccw_device *cdev;
  1184. cdev = sch_get_cdev(sch);
  1185. if (cio_update_schib(sch)) {
  1186. /* Not operational. */
  1187. if (!cdev)
  1188. return IO_SCH_UNREG;
  1189. if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
  1190. return IO_SCH_UNREG;
  1191. return IO_SCH_ORPH_UNREG;
  1192. }
  1193. /* Operational. */
  1194. if (!cdev)
  1195. return IO_SCH_ATTACH;
  1196. if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
  1197. if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
  1198. return IO_SCH_UNREG_ATTACH;
  1199. return IO_SCH_ORPH_ATTACH;
  1200. }
  1201. if ((sch->schib.pmcw.pam & sch->opm) == 0) {
  1202. if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
  1203. return IO_SCH_UNREG;
  1204. return IO_SCH_DISC;
  1205. }
  1206. if (device_is_disconnected(cdev))
  1207. return IO_SCH_REPROBE;
  1208. if (cdev->online)
  1209. return IO_SCH_VERIFY;
  1210. return IO_SCH_NOP;
  1211. }
  1212. /**
  1213. * io_subchannel_sch_event - process subchannel event
  1214. * @sch: subchannel
  1215. * @process: non-zero if function is called in process context
  1216. *
  1217. * An unspecified event occurred for this subchannel. Adjust data according
  1218. * to the current operational state of the subchannel and device. Return
  1219. * zero when the event has been handled sufficiently or -EAGAIN when this
  1220. * function should be called again in process context.
  1221. */
  1222. static int io_subchannel_sch_event(struct subchannel *sch, int process)
  1223. {
  1224. unsigned long flags;
  1225. struct ccw_device *cdev;
  1226. struct ccw_dev_id dev_id;
  1227. enum io_sch_action action;
  1228. int rc = -EAGAIN;
  1229. spin_lock_irqsave(sch->lock, flags);
  1230. if (!device_is_registered(&sch->dev))
  1231. goto out_unlock;
  1232. if (work_pending(&sch->todo_work))
  1233. goto out_unlock;
  1234. cdev = sch_get_cdev(sch);
  1235. if (cdev && work_pending(&cdev->private->todo_work))
  1236. goto out_unlock;
  1237. action = sch_get_action(sch);
  1238. CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
  1239. sch->schid.ssid, sch->schid.sch_no, process,
  1240. action);
  1241. /* Perform immediate actions while holding the lock. */
  1242. switch (action) {
  1243. case IO_SCH_REPROBE:
  1244. /* Trigger device recognition. */
  1245. ccw_device_trigger_reprobe(cdev);
  1246. rc = 0;
  1247. goto out_unlock;
  1248. case IO_SCH_VERIFY:
  1249. if (cdev->private->flags.resuming == 1) {
  1250. if (cio_enable_subchannel(sch, (u32)(addr_t)sch)) {
  1251. ccw_device_set_notoper(cdev);
  1252. break;
  1253. }
  1254. }
  1255. /* Trigger path verification. */
  1256. io_subchannel_verify(sch);
  1257. rc = 0;
  1258. goto out_unlock;
  1259. case IO_SCH_DISC:
  1260. ccw_device_set_disconnected(cdev);
  1261. rc = 0;
  1262. goto out_unlock;
  1263. case IO_SCH_ORPH_UNREG:
  1264. case IO_SCH_ORPH_ATTACH:
  1265. ccw_device_set_disconnected(cdev);
  1266. break;
  1267. case IO_SCH_UNREG_ATTACH:
  1268. case IO_SCH_UNREG:
  1269. if (cdev)
  1270. ccw_device_set_notoper(cdev);
  1271. break;
  1272. case IO_SCH_NOP:
  1273. rc = 0;
  1274. goto out_unlock;
  1275. default:
  1276. break;
  1277. }
  1278. spin_unlock_irqrestore(sch->lock, flags);
  1279. /* All other actions require process context. */
  1280. if (!process)
  1281. goto out;
  1282. /* Handle attached ccw device. */
  1283. switch (action) {
  1284. case IO_SCH_ORPH_UNREG:
  1285. case IO_SCH_ORPH_ATTACH:
  1286. /* Move ccw device to orphanage. */
  1287. rc = ccw_device_move_to_orph(cdev);
  1288. if (rc)
  1289. goto out;
  1290. break;
  1291. case IO_SCH_UNREG_ATTACH:
  1292. /* Unregister ccw device. */
  1293. if (!cdev->private->flags.resuming)
  1294. ccw_device_unregister(cdev);
  1295. break;
  1296. default:
  1297. break;
  1298. }
  1299. /* Handle subchannel. */
  1300. switch (action) {
  1301. case IO_SCH_ORPH_UNREG:
  1302. case IO_SCH_UNREG:
  1303. if (!cdev || !cdev->private->flags.resuming)
  1304. css_sch_device_unregister(sch);
  1305. break;
  1306. case IO_SCH_ORPH_ATTACH:
  1307. case IO_SCH_UNREG_ATTACH:
  1308. case IO_SCH_ATTACH:
  1309. dev_id.ssid = sch->schid.ssid;
  1310. dev_id.devno = sch->schib.pmcw.dev;
  1311. cdev = get_ccwdev_by_dev_id(&dev_id);
  1312. if (!cdev) {
  1313. sch_create_and_recog_new_device(sch);
  1314. break;
  1315. }
  1316. rc = ccw_device_move_to_sch(cdev, sch);
  1317. if (rc) {
  1318. /* Release reference from get_ccwdev_by_dev_id() */
  1319. put_device(&cdev->dev);
  1320. goto out;
  1321. }
  1322. spin_lock_irqsave(sch->lock, flags);
  1323. ccw_device_trigger_reprobe(cdev);
  1324. spin_unlock_irqrestore(sch->lock, flags);
  1325. /* Release reference from get_ccwdev_by_dev_id() */
  1326. put_device(&cdev->dev);
  1327. break;
  1328. default:
  1329. break;
  1330. }
  1331. return 0;
  1332. out_unlock:
  1333. spin_unlock_irqrestore(sch->lock, flags);
  1334. out:
  1335. return rc;
  1336. }
  1337. #ifdef CONFIG_CCW_CONSOLE
  1338. static struct ccw_device console_cdev;
  1339. static struct ccw_device_private console_private;
  1340. static int console_cdev_in_use;
  1341. static DEFINE_SPINLOCK(ccw_console_lock);
  1342. spinlock_t * cio_get_console_lock(void)
  1343. {
  1344. return &ccw_console_lock;
  1345. }
  1346. static int ccw_device_console_enable(struct ccw_device *cdev,
  1347. struct subchannel *sch)
  1348. {
  1349. int rc;
  1350. /* Attach subchannel private data. */
  1351. sch->private = cio_get_console_priv();
  1352. memset(sch->private, 0, sizeof(struct io_subchannel_private));
  1353. io_subchannel_init_fields(sch);
  1354. rc = cio_commit_config(sch);
  1355. if (rc)
  1356. return rc;
  1357. sch->driver = &io_subchannel_driver;
  1358. /* Initialize the ccw_device structure. */
  1359. cdev->dev.parent= &sch->dev;
  1360. sch_set_cdev(sch, cdev);
  1361. io_subchannel_recog(cdev, sch);
  1362. /* Now wait for the async. recognition to come to an end. */
  1363. spin_lock_irq(cdev->ccwlock);
  1364. while (!dev_fsm_final_state(cdev))
  1365. wait_cons_dev();
  1366. rc = -EIO;
  1367. if (cdev->private->state != DEV_STATE_OFFLINE)
  1368. goto out_unlock;
  1369. ccw_device_online(cdev);
  1370. while (!dev_fsm_final_state(cdev))
  1371. wait_cons_dev();
  1372. if (cdev->private->state != DEV_STATE_ONLINE)
  1373. goto out_unlock;
  1374. rc = 0;
  1375. out_unlock:
  1376. spin_unlock_irq(cdev->ccwlock);
  1377. return rc;
  1378. }
  1379. struct ccw_device *
  1380. ccw_device_probe_console(void)
  1381. {
  1382. struct subchannel *sch;
  1383. int ret;
  1384. if (xchg(&console_cdev_in_use, 1) != 0)
  1385. return ERR_PTR(-EBUSY);
  1386. sch = cio_probe_console();
  1387. if (IS_ERR(sch)) {
  1388. console_cdev_in_use = 0;
  1389. return (void *) sch;
  1390. }
  1391. memset(&console_cdev, 0, sizeof(struct ccw_device));
  1392. memset(&console_private, 0, sizeof(struct ccw_device_private));
  1393. console_cdev.private = &console_private;
  1394. console_private.cdev = &console_cdev;
  1395. ret = ccw_device_console_enable(&console_cdev, sch);
  1396. if (ret) {
  1397. cio_release_console();
  1398. console_cdev_in_use = 0;
  1399. return ERR_PTR(ret);
  1400. }
  1401. console_cdev.online = 1;
  1402. return &console_cdev;
  1403. }
  1404. static int ccw_device_pm_restore(struct device *dev);
  1405. int ccw_device_force_console(void)
  1406. {
  1407. if (!console_cdev_in_use)
  1408. return -ENODEV;
  1409. return ccw_device_pm_restore(&console_cdev.dev);
  1410. }
  1411. EXPORT_SYMBOL_GPL(ccw_device_force_console);
  1412. #endif
  1413. /*
  1414. * get ccw_device matching the busid, but only if owned by cdrv
  1415. */
  1416. static int
  1417. __ccwdev_check_busid(struct device *dev, void *id)
  1418. {
  1419. char *bus_id;
  1420. bus_id = id;
  1421. return (strcmp(bus_id, dev_name(dev)) == 0);
  1422. }
  1423. /**
  1424. * get_ccwdev_by_busid() - obtain device from a bus id
  1425. * @cdrv: driver the device is owned by
  1426. * @bus_id: bus id of the device to be searched
  1427. *
  1428. * This function searches all devices owned by @cdrv for a device with a bus
  1429. * id matching @bus_id.
  1430. * Returns:
  1431. * If a match is found, its reference count of the found device is increased
  1432. * and it is returned; else %NULL is returned.
  1433. */
  1434. struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
  1435. const char *bus_id)
  1436. {
  1437. struct device *dev;
  1438. struct device_driver *drv;
  1439. drv = get_driver(&cdrv->driver);
  1440. if (!drv)
  1441. return NULL;
  1442. dev = driver_find_device(drv, NULL, (void *)bus_id,
  1443. __ccwdev_check_busid);
  1444. put_driver(drv);
  1445. return dev ? to_ccwdev(dev) : NULL;
  1446. }
  1447. /************************** device driver handling ************************/
  1448. /* This is the implementation of the ccw_driver class. The probe, remove
  1449. * and release methods are initially very similar to the device_driver
  1450. * implementations, with the difference that they have ccw_device
  1451. * arguments.
  1452. *
  1453. * A ccw driver also contains the information that is needed for
  1454. * device matching.
  1455. */
  1456. static int
  1457. ccw_device_probe (struct device *dev)
  1458. {
  1459. struct ccw_device *cdev = to_ccwdev(dev);
  1460. struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
  1461. int ret;
  1462. cdev->drv = cdrv; /* to let the driver call _set_online */
  1463. ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
  1464. if (ret) {
  1465. cdev->drv = NULL;
  1466. return ret;
  1467. }
  1468. return 0;
  1469. }
  1470. static int
  1471. ccw_device_remove (struct device *dev)
  1472. {
  1473. struct ccw_device *cdev = to_ccwdev(dev);
  1474. struct ccw_driver *cdrv = cdev->drv;
  1475. int ret;
  1476. if (cdrv->remove)
  1477. cdrv->remove(cdev);
  1478. if (cdev->online) {
  1479. cdev->online = 0;
  1480. spin_lock_irq(cdev->ccwlock);
  1481. ret = ccw_device_offline(cdev);
  1482. spin_unlock_irq(cdev->ccwlock);
  1483. if (ret == 0)
  1484. wait_event(cdev->private->wait_q,
  1485. dev_fsm_final_state(cdev));
  1486. else
  1487. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
  1488. "device 0.%x.%04x\n",
  1489. ret, cdev->private->dev_id.ssid,
  1490. cdev->private->dev_id.devno);
  1491. /* Give up reference obtained in ccw_device_set_online(). */
  1492. put_device(&cdev->dev);
  1493. }
  1494. ccw_device_set_timeout(cdev, 0);
  1495. cdev->drv = NULL;
  1496. return 0;
  1497. }
  1498. static void ccw_device_shutdown(struct device *dev)
  1499. {
  1500. struct ccw_device *cdev;
  1501. cdev = to_ccwdev(dev);
  1502. if (cdev->drv && cdev->drv->shutdown)
  1503. cdev->drv->shutdown(cdev);
  1504. disable_cmf(cdev);
  1505. }
  1506. static int ccw_device_pm_prepare(struct device *dev)
  1507. {
  1508. struct ccw_device *cdev = to_ccwdev(dev);
  1509. if (work_pending(&cdev->private->todo_work))
  1510. return -EAGAIN;
  1511. /* Fail while device is being set online/offline. */
  1512. if (atomic_read(&cdev->private->onoff))
  1513. return -EAGAIN;
  1514. if (cdev->online && cdev->drv && cdev->drv->prepare)
  1515. return cdev->drv->prepare(cdev);
  1516. return 0;
  1517. }
  1518. static void ccw_device_pm_complete(struct device *dev)
  1519. {
  1520. struct ccw_device *cdev = to_ccwdev(dev);
  1521. if (cdev->online && cdev->drv && cdev->drv->complete)
  1522. cdev->drv->complete(cdev);
  1523. }
  1524. static int ccw_device_pm_freeze(struct device *dev)
  1525. {
  1526. struct ccw_device *cdev = to_ccwdev(dev);
  1527. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1528. int ret, cm_enabled;
  1529. /* Fail suspend while device is in transistional state. */
  1530. if (!dev_fsm_final_state(cdev))
  1531. return -EAGAIN;
  1532. if (!cdev->online)
  1533. return 0;
  1534. if (cdev->drv && cdev->drv->freeze) {
  1535. ret = cdev->drv->freeze(cdev);
  1536. if (ret)
  1537. return ret;
  1538. }
  1539. spin_lock_irq(sch->lock);
  1540. cm_enabled = cdev->private->cmb != NULL;
  1541. spin_unlock_irq(sch->lock);
  1542. if (cm_enabled) {
  1543. /* Don't have the css write on memory. */
  1544. ret = ccw_set_cmf(cdev, 0);
  1545. if (ret)
  1546. return ret;
  1547. }
  1548. /* From here on, disallow device driver I/O. */
  1549. spin_lock_irq(sch->lock);
  1550. ret = cio_disable_subchannel(sch);
  1551. spin_unlock_irq(sch->lock);
  1552. return ret;
  1553. }
  1554. static int ccw_device_pm_thaw(struct device *dev)
  1555. {
  1556. struct ccw_device *cdev = to_ccwdev(dev);
  1557. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1558. int ret, cm_enabled;
  1559. if (!cdev->online)
  1560. return 0;
  1561. spin_lock_irq(sch->lock);
  1562. /* Allow device driver I/O again. */
  1563. ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
  1564. cm_enabled = cdev->private->cmb != NULL;
  1565. spin_unlock_irq(sch->lock);
  1566. if (ret)
  1567. return ret;
  1568. if (cm_enabled) {
  1569. ret = ccw_set_cmf(cdev, 1);
  1570. if (ret)
  1571. return ret;
  1572. }
  1573. if (cdev->drv && cdev->drv->thaw)
  1574. ret = cdev->drv->thaw(cdev);
  1575. return ret;
  1576. }
  1577. static void __ccw_device_pm_restore(struct ccw_device *cdev)
  1578. {
  1579. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1580. spin_lock_irq(sch->lock);
  1581. if (cio_is_console(sch->schid)) {
  1582. cio_enable_subchannel(sch, (u32)(addr_t)sch);
  1583. goto out_unlock;
  1584. }
  1585. /*
  1586. * While we were sleeping, devices may have gone or become
  1587. * available again. Kick re-detection.
  1588. */
  1589. cdev->private->flags.resuming = 1;
  1590. css_schedule_eval(sch->schid);
  1591. spin_unlock_irq(sch->lock);
  1592. css_complete_work();
  1593. /* cdev may have been moved to a different subchannel. */
  1594. sch = to_subchannel(cdev->dev.parent);
  1595. spin_lock_irq(sch->lock);
  1596. if (cdev->private->state != DEV_STATE_ONLINE &&
  1597. cdev->private->state != DEV_STATE_OFFLINE)
  1598. goto out_unlock;
  1599. ccw_device_recognition(cdev);
  1600. spin_unlock_irq(sch->lock);
  1601. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
  1602. cdev->private->state == DEV_STATE_DISCONNECTED);
  1603. spin_lock_irq(sch->lock);
  1604. out_unlock:
  1605. cdev->private->flags.resuming = 0;
  1606. spin_unlock_irq(sch->lock);
  1607. }
  1608. static int resume_handle_boxed(struct ccw_device *cdev)
  1609. {
  1610. cdev->private->state = DEV_STATE_BOXED;
  1611. if (ccw_device_notify(cdev, CIO_BOXED) == NOTIFY_OK)
  1612. return 0;
  1613. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  1614. return -ENODEV;
  1615. }
  1616. static int resume_handle_disc(struct ccw_device *cdev)
  1617. {
  1618. cdev->private->state = DEV_STATE_DISCONNECTED;
  1619. if (ccw_device_notify(cdev, CIO_GONE) == NOTIFY_OK)
  1620. return 0;
  1621. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  1622. return -ENODEV;
  1623. }
  1624. static int ccw_device_pm_restore(struct device *dev)
  1625. {
  1626. struct ccw_device *cdev = to_ccwdev(dev);
  1627. struct subchannel *sch;
  1628. int ret = 0;
  1629. __ccw_device_pm_restore(cdev);
  1630. sch = to_subchannel(cdev->dev.parent);
  1631. spin_lock_irq(sch->lock);
  1632. if (cio_is_console(sch->schid))
  1633. goto out_restore;
  1634. /* check recognition results */
  1635. switch (cdev->private->state) {
  1636. case DEV_STATE_OFFLINE:
  1637. case DEV_STATE_ONLINE:
  1638. cdev->private->flags.donotify = 0;
  1639. break;
  1640. case DEV_STATE_BOXED:
  1641. ret = resume_handle_boxed(cdev);
  1642. if (ret)
  1643. goto out_unlock;
  1644. goto out_restore;
  1645. default:
  1646. ret = resume_handle_disc(cdev);
  1647. if (ret)
  1648. goto out_unlock;
  1649. goto out_restore;
  1650. }
  1651. /* check if the device type has changed */
  1652. if (!ccw_device_test_sense_data(cdev)) {
  1653. ccw_device_update_sense_data(cdev);
  1654. ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
  1655. ret = -ENODEV;
  1656. goto out_unlock;
  1657. }
  1658. if (!cdev->online)
  1659. goto out_unlock;
  1660. if (ccw_device_online(cdev)) {
  1661. ret = resume_handle_disc(cdev);
  1662. if (ret)
  1663. goto out_unlock;
  1664. goto out_restore;
  1665. }
  1666. spin_unlock_irq(sch->lock);
  1667. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  1668. spin_lock_irq(sch->lock);
  1669. if (ccw_device_notify(cdev, CIO_OPER) == NOTIFY_BAD) {
  1670. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  1671. ret = -ENODEV;
  1672. goto out_unlock;
  1673. }
  1674. /* reenable cmf, if needed */
  1675. if (cdev->private->cmb) {
  1676. spin_unlock_irq(sch->lock);
  1677. ret = ccw_set_cmf(cdev, 1);
  1678. spin_lock_irq(sch->lock);
  1679. if (ret) {
  1680. CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
  1681. "(rc=%d)\n", cdev->private->dev_id.ssid,
  1682. cdev->private->dev_id.devno, ret);
  1683. ret = 0;
  1684. }
  1685. }
  1686. out_restore:
  1687. spin_unlock_irq(sch->lock);
  1688. if (cdev->online && cdev->drv && cdev->drv->restore)
  1689. ret = cdev->drv->restore(cdev);
  1690. return ret;
  1691. out_unlock:
  1692. spin_unlock_irq(sch->lock);
  1693. return ret;
  1694. }
  1695. static const struct dev_pm_ops ccw_pm_ops = {
  1696. .prepare = ccw_device_pm_prepare,
  1697. .complete = ccw_device_pm_complete,
  1698. .freeze = ccw_device_pm_freeze,
  1699. .thaw = ccw_device_pm_thaw,
  1700. .restore = ccw_device_pm_restore,
  1701. };
  1702. struct bus_type ccw_bus_type = {
  1703. .name = "ccw",
  1704. .match = ccw_bus_match,
  1705. .uevent = ccw_uevent,
  1706. .probe = ccw_device_probe,
  1707. .remove = ccw_device_remove,
  1708. .shutdown = ccw_device_shutdown,
  1709. .pm = &ccw_pm_ops,
  1710. };
  1711. /**
  1712. * ccw_driver_register() - register a ccw driver
  1713. * @cdriver: driver to be registered
  1714. *
  1715. * This function is mainly a wrapper around driver_register().
  1716. * Returns:
  1717. * %0 on success and a negative error value on failure.
  1718. */
  1719. int ccw_driver_register(struct ccw_driver *cdriver)
  1720. {
  1721. struct device_driver *drv = &cdriver->driver;
  1722. drv->bus = &ccw_bus_type;
  1723. drv->name = cdriver->name;
  1724. drv->owner = cdriver->owner;
  1725. return driver_register(drv);
  1726. }
  1727. /**
  1728. * ccw_driver_unregister() - deregister a ccw driver
  1729. * @cdriver: driver to be deregistered
  1730. *
  1731. * This function is mainly a wrapper around driver_unregister().
  1732. */
  1733. void ccw_driver_unregister(struct ccw_driver *cdriver)
  1734. {
  1735. driver_unregister(&cdriver->driver);
  1736. }
  1737. /* Helper func for qdio. */
  1738. struct subchannel_id
  1739. ccw_device_get_subchannel_id(struct ccw_device *cdev)
  1740. {
  1741. struct subchannel *sch;
  1742. sch = to_subchannel(cdev->dev.parent);
  1743. return sch->schid;
  1744. }
  1745. static void ccw_device_todo(struct work_struct *work)
  1746. {
  1747. struct ccw_device_private *priv;
  1748. struct ccw_device *cdev;
  1749. struct subchannel *sch;
  1750. enum cdev_todo todo;
  1751. priv = container_of(work, struct ccw_device_private, todo_work);
  1752. cdev = priv->cdev;
  1753. sch = to_subchannel(cdev->dev.parent);
  1754. /* Find out todo. */
  1755. spin_lock_irq(cdev->ccwlock);
  1756. todo = priv->todo;
  1757. priv->todo = CDEV_TODO_NOTHING;
  1758. CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
  1759. priv->dev_id.ssid, priv->dev_id.devno, todo);
  1760. spin_unlock_irq(cdev->ccwlock);
  1761. /* Perform todo. */
  1762. switch (todo) {
  1763. case CDEV_TODO_ENABLE_CMF:
  1764. cmf_reenable(cdev);
  1765. break;
  1766. case CDEV_TODO_REBIND:
  1767. ccw_device_do_unbind_bind(cdev);
  1768. break;
  1769. case CDEV_TODO_REGISTER:
  1770. io_subchannel_register(cdev);
  1771. break;
  1772. case CDEV_TODO_UNREG_EVAL:
  1773. if (!sch_is_pseudo_sch(sch))
  1774. css_schedule_eval(sch->schid);
  1775. /* fall-through */
  1776. case CDEV_TODO_UNREG:
  1777. if (sch_is_pseudo_sch(sch))
  1778. ccw_device_unregister(cdev);
  1779. else
  1780. ccw_device_call_sch_unregister(cdev);
  1781. break;
  1782. default:
  1783. break;
  1784. }
  1785. /* Release workqueue ref. */
  1786. put_device(&cdev->dev);
  1787. }
  1788. /**
  1789. * ccw_device_sched_todo - schedule ccw device operation
  1790. * @cdev: ccw device
  1791. * @todo: todo
  1792. *
  1793. * Schedule the operation identified by @todo to be performed on the slow path
  1794. * workqueue. Do nothing if another operation with higher priority is already
  1795. * scheduled. Needs to be called with ccwdev lock held.
  1796. */
  1797. void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
  1798. {
  1799. CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
  1800. cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
  1801. todo);
  1802. if (cdev->private->todo >= todo)
  1803. return;
  1804. cdev->private->todo = todo;
  1805. /* Get workqueue ref. */
  1806. if (!get_device(&cdev->dev))
  1807. return;
  1808. if (!queue_work(cio_work_q, &cdev->private->todo_work)) {
  1809. /* Already queued, release workqueue ref. */
  1810. put_device(&cdev->dev);
  1811. }
  1812. }
  1813. MODULE_LICENSE("GPL");
  1814. EXPORT_SYMBOL(ccw_device_set_online);
  1815. EXPORT_SYMBOL(ccw_device_set_offline);
  1816. EXPORT_SYMBOL(ccw_driver_register);
  1817. EXPORT_SYMBOL(ccw_driver_unregister);
  1818. EXPORT_SYMBOL(get_ccwdev_by_busid);
  1819. EXPORT_SYMBOL(ccw_bus_type);
  1820. EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);