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