sbp2.c 47 KB

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