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 85*83d5cde4SAlexey Dobriyan static const 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 { 12303fa68c2SGeert Uytterhoeven struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd); 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, 13783096ebfSTejun Heo "%s:%u: %s req has %u bvecs for %u sectors\n", 13883096ebfSTejun Heo __func__, __LINE__, op, n, blk_rq_sectors(req)); 139c6131fa5SGeert Uytterhoeven #endif 140c6131fa5SGeert Uytterhoeven 14183096ebfSTejun Heo start_sector = blk_rq_pos(req) * priv->blocking_factor; 14283096ebfSTejun Heo sectors = blk_rq_sectors(req) * priv->blocking_factor; 143e377c6e2SStephen Rothwell dev_dbg(&dev->sbd.core, "%s:%u: %s %llu sectors starting at %llu\n", 144c6131fa5SGeert Uytterhoeven __func__, __LINE__, op, sectors, start_sector); 145c6131fa5SGeert Uytterhoeven 146c6131fa5SGeert Uytterhoeven if (write) { 147c6131fa5SGeert Uytterhoeven ps3disk_scatter_gather(dev, req, 1); 148c6131fa5SGeert Uytterhoeven 149c6131fa5SGeert Uytterhoeven res = lv1_storage_write(dev->sbd.dev_id, region_id, 150c6131fa5SGeert Uytterhoeven start_sector, sectors, 0, 151c6131fa5SGeert Uytterhoeven dev->bounce_lpar, &dev->tag); 152c6131fa5SGeert Uytterhoeven } else { 153c6131fa5SGeert Uytterhoeven res = lv1_storage_read(dev->sbd.dev_id, region_id, 154c6131fa5SGeert Uytterhoeven start_sector, sectors, 0, 155c6131fa5SGeert Uytterhoeven dev->bounce_lpar, &dev->tag); 156c6131fa5SGeert Uytterhoeven } 157c6131fa5SGeert Uytterhoeven if (res) { 158c6131fa5SGeert Uytterhoeven dev_err(&dev->sbd.core, "%s:%u: %s failed %d\n", __func__, 159c6131fa5SGeert Uytterhoeven __LINE__, op, res); 16010e1e629STejun Heo __blk_end_request_all(req, -EIO); 161c6131fa5SGeert Uytterhoeven return 0; 162c6131fa5SGeert Uytterhoeven } 163c6131fa5SGeert Uytterhoeven 164c6131fa5SGeert Uytterhoeven priv->req = req; 165c6131fa5SGeert Uytterhoeven return 1; 166c6131fa5SGeert Uytterhoeven } 167c6131fa5SGeert Uytterhoeven 168c6131fa5SGeert Uytterhoeven static int ps3disk_submit_flush_request(struct ps3_storage_device *dev, 169c6131fa5SGeert Uytterhoeven struct request *req) 170c6131fa5SGeert Uytterhoeven { 17103fa68c2SGeert Uytterhoeven struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd); 172c6131fa5SGeert Uytterhoeven u64 res; 173c6131fa5SGeert Uytterhoeven 174c6131fa5SGeert Uytterhoeven dev_dbg(&dev->sbd.core, "%s:%u: flush request\n", __func__, __LINE__); 175c6131fa5SGeert Uytterhoeven 176c6131fa5SGeert Uytterhoeven res = lv1_storage_send_device_command(dev->sbd.dev_id, 177c6131fa5SGeert Uytterhoeven LV1_STORAGE_ATA_HDDOUT, 0, 0, 0, 178c6131fa5SGeert Uytterhoeven 0, &dev->tag); 179c6131fa5SGeert Uytterhoeven if (res) { 180e377c6e2SStephen Rothwell dev_err(&dev->sbd.core, "%s:%u: sync cache failed 0x%llx\n", 181c6131fa5SGeert Uytterhoeven __func__, __LINE__, res); 18210e1e629STejun Heo __blk_end_request_all(req, -EIO); 183c6131fa5SGeert Uytterhoeven return 0; 184c6131fa5SGeert Uytterhoeven } 185c6131fa5SGeert Uytterhoeven 186c6131fa5SGeert Uytterhoeven priv->req = req; 187c6131fa5SGeert Uytterhoeven return 1; 188c6131fa5SGeert Uytterhoeven } 189c6131fa5SGeert Uytterhoeven 190c6131fa5SGeert Uytterhoeven static void ps3disk_do_request(struct ps3_storage_device *dev, 191165125e1SJens Axboe struct request_queue *q) 192c6131fa5SGeert Uytterhoeven { 193c6131fa5SGeert Uytterhoeven struct request *req; 194c6131fa5SGeert Uytterhoeven 195c6131fa5SGeert Uytterhoeven dev_dbg(&dev->sbd.core, "%s:%u\n", __func__, __LINE__); 196c6131fa5SGeert Uytterhoeven 1979934c8c0STejun Heo while ((req = blk_fetch_request(q))) { 198c6131fa5SGeert Uytterhoeven if (blk_fs_request(req)) { 199c6131fa5SGeert Uytterhoeven if (ps3disk_submit_request_sg(dev, req)) 200c6131fa5SGeert Uytterhoeven break; 2011a8e2bddSDavid Woodhouse } else if (req->cmd_type == REQ_TYPE_LINUX_BLOCK && 2021a8e2bddSDavid Woodhouse req->cmd[0] == REQ_LB_OP_FLUSH) { 203c6131fa5SGeert Uytterhoeven if (ps3disk_submit_flush_request(dev, req)) 204c6131fa5SGeert Uytterhoeven 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 2591a8e2bddSDavid Woodhouse if (req->cmd_type == REQ_TYPE_LINUX_BLOCK && 2601a8e2bddSDavid Woodhouse req->cmd[0] == REQ_LB_OP_FLUSH) { 261c6131fa5SGeert Uytterhoeven read = 0; 262c6131fa5SGeert Uytterhoeven op = "flush"; 263c6131fa5SGeert Uytterhoeven } else { 264c6131fa5SGeert Uytterhoeven read = !rq_data_dir(req); 265c6131fa5SGeert Uytterhoeven op = read ? "read" : "write"; 266c6131fa5SGeert Uytterhoeven } 267c6131fa5SGeert Uytterhoeven if (status) { 268e377c6e2SStephen Rothwell dev_dbg(&dev->sbd.core, "%s:%u: %s failed 0x%llx\n", __func__, 269c6131fa5SGeert Uytterhoeven __LINE__, op, status); 270f01ab252SKiyoshi Ueda error = -EIO; 271c6131fa5SGeert Uytterhoeven } else { 272c6131fa5SGeert Uytterhoeven dev_dbg(&dev->sbd.core, "%s:%u: %s completed\n", __func__, 273c6131fa5SGeert Uytterhoeven __LINE__, op); 274f01ab252SKiyoshi Ueda error = 0; 275c6131fa5SGeert Uytterhoeven if (read) 276c6131fa5SGeert Uytterhoeven ps3disk_scatter_gather(dev, req, 0); 277c6131fa5SGeert Uytterhoeven } 278c6131fa5SGeert Uytterhoeven 279c6131fa5SGeert Uytterhoeven spin_lock(&priv->lock); 280cd4c34ebSTejun Heo __blk_end_request_all(req, error); 281c6131fa5SGeert Uytterhoeven priv->req = NULL; 282c6131fa5SGeert Uytterhoeven ps3disk_do_request(dev, priv->queue); 283c6131fa5SGeert Uytterhoeven spin_unlock(&priv->lock); 284c6131fa5SGeert Uytterhoeven 285c6131fa5SGeert Uytterhoeven return IRQ_HANDLED; 286c6131fa5SGeert Uytterhoeven } 287c6131fa5SGeert Uytterhoeven 288c6131fa5SGeert Uytterhoeven static int ps3disk_sync_cache(struct ps3_storage_device *dev) 289c6131fa5SGeert Uytterhoeven { 290c6131fa5SGeert Uytterhoeven u64 res; 291c6131fa5SGeert Uytterhoeven 292c6131fa5SGeert Uytterhoeven dev_dbg(&dev->sbd.core, "%s:%u: sync cache\n", __func__, __LINE__); 293c6131fa5SGeert Uytterhoeven 294c6131fa5SGeert Uytterhoeven res = ps3stor_send_command(dev, LV1_STORAGE_ATA_HDDOUT, 0, 0, 0, 0); 295c6131fa5SGeert Uytterhoeven if (res) { 296e377c6e2SStephen Rothwell dev_err(&dev->sbd.core, "%s:%u: sync cache failed 0x%llx\n", 297c6131fa5SGeert Uytterhoeven __func__, __LINE__, res); 298c6131fa5SGeert Uytterhoeven return -EIO; 299c6131fa5SGeert Uytterhoeven } 300c6131fa5SGeert Uytterhoeven return 0; 301c6131fa5SGeert Uytterhoeven } 302c6131fa5SGeert Uytterhoeven 303c6131fa5SGeert Uytterhoeven 304c6131fa5SGeert Uytterhoeven /* ATA helpers copied from drivers/ata/libata-core.c */ 305c6131fa5SGeert Uytterhoeven 306c6131fa5SGeert Uytterhoeven static void swap_buf_le16(u16 *buf, unsigned int buf_words) 307c6131fa5SGeert Uytterhoeven { 308c6131fa5SGeert Uytterhoeven #ifdef __BIG_ENDIAN 309c6131fa5SGeert Uytterhoeven unsigned int i; 310c6131fa5SGeert Uytterhoeven 311c6131fa5SGeert Uytterhoeven for (i = 0; i < buf_words; i++) 312c6131fa5SGeert Uytterhoeven buf[i] = le16_to_cpu(buf[i]); 313c6131fa5SGeert Uytterhoeven #endif /* __BIG_ENDIAN */ 314c6131fa5SGeert Uytterhoeven } 315c6131fa5SGeert Uytterhoeven 316c6131fa5SGeert Uytterhoeven static u64 ata_id_n_sectors(const u16 *id) 317c6131fa5SGeert Uytterhoeven { 318c6131fa5SGeert Uytterhoeven if (ata_id_has_lba(id)) { 319c6131fa5SGeert Uytterhoeven if (ata_id_has_lba48(id)) 320c6131fa5SGeert Uytterhoeven return ata_id_u64(id, 100); 321c6131fa5SGeert Uytterhoeven else 322c6131fa5SGeert Uytterhoeven return ata_id_u32(id, 60); 323c6131fa5SGeert Uytterhoeven } else { 324c6131fa5SGeert Uytterhoeven if (ata_id_current_chs_valid(id)) 325c6131fa5SGeert Uytterhoeven return ata_id_u32(id, 57); 326c6131fa5SGeert Uytterhoeven else 327c6131fa5SGeert Uytterhoeven return id[1] * id[3] * id[6]; 328c6131fa5SGeert Uytterhoeven } 329c6131fa5SGeert Uytterhoeven } 330c6131fa5SGeert Uytterhoeven 331c6131fa5SGeert Uytterhoeven static void ata_id_string(const u16 *id, unsigned char *s, unsigned int ofs, 332c6131fa5SGeert Uytterhoeven unsigned int len) 333c6131fa5SGeert Uytterhoeven { 334c6131fa5SGeert Uytterhoeven unsigned int c; 335c6131fa5SGeert Uytterhoeven 336c6131fa5SGeert Uytterhoeven while (len > 0) { 337c6131fa5SGeert Uytterhoeven c = id[ofs] >> 8; 338c6131fa5SGeert Uytterhoeven *s = c; 339c6131fa5SGeert Uytterhoeven s++; 340c6131fa5SGeert Uytterhoeven 341c6131fa5SGeert Uytterhoeven c = id[ofs] & 0xff; 342c6131fa5SGeert Uytterhoeven *s = c; 343c6131fa5SGeert Uytterhoeven s++; 344c6131fa5SGeert Uytterhoeven 345c6131fa5SGeert Uytterhoeven ofs++; 346c6131fa5SGeert Uytterhoeven len -= 2; 347c6131fa5SGeert Uytterhoeven } 348c6131fa5SGeert Uytterhoeven } 349c6131fa5SGeert Uytterhoeven 350c6131fa5SGeert Uytterhoeven static void ata_id_c_string(const u16 *id, unsigned char *s, unsigned int ofs, 351c6131fa5SGeert Uytterhoeven unsigned int len) 352c6131fa5SGeert Uytterhoeven { 353c6131fa5SGeert Uytterhoeven unsigned char *p; 354c6131fa5SGeert Uytterhoeven 355c6131fa5SGeert Uytterhoeven WARN_ON(!(len & 1)); 356c6131fa5SGeert Uytterhoeven 357c6131fa5SGeert Uytterhoeven ata_id_string(id, s, ofs, len - 1); 358c6131fa5SGeert Uytterhoeven 359c6131fa5SGeert Uytterhoeven p = s + strnlen(s, len - 1); 360c6131fa5SGeert Uytterhoeven while (p > s && p[-1] == ' ') 361c6131fa5SGeert Uytterhoeven p--; 362c6131fa5SGeert Uytterhoeven *p = '\0'; 363c6131fa5SGeert Uytterhoeven } 364c6131fa5SGeert Uytterhoeven 365c6131fa5SGeert Uytterhoeven static int ps3disk_identify(struct ps3_storage_device *dev) 366c6131fa5SGeert Uytterhoeven { 36703fa68c2SGeert Uytterhoeven struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd); 368c6131fa5SGeert Uytterhoeven struct lv1_ata_cmnd_block ata_cmnd; 369c6131fa5SGeert Uytterhoeven u16 *id = dev->bounce_buf; 370c6131fa5SGeert Uytterhoeven u64 res; 371c6131fa5SGeert Uytterhoeven 372c6131fa5SGeert Uytterhoeven dev_dbg(&dev->sbd.core, "%s:%u: identify disk\n", __func__, __LINE__); 373c6131fa5SGeert Uytterhoeven 374c6131fa5SGeert Uytterhoeven memset(&ata_cmnd, 0, sizeof(struct lv1_ata_cmnd_block)); 375c6131fa5SGeert Uytterhoeven ata_cmnd.command = ATA_CMD_ID_ATA; 376c6131fa5SGeert Uytterhoeven ata_cmnd.sector_count = 1; 377c6131fa5SGeert Uytterhoeven ata_cmnd.size = ata_cmnd.arglen = ATA_ID_WORDS * 2; 378c6131fa5SGeert Uytterhoeven ata_cmnd.buffer = dev->bounce_lpar; 379c6131fa5SGeert Uytterhoeven ata_cmnd.proto = PIO_DATA_IN_PROTO; 380c6131fa5SGeert Uytterhoeven ata_cmnd.in_out = DIR_READ; 381c6131fa5SGeert Uytterhoeven 382c6131fa5SGeert Uytterhoeven res = ps3stor_send_command(dev, LV1_STORAGE_SEND_ATA_COMMAND, 383c6131fa5SGeert Uytterhoeven ps3_mm_phys_to_lpar(__pa(&ata_cmnd)), 384c6131fa5SGeert Uytterhoeven sizeof(ata_cmnd), ata_cmnd.buffer, 385c6131fa5SGeert Uytterhoeven ata_cmnd.arglen); 386c6131fa5SGeert Uytterhoeven if (res) { 387e377c6e2SStephen Rothwell dev_err(&dev->sbd.core, "%s:%u: identify disk failed 0x%llx\n", 388c6131fa5SGeert Uytterhoeven __func__, __LINE__, res); 389c6131fa5SGeert Uytterhoeven return -EIO; 390c6131fa5SGeert Uytterhoeven } 391c6131fa5SGeert Uytterhoeven 392c6131fa5SGeert Uytterhoeven swap_buf_le16(id, ATA_ID_WORDS); 393c6131fa5SGeert Uytterhoeven 394c6131fa5SGeert Uytterhoeven /* All we're interested in are raw capacity and model name */ 395c6131fa5SGeert Uytterhoeven priv->raw_capacity = ata_id_n_sectors(id); 396c6131fa5SGeert Uytterhoeven ata_id_c_string(id, priv->model, ATA_ID_PROD, sizeof(priv->model)); 397c6131fa5SGeert Uytterhoeven return 0; 398c6131fa5SGeert Uytterhoeven } 399c6131fa5SGeert Uytterhoeven 400165125e1SJens Axboe static void ps3disk_prepare_flush(struct request_queue *q, struct request *req) 401c6131fa5SGeert Uytterhoeven { 402c6131fa5SGeert Uytterhoeven struct ps3_storage_device *dev = q->queuedata; 403c6131fa5SGeert Uytterhoeven 404c6131fa5SGeert Uytterhoeven dev_dbg(&dev->sbd.core, "%s:%u\n", __func__, __LINE__); 405c6131fa5SGeert Uytterhoeven 4061a8e2bddSDavid Woodhouse req->cmd_type = REQ_TYPE_LINUX_BLOCK; 4071a8e2bddSDavid Woodhouse req->cmd[0] = REQ_LB_OP_FLUSH; 408c6131fa5SGeert Uytterhoeven } 409c6131fa5SGeert Uytterhoeven 410c6131fa5SGeert Uytterhoeven static unsigned long ps3disk_mask; 411c6131fa5SGeert Uytterhoeven 412c6131fa5SGeert Uytterhoeven static DEFINE_MUTEX(ps3disk_mask_mutex); 413c6131fa5SGeert Uytterhoeven 414c6131fa5SGeert Uytterhoeven static int __devinit ps3disk_probe(struct ps3_system_bus_device *_dev) 415c6131fa5SGeert Uytterhoeven { 416c6131fa5SGeert Uytterhoeven struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core); 417c6131fa5SGeert Uytterhoeven struct ps3disk_private *priv; 418c6131fa5SGeert Uytterhoeven int error; 419c6131fa5SGeert Uytterhoeven unsigned int devidx; 420c6131fa5SGeert Uytterhoeven struct request_queue *queue; 421c6131fa5SGeert Uytterhoeven struct gendisk *gendisk; 422c6131fa5SGeert Uytterhoeven 423c6131fa5SGeert Uytterhoeven if (dev->blk_size < 512) { 424c6131fa5SGeert Uytterhoeven dev_err(&dev->sbd.core, 425e377c6e2SStephen Rothwell "%s:%u: cannot handle block size %llu\n", __func__, 426c6131fa5SGeert Uytterhoeven __LINE__, dev->blk_size); 427c6131fa5SGeert Uytterhoeven return -EINVAL; 428c6131fa5SGeert Uytterhoeven } 429c6131fa5SGeert Uytterhoeven 430c6131fa5SGeert Uytterhoeven BUILD_BUG_ON(PS3DISK_MAX_DISKS > BITS_PER_LONG); 431c6131fa5SGeert Uytterhoeven mutex_lock(&ps3disk_mask_mutex); 432c6131fa5SGeert Uytterhoeven devidx = find_first_zero_bit(&ps3disk_mask, PS3DISK_MAX_DISKS); 433c6131fa5SGeert Uytterhoeven if (devidx >= PS3DISK_MAX_DISKS) { 434c6131fa5SGeert Uytterhoeven dev_err(&dev->sbd.core, "%s:%u: Too many disks\n", __func__, 435c6131fa5SGeert Uytterhoeven __LINE__); 436c6131fa5SGeert Uytterhoeven mutex_unlock(&ps3disk_mask_mutex); 437c6131fa5SGeert Uytterhoeven return -ENOSPC; 438c6131fa5SGeert Uytterhoeven } 439c6131fa5SGeert Uytterhoeven __set_bit(devidx, &ps3disk_mask); 440c6131fa5SGeert Uytterhoeven mutex_unlock(&ps3disk_mask_mutex); 441c6131fa5SGeert Uytterhoeven 442c6131fa5SGeert Uytterhoeven priv = kzalloc(sizeof(*priv), GFP_KERNEL); 443c6131fa5SGeert Uytterhoeven if (!priv) { 444c6131fa5SGeert Uytterhoeven error = -ENOMEM; 445c6131fa5SGeert Uytterhoeven goto fail; 446c6131fa5SGeert Uytterhoeven } 447c6131fa5SGeert Uytterhoeven 44803fa68c2SGeert Uytterhoeven ps3_system_bus_set_drvdata(_dev, priv); 449c6131fa5SGeert Uytterhoeven spin_lock_init(&priv->lock); 450c6131fa5SGeert Uytterhoeven 451c6131fa5SGeert Uytterhoeven dev->bounce_size = BOUNCE_SIZE; 452c6131fa5SGeert Uytterhoeven dev->bounce_buf = kmalloc(BOUNCE_SIZE, GFP_DMA); 453c6131fa5SGeert Uytterhoeven if (!dev->bounce_buf) { 454c6131fa5SGeert Uytterhoeven error = -ENOMEM; 455c6131fa5SGeert Uytterhoeven goto fail_free_priv; 456c6131fa5SGeert Uytterhoeven } 457c6131fa5SGeert Uytterhoeven 458c6131fa5SGeert Uytterhoeven error = ps3stor_setup(dev, ps3disk_interrupt); 459c6131fa5SGeert Uytterhoeven if (error) 460c6131fa5SGeert Uytterhoeven goto fail_free_bounce; 461c6131fa5SGeert Uytterhoeven 462c6131fa5SGeert Uytterhoeven ps3disk_identify(dev); 463c6131fa5SGeert Uytterhoeven 464c6131fa5SGeert Uytterhoeven queue = blk_init_queue(ps3disk_request, &priv->lock); 465c6131fa5SGeert Uytterhoeven if (!queue) { 466c6131fa5SGeert Uytterhoeven dev_err(&dev->sbd.core, "%s:%u: blk_init_queue failed\n", 467c6131fa5SGeert Uytterhoeven __func__, __LINE__); 468c6131fa5SGeert Uytterhoeven error = -ENOMEM; 469c6131fa5SGeert Uytterhoeven goto fail_teardown; 470c6131fa5SGeert Uytterhoeven } 471c6131fa5SGeert Uytterhoeven 472c6131fa5SGeert Uytterhoeven priv->queue = queue; 473c6131fa5SGeert Uytterhoeven queue->queuedata = dev; 474c6131fa5SGeert Uytterhoeven 475c6131fa5SGeert Uytterhoeven blk_queue_bounce_limit(queue, BLK_BOUNCE_HIGH); 476c6131fa5SGeert Uytterhoeven 477c6131fa5SGeert Uytterhoeven blk_queue_max_sectors(queue, dev->bounce_size >> 9); 478c6131fa5SGeert Uytterhoeven blk_queue_segment_boundary(queue, -1UL); 479c6131fa5SGeert Uytterhoeven blk_queue_dma_alignment(queue, dev->blk_size-1); 480e1defc4fSMartin K. Petersen blk_queue_logical_block_size(queue, dev->blk_size); 481c6131fa5SGeert Uytterhoeven 482c6131fa5SGeert Uytterhoeven blk_queue_ordered(queue, QUEUE_ORDERED_DRAIN_FLUSH, 483c6131fa5SGeert Uytterhoeven ps3disk_prepare_flush); 484c6131fa5SGeert Uytterhoeven 485c6131fa5SGeert Uytterhoeven blk_queue_max_phys_segments(queue, -1); 486c6131fa5SGeert Uytterhoeven blk_queue_max_hw_segments(queue, -1); 487c6131fa5SGeert Uytterhoeven blk_queue_max_segment_size(queue, dev->bounce_size); 488c6131fa5SGeert Uytterhoeven 489c6131fa5SGeert Uytterhoeven gendisk = alloc_disk(PS3DISK_MINORS); 490c6131fa5SGeert Uytterhoeven if (!gendisk) { 491c6131fa5SGeert Uytterhoeven dev_err(&dev->sbd.core, "%s:%u: alloc_disk failed\n", __func__, 492c6131fa5SGeert Uytterhoeven __LINE__); 493c6131fa5SGeert Uytterhoeven error = -ENOMEM; 494c6131fa5SGeert Uytterhoeven goto fail_cleanup_queue; 495c6131fa5SGeert Uytterhoeven } 496c6131fa5SGeert Uytterhoeven 497c6131fa5SGeert Uytterhoeven priv->gendisk = gendisk; 498c6131fa5SGeert Uytterhoeven gendisk->major = ps3disk_major; 499c6131fa5SGeert Uytterhoeven gendisk->first_minor = devidx * PS3DISK_MINORS; 500c6131fa5SGeert Uytterhoeven gendisk->fops = &ps3disk_fops; 501c6131fa5SGeert Uytterhoeven gendisk->queue = queue; 502c6131fa5SGeert Uytterhoeven gendisk->private_data = dev; 503c6131fa5SGeert Uytterhoeven gendisk->driverfs_dev = &dev->sbd.core; 504c6131fa5SGeert Uytterhoeven snprintf(gendisk->disk_name, sizeof(gendisk->disk_name), PS3DISK_NAME, 505c6131fa5SGeert Uytterhoeven devidx+'a'); 506c6131fa5SGeert Uytterhoeven priv->blocking_factor = dev->blk_size >> 9; 507c6131fa5SGeert Uytterhoeven set_capacity(gendisk, 508c6131fa5SGeert Uytterhoeven dev->regions[dev->region_idx].size*priv->blocking_factor); 509c6131fa5SGeert Uytterhoeven 510c6131fa5SGeert Uytterhoeven dev_info(&dev->sbd.core, 511e377c6e2SStephen Rothwell "%s is a %s (%llu MiB total, %lu MiB for OtherOS)\n", 512c6131fa5SGeert Uytterhoeven gendisk->disk_name, priv->model, priv->raw_capacity >> 11, 513c6131fa5SGeert Uytterhoeven get_capacity(gendisk) >> 11); 514c6131fa5SGeert Uytterhoeven 515c6131fa5SGeert Uytterhoeven add_disk(gendisk); 516c6131fa5SGeert Uytterhoeven return 0; 517c6131fa5SGeert Uytterhoeven 518c6131fa5SGeert Uytterhoeven fail_cleanup_queue: 519c6131fa5SGeert Uytterhoeven blk_cleanup_queue(queue); 520c6131fa5SGeert Uytterhoeven fail_teardown: 521c6131fa5SGeert Uytterhoeven ps3stor_teardown(dev); 522c6131fa5SGeert Uytterhoeven fail_free_bounce: 523c6131fa5SGeert Uytterhoeven kfree(dev->bounce_buf); 524c6131fa5SGeert Uytterhoeven fail_free_priv: 525c6131fa5SGeert Uytterhoeven kfree(priv); 52603fa68c2SGeert Uytterhoeven ps3_system_bus_set_drvdata(_dev, NULL); 527c6131fa5SGeert Uytterhoeven fail: 528c6131fa5SGeert Uytterhoeven mutex_lock(&ps3disk_mask_mutex); 529c6131fa5SGeert Uytterhoeven __clear_bit(devidx, &ps3disk_mask); 530c6131fa5SGeert Uytterhoeven mutex_unlock(&ps3disk_mask_mutex); 531c6131fa5SGeert Uytterhoeven return error; 532c6131fa5SGeert Uytterhoeven } 533c6131fa5SGeert Uytterhoeven 534c6131fa5SGeert Uytterhoeven static int ps3disk_remove(struct ps3_system_bus_device *_dev) 535c6131fa5SGeert Uytterhoeven { 536c6131fa5SGeert Uytterhoeven struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core); 53703fa68c2SGeert Uytterhoeven struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd); 538c6131fa5SGeert Uytterhoeven 539c6131fa5SGeert Uytterhoeven mutex_lock(&ps3disk_mask_mutex); 540f331c029STejun Heo __clear_bit(MINOR(disk_devt(priv->gendisk)) / PS3DISK_MINORS, 541c6131fa5SGeert Uytterhoeven &ps3disk_mask); 542c6131fa5SGeert Uytterhoeven mutex_unlock(&ps3disk_mask_mutex); 543c6131fa5SGeert Uytterhoeven del_gendisk(priv->gendisk); 544c6131fa5SGeert Uytterhoeven blk_cleanup_queue(priv->queue); 545c6131fa5SGeert Uytterhoeven put_disk(priv->gendisk); 546c6131fa5SGeert Uytterhoeven dev_notice(&dev->sbd.core, "Synchronizing disk cache\n"); 547c6131fa5SGeert Uytterhoeven ps3disk_sync_cache(dev); 548c6131fa5SGeert Uytterhoeven ps3stor_teardown(dev); 549c6131fa5SGeert Uytterhoeven kfree(dev->bounce_buf); 550c6131fa5SGeert Uytterhoeven kfree(priv); 55103fa68c2SGeert Uytterhoeven ps3_system_bus_set_drvdata(_dev, NULL); 552c6131fa5SGeert Uytterhoeven return 0; 553c6131fa5SGeert Uytterhoeven } 554c6131fa5SGeert Uytterhoeven 555c6131fa5SGeert Uytterhoeven static struct ps3_system_bus_driver ps3disk = { 556c6131fa5SGeert Uytterhoeven .match_id = PS3_MATCH_ID_STOR_DISK, 557c6131fa5SGeert Uytterhoeven .core.name = DEVICE_NAME, 558c6131fa5SGeert Uytterhoeven .core.owner = THIS_MODULE, 559c6131fa5SGeert Uytterhoeven .probe = ps3disk_probe, 560c6131fa5SGeert Uytterhoeven .remove = ps3disk_remove, 561c6131fa5SGeert Uytterhoeven .shutdown = ps3disk_remove, 562c6131fa5SGeert Uytterhoeven }; 563c6131fa5SGeert Uytterhoeven 564c6131fa5SGeert Uytterhoeven 565c6131fa5SGeert Uytterhoeven static int __init ps3disk_init(void) 566c6131fa5SGeert Uytterhoeven { 567c6131fa5SGeert Uytterhoeven int error; 568c6131fa5SGeert Uytterhoeven 569c6131fa5SGeert Uytterhoeven if (!firmware_has_feature(FW_FEATURE_PS3_LV1)) 570c6131fa5SGeert Uytterhoeven return -ENODEV; 571c6131fa5SGeert Uytterhoeven 572c6131fa5SGeert Uytterhoeven error = register_blkdev(0, DEVICE_NAME); 573c6131fa5SGeert Uytterhoeven if (error <= 0) { 574c6131fa5SGeert Uytterhoeven printk(KERN_ERR "%s:%u: register_blkdev failed %d\n", __func__, 575c6131fa5SGeert Uytterhoeven __LINE__, error); 576c6131fa5SGeert Uytterhoeven return error; 577c6131fa5SGeert Uytterhoeven } 578c6131fa5SGeert Uytterhoeven ps3disk_major = error; 579c6131fa5SGeert Uytterhoeven 580c6131fa5SGeert Uytterhoeven pr_info("%s:%u: registered block device major %d\n", __func__, 581c6131fa5SGeert Uytterhoeven __LINE__, ps3disk_major); 582c6131fa5SGeert Uytterhoeven 583c6131fa5SGeert Uytterhoeven error = ps3_system_bus_driver_register(&ps3disk); 584c6131fa5SGeert Uytterhoeven if (error) 585c6131fa5SGeert Uytterhoeven unregister_blkdev(ps3disk_major, DEVICE_NAME); 586c6131fa5SGeert Uytterhoeven 587c6131fa5SGeert Uytterhoeven return error; 588c6131fa5SGeert Uytterhoeven } 589c6131fa5SGeert Uytterhoeven 590c6131fa5SGeert Uytterhoeven static void __exit ps3disk_exit(void) 591c6131fa5SGeert Uytterhoeven { 592c6131fa5SGeert Uytterhoeven ps3_system_bus_driver_unregister(&ps3disk); 593c6131fa5SGeert Uytterhoeven unregister_blkdev(ps3disk_major, DEVICE_NAME); 594c6131fa5SGeert Uytterhoeven } 595c6131fa5SGeert Uytterhoeven 596c6131fa5SGeert Uytterhoeven module_init(ps3disk_init); 597c6131fa5SGeert Uytterhoeven module_exit(ps3disk_exit); 598c6131fa5SGeert Uytterhoeven 599c6131fa5SGeert Uytterhoeven MODULE_LICENSE("GPL"); 600c6131fa5SGeert Uytterhoeven MODULE_DESCRIPTION("PS3 Disk Storage Driver"); 601c6131fa5SGeert Uytterhoeven MODULE_AUTHOR("Sony Corporation"); 602c6131fa5SGeert Uytterhoeven MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_DISK); 603