From patchwork Tue Jul 23 20:38:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Patchwork-Id: 813993 Delivered-To: patch@linaro.org Received: by 2002:adf:f288:0:b0:367:895a:4699 with SMTP id k8csp2490021wro; Tue, 23 Jul 2024 13:43:43 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUhdtrfZ2oyUc6ENOAqeba4y1REMbAvhS8Dm0p2SpVsrKgKMKGhYAgi+RW4vmOLwNlesM5vwmLGrMeNPZFrX2nY X-Google-Smtp-Source: AGHT+IHfgqLD4HZyyYSiQXF9oHQ8680ZCuHXz8p4AfkH1OWuBC10A7cTxilg5fjNe/ADiNNlOUHv X-Received: by 2002:a05:620a:390c:b0:79f:11d7:8175 with SMTP id af79cd13be357-7a1cbcfcecbmr19501685a.32.1721767422773; Tue, 23 Jul 2024 13:43:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1721767422; cv=none; d=google.com; s=arc-20160816; b=CViVeODZpmuOc2jy1gXyRNTIeGfOjEgFQd9kSM3U8ed3imgd1MkKvwfuLMhxe8m6cK Q2m2h2mhCCcI4lsxN7Uh7tsHfuaRQpp6g6C72n+AOL04gNCqn3Q4mSGLU241thyHZany s3S+pzrnC8xkhxgNt4ZSPdX0gmJHNv/bx/8lGfGVlmPWjyzaFFFpOm8gTnvbkDHNOlPI tDA2QIg8crWbGFS7tzBsk/yDnpkfbsplPWH5+ummF0lKpQFDRz40PXsp3nEdMcbaQBE4 AvIiPnUqKmm+OhZSC/M0O8NDBenVpfbDvvtJEjCoF4ohkeBuOrPc1p7wRClzJ3hQSIt9 MZZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=T1Rd8JLREenPY7uD+H493cwaNhRJxArDOSc9CQhnapI=; fh=kpPLvM4LppkMl/YKImZ3Ct1+jOKnps+13Iso3OkL16s=; b=jjDfk+0bGG5R8R5soaiCSgMJXWxuaUqRiN8+kXIJxFIK3eQq7POG945O82B8G1apSB BaByQNNhVkIsQeeUPR0Ckn0cgJAPxXOuoNWD/yoUEPoRnNbSBN4n4G5p8HvGtYEPU9pr 2a1JFPrtxtiTDe/okYkUNaKx4XZt4K+45R/95FRC4DcKxjhSngx1pCdHlJnyL3i0tLbi HmlAjMC+h5qYJrI84sKcVZZwm1gP0lv/F23VeMuzWB8bGZBh2bfhUtJjs9JvBPAzVvT7 AJnBSfc39KgUNbIEsyE25LFnV4lIzXGpViPsSRXlX9GlzeaLHL29YCFclyr1i3NHh1+1 KpGA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=wBRBmOHi; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id af79cd13be357-7a19906fdeasi1069019885a.485.2024.07.23.13.43.42 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 23 Jul 2024 13:43:42 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=wBRBmOHi; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sWMKN-00013C-72; Tue, 23 Jul 2024 16:41:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sWMK3-00087D-Br for qemu-devel@nongnu.org; Tue, 23 Jul 2024 16:41:24 -0400 Received: from mail-wm1-x332.google.com ([2a00:1450:4864:20::332]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sWMK0-0004WL-K9 for qemu-devel@nongnu.org; Tue, 23 Jul 2024 16:41:22 -0400 Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-427db004e36so29717495e9.3 for ; Tue, 23 Jul 2024 13:41:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1721767277; x=1722372077; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=T1Rd8JLREenPY7uD+H493cwaNhRJxArDOSc9CQhnapI=; b=wBRBmOHiukq57BhbTTUKFtfQhk5rj3IlB9SBzreTSsHHLD34Fs2z2SZEupTFL8hjcO cuiuMaRHPOMyF1LFGdYjtLLstqxamaRYsbcVyeOlOtyvaK2mem1vwL7JJtbMzg3niEf1 DrAeAVirHjtGAnVrrDLElmT1trzEVW4yP4pDXxajPqaWtz90PvxPlKvAhwvEJ9HJr8GJ tzcA1rfyPPMfkmEhnnDBlBTKa4sNgXBj6c8z7TC1DlkmPB7BtmQhvbYYgEux5D/AXsCf YkGPtUuOp2GhymioSTZ6xiIKXJH08EHxvXPspgYbAka4QhQc3peP837cck7dCui+XvvN Q11g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721767277; x=1722372077; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=T1Rd8JLREenPY7uD+H493cwaNhRJxArDOSc9CQhnapI=; b=S3d34h+g2p/O99EhO2sanr2YBUQ/p2x6GwOaEAJ7jXIx8bSofvKWbTrDiL2MlIxtgl YSefZb1EfjlwBv4phHxDG91PEBONBlEUhFINARhDD7wWUuz2GmNOCaU+r2ZiBI4/K2No JTqJ2ZFjslTyzuZDniiHAbZWqmmNSKpZq3jAR8E5PO7XpsKQBFV6nukYgvpCQRNqVYQJ dg1QnyF5asqJ5DwDJG3NEuiyMAnoXHWsq4HcyU/uwwaaZP4PjBllPjJbvtco5AZOom4S CoNThRMA+g97WeXIvufJX6Vbd/H7xQX9HgMxu/SjOvUDpYsRqRgJN4x6cUlIAkFiKoxx cUWA== X-Gm-Message-State: AOJu0Yzb/O8EVl965ZvNyIMgClpY5FDrOxoPgCIHR1tcxWNAGjUeRH2m Hd8X0tKJcPqtzf4ZML2eW75Mm6Vtbytg7faTlE7QB8eLqGDau2lsXiif5pF/lSGG8AZsV2fXp5P HwhY= X-Received: by 2002:a05:600c:4694:b0:426:6688:2421 with SMTP id 5b1f17b1804b1-427f9554a96mr119885e9.11.1721767277493; Tue, 23 Jul 2024 13:41:17 -0700 (PDT) Received: from localhost.localdomain (vbo91-h01-176-184-50-4.dsl.sta.abo.bbox.fr. [176.184.50.4]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-427f9398602sm510395e9.26.2024.07.23.13.41.16 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 23 Jul 2024 13:41:17 -0700 (PDT) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Mark Cave-Ayland , Pierrick Bouvier Subject: [PULL 25/28] util/fifo8: Expose fifo8_pop_buf() Date: Tue, 23 Jul 2024 22:38:52 +0200 Message-ID: <20240723203855.65033-26-philmd@linaro.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240723203855.65033-1-philmd@linaro.org> References: <20240723203855.65033-1-philmd@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::332; envelope-from=philmd@linaro.org; helo=mail-wm1-x332.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Extract fifo8_pop_buf() from hw/scsi/esp.c and expose it as part of the API. This function takes care of non-contiguous (wrapped) FIFO buffer (which is an implementation detail). Suggested-by: Mark Cave-Ayland Signed-off-by: Philippe Mathieu-Daudé Reviewed-by: Pierrick Bouvier Reviewed-by: Mark Cave-Ayland Message-Id: <20240722160745.67904-7-philmd@linaro.org> --- include/qemu/fifo8.h | 16 ++++++++++++++++ hw/scsi/esp.c | 36 +++--------------------------------- util/fifo8.c | 29 +++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 33 deletions(-) diff --git a/include/qemu/fifo8.h b/include/qemu/fifo8.h index a30220c8e96..bca6da306f7 100644 --- a/include/qemu/fifo8.h +++ b/include/qemu/fifo8.h @@ -62,12 +62,28 @@ void fifo8_push_all(Fifo8 *fifo, const uint8_t *data, uint32_t num); */ uint8_t fifo8_pop(Fifo8 *fifo); +/** + * fifo8_pop_buf: + * @fifo: FIFO to pop from + * @dest: the buffer to write the data into (can be NULL) + * @destlen: size of @dest and maximum number of bytes to pop + * + * Pop a number of elements from the FIFO up to a maximum of @destlen. + * The popped data is copied into the @dest buffer. + * Care is taken when the data wraps around in the ring buffer. + * + * Returns: number of bytes popped. + */ +uint32_t fifo8_pop_buf(Fifo8 *fifo, uint8_t *dest, uint32_t destlen); + /** * fifo8_pop_bufptr: * @fifo: FIFO to pop from * @max: maximum number of bytes to pop * @numptr: pointer filled with number of bytes returned (can be NULL) * + * New code should prefer to use fifo8_pop_buf() instead of fifo8_pop_bufptr(). + * * Pop a number of elements from the FIFO up to a maximum of @max. The buffer * containing the popped data is returned. This buffer points directly into * the internal FIFO backing store and data (without checking for overflow!) diff --git a/hw/scsi/esp.c b/hw/scsi/esp.c index 7e9657e9c30..04c9a557b69 100644 --- a/hw/scsi/esp.c +++ b/hw/scsi/esp.c @@ -197,39 +197,9 @@ static uint8_t esp_fifo_pop(ESPState *s) return val; } -static uint32_t esp_fifo8_pop_buf(Fifo8 *fifo, uint8_t *dest, int maxlen) -{ - const uint8_t *buf; - uint32_t n, n2; - int len; - - if (maxlen == 0) { - return 0; - } - - len = maxlen; - buf = fifo8_pop_bufptr(fifo, len, &n); - if (dest) { - memcpy(dest, buf, n); - } - - /* Add FIFO wraparound if needed */ - len -= n; - len = MIN(len, fifo8_num_used(fifo)); - if (len) { - buf = fifo8_pop_bufptr(fifo, len, &n2); - if (dest) { - memcpy(&dest[n], buf, n2); - } - n += n2; - } - - return n; -} - static uint32_t esp_fifo_pop_buf(ESPState *s, uint8_t *dest, int maxlen) { - uint32_t len = esp_fifo8_pop_buf(&s->fifo, dest, maxlen); + uint32_t len = fifo8_pop_buf(&s->fifo, dest, maxlen); esp_update_drq(s); return len; @@ -335,7 +305,7 @@ static void do_command_phase(ESPState *s) if (!cmdlen || !s->current_dev) { return; } - esp_fifo8_pop_buf(&s->cmdfifo, buf, cmdlen); + fifo8_pop_buf(&s->cmdfifo, buf, cmdlen); current_lun = scsi_device_find(&s->bus, 0, s->current_dev->id, s->lun); if (!current_lun) { @@ -381,7 +351,7 @@ static void do_message_phase(ESPState *s) /* Ignore extended messages for now */ if (s->cmdfifo_cdb_offset) { int len = MIN(s->cmdfifo_cdb_offset, fifo8_num_used(&s->cmdfifo)); - esp_fifo8_pop_buf(&s->cmdfifo, NULL, len); + fifo8_pop_buf(&s->cmdfifo, NULL, len); s->cmdfifo_cdb_offset = 0; } } diff --git a/util/fifo8.c b/util/fifo8.c index 5bbb6150b60..a250ea9f804 100644 --- a/util/fifo8.c +++ b/util/fifo8.c @@ -102,6 +102,35 @@ const uint8_t *fifo8_pop_bufptr(Fifo8 *fifo, uint32_t max, uint32_t *numptr) return fifo8_peekpop_buf(fifo, max, numptr, true); } +uint32_t fifo8_pop_buf(Fifo8 *fifo, uint8_t *dest, uint32_t destlen) +{ + const uint8_t *buf; + uint32_t n1, n2 = 0; + uint32_t len; + + if (destlen == 0) { + return 0; + } + + len = destlen; + buf = fifo8_pop_bufptr(fifo, len, &n1); + if (dest) { + memcpy(dest, buf, n1); + } + + /* Add FIFO wraparound if needed */ + len -= n1; + len = MIN(len, fifo8_num_used(fifo)); + if (len) { + buf = fifo8_pop_bufptr(fifo, len, &n2); + if (dest) { + memcpy(&dest[n1], buf, n2); + } + } + + return n1 + n2; +} + bool fifo8_is_empty(Fifo8 *fifo) { return (fifo->num == 0);