sbp2.c 48 KB

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