virtio_ccw.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373
  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. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  684. if (!ccw)
  685. return 0;
  686. features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
  687. if (!features)
  688. goto out_free;
  689. /* Give virtio_ring a chance to accept features. */
  690. vring_transport_features(vdev);
  691. features->index = 0;
  692. features->features = cpu_to_le32((u32)vdev->features);
  693. /* Write the first half of the feature bits to the host. */
  694. ccw->cmd_code = CCW_CMD_WRITE_FEAT;
  695. ccw->flags = 0;
  696. ccw->count = sizeof(*features);
  697. ccw->cda = (__u32)(unsigned long)features;
  698. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
  699. if (vcdev->revision == 0)
  700. goto out_free;
  701. features->index = 1;
  702. features->features = cpu_to_le32(vdev->features >> 32);
  703. /* Write the second half of the feature bits to the host. */
  704. ccw->cmd_code = CCW_CMD_WRITE_FEAT;
  705. ccw->flags = 0;
  706. ccw->count = sizeof(*features);
  707. ccw->cda = (__u32)(unsigned long)features;
  708. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
  709. out_free:
  710. kfree(features);
  711. kfree(ccw);
  712. return 0;
  713. }
  714. static void virtio_ccw_get_config(struct virtio_device *vdev,
  715. unsigned int offset, void *buf, unsigned len)
  716. {
  717. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  718. int ret;
  719. struct ccw1 *ccw;
  720. void *config_area;
  721. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  722. if (!ccw)
  723. return;
  724. config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
  725. if (!config_area)
  726. goto out_free;
  727. /* Read the config area from the host. */
  728. ccw->cmd_code = CCW_CMD_READ_CONF;
  729. ccw->flags = 0;
  730. ccw->count = offset + len;
  731. ccw->cda = (__u32)(unsigned long)config_area;
  732. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
  733. if (ret)
  734. goto out_free;
  735. memcpy(vcdev->config, config_area, sizeof(vcdev->config));
  736. memcpy(buf, &vcdev->config[offset], len);
  737. out_free:
  738. kfree(config_area);
  739. kfree(ccw);
  740. }
  741. static void virtio_ccw_set_config(struct virtio_device *vdev,
  742. unsigned int offset, const void *buf,
  743. unsigned len)
  744. {
  745. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  746. struct ccw1 *ccw;
  747. void *config_area;
  748. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  749. if (!ccw)
  750. return;
  751. config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
  752. if (!config_area)
  753. goto out_free;
  754. memcpy(&vcdev->config[offset], buf, len);
  755. /* Write the config area to the host. */
  756. memcpy(config_area, vcdev->config, sizeof(vcdev->config));
  757. ccw->cmd_code = CCW_CMD_WRITE_CONF;
  758. ccw->flags = 0;
  759. ccw->count = offset + len;
  760. ccw->cda = (__u32)(unsigned long)config_area;
  761. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
  762. out_free:
  763. kfree(config_area);
  764. kfree(ccw);
  765. }
  766. static u8 virtio_ccw_get_status(struct virtio_device *vdev)
  767. {
  768. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  769. return *vcdev->status;
  770. }
  771. static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
  772. {
  773. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  774. u8 old_status = *vcdev->status;
  775. struct ccw1 *ccw;
  776. int ret;
  777. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  778. if (!ccw)
  779. return;
  780. /* Write the status to the host. */
  781. *vcdev->status = status;
  782. ccw->cmd_code = CCW_CMD_WRITE_STATUS;
  783. ccw->flags = 0;
  784. ccw->count = sizeof(status);
  785. ccw->cda = (__u32)(unsigned long)vcdev->status;
  786. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
  787. /* Write failed? We assume status is unchanged. */
  788. if (ret)
  789. *vcdev->status = old_status;
  790. kfree(ccw);
  791. }
  792. static struct virtio_config_ops virtio_ccw_config_ops = {
  793. .get_features = virtio_ccw_get_features,
  794. .finalize_features = virtio_ccw_finalize_features,
  795. .get = virtio_ccw_get_config,
  796. .set = virtio_ccw_set_config,
  797. .get_status = virtio_ccw_get_status,
  798. .set_status = virtio_ccw_set_status,
  799. .reset = virtio_ccw_reset,
  800. .find_vqs = virtio_ccw_find_vqs,
  801. .del_vqs = virtio_ccw_del_vqs,
  802. };
  803. /*
  804. * ccw bus driver related functions
  805. */
  806. static void virtio_ccw_release_dev(struct device *_d)
  807. {
  808. struct virtio_device *dev = container_of(_d, struct virtio_device,
  809. dev);
  810. struct virtio_ccw_device *vcdev = to_vc_device(dev);
  811. kfree(vcdev->status);
  812. kfree(vcdev->config_block);
  813. kfree(vcdev);
  814. }
  815. static int irb_is_error(struct irb *irb)
  816. {
  817. if (scsw_cstat(&irb->scsw) != 0)
  818. return 1;
  819. if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
  820. return 1;
  821. if (scsw_cc(&irb->scsw) != 0)
  822. return 1;
  823. return 0;
  824. }
  825. static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
  826. int index)
  827. {
  828. struct virtio_ccw_vq_info *info;
  829. unsigned long flags;
  830. struct virtqueue *vq;
  831. vq = NULL;
  832. spin_lock_irqsave(&vcdev->lock, flags);
  833. list_for_each_entry(info, &vcdev->virtqueues, node) {
  834. if (info->vq->index == index) {
  835. vq = info->vq;
  836. break;
  837. }
  838. }
  839. spin_unlock_irqrestore(&vcdev->lock, flags);
  840. return vq;
  841. }
  842. static void virtio_ccw_int_handler(struct ccw_device *cdev,
  843. unsigned long intparm,
  844. struct irb *irb)
  845. {
  846. __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
  847. struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
  848. int i;
  849. struct virtqueue *vq;
  850. if (!vcdev)
  851. return;
  852. /* Check if it's a notification from the host. */
  853. if ((intparm == 0) &&
  854. (scsw_stctl(&irb->scsw) ==
  855. (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
  856. /* OK */
  857. }
  858. if (irb_is_error(irb)) {
  859. /* Command reject? */
  860. if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
  861. (irb->ecw[0] & SNS0_CMD_REJECT))
  862. vcdev->err = -EOPNOTSUPP;
  863. else
  864. /* Map everything else to -EIO. */
  865. vcdev->err = -EIO;
  866. }
  867. if (vcdev->curr_io & activity) {
  868. switch (activity) {
  869. case VIRTIO_CCW_DOING_READ_FEAT:
  870. case VIRTIO_CCW_DOING_WRITE_FEAT:
  871. case VIRTIO_CCW_DOING_READ_CONFIG:
  872. case VIRTIO_CCW_DOING_WRITE_CONFIG:
  873. case VIRTIO_CCW_DOING_WRITE_STATUS:
  874. case VIRTIO_CCW_DOING_SET_VQ:
  875. case VIRTIO_CCW_DOING_SET_IND:
  876. case VIRTIO_CCW_DOING_SET_CONF_IND:
  877. case VIRTIO_CCW_DOING_RESET:
  878. case VIRTIO_CCW_DOING_READ_VQ_CONF:
  879. case VIRTIO_CCW_DOING_SET_IND_ADAPTER:
  880. case VIRTIO_CCW_DOING_SET_VIRTIO_REV:
  881. vcdev->curr_io &= ~activity;
  882. wake_up(&vcdev->wait_q);
  883. break;
  884. default:
  885. /* don't know what to do... */
  886. dev_warn(&cdev->dev, "Suspicious activity '%08x'\n",
  887. activity);
  888. WARN_ON(1);
  889. break;
  890. }
  891. }
  892. for_each_set_bit(i, &vcdev->indicators,
  893. sizeof(vcdev->indicators) * BITS_PER_BYTE) {
  894. /* The bit clear must happen before the vring kick. */
  895. clear_bit(i, &vcdev->indicators);
  896. barrier();
  897. vq = virtio_ccw_vq_by_ind(vcdev, i);
  898. vring_interrupt(0, vq);
  899. }
  900. if (test_bit(0, &vcdev->indicators2)) {
  901. virtio_config_changed(&vcdev->vdev);
  902. clear_bit(0, &vcdev->indicators2);
  903. }
  904. }
  905. /*
  906. * We usually want to autoonline all devices, but give the admin
  907. * a way to exempt devices from this.
  908. */
  909. #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
  910. (8*sizeof(long)))
  911. static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
  912. static char *no_auto = "";
  913. module_param(no_auto, charp, 0444);
  914. MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
  915. static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
  916. {
  917. struct ccw_dev_id id;
  918. ccw_device_get_id(cdev, &id);
  919. if (test_bit(id.devno, devs_no_auto[id.ssid]))
  920. return 0;
  921. return 1;
  922. }
  923. static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
  924. {
  925. struct ccw_device *cdev = data;
  926. int ret;
  927. ret = ccw_device_set_online(cdev);
  928. if (ret)
  929. dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
  930. }
  931. static int virtio_ccw_probe(struct ccw_device *cdev)
  932. {
  933. cdev->handler = virtio_ccw_int_handler;
  934. if (virtio_ccw_check_autoonline(cdev))
  935. async_schedule(virtio_ccw_auto_online, cdev);
  936. return 0;
  937. }
  938. static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev)
  939. {
  940. unsigned long flags;
  941. struct virtio_ccw_device *vcdev;
  942. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  943. vcdev = dev_get_drvdata(&cdev->dev);
  944. if (!vcdev || vcdev->going_away) {
  945. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  946. return NULL;
  947. }
  948. vcdev->going_away = true;
  949. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  950. return vcdev;
  951. }
  952. static void virtio_ccw_remove(struct ccw_device *cdev)
  953. {
  954. unsigned long flags;
  955. struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
  956. if (vcdev && cdev->online) {
  957. if (vcdev->device_lost)
  958. virtio_break_device(&vcdev->vdev);
  959. unregister_virtio_device(&vcdev->vdev);
  960. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  961. dev_set_drvdata(&cdev->dev, NULL);
  962. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  963. }
  964. cdev->handler = NULL;
  965. }
  966. static int virtio_ccw_offline(struct ccw_device *cdev)
  967. {
  968. unsigned long flags;
  969. struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
  970. if (!vcdev)
  971. return 0;
  972. if (vcdev->device_lost)
  973. virtio_break_device(&vcdev->vdev);
  974. unregister_virtio_device(&vcdev->vdev);
  975. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  976. dev_set_drvdata(&cdev->dev, NULL);
  977. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  978. return 0;
  979. }
  980. static int virtio_ccw_set_transport_rev(struct virtio_ccw_device *vcdev)
  981. {
  982. struct virtio_rev_info *rev;
  983. struct ccw1 *ccw;
  984. int ret;
  985. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  986. if (!ccw)
  987. return -ENOMEM;
  988. rev = kzalloc(sizeof(*rev), GFP_DMA | GFP_KERNEL);
  989. if (!rev) {
  990. kfree(ccw);
  991. return -ENOMEM;
  992. }
  993. /* Set transport revision */
  994. ccw->cmd_code = CCW_CMD_SET_VIRTIO_REV;
  995. ccw->flags = 0;
  996. ccw->count = sizeof(*rev);
  997. ccw->cda = (__u32)(unsigned long)rev;
  998. vcdev->revision = VIRTIO_CCW_REV_MAX;
  999. do {
  1000. rev->revision = vcdev->revision;
  1001. /* none of our supported revisions carry payload */
  1002. rev->length = 0;
  1003. ret = ccw_io_helper(vcdev, ccw,
  1004. VIRTIO_CCW_DOING_SET_VIRTIO_REV);
  1005. if (ret == -EOPNOTSUPP) {
  1006. if (vcdev->revision == 0)
  1007. /*
  1008. * The host device does not support setting
  1009. * the revision: let's operate it in legacy
  1010. * mode.
  1011. */
  1012. ret = 0;
  1013. else
  1014. vcdev->revision--;
  1015. }
  1016. } while (ret == -EOPNOTSUPP);
  1017. kfree(ccw);
  1018. kfree(rev);
  1019. return ret;
  1020. }
  1021. static int virtio_ccw_online(struct ccw_device *cdev)
  1022. {
  1023. int ret;
  1024. struct virtio_ccw_device *vcdev;
  1025. unsigned long flags;
  1026. vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
  1027. if (!vcdev) {
  1028. dev_warn(&cdev->dev, "Could not get memory for virtio\n");
  1029. ret = -ENOMEM;
  1030. goto out_free;
  1031. }
  1032. vcdev->config_block = kzalloc(sizeof(*vcdev->config_block),
  1033. GFP_DMA | GFP_KERNEL);
  1034. if (!vcdev->config_block) {
  1035. ret = -ENOMEM;
  1036. goto out_free;
  1037. }
  1038. vcdev->status = kzalloc(sizeof(*vcdev->status), GFP_DMA | GFP_KERNEL);
  1039. if (!vcdev->status) {
  1040. ret = -ENOMEM;
  1041. goto out_free;
  1042. }
  1043. vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */
  1044. vcdev->vdev.dev.parent = &cdev->dev;
  1045. vcdev->vdev.dev.release = virtio_ccw_release_dev;
  1046. vcdev->vdev.config = &virtio_ccw_config_ops;
  1047. vcdev->cdev = cdev;
  1048. init_waitqueue_head(&vcdev->wait_q);
  1049. INIT_LIST_HEAD(&vcdev->virtqueues);
  1050. spin_lock_init(&vcdev->lock);
  1051. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1052. dev_set_drvdata(&cdev->dev, vcdev);
  1053. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1054. vcdev->vdev.id.vendor = cdev->id.cu_type;
  1055. vcdev->vdev.id.device = cdev->id.cu_model;
  1056. if (virtio_device_is_legacy_only(vcdev->vdev.id)) {
  1057. vcdev->revision = 0;
  1058. } else {
  1059. ret = virtio_ccw_set_transport_rev(vcdev);
  1060. if (ret)
  1061. goto out_free;
  1062. }
  1063. ret = register_virtio_device(&vcdev->vdev);
  1064. if (ret) {
  1065. dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
  1066. ret);
  1067. goto out_put;
  1068. }
  1069. return 0;
  1070. out_put:
  1071. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1072. dev_set_drvdata(&cdev->dev, NULL);
  1073. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1074. put_device(&vcdev->vdev.dev);
  1075. return ret;
  1076. out_free:
  1077. if (vcdev) {
  1078. kfree(vcdev->status);
  1079. kfree(vcdev->config_block);
  1080. }
  1081. kfree(vcdev);
  1082. return ret;
  1083. }
  1084. static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
  1085. {
  1086. int rc;
  1087. struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
  1088. /*
  1089. * Make sure vcdev is set
  1090. * i.e. set_offline/remove callback not already running
  1091. */
  1092. if (!vcdev)
  1093. return NOTIFY_DONE;
  1094. switch (event) {
  1095. case CIO_GONE:
  1096. vcdev->device_lost = true;
  1097. rc = NOTIFY_DONE;
  1098. break;
  1099. default:
  1100. rc = NOTIFY_DONE;
  1101. break;
  1102. }
  1103. return rc;
  1104. }
  1105. static struct ccw_device_id virtio_ids[] = {
  1106. { CCW_DEVICE(0x3832, 0) },
  1107. {},
  1108. };
  1109. MODULE_DEVICE_TABLE(ccw, virtio_ids);
  1110. static struct ccw_driver virtio_ccw_driver = {
  1111. .driver = {
  1112. .owner = THIS_MODULE,
  1113. .name = "virtio_ccw",
  1114. },
  1115. .ids = virtio_ids,
  1116. .probe = virtio_ccw_probe,
  1117. .remove = virtio_ccw_remove,
  1118. .set_offline = virtio_ccw_offline,
  1119. .set_online = virtio_ccw_online,
  1120. .notify = virtio_ccw_cio_notify,
  1121. .int_class = IRQIO_VIR,
  1122. };
  1123. static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
  1124. int max_digit, int max_val)
  1125. {
  1126. int diff;
  1127. diff = 0;
  1128. *val = 0;
  1129. while (diff <= max_digit) {
  1130. int value = hex_to_bin(**cp);
  1131. if (value < 0)
  1132. break;
  1133. *val = *val * 16 + value;
  1134. (*cp)++;
  1135. diff++;
  1136. }
  1137. if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
  1138. return 1;
  1139. return 0;
  1140. }
  1141. static int __init parse_busid(char *str, unsigned int *cssid,
  1142. unsigned int *ssid, unsigned int *devno)
  1143. {
  1144. char *str_work;
  1145. int rc, ret;
  1146. rc = 1;
  1147. if (*str == '\0')
  1148. goto out;
  1149. str_work = str;
  1150. ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
  1151. if (ret || (str_work[0] != '.'))
  1152. goto out;
  1153. str_work++;
  1154. ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
  1155. if (ret || (str_work[0] != '.'))
  1156. goto out;
  1157. str_work++;
  1158. ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
  1159. if (ret || (str_work[0] != '\0'))
  1160. goto out;
  1161. rc = 0;
  1162. out:
  1163. return rc;
  1164. }
  1165. static void __init no_auto_parse(void)
  1166. {
  1167. unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
  1168. char *parm, *str;
  1169. int rc;
  1170. str = no_auto;
  1171. while ((parm = strsep(&str, ","))) {
  1172. rc = parse_busid(strsep(&parm, "-"), &from_cssid,
  1173. &from_ssid, &from);
  1174. if (rc)
  1175. continue;
  1176. if (parm != NULL) {
  1177. rc = parse_busid(parm, &to_cssid,
  1178. &to_ssid, &to);
  1179. if ((from_ssid > to_ssid) ||
  1180. ((from_ssid == to_ssid) && (from > to)))
  1181. rc = -EINVAL;
  1182. } else {
  1183. to_cssid = from_cssid;
  1184. to_ssid = from_ssid;
  1185. to = from;
  1186. }
  1187. if (rc)
  1188. continue;
  1189. while ((from_ssid < to_ssid) ||
  1190. ((from_ssid == to_ssid) && (from <= to))) {
  1191. set_bit(from, devs_no_auto[from_ssid]);
  1192. from++;
  1193. if (from > __MAX_SUBCHANNEL) {
  1194. from_ssid++;
  1195. from = 0;
  1196. }
  1197. }
  1198. }
  1199. }
  1200. static int __init virtio_ccw_init(void)
  1201. {
  1202. /* parse no_auto string before we do anything further */
  1203. no_auto_parse();
  1204. return ccw_driver_register(&virtio_ccw_driver);
  1205. }
  1206. module_init(virtio_ccw_init);
  1207. static void __exit virtio_ccw_exit(void)
  1208. {
  1209. int i;
  1210. ccw_driver_unregister(&virtio_ccw_driver);
  1211. for (i = 0; i < MAX_AIRQ_AREAS; i++)
  1212. destroy_airq_info(airq_areas[i]);
  1213. }
  1214. module_exit(virtio_ccw_exit);