fw-sbp2.c 32 KB

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