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/timer.h>
41 #include <scsi/scsi.h>
42 #include <scsi/scsi_cmnd.h>
43 #include <scsi/scsi_dbg.h>
44 #include <scsi/scsi_device.h>
45 #include <scsi/scsi_host.h>
47 #include "fw-transaction.h"
48 #include "fw-topology.h"
49 #include "fw-device.h"
52 * So far only bridges from Oxford Semiconductor are known to support
53 * concurrent logins. Depending on firmware, four or two concurrent logins
54 * are possible on OXFW911 and newer Oxsemi bridges.
56 * Concurrent logins are useful together with cluster filesystems.
58 static int sbp2_param_exclusive_login = 1;
59 module_param_named(exclusive_login, sbp2_param_exclusive_login, bool, 0644);
60 MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device "
61 "(default = Y, use N for concurrent initiators)");
63 /* I don't know why the SCSI stack doesn't define something like this... */
64 typedef void (*scsi_done_fn_t)(struct scsi_cmnd *);
66 static const char sbp2_driver_name[] = "sbp2";
71 struct fw_address_handler address_handler;
72 struct list_head orb_list;
73 u64 management_agent_address;
74 u64 command_block_agent_address;
79 * We cache these addresses and only update them once we've
80 * logged in or reconnected to the sbp2 device. That way, any
81 * IO to the device will automatically fail and get retried if
82 * it happens in a window where the device is not ready to
83 * handle it (e.g. after a bus reset but before we reconnect).
90 struct delayed_work work;
93 #define SBP2_MAX_SG_ELEMENT_LENGTH 0xf000
94 #define SBP2_MAX_SECTORS 255 /* Max sectors supported */
95 #define SBP2_ORB_TIMEOUT 2000 /* Timeout in ms */
97 #define SBP2_ORB_NULL 0x80000000
99 #define SBP2_DIRECTION_TO_MEDIA 0x0
100 #define SBP2_DIRECTION_FROM_MEDIA 0x1
102 /* Unit directory keys */
103 #define SBP2_COMMAND_SET_SPECIFIER 0x38
104 #define SBP2_COMMAND_SET 0x39
105 #define SBP2_COMMAND_SET_REVISION 0x3b
106 #define SBP2_FIRMWARE_REVISION 0x3c
108 /* Flags for detected oddities and brokeness */
109 #define SBP2_WORKAROUND_128K_MAX_TRANS 0x1
110 #define SBP2_WORKAROUND_INQUIRY_36 0x2
111 #define SBP2_WORKAROUND_MODE_SENSE_8 0x4
112 #define SBP2_WORKAROUND_FIX_CAPACITY 0x8
113 #define SBP2_WORKAROUND_OVERRIDE 0x100
115 /* Management orb opcodes */
116 #define SBP2_LOGIN_REQUEST 0x0
117 #define SBP2_QUERY_LOGINS_REQUEST 0x1
118 #define SBP2_RECONNECT_REQUEST 0x3
119 #define SBP2_SET_PASSWORD_REQUEST 0x4
120 #define SBP2_LOGOUT_REQUEST 0x7
121 #define SBP2_ABORT_TASK_REQUEST 0xb
122 #define SBP2_ABORT_TASK_SET 0xc
123 #define SBP2_LOGICAL_UNIT_RESET 0xe
124 #define SBP2_TARGET_RESET_REQUEST 0xf
126 /* Offsets for command block agent registers */
127 #define SBP2_AGENT_STATE 0x00
128 #define SBP2_AGENT_RESET 0x04
129 #define SBP2_ORB_POINTER 0x08
130 #define SBP2_DOORBELL 0x10
131 #define SBP2_UNSOLICITED_STATUS_ENABLE 0x14
133 /* Status write response codes */
134 #define SBP2_STATUS_REQUEST_COMPLETE 0x0
135 #define SBP2_STATUS_TRANSPORT_FAILURE 0x1
136 #define SBP2_STATUS_ILLEGAL_REQUEST 0x2
137 #define SBP2_STATUS_VENDOR_DEPENDENT 0x3
139 #define STATUS_GET_ORB_HIGH(v) ((v).status & 0xffff)
140 #define STATUS_GET_SBP_STATUS(v) (((v).status >> 16) & 0xff)
141 #define STATUS_GET_LEN(v) (((v).status >> 24) & 0x07)
142 #define STATUS_GET_DEAD(v) (((v).status >> 27) & 0x01)
143 #define STATUS_GET_RESPONSE(v) (((v).status >> 28) & 0x03)
144 #define STATUS_GET_SOURCE(v) (((v).status >> 30) & 0x03)
145 #define STATUS_GET_ORB_LOW(v) ((v).orb_low)
146 #define STATUS_GET_DATA(v) ((v).data)
154 struct sbp2_pointer {
160 struct fw_transaction t;
161 dma_addr_t request_bus;
163 struct sbp2_pointer pointer;
164 void (*callback)(struct sbp2_orb * orb, struct sbp2_status * status);
165 struct list_head link;
168 #define MANAGEMENT_ORB_LUN(v) ((v))
169 #define MANAGEMENT_ORB_FUNCTION(v) ((v) << 16)
170 #define MANAGEMENT_ORB_RECONNECT(v) ((v) << 20)
171 #define MANAGEMENT_ORB_EXCLUSIVE(v) ((v) ? 1 << 28 : 0)
172 #define MANAGEMENT_ORB_REQUEST_FORMAT(v) ((v) << 29)
173 #define MANAGEMENT_ORB_NOTIFY ((1) << 31)
175 #define MANAGEMENT_ORB_RESPONSE_LENGTH(v) ((v))
176 #define MANAGEMENT_ORB_PASSWORD_LENGTH(v) ((v) << 16)
178 struct sbp2_management_orb {
179 struct sbp2_orb base;
181 struct sbp2_pointer password;
182 struct sbp2_pointer response;
185 struct sbp2_pointer status_fifo;
188 dma_addr_t response_bus;
189 struct completion done;
190 struct sbp2_status status;
193 #define LOGIN_RESPONSE_GET_LOGIN_ID(v) ((v).misc & 0xffff)
194 #define LOGIN_RESPONSE_GET_LENGTH(v) (((v).misc >> 16) & 0xffff)
196 struct sbp2_login_response {
198 struct sbp2_pointer command_block_agent;
201 #define COMMAND_ORB_DATA_SIZE(v) ((v))
202 #define COMMAND_ORB_PAGE_SIZE(v) ((v) << 16)
203 #define COMMAND_ORB_PAGE_TABLE_PRESENT ((1) << 19)
204 #define COMMAND_ORB_MAX_PAYLOAD(v) ((v) << 20)
205 #define COMMAND_ORB_SPEED(v) ((v) << 24)
206 #define COMMAND_ORB_DIRECTION(v) ((v) << 27)
207 #define COMMAND_ORB_REQUEST_FORMAT(v) ((v) << 29)
208 #define COMMAND_ORB_NOTIFY ((1) << 31)
210 struct sbp2_command_orb {
211 struct sbp2_orb base;
213 struct sbp2_pointer next;
214 struct sbp2_pointer data_descriptor;
216 u8 command_block[12];
218 struct scsi_cmnd *cmd;
220 struct fw_unit *unit;
222 struct sbp2_pointer page_table[SG_ALL];
223 dma_addr_t page_table_bus;
227 * List of devices with known bugs.
229 * The firmware_revision field, masked with 0xffff00, is the best
230 * indicator for the type of bridge chip of a device. It yields a few
231 * false positives but this did not break correctly behaving devices
232 * so far. We use ~0 as a wildcard, since the 24 bit values we get
233 * from the config rom can never match that.
235 static const struct {
236 u32 firmware_revision;
238 unsigned workarounds;
239 } sbp2_workarounds_table[] = {
240 /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
241 .firmware_revision = 0x002800,
243 .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
244 SBP2_WORKAROUND_MODE_SENSE_8,
246 /* Initio bridges, actually only needed for some older ones */ {
247 .firmware_revision = 0x000200,
249 .workarounds = SBP2_WORKAROUND_INQUIRY_36,
251 /* Symbios bridge */ {
252 .firmware_revision = 0xa0b800,
254 .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
258 * There are iPods (2nd gen, 3rd gen) with model_id == 0, but
259 * these iPods do not feature the read_capacity bug according
260 * to one report. Read_capacity behaviour as well as model_id
261 * could change due to Apple-supplied firmware updates though.
264 /* iPod 4th generation. */ {
265 .firmware_revision = 0x0a2700,
267 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
270 .firmware_revision = 0x0a2700,
272 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
275 .firmware_revision = 0x0a2700,
277 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
282 sbp2_status_write(struct fw_card *card, struct fw_request *request,
283 int tcode, int destination, int source,
284 int generation, int speed,
285 unsigned long long offset,
286 void *payload, size_t length, void *callback_data)
288 struct sbp2_device *sd = callback_data;
289 struct sbp2_orb *orb;
290 struct sbp2_status status;
294 if (tcode != TCODE_WRITE_BLOCK_REQUEST ||
295 length == 0 || length > sizeof(status)) {
296 fw_send_response(card, request, RCODE_TYPE_ERROR);
300 header_size = min(length, 2 * sizeof(u32));
301 fw_memcpy_from_be32(&status, payload, header_size);
302 if (length > header_size)
303 memcpy(status.data, payload + 8, length - header_size);
304 if (STATUS_GET_SOURCE(status) == 2 || STATUS_GET_SOURCE(status) == 3) {
305 fw_notify("non-orb related status write, not handled\n");
306 fw_send_response(card, request, RCODE_COMPLETE);
310 /* Lookup the orb corresponding to this status write. */
311 spin_lock_irqsave(&card->lock, flags);
312 list_for_each_entry(orb, &sd->orb_list, link) {
313 if (STATUS_GET_ORB_HIGH(status) == 0 &&
314 STATUS_GET_ORB_LOW(status) == orb->request_bus &&
315 orb->rcode == RCODE_COMPLETE) {
316 list_del(&orb->link);
320 spin_unlock_irqrestore(&card->lock, flags);
322 if (&orb->link != &sd->orb_list)
323 orb->callback(orb, &status);
325 fw_error("status write for unknown orb\n");
327 fw_send_response(card, request, RCODE_COMPLETE);
331 complete_transaction(struct fw_card *card, int rcode,
332 void *payload, size_t length, void *data)
334 struct sbp2_orb *orb = data;
338 if (rcode != RCODE_COMPLETE) {
339 spin_lock_irqsave(&card->lock, flags);
340 list_del(&orb->link);
341 spin_unlock_irqrestore(&card->lock, flags);
342 orb->callback(orb, NULL);
347 sbp2_send_orb(struct sbp2_orb *orb, struct fw_unit *unit,
348 int node_id, int generation, u64 offset)
350 struct fw_device *device = fw_device(unit->device.parent);
351 struct sbp2_device *sd = unit->device.driver_data;
354 orb->pointer.high = 0;
355 orb->pointer.low = orb->request_bus;
356 fw_memcpy_to_be32(&orb->pointer, &orb->pointer, sizeof(orb->pointer));
358 spin_lock_irqsave(&device->card->lock, flags);
359 list_add_tail(&orb->link, &sd->orb_list);
360 spin_unlock_irqrestore(&device->card->lock, flags);
362 fw_send_request(device->card, &orb->t, TCODE_WRITE_BLOCK_REQUEST,
363 node_id, generation, device->max_speed, offset,
364 &orb->pointer, sizeof(orb->pointer),
365 complete_transaction, orb);
368 static int sbp2_cancel_orbs(struct fw_unit *unit)
370 struct fw_device *device = fw_device(unit->device.parent);
371 struct sbp2_device *sd = unit->device.driver_data;
372 struct sbp2_orb *orb, *next;
373 struct list_head list;
375 int retval = -ENOENT;
377 INIT_LIST_HEAD(&list);
378 spin_lock_irqsave(&device->card->lock, flags);
379 list_splice_init(&sd->orb_list, &list);
380 spin_unlock_irqrestore(&device->card->lock, flags);
382 list_for_each_entry_safe(orb, next, &list, link) {
384 if (fw_cancel_transaction(device->card, &orb->t) == 0)
387 orb->rcode = RCODE_CANCELLED;
388 orb->callback(orb, NULL);
395 complete_management_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
397 struct sbp2_management_orb *orb =
398 (struct sbp2_management_orb *)base_orb;
401 memcpy(&orb->status, status, sizeof(*status));
402 complete(&orb->done);
406 sbp2_send_management_orb(struct fw_unit *unit, int node_id, int generation,
407 int function, int lun, void *response)
409 struct fw_device *device = fw_device(unit->device.parent);
410 struct sbp2_device *sd = unit->device.driver_data;
411 struct sbp2_management_orb *orb;
412 int retval = -ENOMEM;
414 orb = kzalloc(sizeof(*orb), GFP_ATOMIC);
419 * The sbp2 device is going to send a block read request to
420 * read out the request from host memory, so map it for dma.
422 orb->base.request_bus =
423 dma_map_single(device->card->device, &orb->request,
424 sizeof(orb->request), DMA_TO_DEVICE);
425 if (dma_mapping_error(orb->base.request_bus))
429 dma_map_single(device->card->device, &orb->response,
430 sizeof(orb->response), DMA_FROM_DEVICE);
431 if (dma_mapping_error(orb->response_bus))
434 orb->request.response.high = 0;
435 orb->request.response.low = orb->response_bus;
438 MANAGEMENT_ORB_NOTIFY |
439 MANAGEMENT_ORB_FUNCTION(function) |
440 MANAGEMENT_ORB_LUN(lun);
441 orb->request.length =
442 MANAGEMENT_ORB_RESPONSE_LENGTH(sizeof(orb->response));
444 orb->request.status_fifo.high = sd->address_handler.offset >> 32;
445 orb->request.status_fifo.low = sd->address_handler.offset;
447 if (function == SBP2_LOGIN_REQUEST) {
449 MANAGEMENT_ORB_EXCLUSIVE(sbp2_param_exclusive_login) |
450 MANAGEMENT_ORB_RECONNECT(0);
453 fw_memcpy_to_be32(&orb->request, &orb->request, sizeof(orb->request));
455 init_completion(&orb->done);
456 orb->base.callback = complete_management_orb;
458 sbp2_send_orb(&orb->base, unit,
459 node_id, generation, sd->management_agent_address);
461 wait_for_completion_timeout(&orb->done,
462 msecs_to_jiffies(SBP2_ORB_TIMEOUT));
465 if (sbp2_cancel_orbs(unit) == 0) {
466 fw_error("orb reply timed out, rcode=0x%02x\n",
471 if (orb->base.rcode != RCODE_COMPLETE) {
472 fw_error("management write failed, rcode 0x%02x\n",
477 if (STATUS_GET_RESPONSE(orb->status) != 0 ||
478 STATUS_GET_SBP_STATUS(orb->status) != 0) {
479 fw_error("error status: %d:%d\n",
480 STATUS_GET_RESPONSE(orb->status),
481 STATUS_GET_SBP_STATUS(orb->status));
487 dma_unmap_single(device->card->device, orb->base.request_bus,
488 sizeof(orb->request), DMA_TO_DEVICE);
489 dma_unmap_single(device->card->device, orb->response_bus,
490 sizeof(orb->response), DMA_FROM_DEVICE);
493 fw_memcpy_from_be32(response,
494 orb->response, sizeof(orb->response));
501 complete_agent_reset_write(struct fw_card *card, int rcode,
502 void *payload, size_t length, void *data)
504 struct fw_transaction *t = data;
509 static int sbp2_agent_reset(struct fw_unit *unit)
511 struct fw_device *device = fw_device(unit->device.parent);
512 struct sbp2_device *sd = unit->device.driver_data;
513 struct fw_transaction *t;
516 t = kzalloc(sizeof(*t), GFP_ATOMIC);
520 fw_send_request(device->card, t, TCODE_WRITE_QUADLET_REQUEST,
521 sd->node_id, sd->generation, SCODE_400,
522 sd->command_block_agent_address + SBP2_AGENT_RESET,
523 &zero, sizeof(zero), complete_agent_reset_write, t);
528 static void sbp2_reconnect(struct work_struct *work);
529 static struct scsi_host_template scsi_driver_template;
531 static void release_sbp2_device(struct kref *kref)
533 struct sbp2_device *sd = container_of(kref, struct sbp2_device, kref);
534 struct Scsi_Host *host =
535 container_of((void *)sd, struct Scsi_Host, hostdata[0]);
537 scsi_remove_host(host);
538 sbp2_send_management_orb(sd->unit, sd->node_id, sd->generation,
539 SBP2_LOGOUT_REQUEST, sd->login_id, NULL);
540 fw_core_remove_address_handler(&sd->address_handler);
541 fw_notify("removed sbp2 unit %s\n", sd->unit->device.bus_id);
542 put_device(&sd->unit->device);
546 static void sbp2_login(struct work_struct *work)
548 struct sbp2_device *sd =
549 container_of(work, struct sbp2_device, work.work);
550 struct Scsi_Host *host =
551 container_of((void *)sd, struct Scsi_Host, hostdata[0]);
552 struct fw_unit *unit = sd->unit;
553 struct fw_device *device = fw_device(unit->device.parent);
554 struct sbp2_login_response response;
555 int generation, node_id, local_node_id, lun, retval;
557 /* FIXME: Make this work for multi-lun devices. */
560 generation = device->card->generation;
561 node_id = device->node->node_id;
562 local_node_id = device->card->local_node->node_id;
564 if (sbp2_send_management_orb(unit, node_id, generation,
565 SBP2_LOGIN_REQUEST, lun, &response) < 0) {
566 if (sd->retries++ < 5) {
567 schedule_delayed_work(&sd->work, DIV_ROUND_UP(HZ, 5));
569 fw_error("failed to login to %s\n",
570 unit->device.bus_id);
571 kref_put(&sd->kref, release_sbp2_device);
576 sd->generation = generation;
577 sd->node_id = node_id;
578 sd->address_high = local_node_id << 16;
580 /* Get command block agent offset and login id. */
581 sd->command_block_agent_address =
582 ((u64) (response.command_block_agent.high & 0xffff) << 32) |
583 response.command_block_agent.low;
584 sd->login_id = LOGIN_RESPONSE_GET_LOGIN_ID(response);
586 fw_notify("logged in to sbp2 unit %s (%d retries)\n",
587 unit->device.bus_id, sd->retries);
588 fw_notify(" - management_agent_address: 0x%012llx\n",
589 (unsigned long long) sd->management_agent_address);
590 fw_notify(" - command_block_agent_address: 0x%012llx\n",
591 (unsigned long long) sd->command_block_agent_address);
592 fw_notify(" - status write address: 0x%012llx\n",
593 (unsigned long long) sd->address_handler.offset);
596 /* FIXME: The linux1394 sbp2 does this last step. */
597 sbp2_set_busy_timeout(scsi_id);
600 PREPARE_DELAYED_WORK(&sd->work, sbp2_reconnect);
601 sbp2_agent_reset(unit);
603 /* FIXME: Loop over luns here. */
605 retval = scsi_add_device(host, 0, 0, lun);
607 sbp2_send_management_orb(unit, sd->node_id, sd->generation,
608 SBP2_LOGOUT_REQUEST, sd->login_id,
611 * Set this back to sbp2_login so we fall back and
612 * retry login on bus reset.
614 PREPARE_DELAYED_WORK(&sd->work, sbp2_login);
616 kref_put(&sd->kref, release_sbp2_device);
619 static int sbp2_probe(struct device *dev)
621 struct fw_unit *unit = fw_unit(dev);
622 struct fw_device *device = fw_device(unit->device.parent);
623 struct sbp2_device *sd;
624 struct fw_csr_iterator ci;
625 struct Scsi_Host *host;
626 int i, key, value, err;
627 u32 model, firmware_revision;
630 host = scsi_host_alloc(&scsi_driver_template, sizeof(*sd));
634 sd = (struct sbp2_device *) host->hostdata;
635 unit->device.driver_data = sd;
637 INIT_LIST_HEAD(&sd->orb_list);
638 kref_init(&sd->kref);
640 sd->address_handler.length = 0x100;
641 sd->address_handler.address_callback = sbp2_status_write;
642 sd->address_handler.callback_data = sd;
644 err = fw_core_add_address_handler(&sd->address_handler,
645 &fw_high_memory_region);
649 err = fw_device_enable_phys_dma(device);
651 goto fail_address_handler;
653 err = scsi_add_host(host, &unit->device);
655 goto fail_address_handler;
658 * Scan unit directory to get management agent address,
659 * firmware revison and model. Initialize firmware_revision
660 * and model to values that wont match anything in our table.
662 firmware_revision = 0xff000000;
664 fw_csr_iterator_init(&ci, unit->directory);
665 while (fw_csr_iterator_next(&ci, &key, &value)) {
667 case CSR_DEPENDENT_INFO | CSR_OFFSET:
668 sd->management_agent_address =
669 0xfffff0000000ULL + 4 * value;
671 case SBP2_FIRMWARE_REVISION:
672 firmware_revision = value;
680 for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
681 if (sbp2_workarounds_table[i].firmware_revision !=
682 (firmware_revision & 0xffffff00))
684 if (sbp2_workarounds_table[i].model != model &&
685 sbp2_workarounds_table[i].model != ~0)
687 sd->workarounds |= sbp2_workarounds_table[i].workarounds;
692 fw_notify("Workarounds for node %s: 0x%x "
693 "(firmware_revision 0x%06x, model_id 0x%06x)\n",
695 sd->workarounds, firmware_revision, model);
697 get_device(&unit->device);
700 * We schedule work to do the login so we can easily
701 * reschedule retries. Always get the ref before scheduling
704 INIT_DELAYED_WORK(&sd->work, sbp2_login);
705 if (schedule_delayed_work(&sd->work, 0))
710 fail_address_handler:
711 fw_core_remove_address_handler(&sd->address_handler);
718 static int sbp2_remove(struct device *dev)
720 struct fw_unit *unit = fw_unit(dev);
721 struct sbp2_device *sd = unit->device.driver_data;
723 kref_put(&sd->kref, release_sbp2_device);
728 static void sbp2_reconnect(struct work_struct *work)
730 struct sbp2_device *sd =
731 container_of(work, struct sbp2_device, work.work);
732 struct fw_unit *unit = sd->unit;
733 struct fw_device *device = fw_device(unit->device.parent);
734 int generation, node_id, local_node_id;
736 generation = device->card->generation;
737 node_id = device->node->node_id;
738 local_node_id = device->card->local_node->node_id;
740 if (sbp2_send_management_orb(unit, node_id, generation,
741 SBP2_RECONNECT_REQUEST,
742 sd->login_id, NULL) < 0) {
743 if (sd->retries++ >= 5) {
744 fw_error("failed to reconnect to %s\n",
745 unit->device.bus_id);
746 /* Fall back and try to log in again. */
748 PREPARE_DELAYED_WORK(&sd->work, sbp2_login);
750 schedule_delayed_work(&sd->work, DIV_ROUND_UP(HZ, 5));
754 sd->generation = generation;
755 sd->node_id = node_id;
756 sd->address_high = local_node_id << 16;
758 fw_notify("reconnected to unit %s (%d retries)\n",
759 unit->device.bus_id, sd->retries);
760 sbp2_agent_reset(unit);
761 sbp2_cancel_orbs(unit);
762 kref_put(&sd->kref, release_sbp2_device);
765 static void sbp2_update(struct fw_unit *unit)
767 struct fw_device *device = fw_device(unit->device.parent);
768 struct sbp2_device *sd = unit->device.driver_data;
771 fw_device_enable_phys_dma(device);
772 if (schedule_delayed_work(&sd->work, 0))
776 #define SBP2_UNIT_SPEC_ID_ENTRY 0x0000609e
777 #define SBP2_SW_VERSION_ENTRY 0x00010483
779 static const struct fw_device_id sbp2_id_table[] = {
781 .match_flags = FW_MATCH_SPECIFIER_ID | FW_MATCH_VERSION,
782 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY,
783 .version = SBP2_SW_VERSION_ENTRY,
788 static struct fw_driver sbp2_driver = {
790 .owner = THIS_MODULE,
791 .name = sbp2_driver_name,
794 .remove = sbp2_remove,
796 .update = sbp2_update,
797 .id_table = sbp2_id_table,
801 sbp2_status_to_sense_data(u8 *sbp2_status, u8 *sense_data)
805 sense_data[0] = 0x70;
807 sense_data[2] = sbp2_status[1];
808 sense_data[3] = sbp2_status[4];
809 sense_data[4] = sbp2_status[5];
810 sense_data[5] = sbp2_status[6];
811 sense_data[6] = sbp2_status[7];
813 sense_data[8] = sbp2_status[8];
814 sense_data[9] = sbp2_status[9];
815 sense_data[10] = sbp2_status[10];
816 sense_data[11] = sbp2_status[11];
817 sense_data[12] = sbp2_status[2];
818 sense_data[13] = sbp2_status[3];
819 sense_data[14] = sbp2_status[12];
820 sense_data[15] = sbp2_status[13];
822 sam_status = sbp2_status[0] & 0x3f;
824 switch (sam_status) {
826 case SAM_STAT_CHECK_CONDITION:
827 case SAM_STAT_CONDITION_MET:
829 case SAM_STAT_RESERVATION_CONFLICT:
830 case SAM_STAT_COMMAND_TERMINATED:
831 return DID_OK << 16 | sam_status;
834 return DID_ERROR << 16;
839 complete_command_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
841 struct sbp2_command_orb *orb = (struct sbp2_command_orb *)base_orb;
842 struct fw_unit *unit = orb->unit;
843 struct fw_device *device = fw_device(unit->device.parent);
844 struct scatterlist *sg;
847 if (status != NULL) {
848 if (STATUS_GET_DEAD(*status))
849 sbp2_agent_reset(unit);
851 switch (STATUS_GET_RESPONSE(*status)) {
852 case SBP2_STATUS_REQUEST_COMPLETE:
853 result = DID_OK << 16;
855 case SBP2_STATUS_TRANSPORT_FAILURE:
856 result = DID_BUS_BUSY << 16;
858 case SBP2_STATUS_ILLEGAL_REQUEST:
859 case SBP2_STATUS_VENDOR_DEPENDENT:
861 result = DID_ERROR << 16;
865 if (result == DID_OK << 16 && STATUS_GET_LEN(*status) > 1)
866 result = sbp2_status_to_sense_data(STATUS_GET_DATA(*status),
867 orb->cmd->sense_buffer);
870 * If the orb completes with status == NULL, something
871 * went wrong, typically a bus reset happened mid-orb
872 * or when sending the write (less likely).
874 result = DID_BUS_BUSY << 16;
877 dma_unmap_single(device->card->device, orb->base.request_bus,
878 sizeof(orb->request), DMA_TO_DEVICE);
880 if (orb->cmd->use_sg > 0) {
881 sg = (struct scatterlist *)orb->cmd->request_buffer;
882 dma_unmap_sg(device->card->device, sg, orb->cmd->use_sg,
883 orb->cmd->sc_data_direction);
886 if (orb->page_table_bus != 0)
887 dma_unmap_single(device->card->device, orb->page_table_bus,
888 sizeof(orb->page_table_bus), DMA_TO_DEVICE);
890 orb->cmd->result = result;
895 static int sbp2_command_orb_map_scatterlist(struct sbp2_command_orb *orb)
897 struct sbp2_device *sd =
898 (struct sbp2_device *)orb->cmd->device->host->hostdata;
899 struct fw_unit *unit = sd->unit;
900 struct fw_device *device = fw_device(unit->device.parent);
901 struct scatterlist *sg;
902 int sg_len, l, i, j, count;
906 sg = (struct scatterlist *)orb->cmd->request_buffer;
907 count = dma_map_sg(device->card->device, sg, orb->cmd->use_sg,
908 orb->cmd->sc_data_direction);
913 * Handle the special case where there is only one element in
914 * the scatter list by converting it to an immediate block
915 * request. This is also a workaround for broken devices such
916 * as the second generation iPod which doesn't support page
919 if (count == 1 && sg_dma_len(sg) < SBP2_MAX_SG_ELEMENT_LENGTH) {
920 orb->request.data_descriptor.high = sd->address_high;
921 orb->request.data_descriptor.low = sg_dma_address(sg);
923 COMMAND_ORB_DATA_SIZE(sg_dma_len(sg));
928 * Convert the scatterlist to an sbp2 page table. If any
929 * scatterlist entries are too big for sbp2, we split them as we
930 * go. Even if we ask the block I/O layer to not give us sg
931 * elements larger than 65535 bytes, some IOMMUs may merge sg elements
932 * during DMA mapping, and Linux currently doesn't prevent this.
934 for (i = 0, j = 0; i < count; i++) {
935 sg_len = sg_dma_len(sg + i);
936 sg_addr = sg_dma_address(sg + i);
938 l = min(sg_len, SBP2_MAX_SG_ELEMENT_LENGTH);
939 orb->page_table[j].low = sg_addr;
940 orb->page_table[j].high = (l << 16);
947 size = sizeof(orb->page_table[0]) * j;
950 * The data_descriptor pointer is the one case where we need
951 * to fill in the node ID part of the address. All other
952 * pointers assume that the data referenced reside on the
953 * initiator (i.e. us), but data_descriptor can refer to data
954 * on other nodes so we need to put our ID in descriptor.high.
957 orb->page_table_bus =
958 dma_map_single(device->card->device, orb->page_table,
959 size, DMA_TO_DEVICE);
960 if (dma_mapping_error(orb->page_table_bus))
961 goto fail_page_table;
962 orb->request.data_descriptor.high = sd->address_high;
963 orb->request.data_descriptor.low = orb->page_table_bus;
965 COMMAND_ORB_PAGE_TABLE_PRESENT |
966 COMMAND_ORB_DATA_SIZE(j);
968 fw_memcpy_to_be32(orb->page_table, orb->page_table, size);
973 dma_unmap_sg(device->card->device, sg, orb->cmd->use_sg,
974 orb->cmd->sc_data_direction);
979 /* SCSI stack integration */
981 static int sbp2_scsi_queuecommand(struct scsi_cmnd *cmd, scsi_done_fn_t done)
983 struct sbp2_device *sd =
984 (struct sbp2_device *)cmd->device->host->hostdata;
985 struct fw_unit *unit = sd->unit;
986 struct fw_device *device = fw_device(unit->device.parent);
987 struct sbp2_command_orb *orb;
990 * Bidirectional commands are not yet implemented, and unknown
991 * transfer direction not handled.
993 if (cmd->sc_data_direction == DMA_BIDIRECTIONAL) {
994 fw_error("Can't handle DMA_BIDIRECTIONAL, rejecting command\n");
995 cmd->result = DID_ERROR << 16;
1000 orb = kzalloc(sizeof(*orb), GFP_ATOMIC);
1002 fw_notify("failed to alloc orb\n");
1006 /* Initialize rcode to something not RCODE_COMPLETE. */
1007 orb->base.rcode = -1;
1008 orb->base.request_bus =
1009 dma_map_single(device->card->device, &orb->request,
1010 sizeof(orb->request), DMA_TO_DEVICE);
1011 if (dma_mapping_error(orb->base.request_bus))
1018 orb->request.next.high = SBP2_ORB_NULL;
1019 orb->request.next.low = 0x0;
1021 * At speed 100 we can do 512 bytes per packet, at speed 200,
1022 * 1024 bytes per packet etc. The SBP-2 max_payload field
1023 * specifies the max payload size as 2 ^ (max_payload + 2), so
1024 * if we set this to max_speed + 7, we get the right value.
1027 COMMAND_ORB_MAX_PAYLOAD(device->max_speed + 7) |
1028 COMMAND_ORB_SPEED(device->max_speed) |
1031 if (cmd->sc_data_direction == DMA_FROM_DEVICE)
1032 orb->request.misc |=
1033 COMMAND_ORB_DIRECTION(SBP2_DIRECTION_FROM_MEDIA);
1034 else if (cmd->sc_data_direction == DMA_TO_DEVICE)
1035 orb->request.misc |=
1036 COMMAND_ORB_DIRECTION(SBP2_DIRECTION_TO_MEDIA);
1038 if (cmd->use_sg && sbp2_command_orb_map_scatterlist(orb) < 0)
1039 goto fail_map_payload;
1041 fw_memcpy_to_be32(&orb->request, &orb->request, sizeof(orb->request));
1043 memset(orb->request.command_block,
1044 0, sizeof(orb->request.command_block));
1045 memcpy(orb->request.command_block, cmd->cmnd, COMMAND_SIZE(*cmd->cmnd));
1047 orb->base.callback = complete_command_orb;
1049 sbp2_send_orb(&orb->base, unit, sd->node_id, sd->generation,
1050 sd->command_block_agent_address + SBP2_ORB_POINTER);
1055 dma_unmap_single(device->card->device, orb->base.request_bus,
1056 sizeof(orb->request), DMA_TO_DEVICE);
1060 return SCSI_MLQUEUE_HOST_BUSY;
1063 static int sbp2_scsi_slave_alloc(struct scsi_device *sdev)
1065 struct sbp2_device *sd = (struct sbp2_device *)sdev->host->hostdata;
1067 sdev->allow_restart = 1;
1069 if (sd->workarounds & SBP2_WORKAROUND_INQUIRY_36)
1070 sdev->inquiry_len = 36;
1074 static int sbp2_scsi_slave_configure(struct scsi_device *sdev)
1076 struct sbp2_device *sd = (struct sbp2_device *)sdev->host->hostdata;
1077 struct fw_unit *unit = sd->unit;
1079 sdev->use_10_for_rw = 1;
1081 if (sdev->type == TYPE_ROM)
1082 sdev->use_10_for_ms = 1;
1083 if (sdev->type == TYPE_DISK &&
1084 sd->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
1085 sdev->skip_ms_page_8 = 1;
1086 if (sd->workarounds & SBP2_WORKAROUND_FIX_CAPACITY) {
1087 fw_notify("setting fix_capacity for %s\n", unit->device.bus_id);
1088 sdev->fix_capacity = 1;
1090 if (sd->workarounds & SBP2_WORKAROUND_128K_MAX_TRANS)
1091 blk_queue_max_sectors(sdev->request_queue, 128 * 1024 / 512);
1096 * Called by scsi stack when something has really gone wrong. Usually
1097 * called when a command has timed-out for some reason.
1099 static int sbp2_scsi_abort(struct scsi_cmnd *cmd)
1101 struct sbp2_device *sd =
1102 (struct sbp2_device *)cmd->device->host->hostdata;
1103 struct fw_unit *unit = sd->unit;
1105 fw_notify("sbp2_scsi_abort\n");
1106 sbp2_agent_reset(unit);
1107 sbp2_cancel_orbs(unit);
1113 * Format of /sys/bus/scsi/devices/.../ieee1394_id:
1114 * u64 EUI-64 : u24 directory_ID : u16 LUN (all printed in hexadecimal)
1116 * This is the concatenation of target port identifier and logical unit
1117 * identifier as per SAM-2...SAM-4 annex A.
1120 sbp2_sysfs_ieee1394_id_show(struct device *dev, struct device_attribute *attr,
1123 struct scsi_device *sdev = to_scsi_device(dev);
1124 struct sbp2_device *sd;
1125 struct fw_unit *unit;
1126 struct fw_device *device;
1128 struct fw_csr_iterator ci;
1129 int key, value, lun;
1133 sd = (struct sbp2_device *)sdev->host->hostdata;
1135 device = fw_device(unit->device.parent);
1137 /* implicit directory ID */
1138 directory_id = ((unit->directory - device->config_rom) * 4
1139 + CSR_CONFIG_ROM) & 0xffffff;
1141 /* explicit directory ID, overrides implicit ID if present */
1142 fw_csr_iterator_init(&ci, unit->directory);
1143 while (fw_csr_iterator_next(&ci, &key, &value))
1144 if (key == CSR_DIRECTORY_ID) {
1145 directory_id = value;
1149 /* FIXME: Make this work for multi-lun devices. */
1152 return sprintf(buf, "%08x%08x:%06x:%04x\n",
1153 device->config_rom[3], device->config_rom[4],
1157 static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
1159 static struct device_attribute *sbp2_scsi_sysfs_attrs[] = {
1160 &dev_attr_ieee1394_id,
1164 static struct scsi_host_template scsi_driver_template = {
1165 .module = THIS_MODULE,
1166 .name = "SBP-2 IEEE-1394",
1167 .proc_name = (char *)sbp2_driver_name,
1168 .queuecommand = sbp2_scsi_queuecommand,
1169 .slave_alloc = sbp2_scsi_slave_alloc,
1170 .slave_configure = sbp2_scsi_slave_configure,
1171 .eh_abort_handler = sbp2_scsi_abort,
1173 .sg_tablesize = SG_ALL,
1174 .use_clustering = ENABLE_CLUSTERING,
1177 .sdev_attrs = sbp2_scsi_sysfs_attrs,
1180 MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
1181 MODULE_DESCRIPTION("SCSI over IEEE1394");
1182 MODULE_LICENSE("GPL");
1183 MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
1185 /* Provide a module alias so root-on-sbp2 initrds don't break. */
1186 #ifndef CONFIG_IEEE1394_SBP2_MODULE
1187 MODULE_ALIAS("sbp2");
1190 static int __init sbp2_init(void)
1192 return driver_register(&sbp2_driver.driver);
1195 static void __exit sbp2_cleanup(void)
1197 driver_unregister(&sbp2_driver.driver);
1200 module_init(sbp2_init);
1201 module_exit(sbp2_cleanup);