linux/drivers/scsi/ps3rom.c
<<
>>
Prefs
   1/*
   2 * PS3 BD/DVD/CD-ROM Storage Driver
   3 *
   4 * Copyright (C) 2007 Sony Computer Entertainment Inc.
   5 * Copyright 2007 Sony Corp.
   6 *
   7 * This program is free software; you can redistribute it and/or modify it
   8 * under the terms of the GNU General Public License as published
   9 * by the Free Software Foundation; version 2 of the License.
  10 *
  11 * This program is distributed in the hope that it will be useful, but
  12 * WITHOUT ANY WARRANTY; without even the implied warranty of
  13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14 * General Public License for more details.
  15 *
  16 * You should have received a copy of the GNU General Public License along
  17 * with this program; if not, write to the Free Software Foundation, Inc.,
  18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19 */
  20
  21#include <linux/cdrom.h>
  22#include <linux/highmem.h>
  23#include <linux/module.h>
  24#include <linux/slab.h>
  25
  26#include <scsi/scsi.h>
  27#include <scsi/scsi_cmnd.h>
  28#include <scsi/scsi_dbg.h>
  29#include <scsi/scsi_device.h>
  30#include <scsi/scsi_host.h>
  31#include <scsi/scsi_eh.h>
  32
  33#include <asm/lv1call.h>
  34#include <asm/ps3stor.h>
  35
  36
  37#define DEVICE_NAME                     "ps3rom"
  38
  39#define BOUNCE_SIZE                     (64*1024)
  40
  41#define PS3ROM_MAX_SECTORS              (BOUNCE_SIZE >> 9)
  42
  43
  44struct ps3rom_private {
  45        struct ps3_storage_device *dev;
  46        struct scsi_cmnd *curr_cmd;
  47};
  48
  49
  50#define LV1_STORAGE_SEND_ATAPI_COMMAND  (1)
  51
  52struct lv1_atapi_cmnd_block {
  53        u8      pkt[32];        /* packet command block           */
  54        u32     pktlen;         /* should be 12 for ATAPI 8020    */
  55        u32     blocks;
  56        u32     block_size;
  57        u32     proto;          /* transfer mode                  */
  58        u32     in_out;         /* transfer direction             */
  59        u64     buffer;         /* parameter except command block */
  60        u32     arglen;         /* length above                   */
  61};
  62
  63enum lv1_atapi_proto {
  64        NON_DATA_PROTO     = 0,
  65        PIO_DATA_IN_PROTO  = 1,
  66        PIO_DATA_OUT_PROTO = 2,
  67        DMA_PROTO = 3
  68};
  69
  70enum lv1_atapi_in_out {
  71        DIR_WRITE = 0,          /* memory -> device */
  72        DIR_READ = 1            /* device -> memory */
  73};
  74
  75
  76static int ps3rom_slave_configure(struct scsi_device *scsi_dev)
  77{
  78        struct ps3rom_private *priv = shost_priv(scsi_dev->host);
  79        struct ps3_storage_device *dev = priv->dev;
  80
  81        dev_dbg(&dev->sbd.core, "%s:%u: id %u, lun %u, channel %u\n", __func__,
  82                __LINE__, scsi_dev->id, scsi_dev->lun, scsi_dev->channel);
  83
  84        /*
  85         * ATAPI SFF8020 devices use MODE_SENSE_10,
  86         * so we can prohibit MODE_SENSE_6
  87         */
  88        scsi_dev->use_10_for_ms = 1;
  89
  90        /* we don't support {READ,WRITE}_6 */
  91        scsi_dev->use_10_for_rw = 1;
  92
  93        return 0;
  94}
  95
  96static int ps3rom_atapi_request(struct ps3_storage_device *dev,
  97                                struct scsi_cmnd *cmd)
  98{
  99        struct lv1_atapi_cmnd_block atapi_cmnd;
 100        unsigned char opcode = cmd->cmnd[0];
 101        int res;
 102        u64 lpar;
 103
 104        dev_dbg(&dev->sbd.core, "%s:%u: send ATAPI command 0x%02x\n", __func__,
 105                __LINE__, opcode);
 106
 107        memset(&atapi_cmnd, 0, sizeof(struct lv1_atapi_cmnd_block));
 108        memcpy(&atapi_cmnd.pkt, cmd->cmnd, 12);
 109        atapi_cmnd.pktlen = 12;
 110        atapi_cmnd.block_size = 1; /* transfer size is block_size * blocks */
 111        atapi_cmnd.blocks = atapi_cmnd.arglen = scsi_bufflen(cmd);
 112        atapi_cmnd.buffer = dev->bounce_lpar;
 113
 114        switch (cmd->sc_data_direction) {
 115        case DMA_FROM_DEVICE:
 116                if (scsi_bufflen(cmd) >= CD_FRAMESIZE)
 117                        atapi_cmnd.proto = DMA_PROTO;
 118                else
 119                        atapi_cmnd.proto = PIO_DATA_IN_PROTO;
 120                atapi_cmnd.in_out = DIR_READ;
 121                break;
 122
 123        case DMA_TO_DEVICE:
 124                if (scsi_bufflen(cmd) >= CD_FRAMESIZE)
 125                        atapi_cmnd.proto = DMA_PROTO;
 126                else
 127                        atapi_cmnd.proto = PIO_DATA_OUT_PROTO;
 128                atapi_cmnd.in_out = DIR_WRITE;
 129                scsi_sg_copy_to_buffer(cmd, dev->bounce_buf, dev->bounce_size);
 130                break;
 131
 132        default:
 133                atapi_cmnd.proto = NON_DATA_PROTO;
 134                break;
 135        }
 136
 137        lpar = ps3_mm_phys_to_lpar(__pa(&atapi_cmnd));
 138        res = lv1_storage_send_device_command(dev->sbd.dev_id,
 139                                              LV1_STORAGE_SEND_ATAPI_COMMAND,
 140                                              lpar, sizeof(atapi_cmnd),
 141                                              atapi_cmnd.buffer,
 142                                              atapi_cmnd.arglen, &dev->tag);
 143        if (res == LV1_DENIED_BY_POLICY) {
 144                dev_dbg(&dev->sbd.core,
 145                        "%s:%u: ATAPI command 0x%02x denied by policy\n",
 146                        __func__, __LINE__, opcode);
 147                return DID_ERROR << 16;
 148        }
 149
 150        if (res) {
 151                dev_err(&dev->sbd.core,
 152                        "%s:%u: ATAPI command 0x%02x failed %d\n", __func__,
 153                        __LINE__, opcode, res);
 154                return DID_ERROR << 16;
 155        }
 156
 157        return 0;
 158}
 159
 160static inline unsigned int srb10_lba(const struct scsi_cmnd *cmd)
 161{
 162        return cmd->cmnd[2] << 24 | cmd->cmnd[3] << 16 | cmd->cmnd[4] << 8 |
 163               cmd->cmnd[5];
 164}
 165
 166static inline unsigned int srb10_len(const struct scsi_cmnd *cmd)
 167{
 168        return cmd->cmnd[7] << 8 | cmd->cmnd[8];
 169}
 170
 171static int ps3rom_read_request(struct ps3_storage_device *dev,
 172                               struct scsi_cmnd *cmd, u32 start_sector,
 173                               u32 sectors)
 174{
 175        int res;
 176
 177        dev_dbg(&dev->sbd.core, "%s:%u: read %u sectors starting at %u\n",
 178                __func__, __LINE__, sectors, start_sector);
 179
 180        res = lv1_storage_read(dev->sbd.dev_id,
 181                               dev->regions[dev->region_idx].id, start_sector,
 182                               sectors, 0, dev->bounce_lpar, &dev->tag);
 183        if (res) {
 184                dev_err(&dev->sbd.core, "%s:%u: read failed %d\n", __func__,
 185                        __LINE__, res);
 186                return DID_ERROR << 16;
 187        }
 188
 189        return 0;
 190}
 191
 192static int ps3rom_write_request(struct ps3_storage_device *dev,
 193                                struct scsi_cmnd *cmd, u32 start_sector,
 194                                u32 sectors)
 195{
 196        int res;
 197
 198        dev_dbg(&dev->sbd.core, "%s:%u: write %u sectors starting at %u\n",
 199                __func__, __LINE__, sectors, start_sector);
 200
 201        scsi_sg_copy_to_buffer(cmd, dev->bounce_buf, dev->bounce_size);
 202
 203        res = lv1_storage_write(dev->sbd.dev_id,
 204                                dev->regions[dev->region_idx].id, start_sector,
 205                                sectors, 0, dev->bounce_lpar, &dev->tag);
 206        if (res) {
 207                dev_err(&dev->sbd.core, "%s:%u: write failed %d\n", __func__,
 208                        __LINE__, res);
 209                return DID_ERROR << 16;
 210        }
 211
 212        return 0;
 213}
 214
 215static int ps3rom_queuecommand_lck(struct scsi_cmnd *cmd,
 216                               void (*done)(struct scsi_cmnd *))
 217{
 218        struct ps3rom_private *priv = shost_priv(cmd->device->host);
 219        struct ps3_storage_device *dev = priv->dev;
 220        unsigned char opcode;
 221        int res;
 222
 223#ifdef DEBUG
 224        scsi_print_command(cmd);
 225#endif
 226
 227        priv->curr_cmd = cmd;
 228        cmd->scsi_done = done;
 229
 230        opcode = cmd->cmnd[0];
 231        /*
 232         * While we can submit READ/WRITE SCSI commands as ATAPI commands,
 233         * it's recommended for various reasons (performance, error handling,
 234         * ...) to use lv1_storage_{read,write}() instead
 235         */
 236        switch (opcode) {
 237        case READ_10:
 238                res = ps3rom_read_request(dev, cmd, srb10_lba(cmd),
 239                                          srb10_len(cmd));
 240                break;
 241
 242        case WRITE_10:
 243                res = ps3rom_write_request(dev, cmd, srb10_lba(cmd),
 244                                           srb10_len(cmd));
 245                break;
 246
 247        default:
 248                res = ps3rom_atapi_request(dev, cmd);
 249                break;
 250        }
 251
 252        if (res) {
 253                memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
 254                cmd->result = res;
 255                cmd->sense_buffer[0] = 0x70;
 256                cmd->sense_buffer[2] = ILLEGAL_REQUEST;
 257                priv->curr_cmd = NULL;
 258                cmd->scsi_done(cmd);
 259        }
 260
 261        return 0;
 262}
 263
 264static DEF_SCSI_QCMD(ps3rom_queuecommand)
 265
 266static int decode_lv1_status(u64 status, unsigned char *sense_key,
 267                             unsigned char *asc, unsigned char *ascq)
 268{
 269        if (((status >> 24) & 0xff) != SAM_STAT_CHECK_CONDITION)
 270                return -1;
 271
 272        *sense_key = (status >> 16) & 0xff;
 273        *asc       = (status >>  8) & 0xff;
 274        *ascq      =  status        & 0xff;
 275        return 0;
 276}
 277
 278static irqreturn_t ps3rom_interrupt(int irq, void *data)
 279{
 280        struct ps3_storage_device *dev = data;
 281        struct Scsi_Host *host;
 282        struct ps3rom_private *priv;
 283        struct scsi_cmnd *cmd;
 284        int res;
 285        u64 tag, status;
 286        unsigned char sense_key, asc, ascq;
 287
 288        res = lv1_storage_get_async_status(dev->sbd.dev_id, &tag, &status);
 289        /*
 290         * status = -1 may mean that ATAPI transport completed OK, but
 291         * ATAPI command itself resulted CHECK CONDITION
 292         * so, upper layer should issue REQUEST_SENSE to check the sense data
 293         */
 294
 295        if (tag != dev->tag)
 296                dev_err(&dev->sbd.core,
 297                        "%s:%u: tag mismatch, got %llx, expected %llx\n",
 298                        __func__, __LINE__, tag, dev->tag);
 299
 300        if (res) {
 3              dev_err(& 297                   3a>, dev30 class="sref">sbd.,
 298  class="line" name="L299">.c#L285" id="L285" class="line" name="L285"> 285__LINE__, 3ounce_size);
 252        if , de3->sbdid="L275" class="line" name="L275"3dx].<3 href="+code=id" class="3ref">30"+code=ps3rom_queuecommand" class="sre3unce_lpar3/a>, &de"drivers/scsi/ps3rom.c#L281" id="L281" cla"> 136
 3              , -> 218        struct shost_priv(curr_cmd =  219        struct (, dev-> 209               3retur31s/scsi/ps3rom.c#L!#L285" id="L285" class="line" name="L285"> 285_299
 300        if (mp;3210        }
sbd.c#L299" id="L299" =NULL" class="sref">NULL;
 219    DMA_FROM_DEVIC href="+code=cmdDMA_FROM_DEVIC  285_299
3( 3      return 0;
 213}
,  218 200
ps3rom_queuecommand_lc3(stru3t scsi_cmnd *scsi_sg_copy_to_buffer(ps3rom_queuecommand_lc3<7ce_lpar3code=done" class="sref">3one, dev->bounce_buf<3="sref">s3si_cmnd *))
-&3t;srb10_lba( 218lv1_storage_geaca hrame"L2f"rom_private" class="s3rom_queuecommand_lck" clee="L218"> 218bounce_buf<3s="sref">3ev =  2203/a>  32"> 300        if (SCSI_SENSE_BUFFERSIZE);
sbdgoto class="line" name="L228"> 228        cm3res" clas3="sref">res;
 33" id="L223" class="line3 name32/scsi/ps3rom.c#L213" id="L213" class="3f="+code=3csi_print_command" class3"sref32s/scsi/ps3rom.c#L294" id="L294" ivers/scsi/ps3rom.c#L230" id="L230" class="line" name="L230"> 230      L2219"> 219    a href="drivename="L230"> 23a href="drive 285_299
cmd);
dev-&gmp; 225#endif
 206        if (res) {
 207                dev_err(& 297                   3priv-3gt;,
bounce_buf<3smd-&3a href="+code=scsi_done"3class32s:%u: write %u sectors start">SCSI_SENSE_BUFFERSIZE);
res);
 69" class="line" name="L269"> 269        if (((bounce_buf<3ne" class3"sref">done;
 228        cm3f">opcode3/a> = cmd 221        int         3* Whi33ass="sref">dev-> 265
status;
 267   _299
         * it's r33e=SCSI_SENSE_BUFFERSIZE" class="sref">SCSI_SENSE_BUFFERSIZE);
res);
 234 228        cm3fss="sref3s3rom.c#L235" id="L235" 3lass=3line" name="L2id="L275" class="line" name="L275"3vame="L223lass="line" name="L236">3236asc,  2333s="sref">curr_cmd =  253                status;
 267   a href="+code=cmd" class="sref">cm3fmd-&3, srb1ZE" class="sref">SCSI_SENSE_BUFFERSIZE);
 269        if (((bounce_buf<3         3 ));
 228        <" id="L246" class="line" name="L243     3          break;
 219        struct  257                p3id="L242"3class="line" name="L242"3 242<3a>        case299" id="L299" =NULL" class="sref">NULL;
 258                cmdsrb1, ));
 243     3          break;
 *hoste=pri+codetemplss=="sre136
host="da hr+codetemplss=lass="l99
 246host/scslass="lI_SENSE_BUFFERSIZE" class="srefDEVIC _NAM href="+code=cmdDEVIC _NAM /a>(ps3rom_queuecommand_lc33rom_atap3_request(
(ps3rom_queuecommand_lc3a>);
 264
 264ps3rom_queuecommand_lc3a));
 258       anme="L2lass="lI_SENSE_BUF1ps3rom_queuecommand_lc3a/a>     3250        }
lv1_storage_gethisef"lass="lI_SENSE_BUFFE7ps3rom_queuecommand_lc3ad="L242"3d="L252" class="line" na3e="L232"> 252     dev->ps3rom_queuecommand_lc3a">cmd, 0, ps3rom_queuecommand_lc3a        3a>->re  lv1_storage_geemulss="lass="lI_SENSE_BUFF1,SENSE_BUFFERSIZmp;cmd-3gt;ps3rom_queuecommand_lc3a/a>     3ss="sref">sense_buffer[2] 3 ps3rom_queuecommand_lc3a> 246dev->ps3rom_queuecommand_lc3arom_atap3t;c3d);
3259        }
 270 2603host;
 279{
 3      return 0;

 262}
 279{
 280    to_s="line" name="L279"> 279{
dev_   DEF_SCSI_QCMD(3a hre36s/scsi/ps3rom..c#L191" id="L191"m.c#L>lv1_storage_gee.c#Ld char ps3rom_3ueuecommand)
data;
 281    3+code=sta3us" class="sref">status<3a>, u3signed char * *host;
 282        struct3 char *363rom.c#L187" id="L187" class="line" na3q" class=3sref">ascq)
dev->

res) {
 207                dev_e9
3drivers/scsi/ps3rom.c#L230" id37"> 300        if BUFFERSIZmp;sbd.lx, expected %llx\n",
3      return -1;
sbd. 298  3rom.c#L207" id="L207" class="line" name="L20blkssref">dev->sense_key =3( 252        if ,  282        struct3=asc" cla3s="sref">asc       =3(ascq 3    =37"+code=ps3rom_queuecommand" class="sre3mp; 0xff;3de"drivers/scsi/p     dev-> 280    BOUNCE_( 282        struct3=code=sta3      return 0;
scsi_sg_copy_to_buffer(L280"> 280    kmalloc">scsi_sg_copy_kmalloclass="line" name="L2BOUNCE_( 298  GFP_DMAhref="+code=cmdGFP_DMAscsi    276}
scsi_sg_copy_to_buffer(        if ((int __LINE__,  282        struct3a" class=3sref">data)
 *dL191" id="L191"m.c#L>lv1_storage_gee.c#Ld ch"> 136
host;
rass="line" name="sref">host;
lv1_storage_gee.c#Ld ch        if ( 252        if goto class="line" na/a>-_freffeo_buf>lv1_storage_ge/a>-_freffeo_bufne" name="L282"> 282        struct3aasc" cla3t  284        int  136
scsi_sg_copy_e=pri+codealloc
host="da hr+codetemplss=lasse9
 scsi_cmnd *host;
se3se_key, shost_priv
ascq;
curr_cmd = res) {
 207                dev_eIZmp; 297                   3d, &a3p;srb10_lba( 218,
bounce_buf<3vers/scsi3ps3rom.c#L289" id="L289"3class39_STAT_CHECK_CONDITIONgoto class="line" na/a>-_teardowef="drivers/scsi/a>-_teardowene" name="L282"> 282        struct3 290<3pan class="comment">    3    *39"sref">cmd 291 221        int          * 3o, up39>        case299" id="L299" 26" class="line" name="L226name="L218"> 218        struct shost_priv( 223#ifdef  3              shost_priv(
 284        int ps3_storage_dL280"> 280     ass="sref">ps3_storage_device * !=  243ass="sref3>dev->
"!s:'s="sref">curr_cmd = shost_privlv1_storage_gemax_f"ge_dL28"sref">SAM_STAT_CHECK_CONDITION3ass="sref3>tag, srb1ZE" class="sref" class="sref">shost_privSAM_STAT_CHECK_CONDITION4ag" class4"sref">tag);
dL191" id="L191"m.c#L>lv1_storage_gee.c#Ld ch"> 136
shose=priadd_" clrom_private" class="" class="sref">shost_priv 207                dev_a>(, dev40s/scsi/ps3rom.c#L251" id="L251" m.c#L>lv1_storage_gee.c#Ld ch 299
4ounce_size);
 252        if  res) {
 207                dev_eIZmp; 297                   4"sref">de4->sbd0_lba( 218,
lv1_storage_gee.c#Ld ch >(de4<      */
40ass="sref">result =  -  *, &scsi_cmnd-_+code=ulass="sref">shos/a>-_+code=ulge_device *->, srb1ZE" class="srefaca hr anm" class="sref">shose=prir anm" clrom_private" class="" class="sref">shost_priv(status        &4 <4lt; 16;
 2704210        }
-_+code=ulass="sref">shos/a>-_+code=ulge_d" id="L246" class="line" name="L244<="sref">4shose=pri+code=ulrom_private" class="" class="sref">shost_priv(de4      return 0;
 223#ifdef  3               257                (de4"L213"> 213}
-_teardowef="drivers/scsi/a>-_teardowene" " id="L246" class="line" name="L244de"drivers/scsi/p;
(4one-_freffeo_buf>lv1_storage_ge/a>-_freffeo_bufne" " id="L246" class="line" name="L244 *))
curr_cmd = scsi_sg_copy_kfreflass="line" name="L2        scsi_sg_copy_to_buffer( >(srb1, lv1_storage_gee.c#Ld char 4ev =  2204/a>  42L270" class="line" name="L270"> 270host;
 279{
res;

 279{
 280    to_s="line" name="L279"> 279{
dev_   data;
 136
 3              ,  244name="L224"> 225#endif
shose=priremov=m" clrom_private" class="" class="sref">shost_priv(-4gt;curr_cmd = (-&4a href="+code=scsi_done"4class42s="sref">srb1ZE" class="srefaca h+code=ulass="sref">shose=pri+code=ulrom_private" class="" class="sref">shost_priv(done;
#ifdef  3               257                (opcode4/a> = dL191" id="L191"kfref">scsi_sg_copy_kfreflass="line" name="L2        scsi_sg_copy_to_buffer( >(status        &4        4* Whi43ass="id="L275" class="line" name="L275"4an class=4comment">         * it1s r43/scsi/ps3rom.c#L213" id="L213" class="4 name="L244"> 234 279{
host;
 99
lv1_storage_gematch_f"ne" name="L> 136

4236dev_  host/scslass=me="L> 136
ps3rom_queuecommand_lc4" id="L234" class="line" name="L234"> 2343L247" id="L24  dev_   279{
ps3rom_queuecommand_lc4fmd-&4, host;fob=lass          > 136
host;
ps3rom_queuecommand_lc4         4 hostremov=lass         > 136
host;
ps3rom_queuecommand_lc4 ">opcode4ef="drivers/scsi/ps3rom.4#L2404 id="L}ar      4          break;
 221        int     221        int shos__inil/scsiL191" id="L191" class_inilass="sref">shos class_inillass=lass        if ());
 279{
host;
(     4          break;
 246(shos__exil/scsiL191" id="L191" class_exilass="sref">shos class_exillass=lass        if ();

dL191" id="L191" hrefystem_bus e213" runregister9"> 279{
host;
(     4250        }
cmd, 0, shosmodulf_inillass="line" name="L2 class_inilass="sref">shos class_inillass >(->shosmodulf_exillass="line" name="L2 class_exilass="sref">shos class_exillass >(cmd-4gt; 244a/a>     4ss="sref">sense_buffer[2] 45ignedclass="line" naMODUL _LIC ivename="L230"> 23MODUL _LIC ivelass="mp;( 246 269  MODUL _DESCRIPe="Llass="mp;(res);
(c4d);
,
(4259        }



footer9> The original LXR software bys3romlxr@ ux.nodev_
subfooter9> lxr. ux.no kindly t_pred bysRedpill L pro ASlass, provider of L ux +cnd="Lef= ed Oo" r dioes ser"L27s since 1995.