sunvdc.c 19 KB

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