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; 977988613bSKent 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; 104458b76edSKent Overstreet dev_dbg(&dev->sbd.core, "%s:%u: bio %u: %u sectors from %lu\n", 105458b76edSKent Overstreet __func__, __LINE__, i, bio_sectors(iter.bio), 106458b76edSKent Overstreet iter.bio->bi_iter.bi_sector); 1075705f702SNeilBrown 1087988613bSKent Overstreet size = bvec.bv_len; 1097988613bSKent Overstreet buf = bvec_kmap_irq(&bvec, &flags); 110c6131fa5SGeert Uytterhoeven if (gather) 111c6131fa5SGeert Uytterhoeven memcpy(dev->bounce_buf+offset, buf, size); 112c6131fa5SGeert Uytterhoeven else 113c6131fa5SGeert Uytterhoeven memcpy(buf, dev->bounce_buf+offset, size); 114c6131fa5SGeert Uytterhoeven offset += size; 1157988613bSKent Overstreet flush_kernel_dcache_page(bvec.bv_page); 11693055c31SDan Carpenter bvec_kunmap_irq(buf, &flags); 117c6131fa5SGeert Uytterhoeven i++; 118c6131fa5SGeert Uytterhoeven } 119c6131fa5SGeert Uytterhoeven } 120c6131fa5SGeert Uytterhoeven 121c6131fa5SGeert Uytterhoeven static int ps3disk_submit_request_sg(struct ps3_storage_device *dev, 122c6131fa5SGeert Uytterhoeven struct request *req) 123c6131fa5SGeert Uytterhoeven { 12403fa68c2SGeert Uytterhoeven struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd); 125c6131fa5SGeert Uytterhoeven int write = rq_data_dir(req), res; 126c6131fa5SGeert Uytterhoeven const char *op = write ? "write" : "read"; 127c6131fa5SGeert Uytterhoeven u64 start_sector, sectors; 128c6131fa5SGeert Uytterhoeven unsigned int region_id = dev->regions[dev->region_idx].id; 129c6131fa5SGeert Uytterhoeven 130c6131fa5SGeert Uytterhoeven #ifdef DEBUG 131c6131fa5SGeert Uytterhoeven unsigned int n = 0; 1327988613bSKent Overstreet struct bio_vec bv; 1335705f702SNeilBrown struct req_iterator iter; 134c6131fa5SGeert Uytterhoeven 1355705f702SNeilBrown rq_for_each_segment(bv, req, iter) 136c6131fa5SGeert Uytterhoeven n++; 137c6131fa5SGeert Uytterhoeven dev_dbg(&dev->sbd.core, 13883096ebfSTejun Heo "%s:%u: %s req has %u bvecs for %u sectors\n", 13983096ebfSTejun Heo __func__, __LINE__, op, n, blk_rq_sectors(req)); 140c6131fa5SGeert Uytterhoeven #endif 141c6131fa5SGeert Uytterhoeven 14283096ebfSTejun Heo start_sector = blk_rq_pos(req) * priv->blocking_factor; 14383096ebfSTejun Heo sectors = blk_rq_sectors(req) * priv->blocking_factor; 144e377c6e2SStephen 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); 16110e1e629STejun Heo __blk_end_request_all(req, -EIO); 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 { 17203fa68c2SGeert Uytterhoeven struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd); 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) { 181e377c6e2SStephen Rothwell dev_err(&dev->sbd.core, "%s:%u: sync cache failed 0x%llx\n", 182c6131fa5SGeert Uytterhoeven __func__, __LINE__, res); 18310e1e629STejun Heo __blk_end_request_all(req, -EIO); 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 1989934c8c0STejun Heo while ((req = blk_fetch_request(q))) { 19998d8c8f4SFUJITA Tomonori if (req->cmd_flags & REQ_FLUSH) { 200c6131fa5SGeert Uytterhoeven if (ps3disk_submit_flush_request(dev, req)) 201c6131fa5SGeert Uytterhoeven break; 20298d8c8f4SFUJITA Tomonori } else if (req->cmd_type == REQ_TYPE_FS) { 20398d8c8f4SFUJITA Tomonori if (ps3disk_submit_request_sg(dev, req)) 20498d8c8f4SFUJITA Tomonori break; 205c6131fa5SGeert Uytterhoeven } else { 206c6131fa5SGeert Uytterhoeven blk_dump_rq_flags(req, DEVICE_NAME " bad request"); 20710e1e629STejun Heo __blk_end_request_all(req, -EIO); 208c6131fa5SGeert Uytterhoeven continue; 209c6131fa5SGeert Uytterhoeven } 210c6131fa5SGeert Uytterhoeven } 211c6131fa5SGeert Uytterhoeven } 212c6131fa5SGeert Uytterhoeven 213165125e1SJens Axboe static void ps3disk_request(struct request_queue *q) 214c6131fa5SGeert Uytterhoeven { 215c6131fa5SGeert Uytterhoeven struct ps3_storage_device *dev = q->queuedata; 21603fa68c2SGeert Uytterhoeven struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd); 217c6131fa5SGeert Uytterhoeven 218c6131fa5SGeert Uytterhoeven if (priv->req) { 219c6131fa5SGeert Uytterhoeven dev_dbg(&dev->sbd.core, "%s:%u busy\n", __func__, __LINE__); 220c6131fa5SGeert Uytterhoeven return; 221c6131fa5SGeert Uytterhoeven } 222c6131fa5SGeert Uytterhoeven 223c6131fa5SGeert Uytterhoeven ps3disk_do_request(dev, q); 224c6131fa5SGeert Uytterhoeven } 225c6131fa5SGeert Uytterhoeven 226c6131fa5SGeert Uytterhoeven static irqreturn_t ps3disk_interrupt(int irq, void *data) 227c6131fa5SGeert Uytterhoeven { 228c6131fa5SGeert Uytterhoeven struct ps3_storage_device *dev = data; 229c6131fa5SGeert Uytterhoeven struct ps3disk_private *priv; 230c6131fa5SGeert Uytterhoeven struct request *req; 231f01ab252SKiyoshi Ueda int res, read, error; 232c6131fa5SGeert Uytterhoeven u64 tag, status; 233c6131fa5SGeert Uytterhoeven const char *op; 234c6131fa5SGeert Uytterhoeven 235c6131fa5SGeert Uytterhoeven res = lv1_storage_get_async_status(dev->sbd.dev_id, &tag, &status); 236c6131fa5SGeert Uytterhoeven 237c6131fa5SGeert Uytterhoeven if (tag != dev->tag) 238c6131fa5SGeert Uytterhoeven dev_err(&dev->sbd.core, 239e377c6e2SStephen Rothwell "%s:%u: tag mismatch, got %llx, expected %llx\n", 240c6131fa5SGeert Uytterhoeven __func__, __LINE__, tag, dev->tag); 241c6131fa5SGeert Uytterhoeven 242c6131fa5SGeert Uytterhoeven if (res) { 243e377c6e2SStephen Rothwell dev_err(&dev->sbd.core, "%s:%u: res=%d status=0x%llx\n", 244c6131fa5SGeert Uytterhoeven __func__, __LINE__, res, status); 245c6131fa5SGeert Uytterhoeven return IRQ_HANDLED; 246c6131fa5SGeert Uytterhoeven } 247c6131fa5SGeert Uytterhoeven 24803fa68c2SGeert Uytterhoeven priv = ps3_system_bus_get_drvdata(&dev->sbd); 249c6131fa5SGeert Uytterhoeven req = priv->req; 250c6131fa5SGeert Uytterhoeven if (!req) { 251c6131fa5SGeert Uytterhoeven dev_dbg(&dev->sbd.core, 252c6131fa5SGeert Uytterhoeven "%s:%u non-block layer request completed\n", __func__, 253c6131fa5SGeert Uytterhoeven __LINE__); 254c6131fa5SGeert Uytterhoeven dev->lv1_status = status; 255c6131fa5SGeert Uytterhoeven complete(&dev->done); 256c6131fa5SGeert Uytterhoeven return IRQ_HANDLED; 257c6131fa5SGeert Uytterhoeven } 258c6131fa5SGeert Uytterhoeven 25998d8c8f4SFUJITA Tomonori if (req->cmd_flags & REQ_FLUSH) { 260c6131fa5SGeert Uytterhoeven read = 0; 261c6131fa5SGeert Uytterhoeven op = "flush"; 262c6131fa5SGeert Uytterhoeven } else { 263c6131fa5SGeert Uytterhoeven read = !rq_data_dir(req); 264c6131fa5SGeert Uytterhoeven op = read ? "read" : "write"; 265c6131fa5SGeert Uytterhoeven } 266c6131fa5SGeert Uytterhoeven if (status) { 267e377c6e2SStephen Rothwell dev_dbg(&dev->sbd.core, "%s:%u: %s failed 0x%llx\n", __func__, 268c6131fa5SGeert Uytterhoeven __LINE__, op, status); 269f01ab252SKiyoshi Ueda error = -EIO; 270c6131fa5SGeert Uytterhoeven } else { 271c6131fa5SGeert Uytterhoeven dev_dbg(&dev->sbd.core, "%s:%u: %s completed\n", __func__, 272c6131fa5SGeert Uytterhoeven __LINE__, op); 273f01ab252SKiyoshi Ueda error = 0; 274c6131fa5SGeert Uytterhoeven if (read) 275c6131fa5SGeert Uytterhoeven ps3disk_scatter_gather(dev, req, 0); 276c6131fa5SGeert Uytterhoeven } 277c6131fa5SGeert Uytterhoeven 278c6131fa5SGeert Uytterhoeven spin_lock(&priv->lock); 279cd4c34ebSTejun Heo __blk_end_request_all(req, error); 280c6131fa5SGeert Uytterhoeven priv->req = NULL; 281c6131fa5SGeert Uytterhoeven ps3disk_do_request(dev, priv->queue); 282c6131fa5SGeert Uytterhoeven spin_unlock(&priv->lock); 283c6131fa5SGeert Uytterhoeven 284c6131fa5SGeert Uytterhoeven return IRQ_HANDLED; 285c6131fa5SGeert Uytterhoeven } 286c6131fa5SGeert Uytterhoeven 287c6131fa5SGeert Uytterhoeven static int ps3disk_sync_cache(struct ps3_storage_device *dev) 288c6131fa5SGeert Uytterhoeven { 289c6131fa5SGeert Uytterhoeven u64 res; 290c6131fa5SGeert Uytterhoeven 291c6131fa5SGeert Uytterhoeven dev_dbg(&dev->sbd.core, "%s:%u: sync cache\n", __func__, __LINE__); 292c6131fa5SGeert Uytterhoeven 293c6131fa5SGeert Uytterhoeven res = ps3stor_send_command(dev, LV1_STORAGE_ATA_HDDOUT, 0, 0, 0, 0); 294c6131fa5SGeert Uytterhoeven if (res) { 295e377c6e2SStephen Rothwell dev_err(&dev->sbd.core, "%s:%u: sync cache failed 0x%llx\n", 296c6131fa5SGeert Uytterhoeven __func__, __LINE__, res); 297c6131fa5SGeert Uytterhoeven return -EIO; 298c6131fa5SGeert Uytterhoeven } 299c6131fa5SGeert Uytterhoeven return 0; 300c6131fa5SGeert Uytterhoeven } 301c6131fa5SGeert Uytterhoeven 302c6131fa5SGeert Uytterhoeven 303c6131fa5SGeert Uytterhoeven /* ATA helpers copied from drivers/ata/libata-core.c */ 304c6131fa5SGeert Uytterhoeven 305c6131fa5SGeert Uytterhoeven static void swap_buf_le16(u16 *buf, unsigned int buf_words) 306c6131fa5SGeert Uytterhoeven { 307c6131fa5SGeert Uytterhoeven #ifdef __BIG_ENDIAN 308c6131fa5SGeert Uytterhoeven unsigned int i; 309c6131fa5SGeert Uytterhoeven 310c6131fa5SGeert Uytterhoeven for (i = 0; i < buf_words; i++) 311c6131fa5SGeert Uytterhoeven buf[i] = le16_to_cpu(buf[i]); 312c6131fa5SGeert Uytterhoeven #endif /* __BIG_ENDIAN */ 313c6131fa5SGeert Uytterhoeven } 314c6131fa5SGeert Uytterhoeven 315c6131fa5SGeert Uytterhoeven static u64 ata_id_n_sectors(const u16 *id) 316c6131fa5SGeert Uytterhoeven { 317c6131fa5SGeert Uytterhoeven if (ata_id_has_lba(id)) { 318c6131fa5SGeert Uytterhoeven if (ata_id_has_lba48(id)) 319c6131fa5SGeert Uytterhoeven return ata_id_u64(id, 100); 320c6131fa5SGeert Uytterhoeven else 321c6131fa5SGeert Uytterhoeven return ata_id_u32(id, 60); 322c6131fa5SGeert Uytterhoeven } else { 323c6131fa5SGeert Uytterhoeven if (ata_id_current_chs_valid(id)) 324c6131fa5SGeert Uytterhoeven return ata_id_u32(id, 57); 325c6131fa5SGeert Uytterhoeven else 326c6131fa5SGeert Uytterhoeven return id[1] * id[3] * id[6]; 327c6131fa5SGeert Uytterhoeven } 328c6131fa5SGeert Uytterhoeven } 329c6131fa5SGeert Uytterhoeven 330c6131fa5SGeert Uytterhoeven static void ata_id_string(const u16 *id, unsigned char *s, unsigned int ofs, 331c6131fa5SGeert Uytterhoeven unsigned int len) 332c6131fa5SGeert Uytterhoeven { 333c6131fa5SGeert Uytterhoeven unsigned int c; 334c6131fa5SGeert Uytterhoeven 335c6131fa5SGeert Uytterhoeven while (len > 0) { 336c6131fa5SGeert Uytterhoeven c = id[ofs] >> 8; 337c6131fa5SGeert Uytterhoeven *s = c; 338c6131fa5SGeert Uytterhoeven s++; 339c6131fa5SGeert Uytterhoeven 340c6131fa5SGeert Uytterhoeven c = id[ofs] & 0xff; 341c6131fa5SGeert Uytterhoeven *s = c; 342c6131fa5SGeert Uytterhoeven s++; 343c6131fa5SGeert Uytterhoeven 344c6131fa5SGeert Uytterhoeven ofs++; 345c6131fa5SGeert Uytterhoeven len -= 2; 346c6131fa5SGeert Uytterhoeven } 347c6131fa5SGeert Uytterhoeven } 348c6131fa5SGeert Uytterhoeven 349c6131fa5SGeert Uytterhoeven static void ata_id_c_string(const u16 *id, unsigned char *s, unsigned int ofs, 350c6131fa5SGeert Uytterhoeven unsigned int len) 351c6131fa5SGeert Uytterhoeven { 352c6131fa5SGeert Uytterhoeven unsigned char *p; 353c6131fa5SGeert Uytterhoeven 354c6131fa5SGeert Uytterhoeven WARN_ON(!(len & 1)); 355c6131fa5SGeert Uytterhoeven 356c6131fa5SGeert Uytterhoeven ata_id_string(id, s, ofs, len - 1); 357c6131fa5SGeert Uytterhoeven 358c6131fa5SGeert Uytterhoeven p = s + strnlen(s, len - 1); 359c6131fa5SGeert Uytterhoeven while (p > s && p[-1] == ' ') 360c6131fa5SGeert Uytterhoeven p--; 361c6131fa5SGeert Uytterhoeven *p = '\0'; 362c6131fa5SGeert Uytterhoeven } 363c6131fa5SGeert Uytterhoeven 364c6131fa5SGeert Uytterhoeven static int ps3disk_identify(struct ps3_storage_device *dev) 365c6131fa5SGeert Uytterhoeven { 36603fa68c2SGeert Uytterhoeven struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd); 367c6131fa5SGeert Uytterhoeven struct lv1_ata_cmnd_block ata_cmnd; 368c6131fa5SGeert Uytterhoeven u16 *id = dev->bounce_buf; 369c6131fa5SGeert Uytterhoeven u64 res; 370c6131fa5SGeert Uytterhoeven 371c6131fa5SGeert Uytterhoeven dev_dbg(&dev->sbd.core, "%s:%u: identify disk\n", __func__, __LINE__); 372c6131fa5SGeert Uytterhoeven 373c6131fa5SGeert Uytterhoeven memset(&ata_cmnd, 0, sizeof(struct lv1_ata_cmnd_block)); 374c6131fa5SGeert Uytterhoeven ata_cmnd.command = ATA_CMD_ID_ATA; 375c6131fa5SGeert Uytterhoeven ata_cmnd.sector_count = 1; 376c6131fa5SGeert Uytterhoeven ata_cmnd.size = ata_cmnd.arglen = ATA_ID_WORDS * 2; 377c6131fa5SGeert Uytterhoeven ata_cmnd.buffer = dev->bounce_lpar; 378c6131fa5SGeert Uytterhoeven ata_cmnd.proto = PIO_DATA_IN_PROTO; 379c6131fa5SGeert Uytterhoeven ata_cmnd.in_out = DIR_READ; 380c6131fa5SGeert Uytterhoeven 381c6131fa5SGeert Uytterhoeven res = ps3stor_send_command(dev, LV1_STORAGE_SEND_ATA_COMMAND, 382c6131fa5SGeert Uytterhoeven ps3_mm_phys_to_lpar(__pa(&ata_cmnd)), 383c6131fa5SGeert Uytterhoeven sizeof(ata_cmnd), ata_cmnd.buffer, 384c6131fa5SGeert Uytterhoeven ata_cmnd.arglen); 385c6131fa5SGeert Uytterhoeven if (res) { 386e377c6e2SStephen Rothwell dev_err(&dev->sbd.core, "%s:%u: identify disk failed 0x%llx\n", 387c6131fa5SGeert Uytterhoeven __func__, __LINE__, res); 388c6131fa5SGeert Uytterhoeven return -EIO; 389c6131fa5SGeert Uytterhoeven } 390c6131fa5SGeert Uytterhoeven 391c6131fa5SGeert Uytterhoeven swap_buf_le16(id, ATA_ID_WORDS); 392c6131fa5SGeert Uytterhoeven 393c6131fa5SGeert Uytterhoeven /* All we're interested in are raw capacity and model name */ 394c6131fa5SGeert Uytterhoeven priv->raw_capacity = ata_id_n_sectors(id); 395c6131fa5SGeert Uytterhoeven ata_id_c_string(id, priv->model, ATA_ID_PROD, sizeof(priv->model)); 396c6131fa5SGeert Uytterhoeven return 0; 397c6131fa5SGeert Uytterhoeven } 398c6131fa5SGeert Uytterhoeven 399c6131fa5SGeert Uytterhoeven static unsigned long ps3disk_mask; 400c6131fa5SGeert Uytterhoeven 401c6131fa5SGeert Uytterhoeven static DEFINE_MUTEX(ps3disk_mask_mutex); 402c6131fa5SGeert Uytterhoeven 4038d85fce7SGreg Kroah-Hartman static int ps3disk_probe(struct ps3_system_bus_device *_dev) 404c6131fa5SGeert Uytterhoeven { 405c6131fa5SGeert Uytterhoeven struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core); 406c6131fa5SGeert Uytterhoeven struct ps3disk_private *priv; 407c6131fa5SGeert Uytterhoeven int error; 408c6131fa5SGeert Uytterhoeven unsigned int devidx; 409c6131fa5SGeert Uytterhoeven struct request_queue *queue; 410c6131fa5SGeert Uytterhoeven struct gendisk *gendisk; 411c6131fa5SGeert Uytterhoeven 412c6131fa5SGeert Uytterhoeven if (dev->blk_size < 512) { 413c6131fa5SGeert Uytterhoeven dev_err(&dev->sbd.core, 414e377c6e2SStephen Rothwell "%s:%u: cannot handle block size %llu\n", __func__, 415c6131fa5SGeert Uytterhoeven __LINE__, dev->blk_size); 416c6131fa5SGeert Uytterhoeven return -EINVAL; 417c6131fa5SGeert Uytterhoeven } 418c6131fa5SGeert Uytterhoeven 419c6131fa5SGeert Uytterhoeven BUILD_BUG_ON(PS3DISK_MAX_DISKS > BITS_PER_LONG); 420c6131fa5SGeert Uytterhoeven mutex_lock(&ps3disk_mask_mutex); 421c6131fa5SGeert Uytterhoeven devidx = find_first_zero_bit(&ps3disk_mask, PS3DISK_MAX_DISKS); 422c6131fa5SGeert Uytterhoeven if (devidx >= PS3DISK_MAX_DISKS) { 423c6131fa5SGeert Uytterhoeven dev_err(&dev->sbd.core, "%s:%u: Too many disks\n", __func__, 424c6131fa5SGeert Uytterhoeven __LINE__); 425c6131fa5SGeert Uytterhoeven mutex_unlock(&ps3disk_mask_mutex); 426c6131fa5SGeert Uytterhoeven return -ENOSPC; 427c6131fa5SGeert Uytterhoeven } 428c6131fa5SGeert Uytterhoeven __set_bit(devidx, &ps3disk_mask); 429c6131fa5SGeert Uytterhoeven mutex_unlock(&ps3disk_mask_mutex); 430c6131fa5SGeert Uytterhoeven 431c6131fa5SGeert Uytterhoeven priv = kzalloc(sizeof(*priv), GFP_KERNEL); 432c6131fa5SGeert Uytterhoeven if (!priv) { 433c6131fa5SGeert Uytterhoeven error = -ENOMEM; 434c6131fa5SGeert Uytterhoeven goto fail; 435c6131fa5SGeert Uytterhoeven } 436c6131fa5SGeert Uytterhoeven 43703fa68c2SGeert Uytterhoeven ps3_system_bus_set_drvdata(_dev, priv); 438c6131fa5SGeert Uytterhoeven spin_lock_init(&priv->lock); 439c6131fa5SGeert Uytterhoeven 440c6131fa5SGeert Uytterhoeven dev->bounce_size = BOUNCE_SIZE; 441c6131fa5SGeert Uytterhoeven dev->bounce_buf = kmalloc(BOUNCE_SIZE, GFP_DMA); 442c6131fa5SGeert Uytterhoeven if (!dev->bounce_buf) { 443c6131fa5SGeert Uytterhoeven error = -ENOMEM; 444c6131fa5SGeert Uytterhoeven goto fail_free_priv; 445c6131fa5SGeert Uytterhoeven } 446c6131fa5SGeert Uytterhoeven 447c6131fa5SGeert Uytterhoeven error = ps3stor_setup(dev, ps3disk_interrupt); 448c6131fa5SGeert Uytterhoeven if (error) 449c6131fa5SGeert Uytterhoeven goto fail_free_bounce; 450c6131fa5SGeert Uytterhoeven 451c6131fa5SGeert Uytterhoeven ps3disk_identify(dev); 452c6131fa5SGeert Uytterhoeven 453c6131fa5SGeert Uytterhoeven queue = blk_init_queue(ps3disk_request, &priv->lock); 454c6131fa5SGeert Uytterhoeven if (!queue) { 455c6131fa5SGeert Uytterhoeven dev_err(&dev->sbd.core, "%s:%u: blk_init_queue failed\n", 456c6131fa5SGeert Uytterhoeven __func__, __LINE__); 457c6131fa5SGeert Uytterhoeven error = -ENOMEM; 458c6131fa5SGeert Uytterhoeven goto fail_teardown; 459c6131fa5SGeert Uytterhoeven } 460c6131fa5SGeert Uytterhoeven 461c6131fa5SGeert Uytterhoeven priv->queue = queue; 462c6131fa5SGeert Uytterhoeven queue->queuedata = dev; 463c6131fa5SGeert Uytterhoeven 464c6131fa5SGeert Uytterhoeven blk_queue_bounce_limit(queue, BLK_BOUNCE_HIGH); 465c6131fa5SGeert Uytterhoeven 466086fa5ffSMartin K. Petersen blk_queue_max_hw_sectors(queue, dev->bounce_size >> 9); 467c6131fa5SGeert Uytterhoeven blk_queue_segment_boundary(queue, -1UL); 468c6131fa5SGeert Uytterhoeven blk_queue_dma_alignment(queue, dev->blk_size-1); 469e1defc4fSMartin K. Petersen blk_queue_logical_block_size(queue, dev->blk_size); 470c6131fa5SGeert Uytterhoeven 471*12c95f13SJens Axboe blk_queue_write_cache(queue, true, false); 472c6131fa5SGeert Uytterhoeven 4738a78362cSMartin K. Petersen blk_queue_max_segments(queue, -1); 474c6131fa5SGeert Uytterhoeven blk_queue_max_segment_size(queue, dev->bounce_size); 475c6131fa5SGeert Uytterhoeven 476c6131fa5SGeert Uytterhoeven gendisk = alloc_disk(PS3DISK_MINORS); 477c6131fa5SGeert Uytterhoeven if (!gendisk) { 478c6131fa5SGeert Uytterhoeven dev_err(&dev->sbd.core, "%s:%u: alloc_disk failed\n", __func__, 479c6131fa5SGeert Uytterhoeven __LINE__); 480c6131fa5SGeert Uytterhoeven error = -ENOMEM; 481c6131fa5SGeert Uytterhoeven goto fail_cleanup_queue; 482c6131fa5SGeert Uytterhoeven } 483c6131fa5SGeert Uytterhoeven 484c6131fa5SGeert Uytterhoeven priv->gendisk = gendisk; 485c6131fa5SGeert Uytterhoeven gendisk->major = ps3disk_major; 486c6131fa5SGeert Uytterhoeven gendisk->first_minor = devidx * PS3DISK_MINORS; 487c6131fa5SGeert Uytterhoeven gendisk->fops = &ps3disk_fops; 488c6131fa5SGeert Uytterhoeven gendisk->queue = queue; 489c6131fa5SGeert Uytterhoeven gendisk->private_data = dev; 490c6131fa5SGeert Uytterhoeven gendisk->driverfs_dev = &dev->sbd.core; 491c6131fa5SGeert Uytterhoeven snprintf(gendisk->disk_name, sizeof(gendisk->disk_name), PS3DISK_NAME, 492c6131fa5SGeert Uytterhoeven devidx+'a'); 493c6131fa5SGeert Uytterhoeven priv->blocking_factor = dev->blk_size >> 9; 494c6131fa5SGeert Uytterhoeven set_capacity(gendisk, 495c6131fa5SGeert Uytterhoeven dev->regions[dev->region_idx].size*priv->blocking_factor); 496c6131fa5SGeert Uytterhoeven 497c6131fa5SGeert Uytterhoeven dev_info(&dev->sbd.core, 498e377c6e2SStephen Rothwell "%s is a %s (%llu MiB total, %lu MiB for OtherOS)\n", 499c6131fa5SGeert Uytterhoeven gendisk->disk_name, priv->model, priv->raw_capacity >> 11, 500c6131fa5SGeert Uytterhoeven get_capacity(gendisk) >> 11); 501c6131fa5SGeert Uytterhoeven 502c6131fa5SGeert Uytterhoeven add_disk(gendisk); 503c6131fa5SGeert Uytterhoeven return 0; 504c6131fa5SGeert Uytterhoeven 505c6131fa5SGeert Uytterhoeven fail_cleanup_queue: 506c6131fa5SGeert Uytterhoeven blk_cleanup_queue(queue); 507c6131fa5SGeert Uytterhoeven fail_teardown: 508c6131fa5SGeert Uytterhoeven ps3stor_teardown(dev); 509c6131fa5SGeert Uytterhoeven fail_free_bounce: 510c6131fa5SGeert Uytterhoeven kfree(dev->bounce_buf); 511c6131fa5SGeert Uytterhoeven fail_free_priv: 512c6131fa5SGeert Uytterhoeven kfree(priv); 51303fa68c2SGeert Uytterhoeven ps3_system_bus_set_drvdata(_dev, NULL); 514c6131fa5SGeert Uytterhoeven fail: 515c6131fa5SGeert Uytterhoeven mutex_lock(&ps3disk_mask_mutex); 516c6131fa5SGeert Uytterhoeven __clear_bit(devidx, &ps3disk_mask); 517c6131fa5SGeert Uytterhoeven mutex_unlock(&ps3disk_mask_mutex); 518c6131fa5SGeert Uytterhoeven return error; 519c6131fa5SGeert Uytterhoeven } 520c6131fa5SGeert Uytterhoeven 521c6131fa5SGeert Uytterhoeven static int ps3disk_remove(struct ps3_system_bus_device *_dev) 522c6131fa5SGeert Uytterhoeven { 523c6131fa5SGeert Uytterhoeven struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core); 52403fa68c2SGeert Uytterhoeven struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd); 525c6131fa5SGeert Uytterhoeven 526c6131fa5SGeert Uytterhoeven mutex_lock(&ps3disk_mask_mutex); 527f331c029STejun Heo __clear_bit(MINOR(disk_devt(priv->gendisk)) / PS3DISK_MINORS, 528c6131fa5SGeert Uytterhoeven &ps3disk_mask); 529c6131fa5SGeert Uytterhoeven mutex_unlock(&ps3disk_mask_mutex); 530c6131fa5SGeert Uytterhoeven del_gendisk(priv->gendisk); 531c6131fa5SGeert Uytterhoeven blk_cleanup_queue(priv->queue); 532c6131fa5SGeert Uytterhoeven put_disk(priv->gendisk); 533c6131fa5SGeert Uytterhoeven dev_notice(&dev->sbd.core, "Synchronizing disk cache\n"); 534c6131fa5SGeert Uytterhoeven ps3disk_sync_cache(dev); 535c6131fa5SGeert Uytterhoeven ps3stor_teardown(dev); 536c6131fa5SGeert Uytterhoeven kfree(dev->bounce_buf); 537c6131fa5SGeert Uytterhoeven kfree(priv); 53803fa68c2SGeert Uytterhoeven ps3_system_bus_set_drvdata(_dev, NULL); 539c6131fa5SGeert Uytterhoeven return 0; 540c6131fa5SGeert Uytterhoeven } 541c6131fa5SGeert Uytterhoeven 542c6131fa5SGeert Uytterhoeven static struct ps3_system_bus_driver ps3disk = { 543c6131fa5SGeert Uytterhoeven .match_id = PS3_MATCH_ID_STOR_DISK, 544c6131fa5SGeert Uytterhoeven .core.name = DEVICE_NAME, 545c6131fa5SGeert Uytterhoeven .core.owner = THIS_MODULE, 546c6131fa5SGeert Uytterhoeven .probe = ps3disk_probe, 547c6131fa5SGeert Uytterhoeven .remove = ps3disk_remove, 548c6131fa5SGeert Uytterhoeven .shutdown = ps3disk_remove, 549c6131fa5SGeert Uytterhoeven }; 550c6131fa5SGeert Uytterhoeven 551c6131fa5SGeert Uytterhoeven 552c6131fa5SGeert Uytterhoeven static int __init ps3disk_init(void) 553c6131fa5SGeert Uytterhoeven { 554c6131fa5SGeert Uytterhoeven int error; 555c6131fa5SGeert Uytterhoeven 556c6131fa5SGeert Uytterhoeven if (!firmware_has_feature(FW_FEATURE_PS3_LV1)) 557c6131fa5SGeert Uytterhoeven return -ENODEV; 558c6131fa5SGeert Uytterhoeven 559c6131fa5SGeert Uytterhoeven error = register_blkdev(0, DEVICE_NAME); 560c6131fa5SGeert Uytterhoeven if (error <= 0) { 561c6131fa5SGeert Uytterhoeven printk(KERN_ERR "%s:%u: register_blkdev failed %d\n", __func__, 562c6131fa5SGeert Uytterhoeven __LINE__, error); 563c6131fa5SGeert Uytterhoeven return error; 564c6131fa5SGeert Uytterhoeven } 565c6131fa5SGeert Uytterhoeven ps3disk_major = error; 566c6131fa5SGeert Uytterhoeven 567c6131fa5SGeert Uytterhoeven pr_info("%s:%u: registered block device major %d\n", __func__, 568c6131fa5SGeert Uytterhoeven __LINE__, ps3disk_major); 569c6131fa5SGeert Uytterhoeven 570c6131fa5SGeert Uytterhoeven error = ps3_system_bus_driver_register(&ps3disk); 571c6131fa5SGeert Uytterhoeven if (error) 572c6131fa5SGeert Uytterhoeven unregister_blkdev(ps3disk_major, DEVICE_NAME); 573c6131fa5SGeert Uytterhoeven 574c6131fa5SGeert Uytterhoeven return error; 575c6131fa5SGeert Uytterhoeven } 576c6131fa5SGeert Uytterhoeven 577c6131fa5SGeert Uytterhoeven static void __exit ps3disk_exit(void) 578c6131fa5SGeert Uytterhoeven { 579c6131fa5SGeert Uytterhoeven ps3_system_bus_driver_unregister(&ps3disk); 580c6131fa5SGeert Uytterhoeven unregister_blkdev(ps3disk_major, DEVICE_NAME); 581c6131fa5SGeert Uytterhoeven } 582c6131fa5SGeert Uytterhoeven 583c6131fa5SGeert Uytterhoeven module_init(ps3disk_init); 584c6131fa5SGeert Uytterhoeven module_exit(ps3disk_exit); 585c6131fa5SGeert Uytterhoeven 586c6131fa5SGeert Uytterhoeven MODULE_LICENSE("GPL"); 587c6131fa5SGeert Uytterhoeven MODULE_DESCRIPTION("PS3 Disk Storage Driver"); 588c6131fa5SGeert Uytterhoeven MODULE_AUTHOR("Sony Corporation"); 589c6131fa5SGeert Uytterhoeven MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_DISK); 590