firewire: Add rom_index attribute for unit sysfs directories.
[linux-2.6-block.git] / drivers / firewire / fw-sbp2.c
CommitLineData
9ba136d0 1/* -*- c-basic-offset: 8 -*-
1da0c93b 2 * fw-spb2.c -- SBP2 driver (SCSI over IEEE1394)
9ba136d0 3 *
27a15e50 4 * Copyright (C) 2005-2007 Kristian Hoegsberg <krh@bitplanet.net>
9ba136d0
KH
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
27a15e50
KH
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>
27 * and many others.
28 */
29
9ba136d0
KH
30#include <linux/kernel.h>
31#include <linux/module.h>
fe69ca3a 32#include <linux/mod_devicetable.h>
9ba136d0 33#include <linux/device.h>
0b5b2903 34#include <linux/scatterlist.h>
9ba136d0 35#include <linux/dma-mapping.h>
1d3d52c5 36#include <linux/timer.h>
9ba136d0
KH
37
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>
43
44#include "fw-transaction.h"
45#include "fw-topology.h"
46#include "fw-device.h"
47
48/* I don't know why the SCSI stack doesn't define something like this... */
49typedef void (*scsi_done_fn_t) (struct scsi_cmnd *);
50
51static const char sbp2_driver_name[] = "sbp2";
52
53struct sbp2_device {
54 struct fw_unit *unit;
55 struct fw_address_handler address_handler;
56 struct list_head orb_list;
57 u64 management_agent_address;
58 u64 command_block_agent_address;
59 u32 workarounds;
60 int login_id;
61
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). */
67 int node_id;
68 int address_high;
69 int generation;
70
1d3d52c5
KH
71 /* Timer for flushing ORBs. */
72 struct timer_list orb_timer;
73
7f37c426
KH
74 int retries;
75 struct delayed_work work;
9ba136d0
KH
76 struct Scsi_Host *scsi_host;
77};
78
79#define SBP2_MAX_SG_ELEMENT_LENGTH 0xf000
80#define SBP2_MAX_SECTORS 255 /* Max sectors supported */
1d3d52c5 81#define SBP2_ORB_TIMEOUT 2000 /* Timeout in ms */
9ba136d0
KH
82
83#define SBP2_ORB_NULL 0x80000000
84
85#define SBP2_DIRECTION_TO_MEDIA 0x0
86#define SBP2_DIRECTION_FROM_MEDIA 0x1
87
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
93
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
100
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
111
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
118
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
124
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)
133
134struct sbp2_status {
135 u32 status;
136 u32 orb_low;
137 u8 data[24];
138};
139
140struct sbp2_pointer {
141 u32 high;
142 u32 low;
143};
144
145struct sbp2_orb {
146 struct fw_transaction t;
147 dma_addr_t request_bus;
148 int rcode;
149 struct sbp2_pointer pointer;
150 void (*callback) (struct sbp2_orb * orb, struct sbp2_status * status);
151 struct list_head link;
152};
153
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)
160
161#define management_orb_response_length(v) ((v))
162#define management_orb_password_length(v) ((v) << 16)
163
164struct sbp2_management_orb {
165 struct sbp2_orb base;
166 struct {
167 struct sbp2_pointer password;
168 struct sbp2_pointer response;
169 u32 misc;
170 u32 length;
171 struct sbp2_pointer status_fifo;
172 } request;
173 __be32 response[4];
174 dma_addr_t response_bus;
175 struct completion done;
176 struct sbp2_status status;
177};
178
179#define login_response_get_login_id(v) ((v).misc & 0xffff)
180#define login_response_get_length(v) (((v).misc >> 16) & 0xffff)
181
182struct sbp2_login_response {
183 u32 misc;
184 struct sbp2_pointer command_block_agent;
185 u32 reconnect_hold;
186};
187
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)
196
197struct sbp2_command_orb {
198 struct sbp2_orb base;
199 struct {
200 struct sbp2_pointer next;
201 struct sbp2_pointer data_descriptor;
202 u32 misc;
203 u8 command_block[12];
204 } request;
205 struct scsi_cmnd *cmd;
206 scsi_done_fn_t done;
207 struct fw_unit *unit;
208
209 struct sbp2_pointer page_table[SG_ALL];
210 dma_addr_t page_table_bus;
211 dma_addr_t request_buffer_bus;
212};
213
214/*
215 * List of devices with known bugs.
216 *
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.
222 */
223static const struct {
224 u32 firmware_revision;
225 u32 model;
226 unsigned workarounds;
227} sbp2_workarounds_table[] = {
228 /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
229 .firmware_revision = 0x002800,
230 .model = 0x001010,
231 .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
232 SBP2_WORKAROUND_MODE_SENSE_8,
233 },
234 /* Initio bridges, actually only needed for some older ones */ {
235 .firmware_revision = 0x000200,
236 .model = ~0,
237 .workarounds = SBP2_WORKAROUND_INQUIRY_36,
238 },
239 /* Symbios bridge */ {
240 .firmware_revision = 0xa0b800,
241 .model = ~0,
242 .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
243 },
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,
250 .model = 0x000021,
251 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
252 },
253 /* iPod mini */ {
254 .firmware_revision = 0x0a2700,
255 .model = 0x000023,
256 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
257 },
258 /* iPod Photo */ {
259 .firmware_revision = 0x0a2700,
260 .model = 0x00007e,
261 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
262 }
263};
264
265static void
266sbp2_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)
271{
272 struct sbp2_device *sd = callback_data;
273 struct sbp2_orb *orb;
274 struct sbp2_status status;
275 size_t header_size;
276 unsigned long flags;
277
278 if (tcode != TCODE_WRITE_BLOCK_REQUEST ||
279 length == 0 || length > sizeof status) {
280 fw_send_response(card, request, RCODE_TYPE_ERROR);
281 return;
282 }
283
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);
291 return;
292 }
293
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);
300 break;
301 }
302 }
303 spin_unlock_irqrestore(&card->lock, flags);
304
305 if (&orb->link != &sd->orb_list)
306 orb->callback(orb, &status);
307 else
308 fw_error("status write for unknown orb\n");
309
310 fw_send_response(card, request, RCODE_COMPLETE);
311}
312
313static void
314complete_transaction(struct fw_card *card, int rcode,
315 void *payload, size_t length, void *data)
316{
317 struct sbp2_orb *orb = data;
318 unsigned long flags;
319
320 orb->rcode = rcode;
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);
326 }
327}
328
329static void
330sbp2_send_orb(struct sbp2_orb *orb, struct fw_unit *unit,
331 int node_id, int generation, u64 offset)
332{
333 struct fw_device *device = fw_device(unit->device.parent);
334 struct sbp2_device *sd = unit->device.driver_data;
335 unsigned long flags;
336
337 orb->pointer.high = 0;
338 orb->pointer.low = orb->request_bus;
339 fw_memcpy_to_be32(&orb->pointer, &orb->pointer, sizeof orb->pointer);
340
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);
344
1d3d52c5
KH
345 mod_timer(&sd->orb_timer,
346 jiffies + DIV_ROUND_UP(SBP2_ORB_TIMEOUT * HZ, 1000));
347
9ba136d0 348 fw_send_request(device->card, &orb->t, TCODE_WRITE_BLOCK_REQUEST,
907293d7 349 node_id, generation,
9ba136d0
KH
350 device->node->max_speed, offset,
351 &orb->pointer, sizeof orb->pointer,
352 complete_transaction, orb);
353}
354
355static void sbp2_cancel_orbs(struct fw_unit *unit)
356{
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;
361 unsigned long flags;
362
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);
367
368 list_for_each_entry_safe(orb, next, &list, link) {
730c32f5
KH
369 if (fw_cancel_transaction(device->card, &orb->t) == 0)
370 continue;
371
9ba136d0
KH
372 orb->rcode = RCODE_CANCELLED;
373 orb->callback(orb, NULL);
374 }
375}
376
1d3d52c5
KH
377static void orb_timer_callback(unsigned long data)
378{
379 struct sbp2_device *sd = (struct sbp2_device *)data;
380
381 sbp2_cancel_orbs(sd->unit);
382}
383
9ba136d0
KH
384static void
385complete_management_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
386{
387 struct sbp2_management_orb *orb =
388 (struct sbp2_management_orb *)base_orb;
389
390 if (status)
391 memcpy(&orb->status, status, sizeof *status);
392 complete(&orb->done);
393}
394
395static int
396sbp2_send_management_orb(struct fw_unit *unit, int node_id, int generation,
397 int function, int lun, void *response)
398{
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;
9ba136d0
KH
402 int retval = -ENOMEM;
403
404 orb = kzalloc(sizeof *orb, GFP_ATOMIC);
405 if (orb == NULL)
406 return -ENOMEM;
407
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
410 * dma. */
411 orb->base.request_bus =
412 dma_map_single(device->card->device, &orb->request,
413 sizeof orb->request, DMA_TO_DEVICE);
82eff9db 414 if (dma_mapping_error(orb->base.request_bus))
9ba136d0
KH
415 goto out;
416
417 orb->response_bus =
418 dma_map_single(device->card->device, &orb->response,
419 sizeof orb->response, DMA_FROM_DEVICE);
82eff9db 420 if (dma_mapping_error(orb->response_bus))
9ba136d0
KH
421 goto out;
422
423 orb->request.response.high = 0;
424 orb->request.response.low = orb->response_bus;
425
426 orb->request.misc =
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);
432
433 orb->request.status_fifo.high = sd->address_handler.offset >> 32;
434 orb->request.status_fifo.low = sd->address_handler.offset;
435
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
439 * option. */
440 if (function == SBP2_LOGIN_REQUEST) {
441 orb->request.misc |=
442 management_orb_exclusive |
443 management_orb_reconnect(0);
444 }
445
446 fw_memcpy_to_be32(&orb->request, &orb->request, sizeof orb->request);
447
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);
452
1d3d52c5 453 wait_for_completion(&orb->done);
9ba136d0 454
9ba136d0
KH
455 retval = -EIO;
456 if (orb->base.rcode != RCODE_COMPLETE) {
457 fw_error("management write failed, rcode 0x%02x\n",
458 orb->base.rcode);
459 goto out;
460 }
461
1d3d52c5 462 if (orb->base.rcode == RCODE_CANCELLED) {
9ba136d0
KH
463 fw_error("orb reply timed out, rcode=0x%02x\n",
464 orb->base.rcode);
465 goto out;
466 }
467
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));
473 goto out;
474 }
475
476 retval = 0;
477 out:
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);
482
483 if (response)
484 fw_memcpy_from_be32(response,
485 orb->response, sizeof orb->response);
486 kfree(orb);
487
488 return retval;
489}
490
491static void
492complete_agent_reset_write(struct fw_card *card, int rcode,
493 void *payload, size_t length, void *data)
494{
495 struct fw_transaction *t = data;
496
9ba136d0
KH
497 kfree(t);
498}
499
500static int sbp2_agent_reset(struct fw_unit *unit)
501{
502 struct fw_device *device = fw_device(unit->device.parent);
503 struct sbp2_device *sd = unit->device.driver_data;
504 struct fw_transaction *t;
505 static u32 zero;
506
507 t = kzalloc(sizeof *t, GFP_ATOMIC);
508 if (t == NULL)
509 return -ENOMEM;
510
511 fw_send_request(device->card, t, TCODE_WRITE_QUADLET_REQUEST,
907293d7 512 sd->node_id, sd->generation, SCODE_400,
9ba136d0
KH
513 sd->command_block_agent_address + SBP2_AGENT_RESET,
514 &zero, sizeof zero, complete_agent_reset_write, t);
515
516 return 0;
517}
518
519static int add_scsi_devices(struct fw_unit *unit);
520static void remove_scsi_devices(struct fw_unit *unit);
7f37c426
KH
521static void sbp2_reconnect(struct work_struct *work);
522
523static void sbp2_login(struct work_struct *work)
524{
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;
531
532 /* FIXME: Make this work for multi-lun devices. */
533 lun = 0;
534
535 generation = device->card->generation;
536 node_id = device->node->node_id;
537 local_node_id = device->card->local_node->node_id;
538
539 if (sbp2_send_management_orb(unit, node_id, generation,
540 SBP2_LOGIN_REQUEST, lun, &response) < 0) {
541 if (sd->retries++ < 5) {
7f37c426
KH
542 schedule_delayed_work(&sd->work, DIV_ROUND_UP(HZ, 5));
543 } else {
544 fw_error("failed to login to %s\n",
545 unit->device.bus_id);
546 remove_scsi_devices(unit);
547 }
548 return;
549 }
550
551 sd->generation = generation;
552 sd->node_id = node_id;
553 sd->address_high = local_node_id << 16;
554
555 /* Get command block agent offset and login id. */
556 sd->command_block_agent_address =
5c5539d8 557 ((u64) (response.command_block_agent.high & 0xffff) << 32) |
7f37c426
KH
558 response.command_block_agent.low;
559 sd->login_id = login_response_get_login_id(response);
560
5c5539d8
KH
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",
7f37c426
KH
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);
5c5539d8 567 fw_notify(" - status write address: 0x%012llx\n",
7f37c426
KH
568 (unsigned long long) sd->address_handler.offset);
569
570#if 0
571 /* FIXME: The linux1394 sbp2 does this last step. */
572 sbp2_set_busy_timeout(scsi_id);
573#endif
574
1da0c93b 575 PREPARE_DELAYED_WORK(&sd->work, sbp2_reconnect);
7f37c426
KH
576 sbp2_agent_reset(unit);
577
578 retval = add_scsi_devices(unit);
579 if (retval < 0) {
580 sbp2_send_management_orb(unit, sd->node_id, sd->generation,
581 SBP2_LOGOUT_REQUEST, sd->login_id,
582 NULL);
583 /* Set this back to sbp2_login so we fall back and
584 * retry login on bus reset. */
1da0c93b 585 PREPARE_DELAYED_WORK(&sd->work, sbp2_login);
7f37c426
KH
586 }
587}
9ba136d0
KH
588
589static int sbp2_probe(struct device *dev)
590{
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;
7f37c426 595 int i, key, value;
9ba136d0
KH
596 u32 model, firmware_revision;
597
598 sd = kzalloc(sizeof *sd, GFP_KERNEL);
599 if (sd == NULL)
600 return -ENOMEM;
601
602 unit->device.driver_data = sd;
603 sd->unit = unit;
604 INIT_LIST_HEAD(&sd->orb_list);
1d3d52c5 605 setup_timer(&sd->orb_timer, orb_timer_callback, (unsigned long)sd);
9ba136d0
KH
606
607 sd->address_handler.length = 0x100;
608 sd->address_handler.address_callback = sbp2_status_write;
609 sd->address_handler.callback_data = sd;
610
611 if (fw_core_add_address_handler(&sd->address_handler,
612 &fw_high_memory_region) < 0) {
613 kfree(sd);
614 return -EBUSY;
615 }
616
617 if (fw_device_enable_phys_dma(device) < 0) {
618 fw_core_remove_address_handler(&sd->address_handler);
619 kfree(sd);
620 return -EBUSY;
621 }
622
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;
627 model = 0xff000000;
628 fw_csr_iterator_init(&ci, unit->directory);
629 while (fw_csr_iterator_next(&ci, &key, &value)) {
630 switch (key) {
631 case CSR_DEPENDENT_INFO | CSR_OFFSET:
632 sd->management_agent_address =
633 0xfffff0000000ULL + 4 * value;
634 break;
635 case SBP2_FIRMWARE_REVISION:
636 firmware_revision = value;
637 break;
638 case CSR_MODEL:
639 model = value;
640 break;
641 }
642 }
643
644 for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
645 if (sbp2_workarounds_table[i].firmware_revision !=
646 (firmware_revision & 0xffffff00))
647 continue;
648 if (sbp2_workarounds_table[i].model != model &&
649 sbp2_workarounds_table[i].model != ~0)
650 continue;
651 sd->workarounds |= sbp2_workarounds_table[i].workarounds;
652 break;
653 }
654
655 if (sd->workarounds)
656 fw_notify("Workarounds for node %s: 0x%x "
657 "(firmware_revision 0x%06x, model_id 0x%06x)\n",
658 unit->device.bus_id,
659 sd->workarounds, firmware_revision, model);
660
7f37c426
KH
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);
9ba136d0
KH
665
666 return 0;
667}
668
669static int sbp2_remove(struct device *dev)
670{
671 struct fw_unit *unit = fw_unit(dev);
672 struct sbp2_device *sd = unit->device.driver_data;
673
674 sbp2_send_management_orb(unit, sd->node_id, sd->generation,
675 SBP2_LOGOUT_REQUEST, sd->login_id, NULL);
676
677 remove_scsi_devices(unit);
1d3d52c5 678 del_timer_sync(&sd->orb_timer);
9ba136d0
KH
679
680 fw_core_remove_address_handler(&sd->address_handler);
681 kfree(sd);
682
683 fw_notify("removed sbp2 unit %s\n", dev->bus_id);
684
685 return 0;
686}
687
688static void sbp2_reconnect(struct work_struct *work)
689{
7f37c426
KH
690 struct sbp2_device *sd =
691 container_of(work, struct sbp2_device, work.work);
9ba136d0
KH
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;
695
9ba136d0
KH
696 generation = device->card->generation;
697 node_id = device->node->node_id;
698 local_node_id = device->card->local_node->node_id;
699
7f37c426
KH
700 if (sbp2_send_management_orb(unit, node_id, generation,
701 SBP2_RECONNECT_REQUEST,
702 sd->login_id, NULL) < 0) {
5c5539d8 703 if (sd->retries++ >= 5) {
7f37c426
KH
704 fw_error("failed to reconnect to %s\n",
705 unit->device.bus_id);
706 /* Fall back and try to log in again. */
707 sd->retries = 0;
1da0c93b 708 PREPARE_DELAYED_WORK(&sd->work, sbp2_login);
7f37c426
KH
709 }
710 schedule_delayed_work(&sd->work, DIV_ROUND_UP(HZ, 5));
711 return;
712 }
9ba136d0
KH
713
714 sd->generation = generation;
715 sd->node_id = node_id;
907293d7 716 sd->address_high = local_node_id << 16;
7f37c426 717
5c5539d8
KH
718 fw_notify("reconnected to unit %s (%d retries)\n",
719 unit->device.bus_id, sd->retries);
7f37c426
KH
720 sbp2_agent_reset(unit);
721 sbp2_cancel_orbs(unit);
9ba136d0
KH
722}
723
724static void sbp2_update(struct fw_unit *unit)
725{
726 struct fw_device *device = fw_device(unit->device.parent);
727 struct sbp2_device *sd = unit->device.driver_data;
728
7f37c426 729 sd->retries = 0;
9ba136d0 730 fw_device_enable_phys_dma(device);
7f37c426 731 schedule_delayed_work(&sd->work, 0);
9ba136d0
KH
732}
733
734#define SBP2_UNIT_SPEC_ID_ENTRY 0x0000609e
735#define SBP2_SW_VERSION_ENTRY 0x00010483
736
21ebcd12 737static const struct fw_device_id sbp2_id_table[] = {
9ba136d0
KH
738 {
739 .match_flags = FW_MATCH_SPECIFIER_ID | FW_MATCH_VERSION,
740 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY,
5af4e5ea 741 .version = SBP2_SW_VERSION_ENTRY,
9ba136d0
KH
742 },
743 { }
744};
745
746static struct fw_driver sbp2_driver = {
747 .driver = {
748 .owner = THIS_MODULE,
749 .name = sbp2_driver_name,
750 .bus = &fw_bus_type,
751 .probe = sbp2_probe,
752 .remove = sbp2_remove,
753 },
754 .update = sbp2_update,
755 .id_table = sbp2_id_table,
756};
757
758static unsigned int sbp2_status_to_sense_data(u8 * sbp2_status, u8 * sense_data)
759{
760 sense_data[0] = 0x70;
761 sense_data[1] = 0x0;
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];
767 sense_data[7] = 10;
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];
776
777 switch (sbp2_status[0] & 0x3f) {
778 case SAM_STAT_GOOD:
779 return DID_OK;
780
781 case SAM_STAT_CHECK_CONDITION:
782 /* return CHECK_CONDITION << 1 | DID_OK << 16; */
783 return DID_OK;
784
785 case SAM_STAT_BUSY:
786 return DID_BUS_BUSY;
787
788 case SAM_STAT_CONDITION_MET:
789 case SAM_STAT_RESERVATION_CONFLICT:
790 case SAM_STAT_COMMAND_TERMINATED:
791 default:
792 return DID_ERROR;
793 }
794}
795
796static void
797complete_command_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
798{
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;
803 int result;
804
805 if (status != NULL) {
806 if (status_get_dead(*status)) {
807 fw_notify("agent died, issuing agent reset\n");
808 sbp2_agent_reset(unit);
809 }
810
811 switch (status_get_response(*status)) {
812 case SBP2_STATUS_REQUEST_COMPLETE:
813 result = DID_OK;
814 break;
815 case SBP2_STATUS_TRANSPORT_FAILURE:
816 result = DID_BUS_BUSY;
817 break;
818 case SBP2_STATUS_ILLEGAL_REQUEST:
819 case SBP2_STATUS_VENDOR_DEPENDENT:
820 default:
821 result = DID_ERROR;
822 break;
823 }
824
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);
828 } else {
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). */
374a0039 832 result = DID_BUS_BUSY;
9ba136d0
KH
833 }
834
835 dma_unmap_single(device->card->device, orb->base.request_bus,
836 sizeof orb->request, DMA_TO_DEVICE);
837
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);
842 }
843
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);
847
848 if (orb->request_buffer_bus != 0)
849 dma_unmap_single(device->card->device, orb->request_buffer_bus,
850 sizeof orb->request_buffer_bus,
851 DMA_FROM_DEVICE);
852
853 orb->cmd->result = result << 16;
854 orb->done(orb->cmd);
855
856 kfree(orb);
857}
858
859static void sbp2_command_orb_map_scatterlist(struct sbp2_command_orb *orb)
860{
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;
867 size_t size;
868 dma_addr_t sg_addr;
869
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);
873
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
878 * tables. */
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);
882 orb->request.misc |=
883 command_orb_data_size(sg_dma_len(sg));
884 return;
885 }
886
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);
892 while (sg_len) {
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);
896 sg_addr += l;
897 sg_len -= l;
898 j++;
899 }
900 }
901
902 size = sizeof orb->page_table[0] * j;
903
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. */
909
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;
915 orb->request.misc |=
916 command_orb_page_table_present |
917 command_orb_data_size(j);
918
919 fw_memcpy_to_be32(orb->page_table, orb->page_table, size);
920}
921
922static void sbp2_command_orb_map_buffer(struct sbp2_command_orb *orb)
923{
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;
928
929 /* As for map_scatterlist, we need to fill in the high bits of
930 * the data_descriptor pointer. */
931
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;
939 orb->request.misc |=
940 command_orb_data_size(orb->cmd->request_bufflen);
941}
942
943/* SCSI stack integration */
944
945static int sbp2_scsi_queuecommand(struct scsi_cmnd *cmd, scsi_done_fn_t done)
946{
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;
951
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");
82eff9db 956 goto fail_alloc;
9ba136d0
KH
957 }
958
959 orb = kzalloc(sizeof *orb, GFP_ATOMIC);
960 if (orb == NULL) {
961 fw_notify("failed to alloc orb\n");
82eff9db 962 goto fail_alloc;
9ba136d0
KH
963 }
964
965 orb->base.request_bus =
966 dma_map_single(device->card->device, &orb->request,
967 sizeof orb->request, DMA_TO_DEVICE);
82eff9db
KH
968 if (dma_mapping_error(orb->base.request_bus))
969 goto fail_mapping;
9ba136d0
KH
970
971 orb->unit = unit;
972 orb->done = done;
973 orb->cmd = cmd;
974
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. */
981 orb->request.misc =
982 command_orb_max_payload(device->node->max_speed + 7) |
983 command_orb_speed(device->node->max_speed) |
984 command_orb_notify;
985
986 if (cmd->sc_data_direction == DMA_FROM_DEVICE)
987 orb->request.misc |=
988 command_orb_direction(SBP2_DIRECTION_FROM_MEDIA);
989 else if (cmd->sc_data_direction == DMA_TO_DEVICE)
990 orb->request.misc |=
991 command_orb_direction(SBP2_DIRECTION_TO_MEDIA);
992
993 if (cmd->use_sg) {
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");
82eff9db 1000 goto fail_bufflen;
9ba136d0
KH
1001 } else if (cmd->request_bufflen > 0) {
1002 sbp2_command_orb_map_buffer(orb);
1003 }
1004
1005 fw_memcpy_to_be32(&orb->request, &orb->request, sizeof orb->request);
1006
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));
1010
1011 orb->base.callback = complete_command_orb;
1012
1013 sbp2_send_orb(&orb->base, unit, sd->node_id, sd->generation,
1014 sd->command_block_agent_address + SBP2_ORB_POINTER);
1015
1016 return 0;
82eff9db
KH
1017
1018 fail_bufflen:
1019 dma_unmap_single(device->card->device, orb->base.request_bus,
1020 sizeof orb->request, DMA_TO_DEVICE);
1021 fail_mapping:
1022 kfree(orb);
1023 fail_alloc:
1024 cmd->result = DID_ERROR << 16;
1025 done(cmd);
1026 return 0;
9ba136d0
KH
1027}
1028
cfb01381
SR
1029static int sbp2_scsi_slave_alloc(struct scsi_device *sdev)
1030{
1031 struct fw_unit *unit = (struct fw_unit *)sdev->host->hostdata[0];
1032 struct sbp2_device *sd = unit->device.driver_data;
1033
1034 sdev->allow_restart = 1;
1035
1036 if (sd->workarounds & SBP2_WORKAROUND_INQUIRY_36)
1037 sdev->inquiry_len = 36;
1038 return 0;
1039}
1040
9ba136d0
KH
1041static int sbp2_scsi_slave_configure(struct scsi_device *sdev)
1042{
1043 struct fw_unit *unit = (struct fw_unit *)sdev->host->hostdata[0];
1044 struct sbp2_device *sd = unit->device.driver_data;
1045
cfb01381
SR
1046 sdev->use_10_for_rw = 1;
1047
1048 if (sdev->type == TYPE_ROM)
1049 sdev->use_10_for_ms = 1;
9ba136d0
KH
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;
1056 }
1057
1058 return 0;
1059}
1060
1061/*
1062 * Called by scsi stack when something has really gone wrong. Usually
1063 * called when a command has timed-out for some reason.
1064 */
1065static int sbp2_scsi_abort(struct scsi_cmnd *cmd)
1066{
1067 struct fw_unit *unit = (struct fw_unit *)cmd->device->host->hostdata[0];
1068
1069 fw_notify("sbp2_scsi_abort\n");
1070
1071 sbp2_cancel_orbs(unit);
1072
1073 return SUCCESS;
1074}
1075
1076static 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,
cfb01381 1081 .slave_alloc = sbp2_scsi_slave_alloc,
9ba136d0
KH
1082 .slave_configure = sbp2_scsi_slave_configure,
1083 .eh_abort_handler = sbp2_scsi_abort,
1084 .this_id = -1,
1085 .sg_tablesize = SG_ALL,
1086 .use_clustering = ENABLE_CLUSTERING,
02af8e70
SR
1087 .cmd_per_lun = 1,
1088 .can_queue = 1,
9ba136d0
KH
1089};
1090
1091static int add_scsi_devices(struct fw_unit *unit)
1092{
1093 struct sbp2_device *sd = unit->device.driver_data;
1094 int retval, lun;
1095
7f37c426
KH
1096 if (sd->scsi_host != NULL)
1097 return 0;
1098
9ba136d0
KH
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");
1103 return -1;
1104 }
1105
1106 sd->scsi_host->hostdata[0] = (unsigned long)unit;
1107 retval = scsi_add_host(sd->scsi_host, &unit->device);
1108 if (retval < 0) {
1109 fw_error("failed to add scsi host\n");
1110 scsi_host_put(sd->scsi_host);
1111 return retval;
1112 }
1113
1114 /* FIXME: Loop over luns here. */
1115 lun = 0;
1116 retval = scsi_add_device(sd->scsi_host, 0, 0, lun);
1117 if (retval < 0) {
1118 fw_error("failed to add scsi device\n");
1119 scsi_remove_host(sd->scsi_host);
1120 scsi_host_put(sd->scsi_host);
1121 return retval;
1122 }
1123
1124 return 0;
1125}
1126
1127static void remove_scsi_devices(struct fw_unit *unit)
1128{
1129 struct sbp2_device *sd = unit->device.driver_data;
1130
7f37c426
KH
1131 if (sd->scsi_host != NULL) {
1132 scsi_remove_host(sd->scsi_host);
1133 scsi_host_put(sd->scsi_host);
1134 }
1135 sd->scsi_host = NULL;
9ba136d0
KH
1136}
1137
1138MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
1139MODULE_DESCRIPTION("SCSI over IEEE1394");
1140MODULE_LICENSE("GPL");
1141MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
1142
1143static int __init sbp2_init(void)
1144{
1145 return driver_register(&sbp2_driver.driver);
1146}
1147
1148static void __exit sbp2_cleanup(void)
1149{
1150 driver_unregister(&sbp2_driver.driver);
1151}
1152
1153module_init(sbp2_init);
1154module_exit(sbp2_cleanup);