fw-sbp2.c 46 KB

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