/* * linux/drivers/scsi/esas2r/esas2r_ioctl.c * For use with ATTO ExpressSAS R6xx SAS/SATA RAID controllers * * Copyright (c) 2001-2013 ATTO Technology, Inc. * (mailto:linuxdrivers@attotech.com) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * NO WARRANTY * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is * solely responsible for determining the appropriateness of using and * distributing the Program and assumes all risks associated with its * exercise of rights under this Agreement, including but not limited to * the risks and costs of program errors, damage to or loss of data, * programs or equipment, and unavailability or interruption of operations. * * DISCLAIMER OF LIABILITY * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA.
*/
#include <linux/bitfield.h>
#include"esas2r.h"
/* * Buffered ioctl handlers. A buffered ioctl is one which requires that we * allocate a DMA-able memory area to communicate with the firmware. In * order to prevent continually allocating and freeing consistent memory, * we will allocate a global buffer the first time we need it and re-use * it for subsequent ioctl calls that require it.
*/
if (!esas2r_fm_api(a, (struct esas2r_flash_img *)a->save_offset, rq,
&a->fm_api_sgc)) goto all_done;
/* Now wait around for it to complete. */ while (!a->fm_api_command_done)
wait_event_interruptible(a->fm_api_waiter,
a->fm_api_command_done);
all_done: if (fi == &a->firmware.header) {
memcpy(fi, a->firmware.header_buff, sizeof(struct esas2r_flash_img));
if (down_interruptible(&buffered_ioctl_semaphore)) return IOCTL_OUT_OF_RESOURCES;
/* allocate a buffer or use the existing buffer. */ if (esas2r_buffered_ioctl) { if (esas2r_buffered_ioctl_size < bi->length) { /* free the too-small buffer and get a new one */
dma_free_coherent(&a->pcid->dev,
(size_t)esas2r_buffered_ioctl_size,
esas2r_buffered_ioctl,
esas2r_buffered_ioctl_addr);
if (!(*bi->callback)(a, rq, &sgc, bi->context)) { /* completed immediately, no need to wait */
a->buffered_ioctl_done = 0; goto free_andexit_cleanly;
}
/* now wait around for it to complete. */ while (!a->buffered_ioctl_done)
wait_event_interruptible(a->buffered_ioctl_waiter,
a->buffered_ioctl_done);
free_andexit_cleanly: if (result == IOCTL_SUCCESS && bi->done_callback)
(*bi->done_callback)(a, rq, bi->done_context);
esas2r_free_request(a, rq);
exit_cleanly: if (result == IOCTL_SUCCESS)
memcpy(bi->ioctl, esas2r_buffered_ioctl, bi->length);
case CSMI_CC_FW_DOWNLOAD: case CSMI_CC_GET_RAID_INFO: case CSMI_CC_GET_RAID_CFG:
sts = CSMI_STS_BAD_CTRL_CODE; break;
case CSMI_CC_SMP_PASSTHRU: case CSMI_CC_SSP_PASSTHRU: case CSMI_CC_STP_PASSTHRU: case CSMI_CC_GET_PHY_INFO: case CSMI_CC_SET_PHY_INFO: case CSMI_CC_GET_LINK_ERRORS: case CSMI_CC_GET_SATA_SIG: case CSMI_CC_GET_CONN_INFO: case CSMI_CC_PHY_CTRL:
if (!check_lun(lun)) {
sts = CSMI_STS_NO_SCSI_ADDR; break;
}
/* make sure the device is present */
spin_lock_irqsave(&a->mem_lock, flags);
t = esas2r_targ_db_find_by_sas_addr(a, (u64 *)gsa->sas_addr);
spin_unlock_irqrestore(&a->mem_lock, flags);
if (t == NULL) {
sts = CSMI_STS_NO_SCSI_ADDR; break;
}
/* fill in the result */
*(u64 *)gda->sas_addr = t->sas_addr;
memset(gda->sas_lun, 0, sizeof(gda->sas_lun));
gda->sas_lun[1] = (u8)le32_to_cpu(rq->vrq->scsi.flags); break;
}
case CSMI_CC_TASK_MGT:
/* make sure the target is present */
t = a->targetdb + rq->target_id;
/* Tunnel an ATTO HBA IOCTL to the back end driver for processing. */ staticbool hba_ioctl_tunnel(struct esas2r_adapter *a, struct atto_ioctl *hi, struct esas2r_request *rq, struct esas2r_sg_context *sgc)
{
esas2r_sgc_init(sgc, a, rq, rq->vrq->ioctl.sge);
if (esas2r_nvram_write(a, rq, data)) { /* now wait around for it to complete. */ while (!a->nvram_command_done)
wait_event_interruptible(a->nvram_waiter,
a->nvram_command_done);
;
/* done, check the status. */ if (rq->req_stat == RS_SUCCESS)
result = 1;
} return result;
}
/* This function only cares about ATTO-specific ioctls (atto_express_ioctl) */ int esas2r_ioctl_handler(void *hostdata, unsignedint cmd, void __user *arg)
{ struct atto_express_ioctl *ioctl = NULL; struct esas2r_adapter *a; struct esas2r_request *rq;
u16 code; int err;
/* Always copy the buffer back, if only to pick up the status */
err = copy_to_user(arg, ioctl, sizeof(struct atto_express_ioctl)); if (err != 0) {
esas2r_log(ESAS2R_LOG_WARN, "ioctl_handler copy_to_user didn't copy everything (err %d, cmd %u)",
err, cmd);
kfree(ioctl);
if (!a->firmware.data) {
esas2r_debug("buffer alloc failed!"); return 0;
}
return 1;
}
/* Handle a call to read firmware. */ int esas2r_read_fw(struct esas2r_adapter *a, char *buf, long off, int count)
{
esas2r_trace_enter(); /* if the cached header is a status, simply copy it over and return. */ if (a->firmware.state == FW_STATUS_ST) { int size = min_t(int, count, sizeof(a->firmware.header));
esas2r_trace_exit();
memcpy(buf, &a->firmware.header, size);
esas2r_debug("esas2r_read_fw: STATUS size %d", size); return size;
}
/* * if the cached header is a command, do it if at * offset 0, otherwise copy the pieces.
*/
if (a->firmware.state == FW_COMMAND_ST) {
u32 length = a->firmware.header.length;
esas2r_trace_exit();
esas2r_debug("esas2r_read_fw: COMMAND length %d off %d",
length,
off);
if (off == 0) { if (a->firmware.header.action == FI_ACT_UP) { if (!allocate_fw_buffers(a, length)) return -ENOMEM;
int min_size = sizeof(struct esas2r_flash_img_v0);
a->firmware.state = FW_INVALID_ST;
/* validate the version field first */
if (count < 4
|| header->fi_version > FI_VERSION_1) {
esas2r_debug( "esas2r_write_fw: short header or invalid version"); return -EINVAL;
}
/* See if its a version 1 flash image */
if (header->fi_version == FI_VERSION_1)
min_size = sizeof(struct esas2r_flash_img);
/* If this is the start, the header must be full and valid. */ if (count < min_size) {
esas2r_debug("esas2r_write_fw: short header, aborting"); return -EINVAL;
}
/* Make sure the size is reasonable. */
length = header->length;
/* * If this is a write command, allocate memory because * we have to cache everything. otherwise, just cache * the header, because the read op will do the command.
*/
if (header->action == FI_ACT_DOWN) { if (!allocate_fw_buffers(a, length)) return -ENOMEM;
/* * Store the command, so there is context on subsequent * calls.
*/
memcpy(&a->firmware.header,
buf, sizeof(*header));
} elseif (header->action == FI_ACT_UP
|| header->action == FI_ACT_UPSZ) { /* Save the command, result will be picked up on read */
memcpy(&a->firmware.header,
buf, sizeof(*header));
/* * We only get here on a download command, regardless of offset. * the chunks written by the system need to be cached, and when * the final one arrives, issue the fmapi command.
*/
if (off + count > length)
count = length - off;
if (count > 0) {
esas2r_debug("esas2r_write_fw: off %d count %d length %d", off,
count,
length);
/* * On a full upload, the system tries sending the whole buffer. * there's nothing to do with it, so just drop it here, before * trying to copy over into unallocated memory!
*/ if (a->firmware.header.action == FI_ACT_UP) return count;
if (!a->firmware.data) {
esas2r_debug( "write: nonzero offset but no buffer available!"); return -ENOMEM;
}
memcpy(&a->firmware.data[off], buf, count);
if (length == off + count) {
do_fm_api(a,
(struct esas2r_flash_img *)a->firmware.data);
/* * Now copy the header result to be picked up by the * next read
*/
memcpy(&a->firmware.header,
a->firmware.data, sizeof(a->firmware.header));
a->firmware.state = FW_STATUS_ST;
esas2r_debug("write completed");
/* * Since the system has the data buffered, the only way * this can leak is if a root user writes a program * that writes a shorter buffer than it claims, and the * copyin fails.
*/
free_fw_buffers(a);
}
}
return count;
}
/* Callback for the completion of a VDA request. */ staticvoid vda_complete_req(struct esas2r_adapter *a, struct esas2r_request *rq)
{
a->vda_command_done = 1;
wake_up_interruptible(&a->vda_waiter);
}
/* Handle a call to read a VDA command. */ int esas2r_read_vda(struct esas2r_adapter *a, char *buf, long off, int count)
{ if (!a->vda_buffer) return -ENOMEM;
/* * Presumeably, someone has already written to the vda_buffer, * and now they are reading the node the response, so now we * will actually issue the request to the chip and reply.
*/
/* allocate a request */
rq = esas2r_alloc_request(a); if (rq == NULL) {
esas2r_debug("esas2r_read_vda: out of requests"); return -EBUSY;
}
if (count + off > VDA_MAX_BUFFER_SIZE)
count = VDA_MAX_BUFFER_SIZE - off;
if (count < 0) return 0;
memcpy(buf, a->vda_buffer + off, count);
return count;
}
/* Handle a call to write a VDA command. */ int esas2r_write_vda(struct esas2r_adapter *a, constchar *buf, long off, int count)
{ /* * allocate memory for it, if not already done. once allocated, * we will keep it around until the driver is unloaded.
*/
if (count + off > VDA_MAX_BUFFER_SIZE)
count = VDA_MAX_BUFFER_SIZE - off;
if (count < 1) return 0;
memcpy(a->vda_buffer + off, buf, count);
return count;
}
/* Callback for the completion of an FS_API request.*/ staticvoid fs_api_complete_req(struct esas2r_adapter *a, struct esas2r_request *rq)
{
a->fs_api_command_done = 1;
/* Handle a call to read firmware via FS_API. */ int esas2r_read_fs(struct esas2r_adapter *a, char *buf, long off, int count)
{ if (!a->fs_api_buffer) return -ENOMEM;
/* If another flash request is already in progress, return. */ if (mutex_lock_interruptible(&a->fs_api_mutex)) {
busy:
fs->status = ATTO_STS_OUT_OF_RSRC; return -EBUSY;
}
/* * Presumeably, someone has already written to the * fs_api_buffer, and now they are reading the node the * response, so now we will actually issue the request to the * chip and reply. Allocate a request
*/
rq = esas2r_alloc_request(a); if (rq == NULL) {
esas2r_debug("esas2r_read_fs: out of requests");
mutex_unlock(&a->fs_api_mutex); goto busy;
}
rq->comp_cb = fs_api_complete_req;
/* Set up the SGCONTEXT for to build the s/g table */
if (!esas2r_process_fs_ioctl(a, fs, rq, &sgc)) { if (fs->status == ATTO_STS_OUT_OF_RSRC)
count = -EBUSY;
goto dont_wait;
}
/* Now wait around for it to complete. */
while (!a->fs_api_command_done)
wait_event_interruptible(a->fs_api_waiter,
a->fs_api_command_done);
;
dont_wait: /* Free the request and keep going */
mutex_unlock(&a->fs_api_mutex);
esas2r_free_request(a, (struct esas2r_request *)rq);
/* Pick up possible error code from above */ if (count < 0) return count;
}
if (off > a->fs_api_buffer_size) return 0;
if (count + off > a->fs_api_buffer_size)
count = a->fs_api_buffer_size - off;
if (count < 0) return 0;
memcpy(buf, a->fs_api_buffer + off, count);
return count;
}
/* Handle a call to write firmware via FS_API. */ int esas2r_write_fs(struct esas2r_adapter *a, constchar *buf, long off, int count)
{ if (off == 0) { struct esas2r_ioctl_fs *fs = (struct esas2r_ioctl_fs *)buf;
u32 length = fs->command.length + offsetof( struct esas2r_ioctl_fs,
data);
/* * Special case, for BEGIN commands, the length field * is lying to us, so just get enough for the header.
*/
if (fs->command.command == ESAS2R_FS_CMD_BEGINW)
length = offsetof(struct esas2r_ioctl_fs, data);
/* * Beginning a command. We assume we'll get at least * enough in the first write so we can look at the * header and see how much we need to alloc.
*/
if (count < offsetof(struct esas2r_ioctl_fs, data)) return -EINVAL;
/* Allocate a buffer or use the existing buffer. */ if (a->fs_api_buffer) { if (a->fs_api_buffer_size < length) { /* Free too-small buffer and get a new one */
dma_free_coherent(&a->pcid->dev,
(size_t)a->fs_api_buffer_size,
a->fs_api_buffer,
(dma_addr_t)a->ppfs_api_buffer);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.