Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * SBP2 driver (SCSI over IEEE1394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (C) 2005-2007  Kristian Hoegsberg <krh@bitplanet.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  * The basic structure of this driver is based on the old storage driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  * drivers/ieee1394/sbp2.c, originally written by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  *     James Goodwin <jamesg@filanet.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  * with later contributions and ongoing maintenance from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  *     Ben Collins <bcollins@debian.org>,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  *     Stefan Richter <stefanr@s5r6.in-berlin.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  * and many others.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/bug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/completion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/dma-mapping.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/firewire.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/firewire-constants.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/kref.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/mod_devicetable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include <linux/stringify.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #include <asm/byteorder.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #include <scsi/scsi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #include <scsi/scsi_cmnd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #include <scsi/scsi_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #include <scsi/scsi_host.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49)  * So far only bridges from Oxford Semiconductor are known to support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50)  * concurrent logins. Depending on firmware, four or two concurrent logins
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51)  * are possible on OXFW911 and newer Oxsemi bridges.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53)  * Concurrent logins are useful together with cluster filesystems.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) static bool sbp2_param_exclusive_login = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) module_param_named(exclusive_login, sbp2_param_exclusive_login, bool, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 		 "(default = Y, use N for concurrent initiators)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61)  * Flags for firmware oddities
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63)  * - 128kB max transfer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64)  *   Limit transfer size. Necessary for some old bridges.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66)  * - 36 byte inquiry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67)  *   When scsi_mod probes the device, let the inquiry command look like that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68)  *   from MS Windows.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70)  * - skip mode page 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71)  *   Suppress sending of mode_sense for mode page 8 if the device pretends to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72)  *   support the SCSI Primary Block commands instead of Reduced Block Commands.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74)  * - fix capacity
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75)  *   Tell sd_mod to correct the last sector number reported by read_capacity.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76)  *   Avoids access beyond actual disk limits on devices with an off-by-one bug.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77)  *   Don't use this with devices which don't have this bug.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79)  * - delay inquiry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80)  *   Wait extra SBP2_INQUIRY_DELAY seconds after login before SCSI inquiry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82)  * - power condition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83)  *   Set the power condition field in the START STOP UNIT commands sent by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84)  *   sd_mod on suspend, resume, and shutdown (if manage_start_stop is on).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85)  *   Some disks need this to spin down or to resume properly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87)  * - override internal blacklist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88)  *   Instead of adding to the built-in blacklist, use only the workarounds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89)  *   specified in the module load parameter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90)  *   Useful if a blacklist entry interfered with a non-broken device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) #define SBP2_WORKAROUND_128K_MAX_TRANS	0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) #define SBP2_WORKAROUND_INQUIRY_36	0x2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) #define SBP2_WORKAROUND_MODE_SENSE_8	0x4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) #define SBP2_WORKAROUND_FIX_CAPACITY	0x8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) #define SBP2_WORKAROUND_DELAY_INQUIRY	0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) #define SBP2_INQUIRY_DELAY		12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) #define SBP2_WORKAROUND_POWER_CONDITION	0x20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) #define SBP2_WORKAROUND_OVERRIDE	0x100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) static int sbp2_param_workarounds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) module_param_named(workarounds, sbp2_param_workarounds, int, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	", 128kB max transfer = " __stringify(SBP2_WORKAROUND_128K_MAX_TRANS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	", 36 byte inquiry = "    __stringify(SBP2_WORKAROUND_INQUIRY_36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	", skip mode page 8 = "   __stringify(SBP2_WORKAROUND_MODE_SENSE_8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	", fix capacity = "       __stringify(SBP2_WORKAROUND_FIX_CAPACITY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	", delay inquiry = "      __stringify(SBP2_WORKAROUND_DELAY_INQUIRY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	", set power condition in start stop unit = "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 				  __stringify(SBP2_WORKAROUND_POWER_CONDITION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	", or a combination)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115)  * We create one struct sbp2_logical_unit per SBP-2 Logical Unit Number Entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116)  * and one struct scsi_device per sbp2_logical_unit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) struct sbp2_logical_unit {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	struct sbp2_target *tgt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	struct list_head link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	struct fw_address_handler address_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	struct list_head orb_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	u64 command_block_agent_address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	u16 lun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	int login_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	 * The generation is updated once we've logged in or reconnected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	 * to the logical unit.  Thus, I/O to the device will automatically
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	 * fail and get retried if it happens in a window where the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	 * is not ready, e.g. after a bus reset but before we reconnect.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	int generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	int retries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	work_func_t workfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	struct delayed_work work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	bool has_sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	bool blocked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) static void sbp2_queue_work(struct sbp2_logical_unit *lu, unsigned long delay)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	queue_delayed_work(fw_workqueue, &lu->work, delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148)  * We create one struct sbp2_target per IEEE 1212 Unit Directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149)  * and one struct Scsi_Host per sbp2_target.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) struct sbp2_target {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	struct fw_unit *unit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	struct list_head lu_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	u64 management_agent_address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	u64 guid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	int directory_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	int node_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	int address_high;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	unsigned int workarounds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	unsigned int mgt_orb_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	unsigned int max_payload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	int dont_block;	/* counter for each logical unit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	int blocked;	/* ditto */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) static struct fw_device *target_parent_device(struct sbp2_target *tgt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	return fw_parent_device(tgt->unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) static const struct device *tgt_dev(const struct sbp2_target *tgt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	return &tgt->unit->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) static const struct device *lu_dev(const struct sbp2_logical_unit *lu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	return &lu->tgt->unit->device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) /* Impossible login_id, to detect logout attempt before successful login */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) #define INVALID_LOGIN_ID 0x10000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) #define SBP2_ORB_TIMEOUT		2000U		/* Timeout in ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) #define SBP2_ORB_NULL			0x80000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) #define SBP2_RETRY_LIMIT		0xf		/* 15 retries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) #define SBP2_CYCLE_LIMIT		(0xc8 << 12)	/* 200 125us cycles */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193)  * There is no transport protocol limit to the CDB length,  but we implement
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194)  * a fixed length only.  16 bytes is enough for disks larger than 2 TB.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) #define SBP2_MAX_CDB_SIZE		16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199)  * The maximum SBP-2 data buffer size is 0xffff.  We quadlet-align this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200)  * for compatibility with earlier versions of this driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) #define SBP2_MAX_SEG_SIZE		0xfffc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) /* Unit directory keys */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) #define SBP2_CSR_UNIT_CHARACTERISTICS	0x3a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) #define SBP2_CSR_FIRMWARE_REVISION	0x3c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) #define SBP2_CSR_LOGICAL_UNIT_NUMBER	0x14
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) #define SBP2_CSR_UNIT_UNIQUE_ID		0x8d
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) #define SBP2_CSR_LOGICAL_UNIT_DIRECTORY	0xd4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) /* Management orb opcodes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) #define SBP2_LOGIN_REQUEST		0x0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) #define SBP2_QUERY_LOGINS_REQUEST	0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) #define SBP2_RECONNECT_REQUEST		0x3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) #define SBP2_SET_PASSWORD_REQUEST	0x4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) #define SBP2_LOGOUT_REQUEST		0x7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) #define SBP2_ABORT_TASK_REQUEST		0xb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) #define SBP2_ABORT_TASK_SET		0xc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) #define SBP2_LOGICAL_UNIT_RESET		0xe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) #define SBP2_TARGET_RESET_REQUEST	0xf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) /* Offsets for command block agent registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) #define SBP2_AGENT_STATE		0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) #define SBP2_AGENT_RESET		0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) #define SBP2_ORB_POINTER		0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) #define SBP2_DOORBELL			0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) #define SBP2_UNSOLICITED_STATUS_ENABLE	0x14
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) /* Status write response codes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) #define SBP2_STATUS_REQUEST_COMPLETE	0x0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) #define SBP2_STATUS_TRANSPORT_FAILURE	0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) #define SBP2_STATUS_ILLEGAL_REQUEST	0x2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) #define SBP2_STATUS_VENDOR_DEPENDENT	0x3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) #define STATUS_GET_ORB_HIGH(v)		((v).status & 0xffff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) #define STATUS_GET_SBP_STATUS(v)	(((v).status >> 16) & 0xff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) #define STATUS_GET_LEN(v)		(((v).status >> 24) & 0x07)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) #define STATUS_GET_DEAD(v)		(((v).status >> 27) & 0x01)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) #define STATUS_GET_RESPONSE(v)		(((v).status >> 28) & 0x03)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) #define STATUS_GET_SOURCE(v)		(((v).status >> 30) & 0x03)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) #define STATUS_GET_ORB_LOW(v)		((v).orb_low)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) #define STATUS_GET_DATA(v)		((v).data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) struct sbp2_status {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	u32 status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	u32 orb_low;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	u8 data[24];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) struct sbp2_pointer {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	__be32 high;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	__be32 low;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) struct sbp2_orb {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	struct fw_transaction t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	struct kref kref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	dma_addr_t request_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	int rcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	void (*callback)(struct sbp2_orb * orb, struct sbp2_status * status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	struct sbp2_logical_unit *lu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	struct list_head link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) #define MANAGEMENT_ORB_LUN(v)			((v))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) #define MANAGEMENT_ORB_FUNCTION(v)		((v) << 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) #define MANAGEMENT_ORB_RECONNECT(v)		((v) << 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) #define MANAGEMENT_ORB_EXCLUSIVE(v)		((v) ? 1 << 28 : 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) #define MANAGEMENT_ORB_REQUEST_FORMAT(v)	((v) << 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) #define MANAGEMENT_ORB_NOTIFY			((1) << 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) #define MANAGEMENT_ORB_RESPONSE_LENGTH(v)	((v))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) #define MANAGEMENT_ORB_PASSWORD_LENGTH(v)	((v) << 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) struct sbp2_management_orb {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	struct sbp2_orb base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		struct sbp2_pointer password;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 		struct sbp2_pointer response;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 		__be32 misc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		__be32 length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 		struct sbp2_pointer status_fifo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	} request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	__be32 response[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	dma_addr_t response_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	struct completion done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	struct sbp2_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) struct sbp2_login_response {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	__be32 misc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	struct sbp2_pointer command_block_agent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	__be32 reconnect_hold;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) #define COMMAND_ORB_DATA_SIZE(v)	((v))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) #define COMMAND_ORB_PAGE_SIZE(v)	((v) << 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) #define COMMAND_ORB_PAGE_TABLE_PRESENT	((1) << 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) #define COMMAND_ORB_MAX_PAYLOAD(v)	((v) << 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) #define COMMAND_ORB_SPEED(v)		((v) << 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) #define COMMAND_ORB_DIRECTION		((1) << 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) #define COMMAND_ORB_REQUEST_FORMAT(v)	((v) << 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) #define COMMAND_ORB_NOTIFY		((1) << 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) struct sbp2_command_orb {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	struct sbp2_orb base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 		struct sbp2_pointer next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		struct sbp2_pointer data_descriptor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 		__be32 misc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 		u8 command_block[SBP2_MAX_CDB_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	} request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	struct scsi_cmnd *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	struct sbp2_pointer page_table[SG_ALL] __attribute__((aligned(8)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	dma_addr_t page_table_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) #define SBP2_ROM_VALUE_WILDCARD ~0         /* match all */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) #define SBP2_ROM_VALUE_MISSING  0xff000000 /* not present in the unit dir. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322)  * List of devices with known bugs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324)  * The firmware_revision field, masked with 0xffff00, is the best
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325)  * indicator for the type of bridge chip of a device.  It yields a few
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326)  * false positives but this did not break correctly behaving devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327)  * so far.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) static const struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	u32 firmware_revision;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	u32 model;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	unsigned int workarounds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) } sbp2_workarounds_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	/* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 		.firmware_revision	= 0x002800,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 		.model			= 0x001010,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 		.workarounds		= SBP2_WORKAROUND_INQUIRY_36 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 					  SBP2_WORKAROUND_MODE_SENSE_8 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 					  SBP2_WORKAROUND_POWER_CONDITION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	/* DViCO Momobay FX-3A with TSB42AA9A bridge */ {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 		.firmware_revision	= 0x002800,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 		.model			= 0x000000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		.workarounds		= SBP2_WORKAROUND_POWER_CONDITION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	/* Initio bridges, actually only needed for some older ones */ {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 		.firmware_revision	= 0x000200,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		.model			= SBP2_ROM_VALUE_WILDCARD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 		.workarounds		= SBP2_WORKAROUND_INQUIRY_36,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	/* PL-3507 bridge with Prolific firmware */ {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 		.firmware_revision	= 0x012800,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 		.model			= SBP2_ROM_VALUE_WILDCARD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 		.workarounds		= SBP2_WORKAROUND_POWER_CONDITION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	/* Symbios bridge */ {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 		.firmware_revision	= 0xa0b800,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 		.model			= SBP2_ROM_VALUE_WILDCARD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		.workarounds		= SBP2_WORKAROUND_128K_MAX_TRANS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	/* Datafab MD2-FW2 with Symbios/LSILogic SYM13FW500 bridge */ {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 		.firmware_revision	= 0x002600,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 		.model			= SBP2_ROM_VALUE_WILDCARD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 		.workarounds		= SBP2_WORKAROUND_128K_MAX_TRANS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	 * iPod 2nd generation: needs 128k max transfer size workaround
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	 * iPod 3rd generation: needs fix capacity workaround
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 		.firmware_revision	= 0x0a2700,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 		.model			= 0x000000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 		.workarounds		= SBP2_WORKAROUND_128K_MAX_TRANS |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 					  SBP2_WORKAROUND_FIX_CAPACITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	/* iPod 4th generation */ {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 		.firmware_revision	= 0x0a2700,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 		.model			= 0x000021,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 		.workarounds		= SBP2_WORKAROUND_FIX_CAPACITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	/* iPod mini */ {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 		.firmware_revision	= 0x0a2700,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 		.model			= 0x000022,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		.workarounds		= SBP2_WORKAROUND_FIX_CAPACITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	/* iPod mini */ {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 		.firmware_revision	= 0x0a2700,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 		.model			= 0x000023,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 		.workarounds		= SBP2_WORKAROUND_FIX_CAPACITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	/* iPod Photo */ {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		.firmware_revision	= 0x0a2700,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 		.model			= 0x00007e,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 		.workarounds		= SBP2_WORKAROUND_FIX_CAPACITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) static void free_orb(struct kref *kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	struct sbp2_orb *orb = container_of(kref, struct sbp2_orb, kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	kfree(orb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) static void sbp2_status_write(struct fw_card *card, struct fw_request *request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 			      int tcode, int destination, int source,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 			      int generation, unsigned long long offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 			      void *payload, size_t length, void *callback_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	struct sbp2_logical_unit *lu = callback_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	struct sbp2_orb *orb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	struct sbp2_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	if (tcode != TCODE_WRITE_BLOCK_REQUEST ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	    length < 8 || length > sizeof(status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 		fw_send_response(card, request, RCODE_TYPE_ERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	status.status  = be32_to_cpup(payload);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	status.orb_low = be32_to_cpup(payload + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	memset(status.data, 0, sizeof(status.data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	if (length > 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		memcpy(status.data, payload + 8, length - 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	if (STATUS_GET_SOURCE(status) == 2 || STATUS_GET_SOURCE(status) == 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 		dev_notice(lu_dev(lu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 			   "non-ORB related status write, not handled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 		fw_send_response(card, request, RCODE_COMPLETE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	/* Lookup the orb corresponding to this status write. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	spin_lock_irqsave(&lu->tgt->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	list_for_each_entry(orb, &lu->orb_list, link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 		if (STATUS_GET_ORB_HIGH(status) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 		    STATUS_GET_ORB_LOW(status) == orb->request_bus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 			orb->rcode = RCODE_COMPLETE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 			list_del(&orb->link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	spin_unlock_irqrestore(&lu->tgt->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	if (&orb->link != &lu->orb_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 		orb->callback(orb, &status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 		kref_put(&orb->kref, free_orb); /* orb callback reference */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 		dev_err(lu_dev(lu), "status write for unknown ORB\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	fw_send_response(card, request, RCODE_COMPLETE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) static void complete_transaction(struct fw_card *card, int rcode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 				 void *payload, size_t length, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	struct sbp2_orb *orb = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	 * This is a little tricky.  We can get the status write for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	 * the orb before we get this callback.  The status write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	 * handler above will assume the orb pointer transaction was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	 * successful and set the rcode to RCODE_COMPLETE for the orb.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	 * So this callback only sets the rcode if it hasn't already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	 * been set and only does the cleanup if the transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	 * failed and we didn't already get a status write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	spin_lock_irqsave(&orb->lu->tgt->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	if (orb->rcode == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 		orb->rcode = rcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	if (orb->rcode != RCODE_COMPLETE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 		list_del(&orb->link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		spin_unlock_irqrestore(&orb->lu->tgt->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 		orb->callback(orb, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 		kref_put(&orb->kref, free_orb); /* orb callback reference */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 		spin_unlock_irqrestore(&orb->lu->tgt->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	kref_put(&orb->kref, free_orb); /* transaction callback reference */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) static void sbp2_send_orb(struct sbp2_orb *orb, struct sbp2_logical_unit *lu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 			  int node_id, int generation, u64 offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	struct fw_device *device = target_parent_device(lu->tgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	struct sbp2_pointer orb_pointer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	orb_pointer.high = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	orb_pointer.low = cpu_to_be32(orb->request_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	orb->lu = lu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	spin_lock_irqsave(&lu->tgt->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	list_add_tail(&orb->link, &lu->orb_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	spin_unlock_irqrestore(&lu->tgt->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	kref_get(&orb->kref); /* transaction callback reference */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	kref_get(&orb->kref); /* orb callback reference */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	fw_send_request(device->card, &orb->t, TCODE_WRITE_BLOCK_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 			node_id, generation, device->max_speed, offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 			&orb_pointer, 8, complete_transaction, orb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) static int sbp2_cancel_orbs(struct sbp2_logical_unit *lu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	struct fw_device *device = target_parent_device(lu->tgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	struct sbp2_orb *orb, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	int retval = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	INIT_LIST_HEAD(&list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	spin_lock_irq(&lu->tgt->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	list_splice_init(&lu->orb_list, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	spin_unlock_irq(&lu->tgt->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	list_for_each_entry_safe(orb, next, &list, link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 		retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		if (fw_cancel_transaction(device->card, &orb->t) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 		orb->rcode = RCODE_CANCELLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 		orb->callback(orb, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 		kref_put(&orb->kref, free_orb); /* orb callback reference */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) static void complete_management_orb(struct sbp2_orb *base_orb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 				    struct sbp2_status *status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	struct sbp2_management_orb *orb =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		container_of(base_orb, struct sbp2_management_orb, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		memcpy(&orb->status, status, sizeof(*status));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	complete(&orb->done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) static int sbp2_send_management_orb(struct sbp2_logical_unit *lu, int node_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 				    int generation, int function,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 				    int lun_or_login_id, void *response)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	struct fw_device *device = target_parent_device(lu->tgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	struct sbp2_management_orb *orb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	unsigned int timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	int retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	if (function == SBP2_LOGOUT_REQUEST && fw_device_is_shutdown(device))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	orb = kzalloc(sizeof(*orb), GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	if (orb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	kref_init(&orb->base.kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	orb->response_bus =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 		dma_map_single(device->card->device, &orb->response,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 			       sizeof(orb->response), DMA_FROM_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	if (dma_mapping_error(device->card->device, orb->response_bus))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		goto fail_mapping_response;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	orb->request.response.high = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	orb->request.response.low  = cpu_to_be32(orb->response_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	orb->request.misc = cpu_to_be32(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		MANAGEMENT_ORB_NOTIFY |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		MANAGEMENT_ORB_FUNCTION(function) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 		MANAGEMENT_ORB_LUN(lun_or_login_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	orb->request.length = cpu_to_be32(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 		MANAGEMENT_ORB_RESPONSE_LENGTH(sizeof(orb->response)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	orb->request.status_fifo.high =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 		cpu_to_be32(lu->address_handler.offset >> 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	orb->request.status_fifo.low  =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 		cpu_to_be32(lu->address_handler.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	if (function == SBP2_LOGIN_REQUEST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 		/* Ask for 2^2 == 4 seconds reconnect grace period */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		orb->request.misc |= cpu_to_be32(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 			MANAGEMENT_ORB_RECONNECT(2) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 			MANAGEMENT_ORB_EXCLUSIVE(sbp2_param_exclusive_login));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 		timeout = lu->tgt->mgt_orb_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		timeout = SBP2_ORB_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	init_completion(&orb->done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	orb->base.callback = complete_management_orb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	orb->base.request_bus =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		dma_map_single(device->card->device, &orb->request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 			       sizeof(orb->request), DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	if (dma_mapping_error(device->card->device, orb->base.request_bus))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 		goto fail_mapping_request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	sbp2_send_orb(&orb->base, lu, node_id, generation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 		      lu->tgt->management_agent_address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	wait_for_completion_timeout(&orb->done, msecs_to_jiffies(timeout));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	retval = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	if (sbp2_cancel_orbs(lu) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		dev_err(lu_dev(lu), "ORB reply timed out, rcode 0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 			orb->base.rcode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	if (orb->base.rcode != RCODE_COMPLETE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 		dev_err(lu_dev(lu), "management write failed, rcode 0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 			orb->base.rcode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	if (STATUS_GET_RESPONSE(orb->status) != 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	    STATUS_GET_SBP_STATUS(orb->status) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		dev_err(lu_dev(lu), "error status: %d:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 			 STATUS_GET_RESPONSE(orb->status),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 			 STATUS_GET_SBP_STATUS(orb->status));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	dma_unmap_single(device->card->device, orb->base.request_bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 			 sizeof(orb->request), DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634)  fail_mapping_request:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	dma_unmap_single(device->card->device, orb->response_bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 			 sizeof(orb->response), DMA_FROM_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637)  fail_mapping_response:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	if (response)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		memcpy(response, orb->response, sizeof(orb->response));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	kref_put(&orb->base.kref, free_orb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) static void sbp2_agent_reset(struct sbp2_logical_unit *lu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	struct fw_device *device = target_parent_device(lu->tgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	__be32 d = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	fw_run_transaction(device->card, TCODE_WRITE_QUADLET_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 			   lu->tgt->node_id, lu->generation, device->max_speed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 			   lu->command_block_agent_address + SBP2_AGENT_RESET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 			   &d, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) static void complete_agent_reset_write_no_wait(struct fw_card *card,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		int rcode, void *payload, size_t length, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) static void sbp2_agent_reset_no_wait(struct sbp2_logical_unit *lu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	struct fw_device *device = target_parent_device(lu->tgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	struct fw_transaction *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	static __be32 d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	t = kmalloc(sizeof(*t), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	if (t == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	fw_send_request(device->card, t, TCODE_WRITE_QUADLET_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 			lu->tgt->node_id, lu->generation, device->max_speed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 			lu->command_block_agent_address + SBP2_AGENT_RESET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 			&d, 4, complete_agent_reset_write_no_wait, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) static inline void sbp2_allow_block(struct sbp2_target *tgt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	spin_lock_irq(&tgt->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	--tgt->dont_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	spin_unlock_irq(&tgt->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686)  * Blocks lu->tgt if all of the following conditions are met:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687)  *   - Login, INQUIRY, and high-level SCSI setup of all of the target's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688)  *     logical units have been finished (indicated by dont_block == 0).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689)  *   - lu->generation is stale.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691)  * Note, scsi_block_requests() must be called while holding tgt->lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692)  * otherwise it might foil sbp2_[conditionally_]unblock()'s attempt to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693)  * unblock the target.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) static void sbp2_conditionally_block(struct sbp2_logical_unit *lu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	struct sbp2_target *tgt = lu->tgt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	struct fw_card *card = target_parent_device(tgt)->card;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	struct Scsi_Host *shost =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 		container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	spin_lock_irqsave(&tgt->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	if (!tgt->dont_block && !lu->blocked &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	    lu->generation != card->generation) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 		lu->blocked = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 		if (++tgt->blocked == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 			scsi_block_requests(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	spin_unlock_irqrestore(&tgt->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714)  * Unblocks lu->tgt as soon as all its logical units can be unblocked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715)  * Note, it is harmless to run scsi_unblock_requests() outside the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716)  * tgt->lock protected section.  On the other hand, running it inside
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717)  * the section might clash with shost->host_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) static void sbp2_conditionally_unblock(struct sbp2_logical_unit *lu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	struct sbp2_target *tgt = lu->tgt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	struct fw_card *card = target_parent_device(tgt)->card;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	struct Scsi_Host *shost =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	bool unblock = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	spin_lock_irq(&tgt->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	if (lu->blocked && lu->generation == card->generation) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 		lu->blocked = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		unblock = --tgt->blocked == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	spin_unlock_irq(&tgt->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	if (unblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		scsi_unblock_requests(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739)  * Prevents future blocking of tgt and unblocks it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740)  * Note, it is harmless to run scsi_unblock_requests() outside the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741)  * tgt->lock protected section.  On the other hand, running it inside
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742)  * the section might clash with shost->host_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) static void sbp2_unblock(struct sbp2_target *tgt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	struct Scsi_Host *shost =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	spin_lock_irq(&tgt->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	++tgt->dont_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	spin_unlock_irq(&tgt->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	scsi_unblock_requests(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) static int sbp2_lun2int(u16 lun)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	struct scsi_lun eight_bytes_lun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	memset(&eight_bytes_lun, 0, sizeof(eight_bytes_lun));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	eight_bytes_lun.scsi_lun[0] = (lun >> 8) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	eight_bytes_lun.scsi_lun[1] = lun & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	return scsilun_to_int(&eight_bytes_lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768)  * Write retransmit retry values into the BUSY_TIMEOUT register.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769)  * - The single-phase retry protocol is supported by all SBP-2 devices, but the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770)  *   default retry_limit value is 0 (i.e. never retry transmission). We write a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771)  *   saner value after logging into the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772)  * - The dual-phase retry protocol is optional to implement, and if not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773)  *   supported, writes to the dual-phase portion of the register will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774)  *   ignored. We try to write the original 1394-1995 default here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775)  * - In the case of devices that are also SBP-3-compliant, all writes are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776)  *   ignored, as the register is read-only, but contains single-phase retry of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777)  *   15, which is what we're trying to set for all SBP-2 device anyway, so this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778)  *   write attempt is safe and yields more consistent behavior for all devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780)  * See section 8.3.2.3.5 of the 1394-1995 spec, section 6.2 of the SBP-2 spec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781)  * and section 6.4 of the SBP-3 spec for further details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) static void sbp2_set_busy_timeout(struct sbp2_logical_unit *lu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	struct fw_device *device = target_parent_device(lu->tgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	__be32 d = cpu_to_be32(SBP2_CYCLE_LIMIT | SBP2_RETRY_LIMIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	fw_run_transaction(device->card, TCODE_WRITE_QUADLET_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 			   lu->tgt->node_id, lu->generation, device->max_speed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 			   CSR_REGISTER_BASE + CSR_BUSY_TIMEOUT, &d, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) static void sbp2_reconnect(struct work_struct *work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) static void sbp2_login(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	struct sbp2_logical_unit *lu =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		container_of(work, struct sbp2_logical_unit, work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	struct sbp2_target *tgt = lu->tgt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	struct fw_device *device = target_parent_device(tgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	struct Scsi_Host *shost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	struct scsi_device *sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	struct sbp2_login_response response;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	int generation, node_id, local_node_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	if (fw_device_is_shutdown(device))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	generation    = device->generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	smp_rmb();    /* node IDs must not be older than generation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	node_id       = device->node_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	local_node_id = device->card->node_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	/* If this is a re-login attempt, log out, or we might be rejected. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	if (lu->has_sdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 		sbp2_send_management_orb(lu, device->node_id, generation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 				SBP2_LOGOUT_REQUEST, lu->login_id, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	if (sbp2_send_management_orb(lu, node_id, generation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 				SBP2_LOGIN_REQUEST, lu->lun, &response) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		if (lu->retries++ < 5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 			sbp2_queue_work(lu, DIV_ROUND_UP(HZ, 5));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 			dev_err(tgt_dev(tgt), "failed to login to LUN %04x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 				lu->lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 			/* Let any waiting I/O fail from now on. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 			sbp2_unblock(lu->tgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	tgt->node_id	  = node_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	tgt->address_high = local_node_id << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	smp_wmb();	  /* node IDs must not be older than generation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	lu->generation	  = generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	lu->command_block_agent_address =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		((u64)(be32_to_cpu(response.command_block_agent.high) & 0xffff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		      << 32) | be32_to_cpu(response.command_block_agent.low);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	lu->login_id = be32_to_cpu(response.misc) & 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	dev_notice(tgt_dev(tgt), "logged in to LUN %04x (%d retries)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		   lu->lun, lu->retries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	/* set appropriate retry limit(s) in BUSY_TIMEOUT register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	sbp2_set_busy_timeout(lu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	lu->workfn = sbp2_reconnect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	sbp2_agent_reset(lu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	/* This was a re-login. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	if (lu->has_sdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 		sbp2_cancel_orbs(lu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 		sbp2_conditionally_unblock(lu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	if (lu->tgt->workarounds & SBP2_WORKAROUND_DELAY_INQUIRY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 		ssleep(SBP2_INQUIRY_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	shost = container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	sdev = __scsi_add_device(shost, 0, 0, sbp2_lun2int(lu->lun), lu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	 * FIXME:  We are unable to perform reconnects while in sbp2_login().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	 * Therefore __scsi_add_device() will get into trouble if a bus reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	 * happens in parallel.  It will either fail or leave us with an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	 * unusable sdev.  As a workaround we check for this and retry the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	 * whole login and SCSI probing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	/* Reported error during __scsi_add_device() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	if (IS_ERR(sdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		goto out_logout_login;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	/* Unreported error during __scsi_add_device() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	smp_rmb(); /* get current card generation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	if (generation != device->card->generation) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		scsi_remove_device(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		scsi_device_put(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 		goto out_logout_login;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	/* No error during __scsi_add_device() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	lu->has_sdev = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	scsi_device_put(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	sbp2_allow_block(tgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891)  out_logout_login:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	smp_rmb(); /* generation may have changed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	generation = device->generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	smp_rmb(); /* node_id must not be older than generation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	sbp2_send_management_orb(lu, device->node_id, generation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 				 SBP2_LOGOUT_REQUEST, lu->login_id, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	 * If a bus reset happened, sbp2_update will have requeued
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	 * lu->work already.  Reset the work from reconnect to login.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	lu->workfn = sbp2_login;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) static void sbp2_reconnect(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	struct sbp2_logical_unit *lu =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		container_of(work, struct sbp2_logical_unit, work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	struct sbp2_target *tgt = lu->tgt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	struct fw_device *device = target_parent_device(tgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	int generation, node_id, local_node_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	if (fw_device_is_shutdown(device))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	generation    = device->generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	smp_rmb();    /* node IDs must not be older than generation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	node_id       = device->node_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	local_node_id = device->card->node_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	if (sbp2_send_management_orb(lu, node_id, generation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 				     SBP2_RECONNECT_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 				     lu->login_id, NULL) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 		 * If reconnect was impossible even though we are in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 		 * current generation, fall back and try to log in again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 		 * We could check for "Function rejected" status, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 		 * looking at the bus generation as simpler and more general.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 		smp_rmb(); /* get current card generation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		if (generation == device->card->generation ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		    lu->retries++ >= 5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 			dev_err(tgt_dev(tgt), "failed to reconnect\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 			lu->retries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 			lu->workfn = sbp2_login;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 		sbp2_queue_work(lu, DIV_ROUND_UP(HZ, 5));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	tgt->node_id      = node_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	tgt->address_high = local_node_id << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	smp_wmb();	  /* node IDs must not be older than generation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	lu->generation	  = generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	dev_notice(tgt_dev(tgt), "reconnected to LUN %04x (%d retries)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 		   lu->lun, lu->retries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	sbp2_agent_reset(lu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	sbp2_cancel_orbs(lu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	sbp2_conditionally_unblock(lu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) static void sbp2_lu_workfn(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	struct sbp2_logical_unit *lu = container_of(to_delayed_work(work),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 						struct sbp2_logical_unit, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	lu->workfn(work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) static int sbp2_add_logical_unit(struct sbp2_target *tgt, int lun_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	struct sbp2_logical_unit *lu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	lu = kmalloc(sizeof(*lu), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	if (!lu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	lu->address_handler.length           = 0x100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	lu->address_handler.address_callback = sbp2_status_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	lu->address_handler.callback_data    = lu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	if (fw_core_add_address_handler(&lu->address_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 					&fw_high_memory_region) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		kfree(lu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	lu->tgt      = tgt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	lu->lun      = lun_entry & 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	lu->login_id = INVALID_LOGIN_ID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	lu->retries  = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	lu->has_sdev = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	lu->blocked  = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	++tgt->dont_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	INIT_LIST_HEAD(&lu->orb_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	lu->workfn = sbp2_login;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	INIT_DELAYED_WORK(&lu->work, sbp2_lu_workfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	list_add_tail(&lu->link, &tgt->lu_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) static void sbp2_get_unit_unique_id(struct sbp2_target *tgt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 				    const u32 *leaf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	if ((leaf[0] & 0xffff0000) == 0x00020000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 		tgt->guid = (u64)leaf[1] << 32 | leaf[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) static int sbp2_scan_logical_unit_dir(struct sbp2_target *tgt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 				      const u32 *directory)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	struct fw_csr_iterator ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	int key, value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	fw_csr_iterator_init(&ci, directory);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	while (fw_csr_iterator_next(&ci, &key, &value))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		if (key == SBP2_CSR_LOGICAL_UNIT_NUMBER &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		    sbp2_add_logical_unit(tgt, value) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) static int sbp2_scan_unit_dir(struct sbp2_target *tgt, const u32 *directory,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 			      u32 *model, u32 *firmware_revision)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	struct fw_csr_iterator ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	int key, value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	fw_csr_iterator_init(&ci, directory);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	while (fw_csr_iterator_next(&ci, &key, &value)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 		switch (key) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 		case CSR_DEPENDENT_INFO | CSR_OFFSET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 			tgt->management_agent_address =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 					CSR_REGISTER_BASE + 4 * value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 		case CSR_DIRECTORY_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 			tgt->directory_id = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 		case CSR_MODEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 			*model = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 		case SBP2_CSR_FIRMWARE_REVISION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 			*firmware_revision = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 		case SBP2_CSR_UNIT_CHARACTERISTICS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 			/* the timeout value is stored in 500ms units */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 			tgt->mgt_orb_timeout = (value >> 8 & 0xff) * 500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 		case SBP2_CSR_LOGICAL_UNIT_NUMBER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 			if (sbp2_add_logical_unit(tgt, value) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 				return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 		case SBP2_CSR_UNIT_UNIQUE_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 			sbp2_get_unit_unique_id(tgt, ci.p - 1 + value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 		case SBP2_CSR_LOGICAL_UNIT_DIRECTORY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 			/* Adjust for the increment in the iterator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 			if (sbp2_scan_logical_unit_dir(tgt, ci.p - 1 + value) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 				return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069)  * Per section 7.4.8 of the SBP-2 spec, a mgt_ORB_timeout value can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070)  * provided in the config rom. Most devices do provide a value, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071)  * we'll use for login management orbs, but with some sane limits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) static void sbp2_clamp_management_orb_timeout(struct sbp2_target *tgt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	unsigned int timeout = tgt->mgt_orb_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	if (timeout > 40000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 		dev_notice(tgt_dev(tgt), "%ds mgt_ORB_timeout limited to 40s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 			   timeout / 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	tgt->mgt_orb_timeout = clamp_val(timeout, 5000, 40000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) static void sbp2_init_workarounds(struct sbp2_target *tgt, u32 model,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 				  u32 firmware_revision)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	unsigned int w = sbp2_param_workarounds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	if (w)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 		dev_notice(tgt_dev(tgt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 			   "Please notify linux1394-devel@lists.sf.net "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 			   "if you need the workarounds parameter\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	if (w & SBP2_WORKAROUND_OVERRIDE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 		if (sbp2_workarounds_table[i].firmware_revision !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		    (firmware_revision & 0xffffff00))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 		if (sbp2_workarounds_table[i].model != model &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 		    sbp2_workarounds_table[i].model != SBP2_ROM_VALUE_WILDCARD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 		w |= sbp2_workarounds_table[i].workarounds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	if (w)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 		dev_notice(tgt_dev(tgt), "workarounds 0x%x "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 			   "(firmware_revision 0x%06x, model_id 0x%06x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 			   w, firmware_revision, model);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	tgt->workarounds = w;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) static struct scsi_host_template scsi_driver_template;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) static void sbp2_remove(struct fw_unit *unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) static int sbp2_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	struct fw_device *device = fw_parent_device(unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	struct sbp2_target *tgt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	struct sbp2_logical_unit *lu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	struct Scsi_Host *shost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	u32 model, firmware_revision;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	/* cannot (or should not) handle targets on the local node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	if (device->is_local)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	shost = scsi_host_alloc(&scsi_driver_template, sizeof(*tgt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	if (shost == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	tgt = (struct sbp2_target *)shost->hostdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	dev_set_drvdata(&unit->device, tgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	tgt->unit = unit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	INIT_LIST_HEAD(&tgt->lu_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	spin_lock_init(&tgt->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	tgt->guid = (u64)device->config_rom[3] << 32 | device->config_rom[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	if (fw_device_enable_phys_dma(device) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 		goto fail_shost_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	shost->max_cmd_len = SBP2_MAX_CDB_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	if (scsi_add_host_with_dma(shost, &unit->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 				   device->card->device) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 		goto fail_shost_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	/* implicit directory ID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	tgt->directory_id = ((unit->directory - device->config_rom) * 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 			     + CSR_CONFIG_ROM) & 0xffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	firmware_revision = SBP2_ROM_VALUE_MISSING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	model		  = SBP2_ROM_VALUE_MISSING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	if (sbp2_scan_unit_dir(tgt, unit->directory, &model,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 			       &firmware_revision) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 		goto fail_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	sbp2_clamp_management_orb_timeout(tgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	sbp2_init_workarounds(tgt, model, firmware_revision);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	 * At S100 we can do 512 bytes per packet, at S200 1024 bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	 * and so on up to 4096 bytes.  The SBP-2 max_payload field
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	 * specifies the max payload size as 2 ^ (max_payload + 2), so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	 * if we set this to max_speed + 7, we get the right value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	tgt->max_payload = min3(device->max_speed + 7, 10U,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 				device->card->max_receive - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	/* Do the login in a workqueue so we can easily reschedule retries. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	list_for_each_entry(lu, &tgt->lu_list, link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 		sbp2_queue_work(lu, DIV_ROUND_UP(HZ, 5));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183)  fail_remove:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	sbp2_remove(unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187)  fail_shost_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	scsi_host_put(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) static void sbp2_update(struct fw_unit *unit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	struct sbp2_target *tgt = dev_get_drvdata(&unit->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	struct sbp2_logical_unit *lu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	fw_device_enable_phys_dma(fw_parent_device(unit));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	 * Fw-core serializes sbp2_update() against sbp2_remove().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	 * Iteration over tgt->lu_list is therefore safe here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	list_for_each_entry(lu, &tgt->lu_list, link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 		sbp2_conditionally_block(lu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 		lu->retries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 		sbp2_queue_work(lu, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) static void sbp2_remove(struct fw_unit *unit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	struct fw_device *device = fw_parent_device(unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	struct sbp2_target *tgt = dev_get_drvdata(&unit->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 	struct sbp2_logical_unit *lu, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	struct Scsi_Host *shost =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 		container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	struct scsi_device *sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	/* prevent deadlocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	sbp2_unblock(tgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	list_for_each_entry_safe(lu, next, &tgt->lu_list, link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 		cancel_delayed_work_sync(&lu->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 		sdev = scsi_device_lookup(shost, 0, 0, sbp2_lun2int(lu->lun));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 		if (sdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 			scsi_remove_device(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 			scsi_device_put(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 		if (lu->login_id != INVALID_LOGIN_ID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 			int generation, node_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 			 * tgt->node_id may be obsolete here if we failed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 			 * during initial login or after a bus reset where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 			 * the topology changed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 			generation = device->generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 			smp_rmb(); /* node_id vs. generation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 			node_id    = device->node_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 			sbp2_send_management_orb(lu, node_id, generation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 						 SBP2_LOGOUT_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 						 lu->login_id, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 		fw_core_remove_address_handler(&lu->address_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 		list_del(&lu->link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 		kfree(lu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	scsi_remove_host(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	dev_notice(&unit->device, "released target %d:0:0\n", shost->host_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	scsi_host_put(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) #define SBP2_UNIT_SPEC_ID_ENTRY	0x0000609e
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) #define SBP2_SW_VERSION_ENTRY	0x00010483
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) static const struct ieee1394_device_id sbp2_id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 		.match_flags  = IEEE1394_MATCH_SPECIFIER_ID |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 				IEEE1394_MATCH_VERSION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 		.specifier_id = SBP2_UNIT_SPEC_ID_ENTRY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 		.version      = SBP2_SW_VERSION_ENTRY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) static struct fw_driver sbp2_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	.driver   = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 		.owner  = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 		.name   = KBUILD_MODNAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 		.bus    = &fw_bus_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	.probe    = sbp2_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	.update   = sbp2_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	.remove   = sbp2_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	.id_table = sbp2_id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) static void sbp2_unmap_scatterlist(struct device *card_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 				   struct sbp2_command_orb *orb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 	scsi_dma_unmap(orb->cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	if (orb->request.misc & cpu_to_be32(COMMAND_ORB_PAGE_TABLE_PRESENT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 		dma_unmap_single(card_device, orb->page_table_bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 				 sizeof(orb->page_table), DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) static unsigned int sbp2_status_to_sense_data(u8 *sbp2_status, u8 *sense_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	int sam_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	int sfmt = (sbp2_status[0] >> 6) & 0x03;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	if (sfmt == 2 || sfmt == 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 		 * Reserved for future standardization (2) or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 		 * Status block format vendor-dependent (3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 		return DID_ERROR << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	sense_data[0] = 0x70 | sfmt | (sbp2_status[1] & 0x80);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	sense_data[1] = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	sense_data[2] = ((sbp2_status[1] << 1) & 0xe0) | (sbp2_status[1] & 0x0f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	sense_data[3] = sbp2_status[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	sense_data[4] = sbp2_status[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	sense_data[5] = sbp2_status[6];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	sense_data[6] = sbp2_status[7];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	sense_data[7] = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	sense_data[8] = sbp2_status[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	sense_data[9] = sbp2_status[9];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	sense_data[10] = sbp2_status[10];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	sense_data[11] = sbp2_status[11];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	sense_data[12] = sbp2_status[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	sense_data[13] = sbp2_status[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	sense_data[14] = sbp2_status[12];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 	sense_data[15] = sbp2_status[13];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	sam_status = sbp2_status[0] & 0x3f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 	switch (sam_status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	case SAM_STAT_GOOD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	case SAM_STAT_CHECK_CONDITION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	case SAM_STAT_CONDITION_MET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	case SAM_STAT_BUSY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	case SAM_STAT_RESERVATION_CONFLICT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	case SAM_STAT_COMMAND_TERMINATED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 		return DID_OK << 16 | sam_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 		return DID_ERROR << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) static void complete_command_orb(struct sbp2_orb *base_orb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 				 struct sbp2_status *status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	struct sbp2_command_orb *orb =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 		container_of(base_orb, struct sbp2_command_orb, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	struct fw_device *device = target_parent_device(base_orb->lu->tgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	if (status != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 		if (STATUS_GET_DEAD(*status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 			sbp2_agent_reset_no_wait(base_orb->lu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 		switch (STATUS_GET_RESPONSE(*status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 		case SBP2_STATUS_REQUEST_COMPLETE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 			result = DID_OK << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 		case SBP2_STATUS_TRANSPORT_FAILURE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 			result = DID_BUS_BUSY << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 		case SBP2_STATUS_ILLEGAL_REQUEST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 		case SBP2_STATUS_VENDOR_DEPENDENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 			result = DID_ERROR << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 		if (result == DID_OK << 16 && STATUS_GET_LEN(*status) > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 			result = sbp2_status_to_sense_data(STATUS_GET_DATA(*status),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 							   orb->cmd->sense_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 		 * If the orb completes with status == NULL, something
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 		 * went wrong, typically a bus reset happened mid-orb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 		 * or when sending the write (less likely).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 		result = DID_BUS_BUSY << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 		sbp2_conditionally_block(base_orb->lu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	dma_unmap_single(device->card->device, orb->base.request_bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 			 sizeof(orb->request), DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	sbp2_unmap_scatterlist(device->card->device, orb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	orb->cmd->result = result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	orb->cmd->scsi_done(orb->cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) static int sbp2_map_scatterlist(struct sbp2_command_orb *orb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 		struct fw_device *device, struct sbp2_logical_unit *lu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	struct scatterlist *sg = scsi_sglist(orb->cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	int i, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	n = scsi_dma_map(orb->cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 	if (n <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	 * Handle the special case where there is only one element in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	 * the scatter list by converting it to an immediate block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	 * request. This is also a workaround for broken devices such
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 	 * as the second generation iPod which doesn't support page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	 * tables.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	if (n == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 		orb->request.data_descriptor.high =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 			cpu_to_be32(lu->tgt->address_high);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 		orb->request.data_descriptor.low  =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 			cpu_to_be32(sg_dma_address(sg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 		orb->request.misc |=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 			cpu_to_be32(COMMAND_ORB_DATA_SIZE(sg_dma_len(sg)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	for_each_sg(sg, sg, n, i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 		orb->page_table[i].high = cpu_to_be32(sg_dma_len(sg) << 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 		orb->page_table[i].low = cpu_to_be32(sg_dma_address(sg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	orb->page_table_bus =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 		dma_map_single(device->card->device, orb->page_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 			       sizeof(orb->page_table), DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	if (dma_mapping_error(device->card->device, orb->page_table_bus))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 		goto fail_page_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	 * The data_descriptor pointer is the one case where we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	 * to fill in the node ID part of the address.  All other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	 * pointers assume that the data referenced reside on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 	 * initiator (i.e. us), but data_descriptor can refer to data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 	 * on other nodes so we need to put our ID in descriptor.high.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 	orb->request.data_descriptor.high = cpu_to_be32(lu->tgt->address_high);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	orb->request.data_descriptor.low  = cpu_to_be32(orb->page_table_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	orb->request.misc |= cpu_to_be32(COMMAND_ORB_PAGE_TABLE_PRESENT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 					 COMMAND_ORB_DATA_SIZE(n));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433)  fail_page_table:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	scsi_dma_unmap(orb->cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435)  fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) /* SCSI stack integration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) static int sbp2_scsi_queuecommand(struct Scsi_Host *shost,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 				  struct scsi_cmnd *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	struct sbp2_logical_unit *lu = cmd->device->hostdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	struct fw_device *device = target_parent_device(lu->tgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	struct sbp2_command_orb *orb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 	int generation, retval = SCSI_MLQUEUE_HOST_BUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	orb = kzalloc(sizeof(*orb), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	if (orb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 		return SCSI_MLQUEUE_HOST_BUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	/* Initialize rcode to something not RCODE_COMPLETE. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	orb->base.rcode = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	kref_init(&orb->base.kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	orb->cmd = cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	orb->request.next.high = cpu_to_be32(SBP2_ORB_NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	orb->request.misc = cpu_to_be32(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 		COMMAND_ORB_MAX_PAYLOAD(lu->tgt->max_payload) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 		COMMAND_ORB_SPEED(device->max_speed) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 		COMMAND_ORB_NOTIFY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 	if (cmd->sc_data_direction == DMA_FROM_DEVICE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 		orb->request.misc |= cpu_to_be32(COMMAND_ORB_DIRECTION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 	generation = device->generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	smp_rmb();    /* sbp2_map_scatterlist looks at tgt->address_high */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	if (scsi_sg_count(cmd) && sbp2_map_scatterlist(orb, device, lu) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	memcpy(orb->request.command_block, cmd->cmnd, cmd->cmd_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 	orb->base.callback = complete_command_orb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 	orb->base.request_bus =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 		dma_map_single(device->card->device, &orb->request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 			       sizeof(orb->request), DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	if (dma_mapping_error(device->card->device, orb->base.request_bus)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 		sbp2_unmap_scatterlist(device->card->device, orb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	sbp2_send_orb(&orb->base, lu, lu->tgt->node_id, generation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 		      lu->command_block_agent_address + SBP2_ORB_POINTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	kref_put(&orb->base.kref, free_orb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) static int sbp2_scsi_slave_alloc(struct scsi_device *sdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	struct sbp2_logical_unit *lu = sdev->hostdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	/* (Re-)Adding logical units via the SCSI stack is not supported. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	if (!lu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 		return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	sdev->allow_restart = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 	 * SBP-2 does not require any alignment, but we set it anyway
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	 * for compatibility with earlier versions of this driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 	blk_queue_update_dma_alignment(sdev->request_queue, 4 - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 	if (lu->tgt->workarounds & SBP2_WORKAROUND_INQUIRY_36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 		sdev->inquiry_len = 36;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) static int sbp2_scsi_slave_configure(struct scsi_device *sdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	struct sbp2_logical_unit *lu = sdev->hostdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	sdev->use_10_for_rw = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	if (sbp2_param_exclusive_login)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 		sdev->manage_start_stop = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	if (sdev->type == TYPE_ROM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 		sdev->use_10_for_ms = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	if (sdev->type == TYPE_DISK &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	    lu->tgt->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 		sdev->skip_ms_page_8 = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	if (lu->tgt->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 		sdev->fix_capacity = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	if (lu->tgt->workarounds & SBP2_WORKAROUND_POWER_CONDITION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 		sdev->start_stop_pwr_cond = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	if (lu->tgt->workarounds & SBP2_WORKAROUND_128K_MAX_TRANS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 		blk_queue_max_hw_sectors(sdev->request_queue, 128 * 1024 / 512);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542)  * Called by scsi stack when something has really gone wrong.  Usually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543)  * called when a command has timed-out for some reason.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) static int sbp2_scsi_abort(struct scsi_cmnd *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 	struct sbp2_logical_unit *lu = cmd->device->hostdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	dev_notice(lu_dev(lu), "sbp2_scsi_abort\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	sbp2_agent_reset(lu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	sbp2_cancel_orbs(lu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 	return SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557)  * Format of /sys/bus/scsi/devices/.../ieee1394_id:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558)  * u64 EUI-64 : u24 directory_ID : u16 LUN  (all printed in hexadecimal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560)  * This is the concatenation of target port identifier and logical unit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561)  * identifier as per SAM-2...SAM-4 annex A.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 			struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	struct scsi_device *sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	struct sbp2_logical_unit *lu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 	if (!sdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 	lu = sdev->hostdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	return sprintf(buf, "%016llx:%06x:%04x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 			(unsigned long long)lu->tgt->guid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 			lu->tgt->directory_id, lu->lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) static struct device_attribute *sbp2_scsi_sysfs_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	&dev_attr_ieee1394_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) static struct scsi_host_template scsi_driver_template = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	.module			= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	.name			= "SBP-2 IEEE-1394",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 	.proc_name		= "sbp2",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 	.queuecommand		= sbp2_scsi_queuecommand,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	.slave_alloc		= sbp2_scsi_slave_alloc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 	.slave_configure	= sbp2_scsi_slave_configure,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	.eh_abort_handler	= sbp2_scsi_abort,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	.this_id		= -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 	.sg_tablesize		= SG_ALL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	.max_segment_size	= SBP2_MAX_SEG_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 	.can_queue		= 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 	.sdev_attrs		= sbp2_scsi_sysfs_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) MODULE_DESCRIPTION("SCSI over IEEE1394");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) /* Provide a module alias so root-on-sbp2 initrds don't break. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) MODULE_ALIAS("sbp2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) static int __init sbp2_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	return driver_register(&sbp2_driver.driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) static void __exit sbp2_cleanup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 	driver_unregister(&sbp2_driver.driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) module_init(sbp2_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) module_exit(sbp2_cleanup);