linux/drivers/scsi/ps3rom.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * PS3 BD/DVD/CD-ROM Storage Driver
   4 *
   5 * Copyright (C) 2007 Sony Computer Entertainment Inc.
   6 * Copyright 2007 Sony Corp.
   7 */
   8
   9#include <linux/cdrom.h>
  10#include <linux/highmem.h>
  11#include <linux/module.h>
  12#include <linux/slab.h>
  13
  14#include <scsi/scsi.h>
  15#include <scsi/scsi_cmnd.h>
  16#include <scsi/scsi_dbg.h>
  17#include <scsi/scsi_device.h>
  18#include <scsi/scsi_host.h>
  19#include <scsi/scsi_eh.h>
  20
  21#include <asm/lv1call.h>
  22#include <asm/ps3stor.h>
  23
  24
  25#define DEVICE_NAME                     "ps3rom"
  26
  27#define BOUNCE_SIZE                     (64*1024)
  28
  29#define PS3ROM_MAX_SECTORS              (BOUNCE_SIZE >> 9)
  30
  31
  32struct ps3rom_private {
  33        struct ps3_storage_device *dev;
  34        struct scsi_cmnd *curr_cmd;
  35};
  36
  37
  38#define LV1_STORAGE_SEND_ATAPI_COMMAND  (1)
  39
  40struct lv1_atapi_cmnd_block {
  41        u8      pkt[32];        /* packet command block           */
  42        u32     pktlen;         /* should be 12 for ATAPI 8020    */
  43        u32     blocks;
  44        u32     block_size;
  45        u32     proto;          /* transfer mode                  */
  46        u32     in_out;         /* transfer direction             */
  47        u64     buffer;         /* parameter except command block */
  48        u32     arglen;         /* length above                   */
  49};
  50
  51enum lv1_atapi_proto {
  52        NON_DATA_PROTO     = 0,
  53        PIO_DATA_IN_PROTO  = 1,
  54        PIO_DATA_OUT_PROTO = 2,
  55        DMA_PROTO = 3
  56};
  57
  58enum lv1_atapi_in_out {
  59        DIR_WRITE = 0,          /* memory -> device */
  60        DIR_READ = 1            /* device -> memory */
  61};
  62
  63
  64static int ps3rom_slave_configure(struct scsi_device *scsi_dev)
  65{
  66        struct ps3rom_private *priv = shost_priv(scsi_dev->host);
  67        struct ps3_storage_device *dev = priv->dev;
  68
  69        dev_dbg(&dev->sbd.core, "%s:%u: id %u, lun %llu, channel %u\n", __func__,
  70                __LINE__, scsi_dev->id, scsi_dev->lun, scsi_dev->channel);
  71
  72        /*
  73         * ATAPI SFF8020 devices use MODE_SENSE_10,
  74         * so we can prohibit MODE_SENSE_6
  75         */
  76        scsi_dev->use_10_for_ms = 1;
  77
  78        /* we don't support {READ,WRITE}_6 */
  79        scsi_dev->use_10_for_rw = 1;
  80
  81        return 0;
  82}
  83
  84static int ps3rom_atapi_request(struct ps3_storage_device *dev,
  85                                struct scsi_cmnd *cmd)
  86{
  87        struct lv1_atapi_cmnd_block atapi_cmnd;
  88        unsigned char opcode = cmd->cmnd[0];
  89        int res;
  90        u64 lpar;
  91
  92        dev_dbg(&dev->sbd.core, "%s:%u: send ATAPI command 0x%02x\n", __func__,
  93                __LINE__, opcode);
  94
  95        memset(&atapi_cmnd, 0, sizeof(struct lv1_atapi_cmnd_block));
  96        memcpy(&atapi_cmnd.pkt, cmd->cmnd, 12);
  97        atapi_cmnd.pktlen = 12;
  98        atapi_cmnd.block_size = 1; /* transfer size is block_size * blocks */
  99        atapi_cmnd.blocks = atapi_cmnd.arglen = scsi_bufflen(cmd);
 100        atapi_cmnd.buffer = dev->bounce_lpar;
 101
 102        switch (cmd->sc_data_direction) {
 103        case DMA_FROM_DEVICE:
 104                if (scsi_bufflen(cmd) >= CD_FRAMESIZE)
 105                        atapi_cmnd.proto = DMA_PROTO;
 106                else
 107                        atapi_cmnd.proto = PIO_DATA_IN_PROTO;
 108                atapi_cmnd.in_out = DIR_READ;
 109                break;
 110
 111        case DMA_TO_DEVICE:
 112                if (scsi_bufflen(cmd) >= CD_FRAMESIZE)
 113                        atapi_cmnd.proto = DMA_PROTO;
 114                else
 115                        atapi_cmnd.proto = PIO_DATA_OUT_PROTO;
 116                atapi_cmnd.in_out = DIR_WRITE;
 117                scsi_sg_copy_to_buffer(cmd, dev->bounce_buf, dev->bounce_size);
 118                break;
 119
 120        default:
 121                atapi_cmnd.proto = NON_DATA_PROTO;
 122                break;
 123        }
 124
 125        lpar = ps3_mm_phys_to_lpar(__pa(&atapi_cmnd));
 126        res = lv1_storage_send_device_command(dev->sbd.dev_id,
 127                                              LV1_STORAGE_SEND_ATAPI_COMMAND,
 128                                              lpar, sizeof(atapi_cmnd),
 129                                              atapi_cmnd.buffer,
 130                                              atapi_cmnd.arglen, &dev->tag);
 131        if (res == LV1_DENIED_BY_POLICY) {
 132                dev_dbg(&dev->sbd.core,
 133                        "%s:%u: ATAPI command 0x%02x denied by policy\n",
 134                        __func__, __LINE__, opcode);
 135                return DID_ERROR << 16;
 136        }
 137
 138        if (res) {
 139                dev_err(&dev->sbd.core,
 140                        "%s:%u: ATAPI command 0x%02x failed %d\n", __func__,
 141                        __LINE__, opcode, res);
 142                return DID_ERROR << 16;
 143        }
 144
 145        return 0;
 146}
 147
 148static inline unsigned int srb10_lba(const struct scsi_cmnd *cmd)
 149{
 150        return cmd->cmnd[2] << 24 | cmd->cmnd[3] << 16 | cmd->cmnd[4] << 8 |
 151               cmd->cmnd[5];
 152}
 153
 154static inline unsigned int srb10_len(const struct scsi_cmnd *cmd)
 155{
 156        return cmd->cmnd[7] << 8 | cmd->cmnd[8];
 157}
 158
 159static int ps3rom_read_request(struct ps3_storage_device *dev,
 160                               struct scsi_cmnd *cmd, u32 start_sector,
 161                               u32 sectors)
 162{
 163        int res;
 164
 165        dev_dbg(&dev->sbd.core, "%s:%u: read %u sectors starting at %u\n",
 166                __func__, __LINE__, sectors, start_sector);
 167
 168        res = lv1_storage_read(dev->sbd.dev_id,
 169                               dev->regions[dev->region_idx].id, start_sector,
 170                               sectors, 0, dev->bounce_lpar, &dev->tag);
 171        if (res) {
 172                dev_err(&dev->sbd.core, "%s:%u: read failed %d\n", __func__,
 173                        __LINE__, res);
 174                return DID_ERROR << 16;
 175        }
 176
 177        return 0;
 178}
 179
 180static int ps3rom_write_request(struct ps3_storage_device *dev,
 181                                struct scsi_cmnd *cmd, u32 start_sector,
 182                                u32 sectors)
 183{
 184        int res;
 185
 186        dev_dbg(&dev->sbd.core, "%s:%u: write %u sectors starting at %u\n",
 187                __func__, __LINE__, sectors, start_sector);
 188
 189        scsi_sg_copy_to_buffer(cmd, dev->bounce_buf, dev->bounce_size);
 190
 191        res = lv1_storage_write(dev->sbd.dev_id,
 192                                dev->regions[dev->region_idx].id, start_sector,
 193                                sectors, 0, dev->bounce_lpar, &dev->tag);
 194        if (res) {
 195                dev_err(&dev->sbd.core, "%s:%u: write failed %d\n", __func__,
 196                        __LINE__, res);
 197                return DID_ERROR << 16;
 198        }
 199
 200        return 0;
 201}
 202
 203static int ps3rom_queuecommand_lck(struct scsi_cmnd *cmd,
 204                               void (*done)(struct scsi_cmnd *))
 205{
 206        struct ps3rom_private *priv = shost_priv(cmd->device->host);
 207        struct ps3_storage_device *dev = priv->dev;
 208        unsigned char opcode;
 209        int res;
 210
 211        priv->curr_cmd = cmd;
 212        cmd->scsi_done = done;
 213
 214        opcode = cmd->cmnd[0];
 215        /*
 216         * While we can submit READ/WRITE SCSI commands as ATAPI commands,
 217         * it's recommended for various reasons (performance, error handling,
 218         * ...) to use lv1_storage_{read,write}() instead
 219         */
 220        switch (opcode) {
 221        case READ_10:
 222                res = ps3rom_read_request(dev, cmd, srb10_lba(cmd),
 223                                          srb10_len(cmd));
 224                break;
 225
 226        case WRITE_10:
 227                res = ps3rom_write_request(dev, cmd, srb10_lba(cmd),
 228                                           srb10_len(cmd));
 229                break;
 230
 231        default:
 232                res = ps3rom_atapi_request(dev, cmd);
 233                break;
 234        }
 235
 236        if (res) {
 237                scsi_build_sense(cmd, 0, ILLEGAL_REQUEST, 0, 0);
 238                cmd->result = res;
 239                priv->curr_cmd = NULL;
 240                cmd->scsi_done(cmd);
 241        }
 242
 243        return 0;
 244}
 245
 246static DEF_SCSI_QCMD(ps3rom_queuecommand)
 247
 248static int decode_lv1_status(u64 status, unsigned char *sense_key,
 249                             unsigned char *asc, unsigned char *ascq)
 250{
 251        if (((status >> 24) & 0xff) != SAM_STAT_CHECK_CONDITION)
 252                return -1;
 253
 254        *sense_key = (status >> 16) & 0xff;
 255        *asc       = (status >>  8) & 0xff;
 256        *ascq      =  status        & 0xff;
 257        return 0;
 258}
 259
 260static irqreturn_t ps3rom_interrupt(int irq, void *data)
 261{
 262        struct ps3_storage_device *dev = data;
 263        struct Scsi_Host *host;
 264        struct ps3rom_private *priv;
 265        struct scsi_cmnd *cmd;
 266        int res;
 267        u64 tag, status;
 268        unsigned char sense_key, asc, ascq;
 269
 270        res = lv1_storage_get_async_status(dev->sbd.dev_id, &tag, &status);
 271        /*
 272         * status = -1 may mean that ATAPI transport completed OK, but
 273         * ATAPI command itself resulted CHECK CONDITION
 274         * so, upper layer should issue REQUEST_SENSE to check the sense data
 275         */
 276
 277        if (tag != dev->tag)
 278                dev_err(&dev->sbd.core,
 279                        "%s:%u: tag mismatch, got %llx, expected %llx\n",
 280                        __func__, __LINE__, tag, dev->tag);
 281
 282        if (res) {
 283                dev_err(&dev->sbd.core, "%s:%u: res=%d status=0x%llx\n",
 284                        __func__, __LINE__, res, status);
 285                return IRQ_HANDLED;
 286        }
 287
 288        host = ps3_system_bus_get_drvdata(&dev->sbd);
 289        priv = shost_priv(host);
 290        cmd = priv->curr_cmd;
 291
 292        if (!status) {
 293                /* OK, completed */
 294                if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
 295                        int len;
 296
 297                        len = scsi_sg_copy_from_buffer(cmd,
 298                                                       dev->bounce_buf,
 299                                                       dev->bounce_size);
 300
 301                        scsi_set_resid(cmd, scsi_bufflen(cmd) - len);
 302                }
 303                cmd->result = DID_OK << 16;
 304                goto done;
 305        }
 306
 307        if (cmd->cmnd[0] == REQUEST_SENSE) {
 308                /* SCSI spec says request sense should never get error */
 309                dev_err(&dev->sbd.core, "%s:%u: end error without autosense\n",
 310                        __func__, __LINE__);
 311                cmd->result = DID_ERROR << 16 | SAM_STAT_CHECK_CONDITION;
 312                goto done;
 313        }
 314
 315        if (decode_lv1_status(status, &sense_key, &asc, &ascq)) {
 316                cmd->result = DID_ERROR << 16;
 317                goto done;
 318        }
 319
 320        scsi_build_sense(cmd, 0, sense_key, asc, ascq);
 321
 322done:
 323        priv->curr_cmd = NULL;
 324        cmd->scsi_done(cmd);
 325        return IRQ_HANDLED;
 326}
 327
 328static struct scsi_host_template ps3rom_host_template = {
 329        .name =                 DEVICE_NAME,
 330        .slave_configure =      ps3rom_slave_configure,
 331        .queuecommand =         ps3rom_queuecommand,
 332        .can_queue =            1,
 333        .this_id =              7,
 334        .sg_tablesize =         SG_ALL,
 335        .emulated =             1,              /* only sg driver uses this */
 336        .max_sectors =          PS3ROM_MAX_SECTORS,
 337        .module =               THIS_MODULE,
 338};
 339
 340
 341static int ps3rom_probe(struct ps3_system_bus_device *_dev)
 342{
 343        struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
 344        int error;
 345        struct Scsi_Host *host;
 346        struct ps3rom_private *priv;
 347
 348        if (dev->blk_size != CD_FRAMESIZE) {
 349                dev_err(&dev->sbd.core,
 350                        "%s:%u: cannot handle block size %llu\n", __func__,
 351                        __LINE__, dev->blk_size);
 352                return -EINVAL;
 353        }
 354
 355        dev->bounce_size = BOUNCE_SIZE;
 356        dev->bounce_buf = kmalloc(BOUNCE_SIZE, GFP_DMA);
 357        if (!dev->bounce_buf)
 358                return -ENOMEM;
 359
 360        error = ps3stor_setup(dev, ps3rom_interrupt);
 361        if (error)
 362                goto fail_free_bounce;
 363
 364        host = scsi_host_alloc(&ps3rom_host_template,
 365                               sizeof(struct ps3rom_private));
 366        if (!host) {
 367                dev_err(&dev->sbd.core, "%s:%u: scsi_host_alloc failed\n",
 368                        __func__, __LINE__);
 369                error = -ENOMEM;
 370                goto fail_teardown;
 371        }
 372
 373        priv = shost_priv(host);
 374        ps3_system_bus_set_drvdata(&dev->sbd, host);
 375        priv->dev = dev;
 376
 377        /* One device/LUN per SCSI bus */
 378        host->max_id = 1;
 379        host->max_lun = 1;
 380
 381        error = scsi_add_host(host, &dev->sbd.core);
 382        if (error) {
 383                dev_err(&dev->sbd.core, "%s:%u: scsi_host_alloc failed %d\n",
 384                        __func__, __LINE__, error);
 385                error = -ENODEV;
 386                goto fail_host_put;
 387        }
 388
 389        scsi_scan_host(host);
 390        return 0;
 391
 392fail_host_put:
 393        scsi_host_put(host);
 394        ps3_system_bus_set_drvdata(&dev->sbd, NULL);
 395fail_teardown:
 396        ps3stor_teardown(dev);
 397fail_free_bounce:
 398        kfree(dev->bounce_buf);
 399        return error;
 400}
 401
 402static void ps3rom_remove(struct ps3_system_bus_device *_dev)
 403{
 404        struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
 405        struct Scsi_Host *host = ps3_system_bus_get_drvdata(&dev->sbd);
 406
 407        scsi_remove_host(host);
 408        ps3stor_teardown(dev);
 409        scsi_host_put(host);
 410        ps3_system_bus_set_drvdata(&dev->sbd, NULL);
 411        kfree(dev->bounce_buf);
 412}
 413
 414static struct ps3_system_bus_driver ps3rom = {
 415        .match_id       = PS3_MATCH_ID_STOR_ROM,
 416        .core.name      = DEVICE_NAME,
 417        .core.owner     = THIS_MODULE,
 418        .probe          = ps3rom_probe,
 419        .remove         = ps3rom_remove
 420};
 421
 422
 423static int __init ps3rom_init(void)
 424{
 425        return ps3_system_bus_driver_register(&ps3rom);
 426}
 427
 428static void __exit ps3rom_exit(void)
 429{
 430        ps3_system_bus_driver_unregister(&ps3rom);
 431}
 432
 433module_init(ps3rom_init);
 434module_exit(ps3rom_exit);
 435
 436MODULE_LICENSE("GPL");
 437MODULE_DESCRIPTION("PS3 BD/DVD/CD-ROM Storage Driver");
 438MODULE_AUTHOR("Sony Corporation");
 439MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_ROM);
 440