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