aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/firewire/fw-sbp2.c
diff options
context:
space:
mode:
authorKristian Høgsberg <krh@redhat.com>2006-12-19 19:58:40 -0500
committerStefan Richter <stefanr@s5r6.in-berlin.de>2007-03-09 16:02:34 -0500
commit9ba136d0fe5a3dd33533b4a2a21156aa22f80ebe (patch)
tree917fec1447a82f7bfa4e9e89f8a33cc65e412626 /drivers/firewire/fw-sbp2.c
parented5689122f4cdb5cb8c6770ad1a2c8561b32d9b3 (diff)
firewire: Add SBP-2 protocol driver for storage devices.
Signed-off-by: Kristian Høgsberg <krh@redhat.com> Signed-off-by: Stefan Richter <stefanr@s5r6.in-berlin.de>
Diffstat (limited to 'drivers/firewire/fw-sbp2.c')
-rw-r--r--drivers/firewire/fw-sbp2.c1073
1 files changed, 1073 insertions, 0 deletions
diff --git a/drivers/firewire/fw-sbp2.c b/drivers/firewire/fw-sbp2.c
new file mode 100644
index 000000000000..2756e0c2f9cd
--- /dev/null
+++ b/drivers/firewire/fw-sbp2.c
@@ -0,0 +1,1073 @@
1/* -*- c-basic-offset: 8 -*-
2 * fw-sbp2.c -- SBP2 driver (SCSI over IEEE1394)
3 *
4 * Copyright (C) 2005-2006 Kristian Hoegsberg <krh@bitplanet.net>
5 *
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.
10 *
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.
15 *
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.
19 */
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/device.h>
24#include <linux/dma-mapping.h>
25
26#include <scsi/scsi.h>
27#include <scsi/scsi_cmnd.h>
28#include <scsi/scsi_dbg.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_host.h>
31
32#include "fw-transaction.h"
33#include "fw-topology.h"
34#include "fw-device.h"
35
36/* I don't know why the SCSI stack doesn't define something like this... */
37typedef void (*scsi_done_fn_t) (struct scsi_cmnd *);
38
39static const char sbp2_driver_name[] = "sbp2";
40
41struct sbp2_device {
42 struct fw_unit *unit;
43 struct fw_address_handler address_handler;
44 struct list_head orb_list;
45 u64 management_agent_address;
46 u64 command_block_agent_address;
47 u32 workarounds;
48 int login_id;
49
50 /* We cache these addresses and only update them once we've
51 * logged in or reconnected to the sbp2 device. That way, any
52 * IO to the device will automatically fail and get retried if
53 * it happens in a window where the device is not ready to
54 * handle it (e.g. after a bus reset but before we reconnect). */
55 int node_id;
56 int address_high;
57 int generation;
58
59 struct work_struct work;
60 struct Scsi_Host *scsi_host;
61};
62
63#define SBP2_MAX_SG_ELEMENT_LENGTH 0xf000
64#define SBP2_MAX_SECTORS 255 /* Max sectors supported */
65#define SBP2_MAX_CMDS 8 /* This should be safe */
66
67#define SBP2_ORB_NULL 0x80000000
68
69#define SBP2_DIRECTION_TO_MEDIA 0x0
70#define SBP2_DIRECTION_FROM_MEDIA 0x1
71
72/* Unit directory keys */
73#define SBP2_COMMAND_SET_SPECIFIER 0x38
74#define SBP2_COMMAND_SET 0x39
75#define SBP2_COMMAND_SET_REVISION 0x3b
76#define SBP2_FIRMWARE_REVISION 0x3c
77
78/* Flags for detected oddities and brokeness */
79#define SBP2_WORKAROUND_128K_MAX_TRANS 0x1
80#define SBP2_WORKAROUND_INQUIRY_36 0x2
81#define SBP2_WORKAROUND_MODE_SENSE_8 0x4
82#define SBP2_WORKAROUND_FIX_CAPACITY 0x8
83#define SBP2_WORKAROUND_OVERRIDE 0x100
84
85/* Management orb opcodes */
86#define SBP2_LOGIN_REQUEST 0x0
87#define SBP2_QUERY_LOGINS_REQUEST 0x1
88#define SBP2_RECONNECT_REQUEST 0x3
89#define SBP2_SET_PASSWORD_REQUEST 0x4
90#define SBP2_LOGOUT_REQUEST 0x7
91#define SBP2_ABORT_TASK_REQUEST 0xb
92#define SBP2_ABORT_TASK_SET 0xc
93#define SBP2_LOGICAL_UNIT_RESET 0xe
94#define SBP2_TARGET_RESET_REQUEST 0xf
95
96/* Offsets for command block agent registers */
97#define SBP2_AGENT_STATE 0x00
98#define SBP2_AGENT_RESET 0x04
99#define SBP2_ORB_POINTER 0x08
100#define SBP2_DOORBELL 0x10
101#define SBP2_UNSOLICITED_STATUS_ENABLE 0x14
102
103/* Status write response codes */
104#define SBP2_STATUS_REQUEST_COMPLETE 0x0
105#define SBP2_STATUS_TRANSPORT_FAILURE 0x1
106#define SBP2_STATUS_ILLEGAL_REQUEST 0x2
107#define SBP2_STATUS_VENDOR_DEPENDENT 0x3
108
109#define status_get_orb_high(v) ((v).status & 0xffff)
110#define status_get_sbp_status(v) (((v).status >> 16) & 0xff)
111#define status_get_len(v) (((v).status >> 24) & 0x07)
112#define status_get_dead(v) (((v).status >> 27) & 0x01)
113#define status_get_response(v) (((v).status >> 28) & 0x03)
114#define status_get_source(v) (((v).status >> 30) & 0x03)
115#define status_get_orb_low(v) ((v).orb_low)
116#define status_get_data(v) ((v).data)
117
118struct sbp2_status {
119 u32 status;
120 u32 orb_low;
121 u8 data[24];
122};
123
124struct sbp2_pointer {
125 u32 high;
126 u32 low;
127};
128
129struct sbp2_orb {
130 struct fw_transaction t;
131 dma_addr_t request_bus;
132 int rcode;
133 struct sbp2_pointer pointer;
134 void (*callback) (struct sbp2_orb * orb, struct sbp2_status * status);
135 struct list_head link;
136};
137
138#define management_orb_lun(v) ((v))
139#define management_orb_function(v) ((v) << 16)
140#define management_orb_reconnect(v) ((v) << 20)
141#define management_orb_exclusive ((1) << 28)
142#define management_orb_request_format(v) ((v) << 29)
143#define management_orb_notify ((1) << 31)
144
145#define management_orb_response_length(v) ((v))
146#define management_orb_password_length(v) ((v) << 16)
147
148struct sbp2_management_orb {
149 struct sbp2_orb base;
150 struct {
151 struct sbp2_pointer password;
152 struct sbp2_pointer response;
153 u32 misc;
154 u32 length;
155 struct sbp2_pointer status_fifo;
156 } request;
157 __be32 response[4];
158 dma_addr_t response_bus;
159 struct completion done;
160 struct sbp2_status status;
161};
162
163#define login_response_get_login_id(v) ((v).misc & 0xffff)
164#define login_response_get_length(v) (((v).misc >> 16) & 0xffff)
165
166struct sbp2_login_response {
167 u32 misc;
168 struct sbp2_pointer command_block_agent;
169 u32 reconnect_hold;
170};
171
172#define command_orb_data_size(v) ((v))
173#define command_orb_page_size(v) ((v) << 16)
174#define command_orb_page_table_present ((1) << 19)
175#define command_orb_max_payload(v) ((v) << 20)
176#define command_orb_speed(v) ((v) << 24)
177#define command_orb_direction(v) ((v) << 27)
178#define command_orb_request_format(v) ((v) << 29)
179#define command_orb_notify ((1) << 31)
180
181struct sbp2_command_orb {
182 struct sbp2_orb base;
183 struct {
184 struct sbp2_pointer next;
185 struct sbp2_pointer data_descriptor;
186 u32 misc;
187 u8 command_block[12];
188 } request;
189 struct scsi_cmnd *cmd;
190 scsi_done_fn_t done;
191 struct fw_unit *unit;
192
193 struct sbp2_pointer page_table[SG_ALL];
194 dma_addr_t page_table_bus;
195 dma_addr_t request_buffer_bus;
196};
197
198/*
199 * List of devices with known bugs.
200 *
201 * The firmware_revision field, masked with 0xffff00, is the best
202 * indicator for the type of bridge chip of a device. It yields a few
203 * false positives but this did not break correctly behaving devices
204 * so far. We use ~0 as a wildcard, since the 24 bit values we get
205 * from the config rom can never match that.
206 */
207static const struct {
208 u32 firmware_revision;
209 u32 model;
210 unsigned workarounds;
211} sbp2_workarounds_table[] = {
212 /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
213 .firmware_revision = 0x002800,
214 .model = 0x001010,
215 .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
216 SBP2_WORKAROUND_MODE_SENSE_8,
217 },
218 /* Initio bridges, actually only needed for some older ones */ {
219 .firmware_revision = 0x000200,
220 .model = ~0,
221 .workarounds = SBP2_WORKAROUND_INQUIRY_36,
222 },
223 /* Symbios bridge */ {
224 .firmware_revision = 0xa0b800,
225 .model = ~0,
226 .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
227 },
228 /* There are iPods (2nd gen, 3rd gen) with model_id == 0, but
229 * these iPods do not feature the read_capacity bug according
230 * to one report. Read_capacity behaviour as well as model_id
231 * could change due to Apple-supplied firmware updates though. */
232 /* iPod 4th generation. */ {
233 .firmware_revision = 0x0a2700,
234 .model = 0x000021,
235 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
236 },
237 /* iPod mini */ {
238 .firmware_revision = 0x0a2700,
239 .model = 0x000023,
240 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
241 },
242 /* iPod Photo */ {
243 .firmware_revision = 0x0a2700,
244 .model = 0x00007e,
245 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
246 }
247};
248
249static void
250sbp2_status_write(struct fw_card *card, struct fw_request *request,
251 int tcode, int destination, int source,
252 int generation, int speed,
253 unsigned long long offset,
254 void *payload, size_t length, void *callback_data)
255{
256 struct sbp2_device *sd = callback_data;
257 struct sbp2_orb *orb;
258 struct sbp2_status status;
259 size_t header_size;
260 unsigned long flags;
261
262 if (tcode != TCODE_WRITE_BLOCK_REQUEST ||
263 length == 0 || length > sizeof status) {
264 fw_send_response(card, request, RCODE_TYPE_ERROR);
265 return;
266 }
267
268 header_size = min(length, 2 * sizeof(u32));
269 fw_memcpy_from_be32(&status, payload, header_size);
270 if (length > header_size)
271 memcpy(status.data, payload + 8, length - header_size);
272 if (status_get_source(status) == 2 || status_get_source(status) == 3) {
273 fw_notify("non-orb related status write, not handled\n");
274 fw_send_response(card, request, RCODE_COMPLETE);
275 return;
276 }
277
278 /* Lookup the orb corresponding to this status write. */
279 spin_lock_irqsave(&card->lock, flags);
280 list_for_each_entry(orb, &sd->orb_list, link) {
281 if (status_get_orb_high(status) == 0 &&
282 status_get_orb_low(status) == orb->request_bus) {
283 list_del(&orb->link);
284 break;
285 }
286 }
287 spin_unlock_irqrestore(&card->lock, flags);
288
289 if (&orb->link != &sd->orb_list)
290 orb->callback(orb, &status);
291 else
292 fw_error("status write for unknown orb\n");
293
294 fw_send_response(card, request, RCODE_COMPLETE);
295}
296
297static void
298complete_transaction(struct fw_card *card, int rcode,
299 void *payload, size_t length, void *data)
300{
301 struct sbp2_orb *orb = data;
302 unsigned long flags;
303
304 orb->rcode = rcode;
305 if (rcode != RCODE_COMPLETE) {
306 spin_lock_irqsave(&card->lock, flags);
307 list_del(&orb->link);
308 spin_unlock_irqrestore(&card->lock, flags);
309 orb->callback(orb, NULL);
310 }
311}
312
313static void
314sbp2_send_orb(struct sbp2_orb *orb, struct fw_unit *unit,
315 int node_id, int generation, u64 offset)
316{
317 struct fw_device *device = fw_device(unit->device.parent);
318 struct sbp2_device *sd = unit->device.driver_data;
319 unsigned long flags;
320
321 orb->pointer.high = 0;
322 orb->pointer.low = orb->request_bus;
323 fw_memcpy_to_be32(&orb->pointer, &orb->pointer, sizeof orb->pointer);
324
325 spin_lock_irqsave(&device->card->lock, flags);
326 list_add_tail(&orb->link, &sd->orb_list);
327 spin_unlock_irqrestore(&device->card->lock, flags);
328
329 fw_send_request(device->card, &orb->t, TCODE_WRITE_BLOCK_REQUEST,
330 node_id | LOCAL_BUS, generation,
331 device->node->max_speed, offset,
332 &orb->pointer, sizeof orb->pointer,
333 complete_transaction, orb);
334}
335
336static void sbp2_cancel_orbs(struct fw_unit *unit)
337{
338 struct fw_device *device = fw_device(unit->device.parent);
339 struct sbp2_device *sd = unit->device.driver_data;
340 struct sbp2_orb *orb, *next;
341 struct list_head list;
342 unsigned long flags;
343
344 INIT_LIST_HEAD(&list);
345 spin_lock_irqsave(&device->card->lock, flags);
346 list_splice_init(&sd->orb_list, &list);
347 spin_unlock_irqrestore(&device->card->lock, flags);
348
349 list_for_each_entry_safe(orb, next, &list, link) {
350 orb->rcode = RCODE_CANCELLED;
351 orb->callback(orb, NULL);
352 }
353}
354
355static void
356complete_management_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
357{
358 struct sbp2_management_orb *orb =
359 (struct sbp2_management_orb *)base_orb;
360
361 if (status)
362 memcpy(&orb->status, status, sizeof *status);
363 complete(&orb->done);
364}
365
366static int
367sbp2_send_management_orb(struct fw_unit *unit, int node_id, int generation,
368 int function, int lun, void *response)
369{
370 struct fw_device *device = fw_device(unit->device.parent);
371 struct sbp2_device *sd = unit->device.driver_data;
372 struct sbp2_management_orb *orb;
373 unsigned long timeout;
374 int retval = -ENOMEM;
375
376 orb = kzalloc(sizeof *orb, GFP_ATOMIC);
377 if (orb == NULL)
378 return -ENOMEM;
379
380 /* The sbp2 device is going to send a block read request to
381 * read out the request from host memory, so map it for
382 * dma. */
383 orb->base.request_bus =
384 dma_map_single(device->card->device, &orb->request,
385 sizeof orb->request, DMA_TO_DEVICE);
386 if (orb->base.request_bus == 0)
387 goto out;
388
389 orb->response_bus =
390 dma_map_single(device->card->device, &orb->response,
391 sizeof orb->response, DMA_FROM_DEVICE);
392 if (orb->response_bus == 0)
393 goto out;
394
395 orb->request.response.high = 0;
396 orb->request.response.low = orb->response_bus;
397
398 orb->request.misc =
399 management_orb_notify |
400 management_orb_function(function) |
401 management_orb_lun(lun);
402 orb->request.length =
403 management_orb_response_length(sizeof orb->response);
404
405 orb->request.status_fifo.high = sd->address_handler.offset >> 32;
406 orb->request.status_fifo.low = sd->address_handler.offset;
407
408 /* FIXME: Yeah, ok this isn't elegant, we hardwire exclusive
409 * login and 1 second reconnect time. The reconnect setting
410 * is probably fine, but the exclusive login should be an
411 * option. */
412 if (function == SBP2_LOGIN_REQUEST) {
413 orb->request.misc |=
414 management_orb_exclusive |
415 management_orb_reconnect(0);
416 }
417
418 fw_memcpy_to_be32(&orb->request, &orb->request, sizeof orb->request);
419
420 init_completion(&orb->done);
421 orb->base.callback = complete_management_orb;
422 sbp2_send_orb(&orb->base, unit,
423 node_id, generation, sd->management_agent_address);
424
425 timeout = wait_for_completion_timeout(&orb->done, 10 * HZ);
426
427 /* FIXME: Handle bus reset race here. */
428
429 retval = -EIO;
430 if (orb->base.rcode != RCODE_COMPLETE) {
431 fw_error("management write failed, rcode 0x%02x\n",
432 orb->base.rcode);
433 goto out;
434 }
435
436 if (timeout == 0) {
437 fw_error("orb reply timed out, rcode=0x%02x\n",
438 orb->base.rcode);
439 goto out;
440 }
441
442 if (status_get_response(orb->status) != 0 ||
443 status_get_sbp_status(orb->status) != 0) {
444 fw_error("error status: %d:%d\n",
445 status_get_response(orb->status),
446 status_get_sbp_status(orb->status));
447 goto out;
448 }
449
450 retval = 0;
451 out:
452 dma_unmap_single(device->card->device, orb->base.request_bus,
453 sizeof orb->request, DMA_TO_DEVICE);
454 dma_unmap_single(device->card->device, orb->response_bus,
455 sizeof orb->response, DMA_FROM_DEVICE);
456
457 if (response)
458 fw_memcpy_from_be32(response,
459 orb->response, sizeof orb->response);
460 kfree(orb);
461
462 return retval;
463}
464
465static void
466complete_agent_reset_write(struct fw_card *card, int rcode,
467 void *payload, size_t length, void *data)
468{
469 struct fw_transaction *t = data;
470
471 fw_notify("agent reset write rcode=%d\n", rcode);
472 kfree(t);
473}
474
475static int sbp2_agent_reset(struct fw_unit *unit)
476{
477 struct fw_device *device = fw_device(unit->device.parent);
478 struct sbp2_device *sd = unit->device.driver_data;
479 struct fw_transaction *t;
480 static u32 zero;
481
482 t = kzalloc(sizeof *t, GFP_ATOMIC);
483 if (t == NULL)
484 return -ENOMEM;
485
486 fw_send_request(device->card, t, TCODE_WRITE_QUADLET_REQUEST,
487 sd->node_id | LOCAL_BUS, sd->generation, SCODE_400,
488 sd->command_block_agent_address + SBP2_AGENT_RESET,
489 &zero, sizeof zero, complete_agent_reset_write, t);
490
491 return 0;
492}
493
494static int add_scsi_devices(struct fw_unit *unit);
495static void remove_scsi_devices(struct fw_unit *unit);
496
497static int sbp2_probe(struct device *dev)
498{
499 struct fw_unit *unit = fw_unit(dev);
500 struct fw_device *device = fw_device(unit->device.parent);
501 struct sbp2_device *sd;
502 struct fw_csr_iterator ci;
503 int i, key, value, lun, retval;
504 int node_id, generation, local_node_id;
505 struct sbp2_login_response response;
506 u32 model, firmware_revision;
507
508 sd = kzalloc(sizeof *sd, GFP_KERNEL);
509 if (sd == NULL)
510 return -ENOMEM;
511
512 unit->device.driver_data = sd;
513 sd->unit = unit;
514 INIT_LIST_HEAD(&sd->orb_list);
515
516 sd->address_handler.length = 0x100;
517 sd->address_handler.address_callback = sbp2_status_write;
518 sd->address_handler.callback_data = sd;
519
520 if (fw_core_add_address_handler(&sd->address_handler,
521 &fw_high_memory_region) < 0) {
522 kfree(sd);
523 return -EBUSY;
524 }
525
526 if (fw_device_enable_phys_dma(device) < 0) {
527 fw_core_remove_address_handler(&sd->address_handler);
528 kfree(sd);
529 return -EBUSY;
530 }
531
532 /* Scan unit directory to get management agent address,
533 * firmware revison and model. Initialize firmware_revision
534 * and model to values that wont match anything in our table. */
535 firmware_revision = 0xff000000;
536 model = 0xff000000;
537 fw_csr_iterator_init(&ci, unit->directory);
538 while (fw_csr_iterator_next(&ci, &key, &value)) {
539 switch (key) {
540 case CSR_DEPENDENT_INFO | CSR_OFFSET:
541 sd->management_agent_address =
542 0xfffff0000000ULL + 4 * value;
543 break;
544 case SBP2_FIRMWARE_REVISION:
545 firmware_revision = value;
546 break;
547 case CSR_MODEL:
548 model = value;
549 break;
550 }
551 }
552
553 for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
554 if (sbp2_workarounds_table[i].firmware_revision !=
555 (firmware_revision & 0xffffff00))
556 continue;
557 if (sbp2_workarounds_table[i].model != model &&
558 sbp2_workarounds_table[i].model != ~0)
559 continue;
560 sd->workarounds |= sbp2_workarounds_table[i].workarounds;
561 break;
562 }
563
564 if (sd->workarounds)
565 fw_notify("Workarounds for node %s: 0x%x "
566 "(firmware_revision 0x%06x, model_id 0x%06x)\n",
567 unit->device.bus_id,
568 sd->workarounds, firmware_revision, model);
569
570 /* FIXME: Make this work for multi-lun devices. */
571 lun = 0;
572
573 generation = device->card->generation;
574 node_id = device->node->node_id;
575 local_node_id = device->card->local_node->node_id;
576
577 /* FIXME: We should probably do this from a keventd callback
578 * and handle retries by rescheduling the work. */
579 if (sbp2_send_management_orb(unit, node_id, generation,
580 SBP2_LOGIN_REQUEST, lun, &response) < 0) {
581 fw_core_remove_address_handler(&sd->address_handler);
582 kfree(sd);
583 return -EBUSY;
584 }
585
586 sd->generation = generation;
587 sd->node_id = node_id;
588 sd->address_high = (LOCAL_BUS | local_node_id) << 16;
589
590 /* Get command block agent offset and login id. */
591 sd->command_block_agent_address =
592 ((u64) response.command_block_agent.high << 32) |
593 response.command_block_agent.low;
594 sd->login_id = login_response_get_login_id(response);
595
596 fw_notify("logged in to sbp2 unit %s\n", unit->device.bus_id);
597 fw_notify(" - management_agent_address: 0x%012llx\n",
598 (unsigned long long) sd->management_agent_address);
599 fw_notify(" - command_block_agent_address: 0x%012llx\n",
600 (unsigned long long) sd->command_block_agent_address);
601 fw_notify(" - status write address: 0x%012llx\n",
602 (unsigned long long) sd->address_handler.offset);
603
604#if 0
605 /* FIXME: The linux1394 sbp2 does this last step. */
606 sbp2_set_busy_timeout(scsi_id);
607#endif
608
609 sbp2_agent_reset(unit);
610
611 retval = add_scsi_devices(unit);
612 if (retval < 0) {
613 sbp2_send_management_orb(unit, sd->node_id, sd->generation,
614 SBP2_LOGOUT_REQUEST, sd->login_id,
615 NULL);
616 fw_core_remove_address_handler(&sd->address_handler);
617 kfree(sd);
618 return retval;
619 }
620
621 return 0;
622}
623
624static int sbp2_remove(struct device *dev)
625{
626 struct fw_unit *unit = fw_unit(dev);
627 struct sbp2_device *sd = unit->device.driver_data;
628
629 sbp2_send_management_orb(unit, sd->node_id, sd->generation,
630 SBP2_LOGOUT_REQUEST, sd->login_id, NULL);
631
632 remove_scsi_devices(unit);
633
634 fw_core_remove_address_handler(&sd->address_handler);
635 kfree(sd);
636
637 fw_notify("removed sbp2 unit %s\n", dev->bus_id);
638
639 return 0;
640}
641
642static void sbp2_reconnect(struct work_struct *work)
643{
644 struct sbp2_device *sd = container_of(work, struct sbp2_device, work);
645 struct fw_unit *unit = sd->unit;
646 struct fw_device *device = fw_device(unit->device.parent);
647 int generation, node_id, local_node_id;
648
649 fw_notify("in sbp2_reconnect, reconnecting to unit %s\n",
650 unit->device.bus_id);
651
652 generation = device->card->generation;
653 node_id = device->node->node_id;
654 local_node_id = device->card->local_node->node_id;
655
656 sbp2_send_management_orb(unit, node_id, generation,
657 SBP2_RECONNECT_REQUEST, sd->login_id, NULL);
658
659 /* FIXME: handle reconnect failures. */
660
661 sbp2_cancel_orbs(unit);
662
663 sd->generation = generation;
664 sd->node_id = node_id;
665 sd->address_high = (LOCAL_BUS | local_node_id) << 16;
666}
667
668static void sbp2_update(struct fw_unit *unit)
669{
670 struct fw_device *device = fw_device(unit->device.parent);
671 struct sbp2_device *sd = unit->device.driver_data;
672
673 fw_device_enable_phys_dma(device);
674
675 INIT_WORK(&sd->work, sbp2_reconnect);
676 schedule_work(&sd->work);
677}
678
679#define SBP2_UNIT_SPEC_ID_ENTRY 0x0000609e
680#define SBP2_SW_VERSION_ENTRY 0x00010483
681
682static struct fw_device_id sbp2_id_table[] = {
683 {
684 .match_flags = FW_MATCH_SPECIFIER_ID | FW_MATCH_VERSION,
685 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY,
686 .version = SBP2_SW_VERSION_ENTRY
687 },
688 { }
689};
690
691static struct fw_driver sbp2_driver = {
692 .driver = {
693 .owner = THIS_MODULE,
694 .name = sbp2_driver_name,
695 .bus = &fw_bus_type,
696 .probe = sbp2_probe,
697 .remove = sbp2_remove,
698 },
699 .update = sbp2_update,
700 .id_table = sbp2_id_table,
701};
702
703static unsigned int sbp2_status_to_sense_data(u8 * sbp2_status, u8 * sense_data)
704{
705 sense_data[0] = 0x70;
706 sense_data[1] = 0x0;
707 sense_data[2] = sbp2_status[1];
708 sense_data[3] = sbp2_status[4];
709 sense_data[4] = sbp2_status[5];
710 sense_data[5] = sbp2_status[6];
711 sense_data[6] = sbp2_status[7];
712 sense_data[7] = 10;
713 sense_data[8] = sbp2_status[8];
714 sense_data[9] = sbp2_status[9];
715 sense_data[10] = sbp2_status[10];
716 sense_data[11] = sbp2_status[11];
717 sense_data[12] = sbp2_status[2];
718 sense_data[13] = sbp2_status[3];
719 sense_data[14] = sbp2_status[12];
720 sense_data[15] = sbp2_status[13];
721
722 switch (sbp2_status[0] & 0x3f) {
723 case SAM_STAT_GOOD:
724 return DID_OK;
725
726 case SAM_STAT_CHECK_CONDITION:
727 /* return CHECK_CONDITION << 1 | DID_OK << 16; */
728 return DID_OK;
729
730 case SAM_STAT_BUSY:
731 return DID_BUS_BUSY;
732
733 case SAM_STAT_CONDITION_MET:
734 case SAM_STAT_RESERVATION_CONFLICT:
735 case SAM_STAT_COMMAND_TERMINATED:
736 default:
737 return DID_ERROR;
738 }
739}
740
741static void
742complete_command_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
743{
744 struct sbp2_command_orb *orb = (struct sbp2_command_orb *)base_orb;
745 struct fw_unit *unit = orb->unit;
746 struct fw_device *device = fw_device(unit->device.parent);
747 struct scatterlist *sg;
748 int result;
749
750 if (status != NULL) {
751 if (status_get_dead(*status)) {
752 fw_notify("agent died, issuing agent reset\n");
753 sbp2_agent_reset(unit);
754 }
755
756 switch (status_get_response(*status)) {
757 case SBP2_STATUS_REQUEST_COMPLETE:
758 result = DID_OK;
759 break;
760 case SBP2_STATUS_TRANSPORT_FAILURE:
761 result = DID_BUS_BUSY;
762 break;
763 case SBP2_STATUS_ILLEGAL_REQUEST:
764 case SBP2_STATUS_VENDOR_DEPENDENT:
765 default:
766 result = DID_ERROR;
767 break;
768 }
769
770 if (result == DID_OK && status_get_len(*status) > 1)
771 result = sbp2_status_to_sense_data(status_get_data(*status),
772 orb->cmd->sense_buffer);
773 } else {
774 /* If the orb completes with status == NULL, something
775 * went wrong, typically a bus reset happened mid-orb
776 * or when sending the write (less likely). */
777 fw_notify("no command orb status, rcode=%d\n",
778 orb->base.rcode);
779 result = DID_ERROR;
780 }
781
782 dma_unmap_single(device->card->device, orb->base.request_bus,
783 sizeof orb->request, DMA_TO_DEVICE);
784
785 if (orb->cmd->use_sg > 0) {
786 sg = (struct scatterlist *)orb->cmd->request_buffer;
787 dma_unmap_sg(device->card->device, sg, orb->cmd->use_sg,
788 orb->cmd->sc_data_direction);
789 }
790
791 if (orb->page_table_bus != 0)
792 dma_unmap_single(device->card->device, orb->page_table_bus,
793 sizeof orb->page_table_bus, DMA_TO_DEVICE);
794
795 if (orb->request_buffer_bus != 0)
796 dma_unmap_single(device->card->device, orb->request_buffer_bus,
797 sizeof orb->request_buffer_bus,
798 DMA_FROM_DEVICE);
799
800 orb->cmd->result = result << 16;
801 orb->done(orb->cmd);
802
803 kfree(orb);
804}
805
806static void sbp2_command_orb_map_scatterlist(struct sbp2_command_orb *orb)
807{
808 struct fw_unit *unit =
809 (struct fw_unit *)orb->cmd->device->host->hostdata[0];
810 struct fw_device *device = fw_device(unit->device.parent);
811 struct sbp2_device *sd = unit->device.driver_data;
812 struct scatterlist *sg;
813 int sg_len, l, i, j, count;
814 size_t size;
815 dma_addr_t sg_addr;
816
817 sg = (struct scatterlist *)orb->cmd->request_buffer;
818 count = dma_map_sg(device->card->device, sg, orb->cmd->use_sg,
819 orb->cmd->sc_data_direction);
820
821 /* Handle the special case where there is only one element in
822 * the scatter list by converting it to an immediate block
823 * request. This is also a workaround for broken devices such
824 * as the second generation iPod which doesn't support page
825 * tables. */
826 if (count == 1 && sg_dma_len(sg) < SBP2_MAX_SG_ELEMENT_LENGTH) {
827 orb->request.data_descriptor.high = sd->address_high;
828 orb->request.data_descriptor.low = sg_dma_address(sg);
829 orb->request.misc |=
830 command_orb_data_size(sg_dma_len(sg));
831 return;
832 }
833
834 /* Convert the scatterlist to an sbp2 page table. If any
835 * scatterlist entries are too big for sbp2 we split the as we go. */
836 for (i = 0, j = 0; i < count; i++) {
837 sg_len = sg_dma_len(sg + i);
838 sg_addr = sg_dma_address(sg + i);
839 while (sg_len) {
840 l = min(sg_len, SBP2_MAX_SG_ELEMENT_LENGTH);
841 orb->page_table[j].low = sg_addr;
842 orb->page_table[j].high = (l << 16);
843 sg_addr += l;
844 sg_len -= l;
845 j++;
846 }
847 }
848
849 size = sizeof orb->page_table[0] * j;
850
851 /* The data_descriptor pointer is the one case where we need
852 * to fill in the node ID part of the address. All other
853 * pointers assume that the data referenced reside on the
854 * initiator (i.e. us), but data_descriptor can refer to data
855 * on other nodes so we need to put our ID in descriptor.high. */
856
857 orb->page_table_bus =
858 dma_map_single(device->card->device, orb->page_table,
859 size, DMA_TO_DEVICE);
860 orb->request.data_descriptor.high = sd->address_high;
861 orb->request.data_descriptor.low = orb->page_table_bus;
862 orb->request.misc |=
863 command_orb_page_table_present |
864 command_orb_data_size(j);
865
866 fw_memcpy_to_be32(orb->page_table, orb->page_table, size);
867}
868
869static void sbp2_command_orb_map_buffer(struct sbp2_command_orb *orb)
870{
871 struct fw_unit *unit =
872 (struct fw_unit *)orb->cmd->device->host->hostdata[0];
873 struct fw_device *device = fw_device(unit->device.parent);
874 struct sbp2_device *sd = unit->device.driver_data;
875
876 /* As for map_scatterlist, we need to fill in the high bits of
877 * the data_descriptor pointer. */
878
879 orb->request_buffer_bus =
880 dma_map_single(device->card->device,
881 orb->cmd->request_buffer,
882 orb->cmd->request_bufflen,
883 orb->cmd->sc_data_direction);
884 orb->request.data_descriptor.high = sd->address_high;
885 orb->request.data_descriptor.low = orb->request_buffer_bus;
886 orb->request.misc |=
887 command_orb_data_size(orb->cmd->request_bufflen);
888}
889
890/* SCSI stack integration */
891
892static int sbp2_scsi_queuecommand(struct scsi_cmnd *cmd, scsi_done_fn_t done)
893{
894 struct fw_unit *unit = (struct fw_unit *)cmd->device->host->hostdata[0];
895 struct fw_device *device = fw_device(unit->device.parent);
896 struct sbp2_device *sd = unit->device.driver_data;
897 struct sbp2_command_orb *orb;
898
899 /* Bidirectional commands are not yet implemented, and unknown
900 * transfer direction not handled. */
901 if (cmd->sc_data_direction == DMA_BIDIRECTIONAL) {
902 fw_error("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
903 cmd->result = DID_ERROR << 16;
904 done(cmd);
905 return 0;
906 }
907
908 orb = kzalloc(sizeof *orb, GFP_ATOMIC);
909 if (orb == NULL) {
910 fw_notify("failed to alloc orb\n");
911 cmd->result = DID_NO_CONNECT << 16;
912 done(cmd);
913 return 0;
914 }
915
916 orb->base.request_bus =
917 dma_map_single(device->card->device, &orb->request,
918 sizeof orb->request, DMA_TO_DEVICE);
919
920 orb->unit = unit;
921 orb->done = done;
922 orb->cmd = cmd;
923
924 orb->request.next.high = SBP2_ORB_NULL;
925 orb->request.next.low = 0x0;
926 /* At speed 100 we can do 512 bytes per packet, at speed 200,
927 * 1024 bytes per packet etc. The SBP-2 max_payload field
928 * specifies the max payload size as 2 ^ (max_payload + 2), so
929 * if we set this to max_speed + 7, we get the right value. */
930 orb->request.misc =
931 command_orb_max_payload(device->node->max_speed + 7) |
932 command_orb_speed(device->node->max_speed) |
933 command_orb_notify;
934
935 if (cmd->sc_data_direction == DMA_FROM_DEVICE)
936 orb->request.misc |=
937 command_orb_direction(SBP2_DIRECTION_FROM_MEDIA);
938 else if (cmd->sc_data_direction == DMA_TO_DEVICE)
939 orb->request.misc |=
940 command_orb_direction(SBP2_DIRECTION_TO_MEDIA);
941
942 if (cmd->use_sg) {
943 sbp2_command_orb_map_scatterlist(orb);
944 } else if (cmd->request_bufflen > SBP2_MAX_SG_ELEMENT_LENGTH) {
945 /* FIXME: Need to split this into a sg list... but
946 * could we get the scsi or blk layer to do that by
947 * reporting our max supported block size? */
948 fw_error("command > 64k\n");
949 cmd->result = DID_ERROR << 16;
950 done(cmd);
951 return 0;
952 } else if (cmd->request_bufflen > 0) {
953 sbp2_command_orb_map_buffer(orb);
954 }
955
956 fw_memcpy_to_be32(&orb->request, &orb->request, sizeof orb->request);
957
958 memset(orb->request.command_block,
959 0, sizeof orb->request.command_block);
960 memcpy(orb->request.command_block, cmd->cmnd, COMMAND_SIZE(*cmd->cmnd));
961
962 orb->base.callback = complete_command_orb;
963
964 sbp2_send_orb(&orb->base, unit, sd->node_id, sd->generation,
965 sd->command_block_agent_address + SBP2_ORB_POINTER);
966
967 return 0;
968}
969
970static int sbp2_scsi_slave_configure(struct scsi_device *sdev)
971{
972 struct fw_unit *unit = (struct fw_unit *)sdev->host->hostdata[0];
973 struct sbp2_device *sd = unit->device.driver_data;
974
975 if (sdev->type == TYPE_DISK &&
976 sd->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
977 sdev->skip_ms_page_8 = 1;
978 if (sd->workarounds & SBP2_WORKAROUND_FIX_CAPACITY) {
979 fw_notify("setting fix_capacity for %s\n", unit->device.bus_id);
980 sdev->fix_capacity = 1;
981 }
982
983 return 0;
984}
985
986/*
987 * Called by scsi stack when something has really gone wrong. Usually
988 * called when a command has timed-out for some reason.
989 */
990static int sbp2_scsi_abort(struct scsi_cmnd *cmd)
991{
992 struct fw_unit *unit = (struct fw_unit *)cmd->device->host->hostdata[0];
993
994 fw_notify("sbp2_scsi_abort\n");
995
996 sbp2_cancel_orbs(unit);
997
998 return SUCCESS;
999}
1000
1001static struct scsi_host_template scsi_driver_template = {
1002 .module = THIS_MODULE,
1003 .name = "SBP-2 IEEE-1394",
1004 .proc_name = (char *)sbp2_driver_name,
1005 .queuecommand = sbp2_scsi_queuecommand,
1006 .slave_configure = sbp2_scsi_slave_configure,
1007 .eh_abort_handler = sbp2_scsi_abort,
1008 .this_id = -1,
1009 .sg_tablesize = SG_ALL,
1010 .use_clustering = ENABLE_CLUSTERING,
1011 .cmd_per_lun = 1, /* SBP2_MAX_CMDS, */
1012 .can_queue = 1, /* SBP2_MAX_CMDS, */
1013 .emulated = 1,
1014};
1015
1016static int add_scsi_devices(struct fw_unit *unit)
1017{
1018 struct sbp2_device *sd = unit->device.driver_data;
1019 int retval, lun;
1020
1021 sd->scsi_host = scsi_host_alloc(&scsi_driver_template,
1022 sizeof(unsigned long));
1023 if (sd->scsi_host == NULL) {
1024 fw_error("failed to register scsi host\n");
1025 return -1;
1026 }
1027
1028 sd->scsi_host->hostdata[0] = (unsigned long)unit;
1029 retval = scsi_add_host(sd->scsi_host, &unit->device);
1030 if (retval < 0) {
1031 fw_error("failed to add scsi host\n");
1032 scsi_host_put(sd->scsi_host);
1033 return retval;
1034 }
1035
1036 /* FIXME: Loop over luns here. */
1037 lun = 0;
1038 retval = scsi_add_device(sd->scsi_host, 0, 0, lun);
1039 if (retval < 0) {
1040 fw_error("failed to add scsi device\n");
1041 scsi_remove_host(sd->scsi_host);
1042 scsi_host_put(sd->scsi_host);
1043 return retval;
1044 }
1045
1046 return 0;
1047}
1048
1049static void remove_scsi_devices(struct fw_unit *unit)
1050{
1051 struct sbp2_device *sd = unit->device.driver_data;
1052
1053 scsi_remove_host(sd->scsi_host);
1054 scsi_host_put(sd->scsi_host);
1055}
1056
1057MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
1058MODULE_DESCRIPTION("SCSI over IEEE1394");
1059MODULE_LICENSE("GPL");
1060MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
1061
1062static int __init sbp2_init(void)
1063{
1064 return driver_register(&sbp2_driver.driver);
1065}
1066
1067static void __exit sbp2_cleanup(void)
1068{
1069 driver_unregister(&sbp2_driver.driver);
1070}
1071
1072module_init(sbp2_init);
1073module_exit(sbp2_cleanup);