fw-sbp2.c 38 KB

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