virtio_ccw.c 34 KB

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