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