device.c 51 KB

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