fw-sbp2.c 37 KB

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