sbp2.c 47 KB

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