blob: 196de46c00b6f022a57e256cf39362e9a6110e1c [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
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400126#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)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500134
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
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400155#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)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500161
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400162#define MANAGEMENT_ORB_RESPONSE_LENGTH(v) ((v))
163#define MANAGEMENT_ORB_PASSWORD_LENGTH(v) ((v) << 16)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500164
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
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400180#define LOGIN_RESPONSE_GET_LOGIN_ID(v) ((v).misc & 0xffff)
181#define LOGIN_RESPONSE_GET_LENGTH(v) (((v).misc >> 16) & 0xffff)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500182
183struct sbp2_login_response {
184 u32 misc;
185 struct sbp2_pointer command_block_agent;
186 u32 reconnect_hold;
187};
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400188#define COMMAND_ORB_DATA_SIZE(v) ((v))
189#define COMMAND_ORB_PAGE_SIZE(v) ((v) << 16)
190#define COMMAND_ORB_PAGE_TABLE_PRESENT ((1) << 19)
191#define COMMAND_ORB_MAX_PAYLOAD(v) ((v) << 20)
192#define COMMAND_ORB_SPEED(v) ((v) << 24)
193#define COMMAND_ORB_DIRECTION(v) ((v) << 27)
194#define COMMAND_ORB_REQUEST_FORMAT(v) ((v) << 29)
195#define COMMAND_ORB_NOTIFY ((1) << 31)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500196
197struct sbp2_command_orb {
198 struct sbp2_orb base;
199 struct {
200 struct sbp2_pointer next;
201 struct sbp2_pointer data_descriptor;
202 u32 misc;
203 u8 command_block[12];
204 } request;
205 struct scsi_cmnd *cmd;
206 scsi_done_fn_t done;
207 struct fw_unit *unit;
208
209 struct sbp2_pointer page_table[SG_ALL];
210 dma_addr_t page_table_bus;
211 dma_addr_t request_buffer_bus;
212};
213
214/*
215 * List of devices with known bugs.
216 *
217 * The firmware_revision field, masked with 0xffff00, is the best
218 * indicator for the type of bridge chip of a device. It yields a few
219 * false positives but this did not break correctly behaving devices
220 * so far. We use ~0 as a wildcard, since the 24 bit values we get
221 * from the config rom can never match that.
222 */
223static const struct {
224 u32 firmware_revision;
225 u32 model;
226 unsigned workarounds;
227} sbp2_workarounds_table[] = {
228 /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
229 .firmware_revision = 0x002800,
230 .model = 0x001010,
231 .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
232 SBP2_WORKAROUND_MODE_SENSE_8,
233 },
234 /* Initio bridges, actually only needed for some older ones */ {
235 .firmware_revision = 0x000200,
236 .model = ~0,
237 .workarounds = SBP2_WORKAROUND_INQUIRY_36,
238 },
239 /* Symbios bridge */ {
240 .firmware_revision = 0xa0b800,
241 .model = ~0,
242 .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
243 },
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400244
245 /*
246 * There are iPods (2nd gen, 3rd gen) with model_id == 0, but
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500247 * these iPods do not feature the read_capacity bug according
248 * to one report. Read_capacity behaviour as well as model_id
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400249 * could change due to Apple-supplied firmware updates though.
250 */
251
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500252 /* iPod 4th generation. */ {
253 .firmware_revision = 0x0a2700,
254 .model = 0x000021,
255 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
256 },
257 /* iPod mini */ {
258 .firmware_revision = 0x0a2700,
259 .model = 0x000023,
260 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
261 },
262 /* iPod Photo */ {
263 .firmware_revision = 0x0a2700,
264 .model = 0x00007e,
265 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
266 }
267};
268
269static void
270sbp2_status_write(struct fw_card *card, struct fw_request *request,
271 int tcode, int destination, int source,
272 int generation, int speed,
273 unsigned long long offset,
274 void *payload, size_t length, void *callback_data)
275{
276 struct sbp2_device *sd = callback_data;
277 struct sbp2_orb *orb;
278 struct sbp2_status status;
279 size_t header_size;
280 unsigned long flags;
281
282 if (tcode != TCODE_WRITE_BLOCK_REQUEST ||
283 length == 0 || length > sizeof status) {
284 fw_send_response(card, request, RCODE_TYPE_ERROR);
285 return;
286 }
287
288 header_size = min(length, 2 * sizeof(u32));
289 fw_memcpy_from_be32(&status, payload, header_size);
290 if (length > header_size)
291 memcpy(status.data, payload + 8, length - header_size);
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400292 if (STATUS_GET_SOURCE(status) == 2 || STATUS_GET_SOURCE(status) == 3) {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500293 fw_notify("non-orb related status write, not handled\n");
294 fw_send_response(card, request, RCODE_COMPLETE);
295 return;
296 }
297
298 /* Lookup the orb corresponding to this status write. */
299 spin_lock_irqsave(&card->lock, flags);
300 list_for_each_entry(orb, &sd->orb_list, link) {
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400301 if (STATUS_GET_ORB_HIGH(status) == 0 &&
302 STATUS_GET_ORB_LOW(status) == orb->request_bus &&
Kristian Høgsberg12f26aa2007-04-10 18:11:20 -0400303 orb->rcode == RCODE_COMPLETE) {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500304 list_del(&orb->link);
305 break;
306 }
307 }
308 spin_unlock_irqrestore(&card->lock, flags);
309
310 if (&orb->link != &sd->orb_list)
311 orb->callback(orb, &status);
312 else
313 fw_error("status write for unknown orb\n");
314
315 fw_send_response(card, request, RCODE_COMPLETE);
316}
317
318static void
319complete_transaction(struct fw_card *card, int rcode,
320 void *payload, size_t length, void *data)
321{
322 struct sbp2_orb *orb = data;
323 unsigned long flags;
324
325 orb->rcode = rcode;
326 if (rcode != RCODE_COMPLETE) {
327 spin_lock_irqsave(&card->lock, flags);
328 list_del(&orb->link);
329 spin_unlock_irqrestore(&card->lock, flags);
330 orb->callback(orb, NULL);
331 }
332}
333
334static void
335sbp2_send_orb(struct sbp2_orb *orb, struct fw_unit *unit,
336 int node_id, int generation, u64 offset)
337{
338 struct fw_device *device = fw_device(unit->device.parent);
339 struct sbp2_device *sd = unit->device.driver_data;
340 unsigned long flags;
341
342 orb->pointer.high = 0;
343 orb->pointer.low = orb->request_bus;
344 fw_memcpy_to_be32(&orb->pointer, &orb->pointer, sizeof orb->pointer);
345
346 spin_lock_irqsave(&device->card->lock, flags);
347 list_add_tail(&orb->link, &sd->orb_list);
348 spin_unlock_irqrestore(&device->card->lock, flags);
349
350 fw_send_request(device->card, &orb->t, TCODE_WRITE_BLOCK_REQUEST,
Stefan Richter907293d2007-01-23 21:11:43 +0100351 node_id, generation,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500352 device->node->max_speed, offset,
353 &orb->pointer, sizeof orb->pointer,
354 complete_transaction, orb);
355}
356
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500357static int sbp2_cancel_orbs(struct fw_unit *unit)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500358{
359 struct fw_device *device = fw_device(unit->device.parent);
360 struct sbp2_device *sd = unit->device.driver_data;
361 struct sbp2_orb *orb, *next;
362 struct list_head list;
363 unsigned long flags;
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500364 int retval = -ENOENT;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500365
366 INIT_LIST_HEAD(&list);
367 spin_lock_irqsave(&device->card->lock, flags);
368 list_splice_init(&sd->orb_list, &list);
369 spin_unlock_irqrestore(&device->card->lock, flags);
370
371 list_for_each_entry_safe(orb, next, &list, link) {
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500372 retval = 0;
Kristian Høgsberg730c32f2007-02-06 14:49:32 -0500373 if (fw_cancel_transaction(device->card, &orb->t) == 0)
374 continue;
375
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500376 orb->rcode = RCODE_CANCELLED;
377 orb->callback(orb, NULL);
378 }
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500379
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500380 return retval;
Kristian Høgsberg1d3d52c2007-02-06 14:49:33 -0500381}
382
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500383static void
384complete_management_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
385{
386 struct sbp2_management_orb *orb =
387 (struct sbp2_management_orb *)base_orb;
388
389 if (status)
390 memcpy(&orb->status, status, sizeof *status);
391 complete(&orb->done);
392}
393
394static int
395sbp2_send_management_orb(struct fw_unit *unit, int node_id, int generation,
396 int function, int lun, void *response)
397{
398 struct fw_device *device = fw_device(unit->device.parent);
399 struct sbp2_device *sd = unit->device.driver_data;
400 struct sbp2_management_orb *orb;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500401 int retval = -ENOMEM;
402
403 orb = kzalloc(sizeof *orb, GFP_ATOMIC);
404 if (orb == NULL)
405 return -ENOMEM;
406
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400407 /*
408 * The sbp2 device is going to send a block read request to
409 * read out the request from host memory, so map it for dma.
410 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500411 orb->base.request_bus =
412 dma_map_single(device->card->device, &orb->request,
413 sizeof orb->request, DMA_TO_DEVICE);
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -0500414 if (dma_mapping_error(orb->base.request_bus))
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500415 goto out;
416
417 orb->response_bus =
418 dma_map_single(device->card->device, &orb->response,
419 sizeof orb->response, DMA_FROM_DEVICE);
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -0500420 if (dma_mapping_error(orb->response_bus))
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500421 goto out;
422
423 orb->request.response.high = 0;
424 orb->request.response.low = orb->response_bus;
425
426 orb->request.misc =
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400427 MANAGEMENT_ORB_NOTIFY |
428 MANAGEMENT_ORB_FUNCTION(function) |
429 MANAGEMENT_ORB_LUN(lun);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500430 orb->request.length =
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400431 MANAGEMENT_ORB_RESPONSE_LENGTH(sizeof orb->response);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500432
433 orb->request.status_fifo.high = sd->address_handler.offset >> 32;
434 orb->request.status_fifo.low = sd->address_handler.offset;
435
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400436 /*
437 * FIXME: Yeah, ok this isn't elegant, we hardwire exclusive
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500438 * login and 1 second reconnect time. The reconnect setting
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400439 * is probably fine, but the exclusive login should be an option.
440 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500441 if (function == SBP2_LOGIN_REQUEST) {
442 orb->request.misc |=
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400443 MANAGEMENT_ORB_EXCLUSIVE |
444 MANAGEMENT_ORB_RECONNECT(0);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500445 }
446
447 fw_memcpy_to_be32(&orb->request, &orb->request, sizeof orb->request);
448
449 init_completion(&orb->done);
450 orb->base.callback = complete_management_orb;
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500451
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500452 sbp2_send_orb(&orb->base, unit,
453 node_id, generation, sd->management_agent_address);
454
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500455 wait_for_completion_timeout(&orb->done,
456 msecs_to_jiffies(SBP2_ORB_TIMEOUT));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500457
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500458 retval = -EIO;
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500459 if (sbp2_cancel_orbs(unit) == 0) {
460 fw_error("orb reply timed out, rcode=0x%02x\n",
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500461 orb->base.rcode);
462 goto out;
463 }
464
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500465 if (orb->base.rcode != RCODE_COMPLETE) {
466 fw_error("management write failed, rcode 0x%02x\n",
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500467 orb->base.rcode);
468 goto out;
469 }
470
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400471 if (STATUS_GET_RESPONSE(orb->status) != 0 ||
472 STATUS_GET_SBP_STATUS(orb->status) != 0) {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500473 fw_error("error status: %d:%d\n",
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400474 STATUS_GET_RESPONSE(orb->status),
475 STATUS_GET_SBP_STATUS(orb->status));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500476 goto out;
477 }
478
479 retval = 0;
480 out:
481 dma_unmap_single(device->card->device, orb->base.request_bus,
482 sizeof orb->request, DMA_TO_DEVICE);
483 dma_unmap_single(device->card->device, orb->response_bus,
484 sizeof orb->response, DMA_FROM_DEVICE);
485
486 if (response)
487 fw_memcpy_from_be32(response,
488 orb->response, sizeof orb->response);
489 kfree(orb);
490
491 return retval;
492}
493
494static void
495complete_agent_reset_write(struct fw_card *card, int rcode,
496 void *payload, size_t length, void *data)
497{
498 struct fw_transaction *t = data;
499
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500500 kfree(t);
501}
502
503static int sbp2_agent_reset(struct fw_unit *unit)
504{
505 struct fw_device *device = fw_device(unit->device.parent);
506 struct sbp2_device *sd = unit->device.driver_data;
507 struct fw_transaction *t;
508 static u32 zero;
509
510 t = kzalloc(sizeof *t, GFP_ATOMIC);
511 if (t == NULL)
512 return -ENOMEM;
513
514 fw_send_request(device->card, t, TCODE_WRITE_QUADLET_REQUEST,
Stefan Richter907293d2007-01-23 21:11:43 +0100515 sd->node_id, sd->generation, SCODE_400,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500516 sd->command_block_agent_address + SBP2_AGENT_RESET,
517 &zero, sizeof zero, complete_agent_reset_write, t);
518
519 return 0;
520}
521
522static int add_scsi_devices(struct fw_unit *unit);
523static void remove_scsi_devices(struct fw_unit *unit);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500524static void sbp2_reconnect(struct work_struct *work);
525
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400526static void
527release_sbp2_device(struct kref *kref)
528{
529 struct sbp2_device *sd = container_of(kref, struct sbp2_device, kref);
530
531 sbp2_send_management_orb(sd->unit, sd->node_id, sd->generation,
532 SBP2_LOGOUT_REQUEST, sd->login_id, NULL);
533
534 remove_scsi_devices(sd->unit);
535
536 fw_core_remove_address_handler(&sd->address_handler);
537 fw_notify("removed sbp2 unit %s\n", sd->unit->device.bus_id);
538 put_device(&sd->unit->device);
539 kfree(sd);
540}
541
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500542static void sbp2_login(struct work_struct *work)
543{
544 struct sbp2_device *sd =
545 container_of(work, struct sbp2_device, work.work);
546 struct fw_unit *unit = sd->unit;
547 struct fw_device *device = fw_device(unit->device.parent);
548 struct sbp2_login_response response;
549 int generation, node_id, local_node_id, lun, retval;
550
551 /* FIXME: Make this work for multi-lun devices. */
552 lun = 0;
553
554 generation = device->card->generation;
555 node_id = device->node->node_id;
556 local_node_id = device->card->local_node->node_id;
557
558 if (sbp2_send_management_orb(unit, node_id, generation,
559 SBP2_LOGIN_REQUEST, lun, &response) < 0) {
560 if (sd->retries++ < 5) {
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500561 schedule_delayed_work(&sd->work, DIV_ROUND_UP(HZ, 5));
562 } else {
563 fw_error("failed to login to %s\n",
564 unit->device.bus_id);
565 remove_scsi_devices(unit);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400566 kref_put(&sd->kref, release_sbp2_device);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500567 }
568 return;
569 }
570
571 sd->generation = generation;
572 sd->node_id = node_id;
573 sd->address_high = local_node_id << 16;
574
575 /* Get command block agent offset and login id. */
576 sd->command_block_agent_address =
Kristian Høgsberg5c5539d2007-03-07 12:12:45 -0500577 ((u64) (response.command_block_agent.high & 0xffff) << 32) |
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500578 response.command_block_agent.low;
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400579 sd->login_id = LOGIN_RESPONSE_GET_LOGIN_ID(response);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500580
Kristian Høgsberg5c5539d2007-03-07 12:12:45 -0500581 fw_notify("logged in to sbp2 unit %s (%d retries)\n",
582 unit->device.bus_id, sd->retries);
583 fw_notify(" - management_agent_address: 0x%012llx\n",
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500584 (unsigned long long) sd->management_agent_address);
585 fw_notify(" - command_block_agent_address: 0x%012llx\n",
586 (unsigned long long) sd->command_block_agent_address);
Kristian Høgsberg5c5539d2007-03-07 12:12:45 -0500587 fw_notify(" - status write address: 0x%012llx\n",
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500588 (unsigned long long) sd->address_handler.offset);
589
590#if 0
591 /* FIXME: The linux1394 sbp2 does this last step. */
592 sbp2_set_busy_timeout(scsi_id);
593#endif
594
Kristian Høgsberg1da0c932007-03-07 12:12:40 -0500595 PREPARE_DELAYED_WORK(&sd->work, sbp2_reconnect);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500596 sbp2_agent_reset(unit);
597
598 retval = add_scsi_devices(unit);
599 if (retval < 0) {
600 sbp2_send_management_orb(unit, sd->node_id, sd->generation,
601 SBP2_LOGOUT_REQUEST, sd->login_id,
602 NULL);
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400603 /*
604 * Set this back to sbp2_login so we fall back and
605 * retry login on bus reset.
606 */
Kristian Høgsberg1da0c932007-03-07 12:12:40 -0500607 PREPARE_DELAYED_WORK(&sd->work, sbp2_login);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500608 }
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400609 kref_put(&sd->kref, release_sbp2_device);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500610}
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500611
612static int sbp2_probe(struct device *dev)
613{
614 struct fw_unit *unit = fw_unit(dev);
615 struct fw_device *device = fw_device(unit->device.parent);
616 struct sbp2_device *sd;
617 struct fw_csr_iterator ci;
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500618 int i, key, value;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500619 u32 model, firmware_revision;
620
621 sd = kzalloc(sizeof *sd, GFP_KERNEL);
622 if (sd == NULL)
623 return -ENOMEM;
624
625 unit->device.driver_data = sd;
626 sd->unit = unit;
627 INIT_LIST_HEAD(&sd->orb_list);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400628 kref_init(&sd->kref);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500629
630 sd->address_handler.length = 0x100;
631 sd->address_handler.address_callback = sbp2_status_write;
632 sd->address_handler.callback_data = sd;
633
634 if (fw_core_add_address_handler(&sd->address_handler,
635 &fw_high_memory_region) < 0) {
636 kfree(sd);
637 return -EBUSY;
638 }
639
640 if (fw_device_enable_phys_dma(device) < 0) {
641 fw_core_remove_address_handler(&sd->address_handler);
642 kfree(sd);
643 return -EBUSY;
644 }
645
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400646 /*
647 * Scan unit directory to get management agent address,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500648 * firmware revison and model. Initialize firmware_revision
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400649 * and model to values that wont match anything in our table.
650 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500651 firmware_revision = 0xff000000;
652 model = 0xff000000;
653 fw_csr_iterator_init(&ci, unit->directory);
654 while (fw_csr_iterator_next(&ci, &key, &value)) {
655 switch (key) {
656 case CSR_DEPENDENT_INFO | CSR_OFFSET:
657 sd->management_agent_address =
658 0xfffff0000000ULL + 4 * value;
659 break;
660 case SBP2_FIRMWARE_REVISION:
661 firmware_revision = value;
662 break;
663 case CSR_MODEL:
664 model = value;
665 break;
666 }
667 }
668
669 for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
670 if (sbp2_workarounds_table[i].firmware_revision !=
671 (firmware_revision & 0xffffff00))
672 continue;
673 if (sbp2_workarounds_table[i].model != model &&
674 sbp2_workarounds_table[i].model != ~0)
675 continue;
676 sd->workarounds |= sbp2_workarounds_table[i].workarounds;
677 break;
678 }
679
680 if (sd->workarounds)
681 fw_notify("Workarounds for node %s: 0x%x "
682 "(firmware_revision 0x%06x, model_id 0x%06x)\n",
683 unit->device.bus_id,
684 sd->workarounds, firmware_revision, model);
685
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400686 get_device(&unit->device);
687
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400688 /*
689 * We schedule work to do the login so we can easily
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400690 * reschedule retries. Always get the ref before scheduling
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400691 * work.
692 */
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500693 INIT_DELAYED_WORK(&sd->work, sbp2_login);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400694 if (schedule_delayed_work(&sd->work, 0))
695 kref_get(&sd->kref);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500696
697 return 0;
698}
699
700static int sbp2_remove(struct device *dev)
701{
702 struct fw_unit *unit = fw_unit(dev);
703 struct sbp2_device *sd = unit->device.driver_data;
704
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400705 kref_put(&sd->kref, release_sbp2_device);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500706
707 return 0;
708}
709
710static void sbp2_reconnect(struct work_struct *work)
711{
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500712 struct sbp2_device *sd =
713 container_of(work, struct sbp2_device, work.work);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500714 struct fw_unit *unit = sd->unit;
715 struct fw_device *device = fw_device(unit->device.parent);
716 int generation, node_id, local_node_id;
717
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500718 generation = device->card->generation;
719 node_id = device->node->node_id;
720 local_node_id = device->card->local_node->node_id;
721
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500722 if (sbp2_send_management_orb(unit, node_id, generation,
723 SBP2_RECONNECT_REQUEST,
724 sd->login_id, NULL) < 0) {
Kristian Høgsberg5c5539d2007-03-07 12:12:45 -0500725 if (sd->retries++ >= 5) {
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500726 fw_error("failed to reconnect to %s\n",
727 unit->device.bus_id);
728 /* Fall back and try to log in again. */
729 sd->retries = 0;
Kristian Høgsberg1da0c932007-03-07 12:12:40 -0500730 PREPARE_DELAYED_WORK(&sd->work, sbp2_login);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500731 }
732 schedule_delayed_work(&sd->work, DIV_ROUND_UP(HZ, 5));
733 return;
734 }
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500735
736 sd->generation = generation;
737 sd->node_id = node_id;
Stefan Richter907293d2007-01-23 21:11:43 +0100738 sd->address_high = local_node_id << 16;
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500739
Kristian Høgsberg5c5539d2007-03-07 12:12:45 -0500740 fw_notify("reconnected to unit %s (%d retries)\n",
741 unit->device.bus_id, sd->retries);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500742 sbp2_agent_reset(unit);
743 sbp2_cancel_orbs(unit);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400744 kref_put(&sd->kref, release_sbp2_device);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500745}
746
747static void sbp2_update(struct fw_unit *unit)
748{
749 struct fw_device *device = fw_device(unit->device.parent);
750 struct sbp2_device *sd = unit->device.driver_data;
751
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500752 sd->retries = 0;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500753 fw_device_enable_phys_dma(device);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400754 if (schedule_delayed_work(&sd->work, 0))
755 kref_get(&sd->kref);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500756}
757
758#define SBP2_UNIT_SPEC_ID_ENTRY 0x0000609e
759#define SBP2_SW_VERSION_ENTRY 0x00010483
760
Stefan Richter21ebcd12007-01-14 15:29:07 +0100761static const struct fw_device_id sbp2_id_table[] = {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500762 {
763 .match_flags = FW_MATCH_SPECIFIER_ID | FW_MATCH_VERSION,
764 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY,
Stefan Richter5af4e5e2007-01-21 20:45:32 +0100765 .version = SBP2_SW_VERSION_ENTRY,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500766 },
767 { }
768};
769
770static struct fw_driver sbp2_driver = {
771 .driver = {
772 .owner = THIS_MODULE,
773 .name = sbp2_driver_name,
774 .bus = &fw_bus_type,
775 .probe = sbp2_probe,
776 .remove = sbp2_remove,
777 },
778 .update = sbp2_update,
779 .id_table = sbp2_id_table,
780};
781
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400782static unsigned int
783sbp2_status_to_sense_data(u8 *sbp2_status, u8 *sense_data)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500784{
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400785 int sam_status;
786
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500787 sense_data[0] = 0x70;
788 sense_data[1] = 0x0;
789 sense_data[2] = sbp2_status[1];
790 sense_data[3] = sbp2_status[4];
791 sense_data[4] = sbp2_status[5];
792 sense_data[5] = sbp2_status[6];
793 sense_data[6] = sbp2_status[7];
794 sense_data[7] = 10;
795 sense_data[8] = sbp2_status[8];
796 sense_data[9] = sbp2_status[9];
797 sense_data[10] = sbp2_status[10];
798 sense_data[11] = sbp2_status[11];
799 sense_data[12] = sbp2_status[2];
800 sense_data[13] = sbp2_status[3];
801 sense_data[14] = sbp2_status[12];
802 sense_data[15] = sbp2_status[13];
803
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400804 sam_status = sbp2_status[0] & 0x3f;
805
806 switch (sam_status) {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500807 case SAM_STAT_GOOD:
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500808 case SAM_STAT_CHECK_CONDITION:
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500809 case SAM_STAT_CONDITION_MET:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400810 case SAM_STAT_BUSY:
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500811 case SAM_STAT_RESERVATION_CONFLICT:
812 case SAM_STAT_COMMAND_TERMINATED:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400813 return DID_OK << 16 | sam_status;
814
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500815 default:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400816 return DID_ERROR << 16;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500817 }
818}
819
820static void
821complete_command_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
822{
823 struct sbp2_command_orb *orb = (struct sbp2_command_orb *)base_orb;
824 struct fw_unit *unit = orb->unit;
825 struct fw_device *device = fw_device(unit->device.parent);
826 struct scatterlist *sg;
827 int result;
828
829 if (status != NULL) {
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400830 if (STATUS_GET_DEAD(*status))
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500831 sbp2_agent_reset(unit);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500832
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400833 switch (STATUS_GET_RESPONSE(*status)) {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500834 case SBP2_STATUS_REQUEST_COMPLETE:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400835 result = DID_OK << 16;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500836 break;
837 case SBP2_STATUS_TRANSPORT_FAILURE:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400838 result = DID_BUS_BUSY << 16;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500839 break;
840 case SBP2_STATUS_ILLEGAL_REQUEST:
841 case SBP2_STATUS_VENDOR_DEPENDENT:
842 default:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400843 result = DID_ERROR << 16;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500844 break;
845 }
846
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400847 if (result == DID_OK << 16 && STATUS_GET_LEN(*status) > 1)
848 result = sbp2_status_to_sense_data(STATUS_GET_DATA(*status),
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500849 orb->cmd->sense_buffer);
850 } else {
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400851 /*
852 * If the orb completes with status == NULL, something
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500853 * went wrong, typically a bus reset happened mid-orb
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400854 * or when sending the write (less likely).
855 */
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400856 result = DID_BUS_BUSY << 16;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500857 }
858
859 dma_unmap_single(device->card->device, orb->base.request_bus,
860 sizeof orb->request, DMA_TO_DEVICE);
861
862 if (orb->cmd->use_sg > 0) {
863 sg = (struct scatterlist *)orb->cmd->request_buffer;
864 dma_unmap_sg(device->card->device, sg, orb->cmd->use_sg,
865 orb->cmd->sc_data_direction);
866 }
867
868 if (orb->page_table_bus != 0)
869 dma_unmap_single(device->card->device, orb->page_table_bus,
870 sizeof orb->page_table_bus, DMA_TO_DEVICE);
871
872 if (orb->request_buffer_bus != 0)
873 dma_unmap_single(device->card->device, orb->request_buffer_bus,
874 sizeof orb->request_buffer_bus,
875 DMA_FROM_DEVICE);
876
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400877 orb->cmd->result = result;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500878 orb->done(orb->cmd);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500879 kfree(orb);
880}
881
882static void sbp2_command_orb_map_scatterlist(struct sbp2_command_orb *orb)
883{
884 struct fw_unit *unit =
885 (struct fw_unit *)orb->cmd->device->host->hostdata[0];
886 struct fw_device *device = fw_device(unit->device.parent);
887 struct sbp2_device *sd = unit->device.driver_data;
888 struct scatterlist *sg;
889 int sg_len, l, i, j, count;
890 size_t size;
891 dma_addr_t sg_addr;
892
893 sg = (struct scatterlist *)orb->cmd->request_buffer;
894 count = dma_map_sg(device->card->device, sg, orb->cmd->use_sg,
895 orb->cmd->sc_data_direction);
896
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400897 /*
898 * Handle the special case where there is only one element in
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500899 * the scatter list by converting it to an immediate block
900 * request. This is also a workaround for broken devices such
901 * as the second generation iPod which doesn't support page
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400902 * tables.
903 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500904 if (count == 1 && sg_dma_len(sg) < SBP2_MAX_SG_ELEMENT_LENGTH) {
905 orb->request.data_descriptor.high = sd->address_high;
906 orb->request.data_descriptor.low = sg_dma_address(sg);
907 orb->request.misc |=
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400908 COMMAND_ORB_DATA_SIZE(sg_dma_len(sg));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500909 return;
910 }
911
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400912 /*
913 * Convert the scatterlist to an sbp2 page table. If any
914 * scatterlist entries are too big for sbp2 we split the as we go.
915 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500916 for (i = 0, j = 0; i < count; i++) {
917 sg_len = sg_dma_len(sg + i);
918 sg_addr = sg_dma_address(sg + i);
919 while (sg_len) {
920 l = min(sg_len, SBP2_MAX_SG_ELEMENT_LENGTH);
921 orb->page_table[j].low = sg_addr;
922 orb->page_table[j].high = (l << 16);
923 sg_addr += l;
924 sg_len -= l;
925 j++;
926 }
927 }
928
929 size = sizeof orb->page_table[0] * j;
930
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400931 /*
932 * The data_descriptor pointer is the one case where we need
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500933 * to fill in the node ID part of the address. All other
934 * pointers assume that the data referenced reside on the
935 * initiator (i.e. us), but data_descriptor can refer to data
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400936 * on other nodes so we need to put our ID in descriptor.high.
937 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500938
939 orb->page_table_bus =
940 dma_map_single(device->card->device, orb->page_table,
941 size, DMA_TO_DEVICE);
942 orb->request.data_descriptor.high = sd->address_high;
943 orb->request.data_descriptor.low = orb->page_table_bus;
944 orb->request.misc |=
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400945 COMMAND_ORB_PAGE_TABLE_PRESENT |
946 COMMAND_ORB_DATA_SIZE(j);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500947
948 fw_memcpy_to_be32(orb->page_table, orb->page_table, size);
949}
950
951static void sbp2_command_orb_map_buffer(struct sbp2_command_orb *orb)
952{
953 struct fw_unit *unit =
954 (struct fw_unit *)orb->cmd->device->host->hostdata[0];
955 struct fw_device *device = fw_device(unit->device.parent);
956 struct sbp2_device *sd = unit->device.driver_data;
957
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400958 /*
959 * As for map_scatterlist, we need to fill in the high bits of
960 * the data_descriptor pointer.
961 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500962
963 orb->request_buffer_bus =
964 dma_map_single(device->card->device,
965 orb->cmd->request_buffer,
966 orb->cmd->request_bufflen,
967 orb->cmd->sc_data_direction);
968 orb->request.data_descriptor.high = sd->address_high;
969 orb->request.data_descriptor.low = orb->request_buffer_bus;
970 orb->request.misc |=
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400971 COMMAND_ORB_DATA_SIZE(orb->cmd->request_bufflen);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500972}
973
974/* SCSI stack integration */
975
976static int sbp2_scsi_queuecommand(struct scsi_cmnd *cmd, scsi_done_fn_t done)
977{
978 struct fw_unit *unit = (struct fw_unit *)cmd->device->host->hostdata[0];
979 struct fw_device *device = fw_device(unit->device.parent);
980 struct sbp2_device *sd = unit->device.driver_data;
981 struct sbp2_command_orb *orb;
982
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400983 /*
984 * Bidirectional commands are not yet implemented, and unknown
985 * transfer direction not handled.
986 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500987 if (cmd->sc_data_direction == DMA_BIDIRECTIONAL) {
988 fw_error("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -0500989 goto fail_alloc;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500990 }
991
992 orb = kzalloc(sizeof *orb, GFP_ATOMIC);
993 if (orb == NULL) {
994 fw_notify("failed to alloc orb\n");
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -0500995 goto fail_alloc;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500996 }
997
Kristian Høgsberg12f26aa2007-04-10 18:11:20 -0400998 /* Initialize rcode to something not RCODE_COMPLETE. */
999 orb->base.rcode = -1;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001000 orb->base.request_bus =
1001 dma_map_single(device->card->device, &orb->request,
1002 sizeof orb->request, DMA_TO_DEVICE);
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -05001003 if (dma_mapping_error(orb->base.request_bus))
1004 goto fail_mapping;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001005
1006 orb->unit = unit;
1007 orb->done = done;
1008 orb->cmd = cmd;
1009
1010 orb->request.next.high = SBP2_ORB_NULL;
1011 orb->request.next.low = 0x0;
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001012 /*
1013 * At speed 100 we can do 512 bytes per packet, at speed 200,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001014 * 1024 bytes per packet etc. The SBP-2 max_payload field
1015 * specifies the max payload size as 2 ^ (max_payload + 2), so
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001016 * if we set this to max_speed + 7, we get the right value.
1017 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001018 orb->request.misc =
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001019 COMMAND_ORB_MAX_PAYLOAD(device->node->max_speed + 7) |
1020 COMMAND_ORB_SPEED(device->node->max_speed) |
1021 COMMAND_ORB_NOTIFY;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001022
1023 if (cmd->sc_data_direction == DMA_FROM_DEVICE)
1024 orb->request.misc |=
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001025 COMMAND_ORB_DIRECTION(SBP2_DIRECTION_FROM_MEDIA);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001026 else if (cmd->sc_data_direction == DMA_TO_DEVICE)
1027 orb->request.misc |=
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001028 COMMAND_ORB_DIRECTION(SBP2_DIRECTION_TO_MEDIA);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001029
1030 if (cmd->use_sg) {
1031 sbp2_command_orb_map_scatterlist(orb);
1032 } else if (cmd->request_bufflen > SBP2_MAX_SG_ELEMENT_LENGTH) {
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001033 /*
1034 * FIXME: Need to split this into a sg list... but
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001035 * could we get the scsi or blk layer to do that by
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001036 * reporting our max supported block size?
1037 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001038 fw_error("command > 64k\n");
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -05001039 goto fail_bufflen;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001040 } else if (cmd->request_bufflen > 0) {
1041 sbp2_command_orb_map_buffer(orb);
1042 }
1043
1044 fw_memcpy_to_be32(&orb->request, &orb->request, sizeof orb->request);
1045
1046 memset(orb->request.command_block,
1047 0, sizeof orb->request.command_block);
1048 memcpy(orb->request.command_block, cmd->cmnd, COMMAND_SIZE(*cmd->cmnd));
1049
1050 orb->base.callback = complete_command_orb;
1051
1052 sbp2_send_orb(&orb->base, unit, sd->node_id, sd->generation,
1053 sd->command_block_agent_address + SBP2_ORB_POINTER);
1054
1055 return 0;
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -05001056
1057 fail_bufflen:
1058 dma_unmap_single(device->card->device, orb->base.request_bus,
1059 sizeof orb->request, DMA_TO_DEVICE);
1060 fail_mapping:
1061 kfree(orb);
1062 fail_alloc:
1063 cmd->result = DID_ERROR << 16;
1064 done(cmd);
1065 return 0;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001066}
1067
Stefan Richtercfb01382007-01-23 21:20:08 +01001068static int sbp2_scsi_slave_alloc(struct scsi_device *sdev)
1069{
1070 struct fw_unit *unit = (struct fw_unit *)sdev->host->hostdata[0];
1071 struct sbp2_device *sd = unit->device.driver_data;
1072
1073 sdev->allow_restart = 1;
1074
1075 if (sd->workarounds & SBP2_WORKAROUND_INQUIRY_36)
1076 sdev->inquiry_len = 36;
1077 return 0;
1078}
1079
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001080static int sbp2_scsi_slave_configure(struct scsi_device *sdev)
1081{
1082 struct fw_unit *unit = (struct fw_unit *)sdev->host->hostdata[0];
1083 struct sbp2_device *sd = unit->device.driver_data;
1084
Stefan Richtercfb01382007-01-23 21:20:08 +01001085 sdev->use_10_for_rw = 1;
1086
1087 if (sdev->type == TYPE_ROM)
1088 sdev->use_10_for_ms = 1;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001089 if (sdev->type == TYPE_DISK &&
1090 sd->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
1091 sdev->skip_ms_page_8 = 1;
1092 if (sd->workarounds & SBP2_WORKAROUND_FIX_CAPACITY) {
1093 fw_notify("setting fix_capacity for %s\n", unit->device.bus_id);
1094 sdev->fix_capacity = 1;
1095 }
1096
1097 return 0;
1098}
1099
1100/*
1101 * Called by scsi stack when something has really gone wrong. Usually
1102 * called when a command has timed-out for some reason.
1103 */
1104static int sbp2_scsi_abort(struct scsi_cmnd *cmd)
1105{
1106 struct fw_unit *unit = (struct fw_unit *)cmd->device->host->hostdata[0];
1107
1108 fw_notify("sbp2_scsi_abort\n");
Kristian Høgsberg0fc7d6e2007-04-11 18:44:33 -04001109 sbp2_agent_reset(unit);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001110 sbp2_cancel_orbs(unit);
1111
1112 return SUCCESS;
1113}
1114
1115static struct scsi_host_template scsi_driver_template = {
1116 .module = THIS_MODULE,
1117 .name = "SBP-2 IEEE-1394",
1118 .proc_name = (char *)sbp2_driver_name,
1119 .queuecommand = sbp2_scsi_queuecommand,
Stefan Richtercfb01382007-01-23 21:20:08 +01001120 .slave_alloc = sbp2_scsi_slave_alloc,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001121 .slave_configure = sbp2_scsi_slave_configure,
1122 .eh_abort_handler = sbp2_scsi_abort,
1123 .this_id = -1,
1124 .sg_tablesize = SG_ALL,
1125 .use_clustering = ENABLE_CLUSTERING,
Stefan Richter02af8e72007-01-21 20:50:11 +01001126 .cmd_per_lun = 1,
1127 .can_queue = 1,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001128};
1129
1130static int add_scsi_devices(struct fw_unit *unit)
1131{
1132 struct sbp2_device *sd = unit->device.driver_data;
1133 int retval, lun;
1134
Kristian Høgsberg7f37c422007-02-06 14:49:34 -05001135 if (sd->scsi_host != NULL)
1136 return 0;
1137
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001138 sd->scsi_host = scsi_host_alloc(&scsi_driver_template,
1139 sizeof(unsigned long));
1140 if (sd->scsi_host == NULL) {
1141 fw_error("failed to register scsi host\n");
1142 return -1;
1143 }
1144
1145 sd->scsi_host->hostdata[0] = (unsigned long)unit;
1146 retval = scsi_add_host(sd->scsi_host, &unit->device);
1147 if (retval < 0) {
1148 fw_error("failed to add scsi host\n");
1149 scsi_host_put(sd->scsi_host);
Kristian Høgsberg693b9022007-03-14 17:34:56 -04001150 sd->scsi_host = NULL;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001151 return retval;
1152 }
1153
1154 /* FIXME: Loop over luns here. */
1155 lun = 0;
1156 retval = scsi_add_device(sd->scsi_host, 0, 0, lun);
1157 if (retval < 0) {
1158 fw_error("failed to add scsi device\n");
1159 scsi_remove_host(sd->scsi_host);
1160 scsi_host_put(sd->scsi_host);
Kristian Høgsberg693b9022007-03-14 17:34:56 -04001161 sd->scsi_host = NULL;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001162 return retval;
1163 }
1164
1165 return 0;
1166}
1167
1168static void remove_scsi_devices(struct fw_unit *unit)
1169{
1170 struct sbp2_device *sd = unit->device.driver_data;
1171
Kristian Høgsberg7f37c422007-02-06 14:49:34 -05001172 if (sd->scsi_host != NULL) {
1173 scsi_remove_host(sd->scsi_host);
1174 scsi_host_put(sd->scsi_host);
1175 }
1176 sd->scsi_host = NULL;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001177}
1178
1179MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
1180MODULE_DESCRIPTION("SCSI over IEEE1394");
1181MODULE_LICENSE("GPL");
1182MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
1183
1184static int __init sbp2_init(void)
1185{
1186 return driver_register(&sbp2_driver.driver);
1187}
1188
1189static void __exit sbp2_cleanup(void)
1190{
1191 driver_unregister(&sbp2_driver.driver);
1192}
1193
1194module_init(sbp2_init);
1195module_exit(sbp2_cleanup);