fw-sbp2.c 47 KB

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