1 /* -*- c-basic-offset: 8 -*-
2 * fw-spb2.c -- 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.
21 /* The basic structure of this driver is based the old storage driver,
22 * drivers/ieee1394/sbp2.c, originally written by
23 * James Goodwin <jamesg@filanet.com>
24 * with later contributions and ongoing maintenance from
25 * Ben Collins <bcollins@debian.org>,
26 * Stefan Richter <stefanr@s5r6.in-berlin.de>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/mod_devicetable.h>
33 #include <linux/device.h>
34 #include <linux/scatterlist.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/timer.h>
38 #include <scsi/scsi.h>
39 #include <scsi/scsi_cmnd.h>
40 #include <scsi/scsi_dbg.h>
41 #include <scsi/scsi_device.h>
42 #include <scsi/scsi_host.h>
44 #include "fw-transaction.h"
45 #include "fw-topology.h"
46 #include "fw-device.h"
48 /* I don't know why the SCSI stack doesn't define something like this... */
49 typedef void (*scsi_done_fn_t) (struct scsi_cmnd *);
51 static const char sbp2_driver_name[] = "sbp2";
55 struct fw_address_handler address_handler;
56 struct list_head orb_list;
57 u64 management_agent_address;
58 u64 command_block_agent_address;
62 /* We cache these addresses and only update them once we've
63 * logged in or reconnected to the sbp2 device. That way, any
64 * IO to the device will automatically fail and get retried if
65 * it happens in a window where the device is not ready to
66 * handle it (e.g. after a bus reset but before we reconnect). */
71 /* Timer for flushing ORBs. */
72 struct timer_list orb_timer;
75 struct delayed_work work;
76 struct Scsi_Host *scsi_host;
79 #define SBP2_MAX_SG_ELEMENT_LENGTH 0xf000
80 #define SBP2_MAX_SECTORS 255 /* Max sectors supported */
81 #define SBP2_ORB_TIMEOUT 2000 /* Timeout in ms */
83 #define SBP2_ORB_NULL 0x80000000
85 #define SBP2_DIRECTION_TO_MEDIA 0x0
86 #define SBP2_DIRECTION_FROM_MEDIA 0x1
88 /* Unit directory keys */
89 #define SBP2_COMMAND_SET_SPECIFIER 0x38
90 #define SBP2_COMMAND_SET 0x39
91 #define SBP2_COMMAND_SET_REVISION 0x3b
92 #define SBP2_FIRMWARE_REVISION 0x3c
94 /* Flags for detected oddities and brokeness */
95 #define SBP2_WORKAROUND_128K_MAX_TRANS 0x1
96 #define SBP2_WORKAROUND_INQUIRY_36 0x2
97 #define SBP2_WORKAROUND_MODE_SENSE_8 0x4
98 #define SBP2_WORKAROUND_FIX_CAPACITY 0x8
99 #define SBP2_WORKAROUND_OVERRIDE 0x100
101 /* Management orb opcodes */
102 #define SBP2_LOGIN_REQUEST 0x0
103 #define SBP2_QUERY_LOGINS_REQUEST 0x1
104 #define SBP2_RECONNECT_REQUEST 0x3
105 #define SBP2_SET_PASSWORD_REQUEST 0x4
106 #define SBP2_LOGOUT_REQUEST 0x7
107 #define SBP2_ABORT_TASK_REQUEST 0xb
108 #define SBP2_ABORT_TASK_SET 0xc
109 #define SBP2_LOGICAL_UNIT_RESET 0xe
110 #define SBP2_TARGET_RESET_REQUEST 0xf
112 /* Offsets for command block agent registers */
113 #define SBP2_AGENT_STATE 0x00
114 #define SBP2_AGENT_RESET 0x04
115 #define SBP2_ORB_POINTER 0x08
116 #define SBP2_DOORBELL 0x10
117 #define SBP2_UNSOLICITED_STATUS_ENABLE 0x14
119 /* Status write response codes */
120 #define SBP2_STATUS_REQUEST_COMPLETE 0x0
121 #define SBP2_STATUS_TRANSPORT_FAILURE 0x1
122 #define SBP2_STATUS_ILLEGAL_REQUEST 0x2
123 #define SBP2_STATUS_VENDOR_DEPENDENT 0x3
125 #define status_get_orb_high(v) ((v).status & 0xffff)
126 #define status_get_sbp_status(v) (((v).status >> 16) & 0xff)
127 #define status_get_len(v) (((v).status >> 24) & 0x07)
128 #define status_get_dead(v) (((v).status >> 27) & 0x01)
129 #define status_get_response(v) (((v).status >> 28) & 0x03)
130 #define status_get_source(v) (((v).status >> 30) & 0x03)
131 #define status_get_orb_low(v) ((v).orb_low)
132 #define status_get_data(v) ((v).data)
140 struct sbp2_pointer {
146 struct fw_transaction t;
147 dma_addr_t request_bus;
149 struct sbp2_pointer pointer;
150 void (*callback) (struct sbp2_orb * orb, struct sbp2_status * status);
151 struct list_head link;
154 #define management_orb_lun(v) ((v))
155 #define management_orb_function(v) ((v) << 16)
156 #define management_orb_reconnect(v) ((v) << 20)
157 #define management_orb_exclusive ((1) << 28)
158 #define management_orb_request_format(v) ((v) << 29)
159 #define management_orb_notify ((1) << 31)
161 #define management_orb_response_length(v) ((v))
162 #define management_orb_password_length(v) ((v) << 16)
164 struct sbp2_management_orb {
165 struct sbp2_orb base;
167 struct sbp2_pointer password;
168 struct sbp2_pointer response;
171 struct sbp2_pointer status_fifo;
174 dma_addr_t response_bus;
175 struct completion done;
176 struct sbp2_status status;
179 #define login_response_get_login_id(v) ((v).misc & 0xffff)
180 #define login_response_get_length(v) (((v).misc >> 16) & 0xffff)
182 struct sbp2_login_response {
184 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,
244 /* There are iPods (2nd gen, 3rd gen) with model_id == 0, but
245 * these iPods do not feature the read_capacity bug according
246 * to one report. Read_capacity behaviour as well as model_id
247 * could change due to Apple-supplied firmware updates though. */
248 /* iPod 4th generation. */ {
249 .firmware_revision = 0x0a2700,
251 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
254 .firmware_revision = 0x0a2700,
256 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
259 .firmware_revision = 0x0a2700,
261 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
266 sbp2_status_write(struct fw_card *card, struct fw_request *request,
267 int tcode, int destination, int source,
268 int generation, int speed,
269 unsigned long long offset,
270 void *payload, size_t length, void *callback_data)
272 struct sbp2_device *sd = callback_data;
273 struct sbp2_orb *orb;
274 struct sbp2_status status;
278 if (tcode != TCODE_WRITE_BLOCK_REQUEST ||
279 length == 0 || length > sizeof status) {
280 fw_send_response(card, request, RCODE_TYPE_ERROR);
284 header_size = min(length, 2 * sizeof(u32));
285 fw_memcpy_from_be32(&status, payload, header_size);
286 if (length > header_size)
287 memcpy(status.data, payload + 8, length - header_size);
288 if (status_get_source(status) == 2 || status_get_source(status) == 3) {
289 fw_notify("non-orb related status write, not handled\n");
290 fw_send_response(card, request, RCODE_COMPLETE);
294 /* Lookup the orb corresponding to this status write. */
295 spin_lock_irqsave(&card->lock, flags);
296 list_for_each_entry(orb, &sd->orb_list, link) {
297 if (status_get_orb_high(status) == 0 &&
298 status_get_orb_low(status) == orb->request_bus) {
299 list_del(&orb->link);
303 spin_unlock_irqrestore(&card->lock, flags);
305 if (&orb->link != &sd->orb_list)
306 orb->callback(orb, &status);
308 fw_error("status write for unknown orb\n");
310 fw_send_response(card, request, RCODE_COMPLETE);
314 complete_transaction(struct fw_card *card, int rcode,
315 void *payload, size_t length, void *data)
317 struct sbp2_orb *orb = data;
321 if (rcode != RCODE_COMPLETE) {
322 spin_lock_irqsave(&card->lock, flags);
323 list_del(&orb->link);
324 spin_unlock_irqrestore(&card->lock, flags);
325 orb->callback(orb, NULL);
330 sbp2_send_orb(struct sbp2_orb *orb, struct fw_unit *unit,
331 int node_id, int generation, u64 offset)
333 struct fw_device *device = fw_device(unit->device.parent);
334 struct sbp2_device *sd = unit->device.driver_data;
337 orb->pointer.high = 0;
338 orb->pointer.low = orb->request_bus;
339 fw_memcpy_to_be32(&orb->pointer, &orb->pointer, sizeof orb->pointer);
341 spin_lock_irqsave(&device->card->lock, flags);
342 list_add_tail(&orb->link, &sd->orb_list);
343 spin_unlock_irqrestore(&device->card->lock, flags);
345 mod_timer(&sd->orb_timer,
346 jiffies + DIV_ROUND_UP(SBP2_ORB_TIMEOUT * HZ, 1000));
348 fw_send_request(device->card, &orb->t, TCODE_WRITE_BLOCK_REQUEST,
350 device->node->max_speed, offset,
351 &orb->pointer, sizeof orb->pointer,
352 complete_transaction, orb);
355 static void sbp2_cancel_orbs(struct fw_unit *unit)
357 struct fw_device *device = fw_device(unit->device.parent);
358 struct sbp2_device *sd = unit->device.driver_data;
359 struct sbp2_orb *orb, *next;
360 struct list_head list;
363 INIT_LIST_HEAD(&list);
364 spin_lock_irqsave(&device->card->lock, flags);
365 list_splice_init(&sd->orb_list, &list);
366 spin_unlock_irqrestore(&device->card->lock, flags);
368 list_for_each_entry_safe(orb, next, &list, link) {
369 if (fw_cancel_transaction(device->card, &orb->t) == 0)
372 orb->rcode = RCODE_CANCELLED;
373 orb->callback(orb, NULL);
377 static void orb_timer_callback(unsigned long data)
379 struct sbp2_device *sd = (struct sbp2_device *)data;
381 sbp2_cancel_orbs(sd->unit);
385 complete_management_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
387 struct sbp2_management_orb *orb =
388 (struct sbp2_management_orb *)base_orb;
391 memcpy(&orb->status, status, sizeof *status);
392 complete(&orb->done);
396 sbp2_send_management_orb(struct fw_unit *unit, int node_id, int generation,
397 int function, int lun, void *response)
399 struct fw_device *device = fw_device(unit->device.parent);
400 struct sbp2_device *sd = unit->device.driver_data;
401 struct sbp2_management_orb *orb;
402 int retval = -ENOMEM;
404 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
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;
436 /* FIXME: Yeah, ok this isn't elegant, we hardwire exclusive
437 * login and 1 second reconnect time. The reconnect setting
438 * is probably fine, but the exclusive login should be an
440 if (function == SBP2_LOGIN_REQUEST) {
442 management_orb_exclusive |
443 management_orb_reconnect(0);
446 fw_memcpy_to_be32(&orb->request, &orb->request, sizeof orb->request);
448 init_completion(&orb->done);
449 orb->base.callback = complete_management_orb;
450 sbp2_send_orb(&orb->base, unit,
451 node_id, generation, sd->management_agent_address);
453 wait_for_completion(&orb->done);
456 if (orb->base.rcode != RCODE_COMPLETE) {
457 fw_error("management write failed, rcode 0x%02x\n",
462 if (orb->base.rcode == RCODE_CANCELLED) {
463 fw_error("orb reply timed out, rcode=0x%02x\n",
468 if (status_get_response(orb->status) != 0 ||
469 status_get_sbp_status(orb->status) != 0) {
470 fw_error("error status: %d:%d\n",
471 status_get_response(orb->status),
472 status_get_sbp_status(orb->status));
478 dma_unmap_single(device->card->device, orb->base.request_bus,
479 sizeof orb->request, DMA_TO_DEVICE);
480 dma_unmap_single(device->card->device, orb->response_bus,
481 sizeof orb->response, DMA_FROM_DEVICE);
484 fw_memcpy_from_be32(response,
485 orb->response, sizeof orb->response);
492 complete_agent_reset_write(struct fw_card *card, int rcode,
493 void *payload, size_t length, void *data)
495 struct fw_transaction *t = data;
500 static int sbp2_agent_reset(struct fw_unit *unit)
502 struct fw_device *device = fw_device(unit->device.parent);
503 struct sbp2_device *sd = unit->device.driver_data;
504 struct fw_transaction *t;
507 t = kzalloc(sizeof *t, GFP_ATOMIC);
511 fw_send_request(device->card, t, TCODE_WRITE_QUADLET_REQUEST,
512 sd->node_id, sd->generation, SCODE_400,
513 sd->command_block_agent_address + SBP2_AGENT_RESET,
514 &zero, sizeof zero, complete_agent_reset_write, t);
519 static int add_scsi_devices(struct fw_unit *unit);
520 static void remove_scsi_devices(struct fw_unit *unit);
521 static void sbp2_reconnect(struct work_struct *work);
523 static void sbp2_login(struct work_struct *work)
525 struct sbp2_device *sd =
526 container_of(work, struct sbp2_device, work.work);
527 struct fw_unit *unit = sd->unit;
528 struct fw_device *device = fw_device(unit->device.parent);
529 struct sbp2_login_response response;
530 int generation, node_id, local_node_id, lun, retval;
532 /* FIXME: Make this work for multi-lun devices. */
535 generation = device->card->generation;
536 node_id = device->node->node_id;
537 local_node_id = device->card->local_node->node_id;
539 if (sbp2_send_management_orb(unit, node_id, generation,
540 SBP2_LOGIN_REQUEST, lun, &response) < 0) {
541 if (sd->retries++ < 5) {
542 schedule_delayed_work(&sd->work, DIV_ROUND_UP(HZ, 5));
544 fw_error("failed to login to %s\n",
545 unit->device.bus_id);
546 remove_scsi_devices(unit);
551 sd->generation = generation;
552 sd->node_id = node_id;
553 sd->address_high = local_node_id << 16;
555 /* Get command block agent offset and login id. */
556 sd->command_block_agent_address =
557 ((u64) (response.command_block_agent.high & 0xffff) << 32) |
558 response.command_block_agent.low;
559 sd->login_id = login_response_get_login_id(response);
561 fw_notify("logged in to sbp2 unit %s (%d retries)\n",
562 unit->device.bus_id, sd->retries);
563 fw_notify(" - management_agent_address: 0x%012llx\n",
564 (unsigned long long) sd->management_agent_address);
565 fw_notify(" - command_block_agent_address: 0x%012llx\n",
566 (unsigned long long) sd->command_block_agent_address);
567 fw_notify(" - status write address: 0x%012llx\n",
568 (unsigned long long) sd->address_handler.offset);
571 /* FIXME: The linux1394 sbp2 does this last step. */
572 sbp2_set_busy_timeout(scsi_id);
575 PREPARE_DELAYED_WORK(&sd->work, sbp2_reconnect);
576 sbp2_agent_reset(unit);
578 retval = add_scsi_devices(unit);
580 sbp2_send_management_orb(unit, sd->node_id, sd->generation,
581 SBP2_LOGOUT_REQUEST, sd->login_id,
583 /* Set this back to sbp2_login so we fall back and
584 * retry login on bus reset. */
585 PREPARE_DELAYED_WORK(&sd->work, sbp2_login);
589 static int sbp2_probe(struct device *dev)
591 struct fw_unit *unit = fw_unit(dev);
592 struct fw_device *device = fw_device(unit->device.parent);
593 struct sbp2_device *sd;
594 struct fw_csr_iterator ci;
596 u32 model, firmware_revision;
598 sd = kzalloc(sizeof *sd, GFP_KERNEL);
602 unit->device.driver_data = sd;
604 INIT_LIST_HEAD(&sd->orb_list);
605 setup_timer(&sd->orb_timer, orb_timer_callback, (unsigned long)sd);
607 sd->address_handler.length = 0x100;
608 sd->address_handler.address_callback = sbp2_status_write;
609 sd->address_handler.callback_data = sd;
611 if (fw_core_add_address_handler(&sd->address_handler,
612 &fw_high_memory_region) < 0) {
617 if (fw_device_enable_phys_dma(device) < 0) {
618 fw_core_remove_address_handler(&sd->address_handler);
623 /* Scan unit directory to get management agent address,
624 * firmware revison and model. Initialize firmware_revision
625 * and model to values that wont match anything in our table. */
626 firmware_revision = 0xff000000;
628 fw_csr_iterator_init(&ci, unit->directory);
629 while (fw_csr_iterator_next(&ci, &key, &value)) {
631 case CSR_DEPENDENT_INFO | CSR_OFFSET:
632 sd->management_agent_address =
633 0xfffff0000000ULL + 4 * value;
635 case SBP2_FIRMWARE_REVISION:
636 firmware_revision = value;
644 for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
645 if (sbp2_workarounds_table[i].firmware_revision !=
646 (firmware_revision & 0xffffff00))
648 if (sbp2_workarounds_table[i].model != model &&
649 sbp2_workarounds_table[i].model != ~0)
651 sd->workarounds |= sbp2_workarounds_table[i].workarounds;
656 fw_notify("Workarounds for node %s: 0x%x "
657 "(firmware_revision 0x%06x, model_id 0x%06x)\n",
659 sd->workarounds, firmware_revision, model);
661 /* We schedule work to do the login so we can easily
662 * reschedule retries. */
663 INIT_DELAYED_WORK(&sd->work, sbp2_login);
664 schedule_delayed_work(&sd->work, 0);
669 static int sbp2_remove(struct device *dev)
671 struct fw_unit *unit = fw_unit(dev);
672 struct sbp2_device *sd = unit->device.driver_data;
674 sbp2_send_management_orb(unit, sd->node_id, sd->generation,
675 SBP2_LOGOUT_REQUEST, sd->login_id, NULL);
677 remove_scsi_devices(unit);
678 del_timer_sync(&sd->orb_timer);
680 fw_core_remove_address_handler(&sd->address_handler);
683 fw_notify("removed sbp2 unit %s\n", dev->bus_id);
688 static void sbp2_reconnect(struct work_struct *work)
690 struct sbp2_device *sd =
691 container_of(work, struct sbp2_device, work.work);
692 struct fw_unit *unit = sd->unit;
693 struct fw_device *device = fw_device(unit->device.parent);
694 int generation, node_id, local_node_id;
696 generation = device->card->generation;
697 node_id = device->node->node_id;
698 local_node_id = device->card->local_node->node_id;
700 if (sbp2_send_management_orb(unit, node_id, generation,
701 SBP2_RECONNECT_REQUEST,
702 sd->login_id, NULL) < 0) {
703 if (sd->retries++ >= 5) {
704 fw_error("failed to reconnect to %s\n",
705 unit->device.bus_id);
706 /* Fall back and try to log in again. */
708 PREPARE_DELAYED_WORK(&sd->work, sbp2_login);
710 schedule_delayed_work(&sd->work, DIV_ROUND_UP(HZ, 5));
714 sd->generation = generation;
715 sd->node_id = node_id;
716 sd->address_high = local_node_id << 16;
718 fw_notify("reconnected to unit %s (%d retries)\n",
719 unit->device.bus_id, sd->retries);
720 sbp2_agent_reset(unit);
721 sbp2_cancel_orbs(unit);
724 static void sbp2_update(struct fw_unit *unit)
726 struct fw_device *device = fw_device(unit->device.parent);
727 struct sbp2_device *sd = unit->device.driver_data;
730 fw_device_enable_phys_dma(device);
731 schedule_delayed_work(&sd->work, 0);
734 #define SBP2_UNIT_SPEC_ID_ENTRY 0x0000609e
735 #define SBP2_SW_VERSION_ENTRY 0x00010483
737 static const struct fw_device_id sbp2_id_table[] = {
739 .match_flags = FW_MATCH_SPECIFIER_ID | FW_MATCH_VERSION,
740 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY,
741 .version = SBP2_SW_VERSION_ENTRY,
746 static struct fw_driver sbp2_driver = {
748 .owner = THIS_MODULE,
749 .name = sbp2_driver_name,
752 .remove = sbp2_remove,
754 .update = sbp2_update,
755 .id_table = sbp2_id_table,
758 static unsigned int sbp2_status_to_sense_data(u8 * sbp2_status, u8 * sense_data)
760 sense_data[0] = 0x70;
762 sense_data[2] = sbp2_status[1];
763 sense_data[3] = sbp2_status[4];
764 sense_data[4] = sbp2_status[5];
765 sense_data[5] = sbp2_status[6];
766 sense_data[6] = sbp2_status[7];
768 sense_data[8] = sbp2_status[8];
769 sense_data[9] = sbp2_status[9];
770 sense_data[10] = sbp2_status[10];
771 sense_data[11] = sbp2_status[11];
772 sense_data[12] = sbp2_status[2];
773 sense_data[13] = sbp2_status[3];
774 sense_data[14] = sbp2_status[12];
775 sense_data[15] = sbp2_status[13];
777 switch (sbp2_status[0] & 0x3f) {
781 case SAM_STAT_CHECK_CONDITION:
782 /* return CHECK_CONDITION << 1 | DID_OK << 16; */
788 case SAM_STAT_CONDITION_MET:
789 case SAM_STAT_RESERVATION_CONFLICT:
790 case SAM_STAT_COMMAND_TERMINATED:
797 complete_command_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
799 struct sbp2_command_orb *orb = (struct sbp2_command_orb *)base_orb;
800 struct fw_unit *unit = orb->unit;
801 struct fw_device *device = fw_device(unit->device.parent);
802 struct scatterlist *sg;
805 if (status != NULL) {
806 if (status_get_dead(*status)) {
807 fw_notify("agent died, issuing agent reset\n");
808 sbp2_agent_reset(unit);
811 switch (status_get_response(*status)) {
812 case SBP2_STATUS_REQUEST_COMPLETE:
815 case SBP2_STATUS_TRANSPORT_FAILURE:
816 result = DID_BUS_BUSY;
818 case SBP2_STATUS_ILLEGAL_REQUEST:
819 case SBP2_STATUS_VENDOR_DEPENDENT:
825 if (result == DID_OK && status_get_len(*status) > 1)
826 result = sbp2_status_to_sense_data(status_get_data(*status),
827 orb->cmd->sense_buffer);
829 /* If the orb completes with status == NULL, something
830 * went wrong, typically a bus reset happened mid-orb
831 * or when sending the write (less likely). */
832 result = DID_BUS_BUSY;
835 dma_unmap_single(device->card->device, orb->base.request_bus,
836 sizeof orb->request, DMA_TO_DEVICE);
838 if (orb->cmd->use_sg > 0) {
839 sg = (struct scatterlist *)orb->cmd->request_buffer;
840 dma_unmap_sg(device->card->device, sg, orb->cmd->use_sg,
841 orb->cmd->sc_data_direction);
844 if (orb->page_table_bus != 0)
845 dma_unmap_single(device->card->device, orb->page_table_bus,
846 sizeof orb->page_table_bus, DMA_TO_DEVICE);
848 if (orb->request_buffer_bus != 0)
849 dma_unmap_single(device->card->device, orb->request_buffer_bus,
850 sizeof orb->request_buffer_bus,
853 orb->cmd->result = result << 16;
859 static void sbp2_command_orb_map_scatterlist(struct sbp2_command_orb *orb)
861 struct fw_unit *unit =
862 (struct fw_unit *)orb->cmd->device->host->hostdata[0];
863 struct fw_device *device = fw_device(unit->device.parent);
864 struct sbp2_device *sd = unit->device.driver_data;
865 struct scatterlist *sg;
866 int sg_len, l, i, j, count;
870 sg = (struct scatterlist *)orb->cmd->request_buffer;
871 count = dma_map_sg(device->card->device, sg, orb->cmd->use_sg,
872 orb->cmd->sc_data_direction);
874 /* Handle the special case where there is only one element in
875 * the scatter list by converting it to an immediate block
876 * request. This is also a workaround for broken devices such
877 * as the second generation iPod which doesn't support page
879 if (count == 1 && sg_dma_len(sg) < SBP2_MAX_SG_ELEMENT_LENGTH) {
880 orb->request.data_descriptor.high = sd->address_high;
881 orb->request.data_descriptor.low = sg_dma_address(sg);
883 command_orb_data_size(sg_dma_len(sg));
887 /* Convert the scatterlist to an sbp2 page table. If any
888 * scatterlist entries are too big for sbp2 we split the as we go. */
889 for (i = 0, j = 0; i < count; i++) {
890 sg_len = sg_dma_len(sg + i);
891 sg_addr = sg_dma_address(sg + i);
893 l = min(sg_len, SBP2_MAX_SG_ELEMENT_LENGTH);
894 orb->page_table[j].low = sg_addr;
895 orb->page_table[j].high = (l << 16);
902 size = sizeof orb->page_table[0] * j;
904 /* The data_descriptor pointer is the one case where we need
905 * to fill in the node ID part of the address. All other
906 * pointers assume that the data referenced reside on the
907 * initiator (i.e. us), but data_descriptor can refer to data
908 * on other nodes so we need to put our ID in descriptor.high. */
910 orb->page_table_bus =
911 dma_map_single(device->card->device, orb->page_table,
912 size, DMA_TO_DEVICE);
913 orb->request.data_descriptor.high = sd->address_high;
914 orb->request.data_descriptor.low = orb->page_table_bus;
916 command_orb_page_table_present |
917 command_orb_data_size(j);
919 fw_memcpy_to_be32(orb->page_table, orb->page_table, size);
922 static void sbp2_command_orb_map_buffer(struct sbp2_command_orb *orb)
924 struct fw_unit *unit =
925 (struct fw_unit *)orb->cmd->device->host->hostdata[0];
926 struct fw_device *device = fw_device(unit->device.parent);
927 struct sbp2_device *sd = unit->device.driver_data;
929 /* As for map_scatterlist, we need to fill in the high bits of
930 * the data_descriptor pointer. */
932 orb->request_buffer_bus =
933 dma_map_single(device->card->device,
934 orb->cmd->request_buffer,
935 orb->cmd->request_bufflen,
936 orb->cmd->sc_data_direction);
937 orb->request.data_descriptor.high = sd->address_high;
938 orb->request.data_descriptor.low = orb->request_buffer_bus;
940 command_orb_data_size(orb->cmd->request_bufflen);
943 /* SCSI stack integration */
945 static int sbp2_scsi_queuecommand(struct scsi_cmnd *cmd, scsi_done_fn_t done)
947 struct fw_unit *unit = (struct fw_unit *)cmd->device->host->hostdata[0];
948 struct fw_device *device = fw_device(unit->device.parent);
949 struct sbp2_device *sd = unit->device.driver_data;
950 struct sbp2_command_orb *orb;
952 /* Bidirectional commands are not yet implemented, and unknown
953 * transfer direction not handled. */
954 if (cmd->sc_data_direction == DMA_BIDIRECTIONAL) {
955 fw_error("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
959 orb = kzalloc(sizeof *orb, GFP_ATOMIC);
961 fw_notify("failed to alloc orb\n");
965 orb->base.request_bus =
966 dma_map_single(device->card->device, &orb->request,
967 sizeof orb->request, DMA_TO_DEVICE);
968 if (dma_mapping_error(orb->base.request_bus))
975 orb->request.next.high = SBP2_ORB_NULL;
976 orb->request.next.low = 0x0;
977 /* At speed 100 we can do 512 bytes per packet, at speed 200,
978 * 1024 bytes per packet etc. The SBP-2 max_payload field
979 * specifies the max payload size as 2 ^ (max_payload + 2), so
980 * if we set this to max_speed + 7, we get the right value. */
982 command_orb_max_payload(device->node->max_speed + 7) |
983 command_orb_speed(device->node->max_speed) |
986 if (cmd->sc_data_direction == DMA_FROM_DEVICE)
988 command_orb_direction(SBP2_DIRECTION_FROM_MEDIA);
989 else if (cmd->sc_data_direction == DMA_TO_DEVICE)
991 command_orb_direction(SBP2_DIRECTION_TO_MEDIA);
994 sbp2_command_orb_map_scatterlist(orb);
995 } else if (cmd->request_bufflen > SBP2_MAX_SG_ELEMENT_LENGTH) {
996 /* FIXME: Need to split this into a sg list... but
997 * could we get the scsi or blk layer to do that by
998 * reporting our max supported block size? */
999 fw_error("command > 64k\n");
1001 } else if (cmd->request_bufflen > 0) {
1002 sbp2_command_orb_map_buffer(orb);
1005 fw_memcpy_to_be32(&orb->request, &orb->request, sizeof orb->request);
1007 memset(orb->request.command_block,
1008 0, sizeof orb->request.command_block);
1009 memcpy(orb->request.command_block, cmd->cmnd, COMMAND_SIZE(*cmd->cmnd));
1011 orb->base.callback = complete_command_orb;
1013 sbp2_send_orb(&orb->base, unit, sd->node_id, sd->generation,
1014 sd->command_block_agent_address + SBP2_ORB_POINTER);
1019 dma_unmap_single(device->card->device, orb->base.request_bus,
1020 sizeof orb->request, DMA_TO_DEVICE);
1024 cmd->result = DID_ERROR << 16;
1029 static int sbp2_scsi_slave_alloc(struct scsi_device *sdev)
1031 struct fw_unit *unit = (struct fw_unit *)sdev->host->hostdata[0];
1032 struct sbp2_device *sd = unit->device.driver_data;
1034 sdev->allow_restart = 1;
1036 if (sd->workarounds & SBP2_WORKAROUND_INQUIRY_36)
1037 sdev->inquiry_len = 36;
1041 static int sbp2_scsi_slave_configure(struct scsi_device *sdev)
1043 struct fw_unit *unit = (struct fw_unit *)sdev->host->hostdata[0];
1044 struct sbp2_device *sd = unit->device.driver_data;
1046 sdev->use_10_for_rw = 1;
1048 if (sdev->type == TYPE_ROM)
1049 sdev->use_10_for_ms = 1;
1050 if (sdev->type == TYPE_DISK &&
1051 sd->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
1052 sdev->skip_ms_page_8 = 1;
1053 if (sd->workarounds & SBP2_WORKAROUND_FIX_CAPACITY) {
1054 fw_notify("setting fix_capacity for %s\n", unit->device.bus_id);
1055 sdev->fix_capacity = 1;
1062 * Called by scsi stack when something has really gone wrong. Usually
1063 * called when a command has timed-out for some reason.
1065 static int sbp2_scsi_abort(struct scsi_cmnd *cmd)
1067 struct fw_unit *unit = (struct fw_unit *)cmd->device->host->hostdata[0];
1069 fw_notify("sbp2_scsi_abort\n");
1071 sbp2_cancel_orbs(unit);
1076 static struct scsi_host_template scsi_driver_template = {
1077 .module = THIS_MODULE,
1078 .name = "SBP-2 IEEE-1394",
1079 .proc_name = (char *)sbp2_driver_name,
1080 .queuecommand = sbp2_scsi_queuecommand,
1081 .slave_alloc = sbp2_scsi_slave_alloc,
1082 .slave_configure = sbp2_scsi_slave_configure,
1083 .eh_abort_handler = sbp2_scsi_abort,
1085 .sg_tablesize = SG_ALL,
1086 .use_clustering = ENABLE_CLUSTERING,
1091 static int add_scsi_devices(struct fw_unit *unit)
1093 struct sbp2_device *sd = unit->device.driver_data;
1096 if (sd->scsi_host != NULL)
1099 sd->scsi_host = scsi_host_alloc(&scsi_driver_template,
1100 sizeof(unsigned long));
1101 if (sd->scsi_host == NULL) {
1102 fw_error("failed to register scsi host\n");
1106 sd->scsi_host->hostdata[0] = (unsigned long)unit;
1107 retval = scsi_add_host(sd->scsi_host, &unit->device);
1109 fw_error("failed to add scsi host\n");
1110 scsi_host_put(sd->scsi_host);
1114 /* FIXME: Loop over luns here. */
1116 retval = scsi_add_device(sd->scsi_host, 0, 0, lun);
1118 fw_error("failed to add scsi device\n");
1119 scsi_remove_host(sd->scsi_host);
1120 scsi_host_put(sd->scsi_host);
1127 static void remove_scsi_devices(struct fw_unit *unit)
1129 struct sbp2_device *sd = unit->device.driver_data;
1131 if (sd->scsi_host != NULL) {
1132 scsi_remove_host(sd->scsi_host);
1133 scsi_host_put(sd->scsi_host);
1135 sd->scsi_host = NULL;
1138 MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
1139 MODULE_DESCRIPTION("SCSI over IEEE1394");
1140 MODULE_LICENSE("GPL");
1141 MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
1143 static int __init sbp2_init(void)
1145 return driver_register(&sbp2_driver.driver);
1148 static void __exit sbp2_cleanup(void)
1150 driver_unregister(&sbp2_driver.driver);
1153 module_init(sbp2_init);
1154 module_exit(sbp2_cleanup);