Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / scsi / ps3rom.c
blob986106269f0f16bd48f60b43960c1a10c84ea593
1 /*
2 * PS3 BD/DVD/CD-ROM Storage Driver
4 * Copyright (C) 2007 Sony Computer Entertainment Inc.
5 * Copyright 2007 Sony Corp.
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.
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.
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.
21 #include <linux/cdrom.h>
22 #include <linux/highmem.h>
24 #include <scsi/scsi.h>
25 #include <scsi/scsi_cmnd.h>
26 #include <scsi/scsi_dbg.h>
27 #include <scsi/scsi_device.h>
28 #include <scsi/scsi_host.h>
30 #include <asm/lv1call.h>
31 #include <asm/ps3stor.h>
34 #define DEVICE_NAME "ps3rom"
36 #define BOUNCE_SIZE (64*1024)
38 #define PS3ROM_MAX_SECTORS (BOUNCE_SIZE >> 9)
41 struct ps3rom_private {
42 struct ps3_storage_device *dev;
43 struct scsi_cmnd *curr_cmd;
47 #define LV1_STORAGE_SEND_ATAPI_COMMAND (1)
49 struct lv1_atapi_cmnd_block {
50 u8 pkt[32]; /* packet command block */
51 u32 pktlen; /* should be 12 for ATAPI 8020 */
52 u32 blocks;
53 u32 block_size;
54 u32 proto; /* transfer mode */
55 u32 in_out; /* transfer direction */
56 u64 buffer; /* parameter except command block */
57 u32 arglen; /* length above */
60 enum lv1_atapi_proto {
61 NON_DATA_PROTO = 0,
62 PIO_DATA_IN_PROTO = 1,
63 PIO_DATA_OUT_PROTO = 2,
64 DMA_PROTO = 3
67 enum lv1_atapi_in_out {
68 DIR_WRITE = 0, /* memory -> device */
69 DIR_READ = 1 /* device -> memory */
73 static int ps3rom_slave_configure(struct scsi_device *scsi_dev)
75 struct ps3rom_private *priv = shost_priv(scsi_dev->host);
76 struct ps3_storage_device *dev = priv->dev;
78 dev_dbg(&dev->sbd.core, "%s:%u: id %u, lun %u, channel %u\n", __func__,
79 __LINE__, scsi_dev->id, scsi_dev->lun, scsi_dev->channel);
82 * ATAPI SFF8020 devices use MODE_SENSE_10,
83 * so we can prohibit MODE_SENSE_6
85 scsi_dev->use_10_for_ms = 1;
87 /* we don't support {READ,WRITE}_6 */
88 scsi_dev->use_10_for_rw = 1;
90 return 0;
94 * copy data from device into scatter/gather buffer
96 static int fill_from_dev_buffer(struct scsi_cmnd *cmd, const void *buf)
98 int k, req_len, act_len, len, active;
99 void *kaddr;
100 struct scatterlist *sgpnt;
101 unsigned int buflen;
103 buflen = scsi_bufflen(cmd);
104 if (!buflen)
105 return 0;
107 if (!scsi_sglist(cmd))
108 return -1;
110 active = 1;
111 req_len = act_len = 0;
112 scsi_for_each_sg(cmd, sgpnt, scsi_sg_count(cmd), k) {
113 if (active) {
114 kaddr = kmap_atomic(sg_page(sgpnt), KM_IRQ0);
115 len = sgpnt->length;
116 if ((req_len + len) > buflen) {
117 active = 0;
118 len = buflen - req_len;
120 memcpy(kaddr + sgpnt->offset, buf + req_len, len);
121 flush_kernel_dcache_page(sg_page(sgpnt));
122 kunmap_atomic(kaddr, KM_IRQ0);
123 act_len += len;
125 req_len += sgpnt->length;
127 <<<<<<< HEAD:drivers/scsi/ps3rom.c
128 scsi_set_resid(cmd, req_len - act_len);
129 =======
130 scsi_set_resid(cmd, buflen - act_len);
131 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/ps3rom.c
132 return 0;
136 * copy data from scatter/gather into device's buffer
138 static int fetch_to_dev_buffer(struct scsi_cmnd *cmd, void *buf)
140 int k, req_len, len, fin;
141 void *kaddr;
142 struct scatterlist *sgpnt;
143 unsigned int buflen;
145 buflen = scsi_bufflen(cmd);
146 if (!buflen)
147 return 0;
149 if (!scsi_sglist(cmd))
150 return -1;
152 req_len = fin = 0;
153 scsi_for_each_sg(cmd, sgpnt, scsi_sg_count(cmd), k) {
154 kaddr = kmap_atomic(sg_page(sgpnt), KM_IRQ0);
155 len = sgpnt->length;
156 if ((req_len + len) > buflen) {
157 len = buflen - req_len;
158 fin = 1;
160 memcpy(buf + req_len, kaddr + sgpnt->offset, len);
161 kunmap_atomic(kaddr, KM_IRQ0);
162 if (fin)
163 return req_len + len;
164 req_len += sgpnt->length;
166 return req_len;
169 static int ps3rom_atapi_request(struct ps3_storage_device *dev,
170 struct scsi_cmnd *cmd)
172 struct lv1_atapi_cmnd_block atapi_cmnd;
173 unsigned char opcode = cmd->cmnd[0];
174 int res;
175 u64 lpar;
177 dev_dbg(&dev->sbd.core, "%s:%u: send ATAPI command 0x%02x\n", __func__,
178 __LINE__, opcode);
180 memset(&atapi_cmnd, 0, sizeof(struct lv1_atapi_cmnd_block));
181 memcpy(&atapi_cmnd.pkt, cmd->cmnd, 12);
182 atapi_cmnd.pktlen = 12;
183 atapi_cmnd.block_size = 1; /* transfer size is block_size * blocks */
184 atapi_cmnd.blocks = atapi_cmnd.arglen = scsi_bufflen(cmd);
185 atapi_cmnd.buffer = dev->bounce_lpar;
187 switch (cmd->sc_data_direction) {
188 case DMA_FROM_DEVICE:
189 if (scsi_bufflen(cmd) >= CD_FRAMESIZE)
190 atapi_cmnd.proto = DMA_PROTO;
191 else
192 atapi_cmnd.proto = PIO_DATA_IN_PROTO;
193 atapi_cmnd.in_out = DIR_READ;
194 break;
196 case DMA_TO_DEVICE:
197 if (scsi_bufflen(cmd) >= CD_FRAMESIZE)
198 atapi_cmnd.proto = DMA_PROTO;
199 else
200 atapi_cmnd.proto = PIO_DATA_OUT_PROTO;
201 atapi_cmnd.in_out = DIR_WRITE;
202 res = fetch_to_dev_buffer(cmd, dev->bounce_buf);
203 if (res < 0)
204 return DID_ERROR << 16;
205 break;
207 default:
208 atapi_cmnd.proto = NON_DATA_PROTO;
209 break;
212 lpar = ps3_mm_phys_to_lpar(__pa(&atapi_cmnd));
213 res = lv1_storage_send_device_command(dev->sbd.dev_id,
214 LV1_STORAGE_SEND_ATAPI_COMMAND,
215 lpar, sizeof(atapi_cmnd),
216 atapi_cmnd.buffer,
217 atapi_cmnd.arglen, &dev->tag);
218 if (res == LV1_DENIED_BY_POLICY) {
219 dev_dbg(&dev->sbd.core,
220 "%s:%u: ATAPI command 0x%02x denied by policy\n",
221 __func__, __LINE__, opcode);
222 return DID_ERROR << 16;
225 if (res) {
226 dev_err(&dev->sbd.core,
227 "%s:%u: ATAPI command 0x%02x failed %d\n", __func__,
228 __LINE__, opcode, res);
229 return DID_ERROR << 16;
232 return 0;
235 static inline unsigned int srb10_lba(const struct scsi_cmnd *cmd)
237 return cmd->cmnd[2] << 24 | cmd->cmnd[3] << 16 | cmd->cmnd[4] << 8 |
238 cmd->cmnd[5];
241 static inline unsigned int srb10_len(const struct scsi_cmnd *cmd)
243 return cmd->cmnd[7] << 8 | cmd->cmnd[8];
246 static int ps3rom_read_request(struct ps3_storage_device *dev,
247 struct scsi_cmnd *cmd, u32 start_sector,
248 u32 sectors)
250 int res;
252 dev_dbg(&dev->sbd.core, "%s:%u: read %u sectors starting at %u\n",
253 __func__, __LINE__, sectors, start_sector);
255 res = lv1_storage_read(dev->sbd.dev_id,
256 dev->regions[dev->region_idx].id, start_sector,
257 sectors, 0, dev->bounce_lpar, &dev->tag);
258 if (res) {
259 dev_err(&dev->sbd.core, "%s:%u: read failed %d\n", __func__,
260 __LINE__, res);
261 return DID_ERROR << 16;
264 return 0;
267 static int ps3rom_write_request(struct ps3_storage_device *dev,
268 struct scsi_cmnd *cmd, u32 start_sector,
269 u32 sectors)
271 int res;
273 dev_dbg(&dev->sbd.core, "%s:%u: write %u sectors starting at %u\n",
274 __func__, __LINE__, sectors, start_sector);
276 res = fetch_to_dev_buffer(cmd, dev->bounce_buf);
277 if (res < 0)
278 return DID_ERROR << 16;
280 res = lv1_storage_write(dev->sbd.dev_id,
281 dev->regions[dev->region_idx].id, start_sector,
282 sectors, 0, dev->bounce_lpar, &dev->tag);
283 if (res) {
284 dev_err(&dev->sbd.core, "%s:%u: write failed %d\n", __func__,
285 __LINE__, res);
286 return DID_ERROR << 16;
289 return 0;
292 static int ps3rom_queuecommand(struct scsi_cmnd *cmd,
293 void (*done)(struct scsi_cmnd *))
295 struct ps3rom_private *priv = shost_priv(cmd->device->host);
296 struct ps3_storage_device *dev = priv->dev;
297 unsigned char opcode;
298 int res;
300 #ifdef DEBUG
301 scsi_print_command(cmd);
302 #endif
304 priv->curr_cmd = cmd;
305 cmd->scsi_done = done;
307 opcode = cmd->cmnd[0];
309 * While we can submit READ/WRITE SCSI commands as ATAPI commands,
310 * it's recommended for various reasons (performance, error handling,
311 * ...) to use lv1_storage_{read,write}() instead
313 switch (opcode) {
314 case READ_10:
315 res = ps3rom_read_request(dev, cmd, srb10_lba(cmd),
316 srb10_len(cmd));
317 break;
319 case WRITE_10:
320 res = ps3rom_write_request(dev, cmd, srb10_lba(cmd),
321 srb10_len(cmd));
322 break;
324 default:
325 res = ps3rom_atapi_request(dev, cmd);
326 break;
329 if (res) {
330 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
331 cmd->result = res;
332 cmd->sense_buffer[0] = 0x70;
333 cmd->sense_buffer[2] = ILLEGAL_REQUEST;
334 priv->curr_cmd = NULL;
335 cmd->scsi_done(cmd);
338 return 0;
341 static int decode_lv1_status(u64 status, unsigned char *sense_key,
342 unsigned char *asc, unsigned char *ascq)
344 if (((status >> 24) & 0xff) != SAM_STAT_CHECK_CONDITION)
345 return -1;
347 *sense_key = (status >> 16) & 0xff;
348 *asc = (status >> 8) & 0xff;
349 *ascq = status & 0xff;
350 return 0;
353 static irqreturn_t ps3rom_interrupt(int irq, void *data)
355 struct ps3_storage_device *dev = data;
356 struct Scsi_Host *host;
357 struct ps3rom_private *priv;
358 struct scsi_cmnd *cmd;
359 int res;
360 u64 tag, status;
361 unsigned char sense_key, asc, ascq;
363 res = lv1_storage_get_async_status(dev->sbd.dev_id, &tag, &status);
365 * status = -1 may mean that ATAPI transport completed OK, but
366 * ATAPI command itself resulted CHECK CONDITION
367 * so, upper layer should issue REQUEST_SENSE to check the sense data
370 if (tag != dev->tag)
371 dev_err(&dev->sbd.core,
372 "%s:%u: tag mismatch, got %lx, expected %lx\n",
373 __func__, __LINE__, tag, dev->tag);
375 if (res) {
376 dev_err(&dev->sbd.core, "%s:%u: res=%d status=0x%lx\n",
377 __func__, __LINE__, res, status);
378 return IRQ_HANDLED;
381 host = dev->sbd.core.driver_data;
382 priv = shost_priv(host);
383 cmd = priv->curr_cmd;
385 if (!status) {
386 /* OK, completed */
387 if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
388 res = fill_from_dev_buffer(cmd, dev->bounce_buf);
389 if (res) {
390 cmd->result = DID_ERROR << 16;
391 goto done;
394 cmd->result = DID_OK << 16;
395 goto done;
398 if (cmd->cmnd[0] == REQUEST_SENSE) {
399 /* SCSI spec says request sense should never get error */
400 dev_err(&dev->sbd.core, "%s:%u: end error without autosense\n",
401 __func__, __LINE__);
402 cmd->result = DID_ERROR << 16 | SAM_STAT_CHECK_CONDITION;
403 goto done;
406 if (decode_lv1_status(status, &sense_key, &asc, &ascq)) {
407 cmd->result = DID_ERROR << 16;
408 goto done;
411 cmd->sense_buffer[0] = 0x70;
412 cmd->sense_buffer[2] = sense_key;
413 cmd->sense_buffer[7] = 16 - 6;
414 cmd->sense_buffer[12] = asc;
415 cmd->sense_buffer[13] = ascq;
416 cmd->result = SAM_STAT_CHECK_CONDITION;
418 done:
419 priv->curr_cmd = NULL;
420 cmd->scsi_done(cmd);
421 return IRQ_HANDLED;
424 static struct scsi_host_template ps3rom_host_template = {
425 .name = DEVICE_NAME,
426 .slave_configure = ps3rom_slave_configure,
427 .queuecommand = ps3rom_queuecommand,
428 .can_queue = 1,
429 .this_id = 7,
430 .sg_tablesize = SG_ALL,
431 .cmd_per_lun = 1,
432 .emulated = 1, /* only sg driver uses this */
433 .max_sectors = PS3ROM_MAX_SECTORS,
434 <<<<<<< HEAD:drivers/scsi/ps3rom.c
435 .use_clustering = ENABLE_CLUSTERING,
436 =======
437 .use_clustering = DISABLE_CLUSTERING,
438 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/scsi/ps3rom.c
439 .module = THIS_MODULE,
443 static int __devinit ps3rom_probe(struct ps3_system_bus_device *_dev)
445 struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
446 int error;
447 struct Scsi_Host *host;
448 struct ps3rom_private *priv;
450 if (dev->blk_size != CD_FRAMESIZE) {
451 dev_err(&dev->sbd.core,
452 "%s:%u: cannot handle block size %lu\n", __func__,
453 __LINE__, dev->blk_size);
454 return -EINVAL;
457 dev->bounce_size = BOUNCE_SIZE;
458 dev->bounce_buf = kmalloc(BOUNCE_SIZE, GFP_DMA);
459 if (!dev->bounce_buf)
460 return -ENOMEM;
462 error = ps3stor_setup(dev, ps3rom_interrupt);
463 if (error)
464 goto fail_free_bounce;
466 host = scsi_host_alloc(&ps3rom_host_template,
467 sizeof(struct ps3rom_private));
468 if (!host) {
469 dev_err(&dev->sbd.core, "%s:%u: scsi_host_alloc failed\n",
470 __func__, __LINE__);
471 goto fail_teardown;
474 priv = shost_priv(host);
475 dev->sbd.core.driver_data = host;
476 priv->dev = dev;
478 /* One device/LUN per SCSI bus */
479 host->max_id = 1;
480 host->max_lun = 1;
482 error = scsi_add_host(host, &dev->sbd.core);
483 if (error) {
484 dev_err(&dev->sbd.core, "%s:%u: scsi_host_alloc failed %d\n",
485 __func__, __LINE__, error);
486 error = -ENODEV;
487 goto fail_host_put;
490 scsi_scan_host(host);
491 return 0;
493 fail_host_put:
494 scsi_host_put(host);
495 dev->sbd.core.driver_data = NULL;
496 fail_teardown:
497 ps3stor_teardown(dev);
498 fail_free_bounce:
499 kfree(dev->bounce_buf);
500 return error;
503 static int ps3rom_remove(struct ps3_system_bus_device *_dev)
505 struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
506 struct Scsi_Host *host = dev->sbd.core.driver_data;
508 scsi_remove_host(host);
509 ps3stor_teardown(dev);
510 scsi_host_put(host);
511 dev->sbd.core.driver_data = NULL;
512 kfree(dev->bounce_buf);
513 return 0;
516 static struct ps3_system_bus_driver ps3rom = {
517 .match_id = PS3_MATCH_ID_STOR_ROM,
518 .core.name = DEVICE_NAME,
519 .core.owner = THIS_MODULE,
520 .probe = ps3rom_probe,
521 .remove = ps3rom_remove
525 static int __init ps3rom_init(void)
527 return ps3_system_bus_driver_register(&ps3rom);
530 static void __exit ps3rom_exit(void)
532 ps3_system_bus_driver_unregister(&ps3rom);
535 module_init(ps3rom_init);
536 module_exit(ps3rom_exit);
538 MODULE_LICENSE("GPL");
539 MODULE_DESCRIPTION("PS3 BD/DVD/CD-ROM Storage Driver");
540 MODULE_AUTHOR("Sony Corporation");
541 MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_ROM);