virtio_ccw.c 34 KB

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