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