sunvdc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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/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 const 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[%llu]\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[%llx] disk_size[%llu] disk_type[%x] "
  130. "xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\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_one(struct vdc_port *port, struct vio_dring_state *dr,
  170. unsigned int index)
  171. {
  172. struct vio_disk_desc *desc = vio_dring_entry(dr, index);
  173. struct vdc_req_entry *rqe = &port->rq_arr[index];
  174. struct request *req;
  175. if (unlikely(desc->hdr.state != VIO_DESC_DONE))
  176. return;
  177. ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
  178. desc->hdr.state = VIO_DESC_FREE;
  179. dr->cons = (index + 1) & (VDC_TX_RING_SIZE - 1);
  180. req = rqe->req;
  181. if (req == NULL) {
  182. vdc_end_special(port, desc);
  183. return;
  184. }
  185. rqe->req = NULL;
  186. __blk_end_request(req, (desc->status ? -EIO : 0), desc->size);
  187. if (blk_queue_stopped(port->disk->queue))
  188. blk_start_queue(port->disk->queue);
  189. }
  190. static int vdc_ack(struct vdc_port *port, void *msgbuf)
  191. {
  192. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  193. struct vio_dring_data *pkt = msgbuf;
  194. if (unlikely(pkt->dring_ident != dr->ident ||
  195. pkt->start_idx != pkt->end_idx ||
  196. pkt->start_idx >= VDC_TX_RING_SIZE))
  197. return 0;
  198. vdc_end_one(port, dr, pkt->start_idx);
  199. return 0;
  200. }
  201. static int vdc_nack(struct vdc_port *port, void *msgbuf)
  202. {
  203. /* XXX Implement me XXX */
  204. return 0;
  205. }
  206. static void vdc_event(void *arg, int event)
  207. {
  208. struct vdc_port *port = arg;
  209. struct vio_driver_state *vio = &port->vio;
  210. unsigned long flags;
  211. int err;
  212. spin_lock_irqsave(&vio->lock, flags);
  213. if (unlikely(event == LDC_EVENT_RESET ||
  214. event == LDC_EVENT_UP)) {
  215. vio_link_state_change(vio, event);
  216. spin_unlock_irqrestore(&vio->lock, flags);
  217. return;
  218. }
  219. if (unlikely(event != LDC_EVENT_DATA_READY)) {
  220. printk(KERN_WARNING PFX "Unexpected LDC event %d\n", event);
  221. spin_unlock_irqrestore(&vio->lock, flags);
  222. return;
  223. }
  224. err = 0;
  225. while (1) {
  226. union {
  227. struct vio_msg_tag tag;
  228. u64 raw[8];
  229. } msgbuf;
  230. err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf));
  231. if (unlikely(err < 0)) {
  232. if (err == -ECONNRESET)
  233. vio_conn_reset(vio);
  234. break;
  235. }
  236. if (err == 0)
  237. break;
  238. viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n",
  239. msgbuf.tag.type,
  240. msgbuf.tag.stype,
  241. msgbuf.tag.stype_env,
  242. msgbuf.tag.sid);
  243. err = vio_validate_sid(vio, &msgbuf.tag);
  244. if (err < 0)
  245. break;
  246. if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) {
  247. if (msgbuf.tag.stype == VIO_SUBTYPE_ACK)
  248. err = vdc_ack(port, &msgbuf);
  249. else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK)
  250. err = vdc_nack(port, &msgbuf);
  251. else
  252. err = vdc_handle_unknown(port, &msgbuf);
  253. } else if (msgbuf.tag.type == VIO_TYPE_CTRL) {
  254. err = vio_control_pkt_engine(vio, &msgbuf);
  255. } else {
  256. err = vdc_handle_unknown(port, &msgbuf);
  257. }
  258. if (err < 0)
  259. break;
  260. }
  261. if (err < 0)
  262. vdc_finish(&port->vio, err, WAITING_FOR_ANY);
  263. spin_unlock_irqrestore(&vio->lock, flags);
  264. }
  265. static int __vdc_tx_trigger(struct vdc_port *port)
  266. {
  267. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  268. struct vio_dring_data hdr = {
  269. .tag = {
  270. .type = VIO_TYPE_DATA,
  271. .stype = VIO_SUBTYPE_INFO,
  272. .stype_env = VIO_DRING_DATA,
  273. .sid = vio_send_sid(&port->vio),
  274. },
  275. .dring_ident = dr->ident,
  276. .start_idx = dr->prod,
  277. .end_idx = dr->prod,
  278. };
  279. int err, delay;
  280. hdr.seq = dr->snd_nxt;
  281. delay = 1;
  282. do {
  283. err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
  284. if (err > 0) {
  285. dr->snd_nxt++;
  286. break;
  287. }
  288. udelay(delay);
  289. if ((delay <<= 1) > 128)
  290. delay = 128;
  291. } while (err == -EAGAIN);
  292. return err;
  293. }
  294. static int __send_request(struct request *req)
  295. {
  296. struct vdc_port *port = req->rq_disk->private_data;
  297. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  298. struct scatterlist sg[port->ring_cookies];
  299. struct vdc_req_entry *rqe;
  300. struct vio_disk_desc *desc;
  301. unsigned int map_perm;
  302. int nsg, err, i;
  303. u64 len;
  304. u8 op;
  305. map_perm = LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
  306. if (rq_data_dir(req) == READ) {
  307. map_perm |= LDC_MAP_W;
  308. op = VD_OP_BREAD;
  309. } else {
  310. map_perm |= LDC_MAP_R;
  311. op = VD_OP_BWRITE;
  312. }
  313. sg_init_table(sg, port->ring_cookies);
  314. nsg = blk_rq_map_sg(req->q, req, sg);
  315. len = 0;
  316. for (i = 0; i < nsg; i++)
  317. len += sg[i].length;
  318. if (unlikely(vdc_tx_dring_avail(dr) < 1)) {
  319. blk_stop_queue(port->disk->queue);
  320. err = -ENOMEM;
  321. goto out;
  322. }
  323. desc = vio_dring_cur(dr);
  324. err = ldc_map_sg(port->vio.lp, sg, nsg,
  325. desc->cookies, port->ring_cookies,
  326. map_perm);
  327. if (err < 0) {
  328. printk(KERN_ERR PFX "ldc_map_sg() failure, err=%d.\n", err);
  329. return err;
  330. }
  331. rqe = &port->rq_arr[dr->prod];
  332. rqe->req = req;
  333. desc->hdr.ack = VIO_ACK_ENABLE;
  334. desc->req_id = port->req_id;
  335. desc->operation = op;
  336. if (port->vdisk_type == VD_DISK_TYPE_DISK) {
  337. desc->slice = 0xff;
  338. } else {
  339. desc->slice = 0;
  340. }
  341. desc->status = ~0;
  342. desc->offset = (blk_rq_pos(req) << 9) / port->vdisk_block_size;
  343. desc->size = len;
  344. desc->ncookies = err;
  345. /* This has to be a non-SMP write barrier because we are writing
  346. * to memory which is shared with the peer LDOM.
  347. */
  348. wmb();
  349. desc->hdr.state = VIO_DESC_READY;
  350. err = __vdc_tx_trigger(port);
  351. if (err < 0) {
  352. printk(KERN_ERR PFX "vdc_tx_trigger() failure, err=%d\n", err);
  353. } else {
  354. port->req_id++;
  355. dr->prod = (dr->prod + 1) & (VDC_TX_RING_SIZE - 1);
  356. }
  357. out:
  358. return err;
  359. }
  360. static void do_vdc_request(struct request_queue *q)
  361. {
  362. while (1) {
  363. struct request *req = blk_fetch_request(q);
  364. if (!req)
  365. break;
  366. if (__send_request(req) < 0)
  367. __blk_end_request_all(req, -EIO);
  368. }
  369. }
  370. static int generic_request(struct vdc_port *port, u8 op, void *buf, int len)
  371. {
  372. struct vio_dring_state *dr;
  373. struct vio_completion comp;
  374. struct vio_disk_desc *desc;
  375. unsigned int map_perm;
  376. unsigned long flags;
  377. int op_len, err;
  378. void *req_buf;
  379. if (!(((u64)1 << ((u64)op - 1)) & port->operations))
  380. return -EOPNOTSUPP;
  381. switch (op) {
  382. case VD_OP_BREAD:
  383. case VD_OP_BWRITE:
  384. default:
  385. return -EINVAL;
  386. case VD_OP_FLUSH:
  387. op_len = 0;
  388. map_perm = 0;
  389. break;
  390. case VD_OP_GET_WCE:
  391. op_len = sizeof(u32);
  392. map_perm = LDC_MAP_W;
  393. break;
  394. case VD_OP_SET_WCE:
  395. op_len = sizeof(u32);
  396. map_perm = LDC_MAP_R;
  397. break;
  398. case VD_OP_GET_VTOC:
  399. op_len = sizeof(struct vio_disk_vtoc);
  400. map_perm = LDC_MAP_W;
  401. break;
  402. case VD_OP_SET_VTOC:
  403. op_len = sizeof(struct vio_disk_vtoc);
  404. map_perm = LDC_MAP_R;
  405. break;
  406. case VD_OP_GET_DISKGEOM:
  407. op_len = sizeof(struct vio_disk_geom);
  408. map_perm = LDC_MAP_W;
  409. break;
  410. case VD_OP_SET_DISKGEOM:
  411. op_len = sizeof(struct vio_disk_geom);
  412. map_perm = LDC_MAP_R;
  413. break;
  414. case VD_OP_SCSICMD:
  415. op_len = 16;
  416. map_perm = LDC_MAP_RW;
  417. break;
  418. case VD_OP_GET_DEVID:
  419. op_len = sizeof(struct vio_disk_devid);
  420. map_perm = LDC_MAP_W;
  421. break;
  422. case VD_OP_GET_EFI:
  423. case VD_OP_SET_EFI:
  424. return -EOPNOTSUPP;
  425. break;
  426. };
  427. map_perm |= LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
  428. op_len = (op_len + 7) & ~7;
  429. req_buf = kzalloc(op_len, GFP_KERNEL);
  430. if (!req_buf)
  431. return -ENOMEM;
  432. if (len > op_len)
  433. len = op_len;
  434. if (map_perm & LDC_MAP_R)
  435. memcpy(req_buf, buf, len);
  436. spin_lock_irqsave(&port->vio.lock, flags);
  437. dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  438. /* XXX If we want to use this code generically we have to
  439. * XXX handle TX ring exhaustion etc.
  440. */
  441. desc = vio_dring_cur(dr);
  442. err = ldc_map_single(port->vio.lp, req_buf, op_len,
  443. desc->cookies, port->ring_cookies,
  444. map_perm);
  445. if (err < 0) {
  446. spin_unlock_irqrestore(&port->vio.lock, flags);
  447. kfree(req_buf);
  448. return err;
  449. }
  450. init_completion(&comp.com);
  451. comp.waiting_for = WAITING_FOR_GEN_CMD;
  452. port->vio.cmp = &comp;
  453. desc->hdr.ack = VIO_ACK_ENABLE;
  454. desc->req_id = port->req_id;
  455. desc->operation = op;
  456. desc->slice = 0;
  457. desc->status = ~0;
  458. desc->offset = 0;
  459. desc->size = op_len;
  460. desc->ncookies = err;
  461. /* This has to be a non-SMP write barrier because we are writing
  462. * to memory which is shared with the peer LDOM.
  463. */
  464. wmb();
  465. desc->hdr.state = VIO_DESC_READY;
  466. err = __vdc_tx_trigger(port);
  467. if (err >= 0) {
  468. port->req_id++;
  469. dr->prod = (dr->prod + 1) & (VDC_TX_RING_SIZE - 1);
  470. spin_unlock_irqrestore(&port->vio.lock, flags);
  471. wait_for_completion(&comp.com);
  472. err = comp.err;
  473. } else {
  474. port->vio.cmp = NULL;
  475. spin_unlock_irqrestore(&port->vio.lock, flags);
  476. }
  477. if (map_perm & LDC_MAP_W)
  478. memcpy(buf, req_buf, len);
  479. kfree(req_buf);
  480. return err;
  481. }
  482. static int __devinit vdc_alloc_tx_ring(struct vdc_port *port)
  483. {
  484. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  485. unsigned long len, entry_size;
  486. int ncookies;
  487. void *dring;
  488. entry_size = sizeof(struct vio_disk_desc) +
  489. (sizeof(struct ldc_trans_cookie) * port->ring_cookies);
  490. len = (VDC_TX_RING_SIZE * entry_size);
  491. ncookies = VIO_MAX_RING_COOKIES;
  492. dring = ldc_alloc_exp_dring(port->vio.lp, len,
  493. dr->cookies, &ncookies,
  494. (LDC_MAP_SHADOW |
  495. LDC_MAP_DIRECT |
  496. LDC_MAP_RW));
  497. if (IS_ERR(dring))
  498. return PTR_ERR(dring);
  499. dr->base = dring;
  500. dr->entry_size = entry_size;
  501. dr->num_entries = VDC_TX_RING_SIZE;
  502. dr->prod = dr->cons = 0;
  503. dr->pending = VDC_TX_RING_SIZE;
  504. dr->ncookies = ncookies;
  505. return 0;
  506. }
  507. static void vdc_free_tx_ring(struct vdc_port *port)
  508. {
  509. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  510. if (dr->base) {
  511. ldc_free_exp_dring(port->vio.lp, dr->base,
  512. (dr->entry_size * dr->num_entries),
  513. dr->cookies, dr->ncookies);
  514. dr->base = NULL;
  515. dr->entry_size = 0;
  516. dr->num_entries = 0;
  517. dr->pending = 0;
  518. dr->ncookies = 0;
  519. }
  520. }
  521. static int probe_disk(struct vdc_port *port)
  522. {
  523. struct vio_completion comp;
  524. struct request_queue *q;
  525. struct gendisk *g;
  526. int err;
  527. init_completion(&comp.com);
  528. comp.err = 0;
  529. comp.waiting_for = WAITING_FOR_LINK_UP;
  530. port->vio.cmp = &comp;
  531. vio_port_up(&port->vio);
  532. wait_for_completion(&comp.com);
  533. if (comp.err)
  534. return comp.err;
  535. err = generic_request(port, VD_OP_GET_VTOC,
  536. &port->label, sizeof(port->label));
  537. if (err < 0) {
  538. printk(KERN_ERR PFX "VD_OP_GET_VTOC returns error %d\n", err);
  539. return err;
  540. }
  541. err = generic_request(port, VD_OP_GET_DISKGEOM,
  542. &port->geom, sizeof(port->geom));
  543. if (err < 0) {
  544. printk(KERN_ERR PFX "VD_OP_GET_DISKGEOM returns "
  545. "error %d\n", err);
  546. return err;
  547. }
  548. port->vdisk_size = ((u64)port->geom.num_cyl *
  549. (u64)port->geom.num_hd *
  550. (u64)port->geom.num_sec);
  551. q = blk_init_queue(do_vdc_request, &port->vio.lock);
  552. if (!q) {
  553. printk(KERN_ERR PFX "%s: Could not allocate queue.\n",
  554. port->vio.name);
  555. return -ENOMEM;
  556. }
  557. g = alloc_disk(1 << PARTITION_SHIFT);
  558. if (!g) {
  559. printk(KERN_ERR PFX "%s: Could not allocate gendisk.\n",
  560. port->vio.name);
  561. blk_cleanup_queue(q);
  562. return -ENOMEM;
  563. }
  564. port->disk = g;
  565. blk_queue_max_segments(q, port->ring_cookies);
  566. blk_queue_max_hw_sectors(q, port->max_xfer_size);
  567. g->major = vdc_major;
  568. g->first_minor = port->vio.vdev->dev_no << PARTITION_SHIFT;
  569. strcpy(g->disk_name, port->disk_name);
  570. g->fops = &vdc_fops;
  571. g->queue = q;
  572. g->private_data = port;
  573. g->driverfs_dev = &port->vio.vdev->dev;
  574. set_capacity(g, port->vdisk_size);
  575. printk(KERN_INFO PFX "%s: %u sectors (%u MB)\n",
  576. g->disk_name,
  577. port->vdisk_size, (port->vdisk_size >> (20 - 9)));
  578. add_disk(g);
  579. return 0;
  580. }
  581. static struct ldc_channel_config vdc_ldc_cfg = {
  582. .event = vdc_event,
  583. .mtu = 64,
  584. .mode = LDC_MODE_UNRELIABLE,
  585. };
  586. static struct vio_driver_ops vdc_vio_ops = {
  587. .send_attr = vdc_send_attr,
  588. .handle_attr = vdc_handle_attr,
  589. .handshake_complete = vdc_handshake_complete,
  590. };
  591. static void __devinit print_version(void)
  592. {
  593. static int version_printed;
  594. if (version_printed++ == 0)
  595. printk(KERN_INFO "%s", version);
  596. }
  597. static int __devinit vdc_port_probe(struct vio_dev *vdev,
  598. const struct vio_device_id *id)
  599. {
  600. struct mdesc_handle *hp;
  601. struct vdc_port *port;
  602. int err;
  603. print_version();
  604. hp = mdesc_grab();
  605. err = -ENODEV;
  606. if ((vdev->dev_no << PARTITION_SHIFT) & ~(u64)MINORMASK) {
  607. printk(KERN_ERR PFX "Port id [%llu] too large.\n",
  608. vdev->dev_no);
  609. goto err_out_release_mdesc;
  610. }
  611. port = kzalloc(sizeof(*port), GFP_KERNEL);
  612. err = -ENOMEM;
  613. if (!port) {
  614. printk(KERN_ERR PFX "Cannot allocate vdc_port.\n");
  615. goto err_out_release_mdesc;
  616. }
  617. if (vdev->dev_no >= 26)
  618. snprintf(port->disk_name, sizeof(port->disk_name),
  619. VDCBLK_NAME "%c%c",
  620. 'a' + ((int)vdev->dev_no / 26) - 1,
  621. 'a' + ((int)vdev->dev_no % 26));
  622. else
  623. snprintf(port->disk_name, sizeof(port->disk_name),
  624. VDCBLK_NAME "%c", 'a' + ((int)vdev->dev_no % 26));
  625. err = vio_driver_init(&port->vio, vdev, VDEV_DISK,
  626. vdc_versions, ARRAY_SIZE(vdc_versions),
  627. &vdc_vio_ops, port->disk_name);
  628. if (err)
  629. goto err_out_free_port;
  630. port->vdisk_block_size = 512;
  631. port->max_xfer_size = ((128 * 1024) / port->vdisk_block_size);
  632. port->ring_cookies = ((port->max_xfer_size *
  633. port->vdisk_block_size) / PAGE_SIZE) + 2;
  634. err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
  635. if (err)
  636. goto err_out_free_port;
  637. err = vdc_alloc_tx_ring(port);
  638. if (err)
  639. goto err_out_free_ldc;
  640. err = probe_disk(port);
  641. if (err)
  642. goto err_out_free_tx_ring;
  643. dev_set_drvdata(&vdev->dev, port);
  644. mdesc_release(hp);
  645. return 0;
  646. err_out_free_tx_ring:
  647. vdc_free_tx_ring(port);
  648. err_out_free_ldc:
  649. vio_ldc_free(&port->vio);
  650. err_out_free_port:
  651. kfree(port);
  652. err_out_release_mdesc:
  653. mdesc_release(hp);
  654. return err;
  655. }
  656. static int vdc_port_remove(struct vio_dev *vdev)
  657. {
  658. struct vdc_port *port = dev_get_drvdata(&vdev->dev);
  659. if (port) {
  660. del_timer_sync(&port->vio.timer);
  661. vdc_free_tx_ring(port);
  662. vio_ldc_free(&port->vio);
  663. dev_set_drvdata(&vdev->dev, NULL);
  664. kfree(port);
  665. }
  666. return 0;
  667. }
  668. static const struct vio_device_id vdc_port_match[] = {
  669. {
  670. .type = "vdc-port",
  671. },
  672. {},
  673. };
  674. MODULE_DEVICE_TABLE(vio, vdc_port_match);
  675. static struct vio_driver vdc_port_driver = {
  676. .id_table = vdc_port_match,
  677. .probe = vdc_port_probe,
  678. .remove = vdc_port_remove,
  679. .driver = {
  680. .name = "vdc_port",
  681. .owner = THIS_MODULE,
  682. }
  683. };
  684. static int __init vdc_init(void)
  685. {
  686. int err;
  687. err = register_blkdev(0, VDCBLK_NAME);
  688. if (err < 0)
  689. goto out_err;
  690. vdc_major = err;
  691. err = vio_register_driver(&vdc_port_driver);
  692. if (err)
  693. goto out_unregister_blkdev;
  694. return 0;
  695. out_unregister_blkdev:
  696. unregister_blkdev(vdc_major, VDCBLK_NAME);
  697. vdc_major = 0;
  698. out_err:
  699. return err;
  700. }
  701. static void __exit vdc_exit(void)
  702. {
  703. vio_unregister_driver(&vdc_port_driver);
  704. unregister_blkdev(vdc_major, VDCBLK_NAME);
  705. }
  706. module_init(vdc_init);
  707. module_exit(vdc_exit);