fw-sbp2.c 38 KB

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