fw-sbp2.c 47 KB

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