device.c 53 KB

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