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