device.c 52 KB

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