fw-sbp2.c 31 KB

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