blob: 2c39f19a9ee187fab11989e0570a8a19e217d18e [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 Richter5cd54c92007-06-17 23:55:41 +020033#include <linux/moduleparam.h>
Stefan Richterfe69ca32006-12-28 12:46:54 +010034#include <linux/mod_devicetable.h>
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050035#include <linux/device.h>
Andrew Morton0b5b2902006-12-27 14:49:23 -080036#include <linux/scatterlist.h>
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050037#include <linux/dma-mapping.h>
Stefan Richtercf47c7a2007-06-17 23:52:08 +020038#include <linux/blkdev.h>
Kristian Høgsberg1d3d52c2007-02-06 14:49:33 -050039#include <linux/timer.h>
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050040
41#include <scsi/scsi.h>
42#include <scsi/scsi_cmnd.h>
43#include <scsi/scsi_dbg.h>
44#include <scsi/scsi_device.h>
45#include <scsi/scsi_host.h>
46
47#include "fw-transaction.h"
48#include "fw-topology.h"
49#include "fw-device.h"
50
Stefan Richter5cd54c92007-06-17 23:55:41 +020051/*
52 * So far only bridges from Oxford Semiconductor are known to support
53 * concurrent logins. Depending on firmware, four or two concurrent logins
54 * are possible on OXFW911 and newer Oxsemi bridges.
55 *
56 * Concurrent logins are useful together with cluster filesystems.
57 */
58static int sbp2_param_exclusive_login = 1;
59module_param_named(exclusive_login, sbp2_param_exclusive_login, bool, 0644);
60MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device "
61 "(default = Y, use N for concurrent initiators)");
62
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050063/* I don't know why the SCSI stack doesn't define something like this... */
Kristian Høgsberga98e2712007-05-07 20:33:34 -040064typedef void (*scsi_done_fn_t)(struct scsi_cmnd *);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050065
66static const char sbp2_driver_name[] = "sbp2";
67
68struct sbp2_device {
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -040069 struct kref kref;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050070 struct fw_unit *unit;
71 struct fw_address_handler address_handler;
72 struct list_head orb_list;
73 u64 management_agent_address;
74 u64 command_block_agent_address;
75 u32 workarounds;
76 int login_id;
77
Kristian Høgsbergc781c062007-05-07 20:33:32 -040078 /*
79 * We cache these addresses and only update them once we've
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050080 * logged in or reconnected to the sbp2 device. That way, any
81 * IO to the device will automatically fail and get retried if
82 * it happens in a window where the device is not ready to
Kristian Høgsbergc781c062007-05-07 20:33:32 -040083 * handle it (e.g. after a bus reset but before we reconnect).
84 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050085 int node_id;
86 int address_high;
87 int generation;
88
Kristian Høgsberg7f37c422007-02-06 14:49:34 -050089 int retries;
90 struct delayed_work work;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050091};
92
93#define SBP2_MAX_SG_ELEMENT_LENGTH 0xf000
94#define SBP2_MAX_SECTORS 255 /* Max sectors supported */
Kristian Høgsberg1d3d52c2007-02-06 14:49:33 -050095#define SBP2_ORB_TIMEOUT 2000 /* Timeout in ms */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050096
97#define SBP2_ORB_NULL 0x80000000
98
99#define SBP2_DIRECTION_TO_MEDIA 0x0
100#define SBP2_DIRECTION_FROM_MEDIA 0x1
101
102/* Unit directory keys */
103#define SBP2_COMMAND_SET_SPECIFIER 0x38
104#define SBP2_COMMAND_SET 0x39
105#define SBP2_COMMAND_SET_REVISION 0x3b
106#define SBP2_FIRMWARE_REVISION 0x3c
107
108/* Flags for detected oddities and brokeness */
109#define SBP2_WORKAROUND_128K_MAX_TRANS 0x1
110#define SBP2_WORKAROUND_INQUIRY_36 0x2
111#define SBP2_WORKAROUND_MODE_SENSE_8 0x4
112#define SBP2_WORKAROUND_FIX_CAPACITY 0x8
113#define SBP2_WORKAROUND_OVERRIDE 0x100
114
115/* Management orb opcodes */
116#define SBP2_LOGIN_REQUEST 0x0
117#define SBP2_QUERY_LOGINS_REQUEST 0x1
118#define SBP2_RECONNECT_REQUEST 0x3
119#define SBP2_SET_PASSWORD_REQUEST 0x4
120#define SBP2_LOGOUT_REQUEST 0x7
121#define SBP2_ABORT_TASK_REQUEST 0xb
122#define SBP2_ABORT_TASK_SET 0xc
123#define SBP2_LOGICAL_UNIT_RESET 0xe
124#define SBP2_TARGET_RESET_REQUEST 0xf
125
126/* Offsets for command block agent registers */
127#define SBP2_AGENT_STATE 0x00
128#define SBP2_AGENT_RESET 0x04
129#define SBP2_ORB_POINTER 0x08
130#define SBP2_DOORBELL 0x10
131#define SBP2_UNSOLICITED_STATUS_ENABLE 0x14
132
133/* Status write response codes */
134#define SBP2_STATUS_REQUEST_COMPLETE 0x0
135#define SBP2_STATUS_TRANSPORT_FAILURE 0x1
136#define SBP2_STATUS_ILLEGAL_REQUEST 0x2
137#define SBP2_STATUS_VENDOR_DEPENDENT 0x3
138
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400139#define STATUS_GET_ORB_HIGH(v) ((v).status & 0xffff)
140#define STATUS_GET_SBP_STATUS(v) (((v).status >> 16) & 0xff)
141#define STATUS_GET_LEN(v) (((v).status >> 24) & 0x07)
142#define STATUS_GET_DEAD(v) (((v).status >> 27) & 0x01)
143#define STATUS_GET_RESPONSE(v) (((v).status >> 28) & 0x03)
144#define STATUS_GET_SOURCE(v) (((v).status >> 30) & 0x03)
145#define STATUS_GET_ORB_LOW(v) ((v).orb_low)
146#define STATUS_GET_DATA(v) ((v).data)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500147
148struct sbp2_status {
149 u32 status;
150 u32 orb_low;
151 u8 data[24];
152};
153
154struct sbp2_pointer {
155 u32 high;
156 u32 low;
157};
158
159struct sbp2_orb {
160 struct fw_transaction t;
161 dma_addr_t request_bus;
162 int rcode;
163 struct sbp2_pointer pointer;
Kristian Høgsberga98e2712007-05-07 20:33:34 -0400164 void (*callback)(struct sbp2_orb * orb, struct sbp2_status * status);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500165 struct list_head link;
166};
167
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400168#define MANAGEMENT_ORB_LUN(v) ((v))
169#define MANAGEMENT_ORB_FUNCTION(v) ((v) << 16)
170#define MANAGEMENT_ORB_RECONNECT(v) ((v) << 20)
Stefan Richter5cd54c92007-06-17 23:55:41 +0200171#define MANAGEMENT_ORB_EXCLUSIVE(v) ((v) ? 1 << 28 : 0)
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400172#define MANAGEMENT_ORB_REQUEST_FORMAT(v) ((v) << 29)
173#define MANAGEMENT_ORB_NOTIFY ((1) << 31)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500174
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400175#define MANAGEMENT_ORB_RESPONSE_LENGTH(v) ((v))
176#define MANAGEMENT_ORB_PASSWORD_LENGTH(v) ((v) << 16)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500177
178struct sbp2_management_orb {
179 struct sbp2_orb base;
180 struct {
181 struct sbp2_pointer password;
182 struct sbp2_pointer response;
183 u32 misc;
184 u32 length;
185 struct sbp2_pointer status_fifo;
186 } request;
187 __be32 response[4];
188 dma_addr_t response_bus;
189 struct completion done;
190 struct sbp2_status status;
191};
192
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400193#define LOGIN_RESPONSE_GET_LOGIN_ID(v) ((v).misc & 0xffff)
194#define LOGIN_RESPONSE_GET_LENGTH(v) (((v).misc >> 16) & 0xffff)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500195
196struct sbp2_login_response {
197 u32 misc;
198 struct sbp2_pointer command_block_agent;
199 u32 reconnect_hold;
200};
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400201#define COMMAND_ORB_DATA_SIZE(v) ((v))
202#define COMMAND_ORB_PAGE_SIZE(v) ((v) << 16)
203#define COMMAND_ORB_PAGE_TABLE_PRESENT ((1) << 19)
204#define COMMAND_ORB_MAX_PAYLOAD(v) ((v) << 20)
205#define COMMAND_ORB_SPEED(v) ((v) << 24)
206#define COMMAND_ORB_DIRECTION(v) ((v) << 27)
207#define COMMAND_ORB_REQUEST_FORMAT(v) ((v) << 29)
208#define COMMAND_ORB_NOTIFY ((1) << 31)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500209
210struct sbp2_command_orb {
211 struct sbp2_orb base;
212 struct {
213 struct sbp2_pointer next;
214 struct sbp2_pointer data_descriptor;
215 u32 misc;
216 u8 command_block[12];
217 } request;
218 struct scsi_cmnd *cmd;
219 scsi_done_fn_t done;
220 struct fw_unit *unit;
221
222 struct sbp2_pointer page_table[SG_ALL];
223 dma_addr_t page_table_bus;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500224};
225
226/*
227 * List of devices with known bugs.
228 *
229 * The firmware_revision field, masked with 0xffff00, is the best
230 * indicator for the type of bridge chip of a device. It yields a few
231 * false positives but this did not break correctly behaving devices
232 * so far. We use ~0 as a wildcard, since the 24 bit values we get
233 * from the config rom can never match that.
234 */
235static const struct {
236 u32 firmware_revision;
237 u32 model;
238 unsigned workarounds;
239} sbp2_workarounds_table[] = {
240 /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
241 .firmware_revision = 0x002800,
242 .model = 0x001010,
243 .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
244 SBP2_WORKAROUND_MODE_SENSE_8,
245 },
246 /* Initio bridges, actually only needed for some older ones */ {
247 .firmware_revision = 0x000200,
248 .model = ~0,
249 .workarounds = SBP2_WORKAROUND_INQUIRY_36,
250 },
251 /* Symbios bridge */ {
252 .firmware_revision = 0xa0b800,
253 .model = ~0,
254 .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
255 },
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400256
257 /*
258 * There are iPods (2nd gen, 3rd gen) with model_id == 0, but
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500259 * these iPods do not feature the read_capacity bug according
260 * to one report. Read_capacity behaviour as well as model_id
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400261 * could change due to Apple-supplied firmware updates though.
262 */
263
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500264 /* iPod 4th generation. */ {
265 .firmware_revision = 0x0a2700,
266 .model = 0x000021,
267 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
268 },
269 /* iPod mini */ {
270 .firmware_revision = 0x0a2700,
271 .model = 0x000023,
272 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
273 },
274 /* iPod Photo */ {
275 .firmware_revision = 0x0a2700,
276 .model = 0x00007e,
277 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
278 }
279};
280
281static void
282sbp2_status_write(struct fw_card *card, struct fw_request *request,
283 int tcode, int destination, int source,
284 int generation, int speed,
285 unsigned long long offset,
286 void *payload, size_t length, void *callback_data)
287{
288 struct sbp2_device *sd = callback_data;
289 struct sbp2_orb *orb;
290 struct sbp2_status status;
291 size_t header_size;
292 unsigned long flags;
293
294 if (tcode != TCODE_WRITE_BLOCK_REQUEST ||
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400295 length == 0 || length > sizeof(status)) {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500296 fw_send_response(card, request, RCODE_TYPE_ERROR);
297 return;
298 }
299
300 header_size = min(length, 2 * sizeof(u32));
301 fw_memcpy_from_be32(&status, payload, header_size);
302 if (length > header_size)
303 memcpy(status.data, payload + 8, length - header_size);
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400304 if (STATUS_GET_SOURCE(status) == 2 || STATUS_GET_SOURCE(status) == 3) {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500305 fw_notify("non-orb related status write, not handled\n");
306 fw_send_response(card, request, RCODE_COMPLETE);
307 return;
308 }
309
310 /* Lookup the orb corresponding to this status write. */
311 spin_lock_irqsave(&card->lock, flags);
312 list_for_each_entry(orb, &sd->orb_list, link) {
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400313 if (STATUS_GET_ORB_HIGH(status) == 0 &&
314 STATUS_GET_ORB_LOW(status) == orb->request_bus &&
Kristian Høgsberg12f26aa2007-04-10 18:11:20 -0400315 orb->rcode == RCODE_COMPLETE) {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500316 list_del(&orb->link);
317 break;
318 }
319 }
320 spin_unlock_irqrestore(&card->lock, flags);
321
322 if (&orb->link != &sd->orb_list)
323 orb->callback(orb, &status);
324 else
325 fw_error("status write for unknown orb\n");
326
327 fw_send_response(card, request, RCODE_COMPLETE);
328}
329
330static void
331complete_transaction(struct fw_card *card, int rcode,
332 void *payload, size_t length, void *data)
333{
334 struct sbp2_orb *orb = data;
335 unsigned long flags;
336
337 orb->rcode = rcode;
338 if (rcode != RCODE_COMPLETE) {
339 spin_lock_irqsave(&card->lock, flags);
340 list_del(&orb->link);
341 spin_unlock_irqrestore(&card->lock, flags);
342 orb->callback(orb, NULL);
343 }
344}
345
346static void
347sbp2_send_orb(struct sbp2_orb *orb, struct fw_unit *unit,
348 int node_id, int generation, u64 offset)
349{
350 struct fw_device *device = fw_device(unit->device.parent);
351 struct sbp2_device *sd = unit->device.driver_data;
352 unsigned long flags;
353
354 orb->pointer.high = 0;
355 orb->pointer.low = orb->request_bus;
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400356 fw_memcpy_to_be32(&orb->pointer, &orb->pointer, sizeof(orb->pointer));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500357
358 spin_lock_irqsave(&device->card->lock, flags);
359 list_add_tail(&orb->link, &sd->orb_list);
360 spin_unlock_irqrestore(&device->card->lock, flags);
361
362 fw_send_request(device->card, &orb->t, TCODE_WRITE_BLOCK_REQUEST,
Stefan Richterf1397492007-06-10 21:31:36 +0200363 node_id, generation, device->max_speed, offset,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400364 &orb->pointer, sizeof(orb->pointer),
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500365 complete_transaction, orb);
366}
367
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500368static int sbp2_cancel_orbs(struct fw_unit *unit)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500369{
370 struct fw_device *device = fw_device(unit->device.parent);
371 struct sbp2_device *sd = unit->device.driver_data;
372 struct sbp2_orb *orb, *next;
373 struct list_head list;
374 unsigned long flags;
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500375 int retval = -ENOENT;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500376
377 INIT_LIST_HEAD(&list);
378 spin_lock_irqsave(&device->card->lock, flags);
379 list_splice_init(&sd->orb_list, &list);
380 spin_unlock_irqrestore(&device->card->lock, flags);
381
382 list_for_each_entry_safe(orb, next, &list, link) {
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500383 retval = 0;
Kristian Høgsberg730c32f2007-02-06 14:49:32 -0500384 if (fw_cancel_transaction(device->card, &orb->t) == 0)
385 continue;
386
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500387 orb->rcode = RCODE_CANCELLED;
388 orb->callback(orb, NULL);
389 }
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500390
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500391 return retval;
Kristian Høgsberg1d3d52c2007-02-06 14:49:33 -0500392}
393
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500394static void
395complete_management_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
396{
397 struct sbp2_management_orb *orb =
398 (struct sbp2_management_orb *)base_orb;
399
400 if (status)
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400401 memcpy(&orb->status, status, sizeof(*status));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500402 complete(&orb->done);
403}
404
405static int
406sbp2_send_management_orb(struct fw_unit *unit, int node_id, int generation,
407 int function, int lun, void *response)
408{
409 struct fw_device *device = fw_device(unit->device.parent);
410 struct sbp2_device *sd = unit->device.driver_data;
411 struct sbp2_management_orb *orb;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500412 int retval = -ENOMEM;
413
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400414 orb = kzalloc(sizeof(*orb), GFP_ATOMIC);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500415 if (orb == NULL)
416 return -ENOMEM;
417
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400418 /*
419 * The sbp2 device is going to send a block read request to
420 * read out the request from host memory, so map it for dma.
421 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500422 orb->base.request_bus =
423 dma_map_single(device->card->device, &orb->request,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400424 sizeof(orb->request), DMA_TO_DEVICE);
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -0500425 if (dma_mapping_error(orb->base.request_bus))
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500426 goto out;
427
428 orb->response_bus =
429 dma_map_single(device->card->device, &orb->response,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400430 sizeof(orb->response), DMA_FROM_DEVICE);
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -0500431 if (dma_mapping_error(orb->response_bus))
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500432 goto out;
433
434 orb->request.response.high = 0;
435 orb->request.response.low = orb->response_bus;
436
437 orb->request.misc =
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400438 MANAGEMENT_ORB_NOTIFY |
439 MANAGEMENT_ORB_FUNCTION(function) |
440 MANAGEMENT_ORB_LUN(lun);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500441 orb->request.length =
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400442 MANAGEMENT_ORB_RESPONSE_LENGTH(sizeof(orb->response));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500443
444 orb->request.status_fifo.high = sd->address_handler.offset >> 32;
445 orb->request.status_fifo.low = sd->address_handler.offset;
446
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500447 if (function == SBP2_LOGIN_REQUEST) {
448 orb->request.misc |=
Stefan Richter5cd54c92007-06-17 23:55:41 +0200449 MANAGEMENT_ORB_EXCLUSIVE(sbp2_param_exclusive_login) |
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400450 MANAGEMENT_ORB_RECONNECT(0);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500451 }
452
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400453 fw_memcpy_to_be32(&orb->request, &orb->request, sizeof(orb->request));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500454
455 init_completion(&orb->done);
456 orb->base.callback = complete_management_orb;
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500457
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500458 sbp2_send_orb(&orb->base, unit,
459 node_id, generation, sd->management_agent_address);
460
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500461 wait_for_completion_timeout(&orb->done,
462 msecs_to_jiffies(SBP2_ORB_TIMEOUT));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500463
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500464 retval = -EIO;
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500465 if (sbp2_cancel_orbs(unit) == 0) {
466 fw_error("orb reply timed out, rcode=0x%02x\n",
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500467 orb->base.rcode);
468 goto out;
469 }
470
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500471 if (orb->base.rcode != RCODE_COMPLETE) {
472 fw_error("management write failed, rcode 0x%02x\n",
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500473 orb->base.rcode);
474 goto out;
475 }
476
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400477 if (STATUS_GET_RESPONSE(orb->status) != 0 ||
478 STATUS_GET_SBP_STATUS(orb->status) != 0) {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500479 fw_error("error status: %d:%d\n",
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400480 STATUS_GET_RESPONSE(orb->status),
481 STATUS_GET_SBP_STATUS(orb->status));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500482 goto out;
483 }
484
485 retval = 0;
486 out:
487 dma_unmap_single(device->card->device, orb->base.request_bus,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400488 sizeof(orb->request), DMA_TO_DEVICE);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500489 dma_unmap_single(device->card->device, orb->response_bus,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400490 sizeof(orb->response), DMA_FROM_DEVICE);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500491
492 if (response)
493 fw_memcpy_from_be32(response,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400494 orb->response, sizeof(orb->response));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500495 kfree(orb);
496
497 return retval;
498}
499
500static void
501complete_agent_reset_write(struct fw_card *card, int rcode,
502 void *payload, size_t length, void *data)
503{
504 struct fw_transaction *t = data;
505
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500506 kfree(t);
507}
508
509static int sbp2_agent_reset(struct fw_unit *unit)
510{
511 struct fw_device *device = fw_device(unit->device.parent);
512 struct sbp2_device *sd = unit->device.driver_data;
513 struct fw_transaction *t;
514 static u32 zero;
515
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400516 t = kzalloc(sizeof(*t), GFP_ATOMIC);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500517 if (t == NULL)
518 return -ENOMEM;
519
520 fw_send_request(device->card, t, TCODE_WRITE_QUADLET_REQUEST,
Stefan Richter907293d2007-01-23 21:11:43 +0100521 sd->node_id, sd->generation, SCODE_400,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500522 sd->command_block_agent_address + SBP2_AGENT_RESET,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400523 &zero, sizeof(zero), complete_agent_reset_write, t);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500524
525 return 0;
526}
527
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500528static void sbp2_reconnect(struct work_struct *work);
Kristian Høgsbergad85274f2007-05-09 19:23:07 -0400529static struct scsi_host_template scsi_driver_template;
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500530
Stefan Richter79352e92007-06-18 18:46:49 +0200531static void release_sbp2_device(struct kref *kref)
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400532{
533 struct sbp2_device *sd = container_of(kref, struct sbp2_device, kref);
Kristian Høgsbergad85274f2007-05-09 19:23:07 -0400534 struct Scsi_Host *host =
535 container_of((void *)sd, struct Scsi_Host, hostdata[0]);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400536
Stefan Richter79352e92007-06-18 18:46:49 +0200537 scsi_remove_host(host);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400538 sbp2_send_management_orb(sd->unit, sd->node_id, sd->generation,
539 SBP2_LOGOUT_REQUEST, sd->login_id, NULL);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400540 fw_core_remove_address_handler(&sd->address_handler);
541 fw_notify("removed sbp2 unit %s\n", sd->unit->device.bus_id);
542 put_device(&sd->unit->device);
Kristian Høgsbergad85274f2007-05-09 19:23:07 -0400543 scsi_host_put(host);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400544}
545
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500546static void sbp2_login(struct work_struct *work)
547{
548 struct sbp2_device *sd =
549 container_of(work, struct sbp2_device, work.work);
Kristian Høgsbergad85274f2007-05-09 19:23:07 -0400550 struct Scsi_Host *host =
551 container_of((void *)sd, struct Scsi_Host, hostdata[0]);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500552 struct fw_unit *unit = sd->unit;
553 struct fw_device *device = fw_device(unit->device.parent);
554 struct sbp2_login_response response;
555 int generation, node_id, local_node_id, lun, retval;
556
557 /* FIXME: Make this work for multi-lun devices. */
558 lun = 0;
559
560 generation = device->card->generation;
561 node_id = device->node->node_id;
562 local_node_id = device->card->local_node->node_id;
563
564 if (sbp2_send_management_orb(unit, node_id, generation,
565 SBP2_LOGIN_REQUEST, lun, &response) < 0) {
566 if (sd->retries++ < 5) {
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500567 schedule_delayed_work(&sd->work, DIV_ROUND_UP(HZ, 5));
568 } else {
569 fw_error("failed to login to %s\n",
570 unit->device.bus_id);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400571 kref_put(&sd->kref, release_sbp2_device);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500572 }
573 return;
574 }
575
576 sd->generation = generation;
577 sd->node_id = node_id;
578 sd->address_high = local_node_id << 16;
579
580 /* Get command block agent offset and login id. */
581 sd->command_block_agent_address =
Kristian Høgsberg5c5539d2007-03-07 12:12:45 -0500582 ((u64) (response.command_block_agent.high & 0xffff) << 32) |
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500583 response.command_block_agent.low;
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400584 sd->login_id = LOGIN_RESPONSE_GET_LOGIN_ID(response);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500585
Kristian Høgsberg5c5539d2007-03-07 12:12:45 -0500586 fw_notify("logged in to sbp2 unit %s (%d retries)\n",
587 unit->device.bus_id, sd->retries);
588 fw_notify(" - management_agent_address: 0x%012llx\n",
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500589 (unsigned long long) sd->management_agent_address);
590 fw_notify(" - command_block_agent_address: 0x%012llx\n",
591 (unsigned long long) sd->command_block_agent_address);
Kristian Høgsberg5c5539d2007-03-07 12:12:45 -0500592 fw_notify(" - status write address: 0x%012llx\n",
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500593 (unsigned long long) sd->address_handler.offset);
594
595#if 0
596 /* FIXME: The linux1394 sbp2 does this last step. */
597 sbp2_set_busy_timeout(scsi_id);
598#endif
599
Kristian Høgsberg1da0c932007-03-07 12:12:40 -0500600 PREPARE_DELAYED_WORK(&sd->work, sbp2_reconnect);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500601 sbp2_agent_reset(unit);
602
Kristian Høgsbergad85274f2007-05-09 19:23:07 -0400603 /* FIXME: Loop over luns here. */
604 lun = 0;
605 retval = scsi_add_device(host, 0, 0, lun);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500606 if (retval < 0) {
607 sbp2_send_management_orb(unit, sd->node_id, sd->generation,
608 SBP2_LOGOUT_REQUEST, sd->login_id,
609 NULL);
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400610 /*
611 * Set this back to sbp2_login so we fall back and
612 * retry login on bus reset.
613 */
Kristian Høgsberg1da0c932007-03-07 12:12:40 -0500614 PREPARE_DELAYED_WORK(&sd->work, sbp2_login);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500615 }
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400616 kref_put(&sd->kref, release_sbp2_device);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500617}
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500618
619static int sbp2_probe(struct device *dev)
620{
621 struct fw_unit *unit = fw_unit(dev);
622 struct fw_device *device = fw_device(unit->device.parent);
623 struct sbp2_device *sd;
624 struct fw_csr_iterator ci;
Kristian Høgsbergad85274f2007-05-09 19:23:07 -0400625 struct Scsi_Host *host;
626 int i, key, value, err;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500627 u32 model, firmware_revision;
628
Kristian Høgsbergad85274f2007-05-09 19:23:07 -0400629 err = -ENOMEM;
630 host = scsi_host_alloc(&scsi_driver_template, sizeof(*sd));
631 if (host == NULL)
632 goto fail;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500633
Kristian Høgsbergad85274f2007-05-09 19:23:07 -0400634 sd = (struct sbp2_device *) host->hostdata;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500635 unit->device.driver_data = sd;
636 sd->unit = unit;
637 INIT_LIST_HEAD(&sd->orb_list);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400638 kref_init(&sd->kref);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500639
640 sd->address_handler.length = 0x100;
641 sd->address_handler.address_callback = sbp2_status_write;
642 sd->address_handler.callback_data = sd;
643
Kristian Høgsbergad85274f2007-05-09 19:23:07 -0400644 err = fw_core_add_address_handler(&sd->address_handler,
645 &fw_high_memory_region);
646 if (err < 0)
647 goto fail_host;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500648
Kristian Høgsbergad85274f2007-05-09 19:23:07 -0400649 err = fw_device_enable_phys_dma(device);
650 if (err < 0)
651 goto fail_address_handler;
652
653 err = scsi_add_host(host, &unit->device);
654 if (err < 0)
655 goto fail_address_handler;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500656
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400657 /*
658 * Scan unit directory to get management agent address,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500659 * firmware revison and model. Initialize firmware_revision
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400660 * and model to values that wont match anything in our table.
661 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500662 firmware_revision = 0xff000000;
663 model = 0xff000000;
664 fw_csr_iterator_init(&ci, unit->directory);
665 while (fw_csr_iterator_next(&ci, &key, &value)) {
666 switch (key) {
667 case CSR_DEPENDENT_INFO | CSR_OFFSET:
668 sd->management_agent_address =
669 0xfffff0000000ULL + 4 * value;
670 break;
671 case SBP2_FIRMWARE_REVISION:
672 firmware_revision = value;
673 break;
674 case CSR_MODEL:
675 model = value;
676 break;
677 }
678 }
679
680 for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
681 if (sbp2_workarounds_table[i].firmware_revision !=
682 (firmware_revision & 0xffffff00))
683 continue;
684 if (sbp2_workarounds_table[i].model != model &&
685 sbp2_workarounds_table[i].model != ~0)
686 continue;
687 sd->workarounds |= sbp2_workarounds_table[i].workarounds;
688 break;
689 }
690
691 if (sd->workarounds)
692 fw_notify("Workarounds for node %s: 0x%x "
693 "(firmware_revision 0x%06x, model_id 0x%06x)\n",
694 unit->device.bus_id,
695 sd->workarounds, firmware_revision, model);
696
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400697 get_device(&unit->device);
698
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400699 /*
700 * We schedule work to do the login so we can easily
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400701 * reschedule retries. Always get the ref before scheduling
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400702 * work.
703 */
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500704 INIT_DELAYED_WORK(&sd->work, sbp2_login);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400705 if (schedule_delayed_work(&sd->work, 0))
706 kref_get(&sd->kref);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500707
708 return 0;
Kristian Høgsbergad85274f2007-05-09 19:23:07 -0400709
710 fail_address_handler:
711 fw_core_remove_address_handler(&sd->address_handler);
712 fail_host:
713 scsi_host_put(host);
714 fail:
715 return err;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500716}
717
718static int sbp2_remove(struct device *dev)
719{
720 struct fw_unit *unit = fw_unit(dev);
721 struct sbp2_device *sd = unit->device.driver_data;
722
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400723 kref_put(&sd->kref, release_sbp2_device);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500724
725 return 0;
726}
727
728static void sbp2_reconnect(struct work_struct *work)
729{
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500730 struct sbp2_device *sd =
731 container_of(work, struct sbp2_device, work.work);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500732 struct fw_unit *unit = sd->unit;
733 struct fw_device *device = fw_device(unit->device.parent);
734 int generation, node_id, local_node_id;
735
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500736 generation = device->card->generation;
737 node_id = device->node->node_id;
738 local_node_id = device->card->local_node->node_id;
739
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500740 if (sbp2_send_management_orb(unit, node_id, generation,
741 SBP2_RECONNECT_REQUEST,
742 sd->login_id, NULL) < 0) {
Kristian Høgsberg5c5539d2007-03-07 12:12:45 -0500743 if (sd->retries++ >= 5) {
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500744 fw_error("failed to reconnect to %s\n",
745 unit->device.bus_id);
746 /* Fall back and try to log in again. */
747 sd->retries = 0;
Kristian Høgsberg1da0c932007-03-07 12:12:40 -0500748 PREPARE_DELAYED_WORK(&sd->work, sbp2_login);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500749 }
750 schedule_delayed_work(&sd->work, DIV_ROUND_UP(HZ, 5));
751 return;
752 }
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500753
754 sd->generation = generation;
755 sd->node_id = node_id;
Stefan Richter907293d2007-01-23 21:11:43 +0100756 sd->address_high = local_node_id << 16;
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500757
Kristian Høgsberg5c5539d2007-03-07 12:12:45 -0500758 fw_notify("reconnected to unit %s (%d retries)\n",
759 unit->device.bus_id, sd->retries);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500760 sbp2_agent_reset(unit);
761 sbp2_cancel_orbs(unit);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400762 kref_put(&sd->kref, release_sbp2_device);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500763}
764
765static void sbp2_update(struct fw_unit *unit)
766{
767 struct fw_device *device = fw_device(unit->device.parent);
768 struct sbp2_device *sd = unit->device.driver_data;
769
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500770 sd->retries = 0;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500771 fw_device_enable_phys_dma(device);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400772 if (schedule_delayed_work(&sd->work, 0))
773 kref_get(&sd->kref);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500774}
775
776#define SBP2_UNIT_SPEC_ID_ENTRY 0x0000609e
777#define SBP2_SW_VERSION_ENTRY 0x00010483
778
Stefan Richter21ebcd12007-01-14 15:29:07 +0100779static const struct fw_device_id sbp2_id_table[] = {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500780 {
781 .match_flags = FW_MATCH_SPECIFIER_ID | FW_MATCH_VERSION,
782 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY,
Stefan Richter5af4e5e2007-01-21 20:45:32 +0100783 .version = SBP2_SW_VERSION_ENTRY,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500784 },
785 { }
786};
787
788static struct fw_driver sbp2_driver = {
789 .driver = {
790 .owner = THIS_MODULE,
791 .name = sbp2_driver_name,
792 .bus = &fw_bus_type,
793 .probe = sbp2_probe,
794 .remove = sbp2_remove,
795 },
796 .update = sbp2_update,
797 .id_table = sbp2_id_table,
798};
799
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400800static unsigned int
801sbp2_status_to_sense_data(u8 *sbp2_status, u8 *sense_data)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500802{
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400803 int sam_status;
804
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500805 sense_data[0] = 0x70;
806 sense_data[1] = 0x0;
807 sense_data[2] = sbp2_status[1];
808 sense_data[3] = sbp2_status[4];
809 sense_data[4] = sbp2_status[5];
810 sense_data[5] = sbp2_status[6];
811 sense_data[6] = sbp2_status[7];
812 sense_data[7] = 10;
813 sense_data[8] = sbp2_status[8];
814 sense_data[9] = sbp2_status[9];
815 sense_data[10] = sbp2_status[10];
816 sense_data[11] = sbp2_status[11];
817 sense_data[12] = sbp2_status[2];
818 sense_data[13] = sbp2_status[3];
819 sense_data[14] = sbp2_status[12];
820 sense_data[15] = sbp2_status[13];
821
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400822 sam_status = sbp2_status[0] & 0x3f;
823
824 switch (sam_status) {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500825 case SAM_STAT_GOOD:
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500826 case SAM_STAT_CHECK_CONDITION:
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500827 case SAM_STAT_CONDITION_MET:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400828 case SAM_STAT_BUSY:
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500829 case SAM_STAT_RESERVATION_CONFLICT:
830 case SAM_STAT_COMMAND_TERMINATED:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400831 return DID_OK << 16 | sam_status;
832
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500833 default:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400834 return DID_ERROR << 16;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500835 }
836}
837
838static void
839complete_command_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
840{
841 struct sbp2_command_orb *orb = (struct sbp2_command_orb *)base_orb;
842 struct fw_unit *unit = orb->unit;
843 struct fw_device *device = fw_device(unit->device.parent);
844 struct scatterlist *sg;
845 int result;
846
847 if (status != NULL) {
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400848 if (STATUS_GET_DEAD(*status))
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500849 sbp2_agent_reset(unit);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500850
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400851 switch (STATUS_GET_RESPONSE(*status)) {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500852 case SBP2_STATUS_REQUEST_COMPLETE:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400853 result = DID_OK << 16;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500854 break;
855 case SBP2_STATUS_TRANSPORT_FAILURE:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400856 result = DID_BUS_BUSY << 16;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500857 break;
858 case SBP2_STATUS_ILLEGAL_REQUEST:
859 case SBP2_STATUS_VENDOR_DEPENDENT:
860 default:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400861 result = DID_ERROR << 16;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500862 break;
863 }
864
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400865 if (result == DID_OK << 16 && STATUS_GET_LEN(*status) > 1)
866 result = sbp2_status_to_sense_data(STATUS_GET_DATA(*status),
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500867 orb->cmd->sense_buffer);
868 } else {
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400869 /*
870 * If the orb completes with status == NULL, something
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500871 * went wrong, typically a bus reset happened mid-orb
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400872 * or when sending the write (less likely).
873 */
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400874 result = DID_BUS_BUSY << 16;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500875 }
876
877 dma_unmap_single(device->card->device, orb->base.request_bus,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400878 sizeof(orb->request), DMA_TO_DEVICE);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500879
880 if (orb->cmd->use_sg > 0) {
881 sg = (struct scatterlist *)orb->cmd->request_buffer;
882 dma_unmap_sg(device->card->device, sg, orb->cmd->use_sg,
883 orb->cmd->sc_data_direction);
884 }
885
886 if (orb->page_table_bus != 0)
887 dma_unmap_single(device->card->device, orb->page_table_bus,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400888 sizeof(orb->page_table_bus), DMA_TO_DEVICE);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500889
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400890 orb->cmd->result = result;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500891 orb->done(orb->cmd);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500892 kfree(orb);
893}
894
Kristian Høgsberg95ffc5e2007-05-09 19:23:08 -0400895static int sbp2_command_orb_map_scatterlist(struct sbp2_command_orb *orb)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500896{
Kristian Høgsbergad85274f2007-05-09 19:23:07 -0400897 struct sbp2_device *sd =
898 (struct sbp2_device *)orb->cmd->device->host->hostdata;
899 struct fw_unit *unit = sd->unit;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500900 struct fw_device *device = fw_device(unit->device.parent);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500901 struct scatterlist *sg;
902 int sg_len, l, i, j, count;
903 size_t size;
904 dma_addr_t sg_addr;
905
906 sg = (struct scatterlist *)orb->cmd->request_buffer;
907 count = dma_map_sg(device->card->device, sg, orb->cmd->use_sg,
908 orb->cmd->sc_data_direction);
Kristian Høgsberg95ffc5e2007-05-09 19:23:08 -0400909 if (count == 0)
910 goto fail;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500911
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400912 /*
913 * Handle the special case where there is only one element in
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500914 * the scatter list by converting it to an immediate block
915 * request. This is also a workaround for broken devices such
916 * as the second generation iPod which doesn't support page
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400917 * tables.
918 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500919 if (count == 1 && sg_dma_len(sg) < SBP2_MAX_SG_ELEMENT_LENGTH) {
920 orb->request.data_descriptor.high = sd->address_high;
921 orb->request.data_descriptor.low = sg_dma_address(sg);
922 orb->request.misc |=
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400923 COMMAND_ORB_DATA_SIZE(sg_dma_len(sg));
Kristian Høgsberg95ffc5e2007-05-09 19:23:08 -0400924 return 0;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500925 }
926
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400927 /*
928 * Convert the scatterlist to an sbp2 page table. If any
Kristian Høgsberg, Stefan Richter36abb3b2007-05-09 19:23:10 -0400929 * scatterlist entries are too big for sbp2, we split them as we
930 * go. Even if we ask the block I/O layer to not give us sg
931 * elements larger than 65535 bytes, some IOMMUs may merge sg elements
932 * during DMA mapping, and Linux currently doesn't prevent this.
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400933 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500934 for (i = 0, j = 0; i < count; i++) {
935 sg_len = sg_dma_len(sg + i);
936 sg_addr = sg_dma_address(sg + i);
937 while (sg_len) {
938 l = min(sg_len, SBP2_MAX_SG_ELEMENT_LENGTH);
939 orb->page_table[j].low = sg_addr;
940 orb->page_table[j].high = (l << 16);
941 sg_addr += l;
942 sg_len -= l;
943 j++;
944 }
945 }
946
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400947 size = sizeof(orb->page_table[0]) * j;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500948
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400949 /*
950 * The data_descriptor pointer is the one case where we need
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500951 * to fill in the node ID part of the address. All other
952 * pointers assume that the data referenced reside on the
953 * initiator (i.e. us), but data_descriptor can refer to data
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400954 * on other nodes so we need to put our ID in descriptor.high.
955 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500956
957 orb->page_table_bus =
958 dma_map_single(device->card->device, orb->page_table,
959 size, DMA_TO_DEVICE);
Kristian Høgsberg95ffc5e2007-05-09 19:23:08 -0400960 if (dma_mapping_error(orb->page_table_bus))
961 goto fail_page_table;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500962 orb->request.data_descriptor.high = sd->address_high;
963 orb->request.data_descriptor.low = orb->page_table_bus;
964 orb->request.misc |=
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400965 COMMAND_ORB_PAGE_TABLE_PRESENT |
966 COMMAND_ORB_DATA_SIZE(j);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500967
968 fw_memcpy_to_be32(orb->page_table, orb->page_table, size);
Kristian Høgsberg95ffc5e2007-05-09 19:23:08 -0400969
970 return 0;
971
972 fail_page_table:
973 dma_unmap_sg(device->card->device, sg, orb->cmd->use_sg,
974 orb->cmd->sc_data_direction);
975 fail:
976 return -ENOMEM;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500977}
978
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500979/* SCSI stack integration */
980
981static int sbp2_scsi_queuecommand(struct scsi_cmnd *cmd, scsi_done_fn_t done)
982{
Kristian Høgsbergad85274f2007-05-09 19:23:07 -0400983 struct sbp2_device *sd =
984 (struct sbp2_device *)cmd->device->host->hostdata;
985 struct fw_unit *unit = sd->unit;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500986 struct fw_device *device = fw_device(unit->device.parent);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500987 struct sbp2_command_orb *orb;
988
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400989 /*
990 * Bidirectional commands are not yet implemented, and unknown
991 * transfer direction not handled.
992 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500993 if (cmd->sc_data_direction == DMA_BIDIRECTIONAL) {
Stefan Richter8a8cea22007-06-09 19:26:22 +0200994 fw_error("Can't handle DMA_BIDIRECTIONAL, rejecting command\n");
Kristian Høgsberge1b68c42007-05-09 19:23:09 -0400995 cmd->result = DID_ERROR << 16;
996 done(cmd);
997 return 0;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500998 }
999
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001000 orb = kzalloc(sizeof(*orb), GFP_ATOMIC);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001001 if (orb == NULL) {
1002 fw_notify("failed to alloc orb\n");
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -05001003 goto fail_alloc;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001004 }
1005
Kristian Høgsberg12f26aa2007-04-10 18:11:20 -04001006 /* Initialize rcode to something not RCODE_COMPLETE. */
1007 orb->base.rcode = -1;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001008 orb->base.request_bus =
1009 dma_map_single(device->card->device, &orb->request,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001010 sizeof(orb->request), DMA_TO_DEVICE);
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -05001011 if (dma_mapping_error(orb->base.request_bus))
1012 goto fail_mapping;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001013
1014 orb->unit = unit;
1015 orb->done = done;
1016 orb->cmd = cmd;
1017
1018 orb->request.next.high = SBP2_ORB_NULL;
1019 orb->request.next.low = 0x0;
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001020 /*
1021 * At speed 100 we can do 512 bytes per packet, at speed 200,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001022 * 1024 bytes per packet etc. The SBP-2 max_payload field
1023 * specifies the max payload size as 2 ^ (max_payload + 2), so
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001024 * if we set this to max_speed + 7, we get the right value.
1025 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001026 orb->request.misc =
Stefan Richterf1397492007-06-10 21:31:36 +02001027 COMMAND_ORB_MAX_PAYLOAD(device->max_speed + 7) |
1028 COMMAND_ORB_SPEED(device->max_speed) |
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001029 COMMAND_ORB_NOTIFY;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001030
1031 if (cmd->sc_data_direction == DMA_FROM_DEVICE)
1032 orb->request.misc |=
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001033 COMMAND_ORB_DIRECTION(SBP2_DIRECTION_FROM_MEDIA);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001034 else if (cmd->sc_data_direction == DMA_TO_DEVICE)
1035 orb->request.misc |=
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001036 COMMAND_ORB_DIRECTION(SBP2_DIRECTION_TO_MEDIA);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001037
Kristian Høgsberg213d7bb2007-05-09 19:23:11 -04001038 if (cmd->use_sg && sbp2_command_orb_map_scatterlist(orb) < 0)
Kristian Høgsberg95ffc5e2007-05-09 19:23:08 -04001039 goto fail_map_payload;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001040
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001041 fw_memcpy_to_be32(&orb->request, &orb->request, sizeof(orb->request));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001042
1043 memset(orb->request.command_block,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001044 0, sizeof(orb->request.command_block));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001045 memcpy(orb->request.command_block, cmd->cmnd, COMMAND_SIZE(*cmd->cmnd));
1046
1047 orb->base.callback = complete_command_orb;
1048
1049 sbp2_send_orb(&orb->base, unit, sd->node_id, sd->generation,
1050 sd->command_block_agent_address + SBP2_ORB_POINTER);
1051
1052 return 0;
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -05001053
Kristian Høgsberg95ffc5e2007-05-09 19:23:08 -04001054 fail_map_payload:
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -05001055 dma_unmap_single(device->card->device, orb->base.request_bus,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001056 sizeof(orb->request), DMA_TO_DEVICE);
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -05001057 fail_mapping:
1058 kfree(orb);
1059 fail_alloc:
Kristian Høgsberge1b68c42007-05-09 19:23:09 -04001060 return SCSI_MLQUEUE_HOST_BUSY;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001061}
1062
Stefan Richtercfb01382007-01-23 21:20:08 +01001063static int sbp2_scsi_slave_alloc(struct scsi_device *sdev)
1064{
Kristian Høgsbergad85274f2007-05-09 19:23:07 -04001065 struct sbp2_device *sd = (struct sbp2_device *)sdev->host->hostdata;
Stefan Richtercfb01382007-01-23 21:20:08 +01001066
1067 sdev->allow_restart = 1;
1068
1069 if (sd->workarounds & SBP2_WORKAROUND_INQUIRY_36)
1070 sdev->inquiry_len = 36;
1071 return 0;
1072}
1073
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001074static int sbp2_scsi_slave_configure(struct scsi_device *sdev)
1075{
Kristian Høgsbergad85274f2007-05-09 19:23:07 -04001076 struct sbp2_device *sd = (struct sbp2_device *)sdev->host->hostdata;
1077 struct fw_unit *unit = sd->unit;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001078
Stefan Richtercfb01382007-01-23 21:20:08 +01001079 sdev->use_10_for_rw = 1;
1080
1081 if (sdev->type == TYPE_ROM)
1082 sdev->use_10_for_ms = 1;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001083 if (sdev->type == TYPE_DISK &&
1084 sd->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
1085 sdev->skip_ms_page_8 = 1;
1086 if (sd->workarounds & SBP2_WORKAROUND_FIX_CAPACITY) {
1087 fw_notify("setting fix_capacity for %s\n", unit->device.bus_id);
1088 sdev->fix_capacity = 1;
1089 }
Stefan Richtercf47c7a2007-06-17 23:52:08 +02001090 if (sd->workarounds & SBP2_WORKAROUND_128K_MAX_TRANS)
1091 blk_queue_max_sectors(sdev->request_queue, 128 * 1024 / 512);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001092 return 0;
1093}
1094
1095/*
1096 * Called by scsi stack when something has really gone wrong. Usually
1097 * called when a command has timed-out for some reason.
1098 */
1099static int sbp2_scsi_abort(struct scsi_cmnd *cmd)
1100{
Kristian Høgsbergad85274f2007-05-09 19:23:07 -04001101 struct sbp2_device *sd =
1102 (struct sbp2_device *)cmd->device->host->hostdata;
1103 struct fw_unit *unit = sd->unit;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001104
1105 fw_notify("sbp2_scsi_abort\n");
Kristian Høgsberg0fc7d6e2007-04-11 18:44:33 -04001106 sbp2_agent_reset(unit);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001107 sbp2_cancel_orbs(unit);
1108
1109 return SUCCESS;
1110}
1111
Stefan Richter14e21982007-05-27 13:18:27 +02001112/*
1113 * Format of /sys/bus/scsi/devices/.../ieee1394_id:
1114 * u64 EUI-64 : u24 directory_ID : u16 LUN (all printed in hexadecimal)
1115 *
1116 * This is the concatenation of target port identifier and logical unit
1117 * identifier as per SAM-2...SAM-4 annex A.
1118 */
1119static ssize_t
1120sbp2_sysfs_ieee1394_id_show(struct device *dev, struct device_attribute *attr,
1121 char *buf)
1122{
1123 struct scsi_device *sdev = to_scsi_device(dev);
1124 struct sbp2_device *sd;
1125 struct fw_unit *unit;
1126 struct fw_device *device;
1127 u32 directory_id;
1128 struct fw_csr_iterator ci;
1129 int key, value, lun;
1130
1131 if (!sdev)
1132 return 0;
1133 sd = (struct sbp2_device *)sdev->host->hostdata;
1134 unit = sd->unit;
1135 device = fw_device(unit->device.parent);
1136
1137 /* implicit directory ID */
1138 directory_id = ((unit->directory - device->config_rom) * 4
1139 + CSR_CONFIG_ROM) & 0xffffff;
1140
1141 /* explicit directory ID, overrides implicit ID if present */
1142 fw_csr_iterator_init(&ci, unit->directory);
1143 while (fw_csr_iterator_next(&ci, &key, &value))
1144 if (key == CSR_DIRECTORY_ID) {
1145 directory_id = value;
1146 break;
1147 }
1148
1149 /* FIXME: Make this work for multi-lun devices. */
1150 lun = 0;
1151
1152 return sprintf(buf, "%08x%08x:%06x:%04x\n",
1153 device->config_rom[3], device->config_rom[4],
1154 directory_id, lun);
1155}
1156
1157static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
1158
1159static struct device_attribute *sbp2_scsi_sysfs_attrs[] = {
1160 &dev_attr_ieee1394_id,
1161 NULL
1162};
1163
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001164static struct scsi_host_template scsi_driver_template = {
1165 .module = THIS_MODULE,
1166 .name = "SBP-2 IEEE-1394",
1167 .proc_name = (char *)sbp2_driver_name,
1168 .queuecommand = sbp2_scsi_queuecommand,
Stefan Richtercfb01382007-01-23 21:20:08 +01001169 .slave_alloc = sbp2_scsi_slave_alloc,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001170 .slave_configure = sbp2_scsi_slave_configure,
1171 .eh_abort_handler = sbp2_scsi_abort,
1172 .this_id = -1,
1173 .sg_tablesize = SG_ALL,
1174 .use_clustering = ENABLE_CLUSTERING,
Stefan Richter02af8e72007-01-21 20:50:11 +01001175 .cmd_per_lun = 1,
1176 .can_queue = 1,
Stefan Richter14e21982007-05-27 13:18:27 +02001177 .sdev_attrs = sbp2_scsi_sysfs_attrs,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001178};
1179
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001180MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
1181MODULE_DESCRIPTION("SCSI over IEEE1394");
1182MODULE_LICENSE("GPL");
1183MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
1184
Olaf Hering1e4c7b02007-05-05 23:17:13 +02001185/* Provide a module alias so root-on-sbp2 initrds don't break. */
1186#ifndef CONFIG_IEEE1394_SBP2_MODULE
1187MODULE_ALIAS("sbp2");
1188#endif
1189
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001190static int __init sbp2_init(void)
1191{
1192 return driver_register(&sbp2_driver.driver);
1193}
1194
1195static void __exit sbp2_cleanup(void)
1196{
1197 driver_unregister(&sbp2_driver.driver);
1198}
1199
1200module_init(sbp2_init);
1201module_exit(sbp2_cleanup);