device.c 52 KB

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