sunvdc.c 28 KB

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