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