sbp2.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * SBP2 driver (SCSI over IEEE1394)
  4. *
  5. * Copyright (C) 2005-2007 Kristian Hoegsberg <krh@bitplanet.net>
  6. */
  7. /*
  8. * The basic structure of this driver is based on the old storage driver,
  9. * drivers/ieee1394/sbp2.c, originally written by
  10. * James Goodwin <jamesg@filanet.com>
  11. * with later contributions and ongoing maintenance from
  12. * Ben Collins <bcollins@debian.org>,
  13. * Stefan Richter <stefanr@s5r6.in-berlin.de>
  14. * and many others.
  15. */
  16. #include <linux/blkdev.h>
  17. #include <linux/bug.h>
  18. #include <linux/completion.h>
  19. #include <linux/delay.h>
  20. #include <linux/device.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/firewire.h>
  23. #include <linux/firewire-constants.h>
  24. #include <linux/init.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/kernel.h>
  27. #include <linux/kref.h>
  28. #include <linux/list.h>
  29. #include <linux/mod_devicetable.h>
  30. #include <linux/module.h>
  31. #include <linux/moduleparam.h>
  32. #include <linux/scatterlist.h>
  33. #include <linux/slab.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/string.h>
  36. #include <linux/stringify.h>
  37. #include <linux/workqueue.h>
  38. #include <asm/byteorder.h>
  39. #include <scsi/scsi.h>
  40. #include <scsi/scsi_cmnd.h>
  41. #include <scsi/scsi_device.h>
  42. #include <scsi/scsi_host.h>
  43. /*
  44. * So far only bridges from Oxford Semiconductor are known to support
  45. * concurrent logins. Depending on firmware, four or two concurrent logins
  46. * are possible on OXFW911 and newer Oxsemi bridges.
  47. *
  48. * Concurrent logins are useful together with cluster filesystems.
  49. */
  50. static bool sbp2_param_exclusive_login = 1;
  51. module_param_named(exclusive_login, sbp2_param_exclusive_login, bool, 0644);
  52. MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device "
  53. "(default = Y, use N for concurrent initiators)");
  54. /*
  55. * Flags for firmware oddities
  56. *
  57. * - 128kB max transfer
  58. * Limit transfer size. Necessary for some old bridges.
  59. *
  60. * - 36 byte inquiry
  61. * When scsi_mod probes the device, let the inquiry command look like that
  62. * from MS Windows.
  63. *
  64. * - skip mode page 8
  65. * Suppress sending of mode_sense for mode page 8 if the device pretends to
  66. * support the SCSI Primary Block commands instead of Reduced Block Commands.
  67. *
  68. * - fix capacity
  69. * Tell sd_mod to correct the last sector number reported by read_capacity.
  70. * Avoids access beyond actual disk limits on devices with an off-by-one bug.
  71. * Don't use this with devices which don't have this bug.
  72. *
  73. * - delay inquiry
  74. * Wait extra SBP2_INQUIRY_DELAY seconds after login before SCSI inquiry.
  75. *
  76. * - power condition
  77. * Set the power condition field in the START STOP UNIT commands sent by
  78. * sd_mod on suspend, resume, and shutdown (if manage_system_start_stop or
  79. * manage_runtime_start_stop is on).
  80. * Some disks need this to spin down or to resume properly.
  81. *
  82. * - override internal blacklist
  83. * Instead of adding to the built-in blacklist, use only the workarounds
  84. * specified in the module load parameter.
  85. * Useful if a blacklist entry interfered with a non-broken device.
  86. */
  87. #define SBP2_WORKAROUND_128K_MAX_TRANS 0x1
  88. #define SBP2_WORKAROUND_INQUIRY_36 0x2
  89. #define SBP2_WORKAROUND_MODE_SENSE_8 0x4
  90. #define SBP2_WORKAROUND_FIX_CAPACITY 0x8
  91. #define SBP2_WORKAROUND_DELAY_INQUIRY 0x10
  92. #define SBP2_INQUIRY_DELAY 12
  93. #define SBP2_WORKAROUND_POWER_CONDITION 0x20
  94. #define SBP2_WORKAROUND_OVERRIDE 0x100
  95. static int sbp2_param_workarounds;
  96. module_param_named(workarounds, sbp2_param_workarounds, int, 0644);
  97. MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
  98. ", 128kB max transfer = " __stringify(SBP2_WORKAROUND_128K_MAX_TRANS)
  99. ", 36 byte inquiry = " __stringify(SBP2_WORKAROUND_INQUIRY_36)
  100. ", skip mode page 8 = " __stringify(SBP2_WORKAROUND_MODE_SENSE_8)
  101. ", fix capacity = " __stringify(SBP2_WORKAROUND_FIX_CAPACITY)
  102. ", delay inquiry = " __stringify(SBP2_WORKAROUND_DELAY_INQUIRY)
  103. ", set power condition in start stop unit = "
  104. __stringify(SBP2_WORKAROUND_POWER_CONDITION)
  105. ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
  106. ", or a combination)");
  107. /*
  108. * We create one struct sbp2_logical_unit per SBP-2 Logical Unit Number Entry
  109. * and one struct scsi_device per sbp2_logical_unit.
  110. */
  111. struct sbp2_logical_unit {
  112. struct sbp2_target *tgt;
  113. struct list_head link;
  114. struct fw_address_handler address_handler;
  115. struct list_head orb_list;
  116. u64 command_block_agent_address;
  117. u16 lun;
  118. int login_id;
  119. /*
  120. * The generation is updated once we've logged in or reconnected
  121. * to the logical unit. Thus, I/O to the device will automatically
  122. * fail and get retried if it happens in a window where the device
  123. * is not ready, e.g. after a bus reset but before we reconnect.
  124. */
  125. int generation;
  126. int retries;
  127. work_func_t workfn;
  128. struct delayed_work work;
  129. bool has_sdev;
  130. bool blocked;
  131. };
  132. static void sbp2_queue_work(struct sbp2_logical_unit *lu, unsigned long delay)
  133. {
  134. queue_delayed_work(fw_workqueue, &lu->work, delay);
  135. }
  136. /*
  137. * We create one struct sbp2_target per IEEE 1212 Unit Directory
  138. * and one struct Scsi_Host per sbp2_target.
  139. */
  140. struct sbp2_target {
  141. struct fw_unit *unit;
  142. struct list_head lu_list;
  143. u64 management_agent_address;
  144. u64 guid;
  145. int directory_id;
  146. int node_id;
  147. int address_high;
  148. unsigned int workarounds;
  149. unsigned int mgt_orb_timeout;
  150. unsigned int max_payload;
  151. spinlock_t lock;
  152. int dont_block; /* counter for each logical unit */
  153. int blocked; /* ditto */
  154. };
  155. static struct fw_device *target_parent_device(struct sbp2_target *tgt)
  156. {
  157. return fw_parent_device(tgt->unit);
  158. }
  159. static const struct device *tgt_dev(const struct sbp2_target *tgt)
  160. {
  161. return &tgt->unit->device;
  162. }
  163. static const struct device *lu_dev(const struct sbp2_logical_unit *lu)
  164. {
  165. return &lu->tgt->unit->device;
  166. }
  167. /* Impossible login_id, to detect logout attempt before successful login */
  168. #define INVALID_LOGIN_ID 0x10000
  169. #define SBP2_ORB_TIMEOUT 2000U /* Timeout in ms */
  170. #define SBP2_ORB_NULL 0x80000000
  171. #define SBP2_RETRY_LIMIT 0xf /* 15 retries */
  172. #define SBP2_CYCLE_LIMIT (0xc8 << 12) /* 200 125us cycles */
  173. /*
  174. * There is no transport protocol limit to the CDB length, but we implement
  175. * a fixed length only. 16 bytes is enough for disks larger than 2 TB.
  176. */
  177. #define SBP2_MAX_CDB_SIZE 16
  178. /*
  179. * The maximum SBP-2 data buffer size is 0xffff. We quadlet-align this
  180. * for compatibility with earlier versions of this driver.
  181. */
  182. #define SBP2_MAX_SEG_SIZE 0xfffc
  183. /* Unit directory keys */
  184. #define SBP2_CSR_UNIT_CHARACTERISTICS 0x3a
  185. #define SBP2_CSR_FIRMWARE_REVISION 0x3c
  186. #define SBP2_CSR_LOGICAL_UNIT_NUMBER 0x14
  187. #define SBP2_CSR_UNIT_UNIQUE_ID 0x8d
  188. #define SBP2_CSR_LOGICAL_UNIT_DIRECTORY 0xd4
  189. /* Management orb opcodes */
  190. #define SBP2_LOGIN_REQUEST 0x0
  191. #define SBP2_QUERY_LOGINS_REQUEST 0x1
  192. #define SBP2_RECONNECT_REQUEST 0x3
  193. #define SBP2_SET_PASSWORD_REQUEST 0x4
  194. #define SBP2_LOGOUT_REQUEST 0x7
  195. #define SBP2_ABORT_TASK_REQUEST 0xb
  196. #define SBP2_ABORT_TASK_SET 0xc
  197. #define SBP2_LOGICAL_UNIT_RESET 0xe
  198. #define SBP2_TARGET_RESET_REQUEST 0xf
  199. /* Offsets for command block agent registers */
  200. #define SBP2_AGENT_STATE 0x00
  201. #define SBP2_AGENT_RESET 0x04
  202. #define SBP2_ORB_POINTER 0x08
  203. #define SBP2_DOORBELL 0x10
  204. #define SBP2_UNSOLICITED_STATUS_ENABLE 0x14
  205. /* Status write response codes */
  206. #define SBP2_STATUS_REQUEST_COMPLETE 0x0
  207. #define SBP2_STATUS_TRANSPORT_FAILURE 0x1
  208. #define SBP2_STATUS_ILLEGAL_REQUEST 0x2
  209. #define SBP2_STATUS_VENDOR_DEPENDENT 0x3
  210. #define STATUS_GET_ORB_HIGH(v) ((v).status & 0xffff)
  211. #define STATUS_GET_SBP_STATUS(v) (((v).status >> 16) & 0xff)
  212. #define STATUS_GET_LEN(v) (((v).status >> 24) & 0x07)
  213. #define STATUS_GET_DEAD(v) (((v).status >> 27) & 0x01)
  214. #define STATUS_GET_RESPONSE(v) (((v).status >> 28) & 0x03)
  215. #define STATUS_GET_SOURCE(v) (((v).status >> 30) & 0x03)
  216. #define STATUS_GET_ORB_LOW(v) ((v).orb_low)
  217. #define STATUS_GET_DATA(v) ((v).data)
  218. struct sbp2_status {
  219. u32 status;
  220. u32 orb_low;
  221. u8 data[24];
  222. };
  223. struct sbp2_pointer {
  224. __be32 high;
  225. __be32 low;
  226. };
  227. struct sbp2_orb {
  228. struct fw_transaction t;
  229. struct kref kref;
  230. dma_addr_t request_bus;
  231. int rcode;
  232. void (*callback)(struct sbp2_orb * orb, struct sbp2_status * status);
  233. struct sbp2_logical_unit *lu;
  234. struct list_head link;
  235. };
  236. #define MANAGEMENT_ORB_LUN(v) ((v))
  237. #define MANAGEMENT_ORB_FUNCTION(v) ((v) << 16)
  238. #define MANAGEMENT_ORB_RECONNECT(v) ((v) << 20)
  239. #define MANAGEMENT_ORB_EXCLUSIVE(v) ((v) ? 1 << 28 : 0)
  240. #define MANAGEMENT_ORB_REQUEST_FORMAT(v) ((v) << 29)
  241. #define MANAGEMENT_ORB_NOTIFY ((1) << 31)
  242. #define MANAGEMENT_ORB_RESPONSE_LENGTH(v) ((v))
  243. #define MANAGEMENT_ORB_PASSWORD_LENGTH(v) ((v) << 16)
  244. struct sbp2_management_orb {
  245. struct sbp2_orb base;
  246. struct {
  247. struct sbp2_pointer password;
  248. struct sbp2_pointer response;
  249. __be32 misc;
  250. __be32 length;
  251. struct sbp2_pointer status_fifo;
  252. } request;
  253. __be32 response[4];
  254. dma_addr_t response_bus;
  255. struct completion done;
  256. struct sbp2_status status;
  257. };
  258. struct sbp2_login_response {
  259. __be32 misc;
  260. struct sbp2_pointer command_block_agent;
  261. __be32 reconnect_hold;
  262. };
  263. #define COMMAND_ORB_DATA_SIZE(v) ((v))
  264. #define COMMAND_ORB_PAGE_SIZE(v) ((v) << 16)
  265. #define COMMAND_ORB_PAGE_TABLE_PRESENT ((1) << 19)
  266. #define COMMAND_ORB_MAX_PAYLOAD(v) ((v) << 20)
  267. #define COMMAND_ORB_SPEED(v) ((v) << 24)
  268. #define COMMAND_ORB_DIRECTION ((1) << 27)
  269. #define COMMAND_ORB_REQUEST_FORMAT(v) ((v) << 29)
  270. #define COMMAND_ORB_NOTIFY ((1) << 31)
  271. struct sbp2_command_orb {
  272. struct sbp2_orb base;
  273. struct {
  274. struct sbp2_pointer next;
  275. struct sbp2_pointer data_descriptor;
  276. __be32 misc;
  277. u8 command_block[SBP2_MAX_CDB_SIZE];
  278. } request;
  279. struct scsi_cmnd *cmd;
  280. struct sbp2_pointer page_table[SG_ALL] __attribute__((aligned(8)));
  281. dma_addr_t page_table_bus;
  282. };
  283. #define SBP2_ROM_VALUE_WILDCARD ~0 /* match all */
  284. #define SBP2_ROM_VALUE_MISSING 0xff000000 /* not present in the unit dir. */
  285. /*
  286. * List of devices with known bugs.
  287. *
  288. * The firmware_revision field, masked with 0xffff00, is the best
  289. * indicator for the type of bridge chip of a device. It yields a few
  290. * false positives but this did not break correctly behaving devices
  291. * so far.
  292. */
  293. static const struct {
  294. u32 firmware_revision;
  295. u32 model;
  296. unsigned int workarounds;
  297. } sbp2_workarounds_table[] = {
  298. /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
  299. .firmware_revision = 0x002800,
  300. .model = 0x001010,
  301. .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
  302. SBP2_WORKAROUND_MODE_SENSE_8 |
  303. SBP2_WORKAROUND_POWER_CONDITION,
  304. },
  305. /* DViCO Momobay FX-3A with TSB42AA9A bridge */ {
  306. .firmware_revision = 0x002800,
  307. .model = 0x000000,
  308. .workarounds = SBP2_WORKAROUND_POWER_CONDITION,
  309. },
  310. /* Initio bridges, actually only needed for some older ones */ {
  311. .firmware_revision = 0x000200,
  312. .model = SBP2_ROM_VALUE_WILDCARD,
  313. .workarounds = SBP2_WORKAROUND_INQUIRY_36,
  314. },
  315. /* PL-3507 bridge with Prolific firmware */ {
  316. .firmware_revision = 0x012800,
  317. .model = SBP2_ROM_VALUE_WILDCARD,
  318. .workarounds = SBP2_WORKAROUND_POWER_CONDITION,
  319. },
  320. /* Symbios bridge */ {
  321. .firmware_revision = 0xa0b800,
  322. .model = SBP2_ROM_VALUE_WILDCARD,
  323. .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
  324. },
  325. /* Datafab MD2-FW2 with Symbios/LSILogic SYM13FW500 bridge */ {
  326. .firmware_revision = 0x002600,
  327. .model = SBP2_ROM_VALUE_WILDCARD,
  328. .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
  329. },
  330. /*
  331. * iPod 2nd generation: needs 128k max transfer size workaround
  332. * iPod 3rd generation: needs fix capacity workaround
  333. */
  334. {
  335. .firmware_revision = 0x0a2700,
  336. .model = 0x000000,
  337. .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS |
  338. SBP2_WORKAROUND_FIX_CAPACITY,
  339. },
  340. /* iPod 4th generation */ {
  341. .firmware_revision = 0x0a2700,
  342. .model = 0x000021,
  343. .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
  344. },
  345. /* iPod mini */ {
  346. .firmware_revision = 0x0a2700,
  347. .model = 0x000022,
  348. .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
  349. },
  350. /* iPod mini */ {
  351. .firmware_revision = 0x0a2700,
  352. .model = 0x000023,
  353. .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
  354. },
  355. /* iPod Photo */ {
  356. .firmware_revision = 0x0a2700,
  357. .model = 0x00007e,
  358. .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
  359. }
  360. };
  361. static void free_orb(struct kref *kref)
  362. {
  363. struct sbp2_orb *orb = container_of(kref, struct sbp2_orb, kref);
  364. kfree(orb);
  365. }
  366. static void sbp2_status_write(struct fw_card *card, struct fw_request *request,
  367. int tcode, int destination, int source,
  368. int generation, unsigned long long offset,
  369. void *payload, size_t length, void *callback_data)
  370. {
  371. struct sbp2_logical_unit *lu = callback_data;
  372. struct sbp2_orb *orb = NULL, *iter;
  373. struct sbp2_status status;
  374. unsigned long flags;
  375. if (tcode != TCODE_WRITE_BLOCK_REQUEST ||
  376. length < 8 || length > sizeof(status)) {
  377. fw_send_response(card, request, RCODE_TYPE_ERROR);
  378. return;
  379. }
  380. status.status = be32_to_cpup(payload);
  381. status.orb_low = be32_to_cpup(payload + 4);
  382. memset(status.data, 0, sizeof(status.data));
  383. if (length > 8)
  384. memcpy(status.data, payload + 8, length - 8);
  385. if (STATUS_GET_SOURCE(status) == 2 || STATUS_GET_SOURCE(status) == 3) {
  386. dev_notice(lu_dev(lu),
  387. "non-ORB related status write, not handled\n");
  388. fw_send_response(card, request, RCODE_COMPLETE);
  389. return;
  390. }
  391. /* Lookup the orb corresponding to this status write. */
  392. spin_lock_irqsave(&lu->tgt->lock, flags);
  393. list_for_each_entry(iter, &lu->orb_list, link) {
  394. if (STATUS_GET_ORB_HIGH(status) == 0 &&
  395. STATUS_GET_ORB_LOW(status) == iter->request_bus) {
  396. iter->rcode = RCODE_COMPLETE;
  397. list_del(&iter->link);
  398. orb = iter;
  399. break;
  400. }
  401. }
  402. spin_unlock_irqrestore(&lu->tgt->lock, flags);
  403. if (orb) {
  404. orb->callback(orb, &status);
  405. kref_put(&orb->kref, free_orb); /* orb callback reference */
  406. } else {
  407. dev_err(lu_dev(lu), "status write for unknown ORB\n");
  408. }
  409. fw_send_response(card, request, RCODE_COMPLETE);
  410. }
  411. static void complete_transaction(struct fw_card *card, int rcode,
  412. void *payload, size_t length, void *data)
  413. {
  414. struct sbp2_orb *orb = data;
  415. unsigned long flags;
  416. /*
  417. * This is a little tricky. We can get the status write for
  418. * the orb before we get this callback. The status write
  419. * handler above will assume the orb pointer transaction was
  420. * successful and set the rcode to RCODE_COMPLETE for the orb.
  421. * So this callback only sets the rcode if it hasn't already
  422. * been set and only does the cleanup if the transaction
  423. * failed and we didn't already get a status write.
  424. */
  425. spin_lock_irqsave(&orb->lu->tgt->lock, flags);
  426. if (orb->rcode == -1)
  427. orb->rcode = rcode;
  428. if (orb->rcode != RCODE_COMPLETE) {
  429. list_del(&orb->link);
  430. spin_unlock_irqrestore(&orb->lu->tgt->lock, flags);
  431. orb->callback(orb, NULL);
  432. kref_put(&orb->kref, free_orb); /* orb callback reference */
  433. } else {
  434. spin_unlock_irqrestore(&orb->lu->tgt->lock, flags);
  435. }
  436. kref_put(&orb->kref, free_orb); /* transaction callback reference */
  437. }
  438. static void sbp2_send_orb(struct sbp2_orb *orb, struct sbp2_logical_unit *lu,
  439. int node_id, int generation, u64 offset)
  440. {
  441. struct fw_device *device = target_parent_device(lu->tgt);
  442. struct sbp2_pointer orb_pointer;
  443. unsigned long flags;
  444. orb_pointer.high = 0;
  445. orb_pointer.low = cpu_to_be32(orb->request_bus);
  446. orb->lu = lu;
  447. spin_lock_irqsave(&lu->tgt->lock, flags);
  448. list_add_tail(&orb->link, &lu->orb_list);
  449. spin_unlock_irqrestore(&lu->tgt->lock, flags);
  450. kref_get(&orb->kref); /* transaction callback reference */
  451. kref_get(&orb->kref); /* orb callback reference */
  452. fw_send_request(device->card, &orb->t, TCODE_WRITE_BLOCK_REQUEST,
  453. node_id, generation, device->max_speed, offset,
  454. &orb_pointer, 8, complete_transaction, orb);
  455. }
  456. static int sbp2_cancel_orbs(struct sbp2_logical_unit *lu)
  457. {
  458. struct fw_device *device = target_parent_device(lu->tgt);
  459. struct sbp2_orb *orb, *next;
  460. struct list_head list;
  461. int retval = -ENOENT;
  462. INIT_LIST_HEAD(&list);
  463. spin_lock_irq(&lu->tgt->lock);
  464. list_splice_init(&lu->orb_list, &list);
  465. spin_unlock_irq(&lu->tgt->lock);
  466. list_for_each_entry_safe(orb, next, &list, link) {
  467. retval = 0;
  468. if (fw_cancel_transaction(device->card, &orb->t) == 0)
  469. continue;
  470. orb->rcode = RCODE_CANCELLED;
  471. orb->callback(orb, NULL);
  472. kref_put(&orb->kref, free_orb); /* orb callback reference */
  473. }
  474. return retval;
  475. }
  476. static void complete_management_orb(struct sbp2_orb *base_orb,
  477. struct sbp2_status *status)
  478. {
  479. struct sbp2_management_orb *orb =
  480. container_of(base_orb, struct sbp2_management_orb, base);
  481. if (status)
  482. memcpy(&orb->status, status, sizeof(*status));
  483. complete(&orb->done);
  484. }
  485. static int sbp2_send_management_orb(struct sbp2_logical_unit *lu, int node_id,
  486. int generation, int function,
  487. int lun_or_login_id, void *response)
  488. {
  489. struct fw_device *device = target_parent_device(lu->tgt);
  490. struct sbp2_management_orb *orb;
  491. unsigned int timeout;
  492. int retval = -ENOMEM;
  493. if (function == SBP2_LOGOUT_REQUEST && fw_device_is_shutdown(device))
  494. return 0;
  495. orb = kzalloc_obj(*orb, GFP_NOIO);
  496. if (orb == NULL)
  497. return -ENOMEM;
  498. kref_init(&orb->base.kref);
  499. orb->response_bus =
  500. dma_map_single(device->card->device, &orb->response,
  501. sizeof(orb->response), DMA_FROM_DEVICE);
  502. if (dma_mapping_error(device->card->device, orb->response_bus))
  503. goto fail_mapping_response;
  504. orb->request.response.high = 0;
  505. orb->request.response.low = cpu_to_be32(orb->response_bus);
  506. orb->request.misc = cpu_to_be32(
  507. MANAGEMENT_ORB_NOTIFY |
  508. MANAGEMENT_ORB_FUNCTION(function) |
  509. MANAGEMENT_ORB_LUN(lun_or_login_id));
  510. orb->request.length = cpu_to_be32(
  511. MANAGEMENT_ORB_RESPONSE_LENGTH(sizeof(orb->response)));
  512. orb->request.status_fifo.high =
  513. cpu_to_be32(lu->address_handler.offset >> 32);
  514. orb->request.status_fifo.low =
  515. cpu_to_be32(lu->address_handler.offset);
  516. if (function == SBP2_LOGIN_REQUEST) {
  517. /* Ask for 2^2 == 4 seconds reconnect grace period */
  518. orb->request.misc |= cpu_to_be32(
  519. MANAGEMENT_ORB_RECONNECT(2) |
  520. MANAGEMENT_ORB_EXCLUSIVE(sbp2_param_exclusive_login));
  521. timeout = lu->tgt->mgt_orb_timeout;
  522. } else {
  523. timeout = SBP2_ORB_TIMEOUT;
  524. }
  525. init_completion(&orb->done);
  526. orb->base.callback = complete_management_orb;
  527. orb->base.request_bus =
  528. dma_map_single(device->card->device, &orb->request,
  529. sizeof(orb->request), DMA_TO_DEVICE);
  530. if (dma_mapping_error(device->card->device, orb->base.request_bus))
  531. goto fail_mapping_request;
  532. sbp2_send_orb(&orb->base, lu, node_id, generation,
  533. lu->tgt->management_agent_address);
  534. wait_for_completion_timeout(&orb->done, msecs_to_jiffies(timeout));
  535. retval = -EIO;
  536. if (sbp2_cancel_orbs(lu) == 0) {
  537. dev_err(lu_dev(lu), "ORB reply timed out, rcode 0x%02x\n",
  538. orb->base.rcode);
  539. goto out;
  540. }
  541. if (orb->base.rcode != RCODE_COMPLETE) {
  542. dev_err(lu_dev(lu), "management write failed, rcode 0x%02x\n",
  543. orb->base.rcode);
  544. goto out;
  545. }
  546. if (STATUS_GET_RESPONSE(orb->status) != 0 ||
  547. STATUS_GET_SBP_STATUS(orb->status) != 0) {
  548. dev_err(lu_dev(lu), "error status: %d:%d\n",
  549. STATUS_GET_RESPONSE(orb->status),
  550. STATUS_GET_SBP_STATUS(orb->status));
  551. goto out;
  552. }
  553. retval = 0;
  554. out:
  555. dma_unmap_single(device->card->device, orb->base.request_bus,
  556. sizeof(orb->request), DMA_TO_DEVICE);
  557. fail_mapping_request:
  558. dma_unmap_single(device->card->device, orb->response_bus,
  559. sizeof(orb->response), DMA_FROM_DEVICE);
  560. fail_mapping_response:
  561. if (response)
  562. memcpy(response, orb->response, sizeof(orb->response));
  563. kref_put(&orb->base.kref, free_orb);
  564. return retval;
  565. }
  566. static void sbp2_agent_reset(struct sbp2_logical_unit *lu)
  567. {
  568. struct fw_device *device = target_parent_device(lu->tgt);
  569. __be32 d = 0;
  570. fw_run_transaction(device->card, TCODE_WRITE_QUADLET_REQUEST,
  571. lu->tgt->node_id, lu->generation, device->max_speed,
  572. lu->command_block_agent_address + SBP2_AGENT_RESET,
  573. &d, 4);
  574. }
  575. static void complete_agent_reset_write_no_wait(struct fw_card *card,
  576. int rcode, void *payload, size_t length, void *data)
  577. {
  578. kfree(data);
  579. }
  580. static void sbp2_agent_reset_no_wait(struct sbp2_logical_unit *lu)
  581. {
  582. struct fw_device *device = target_parent_device(lu->tgt);
  583. struct fw_transaction *t;
  584. static __be32 d;
  585. t = kmalloc_obj(*t, GFP_ATOMIC);
  586. if (t == NULL)
  587. return;
  588. fw_send_request(device->card, t, TCODE_WRITE_QUADLET_REQUEST,
  589. lu->tgt->node_id, lu->generation, device->max_speed,
  590. lu->command_block_agent_address + SBP2_AGENT_RESET,
  591. &d, 4, complete_agent_reset_write_no_wait, t);
  592. }
  593. static inline void sbp2_allow_block(struct sbp2_target *tgt)
  594. {
  595. spin_lock_irq(&tgt->lock);
  596. --tgt->dont_block;
  597. spin_unlock_irq(&tgt->lock);
  598. }
  599. /*
  600. * Blocks lu->tgt if all of the following conditions are met:
  601. * - Login, INQUIRY, and high-level SCSI setup of all of the target's
  602. * logical units have been finished (indicated by dont_block == 0).
  603. * - lu->generation is stale.
  604. *
  605. * Note, scsi_block_requests() must be called while holding tgt->lock,
  606. * otherwise it might foil sbp2_[conditionally_]unblock()'s attempt to
  607. * unblock the target.
  608. */
  609. static void sbp2_conditionally_block(struct sbp2_logical_unit *lu)
  610. {
  611. struct sbp2_target *tgt = lu->tgt;
  612. struct fw_card *card = target_parent_device(tgt)->card;
  613. struct Scsi_Host *shost =
  614. container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
  615. unsigned long flags;
  616. spin_lock_irqsave(&tgt->lock, flags);
  617. if (!tgt->dont_block && !lu->blocked &&
  618. lu->generation != card->generation) {
  619. lu->blocked = true;
  620. if (++tgt->blocked == 1)
  621. scsi_block_requests(shost);
  622. }
  623. spin_unlock_irqrestore(&tgt->lock, flags);
  624. }
  625. /*
  626. * Unblocks lu->tgt as soon as all its logical units can be unblocked.
  627. * Note, it is harmless to run scsi_unblock_requests() outside the
  628. * tgt->lock protected section. On the other hand, running it inside
  629. * the section might clash with shost->host_lock.
  630. */
  631. static void sbp2_conditionally_unblock(struct sbp2_logical_unit *lu)
  632. {
  633. struct sbp2_target *tgt = lu->tgt;
  634. struct fw_card *card = target_parent_device(tgt)->card;
  635. struct Scsi_Host *shost =
  636. container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
  637. bool unblock = false;
  638. spin_lock_irq(&tgt->lock);
  639. if (lu->blocked && lu->generation == card->generation) {
  640. lu->blocked = false;
  641. unblock = --tgt->blocked == 0;
  642. }
  643. spin_unlock_irq(&tgt->lock);
  644. if (unblock)
  645. scsi_unblock_requests(shost);
  646. }
  647. /*
  648. * Prevents future blocking of tgt and unblocks it.
  649. * Note, it is harmless to run scsi_unblock_requests() outside the
  650. * tgt->lock protected section. On the other hand, running it inside
  651. * the section might clash with shost->host_lock.
  652. */
  653. static void sbp2_unblock(struct sbp2_target *tgt)
  654. {
  655. struct Scsi_Host *shost =
  656. container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
  657. spin_lock_irq(&tgt->lock);
  658. ++tgt->dont_block;
  659. spin_unlock_irq(&tgt->lock);
  660. scsi_unblock_requests(shost);
  661. }
  662. static int sbp2_lun2int(u16 lun)
  663. {
  664. struct scsi_lun eight_bytes_lun;
  665. memset(&eight_bytes_lun, 0, sizeof(eight_bytes_lun));
  666. eight_bytes_lun.scsi_lun[0] = (lun >> 8) & 0xff;
  667. eight_bytes_lun.scsi_lun[1] = lun & 0xff;
  668. return scsilun_to_int(&eight_bytes_lun);
  669. }
  670. /*
  671. * Write retransmit retry values into the BUSY_TIMEOUT register.
  672. * - The single-phase retry protocol is supported by all SBP-2 devices, but the
  673. * default retry_limit value is 0 (i.e. never retry transmission). We write a
  674. * saner value after logging into the device.
  675. * - The dual-phase retry protocol is optional to implement, and if not
  676. * supported, writes to the dual-phase portion of the register will be
  677. * ignored. We try to write the original 1394-1995 default here.
  678. * - In the case of devices that are also SBP-3-compliant, all writes are
  679. * ignored, as the register is read-only, but contains single-phase retry of
  680. * 15, which is what we're trying to set for all SBP-2 device anyway, so this
  681. * write attempt is safe and yields more consistent behavior for all devices.
  682. *
  683. * See section 8.3.2.3.5 of the 1394-1995 spec, section 6.2 of the SBP-2 spec,
  684. * and section 6.4 of the SBP-3 spec for further details.
  685. */
  686. static void sbp2_set_busy_timeout(struct sbp2_logical_unit *lu)
  687. {
  688. struct fw_device *device = target_parent_device(lu->tgt);
  689. __be32 d = cpu_to_be32(SBP2_CYCLE_LIMIT | SBP2_RETRY_LIMIT);
  690. fw_run_transaction(device->card, TCODE_WRITE_QUADLET_REQUEST,
  691. lu->tgt->node_id, lu->generation, device->max_speed,
  692. CSR_REGISTER_BASE + CSR_BUSY_TIMEOUT, &d, 4);
  693. }
  694. static void sbp2_reconnect(struct work_struct *work);
  695. static void sbp2_login(struct work_struct *work)
  696. {
  697. struct sbp2_logical_unit *lu =
  698. container_of(work, struct sbp2_logical_unit, work.work);
  699. struct sbp2_target *tgt = lu->tgt;
  700. struct fw_device *device = target_parent_device(tgt);
  701. struct Scsi_Host *shost;
  702. struct scsi_device *sdev;
  703. struct sbp2_login_response response;
  704. int generation, node_id, local_node_id;
  705. if (fw_device_is_shutdown(device))
  706. return;
  707. generation = device->generation;
  708. smp_rmb(); /* node IDs must not be older than generation */
  709. node_id = device->node_id;
  710. local_node_id = device->card->node_id;
  711. /* If this is a re-login attempt, log out, or we might be rejected. */
  712. if (lu->has_sdev)
  713. sbp2_send_management_orb(lu, device->node_id, generation,
  714. SBP2_LOGOUT_REQUEST, lu->login_id, NULL);
  715. if (sbp2_send_management_orb(lu, node_id, generation,
  716. SBP2_LOGIN_REQUEST, lu->lun, &response) < 0) {
  717. if (lu->retries++ < 5) {
  718. sbp2_queue_work(lu, DIV_ROUND_UP(HZ, 5));
  719. } else {
  720. dev_err(tgt_dev(tgt), "failed to login to LUN %04x\n",
  721. lu->lun);
  722. /* Let any waiting I/O fail from now on. */
  723. sbp2_unblock(lu->tgt);
  724. }
  725. return;
  726. }
  727. tgt->node_id = node_id;
  728. tgt->address_high = local_node_id << 16;
  729. smp_wmb(); /* node IDs must not be older than generation */
  730. lu->generation = generation;
  731. lu->command_block_agent_address =
  732. ((u64)(be32_to_cpu(response.command_block_agent.high) & 0xffff)
  733. << 32) | be32_to_cpu(response.command_block_agent.low);
  734. lu->login_id = be32_to_cpu(response.misc) & 0xffff;
  735. dev_notice(tgt_dev(tgt), "logged in to LUN %04x (%d retries)\n",
  736. lu->lun, lu->retries);
  737. /* set appropriate retry limit(s) in BUSY_TIMEOUT register */
  738. sbp2_set_busy_timeout(lu);
  739. lu->workfn = sbp2_reconnect;
  740. sbp2_agent_reset(lu);
  741. /* This was a re-login. */
  742. if (lu->has_sdev) {
  743. sbp2_cancel_orbs(lu);
  744. sbp2_conditionally_unblock(lu);
  745. return;
  746. }
  747. if (lu->tgt->workarounds & SBP2_WORKAROUND_DELAY_INQUIRY)
  748. ssleep(SBP2_INQUIRY_DELAY);
  749. shost = container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
  750. sdev = __scsi_add_device(shost, 0, 0, sbp2_lun2int(lu->lun), lu);
  751. /*
  752. * FIXME: We are unable to perform reconnects while in sbp2_login().
  753. * Therefore __scsi_add_device() will get into trouble if a bus reset
  754. * happens in parallel. It will either fail or leave us with an
  755. * unusable sdev. As a workaround we check for this and retry the
  756. * whole login and SCSI probing.
  757. */
  758. /* Reported error during __scsi_add_device() */
  759. if (IS_ERR(sdev))
  760. goto out_logout_login;
  761. /* Unreported error during __scsi_add_device() */
  762. smp_rmb(); /* get current card generation */
  763. if (generation != device->card->generation) {
  764. scsi_remove_device(sdev);
  765. scsi_device_put(sdev);
  766. goto out_logout_login;
  767. }
  768. /* No error during __scsi_add_device() */
  769. lu->has_sdev = true;
  770. scsi_device_put(sdev);
  771. sbp2_allow_block(tgt);
  772. return;
  773. out_logout_login:
  774. smp_rmb(); /* generation may have changed */
  775. generation = device->generation;
  776. smp_rmb(); /* node_id must not be older than generation */
  777. sbp2_send_management_orb(lu, device->node_id, generation,
  778. SBP2_LOGOUT_REQUEST, lu->login_id, NULL);
  779. /*
  780. * If a bus reset happened, sbp2_update will have requeued
  781. * lu->work already. Reset the work from reconnect to login.
  782. */
  783. lu->workfn = sbp2_login;
  784. }
  785. static void sbp2_reconnect(struct work_struct *work)
  786. {
  787. struct sbp2_logical_unit *lu =
  788. container_of(work, struct sbp2_logical_unit, work.work);
  789. struct sbp2_target *tgt = lu->tgt;
  790. struct fw_device *device = target_parent_device(tgt);
  791. int generation, node_id, local_node_id;
  792. if (fw_device_is_shutdown(device))
  793. return;
  794. generation = device->generation;
  795. smp_rmb(); /* node IDs must not be older than generation */
  796. node_id = device->node_id;
  797. local_node_id = device->card->node_id;
  798. if (sbp2_send_management_orb(lu, node_id, generation,
  799. SBP2_RECONNECT_REQUEST,
  800. lu->login_id, NULL) < 0) {
  801. /*
  802. * If reconnect was impossible even though we are in the
  803. * current generation, fall back and try to log in again.
  804. *
  805. * We could check for "Function rejected" status, but
  806. * looking at the bus generation as simpler and more general.
  807. */
  808. smp_rmb(); /* get current card generation */
  809. if (generation == device->card->generation ||
  810. lu->retries++ >= 5) {
  811. dev_err(tgt_dev(tgt), "failed to reconnect\n");
  812. lu->retries = 0;
  813. lu->workfn = sbp2_login;
  814. }
  815. sbp2_queue_work(lu, DIV_ROUND_UP(HZ, 5));
  816. return;
  817. }
  818. tgt->node_id = node_id;
  819. tgt->address_high = local_node_id << 16;
  820. smp_wmb(); /* node IDs must not be older than generation */
  821. lu->generation = generation;
  822. dev_notice(tgt_dev(tgt), "reconnected to LUN %04x (%d retries)\n",
  823. lu->lun, lu->retries);
  824. sbp2_agent_reset(lu);
  825. sbp2_cancel_orbs(lu);
  826. sbp2_conditionally_unblock(lu);
  827. }
  828. static void sbp2_lu_workfn(struct work_struct *work)
  829. {
  830. struct sbp2_logical_unit *lu = container_of(to_delayed_work(work),
  831. struct sbp2_logical_unit, work);
  832. lu->workfn(work);
  833. }
  834. static int sbp2_add_logical_unit(struct sbp2_target *tgt, int lun_entry)
  835. {
  836. struct sbp2_logical_unit *lu;
  837. lu = kmalloc_obj(*lu);
  838. if (!lu)
  839. return -ENOMEM;
  840. lu->address_handler.length = 0x100;
  841. lu->address_handler.address_callback = sbp2_status_write;
  842. lu->address_handler.callback_data = lu;
  843. if (fw_core_add_address_handler(&lu->address_handler,
  844. &fw_high_memory_region) < 0) {
  845. kfree(lu);
  846. return -ENOMEM;
  847. }
  848. lu->tgt = tgt;
  849. lu->lun = lun_entry & 0xffff;
  850. lu->login_id = INVALID_LOGIN_ID;
  851. lu->retries = 0;
  852. lu->has_sdev = false;
  853. lu->blocked = false;
  854. ++tgt->dont_block;
  855. INIT_LIST_HEAD(&lu->orb_list);
  856. lu->workfn = sbp2_login;
  857. INIT_DELAYED_WORK(&lu->work, sbp2_lu_workfn);
  858. list_add_tail(&lu->link, &tgt->lu_list);
  859. return 0;
  860. }
  861. static void sbp2_get_unit_unique_id(struct sbp2_target *tgt,
  862. const u32 *leaf)
  863. {
  864. if ((leaf[0] & 0xffff0000) == 0x00020000)
  865. tgt->guid = (u64)leaf[1] << 32 | leaf[2];
  866. }
  867. static int sbp2_scan_logical_unit_dir(struct sbp2_target *tgt,
  868. const u32 *directory)
  869. {
  870. struct fw_csr_iterator ci;
  871. int key, value;
  872. fw_csr_iterator_init(&ci, directory);
  873. while (fw_csr_iterator_next(&ci, &key, &value))
  874. if (key == SBP2_CSR_LOGICAL_UNIT_NUMBER &&
  875. sbp2_add_logical_unit(tgt, value) < 0)
  876. return -ENOMEM;
  877. return 0;
  878. }
  879. static int sbp2_scan_unit_dir(struct sbp2_target *tgt, const u32 *directory,
  880. u32 *model, u32 *firmware_revision)
  881. {
  882. struct fw_csr_iterator ci;
  883. int key, value;
  884. fw_csr_iterator_init(&ci, directory);
  885. while (fw_csr_iterator_next(&ci, &key, &value)) {
  886. switch (key) {
  887. case CSR_DEPENDENT_INFO | CSR_OFFSET:
  888. tgt->management_agent_address =
  889. CSR_REGISTER_BASE + 4 * value;
  890. break;
  891. case CSR_DIRECTORY_ID:
  892. tgt->directory_id = value;
  893. break;
  894. case CSR_MODEL:
  895. *model = value;
  896. break;
  897. case SBP2_CSR_FIRMWARE_REVISION:
  898. *firmware_revision = value;
  899. break;
  900. case SBP2_CSR_UNIT_CHARACTERISTICS:
  901. /* the timeout value is stored in 500ms units */
  902. tgt->mgt_orb_timeout = (value >> 8 & 0xff) * 500;
  903. break;
  904. case SBP2_CSR_LOGICAL_UNIT_NUMBER:
  905. if (sbp2_add_logical_unit(tgt, value) < 0)
  906. return -ENOMEM;
  907. break;
  908. case SBP2_CSR_UNIT_UNIQUE_ID:
  909. sbp2_get_unit_unique_id(tgt, ci.p - 1 + value);
  910. break;
  911. case SBP2_CSR_LOGICAL_UNIT_DIRECTORY:
  912. /* Adjust for the increment in the iterator */
  913. if (sbp2_scan_logical_unit_dir(tgt, ci.p - 1 + value) < 0)
  914. return -ENOMEM;
  915. break;
  916. }
  917. }
  918. return 0;
  919. }
  920. /*
  921. * Per section 7.4.8 of the SBP-2 spec, a mgt_ORB_timeout value can be
  922. * provided in the config rom. Most devices do provide a value, which
  923. * we'll use for login management orbs, but with some sane limits.
  924. */
  925. static void sbp2_clamp_management_orb_timeout(struct sbp2_target *tgt)
  926. {
  927. unsigned int timeout = tgt->mgt_orb_timeout;
  928. if (timeout > 40000)
  929. dev_notice(tgt_dev(tgt), "%ds mgt_ORB_timeout limited to 40s\n",
  930. timeout / 1000);
  931. tgt->mgt_orb_timeout = clamp_val(timeout, 5000, 40000);
  932. }
  933. static void sbp2_init_workarounds(struct sbp2_target *tgt, u32 model,
  934. u32 firmware_revision)
  935. {
  936. int i;
  937. unsigned int w = sbp2_param_workarounds;
  938. if (w)
  939. dev_notice(tgt_dev(tgt),
  940. "Please notify linux1394-devel@lists.sf.net "
  941. "if you need the workarounds parameter\n");
  942. if (w & SBP2_WORKAROUND_OVERRIDE)
  943. goto out;
  944. for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
  945. if (sbp2_workarounds_table[i].firmware_revision !=
  946. (firmware_revision & 0xffffff00))
  947. continue;
  948. if (sbp2_workarounds_table[i].model != model &&
  949. sbp2_workarounds_table[i].model != SBP2_ROM_VALUE_WILDCARD)
  950. continue;
  951. w |= sbp2_workarounds_table[i].workarounds;
  952. break;
  953. }
  954. out:
  955. if (w)
  956. dev_notice(tgt_dev(tgt), "workarounds 0x%x "
  957. "(firmware_revision 0x%06x, model_id 0x%06x)\n",
  958. w, firmware_revision, model);
  959. tgt->workarounds = w;
  960. }
  961. static const struct scsi_host_template scsi_driver_template;
  962. static void sbp2_remove(struct fw_unit *unit);
  963. static int sbp2_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
  964. {
  965. struct fw_device *device = fw_parent_device(unit);
  966. struct sbp2_target *tgt;
  967. struct sbp2_logical_unit *lu;
  968. struct Scsi_Host *shost;
  969. u32 model, firmware_revision;
  970. /* cannot (or should not) handle targets on the local node */
  971. if (device->is_local)
  972. return -ENODEV;
  973. shost = scsi_host_alloc(&scsi_driver_template, sizeof(*tgt));
  974. if (shost == NULL)
  975. return -ENOMEM;
  976. tgt = (struct sbp2_target *)shost->hostdata;
  977. dev_set_drvdata(&unit->device, tgt);
  978. tgt->unit = unit;
  979. INIT_LIST_HEAD(&tgt->lu_list);
  980. spin_lock_init(&tgt->lock);
  981. tgt->guid = (u64)device->config_rom[3] << 32 | device->config_rom[4];
  982. if (fw_device_enable_phys_dma(device) < 0)
  983. goto fail_shost_put;
  984. shost->max_cmd_len = SBP2_MAX_CDB_SIZE;
  985. if (scsi_add_host_with_dma(shost, &unit->device,
  986. device->card->device) < 0)
  987. goto fail_shost_put;
  988. /* implicit directory ID */
  989. tgt->directory_id = ((unit->directory - device->config_rom) * 4
  990. + CSR_CONFIG_ROM) & 0xffffff;
  991. firmware_revision = SBP2_ROM_VALUE_MISSING;
  992. model = SBP2_ROM_VALUE_MISSING;
  993. if (sbp2_scan_unit_dir(tgt, unit->directory, &model,
  994. &firmware_revision) < 0)
  995. goto fail_remove;
  996. sbp2_clamp_management_orb_timeout(tgt);
  997. sbp2_init_workarounds(tgt, model, firmware_revision);
  998. /*
  999. * At S100 we can do 512 bytes per packet, at S200 1024 bytes,
  1000. * and so on up to 4096 bytes. The SBP-2 max_payload field
  1001. * specifies the max payload size as 2 ^ (max_payload + 2), so
  1002. * if we set this to max_speed + 7, we get the right value.
  1003. */
  1004. tgt->max_payload = min3(device->max_speed + 7, 10U,
  1005. device->card->max_receive - 1);
  1006. /* Do the login in a workqueue so we can easily reschedule retries. */
  1007. list_for_each_entry(lu, &tgt->lu_list, link)
  1008. sbp2_queue_work(lu, DIV_ROUND_UP(HZ, 5));
  1009. return 0;
  1010. fail_remove:
  1011. sbp2_remove(unit);
  1012. return -ENOMEM;
  1013. fail_shost_put:
  1014. scsi_host_put(shost);
  1015. return -ENOMEM;
  1016. }
  1017. static void sbp2_update(struct fw_unit *unit)
  1018. {
  1019. struct sbp2_target *tgt = dev_get_drvdata(&unit->device);
  1020. struct sbp2_logical_unit *lu;
  1021. fw_device_enable_phys_dma(fw_parent_device(unit));
  1022. /*
  1023. * Fw-core serializes sbp2_update() against sbp2_remove().
  1024. * Iteration over tgt->lu_list is therefore safe here.
  1025. */
  1026. list_for_each_entry(lu, &tgt->lu_list, link) {
  1027. sbp2_conditionally_block(lu);
  1028. lu->retries = 0;
  1029. sbp2_queue_work(lu, 0);
  1030. }
  1031. }
  1032. static void sbp2_remove(struct fw_unit *unit)
  1033. {
  1034. struct fw_device *device = fw_parent_device(unit);
  1035. struct sbp2_target *tgt = dev_get_drvdata(&unit->device);
  1036. struct sbp2_logical_unit *lu, *next;
  1037. struct Scsi_Host *shost =
  1038. container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
  1039. struct scsi_device *sdev;
  1040. /* prevent deadlocks */
  1041. sbp2_unblock(tgt);
  1042. list_for_each_entry_safe(lu, next, &tgt->lu_list, link) {
  1043. cancel_delayed_work_sync(&lu->work);
  1044. sdev = scsi_device_lookup(shost, 0, 0, sbp2_lun2int(lu->lun));
  1045. if (sdev) {
  1046. scsi_remove_device(sdev);
  1047. scsi_device_put(sdev);
  1048. }
  1049. if (lu->login_id != INVALID_LOGIN_ID) {
  1050. int generation, node_id;
  1051. /*
  1052. * tgt->node_id may be obsolete here if we failed
  1053. * during initial login or after a bus reset where
  1054. * the topology changed.
  1055. */
  1056. generation = device->generation;
  1057. smp_rmb(); /* node_id vs. generation */
  1058. node_id = device->node_id;
  1059. sbp2_send_management_orb(lu, node_id, generation,
  1060. SBP2_LOGOUT_REQUEST,
  1061. lu->login_id, NULL);
  1062. }
  1063. fw_core_remove_address_handler(&lu->address_handler);
  1064. list_del(&lu->link);
  1065. kfree(lu);
  1066. }
  1067. scsi_remove_host(shost);
  1068. dev_notice(&unit->device, "released target %d:0:0\n", shost->host_no);
  1069. scsi_host_put(shost);
  1070. }
  1071. #define SBP2_UNIT_SPEC_ID_ENTRY 0x0000609e
  1072. #define SBP2_SW_VERSION_ENTRY 0x00010483
  1073. static const struct ieee1394_device_id sbp2_id_table[] = {
  1074. {
  1075. .match_flags = IEEE1394_MATCH_SPECIFIER_ID |
  1076. IEEE1394_MATCH_VERSION,
  1077. .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY,
  1078. .version = SBP2_SW_VERSION_ENTRY,
  1079. },
  1080. { }
  1081. };
  1082. static struct fw_driver sbp2_driver = {
  1083. .driver = {
  1084. .owner = THIS_MODULE,
  1085. .name = KBUILD_MODNAME,
  1086. .bus = &fw_bus_type,
  1087. },
  1088. .probe = sbp2_probe,
  1089. .update = sbp2_update,
  1090. .remove = sbp2_remove,
  1091. .id_table = sbp2_id_table,
  1092. };
  1093. static void sbp2_unmap_scatterlist(struct device *card_device,
  1094. struct sbp2_command_orb *orb)
  1095. {
  1096. scsi_dma_unmap(orb->cmd);
  1097. if (orb->request.misc & cpu_to_be32(COMMAND_ORB_PAGE_TABLE_PRESENT))
  1098. dma_unmap_single(card_device, orb->page_table_bus,
  1099. sizeof(orb->page_table), DMA_TO_DEVICE);
  1100. }
  1101. static unsigned int sbp2_status_to_sense_data(u8 *sbp2_status, u8 *sense_data)
  1102. {
  1103. int sam_status;
  1104. int sfmt = (sbp2_status[0] >> 6) & 0x03;
  1105. if (sfmt == 2 || sfmt == 3) {
  1106. /*
  1107. * Reserved for future standardization (2) or
  1108. * Status block format vendor-dependent (3)
  1109. */
  1110. return DID_ERROR << 16;
  1111. }
  1112. sense_data[0] = 0x70 | sfmt | (sbp2_status[1] & 0x80);
  1113. sense_data[1] = 0x0;
  1114. sense_data[2] = ((sbp2_status[1] << 1) & 0xe0) | (sbp2_status[1] & 0x0f);
  1115. sense_data[3] = sbp2_status[4];
  1116. sense_data[4] = sbp2_status[5];
  1117. sense_data[5] = sbp2_status[6];
  1118. sense_data[6] = sbp2_status[7];
  1119. sense_data[7] = 10;
  1120. sense_data[8] = sbp2_status[8];
  1121. sense_data[9] = sbp2_status[9];
  1122. sense_data[10] = sbp2_status[10];
  1123. sense_data[11] = sbp2_status[11];
  1124. sense_data[12] = sbp2_status[2];
  1125. sense_data[13] = sbp2_status[3];
  1126. sense_data[14] = sbp2_status[12];
  1127. sense_data[15] = sbp2_status[13];
  1128. sam_status = sbp2_status[0] & 0x3f;
  1129. switch (sam_status) {
  1130. case SAM_STAT_GOOD:
  1131. case SAM_STAT_CHECK_CONDITION:
  1132. case SAM_STAT_CONDITION_MET:
  1133. case SAM_STAT_BUSY:
  1134. case SAM_STAT_RESERVATION_CONFLICT:
  1135. case SAM_STAT_COMMAND_TERMINATED:
  1136. return DID_OK << 16 | sam_status;
  1137. default:
  1138. return DID_ERROR << 16;
  1139. }
  1140. }
  1141. static void complete_command_orb(struct sbp2_orb *base_orb,
  1142. struct sbp2_status *status)
  1143. {
  1144. struct sbp2_command_orb *orb =
  1145. container_of(base_orb, struct sbp2_command_orb, base);
  1146. struct fw_device *device = target_parent_device(base_orb->lu->tgt);
  1147. int result;
  1148. if (status != NULL) {
  1149. if (STATUS_GET_DEAD(*status))
  1150. sbp2_agent_reset_no_wait(base_orb->lu);
  1151. switch (STATUS_GET_RESPONSE(*status)) {
  1152. case SBP2_STATUS_REQUEST_COMPLETE:
  1153. result = DID_OK << 16;
  1154. break;
  1155. case SBP2_STATUS_TRANSPORT_FAILURE:
  1156. result = DID_BUS_BUSY << 16;
  1157. break;
  1158. case SBP2_STATUS_ILLEGAL_REQUEST:
  1159. case SBP2_STATUS_VENDOR_DEPENDENT:
  1160. default:
  1161. result = DID_ERROR << 16;
  1162. break;
  1163. }
  1164. if (result == DID_OK << 16 && STATUS_GET_LEN(*status) > 1)
  1165. result = sbp2_status_to_sense_data(STATUS_GET_DATA(*status),
  1166. orb->cmd->sense_buffer);
  1167. } else {
  1168. /*
  1169. * If the orb completes with status == NULL, something
  1170. * went wrong, typically a bus reset happened mid-orb
  1171. * or when sending the write (less likely).
  1172. */
  1173. result = DID_BUS_BUSY << 16;
  1174. sbp2_conditionally_block(base_orb->lu);
  1175. }
  1176. dma_unmap_single(device->card->device, orb->base.request_bus,
  1177. sizeof(orb->request), DMA_TO_DEVICE);
  1178. sbp2_unmap_scatterlist(device->card->device, orb);
  1179. orb->cmd->result = result;
  1180. scsi_done(orb->cmd);
  1181. }
  1182. static int sbp2_map_scatterlist(struct sbp2_command_orb *orb,
  1183. struct fw_device *device, struct sbp2_logical_unit *lu)
  1184. {
  1185. struct scatterlist *sg = scsi_sglist(orb->cmd);
  1186. int i, n;
  1187. n = scsi_dma_map(orb->cmd);
  1188. if (n <= 0)
  1189. goto fail;
  1190. /*
  1191. * Handle the special case where there is only one element in
  1192. * the scatter list by converting it to an immediate block
  1193. * request. This is also a workaround for broken devices such
  1194. * as the second generation iPod which doesn't support page
  1195. * tables.
  1196. */
  1197. if (n == 1) {
  1198. orb->request.data_descriptor.high =
  1199. cpu_to_be32(lu->tgt->address_high);
  1200. orb->request.data_descriptor.low =
  1201. cpu_to_be32(sg_dma_address(sg));
  1202. orb->request.misc |=
  1203. cpu_to_be32(COMMAND_ORB_DATA_SIZE(sg_dma_len(sg)));
  1204. return 0;
  1205. }
  1206. for_each_sg(sg, sg, n, i) {
  1207. orb->page_table[i].high = cpu_to_be32(sg_dma_len(sg) << 16);
  1208. orb->page_table[i].low = cpu_to_be32(sg_dma_address(sg));
  1209. }
  1210. orb->page_table_bus =
  1211. dma_map_single(device->card->device, orb->page_table,
  1212. sizeof(orb->page_table), DMA_TO_DEVICE);
  1213. if (dma_mapping_error(device->card->device, orb->page_table_bus))
  1214. goto fail_page_table;
  1215. /*
  1216. * The data_descriptor pointer is the one case where we need
  1217. * to fill in the node ID part of the address. All other
  1218. * pointers assume that the data referenced reside on the
  1219. * initiator (i.e. us), but data_descriptor can refer to data
  1220. * on other nodes so we need to put our ID in descriptor.high.
  1221. */
  1222. orb->request.data_descriptor.high = cpu_to_be32(lu->tgt->address_high);
  1223. orb->request.data_descriptor.low = cpu_to_be32(orb->page_table_bus);
  1224. orb->request.misc |= cpu_to_be32(COMMAND_ORB_PAGE_TABLE_PRESENT |
  1225. COMMAND_ORB_DATA_SIZE(n));
  1226. return 0;
  1227. fail_page_table:
  1228. scsi_dma_unmap(orb->cmd);
  1229. fail:
  1230. return -ENOMEM;
  1231. }
  1232. /* SCSI stack integration */
  1233. static enum scsi_qc_status sbp2_scsi_queuecommand(struct Scsi_Host *shost,
  1234. struct scsi_cmnd *cmd)
  1235. {
  1236. struct sbp2_logical_unit *lu = cmd->device->hostdata;
  1237. struct fw_device *device = target_parent_device(lu->tgt);
  1238. enum scsi_qc_status retval = SCSI_MLQUEUE_HOST_BUSY;
  1239. struct sbp2_command_orb *orb;
  1240. int generation;
  1241. orb = kzalloc_obj(*orb, GFP_ATOMIC);
  1242. if (orb == NULL)
  1243. return SCSI_MLQUEUE_HOST_BUSY;
  1244. /* Initialize rcode to something not RCODE_COMPLETE. */
  1245. orb->base.rcode = -1;
  1246. kref_init(&orb->base.kref);
  1247. orb->cmd = cmd;
  1248. orb->request.next.high = cpu_to_be32(SBP2_ORB_NULL);
  1249. orb->request.misc = cpu_to_be32(
  1250. COMMAND_ORB_MAX_PAYLOAD(lu->tgt->max_payload) |
  1251. COMMAND_ORB_SPEED(device->max_speed) |
  1252. COMMAND_ORB_NOTIFY);
  1253. if (cmd->sc_data_direction == DMA_FROM_DEVICE)
  1254. orb->request.misc |= cpu_to_be32(COMMAND_ORB_DIRECTION);
  1255. generation = device->generation;
  1256. smp_rmb(); /* sbp2_map_scatterlist looks at tgt->address_high */
  1257. if (scsi_sg_count(cmd) && sbp2_map_scatterlist(orb, device, lu) < 0)
  1258. goto out;
  1259. memcpy(orb->request.command_block, cmd->cmnd, cmd->cmd_len);
  1260. orb->base.callback = complete_command_orb;
  1261. orb->base.request_bus =
  1262. dma_map_single(device->card->device, &orb->request,
  1263. sizeof(orb->request), DMA_TO_DEVICE);
  1264. if (dma_mapping_error(device->card->device, orb->base.request_bus)) {
  1265. sbp2_unmap_scatterlist(device->card->device, orb);
  1266. goto out;
  1267. }
  1268. sbp2_send_orb(&orb->base, lu, lu->tgt->node_id, generation,
  1269. lu->command_block_agent_address + SBP2_ORB_POINTER);
  1270. retval = 0;
  1271. out:
  1272. kref_put(&orb->base.kref, free_orb);
  1273. return retval;
  1274. }
  1275. static int sbp2_scsi_sdev_init(struct scsi_device *sdev)
  1276. {
  1277. struct sbp2_logical_unit *lu = sdev->hostdata;
  1278. /* (Re-)Adding logical units via the SCSI stack is not supported. */
  1279. if (!lu)
  1280. return -ENOSYS;
  1281. sdev->allow_restart = 1;
  1282. if (lu->tgt->workarounds & SBP2_WORKAROUND_INQUIRY_36)
  1283. sdev->inquiry_len = 36;
  1284. return 0;
  1285. }
  1286. static int sbp2_scsi_sdev_configure(struct scsi_device *sdev,
  1287. struct queue_limits *lim)
  1288. {
  1289. struct sbp2_logical_unit *lu = sdev->hostdata;
  1290. sdev->use_10_for_rw = 1;
  1291. if (sbp2_param_exclusive_login) {
  1292. sdev->manage_system_start_stop = 1;
  1293. sdev->manage_runtime_start_stop = 1;
  1294. sdev->manage_shutdown = 1;
  1295. }
  1296. if (sdev->type == TYPE_ROM)
  1297. sdev->use_10_for_ms = 1;
  1298. if (sdev->type == TYPE_DISK &&
  1299. lu->tgt->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
  1300. sdev->skip_ms_page_8 = 1;
  1301. if (lu->tgt->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
  1302. sdev->fix_capacity = 1;
  1303. if (lu->tgt->workarounds & SBP2_WORKAROUND_POWER_CONDITION)
  1304. sdev->start_stop_pwr_cond = 1;
  1305. if (lu->tgt->workarounds & SBP2_WORKAROUND_128K_MAX_TRANS)
  1306. lim->max_hw_sectors = 128 * 1024 / 512;
  1307. return 0;
  1308. }
  1309. /*
  1310. * Called by scsi stack when something has really gone wrong. Usually
  1311. * called when a command has timed-out for some reason.
  1312. */
  1313. static int sbp2_scsi_abort(struct scsi_cmnd *cmd)
  1314. {
  1315. struct sbp2_logical_unit *lu = cmd->device->hostdata;
  1316. dev_notice(lu_dev(lu), "sbp2_scsi_abort\n");
  1317. sbp2_agent_reset(lu);
  1318. sbp2_cancel_orbs(lu);
  1319. return SUCCESS;
  1320. }
  1321. /*
  1322. * Format of /sys/bus/scsi/devices/.../ieee1394_id:
  1323. * u64 EUI-64 : u24 directory_ID : u16 LUN (all printed in hexadecimal)
  1324. *
  1325. * This is the concatenation of target port identifier and logical unit
  1326. * identifier as per SAM-2...SAM-4 annex A.
  1327. */
  1328. static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *dev,
  1329. struct device_attribute *attr, char *buf)
  1330. {
  1331. struct scsi_device *sdev = to_scsi_device(dev);
  1332. struct sbp2_logical_unit *lu;
  1333. if (!sdev)
  1334. return 0;
  1335. lu = sdev->hostdata;
  1336. return sprintf(buf, "%016llx:%06x:%04x\n",
  1337. (unsigned long long)lu->tgt->guid,
  1338. lu->tgt->directory_id, lu->lun);
  1339. }
  1340. static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
  1341. static struct attribute *sbp2_scsi_sysfs_attrs[] = {
  1342. &dev_attr_ieee1394_id.attr,
  1343. NULL
  1344. };
  1345. ATTRIBUTE_GROUPS(sbp2_scsi_sysfs);
  1346. static const struct scsi_host_template scsi_driver_template = {
  1347. .module = THIS_MODULE,
  1348. .name = "SBP-2 IEEE-1394",
  1349. .proc_name = "sbp2",
  1350. .queuecommand = sbp2_scsi_queuecommand,
  1351. .sdev_init = sbp2_scsi_sdev_init,
  1352. .sdev_configure = sbp2_scsi_sdev_configure,
  1353. .eh_abort_handler = sbp2_scsi_abort,
  1354. .this_id = -1,
  1355. .sg_tablesize = SG_ALL,
  1356. .max_segment_size = SBP2_MAX_SEG_SIZE,
  1357. .can_queue = 1,
  1358. .sdev_groups = sbp2_scsi_sysfs_groups,
  1359. };
  1360. MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
  1361. MODULE_DESCRIPTION("SCSI over IEEE1394");
  1362. MODULE_LICENSE("GPL");
  1363. MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
  1364. /* Provide a module alias so root-on-sbp2 initrds don't break. */
  1365. MODULE_ALIAS("sbp2");
  1366. static int __init sbp2_init(void)
  1367. {
  1368. return driver_register(&sbp2_driver.driver);
  1369. }
  1370. static void __exit sbp2_cleanup(void)
  1371. {
  1372. driver_unregister(&sbp2_driver.driver);
  1373. }
  1374. module_init(sbp2_init);
  1375. module_exit(sbp2_cleanup);