Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / firewire / fw-sbp2.c
blob8ca2832315be552c1633e90d609f5b35313c8ce4
1 /*
2 * SBP2 driver (SCSI over IEEE1394)
4 * Copyright (C) 2005-2007 Kristian Hoegsberg <krh@bitplanet.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * The basic structure of this driver is based on the old storage driver,
23 * drivers/ieee1394/sbp2.c, originally written by
24 * James Goodwin <jamesg@filanet.com>
25 * with later contributions and ongoing maintenance from
26 * Ben Collins <bcollins@debian.org>,
27 * Stefan Richter <stefanr@s5r6.in-berlin.de>
28 * and many others.
31 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
32 =======
33 #include <linux/blkdev.h>
34 #include <linux/delay.h>
35 #include <linux/device.h>
36 #include <linux/dma-mapping.h>
37 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
38 #include <linux/kernel.h>
39 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
40 =======
41 #include <linux/mod_devicetable.h>
42 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
43 #include <linux/module.h>
44 #include <linux/moduleparam.h>
45 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
46 #include <linux/mod_devicetable.h>
47 #include <linux/device.h>
48 =======
49 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
50 #include <linux/scatterlist.h>
51 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
52 #include <linux/dma-mapping.h>
53 #include <linux/blkdev.h>
54 =======
55 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
56 #include <linux/string.h>
57 #include <linux/stringify.h>
58 #include <linux/timer.h>
59 #include <linux/workqueue.h>
60 #include <asm/system.h>
62 #include <scsi/scsi.h>
63 #include <scsi/scsi_cmnd.h>
64 #include <scsi/scsi_device.h>
65 #include <scsi/scsi_host.h>
67 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
68 #include "fw-transaction.h"
69 #include "fw-topology.h"
70 =======
71 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
72 #include "fw-device.h"
73 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
74 =======
75 #include "fw-topology.h"
76 #include "fw-transaction.h"
77 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
80 * So far only bridges from Oxford Semiconductor are known to support
81 * concurrent logins. Depending on firmware, four or two concurrent logins
82 * are possible on OXFW911 and newer Oxsemi bridges.
84 * Concurrent logins are useful together with cluster filesystems.
86 static int sbp2_param_exclusive_login = 1;
87 module_param_named(exclusive_login, sbp2_param_exclusive_login, bool, 0644);
88 MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device "
89 "(default = Y, use N for concurrent initiators)");
92 * Flags for firmware oddities
94 * - 128kB max transfer
95 * Limit transfer size. Necessary for some old bridges.
97 * - 36 byte inquiry
98 * When scsi_mod probes the device, let the inquiry command look like that
99 * from MS Windows.
101 * - skip mode page 8
102 * Suppress sending of mode_sense for mode page 8 if the device pretends to
103 * support the SCSI Primary Block commands instead of Reduced Block Commands.
105 * - fix capacity
106 * Tell sd_mod to correct the last sector number reported by read_capacity.
107 * Avoids access beyond actual disk limits on devices with an off-by-one bug.
108 * Don't use this with devices which don't have this bug.
110 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
111 =======
112 * - delay inquiry
113 * Wait extra SBP2_INQUIRY_DELAY seconds after login before SCSI inquiry.
115 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
116 * - override internal blacklist
117 * Instead of adding to the built-in blacklist, use only the workarounds
118 * specified in the module load parameter.
119 * Useful if a blacklist entry interfered with a non-broken device.
121 #define SBP2_WORKAROUND_128K_MAX_TRANS 0x1
122 #define SBP2_WORKAROUND_INQUIRY_36 0x2
123 #define SBP2_WORKAROUND_MODE_SENSE_8 0x4
124 #define SBP2_WORKAROUND_FIX_CAPACITY 0x8
125 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
126 =======
127 #define SBP2_WORKAROUND_DELAY_INQUIRY 0x10
128 #define SBP2_INQUIRY_DELAY 12
129 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
130 #define SBP2_WORKAROUND_OVERRIDE 0x100
132 static int sbp2_param_workarounds;
133 module_param_named(workarounds, sbp2_param_workarounds, int, 0644);
134 MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
135 ", 128kB max transfer = " __stringify(SBP2_WORKAROUND_128K_MAX_TRANS)
136 ", 36 byte inquiry = " __stringify(SBP2_WORKAROUND_INQUIRY_36)
137 ", skip mode page 8 = " __stringify(SBP2_WORKAROUND_MODE_SENSE_8)
138 ", fix capacity = " __stringify(SBP2_WORKAROUND_FIX_CAPACITY)
139 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
140 =======
141 ", delay inquiry = " __stringify(SBP2_WORKAROUND_DELAY_INQUIRY)
142 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
143 ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
144 ", or a combination)");
146 /* I don't know why the SCSI stack doesn't define something like this... */
147 typedef void (*scsi_done_fn_t)(struct scsi_cmnd *);
149 static const char sbp2_driver_name[] = "sbp2";
152 * We create one struct sbp2_logical_unit per SBP-2 Logical Unit Number Entry
153 * and one struct scsi_device per sbp2_logical_unit.
155 struct sbp2_logical_unit {
156 struct sbp2_target *tgt;
157 struct list_head link;
158 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
159 struct scsi_device *sdev;
160 =======
161 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
162 struct fw_address_handler address_handler;
163 struct list_head orb_list;
165 u64 command_block_agent_address;
166 u16 lun;
167 int login_id;
170 * The generation is updated once we've logged in or reconnected
171 * to the logical unit. Thus, I/O to the device will automatically
172 * fail and get retried if it happens in a window where the device
173 * is not ready, e.g. after a bus reset but before we reconnect.
175 int generation;
176 int retries;
177 struct delayed_work work;
178 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
179 =======
180 bool has_sdev;
181 bool blocked;
182 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
186 * We create one struct sbp2_target per IEEE 1212 Unit Directory
187 * and one struct Scsi_Host per sbp2_target.
189 struct sbp2_target {
190 struct kref kref;
191 struct fw_unit *unit;
192 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
193 =======
194 const char *bus_id;
195 struct list_head lu_list;
196 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
198 u64 management_agent_address;
199 int directory_id;
200 int node_id;
201 int address_high;
202 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
204 unsigned workarounds;
205 struct list_head lu_list;
207 =======
208 unsigned int workarounds;
209 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
210 unsigned int mgt_orb_timeout;
211 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
212 =======
214 int dont_block; /* counter for each logical unit */
215 int blocked; /* ditto */
216 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
220 * Per section 7.4.8 of the SBP-2 spec, a mgt_ORB_timeout value can be
221 * provided in the config rom. Most devices do provide a value, which
222 * we'll use for login management orbs, but with some sane limits.
224 #define SBP2_MIN_LOGIN_ORB_TIMEOUT 5000U /* Timeout in ms */
225 #define SBP2_MAX_LOGIN_ORB_TIMEOUT 40000U /* Timeout in ms */
226 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
227 #define SBP2_ORB_TIMEOUT 2000 /* Timeout in ms */
228 =======
229 #define SBP2_ORB_TIMEOUT 2000U /* Timeout in ms */
230 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
231 #define SBP2_ORB_NULL 0x80000000
232 #define SBP2_MAX_SG_ELEMENT_LENGTH 0xf000
233 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
234 =======
235 #define SBP2_RETRY_LIMIT 0xf /* 15 retries */
236 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
238 #define SBP2_DIRECTION_TO_MEDIA 0x0
239 #define SBP2_DIRECTION_FROM_MEDIA 0x1
241 /* Unit directory keys */
242 #define SBP2_CSR_UNIT_CHARACTERISTICS 0x3a
243 #define SBP2_CSR_FIRMWARE_REVISION 0x3c
244 #define SBP2_CSR_LOGICAL_UNIT_NUMBER 0x14
245 #define SBP2_CSR_LOGICAL_UNIT_DIRECTORY 0xd4
247 /* Management orb opcodes */
248 #define SBP2_LOGIN_REQUEST 0x0
249 #define SBP2_QUERY_LOGINS_REQUEST 0x1
250 #define SBP2_RECONNECT_REQUEST 0x3
251 #define SBP2_SET_PASSWORD_REQUEST 0x4
252 #define SBP2_LOGOUT_REQUEST 0x7
253 #define SBP2_ABORT_TASK_REQUEST 0xb
254 #define SBP2_ABORT_TASK_SET 0xc
255 #define SBP2_LOGICAL_UNIT_RESET 0xe
256 #define SBP2_TARGET_RESET_REQUEST 0xf
258 /* Offsets for command block agent registers */
259 #define SBP2_AGENT_STATE 0x00
260 #define SBP2_AGENT_RESET 0x04
261 #define SBP2_ORB_POINTER 0x08
262 #define SBP2_DOORBELL 0x10
263 #define SBP2_UNSOLICITED_STATUS_ENABLE 0x14
265 /* Status write response codes */
266 #define SBP2_STATUS_REQUEST_COMPLETE 0x0
267 #define SBP2_STATUS_TRANSPORT_FAILURE 0x1
268 #define SBP2_STATUS_ILLEGAL_REQUEST 0x2
269 #define SBP2_STATUS_VENDOR_DEPENDENT 0x3
271 #define STATUS_GET_ORB_HIGH(v) ((v).status & 0xffff)
272 #define STATUS_GET_SBP_STATUS(v) (((v).status >> 16) & 0xff)
273 #define STATUS_GET_LEN(v) (((v).status >> 24) & 0x07)
274 #define STATUS_GET_DEAD(v) (((v).status >> 27) & 0x01)
275 #define STATUS_GET_RESPONSE(v) (((v).status >> 28) & 0x03)
276 #define STATUS_GET_SOURCE(v) (((v).status >> 30) & 0x03)
277 #define STATUS_GET_ORB_LOW(v) ((v).orb_low)
278 #define STATUS_GET_DATA(v) ((v).data)
280 struct sbp2_status {
281 u32 status;
282 u32 orb_low;
283 u8 data[24];
286 struct sbp2_pointer {
287 u32 high;
288 u32 low;
291 struct sbp2_orb {
292 struct fw_transaction t;
293 struct kref kref;
294 dma_addr_t request_bus;
295 int rcode;
296 struct sbp2_pointer pointer;
297 void (*callback)(struct sbp2_orb * orb, struct sbp2_status * status);
298 struct list_head link;
301 #define MANAGEMENT_ORB_LUN(v) ((v))
302 #define MANAGEMENT_ORB_FUNCTION(v) ((v) << 16)
303 #define MANAGEMENT_ORB_RECONNECT(v) ((v) << 20)
304 #define MANAGEMENT_ORB_EXCLUSIVE(v) ((v) ? 1 << 28 : 0)
305 #define MANAGEMENT_ORB_REQUEST_FORMAT(v) ((v) << 29)
306 #define MANAGEMENT_ORB_NOTIFY ((1) << 31)
308 #define MANAGEMENT_ORB_RESPONSE_LENGTH(v) ((v))
309 #define MANAGEMENT_ORB_PASSWORD_LENGTH(v) ((v) << 16)
311 struct sbp2_management_orb {
312 struct sbp2_orb base;
313 struct {
314 struct sbp2_pointer password;
315 struct sbp2_pointer response;
316 u32 misc;
317 u32 length;
318 struct sbp2_pointer status_fifo;
319 } request;
320 __be32 response[4];
321 dma_addr_t response_bus;
322 struct completion done;
323 struct sbp2_status status;
326 #define LOGIN_RESPONSE_GET_LOGIN_ID(v) ((v).misc & 0xffff)
327 #define LOGIN_RESPONSE_GET_LENGTH(v) (((v).misc >> 16) & 0xffff)
329 struct sbp2_login_response {
330 u32 misc;
331 struct sbp2_pointer command_block_agent;
332 u32 reconnect_hold;
334 #define COMMAND_ORB_DATA_SIZE(v) ((v))
335 #define COMMAND_ORB_PAGE_SIZE(v) ((v) << 16)
336 #define COMMAND_ORB_PAGE_TABLE_PRESENT ((1) << 19)
337 #define COMMAND_ORB_MAX_PAYLOAD(v) ((v) << 20)
338 #define COMMAND_ORB_SPEED(v) ((v) << 24)
339 #define COMMAND_ORB_DIRECTION(v) ((v) << 27)
340 #define COMMAND_ORB_REQUEST_FORMAT(v) ((v) << 29)
341 #define COMMAND_ORB_NOTIFY ((1) << 31)
343 struct sbp2_command_orb {
344 struct sbp2_orb base;
345 struct {
346 struct sbp2_pointer next;
347 struct sbp2_pointer data_descriptor;
348 u32 misc;
349 u8 command_block[12];
350 } request;
351 struct scsi_cmnd *cmd;
352 scsi_done_fn_t done;
353 struct sbp2_logical_unit *lu;
355 struct sbp2_pointer page_table[SG_ALL] __attribute__((aligned(8)));
356 dma_addr_t page_table_bus;
360 * List of devices with known bugs.
362 * The firmware_revision field, masked with 0xffff00, is the best
363 * indicator for the type of bridge chip of a device. It yields a few
364 * false positives but this did not break correctly behaving devices
365 * so far. We use ~0 as a wildcard, since the 24 bit values we get
366 * from the config rom can never match that.
368 static const struct {
369 u32 firmware_revision;
370 u32 model;
371 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
372 unsigned workarounds;
373 =======
374 unsigned int workarounds;
375 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
376 } sbp2_workarounds_table[] = {
377 /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
378 .firmware_revision = 0x002800,
379 .model = 0x001010,
380 .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
381 SBP2_WORKAROUND_MODE_SENSE_8,
383 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
384 =======
385 /* DViCO Momobay FX-3A with TSB42AA9A bridge */ {
386 .firmware_revision = 0x002800,
387 .model = 0x000000,
388 .workarounds = SBP2_WORKAROUND_DELAY_INQUIRY,
390 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
391 /* Initio bridges, actually only needed for some older ones */ {
392 .firmware_revision = 0x000200,
393 .model = ~0,
394 .workarounds = SBP2_WORKAROUND_INQUIRY_36,
396 /* Symbios bridge */ {
397 .firmware_revision = 0xa0b800,
398 .model = ~0,
399 .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
401 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
402 =======
403 /* Datafab MD2-FW2 with Symbios/LSILogic SYM13FW500 bridge */ {
404 .firmware_revision = 0x002600,
405 .model = ~0,
406 .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
408 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
411 * There are iPods (2nd gen, 3rd gen) with model_id == 0, but
412 * these iPods do not feature the read_capacity bug according
413 * to one report. Read_capacity behaviour as well as model_id
414 * could change due to Apple-supplied firmware updates though.
417 /* iPod 4th generation. */ {
418 .firmware_revision = 0x0a2700,
419 .model = 0x000021,
420 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
422 /* iPod mini */ {
423 .firmware_revision = 0x0a2700,
424 .model = 0x000023,
425 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
427 /* iPod Photo */ {
428 .firmware_revision = 0x0a2700,
429 .model = 0x00007e,
430 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
434 static void
435 free_orb(struct kref *kref)
437 struct sbp2_orb *orb = container_of(kref, struct sbp2_orb, kref);
439 kfree(orb);
442 static void
443 sbp2_status_write(struct fw_card *card, struct fw_request *request,
444 int tcode, int destination, int source,
445 int generation, int speed,
446 unsigned long long offset,
447 void *payload, size_t length, void *callback_data)
449 struct sbp2_logical_unit *lu = callback_data;
450 struct sbp2_orb *orb;
451 struct sbp2_status status;
452 size_t header_size;
453 unsigned long flags;
455 if (tcode != TCODE_WRITE_BLOCK_REQUEST ||
456 length == 0 || length > sizeof(status)) {
457 fw_send_response(card, request, RCODE_TYPE_ERROR);
458 return;
461 header_size = min(length, 2 * sizeof(u32));
462 fw_memcpy_from_be32(&status, payload, header_size);
463 if (length > header_size)
464 memcpy(status.data, payload + 8, length - header_size);
465 if (STATUS_GET_SOURCE(status) == 2 || STATUS_GET_SOURCE(status) == 3) {
466 fw_notify("non-orb related status write, not handled\n");
467 fw_send_response(card, request, RCODE_COMPLETE);
468 return;
471 /* Lookup the orb corresponding to this status write. */
472 spin_lock_irqsave(&card->lock, flags);
473 list_for_each_entry(orb, &lu->orb_list, link) {
474 if (STATUS_GET_ORB_HIGH(status) == 0 &&
475 STATUS_GET_ORB_LOW(status) == orb->request_bus) {
476 orb->rcode = RCODE_COMPLETE;
477 list_del(&orb->link);
478 break;
481 spin_unlock_irqrestore(&card->lock, flags);
483 if (&orb->link != &lu->orb_list)
484 orb->callback(orb, &status);
485 else
486 fw_error("status write for unknown orb\n");
488 kref_put(&orb->kref, free_orb);
490 fw_send_response(card, request, RCODE_COMPLETE);
493 static void
494 complete_transaction(struct fw_card *card, int rcode,
495 void *payload, size_t length, void *data)
497 struct sbp2_orb *orb = data;
498 unsigned long flags;
501 * This is a little tricky. We can get the status write for
502 * the orb before we get this callback. The status write
503 * handler above will assume the orb pointer transaction was
504 * successful and set the rcode to RCODE_COMPLETE for the orb.
505 * So this callback only sets the rcode if it hasn't already
506 * been set and only does the cleanup if the transaction
507 * failed and we didn't already get a status write.
509 spin_lock_irqsave(&card->lock, flags);
511 if (orb->rcode == -1)
512 orb->rcode = rcode;
513 if (orb->rcode != RCODE_COMPLETE) {
514 list_del(&orb->link);
515 spin_unlock_irqrestore(&card->lock, flags);
516 orb->callback(orb, NULL);
517 } else {
518 spin_unlock_irqrestore(&card->lock, flags);
521 kref_put(&orb->kref, free_orb);
524 static void
525 sbp2_send_orb(struct sbp2_orb *orb, struct sbp2_logical_unit *lu,
526 int node_id, int generation, u64 offset)
528 struct fw_device *device = fw_device(lu->tgt->unit->device.parent);
529 unsigned long flags;
531 orb->pointer.high = 0;
532 orb->pointer.low = orb->request_bus;
533 fw_memcpy_to_be32(&orb->pointer, &orb->pointer, sizeof(orb->pointer));
535 spin_lock_irqsave(&device->card->lock, flags);
536 list_add_tail(&orb->link, &lu->orb_list);
537 spin_unlock_irqrestore(&device->card->lock, flags);
539 /* Take a ref for the orb list and for the transaction callback. */
540 kref_get(&orb->kref);
541 kref_get(&orb->kref);
543 fw_send_request(device->card, &orb->t, TCODE_WRITE_BLOCK_REQUEST,
544 node_id, generation, device->max_speed, offset,
545 &orb->pointer, sizeof(orb->pointer),
546 complete_transaction, orb);
549 static int sbp2_cancel_orbs(struct sbp2_logical_unit *lu)
551 struct fw_device *device = fw_device(lu->tgt->unit->device.parent);
552 struct sbp2_orb *orb, *next;
553 struct list_head list;
554 unsigned long flags;
555 int retval = -ENOENT;
557 INIT_LIST_HEAD(&list);
558 spin_lock_irqsave(&device->card->lock, flags);
559 list_splice_init(&lu->orb_list, &list);
560 spin_unlock_irqrestore(&device->card->lock, flags);
562 list_for_each_entry_safe(orb, next, &list, link) {
563 retval = 0;
564 if (fw_cancel_transaction(device->card, &orb->t) == 0)
565 continue;
567 orb->rcode = RCODE_CANCELLED;
568 orb->callback(orb, NULL);
571 return retval;
574 static void
575 complete_management_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
577 struct sbp2_management_orb *orb =
578 container_of(base_orb, struct sbp2_management_orb, base);
580 if (status)
581 memcpy(&orb->status, status, sizeof(*status));
582 complete(&orb->done);
585 static int
586 sbp2_send_management_orb(struct sbp2_logical_unit *lu, int node_id,
587 int generation, int function, int lun_or_login_id,
588 void *response)
590 struct fw_device *device = fw_device(lu->tgt->unit->device.parent);
591 struct sbp2_management_orb *orb;
592 unsigned int timeout;
593 int retval = -ENOMEM;
595 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
596 =======
597 if (function == SBP2_LOGOUT_REQUEST && fw_device_is_shutdown(device))
598 return 0;
600 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
601 orb = kzalloc(sizeof(*orb), GFP_ATOMIC);
602 if (orb == NULL)
603 return -ENOMEM;
605 kref_init(&orb->base.kref);
606 orb->response_bus =
607 dma_map_single(device->card->device, &orb->response,
608 sizeof(orb->response), DMA_FROM_DEVICE);
609 if (dma_mapping_error(orb->response_bus))
610 goto fail_mapping_response;
612 orb->request.response.high = 0;
613 orb->request.response.low = orb->response_bus;
615 orb->request.misc =
616 MANAGEMENT_ORB_NOTIFY |
617 MANAGEMENT_ORB_FUNCTION(function) |
618 MANAGEMENT_ORB_LUN(lun_or_login_id);
619 orb->request.length =
620 MANAGEMENT_ORB_RESPONSE_LENGTH(sizeof(orb->response));
622 orb->request.status_fifo.high = lu->address_handler.offset >> 32;
623 orb->request.status_fifo.low = lu->address_handler.offset;
625 if (function == SBP2_LOGIN_REQUEST) {
626 /* Ask for 2^2 == 4 seconds reconnect grace period */
627 orb->request.misc |=
628 MANAGEMENT_ORB_RECONNECT(2) |
629 MANAGEMENT_ORB_EXCLUSIVE(sbp2_param_exclusive_login);
630 timeout = lu->tgt->mgt_orb_timeout;
631 } else {
632 timeout = SBP2_ORB_TIMEOUT;
635 fw_memcpy_to_be32(&orb->request, &orb->request, sizeof(orb->request));
637 init_completion(&orb->done);
638 orb->base.callback = complete_management_orb;
640 orb->base.request_bus =
641 dma_map_single(device->card->device, &orb->request,
642 sizeof(orb->request), DMA_TO_DEVICE);
643 if (dma_mapping_error(orb->base.request_bus))
644 goto fail_mapping_request;
646 sbp2_send_orb(&orb->base, lu, node_id, generation,
647 lu->tgt->management_agent_address);
649 wait_for_completion_timeout(&orb->done, msecs_to_jiffies(timeout));
651 retval = -EIO;
652 if (sbp2_cancel_orbs(lu) == 0) {
653 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
654 fw_error("orb reply timed out, rcode=0x%02x\n",
655 orb->base.rcode);
656 =======
657 fw_error("%s: orb reply timed out, rcode=0x%02x\n",
658 lu->tgt->bus_id, orb->base.rcode);
659 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
660 goto out;
663 if (orb->base.rcode != RCODE_COMPLETE) {
664 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
665 fw_error("management write failed, rcode 0x%02x\n",
666 orb->base.rcode);
667 =======
668 fw_error("%s: management write failed, rcode 0x%02x\n",
669 lu->tgt->bus_id, orb->base.rcode);
670 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
671 goto out;
674 if (STATUS_GET_RESPONSE(orb->status) != 0 ||
675 STATUS_GET_SBP_STATUS(orb->status) != 0) {
676 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
677 fw_error("error status: %d:%d\n",
678 =======
679 fw_error("%s: error status: %d:%d\n", lu->tgt->bus_id,
680 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
681 STATUS_GET_RESPONSE(orb->status),
682 STATUS_GET_SBP_STATUS(orb->status));
683 goto out;
686 retval = 0;
687 out:
688 dma_unmap_single(device->card->device, orb->base.request_bus,
689 sizeof(orb->request), DMA_TO_DEVICE);
690 fail_mapping_request:
691 dma_unmap_single(device->card->device, orb->response_bus,
692 sizeof(orb->response), DMA_FROM_DEVICE);
693 fail_mapping_response:
694 if (response)
695 fw_memcpy_from_be32(response,
696 orb->response, sizeof(orb->response));
697 kref_put(&orb->base.kref, free_orb);
699 return retval;
702 static void
703 complete_agent_reset_write(struct fw_card *card, int rcode,
704 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
705 void *payload, size_t length, void *data)
706 =======
707 void *payload, size_t length, void *done)
708 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
710 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
711 struct fw_transaction *t = data;
712 =======
713 complete(done);
715 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
717 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
718 kfree(t);
719 =======
720 static void sbp2_agent_reset(struct sbp2_logical_unit *lu)
722 struct fw_device *device = fw_device(lu->tgt->unit->device.parent);
723 DECLARE_COMPLETION_ONSTACK(done);
724 struct fw_transaction t;
725 static u32 z;
727 fw_send_request(device->card, &t, TCODE_WRITE_QUADLET_REQUEST,
728 lu->tgt->node_id, lu->generation, device->max_speed,
729 lu->command_block_agent_address + SBP2_AGENT_RESET,
730 &z, sizeof(z), complete_agent_reset_write, &done);
731 wait_for_completion(&done);
732 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
735 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
736 static int sbp2_agent_reset(struct sbp2_logical_unit *lu)
737 =======
738 static void
739 complete_agent_reset_write_no_wait(struct fw_card *card, int rcode,
740 void *payload, size_t length, void *data)
742 kfree(data);
745 static void sbp2_agent_reset_no_wait(struct sbp2_logical_unit *lu)
746 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
748 struct fw_device *device = fw_device(lu->tgt->unit->device.parent);
749 struct fw_transaction *t;
750 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
751 static u32 zero;
752 =======
753 static u32 z;
754 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
756 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
757 t = kzalloc(sizeof(*t), GFP_ATOMIC);
758 =======
759 t = kmalloc(sizeof(*t), GFP_ATOMIC);
760 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
761 if (t == NULL)
762 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
763 return -ENOMEM;
764 =======
765 return;
766 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
768 fw_send_request(device->card, t, TCODE_WRITE_QUADLET_REQUEST,
769 lu->tgt->node_id, lu->generation, device->max_speed,
770 lu->command_block_agent_address + SBP2_AGENT_RESET,
771 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
772 &zero, sizeof(zero), complete_agent_reset_write, t);
773 =======
774 &z, sizeof(z), complete_agent_reset_write_no_wait, t);
776 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
778 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
779 return 0;
780 =======
781 static void sbp2_set_generation(struct sbp2_logical_unit *lu, int generation)
783 struct fw_card *card = fw_device(lu->tgt->unit->device.parent)->card;
784 unsigned long flags;
786 /* serialize with comparisons of lu->generation and card->generation */
787 spin_lock_irqsave(&card->lock, flags);
788 lu->generation = generation;
789 spin_unlock_irqrestore(&card->lock, flags);
792 static inline void sbp2_allow_block(struct sbp2_logical_unit *lu)
795 * We may access dont_block without taking card->lock here:
796 * All callers of sbp2_allow_block() and all callers of sbp2_unblock()
797 * are currently serialized against each other.
798 * And a wrong result in sbp2_conditionally_block()'s access of
799 * dont_block is rather harmless, it simply misses its first chance.
801 --lu->tgt->dont_block;
805 * Blocks lu->tgt if all of the following conditions are met:
806 * - Login, INQUIRY, and high-level SCSI setup of all of the target's
807 * logical units have been finished (indicated by dont_block == 0).
808 * - lu->generation is stale.
810 * Note, scsi_block_requests() must be called while holding card->lock,
811 * otherwise it might foil sbp2_[conditionally_]unblock()'s attempt to
812 * unblock the target.
814 static void sbp2_conditionally_block(struct sbp2_logical_unit *lu)
816 struct sbp2_target *tgt = lu->tgt;
817 struct fw_card *card = fw_device(tgt->unit->device.parent)->card;
818 struct Scsi_Host *shost =
819 container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
820 unsigned long flags;
822 spin_lock_irqsave(&card->lock, flags);
823 if (!tgt->dont_block && !lu->blocked &&
824 lu->generation != card->generation) {
825 lu->blocked = true;
826 if (++tgt->blocked == 1) {
827 scsi_block_requests(shost);
828 fw_notify("blocked %s\n", lu->tgt->bus_id);
831 spin_unlock_irqrestore(&card->lock, flags);
835 * Unblocks lu->tgt as soon as all its logical units can be unblocked.
836 * Note, it is harmless to run scsi_unblock_requests() outside the
837 * card->lock protected section. On the other hand, running it inside
838 * the section might clash with shost->host_lock.
840 static void sbp2_conditionally_unblock(struct sbp2_logical_unit *lu)
842 struct sbp2_target *tgt = lu->tgt;
843 struct fw_card *card = fw_device(tgt->unit->device.parent)->card;
844 struct Scsi_Host *shost =
845 container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
846 unsigned long flags;
847 bool unblock = false;
849 spin_lock_irqsave(&card->lock, flags);
850 if (lu->blocked && lu->generation == card->generation) {
851 lu->blocked = false;
852 unblock = --tgt->blocked == 0;
854 spin_unlock_irqrestore(&card->lock, flags);
856 if (unblock) {
857 scsi_unblock_requests(shost);
858 fw_notify("unblocked %s\n", lu->tgt->bus_id);
863 * Prevents future blocking of tgt and unblocks it.
864 * Note, it is harmless to run scsi_unblock_requests() outside the
865 * card->lock protected section. On the other hand, running it inside
866 * the section might clash with shost->host_lock.
868 static void sbp2_unblock(struct sbp2_target *tgt)
870 struct fw_card *card = fw_device(tgt->unit->device.parent)->card;
871 struct Scsi_Host *shost =
872 container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
873 unsigned long flags;
875 spin_lock_irqsave(&card->lock, flags);
876 ++tgt->dont_block;
877 spin_unlock_irqrestore(&card->lock, flags);
879 scsi_unblock_requests(shost);
882 static int sbp2_lun2int(u16 lun)
884 struct scsi_lun eight_bytes_lun;
886 memset(&eight_bytes_lun, 0, sizeof(eight_bytes_lun));
887 eight_bytes_lun.scsi_lun[0] = (lun >> 8) & 0xff;
888 eight_bytes_lun.scsi_lun[1] = lun & 0xff;
890 return scsilun_to_int(&eight_bytes_lun);
891 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
894 static void sbp2_release_target(struct kref *kref)
896 struct sbp2_target *tgt = container_of(kref, struct sbp2_target, kref);
897 struct sbp2_logical_unit *lu, *next;
898 struct Scsi_Host *shost =
899 container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
900 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
901 =======
902 struct scsi_device *sdev;
903 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
904 struct fw_device *device = fw_device(tgt->unit->device.parent);
906 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
907 list_for_each_entry_safe(lu, next, &tgt->lu_list, link) {
908 if (lu->sdev)
909 scsi_remove_device(lu->sdev);
910 =======
911 /* prevent deadlocks */
912 sbp2_unblock(tgt);
913 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
915 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
916 if (!fw_device_is_shutdown(device))
917 sbp2_send_management_orb(lu, tgt->node_id,
918 lu->generation, SBP2_LOGOUT_REQUEST,
919 lu->login_id, NULL);
920 =======
921 list_for_each_entry_safe(lu, next, &tgt->lu_list, link) {
922 sdev = scsi_device_lookup(shost, 0, 0, sbp2_lun2int(lu->lun));
923 if (sdev) {
924 scsi_remove_device(sdev);
925 scsi_device_put(sdev);
927 sbp2_send_management_orb(lu, tgt->node_id, lu->generation,
928 SBP2_LOGOUT_REQUEST, lu->login_id, NULL);
929 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
931 fw_core_remove_address_handler(&lu->address_handler);
932 list_del(&lu->link);
933 kfree(lu);
935 scsi_remove_host(shost);
936 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
937 fw_notify("released %s\n", tgt->unit->device.bus_id);
938 =======
939 fw_notify("released %s\n", tgt->bus_id);
940 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
942 put_device(&tgt->unit->device);
943 scsi_host_put(shost);
944 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
945 =======
946 fw_device_put(device);
947 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
950 static struct workqueue_struct *sbp2_wq;
953 * Always get the target's kref when scheduling work on one its units.
954 * Each workqueue job is responsible to call sbp2_target_put() upon return.
956 static void sbp2_queue_work(struct sbp2_logical_unit *lu, unsigned long delay)
958 if (queue_delayed_work(sbp2_wq, &lu->work, delay))
959 kref_get(&lu->tgt->kref);
962 static void sbp2_target_put(struct sbp2_target *tgt)
964 kref_put(&tgt->kref, sbp2_release_target);
967 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
968 =======
969 static void
970 complete_set_busy_timeout(struct fw_card *card, int rcode,
971 void *payload, size_t length, void *done)
973 complete(done);
976 static void sbp2_set_busy_timeout(struct sbp2_logical_unit *lu)
978 struct fw_device *device = fw_device(lu->tgt->unit->device.parent);
979 DECLARE_COMPLETION_ONSTACK(done);
980 struct fw_transaction t;
981 static __be32 busy_timeout;
983 /* FIXME: we should try to set dual-phase cycle_limit too */
984 busy_timeout = cpu_to_be32(SBP2_RETRY_LIMIT);
986 fw_send_request(device->card, &t, TCODE_WRITE_QUADLET_REQUEST,
987 lu->tgt->node_id, lu->generation, device->max_speed,
988 CSR_REGISTER_BASE + CSR_BUSY_TIMEOUT, &busy_timeout,
989 sizeof(busy_timeout), complete_set_busy_timeout, &done);
990 wait_for_completion(&done);
993 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
994 static void sbp2_reconnect(struct work_struct *work);
996 static void sbp2_login(struct work_struct *work)
998 struct sbp2_logical_unit *lu =
999 container_of(work, struct sbp2_logical_unit, work.work);
1000 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1001 struct Scsi_Host *shost =
1002 container_of((void *)lu->tgt, struct Scsi_Host, hostdata[0]);
1003 =======
1004 struct sbp2_target *tgt = lu->tgt;
1005 struct fw_device *device = fw_device(tgt->unit->device.parent);
1006 struct Scsi_Host *shost;
1007 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1008 struct scsi_device *sdev;
1009 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1010 struct scsi_lun eight_bytes_lun;
1011 struct fw_unit *unit = lu->tgt->unit;
1012 struct fw_device *device = fw_device(unit->device.parent);
1013 =======
1014 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1015 struct sbp2_login_response response;
1016 int generation, node_id, local_node_id;
1018 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1019 =======
1020 if (fw_device_is_shutdown(device))
1021 goto out;
1023 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1024 generation = device->generation;
1025 smp_rmb(); /* node_id must not be older than generation */
1026 node_id = device->node_id;
1027 local_node_id = device->card->node_id;
1029 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1030 =======
1031 /* If this is a re-login attempt, log out, or we might be rejected. */
1032 if (lu->has_sdev)
1033 sbp2_send_management_orb(lu, device->node_id, generation,
1034 SBP2_LOGOUT_REQUEST, lu->login_id, NULL);
1036 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1037 if (sbp2_send_management_orb(lu, node_id, generation,
1038 SBP2_LOGIN_REQUEST, lu->lun, &response) < 0) {
1039 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1040 if (lu->retries++ < 5)
1041 =======
1042 if (lu->retries++ < 5) {
1043 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1044 sbp2_queue_work(lu, DIV_ROUND_UP(HZ, 5));
1045 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1046 else
1047 fw_error("failed to login to %s LUN %04x\n",
1048 unit->device.bus_id, lu->lun);
1049 =======
1050 } else {
1051 fw_error("%s: failed to login to LUN %04x\n",
1052 tgt->bus_id, lu->lun);
1053 /* Let any waiting I/O fail from now on. */
1054 sbp2_unblock(lu->tgt);
1056 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1057 goto out;
1060 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1061 lu->generation = generation;
1062 lu->tgt->node_id = node_id;
1063 lu->tgt->address_high = local_node_id << 16;
1064 =======
1065 tgt->node_id = node_id;
1066 tgt->address_high = local_node_id << 16;
1067 sbp2_set_generation(lu, generation);
1068 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1070 /* Get command block agent offset and login id. */
1071 lu->command_block_agent_address =
1072 ((u64) (response.command_block_agent.high & 0xffff) << 32) |
1073 response.command_block_agent.low;
1074 lu->login_id = LOGIN_RESPONSE_GET_LOGIN_ID(response);
1076 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1077 fw_notify("logged in to %s LUN %04x (%d retries)\n",
1078 unit->device.bus_id, lu->lun, lu->retries);
1079 =======
1080 fw_notify("%s: logged in to LUN %04x (%d retries)\n",
1081 tgt->bus_id, lu->lun, lu->retries);
1082 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1084 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1085 #if 0
1086 /* FIXME: The linux1394 sbp2 does this last step. */
1087 sbp2_set_busy_timeout(scsi_id);
1088 #endif
1089 =======
1090 /* set appropriate retry limit(s) in BUSY_TIMEOUT register */
1091 sbp2_set_busy_timeout(lu);
1092 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1094 PREPARE_DELAYED_WORK(&lu->work, sbp2_reconnect);
1095 sbp2_agent_reset(lu);
1097 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1098 memset(&eight_bytes_lun, 0, sizeof(eight_bytes_lun));
1099 eight_bytes_lun.scsi_lun[0] = (lu->lun >> 8) & 0xff;
1100 eight_bytes_lun.scsi_lun[1] = lu->lun & 0xff;
1101 =======
1102 /* This was a re-login. */
1103 if (lu->has_sdev) {
1104 sbp2_cancel_orbs(lu);
1105 sbp2_conditionally_unblock(lu);
1106 goto out;
1108 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1110 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1111 sdev = __scsi_add_device(shost, 0, 0,
1112 scsilun_to_int(&eight_bytes_lun), lu);
1113 if (IS_ERR(sdev)) {
1114 sbp2_send_management_orb(lu, node_id, generation,
1115 SBP2_LOGOUT_REQUEST, lu->login_id, NULL);
1117 * Set this back to sbp2_login so we fall back and
1118 * retry login on bus reset.
1120 PREPARE_DELAYED_WORK(&lu->work, sbp2_login);
1121 } else {
1122 lu->sdev = sdev;
1123 =======
1124 if (lu->tgt->workarounds & SBP2_WORKAROUND_DELAY_INQUIRY)
1125 ssleep(SBP2_INQUIRY_DELAY);
1127 shost = container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
1128 sdev = __scsi_add_device(shost, 0, 0, sbp2_lun2int(lu->lun), lu);
1130 * FIXME: We are unable to perform reconnects while in sbp2_login().
1131 * Therefore __scsi_add_device() will get into trouble if a bus reset
1132 * happens in parallel. It will either fail or leave us with an
1133 * unusable sdev. As a workaround we check for this and retry the
1134 * whole login and SCSI probing.
1137 /* Reported error during __scsi_add_device() */
1138 if (IS_ERR(sdev))
1139 goto out_logout_login;
1141 /* Unreported error during __scsi_add_device() */
1142 smp_rmb(); /* get current card generation */
1143 if (generation != device->card->generation) {
1144 scsi_remove_device(sdev);
1145 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1146 scsi_device_put(sdev);
1147 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1148 =======
1149 goto out_logout_login;
1150 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1152 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1153 =======
1155 /* No error during __scsi_add_device() */
1156 lu->has_sdev = true;
1157 scsi_device_put(sdev);
1158 sbp2_allow_block(lu);
1159 goto out;
1161 out_logout_login:
1162 smp_rmb(); /* generation may have changed */
1163 generation = device->generation;
1164 smp_rmb(); /* node_id must not be older than generation */
1166 sbp2_send_management_orb(lu, device->node_id, generation,
1167 SBP2_LOGOUT_REQUEST, lu->login_id, NULL);
1169 * If a bus reset happened, sbp2_update will have requeued
1170 * lu->work already. Reset the work from reconnect to login.
1172 PREPARE_DELAYED_WORK(&lu->work, sbp2_login);
1173 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1174 out:
1175 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1176 sbp2_target_put(lu->tgt);
1177 =======
1178 sbp2_target_put(tgt);
1179 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1182 static int sbp2_add_logical_unit(struct sbp2_target *tgt, int lun_entry)
1184 struct sbp2_logical_unit *lu;
1186 lu = kmalloc(sizeof(*lu), GFP_KERNEL);
1187 if (!lu)
1188 return -ENOMEM;
1190 lu->address_handler.length = 0x100;
1191 lu->address_handler.address_callback = sbp2_status_write;
1192 lu->address_handler.callback_data = lu;
1194 if (fw_core_add_address_handler(&lu->address_handler,
1195 &fw_high_memory_region) < 0) {
1196 kfree(lu);
1197 return -ENOMEM;
1200 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1201 lu->tgt = tgt;
1202 lu->sdev = NULL;
1203 lu->lun = lun_entry & 0xffff;
1204 lu->retries = 0;
1205 =======
1206 lu->tgt = tgt;
1207 lu->lun = lun_entry & 0xffff;
1208 lu->retries = 0;
1209 lu->has_sdev = false;
1210 lu->blocked = false;
1211 ++tgt->dont_block;
1212 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1213 INIT_LIST_HEAD(&lu->orb_list);
1214 INIT_DELAYED_WORK(&lu->work, sbp2_login);
1216 list_add_tail(&lu->link, &tgt->lu_list);
1217 return 0;
1220 static int sbp2_scan_logical_unit_dir(struct sbp2_target *tgt, u32 *directory)
1222 struct fw_csr_iterator ci;
1223 int key, value;
1225 fw_csr_iterator_init(&ci, directory);
1226 while (fw_csr_iterator_next(&ci, &key, &value))
1227 if (key == SBP2_CSR_LOGICAL_UNIT_NUMBER &&
1228 sbp2_add_logical_unit(tgt, value) < 0)
1229 return -ENOMEM;
1230 return 0;
1233 static int sbp2_scan_unit_dir(struct sbp2_target *tgt, u32 *directory,
1234 u32 *model, u32 *firmware_revision)
1236 struct fw_csr_iterator ci;
1237 int key, value;
1238 unsigned int timeout;
1240 fw_csr_iterator_init(&ci, directory);
1241 while (fw_csr_iterator_next(&ci, &key, &value)) {
1242 switch (key) {
1244 case CSR_DEPENDENT_INFO | CSR_OFFSET:
1245 tgt->management_agent_address =
1246 CSR_REGISTER_BASE + 4 * value;
1247 break;
1249 case CSR_DIRECTORY_ID:
1250 tgt->directory_id = value;
1251 break;
1253 case CSR_MODEL:
1254 *model = value;
1255 break;
1257 case SBP2_CSR_FIRMWARE_REVISION:
1258 *firmware_revision = value;
1259 break;
1261 case SBP2_CSR_UNIT_CHARACTERISTICS:
1262 /* the timeout value is stored in 500ms units */
1263 timeout = ((unsigned int) value >> 8 & 0xff) * 500;
1264 timeout = max(timeout, SBP2_MIN_LOGIN_ORB_TIMEOUT);
1265 tgt->mgt_orb_timeout =
1266 min(timeout, SBP2_MAX_LOGIN_ORB_TIMEOUT);
1268 if (timeout > tgt->mgt_orb_timeout)
1269 fw_notify("%s: config rom contains %ds "
1270 "management ORB timeout, limiting "
1271 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1272 "to %ds\n", tgt->unit->device.bus_id,
1273 =======
1274 "to %ds\n", tgt->bus_id,
1275 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1276 timeout / 1000,
1277 tgt->mgt_orb_timeout / 1000);
1278 break;
1280 case SBP2_CSR_LOGICAL_UNIT_NUMBER:
1281 if (sbp2_add_logical_unit(tgt, value) < 0)
1282 return -ENOMEM;
1283 break;
1285 case SBP2_CSR_LOGICAL_UNIT_DIRECTORY:
1286 if (sbp2_scan_logical_unit_dir(tgt, ci.p + value) < 0)
1287 return -ENOMEM;
1288 break;
1291 return 0;
1294 static void sbp2_init_workarounds(struct sbp2_target *tgt, u32 model,
1295 u32 firmware_revision)
1297 int i;
1298 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1299 unsigned w = sbp2_param_workarounds;
1300 =======
1301 unsigned int w = sbp2_param_workarounds;
1302 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1304 if (w)
1305 fw_notify("Please notify linux1394-devel@lists.sourceforge.net "
1306 "if you need the workarounds parameter for %s\n",
1307 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1308 tgt->unit->device.bus_id);
1309 =======
1310 tgt->bus_id);
1311 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1313 if (w & SBP2_WORKAROUND_OVERRIDE)
1314 goto out;
1316 for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
1318 if (sbp2_workarounds_table[i].firmware_revision !=
1319 (firmware_revision & 0xffffff00))
1320 continue;
1322 if (sbp2_workarounds_table[i].model != model &&
1323 sbp2_workarounds_table[i].model != ~0)
1324 continue;
1326 w |= sbp2_workarounds_table[i].workarounds;
1327 break;
1329 out:
1330 if (w)
1331 fw_notify("Workarounds for %s: 0x%x "
1332 "(firmware_revision 0x%06x, model_id 0x%06x)\n",
1333 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1334 tgt->unit->device.bus_id,
1335 w, firmware_revision, model);
1336 =======
1337 tgt->bus_id, w, firmware_revision, model);
1338 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1339 tgt->workarounds = w;
1342 static struct scsi_host_template scsi_driver_template;
1344 static int sbp2_probe(struct device *dev)
1346 struct fw_unit *unit = fw_unit(dev);
1347 struct fw_device *device = fw_device(unit->device.parent);
1348 struct sbp2_target *tgt;
1349 struct sbp2_logical_unit *lu;
1350 struct Scsi_Host *shost;
1351 u32 model, firmware_revision;
1353 shost = scsi_host_alloc(&scsi_driver_template, sizeof(*tgt));
1354 if (shost == NULL)
1355 return -ENOMEM;
1357 tgt = (struct sbp2_target *)shost->hostdata;
1358 unit->device.driver_data = tgt;
1359 tgt->unit = unit;
1360 kref_init(&tgt->kref);
1361 INIT_LIST_HEAD(&tgt->lu_list);
1362 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1363 =======
1364 tgt->bus_id = unit->device.bus_id;
1365 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1367 if (fw_device_enable_phys_dma(device) < 0)
1368 goto fail_shost_put;
1370 if (scsi_add_host(shost, &unit->device) < 0)
1371 goto fail_shost_put;
1373 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1374 =======
1375 fw_device_get(device);
1377 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1378 /* Initialize to values that won't match anything in our table. */
1379 firmware_revision = 0xff000000;
1380 model = 0xff000000;
1382 /* implicit directory ID */
1383 tgt->directory_id = ((unit->directory - device->config_rom) * 4
1384 + CSR_CONFIG_ROM) & 0xffffff;
1386 if (sbp2_scan_unit_dir(tgt, unit->directory, &model,
1387 &firmware_revision) < 0)
1388 goto fail_tgt_put;
1390 sbp2_init_workarounds(tgt, model, firmware_revision);
1392 get_device(&unit->device);
1394 /* Do the login in a workqueue so we can easily reschedule retries. */
1395 list_for_each_entry(lu, &tgt->lu_list, link)
1396 sbp2_queue_work(lu, 0);
1397 return 0;
1399 fail_tgt_put:
1400 sbp2_target_put(tgt);
1401 return -ENOMEM;
1403 fail_shost_put:
1404 scsi_host_put(shost);
1405 return -ENOMEM;
1408 static int sbp2_remove(struct device *dev)
1410 struct fw_unit *unit = fw_unit(dev);
1411 struct sbp2_target *tgt = unit->device.driver_data;
1413 sbp2_target_put(tgt);
1414 return 0;
1417 static void sbp2_reconnect(struct work_struct *work)
1419 struct sbp2_logical_unit *lu =
1420 container_of(work, struct sbp2_logical_unit, work.work);
1421 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1422 struct fw_unit *unit = lu->tgt->unit;
1423 struct fw_device *device = fw_device(unit->device.parent);
1424 =======
1425 struct sbp2_target *tgt = lu->tgt;
1426 struct fw_device *device = fw_device(tgt->unit->device.parent);
1427 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1428 int generation, node_id, local_node_id;
1430 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1431 =======
1432 if (fw_device_is_shutdown(device))
1433 goto out;
1435 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1436 generation = device->generation;
1437 smp_rmb(); /* node_id must not be older than generation */
1438 node_id = device->node_id;
1439 local_node_id = device->card->node_id;
1441 if (sbp2_send_management_orb(lu, node_id, generation,
1442 SBP2_RECONNECT_REQUEST,
1443 lu->login_id, NULL) < 0) {
1444 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1445 if (lu->retries++ >= 5) {
1446 fw_error("failed to reconnect to %s\n",
1447 unit->device.bus_id);
1448 /* Fall back and try to log in again. */
1449 =======
1451 * If reconnect was impossible even though we are in the
1452 * current generation, fall back and try to log in again.
1454 * We could check for "Function rejected" status, but
1455 * looking at the bus generation as simpler and more general.
1457 smp_rmb(); /* get current card generation */
1458 if (generation == device->card->generation ||
1459 lu->retries++ >= 5) {
1460 fw_error("%s: failed to reconnect\n", tgt->bus_id);
1461 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1462 lu->retries = 0;
1463 PREPARE_DELAYED_WORK(&lu->work, sbp2_login);
1465 sbp2_queue_work(lu, DIV_ROUND_UP(HZ, 5));
1466 goto out;
1469 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1470 lu->generation = generation;
1471 lu->tgt->node_id = node_id;
1472 lu->tgt->address_high = local_node_id << 16;
1473 =======
1474 tgt->node_id = node_id;
1475 tgt->address_high = local_node_id << 16;
1476 sbp2_set_generation(lu, generation);
1477 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1479 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1480 fw_notify("reconnected to %s LUN %04x (%d retries)\n",
1481 unit->device.bus_id, lu->lun, lu->retries);
1482 =======
1483 fw_notify("%s: reconnected to LUN %04x (%d retries)\n",
1484 tgt->bus_id, lu->lun, lu->retries);
1485 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1487 sbp2_agent_reset(lu);
1488 sbp2_cancel_orbs(lu);
1489 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1490 =======
1491 sbp2_conditionally_unblock(lu);
1492 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1493 out:
1494 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1495 sbp2_target_put(lu->tgt);
1496 =======
1497 sbp2_target_put(tgt);
1498 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1501 static void sbp2_update(struct fw_unit *unit)
1503 struct sbp2_target *tgt = unit->device.driver_data;
1504 struct sbp2_logical_unit *lu;
1506 fw_device_enable_phys_dma(fw_device(unit->device.parent));
1509 * Fw-core serializes sbp2_update() against sbp2_remove().
1510 * Iteration over tgt->lu_list is therefore safe here.
1512 list_for_each_entry(lu, &tgt->lu_list, link) {
1513 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1514 =======
1515 sbp2_conditionally_block(lu);
1516 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1517 lu->retries = 0;
1518 sbp2_queue_work(lu, 0);
1522 #define SBP2_UNIT_SPEC_ID_ENTRY 0x0000609e
1523 #define SBP2_SW_VERSION_ENTRY 0x00010483
1525 static const struct fw_device_id sbp2_id_table[] = {
1527 .match_flags = FW_MATCH_SPECIFIER_ID | FW_MATCH_VERSION,
1528 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY,
1529 .version = SBP2_SW_VERSION_ENTRY,
1534 static struct fw_driver sbp2_driver = {
1535 .driver = {
1536 .owner = THIS_MODULE,
1537 .name = sbp2_driver_name,
1538 .bus = &fw_bus_type,
1539 .probe = sbp2_probe,
1540 .remove = sbp2_remove,
1542 .update = sbp2_update,
1543 .id_table = sbp2_id_table,
1546 static unsigned int
1547 sbp2_status_to_sense_data(u8 *sbp2_status, u8 *sense_data)
1549 int sam_status;
1551 sense_data[0] = 0x70;
1552 sense_data[1] = 0x0;
1553 sense_data[2] = sbp2_status[1];
1554 sense_data[3] = sbp2_status[4];
1555 sense_data[4] = sbp2_status[5];
1556 sense_data[5] = sbp2_status[6];
1557 sense_data[6] = sbp2_status[7];
1558 sense_data[7] = 10;
1559 sense_data[8] = sbp2_status[8];
1560 sense_data[9] = sbp2_status[9];
1561 sense_data[10] = sbp2_status[10];
1562 sense_data[11] = sbp2_status[11];
1563 sense_data[12] = sbp2_status[2];
1564 sense_data[13] = sbp2_status[3];
1565 sense_data[14] = sbp2_status[12];
1566 sense_data[15] = sbp2_status[13];
1568 sam_status = sbp2_status[0] & 0x3f;
1570 switch (sam_status) {
1571 case SAM_STAT_GOOD:
1572 case SAM_STAT_CHECK_CONDITION:
1573 case SAM_STAT_CONDITION_MET:
1574 case SAM_STAT_BUSY:
1575 case SAM_STAT_RESERVATION_CONFLICT:
1576 case SAM_STAT_COMMAND_TERMINATED:
1577 return DID_OK << 16 | sam_status;
1579 default:
1580 return DID_ERROR << 16;
1584 static void
1585 complete_command_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
1587 struct sbp2_command_orb *orb =
1588 container_of(base_orb, struct sbp2_command_orb, base);
1589 struct fw_device *device = fw_device(orb->lu->tgt->unit->device.parent);
1590 int result;
1592 if (status != NULL) {
1593 if (STATUS_GET_DEAD(*status))
1594 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1595 sbp2_agent_reset(orb->lu);
1596 =======
1597 sbp2_agent_reset_no_wait(orb->lu);
1598 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1600 switch (STATUS_GET_RESPONSE(*status)) {
1601 case SBP2_STATUS_REQUEST_COMPLETE:
1602 result = DID_OK << 16;
1603 break;
1604 case SBP2_STATUS_TRANSPORT_FAILURE:
1605 result = DID_BUS_BUSY << 16;
1606 break;
1607 case SBP2_STATUS_ILLEGAL_REQUEST:
1608 case SBP2_STATUS_VENDOR_DEPENDENT:
1609 default:
1610 result = DID_ERROR << 16;
1611 break;
1614 if (result == DID_OK << 16 && STATUS_GET_LEN(*status) > 1)
1615 result = sbp2_status_to_sense_data(STATUS_GET_DATA(*status),
1616 orb->cmd->sense_buffer);
1617 } else {
1619 * If the orb completes with status == NULL, something
1620 * went wrong, typically a bus reset happened mid-orb
1621 * or when sending the write (less likely).
1623 result = DID_BUS_BUSY << 16;
1624 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1625 =======
1626 sbp2_conditionally_block(orb->lu);
1627 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1630 dma_unmap_single(device->card->device, orb->base.request_bus,
1631 sizeof(orb->request), DMA_TO_DEVICE);
1633 if (scsi_sg_count(orb->cmd) > 0)
1634 dma_unmap_sg(device->card->device, scsi_sglist(orb->cmd),
1635 scsi_sg_count(orb->cmd),
1636 orb->cmd->sc_data_direction);
1638 if (orb->page_table_bus != 0)
1639 dma_unmap_single(device->card->device, orb->page_table_bus,
1640 sizeof(orb->page_table), DMA_TO_DEVICE);
1642 orb->cmd->result = result;
1643 orb->done(orb->cmd);
1646 static int
1647 sbp2_map_scatterlist(struct sbp2_command_orb *orb, struct fw_device *device,
1648 struct sbp2_logical_unit *lu)
1650 struct scatterlist *sg;
1651 int sg_len, l, i, j, count;
1652 dma_addr_t sg_addr;
1654 sg = scsi_sglist(orb->cmd);
1655 count = dma_map_sg(device->card->device, sg, scsi_sg_count(orb->cmd),
1656 orb->cmd->sc_data_direction);
1657 if (count == 0)
1658 goto fail;
1661 * Handle the special case where there is only one element in
1662 * the scatter list by converting it to an immediate block
1663 * request. This is also a workaround for broken devices such
1664 * as the second generation iPod which doesn't support page
1665 * tables.
1667 if (count == 1 && sg_dma_len(sg) < SBP2_MAX_SG_ELEMENT_LENGTH) {
1668 orb->request.data_descriptor.high = lu->tgt->address_high;
1669 orb->request.data_descriptor.low = sg_dma_address(sg);
1670 orb->request.misc |= COMMAND_ORB_DATA_SIZE(sg_dma_len(sg));
1671 return 0;
1675 * Convert the scatterlist to an sbp2 page table. If any
1676 * scatterlist entries are too big for sbp2, we split them as we
1677 * go. Even if we ask the block I/O layer to not give us sg
1678 * elements larger than 65535 bytes, some IOMMUs may merge sg elements
1679 * during DMA mapping, and Linux currently doesn't prevent this.
1681 for (i = 0, j = 0; i < count; i++, sg = sg_next(sg)) {
1682 sg_len = sg_dma_len(sg);
1683 sg_addr = sg_dma_address(sg);
1684 while (sg_len) {
1685 /* FIXME: This won't get us out of the pinch. */
1686 if (unlikely(j >= ARRAY_SIZE(orb->page_table))) {
1687 fw_error("page table overflow\n");
1688 goto fail_page_table;
1690 l = min(sg_len, SBP2_MAX_SG_ELEMENT_LENGTH);
1691 orb->page_table[j].low = sg_addr;
1692 orb->page_table[j].high = (l << 16);
1693 sg_addr += l;
1694 sg_len -= l;
1695 j++;
1699 fw_memcpy_to_be32(orb->page_table, orb->page_table,
1700 sizeof(orb->page_table[0]) * j);
1701 orb->page_table_bus =
1702 dma_map_single(device->card->device, orb->page_table,
1703 sizeof(orb->page_table), DMA_TO_DEVICE);
1704 if (dma_mapping_error(orb->page_table_bus))
1705 goto fail_page_table;
1708 * The data_descriptor pointer is the one case where we need
1709 * to fill in the node ID part of the address. All other
1710 * pointers assume that the data referenced reside on the
1711 * initiator (i.e. us), but data_descriptor can refer to data
1712 * on other nodes so we need to put our ID in descriptor.high.
1714 orb->request.data_descriptor.high = lu->tgt->address_high;
1715 orb->request.data_descriptor.low = orb->page_table_bus;
1716 orb->request.misc |=
1717 COMMAND_ORB_PAGE_TABLE_PRESENT |
1718 COMMAND_ORB_DATA_SIZE(j);
1720 return 0;
1722 fail_page_table:
1723 dma_unmap_sg(device->card->device, sg, scsi_sg_count(orb->cmd),
1724 orb->cmd->sc_data_direction);
1725 fail:
1726 return -ENOMEM;
1729 /* SCSI stack integration */
1731 static int sbp2_scsi_queuecommand(struct scsi_cmnd *cmd, scsi_done_fn_t done)
1733 struct sbp2_logical_unit *lu = cmd->device->hostdata;
1734 struct fw_device *device = fw_device(lu->tgt->unit->device.parent);
1735 struct sbp2_command_orb *orb;
1736 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1737 unsigned max_payload;
1738 =======
1739 unsigned int max_payload;
1740 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1741 int retval = SCSI_MLQUEUE_HOST_BUSY;
1744 * Bidirectional commands are not yet implemented, and unknown
1745 * transfer direction not handled.
1747 if (cmd->sc_data_direction == DMA_BIDIRECTIONAL) {
1748 fw_error("Can't handle DMA_BIDIRECTIONAL, rejecting command\n");
1749 cmd->result = DID_ERROR << 16;
1750 done(cmd);
1751 return 0;
1754 orb = kzalloc(sizeof(*orb), GFP_ATOMIC);
1755 if (orb == NULL) {
1756 fw_notify("failed to alloc orb\n");
1757 return SCSI_MLQUEUE_HOST_BUSY;
1760 /* Initialize rcode to something not RCODE_COMPLETE. */
1761 orb->base.rcode = -1;
1762 kref_init(&orb->base.kref);
1764 orb->lu = lu;
1765 orb->done = done;
1766 orb->cmd = cmd;
1768 orb->request.next.high = SBP2_ORB_NULL;
1769 orb->request.next.low = 0x0;
1771 * At speed 100 we can do 512 bytes per packet, at speed 200,
1772 * 1024 bytes per packet etc. The SBP-2 max_payload field
1773 * specifies the max payload size as 2 ^ (max_payload + 2), so
1774 * if we set this to max_speed + 7, we get the right value.
1776 max_payload = min(device->max_speed + 7,
1777 device->card->max_receive - 1);
1778 orb->request.misc =
1779 COMMAND_ORB_MAX_PAYLOAD(max_payload) |
1780 COMMAND_ORB_SPEED(device->max_speed) |
1781 COMMAND_ORB_NOTIFY;
1783 if (cmd->sc_data_direction == DMA_FROM_DEVICE)
1784 orb->request.misc |=
1785 COMMAND_ORB_DIRECTION(SBP2_DIRECTION_FROM_MEDIA);
1786 else if (cmd->sc_data_direction == DMA_TO_DEVICE)
1787 orb->request.misc |=
1788 COMMAND_ORB_DIRECTION(SBP2_DIRECTION_TO_MEDIA);
1790 if (scsi_sg_count(cmd) && sbp2_map_scatterlist(orb, device, lu) < 0)
1791 goto out;
1793 fw_memcpy_to_be32(&orb->request, &orb->request, sizeof(orb->request));
1795 memset(orb->request.command_block,
1796 0, sizeof(orb->request.command_block));
1797 memcpy(orb->request.command_block, cmd->cmnd, COMMAND_SIZE(*cmd->cmnd));
1799 orb->base.callback = complete_command_orb;
1800 orb->base.request_bus =
1801 dma_map_single(device->card->device, &orb->request,
1802 sizeof(orb->request), DMA_TO_DEVICE);
1803 if (dma_mapping_error(orb->base.request_bus))
1804 goto out;
1806 sbp2_send_orb(&orb->base, lu, lu->tgt->node_id, lu->generation,
1807 lu->command_block_agent_address + SBP2_ORB_POINTER);
1808 retval = 0;
1809 out:
1810 kref_put(&orb->base.kref, free_orb);
1811 return retval;
1814 static int sbp2_scsi_slave_alloc(struct scsi_device *sdev)
1816 struct sbp2_logical_unit *lu = sdev->hostdata;
1818 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1819 =======
1820 /* (Re-)Adding logical units via the SCSI stack is not supported. */
1821 if (!lu)
1822 return -ENOSYS;
1824 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1825 sdev->allow_restart = 1;
1828 * Update the dma alignment (minimum alignment requirements for
1829 * start and end of DMA transfers) to be a sector
1831 blk_queue_update_dma_alignment(sdev->request_queue, 511);
1833 if (lu->tgt->workarounds & SBP2_WORKAROUND_INQUIRY_36)
1834 sdev->inquiry_len = 36;
1836 return 0;
1839 static int sbp2_scsi_slave_configure(struct scsi_device *sdev)
1841 struct sbp2_logical_unit *lu = sdev->hostdata;
1843 sdev->use_10_for_rw = 1;
1845 if (sdev->type == TYPE_ROM)
1846 sdev->use_10_for_ms = 1;
1848 if (sdev->type == TYPE_DISK &&
1849 lu->tgt->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
1850 sdev->skip_ms_page_8 = 1;
1852 if (lu->tgt->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
1853 sdev->fix_capacity = 1;
1855 if (lu->tgt->workarounds & SBP2_WORKAROUND_128K_MAX_TRANS)
1856 blk_queue_max_sectors(sdev->request_queue, 128 * 1024 / 512);
1858 return 0;
1862 * Called by scsi stack when something has really gone wrong. Usually
1863 * called when a command has timed-out for some reason.
1865 static int sbp2_scsi_abort(struct scsi_cmnd *cmd)
1867 struct sbp2_logical_unit *lu = cmd->device->hostdata;
1869 <<<<<<< HEAD:drivers/firewire/fw-sbp2.c
1870 fw_notify("sbp2_scsi_abort\n");
1871 =======
1872 fw_notify("%s: sbp2_scsi_abort\n", lu->tgt->bus_id);
1873 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-sbp2.c
1874 sbp2_agent_reset(lu);
1875 sbp2_cancel_orbs(lu);
1877 return SUCCESS;
1881 * Format of /sys/bus/scsi/devices/.../ieee1394_id:
1882 * u64 EUI-64 : u24 directory_ID : u16 LUN (all printed in hexadecimal)
1884 * This is the concatenation of target port identifier and logical unit
1885 * identifier as per SAM-2...SAM-4 annex A.
1887 static ssize_t
1888 sbp2_sysfs_ieee1394_id_show(struct device *dev, struct device_attribute *attr,
1889 char *buf)
1891 struct scsi_device *sdev = to_scsi_device(dev);
1892 struct sbp2_logical_unit *lu;
1893 struct fw_device *device;
1895 if (!sdev)
1896 return 0;
1898 lu = sdev->hostdata;
1899 device = fw_device(lu->tgt->unit->device.parent);
1901 return sprintf(buf, "%08x%08x:%06x:%04x\n",
1902 device->config_rom[3], device->config_rom[4],
1903 lu->tgt->directory_id, lu->lun);
1906 static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
1908 static struct device_attribute *sbp2_scsi_sysfs_attrs[] = {
1909 &dev_attr_ieee1394_id,
1910 NULL
1913 static struct scsi_host_template scsi_driver_template = {
1914 .module = THIS_MODULE,
1915 .name = "SBP-2 IEEE-1394",
1916 .proc_name = sbp2_driver_name,
1917 .queuecommand = sbp2_scsi_queuecommand,
1918 .slave_alloc = sbp2_scsi_slave_alloc,
1919 .slave_configure = sbp2_scsi_slave_configure,
1920 .eh_abort_handler = sbp2_scsi_abort,
1921 .this_id = -1,
1922 .sg_tablesize = SG_ALL,
1923 .use_clustering = ENABLE_CLUSTERING,
1924 .cmd_per_lun = 1,
1925 .can_queue = 1,
1926 .sdev_attrs = sbp2_scsi_sysfs_attrs,
1929 MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
1930 MODULE_DESCRIPTION("SCSI over IEEE1394");
1931 MODULE_LICENSE("GPL");
1932 MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
1934 /* Provide a module alias so root-on-sbp2 initrds don't break. */
1935 #ifndef CONFIG_IEEE1394_SBP2_MODULE
1936 MODULE_ALIAS("sbp2");
1937 #endif
1939 static int __init sbp2_init(void)
1941 sbp2_wq = create_singlethread_workqueue(KBUILD_MODNAME);
1942 if (!sbp2_wq)
1943 return -ENOMEM;
1945 return driver_register(&sbp2_driver.driver);
1948 static void __exit sbp2_cleanup(void)
1950 driver_unregister(&sbp2_driver.driver);
1951 destroy_workqueue(sbp2_wq);
1954 module_init(sbp2_init);
1955 module_exit(sbp2_cleanup);