35 #include <linux/device.h>
41 #include <linux/kernel.h>
43 #include <linux/list.h>
45 #include <linux/module.h>
48 #include <linux/slab.h>
50 #include <linux/string.h>
54 #include <asm/byteorder.h>
56 #include <scsi/scsi.h>
68 static bool sbp2_param_exclusive_login = 1;
71 "(default = Y, use N for concurrent initiators)");
105 #define SBP2_WORKAROUND_128K_MAX_TRANS 0x1
106 #define SBP2_WORKAROUND_INQUIRY_36 0x2
107 #define SBP2_WORKAROUND_MODE_SENSE_8 0x4
108 #define SBP2_WORKAROUND_FIX_CAPACITY 0x8
109 #define SBP2_WORKAROUND_DELAY_INQUIRY 0x10
110 #define SBP2_INQUIRY_DELAY 12
111 #define SBP2_WORKAROUND_POWER_CONDITION 0x20
112 #define SBP2_WORKAROUND_OVERRIDE 0x100
114 static int sbp2_param_workarounds;
122 ", set power condition in start stop unit = "
125 ", or a combination)");
182 return fw_parent_device(tgt->
unit);
187 return &tgt->
unit->device;
192 return &lu->
tgt->unit->device;
196 #define INVALID_LOGIN_ID 0x10000
198 #define SBP2_ORB_TIMEOUT 2000U
199 #define SBP2_ORB_NULL 0x80000000
200 #define SBP2_RETRY_LIMIT 0xf
201 #define SBP2_CYCLE_LIMIT (0xc8 << 12)
207 #define SBP2_MAX_CDB_SIZE 16
213 #define SBP2_MAX_SEG_SIZE 0xfffc
216 #define SBP2_CSR_UNIT_CHARACTERISTICS 0x3a
217 #define SBP2_CSR_FIRMWARE_REVISION 0x3c
218 #define SBP2_CSR_LOGICAL_UNIT_NUMBER 0x14
219 #define SBP2_CSR_UNIT_UNIQUE_ID 0x8d
220 #define SBP2_CSR_LOGICAL_UNIT_DIRECTORY 0xd4
223 #define SBP2_LOGIN_REQUEST 0x0
224 #define SBP2_QUERY_LOGINS_REQUEST 0x1
225 #define SBP2_RECONNECT_REQUEST 0x3
226 #define SBP2_SET_PASSWORD_REQUEST 0x4
227 #define SBP2_LOGOUT_REQUEST 0x7
228 #define SBP2_ABORT_TASK_REQUEST 0xb
229 #define SBP2_ABORT_TASK_SET 0xc
230 #define SBP2_LOGICAL_UNIT_RESET 0xe
231 #define SBP2_TARGET_RESET_REQUEST 0xf
234 #define SBP2_AGENT_STATE 0x00
235 #define SBP2_AGENT_RESET 0x04
236 #define SBP2_ORB_POINTER 0x08
237 #define SBP2_DOORBELL 0x10
238 #define SBP2_UNSOLICITED_STATUS_ENABLE 0x14
241 #define SBP2_STATUS_REQUEST_COMPLETE 0x0
242 #define SBP2_STATUS_TRANSPORT_FAILURE 0x1
243 #define SBP2_STATUS_ILLEGAL_REQUEST 0x2
244 #define SBP2_STATUS_VENDOR_DEPENDENT 0x3
246 #define STATUS_GET_ORB_HIGH(v) ((v).status & 0xffff)
247 #define STATUS_GET_SBP_STATUS(v) (((v).status >> 16) & 0xff)
248 #define STATUS_GET_LEN(v) (((v).status >> 24) & 0x07)
249 #define STATUS_GET_DEAD(v) (((v).status >> 27) & 0x01)
250 #define STATUS_GET_RESPONSE(v) (((v).status >> 28) & 0x03)
251 #define STATUS_GET_SOURCE(v) (((v).status >> 30) & 0x03)
252 #define STATUS_GET_ORB_LOW(v) ((v).orb_low)
253 #define STATUS_GET_DATA(v) ((v).data)
275 #define MANAGEMENT_ORB_LUN(v) ((v))
276 #define MANAGEMENT_ORB_FUNCTION(v) ((v) << 16)
277 #define MANAGEMENT_ORB_RECONNECT(v) ((v) << 20)
278 #define MANAGEMENT_ORB_EXCLUSIVE(v) ((v) ? 1 << 28 : 0)
279 #define MANAGEMENT_ORB_REQUEST_FORMAT(v) ((v) << 29)
280 #define MANAGEMENT_ORB_NOTIFY ((1) << 31)
282 #define MANAGEMENT_ORB_RESPONSE_LENGTH(v) ((v))
283 #define MANAGEMENT_ORB_PASSWORD_LENGTH(v) ((v) << 16)
305 #define COMMAND_ORB_DATA_SIZE(v) ((v))
306 #define COMMAND_ORB_PAGE_SIZE(v) ((v) << 16)
307 #define COMMAND_ORB_PAGE_TABLE_PRESENT ((1) << 19)
308 #define COMMAND_ORB_MAX_PAYLOAD(v) ((v) << 20)
309 #define COMMAND_ORB_SPEED(v) ((v) << 24)
310 #define COMMAND_ORB_DIRECTION ((1) << 27)
311 #define COMMAND_ORB_REQUEST_FORMAT(v) ((v) << 29)
312 #define COMMAND_ORB_NOTIFY ((1) << 31)
329 #define SBP2_ROM_VALUE_WILDCARD ~0
330 #define SBP2_ROM_VALUE_MISSING 0xff000000
340 static const struct {
344 } sbp2_workarounds_table[] = {
346 .firmware_revision = 0x002800,
353 .firmware_revision = 0x002800,
358 .firmware_revision = 0x000200,
363 .firmware_revision = 0x012800,
368 .firmware_revision = 0xa0b800,
373 .firmware_revision = 0x002600,
382 .firmware_revision = 0x0a2700,
388 .firmware_revision = 0x0a2700,
393 .firmware_revision = 0x0a2700,
398 .firmware_revision = 0x0a2700,
403 .firmware_revision = 0x0a2700,
409 static void free_orb(
struct kref *
kref)
417 int tcode,
int destination,
int source,
427 length < 8 || length >
sizeof(
status)) {
439 dev_notice(lu_dev(lu),
440 "non-ORB related status write, not handled\n");
455 spin_unlock_irqrestore(&card->
lock, flags);
459 kref_put(&orb->
kref, free_orb);
461 dev_err(lu_dev(lu),
"status write for unknown ORB\n");
467 static void complete_transaction(
struct fw_card *card,
int rcode,
468 void *payload,
size_t length,
void *
data)
484 if (orb->
rcode == -1)
488 spin_unlock_irqrestore(&card->
lock, flags);
491 kref_put(&orb->
kref, free_orb);
493 spin_unlock_irqrestore(&card->
lock, flags);
496 kref_put(&orb->
kref, free_orb);
506 orb_pointer.high = 0;
511 spin_unlock_irqrestore(&device->
card->lock, flags);
513 kref_get(&orb->
kref);
514 kref_get(&orb->
kref);
517 node_id, generation, device->
max_speed, offset,
518 &orb_pointer, 8, complete_transaction, orb);
529 INIT_LIST_HEAD(&
list);
532 spin_unlock_irqrestore(&device->
card->lock, flags);
541 kref_put(&orb->
kref, free_orb);
547 static void complete_management_orb(
struct sbp2_orb *base_orb,
559 int generation,
int function,
560 int lun_or_login_id,
void *
response)
562 struct fw_device *device = target_parent_device(lu->
tgt);
570 orb = kzalloc(
sizeof(*orb),
GFP_NOIO);
574 kref_init(&orb->
base.kref);
579 goto fail_mapping_response;
581 orb->
request.response.high = 0;
591 orb->
request.status_fifo.high =
601 timeout = lu->
tgt->mgt_orb_timeout;
606 init_completion(&orb->
done);
607 orb->
base.callback = complete_management_orb;
609 orb->
base.request_bus =
613 goto fail_mapping_request;
615 sbp2_send_orb(&orb->
base, lu, node_id, generation,
616 lu->
tgt->management_agent_address);
621 if (sbp2_cancel_orbs(lu) == 0) {
622 dev_err(lu_dev(lu),
"ORB reply timed out, rcode 0x%02x\n",
628 dev_err(lu_dev(lu),
"management write failed, rcode 0x%02x\n",
635 dev_err(lu_dev(lu),
"error status: %d:%d\n",
645 fail_mapping_request:
648 fail_mapping_response:
651 kref_put(&orb->
base.kref, free_orb);
658 struct fw_device *device = target_parent_device(lu->
tgt);
667 static void complete_agent_reset_write_no_wait(
struct fw_card *card,
668 int rcode,
void *payload,
size_t length,
void *data)
675 struct fw_device *device = target_parent_device(lu->
tgt);
686 &d, 4, complete_agent_reset_write_no_wait, t);
698 --lu->
tgt->dont_block;
714 struct fw_card *card = target_parent_device(tgt)->card;
726 spin_unlock_irqrestore(&card->
lock, flags);
738 struct fw_card *card = target_parent_device(tgt)->card;
742 bool unblock =
false;
749 spin_unlock_irqrestore(&card->
lock, flags);
763 struct fw_card *card = target_parent_device(tgt)->card;
770 spin_unlock_irqrestore(&card->
lock, flags);
775 static int sbp2_lun2int(
u16 lun)
779 memset(&eight_bytes_lun, 0,
sizeof(eight_bytes_lun));
780 eight_bytes_lun.scsi_lun[0] = (lun >> 8) & 0xff;
781 eight_bytes_lun.scsi_lun[1] = lun & 0xff;
804 struct fw_device *device = target_parent_device(lu->
tgt);
819 struct fw_device *device = target_parent_device(tgt);
825 if (fw_device_is_shutdown(device))
831 local_node_id = device->
card->node_id;
835 sbp2_send_management_orb(lu, device->
node_id, generation,
838 if (sbp2_send_management_orb(lu, node_id, generation,
843 dev_err(tgt_dev(tgt),
"failed to login to LUN %04x\n",
846 sbp2_unblock(lu->
tgt);
858 << 32) |
be32_to_cpu(response.command_block_agent.low);
861 dev_notice(tgt_dev(tgt),
"logged in to LUN %04x (%d retries)\n",
865 sbp2_set_busy_timeout(lu);
868 sbp2_agent_reset(lu);
872 sbp2_cancel_orbs(lu);
873 sbp2_conditionally_unblock(lu);
893 goto out_logout_login;
897 if (generation != device->
card->generation) {
900 goto out_logout_login;
906 sbp2_allow_block(lu);
915 sbp2_send_management_orb(lu, device->
node_id, generation,
924 static void sbp2_reconnect(
struct work_struct *work)
929 struct fw_device *device = target_parent_device(tgt);
932 if (fw_device_is_shutdown(device))
938 local_node_id = device->
card->node_id;
940 if (sbp2_send_management_orb(lu, node_id, generation,
951 if (generation == device->
card->generation ||
953 dev_err(tgt_dev(tgt),
"failed to reconnect\n");
967 dev_notice(tgt_dev(tgt),
"reconnected to LUN %04x (%d retries)\n",
970 sbp2_agent_reset(lu);
971 sbp2_cancel_orbs(lu);
972 sbp2_conditionally_unblock(lu);
975 static int sbp2_add_logical_unit(
struct sbp2_target *tgt,
int lun_entry)
994 lu->
lun = lun_entry & 0xffff;
1007 static void sbp2_get_unit_unique_id(
struct sbp2_target *tgt,
1010 if ((leaf[0] & 0xffff0000) == 0x00020000)
1011 tgt->
guid = (
u64)leaf[1] << 32 | leaf[2];
1014 static int sbp2_scan_logical_unit_dir(
struct sbp2_target *tgt,
1015 const u32 *directory)
1023 sbp2_add_logical_unit(tgt, value) < 0)
1028 static int sbp2_scan_unit_dir(
struct sbp2_target *tgt,
const u32 *directory,
1052 *firmware_revision =
value;
1061 if (sbp2_add_logical_unit(tgt, value) < 0)
1066 sbp2_get_unit_unique_id(tgt, ci.p - 1 + value);
1071 if (sbp2_scan_logical_unit_dir(tgt, ci.p - 1 + value) < 0)
1084 static void sbp2_clamp_management_orb_timeout(
struct sbp2_target *tgt)
1088 if (timeout > 40000)
1089 dev_notice(tgt_dev(tgt),
"%ds mgt_ORB_timeout limited to 40s\n",
1095 static void sbp2_init_workarounds(
struct sbp2_target *tgt,
u32 model,
1096 u32 firmware_revision)
1099 unsigned int w = sbp2_param_workarounds;
1102 dev_notice(tgt_dev(tgt),
1104 "if you need the workarounds parameter\n");
1109 for (i = 0; i <
ARRAY_SIZE(sbp2_workarounds_table); i++) {
1111 if (sbp2_workarounds_table[i].firmware_revision !=
1112 (firmware_revision & 0xffffff00))
1115 if (sbp2_workarounds_table[i].model != model &&
1119 w |= sbp2_workarounds_table[
i].workarounds;
1124 dev_notice(tgt_dev(tgt),
"workarounds 0x%x "
1125 "(firmware_revision 0x%06x, model_id 0x%06x)\n",
1126 w, firmware_revision, model);
1131 static int sbp2_remove(
struct device *
dev);
1133 static int sbp2_probe(
struct device *
dev)
1136 struct fw_device *device = fw_parent_device(unit);
1147 BUG_ON(dma_set_max_seg_size(device->
card->device,
1157 INIT_LIST_HEAD(&tgt->
lu_list);
1161 goto fail_shost_put;
1166 device->
card->device) < 0)
1167 goto fail_shost_put;
1176 if (sbp2_scan_unit_dir(tgt, unit->
directory, &model,
1177 &firmware_revision) < 0)
1180 sbp2_clamp_management_orb_timeout(tgt);
1181 sbp2_init_workarounds(tgt, model, firmware_revision);
1190 device->
card->max_receive - 1);
1219 sbp2_conditionally_block(lu);
1221 sbp2_queue_work(lu, 0);
1225 static int sbp2_remove(
struct device *dev)
1228 struct fw_device *device = fw_parent_device(unit);
1255 sbp2_send_management_orb(lu, node_id, generation,
1264 dev_notice(dev,
"released target %d:0:0\n", shost->
host_no);
1270 #define SBP2_UNIT_SPEC_ID_ENTRY 0x0000609e
1271 #define SBP2_SW_VERSION_ENTRY 0x00010483
1286 .name = KBUILD_MODNAME,
1288 .probe = sbp2_probe,
1289 .remove = sbp2_remove,
1291 .update = sbp2_update,
1292 .id_table = sbp2_id_table,
1295 static void sbp2_unmap_scatterlist(
struct device *card_device,
1308 int sfmt = (sbp2_status[0] >> 6) & 0x03;
1310 if (sfmt == 2 || sfmt == 3) {
1318 sense_data[0] = 0x70 | sfmt | (sbp2_status[1] & 0x80);
1319 sense_data[1] = 0x0;
1320 sense_data[2] = ((sbp2_status[1] << 1) & 0xe0) | (sbp2_status[1] & 0x0f);
1321 sense_data[3] = sbp2_status[4];
1322 sense_data[4] = sbp2_status[5];
1323 sense_data[5] = sbp2_status[6];
1324 sense_data[6] = sbp2_status[7];
1326 sense_data[8] = sbp2_status[8];
1327 sense_data[9] = sbp2_status[9];
1328 sense_data[10] = sbp2_status[10];
1329 sense_data[11] = sbp2_status[11];
1330 sense_data[12] = sbp2_status[2];
1331 sense_data[13] = sbp2_status[3];
1332 sense_data[14] = sbp2_status[12];
1333 sense_data[15] = sbp2_status[13];
1335 sam_status = sbp2_status[0] & 0x3f;
1337 switch (sam_status) {
1344 return DID_OK << 16 | sam_status;
1351 static void complete_command_orb(
struct sbp2_orb *base_orb,
1352 struct sbp2_status *status)
1356 struct fw_device *device = target_parent_device(orb->
lu->tgt);
1359 if (status !=
NULL) {
1361 sbp2_agent_reset_no_wait(orb->
lu);
1379 orb->
cmd->sense_buffer);
1387 sbp2_conditionally_block(orb->
lu);
1392 sbp2_unmap_scatterlist(device->
card->device, orb);
1395 orb->
cmd->scsi_done(orb->
cmd);
1416 orb->
request.data_descriptor.high =
1418 orb->
request.data_descriptor.low =
1434 goto fail_page_table;
1458 static int sbp2_scsi_queuecommand(
struct Scsi_Host *shost,
1462 struct fw_device *device = target_parent_device(lu->
tgt);
1471 dev_err(lu_dev(lu),
"cannot handle bidirectional command\n");
1479 dev_notice(lu_dev(lu),
"failed to alloc ORB\n");
1484 orb->
base.rcode = -1;
1485 kref_init(&orb->
base.kref);
1500 if (scsi_sg_count(cmd) && sbp2_map_scatterlist(orb, device, lu) < 0)
1505 orb->
base.callback = complete_command_orb;
1506 orb->
base.request_bus =
1510 sbp2_unmap_scatterlist(device->
card->device, orb);
1514 sbp2_send_orb(&orb->
base, lu, lu->
tgt->node_id, generation,
1518 kref_put(&orb->
base.kref, free_orb);
1522 static int sbp2_scsi_slave_alloc(
struct scsi_device *sdev)
1544 static int sbp2_scsi_slave_configure(
struct scsi_device *sdev)
1552 if (sbp2_param_exclusive_login)
1578 static int sbp2_scsi_abort(
struct scsi_cmnd *cmd)
1582 dev_notice(lu_dev(lu),
"sbp2_scsi_abort\n");
1583 sbp2_agent_reset(lu);
1584 sbp2_cancel_orbs(lu);
1596 static ssize_t sbp2_sysfs_ieee1394_id_show(
struct device *dev,
1607 return sprintf(buf,
"%016llx:%06x:%04x\n",
1608 (
unsigned long long)lu->
tgt->guid,
1609 lu->
tgt->directory_id, lu->
lun);
1615 &dev_attr_ieee1394_id,
1621 .name =
"SBP-2 IEEE-1394",
1622 .proc_name =
"sbp2",
1623 .queuecommand = sbp2_scsi_queuecommand,
1624 .slave_alloc = sbp2_scsi_slave_alloc,
1625 .slave_configure = sbp2_scsi_slave_configure,
1626 .eh_abort_handler = sbp2_scsi_abort,
1632 .sdev_attrs = sbp2_scsi_sysfs_attrs,
1641 #ifndef CONFIG_IEEE1394_SBP2_MODULE
1645 static int __init sbp2_init(
void)
1650 static void __exit sbp2_cleanup(
void)