linux/drivers/firewire/sbp2.c

// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * SBP2 driver (SCSI over IEEE1394)
 *
 * Copyright (C) 2005-2007  Kristian Hoegsberg <[email protected]>
 */

/*
 * The basic structure of this driver is based on the old storage driver,
 * drivers/ieee1394/sbp2.c, originally written by
 *     James Goodwin <[email protected]>
 * with later contributions and ongoing maintenance from
 *     Ben Collins <[email protected]>,
 *     Stefan Richter <[email protected]>
 * and many others.
 */

#include <linux/blkdev.h>
#include <linux/bug.h>
#include <linux/completion.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/dma-mapping.h>
#include <linux/firewire.h>
#include <linux/firewire-constants.h>
#include <linux/init.h>
#include <linux/jiffies.h>
#include <linux/kernel.h>
#include <linux/kref.h>
#include <linux/list.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/scatterlist.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/string.h>
#include <linux/stringify.h>
#include <linux/workqueue.h>

#include <asm/byteorder.h>

#include <scsi/scsi.h>
#include <scsi/scsi_cmnd.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi_host.h>

/*
 * So far only bridges from Oxford Semiconductor are known to support
 * concurrent logins. Depending on firmware, four or two concurrent logins
 * are possible on OXFW911 and newer Oxsemi bridges.
 *
 * Concurrent logins are useful together with cluster filesystems.
 */
static bool sbp2_param_exclusive_login =;
module_param_named(exclusive_login, sbp2_param_exclusive_login, bool, 0644);
MODULE_PARM_DESC();

/*
 * Flags for firmware oddities
 *
 * - 128kB max transfer
 *   Limit transfer size. Necessary for some old bridges.
 *
 * - 36 byte inquiry
 *   When scsi_mod probes the device, let the inquiry command look like that
 *   from MS Windows.
 *
 * - skip mode page 8
 *   Suppress sending of mode_sense for mode page 8 if the device pretends to
 *   support the SCSI Primary Block commands instead of Reduced Block Commands.
 *
 * - fix capacity
 *   Tell sd_mod to correct the last sector number reported by read_capacity.
 *   Avoids access beyond actual disk limits on devices with an off-by-one bug.
 *   Don't use this with devices which don't have this bug.
 *
 * - delay inquiry
 *   Wait extra SBP2_INQUIRY_DELAY seconds after login before SCSI inquiry.
 *
 * - power condition
 *   Set the power condition field in the START STOP UNIT commands sent by
 *   sd_mod on suspend, resume, and shutdown (if manage_system_start_stop or
 *   manage_runtime_start_stop is on).
 *   Some disks need this to spin down or to resume properly.
 *
 * - override internal blacklist
 *   Instead of adding to the built-in blacklist, use only the workarounds
 *   specified in the module load parameter.
 *   Useful if a blacklist entry interfered with a non-broken device.
 */
#define SBP2_WORKAROUND_128K_MAX_TRANS
#define SBP2_WORKAROUND_INQUIRY_36
#define SBP2_WORKAROUND_MODE_SENSE_8
#define SBP2_WORKAROUND_FIX_CAPACITY
#define SBP2_WORKAROUND_DELAY_INQUIRY
#define SBP2_INQUIRY_DELAY
#define SBP2_WORKAROUND_POWER_CONDITION
#define SBP2_WORKAROUND_OVERRIDE

static int sbp2_param_workarounds;
module_param_named(workarounds, sbp2_param_workarounds, int, 0644);
MODULE_PARM_DESC();

/*
 * We create one struct sbp2_logical_unit per SBP-2 Logical Unit Number Entry
 * and one struct scsi_device per sbp2_logical_unit.
 */
struct sbp2_logical_unit {};

static void sbp2_queue_work(struct sbp2_logical_unit *lu, unsigned long delay)
{}

/*
 * We create one struct sbp2_target per IEEE 1212 Unit Directory
 * and one struct Scsi_Host per sbp2_target.
 */
struct sbp2_target {};

static struct fw_device *target_parent_device(struct sbp2_target *tgt)
{}

static const struct device *tgt_dev(const struct sbp2_target *tgt)
{}

static const struct device *lu_dev(const struct sbp2_logical_unit *lu)
{}

/* Impossible login_id, to detect logout attempt before successful login */
#define INVALID_LOGIN_ID

#define SBP2_ORB_TIMEOUT
#define SBP2_ORB_NULL
#define SBP2_RETRY_LIMIT
#define SBP2_CYCLE_LIMIT

/*
 * There is no transport protocol limit to the CDB length,  but we implement
 * a fixed length only.  16 bytes is enough for disks larger than 2 TB.
 */
#define SBP2_MAX_CDB_SIZE

/*
 * The maximum SBP-2 data buffer size is 0xffff.  We quadlet-align this
 * for compatibility with earlier versions of this driver.
 */
#define SBP2_MAX_SEG_SIZE

/* Unit directory keys */
#define SBP2_CSR_UNIT_CHARACTERISTICS
#define SBP2_CSR_FIRMWARE_REVISION
#define SBP2_CSR_LOGICAL_UNIT_NUMBER
#define SBP2_CSR_UNIT_UNIQUE_ID
#define SBP2_CSR_LOGICAL_UNIT_DIRECTORY

/* Management orb opcodes */
#define SBP2_LOGIN_REQUEST
#define SBP2_QUERY_LOGINS_REQUEST
#define SBP2_RECONNECT_REQUEST
#define SBP2_SET_PASSWORD_REQUEST
#define SBP2_LOGOUT_REQUEST
#define SBP2_ABORT_TASK_REQUEST
#define SBP2_ABORT_TASK_SET
#define SBP2_LOGICAL_UNIT_RESET
#define SBP2_TARGET_RESET_REQUEST

/* Offsets for command block agent registers */
#define SBP2_AGENT_STATE
#define SBP2_AGENT_RESET
#define SBP2_ORB_POINTER
#define SBP2_DOORBELL
#define SBP2_UNSOLICITED_STATUS_ENABLE

/* Status write response codes */
#define SBP2_STATUS_REQUEST_COMPLETE
#define SBP2_STATUS_TRANSPORT_FAILURE
#define SBP2_STATUS_ILLEGAL_REQUEST
#define SBP2_STATUS_VENDOR_DEPENDENT

#define STATUS_GET_ORB_HIGH(v)
#define STATUS_GET_SBP_STATUS(v)
#define STATUS_GET_LEN(v)
#define STATUS_GET_DEAD(v)
#define STATUS_GET_RESPONSE(v)
#define STATUS_GET_SOURCE(v)
#define STATUS_GET_ORB_LOW(v)
#define STATUS_GET_DATA(v)

struct sbp2_status {};

struct sbp2_pointer {};

struct sbp2_orb {};

#define MANAGEMENT_ORB_LUN(v)
#define MANAGEMENT_ORB_FUNCTION(v)
#define MANAGEMENT_ORB_RECONNECT(v)
#define MANAGEMENT_ORB_EXCLUSIVE(v)
#define MANAGEMENT_ORB_REQUEST_FORMAT(v)
#define MANAGEMENT_ORB_NOTIFY

#define MANAGEMENT_ORB_RESPONSE_LENGTH(v)
#define MANAGEMENT_ORB_PASSWORD_LENGTH(v)

struct sbp2_management_orb {};

struct sbp2_login_response {};
#define COMMAND_ORB_DATA_SIZE(v)
#define COMMAND_ORB_PAGE_SIZE(v)
#define COMMAND_ORB_PAGE_TABLE_PRESENT
#define COMMAND_ORB_MAX_PAYLOAD(v)
#define COMMAND_ORB_SPEED(v)
#define COMMAND_ORB_DIRECTION
#define COMMAND_ORB_REQUEST_FORMAT(v)
#define COMMAND_ORB_NOTIFY

struct sbp2_command_orb {};

#define SBP2_ROM_VALUE_WILDCARD
#define SBP2_ROM_VALUE_MISSING

/*
 * List of devices with known bugs.
 *
 * The firmware_revision field, masked with 0xffff00, is the best
 * indicator for the type of bridge chip of a device.  It yields a few
 * false positives but this did not break correctly behaving devices
 * so far.
 */
static const struct {} sbp2_workarounds_table[] =;

static void free_orb(struct kref *kref)
{}

static void sbp2_status_write(struct fw_card *card, struct fw_request *request,
			      int tcode, int destination, int source,
			      int generation, unsigned long long offset,
			      void *payload, size_t length, void *callback_data)
{}

static void complete_transaction(struct fw_card *card, int rcode,
				 void *payload, size_t length, void *data)
{}

static void sbp2_send_orb(struct sbp2_orb *orb, struct sbp2_logical_unit *lu,
			  int node_id, int generation, u64 offset)
{}

static int sbp2_cancel_orbs(struct sbp2_logical_unit *lu)
{}

static void complete_management_orb(struct sbp2_orb *base_orb,
				    struct sbp2_status *status)
{}

static int sbp2_send_management_orb(struct sbp2_logical_unit *lu, int node_id,
				    int generation, int function,
				    int lun_or_login_id, void *response)
{}

static void sbp2_agent_reset(struct sbp2_logical_unit *lu)
{}

static void complete_agent_reset_write_no_wait(struct fw_card *card,
		int rcode, void *payload, size_t length, void *data)
{}

static void sbp2_agent_reset_no_wait(struct sbp2_logical_unit *lu)
{}

static inline void sbp2_allow_block(struct sbp2_target *tgt)
{}

/*
 * Blocks lu->tgt if all of the following conditions are met:
 *   - Login, INQUIRY, and high-level SCSI setup of all of the target's
 *     logical units have been finished (indicated by dont_block == 0).
 *   - lu->generation is stale.
 *
 * Note, scsi_block_requests() must be called while holding tgt->lock,
 * otherwise it might foil sbp2_[conditionally_]unblock()'s attempt to
 * unblock the target.
 */
static void sbp2_conditionally_block(struct sbp2_logical_unit *lu)
{}

/*
 * Unblocks lu->tgt as soon as all its logical units can be unblocked.
 * Note, it is harmless to run scsi_unblock_requests() outside the
 * tgt->lock protected section.  On the other hand, running it inside
 * the section might clash with shost->host_lock.
 */
static void sbp2_conditionally_unblock(struct sbp2_logical_unit *lu)
{}

/*
 * Prevents future blocking of tgt and unblocks it.
 * Note, it is harmless to run scsi_unblock_requests() outside the
 * tgt->lock protected section.  On the other hand, running it inside
 * the section might clash with shost->host_lock.
 */
static void sbp2_unblock(struct sbp2_target *tgt)
{}

static int sbp2_lun2int(u16 lun)
{}

/*
 * Write retransmit retry values into the BUSY_TIMEOUT register.
 * - The single-phase retry protocol is supported by all SBP-2 devices, but the
 *   default retry_limit value is 0 (i.e. never retry transmission). We write a
 *   saner value after logging into the device.
 * - The dual-phase retry protocol is optional to implement, and if not
 *   supported, writes to the dual-phase portion of the register will be
 *   ignored. We try to write the original 1394-1995 default here.
 * - In the case of devices that are also SBP-3-compliant, all writes are
 *   ignored, as the register is read-only, but contains single-phase retry of
 *   15, which is what we're trying to set for all SBP-2 device anyway, so this
 *   write attempt is safe and yields more consistent behavior for all devices.
 *
 * See section 8.3.2.3.5 of the 1394-1995 spec, section 6.2 of the SBP-2 spec,
 * and section 6.4 of the SBP-3 spec for further details.
 */
static void sbp2_set_busy_timeout(struct sbp2_logical_unit *lu)
{}

static void sbp2_reconnect(struct work_struct *work);

static void sbp2_login(struct work_struct *work)
{}

static void sbp2_reconnect(struct work_struct *work)
{}

static void sbp2_lu_workfn(struct work_struct *work)
{}

static int sbp2_add_logical_unit(struct sbp2_target *tgt, int lun_entry)
{}

static void sbp2_get_unit_unique_id(struct sbp2_target *tgt,
				    const u32 *leaf)
{}

static int sbp2_scan_logical_unit_dir(struct sbp2_target *tgt,
				      const u32 *directory)
{}

static int sbp2_scan_unit_dir(struct sbp2_target *tgt, const u32 *directory,
			      u32 *model, u32 *firmware_revision)
{}

/*
 * Per section 7.4.8 of the SBP-2 spec, a mgt_ORB_timeout value can be
 * provided in the config rom. Most devices do provide a value, which
 * we'll use for login management orbs, but with some sane limits.
 */
static void sbp2_clamp_management_orb_timeout(struct sbp2_target *tgt)
{}

static void sbp2_init_workarounds(struct sbp2_target *tgt, u32 model,
				  u32 firmware_revision)
{}

static const struct scsi_host_template scsi_driver_template;
static void sbp2_remove(struct fw_unit *unit);

static int sbp2_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
{}

static void sbp2_update(struct fw_unit *unit)
{}

static void sbp2_remove(struct fw_unit *unit)
{}

#define SBP2_UNIT_SPEC_ID_ENTRY
#define SBP2_SW_VERSION_ENTRY

static const struct ieee1394_device_id sbp2_id_table[] =;

static struct fw_driver sbp2_driver =;

static void sbp2_unmap_scatterlist(struct device *card_device,
				   struct sbp2_command_orb *orb)
{}

static unsigned int sbp2_status_to_sense_data(u8 *sbp2_status, u8 *sense_data)
{}

static void complete_command_orb(struct sbp2_orb *base_orb,
				 struct sbp2_status *status)
{}

static int sbp2_map_scatterlist(struct sbp2_command_orb *orb,
		struct fw_device *device, struct sbp2_logical_unit *lu)
{}

/* SCSI stack integration */

static int sbp2_scsi_queuecommand(struct Scsi_Host *shost,
				  struct scsi_cmnd *cmd)
{}

static int sbp2_scsi_slave_alloc(struct scsi_device *sdev)
{}

static int sbp2_scsi_device_configure(struct scsi_device *sdev,
		struct queue_limits *lim)
{}

/*
 * Called by scsi stack when something has really gone wrong.  Usually
 * called when a command has timed-out for some reason.
 */
static int sbp2_scsi_abort(struct scsi_cmnd *cmd)
{}

/*
 * 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)
{}

static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);

static struct attribute *sbp2_scsi_sysfs_attrs[] =;

ATTRIBUTE_GROUPS();

static const struct scsi_host_template scsi_driver_template =;

MODULE_AUTHOR();
MODULE_DESCRIPTION();
MODULE_LICENSE();
MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);

/* Provide a module alias so root-on-sbp2 initrds don't break. */
MODULE_ALIAS();

static int __init sbp2_init(void)
{}

static void __exit sbp2_cleanup(void)
{}

module_init();
module_exit(sbp2_cleanup);