sbp2.c 48 KB

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