blob: 846aad8666f582cd80ff0e52ae1fd402cfb98b70 [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>
Stefan Richtercf47c7a2007-06-17 23:52:08 +020037#include <linux/blkdev.h>
Kristian Høgsberg1d3d52c2007-02-06 14:49:33 -050038#include <linux/timer.h>
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050039
40#include <scsi/scsi.h>
41#include <scsi/scsi_cmnd.h>
42#include <scsi/scsi_dbg.h>
43#include <scsi/scsi_device.h>
44#include <scsi/scsi_host.h>
45
46#include "fw-transaction.h"
47#include "fw-topology.h"
48#include "fw-device.h"
49
50/* I don't know why the SCSI stack doesn't define something like this... */
Kristian Høgsberga98e2712007-05-07 20:33:34 -040051typedef void (*scsi_done_fn_t)(struct scsi_cmnd *);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050052
53static const char sbp2_driver_name[] = "sbp2";
54
55struct sbp2_device {
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -040056 struct kref kref;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050057 struct fw_unit *unit;
58 struct fw_address_handler address_handler;
59 struct list_head orb_list;
60 u64 management_agent_address;
61 u64 command_block_agent_address;
62 u32 workarounds;
63 int login_id;
64
Kristian Høgsbergc781c062007-05-07 20:33:32 -040065 /*
66 * We cache these addresses and only update them once we've
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050067 * logged in or reconnected to the sbp2 device. That way, any
68 * IO to the device will automatically fail and get retried if
69 * it happens in a window where the device is not ready to
Kristian Høgsbergc781c062007-05-07 20:33:32 -040070 * handle it (e.g. after a bus reset but before we reconnect).
71 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050072 int node_id;
73 int address_high;
74 int generation;
75
Kristian Høgsberg7f37c422007-02-06 14:49:34 -050076 int retries;
77 struct delayed_work work;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050078};
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;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500211};
212
213/*
214 * List of devices with known bugs.
215 *
216 * The firmware_revision field, masked with 0xffff00, is the best
217 * indicator for the type of bridge chip of a device. It yields a few
218 * false positives but this did not break correctly behaving devices
219 * so far. We use ~0 as a wildcard, since the 24 bit values we get
220 * from the config rom can never match that.
221 */
222static const struct {
223 u32 firmware_revision;
224 u32 model;
225 unsigned workarounds;
226} sbp2_workarounds_table[] = {
227 /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
228 .firmware_revision = 0x002800,
229 .model = 0x001010,
230 .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
231 SBP2_WORKAROUND_MODE_SENSE_8,
232 },
233 /* Initio bridges, actually only needed for some older ones */ {
234 .firmware_revision = 0x000200,
235 .model = ~0,
236 .workarounds = SBP2_WORKAROUND_INQUIRY_36,
237 },
238 /* Symbios bridge */ {
239 .firmware_revision = 0xa0b800,
240 .model = ~0,
241 .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
242 },
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400243
244 /*
245 * There are iPods (2nd gen, 3rd gen) with model_id == 0, but
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500246 * these iPods do not feature the read_capacity bug according
247 * to one report. Read_capacity behaviour as well as model_id
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400248 * could change due to Apple-supplied firmware updates though.
249 */
250
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500251 /* iPod 4th generation. */ {
252 .firmware_revision = 0x0a2700,
253 .model = 0x000021,
254 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
255 },
256 /* iPod mini */ {
257 .firmware_revision = 0x0a2700,
258 .model = 0x000023,
259 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
260 },
261 /* iPod Photo */ {
262 .firmware_revision = 0x0a2700,
263 .model = 0x00007e,
264 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
265 }
266};
267
268static void
269sbp2_status_write(struct fw_card *card, struct fw_request *request,
270 int tcode, int destination, int source,
271 int generation, int speed,
272 unsigned long long offset,
273 void *payload, size_t length, void *callback_data)
274{
275 struct sbp2_device *sd = callback_data;
276 struct sbp2_orb *orb;
277 struct sbp2_status status;
278 size_t header_size;
279 unsigned long flags;
280
281 if (tcode != TCODE_WRITE_BLOCK_REQUEST ||
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400282 length == 0 || length > sizeof(status)) {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500283 fw_send_response(card, request, RCODE_TYPE_ERROR);
284 return;
285 }
286
287 header_size = min(length, 2 * sizeof(u32));
288 fw_memcpy_from_be32(&status, payload, header_size);
289 if (length > header_size)
290 memcpy(status.data, payload + 8, length - header_size);
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400291 if (STATUS_GET_SOURCE(status) == 2 || STATUS_GET_SOURCE(status) == 3) {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500292 fw_notify("non-orb related status write, not handled\n");
293 fw_send_response(card, request, RCODE_COMPLETE);
294 return;
295 }
296
297 /* Lookup the orb corresponding to this status write. */
298 spin_lock_irqsave(&card->lock, flags);
299 list_for_each_entry(orb, &sd->orb_list, link) {
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400300 if (STATUS_GET_ORB_HIGH(status) == 0 &&
301 STATUS_GET_ORB_LOW(status) == orb->request_bus &&
Kristian Høgsberg12f26aa2007-04-10 18:11:20 -0400302 orb->rcode == RCODE_COMPLETE) {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500303 list_del(&orb->link);
304 break;
305 }
306 }
307 spin_unlock_irqrestore(&card->lock, flags);
308
309 if (&orb->link != &sd->orb_list)
310 orb->callback(orb, &status);
311 else
312 fw_error("status write for unknown orb\n");
313
314 fw_send_response(card, request, RCODE_COMPLETE);
315}
316
317static void
318complete_transaction(struct fw_card *card, int rcode,
319 void *payload, size_t length, void *data)
320{
321 struct sbp2_orb *orb = data;
322 unsigned long flags;
323
324 orb->rcode = rcode;
325 if (rcode != RCODE_COMPLETE) {
326 spin_lock_irqsave(&card->lock, flags);
327 list_del(&orb->link);
328 spin_unlock_irqrestore(&card->lock, flags);
329 orb->callback(orb, NULL);
330 }
331}
332
333static void
334sbp2_send_orb(struct sbp2_orb *orb, struct fw_unit *unit,
335 int node_id, int generation, u64 offset)
336{
337 struct fw_device *device = fw_device(unit->device.parent);
338 struct sbp2_device *sd = unit->device.driver_data;
339 unsigned long flags;
340
341 orb->pointer.high = 0;
342 orb->pointer.low = orb->request_bus;
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400343 fw_memcpy_to_be32(&orb->pointer, &orb->pointer, sizeof(orb->pointer));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500344
345 spin_lock_irqsave(&device->card->lock, flags);
346 list_add_tail(&orb->link, &sd->orb_list);
347 spin_unlock_irqrestore(&device->card->lock, flags);
348
349 fw_send_request(device->card, &orb->t, TCODE_WRITE_BLOCK_REQUEST,
Stefan Richterf1397492007-06-10 21:31:36 +0200350 node_id, generation, device->max_speed, offset,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400351 &orb->pointer, sizeof(orb->pointer),
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500352 complete_transaction, orb);
353}
354
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500355static int sbp2_cancel_orbs(struct fw_unit *unit)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500356{
357 struct fw_device *device = fw_device(unit->device.parent);
358 struct sbp2_device *sd = unit->device.driver_data;
359 struct sbp2_orb *orb, *next;
360 struct list_head list;
361 unsigned long flags;
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500362 int retval = -ENOENT;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500363
364 INIT_LIST_HEAD(&list);
365 spin_lock_irqsave(&device->card->lock, flags);
366 list_splice_init(&sd->orb_list, &list);
367 spin_unlock_irqrestore(&device->card->lock, flags);
368
369 list_for_each_entry_safe(orb, next, &list, link) {
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500370 retval = 0;
Kristian Høgsberg730c32f2007-02-06 14:49:32 -0500371 if (fw_cancel_transaction(device->card, &orb->t) == 0)
372 continue;
373
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500374 orb->rcode = RCODE_CANCELLED;
375 orb->callback(orb, NULL);
376 }
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500377
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500378 return retval;
Kristian Høgsberg1d3d52c2007-02-06 14:49:33 -0500379}
380
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500381static void
382complete_management_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
383{
384 struct sbp2_management_orb *orb =
385 (struct sbp2_management_orb *)base_orb;
386
387 if (status)
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400388 memcpy(&orb->status, status, sizeof(*status));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500389 complete(&orb->done);
390}
391
392static int
393sbp2_send_management_orb(struct fw_unit *unit, int node_id, int generation,
394 int function, int lun, void *response)
395{
396 struct fw_device *device = fw_device(unit->device.parent);
397 struct sbp2_device *sd = unit->device.driver_data;
398 struct sbp2_management_orb *orb;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500399 int retval = -ENOMEM;
400
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400401 orb = kzalloc(sizeof(*orb), GFP_ATOMIC);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500402 if (orb == NULL)
403 return -ENOMEM;
404
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400405 /*
406 * The sbp2 device is going to send a block read request to
407 * read out the request from host memory, so map it for dma.
408 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500409 orb->base.request_bus =
410 dma_map_single(device->card->device, &orb->request,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400411 sizeof(orb->request), DMA_TO_DEVICE);
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -0500412 if (dma_mapping_error(orb->base.request_bus))
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500413 goto out;
414
415 orb->response_bus =
416 dma_map_single(device->card->device, &orb->response,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400417 sizeof(orb->response), DMA_FROM_DEVICE);
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -0500418 if (dma_mapping_error(orb->response_bus))
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500419 goto out;
420
421 orb->request.response.high = 0;
422 orb->request.response.low = orb->response_bus;
423
424 orb->request.misc =
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400425 MANAGEMENT_ORB_NOTIFY |
426 MANAGEMENT_ORB_FUNCTION(function) |
427 MANAGEMENT_ORB_LUN(lun);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500428 orb->request.length =
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400429 MANAGEMENT_ORB_RESPONSE_LENGTH(sizeof(orb->response));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500430
431 orb->request.status_fifo.high = sd->address_handler.offset >> 32;
432 orb->request.status_fifo.low = sd->address_handler.offset;
433
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400434 /*
435 * FIXME: Yeah, ok this isn't elegant, we hardwire exclusive
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500436 * login and 1 second reconnect time. The reconnect setting
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400437 * is probably fine, but the exclusive login should be an option.
438 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500439 if (function == SBP2_LOGIN_REQUEST) {
440 orb->request.misc |=
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400441 MANAGEMENT_ORB_EXCLUSIVE |
442 MANAGEMENT_ORB_RECONNECT(0);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500443 }
444
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400445 fw_memcpy_to_be32(&orb->request, &orb->request, sizeof(orb->request));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500446
447 init_completion(&orb->done);
448 orb->base.callback = complete_management_orb;
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500449
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500450 sbp2_send_orb(&orb->base, unit,
451 node_id, generation, sd->management_agent_address);
452
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500453 wait_for_completion_timeout(&orb->done,
454 msecs_to_jiffies(SBP2_ORB_TIMEOUT));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500455
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500456 retval = -EIO;
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500457 if (sbp2_cancel_orbs(unit) == 0) {
458 fw_error("orb reply timed out, rcode=0x%02x\n",
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500459 orb->base.rcode);
460 goto out;
461 }
462
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500463 if (orb->base.rcode != RCODE_COMPLETE) {
464 fw_error("management write failed, rcode 0x%02x\n",
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500465 orb->base.rcode);
466 goto out;
467 }
468
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400469 if (STATUS_GET_RESPONSE(orb->status) != 0 ||
470 STATUS_GET_SBP_STATUS(orb->status) != 0) {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500471 fw_error("error status: %d:%d\n",
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400472 STATUS_GET_RESPONSE(orb->status),
473 STATUS_GET_SBP_STATUS(orb->status));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500474 goto out;
475 }
476
477 retval = 0;
478 out:
479 dma_unmap_single(device->card->device, orb->base.request_bus,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400480 sizeof(orb->request), DMA_TO_DEVICE);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500481 dma_unmap_single(device->card->device, orb->response_bus,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400482 sizeof(orb->response), DMA_FROM_DEVICE);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500483
484 if (response)
485 fw_memcpy_from_be32(response,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400486 orb->response, sizeof(orb->response));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500487 kfree(orb);
488
489 return retval;
490}
491
492static void
493complete_agent_reset_write(struct fw_card *card, int rcode,
494 void *payload, size_t length, void *data)
495{
496 struct fw_transaction *t = data;
497
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500498 kfree(t);
499}
500
501static int sbp2_agent_reset(struct fw_unit *unit)
502{
503 struct fw_device *device = fw_device(unit->device.parent);
504 struct sbp2_device *sd = unit->device.driver_data;
505 struct fw_transaction *t;
506 static u32 zero;
507
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400508 t = kzalloc(sizeof(*t), GFP_ATOMIC);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500509 if (t == NULL)
510 return -ENOMEM;
511
512 fw_send_request(device->card, t, TCODE_WRITE_QUADLET_REQUEST,
Stefan Richter907293d2007-01-23 21:11:43 +0100513 sd->node_id, sd->generation, SCODE_400,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500514 sd->command_block_agent_address + SBP2_AGENT_RESET,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400515 &zero, sizeof(zero), complete_agent_reset_write, t);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500516
517 return 0;
518}
519
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500520static void sbp2_reconnect(struct work_struct *work);
Kristian Høgsbergad852742007-05-09 19:23:07 -0400521static struct scsi_host_template scsi_driver_template;
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500522
Stefan Richter79352e92007-06-18 18:46:49 +0200523static void release_sbp2_device(struct kref *kref)
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400524{
525 struct sbp2_device *sd = container_of(kref, struct sbp2_device, kref);
Kristian Høgsbergad852742007-05-09 19:23:07 -0400526 struct Scsi_Host *host =
527 container_of((void *)sd, struct Scsi_Host, hostdata[0]);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400528
Stefan Richter79352e92007-06-18 18:46:49 +0200529 scsi_remove_host(host);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400530 sbp2_send_management_orb(sd->unit, sd->node_id, sd->generation,
531 SBP2_LOGOUT_REQUEST, sd->login_id, NULL);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400532 fw_core_remove_address_handler(&sd->address_handler);
533 fw_notify("removed sbp2 unit %s\n", sd->unit->device.bus_id);
534 put_device(&sd->unit->device);
Kristian Høgsbergad852742007-05-09 19:23:07 -0400535 scsi_host_put(host);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400536}
537
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500538static void sbp2_login(struct work_struct *work)
539{
540 struct sbp2_device *sd =
541 container_of(work, struct sbp2_device, work.work);
Kristian Høgsbergad852742007-05-09 19:23:07 -0400542 struct Scsi_Host *host =
543 container_of((void *)sd, struct Scsi_Host, hostdata[0]);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500544 struct fw_unit *unit = sd->unit;
545 struct fw_device *device = fw_device(unit->device.parent);
546 struct sbp2_login_response response;
547 int generation, node_id, local_node_id, lun, retval;
548
549 /* FIXME: Make this work for multi-lun devices. */
550 lun = 0;
551
552 generation = device->card->generation;
553 node_id = device->node->node_id;
554 local_node_id = device->card->local_node->node_id;
555
556 if (sbp2_send_management_orb(unit, node_id, generation,
557 SBP2_LOGIN_REQUEST, lun, &response) < 0) {
558 if (sd->retries++ < 5) {
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500559 schedule_delayed_work(&sd->work, DIV_ROUND_UP(HZ, 5));
560 } else {
561 fw_error("failed to login to %s\n",
562 unit->device.bus_id);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400563 kref_put(&sd->kref, release_sbp2_device);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500564 }
565 return;
566 }
567
568 sd->generation = generation;
569 sd->node_id = node_id;
570 sd->address_high = local_node_id << 16;
571
572 /* Get command block agent offset and login id. */
573 sd->command_block_agent_address =
Kristian Høgsberg5c5539d2007-03-07 12:12:45 -0500574 ((u64) (response.command_block_agent.high & 0xffff) << 32) |
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500575 response.command_block_agent.low;
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400576 sd->login_id = LOGIN_RESPONSE_GET_LOGIN_ID(response);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500577
Kristian Høgsberg5c5539d2007-03-07 12:12:45 -0500578 fw_notify("logged in to sbp2 unit %s (%d retries)\n",
579 unit->device.bus_id, sd->retries);
580 fw_notify(" - management_agent_address: 0x%012llx\n",
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500581 (unsigned long long) sd->management_agent_address);
582 fw_notify(" - command_block_agent_address: 0x%012llx\n",
583 (unsigned long long) sd->command_block_agent_address);
Kristian Høgsberg5c5539d2007-03-07 12:12:45 -0500584 fw_notify(" - status write address: 0x%012llx\n",
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500585 (unsigned long long) sd->address_handler.offset);
586
587#if 0
588 /* FIXME: The linux1394 sbp2 does this last step. */
589 sbp2_set_busy_timeout(scsi_id);
590#endif
591
Kristian Høgsberg1da0c932007-03-07 12:12:40 -0500592 PREPARE_DELAYED_WORK(&sd->work, sbp2_reconnect);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500593 sbp2_agent_reset(unit);
594
Kristian Høgsbergad852742007-05-09 19:23:07 -0400595 /* FIXME: Loop over luns here. */
596 lun = 0;
597 retval = scsi_add_device(host, 0, 0, lun);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500598 if (retval < 0) {
599 sbp2_send_management_orb(unit, sd->node_id, sd->generation,
600 SBP2_LOGOUT_REQUEST, sd->login_id,
601 NULL);
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400602 /*
603 * Set this back to sbp2_login so we fall back and
604 * retry login on bus reset.
605 */
Kristian Høgsberg1da0c932007-03-07 12:12:40 -0500606 PREPARE_DELAYED_WORK(&sd->work, sbp2_login);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500607 }
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400608 kref_put(&sd->kref, release_sbp2_device);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500609}
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500610
611static int sbp2_probe(struct device *dev)
612{
613 struct fw_unit *unit = fw_unit(dev);
614 struct fw_device *device = fw_device(unit->device.parent);
615 struct sbp2_device *sd;
616 struct fw_csr_iterator ci;
Kristian Høgsbergad852742007-05-09 19:23:07 -0400617 struct Scsi_Host *host;
618 int i, key, value, err;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500619 u32 model, firmware_revision;
620
Kristian Høgsbergad852742007-05-09 19:23:07 -0400621 err = -ENOMEM;
622 host = scsi_host_alloc(&scsi_driver_template, sizeof(*sd));
623 if (host == NULL)
624 goto fail;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500625
Kristian Høgsbergad852742007-05-09 19:23:07 -0400626 sd = (struct sbp2_device *) host->hostdata;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500627 unit->device.driver_data = sd;
628 sd->unit = unit;
629 INIT_LIST_HEAD(&sd->orb_list);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400630 kref_init(&sd->kref);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500631
632 sd->address_handler.length = 0x100;
633 sd->address_handler.address_callback = sbp2_status_write;
634 sd->address_handler.callback_data = sd;
635
Kristian Høgsbergad852742007-05-09 19:23:07 -0400636 err = fw_core_add_address_handler(&sd->address_handler,
637 &fw_high_memory_region);
638 if (err < 0)
639 goto fail_host;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500640
Kristian Høgsbergad852742007-05-09 19:23:07 -0400641 err = fw_device_enable_phys_dma(device);
642 if (err < 0)
643 goto fail_address_handler;
644
645 err = scsi_add_host(host, &unit->device);
646 if (err < 0)
647 goto fail_address_handler;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500648
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400649 /*
650 * Scan unit directory to get management agent address,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500651 * firmware revison and model. Initialize firmware_revision
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400652 * and model to values that wont match anything in our table.
653 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500654 firmware_revision = 0xff000000;
655 model = 0xff000000;
656 fw_csr_iterator_init(&ci, unit->directory);
657 while (fw_csr_iterator_next(&ci, &key, &value)) {
658 switch (key) {
659 case CSR_DEPENDENT_INFO | CSR_OFFSET:
660 sd->management_agent_address =
661 0xfffff0000000ULL + 4 * value;
662 break;
663 case SBP2_FIRMWARE_REVISION:
664 firmware_revision = value;
665 break;
666 case CSR_MODEL:
667 model = value;
668 break;
669 }
670 }
671
672 for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
673 if (sbp2_workarounds_table[i].firmware_revision !=
674 (firmware_revision & 0xffffff00))
675 continue;
676 if (sbp2_workarounds_table[i].model != model &&
677 sbp2_workarounds_table[i].model != ~0)
678 continue;
679 sd->workarounds |= sbp2_workarounds_table[i].workarounds;
680 break;
681 }
682
683 if (sd->workarounds)
684 fw_notify("Workarounds for node %s: 0x%x "
685 "(firmware_revision 0x%06x, model_id 0x%06x)\n",
686 unit->device.bus_id,
687 sd->workarounds, firmware_revision, model);
688
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400689 get_device(&unit->device);
690
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400691 /*
692 * We schedule work to do the login so we can easily
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400693 * reschedule retries. Always get the ref before scheduling
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400694 * work.
695 */
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500696 INIT_DELAYED_WORK(&sd->work, sbp2_login);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400697 if (schedule_delayed_work(&sd->work, 0))
698 kref_get(&sd->kref);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500699
700 return 0;
Kristian Høgsbergad852742007-05-09 19:23:07 -0400701
702 fail_address_handler:
703 fw_core_remove_address_handler(&sd->address_handler);
704 fail_host:
705 scsi_host_put(host);
706 fail:
707 return err;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500708}
709
710static int sbp2_remove(struct device *dev)
711{
712 struct fw_unit *unit = fw_unit(dev);
713 struct sbp2_device *sd = unit->device.driver_data;
714
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400715 kref_put(&sd->kref, release_sbp2_device);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500716
717 return 0;
718}
719
720static void sbp2_reconnect(struct work_struct *work)
721{
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500722 struct sbp2_device *sd =
723 container_of(work, struct sbp2_device, work.work);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500724 struct fw_unit *unit = sd->unit;
725 struct fw_device *device = fw_device(unit->device.parent);
726 int generation, node_id, local_node_id;
727
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500728 generation = device->card->generation;
729 node_id = device->node->node_id;
730 local_node_id = device->card->local_node->node_id;
731
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500732 if (sbp2_send_management_orb(unit, node_id, generation,
733 SBP2_RECONNECT_REQUEST,
734 sd->login_id, NULL) < 0) {
Kristian Høgsberg5c5539d2007-03-07 12:12:45 -0500735 if (sd->retries++ >= 5) {
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500736 fw_error("failed to reconnect to %s\n",
737 unit->device.bus_id);
738 /* Fall back and try to log in again. */
739 sd->retries = 0;
Kristian Høgsberg1da0c932007-03-07 12:12:40 -0500740 PREPARE_DELAYED_WORK(&sd->work, sbp2_login);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500741 }
742 schedule_delayed_work(&sd->work, DIV_ROUND_UP(HZ, 5));
743 return;
744 }
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500745
746 sd->generation = generation;
747 sd->node_id = node_id;
Stefan Richter907293d2007-01-23 21:11:43 +0100748 sd->address_high = local_node_id << 16;
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500749
Kristian Høgsberg5c5539d2007-03-07 12:12:45 -0500750 fw_notify("reconnected to unit %s (%d retries)\n",
751 unit->device.bus_id, sd->retries);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500752 sbp2_agent_reset(unit);
753 sbp2_cancel_orbs(unit);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400754 kref_put(&sd->kref, release_sbp2_device);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500755}
756
757static void sbp2_update(struct fw_unit *unit)
758{
759 struct fw_device *device = fw_device(unit->device.parent);
760 struct sbp2_device *sd = unit->device.driver_data;
761
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500762 sd->retries = 0;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500763 fw_device_enable_phys_dma(device);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400764 if (schedule_delayed_work(&sd->work, 0))
765 kref_get(&sd->kref);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500766}
767
768#define SBP2_UNIT_SPEC_ID_ENTRY 0x0000609e
769#define SBP2_SW_VERSION_ENTRY 0x00010483
770
Stefan Richter21ebcd12007-01-14 15:29:07 +0100771static const struct fw_device_id sbp2_id_table[] = {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500772 {
773 .match_flags = FW_MATCH_SPECIFIER_ID | FW_MATCH_VERSION,
774 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY,
Stefan Richter5af4e5e2007-01-21 20:45:32 +0100775 .version = SBP2_SW_VERSION_ENTRY,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500776 },
777 { }
778};
779
780static struct fw_driver sbp2_driver = {
781 .driver = {
782 .owner = THIS_MODULE,
783 .name = sbp2_driver_name,
784 .bus = &fw_bus_type,
785 .probe = sbp2_probe,
786 .remove = sbp2_remove,
787 },
788 .update = sbp2_update,
789 .id_table = sbp2_id_table,
790};
791
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400792static unsigned int
793sbp2_status_to_sense_data(u8 *sbp2_status, u8 *sense_data)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500794{
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400795 int sam_status;
796
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500797 sense_data[0] = 0x70;
798 sense_data[1] = 0x0;
799 sense_data[2] = sbp2_status[1];
800 sense_data[3] = sbp2_status[4];
801 sense_data[4] = sbp2_status[5];
802 sense_data[5] = sbp2_status[6];
803 sense_data[6] = sbp2_status[7];
804 sense_data[7] = 10;
805 sense_data[8] = sbp2_status[8];
806 sense_data[9] = sbp2_status[9];
807 sense_data[10] = sbp2_status[10];
808 sense_data[11] = sbp2_status[11];
809 sense_data[12] = sbp2_status[2];
810 sense_data[13] = sbp2_status[3];
811 sense_data[14] = sbp2_status[12];
812 sense_data[15] = sbp2_status[13];
813
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400814 sam_status = sbp2_status[0] & 0x3f;
815
816 switch (sam_status) {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500817 case SAM_STAT_GOOD:
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500818 case SAM_STAT_CHECK_CONDITION:
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500819 case SAM_STAT_CONDITION_MET:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400820 case SAM_STAT_BUSY:
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500821 case SAM_STAT_RESERVATION_CONFLICT:
822 case SAM_STAT_COMMAND_TERMINATED:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400823 return DID_OK << 16 | sam_status;
824
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500825 default:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400826 return DID_ERROR << 16;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500827 }
828}
829
830static void
831complete_command_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
832{
833 struct sbp2_command_orb *orb = (struct sbp2_command_orb *)base_orb;
834 struct fw_unit *unit = orb->unit;
835 struct fw_device *device = fw_device(unit->device.parent);
836 struct scatterlist *sg;
837 int result;
838
839 if (status != NULL) {
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400840 if (STATUS_GET_DEAD(*status))
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500841 sbp2_agent_reset(unit);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500842
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400843 switch (STATUS_GET_RESPONSE(*status)) {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500844 case SBP2_STATUS_REQUEST_COMPLETE:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400845 result = DID_OK << 16;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500846 break;
847 case SBP2_STATUS_TRANSPORT_FAILURE:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400848 result = DID_BUS_BUSY << 16;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500849 break;
850 case SBP2_STATUS_ILLEGAL_REQUEST:
851 case SBP2_STATUS_VENDOR_DEPENDENT:
852 default:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400853 result = DID_ERROR << 16;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500854 break;
855 }
856
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400857 if (result == DID_OK << 16 && STATUS_GET_LEN(*status) > 1)
858 result = sbp2_status_to_sense_data(STATUS_GET_DATA(*status),
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500859 orb->cmd->sense_buffer);
860 } else {
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400861 /*
862 * If the orb completes with status == NULL, something
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500863 * went wrong, typically a bus reset happened mid-orb
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400864 * or when sending the write (less likely).
865 */
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400866 result = DID_BUS_BUSY << 16;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500867 }
868
869 dma_unmap_single(device->card->device, orb->base.request_bus,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400870 sizeof(orb->request), DMA_TO_DEVICE);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500871
872 if (orb->cmd->use_sg > 0) {
873 sg = (struct scatterlist *)orb->cmd->request_buffer;
874 dma_unmap_sg(device->card->device, sg, orb->cmd->use_sg,
875 orb->cmd->sc_data_direction);
876 }
877
878 if (orb->page_table_bus != 0)
879 dma_unmap_single(device->card->device, orb->page_table_bus,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400880 sizeof(orb->page_table_bus), DMA_TO_DEVICE);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500881
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400882 orb->cmd->result = result;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500883 orb->done(orb->cmd);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500884 kfree(orb);
885}
886
Kristian Høgsberg95ffc5e2007-05-09 19:23:08 -0400887static int sbp2_command_orb_map_scatterlist(struct sbp2_command_orb *orb)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500888{
Kristian Høgsbergad852742007-05-09 19:23:07 -0400889 struct sbp2_device *sd =
890 (struct sbp2_device *)orb->cmd->device->host->hostdata;
891 struct fw_unit *unit = sd->unit;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500892 struct fw_device *device = fw_device(unit->device.parent);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500893 struct scatterlist *sg;
894 int sg_len, l, i, j, count;
895 size_t size;
896 dma_addr_t sg_addr;
897
898 sg = (struct scatterlist *)orb->cmd->request_buffer;
899 count = dma_map_sg(device->card->device, sg, orb->cmd->use_sg,
900 orb->cmd->sc_data_direction);
Kristian Høgsberg95ffc5e2007-05-09 19:23:08 -0400901 if (count == 0)
902 goto fail;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500903
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400904 /*
905 * Handle the special case where there is only one element in
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500906 * the scatter list by converting it to an immediate block
907 * request. This is also a workaround for broken devices such
908 * as the second generation iPod which doesn't support page
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400909 * tables.
910 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500911 if (count == 1 && sg_dma_len(sg) < SBP2_MAX_SG_ELEMENT_LENGTH) {
912 orb->request.data_descriptor.high = sd->address_high;
913 orb->request.data_descriptor.low = sg_dma_address(sg);
914 orb->request.misc |=
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400915 COMMAND_ORB_DATA_SIZE(sg_dma_len(sg));
Kristian Høgsberg95ffc5e2007-05-09 19:23:08 -0400916 return 0;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500917 }
918
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400919 /*
920 * Convert the scatterlist to an sbp2 page table. If any
Kristian Høgsberg, Stefan Richter36abb3b2007-05-09 19:23:10 -0400921 * scatterlist entries are too big for sbp2, we split them as we
922 * go. Even if we ask the block I/O layer to not give us sg
923 * elements larger than 65535 bytes, some IOMMUs may merge sg elements
924 * during DMA mapping, and Linux currently doesn't prevent this.
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400925 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500926 for (i = 0, j = 0; i < count; i++) {
927 sg_len = sg_dma_len(sg + i);
928 sg_addr = sg_dma_address(sg + i);
929 while (sg_len) {
930 l = min(sg_len, SBP2_MAX_SG_ELEMENT_LENGTH);
931 orb->page_table[j].low = sg_addr;
932 orb->page_table[j].high = (l << 16);
933 sg_addr += l;
934 sg_len -= l;
935 j++;
936 }
937 }
938
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400939 size = sizeof(orb->page_table[0]) * j;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500940
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400941 /*
942 * The data_descriptor pointer is the one case where we need
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500943 * to fill in the node ID part of the address. All other
944 * pointers assume that the data referenced reside on the
945 * initiator (i.e. us), but data_descriptor can refer to data
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400946 * on other nodes so we need to put our ID in descriptor.high.
947 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500948
949 orb->page_table_bus =
950 dma_map_single(device->card->device, orb->page_table,
951 size, DMA_TO_DEVICE);
Kristian Høgsberg95ffc5e2007-05-09 19:23:08 -0400952 if (dma_mapping_error(orb->page_table_bus))
953 goto fail_page_table;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500954 orb->request.data_descriptor.high = sd->address_high;
955 orb->request.data_descriptor.low = orb->page_table_bus;
956 orb->request.misc |=
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400957 COMMAND_ORB_PAGE_TABLE_PRESENT |
958 COMMAND_ORB_DATA_SIZE(j);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500959
960 fw_memcpy_to_be32(orb->page_table, orb->page_table, size);
Kristian Høgsberg95ffc5e2007-05-09 19:23:08 -0400961
962 return 0;
963
964 fail_page_table:
965 dma_unmap_sg(device->card->device, sg, orb->cmd->use_sg,
966 orb->cmd->sc_data_direction);
967 fail:
968 return -ENOMEM;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500969}
970
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500971/* SCSI stack integration */
972
973static int sbp2_scsi_queuecommand(struct scsi_cmnd *cmd, scsi_done_fn_t done)
974{
Kristian Høgsbergad852742007-05-09 19:23:07 -0400975 struct sbp2_device *sd =
976 (struct sbp2_device *)cmd->device->host->hostdata;
977 struct fw_unit *unit = sd->unit;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500978 struct fw_device *device = fw_device(unit->device.parent);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500979 struct sbp2_command_orb *orb;
980
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400981 /*
982 * Bidirectional commands are not yet implemented, and unknown
983 * transfer direction not handled.
984 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500985 if (cmd->sc_data_direction == DMA_BIDIRECTIONAL) {
Stefan Richter8a8cea22007-06-09 19:26:22 +0200986 fw_error("Can't handle DMA_BIDIRECTIONAL, rejecting command\n");
Kristian Høgsberge1b68c42007-05-09 19:23:09 -0400987 cmd->result = DID_ERROR << 16;
988 done(cmd);
989 return 0;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500990 }
991
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400992 orb = kzalloc(sizeof(*orb), GFP_ATOMIC);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500993 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,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001002 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 =
Stefan Richterf1397492007-06-10 21:31:36 +02001019 COMMAND_ORB_MAX_PAYLOAD(device->max_speed + 7) |
1020 COMMAND_ORB_SPEED(device->max_speed) |
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001021 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
Kristian Høgsberg213d7bb2007-05-09 19:23:11 -04001030 if (cmd->use_sg && sbp2_command_orb_map_scatterlist(orb) < 0)
Kristian Høgsberg95ffc5e2007-05-09 19:23:08 -04001031 goto fail_map_payload;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001032
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001033 fw_memcpy_to_be32(&orb->request, &orb->request, sizeof(orb->request));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001034
1035 memset(orb->request.command_block,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001036 0, sizeof(orb->request.command_block));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001037 memcpy(orb->request.command_block, cmd->cmnd, COMMAND_SIZE(*cmd->cmnd));
1038
1039 orb->base.callback = complete_command_orb;
1040
1041 sbp2_send_orb(&orb->base, unit, sd->node_id, sd->generation,
1042 sd->command_block_agent_address + SBP2_ORB_POINTER);
1043
1044 return 0;
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -05001045
Kristian Høgsberg95ffc5e2007-05-09 19:23:08 -04001046 fail_map_payload:
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -05001047 dma_unmap_single(device->card->device, orb->base.request_bus,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001048 sizeof(orb->request), DMA_TO_DEVICE);
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -05001049 fail_mapping:
1050 kfree(orb);
1051 fail_alloc:
Kristian Høgsberge1b68c42007-05-09 19:23:09 -04001052 return SCSI_MLQUEUE_HOST_BUSY;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001053}
1054
Stefan Richtercfb01382007-01-23 21:20:08 +01001055static int sbp2_scsi_slave_alloc(struct scsi_device *sdev)
1056{
Kristian Høgsbergad852742007-05-09 19:23:07 -04001057 struct sbp2_device *sd = (struct sbp2_device *)sdev->host->hostdata;
Stefan Richtercfb01382007-01-23 21:20:08 +01001058
1059 sdev->allow_restart = 1;
1060
1061 if (sd->workarounds & SBP2_WORKAROUND_INQUIRY_36)
1062 sdev->inquiry_len = 36;
1063 return 0;
1064}
1065
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001066static int sbp2_scsi_slave_configure(struct scsi_device *sdev)
1067{
Kristian Høgsbergad852742007-05-09 19:23:07 -04001068 struct sbp2_device *sd = (struct sbp2_device *)sdev->host->hostdata;
1069 struct fw_unit *unit = sd->unit;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001070
Stefan Richtercfb01382007-01-23 21:20:08 +01001071 sdev->use_10_for_rw = 1;
1072
1073 if (sdev->type == TYPE_ROM)
1074 sdev->use_10_for_ms = 1;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001075 if (sdev->type == TYPE_DISK &&
1076 sd->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
1077 sdev->skip_ms_page_8 = 1;
1078 if (sd->workarounds & SBP2_WORKAROUND_FIX_CAPACITY) {
1079 fw_notify("setting fix_capacity for %s\n", unit->device.bus_id);
1080 sdev->fix_capacity = 1;
1081 }
Stefan Richtercf47c7a2007-06-17 23:52:08 +02001082 if (sd->workarounds & SBP2_WORKAROUND_128K_MAX_TRANS)
1083 blk_queue_max_sectors(sdev->request_queue, 128 * 1024 / 512);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001084 return 0;
1085}
1086
1087/*
1088 * Called by scsi stack when something has really gone wrong. Usually
1089 * called when a command has timed-out for some reason.
1090 */
1091static int sbp2_scsi_abort(struct scsi_cmnd *cmd)
1092{
Kristian Høgsbergad852742007-05-09 19:23:07 -04001093 struct sbp2_device *sd =
1094 (struct sbp2_device *)cmd->device->host->hostdata;
1095 struct fw_unit *unit = sd->unit;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001096
1097 fw_notify("sbp2_scsi_abort\n");
Kristian Høgsberg0fc7d6e2007-04-11 18:44:33 -04001098 sbp2_agent_reset(unit);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001099 sbp2_cancel_orbs(unit);
1100
1101 return SUCCESS;
1102}
1103
Stefan Richter14e21982007-05-27 13:18:27 +02001104/*
1105 * Format of /sys/bus/scsi/devices/.../ieee1394_id:
1106 * u64 EUI-64 : u24 directory_ID : u16 LUN (all printed in hexadecimal)
1107 *
1108 * This is the concatenation of target port identifier and logical unit
1109 * identifier as per SAM-2...SAM-4 annex A.
1110 */
1111static ssize_t
1112sbp2_sysfs_ieee1394_id_show(struct device *dev, struct device_attribute *attr,
1113 char *buf)
1114{
1115 struct scsi_device *sdev = to_scsi_device(dev);
1116 struct sbp2_device *sd;
1117 struct fw_unit *unit;
1118 struct fw_device *device;
1119 u32 directory_id;
1120 struct fw_csr_iterator ci;
1121 int key, value, lun;
1122
1123 if (!sdev)
1124 return 0;
1125 sd = (struct sbp2_device *)sdev->host->hostdata;
1126 unit = sd->unit;
1127 device = fw_device(unit->device.parent);
1128
1129 /* implicit directory ID */
1130 directory_id = ((unit->directory - device->config_rom) * 4
1131 + CSR_CONFIG_ROM) & 0xffffff;
1132
1133 /* explicit directory ID, overrides implicit ID if present */
1134 fw_csr_iterator_init(&ci, unit->directory);
1135 while (fw_csr_iterator_next(&ci, &key, &value))
1136 if (key == CSR_DIRECTORY_ID) {
1137 directory_id = value;
1138 break;
1139 }
1140
1141 /* FIXME: Make this work for multi-lun devices. */
1142 lun = 0;
1143
1144 return sprintf(buf, "%08x%08x:%06x:%04x\n",
1145 device->config_rom[3], device->config_rom[4],
1146 directory_id, lun);
1147}
1148
1149static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
1150
1151static struct device_attribute *sbp2_scsi_sysfs_attrs[] = {
1152 &dev_attr_ieee1394_id,
1153 NULL
1154};
1155
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001156static struct scsi_host_template scsi_driver_template = {
1157 .module = THIS_MODULE,
1158 .name = "SBP-2 IEEE-1394",
1159 .proc_name = (char *)sbp2_driver_name,
1160 .queuecommand = sbp2_scsi_queuecommand,
Stefan Richtercfb01382007-01-23 21:20:08 +01001161 .slave_alloc = sbp2_scsi_slave_alloc,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001162 .slave_configure = sbp2_scsi_slave_configure,
1163 .eh_abort_handler = sbp2_scsi_abort,
1164 .this_id = -1,
1165 .sg_tablesize = SG_ALL,
1166 .use_clustering = ENABLE_CLUSTERING,
Stefan Richter02af8e72007-01-21 20:50:11 +01001167 .cmd_per_lun = 1,
1168 .can_queue = 1,
Stefan Richter14e21982007-05-27 13:18:27 +02001169 .sdev_attrs = sbp2_scsi_sysfs_attrs,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001170};
1171
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001172MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
1173MODULE_DESCRIPTION("SCSI over IEEE1394");
1174MODULE_LICENSE("GPL");
1175MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
1176
Olaf Hering1e4c7b02007-05-05 23:17:13 +02001177/* Provide a module alias so root-on-sbp2 initrds don't break. */
1178#ifndef CONFIG_IEEE1394_SBP2_MODULE
1179MODULE_ALIAS("sbp2");
1180#endif
1181
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001182static int __init sbp2_init(void)
1183{
1184 return driver_register(&sbp2_driver.driver);
1185}
1186
1187static void __exit sbp2_cleanup(void)
1188{
1189 driver_unregister(&sbp2_driver.driver);
1190}
1191
1192module_init(sbp2_init);
1193module_exit(sbp2_cleanup);