fw-sbp2.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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/mod_devicetable.h>
  32. #include <linux/device.h>
  33. #include <linux/scatterlist.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/timer.h>
  36. #include <scsi/scsi.h>
  37. #include <scsi/scsi_cmnd.h>
  38. #include <scsi/scsi_dbg.h>
  39. #include <scsi/scsi_device.h>
  40. #include <scsi/scsi_host.h>
  41. #include "fw-transaction.h"
  42. #include "fw-topology.h"
  43. #include "fw-device.h"
  44. /* I don't know why the SCSI stack doesn't define something like this... */
  45. typedef void (*scsi_done_fn_t)(struct scsi_cmnd *);
  46. static const char sbp2_driver_name[] = "sbp2";
  47. struct sbp2_device {
  48. struct kref kref;
  49. struct fw_unit *unit;
  50. struct fw_address_handler address_handler;
  51. struct list_head orb_list;
  52. u64 management_agent_address;
  53. u64 command_block_agent_address;
  54. u32 workarounds;
  55. int login_id;
  56. /*
  57. * We cache these addresses and only update them once we've
  58. * logged in or reconnected to the sbp2 device. That way, any
  59. * IO to the device will automatically fail and get retried if
  60. * it happens in a window where the device is not ready to
  61. * handle it (e.g. after a bus reset but before we reconnect).
  62. */
  63. int node_id;
  64. int address_high;
  65. int generation;
  66. int retries;
  67. struct delayed_work work;
  68. };
  69. #define SBP2_MAX_SG_ELEMENT_LENGTH 0xf000
  70. #define SBP2_MAX_SECTORS 255 /* Max sectors supported */
  71. #define SBP2_ORB_TIMEOUT 2000 /* Timeout in ms */
  72. #define SBP2_ORB_NULL 0x80000000
  73. #define SBP2_DIRECTION_TO_MEDIA 0x0
  74. #define SBP2_DIRECTION_FROM_MEDIA 0x1
  75. /* Unit directory keys */
  76. #define SBP2_COMMAND_SET_SPECIFIER 0x38
  77. #define SBP2_COMMAND_SET 0x39
  78. #define SBP2_COMMAND_SET_REVISION 0x3b
  79. #define SBP2_FIRMWARE_REVISION 0x3c
  80. /* Flags for detected oddities and brokeness */
  81. #define SBP2_WORKAROUND_128K_MAX_TRANS 0x1
  82. #define SBP2_WORKAROUND_INQUIRY_36 0x2
  83. #define SBP2_WORKAROUND_MODE_SENSE_8 0x4
  84. #define SBP2_WORKAROUND_FIX_CAPACITY 0x8
  85. #define SBP2_WORKAROUND_OVERRIDE 0x100
  86. /* Management orb opcodes */
  87. #define SBP2_LOGIN_REQUEST 0x0
  88. #define SBP2_QUERY_LOGINS_REQUEST 0x1
  89. #define SBP2_RECONNECT_REQUEST 0x3
  90. #define SBP2_SET_PASSWORD_REQUEST 0x4
  91. #define SBP2_LOGOUT_REQUEST 0x7
  92. #define SBP2_ABORT_TASK_REQUEST 0xb
  93. #define SBP2_ABORT_TASK_SET 0xc
  94. #define SBP2_LOGICAL_UNIT_RESET 0xe
  95. #define SBP2_TARGET_RESET_REQUEST 0xf
  96. /* Offsets for command block agent registers */
  97. #define SBP2_AGENT_STATE 0x00
  98. #define SBP2_AGENT_RESET 0x04
  99. #define SBP2_ORB_POINTER 0x08
  100. #define SBP2_DOORBELL 0x10
  101. #define SBP2_UNSOLICITED_STATUS_ENABLE 0x14
  102. /* Status write response codes */
  103. #define SBP2_STATUS_REQUEST_COMPLETE 0x0
  104. #define SBP2_STATUS_TRANSPORT_FAILURE 0x1
  105. #define SBP2_STATUS_ILLEGAL_REQUEST 0x2
  106. #define SBP2_STATUS_VENDOR_DEPENDENT 0x3
  107. #define STATUS_GET_ORB_HIGH(v) ((v).status & 0xffff)
  108. #define STATUS_GET_SBP_STATUS(v) (((v).status >> 16) & 0xff)
  109. #define STATUS_GET_LEN(v) (((v).status >> 24) & 0x07)
  110. #define STATUS_GET_DEAD(v) (((v).status >> 27) & 0x01)
  111. #define STATUS_GET_RESPONSE(v) (((v).status >> 28) & 0x03)
  112. #define STATUS_GET_SOURCE(v) (((v).status >> 30) & 0x03)
  113. #define STATUS_GET_ORB_LOW(v) ((v).orb_low)
  114. #define STATUS_GET_DATA(v) ((v).data)
  115. struct sbp2_status {
  116. u32 status;
  117. u32 orb_low;
  118. u8 data[24];
  119. };
  120. struct sbp2_pointer {
  121. u32 high;
  122. u32 low;
  123. };
  124. struct sbp2_orb {
  125. struct fw_transaction t;
  126. dma_addr_t request_bus;
  127. int rcode;
  128. struct sbp2_pointer pointer;
  129. void (*callback)(struct sbp2_orb * orb, struct sbp2_status * status);
  130. struct list_head link;
  131. };
  132. #define MANAGEMENT_ORB_LUN(v) ((v))
  133. #define MANAGEMENT_ORB_FUNCTION(v) ((v) << 16)
  134. #define MANAGEMENT_ORB_RECONNECT(v) ((v) << 20)
  135. #define MANAGEMENT_ORB_EXCLUSIVE ((1) << 28)
  136. #define MANAGEMENT_ORB_REQUEST_FORMAT(v) ((v) << 29)
  137. #define MANAGEMENT_ORB_NOTIFY ((1) << 31)
  138. #define MANAGEMENT_ORB_RESPONSE_LENGTH(v) ((v))
  139. #define MANAGEMENT_ORB_PASSWORD_LENGTH(v) ((v) << 16)
  140. struct sbp2_management_orb {
  141. struct sbp2_orb base;
  142. struct {
  143. struct sbp2_pointer password;
  144. struct sbp2_pointer response;
  145. u32 misc;
  146. u32 length;
  147. struct sbp2_pointer status_fifo;
  148. } request;
  149. __be32 response[4];
  150. dma_addr_t response_bus;
  151. struct completion done;
  152. struct sbp2_status status;
  153. };
  154. #define LOGIN_RESPONSE_GET_LOGIN_ID(v) ((v).misc & 0xffff)
  155. #define LOGIN_RESPONSE_GET_LENGTH(v) (((v).misc >> 16) & 0xffff)
  156. struct sbp2_login_response {
  157. u32 misc;
  158. struct sbp2_pointer command_block_agent;
  159. u32 reconnect_hold;
  160. };
  161. #define COMMAND_ORB_DATA_SIZE(v) ((v))
  162. #define COMMAND_ORB_PAGE_SIZE(v) ((v) << 16)
  163. #define COMMAND_ORB_PAGE_TABLE_PRESENT ((1) << 19)
  164. #define COMMAND_ORB_MAX_PAYLOAD(v) ((v) << 20)
  165. #define COMMAND_ORB_SPEED(v) ((v) << 24)
  166. #define COMMAND_ORB_DIRECTION(v) ((v) << 27)
  167. #define COMMAND_ORB_REQUEST_FORMAT(v) ((v) << 29)
  168. #define COMMAND_ORB_NOTIFY ((1) << 31)
  169. struct sbp2_command_orb {
  170. struct sbp2_orb base;
  171. struct {
  172. struct sbp2_pointer next;
  173. struct sbp2_pointer data_descriptor;
  174. u32 misc;
  175. u8 command_block[12];
  176. } request;
  177. struct scsi_cmnd *cmd;
  178. scsi_done_fn_t done;
  179. struct fw_unit *unit;
  180. struct sbp2_pointer page_table[SG_ALL];
  181. dma_addr_t page_table_bus;
  182. dma_addr_t request_buffer_bus;
  183. };
  184. /*
  185. * List of devices with known bugs.
  186. *
  187. * The firmware_revision field, masked with 0xffff00, is the best
  188. * indicator for the type of bridge chip of a device. It yields a few
  189. * false positives but this did not break correctly behaving devices
  190. * so far. We use ~0 as a wildcard, since the 24 bit values we get
  191. * from the config rom can never match that.
  192. */
  193. static const struct {
  194. u32 firmware_revision;
  195. u32 model;
  196. unsigned workarounds;
  197. } sbp2_workarounds_table[] = {
  198. /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
  199. .firmware_revision = 0x002800,
  200. .model = 0x001010,
  201. .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
  202. SBP2_WORKAROUND_MODE_SENSE_8,
  203. },
  204. /* Initio bridges, actually only needed for some older ones */ {
  205. .firmware_revision = 0x000200,
  206. .model = ~0,
  207. .workarounds = SBP2_WORKAROUND_INQUIRY_36,
  208. },
  209. /* Symbios bridge */ {
  210. .firmware_revision = 0xa0b800,
  211. .model = ~0,
  212. .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
  213. },
  214. /*
  215. * There are iPods (2nd gen, 3rd gen) with model_id == 0, but
  216. * these iPods do not feature the read_capacity bug according
  217. * to one report. Read_capacity behaviour as well as model_id
  218. * could change due to Apple-supplied firmware updates though.
  219. */
  220. /* iPod 4th generation. */ {
  221. .firmware_revision = 0x0a2700,
  222. .model = 0x000021,
  223. .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
  224. },
  225. /* iPod mini */ {
  226. .firmware_revision = 0x0a2700,
  227. .model = 0x000023,
  228. .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
  229. },
  230. /* iPod Photo */ {
  231. .firmware_revision = 0x0a2700,
  232. .model = 0x00007e,
  233. .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
  234. }
  235. };
  236. static void
  237. sbp2_status_write(struct fw_card *card, struct fw_request *request,
  238. int tcode, int destination, int source,
  239. int generation, int speed,
  240. unsigned long long offset,
  241. void *payload, size_t length, void *callback_data)
  242. {
  243. struct sbp2_device *sd = callback_data;
  244. struct sbp2_orb *orb;
  245. struct sbp2_status status;
  246. size_t header_size;
  247. unsigned long flags;
  248. if (tcode != TCODE_WRITE_BLOCK_REQUEST ||
  249. length == 0 || length > sizeof(status)) {
  250. fw_send_response(card, request, RCODE_TYPE_ERROR);
  251. return;
  252. }
  253. header_size = min(length, 2 * sizeof(u32));
  254. fw_memcpy_from_be32(&status, payload, header_size);
  255. if (length > header_size)
  256. memcpy(status.data, payload + 8, length - header_size);
  257. if (STATUS_GET_SOURCE(status) == 2 || STATUS_GET_SOURCE(status) == 3) {
  258. fw_notify("non-orb related status write, not handled\n");
  259. fw_send_response(card, request, RCODE_COMPLETE);
  260. return;
  261. }
  262. /* Lookup the orb corresponding to this status write. */
  263. spin_lock_irqsave(&card->lock, flags);
  264. list_for_each_entry(orb, &sd->orb_list, link) {
  265. if (STATUS_GET_ORB_HIGH(status) == 0 &&
  266. STATUS_GET_ORB_LOW(status) == orb->request_bus &&
  267. orb->rcode == RCODE_COMPLETE) {
  268. list_del(&orb->link);
  269. break;
  270. }
  271. }
  272. spin_unlock_irqrestore(&card->lock, flags);
  273. if (&orb->link != &sd->orb_list)
  274. orb->callback(orb, &status);
  275. else
  276. fw_error("status write for unknown orb\n");
  277. fw_send_response(card, request, RCODE_COMPLETE);
  278. }
  279. static void
  280. complete_transaction(struct fw_card *card, int rcode,
  281. void *payload, size_t length, void *data)
  282. {
  283. struct sbp2_orb *orb = data;
  284. unsigned long flags;
  285. orb->rcode = rcode;
  286. if (rcode != RCODE_COMPLETE) {
  287. spin_lock_irqsave(&card->lock, flags);
  288. list_del(&orb->link);
  289. spin_unlock_irqrestore(&card->lock, flags);
  290. orb->callback(orb, NULL);
  291. }
  292. }
  293. static void
  294. sbp2_send_orb(struct sbp2_orb *orb, struct fw_unit *unit,
  295. int node_id, int generation, u64 offset)
  296. {
  297. struct fw_device *device = fw_device(unit->device.parent);
  298. struct sbp2_device *sd = unit->device.driver_data;
  299. unsigned long flags;
  300. orb->pointer.high = 0;
  301. orb->pointer.low = orb->request_bus;
  302. fw_memcpy_to_be32(&orb->pointer, &orb->pointer, sizeof(orb->pointer));
  303. spin_lock_irqsave(&device->card->lock, flags);
  304. list_add_tail(&orb->link, &sd->orb_list);
  305. spin_unlock_irqrestore(&device->card->lock, flags);
  306. fw_send_request(device->card, &orb->t, TCODE_WRITE_BLOCK_REQUEST,
  307. node_id, generation,
  308. device->node->max_speed, offset,
  309. &orb->pointer, sizeof(orb->pointer),
  310. complete_transaction, orb);
  311. }
  312. static int sbp2_cancel_orbs(struct fw_unit *unit)
  313. {
  314. struct fw_device *device = fw_device(unit->device.parent);
  315. struct sbp2_device *sd = unit->device.driver_data;
  316. struct sbp2_orb *orb, *next;
  317. struct list_head list;
  318. unsigned long flags;
  319. int retval = -ENOENT;
  320. INIT_LIST_HEAD(&list);
  321. spin_lock_irqsave(&device->card->lock, flags);
  322. list_splice_init(&sd->orb_list, &list);
  323. spin_unlock_irqrestore(&device->card->lock, flags);
  324. list_for_each_entry_safe(orb, next, &list, link) {
  325. retval = 0;
  326. if (fw_cancel_transaction(device->card, &orb->t) == 0)
  327. continue;
  328. orb->rcode = RCODE_CANCELLED;
  329. orb->callback(orb, NULL);
  330. }
  331. return retval;
  332. }
  333. static void
  334. complete_management_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
  335. {
  336. struct sbp2_management_orb *orb =
  337. (struct sbp2_management_orb *)base_orb;
  338. if (status)
  339. memcpy(&orb->status, status, sizeof(*status));
  340. complete(&orb->done);
  341. }
  342. static int
  343. sbp2_send_management_orb(struct fw_unit *unit, int node_id, int generation,
  344. int function, int lun, void *response)
  345. {
  346. struct fw_device *device = fw_device(unit->device.parent);
  347. struct sbp2_device *sd = unit->device.driver_data;
  348. struct sbp2_management_orb *orb;
  349. int retval = -ENOMEM;
  350. orb = kzalloc(sizeof(*orb), GFP_ATOMIC);
  351. if (orb == NULL)
  352. return -ENOMEM;
  353. /*
  354. * The sbp2 device is going to send a block read request to
  355. * read out the request from host memory, so map it for dma.
  356. */
  357. orb->base.request_bus =
  358. dma_map_single(device->card->device, &orb->request,
  359. sizeof(orb->request), DMA_TO_DEVICE);
  360. if (dma_mapping_error(orb->base.request_bus))
  361. goto out;
  362. orb->response_bus =
  363. dma_map_single(device->card->device, &orb->response,
  364. sizeof(orb->response), DMA_FROM_DEVICE);
  365. if (dma_mapping_error(orb->response_bus))
  366. goto out;
  367. orb->request.response.high = 0;
  368. orb->request.response.low = orb->response_bus;
  369. orb->request.misc =
  370. MANAGEMENT_ORB_NOTIFY |
  371. MANAGEMENT_ORB_FUNCTION(function) |
  372. MANAGEMENT_ORB_LUN(lun);
  373. orb->request.length =
  374. MANAGEMENT_ORB_RESPONSE_LENGTH(sizeof(orb->response));
  375. orb->request.status_fifo.high = sd->address_handler.offset >> 32;
  376. orb->request.status_fifo.low = sd->address_handler.offset;
  377. /*
  378. * FIXME: Yeah, ok this isn't elegant, we hardwire exclusive
  379. * login and 1 second reconnect time. The reconnect setting
  380. * is probably fine, but the exclusive login should be an option.
  381. */
  382. if (function == SBP2_LOGIN_REQUEST) {
  383. orb->request.misc |=
  384. MANAGEMENT_ORB_EXCLUSIVE |
  385. MANAGEMENT_ORB_RECONNECT(0);
  386. }
  387. fw_memcpy_to_be32(&orb->request, &orb->request, sizeof(orb->request));
  388. init_completion(&orb->done);
  389. orb->base.callback = complete_management_orb;
  390. sbp2_send_orb(&orb->base, unit,
  391. node_id, generation, sd->management_agent_address);
  392. wait_for_completion_timeout(&orb->done,
  393. msecs_to_jiffies(SBP2_ORB_TIMEOUT));
  394. retval = -EIO;
  395. if (sbp2_cancel_orbs(unit) == 0) {
  396. fw_error("orb reply timed out, rcode=0x%02x\n",
  397. orb->base.rcode);
  398. goto out;
  399. }
  400. if (orb->base.rcode != RCODE_COMPLETE) {
  401. fw_error("management write failed, rcode 0x%02x\n",
  402. orb->base.rcode);
  403. goto out;
  404. }
  405. if (STATUS_GET_RESPONSE(orb->status) != 0 ||
  406. STATUS_GET_SBP_STATUS(orb->status) != 0) {
  407. fw_error("error status: %d:%d\n",
  408. STATUS_GET_RESPONSE(orb->status),
  409. STATUS_GET_SBP_STATUS(orb->status));
  410. goto out;
  411. }
  412. retval = 0;
  413. out:
  414. dma_unmap_single(device->card->device, orb->base.request_bus,
  415. sizeof(orb->request), DMA_TO_DEVICE);
  416. dma_unmap_single(device->card->device, orb->response_bus,
  417. sizeof(orb->response), DMA_FROM_DEVICE);
  418. if (response)
  419. fw_memcpy_from_be32(response,
  420. orb->response, sizeof(orb->response));
  421. kfree(orb);
  422. return retval;
  423. }
  424. static void
  425. complete_agent_reset_write(struct fw_card *card, int rcode,
  426. void *payload, size_t length, void *data)
  427. {
  428. struct fw_transaction *t = data;
  429. kfree(t);
  430. }
  431. static int sbp2_agent_reset(struct fw_unit *unit)
  432. {
  433. struct fw_device *device = fw_device(unit->device.parent);
  434. struct sbp2_device *sd = unit->device.driver_data;
  435. struct fw_transaction *t;
  436. static u32 zero;
  437. t = kzalloc(sizeof(*t), GFP_ATOMIC);
  438. if (t == NULL)
  439. return -ENOMEM;
  440. fw_send_request(device->card, t, TCODE_WRITE_QUADLET_REQUEST,
  441. sd->node_id, sd->generation, SCODE_400,
  442. sd->command_block_agent_address + SBP2_AGENT_RESET,
  443. &zero, sizeof(zero), complete_agent_reset_write, t);
  444. return 0;
  445. }
  446. static void sbp2_reconnect(struct work_struct *work);
  447. static struct scsi_host_template scsi_driver_template;
  448. static void
  449. release_sbp2_device(struct kref *kref)
  450. {
  451. struct sbp2_device *sd = container_of(kref, struct sbp2_device, kref);
  452. struct Scsi_Host *host =
  453. container_of((void *)sd, struct Scsi_Host, hostdata[0]);
  454. sbp2_send_management_orb(sd->unit, sd->node_id, sd->generation,
  455. SBP2_LOGOUT_REQUEST, sd->login_id, NULL);
  456. scsi_remove_host(host);
  457. fw_core_remove_address_handler(&sd->address_handler);
  458. fw_notify("removed sbp2 unit %s\n", sd->unit->device.bus_id);
  459. put_device(&sd->unit->device);
  460. scsi_host_put(host);
  461. }
  462. static void sbp2_login(struct work_struct *work)
  463. {
  464. struct sbp2_device *sd =
  465. container_of(work, struct sbp2_device, work.work);
  466. struct Scsi_Host *host =
  467. container_of((void *)sd, struct Scsi_Host, hostdata[0]);
  468. struct fw_unit *unit = sd->unit;
  469. struct fw_device *device = fw_device(unit->device.parent);
  470. struct sbp2_login_response response;
  471. int generation, node_id, local_node_id, lun, retval;
  472. /* FIXME: Make this work for multi-lun devices. */
  473. lun = 0;
  474. generation = device->card->generation;
  475. node_id = device->node->node_id;
  476. local_node_id = device->card->local_node->node_id;
  477. if (sbp2_send_management_orb(unit, node_id, generation,
  478. SBP2_LOGIN_REQUEST, lun, &response) < 0) {
  479. if (sd->retries++ < 5) {
  480. schedule_delayed_work(&sd->work, DIV_ROUND_UP(HZ, 5));
  481. } else {
  482. fw_error("failed to login to %s\n",
  483. unit->device.bus_id);
  484. kref_put(&sd->kref, release_sbp2_device);
  485. }
  486. return;
  487. }
  488. sd->generation = generation;
  489. sd->node_id = node_id;
  490. sd->address_high = local_node_id << 16;
  491. /* Get command block agent offset and login id. */
  492. sd->command_block_agent_address =
  493. ((u64) (response.command_block_agent.high & 0xffff) << 32) |
  494. response.command_block_agent.low;
  495. sd->login_id = LOGIN_RESPONSE_GET_LOGIN_ID(response);
  496. fw_notify("logged in to sbp2 unit %s (%d retries)\n",
  497. unit->device.bus_id, sd->retries);
  498. fw_notify(" - management_agent_address: 0x%012llx\n",
  499. (unsigned long long) sd->management_agent_address);
  500. fw_notify(" - command_block_agent_address: 0x%012llx\n",
  501. (unsigned long long) sd->command_block_agent_address);
  502. fw_notify(" - status write address: 0x%012llx\n",
  503. (unsigned long long) sd->address_handler.offset);
  504. #if 0
  505. /* FIXME: The linux1394 sbp2 does this last step. */
  506. sbp2_set_busy_timeout(scsi_id);
  507. #endif
  508. PREPARE_DELAYED_WORK(&sd->work, sbp2_reconnect);
  509. sbp2_agent_reset(unit);
  510. /* FIXME: Loop over luns here. */
  511. lun = 0;
  512. retval = scsi_add_device(host, 0, 0, lun);
  513. if (retval < 0) {
  514. sbp2_send_management_orb(unit, sd->node_id, sd->generation,
  515. SBP2_LOGOUT_REQUEST, sd->login_id,
  516. NULL);
  517. /*
  518. * Set this back to sbp2_login so we fall back and
  519. * retry login on bus reset.
  520. */
  521. PREPARE_DELAYED_WORK(&sd->work, sbp2_login);
  522. }
  523. kref_put(&sd->kref, release_sbp2_device);
  524. }
  525. static int sbp2_probe(struct device *dev)
  526. {
  527. struct fw_unit *unit = fw_unit(dev);
  528. struct fw_device *device = fw_device(unit->device.parent);
  529. struct sbp2_device *sd;
  530. struct fw_csr_iterator ci;
  531. struct Scsi_Host *host;
  532. int i, key, value, err;
  533. u32 model, firmware_revision;
  534. err = -ENOMEM;
  535. host = scsi_host_alloc(&scsi_driver_template, sizeof(*sd));
  536. if (host == NULL)
  537. goto fail;
  538. sd = (struct sbp2_device *) host->hostdata;
  539. unit->device.driver_data = sd;
  540. sd->unit = unit;
  541. INIT_LIST_HEAD(&sd->orb_list);
  542. kref_init(&sd->kref);
  543. sd->address_handler.length = 0x100;
  544. sd->address_handler.address_callback = sbp2_status_write;
  545. sd->address_handler.callback_data = sd;
  546. err = fw_core_add_address_handler(&sd->address_handler,
  547. &fw_high_memory_region);
  548. if (err < 0)
  549. goto fail_host;
  550. err = fw_device_enable_phys_dma(device);
  551. if (err < 0)
  552. goto fail_address_handler;
  553. err = scsi_add_host(host, &unit->device);
  554. if (err < 0)
  555. goto fail_address_handler;
  556. /*
  557. * Scan unit directory to get management agent address,
  558. * firmware revison and model. Initialize firmware_revision
  559. * and model to values that wont match anything in our table.
  560. */
  561. firmware_revision = 0xff000000;
  562. model = 0xff000000;
  563. fw_csr_iterator_init(&ci, unit->directory);
  564. while (fw_csr_iterator_next(&ci, &key, &value)) {
  565. switch (key) {
  566. case CSR_DEPENDENT_INFO | CSR_OFFSET:
  567. sd->management_agent_address =
  568. 0xfffff0000000ULL + 4 * value;
  569. break;
  570. case SBP2_FIRMWARE_REVISION:
  571. firmware_revision = value;
  572. break;
  573. case CSR_MODEL:
  574. model = value;
  575. break;
  576. }
  577. }
  578. for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
  579. if (sbp2_workarounds_table[i].firmware_revision !=
  580. (firmware_revision & 0xffffff00))
  581. continue;
  582. if (sbp2_workarounds_table[i].model != model &&
  583. sbp2_workarounds_table[i].model != ~0)
  584. continue;
  585. sd->workarounds |= sbp2_workarounds_table[i].workarounds;
  586. break;
  587. }
  588. if (sd->workarounds)
  589. fw_notify("Workarounds for node %s: 0x%x "
  590. "(firmware_revision 0x%06x, model_id 0x%06x)\n",
  591. unit->device.bus_id,
  592. sd->workarounds, firmware_revision, model);
  593. get_device(&unit->device);
  594. /*
  595. * We schedule work to do the login so we can easily
  596. * reschedule retries. Always get the ref before scheduling
  597. * work.
  598. */
  599. INIT_DELAYED_WORK(&sd->work, sbp2_login);
  600. if (schedule_delayed_work(&sd->work, 0))
  601. kref_get(&sd->kref);
  602. return 0;
  603. fail_address_handler:
  604. fw_core_remove_address_handler(&sd->address_handler);
  605. fail_host:
  606. scsi_host_put(host);
  607. fail:
  608. return err;
  609. }
  610. static int sbp2_remove(struct device *dev)
  611. {
  612. struct fw_unit *unit = fw_unit(dev);
  613. struct sbp2_device *sd = unit->device.driver_data;
  614. kref_put(&sd->kref, release_sbp2_device);
  615. return 0;
  616. }
  617. static void sbp2_reconnect(struct work_struct *work)
  618. {
  619. struct sbp2_device *sd =
  620. container_of(work, struct sbp2_device, work.work);
  621. struct fw_unit *unit = sd->unit;
  622. struct fw_device *device = fw_device(unit->device.parent);
  623. int generation, node_id, local_node_id;
  624. generation = device->card->generation;
  625. node_id = device->node->node_id;
  626. local_node_id = device->card->local_node->node_id;
  627. if (sbp2_send_management_orb(unit, node_id, generation,
  628. SBP2_RECONNECT_REQUEST,
  629. sd->login_id, NULL) < 0) {
  630. if (sd->retries++ >= 5) {
  631. fw_error("failed to reconnect to %s\n",
  632. unit->device.bus_id);
  633. /* Fall back and try to log in again. */
  634. sd->retries = 0;
  635. PREPARE_DELAYED_WORK(&sd->work, sbp2_login);
  636. }
  637. schedule_delayed_work(&sd->work, DIV_ROUND_UP(HZ, 5));
  638. return;
  639. }
  640. sd->generation = generation;
  641. sd->node_id = node_id;
  642. sd->address_high = local_node_id << 16;
  643. fw_notify("reconnected to unit %s (%d retries)\n",
  644. unit->device.bus_id, sd->retries);
  645. sbp2_agent_reset(unit);
  646. sbp2_cancel_orbs(unit);
  647. kref_put(&sd->kref, release_sbp2_device);
  648. }
  649. static void sbp2_update(struct fw_unit *unit)
  650. {
  651. struct fw_device *device = fw_device(unit->device.parent);
  652. struct sbp2_device *sd = unit->device.driver_data;
  653. sd->retries = 0;
  654. fw_device_enable_phys_dma(device);
  655. if (schedule_delayed_work(&sd->work, 0))
  656. kref_get(&sd->kref);
  657. }
  658. #define SBP2_UNIT_SPEC_ID_ENTRY 0x0000609e
  659. #define SBP2_SW_VERSION_ENTRY 0x00010483
  660. static const struct fw_device_id sbp2_id_table[] = {
  661. {
  662. .match_flags = FW_MATCH_SPECIFIER_ID | FW_MATCH_VERSION,
  663. .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY,
  664. .version = SBP2_SW_VERSION_ENTRY,
  665. },
  666. { }
  667. };
  668. static struct fw_driver sbp2_driver = {
  669. .driver = {
  670. .owner = THIS_MODULE,
  671. .name = sbp2_driver_name,
  672. .bus = &fw_bus_type,
  673. .probe = sbp2_probe,
  674. .remove = sbp2_remove,
  675. },
  676. .update = sbp2_update,
  677. .id_table = sbp2_id_table,
  678. };
  679. static unsigned int
  680. sbp2_status_to_sense_data(u8 *sbp2_status, u8 *sense_data)
  681. {
  682. int sam_status;
  683. sense_data[0] = 0x70;
  684. sense_data[1] = 0x0;
  685. sense_data[2] = sbp2_status[1];
  686. sense_data[3] = sbp2_status[4];
  687. sense_data[4] = sbp2_status[5];
  688. sense_data[5] = sbp2_status[6];
  689. sense_data[6] = sbp2_status[7];
  690. sense_data[7] = 10;
  691. sense_data[8] = sbp2_status[8];
  692. sense_data[9] = sbp2_status[9];
  693. sense_data[10] = sbp2_status[10];
  694. sense_data[11] = sbp2_status[11];
  695. sense_data[12] = sbp2_status[2];
  696. sense_data[13] = sbp2_status[3];
  697. sense_data[14] = sbp2_status[12];
  698. sense_data[15] = sbp2_status[13];
  699. sam_status = sbp2_status[0] & 0x3f;
  700. switch (sam_status) {
  701. case SAM_STAT_GOOD:
  702. case SAM_STAT_CHECK_CONDITION:
  703. case SAM_STAT_CONDITION_MET:
  704. case SAM_STAT_BUSY:
  705. case SAM_STAT_RESERVATION_CONFLICT:
  706. case SAM_STAT_COMMAND_TERMINATED:
  707. return DID_OK << 16 | sam_status;
  708. default:
  709. return DID_ERROR << 16;
  710. }
  711. }
  712. static void
  713. complete_command_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
  714. {
  715. struct sbp2_command_orb *orb = (struct sbp2_command_orb *)base_orb;
  716. struct fw_unit *unit = orb->unit;
  717. struct fw_device *device = fw_device(unit->device.parent);
  718. struct scatterlist *sg;
  719. int result;
  720. if (status != NULL) {
  721. if (STATUS_GET_DEAD(*status))
  722. sbp2_agent_reset(unit);
  723. switch (STATUS_GET_RESPONSE(*status)) {
  724. case SBP2_STATUS_REQUEST_COMPLETE:
  725. result = DID_OK << 16;
  726. break;
  727. case SBP2_STATUS_TRANSPORT_FAILURE:
  728. result = DID_BUS_BUSY << 16;
  729. break;
  730. case SBP2_STATUS_ILLEGAL_REQUEST:
  731. case SBP2_STATUS_VENDOR_DEPENDENT:
  732. default:
  733. result = DID_ERROR << 16;
  734. break;
  735. }
  736. if (result == DID_OK << 16 && STATUS_GET_LEN(*status) > 1)
  737. result = sbp2_status_to_sense_data(STATUS_GET_DATA(*status),
  738. orb->cmd->sense_buffer);
  739. } else {
  740. /*
  741. * If the orb completes with status == NULL, something
  742. * went wrong, typically a bus reset happened mid-orb
  743. * or when sending the write (less likely).
  744. */
  745. result = DID_BUS_BUSY << 16;
  746. }
  747. dma_unmap_single(device->card->device, orb->base.request_bus,
  748. sizeof(orb->request), DMA_TO_DEVICE);
  749. if (orb->cmd->use_sg > 0) {
  750. sg = (struct scatterlist *)orb->cmd->request_buffer;
  751. dma_unmap_sg(device->card->device, sg, orb->cmd->use_sg,
  752. orb->cmd->sc_data_direction);
  753. }
  754. if (orb->page_table_bus != 0)
  755. dma_unmap_single(device->card->device, orb->page_table_bus,
  756. sizeof(orb->page_table_bus), DMA_TO_DEVICE);
  757. if (orb->request_buffer_bus != 0)
  758. dma_unmap_single(device->card->device, orb->request_buffer_bus,
  759. sizeof(orb->request_buffer_bus),
  760. DMA_FROM_DEVICE);
  761. orb->cmd->result = result;
  762. orb->done(orb->cmd);
  763. kfree(orb);
  764. }
  765. static int sbp2_command_orb_map_scatterlist(struct sbp2_command_orb *orb)
  766. {
  767. struct sbp2_device *sd =
  768. (struct sbp2_device *)orb->cmd->device->host->hostdata;
  769. struct fw_unit *unit = sd->unit;
  770. struct fw_device *device = fw_device(unit->device.parent);
  771. struct scatterlist *sg;
  772. int sg_len, l, i, j, count;
  773. size_t size;
  774. dma_addr_t sg_addr;
  775. sg = (struct scatterlist *)orb->cmd->request_buffer;
  776. count = dma_map_sg(device->card->device, sg, orb->cmd->use_sg,
  777. orb->cmd->sc_data_direction);
  778. if (count == 0)
  779. goto fail;
  780. /*
  781. * Handle the special case where there is only one element in
  782. * the scatter list by converting it to an immediate block
  783. * request. This is also a workaround for broken devices such
  784. * as the second generation iPod which doesn't support page
  785. * tables.
  786. */
  787. if (count == 1 && sg_dma_len(sg) < SBP2_MAX_SG_ELEMENT_LENGTH) {
  788. orb->request.data_descriptor.high = sd->address_high;
  789. orb->request.data_descriptor.low = sg_dma_address(sg);
  790. orb->request.misc |=
  791. COMMAND_ORB_DATA_SIZE(sg_dma_len(sg));
  792. return 0;
  793. }
  794. /*
  795. * Convert the scatterlist to an sbp2 page table. If any
  796. * scatterlist entries are too big for sbp2, we split them as we
  797. * go. Even if we ask the block I/O layer to not give us sg
  798. * elements larger than 65535 bytes, some IOMMUs may merge sg elements
  799. * during DMA mapping, and Linux currently doesn't prevent this.
  800. */
  801. for (i = 0, j = 0; i < count; i++) {
  802. sg_len = sg_dma_len(sg + i);
  803. sg_addr = sg_dma_address(sg + i);
  804. while (sg_len) {
  805. l = min(sg_len, SBP2_MAX_SG_ELEMENT_LENGTH);
  806. orb->page_table[j].low = sg_addr;
  807. orb->page_table[j].high = (l << 16);
  808. sg_addr += l;
  809. sg_len -= l;
  810. j++;
  811. }
  812. }
  813. size = sizeof(orb->page_table[0]) * j;
  814. /*
  815. * The data_descriptor pointer is the one case where we need
  816. * to fill in the node ID part of the address. All other
  817. * pointers assume that the data referenced reside on the
  818. * initiator (i.e. us), but data_descriptor can refer to data
  819. * on other nodes so we need to put our ID in descriptor.high.
  820. */
  821. orb->page_table_bus =
  822. dma_map_single(device->card->device, orb->page_table,
  823. size, DMA_TO_DEVICE);
  824. if (dma_mapping_error(orb->page_table_bus))
  825. goto fail_page_table;
  826. orb->request.data_descriptor.high = sd->address_high;
  827. orb->request.data_descriptor.low = orb->page_table_bus;
  828. orb->request.misc |=
  829. COMMAND_ORB_PAGE_TABLE_PRESENT |
  830. COMMAND_ORB_DATA_SIZE(j);
  831. fw_memcpy_to_be32(orb->page_table, orb->page_table, size);
  832. return 0;
  833. fail_page_table:
  834. dma_unmap_sg(device->card->device, sg, orb->cmd->use_sg,
  835. orb->cmd->sc_data_direction);
  836. fail:
  837. return -ENOMEM;
  838. }
  839. /* SCSI stack integration */
  840. static int sbp2_scsi_queuecommand(struct scsi_cmnd *cmd, scsi_done_fn_t done)
  841. {
  842. struct sbp2_device *sd =
  843. (struct sbp2_device *)cmd->device->host->hostdata;
  844. struct fw_unit *unit = sd->unit;
  845. struct fw_device *device = fw_device(unit->device.parent);
  846. struct sbp2_command_orb *orb;
  847. /*
  848. * Bidirectional commands are not yet implemented, and unknown
  849. * transfer direction not handled.
  850. */
  851. if (cmd->sc_data_direction == DMA_BIDIRECTIONAL) {
  852. fw_error("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
  853. cmd->result = DID_ERROR << 16;
  854. done(cmd);
  855. return 0;
  856. }
  857. orb = kzalloc(sizeof(*orb), GFP_ATOMIC);
  858. if (orb == NULL) {
  859. fw_notify("failed to alloc orb\n");
  860. goto fail_alloc;
  861. }
  862. /* Initialize rcode to something not RCODE_COMPLETE. */
  863. orb->base.rcode = -1;
  864. orb->base.request_bus =
  865. dma_map_single(device->card->device, &orb->request,
  866. sizeof(orb->request), DMA_TO_DEVICE);
  867. if (dma_mapping_error(orb->base.request_bus))
  868. goto fail_mapping;
  869. orb->unit = unit;
  870. orb->done = done;
  871. orb->cmd = cmd;
  872. orb->request.next.high = SBP2_ORB_NULL;
  873. orb->request.next.low = 0x0;
  874. /*
  875. * At speed 100 we can do 512 bytes per packet, at speed 200,
  876. * 1024 bytes per packet etc. The SBP-2 max_payload field
  877. * specifies the max payload size as 2 ^ (max_payload + 2), so
  878. * if we set this to max_speed + 7, we get the right value.
  879. */
  880. orb->request.misc =
  881. COMMAND_ORB_MAX_PAYLOAD(device->node->max_speed + 7) |
  882. COMMAND_ORB_SPEED(device->node->max_speed) |
  883. COMMAND_ORB_NOTIFY;
  884. if (cmd->sc_data_direction == DMA_FROM_DEVICE)
  885. orb->request.misc |=
  886. COMMAND_ORB_DIRECTION(SBP2_DIRECTION_FROM_MEDIA);
  887. else if (cmd->sc_data_direction == DMA_TO_DEVICE)
  888. orb->request.misc |=
  889. COMMAND_ORB_DIRECTION(SBP2_DIRECTION_TO_MEDIA);
  890. if (cmd->use_sg && sbp2_command_orb_map_scatterlist(orb) < 0)
  891. goto fail_map_payload;
  892. fw_memcpy_to_be32(&orb->request, &orb->request, sizeof(orb->request));
  893. memset(orb->request.command_block,
  894. 0, sizeof(orb->request.command_block));
  895. memcpy(orb->request.command_block, cmd->cmnd, COMMAND_SIZE(*cmd->cmnd));
  896. orb->base.callback = complete_command_orb;
  897. sbp2_send_orb(&orb->base, unit, sd->node_id, sd->generation,
  898. sd->command_block_agent_address + SBP2_ORB_POINTER);
  899. return 0;
  900. fail_map_payload:
  901. dma_unmap_single(device->card->device, orb->base.request_bus,
  902. sizeof(orb->request), DMA_TO_DEVICE);
  903. fail_mapping:
  904. kfree(orb);
  905. fail_alloc:
  906. return SCSI_MLQUEUE_HOST_BUSY;
  907. }
  908. static int sbp2_scsi_slave_alloc(struct scsi_device *sdev)
  909. {
  910. struct sbp2_device *sd = (struct sbp2_device *)sdev->host->hostdata;
  911. sdev->allow_restart = 1;
  912. if (sd->workarounds & SBP2_WORKAROUND_INQUIRY_36)
  913. sdev->inquiry_len = 36;
  914. return 0;
  915. }
  916. static int sbp2_scsi_slave_configure(struct scsi_device *sdev)
  917. {
  918. struct sbp2_device *sd = (struct sbp2_device *)sdev->host->hostdata;
  919. struct fw_unit *unit = sd->unit;
  920. sdev->use_10_for_rw = 1;
  921. if (sdev->type == TYPE_ROM)
  922. sdev->use_10_for_ms = 1;
  923. if (sdev->type == TYPE_DISK &&
  924. sd->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
  925. sdev->skip_ms_page_8 = 1;
  926. if (sd->workarounds & SBP2_WORKAROUND_FIX_CAPACITY) {
  927. fw_notify("setting fix_capacity for %s\n", unit->device.bus_id);
  928. sdev->fix_capacity = 1;
  929. }
  930. return 0;
  931. }
  932. /*
  933. * Called by scsi stack when something has really gone wrong. Usually
  934. * called when a command has timed-out for some reason.
  935. */
  936. static int sbp2_scsi_abort(struct scsi_cmnd *cmd)
  937. {
  938. struct sbp2_device *sd =
  939. (struct sbp2_device *)cmd->device->host->hostdata;
  940. struct fw_unit *unit = sd->unit;
  941. fw_notify("sbp2_scsi_abort\n");
  942. sbp2_agent_reset(unit);
  943. sbp2_cancel_orbs(unit);
  944. return SUCCESS;
  945. }
  946. static struct scsi_host_template scsi_driver_template = {
  947. .module = THIS_MODULE,
  948. .name = "SBP-2 IEEE-1394",
  949. .proc_name = (char *)sbp2_driver_name,
  950. .queuecommand = sbp2_scsi_queuecommand,
  951. .slave_alloc = sbp2_scsi_slave_alloc,
  952. .slave_configure = sbp2_scsi_slave_configure,
  953. .eh_abort_handler = sbp2_scsi_abort,
  954. .this_id = -1,
  955. .sg_tablesize = SG_ALL,
  956. .use_clustering = ENABLE_CLUSTERING,
  957. .cmd_per_lun = 1,
  958. .can_queue = 1,
  959. };
  960. MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
  961. MODULE_DESCRIPTION("SCSI over IEEE1394");
  962. MODULE_LICENSE("GPL");
  963. MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
  964. /* Provide a module alias so root-on-sbp2 initrds don't break. */
  965. #ifndef CONFIG_IEEE1394_SBP2_MODULE
  966. MODULE_ALIAS("sbp2");
  967. #endif
  968. static int __init sbp2_init(void)
  969. {
  970. return driver_register(&sbp2_driver.driver);
  971. }
  972. static void __exit sbp2_cleanup(void)
  973. {
  974. driver_unregister(&sbp2_driver.driver);
  975. }
  976. module_init(sbp2_init);
  977. module_exit(sbp2_cleanup);