sunvdc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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. sg_init_table(sg, port->ring_cookies);
  320. nsg = blk_rq_map_sg(req->q, req, sg);
  321. len = 0;
  322. for (i = 0; i < nsg; i++)
  323. len += sg[i].length;
  324. if (unlikely(vdc_tx_dring_avail(dr) < 1)) {
  325. blk_stop_queue(port->disk->queue);
  326. err = -ENOMEM;
  327. goto out;
  328. }
  329. desc = vio_dring_cur(dr);
  330. err = ldc_map_sg(port->vio.lp, sg, nsg,
  331. desc->cookies, port->ring_cookies,
  332. map_perm);
  333. if (err < 0) {
  334. printk(KERN_ERR PFX "ldc_map_sg() failure, err=%d.\n", err);
  335. return err;
  336. }
  337. rqe = &port->rq_arr[dr->prod];
  338. rqe->req = req;
  339. desc->hdr.ack = VIO_ACK_ENABLE;
  340. desc->req_id = port->req_id;
  341. desc->operation = op;
  342. if (port->vdisk_type == VD_DISK_TYPE_DISK) {
  343. desc->slice = 0xff;
  344. } else {
  345. desc->slice = 0;
  346. }
  347. desc->status = ~0;
  348. desc->offset = (req->sector << 9) / port->vdisk_block_size;
  349. desc->size = len;
  350. desc->ncookies = err;
  351. /* This has to be a non-SMP write barrier because we are writing
  352. * to memory which is shared with the peer LDOM.
  353. */
  354. wmb();
  355. desc->hdr.state = VIO_DESC_READY;
  356. err = __vdc_tx_trigger(port);
  357. if (err < 0) {
  358. printk(KERN_ERR PFX "vdc_tx_trigger() failure, err=%d\n", err);
  359. } else {
  360. port->req_id++;
  361. dr->prod = (dr->prod + 1) & (VDC_TX_RING_SIZE - 1);
  362. }
  363. out:
  364. return err;
  365. }
  366. static void do_vdc_request(struct request_queue *q)
  367. {
  368. while (1) {
  369. struct request *req = elv_next_request(q);
  370. if (!req)
  371. break;
  372. blkdev_dequeue_request(req);
  373. if (__send_request(req) < 0)
  374. vdc_end_request(req, 0, req->hard_nr_sectors);
  375. }
  376. }
  377. static int generic_request(struct vdc_port *port, u8 op, void *buf, int len)
  378. {
  379. struct vio_dring_state *dr;
  380. struct vio_completion comp;
  381. struct vio_disk_desc *desc;
  382. unsigned int map_perm;
  383. unsigned long flags;
  384. int op_len, err;
  385. void *req_buf;
  386. if (!(((u64)1 << ((u64)op - 1)) & port->operations))
  387. return -EOPNOTSUPP;
  388. switch (op) {
  389. case VD_OP_BREAD:
  390. case VD_OP_BWRITE:
  391. default:
  392. return -EINVAL;
  393. case VD_OP_FLUSH:
  394. op_len = 0;
  395. map_perm = 0;
  396. break;
  397. case VD_OP_GET_WCE:
  398. op_len = sizeof(u32);
  399. map_perm = LDC_MAP_W;
  400. break;
  401. case VD_OP_SET_WCE:
  402. op_len = sizeof(u32);
  403. map_perm = LDC_MAP_R;
  404. break;
  405. case VD_OP_GET_VTOC:
  406. op_len = sizeof(struct vio_disk_vtoc);
  407. map_perm = LDC_MAP_W;
  408. break;
  409. case VD_OP_SET_VTOC:
  410. op_len = sizeof(struct vio_disk_vtoc);
  411. map_perm = LDC_MAP_R;
  412. break;
  413. case VD_OP_GET_DISKGEOM:
  414. op_len = sizeof(struct vio_disk_geom);
  415. map_perm = LDC_MAP_W;
  416. break;
  417. case VD_OP_SET_DISKGEOM:
  418. op_len = sizeof(struct vio_disk_geom);
  419. map_perm = LDC_MAP_R;
  420. break;
  421. case VD_OP_SCSICMD:
  422. op_len = 16;
  423. map_perm = LDC_MAP_RW;
  424. break;
  425. case VD_OP_GET_DEVID:
  426. op_len = sizeof(struct vio_disk_devid);
  427. map_perm = LDC_MAP_W;
  428. break;
  429. case VD_OP_GET_EFI:
  430. case VD_OP_SET_EFI:
  431. return -EOPNOTSUPP;
  432. break;
  433. };
  434. map_perm |= LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
  435. op_len = (op_len + 7) & ~7;
  436. req_buf = kzalloc(op_len, GFP_KERNEL);
  437. if (!req_buf)
  438. return -ENOMEM;
  439. if (len > op_len)
  440. len = op_len;
  441. if (map_perm & LDC_MAP_R)
  442. memcpy(req_buf, buf, len);
  443. spin_lock_irqsave(&port->vio.lock, flags);
  444. dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  445. /* XXX If we want to use this code generically we have to
  446. * XXX handle TX ring exhaustion etc.
  447. */
  448. desc = vio_dring_cur(dr);
  449. err = ldc_map_single(port->vio.lp, req_buf, op_len,
  450. desc->cookies, port->ring_cookies,
  451. map_perm);
  452. if (err < 0) {
  453. spin_unlock_irqrestore(&port->vio.lock, flags);
  454. kfree(req_buf);
  455. return err;
  456. }
  457. init_completion(&comp.com);
  458. comp.waiting_for = WAITING_FOR_GEN_CMD;
  459. port->vio.cmp = &comp;
  460. desc->hdr.ack = VIO_ACK_ENABLE;
  461. desc->req_id = port->req_id;
  462. desc->operation = op;
  463. desc->slice = 0;
  464. desc->status = ~0;
  465. desc->offset = 0;
  466. desc->size = op_len;
  467. desc->ncookies = err;
  468. /* This has to be a non-SMP write barrier because we are writing
  469. * to memory which is shared with the peer LDOM.
  470. */
  471. wmb();
  472. desc->hdr.state = VIO_DESC_READY;
  473. err = __vdc_tx_trigger(port);
  474. if (err >= 0) {
  475. port->req_id++;
  476. dr->prod = (dr->prod + 1) & (VDC_TX_RING_SIZE - 1);
  477. spin_unlock_irqrestore(&port->vio.lock, flags);
  478. wait_for_completion(&comp.com);
  479. err = comp.err;
  480. } else {
  481. port->vio.cmp = NULL;
  482. spin_unlock_irqrestore(&port->vio.lock, flags);
  483. }
  484. if (map_perm & LDC_MAP_W)
  485. memcpy(buf, req_buf, len);
  486. kfree(req_buf);
  487. return err;
  488. }
  489. static int __devinit vdc_alloc_tx_ring(struct vdc_port *port)
  490. {
  491. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  492. unsigned long len, entry_size;
  493. int ncookies;
  494. void *dring;
  495. entry_size = sizeof(struct vio_disk_desc) +
  496. (sizeof(struct ldc_trans_cookie) * port->ring_cookies);
  497. len = (VDC_TX_RING_SIZE * entry_size);
  498. ncookies = VIO_MAX_RING_COOKIES;
  499. dring = ldc_alloc_exp_dring(port->vio.lp, len,
  500. dr->cookies, &ncookies,
  501. (LDC_MAP_SHADOW |
  502. LDC_MAP_DIRECT |
  503. LDC_MAP_RW));
  504. if (IS_ERR(dring))
  505. return PTR_ERR(dring);
  506. dr->base = dring;
  507. dr->entry_size = entry_size;
  508. dr->num_entries = VDC_TX_RING_SIZE;
  509. dr->prod = dr->cons = 0;
  510. dr->pending = VDC_TX_RING_SIZE;
  511. dr->ncookies = ncookies;
  512. return 0;
  513. }
  514. static void vdc_free_tx_ring(struct vdc_port *port)
  515. {
  516. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  517. if (dr->base) {
  518. ldc_free_exp_dring(port->vio.lp, dr->base,
  519. (dr->entry_size * dr->num_entries),
  520. dr->cookies, dr->ncookies);
  521. dr->base = NULL;
  522. dr->entry_size = 0;
  523. dr->num_entries = 0;
  524. dr->pending = 0;
  525. dr->ncookies = 0;
  526. }
  527. }
  528. static int probe_disk(struct vdc_port *port)
  529. {
  530. struct vio_completion comp;
  531. struct request_queue *q;
  532. struct gendisk *g;
  533. int err;
  534. init_completion(&comp.com);
  535. comp.err = 0;
  536. comp.waiting_for = WAITING_FOR_LINK_UP;
  537. port->vio.cmp = &comp;
  538. vio_port_up(&port->vio);
  539. wait_for_completion(&comp.com);
  540. if (comp.err)
  541. return comp.err;
  542. err = generic_request(port, VD_OP_GET_VTOC,
  543. &port->label, sizeof(port->label));
  544. if (err < 0) {
  545. printk(KERN_ERR PFX "VD_OP_GET_VTOC returns error %d\n", err);
  546. return err;
  547. }
  548. err = generic_request(port, VD_OP_GET_DISKGEOM,
  549. &port->geom, sizeof(port->geom));
  550. if (err < 0) {
  551. printk(KERN_ERR PFX "VD_OP_GET_DISKGEOM returns "
  552. "error %d\n", err);
  553. return err;
  554. }
  555. port->vdisk_size = ((u64)port->geom.num_cyl *
  556. (u64)port->geom.num_hd *
  557. (u64)port->geom.num_sec);
  558. q = blk_init_queue(do_vdc_request, &port->vio.lock);
  559. if (!q) {
  560. printk(KERN_ERR PFX "%s: Could not allocate queue.\n",
  561. port->vio.name);
  562. return -ENOMEM;
  563. }
  564. g = alloc_disk(1 << PARTITION_SHIFT);
  565. if (!g) {
  566. printk(KERN_ERR PFX "%s: Could not allocate gendisk.\n",
  567. port->vio.name);
  568. blk_cleanup_queue(q);
  569. return -ENOMEM;
  570. }
  571. port->disk = g;
  572. blk_queue_max_hw_segments(q, port->ring_cookies);
  573. blk_queue_max_phys_segments(q, port->ring_cookies);
  574. blk_queue_max_sectors(q, port->max_xfer_size);
  575. g->major = vdc_major;
  576. g->first_minor = port->vio.vdev->dev_no << PARTITION_SHIFT;
  577. strcpy(g->disk_name, port->disk_name);
  578. g->fops = &vdc_fops;
  579. g->queue = q;
  580. g->private_data = port;
  581. g->driverfs_dev = &port->vio.vdev->dev;
  582. set_capacity(g, port->vdisk_size);
  583. printk(KERN_INFO PFX "%s: %u sectors (%u MB)\n",
  584. g->disk_name,
  585. port->vdisk_size, (port->vdisk_size >> (20 - 9)));
  586. add_disk(g);
  587. return 0;
  588. }
  589. static struct ldc_channel_config vdc_ldc_cfg = {
  590. .event = vdc_event,
  591. .mtu = 64,
  592. .mode = LDC_MODE_UNRELIABLE,
  593. };
  594. static struct vio_driver_ops vdc_vio_ops = {
  595. .send_attr = vdc_send_attr,
  596. .handle_attr = vdc_handle_attr,
  597. .handshake_complete = vdc_handshake_complete,
  598. };
  599. static void print_version(void)
  600. {
  601. static int version_printed;
  602. if (version_printed++ == 0)
  603. printk(KERN_INFO "%s", version);
  604. }
  605. static int __devinit vdc_port_probe(struct vio_dev *vdev,
  606. const struct vio_device_id *id)
  607. {
  608. struct mdesc_handle *hp;
  609. struct vdc_port *port;
  610. int err;
  611. print_version();
  612. hp = mdesc_grab();
  613. err = -ENODEV;
  614. if ((vdev->dev_no << PARTITION_SHIFT) & ~(u64)MINORMASK) {
  615. printk(KERN_ERR PFX "Port id [%lu] too large.\n",
  616. vdev->dev_no);
  617. goto err_out_release_mdesc;
  618. }
  619. port = kzalloc(sizeof(*port), GFP_KERNEL);
  620. err = -ENOMEM;
  621. if (!port) {
  622. printk(KERN_ERR PFX "Cannot allocate vdc_port.\n");
  623. goto err_out_release_mdesc;
  624. }
  625. if (vdev->dev_no >= 26)
  626. snprintf(port->disk_name, sizeof(port->disk_name),
  627. VDCBLK_NAME "%c%c",
  628. 'a' + ((int)vdev->dev_no / 26) - 1,
  629. 'a' + ((int)vdev->dev_no % 26));
  630. else
  631. snprintf(port->disk_name, sizeof(port->disk_name),
  632. VDCBLK_NAME "%c", 'a' + ((int)vdev->dev_no % 26));
  633. err = vio_driver_init(&port->vio, vdev, VDEV_DISK,
  634. vdc_versions, ARRAY_SIZE(vdc_versions),
  635. &vdc_vio_ops, port->disk_name);
  636. if (err)
  637. goto err_out_free_port;
  638. port->vdisk_block_size = 512;
  639. port->max_xfer_size = ((128 * 1024) / port->vdisk_block_size);
  640. port->ring_cookies = ((port->max_xfer_size *
  641. port->vdisk_block_size) / PAGE_SIZE) + 2;
  642. err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
  643. if (err)
  644. goto err_out_free_port;
  645. err = vdc_alloc_tx_ring(port);
  646. if (err)
  647. goto err_out_free_ldc;
  648. err = probe_disk(port);
  649. if (err)
  650. goto err_out_free_tx_ring;
  651. dev_set_drvdata(&vdev->dev, port);
  652. mdesc_release(hp);
  653. return 0;
  654. err_out_free_tx_ring:
  655. vdc_free_tx_ring(port);
  656. err_out_free_ldc:
  657. vio_ldc_free(&port->vio);
  658. err_out_free_port:
  659. kfree(port);
  660. err_out_release_mdesc:
  661. mdesc_release(hp);
  662. return err;
  663. }
  664. static int vdc_port_remove(struct vio_dev *vdev)
  665. {
  666. struct vdc_port *port = dev_get_drvdata(&vdev->dev);
  667. if (port) {
  668. del_timer_sync(&port->vio.timer);
  669. vdc_free_tx_ring(port);
  670. vio_ldc_free(&port->vio);
  671. dev_set_drvdata(&vdev->dev, NULL);
  672. kfree(port);
  673. }
  674. return 0;
  675. }
  676. static struct vio_device_id vdc_port_match[] = {
  677. {
  678. .type = "vdc-port",
  679. },
  680. {},
  681. };
  682. MODULE_DEVICE_TABLE(vio, vdc_port_match);
  683. static struct vio_driver vdc_port_driver = {
  684. .id_table = vdc_port_match,
  685. .probe = vdc_port_probe,
  686. .remove = vdc_port_remove,
  687. .driver = {
  688. .name = "vdc_port",
  689. .owner = THIS_MODULE,
  690. }
  691. };
  692. static int __init vdc_init(void)
  693. {
  694. int err;
  695. err = register_blkdev(0, VDCBLK_NAME);
  696. if (err < 0)
  697. goto out_err;
  698. vdc_major = err;
  699. err = vio_register_driver(&vdc_port_driver);
  700. if (err)
  701. goto out_unregister_blkdev;
  702. return 0;
  703. out_unregister_blkdev:
  704. unregister_blkdev(vdc_major, VDCBLK_NAME);
  705. vdc_major = 0;
  706. out_err:
  707. return err;
  708. }
  709. static void __exit vdc_exit(void)
  710. {
  711. vio_unregister_driver(&vdc_port_driver);
  712. unregister_blkdev(vdc_major, VDCBLK_NAME);
  713. }
  714. module_init(vdc_init);
  715. module_exit(vdc_exit);