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