fw-sbp2.c 45 KB

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