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>
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/mod_devicetable.h>
34 #include <linux/device.h>
35 #include <linux/scatterlist.h>
36 #include <linux/dma-mapping.h>
37 #include <linux/timer.h>
39 #include <scsi/scsi.h>
40 #include <scsi/scsi_cmnd.h>
41 #include <scsi/scsi_dbg.h>
42 #include <scsi/scsi_device.h>
43 #include <scsi/scsi_host.h>
45 #include "fw-transaction.h"
46 #include "fw-topology.h"
47 #include "fw-device.h"
49 /* I don't know why the SCSI stack doesn't define something like this... */
50 typedef void (*scsi_done_fn_t
)(struct scsi_cmnd
*);
52 static const char sbp2_driver_name
[] = "sbp2";
57 struct fw_address_handler address_handler
;
58 struct list_head orb_list
;
59 u64 management_agent_address
;
60 u64 command_block_agent_address
;
65 * We cache these addresses and only update them once we've
66 * logged in or reconnected to the sbp2 device. That way, any
67 * IO to the device will automatically fail and get retried if
68 * it happens in a window where the device is not ready to
69 * handle it (e.g. after a bus reset but before we reconnect).
76 struct delayed_work work
;
77 struct Scsi_Host
*scsi_host
;
80 #define SBP2_MAX_SG_ELEMENT_LENGTH 0xf000
81 #define SBP2_MAX_SECTORS 255 /* Max sectors supported */
82 #define SBP2_ORB_TIMEOUT 2000 /* Timeout in ms */
84 #define SBP2_ORB_NULL 0x80000000
86 #define SBP2_DIRECTION_TO_MEDIA 0x0
87 #define SBP2_DIRECTION_FROM_MEDIA 0x1
89 /* Unit directory keys */
90 #define SBP2_COMMAND_SET_SPECIFIER 0x38
91 #define SBP2_COMMAND_SET 0x39
92 #define SBP2_COMMAND_SET_REVISION 0x3b
93 #define SBP2_FIRMWARE_REVISION 0x3c
95 /* Flags for detected oddities and brokeness */
96 #define SBP2_WORKAROUND_128K_MAX_TRANS 0x1
97 #define SBP2_WORKAROUND_INQUIRY_36 0x2
98 #define SBP2_WORKAROUND_MODE_SENSE_8 0x4
99 #define SBP2_WORKAROUND_FIX_CAPACITY 0x8
100 #define SBP2_WORKAROUND_OVERRIDE 0x100
102 /* Management orb opcodes */
103 #define SBP2_LOGIN_REQUEST 0x0
104 #define SBP2_QUERY_LOGINS_REQUEST 0x1
105 #define SBP2_RECONNECT_REQUEST 0x3
106 #define SBP2_SET_PASSWORD_REQUEST 0x4
107 #define SBP2_LOGOUT_REQUEST 0x7
108 #define SBP2_ABORT_TASK_REQUEST 0xb
109 #define SBP2_ABORT_TASK_SET 0xc
110 #define SBP2_LOGICAL_UNIT_RESET 0xe
111 #define SBP2_TARGET_RESET_REQUEST 0xf
113 /* Offsets for command block agent registers */
114 #define SBP2_AGENT_STATE 0x00
115 #define SBP2_AGENT_RESET 0x04
116 #define SBP2_ORB_POINTER 0x08
117 #define SBP2_DOORBELL 0x10
118 #define SBP2_UNSOLICITED_STATUS_ENABLE 0x14
120 /* Status write response codes */
121 #define SBP2_STATUS_REQUEST_COMPLETE 0x0
122 #define SBP2_STATUS_TRANSPORT_FAILURE 0x1
123 #define SBP2_STATUS_ILLEGAL_REQUEST 0x2
124 #define SBP2_STATUS_VENDOR_DEPENDENT 0x3
126 #define STATUS_GET_ORB_HIGH(v) ((v).status & 0xffff)
127 #define STATUS_GET_SBP_STATUS(v) (((v).status >> 16) & 0xff)
128 #define STATUS_GET_LEN(v) (((v).status >> 24) & 0x07)
129 #define STATUS_GET_DEAD(v) (((v).status >> 27) & 0x01)
130 #define STATUS_GET_RESPONSE(v) (((v).status >> 28) & 0x03)
131 #define STATUS_GET_SOURCE(v) (((v).status >> 30) & 0x03)
132 #define STATUS_GET_ORB_LOW(v) ((v).orb_low)
133 #define STATUS_GET_DATA(v) ((v).data)
141 struct sbp2_pointer
{
147 struct fw_transaction t
;
148 dma_addr_t request_bus
;
150 struct sbp2_pointer pointer
;
151 void (*callback
)(struct sbp2_orb
* orb
, struct sbp2_status
* status
);
152 struct list_head link
;
155 #define MANAGEMENT_ORB_LUN(v) ((v))
156 #define MANAGEMENT_ORB_FUNCTION(v) ((v) << 16)
157 #define MANAGEMENT_ORB_RECONNECT(v) ((v) << 20)
158 #define MANAGEMENT_ORB_EXCLUSIVE ((1) << 28)
159 #define MANAGEMENT_ORB_REQUEST_FORMAT(v) ((v) << 29)
160 #define MANAGEMENT_ORB_NOTIFY ((1) << 31)
162 #define MANAGEMENT_ORB_RESPONSE_LENGTH(v) ((v))
163 #define MANAGEMENT_ORB_PASSWORD_LENGTH(v) ((v) << 16)
165 struct sbp2_management_orb
{
166 struct sbp2_orb base
;
168 struct sbp2_pointer password
;
169 struct sbp2_pointer response
;
172 struct sbp2_pointer status_fifo
;
175 dma_addr_t response_bus
;
176 struct completion done
;
177 struct sbp2_status status
;
180 #define LOGIN_RESPONSE_GET_LOGIN_ID(v) ((v).misc & 0xffff)
181 #define LOGIN_RESPONSE_GET_LENGTH(v) (((v).misc >> 16) & 0xffff)
183 struct sbp2_login_response
{
185 struct sbp2_pointer command_block_agent
;
188 #define COMMAND_ORB_DATA_SIZE(v) ((v))
189 #define COMMAND_ORB_PAGE_SIZE(v) ((v) << 16)
190 #define COMMAND_ORB_PAGE_TABLE_PRESENT ((1) << 19)
191 #define COMMAND_ORB_MAX_PAYLOAD(v) ((v) << 20)
192 #define COMMAND_ORB_SPEED(v) ((v) << 24)
193 #define COMMAND_ORB_DIRECTION(v) ((v) << 27)
194 #define COMMAND_ORB_REQUEST_FORMAT(v) ((v) << 29)
195 #define COMMAND_ORB_NOTIFY ((1) << 31)
197 struct sbp2_command_orb
{
198 struct sbp2_orb base
;
200 struct sbp2_pointer next
;
201 struct sbp2_pointer data_descriptor
;
203 u8 command_block
[12];
205 struct scsi_cmnd
*cmd
;
207 struct fw_unit
*unit
;
209 struct sbp2_pointer page_table
[SG_ALL
];
210 dma_addr_t page_table_bus
;
211 dma_addr_t request_buffer_bus
;
215 * List of devices with known bugs.
217 * The firmware_revision field, masked with 0xffff00, is the best
218 * indicator for the type of bridge chip of a device. It yields a few
219 * false positives but this did not break correctly behaving devices
220 * so far. We use ~0 as a wildcard, since the 24 bit values we get
221 * from the config rom can never match that.
223 static const struct {
224 u32 firmware_revision
;
226 unsigned workarounds
;
227 } sbp2_workarounds_table
[] = {
228 /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
229 .firmware_revision
= 0x002800,
231 .workarounds
= SBP2_WORKAROUND_INQUIRY_36
|
232 SBP2_WORKAROUND_MODE_SENSE_8
,
234 /* Initio bridges, actually only needed for some older ones */ {
235 .firmware_revision
= 0x000200,
237 .workarounds
= SBP2_WORKAROUND_INQUIRY_36
,
239 /* Symbios bridge */ {
240 .firmware_revision
= 0xa0b800,
242 .workarounds
= SBP2_WORKAROUND_128K_MAX_TRANS
,
246 * There are iPods (2nd gen, 3rd gen) with model_id == 0, but
247 * these iPods do not feature the read_capacity bug according
248 * to one report. Read_capacity behaviour as well as model_id
249 * could change due to Apple-supplied firmware updates though.
252 /* iPod 4th generation. */ {
253 .firmware_revision
= 0x0a2700,
255 .workarounds
= SBP2_WORKAROUND_FIX_CAPACITY
,
258 .firmware_revision
= 0x0a2700,
260 .workarounds
= SBP2_WORKAROUND_FIX_CAPACITY
,
263 .firmware_revision
= 0x0a2700,
265 .workarounds
= SBP2_WORKAROUND_FIX_CAPACITY
,
270 sbp2_status_write(struct fw_card
*card
, struct fw_request
*request
,
271 int tcode
, int destination
, int source
,
272 int generation
, int speed
,
273 unsigned long long offset
,
274 void *payload
, size_t length
, void *callback_data
)
276 struct sbp2_device
*sd
= callback_data
;
277 struct sbp2_orb
*orb
;
278 struct sbp2_status status
;
282 if (tcode
!= TCODE_WRITE_BLOCK_REQUEST
||
283 length
== 0 || length
> sizeof status
) {
284 fw_send_response(card
, request
, RCODE_TYPE_ERROR
);
288 header_size
= min(length
, 2 * sizeof(u32
));
289 fw_memcpy_from_be32(&status
, payload
, header_size
);
290 if (length
> header_size
)
291 memcpy(status
.data
, payload
+ 8, length
- header_size
);
292 if (STATUS_GET_SOURCE(status
) == 2 || STATUS_GET_SOURCE(status
) == 3) {
293 fw_notify("non-orb related status write, not handled\n");
294 fw_send_response(card
, request
, RCODE_COMPLETE
);
298 /* Lookup the orb corresponding to this status write. */
299 spin_lock_irqsave(&card
->lock
, flags
);
300 list_for_each_entry(orb
, &sd
->orb_list
, link
) {
301 if (STATUS_GET_ORB_HIGH(status
) == 0 &&
302 STATUS_GET_ORB_LOW(status
) == orb
->request_bus
&&
303 orb
->rcode
== RCODE_COMPLETE
) {
304 list_del(&orb
->link
);
308 spin_unlock_irqrestore(&card
->lock
, flags
);
310 if (&orb
->link
!= &sd
->orb_list
)
311 orb
->callback(orb
, &status
);
313 fw_error("status write for unknown orb\n");
315 fw_send_response(card
, request
, RCODE_COMPLETE
);
319 complete_transaction(struct fw_card
*card
, int rcode
,
320 void *payload
, size_t length
, void *data
)
322 struct sbp2_orb
*orb
= data
;
326 if (rcode
!= RCODE_COMPLETE
) {
327 spin_lock_irqsave(&card
->lock
, flags
);
328 list_del(&orb
->link
);
329 spin_unlock_irqrestore(&card
->lock
, flags
);
330 orb
->callback(orb
, NULL
);
335 sbp2_send_orb(struct sbp2_orb
*orb
, struct fw_unit
*unit
,
336 int node_id
, int generation
, u64 offset
)
338 struct fw_device
*device
= fw_device(unit
->device
.parent
);
339 struct sbp2_device
*sd
= unit
->device
.driver_data
;
342 orb
->pointer
.high
= 0;
343 orb
->pointer
.low
= orb
->request_bus
;
344 fw_memcpy_to_be32(&orb
->pointer
, &orb
->pointer
, sizeof orb
->pointer
);
346 spin_lock_irqsave(&device
->card
->lock
, flags
);
347 list_add_tail(&orb
->link
, &sd
->orb_list
);
348 spin_unlock_irqrestore(&device
->card
->lock
, flags
);
350 fw_send_request(device
->card
, &orb
->t
, TCODE_WRITE_BLOCK_REQUEST
,
352 device
->node
->max_speed
, offset
,
353 &orb
->pointer
, sizeof orb
->pointer
,
354 complete_transaction
, orb
);
357 static int sbp2_cancel_orbs(struct fw_unit
*unit
)
359 struct fw_device
*device
= fw_device(unit
->device
.parent
);
360 struct sbp2_device
*sd
= unit
->device
.driver_data
;
361 struct sbp2_orb
*orb
, *next
;
362 struct list_head list
;
364 int retval
= -ENOENT
;
366 INIT_LIST_HEAD(&list
);
367 spin_lock_irqsave(&device
->card
->lock
, flags
);
368 list_splice_init(&sd
->orb_list
, &list
);
369 spin_unlock_irqrestore(&device
->card
->lock
, flags
);
371 list_for_each_entry_safe(orb
, next
, &list
, link
) {
373 if (fw_cancel_transaction(device
->card
, &orb
->t
) == 0)
376 orb
->rcode
= RCODE_CANCELLED
;
377 orb
->callback(orb
, NULL
);
384 complete_management_orb(struct sbp2_orb
*base_orb
, struct sbp2_status
*status
)
386 struct sbp2_management_orb
*orb
=
387 (struct sbp2_management_orb
*)base_orb
;
390 memcpy(&orb
->status
, status
, sizeof *status
);
391 complete(&orb
->done
);
395 sbp2_send_management_orb(struct fw_unit
*unit
, int node_id
, int generation
,
396 int function
, int lun
, void *response
)
398 struct fw_device
*device
= fw_device(unit
->device
.parent
);
399 struct sbp2_device
*sd
= unit
->device
.driver_data
;
400 struct sbp2_management_orb
*orb
;
401 int retval
= -ENOMEM
;
403 orb
= kzalloc(sizeof *orb
, GFP_ATOMIC
);
408 * The sbp2 device is going to send a block read request to
409 * read out the request from host memory, so map it for dma.
411 orb
->base
.request_bus
=
412 dma_map_single(device
->card
->device
, &orb
->request
,
413 sizeof orb
->request
, DMA_TO_DEVICE
);
414 if (dma_mapping_error(orb
->base
.request_bus
))
418 dma_map_single(device
->card
->device
, &orb
->response
,
419 sizeof orb
->response
, DMA_FROM_DEVICE
);
420 if (dma_mapping_error(orb
->response_bus
))
423 orb
->request
.response
.high
= 0;
424 orb
->request
.response
.low
= orb
->response_bus
;
427 MANAGEMENT_ORB_NOTIFY
|
428 MANAGEMENT_ORB_FUNCTION(function
) |
429 MANAGEMENT_ORB_LUN(lun
);
430 orb
->request
.length
=
431 MANAGEMENT_ORB_RESPONSE_LENGTH(sizeof orb
->response
);
433 orb
->request
.status_fifo
.high
= sd
->address_handler
.offset
>> 32;
434 orb
->request
.status_fifo
.low
= sd
->address_handler
.offset
;
437 * FIXME: Yeah, ok this isn't elegant, we hardwire exclusive
438 * login and 1 second reconnect time. The reconnect setting
439 * is probably fine, but the exclusive login should be an option.
441 if (function
== SBP2_LOGIN_REQUEST
) {
443 MANAGEMENT_ORB_EXCLUSIVE
|
444 MANAGEMENT_ORB_RECONNECT(0);
447 fw_memcpy_to_be32(&orb
->request
, &orb
->request
, sizeof orb
->request
);
449 init_completion(&orb
->done
);
450 orb
->base
.callback
= complete_management_orb
;
452 sbp2_send_orb(&orb
->base
, unit
,
453 node_id
, generation
, sd
->management_agent_address
);
455 wait_for_completion_timeout(&orb
->done
,
456 msecs_to_jiffies(SBP2_ORB_TIMEOUT
));
459 if (sbp2_cancel_orbs(unit
) == 0) {
460 fw_error("orb reply timed out, rcode=0x%02x\n",
465 if (orb
->base
.rcode
!= RCODE_COMPLETE
) {
466 fw_error("management write failed, rcode 0x%02x\n",
471 if (STATUS_GET_RESPONSE(orb
->status
) != 0 ||
472 STATUS_GET_SBP_STATUS(orb
->status
) != 0) {
473 fw_error("error status: %d:%d\n",
474 STATUS_GET_RESPONSE(orb
->status
),
475 STATUS_GET_SBP_STATUS(orb
->status
));
481 dma_unmap_single(device
->card
->device
, orb
->base
.request_bus
,
482 sizeof orb
->request
, DMA_TO_DEVICE
);
483 dma_unmap_single(device
->card
->device
, orb
->response_bus
,
484 sizeof orb
->response
, DMA_FROM_DEVICE
);
487 fw_memcpy_from_be32(response
,
488 orb
->response
, sizeof orb
->response
);
495 complete_agent_reset_write(struct fw_card
*card
, int rcode
,
496 void *payload
, size_t length
, void *data
)
498 struct fw_transaction
*t
= data
;
503 static int sbp2_agent_reset(struct fw_unit
*unit
)
505 struct fw_device
*device
= fw_device(unit
->device
.parent
);
506 struct sbp2_device
*sd
= unit
->device
.driver_data
;
507 struct fw_transaction
*t
;
510 t
= kzalloc(sizeof *t
, GFP_ATOMIC
);
514 fw_send_request(device
->card
, t
, TCODE_WRITE_QUADLET_REQUEST
,
515 sd
->node_id
, sd
->generation
, SCODE_400
,
516 sd
->command_block_agent_address
+ SBP2_AGENT_RESET
,
517 &zero
, sizeof zero
, complete_agent_reset_write
, t
);
522 static int add_scsi_devices(struct fw_unit
*unit
);
523 static void remove_scsi_devices(struct fw_unit
*unit
);
524 static void sbp2_reconnect(struct work_struct
*work
);
527 release_sbp2_device(struct kref
*kref
)
529 struct sbp2_device
*sd
= container_of(kref
, struct sbp2_device
, kref
);
531 sbp2_send_management_orb(sd
->unit
, sd
->node_id
, sd
->generation
,
532 SBP2_LOGOUT_REQUEST
, sd
->login_id
, NULL
);
534 remove_scsi_devices(sd
->unit
);
536 fw_core_remove_address_handler(&sd
->address_handler
);
537 fw_notify("removed sbp2 unit %s\n", sd
->unit
->device
.bus_id
);
538 put_device(&sd
->unit
->device
);
542 static void sbp2_login(struct work_struct
*work
)
544 struct sbp2_device
*sd
=
545 container_of(work
, struct sbp2_device
, work
.work
);
546 struct fw_unit
*unit
= sd
->unit
;
547 struct fw_device
*device
= fw_device(unit
->device
.parent
);
548 struct sbp2_login_response response
;
549 int generation
, node_id
, local_node_id
, lun
, retval
;
551 /* FIXME: Make this work for multi-lun devices. */
554 generation
= device
->card
->generation
;
555 node_id
= device
->node
->node_id
;
556 local_node_id
= device
->card
->local_node
->node_id
;
558 if (sbp2_send_management_orb(unit
, node_id
, generation
,
559 SBP2_LOGIN_REQUEST
, lun
, &response
) < 0) {
560 if (sd
->retries
++ < 5) {
561 schedule_delayed_work(&sd
->work
, DIV_ROUND_UP(HZ
, 5));
563 fw_error("failed to login to %s\n",
564 unit
->device
.bus_id
);
565 remove_scsi_devices(unit
);
566 kref_put(&sd
->kref
, release_sbp2_device
);
571 sd
->generation
= generation
;
572 sd
->node_id
= node_id
;
573 sd
->address_high
= local_node_id
<< 16;
575 /* Get command block agent offset and login id. */
576 sd
->command_block_agent_address
=
577 ((u64
) (response
.command_block_agent
.high
& 0xffff) << 32) |
578 response
.command_block_agent
.low
;
579 sd
->login_id
= LOGIN_RESPONSE_GET_LOGIN_ID(response
);
581 fw_notify("logged in to sbp2 unit %s (%d retries)\n",
582 unit
->device
.bus_id
, sd
->retries
);
583 fw_notify(" - management_agent_address: 0x%012llx\n",
584 (unsigned long long) sd
->management_agent_address
);
585 fw_notify(" - command_block_agent_address: 0x%012llx\n",
586 (unsigned long long) sd
->command_block_agent_address
);
587 fw_notify(" - status write address: 0x%012llx\n",
588 (unsigned long long) sd
->address_handler
.offset
);
591 /* FIXME: The linux1394 sbp2 does this last step. */
592 sbp2_set_busy_timeout(scsi_id
);
595 PREPARE_DELAYED_WORK(&sd
->work
, sbp2_reconnect
);
596 sbp2_agent_reset(unit
);
598 retval
= add_scsi_devices(unit
);
600 sbp2_send_management_orb(unit
, sd
->node_id
, sd
->generation
,
601 SBP2_LOGOUT_REQUEST
, sd
->login_id
,
604 * Set this back to sbp2_login so we fall back and
605 * retry login on bus reset.
607 PREPARE_DELAYED_WORK(&sd
->work
, sbp2_login
);
609 kref_put(&sd
->kref
, release_sbp2_device
);
612 static int sbp2_probe(struct device
*dev
)
614 struct fw_unit
*unit
= fw_unit(dev
);
615 struct fw_device
*device
= fw_device(unit
->device
.parent
);
616 struct sbp2_device
*sd
;
617 struct fw_csr_iterator ci
;
619 u32 model
, firmware_revision
;
621 sd
= kzalloc(sizeof *sd
, GFP_KERNEL
);
625 unit
->device
.driver_data
= sd
;
627 INIT_LIST_HEAD(&sd
->orb_list
);
628 kref_init(&sd
->kref
);
630 sd
->address_handler
.length
= 0x100;
631 sd
->address_handler
.address_callback
= sbp2_status_write
;
632 sd
->address_handler
.callback_data
= sd
;
634 if (fw_core_add_address_handler(&sd
->address_handler
,
635 &fw_high_memory_region
) < 0) {
640 if (fw_device_enable_phys_dma(device
) < 0) {
641 fw_core_remove_address_handler(&sd
->address_handler
);
647 * Scan unit directory to get management agent address,
648 * firmware revison and model. Initialize firmware_revision
649 * and model to values that wont match anything in our table.
651 firmware_revision
= 0xff000000;
653 fw_csr_iterator_init(&ci
, unit
->directory
);
654 while (fw_csr_iterator_next(&ci
, &key
, &value
)) {
656 case CSR_DEPENDENT_INFO
| CSR_OFFSET
:
657 sd
->management_agent_address
=
658 0xfffff0000000ULL
+ 4 * value
;
660 case SBP2_FIRMWARE_REVISION
:
661 firmware_revision
= value
;
669 for (i
= 0; i
< ARRAY_SIZE(sbp2_workarounds_table
); i
++) {
670 if (sbp2_workarounds_table
[i
].firmware_revision
!=
671 (firmware_revision
& 0xffffff00))
673 if (sbp2_workarounds_table
[i
].model
!= model
&&
674 sbp2_workarounds_table
[i
].model
!= ~0)
676 sd
->workarounds
|= sbp2_workarounds_table
[i
].workarounds
;
681 fw_notify("Workarounds for node %s: 0x%x "
682 "(firmware_revision 0x%06x, model_id 0x%06x)\n",
684 sd
->workarounds
, firmware_revision
, model
);
686 get_device(&unit
->device
);
689 * We schedule work to do the login so we can easily
690 * reschedule retries. Always get the ref before scheduling
693 INIT_DELAYED_WORK(&sd
->work
, sbp2_login
);
694 if (schedule_delayed_work(&sd
->work
, 0))
700 static int sbp2_remove(struct device
*dev
)
702 struct fw_unit
*unit
= fw_unit(dev
);
703 struct sbp2_device
*sd
= unit
->device
.driver_data
;
705 kref_put(&sd
->kref
, release_sbp2_device
);
710 static void sbp2_reconnect(struct work_struct
*work
)
712 struct sbp2_device
*sd
=
713 container_of(work
, struct sbp2_device
, work
.work
);
714 struct fw_unit
*unit
= sd
->unit
;
715 struct fw_device
*device
= fw_device(unit
->device
.parent
);
716 int generation
, node_id
, local_node_id
;
718 generation
= device
->card
->generation
;
719 node_id
= device
->node
->node_id
;
720 local_node_id
= device
->card
->local_node
->node_id
;
722 if (sbp2_send_management_orb(unit
, node_id
, generation
,
723 SBP2_RECONNECT_REQUEST
,
724 sd
->login_id
, NULL
) < 0) {
725 if (sd
->retries
++ >= 5) {
726 fw_error("failed to reconnect to %s\n",
727 unit
->device
.bus_id
);
728 /* Fall back and try to log in again. */
730 PREPARE_DELAYED_WORK(&sd
->work
, sbp2_login
);
732 schedule_delayed_work(&sd
->work
, DIV_ROUND_UP(HZ
, 5));
736 sd
->generation
= generation
;
737 sd
->node_id
= node_id
;
738 sd
->address_high
= local_node_id
<< 16;
740 fw_notify("reconnected to unit %s (%d retries)\n",
741 unit
->device
.bus_id
, sd
->retries
);
742 sbp2_agent_reset(unit
);
743 sbp2_cancel_orbs(unit
);
744 kref_put(&sd
->kref
, release_sbp2_device
);
747 static void sbp2_update(struct fw_unit
*unit
)
749 struct fw_device
*device
= fw_device(unit
->device
.parent
);
750 struct sbp2_device
*sd
= unit
->device
.driver_data
;
753 fw_device_enable_phys_dma(device
);
754 if (schedule_delayed_work(&sd
->work
, 0))
758 #define SBP2_UNIT_SPEC_ID_ENTRY 0x0000609e
759 #define SBP2_SW_VERSION_ENTRY 0x00010483
761 static const struct fw_device_id sbp2_id_table
[] = {
763 .match_flags
= FW_MATCH_SPECIFIER_ID
| FW_MATCH_VERSION
,
764 .specifier_id
= SBP2_UNIT_SPEC_ID_ENTRY
,
765 .version
= SBP2_SW_VERSION_ENTRY
,
770 static struct fw_driver sbp2_driver
= {
772 .owner
= THIS_MODULE
,
773 .name
= sbp2_driver_name
,
776 .remove
= sbp2_remove
,
778 .update
= sbp2_update
,
779 .id_table
= sbp2_id_table
,
783 sbp2_status_to_sense_data(u8
*sbp2_status
, u8
*sense_data
)
787 sense_data
[0] = 0x70;
789 sense_data
[2] = sbp2_status
[1];
790 sense_data
[3] = sbp2_status
[4];
791 sense_data
[4] = sbp2_status
[5];
792 sense_data
[5] = sbp2_status
[6];
793 sense_data
[6] = sbp2_status
[7];
795 sense_data
[8] = sbp2_status
[8];
796 sense_data
[9] = sbp2_status
[9];
797 sense_data
[10] = sbp2_status
[10];
798 sense_data
[11] = sbp2_status
[11];
799 sense_data
[12] = sbp2_status
[2];
800 sense_data
[13] = sbp2_status
[3];
801 sense_data
[14] = sbp2_status
[12];
802 sense_data
[15] = sbp2_status
[13];
804 sam_status
= sbp2_status
[0] & 0x3f;
806 switch (sam_status
) {
808 case SAM_STAT_CHECK_CONDITION
:
809 case SAM_STAT_CONDITION_MET
:
811 case SAM_STAT_RESERVATION_CONFLICT
:
812 case SAM_STAT_COMMAND_TERMINATED
:
813 return DID_OK
<< 16 | sam_status
;
816 return DID_ERROR
<< 16;
821 complete_command_orb(struct sbp2_orb
*base_orb
, struct sbp2_status
*status
)
823 struct sbp2_command_orb
*orb
= (struct sbp2_command_orb
*)base_orb
;
824 struct fw_unit
*unit
= orb
->unit
;
825 struct fw_device
*device
= fw_device(unit
->device
.parent
);
826 struct scatterlist
*sg
;
829 if (status
!= NULL
) {
830 if (STATUS_GET_DEAD(*status
))
831 sbp2_agent_reset(unit
);
833 switch (STATUS_GET_RESPONSE(*status
)) {
834 case SBP2_STATUS_REQUEST_COMPLETE
:
835 result
= DID_OK
<< 16;
837 case SBP2_STATUS_TRANSPORT_FAILURE
:
838 result
= DID_BUS_BUSY
<< 16;
840 case SBP2_STATUS_ILLEGAL_REQUEST
:
841 case SBP2_STATUS_VENDOR_DEPENDENT
:
843 result
= DID_ERROR
<< 16;
847 if (result
== DID_OK
<< 16 && STATUS_GET_LEN(*status
) > 1)
848 result
= sbp2_status_to_sense_data(STATUS_GET_DATA(*status
),
849 orb
->cmd
->sense_buffer
);
852 * If the orb completes with status == NULL, something
853 * went wrong, typically a bus reset happened mid-orb
854 * or when sending the write (less likely).
856 result
= DID_BUS_BUSY
<< 16;
859 dma_unmap_single(device
->card
->device
, orb
->base
.request_bus
,
860 sizeof orb
->request
, DMA_TO_DEVICE
);
862 if (orb
->cmd
->use_sg
> 0) {
863 sg
= (struct scatterlist
*)orb
->cmd
->request_buffer
;
864 dma_unmap_sg(device
->card
->device
, sg
, orb
->cmd
->use_sg
,
865 orb
->cmd
->sc_data_direction
);
868 if (orb
->page_table_bus
!= 0)
869 dma_unmap_single(device
->card
->device
, orb
->page_table_bus
,
870 sizeof orb
->page_table_bus
, DMA_TO_DEVICE
);
872 if (orb
->request_buffer_bus
!= 0)
873 dma_unmap_single(device
->card
->device
, orb
->request_buffer_bus
,
874 sizeof orb
->request_buffer_bus
,
877 orb
->cmd
->result
= result
;
882 static void sbp2_command_orb_map_scatterlist(struct sbp2_command_orb
*orb
)
884 struct fw_unit
*unit
=
885 (struct fw_unit
*)orb
->cmd
->device
->host
->hostdata
[0];
886 struct fw_device
*device
= fw_device(unit
->device
.parent
);
887 struct sbp2_device
*sd
= unit
->device
.driver_data
;
888 struct scatterlist
*sg
;
889 int sg_len
, l
, i
, j
, count
;
893 sg
= (struct scatterlist
*)orb
->cmd
->request_buffer
;
894 count
= dma_map_sg(device
->card
->device
, sg
, orb
->cmd
->use_sg
,
895 orb
->cmd
->sc_data_direction
);
898 * Handle the special case where there is only one element in
899 * the scatter list by converting it to an immediate block
900 * request. This is also a workaround for broken devices such
901 * as the second generation iPod which doesn't support page
904 if (count
== 1 && sg_dma_len(sg
) < SBP2_MAX_SG_ELEMENT_LENGTH
) {
905 orb
->request
.data_descriptor
.high
= sd
->address_high
;
906 orb
->request
.data_descriptor
.low
= sg_dma_address(sg
);
908 COMMAND_ORB_DATA_SIZE(sg_dma_len(sg
));
913 * Convert the scatterlist to an sbp2 page table. If any
914 * scatterlist entries are too big for sbp2 we split the as we go.
916 for (i
= 0, j
= 0; i
< count
; i
++) {
917 sg_len
= sg_dma_len(sg
+ i
);
918 sg_addr
= sg_dma_address(sg
+ i
);
920 l
= min(sg_len
, SBP2_MAX_SG_ELEMENT_LENGTH
);
921 orb
->page_table
[j
].low
= sg_addr
;
922 orb
->page_table
[j
].high
= (l
<< 16);
929 size
= sizeof orb
->page_table
[0] * j
;
932 * The data_descriptor pointer is the one case where we need
933 * to fill in the node ID part of the address. All other
934 * pointers assume that the data referenced reside on the
935 * initiator (i.e. us), but data_descriptor can refer to data
936 * on other nodes so we need to put our ID in descriptor.high.
939 orb
->page_table_bus
=
940 dma_map_single(device
->card
->device
, orb
->page_table
,
941 size
, DMA_TO_DEVICE
);
942 orb
->request
.data_descriptor
.high
= sd
->address_high
;
943 orb
->request
.data_descriptor
.low
= orb
->page_table_bus
;
945 COMMAND_ORB_PAGE_TABLE_PRESENT
|
946 COMMAND_ORB_DATA_SIZE(j
);
948 fw_memcpy_to_be32(orb
->page_table
, orb
->page_table
, size
);
951 static void sbp2_command_orb_map_buffer(struct sbp2_command_orb
*orb
)
953 struct fw_unit
*unit
=
954 (struct fw_unit
*)orb
->cmd
->device
->host
->hostdata
[0];
955 struct fw_device
*device
= fw_device(unit
->device
.parent
);
956 struct sbp2_device
*sd
= unit
->device
.driver_data
;
959 * As for map_scatterlist, we need to fill in the high bits of
960 * the data_descriptor pointer.
963 orb
->request_buffer_bus
=
964 dma_map_single(device
->card
->device
,
965 orb
->cmd
->request_buffer
,
966 orb
->cmd
->request_bufflen
,
967 orb
->cmd
->sc_data_direction
);
968 orb
->request
.data_descriptor
.high
= sd
->address_high
;
969 orb
->request
.data_descriptor
.low
= orb
->request_buffer_bus
;
971 COMMAND_ORB_DATA_SIZE(orb
->cmd
->request_bufflen
);
974 /* SCSI stack integration */
976 static int sbp2_scsi_queuecommand(struct scsi_cmnd
*cmd
, scsi_done_fn_t done
)
978 struct fw_unit
*unit
= (struct fw_unit
*)cmd
->device
->host
->hostdata
[0];
979 struct fw_device
*device
= fw_device(unit
->device
.parent
);
980 struct sbp2_device
*sd
= unit
->device
.driver_data
;
981 struct sbp2_command_orb
*orb
;
984 * Bidirectional commands are not yet implemented, and unknown
985 * transfer direction not handled.
987 if (cmd
->sc_data_direction
== DMA_BIDIRECTIONAL
) {
988 fw_error("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
992 orb
= kzalloc(sizeof *orb
, GFP_ATOMIC
);
994 fw_notify("failed to alloc orb\n");
998 /* Initialize rcode to something not RCODE_COMPLETE. */
999 orb
->base
.rcode
= -1;
1000 orb
->base
.request_bus
=
1001 dma_map_single(device
->card
->device
, &orb
->request
,
1002 sizeof orb
->request
, DMA_TO_DEVICE
);
1003 if (dma_mapping_error(orb
->base
.request_bus
))
1010 orb
->request
.next
.high
= SBP2_ORB_NULL
;
1011 orb
->request
.next
.low
= 0x0;
1013 * At speed 100 we can do 512 bytes per packet, at speed 200,
1014 * 1024 bytes per packet etc. The SBP-2 max_payload field
1015 * specifies the max payload size as 2 ^ (max_payload + 2), so
1016 * if we set this to max_speed + 7, we get the right value.
1019 COMMAND_ORB_MAX_PAYLOAD(device
->node
->max_speed
+ 7) |
1020 COMMAND_ORB_SPEED(device
->node
->max_speed
) |
1023 if (cmd
->sc_data_direction
== DMA_FROM_DEVICE
)
1024 orb
->request
.misc
|=
1025 COMMAND_ORB_DIRECTION(SBP2_DIRECTION_FROM_MEDIA
);
1026 else if (cmd
->sc_data_direction
== DMA_TO_DEVICE
)
1027 orb
->request
.misc
|=
1028 COMMAND_ORB_DIRECTION(SBP2_DIRECTION_TO_MEDIA
);
1031 sbp2_command_orb_map_scatterlist(orb
);
1032 } else if (cmd
->request_bufflen
> SBP2_MAX_SG_ELEMENT_LENGTH
) {
1034 * FIXME: Need to split this into a sg list... but
1035 * could we get the scsi or blk layer to do that by
1036 * reporting our max supported block size?
1038 fw_error("command > 64k\n");
1040 } else if (cmd
->request_bufflen
> 0) {
1041 sbp2_command_orb_map_buffer(orb
);
1044 fw_memcpy_to_be32(&orb
->request
, &orb
->request
, sizeof orb
->request
);
1046 memset(orb
->request
.command_block
,
1047 0, sizeof orb
->request
.command_block
);
1048 memcpy(orb
->request
.command_block
, cmd
->cmnd
, COMMAND_SIZE(*cmd
->cmnd
));
1050 orb
->base
.callback
= complete_command_orb
;
1052 sbp2_send_orb(&orb
->base
, unit
, sd
->node_id
, sd
->generation
,
1053 sd
->command_block_agent_address
+ SBP2_ORB_POINTER
);
1058 dma_unmap_single(device
->card
->device
, orb
->base
.request_bus
,
1059 sizeof orb
->request
, DMA_TO_DEVICE
);
1063 cmd
->result
= DID_ERROR
<< 16;
1068 static int sbp2_scsi_slave_alloc(struct scsi_device
*sdev
)
1070 struct fw_unit
*unit
= (struct fw_unit
*)sdev
->host
->hostdata
[0];
1071 struct sbp2_device
*sd
= unit
->device
.driver_data
;
1073 sdev
->allow_restart
= 1;
1075 if (sd
->workarounds
& SBP2_WORKAROUND_INQUIRY_36
)
1076 sdev
->inquiry_len
= 36;
1080 static int sbp2_scsi_slave_configure(struct scsi_device
*sdev
)
1082 struct fw_unit
*unit
= (struct fw_unit
*)sdev
->host
->hostdata
[0];
1083 struct sbp2_device
*sd
= unit
->device
.driver_data
;
1085 sdev
->use_10_for_rw
= 1;
1087 if (sdev
->type
== TYPE_ROM
)
1088 sdev
->use_10_for_ms
= 1;
1089 if (sdev
->type
== TYPE_DISK
&&
1090 sd
->workarounds
& SBP2_WORKAROUND_MODE_SENSE_8
)
1091 sdev
->skip_ms_page_8
= 1;
1092 if (sd
->workarounds
& SBP2_WORKAROUND_FIX_CAPACITY
) {
1093 fw_notify("setting fix_capacity for %s\n", unit
->device
.bus_id
);
1094 sdev
->fix_capacity
= 1;
1101 * Called by scsi stack when something has really gone wrong. Usually
1102 * called when a command has timed-out for some reason.
1104 static int sbp2_scsi_abort(struct scsi_cmnd
*cmd
)
1106 struct fw_unit
*unit
= (struct fw_unit
*)cmd
->device
->host
->hostdata
[0];
1108 fw_notify("sbp2_scsi_abort\n");
1109 sbp2_agent_reset(unit
);
1110 sbp2_cancel_orbs(unit
);
1115 static struct scsi_host_template scsi_driver_template
= {
1116 .module
= THIS_MODULE
,
1117 .name
= "SBP-2 IEEE-1394",
1118 .proc_name
= (char *)sbp2_driver_name
,
1119 .queuecommand
= sbp2_scsi_queuecommand
,
1120 .slave_alloc
= sbp2_scsi_slave_alloc
,
1121 .slave_configure
= sbp2_scsi_slave_configure
,
1122 .eh_abort_handler
= sbp2_scsi_abort
,
1124 .sg_tablesize
= SG_ALL
,
1125 .use_clustering
= ENABLE_CLUSTERING
,
1130 static int add_scsi_devices(struct fw_unit
*unit
)
1132 struct sbp2_device
*sd
= unit
->device
.driver_data
;
1135 if (sd
->scsi_host
!= NULL
)
1138 sd
->scsi_host
= scsi_host_alloc(&scsi_driver_template
,
1139 sizeof(unsigned long));
1140 if (sd
->scsi_host
== NULL
) {
1141 fw_error("failed to register scsi host\n");
1145 sd
->scsi_host
->hostdata
[0] = (unsigned long)unit
;
1146 retval
= scsi_add_host(sd
->scsi_host
, &unit
->device
);
1148 fw_error("failed to add scsi host\n");
1149 scsi_host_put(sd
->scsi_host
);
1150 sd
->scsi_host
= NULL
;
1154 /* FIXME: Loop over luns here. */
1156 retval
= scsi_add_device(sd
->scsi_host
, 0, 0, lun
);
1158 fw_error("failed to add scsi device\n");
1159 scsi_remove_host(sd
->scsi_host
);
1160 scsi_host_put(sd
->scsi_host
);
1161 sd
->scsi_host
= NULL
;
1168 static void remove_scsi_devices(struct fw_unit
*unit
)
1170 struct sbp2_device
*sd
= unit
->device
.driver_data
;
1172 if (sd
->scsi_host
!= NULL
) {
1173 scsi_remove_host(sd
->scsi_host
);
1174 scsi_host_put(sd
->scsi_host
);
1176 sd
->scsi_host
= NULL
;
1179 MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
1180 MODULE_DESCRIPTION("SCSI over IEEE1394");
1181 MODULE_LICENSE("GPL");
1182 MODULE_DEVICE_TABLE(ieee1394
, sbp2_id_table
);
1184 /* Provide a module alias so root-on-sbp2 initrds don't break. */
1185 #ifndef CONFIG_IEEE1394_SBP2_MODULE
1186 MODULE_ALIAS("sbp2");
1189 static int __init
sbp2_init(void)
1191 return driver_register(&sbp2_driver
.driver
);
1194 static void __exit
sbp2_cleanup(void)
1196 driver_unregister(&sbp2_driver
.driver
);
1199 module_init(sbp2_init
);
1200 module_exit(sbp2_cleanup
);