virtio_ccw.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  1. /*
  2. * ccw based virtio transport
  3. *
  4. * Copyright IBM Corp. 2012, 2014
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License (version 2 only)
  8. * as published by the Free Software Foundation.
  9. *
  10. * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
  11. */
  12. #include <linux/kernel_stat.h>
  13. #include <linux/init.h>
  14. #include <linux/bootmem.h>
  15. #include <linux/err.h>
  16. #include <linux/virtio.h>
  17. #include <linux/virtio_config.h>
  18. #include <linux/slab.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/virtio_ring.h>
  21. #include <linux/pfn.h>
  22. #include <linux/async.h>
  23. #include <linux/wait.h>
  24. #include <linux/list.h>
  25. #include <linux/bitops.h>
  26. #include <linux/module.h>
  27. #include <linux/io.h>
  28. #include <linux/kvm_para.h>
  29. #include <linux/notifier.h>
  30. #include <asm/diag.h>
  31. #include <asm/setup.h>
  32. #include <asm/irq.h>
  33. #include <asm/cio.h>
  34. #include <asm/ccwdev.h>
  35. #include <asm/virtio-ccw.h>
  36. #include <asm/isc.h>
  37. #include <asm/airq.h>
  38. /*
  39. * virtio related functions
  40. */
  41. struct vq_config_block {
  42. __u16 index;
  43. __u16 num;
  44. } __packed;
  45. #define VIRTIO_CCW_CONFIG_SIZE 0x100
  46. /* same as PCI config space size, should be enough for all drivers */
  47. struct virtio_ccw_device {
  48. struct virtio_device vdev;
  49. __u8 *status;
  50. __u8 config[VIRTIO_CCW_CONFIG_SIZE];
  51. struct ccw_device *cdev;
  52. __u32 curr_io;
  53. int err;
  54. unsigned int revision; /* Transport revision */
  55. wait_queue_head_t wait_q;
  56. spinlock_t lock;
  57. struct mutex io_lock; /* Serializes I/O requests */
  58. struct list_head virtqueues;
  59. unsigned long indicators;
  60. unsigned long indicators2;
  61. struct vq_config_block *config_block;
  62. bool is_thinint;
  63. bool going_away;
  64. bool device_lost;
  65. unsigned int config_ready;
  66. void *airq_info;
  67. };
  68. struct vq_info_block_legacy {
  69. __u64 queue;
  70. __u32 align;
  71. __u16 index;
  72. __u16 num;
  73. } __packed;
  74. struct vq_info_block {
  75. __u64 desc;
  76. __u32 res0;
  77. __u16 index;
  78. __u16 num;
  79. __u64 avail;
  80. __u64 used;
  81. } __packed;
  82. struct virtio_feature_desc {
  83. __u32 features;
  84. __u8 index;
  85. } __packed;
  86. struct virtio_thinint_area {
  87. unsigned long summary_indicator;
  88. unsigned long indicator;
  89. u64 bit_nr;
  90. u8 isc;
  91. } __packed;
  92. struct virtio_rev_info {
  93. __u16 revision;
  94. __u16 length;
  95. __u8 data[];
  96. };
  97. /* the highest virtio-ccw revision we support */
  98. #define VIRTIO_CCW_REV_MAX 1
  99. struct virtio_ccw_vq_info {
  100. struct virtqueue *vq;
  101. int num;
  102. void *queue;
  103. union {
  104. struct vq_info_block s;
  105. struct vq_info_block_legacy l;
  106. } *info_block;
  107. int bit_nr;
  108. struct list_head node;
  109. long cookie;
  110. };
  111. #define VIRTIO_AIRQ_ISC IO_SCH_ISC /* inherit from subchannel */
  112. #define VIRTIO_IV_BITS (L1_CACHE_BYTES * 8)
  113. #define MAX_AIRQ_AREAS 20
  114. static int virtio_ccw_use_airq = 1;
  115. struct airq_info {
  116. rwlock_t lock;
  117. u8 summary_indicator;
  118. struct airq_struct airq;
  119. struct airq_iv *aiv;
  120. };
  121. static struct airq_info *airq_areas[MAX_AIRQ_AREAS];
  122. #define CCW_CMD_SET_VQ 0x13
  123. #define CCW_CMD_VDEV_RESET 0x33
  124. #define CCW_CMD_SET_IND 0x43
  125. #define CCW_CMD_SET_CONF_IND 0x53
  126. #define CCW_CMD_READ_FEAT 0x12
  127. #define CCW_CMD_WRITE_FEAT 0x11
  128. #define CCW_CMD_READ_CONF 0x22
  129. #define CCW_CMD_WRITE_CONF 0x21
  130. #define CCW_CMD_WRITE_STATUS 0x31
  131. #define CCW_CMD_READ_VQ_CONF 0x32
  132. #define CCW_CMD_SET_IND_ADAPTER 0x73
  133. #define CCW_CMD_SET_VIRTIO_REV 0x83
  134. #define VIRTIO_CCW_DOING_SET_VQ 0x00010000
  135. #define VIRTIO_CCW_DOING_RESET 0x00040000
  136. #define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
  137. #define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
  138. #define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
  139. #define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
  140. #define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
  141. #define VIRTIO_CCW_DOING_SET_IND 0x01000000
  142. #define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
  143. #define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
  144. #define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000
  145. #define VIRTIO_CCW_DOING_SET_VIRTIO_REV 0x10000000
  146. #define VIRTIO_CCW_INTPARM_MASK 0xffff0000
  147. static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev)
  148. {
  149. return container_of(vdev, struct virtio_ccw_device, vdev);
  150. }
  151. static void drop_airq_indicator(struct virtqueue *vq, struct airq_info *info)
  152. {
  153. unsigned long i, flags;
  154. write_lock_irqsave(&info->lock, flags);
  155. for (i = 0; i < airq_iv_end(info->aiv); i++) {
  156. if (vq == (void *)airq_iv_get_ptr(info->aiv, i)) {
  157. airq_iv_free_bit(info->aiv, i);
  158. airq_iv_set_ptr(info->aiv, i, 0);
  159. break;
  160. }
  161. }
  162. write_unlock_irqrestore(&info->lock, flags);
  163. }
  164. static void virtio_airq_handler(struct airq_struct *airq)
  165. {
  166. struct airq_info *info = container_of(airq, struct airq_info, airq);
  167. unsigned long ai;
  168. inc_irq_stat(IRQIO_VAI);
  169. read_lock(&info->lock);
  170. /* Walk through indicators field, summary indicator active. */
  171. for (ai = 0;;) {
  172. ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
  173. if (ai == -1UL)
  174. break;
  175. vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
  176. }
  177. info->summary_indicator = 0;
  178. smp_wmb();
  179. /* Walk through indicators field, summary indicator not active. */
  180. for (ai = 0;;) {
  181. ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
  182. if (ai == -1UL)
  183. break;
  184. vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
  185. }
  186. read_unlock(&info->lock);
  187. }
  188. static struct airq_info *new_airq_info(void)
  189. {
  190. struct airq_info *info;
  191. int rc;
  192. info = kzalloc(sizeof(*info), GFP_KERNEL);
  193. if (!info)
  194. return NULL;
  195. rwlock_init(&info->lock);
  196. info->aiv = airq_iv_create(VIRTIO_IV_BITS, AIRQ_IV_ALLOC | AIRQ_IV_PTR);
  197. if (!info->aiv) {
  198. kfree(info);
  199. return NULL;
  200. }
  201. info->airq.handler = virtio_airq_handler;
  202. info->airq.lsi_ptr = &info->summary_indicator;
  203. info->airq.lsi_mask = 0xff;
  204. info->airq.isc = VIRTIO_AIRQ_ISC;
  205. rc = register_adapter_interrupt(&info->airq);
  206. if (rc) {
  207. airq_iv_release(info->aiv);
  208. kfree(info);
  209. return NULL;
  210. }
  211. return info;
  212. }
  213. static void destroy_airq_info(struct airq_info *info)
  214. {
  215. if (!info)
  216. return;
  217. unregister_adapter_interrupt(&info->airq);
  218. airq_iv_release(info->aiv);
  219. kfree(info);
  220. }
  221. static unsigned long get_airq_indicator(struct virtqueue *vqs[], int nvqs,
  222. u64 *first, void **airq_info)
  223. {
  224. int i, j;
  225. struct airq_info *info;
  226. unsigned long indicator_addr = 0;
  227. unsigned long bit, flags;
  228. for (i = 0; i < MAX_AIRQ_AREAS && !indicator_addr; i++) {
  229. if (!airq_areas[i])
  230. airq_areas[i] = new_airq_info();
  231. info = airq_areas[i];
  232. if (!info)
  233. return 0;
  234. write_lock_irqsave(&info->lock, flags);
  235. bit = airq_iv_alloc(info->aiv, nvqs);
  236. if (bit == -1UL) {
  237. /* Not enough vacancies. */
  238. write_unlock_irqrestore(&info->lock, flags);
  239. continue;
  240. }
  241. *first = bit;
  242. *airq_info = info;
  243. indicator_addr = (unsigned long)info->aiv->vector;
  244. for (j = 0; j < nvqs; j++) {
  245. airq_iv_set_ptr(info->aiv, bit + j,
  246. (unsigned long)vqs[j]);
  247. }
  248. write_unlock_irqrestore(&info->lock, flags);
  249. }
  250. return indicator_addr;
  251. }
  252. static void virtio_ccw_drop_indicators(struct virtio_ccw_device *vcdev)
  253. {
  254. struct virtio_ccw_vq_info *info;
  255. if (!vcdev->airq_info)
  256. return;
  257. list_for_each_entry(info, &vcdev->virtqueues, node)
  258. drop_airq_indicator(info->vq, vcdev->airq_info);
  259. }
  260. static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag)
  261. {
  262. unsigned long flags;
  263. __u32 ret;
  264. spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
  265. if (vcdev->err)
  266. ret = 0;
  267. else
  268. ret = vcdev->curr_io & flag;
  269. spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
  270. return ret;
  271. }
  272. static int ccw_io_helper(struct virtio_ccw_device *vcdev,
  273. struct ccw1 *ccw, __u32 intparm)
  274. {
  275. int ret;
  276. unsigned long flags;
  277. int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
  278. mutex_lock(&vcdev->io_lock);
  279. do {
  280. spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
  281. ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
  282. if (!ret) {
  283. if (!vcdev->curr_io)
  284. vcdev->err = 0;
  285. vcdev->curr_io |= flag;
  286. }
  287. spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
  288. cpu_relax();
  289. } while (ret == -EBUSY);
  290. wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
  291. ret = ret ? ret : vcdev->err;
  292. mutex_unlock(&vcdev->io_lock);
  293. return ret;
  294. }
  295. static void virtio_ccw_drop_indicator(struct virtio_ccw_device *vcdev,
  296. struct ccw1 *ccw)
  297. {
  298. int ret;
  299. unsigned long *indicatorp = NULL;
  300. struct virtio_thinint_area *thinint_area = NULL;
  301. struct airq_info *airq_info = vcdev->airq_info;
  302. if (vcdev->is_thinint) {
  303. thinint_area = kzalloc(sizeof(*thinint_area),
  304. GFP_DMA | GFP_KERNEL);
  305. if (!thinint_area)
  306. return;
  307. thinint_area->summary_indicator =
  308. (unsigned long) &airq_info->summary_indicator;
  309. thinint_area->isc = VIRTIO_AIRQ_ISC;
  310. ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
  311. ccw->count = sizeof(*thinint_area);
  312. ccw->cda = (__u32)(unsigned long) thinint_area;
  313. } else {
  314. indicatorp = kmalloc(sizeof(&vcdev->indicators),
  315. GFP_DMA | GFP_KERNEL);
  316. if (!indicatorp)
  317. return;
  318. *indicatorp = 0;
  319. ccw->cmd_code = CCW_CMD_SET_IND;
  320. ccw->count = sizeof(vcdev->indicators);
  321. ccw->cda = (__u32)(unsigned long) indicatorp;
  322. }
  323. /* Deregister indicators from host. */
  324. vcdev->indicators = 0;
  325. ccw->flags = 0;
  326. ret = ccw_io_helper(vcdev, ccw,
  327. vcdev->is_thinint ?
  328. VIRTIO_CCW_DOING_SET_IND_ADAPTER :
  329. VIRTIO_CCW_DOING_SET_IND);
  330. if (ret && (ret != -ENODEV))
  331. dev_info(&vcdev->cdev->dev,
  332. "Failed to deregister indicators (%d)\n", ret);
  333. else if (vcdev->is_thinint)
  334. virtio_ccw_drop_indicators(vcdev);
  335. kfree(indicatorp);
  336. kfree(thinint_area);
  337. }
  338. static inline long __do_kvm_notify(struct subchannel_id schid,
  339. unsigned long queue_index,
  340. long cookie)
  341. {
  342. register unsigned long __nr asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY;
  343. register struct subchannel_id __schid asm("2") = schid;
  344. register unsigned long __index asm("3") = queue_index;
  345. register long __rc asm("2");
  346. register long __cookie asm("4") = cookie;
  347. asm volatile ("diag 2,4,0x500\n"
  348. : "=d" (__rc) : "d" (__nr), "d" (__schid), "d" (__index),
  349. "d"(__cookie)
  350. : "memory", "cc");
  351. return __rc;
  352. }
  353. static inline long do_kvm_notify(struct subchannel_id schid,
  354. unsigned long queue_index,
  355. long cookie)
  356. {
  357. diag_stat_inc(DIAG_STAT_X500);
  358. return __do_kvm_notify(schid, queue_index, cookie);
  359. }
  360. static bool virtio_ccw_kvm_notify(struct virtqueue *vq)
  361. {
  362. struct virtio_ccw_vq_info *info = vq->priv;
  363. struct virtio_ccw_device *vcdev;
  364. struct subchannel_id schid;
  365. vcdev = to_vc_device(info->vq->vdev);
  366. ccw_device_get_schid(vcdev->cdev, &schid);
  367. info->cookie = do_kvm_notify(schid, vq->index, info->cookie);
  368. if (info->cookie < 0)
  369. return false;
  370. return true;
  371. }
  372. static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
  373. struct ccw1 *ccw, int index)
  374. {
  375. int ret;
  376. vcdev->config_block->index = index;
  377. ccw->cmd_code = CCW_CMD_READ_VQ_CONF;
  378. ccw->flags = 0;
  379. ccw->count = sizeof(struct vq_config_block);
  380. ccw->cda = (__u32)(unsigned long)(vcdev->config_block);
  381. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF);
  382. if (ret)
  383. return ret;
  384. return vcdev->config_block->num ?: -ENOENT;
  385. }
  386. static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
  387. {
  388. struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev);
  389. struct virtio_ccw_vq_info *info = vq->priv;
  390. unsigned long flags;
  391. unsigned long size;
  392. int ret;
  393. unsigned int index = vq->index;
  394. /* Remove from our list. */
  395. spin_lock_irqsave(&vcdev->lock, flags);
  396. list_del(&info->node);
  397. spin_unlock_irqrestore(&vcdev->lock, flags);
  398. /* Release from host. */
  399. if (vcdev->revision == 0) {
  400. info->info_block->l.queue = 0;
  401. info->info_block->l.align = 0;
  402. info->info_block->l.index = index;
  403. info->info_block->l.num = 0;
  404. ccw->count = sizeof(info->info_block->l);
  405. } else {
  406. info->info_block->s.desc = 0;
  407. info->info_block->s.index = index;
  408. info->info_block->s.num = 0;
  409. info->info_block->s.avail = 0;
  410. info->info_block->s.used = 0;
  411. ccw->count = sizeof(info->info_block->s);
  412. }
  413. ccw->cmd_code = CCW_CMD_SET_VQ;
  414. ccw->flags = 0;
  415. ccw->cda = (__u32)(unsigned long)(info->info_block);
  416. ret = ccw_io_helper(vcdev, ccw,
  417. VIRTIO_CCW_DOING_SET_VQ | index);
  418. /*
  419. * -ENODEV isn't considered an error: The device is gone anyway.
  420. * This may happen on device detach.
  421. */
  422. if (ret && (ret != -ENODEV))
  423. dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d",
  424. ret, index);
  425. vring_del_virtqueue(vq);
  426. size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
  427. free_pages_exact(info->queue, size);
  428. kfree(info->info_block);
  429. kfree(info);
  430. }
  431. static void virtio_ccw_del_vqs(struct virtio_device *vdev)
  432. {
  433. struct virtqueue *vq, *n;
  434. struct ccw1 *ccw;
  435. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  436. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  437. if (!ccw)
  438. return;
  439. virtio_ccw_drop_indicator(vcdev, ccw);
  440. list_for_each_entry_safe(vq, n, &vdev->vqs, list)
  441. virtio_ccw_del_vq(vq, ccw);
  442. kfree(ccw);
  443. }
  444. static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
  445. int i, vq_callback_t *callback,
  446. const char *name,
  447. struct ccw1 *ccw)
  448. {
  449. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  450. int err;
  451. struct virtqueue *vq = NULL;
  452. struct virtio_ccw_vq_info *info;
  453. unsigned long size = 0; /* silence the compiler */
  454. unsigned long flags;
  455. /* Allocate queue. */
  456. info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL);
  457. if (!info) {
  458. dev_warn(&vcdev->cdev->dev, "no info\n");
  459. err = -ENOMEM;
  460. goto out_err;
  461. }
  462. info->info_block = kzalloc(sizeof(*info->info_block),
  463. GFP_DMA | GFP_KERNEL);
  464. if (!info->info_block) {
  465. dev_warn(&vcdev->cdev->dev, "no info block\n");
  466. err = -ENOMEM;
  467. goto out_err;
  468. }
  469. info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i);
  470. if (info->num < 0) {
  471. err = info->num;
  472. goto out_err;
  473. }
  474. size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
  475. info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
  476. if (info->queue == NULL) {
  477. dev_warn(&vcdev->cdev->dev, "no queue\n");
  478. err = -ENOMEM;
  479. goto out_err;
  480. }
  481. vq = vring_new_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN, vdev,
  482. true, info->queue, virtio_ccw_kvm_notify,
  483. callback, name);
  484. if (!vq) {
  485. /* For now, we fail if we can't get the requested size. */
  486. dev_warn(&vcdev->cdev->dev, "no vq\n");
  487. err = -ENOMEM;
  488. goto out_err;
  489. }
  490. /* Register it with the host. */
  491. if (vcdev->revision == 0) {
  492. info->info_block->l.queue = (__u64)info->queue;
  493. info->info_block->l.align = KVM_VIRTIO_CCW_RING_ALIGN;
  494. info->info_block->l.index = i;
  495. info->info_block->l.num = info->num;
  496. ccw->count = sizeof(info->info_block->l);
  497. } else {
  498. info->info_block->s.desc = (__u64)info->queue;
  499. info->info_block->s.index = i;
  500. info->info_block->s.num = info->num;
  501. info->info_block->s.avail = (__u64)virtqueue_get_avail(vq);
  502. info->info_block->s.used = (__u64)virtqueue_get_used(vq);
  503. ccw->count = sizeof(info->info_block->s);
  504. }
  505. ccw->cmd_code = CCW_CMD_SET_VQ;
  506. ccw->flags = 0;
  507. ccw->cda = (__u32)(unsigned long)(info->info_block);
  508. err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i);
  509. if (err) {
  510. dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n");
  511. goto out_err;
  512. }
  513. info->vq = vq;
  514. vq->priv = info;
  515. /* Save it to our list. */
  516. spin_lock_irqsave(&vcdev->lock, flags);
  517. list_add(&info->node, &vcdev->virtqueues);
  518. spin_unlock_irqrestore(&vcdev->lock, flags);
  519. return vq;
  520. out_err:
  521. if (vq)
  522. vring_del_virtqueue(vq);
  523. if (info) {
  524. if (info->queue)
  525. free_pages_exact(info->queue, size);
  526. kfree(info->info_block);
  527. }
  528. kfree(info);
  529. return ERR_PTR(err);
  530. }
  531. static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device *vcdev,
  532. struct virtqueue *vqs[], int nvqs,
  533. struct ccw1 *ccw)
  534. {
  535. int ret;
  536. struct virtio_thinint_area *thinint_area = NULL;
  537. struct airq_info *info;
  538. thinint_area = kzalloc(sizeof(*thinint_area), GFP_DMA | GFP_KERNEL);
  539. if (!thinint_area) {
  540. ret = -ENOMEM;
  541. goto out;
  542. }
  543. /* Try to get an indicator. */
  544. thinint_area->indicator = get_airq_indicator(vqs, nvqs,
  545. &thinint_area->bit_nr,
  546. &vcdev->airq_info);
  547. if (!thinint_area->indicator) {
  548. ret = -ENOSPC;
  549. goto out;
  550. }
  551. info = vcdev->airq_info;
  552. thinint_area->summary_indicator =
  553. (unsigned long) &info->summary_indicator;
  554. thinint_area->isc = VIRTIO_AIRQ_ISC;
  555. ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
  556. ccw->flags = CCW_FLAG_SLI;
  557. ccw->count = sizeof(*thinint_area);
  558. ccw->cda = (__u32)(unsigned long)thinint_area;
  559. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND_ADAPTER);
  560. if (ret) {
  561. if (ret == -EOPNOTSUPP) {
  562. /*
  563. * The host does not support adapter interrupts
  564. * for virtio-ccw, stop trying.
  565. */
  566. virtio_ccw_use_airq = 0;
  567. pr_info("Adapter interrupts unsupported on host\n");
  568. } else
  569. dev_warn(&vcdev->cdev->dev,
  570. "enabling adapter interrupts = %d\n", ret);
  571. virtio_ccw_drop_indicators(vcdev);
  572. }
  573. out:
  574. kfree(thinint_area);
  575. return ret;
  576. }
  577. static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  578. struct virtqueue *vqs[],
  579. vq_callback_t *callbacks[],
  580. const char *names[])
  581. {
  582. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  583. unsigned long *indicatorp = NULL;
  584. int ret, i;
  585. struct ccw1 *ccw;
  586. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  587. if (!ccw)
  588. return -ENOMEM;
  589. for (i = 0; i < nvqs; ++i) {
  590. vqs[i] = virtio_ccw_setup_vq(vdev, i, callbacks[i], names[i],
  591. ccw);
  592. if (IS_ERR(vqs[i])) {
  593. ret = PTR_ERR(vqs[i]);
  594. vqs[i] = NULL;
  595. goto out;
  596. }
  597. }
  598. ret = -ENOMEM;
  599. /* We need a data area under 2G to communicate. */
  600. indicatorp = kmalloc(sizeof(&vcdev->indicators), GFP_DMA | GFP_KERNEL);
  601. if (!indicatorp)
  602. goto out;
  603. *indicatorp = (unsigned long) &vcdev->indicators;
  604. if (vcdev->is_thinint) {
  605. ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw);
  606. if (ret)
  607. /* no error, just fall back to legacy interrupts */
  608. vcdev->is_thinint = 0;
  609. }
  610. if (!vcdev->is_thinint) {
  611. /* Register queue indicators with host. */
  612. vcdev->indicators = 0;
  613. ccw->cmd_code = CCW_CMD_SET_IND;
  614. ccw->flags = 0;
  615. ccw->count = sizeof(vcdev->indicators);
  616. ccw->cda = (__u32)(unsigned long) indicatorp;
  617. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
  618. if (ret)
  619. goto out;
  620. }
  621. /* Register indicators2 with host for config changes */
  622. *indicatorp = (unsigned long) &vcdev->indicators2;
  623. vcdev->indicators2 = 0;
  624. ccw->cmd_code = CCW_CMD_SET_CONF_IND;
  625. ccw->flags = 0;
  626. ccw->count = sizeof(vcdev->indicators2);
  627. ccw->cda = (__u32)(unsigned long) indicatorp;
  628. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
  629. if (ret)
  630. goto out;
  631. kfree(indicatorp);
  632. kfree(ccw);
  633. return 0;
  634. out:
  635. kfree(indicatorp);
  636. kfree(ccw);
  637. virtio_ccw_del_vqs(vdev);
  638. return ret;
  639. }
  640. static void virtio_ccw_reset(struct virtio_device *vdev)
  641. {
  642. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  643. struct ccw1 *ccw;
  644. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  645. if (!ccw)
  646. return;
  647. /* Zero status bits. */
  648. *vcdev->status = 0;
  649. /* Send a reset ccw on device. */
  650. ccw->cmd_code = CCW_CMD_VDEV_RESET;
  651. ccw->flags = 0;
  652. ccw->count = 0;
  653. ccw->cda = 0;
  654. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
  655. kfree(ccw);
  656. }
  657. static u64 virtio_ccw_get_features(struct virtio_device *vdev)
  658. {
  659. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  660. struct virtio_feature_desc *features;
  661. int ret;
  662. u64 rc;
  663. struct ccw1 *ccw;
  664. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  665. if (!ccw)
  666. return 0;
  667. features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
  668. if (!features) {
  669. rc = 0;
  670. goto out_free;
  671. }
  672. /* Read the feature bits from the host. */
  673. features->index = 0;
  674. ccw->cmd_code = CCW_CMD_READ_FEAT;
  675. ccw->flags = 0;
  676. ccw->count = sizeof(*features);
  677. ccw->cda = (__u32)(unsigned long)features;
  678. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
  679. if (ret) {
  680. rc = 0;
  681. goto out_free;
  682. }
  683. rc = le32_to_cpu(features->features);
  684. if (vcdev->revision == 0)
  685. goto out_free;
  686. /* Read second half of the feature bits from the host. */
  687. features->index = 1;
  688. ccw->cmd_code = CCW_CMD_READ_FEAT;
  689. ccw->flags = 0;
  690. ccw->count = sizeof(*features);
  691. ccw->cda = (__u32)(unsigned long)features;
  692. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
  693. if (ret == 0)
  694. rc |= (u64)le32_to_cpu(features->features) << 32;
  695. out_free:
  696. kfree(features);
  697. kfree(ccw);
  698. return rc;
  699. }
  700. static int virtio_ccw_finalize_features(struct virtio_device *vdev)
  701. {
  702. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  703. struct virtio_feature_desc *features;
  704. struct ccw1 *ccw;
  705. int ret;
  706. if (vcdev->revision >= 1 &&
  707. !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
  708. dev_err(&vdev->dev, "virtio: device uses revision 1 "
  709. "but does not have VIRTIO_F_VERSION_1\n");
  710. return -EINVAL;
  711. }
  712. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  713. if (!ccw)
  714. return -ENOMEM;
  715. features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
  716. if (!features) {
  717. ret = -ENOMEM;
  718. goto out_free;
  719. }
  720. /* Give virtio_ring a chance to accept features. */
  721. vring_transport_features(vdev);
  722. features->index = 0;
  723. features->features = cpu_to_le32((u32)vdev->features);
  724. /* Write the first half of the feature bits to the host. */
  725. ccw->cmd_code = CCW_CMD_WRITE_FEAT;
  726. ccw->flags = 0;
  727. ccw->count = sizeof(*features);
  728. ccw->cda = (__u32)(unsigned long)features;
  729. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
  730. if (ret)
  731. goto out_free;
  732. if (vcdev->revision == 0)
  733. goto out_free;
  734. features->index = 1;
  735. features->features = cpu_to_le32(vdev->features >> 32);
  736. /* Write the second half of the feature bits to the host. */
  737. ccw->cmd_code = CCW_CMD_WRITE_FEAT;
  738. ccw->flags = 0;
  739. ccw->count = sizeof(*features);
  740. ccw->cda = (__u32)(unsigned long)features;
  741. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
  742. out_free:
  743. kfree(features);
  744. kfree(ccw);
  745. return ret;
  746. }
  747. static void virtio_ccw_get_config(struct virtio_device *vdev,
  748. unsigned int offset, void *buf, unsigned len)
  749. {
  750. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  751. int ret;
  752. struct ccw1 *ccw;
  753. void *config_area;
  754. unsigned long flags;
  755. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  756. if (!ccw)
  757. return;
  758. config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
  759. if (!config_area)
  760. goto out_free;
  761. /* Read the config area from the host. */
  762. ccw->cmd_code = CCW_CMD_READ_CONF;
  763. ccw->flags = 0;
  764. ccw->count = offset + len;
  765. ccw->cda = (__u32)(unsigned long)config_area;
  766. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
  767. if (ret)
  768. goto out_free;
  769. spin_lock_irqsave(&vcdev->lock, flags);
  770. memcpy(vcdev->config, config_area, offset + len);
  771. if (vcdev->config_ready < offset + len)
  772. vcdev->config_ready = offset + len;
  773. spin_unlock_irqrestore(&vcdev->lock, flags);
  774. if (buf)
  775. memcpy(buf, config_area + offset, len);
  776. out_free:
  777. kfree(config_area);
  778. kfree(ccw);
  779. }
  780. static void virtio_ccw_set_config(struct virtio_device *vdev,
  781. unsigned int offset, const void *buf,
  782. unsigned len)
  783. {
  784. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  785. struct ccw1 *ccw;
  786. void *config_area;
  787. unsigned long flags;
  788. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  789. if (!ccw)
  790. return;
  791. config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
  792. if (!config_area)
  793. goto out_free;
  794. /* Make sure we don't overwrite fields. */
  795. if (vcdev->config_ready < offset)
  796. virtio_ccw_get_config(vdev, 0, NULL, offset);
  797. spin_lock_irqsave(&vcdev->lock, flags);
  798. memcpy(&vcdev->config[offset], buf, len);
  799. /* Write the config area to the host. */
  800. memcpy(config_area, vcdev->config, sizeof(vcdev->config));
  801. spin_unlock_irqrestore(&vcdev->lock, flags);
  802. ccw->cmd_code = CCW_CMD_WRITE_CONF;
  803. ccw->flags = 0;
  804. ccw->count = offset + len;
  805. ccw->cda = (__u32)(unsigned long)config_area;
  806. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
  807. out_free:
  808. kfree(config_area);
  809. kfree(ccw);
  810. }
  811. static u8 virtio_ccw_get_status(struct virtio_device *vdev)
  812. {
  813. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  814. return *vcdev->status;
  815. }
  816. static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
  817. {
  818. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  819. u8 old_status = *vcdev->status;
  820. struct ccw1 *ccw;
  821. int ret;
  822. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  823. if (!ccw)
  824. return;
  825. /* Write the status to the host. */
  826. *vcdev->status = status;
  827. ccw->cmd_code = CCW_CMD_WRITE_STATUS;
  828. ccw->flags = 0;
  829. ccw->count = sizeof(status);
  830. ccw->cda = (__u32)(unsigned long)vcdev->status;
  831. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
  832. /* Write failed? We assume status is unchanged. */
  833. if (ret)
  834. *vcdev->status = old_status;
  835. kfree(ccw);
  836. }
  837. static struct virtio_config_ops virtio_ccw_config_ops = {
  838. .get_features = virtio_ccw_get_features,
  839. .finalize_features = virtio_ccw_finalize_features,
  840. .get = virtio_ccw_get_config,
  841. .set = virtio_ccw_set_config,
  842. .get_status = virtio_ccw_get_status,
  843. .set_status = virtio_ccw_set_status,
  844. .reset = virtio_ccw_reset,
  845. .find_vqs = virtio_ccw_find_vqs,
  846. .del_vqs = virtio_ccw_del_vqs,
  847. };
  848. /*
  849. * ccw bus driver related functions
  850. */
  851. static void virtio_ccw_release_dev(struct device *_d)
  852. {
  853. struct virtio_device *dev = container_of(_d, struct virtio_device,
  854. dev);
  855. struct virtio_ccw_device *vcdev = to_vc_device(dev);
  856. kfree(vcdev->status);
  857. kfree(vcdev->config_block);
  858. kfree(vcdev);
  859. }
  860. static int irb_is_error(struct irb *irb)
  861. {
  862. if (scsw_cstat(&irb->scsw) != 0)
  863. return 1;
  864. if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
  865. return 1;
  866. if (scsw_cc(&irb->scsw) != 0)
  867. return 1;
  868. return 0;
  869. }
  870. static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
  871. int index)
  872. {
  873. struct virtio_ccw_vq_info *info;
  874. unsigned long flags;
  875. struct virtqueue *vq;
  876. vq = NULL;
  877. spin_lock_irqsave(&vcdev->lock, flags);
  878. list_for_each_entry(info, &vcdev->virtqueues, node) {
  879. if (info->vq->index == index) {
  880. vq = info->vq;
  881. break;
  882. }
  883. }
  884. spin_unlock_irqrestore(&vcdev->lock, flags);
  885. return vq;
  886. }
  887. static void virtio_ccw_check_activity(struct virtio_ccw_device *vcdev,
  888. __u32 activity)
  889. {
  890. if (vcdev->curr_io & activity) {
  891. switch (activity) {
  892. case VIRTIO_CCW_DOING_READ_FEAT:
  893. case VIRTIO_CCW_DOING_WRITE_FEAT:
  894. case VIRTIO_CCW_DOING_READ_CONFIG:
  895. case VIRTIO_CCW_DOING_WRITE_CONFIG:
  896. case VIRTIO_CCW_DOING_WRITE_STATUS:
  897. case VIRTIO_CCW_DOING_SET_VQ:
  898. case VIRTIO_CCW_DOING_SET_IND:
  899. case VIRTIO_CCW_DOING_SET_CONF_IND:
  900. case VIRTIO_CCW_DOING_RESET:
  901. case VIRTIO_CCW_DOING_READ_VQ_CONF:
  902. case VIRTIO_CCW_DOING_SET_IND_ADAPTER:
  903. case VIRTIO_CCW_DOING_SET_VIRTIO_REV:
  904. vcdev->curr_io &= ~activity;
  905. wake_up(&vcdev->wait_q);
  906. break;
  907. default:
  908. /* don't know what to do... */
  909. dev_warn(&vcdev->cdev->dev,
  910. "Suspicious activity '%08x'\n", activity);
  911. WARN_ON(1);
  912. break;
  913. }
  914. }
  915. }
  916. static void virtio_ccw_int_handler(struct ccw_device *cdev,
  917. unsigned long intparm,
  918. struct irb *irb)
  919. {
  920. __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
  921. struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
  922. int i;
  923. struct virtqueue *vq;
  924. if (!vcdev)
  925. return;
  926. if (IS_ERR(irb)) {
  927. vcdev->err = PTR_ERR(irb);
  928. virtio_ccw_check_activity(vcdev, activity);
  929. /* Don't poke around indicators, something's wrong. */
  930. return;
  931. }
  932. /* Check if it's a notification from the host. */
  933. if ((intparm == 0) &&
  934. (scsw_stctl(&irb->scsw) ==
  935. (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
  936. /* OK */
  937. }
  938. if (irb_is_error(irb)) {
  939. /* Command reject? */
  940. if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
  941. (irb->ecw[0] & SNS0_CMD_REJECT))
  942. vcdev->err = -EOPNOTSUPP;
  943. else
  944. /* Map everything else to -EIO. */
  945. vcdev->err = -EIO;
  946. }
  947. virtio_ccw_check_activity(vcdev, activity);
  948. for_each_set_bit(i, &vcdev->indicators,
  949. sizeof(vcdev->indicators) * BITS_PER_BYTE) {
  950. /* The bit clear must happen before the vring kick. */
  951. clear_bit(i, &vcdev->indicators);
  952. barrier();
  953. vq = virtio_ccw_vq_by_ind(vcdev, i);
  954. vring_interrupt(0, vq);
  955. }
  956. if (test_bit(0, &vcdev->indicators2)) {
  957. virtio_config_changed(&vcdev->vdev);
  958. clear_bit(0, &vcdev->indicators2);
  959. }
  960. }
  961. /*
  962. * We usually want to autoonline all devices, but give the admin
  963. * a way to exempt devices from this.
  964. */
  965. #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
  966. (8*sizeof(long)))
  967. static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
  968. static char *no_auto = "";
  969. module_param(no_auto, charp, 0444);
  970. MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
  971. static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
  972. {
  973. struct ccw_dev_id id;
  974. ccw_device_get_id(cdev, &id);
  975. if (test_bit(id.devno, devs_no_auto[id.ssid]))
  976. return 0;
  977. return 1;
  978. }
  979. static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
  980. {
  981. struct ccw_device *cdev = data;
  982. int ret;
  983. ret = ccw_device_set_online(cdev);
  984. if (ret)
  985. dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
  986. }
  987. static int virtio_ccw_probe(struct ccw_device *cdev)
  988. {
  989. cdev->handler = virtio_ccw_int_handler;
  990. if (virtio_ccw_check_autoonline(cdev))
  991. async_schedule(virtio_ccw_auto_online, cdev);
  992. return 0;
  993. }
  994. static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev)
  995. {
  996. unsigned long flags;
  997. struct virtio_ccw_device *vcdev;
  998. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  999. vcdev = dev_get_drvdata(&cdev->dev);
  1000. if (!vcdev || vcdev->going_away) {
  1001. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1002. return NULL;
  1003. }
  1004. vcdev->going_away = true;
  1005. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1006. return vcdev;
  1007. }
  1008. static void virtio_ccw_remove(struct ccw_device *cdev)
  1009. {
  1010. unsigned long flags;
  1011. struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
  1012. if (vcdev && cdev->online) {
  1013. if (vcdev->device_lost)
  1014. virtio_break_device(&vcdev->vdev);
  1015. unregister_virtio_device(&vcdev->vdev);
  1016. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1017. dev_set_drvdata(&cdev->dev, NULL);
  1018. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1019. }
  1020. cdev->handler = NULL;
  1021. }
  1022. static int virtio_ccw_offline(struct ccw_device *cdev)
  1023. {
  1024. unsigned long flags;
  1025. struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
  1026. if (!vcdev)
  1027. return 0;
  1028. if (vcdev->device_lost)
  1029. virtio_break_device(&vcdev->vdev);
  1030. unregister_virtio_device(&vcdev->vdev);
  1031. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1032. dev_set_drvdata(&cdev->dev, NULL);
  1033. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1034. return 0;
  1035. }
  1036. static int virtio_ccw_set_transport_rev(struct virtio_ccw_device *vcdev)
  1037. {
  1038. struct virtio_rev_info *rev;
  1039. struct ccw1 *ccw;
  1040. int ret;
  1041. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  1042. if (!ccw)
  1043. return -ENOMEM;
  1044. rev = kzalloc(sizeof(*rev), GFP_DMA | GFP_KERNEL);
  1045. if (!rev) {
  1046. kfree(ccw);
  1047. return -ENOMEM;
  1048. }
  1049. /* Set transport revision */
  1050. ccw->cmd_code = CCW_CMD_SET_VIRTIO_REV;
  1051. ccw->flags = 0;
  1052. ccw->count = sizeof(*rev);
  1053. ccw->cda = (__u32)(unsigned long)rev;
  1054. vcdev->revision = VIRTIO_CCW_REV_MAX;
  1055. do {
  1056. rev->revision = vcdev->revision;
  1057. /* none of our supported revisions carry payload */
  1058. rev->length = 0;
  1059. ret = ccw_io_helper(vcdev, ccw,
  1060. VIRTIO_CCW_DOING_SET_VIRTIO_REV);
  1061. if (ret == -EOPNOTSUPP) {
  1062. if (vcdev->revision == 0)
  1063. /*
  1064. * The host device does not support setting
  1065. * the revision: let's operate it in legacy
  1066. * mode.
  1067. */
  1068. ret = 0;
  1069. else
  1070. vcdev->revision--;
  1071. }
  1072. } while (ret == -EOPNOTSUPP);
  1073. kfree(ccw);
  1074. kfree(rev);
  1075. return ret;
  1076. }
  1077. static int virtio_ccw_online(struct ccw_device *cdev)
  1078. {
  1079. int ret;
  1080. struct virtio_ccw_device *vcdev;
  1081. unsigned long flags;
  1082. vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
  1083. if (!vcdev) {
  1084. dev_warn(&cdev->dev, "Could not get memory for virtio\n");
  1085. ret = -ENOMEM;
  1086. goto out_free;
  1087. }
  1088. vcdev->config_block = kzalloc(sizeof(*vcdev->config_block),
  1089. GFP_DMA | GFP_KERNEL);
  1090. if (!vcdev->config_block) {
  1091. ret = -ENOMEM;
  1092. goto out_free;
  1093. }
  1094. vcdev->status = kzalloc(sizeof(*vcdev->status), GFP_DMA | GFP_KERNEL);
  1095. if (!vcdev->status) {
  1096. ret = -ENOMEM;
  1097. goto out_free;
  1098. }
  1099. vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */
  1100. vcdev->vdev.dev.parent = &cdev->dev;
  1101. vcdev->vdev.dev.release = virtio_ccw_release_dev;
  1102. vcdev->vdev.config = &virtio_ccw_config_ops;
  1103. vcdev->cdev = cdev;
  1104. init_waitqueue_head(&vcdev->wait_q);
  1105. INIT_LIST_HEAD(&vcdev->virtqueues);
  1106. spin_lock_init(&vcdev->lock);
  1107. mutex_init(&vcdev->io_lock);
  1108. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1109. dev_set_drvdata(&cdev->dev, vcdev);
  1110. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1111. vcdev->vdev.id.vendor = cdev->id.cu_type;
  1112. vcdev->vdev.id.device = cdev->id.cu_model;
  1113. ret = virtio_ccw_set_transport_rev(vcdev);
  1114. if (ret)
  1115. goto out_free;
  1116. ret = register_virtio_device(&vcdev->vdev);
  1117. if (ret) {
  1118. dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
  1119. ret);
  1120. goto out_put;
  1121. }
  1122. return 0;
  1123. out_put:
  1124. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1125. dev_set_drvdata(&cdev->dev, NULL);
  1126. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1127. put_device(&vcdev->vdev.dev);
  1128. return ret;
  1129. out_free:
  1130. if (vcdev) {
  1131. kfree(vcdev->status);
  1132. kfree(vcdev->config_block);
  1133. }
  1134. kfree(vcdev);
  1135. return ret;
  1136. }
  1137. static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
  1138. {
  1139. int rc;
  1140. struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
  1141. /*
  1142. * Make sure vcdev is set
  1143. * i.e. set_offline/remove callback not already running
  1144. */
  1145. if (!vcdev)
  1146. return NOTIFY_DONE;
  1147. switch (event) {
  1148. case CIO_GONE:
  1149. vcdev->device_lost = true;
  1150. rc = NOTIFY_DONE;
  1151. break;
  1152. default:
  1153. rc = NOTIFY_DONE;
  1154. break;
  1155. }
  1156. return rc;
  1157. }
  1158. static struct ccw_device_id virtio_ids[] = {
  1159. { CCW_DEVICE(0x3832, 0) },
  1160. {},
  1161. };
  1162. MODULE_DEVICE_TABLE(ccw, virtio_ids);
  1163. static struct ccw_driver virtio_ccw_driver = {
  1164. .driver = {
  1165. .owner = THIS_MODULE,
  1166. .name = "virtio_ccw",
  1167. },
  1168. .ids = virtio_ids,
  1169. .probe = virtio_ccw_probe,
  1170. .remove = virtio_ccw_remove,
  1171. .set_offline = virtio_ccw_offline,
  1172. .set_online = virtio_ccw_online,
  1173. .notify = virtio_ccw_cio_notify,
  1174. .int_class = IRQIO_VIR,
  1175. };
  1176. static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
  1177. int max_digit, int max_val)
  1178. {
  1179. int diff;
  1180. diff = 0;
  1181. *val = 0;
  1182. while (diff <= max_digit) {
  1183. int value = hex_to_bin(**cp);
  1184. if (value < 0)
  1185. break;
  1186. *val = *val * 16 + value;
  1187. (*cp)++;
  1188. diff++;
  1189. }
  1190. if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
  1191. return 1;
  1192. return 0;
  1193. }
  1194. static int __init parse_busid(char *str, unsigned int *cssid,
  1195. unsigned int *ssid, unsigned int *devno)
  1196. {
  1197. char *str_work;
  1198. int rc, ret;
  1199. rc = 1;
  1200. if (*str == '\0')
  1201. goto out;
  1202. str_work = str;
  1203. ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
  1204. if (ret || (str_work[0] != '.'))
  1205. goto out;
  1206. str_work++;
  1207. ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
  1208. if (ret || (str_work[0] != '.'))
  1209. goto out;
  1210. str_work++;
  1211. ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
  1212. if (ret || (str_work[0] != '\0'))
  1213. goto out;
  1214. rc = 0;
  1215. out:
  1216. return rc;
  1217. }
  1218. static void __init no_auto_parse(void)
  1219. {
  1220. unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
  1221. char *parm, *str;
  1222. int rc;
  1223. str = no_auto;
  1224. while ((parm = strsep(&str, ","))) {
  1225. rc = parse_busid(strsep(&parm, "-"), &from_cssid,
  1226. &from_ssid, &from);
  1227. if (rc)
  1228. continue;
  1229. if (parm != NULL) {
  1230. rc = parse_busid(parm, &to_cssid,
  1231. &to_ssid, &to);
  1232. if ((from_ssid > to_ssid) ||
  1233. ((from_ssid == to_ssid) && (from > to)))
  1234. rc = -EINVAL;
  1235. } else {
  1236. to_cssid = from_cssid;
  1237. to_ssid = from_ssid;
  1238. to = from;
  1239. }
  1240. if (rc)
  1241. continue;
  1242. while ((from_ssid < to_ssid) ||
  1243. ((from_ssid == to_ssid) && (from <= to))) {
  1244. set_bit(from, devs_no_auto[from_ssid]);
  1245. from++;
  1246. if (from > __MAX_SUBCHANNEL) {
  1247. from_ssid++;
  1248. from = 0;
  1249. }
  1250. }
  1251. }
  1252. }
  1253. static int __init virtio_ccw_init(void)
  1254. {
  1255. /* parse no_auto string before we do anything further */
  1256. no_auto_parse();
  1257. return ccw_driver_register(&virtio_ccw_driver);
  1258. }
  1259. module_init(virtio_ccw_init);
  1260. static void __exit virtio_ccw_exit(void)
  1261. {
  1262. int i;
  1263. ccw_driver_unregister(&virtio_ccw_driver);
  1264. for (i = 0; i < MAX_AIRQ_AREAS; i++)
  1265. destroy_airq_info(airq_areas[i]);
  1266. }
  1267. module_exit(virtio_ccw_exit);