blob: 3308bc089beb18c1658a7d30dca42174678626e5 [file] [log] [blame]
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001/*
2 * 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øgsbergc781c062007-05-07 20:33:32 -040021/*
22 * The basic structure of this driver is based on the old storage driver,
Kristian Høgsberg27a15e52007-02-06 14:49:39 -050023 * drivers/ieee1394/sbp2.c, originally written by
24 * James Goodwin <jamesg@filanet.com>
25 * with later contributions and ongoing maintenance from
26 * Ben Collins <bcollins@debian.org>,
27 * Stefan Richter <stefanr@s5r6.in-berlin.de>
28 * and many others.
29 */
30
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050031#include <linux/kernel.h>
32#include <linux/module.h>
Stefan Richterfe69ca32006-12-28 12:46:54 +010033#include <linux/mod_devicetable.h>
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050034#include <linux/device.h>
Andrew Morton0b5b2902006-12-27 14:49:23 -080035#include <linux/scatterlist.h>
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050036#include <linux/dma-mapping.h>
Kristian Høgsberg1d3d52c2007-02-06 14:49:33 -050037#include <linux/timer.h>
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050038
39#include <scsi/scsi.h>
40#include <scsi/scsi_cmnd.h>
41#include <scsi/scsi_dbg.h>
42#include <scsi/scsi_device.h>
43#include <scsi/scsi_host.h>
44
45#include "fw-transaction.h"
46#include "fw-topology.h"
47#include "fw-device.h"
48
49/* I don't know why the SCSI stack doesn't define something like this... */
Kristian Høgsberga98e2712007-05-07 20:33:34 -040050typedef void (*scsi_done_fn_t)(struct scsi_cmnd *);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050051
52static const char sbp2_driver_name[] = "sbp2";
53
54struct sbp2_device {
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -040055 struct kref kref;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050056 struct fw_unit *unit;
57 struct fw_address_handler address_handler;
58 struct list_head orb_list;
59 u64 management_agent_address;
60 u64 command_block_agent_address;
61 u32 workarounds;
62 int login_id;
63
Kristian Høgsbergc781c062007-05-07 20:33:32 -040064 /*
65 * We cache these addresses and only update them once we've
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050066 * logged in or reconnected to the sbp2 device. That way, any
67 * IO to the device will automatically fail and get retried if
68 * it happens in a window where the device is not ready to
Kristian Høgsbergc781c062007-05-07 20:33:32 -040069 * handle it (e.g. after a bus reset but before we reconnect).
70 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050071 int node_id;
72 int address_high;
73 int generation;
74
Kristian Høgsberg7f37c422007-02-06 14:49:34 -050075 int retries;
76 struct delayed_work work;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050077 struct Scsi_Host *scsi_host;
78};
79
80#define SBP2_MAX_SG_ELEMENT_LENGTH 0xf000
81#define SBP2_MAX_SECTORS 255 /* Max sectors supported */
Kristian Høgsberg1d3d52c2007-02-06 14:49:33 -050082#define SBP2_ORB_TIMEOUT 2000 /* Timeout in ms */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050083
84#define SBP2_ORB_NULL 0x80000000
85
86#define SBP2_DIRECTION_TO_MEDIA 0x0
87#define SBP2_DIRECTION_FROM_MEDIA 0x1
88
89/* Unit directory keys */
90#define SBP2_COMMAND_SET_SPECIFIER 0x38
91#define SBP2_COMMAND_SET 0x39
92#define SBP2_COMMAND_SET_REVISION 0x3b
93#define SBP2_FIRMWARE_REVISION 0x3c
94
95/* Flags for detected oddities and brokeness */
96#define SBP2_WORKAROUND_128K_MAX_TRANS 0x1
97#define SBP2_WORKAROUND_INQUIRY_36 0x2
98#define SBP2_WORKAROUND_MODE_SENSE_8 0x4
99#define SBP2_WORKAROUND_FIX_CAPACITY 0x8
100#define SBP2_WORKAROUND_OVERRIDE 0x100
101
102/* Management orb opcodes */
103#define SBP2_LOGIN_REQUEST 0x0
104#define SBP2_QUERY_LOGINS_REQUEST 0x1
105#define SBP2_RECONNECT_REQUEST 0x3
106#define SBP2_SET_PASSWORD_REQUEST 0x4
107#define SBP2_LOGOUT_REQUEST 0x7
108#define SBP2_ABORT_TASK_REQUEST 0xb
109#define SBP2_ABORT_TASK_SET 0xc
110#define SBP2_LOGICAL_UNIT_RESET 0xe
111#define SBP2_TARGET_RESET_REQUEST 0xf
112
113/* Offsets for command block agent registers */
114#define SBP2_AGENT_STATE 0x00
115#define SBP2_AGENT_RESET 0x04
116#define SBP2_ORB_POINTER 0x08
117#define SBP2_DOORBELL 0x10
118#define SBP2_UNSOLICITED_STATUS_ENABLE 0x14
119
120/* Status write response codes */
121#define SBP2_STATUS_REQUEST_COMPLETE 0x0
122#define SBP2_STATUS_TRANSPORT_FAILURE 0x1
123#define SBP2_STATUS_ILLEGAL_REQUEST 0x2
124#define SBP2_STATUS_VENDOR_DEPENDENT 0x3
125
126#define status_get_orb_high(v) ((v).status & 0xffff)
127#define status_get_sbp_status(v) (((v).status >> 16) & 0xff)
128#define status_get_len(v) (((v).status >> 24) & 0x07)
129#define status_get_dead(v) (((v).status >> 27) & 0x01)
130#define status_get_response(v) (((v).status >> 28) & 0x03)
131#define status_get_source(v) (((v).status >> 30) & 0x03)
132#define status_get_orb_low(v) ((v).orb_low)
133#define status_get_data(v) ((v).data)
134
135struct sbp2_status {
136 u32 status;
137 u32 orb_low;
138 u8 data[24];
139};
140
141struct sbp2_pointer {
142 u32 high;
143 u32 low;
144};
145
146struct sbp2_orb {
147 struct fw_transaction t;
148 dma_addr_t request_bus;
149 int rcode;
150 struct sbp2_pointer pointer;
Kristian Høgsberga98e2712007-05-07 20:33:34 -0400151 void (*callback)(struct sbp2_orb * orb, struct sbp2_status * status);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500152 struct list_head link;
153};
154
155#define management_orb_lun(v) ((v))
156#define management_orb_function(v) ((v) << 16)
157#define management_orb_reconnect(v) ((v) << 20)
158#define management_orb_exclusive ((1) << 28)
159#define management_orb_request_format(v) ((v) << 29)
160#define management_orb_notify ((1) << 31)
161
162#define management_orb_response_length(v) ((v))
163#define management_orb_password_length(v) ((v) << 16)
164
165struct sbp2_management_orb {
166 struct sbp2_orb base;
167 struct {
168 struct sbp2_pointer password;
169 struct sbp2_pointer response;
170 u32 misc;
171 u32 length;
172 struct sbp2_pointer status_fifo;
173 } request;
174 __be32 response[4];
175 dma_addr_t response_bus;
176 struct completion done;
177 struct sbp2_status status;
178};
179
180#define login_response_get_login_id(v) ((v).misc & 0xffff)
181#define login_response_get_length(v) (((v).misc >> 16) & 0xffff)
182
183struct sbp2_login_response {
184 u32 misc;
185 struct sbp2_pointer command_block_agent;
186 u32 reconnect_hold;
187};
188
189#define command_orb_data_size(v) ((v))
190#define command_orb_page_size(v) ((v) << 16)
191#define command_orb_page_table_present ((1) << 19)
192#define command_orb_max_payload(v) ((v) << 20)
193#define command_orb_speed(v) ((v) << 24)
194#define command_orb_direction(v) ((v) << 27)
195#define command_orb_request_format(v) ((v) << 29)
196#define command_orb_notify ((1) << 31)
197
198struct sbp2_command_orb {
199 struct sbp2_orb base;
200 struct {
201 struct sbp2_pointer next;
202 struct sbp2_pointer data_descriptor;
203 u32 misc;
204 u8 command_block[12];
205 } request;
206 struct scsi_cmnd *cmd;
207 scsi_done_fn_t done;
208 struct fw_unit *unit;
209
210 struct sbp2_pointer page_table[SG_ALL];
211 dma_addr_t page_table_bus;
212 dma_addr_t request_buffer_bus;
213};
214
215/*
216 * List of devices with known bugs.
217 *
218 * The firmware_revision field, masked with 0xffff00, is the best
219 * indicator for the type of bridge chip of a device. It yields a few
220 * false positives but this did not break correctly behaving devices
221 * so far. We use ~0 as a wildcard, since the 24 bit values we get
222 * from the config rom can never match that.
223 */
224static const struct {
225 u32 firmware_revision;
226 u32 model;
227 unsigned workarounds;
228} sbp2_workarounds_table[] = {
229 /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
230 .firmware_revision = 0x002800,
231 .model = 0x001010,
232 .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
233 SBP2_WORKAROUND_MODE_SENSE_8,
234 },
235 /* Initio bridges, actually only needed for some older ones */ {
236 .firmware_revision = 0x000200,
237 .model = ~0,
238 .workarounds = SBP2_WORKAROUND_INQUIRY_36,
239 },
240 /* Symbios bridge */ {
241 .firmware_revision = 0xa0b800,
242 .model = ~0,
243 .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
244 },
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400245
246 /*
247 * There are iPods (2nd gen, 3rd gen) with model_id == 0, but
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500248 * these iPods do not feature the read_capacity bug according
249 * to one report. Read_capacity behaviour as well as model_id
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400250 * could change due to Apple-supplied firmware updates though.
251 */
252
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500253 /* iPod 4th generation. */ {
254 .firmware_revision = 0x0a2700,
255 .model = 0x000021,
256 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
257 },
258 /* iPod mini */ {
259 .firmware_revision = 0x0a2700,
260 .model = 0x000023,
261 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
262 },
263 /* iPod Photo */ {
264 .firmware_revision = 0x0a2700,
265 .model = 0x00007e,
266 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
267 }
268};
269
270static void
271sbp2_status_write(struct fw_card *card, struct fw_request *request,
272 int tcode, int destination, int source,
273 int generation, int speed,
274 unsigned long long offset,
275 void *payload, size_t length, void *callback_data)
276{
277 struct sbp2_device *sd = callback_data;
278 struct sbp2_orb *orb;
279 struct sbp2_status status;
280 size_t header_size;
281 unsigned long flags;
282
283 if (tcode != TCODE_WRITE_BLOCK_REQUEST ||
284 length == 0 || length > sizeof status) {
285 fw_send_response(card, request, RCODE_TYPE_ERROR);
286 return;
287 }
288
289 header_size = min(length, 2 * sizeof(u32));
290 fw_memcpy_from_be32(&status, payload, header_size);
291 if (length > header_size)
292 memcpy(status.data, payload + 8, length - header_size);
293 if (status_get_source(status) == 2 || status_get_source(status) == 3) {
294 fw_notify("non-orb related status write, not handled\n");
295 fw_send_response(card, request, RCODE_COMPLETE);
296 return;
297 }
298
299 /* Lookup the orb corresponding to this status write. */
300 spin_lock_irqsave(&card->lock, flags);
301 list_for_each_entry(orb, &sd->orb_list, link) {
302 if (status_get_orb_high(status) == 0 &&
Kristian Høgsberg12f26aa2007-04-10 18:11:20 -0400303 status_get_orb_low(status) == orb->request_bus &&
304 orb->rcode == RCODE_COMPLETE) {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500305 list_del(&orb->link);
306 break;
307 }
308 }
309 spin_unlock_irqrestore(&card->lock, flags);
310
311 if (&orb->link != &sd->orb_list)
312 orb->callback(orb, &status);
313 else
314 fw_error("status write for unknown orb\n");
315
316 fw_send_response(card, request, RCODE_COMPLETE);
317}
318
319static void
320complete_transaction(struct fw_card *card, int rcode,
321 void *payload, size_t length, void *data)
322{
323 struct sbp2_orb *orb = data;
324 unsigned long flags;
325
326 orb->rcode = rcode;
327 if (rcode != RCODE_COMPLETE) {
328 spin_lock_irqsave(&card->lock, flags);
329 list_del(&orb->link);
330 spin_unlock_irqrestore(&card->lock, flags);
331 orb->callback(orb, NULL);
332 }
333}
334
335static void
336sbp2_send_orb(struct sbp2_orb *orb, struct fw_unit *unit,
337 int node_id, int generation, u64 offset)
338{
339 struct fw_device *device = fw_device(unit->device.parent);
340 struct sbp2_device *sd = unit->device.driver_data;
341 unsigned long flags;
342
343 orb->pointer.high = 0;
344 orb->pointer.low = orb->request_bus;
345 fw_memcpy_to_be32(&orb->pointer, &orb->pointer, sizeof orb->pointer);
346
347 spin_lock_irqsave(&device->card->lock, flags);
348 list_add_tail(&orb->link, &sd->orb_list);
349 spin_unlock_irqrestore(&device->card->lock, flags);
350
351 fw_send_request(device->card, &orb->t, TCODE_WRITE_BLOCK_REQUEST,
Stefan Richter907293d2007-01-23 21:11:43 +0100352 node_id, generation,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500353 device->node->max_speed, offset,
354 &orb->pointer, sizeof orb->pointer,
355 complete_transaction, orb);
356}
357
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500358static int sbp2_cancel_orbs(struct fw_unit *unit)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500359{
360 struct fw_device *device = fw_device(unit->device.parent);
361 struct sbp2_device *sd = unit->device.driver_data;
362 struct sbp2_orb *orb, *next;
363 struct list_head list;
364 unsigned long flags;
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500365 int retval = -ENOENT;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500366
367 INIT_LIST_HEAD(&list);
368 spin_lock_irqsave(&device->card->lock, flags);
369 list_splice_init(&sd->orb_list, &list);
370 spin_unlock_irqrestore(&device->card->lock, flags);
371
372 list_for_each_entry_safe(orb, next, &list, link) {
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500373 retval = 0;
Kristian Høgsberg730c32f2007-02-06 14:49:32 -0500374 if (fw_cancel_transaction(device->card, &orb->t) == 0)
375 continue;
376
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500377 orb->rcode = RCODE_CANCELLED;
378 orb->callback(orb, NULL);
379 }
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500380
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500381 return retval;
Kristian Høgsberg1d3d52c2007-02-06 14:49:33 -0500382}
383
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500384static void
385complete_management_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
386{
387 struct sbp2_management_orb *orb =
388 (struct sbp2_management_orb *)base_orb;
389
390 if (status)
391 memcpy(&orb->status, status, sizeof *status);
392 complete(&orb->done);
393}
394
395static int
396sbp2_send_management_orb(struct fw_unit *unit, int node_id, int generation,
397 int function, int lun, void *response)
398{
399 struct fw_device *device = fw_device(unit->device.parent);
400 struct sbp2_device *sd = unit->device.driver_data;
401 struct sbp2_management_orb *orb;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500402 int retval = -ENOMEM;
403
404 orb = kzalloc(sizeof *orb, GFP_ATOMIC);
405 if (orb == NULL)
406 return -ENOMEM;
407
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400408 /*
409 * The sbp2 device is going to send a block read request to
410 * read out the request from host memory, so map it for dma.
411 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500412 orb->base.request_bus =
413 dma_map_single(device->card->device, &orb->request,
414 sizeof orb->request, DMA_TO_DEVICE);
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -0500415 if (dma_mapping_error(orb->base.request_bus))
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500416 goto out;
417
418 orb->response_bus =
419 dma_map_single(device->card->device, &orb->response,
420 sizeof orb->response, DMA_FROM_DEVICE);
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -0500421 if (dma_mapping_error(orb->response_bus))
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500422 goto out;
423
424 orb->request.response.high = 0;
425 orb->request.response.low = orb->response_bus;
426
427 orb->request.misc =
428 management_orb_notify |
429 management_orb_function(function) |
430 management_orb_lun(lun);
431 orb->request.length =
432 management_orb_response_length(sizeof orb->response);
433
434 orb->request.status_fifo.high = sd->address_handler.offset >> 32;
435 orb->request.status_fifo.low = sd->address_handler.offset;
436
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400437 /*
438 * FIXME: Yeah, ok this isn't elegant, we hardwire exclusive
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500439 * login and 1 second reconnect time. The reconnect setting
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400440 * is probably fine, but the exclusive login should be an option.
441 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500442 if (function == SBP2_LOGIN_REQUEST) {
443 orb->request.misc |=
444 management_orb_exclusive |
445 management_orb_reconnect(0);
446 }
447
448 fw_memcpy_to_be32(&orb->request, &orb->request, sizeof orb->request);
449
450 init_completion(&orb->done);
451 orb->base.callback = complete_management_orb;
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500452
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500453 sbp2_send_orb(&orb->base, unit,
454 node_id, generation, sd->management_agent_address);
455
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500456 wait_for_completion_timeout(&orb->done,
457 msecs_to_jiffies(SBP2_ORB_TIMEOUT));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500458
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500459 retval = -EIO;
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500460 if (sbp2_cancel_orbs(unit) == 0) {
461 fw_error("orb reply timed out, rcode=0x%02x\n",
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500462 orb->base.rcode);
463 goto out;
464 }
465
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500466 if (orb->base.rcode != RCODE_COMPLETE) {
467 fw_error("management write failed, rcode 0x%02x\n",
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500468 orb->base.rcode);
469 goto out;
470 }
471
472 if (status_get_response(orb->status) != 0 ||
473 status_get_sbp_status(orb->status) != 0) {
474 fw_error("error status: %d:%d\n",
475 status_get_response(orb->status),
476 status_get_sbp_status(orb->status));
477 goto out;
478 }
479
480 retval = 0;
481 out:
482 dma_unmap_single(device->card->device, orb->base.request_bus,
483 sizeof orb->request, DMA_TO_DEVICE);
484 dma_unmap_single(device->card->device, orb->response_bus,
485 sizeof orb->response, DMA_FROM_DEVICE);
486
487 if (response)
488 fw_memcpy_from_be32(response,
489 orb->response, sizeof orb->response);
490 kfree(orb);
491
492 return retval;
493}
494
495static void
496complete_agent_reset_write(struct fw_card *card, int rcode,
497 void *payload, size_t length, void *data)
498{
499 struct fw_transaction *t = data;
500
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500501 kfree(t);
502}
503
504static int sbp2_agent_reset(struct fw_unit *unit)
505{
506 struct fw_device *device = fw_device(unit->device.parent);
507 struct sbp2_device *sd = unit->device.driver_data;
508 struct fw_transaction *t;
509 static u32 zero;
510
511 t = kzalloc(sizeof *t, GFP_ATOMIC);
512 if (t == NULL)
513 return -ENOMEM;
514
515 fw_send_request(device->card, t, TCODE_WRITE_QUADLET_REQUEST,
Stefan Richter907293d2007-01-23 21:11:43 +0100516 sd->node_id, sd->generation, SCODE_400,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500517 sd->command_block_agent_address + SBP2_AGENT_RESET,
518 &zero, sizeof zero, complete_agent_reset_write, t);
519
520 return 0;
521}
522
523static int add_scsi_devices(struct fw_unit *unit);
524static void remove_scsi_devices(struct fw_unit *unit);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500525static void sbp2_reconnect(struct work_struct *work);
526
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400527static void
528release_sbp2_device(struct kref *kref)
529{
530 struct sbp2_device *sd = container_of(kref, struct sbp2_device, kref);
531
532 sbp2_send_management_orb(sd->unit, sd->node_id, sd->generation,
533 SBP2_LOGOUT_REQUEST, sd->login_id, NULL);
534
535 remove_scsi_devices(sd->unit);
536
537 fw_core_remove_address_handler(&sd->address_handler);
538 fw_notify("removed sbp2 unit %s\n", sd->unit->device.bus_id);
539 put_device(&sd->unit->device);
540 kfree(sd);
541}
542
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500543static void sbp2_login(struct work_struct *work)
544{
545 struct sbp2_device *sd =
546 container_of(work, struct sbp2_device, work.work);
547 struct fw_unit *unit = sd->unit;
548 struct fw_device *device = fw_device(unit->device.parent);
549 struct sbp2_login_response response;
550 int generation, node_id, local_node_id, lun, retval;
551
552 /* FIXME: Make this work for multi-lun devices. */
553 lun = 0;
554
555 generation = device->card->generation;
556 node_id = device->node->node_id;
557 local_node_id = device->card->local_node->node_id;
558
559 if (sbp2_send_management_orb(unit, node_id, generation,
560 SBP2_LOGIN_REQUEST, lun, &response) < 0) {
561 if (sd->retries++ < 5) {
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500562 schedule_delayed_work(&sd->work, DIV_ROUND_UP(HZ, 5));
563 } else {
564 fw_error("failed to login to %s\n",
565 unit->device.bus_id);
566 remove_scsi_devices(unit);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400567 kref_put(&sd->kref, release_sbp2_device);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500568 }
569 return;
570 }
571
572 sd->generation = generation;
573 sd->node_id = node_id;
574 sd->address_high = local_node_id << 16;
575
576 /* Get command block agent offset and login id. */
577 sd->command_block_agent_address =
Kristian Høgsberg5c5539d2007-03-07 12:12:45 -0500578 ((u64) (response.command_block_agent.high & 0xffff) << 32) |
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500579 response.command_block_agent.low;
580 sd->login_id = login_response_get_login_id(response);
581
Kristian Høgsberg5c5539d2007-03-07 12:12:45 -0500582 fw_notify("logged in to sbp2 unit %s (%d retries)\n",
583 unit->device.bus_id, sd->retries);
584 fw_notify(" - management_agent_address: 0x%012llx\n",
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500585 (unsigned long long) sd->management_agent_address);
586 fw_notify(" - command_block_agent_address: 0x%012llx\n",
587 (unsigned long long) sd->command_block_agent_address);
Kristian Høgsberg5c5539d2007-03-07 12:12:45 -0500588 fw_notify(" - status write address: 0x%012llx\n",
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500589 (unsigned long long) sd->address_handler.offset);
590
591#if 0
592 /* FIXME: The linux1394 sbp2 does this last step. */
593 sbp2_set_busy_timeout(scsi_id);
594#endif
595
Kristian Høgsberg1da0c932007-03-07 12:12:40 -0500596 PREPARE_DELAYED_WORK(&sd->work, sbp2_reconnect);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500597 sbp2_agent_reset(unit);
598
599 retval = add_scsi_devices(unit);
600 if (retval < 0) {
601 sbp2_send_management_orb(unit, sd->node_id, sd->generation,
602 SBP2_LOGOUT_REQUEST, sd->login_id,
603 NULL);
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400604 /*
605 * Set this back to sbp2_login so we fall back and
606 * retry login on bus reset.
607 */
Kristian Høgsberg1da0c932007-03-07 12:12:40 -0500608 PREPARE_DELAYED_WORK(&sd->work, sbp2_login);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500609 }
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400610 kref_put(&sd->kref, release_sbp2_device);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500611}
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500612
613static int sbp2_probe(struct device *dev)
614{
615 struct fw_unit *unit = fw_unit(dev);
616 struct fw_device *device = fw_device(unit->device.parent);
617 struct sbp2_device *sd;
618 struct fw_csr_iterator ci;
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500619 int i, key, value;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500620 u32 model, firmware_revision;
621
622 sd = kzalloc(sizeof *sd, GFP_KERNEL);
623 if (sd == NULL)
624 return -ENOMEM;
625
626 unit->device.driver_data = sd;
627 sd->unit = unit;
628 INIT_LIST_HEAD(&sd->orb_list);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400629 kref_init(&sd->kref);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500630
631 sd->address_handler.length = 0x100;
632 sd->address_handler.address_callback = sbp2_status_write;
633 sd->address_handler.callback_data = sd;
634
635 if (fw_core_add_address_handler(&sd->address_handler,
636 &fw_high_memory_region) < 0) {
637 kfree(sd);
638 return -EBUSY;
639 }
640
641 if (fw_device_enable_phys_dma(device) < 0) {
642 fw_core_remove_address_handler(&sd->address_handler);
643 kfree(sd);
644 return -EBUSY;
645 }
646
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400647 /*
648 * Scan unit directory to get management agent address,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500649 * firmware revison and model. Initialize firmware_revision
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400650 * and model to values that wont match anything in our table.
651 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500652 firmware_revision = 0xff000000;
653 model = 0xff000000;
654 fw_csr_iterator_init(&ci, unit->directory);
655 while (fw_csr_iterator_next(&ci, &key, &value)) {
656 switch (key) {
657 case CSR_DEPENDENT_INFO | CSR_OFFSET:
658 sd->management_agent_address =
659 0xfffff0000000ULL + 4 * value;
660 break;
661 case SBP2_FIRMWARE_REVISION:
662 firmware_revision = value;
663 break;
664 case CSR_MODEL:
665 model = value;
666 break;
667 }
668 }
669
670 for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
671 if (sbp2_workarounds_table[i].firmware_revision !=
672 (firmware_revision & 0xffffff00))
673 continue;
674 if (sbp2_workarounds_table[i].model != model &&
675 sbp2_workarounds_table[i].model != ~0)
676 continue;
677 sd->workarounds |= sbp2_workarounds_table[i].workarounds;
678 break;
679 }
680
681 if (sd->workarounds)
682 fw_notify("Workarounds for node %s: 0x%x "
683 "(firmware_revision 0x%06x, model_id 0x%06x)\n",
684 unit->device.bus_id,
685 sd->workarounds, firmware_revision, model);
686
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400687 get_device(&unit->device);
688
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400689 /*
690 * We schedule work to do the login so we can easily
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400691 * reschedule retries. Always get the ref before scheduling
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400692 * work.
693 */
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500694 INIT_DELAYED_WORK(&sd->work, sbp2_login);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400695 if (schedule_delayed_work(&sd->work, 0))
696 kref_get(&sd->kref);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500697
698 return 0;
699}
700
701static int sbp2_remove(struct device *dev)
702{
703 struct fw_unit *unit = fw_unit(dev);
704 struct sbp2_device *sd = unit->device.driver_data;
705
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400706 kref_put(&sd->kref, release_sbp2_device);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500707
708 return 0;
709}
710
711static void sbp2_reconnect(struct work_struct *work)
712{
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500713 struct sbp2_device *sd =
714 container_of(work, struct sbp2_device, work.work);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500715 struct fw_unit *unit = sd->unit;
716 struct fw_device *device = fw_device(unit->device.parent);
717 int generation, node_id, local_node_id;
718
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500719 generation = device->card->generation;
720 node_id = device->node->node_id;
721 local_node_id = device->card->local_node->node_id;
722
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500723 if (sbp2_send_management_orb(unit, node_id, generation,
724 SBP2_RECONNECT_REQUEST,
725 sd->login_id, NULL) < 0) {
Kristian Høgsberg5c5539d2007-03-07 12:12:45 -0500726 if (sd->retries++ >= 5) {
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500727 fw_error("failed to reconnect to %s\n",
728 unit->device.bus_id);
729 /* Fall back and try to log in again. */
730 sd->retries = 0;
Kristian Høgsberg1da0c932007-03-07 12:12:40 -0500731 PREPARE_DELAYED_WORK(&sd->work, sbp2_login);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500732 }
733 schedule_delayed_work(&sd->work, DIV_ROUND_UP(HZ, 5));
734 return;
735 }
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500736
737 sd->generation = generation;
738 sd->node_id = node_id;
Stefan Richter907293d2007-01-23 21:11:43 +0100739 sd->address_high = local_node_id << 16;
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500740
Kristian Høgsberg5c5539d2007-03-07 12:12:45 -0500741 fw_notify("reconnected to unit %s (%d retries)\n",
742 unit->device.bus_id, sd->retries);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500743 sbp2_agent_reset(unit);
744 sbp2_cancel_orbs(unit);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400745 kref_put(&sd->kref, release_sbp2_device);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500746}
747
748static void sbp2_update(struct fw_unit *unit)
749{
750 struct fw_device *device = fw_device(unit->device.parent);
751 struct sbp2_device *sd = unit->device.driver_data;
752
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500753 sd->retries = 0;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500754 fw_device_enable_phys_dma(device);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400755 if (schedule_delayed_work(&sd->work, 0))
756 kref_get(&sd->kref);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500757}
758
759#define SBP2_UNIT_SPEC_ID_ENTRY 0x0000609e
760#define SBP2_SW_VERSION_ENTRY 0x00010483
761
Stefan Richter21ebcd12007-01-14 15:29:07 +0100762static const struct fw_device_id sbp2_id_table[] = {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500763 {
764 .match_flags = FW_MATCH_SPECIFIER_ID | FW_MATCH_VERSION,
765 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY,
Stefan Richter5af4e5e2007-01-21 20:45:32 +0100766 .version = SBP2_SW_VERSION_ENTRY,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500767 },
768 { }
769};
770
771static struct fw_driver sbp2_driver = {
772 .driver = {
773 .owner = THIS_MODULE,
774 .name = sbp2_driver_name,
775 .bus = &fw_bus_type,
776 .probe = sbp2_probe,
777 .remove = sbp2_remove,
778 },
779 .update = sbp2_update,
780 .id_table = sbp2_id_table,
781};
782
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400783static unsigned int
784sbp2_status_to_sense_data(u8 *sbp2_status, u8 *sense_data)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500785{
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400786 int sam_status;
787
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500788 sense_data[0] = 0x70;
789 sense_data[1] = 0x0;
790 sense_data[2] = sbp2_status[1];
791 sense_data[3] = sbp2_status[4];
792 sense_data[4] = sbp2_status[5];
793 sense_data[5] = sbp2_status[6];
794 sense_data[6] = sbp2_status[7];
795 sense_data[7] = 10;
796 sense_data[8] = sbp2_status[8];
797 sense_data[9] = sbp2_status[9];
798 sense_data[10] = sbp2_status[10];
799 sense_data[11] = sbp2_status[11];
800 sense_data[12] = sbp2_status[2];
801 sense_data[13] = sbp2_status[3];
802 sense_data[14] = sbp2_status[12];
803 sense_data[15] = sbp2_status[13];
804
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400805 sam_status = sbp2_status[0] & 0x3f;
806
807 switch (sam_status) {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500808 case SAM_STAT_GOOD:
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500809 case SAM_STAT_CHECK_CONDITION:
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500810 case SAM_STAT_CONDITION_MET:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400811 case SAM_STAT_BUSY:
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500812 case SAM_STAT_RESERVATION_CONFLICT:
813 case SAM_STAT_COMMAND_TERMINATED:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400814 return DID_OK << 16 | sam_status;
815
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500816 default:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400817 return DID_ERROR << 16;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500818 }
819}
820
821static void
822complete_command_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
823{
824 struct sbp2_command_orb *orb = (struct sbp2_command_orb *)base_orb;
825 struct fw_unit *unit = orb->unit;
826 struct fw_device *device = fw_device(unit->device.parent);
827 struct scatterlist *sg;
828 int result;
829
830 if (status != NULL) {
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400831 if (status_get_dead(*status))
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500832 sbp2_agent_reset(unit);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500833
834 switch (status_get_response(*status)) {
835 case SBP2_STATUS_REQUEST_COMPLETE:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400836 result = DID_OK << 16;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500837 break;
838 case SBP2_STATUS_TRANSPORT_FAILURE:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400839 result = DID_BUS_BUSY << 16;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500840 break;
841 case SBP2_STATUS_ILLEGAL_REQUEST:
842 case SBP2_STATUS_VENDOR_DEPENDENT:
843 default:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400844 result = DID_ERROR << 16;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500845 break;
846 }
847
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400848 if (result == DID_OK << 16 && status_get_len(*status) > 1)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500849 result = sbp2_status_to_sense_data(status_get_data(*status),
850 orb->cmd->sense_buffer);
851 } else {
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400852 /*
853 * If the orb completes with status == NULL, something
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500854 * went wrong, typically a bus reset happened mid-orb
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400855 * or when sending the write (less likely).
856 */
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400857 result = DID_BUS_BUSY << 16;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500858 }
859
860 dma_unmap_single(device->card->device, orb->base.request_bus,
861 sizeof orb->request, DMA_TO_DEVICE);
862
863 if (orb->cmd->use_sg > 0) {
864 sg = (struct scatterlist *)orb->cmd->request_buffer;
865 dma_unmap_sg(device->card->device, sg, orb->cmd->use_sg,
866 orb->cmd->sc_data_direction);
867 }
868
869 if (orb->page_table_bus != 0)
870 dma_unmap_single(device->card->device, orb->page_table_bus,
871 sizeof orb->page_table_bus, DMA_TO_DEVICE);
872
873 if (orb->request_buffer_bus != 0)
874 dma_unmap_single(device->card->device, orb->request_buffer_bus,
875 sizeof orb->request_buffer_bus,
876 DMA_FROM_DEVICE);
877
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400878 orb->cmd->result = result;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500879 orb->done(orb->cmd);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500880 kfree(orb);
881}
882
883static void sbp2_command_orb_map_scatterlist(struct sbp2_command_orb *orb)
884{
885 struct fw_unit *unit =
886 (struct fw_unit *)orb->cmd->device->host->hostdata[0];
887 struct fw_device *device = fw_device(unit->device.parent);
888 struct sbp2_device *sd = unit->device.driver_data;
889 struct scatterlist *sg;
890 int sg_len, l, i, j, count;
891 size_t size;
892 dma_addr_t sg_addr;
893
894 sg = (struct scatterlist *)orb->cmd->request_buffer;
895 count = dma_map_sg(device->card->device, sg, orb->cmd->use_sg,
896 orb->cmd->sc_data_direction);
897
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400898 /*
899 * Handle the special case where there is only one element in
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500900 * the scatter list by converting it to an immediate block
901 * request. This is also a workaround for broken devices such
902 * as the second generation iPod which doesn't support page
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400903 * tables.
904 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500905 if (count == 1 && sg_dma_len(sg) < SBP2_MAX_SG_ELEMENT_LENGTH) {
906 orb->request.data_descriptor.high = sd->address_high;
907 orb->request.data_descriptor.low = sg_dma_address(sg);
908 orb->request.misc |=
909 command_orb_data_size(sg_dma_len(sg));
910 return;
911 }
912
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400913 /*
914 * Convert the scatterlist to an sbp2 page table. If any
915 * scatterlist entries are too big for sbp2 we split the as we go.
916 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500917 for (i = 0, j = 0; i < count; i++) {
918 sg_len = sg_dma_len(sg + i);
919 sg_addr = sg_dma_address(sg + i);
920 while (sg_len) {
921 l = min(sg_len, SBP2_MAX_SG_ELEMENT_LENGTH);
922 orb->page_table[j].low = sg_addr;
923 orb->page_table[j].high = (l << 16);
924 sg_addr += l;
925 sg_len -= l;
926 j++;
927 }
928 }
929
930 size = sizeof orb->page_table[0] * j;
931
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400932 /*
933 * The data_descriptor pointer is the one case where we need
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500934 * to fill in the node ID part of the address. All other
935 * pointers assume that the data referenced reside on the
936 * initiator (i.e. us), but data_descriptor can refer to data
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400937 * on other nodes so we need to put our ID in descriptor.high.
938 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500939
940 orb->page_table_bus =
941 dma_map_single(device->card->device, orb->page_table,
942 size, DMA_TO_DEVICE);
943 orb->request.data_descriptor.high = sd->address_high;
944 orb->request.data_descriptor.low = orb->page_table_bus;
945 orb->request.misc |=
946 command_orb_page_table_present |
947 command_orb_data_size(j);
948
949 fw_memcpy_to_be32(orb->page_table, orb->page_table, size);
950}
951
952static void sbp2_command_orb_map_buffer(struct sbp2_command_orb *orb)
953{
954 struct fw_unit *unit =
955 (struct fw_unit *)orb->cmd->device->host->hostdata[0];
956 struct fw_device *device = fw_device(unit->device.parent);
957 struct sbp2_device *sd = unit->device.driver_data;
958
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400959 /*
960 * As for map_scatterlist, we need to fill in the high bits of
961 * the data_descriptor pointer.
962 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500963
964 orb->request_buffer_bus =
965 dma_map_single(device->card->device,
966 orb->cmd->request_buffer,
967 orb->cmd->request_bufflen,
968 orb->cmd->sc_data_direction);
969 orb->request.data_descriptor.high = sd->address_high;
970 orb->request.data_descriptor.low = orb->request_buffer_bus;
971 orb->request.misc |=
972 command_orb_data_size(orb->cmd->request_bufflen);
973}
974
975/* SCSI stack integration */
976
977static int sbp2_scsi_queuecommand(struct scsi_cmnd *cmd, scsi_done_fn_t done)
978{
979 struct fw_unit *unit = (struct fw_unit *)cmd->device->host->hostdata[0];
980 struct fw_device *device = fw_device(unit->device.parent);
981 struct sbp2_device *sd = unit->device.driver_data;
982 struct sbp2_command_orb *orb;
983
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400984 /*
985 * Bidirectional commands are not yet implemented, and unknown
986 * transfer direction not handled.
987 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500988 if (cmd->sc_data_direction == DMA_BIDIRECTIONAL) {
989 fw_error("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -0500990 goto fail_alloc;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500991 }
992
993 orb = kzalloc(sizeof *orb, GFP_ATOMIC);
994 if (orb == NULL) {
995 fw_notify("failed to alloc orb\n");
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -0500996 goto fail_alloc;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500997 }
998
Kristian Høgsberg12f26aa2007-04-10 18:11:20 -0400999 /* Initialize rcode to something not RCODE_COMPLETE. */
1000 orb->base.rcode = -1;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001001 orb->base.request_bus =
1002 dma_map_single(device->card->device, &orb->request,
1003 sizeof orb->request, DMA_TO_DEVICE);
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -05001004 if (dma_mapping_error(orb->base.request_bus))
1005 goto fail_mapping;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001006
1007 orb->unit = unit;
1008 orb->done = done;
1009 orb->cmd = cmd;
1010
1011 orb->request.next.high = SBP2_ORB_NULL;
1012 orb->request.next.low = 0x0;
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001013 /*
1014 * At speed 100 we can do 512 bytes per packet, at speed 200,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001015 * 1024 bytes per packet etc. The SBP-2 max_payload field
1016 * specifies the max payload size as 2 ^ (max_payload + 2), so
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001017 * if we set this to max_speed + 7, we get the right value.
1018 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001019 orb->request.misc =
1020 command_orb_max_payload(device->node->max_speed + 7) |
1021 command_orb_speed(device->node->max_speed) |
1022 command_orb_notify;
1023
1024 if (cmd->sc_data_direction == DMA_FROM_DEVICE)
1025 orb->request.misc |=
1026 command_orb_direction(SBP2_DIRECTION_FROM_MEDIA);
1027 else if (cmd->sc_data_direction == DMA_TO_DEVICE)
1028 orb->request.misc |=
1029 command_orb_direction(SBP2_DIRECTION_TO_MEDIA);
1030
1031 if (cmd->use_sg) {
1032 sbp2_command_orb_map_scatterlist(orb);
1033 } else if (cmd->request_bufflen > SBP2_MAX_SG_ELEMENT_LENGTH) {
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001034 /*
1035 * FIXME: Need to split this into a sg list... but
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001036 * could we get the scsi or blk layer to do that by
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001037 * reporting our max supported block size?
1038 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001039 fw_error("command > 64k\n");
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -05001040 goto fail_bufflen;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001041 } else if (cmd->request_bufflen > 0) {
1042 sbp2_command_orb_map_buffer(orb);
1043 }
1044
1045 fw_memcpy_to_be32(&orb->request, &orb->request, sizeof orb->request);
1046
1047 memset(orb->request.command_block,
1048 0, sizeof orb->request.command_block);
1049 memcpy(orb->request.command_block, cmd->cmnd, COMMAND_SIZE(*cmd->cmnd));
1050
1051 orb->base.callback = complete_command_orb;
1052
1053 sbp2_send_orb(&orb->base, unit, sd->node_id, sd->generation,
1054 sd->command_block_agent_address + SBP2_ORB_POINTER);
1055
1056 return 0;
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -05001057
1058 fail_bufflen:
1059 dma_unmap_single(device->card->device, orb->base.request_bus,
1060 sizeof orb->request, DMA_TO_DEVICE);
1061 fail_mapping:
1062 kfree(orb);
1063 fail_alloc:
1064 cmd->result = DID_ERROR << 16;
1065 done(cmd);
1066 return 0;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001067}
1068
Stefan Richtercfb01382007-01-23 21:20:08 +01001069static int sbp2_scsi_slave_alloc(struct scsi_device *sdev)
1070{
1071 struct fw_unit *unit = (struct fw_unit *)sdev->host->hostdata[0];
1072 struct sbp2_device *sd = unit->device.driver_data;
1073
1074 sdev->allow_restart = 1;
1075
1076 if (sd->workarounds & SBP2_WORKAROUND_INQUIRY_36)
1077 sdev->inquiry_len = 36;
1078 return 0;
1079}
1080
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001081static int sbp2_scsi_slave_configure(struct scsi_device *sdev)
1082{
1083 struct fw_unit *unit = (struct fw_unit *)sdev->host->hostdata[0];
1084 struct sbp2_device *sd = unit->device.driver_data;
1085
Stefan Richtercfb01382007-01-23 21:20:08 +01001086 sdev->use_10_for_rw = 1;
1087
1088 if (sdev->type == TYPE_ROM)
1089 sdev->use_10_for_ms = 1;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001090 if (sdev->type == TYPE_DISK &&
1091 sd->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
1092 sdev->skip_ms_page_8 = 1;
1093 if (sd->workarounds & SBP2_WORKAROUND_FIX_CAPACITY) {
1094 fw_notify("setting fix_capacity for %s\n", unit->device.bus_id);
1095 sdev->fix_capacity = 1;
1096 }
1097
1098 return 0;
1099}
1100
1101/*
1102 * Called by scsi stack when something has really gone wrong. Usually
1103 * called when a command has timed-out for some reason.
1104 */
1105static int sbp2_scsi_abort(struct scsi_cmnd *cmd)
1106{
1107 struct fw_unit *unit = (struct fw_unit *)cmd->device->host->hostdata[0];
1108
1109 fw_notify("sbp2_scsi_abort\n");
Kristian Høgsberg0fc7d6e2007-04-11 18:44:33 -04001110 sbp2_agent_reset(unit);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001111 sbp2_cancel_orbs(unit);
1112
1113 return SUCCESS;
1114}
1115
1116static struct scsi_host_template scsi_driver_template = {
1117 .module = THIS_MODULE,
1118 .name = "SBP-2 IEEE-1394",
1119 .proc_name = (char *)sbp2_driver_name,
1120 .queuecommand = sbp2_scsi_queuecommand,
Stefan Richtercfb01382007-01-23 21:20:08 +01001121 .slave_alloc = sbp2_scsi_slave_alloc,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001122 .slave_configure = sbp2_scsi_slave_configure,
1123 .eh_abort_handler = sbp2_scsi_abort,
1124 .this_id = -1,
1125 .sg_tablesize = SG_ALL,
1126 .use_clustering = ENABLE_CLUSTERING,
Stefan Richter02af8e72007-01-21 20:50:11 +01001127 .cmd_per_lun = 1,
1128 .can_queue = 1,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001129};
1130
1131static int add_scsi_devices(struct fw_unit *unit)
1132{
1133 struct sbp2_device *sd = unit->device.driver_data;
1134 int retval, lun;
1135
Kristian Høgsberg7f37c422007-02-06 14:49:34 -05001136 if (sd->scsi_host != NULL)
1137 return 0;
1138
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001139 sd->scsi_host = scsi_host_alloc(&scsi_driver_template,
1140 sizeof(unsigned long));
1141 if (sd->scsi_host == NULL) {
1142 fw_error("failed to register scsi host\n");
1143 return -1;
1144 }
1145
1146 sd->scsi_host->hostdata[0] = (unsigned long)unit;
1147 retval = scsi_add_host(sd->scsi_host, &unit->device);
1148 if (retval < 0) {
1149 fw_error("failed to add scsi host\n");
1150 scsi_host_put(sd->scsi_host);
Kristian Høgsberg693b9022007-03-14 17:34:56 -04001151 sd->scsi_host = NULL;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001152 return retval;
1153 }
1154
1155 /* FIXME: Loop over luns here. */
1156 lun = 0;
1157 retval = scsi_add_device(sd->scsi_host, 0, 0, lun);
1158 if (retval < 0) {
1159 fw_error("failed to add scsi device\n");
1160 scsi_remove_host(sd->scsi_host);
1161 scsi_host_put(sd->scsi_host);
Kristian Høgsberg693b9022007-03-14 17:34:56 -04001162 sd->scsi_host = NULL;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001163 return retval;
1164 }
1165
1166 return 0;
1167}
1168
1169static void remove_scsi_devices(struct fw_unit *unit)
1170{
1171 struct sbp2_device *sd = unit->device.driver_data;
1172
Kristian Høgsberg7f37c422007-02-06 14:49:34 -05001173 if (sd->scsi_host != NULL) {
1174 scsi_remove_host(sd->scsi_host);
1175 scsi_host_put(sd->scsi_host);
1176 }
1177 sd->scsi_host = NULL;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001178}
1179
1180MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
1181MODULE_DESCRIPTION("SCSI over IEEE1394");
1182MODULE_LICENSE("GPL");
1183MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
1184
1185static int __init sbp2_init(void)
1186{
1187 return driver_register(&sbp2_driver.driver);
1188}
1189
1190static void __exit sbp2_cleanup(void)
1191{
1192 driver_unregister(&sbp2_driver.driver);
1193}
1194
1195module_init(sbp2_init);
1196module_exit(sbp2_cleanup);