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 64 #include <linux/libata.h> 65 65 #include <asm/semaphore.h> 66 66 #include <asm/byteorder.h> 67 + #include <linux/cdrom.h> 67 68 68 69 #include "libata.h" 69 70 ··· 4653 4652 } 4654 4653 4655 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 + /** 4656 4692 * ata_std_qc_defer - Check whether a qc needs to be deferred 4657 4693 * @qc: ATA command in question 4658 4694 * ··· 5177 5139 * Inherited from caller. 5178 5140 * 5179 5141 */ 5180 - 5181 - static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes) 5142 + static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes) 5182 5143 { 5183 5144 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 5145 struct ata_port *ap = qc->ap; 5146 + struct ata_eh_info *ehi = &qc->dev->link->eh_info; 5147 + struct scatterlist *sg; 5187 5148 struct page *page; 5188 5149 unsigned char *buf; 5189 5150 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 5151 5195 5152 next_sg: 5196 - if (unlikely(no_more_sg)) { 5153 + sg = qc->cursg; 5154 + if (unlikely(!sg)) { 5197 5155 /* 5198 5156 * The end of qc->sg is reached and the device expects 5199 5157 * more data to transfer. In order not to overrun qc->sg ··· 5198 5164 * - for write case, padding zero data to the device 5199 5165 */ 5200 5166 u16 pad_buf[1] = { 0 }; 5201 - unsigned int words = bytes >> 1; 5202 5167 unsigned int i; 5203 5168 5204 - if (words) /* warning if bytes > 1 */ 5205 - ata_dev_printk(qc->dev, KERN_WARNING, 5206 - "%u bytes trailing data\n", bytes); 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 + } 5207 5175 5208 - for (i = 0; i < words; i++) 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++) 5209 5183 ap->ops->data_xfer(qc->dev, (unsigned char *)pad_buf, 2, do_write); 5210 5184 5211 - ap->hsm_task_state = HSM_ST_LAST; 5212 - return; 5213 - } 5185 + qc->curbytes += bytes; 5214 5186 5215 - sg = qc->cursg; 5187 + return 0; 5188 + } 5216 5189 5217 5190 page = sg_page(sg); 5218 5191 offset = sg->offset + qc->cursg_ofs; ··· 5254 5213 } 5255 5214 5256 5215 bytes -= count; 5216 + if ((count & 1) && bytes) 5217 + bytes--; 5257 5218 qc->curbytes += count; 5258 5219 qc->cursg_ofs += count; 5259 5220 5260 5221 if (qc->cursg_ofs == sg->length) { 5261 - if (qc->cursg == lsg) 5262 - no_more_sg = 1; 5263 - 5264 5222 qc->cursg = sg_next(qc->cursg); 5265 5223 qc->cursg_ofs = 0; 5266 5224 } 5267 5225 5268 5226 if (bytes) 5269 5227 goto next_sg; 5228 + 5229 + return 0; 5270 5230 } 5271 5231 5272 5232 /** ··· 5310 5268 5311 5269 VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes); 5312 5270 5313 - __atapi_pio_bytes(qc, bytes); 5271 + if (__atapi_pio_bytes(qc, bytes)) 5272 + goto err_out; 5314 5273 ata_altstatus(ap); /* flush */ 5315 5274 5316 5275 return;
+2
include/linux/libata.h
··· 119 119 ATA_DEF_BUSY_WAIT = 10000, 120 120 ATA_SHORT_PAUSE = (HZ >> 6) + 1, 121 121 122 + ATAPI_MAX_DRAIN = 16 << 10, 123 + 122 124 ATA_SHT_EMULATED = 1, 123 125 ATA_SHT_CMD_PER_LUN = 1, 124 126 ATA_SHT_THIS_ID = -1,