/* RETURNS: Number of completed commands on success, -errno otherwise.  */
int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active)
/* RETURNS: Number of completed commands on success, -errno otherwise.  */
int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active)
{
	int nr_done = 0;
	u32 done_mask;
	done_mask = ap->qc_active ^ qc_active;
	if (unlikely(done_mask & qc_active)) {
		ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
				"(%08x->%08x)\n", ap->qc_active, qc_active);
		return -EINVAL;
	}
	while (done_mask) {
		struct ata_queued_cmd *qc;
		unsigned int tag = __ffs(done_mask);
		qc = ata_qc_from_tag(ap, tag);
		if (qc) {
			ata_qc_complete(qc);
			nr_done++;
		}
		done_mask &= ~(1 << tag);
	}
	return nr_done;
}