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