Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * sbp2.c - SBP-2 protocol driver for IEEE-1394 | |
3 | * | |
4 | * Copyright (C) 2000 James Goodwin, Filanet Corporation (www.filanet.com) | |
5 | * jamesg@filanet.com (JSG) | |
6 | * | |
7 | * Copyright (C) 2003 Ben Collins <bcollins@debian.org> | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software Foundation, | |
21 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
22 | */ | |
23 | ||
24 | /* | |
25 | * Brief Description: | |
26 | * | |
27 | * This driver implements the Serial Bus Protocol 2 (SBP-2) over IEEE-1394 | |
28 | * under Linux. The SBP-2 driver is implemented as an IEEE-1394 high-level | |
29 | * driver. It also registers as a SCSI lower-level driver in order to accept | |
30 | * SCSI commands for transport using SBP-2. | |
31 | * | |
32 | * You may access any attached SBP-2 storage devices as if they were SCSI | |
33 | * devices (e.g. mount /dev/sda1, fdisk, mkfs, etc.). | |
34 | * | |
35 | * Current Issues: | |
36 | * | |
37 | * - Error Handling: SCSI aborts and bus reset requests are handled somewhat | |
38 | * but the code needs additional debugging. | |
39 | */ | |
40 | ||
41 | #include <linux/config.h> | |
42 | #include <linux/kernel.h> | |
43 | #include <linux/list.h> | |
44 | #include <linux/string.h> | |
24d3bf88 | 45 | #include <linux/stringify.h> |
1da177e4 LT |
46 | #include <linux/slab.h> |
47 | #include <linux/interrupt.h> | |
48 | #include <linux/fs.h> | |
49 | #include <linux/poll.h> | |
50 | #include <linux/module.h> | |
51 | #include <linux/moduleparam.h> | |
52 | #include <linux/types.h> | |
53 | #include <linux/delay.h> | |
54 | #include <linux/sched.h> | |
55 | #include <linux/blkdev.h> | |
56 | #include <linux/smp_lock.h> | |
57 | #include <linux/init.h> | |
58 | #include <linux/pci.h> | |
59 | ||
60 | #include <asm/current.h> | |
61 | #include <asm/uaccess.h> | |
62 | #include <asm/io.h> | |
63 | #include <asm/byteorder.h> | |
64 | #include <asm/atomic.h> | |
65 | #include <asm/system.h> | |
66 | #include <asm/scatterlist.h> | |
67 | ||
68 | #include <scsi/scsi.h> | |
69 | #include <scsi/scsi_cmnd.h> | |
70 | #include <scsi/scsi_dbg.h> | |
71 | #include <scsi/scsi_device.h> | |
72 | #include <scsi/scsi_host.h> | |
73 | ||
74 | #include "csr1212.h" | |
75 | #include "ieee1394.h" | |
76 | #include "ieee1394_types.h" | |
77 | #include "ieee1394_core.h" | |
78 | #include "nodemgr.h" | |
79 | #include "hosts.h" | |
80 | #include "highlevel.h" | |
81 | #include "ieee1394_transactions.h" | |
82 | #include "sbp2.h" | |
83 | ||
1da177e4 LT |
84 | /* |
85 | * Module load parameter definitions | |
86 | */ | |
87 | ||
88 | /* | |
89 | * Change max_speed on module load if you have a bad IEEE-1394 | |
90 | * controller that has trouble running 2KB packets at 400mb. | |
91 | * | |
92 | * NOTE: On certain OHCI parts I have seen short packets on async transmit | |
93 | * (probably due to PCI latency/throughput issues with the part). You can | |
94 | * bump down the speed if you are running into problems. | |
95 | */ | |
96 | static int max_speed = IEEE1394_SPEED_MAX; | |
97 | module_param(max_speed, int, 0644); | |
2bab359a | 98 | MODULE_PARM_DESC(max_speed, "Force max speed (3 = 800mb, 2 = 400mb, 1 = 200mb, 0 = 100mb)"); |
1da177e4 LT |
99 | |
100 | /* | |
101 | * Set serialize_io to 1 if you'd like only one scsi command sent | |
102 | * down to us at a time (debugging). This might be necessary for very | |
103 | * badly behaved sbp2 devices. | |
2bab359a JM |
104 | * |
105 | * TODO: Make this configurable per device. | |
1da177e4 | 106 | */ |
2bab359a | 107 | static int serialize_io = 1; |
1da177e4 | 108 | module_param(serialize_io, int, 0444); |
2bab359a | 109 | MODULE_PARM_DESC(serialize_io, "Serialize I/O coming from scsi drivers (default = 1, faster = 0)"); |
1da177e4 LT |
110 | |
111 | /* | |
112 | * Bump up max_sectors if you'd like to support very large sized | |
113 | * transfers. Please note that some older sbp2 bridge chips are broken for | |
114 | * transfers greater or equal to 128KB. Default is a value of 255 | |
115 | * sectors, or just under 128KB (at 512 byte sector size). I can note that | |
116 | * the Oxsemi sbp2 chipsets have no problems supporting very large | |
117 | * transfer sizes. | |
118 | */ | |
119 | static int max_sectors = SBP2_MAX_SECTORS; | |
120 | module_param(max_sectors, int, 0444); | |
24d3bf88 SR |
121 | MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported (default = " |
122 | __stringify(SBP2_MAX_SECTORS) ")"); | |
1da177e4 LT |
123 | |
124 | /* | |
125 | * Exclusive login to sbp2 device? In most cases, the sbp2 driver should | |
126 | * do an exclusive login, as it's generally unsafe to have two hosts | |
127 | * talking to a single sbp2 device at the same time (filesystem coherency, | |
128 | * etc.). If you're running an sbp2 device that supports multiple logins, | |
129 | * and you're either running read-only filesystems or some sort of special | |
20f45781 BC |
130 | * filesystem supporting multiple hosts, e.g. OpenGFS, Oracle Cluster |
131 | * File System, or Lustre, then set exclusive_login to zero. | |
132 | * | |
133 | * So far only bridges from Oxford Semiconductor are known to support | |
134 | * concurrent logins. Depending on firmware, four or two concurrent logins | |
135 | * are possible on OXFW911 and newer Oxsemi bridges. | |
1da177e4 LT |
136 | */ |
137 | static int exclusive_login = 1; | |
138 | module_param(exclusive_login, int, 0644); | |
139 | MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device (default = 1)"); | |
140 | ||
141 | /* | |
24d3bf88 SR |
142 | * If any of the following workarounds is required for your device to work, |
143 | * please submit the kernel messages logged by sbp2 to the linux1394-devel | |
144 | * mailing list. | |
1da177e4 | 145 | * |
24d3bf88 SR |
146 | * - 128kB max transfer |
147 | * Limit transfer size. Necessary for some old bridges. | |
148 | * | |
149 | * - 36 byte inquiry | |
150 | * When scsi_mod probes the device, let the inquiry command look like that | |
151 | * from MS Windows. | |
152 | * | |
153 | * - skip mode page 8 | |
154 | * Suppress sending of mode_sense for mode page 8 if the device pretends to | |
155 | * support the SCSI Primary Block commands instead of Reduced Block Commands. | |
e9a1c52c SR |
156 | * |
157 | * - fix capacity | |
158 | * Tell sd_mod to correct the last sector number reported by read_capacity. | |
159 | * Avoids access beyond actual disk limits on devices with an off-by-one bug. | |
160 | * Don't use this with devices which don't have this bug. | |
679c0cd2 SR |
161 | * |
162 | * - override internal blacklist | |
163 | * Instead of adding to the built-in blacklist, use only the workarounds | |
164 | * specified in the module load parameter. | |
165 | * Useful if a blacklist entry interfered with a non-broken device. | |
1da177e4 | 166 | */ |
24d3bf88 SR |
167 | static int sbp2_default_workarounds; |
168 | module_param_named(workarounds, sbp2_default_workarounds, int, 0644); | |
169 | MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0" | |
170 | ", 128kB max transfer = " __stringify(SBP2_WORKAROUND_128K_MAX_TRANS) | |
171 | ", 36 byte inquiry = " __stringify(SBP2_WORKAROUND_INQUIRY_36) | |
172 | ", skip mode page 8 = " __stringify(SBP2_WORKAROUND_MODE_SENSE_8) | |
e9a1c52c | 173 | ", fix capacity = " __stringify(SBP2_WORKAROUND_FIX_CAPACITY) |
679c0cd2 | 174 | ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE) |
24d3bf88 SR |
175 | ", or a combination)"); |
176 | ||
177 | /* legacy parameter */ | |
1934b8b6 | 178 | static int force_inquiry_hack; |
a80614d1 | 179 | module_param(force_inquiry_hack, int, 0644); |
24d3bf88 | 180 | MODULE_PARM_DESC(force_inquiry_hack, "Deprecated, use 'workarounds'"); |
1da177e4 | 181 | |
1da177e4 LT |
182 | /* |
183 | * Export information about protocols/devices supported by this driver. | |
184 | */ | |
185 | static struct ieee1394_device_id sbp2_id_table[] = { | |
186 | { | |
a237f35f SR |
187 | .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION, |
188 | .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY & 0xffffff, | |
189 | .version = SBP2_SW_VERSION_ENTRY & 0xffffff}, | |
190 | {} | |
1da177e4 LT |
191 | }; |
192 | ||
193 | MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table); | |
194 | ||
195 | /* | |
196 | * Debug levels, configured via kernel config, or enable here. | |
197 | */ | |
198 | ||
44456d37 | 199 | #define CONFIG_IEEE1394_SBP2_DEBUG 0 |
1da177e4 LT |
200 | /* #define CONFIG_IEEE1394_SBP2_DEBUG_ORBS */ |
201 | /* #define CONFIG_IEEE1394_SBP2_DEBUG_DMA */ | |
202 | /* #define CONFIG_IEEE1394_SBP2_DEBUG 1 */ | |
203 | /* #define CONFIG_IEEE1394_SBP2_DEBUG 2 */ | |
204 | /* #define CONFIG_IEEE1394_SBP2_PACKET_DUMP */ | |
205 | ||
206 | #ifdef CONFIG_IEEE1394_SBP2_DEBUG_ORBS | |
207 | #define SBP2_ORB_DEBUG(fmt, args...) HPSB_ERR("sbp2(%s): "fmt, __FUNCTION__, ## args) | |
208 | static u32 global_outstanding_command_orbs = 0; | |
209 | #define outstanding_orb_incr global_outstanding_command_orbs++ | |
210 | #define outstanding_orb_decr global_outstanding_command_orbs-- | |
211 | #else | |
212 | #define SBP2_ORB_DEBUG(fmt, args...) | |
213 | #define outstanding_orb_incr | |
214 | #define outstanding_orb_decr | |
215 | #endif | |
216 | ||
217 | #ifdef CONFIG_IEEE1394_SBP2_DEBUG_DMA | |
218 | #define SBP2_DMA_ALLOC(fmt, args...) \ | |
219 | HPSB_ERR("sbp2(%s)alloc(%d): "fmt, __FUNCTION__, \ | |
220 | ++global_outstanding_dmas, ## args) | |
221 | #define SBP2_DMA_FREE(fmt, args...) \ | |
222 | HPSB_ERR("sbp2(%s)free(%d): "fmt, __FUNCTION__, \ | |
223 | --global_outstanding_dmas, ## args) | |
224 | static u32 global_outstanding_dmas = 0; | |
225 | #else | |
226 | #define SBP2_DMA_ALLOC(fmt, args...) | |
227 | #define SBP2_DMA_FREE(fmt, args...) | |
228 | #endif | |
229 | ||
230 | #if CONFIG_IEEE1394_SBP2_DEBUG >= 2 | |
231 | #define SBP2_DEBUG(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args) | |
232 | #define SBP2_INFO(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args) | |
233 | #define SBP2_NOTICE(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args) | |
234 | #define SBP2_WARN(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args) | |
235 | #elif CONFIG_IEEE1394_SBP2_DEBUG == 1 | |
236 | #define SBP2_DEBUG(fmt, args...) HPSB_DEBUG("sbp2: "fmt, ## args) | |
237 | #define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args) | |
238 | #define SBP2_NOTICE(fmt, args...) HPSB_NOTICE("sbp2: "fmt, ## args) | |
239 | #define SBP2_WARN(fmt, args...) HPSB_WARN("sbp2: "fmt, ## args) | |
240 | #else | |
241 | #define SBP2_DEBUG(fmt, args...) | |
242 | #define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args) | |
243 | #define SBP2_NOTICE(fmt, args...) HPSB_NOTICE("sbp2: "fmt, ## args) | |
244 | #define SBP2_WARN(fmt, args...) HPSB_WARN("sbp2: "fmt, ## args) | |
245 | #endif | |
246 | ||
247 | #define SBP2_ERR(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args) | |
d024ebc6 | 248 | #define SBP2_DEBUG_ENTER() SBP2_DEBUG("%s", __FUNCTION__) |
1da177e4 | 249 | |
1da177e4 LT |
250 | /* |
251 | * Globals | |
252 | */ | |
253 | ||
254 | static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id, | |
255 | u32 status); | |
256 | ||
257 | static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id, | |
258 | u32 scsi_status, struct scsi_cmnd *SCpnt, | |
259 | void (*done)(struct scsi_cmnd *)); | |
260 | ||
261 | static struct scsi_host_template scsi_driver_template; | |
262 | ||
263 | static const u8 sbp2_speedto_max_payload[] = { 0x7, 0x8, 0x9, 0xA, 0xB, 0xC }; | |
264 | ||
265 | static void sbp2_host_reset(struct hpsb_host *host); | |
266 | ||
267 | static int sbp2_probe(struct device *dev); | |
268 | static int sbp2_remove(struct device *dev); | |
269 | static int sbp2_update(struct unit_directory *ud); | |
270 | ||
271 | static struct hpsb_highlevel sbp2_highlevel = { | |
272 | .name = SBP2_DEVICE_NAME, | |
273 | .host_reset = sbp2_host_reset, | |
274 | }; | |
275 | ||
276 | static struct hpsb_address_ops sbp2_ops = { | |
277 | .write = sbp2_handle_status_write | |
278 | }; | |
279 | ||
280 | #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA | |
281 | static struct hpsb_address_ops sbp2_physdma_ops = { | |
a237f35f SR |
282 | .read = sbp2_handle_physdma_read, |
283 | .write = sbp2_handle_physdma_write, | |
1da177e4 LT |
284 | }; |
285 | #endif | |
286 | ||
287 | static struct hpsb_protocol_driver sbp2_driver = { | |
288 | .name = "SBP2 Driver", | |
289 | .id_table = sbp2_id_table, | |
290 | .update = sbp2_update, | |
291 | .driver = { | |
292 | .name = SBP2_DEVICE_NAME, | |
293 | .bus = &ieee1394_bus_type, | |
294 | .probe = sbp2_probe, | |
295 | .remove = sbp2_remove, | |
296 | }, | |
297 | }; | |
298 | ||
a80614d1 | 299 | /* |
24d3bf88 SR |
300 | * List of devices with known bugs. |
301 | * | |
302 | * The firmware_revision field, masked with 0xffff00, is the best indicator | |
303 | * for the type of bridge chip of a device. It yields a few false positives | |
304 | * but this did not break correctly behaving devices so far. | |
a80614d1 | 305 | */ |
24d3bf88 SR |
306 | static const struct { |
307 | u32 firmware_revision; | |
e9a1c52c | 308 | u32 model_id; |
24d3bf88 SR |
309 | unsigned workarounds; |
310 | } sbp2_workarounds_table[] = { | |
4b9a3347 | 311 | /* DViCO Momobay CX-1 with TSB42AA9 bridge */ { |
24d3bf88 | 312 | .firmware_revision = 0x002800, |
4b9a3347 | 313 | .model_id = 0x001010, |
24d3bf88 SR |
314 | .workarounds = SBP2_WORKAROUND_INQUIRY_36 | |
315 | SBP2_WORKAROUND_MODE_SENSE_8, | |
316 | }, | |
317 | /* Initio bridges, actually only needed for some older ones */ { | |
318 | .firmware_revision = 0x000200, | |
319 | .workarounds = SBP2_WORKAROUND_INQUIRY_36, | |
320 | }, | |
321 | /* Symbios bridge */ { | |
322 | .firmware_revision = 0xa0b800, | |
323 | .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS, | |
e9a1c52c SR |
324 | }, |
325 | /* | |
326 | * Note about the following Apple iPod blacklist entries: | |
327 | * | |
328 | * There are iPods (2nd gen, 3rd gen) with model_id==0. Since our | |
329 | * matching logic treats 0 as a wildcard, we cannot match this ID | |
330 | * without rewriting the matching routine. Fortunately these iPods | |
331 | * do not feature the read_capacity bug according to one report. | |
332 | * Read_capacity behaviour as well as model_id could change due to | |
333 | * Apple-supplied firmware updates though. | |
334 | */ | |
335 | /* iPod 4th generation */ { | |
336 | .firmware_revision = 0x0a2700, | |
337 | .model_id = 0x000021, | |
338 | .workarounds = SBP2_WORKAROUND_FIX_CAPACITY, | |
339 | }, | |
340 | /* iPod mini */ { | |
341 | .firmware_revision = 0x0a2700, | |
342 | .model_id = 0x000023, | |
343 | .workarounds = SBP2_WORKAROUND_FIX_CAPACITY, | |
344 | }, | |
345 | /* iPod Photo */ { | |
346 | .firmware_revision = 0x0a2700, | |
347 | .model_id = 0x00007e, | |
348 | .workarounds = SBP2_WORKAROUND_FIX_CAPACITY, | |
24d3bf88 | 349 | } |
1da177e4 LT |
350 | }; |
351 | ||
1da177e4 LT |
352 | /************************************** |
353 | * General utility functions | |
354 | **************************************/ | |
355 | ||
1da177e4 LT |
356 | #ifndef __BIG_ENDIAN |
357 | /* | |
358 | * Converts a buffer from be32 to cpu byte ordering. Length is in bytes. | |
359 | */ | |
360 | static __inline__ void sbp2util_be32_to_cpu_buffer(void *buffer, int length) | |
361 | { | |
362 | u32 *temp = buffer; | |
363 | ||
364 | for (length = (length >> 2); length--; ) | |
365 | temp[length] = be32_to_cpu(temp[length]); | |
366 | ||
367 | return; | |
368 | } | |
369 | ||
370 | /* | |
371 | * Converts a buffer from cpu to be32 byte ordering. Length is in bytes. | |
372 | */ | |
373 | static __inline__ void sbp2util_cpu_to_be32_buffer(void *buffer, int length) | |
374 | { | |
375 | u32 *temp = buffer; | |
376 | ||
377 | for (length = (length >> 2); length--; ) | |
378 | temp[length] = cpu_to_be32(temp[length]); | |
379 | ||
380 | return; | |
381 | } | |
382 | #else /* BIG_ENDIAN */ | |
383 | /* Why waste the cpu cycles? */ | |
384 | #define sbp2util_be32_to_cpu_buffer(x,y) | |
385 | #define sbp2util_cpu_to_be32_buffer(x,y) | |
386 | #endif | |
387 | ||
388 | #ifdef CONFIG_IEEE1394_SBP2_PACKET_DUMP | |
389 | /* | |
390 | * Debug packet dump routine. Length is in bytes. | |
391 | */ | |
a237f35f SR |
392 | static void sbp2util_packet_dump(void *buffer, int length, char *dump_name, |
393 | u32 dump_phys_addr) | |
1da177e4 LT |
394 | { |
395 | int i; | |
396 | unsigned char *dump = buffer; | |
397 | ||
398 | if (!dump || !length || !dump_name) | |
399 | return; | |
400 | ||
401 | if (dump_phys_addr) | |
402 | printk("[%s, 0x%x]", dump_name, dump_phys_addr); | |
403 | else | |
404 | printk("[%s]", dump_name); | |
405 | for (i = 0; i < length; i++) { | |
406 | if (i > 0x3f) { | |
407 | printk("\n ..."); | |
408 | break; | |
409 | } | |
410 | if ((i & 0x3) == 0) | |
411 | printk(" "); | |
412 | if ((i & 0xf) == 0) | |
413 | printk("\n "); | |
a237f35f | 414 | printk("%02x ", (int)dump[i]); |
1da177e4 LT |
415 | } |
416 | printk("\n"); | |
417 | ||
418 | return; | |
419 | } | |
420 | #else | |
421 | #define sbp2util_packet_dump(w,x,y,z) | |
422 | #endif | |
423 | ||
424 | /* | |
425 | * Goofy routine that basically does a down_timeout function. | |
426 | */ | |
427 | static int sbp2util_down_timeout(atomic_t *done, int timeout) | |
428 | { | |
429 | int i; | |
430 | ||
431 | for (i = timeout; (i > 0 && atomic_read(done) == 0); i-= HZ/10) { | |
432 | if (msleep_interruptible(100)) /* 100ms */ | |
a237f35f | 433 | return 1; |
1da177e4 | 434 | } |
a237f35f | 435 | return (i > 0) ? 0 : 1; |
1da177e4 LT |
436 | } |
437 | ||
438 | /* Free's an allocated packet */ | |
439 | static void sbp2_free_packet(struct hpsb_packet *packet) | |
440 | { | |
441 | hpsb_free_tlabel(packet); | |
442 | hpsb_free_packet(packet); | |
443 | } | |
444 | ||
445 | /* This is much like hpsb_node_write(), except it ignores the response | |
446 | * subaction and returns immediately. Can be used from interrupts. | |
447 | */ | |
448 | static int sbp2util_node_write_no_wait(struct node_entry *ne, u64 addr, | |
a237f35f | 449 | quadlet_t *buffer, size_t length) |
1da177e4 LT |
450 | { |
451 | struct hpsb_packet *packet; | |
452 | ||
453 | packet = hpsb_make_writepacket(ne->host, ne->nodeid, | |
454 | addr, buffer, length); | |
a237f35f SR |
455 | if (!packet) |
456 | return -ENOMEM; | |
1da177e4 | 457 | |
a237f35f SR |
458 | hpsb_set_packet_complete_task(packet, |
459 | (void (*)(void *))sbp2_free_packet, | |
1da177e4 LT |
460 | packet); |
461 | ||
462 | hpsb_node_fill_packet(ne, packet); | |
463 | ||
a237f35f | 464 | if (hpsb_send_packet(packet) < 0) { |
1da177e4 LT |
465 | sbp2_free_packet(packet); |
466 | return -EIO; | |
467 | } | |
468 | ||
469 | return 0; | |
470 | } | |
471 | ||
472 | /* | |
473 | * This function is called to create a pool of command orbs used for | |
474 | * command processing. It is called when a new sbp2 device is detected. | |
475 | */ | |
476 | static int sbp2util_create_command_orb_pool(struct scsi_id_instance_data *scsi_id) | |
477 | { | |
478 | struct sbp2scsi_host_info *hi = scsi_id->hi; | |
479 | int i; | |
480 | unsigned long flags, orbs; | |
481 | struct sbp2_command_info *command; | |
482 | ||
483 | orbs = serialize_io ? 2 : SBP2_MAX_CMDS; | |
484 | ||
485 | spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags); | |
486 | for (i = 0; i < orbs; i++) { | |
8551158a | 487 | command = kzalloc(sizeof(*command), GFP_ATOMIC); |
1da177e4 | 488 | if (!command) { |
a237f35f SR |
489 | spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, |
490 | flags); | |
491 | return -ENOMEM; | |
1da177e4 | 492 | } |
1da177e4 | 493 | command->command_orb_dma = |
a237f35f SR |
494 | pci_map_single(hi->host->pdev, &command->command_orb, |
495 | sizeof(struct sbp2_command_orb), | |
496 | PCI_DMA_BIDIRECTIONAL); | |
1da177e4 LT |
497 | SBP2_DMA_ALLOC("single command orb DMA"); |
498 | command->sge_dma = | |
a237f35f SR |
499 | pci_map_single(hi->host->pdev, |
500 | &command->scatter_gather_element, | |
501 | sizeof(command->scatter_gather_element), | |
502 | PCI_DMA_BIDIRECTIONAL); | |
1da177e4 LT |
503 | SBP2_DMA_ALLOC("scatter_gather_element"); |
504 | INIT_LIST_HEAD(&command->list); | |
505 | list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed); | |
506 | } | |
507 | spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags); | |
508 | return 0; | |
509 | } | |
510 | ||
511 | /* | |
512 | * This function is called to delete a pool of command orbs. | |
513 | */ | |
514 | static void sbp2util_remove_command_orb_pool(struct scsi_id_instance_data *scsi_id) | |
515 | { | |
516 | struct hpsb_host *host = scsi_id->hi->host; | |
517 | struct list_head *lh, *next; | |
518 | struct sbp2_command_info *command; | |
519 | unsigned long flags; | |
520 | ||
521 | spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags); | |
522 | if (!list_empty(&scsi_id->sbp2_command_orb_completed)) { | |
523 | list_for_each_safe(lh, next, &scsi_id->sbp2_command_orb_completed) { | |
524 | command = list_entry(lh, struct sbp2_command_info, list); | |
525 | ||
526 | /* Release our generic DMA's */ | |
527 | pci_unmap_single(host->pdev, command->command_orb_dma, | |
528 | sizeof(struct sbp2_command_orb), | |
529 | PCI_DMA_BIDIRECTIONAL); | |
530 | SBP2_DMA_FREE("single command orb DMA"); | |
531 | pci_unmap_single(host->pdev, command->sge_dma, | |
532 | sizeof(command->scatter_gather_element), | |
533 | PCI_DMA_BIDIRECTIONAL); | |
534 | SBP2_DMA_FREE("scatter_gather_element"); | |
535 | ||
536 | kfree(command); | |
537 | } | |
538 | } | |
539 | spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags); | |
540 | return; | |
541 | } | |
542 | ||
543 | /* | |
544 | * This function finds the sbp2_command for a given outstanding command | |
545 | * orb.Only looks at the inuse list. | |
546 | */ | |
547 | static struct sbp2_command_info *sbp2util_find_command_for_orb( | |
548 | struct scsi_id_instance_data *scsi_id, dma_addr_t orb) | |
549 | { | |
550 | struct sbp2_command_info *command; | |
551 | unsigned long flags; | |
552 | ||
553 | spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags); | |
554 | if (!list_empty(&scsi_id->sbp2_command_orb_inuse)) { | |
555 | list_for_each_entry(command, &scsi_id->sbp2_command_orb_inuse, list) { | |
556 | if (command->command_orb_dma == orb) { | |
557 | spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags); | |
a237f35f | 558 | return command; |
1da177e4 LT |
559 | } |
560 | } | |
561 | } | |
562 | spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags); | |
563 | ||
564 | SBP2_ORB_DEBUG("could not match command orb %x", (unsigned int)orb); | |
565 | ||
a237f35f | 566 | return NULL; |
1da177e4 LT |
567 | } |
568 | ||
569 | /* | |
570 | * This function finds the sbp2_command for a given outstanding SCpnt. | |
571 | * Only looks at the inuse list. | |
24c7cd06 | 572 | * Must be called with scsi_id->sbp2_command_orb_lock held. |
1da177e4 | 573 | */ |
24c7cd06 SR |
574 | static struct sbp2_command_info *sbp2util_find_command_for_SCpnt( |
575 | struct scsi_id_instance_data *scsi_id, void *SCpnt) | |
1da177e4 LT |
576 | { |
577 | struct sbp2_command_info *command; | |
1da177e4 | 578 | |
24c7cd06 SR |
579 | if (!list_empty(&scsi_id->sbp2_command_orb_inuse)) |
580 | list_for_each_entry(command, &scsi_id->sbp2_command_orb_inuse, list) | |
581 | if (command->Current_SCpnt == SCpnt) | |
a237f35f | 582 | return command; |
a237f35f | 583 | return NULL; |
1da177e4 LT |
584 | } |
585 | ||
586 | /* | |
587 | * This function allocates a command orb used to send a scsi command. | |
588 | */ | |
589 | static struct sbp2_command_info *sbp2util_allocate_command_orb( | |
590 | struct scsi_id_instance_data *scsi_id, | |
591 | struct scsi_cmnd *Current_SCpnt, | |
592 | void (*Current_done)(struct scsi_cmnd *)) | |
593 | { | |
594 | struct list_head *lh; | |
595 | struct sbp2_command_info *command = NULL; | |
596 | unsigned long flags; | |
597 | ||
598 | spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags); | |
599 | if (!list_empty(&scsi_id->sbp2_command_orb_completed)) { | |
600 | lh = scsi_id->sbp2_command_orb_completed.next; | |
601 | list_del(lh); | |
602 | command = list_entry(lh, struct sbp2_command_info, list); | |
603 | command->Current_done = Current_done; | |
604 | command->Current_SCpnt = Current_SCpnt; | |
605 | list_add_tail(&command->list, &scsi_id->sbp2_command_orb_inuse); | |
606 | } else { | |
d024ebc6 | 607 | SBP2_ERR("%s: no orbs available", __FUNCTION__); |
1da177e4 LT |
608 | } |
609 | spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags); | |
a237f35f | 610 | return command; |
1da177e4 LT |
611 | } |
612 | ||
613 | /* Free our DMA's */ | |
614 | static void sbp2util_free_command_dma(struct sbp2_command_info *command) | |
615 | { | |
616 | struct scsi_id_instance_data *scsi_id = | |
617 | (struct scsi_id_instance_data *)command->Current_SCpnt->device->host->hostdata[0]; | |
618 | struct hpsb_host *host; | |
619 | ||
620 | if (!scsi_id) { | |
d024ebc6 | 621 | SBP2_ERR("%s: scsi_id == NULL", __FUNCTION__); |
1da177e4 LT |
622 | return; |
623 | } | |
624 | ||
625 | host = scsi_id->ud->ne->host; | |
626 | ||
627 | if (command->cmd_dma) { | |
628 | if (command->dma_type == CMD_DMA_SINGLE) { | |
629 | pci_unmap_single(host->pdev, command->cmd_dma, | |
630 | command->dma_size, command->dma_dir); | |
631 | SBP2_DMA_FREE("single bulk"); | |
632 | } else if (command->dma_type == CMD_DMA_PAGE) { | |
633 | pci_unmap_page(host->pdev, command->cmd_dma, | |
634 | command->dma_size, command->dma_dir); | |
635 | SBP2_DMA_FREE("single page"); | |
636 | } /* XXX: Check for CMD_DMA_NONE bug */ | |
637 | command->dma_type = CMD_DMA_NONE; | |
638 | command->cmd_dma = 0; | |
639 | } | |
640 | ||
641 | if (command->sge_buffer) { | |
642 | pci_unmap_sg(host->pdev, command->sge_buffer, | |
643 | command->dma_size, command->dma_dir); | |
644 | SBP2_DMA_FREE("scatter list"); | |
645 | command->sge_buffer = NULL; | |
646 | } | |
647 | } | |
648 | ||
649 | /* | |
650 | * This function moves a command to the completed orb list. | |
24c7cd06 | 651 | * Must be called with scsi_id->sbp2_command_orb_lock held. |
1da177e4 | 652 | */ |
24c7cd06 SR |
653 | static void sbp2util_mark_command_completed( |
654 | struct scsi_id_instance_data *scsi_id, | |
655 | struct sbp2_command_info *command) | |
1da177e4 | 656 | { |
1da177e4 LT |
657 | list_del(&command->list); |
658 | sbp2util_free_command_dma(command); | |
659 | list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed); | |
1da177e4 LT |
660 | } |
661 | ||
abd559b1 JM |
662 | /* |
663 | * Is scsi_id valid? Is the 1394 node still present? | |
664 | */ | |
665 | static inline int sbp2util_node_is_available(struct scsi_id_instance_data *scsi_id) | |
666 | { | |
667 | return scsi_id && scsi_id->ne && !scsi_id->ne->in_limbo; | |
668 | } | |
669 | ||
1da177e4 LT |
670 | /********************************************* |
671 | * IEEE-1394 core driver stack related section | |
672 | *********************************************/ | |
673 | static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud); | |
674 | ||
675 | static int sbp2_probe(struct device *dev) | |
676 | { | |
677 | struct unit_directory *ud; | |
678 | struct scsi_id_instance_data *scsi_id; | |
679 | ||
d024ebc6 | 680 | SBP2_DEBUG_ENTER(); |
1da177e4 LT |
681 | |
682 | ud = container_of(dev, struct unit_directory, device); | |
683 | ||
684 | /* Don't probe UD's that have the LUN flag. We'll probe the LUN(s) | |
685 | * instead. */ | |
686 | if (ud->flags & UNIT_DIRECTORY_HAS_LUN_DIRECTORY) | |
687 | return -ENODEV; | |
688 | ||
a237f35f | 689 | scsi_id = sbp2_alloc_device(ud); |
1da177e4 | 690 | |
a237f35f SR |
691 | if (!scsi_id) |
692 | return -ENOMEM; | |
1da177e4 | 693 | |
a237f35f | 694 | sbp2_parse_unit_directory(scsi_id, ud); |
1da177e4 | 695 | |
a237f35f | 696 | return sbp2_start_device(scsi_id); |
1da177e4 LT |
697 | } |
698 | ||
699 | static int sbp2_remove(struct device *dev) | |
700 | { | |
701 | struct unit_directory *ud; | |
702 | struct scsi_id_instance_data *scsi_id; | |
abd559b1 | 703 | struct scsi_device *sdev; |
1da177e4 | 704 | |
d024ebc6 | 705 | SBP2_DEBUG_ENTER(); |
1da177e4 LT |
706 | |
707 | ud = container_of(dev, struct unit_directory, device); | |
708 | scsi_id = ud->device.driver_data; | |
abd559b1 JM |
709 | if (!scsi_id) |
710 | return 0; | |
711 | ||
bf637ec3 SR |
712 | if (scsi_id->scsi_host) { |
713 | /* Get rid of enqueued commands if there is no chance to | |
714 | * send them. */ | |
715 | if (!sbp2util_node_is_available(scsi_id)) | |
716 | sbp2scsi_complete_all_commands(scsi_id, DID_NO_CONNECT); | |
717 | /* scsi_remove_device() will trigger shutdown functions of SCSI | |
718 | * highlevel drivers which would deadlock if blocked. */ | |
abd559b1 | 719 | scsi_unblock_requests(scsi_id->scsi_host); |
bf637ec3 | 720 | } |
abd559b1 JM |
721 | sdev = scsi_id->sdev; |
722 | if (sdev) { | |
723 | scsi_id->sdev = NULL; | |
724 | scsi_remove_device(sdev); | |
725 | } | |
1da177e4 LT |
726 | |
727 | sbp2_logout_device(scsi_id); | |
728 | sbp2_remove_device(scsi_id); | |
729 | ||
730 | return 0; | |
731 | } | |
732 | ||
733 | static int sbp2_update(struct unit_directory *ud) | |
734 | { | |
735 | struct scsi_id_instance_data *scsi_id = ud->device.driver_data; | |
736 | ||
d024ebc6 | 737 | SBP2_DEBUG_ENTER(); |
1da177e4 LT |
738 | |
739 | if (sbp2_reconnect_device(scsi_id)) { | |
740 | ||
741 | /* | |
742 | * Ok, reconnect has failed. Perhaps we didn't | |
743 | * reconnect fast enough. Try doing a regular login, but | |
744 | * first do a logout just in case of any weirdness. | |
745 | */ | |
746 | sbp2_logout_device(scsi_id); | |
747 | ||
748 | if (sbp2_login_device(scsi_id)) { | |
749 | /* Login failed too, just fail, and the backend | |
750 | * will call our sbp2_remove for us */ | |
751 | SBP2_ERR("Failed to reconnect to sbp2 device!"); | |
752 | return -EBUSY; | |
753 | } | |
754 | } | |
755 | ||
756 | /* Set max retries to something large on the device. */ | |
757 | sbp2_set_busy_timeout(scsi_id); | |
758 | ||
759 | /* Do a SBP-2 fetch agent reset. */ | |
760 | sbp2_agent_reset(scsi_id, 1); | |
761 | ||
762 | /* Get the max speed and packet size that we can use. */ | |
763 | sbp2_max_speed_and_size(scsi_id); | |
764 | ||
765 | /* Complete any pending commands with busy (so they get | |
766 | * retried) and remove them from our queue | |
767 | */ | |
768 | sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY); | |
769 | ||
770 | /* Make sure we unblock requests (since this is likely after a bus | |
771 | * reset). */ | |
772 | scsi_unblock_requests(scsi_id->scsi_host); | |
773 | ||
774 | return 0; | |
775 | } | |
776 | ||
777 | /* This functions is called by the sbp2_probe, for each new device. We now | |
778 | * allocate one scsi host for each scsi_id (unit directory). */ | |
779 | static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud) | |
780 | { | |
781 | struct sbp2scsi_host_info *hi; | |
782 | struct Scsi_Host *scsi_host = NULL; | |
783 | struct scsi_id_instance_data *scsi_id = NULL; | |
784 | ||
d024ebc6 | 785 | SBP2_DEBUG_ENTER(); |
1da177e4 | 786 | |
8551158a | 787 | scsi_id = kzalloc(sizeof(*scsi_id), GFP_KERNEL); |
1da177e4 LT |
788 | if (!scsi_id) { |
789 | SBP2_ERR("failed to create scsi_id"); | |
790 | goto failed_alloc; | |
791 | } | |
1da177e4 LT |
792 | |
793 | scsi_id->ne = ud->ne; | |
794 | scsi_id->ud = ud; | |
795 | scsi_id->speed_code = IEEE1394_SPEED_100; | |
796 | scsi_id->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100]; | |
797 | atomic_set(&scsi_id->sbp2_login_complete, 0); | |
798 | INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_inuse); | |
799 | INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_completed); | |
800 | INIT_LIST_HEAD(&scsi_id->scsi_list); | |
801 | spin_lock_init(&scsi_id->sbp2_command_orb_lock); | |
e309fc6d | 802 | scsi_id->sbp2_lun = 0; |
1da177e4 LT |
803 | |
804 | ud->device.driver_data = scsi_id; | |
805 | ||
806 | hi = hpsb_get_hostinfo(&sbp2_highlevel, ud->ne->host); | |
807 | if (!hi) { | |
808 | hi = hpsb_create_hostinfo(&sbp2_highlevel, ud->ne->host, sizeof(*hi)); | |
809 | if (!hi) { | |
810 | SBP2_ERR("failed to allocate hostinfo"); | |
811 | goto failed_alloc; | |
812 | } | |
813 | SBP2_DEBUG("sbp2_alloc_device: allocated hostinfo"); | |
814 | hi->host = ud->ne->host; | |
815 | INIT_LIST_HEAD(&hi->scsi_ids); | |
816 | ||
1da177e4 LT |
817 | #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA |
818 | /* Handle data movement if physical dma is not | |
55664051 SR |
819 | * enabled or not supported on host controller */ |
820 | if (!hpsb_register_addrspace(&sbp2_highlevel, ud->ne->host, | |
821 | &sbp2_physdma_ops, | |
822 | 0x0ULL, 0xfffffffcULL)) { | |
823 | SBP2_ERR("failed to register lower 4GB address range"); | |
824 | goto failed_alloc; | |
825 | } | |
1da177e4 LT |
826 | #endif |
827 | } | |
828 | ||
147830f2 SR |
829 | /* Prevent unloading of the 1394 host */ |
830 | if (!try_module_get(hi->host->driver->owner)) { | |
831 | SBP2_ERR("failed to get a reference on 1394 host driver"); | |
832 | goto failed_alloc; | |
833 | } | |
834 | ||
1da177e4 LT |
835 | scsi_id->hi = hi; |
836 | ||
837 | list_add_tail(&scsi_id->scsi_list, &hi->scsi_ids); | |
838 | ||
35bdddb8 SR |
839 | /* Register the status FIFO address range. We could use the same FIFO |
840 | * for targets at different nodes. However we need different FIFOs per | |
a54c9d30 SR |
841 | * target in order to support multi-unit devices. |
842 | * The FIFO is located out of the local host controller's physical range | |
843 | * but, if possible, within the posted write area. Status writes will | |
844 | * then be performed as unified transactions. This slightly reduces | |
845 | * bandwidth usage, and some Prolific based devices seem to require it. | |
846 | */ | |
35bdddb8 SR |
847 | scsi_id->status_fifo_addr = hpsb_allocate_and_register_addrspace( |
848 | &sbp2_highlevel, ud->ne->host, &sbp2_ops, | |
849 | sizeof(struct sbp2_status_block), sizeof(quadlet_t), | |
a54c9d30 | 850 | 0x010000000000ULL, CSR1212_ALL_SPACE_END); |
829a1985 | 851 | if (scsi_id->status_fifo_addr == ~0ULL) { |
35bdddb8 SR |
852 | SBP2_ERR("failed to allocate status FIFO address range"); |
853 | goto failed_alloc; | |
854 | } | |
855 | ||
1da177e4 | 856 | /* Register our host with the SCSI stack. */ |
a2ef79e1 | 857 | scsi_host = scsi_host_alloc(&scsi_driver_template, |
a237f35f | 858 | sizeof(unsigned long)); |
1da177e4 LT |
859 | if (!scsi_host) { |
860 | SBP2_ERR("failed to register scsi host"); | |
861 | goto failed_alloc; | |
862 | } | |
863 | ||
864 | scsi_host->hostdata[0] = (unsigned long)scsi_id; | |
865 | ||
866 | if (!scsi_add_host(scsi_host, &ud->device)) { | |
867 | scsi_id->scsi_host = scsi_host; | |
868 | return scsi_id; | |
869 | } | |
870 | ||
871 | SBP2_ERR("failed to add scsi host"); | |
872 | scsi_host_put(scsi_host); | |
873 | ||
874 | failed_alloc: | |
875 | sbp2_remove_device(scsi_id); | |
876 | return NULL; | |
877 | } | |
878 | ||
1da177e4 LT |
879 | static void sbp2_host_reset(struct hpsb_host *host) |
880 | { | |
881 | struct sbp2scsi_host_info *hi; | |
882 | struct scsi_id_instance_data *scsi_id; | |
883 | ||
884 | hi = hpsb_get_hostinfo(&sbp2_highlevel, host); | |
885 | ||
886 | if (hi) { | |
887 | list_for_each_entry(scsi_id, &hi->scsi_ids, scsi_list) | |
888 | scsi_block_requests(scsi_id->scsi_host); | |
889 | } | |
890 | } | |
891 | ||
1da177e4 LT |
892 | /* |
893 | * This function is where we first pull the node unique ids, and then | |
894 | * allocate memory and register a SBP-2 device. | |
895 | */ | |
896 | static int sbp2_start_device(struct scsi_id_instance_data *scsi_id) | |
897 | { | |
898 | struct sbp2scsi_host_info *hi = scsi_id->hi; | |
146f7262 | 899 | int error; |
1da177e4 | 900 | |
d024ebc6 | 901 | SBP2_DEBUG_ENTER(); |
1da177e4 LT |
902 | |
903 | /* Login FIFO DMA */ | |
904 | scsi_id->login_response = | |
a237f35f SR |
905 | pci_alloc_consistent(hi->host->pdev, |
906 | sizeof(struct sbp2_login_response), | |
1da177e4 LT |
907 | &scsi_id->login_response_dma); |
908 | if (!scsi_id->login_response) | |
909 | goto alloc_fail; | |
910 | SBP2_DMA_ALLOC("consistent DMA region for login FIFO"); | |
911 | ||
912 | /* Query logins ORB DMA */ | |
913 | scsi_id->query_logins_orb = | |
a237f35f SR |
914 | pci_alloc_consistent(hi->host->pdev, |
915 | sizeof(struct sbp2_query_logins_orb), | |
1da177e4 LT |
916 | &scsi_id->query_logins_orb_dma); |
917 | if (!scsi_id->query_logins_orb) | |
918 | goto alloc_fail; | |
919 | SBP2_DMA_ALLOC("consistent DMA region for query logins ORB"); | |
920 | ||
921 | /* Query logins response DMA */ | |
922 | scsi_id->query_logins_response = | |
a237f35f SR |
923 | pci_alloc_consistent(hi->host->pdev, |
924 | sizeof(struct sbp2_query_logins_response), | |
1da177e4 LT |
925 | &scsi_id->query_logins_response_dma); |
926 | if (!scsi_id->query_logins_response) | |
927 | goto alloc_fail; | |
928 | SBP2_DMA_ALLOC("consistent DMA region for query logins response"); | |
929 | ||
930 | /* Reconnect ORB DMA */ | |
931 | scsi_id->reconnect_orb = | |
a237f35f SR |
932 | pci_alloc_consistent(hi->host->pdev, |
933 | sizeof(struct sbp2_reconnect_orb), | |
1da177e4 LT |
934 | &scsi_id->reconnect_orb_dma); |
935 | if (!scsi_id->reconnect_orb) | |
936 | goto alloc_fail; | |
937 | SBP2_DMA_ALLOC("consistent DMA region for reconnect ORB"); | |
938 | ||
939 | /* Logout ORB DMA */ | |
940 | scsi_id->logout_orb = | |
a237f35f SR |
941 | pci_alloc_consistent(hi->host->pdev, |
942 | sizeof(struct sbp2_logout_orb), | |
1da177e4 LT |
943 | &scsi_id->logout_orb_dma); |
944 | if (!scsi_id->logout_orb) | |
945 | goto alloc_fail; | |
946 | SBP2_DMA_ALLOC("consistent DMA region for logout ORB"); | |
947 | ||
948 | /* Login ORB DMA */ | |
949 | scsi_id->login_orb = | |
a237f35f SR |
950 | pci_alloc_consistent(hi->host->pdev, |
951 | sizeof(struct sbp2_login_orb), | |
1da177e4 | 952 | &scsi_id->login_orb_dma); |
eaceec7f SR |
953 | if (!scsi_id->login_orb) |
954 | goto alloc_fail; | |
1da177e4 LT |
955 | SBP2_DMA_ALLOC("consistent DMA region for login ORB"); |
956 | ||
957 | SBP2_DEBUG("New SBP-2 device inserted, SCSI ID = %x", scsi_id->ud->id); | |
958 | ||
959 | /* | |
960 | * Create our command orb pool | |
961 | */ | |
962 | if (sbp2util_create_command_orb_pool(scsi_id)) { | |
963 | SBP2_ERR("sbp2util_create_command_orb_pool failed!"); | |
964 | sbp2_remove_device(scsi_id); | |
965 | return -ENOMEM; | |
966 | } | |
967 | ||
968 | /* Schedule a timeout here. The reason is that we may be so close | |
969 | * to a bus reset, that the device is not available for logins. | |
970 | * This can happen when the bus reset is caused by the host | |
971 | * connected to the sbp2 device being removed. That host would | |
972 | * have a certain amount of time to relogin before the sbp2 device | |
973 | * allows someone else to login instead. One second makes sense. */ | |
974 | msleep_interruptible(1000); | |
975 | if (signal_pending(current)) { | |
1da177e4 LT |
976 | sbp2_remove_device(scsi_id); |
977 | return -EINTR; | |
978 | } | |
a237f35f | 979 | |
1da177e4 LT |
980 | /* |
981 | * Login to the sbp-2 device | |
982 | */ | |
983 | if (sbp2_login_device(scsi_id)) { | |
984 | /* Login failed, just remove the device. */ | |
985 | sbp2_remove_device(scsi_id); | |
986 | return -EBUSY; | |
987 | } | |
988 | ||
989 | /* | |
990 | * Set max retries to something large on the device | |
991 | */ | |
992 | sbp2_set_busy_timeout(scsi_id); | |
993 | ||
994 | /* | |
995 | * Do a SBP-2 fetch agent reset | |
996 | */ | |
997 | sbp2_agent_reset(scsi_id, 1); | |
998 | ||
999 | /* | |
1000 | * Get the max speed and packet size that we can use | |
1001 | */ | |
1002 | sbp2_max_speed_and_size(scsi_id); | |
1003 | ||
1004 | /* Add this device to the scsi layer now */ | |
146f7262 JB |
1005 | error = scsi_add_device(scsi_id->scsi_host, 0, scsi_id->ud->id, 0); |
1006 | if (error) { | |
1da177e4 | 1007 | SBP2_ERR("scsi_add_device failed"); |
dc3edd54 SR |
1008 | sbp2_logout_device(scsi_id); |
1009 | sbp2_remove_device(scsi_id); | |
146f7262 | 1010 | return error; |
1da177e4 LT |
1011 | } |
1012 | ||
1013 | return 0; | |
eaceec7f SR |
1014 | |
1015 | alloc_fail: | |
1016 | SBP2_ERR("Could not allocate memory for scsi_id"); | |
1017 | sbp2_remove_device(scsi_id); | |
1018 | return -ENOMEM; | |
1da177e4 LT |
1019 | } |
1020 | ||
1021 | /* | |
1022 | * This function removes an sbp2 device from the sbp2scsi_host_info struct. | |
1023 | */ | |
1024 | static void sbp2_remove_device(struct scsi_id_instance_data *scsi_id) | |
1025 | { | |
1026 | struct sbp2scsi_host_info *hi; | |
1027 | ||
d024ebc6 | 1028 | SBP2_DEBUG_ENTER(); |
1da177e4 LT |
1029 | |
1030 | if (!scsi_id) | |
1031 | return; | |
1032 | ||
1033 | hi = scsi_id->hi; | |
1034 | ||
1035 | /* This will remove our scsi device aswell */ | |
1036 | if (scsi_id->scsi_host) { | |
1037 | scsi_remove_host(scsi_id->scsi_host); | |
1038 | scsi_host_put(scsi_id->scsi_host); | |
1039 | } | |
1040 | ||
1041 | sbp2util_remove_command_orb_pool(scsi_id); | |
1042 | ||
1043 | list_del(&scsi_id->scsi_list); | |
1044 | ||
1045 | if (scsi_id->login_response) { | |
1046 | pci_free_consistent(hi->host->pdev, | |
1047 | sizeof(struct sbp2_login_response), | |
1048 | scsi_id->login_response, | |
1049 | scsi_id->login_response_dma); | |
1050 | SBP2_DMA_FREE("single login FIFO"); | |
1051 | } | |
1052 | ||
1053 | if (scsi_id->login_orb) { | |
1054 | pci_free_consistent(hi->host->pdev, | |
1055 | sizeof(struct sbp2_login_orb), | |
1056 | scsi_id->login_orb, | |
1057 | scsi_id->login_orb_dma); | |
1058 | SBP2_DMA_FREE("single login ORB"); | |
1059 | } | |
1060 | ||
1061 | if (scsi_id->reconnect_orb) { | |
1062 | pci_free_consistent(hi->host->pdev, | |
1063 | sizeof(struct sbp2_reconnect_orb), | |
1064 | scsi_id->reconnect_orb, | |
1065 | scsi_id->reconnect_orb_dma); | |
1066 | SBP2_DMA_FREE("single reconnect orb"); | |
1067 | } | |
1068 | ||
1069 | if (scsi_id->logout_orb) { | |
1070 | pci_free_consistent(hi->host->pdev, | |
1071 | sizeof(struct sbp2_logout_orb), | |
1072 | scsi_id->logout_orb, | |
1073 | scsi_id->logout_orb_dma); | |
1074 | SBP2_DMA_FREE("single logout orb"); | |
1075 | } | |
1076 | ||
1077 | if (scsi_id->query_logins_orb) { | |
1078 | pci_free_consistent(hi->host->pdev, | |
1079 | sizeof(struct sbp2_query_logins_orb), | |
1080 | scsi_id->query_logins_orb, | |
1081 | scsi_id->query_logins_orb_dma); | |
1082 | SBP2_DMA_FREE("single query logins orb"); | |
1083 | } | |
1084 | ||
1085 | if (scsi_id->query_logins_response) { | |
1086 | pci_free_consistent(hi->host->pdev, | |
1087 | sizeof(struct sbp2_query_logins_response), | |
1088 | scsi_id->query_logins_response, | |
1089 | scsi_id->query_logins_response_dma); | |
1090 | SBP2_DMA_FREE("single query logins data"); | |
1091 | } | |
1092 | ||
35bdddb8 SR |
1093 | if (scsi_id->status_fifo_addr) |
1094 | hpsb_unregister_addrspace(&sbp2_highlevel, hi->host, | |
1095 | scsi_id->status_fifo_addr); | |
1096 | ||
1da177e4 LT |
1097 | scsi_id->ud->device.driver_data = NULL; |
1098 | ||
147830f2 SR |
1099 | if (hi) |
1100 | module_put(hi->host->driver->owner); | |
1101 | ||
1da177e4 LT |
1102 | SBP2_DEBUG("SBP-2 device removed, SCSI ID = %d", scsi_id->ud->id); |
1103 | ||
1104 | kfree(scsi_id); | |
1105 | } | |
1106 | ||
1107 | #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA | |
1108 | /* | |
1109 | * This function deals with physical dma write requests (for adapters that do not support | |
1110 | * physical dma in hardware). Mostly just here for debugging... | |
1111 | */ | |
a237f35f SR |
1112 | static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid, |
1113 | int destid, quadlet_t *data, u64 addr, | |
1114 | size_t length, u16 flags) | |
1da177e4 LT |
1115 | { |
1116 | ||
a237f35f SR |
1117 | /* |
1118 | * Manually put the data in the right place. | |
1119 | */ | |
1120 | memcpy(bus_to_virt((u32) addr), data, length); | |
1121 | sbp2util_packet_dump(data, length, "sbp2 phys dma write by device", | |
1122 | (u32) addr); | |
1123 | return RCODE_COMPLETE; | |
1da177e4 LT |
1124 | } |
1125 | ||
1126 | /* | |
1127 | * This function deals with physical dma read requests (for adapters that do not support | |
1128 | * physical dma in hardware). Mostly just here for debugging... | |
1129 | */ | |
a237f35f SR |
1130 | static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid, |
1131 | quadlet_t *data, u64 addr, size_t length, | |
1132 | u16 flags) | |
1da177e4 LT |
1133 | { |
1134 | ||
a237f35f SR |
1135 | /* |
1136 | * Grab data from memory and send a read response. | |
1137 | */ | |
1138 | memcpy(data, bus_to_virt((u32) addr), length); | |
1139 | sbp2util_packet_dump(data, length, "sbp2 phys dma read by device", | |
1140 | (u32) addr); | |
1141 | return RCODE_COMPLETE; | |
1da177e4 LT |
1142 | } |
1143 | #endif | |
1144 | ||
1da177e4 LT |
1145 | /************************************** |
1146 | * SBP-2 protocol related section | |
1147 | **************************************/ | |
1148 | ||
1da177e4 LT |
1149 | /* |
1150 | * This function queries the device for the maximum concurrent logins it | |
1151 | * supports. | |
1152 | */ | |
1153 | static int sbp2_query_logins(struct scsi_id_instance_data *scsi_id) | |
1154 | { | |
1155 | struct sbp2scsi_host_info *hi = scsi_id->hi; | |
1156 | quadlet_t data[2]; | |
1157 | int max_logins; | |
1158 | int active_logins; | |
1159 | ||
d024ebc6 | 1160 | SBP2_DEBUG_ENTER(); |
1da177e4 LT |
1161 | |
1162 | scsi_id->query_logins_orb->reserved1 = 0x0; | |
1163 | scsi_id->query_logins_orb->reserved2 = 0x0; | |
1164 | ||
1165 | scsi_id->query_logins_orb->query_response_lo = scsi_id->query_logins_response_dma; | |
1166 | scsi_id->query_logins_orb->query_response_hi = ORB_SET_NODE_ID(hi->host->node_id); | |
1da177e4 LT |
1167 | |
1168 | scsi_id->query_logins_orb->lun_misc = ORB_SET_FUNCTION(SBP2_QUERY_LOGINS_REQUEST); | |
1169 | scsi_id->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1); | |
e309fc6d | 1170 | scsi_id->query_logins_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_lun); |
1da177e4 LT |
1171 | |
1172 | scsi_id->query_logins_orb->reserved_resp_length = | |
1173 | ORB_SET_QUERY_LOGINS_RESP_LENGTH(sizeof(struct sbp2_query_logins_response)); | |
1da177e4 | 1174 | |
35bdddb8 SR |
1175 | scsi_id->query_logins_orb->status_fifo_hi = |
1176 | ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id); | |
1177 | scsi_id->query_logins_orb->status_fifo_lo = | |
1178 | ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr); | |
1da177e4 LT |
1179 | |
1180 | sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb)); | |
1181 | ||
1da177e4 LT |
1182 | sbp2util_packet_dump(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb), |
1183 | "sbp2 query logins orb", scsi_id->query_logins_orb_dma); | |
1184 | ||
1185 | memset(scsi_id->query_logins_response, 0, sizeof(struct sbp2_query_logins_response)); | |
1186 | memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block)); | |
1187 | ||
1da177e4 LT |
1188 | data[0] = ORB_SET_NODE_ID(hi->host->node_id); |
1189 | data[1] = scsi_id->query_logins_orb_dma; | |
1190 | sbp2util_cpu_to_be32_buffer(data, 8); | |
1191 | ||
1192 | atomic_set(&scsi_id->sbp2_login_complete, 0); | |
1193 | ||
1da177e4 | 1194 | hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8); |
1da177e4 LT |
1195 | |
1196 | if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, 2*HZ)) { | |
1197 | SBP2_INFO("Error querying logins to SBP-2 device - timed out"); | |
a237f35f | 1198 | return -EIO; |
1da177e4 LT |
1199 | } |
1200 | ||
1201 | if (scsi_id->status_block.ORB_offset_lo != scsi_id->query_logins_orb_dma) { | |
1202 | SBP2_INFO("Error querying logins to SBP-2 device - timed out"); | |
a237f35f | 1203 | return -EIO; |
1da177e4 LT |
1204 | } |
1205 | ||
1206 | if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) || | |
1207 | STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) || | |
1208 | STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) { | |
1209 | ||
1210 | SBP2_INFO("Error querying logins to SBP-2 device - timed out"); | |
a237f35f | 1211 | return -EIO; |
1da177e4 LT |
1212 | } |
1213 | ||
1214 | sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_response, sizeof(struct sbp2_query_logins_response)); | |
1215 | ||
1216 | SBP2_DEBUG("length_max_logins = %x", | |
1217 | (unsigned int)scsi_id->query_logins_response->length_max_logins); | |
1218 | ||
1da177e4 | 1219 | max_logins = RESPONSE_GET_MAX_LOGINS(scsi_id->query_logins_response->length_max_logins); |
20f45781 | 1220 | SBP2_INFO("Maximum concurrent logins supported: %d", max_logins); |
1da177e4 LT |
1221 | |
1222 | active_logins = RESPONSE_GET_ACTIVE_LOGINS(scsi_id->query_logins_response->length_max_logins); | |
20f45781 | 1223 | SBP2_INFO("Number of active logins: %d", active_logins); |
1da177e4 LT |
1224 | |
1225 | if (active_logins >= max_logins) { | |
a237f35f | 1226 | return -EIO; |
1da177e4 LT |
1227 | } |
1228 | ||
1229 | return 0; | |
1230 | } | |
1231 | ||
1232 | /* | |
1233 | * This function is called in order to login to a particular SBP-2 device, | |
1234 | * after a bus reset. | |
1235 | */ | |
1236 | static int sbp2_login_device(struct scsi_id_instance_data *scsi_id) | |
1237 | { | |
1238 | struct sbp2scsi_host_info *hi = scsi_id->hi; | |
1239 | quadlet_t data[2]; | |
1240 | ||
d024ebc6 | 1241 | SBP2_DEBUG_ENTER(); |
1da177e4 LT |
1242 | |
1243 | if (!scsi_id->login_orb) { | |
d024ebc6 | 1244 | SBP2_DEBUG("%s: login_orb not alloc'd!", __FUNCTION__); |
a237f35f | 1245 | return -EIO; |
1da177e4 LT |
1246 | } |
1247 | ||
1248 | if (!exclusive_login) { | |
1249 | if (sbp2_query_logins(scsi_id)) { | |
1250 | SBP2_INFO("Device does not support any more concurrent logins"); | |
a237f35f | 1251 | return -EIO; |
1da177e4 LT |
1252 | } |
1253 | } | |
1254 | ||
1255 | /* Set-up login ORB, assume no password */ | |
1256 | scsi_id->login_orb->password_hi = 0; | |
1257 | scsi_id->login_orb->password_lo = 0; | |
1da177e4 LT |
1258 | |
1259 | scsi_id->login_orb->login_response_lo = scsi_id->login_response_dma; | |
1260 | scsi_id->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id); | |
1da177e4 LT |
1261 | |
1262 | scsi_id->login_orb->lun_misc = ORB_SET_FUNCTION(SBP2_LOGIN_REQUEST); | |
1263 | scsi_id->login_orb->lun_misc |= ORB_SET_RECONNECT(0); /* One second reconnect time */ | |
1264 | scsi_id->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(exclusive_login); /* Exclusive access to device */ | |
1265 | scsi_id->login_orb->lun_misc |= ORB_SET_NOTIFY(1); /* Notify us of login complete */ | |
e309fc6d | 1266 | scsi_id->login_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_lun); |
1da177e4 LT |
1267 | |
1268 | scsi_id->login_orb->passwd_resp_lengths = | |
1269 | ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response)); | |
1da177e4 | 1270 | |
35bdddb8 SR |
1271 | scsi_id->login_orb->status_fifo_hi = |
1272 | ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id); | |
1273 | scsi_id->login_orb->status_fifo_lo = | |
1274 | ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr); | |
1da177e4 | 1275 | |
1da177e4 LT |
1276 | sbp2util_cpu_to_be32_buffer(scsi_id->login_orb, sizeof(struct sbp2_login_orb)); |
1277 | ||
1da177e4 LT |
1278 | sbp2util_packet_dump(scsi_id->login_orb, sizeof(struct sbp2_login_orb), |
1279 | "sbp2 login orb", scsi_id->login_orb_dma); | |
1280 | ||
1da177e4 LT |
1281 | memset(scsi_id->login_response, 0, sizeof(struct sbp2_login_response)); |
1282 | memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block)); | |
1283 | ||
1da177e4 LT |
1284 | data[0] = ORB_SET_NODE_ID(hi->host->node_id); |
1285 | data[1] = scsi_id->login_orb_dma; | |
1286 | sbp2util_cpu_to_be32_buffer(data, 8); | |
1287 | ||
1288 | atomic_set(&scsi_id->sbp2_login_complete, 0); | |
1289 | ||
1da177e4 | 1290 | hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8); |
1da177e4 LT |
1291 | |
1292 | /* | |
1293 | * Wait for login status (up to 20 seconds)... | |
1294 | */ | |
1295 | if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, 20*HZ)) { | |
1296 | SBP2_ERR("Error logging into SBP-2 device - login timed-out"); | |
a237f35f | 1297 | return -EIO; |
1da177e4 LT |
1298 | } |
1299 | ||
1300 | /* | |
1301 | * Sanity. Make sure status returned matches login orb. | |
1302 | */ | |
1303 | if (scsi_id->status_block.ORB_offset_lo != scsi_id->login_orb_dma) { | |
1304 | SBP2_ERR("Error logging into SBP-2 device - login timed-out"); | |
a237f35f | 1305 | return -EIO; |
1da177e4 LT |
1306 | } |
1307 | ||
1308 | /* | |
1309 | * Check status | |
1310 | */ | |
1311 | if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) || | |
1312 | STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) || | |
1313 | STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) { | |
1314 | ||
1315 | SBP2_ERR("Error logging into SBP-2 device - login failed"); | |
a237f35f | 1316 | return -EIO; |
1da177e4 LT |
1317 | } |
1318 | ||
1319 | /* | |
1320 | * Byte swap the login response, for use when reconnecting or | |
1321 | * logging out. | |
1322 | */ | |
1323 | sbp2util_cpu_to_be32_buffer(scsi_id->login_response, sizeof(struct sbp2_login_response)); | |
1324 | ||
1325 | /* | |
1326 | * Grab our command block agent address from the login response. | |
1327 | */ | |
1328 | SBP2_DEBUG("command_block_agent_hi = %x", | |
1329 | (unsigned int)scsi_id->login_response->command_block_agent_hi); | |
1330 | SBP2_DEBUG("command_block_agent_lo = %x", | |
1331 | (unsigned int)scsi_id->login_response->command_block_agent_lo); | |
1332 | ||
1333 | scsi_id->sbp2_command_block_agent_addr = | |
1334 | ((u64)scsi_id->login_response->command_block_agent_hi) << 32; | |
1335 | scsi_id->sbp2_command_block_agent_addr |= ((u64)scsi_id->login_response->command_block_agent_lo); | |
1336 | scsi_id->sbp2_command_block_agent_addr &= 0x0000ffffffffffffULL; | |
1337 | ||
1338 | SBP2_INFO("Logged into SBP-2 device"); | |
1339 | ||
a237f35f | 1340 | return 0; |
1da177e4 LT |
1341 | |
1342 | } | |
1343 | ||
1344 | /* | |
1345 | * This function is called in order to logout from a particular SBP-2 | |
1346 | * device, usually called during driver unload. | |
1347 | */ | |
1348 | static int sbp2_logout_device(struct scsi_id_instance_data *scsi_id) | |
1349 | { | |
1350 | struct sbp2scsi_host_info *hi = scsi_id->hi; | |
1351 | quadlet_t data[2]; | |
1352 | int error; | |
1353 | ||
d024ebc6 | 1354 | SBP2_DEBUG_ENTER(); |
1da177e4 LT |
1355 | |
1356 | /* | |
1357 | * Set-up logout ORB | |
1358 | */ | |
1359 | scsi_id->logout_orb->reserved1 = 0x0; | |
1360 | scsi_id->logout_orb->reserved2 = 0x0; | |
1361 | scsi_id->logout_orb->reserved3 = 0x0; | |
1362 | scsi_id->logout_orb->reserved4 = 0x0; | |
1363 | ||
1364 | scsi_id->logout_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_LOGOUT_REQUEST); | |
1365 | scsi_id->logout_orb->login_ID_misc |= ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID); | |
1366 | ||
1367 | /* Notify us when complete */ | |
1368 | scsi_id->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1); | |
1369 | ||
1370 | scsi_id->logout_orb->reserved5 = 0x0; | |
35bdddb8 SR |
1371 | scsi_id->logout_orb->status_fifo_hi = |
1372 | ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id); | |
1373 | scsi_id->logout_orb->status_fifo_lo = | |
1374 | ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr); | |
1da177e4 LT |
1375 | |
1376 | /* | |
1377 | * Byte swap ORB if necessary | |
1378 | */ | |
1379 | sbp2util_cpu_to_be32_buffer(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb)); | |
1380 | ||
1381 | sbp2util_packet_dump(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb), | |
1382 | "sbp2 logout orb", scsi_id->logout_orb_dma); | |
1383 | ||
1384 | /* | |
1385 | * Ok, let's write to the target's management agent register | |
1386 | */ | |
1387 | data[0] = ORB_SET_NODE_ID(hi->host->node_id); | |
1388 | data[1] = scsi_id->logout_orb_dma; | |
1389 | sbp2util_cpu_to_be32_buffer(data, 8); | |
1390 | ||
1391 | atomic_set(&scsi_id->sbp2_login_complete, 0); | |
1392 | ||
1393 | error = hpsb_node_write(scsi_id->ne, | |
a237f35f | 1394 | scsi_id->sbp2_management_agent_addr, data, 8); |
1da177e4 LT |
1395 | if (error) |
1396 | return error; | |
1397 | ||
1398 | /* Wait for device to logout...1 second. */ | |
1399 | if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, HZ)) | |
1400 | return -EIO; | |
1401 | ||
1402 | SBP2_INFO("Logged out of SBP-2 device"); | |
1403 | ||
a237f35f | 1404 | return 0; |
1da177e4 LT |
1405 | |
1406 | } | |
1407 | ||
1408 | /* | |
1409 | * This function is called in order to reconnect to a particular SBP-2 | |
1410 | * device, after a bus reset. | |
1411 | */ | |
1412 | static int sbp2_reconnect_device(struct scsi_id_instance_data *scsi_id) | |
1413 | { | |
1414 | struct sbp2scsi_host_info *hi = scsi_id->hi; | |
1415 | quadlet_t data[2]; | |
1416 | int error; | |
1417 | ||
d024ebc6 | 1418 | SBP2_DEBUG_ENTER(); |
1da177e4 LT |
1419 | |
1420 | /* | |
1421 | * Set-up reconnect ORB | |
1422 | */ | |
1423 | scsi_id->reconnect_orb->reserved1 = 0x0; | |
1424 | scsi_id->reconnect_orb->reserved2 = 0x0; | |
1425 | scsi_id->reconnect_orb->reserved3 = 0x0; | |
1426 | scsi_id->reconnect_orb->reserved4 = 0x0; | |
1427 | ||
1428 | scsi_id->reconnect_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_RECONNECT_REQUEST); | |
1429 | scsi_id->reconnect_orb->login_ID_misc |= | |
1430 | ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID); | |
1431 | ||
1432 | /* Notify us when complete */ | |
1433 | scsi_id->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1); | |
1434 | ||
1435 | scsi_id->reconnect_orb->reserved5 = 0x0; | |
35bdddb8 SR |
1436 | scsi_id->reconnect_orb->status_fifo_hi = |
1437 | ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id); | |
1438 | scsi_id->reconnect_orb->status_fifo_lo = | |
1439 | ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr); | |
1da177e4 LT |
1440 | |
1441 | /* | |
1442 | * Byte swap ORB if necessary | |
1443 | */ | |
1444 | sbp2util_cpu_to_be32_buffer(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb)); | |
1445 | ||
1446 | sbp2util_packet_dump(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb), | |
1447 | "sbp2 reconnect orb", scsi_id->reconnect_orb_dma); | |
1448 | ||
1449 | /* | |
1450 | * Initialize status fifo | |
1451 | */ | |
1452 | memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block)); | |
1453 | ||
1454 | /* | |
1455 | * Ok, let's write to the target's management agent register | |
1456 | */ | |
1457 | data[0] = ORB_SET_NODE_ID(hi->host->node_id); | |
1458 | data[1] = scsi_id->reconnect_orb_dma; | |
1459 | sbp2util_cpu_to_be32_buffer(data, 8); | |
1460 | ||
1461 | atomic_set(&scsi_id->sbp2_login_complete, 0); | |
1462 | ||
1463 | error = hpsb_node_write(scsi_id->ne, | |
a237f35f | 1464 | scsi_id->sbp2_management_agent_addr, data, 8); |
1da177e4 LT |
1465 | if (error) |
1466 | return error; | |
1467 | ||
1468 | /* | |
1469 | * Wait for reconnect status (up to 1 second)... | |
1470 | */ | |
1471 | if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, HZ)) { | |
1472 | SBP2_ERR("Error reconnecting to SBP-2 device - reconnect timed-out"); | |
a237f35f | 1473 | return -EIO; |
1da177e4 LT |
1474 | } |
1475 | ||
1476 | /* | |
1477 | * Sanity. Make sure status returned matches reconnect orb. | |
1478 | */ | |
1479 | if (scsi_id->status_block.ORB_offset_lo != scsi_id->reconnect_orb_dma) { | |
1480 | SBP2_ERR("Error reconnecting to SBP-2 device - reconnect timed-out"); | |
a237f35f | 1481 | return -EIO; |
1da177e4 LT |
1482 | } |
1483 | ||
1484 | /* | |
1485 | * Check status | |
1486 | */ | |
1487 | if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) || | |
1488 | STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) || | |
1489 | STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) { | |
1490 | ||
1491 | SBP2_ERR("Error reconnecting to SBP-2 device - reconnect failed"); | |
a237f35f | 1492 | return -EIO; |
1da177e4 LT |
1493 | } |
1494 | ||
1495 | HPSB_DEBUG("Reconnected to SBP-2 device"); | |
1496 | ||
a237f35f | 1497 | return 0; |
1da177e4 LT |
1498 | |
1499 | } | |
1500 | ||
1501 | /* | |
1502 | * This function is called in order to set the busy timeout (number of | |
1503 | * retries to attempt) on the sbp2 device. | |
1504 | */ | |
1505 | static int sbp2_set_busy_timeout(struct scsi_id_instance_data *scsi_id) | |
1506 | { | |
1507 | quadlet_t data; | |
1508 | ||
d024ebc6 | 1509 | SBP2_DEBUG_ENTER(); |
1da177e4 | 1510 | |
1da177e4 | 1511 | data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE); |
d024ebc6 SR |
1512 | if (hpsb_node_write(scsi_id->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4)) |
1513 | SBP2_ERR("%s error", __FUNCTION__); | |
a237f35f | 1514 | return 0; |
1da177e4 LT |
1515 | } |
1516 | ||
1da177e4 LT |
1517 | /* |
1518 | * This function is called to parse sbp2 device's config rom unit | |
1519 | * directory. Used to determine things like sbp2 management agent offset, | |
1520 | * and command set used (SCSI or RBC). | |
1521 | */ | |
1522 | static void sbp2_parse_unit_directory(struct scsi_id_instance_data *scsi_id, | |
1523 | struct unit_directory *ud) | |
1524 | { | |
1525 | struct csr1212_keyval *kv; | |
1526 | struct csr1212_dentry *dentry; | |
1527 | u64 management_agent_addr; | |
1528 | u32 command_set_spec_id, command_set, unit_characteristics, | |
24d3bf88 SR |
1529 | firmware_revision; |
1530 | unsigned workarounds; | |
1da177e4 LT |
1531 | int i; |
1532 | ||
d024ebc6 | 1533 | SBP2_DEBUG_ENTER(); |
1da177e4 LT |
1534 | |
1535 | management_agent_addr = 0x0; | |
1536 | command_set_spec_id = 0x0; | |
1537 | command_set = 0x0; | |
1538 | unit_characteristics = 0x0; | |
1539 | firmware_revision = 0x0; | |
1540 | ||
1541 | /* Handle different fields in the unit directory, based on keys */ | |
1542 | csr1212_for_each_dir_entry(ud->ne->csr, kv, ud->ud_kv, dentry) { | |
1543 | switch (kv->key.id) { | |
1544 | case CSR1212_KV_ID_DEPENDENT_INFO: | |
1545 | if (kv->key.type == CSR1212_KV_TYPE_CSR_OFFSET) { | |
1546 | /* Save off the management agent address */ | |
1547 | management_agent_addr = | |
a237f35f SR |
1548 | CSR1212_REGISTER_SPACE_BASE + |
1549 | (kv->value.csr_offset << 2); | |
1da177e4 LT |
1550 | |
1551 | SBP2_DEBUG("sbp2_management_agent_addr = %x", | |
a237f35f | 1552 | (unsigned int)management_agent_addr); |
1da177e4 | 1553 | } else if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) { |
a237f35f SR |
1554 | scsi_id->sbp2_lun = |
1555 | ORB_SET_LUN(kv->value.immediate); | |
1da177e4 LT |
1556 | } |
1557 | break; | |
1558 | ||
1559 | case SBP2_COMMAND_SET_SPEC_ID_KEY: | |
1560 | /* Command spec organization */ | |
1561 | command_set_spec_id = kv->value.immediate; | |
1562 | SBP2_DEBUG("sbp2_command_set_spec_id = %x", | |
a237f35f | 1563 | (unsigned int)command_set_spec_id); |
1da177e4 LT |
1564 | break; |
1565 | ||
1566 | case SBP2_COMMAND_SET_KEY: | |
1567 | /* Command set used by sbp2 device */ | |
1568 | command_set = kv->value.immediate; | |
1569 | SBP2_DEBUG("sbp2_command_set = %x", | |
a237f35f | 1570 | (unsigned int)command_set); |
1da177e4 LT |
1571 | break; |
1572 | ||
1573 | case SBP2_UNIT_CHARACTERISTICS_KEY: | |
1574 | /* | |
1575 | * Unit characterisitcs (orb related stuff | |
1576 | * that I'm not yet paying attention to) | |
1577 | */ | |
1578 | unit_characteristics = kv->value.immediate; | |
1579 | SBP2_DEBUG("sbp2_unit_characteristics = %x", | |
a237f35f | 1580 | (unsigned int)unit_characteristics); |
1da177e4 LT |
1581 | break; |
1582 | ||
1583 | case SBP2_FIRMWARE_REVISION_KEY: | |
1584 | /* Firmware revision */ | |
1585 | firmware_revision = kv->value.immediate; | |
24d3bf88 SR |
1586 | SBP2_DEBUG("sbp2_firmware_revision = %x", |
1587 | (unsigned int)firmware_revision); | |
1da177e4 LT |
1588 | break; |
1589 | ||
1590 | default: | |
1591 | break; | |
1592 | } | |
1593 | } | |
1594 | ||
24d3bf88 SR |
1595 | workarounds = sbp2_default_workarounds; |
1596 | if (force_inquiry_hack) { | |
1597 | SBP2_WARN("force_inquiry_hack is deprecated. " | |
1598 | "Use parameter 'workarounds' instead."); | |
1599 | workarounds |= SBP2_WORKAROUND_INQUIRY_36; | |
1600 | } | |
1da177e4 | 1601 | |
679c0cd2 SR |
1602 | if (!(workarounds & SBP2_WORKAROUND_OVERRIDE)) |
1603 | for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) { | |
1604 | if (sbp2_workarounds_table[i].firmware_revision && | |
1605 | sbp2_workarounds_table[i].firmware_revision != | |
1606 | (firmware_revision & 0xffff00)) | |
1607 | continue; | |
1608 | if (sbp2_workarounds_table[i].model_id && | |
1609 | sbp2_workarounds_table[i].model_id != ud->model_id) | |
1610 | continue; | |
1611 | workarounds |= sbp2_workarounds_table[i].workarounds; | |
1612 | break; | |
1613 | } | |
1da177e4 | 1614 | |
24d3bf88 | 1615 | if (workarounds) |
e9a1c52c SR |
1616 | SBP2_INFO("Workarounds for node " NODE_BUS_FMT ": 0x%x " |
1617 | "(firmware_revision 0x%06x, vendor_id 0x%06x," | |
1618 | " model_id 0x%06x)", | |
24d3bf88 | 1619 | NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid), |
e9a1c52c SR |
1620 | workarounds, firmware_revision, |
1621 | ud->vendor_id ? ud->vendor_id : ud->ne->vendor_id, | |
1622 | ud->model_id); | |
24d3bf88 SR |
1623 | |
1624 | /* We would need one SCSI host template for each target to adjust | |
1625 | * max_sectors on the fly, therefore warn only. */ | |
1626 | if (workarounds & SBP2_WORKAROUND_128K_MAX_TRANS && | |
1627 | (max_sectors * 512) > (128 * 1024)) | |
1628 | SBP2_WARN("Node " NODE_BUS_FMT ": Bridge only supports 128KB " | |
1629 | "max transfer size. WARNING: Current max_sectors " | |
1630 | "setting is larger than 128KB (%d sectors)", | |
1631 | NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid), | |
1632 | max_sectors); | |
1633 | ||
1da177e4 LT |
1634 | /* If this is a logical unit directory entry, process the parent |
1635 | * to get the values. */ | |
1636 | if (ud->flags & UNIT_DIRECTORY_LUN_DIRECTORY) { | |
1637 | struct unit_directory *parent_ud = | |
1638 | container_of(ud->device.parent, struct unit_directory, device); | |
1639 | sbp2_parse_unit_directory(scsi_id, parent_ud); | |
1640 | } else { | |
1641 | scsi_id->sbp2_management_agent_addr = management_agent_addr; | |
1642 | scsi_id->sbp2_command_set_spec_id = command_set_spec_id; | |
1643 | scsi_id->sbp2_command_set = command_set; | |
1644 | scsi_id->sbp2_unit_characteristics = unit_characteristics; | |
1645 | scsi_id->sbp2_firmware_revision = firmware_revision; | |
1646 | scsi_id->workarounds = workarounds; | |
1647 | if (ud->flags & UNIT_DIRECTORY_HAS_LUN) | |
e309fc6d | 1648 | scsi_id->sbp2_lun = ORB_SET_LUN(ud->lun); |
1da177e4 LT |
1649 | } |
1650 | } | |
1651 | ||
1652 | /* | |
1653 | * This function is called in order to determine the max speed and packet | |
1654 | * size we can use in our ORBs. Note, that we (the driver and host) only | |
1655 | * initiate the transaction. The SBP-2 device actually transfers the data | |
1656 | * (by reading from the DMA area we tell it). This means that the SBP-2 | |
1657 | * device decides the actual maximum data it can transfer. We just tell it | |
1658 | * the speed that it needs to use, and the max_rec the host supports, and | |
1659 | * it takes care of the rest. | |
1660 | */ | |
1661 | static int sbp2_max_speed_and_size(struct scsi_id_instance_data *scsi_id) | |
1662 | { | |
1663 | struct sbp2scsi_host_info *hi = scsi_id->hi; | |
1664 | ||
d024ebc6 | 1665 | SBP2_DEBUG_ENTER(); |
1da177e4 | 1666 | |
a237f35f | 1667 | scsi_id->speed_code = |
647dcb5f | 1668 | hi->host->speed[NODEID_TO_NODE(scsi_id->ne->nodeid)]; |
1da177e4 LT |
1669 | |
1670 | /* Bump down our speed if the user requested it */ | |
1671 | if (scsi_id->speed_code > max_speed) { | |
1672 | scsi_id->speed_code = max_speed; | |
1673 | SBP2_ERR("Forcing SBP-2 max speed down to %s", | |
1674 | hpsb_speedto_str[scsi_id->speed_code]); | |
1675 | } | |
1676 | ||
1677 | /* Payload size is the lesser of what our speed supports and what | |
1678 | * our host supports. */ | |
a237f35f SR |
1679 | scsi_id->max_payload_size = |
1680 | min(sbp2_speedto_max_payload[scsi_id->speed_code], | |
1681 | (u8) (hi->host->csr.max_rec - 1)); | |
1da177e4 LT |
1682 | |
1683 | HPSB_DEBUG("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]", | |
1684 | NODE_BUS_ARGS(hi->host, scsi_id->ne->nodeid), | |
1685 | hpsb_speedto_str[scsi_id->speed_code], | |
a237f35f | 1686 | 1 << ((u32) scsi_id->max_payload_size + 2)); |
1da177e4 | 1687 | |
a237f35f | 1688 | return 0; |
1da177e4 LT |
1689 | } |
1690 | ||
1691 | /* | |
1692 | * This function is called in order to perform a SBP-2 agent reset. | |
1693 | */ | |
1694 | static int sbp2_agent_reset(struct scsi_id_instance_data *scsi_id, int wait) | |
1695 | { | |
1696 | quadlet_t data; | |
1697 | u64 addr; | |
1698 | int retval; | |
1699 | ||
d024ebc6 | 1700 | SBP2_DEBUG_ENTER(); |
1da177e4 | 1701 | |
1da177e4 LT |
1702 | data = ntohl(SBP2_AGENT_RESET_DATA); |
1703 | addr = scsi_id->sbp2_command_block_agent_addr + SBP2_AGENT_RESET_OFFSET; | |
1704 | ||
1705 | if (wait) | |
1706 | retval = hpsb_node_write(scsi_id->ne, addr, &data, 4); | |
1707 | else | |
1708 | retval = sbp2util_node_write_no_wait(scsi_id->ne, addr, &data, 4); | |
1709 | ||
1710 | if (retval < 0) { | |
1711 | SBP2_ERR("hpsb_node_write failed.\n"); | |
1712 | return -EIO; | |
1713 | } | |
1714 | ||
1715 | /* | |
1716 | * Need to make sure orb pointer is written on next command | |
1717 | */ | |
1718 | scsi_id->last_orb = NULL; | |
1719 | ||
a237f35f | 1720 | return 0; |
1da177e4 LT |
1721 | } |
1722 | ||
cf8d2c09 SR |
1723 | static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb, |
1724 | struct sbp2scsi_host_info *hi, | |
1725 | struct sbp2_command_info *command, | |
1726 | unsigned int scsi_use_sg, | |
1727 | struct scatterlist *sgpnt, | |
1728 | u32 orb_direction, | |
1729 | enum dma_data_direction dma_dir) | |
1730 | { | |
1731 | command->dma_dir = dma_dir; | |
1732 | orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id); | |
1733 | orb->misc |= ORB_SET_DIRECTION(orb_direction); | |
1734 | ||
1735 | /* Special case if only one element (and less than 64KB in size) */ | |
1736 | if ((scsi_use_sg == 1) && | |
1737 | (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) { | |
1738 | ||
1739 | SBP2_DEBUG("Only one s/g element"); | |
1740 | command->dma_size = sgpnt[0].length; | |
1741 | command->dma_type = CMD_DMA_PAGE; | |
1742 | command->cmd_dma = pci_map_page(hi->host->pdev, | |
1743 | sgpnt[0].page, | |
1744 | sgpnt[0].offset, | |
1745 | command->dma_size, | |
1746 | command->dma_dir); | |
1747 | SBP2_DMA_ALLOC("single page scatter element"); | |
1748 | ||
1749 | orb->data_descriptor_lo = command->cmd_dma; | |
1750 | orb->misc |= ORB_SET_DATA_SIZE(command->dma_size); | |
1751 | ||
1752 | } else { | |
1753 | struct sbp2_unrestricted_page_table *sg_element = | |
1754 | &command->scatter_gather_element[0]; | |
1755 | u32 sg_count, sg_len; | |
1756 | dma_addr_t sg_addr; | |
1757 | int i, count = pci_map_sg(hi->host->pdev, sgpnt, scsi_use_sg, | |
1758 | dma_dir); | |
1759 | ||
1760 | SBP2_DMA_ALLOC("scatter list"); | |
1761 | ||
1762 | command->dma_size = scsi_use_sg; | |
1763 | command->sge_buffer = sgpnt; | |
1764 | ||
1765 | /* use page tables (s/g) */ | |
1766 | orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1); | |
1767 | orb->data_descriptor_lo = command->sge_dma; | |
1768 | ||
1769 | /* | |
1770 | * Loop through and fill out our sbp-2 page tables | |
1771 | * (and split up anything too large) | |
1772 | */ | |
1773 | for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) { | |
1774 | sg_len = sg_dma_len(sgpnt); | |
1775 | sg_addr = sg_dma_address(sgpnt); | |
1776 | while (sg_len) { | |
1777 | sg_element[sg_count].segment_base_lo = sg_addr; | |
1778 | if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) { | |
1779 | sg_element[sg_count].length_segment_base_hi = | |
1780 | PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH); | |
1781 | sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH; | |
1782 | sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH; | |
1783 | } else { | |
1784 | sg_element[sg_count].length_segment_base_hi = | |
1785 | PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len); | |
1786 | sg_len = 0; | |
1787 | } | |
1788 | sg_count++; | |
1789 | } | |
1790 | } | |
1791 | ||
1792 | /* Number of page table (s/g) elements */ | |
1793 | orb->misc |= ORB_SET_DATA_SIZE(sg_count); | |
1794 | ||
1795 | sbp2util_packet_dump(sg_element, | |
1796 | (sizeof(struct sbp2_unrestricted_page_table)) * sg_count, | |
1797 | "sbp2 s/g list", command->sge_dma); | |
1798 | ||
1799 | /* Byte swap page tables if necessary */ | |
1800 | sbp2util_cpu_to_be32_buffer(sg_element, | |
1801 | (sizeof(struct sbp2_unrestricted_page_table)) * | |
1802 | sg_count); | |
1803 | } | |
1804 | } | |
1805 | ||
1806 | static void sbp2_prep_command_orb_no_sg(struct sbp2_command_orb *orb, | |
1807 | struct sbp2scsi_host_info *hi, | |
1808 | struct sbp2_command_info *command, | |
1809 | struct scatterlist *sgpnt, | |
1810 | u32 orb_direction, | |
1811 | unsigned int scsi_request_bufflen, | |
1812 | void *scsi_request_buffer, | |
1813 | enum dma_data_direction dma_dir) | |
1814 | { | |
1815 | command->dma_dir = dma_dir; | |
1816 | command->dma_size = scsi_request_bufflen; | |
1817 | command->dma_type = CMD_DMA_SINGLE; | |
1818 | command->cmd_dma = pci_map_single(hi->host->pdev, scsi_request_buffer, | |
1819 | command->dma_size, command->dma_dir); | |
1820 | orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id); | |
1821 | orb->misc |= ORB_SET_DIRECTION(orb_direction); | |
1822 | ||
1823 | SBP2_DMA_ALLOC("single bulk"); | |
1824 | ||
1825 | /* | |
1826 | * Handle case where we get a command w/o s/g enabled (but | |
1827 | * check for transfers larger than 64K) | |
1828 | */ | |
1829 | if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) { | |
1830 | ||
1831 | orb->data_descriptor_lo = command->cmd_dma; | |
1832 | orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen); | |
1833 | ||
1834 | } else { | |
1835 | struct sbp2_unrestricted_page_table *sg_element = | |
1836 | &command->scatter_gather_element[0]; | |
1837 | u32 sg_count, sg_len; | |
1838 | dma_addr_t sg_addr; | |
1839 | ||
1840 | /* | |
1841 | * Need to turn this into page tables, since the | |
1842 | * buffer is too large. | |
1843 | */ | |
1844 | orb->data_descriptor_lo = command->sge_dma; | |
1845 | ||
1846 | /* Use page tables (s/g) */ | |
1847 | orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1); | |
1848 | ||
1849 | /* | |
1850 | * fill out our sbp-2 page tables (and split up | |
1851 | * the large buffer) | |
1852 | */ | |
1853 | sg_count = 0; | |
1854 | sg_len = scsi_request_bufflen; | |
1855 | sg_addr = command->cmd_dma; | |
1856 | while (sg_len) { | |
1857 | sg_element[sg_count].segment_base_lo = sg_addr; | |
1858 | if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) { | |
1859 | sg_element[sg_count].length_segment_base_hi = | |
1860 | PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH); | |
1861 | sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH; | |
1862 | sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH; | |
1863 | } else { | |
1864 | sg_element[sg_count].length_segment_base_hi = | |
1865 | PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len); | |
1866 | sg_len = 0; | |
1867 | } | |
1868 | sg_count++; | |
1869 | } | |
1870 | ||
1871 | /* Number of page table (s/g) elements */ | |
1872 | orb->misc |= ORB_SET_DATA_SIZE(sg_count); | |
1873 | ||
1874 | sbp2util_packet_dump(sg_element, | |
1875 | (sizeof(struct sbp2_unrestricted_page_table)) * sg_count, | |
1876 | "sbp2 s/g list", command->sge_dma); | |
1877 | ||
1878 | /* Byte swap page tables if necessary */ | |
1879 | sbp2util_cpu_to_be32_buffer(sg_element, | |
1880 | (sizeof(struct sbp2_unrestricted_page_table)) * | |
1881 | sg_count); | |
1882 | } | |
1883 | } | |
1884 | ||
1da177e4 LT |
1885 | /* |
1886 | * This function is called to create the actual command orb and s/g list | |
1887 | * out of the scsi command itself. | |
1888 | */ | |
cf8d2c09 SR |
1889 | static void sbp2_create_command_orb(struct scsi_id_instance_data *scsi_id, |
1890 | struct sbp2_command_info *command, | |
1891 | unchar *scsi_cmd, | |
1892 | unsigned int scsi_use_sg, | |
1893 | unsigned int scsi_request_bufflen, | |
1894 | void *scsi_request_buffer, | |
1895 | enum dma_data_direction dma_dir) | |
1da177e4 LT |
1896 | { |
1897 | struct sbp2scsi_host_info *hi = scsi_id->hi; | |
a237f35f | 1898 | struct scatterlist *sgpnt = (struct scatterlist *)scsi_request_buffer; |
1da177e4 | 1899 | struct sbp2_command_orb *command_orb = &command->command_orb; |
cf8d2c09 | 1900 | u32 orb_direction; |
1da177e4 LT |
1901 | |
1902 | /* | |
1903 | * Set-up our command ORB.. | |
1904 | * | |
1905 | * NOTE: We're doing unrestricted page tables (s/g), as this is | |
1906 | * best performance (at least with the devices I have). This means | |
1907 | * that data_size becomes the number of s/g elements, and | |
1908 | * page_size should be zero (for unrestricted). | |
1909 | */ | |
1910 | command_orb->next_ORB_hi = ORB_SET_NULL_PTR(1); | |
1911 | command_orb->next_ORB_lo = 0x0; | |
1912 | command_orb->misc = ORB_SET_MAX_PAYLOAD(scsi_id->max_payload_size); | |
1913 | command_orb->misc |= ORB_SET_SPEED(scsi_id->speed_code); | |
a237f35f | 1914 | command_orb->misc |= ORB_SET_NOTIFY(1); /* Notify us when complete */ |
1da177e4 | 1915 | |
43863eba | 1916 | if (dma_dir == DMA_NONE) |
a237f35f | 1917 | orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER; |
43863eba | 1918 | else if (dma_dir == DMA_TO_DEVICE && scsi_request_bufflen) |
a237f35f | 1919 | orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA; |
43863eba | 1920 | else if (dma_dir == DMA_FROM_DEVICE && scsi_request_bufflen) |
a237f35f | 1921 | orb_direction = ORB_DIRECTION_READ_FROM_MEDIA; |
43863eba SR |
1922 | else { |
1923 | SBP2_WARN("Falling back to DMA_NONE"); | |
1924 | orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER; | |
1da177e4 LT |
1925 | } |
1926 | ||
cf8d2c09 | 1927 | /* Set-up our pagetable stuff */ |
1da177e4 | 1928 | if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) { |
1da177e4 | 1929 | SBP2_DEBUG("No data transfer"); |
1da177e4 LT |
1930 | command_orb->data_descriptor_hi = 0x0; |
1931 | command_orb->data_descriptor_lo = 0x0; | |
1932 | command_orb->misc |= ORB_SET_DIRECTION(1); | |
1da177e4 | 1933 | } else if (scsi_use_sg) { |
1da177e4 | 1934 | SBP2_DEBUG("Use scatter/gather"); |
cf8d2c09 SR |
1935 | sbp2_prep_command_orb_sg(command_orb, hi, command, scsi_use_sg, |
1936 | sgpnt, orb_direction, dma_dir); | |
1da177e4 | 1937 | } else { |
1da177e4 | 1938 | SBP2_DEBUG("No scatter/gather"); |
cf8d2c09 SR |
1939 | sbp2_prep_command_orb_no_sg(command_orb, hi, command, sgpnt, |
1940 | orb_direction, scsi_request_bufflen, | |
1941 | scsi_request_buffer, dma_dir); | |
1da177e4 LT |
1942 | } |
1943 | ||
cf8d2c09 | 1944 | /* Byte swap command ORB if necessary */ |
1da177e4 LT |
1945 | sbp2util_cpu_to_be32_buffer(command_orb, sizeof(struct sbp2_command_orb)); |
1946 | ||
cf8d2c09 | 1947 | /* Put our scsi command in the command ORB */ |
1da177e4 LT |
1948 | memset(command_orb->cdb, 0, 12); |
1949 | memcpy(command_orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd)); | |
1da177e4 LT |
1950 | } |
1951 | ||
1952 | /* | |
1953 | * This function is called in order to begin a regular SBP-2 command. | |
1954 | */ | |
1955 | static int sbp2_link_orb_command(struct scsi_id_instance_data *scsi_id, | |
1956 | struct sbp2_command_info *command) | |
1957 | { | |
1958 | struct sbp2scsi_host_info *hi = scsi_id->hi; | |
1959 | struct sbp2_command_orb *command_orb = &command->command_orb; | |
1960 | struct node_entry *ne = scsi_id->ne; | |
1961 | u64 addr; | |
1962 | ||
1963 | outstanding_orb_incr; | |
1964 | SBP2_ORB_DEBUG("sending command orb %p, total orbs = %x", | |
a237f35f | 1965 | command_orb, global_outstanding_command_orbs); |
1da177e4 LT |
1966 | |
1967 | pci_dma_sync_single_for_device(hi->host->pdev, command->command_orb_dma, | |
1968 | sizeof(struct sbp2_command_orb), | |
1969 | PCI_DMA_BIDIRECTIONAL); | |
1970 | pci_dma_sync_single_for_device(hi->host->pdev, command->sge_dma, | |
1971 | sizeof(command->scatter_gather_element), | |
1972 | PCI_DMA_BIDIRECTIONAL); | |
1973 | /* | |
1974 | * Check to see if there are any previous orbs to use | |
1975 | */ | |
1976 | if (scsi_id->last_orb == NULL) { | |
1977 | quadlet_t data[2]; | |
1978 | ||
1979 | /* | |
1980 | * Ok, let's write to the target's management agent register | |
1981 | */ | |
1982 | addr = scsi_id->sbp2_command_block_agent_addr + SBP2_ORB_POINTER_OFFSET; | |
1983 | data[0] = ORB_SET_NODE_ID(hi->host->node_id); | |
1984 | data[1] = command->command_orb_dma; | |
1985 | sbp2util_cpu_to_be32_buffer(data, 8); | |
1986 | ||
1987 | SBP2_ORB_DEBUG("write command agent, command orb %p", command_orb); | |
1988 | ||
1989 | if (sbp2util_node_write_no_wait(ne, addr, data, 8) < 0) { | |
1990 | SBP2_ERR("sbp2util_node_write_no_wait failed.\n"); | |
1991 | return -EIO; | |
1992 | } | |
1993 | ||
1994 | SBP2_ORB_DEBUG("write command agent complete"); | |
1995 | ||
1996 | scsi_id->last_orb = command_orb; | |
1997 | scsi_id->last_orb_dma = command->command_orb_dma; | |
1998 | ||
1999 | } else { | |
2000 | quadlet_t data; | |
2001 | ||
2002 | /* | |
2003 | * We have an orb already sent (maybe or maybe not | |
2004 | * processed) that we can append this orb to. So do so, | |
2005 | * and ring the doorbell. Have to be very careful | |
2006 | * modifying these next orb pointers, as they are accessed | |
2007 | * both by the sbp2 device and us. | |
2008 | */ | |
2009 | scsi_id->last_orb->next_ORB_lo = | |
a237f35f | 2010 | cpu_to_be32(command->command_orb_dma); |
1da177e4 LT |
2011 | /* Tells hardware that this pointer is valid */ |
2012 | scsi_id->last_orb->next_ORB_hi = 0x0; | |
a237f35f SR |
2013 | pci_dma_sync_single_for_device(hi->host->pdev, |
2014 | scsi_id->last_orb_dma, | |
1da177e4 LT |
2015 | sizeof(struct sbp2_command_orb), |
2016 | PCI_DMA_BIDIRECTIONAL); | |
2017 | ||
2018 | /* | |
2019 | * Ring the doorbell | |
2020 | */ | |
2021 | data = cpu_to_be32(command->command_orb_dma); | |
2022 | addr = scsi_id->sbp2_command_block_agent_addr + SBP2_DOORBELL_OFFSET; | |
2023 | ||
2024 | SBP2_ORB_DEBUG("ring doorbell, command orb %p", command_orb); | |
2025 | ||
2026 | if (sbp2util_node_write_no_wait(ne, addr, &data, 4) < 0) { | |
2027 | SBP2_ERR("sbp2util_node_write_no_wait failed"); | |
a237f35f | 2028 | return -EIO; |
1da177e4 LT |
2029 | } |
2030 | ||
2031 | scsi_id->last_orb = command_orb; | |
2032 | scsi_id->last_orb_dma = command->command_orb_dma; | |
2033 | ||
2034 | } | |
a237f35f | 2035 | return 0; |
1da177e4 LT |
2036 | } |
2037 | ||
2038 | /* | |
2039 | * This function is called in order to begin a regular SBP-2 command. | |
2040 | */ | |
2041 | static int sbp2_send_command(struct scsi_id_instance_data *scsi_id, | |
2042 | struct scsi_cmnd *SCpnt, | |
2043 | void (*done)(struct scsi_cmnd *)) | |
2044 | { | |
2045 | unchar *cmd = (unchar *) SCpnt->cmnd; | |
2046 | unsigned int request_bufflen = SCpnt->request_bufflen; | |
2047 | struct sbp2_command_info *command; | |
2048 | ||
d024ebc6 | 2049 | SBP2_DEBUG_ENTER(); |
1da177e4 LT |
2050 | SBP2_DEBUG("SCSI transfer size = %x", request_bufflen); |
2051 | SBP2_DEBUG("SCSI s/g elements = %x", (unsigned int)SCpnt->use_sg); | |
2052 | ||
2053 | /* | |
2054 | * Allocate a command orb and s/g structure | |
2055 | */ | |
2056 | command = sbp2util_allocate_command_orb(scsi_id, SCpnt, done); | |
2057 | if (!command) { | |
a237f35f | 2058 | return -EIO; |
1da177e4 LT |
2059 | } |
2060 | ||
1da177e4 LT |
2061 | /* |
2062 | * Now actually fill in the comamnd orb and sbp2 s/g list | |
2063 | */ | |
2064 | sbp2_create_command_orb(scsi_id, command, cmd, SCpnt->use_sg, | |
2065 | request_bufflen, SCpnt->request_buffer, | |
2066 | SCpnt->sc_data_direction); | |
1da177e4 LT |
2067 | |
2068 | sbp2util_packet_dump(&command->command_orb, sizeof(struct sbp2_command_orb), | |
2069 | "sbp2 command orb", command->command_orb_dma); | |
2070 | ||
2071 | /* | |
2072 | * Initialize status fifo | |
2073 | */ | |
2074 | memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block)); | |
2075 | ||
2076 | /* | |
2077 | * Link up the orb, and ring the doorbell if needed | |
2078 | */ | |
2079 | sbp2_link_orb_command(scsi_id, command); | |
2080 | ||
a237f35f | 2081 | return 0; |
1da177e4 LT |
2082 | } |
2083 | ||
1da177e4 LT |
2084 | /* |
2085 | * Translates SBP-2 status into SCSI sense data for check conditions | |
2086 | */ | |
2087 | static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status, unchar *sense_data) | |
2088 | { | |
d024ebc6 | 2089 | SBP2_DEBUG_ENTER(); |
1da177e4 LT |
2090 | |
2091 | /* | |
2092 | * Ok, it's pretty ugly... ;-) | |
2093 | */ | |
2094 | sense_data[0] = 0x70; | |
2095 | sense_data[1] = 0x0; | |
2096 | sense_data[2] = sbp2_status[9]; | |
2097 | sense_data[3] = sbp2_status[12]; | |
2098 | sense_data[4] = sbp2_status[13]; | |
2099 | sense_data[5] = sbp2_status[14]; | |
2100 | sense_data[6] = sbp2_status[15]; | |
2101 | sense_data[7] = 10; | |
2102 | sense_data[8] = sbp2_status[16]; | |
2103 | sense_data[9] = sbp2_status[17]; | |
2104 | sense_data[10] = sbp2_status[18]; | |
2105 | sense_data[11] = sbp2_status[19]; | |
2106 | sense_data[12] = sbp2_status[10]; | |
2107 | sense_data[13] = sbp2_status[11]; | |
2108 | sense_data[14] = sbp2_status[20]; | |
2109 | sense_data[15] = sbp2_status[21]; | |
2110 | ||
a237f35f | 2111 | return sbp2_status[8] & 0x3f; /* return scsi status */ |
1da177e4 LT |
2112 | } |
2113 | ||
1da177e4 LT |
2114 | /* |
2115 | * This function deals with status writes from the SBP-2 device | |
2116 | */ | |
2117 | static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid, int destid, | |
2118 | quadlet_t *data, u64 addr, size_t length, u16 fl) | |
2119 | { | |
2120 | struct sbp2scsi_host_info *hi; | |
2121 | struct scsi_id_instance_data *scsi_id = NULL, *scsi_id_tmp; | |
1da177e4 LT |
2122 | struct scsi_cmnd *SCpnt = NULL; |
2123 | u32 scsi_status = SBP2_SCSI_STATUS_GOOD; | |
2124 | struct sbp2_command_info *command; | |
79456197 | 2125 | unsigned long flags; |
1da177e4 | 2126 | |
d024ebc6 | 2127 | SBP2_DEBUG_ENTER(); |
1da177e4 LT |
2128 | |
2129 | sbp2util_packet_dump(data, length, "sbp2 status write by device", (u32)addr); | |
2130 | ||
2131 | if (!host) { | |
2132 | SBP2_ERR("host is NULL - this is bad!"); | |
a237f35f | 2133 | return RCODE_ADDRESS_ERROR; |
1da177e4 LT |
2134 | } |
2135 | ||
2136 | hi = hpsb_get_hostinfo(&sbp2_highlevel, host); | |
2137 | ||
2138 | if (!hi) { | |
2139 | SBP2_ERR("host info is NULL - this is bad!"); | |
a237f35f | 2140 | return RCODE_ADDRESS_ERROR; |
1da177e4 LT |
2141 | } |
2142 | ||
2143 | /* | |
35bdddb8 SR |
2144 | * Find our scsi_id structure by looking at the status fifo address |
2145 | * written to by the sbp2 device. | |
1da177e4 | 2146 | */ |
1da177e4 | 2147 | list_for_each_entry(scsi_id_tmp, &hi->scsi_ids, scsi_list) { |
35bdddb8 SR |
2148 | if (scsi_id_tmp->ne->nodeid == nodeid && |
2149 | scsi_id_tmp->status_fifo_addr == addr) { | |
1da177e4 LT |
2150 | scsi_id = scsi_id_tmp; |
2151 | break; | |
2152 | } | |
2153 | } | |
2154 | ||
2155 | if (!scsi_id) { | |
2156 | SBP2_ERR("scsi_id is NULL - device is gone?"); | |
a237f35f | 2157 | return RCODE_ADDRESS_ERROR; |
1da177e4 LT |
2158 | } |
2159 | ||
2160 | /* | |
2161 | * Put response into scsi_id status fifo... | |
2162 | */ | |
2163 | memcpy(&scsi_id->status_block, data, length); | |
2164 | ||
2165 | /* | |
2166 | * Byte swap first two quadlets (8 bytes) of status for processing | |
2167 | */ | |
2168 | sbp2util_be32_to_cpu_buffer(&scsi_id->status_block, 8); | |
2169 | ||
2170 | /* | |
2171 | * Handle command ORB status here if necessary. First, need to match status with command. | |
2172 | */ | |
2173 | command = sbp2util_find_command_for_orb(scsi_id, scsi_id->status_block.ORB_offset_lo); | |
2174 | if (command) { | |
2175 | ||
2176 | SBP2_DEBUG("Found status for command ORB"); | |
2177 | pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma, | |
2178 | sizeof(struct sbp2_command_orb), | |
2179 | PCI_DMA_BIDIRECTIONAL); | |
2180 | pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma, | |
2181 | sizeof(command->scatter_gather_element), | |
2182 | PCI_DMA_BIDIRECTIONAL); | |
2183 | ||
2184 | SBP2_ORB_DEBUG("matched command orb %p", &command->command_orb); | |
2185 | outstanding_orb_decr; | |
2186 | ||
2187 | /* | |
2188 | * Matched status with command, now grab scsi command pointers and check status | |
2189 | */ | |
2190 | SCpnt = command->Current_SCpnt; | |
24c7cd06 | 2191 | spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags); |
1da177e4 | 2192 | sbp2util_mark_command_completed(scsi_id, command); |
24c7cd06 | 2193 | spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags); |
1da177e4 LT |
2194 | |
2195 | if (SCpnt) { | |
2196 | ||
2197 | /* | |
2198 | * See if the target stored any scsi status information | |
2199 | */ | |
2200 | if (STATUS_GET_LENGTH(scsi_id->status_block.ORB_offset_hi_misc) > 1) { | |
2201 | /* | |
2202 | * Translate SBP-2 status to SCSI sense data | |
2203 | */ | |
2204 | SBP2_DEBUG("CHECK CONDITION"); | |
2205 | scsi_status = sbp2_status_to_sense_data((unchar *)&scsi_id->status_block, SCpnt->sense_buffer); | |
2206 | } | |
2207 | ||
2208 | /* | |
2209 | * Check to see if the dead bit is set. If so, we'll have to initiate | |
2210 | * a fetch agent reset. | |
2211 | */ | |
2212 | if (STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc)) { | |
2213 | ||
2214 | /* | |
2215 | * Initiate a fetch agent reset. | |
2216 | */ | |
2217 | SBP2_DEBUG("Dead bit set - initiating fetch agent reset"); | |
2218 | sbp2_agent_reset(scsi_id, 0); | |
2219 | } | |
2220 | ||
2221 | SBP2_ORB_DEBUG("completing command orb %p", &command->command_orb); | |
2222 | } | |
2223 | ||
2224 | /* | |
2225 | * Check here to see if there are no commands in-use. If there are none, we can | |
2226 | * null out last orb so that next time around we write directly to the orb pointer... | |
2227 | * Quick start saves one 1394 bus transaction. | |
2228 | */ | |
79456197 | 2229 | spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags); |
1da177e4 LT |
2230 | if (list_empty(&scsi_id->sbp2_command_orb_inuse)) { |
2231 | scsi_id->last_orb = NULL; | |
2232 | } | |
79456197 | 2233 | spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags); |
1da177e4 LT |
2234 | |
2235 | } else { | |
2236 | ||
2237 | /* | |
2238 | * It's probably a login/logout/reconnect status. | |
2239 | */ | |
2240 | if ((scsi_id->login_orb_dma == scsi_id->status_block.ORB_offset_lo) || | |
2241 | (scsi_id->query_logins_orb_dma == scsi_id->status_block.ORB_offset_lo) || | |
2242 | (scsi_id->reconnect_orb_dma == scsi_id->status_block.ORB_offset_lo) || | |
2243 | (scsi_id->logout_orb_dma == scsi_id->status_block.ORB_offset_lo)) { | |
2244 | atomic_set(&scsi_id->sbp2_login_complete, 1); | |
2245 | } | |
2246 | } | |
2247 | ||
2248 | if (SCpnt) { | |
2249 | ||
2250 | /* Complete the SCSI command. */ | |
2251 | SBP2_DEBUG("Completing SCSI command"); | |
2252 | sbp2scsi_complete_command(scsi_id, scsi_status, SCpnt, | |
2253 | command->Current_done); | |
2254 | SBP2_ORB_DEBUG("command orb completed"); | |
2255 | } | |
2256 | ||
a237f35f | 2257 | return RCODE_COMPLETE; |
1da177e4 LT |
2258 | } |
2259 | ||
1da177e4 LT |
2260 | /************************************** |
2261 | * SCSI interface related section | |
2262 | **************************************/ | |
2263 | ||
2264 | /* | |
2265 | * This routine is the main request entry routine for doing I/O. It is | |
2266 | * called from the scsi stack directly. | |
2267 | */ | |
2268 | static int sbp2scsi_queuecommand(struct scsi_cmnd *SCpnt, | |
2269 | void (*done)(struct scsi_cmnd *)) | |
2270 | { | |
2271 | struct scsi_id_instance_data *scsi_id = | |
2272 | (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0]; | |
2273 | struct sbp2scsi_host_info *hi; | |
abd559b1 | 2274 | int result = DID_NO_CONNECT << 16; |
1da177e4 | 2275 | |
d024ebc6 SR |
2276 | SBP2_DEBUG_ENTER(); |
2277 | #if (CONFIG_IEEE1394_SBP2_DEBUG >= 2) || defined(CONFIG_IEEE1394_SBP2_PACKET_DUMP) | |
2278 | scsi_print_command(SCpnt); | |
2279 | #endif | |
1da177e4 | 2280 | |
abd559b1 JM |
2281 | if (!sbp2util_node_is_available(scsi_id)) |
2282 | goto done; | |
1da177e4 LT |
2283 | |
2284 | hi = scsi_id->hi; | |
2285 | ||
2286 | if (!hi) { | |
2287 | SBP2_ERR("sbp2scsi_host_info is NULL - this is bad!"); | |
abd559b1 | 2288 | goto done; |
1da177e4 LT |
2289 | } |
2290 | ||
2291 | /* | |
2292 | * Until we handle multiple luns, just return selection time-out | |
2293 | * to any IO directed at non-zero LUNs | |
2294 | */ | |
abd559b1 JM |
2295 | if (SCpnt->device->lun) |
2296 | goto done; | |
1da177e4 LT |
2297 | |
2298 | /* | |
2299 | * Check for request sense command, and handle it here | |
2300 | * (autorequest sense) | |
2301 | */ | |
2302 | if (SCpnt->cmnd[0] == REQUEST_SENSE) { | |
2303 | SBP2_DEBUG("REQUEST_SENSE"); | |
2304 | memcpy(SCpnt->request_buffer, SCpnt->sense_buffer, SCpnt->request_bufflen); | |
2305 | memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer)); | |
2306 | sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_GOOD, SCpnt, done); | |
abd559b1 | 2307 | return 0; |
1da177e4 LT |
2308 | } |
2309 | ||
2310 | /* | |
2311 | * Check to see if we are in the middle of a bus reset. | |
2312 | */ | |
2313 | if (!hpsb_node_entry_valid(scsi_id->ne)) { | |
2314 | SBP2_ERR("Bus reset in progress - rejecting command"); | |
abd559b1 JM |
2315 | result = DID_BUS_BUSY << 16; |
2316 | goto done; | |
1da177e4 LT |
2317 | } |
2318 | ||
43863eba SR |
2319 | /* |
2320 | * Bidirectional commands are not yet implemented, | |
2321 | * and unknown transfer direction not handled. | |
2322 | */ | |
2323 | if (SCpnt->sc_data_direction == DMA_BIDIRECTIONAL) { | |
2324 | SBP2_ERR("Cannot handle DMA_BIDIRECTIONAL - rejecting command"); | |
2325 | result = DID_ERROR << 16; | |
2326 | goto done; | |
2327 | } | |
2328 | ||
1da177e4 LT |
2329 | /* |
2330 | * Try and send our SCSI command | |
2331 | */ | |
2332 | if (sbp2_send_command(scsi_id, SCpnt, done)) { | |
2333 | SBP2_ERR("Error sending SCSI command"); | |
2334 | sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_SELECTION_TIMEOUT, | |
2335 | SCpnt, done); | |
2336 | } | |
abd559b1 | 2337 | return 0; |
1da177e4 | 2338 | |
abd559b1 JM |
2339 | done: |
2340 | SCpnt->result = result; | |
2341 | done(SCpnt); | |
2342 | return 0; | |
1da177e4 LT |
2343 | } |
2344 | ||
2345 | /* | |
2346 | * This function is called in order to complete all outstanding SBP-2 | |
2347 | * commands (in case of resets, etc.). | |
2348 | */ | |
2349 | static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id, | |
2350 | u32 status) | |
2351 | { | |
2352 | struct sbp2scsi_host_info *hi = scsi_id->hi; | |
2353 | struct list_head *lh; | |
2354 | struct sbp2_command_info *command; | |
79456197 | 2355 | unsigned long flags; |
1da177e4 | 2356 | |
d024ebc6 | 2357 | SBP2_DEBUG_ENTER(); |
1da177e4 | 2358 | |
79456197 | 2359 | spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags); |
1da177e4 LT |
2360 | while (!list_empty(&scsi_id->sbp2_command_orb_inuse)) { |
2361 | SBP2_DEBUG("Found pending command to complete"); | |
2362 | lh = scsi_id->sbp2_command_orb_inuse.next; | |
2363 | command = list_entry(lh, struct sbp2_command_info, list); | |
2364 | pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma, | |
2365 | sizeof(struct sbp2_command_orb), | |
2366 | PCI_DMA_BIDIRECTIONAL); | |
2367 | pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma, | |
2368 | sizeof(command->scatter_gather_element), | |
2369 | PCI_DMA_BIDIRECTIONAL); | |
2370 | sbp2util_mark_command_completed(scsi_id, command); | |
2371 | if (command->Current_SCpnt) { | |
2372 | command->Current_SCpnt->result = status << 16; | |
2373 | command->Current_done(command->Current_SCpnt); | |
2374 | } | |
2375 | } | |
79456197 | 2376 | spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags); |
1da177e4 LT |
2377 | |
2378 | return; | |
2379 | } | |
2380 | ||
2381 | /* | |
2382 | * This function is called in order to complete a regular SBP-2 command. | |
2383 | * | |
2384 | * This can be called in interrupt context. | |
2385 | */ | |
2386 | static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id, | |
2387 | u32 scsi_status, struct scsi_cmnd *SCpnt, | |
2388 | void (*done)(struct scsi_cmnd *)) | |
2389 | { | |
d024ebc6 | 2390 | SBP2_DEBUG_ENTER(); |
1da177e4 LT |
2391 | |
2392 | /* | |
2393 | * Sanity | |
2394 | */ | |
2395 | if (!SCpnt) { | |
2396 | SBP2_ERR("SCpnt is NULL"); | |
2397 | return; | |
2398 | } | |
2399 | ||
2400 | /* | |
2401 | * If a bus reset is in progress and there was an error, don't | |
2402 | * complete the command, just let it get retried at the end of the | |
2403 | * bus reset. | |
2404 | */ | |
a237f35f SR |
2405 | if (!hpsb_node_entry_valid(scsi_id->ne) |
2406 | && (scsi_status != SBP2_SCSI_STATUS_GOOD)) { | |
1da177e4 LT |
2407 | SBP2_ERR("Bus reset in progress - retry command later"); |
2408 | return; | |
2409 | } | |
a237f35f | 2410 | |
1da177e4 LT |
2411 | /* |
2412 | * Switch on scsi status | |
2413 | */ | |
2414 | switch (scsi_status) { | |
a237f35f | 2415 | case SBP2_SCSI_STATUS_GOOD: |
8f0525ff | 2416 | SCpnt->result = DID_OK << 16; |
a237f35f | 2417 | break; |
1da177e4 | 2418 | |
a237f35f SR |
2419 | case SBP2_SCSI_STATUS_BUSY: |
2420 | SBP2_ERR("SBP2_SCSI_STATUS_BUSY"); | |
2421 | SCpnt->result = DID_BUS_BUSY << 16; | |
2422 | break; | |
1da177e4 | 2423 | |
a237f35f SR |
2424 | case SBP2_SCSI_STATUS_CHECK_CONDITION: |
2425 | SBP2_DEBUG("SBP2_SCSI_STATUS_CHECK_CONDITION"); | |
8f0525ff | 2426 | SCpnt->result = CHECK_CONDITION << 1 | DID_OK << 16; |
1da177e4 | 2427 | #if CONFIG_IEEE1394_SBP2_DEBUG >= 1 |
a237f35f | 2428 | scsi_print_command(SCpnt); |
d024ebc6 | 2429 | scsi_print_sense(SBP2_DEVICE_NAME, SCpnt); |
1da177e4 | 2430 | #endif |
a237f35f | 2431 | break; |
1da177e4 | 2432 | |
a237f35f SR |
2433 | case SBP2_SCSI_STATUS_SELECTION_TIMEOUT: |
2434 | SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT"); | |
2435 | SCpnt->result = DID_NO_CONNECT << 16; | |
2436 | scsi_print_command(SCpnt); | |
2437 | break; | |
1da177e4 | 2438 | |
a237f35f SR |
2439 | case SBP2_SCSI_STATUS_CONDITION_MET: |
2440 | case SBP2_SCSI_STATUS_RESERVATION_CONFLICT: | |
2441 | case SBP2_SCSI_STATUS_COMMAND_TERMINATED: | |
2442 | SBP2_ERR("Bad SCSI status = %x", scsi_status); | |
2443 | SCpnt->result = DID_ERROR << 16; | |
2444 | scsi_print_command(SCpnt); | |
2445 | break; | |
1da177e4 | 2446 | |
a237f35f SR |
2447 | default: |
2448 | SBP2_ERR("Unsupported SCSI status = %x", scsi_status); | |
2449 | SCpnt->result = DID_ERROR << 16; | |
1da177e4 LT |
2450 | } |
2451 | ||
1da177e4 LT |
2452 | /* |
2453 | * If a bus reset is in progress and there was an error, complete | |
2454 | * the command as busy so that it will get retried. | |
2455 | */ | |
a237f35f SR |
2456 | if (!hpsb_node_entry_valid(scsi_id->ne) |
2457 | && (scsi_status != SBP2_SCSI_STATUS_GOOD)) { | |
1da177e4 LT |
2458 | SBP2_ERR("Completing command with busy (bus reset)"); |
2459 | SCpnt->result = DID_BUS_BUSY << 16; | |
2460 | } | |
2461 | ||
2462 | /* | |
2463 | * If a unit attention occurs, return busy status so it gets | |
2464 | * retried... it could have happened because of a 1394 bus reset | |
2465 | * or hot-plug... | |
8f0525ff SR |
2466 | * XXX DID_BUS_BUSY is actually a bad idea because it will defy |
2467 | * the scsi layer's retry logic. | |
1da177e4 LT |
2468 | */ |
2469 | #if 0 | |
2470 | if ((scsi_status == SBP2_SCSI_STATUS_CHECK_CONDITION) && | |
2471 | (SCpnt->sense_buffer[2] == UNIT_ATTENTION)) { | |
2472 | SBP2_DEBUG("UNIT ATTENTION - return busy"); | |
2473 | SCpnt->result = DID_BUS_BUSY << 16; | |
2474 | } | |
2475 | #endif | |
2476 | ||
2477 | /* | |
2478 | * Tell scsi stack that we're done with this command | |
2479 | */ | |
a237f35f | 2480 | done(SCpnt); |
1da177e4 LT |
2481 | } |
2482 | ||
abd559b1 | 2483 | static int sbp2scsi_slave_alloc(struct scsi_device *sdev) |
1da177e4 | 2484 | { |
a80614d1 SR |
2485 | struct scsi_id_instance_data *scsi_id = |
2486 | (struct scsi_id_instance_data *)sdev->host->hostdata[0]; | |
2487 | ||
2488 | scsi_id->sdev = sdev; | |
2489 | ||
24d3bf88 | 2490 | if (scsi_id->workarounds & SBP2_WORKAROUND_INQUIRY_36) |
a80614d1 | 2491 | sdev->inquiry_len = 36; |
abd559b1 JM |
2492 | return 0; |
2493 | } | |
2494 | ||
abd559b1 JM |
2495 | static int sbp2scsi_slave_configure(struct scsi_device *sdev) |
2496 | { | |
24d3bf88 SR |
2497 | struct scsi_id_instance_data *scsi_id = |
2498 | (struct scsi_id_instance_data *)sdev->host->hostdata[0]; | |
2499 | ||
abd559b1 | 2500 | blk_queue_dma_alignment(sdev->request_queue, (512 - 1)); |
365c786f BC |
2501 | sdev->use_10_for_rw = 1; |
2502 | sdev->use_10_for_ms = 1; | |
24d3bf88 SR |
2503 | |
2504 | if (sdev->type == TYPE_DISK && | |
2505 | scsi_id->workarounds & SBP2_WORKAROUND_MODE_SENSE_8) | |
2506 | sdev->skip_ms_page_8 = 1; | |
e9a1c52c SR |
2507 | if (scsi_id->workarounds & SBP2_WORKAROUND_FIX_CAPACITY) |
2508 | sdev->fix_capacity = 1; | |
1da177e4 LT |
2509 | return 0; |
2510 | } | |
2511 | ||
abd559b1 JM |
2512 | static void sbp2scsi_slave_destroy(struct scsi_device *sdev) |
2513 | { | |
2514 | ((struct scsi_id_instance_data *)sdev->host->hostdata[0])->sdev = NULL; | |
2515 | return; | |
2516 | } | |
2517 | ||
1da177e4 LT |
2518 | /* |
2519 | * Called by scsi stack when something has really gone wrong. Usually | |
2520 | * called when a command has timed-out for some reason. | |
2521 | */ | |
2522 | static int sbp2scsi_abort(struct scsi_cmnd *SCpnt) | |
2523 | { | |
2524 | struct scsi_id_instance_data *scsi_id = | |
2525 | (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0]; | |
2526 | struct sbp2scsi_host_info *hi = scsi_id->hi; | |
2527 | struct sbp2_command_info *command; | |
24c7cd06 | 2528 | unsigned long flags; |
1da177e4 LT |
2529 | |
2530 | SBP2_ERR("aborting sbp2 command"); | |
2531 | scsi_print_command(SCpnt); | |
2532 | ||
abd559b1 | 2533 | if (sbp2util_node_is_available(scsi_id)) { |
1da177e4 LT |
2534 | |
2535 | /* | |
2536 | * Right now, just return any matching command structures | |
2537 | * to the free pool. | |
2538 | */ | |
24c7cd06 | 2539 | spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags); |
1da177e4 LT |
2540 | command = sbp2util_find_command_for_SCpnt(scsi_id, SCpnt); |
2541 | if (command) { | |
2542 | SBP2_DEBUG("Found command to abort"); | |
2543 | pci_dma_sync_single_for_cpu(hi->host->pdev, | |
2544 | command->command_orb_dma, | |
2545 | sizeof(struct sbp2_command_orb), | |
2546 | PCI_DMA_BIDIRECTIONAL); | |
2547 | pci_dma_sync_single_for_cpu(hi->host->pdev, | |
2548 | command->sge_dma, | |
2549 | sizeof(command->scatter_gather_element), | |
2550 | PCI_DMA_BIDIRECTIONAL); | |
2551 | sbp2util_mark_command_completed(scsi_id, command); | |
2552 | if (command->Current_SCpnt) { | |
2553 | command->Current_SCpnt->result = DID_ABORT << 16; | |
2554 | command->Current_done(command->Current_SCpnt); | |
2555 | } | |
2556 | } | |
24c7cd06 | 2557 | spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags); |
1da177e4 LT |
2558 | |
2559 | /* | |
2560 | * Initiate a fetch agent reset. | |
2561 | */ | |
2562 | sbp2_agent_reset(scsi_id, 0); | |
2563 | sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY); | |
2564 | } | |
2565 | ||
a237f35f | 2566 | return SUCCESS; |
1da177e4 LT |
2567 | } |
2568 | ||
2569 | /* | |
2570 | * Called by scsi stack when something has really gone wrong. | |
2571 | */ | |
abd559b1 | 2572 | static int sbp2scsi_reset(struct scsi_cmnd *SCpnt) |
1da177e4 LT |
2573 | { |
2574 | struct scsi_id_instance_data *scsi_id = | |
2575 | (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0]; | |
2576 | ||
2577 | SBP2_ERR("reset requested"); | |
2578 | ||
abd559b1 | 2579 | if (sbp2util_node_is_available(scsi_id)) { |
1da177e4 LT |
2580 | SBP2_ERR("Generating sbp2 fetch agent reset"); |
2581 | sbp2_agent_reset(scsi_id, 0); | |
2582 | } | |
2583 | ||
abd559b1 | 2584 | return SUCCESS; |
94d0e7b8 JG |
2585 | } |
2586 | ||
a237f35f SR |
2587 | static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *dev, |
2588 | struct device_attribute *attr, | |
2589 | char *buf) | |
1da177e4 LT |
2590 | { |
2591 | struct scsi_device *sdev; | |
2592 | struct scsi_id_instance_data *scsi_id; | |
2593 | int lun; | |
2594 | ||
2595 | if (!(sdev = to_scsi_device(dev))) | |
2596 | return 0; | |
2597 | ||
2598 | if (!(scsi_id = (struct scsi_id_instance_data *)sdev->host->hostdata[0])) | |
2599 | return 0; | |
2600 | ||
e309fc6d | 2601 | lun = ORB_SET_LUN(scsi_id->sbp2_lun); |
1da177e4 LT |
2602 | |
2603 | return sprintf(buf, "%016Lx:%d:%d\n", (unsigned long long)scsi_id->ne->guid, | |
2604 | scsi_id->ud->id, lun); | |
2605 | } | |
2606 | static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL); | |
2607 | ||
2608 | static struct device_attribute *sbp2_sysfs_sdev_attrs[] = { | |
2609 | &dev_attr_ieee1394_id, | |
2610 | NULL | |
2611 | }; | |
2612 | ||
2613 | MODULE_AUTHOR("Ben Collins <bcollins@debian.org>"); | |
2614 | MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver"); | |
2615 | MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME); | |
2616 | MODULE_LICENSE("GPL"); | |
2617 | ||
2618 | /* SCSI host template */ | |
2619 | static struct scsi_host_template scsi_driver_template = { | |
2620 | .module = THIS_MODULE, | |
2621 | .name = "SBP-2 IEEE-1394", | |
2622 | .proc_name = SBP2_DEVICE_NAME, | |
1da177e4 LT |
2623 | .queuecommand = sbp2scsi_queuecommand, |
2624 | .eh_abort_handler = sbp2scsi_abort, | |
2625 | .eh_device_reset_handler = sbp2scsi_reset, | |
abd559b1 | 2626 | .slave_alloc = sbp2scsi_slave_alloc, |
1da177e4 | 2627 | .slave_configure = sbp2scsi_slave_configure, |
abd559b1 | 2628 | .slave_destroy = sbp2scsi_slave_destroy, |
1da177e4 LT |
2629 | .this_id = -1, |
2630 | .sg_tablesize = SG_ALL, | |
2631 | .use_clustering = ENABLE_CLUSTERING, | |
2632 | .cmd_per_lun = SBP2_MAX_CMDS, | |
2633 | .can_queue = SBP2_MAX_CMDS, | |
2634 | .emulated = 1, | |
2635 | .sdev_attrs = sbp2_sysfs_sdev_attrs, | |
2636 | }; | |
2637 | ||
2638 | static int sbp2_module_init(void) | |
2639 | { | |
2640 | int ret; | |
2641 | ||
d024ebc6 | 2642 | SBP2_DEBUG_ENTER(); |
1da177e4 | 2643 | |
1da177e4 LT |
2644 | /* Module load debug option to force one command at a time (serializing I/O) */ |
2645 | if (serialize_io) { | |
2bab359a JM |
2646 | SBP2_INFO("Driver forced to serialize I/O (serialize_io=1)"); |
2647 | SBP2_INFO("Try serialize_io=0 for better performance"); | |
1da177e4 LT |
2648 | scsi_driver_template.can_queue = 1; |
2649 | scsi_driver_template.cmd_per_lun = 1; | |
2650 | } | |
2651 | ||
24d3bf88 SR |
2652 | if (sbp2_default_workarounds & SBP2_WORKAROUND_128K_MAX_TRANS && |
2653 | (max_sectors * 512) > (128 * 1024)) | |
2654 | max_sectors = 128 * 1024 / 512; | |
1da177e4 LT |
2655 | scsi_driver_template.max_sectors = max_sectors; |
2656 | ||
1da177e4 LT |
2657 | /* Register our high level driver with 1394 stack */ |
2658 | hpsb_register_highlevel(&sbp2_highlevel); | |
2659 | ||
2660 | ret = hpsb_register_protocol(&sbp2_driver); | |
2661 | if (ret) { | |
2662 | SBP2_ERR("Failed to register protocol"); | |
2663 | hpsb_unregister_highlevel(&sbp2_highlevel); | |
2664 | return ret; | |
2665 | } | |
2666 | ||
2667 | return 0; | |
2668 | } | |
2669 | ||
2670 | static void __exit sbp2_module_exit(void) | |
2671 | { | |
d024ebc6 | 2672 | SBP2_DEBUG_ENTER(); |
1da177e4 LT |
2673 | |
2674 | hpsb_unregister_protocol(&sbp2_driver); | |
2675 | ||
2676 | hpsb_unregister_highlevel(&sbp2_highlevel); | |
2677 | } | |
2678 | ||
2679 | module_init(sbp2_module_init); | |
2680 | module_exit(sbp2_module_exit); |