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