sunvdc.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  1. /* sunvdc.c: Sun LDOM Virtual Disk Client.
  2. *
  3. * Copyright (C) 2007, 2008 David S. Miller <[email protected]>
  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/cdrom.h>
  12. #include <linux/slab.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/completion.h>
  15. #include <linux/delay.h>
  16. #include <linux/init.h>
  17. #include <linux/list.h>
  18. #include <linux/scatterlist.h>
  19. #include <asm/vio.h>
  20. #include <asm/ldc.h>
  21. #define DRV_MODULE_NAME "sunvdc"
  22. #define PFX DRV_MODULE_NAME ": "
  23. #define DRV_MODULE_VERSION "1.2"
  24. #define DRV_MODULE_RELDATE "November 24, 2014"
  25. static char version[] =
  26. DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
  27. MODULE_AUTHOR("David S. Miller ([email protected])");
  28. MODULE_DESCRIPTION("Sun LDOM virtual disk client driver");
  29. MODULE_LICENSE("GPL");
  30. MODULE_VERSION(DRV_MODULE_VERSION);
  31. #define VDC_TX_RING_SIZE 512
  32. #define WAITING_FOR_LINK_UP 0x01
  33. #define WAITING_FOR_TX_SPACE 0x02
  34. #define WAITING_FOR_GEN_CMD 0x04
  35. #define WAITING_FOR_ANY -1
  36. #define VDC_MAX_RETRIES 10
  37. static struct workqueue_struct *sunvdc_wq;
  38. struct vdc_req_entry {
  39. struct request *req;
  40. };
  41. struct vdc_port {
  42. struct vio_driver_state vio;
  43. struct gendisk *disk;
  44. struct vdc_completion *cmp;
  45. u64 req_id;
  46. u64 seq;
  47. struct vdc_req_entry rq_arr[VDC_TX_RING_SIZE];
  48. unsigned long ring_cookies;
  49. u64 max_xfer_size;
  50. u32 vdisk_block_size;
  51. u64 ldc_timeout;
  52. struct timer_list ldc_reset_timer;
  53. struct work_struct ldc_reset_work;
  54. /* The server fills these in for us in the disk attribute
  55. * ACK packet.
  56. */
  57. u64 operations;
  58. u32 vdisk_size;
  59. u8 vdisk_type;
  60. u8 vdisk_mtype;
  61. char disk_name[32];
  62. };
  63. static void vdc_ldc_reset(struct vdc_port *port);
  64. static void vdc_ldc_reset_work(struct work_struct *work);
  65. static void vdc_ldc_reset_timer(unsigned long _arg);
  66. static inline struct vdc_port *to_vdc_port(struct vio_driver_state *vio)
  67. {
  68. return container_of(vio, struct vdc_port, vio);
  69. }
  70. /* Ordered from largest major to lowest */
  71. static struct vio_version vdc_versions[] = {
  72. { .major = 1, .minor = 1 },
  73. { .major = 1, .minor = 0 },
  74. };
  75. static inline int vdc_version_supported(struct vdc_port *port,
  76. u16 major, u16 minor)
  77. {
  78. return port->vio.ver.major == major && port->vio.ver.minor >= minor;
  79. }
  80. #define VDCBLK_NAME "vdisk"
  81. static int vdc_major;
  82. #define PARTITION_SHIFT 3
  83. static inline u32 vdc_tx_dring_avail(struct vio_dring_state *dr)
  84. {
  85. return vio_dring_avail(dr, VDC_TX_RING_SIZE);
  86. }
  87. static int vdc_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  88. {
  89. struct gendisk *disk = bdev->bd_disk;
  90. sector_t nsect = get_capacity(disk);
  91. sector_t cylinders = nsect;
  92. geo->heads = 0xff;
  93. geo->sectors = 0x3f;
  94. sector_div(cylinders, geo->heads * geo->sectors);
  95. geo->cylinders = cylinders;
  96. if ((sector_t)(geo->cylinders + 1) * geo->heads * geo->sectors < nsect)
  97. geo->cylinders = 0xffff;
  98. return 0;
  99. }
  100. /* Add ioctl/CDROM_GET_CAPABILITY to support cdrom_id in udev
  101. * when vdisk_mtype is VD_MEDIA_TYPE_CD or VD_MEDIA_TYPE_DVD.
  102. * Needed to be able to install inside an ldom from an iso image.
  103. */
  104. static int vdc_ioctl(struct block_device *bdev, fmode_t mode,
  105. unsigned command, unsigned long argument)
  106. {
  107. int i;
  108. struct gendisk *disk;
  109. switch (command) {
  110. case CDROMMULTISESSION:
  111. pr_debug(PFX "Multisession CDs not supported\n");
  112. for (i = 0; i < sizeof(struct cdrom_multisession); i++)
  113. if (put_user(0, (char __user *)(argument + i)))
  114. return -EFAULT;
  115. return 0;
  116. case CDROM_GET_CAPABILITY:
  117. disk = bdev->bd_disk;
  118. if (bdev->bd_disk && (disk->flags & GENHD_FL_CD))
  119. return 0;
  120. return -EINVAL;
  121. default:
  122. pr_debug(PFX "ioctl %08x not supported\n", command);
  123. return -EINVAL;
  124. }
  125. }
  126. static const struct block_device_operations vdc_fops = {
  127. .owner = THIS_MODULE,
  128. .getgeo = vdc_getgeo,
  129. .ioctl = vdc_ioctl,
  130. };
  131. static void vdc_blk_queue_start(struct vdc_port *port)
  132. {
  133. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  134. /* restart blk queue when ring is half emptied. also called after
  135. * handshake completes, so check for initial handshake before we've
  136. * allocated a disk.
  137. */
  138. if (port->disk && blk_queue_stopped(port->disk->queue) &&
  139. vdc_tx_dring_avail(dr) * 100 / VDC_TX_RING_SIZE >= 50) {
  140. blk_start_queue(port->disk->queue);
  141. }
  142. }
  143. static void vdc_finish(struct vio_driver_state *vio, int err, int waiting_for)
  144. {
  145. if (vio->cmp &&
  146. (waiting_for == -1 ||
  147. vio->cmp->waiting_for == waiting_for)) {
  148. vio->cmp->err = err;
  149. complete(&vio->cmp->com);
  150. vio->cmp = NULL;
  151. }
  152. }
  153. static void vdc_handshake_complete(struct vio_driver_state *vio)
  154. {
  155. struct vdc_port *port = to_vdc_port(vio);
  156. del_timer(&port->ldc_reset_timer);
  157. vdc_finish(vio, 0, WAITING_FOR_LINK_UP);
  158. vdc_blk_queue_start(port);
  159. }
  160. static int vdc_handle_unknown(struct vdc_port *port, void *arg)
  161. {
  162. struct vio_msg_tag *pkt = arg;
  163. printk(KERN_ERR PFX "Received unknown msg [%02x:%02x:%04x:%08x]\n",
  164. pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
  165. printk(KERN_ERR PFX "Resetting connection.\n");
  166. ldc_disconnect(port->vio.lp);
  167. return -ECONNRESET;
  168. }
  169. static int vdc_send_attr(struct vio_driver_state *vio)
  170. {
  171. struct vdc_port *port = to_vdc_port(vio);
  172. struct vio_disk_attr_info pkt;
  173. memset(&pkt, 0, sizeof(pkt));
  174. pkt.tag.type = VIO_TYPE_CTRL;
  175. pkt.tag.stype = VIO_SUBTYPE_INFO;
  176. pkt.tag.stype_env = VIO_ATTR_INFO;
  177. pkt.tag.sid = vio_send_sid(vio);
  178. pkt.xfer_mode = VIO_DRING_MODE;
  179. pkt.vdisk_block_size = port->vdisk_block_size;
  180. pkt.max_xfer_size = port->max_xfer_size;
  181. viodbg(HS, "SEND ATTR xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
  182. pkt.xfer_mode, pkt.vdisk_block_size, pkt.max_xfer_size);
  183. return vio_ldc_send(&port->vio, &pkt, sizeof(pkt));
  184. }
  185. static int vdc_handle_attr(struct vio_driver_state *vio, void *arg)
  186. {
  187. struct vdc_port *port = to_vdc_port(vio);
  188. struct vio_disk_attr_info *pkt = arg;
  189. viodbg(HS, "GOT ATTR stype[0x%x] ops[%llx] disk_size[%llu] disk_type[%x] "
  190. "mtype[0x%x] xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
  191. pkt->tag.stype, pkt->operations,
  192. pkt->vdisk_size, pkt->vdisk_type, pkt->vdisk_mtype,
  193. pkt->xfer_mode, pkt->vdisk_block_size,
  194. pkt->max_xfer_size);
  195. if (pkt->tag.stype == VIO_SUBTYPE_ACK) {
  196. switch (pkt->vdisk_type) {
  197. case VD_DISK_TYPE_DISK:
  198. case VD_DISK_TYPE_SLICE:
  199. break;
  200. default:
  201. printk(KERN_ERR PFX "%s: Bogus vdisk_type 0x%x\n",
  202. vio->name, pkt->vdisk_type);
  203. return -ECONNRESET;
  204. }
  205. if (pkt->vdisk_block_size > port->vdisk_block_size) {
  206. printk(KERN_ERR PFX "%s: BLOCK size increased "
  207. "%u --> %u\n",
  208. vio->name,
  209. port->vdisk_block_size, pkt->vdisk_block_size);
  210. return -ECONNRESET;
  211. }
  212. port->operations = pkt->operations;
  213. port->vdisk_type = pkt->vdisk_type;
  214. if (vdc_version_supported(port, 1, 1)) {
  215. port->vdisk_size = pkt->vdisk_size;
  216. port->vdisk_mtype = pkt->vdisk_mtype;
  217. }
  218. if (pkt->max_xfer_size < port->max_xfer_size)
  219. port->max_xfer_size = pkt->max_xfer_size;
  220. port->vdisk_block_size = pkt->vdisk_block_size;
  221. return 0;
  222. } else {
  223. printk(KERN_ERR PFX "%s: Attribute NACK\n", vio->name);
  224. return -ECONNRESET;
  225. }
  226. }
  227. static void vdc_end_special(struct vdc_port *port, struct vio_disk_desc *desc)
  228. {
  229. int err = desc->status;
  230. vdc_finish(&port->vio, -err, WAITING_FOR_GEN_CMD);
  231. }
  232. static void vdc_end_one(struct vdc_port *port, struct vio_dring_state *dr,
  233. unsigned int index)
  234. {
  235. struct vio_disk_desc *desc = vio_dring_entry(dr, index);
  236. struct vdc_req_entry *rqe = &port->rq_arr[index];
  237. struct request *req;
  238. if (unlikely(desc->hdr.state != VIO_DESC_DONE))
  239. return;
  240. ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
  241. desc->hdr.state = VIO_DESC_FREE;
  242. dr->cons = vio_dring_next(dr, index);
  243. req = rqe->req;
  244. if (req == NULL) {
  245. vdc_end_special(port, desc);
  246. return;
  247. }
  248. rqe->req = NULL;
  249. __blk_end_request(req, (desc->status ? -EIO : 0), desc->size);
  250. vdc_blk_queue_start(port);
  251. }
  252. static int vdc_ack(struct vdc_port *port, void *msgbuf)
  253. {
  254. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  255. struct vio_dring_data *pkt = msgbuf;
  256. if (unlikely(pkt->dring_ident != dr->ident ||
  257. pkt->start_idx != pkt->end_idx ||
  258. pkt->start_idx >= VDC_TX_RING_SIZE))
  259. return 0;
  260. vdc_end_one(port, dr, pkt->start_idx);
  261. return 0;
  262. }
  263. static int vdc_nack(struct vdc_port *port, void *msgbuf)
  264. {
  265. /* XXX Implement me XXX */
  266. return 0;
  267. }
  268. static void vdc_event(void *arg, int event)
  269. {
  270. struct vdc_port *port = arg;
  271. struct vio_driver_state *vio = &port->vio;
  272. unsigned long flags;
  273. int err;
  274. spin_lock_irqsave(&vio->lock, flags);
  275. if (unlikely(event == LDC_EVENT_RESET)) {
  276. vio_link_state_change(vio, event);
  277. queue_work(sunvdc_wq, &port->ldc_reset_work);
  278. goto out;
  279. }
  280. if (unlikely(event == LDC_EVENT_UP)) {
  281. vio_link_state_change(vio, event);
  282. goto out;
  283. }
  284. if (unlikely(event != LDC_EVENT_DATA_READY)) {
  285. pr_warn(PFX "Unexpected LDC event %d\n", event);
  286. goto out;
  287. }
  288. err = 0;
  289. while (1) {
  290. union {
  291. struct vio_msg_tag tag;
  292. u64 raw[8];
  293. } msgbuf;
  294. err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf));
  295. if (unlikely(err < 0)) {
  296. if (err == -ECONNRESET)
  297. vio_conn_reset(vio);
  298. break;
  299. }
  300. if (err == 0)
  301. break;
  302. viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n",
  303. msgbuf.tag.type,
  304. msgbuf.tag.stype,
  305. msgbuf.tag.stype_env,
  306. msgbuf.tag.sid);
  307. err = vio_validate_sid(vio, &msgbuf.tag);
  308. if (err < 0)
  309. break;
  310. if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) {
  311. if (msgbuf.tag.stype == VIO_SUBTYPE_ACK)
  312. err = vdc_ack(port, &msgbuf);
  313. else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK)
  314. err = vdc_nack(port, &msgbuf);
  315. else
  316. err = vdc_handle_unknown(port, &msgbuf);
  317. } else if (msgbuf.tag.type == VIO_TYPE_CTRL) {
  318. err = vio_control_pkt_engine(vio, &msgbuf);
  319. } else {
  320. err = vdc_handle_unknown(port, &msgbuf);
  321. }
  322. if (err < 0)
  323. break;
  324. }
  325. if (err < 0)
  326. vdc_finish(&port->vio, err, WAITING_FOR_ANY);
  327. out:
  328. spin_unlock_irqrestore(&vio->lock, flags);
  329. }
  330. static int __vdc_tx_trigger(struct vdc_port *port)
  331. {
  332. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  333. struct vio_dring_data hdr = {
  334. .tag = {
  335. .type = VIO_TYPE_DATA,
  336. .stype = VIO_SUBTYPE_INFO,
  337. .stype_env = VIO_DRING_DATA,
  338. .sid = vio_send_sid(&port->vio),
  339. },
  340. .dring_ident = dr->ident,
  341. .start_idx = dr->prod,
  342. .end_idx = dr->prod,
  343. };
  344. int err, delay;
  345. int retries = 0;
  346. hdr.seq = dr->snd_nxt;
  347. delay = 1;
  348. do {
  349. err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
  350. if (err > 0) {
  351. dr->snd_nxt++;
  352. break;
  353. }
  354. udelay(delay);
  355. if ((delay <<= 1) > 128)
  356. delay = 128;
  357. if (retries++ > VDC_MAX_RETRIES)
  358. break;
  359. } while (err == -EAGAIN);
  360. if (err == -ENOTCONN)
  361. vdc_ldc_reset(port);
  362. return err;
  363. }
  364. static int __send_request(struct request *req)
  365. {
  366. struct vdc_port *port = req->rq_disk->private_data;
  367. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  368. struct scatterlist sg[port->ring_cookies];
  369. struct vdc_req_entry *rqe;
  370. struct vio_disk_desc *desc;
  371. unsigned int map_perm;
  372. int nsg, err, i;
  373. u64 len;
  374. u8 op;
  375. map_perm = LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
  376. if (rq_data_dir(req) == READ) {
  377. map_perm |= LDC_MAP_W;
  378. op = VD_OP_BREAD;
  379. } else {
  380. map_perm |= LDC_MAP_R;
  381. op = VD_OP_BWRITE;
  382. }
  383. sg_init_table(sg, port->ring_cookies);
  384. nsg = blk_rq_map_sg(req->q, req, sg);
  385. len = 0;
  386. for (i = 0; i < nsg; i++)
  387. len += sg[i].length;
  388. desc = vio_dring_cur(dr);
  389. err = ldc_map_sg(port->vio.lp, sg, nsg,
  390. desc->cookies, port->ring_cookies,
  391. map_perm);
  392. if (err < 0) {
  393. printk(KERN_ERR PFX "ldc_map_sg() failure, err=%d.\n", err);
  394. return err;
  395. }
  396. rqe = &port->rq_arr[dr->prod];
  397. rqe->req = req;
  398. desc->hdr.ack = VIO_ACK_ENABLE;
  399. desc->req_id = port->req_id;
  400. desc->operation = op;
  401. if (port->vdisk_type == VD_DISK_TYPE_DISK) {
  402. desc->slice = 0xff;
  403. } else {
  404. desc->slice = 0;
  405. }
  406. desc->status = ~0;
  407. desc->offset = (blk_rq_pos(req) << 9) / port->vdisk_block_size;
  408. desc->size = len;
  409. desc->ncookies = err;
  410. /* This has to be a non-SMP write barrier because we are writing
  411. * to memory which is shared with the peer LDOM.
  412. */
  413. wmb();
  414. desc->hdr.state = VIO_DESC_READY;
  415. err = __vdc_tx_trigger(port);
  416. if (err < 0) {
  417. printk(KERN_ERR PFX "vdc_tx_trigger() failure, err=%d\n", err);
  418. } else {
  419. port->req_id++;
  420. dr->prod = vio_dring_next(dr, dr->prod);
  421. }
  422. return err;
  423. }
  424. static void do_vdc_request(struct request_queue *rq)
  425. {
  426. struct request *req;
  427. while ((req = blk_peek_request(rq)) != NULL) {
  428. struct vdc_port *port;
  429. struct vio_dring_state *dr;
  430. port = req->rq_disk->private_data;
  431. dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  432. if (unlikely(vdc_tx_dring_avail(dr) < 1))
  433. goto wait;
  434. blk_start_request(req);
  435. if (__send_request(req) < 0) {
  436. blk_requeue_request(rq, req);
  437. wait:
  438. /* Avoid pointless unplugs. */
  439. blk_stop_queue(rq);
  440. break;
  441. }
  442. }
  443. }
  444. static int generic_request(struct vdc_port *port, u8 op, void *buf, int len)
  445. {
  446. struct vio_dring_state *dr;
  447. struct vio_completion comp;
  448. struct vio_disk_desc *desc;
  449. unsigned int map_perm;
  450. unsigned long flags;
  451. int op_len, err;
  452. void *req_buf;
  453. if (!(((u64)1 << (u64)op) & port->operations))
  454. return -EOPNOTSUPP;
  455. switch (op) {
  456. case VD_OP_BREAD:
  457. case VD_OP_BWRITE:
  458. default:
  459. return -EINVAL;
  460. case VD_OP_FLUSH:
  461. op_len = 0;
  462. map_perm = 0;
  463. break;
  464. case VD_OP_GET_WCE:
  465. op_len = sizeof(u32);
  466. map_perm = LDC_MAP_W;
  467. break;
  468. case VD_OP_SET_WCE:
  469. op_len = sizeof(u32);
  470. map_perm = LDC_MAP_R;
  471. break;
  472. case VD_OP_GET_VTOC:
  473. op_len = sizeof(struct vio_disk_vtoc);
  474. map_perm = LDC_MAP_W;
  475. break;
  476. case VD_OP_SET_VTOC:
  477. op_len = sizeof(struct vio_disk_vtoc);
  478. map_perm = LDC_MAP_R;
  479. break;
  480. case VD_OP_GET_DISKGEOM:
  481. op_len = sizeof(struct vio_disk_geom);
  482. map_perm = LDC_MAP_W;
  483. break;
  484. case VD_OP_SET_DISKGEOM:
  485. op_len = sizeof(struct vio_disk_geom);
  486. map_perm = LDC_MAP_R;
  487. break;
  488. case VD_OP_SCSICMD:
  489. op_len = 16;
  490. map_perm = LDC_MAP_RW;
  491. break;
  492. case VD_OP_GET_DEVID:
  493. op_len = sizeof(struct vio_disk_devid);
  494. map_perm = LDC_MAP_W;
  495. break;
  496. case VD_OP_GET_EFI:
  497. case VD_OP_SET_EFI:
  498. return -EOPNOTSUPP;
  499. break;
  500. };
  501. map_perm |= LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
  502. op_len = (op_len + 7) & ~7;
  503. req_buf = kzalloc(op_len, GFP_KERNEL);
  504. if (!req_buf)
  505. return -ENOMEM;
  506. if (len > op_len)
  507. len = op_len;
  508. if (map_perm & LDC_MAP_R)
  509. memcpy(req_buf, buf, len);
  510. spin_lock_irqsave(&port->vio.lock, flags);
  511. dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  512. /* XXX If we want to use this code generically we have to
  513. * XXX handle TX ring exhaustion etc.
  514. */
  515. desc = vio_dring_cur(dr);
  516. err = ldc_map_single(port->vio.lp, req_buf, op_len,
  517. desc->cookies, port->ring_cookies,
  518. map_perm);
  519. if (err < 0) {
  520. spin_unlock_irqrestore(&port->vio.lock, flags);
  521. kfree(req_buf);
  522. return err;
  523. }
  524. init_completion(&comp.com);
  525. comp.waiting_for = WAITING_FOR_GEN_CMD;
  526. port->vio.cmp = &comp;
  527. desc->hdr.ack = VIO_ACK_ENABLE;
  528. desc->req_id = port->req_id;
  529. desc->operation = op;
  530. desc->slice = 0;
  531. desc->status = ~0;
  532. desc->offset = 0;
  533. desc->size = op_len;
  534. desc->ncookies = err;
  535. /* This has to be a non-SMP write barrier because we are writing
  536. * to memory which is shared with the peer LDOM.
  537. */
  538. wmb();
  539. desc->hdr.state = VIO_DESC_READY;
  540. err = __vdc_tx_trigger(port);
  541. if (err >= 0) {
  542. port->req_id++;
  543. dr->prod = vio_dring_next(dr, dr->prod);
  544. spin_unlock_irqrestore(&port->vio.lock, flags);
  545. wait_for_completion(&comp.com);
  546. err = comp.err;
  547. } else {
  548. port->vio.cmp = NULL;
  549. spin_unlock_irqrestore(&port->vio.lock, flags);
  550. }
  551. if (map_perm & LDC_MAP_W)
  552. memcpy(buf, req_buf, len);
  553. kfree(req_buf);
  554. return err;
  555. }
  556. static int vdc_alloc_tx_ring(struct vdc_port *port)
  557. {
  558. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  559. unsigned long len, entry_size;
  560. int ncookies;
  561. void *dring;
  562. entry_size = sizeof(struct vio_disk_desc) +
  563. (sizeof(struct ldc_trans_cookie) * port->ring_cookies);
  564. len = (VDC_TX_RING_SIZE * entry_size);
  565. ncookies = VIO_MAX_RING_COOKIES;
  566. dring = ldc_alloc_exp_dring(port->vio.lp, len,
  567. dr->cookies, &ncookies,
  568. (LDC_MAP_SHADOW |
  569. LDC_MAP_DIRECT |
  570. LDC_MAP_RW));
  571. if (IS_ERR(dring))
  572. return PTR_ERR(dring);
  573. dr->base = dring;
  574. dr->entry_size = entry_size;
  575. dr->num_entries = VDC_TX_RING_SIZE;
  576. dr->prod = dr->cons = 0;
  577. dr->pending = VDC_TX_RING_SIZE;
  578. dr->ncookies = ncookies;
  579. return 0;
  580. }
  581. static void vdc_free_tx_ring(struct vdc_port *port)
  582. {
  583. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  584. if (dr->base) {
  585. ldc_free_exp_dring(port->vio.lp, dr->base,
  586. (dr->entry_size * dr->num_entries),
  587. dr->cookies, dr->ncookies);
  588. dr->base = NULL;
  589. dr->entry_size = 0;
  590. dr->num_entries = 0;
  591. dr->pending = 0;
  592. dr->ncookies = 0;
  593. }
  594. }
  595. static int vdc_port_up(struct vdc_port *port)
  596. {
  597. struct vio_completion comp;
  598. init_completion(&comp.com);
  599. comp.err = 0;
  600. comp.waiting_for = WAITING_FOR_LINK_UP;
  601. port->vio.cmp = &comp;
  602. vio_port_up(&port->vio);
  603. wait_for_completion(&comp.com);
  604. return comp.err;
  605. }
  606. static void vdc_port_down(struct vdc_port *port)
  607. {
  608. ldc_disconnect(port->vio.lp);
  609. ldc_unbind(port->vio.lp);
  610. vdc_free_tx_ring(port);
  611. vio_ldc_free(&port->vio);
  612. }
  613. static int probe_disk(struct vdc_port *port)
  614. {
  615. struct request_queue *q;
  616. struct gendisk *g;
  617. int err;
  618. err = vdc_port_up(port);
  619. if (err)
  620. return err;
  621. if (vdc_version_supported(port, 1, 1)) {
  622. /* vdisk_size should be set during the handshake, if it wasn't
  623. * then the underlying disk is reserved by another system
  624. */
  625. if (port->vdisk_size == -1)
  626. return -ENODEV;
  627. } else {
  628. struct vio_disk_geom geom;
  629. err = generic_request(port, VD_OP_GET_DISKGEOM,
  630. &geom, sizeof(geom));
  631. if (err < 0) {
  632. printk(KERN_ERR PFX "VD_OP_GET_DISKGEOM returns "
  633. "error %d\n", err);
  634. return err;
  635. }
  636. port->vdisk_size = ((u64)geom.num_cyl *
  637. (u64)geom.num_hd *
  638. (u64)geom.num_sec);
  639. }
  640. q = blk_init_queue(do_vdc_request, &port->vio.lock);
  641. if (!q) {
  642. printk(KERN_ERR PFX "%s: Could not allocate queue.\n",
  643. port->vio.name);
  644. return -ENOMEM;
  645. }
  646. g = alloc_disk(1 << PARTITION_SHIFT);
  647. if (!g) {
  648. printk(KERN_ERR PFX "%s: Could not allocate gendisk.\n",
  649. port->vio.name);
  650. blk_cleanup_queue(q);
  651. return -ENOMEM;
  652. }
  653. port->disk = g;
  654. /* Each segment in a request is up to an aligned page in size. */
  655. blk_queue_segment_boundary(q, PAGE_SIZE - 1);
  656. blk_queue_max_segment_size(q, PAGE_SIZE);
  657. blk_queue_max_segments(q, port->ring_cookies);
  658. blk_queue_max_hw_sectors(q, port->max_xfer_size);
  659. g->major = vdc_major;
  660. g->first_minor = port->vio.vdev->dev_no << PARTITION_SHIFT;
  661. strcpy(g->disk_name, port->disk_name);
  662. g->fops = &vdc_fops;
  663. g->queue = q;
  664. g->private_data = port;
  665. set_capacity(g, port->vdisk_size);
  666. if (vdc_version_supported(port, 1, 1)) {
  667. switch (port->vdisk_mtype) {
  668. case VD_MEDIA_TYPE_CD:
  669. pr_info(PFX "Virtual CDROM %s\n", port->disk_name);
  670. g->flags |= GENHD_FL_CD;
  671. g->flags |= GENHD_FL_REMOVABLE;
  672. set_disk_ro(g, 1);
  673. break;
  674. case VD_MEDIA_TYPE_DVD:
  675. pr_info(PFX "Virtual DVD %s\n", port->disk_name);
  676. g->flags |= GENHD_FL_CD;
  677. g->flags |= GENHD_FL_REMOVABLE;
  678. set_disk_ro(g, 1);
  679. break;
  680. case VD_MEDIA_TYPE_FIXED:
  681. pr_info(PFX "Virtual Hard disk %s\n", port->disk_name);
  682. break;
  683. }
  684. }
  685. pr_info(PFX "%s: %u sectors (%u MB) protocol %d.%d\n",
  686. g->disk_name,
  687. port->vdisk_size, (port->vdisk_size >> (20 - 9)),
  688. port->vio.ver.major, port->vio.ver.minor);
  689. device_add_disk(&port->vio.vdev->dev, g);
  690. return 0;
  691. }
  692. static struct ldc_channel_config vdc_ldc_cfg = {
  693. .event = vdc_event,
  694. .mtu = 64,
  695. .mode = LDC_MODE_UNRELIABLE,
  696. };
  697. static struct vio_driver_ops vdc_vio_ops = {
  698. .send_attr = vdc_send_attr,
  699. .handle_attr = vdc_handle_attr,
  700. .handshake_complete = vdc_handshake_complete,
  701. };
  702. static void print_version(void)
  703. {
  704. static int version_printed;
  705. if (version_printed++ == 0)
  706. printk(KERN_INFO "%s", version);
  707. }
  708. static int vdc_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  709. {
  710. struct mdesc_handle *hp;
  711. struct vdc_port *port;
  712. int err;
  713. const u64 *ldc_timeout;
  714. print_version();
  715. hp = mdesc_grab();
  716. err = -ENODEV;
  717. if ((vdev->dev_no << PARTITION_SHIFT) & ~(u64)MINORMASK) {
  718. printk(KERN_ERR PFX "Port id [%llu] too large.\n",
  719. vdev->dev_no);
  720. goto err_out_release_mdesc;
  721. }
  722. port = kzalloc(sizeof(*port), GFP_KERNEL);
  723. err = -ENOMEM;
  724. if (!port) {
  725. printk(KERN_ERR PFX "Cannot allocate vdc_port.\n");
  726. goto err_out_release_mdesc;
  727. }
  728. if (vdev->dev_no >= 26)
  729. snprintf(port->disk_name, sizeof(port->disk_name),
  730. VDCBLK_NAME "%c%c",
  731. 'a' + ((int)vdev->dev_no / 26) - 1,
  732. 'a' + ((int)vdev->dev_no % 26));
  733. else
  734. snprintf(port->disk_name, sizeof(port->disk_name),
  735. VDCBLK_NAME "%c", 'a' + ((int)vdev->dev_no % 26));
  736. port->vdisk_size = -1;
  737. /* Actual wall time may be double due to do_generic_file_read() doing
  738. * a readahead I/O first, and once that fails it will try to read a
  739. * single page.
  740. */
  741. ldc_timeout = mdesc_get_property(hp, vdev->mp, "vdc-timeout", NULL);
  742. port->ldc_timeout = ldc_timeout ? *ldc_timeout : 0;
  743. setup_timer(&port->ldc_reset_timer, vdc_ldc_reset_timer,
  744. (unsigned long)port);
  745. INIT_WORK(&port->ldc_reset_work, vdc_ldc_reset_work);
  746. err = vio_driver_init(&port->vio, vdev, VDEV_DISK,
  747. vdc_versions, ARRAY_SIZE(vdc_versions),
  748. &vdc_vio_ops, port->disk_name);
  749. if (err)
  750. goto err_out_free_port;
  751. port->vdisk_block_size = 512;
  752. port->max_xfer_size = ((128 * 1024) / port->vdisk_block_size);
  753. port->ring_cookies = ((port->max_xfer_size *
  754. port->vdisk_block_size) / PAGE_SIZE) + 2;
  755. err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
  756. if (err)
  757. goto err_out_free_port;
  758. err = vdc_alloc_tx_ring(port);
  759. if (err)
  760. goto err_out_free_ldc;
  761. err = probe_disk(port);
  762. if (err)
  763. goto err_out_free_tx_ring;
  764. dev_set_drvdata(&vdev->dev, port);
  765. mdesc_release(hp);
  766. return 0;
  767. err_out_free_tx_ring:
  768. vdc_free_tx_ring(port);
  769. err_out_free_ldc:
  770. vio_ldc_free(&port->vio);
  771. err_out_free_port:
  772. kfree(port);
  773. err_out_release_mdesc:
  774. mdesc_release(hp);
  775. return err;
  776. }
  777. static int vdc_port_remove(struct vio_dev *vdev)
  778. {
  779. struct vdc_port *port = dev_get_drvdata(&vdev->dev);
  780. if (port) {
  781. unsigned long flags;
  782. spin_lock_irqsave(&port->vio.lock, flags);
  783. blk_stop_queue(port->disk->queue);
  784. spin_unlock_irqrestore(&port->vio.lock, flags);
  785. flush_work(&port->ldc_reset_work);
  786. del_timer_sync(&port->ldc_reset_timer);
  787. del_timer_sync(&port->vio.timer);
  788. del_gendisk(port->disk);
  789. blk_cleanup_queue(port->disk->queue);
  790. put_disk(port->disk);
  791. port->disk = NULL;
  792. vdc_free_tx_ring(port);
  793. vio_ldc_free(&port->vio);
  794. dev_set_drvdata(&vdev->dev, NULL);
  795. kfree(port);
  796. }
  797. return 0;
  798. }
  799. static void vdc_requeue_inflight(struct vdc_port *port)
  800. {
  801. struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
  802. u32 idx;
  803. for (idx = dr->cons; idx != dr->prod; idx = vio_dring_next(dr, idx)) {
  804. struct vio_disk_desc *desc = vio_dring_entry(dr, idx);
  805. struct vdc_req_entry *rqe = &port->rq_arr[idx];
  806. struct request *req;
  807. ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
  808. desc->hdr.state = VIO_DESC_FREE;
  809. dr->cons = vio_dring_next(dr, idx);
  810. req = rqe->req;
  811. if (req == NULL) {
  812. vdc_end_special(port, desc);
  813. continue;
  814. }
  815. rqe->req = NULL;
  816. blk_requeue_request(port->disk->queue, req);
  817. }
  818. }
  819. static void vdc_queue_drain(struct vdc_port *port)
  820. {
  821. struct request *req;
  822. while ((req = blk_fetch_request(port->disk->queue)) != NULL)
  823. __blk_end_request_all(req, -EIO);
  824. }
  825. static void vdc_ldc_reset_timer(unsigned long _arg)
  826. {
  827. struct vdc_port *port = (struct vdc_port *) _arg;
  828. struct vio_driver_state *vio = &port->vio;
  829. unsigned long flags;
  830. spin_lock_irqsave(&vio->lock, flags);
  831. if (!(port->vio.hs_state & VIO_HS_COMPLETE)) {
  832. pr_warn(PFX "%s ldc down %llu seconds, draining queue\n",
  833. port->disk_name, port->ldc_timeout);
  834. vdc_queue_drain(port);
  835. vdc_blk_queue_start(port);
  836. }
  837. spin_unlock_irqrestore(&vio->lock, flags);
  838. }
  839. static void vdc_ldc_reset_work(struct work_struct *work)
  840. {
  841. struct vdc_port *port;
  842. struct vio_driver_state *vio;
  843. unsigned long flags;
  844. port = container_of(work, struct vdc_port, ldc_reset_work);
  845. vio = &port->vio;
  846. spin_lock_irqsave(&vio->lock, flags);
  847. vdc_ldc_reset(port);
  848. spin_unlock_irqrestore(&vio->lock, flags);
  849. }
  850. static void vdc_ldc_reset(struct vdc_port *port)
  851. {
  852. int err;
  853. assert_spin_locked(&port->vio.lock);
  854. pr_warn(PFX "%s ldc link reset\n", port->disk_name);
  855. blk_stop_queue(port->disk->queue);
  856. vdc_requeue_inflight(port);
  857. vdc_port_down(port);
  858. err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
  859. if (err) {
  860. pr_err(PFX "%s vio_ldc_alloc:%d\n", port->disk_name, err);
  861. return;
  862. }
  863. err = vdc_alloc_tx_ring(port);
  864. if (err) {
  865. pr_err(PFX "%s vio_alloc_tx_ring:%d\n", port->disk_name, err);
  866. goto err_free_ldc;
  867. }
  868. if (port->ldc_timeout)
  869. mod_timer(&port->ldc_reset_timer,
  870. round_jiffies(jiffies + HZ * port->ldc_timeout));
  871. mod_timer(&port->vio.timer, round_jiffies(jiffies + HZ));
  872. return;
  873. err_free_ldc:
  874. vio_ldc_free(&port->vio);
  875. }
  876. static const struct vio_device_id vdc_port_match[] = {
  877. {
  878. .type = "vdc-port",
  879. },
  880. {},
  881. };
  882. MODULE_DEVICE_TABLE(vio, vdc_port_match);
  883. static struct vio_driver vdc_port_driver = {
  884. .id_table = vdc_port_match,
  885. .probe = vdc_port_probe,
  886. .remove = vdc_port_remove,
  887. .name = "vdc_port",
  888. };
  889. static int __init vdc_init(void)
  890. {
  891. int err;
  892. sunvdc_wq = alloc_workqueue("sunvdc", 0, 0);
  893. if (!sunvdc_wq)
  894. return -ENOMEM;
  895. err = register_blkdev(0, VDCBLK_NAME);
  896. if (err < 0)
  897. goto out_free_wq;
  898. vdc_major = err;
  899. err = vio_register_driver(&vdc_port_driver);
  900. if (err)
  901. goto out_unregister_blkdev;
  902. return 0;
  903. out_unregister_blkdev:
  904. unregister_blkdev(vdc_major, VDCBLK_NAME);
  905. vdc_major = 0;
  906. out_free_wq:
  907. destroy_workqueue(sunvdc_wq);
  908. return err;
  909. }
  910. static void __exit vdc_exit(void)
  911. {
  912. vio_unregister_driver(&vdc_port_driver);
  913. unregister_blkdev(vdc_major, VDCBLK_NAME);
  914. destroy_workqueue(sunvdc_wq);
  915. }
  916. module_init(vdc_init);
  917. module_exit(vdc_exit);