sbp2.c 47 KB

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