fw-sbp2.c 33 KB

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