sbp2.c 48 KB

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