blob: 851c2da060d24175a7e059160029ce61a5dae56f [file] [log] [blame]
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001/*
2 * SBP2 driver (SCSI over IEEE1394)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05003 *
Kristian Høgsberg27a15e52007-02-06 14:49:39 -05004 * Copyright (C) 2005-2007 Kristian Hoegsberg <krh@bitplanet.net>
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
Kristian Høgsbergc781c062007-05-07 20:33:32 -040021/*
22 * The basic structure of this driver is based on the old storage driver,
Kristian Høgsberg27a15e52007-02-06 14:49:39 -050023 * drivers/ieee1394/sbp2.c, originally written by
24 * James Goodwin <jamesg@filanet.com>
25 * with later contributions and ongoing maintenance from
26 * Ben Collins <bcollins@debian.org>,
27 * Stefan Richter <stefanr@s5r6.in-berlin.de>
28 * and many others.
29 */
30
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050031#include <linux/kernel.h>
32#include <linux/module.h>
Stefan Richterfe69ca32006-12-28 12:46:54 +010033#include <linux/mod_devicetable.h>
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050034#include <linux/device.h>
Andrew Morton0b5b2902006-12-27 14:49:23 -080035#include <linux/scatterlist.h>
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050036#include <linux/dma-mapping.h>
Kristian Høgsberg1d3d52c2007-02-06 14:49:33 -050037#include <linux/timer.h>
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050038
39#include <scsi/scsi.h>
40#include <scsi/scsi_cmnd.h>
41#include <scsi/scsi_dbg.h>
42#include <scsi/scsi_device.h>
43#include <scsi/scsi_host.h>
44
45#include "fw-transaction.h"
46#include "fw-topology.h"
47#include "fw-device.h"
48
49/* I don't know why the SCSI stack doesn't define something like this... */
Kristian Høgsberga98e2712007-05-07 20:33:34 -040050typedef void (*scsi_done_fn_t)(struct scsi_cmnd *);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050051
52static const char sbp2_driver_name[] = "sbp2";
53
54struct sbp2_device {
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -040055 struct kref kref;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050056 struct fw_unit *unit;
57 struct fw_address_handler address_handler;
58 struct list_head orb_list;
59 u64 management_agent_address;
60 u64 command_block_agent_address;
61 u32 workarounds;
62 int login_id;
63
Kristian Høgsbergc781c062007-05-07 20:33:32 -040064 /*
65 * We cache these addresses and only update them once we've
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050066 * logged in or reconnected to the sbp2 device. That way, any
67 * IO to the device will automatically fail and get retried if
68 * it happens in a window where the device is not ready to
Kristian Høgsbergc781c062007-05-07 20:33:32 -040069 * handle it (e.g. after a bus reset but before we reconnect).
70 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050071 int node_id;
72 int address_high;
73 int generation;
74
Kristian Høgsberg7f37c422007-02-06 14:49:34 -050075 int retries;
76 struct delayed_work work;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050077};
78
79#define SBP2_MAX_SG_ELEMENT_LENGTH 0xf000
80#define SBP2_MAX_SECTORS 255 /* Max sectors supported */
Kristian Høgsberg1d3d52c2007-02-06 14:49:33 -050081#define SBP2_ORB_TIMEOUT 2000 /* Timeout in ms */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -050082
83#define SBP2_ORB_NULL 0x80000000
84
85#define SBP2_DIRECTION_TO_MEDIA 0x0
86#define SBP2_DIRECTION_FROM_MEDIA 0x1
87
88/* Unit directory keys */
89#define SBP2_COMMAND_SET_SPECIFIER 0x38
90#define SBP2_COMMAND_SET 0x39
91#define SBP2_COMMAND_SET_REVISION 0x3b
92#define SBP2_FIRMWARE_REVISION 0x3c
93
94/* Flags for detected oddities and brokeness */
95#define SBP2_WORKAROUND_128K_MAX_TRANS 0x1
96#define SBP2_WORKAROUND_INQUIRY_36 0x2
97#define SBP2_WORKAROUND_MODE_SENSE_8 0x4
98#define SBP2_WORKAROUND_FIX_CAPACITY 0x8
99#define SBP2_WORKAROUND_OVERRIDE 0x100
100
101/* Management orb opcodes */
102#define SBP2_LOGIN_REQUEST 0x0
103#define SBP2_QUERY_LOGINS_REQUEST 0x1
104#define SBP2_RECONNECT_REQUEST 0x3
105#define SBP2_SET_PASSWORD_REQUEST 0x4
106#define SBP2_LOGOUT_REQUEST 0x7
107#define SBP2_ABORT_TASK_REQUEST 0xb
108#define SBP2_ABORT_TASK_SET 0xc
109#define SBP2_LOGICAL_UNIT_RESET 0xe
110#define SBP2_TARGET_RESET_REQUEST 0xf
111
112/* Offsets for command block agent registers */
113#define SBP2_AGENT_STATE 0x00
114#define SBP2_AGENT_RESET 0x04
115#define SBP2_ORB_POINTER 0x08
116#define SBP2_DOORBELL 0x10
117#define SBP2_UNSOLICITED_STATUS_ENABLE 0x14
118
119/* Status write response codes */
120#define SBP2_STATUS_REQUEST_COMPLETE 0x0
121#define SBP2_STATUS_TRANSPORT_FAILURE 0x1
122#define SBP2_STATUS_ILLEGAL_REQUEST 0x2
123#define SBP2_STATUS_VENDOR_DEPENDENT 0x3
124
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400125#define STATUS_GET_ORB_HIGH(v) ((v).status & 0xffff)
126#define STATUS_GET_SBP_STATUS(v) (((v).status >> 16) & 0xff)
127#define STATUS_GET_LEN(v) (((v).status >> 24) & 0x07)
128#define STATUS_GET_DEAD(v) (((v).status >> 27) & 0x01)
129#define STATUS_GET_RESPONSE(v) (((v).status >> 28) & 0x03)
130#define STATUS_GET_SOURCE(v) (((v).status >> 30) & 0x03)
131#define STATUS_GET_ORB_LOW(v) ((v).orb_low)
132#define STATUS_GET_DATA(v) ((v).data)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500133
134struct sbp2_status {
135 u32 status;
136 u32 orb_low;
137 u8 data[24];
138};
139
140struct sbp2_pointer {
141 u32 high;
142 u32 low;
143};
144
145struct sbp2_orb {
146 struct fw_transaction t;
147 dma_addr_t request_bus;
148 int rcode;
149 struct sbp2_pointer pointer;
Kristian Høgsberga98e2712007-05-07 20:33:34 -0400150 void (*callback)(struct sbp2_orb * orb, struct sbp2_status * status);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500151 struct list_head link;
152};
153
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400154#define MANAGEMENT_ORB_LUN(v) ((v))
155#define MANAGEMENT_ORB_FUNCTION(v) ((v) << 16)
156#define MANAGEMENT_ORB_RECONNECT(v) ((v) << 20)
157#define MANAGEMENT_ORB_EXCLUSIVE ((1) << 28)
158#define MANAGEMENT_ORB_REQUEST_FORMAT(v) ((v) << 29)
159#define MANAGEMENT_ORB_NOTIFY ((1) << 31)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500160
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400161#define MANAGEMENT_ORB_RESPONSE_LENGTH(v) ((v))
162#define MANAGEMENT_ORB_PASSWORD_LENGTH(v) ((v) << 16)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500163
164struct sbp2_management_orb {
165 struct sbp2_orb base;
166 struct {
167 struct sbp2_pointer password;
168 struct sbp2_pointer response;
169 u32 misc;
170 u32 length;
171 struct sbp2_pointer status_fifo;
172 } request;
173 __be32 response[4];
174 dma_addr_t response_bus;
175 struct completion done;
176 struct sbp2_status status;
177};
178
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400179#define LOGIN_RESPONSE_GET_LOGIN_ID(v) ((v).misc & 0xffff)
180#define LOGIN_RESPONSE_GET_LENGTH(v) (((v).misc >> 16) & 0xffff)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500181
182struct sbp2_login_response {
183 u32 misc;
184 struct sbp2_pointer command_block_agent;
185 u32 reconnect_hold;
186};
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400187#define COMMAND_ORB_DATA_SIZE(v) ((v))
188#define COMMAND_ORB_PAGE_SIZE(v) ((v) << 16)
189#define COMMAND_ORB_PAGE_TABLE_PRESENT ((1) << 19)
190#define COMMAND_ORB_MAX_PAYLOAD(v) ((v) << 20)
191#define COMMAND_ORB_SPEED(v) ((v) << 24)
192#define COMMAND_ORB_DIRECTION(v) ((v) << 27)
193#define COMMAND_ORB_REQUEST_FORMAT(v) ((v) << 29)
194#define COMMAND_ORB_NOTIFY ((1) << 31)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500195
196struct sbp2_command_orb {
197 struct sbp2_orb base;
198 struct {
199 struct sbp2_pointer next;
200 struct sbp2_pointer data_descriptor;
201 u32 misc;
202 u8 command_block[12];
203 } request;
204 struct scsi_cmnd *cmd;
205 scsi_done_fn_t done;
206 struct fw_unit *unit;
207
208 struct sbp2_pointer page_table[SG_ALL];
209 dma_addr_t page_table_bus;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500210};
211
212/*
213 * List of devices with known bugs.
214 *
215 * The firmware_revision field, masked with 0xffff00, is the best
216 * indicator for the type of bridge chip of a device. It yields a few
217 * false positives but this did not break correctly behaving devices
218 * so far. We use ~0 as a wildcard, since the 24 bit values we get
219 * from the config rom can never match that.
220 */
221static const struct {
222 u32 firmware_revision;
223 u32 model;
224 unsigned workarounds;
225} sbp2_workarounds_table[] = {
226 /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
227 .firmware_revision = 0x002800,
228 .model = 0x001010,
229 .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
230 SBP2_WORKAROUND_MODE_SENSE_8,
231 },
232 /* Initio bridges, actually only needed for some older ones */ {
233 .firmware_revision = 0x000200,
234 .model = ~0,
235 .workarounds = SBP2_WORKAROUND_INQUIRY_36,
236 },
237 /* Symbios bridge */ {
238 .firmware_revision = 0xa0b800,
239 .model = ~0,
240 .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
241 },
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400242
243 /*
244 * There are iPods (2nd gen, 3rd gen) with model_id == 0, but
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500245 * these iPods do not feature the read_capacity bug according
246 * to one report. Read_capacity behaviour as well as model_id
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400247 * could change due to Apple-supplied firmware updates though.
248 */
249
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500250 /* iPod 4th generation. */ {
251 .firmware_revision = 0x0a2700,
252 .model = 0x000021,
253 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
254 },
255 /* iPod mini */ {
256 .firmware_revision = 0x0a2700,
257 .model = 0x000023,
258 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
259 },
260 /* iPod Photo */ {
261 .firmware_revision = 0x0a2700,
262 .model = 0x00007e,
263 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
264 }
265};
266
267static void
268sbp2_status_write(struct fw_card *card, struct fw_request *request,
269 int tcode, int destination, int source,
270 int generation, int speed,
271 unsigned long long offset,
272 void *payload, size_t length, void *callback_data)
273{
274 struct sbp2_device *sd = callback_data;
275 struct sbp2_orb *orb;
276 struct sbp2_status status;
277 size_t header_size;
278 unsigned long flags;
279
280 if (tcode != TCODE_WRITE_BLOCK_REQUEST ||
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400281 length == 0 || length > sizeof(status)) {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500282 fw_send_response(card, request, RCODE_TYPE_ERROR);
283 return;
284 }
285
286 header_size = min(length, 2 * sizeof(u32));
287 fw_memcpy_from_be32(&status, payload, header_size);
288 if (length > header_size)
289 memcpy(status.data, payload + 8, length - header_size);
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400290 if (STATUS_GET_SOURCE(status) == 2 || STATUS_GET_SOURCE(status) == 3) {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500291 fw_notify("non-orb related status write, not handled\n");
292 fw_send_response(card, request, RCODE_COMPLETE);
293 return;
294 }
295
296 /* Lookup the orb corresponding to this status write. */
297 spin_lock_irqsave(&card->lock, flags);
298 list_for_each_entry(orb, &sd->orb_list, link) {
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400299 if (STATUS_GET_ORB_HIGH(status) == 0 &&
300 STATUS_GET_ORB_LOW(status) == orb->request_bus &&
Kristian Høgsberg12f26aa2007-04-10 18:11:20 -0400301 orb->rcode == RCODE_COMPLETE) {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500302 list_del(&orb->link);
303 break;
304 }
305 }
306 spin_unlock_irqrestore(&card->lock, flags);
307
308 if (&orb->link != &sd->orb_list)
309 orb->callback(orb, &status);
310 else
311 fw_error("status write for unknown orb\n");
312
313 fw_send_response(card, request, RCODE_COMPLETE);
314}
315
316static void
317complete_transaction(struct fw_card *card, int rcode,
318 void *payload, size_t length, void *data)
319{
320 struct sbp2_orb *orb = data;
321 unsigned long flags;
322
323 orb->rcode = rcode;
324 if (rcode != RCODE_COMPLETE) {
325 spin_lock_irqsave(&card->lock, flags);
326 list_del(&orb->link);
327 spin_unlock_irqrestore(&card->lock, flags);
328 orb->callback(orb, NULL);
329 }
330}
331
332static void
333sbp2_send_orb(struct sbp2_orb *orb, struct fw_unit *unit,
334 int node_id, int generation, u64 offset)
335{
336 struct fw_device *device = fw_device(unit->device.parent);
337 struct sbp2_device *sd = unit->device.driver_data;
338 unsigned long flags;
339
340 orb->pointer.high = 0;
341 orb->pointer.low = orb->request_bus;
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400342 fw_memcpy_to_be32(&orb->pointer, &orb->pointer, sizeof(orb->pointer));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500343
344 spin_lock_irqsave(&device->card->lock, flags);
345 list_add_tail(&orb->link, &sd->orb_list);
346 spin_unlock_irqrestore(&device->card->lock, flags);
347
348 fw_send_request(device->card, &orb->t, TCODE_WRITE_BLOCK_REQUEST,
Stefan Richterf1397492007-06-10 21:31:36 +0200349 node_id, generation, device->max_speed, offset,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400350 &orb->pointer, sizeof(orb->pointer),
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500351 complete_transaction, orb);
352}
353
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500354static int sbp2_cancel_orbs(struct fw_unit *unit)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500355{
356 struct fw_device *device = fw_device(unit->device.parent);
357 struct sbp2_device *sd = unit->device.driver_data;
358 struct sbp2_orb *orb, *next;
359 struct list_head list;
360 unsigned long flags;
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500361 int retval = -ENOENT;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500362
363 INIT_LIST_HEAD(&list);
364 spin_lock_irqsave(&device->card->lock, flags);
365 list_splice_init(&sd->orb_list, &list);
366 spin_unlock_irqrestore(&device->card->lock, flags);
367
368 list_for_each_entry_safe(orb, next, &list, link) {
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500369 retval = 0;
Kristian Høgsberg730c32f2007-02-06 14:49:32 -0500370 if (fw_cancel_transaction(device->card, &orb->t) == 0)
371 continue;
372
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500373 orb->rcode = RCODE_CANCELLED;
374 orb->callback(orb, NULL);
375 }
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500376
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500377 return retval;
Kristian Høgsberg1d3d52c2007-02-06 14:49:33 -0500378}
379
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500380static void
381complete_management_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
382{
383 struct sbp2_management_orb *orb =
384 (struct sbp2_management_orb *)base_orb;
385
386 if (status)
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400387 memcpy(&orb->status, status, sizeof(*status));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500388 complete(&orb->done);
389}
390
391static int
392sbp2_send_management_orb(struct fw_unit *unit, int node_id, int generation,
393 int function, int lun, void *response)
394{
395 struct fw_device *device = fw_device(unit->device.parent);
396 struct sbp2_device *sd = unit->device.driver_data;
397 struct sbp2_management_orb *orb;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500398 int retval = -ENOMEM;
399
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400400 orb = kzalloc(sizeof(*orb), GFP_ATOMIC);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500401 if (orb == NULL)
402 return -ENOMEM;
403
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400404 /*
405 * The sbp2 device is going to send a block read request to
406 * read out the request from host memory, so map it for dma.
407 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500408 orb->base.request_bus =
409 dma_map_single(device->card->device, &orb->request,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400410 sizeof(orb->request), DMA_TO_DEVICE);
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -0500411 if (dma_mapping_error(orb->base.request_bus))
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500412 goto out;
413
414 orb->response_bus =
415 dma_map_single(device->card->device, &orb->response,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400416 sizeof(orb->response), DMA_FROM_DEVICE);
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -0500417 if (dma_mapping_error(orb->response_bus))
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500418 goto out;
419
420 orb->request.response.high = 0;
421 orb->request.response.low = orb->response_bus;
422
423 orb->request.misc =
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400424 MANAGEMENT_ORB_NOTIFY |
425 MANAGEMENT_ORB_FUNCTION(function) |
426 MANAGEMENT_ORB_LUN(lun);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500427 orb->request.length =
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400428 MANAGEMENT_ORB_RESPONSE_LENGTH(sizeof(orb->response));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500429
430 orb->request.status_fifo.high = sd->address_handler.offset >> 32;
431 orb->request.status_fifo.low = sd->address_handler.offset;
432
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400433 /*
434 * FIXME: Yeah, ok this isn't elegant, we hardwire exclusive
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500435 * login and 1 second reconnect time. The reconnect setting
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400436 * is probably fine, but the exclusive login should be an option.
437 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500438 if (function == SBP2_LOGIN_REQUEST) {
439 orb->request.misc |=
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400440 MANAGEMENT_ORB_EXCLUSIVE |
441 MANAGEMENT_ORB_RECONNECT(0);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500442 }
443
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400444 fw_memcpy_to_be32(&orb->request, &orb->request, sizeof(orb->request));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500445
446 init_completion(&orb->done);
447 orb->base.callback = complete_management_orb;
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500448
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500449 sbp2_send_orb(&orb->base, unit,
450 node_id, generation, sd->management_agent_address);
451
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500452 wait_for_completion_timeout(&orb->done,
453 msecs_to_jiffies(SBP2_ORB_TIMEOUT));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500454
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500455 retval = -EIO;
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500456 if (sbp2_cancel_orbs(unit) == 0) {
457 fw_error("orb reply timed out, rcode=0x%02x\n",
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500458 orb->base.rcode);
459 goto out;
460 }
461
Kristian Høgsberg2aaad972007-03-07 12:12:47 -0500462 if (orb->base.rcode != RCODE_COMPLETE) {
463 fw_error("management write failed, rcode 0x%02x\n",
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500464 orb->base.rcode);
465 goto out;
466 }
467
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400468 if (STATUS_GET_RESPONSE(orb->status) != 0 ||
469 STATUS_GET_SBP_STATUS(orb->status) != 0) {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500470 fw_error("error status: %d:%d\n",
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400471 STATUS_GET_RESPONSE(orb->status),
472 STATUS_GET_SBP_STATUS(orb->status));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500473 goto out;
474 }
475
476 retval = 0;
477 out:
478 dma_unmap_single(device->card->device, orb->base.request_bus,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400479 sizeof(orb->request), DMA_TO_DEVICE);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500480 dma_unmap_single(device->card->device, orb->response_bus,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400481 sizeof(orb->response), DMA_FROM_DEVICE);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500482
483 if (response)
484 fw_memcpy_from_be32(response,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400485 orb->response, sizeof(orb->response));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500486 kfree(orb);
487
488 return retval;
489}
490
491static void
492complete_agent_reset_write(struct fw_card *card, int rcode,
493 void *payload, size_t length, void *data)
494{
495 struct fw_transaction *t = data;
496
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500497 kfree(t);
498}
499
500static int sbp2_agent_reset(struct fw_unit *unit)
501{
502 struct fw_device *device = fw_device(unit->device.parent);
503 struct sbp2_device *sd = unit->device.driver_data;
504 struct fw_transaction *t;
505 static u32 zero;
506
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400507 t = kzalloc(sizeof(*t), GFP_ATOMIC);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500508 if (t == NULL)
509 return -ENOMEM;
510
511 fw_send_request(device->card, t, TCODE_WRITE_QUADLET_REQUEST,
Stefan Richter907293d2007-01-23 21:11:43 +0100512 sd->node_id, sd->generation, SCODE_400,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500513 sd->command_block_agent_address + SBP2_AGENT_RESET,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400514 &zero, sizeof(zero), complete_agent_reset_write, t);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500515
516 return 0;
517}
518
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500519static void sbp2_reconnect(struct work_struct *work);
Kristian Høgsbergad852742007-05-09 19:23:07 -0400520static struct scsi_host_template scsi_driver_template;
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500521
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400522static void
523release_sbp2_device(struct kref *kref)
524{
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
529 sbp2_send_management_orb(sd->unit, sd->node_id, sd->generation,
530 SBP2_LOGOUT_REQUEST, sd->login_id, NULL);
531
Kristian Høgsbergad852742007-05-09 19:23:07 -0400532 scsi_remove_host(host);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400533 fw_core_remove_address_handler(&sd->address_handler);
534 fw_notify("removed sbp2 unit %s\n", sd->unit->device.bus_id);
535 put_device(&sd->unit->device);
Kristian Høgsbergad852742007-05-09 19:23:07 -0400536 scsi_host_put(host);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400537}
538
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500539static void sbp2_login(struct work_struct *work)
540{
541 struct sbp2_device *sd =
542 container_of(work, struct sbp2_device, work.work);
Kristian Høgsbergad852742007-05-09 19:23:07 -0400543 struct Scsi_Host *host =
544 container_of((void *)sd, struct Scsi_Host, hostdata[0]);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500545 struct fw_unit *unit = sd->unit;
546 struct fw_device *device = fw_device(unit->device.parent);
547 struct sbp2_login_response response;
548 int generation, node_id, local_node_id, lun, retval;
549
550 /* FIXME: Make this work for multi-lun devices. */
551 lun = 0;
552
553 generation = device->card->generation;
554 node_id = device->node->node_id;
555 local_node_id = device->card->local_node->node_id;
556
557 if (sbp2_send_management_orb(unit, node_id, generation,
558 SBP2_LOGIN_REQUEST, lun, &response) < 0) {
559 if (sd->retries++ < 5) {
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500560 schedule_delayed_work(&sd->work, DIV_ROUND_UP(HZ, 5));
561 } else {
562 fw_error("failed to login to %s\n",
563 unit->device.bus_id);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400564 kref_put(&sd->kref, release_sbp2_device);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500565 }
566 return;
567 }
568
569 sd->generation = generation;
570 sd->node_id = node_id;
571 sd->address_high = local_node_id << 16;
572
573 /* Get command block agent offset and login id. */
574 sd->command_block_agent_address =
Kristian Høgsberg5c5539d2007-03-07 12:12:45 -0500575 ((u64) (response.command_block_agent.high & 0xffff) << 32) |
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500576 response.command_block_agent.low;
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400577 sd->login_id = LOGIN_RESPONSE_GET_LOGIN_ID(response);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500578
Kristian Høgsberg5c5539d2007-03-07 12:12:45 -0500579 fw_notify("logged in to sbp2 unit %s (%d retries)\n",
580 unit->device.bus_id, sd->retries);
581 fw_notify(" - management_agent_address: 0x%012llx\n",
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500582 (unsigned long long) sd->management_agent_address);
583 fw_notify(" - command_block_agent_address: 0x%012llx\n",
584 (unsigned long long) sd->command_block_agent_address);
Kristian Høgsberg5c5539d2007-03-07 12:12:45 -0500585 fw_notify(" - status write address: 0x%012llx\n",
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500586 (unsigned long long) sd->address_handler.offset);
587
588#if 0
589 /* FIXME: The linux1394 sbp2 does this last step. */
590 sbp2_set_busy_timeout(scsi_id);
591#endif
592
Kristian Høgsberg1da0c932007-03-07 12:12:40 -0500593 PREPARE_DELAYED_WORK(&sd->work, sbp2_reconnect);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500594 sbp2_agent_reset(unit);
595
Kristian Høgsbergad852742007-05-09 19:23:07 -0400596 /* FIXME: Loop over luns here. */
597 lun = 0;
598 retval = scsi_add_device(host, 0, 0, lun);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500599 if (retval < 0) {
600 sbp2_send_management_orb(unit, sd->node_id, sd->generation,
601 SBP2_LOGOUT_REQUEST, sd->login_id,
602 NULL);
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400603 /*
604 * Set this back to sbp2_login so we fall back and
605 * retry login on bus reset.
606 */
Kristian Høgsberg1da0c932007-03-07 12:12:40 -0500607 PREPARE_DELAYED_WORK(&sd->work, sbp2_login);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500608 }
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400609 kref_put(&sd->kref, release_sbp2_device);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500610}
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500611
612static int sbp2_probe(struct device *dev)
613{
614 struct fw_unit *unit = fw_unit(dev);
615 struct fw_device *device = fw_device(unit->device.parent);
616 struct sbp2_device *sd;
617 struct fw_csr_iterator ci;
Kristian Høgsbergad852742007-05-09 19:23:07 -0400618 struct Scsi_Host *host;
619 int i, key, value, err;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500620 u32 model, firmware_revision;
621
Kristian Høgsbergad852742007-05-09 19:23:07 -0400622 err = -ENOMEM;
623 host = scsi_host_alloc(&scsi_driver_template, sizeof(*sd));
624 if (host == NULL)
625 goto fail;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500626
Kristian Høgsbergad852742007-05-09 19:23:07 -0400627 sd = (struct sbp2_device *) host->hostdata;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500628 unit->device.driver_data = sd;
629 sd->unit = unit;
630 INIT_LIST_HEAD(&sd->orb_list);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400631 kref_init(&sd->kref);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500632
633 sd->address_handler.length = 0x100;
634 sd->address_handler.address_callback = sbp2_status_write;
635 sd->address_handler.callback_data = sd;
636
Kristian Høgsbergad852742007-05-09 19:23:07 -0400637 err = fw_core_add_address_handler(&sd->address_handler,
638 &fw_high_memory_region);
639 if (err < 0)
640 goto fail_host;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500641
Kristian Høgsbergad852742007-05-09 19:23:07 -0400642 err = fw_device_enable_phys_dma(device);
643 if (err < 0)
644 goto fail_address_handler;
645
646 err = scsi_add_host(host, &unit->device);
647 if (err < 0)
648 goto fail_address_handler;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500649
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400650 /*
651 * Scan unit directory to get management agent address,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500652 * firmware revison and model. Initialize firmware_revision
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400653 * and model to values that wont match anything in our table.
654 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500655 firmware_revision = 0xff000000;
656 model = 0xff000000;
657 fw_csr_iterator_init(&ci, unit->directory);
658 while (fw_csr_iterator_next(&ci, &key, &value)) {
659 switch (key) {
660 case CSR_DEPENDENT_INFO | CSR_OFFSET:
661 sd->management_agent_address =
662 0xfffff0000000ULL + 4 * value;
663 break;
664 case SBP2_FIRMWARE_REVISION:
665 firmware_revision = value;
666 break;
667 case CSR_MODEL:
668 model = value;
669 break;
670 }
671 }
672
673 for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
674 if (sbp2_workarounds_table[i].firmware_revision !=
675 (firmware_revision & 0xffffff00))
676 continue;
677 if (sbp2_workarounds_table[i].model != model &&
678 sbp2_workarounds_table[i].model != ~0)
679 continue;
680 sd->workarounds |= sbp2_workarounds_table[i].workarounds;
681 break;
682 }
683
684 if (sd->workarounds)
685 fw_notify("Workarounds for node %s: 0x%x "
686 "(firmware_revision 0x%06x, model_id 0x%06x)\n",
687 unit->device.bus_id,
688 sd->workarounds, firmware_revision, model);
689
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400690 get_device(&unit->device);
691
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400692 /*
693 * We schedule work to do the login so we can easily
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400694 * reschedule retries. Always get the ref before scheduling
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400695 * work.
696 */
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500697 INIT_DELAYED_WORK(&sd->work, sbp2_login);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400698 if (schedule_delayed_work(&sd->work, 0))
699 kref_get(&sd->kref);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500700
701 return 0;
Kristian Høgsbergad852742007-05-09 19:23:07 -0400702
703 fail_address_handler:
704 fw_core_remove_address_handler(&sd->address_handler);
705 fail_host:
706 scsi_host_put(host);
707 fail:
708 return err;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500709}
710
711static int sbp2_remove(struct device *dev)
712{
713 struct fw_unit *unit = fw_unit(dev);
714 struct sbp2_device *sd = unit->device.driver_data;
715
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400716 kref_put(&sd->kref, release_sbp2_device);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500717
718 return 0;
719}
720
721static void sbp2_reconnect(struct work_struct *work)
722{
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500723 struct sbp2_device *sd =
724 container_of(work, struct sbp2_device, work.work);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500725 struct fw_unit *unit = sd->unit;
726 struct fw_device *device = fw_device(unit->device.parent);
727 int generation, node_id, local_node_id;
728
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500729 generation = device->card->generation;
730 node_id = device->node->node_id;
731 local_node_id = device->card->local_node->node_id;
732
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500733 if (sbp2_send_management_orb(unit, node_id, generation,
734 SBP2_RECONNECT_REQUEST,
735 sd->login_id, NULL) < 0) {
Kristian Høgsberg5c5539d2007-03-07 12:12:45 -0500736 if (sd->retries++ >= 5) {
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500737 fw_error("failed to reconnect to %s\n",
738 unit->device.bus_id);
739 /* Fall back and try to log in again. */
740 sd->retries = 0;
Kristian Høgsberg1da0c932007-03-07 12:12:40 -0500741 PREPARE_DELAYED_WORK(&sd->work, sbp2_login);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500742 }
743 schedule_delayed_work(&sd->work, DIV_ROUND_UP(HZ, 5));
744 return;
745 }
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500746
747 sd->generation = generation;
748 sd->node_id = node_id;
Stefan Richter907293d2007-01-23 21:11:43 +0100749 sd->address_high = local_node_id << 16;
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500750
Kristian Høgsberg5c5539d2007-03-07 12:12:45 -0500751 fw_notify("reconnected to unit %s (%d retries)\n",
752 unit->device.bus_id, sd->retries);
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500753 sbp2_agent_reset(unit);
754 sbp2_cancel_orbs(unit);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400755 kref_put(&sd->kref, release_sbp2_device);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500756}
757
758static void sbp2_update(struct fw_unit *unit)
759{
760 struct fw_device *device = fw_device(unit->device.parent);
761 struct sbp2_device *sd = unit->device.driver_data;
762
Kristian Høgsberg7f37c422007-02-06 14:49:34 -0500763 sd->retries = 0;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500764 fw_device_enable_phys_dma(device);
Kristian Høgsbergb3d6e152007-03-14 17:34:58 -0400765 if (schedule_delayed_work(&sd->work, 0))
766 kref_get(&sd->kref);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500767}
768
769#define SBP2_UNIT_SPEC_ID_ENTRY 0x0000609e
770#define SBP2_SW_VERSION_ENTRY 0x00010483
771
Stefan Richter21ebcd12007-01-14 15:29:07 +0100772static const struct fw_device_id sbp2_id_table[] = {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500773 {
774 .match_flags = FW_MATCH_SPECIFIER_ID | FW_MATCH_VERSION,
775 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY,
Stefan Richter5af4e5e2007-01-21 20:45:32 +0100776 .version = SBP2_SW_VERSION_ENTRY,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500777 },
778 { }
779};
780
781static struct fw_driver sbp2_driver = {
782 .driver = {
783 .owner = THIS_MODULE,
784 .name = sbp2_driver_name,
785 .bus = &fw_bus_type,
786 .probe = sbp2_probe,
787 .remove = sbp2_remove,
788 },
789 .update = sbp2_update,
790 .id_table = sbp2_id_table,
791};
792
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400793static unsigned int
794sbp2_status_to_sense_data(u8 *sbp2_status, u8 *sense_data)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500795{
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400796 int sam_status;
797
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500798 sense_data[0] = 0x70;
799 sense_data[1] = 0x0;
800 sense_data[2] = sbp2_status[1];
801 sense_data[3] = sbp2_status[4];
802 sense_data[4] = sbp2_status[5];
803 sense_data[5] = sbp2_status[6];
804 sense_data[6] = sbp2_status[7];
805 sense_data[7] = 10;
806 sense_data[8] = sbp2_status[8];
807 sense_data[9] = sbp2_status[9];
808 sense_data[10] = sbp2_status[10];
809 sense_data[11] = sbp2_status[11];
810 sense_data[12] = sbp2_status[2];
811 sense_data[13] = sbp2_status[3];
812 sense_data[14] = sbp2_status[12];
813 sense_data[15] = sbp2_status[13];
814
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400815 sam_status = sbp2_status[0] & 0x3f;
816
817 switch (sam_status) {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500818 case SAM_STAT_GOOD:
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500819 case SAM_STAT_CHECK_CONDITION:
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500820 case SAM_STAT_CONDITION_MET:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400821 case SAM_STAT_BUSY:
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500822 case SAM_STAT_RESERVATION_CONFLICT:
823 case SAM_STAT_COMMAND_TERMINATED:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400824 return DID_OK << 16 | sam_status;
825
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500826 default:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400827 return DID_ERROR << 16;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500828 }
829}
830
831static void
832complete_command_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
833{
834 struct sbp2_command_orb *orb = (struct sbp2_command_orb *)base_orb;
835 struct fw_unit *unit = orb->unit;
836 struct fw_device *device = fw_device(unit->device.parent);
837 struct scatterlist *sg;
838 int result;
839
840 if (status != NULL) {
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400841 if (STATUS_GET_DEAD(*status))
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500842 sbp2_agent_reset(unit);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500843
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400844 switch (STATUS_GET_RESPONSE(*status)) {
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500845 case SBP2_STATUS_REQUEST_COMPLETE:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400846 result = DID_OK << 16;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500847 break;
848 case SBP2_STATUS_TRANSPORT_FAILURE:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400849 result = DID_BUS_BUSY << 16;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500850 break;
851 case SBP2_STATUS_ILLEGAL_REQUEST:
852 case SBP2_STATUS_VENDOR_DEPENDENT:
853 default:
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400854 result = DID_ERROR << 16;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500855 break;
856 }
857
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400858 if (result == DID_OK << 16 && STATUS_GET_LEN(*status) > 1)
859 result = sbp2_status_to_sense_data(STATUS_GET_DATA(*status),
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500860 orb->cmd->sense_buffer);
861 } else {
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400862 /*
863 * If the orb completes with status == NULL, something
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500864 * went wrong, typically a bus reset happened mid-orb
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400865 * or when sending the write (less likely).
866 */
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400867 result = DID_BUS_BUSY << 16;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500868 }
869
870 dma_unmap_single(device->card->device, orb->base.request_bus,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400871 sizeof(orb->request), DMA_TO_DEVICE);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500872
873 if (orb->cmd->use_sg > 0) {
874 sg = (struct scatterlist *)orb->cmd->request_buffer;
875 dma_unmap_sg(device->card->device, sg, orb->cmd->use_sg,
876 orb->cmd->sc_data_direction);
877 }
878
879 if (orb->page_table_bus != 0)
880 dma_unmap_single(device->card->device, orb->page_table_bus,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400881 sizeof(orb->page_table_bus), DMA_TO_DEVICE);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500882
Kristian Høgsbergfbb54232007-04-10 18:11:18 -0400883 orb->cmd->result = result;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500884 orb->done(orb->cmd);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500885 kfree(orb);
886}
887
Kristian Høgsberg95ffc5e2007-05-09 19:23:08 -0400888static int sbp2_command_orb_map_scatterlist(struct sbp2_command_orb *orb)
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500889{
Kristian Høgsbergad852742007-05-09 19:23:07 -0400890 struct sbp2_device *sd =
891 (struct sbp2_device *)orb->cmd->device->host->hostdata;
892 struct fw_unit *unit = sd->unit;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500893 struct fw_device *device = fw_device(unit->device.parent);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500894 struct scatterlist *sg;
895 int sg_len, l, i, j, count;
896 size_t size;
897 dma_addr_t sg_addr;
898
899 sg = (struct scatterlist *)orb->cmd->request_buffer;
900 count = dma_map_sg(device->card->device, sg, orb->cmd->use_sg,
901 orb->cmd->sc_data_direction);
Kristian Høgsberg95ffc5e2007-05-09 19:23:08 -0400902 if (count == 0)
903 goto fail;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500904
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400905 /*
906 * Handle the special case where there is only one element in
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500907 * the scatter list by converting it to an immediate block
908 * request. This is also a workaround for broken devices such
909 * as the second generation iPod which doesn't support page
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400910 * tables.
911 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500912 if (count == 1 && sg_dma_len(sg) < SBP2_MAX_SG_ELEMENT_LENGTH) {
913 orb->request.data_descriptor.high = sd->address_high;
914 orb->request.data_descriptor.low = sg_dma_address(sg);
915 orb->request.misc |=
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400916 COMMAND_ORB_DATA_SIZE(sg_dma_len(sg));
Kristian Høgsberg95ffc5e2007-05-09 19:23:08 -0400917 return 0;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500918 }
919
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400920 /*
921 * Convert the scatterlist to an sbp2 page table. If any
Kristian Høgsberg, Stefan Richter36abb3b2007-05-09 19:23:10 -0400922 * scatterlist entries are too big for sbp2, we split them as we
923 * go. Even if we ask the block I/O layer to not give us sg
924 * elements larger than 65535 bytes, some IOMMUs may merge sg elements
925 * during DMA mapping, and Linux currently doesn't prevent this.
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400926 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500927 for (i = 0, j = 0; i < count; i++) {
928 sg_len = sg_dma_len(sg + i);
929 sg_addr = sg_dma_address(sg + i);
930 while (sg_len) {
931 l = min(sg_len, SBP2_MAX_SG_ELEMENT_LENGTH);
932 orb->page_table[j].low = sg_addr;
933 orb->page_table[j].high = (l << 16);
934 sg_addr += l;
935 sg_len -= l;
936 j++;
937 }
938 }
939
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400940 size = sizeof(orb->page_table[0]) * j;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500941
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400942 /*
943 * The data_descriptor pointer is the one case where we need
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500944 * to fill in the node ID part of the address. All other
945 * pointers assume that the data referenced reside on the
946 * initiator (i.e. us), but data_descriptor can refer to data
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400947 * on other nodes so we need to put our ID in descriptor.high.
948 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500949
950 orb->page_table_bus =
951 dma_map_single(device->card->device, orb->page_table,
952 size, DMA_TO_DEVICE);
Kristian Høgsberg95ffc5e2007-05-09 19:23:08 -0400953 if (dma_mapping_error(orb->page_table_bus))
954 goto fail_page_table;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500955 orb->request.data_descriptor.high = sd->address_high;
956 orb->request.data_descriptor.low = orb->page_table_bus;
957 orb->request.misc |=
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400958 COMMAND_ORB_PAGE_TABLE_PRESENT |
959 COMMAND_ORB_DATA_SIZE(j);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500960
961 fw_memcpy_to_be32(orb->page_table, orb->page_table, size);
Kristian Høgsberg95ffc5e2007-05-09 19:23:08 -0400962
963 return 0;
964
965 fail_page_table:
966 dma_unmap_sg(device->card->device, sg, orb->cmd->use_sg,
967 orb->cmd->sc_data_direction);
968 fail:
969 return -ENOMEM;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500970}
971
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500972/* SCSI stack integration */
973
974static int sbp2_scsi_queuecommand(struct scsi_cmnd *cmd, scsi_done_fn_t done)
975{
Kristian Høgsbergad852742007-05-09 19:23:07 -0400976 struct sbp2_device *sd =
977 (struct sbp2_device *)cmd->device->host->hostdata;
978 struct fw_unit *unit = sd->unit;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500979 struct fw_device *device = fw_device(unit->device.parent);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500980 struct sbp2_command_orb *orb;
981
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400982 /*
983 * Bidirectional commands are not yet implemented, and unknown
984 * transfer direction not handled.
985 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500986 if (cmd->sc_data_direction == DMA_BIDIRECTIONAL) {
Stefan Richter8a8cea22007-06-09 19:26:22 +0200987 fw_error("Can't handle DMA_BIDIRECTIONAL, rejecting command\n");
Kristian Høgsberge1b68c42007-05-09 19:23:09 -0400988 cmd->result = DID_ERROR << 16;
989 done(cmd);
990 return 0;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500991 }
992
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400993 orb = kzalloc(sizeof(*orb), GFP_ATOMIC);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500994 if (orb == NULL) {
995 fw_notify("failed to alloc orb\n");
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -0500996 goto fail_alloc;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -0500997 }
998
Kristian Høgsberg12f26aa2007-04-10 18:11:20 -0400999 /* Initialize rcode to something not RCODE_COMPLETE. */
1000 orb->base.rcode = -1;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001001 orb->base.request_bus =
1002 dma_map_single(device->card->device, &orb->request,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001003 sizeof(orb->request), DMA_TO_DEVICE);
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -05001004 if (dma_mapping_error(orb->base.request_bus))
1005 goto fail_mapping;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001006
1007 orb->unit = unit;
1008 orb->done = done;
1009 orb->cmd = cmd;
1010
1011 orb->request.next.high = SBP2_ORB_NULL;
1012 orb->request.next.low = 0x0;
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001013 /*
1014 * At speed 100 we can do 512 bytes per packet, at speed 200,
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001015 * 1024 bytes per packet etc. The SBP-2 max_payload field
1016 * specifies the max payload size as 2 ^ (max_payload + 2), so
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001017 * if we set this to max_speed + 7, we get the right value.
1018 */
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001019 orb->request.misc =
Stefan Richterf1397492007-06-10 21:31:36 +02001020 COMMAND_ORB_MAX_PAYLOAD(device->max_speed + 7) |
1021 COMMAND_ORB_SPEED(device->max_speed) |
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001022 COMMAND_ORB_NOTIFY;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001023
1024 if (cmd->sc_data_direction == DMA_FROM_DEVICE)
1025 orb->request.misc |=
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001026 COMMAND_ORB_DIRECTION(SBP2_DIRECTION_FROM_MEDIA);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001027 else if (cmd->sc_data_direction == DMA_TO_DEVICE)
1028 orb->request.misc |=
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001029 COMMAND_ORB_DIRECTION(SBP2_DIRECTION_TO_MEDIA);
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001030
Kristian Høgsberg213d7bb2007-05-09 19:23:11 -04001031 if (cmd->use_sg && sbp2_command_orb_map_scatterlist(orb) < 0)
Kristian Høgsberg95ffc5e2007-05-09 19:23:08 -04001032 goto fail_map_payload;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001033
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001034 fw_memcpy_to_be32(&orb->request, &orb->request, sizeof(orb->request));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001035
1036 memset(orb->request.command_block,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001037 0, sizeof(orb->request.command_block));
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001038 memcpy(orb->request.command_block, cmd->cmnd, COMMAND_SIZE(*cmd->cmnd));
1039
1040 orb->base.callback = complete_command_orb;
1041
1042 sbp2_send_orb(&orb->base, unit, sd->node_id, sd->generation,
1043 sd->command_block_agent_address + SBP2_ORB_POINTER);
1044
1045 return 0;
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -05001046
Kristian Høgsberg95ffc5e2007-05-09 19:23:08 -04001047 fail_map_payload:
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -05001048 dma_unmap_single(device->card->device, orb->base.request_bus,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001049 sizeof(orb->request), DMA_TO_DEVICE);
Kristian Høgsberg82eff9d2007-02-06 14:49:40 -05001050 fail_mapping:
1051 kfree(orb);
1052 fail_alloc:
Kristian Høgsberge1b68c42007-05-09 19:23:09 -04001053 return SCSI_MLQUEUE_HOST_BUSY;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001054}
1055
Stefan Richtercfb01382007-01-23 21:20:08 +01001056static int sbp2_scsi_slave_alloc(struct scsi_device *sdev)
1057{
Kristian Høgsbergad852742007-05-09 19:23:07 -04001058 struct sbp2_device *sd = (struct sbp2_device *)sdev->host->hostdata;
Stefan Richtercfb01382007-01-23 21:20:08 +01001059
1060 sdev->allow_restart = 1;
1061
1062 if (sd->workarounds & SBP2_WORKAROUND_INQUIRY_36)
1063 sdev->inquiry_len = 36;
1064 return 0;
1065}
1066
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001067static int sbp2_scsi_slave_configure(struct scsi_device *sdev)
1068{
Kristian Høgsbergad852742007-05-09 19:23:07 -04001069 struct sbp2_device *sd = (struct sbp2_device *)sdev->host->hostdata;
1070 struct fw_unit *unit = sd->unit;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001071
Stefan Richtercfb01382007-01-23 21:20:08 +01001072 sdev->use_10_for_rw = 1;
1073
1074 if (sdev->type == TYPE_ROM)
1075 sdev->use_10_for_ms = 1;
Kristian Høgsberg9ba136d2006-12-19 19:58:40 -05001076 if (sdev->type == TYPE_DISK &&
1077 sd->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
1078 sdev->skip_ms_page_8 = 1;
1079 if (sd->workarounds & SBP2_WORKAROUND_FIX_CAPACITY) {
1080 fw_notify("setting fix_capacity for %s\n", unit->device.bus_id);
1081 sdev->fix_capacity = 1;
1082 }
1083
1084 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);