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/moduleparam.h>
34 #include <linux/mod_devicetable.h>
35 #include <linux/device.h>
36 #include <linux/scatterlist.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/blkdev.h>
39 #include <linux/string.h>
40 #include <linux/timer.h>
42 #include <scsi/scsi.h>
43 #include <scsi/scsi_cmnd.h>
44 #include <scsi/scsi_dbg.h>
45 #include <scsi/scsi_device.h>
46 #include <scsi/scsi_host.h>
48 #include "fw-transaction.h"
49 #include "fw-topology.h"
50 #include "fw-device.h"
53 * So far only bridges from Oxford Semiconductor are known to support
54 * concurrent logins. Depending on firmware, four or two concurrent logins
55 * are possible on OXFW911 and newer Oxsemi bridges.
57 * Concurrent logins are useful together with cluster filesystems.
59 static int sbp2_param_exclusive_login = 1;
60 module_param_named(exclusive_login, sbp2_param_exclusive_login, bool, 0644);
61 MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device "
62 "(default = Y, use N for concurrent initiators)");
64 /* I don't know why the SCSI stack doesn't define something like this... */
65 typedef void (*scsi_done_fn_t)(struct scsi_cmnd *);
67 static const char sbp2_driver_name[] = "sbp2";
72 struct fw_address_handler address_handler;
73 struct list_head orb_list;
74 u64 management_agent_address;
75 u64 command_block_agent_address;
80 * We cache these addresses and only update them once we've
81 * logged in or reconnected to the sbp2 device. That way, any
82 * IO to the device will automatically fail and get retried if
83 * it happens in a window where the device is not ready to
84 * handle it (e.g. after a bus reset but before we reconnect).
91 struct delayed_work work;
94 #define SBP2_MAX_SG_ELEMENT_LENGTH 0xf000
95 #define SBP2_MAX_SECTORS 255 /* Max sectors supported */
96 #define SBP2_ORB_TIMEOUT 2000 /* Timeout in ms */
98 #define SBP2_ORB_NULL 0x80000000
100 #define SBP2_DIRECTION_TO_MEDIA 0x0
101 #define SBP2_DIRECTION_FROM_MEDIA 0x1
103 /* Unit directory keys */
104 #define SBP2_COMMAND_SET_SPECIFIER 0x38
105 #define SBP2_COMMAND_SET 0x39
106 #define SBP2_COMMAND_SET_REVISION 0x3b
107 #define SBP2_FIRMWARE_REVISION 0x3c
109 /* Flags for detected oddities and brokeness */
110 #define SBP2_WORKAROUND_128K_MAX_TRANS 0x1
111 #define SBP2_WORKAROUND_INQUIRY_36 0x2
112 #define SBP2_WORKAROUND_MODE_SENSE_8 0x4
113 #define SBP2_WORKAROUND_FIX_CAPACITY 0x8
114 #define SBP2_WORKAROUND_OVERRIDE 0x100
116 /* Management orb opcodes */
117 #define SBP2_LOGIN_REQUEST 0x0
118 #define SBP2_QUERY_LOGINS_REQUEST 0x1
119 #define SBP2_RECONNECT_REQUEST 0x3
120 #define SBP2_SET_PASSWORD_REQUEST 0x4
121 #define SBP2_LOGOUT_REQUEST 0x7
122 #define SBP2_ABORT_TASK_REQUEST 0xb
123 #define SBP2_ABORT_TASK_SET 0xc
124 #define SBP2_LOGICAL_UNIT_RESET 0xe
125 #define SBP2_TARGET_RESET_REQUEST 0xf
127 /* Offsets for command block agent registers */
128 #define SBP2_AGENT_STATE 0x00
129 #define SBP2_AGENT_RESET 0x04
130 #define SBP2_ORB_POINTER 0x08
131 #define SBP2_DOORBELL 0x10
132 #define SBP2_UNSOLICITED_STATUS_ENABLE 0x14
134 /* Status write response codes */
135 #define SBP2_STATUS_REQUEST_COMPLETE 0x0
136 #define SBP2_STATUS_TRANSPORT_FAILURE 0x1
137 #define SBP2_STATUS_ILLEGAL_REQUEST 0x2
138 #define SBP2_STATUS_VENDOR_DEPENDENT 0x3
140 #define STATUS_GET_ORB_HIGH(v) ((v).status & 0xffff)
141 #define STATUS_GET_SBP_STATUS(v) (((v).status >> 16) & 0xff)
142 #define STATUS_GET_LEN(v) (((v).status >> 24) & 0x07)
143 #define STATUS_GET_DEAD(v) (((v).status >> 27) & 0x01)
144 #define STATUS_GET_RESPONSE(v) (((v).status >> 28) & 0x03)
145 #define STATUS_GET_SOURCE(v) (((v).status >> 30) & 0x03)
146 #define STATUS_GET_ORB_LOW(v) ((v).orb_low)
147 #define STATUS_GET_DATA(v) ((v).data)
155 struct sbp2_pointer {
161 struct fw_transaction t;
163 dma_addr_t request_bus;
165 struct sbp2_pointer pointer;
166 void (*callback)(struct sbp2_orb * orb, struct sbp2_status * status);
167 struct list_head link;
170 #define MANAGEMENT_ORB_LUN(v) ((v))
171 #define MANAGEMENT_ORB_FUNCTION(v) ((v) << 16)
172 #define MANAGEMENT_ORB_RECONNECT(v) ((v) << 20)
173 #define MANAGEMENT_ORB_EXCLUSIVE(v) ((v) ? 1 << 28 : 0)
174 #define MANAGEMENT_ORB_REQUEST_FORMAT(v) ((v) << 29)
175 #define MANAGEMENT_ORB_NOTIFY ((1) << 31)
177 #define MANAGEMENT_ORB_RESPONSE_LENGTH(v) ((v))
178 #define MANAGEMENT_ORB_PASSWORD_LENGTH(v) ((v) << 16)
180 struct sbp2_management_orb {
181 struct sbp2_orb base;
183 struct sbp2_pointer password;
184 struct sbp2_pointer response;
187 struct sbp2_pointer status_fifo;
190 dma_addr_t response_bus;
191 struct completion done;
192 struct sbp2_status status;
195 #define LOGIN_RESPONSE_GET_LOGIN_ID(v) ((v).misc & 0xffff)
196 #define LOGIN_RESPONSE_GET_LENGTH(v) (((v).misc >> 16) & 0xffff)
198 struct sbp2_login_response {
200 struct sbp2_pointer command_block_agent;
203 #define COMMAND_ORB_DATA_SIZE(v) ((v))
204 #define COMMAND_ORB_PAGE_SIZE(v) ((v) << 16)
205 #define COMMAND_ORB_PAGE_TABLE_PRESENT ((1) << 19)
206 #define COMMAND_ORB_MAX_PAYLOAD(v) ((v) << 20)
207 #define COMMAND_ORB_SPEED(v) ((v) << 24)
208 #define COMMAND_ORB_DIRECTION(v) ((v) << 27)
209 #define COMMAND_ORB_REQUEST_FORMAT(v) ((v) << 29)
210 #define COMMAND_ORB_NOTIFY ((1) << 31)
212 struct sbp2_command_orb {
213 struct sbp2_orb base;
215 struct sbp2_pointer next;
216 struct sbp2_pointer data_descriptor;
218 u8 command_block[12];
220 struct scsi_cmnd *cmd;
222 struct fw_unit *unit;
224 struct sbp2_pointer page_table[SG_ALL] __attribute__((aligned(8)));
225 dma_addr_t page_table_bus;
229 * List of devices with known bugs.
231 * The firmware_revision field, masked with 0xffff00, is the best
232 * indicator for the type of bridge chip of a device. It yields a few
233 * false positives but this did not break correctly behaving devices
234 * so far. We use ~0 as a wildcard, since the 24 bit values we get
235 * from the config rom can never match that.
237 static const struct {
238 u32 firmware_revision;
240 unsigned workarounds;
241 } sbp2_workarounds_table[] = {
242 /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
243 .firmware_revision = 0x002800,
245 .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
246 SBP2_WORKAROUND_MODE_SENSE_8,
248 /* Initio bridges, actually only needed for some older ones */ {
249 .firmware_revision = 0x000200,
251 .workarounds = SBP2_WORKAROUND_INQUIRY_36,
253 /* Symbios bridge */ {
254 .firmware_revision = 0xa0b800,
256 .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
260 * There are iPods (2nd gen, 3rd gen) with model_id == 0, but
261 * these iPods do not feature the read_capacity bug according
262 * to one report. Read_capacity behaviour as well as model_id
263 * could change due to Apple-supplied firmware updates though.
266 /* iPod 4th generation. */ {
267 .firmware_revision = 0x0a2700,
269 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
272 .firmware_revision = 0x0a2700,
274 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
277 .firmware_revision = 0x0a2700,
279 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
284 free_orb(struct kref *kref)
286 struct sbp2_orb *orb = container_of(kref, struct sbp2_orb, kref);
292 sbp2_status_write(struct fw_card *card, struct fw_request *request,
293 int tcode, int destination, int source,
294 int generation, int speed,
295 unsigned long long offset,
296 void *payload, size_t length, void *callback_data)
298 struct sbp2_device *sd = callback_data;
299 struct sbp2_orb *orb;
300 struct sbp2_status status;
304 if (tcode != TCODE_WRITE_BLOCK_REQUEST ||
305 length == 0 || length > sizeof(status)) {
306 fw_send_response(card, request, RCODE_TYPE_ERROR);
310 header_size = min(length, 2 * sizeof(u32));
311 fw_memcpy_from_be32(&status, payload, header_size);
312 if (length > header_size)
313 memcpy(status.data, payload + 8, length - header_size);
314 if (STATUS_GET_SOURCE(status) == 2 || STATUS_GET_SOURCE(status) == 3) {
315 fw_notify("non-orb related status write, not handled\n");
316 fw_send_response(card, request, RCODE_COMPLETE);
320 /* Lookup the orb corresponding to this status write. */
321 spin_lock_irqsave(&card->lock, flags);
322 list_for_each_entry(orb, &sd->orb_list, link) {
323 if (STATUS_GET_ORB_HIGH(status) == 0 &&
324 STATUS_GET_ORB_LOW(status) == orb->request_bus) {
325 orb->rcode = RCODE_COMPLETE;
326 list_del(&orb->link);
330 spin_unlock_irqrestore(&card->lock, flags);
332 if (&orb->link != &sd->orb_list)
333 orb->callback(orb, &status);
335 fw_error("status write for unknown orb\n");
337 kref_put(&orb->kref, free_orb);
339 fw_send_response(card, request, RCODE_COMPLETE);
343 complete_transaction(struct fw_card *card, int rcode,
344 void *payload, size_t length, void *data)
346 struct sbp2_orb *orb = data;
350 * This is a little tricky. We can get the status write for
351 * the orb before we get this callback. The status write
352 * handler above will assume the orb pointer transaction was
353 * successful and set the rcode to RCODE_COMPLETE for the orb.
354 * So this callback only sets the rcode if it hasn't already
355 * been set and only does the cleanup if the transaction
356 * failed and we didn't already get a status write.
358 spin_lock_irqsave(&card->lock, flags);
360 if (orb->rcode == -1)
362 if (orb->rcode != RCODE_COMPLETE) {
363 list_del(&orb->link);
364 spin_unlock_irqrestore(&card->lock, flags);
365 orb->callback(orb, NULL);
367 spin_unlock_irqrestore(&card->lock, flags);
370 kref_put(&orb->kref, free_orb);
374 sbp2_send_orb(struct sbp2_orb *orb, struct fw_unit *unit,
375 int node_id, int generation, u64 offset)
377 struct fw_device *device = fw_device(unit->device.parent);
378 struct sbp2_device *sd = unit->device.driver_data;
381 orb->pointer.high = 0;
382 orb->pointer.low = orb->request_bus;
383 fw_memcpy_to_be32(&orb->pointer, &orb->pointer, sizeof(orb->pointer));
385 spin_lock_irqsave(&device->card->lock, flags);
386 list_add_tail(&orb->link, &sd->orb_list);
387 spin_unlock_irqrestore(&device->card->lock, flags);
389 /* Take a ref for the orb list and for the transaction callback. */
390 kref_get(&orb->kref);
391 kref_get(&orb->kref);
393 fw_send_request(device->card, &orb->t, TCODE_WRITE_BLOCK_REQUEST,
394 node_id, generation, device->max_speed, offset,
395 &orb->pointer, sizeof(orb->pointer),
396 complete_transaction, orb);
399 static int sbp2_cancel_orbs(struct fw_unit *unit)
401 struct fw_device *device = fw_device(unit->device.parent);
402 struct sbp2_device *sd = unit->device.driver_data;
403 struct sbp2_orb *orb, *next;
404 struct list_head list;
406 int retval = -ENOENT;
408 INIT_LIST_HEAD(&list);
409 spin_lock_irqsave(&device->card->lock, flags);
410 list_splice_init(&sd->orb_list, &list);
411 spin_unlock_irqrestore(&device->card->lock, flags);
413 list_for_each_entry_safe(orb, next, &list, link) {
415 if (fw_cancel_transaction(device->card, &orb->t) == 0)
418 orb->rcode = RCODE_CANCELLED;
419 orb->callback(orb, NULL);
426 complete_management_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
428 struct sbp2_management_orb *orb =
429 container_of(base_orb, struct sbp2_management_orb, base);
432 memcpy(&orb->status, status, sizeof(*status));
433 complete(&orb->done);
437 sbp2_send_management_orb(struct fw_unit *unit, int node_id, int generation,
438 int function, int lun, void *response)
440 struct fw_device *device = fw_device(unit->device.parent);
441 struct sbp2_device *sd = unit->device.driver_data;
442 struct sbp2_management_orb *orb;
443 int retval = -ENOMEM;
445 orb = kzalloc(sizeof(*orb), GFP_ATOMIC);
449 kref_init(&orb->base.kref);
451 dma_map_single(device->card->device, &orb->response,
452 sizeof(orb->response), DMA_FROM_DEVICE);
453 if (dma_mapping_error(orb->response_bus))
454 goto fail_mapping_response;
456 orb->request.response.high = 0;
457 orb->request.response.low = orb->response_bus;
460 MANAGEMENT_ORB_NOTIFY |
461 MANAGEMENT_ORB_FUNCTION(function) |
462 MANAGEMENT_ORB_LUN(lun);
463 orb->request.length =
464 MANAGEMENT_ORB_RESPONSE_LENGTH(sizeof(orb->response));
466 orb->request.status_fifo.high = sd->address_handler.offset >> 32;
467 orb->request.status_fifo.low = sd->address_handler.offset;
469 if (function == SBP2_LOGIN_REQUEST) {
471 MANAGEMENT_ORB_EXCLUSIVE(sbp2_param_exclusive_login) |
472 MANAGEMENT_ORB_RECONNECT(0);
475 fw_memcpy_to_be32(&orb->request, &orb->request, sizeof(orb->request));
477 init_completion(&orb->done);
478 orb->base.callback = complete_management_orb;
480 orb->base.request_bus =
481 dma_map_single(device->card->device, &orb->request,
482 sizeof(orb->request), DMA_TO_DEVICE);
483 if (dma_mapping_error(orb->base.request_bus))
484 goto fail_mapping_request;
486 sbp2_send_orb(&orb->base, unit,
487 node_id, generation, sd->management_agent_address);
489 wait_for_completion_timeout(&orb->done,
490 msecs_to_jiffies(SBP2_ORB_TIMEOUT));
493 if (sbp2_cancel_orbs(unit) == 0) {
494 fw_error("orb reply timed out, rcode=0x%02x\n",
499 if (orb->base.rcode != RCODE_COMPLETE) {
500 fw_error("management write failed, rcode 0x%02x\n",
505 if (STATUS_GET_RESPONSE(orb->status) != 0 ||
506 STATUS_GET_SBP_STATUS(orb->status) != 0) {
507 fw_error("error status: %d:%d\n",
508 STATUS_GET_RESPONSE(orb->status),
509 STATUS_GET_SBP_STATUS(orb->status));
515 dma_unmap_single(device->card->device, orb->base.request_bus,
516 sizeof(orb->request), DMA_TO_DEVICE);
517 fail_mapping_request:
518 dma_unmap_single(device->card->device, orb->response_bus,
519 sizeof(orb->response), DMA_FROM_DEVICE);
520 fail_mapping_response:
522 fw_memcpy_from_be32(response,
523 orb->response, sizeof(orb->response));
524 kref_put(&orb->base.kref, free_orb);
530 complete_agent_reset_write(struct fw_card *card, int rcode,
531 void *payload, size_t length, void *data)
533 struct fw_transaction *t = data;
538 static int sbp2_agent_reset(struct fw_unit *unit)
540 struct fw_device *device = fw_device(unit->device.parent);
541 struct sbp2_device *sd = unit->device.driver_data;
542 struct fw_transaction *t;
545 t = kzalloc(sizeof(*t), GFP_ATOMIC);
549 fw_send_request(device->card, t, TCODE_WRITE_QUADLET_REQUEST,
550 sd->node_id, sd->generation, device->max_speed,
551 sd->command_block_agent_address + SBP2_AGENT_RESET,
552 &zero, sizeof(zero), complete_agent_reset_write, t);
557 static void sbp2_reconnect(struct work_struct *work);
558 static struct scsi_host_template scsi_driver_template;
560 static void release_sbp2_device(struct kref *kref)
562 struct sbp2_device *sd = container_of(kref, struct sbp2_device, kref);
563 struct Scsi_Host *host =
564 container_of((void *)sd, struct Scsi_Host, hostdata[0]);
566 scsi_remove_host(host);
567 sbp2_send_management_orb(sd->unit, sd->node_id, sd->generation,
568 SBP2_LOGOUT_REQUEST, sd->login_id, NULL);
569 fw_core_remove_address_handler(&sd->address_handler);
570 fw_notify("removed sbp2 unit %s\n", sd->unit->device.bus_id);
571 put_device(&sd->unit->device);
575 static void sbp2_login(struct work_struct *work)
577 struct sbp2_device *sd =
578 container_of(work, struct sbp2_device, work.work);
579 struct Scsi_Host *host =
580 container_of((void *)sd, struct Scsi_Host, hostdata[0]);
581 struct fw_unit *unit = sd->unit;
582 struct fw_device *device = fw_device(unit->device.parent);
583 struct sbp2_login_response response;
584 int generation, node_id, local_node_id, lun, retval;
586 /* FIXME: Make this work for multi-lun devices. */
589 generation = device->card->generation;
590 node_id = device->node->node_id;
591 local_node_id = device->card->local_node->node_id;
593 if (sbp2_send_management_orb(unit, node_id, generation,
594 SBP2_LOGIN_REQUEST, lun, &response) < 0) {
595 if (sd->retries++ < 5) {
596 schedule_delayed_work(&sd->work, DIV_ROUND_UP(HZ, 5));
598 fw_error("failed to login to %s\n",
599 unit->device.bus_id);
600 kref_put(&sd->kref, release_sbp2_device);
605 sd->generation = generation;
606 sd->node_id = node_id;
607 sd->address_high = local_node_id << 16;
609 /* Get command block agent offset and login id. */
610 sd->command_block_agent_address =
611 ((u64) (response.command_block_agent.high & 0xffff) << 32) |
612 response.command_block_agent.low;
613 sd->login_id = LOGIN_RESPONSE_GET_LOGIN_ID(response);
615 fw_notify("logged in to sbp2 unit %s (%d retries)\n",
616 unit->device.bus_id, sd->retries);
617 fw_notify(" - management_agent_address: 0x%012llx\n",
618 (unsigned long long) sd->management_agent_address);
619 fw_notify(" - command_block_agent_address: 0x%012llx\n",
620 (unsigned long long) sd->command_block_agent_address);
621 fw_notify(" - status write address: 0x%012llx\n",
622 (unsigned long long) sd->address_handler.offset);
625 /* FIXME: The linux1394 sbp2 does this last step. */
626 sbp2_set_busy_timeout(scsi_id);
629 PREPARE_DELAYED_WORK(&sd->work, sbp2_reconnect);
630 sbp2_agent_reset(unit);
632 /* FIXME: Loop over luns here. */
634 retval = scsi_add_device(host, 0, 0, lun);
636 sbp2_send_management_orb(unit, sd->node_id, sd->generation,
637 SBP2_LOGOUT_REQUEST, sd->login_id,
640 * Set this back to sbp2_login so we fall back and
641 * retry login on bus reset.
643 PREPARE_DELAYED_WORK(&sd->work, sbp2_login);
645 kref_put(&sd->kref, release_sbp2_device);
648 static int sbp2_probe(struct device *dev)
650 struct fw_unit *unit = fw_unit(dev);
651 struct fw_device *device = fw_device(unit->device.parent);
652 struct sbp2_device *sd;
653 struct fw_csr_iterator ci;
654 struct Scsi_Host *host;
655 int i, key, value, err;
656 u32 model, firmware_revision;
659 host = scsi_host_alloc(&scsi_driver_template, sizeof(*sd));
663 sd = (struct sbp2_device *) host->hostdata;
664 unit->device.driver_data = sd;
666 INIT_LIST_HEAD(&sd->orb_list);
667 kref_init(&sd->kref);
669 sd->address_handler.length = 0x100;
670 sd->address_handler.address_callback = sbp2_status_write;
671 sd->address_handler.callback_data = sd;
673 err = fw_core_add_address_handler(&sd->address_handler,
674 &fw_high_memory_region);
678 err = fw_device_enable_phys_dma(device);
680 goto fail_address_handler;
682 err = scsi_add_host(host, &unit->device);
684 goto fail_address_handler;
687 * Scan unit directory to get management agent address,
688 * firmware revison and model. Initialize firmware_revision
689 * and model to values that wont match anything in our table.
691 firmware_revision = 0xff000000;
693 fw_csr_iterator_init(&ci, unit->directory);
694 while (fw_csr_iterator_next(&ci, &key, &value)) {
696 case CSR_DEPENDENT_INFO | CSR_OFFSET:
697 sd->management_agent_address =
698 0xfffff0000000ULL + 4 * value;
700 case SBP2_FIRMWARE_REVISION:
701 firmware_revision = value;
709 for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
710 if (sbp2_workarounds_table[i].firmware_revision !=
711 (firmware_revision & 0xffffff00))
713 if (sbp2_workarounds_table[i].model != model &&
714 sbp2_workarounds_table[i].model != ~0)
716 sd->workarounds |= sbp2_workarounds_table[i].workarounds;
721 fw_notify("Workarounds for node %s: 0x%x "
722 "(firmware_revision 0x%06x, model_id 0x%06x)\n",
724 sd->workarounds, firmware_revision, model);
726 get_device(&unit->device);
729 * We schedule work to do the login so we can easily
730 * reschedule retries. Always get the ref before scheduling
733 INIT_DELAYED_WORK(&sd->work, sbp2_login);
734 if (schedule_delayed_work(&sd->work, 0))
739 fail_address_handler:
740 fw_core_remove_address_handler(&sd->address_handler);
747 static int sbp2_remove(struct device *dev)
749 struct fw_unit *unit = fw_unit(dev);
750 struct sbp2_device *sd = unit->device.driver_data;
752 kref_put(&sd->kref, release_sbp2_device);
757 static void sbp2_reconnect(struct work_struct *work)
759 struct sbp2_device *sd =
760 container_of(work, struct sbp2_device, work.work);
761 struct fw_unit *unit = sd->unit;
762 struct fw_device *device = fw_device(unit->device.parent);
763 int generation, node_id, local_node_id;
765 generation = device->card->generation;
766 node_id = device->node->node_id;
767 local_node_id = device->card->local_node->node_id;
769 if (sbp2_send_management_orb(unit, node_id, generation,
770 SBP2_RECONNECT_REQUEST,
771 sd->login_id, NULL) < 0) {
772 if (sd->retries++ >= 5) {
773 fw_error("failed to reconnect to %s\n",
774 unit->device.bus_id);
775 /* Fall back and try to log in again. */
777 PREPARE_DELAYED_WORK(&sd->work, sbp2_login);
779 schedule_delayed_work(&sd->work, DIV_ROUND_UP(HZ, 5));
783 sd->generation = generation;
784 sd->node_id = node_id;
785 sd->address_high = local_node_id << 16;
787 fw_notify("reconnected to unit %s (%d retries)\n",
788 unit->device.bus_id, sd->retries);
789 sbp2_agent_reset(unit);
790 sbp2_cancel_orbs(unit);
791 kref_put(&sd->kref, release_sbp2_device);
794 static void sbp2_update(struct fw_unit *unit)
796 struct fw_device *device = fw_device(unit->device.parent);
797 struct sbp2_device *sd = unit->device.driver_data;
800 fw_device_enable_phys_dma(device);
801 if (schedule_delayed_work(&sd->work, 0))
805 #define SBP2_UNIT_SPEC_ID_ENTRY 0x0000609e
806 #define SBP2_SW_VERSION_ENTRY 0x00010483
808 static const struct fw_device_id sbp2_id_table[] = {
810 .match_flags = FW_MATCH_SPECIFIER_ID | FW_MATCH_VERSION,
811 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY,
812 .version = SBP2_SW_VERSION_ENTRY,
817 static struct fw_driver sbp2_driver = {
819 .owner = THIS_MODULE,
820 .name = sbp2_driver_name,
823 .remove = sbp2_remove,
825 .update = sbp2_update,
826 .id_table = sbp2_id_table,
830 sbp2_status_to_sense_data(u8 *sbp2_status, u8 *sense_data)
834 sense_data[0] = 0x70;
836 sense_data[2] = sbp2_status[1];
837 sense_data[3] = sbp2_status[4];
838 sense_data[4] = sbp2_status[5];
839 sense_data[5] = sbp2_status[6];
840 sense_data[6] = sbp2_status[7];
842 sense_data[8] = sbp2_status[8];
843 sense_data[9] = sbp2_status[9];
844 sense_data[10] = sbp2_status[10];
845 sense_data[11] = sbp2_status[11];
846 sense_data[12] = sbp2_status[2];
847 sense_data[13] = sbp2_status[3];
848 sense_data[14] = sbp2_status[12];
849 sense_data[15] = sbp2_status[13];
851 sam_status = sbp2_status[0] & 0x3f;
853 switch (sam_status) {
855 case SAM_STAT_CHECK_CONDITION:
856 case SAM_STAT_CONDITION_MET:
858 case SAM_STAT_RESERVATION_CONFLICT:
859 case SAM_STAT_COMMAND_TERMINATED:
860 return DID_OK << 16 | sam_status;
863 return DID_ERROR << 16;
868 complete_command_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
870 struct sbp2_command_orb *orb =
871 container_of(base_orb, struct sbp2_command_orb, base);
872 struct fw_unit *unit = orb->unit;
873 struct fw_device *device = fw_device(unit->device.parent);
876 if (status != NULL) {
877 if (STATUS_GET_DEAD(*status))
878 sbp2_agent_reset(unit);
880 switch (STATUS_GET_RESPONSE(*status)) {
881 case SBP2_STATUS_REQUEST_COMPLETE:
882 result = DID_OK << 16;
884 case SBP2_STATUS_TRANSPORT_FAILURE:
885 result = DID_BUS_BUSY << 16;
887 case SBP2_STATUS_ILLEGAL_REQUEST:
888 case SBP2_STATUS_VENDOR_DEPENDENT:
890 result = DID_ERROR << 16;
894 if (result == DID_OK << 16 && STATUS_GET_LEN(*status) > 1)
895 result = sbp2_status_to_sense_data(STATUS_GET_DATA(*status),
896 orb->cmd->sense_buffer);
899 * If the orb completes with status == NULL, something
900 * went wrong, typically a bus reset happened mid-orb
901 * or when sending the write (less likely).
903 result = DID_BUS_BUSY << 16;
906 dma_unmap_single(device->card->device, orb->base.request_bus,
907 sizeof(orb->request), DMA_TO_DEVICE);
909 if (scsi_sg_count(orb->cmd) > 0)
910 dma_unmap_sg(device->card->device, scsi_sglist(orb->cmd),
911 scsi_sg_count(orb->cmd),
912 orb->cmd->sc_data_direction);
914 if (orb->page_table_bus != 0)
915 dma_unmap_single(device->card->device, orb->page_table_bus,
916 sizeof(orb->page_table), DMA_TO_DEVICE);
918 orb->cmd->result = result;
922 static int sbp2_command_orb_map_scatterlist(struct sbp2_command_orb *orb)
924 struct sbp2_device *sd =
925 (struct sbp2_device *)orb->cmd->device->host->hostdata;
926 struct fw_unit *unit = sd->unit;
927 struct fw_device *device = fw_device(unit->device.parent);
928 struct scatterlist *sg;
929 int sg_len, l, i, j, count;
932 sg = scsi_sglist(orb->cmd);
933 count = dma_map_sg(device->card->device, sg, scsi_sg_count(orb->cmd),
934 orb->cmd->sc_data_direction);
939 * Handle the special case where there is only one element in
940 * the scatter list by converting it to an immediate block
941 * request. This is also a workaround for broken devices such
942 * as the second generation iPod which doesn't support page
945 if (count == 1 && sg_dma_len(sg) < SBP2_MAX_SG_ELEMENT_LENGTH) {
946 orb->request.data_descriptor.high = sd->address_high;
947 orb->request.data_descriptor.low = sg_dma_address(sg);
949 COMMAND_ORB_DATA_SIZE(sg_dma_len(sg));
954 * Convert the scatterlist to an sbp2 page table. If any
955 * scatterlist entries are too big for sbp2, we split them as we
956 * go. Even if we ask the block I/O layer to not give us sg
957 * elements larger than 65535 bytes, some IOMMUs may merge sg elements
958 * during DMA mapping, and Linux currently doesn't prevent this.
960 for (i = 0, j = 0; i < count; i++) {
961 sg_len = sg_dma_len(sg + i);
962 sg_addr = sg_dma_address(sg + i);
964 /* FIXME: This won't get us out of the pinch. */
965 if (unlikely(j >= ARRAY_SIZE(orb->page_table))) {
966 fw_error("page table overflow\n");
967 goto fail_page_table;
969 l = min(sg_len, SBP2_MAX_SG_ELEMENT_LENGTH);
970 orb->page_table[j].low = sg_addr;
971 orb->page_table[j].high = (l << 16);
978 fw_memcpy_to_be32(orb->page_table, orb->page_table,
979 sizeof(orb->page_table[0]) * j);
980 orb->page_table_bus =
981 dma_map_single(device->card->device, orb->page_table,
982 sizeof(orb->page_table), DMA_TO_DEVICE);
983 if (dma_mapping_error(orb->page_table_bus))
984 goto fail_page_table;
987 * The data_descriptor pointer is the one case where we need
988 * to fill in the node ID part of the address. All other
989 * pointers assume that the data referenced reside on the
990 * initiator (i.e. us), but data_descriptor can refer to data
991 * on other nodes so we need to put our ID in descriptor.high.
993 orb->request.data_descriptor.high = sd->address_high;
994 orb->request.data_descriptor.low = orb->page_table_bus;
996 COMMAND_ORB_PAGE_TABLE_PRESENT |
997 COMMAND_ORB_DATA_SIZE(j);
1002 dma_unmap_sg(device->card->device, sg, scsi_sg_count(orb->cmd),
1003 orb->cmd->sc_data_direction);
1008 /* SCSI stack integration */
1010 static int sbp2_scsi_queuecommand(struct scsi_cmnd *cmd, scsi_done_fn_t done)
1012 struct sbp2_device *sd =
1013 (struct sbp2_device *)cmd->device->host->hostdata;
1014 struct fw_unit *unit = sd->unit;
1015 struct fw_device *device = fw_device(unit->device.parent);
1016 struct sbp2_command_orb *orb;
1017 unsigned max_payload;
1020 * Bidirectional commands are not yet implemented, and unknown
1021 * transfer direction not handled.
1023 if (cmd->sc_data_direction == DMA_BIDIRECTIONAL) {
1024 fw_error("Can't handle DMA_BIDIRECTIONAL, rejecting command\n");
1025 cmd->result = DID_ERROR << 16;
1030 orb = kzalloc(sizeof(*orb), GFP_ATOMIC);
1032 fw_notify("failed to alloc orb\n");
1036 /* Initialize rcode to something not RCODE_COMPLETE. */
1037 orb->base.rcode = -1;
1038 kref_init(&orb->base.kref);
1044 orb->request.next.high = SBP2_ORB_NULL;
1045 orb->request.next.low = 0x0;
1047 * At speed 100 we can do 512 bytes per packet, at speed 200,
1048 * 1024 bytes per packet etc. The SBP-2 max_payload field
1049 * specifies the max payload size as 2 ^ (max_payload + 2), so
1050 * if we set this to max_speed + 7, we get the right value.
1052 max_payload = min(device->max_speed + 7,
1053 device->card->max_receive - 1);
1055 COMMAND_ORB_MAX_PAYLOAD(max_payload) |
1056 COMMAND_ORB_SPEED(device->max_speed) |
1059 if (cmd->sc_data_direction == DMA_FROM_DEVICE)
1060 orb->request.misc |=
1061 COMMAND_ORB_DIRECTION(SBP2_DIRECTION_FROM_MEDIA);
1062 else if (cmd->sc_data_direction == DMA_TO_DEVICE)
1063 orb->request.misc |=
1064 COMMAND_ORB_DIRECTION(SBP2_DIRECTION_TO_MEDIA);
1066 if (scsi_sg_count(cmd) && sbp2_command_orb_map_scatterlist(orb) < 0)
1069 fw_memcpy_to_be32(&orb->request, &orb->request, sizeof(orb->request));
1071 memset(orb->request.command_block,
1072 0, sizeof(orb->request.command_block));
1073 memcpy(orb->request.command_block, cmd->cmnd, COMMAND_SIZE(*cmd->cmnd));
1075 orb->base.callback = complete_command_orb;
1076 orb->base.request_bus =
1077 dma_map_single(device->card->device, &orb->request,
1078 sizeof(orb->request), DMA_TO_DEVICE);
1079 if (dma_mapping_error(orb->base.request_bus))
1082 sbp2_send_orb(&orb->base, unit, sd->node_id, sd->generation,
1083 sd->command_block_agent_address + SBP2_ORB_POINTER);
1085 kref_put(&orb->base.kref, free_orb);
1089 kref_put(&orb->base.kref, free_orb);
1091 return SCSI_MLQUEUE_HOST_BUSY;
1094 static int sbp2_scsi_slave_alloc(struct scsi_device *sdev)
1096 struct sbp2_device *sd = (struct sbp2_device *)sdev->host->hostdata;
1098 sdev->allow_restart = 1;
1100 if (sd->workarounds & SBP2_WORKAROUND_INQUIRY_36)
1101 sdev->inquiry_len = 36;
1105 static int sbp2_scsi_slave_configure(struct scsi_device *sdev)
1107 struct sbp2_device *sd = (struct sbp2_device *)sdev->host->hostdata;
1108 struct fw_unit *unit = sd->unit;
1110 sdev->use_10_for_rw = 1;
1112 if (sdev->type == TYPE_ROM)
1113 sdev->use_10_for_ms = 1;
1114 if (sdev->type == TYPE_DISK &&
1115 sd->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
1116 sdev->skip_ms_page_8 = 1;
1117 if (sd->workarounds & SBP2_WORKAROUND_FIX_CAPACITY) {
1118 fw_notify("setting fix_capacity for %s\n", unit->device.bus_id);
1119 sdev->fix_capacity = 1;
1121 if (sd->workarounds & SBP2_WORKAROUND_128K_MAX_TRANS)
1122 blk_queue_max_sectors(sdev->request_queue, 128 * 1024 / 512);
1127 * Called by scsi stack when something has really gone wrong. Usually
1128 * called when a command has timed-out for some reason.
1130 static int sbp2_scsi_abort(struct scsi_cmnd *cmd)
1132 struct sbp2_device *sd =
1133 (struct sbp2_device *)cmd->device->host->hostdata;
1134 struct fw_unit *unit = sd->unit;
1136 fw_notify("sbp2_scsi_abort\n");
1137 sbp2_agent_reset(unit);
1138 sbp2_cancel_orbs(unit);
1144 * Format of /sys/bus/scsi/devices/.../ieee1394_id:
1145 * u64 EUI-64 : u24 directory_ID : u16 LUN (all printed in hexadecimal)
1147 * This is the concatenation of target port identifier and logical unit
1148 * identifier as per SAM-2...SAM-4 annex A.
1151 sbp2_sysfs_ieee1394_id_show(struct device *dev, struct device_attribute *attr,
1154 struct scsi_device *sdev = to_scsi_device(dev);
1155 struct sbp2_device *sd;
1156 struct fw_unit *unit;
1157 struct fw_device *device;
1159 struct fw_csr_iterator ci;
1160 int key, value, lun;
1164 sd = (struct sbp2_device *)sdev->host->hostdata;
1166 device = fw_device(unit->device.parent);
1168 /* implicit directory ID */
1169 directory_id = ((unit->directory - device->config_rom) * 4
1170 + CSR_CONFIG_ROM) & 0xffffff;
1172 /* explicit directory ID, overrides implicit ID if present */
1173 fw_csr_iterator_init(&ci, unit->directory);
1174 while (fw_csr_iterator_next(&ci, &key, &value))
1175 if (key == CSR_DIRECTORY_ID) {
1176 directory_id = value;
1180 /* FIXME: Make this work for multi-lun devices. */
1183 return sprintf(buf, "%08x%08x:%06x:%04x\n",
1184 device->config_rom[3], device->config_rom[4],
1188 static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
1190 static struct device_attribute *sbp2_scsi_sysfs_attrs[] = {
1191 &dev_attr_ieee1394_id,
1195 static struct scsi_host_template scsi_driver_template = {
1196 .module = THIS_MODULE,
1197 .name = "SBP-2 IEEE-1394",
1198 .proc_name = sbp2_driver_name,
1199 .queuecommand = sbp2_scsi_queuecommand,
1200 .slave_alloc = sbp2_scsi_slave_alloc,
1201 .slave_configure = sbp2_scsi_slave_configure,
1202 .eh_abort_handler = sbp2_scsi_abort,
1204 .sg_tablesize = SG_ALL,
1205 .use_clustering = ENABLE_CLUSTERING,
1208 .sdev_attrs = sbp2_scsi_sysfs_attrs,
1211 MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
1212 MODULE_DESCRIPTION("SCSI over IEEE1394");
1213 MODULE_LICENSE("GPL");
1214 MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
1216 /* Provide a module alias so root-on-sbp2 initrds don't break. */
1217 #ifndef CONFIG_IEEE1394_SBP2_MODULE
1218 MODULE_ALIAS("sbp2");
1221 static int __init sbp2_init(void)
1223 return driver_register(&sbp2_driver.driver);
1226 static void __exit sbp2_cleanup(void)
1228 driver_unregister(&sbp2_driver.driver);
1231 module_init(sbp2_init);
1232 module_exit(sbp2_cleanup);