device.c 51 KB

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