sunvdc.c 25 KB

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