fw-sbp2.c 34 KB

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