fw-sbp2.c 48 KB

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