libata: fix ATAPI draining

With ATAPI transfer chunk size properly programmed, libata PIO HSM
should be able to handle full spurious data chunks. Also, it's a good
idea to suppress trailing data warning for misc ATAPI commands as
there can be many of them per command - for example, if the chunk size
is 16 and the drive tries to transfer 510 bytes, there can be 31
trailing data messages.

This patch makes the following updates to libata ATAPI PIO HSM
implementation.

* Make it drain full spurious chunks.

* Suppress trailing data warning message for misc commands.

* Put limit on how many bytes can be drained.

* If odd, round up consumed bytes and the number of bytes to be
drained. This gets the number of bytes to drain right for drivers
which do 16bit PIO.

This patch is partial backport of improve-ATAPI-data-xfer patchset
pending for #upstream.

Signed-off-by: Tejun Heo <htejun@gmail.com>
Signed-off-by: Jeff Garzik <jeff@garzik.org>

authored by Tejun Heo and committed by Jeff Garzik 140b5e59 f2dfc1a1

+67 -22
+65 -22
drivers/ata/libata-core.c
··· 64 #include <linux/libata.h> 65 #include <asm/semaphore.h> 66 #include <asm/byteorder.h> 67 68 #include "libata.h" 69 ··· 4653 } 4654 4655 /** 4656 * ata_std_qc_defer - Check whether a qc needs to be deferred 4657 * @qc: ATA command in question 4658 * ··· 5177 * Inherited from caller. 5178 * 5179 */ 5180 - 5181 - static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes) 5182 { 5183 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE); 5184 - struct scatterlist *sg = qc->__sg; 5185 - struct scatterlist *lsg = sg_last(qc->__sg, qc->n_elem); 5186 struct ata_port *ap = qc->ap; 5187 struct page *page; 5188 unsigned char *buf; 5189 unsigned int offset, count; 5190 - int no_more_sg = 0; 5191 - 5192 - if (qc->curbytes + bytes >= qc->nbytes) 5193 - ap->hsm_task_state = HSM_ST_LAST; 5194 5195 next_sg: 5196 - if (unlikely(no_more_sg)) { 5197 /* 5198 * The end of qc->sg is reached and the device expects 5199 * more data to transfer. In order not to overrun qc->sg ··· 5198 * - for write case, padding zero data to the device 5199 */ 5200 u16 pad_buf[1] = { 0 }; 5201 - unsigned int words = bytes >> 1; 5202 unsigned int i; 5203 5204 - if (words) /* warning if bytes > 1 */ 5205 - ata_dev_printk(qc->dev, KERN_WARNING, 5206 - "%u bytes trailing data\n", bytes); 5207 5208 - for (i = 0; i < words; i++) 5209 ap->ops->data_xfer(qc->dev, (unsigned char *)pad_buf, 2, do_write); 5210 5211 - ap->hsm_task_state = HSM_ST_LAST; 5212 - return; 5213 - } 5214 5215 - sg = qc->cursg; 5216 5217 page = sg_page(sg); 5218 offset = sg->offset + qc->cursg_ofs; ··· 5254 } 5255 5256 bytes -= count; 5257 qc->curbytes += count; 5258 qc->cursg_ofs += count; 5259 5260 if (qc->cursg_ofs == sg->length) { 5261 - if (qc->cursg == lsg) 5262 - no_more_sg = 1; 5263 - 5264 qc->cursg = sg_next(qc->cursg); 5265 qc->cursg_ofs = 0; 5266 } 5267 5268 if (bytes) 5269 goto next_sg; 5270 } 5271 5272 /** ··· 5310 5311 VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes); 5312 5313 - __atapi_pio_bytes(qc, bytes); 5314 ata_altstatus(ap); /* flush */ 5315 5316 return;
··· 64 #include <linux/libata.h> 65 #include <asm/semaphore.h> 66 #include <asm/byteorder.h> 67 + #include <linux/cdrom.h> 68 69 #include "libata.h" 70 ··· 4652 } 4653 4654 /** 4655 + * atapi_qc_may_overflow - Check whether data transfer may overflow 4656 + * @qc: ATA command in question 4657 + * 4658 + * ATAPI commands which transfer variable length data to host 4659 + * might overflow due to application error or hardare bug. This 4660 + * function checks whether overflow should be drained and ignored 4661 + * for @qc. 4662 + * 4663 + * LOCKING: 4664 + * None. 4665 + * 4666 + * RETURNS: 4667 + * 1 if @qc may overflow; otherwise, 0. 4668 + */ 4669 + static int atapi_qc_may_overflow(struct ata_queued_cmd *qc) 4670 + { 4671 + if (qc->tf.protocol != ATA_PROT_ATAPI && 4672 + qc->tf.protocol != ATA_PROT_ATAPI_DMA) 4673 + return 0; 4674 + 4675 + if (qc->tf.flags & ATA_TFLAG_WRITE) 4676 + return 0; 4677 + 4678 + switch (qc->cdb[0]) { 4679 + case READ_10: 4680 + case READ_12: 4681 + case WRITE_10: 4682 + case WRITE_12: 4683 + case GPCMD_READ_CD: 4684 + case GPCMD_READ_CD_MSF: 4685 + return 0; 4686 + } 4687 + 4688 + return 1; 4689 + } 4690 + 4691 + /** 4692 * ata_std_qc_defer - Check whether a qc needs to be deferred 4693 * @qc: ATA command in question 4694 * ··· 5139 * Inherited from caller. 5140 * 5141 */ 5142 + static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes) 5143 { 5144 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE); 5145 struct ata_port *ap = qc->ap; 5146 + struct ata_eh_info *ehi = &qc->dev->link->eh_info; 5147 + struct scatterlist *sg; 5148 struct page *page; 5149 unsigned char *buf; 5150 unsigned int offset, count; 5151 5152 next_sg: 5153 + sg = qc->cursg; 5154 + if (unlikely(!sg)) { 5155 /* 5156 * The end of qc->sg is reached and the device expects 5157 * more data to transfer. In order not to overrun qc->sg ··· 5164 * - for write case, padding zero data to the device 5165 */ 5166 u16 pad_buf[1] = { 0 }; 5167 unsigned int i; 5168 5169 + if (bytes > qc->curbytes - qc->nbytes + ATAPI_MAX_DRAIN) { 5170 + ata_ehi_push_desc(ehi, "too much trailing data " 5171 + "buf=%u cur=%u bytes=%u", 5172 + qc->nbytes, qc->curbytes, bytes); 5173 + return -1; 5174 + } 5175 5176 + /* overflow is exptected for misc ATAPI commands */ 5177 + if (bytes && !atapi_qc_may_overflow(qc)) 5178 + ata_dev_printk(qc->dev, KERN_WARNING, "ATAPI %u bytes " 5179 + "trailing data (cdb=%02x nbytes=%u)\n", 5180 + bytes, qc->cdb[0], qc->nbytes); 5181 + 5182 + for (i = 0; i < (bytes + 1) / 2; i++) 5183 ap->ops->data_xfer(qc->dev, (unsigned char *)pad_buf, 2, do_write); 5184 5185 + qc->curbytes += bytes; 5186 5187 + return 0; 5188 + } 5189 5190 page = sg_page(sg); 5191 offset = sg->offset + qc->cursg_ofs; ··· 5213 } 5214 5215 bytes -= count; 5216 + if ((count & 1) && bytes) 5217 + bytes--; 5218 qc->curbytes += count; 5219 qc->cursg_ofs += count; 5220 5221 if (qc->cursg_ofs == sg->length) { 5222 qc->cursg = sg_next(qc->cursg); 5223 qc->cursg_ofs = 0; 5224 } 5225 5226 if (bytes) 5227 goto next_sg; 5228 + 5229 + return 0; 5230 } 5231 5232 /** ··· 5268 5269 VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes); 5270 5271 + if (__atapi_pio_bytes(qc, bytes)) 5272 + goto err_out; 5273 ata_altstatus(ap); /* flush */ 5274 5275 return;
+2
include/linux/libata.h
··· 119 ATA_DEF_BUSY_WAIT = 10000, 120 ATA_SHORT_PAUSE = (HZ >> 6) + 1, 121 122 ATA_SHT_EMULATED = 1, 123 ATA_SHT_CMD_PER_LUN = 1, 124 ATA_SHT_THIS_ID = -1,
··· 119 ATA_DEF_BUSY_WAIT = 10000, 120 ATA_SHORT_PAUSE = (HZ >> 6) + 1, 121 122 + ATAPI_MAX_DRAIN = 16 << 10, 123 + 124 ATA_SHT_EMULATED = 1, 125 ATA_SHT_CMD_PER_LUN = 1, 126 ATA_SHT_THIS_ID = -1,