sunvdc.c 28 KB

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