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 */
32fab1adcfSJens Axboe struct blk_mq_tag_set tag_set;
33c6131fa5SGeert Uytterhoeven struct gendisk *gendisk;
34c6131fa5SGeert Uytterhoeven unsigned int blocking_factor;
35c6131fa5SGeert Uytterhoeven struct request *req;
36c6131fa5SGeert Uytterhoeven u64 raw_capacity;
37c6131fa5SGeert Uytterhoeven unsigned char model[ATA_ID_PROD_LEN+1];
38c6131fa5SGeert Uytterhoeven };
39c6131fa5SGeert Uytterhoeven
40c6131fa5SGeert Uytterhoeven
41c6131fa5SGeert Uytterhoeven #define LV1_STORAGE_SEND_ATA_COMMAND (2)
42c6131fa5SGeert Uytterhoeven #define LV1_STORAGE_ATA_HDDOUT (0x23)
43c6131fa5SGeert Uytterhoeven
44c6131fa5SGeert Uytterhoeven struct lv1_ata_cmnd_block {
45c6131fa5SGeert Uytterhoeven u16 features;
46c6131fa5SGeert Uytterhoeven u16 sector_count;
47c6131fa5SGeert Uytterhoeven u16 LBA_low;
48c6131fa5SGeert Uytterhoeven u16 LBA_mid;
49c6131fa5SGeert Uytterhoeven u16 LBA_high;
50c6131fa5SGeert Uytterhoeven u8 device;
51c6131fa5SGeert Uytterhoeven u8 command;
52c6131fa5SGeert Uytterhoeven u32 is_ext;
53c6131fa5SGeert Uytterhoeven u32 proto;
54c6131fa5SGeert Uytterhoeven u32 in_out;
55c6131fa5SGeert Uytterhoeven u32 size;
56c6131fa5SGeert Uytterhoeven u64 buffer;
57c6131fa5SGeert Uytterhoeven u32 arglen;
58c6131fa5SGeert Uytterhoeven };
59c6131fa5SGeert Uytterhoeven
60c6131fa5SGeert Uytterhoeven enum lv1_ata_proto {
61c6131fa5SGeert Uytterhoeven NON_DATA_PROTO = 0,
62c6131fa5SGeert Uytterhoeven PIO_DATA_IN_PROTO = 1,
63c6131fa5SGeert Uytterhoeven PIO_DATA_OUT_PROTO = 2,
64c6131fa5SGeert Uytterhoeven DMA_PROTO = 3
65c6131fa5SGeert Uytterhoeven };
66c6131fa5SGeert Uytterhoeven
67c6131fa5SGeert Uytterhoeven enum lv1_ata_in_out {
68c6131fa5SGeert Uytterhoeven DIR_WRITE = 0, /* memory -> device */
69c6131fa5SGeert Uytterhoeven DIR_READ = 1 /* device -> memory */
70c6131fa5SGeert Uytterhoeven };
71c6131fa5SGeert Uytterhoeven
72c6131fa5SGeert Uytterhoeven static int ps3disk_major;
73c6131fa5SGeert Uytterhoeven
74c6131fa5SGeert Uytterhoeven
7583d5cde4SAlexey Dobriyan static const struct block_device_operations ps3disk_fops = {
76c6131fa5SGeert Uytterhoeven .owner = THIS_MODULE,
77c6131fa5SGeert Uytterhoeven };
78c6131fa5SGeert Uytterhoeven
79c6131fa5SGeert Uytterhoeven
ps3disk_scatter_gather(struct ps3_storage_device * dev,struct request * req,int gather)80c6131fa5SGeert Uytterhoeven static void ps3disk_scatter_gather(struct ps3_storage_device *dev,
81c6131fa5SGeert Uytterhoeven struct request *req, int gather)
82c6131fa5SGeert Uytterhoeven {
83c6131fa5SGeert Uytterhoeven unsigned int offset = 0;
845705f702SNeilBrown struct req_iterator iter;
857988613bSKent Overstreet struct bio_vec bvec;
86c6131fa5SGeert Uytterhoeven
875705f702SNeilBrown rq_for_each_segment(bvec, req, iter) {
88c6131fa5SGeert Uytterhoeven if (gather)
896e0a4855SChristoph Hellwig memcpy_from_bvec(dev->bounce_buf + offset, &bvec);
90c6131fa5SGeert Uytterhoeven else
916e0a4855SChristoph Hellwig memcpy_to_bvec(&bvec, dev->bounce_buf + offset);
92c6131fa5SGeert Uytterhoeven }
93c6131fa5SGeert Uytterhoeven }
94c6131fa5SGeert Uytterhoeven
ps3disk_submit_request_sg(struct ps3_storage_device * dev,struct request * req)95fab1adcfSJens Axboe static blk_status_t ps3disk_submit_request_sg(struct ps3_storage_device *dev,
96c6131fa5SGeert Uytterhoeven struct request *req)
97c6131fa5SGeert Uytterhoeven {
9803fa68c2SGeert Uytterhoeven struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
99c6131fa5SGeert Uytterhoeven int write = rq_data_dir(req), res;
100c6131fa5SGeert Uytterhoeven const char *op = write ? "write" : "read";
101c6131fa5SGeert Uytterhoeven u64 start_sector, sectors;
102c6131fa5SGeert Uytterhoeven unsigned int region_id = dev->regions[dev->region_idx].id;
103c6131fa5SGeert Uytterhoeven
104c6131fa5SGeert Uytterhoeven #ifdef DEBUG
105c6131fa5SGeert Uytterhoeven unsigned int n = 0;
1067988613bSKent Overstreet struct bio_vec bv;
1075705f702SNeilBrown struct req_iterator iter;
108c6131fa5SGeert Uytterhoeven
1095705f702SNeilBrown rq_for_each_segment(bv, req, iter)
110c6131fa5SGeert Uytterhoeven n++;
111c6131fa5SGeert Uytterhoeven dev_dbg(&dev->sbd.core,
11283096ebfSTejun Heo "%s:%u: %s req has %u bvecs for %u sectors\n",
11383096ebfSTejun Heo __func__, __LINE__, op, n, blk_rq_sectors(req));
114c6131fa5SGeert Uytterhoeven #endif
115c6131fa5SGeert Uytterhoeven
11683096ebfSTejun Heo start_sector = blk_rq_pos(req) * priv->blocking_factor;
11783096ebfSTejun Heo sectors = blk_rq_sectors(req) * priv->blocking_factor;
118e377c6e2SStephen Rothwell dev_dbg(&dev->sbd.core, "%s:%u: %s %llu sectors starting at %llu\n",
119c6131fa5SGeert Uytterhoeven __func__, __LINE__, op, sectors, start_sector);
120c6131fa5SGeert Uytterhoeven
121c6131fa5SGeert Uytterhoeven if (write) {
122c6131fa5SGeert Uytterhoeven ps3disk_scatter_gather(dev, req, 1);
123c6131fa5SGeert Uytterhoeven
124c6131fa5SGeert Uytterhoeven res = lv1_storage_write(dev->sbd.dev_id, region_id,
125c6131fa5SGeert Uytterhoeven start_sector, sectors, 0,
126c6131fa5SGeert Uytterhoeven dev->bounce_lpar, &dev->tag);
127c6131fa5SGeert Uytterhoeven } else {
128c6131fa5SGeert Uytterhoeven res = lv1_storage_read(dev->sbd.dev_id, region_id,
129c6131fa5SGeert Uytterhoeven start_sector, sectors, 0,
130c6131fa5SGeert Uytterhoeven dev->bounce_lpar, &dev->tag);
131c6131fa5SGeert Uytterhoeven }
132c6131fa5SGeert Uytterhoeven if (res) {
133c6131fa5SGeert Uytterhoeven dev_err(&dev->sbd.core, "%s:%u: %s failed %d\n", __func__,
134c6131fa5SGeert Uytterhoeven __LINE__, op, res);
135fab1adcfSJens Axboe return BLK_STS_IOERR;
136c6131fa5SGeert Uytterhoeven }
137c6131fa5SGeert Uytterhoeven
138c6131fa5SGeert Uytterhoeven priv->req = req;
139fab1adcfSJens Axboe return BLK_STS_OK;
140c6131fa5SGeert Uytterhoeven }
141c6131fa5SGeert Uytterhoeven
ps3disk_submit_flush_request(struct ps3_storage_device * dev,struct request * req)142fab1adcfSJens Axboe static blk_status_t ps3disk_submit_flush_request(struct ps3_storage_device *dev,
143c6131fa5SGeert Uytterhoeven struct request *req)
144c6131fa5SGeert Uytterhoeven {
14503fa68c2SGeert Uytterhoeven struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
146c6131fa5SGeert Uytterhoeven u64 res;
147c6131fa5SGeert Uytterhoeven
148c6131fa5SGeert Uytterhoeven dev_dbg(&dev->sbd.core, "%s:%u: flush request\n", __func__, __LINE__);
149c6131fa5SGeert Uytterhoeven
150c6131fa5SGeert Uytterhoeven res = lv1_storage_send_device_command(dev->sbd.dev_id,
151c6131fa5SGeert Uytterhoeven LV1_STORAGE_ATA_HDDOUT, 0, 0, 0,
152c6131fa5SGeert Uytterhoeven 0, &dev->tag);
153c6131fa5SGeert Uytterhoeven if (res) {
154e377c6e2SStephen Rothwell dev_err(&dev->sbd.core, "%s:%u: sync cache failed 0x%llx\n",
155c6131fa5SGeert Uytterhoeven __func__, __LINE__, res);
156fab1adcfSJens Axboe return BLK_STS_IOERR;
157c6131fa5SGeert Uytterhoeven }
158c6131fa5SGeert Uytterhoeven
159c6131fa5SGeert Uytterhoeven priv->req = req;
160fab1adcfSJens Axboe return BLK_STS_OK;
161c6131fa5SGeert Uytterhoeven }
162c6131fa5SGeert Uytterhoeven
ps3disk_do_request(struct ps3_storage_device * dev,struct request * req)163fab1adcfSJens Axboe static blk_status_t ps3disk_do_request(struct ps3_storage_device *dev,
164fab1adcfSJens Axboe struct request *req)
165c6131fa5SGeert Uytterhoeven {
166c6131fa5SGeert Uytterhoeven dev_dbg(&dev->sbd.core, "%s:%u\n", __func__, __LINE__);
167c6131fa5SGeert Uytterhoeven
168aebf526bSChristoph Hellwig switch (req_op(req)) {
169aebf526bSChristoph Hellwig case REQ_OP_FLUSH:
170fab1adcfSJens Axboe return ps3disk_submit_flush_request(dev, req);
171aebf526bSChristoph Hellwig case REQ_OP_READ:
172aebf526bSChristoph Hellwig case REQ_OP_WRITE:
173fab1adcfSJens Axboe return ps3disk_submit_request_sg(dev, req);
174aebf526bSChristoph Hellwig default:
175c6131fa5SGeert Uytterhoeven blk_dump_rq_flags(req, DEVICE_NAME " bad request");
176fab1adcfSJens Axboe return BLK_STS_IOERR;
177c6131fa5SGeert Uytterhoeven }
178c6131fa5SGeert Uytterhoeven }
179c6131fa5SGeert Uytterhoeven
ps3disk_queue_rq(struct blk_mq_hw_ctx * hctx,const struct blk_mq_queue_data * bd)180fab1adcfSJens Axboe static blk_status_t ps3disk_queue_rq(struct blk_mq_hw_ctx *hctx,
181fab1adcfSJens Axboe const struct blk_mq_queue_data *bd)
182c6131fa5SGeert Uytterhoeven {
183fab1adcfSJens Axboe struct request_queue *q = hctx->queue;
184c6131fa5SGeert Uytterhoeven struct ps3_storage_device *dev = q->queuedata;
18503fa68c2SGeert Uytterhoeven struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
186fab1adcfSJens Axboe blk_status_t ret;
187c6131fa5SGeert Uytterhoeven
188fab1adcfSJens Axboe blk_mq_start_request(bd->rq);
189c6131fa5SGeert Uytterhoeven
190fab1adcfSJens Axboe spin_lock_irq(&priv->lock);
191fab1adcfSJens Axboe ret = ps3disk_do_request(dev, bd->rq);
192fab1adcfSJens Axboe spin_unlock_irq(&priv->lock);
193fab1adcfSJens Axboe
194fab1adcfSJens Axboe return ret;
195c6131fa5SGeert Uytterhoeven }
196c6131fa5SGeert Uytterhoeven
ps3disk_interrupt(int irq,void * data)197c6131fa5SGeert Uytterhoeven static irqreturn_t ps3disk_interrupt(int irq, void *data)
198c6131fa5SGeert Uytterhoeven {
199c6131fa5SGeert Uytterhoeven struct ps3_storage_device *dev = data;
200c6131fa5SGeert Uytterhoeven struct ps3disk_private *priv;
201c6131fa5SGeert Uytterhoeven struct request *req;
2022a842acaSChristoph Hellwig int res, read;
2032a842acaSChristoph Hellwig blk_status_t error;
204c6131fa5SGeert Uytterhoeven u64 tag, status;
205c6131fa5SGeert Uytterhoeven const char *op;
206c6131fa5SGeert Uytterhoeven
207c6131fa5SGeert Uytterhoeven res = lv1_storage_get_async_status(dev->sbd.dev_id, &tag, &status);
208c6131fa5SGeert Uytterhoeven
209c6131fa5SGeert Uytterhoeven if (tag != dev->tag)
210c6131fa5SGeert Uytterhoeven dev_err(&dev->sbd.core,
211e377c6e2SStephen Rothwell "%s:%u: tag mismatch, got %llx, expected %llx\n",
212c6131fa5SGeert Uytterhoeven __func__, __LINE__, tag, dev->tag);
213c6131fa5SGeert Uytterhoeven
214c6131fa5SGeert Uytterhoeven if (res) {
215e377c6e2SStephen Rothwell dev_err(&dev->sbd.core, "%s:%u: res=%d status=0x%llx\n",
216c6131fa5SGeert Uytterhoeven __func__, __LINE__, res, status);
217c6131fa5SGeert Uytterhoeven return IRQ_HANDLED;
218c6131fa5SGeert Uytterhoeven }
219c6131fa5SGeert Uytterhoeven
22003fa68c2SGeert Uytterhoeven priv = ps3_system_bus_get_drvdata(&dev->sbd);
221c6131fa5SGeert Uytterhoeven req = priv->req;
222c6131fa5SGeert Uytterhoeven if (!req) {
223c6131fa5SGeert Uytterhoeven dev_dbg(&dev->sbd.core,
224c6131fa5SGeert Uytterhoeven "%s:%u non-block layer request completed\n", __func__,
225c6131fa5SGeert Uytterhoeven __LINE__);
226c6131fa5SGeert Uytterhoeven dev->lv1_status = status;
227c6131fa5SGeert Uytterhoeven complete(&dev->done);
228c6131fa5SGeert Uytterhoeven return IRQ_HANDLED;
229c6131fa5SGeert Uytterhoeven }
230c6131fa5SGeert Uytterhoeven
2313a5e02ceSMike Christie if (req_op(req) == REQ_OP_FLUSH) {
232c6131fa5SGeert Uytterhoeven read = 0;
233c6131fa5SGeert Uytterhoeven op = "flush";
234c6131fa5SGeert Uytterhoeven } else {
235c6131fa5SGeert Uytterhoeven read = !rq_data_dir(req);
236c6131fa5SGeert Uytterhoeven op = read ? "read" : "write";
237c6131fa5SGeert Uytterhoeven }
238c6131fa5SGeert Uytterhoeven if (status) {
239e377c6e2SStephen Rothwell dev_dbg(&dev->sbd.core, "%s:%u: %s failed 0x%llx\n", __func__,
240c6131fa5SGeert Uytterhoeven __LINE__, op, status);
2412a842acaSChristoph Hellwig error = BLK_STS_IOERR;
242c6131fa5SGeert Uytterhoeven } else {
243c6131fa5SGeert Uytterhoeven dev_dbg(&dev->sbd.core, "%s:%u: %s completed\n", __func__,
244c6131fa5SGeert Uytterhoeven __LINE__, op);
245f01ab252SKiyoshi Ueda error = 0;
246c6131fa5SGeert Uytterhoeven if (read)
247c6131fa5SGeert Uytterhoeven ps3disk_scatter_gather(dev, req, 0);
248c6131fa5SGeert Uytterhoeven }
249c6131fa5SGeert Uytterhoeven
250c6131fa5SGeert Uytterhoeven spin_lock(&priv->lock);
251c6131fa5SGeert Uytterhoeven priv->req = NULL;
252fab1adcfSJens Axboe blk_mq_end_request(req, error);
253c6131fa5SGeert Uytterhoeven spin_unlock(&priv->lock);
254c6131fa5SGeert Uytterhoeven
25589662ac5SChristoph Hellwig blk_mq_run_hw_queues(priv->gendisk->queue, true);
256c6131fa5SGeert Uytterhoeven return IRQ_HANDLED;
257c6131fa5SGeert Uytterhoeven }
258c6131fa5SGeert Uytterhoeven
ps3disk_sync_cache(struct ps3_storage_device * dev)259c6131fa5SGeert Uytterhoeven static int ps3disk_sync_cache(struct ps3_storage_device *dev)
260c6131fa5SGeert Uytterhoeven {
261c6131fa5SGeert Uytterhoeven u64 res;
262c6131fa5SGeert Uytterhoeven
263c6131fa5SGeert Uytterhoeven dev_dbg(&dev->sbd.core, "%s:%u: sync cache\n", __func__, __LINE__);
264c6131fa5SGeert Uytterhoeven
265c6131fa5SGeert Uytterhoeven res = ps3stor_send_command(dev, LV1_STORAGE_ATA_HDDOUT, 0, 0, 0, 0);
266c6131fa5SGeert Uytterhoeven if (res) {
267e377c6e2SStephen Rothwell dev_err(&dev->sbd.core, "%s:%u: sync cache failed 0x%llx\n",
268c6131fa5SGeert Uytterhoeven __func__, __LINE__, res);
269c6131fa5SGeert Uytterhoeven return -EIO;
270c6131fa5SGeert Uytterhoeven }
271c6131fa5SGeert Uytterhoeven return 0;
272c6131fa5SGeert Uytterhoeven }
273c6131fa5SGeert Uytterhoeven
274c6131fa5SGeert Uytterhoeven
275c6131fa5SGeert Uytterhoeven /* ATA helpers copied from drivers/ata/libata-core.c */
276c6131fa5SGeert Uytterhoeven
swap_buf_le16(u16 * buf,unsigned int buf_words)277c6131fa5SGeert Uytterhoeven static void swap_buf_le16(u16 *buf, unsigned int buf_words)
278c6131fa5SGeert Uytterhoeven {
279c6131fa5SGeert Uytterhoeven #ifdef __BIG_ENDIAN
280c6131fa5SGeert Uytterhoeven unsigned int i;
281c6131fa5SGeert Uytterhoeven
282c6131fa5SGeert Uytterhoeven for (i = 0; i < buf_words; i++)
283c6131fa5SGeert Uytterhoeven buf[i] = le16_to_cpu(buf[i]);
284c6131fa5SGeert Uytterhoeven #endif /* __BIG_ENDIAN */
285c6131fa5SGeert Uytterhoeven }
286c6131fa5SGeert Uytterhoeven
ata_id_n_sectors(const u16 * id)287c6131fa5SGeert Uytterhoeven static u64 ata_id_n_sectors(const u16 *id)
288c6131fa5SGeert Uytterhoeven {
289c6131fa5SGeert Uytterhoeven if (ata_id_has_lba(id)) {
290c6131fa5SGeert Uytterhoeven if (ata_id_has_lba48(id))
291c6131fa5SGeert Uytterhoeven return ata_id_u64(id, 100);
292c6131fa5SGeert Uytterhoeven else
293c6131fa5SGeert Uytterhoeven return ata_id_u32(id, 60);
294c6131fa5SGeert Uytterhoeven } else {
295c6131fa5SGeert Uytterhoeven if (ata_id_current_chs_valid(id))
296c6131fa5SGeert Uytterhoeven return ata_id_u32(id, 57);
297c6131fa5SGeert Uytterhoeven else
298c6131fa5SGeert Uytterhoeven return id[1] * id[3] * id[6];
299c6131fa5SGeert Uytterhoeven }
300c6131fa5SGeert Uytterhoeven }
301c6131fa5SGeert Uytterhoeven
ata_id_string(const u16 * id,unsigned char * s,unsigned int ofs,unsigned int len)302c6131fa5SGeert Uytterhoeven static void ata_id_string(const u16 *id, unsigned char *s, unsigned int ofs,
303c6131fa5SGeert Uytterhoeven unsigned int len)
304c6131fa5SGeert Uytterhoeven {
305c6131fa5SGeert Uytterhoeven unsigned int c;
306c6131fa5SGeert Uytterhoeven
307c6131fa5SGeert Uytterhoeven while (len > 0) {
308c6131fa5SGeert Uytterhoeven c = id[ofs] >> 8;
309c6131fa5SGeert Uytterhoeven *s = c;
310c6131fa5SGeert Uytterhoeven s++;
311c6131fa5SGeert Uytterhoeven
312c6131fa5SGeert Uytterhoeven c = id[ofs] & 0xff;
313c6131fa5SGeert Uytterhoeven *s = c;
314c6131fa5SGeert Uytterhoeven s++;
315c6131fa5SGeert Uytterhoeven
316c6131fa5SGeert Uytterhoeven ofs++;
317c6131fa5SGeert Uytterhoeven len -= 2;
318c6131fa5SGeert Uytterhoeven }
319c6131fa5SGeert Uytterhoeven }
320c6131fa5SGeert Uytterhoeven
ata_id_c_string(const u16 * id,unsigned char * s,unsigned int ofs,unsigned int len)321c6131fa5SGeert Uytterhoeven static void ata_id_c_string(const u16 *id, unsigned char *s, unsigned int ofs,
322c6131fa5SGeert Uytterhoeven unsigned int len)
323c6131fa5SGeert Uytterhoeven {
324c6131fa5SGeert Uytterhoeven unsigned char *p;
325c6131fa5SGeert Uytterhoeven
326c6131fa5SGeert Uytterhoeven WARN_ON(!(len & 1));
327c6131fa5SGeert Uytterhoeven
328c6131fa5SGeert Uytterhoeven ata_id_string(id, s, ofs, len - 1);
329c6131fa5SGeert Uytterhoeven
330c6131fa5SGeert Uytterhoeven p = s + strnlen(s, len - 1);
331c6131fa5SGeert Uytterhoeven while (p > s && p[-1] == ' ')
332c6131fa5SGeert Uytterhoeven p--;
333c6131fa5SGeert Uytterhoeven *p = '\0';
334c6131fa5SGeert Uytterhoeven }
335c6131fa5SGeert Uytterhoeven
ps3disk_identify(struct ps3_storage_device * dev)336c6131fa5SGeert Uytterhoeven static int ps3disk_identify(struct ps3_storage_device *dev)
337c6131fa5SGeert Uytterhoeven {
33803fa68c2SGeert Uytterhoeven struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
339c6131fa5SGeert Uytterhoeven struct lv1_ata_cmnd_block ata_cmnd;
340c6131fa5SGeert Uytterhoeven u16 *id = dev->bounce_buf;
341c6131fa5SGeert Uytterhoeven u64 res;
342c6131fa5SGeert Uytterhoeven
343c6131fa5SGeert Uytterhoeven dev_dbg(&dev->sbd.core, "%s:%u: identify disk\n", __func__, __LINE__);
344c6131fa5SGeert Uytterhoeven
345c6131fa5SGeert Uytterhoeven memset(&ata_cmnd, 0, sizeof(struct lv1_ata_cmnd_block));
346c6131fa5SGeert Uytterhoeven ata_cmnd.command = ATA_CMD_ID_ATA;
347c6131fa5SGeert Uytterhoeven ata_cmnd.sector_count = 1;
348c6131fa5SGeert Uytterhoeven ata_cmnd.size = ata_cmnd.arglen = ATA_ID_WORDS * 2;
349c6131fa5SGeert Uytterhoeven ata_cmnd.buffer = dev->bounce_lpar;
350c6131fa5SGeert Uytterhoeven ata_cmnd.proto = PIO_DATA_IN_PROTO;
351c6131fa5SGeert Uytterhoeven ata_cmnd.in_out = DIR_READ;
352c6131fa5SGeert Uytterhoeven
353c6131fa5SGeert Uytterhoeven res = ps3stor_send_command(dev, LV1_STORAGE_SEND_ATA_COMMAND,
354c6131fa5SGeert Uytterhoeven ps3_mm_phys_to_lpar(__pa(&ata_cmnd)),
355c6131fa5SGeert Uytterhoeven sizeof(ata_cmnd), ata_cmnd.buffer,
356c6131fa5SGeert Uytterhoeven ata_cmnd.arglen);
357c6131fa5SGeert Uytterhoeven if (res) {
358e377c6e2SStephen Rothwell dev_err(&dev->sbd.core, "%s:%u: identify disk failed 0x%llx\n",
359c6131fa5SGeert Uytterhoeven __func__, __LINE__, res);
360c6131fa5SGeert Uytterhoeven return -EIO;
361c6131fa5SGeert Uytterhoeven }
362c6131fa5SGeert Uytterhoeven
363c6131fa5SGeert Uytterhoeven swap_buf_le16(id, ATA_ID_WORDS);
364c6131fa5SGeert Uytterhoeven
365c6131fa5SGeert Uytterhoeven /* All we're interested in are raw capacity and model name */
366c6131fa5SGeert Uytterhoeven priv->raw_capacity = ata_id_n_sectors(id);
367c6131fa5SGeert Uytterhoeven ata_id_c_string(id, priv->model, ATA_ID_PROD, sizeof(priv->model));
368c6131fa5SGeert Uytterhoeven return 0;
369c6131fa5SGeert Uytterhoeven }
370c6131fa5SGeert Uytterhoeven
371c6131fa5SGeert Uytterhoeven static unsigned long ps3disk_mask;
372c6131fa5SGeert Uytterhoeven
373c6131fa5SGeert Uytterhoeven static DEFINE_MUTEX(ps3disk_mask_mutex);
374c6131fa5SGeert Uytterhoeven
375fab1adcfSJens Axboe static const struct blk_mq_ops ps3disk_mq_ops = {
376fab1adcfSJens Axboe .queue_rq = ps3disk_queue_rq,
377fab1adcfSJens Axboe };
378fab1adcfSJens Axboe
ps3disk_probe(struct ps3_system_bus_device * _dev)3798d85fce7SGreg Kroah-Hartman static int ps3disk_probe(struct ps3_system_bus_device *_dev)
380c6131fa5SGeert Uytterhoeven {
381c6131fa5SGeert Uytterhoeven struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
382c6131fa5SGeert Uytterhoeven struct ps3disk_private *priv;
383c6131fa5SGeert Uytterhoeven int error;
384c6131fa5SGeert Uytterhoeven unsigned int devidx;
385c6131fa5SGeert Uytterhoeven struct request_queue *queue;
386c6131fa5SGeert Uytterhoeven struct gendisk *gendisk;
387c6131fa5SGeert Uytterhoeven
388c6131fa5SGeert Uytterhoeven if (dev->blk_size < 512) {
389c6131fa5SGeert Uytterhoeven dev_err(&dev->sbd.core,
390e377c6e2SStephen Rothwell "%s:%u: cannot handle block size %llu\n", __func__,
391c6131fa5SGeert Uytterhoeven __LINE__, dev->blk_size);
392c6131fa5SGeert Uytterhoeven return -EINVAL;
393c6131fa5SGeert Uytterhoeven }
394c6131fa5SGeert Uytterhoeven
395c6131fa5SGeert Uytterhoeven BUILD_BUG_ON(PS3DISK_MAX_DISKS > BITS_PER_LONG);
396c6131fa5SGeert Uytterhoeven mutex_lock(&ps3disk_mask_mutex);
397c6131fa5SGeert Uytterhoeven devidx = find_first_zero_bit(&ps3disk_mask, PS3DISK_MAX_DISKS);
398c6131fa5SGeert Uytterhoeven if (devidx >= PS3DISK_MAX_DISKS) {
399c6131fa5SGeert Uytterhoeven dev_err(&dev->sbd.core, "%s:%u: Too many disks\n", __func__,
400c6131fa5SGeert Uytterhoeven __LINE__);
401c6131fa5SGeert Uytterhoeven mutex_unlock(&ps3disk_mask_mutex);
402c6131fa5SGeert Uytterhoeven return -ENOSPC;
403c6131fa5SGeert Uytterhoeven }
404c6131fa5SGeert Uytterhoeven __set_bit(devidx, &ps3disk_mask);
405c6131fa5SGeert Uytterhoeven mutex_unlock(&ps3disk_mask_mutex);
406c6131fa5SGeert Uytterhoeven
407c6131fa5SGeert Uytterhoeven priv = kzalloc(sizeof(*priv), GFP_KERNEL);
408c6131fa5SGeert Uytterhoeven if (!priv) {
409c6131fa5SGeert Uytterhoeven error = -ENOMEM;
410c6131fa5SGeert Uytterhoeven goto fail;
411c6131fa5SGeert Uytterhoeven }
412c6131fa5SGeert Uytterhoeven
41303fa68c2SGeert Uytterhoeven ps3_system_bus_set_drvdata(_dev, priv);
414c6131fa5SGeert Uytterhoeven spin_lock_init(&priv->lock);
415c6131fa5SGeert Uytterhoeven
416c6131fa5SGeert Uytterhoeven dev->bounce_size = BOUNCE_SIZE;
417c6131fa5SGeert Uytterhoeven dev->bounce_buf = kmalloc(BOUNCE_SIZE, GFP_DMA);
418c6131fa5SGeert Uytterhoeven if (!dev->bounce_buf) {
419c6131fa5SGeert Uytterhoeven error = -ENOMEM;
420c6131fa5SGeert Uytterhoeven goto fail_free_priv;
421c6131fa5SGeert Uytterhoeven }
422c6131fa5SGeert Uytterhoeven
423c6131fa5SGeert Uytterhoeven error = ps3stor_setup(dev, ps3disk_interrupt);
424c6131fa5SGeert Uytterhoeven if (error)
425c6131fa5SGeert Uytterhoeven goto fail_free_bounce;
426c6131fa5SGeert Uytterhoeven
427c6131fa5SGeert Uytterhoeven ps3disk_identify(dev);
428c6131fa5SGeert Uytterhoeven
42989662ac5SChristoph Hellwig error = blk_mq_alloc_sq_tag_set(&priv->tag_set, &ps3disk_mq_ops, 1,
430fab1adcfSJens Axboe BLK_MQ_F_SHOULD_MERGE);
43189662ac5SChristoph Hellwig if (error)
432c6131fa5SGeert Uytterhoeven goto fail_teardown;
43389662ac5SChristoph Hellwig
43489662ac5SChristoph Hellwig gendisk = blk_mq_alloc_disk(&priv->tag_set, dev);
43589662ac5SChristoph Hellwig if (IS_ERR(gendisk)) {
43689662ac5SChristoph Hellwig dev_err(&dev->sbd.core, "%s:%u: blk_mq_alloc_disk failed\n",
43789662ac5SChristoph Hellwig __func__, __LINE__);
43889662ac5SChristoph Hellwig error = PTR_ERR(gendisk);
43989662ac5SChristoph Hellwig goto fail_free_tag_set;
440c6131fa5SGeert Uytterhoeven }
441c6131fa5SGeert Uytterhoeven
44289662ac5SChristoph Hellwig queue = gendisk->queue;
443c6131fa5SGeert Uytterhoeven
444086fa5ffSMartin K. Petersen blk_queue_max_hw_sectors(queue, dev->bounce_size >> 9);
445c6131fa5SGeert Uytterhoeven blk_queue_dma_alignment(queue, dev->blk_size-1);
446e1defc4fSMartin K. Petersen blk_queue_logical_block_size(queue, dev->blk_size);
447c6131fa5SGeert Uytterhoeven
44812c95f13SJens Axboe blk_queue_write_cache(queue, true, false);
449c6131fa5SGeert Uytterhoeven
4508a78362cSMartin K. Petersen blk_queue_max_segments(queue, -1);
451c6131fa5SGeert Uytterhoeven blk_queue_max_segment_size(queue, dev->bounce_size);
452c6131fa5SGeert Uytterhoeven
453c6131fa5SGeert Uytterhoeven priv->gendisk = gendisk;
454c6131fa5SGeert Uytterhoeven gendisk->major = ps3disk_major;
455c6131fa5SGeert Uytterhoeven gendisk->first_minor = devidx * PS3DISK_MINORS;
45689662ac5SChristoph Hellwig gendisk->minors = PS3DISK_MINORS;
457c6131fa5SGeert Uytterhoeven gendisk->fops = &ps3disk_fops;
458c6131fa5SGeert Uytterhoeven gendisk->private_data = dev;
459c6131fa5SGeert Uytterhoeven snprintf(gendisk->disk_name, sizeof(gendisk->disk_name), PS3DISK_NAME,
460c6131fa5SGeert Uytterhoeven devidx+'a');
461c6131fa5SGeert Uytterhoeven priv->blocking_factor = dev->blk_size >> 9;
462c6131fa5SGeert Uytterhoeven set_capacity(gendisk,
463c6131fa5SGeert Uytterhoeven dev->regions[dev->region_idx].size*priv->blocking_factor);
464c6131fa5SGeert Uytterhoeven
465c6131fa5SGeert Uytterhoeven dev_info(&dev->sbd.core,
46672deb455SChristoph Hellwig "%s is a %s (%llu MiB total, %llu MiB for OtherOS)\n",
467c6131fa5SGeert Uytterhoeven gendisk->disk_name, priv->model, priv->raw_capacity >> 11,
468c6131fa5SGeert Uytterhoeven get_capacity(gendisk) >> 11);
469c6131fa5SGeert Uytterhoeven
470ff4cbe0fSLuis Chamberlain error = device_add_disk(&dev->sbd.core, gendisk, NULL);
471ff4cbe0fSLuis Chamberlain if (error)
472ff4cbe0fSLuis Chamberlain goto fail_cleanup_disk;
473c6131fa5SGeert Uytterhoeven
474ff4cbe0fSLuis Chamberlain return 0;
475ff4cbe0fSLuis Chamberlain fail_cleanup_disk:
476*8b9ab626SChristoph Hellwig put_disk(gendisk);
47789662ac5SChristoph Hellwig fail_free_tag_set:
478fab1adcfSJens Axboe blk_mq_free_tag_set(&priv->tag_set);
479c6131fa5SGeert Uytterhoeven fail_teardown:
480c6131fa5SGeert Uytterhoeven ps3stor_teardown(dev);
481c6131fa5SGeert Uytterhoeven fail_free_bounce:
482c6131fa5SGeert Uytterhoeven kfree(dev->bounce_buf);
483c6131fa5SGeert Uytterhoeven fail_free_priv:
484c6131fa5SGeert Uytterhoeven kfree(priv);
48503fa68c2SGeert Uytterhoeven ps3_system_bus_set_drvdata(_dev, NULL);
486c6131fa5SGeert Uytterhoeven fail:
487c6131fa5SGeert Uytterhoeven mutex_lock(&ps3disk_mask_mutex);
488c6131fa5SGeert Uytterhoeven __clear_bit(devidx, &ps3disk_mask);
489c6131fa5SGeert Uytterhoeven mutex_unlock(&ps3disk_mask_mutex);
490c6131fa5SGeert Uytterhoeven return error;
491c6131fa5SGeert Uytterhoeven }
492c6131fa5SGeert Uytterhoeven
ps3disk_remove(struct ps3_system_bus_device * _dev)4936d247e4dSUwe Kleine-König static void ps3disk_remove(struct ps3_system_bus_device *_dev)
494c6131fa5SGeert Uytterhoeven {
495c6131fa5SGeert Uytterhoeven struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
49603fa68c2SGeert Uytterhoeven struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
497c6131fa5SGeert Uytterhoeven
498c6131fa5SGeert Uytterhoeven mutex_lock(&ps3disk_mask_mutex);
499f331c029STejun Heo __clear_bit(MINOR(disk_devt(priv->gendisk)) / PS3DISK_MINORS,
500c6131fa5SGeert Uytterhoeven &ps3disk_mask);
501c6131fa5SGeert Uytterhoeven mutex_unlock(&ps3disk_mask_mutex);
502c6131fa5SGeert Uytterhoeven del_gendisk(priv->gendisk);
503*8b9ab626SChristoph Hellwig put_disk(priv->gendisk);
504fab1adcfSJens Axboe blk_mq_free_tag_set(&priv->tag_set);
505c6131fa5SGeert Uytterhoeven dev_notice(&dev->sbd.core, "Synchronizing disk cache\n");
506c6131fa5SGeert Uytterhoeven ps3disk_sync_cache(dev);
507c6131fa5SGeert Uytterhoeven ps3stor_teardown(dev);
508c6131fa5SGeert Uytterhoeven kfree(dev->bounce_buf);
509c6131fa5SGeert Uytterhoeven kfree(priv);
51003fa68c2SGeert Uytterhoeven ps3_system_bus_set_drvdata(_dev, NULL);
511c6131fa5SGeert Uytterhoeven }
512c6131fa5SGeert Uytterhoeven
513c6131fa5SGeert Uytterhoeven static struct ps3_system_bus_driver ps3disk = {
514c6131fa5SGeert Uytterhoeven .match_id = PS3_MATCH_ID_STOR_DISK,
515c6131fa5SGeert Uytterhoeven .core.name = DEVICE_NAME,
516c6131fa5SGeert Uytterhoeven .core.owner = THIS_MODULE,
517c6131fa5SGeert Uytterhoeven .probe = ps3disk_probe,
518c6131fa5SGeert Uytterhoeven .remove = ps3disk_remove,
519c6131fa5SGeert Uytterhoeven .shutdown = ps3disk_remove,
520c6131fa5SGeert Uytterhoeven };
521c6131fa5SGeert Uytterhoeven
522c6131fa5SGeert Uytterhoeven
ps3disk_init(void)523c6131fa5SGeert Uytterhoeven static int __init ps3disk_init(void)
524c6131fa5SGeert Uytterhoeven {
525c6131fa5SGeert Uytterhoeven int error;
526c6131fa5SGeert Uytterhoeven
527c6131fa5SGeert Uytterhoeven if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
528c6131fa5SGeert Uytterhoeven return -ENODEV;
529c6131fa5SGeert Uytterhoeven
530c6131fa5SGeert Uytterhoeven error = register_blkdev(0, DEVICE_NAME);
531c6131fa5SGeert Uytterhoeven if (error <= 0) {
532c6131fa5SGeert Uytterhoeven printk(KERN_ERR "%s:%u: register_blkdev failed %d\n", __func__,
533c6131fa5SGeert Uytterhoeven __LINE__, error);
534c6131fa5SGeert Uytterhoeven return error;
535c6131fa5SGeert Uytterhoeven }
536c6131fa5SGeert Uytterhoeven ps3disk_major = error;
537c6131fa5SGeert Uytterhoeven
538c6131fa5SGeert Uytterhoeven pr_info("%s:%u: registered block device major %d\n", __func__,
539c6131fa5SGeert Uytterhoeven __LINE__, ps3disk_major);
540c6131fa5SGeert Uytterhoeven
541c6131fa5SGeert Uytterhoeven error = ps3_system_bus_driver_register(&ps3disk);
542c6131fa5SGeert Uytterhoeven if (error)
543c6131fa5SGeert Uytterhoeven unregister_blkdev(ps3disk_major, DEVICE_NAME);
544c6131fa5SGeert Uytterhoeven
545c6131fa5SGeert Uytterhoeven return error;
546c6131fa5SGeert Uytterhoeven }
547c6131fa5SGeert Uytterhoeven
ps3disk_exit(void)548c6131fa5SGeert Uytterhoeven static void __exit ps3disk_exit(void)
549c6131fa5SGeert Uytterhoeven {
550c6131fa5SGeert Uytterhoeven ps3_system_bus_driver_unregister(&ps3disk);
551c6131fa5SGeert Uytterhoeven unregister_blkdev(ps3disk_major, DEVICE_NAME);
552c6131fa5SGeert Uytterhoeven }
553c6131fa5SGeert Uytterhoeven
554c6131fa5SGeert Uytterhoeven module_init(ps3disk_init);
555c6131fa5SGeert Uytterhoeven module_exit(ps3disk_exit);
556c6131fa5SGeert Uytterhoeven
557c6131fa5SGeert Uytterhoeven MODULE_LICENSE("GPL");
558c6131fa5SGeert Uytterhoeven MODULE_DESCRIPTION("PS3 Disk Storage Driver");
559c6131fa5SGeert Uytterhoeven MODULE_AUTHOR("Sony Corporation");
560c6131fa5SGeert Uytterhoeven MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_DISK);
561