sunvdc.c 21 KB

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