zfcp_scsi.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. /*
  2. *
  3. * linux/drivers/s390/scsi/zfcp_scsi.c
  4. *
  5. * FCP adapter driver for IBM eServer zSeries
  6. *
  7. * (C) Copyright IBM Corp. 2002, 2004
  8. *
  9. * Author(s): Martin Peschke <mpeschke@de.ibm.com>
  10. * Raimund Schroeder <raimund.schroeder@de.ibm.com>
  11. * Aron Zeh
  12. * Wolfgang Taphorn
  13. * Stefan Bader <stefan.bader@de.ibm.com>
  14. * Heiko Carstens <heiko.carstens@de.ibm.com>
  15. * Andreas Herrmann <aherrman@de.ibm.com>
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2, or (at your option)
  20. * any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  30. */
  31. #define ZFCP_LOG_AREA ZFCP_LOG_AREA_SCSI
  32. #define ZFCP_SCSI_REVISION "$Revision: 1.74 $"
  33. #include "zfcp_ext.h"
  34. static void zfcp_scsi_slave_destroy(struct scsi_device *sdp);
  35. static int zfcp_scsi_slave_alloc(struct scsi_device *sdp);
  36. static int zfcp_scsi_slave_configure(struct scsi_device *sdp);
  37. static int zfcp_scsi_queuecommand(struct scsi_cmnd *,
  38. void (*done) (struct scsi_cmnd *));
  39. static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *);
  40. static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *);
  41. static int zfcp_scsi_eh_bus_reset_handler(struct scsi_cmnd *);
  42. static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *);
  43. static int zfcp_task_management_function(struct zfcp_unit *, u8);
  44. static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *, int, scsi_id_t,
  45. scsi_lun_t);
  46. static struct zfcp_port *zfcp_port_lookup(struct zfcp_adapter *, int,
  47. scsi_id_t);
  48. static struct device_attribute *zfcp_sysfs_sdev_attrs[];
  49. struct scsi_transport_template *zfcp_transport_template;
  50. struct zfcp_data zfcp_data = {
  51. .scsi_host_template = {
  52. name: ZFCP_NAME,
  53. proc_name: "zfcp",
  54. proc_info: NULL,
  55. detect: NULL,
  56. slave_alloc: zfcp_scsi_slave_alloc,
  57. slave_configure: zfcp_scsi_slave_configure,
  58. slave_destroy: zfcp_scsi_slave_destroy,
  59. queuecommand: zfcp_scsi_queuecommand,
  60. eh_abort_handler: zfcp_scsi_eh_abort_handler,
  61. eh_device_reset_handler: zfcp_scsi_eh_device_reset_handler,
  62. eh_bus_reset_handler: zfcp_scsi_eh_bus_reset_handler,
  63. eh_host_reset_handler: zfcp_scsi_eh_host_reset_handler,
  64. /* FIXME(openfcp): Tune */
  65. can_queue: 4096,
  66. this_id: 0,
  67. /*
  68. * FIXME:
  69. * one less? can zfcp_create_sbale cope with it?
  70. */
  71. sg_tablesize: ZFCP_MAX_SBALES_PER_REQ,
  72. cmd_per_lun: 1,
  73. unchecked_isa_dma: 0,
  74. use_clustering: 1,
  75. sdev_attrs: zfcp_sysfs_sdev_attrs,
  76. },
  77. .driver_version = ZFCP_VERSION,
  78. /* rest initialised with zeros */
  79. };
  80. /* Find start of Response Information in FCP response unit*/
  81. char *
  82. zfcp_get_fcp_rsp_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
  83. {
  84. char *fcp_rsp_info_ptr;
  85. fcp_rsp_info_ptr =
  86. (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
  87. return fcp_rsp_info_ptr;
  88. }
  89. /* Find start of Sense Information in FCP response unit*/
  90. char *
  91. zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
  92. {
  93. char *fcp_sns_info_ptr;
  94. fcp_sns_info_ptr =
  95. (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
  96. if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
  97. fcp_sns_info_ptr = (char *) fcp_sns_info_ptr +
  98. fcp_rsp_iu->fcp_rsp_len;
  99. return fcp_sns_info_ptr;
  100. }
  101. fcp_dl_t *
  102. zfcp_get_fcp_dl_ptr(struct fcp_cmnd_iu * fcp_cmd)
  103. {
  104. int additional_length = fcp_cmd->add_fcp_cdb_length << 2;
  105. fcp_dl_t *fcp_dl_addr;
  106. fcp_dl_addr = (fcp_dl_t *)
  107. ((unsigned char *) fcp_cmd +
  108. sizeof (struct fcp_cmnd_iu) + additional_length);
  109. /*
  110. * fcp_dl_addr = start address of fcp_cmnd structure +
  111. * size of fixed part + size of dynamically sized add_dcp_cdb field
  112. * SEE FCP-2 documentation
  113. */
  114. return fcp_dl_addr;
  115. }
  116. fcp_dl_t
  117. zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
  118. {
  119. return *zfcp_get_fcp_dl_ptr(fcp_cmd);
  120. }
  121. void
  122. zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
  123. {
  124. *zfcp_get_fcp_dl_ptr(fcp_cmd) = fcp_dl;
  125. }
  126. /*
  127. * note: it's a bit-or operation not an assignment
  128. * regarding the specified byte
  129. */
  130. static inline void
  131. set_byte(u32 * result, char status, char pos)
  132. {
  133. *result |= status << (pos * 8);
  134. }
  135. void
  136. set_host_byte(u32 * result, char status)
  137. {
  138. set_byte(result, status, 2);
  139. }
  140. void
  141. set_driver_byte(u32 * result, char status)
  142. {
  143. set_byte(result, status, 3);
  144. }
  145. /*
  146. * function: zfcp_scsi_slave_alloc
  147. *
  148. * purpose:
  149. *
  150. * returns:
  151. */
  152. static int
  153. zfcp_scsi_slave_alloc(struct scsi_device *sdp)
  154. {
  155. struct zfcp_adapter *adapter;
  156. struct zfcp_unit *unit;
  157. unsigned long flags;
  158. int retval = -ENODEV;
  159. adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
  160. if (!adapter)
  161. goto out;
  162. read_lock_irqsave(&zfcp_data.config_lock, flags);
  163. unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
  164. if (unit) {
  165. sdp->hostdata = unit;
  166. unit->device = sdp;
  167. zfcp_unit_get(unit);
  168. retval = 0;
  169. }
  170. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  171. out:
  172. return retval;
  173. }
  174. /*
  175. * function: zfcp_scsi_slave_destroy
  176. *
  177. * purpose:
  178. *
  179. * returns:
  180. */
  181. static void
  182. zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
  183. {
  184. struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
  185. if (unit) {
  186. sdpnt->hostdata = NULL;
  187. unit->device = NULL;
  188. zfcp_unit_put(unit);
  189. } else {
  190. ZFCP_LOG_NORMAL("bug: no unit associated with SCSI device at "
  191. "address %p\n", sdpnt);
  192. }
  193. }
  194. /*
  195. * called from scsi midlayer to allow finetuning of a device.
  196. */
  197. static int
  198. zfcp_scsi_slave_configure(struct scsi_device *sdp)
  199. {
  200. if (sdp->tagged_supported)
  201. scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
  202. else
  203. scsi_adjust_queue_depth(sdp, 0, 1);
  204. return 0;
  205. }
  206. /**
  207. * zfcp_scsi_command_fail - set result in scsi_cmnd and call scsi_done function
  208. * @scpnt: pointer to struct scsi_cmnd where result is set
  209. * @result: result to be set in scpnt (e.g. DID_ERROR)
  210. */
  211. static void
  212. zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
  213. {
  214. set_host_byte(&scpnt->result, result);
  215. zfcp_cmd_dbf_event_scsi("failing", scpnt);
  216. /* return directly */
  217. scpnt->scsi_done(scpnt);
  218. }
  219. /**
  220. * zfcp_scsi_command_async - worker for zfcp_scsi_queuecommand and
  221. * zfcp_scsi_command_sync
  222. * @adapter: adapter where scsi command is issued
  223. * @unit: unit to which scsi command is sent
  224. * @scpnt: scsi command to be sent
  225. * @timer: timer to be started if request is successfully initiated
  226. *
  227. * Note: In scsi_done function must be set in scpnt.
  228. */
  229. int
  230. zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
  231. struct scsi_cmnd *scpnt, struct timer_list *timer)
  232. {
  233. int tmp;
  234. int retval;
  235. retval = 0;
  236. BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
  237. BUG_ON(scpnt->scsi_done == NULL);
  238. if (unlikely(NULL == unit)) {
  239. zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
  240. goto out;
  241. }
  242. if (unlikely(
  243. atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status) ||
  244. !atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status))) {
  245. ZFCP_LOG_DEBUG("stopping SCSI I/O on unit 0x%016Lx on port "
  246. "0x%016Lx on adapter %s\n",
  247. unit->fcp_lun, unit->port->wwpn,
  248. zfcp_get_busid_by_adapter(adapter));
  249. zfcp_scsi_command_fail(scpnt, DID_ERROR);
  250. goto out;
  251. }
  252. if (unlikely(
  253. !atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))) {
  254. ZFCP_LOG_DEBUG("adapter %s not ready or unit 0x%016Lx "
  255. "on port 0x%016Lx in recovery\n",
  256. zfcp_get_busid_by_unit(unit),
  257. unit->fcp_lun, unit->port->wwpn);
  258. retval = SCSI_MLQUEUE_DEVICE_BUSY;
  259. goto out;
  260. }
  261. tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, timer,
  262. ZFCP_REQ_AUTO_CLEANUP);
  263. if (unlikely(tmp < 0)) {
  264. ZFCP_LOG_DEBUG("error: initiation of Send FCP Cmnd failed\n");
  265. retval = SCSI_MLQUEUE_HOST_BUSY;
  266. }
  267. out:
  268. return retval;
  269. }
  270. void
  271. zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
  272. {
  273. struct completion *wait = (struct completion *) scpnt->SCp.ptr;
  274. complete(wait);
  275. }
  276. /**
  277. * zfcp_scsi_command_sync - send a SCSI command and wait for completion
  278. * @unit: unit where command is sent to
  279. * @scpnt: scsi command to be sent
  280. * @timer: timer to be started if request is successfully initiated
  281. * Return: 0
  282. *
  283. * Errors are indicated in scpnt->result
  284. */
  285. int
  286. zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
  287. struct timer_list *timer)
  288. {
  289. int ret;
  290. DECLARE_COMPLETION(wait);
  291. scpnt->SCp.ptr = (void *) &wait; /* silent re-use */
  292. scpnt->scsi_done = zfcp_scsi_command_sync_handler;
  293. ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt, timer);
  294. if (ret == 0)
  295. wait_for_completion(&wait);
  296. scpnt->SCp.ptr = NULL;
  297. return 0;
  298. }
  299. /*
  300. * function: zfcp_scsi_queuecommand
  301. *
  302. * purpose: enqueues a SCSI command to the specified target device
  303. *
  304. * returns: 0 - success, SCSI command enqueued
  305. * !0 - failure
  306. */
  307. int
  308. zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
  309. void (*done) (struct scsi_cmnd *))
  310. {
  311. struct zfcp_unit *unit;
  312. struct zfcp_adapter *adapter;
  313. /* reset the status for this request */
  314. scpnt->result = 0;
  315. scpnt->host_scribble = NULL;
  316. scpnt->scsi_done = done;
  317. /*
  318. * figure out adapter and target device
  319. * (stored there by zfcp_scsi_slave_alloc)
  320. */
  321. adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
  322. unit = (struct zfcp_unit *) scpnt->device->hostdata;
  323. return zfcp_scsi_command_async(adapter, unit, scpnt, NULL);
  324. }
  325. /*
  326. * function: zfcp_unit_lookup
  327. *
  328. * purpose:
  329. *
  330. * returns:
  331. *
  332. * context:
  333. */
  334. static struct zfcp_unit *
  335. zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, scsi_id_t id,
  336. scsi_lun_t lun)
  337. {
  338. struct zfcp_port *port;
  339. struct zfcp_unit *unit, *retval = NULL;
  340. list_for_each_entry(port, &adapter->port_list_head, list) {
  341. if (!port->rport || (id != port->rport->scsi_target_id))
  342. continue;
  343. list_for_each_entry(unit, &port->unit_list_head, list) {
  344. if (lun == unit->scsi_lun) {
  345. retval = unit;
  346. goto out;
  347. }
  348. }
  349. }
  350. out:
  351. return retval;
  352. }
  353. static struct zfcp_port *
  354. zfcp_port_lookup(struct zfcp_adapter *adapter, int channel, scsi_id_t id)
  355. {
  356. struct zfcp_port *port;
  357. list_for_each_entry(port, &adapter->port_list_head, list) {
  358. if (port->rport && (id == port->rport->scsi_target_id))
  359. return port;
  360. }
  361. return (struct zfcp_port *) NULL;
  362. }
  363. /*
  364. * function: zfcp_scsi_eh_abort_handler
  365. *
  366. * purpose: tries to abort the specified (timed out) SCSI command
  367. *
  368. * note: We do not need to care for a SCSI command which completes
  369. * normally but late during this abort routine runs.
  370. * We are allowed to return late commands to the SCSI stack.
  371. * It tracks the state of commands and will handle late commands.
  372. * (Usually, the normal completion of late commands is ignored with
  373. * respect to the running abort operation. Grep for 'done_late'
  374. * in the SCSI stacks sources.)
  375. *
  376. * returns: SUCCESS - command has been aborted and cleaned up in internal
  377. * bookkeeping,
  378. * SCSI stack won't be called for aborted command
  379. * FAILED - otherwise
  380. */
  381. int
  382. __zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
  383. {
  384. int retval = SUCCESS;
  385. struct zfcp_fsf_req *new_fsf_req, *old_fsf_req;
  386. struct zfcp_adapter *adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
  387. struct zfcp_unit *unit = (struct zfcp_unit *) scpnt->device->hostdata;
  388. struct zfcp_port *port = unit->port;
  389. struct Scsi_Host *scsi_host = scpnt->device->host;
  390. union zfcp_req_data *req_data = NULL;
  391. unsigned long flags;
  392. u32 status = 0;
  393. /* the components of a abort_dbf record (fixed size record) */
  394. u64 dbf_scsi_cmnd = (unsigned long) scpnt;
  395. char dbf_opcode[ZFCP_ABORT_DBF_LENGTH];
  396. wwn_t dbf_wwn = port->wwpn;
  397. fcp_lun_t dbf_fcp_lun = unit->fcp_lun;
  398. u64 dbf_retries = scpnt->retries;
  399. u64 dbf_allowed = scpnt->allowed;
  400. u64 dbf_timeout = 0;
  401. u64 dbf_fsf_req = 0;
  402. u64 dbf_fsf_status = 0;
  403. u64 dbf_fsf_qual[2] = { 0, 0 };
  404. char dbf_result[ZFCP_ABORT_DBF_LENGTH] = "##undef";
  405. memset(dbf_opcode, 0, ZFCP_ABORT_DBF_LENGTH);
  406. memcpy(dbf_opcode,
  407. scpnt->cmnd,
  408. min(scpnt->cmd_len, (unsigned char) ZFCP_ABORT_DBF_LENGTH));
  409. ZFCP_LOG_INFO("aborting scsi_cmnd=%p on adapter %s\n",
  410. scpnt, zfcp_get_busid_by_adapter(adapter));
  411. spin_unlock_irq(scsi_host->host_lock);
  412. /*
  413. * Race condition between normal (late) completion and abort has
  414. * to be avoided.
  415. * The entirity of all accesses to scsi_req have to be atomic.
  416. * scsi_req is usually part of the fsf_req and thus we block the
  417. * release of fsf_req as long as we need to access scsi_req.
  418. */
  419. write_lock_irqsave(&adapter->abort_lock, flags);
  420. /*
  421. * Check whether command has just completed and can not be aborted.
  422. * Even if the command has just been completed late, we can access
  423. * scpnt since the SCSI stack does not release it at least until
  424. * this routine returns. (scpnt is parameter passed to this routine
  425. * and must not disappear during abort even on late completion.)
  426. */
  427. req_data = (union zfcp_req_data *) scpnt->host_scribble;
  428. /* DEBUG */
  429. ZFCP_LOG_DEBUG("req_data=%p\n", req_data);
  430. if (!req_data) {
  431. ZFCP_LOG_DEBUG("late command completion overtook abort\n");
  432. /*
  433. * That's it.
  434. * Do not initiate abort but return SUCCESS.
  435. */
  436. write_unlock_irqrestore(&adapter->abort_lock, flags);
  437. retval = SUCCESS;
  438. strncpy(dbf_result, "##late1", ZFCP_ABORT_DBF_LENGTH);
  439. goto out;
  440. }
  441. /* Figure out which fsf_req needs to be aborted. */
  442. old_fsf_req = req_data->send_fcp_command_task.fsf_req;
  443. dbf_fsf_req = (unsigned long) old_fsf_req;
  444. dbf_timeout =
  445. (jiffies - req_data->send_fcp_command_task.start_jiffies) / HZ;
  446. ZFCP_LOG_DEBUG("old_fsf_req=%p\n", old_fsf_req);
  447. if (!old_fsf_req) {
  448. write_unlock_irqrestore(&adapter->abort_lock, flags);
  449. ZFCP_LOG_NORMAL("bug: no old fsf request found\n");
  450. ZFCP_LOG_NORMAL("req_data:\n");
  451. ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_NORMAL,
  452. (char *) req_data, sizeof (union zfcp_req_data));
  453. ZFCP_LOG_NORMAL("scsi_cmnd:\n");
  454. ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_NORMAL,
  455. (char *) scpnt, sizeof (struct scsi_cmnd));
  456. retval = FAILED;
  457. strncpy(dbf_result, "##bug:r", ZFCP_ABORT_DBF_LENGTH);
  458. goto out;
  459. }
  460. old_fsf_req->data.send_fcp_command_task.scsi_cmnd = NULL;
  461. /* mark old request as being aborted */
  462. old_fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
  463. /*
  464. * We have to collect all information (e.g. unit) needed by
  465. * zfcp_fsf_abort_fcp_command before calling that routine
  466. * since that routine is not allowed to access
  467. * fsf_req which it is going to abort.
  468. * This is because of we need to release fsf_req_list_lock
  469. * before calling zfcp_fsf_abort_fcp_command.
  470. * Since this lock will not be held, fsf_req may complete
  471. * late and may be released meanwhile.
  472. */
  473. ZFCP_LOG_DEBUG("unit 0x%016Lx (%p)\n", unit->fcp_lun, unit);
  474. /*
  475. * We block (call schedule)
  476. * That's why we must release the lock and enable the
  477. * interrupts before.
  478. * On the other hand we do not need the lock anymore since
  479. * all critical accesses to scsi_req are done.
  480. */
  481. write_unlock_irqrestore(&adapter->abort_lock, flags);
  482. /* call FSF routine which does the abort */
  483. new_fsf_req = zfcp_fsf_abort_fcp_command((unsigned long) old_fsf_req,
  484. adapter, unit, 0);
  485. ZFCP_LOG_DEBUG("new_fsf_req=%p\n", new_fsf_req);
  486. if (!new_fsf_req) {
  487. retval = FAILED;
  488. ZFCP_LOG_NORMAL("error: initiation of Abort FCP Cmnd "
  489. "failed\n");
  490. strncpy(dbf_result, "##nores", ZFCP_ABORT_DBF_LENGTH);
  491. goto out;
  492. }
  493. /* wait for completion of abort */
  494. ZFCP_LOG_DEBUG("waiting for cleanup...\n");
  495. #if 1
  496. /*
  497. * FIXME:
  498. * copying zfcp_fsf_req_wait_and_cleanup code is not really nice
  499. */
  500. __wait_event(new_fsf_req->completion_wq,
  501. new_fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
  502. status = new_fsf_req->status;
  503. dbf_fsf_status = new_fsf_req->qtcb->header.fsf_status;
  504. /*
  505. * Ralphs special debug load provides timestamps in the FSF
  506. * status qualifier. This might be specified later if being
  507. * useful for debugging aborts.
  508. */
  509. dbf_fsf_qual[0] =
  510. *(u64 *) & new_fsf_req->qtcb->header.fsf_status_qual.word[0];
  511. dbf_fsf_qual[1] =
  512. *(u64 *) & new_fsf_req->qtcb->header.fsf_status_qual.word[2];
  513. zfcp_fsf_req_free(new_fsf_req);
  514. #else
  515. retval = zfcp_fsf_req_wait_and_cleanup(new_fsf_req,
  516. ZFCP_UNINTERRUPTIBLE, &status);
  517. #endif
  518. ZFCP_LOG_DEBUG("Waiting for cleanup complete, status=0x%x\n", status);
  519. /* status should be valid since signals were not permitted */
  520. if (status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
  521. retval = SUCCESS;
  522. strncpy(dbf_result, "##succ", ZFCP_ABORT_DBF_LENGTH);
  523. } else if (status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
  524. retval = SUCCESS;
  525. strncpy(dbf_result, "##late2", ZFCP_ABORT_DBF_LENGTH);
  526. } else {
  527. retval = FAILED;
  528. strncpy(dbf_result, "##fail", ZFCP_ABORT_DBF_LENGTH);
  529. }
  530. out:
  531. debug_event(adapter->abort_dbf, 1, &dbf_scsi_cmnd, sizeof (u64));
  532. debug_event(adapter->abort_dbf, 1, &dbf_opcode, ZFCP_ABORT_DBF_LENGTH);
  533. debug_event(adapter->abort_dbf, 1, &dbf_wwn, sizeof (wwn_t));
  534. debug_event(adapter->abort_dbf, 1, &dbf_fcp_lun, sizeof (fcp_lun_t));
  535. debug_event(adapter->abort_dbf, 1, &dbf_retries, sizeof (u64));
  536. debug_event(adapter->abort_dbf, 1, &dbf_allowed, sizeof (u64));
  537. debug_event(adapter->abort_dbf, 1, &dbf_timeout, sizeof (u64));
  538. debug_event(adapter->abort_dbf, 1, &dbf_fsf_req, sizeof (u64));
  539. debug_event(adapter->abort_dbf, 1, &dbf_fsf_status, sizeof (u64));
  540. debug_event(adapter->abort_dbf, 1, &dbf_fsf_qual[0], sizeof (u64));
  541. debug_event(adapter->abort_dbf, 1, &dbf_fsf_qual[1], sizeof (u64));
  542. debug_text_event(adapter->abort_dbf, 1, dbf_result);
  543. spin_lock_irq(scsi_host->host_lock);
  544. return retval;
  545. }
  546. int
  547. zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
  548. {
  549. int rc;
  550. struct Scsi_Host *scsi_host = scpnt->device->host;
  551. spin_lock_irq(scsi_host->host_lock);
  552. rc = __zfcp_scsi_eh_abort_handler(scpnt);
  553. spin_unlock_irq(scsi_host->host_lock);
  554. return rc;
  555. }
  556. /*
  557. * function: zfcp_scsi_eh_device_reset_handler
  558. *
  559. * purpose:
  560. *
  561. * returns:
  562. */
  563. int
  564. zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
  565. {
  566. int retval;
  567. struct zfcp_unit *unit = (struct zfcp_unit *) scpnt->device->hostdata;
  568. if (!unit) {
  569. ZFCP_LOG_NORMAL("bug: Tried reset for nonexistent unit\n");
  570. retval = SUCCESS;
  571. goto out;
  572. }
  573. ZFCP_LOG_NORMAL("resetting unit 0x%016Lx\n", unit->fcp_lun);
  574. /*
  575. * If we do not know whether the unit supports 'logical unit reset'
  576. * then try 'logical unit reset' and proceed with 'target reset'
  577. * if 'logical unit reset' fails.
  578. * If the unit is known not to support 'logical unit reset' then
  579. * skip 'logical unit reset' and try 'target reset' immediately.
  580. */
  581. if (!atomic_test_mask(ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
  582. &unit->status)) {
  583. retval =
  584. zfcp_task_management_function(unit, FCP_LOGICAL_UNIT_RESET);
  585. if (retval) {
  586. ZFCP_LOG_DEBUG("unit reset failed (unit=%p)\n", unit);
  587. if (retval == -ENOTSUPP)
  588. atomic_set_mask
  589. (ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
  590. &unit->status);
  591. /* fall through and try 'target reset' next */
  592. } else {
  593. ZFCP_LOG_DEBUG("unit reset succeeded (unit=%p)\n",
  594. unit);
  595. /* avoid 'target reset' */
  596. retval = SUCCESS;
  597. goto out;
  598. }
  599. }
  600. retval = zfcp_task_management_function(unit, FCP_TARGET_RESET);
  601. if (retval) {
  602. ZFCP_LOG_DEBUG("target reset failed (unit=%p)\n", unit);
  603. retval = FAILED;
  604. } else {
  605. ZFCP_LOG_DEBUG("target reset succeeded (unit=%p)\n", unit);
  606. retval = SUCCESS;
  607. }
  608. out:
  609. return retval;
  610. }
  611. static int
  612. zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags)
  613. {
  614. struct zfcp_adapter *adapter = unit->port->adapter;
  615. int retval;
  616. int status;
  617. struct zfcp_fsf_req *fsf_req;
  618. /* issue task management function */
  619. fsf_req = zfcp_fsf_send_fcp_command_task_management
  620. (adapter, unit, tm_flags, 0);
  621. if (!fsf_req) {
  622. ZFCP_LOG_INFO("error: creation of task management request "
  623. "failed for unit 0x%016Lx on port 0x%016Lx on "
  624. "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
  625. zfcp_get_busid_by_adapter(adapter));
  626. retval = -ENOMEM;
  627. goto out;
  628. }
  629. retval = zfcp_fsf_req_wait_and_cleanup(fsf_req,
  630. ZFCP_UNINTERRUPTIBLE, &status);
  631. /*
  632. * check completion status of task management function
  633. * (status should always be valid since no signals permitted)
  634. */
  635. if (status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED)
  636. retval = -EIO;
  637. else if (status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP)
  638. retval = -ENOTSUPP;
  639. else
  640. retval = 0;
  641. out:
  642. return retval;
  643. }
  644. /*
  645. * function: zfcp_scsi_eh_bus_reset_handler
  646. *
  647. * purpose:
  648. *
  649. * returns:
  650. */
  651. int
  652. zfcp_scsi_eh_bus_reset_handler(struct scsi_cmnd *scpnt)
  653. {
  654. int retval = 0;
  655. struct zfcp_unit *unit;
  656. unit = (struct zfcp_unit *) scpnt->device->hostdata;
  657. ZFCP_LOG_NORMAL("bus reset because of problems with "
  658. "unit 0x%016Lx\n", unit->fcp_lun);
  659. zfcp_erp_adapter_reopen(unit->port->adapter, 0);
  660. zfcp_erp_wait(unit->port->adapter);
  661. retval = SUCCESS;
  662. return retval;
  663. }
  664. /*
  665. * function: zfcp_scsi_eh_host_reset_handler
  666. *
  667. * purpose:
  668. *
  669. * returns:
  670. */
  671. int
  672. zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
  673. {
  674. int retval = 0;
  675. struct zfcp_unit *unit;
  676. unit = (struct zfcp_unit *) scpnt->device->hostdata;
  677. ZFCP_LOG_NORMAL("host reset because of problems with "
  678. "unit 0x%016Lx\n", unit->fcp_lun);
  679. zfcp_erp_adapter_reopen(unit->port->adapter, 0);
  680. zfcp_erp_wait(unit->port->adapter);
  681. retval = SUCCESS;
  682. return retval;
  683. }
  684. /*
  685. * function:
  686. *
  687. * purpose:
  688. *
  689. * returns:
  690. */
  691. int
  692. zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
  693. {
  694. int retval = 0;
  695. static unsigned int unique_id = 0;
  696. /* register adapter as SCSI host with mid layer of SCSI stack */
  697. adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
  698. sizeof (struct zfcp_adapter *));
  699. if (!adapter->scsi_host) {
  700. ZFCP_LOG_NORMAL("error: registration with SCSI stack failed "
  701. "for adapter %s ",
  702. zfcp_get_busid_by_adapter(adapter));
  703. retval = -EIO;
  704. goto out;
  705. }
  706. ZFCP_LOG_DEBUG("host registered, scsi_host=%p\n", adapter->scsi_host);
  707. /* tell the SCSI stack some characteristics of this adapter */
  708. adapter->scsi_host->max_id = 1;
  709. adapter->scsi_host->max_lun = 1;
  710. adapter->scsi_host->max_channel = 0;
  711. adapter->scsi_host->unique_id = unique_id++; /* FIXME */
  712. adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
  713. adapter->scsi_host->transportt = zfcp_transport_template;
  714. /*
  715. * Reverse mapping of the host number to avoid race condition
  716. */
  717. adapter->scsi_host_no = adapter->scsi_host->host_no;
  718. /*
  719. * save a pointer to our own adapter data structure within
  720. * hostdata field of SCSI host data structure
  721. */
  722. adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
  723. if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
  724. scsi_host_put(adapter->scsi_host);
  725. retval = -EIO;
  726. goto out;
  727. }
  728. atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
  729. out:
  730. return retval;
  731. }
  732. /*
  733. * function:
  734. *
  735. * purpose:
  736. *
  737. * returns:
  738. */
  739. void
  740. zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
  741. {
  742. struct Scsi_Host *shost;
  743. shost = adapter->scsi_host;
  744. if (!shost)
  745. return;
  746. fc_remove_host(shost);
  747. scsi_remove_host(shost);
  748. scsi_host_put(shost);
  749. adapter->scsi_host = NULL;
  750. adapter->scsi_host_no = 0;
  751. atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
  752. return;
  753. }
  754. void
  755. zfcp_fsf_start_scsi_er_timer(struct zfcp_adapter *adapter)
  756. {
  757. adapter->scsi_er_timer.function = zfcp_fsf_scsi_er_timeout_handler;
  758. adapter->scsi_er_timer.data = (unsigned long) adapter;
  759. adapter->scsi_er_timer.expires = jiffies + ZFCP_SCSI_ER_TIMEOUT;
  760. add_timer(&adapter->scsi_er_timer);
  761. }
  762. /*
  763. * Support functions for FC transport class
  764. */
  765. static void
  766. zfcp_get_port_id(struct scsi_target *starget)
  767. {
  768. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  769. struct zfcp_adapter *adapter = (struct zfcp_adapter *)shost->hostdata[0];
  770. struct zfcp_port *port;
  771. unsigned long flags;
  772. read_lock_irqsave(&zfcp_data.config_lock, flags);
  773. port = zfcp_port_lookup(adapter, starget->channel, starget->id);
  774. if (port)
  775. fc_starget_port_id(starget) = port->d_id;
  776. else
  777. fc_starget_port_id(starget) = -1;
  778. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  779. }
  780. static void
  781. zfcp_get_port_name(struct scsi_target *starget)
  782. {
  783. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  784. struct zfcp_adapter *adapter = (struct zfcp_adapter *)shost->hostdata[0];
  785. struct zfcp_port *port;
  786. unsigned long flags;
  787. read_lock_irqsave(&zfcp_data.config_lock, flags);
  788. port = zfcp_port_lookup(adapter, starget->channel, starget->id);
  789. if (port)
  790. fc_starget_port_name(starget) = port->wwpn;
  791. else
  792. fc_starget_port_name(starget) = -1;
  793. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  794. }
  795. static void
  796. zfcp_get_node_name(struct scsi_target *starget)
  797. {
  798. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  799. struct zfcp_adapter *adapter = (struct zfcp_adapter *)shost->hostdata[0];
  800. struct zfcp_port *port;
  801. unsigned long flags;
  802. read_lock_irqsave(&zfcp_data.config_lock, flags);
  803. port = zfcp_port_lookup(adapter, starget->channel, starget->id);
  804. if (port)
  805. fc_starget_node_name(starget) = port->wwnn;
  806. else
  807. fc_starget_node_name(starget) = -1;
  808. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  809. }
  810. void
  811. zfcp_set_fc_host_attrs(struct zfcp_adapter *adapter)
  812. {
  813. struct Scsi_Host *shost = adapter->scsi_host;
  814. fc_host_node_name(shost) = adapter->wwnn;
  815. fc_host_port_name(shost) = adapter->wwpn;
  816. strncpy(fc_host_serial_number(shost), adapter->serial_number,
  817. min(FC_SERIAL_NUMBER_SIZE, 32));
  818. fc_host_supported_classes(shost) = FC_COS_CLASS2 | FC_COS_CLASS3;
  819. }
  820. struct fc_function_template zfcp_transport_functions = {
  821. .get_starget_port_id = zfcp_get_port_id,
  822. .get_starget_port_name = zfcp_get_port_name,
  823. .get_starget_node_name = zfcp_get_node_name,
  824. .show_starget_port_id = 1,
  825. .show_starget_port_name = 1,
  826. .show_starget_node_name = 1,
  827. .show_rport_supported_classes = 1,
  828. .show_host_node_name = 1,
  829. .show_host_port_name = 1,
  830. .show_host_supported_classes = 1,
  831. .show_host_serial_number = 1,
  832. };
  833. /**
  834. * ZFCP_DEFINE_SCSI_ATTR
  835. * @_name: name of show attribute
  836. * @_format: format string
  837. * @_value: value to print
  838. *
  839. * Generates attribute for a unit.
  840. */
  841. #define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value) \
  842. static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr, \
  843. char *buf) \
  844. { \
  845. struct scsi_device *sdev; \
  846. struct zfcp_unit *unit; \
  847. \
  848. sdev = to_scsi_device(dev); \
  849. unit = sdev->hostdata; \
  850. return sprintf(buf, _format, _value); \
  851. } \
  852. \
  853. static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
  854. ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
  855. ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
  856. ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
  857. static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
  858. &dev_attr_fcp_lun,
  859. &dev_attr_wwpn,
  860. &dev_attr_hba_id,
  861. NULL
  862. };
  863. #undef ZFCP_LOG_AREA