fw-sbp2.c 45 KB

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