sunvdc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. /* sunvdc.c: Sun LDOM Virtual Disk Client.
  2. *
  3. * Copyright (C) 2007 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/slab.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/completion.h>
  14. #include <linux/delay.h>
  15. #include <linux/init.h>
  16. #include <linux/list.h>
  17. #include <asm/vio.h>
  18. #include <asm/ldc.h>
  19. #define DRV_MODULE_NAME "sunvdc"
  20. #define PFX DRV_MODULE_NAME ": "
  21. #define DRV_MODULE_VERSION "1.0"
  22. #define DRV_MODULE_RELDATE "June 25, 2007"
  23. static char version[] __devinitdata =
  24. DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
  25. MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
  26. MODULE_DESCRIPTION("Sun LDOM virtual disk client driver");
  27. MODULE_LICENSE("GPL");
  28. MODULE_VERSION(DRV_MODULE_VERSION);
  29. #define VDC_TX_RING_SIZE 256
  30. #define WAITING_FOR_LINK_UP 0x01
  31. #define WAITING_FOR_TX_SPACE 0x02
  32. #define WAITING_FOR_GEN_CMD 0x04
  33. #define WAITING_FOR_ANY -1
  34. struct vdc_req_entry {
  35. struct request *req;
  36. };
  37. struct vdc_port {
  38. struct vio_driver_state vio;
  39. struct gendisk *disk;
  40. struct vdc_completion *cmp;
  41. u64 req_id;
  42. u64 seq;
  43. struct vdc_req_entry rq_arr[VDC_TX_RING_SIZE];
  44. unsigned long ring_cookies;
  45. u64 max_xfer_size;
  46. u32 vdisk_block_size;
  47. /* The server fills these in for us in the disk attribute
  48. * ACK packet.
  49. */
  50. u64 operations;
  51. u32 vdisk_size;
  52. u8 vdisk_type;
  53. char disk_name[32];
  54. struct vio_disk_geom geom;
  55. struct vio_disk_vtoc label;
  56. };
  57. static inline struct vdc_port *to_vdc_port(struct vio_driver_state *vio)
  58. {
  59. return container_of(vio, struct vdc_port, vio);
  60. }
  61. /* Ordered from largest major to lowest */
  62. static struct vio_version vdc_versions[] = {
  63. { .major = 1, .minor = 0 },
  64. };
  65. #define VDCBLK_NAME "vdisk"
  66. static int vdc_major;
  67. #define PARTITION_SHIFT 3
  68. static inline u32 vdc_tx_dring_avail(struct vio_dring_state *dr)
  69. {
  70. return vio_dring_avail(dr, VDC_TX_RING_SIZE);
  71. }
  72. static int vdc_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  73. {
  74. struct gendisk *disk = bdev->bd_disk;
  75. struct vdc_port *port = disk->private_data;
  76. geo->heads = (u8) port->geom.num_hd;
  77. geo->sectors = (u8) port->geom.num_sec;
  78. geo->cylinders = port->geom.num_cyl;
  79. return 0;
  80. }
  81. static struct block_device_operations vdc_fops = {
  82. .owner = THIS_MODULE,
  83. .getgeo = vdc_getgeo,
  84. };
  85. static void vdc_finish(struct vio_driver_state *vio, int err, int waiting_for)
  86. {
  87. if (vio->cmp &&
  88. (waiting_for == -1 ||
  89. vio->cmp->waiting_for == waiting_for)) {
  90. vio->cmp->err = err;
  91. complete(&vio->cmp->com);
  92. vio->cmp = NULL;
  93. }
  94. }
  95. static void vdc_handshake_complete(struct vio_driver_state *vio)
  96. {
  97. vdc_finish(vio, 0, WAITING_FOR_LINK_UP);
  98. }
  99. static int vdc_handle_unknown(struct vdc_port *port, void *arg)
  100. {
  101. struct vio_msg_tag *pkt = arg;
  102. printk(KERN_ERR PFX "Received unknown msg [%02x:%02x:%04x:%08x]\n",
  103. pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
  104. printk(KERN_ERR PFX "Resetting connection.\n");
  105. ldc_disconnect(port->vio.lp);
  106. return -ECONNRESET;
  107. }
  108. static int vdc_send_attr(struct vio_driver_state *vio)
  109. {
  110. struct vdc_port *port = to_vdc_port(vio);
  111. struct vio_disk_attr_info pkt;
  112. memset(&pkt, 0, sizeof(pkt));
  113. pkt.tag.type = VIO_TYPE_CTRL;
  114. pkt.tag.stype = VIO_SUBTYPE_INFO;
  115. pkt.tag.stype_env = VIO_ATTR_INFO;
  116. pkt.tag.sid = vio_send_sid(vio);
  117. pkt.xfer_mode = VIO_DRING_MODE;
  118. pkt.vdisk_block_size = port->vdisk_block_size;
  119. pkt.max_xfer_size = port->max_xfer_size;
  120. viodbg(HS, "SEND ATTR xfer_mode[0x%x] blksz[%u] max_xfer[%lu]\n",
  121. pkt.xfer_mode, pkt.vdisk_block_size, pkt.max_xfer_size);
  122. return vio_ldc_send(&port->vio, &pkt, sizeof(pkt));
  123. }
  124. static int vdc_handle_attr(struct vio_driver_state *vio, void *arg)
  125. {
  126. struct vdc_port *port = to_vdc_port(vio);
  127. struct vio_disk_attr_info *pkt = arg;
  128. viodbg(HS, "GOT ATTR stype[0x%x] ops[%lx] disk_size[%lu] disk_type[%x] "
  129. "xfer_mode[0x%x] blksz[%u] max_xfer[%lu]\n",
  130. pkt->tag.stype, pkt->operations,
  131. pkt->vdisk_size, pkt->vdisk_type,
  132. pkt->xfer_mode, pkt->vdisk_block_size,
  133. pkt->max_xfer_size);
  134. if (pkt->tag.stype == VIO_SUBTYPE_ACK) {
  135. switch (pkt->vdisk_type) {
  136. case VD_DISK_TYPE_DISK:
  137. case VD_DISK_TYPE_SLICE:
  138. break;
  139. default:
  140. printk(KERN_ERR PFX "%s: Bogus vdisk_type 0x%x\n",
  141. vio->name, pkt->vdisk_type);
  142. return -ECONNRESET;
  143. }
  144. if (pkt->vdisk_block_size > port->vdisk_block_size) {
  145. printk(KERN_ERR PFX "%s: BLOCK size increased "
  146. "%u --> %u\n",
  147. vio->name,
  148. port->vdisk_block_size, pkt->vdisk_block_size);
  149. return -ECONNRESET;
  150. }
  151. port->operations = pkt->operations;
  152. port->vdisk_size = pkt->vdisk_size;
  153. port->vdisk_type = pkt->vdisk_type;
  154. if (pkt->max_xfer_size < port->max_xfer_size)
  155. port->max_xfer_size = pkt->max_xfer_size;
  156. port->vdisk_block_size = pkt->vdisk_block_size;
  157. return 0;
  158. } else {
  159. printk(KERN_ERR PFX "%s: Attribute NACK\n", vio->name);
  160. return -ECONNRESET;
  161. }
  162. }
  163. static void vdc_end_special(struct vdc_port *port, struct vio_disk_desc *desc)
  164. {
  165. int err = desc->status;
  166. vdc_finish(&port->vio, -err, WAITING_FOR_GEN_CMD);
  167. }
  168. static void vdc_end_request(struct request *req, int uptodate, int num_sectors)
  169. {
  170. if (end_that_request_first(req, uptodate, num_sectors))
  171. return;
  172. add_disk_randomness(req->rq_disk);
  173. end_that_request_last(req, uptodate);
  174. }
  175. static void vdc_end_one(struct vdc_port *port, struct vio_dring_state *dr,
  176. unsigned int index)
  177. {
  178. struct vio_disk_desc *desc = vio_dring_entry(dr, index);
  179. struct vdc_req_entry *rqe = &port->rq_arr[index];
  180. struct request *req;
  181. if (unlikely(desc->hdr.state != VIO_DESC_DONE))
  182. return;
  183. ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
  184. desc->hdr.state = VIO_DESC_FREE;
  185. dr->cons = (index + 1) & (VDC_TX_RING_SIZE - 1);
  186. req = rqe->req;
  187. if (req == NULL) {
  188. vdc_end_special(port, desc);
  189. return;
  190. }
  191. rqe->req = NULL;
  192. vdc_end_request(req, !desc->status, desc->size >> 9);
  193. if (blk_queue_stopped(port->disk->queue))
  194. blk_start_queue(port->disk->queue);
  195. }
  196. static int vdc_ack(struct vdc_port *port, void *msgbuf)
  197. {
  198. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  199. struct vio_dring_data *pkt = msgbuf;
  200. if (unlikely(pkt->dring_ident != dr->ident ||
  201. pkt->start_idx != pkt->end_idx ||
  202. pkt->start_idx >= VDC_TX_RING_SIZE))
  203. return 0;
  204. vdc_end_one(port, dr, pkt->start_idx);
  205. return 0;
  206. }
  207. static int vdc_nack(struct vdc_port *port, void *msgbuf)
  208. {
  209. /* XXX Implement me XXX */
  210. return 0;
  211. }
  212. static void vdc_event(void *arg, int event)
  213. {
  214. struct vdc_port *port = arg;
  215. struct vio_driver_state *vio = &port->vio;
  216. unsigned long flags;
  217. int err;
  218. spin_lock_irqsave(&vio->lock, flags);
  219. if (unlikely(event == LDC_EVENT_RESET ||
  220. event == LDC_EVENT_UP)) {
  221. vio_link_state_change(vio, event);
  222. spin_unlock_irqrestore(&vio->lock, flags);
  223. return;
  224. }
  225. if (unlikely(event != LDC_EVENT_DATA_READY)) {
  226. printk(KERN_WARNING PFX "Unexpected LDC event %d\n", event);
  227. spin_unlock_irqrestore(&vio->lock, flags);
  228. return;
  229. }
  230. err = 0;
  231. while (1) {
  232. union {
  233. struct vio_msg_tag tag;
  234. u64 raw[8];
  235. } msgbuf;
  236. err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf));
  237. if (unlikely(err < 0)) {
  238. if (err == -ECONNRESET)
  239. vio_conn_reset(vio);
  240. break;
  241. }
  242. if (err == 0)
  243. break;
  244. viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n",
  245. msgbuf.tag.type,
  246. msgbuf.tag.stype,
  247. msgbuf.tag.stype_env,
  248. msgbuf.tag.sid);
  249. err = vio_validate_sid(vio, &msgbuf.tag);
  250. if (err < 0)
  251. break;
  252. if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) {
  253. if (msgbuf.tag.stype == VIO_SUBTYPE_ACK)
  254. err = vdc_ack(port, &msgbuf);
  255. else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK)
  256. err = vdc_nack(port, &msgbuf);
  257. else
  258. err = vdc_handle_unknown(port, &msgbuf);
  259. } else if (msgbuf.tag.type == VIO_TYPE_CTRL) {
  260. err = vio_control_pkt_engine(vio, &msgbuf);
  261. } else {
  262. err = vdc_handle_unknown(port, &msgbuf);
  263. }
  264. if (err < 0)
  265. break;
  266. }
  267. if (err < 0)
  268. vdc_finish(&port->vio, err, WAITING_FOR_ANY);
  269. spin_unlock_irqrestore(&vio->lock, flags);
  270. }
  271. static int __vdc_tx_trigger(struct vdc_port *port)
  272. {
  273. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  274. struct vio_dring_data hdr = {
  275. .tag = {
  276. .type = VIO_TYPE_DATA,
  277. .stype = VIO_SUBTYPE_INFO,
  278. .stype_env = VIO_DRING_DATA,
  279. .sid = vio_send_sid(&port->vio),
  280. },
  281. .dring_ident = dr->ident,
  282. .start_idx = dr->prod,
  283. .end_idx = dr->prod,
  284. };
  285. int err, delay;
  286. hdr.seq = dr->snd_nxt;
  287. delay = 1;
  288. do {
  289. err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
  290. if (err > 0) {
  291. dr->snd_nxt++;
  292. break;
  293. }
  294. udelay(delay);
  295. if ((delay <<= 1) > 128)
  296. delay = 128;
  297. } while (err == -EAGAIN);
  298. return err;
  299. }
  300. static int __send_request(struct request *req)
  301. {
  302. struct vdc_port *port = req->rq_disk->private_data;
  303. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  304. struct scatterlist sg[port->ring_cookies];
  305. struct vdc_req_entry *rqe;
  306. struct vio_disk_desc *desc;
  307. unsigned int map_perm;
  308. int nsg, err, i;
  309. u64 len;
  310. u8 op;
  311. map_perm = LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
  312. if (rq_data_dir(req) == READ) {
  313. map_perm |= LDC_MAP_W;
  314. op = VD_OP_BREAD;
  315. } else {
  316. map_perm |= LDC_MAP_R;
  317. op = VD_OP_BWRITE;
  318. }
  319. nsg = blk_rq_map_sg(req->q, req, sg);
  320. len = 0;
  321. for (i = 0; i < nsg; i++)
  322. len += sg[i].length;
  323. if (unlikely(vdc_tx_dring_avail(dr) < 1)) {
  324. blk_stop_queue(port->disk->queue);
  325. err = -ENOMEM;
  326. goto out;
  327. }
  328. desc = vio_dring_cur(dr);
  329. err = ldc_map_sg(port->vio.lp, sg, nsg,
  330. desc->cookies, port->ring_cookies,
  331. map_perm);
  332. if (err < 0) {
  333. printk(KERN_ERR PFX "ldc_map_sg() failure, err=%d.\n", err);
  334. return err;
  335. }
  336. rqe = &port->rq_arr[dr->prod];
  337. rqe->req = req;
  338. desc->hdr.ack = VIO_ACK_ENABLE;
  339. desc->req_id = port->req_id;
  340. desc->operation = op;
  341. if (port->vdisk_type == VD_DISK_TYPE_DISK) {
  342. desc->slice = 0xff;
  343. } else {
  344. desc->slice = 0;
  345. }
  346. desc->status = ~0;
  347. desc->offset = (req->sector << 9) / port->vdisk_block_size;
  348. desc->size = len;
  349. desc->ncookies = err;
  350. /* This has to be a non-SMP write barrier because we are writing
  351. * to memory which is shared with the peer LDOM.
  352. */
  353. wmb();
  354. desc->hdr.state = VIO_DESC_READY;
  355. err = __vdc_tx_trigger(port);
  356. if (err < 0) {
  357. printk(KERN_ERR PFX "vdc_tx_trigger() failure, err=%d\n", err);
  358. } else {
  359. port->req_id++;
  360. dr->prod = (dr->prod + 1) & (VDC_TX_RING_SIZE - 1);
  361. }
  362. out:
  363. return err;
  364. }
  365. static void do_vdc_request(struct request_queue *q)
  366. {
  367. while (1) {
  368. struct request *req = elv_next_request(q);
  369. if (!req)
  370. break;
  371. blkdev_dequeue_request(req);
  372. if (__send_request(req) < 0)
  373. vdc_end_request(req, 0, req->hard_nr_sectors);
  374. }
  375. }
  376. static int generic_request(struct vdc_port *port, u8 op, void *buf, int len)
  377. {
  378. struct vio_dring_state *dr;
  379. struct vio_completion comp;
  380. struct vio_disk_desc *desc;
  381. unsigned int map_perm;
  382. unsigned long flags;
  383. int op_len, err;
  384. void *req_buf;
  385. if (!(((u64)1 << ((u64)op - 1)) & port->operations))
  386. return -EOPNOTSUPP;
  387. switch (op) {
  388. case VD_OP_BREAD:
  389. case VD_OP_BWRITE:
  390. default:
  391. return -EINVAL;
  392. case VD_OP_FLUSH:
  393. op_len = 0;
  394. map_perm = 0;
  395. break;
  396. case VD_OP_GET_WCE:
  397. op_len = sizeof(u32);
  398. map_perm = LDC_MAP_W;
  399. break;
  400. case VD_OP_SET_WCE:
  401. op_len = sizeof(u32);
  402. map_perm = LDC_MAP_R;
  403. break;
  404. case VD_OP_GET_VTOC:
  405. op_len = sizeof(struct vio_disk_vtoc);
  406. map_perm = LDC_MAP_W;
  407. break;
  408. case VD_OP_SET_VTOC:
  409. op_len = sizeof(struct vio_disk_vtoc);
  410. map_perm = LDC_MAP_R;
  411. break;
  412. case VD_OP_GET_DISKGEOM:
  413. op_len = sizeof(struct vio_disk_geom);
  414. map_perm = LDC_MAP_W;
  415. break;
  416. case VD_OP_SET_DISKGEOM:
  417. op_len = sizeof(struct vio_disk_geom);
  418. map_perm = LDC_MAP_R;
  419. break;
  420. case VD_OP_SCSICMD:
  421. op_len = 16;
  422. map_perm = LDC_MAP_RW;
  423. break;
  424. case VD_OP_GET_DEVID:
  425. op_len = sizeof(struct vio_disk_devid);
  426. map_perm = LDC_MAP_W;
  427. break;
  428. case VD_OP_GET_EFI:
  429. case VD_OP_SET_EFI:
  430. return -EOPNOTSUPP;
  431. break;
  432. };
  433. map_perm |= LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
  434. op_len = (op_len + 7) & ~7;
  435. req_buf = kzalloc(op_len, GFP_KERNEL);
  436. if (!req_buf)
  437. return -ENOMEM;
  438. if (len > op_len)
  439. len = op_len;
  440. if (map_perm & LDC_MAP_R)
  441. memcpy(req_buf, buf, len);
  442. spin_lock_irqsave(&port->vio.lock, flags);
  443. dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  444. /* XXX If we want to use this code generically we have to
  445. * XXX handle TX ring exhaustion etc.
  446. */
  447. desc = vio_dring_cur(dr);
  448. err = ldc_map_single(port->vio.lp, req_buf, op_len,
  449. desc->cookies, port->ring_cookies,
  450. map_perm);
  451. if (err < 0) {
  452. spin_unlock_irqrestore(&port->vio.lock, flags);
  453. kfree(req_buf);
  454. return err;
  455. }
  456. init_completion(&comp.com);
  457. comp.waiting_for = WAITING_FOR_GEN_CMD;
  458. port->vio.cmp = &comp;
  459. desc->hdr.ack = VIO_ACK_ENABLE;
  460. desc->req_id = port->req_id;
  461. desc->operation = op;
  462. desc->slice = 0;
  463. desc->status = ~0;
  464. desc->offset = 0;
  465. desc->size = op_len;
  466. desc->ncookies = err;
  467. /* This has to be a non-SMP write barrier because we are writing
  468. * to memory which is shared with the peer LDOM.
  469. */
  470. wmb();
  471. desc->hdr.state = VIO_DESC_READY;
  472. err = __vdc_tx_trigger(port);
  473. if (err >= 0) {
  474. port->req_id++;
  475. dr->prod = (dr->prod + 1) & (VDC_TX_RING_SIZE - 1);
  476. spin_unlock_irqrestore(&port->vio.lock, flags);
  477. wait_for_completion(&comp.com);
  478. err = comp.err;
  479. } else {
  480. port->vio.cmp = NULL;
  481. spin_unlock_irqrestore(&port->vio.lock, flags);
  482. }
  483. if (map_perm & LDC_MAP_W)
  484. memcpy(buf, req_buf, len);
  485. kfree(req_buf);
  486. return err;
  487. }
  488. static int __devinit vdc_alloc_tx_ring(struct vdc_port *port)
  489. {
  490. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  491. unsigned long len, entry_size;
  492. int ncookies;
  493. void *dring;
  494. entry_size = sizeof(struct vio_disk_desc) +
  495. (sizeof(struct ldc_trans_cookie) * port->ring_cookies);
  496. len = (VDC_TX_RING_SIZE * entry_size);
  497. ncookies = VIO_MAX_RING_COOKIES;
  498. dring = ldc_alloc_exp_dring(port->vio.lp, len,
  499. dr->cookies, &ncookies,
  500. (LDC_MAP_SHADOW |
  501. LDC_MAP_DIRECT |
  502. LDC_MAP_RW));
  503. if (IS_ERR(dring))
  504. return PTR_ERR(dring);
  505. dr->base = dring;
  506. dr->entry_size = entry_size;
  507. dr->num_entries = VDC_TX_RING_SIZE;
  508. dr->prod = dr->cons = 0;
  509. dr->pending = VDC_TX_RING_SIZE;
  510. dr->ncookies = ncookies;
  511. return 0;
  512. }
  513. static void vdc_free_tx_ring(struct vdc_port *port)
  514. {
  515. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  516. if (dr->base) {
  517. ldc_free_exp_dring(port->vio.lp, dr->base,
  518. (dr->entry_size * dr->num_entries),
  519. dr->cookies, dr->ncookies);
  520. dr->base = NULL;
  521. dr->entry_size = 0;
  522. dr->num_entries = 0;
  523. dr->pending = 0;
  524. dr->ncookies = 0;
  525. }
  526. }
  527. static int probe_disk(struct vdc_port *port)
  528. {
  529. struct vio_completion comp;
  530. struct request_queue *q;
  531. struct gendisk *g;
  532. int err;
  533. init_completion(&comp.com);
  534. comp.err = 0;
  535. comp.waiting_for = WAITING_FOR_LINK_UP;
  536. port->vio.cmp = &comp;
  537. vio_port_up(&port->vio);
  538. wait_for_completion(&comp.com);
  539. if (comp.err)
  540. return comp.err;
  541. err = generic_request(port, VD_OP_GET_VTOC,
  542. &port->label, sizeof(port->label));
  543. if (err < 0) {
  544. printk(KERN_ERR PFX "VD_OP_GET_VTOC returns error %d\n", err);
  545. return err;
  546. }
  547. err = generic_request(port, VD_OP_GET_DISKGEOM,
  548. &port->geom, sizeof(port->geom));
  549. if (err < 0) {
  550. printk(KERN_ERR PFX "VD_OP_GET_DISKGEOM returns "
  551. "error %d\n", err);
  552. return err;
  553. }
  554. port->vdisk_size = ((u64)port->geom.num_cyl *
  555. (u64)port->geom.num_hd *
  556. (u64)port->geom.num_sec);
  557. q = blk_init_queue(do_vdc_request, &port->vio.lock);
  558. if (!q) {
  559. printk(KERN_ERR PFX "%s: Could not allocate queue.\n",
  560. port->vio.name);
  561. return -ENOMEM;
  562. }
  563. g = alloc_disk(1 << PARTITION_SHIFT);
  564. if (!g) {
  565. printk(KERN_ERR PFX "%s: Could not allocate gendisk.\n",
  566. port->vio.name);
  567. blk_cleanup_queue(q);
  568. return -ENOMEM;
  569. }
  570. port->disk = g;
  571. blk_queue_max_hw_segments(q, port->ring_cookies);
  572. blk_queue_max_phys_segments(q, port->ring_cookies);
  573. blk_queue_max_sectors(q, port->max_xfer_size);
  574. g->major = vdc_major;
  575. g->first_minor = port->vio.vdev->dev_no << PARTITION_SHIFT;
  576. strcpy(g->disk_name, port->disk_name);
  577. g->fops = &vdc_fops;
  578. g->queue = q;
  579. g->private_data = port;
  580. g->driverfs_dev = &port->vio.vdev->dev;
  581. set_capacity(g, port->vdisk_size);
  582. printk(KERN_INFO PFX "%s: %u sectors (%u MB)\n",
  583. g->disk_name,
  584. port->vdisk_size, (port->vdisk_size >> (20 - 9)));
  585. add_disk(g);
  586. return 0;
  587. }
  588. static struct ldc_channel_config vdc_ldc_cfg = {
  589. .event = vdc_event,
  590. .mtu = 64,
  591. .mode = LDC_MODE_UNRELIABLE,
  592. };
  593. static struct vio_driver_ops vdc_vio_ops = {
  594. .send_attr = vdc_send_attr,
  595. .handle_attr = vdc_handle_attr,
  596. .handshake_complete = vdc_handshake_complete,
  597. };
  598. static void print_version(void)
  599. {
  600. static int version_printed;
  601. if (version_printed++ == 0)
  602. printk(KERN_INFO "%s", version);
  603. }
  604. static int __devinit vdc_port_probe(struct vio_dev *vdev,
  605. const struct vio_device_id *id)
  606. {
  607. struct mdesc_handle *hp;
  608. struct vdc_port *port;
  609. int err;
  610. print_version();
  611. hp = mdesc_grab();
  612. err = -ENODEV;
  613. if ((vdev->dev_no << PARTITION_SHIFT) & ~(u64)MINORMASK) {
  614. printk(KERN_ERR PFX "Port id [%lu] too large.\n",
  615. vdev->dev_no);
  616. goto err_out_release_mdesc;
  617. }
  618. port = kzalloc(sizeof(*port), GFP_KERNEL);
  619. err = -ENOMEM;
  620. if (!port) {
  621. printk(KERN_ERR PFX "Cannot allocate vdc_port.\n");
  622. goto err_out_release_mdesc;
  623. }
  624. if (vdev->dev_no >= 26)
  625. snprintf(port->disk_name, sizeof(port->disk_name),
  626. VDCBLK_NAME "%c%c",
  627. 'a' + ((int)vdev->dev_no / 26) - 1,
  628. 'a' + ((int)vdev->dev_no % 26));
  629. else
  630. snprintf(port->disk_name, sizeof(port->disk_name),
  631. VDCBLK_NAME "%c", 'a' + ((int)vdev->dev_no % 26));
  632. err = vio_driver_init(&port->vio, vdev, VDEV_DISK,
  633. vdc_versions, ARRAY_SIZE(vdc_versions),
  634. &vdc_vio_ops, port->disk_name);
  635. if (err)
  636. goto err_out_free_port;
  637. port->vdisk_block_size = 512;
  638. port->max_xfer_size = ((128 * 1024) / port->vdisk_block_size);
  639. port->ring_cookies = ((port->max_xfer_size *
  640. port->vdisk_block_size) / PAGE_SIZE) + 2;
  641. err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
  642. if (err)
  643. goto err_out_free_port;
  644. err = vdc_alloc_tx_ring(port);
  645. if (err)
  646. goto err_out_free_ldc;
  647. err = probe_disk(port);
  648. if (err)
  649. goto err_out_free_tx_ring;
  650. dev_set_drvdata(&vdev->dev, port);
  651. mdesc_release(hp);
  652. return 0;
  653. err_out_free_tx_ring:
  654. vdc_free_tx_ring(port);
  655. err_out_free_ldc:
  656. vio_ldc_free(&port->vio);
  657. err_out_free_port:
  658. kfree(port);
  659. err_out_release_mdesc:
  660. mdesc_release(hp);
  661. return err;
  662. }
  663. static int vdc_port_remove(struct vio_dev *vdev)
  664. {
  665. struct vdc_port *port = dev_get_drvdata(&vdev->dev);
  666. if (port) {
  667. del_timer_sync(&port->vio.timer);
  668. vdc_free_tx_ring(port);
  669. vio_ldc_free(&port->vio);
  670. dev_set_drvdata(&vdev->dev, NULL);
  671. kfree(port);
  672. }
  673. return 0;
  674. }
  675. static struct vio_device_id vdc_port_match[] = {
  676. {
  677. .type = "vdc-port",
  678. },
  679. {},
  680. };
  681. MODULE_DEVICE_TABLE(vio, vdc_port_match);
  682. static struct vio_driver vdc_port_driver = {
  683. .id_table = vdc_port_match,
  684. .probe = vdc_port_probe,
  685. .remove = vdc_port_remove,
  686. .driver = {
  687. .name = "vdc_port",
  688. .owner = THIS_MODULE,
  689. }
  690. };
  691. static int __init vdc_init(void)
  692. {
  693. int err;
  694. err = register_blkdev(0, VDCBLK_NAME);
  695. if (err < 0)
  696. goto out_err;
  697. vdc_major = err;
  698. err = vio_register_driver(&vdc_port_driver);
  699. if (err)
  700. goto out_unregister_blkdev;
  701. return 0;
  702. out_unregister_blkdev:
  703. unregister_blkdev(vdc_major, VDCBLK_NAME);
  704. vdc_major = 0;
  705. out_err:
  706. return err;
  707. }
  708. static void __exit vdc_exit(void)
  709. {
  710. vio_unregister_driver(&vdc_port_driver);
  711. unregister_blkdev(vdc_major, VDCBLK_NAME);
  712. }
  713. module_init(vdc_init);
  714. module_exit(vdc_exit);