fw-sbp2.c 30 KB

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