fw-sbp2.c 48 KB

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