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