virtio_blk.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. //#define DEBUG
  2. #include <linux/spinlock.h>
  3. #include <linux/slab.h>
  4. #include <linux/blkdev.h>
  5. #include <linux/hdreg.h>
  6. #include <linux/module.h>
  7. #include <linux/mutex.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/virtio.h>
  10. #include <linux/virtio_blk.h>
  11. #include <linux/scatterlist.h>
  12. #include <linux/string_helpers.h>
  13. #include <scsi/scsi_cmnd.h>
  14. #include <linux/idr.h>
  15. #include <linux/blk-mq.h>
  16. #include <linux/blk-mq-virtio.h>
  17. #include <linux/numa.h>
  18. #define PART_BITS 4
  19. #define VQ_NAME_LEN 16
  20. static int major;
  21. static DEFINE_IDA(vd_index_ida);
  22. static struct workqueue_struct *virtblk_wq;
  23. struct virtio_blk_vq {
  24. struct virtqueue *vq;
  25. spinlock_t lock;
  26. char name[VQ_NAME_LEN];
  27. } ____cacheline_aligned_in_smp;
  28. struct virtio_blk {
  29. /*
  30. * This mutex must be held by anything that may run after
  31. * virtblk_remove() sets vblk->vdev to NULL.
  32. *
  33. * blk-mq, virtqueue processing, and sysfs attribute code paths are
  34. * shut down before vblk->vdev is set to NULL and therefore do not need
  35. * to hold this mutex.
  36. */
  37. struct mutex vdev_mutex;
  38. struct virtio_device *vdev;
  39. /* The disk structure for the kernel. */
  40. struct gendisk *disk;
  41. /* Block layer tags. */
  42. struct blk_mq_tag_set tag_set;
  43. /* Process context for config space updates */
  44. struct work_struct config_work;
  45. /*
  46. * Tracks references from block_device_operations open/release and
  47. * virtio_driver probe/remove so this object can be freed once no
  48. * longer in use.
  49. */
  50. refcount_t refs;
  51. /* What host tells us, plus 2 for header & tailer. */
  52. unsigned int sg_elems;
  53. /* Ida index - used to track minor number allocations. */
  54. int index;
  55. /* num of vqs */
  56. int num_vqs;
  57. struct virtio_blk_vq *vqs;
  58. };
  59. struct virtblk_req {
  60. #ifdef CONFIG_VIRTIO_BLK_SCSI
  61. struct scsi_request sreq; /* for SCSI passthrough, must be first */
  62. u8 sense[SCSI_SENSE_BUFFERSIZE];
  63. struct virtio_scsi_inhdr in_hdr;
  64. #endif
  65. struct virtio_blk_outhdr out_hdr;
  66. u8 status;
  67. struct scatterlist sg[];
  68. };
  69. static inline blk_status_t virtblk_result(struct virtblk_req *vbr)
  70. {
  71. switch (vbr->status) {
  72. case VIRTIO_BLK_S_OK:
  73. return BLK_STS_OK;
  74. case VIRTIO_BLK_S_UNSUPP:
  75. return BLK_STS_NOTSUPP;
  76. default:
  77. return BLK_STS_IOERR;
  78. }
  79. }
  80. /*
  81. * If this is a packet command we need a couple of additional headers. Behind
  82. * the normal outhdr we put a segment with the scsi command block, and before
  83. * the normal inhdr we put the sense data and the inhdr with additional status
  84. * information.
  85. */
  86. #ifdef CONFIG_VIRTIO_BLK_SCSI
  87. static int virtblk_add_req_scsi(struct virtqueue *vq, struct virtblk_req *vbr,
  88. struct scatterlist *data_sg, bool have_data)
  89. {
  90. struct scatterlist hdr, status, cmd, sense, inhdr, *sgs[6];
  91. unsigned int num_out = 0, num_in = 0;
  92. sg_init_one(&hdr, &vbr->out_hdr, sizeof(vbr->out_hdr));
  93. sgs[num_out++] = &hdr;
  94. sg_init_one(&cmd, vbr->sreq.cmd, vbr->sreq.cmd_len);
  95. sgs[num_out++] = &cmd;
  96. if (have_data) {
  97. if (vbr->out_hdr.type & cpu_to_virtio32(vq->vdev, VIRTIO_BLK_T_OUT))
  98. sgs[num_out++] = data_sg;
  99. else
  100. sgs[num_out + num_in++] = data_sg;
  101. }
  102. sg_init_one(&sense, vbr->sense, SCSI_SENSE_BUFFERSIZE);
  103. sgs[num_out + num_in++] = &sense;
  104. sg_init_one(&inhdr, &vbr->in_hdr, sizeof(vbr->in_hdr));
  105. sgs[num_out + num_in++] = &inhdr;
  106. sg_init_one(&status, &vbr->status, sizeof(vbr->status));
  107. sgs[num_out + num_in++] = &status;
  108. return virtqueue_add_sgs(vq, sgs, num_out, num_in, vbr, GFP_ATOMIC);
  109. }
  110. static inline void virtblk_scsi_request_done(struct request *req)
  111. {
  112. struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
  113. struct virtio_blk *vblk = req->q->queuedata;
  114. struct scsi_request *sreq = &vbr->sreq;
  115. sreq->resid_len = virtio32_to_cpu(vblk->vdev, vbr->in_hdr.residual);
  116. sreq->sense_len = virtio32_to_cpu(vblk->vdev, vbr->in_hdr.sense_len);
  117. sreq->result = virtio32_to_cpu(vblk->vdev, vbr->in_hdr.errors);
  118. }
  119. static int virtblk_ioctl(struct block_device *bdev, fmode_t mode,
  120. unsigned int cmd, unsigned long data)
  121. {
  122. struct gendisk *disk = bdev->bd_disk;
  123. struct virtio_blk *vblk = disk->private_data;
  124. /*
  125. * Only allow the generic SCSI ioctls if the host can support it.
  126. */
  127. if (!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_SCSI))
  128. return -ENOTTY;
  129. return scsi_cmd_blk_ioctl(bdev, mode, cmd,
  130. (void __user *)data);
  131. }
  132. #else
  133. static inline int virtblk_add_req_scsi(struct virtqueue *vq,
  134. struct virtblk_req *vbr, struct scatterlist *data_sg,
  135. bool have_data)
  136. {
  137. return -EIO;
  138. }
  139. static inline void virtblk_scsi_request_done(struct request *req)
  140. {
  141. }
  142. #define virtblk_ioctl NULL
  143. #endif /* CONFIG_VIRTIO_BLK_SCSI */
  144. static int virtblk_add_req(struct virtqueue *vq, struct virtblk_req *vbr,
  145. struct scatterlist *data_sg, bool have_data)
  146. {
  147. struct scatterlist hdr, status, *sgs[3];
  148. unsigned int num_out = 0, num_in = 0;
  149. sg_init_one(&hdr, &vbr->out_hdr, sizeof(vbr->out_hdr));
  150. sgs[num_out++] = &hdr;
  151. if (have_data) {
  152. if (vbr->out_hdr.type & cpu_to_virtio32(vq->vdev, VIRTIO_BLK_T_OUT))
  153. sgs[num_out++] = data_sg;
  154. else
  155. sgs[num_out + num_in++] = data_sg;
  156. }
  157. sg_init_one(&status, &vbr->status, sizeof(vbr->status));
  158. sgs[num_out + num_in++] = &status;
  159. return virtqueue_add_sgs(vq, sgs, num_out, num_in, vbr, GFP_ATOMIC);
  160. }
  161. static inline void virtblk_request_done(struct request *req)
  162. {
  163. struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
  164. switch (req_op(req)) {
  165. case REQ_OP_SCSI_IN:
  166. case REQ_OP_SCSI_OUT:
  167. virtblk_scsi_request_done(req);
  168. break;
  169. }
  170. blk_mq_end_request(req, virtblk_result(vbr));
  171. }
  172. static void virtblk_done(struct virtqueue *vq)
  173. {
  174. struct virtio_blk *vblk = vq->vdev->priv;
  175. bool req_done = false;
  176. int qid = vq->index;
  177. struct virtblk_req *vbr;
  178. unsigned long flags;
  179. unsigned int len;
  180. spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
  181. do {
  182. virtqueue_disable_cb(vq);
  183. while ((vbr = virtqueue_get_buf(vblk->vqs[qid].vq, &len)) != NULL) {
  184. struct request *req = blk_mq_rq_from_pdu(vbr);
  185. blk_mq_complete_request(req);
  186. req_done = true;
  187. }
  188. if (unlikely(virtqueue_is_broken(vq)))
  189. break;
  190. } while (!virtqueue_enable_cb(vq));
  191. /* In case queue is stopped waiting for more buffers. */
  192. if (req_done)
  193. blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
  194. spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
  195. }
  196. static blk_status_t virtio_queue_rq(struct blk_mq_hw_ctx *hctx,
  197. const struct blk_mq_queue_data *bd)
  198. {
  199. struct virtio_blk *vblk = hctx->queue->queuedata;
  200. struct request *req = bd->rq;
  201. struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
  202. unsigned long flags;
  203. unsigned int num;
  204. int qid = hctx->queue_num;
  205. int err;
  206. bool notify = false;
  207. u32 type;
  208. BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
  209. switch (req_op(req)) {
  210. case REQ_OP_READ:
  211. case REQ_OP_WRITE:
  212. type = 0;
  213. break;
  214. case REQ_OP_FLUSH:
  215. type = VIRTIO_BLK_T_FLUSH;
  216. break;
  217. case REQ_OP_SCSI_IN:
  218. case REQ_OP_SCSI_OUT:
  219. type = VIRTIO_BLK_T_SCSI_CMD;
  220. break;
  221. case REQ_OP_DRV_IN:
  222. type = VIRTIO_BLK_T_GET_ID;
  223. break;
  224. default:
  225. WARN_ON_ONCE(1);
  226. return BLK_STS_IOERR;
  227. }
  228. vbr->out_hdr.type = cpu_to_virtio32(vblk->vdev, type);
  229. vbr->out_hdr.sector = type ?
  230. 0 : cpu_to_virtio64(vblk->vdev, blk_rq_pos(req));
  231. vbr->out_hdr.ioprio = cpu_to_virtio32(vblk->vdev, req_get_ioprio(req));
  232. blk_mq_start_request(req);
  233. num = blk_rq_map_sg(hctx->queue, req, vbr->sg);
  234. if (num) {
  235. if (rq_data_dir(req) == WRITE)
  236. vbr->out_hdr.type |= cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_OUT);
  237. else
  238. vbr->out_hdr.type |= cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_IN);
  239. }
  240. spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
  241. if (blk_rq_is_scsi(req))
  242. err = virtblk_add_req_scsi(vblk->vqs[qid].vq, vbr, vbr->sg, num);
  243. else
  244. err = virtblk_add_req(vblk->vqs[qid].vq, vbr, vbr->sg, num);
  245. if (err) {
  246. virtqueue_kick(vblk->vqs[qid].vq);
  247. /* Don't stop the queue if -ENOMEM: we may have failed to
  248. * bounce the buffer due to global resource outage.
  249. */
  250. if (err == -ENOSPC)
  251. blk_mq_stop_hw_queue(hctx);
  252. spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
  253. switch (err) {
  254. case -ENOSPC:
  255. return BLK_STS_DEV_RESOURCE;
  256. case -ENOMEM:
  257. return BLK_STS_RESOURCE;
  258. default:
  259. return BLK_STS_IOERR;
  260. }
  261. }
  262. if (bd->last && virtqueue_kick_prepare(vblk->vqs[qid].vq))
  263. notify = true;
  264. spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
  265. if (notify)
  266. virtqueue_notify(vblk->vqs[qid].vq);
  267. return BLK_STS_OK;
  268. }
  269. /* return id (s/n) string for *disk to *id_str
  270. */
  271. static int virtblk_get_id(struct gendisk *disk, char *id_str)
  272. {
  273. struct virtio_blk *vblk = disk->private_data;
  274. struct request_queue *q = vblk->disk->queue;
  275. struct request *req;
  276. int err;
  277. req = blk_get_request(q, REQ_OP_DRV_IN, 0);
  278. if (IS_ERR(req))
  279. return PTR_ERR(req);
  280. err = blk_rq_map_kern(q, req, id_str, VIRTIO_BLK_ID_BYTES, GFP_KERNEL);
  281. if (err)
  282. goto out;
  283. blk_execute_rq(vblk->disk->queue, vblk->disk, req, false);
  284. err = blk_status_to_errno(virtblk_result(blk_mq_rq_to_pdu(req)));
  285. out:
  286. blk_put_request(req);
  287. return err;
  288. }
  289. static void virtblk_get(struct virtio_blk *vblk)
  290. {
  291. refcount_inc(&vblk->refs);
  292. }
  293. static void virtblk_put(struct virtio_blk *vblk)
  294. {
  295. if (refcount_dec_and_test(&vblk->refs)) {
  296. ida_simple_remove(&vd_index_ida, vblk->index);
  297. mutex_destroy(&vblk->vdev_mutex);
  298. kfree(vblk);
  299. }
  300. }
  301. static int virtblk_open(struct block_device *bd, fmode_t mode)
  302. {
  303. struct virtio_blk *vblk = bd->bd_disk->private_data;
  304. int ret = 0;
  305. mutex_lock(&vblk->vdev_mutex);
  306. if (vblk->vdev)
  307. virtblk_get(vblk);
  308. else
  309. ret = -ENXIO;
  310. mutex_unlock(&vblk->vdev_mutex);
  311. return ret;
  312. }
  313. static void virtblk_release(struct gendisk *disk, fmode_t mode)
  314. {
  315. struct virtio_blk *vblk = disk->private_data;
  316. virtblk_put(vblk);
  317. }
  318. /* We provide getgeo only to please some old bootloader/partitioning tools */
  319. static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
  320. {
  321. struct virtio_blk *vblk = bd->bd_disk->private_data;
  322. int ret = 0;
  323. mutex_lock(&vblk->vdev_mutex);
  324. if (!vblk->vdev) {
  325. ret = -ENXIO;
  326. goto out;
  327. }
  328. /* see if the host passed in geometry config */
  329. if (virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_GEOMETRY)) {
  330. virtio_cread(vblk->vdev, struct virtio_blk_config,
  331. geometry.cylinders, &geo->cylinders);
  332. virtio_cread(vblk->vdev, struct virtio_blk_config,
  333. geometry.heads, &geo->heads);
  334. virtio_cread(vblk->vdev, struct virtio_blk_config,
  335. geometry.sectors, &geo->sectors);
  336. } else {
  337. /* some standard values, similar to sd */
  338. geo->heads = 1 << 6;
  339. geo->sectors = 1 << 5;
  340. geo->cylinders = get_capacity(bd->bd_disk) >> 11;
  341. }
  342. out:
  343. mutex_unlock(&vblk->vdev_mutex);
  344. return ret;
  345. }
  346. static const struct block_device_operations virtblk_fops = {
  347. .ioctl = virtblk_ioctl,
  348. .owner = THIS_MODULE,
  349. .open = virtblk_open,
  350. .release = virtblk_release,
  351. .getgeo = virtblk_getgeo,
  352. };
  353. static int index_to_minor(int index)
  354. {
  355. return index << PART_BITS;
  356. }
  357. static int minor_to_index(int minor)
  358. {
  359. return minor >> PART_BITS;
  360. }
  361. static ssize_t serial_show(struct device *dev,
  362. struct device_attribute *attr, char *buf)
  363. {
  364. struct gendisk *disk = dev_to_disk(dev);
  365. int err;
  366. /* sysfs gives us a PAGE_SIZE buffer */
  367. BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
  368. buf[VIRTIO_BLK_ID_BYTES] = '\0';
  369. err = virtblk_get_id(disk, buf);
  370. if (!err)
  371. return strlen(buf);
  372. if (err == -EIO) /* Unsupported? Make it empty. */
  373. return 0;
  374. return err;
  375. }
  376. static DEVICE_ATTR_RO(serial);
  377. /* The queue's logical block size must be set before calling this */
  378. static void virtblk_update_capacity(struct virtio_blk *vblk, bool resize)
  379. {
  380. struct virtio_device *vdev = vblk->vdev;
  381. struct request_queue *q = vblk->disk->queue;
  382. char cap_str_2[10], cap_str_10[10];
  383. unsigned long long nblocks;
  384. u64 capacity;
  385. /* Host must always specify the capacity. */
  386. virtio_cread(vdev, struct virtio_blk_config, capacity, &capacity);
  387. /* If capacity is too big, truncate with warning. */
  388. if ((sector_t)capacity != capacity) {
  389. dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
  390. (unsigned long long)capacity);
  391. capacity = (sector_t)-1;
  392. }
  393. nblocks = DIV_ROUND_UP_ULL(capacity, queue_logical_block_size(q) >> 9);
  394. string_get_size(nblocks, queue_logical_block_size(q),
  395. STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
  396. string_get_size(nblocks, queue_logical_block_size(q),
  397. STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
  398. dev_notice(&vdev->dev,
  399. "[%s] %s%llu %d-byte logical blocks (%s/%s)\n",
  400. vblk->disk->disk_name,
  401. resize ? "new size: " : "",
  402. nblocks,
  403. queue_logical_block_size(q),
  404. cap_str_10,
  405. cap_str_2);
  406. set_capacity(vblk->disk, capacity);
  407. }
  408. static void virtblk_config_changed_work(struct work_struct *work)
  409. {
  410. struct virtio_blk *vblk =
  411. container_of(work, struct virtio_blk, config_work);
  412. char *envp[] = { "RESIZE=1", NULL };
  413. virtblk_update_capacity(vblk, true);
  414. revalidate_disk(vblk->disk);
  415. kobject_uevent_env(&disk_to_dev(vblk->disk)->kobj, KOBJ_CHANGE, envp);
  416. }
  417. static void virtblk_config_changed(struct virtio_device *vdev)
  418. {
  419. struct virtio_blk *vblk = vdev->priv;
  420. queue_work(virtblk_wq, &vblk->config_work);
  421. }
  422. static int init_vq(struct virtio_blk *vblk)
  423. {
  424. int err;
  425. int i;
  426. vq_callback_t **callbacks;
  427. const char **names;
  428. struct virtqueue **vqs;
  429. unsigned short num_vqs;
  430. struct virtio_device *vdev = vblk->vdev;
  431. struct irq_affinity desc = { 0, };
  432. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_MQ,
  433. struct virtio_blk_config, num_queues,
  434. &num_vqs);
  435. if (err)
  436. num_vqs = 1;
  437. num_vqs = min_t(unsigned int, nr_cpu_ids, num_vqs);
  438. vblk->vqs = kmalloc_array(num_vqs, sizeof(*vblk->vqs), GFP_KERNEL);
  439. if (!vblk->vqs)
  440. return -ENOMEM;
  441. names = kmalloc_array(num_vqs, sizeof(*names), GFP_KERNEL);
  442. callbacks = kmalloc_array(num_vqs, sizeof(*callbacks), GFP_KERNEL);
  443. vqs = kmalloc_array(num_vqs, sizeof(*vqs), GFP_KERNEL);
  444. if (!names || !callbacks || !vqs) {
  445. err = -ENOMEM;
  446. goto out;
  447. }
  448. for (i = 0; i < num_vqs; i++) {
  449. callbacks[i] = virtblk_done;
  450. snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req.%d", i);
  451. names[i] = vblk->vqs[i].name;
  452. }
  453. /* Discover virtqueues and write information to configuration. */
  454. err = virtio_find_vqs(vdev, num_vqs, vqs, callbacks, names, &desc);
  455. if (err)
  456. goto out;
  457. for (i = 0; i < num_vqs; i++) {
  458. spin_lock_init(&vblk->vqs[i].lock);
  459. vblk->vqs[i].vq = vqs[i];
  460. }
  461. vblk->num_vqs = num_vqs;
  462. out:
  463. kfree(vqs);
  464. kfree(callbacks);
  465. kfree(names);
  466. if (err)
  467. kfree(vblk->vqs);
  468. return err;
  469. }
  470. /*
  471. * Legacy naming scheme used for virtio devices. We are stuck with it for
  472. * virtio blk but don't ever use it for any new driver.
  473. */
  474. static int virtblk_name_format(char *prefix, int index, char *buf, int buflen)
  475. {
  476. const int base = 'z' - 'a' + 1;
  477. char *begin = buf + strlen(prefix);
  478. char *end = buf + buflen;
  479. char *p;
  480. int unit;
  481. p = end - 1;
  482. *p = '\0';
  483. unit = base;
  484. do {
  485. if (p == begin)
  486. return -EINVAL;
  487. *--p = 'a' + (index % unit);
  488. index = (index / unit) - 1;
  489. } while (index >= 0);
  490. memmove(begin, p, end - p);
  491. memcpy(buf, prefix, strlen(prefix));
  492. return 0;
  493. }
  494. static int virtblk_get_cache_mode(struct virtio_device *vdev)
  495. {
  496. u8 writeback;
  497. int err;
  498. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE,
  499. struct virtio_blk_config, wce,
  500. &writeback);
  501. /*
  502. * If WCE is not configurable and flush is not available,
  503. * assume no writeback cache is in use.
  504. */
  505. if (err)
  506. writeback = virtio_has_feature(vdev, VIRTIO_BLK_F_FLUSH);
  507. return writeback;
  508. }
  509. static void virtblk_update_cache_mode(struct virtio_device *vdev)
  510. {
  511. u8 writeback = virtblk_get_cache_mode(vdev);
  512. struct virtio_blk *vblk = vdev->priv;
  513. blk_queue_write_cache(vblk->disk->queue, writeback, false);
  514. revalidate_disk(vblk->disk);
  515. }
  516. static const char *const virtblk_cache_types[] = {
  517. "write through", "write back"
  518. };
  519. static ssize_t
  520. cache_type_store(struct device *dev, struct device_attribute *attr,
  521. const char *buf, size_t count)
  522. {
  523. struct gendisk *disk = dev_to_disk(dev);
  524. struct virtio_blk *vblk = disk->private_data;
  525. struct virtio_device *vdev = vblk->vdev;
  526. int i;
  527. BUG_ON(!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_CONFIG_WCE));
  528. i = sysfs_match_string(virtblk_cache_types, buf);
  529. if (i < 0)
  530. return i;
  531. virtio_cwrite8(vdev, offsetof(struct virtio_blk_config, wce), i);
  532. virtblk_update_cache_mode(vdev);
  533. return count;
  534. }
  535. static ssize_t
  536. cache_type_show(struct device *dev, struct device_attribute *attr, char *buf)
  537. {
  538. struct gendisk *disk = dev_to_disk(dev);
  539. struct virtio_blk *vblk = disk->private_data;
  540. u8 writeback = virtblk_get_cache_mode(vblk->vdev);
  541. BUG_ON(writeback >= ARRAY_SIZE(virtblk_cache_types));
  542. return snprintf(buf, 40, "%s\n", virtblk_cache_types[writeback]);
  543. }
  544. static DEVICE_ATTR_RW(cache_type);
  545. static struct attribute *virtblk_attrs[] = {
  546. &dev_attr_serial.attr,
  547. &dev_attr_cache_type.attr,
  548. NULL,
  549. };
  550. static umode_t virtblk_attrs_are_visible(struct kobject *kobj,
  551. struct attribute *a, int n)
  552. {
  553. struct device *dev = container_of(kobj, struct device, kobj);
  554. struct gendisk *disk = dev_to_disk(dev);
  555. struct virtio_blk *vblk = disk->private_data;
  556. struct virtio_device *vdev = vblk->vdev;
  557. if (a == &dev_attr_cache_type.attr &&
  558. !virtio_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE))
  559. return S_IRUGO;
  560. return a->mode;
  561. }
  562. static const struct attribute_group virtblk_attr_group = {
  563. .attrs = virtblk_attrs,
  564. .is_visible = virtblk_attrs_are_visible,
  565. };
  566. static const struct attribute_group *virtblk_attr_groups[] = {
  567. &virtblk_attr_group,
  568. NULL,
  569. };
  570. static int virtblk_init_request(struct blk_mq_tag_set *set, struct request *rq,
  571. unsigned int hctx_idx, unsigned int numa_node)
  572. {
  573. struct virtio_blk *vblk = set->driver_data;
  574. struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
  575. #ifdef CONFIG_VIRTIO_BLK_SCSI
  576. vbr->sreq.sense = vbr->sense;
  577. #endif
  578. sg_init_table(vbr->sg, vblk->sg_elems);
  579. return 0;
  580. }
  581. static int virtblk_map_queues(struct blk_mq_tag_set *set)
  582. {
  583. struct virtio_blk *vblk = set->driver_data;
  584. return blk_mq_virtio_map_queues(set, vblk->vdev, 0);
  585. }
  586. #ifdef CONFIG_VIRTIO_BLK_SCSI
  587. static void virtblk_initialize_rq(struct request *req)
  588. {
  589. struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
  590. scsi_req_init(&vbr->sreq);
  591. }
  592. #endif
  593. static const struct blk_mq_ops virtio_mq_ops = {
  594. .queue_rq = virtio_queue_rq,
  595. .complete = virtblk_request_done,
  596. .init_request = virtblk_init_request,
  597. #ifdef CONFIG_VIRTIO_BLK_SCSI
  598. .initialize_rq_fn = virtblk_initialize_rq,
  599. #endif
  600. .map_queues = virtblk_map_queues,
  601. };
  602. static unsigned int virtblk_queue_depth;
  603. module_param_named(queue_depth, virtblk_queue_depth, uint, 0444);
  604. static int virtblk_probe(struct virtio_device *vdev)
  605. {
  606. struct virtio_blk *vblk;
  607. struct request_queue *q;
  608. int err, index;
  609. u32 v, blk_size, sg_elems, opt_io_size;
  610. u16 min_io_size;
  611. u8 physical_block_exp, alignment_offset;
  612. if (!vdev->config->get) {
  613. dev_err(&vdev->dev, "%s failure: config access disabled\n",
  614. __func__);
  615. return -EINVAL;
  616. }
  617. err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS),
  618. GFP_KERNEL);
  619. if (err < 0)
  620. goto out;
  621. index = err;
  622. /* We need to know how many segments before we allocate. */
  623. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SEG_MAX,
  624. struct virtio_blk_config, seg_max,
  625. &sg_elems);
  626. /* We need at least one SG element, whatever they say. */
  627. if (err || !sg_elems)
  628. sg_elems = 1;
  629. /* We need an extra sg elements at head and tail. */
  630. sg_elems += 2;
  631. vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
  632. if (!vblk) {
  633. err = -ENOMEM;
  634. goto out_free_index;
  635. }
  636. /* This reference is dropped in virtblk_remove(). */
  637. refcount_set(&vblk->refs, 1);
  638. mutex_init(&vblk->vdev_mutex);
  639. vblk->vdev = vdev;
  640. vblk->sg_elems = sg_elems;
  641. INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
  642. err = init_vq(vblk);
  643. if (err)
  644. goto out_free_vblk;
  645. /* FIXME: How many partitions? How long is a piece of string? */
  646. vblk->disk = alloc_disk(1 << PART_BITS);
  647. if (!vblk->disk) {
  648. err = -ENOMEM;
  649. goto out_free_vq;
  650. }
  651. /* Default queue sizing is to fill the ring. */
  652. if (!virtblk_queue_depth) {
  653. virtblk_queue_depth = vblk->vqs[0].vq->num_free;
  654. /* ... but without indirect descs, we use 2 descs per req */
  655. if (!virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC))
  656. virtblk_queue_depth /= 2;
  657. }
  658. memset(&vblk->tag_set, 0, sizeof(vblk->tag_set));
  659. vblk->tag_set.ops = &virtio_mq_ops;
  660. vblk->tag_set.queue_depth = virtblk_queue_depth;
  661. vblk->tag_set.numa_node = NUMA_NO_NODE;
  662. vblk->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
  663. vblk->tag_set.cmd_size =
  664. sizeof(struct virtblk_req) +
  665. sizeof(struct scatterlist) * sg_elems;
  666. vblk->tag_set.driver_data = vblk;
  667. vblk->tag_set.nr_hw_queues = vblk->num_vqs;
  668. err = blk_mq_alloc_tag_set(&vblk->tag_set);
  669. if (err)
  670. goto out_put_disk;
  671. q = blk_mq_init_queue(&vblk->tag_set);
  672. if (IS_ERR(q)) {
  673. err = -ENOMEM;
  674. goto out_free_tags;
  675. }
  676. vblk->disk->queue = q;
  677. q->queuedata = vblk;
  678. virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
  679. vblk->disk->major = major;
  680. vblk->disk->first_minor = index_to_minor(index);
  681. vblk->disk->private_data = vblk;
  682. vblk->disk->fops = &virtblk_fops;
  683. vblk->disk->flags |= GENHD_FL_EXT_DEVT;
  684. vblk->index = index;
  685. /* configure queue flush support */
  686. virtblk_update_cache_mode(vdev);
  687. /* If disk is read-only in the host, the guest should obey */
  688. if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
  689. set_disk_ro(vblk->disk, 1);
  690. /* We can handle whatever the host told us to handle. */
  691. blk_queue_max_segments(q, vblk->sg_elems-2);
  692. /* No real sector limit. */
  693. blk_queue_max_hw_sectors(q, -1U);
  694. /* Host can optionally specify maximum segment size and number of
  695. * segments. */
  696. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SIZE_MAX,
  697. struct virtio_blk_config, size_max, &v);
  698. if (!err)
  699. blk_queue_max_segment_size(q, v);
  700. else
  701. blk_queue_max_segment_size(q, -1U);
  702. /* Host can optionally specify the block size of the device */
  703. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,
  704. struct virtio_blk_config, blk_size,
  705. &blk_size);
  706. if (!err)
  707. blk_queue_logical_block_size(q, blk_size);
  708. else
  709. blk_size = queue_logical_block_size(q);
  710. /* Use topology information if available */
  711. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
  712. struct virtio_blk_config, physical_block_exp,
  713. &physical_block_exp);
  714. if (!err && physical_block_exp)
  715. blk_queue_physical_block_size(q,
  716. blk_size * (1 << physical_block_exp));
  717. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
  718. struct virtio_blk_config, alignment_offset,
  719. &alignment_offset);
  720. if (!err && alignment_offset)
  721. blk_queue_alignment_offset(q, blk_size * alignment_offset);
  722. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
  723. struct virtio_blk_config, min_io_size,
  724. &min_io_size);
  725. if (!err && min_io_size)
  726. blk_queue_io_min(q, blk_size * min_io_size);
  727. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
  728. struct virtio_blk_config, opt_io_size,
  729. &opt_io_size);
  730. if (!err && opt_io_size)
  731. blk_queue_io_opt(q, blk_size * opt_io_size);
  732. virtblk_update_capacity(vblk, false);
  733. virtio_device_ready(vdev);
  734. device_add_disk(&vdev->dev, vblk->disk, virtblk_attr_groups);
  735. return 0;
  736. out_free_tags:
  737. blk_mq_free_tag_set(&vblk->tag_set);
  738. out_put_disk:
  739. put_disk(vblk->disk);
  740. out_free_vq:
  741. vdev->config->del_vqs(vdev);
  742. kfree(vblk->vqs);
  743. out_free_vblk:
  744. kfree(vblk);
  745. out_free_index:
  746. ida_simple_remove(&vd_index_ida, index);
  747. out:
  748. return err;
  749. }
  750. static void virtblk_remove(struct virtio_device *vdev)
  751. {
  752. struct virtio_blk *vblk = vdev->priv;
  753. /* Make sure no work handler is accessing the device. */
  754. flush_work(&vblk->config_work);
  755. del_gendisk(vblk->disk);
  756. blk_cleanup_queue(vblk->disk->queue);
  757. blk_mq_free_tag_set(&vblk->tag_set);
  758. mutex_lock(&vblk->vdev_mutex);
  759. /* Stop all the virtqueues. */
  760. vdev->config->reset(vdev);
  761. /* Virtqueues are stopped, nothing can use vblk->vdev anymore. */
  762. vblk->vdev = NULL;
  763. put_disk(vblk->disk);
  764. vdev->config->del_vqs(vdev);
  765. kfree(vblk->vqs);
  766. mutex_unlock(&vblk->vdev_mutex);
  767. virtblk_put(vblk);
  768. }
  769. #ifdef CONFIG_PM_SLEEP
  770. static int virtblk_freeze(struct virtio_device *vdev)
  771. {
  772. struct virtio_blk *vblk = vdev->priv;
  773. /* Ensure we don't receive any more interrupts */
  774. vdev->config->reset(vdev);
  775. /* Make sure no work handler is accessing the device. */
  776. flush_work(&vblk->config_work);
  777. blk_mq_quiesce_queue(vblk->disk->queue);
  778. vdev->config->del_vqs(vdev);
  779. return 0;
  780. }
  781. static int virtblk_restore(struct virtio_device *vdev)
  782. {
  783. struct virtio_blk *vblk = vdev->priv;
  784. int ret;
  785. ret = init_vq(vdev->priv);
  786. if (ret)
  787. return ret;
  788. virtio_device_ready(vdev);
  789. blk_mq_unquiesce_queue(vblk->disk->queue);
  790. return 0;
  791. }
  792. #endif
  793. static const struct virtio_device_id id_table[] = {
  794. { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
  795. { 0 },
  796. };
  797. static unsigned int features_legacy[] = {
  798. VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
  799. VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
  800. #ifdef CONFIG_VIRTIO_BLK_SCSI
  801. VIRTIO_BLK_F_SCSI,
  802. #endif
  803. VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
  804. VIRTIO_BLK_F_MQ,
  805. }
  806. ;
  807. static unsigned int features[] = {
  808. VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
  809. VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
  810. VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
  811. VIRTIO_BLK_F_MQ,
  812. };
  813. static struct virtio_driver virtio_blk = {
  814. .feature_table = features,
  815. .feature_table_size = ARRAY_SIZE(features),
  816. .feature_table_legacy = features_legacy,
  817. .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
  818. .driver.name = KBUILD_MODNAME,
  819. .driver.owner = THIS_MODULE,
  820. .id_table = id_table,
  821. .probe = virtblk_probe,
  822. .remove = virtblk_remove,
  823. .config_changed = virtblk_config_changed,
  824. #ifdef CONFIG_PM_SLEEP
  825. .freeze = virtblk_freeze,
  826. .restore = virtblk_restore,
  827. #endif
  828. };
  829. static int __init init(void)
  830. {
  831. int error;
  832. virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
  833. if (!virtblk_wq)
  834. return -ENOMEM;
  835. major = register_blkdev(0, "virtblk");
  836. if (major < 0) {
  837. error = major;
  838. goto out_destroy_workqueue;
  839. }
  840. error = register_virtio_driver(&virtio_blk);
  841. if (error)
  842. goto out_unregister_blkdev;
  843. return 0;
  844. out_unregister_blkdev:
  845. unregister_blkdev(major, "virtblk");
  846. out_destroy_workqueue:
  847. destroy_workqueue(virtblk_wq);
  848. return error;
  849. }
  850. static void __exit fini(void)
  851. {
  852. unregister_virtio_driver(&virtio_blk);
  853. unregister_blkdev(major, "virtblk");
  854. destroy_workqueue(virtblk_wq);
  855. }
  856. module_init(init);
  857. module_exit(fini);
  858. MODULE_DEVICE_TABLE(virtio, id_table);
  859. MODULE_DESCRIPTION("Virtio block driver");
  860. MODULE_LICENSE("GPL");