]>
Commit | Line | Data |
---|---|---|
c781c06d KH |
1 | /* |
2 | * SBP2 driver (SCSI over IEEE1394) | |
9ba136d0 | 3 | * |
27a15e50 | 4 | * Copyright (C) 2005-2007 Kristian Hoegsberg <krh@bitplanet.net> |
9ba136d0 KH |
5 | * |
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 | ||
c781c06d KH |
21 | /* |
22 | * The basic structure of this driver is based on the old storage driver, | |
27a15e50 KH |
23 | * 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 | ||
9ba136d0 KH |
31 | #include <linux/kernel.h> |
32 | #include <linux/module.h> | |
5cd54c94 | 33 | #include <linux/moduleparam.h> |
fe69ca3a | 34 | #include <linux/mod_devicetable.h> |
9ba136d0 | 35 | #include <linux/device.h> |
0b5b2903 | 36 | #include <linux/scatterlist.h> |
9ba136d0 | 37 | #include <linux/dma-mapping.h> |
cf47c7a2 | 38 | #include <linux/blkdev.h> |
e7cdf237 | 39 | #include <linux/string.h> |
1d3d52c5 | 40 | #include <linux/timer.h> |
9ba136d0 KH |
41 | |
42 | #include <scsi/scsi.h> | |
43 | #include <scsi/scsi_cmnd.h> | |
44 | #include <scsi/scsi_dbg.h> | |
45 | #include <scsi/scsi_device.h> | |
46 | #include <scsi/scsi_host.h> | |
47 | ||
48 | #include "fw-transaction.h" | |
49 | #include "fw-topology.h" | |
50 | #include "fw-device.h" | |
51 | ||
5cd54c94 SR |
52 | /* |
53 | * So far only bridges from Oxford Semiconductor are known to support | |
54 | * concurrent logins. Depending on firmware, four or two concurrent logins | |
55 | * are possible on OXFW911 and newer Oxsemi bridges. | |
56 | * | |
57 | * Concurrent logins are useful together with cluster filesystems. | |
58 | */ | |
59 | static int sbp2_param_exclusive_login = 1; | |
60 | module_param_named(exclusive_login, sbp2_param_exclusive_login, bool, 0644); | |
61 | MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device " | |
62 | "(default = Y, use N for concurrent initiators)"); | |
63 | ||
9ba136d0 | 64 | /* I don't know why the SCSI stack doesn't define something like this... */ |
a98e2719 | 65 | typedef void (*scsi_done_fn_t)(struct scsi_cmnd *); |
9ba136d0 KH |
66 | |
67 | static const char sbp2_driver_name[] = "sbp2"; | |
68 | ||
69 | struct sbp2_device { | |
b3d6e151 | 70 | struct kref kref; |
9ba136d0 KH |
71 | struct fw_unit *unit; |
72 | struct fw_address_handler address_handler; | |
73 | struct list_head orb_list; | |
74 | u64 management_agent_address; | |
75 | u64 command_block_agent_address; | |
76 | u32 workarounds; | |
77 | int login_id; | |
78 | ||
c781c06d KH |
79 | /* |
80 | * We cache these addresses and only update them once we've | |
9ba136d0 KH |
81 | * logged in or reconnected to the sbp2 device. That way, any |
82 | * IO to the device will automatically fail and get retried if | |
83 | * it happens in a window where the device is not ready to | |
c781c06d KH |
84 | * handle it (e.g. after a bus reset but before we reconnect). |
85 | */ | |
9ba136d0 KH |
86 | int node_id; |
87 | int address_high; | |
88 | int generation; | |
89 | ||
7f37c426 KH |
90 | int retries; |
91 | struct delayed_work work; | |
9ba136d0 KH |
92 | }; |
93 | ||
94 | #define SBP2_MAX_SG_ELEMENT_LENGTH 0xf000 | |
95 | #define SBP2_MAX_SECTORS 255 /* Max sectors supported */ | |
1d3d52c5 | 96 | #define SBP2_ORB_TIMEOUT 2000 /* Timeout in ms */ |
9ba136d0 KH |
97 | |
98 | #define SBP2_ORB_NULL 0x80000000 | |
99 | ||
100 | #define SBP2_DIRECTION_TO_MEDIA 0x0 | |
101 | #define SBP2_DIRECTION_FROM_MEDIA 0x1 | |
102 | ||
103 | /* Unit directory keys */ | |
104 | #define SBP2_COMMAND_SET_SPECIFIER 0x38 | |
105 | #define SBP2_COMMAND_SET 0x39 | |
106 | #define SBP2_COMMAND_SET_REVISION 0x3b | |
107 | #define SBP2_FIRMWARE_REVISION 0x3c | |
108 | ||
109 | /* Flags for detected oddities and brokeness */ | |
110 | #define SBP2_WORKAROUND_128K_MAX_TRANS 0x1 | |
111 | #define SBP2_WORKAROUND_INQUIRY_36 0x2 | |
112 | #define SBP2_WORKAROUND_MODE_SENSE_8 0x4 | |
113 | #define SBP2_WORKAROUND_FIX_CAPACITY 0x8 | |
114 | #define SBP2_WORKAROUND_OVERRIDE 0x100 | |
115 | ||
116 | /* Management orb opcodes */ | |
117 | #define SBP2_LOGIN_REQUEST 0x0 | |
118 | #define SBP2_QUERY_LOGINS_REQUEST 0x1 | |
119 | #define SBP2_RECONNECT_REQUEST 0x3 | |
120 | #define SBP2_SET_PASSWORD_REQUEST 0x4 | |
121 | #define SBP2_LOGOUT_REQUEST 0x7 | |
122 | #define SBP2_ABORT_TASK_REQUEST 0xb | |
123 | #define SBP2_ABORT_TASK_SET 0xc | |
124 | #define SBP2_LOGICAL_UNIT_RESET 0xe | |
125 | #define SBP2_TARGET_RESET_REQUEST 0xf | |
126 | ||
127 | /* Offsets for command block agent registers */ | |
128 | #define SBP2_AGENT_STATE 0x00 | |
129 | #define SBP2_AGENT_RESET 0x04 | |
130 | #define SBP2_ORB_POINTER 0x08 | |
131 | #define SBP2_DOORBELL 0x10 | |
132 | #define SBP2_UNSOLICITED_STATUS_ENABLE 0x14 | |
133 | ||
134 | /* Status write response codes */ | |
135 | #define SBP2_STATUS_REQUEST_COMPLETE 0x0 | |
136 | #define SBP2_STATUS_TRANSPORT_FAILURE 0x1 | |
137 | #define SBP2_STATUS_ILLEGAL_REQUEST 0x2 | |
138 | #define SBP2_STATUS_VENDOR_DEPENDENT 0x3 | |
139 | ||
a77754a7 KH |
140 | #define STATUS_GET_ORB_HIGH(v) ((v).status & 0xffff) |
141 | #define STATUS_GET_SBP_STATUS(v) (((v).status >> 16) & 0xff) | |
142 | #define STATUS_GET_LEN(v) (((v).status >> 24) & 0x07) | |
143 | #define STATUS_GET_DEAD(v) (((v).status >> 27) & 0x01) | |
144 | #define STATUS_GET_RESPONSE(v) (((v).status >> 28) & 0x03) | |
145 | #define STATUS_GET_SOURCE(v) (((v).status >> 30) & 0x03) | |
146 | #define STATUS_GET_ORB_LOW(v) ((v).orb_low) | |
147 | #define STATUS_GET_DATA(v) ((v).data) | |
9ba136d0 KH |
148 | |
149 | struct sbp2_status { | |
150 | u32 status; | |
151 | u32 orb_low; | |
152 | u8 data[24]; | |
153 | }; | |
154 | ||
155 | struct sbp2_pointer { | |
156 | u32 high; | |
157 | u32 low; | |
158 | }; | |
159 | ||
160 | struct sbp2_orb { | |
161 | struct fw_transaction t; | |
162 | dma_addr_t request_bus; | |
163 | int rcode; | |
164 | struct sbp2_pointer pointer; | |
a98e2719 | 165 | void (*callback)(struct sbp2_orb * orb, struct sbp2_status * status); |
9ba136d0 KH |
166 | struct list_head link; |
167 | }; | |
168 | ||
a77754a7 KH |
169 | #define MANAGEMENT_ORB_LUN(v) ((v)) |
170 | #define MANAGEMENT_ORB_FUNCTION(v) ((v) << 16) | |
171 | #define MANAGEMENT_ORB_RECONNECT(v) ((v) << 20) | |
5cd54c94 | 172 | #define MANAGEMENT_ORB_EXCLUSIVE(v) ((v) ? 1 << 28 : 0) |
a77754a7 KH |
173 | #define MANAGEMENT_ORB_REQUEST_FORMAT(v) ((v) << 29) |
174 | #define MANAGEMENT_ORB_NOTIFY ((1) << 31) | |
9ba136d0 | 175 | |
a77754a7 KH |
176 | #define MANAGEMENT_ORB_RESPONSE_LENGTH(v) ((v)) |
177 | #define MANAGEMENT_ORB_PASSWORD_LENGTH(v) ((v) << 16) | |
9ba136d0 KH |
178 | |
179 | struct sbp2_management_orb { | |
180 | struct sbp2_orb base; | |
181 | struct { | |
182 | struct sbp2_pointer password; | |
183 | struct sbp2_pointer response; | |
184 | u32 misc; | |
185 | u32 length; | |
186 | struct sbp2_pointer status_fifo; | |
187 | } request; | |
188 | __be32 response[4]; | |
189 | dma_addr_t response_bus; | |
190 | struct completion done; | |
191 | struct sbp2_status status; | |
192 | }; | |
193 | ||
a77754a7 KH |
194 | #define LOGIN_RESPONSE_GET_LOGIN_ID(v) ((v).misc & 0xffff) |
195 | #define LOGIN_RESPONSE_GET_LENGTH(v) (((v).misc >> 16) & 0xffff) | |
9ba136d0 KH |
196 | |
197 | struct sbp2_login_response { | |
198 | u32 misc; | |
199 | struct sbp2_pointer command_block_agent; | |
200 | u32 reconnect_hold; | |
201 | }; | |
a77754a7 KH |
202 | #define COMMAND_ORB_DATA_SIZE(v) ((v)) |
203 | #define COMMAND_ORB_PAGE_SIZE(v) ((v) << 16) | |
204 | #define COMMAND_ORB_PAGE_TABLE_PRESENT ((1) << 19) | |
205 | #define COMMAND_ORB_MAX_PAYLOAD(v) ((v) << 20) | |
206 | #define COMMAND_ORB_SPEED(v) ((v) << 24) | |
207 | #define COMMAND_ORB_DIRECTION(v) ((v) << 27) | |
208 | #define COMMAND_ORB_REQUEST_FORMAT(v) ((v) << 29) | |
209 | #define COMMAND_ORB_NOTIFY ((1) << 31) | |
9ba136d0 KH |
210 | |
211 | struct sbp2_command_orb { | |
212 | struct sbp2_orb base; | |
213 | struct { | |
214 | struct sbp2_pointer next; | |
215 | struct sbp2_pointer data_descriptor; | |
216 | u32 misc; | |
217 | u8 command_block[12]; | |
218 | } request; | |
219 | struct scsi_cmnd *cmd; | |
220 | scsi_done_fn_t done; | |
221 | struct fw_unit *unit; | |
222 | ||
9fb2dd12 | 223 | struct sbp2_pointer page_table[SG_ALL] __attribute__((aligned(8))); |
9ba136d0 | 224 | dma_addr_t page_table_bus; |
9ba136d0 KH |
225 | }; |
226 | ||
227 | /* | |
228 | * List of devices with known bugs. | |
229 | * | |
230 | * The firmware_revision field, masked with 0xffff00, is the best | |
231 | * indicator for the type of bridge chip of a device. It yields a few | |
232 | * false positives but this did not break correctly behaving devices | |
233 | * so far. We use ~0 as a wildcard, since the 24 bit values we get | |
234 | * from the config rom can never match that. | |
235 | */ | |
236 | static const struct { | |
237 | u32 firmware_revision; | |
238 | u32 model; | |
239 | unsigned workarounds; | |
240 | } sbp2_workarounds_table[] = { | |
241 | /* DViCO Momobay CX-1 with TSB42AA9 bridge */ { | |
242 | .firmware_revision = 0x002800, | |
243 | .model = 0x001010, | |
244 | .workarounds = SBP2_WORKAROUND_INQUIRY_36 | | |
245 | SBP2_WORKAROUND_MODE_SENSE_8, | |
246 | }, | |
247 | /* Initio bridges, actually only needed for some older ones */ { | |
248 | .firmware_revision = 0x000200, | |
249 | .model = ~0, | |
250 | .workarounds = SBP2_WORKAROUND_INQUIRY_36, | |
251 | }, | |
252 | /* Symbios bridge */ { | |
253 | .firmware_revision = 0xa0b800, | |
254 | .model = ~0, | |
255 | .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS, | |
256 | }, | |
c781c06d KH |
257 | |
258 | /* | |
259 | * There are iPods (2nd gen, 3rd gen) with model_id == 0, but | |
9ba136d0 KH |
260 | * these iPods do not feature the read_capacity bug according |
261 | * to one report. Read_capacity behaviour as well as model_id | |
c781c06d KH |
262 | * could change due to Apple-supplied firmware updates though. |
263 | */ | |
264 | ||
9ba136d0 KH |
265 | /* iPod 4th generation. */ { |
266 | .firmware_revision = 0x0a2700, | |
267 | .model = 0x000021, | |
268 | .workarounds = SBP2_WORKAROUND_FIX_CAPACITY, | |
269 | }, | |
270 | /* iPod mini */ { | |
271 | .firmware_revision = 0x0a2700, | |
272 | .model = 0x000023, | |
273 | .workarounds = SBP2_WORKAROUND_FIX_CAPACITY, | |
274 | }, | |
275 | /* iPod Photo */ { | |
276 | .firmware_revision = 0x0a2700, | |
277 | .model = 0x00007e, | |
278 | .workarounds = SBP2_WORKAROUND_FIX_CAPACITY, | |
279 | } | |
280 | }; | |
281 | ||
282 | static void | |
283 | sbp2_status_write(struct fw_card *card, struct fw_request *request, | |
284 | int tcode, int destination, int source, | |
285 | int generation, int speed, | |
286 | unsigned long long offset, | |
287 | void *payload, size_t length, void *callback_data) | |
288 | { | |
289 | struct sbp2_device *sd = callback_data; | |
290 | struct sbp2_orb *orb; | |
291 | struct sbp2_status status; | |
292 | size_t header_size; | |
293 | unsigned long flags; | |
294 | ||
295 | if (tcode != TCODE_WRITE_BLOCK_REQUEST || | |
2d826cc5 | 296 | length == 0 || length > sizeof(status)) { |
9ba136d0 KH |
297 | fw_send_response(card, request, RCODE_TYPE_ERROR); |
298 | return; | |
299 | } | |
300 | ||
301 | header_size = min(length, 2 * sizeof(u32)); | |
302 | fw_memcpy_from_be32(&status, payload, header_size); | |
303 | if (length > header_size) | |
304 | memcpy(status.data, payload + 8, length - header_size); | |
a77754a7 | 305 | if (STATUS_GET_SOURCE(status) == 2 || STATUS_GET_SOURCE(status) == 3) { |
9ba136d0 KH |
306 | fw_notify("non-orb related status write, not handled\n"); |
307 | fw_send_response(card, request, RCODE_COMPLETE); | |
308 | return; | |
309 | } | |
310 | ||
311 | /* Lookup the orb corresponding to this status write. */ | |
312 | spin_lock_irqsave(&card->lock, flags); | |
313 | list_for_each_entry(orb, &sd->orb_list, link) { | |
a77754a7 KH |
314 | if (STATUS_GET_ORB_HIGH(status) == 0 && |
315 | STATUS_GET_ORB_LOW(status) == orb->request_bus && | |
12f26aa1 | 316 | orb->rcode == RCODE_COMPLETE) { |
9ba136d0 KH |
317 | list_del(&orb->link); |
318 | break; | |
319 | } | |
320 | } | |
321 | spin_unlock_irqrestore(&card->lock, flags); | |
322 | ||
323 | if (&orb->link != &sd->orb_list) | |
324 | orb->callback(orb, &status); | |
325 | else | |
326 | fw_error("status write for unknown orb\n"); | |
327 | ||
328 | fw_send_response(card, request, RCODE_COMPLETE); | |
329 | } | |
330 | ||
331 | static void | |
332 | complete_transaction(struct fw_card *card, int rcode, | |
333 | void *payload, size_t length, void *data) | |
334 | { | |
335 | struct sbp2_orb *orb = data; | |
336 | unsigned long flags; | |
337 | ||
338 | orb->rcode = rcode; | |
339 | if (rcode != RCODE_COMPLETE) { | |
340 | spin_lock_irqsave(&card->lock, flags); | |
341 | list_del(&orb->link); | |
342 | spin_unlock_irqrestore(&card->lock, flags); | |
343 | orb->callback(orb, NULL); | |
344 | } | |
345 | } | |
346 | ||
347 | static void | |
348 | sbp2_send_orb(struct sbp2_orb *orb, struct fw_unit *unit, | |
349 | int node_id, int generation, u64 offset) | |
350 | { | |
351 | struct fw_device *device = fw_device(unit->device.parent); | |
352 | struct sbp2_device *sd = unit->device.driver_data; | |
353 | unsigned long flags; | |
354 | ||
355 | orb->pointer.high = 0; | |
356 | orb->pointer.low = orb->request_bus; | |
2d826cc5 | 357 | fw_memcpy_to_be32(&orb->pointer, &orb->pointer, sizeof(orb->pointer)); |
9ba136d0 KH |
358 | |
359 | spin_lock_irqsave(&device->card->lock, flags); | |
360 | list_add_tail(&orb->link, &sd->orb_list); | |
361 | spin_unlock_irqrestore(&device->card->lock, flags); | |
362 | ||
363 | fw_send_request(device->card, &orb->t, TCODE_WRITE_BLOCK_REQUEST, | |
f1397490 | 364 | node_id, generation, device->max_speed, offset, |
2d826cc5 | 365 | &orb->pointer, sizeof(orb->pointer), |
9ba136d0 KH |
366 | complete_transaction, orb); |
367 | } | |
368 | ||
2aaad97b | 369 | static int sbp2_cancel_orbs(struct fw_unit *unit) |
9ba136d0 KH |
370 | { |
371 | struct fw_device *device = fw_device(unit->device.parent); | |
372 | struct sbp2_device *sd = unit->device.driver_data; | |
373 | struct sbp2_orb *orb, *next; | |
374 | struct list_head list; | |
375 | unsigned long flags; | |
2aaad97b | 376 | int retval = -ENOENT; |
9ba136d0 KH |
377 | |
378 | INIT_LIST_HEAD(&list); | |
379 | spin_lock_irqsave(&device->card->lock, flags); | |
380 | list_splice_init(&sd->orb_list, &list); | |
381 | spin_unlock_irqrestore(&device->card->lock, flags); | |
382 | ||
383 | list_for_each_entry_safe(orb, next, &list, link) { | |
2aaad97b | 384 | retval = 0; |
730c32f5 KH |
385 | if (fw_cancel_transaction(device->card, &orb->t) == 0) |
386 | continue; | |
387 | ||
9ba136d0 KH |
388 | orb->rcode = RCODE_CANCELLED; |
389 | orb->callback(orb, NULL); | |
390 | } | |
9ba136d0 | 391 | |
2aaad97b | 392 | return retval; |
1d3d52c5 KH |
393 | } |
394 | ||
9ba136d0 KH |
395 | static void |
396 | complete_management_orb(struct sbp2_orb *base_orb, struct sbp2_status *status) | |
397 | { | |
398 | struct sbp2_management_orb *orb = | |
6f061487 | 399 | container_of(base_orb, struct sbp2_management_orb, base); |
9ba136d0 KH |
400 | |
401 | if (status) | |
2d826cc5 | 402 | memcpy(&orb->status, status, sizeof(*status)); |
9ba136d0 KH |
403 | complete(&orb->done); |
404 | } | |
405 | ||
406 | static int | |
407 | sbp2_send_management_orb(struct fw_unit *unit, int node_id, int generation, | |
408 | int function, int lun, void *response) | |
409 | { | |
410 | struct fw_device *device = fw_device(unit->device.parent); | |
411 | struct sbp2_device *sd = unit->device.driver_data; | |
412 | struct sbp2_management_orb *orb; | |
9ba136d0 KH |
413 | int retval = -ENOMEM; |
414 | ||
2d826cc5 | 415 | orb = kzalloc(sizeof(*orb), GFP_ATOMIC); |
9ba136d0 KH |
416 | if (orb == NULL) |
417 | return -ENOMEM; | |
418 | ||
c781c06d KH |
419 | /* |
420 | * The sbp2 device is going to send a block read request to | |
421 | * read out the request from host memory, so map it for dma. | |
422 | */ | |
9ba136d0 KH |
423 | orb->base.request_bus = |
424 | dma_map_single(device->card->device, &orb->request, | |
2d826cc5 | 425 | sizeof(orb->request), DMA_TO_DEVICE); |
82eff9db | 426 | if (dma_mapping_error(orb->base.request_bus)) |
9ba136d0 KH |
427 | goto out; |
428 | ||
429 | orb->response_bus = | |
430 | dma_map_single(device->card->device, &orb->response, | |
2d826cc5 | 431 | sizeof(orb->response), DMA_FROM_DEVICE); |
82eff9db | 432 | if (dma_mapping_error(orb->response_bus)) |
9ba136d0 KH |
433 | goto out; |
434 | ||
435 | orb->request.response.high = 0; | |
436 | orb->request.response.low = orb->response_bus; | |
437 | ||
438 | orb->request.misc = | |
a77754a7 KH |
439 | MANAGEMENT_ORB_NOTIFY | |
440 | MANAGEMENT_ORB_FUNCTION(function) | | |
441 | MANAGEMENT_ORB_LUN(lun); | |
9ba136d0 | 442 | orb->request.length = |
2d826cc5 | 443 | MANAGEMENT_ORB_RESPONSE_LENGTH(sizeof(orb->response)); |
9ba136d0 KH |
444 | |
445 | orb->request.status_fifo.high = sd->address_handler.offset >> 32; | |
446 | orb->request.status_fifo.low = sd->address_handler.offset; | |
447 | ||
9ba136d0 KH |
448 | if (function == SBP2_LOGIN_REQUEST) { |
449 | orb->request.misc |= | |
5cd54c94 | 450 | MANAGEMENT_ORB_EXCLUSIVE(sbp2_param_exclusive_login) | |
a77754a7 | 451 | MANAGEMENT_ORB_RECONNECT(0); |
9ba136d0 KH |
452 | } |
453 | ||
2d826cc5 | 454 | fw_memcpy_to_be32(&orb->request, &orb->request, sizeof(orb->request)); |
9ba136d0 KH |
455 | |
456 | init_completion(&orb->done); | |
457 | orb->base.callback = complete_management_orb; | |
2aaad97b | 458 | |
9ba136d0 KH |
459 | sbp2_send_orb(&orb->base, unit, |
460 | node_id, generation, sd->management_agent_address); | |
461 | ||
2aaad97b KH |
462 | wait_for_completion_timeout(&orb->done, |
463 | msecs_to_jiffies(SBP2_ORB_TIMEOUT)); | |
9ba136d0 | 464 | |
9ba136d0 | 465 | retval = -EIO; |
2aaad97b KH |
466 | if (sbp2_cancel_orbs(unit) == 0) { |
467 | fw_error("orb reply timed out, rcode=0x%02x\n", | |
9ba136d0 KH |
468 | orb->base.rcode); |
469 | goto out; | |
470 | } | |
471 | ||
2aaad97b KH |
472 | if (orb->base.rcode != RCODE_COMPLETE) { |
473 | fw_error("management write failed, rcode 0x%02x\n", | |
9ba136d0 KH |
474 | orb->base.rcode); |
475 | goto out; | |
476 | } | |
477 | ||
a77754a7 KH |
478 | if (STATUS_GET_RESPONSE(orb->status) != 0 || |
479 | STATUS_GET_SBP_STATUS(orb->status) != 0) { | |
9ba136d0 | 480 | fw_error("error status: %d:%d\n", |
a77754a7 KH |
481 | STATUS_GET_RESPONSE(orb->status), |
482 | STATUS_GET_SBP_STATUS(orb->status)); | |
9ba136d0 KH |
483 | goto out; |
484 | } | |
485 | ||
486 | retval = 0; | |
487 | out: | |
488 | dma_unmap_single(device->card->device, orb->base.request_bus, | |
2d826cc5 | 489 | sizeof(orb->request), DMA_TO_DEVICE); |
9ba136d0 | 490 | dma_unmap_single(device->card->device, orb->response_bus, |
2d826cc5 | 491 | sizeof(orb->response), DMA_FROM_DEVICE); |
9ba136d0 KH |
492 | |
493 | if (response) | |
494 | fw_memcpy_from_be32(response, | |
2d826cc5 | 495 | orb->response, sizeof(orb->response)); |
9ba136d0 KH |
496 | kfree(orb); |
497 | ||
498 | return retval; | |
499 | } | |
500 | ||
501 | static void | |
502 | complete_agent_reset_write(struct fw_card *card, int rcode, | |
503 | void *payload, size_t length, void *data) | |
504 | { | |
505 | struct fw_transaction *t = data; | |
506 | ||
9ba136d0 KH |
507 | kfree(t); |
508 | } | |
509 | ||
510 | static int sbp2_agent_reset(struct fw_unit *unit) | |
511 | { | |
512 | struct fw_device *device = fw_device(unit->device.parent); | |
513 | struct sbp2_device *sd = unit->device.driver_data; | |
514 | struct fw_transaction *t; | |
515 | static u32 zero; | |
516 | ||
2d826cc5 | 517 | t = kzalloc(sizeof(*t), GFP_ATOMIC); |
9ba136d0 KH |
518 | if (t == NULL) |
519 | return -ENOMEM; | |
520 | ||
521 | fw_send_request(device->card, t, TCODE_WRITE_QUADLET_REQUEST, | |
ffd0db26 | 522 | sd->node_id, sd->generation, device->max_speed, |
9ba136d0 | 523 | sd->command_block_agent_address + SBP2_AGENT_RESET, |
2d826cc5 | 524 | &zero, sizeof(zero), complete_agent_reset_write, t); |
9ba136d0 KH |
525 | |
526 | return 0; | |
527 | } | |
528 | ||
7f37c426 | 529 | static void sbp2_reconnect(struct work_struct *work); |
ad85274f | 530 | static struct scsi_host_template scsi_driver_template; |
7f37c426 | 531 | |
79352e9f | 532 | static void release_sbp2_device(struct kref *kref) |
b3d6e151 KH |
533 | { |
534 | struct sbp2_device *sd = container_of(kref, struct sbp2_device, kref); | |
ad85274f KH |
535 | struct Scsi_Host *host = |
536 | container_of((void *)sd, struct Scsi_Host, hostdata[0]); | |
b3d6e151 | 537 | |
79352e9f | 538 | scsi_remove_host(host); |
b3d6e151 KH |
539 | sbp2_send_management_orb(sd->unit, sd->node_id, sd->generation, |
540 | SBP2_LOGOUT_REQUEST, sd->login_id, NULL); | |
b3d6e151 KH |
541 | fw_core_remove_address_handler(&sd->address_handler); |
542 | fw_notify("removed sbp2 unit %s\n", sd->unit->device.bus_id); | |
543 | put_device(&sd->unit->device); | |
ad85274f | 544 | scsi_host_put(host); |
b3d6e151 KH |
545 | } |
546 | ||
7f37c426 KH |
547 | static void sbp2_login(struct work_struct *work) |
548 | { | |
549 | struct sbp2_device *sd = | |
550 | container_of(work, struct sbp2_device, work.work); | |
ad85274f KH |
551 | struct Scsi_Host *host = |
552 | container_of((void *)sd, struct Scsi_Host, hostdata[0]); | |
7f37c426 KH |
553 | struct fw_unit *unit = sd->unit; |
554 | struct fw_device *device = fw_device(unit->device.parent); | |
555 | struct sbp2_login_response response; | |
556 | int generation, node_id, local_node_id, lun, retval; | |
557 | ||
558 | /* FIXME: Make this work for multi-lun devices. */ | |
559 | lun = 0; | |
560 | ||
561 | generation = device->card->generation; | |
562 | node_id = device->node->node_id; | |
563 | local_node_id = device->card->local_node->node_id; | |
564 | ||
565 | if (sbp2_send_management_orb(unit, node_id, generation, | |
566 | SBP2_LOGIN_REQUEST, lun, &response) < 0) { | |
567 | if (sd->retries++ < 5) { | |
7f37c426 KH |
568 | schedule_delayed_work(&sd->work, DIV_ROUND_UP(HZ, 5)); |
569 | } else { | |
570 | fw_error("failed to login to %s\n", | |
571 | unit->device.bus_id); | |
b3d6e151 | 572 | kref_put(&sd->kref, release_sbp2_device); |
7f37c426 KH |
573 | } |
574 | return; | |
575 | } | |
576 | ||
577 | sd->generation = generation; | |
578 | sd->node_id = node_id; | |
579 | sd->address_high = local_node_id << 16; | |
580 | ||
581 | /* Get command block agent offset and login id. */ | |
582 | sd->command_block_agent_address = | |
5c5539d8 | 583 | ((u64) (response.command_block_agent.high & 0xffff) << 32) | |
7f37c426 | 584 | response.command_block_agent.low; |
a77754a7 | 585 | sd->login_id = LOGIN_RESPONSE_GET_LOGIN_ID(response); |
7f37c426 | 586 | |
5c5539d8 KH |
587 | fw_notify("logged in to sbp2 unit %s (%d retries)\n", |
588 | unit->device.bus_id, sd->retries); | |
589 | fw_notify(" - management_agent_address: 0x%012llx\n", | |
7f37c426 KH |
590 | (unsigned long long) sd->management_agent_address); |
591 | fw_notify(" - command_block_agent_address: 0x%012llx\n", | |
592 | (unsigned long long) sd->command_block_agent_address); | |
5c5539d8 | 593 | fw_notify(" - status write address: 0x%012llx\n", |
7f37c426 KH |
594 | (unsigned long long) sd->address_handler.offset); |
595 | ||
596 | #if 0 | |
597 | /* FIXME: The linux1394 sbp2 does this last step. */ | |
598 | sbp2_set_busy_timeout(scsi_id); | |
599 | #endif | |
600 | ||
1da0c93b | 601 | PREPARE_DELAYED_WORK(&sd->work, sbp2_reconnect); |
7f37c426 KH |
602 | sbp2_agent_reset(unit); |
603 | ||
ad85274f KH |
604 | /* FIXME: Loop over luns here. */ |
605 | lun = 0; | |
606 | retval = scsi_add_device(host, 0, 0, lun); | |
7f37c426 KH |
607 | if (retval < 0) { |
608 | sbp2_send_management_orb(unit, sd->node_id, sd->generation, | |
609 | SBP2_LOGOUT_REQUEST, sd->login_id, | |
610 | NULL); | |
c781c06d KH |
611 | /* |
612 | * Set this back to sbp2_login so we fall back and | |
613 | * retry login on bus reset. | |
614 | */ | |
1da0c93b | 615 | PREPARE_DELAYED_WORK(&sd->work, sbp2_login); |
7f37c426 | 616 | } |
b3d6e151 | 617 | kref_put(&sd->kref, release_sbp2_device); |
7f37c426 | 618 | } |
9ba136d0 KH |
619 | |
620 | static int sbp2_probe(struct device *dev) | |
621 | { | |
622 | struct fw_unit *unit = fw_unit(dev); | |
623 | struct fw_device *device = fw_device(unit->device.parent); | |
624 | struct sbp2_device *sd; | |
625 | struct fw_csr_iterator ci; | |
ad85274f KH |
626 | struct Scsi_Host *host; |
627 | int i, key, value, err; | |
9ba136d0 KH |
628 | u32 model, firmware_revision; |
629 | ||
ad85274f KH |
630 | err = -ENOMEM; |
631 | host = scsi_host_alloc(&scsi_driver_template, sizeof(*sd)); | |
632 | if (host == NULL) | |
633 | goto fail; | |
9ba136d0 | 634 | |
ad85274f | 635 | sd = (struct sbp2_device *) host->hostdata; |
9ba136d0 KH |
636 | unit->device.driver_data = sd; |
637 | sd->unit = unit; | |
638 | INIT_LIST_HEAD(&sd->orb_list); | |
b3d6e151 | 639 | kref_init(&sd->kref); |
9ba136d0 KH |
640 | |
641 | sd->address_handler.length = 0x100; | |
642 | sd->address_handler.address_callback = sbp2_status_write; | |
643 | sd->address_handler.callback_data = sd; | |
644 | ||
ad85274f KH |
645 | err = fw_core_add_address_handler(&sd->address_handler, |
646 | &fw_high_memory_region); | |
647 | if (err < 0) | |
648 | goto fail_host; | |
9ba136d0 | 649 | |
ad85274f KH |
650 | err = fw_device_enable_phys_dma(device); |
651 | if (err < 0) | |
652 | goto fail_address_handler; | |
653 | ||
654 | err = scsi_add_host(host, &unit->device); | |
655 | if (err < 0) | |
656 | goto fail_address_handler; | |
9ba136d0 | 657 | |
c781c06d KH |
658 | /* |
659 | * Scan unit directory to get management agent address, | |
9ba136d0 | 660 | * firmware revison and model. Initialize firmware_revision |
c781c06d KH |
661 | * and model to values that wont match anything in our table. |
662 | */ | |
9ba136d0 KH |
663 | firmware_revision = 0xff000000; |
664 | model = 0xff000000; | |
665 | fw_csr_iterator_init(&ci, unit->directory); | |
666 | while (fw_csr_iterator_next(&ci, &key, &value)) { | |
667 | switch (key) { | |
668 | case CSR_DEPENDENT_INFO | CSR_OFFSET: | |
669 | sd->management_agent_address = | |
670 | 0xfffff0000000ULL + 4 * value; | |
671 | break; | |
672 | case SBP2_FIRMWARE_REVISION: | |
673 | firmware_revision = value; | |
674 | break; | |
675 | case CSR_MODEL: | |
676 | model = value; | |
677 | break; | |
678 | } | |
679 | } | |
680 | ||
681 | for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) { | |
682 | if (sbp2_workarounds_table[i].firmware_revision != | |
683 | (firmware_revision & 0xffffff00)) | |
684 | continue; | |
685 | if (sbp2_workarounds_table[i].model != model && | |
686 | sbp2_workarounds_table[i].model != ~0) | |
687 | continue; | |
688 | sd->workarounds |= sbp2_workarounds_table[i].workarounds; | |
689 | break; | |
690 | } | |
691 | ||
692 | if (sd->workarounds) | |
693 | fw_notify("Workarounds for node %s: 0x%x " | |
694 | "(firmware_revision 0x%06x, model_id 0x%06x)\n", | |
695 | unit->device.bus_id, | |
696 | sd->workarounds, firmware_revision, model); | |
697 | ||
b3d6e151 KH |
698 | get_device(&unit->device); |
699 | ||
c781c06d KH |
700 | /* |
701 | * We schedule work to do the login so we can easily | |
b3d6e151 | 702 | * reschedule retries. Always get the ref before scheduling |
c781c06d KH |
703 | * work. |
704 | */ | |
7f37c426 | 705 | INIT_DELAYED_WORK(&sd->work, sbp2_login); |
b3d6e151 KH |
706 | if (schedule_delayed_work(&sd->work, 0)) |
707 | kref_get(&sd->kref); | |
9ba136d0 KH |
708 | |
709 | return 0; | |
ad85274f KH |
710 | |
711 | fail_address_handler: | |
712 | fw_core_remove_address_handler(&sd->address_handler); | |
713 | fail_host: | |
714 | scsi_host_put(host); | |
715 | fail: | |
716 | return err; | |
9ba136d0 KH |
717 | } |
718 | ||
719 | static int sbp2_remove(struct device *dev) | |
720 | { | |
721 | struct fw_unit *unit = fw_unit(dev); | |
722 | struct sbp2_device *sd = unit->device.driver_data; | |
723 | ||
b3d6e151 | 724 | kref_put(&sd->kref, release_sbp2_device); |
9ba136d0 KH |
725 | |
726 | return 0; | |
727 | } | |
728 | ||
729 | static void sbp2_reconnect(struct work_struct *work) | |
730 | { | |
7f37c426 KH |
731 | struct sbp2_device *sd = |
732 | container_of(work, struct sbp2_device, work.work); | |
9ba136d0 KH |
733 | struct fw_unit *unit = sd->unit; |
734 | struct fw_device *device = fw_device(unit->device.parent); | |
735 | int generation, node_id, local_node_id; | |
736 | ||
9ba136d0 KH |
737 | generation = device->card->generation; |
738 | node_id = device->node->node_id; | |
739 | local_node_id = device->card->local_node->node_id; | |
740 | ||
7f37c426 KH |
741 | if (sbp2_send_management_orb(unit, node_id, generation, |
742 | SBP2_RECONNECT_REQUEST, | |
743 | sd->login_id, NULL) < 0) { | |
5c5539d8 | 744 | if (sd->retries++ >= 5) { |
7f37c426 KH |
745 | fw_error("failed to reconnect to %s\n", |
746 | unit->device.bus_id); | |
747 | /* Fall back and try to log in again. */ | |
748 | sd->retries = 0; | |
1da0c93b | 749 | PREPARE_DELAYED_WORK(&sd->work, sbp2_login); |
7f37c426 KH |
750 | } |
751 | schedule_delayed_work(&sd->work, DIV_ROUND_UP(HZ, 5)); | |
752 | return; | |
753 | } | |
9ba136d0 KH |
754 | |
755 | sd->generation = generation; | |
756 | sd->node_id = node_id; | |
907293d7 | 757 | sd->address_high = local_node_id << 16; |
7f37c426 | 758 | |
5c5539d8 KH |
759 | fw_notify("reconnected to unit %s (%d retries)\n", |
760 | unit->device.bus_id, sd->retries); | |
7f37c426 KH |
761 | sbp2_agent_reset(unit); |
762 | sbp2_cancel_orbs(unit); | |
b3d6e151 | 763 | kref_put(&sd->kref, release_sbp2_device); |
9ba136d0 KH |
764 | } |
765 | ||
766 | static void sbp2_update(struct fw_unit *unit) | |
767 | { | |
768 | struct fw_device *device = fw_device(unit->device.parent); | |
769 | struct sbp2_device *sd = unit->device.driver_data; | |
770 | ||
7f37c426 | 771 | sd->retries = 0; |
9ba136d0 | 772 | fw_device_enable_phys_dma(device); |
b3d6e151 KH |
773 | if (schedule_delayed_work(&sd->work, 0)) |
774 | kref_get(&sd->kref); | |
9ba136d0 KH |
775 | } |
776 | ||
777 | #define SBP2_UNIT_SPEC_ID_ENTRY 0x0000609e | |
778 | #define SBP2_SW_VERSION_ENTRY 0x00010483 | |
779 | ||
21ebcd12 | 780 | static const struct fw_device_id sbp2_id_table[] = { |
9ba136d0 KH |
781 | { |
782 | .match_flags = FW_MATCH_SPECIFIER_ID | FW_MATCH_VERSION, | |
783 | .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY, | |
5af4e5ea | 784 | .version = SBP2_SW_VERSION_ENTRY, |
9ba136d0 KH |
785 | }, |
786 | { } | |
787 | }; | |
788 | ||
789 | static struct fw_driver sbp2_driver = { | |
790 | .driver = { | |
791 | .owner = THIS_MODULE, | |
792 | .name = sbp2_driver_name, | |
793 | .bus = &fw_bus_type, | |
794 | .probe = sbp2_probe, | |
795 | .remove = sbp2_remove, | |
796 | }, | |
797 | .update = sbp2_update, | |
798 | .id_table = sbp2_id_table, | |
799 | }; | |
800 | ||
fbb5423c KH |
801 | static unsigned int |
802 | sbp2_status_to_sense_data(u8 *sbp2_status, u8 *sense_data) | |
9ba136d0 | 803 | { |
fbb5423c KH |
804 | int sam_status; |
805 | ||
9ba136d0 KH |
806 | sense_data[0] = 0x70; |
807 | sense_data[1] = 0x0; | |
808 | sense_data[2] = sbp2_status[1]; | |
809 | sense_data[3] = sbp2_status[4]; | |
810 | sense_data[4] = sbp2_status[5]; | |
811 | sense_data[5] = sbp2_status[6]; | |
812 | sense_data[6] = sbp2_status[7]; | |
813 | sense_data[7] = 10; | |
814 | sense_data[8] = sbp2_status[8]; | |
815 | sense_data[9] = sbp2_status[9]; | |
816 | sense_data[10] = sbp2_status[10]; | |
817 | sense_data[11] = sbp2_status[11]; | |
818 | sense_data[12] = sbp2_status[2]; | |
819 | sense_data[13] = sbp2_status[3]; | |
820 | sense_data[14] = sbp2_status[12]; | |
821 | sense_data[15] = sbp2_status[13]; | |
822 | ||
fbb5423c | 823 | sam_status = sbp2_status[0] & 0x3f; |
9ba136d0 | 824 | |
fbb5423c KH |
825 | switch (sam_status) { |
826 | case SAM_STAT_GOOD: | |
9ba136d0 | 827 | case SAM_STAT_CHECK_CONDITION: |
9ba136d0 | 828 | case SAM_STAT_CONDITION_MET: |
fbb5423c | 829 | case SAM_STAT_BUSY: |
9ba136d0 KH |
830 | case SAM_STAT_RESERVATION_CONFLICT: |
831 | case SAM_STAT_COMMAND_TERMINATED: | |
fbb5423c KH |
832 | return DID_OK << 16 | sam_status; |
833 | ||
9ba136d0 | 834 | default: |
fbb5423c | 835 | return DID_ERROR << 16; |
9ba136d0 KH |
836 | } |
837 | } | |
838 | ||
839 | static void | |
840 | complete_command_orb(struct sbp2_orb *base_orb, struct sbp2_status *status) | |
841 | { | |
6f061487 JF |
842 | struct sbp2_command_orb *orb = |
843 | container_of(base_orb, struct sbp2_command_orb, base); | |
9ba136d0 KH |
844 | struct fw_unit *unit = orb->unit; |
845 | struct fw_device *device = fw_device(unit->device.parent); | |
846 | struct scatterlist *sg; | |
847 | int result; | |
848 | ||
849 | if (status != NULL) { | |
a77754a7 | 850 | if (STATUS_GET_DEAD(*status)) |
9ba136d0 | 851 | sbp2_agent_reset(unit); |
9ba136d0 | 852 | |
a77754a7 | 853 | switch (STATUS_GET_RESPONSE(*status)) { |
9ba136d0 | 854 | case SBP2_STATUS_REQUEST_COMPLETE: |
fbb5423c | 855 | result = DID_OK << 16; |
9ba136d0 KH |
856 | break; |
857 | case SBP2_STATUS_TRANSPORT_FAILURE: | |
fbb5423c | 858 | result = DID_BUS_BUSY << 16; |
9ba136d0 KH |
859 | break; |
860 | case SBP2_STATUS_ILLEGAL_REQUEST: | |
861 | case SBP2_STATUS_VENDOR_DEPENDENT: | |
862 | default: | |
fbb5423c | 863 | result = DID_ERROR << 16; |
9ba136d0 KH |
864 | break; |
865 | } | |
866 | ||
a77754a7 KH |
867 | if (result == DID_OK << 16 && STATUS_GET_LEN(*status) > 1) |
868 | result = sbp2_status_to_sense_data(STATUS_GET_DATA(*status), | |
9ba136d0 KH |
869 | orb->cmd->sense_buffer); |
870 | } else { | |
c781c06d KH |
871 | /* |
872 | * If the orb completes with status == NULL, something | |
9ba136d0 | 873 | * went wrong, typically a bus reset happened mid-orb |
c781c06d KH |
874 | * or when sending the write (less likely). |
875 | */ | |
fbb5423c | 876 | result = DID_BUS_BUSY << 16; |
9ba136d0 KH |
877 | } |
878 | ||
879 | dma_unmap_single(device->card->device, orb->base.request_bus, | |
2d826cc5 | 880 | sizeof(orb->request), DMA_TO_DEVICE); |
9ba136d0 KH |
881 | |
882 | if (orb->cmd->use_sg > 0) { | |
883 | sg = (struct scatterlist *)orb->cmd->request_buffer; | |
884 | dma_unmap_sg(device->card->device, sg, orb->cmd->use_sg, | |
885 | orb->cmd->sc_data_direction); | |
886 | } | |
887 | ||
888 | if (orb->page_table_bus != 0) | |
889 | dma_unmap_single(device->card->device, orb->page_table_bus, | |
2d826cc5 | 890 | sizeof(orb->page_table_bus), DMA_TO_DEVICE); |
9ba136d0 | 891 | |
fbb5423c | 892 | orb->cmd->result = result; |
9ba136d0 | 893 | orb->done(orb->cmd); |
9ba136d0 KH |
894 | kfree(orb); |
895 | } | |
896 | ||
95ffc5e3 | 897 | static int sbp2_command_orb_map_scatterlist(struct sbp2_command_orb *orb) |
9ba136d0 | 898 | { |
ad85274f KH |
899 | struct sbp2_device *sd = |
900 | (struct sbp2_device *)orb->cmd->device->host->hostdata; | |
901 | struct fw_unit *unit = sd->unit; | |
9ba136d0 | 902 | struct fw_device *device = fw_device(unit->device.parent); |
9ba136d0 KH |
903 | struct scatterlist *sg; |
904 | int sg_len, l, i, j, count; | |
905 | size_t size; | |
906 | dma_addr_t sg_addr; | |
907 | ||
908 | sg = (struct scatterlist *)orb->cmd->request_buffer; | |
909 | count = dma_map_sg(device->card->device, sg, orb->cmd->use_sg, | |
910 | orb->cmd->sc_data_direction); | |
95ffc5e3 KH |
911 | if (count == 0) |
912 | goto fail; | |
9ba136d0 | 913 | |
c781c06d KH |
914 | /* |
915 | * Handle the special case where there is only one element in | |
9ba136d0 KH |
916 | * the scatter list by converting it to an immediate block |
917 | * request. This is also a workaround for broken devices such | |
918 | * as the second generation iPod which doesn't support page | |
c781c06d KH |
919 | * tables. |
920 | */ | |
9ba136d0 KH |
921 | if (count == 1 && sg_dma_len(sg) < SBP2_MAX_SG_ELEMENT_LENGTH) { |
922 | orb->request.data_descriptor.high = sd->address_high; | |
923 | orb->request.data_descriptor.low = sg_dma_address(sg); | |
924 | orb->request.misc |= | |
a77754a7 | 925 | COMMAND_ORB_DATA_SIZE(sg_dma_len(sg)); |
95ffc5e3 | 926 | return 0; |
9ba136d0 KH |
927 | } |
928 | ||
c781c06d KH |
929 | /* |
930 | * Convert the scatterlist to an sbp2 page table. If any | |
36abb3b1 KHSR |
931 | * scatterlist entries are too big for sbp2, we split them as we |
932 | * go. Even if we ask the block I/O layer to not give us sg | |
933 | * elements larger than 65535 bytes, some IOMMUs may merge sg elements | |
934 | * during DMA mapping, and Linux currently doesn't prevent this. | |
c781c06d | 935 | */ |
9ba136d0 KH |
936 | for (i = 0, j = 0; i < count; i++) { |
937 | sg_len = sg_dma_len(sg + i); | |
938 | sg_addr = sg_dma_address(sg + i); | |
939 | while (sg_len) { | |
332ef331 SR |
940 | /* FIXME: This won't get us out of the pinch. */ |
941 | if (unlikely(j >= ARRAY_SIZE(orb->page_table))) { | |
942 | fw_error("page table overflow\n"); | |
943 | goto fail_page_table; | |
944 | } | |
9ba136d0 KH |
945 | l = min(sg_len, SBP2_MAX_SG_ELEMENT_LENGTH); |
946 | orb->page_table[j].low = sg_addr; | |
947 | orb->page_table[j].high = (l << 16); | |
948 | sg_addr += l; | |
949 | sg_len -= l; | |
950 | j++; | |
951 | } | |
952 | } | |
953 | ||
2d826cc5 | 954 | size = sizeof(orb->page_table[0]) * j; |
9ba136d0 | 955 | |
c781c06d KH |
956 | /* |
957 | * The data_descriptor pointer is the one case where we need | |
9ba136d0 KH |
958 | * to fill in the node ID part of the address. All other |
959 | * pointers assume that the data referenced reside on the | |
960 | * initiator (i.e. us), but data_descriptor can refer to data | |
c781c06d KH |
961 | * on other nodes so we need to put our ID in descriptor.high. |
962 | */ | |
9ba136d0 KH |
963 | |
964 | orb->page_table_bus = | |
965 | dma_map_single(device->card->device, orb->page_table, | |
966 | size, DMA_TO_DEVICE); | |
95ffc5e3 KH |
967 | if (dma_mapping_error(orb->page_table_bus)) |
968 | goto fail_page_table; | |
9ba136d0 KH |
969 | orb->request.data_descriptor.high = sd->address_high; |
970 | orb->request.data_descriptor.low = orb->page_table_bus; | |
971 | orb->request.misc |= | |
a77754a7 KH |
972 | COMMAND_ORB_PAGE_TABLE_PRESENT | |
973 | COMMAND_ORB_DATA_SIZE(j); | |
9ba136d0 KH |
974 | |
975 | fw_memcpy_to_be32(orb->page_table, orb->page_table, size); | |
95ffc5e3 KH |
976 | |
977 | return 0; | |
978 | ||
979 | fail_page_table: | |
980 | dma_unmap_sg(device->card->device, sg, orb->cmd->use_sg, | |
981 | orb->cmd->sc_data_direction); | |
982 | fail: | |
983 | return -ENOMEM; | |
9ba136d0 KH |
984 | } |
985 | ||
9ba136d0 KH |
986 | /* SCSI stack integration */ |
987 | ||
988 | static int sbp2_scsi_queuecommand(struct scsi_cmnd *cmd, scsi_done_fn_t done) | |
989 | { | |
ad85274f KH |
990 | struct sbp2_device *sd = |
991 | (struct sbp2_device *)cmd->device->host->hostdata; | |
992 | struct fw_unit *unit = sd->unit; | |
9ba136d0 | 993 | struct fw_device *device = fw_device(unit->device.parent); |
9ba136d0 KH |
994 | struct sbp2_command_orb *orb; |
995 | ||
c781c06d KH |
996 | /* |
997 | * Bidirectional commands are not yet implemented, and unknown | |
998 | * transfer direction not handled. | |
999 | */ | |
9ba136d0 | 1000 | if (cmd->sc_data_direction == DMA_BIDIRECTIONAL) { |
8a8cea27 | 1001 | fw_error("Can't handle DMA_BIDIRECTIONAL, rejecting command\n"); |
e1b68c4d KH |
1002 | cmd->result = DID_ERROR << 16; |
1003 | done(cmd); | |
1004 | return 0; | |
9ba136d0 KH |
1005 | } |
1006 | ||
2d826cc5 | 1007 | orb = kzalloc(sizeof(*orb), GFP_ATOMIC); |
9ba136d0 KH |
1008 | if (orb == NULL) { |
1009 | fw_notify("failed to alloc orb\n"); | |
82eff9db | 1010 | goto fail_alloc; |
9ba136d0 KH |
1011 | } |
1012 | ||
12f26aa1 KH |
1013 | /* Initialize rcode to something not RCODE_COMPLETE. */ |
1014 | orb->base.rcode = -1; | |
9ba136d0 KH |
1015 | orb->base.request_bus = |
1016 | dma_map_single(device->card->device, &orb->request, | |
2d826cc5 | 1017 | sizeof(orb->request), DMA_TO_DEVICE); |
82eff9db KH |
1018 | if (dma_mapping_error(orb->base.request_bus)) |
1019 | goto fail_mapping; | |
9ba136d0 KH |
1020 | |
1021 | orb->unit = unit; | |
1022 | orb->done = done; | |
1023 | orb->cmd = cmd; | |
1024 | ||
1025 | orb->request.next.high = SBP2_ORB_NULL; | |
1026 | orb->request.next.low = 0x0; | |
c781c06d KH |
1027 | /* |
1028 | * At speed 100 we can do 512 bytes per packet, at speed 200, | |
9ba136d0 KH |
1029 | * 1024 bytes per packet etc. The SBP-2 max_payload field |
1030 | * specifies the max payload size as 2 ^ (max_payload + 2), so | |
c781c06d KH |
1031 | * if we set this to max_speed + 7, we get the right value. |
1032 | */ | |
9ba136d0 | 1033 | orb->request.misc = |
f1397490 SR |
1034 | COMMAND_ORB_MAX_PAYLOAD(device->max_speed + 7) | |
1035 | COMMAND_ORB_SPEED(device->max_speed) | | |
a77754a7 | 1036 | COMMAND_ORB_NOTIFY; |
9ba136d0 KH |
1037 | |
1038 | if (cmd->sc_data_direction == DMA_FROM_DEVICE) | |
1039 | orb->request.misc |= | |
a77754a7 | 1040 | COMMAND_ORB_DIRECTION(SBP2_DIRECTION_FROM_MEDIA); |
9ba136d0 KH |
1041 | else if (cmd->sc_data_direction == DMA_TO_DEVICE) |
1042 | orb->request.misc |= | |
a77754a7 | 1043 | COMMAND_ORB_DIRECTION(SBP2_DIRECTION_TO_MEDIA); |
9ba136d0 | 1044 | |
213d7bbd | 1045 | if (cmd->use_sg && sbp2_command_orb_map_scatterlist(orb) < 0) |
95ffc5e3 | 1046 | goto fail_map_payload; |
9ba136d0 | 1047 | |
2d826cc5 | 1048 | fw_memcpy_to_be32(&orb->request, &orb->request, sizeof(orb->request)); |
9ba136d0 KH |
1049 | |
1050 | memset(orb->request.command_block, | |
2d826cc5 | 1051 | 0, sizeof(orb->request.command_block)); |
9ba136d0 KH |
1052 | memcpy(orb->request.command_block, cmd->cmnd, COMMAND_SIZE(*cmd->cmnd)); |
1053 | ||
1054 | orb->base.callback = complete_command_orb; | |
1055 | ||
1056 | sbp2_send_orb(&orb->base, unit, sd->node_id, sd->generation, | |
1057 | sd->command_block_agent_address + SBP2_ORB_POINTER); | |
1058 | ||
1059 | return 0; | |
82eff9db | 1060 | |
95ffc5e3 | 1061 | fail_map_payload: |
82eff9db | 1062 | dma_unmap_single(device->card->device, orb->base.request_bus, |
2d826cc5 | 1063 | sizeof(orb->request), DMA_TO_DEVICE); |
82eff9db KH |
1064 | fail_mapping: |
1065 | kfree(orb); | |
1066 | fail_alloc: | |
e1b68c4d | 1067 | return SCSI_MLQUEUE_HOST_BUSY; |
9ba136d0 KH |
1068 | } |
1069 | ||
cfb01381 SR |
1070 | static int sbp2_scsi_slave_alloc(struct scsi_device *sdev) |
1071 | { | |
ad85274f | 1072 | struct sbp2_device *sd = (struct sbp2_device *)sdev->host->hostdata; |
cfb01381 SR |
1073 | |
1074 | sdev->allow_restart = 1; | |
1075 | ||
1076 | if (sd->workarounds & SBP2_WORKAROUND_INQUIRY_36) | |
1077 | sdev->inquiry_len = 36; | |
1078 | return 0; | |
1079 | } | |
1080 | ||
9ba136d0 KH |
1081 | static int sbp2_scsi_slave_configure(struct scsi_device *sdev) |
1082 | { | |
ad85274f KH |
1083 | struct sbp2_device *sd = (struct sbp2_device *)sdev->host->hostdata; |
1084 | struct fw_unit *unit = sd->unit; | |
9ba136d0 | 1085 | |
cfb01381 SR |
1086 | sdev->use_10_for_rw = 1; |
1087 | ||
1088 | if (sdev->type == TYPE_ROM) | |
1089 | sdev->use_10_for_ms = 1; | |
9ba136d0 KH |
1090 | if (sdev->type == TYPE_DISK && |
1091 | sd->workarounds & SBP2_WORKAROUND_MODE_SENSE_8) | |
1092 | sdev->skip_ms_page_8 = 1; | |
1093 | if (sd->workarounds & SBP2_WORKAROUND_FIX_CAPACITY) { | |
1094 | fw_notify("setting fix_capacity for %s\n", unit->device.bus_id); | |
1095 | sdev->fix_capacity = 1; | |
1096 | } | |
cf47c7a2 SR |
1097 | if (sd->workarounds & SBP2_WORKAROUND_128K_MAX_TRANS) |
1098 | blk_queue_max_sectors(sdev->request_queue, 128 * 1024 / 512); | |
9ba136d0 KH |
1099 | return 0; |
1100 | } | |
1101 | ||
1102 | /* | |
1103 | * Called by scsi stack when something has really gone wrong. Usually | |
1104 | * called when a command has timed-out for some reason. | |
1105 | */ | |
1106 | static int sbp2_scsi_abort(struct scsi_cmnd *cmd) | |
1107 | { | |
ad85274f KH |
1108 | struct sbp2_device *sd = |
1109 | (struct sbp2_device *)cmd->device->host->hostdata; | |
1110 | struct fw_unit *unit = sd->unit; | |
9ba136d0 KH |
1111 | |
1112 | fw_notify("sbp2_scsi_abort\n"); | |
0fc7d6e4 | 1113 | sbp2_agent_reset(unit); |
9ba136d0 KH |
1114 | sbp2_cancel_orbs(unit); |
1115 | ||
1116 | return SUCCESS; | |
1117 | } | |
1118 | ||
14e21986 SR |
1119 | /* |
1120 | * Format of /sys/bus/scsi/devices/.../ieee1394_id: | |
1121 | * u64 EUI-64 : u24 directory_ID : u16 LUN (all printed in hexadecimal) | |
1122 | * | |
1123 | * This is the concatenation of target port identifier and logical unit | |
1124 | * identifier as per SAM-2...SAM-4 annex A. | |
1125 | */ | |
1126 | static ssize_t | |
1127 | sbp2_sysfs_ieee1394_id_show(struct device *dev, struct device_attribute *attr, | |
1128 | char *buf) | |
1129 | { | |
1130 | struct scsi_device *sdev = to_scsi_device(dev); | |
1131 | struct sbp2_device *sd; | |
1132 | struct fw_unit *unit; | |
1133 | struct fw_device *device; | |
1134 | u32 directory_id; | |
1135 | struct fw_csr_iterator ci; | |
1136 | int key, value, lun; | |
1137 | ||
1138 | if (!sdev) | |
1139 | return 0; | |
1140 | sd = (struct sbp2_device *)sdev->host->hostdata; | |
1141 | unit = sd->unit; | |
1142 | device = fw_device(unit->device.parent); | |
1143 | ||
1144 | /* implicit directory ID */ | |
1145 | directory_id = ((unit->directory - device->config_rom) * 4 | |
1146 | + CSR_CONFIG_ROM) & 0xffffff; | |
1147 | ||
1148 | /* explicit directory ID, overrides implicit ID if present */ | |
1149 | fw_csr_iterator_init(&ci, unit->directory); | |
1150 | while (fw_csr_iterator_next(&ci, &key, &value)) | |
1151 | if (key == CSR_DIRECTORY_ID) { | |
1152 | directory_id = value; | |
1153 | break; | |
1154 | } | |
1155 | ||
1156 | /* FIXME: Make this work for multi-lun devices. */ | |
1157 | lun = 0; | |
1158 | ||
1159 | return sprintf(buf, "%08x%08x:%06x:%04x\n", | |
1160 | device->config_rom[3], device->config_rom[4], | |
1161 | directory_id, lun); | |
1162 | } | |
1163 | ||
1164 | static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL); | |
1165 | ||
1166 | static struct device_attribute *sbp2_scsi_sysfs_attrs[] = { | |
1167 | &dev_attr_ieee1394_id, | |
1168 | NULL | |
1169 | }; | |
1170 | ||
9ba136d0 KH |
1171 | static struct scsi_host_template scsi_driver_template = { |
1172 | .module = THIS_MODULE, | |
1173 | .name = "SBP-2 IEEE-1394", | |
1174 | .proc_name = (char *)sbp2_driver_name, | |
1175 | .queuecommand = sbp2_scsi_queuecommand, | |
cfb01381 | 1176 | .slave_alloc = sbp2_scsi_slave_alloc, |
9ba136d0 KH |
1177 | .slave_configure = sbp2_scsi_slave_configure, |
1178 | .eh_abort_handler = sbp2_scsi_abort, | |
1179 | .this_id = -1, | |
1180 | .sg_tablesize = SG_ALL, | |
1181 | .use_clustering = ENABLE_CLUSTERING, | |
02af8e70 SR |
1182 | .cmd_per_lun = 1, |
1183 | .can_queue = 1, | |
14e21986 | 1184 | .sdev_attrs = sbp2_scsi_sysfs_attrs, |
9ba136d0 KH |
1185 | }; |
1186 | ||
9ba136d0 KH |
1187 | MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>"); |
1188 | MODULE_DESCRIPTION("SCSI over IEEE1394"); | |
1189 | MODULE_LICENSE("GPL"); | |
1190 | MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table); | |
1191 | ||
1e4c7b0d OH |
1192 | /* Provide a module alias so root-on-sbp2 initrds don't break. */ |
1193 | #ifndef CONFIG_IEEE1394_SBP2_MODULE | |
1194 | MODULE_ALIAS("sbp2"); | |
1195 | #endif | |
1196 | ||
9ba136d0 KH |
1197 | static int __init sbp2_init(void) |
1198 | { | |
1199 | return driver_register(&sbp2_driver.driver); | |
1200 | } | |
1201 | ||
1202 | static void __exit sbp2_cleanup(void) | |
1203 | { | |
1204 | driver_unregister(&sbp2_driver.driver); | |
1205 | } | |
1206 | ||
1207 | module_init(sbp2_init); | |
1208 | module_exit(sbp2_cleanup); |