fw-sbp2.c 33 KB

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