sunvdc.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. /* sunvdc.c: Sun LDOM Virtual Disk Client.
  2. *
  3. * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
  4. */
  5. #include <linux/module.h>
  6. #include <linux/kernel.h>
  7. #include <linux/types.h>
  8. #include <linux/blkdev.h>
  9. #include <linux/hdreg.h>
  10. #include <linux/genhd.h>
  11. #include <linux/cdrom.h>
  12. #include <linux/slab.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/completion.h>
  15. #include <linux/delay.h>
  16. #include <linux/init.h>
  17. #include <linux/list.h>
  18. #include <linux/scatterlist.h>
  19. #include <asm/vio.h>
  20. #include <asm/ldc.h>
  21. #define DRV_MODULE_NAME "sunvdc"
  22. #define PFX DRV_MODULE_NAME ": "
  23. #define DRV_MODULE_VERSION "1.2"
  24. #define DRV_MODULE_RELDATE "November 24, 2014"
  25. static char version[] =
  26. DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
  27. MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
  28. MODULE_DESCRIPTION("Sun LDOM virtual disk client driver");
  29. MODULE_LICENSE("GPL");
  30. MODULE_VERSION(DRV_MODULE_VERSION);
  31. #define VDC_TX_RING_SIZE 512
  32. #define VDC_DEFAULT_BLK_SIZE 512
  33. #define WAITING_FOR_LINK_UP 0x01
  34. #define WAITING_FOR_TX_SPACE 0x02
  35. #define WAITING_FOR_GEN_CMD 0x04
  36. #define WAITING_FOR_ANY -1
  37. static struct workqueue_struct *sunvdc_wq;
  38. struct vdc_req_entry {
  39. struct request *req;
  40. };
  41. struct vdc_port {
  42. struct vio_driver_state vio;
  43. struct gendisk *disk;
  44. struct vdc_completion *cmp;
  45. u64 req_id;
  46. u64 seq;
  47. struct vdc_req_entry rq_arr[VDC_TX_RING_SIZE];
  48. unsigned long ring_cookies;
  49. u64 max_xfer_size;
  50. u32 vdisk_block_size;
  51. u64 ldc_timeout;
  52. struct timer_list ldc_reset_timer;
  53. struct work_struct ldc_reset_work;
  54. /* The server fills these in for us in the disk attribute
  55. * ACK packet.
  56. */
  57. u64 operations;
  58. u32 vdisk_size;
  59. u8 vdisk_type;
  60. u8 vdisk_mtype;
  61. u32 vdisk_phys_blksz;
  62. char disk_name[32];
  63. };
  64. static void vdc_ldc_reset(struct vdc_port *port);
  65. static void vdc_ldc_reset_work(struct work_struct *work);
  66. static void vdc_ldc_reset_timer(unsigned long _arg);
  67. static inline struct vdc_port *to_vdc_port(struct vio_driver_state *vio)
  68. {
  69. return container_of(vio, struct vdc_port, vio);
  70. }
  71. /* Ordered from largest major to lowest */
  72. static struct vio_version vdc_versions[] = {
  73. { .major = 1, .minor = 2 },
  74. { .major = 1, .minor = 1 },
  75. { .major = 1, .minor = 0 },
  76. };
  77. static inline int vdc_version_supported(struct vdc_port *port,
  78. u16 major, u16 minor)
  79. {
  80. return port->vio.ver.major == major && port->vio.ver.minor >= minor;
  81. }
  82. #define VDCBLK_NAME "vdisk"
  83. static int vdc_major;
  84. #define PARTITION_SHIFT 3
  85. static inline u32 vdc_tx_dring_avail(struct vio_dring_state *dr)
  86. {
  87. return vio_dring_avail(dr, VDC_TX_RING_SIZE);
  88. }
  89. static int vdc_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  90. {
  91. struct gendisk *disk = bdev->bd_disk;
  92. sector_t nsect = get_capacity(disk);
  93. sector_t cylinders = nsect;
  94. geo->heads = 0xff;
  95. geo->sectors = 0x3f;
  96. sector_div(cylinders, geo->heads * geo->sectors);
  97. geo->cylinders = cylinders;
  98. if ((sector_t)(geo->cylinders + 1) * geo->heads * geo->sectors < nsect)
  99. geo->cylinders = 0xffff;
  100. return 0;
  101. }
  102. /* Add ioctl/CDROM_GET_CAPABILITY to support cdrom_id in udev
  103. * when vdisk_mtype is VD_MEDIA_TYPE_CD or VD_MEDIA_TYPE_DVD.
  104. * Needed to be able to install inside an ldom from an iso image.
  105. */
  106. static int vdc_ioctl(struct block_device *bdev, fmode_t mode,
  107. unsigned command, unsigned long argument)
  108. {
  109. int i;
  110. struct gendisk *disk;
  111. switch (command) {
  112. case CDROMMULTISESSION:
  113. pr_debug(PFX "Multisession CDs not supported\n");
  114. for (i = 0; i < sizeof(struct cdrom_multisession); i++)
  115. if (put_user(0, (char __user *)(argument + i)))
  116. return -EFAULT;
  117. return 0;
  118. case CDROM_GET_CAPABILITY:
  119. disk = bdev->bd_disk;
  120. if (bdev->bd_disk && (disk->flags & GENHD_FL_CD))
  121. return 0;
  122. return -EINVAL;
  123. default:
  124. pr_debug(PFX "ioctl %08x not supported\n", command);
  125. return -EINVAL;
  126. }
  127. }
  128. static const struct block_device_operations vdc_fops = {
  129. .owner = THIS_MODULE,
  130. .getgeo = vdc_getgeo,
  131. .ioctl = vdc_ioctl,
  132. };
  133. static void vdc_blk_queue_start(struct vdc_port *port)
  134. {
  135. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  136. /* restart blk queue when ring is half emptied. also called after
  137. * handshake completes, so check for initial handshake before we've
  138. * allocated a disk.
  139. */
  140. if (port->disk && blk_queue_stopped(port->disk->queue) &&
  141. vdc_tx_dring_avail(dr) * 100 / VDC_TX_RING_SIZE >= 50) {
  142. blk_start_queue(port->disk->queue);
  143. }
  144. }
  145. static void vdc_finish(struct vio_driver_state *vio, int err, int waiting_for)
  146. {
  147. if (vio->cmp &&
  148. (waiting_for == -1 ||
  149. vio->cmp->waiting_for == waiting_for)) {
  150. vio->cmp->err = err;
  151. complete(&vio->cmp->com);
  152. vio->cmp = NULL;
  153. }
  154. }
  155. static void vdc_handshake_complete(struct vio_driver_state *vio)
  156. {
  157. struct vdc_port *port = to_vdc_port(vio);
  158. del_timer(&port->ldc_reset_timer);
  159. vdc_finish(vio, 0, WAITING_FOR_LINK_UP);
  160. vdc_blk_queue_start(port);
  161. }
  162. static int vdc_handle_unknown(struct vdc_port *port, void *arg)
  163. {
  164. struct vio_msg_tag *pkt = arg;
  165. printk(KERN_ERR PFX "Received unknown msg [%02x:%02x:%04x:%08x]\n",
  166. pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
  167. printk(KERN_ERR PFX "Resetting connection.\n");
  168. ldc_disconnect(port->vio.lp);
  169. return -ECONNRESET;
  170. }
  171. static int vdc_send_attr(struct vio_driver_state *vio)
  172. {
  173. struct vdc_port *port = to_vdc_port(vio);
  174. struct vio_disk_attr_info pkt;
  175. memset(&pkt, 0, sizeof(pkt));
  176. pkt.tag.type = VIO_TYPE_CTRL;
  177. pkt.tag.stype = VIO_SUBTYPE_INFO;
  178. pkt.tag.stype_env = VIO_ATTR_INFO;
  179. pkt.tag.sid = vio_send_sid(vio);
  180. pkt.xfer_mode = VIO_DRING_MODE;
  181. pkt.vdisk_block_size = port->vdisk_block_size;
  182. pkt.max_xfer_size = port->max_xfer_size;
  183. viodbg(HS, "SEND ATTR xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
  184. pkt.xfer_mode, pkt.vdisk_block_size, pkt.max_xfer_size);
  185. return vio_ldc_send(&port->vio, &pkt, sizeof(pkt));
  186. }
  187. static int vdc_handle_attr(struct vio_driver_state *vio, void *arg)
  188. {
  189. struct vdc_port *port = to_vdc_port(vio);
  190. struct vio_disk_attr_info *pkt = arg;
  191. viodbg(HS, "GOT ATTR stype[0x%x] ops[%llx] disk_size[%llu] disk_type[%x] "
  192. "mtype[0x%x] xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
  193. pkt->tag.stype, pkt->operations,
  194. pkt->vdisk_size, pkt->vdisk_type, pkt->vdisk_mtype,
  195. pkt->xfer_mode, pkt->vdisk_block_size,
  196. pkt->max_xfer_size);
  197. if (pkt->tag.stype == VIO_SUBTYPE_ACK) {
  198. switch (pkt->vdisk_type) {
  199. case VD_DISK_TYPE_DISK:
  200. case VD_DISK_TYPE_SLICE:
  201. break;
  202. default:
  203. printk(KERN_ERR PFX "%s: Bogus vdisk_type 0x%x\n",
  204. vio->name, pkt->vdisk_type);
  205. return -ECONNRESET;
  206. }
  207. if (pkt->vdisk_block_size > port->vdisk_block_size) {
  208. printk(KERN_ERR PFX "%s: BLOCK size increased "
  209. "%u --> %u\n",
  210. vio->name,
  211. port->vdisk_block_size, pkt->vdisk_block_size);
  212. return -ECONNRESET;
  213. }
  214. port->operations = pkt->operations;
  215. port->vdisk_type = pkt->vdisk_type;
  216. if (vdc_version_supported(port, 1, 1)) {
  217. port->vdisk_size = pkt->vdisk_size;
  218. port->vdisk_mtype = pkt->vdisk_mtype;
  219. }
  220. if (pkt->max_xfer_size < port->max_xfer_size)
  221. port->max_xfer_size = pkt->max_xfer_size;
  222. port->vdisk_block_size = pkt->vdisk_block_size;
  223. port->vdisk_phys_blksz = VDC_DEFAULT_BLK_SIZE;
  224. if (vdc_version_supported(port, 1, 2))
  225. port->vdisk_phys_blksz = pkt->phys_block_size;
  226. return 0;
  227. } else {
  228. printk(KERN_ERR PFX "%s: Attribute NACK\n", vio->name);
  229. return -ECONNRESET;
  230. }
  231. }
  232. static void vdc_end_special(struct vdc_port *port, struct vio_disk_desc *desc)
  233. {
  234. int err = desc->status;
  235. vdc_finish(&port->vio, -err, WAITING_FOR_GEN_CMD);
  236. }
  237. static void vdc_end_one(struct vdc_port *port, struct vio_dring_state *dr,
  238. unsigned int index)
  239. {
  240. struct vio_disk_desc *desc = vio_dring_entry(dr, index);
  241. struct vdc_req_entry *rqe = &port->rq_arr[index];
  242. struct request *req;
  243. if (unlikely(desc->hdr.state != VIO_DESC_DONE))
  244. return;
  245. ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
  246. desc->hdr.state = VIO_DESC_FREE;
  247. dr->cons = vio_dring_next(dr, index);
  248. req = rqe->req;
  249. if (req == NULL) {
  250. vdc_end_special(port, desc);
  251. return;
  252. }
  253. rqe->req = NULL;
  254. __blk_end_request(req, (desc->status ? BLK_STS_IOERR : 0), desc->size);
  255. vdc_blk_queue_start(port);
  256. }
  257. static int vdc_ack(struct vdc_port *port, void *msgbuf)
  258. {
  259. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  260. struct vio_dring_data *pkt = msgbuf;
  261. if (unlikely(pkt->dring_ident != dr->ident ||
  262. pkt->start_idx != pkt->end_idx ||
  263. pkt->start_idx >= VDC_TX_RING_SIZE))
  264. return 0;
  265. vdc_end_one(port, dr, pkt->start_idx);
  266. return 0;
  267. }
  268. static int vdc_nack(struct vdc_port *port, void *msgbuf)
  269. {
  270. /* XXX Implement me XXX */
  271. return 0;
  272. }
  273. static void vdc_event(void *arg, int event)
  274. {
  275. struct vdc_port *port = arg;
  276. struct vio_driver_state *vio = &port->vio;
  277. unsigned long flags;
  278. int err;
  279. spin_lock_irqsave(&vio->lock, flags);
  280. if (unlikely(event == LDC_EVENT_RESET)) {
  281. vio_link_state_change(vio, event);
  282. queue_work(sunvdc_wq, &port->ldc_reset_work);
  283. goto out;
  284. }
  285. if (unlikely(event == LDC_EVENT_UP)) {
  286. vio_link_state_change(vio, event);
  287. goto out;
  288. }
  289. if (unlikely(event != LDC_EVENT_DATA_READY)) {
  290. pr_warn(PFX "Unexpected LDC event %d\n", event);
  291. goto out;
  292. }
  293. err = 0;
  294. while (1) {
  295. union {
  296. struct vio_msg_tag tag;
  297. u64 raw[8];
  298. } msgbuf;
  299. err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf));
  300. if (unlikely(err < 0)) {
  301. if (err == -ECONNRESET)
  302. vio_conn_reset(vio);
  303. break;
  304. }
  305. if (err == 0)
  306. break;
  307. viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n",
  308. msgbuf.tag.type,
  309. msgbuf.tag.stype,
  310. msgbuf.tag.stype_env,
  311. msgbuf.tag.sid);
  312. err = vio_validate_sid(vio, &msgbuf.tag);
  313. if (err < 0)
  314. break;
  315. if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) {
  316. if (msgbuf.tag.stype == VIO_SUBTYPE_ACK)
  317. err = vdc_ack(port, &msgbuf);
  318. else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK)
  319. err = vdc_nack(port, &msgbuf);
  320. else
  321. err = vdc_handle_unknown(port, &msgbuf);
  322. } else if (msgbuf.tag.type == VIO_TYPE_CTRL) {
  323. err = vio_control_pkt_engine(vio, &msgbuf);
  324. } else {
  325. err = vdc_handle_unknown(port, &msgbuf);
  326. }
  327. if (err < 0)
  328. break;
  329. }
  330. if (err < 0)
  331. vdc_finish(&port->vio, err, WAITING_FOR_ANY);
  332. out:
  333. spin_unlock_irqrestore(&vio->lock, flags);
  334. }
  335. static int __vdc_tx_trigger(struct vdc_port *port)
  336. {
  337. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  338. struct vio_dring_data hdr = {
  339. .tag = {
  340. .type = VIO_TYPE_DATA,
  341. .stype = VIO_SUBTYPE_INFO,
  342. .stype_env = VIO_DRING_DATA,
  343. .sid = vio_send_sid(&port->vio),
  344. },
  345. .dring_ident = dr->ident,
  346. .start_idx = dr->prod,
  347. .end_idx = dr->prod,
  348. };
  349. int err, delay;
  350. hdr.seq = dr->snd_nxt;
  351. delay = 1;
  352. do {
  353. err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
  354. if (err > 0) {
  355. dr->snd_nxt++;
  356. break;
  357. }
  358. udelay(delay);
  359. if ((delay <<= 1) > 128)
  360. delay = 128;
  361. } while (err == -EAGAIN);
  362. if (err == -ENOTCONN)
  363. vdc_ldc_reset(port);
  364. return err;
  365. }
  366. static int __send_request(struct request *req)
  367. {
  368. struct vdc_port *port = req->rq_disk->private_data;
  369. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  370. struct scatterlist sg[port->ring_cookies];
  371. struct vdc_req_entry *rqe;
  372. struct vio_disk_desc *desc;
  373. unsigned int map_perm;
  374. int nsg, err, i;
  375. u64 len;
  376. u8 op;
  377. map_perm = LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
  378. if (rq_data_dir(req) == READ) {
  379. map_perm |= LDC_MAP_W;
  380. op = VD_OP_BREAD;
  381. } else {
  382. map_perm |= LDC_MAP_R;
  383. op = VD_OP_BWRITE;
  384. }
  385. sg_init_table(sg, port->ring_cookies);
  386. nsg = blk_rq_map_sg(req->q, req, sg);
  387. len = 0;
  388. for (i = 0; i < nsg; i++)
  389. len += sg[i].length;
  390. desc = vio_dring_cur(dr);
  391. err = ldc_map_sg(port->vio.lp, sg, nsg,
  392. desc->cookies, port->ring_cookies,
  393. map_perm);
  394. if (err < 0) {
  395. printk(KERN_ERR PFX "ldc_map_sg() failure, err=%d.\n", err);
  396. return err;
  397. }
  398. rqe = &port->rq_arr[dr->prod];
  399. rqe->req = req;
  400. desc->hdr.ack = VIO_ACK_ENABLE;
  401. desc->req_id = port->req_id;
  402. desc->operation = op;
  403. if (port->vdisk_type == VD_DISK_TYPE_DISK) {
  404. desc->slice = 0xff;
  405. } else {
  406. desc->slice = 0;
  407. }
  408. desc->status = ~0;
  409. desc->offset = (blk_rq_pos(req) << 9) / port->vdisk_block_size;
  410. desc->size = len;
  411. desc->ncookies = err;
  412. /* This has to be a non-SMP write barrier because we are writing
  413. * to memory which is shared with the peer LDOM.
  414. */
  415. wmb();
  416. desc->hdr.state = VIO_DESC_READY;
  417. err = __vdc_tx_trigger(port);
  418. if (err < 0) {
  419. printk(KERN_ERR PFX "vdc_tx_trigger() failure, err=%d\n", err);
  420. } else {
  421. port->req_id++;
  422. dr->prod = vio_dring_next(dr, dr->prod);
  423. }
  424. return err;
  425. }
  426. static void do_vdc_request(struct request_queue *rq)
  427. {
  428. struct request *req;
  429. while ((req = blk_peek_request(rq)) != NULL) {
  430. struct vdc_port *port;
  431. struct vio_dring_state *dr;
  432. port = req->rq_disk->private_data;
  433. dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  434. if (unlikely(vdc_tx_dring_avail(dr) < 1))
  435. goto wait;
  436. blk_start_request(req);
  437. if (__send_request(req) < 0) {
  438. blk_requeue_request(rq, req);
  439. wait:
  440. /* Avoid pointless unplugs. */
  441. blk_stop_queue(rq);
  442. break;
  443. }
  444. }
  445. }
  446. static int generic_request(struct vdc_port *port, u8 op, void *buf, int len)
  447. {
  448. struct vio_dring_state *dr;
  449. struct vio_completion comp;
  450. struct vio_disk_desc *desc;
  451. unsigned int map_perm;
  452. unsigned long flags;
  453. int op_len, err;
  454. void *req_buf;
  455. if (!(((u64)1 << (u64)op) & port->operations))
  456. return -EOPNOTSUPP;
  457. switch (op) {
  458. case VD_OP_BREAD:
  459. case VD_OP_BWRITE:
  460. default:
  461. return -EINVAL;
  462. case VD_OP_FLUSH:
  463. op_len = 0;
  464. map_perm = 0;
  465. break;
  466. case VD_OP_GET_WCE:
  467. op_len = sizeof(u32);
  468. map_perm = LDC_MAP_W;
  469. break;
  470. case VD_OP_SET_WCE:
  471. op_len = sizeof(u32);
  472. map_perm = LDC_MAP_R;
  473. break;
  474. case VD_OP_GET_VTOC:
  475. op_len = sizeof(struct vio_disk_vtoc);
  476. map_perm = LDC_MAP_W;
  477. break;
  478. case VD_OP_SET_VTOC:
  479. op_len = sizeof(struct vio_disk_vtoc);
  480. map_perm = LDC_MAP_R;
  481. break;
  482. case VD_OP_GET_DISKGEOM:
  483. op_len = sizeof(struct vio_disk_geom);
  484. map_perm = LDC_MAP_W;
  485. break;
  486. case VD_OP_SET_DISKGEOM:
  487. op_len = sizeof(struct vio_disk_geom);
  488. map_perm = LDC_MAP_R;
  489. break;
  490. case VD_OP_SCSICMD:
  491. op_len = 16;
  492. map_perm = LDC_MAP_RW;
  493. break;
  494. case VD_OP_GET_DEVID:
  495. op_len = sizeof(struct vio_disk_devid);
  496. map_perm = LDC_MAP_W;
  497. break;
  498. case VD_OP_GET_EFI:
  499. case VD_OP_SET_EFI:
  500. return -EOPNOTSUPP;
  501. break;
  502. };
  503. map_perm |= LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
  504. op_len = (op_len + 7) & ~7;
  505. req_buf = kzalloc(op_len, GFP_KERNEL);
  506. if (!req_buf)
  507. return -ENOMEM;
  508. if (len > op_len)
  509. len = op_len;
  510. if (map_perm & LDC_MAP_R)
  511. memcpy(req_buf, buf, len);
  512. spin_lock_irqsave(&port->vio.lock, flags);
  513. dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  514. /* XXX If we want to use this code generically we have to
  515. * XXX handle TX ring exhaustion etc.
  516. */
  517. desc = vio_dring_cur(dr);
  518. err = ldc_map_single(port->vio.lp, req_buf, op_len,
  519. desc->cookies, port->ring_cookies,
  520. map_perm);
  521. if (err < 0) {
  522. spin_unlock_irqrestore(&port->vio.lock, flags);
  523. kfree(req_buf);
  524. return err;
  525. }
  526. init_completion(&comp.com);
  527. comp.waiting_for = WAITING_FOR_GEN_CMD;
  528. port->vio.cmp = &comp;
  529. desc->hdr.ack = VIO_ACK_ENABLE;
  530. desc->req_id = port->req_id;
  531. desc->operation = op;
  532. desc->slice = 0;
  533. desc->status = ~0;
  534. desc->offset = 0;
  535. desc->size = op_len;
  536. desc->ncookies = err;
  537. /* This has to be a non-SMP write barrier because we are writing
  538. * to memory which is shared with the peer LDOM.
  539. */
  540. wmb();
  541. desc->hdr.state = VIO_DESC_READY;
  542. err = __vdc_tx_trigger(port);
  543. if (err >= 0) {
  544. port->req_id++;
  545. dr->prod = vio_dring_next(dr, dr->prod);
  546. spin_unlock_irqrestore(&port->vio.lock, flags);
  547. wait_for_completion(&comp.com);
  548. err = comp.err;
  549. } else {
  550. port->vio.cmp = NULL;
  551. spin_unlock_irqrestore(&port->vio.lock, flags);
  552. }
  553. if (map_perm & LDC_MAP_W)
  554. memcpy(buf, req_buf, len);
  555. kfree(req_buf);
  556. return err;
  557. }
  558. static int vdc_alloc_tx_ring(struct vdc_port *port)
  559. {
  560. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  561. unsigned long len, entry_size;
  562. int ncookies;
  563. void *dring;
  564. entry_size = sizeof(struct vio_disk_desc) +
  565. (sizeof(struct ldc_trans_cookie) * port->ring_cookies);
  566. len = (VDC_TX_RING_SIZE * entry_size);
  567. ncookies = VIO_MAX_RING_COOKIES;
  568. dring = ldc_alloc_exp_dring(port->vio.lp, len,
  569. dr->cookies, &ncookies,
  570. (LDC_MAP_SHADOW |
  571. LDC_MAP_DIRECT |
  572. LDC_MAP_RW));
  573. if (IS_ERR(dring))
  574. return PTR_ERR(dring);
  575. dr->base = dring;
  576. dr->entry_size = entry_size;
  577. dr->num_entries = VDC_TX_RING_SIZE;
  578. dr->prod = dr->cons = 0;
  579. dr->pending = VDC_TX_RING_SIZE;
  580. dr->ncookies = ncookies;
  581. return 0;
  582. }
  583. static void vdc_free_tx_ring(struct vdc_port *port)
  584. {
  585. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  586. if (dr->base) {
  587. ldc_free_exp_dring(port->vio.lp, dr->base,
  588. (dr->entry_size * dr->num_entries),
  589. dr->cookies, dr->ncookies);
  590. dr->base = NULL;
  591. dr->entry_size = 0;
  592. dr->num_entries = 0;
  593. dr->pending = 0;
  594. dr->ncookies = 0;
  595. }
  596. }
  597. static int vdc_port_up(struct vdc_port *port)
  598. {
  599. struct vio_completion comp;
  600. init_completion(&comp.com);
  601. comp.err = 0;
  602. comp.waiting_for = WAITING_FOR_LINK_UP;
  603. port->vio.cmp = &comp;
  604. vio_port_up(&port->vio);
  605. wait_for_completion(&comp.com);
  606. return comp.err;
  607. }
  608. static void vdc_port_down(struct vdc_port *port)
  609. {
  610. ldc_disconnect(port->vio.lp);
  611. ldc_unbind(port->vio.lp);
  612. vdc_free_tx_ring(port);
  613. vio_ldc_free(&port->vio);
  614. }
  615. static int probe_disk(struct vdc_port *port)
  616. {
  617. struct request_queue *q;
  618. struct gendisk *g;
  619. int err;
  620. err = vdc_port_up(port);
  621. if (err)
  622. return err;
  623. /* Using version 1.2 means vdisk_phys_blksz should be set unless the
  624. * disk is reserved by another system.
  625. */
  626. if (vdc_version_supported(port, 1, 2) && !port->vdisk_phys_blksz)
  627. return -ENODEV;
  628. if (vdc_version_supported(port, 1, 1)) {
  629. /* vdisk_size should be set during the handshake, if it wasn't
  630. * then the underlying disk is reserved by another system
  631. */
  632. if (port->vdisk_size == -1)
  633. return -ENODEV;
  634. } else {
  635. struct vio_disk_geom geom;
  636. err = generic_request(port, VD_OP_GET_DISKGEOM,
  637. &geom, sizeof(geom));
  638. if (err < 0) {
  639. printk(KERN_ERR PFX "VD_OP_GET_DISKGEOM returns "
  640. "error %d\n", err);
  641. return err;
  642. }
  643. port->vdisk_size = ((u64)geom.num_cyl *
  644. (u64)geom.num_hd *
  645. (u64)geom.num_sec);
  646. }
  647. q = blk_init_queue(do_vdc_request, &port->vio.lock);
  648. if (!q) {
  649. printk(KERN_ERR PFX "%s: Could not allocate queue.\n",
  650. port->vio.name);
  651. return -ENOMEM;
  652. }
  653. g = alloc_disk(1 << PARTITION_SHIFT);
  654. if (!g) {
  655. printk(KERN_ERR PFX "%s: Could not allocate gendisk.\n",
  656. port->vio.name);
  657. blk_cleanup_queue(q);
  658. return -ENOMEM;
  659. }
  660. port->disk = g;
  661. /* Each segment in a request is up to an aligned page in size. */
  662. blk_queue_segment_boundary(q, PAGE_SIZE - 1);
  663. blk_queue_max_segment_size(q, PAGE_SIZE);
  664. blk_queue_max_segments(q, port->ring_cookies);
  665. blk_queue_max_hw_sectors(q, port->max_xfer_size);
  666. g->major = vdc_major;
  667. g->first_minor = port->vio.vdev->dev_no << PARTITION_SHIFT;
  668. strcpy(g->disk_name, port->disk_name);
  669. g->fops = &vdc_fops;
  670. g->queue = q;
  671. g->private_data = port;
  672. set_capacity(g, port->vdisk_size);
  673. if (vdc_version_supported(port, 1, 1)) {
  674. switch (port->vdisk_mtype) {
  675. case VD_MEDIA_TYPE_CD:
  676. pr_info(PFX "Virtual CDROM %s\n", port->disk_name);
  677. g->flags |= GENHD_FL_CD;
  678. g->flags |= GENHD_FL_REMOVABLE;
  679. set_disk_ro(g, 1);
  680. break;
  681. case VD_MEDIA_TYPE_DVD:
  682. pr_info(PFX "Virtual DVD %s\n", port->disk_name);
  683. g->flags |= GENHD_FL_CD;
  684. g->flags |= GENHD_FL_REMOVABLE;
  685. set_disk_ro(g, 1);
  686. break;
  687. case VD_MEDIA_TYPE_FIXED:
  688. pr_info(PFX "Virtual Hard disk %s\n", port->disk_name);
  689. break;
  690. }
  691. }
  692. blk_queue_physical_block_size(q, port->vdisk_phys_blksz);
  693. pr_info(PFX "%s: %u sectors (%u MB) protocol %d.%d\n",
  694. g->disk_name,
  695. port->vdisk_size, (port->vdisk_size >> (20 - 9)),
  696. port->vio.ver.major, port->vio.ver.minor);
  697. device_add_disk(&port->vio.vdev->dev, g);
  698. return 0;
  699. }
  700. static struct ldc_channel_config vdc_ldc_cfg = {
  701. .event = vdc_event,
  702. .mtu = 64,
  703. .mode = LDC_MODE_UNRELIABLE,
  704. };
  705. static struct vio_driver_ops vdc_vio_ops = {
  706. .send_attr = vdc_send_attr,
  707. .handle_attr = vdc_handle_attr,
  708. .handshake_complete = vdc_handshake_complete,
  709. };
  710. static void print_version(void)
  711. {
  712. static int version_printed;
  713. if (version_printed++ == 0)
  714. printk(KERN_INFO "%s", version);
  715. }
  716. static int vdc_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  717. {
  718. struct mdesc_handle *hp;
  719. struct vdc_port *port;
  720. int err;
  721. const u64 *ldc_timeout;
  722. print_version();
  723. hp = mdesc_grab();
  724. err = -ENODEV;
  725. if ((vdev->dev_no << PARTITION_SHIFT) & ~(u64)MINORMASK) {
  726. printk(KERN_ERR PFX "Port id [%llu] too large.\n",
  727. vdev->dev_no);
  728. goto err_out_release_mdesc;
  729. }
  730. port = kzalloc(sizeof(*port), GFP_KERNEL);
  731. err = -ENOMEM;
  732. if (!port) {
  733. printk(KERN_ERR PFX "Cannot allocate vdc_port.\n");
  734. goto err_out_release_mdesc;
  735. }
  736. if (vdev->dev_no >= 26)
  737. snprintf(port->disk_name, sizeof(port->disk_name),
  738. VDCBLK_NAME "%c%c",
  739. 'a' + ((int)vdev->dev_no / 26) - 1,
  740. 'a' + ((int)vdev->dev_no % 26));
  741. else
  742. snprintf(port->disk_name, sizeof(port->disk_name),
  743. VDCBLK_NAME "%c", 'a' + ((int)vdev->dev_no % 26));
  744. port->vdisk_size = -1;
  745. /* Actual wall time may be double due to do_generic_file_read() doing
  746. * a readahead I/O first, and once that fails it will try to read a
  747. * single page.
  748. */
  749. ldc_timeout = mdesc_get_property(hp, vdev->mp, "vdc-timeout", NULL);
  750. port->ldc_timeout = ldc_timeout ? *ldc_timeout : 0;
  751. setup_timer(&port->ldc_reset_timer, vdc_ldc_reset_timer,
  752. (unsigned long)port);
  753. INIT_WORK(&port->ldc_reset_work, vdc_ldc_reset_work);
  754. err = vio_driver_init(&port->vio, vdev, VDEV_DISK,
  755. vdc_versions, ARRAY_SIZE(vdc_versions),
  756. &vdc_vio_ops, port->disk_name);
  757. if (err)
  758. goto err_out_free_port;
  759. port->vdisk_block_size = VDC_DEFAULT_BLK_SIZE;
  760. port->max_xfer_size = ((128 * 1024) / port->vdisk_block_size);
  761. port->ring_cookies = ((port->max_xfer_size *
  762. port->vdisk_block_size) / PAGE_SIZE) + 2;
  763. err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
  764. if (err)
  765. goto err_out_free_port;
  766. err = vdc_alloc_tx_ring(port);
  767. if (err)
  768. goto err_out_free_ldc;
  769. err = probe_disk(port);
  770. if (err)
  771. goto err_out_free_tx_ring;
  772. dev_set_drvdata(&vdev->dev, port);
  773. mdesc_release(hp);
  774. return 0;
  775. err_out_free_tx_ring:
  776. vdc_free_tx_ring(port);
  777. err_out_free_ldc:
  778. vio_ldc_free(&port->vio);
  779. err_out_free_port:
  780. kfree(port);
  781. err_out_release_mdesc:
  782. mdesc_release(hp);
  783. return err;
  784. }
  785. static int vdc_port_remove(struct vio_dev *vdev)
  786. {
  787. struct vdc_port *port = dev_get_drvdata(&vdev->dev);
  788. if (port) {
  789. unsigned long flags;
  790. spin_lock_irqsave(&port->vio.lock, flags);
  791. blk_stop_queue(port->disk->queue);
  792. spin_unlock_irqrestore(&port->vio.lock, flags);
  793. flush_work(&port->ldc_reset_work);
  794. del_timer_sync(&port->ldc_reset_timer);
  795. del_timer_sync(&port->vio.timer);
  796. del_gendisk(port->disk);
  797. blk_cleanup_queue(port->disk->queue);
  798. put_disk(port->disk);
  799. port->disk = NULL;
  800. vdc_free_tx_ring(port);
  801. vio_ldc_free(&port->vio);
  802. dev_set_drvdata(&vdev->dev, NULL);
  803. kfree(port);
  804. }
  805. return 0;
  806. }
  807. static void vdc_requeue_inflight(struct vdc_port *port)
  808. {
  809. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  810. u32 idx;
  811. for (idx = dr->cons; idx != dr->prod; idx = vio_dring_next(dr, idx)) {
  812. struct vio_disk_desc *desc = vio_dring_entry(dr, idx);
  813. struct vdc_req_entry *rqe = &port->rq_arr[idx];
  814. struct request *req;
  815. ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
  816. desc->hdr.state = VIO_DESC_FREE;
  817. dr->cons = vio_dring_next(dr, idx);
  818. req = rqe->req;
  819. if (req == NULL) {
  820. vdc_end_special(port, desc);
  821. continue;
  822. }
  823. rqe->req = NULL;
  824. blk_requeue_request(port->disk->queue, req);
  825. }
  826. }
  827. static void vdc_queue_drain(struct vdc_port *port)
  828. {
  829. struct request *req;
  830. while ((req = blk_fetch_request(port->disk->queue)) != NULL)
  831. __blk_end_request_all(req, BLK_STS_IOERR);
  832. }
  833. static void vdc_ldc_reset_timer(unsigned long _arg)
  834. {
  835. struct vdc_port *port = (struct vdc_port *) _arg;
  836. struct vio_driver_state *vio = &port->vio;
  837. unsigned long flags;
  838. spin_lock_irqsave(&vio->lock, flags);
  839. if (!(port->vio.hs_state & VIO_HS_COMPLETE)) {
  840. pr_warn(PFX "%s ldc down %llu seconds, draining queue\n",
  841. port->disk_name, port->ldc_timeout);
  842. vdc_queue_drain(port);
  843. vdc_blk_queue_start(port);
  844. }
  845. spin_unlock_irqrestore(&vio->lock, flags);
  846. }
  847. static void vdc_ldc_reset_work(struct work_struct *work)
  848. {
  849. struct vdc_port *port;
  850. struct vio_driver_state *vio;
  851. unsigned long flags;
  852. port = container_of(work, struct vdc_port, ldc_reset_work);
  853. vio = &port->vio;
  854. spin_lock_irqsave(&vio->lock, flags);
  855. vdc_ldc_reset(port);
  856. spin_unlock_irqrestore(&vio->lock, flags);
  857. }
  858. static void vdc_ldc_reset(struct vdc_port *port)
  859. {
  860. int err;
  861. assert_spin_locked(&port->vio.lock);
  862. pr_warn(PFX "%s ldc link reset\n", port->disk_name);
  863. blk_stop_queue(port->disk->queue);
  864. vdc_requeue_inflight(port);
  865. vdc_port_down(port);
  866. err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
  867. if (err) {
  868. pr_err(PFX "%s vio_ldc_alloc:%d\n", port->disk_name, err);
  869. return;
  870. }
  871. err = vdc_alloc_tx_ring(port);
  872. if (err) {
  873. pr_err(PFX "%s vio_alloc_tx_ring:%d\n", port->disk_name, err);
  874. goto err_free_ldc;
  875. }
  876. if (port->ldc_timeout)
  877. mod_timer(&port->ldc_reset_timer,
  878. round_jiffies(jiffies + HZ * port->ldc_timeout));
  879. mod_timer(&port->vio.timer, round_jiffies(jiffies + HZ));
  880. return;
  881. err_free_ldc:
  882. vio_ldc_free(&port->vio);
  883. }
  884. static const struct vio_device_id vdc_port_match[] = {
  885. {
  886. .type = "vdc-port",
  887. },
  888. {},
  889. };
  890. MODULE_DEVICE_TABLE(vio, vdc_port_match);
  891. static struct vio_driver vdc_port_driver = {
  892. .id_table = vdc_port_match,
  893. .probe = vdc_port_probe,
  894. .remove = vdc_port_remove,
  895. .name = "vdc_port",
  896. };
  897. static int __init vdc_init(void)
  898. {
  899. int err;
  900. sunvdc_wq = alloc_workqueue("sunvdc", 0, 0);
  901. if (!sunvdc_wq)
  902. return -ENOMEM;
  903. err = register_blkdev(0, VDCBLK_NAME);
  904. if (err < 0)
  905. goto out_free_wq;
  906. vdc_major = err;
  907. err = vio_register_driver(&vdc_port_driver);
  908. if (err)
  909. goto out_unregister_blkdev;
  910. return 0;
  911. out_unregister_blkdev:
  912. unregister_blkdev(vdc_major, VDCBLK_NAME);
  913. vdc_major = 0;
  914. out_free_wq:
  915. destroy_workqueue(sunvdc_wq);
  916. return err;
  917. }
  918. static void __exit vdc_exit(void)
  919. {
  920. vio_unregister_driver(&vdc_port_driver);
  921. unregister_blkdev(vdc_major, VDCBLK_NAME);
  922. destroy_workqueue(sunvdc_wq);
  923. }
  924. module_init(vdc_init);
  925. module_exit(vdc_exit);