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