fw-sbp2.c 41 KB

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