device.c 53 KB

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