fw-sbp2.c 47 KB

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