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