xref: /openbmc/linux/drivers/block/ps3disk.c (revision 6d247e4d264961aa3b871290f9b11a48d5a567f2)
1935912c5SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2c6131fa5SGeert Uytterhoeven /*
3c6131fa5SGeert Uytterhoeven  * PS3 Disk Storage Driver
4c6131fa5SGeert Uytterhoeven  *
5c6131fa5SGeert Uytterhoeven  * Copyright (C) 2007 Sony Computer Entertainment Inc.
6c6131fa5SGeert Uytterhoeven  * Copyright 2007 Sony Corp.
7c6131fa5SGeert Uytterhoeven  */
8c6131fa5SGeert Uytterhoeven 
9c6131fa5SGeert Uytterhoeven #include <linux/ata.h>
10fab1adcfSJens Axboe #include <linux/blk-mq.h>
115a0e3ad6STejun Heo #include <linux/slab.h>
120c8d44f2SPaul Gortmaker #include <linux/module.h>
13c6131fa5SGeert Uytterhoeven 
14c6131fa5SGeert Uytterhoeven #include <asm/lv1call.h>
15c6131fa5SGeert Uytterhoeven #include <asm/ps3stor.h>
16c6131fa5SGeert Uytterhoeven #include <asm/firmware.h>
17c6131fa5SGeert Uytterhoeven 
18c6131fa5SGeert Uytterhoeven 
19c6131fa5SGeert Uytterhoeven #define DEVICE_NAME		"ps3disk"
20c6131fa5SGeert Uytterhoeven 
21c6131fa5SGeert Uytterhoeven #define BOUNCE_SIZE		(64*1024)
22c6131fa5SGeert Uytterhoeven 
23c6131fa5SGeert Uytterhoeven #define PS3DISK_MAX_DISKS	16
24c6131fa5SGeert Uytterhoeven #define PS3DISK_MINORS		16
25c6131fa5SGeert Uytterhoeven 
26c6131fa5SGeert Uytterhoeven 
27c6131fa5SGeert Uytterhoeven #define PS3DISK_NAME		"ps3d%c"
28c6131fa5SGeert Uytterhoeven 
29c6131fa5SGeert Uytterhoeven 
30c6131fa5SGeert Uytterhoeven struct ps3disk_private {
31c6131fa5SGeert Uytterhoeven 	spinlock_t lock;		/* Request queue spinlock */
32c6131fa5SGeert Uytterhoeven 	struct request_queue *queue;
33fab1adcfSJens Axboe 	struct blk_mq_tag_set tag_set;
34c6131fa5SGeert Uytterhoeven 	struct gendisk *gendisk;
35c6131fa5SGeert Uytterhoeven 	unsigned int blocking_factor;
36c6131fa5SGeert Uytterhoeven 	struct request *req;
37c6131fa5SGeert Uytterhoeven 	u64 raw_capacity;
38c6131fa5SGeert Uytterhoeven 	unsigned char model[ATA_ID_PROD_LEN+1];
39c6131fa5SGeert Uytterhoeven };
40c6131fa5SGeert Uytterhoeven 
41c6131fa5SGeert Uytterhoeven 
42c6131fa5SGeert Uytterhoeven #define LV1_STORAGE_SEND_ATA_COMMAND	(2)
43c6131fa5SGeert Uytterhoeven #define LV1_STORAGE_ATA_HDDOUT		(0x23)
44c6131fa5SGeert Uytterhoeven 
45c6131fa5SGeert Uytterhoeven struct lv1_ata_cmnd_block {
46c6131fa5SGeert Uytterhoeven 	u16	features;
47c6131fa5SGeert Uytterhoeven 	u16	sector_count;
48c6131fa5SGeert Uytterhoeven 	u16	LBA_low;
49c6131fa5SGeert Uytterhoeven 	u16	LBA_mid;
50c6131fa5SGeert Uytterhoeven 	u16	LBA_high;
51c6131fa5SGeert Uytterhoeven 	u8	device;
52c6131fa5SGeert Uytterhoeven 	u8	command;
53c6131fa5SGeert Uytterhoeven 	u32	is_ext;
54c6131fa5SGeert Uytterhoeven 	u32	proto;
55c6131fa5SGeert Uytterhoeven 	u32	in_out;
56c6131fa5SGeert Uytterhoeven 	u32	size;
57c6131fa5SGeert Uytterhoeven 	u64	buffer;
58c6131fa5SGeert Uytterhoeven 	u32	arglen;
59c6131fa5SGeert Uytterhoeven };
60c6131fa5SGeert Uytterhoeven 
61c6131fa5SGeert Uytterhoeven enum lv1_ata_proto {
62c6131fa5SGeert Uytterhoeven 	NON_DATA_PROTO     = 0,
63c6131fa5SGeert Uytterhoeven 	PIO_DATA_IN_PROTO  = 1,
64c6131fa5SGeert Uytterhoeven 	PIO_DATA_OUT_PROTO = 2,
65c6131fa5SGeert Uytterhoeven 	DMA_PROTO = 3
66c6131fa5SGeert Uytterhoeven };
67c6131fa5SGeert Uytterhoeven 
68c6131fa5SGeert Uytterhoeven enum lv1_ata_in_out {
69c6131fa5SGeert Uytterhoeven 	DIR_WRITE = 0,			/* memory -> device */
70c6131fa5SGeert Uytterhoeven 	DIR_READ = 1			/* device -> memory */
71c6131fa5SGeert Uytterhoeven };
72c6131fa5SGeert Uytterhoeven 
73c6131fa5SGeert Uytterhoeven static int ps3disk_major;
74c6131fa5SGeert Uytterhoeven 
75c6131fa5SGeert Uytterhoeven 
7683d5cde4SAlexey Dobriyan static const struct block_device_operations ps3disk_fops = {
77c6131fa5SGeert Uytterhoeven 	.owner		= THIS_MODULE,
78c6131fa5SGeert Uytterhoeven };
79c6131fa5SGeert Uytterhoeven 
80c6131fa5SGeert Uytterhoeven 
81c6131fa5SGeert Uytterhoeven static void ps3disk_scatter_gather(struct ps3_storage_device *dev,
82c6131fa5SGeert Uytterhoeven 				   struct request *req, int gather)
83c6131fa5SGeert Uytterhoeven {
84c6131fa5SGeert Uytterhoeven 	unsigned int offset = 0;
855705f702SNeilBrown 	struct req_iterator iter;
867988613bSKent Overstreet 	struct bio_vec bvec;
875705f702SNeilBrown 	unsigned int i = 0;
88c6131fa5SGeert Uytterhoeven 	size_t size;
89c6131fa5SGeert Uytterhoeven 	void *buf;
90c6131fa5SGeert Uytterhoeven 
915705f702SNeilBrown 	rq_for_each_segment(bvec, req, iter) {
925705f702SNeilBrown 		unsigned long flags;
9372deb455SChristoph Hellwig 		dev_dbg(&dev->sbd.core, "%s:%u: bio %u: %u sectors from %llu\n",
94458b76edSKent Overstreet 			__func__, __LINE__, i, bio_sectors(iter.bio),
95458b76edSKent Overstreet 			iter.bio->bi_iter.bi_sector);
965705f702SNeilBrown 
977988613bSKent Overstreet 		size = bvec.bv_len;
987988613bSKent Overstreet 		buf = bvec_kmap_irq(&bvec, &flags);
99c6131fa5SGeert Uytterhoeven 		if (gather)
100c6131fa5SGeert Uytterhoeven 			memcpy(dev->bounce_buf+offset, buf, size);
101c6131fa5SGeert Uytterhoeven 		else
102c6131fa5SGeert Uytterhoeven 			memcpy(buf, dev->bounce_buf+offset, size);
103c6131fa5SGeert Uytterhoeven 		offset += size;
1047988613bSKent Overstreet 		flush_kernel_dcache_page(bvec.bv_page);
10593055c31SDan Carpenter 		bvec_kunmap_irq(buf, &flags);
106c6131fa5SGeert Uytterhoeven 		i++;
107c6131fa5SGeert Uytterhoeven 	}
108c6131fa5SGeert Uytterhoeven }
109c6131fa5SGeert Uytterhoeven 
110fab1adcfSJens Axboe static blk_status_t ps3disk_submit_request_sg(struct ps3_storage_device *dev,
111c6131fa5SGeert Uytterhoeven 					      struct request *req)
112c6131fa5SGeert Uytterhoeven {
11303fa68c2SGeert Uytterhoeven 	struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
114c6131fa5SGeert Uytterhoeven 	int write = rq_data_dir(req), res;
115c6131fa5SGeert Uytterhoeven 	const char *op = write ? "write" : "read";
116c6131fa5SGeert Uytterhoeven 	u64 start_sector, sectors;
117c6131fa5SGeert Uytterhoeven 	unsigned int region_id = dev->regions[dev->region_idx].id;
118c6131fa5SGeert Uytterhoeven 
119c6131fa5SGeert Uytterhoeven #ifdef DEBUG
120c6131fa5SGeert Uytterhoeven 	unsigned int n = 0;
1217988613bSKent Overstreet 	struct bio_vec bv;
1225705f702SNeilBrown 	struct req_iterator iter;
123c6131fa5SGeert Uytterhoeven 
1245705f702SNeilBrown 	rq_for_each_segment(bv, req, iter)
125c6131fa5SGeert Uytterhoeven 		n++;
126c6131fa5SGeert Uytterhoeven 	dev_dbg(&dev->sbd.core,
12783096ebfSTejun Heo 		"%s:%u: %s req has %u bvecs for %u sectors\n",
12883096ebfSTejun Heo 		__func__, __LINE__, op, n, blk_rq_sectors(req));
129c6131fa5SGeert Uytterhoeven #endif
130c6131fa5SGeert Uytterhoeven 
13183096ebfSTejun Heo 	start_sector = blk_rq_pos(req) * priv->blocking_factor;
13283096ebfSTejun Heo 	sectors = blk_rq_sectors(req) * priv->blocking_factor;
133e377c6e2SStephen Rothwell 	dev_dbg(&dev->sbd.core, "%s:%u: %s %llu sectors starting at %llu\n",
134c6131fa5SGeert Uytterhoeven 		__func__, __LINE__, op, sectors, start_sector);
135c6131fa5SGeert Uytterhoeven 
136c6131fa5SGeert Uytterhoeven 	if (write) {
137c6131fa5SGeert Uytterhoeven 		ps3disk_scatter_gather(dev, req, 1);
138c6131fa5SGeert Uytterhoeven 
139c6131fa5SGeert Uytterhoeven 		res = lv1_storage_write(dev->sbd.dev_id, region_id,
140c6131fa5SGeert Uytterhoeven 					start_sector, sectors, 0,
141c6131fa5SGeert Uytterhoeven 					dev->bounce_lpar, &dev->tag);
142c6131fa5SGeert Uytterhoeven 	} else {
143c6131fa5SGeert Uytterhoeven 		res = lv1_storage_read(dev->sbd.dev_id, region_id,
144c6131fa5SGeert Uytterhoeven 				       start_sector, sectors, 0,
145c6131fa5SGeert Uytterhoeven 				       dev->bounce_lpar, &dev->tag);
146c6131fa5SGeert Uytterhoeven 	}
147c6131fa5SGeert Uytterhoeven 	if (res) {
148c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core, "%s:%u: %s failed %d\n", __func__,
149c6131fa5SGeert Uytterhoeven 			__LINE__, op, res);
150fab1adcfSJens Axboe 		return BLK_STS_IOERR;
151c6131fa5SGeert Uytterhoeven 	}
152c6131fa5SGeert Uytterhoeven 
153c6131fa5SGeert Uytterhoeven 	priv->req = req;
154fab1adcfSJens Axboe 	return BLK_STS_OK;
155c6131fa5SGeert Uytterhoeven }
156c6131fa5SGeert Uytterhoeven 
157fab1adcfSJens Axboe static blk_status_t ps3disk_submit_flush_request(struct ps3_storage_device *dev,
158c6131fa5SGeert Uytterhoeven 						 struct request *req)
159c6131fa5SGeert Uytterhoeven {
16003fa68c2SGeert Uytterhoeven 	struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
161c6131fa5SGeert Uytterhoeven 	u64 res;
162c6131fa5SGeert Uytterhoeven 
163c6131fa5SGeert Uytterhoeven 	dev_dbg(&dev->sbd.core, "%s:%u: flush request\n", __func__, __LINE__);
164c6131fa5SGeert Uytterhoeven 
165c6131fa5SGeert Uytterhoeven 	res = lv1_storage_send_device_command(dev->sbd.dev_id,
166c6131fa5SGeert Uytterhoeven 					      LV1_STORAGE_ATA_HDDOUT, 0, 0, 0,
167c6131fa5SGeert Uytterhoeven 					      0, &dev->tag);
168c6131fa5SGeert Uytterhoeven 	if (res) {
169e377c6e2SStephen Rothwell 		dev_err(&dev->sbd.core, "%s:%u: sync cache failed 0x%llx\n",
170c6131fa5SGeert Uytterhoeven 			__func__, __LINE__, res);
171fab1adcfSJens Axboe 		return BLK_STS_IOERR;
172c6131fa5SGeert Uytterhoeven 	}
173c6131fa5SGeert Uytterhoeven 
174c6131fa5SGeert Uytterhoeven 	priv->req = req;
175fab1adcfSJens Axboe 	return BLK_STS_OK;
176c6131fa5SGeert Uytterhoeven }
177c6131fa5SGeert Uytterhoeven 
178fab1adcfSJens Axboe static blk_status_t ps3disk_do_request(struct ps3_storage_device *dev,
179fab1adcfSJens Axboe 				       struct request *req)
180c6131fa5SGeert Uytterhoeven {
181c6131fa5SGeert Uytterhoeven 	dev_dbg(&dev->sbd.core, "%s:%u\n", __func__, __LINE__);
182c6131fa5SGeert Uytterhoeven 
183aebf526bSChristoph Hellwig 	switch (req_op(req)) {
184aebf526bSChristoph Hellwig 	case REQ_OP_FLUSH:
185fab1adcfSJens Axboe 		return ps3disk_submit_flush_request(dev, req);
186aebf526bSChristoph Hellwig 	case REQ_OP_READ:
187aebf526bSChristoph Hellwig 	case REQ_OP_WRITE:
188fab1adcfSJens Axboe 		return ps3disk_submit_request_sg(dev, req);
189aebf526bSChristoph Hellwig 	default:
190c6131fa5SGeert Uytterhoeven 		blk_dump_rq_flags(req, DEVICE_NAME " bad request");
191fab1adcfSJens Axboe 		return BLK_STS_IOERR;
192c6131fa5SGeert Uytterhoeven 	}
193c6131fa5SGeert Uytterhoeven }
194c6131fa5SGeert Uytterhoeven 
195fab1adcfSJens Axboe static blk_status_t ps3disk_queue_rq(struct blk_mq_hw_ctx *hctx,
196fab1adcfSJens Axboe 				     const struct blk_mq_queue_data *bd)
197c6131fa5SGeert Uytterhoeven {
198fab1adcfSJens Axboe 	struct request_queue *q = hctx->queue;
199c6131fa5SGeert Uytterhoeven 	struct ps3_storage_device *dev = q->queuedata;
20003fa68c2SGeert Uytterhoeven 	struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
201fab1adcfSJens Axboe 	blk_status_t ret;
202c6131fa5SGeert Uytterhoeven 
203fab1adcfSJens Axboe 	blk_mq_start_request(bd->rq);
204c6131fa5SGeert Uytterhoeven 
205fab1adcfSJens Axboe 	spin_lock_irq(&priv->lock);
206fab1adcfSJens Axboe 	ret = ps3disk_do_request(dev, bd->rq);
207fab1adcfSJens Axboe 	spin_unlock_irq(&priv->lock);
208fab1adcfSJens Axboe 
209fab1adcfSJens Axboe 	return ret;
210c6131fa5SGeert Uytterhoeven }
211c6131fa5SGeert Uytterhoeven 
212c6131fa5SGeert Uytterhoeven static irqreturn_t ps3disk_interrupt(int irq, void *data)
213c6131fa5SGeert Uytterhoeven {
214c6131fa5SGeert Uytterhoeven 	struct ps3_storage_device *dev = data;
215c6131fa5SGeert Uytterhoeven 	struct ps3disk_private *priv;
216c6131fa5SGeert Uytterhoeven 	struct request *req;
2172a842acaSChristoph Hellwig 	int res, read;
2182a842acaSChristoph Hellwig 	blk_status_t error;
219c6131fa5SGeert Uytterhoeven 	u64 tag, status;
220c6131fa5SGeert Uytterhoeven 	const char *op;
221c6131fa5SGeert Uytterhoeven 
222c6131fa5SGeert Uytterhoeven 	res = lv1_storage_get_async_status(dev->sbd.dev_id, &tag, &status);
223c6131fa5SGeert Uytterhoeven 
224c6131fa5SGeert Uytterhoeven 	if (tag != dev->tag)
225c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core,
226e377c6e2SStephen Rothwell 			"%s:%u: tag mismatch, got %llx, expected %llx\n",
227c6131fa5SGeert Uytterhoeven 			__func__, __LINE__, tag, dev->tag);
228c6131fa5SGeert Uytterhoeven 
229c6131fa5SGeert Uytterhoeven 	if (res) {
230e377c6e2SStephen Rothwell 		dev_err(&dev->sbd.core, "%s:%u: res=%d status=0x%llx\n",
231c6131fa5SGeert Uytterhoeven 			__func__, __LINE__, res, status);
232c6131fa5SGeert Uytterhoeven 		return IRQ_HANDLED;
233c6131fa5SGeert Uytterhoeven 	}
234c6131fa5SGeert Uytterhoeven 
23503fa68c2SGeert Uytterhoeven 	priv = ps3_system_bus_get_drvdata(&dev->sbd);
236c6131fa5SGeert Uytterhoeven 	req = priv->req;
237c6131fa5SGeert Uytterhoeven 	if (!req) {
238c6131fa5SGeert Uytterhoeven 		dev_dbg(&dev->sbd.core,
239c6131fa5SGeert Uytterhoeven 			"%s:%u non-block layer request completed\n", __func__,
240c6131fa5SGeert Uytterhoeven 			__LINE__);
241c6131fa5SGeert Uytterhoeven 		dev->lv1_status = status;
242c6131fa5SGeert Uytterhoeven 		complete(&dev->done);
243c6131fa5SGeert Uytterhoeven 		return IRQ_HANDLED;
244c6131fa5SGeert Uytterhoeven 	}
245c6131fa5SGeert Uytterhoeven 
2463a5e02ceSMike Christie 	if (req_op(req) == REQ_OP_FLUSH) {
247c6131fa5SGeert Uytterhoeven 		read = 0;
248c6131fa5SGeert Uytterhoeven 		op = "flush";
249c6131fa5SGeert Uytterhoeven 	} else {
250c6131fa5SGeert Uytterhoeven 		read = !rq_data_dir(req);
251c6131fa5SGeert Uytterhoeven 		op = read ? "read" : "write";
252c6131fa5SGeert Uytterhoeven 	}
253c6131fa5SGeert Uytterhoeven 	if (status) {
254e377c6e2SStephen Rothwell 		dev_dbg(&dev->sbd.core, "%s:%u: %s failed 0x%llx\n", __func__,
255c6131fa5SGeert Uytterhoeven 			__LINE__, op, status);
2562a842acaSChristoph Hellwig 		error = BLK_STS_IOERR;
257c6131fa5SGeert Uytterhoeven 	} else {
258c6131fa5SGeert Uytterhoeven 		dev_dbg(&dev->sbd.core, "%s:%u: %s completed\n", __func__,
259c6131fa5SGeert Uytterhoeven 			__LINE__, op);
260f01ab252SKiyoshi Ueda 		error = 0;
261c6131fa5SGeert Uytterhoeven 		if (read)
262c6131fa5SGeert Uytterhoeven 			ps3disk_scatter_gather(dev, req, 0);
263c6131fa5SGeert Uytterhoeven 	}
264c6131fa5SGeert Uytterhoeven 
265c6131fa5SGeert Uytterhoeven 	spin_lock(&priv->lock);
266c6131fa5SGeert Uytterhoeven 	priv->req = NULL;
267fab1adcfSJens Axboe 	blk_mq_end_request(req, error);
268c6131fa5SGeert Uytterhoeven 	spin_unlock(&priv->lock);
269c6131fa5SGeert Uytterhoeven 
270fab1adcfSJens Axboe 	blk_mq_run_hw_queues(priv->queue, true);
271c6131fa5SGeert Uytterhoeven 	return IRQ_HANDLED;
272c6131fa5SGeert Uytterhoeven }
273c6131fa5SGeert Uytterhoeven 
274c6131fa5SGeert Uytterhoeven static int ps3disk_sync_cache(struct ps3_storage_device *dev)
275c6131fa5SGeert Uytterhoeven {
276c6131fa5SGeert Uytterhoeven 	u64 res;
277c6131fa5SGeert Uytterhoeven 
278c6131fa5SGeert Uytterhoeven 	dev_dbg(&dev->sbd.core, "%s:%u: sync cache\n", __func__, __LINE__);
279c6131fa5SGeert Uytterhoeven 
280c6131fa5SGeert Uytterhoeven 	res = ps3stor_send_command(dev, LV1_STORAGE_ATA_HDDOUT, 0, 0, 0, 0);
281c6131fa5SGeert Uytterhoeven 	if (res) {
282e377c6e2SStephen Rothwell 		dev_err(&dev->sbd.core, "%s:%u: sync cache failed 0x%llx\n",
283c6131fa5SGeert Uytterhoeven 			__func__, __LINE__, res);
284c6131fa5SGeert Uytterhoeven 		return -EIO;
285c6131fa5SGeert Uytterhoeven 	}
286c6131fa5SGeert Uytterhoeven 	return 0;
287c6131fa5SGeert Uytterhoeven }
288c6131fa5SGeert Uytterhoeven 
289c6131fa5SGeert Uytterhoeven 
290c6131fa5SGeert Uytterhoeven /* ATA helpers copied from drivers/ata/libata-core.c */
291c6131fa5SGeert Uytterhoeven 
292c6131fa5SGeert Uytterhoeven static void swap_buf_le16(u16 *buf, unsigned int buf_words)
293c6131fa5SGeert Uytterhoeven {
294c6131fa5SGeert Uytterhoeven #ifdef __BIG_ENDIAN
295c6131fa5SGeert Uytterhoeven 	unsigned int i;
296c6131fa5SGeert Uytterhoeven 
297c6131fa5SGeert Uytterhoeven 	for (i = 0; i < buf_words; i++)
298c6131fa5SGeert Uytterhoeven 		buf[i] = le16_to_cpu(buf[i]);
299c6131fa5SGeert Uytterhoeven #endif /* __BIG_ENDIAN */
300c6131fa5SGeert Uytterhoeven }
301c6131fa5SGeert Uytterhoeven 
302c6131fa5SGeert Uytterhoeven static u64 ata_id_n_sectors(const u16 *id)
303c6131fa5SGeert Uytterhoeven {
304c6131fa5SGeert Uytterhoeven 	if (ata_id_has_lba(id)) {
305c6131fa5SGeert Uytterhoeven 		if (ata_id_has_lba48(id))
306c6131fa5SGeert Uytterhoeven 			return ata_id_u64(id, 100);
307c6131fa5SGeert Uytterhoeven 		else
308c6131fa5SGeert Uytterhoeven 			return ata_id_u32(id, 60);
309c6131fa5SGeert Uytterhoeven 	} else {
310c6131fa5SGeert Uytterhoeven 		if (ata_id_current_chs_valid(id))
311c6131fa5SGeert Uytterhoeven 			return ata_id_u32(id, 57);
312c6131fa5SGeert Uytterhoeven 		else
313c6131fa5SGeert Uytterhoeven 			return id[1] * id[3] * id[6];
314c6131fa5SGeert Uytterhoeven 	}
315c6131fa5SGeert Uytterhoeven }
316c6131fa5SGeert Uytterhoeven 
317c6131fa5SGeert Uytterhoeven static void ata_id_string(const u16 *id, unsigned char *s, unsigned int ofs,
318c6131fa5SGeert Uytterhoeven 			  unsigned int len)
319c6131fa5SGeert Uytterhoeven {
320c6131fa5SGeert Uytterhoeven 	unsigned int c;
321c6131fa5SGeert Uytterhoeven 
322c6131fa5SGeert Uytterhoeven 	while (len > 0) {
323c6131fa5SGeert Uytterhoeven 		c = id[ofs] >> 8;
324c6131fa5SGeert Uytterhoeven 		*s = c;
325c6131fa5SGeert Uytterhoeven 		s++;
326c6131fa5SGeert Uytterhoeven 
327c6131fa5SGeert Uytterhoeven 		c = id[ofs] & 0xff;
328c6131fa5SGeert Uytterhoeven 		*s = c;
329c6131fa5SGeert Uytterhoeven 		s++;
330c6131fa5SGeert Uytterhoeven 
331c6131fa5SGeert Uytterhoeven 		ofs++;
332c6131fa5SGeert Uytterhoeven 		len -= 2;
333c6131fa5SGeert Uytterhoeven 	}
334c6131fa5SGeert Uytterhoeven }
335c6131fa5SGeert Uytterhoeven 
336c6131fa5SGeert Uytterhoeven static void ata_id_c_string(const u16 *id, unsigned char *s, unsigned int ofs,
337c6131fa5SGeert Uytterhoeven 			    unsigned int len)
338c6131fa5SGeert Uytterhoeven {
339c6131fa5SGeert Uytterhoeven 	unsigned char *p;
340c6131fa5SGeert Uytterhoeven 
341c6131fa5SGeert Uytterhoeven 	WARN_ON(!(len & 1));
342c6131fa5SGeert Uytterhoeven 
343c6131fa5SGeert Uytterhoeven 	ata_id_string(id, s, ofs, len - 1);
344c6131fa5SGeert Uytterhoeven 
345c6131fa5SGeert Uytterhoeven 	p = s + strnlen(s, len - 1);
346c6131fa5SGeert Uytterhoeven 	while (p > s && p[-1] == ' ')
347c6131fa5SGeert Uytterhoeven 		p--;
348c6131fa5SGeert Uytterhoeven 	*p = '\0';
349c6131fa5SGeert Uytterhoeven }
350c6131fa5SGeert Uytterhoeven 
351c6131fa5SGeert Uytterhoeven static int ps3disk_identify(struct ps3_storage_device *dev)
352c6131fa5SGeert Uytterhoeven {
35303fa68c2SGeert Uytterhoeven 	struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
354c6131fa5SGeert Uytterhoeven 	struct lv1_ata_cmnd_block ata_cmnd;
355c6131fa5SGeert Uytterhoeven 	u16 *id = dev->bounce_buf;
356c6131fa5SGeert Uytterhoeven 	u64 res;
357c6131fa5SGeert Uytterhoeven 
358c6131fa5SGeert Uytterhoeven 	dev_dbg(&dev->sbd.core, "%s:%u: identify disk\n", __func__, __LINE__);
359c6131fa5SGeert Uytterhoeven 
360c6131fa5SGeert Uytterhoeven 	memset(&ata_cmnd, 0, sizeof(struct lv1_ata_cmnd_block));
361c6131fa5SGeert Uytterhoeven 	ata_cmnd.command = ATA_CMD_ID_ATA;
362c6131fa5SGeert Uytterhoeven 	ata_cmnd.sector_count = 1;
363c6131fa5SGeert Uytterhoeven 	ata_cmnd.size = ata_cmnd.arglen = ATA_ID_WORDS * 2;
364c6131fa5SGeert Uytterhoeven 	ata_cmnd.buffer = dev->bounce_lpar;
365c6131fa5SGeert Uytterhoeven 	ata_cmnd.proto = PIO_DATA_IN_PROTO;
366c6131fa5SGeert Uytterhoeven 	ata_cmnd.in_out = DIR_READ;
367c6131fa5SGeert Uytterhoeven 
368c6131fa5SGeert Uytterhoeven 	res = ps3stor_send_command(dev, LV1_STORAGE_SEND_ATA_COMMAND,
369c6131fa5SGeert Uytterhoeven 				   ps3_mm_phys_to_lpar(__pa(&ata_cmnd)),
370c6131fa5SGeert Uytterhoeven 				   sizeof(ata_cmnd), ata_cmnd.buffer,
371c6131fa5SGeert Uytterhoeven 				   ata_cmnd.arglen);
372c6131fa5SGeert Uytterhoeven 	if (res) {
373e377c6e2SStephen Rothwell 		dev_err(&dev->sbd.core, "%s:%u: identify disk failed 0x%llx\n",
374c6131fa5SGeert Uytterhoeven 			__func__, __LINE__, res);
375c6131fa5SGeert Uytterhoeven 		return -EIO;
376c6131fa5SGeert Uytterhoeven 	}
377c6131fa5SGeert Uytterhoeven 
378c6131fa5SGeert Uytterhoeven 	swap_buf_le16(id, ATA_ID_WORDS);
379c6131fa5SGeert Uytterhoeven 
380c6131fa5SGeert Uytterhoeven 	/* All we're interested in are raw capacity and model name */
381c6131fa5SGeert Uytterhoeven 	priv->raw_capacity = ata_id_n_sectors(id);
382c6131fa5SGeert Uytterhoeven 	ata_id_c_string(id, priv->model, ATA_ID_PROD, sizeof(priv->model));
383c6131fa5SGeert Uytterhoeven 	return 0;
384c6131fa5SGeert Uytterhoeven }
385c6131fa5SGeert Uytterhoeven 
386c6131fa5SGeert Uytterhoeven static unsigned long ps3disk_mask;
387c6131fa5SGeert Uytterhoeven 
388c6131fa5SGeert Uytterhoeven static DEFINE_MUTEX(ps3disk_mask_mutex);
389c6131fa5SGeert Uytterhoeven 
390fab1adcfSJens Axboe static const struct blk_mq_ops ps3disk_mq_ops = {
391fab1adcfSJens Axboe 	.queue_rq	= ps3disk_queue_rq,
392fab1adcfSJens Axboe };
393fab1adcfSJens Axboe 
3948d85fce7SGreg Kroah-Hartman static int ps3disk_probe(struct ps3_system_bus_device *_dev)
395c6131fa5SGeert Uytterhoeven {
396c6131fa5SGeert Uytterhoeven 	struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
397c6131fa5SGeert Uytterhoeven 	struct ps3disk_private *priv;
398c6131fa5SGeert Uytterhoeven 	int error;
399c6131fa5SGeert Uytterhoeven 	unsigned int devidx;
400c6131fa5SGeert Uytterhoeven 	struct request_queue *queue;
401c6131fa5SGeert Uytterhoeven 	struct gendisk *gendisk;
402c6131fa5SGeert Uytterhoeven 
403c6131fa5SGeert Uytterhoeven 	if (dev->blk_size < 512) {
404c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core,
405e377c6e2SStephen Rothwell 			"%s:%u: cannot handle block size %llu\n", __func__,
406c6131fa5SGeert Uytterhoeven 			__LINE__, dev->blk_size);
407c6131fa5SGeert Uytterhoeven 		return -EINVAL;
408c6131fa5SGeert Uytterhoeven 	}
409c6131fa5SGeert Uytterhoeven 
410c6131fa5SGeert Uytterhoeven 	BUILD_BUG_ON(PS3DISK_MAX_DISKS > BITS_PER_LONG);
411c6131fa5SGeert Uytterhoeven 	mutex_lock(&ps3disk_mask_mutex);
412c6131fa5SGeert Uytterhoeven 	devidx = find_first_zero_bit(&ps3disk_mask, PS3DISK_MAX_DISKS);
413c6131fa5SGeert Uytterhoeven 	if (devidx >= PS3DISK_MAX_DISKS) {
414c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core, "%s:%u: Too many disks\n", __func__,
415c6131fa5SGeert Uytterhoeven 			__LINE__);
416c6131fa5SGeert Uytterhoeven 		mutex_unlock(&ps3disk_mask_mutex);
417c6131fa5SGeert Uytterhoeven 		return -ENOSPC;
418c6131fa5SGeert Uytterhoeven 	}
419c6131fa5SGeert Uytterhoeven 	__set_bit(devidx, &ps3disk_mask);
420c6131fa5SGeert Uytterhoeven 	mutex_unlock(&ps3disk_mask_mutex);
421c6131fa5SGeert Uytterhoeven 
422c6131fa5SGeert Uytterhoeven 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
423c6131fa5SGeert Uytterhoeven 	if (!priv) {
424c6131fa5SGeert Uytterhoeven 		error = -ENOMEM;
425c6131fa5SGeert Uytterhoeven 		goto fail;
426c6131fa5SGeert Uytterhoeven 	}
427c6131fa5SGeert Uytterhoeven 
42803fa68c2SGeert Uytterhoeven 	ps3_system_bus_set_drvdata(_dev, priv);
429c6131fa5SGeert Uytterhoeven 	spin_lock_init(&priv->lock);
430c6131fa5SGeert Uytterhoeven 
431c6131fa5SGeert Uytterhoeven 	dev->bounce_size = BOUNCE_SIZE;
432c6131fa5SGeert Uytterhoeven 	dev->bounce_buf = kmalloc(BOUNCE_SIZE, GFP_DMA);
433c6131fa5SGeert Uytterhoeven 	if (!dev->bounce_buf) {
434c6131fa5SGeert Uytterhoeven 		error = -ENOMEM;
435c6131fa5SGeert Uytterhoeven 		goto fail_free_priv;
436c6131fa5SGeert Uytterhoeven 	}
437c6131fa5SGeert Uytterhoeven 
438c6131fa5SGeert Uytterhoeven 	error = ps3stor_setup(dev, ps3disk_interrupt);
439c6131fa5SGeert Uytterhoeven 	if (error)
440c6131fa5SGeert Uytterhoeven 		goto fail_free_bounce;
441c6131fa5SGeert Uytterhoeven 
442c6131fa5SGeert Uytterhoeven 	ps3disk_identify(dev);
443c6131fa5SGeert Uytterhoeven 
444fab1adcfSJens Axboe 	queue = blk_mq_init_sq_queue(&priv->tag_set, &ps3disk_mq_ops, 1,
445fab1adcfSJens Axboe 					BLK_MQ_F_SHOULD_MERGE);
446fab1adcfSJens Axboe 	if (IS_ERR(queue)) {
447fab1adcfSJens Axboe 		dev_err(&dev->sbd.core, "%s:%u: blk_mq_init_queue failed\n",
448c6131fa5SGeert Uytterhoeven 			__func__, __LINE__);
449fab1adcfSJens Axboe 		error = PTR_ERR(queue);
450c6131fa5SGeert Uytterhoeven 		goto fail_teardown;
451c6131fa5SGeert Uytterhoeven 	}
452c6131fa5SGeert Uytterhoeven 
453c6131fa5SGeert Uytterhoeven 	priv->queue = queue;
454c6131fa5SGeert Uytterhoeven 	queue->queuedata = dev;
455c6131fa5SGeert Uytterhoeven 
456086fa5ffSMartin K. Petersen 	blk_queue_max_hw_sectors(queue, dev->bounce_size >> 9);
457c6131fa5SGeert Uytterhoeven 	blk_queue_dma_alignment(queue, dev->blk_size-1);
458e1defc4fSMartin K. Petersen 	blk_queue_logical_block_size(queue, dev->blk_size);
459c6131fa5SGeert Uytterhoeven 
46012c95f13SJens Axboe 	blk_queue_write_cache(queue, true, false);
461c6131fa5SGeert Uytterhoeven 
4628a78362cSMartin K. Petersen 	blk_queue_max_segments(queue, -1);
463c6131fa5SGeert Uytterhoeven 	blk_queue_max_segment_size(queue, dev->bounce_size);
464c6131fa5SGeert Uytterhoeven 
465c6131fa5SGeert Uytterhoeven 	gendisk = alloc_disk(PS3DISK_MINORS);
466c6131fa5SGeert Uytterhoeven 	if (!gendisk) {
467c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core, "%s:%u: alloc_disk failed\n", __func__,
468c6131fa5SGeert Uytterhoeven 			__LINE__);
469c6131fa5SGeert Uytterhoeven 		error = -ENOMEM;
470c6131fa5SGeert Uytterhoeven 		goto fail_cleanup_queue;
471c6131fa5SGeert Uytterhoeven 	}
472c6131fa5SGeert Uytterhoeven 
473c6131fa5SGeert Uytterhoeven 	priv->gendisk = gendisk;
474c6131fa5SGeert Uytterhoeven 	gendisk->major = ps3disk_major;
475c6131fa5SGeert Uytterhoeven 	gendisk->first_minor = devidx * PS3DISK_MINORS;
476c6131fa5SGeert Uytterhoeven 	gendisk->fops = &ps3disk_fops;
477c6131fa5SGeert Uytterhoeven 	gendisk->queue = queue;
478c6131fa5SGeert Uytterhoeven 	gendisk->private_data = dev;
479c6131fa5SGeert Uytterhoeven 	snprintf(gendisk->disk_name, sizeof(gendisk->disk_name), PS3DISK_NAME,
480c6131fa5SGeert Uytterhoeven 		 devidx+'a');
481c6131fa5SGeert Uytterhoeven 	priv->blocking_factor = dev->blk_size >> 9;
482c6131fa5SGeert Uytterhoeven 	set_capacity(gendisk,
483c6131fa5SGeert Uytterhoeven 		     dev->regions[dev->region_idx].size*priv->blocking_factor);
484c6131fa5SGeert Uytterhoeven 
485c6131fa5SGeert Uytterhoeven 	dev_info(&dev->sbd.core,
48672deb455SChristoph Hellwig 		 "%s is a %s (%llu MiB total, %llu MiB for OtherOS)\n",
487c6131fa5SGeert Uytterhoeven 		 gendisk->disk_name, priv->model, priv->raw_capacity >> 11,
488c6131fa5SGeert Uytterhoeven 		 get_capacity(gendisk) >> 11);
489c6131fa5SGeert Uytterhoeven 
490fef912bfSHannes Reinecke 	device_add_disk(&dev->sbd.core, gendisk, NULL);
491c6131fa5SGeert Uytterhoeven 	return 0;
492c6131fa5SGeert Uytterhoeven 
493c6131fa5SGeert Uytterhoeven fail_cleanup_queue:
494c6131fa5SGeert Uytterhoeven 	blk_cleanup_queue(queue);
495fab1adcfSJens Axboe 	blk_mq_free_tag_set(&priv->tag_set);
496c6131fa5SGeert Uytterhoeven fail_teardown:
497c6131fa5SGeert Uytterhoeven 	ps3stor_teardown(dev);
498c6131fa5SGeert Uytterhoeven fail_free_bounce:
499c6131fa5SGeert Uytterhoeven 	kfree(dev->bounce_buf);
500c6131fa5SGeert Uytterhoeven fail_free_priv:
501c6131fa5SGeert Uytterhoeven 	kfree(priv);
50203fa68c2SGeert Uytterhoeven 	ps3_system_bus_set_drvdata(_dev, NULL);
503c6131fa5SGeert Uytterhoeven fail:
504c6131fa5SGeert Uytterhoeven 	mutex_lock(&ps3disk_mask_mutex);
505c6131fa5SGeert Uytterhoeven 	__clear_bit(devidx, &ps3disk_mask);
506c6131fa5SGeert Uytterhoeven 	mutex_unlock(&ps3disk_mask_mutex);
507c6131fa5SGeert Uytterhoeven 	return error;
508c6131fa5SGeert Uytterhoeven }
509c6131fa5SGeert Uytterhoeven 
510*6d247e4dSUwe Kleine-König static void ps3disk_remove(struct ps3_system_bus_device *_dev)
511c6131fa5SGeert Uytterhoeven {
512c6131fa5SGeert Uytterhoeven 	struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
51303fa68c2SGeert Uytterhoeven 	struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
514c6131fa5SGeert Uytterhoeven 
515c6131fa5SGeert Uytterhoeven 	mutex_lock(&ps3disk_mask_mutex);
516f331c029STejun Heo 	__clear_bit(MINOR(disk_devt(priv->gendisk)) / PS3DISK_MINORS,
517c6131fa5SGeert Uytterhoeven 		    &ps3disk_mask);
518c6131fa5SGeert Uytterhoeven 	mutex_unlock(&ps3disk_mask_mutex);
519c6131fa5SGeert Uytterhoeven 	del_gendisk(priv->gendisk);
520c6131fa5SGeert Uytterhoeven 	blk_cleanup_queue(priv->queue);
521fab1adcfSJens Axboe 	blk_mq_free_tag_set(&priv->tag_set);
522c6131fa5SGeert Uytterhoeven 	put_disk(priv->gendisk);
523c6131fa5SGeert Uytterhoeven 	dev_notice(&dev->sbd.core, "Synchronizing disk cache\n");
524c6131fa5SGeert Uytterhoeven 	ps3disk_sync_cache(dev);
525c6131fa5SGeert Uytterhoeven 	ps3stor_teardown(dev);
526c6131fa5SGeert Uytterhoeven 	kfree(dev->bounce_buf);
527c6131fa5SGeert Uytterhoeven 	kfree(priv);
52803fa68c2SGeert Uytterhoeven 	ps3_system_bus_set_drvdata(_dev, NULL);
529c6131fa5SGeert Uytterhoeven }
530c6131fa5SGeert Uytterhoeven 
531c6131fa5SGeert Uytterhoeven static struct ps3_system_bus_driver ps3disk = {
532c6131fa5SGeert Uytterhoeven 	.match_id	= PS3_MATCH_ID_STOR_DISK,
533c6131fa5SGeert Uytterhoeven 	.core.name	= DEVICE_NAME,
534c6131fa5SGeert Uytterhoeven 	.core.owner	= THIS_MODULE,
535c6131fa5SGeert Uytterhoeven 	.probe		= ps3disk_probe,
536c6131fa5SGeert Uytterhoeven 	.remove		= ps3disk_remove,
537c6131fa5SGeert Uytterhoeven 	.shutdown	= ps3disk_remove,
538c6131fa5SGeert Uytterhoeven };
539c6131fa5SGeert Uytterhoeven 
540c6131fa5SGeert Uytterhoeven 
541c6131fa5SGeert Uytterhoeven static int __init ps3disk_init(void)
542c6131fa5SGeert Uytterhoeven {
543c6131fa5SGeert Uytterhoeven 	int error;
544c6131fa5SGeert Uytterhoeven 
545c6131fa5SGeert Uytterhoeven 	if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
546c6131fa5SGeert Uytterhoeven 		return -ENODEV;
547c6131fa5SGeert Uytterhoeven 
548c6131fa5SGeert Uytterhoeven 	error = register_blkdev(0, DEVICE_NAME);
549c6131fa5SGeert Uytterhoeven 	if (error <= 0) {
550c6131fa5SGeert Uytterhoeven 		printk(KERN_ERR "%s:%u: register_blkdev failed %d\n", __func__,
551c6131fa5SGeert Uytterhoeven 		       __LINE__, error);
552c6131fa5SGeert Uytterhoeven 		return error;
553c6131fa5SGeert Uytterhoeven 	}
554c6131fa5SGeert Uytterhoeven 	ps3disk_major = error;
555c6131fa5SGeert Uytterhoeven 
556c6131fa5SGeert Uytterhoeven 	pr_info("%s:%u: registered block device major %d\n", __func__,
557c6131fa5SGeert Uytterhoeven 		__LINE__, ps3disk_major);
558c6131fa5SGeert Uytterhoeven 
559c6131fa5SGeert Uytterhoeven 	error = ps3_system_bus_driver_register(&ps3disk);
560c6131fa5SGeert Uytterhoeven 	if (error)
561c6131fa5SGeert Uytterhoeven 		unregister_blkdev(ps3disk_major, DEVICE_NAME);
562c6131fa5SGeert Uytterhoeven 
563c6131fa5SGeert Uytterhoeven 	return error;
564c6131fa5SGeert Uytterhoeven }
565c6131fa5SGeert Uytterhoeven 
566c6131fa5SGeert Uytterhoeven static void __exit ps3disk_exit(void)
567c6131fa5SGeert Uytterhoeven {
568c6131fa5SGeert Uytterhoeven 	ps3_system_bus_driver_unregister(&ps3disk);
569c6131fa5SGeert Uytterhoeven 	unregister_blkdev(ps3disk_major, DEVICE_NAME);
570c6131fa5SGeert Uytterhoeven }
571c6131fa5SGeert Uytterhoeven 
572c6131fa5SGeert Uytterhoeven module_init(ps3disk_init);
573c6131fa5SGeert Uytterhoeven module_exit(ps3disk_exit);
574c6131fa5SGeert Uytterhoeven 
575c6131fa5SGeert Uytterhoeven MODULE_LICENSE("GPL");
576c6131fa5SGeert Uytterhoeven MODULE_DESCRIPTION("PS3 Disk Storage Driver");
577c6131fa5SGeert Uytterhoeven MODULE_AUTHOR("Sony Corporation");
578c6131fa5SGeert Uytterhoeven MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_DISK);
579