xref: /openbmc/linux/drivers/block/ps3disk.c (revision 7988613b0e5b2638caf6cd493cc78e9595eba19c)
1c6131fa5SGeert Uytterhoeven /*
2c6131fa5SGeert Uytterhoeven  * PS3 Disk Storage Driver
3c6131fa5SGeert Uytterhoeven  *
4c6131fa5SGeert Uytterhoeven  * Copyright (C) 2007 Sony Computer Entertainment Inc.
5c6131fa5SGeert Uytterhoeven  * Copyright 2007 Sony Corp.
6c6131fa5SGeert Uytterhoeven  *
7c6131fa5SGeert Uytterhoeven  * This program is free software; you can redistribute it and/or modify it
8c6131fa5SGeert Uytterhoeven  * under the terms of the GNU General Public License as published
9c6131fa5SGeert Uytterhoeven  * by the Free Software Foundation; version 2 of the License.
10c6131fa5SGeert Uytterhoeven  *
11c6131fa5SGeert Uytterhoeven  * This program is distributed in the hope that it will be useful, but
12c6131fa5SGeert Uytterhoeven  * WITHOUT ANY WARRANTY; without even the implied warranty of
13c6131fa5SGeert Uytterhoeven  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14c6131fa5SGeert Uytterhoeven  * General Public License for more details.
15c6131fa5SGeert Uytterhoeven  *
16c6131fa5SGeert Uytterhoeven  * You should have received a copy of the GNU General Public License along
17c6131fa5SGeert Uytterhoeven  * with this program; if not, write to the Free Software Foundation, Inc.,
18c6131fa5SGeert Uytterhoeven  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19c6131fa5SGeert Uytterhoeven  */
20c6131fa5SGeert Uytterhoeven 
21c6131fa5SGeert Uytterhoeven #include <linux/ata.h>
22c6131fa5SGeert Uytterhoeven #include <linux/blkdev.h>
235a0e3ad6STejun Heo #include <linux/slab.h>
240c8d44f2SPaul Gortmaker #include <linux/module.h>
25c6131fa5SGeert Uytterhoeven 
26c6131fa5SGeert Uytterhoeven #include <asm/lv1call.h>
27c6131fa5SGeert Uytterhoeven #include <asm/ps3stor.h>
28c6131fa5SGeert Uytterhoeven #include <asm/firmware.h>
29c6131fa5SGeert Uytterhoeven 
30c6131fa5SGeert Uytterhoeven 
31c6131fa5SGeert Uytterhoeven #define DEVICE_NAME		"ps3disk"
32c6131fa5SGeert Uytterhoeven 
33c6131fa5SGeert Uytterhoeven #define BOUNCE_SIZE		(64*1024)
34c6131fa5SGeert Uytterhoeven 
35c6131fa5SGeert Uytterhoeven #define PS3DISK_MAX_DISKS	16
36c6131fa5SGeert Uytterhoeven #define PS3DISK_MINORS		16
37c6131fa5SGeert Uytterhoeven 
38c6131fa5SGeert Uytterhoeven 
39c6131fa5SGeert Uytterhoeven #define PS3DISK_NAME		"ps3d%c"
40c6131fa5SGeert Uytterhoeven 
41c6131fa5SGeert Uytterhoeven 
42c6131fa5SGeert Uytterhoeven struct ps3disk_private {
43c6131fa5SGeert Uytterhoeven 	spinlock_t lock;		/* Request queue spinlock */
44c6131fa5SGeert Uytterhoeven 	struct request_queue *queue;
45c6131fa5SGeert Uytterhoeven 	struct gendisk *gendisk;
46c6131fa5SGeert Uytterhoeven 	unsigned int blocking_factor;
47c6131fa5SGeert Uytterhoeven 	struct request *req;
48c6131fa5SGeert Uytterhoeven 	u64 raw_capacity;
49c6131fa5SGeert Uytterhoeven 	unsigned char model[ATA_ID_PROD_LEN+1];
50c6131fa5SGeert Uytterhoeven };
51c6131fa5SGeert Uytterhoeven 
52c6131fa5SGeert Uytterhoeven 
53c6131fa5SGeert Uytterhoeven #define LV1_STORAGE_SEND_ATA_COMMAND	(2)
54c6131fa5SGeert Uytterhoeven #define LV1_STORAGE_ATA_HDDOUT		(0x23)
55c6131fa5SGeert Uytterhoeven 
56c6131fa5SGeert Uytterhoeven struct lv1_ata_cmnd_block {
57c6131fa5SGeert Uytterhoeven 	u16	features;
58c6131fa5SGeert Uytterhoeven 	u16	sector_count;
59c6131fa5SGeert Uytterhoeven 	u16	LBA_low;
60c6131fa5SGeert Uytterhoeven 	u16	LBA_mid;
61c6131fa5SGeert Uytterhoeven 	u16	LBA_high;
62c6131fa5SGeert Uytterhoeven 	u8	device;
63c6131fa5SGeert Uytterhoeven 	u8	command;
64c6131fa5SGeert Uytterhoeven 	u32	is_ext;
65c6131fa5SGeert Uytterhoeven 	u32	proto;
66c6131fa5SGeert Uytterhoeven 	u32	in_out;
67c6131fa5SGeert Uytterhoeven 	u32	size;
68c6131fa5SGeert Uytterhoeven 	u64	buffer;
69c6131fa5SGeert Uytterhoeven 	u32	arglen;
70c6131fa5SGeert Uytterhoeven };
71c6131fa5SGeert Uytterhoeven 
72c6131fa5SGeert Uytterhoeven enum lv1_ata_proto {
73c6131fa5SGeert Uytterhoeven 	NON_DATA_PROTO     = 0,
74c6131fa5SGeert Uytterhoeven 	PIO_DATA_IN_PROTO  = 1,
75c6131fa5SGeert Uytterhoeven 	PIO_DATA_OUT_PROTO = 2,
76c6131fa5SGeert Uytterhoeven 	DMA_PROTO = 3
77c6131fa5SGeert Uytterhoeven };
78c6131fa5SGeert Uytterhoeven 
79c6131fa5SGeert Uytterhoeven enum lv1_ata_in_out {
80c6131fa5SGeert Uytterhoeven 	DIR_WRITE = 0,			/* memory -> device */
81c6131fa5SGeert Uytterhoeven 	DIR_READ = 1			/* device -> memory */
82c6131fa5SGeert Uytterhoeven };
83c6131fa5SGeert Uytterhoeven 
84c6131fa5SGeert Uytterhoeven static int ps3disk_major;
85c6131fa5SGeert Uytterhoeven 
86c6131fa5SGeert Uytterhoeven 
8783d5cde4SAlexey Dobriyan static const struct block_device_operations ps3disk_fops = {
88c6131fa5SGeert Uytterhoeven 	.owner		= THIS_MODULE,
89c6131fa5SGeert Uytterhoeven };
90c6131fa5SGeert Uytterhoeven 
91c6131fa5SGeert Uytterhoeven 
92c6131fa5SGeert Uytterhoeven static void ps3disk_scatter_gather(struct ps3_storage_device *dev,
93c6131fa5SGeert Uytterhoeven 				   struct request *req, int gather)
94c6131fa5SGeert Uytterhoeven {
95c6131fa5SGeert Uytterhoeven 	unsigned int offset = 0;
965705f702SNeilBrown 	struct req_iterator iter;
97*7988613bSKent Overstreet 	struct bio_vec bvec;
985705f702SNeilBrown 	unsigned int i = 0;
99c6131fa5SGeert Uytterhoeven 	size_t size;
100c6131fa5SGeert Uytterhoeven 	void *buf;
101c6131fa5SGeert Uytterhoeven 
1025705f702SNeilBrown 	rq_for_each_segment(bvec, req, iter) {
1035705f702SNeilBrown 		unsigned long flags;
104c6131fa5SGeert Uytterhoeven 		dev_dbg(&dev->sbd.core,
105c6131fa5SGeert Uytterhoeven 			"%s:%u: bio %u: %u segs %u sectors from %lu\n",
1065705f702SNeilBrown 			__func__, __LINE__, i, bio_segments(iter.bio),
1074f024f37SKent Overstreet 			bio_sectors(iter.bio), iter.bio->bi_iter.bi_sector);
1085705f702SNeilBrown 
109*7988613bSKent Overstreet 		size = bvec.bv_len;
110*7988613bSKent Overstreet 		buf = bvec_kmap_irq(&bvec, &flags);
111c6131fa5SGeert Uytterhoeven 		if (gather)
112c6131fa5SGeert Uytterhoeven 			memcpy(dev->bounce_buf+offset, buf, size);
113c6131fa5SGeert Uytterhoeven 		else
114c6131fa5SGeert Uytterhoeven 			memcpy(buf, dev->bounce_buf+offset, size);
115c6131fa5SGeert Uytterhoeven 		offset += size;
116*7988613bSKent Overstreet 		flush_kernel_dcache_page(bvec.bv_page);
11793055c31SDan Carpenter 		bvec_kunmap_irq(buf, &flags);
118c6131fa5SGeert Uytterhoeven 		i++;
119c6131fa5SGeert Uytterhoeven 	}
120c6131fa5SGeert Uytterhoeven }
121c6131fa5SGeert Uytterhoeven 
122c6131fa5SGeert Uytterhoeven static int ps3disk_submit_request_sg(struct ps3_storage_device *dev,
123c6131fa5SGeert Uytterhoeven 				     struct request *req)
124c6131fa5SGeert Uytterhoeven {
12503fa68c2SGeert Uytterhoeven 	struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
126c6131fa5SGeert Uytterhoeven 	int write = rq_data_dir(req), res;
127c6131fa5SGeert Uytterhoeven 	const char *op = write ? "write" : "read";
128c6131fa5SGeert Uytterhoeven 	u64 start_sector, sectors;
129c6131fa5SGeert Uytterhoeven 	unsigned int region_id = dev->regions[dev->region_idx].id;
130c6131fa5SGeert Uytterhoeven 
131c6131fa5SGeert Uytterhoeven #ifdef DEBUG
132c6131fa5SGeert Uytterhoeven 	unsigned int n = 0;
133*7988613bSKent Overstreet 	struct bio_vec bv;
1345705f702SNeilBrown 	struct req_iterator iter;
135c6131fa5SGeert Uytterhoeven 
1365705f702SNeilBrown 	rq_for_each_segment(bv, req, iter)
137c6131fa5SGeert Uytterhoeven 		n++;
138c6131fa5SGeert Uytterhoeven 	dev_dbg(&dev->sbd.core,
13983096ebfSTejun Heo 		"%s:%u: %s req has %u bvecs for %u sectors\n",
14083096ebfSTejun Heo 		__func__, __LINE__, op, n, blk_rq_sectors(req));
141c6131fa5SGeert Uytterhoeven #endif
142c6131fa5SGeert Uytterhoeven 
14383096ebfSTejun Heo 	start_sector = blk_rq_pos(req) * priv->blocking_factor;
14483096ebfSTejun Heo 	sectors = blk_rq_sectors(req) * priv->blocking_factor;
145e377c6e2SStephen Rothwell 	dev_dbg(&dev->sbd.core, "%s:%u: %s %llu sectors starting at %llu\n",
146c6131fa5SGeert Uytterhoeven 		__func__, __LINE__, op, sectors, start_sector);
147c6131fa5SGeert Uytterhoeven 
148c6131fa5SGeert Uytterhoeven 	if (write) {
149c6131fa5SGeert Uytterhoeven 		ps3disk_scatter_gather(dev, req, 1);
150c6131fa5SGeert Uytterhoeven 
151c6131fa5SGeert Uytterhoeven 		res = lv1_storage_write(dev->sbd.dev_id, region_id,
152c6131fa5SGeert Uytterhoeven 					start_sector, sectors, 0,
153c6131fa5SGeert Uytterhoeven 					dev->bounce_lpar, &dev->tag);
154c6131fa5SGeert Uytterhoeven 	} else {
155c6131fa5SGeert Uytterhoeven 		res = lv1_storage_read(dev->sbd.dev_id, region_id,
156c6131fa5SGeert Uytterhoeven 				       start_sector, sectors, 0,
157c6131fa5SGeert Uytterhoeven 				       dev->bounce_lpar, &dev->tag);
158c6131fa5SGeert Uytterhoeven 	}
159c6131fa5SGeert Uytterhoeven 	if (res) {
160c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core, "%s:%u: %s failed %d\n", __func__,
161c6131fa5SGeert Uytterhoeven 			__LINE__, op, res);
16210e1e629STejun Heo 		__blk_end_request_all(req, -EIO);
163c6131fa5SGeert Uytterhoeven 		return 0;
164c6131fa5SGeert Uytterhoeven 	}
165c6131fa5SGeert Uytterhoeven 
166c6131fa5SGeert Uytterhoeven 	priv->req = req;
167c6131fa5SGeert Uytterhoeven 	return 1;
168c6131fa5SGeert Uytterhoeven }
169c6131fa5SGeert Uytterhoeven 
170c6131fa5SGeert Uytterhoeven static int ps3disk_submit_flush_request(struct ps3_storage_device *dev,
171c6131fa5SGeert Uytterhoeven 					struct request *req)
172c6131fa5SGeert Uytterhoeven {
17303fa68c2SGeert Uytterhoeven 	struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
174c6131fa5SGeert Uytterhoeven 	u64 res;
175c6131fa5SGeert Uytterhoeven 
176c6131fa5SGeert Uytterhoeven 	dev_dbg(&dev->sbd.core, "%s:%u: flush request\n", __func__, __LINE__);
177c6131fa5SGeert Uytterhoeven 
178c6131fa5SGeert Uytterhoeven 	res = lv1_storage_send_device_command(dev->sbd.dev_id,
179c6131fa5SGeert Uytterhoeven 					      LV1_STORAGE_ATA_HDDOUT, 0, 0, 0,
180c6131fa5SGeert Uytterhoeven 					      0, &dev->tag);
181c6131fa5SGeert Uytterhoeven 	if (res) {
182e377c6e2SStephen Rothwell 		dev_err(&dev->sbd.core, "%s:%u: sync cache failed 0x%llx\n",
183c6131fa5SGeert Uytterhoeven 			__func__, __LINE__, res);
18410e1e629STejun Heo 		__blk_end_request_all(req, -EIO);
185c6131fa5SGeert Uytterhoeven 		return 0;
186c6131fa5SGeert Uytterhoeven 	}
187c6131fa5SGeert Uytterhoeven 
188c6131fa5SGeert Uytterhoeven 	priv->req = req;
189c6131fa5SGeert Uytterhoeven 	return 1;
190c6131fa5SGeert Uytterhoeven }
191c6131fa5SGeert Uytterhoeven 
192c6131fa5SGeert Uytterhoeven static void ps3disk_do_request(struct ps3_storage_device *dev,
193165125e1SJens Axboe 			       struct request_queue *q)
194c6131fa5SGeert Uytterhoeven {
195c6131fa5SGeert Uytterhoeven 	struct request *req;
196c6131fa5SGeert Uytterhoeven 
197c6131fa5SGeert Uytterhoeven 	dev_dbg(&dev->sbd.core, "%s:%u\n", __func__, __LINE__);
198c6131fa5SGeert Uytterhoeven 
1999934c8c0STejun Heo 	while ((req = blk_fetch_request(q))) {
20098d8c8f4SFUJITA Tomonori 		if (req->cmd_flags & REQ_FLUSH) {
201c6131fa5SGeert Uytterhoeven 			if (ps3disk_submit_flush_request(dev, req))
202c6131fa5SGeert Uytterhoeven 				break;
20398d8c8f4SFUJITA Tomonori 		} else if (req->cmd_type == REQ_TYPE_FS) {
20498d8c8f4SFUJITA Tomonori 			if (ps3disk_submit_request_sg(dev, req))
20598d8c8f4SFUJITA Tomonori 				break;
206c6131fa5SGeert Uytterhoeven 		} else {
207c6131fa5SGeert Uytterhoeven 			blk_dump_rq_flags(req, DEVICE_NAME " bad request");
20810e1e629STejun Heo 			__blk_end_request_all(req, -EIO);
209c6131fa5SGeert Uytterhoeven 			continue;
210c6131fa5SGeert Uytterhoeven 		}
211c6131fa5SGeert Uytterhoeven 	}
212c6131fa5SGeert Uytterhoeven }
213c6131fa5SGeert Uytterhoeven 
214165125e1SJens Axboe static void ps3disk_request(struct request_queue *q)
215c6131fa5SGeert Uytterhoeven {
216c6131fa5SGeert Uytterhoeven 	struct ps3_storage_device *dev = q->queuedata;
21703fa68c2SGeert Uytterhoeven 	struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
218c6131fa5SGeert Uytterhoeven 
219c6131fa5SGeert Uytterhoeven 	if (priv->req) {
220c6131fa5SGeert Uytterhoeven 		dev_dbg(&dev->sbd.core, "%s:%u busy\n", __func__, __LINE__);
221c6131fa5SGeert Uytterhoeven 		return;
222c6131fa5SGeert Uytterhoeven 	}
223c6131fa5SGeert Uytterhoeven 
224c6131fa5SGeert Uytterhoeven 	ps3disk_do_request(dev, q);
225c6131fa5SGeert Uytterhoeven }
226c6131fa5SGeert Uytterhoeven 
227c6131fa5SGeert Uytterhoeven static irqreturn_t ps3disk_interrupt(int irq, void *data)
228c6131fa5SGeert Uytterhoeven {
229c6131fa5SGeert Uytterhoeven 	struct ps3_storage_device *dev = data;
230c6131fa5SGeert Uytterhoeven 	struct ps3disk_private *priv;
231c6131fa5SGeert Uytterhoeven 	struct request *req;
232f01ab252SKiyoshi Ueda 	int res, read, error;
233c6131fa5SGeert Uytterhoeven 	u64 tag, status;
234c6131fa5SGeert Uytterhoeven 	const char *op;
235c6131fa5SGeert Uytterhoeven 
236c6131fa5SGeert Uytterhoeven 	res = lv1_storage_get_async_status(dev->sbd.dev_id, &tag, &status);
237c6131fa5SGeert Uytterhoeven 
238c6131fa5SGeert Uytterhoeven 	if (tag != dev->tag)
239c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core,
240e377c6e2SStephen Rothwell 			"%s:%u: tag mismatch, got %llx, expected %llx\n",
241c6131fa5SGeert Uytterhoeven 			__func__, __LINE__, tag, dev->tag);
242c6131fa5SGeert Uytterhoeven 
243c6131fa5SGeert Uytterhoeven 	if (res) {
244e377c6e2SStephen Rothwell 		dev_err(&dev->sbd.core, "%s:%u: res=%d status=0x%llx\n",
245c6131fa5SGeert Uytterhoeven 			__func__, __LINE__, res, status);
246c6131fa5SGeert Uytterhoeven 		return IRQ_HANDLED;
247c6131fa5SGeert Uytterhoeven 	}
248c6131fa5SGeert Uytterhoeven 
24903fa68c2SGeert Uytterhoeven 	priv = ps3_system_bus_get_drvdata(&dev->sbd);
250c6131fa5SGeert Uytterhoeven 	req = priv->req;
251c6131fa5SGeert Uytterhoeven 	if (!req) {
252c6131fa5SGeert Uytterhoeven 		dev_dbg(&dev->sbd.core,
253c6131fa5SGeert Uytterhoeven 			"%s:%u non-block layer request completed\n", __func__,
254c6131fa5SGeert Uytterhoeven 			__LINE__);
255c6131fa5SGeert Uytterhoeven 		dev->lv1_status = status;
256c6131fa5SGeert Uytterhoeven 		complete(&dev->done);
257c6131fa5SGeert Uytterhoeven 		return IRQ_HANDLED;
258c6131fa5SGeert Uytterhoeven 	}
259c6131fa5SGeert Uytterhoeven 
26098d8c8f4SFUJITA Tomonori 	if (req->cmd_flags & REQ_FLUSH) {
261c6131fa5SGeert Uytterhoeven 		read = 0;
262c6131fa5SGeert Uytterhoeven 		op = "flush";
263c6131fa5SGeert Uytterhoeven 	} else {
264c6131fa5SGeert Uytterhoeven 		read = !rq_data_dir(req);
265c6131fa5SGeert Uytterhoeven 		op = read ? "read" : "write";
266c6131fa5SGeert Uytterhoeven 	}
267c6131fa5SGeert Uytterhoeven 	if (status) {
268e377c6e2SStephen Rothwell 		dev_dbg(&dev->sbd.core, "%s:%u: %s failed 0x%llx\n", __func__,
269c6131fa5SGeert Uytterhoeven 			__LINE__, op, status);
270f01ab252SKiyoshi Ueda 		error = -EIO;
271c6131fa5SGeert Uytterhoeven 	} else {
272c6131fa5SGeert Uytterhoeven 		dev_dbg(&dev->sbd.core, "%s:%u: %s completed\n", __func__,
273c6131fa5SGeert Uytterhoeven 			__LINE__, op);
274f01ab252SKiyoshi Ueda 		error = 0;
275c6131fa5SGeert Uytterhoeven 		if (read)
276c6131fa5SGeert Uytterhoeven 			ps3disk_scatter_gather(dev, req, 0);
277c6131fa5SGeert Uytterhoeven 	}
278c6131fa5SGeert Uytterhoeven 
279c6131fa5SGeert Uytterhoeven 	spin_lock(&priv->lock);
280cd4c34ebSTejun Heo 	__blk_end_request_all(req, error);
281c6131fa5SGeert Uytterhoeven 	priv->req = NULL;
282c6131fa5SGeert Uytterhoeven 	ps3disk_do_request(dev, priv->queue);
283c6131fa5SGeert Uytterhoeven 	spin_unlock(&priv->lock);
284c6131fa5SGeert Uytterhoeven 
285c6131fa5SGeert Uytterhoeven 	return IRQ_HANDLED;
286c6131fa5SGeert Uytterhoeven }
287c6131fa5SGeert Uytterhoeven 
288c6131fa5SGeert Uytterhoeven static int ps3disk_sync_cache(struct ps3_storage_device *dev)
289c6131fa5SGeert Uytterhoeven {
290c6131fa5SGeert Uytterhoeven 	u64 res;
291c6131fa5SGeert Uytterhoeven 
292c6131fa5SGeert Uytterhoeven 	dev_dbg(&dev->sbd.core, "%s:%u: sync cache\n", __func__, __LINE__);
293c6131fa5SGeert Uytterhoeven 
294c6131fa5SGeert Uytterhoeven 	res = ps3stor_send_command(dev, LV1_STORAGE_ATA_HDDOUT, 0, 0, 0, 0);
295c6131fa5SGeert Uytterhoeven 	if (res) {
296e377c6e2SStephen Rothwell 		dev_err(&dev->sbd.core, "%s:%u: sync cache failed 0x%llx\n",
297c6131fa5SGeert Uytterhoeven 			__func__, __LINE__, res);
298c6131fa5SGeert Uytterhoeven 		return -EIO;
299c6131fa5SGeert Uytterhoeven 	}
300c6131fa5SGeert Uytterhoeven 	return 0;
301c6131fa5SGeert Uytterhoeven }
302c6131fa5SGeert Uytterhoeven 
303c6131fa5SGeert Uytterhoeven 
304c6131fa5SGeert Uytterhoeven /* ATA helpers copied from drivers/ata/libata-core.c */
305c6131fa5SGeert Uytterhoeven 
306c6131fa5SGeert Uytterhoeven static void swap_buf_le16(u16 *buf, unsigned int buf_words)
307c6131fa5SGeert Uytterhoeven {
308c6131fa5SGeert Uytterhoeven #ifdef __BIG_ENDIAN
309c6131fa5SGeert Uytterhoeven 	unsigned int i;
310c6131fa5SGeert Uytterhoeven 
311c6131fa5SGeert Uytterhoeven 	for (i = 0; i < buf_words; i++)
312c6131fa5SGeert Uytterhoeven 		buf[i] = le16_to_cpu(buf[i]);
313c6131fa5SGeert Uytterhoeven #endif /* __BIG_ENDIAN */
314c6131fa5SGeert Uytterhoeven }
315c6131fa5SGeert Uytterhoeven 
316c6131fa5SGeert Uytterhoeven static u64 ata_id_n_sectors(const u16 *id)
317c6131fa5SGeert Uytterhoeven {
318c6131fa5SGeert Uytterhoeven 	if (ata_id_has_lba(id)) {
319c6131fa5SGeert Uytterhoeven 		if (ata_id_has_lba48(id))
320c6131fa5SGeert Uytterhoeven 			return ata_id_u64(id, 100);
321c6131fa5SGeert Uytterhoeven 		else
322c6131fa5SGeert Uytterhoeven 			return ata_id_u32(id, 60);
323c6131fa5SGeert Uytterhoeven 	} else {
324c6131fa5SGeert Uytterhoeven 		if (ata_id_current_chs_valid(id))
325c6131fa5SGeert Uytterhoeven 			return ata_id_u32(id, 57);
326c6131fa5SGeert Uytterhoeven 		else
327c6131fa5SGeert Uytterhoeven 			return id[1] * id[3] * id[6];
328c6131fa5SGeert Uytterhoeven 	}
329c6131fa5SGeert Uytterhoeven }
330c6131fa5SGeert Uytterhoeven 
331c6131fa5SGeert Uytterhoeven static void ata_id_string(const u16 *id, unsigned char *s, unsigned int ofs,
332c6131fa5SGeert Uytterhoeven 			  unsigned int len)
333c6131fa5SGeert Uytterhoeven {
334c6131fa5SGeert Uytterhoeven 	unsigned int c;
335c6131fa5SGeert Uytterhoeven 
336c6131fa5SGeert Uytterhoeven 	while (len > 0) {
337c6131fa5SGeert Uytterhoeven 		c = id[ofs] >> 8;
338c6131fa5SGeert Uytterhoeven 		*s = c;
339c6131fa5SGeert Uytterhoeven 		s++;
340c6131fa5SGeert Uytterhoeven 
341c6131fa5SGeert Uytterhoeven 		c = id[ofs] & 0xff;
342c6131fa5SGeert Uytterhoeven 		*s = c;
343c6131fa5SGeert Uytterhoeven 		s++;
344c6131fa5SGeert Uytterhoeven 
345c6131fa5SGeert Uytterhoeven 		ofs++;
346c6131fa5SGeert Uytterhoeven 		len -= 2;
347c6131fa5SGeert Uytterhoeven 	}
348c6131fa5SGeert Uytterhoeven }
349c6131fa5SGeert Uytterhoeven 
350c6131fa5SGeert Uytterhoeven static void ata_id_c_string(const u16 *id, unsigned char *s, unsigned int ofs,
351c6131fa5SGeert Uytterhoeven 			    unsigned int len)
352c6131fa5SGeert Uytterhoeven {
353c6131fa5SGeert Uytterhoeven 	unsigned char *p;
354c6131fa5SGeert Uytterhoeven 
355c6131fa5SGeert Uytterhoeven 	WARN_ON(!(len & 1));
356c6131fa5SGeert Uytterhoeven 
357c6131fa5SGeert Uytterhoeven 	ata_id_string(id, s, ofs, len - 1);
358c6131fa5SGeert Uytterhoeven 
359c6131fa5SGeert Uytterhoeven 	p = s + strnlen(s, len - 1);
360c6131fa5SGeert Uytterhoeven 	while (p > s && p[-1] == ' ')
361c6131fa5SGeert Uytterhoeven 		p--;
362c6131fa5SGeert Uytterhoeven 	*p = '\0';
363c6131fa5SGeert Uytterhoeven }
364c6131fa5SGeert Uytterhoeven 
365c6131fa5SGeert Uytterhoeven static int ps3disk_identify(struct ps3_storage_device *dev)
366c6131fa5SGeert Uytterhoeven {
36703fa68c2SGeert Uytterhoeven 	struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
368c6131fa5SGeert Uytterhoeven 	struct lv1_ata_cmnd_block ata_cmnd;
369c6131fa5SGeert Uytterhoeven 	u16 *id = dev->bounce_buf;
370c6131fa5SGeert Uytterhoeven 	u64 res;
371c6131fa5SGeert Uytterhoeven 
372c6131fa5SGeert Uytterhoeven 	dev_dbg(&dev->sbd.core, "%s:%u: identify disk\n", __func__, __LINE__);
373c6131fa5SGeert Uytterhoeven 
374c6131fa5SGeert Uytterhoeven 	memset(&ata_cmnd, 0, sizeof(struct lv1_ata_cmnd_block));
375c6131fa5SGeert Uytterhoeven 	ata_cmnd.command = ATA_CMD_ID_ATA;
376c6131fa5SGeert Uytterhoeven 	ata_cmnd.sector_count = 1;
377c6131fa5SGeert Uytterhoeven 	ata_cmnd.size = ata_cmnd.arglen = ATA_ID_WORDS * 2;
378c6131fa5SGeert Uytterhoeven 	ata_cmnd.buffer = dev->bounce_lpar;
379c6131fa5SGeert Uytterhoeven 	ata_cmnd.proto = PIO_DATA_IN_PROTO;
380c6131fa5SGeert Uytterhoeven 	ata_cmnd.in_out = DIR_READ;
381c6131fa5SGeert Uytterhoeven 
382c6131fa5SGeert Uytterhoeven 	res = ps3stor_send_command(dev, LV1_STORAGE_SEND_ATA_COMMAND,
383c6131fa5SGeert Uytterhoeven 				   ps3_mm_phys_to_lpar(__pa(&ata_cmnd)),
384c6131fa5SGeert Uytterhoeven 				   sizeof(ata_cmnd), ata_cmnd.buffer,
385c6131fa5SGeert Uytterhoeven 				   ata_cmnd.arglen);
386c6131fa5SGeert Uytterhoeven 	if (res) {
387e377c6e2SStephen Rothwell 		dev_err(&dev->sbd.core, "%s:%u: identify disk failed 0x%llx\n",
388c6131fa5SGeert Uytterhoeven 			__func__, __LINE__, res);
389c6131fa5SGeert Uytterhoeven 		return -EIO;
390c6131fa5SGeert Uytterhoeven 	}
391c6131fa5SGeert Uytterhoeven 
392c6131fa5SGeert Uytterhoeven 	swap_buf_le16(id, ATA_ID_WORDS);
393c6131fa5SGeert Uytterhoeven 
394c6131fa5SGeert Uytterhoeven 	/* All we're interested in are raw capacity and model name */
395c6131fa5SGeert Uytterhoeven 	priv->raw_capacity = ata_id_n_sectors(id);
396c6131fa5SGeert Uytterhoeven 	ata_id_c_string(id, priv->model, ATA_ID_PROD, sizeof(priv->model));
397c6131fa5SGeert Uytterhoeven 	return 0;
398c6131fa5SGeert Uytterhoeven }
399c6131fa5SGeert Uytterhoeven 
400c6131fa5SGeert Uytterhoeven static unsigned long ps3disk_mask;
401c6131fa5SGeert Uytterhoeven 
402c6131fa5SGeert Uytterhoeven static DEFINE_MUTEX(ps3disk_mask_mutex);
403c6131fa5SGeert Uytterhoeven 
4048d85fce7SGreg Kroah-Hartman static int ps3disk_probe(struct ps3_system_bus_device *_dev)
405c6131fa5SGeert Uytterhoeven {
406c6131fa5SGeert Uytterhoeven 	struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
407c6131fa5SGeert Uytterhoeven 	struct ps3disk_private *priv;
408c6131fa5SGeert Uytterhoeven 	int error;
409c6131fa5SGeert Uytterhoeven 	unsigned int devidx;
410c6131fa5SGeert Uytterhoeven 	struct request_queue *queue;
411c6131fa5SGeert Uytterhoeven 	struct gendisk *gendisk;
412c6131fa5SGeert Uytterhoeven 
413c6131fa5SGeert Uytterhoeven 	if (dev->blk_size < 512) {
414c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core,
415e377c6e2SStephen Rothwell 			"%s:%u: cannot handle block size %llu\n", __func__,
416c6131fa5SGeert Uytterhoeven 			__LINE__, dev->blk_size);
417c6131fa5SGeert Uytterhoeven 		return -EINVAL;
418c6131fa5SGeert Uytterhoeven 	}
419c6131fa5SGeert Uytterhoeven 
420c6131fa5SGeert Uytterhoeven 	BUILD_BUG_ON(PS3DISK_MAX_DISKS > BITS_PER_LONG);
421c6131fa5SGeert Uytterhoeven 	mutex_lock(&ps3disk_mask_mutex);
422c6131fa5SGeert Uytterhoeven 	devidx = find_first_zero_bit(&ps3disk_mask, PS3DISK_MAX_DISKS);
423c6131fa5SGeert Uytterhoeven 	if (devidx >= PS3DISK_MAX_DISKS) {
424c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core, "%s:%u: Too many disks\n", __func__,
425c6131fa5SGeert Uytterhoeven 			__LINE__);
426c6131fa5SGeert Uytterhoeven 		mutex_unlock(&ps3disk_mask_mutex);
427c6131fa5SGeert Uytterhoeven 		return -ENOSPC;
428c6131fa5SGeert Uytterhoeven 	}
429c6131fa5SGeert Uytterhoeven 	__set_bit(devidx, &ps3disk_mask);
430c6131fa5SGeert Uytterhoeven 	mutex_unlock(&ps3disk_mask_mutex);
431c6131fa5SGeert Uytterhoeven 
432c6131fa5SGeert Uytterhoeven 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
433c6131fa5SGeert Uytterhoeven 	if (!priv) {
434c6131fa5SGeert Uytterhoeven 		error = -ENOMEM;
435c6131fa5SGeert Uytterhoeven 		goto fail;
436c6131fa5SGeert Uytterhoeven 	}
437c6131fa5SGeert Uytterhoeven 
43803fa68c2SGeert Uytterhoeven 	ps3_system_bus_set_drvdata(_dev, priv);
439c6131fa5SGeert Uytterhoeven 	spin_lock_init(&priv->lock);
440c6131fa5SGeert Uytterhoeven 
441c6131fa5SGeert Uytterhoeven 	dev->bounce_size = BOUNCE_SIZE;
442c6131fa5SGeert Uytterhoeven 	dev->bounce_buf = kmalloc(BOUNCE_SIZE, GFP_DMA);
443c6131fa5SGeert Uytterhoeven 	if (!dev->bounce_buf) {
444c6131fa5SGeert Uytterhoeven 		error = -ENOMEM;
445c6131fa5SGeert Uytterhoeven 		goto fail_free_priv;
446c6131fa5SGeert Uytterhoeven 	}
447c6131fa5SGeert Uytterhoeven 
448c6131fa5SGeert Uytterhoeven 	error = ps3stor_setup(dev, ps3disk_interrupt);
449c6131fa5SGeert Uytterhoeven 	if (error)
450c6131fa5SGeert Uytterhoeven 		goto fail_free_bounce;
451c6131fa5SGeert Uytterhoeven 
452c6131fa5SGeert Uytterhoeven 	ps3disk_identify(dev);
453c6131fa5SGeert Uytterhoeven 
454c6131fa5SGeert Uytterhoeven 	queue = blk_init_queue(ps3disk_request, &priv->lock);
455c6131fa5SGeert Uytterhoeven 	if (!queue) {
456c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core, "%s:%u: blk_init_queue failed\n",
457c6131fa5SGeert Uytterhoeven 			__func__, __LINE__);
458c6131fa5SGeert Uytterhoeven 		error = -ENOMEM;
459c6131fa5SGeert Uytterhoeven 		goto fail_teardown;
460c6131fa5SGeert Uytterhoeven 	}
461c6131fa5SGeert Uytterhoeven 
462c6131fa5SGeert Uytterhoeven 	priv->queue = queue;
463c6131fa5SGeert Uytterhoeven 	queue->queuedata = dev;
464c6131fa5SGeert Uytterhoeven 
465c6131fa5SGeert Uytterhoeven 	blk_queue_bounce_limit(queue, BLK_BOUNCE_HIGH);
466c6131fa5SGeert Uytterhoeven 
467086fa5ffSMartin K. Petersen 	blk_queue_max_hw_sectors(queue, dev->bounce_size >> 9);
468c6131fa5SGeert Uytterhoeven 	blk_queue_segment_boundary(queue, -1UL);
469c6131fa5SGeert Uytterhoeven 	blk_queue_dma_alignment(queue, dev->blk_size-1);
470e1defc4fSMartin K. Petersen 	blk_queue_logical_block_size(queue, dev->blk_size);
471c6131fa5SGeert Uytterhoeven 
4724913efe4STejun Heo 	blk_queue_flush(queue, REQ_FLUSH);
473c6131fa5SGeert Uytterhoeven 
4748a78362cSMartin K. Petersen 	blk_queue_max_segments(queue, -1);
475c6131fa5SGeert Uytterhoeven 	blk_queue_max_segment_size(queue, dev->bounce_size);
476c6131fa5SGeert Uytterhoeven 
477c6131fa5SGeert Uytterhoeven 	gendisk = alloc_disk(PS3DISK_MINORS);
478c6131fa5SGeert Uytterhoeven 	if (!gendisk) {
479c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core, "%s:%u: alloc_disk failed\n", __func__,
480c6131fa5SGeert Uytterhoeven 			__LINE__);
481c6131fa5SGeert Uytterhoeven 		error = -ENOMEM;
482c6131fa5SGeert Uytterhoeven 		goto fail_cleanup_queue;
483c6131fa5SGeert Uytterhoeven 	}
484c6131fa5SGeert Uytterhoeven 
485c6131fa5SGeert Uytterhoeven 	priv->gendisk = gendisk;
486c6131fa5SGeert Uytterhoeven 	gendisk->major = ps3disk_major;
487c6131fa5SGeert Uytterhoeven 	gendisk->first_minor = devidx * PS3DISK_MINORS;
488c6131fa5SGeert Uytterhoeven 	gendisk->fops = &ps3disk_fops;
489c6131fa5SGeert Uytterhoeven 	gendisk->queue = queue;
490c6131fa5SGeert Uytterhoeven 	gendisk->private_data = dev;
491c6131fa5SGeert Uytterhoeven 	gendisk->driverfs_dev = &dev->sbd.core;
492c6131fa5SGeert Uytterhoeven 	snprintf(gendisk->disk_name, sizeof(gendisk->disk_name), PS3DISK_NAME,
493c6131fa5SGeert Uytterhoeven 		 devidx+'a');
494c6131fa5SGeert Uytterhoeven 	priv->blocking_factor = dev->blk_size >> 9;
495c6131fa5SGeert Uytterhoeven 	set_capacity(gendisk,
496c6131fa5SGeert Uytterhoeven 		     dev->regions[dev->region_idx].size*priv->blocking_factor);
497c6131fa5SGeert Uytterhoeven 
498c6131fa5SGeert Uytterhoeven 	dev_info(&dev->sbd.core,
499e377c6e2SStephen Rothwell 		 "%s is a %s (%llu MiB total, %lu MiB for OtherOS)\n",
500c6131fa5SGeert Uytterhoeven 		 gendisk->disk_name, priv->model, priv->raw_capacity >> 11,
501c6131fa5SGeert Uytterhoeven 		 get_capacity(gendisk) >> 11);
502c6131fa5SGeert Uytterhoeven 
503c6131fa5SGeert Uytterhoeven 	add_disk(gendisk);
504c6131fa5SGeert Uytterhoeven 	return 0;
505c6131fa5SGeert Uytterhoeven 
506c6131fa5SGeert Uytterhoeven fail_cleanup_queue:
507c6131fa5SGeert Uytterhoeven 	blk_cleanup_queue(queue);
508c6131fa5SGeert Uytterhoeven fail_teardown:
509c6131fa5SGeert Uytterhoeven 	ps3stor_teardown(dev);
510c6131fa5SGeert Uytterhoeven fail_free_bounce:
511c6131fa5SGeert Uytterhoeven 	kfree(dev->bounce_buf);
512c6131fa5SGeert Uytterhoeven fail_free_priv:
513c6131fa5SGeert Uytterhoeven 	kfree(priv);
51403fa68c2SGeert Uytterhoeven 	ps3_system_bus_set_drvdata(_dev, NULL);
515c6131fa5SGeert Uytterhoeven fail:
516c6131fa5SGeert Uytterhoeven 	mutex_lock(&ps3disk_mask_mutex);
517c6131fa5SGeert Uytterhoeven 	__clear_bit(devidx, &ps3disk_mask);
518c6131fa5SGeert Uytterhoeven 	mutex_unlock(&ps3disk_mask_mutex);
519c6131fa5SGeert Uytterhoeven 	return error;
520c6131fa5SGeert Uytterhoeven }
521c6131fa5SGeert Uytterhoeven 
522c6131fa5SGeert Uytterhoeven static int ps3disk_remove(struct ps3_system_bus_device *_dev)
523c6131fa5SGeert Uytterhoeven {
524c6131fa5SGeert Uytterhoeven 	struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
52503fa68c2SGeert Uytterhoeven 	struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
526c6131fa5SGeert Uytterhoeven 
527c6131fa5SGeert Uytterhoeven 	mutex_lock(&ps3disk_mask_mutex);
528f331c029STejun Heo 	__clear_bit(MINOR(disk_devt(priv->gendisk)) / PS3DISK_MINORS,
529c6131fa5SGeert Uytterhoeven 		    &ps3disk_mask);
530c6131fa5SGeert Uytterhoeven 	mutex_unlock(&ps3disk_mask_mutex);
531c6131fa5SGeert Uytterhoeven 	del_gendisk(priv->gendisk);
532c6131fa5SGeert Uytterhoeven 	blk_cleanup_queue(priv->queue);
533c6131fa5SGeert Uytterhoeven 	put_disk(priv->gendisk);
534c6131fa5SGeert Uytterhoeven 	dev_notice(&dev->sbd.core, "Synchronizing disk cache\n");
535c6131fa5SGeert Uytterhoeven 	ps3disk_sync_cache(dev);
536c6131fa5SGeert Uytterhoeven 	ps3stor_teardown(dev);
537c6131fa5SGeert Uytterhoeven 	kfree(dev->bounce_buf);
538c6131fa5SGeert Uytterhoeven 	kfree(priv);
53903fa68c2SGeert Uytterhoeven 	ps3_system_bus_set_drvdata(_dev, NULL);
540c6131fa5SGeert Uytterhoeven 	return 0;
541c6131fa5SGeert Uytterhoeven }
542c6131fa5SGeert Uytterhoeven 
543c6131fa5SGeert Uytterhoeven static struct ps3_system_bus_driver ps3disk = {
544c6131fa5SGeert Uytterhoeven 	.match_id	= PS3_MATCH_ID_STOR_DISK,
545c6131fa5SGeert Uytterhoeven 	.core.name	= DEVICE_NAME,
546c6131fa5SGeert Uytterhoeven 	.core.owner	= THIS_MODULE,
547c6131fa5SGeert Uytterhoeven 	.probe		= ps3disk_probe,
548c6131fa5SGeert Uytterhoeven 	.remove		= ps3disk_remove,
549c6131fa5SGeert Uytterhoeven 	.shutdown	= ps3disk_remove,
550c6131fa5SGeert Uytterhoeven };
551c6131fa5SGeert Uytterhoeven 
552c6131fa5SGeert Uytterhoeven 
553c6131fa5SGeert Uytterhoeven static int __init ps3disk_init(void)
554c6131fa5SGeert Uytterhoeven {
555c6131fa5SGeert Uytterhoeven 	int error;
556c6131fa5SGeert Uytterhoeven 
557c6131fa5SGeert Uytterhoeven 	if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
558c6131fa5SGeert Uytterhoeven 		return -ENODEV;
559c6131fa5SGeert Uytterhoeven 
560c6131fa5SGeert Uytterhoeven 	error = register_blkdev(0, DEVICE_NAME);
561c6131fa5SGeert Uytterhoeven 	if (error <= 0) {
562c6131fa5SGeert Uytterhoeven 		printk(KERN_ERR "%s:%u: register_blkdev failed %d\n", __func__,
563c6131fa5SGeert Uytterhoeven 		       __LINE__, error);
564c6131fa5SGeert Uytterhoeven 		return error;
565c6131fa5SGeert Uytterhoeven 	}
566c6131fa5SGeert Uytterhoeven 	ps3disk_major = error;
567c6131fa5SGeert Uytterhoeven 
568c6131fa5SGeert Uytterhoeven 	pr_info("%s:%u: registered block device major %d\n", __func__,
569c6131fa5SGeert Uytterhoeven 		__LINE__, ps3disk_major);
570c6131fa5SGeert Uytterhoeven 
571c6131fa5SGeert Uytterhoeven 	error = ps3_system_bus_driver_register(&ps3disk);
572c6131fa5SGeert Uytterhoeven 	if (error)
573c6131fa5SGeert Uytterhoeven 		unregister_blkdev(ps3disk_major, DEVICE_NAME);
574c6131fa5SGeert Uytterhoeven 
575c6131fa5SGeert Uytterhoeven 	return error;
576c6131fa5SGeert Uytterhoeven }
577c6131fa5SGeert Uytterhoeven 
578c6131fa5SGeert Uytterhoeven static void __exit ps3disk_exit(void)
579c6131fa5SGeert Uytterhoeven {
580c6131fa5SGeert Uytterhoeven 	ps3_system_bus_driver_unregister(&ps3disk);
581c6131fa5SGeert Uytterhoeven 	unregister_blkdev(ps3disk_major, DEVICE_NAME);
582c6131fa5SGeert Uytterhoeven }
583c6131fa5SGeert Uytterhoeven 
584c6131fa5SGeert Uytterhoeven module_init(ps3disk_init);
585c6131fa5SGeert Uytterhoeven module_exit(ps3disk_exit);
586c6131fa5SGeert Uytterhoeven 
587c6131fa5SGeert Uytterhoeven MODULE_LICENSE("GPL");
588c6131fa5SGeert Uytterhoeven MODULE_DESCRIPTION("PS3 Disk Storage Driver");
589c6131fa5SGeert Uytterhoeven MODULE_AUTHOR("Sony Corporation");
590c6131fa5SGeert Uytterhoeven MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_DISK);
591