sunvdc.c 19 KB

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