-/* -*- c-basic-offset: 8 -*-
- * fw-sbp2.c -- SBP2 driver (SCSI over IEEE1394)
+/*
+ * SBP2 driver (SCSI over IEEE1394)
*
* Copyright (C) 2005-2007 Kristian Hoegsberg <krh@bitplanet.net>
*
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
-/* The basic structure of this driver is based the old storage driver,
+/*
+ * The basic structure of this driver is based on the old storage driver,
* drivers/ieee1394/sbp2.c, originally written by
* James Goodwin <jamesg@filanet.com>
* with later contributions and ongoing maintenance from
#include <linux/device.h>
#include <linux/scatterlist.h>
#include <linux/dma-mapping.h>
+#include <linux/blkdev.h>
#include <linux/timer.h>
#include <scsi/scsi.h>
#include "fw-device.h"
/* I don't know why the SCSI stack doesn't define something like this... */
-typedef void (*scsi_done_fn_t) (struct scsi_cmnd *);
+typedef void (*scsi_done_fn_t)(struct scsi_cmnd *);
static const char sbp2_driver_name[] = "sbp2";
struct sbp2_device {
+ struct kref kref;
struct fw_unit *unit;
struct fw_address_handler address_handler;
struct list_head orb_list;
u32 workarounds;
int login_id;
- /* We cache these addresses and only update them once we've
+ /*
+ * We cache these addresses and only update them once we've
* logged in or reconnected to the sbp2 device. That way, any
* IO to the device will automatically fail and get retried if
* it happens in a window where the device is not ready to
- * handle it (e.g. after a bus reset but before we reconnect). */
+ * handle it (e.g. after a bus reset but before we reconnect).
+ */
int node_id;
int address_high;
int generation;
- /* Timer for flushing ORBs. */
- struct timer_list orb_timer;
-
int retries;
struct delayed_work work;
- struct Scsi_Host *scsi_host;
};
#define SBP2_MAX_SG_ELEMENT_LENGTH 0xf000
#define SBP2_STATUS_ILLEGAL_REQUEST 0x2
#define SBP2_STATUS_VENDOR_DEPENDENT 0x3
-#define status_get_orb_high(v) ((v).status & 0xffff)
-#define status_get_sbp_status(v) (((v).status >> 16) & 0xff)
-#define status_get_len(v) (((v).status >> 24) & 0x07)
-#define status_get_dead(v) (((v).status >> 27) & 0x01)
-#define status_get_response(v) (((v).status >> 28) & 0x03)
-#define status_get_source(v) (((v).status >> 30) & 0x03)
-#define status_get_orb_low(v) ((v).orb_low)
-#define status_get_data(v) ((v).data)
+#define STATUS_GET_ORB_HIGH(v) ((v).status & 0xffff)
+#define STATUS_GET_SBP_STATUS(v) (((v).status >> 16) & 0xff)
+#define STATUS_GET_LEN(v) (((v).status >> 24) & 0x07)
+#define STATUS_GET_DEAD(v) (((v).status >> 27) & 0x01)
+#define STATUS_GET_RESPONSE(v) (((v).status >> 28) & 0x03)
+#define STATUS_GET_SOURCE(v) (((v).status >> 30) & 0x03)
+#define STATUS_GET_ORB_LOW(v) ((v).orb_low)
+#define STATUS_GET_DATA(v) ((v).data)
struct sbp2_status {
u32 status;
dma_addr_t request_bus;
int rcode;
struct sbp2_pointer pointer;
- void (*callback) (struct sbp2_orb * orb, struct sbp2_status * status);
+ void (*callback)(struct sbp2_orb * orb, struct sbp2_status * status);
struct list_head link;
};
-#define management_orb_lun(v) ((v))
-#define management_orb_function(v) ((v) << 16)
-#define management_orb_reconnect(v) ((v) << 20)
-#define management_orb_exclusive ((1) << 28)
-#define management_orb_request_format(v) ((v) << 29)
-#define management_orb_notify ((1) << 31)
+#define MANAGEMENT_ORB_LUN(v) ((v))
+#define MANAGEMENT_ORB_FUNCTION(v) ((v) << 16)
+#define MANAGEMENT_ORB_RECONNECT(v) ((v) << 20)
+#define MANAGEMENT_ORB_EXCLUSIVE ((1) << 28)
+#define MANAGEMENT_ORB_REQUEST_FORMAT(v) ((v) << 29)
+#define MANAGEMENT_ORB_NOTIFY ((1) << 31)
-#define management_orb_response_length(v) ((v))
-#define management_orb_password_length(v) ((v) << 16)
+#define MANAGEMENT_ORB_RESPONSE_LENGTH(v) ((v))
+#define MANAGEMENT_ORB_PASSWORD_LENGTH(v) ((v) << 16)
struct sbp2_management_orb {
struct sbp2_orb base;
struct sbp2_status status;
};
-#define login_response_get_login_id(v) ((v).misc & 0xffff)
-#define login_response_get_length(v) (((v).misc >> 16) & 0xffff)
+#define LOGIN_RESPONSE_GET_LOGIN_ID(v) ((v).misc & 0xffff)
+#define LOGIN_RESPONSE_GET_LENGTH(v) (((v).misc >> 16) & 0xffff)
struct sbp2_login_response {
u32 misc;
struct sbp2_pointer command_block_agent;
u32 reconnect_hold;
};
-
-#define command_orb_data_size(v) ((v))
-#define command_orb_page_size(v) ((v) << 16)
-#define command_orb_page_table_present ((1) << 19)
-#define command_orb_max_payload(v) ((v) << 20)
-#define command_orb_speed(v) ((v) << 24)
-#define command_orb_direction(v) ((v) << 27)
-#define command_orb_request_format(v) ((v) << 29)
-#define command_orb_notify ((1) << 31)
+#define COMMAND_ORB_DATA_SIZE(v) ((v))
+#define COMMAND_ORB_PAGE_SIZE(v) ((v) << 16)
+#define COMMAND_ORB_PAGE_TABLE_PRESENT ((1) << 19)
+#define COMMAND_ORB_MAX_PAYLOAD(v) ((v) << 20)
+#define COMMAND_ORB_SPEED(v) ((v) << 24)
+#define COMMAND_ORB_DIRECTION(v) ((v) << 27)
+#define COMMAND_ORB_REQUEST_FORMAT(v) ((v) << 29)
+#define COMMAND_ORB_NOTIFY ((1) << 31)
struct sbp2_command_orb {
struct sbp2_orb base;
struct sbp2_pointer page_table[SG_ALL];
dma_addr_t page_table_bus;
- dma_addr_t request_buffer_bus;
};
/*
.model = ~0,
.workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
},
- /* There are iPods (2nd gen, 3rd gen) with model_id == 0, but
+
+ /*
+ * There are iPods (2nd gen, 3rd gen) with model_id == 0, but
* these iPods do not feature the read_capacity bug according
* to one report. Read_capacity behaviour as well as model_id
- * could change due to Apple-supplied firmware updates though. */
+ * could change due to Apple-supplied firmware updates though.
+ */
+
/* iPod 4th generation. */ {
.firmware_revision = 0x0a2700,
.model = 0x000021,
unsigned long flags;
if (tcode != TCODE_WRITE_BLOCK_REQUEST ||
- length == 0 || length > sizeof status) {
+ length == 0 || length > sizeof(status)) {
fw_send_response(card, request, RCODE_TYPE_ERROR);
return;
}
fw_memcpy_from_be32(&status, payload, header_size);
if (length > header_size)
memcpy(status.data, payload + 8, length - header_size);
- if (status_get_source(status) == 2 || status_get_source(status) == 3) {
+ if (STATUS_GET_SOURCE(status) == 2 || STATUS_GET_SOURCE(status) == 3) {
fw_notify("non-orb related status write, not handled\n");
fw_send_response(card, request, RCODE_COMPLETE);
return;
/* Lookup the orb corresponding to this status write. */
spin_lock_irqsave(&card->lock, flags);
list_for_each_entry(orb, &sd->orb_list, link) {
- if (status_get_orb_high(status) == 0 &&
- status_get_orb_low(status) == orb->request_bus) {
+ if (STATUS_GET_ORB_HIGH(status) == 0 &&
+ STATUS_GET_ORB_LOW(status) == orb->request_bus &&
+ orb->rcode == RCODE_COMPLETE) {
list_del(&orb->link);
break;
}
orb->pointer.high = 0;
orb->pointer.low = orb->request_bus;
- fw_memcpy_to_be32(&orb->pointer, &orb->pointer, sizeof orb->pointer);
+ fw_memcpy_to_be32(&orb->pointer, &orb->pointer, sizeof(orb->pointer));
spin_lock_irqsave(&device->card->lock, flags);
list_add_tail(&orb->link, &sd->orb_list);
spin_unlock_irqrestore(&device->card->lock, flags);
- mod_timer(&sd->orb_timer,
- jiffies + DIV_ROUND_UP(SBP2_ORB_TIMEOUT * HZ, 1000));
-
fw_send_request(device->card, &orb->t, TCODE_WRITE_BLOCK_REQUEST,
- node_id, generation,
- device->node->max_speed, offset,
- &orb->pointer, sizeof orb->pointer,
+ node_id, generation, device->max_speed, offset,
+ &orb->pointer, sizeof(orb->pointer),
complete_transaction, orb);
}
-static void sbp2_cancel_orbs(struct fw_unit *unit)
+static int sbp2_cancel_orbs(struct fw_unit *unit)
{
struct fw_device *device = fw_device(unit->device.parent);
struct sbp2_device *sd = unit->device.driver_data;
struct sbp2_orb *orb, *next;
struct list_head list;
unsigned long flags;
+ int retval = -ENOENT;
INIT_LIST_HEAD(&list);
spin_lock_irqsave(&device->card->lock, flags);
spin_unlock_irqrestore(&device->card->lock, flags);
list_for_each_entry_safe(orb, next, &list, link) {
+ retval = 0;
if (fw_cancel_transaction(device->card, &orb->t) == 0)
continue;
orb->rcode = RCODE_CANCELLED;
orb->callback(orb, NULL);
}
-}
-static void orb_timer_callback(unsigned long data)
-{
- struct sbp2_device *sd = (struct sbp2_device *)data;
-
- sbp2_cancel_orbs(sd->unit);
+ return retval;
}
static void
(struct sbp2_management_orb *)base_orb;
if (status)
- memcpy(&orb->status, status, sizeof *status);
+ memcpy(&orb->status, status, sizeof(*status));
complete(&orb->done);
}
struct sbp2_management_orb *orb;
int retval = -ENOMEM;
- orb = kzalloc(sizeof *orb, GFP_ATOMIC);
+ orb = kzalloc(sizeof(*orb), GFP_ATOMIC);
if (orb == NULL)
return -ENOMEM;
- /* The sbp2 device is going to send a block read request to
- * read out the request from host memory, so map it for
- * dma. */
+ /*
+ * The sbp2 device is going to send a block read request to
+ * read out the request from host memory, so map it for dma.
+ */
orb->base.request_bus =
dma_map_single(device->card->device, &orb->request,
- sizeof orb->request, DMA_TO_DEVICE);
- if (orb->base.request_bus == 0)
+ sizeof(orb->request), DMA_TO_DEVICE);
+ if (dma_mapping_error(orb->base.request_bus))
goto out;
orb->response_bus =
dma_map_single(device->card->device, &orb->response,
- sizeof orb->response, DMA_FROM_DEVICE);
- if (orb->response_bus == 0)
+ sizeof(orb->response), DMA_FROM_DEVICE);
+ if (dma_mapping_error(orb->response_bus))
goto out;
orb->request.response.high = 0;
orb->request.response.low = orb->response_bus;
orb->request.misc =
- management_orb_notify |
- management_orb_function(function) |
- management_orb_lun(lun);
+ MANAGEMENT_ORB_NOTIFY |
+ MANAGEMENT_ORB_FUNCTION(function) |
+ MANAGEMENT_ORB_LUN(lun);
orb->request.length =
- management_orb_response_length(sizeof orb->response);
+ MANAGEMENT_ORB_RESPONSE_LENGTH(sizeof(orb->response));
orb->request.status_fifo.high = sd->address_handler.offset >> 32;
orb->request.status_fifo.low = sd->address_handler.offset;
- /* FIXME: Yeah, ok this isn't elegant, we hardwire exclusive
+ /*
+ * FIXME: Yeah, ok this isn't elegant, we hardwire exclusive
* login and 1 second reconnect time. The reconnect setting
- * is probably fine, but the exclusive login should be an
- * option. */
+ * is probably fine, but the exclusive login should be an option.
+ */
if (function == SBP2_LOGIN_REQUEST) {
orb->request.misc |=
- management_orb_exclusive |
- management_orb_reconnect(0);
+ MANAGEMENT_ORB_EXCLUSIVE |
+ MANAGEMENT_ORB_RECONNECT(0);
}
- fw_memcpy_to_be32(&orb->request, &orb->request, sizeof orb->request);
+ fw_memcpy_to_be32(&orb->request, &orb->request, sizeof(orb->request));
init_completion(&orb->done);
orb->base.callback = complete_management_orb;
+
sbp2_send_orb(&orb->base, unit,
node_id, generation, sd->management_agent_address);
- wait_for_completion(&orb->done);
-
- /* FIXME: Handle bus reset race here. */
+ wait_for_completion_timeout(&orb->done,
+ msecs_to_jiffies(SBP2_ORB_TIMEOUT));
retval = -EIO;
- if (orb->base.rcode != RCODE_COMPLETE) {
- fw_error("management write failed, rcode 0x%02x\n",
+ if (sbp2_cancel_orbs(unit) == 0) {
+ fw_error("orb reply timed out, rcode=0x%02x\n",
orb->base.rcode);
goto out;
}
- if (orb->base.rcode == RCODE_CANCELLED) {
- fw_error("orb reply timed out, rcode=0x%02x\n",
+ if (orb->base.rcode != RCODE_COMPLETE) {
+ fw_error("management write failed, rcode 0x%02x\n",
orb->base.rcode);
goto out;
}
- if (status_get_response(orb->status) != 0 ||
- status_get_sbp_status(orb->status) != 0) {
+ if (STATUS_GET_RESPONSE(orb->status) != 0 ||
+ STATUS_GET_SBP_STATUS(orb->status) != 0) {
fw_error("error status: %d:%d\n",
- status_get_response(orb->status),
- status_get_sbp_status(orb->status));
+ STATUS_GET_RESPONSE(orb->status),
+ STATUS_GET_SBP_STATUS(orb->status));
goto out;
}
retval = 0;
out:
dma_unmap_single(device->card->device, orb->base.request_bus,
- sizeof orb->request, DMA_TO_DEVICE);
+ sizeof(orb->request), DMA_TO_DEVICE);
dma_unmap_single(device->card->device, orb->response_bus,
- sizeof orb->response, DMA_FROM_DEVICE);
+ sizeof(orb->response), DMA_FROM_DEVICE);
if (response)
fw_memcpy_from_be32(response,
- orb->response, sizeof orb->response);
+ orb->response, sizeof(orb->response));
kfree(orb);
return retval;
{
struct fw_transaction *t = data;
- fw_notify("agent reset write rcode=%d\n", rcode);
kfree(t);
}
struct fw_transaction *t;
static u32 zero;
- t = kzalloc(sizeof *t, GFP_ATOMIC);
+ t = kzalloc(sizeof(*t), GFP_ATOMIC);
if (t == NULL)
return -ENOMEM;
fw_send_request(device->card, t, TCODE_WRITE_QUADLET_REQUEST,
sd->node_id, sd->generation, SCODE_400,
sd->command_block_agent_address + SBP2_AGENT_RESET,
- &zero, sizeof zero, complete_agent_reset_write, t);
+ &zero, sizeof(zero), complete_agent_reset_write, t);
return 0;
}
-static int add_scsi_devices(struct fw_unit *unit);
-static void remove_scsi_devices(struct fw_unit *unit);
static void sbp2_reconnect(struct work_struct *work);
+static struct scsi_host_template scsi_driver_template;
+
+static void release_sbp2_device(struct kref *kref)
+{
+ struct sbp2_device *sd = container_of(kref, struct sbp2_device, kref);
+ struct Scsi_Host *host =
+ container_of((void *)sd, struct Scsi_Host, hostdata[0]);
+
+ scsi_remove_host(host);
+ sbp2_send_management_orb(sd->unit, sd->node_id, sd->generation,
+ SBP2_LOGOUT_REQUEST, sd->login_id, NULL);
+ fw_core_remove_address_handler(&sd->address_handler);
+ fw_notify("removed sbp2 unit %s\n", sd->unit->device.bus_id);
+ put_device(&sd->unit->device);
+ scsi_host_put(host);
+}
static void sbp2_login(struct work_struct *work)
{
struct sbp2_device *sd =
container_of(work, struct sbp2_device, work.work);
+ struct Scsi_Host *host =
+ container_of((void *)sd, struct Scsi_Host, hostdata[0]);
struct fw_unit *unit = sd->unit;
struct fw_device *device = fw_device(unit->device.parent);
struct sbp2_login_response response;
if (sbp2_send_management_orb(unit, node_id, generation,
SBP2_LOGIN_REQUEST, lun, &response) < 0) {
if (sd->retries++ < 5) {
- fw_error("login attempt %d for %s failed, "
- "rescheduling\n",
- sd->retries, unit->device.bus_id);
schedule_delayed_work(&sd->work, DIV_ROUND_UP(HZ, 5));
} else {
fw_error("failed to login to %s\n",
unit->device.bus_id);
- remove_scsi_devices(unit);
+ kref_put(&sd->kref, release_sbp2_device);
}
return;
}
/* Get command block agent offset and login id. */
sd->command_block_agent_address =
- ((u64) response.command_block_agent.high << 32) |
+ ((u64) (response.command_block_agent.high & 0xffff) << 32) |
response.command_block_agent.low;
- sd->login_id = login_response_get_login_id(response);
+ sd->login_id = LOGIN_RESPONSE_GET_LOGIN_ID(response);
- fw_notify("logged in to sbp2 unit %s\n", unit->device.bus_id);
- fw_notify(" - management_agent_address: 0x%012llx\n",
+ fw_notify("logged in to sbp2 unit %s (%d retries)\n",
+ unit->device.bus_id, sd->retries);
+ fw_notify(" - management_agent_address: 0x%012llx\n",
(unsigned long long) sd->management_agent_address);
fw_notify(" - command_block_agent_address: 0x%012llx\n",
(unsigned long long) sd->command_block_agent_address);
- fw_notify(" - status write address: 0x%012llx\n",
+ fw_notify(" - status write address: 0x%012llx\n",
(unsigned long long) sd->address_handler.offset);
#if 0
sbp2_set_busy_timeout(scsi_id);
#endif
- INIT_DELAYED_WORK(&sd->work, sbp2_reconnect);
+ PREPARE_DELAYED_WORK(&sd->work, sbp2_reconnect);
sbp2_agent_reset(unit);
- retval = add_scsi_devices(unit);
+ /* FIXME: Loop over luns here. */
+ lun = 0;
+ retval = scsi_add_device(host, 0, 0, lun);
if (retval < 0) {
sbp2_send_management_orb(unit, sd->node_id, sd->generation,
SBP2_LOGOUT_REQUEST, sd->login_id,
NULL);
- /* Set this back to sbp2_login so we fall back and
- * retry login on bus reset. */
- INIT_DELAYED_WORK(&sd->work, sbp2_login);
+ /*
+ * Set this back to sbp2_login so we fall back and
+ * retry login on bus reset.
+ */
+ PREPARE_DELAYED_WORK(&sd->work, sbp2_login);
}
+ kref_put(&sd->kref, release_sbp2_device);
}
static int sbp2_probe(struct device *dev)
struct fw_device *device = fw_device(unit->device.parent);
struct sbp2_device *sd;
struct fw_csr_iterator ci;
- int i, key, value;
+ struct Scsi_Host *host;
+ int i, key, value, err;
u32 model, firmware_revision;
- sd = kzalloc(sizeof *sd, GFP_KERNEL);
- if (sd == NULL)
- return -ENOMEM;
+ err = -ENOMEM;
+ host = scsi_host_alloc(&scsi_driver_template, sizeof(*sd));
+ if (host == NULL)
+ goto fail;
+ sd = (struct sbp2_device *) host->hostdata;
unit->device.driver_data = sd;
sd->unit = unit;
INIT_LIST_HEAD(&sd->orb_list);
- setup_timer(&sd->orb_timer, orb_timer_callback, (unsigned long)sd);
+ kref_init(&sd->kref);
sd->address_handler.length = 0x100;
sd->address_handler.address_callback = sbp2_status_write;
sd->address_handler.callback_data = sd;
- if (fw_core_add_address_handler(&sd->address_handler,
- &fw_high_memory_region) < 0) {
- kfree(sd);
- return -EBUSY;
- }
+ err = fw_core_add_address_handler(&sd->address_handler,
+ &fw_high_memory_region);
+ if (err < 0)
+ goto fail_host;
- if (fw_device_enable_phys_dma(device) < 0) {
- fw_core_remove_address_handler(&sd->address_handler);
- kfree(sd);
- return -EBUSY;
- }
+ err = fw_device_enable_phys_dma(device);
+ if (err < 0)
+ goto fail_address_handler;
- /* Scan unit directory to get management agent address,
+ err = scsi_add_host(host, &unit->device);
+ if (err < 0)
+ goto fail_address_handler;
+
+ /*
+ * Scan unit directory to get management agent address,
* firmware revison and model. Initialize firmware_revision
- * and model to values that wont match anything in our table. */
+ * and model to values that wont match anything in our table.
+ */
firmware_revision = 0xff000000;
model = 0xff000000;
fw_csr_iterator_init(&ci, unit->directory);
unit->device.bus_id,
sd->workarounds, firmware_revision, model);
- /* We schedule work to do the login so we can easily
- * reschedule retries. */
+ get_device(&unit->device);
+
+ /*
+ * We schedule work to do the login so we can easily
+ * reschedule retries. Always get the ref before scheduling
+ * work.
+ */
INIT_DELAYED_WORK(&sd->work, sbp2_login);
- schedule_delayed_work(&sd->work, 0);
+ if (schedule_delayed_work(&sd->work, 0))
+ kref_get(&sd->kref);
return 0;
+
+ fail_address_handler:
+ fw_core_remove_address_handler(&sd->address_handler);
+ fail_host:
+ scsi_host_put(host);
+ fail:
+ return err;
}
static int sbp2_remove(struct device *dev)
struct fw_unit *unit = fw_unit(dev);
struct sbp2_device *sd = unit->device.driver_data;
- sbp2_send_management_orb(unit, sd->node_id, sd->generation,
- SBP2_LOGOUT_REQUEST, sd->login_id, NULL);
-
- remove_scsi_devices(unit);
- del_timer_sync(&sd->orb_timer);
-
- fw_core_remove_address_handler(&sd->address_handler);
- kfree(sd);
-
- fw_notify("removed sbp2 unit %s\n", dev->bus_id);
+ kref_put(&sd->kref, release_sbp2_device);
return 0;
}
if (sbp2_send_management_orb(unit, node_id, generation,
SBP2_RECONNECT_REQUEST,
sd->login_id, NULL) < 0) {
- if (sd->retries++ < 5) {
- fw_error("reconnect attempt %d for %s failed, "
- "rescheduling\n",
- sd->retries, unit->device.bus_id);
- } else {
+ if (sd->retries++ >= 5) {
fw_error("failed to reconnect to %s\n",
unit->device.bus_id);
/* Fall back and try to log in again. */
sd->retries = 0;
- INIT_DELAYED_WORK(&sd->work, sbp2_login);
+ PREPARE_DELAYED_WORK(&sd->work, sbp2_login);
}
schedule_delayed_work(&sd->work, DIV_ROUND_UP(HZ, 5));
return;
sd->node_id = node_id;
sd->address_high = local_node_id << 16;
- fw_notify("reconnected to unit %s\n", unit->device.bus_id);
+ fw_notify("reconnected to unit %s (%d retries)\n",
+ unit->device.bus_id, sd->retries);
sbp2_agent_reset(unit);
sbp2_cancel_orbs(unit);
+ kref_put(&sd->kref, release_sbp2_device);
}
static void sbp2_update(struct fw_unit *unit)
sd->retries = 0;
fw_device_enable_phys_dma(device);
- schedule_delayed_work(&sd->work, 0);
+ if (schedule_delayed_work(&sd->work, 0))
+ kref_get(&sd->kref);
}
#define SBP2_UNIT_SPEC_ID_ENTRY 0x0000609e
.id_table = sbp2_id_table,
};
-static unsigned int sbp2_status_to_sense_data(u8 * sbp2_status, u8 * sense_data)
+static unsigned int
+sbp2_status_to_sense_data(u8 *sbp2_status, u8 *sense_data)
{
+ int sam_status;
+
sense_data[0] = 0x70;
sense_data[1] = 0x0;
sense_data[2] = sbp2_status[1];
sense_data[14] = sbp2_status[12];
sense_data[15] = sbp2_status[13];
- switch (sbp2_status[0] & 0x3f) {
- case SAM_STAT_GOOD:
- return DID_OK;
+ sam_status = sbp2_status[0] & 0x3f;
+ switch (sam_status) {
+ case SAM_STAT_GOOD:
case SAM_STAT_CHECK_CONDITION:
- /* return CHECK_CONDITION << 1 | DID_OK << 16; */
- return DID_OK;
-
- case SAM_STAT_BUSY:
- return DID_BUS_BUSY;
-
case SAM_STAT_CONDITION_MET:
+ case SAM_STAT_BUSY:
case SAM_STAT_RESERVATION_CONFLICT:
case SAM_STAT_COMMAND_TERMINATED:
+ return DID_OK << 16 | sam_status;
+
default:
- return DID_ERROR;
+ return DID_ERROR << 16;
}
}
int result;
if (status != NULL) {
- if (status_get_dead(*status)) {
- fw_notify("agent died, issuing agent reset\n");
+ if (STATUS_GET_DEAD(*status))
sbp2_agent_reset(unit);
- }
- switch (status_get_response(*status)) {
+ switch (STATUS_GET_RESPONSE(*status)) {
case SBP2_STATUS_REQUEST_COMPLETE:
- result = DID_OK;
+ result = DID_OK << 16;
break;
case SBP2_STATUS_TRANSPORT_FAILURE:
- result = DID_BUS_BUSY;
+ result = DID_BUS_BUSY << 16;
break;
case SBP2_STATUS_ILLEGAL_REQUEST:
case SBP2_STATUS_VENDOR_DEPENDENT:
default:
- result = DID_ERROR;
+ result = DID_ERROR << 16;
break;
}
- if (result == DID_OK && status_get_len(*status) > 1)
- result = sbp2_status_to_sense_data(status_get_data(*status),
+ if (result == DID_OK << 16 && STATUS_GET_LEN(*status) > 1)
+ result = sbp2_status_to_sense_data(STATUS_GET_DATA(*status),
orb->cmd->sense_buffer);
} else {
- /* If the orb completes with status == NULL, something
+ /*
+ * If the orb completes with status == NULL, something
* went wrong, typically a bus reset happened mid-orb
- * or when sending the write (less likely). */
- fw_notify("no command orb status, rcode=%d\n",
- orb->base.rcode);
- result = DID_BUS_BUSY;
+ * or when sending the write (less likely).
+ */
+ result = DID_BUS_BUSY << 16;
}
dma_unmap_single(device->card->device, orb->base.request_bus,
- sizeof orb->request, DMA_TO_DEVICE);
+ sizeof(orb->request), DMA_TO_DEVICE);
if (orb->cmd->use_sg > 0) {
sg = (struct scatterlist *)orb->cmd->request_buffer;
if (orb->page_table_bus != 0)
dma_unmap_single(device->card->device, orb->page_table_bus,
- sizeof orb->page_table_bus, DMA_TO_DEVICE);
-
- if (orb->request_buffer_bus != 0)
- dma_unmap_single(device->card->device, orb->request_buffer_bus,
- sizeof orb->request_buffer_bus,
- DMA_FROM_DEVICE);
+ sizeof(orb->page_table_bus), DMA_TO_DEVICE);
- orb->cmd->result = result << 16;
+ orb->cmd->result = result;
orb->done(orb->cmd);
-
kfree(orb);
}
-static void sbp2_command_orb_map_scatterlist(struct sbp2_command_orb *orb)
+static int sbp2_command_orb_map_scatterlist(struct sbp2_command_orb *orb)
{
- struct fw_unit *unit =
- (struct fw_unit *)orb->cmd->device->host->hostdata[0];
+ struct sbp2_device *sd =
+ (struct sbp2_device *)orb->cmd->device->host->hostdata;
+ struct fw_unit *unit = sd->unit;
struct fw_device *device = fw_device(unit->device.parent);
- struct sbp2_device *sd = unit->device.driver_data;
struct scatterlist *sg;
int sg_len, l, i, j, count;
size_t size;
sg = (struct scatterlist *)orb->cmd->request_buffer;
count = dma_map_sg(device->card->device, sg, orb->cmd->use_sg,
orb->cmd->sc_data_direction);
+ if (count == 0)
+ goto fail;
- /* Handle the special case where there is only one element in
+ /*
+ * Handle the special case where there is only one element in
* the scatter list by converting it to an immediate block
* request. This is also a workaround for broken devices such
* as the second generation iPod which doesn't support page
- * tables. */
+ * tables.
+ */
if (count == 1 && sg_dma_len(sg) < SBP2_MAX_SG_ELEMENT_LENGTH) {
orb->request.data_descriptor.high = sd->address_high;
orb->request.data_descriptor.low = sg_dma_address(sg);
orb->request.misc |=
- command_orb_data_size(sg_dma_len(sg));
- return;
+ COMMAND_ORB_DATA_SIZE(sg_dma_len(sg));
+ return 0;
}
- /* Convert the scatterlist to an sbp2 page table. If any
- * scatterlist entries are too big for sbp2 we split the as we go. */
+ /*
+ * Convert the scatterlist to an sbp2 page table. If any
+ * scatterlist entries are too big for sbp2, we split them as we
+ * go. Even if we ask the block I/O layer to not give us sg
+ * elements larger than 65535 bytes, some IOMMUs may merge sg elements
+ * during DMA mapping, and Linux currently doesn't prevent this.
+ */
for (i = 0, j = 0; i < count; i++) {
sg_len = sg_dma_len(sg + i);
sg_addr = sg_dma_address(sg + i);
}
}
- size = sizeof orb->page_table[0] * j;
+ size = sizeof(orb->page_table[0]) * j;
- /* The data_descriptor pointer is the one case where we need
+ /*
+ * The data_descriptor pointer is the one case where we need
* to fill in the node ID part of the address. All other
* pointers assume that the data referenced reside on the
* initiator (i.e. us), but data_descriptor can refer to data
- * on other nodes so we need to put our ID in descriptor.high. */
+ * on other nodes so we need to put our ID in descriptor.high.
+ */
orb->page_table_bus =
dma_map_single(device->card->device, orb->page_table,
size, DMA_TO_DEVICE);
+ if (dma_mapping_error(orb->page_table_bus))
+ goto fail_page_table;
orb->request.data_descriptor.high = sd->address_high;
orb->request.data_descriptor.low = orb->page_table_bus;
orb->request.misc |=
- command_orb_page_table_present |
- command_orb_data_size(j);
+ COMMAND_ORB_PAGE_TABLE_PRESENT |
+ COMMAND_ORB_DATA_SIZE(j);
fw_memcpy_to_be32(orb->page_table, orb->page_table, size);
-}
-static void sbp2_command_orb_map_buffer(struct sbp2_command_orb *orb)
-{
- struct fw_unit *unit =
- (struct fw_unit *)orb->cmd->device->host->hostdata[0];
- struct fw_device *device = fw_device(unit->device.parent);
- struct sbp2_device *sd = unit->device.driver_data;
-
- /* As for map_scatterlist, we need to fill in the high bits of
- * the data_descriptor pointer. */
+ return 0;
- orb->request_buffer_bus =
- dma_map_single(device->card->device,
- orb->cmd->request_buffer,
- orb->cmd->request_bufflen,
- orb->cmd->sc_data_direction);
- orb->request.data_descriptor.high = sd->address_high;
- orb->request.data_descriptor.low = orb->request_buffer_bus;
- orb->request.misc |=
- command_orb_data_size(orb->cmd->request_bufflen);
+ fail_page_table:
+ dma_unmap_sg(device->card->device, sg, orb->cmd->use_sg,
+ orb->cmd->sc_data_direction);
+ fail:
+ return -ENOMEM;
}
/* SCSI stack integration */
static int sbp2_scsi_queuecommand(struct scsi_cmnd *cmd, scsi_done_fn_t done)
{
- struct fw_unit *unit = (struct fw_unit *)cmd->device->host->hostdata[0];
+ struct sbp2_device *sd =
+ (struct sbp2_device *)cmd->device->host->hostdata;
+ struct fw_unit *unit = sd->unit;
struct fw_device *device = fw_device(unit->device.parent);
- struct sbp2_device *sd = unit->device.driver_data;
struct sbp2_command_orb *orb;
- /* Bidirectional commands are not yet implemented, and unknown
- * transfer direction not handled. */
+ /*
+ * Bidirectional commands are not yet implemented, and unknown
+ * transfer direction not handled.
+ */
if (cmd->sc_data_direction == DMA_BIDIRECTIONAL) {
- fw_error("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
+ fw_error("Can't handle DMA_BIDIRECTIONAL, rejecting command\n");
cmd->result = DID_ERROR << 16;
done(cmd);
return 0;
}
- orb = kzalloc(sizeof *orb, GFP_ATOMIC);
+ orb = kzalloc(sizeof(*orb), GFP_ATOMIC);
if (orb == NULL) {
fw_notify("failed to alloc orb\n");
- cmd->result = DID_NO_CONNECT << 16;
- done(cmd);
- return 0;
+ goto fail_alloc;
}
+ /* Initialize rcode to something not RCODE_COMPLETE. */
+ orb->base.rcode = -1;
orb->base.request_bus =
dma_map_single(device->card->device, &orb->request,
- sizeof orb->request, DMA_TO_DEVICE);
+ sizeof(orb->request), DMA_TO_DEVICE);
+ if (dma_mapping_error(orb->base.request_bus))
+ goto fail_mapping;
orb->unit = unit;
orb->done = done;
orb->request.next.high = SBP2_ORB_NULL;
orb->request.next.low = 0x0;
- /* At speed 100 we can do 512 bytes per packet, at speed 200,
+ /*
+ * At speed 100 we can do 512 bytes per packet, at speed 200,
* 1024 bytes per packet etc. The SBP-2 max_payload field
* specifies the max payload size as 2 ^ (max_payload + 2), so
- * if we set this to max_speed + 7, we get the right value. */
+ * if we set this to max_speed + 7, we get the right value.
+ */
orb->request.misc =
- command_orb_max_payload(device->node->max_speed + 7) |
- command_orb_speed(device->node->max_speed) |
- command_orb_notify;
+ COMMAND_ORB_MAX_PAYLOAD(device->max_speed + 7) |
+ COMMAND_ORB_SPEED(device->max_speed) |
+ COMMAND_ORB_NOTIFY;
if (cmd->sc_data_direction == DMA_FROM_DEVICE)
orb->request.misc |=
- command_orb_direction(SBP2_DIRECTION_FROM_MEDIA);
+ COMMAND_ORB_DIRECTION(SBP2_DIRECTION_FROM_MEDIA);
else if (cmd->sc_data_direction == DMA_TO_DEVICE)
orb->request.misc |=
- command_orb_direction(SBP2_DIRECTION_TO_MEDIA);
-
- if (cmd->use_sg) {
- sbp2_command_orb_map_scatterlist(orb);
- } else if (cmd->request_bufflen > SBP2_MAX_SG_ELEMENT_LENGTH) {
- /* FIXME: Need to split this into a sg list... but
- * could we get the scsi or blk layer to do that by
- * reporting our max supported block size? */
- fw_error("command > 64k\n");
- cmd->result = DID_ERROR << 16;
- done(cmd);
- return 0;
- } else if (cmd->request_bufflen > 0) {
- sbp2_command_orb_map_buffer(orb);
- }
+ COMMAND_ORB_DIRECTION(SBP2_DIRECTION_TO_MEDIA);
+
+ if (cmd->use_sg && sbp2_command_orb_map_scatterlist(orb) < 0)
+ goto fail_map_payload;
- fw_memcpy_to_be32(&orb->request, &orb->request, sizeof orb->request);
+ fw_memcpy_to_be32(&orb->request, &orb->request, sizeof(orb->request));
memset(orb->request.command_block,
- 0, sizeof orb->request.command_block);
+ 0, sizeof(orb->request.command_block));
memcpy(orb->request.command_block, cmd->cmnd, COMMAND_SIZE(*cmd->cmnd));
orb->base.callback = complete_command_orb;
sd->command_block_agent_address + SBP2_ORB_POINTER);
return 0;
+
+ fail_map_payload:
+ dma_unmap_single(device->card->device, orb->base.request_bus,
+ sizeof(orb->request), DMA_TO_DEVICE);
+ fail_mapping:
+ kfree(orb);
+ fail_alloc:
+ return SCSI_MLQUEUE_HOST_BUSY;
}
static int sbp2_scsi_slave_alloc(struct scsi_device *sdev)
{
- struct fw_unit *unit = (struct fw_unit *)sdev->host->hostdata[0];
- struct sbp2_device *sd = unit->device.driver_data;
+ struct sbp2_device *sd = (struct sbp2_device *)sdev->host->hostdata;
sdev->allow_restart = 1;
static int sbp2_scsi_slave_configure(struct scsi_device *sdev)
{
- struct fw_unit *unit = (struct fw_unit *)sdev->host->hostdata[0];
- struct sbp2_device *sd = unit->device.driver_data;
+ struct sbp2_device *sd = (struct sbp2_device *)sdev->host->hostdata;
+ struct fw_unit *unit = sd->unit;
sdev->use_10_for_rw = 1;
fw_notify("setting fix_capacity for %s\n", unit->device.bus_id);
sdev->fix_capacity = 1;
}
-
+ if (sd->workarounds & SBP2_WORKAROUND_128K_MAX_TRANS)
+ blk_queue_max_sectors(sdev->request_queue, 128 * 1024 / 512);
return 0;
}
*/
static int sbp2_scsi_abort(struct scsi_cmnd *cmd)
{
- struct fw_unit *unit = (struct fw_unit *)cmd->device->host->hostdata[0];
+ struct sbp2_device *sd =
+ (struct sbp2_device *)cmd->device->host->hostdata;
+ struct fw_unit *unit = sd->unit;
fw_notify("sbp2_scsi_abort\n");
-
+ sbp2_agent_reset(unit);
sbp2_cancel_orbs(unit);
return SUCCESS;
}
+/*
+ * Format of /sys/bus/scsi/devices/.../ieee1394_id:
+ * u64 EUI-64 : u24 directory_ID : u16 LUN (all printed in hexadecimal)
+ *
+ * This is the concatenation of target port identifier and logical unit
+ * identifier as per SAM-2...SAM-4 annex A.
+ */
+static ssize_t
+sbp2_sysfs_ieee1394_id_show(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ struct scsi_device *sdev = to_scsi_device(dev);
+ struct sbp2_device *sd;
+ struct fw_unit *unit;
+ struct fw_device *device;
+ u32 directory_id;
+ struct fw_csr_iterator ci;
+ int key, value, lun;
+
+ if (!sdev)
+ return 0;
+ sd = (struct sbp2_device *)sdev->host->hostdata;
+ unit = sd->unit;
+ device = fw_device(unit->device.parent);
+
+ /* implicit directory ID */
+ directory_id = ((unit->directory - device->config_rom) * 4
+ + CSR_CONFIG_ROM) & 0xffffff;
+
+ /* explicit directory ID, overrides implicit ID if present */
+ fw_csr_iterator_init(&ci, unit->directory);
+ while (fw_csr_iterator_next(&ci, &key, &value))
+ if (key == CSR_DIRECTORY_ID) {
+ directory_id = value;
+ break;
+ }
+
+ /* FIXME: Make this work for multi-lun devices. */
+ lun = 0;
+
+ return sprintf(buf, "%08x%08x:%06x:%04x\n",
+ device->config_rom[3], device->config_rom[4],
+ directory_id, lun);
+}
+
+static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
+
+static struct device_attribute *sbp2_scsi_sysfs_attrs[] = {
+ &dev_attr_ieee1394_id,
+ NULL
+};
+
static struct scsi_host_template scsi_driver_template = {
.module = THIS_MODULE,
.name = "SBP-2 IEEE-1394",
.use_clustering = ENABLE_CLUSTERING,
.cmd_per_lun = 1,
.can_queue = 1,
+ .sdev_attrs = sbp2_scsi_sysfs_attrs,
};
-static int add_scsi_devices(struct fw_unit *unit)
-{
- struct sbp2_device *sd = unit->device.driver_data;
- int retval, lun;
-
- if (sd->scsi_host != NULL)
- return 0;
-
- sd->scsi_host = scsi_host_alloc(&scsi_driver_template,
- sizeof(unsigned long));
- if (sd->scsi_host == NULL) {
- fw_error("failed to register scsi host\n");
- return -1;
- }
-
- sd->scsi_host->hostdata[0] = (unsigned long)unit;
- retval = scsi_add_host(sd->scsi_host, &unit->device);
- if (retval < 0) {
- fw_error("failed to add scsi host\n");
- scsi_host_put(sd->scsi_host);
- return retval;
- }
-
- /* FIXME: Loop over luns here. */
- lun = 0;
- retval = scsi_add_device(sd->scsi_host, 0, 0, lun);
- if (retval < 0) {
- fw_error("failed to add scsi device\n");
- scsi_remove_host(sd->scsi_host);
- scsi_host_put(sd->scsi_host);
- return retval;
- }
-
- return 0;
-}
-
-static void remove_scsi_devices(struct fw_unit *unit)
-{
- struct sbp2_device *sd = unit->device.driver_data;
-
- if (sd->scsi_host != NULL) {
- scsi_remove_host(sd->scsi_host);
- scsi_host_put(sd->scsi_host);
- }
- sd->scsi_host = NULL;
-}
-
MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
MODULE_DESCRIPTION("SCSI over IEEE1394");
MODULE_LICENSE("GPL");
MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
+/* Provide a module alias so root-on-sbp2 initrds don't break. */
+#ifndef CONFIG_IEEE1394_SBP2_MODULE
+MODULE_ALIAS("sbp2");
+#endif
+
static int __init sbp2_init(void)
{
return driver_register(&sbp2_driver.driver);