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