fw-sbp2.c 47 KB

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