sbp2.c 47 KB

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