aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/ata/libata-scsi.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/ata/libata-scsi.c')
-rw-r--r--drivers/ata/libata-scsi.c496
1 files changed, 333 insertions, 163 deletions
diff --git a/drivers/ata/libata-scsi.c b/drivers/ata/libata-scsi.c
index e83647651b31..ea53e6a570b4 100644
--- a/drivers/ata/libata-scsi.c
+++ b/drivers/ata/libata-scsi.c
@@ -71,11 +71,10 @@ static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
71#define ALL_SUB_MPAGES 0xff 71#define ALL_SUB_MPAGES 0xff
72 72
73 73
74static const u8 def_rw_recovery_mpage[] = { 74static const u8 def_rw_recovery_mpage[RW_RECOVERY_MPAGE_LEN] = {
75 RW_RECOVERY_MPAGE, 75 RW_RECOVERY_MPAGE,
76 RW_RECOVERY_MPAGE_LEN - 2, 76 RW_RECOVERY_MPAGE_LEN - 2,
77 (1 << 7) | /* AWRE, sat-r06 say it shall be 0 */ 77 (1 << 7), /* AWRE */
78 (1 << 6), /* ARRE (auto read reallocation) */
79 0, /* read retry count */ 78 0, /* read retry count */
80 0, 0, 0, 0, 79 0, 0, 0, 0,
81 0, /* write retry count */ 80 0, /* write retry count */
@@ -450,13 +449,8 @@ static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
450 qc->scsicmd = cmd; 449 qc->scsicmd = cmd;
451 qc->scsidone = done; 450 qc->scsidone = done;
452 451
453 if (cmd->use_sg) { 452 qc->__sg = scsi_sglist(cmd);
454 qc->__sg = (struct scatterlist *) cmd->request_buffer; 453 qc->n_elem = scsi_sg_count(cmd);
455 qc->n_elem = cmd->use_sg;
456 } else if (cmd->request_bufflen) {
457 qc->__sg = &qc->sgent;
458 qc->n_elem = 1;
459 }
460 } else { 454 } else {
461 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1); 455 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
462 done(cmd); 456 done(cmd);
@@ -755,6 +749,13 @@ static void ata_scsi_sdev_config(struct scsi_device *sdev)
755{ 749{
756 sdev->use_10_for_rw = 1; 750 sdev->use_10_for_rw = 1;
757 sdev->use_10_for_ms = 1; 751 sdev->use_10_for_ms = 1;
752
753 /* Schedule policy is determined by ->qc_defer() callback and
754 * it needs to see every deferred qc. Set dev_blocked to 1 to
755 * prevent SCSI midlayer from automatically deferring
756 * requests.
757 */
758 sdev->max_device_blocked = 1;
758} 759}
759 760
760static void ata_scsi_dev_config(struct scsi_device *sdev, 761static void ata_scsi_dev_config(struct scsi_device *sdev,
@@ -943,6 +944,13 @@ static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
943 goto invalid_fld; /* LOEJ bit set not supported */ 944 goto invalid_fld; /* LOEJ bit set not supported */
944 if (((cdb[4] >> 4) & 0xf) != 0) 945 if (((cdb[4] >> 4) & 0xf) != 0)
945 goto invalid_fld; /* power conditions not supported */ 946 goto invalid_fld; /* power conditions not supported */
947
948 if (qc->dev->horkage & ATA_HORKAGE_SKIP_PM) {
949 /* the device lacks PM support, finish without doing anything */
950 scmd->result = SAM_STAT_GOOD;
951 return 1;
952 }
953
946 if (cdb[4] & 0x1) { 954 if (cdb[4] & 0x1) {
947 tf->nsect = 1; /* 1 sector, lba=0 */ 955 tf->nsect = 1; /* 1 sector, lba=0 */
948 956
@@ -1368,14 +1376,14 @@ static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
1368 case ATA_CMD_SET_FEATURES: 1376 case ATA_CMD_SET_FEATURES:
1369 if ((qc->tf.feature == SETFEATURES_WC_ON) || 1377 if ((qc->tf.feature == SETFEATURES_WC_ON) ||
1370 (qc->tf.feature == SETFEATURES_WC_OFF)) { 1378 (qc->tf.feature == SETFEATURES_WC_OFF)) {
1371 ap->eh_info.action |= ATA_EH_REVALIDATE; 1379 ap->link.eh_info.action |= ATA_EH_REVALIDATE;
1372 ata_port_schedule_eh(ap); 1380 ata_port_schedule_eh(ap);
1373 } 1381 }
1374 break; 1382 break;
1375 1383
1376 case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */ 1384 case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
1377 case ATA_CMD_SET_MULTI: /* multi_count changed */ 1385 case ATA_CMD_SET_MULTI: /* multi_count changed */
1378 ap->eh_info.action |= ATA_EH_REVALIDATE; 1386 ap->link.eh_info.action |= ATA_EH_REVALIDATE;
1379 ata_port_schedule_eh(ap); 1387 ata_port_schedule_eh(ap);
1380 break; 1388 break;
1381 } 1389 }
@@ -1422,37 +1430,6 @@ static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
1422} 1430}
1423 1431
1424/** 1432/**
1425 * ata_scmd_need_defer - Check whether we need to defer scmd
1426 * @dev: ATA device to which the command is addressed
1427 * @is_io: Is the command IO (and thus possibly NCQ)?
1428 *
1429 * NCQ and non-NCQ commands cannot run together. As upper layer
1430 * only knows the queue depth, we are responsible for maintaining
1431 * exclusion. This function checks whether a new command can be
1432 * issued to @dev.
1433 *
1434 * LOCKING:
1435 * spin_lock_irqsave(host lock)
1436 *
1437 * RETURNS:
1438 * 1 if deferring is needed, 0 otherwise.
1439 */
1440static int ata_scmd_need_defer(struct ata_device *dev, int is_io)
1441{
1442 struct ata_port *ap = dev->ap;
1443 int is_ncq = is_io && ata_ncq_enabled(dev);
1444
1445 if (is_ncq) {
1446 if (!ata_tag_valid(ap->active_tag))
1447 return 0;
1448 } else {
1449 if (!ata_tag_valid(ap->active_tag) && !ap->sactive)
1450 return 0;
1451 }
1452 return 1;
1453}
1454
1455/**
1456 * ata_scsi_translate - Translate then issue SCSI command to ATA device 1433 * ata_scsi_translate - Translate then issue SCSI command to ATA device
1457 * @dev: ATA device to which the command is addressed 1434 * @dev: ATA device to which the command is addressed
1458 * @cmd: SCSI command to execute 1435 * @cmd: SCSI command to execute
@@ -1483,14 +1460,12 @@ static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
1483 void (*done)(struct scsi_cmnd *), 1460 void (*done)(struct scsi_cmnd *),
1484 ata_xlat_func_t xlat_func) 1461 ata_xlat_func_t xlat_func)
1485{ 1462{
1463 struct ata_port *ap = dev->link->ap;
1486 struct ata_queued_cmd *qc; 1464 struct ata_queued_cmd *qc;
1487 int is_io = xlat_func == ata_scsi_rw_xlat; 1465 int rc;
1488 1466
1489 VPRINTK("ENTER\n"); 1467 VPRINTK("ENTER\n");
1490 1468
1491 if (unlikely(ata_scmd_need_defer(dev, is_io)))
1492 goto defer;
1493
1494 qc = ata_scsi_qc_new(dev, cmd, done); 1469 qc = ata_scsi_qc_new(dev, cmd, done);
1495 if (!qc) 1470 if (!qc)
1496 goto err_mem; 1471 goto err_mem;
@@ -1498,17 +1473,13 @@ static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
1498 /* data is present; dma-map it */ 1473 /* data is present; dma-map it */
1499 if (cmd->sc_data_direction == DMA_FROM_DEVICE || 1474 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1500 cmd->sc_data_direction == DMA_TO_DEVICE) { 1475 cmd->sc_data_direction == DMA_TO_DEVICE) {
1501 if (unlikely(cmd->request_bufflen < 1)) { 1476 if (unlikely(scsi_bufflen(cmd) < 1)) {
1502 ata_dev_printk(dev, KERN_WARNING, 1477 ata_dev_printk(dev, KERN_WARNING,
1503 "WARNING: zero len r/w req\n"); 1478 "WARNING: zero len r/w req\n");
1504 goto err_did; 1479 goto err_did;
1505 } 1480 }
1506 1481
1507 if (cmd->use_sg) 1482 ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd));
1508 ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
1509 else
1510 ata_sg_init_one(qc, cmd->request_buffer,
1511 cmd->request_bufflen);
1512 1483
1513 qc->dma_dir = cmd->sc_data_direction; 1484 qc->dma_dir = cmd->sc_data_direction;
1514 } 1485 }
@@ -1518,6 +1489,11 @@ static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
1518 if (xlat_func(qc)) 1489 if (xlat_func(qc))
1519 goto early_finish; 1490 goto early_finish;
1520 1491
1492 if (ap->ops->qc_defer) {
1493 if ((rc = ap->ops->qc_defer(qc)))
1494 goto defer;
1495 }
1496
1521 /* select device, send command to hardware */ 1497 /* select device, send command to hardware */
1522 ata_qc_issue(qc); 1498 ata_qc_issue(qc);
1523 1499
@@ -1539,8 +1515,12 @@ err_mem:
1539 return 0; 1515 return 0;
1540 1516
1541defer: 1517defer:
1518 ata_qc_free(qc);
1542 DPRINTK("EXIT - defer\n"); 1519 DPRINTK("EXIT - defer\n");
1543 return SCSI_MLQUEUE_DEVICE_BUSY; 1520 if (rc == ATA_DEFER_LINK)
1521 return SCSI_MLQUEUE_DEVICE_BUSY;
1522 else
1523 return SCSI_MLQUEUE_HOST_BUSY;
1544} 1524}
1545 1525
1546/** 1526/**
@@ -1562,15 +1542,14 @@ static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
1562 u8 *buf; 1542 u8 *buf;
1563 unsigned int buflen; 1543 unsigned int buflen;
1564 1544
1565 if (cmd->use_sg) { 1545 struct scatterlist *sg = scsi_sglist(cmd);
1566 struct scatterlist *sg;
1567 1546
1568 sg = (struct scatterlist *) cmd->request_buffer; 1547 if (sg) {
1569 buf = kmap_atomic(sg->page, KM_IRQ0) + sg->offset; 1548 buf = kmap_atomic(sg->page, KM_IRQ0) + sg->offset;
1570 buflen = sg->length; 1549 buflen = sg->length;
1571 } else { 1550 } else {
1572 buf = cmd->request_buffer; 1551 buf = NULL;
1573 buflen = cmd->request_bufflen; 1552 buflen = 0;
1574 } 1553 }
1575 1554
1576 *buf_out = buf; 1555 *buf_out = buf;
@@ -1590,12 +1569,9 @@ static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
1590 1569
1591static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf) 1570static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
1592{ 1571{
1593 if (cmd->use_sg) { 1572 struct scatterlist *sg = scsi_sglist(cmd);
1594 struct scatterlist *sg; 1573 if (sg)
1595
1596 sg = (struct scatterlist *) cmd->request_buffer;
1597 kunmap_atomic(buf - sg->offset, KM_IRQ0); 1574 kunmap_atomic(buf - sg->offset, KM_IRQ0);
1598 }
1599} 1575}
1600 1576
1601/** 1577/**
@@ -1817,6 +1793,62 @@ unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
1817} 1793}
1818 1794
1819/** 1795/**
1796 * ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info
1797 * @args: device IDENTIFY data / SCSI command of interest.
1798 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1799 * @buflen: Response buffer length.
1800 *
1801 * Yields SAT-specified ATA VPD page.
1802 *
1803 * LOCKING:
1804 * spin_lock_irqsave(host lock)
1805 */
1806
1807unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf,
1808 unsigned int buflen)
1809{
1810 u8 pbuf[60];
1811 struct ata_taskfile tf;
1812 unsigned int i;
1813
1814 if (!buflen)
1815 return 0;
1816
1817 memset(&pbuf, 0, sizeof(pbuf));
1818 memset(&tf, 0, sizeof(tf));
1819
1820 pbuf[1] = 0x89; /* our page code */
1821 pbuf[2] = (0x238 >> 8); /* page size fixed at 238h */
1822 pbuf[3] = (0x238 & 0xff);
1823
1824 memcpy(&pbuf[8], "linux ", 8);
1825 memcpy(&pbuf[16], "libata ", 16);
1826 memcpy(&pbuf[32], DRV_VERSION, 4);
1827 ata_id_string(args->id, &pbuf[32], ATA_ID_FW_REV, 4);
1828
1829 /* we don't store the ATA device signature, so we fake it */
1830
1831 tf.command = ATA_DRDY; /* really, this is Status reg */
1832 tf.lbal = 0x1;
1833 tf.nsect = 0x1;
1834
1835 ata_tf_to_fis(&tf, 0, 1, &pbuf[36]); /* TODO: PMP? */
1836 pbuf[36] = 0x34; /* force D2H Reg FIS (34h) */
1837
1838 pbuf[56] = ATA_CMD_ID_ATA;
1839
1840 i = min(buflen, 60U);
1841 memcpy(rbuf, &pbuf[0], i);
1842 buflen -= i;
1843
1844 if (!buflen)
1845 return 0;
1846
1847 memcpy(&rbuf[60], &args->id[0], min(buflen, 512U));
1848 return 0;
1849}
1850
1851/**
1820 * ata_scsiop_noop - Command handler that simply returns success. 1852 * ata_scsiop_noop - Command handler that simply returns success.
1821 * @args: device IDENTIFY data / SCSI command of interest. 1853 * @args: device IDENTIFY data / SCSI command of interest.
1822 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 1854 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
@@ -2273,8 +2305,8 @@ static void atapi_request_sense(struct ata_queued_cmd *qc)
2273 qc->tf.feature |= ATAPI_PKT_DMA; 2305 qc->tf.feature |= ATAPI_PKT_DMA;
2274 } else { 2306 } else {
2275 qc->tf.protocol = ATA_PROT_ATAPI; 2307 qc->tf.protocol = ATA_PROT_ATAPI;
2276 qc->tf.lbam = (8 * 1024) & 0xff; 2308 qc->tf.lbam = SCSI_SENSE_BUFFERSIZE;
2277 qc->tf.lbah = (8 * 1024) >> 8; 2309 qc->tf.lbah = 0;
2278 } 2310 }
2279 qc->nbytes = SCSI_SENSE_BUFFERSIZE; 2311 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2280 2312
@@ -2383,6 +2415,7 @@ static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
2383 struct ata_device *dev = qc->dev; 2415 struct ata_device *dev = qc->dev;
2384 int using_pio = (dev->flags & ATA_DFLAG_PIO); 2416 int using_pio = (dev->flags & ATA_DFLAG_PIO);
2385 int nodata = (scmd->sc_data_direction == DMA_NONE); 2417 int nodata = (scmd->sc_data_direction == DMA_NONE);
2418 unsigned int nbytes;
2386 2419
2387 memset(qc->cdb, 0, dev->cdb_len); 2420 memset(qc->cdb, 0, dev->cdb_len);
2388 memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len); 2421 memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
@@ -2396,20 +2429,26 @@ static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
2396 } 2429 }
2397 2430
2398 qc->tf.command = ATA_CMD_PACKET; 2431 qc->tf.command = ATA_CMD_PACKET;
2399 qc->nbytes = scmd->request_bufflen; 2432 qc->nbytes = scsi_bufflen(scmd);
2400 2433
2401 /* check whether ATAPI DMA is safe */ 2434 /* check whether ATAPI DMA is safe */
2402 if (!using_pio && ata_check_atapi_dma(qc)) 2435 if (!using_pio && ata_check_atapi_dma(qc))
2403 using_pio = 1; 2436 using_pio = 1;
2404 2437
2438 /* Some controller variants snoop this value for Packet transfers
2439 to do state machine and FIFO management. Thus we want to set it
2440 properly, and for DMA where it is effectively meaningless */
2441 nbytes = min(qc->nbytes, (unsigned int)63 * 1024);
2442
2443 qc->tf.lbam = (nbytes & 0xFF);
2444 qc->tf.lbah = (nbytes >> 8);
2445
2405 if (using_pio || nodata) { 2446 if (using_pio || nodata) {
2406 /* no data, or PIO data xfer */ 2447 /* no data, or PIO data xfer */
2407 if (nodata) 2448 if (nodata)
2408 qc->tf.protocol = ATA_PROT_ATAPI_NODATA; 2449 qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
2409 else 2450 else
2410 qc->tf.protocol = ATA_PROT_ATAPI; 2451 qc->tf.protocol = ATA_PROT_ATAPI;
2411 qc->tf.lbam = (8 * 1024) & 0xff;
2412 qc->tf.lbah = (8 * 1024) >> 8;
2413 } else { 2452 } else {
2414 /* DMA data xfer */ 2453 /* DMA data xfer */
2415 qc->tf.protocol = ATA_PROT_ATAPI_DMA; 2454 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
@@ -2420,24 +2459,42 @@ static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
2420 qc->tf.feature |= ATAPI_DMADIR; 2459 qc->tf.feature |= ATAPI_DMADIR;
2421 } 2460 }
2422 2461
2462
2463 /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE
2464 as ATAPI tape drives don't get this right otherwise */
2423 return 0; 2465 return 0;
2424} 2466}
2425 2467
2426static struct ata_device * ata_find_dev(struct ata_port *ap, int id) 2468static struct ata_device * ata_find_dev(struct ata_port *ap, int devno)
2427{ 2469{
2428 if (likely(id < ATA_MAX_DEVICES)) 2470 if (ap->nr_pmp_links == 0) {
2429 return &ap->device[id]; 2471 if (likely(devno < ata_link_max_devices(&ap->link)))
2472 return &ap->link.device[devno];
2473 } else {
2474 if (likely(devno < ap->nr_pmp_links))
2475 return &ap->pmp_link[devno].device[0];
2476 }
2477
2430 return NULL; 2478 return NULL;
2431} 2479}
2432 2480
2433static struct ata_device * __ata_scsi_find_dev(struct ata_port *ap, 2481static struct ata_device * __ata_scsi_find_dev(struct ata_port *ap,
2434 const struct scsi_device *scsidev) 2482 const struct scsi_device *scsidev)
2435{ 2483{
2484 int devno;
2485
2436 /* skip commands not addressed to targets we simulate */ 2486 /* skip commands not addressed to targets we simulate */
2437 if (unlikely(scsidev->channel || scsidev->lun)) 2487 if (ap->nr_pmp_links == 0) {
2438 return NULL; 2488 if (unlikely(scsidev->channel || scsidev->lun))
2489 return NULL;
2490 devno = scsidev->id;
2491 } else {
2492 if (unlikely(scsidev->id || scsidev->lun))
2493 return NULL;
2494 devno = scsidev->channel;
2495 }
2439 2496
2440 return ata_find_dev(ap, scsidev->id); 2497 return ata_find_dev(ap, devno);
2441} 2498}
2442 2499
2443/** 2500/**
@@ -2458,7 +2515,7 @@ static int ata_scsi_dev_enabled(struct ata_device *dev)
2458 if (unlikely(!ata_dev_enabled(dev))) 2515 if (unlikely(!ata_dev_enabled(dev)))
2459 return 0; 2516 return 0;
2460 2517
2461 if (!atapi_enabled || (dev->ap->flags & ATA_FLAG_NO_ATAPI)) { 2518 if (!atapi_enabled || (dev->link->ap->flags & ATA_FLAG_NO_ATAPI)) {
2462 if (unlikely(dev->class == ATA_DEV_ATAPI)) { 2519 if (unlikely(dev->class == ATA_DEV_ATAPI)) {
2463 ata_dev_printk(dev, KERN_WARNING, 2520 ata_dev_printk(dev, KERN_WARNING,
2464 "WARNING: ATAPI is %s, device ignored.\n", 2521 "WARNING: ATAPI is %s, device ignored.\n",
@@ -2631,7 +2688,7 @@ static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
2631 case ATA_CMD_WRITE_LONG_ONCE: 2688 case ATA_CMD_WRITE_LONG_ONCE:
2632 if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1) 2689 if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1)
2633 goto invalid_fld; 2690 goto invalid_fld;
2634 qc->sect_size = scmd->request_bufflen; 2691 qc->sect_size = scsi_bufflen(scmd);
2635 } 2692 }
2636 2693
2637 /* 2694 /*
@@ -2661,7 +2718,7 @@ static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
2661 * TODO: find out if we need to do more here to 2718 * TODO: find out if we need to do more here to
2662 * cover scatter/gather case. 2719 * cover scatter/gather case.
2663 */ 2720 */
2664 qc->nbytes = scmd->request_bufflen; 2721 qc->nbytes = scsi_bufflen(scmd);
2665 2722
2666 /* request result TF */ 2723 /* request result TF */
2667 qc->flags |= ATA_QCFLAG_RESULT_TF; 2724 qc->flags |= ATA_QCFLAG_RESULT_TF;
@@ -2746,28 +2803,48 @@ static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
2746 void (*done)(struct scsi_cmnd *), 2803 void (*done)(struct scsi_cmnd *),
2747 struct ata_device *dev) 2804 struct ata_device *dev)
2748{ 2805{
2806 u8 scsi_op = scmd->cmnd[0];
2807 ata_xlat_func_t xlat_func;
2749 int rc = 0; 2808 int rc = 0;
2750 2809
2751 if (unlikely(!scmd->cmd_len || scmd->cmd_len > dev->cdb_len)) {
2752 DPRINTK("bad CDB len=%u, max=%u\n",
2753 scmd->cmd_len, dev->cdb_len);
2754 scmd->result = DID_ERROR << 16;
2755 done(scmd);
2756 return 0;
2757 }
2758
2759 if (dev->class == ATA_DEV_ATA) { 2810 if (dev->class == ATA_DEV_ATA) {
2760 ata_xlat_func_t xlat_func = ata_get_xlat_func(dev, 2811 if (unlikely(!scmd->cmd_len || scmd->cmd_len > dev->cdb_len))
2761 scmd->cmnd[0]); 2812 goto bad_cdb_len;
2762 2813
2763 if (xlat_func) 2814 xlat_func = ata_get_xlat_func(dev, scsi_op);
2764 rc = ata_scsi_translate(dev, scmd, done, xlat_func); 2815 } else {
2765 else 2816 if (unlikely(!scmd->cmd_len))
2766 ata_scsi_simulate(dev, scmd, done); 2817 goto bad_cdb_len;
2767 } else 2818
2768 rc = ata_scsi_translate(dev, scmd, done, atapi_xlat); 2819 xlat_func = NULL;
2820 if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
2821 /* relay SCSI command to ATAPI device */
2822 if (unlikely(scmd->cmd_len > dev->cdb_len))
2823 goto bad_cdb_len;
2824
2825 xlat_func = atapi_xlat;
2826 } else {
2827 /* ATA_16 passthru, treat as an ATA command */
2828 if (unlikely(scmd->cmd_len > 16))
2829 goto bad_cdb_len;
2830
2831 xlat_func = ata_get_xlat_func(dev, scsi_op);
2832 }
2833 }
2834
2835 if (xlat_func)
2836 rc = ata_scsi_translate(dev, scmd, done, xlat_func);
2837 else
2838 ata_scsi_simulate(dev, scmd, done);
2769 2839
2770 return rc; 2840 return rc;
2841
2842 bad_cdb_len:
2843 DPRINTK("bad CDB len=%u, scsi_op=0x%02x, max=%u\n",
2844 scmd->cmd_len, scsi_op, dev->cdb_len);
2845 scmd->result = DID_ERROR << 16;
2846 done(scmd);
2847 return 0;
2771} 2848}
2772 2849
2773/** 2850/**
@@ -2835,6 +2912,7 @@ void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd,
2835{ 2912{
2836 struct ata_scsi_args args; 2913 struct ata_scsi_args args;
2837 const u8 *scsicmd = cmd->cmnd; 2914 const u8 *scsicmd = cmd->cmnd;
2915 u8 tmp8;
2838 2916
2839 args.dev = dev; 2917 args.dev = dev;
2840 args.id = dev->id; 2918 args.id = dev->id;
@@ -2842,15 +2920,9 @@ void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd,
2842 args.done = done; 2920 args.done = done;
2843 2921
2844 switch(scsicmd[0]) { 2922 switch(scsicmd[0]) {
2845 /* no-op's, complete with success */ 2923 /* TODO: worth improving? */
2846 case SYNCHRONIZE_CACHE: 2924 case FORMAT_UNIT:
2847 case REZERO_UNIT: 2925 ata_scsi_invalid_field(cmd, done);
2848 case SEEK_6:
2849 case SEEK_10:
2850 case TEST_UNIT_READY:
2851 case FORMAT_UNIT: /* FIXME: correct? */
2852 case SEND_DIAGNOSTIC: /* FIXME: correct? */
2853 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
2854 break; 2926 break;
2855 2927
2856 case INQUIRY: 2928 case INQUIRY:
@@ -2858,14 +2930,23 @@ void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd,
2858 ata_scsi_invalid_field(cmd, done); 2930 ata_scsi_invalid_field(cmd, done);
2859 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */ 2931 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
2860 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std); 2932 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
2861 else if (scsicmd[2] == 0x00) 2933 else switch (scsicmd[2]) {
2934 case 0x00:
2862 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00); 2935 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
2863 else if (scsicmd[2] == 0x80) 2936 break;
2937 case 0x80:
2864 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80); 2938 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
2865 else if (scsicmd[2] == 0x83) 2939 break;
2940 case 0x83:
2866 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83); 2941 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
2867 else 2942 break;
2943 case 0x89:
2944 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89);
2945 break;
2946 default:
2868 ata_scsi_invalid_field(cmd, done); 2947 ata_scsi_invalid_field(cmd, done);
2948 break;
2949 }
2869 break; 2950 break;
2870 2951
2871 case MODE_SENSE: 2952 case MODE_SENSE:
@@ -2893,8 +2974,33 @@ void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd,
2893 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns); 2974 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
2894 break; 2975 break;
2895 2976
2896 /* mandatory commands we haven't implemented yet */
2897 case REQUEST_SENSE: 2977 case REQUEST_SENSE:
2978 ata_scsi_set_sense(cmd, 0, 0, 0);
2979 cmd->result = (DRIVER_SENSE << 24);
2980 done(cmd);
2981 break;
2982
2983 /* if we reach this, then writeback caching is disabled,
2984 * turning this into a no-op.
2985 */
2986 case SYNCHRONIZE_CACHE:
2987 /* fall through */
2988
2989 /* no-op's, complete with success */
2990 case REZERO_UNIT:
2991 case SEEK_6:
2992 case SEEK_10:
2993 case TEST_UNIT_READY:
2994 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
2995 break;
2996
2997 case SEND_DIAGNOSTIC:
2998 tmp8 = scsicmd[1] & ~(1 << 3);
2999 if ((tmp8 == 0x4) && (!scsicmd[3]) && (!scsicmd[4]))
3000 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
3001 else
3002 ata_scsi_invalid_field(cmd, done);
3003 break;
2898 3004
2899 /* all other commands */ 3005 /* all other commands */
2900 default: 3006 default:
@@ -2928,6 +3034,13 @@ int ata_scsi_add_hosts(struct ata_host *host, struct scsi_host_template *sht)
2928 shost->max_channel = 1; 3034 shost->max_channel = 1;
2929 shost->max_cmd_len = 16; 3035 shost->max_cmd_len = 16;
2930 3036
3037 /* Schedule policy is determined by ->qc_defer()
3038 * callback and it needs to see every deferred qc.
3039 * Set host_blocked to 1 to prevent SCSI midlayer from
3040 * automatically deferring requests.
3041 */
3042 shost->max_host_blocked = 1;
3043
2931 rc = scsi_add_host(ap->scsi_host, ap->host->dev); 3044 rc = scsi_add_host(ap->scsi_host, ap->host->dev);
2932 if (rc) 3045 if (rc)
2933 goto err_add; 3046 goto err_add;
@@ -2951,25 +3064,32 @@ void ata_scsi_scan_host(struct ata_port *ap, int sync)
2951{ 3064{
2952 int tries = 5; 3065 int tries = 5;
2953 struct ata_device *last_failed_dev = NULL; 3066 struct ata_device *last_failed_dev = NULL;
3067 struct ata_link *link;
2954 struct ata_device *dev; 3068 struct ata_device *dev;
2955 unsigned int i;
2956 3069
2957 if (ap->flags & ATA_FLAG_DISABLED) 3070 if (ap->flags & ATA_FLAG_DISABLED)
2958 return; 3071 return;
2959 3072
2960 repeat: 3073 repeat:
2961 for (i = 0; i < ATA_MAX_DEVICES; i++) { 3074 ata_port_for_each_link(link, ap) {
2962 struct scsi_device *sdev; 3075 ata_link_for_each_dev(dev, link) {
3076 struct scsi_device *sdev;
3077 int channel = 0, id = 0;
2963 3078
2964 dev = &ap->device[i]; 3079 if (!ata_dev_enabled(dev) || dev->sdev)
3080 continue;
2965 3081
2966 if (!ata_dev_enabled(dev) || dev->sdev) 3082 if (ata_is_host_link(link))
2967 continue; 3083 id = dev->devno;
3084 else
3085 channel = link->pmp;
2968 3086
2969 sdev = __scsi_add_device(ap->scsi_host, 0, i, 0, NULL); 3087 sdev = __scsi_add_device(ap->scsi_host, channel, id, 0,
2970 if (!IS_ERR(sdev)) { 3088 NULL);
2971 dev->sdev = sdev; 3089 if (!IS_ERR(sdev)) {
2972 scsi_device_put(sdev); 3090 dev->sdev = sdev;
3091 scsi_device_put(sdev);
3092 }
2973 } 3093 }
2974 } 3094 }
2975 3095
@@ -2977,12 +3097,14 @@ void ata_scsi_scan_host(struct ata_port *ap, int sync)
2977 * failure occurred, scan would have failed silently. Check 3097 * failure occurred, scan would have failed silently. Check
2978 * whether all devices are attached. 3098 * whether all devices are attached.
2979 */ 3099 */
2980 for (i = 0; i < ATA_MAX_DEVICES; i++) { 3100 ata_port_for_each_link(link, ap) {
2981 dev = &ap->device[i]; 3101 ata_link_for_each_dev(dev, link) {
2982 if (ata_dev_enabled(dev) && !dev->sdev) 3102 if (ata_dev_enabled(dev) && !dev->sdev)
2983 break; 3103 goto exit_loop;
3104 }
2984 } 3105 }
2985 if (i == ATA_MAX_DEVICES) 3106 exit_loop:
3107 if (!link)
2986 return; 3108 return;
2987 3109
2988 /* we're missing some SCSI devices */ 3110 /* we're missing some SCSI devices */
@@ -3049,7 +3171,7 @@ int ata_scsi_offline_dev(struct ata_device *dev)
3049 */ 3171 */
3050static void ata_scsi_remove_dev(struct ata_device *dev) 3172static void ata_scsi_remove_dev(struct ata_device *dev)
3051{ 3173{
3052 struct ata_port *ap = dev->ap; 3174 struct ata_port *ap = dev->link->ap;
3053 struct scsi_device *sdev; 3175 struct scsi_device *sdev;
3054 unsigned long flags; 3176 unsigned long flags;
3055 3177
@@ -3096,6 +3218,43 @@ static void ata_scsi_remove_dev(struct ata_device *dev)
3096 } 3218 }
3097} 3219}
3098 3220
3221static void ata_scsi_handle_link_detach(struct ata_link *link)
3222{
3223 struct ata_port *ap = link->ap;
3224 struct ata_device *dev;
3225
3226 ata_link_for_each_dev(dev, link) {
3227 unsigned long flags;
3228
3229 if (!(dev->flags & ATA_DFLAG_DETACHED))
3230 continue;
3231
3232 spin_lock_irqsave(ap->lock, flags);
3233 dev->flags &= ~ATA_DFLAG_DETACHED;
3234 spin_unlock_irqrestore(ap->lock, flags);
3235
3236 ata_scsi_remove_dev(dev);
3237 }
3238}
3239
3240/**
3241 * ata_scsi_media_change_notify - send media change event
3242 * @atadev: Pointer to the disk device with media change event
3243 *
3244 * Tell the block layer to send a media change notification
3245 * event.
3246 *
3247 * LOCKING:
3248 * spin_lock_irqsave(host lock)
3249 */
3250void ata_scsi_media_change_notify(struct ata_device *dev)
3251{
3252#ifdef OTHER_AN_PATCHES_HAVE_BEEN_APPLIED
3253 if (dev->sdev)
3254 scsi_device_event_notify(dev->sdev, SDEV_MEDIA_CHANGE);
3255#endif
3256}
3257
3099/** 3258/**
3100 * ata_scsi_hotplug - SCSI part of hotplug 3259 * ata_scsi_hotplug - SCSI part of hotplug
3101 * @work: Pointer to ATA port to perform SCSI hotplug on 3260 * @work: Pointer to ATA port to perform SCSI hotplug on
@@ -3121,20 +3280,14 @@ void ata_scsi_hotplug(struct work_struct *work)
3121 3280
3122 DPRINTK("ENTER\n"); 3281 DPRINTK("ENTER\n");
3123 3282
3124 /* unplug detached devices */ 3283 /* Unplug detached devices. We cannot use link iterator here
3125 for (i = 0; i < ATA_MAX_DEVICES; i++) { 3284 * because PMP links have to be scanned even if PMP is
3126 struct ata_device *dev = &ap->device[i]; 3285 * currently not attached. Iterate manually.
3127 unsigned long flags; 3286 */
3128 3287 ata_scsi_handle_link_detach(&ap->link);
3129 if (!(dev->flags & ATA_DFLAG_DETACHED)) 3288 if (ap->pmp_link)
3130 continue; 3289 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
3131 3290 ata_scsi_handle_link_detach(&ap->pmp_link[i]);
3132 spin_lock_irqsave(ap->lock, flags);
3133 dev->flags &= ~ATA_DFLAG_DETACHED;
3134 spin_unlock_irqrestore(ap->lock, flags);
3135
3136 ata_scsi_remove_dev(dev);
3137 }
3138 3291
3139 /* scan for new ones */ 3292 /* scan for new ones */
3140 ata_scsi_scan_host(ap, 0); 3293 ata_scsi_scan_host(ap, 0);
@@ -3163,27 +3316,42 @@ static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
3163{ 3316{
3164 struct ata_port *ap = ata_shost_to_port(shost); 3317 struct ata_port *ap = ata_shost_to_port(shost);
3165 unsigned long flags; 3318 unsigned long flags;
3166 int rc = 0; 3319 int devno, rc = 0;
3167 3320
3168 if (!ap->ops->error_handler) 3321 if (!ap->ops->error_handler)
3169 return -EOPNOTSUPP; 3322 return -EOPNOTSUPP;
3170 3323
3171 if ((channel != SCAN_WILD_CARD && channel != 0) || 3324 if (lun != SCAN_WILD_CARD && lun)
3172 (lun != SCAN_WILD_CARD && lun != 0))
3173 return -EINVAL; 3325 return -EINVAL;
3174 3326
3327 if (ap->nr_pmp_links == 0) {
3328 if (channel != SCAN_WILD_CARD && channel)
3329 return -EINVAL;
3330 devno = id;
3331 } else {
3332 if (id != SCAN_WILD_CARD && id)
3333 return -EINVAL;
3334 devno = channel;
3335 }
3336
3175 spin_lock_irqsave(ap->lock, flags); 3337 spin_lock_irqsave(ap->lock, flags);
3176 3338
3177 if (id == SCAN_WILD_CARD) { 3339 if (devno == SCAN_WILD_CARD) {
3178 ap->eh_info.probe_mask |= (1 << ATA_MAX_DEVICES) - 1; 3340 struct ata_link *link;
3179 ap->eh_info.action |= ATA_EH_SOFTRESET; 3341
3342 ata_port_for_each_link(link, ap) {
3343 struct ata_eh_info *ehi = &link->eh_info;
3344 ehi->probe_mask |= (1 << ata_link_max_devices(link)) - 1;
3345 ehi->action |= ATA_EH_SOFTRESET;
3346 }
3180 } else { 3347 } else {
3181 struct ata_device *dev = ata_find_dev(ap, id); 3348 struct ata_device *dev = ata_find_dev(ap, devno);
3182 3349
3183 if (dev) { 3350 if (dev) {
3184 ap->eh_info.probe_mask |= 1 << dev->devno; 3351 struct ata_eh_info *ehi = &dev->link->eh_info;
3185 ap->eh_info.action |= ATA_EH_SOFTRESET; 3352 ehi->probe_mask |= 1 << dev->devno;
3186 ap->eh_info.flags |= ATA_EHI_RESUME_LINK; 3353 ehi->action |= ATA_EH_SOFTRESET;
3354 ehi->flags |= ATA_EHI_RESUME_LINK;
3187 } else 3355 } else
3188 rc = -EINVAL; 3356 rc = -EINVAL;
3189 } 3357 }
@@ -3214,24 +3382,26 @@ void ata_scsi_dev_rescan(struct work_struct *work)
3214{ 3382{
3215 struct ata_port *ap = 3383 struct ata_port *ap =
3216 container_of(work, struct ata_port, scsi_rescan_task); 3384 container_of(work, struct ata_port, scsi_rescan_task);
3385 struct ata_link *link;
3386 struct ata_device *dev;
3217 unsigned long flags; 3387 unsigned long flags;
3218 unsigned int i;
3219 3388
3220 spin_lock_irqsave(ap->lock, flags); 3389 spin_lock_irqsave(ap->lock, flags);
3221 3390
3222 for (i = 0; i < ATA_MAX_DEVICES; i++) { 3391 ata_port_for_each_link(link, ap) {
3223 struct ata_device *dev = &ap->device[i]; 3392 ata_link_for_each_dev(dev, link) {
3224 struct scsi_device *sdev = dev->sdev; 3393 struct scsi_device *sdev = dev->sdev;
3225 3394
3226 if (!ata_dev_enabled(dev) || !sdev) 3395 if (!ata_dev_enabled(dev) || !sdev)
3227 continue; 3396 continue;
3228 if (scsi_device_get(sdev)) 3397 if (scsi_device_get(sdev))
3229 continue; 3398 continue;
3230 3399
3231 spin_unlock_irqrestore(ap->lock, flags); 3400 spin_unlock_irqrestore(ap->lock, flags);
3232 scsi_rescan_device(&(sdev->sdev_gendev)); 3401 scsi_rescan_device(&(sdev->sdev_gendev));
3233 scsi_device_put(sdev); 3402 scsi_device_put(sdev);
3234 spin_lock_irqsave(ap->lock, flags); 3403 spin_lock_irqsave(ap->lock, flags);
3404 }
3235 } 3405 }
3236 3406
3237 spin_unlock_irqrestore(ap->lock, flags); 3407 spin_unlock_irqrestore(ap->lock, flags);
@@ -3359,7 +3529,7 @@ EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
3359int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap) 3529int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
3360{ 3530{
3361 ata_scsi_sdev_config(sdev); 3531 ata_scsi_sdev_config(sdev);
3362 ata_scsi_dev_config(sdev, ap->device); 3532 ata_scsi_dev_config(sdev, ap->link.device);
3363 return 0; 3533 return 0;
3364} 3534}
3365EXPORT_SYMBOL_GPL(ata_sas_slave_configure); 3535EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
@@ -3382,8 +3552,8 @@ int ata_sas_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *),
3382 3552
3383 ata_scsi_dump_cdb(ap, cmd); 3553 ata_scsi_dump_cdb(ap, cmd);
3384 3554
3385 if (likely(ata_scsi_dev_enabled(ap->device))) 3555 if (likely(ata_scsi_dev_enabled(ap->link.device)))
3386 rc = __ata_scsi_queuecmd(cmd, done, ap->device); 3556 rc = __ata_scsi_queuecmd(cmd, done, ap->link.device);
3387 else { 3557 else {
3388 cmd->result = (DID_BAD_TARGET << 16); 3558 cmd->result = (DID_BAD_TARGET << 16);
3389 done(cmd); 3559 done(cmd);