device.c 54 KB

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