fw-sbp2.c 39 KB

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