xref: /openbmc/linux/drivers/block/ps3disk.c (revision e377c6e24d1b465ebd18fe49d0dc06932ced9bb0)
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>
23c6131fa5SGeert Uytterhoeven 
24c6131fa5SGeert Uytterhoeven #include <asm/lv1call.h>
25c6131fa5SGeert Uytterhoeven #include <asm/ps3stor.h>
26c6131fa5SGeert Uytterhoeven #include <asm/firmware.h>
27c6131fa5SGeert Uytterhoeven 
28c6131fa5SGeert Uytterhoeven 
29c6131fa5SGeert Uytterhoeven #define DEVICE_NAME		"ps3disk"
30c6131fa5SGeert Uytterhoeven 
31c6131fa5SGeert Uytterhoeven #define BOUNCE_SIZE		(64*1024)
32c6131fa5SGeert Uytterhoeven 
33c6131fa5SGeert Uytterhoeven #define PS3DISK_MAX_DISKS	16
34c6131fa5SGeert Uytterhoeven #define PS3DISK_MINORS		16
35c6131fa5SGeert Uytterhoeven 
36c6131fa5SGeert Uytterhoeven 
37c6131fa5SGeert Uytterhoeven #define PS3DISK_NAME		"ps3d%c"
38c6131fa5SGeert Uytterhoeven 
39c6131fa5SGeert Uytterhoeven 
40c6131fa5SGeert Uytterhoeven struct ps3disk_private {
41c6131fa5SGeert Uytterhoeven 	spinlock_t lock;		/* Request queue spinlock */
42c6131fa5SGeert Uytterhoeven 	struct request_queue *queue;
43c6131fa5SGeert Uytterhoeven 	struct gendisk *gendisk;
44c6131fa5SGeert Uytterhoeven 	unsigned int blocking_factor;
45c6131fa5SGeert Uytterhoeven 	struct request *req;
46c6131fa5SGeert Uytterhoeven 	u64 raw_capacity;
47c6131fa5SGeert Uytterhoeven 	unsigned char model[ATA_ID_PROD_LEN+1];
48c6131fa5SGeert Uytterhoeven };
49c6131fa5SGeert Uytterhoeven 
50c6131fa5SGeert Uytterhoeven 
51c6131fa5SGeert Uytterhoeven #define LV1_STORAGE_SEND_ATA_COMMAND	(2)
52c6131fa5SGeert Uytterhoeven #define LV1_STORAGE_ATA_HDDOUT		(0x23)
53c6131fa5SGeert Uytterhoeven 
54c6131fa5SGeert Uytterhoeven struct lv1_ata_cmnd_block {
55c6131fa5SGeert Uytterhoeven 	u16	features;
56c6131fa5SGeert Uytterhoeven 	u16	sector_count;
57c6131fa5SGeert Uytterhoeven 	u16	LBA_low;
58c6131fa5SGeert Uytterhoeven 	u16	LBA_mid;
59c6131fa5SGeert Uytterhoeven 	u16	LBA_high;
60c6131fa5SGeert Uytterhoeven 	u8	device;
61c6131fa5SGeert Uytterhoeven 	u8	command;
62c6131fa5SGeert Uytterhoeven 	u32	is_ext;
63c6131fa5SGeert Uytterhoeven 	u32	proto;
64c6131fa5SGeert Uytterhoeven 	u32	in_out;
65c6131fa5SGeert Uytterhoeven 	u32	size;
66c6131fa5SGeert Uytterhoeven 	u64	buffer;
67c6131fa5SGeert Uytterhoeven 	u32	arglen;
68c6131fa5SGeert Uytterhoeven };
69c6131fa5SGeert Uytterhoeven 
70c6131fa5SGeert Uytterhoeven enum lv1_ata_proto {
71c6131fa5SGeert Uytterhoeven 	NON_DATA_PROTO     = 0,
72c6131fa5SGeert Uytterhoeven 	PIO_DATA_IN_PROTO  = 1,
73c6131fa5SGeert Uytterhoeven 	PIO_DATA_OUT_PROTO = 2,
74c6131fa5SGeert Uytterhoeven 	DMA_PROTO = 3
75c6131fa5SGeert Uytterhoeven };
76c6131fa5SGeert Uytterhoeven 
77c6131fa5SGeert Uytterhoeven enum lv1_ata_in_out {
78c6131fa5SGeert Uytterhoeven 	DIR_WRITE = 0,			/* memory -> device */
79c6131fa5SGeert Uytterhoeven 	DIR_READ = 1			/* device -> memory */
80c6131fa5SGeert Uytterhoeven };
81c6131fa5SGeert Uytterhoeven 
82c6131fa5SGeert Uytterhoeven static int ps3disk_major;
83c6131fa5SGeert Uytterhoeven 
84c6131fa5SGeert Uytterhoeven 
85c6131fa5SGeert Uytterhoeven static struct block_device_operations ps3disk_fops = {
86c6131fa5SGeert Uytterhoeven 	.owner		= THIS_MODULE,
87c6131fa5SGeert Uytterhoeven };
88c6131fa5SGeert Uytterhoeven 
89c6131fa5SGeert Uytterhoeven 
90c6131fa5SGeert Uytterhoeven static void ps3disk_scatter_gather(struct ps3_storage_device *dev,
91c6131fa5SGeert Uytterhoeven 				   struct request *req, int gather)
92c6131fa5SGeert Uytterhoeven {
93c6131fa5SGeert Uytterhoeven 	unsigned int offset = 0;
945705f702SNeilBrown 	struct req_iterator iter;
95c6131fa5SGeert Uytterhoeven 	struct bio_vec *bvec;
965705f702SNeilBrown 	unsigned int i = 0;
97c6131fa5SGeert Uytterhoeven 	size_t size;
98c6131fa5SGeert Uytterhoeven 	void *buf;
99c6131fa5SGeert Uytterhoeven 
1005705f702SNeilBrown 	rq_for_each_segment(bvec, req, iter) {
1015705f702SNeilBrown 		unsigned long flags;
102c6131fa5SGeert Uytterhoeven 		dev_dbg(&dev->sbd.core,
103c6131fa5SGeert Uytterhoeven 			"%s:%u: bio %u: %u segs %u sectors from %lu\n",
1045705f702SNeilBrown 			__func__, __LINE__, i, bio_segments(iter.bio),
10531e103c5SGeert Uytterhoeven 			bio_sectors(iter.bio), iter.bio->bi_sector);
1065705f702SNeilBrown 
107c6131fa5SGeert Uytterhoeven 		size = bvec->bv_len;
1085705f702SNeilBrown 		buf = bvec_kmap_irq(bvec, &flags);
109c6131fa5SGeert Uytterhoeven 		if (gather)
110c6131fa5SGeert Uytterhoeven 			memcpy(dev->bounce_buf+offset, buf, size);
111c6131fa5SGeert Uytterhoeven 		else
112c6131fa5SGeert Uytterhoeven 			memcpy(buf, dev->bounce_buf+offset, size);
113c6131fa5SGeert Uytterhoeven 		offset += size;
1145705f702SNeilBrown 		flush_kernel_dcache_page(bvec->bv_page);
1155705f702SNeilBrown 		bvec_kunmap_irq(bvec, &flags);
116c6131fa5SGeert Uytterhoeven 		i++;
117c6131fa5SGeert Uytterhoeven 	}
118c6131fa5SGeert Uytterhoeven }
119c6131fa5SGeert Uytterhoeven 
120c6131fa5SGeert Uytterhoeven static int ps3disk_submit_request_sg(struct ps3_storage_device *dev,
121c6131fa5SGeert Uytterhoeven 				     struct request *req)
122c6131fa5SGeert Uytterhoeven {
123c6131fa5SGeert Uytterhoeven 	struct ps3disk_private *priv = dev->sbd.core.driver_data;
124c6131fa5SGeert Uytterhoeven 	int write = rq_data_dir(req), res;
125c6131fa5SGeert Uytterhoeven 	const char *op = write ? "write" : "read";
126c6131fa5SGeert Uytterhoeven 	u64 start_sector, sectors;
127c6131fa5SGeert Uytterhoeven 	unsigned int region_id = dev->regions[dev->region_idx].id;
128c6131fa5SGeert Uytterhoeven 
129c6131fa5SGeert Uytterhoeven #ifdef DEBUG
130c6131fa5SGeert Uytterhoeven 	unsigned int n = 0;
1315705f702SNeilBrown 	struct bio_vec *bv;
1325705f702SNeilBrown 	struct req_iterator iter;
133c6131fa5SGeert Uytterhoeven 
1345705f702SNeilBrown 	rq_for_each_segment(bv, req, iter)
135c6131fa5SGeert Uytterhoeven 		n++;
136c6131fa5SGeert Uytterhoeven 	dev_dbg(&dev->sbd.core,
1375705f702SNeilBrown 		"%s:%u: %s req has %u bvecs for %lu sectors %lu hard sectors\n",
138c6131fa5SGeert Uytterhoeven 		__func__, __LINE__, op, n, req->nr_sectors,
139c6131fa5SGeert Uytterhoeven 		req->hard_nr_sectors);
140c6131fa5SGeert Uytterhoeven #endif
141c6131fa5SGeert Uytterhoeven 
142c6131fa5SGeert Uytterhoeven 	start_sector = req->sector * priv->blocking_factor;
143c6131fa5SGeert Uytterhoeven 	sectors = req->nr_sectors * priv->blocking_factor;
144*e377c6e2SStephen Rothwell 	dev_dbg(&dev->sbd.core, "%s:%u: %s %llu sectors starting at %llu\n",
145c6131fa5SGeert Uytterhoeven 		__func__, __LINE__, op, sectors, start_sector);
146c6131fa5SGeert Uytterhoeven 
147c6131fa5SGeert Uytterhoeven 	if (write) {
148c6131fa5SGeert Uytterhoeven 		ps3disk_scatter_gather(dev, req, 1);
149c6131fa5SGeert Uytterhoeven 
150c6131fa5SGeert Uytterhoeven 		res = lv1_storage_write(dev->sbd.dev_id, region_id,
151c6131fa5SGeert Uytterhoeven 					start_sector, sectors, 0,
152c6131fa5SGeert Uytterhoeven 					dev->bounce_lpar, &dev->tag);
153c6131fa5SGeert Uytterhoeven 	} else {
154c6131fa5SGeert Uytterhoeven 		res = lv1_storage_read(dev->sbd.dev_id, region_id,
155c6131fa5SGeert Uytterhoeven 				       start_sector, sectors, 0,
156c6131fa5SGeert Uytterhoeven 				       dev->bounce_lpar, &dev->tag);
157c6131fa5SGeert Uytterhoeven 	}
158c6131fa5SGeert Uytterhoeven 	if (res) {
159c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core, "%s:%u: %s failed %d\n", __func__,
160c6131fa5SGeert Uytterhoeven 			__LINE__, op, res);
161c6131fa5SGeert Uytterhoeven 		end_request(req, 0);
162c6131fa5SGeert Uytterhoeven 		return 0;
163c6131fa5SGeert Uytterhoeven 	}
164c6131fa5SGeert Uytterhoeven 
165c6131fa5SGeert Uytterhoeven 	priv->req = req;
166c6131fa5SGeert Uytterhoeven 	return 1;
167c6131fa5SGeert Uytterhoeven }
168c6131fa5SGeert Uytterhoeven 
169c6131fa5SGeert Uytterhoeven static int ps3disk_submit_flush_request(struct ps3_storage_device *dev,
170c6131fa5SGeert Uytterhoeven 					struct request *req)
171c6131fa5SGeert Uytterhoeven {
172c6131fa5SGeert Uytterhoeven 	struct ps3disk_private *priv = dev->sbd.core.driver_data;
173c6131fa5SGeert Uytterhoeven 	u64 res;
174c6131fa5SGeert Uytterhoeven 
175c6131fa5SGeert Uytterhoeven 	dev_dbg(&dev->sbd.core, "%s:%u: flush request\n", __func__, __LINE__);
176c6131fa5SGeert Uytterhoeven 
177c6131fa5SGeert Uytterhoeven 	res = lv1_storage_send_device_command(dev->sbd.dev_id,
178c6131fa5SGeert Uytterhoeven 					      LV1_STORAGE_ATA_HDDOUT, 0, 0, 0,
179c6131fa5SGeert Uytterhoeven 					      0, &dev->tag);
180c6131fa5SGeert Uytterhoeven 	if (res) {
181*e377c6e2SStephen Rothwell 		dev_err(&dev->sbd.core, "%s:%u: sync cache failed 0x%llx\n",
182c6131fa5SGeert Uytterhoeven 			__func__, __LINE__, res);
183c6131fa5SGeert Uytterhoeven 		end_request(req, 0);
184c6131fa5SGeert Uytterhoeven 		return 0;
185c6131fa5SGeert Uytterhoeven 	}
186c6131fa5SGeert Uytterhoeven 
187c6131fa5SGeert Uytterhoeven 	priv->req = req;
188c6131fa5SGeert Uytterhoeven 	return 1;
189c6131fa5SGeert Uytterhoeven }
190c6131fa5SGeert Uytterhoeven 
191c6131fa5SGeert Uytterhoeven static void ps3disk_do_request(struct ps3_storage_device *dev,
192165125e1SJens Axboe 			       struct request_queue *q)
193c6131fa5SGeert Uytterhoeven {
194c6131fa5SGeert Uytterhoeven 	struct request *req;
195c6131fa5SGeert Uytterhoeven 
196c6131fa5SGeert Uytterhoeven 	dev_dbg(&dev->sbd.core, "%s:%u\n", __func__, __LINE__);
197c6131fa5SGeert Uytterhoeven 
198c6131fa5SGeert Uytterhoeven 	while ((req = elv_next_request(q))) {
199c6131fa5SGeert Uytterhoeven 		if (blk_fs_request(req)) {
200c6131fa5SGeert Uytterhoeven 			if (ps3disk_submit_request_sg(dev, req))
201c6131fa5SGeert Uytterhoeven 				break;
2021a8e2bddSDavid Woodhouse 		} else if (req->cmd_type == REQ_TYPE_LINUX_BLOCK &&
2031a8e2bddSDavid Woodhouse 			   req->cmd[0] == REQ_LB_OP_FLUSH) {
204c6131fa5SGeert Uytterhoeven 			if (ps3disk_submit_flush_request(dev, req))
205c6131fa5SGeert Uytterhoeven 				break;
206c6131fa5SGeert Uytterhoeven 		} else {
207c6131fa5SGeert Uytterhoeven 			blk_dump_rq_flags(req, DEVICE_NAME " bad request");
208c6131fa5SGeert Uytterhoeven 			end_request(req, 0);
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;
217c6131fa5SGeert Uytterhoeven 	struct ps3disk_private *priv = dev->sbd.core.driver_data;
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 	unsigned long num_sectors;
235c6131fa5SGeert Uytterhoeven 	const char *op;
236c6131fa5SGeert Uytterhoeven 
237c6131fa5SGeert Uytterhoeven 	res = lv1_storage_get_async_status(dev->sbd.dev_id, &tag, &status);
238c6131fa5SGeert Uytterhoeven 
239c6131fa5SGeert Uytterhoeven 	if (tag != dev->tag)
240c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core,
241*e377c6e2SStephen Rothwell 			"%s:%u: tag mismatch, got %llx, expected %llx\n",
242c6131fa5SGeert Uytterhoeven 			__func__, __LINE__, tag, dev->tag);
243c6131fa5SGeert Uytterhoeven 
244c6131fa5SGeert Uytterhoeven 	if (res) {
245*e377c6e2SStephen Rothwell 		dev_err(&dev->sbd.core, "%s:%u: res=%d status=0x%llx\n",
246c6131fa5SGeert Uytterhoeven 			__func__, __LINE__, res, status);
247c6131fa5SGeert Uytterhoeven 		return IRQ_HANDLED;
248c6131fa5SGeert Uytterhoeven 	}
249c6131fa5SGeert Uytterhoeven 
250c6131fa5SGeert Uytterhoeven 	priv = dev->sbd.core.driver_data;
251c6131fa5SGeert Uytterhoeven 	req = priv->req;
252c6131fa5SGeert Uytterhoeven 	if (!req) {
253c6131fa5SGeert Uytterhoeven 		dev_dbg(&dev->sbd.core,
254c6131fa5SGeert Uytterhoeven 			"%s:%u non-block layer request completed\n", __func__,
255c6131fa5SGeert Uytterhoeven 			__LINE__);
256c6131fa5SGeert Uytterhoeven 		dev->lv1_status = status;
257c6131fa5SGeert Uytterhoeven 		complete(&dev->done);
258c6131fa5SGeert Uytterhoeven 		return IRQ_HANDLED;
259c6131fa5SGeert Uytterhoeven 	}
260c6131fa5SGeert Uytterhoeven 
2611a8e2bddSDavid Woodhouse 	if (req->cmd_type == REQ_TYPE_LINUX_BLOCK &&
2621a8e2bddSDavid Woodhouse 	    req->cmd[0] == REQ_LB_OP_FLUSH) {
263c6131fa5SGeert Uytterhoeven 		read = 0;
264c6131fa5SGeert Uytterhoeven 		num_sectors = req->hard_cur_sectors;
265c6131fa5SGeert Uytterhoeven 		op = "flush";
266c6131fa5SGeert Uytterhoeven 	} else {
267c6131fa5SGeert Uytterhoeven 		read = !rq_data_dir(req);
268c6131fa5SGeert Uytterhoeven 		num_sectors = req->nr_sectors;
269c6131fa5SGeert Uytterhoeven 		op = read ? "read" : "write";
270c6131fa5SGeert Uytterhoeven 	}
271c6131fa5SGeert Uytterhoeven 	if (status) {
272*e377c6e2SStephen Rothwell 		dev_dbg(&dev->sbd.core, "%s:%u: %s failed 0x%llx\n", __func__,
273c6131fa5SGeert Uytterhoeven 			__LINE__, op, status);
274f01ab252SKiyoshi Ueda 		error = -EIO;
275c6131fa5SGeert Uytterhoeven 	} else {
276c6131fa5SGeert Uytterhoeven 		dev_dbg(&dev->sbd.core, "%s:%u: %s completed\n", __func__,
277c6131fa5SGeert Uytterhoeven 			__LINE__, op);
278f01ab252SKiyoshi Ueda 		error = 0;
279c6131fa5SGeert Uytterhoeven 		if (read)
280c6131fa5SGeert Uytterhoeven 			ps3disk_scatter_gather(dev, req, 0);
281c6131fa5SGeert Uytterhoeven 	}
282c6131fa5SGeert Uytterhoeven 
283c6131fa5SGeert Uytterhoeven 	spin_lock(&priv->lock);
284f01ab252SKiyoshi Ueda 	__blk_end_request(req, error, num_sectors << 9);
285c6131fa5SGeert Uytterhoeven 	priv->req = NULL;
286c6131fa5SGeert Uytterhoeven 	ps3disk_do_request(dev, priv->queue);
287c6131fa5SGeert Uytterhoeven 	spin_unlock(&priv->lock);
288c6131fa5SGeert Uytterhoeven 
289c6131fa5SGeert Uytterhoeven 	return IRQ_HANDLED;
290c6131fa5SGeert Uytterhoeven }
291c6131fa5SGeert Uytterhoeven 
292c6131fa5SGeert Uytterhoeven static int ps3disk_sync_cache(struct ps3_storage_device *dev)
293c6131fa5SGeert Uytterhoeven {
294c6131fa5SGeert Uytterhoeven 	u64 res;
295c6131fa5SGeert Uytterhoeven 
296c6131fa5SGeert Uytterhoeven 	dev_dbg(&dev->sbd.core, "%s:%u: sync cache\n", __func__, __LINE__);
297c6131fa5SGeert Uytterhoeven 
298c6131fa5SGeert Uytterhoeven 	res = ps3stor_send_command(dev, LV1_STORAGE_ATA_HDDOUT, 0, 0, 0, 0);
299c6131fa5SGeert Uytterhoeven 	if (res) {
300*e377c6e2SStephen Rothwell 		dev_err(&dev->sbd.core, "%s:%u: sync cache failed 0x%llx\n",
301c6131fa5SGeert Uytterhoeven 			__func__, __LINE__, res);
302c6131fa5SGeert Uytterhoeven 		return -EIO;
303c6131fa5SGeert Uytterhoeven 	}
304c6131fa5SGeert Uytterhoeven 	return 0;
305c6131fa5SGeert Uytterhoeven }
306c6131fa5SGeert Uytterhoeven 
307c6131fa5SGeert Uytterhoeven 
308c6131fa5SGeert Uytterhoeven /* ATA helpers copied from drivers/ata/libata-core.c */
309c6131fa5SGeert Uytterhoeven 
310c6131fa5SGeert Uytterhoeven static void swap_buf_le16(u16 *buf, unsigned int buf_words)
311c6131fa5SGeert Uytterhoeven {
312c6131fa5SGeert Uytterhoeven #ifdef __BIG_ENDIAN
313c6131fa5SGeert Uytterhoeven 	unsigned int i;
314c6131fa5SGeert Uytterhoeven 
315c6131fa5SGeert Uytterhoeven 	for (i = 0; i < buf_words; i++)
316c6131fa5SGeert Uytterhoeven 		buf[i] = le16_to_cpu(buf[i]);
317c6131fa5SGeert Uytterhoeven #endif /* __BIG_ENDIAN */
318c6131fa5SGeert Uytterhoeven }
319c6131fa5SGeert Uytterhoeven 
320c6131fa5SGeert Uytterhoeven static u64 ata_id_n_sectors(const u16 *id)
321c6131fa5SGeert Uytterhoeven {
322c6131fa5SGeert Uytterhoeven 	if (ata_id_has_lba(id)) {
323c6131fa5SGeert Uytterhoeven 		if (ata_id_has_lba48(id))
324c6131fa5SGeert Uytterhoeven 			return ata_id_u64(id, 100);
325c6131fa5SGeert Uytterhoeven 		else
326c6131fa5SGeert Uytterhoeven 			return ata_id_u32(id, 60);
327c6131fa5SGeert Uytterhoeven 	} else {
328c6131fa5SGeert Uytterhoeven 		if (ata_id_current_chs_valid(id))
329c6131fa5SGeert Uytterhoeven 			return ata_id_u32(id, 57);
330c6131fa5SGeert Uytterhoeven 		else
331c6131fa5SGeert Uytterhoeven 			return id[1] * id[3] * id[6];
332c6131fa5SGeert Uytterhoeven 	}
333c6131fa5SGeert Uytterhoeven }
334c6131fa5SGeert Uytterhoeven 
335c6131fa5SGeert Uytterhoeven static void ata_id_string(const u16 *id, unsigned char *s, unsigned int ofs,
336c6131fa5SGeert Uytterhoeven 			  unsigned int len)
337c6131fa5SGeert Uytterhoeven {
338c6131fa5SGeert Uytterhoeven 	unsigned int c;
339c6131fa5SGeert Uytterhoeven 
340c6131fa5SGeert Uytterhoeven 	while (len > 0) {
341c6131fa5SGeert Uytterhoeven 		c = id[ofs] >> 8;
342c6131fa5SGeert Uytterhoeven 		*s = c;
343c6131fa5SGeert Uytterhoeven 		s++;
344c6131fa5SGeert Uytterhoeven 
345c6131fa5SGeert Uytterhoeven 		c = id[ofs] & 0xff;
346c6131fa5SGeert Uytterhoeven 		*s = c;
347c6131fa5SGeert Uytterhoeven 		s++;
348c6131fa5SGeert Uytterhoeven 
349c6131fa5SGeert Uytterhoeven 		ofs++;
350c6131fa5SGeert Uytterhoeven 		len -= 2;
351c6131fa5SGeert Uytterhoeven 	}
352c6131fa5SGeert Uytterhoeven }
353c6131fa5SGeert Uytterhoeven 
354c6131fa5SGeert Uytterhoeven static void ata_id_c_string(const u16 *id, unsigned char *s, unsigned int ofs,
355c6131fa5SGeert Uytterhoeven 			    unsigned int len)
356c6131fa5SGeert Uytterhoeven {
357c6131fa5SGeert Uytterhoeven 	unsigned char *p;
358c6131fa5SGeert Uytterhoeven 
359c6131fa5SGeert Uytterhoeven 	WARN_ON(!(len & 1));
360c6131fa5SGeert Uytterhoeven 
361c6131fa5SGeert Uytterhoeven 	ata_id_string(id, s, ofs, len - 1);
362c6131fa5SGeert Uytterhoeven 
363c6131fa5SGeert Uytterhoeven 	p = s + strnlen(s, len - 1);
364c6131fa5SGeert Uytterhoeven 	while (p > s && p[-1] == ' ')
365c6131fa5SGeert Uytterhoeven 		p--;
366c6131fa5SGeert Uytterhoeven 	*p = '\0';
367c6131fa5SGeert Uytterhoeven }
368c6131fa5SGeert Uytterhoeven 
369c6131fa5SGeert Uytterhoeven static int ps3disk_identify(struct ps3_storage_device *dev)
370c6131fa5SGeert Uytterhoeven {
371c6131fa5SGeert Uytterhoeven 	struct ps3disk_private *priv = dev->sbd.core.driver_data;
372c6131fa5SGeert Uytterhoeven 	struct lv1_ata_cmnd_block ata_cmnd;
373c6131fa5SGeert Uytterhoeven 	u16 *id = dev->bounce_buf;
374c6131fa5SGeert Uytterhoeven 	u64 res;
375c6131fa5SGeert Uytterhoeven 
376c6131fa5SGeert Uytterhoeven 	dev_dbg(&dev->sbd.core, "%s:%u: identify disk\n", __func__, __LINE__);
377c6131fa5SGeert Uytterhoeven 
378c6131fa5SGeert Uytterhoeven 	memset(&ata_cmnd, 0, sizeof(struct lv1_ata_cmnd_block));
379c6131fa5SGeert Uytterhoeven 	ata_cmnd.command = ATA_CMD_ID_ATA;
380c6131fa5SGeert Uytterhoeven 	ata_cmnd.sector_count = 1;
381c6131fa5SGeert Uytterhoeven 	ata_cmnd.size = ata_cmnd.arglen = ATA_ID_WORDS * 2;
382c6131fa5SGeert Uytterhoeven 	ata_cmnd.buffer = dev->bounce_lpar;
383c6131fa5SGeert Uytterhoeven 	ata_cmnd.proto = PIO_DATA_IN_PROTO;
384c6131fa5SGeert Uytterhoeven 	ata_cmnd.in_out = DIR_READ;
385c6131fa5SGeert Uytterhoeven 
386c6131fa5SGeert Uytterhoeven 	res = ps3stor_send_command(dev, LV1_STORAGE_SEND_ATA_COMMAND,
387c6131fa5SGeert Uytterhoeven 				   ps3_mm_phys_to_lpar(__pa(&ata_cmnd)),
388c6131fa5SGeert Uytterhoeven 				   sizeof(ata_cmnd), ata_cmnd.buffer,
389c6131fa5SGeert Uytterhoeven 				   ata_cmnd.arglen);
390c6131fa5SGeert Uytterhoeven 	if (res) {
391*e377c6e2SStephen Rothwell 		dev_err(&dev->sbd.core, "%s:%u: identify disk failed 0x%llx\n",
392c6131fa5SGeert Uytterhoeven 			__func__, __LINE__, res);
393c6131fa5SGeert Uytterhoeven 		return -EIO;
394c6131fa5SGeert Uytterhoeven 	}
395c6131fa5SGeert Uytterhoeven 
396c6131fa5SGeert Uytterhoeven 	swap_buf_le16(id, ATA_ID_WORDS);
397c6131fa5SGeert Uytterhoeven 
398c6131fa5SGeert Uytterhoeven 	/* All we're interested in are raw capacity and model name */
399c6131fa5SGeert Uytterhoeven 	priv->raw_capacity = ata_id_n_sectors(id);
400c6131fa5SGeert Uytterhoeven 	ata_id_c_string(id, priv->model, ATA_ID_PROD, sizeof(priv->model));
401c6131fa5SGeert Uytterhoeven 	return 0;
402c6131fa5SGeert Uytterhoeven }
403c6131fa5SGeert Uytterhoeven 
404165125e1SJens Axboe static void ps3disk_prepare_flush(struct request_queue *q, struct request *req)
405c6131fa5SGeert Uytterhoeven {
406c6131fa5SGeert Uytterhoeven 	struct ps3_storage_device *dev = q->queuedata;
407c6131fa5SGeert Uytterhoeven 
408c6131fa5SGeert Uytterhoeven 	dev_dbg(&dev->sbd.core, "%s:%u\n", __func__, __LINE__);
409c6131fa5SGeert Uytterhoeven 
4101a8e2bddSDavid Woodhouse 	req->cmd_type = REQ_TYPE_LINUX_BLOCK;
4111a8e2bddSDavid Woodhouse 	req->cmd[0] = REQ_LB_OP_FLUSH;
412c6131fa5SGeert Uytterhoeven }
413c6131fa5SGeert Uytterhoeven 
414c6131fa5SGeert Uytterhoeven static unsigned long ps3disk_mask;
415c6131fa5SGeert Uytterhoeven 
416c6131fa5SGeert Uytterhoeven static DEFINE_MUTEX(ps3disk_mask_mutex);
417c6131fa5SGeert Uytterhoeven 
418c6131fa5SGeert Uytterhoeven static int __devinit ps3disk_probe(struct ps3_system_bus_device *_dev)
419c6131fa5SGeert Uytterhoeven {
420c6131fa5SGeert Uytterhoeven 	struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
421c6131fa5SGeert Uytterhoeven 	struct ps3disk_private *priv;
422c6131fa5SGeert Uytterhoeven 	int error;
423c6131fa5SGeert Uytterhoeven 	unsigned int devidx;
424c6131fa5SGeert Uytterhoeven 	struct request_queue *queue;
425c6131fa5SGeert Uytterhoeven 	struct gendisk *gendisk;
426c6131fa5SGeert Uytterhoeven 
427c6131fa5SGeert Uytterhoeven 	if (dev->blk_size < 512) {
428c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core,
429*e377c6e2SStephen Rothwell 			"%s:%u: cannot handle block size %llu\n", __func__,
430c6131fa5SGeert Uytterhoeven 			__LINE__, dev->blk_size);
431c6131fa5SGeert Uytterhoeven 		return -EINVAL;
432c6131fa5SGeert Uytterhoeven 	}
433c6131fa5SGeert Uytterhoeven 
434c6131fa5SGeert Uytterhoeven 	BUILD_BUG_ON(PS3DISK_MAX_DISKS > BITS_PER_LONG);
435c6131fa5SGeert Uytterhoeven 	mutex_lock(&ps3disk_mask_mutex);
436c6131fa5SGeert Uytterhoeven 	devidx = find_first_zero_bit(&ps3disk_mask, PS3DISK_MAX_DISKS);
437c6131fa5SGeert Uytterhoeven 	if (devidx >= PS3DISK_MAX_DISKS) {
438c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core, "%s:%u: Too many disks\n", __func__,
439c6131fa5SGeert Uytterhoeven 			__LINE__);
440c6131fa5SGeert Uytterhoeven 		mutex_unlock(&ps3disk_mask_mutex);
441c6131fa5SGeert Uytterhoeven 		return -ENOSPC;
442c6131fa5SGeert Uytterhoeven 	}
443c6131fa5SGeert Uytterhoeven 	__set_bit(devidx, &ps3disk_mask);
444c6131fa5SGeert Uytterhoeven 	mutex_unlock(&ps3disk_mask_mutex);
445c6131fa5SGeert Uytterhoeven 
446c6131fa5SGeert Uytterhoeven 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
447c6131fa5SGeert Uytterhoeven 	if (!priv) {
448c6131fa5SGeert Uytterhoeven 		error = -ENOMEM;
449c6131fa5SGeert Uytterhoeven 		goto fail;
450c6131fa5SGeert Uytterhoeven 	}
451c6131fa5SGeert Uytterhoeven 
452c6131fa5SGeert Uytterhoeven 	dev->sbd.core.driver_data = priv;
453c6131fa5SGeert Uytterhoeven 	spin_lock_init(&priv->lock);
454c6131fa5SGeert Uytterhoeven 
455c6131fa5SGeert Uytterhoeven 	dev->bounce_size = BOUNCE_SIZE;
456c6131fa5SGeert Uytterhoeven 	dev->bounce_buf = kmalloc(BOUNCE_SIZE, GFP_DMA);
457c6131fa5SGeert Uytterhoeven 	if (!dev->bounce_buf) {
458c6131fa5SGeert Uytterhoeven 		error = -ENOMEM;
459c6131fa5SGeert Uytterhoeven 		goto fail_free_priv;
460c6131fa5SGeert Uytterhoeven 	}
461c6131fa5SGeert Uytterhoeven 
462c6131fa5SGeert Uytterhoeven 	error = ps3stor_setup(dev, ps3disk_interrupt);
463c6131fa5SGeert Uytterhoeven 	if (error)
464c6131fa5SGeert Uytterhoeven 		goto fail_free_bounce;
465c6131fa5SGeert Uytterhoeven 
466c6131fa5SGeert Uytterhoeven 	ps3disk_identify(dev);
467c6131fa5SGeert Uytterhoeven 
468c6131fa5SGeert Uytterhoeven 	queue = blk_init_queue(ps3disk_request, &priv->lock);
469c6131fa5SGeert Uytterhoeven 	if (!queue) {
470c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core, "%s:%u: blk_init_queue failed\n",
471c6131fa5SGeert Uytterhoeven 			__func__, __LINE__);
472c6131fa5SGeert Uytterhoeven 		error = -ENOMEM;
473c6131fa5SGeert Uytterhoeven 		goto fail_teardown;
474c6131fa5SGeert Uytterhoeven 	}
475c6131fa5SGeert Uytterhoeven 
476c6131fa5SGeert Uytterhoeven 	priv->queue = queue;
477c6131fa5SGeert Uytterhoeven 	queue->queuedata = dev;
478c6131fa5SGeert Uytterhoeven 
479c6131fa5SGeert Uytterhoeven 	blk_queue_bounce_limit(queue, BLK_BOUNCE_HIGH);
480c6131fa5SGeert Uytterhoeven 
481c6131fa5SGeert Uytterhoeven 	blk_queue_max_sectors(queue, dev->bounce_size >> 9);
482c6131fa5SGeert Uytterhoeven 	blk_queue_segment_boundary(queue, -1UL);
483c6131fa5SGeert Uytterhoeven 	blk_queue_dma_alignment(queue, dev->blk_size-1);
484c6131fa5SGeert Uytterhoeven 	blk_queue_hardsect_size(queue, dev->blk_size);
485c6131fa5SGeert Uytterhoeven 
486c6131fa5SGeert Uytterhoeven 	blk_queue_ordered(queue, QUEUE_ORDERED_DRAIN_FLUSH,
487c6131fa5SGeert Uytterhoeven 			  ps3disk_prepare_flush);
488c6131fa5SGeert Uytterhoeven 
489c6131fa5SGeert Uytterhoeven 	blk_queue_max_phys_segments(queue, -1);
490c6131fa5SGeert Uytterhoeven 	blk_queue_max_hw_segments(queue, -1);
491c6131fa5SGeert Uytterhoeven 	blk_queue_max_segment_size(queue, dev->bounce_size);
492c6131fa5SGeert Uytterhoeven 
493c6131fa5SGeert Uytterhoeven 	gendisk = alloc_disk(PS3DISK_MINORS);
494c6131fa5SGeert Uytterhoeven 	if (!gendisk) {
495c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core, "%s:%u: alloc_disk failed\n", __func__,
496c6131fa5SGeert Uytterhoeven 			__LINE__);
497c6131fa5SGeert Uytterhoeven 		error = -ENOMEM;
498c6131fa5SGeert Uytterhoeven 		goto fail_cleanup_queue;
499c6131fa5SGeert Uytterhoeven 	}
500c6131fa5SGeert Uytterhoeven 
501c6131fa5SGeert Uytterhoeven 	priv->gendisk = gendisk;
502c6131fa5SGeert Uytterhoeven 	gendisk->major = ps3disk_major;
503c6131fa5SGeert Uytterhoeven 	gendisk->first_minor = devidx * PS3DISK_MINORS;
504c6131fa5SGeert Uytterhoeven 	gendisk->fops = &ps3disk_fops;
505c6131fa5SGeert Uytterhoeven 	gendisk->queue = queue;
506c6131fa5SGeert Uytterhoeven 	gendisk->private_data = dev;
507c6131fa5SGeert Uytterhoeven 	gendisk->driverfs_dev = &dev->sbd.core;
508c6131fa5SGeert Uytterhoeven 	snprintf(gendisk->disk_name, sizeof(gendisk->disk_name), PS3DISK_NAME,
509c6131fa5SGeert Uytterhoeven 		 devidx+'a');
510c6131fa5SGeert Uytterhoeven 	priv->blocking_factor = dev->blk_size >> 9;
511c6131fa5SGeert Uytterhoeven 	set_capacity(gendisk,
512c6131fa5SGeert Uytterhoeven 		     dev->regions[dev->region_idx].size*priv->blocking_factor);
513c6131fa5SGeert Uytterhoeven 
514c6131fa5SGeert Uytterhoeven 	dev_info(&dev->sbd.core,
515*e377c6e2SStephen Rothwell 		 "%s is a %s (%llu MiB total, %lu MiB for OtherOS)\n",
516c6131fa5SGeert Uytterhoeven 		 gendisk->disk_name, priv->model, priv->raw_capacity >> 11,
517c6131fa5SGeert Uytterhoeven 		 get_capacity(gendisk) >> 11);
518c6131fa5SGeert Uytterhoeven 
519c6131fa5SGeert Uytterhoeven 	add_disk(gendisk);
520c6131fa5SGeert Uytterhoeven 	return 0;
521c6131fa5SGeert Uytterhoeven 
522c6131fa5SGeert Uytterhoeven fail_cleanup_queue:
523c6131fa5SGeert Uytterhoeven 	blk_cleanup_queue(queue);
524c6131fa5SGeert Uytterhoeven fail_teardown:
525c6131fa5SGeert Uytterhoeven 	ps3stor_teardown(dev);
526c6131fa5SGeert Uytterhoeven fail_free_bounce:
527c6131fa5SGeert Uytterhoeven 	kfree(dev->bounce_buf);
528c6131fa5SGeert Uytterhoeven fail_free_priv:
529c6131fa5SGeert Uytterhoeven 	kfree(priv);
530c6131fa5SGeert Uytterhoeven 	dev->sbd.core.driver_data = NULL;
531c6131fa5SGeert Uytterhoeven fail:
532c6131fa5SGeert Uytterhoeven 	mutex_lock(&ps3disk_mask_mutex);
533c6131fa5SGeert Uytterhoeven 	__clear_bit(devidx, &ps3disk_mask);
534c6131fa5SGeert Uytterhoeven 	mutex_unlock(&ps3disk_mask_mutex);
535c6131fa5SGeert Uytterhoeven 	return error;
536c6131fa5SGeert Uytterhoeven }
537c6131fa5SGeert Uytterhoeven 
538c6131fa5SGeert Uytterhoeven static int ps3disk_remove(struct ps3_system_bus_device *_dev)
539c6131fa5SGeert Uytterhoeven {
540c6131fa5SGeert Uytterhoeven 	struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
541c6131fa5SGeert Uytterhoeven 	struct ps3disk_private *priv = dev->sbd.core.driver_data;
542c6131fa5SGeert Uytterhoeven 
543c6131fa5SGeert Uytterhoeven 	mutex_lock(&ps3disk_mask_mutex);
544f331c029STejun Heo 	__clear_bit(MINOR(disk_devt(priv->gendisk)) / PS3DISK_MINORS,
545c6131fa5SGeert Uytterhoeven 		    &ps3disk_mask);
546c6131fa5SGeert Uytterhoeven 	mutex_unlock(&ps3disk_mask_mutex);
547c6131fa5SGeert Uytterhoeven 	del_gendisk(priv->gendisk);
548c6131fa5SGeert Uytterhoeven 	blk_cleanup_queue(priv->queue);
549c6131fa5SGeert Uytterhoeven 	put_disk(priv->gendisk);
550c6131fa5SGeert Uytterhoeven 	dev_notice(&dev->sbd.core, "Synchronizing disk cache\n");
551c6131fa5SGeert Uytterhoeven 	ps3disk_sync_cache(dev);
552c6131fa5SGeert Uytterhoeven 	ps3stor_teardown(dev);
553c6131fa5SGeert Uytterhoeven 	kfree(dev->bounce_buf);
554c6131fa5SGeert Uytterhoeven 	kfree(priv);
555c6131fa5SGeert Uytterhoeven 	dev->sbd.core.driver_data = NULL;
556c6131fa5SGeert Uytterhoeven 	return 0;
557c6131fa5SGeert Uytterhoeven }
558c6131fa5SGeert Uytterhoeven 
559c6131fa5SGeert Uytterhoeven static struct ps3_system_bus_driver ps3disk = {
560c6131fa5SGeert Uytterhoeven 	.match_id	= PS3_MATCH_ID_STOR_DISK,
561c6131fa5SGeert Uytterhoeven 	.core.name	= DEVICE_NAME,
562c6131fa5SGeert Uytterhoeven 	.core.owner	= THIS_MODULE,
563c6131fa5SGeert Uytterhoeven 	.probe		= ps3disk_probe,
564c6131fa5SGeert Uytterhoeven 	.remove		= ps3disk_remove,
565c6131fa5SGeert Uytterhoeven 	.shutdown	= ps3disk_remove,
566c6131fa5SGeert Uytterhoeven };
567c6131fa5SGeert Uytterhoeven 
568c6131fa5SGeert Uytterhoeven 
569c6131fa5SGeert Uytterhoeven static int __init ps3disk_init(void)
570c6131fa5SGeert Uytterhoeven {
571c6131fa5SGeert Uytterhoeven 	int error;
572c6131fa5SGeert Uytterhoeven 
573c6131fa5SGeert Uytterhoeven 	if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
574c6131fa5SGeert Uytterhoeven 		return -ENODEV;
575c6131fa5SGeert Uytterhoeven 
576c6131fa5SGeert Uytterhoeven 	error = register_blkdev(0, DEVICE_NAME);
577c6131fa5SGeert Uytterhoeven 	if (error <= 0) {
578c6131fa5SGeert Uytterhoeven 		printk(KERN_ERR "%s:%u: register_blkdev failed %d\n", __func__,
579c6131fa5SGeert Uytterhoeven 		       __LINE__, error);
580c6131fa5SGeert Uytterhoeven 		return error;
581c6131fa5SGeert Uytterhoeven 	}
582c6131fa5SGeert Uytterhoeven 	ps3disk_major = error;
583c6131fa5SGeert Uytterhoeven 
584c6131fa5SGeert Uytterhoeven 	pr_info("%s:%u: registered block device major %d\n", __func__,
585c6131fa5SGeert Uytterhoeven 		__LINE__, ps3disk_major);
586c6131fa5SGeert Uytterhoeven 
587c6131fa5SGeert Uytterhoeven 	error = ps3_system_bus_driver_register(&ps3disk);
588c6131fa5SGeert Uytterhoeven 	if (error)
589c6131fa5SGeert Uytterhoeven 		unregister_blkdev(ps3disk_major, DEVICE_NAME);
590c6131fa5SGeert Uytterhoeven 
591c6131fa5SGeert Uytterhoeven 	return error;
592c6131fa5SGeert Uytterhoeven }
593c6131fa5SGeert Uytterhoeven 
594c6131fa5SGeert Uytterhoeven static void __exit ps3disk_exit(void)
595c6131fa5SGeert Uytterhoeven {
596c6131fa5SGeert Uytterhoeven 	ps3_system_bus_driver_unregister(&ps3disk);
597c6131fa5SGeert Uytterhoeven 	unregister_blkdev(ps3disk_major, DEVICE_NAME);
598c6131fa5SGeert Uytterhoeven }
599c6131fa5SGeert Uytterhoeven 
600c6131fa5SGeert Uytterhoeven module_init(ps3disk_init);
601c6131fa5SGeert Uytterhoeven module_exit(ps3disk_exit);
602c6131fa5SGeert Uytterhoeven 
603c6131fa5SGeert Uytterhoeven MODULE_LICENSE("GPL");
604c6131fa5SGeert Uytterhoeven MODULE_DESCRIPTION("PS3 Disk Storage Driver");
605c6131fa5SGeert Uytterhoeven MODULE_AUTHOR("Sony Corporation");
606c6131fa5SGeert Uytterhoeven MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_DISK);
607