zfcp_scsi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. /*
  2. * This file is part of the zfcp device driver for
  3. * FCP adapters for IBM System z9 and zSeries.
  4. *
  5. * (C) Copyright IBM Corp. 2002, 2006
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #define ZFCP_LOG_AREA ZFCP_LOG_AREA_SCSI
  22. #include "zfcp_ext.h"
  23. #include <asm/atomic.h>
  24. static void zfcp_scsi_slave_destroy(struct scsi_device *sdp);
  25. static int zfcp_scsi_slave_alloc(struct scsi_device *sdp);
  26. static int zfcp_scsi_slave_configure(struct scsi_device *sdp);
  27. static int zfcp_scsi_queuecommand(struct scsi_cmnd *,
  28. void (*done) (struct scsi_cmnd *));
  29. static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *);
  30. static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *);
  31. static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *);
  32. static int zfcp_task_management_function(struct zfcp_unit *, u8,
  33. struct scsi_cmnd *);
  34. static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *, int,
  35. unsigned int, unsigned int);
  36. static struct device_attribute *zfcp_sysfs_sdev_attrs[];
  37. struct zfcp_data zfcp_data = {
  38. .scsi_host_template = {
  39. .name = ZFCP_NAME,
  40. .module = THIS_MODULE,
  41. .proc_name = "zfcp",
  42. .slave_alloc = zfcp_scsi_slave_alloc,
  43. .slave_configure = zfcp_scsi_slave_configure,
  44. .slave_destroy = zfcp_scsi_slave_destroy,
  45. .queuecommand = zfcp_scsi_queuecommand,
  46. .eh_abort_handler = zfcp_scsi_eh_abort_handler,
  47. .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
  48. .eh_bus_reset_handler = zfcp_scsi_eh_host_reset_handler,
  49. .eh_host_reset_handler = zfcp_scsi_eh_host_reset_handler,
  50. .can_queue = 4096,
  51. .this_id = -1,
  52. .sg_tablesize = ZFCP_MAX_SBALES_PER_REQ,
  53. .cmd_per_lun = 1,
  54. .use_clustering = 1,
  55. .sdev_attrs = zfcp_sysfs_sdev_attrs,
  56. .max_sectors = ZFCP_MAX_SECTORS,
  57. },
  58. .driver_version = ZFCP_VERSION,
  59. };
  60. /* Find start of Response Information in FCP response unit*/
  61. char *
  62. zfcp_get_fcp_rsp_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
  63. {
  64. char *fcp_rsp_info_ptr;
  65. fcp_rsp_info_ptr =
  66. (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
  67. return fcp_rsp_info_ptr;
  68. }
  69. /* Find start of Sense Information in FCP response unit*/
  70. char *
  71. zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
  72. {
  73. char *fcp_sns_info_ptr;
  74. fcp_sns_info_ptr =
  75. (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
  76. if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
  77. fcp_sns_info_ptr = (char *) fcp_sns_info_ptr +
  78. fcp_rsp_iu->fcp_rsp_len;
  79. return fcp_sns_info_ptr;
  80. }
  81. static fcp_dl_t *
  82. zfcp_get_fcp_dl_ptr(struct fcp_cmnd_iu * fcp_cmd)
  83. {
  84. int additional_length = fcp_cmd->add_fcp_cdb_length << 2;
  85. fcp_dl_t *fcp_dl_addr;
  86. fcp_dl_addr = (fcp_dl_t *)
  87. ((unsigned char *) fcp_cmd +
  88. sizeof (struct fcp_cmnd_iu) + additional_length);
  89. /*
  90. * fcp_dl_addr = start address of fcp_cmnd structure +
  91. * size of fixed part + size of dynamically sized add_dcp_cdb field
  92. * SEE FCP-2 documentation
  93. */
  94. return fcp_dl_addr;
  95. }
  96. fcp_dl_t
  97. zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
  98. {
  99. return *zfcp_get_fcp_dl_ptr(fcp_cmd);
  100. }
  101. void
  102. zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
  103. {
  104. *zfcp_get_fcp_dl_ptr(fcp_cmd) = fcp_dl;
  105. }
  106. /*
  107. * note: it's a bit-or operation not an assignment
  108. * regarding the specified byte
  109. */
  110. static inline void
  111. set_byte(int *result, char status, char pos)
  112. {
  113. *result |= status << (pos * 8);
  114. }
  115. void
  116. set_host_byte(int *result, char status)
  117. {
  118. set_byte(result, status, 2);
  119. }
  120. void
  121. set_driver_byte(int *result, char status)
  122. {
  123. set_byte(result, status, 3);
  124. }
  125. static int
  126. zfcp_scsi_slave_alloc(struct scsi_device *sdp)
  127. {
  128. struct zfcp_adapter *adapter;
  129. struct zfcp_unit *unit;
  130. unsigned long flags;
  131. int retval = -ENXIO;
  132. adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
  133. if (!adapter)
  134. goto out;
  135. read_lock_irqsave(&zfcp_data.config_lock, flags);
  136. unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
  137. if (unit && atomic_test_mask(ZFCP_STATUS_UNIT_REGISTERED,
  138. &unit->status)) {
  139. sdp->hostdata = unit;
  140. unit->device = sdp;
  141. zfcp_unit_get(unit);
  142. retval = 0;
  143. }
  144. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  145. out:
  146. return retval;
  147. }
  148. /**
  149. * zfcp_scsi_slave_destroy - called when scsi device is removed
  150. *
  151. * Remove reference to associated scsi device for an zfcp_unit.
  152. * Mark zfcp_unit as failed. The scsi device might be deleted via sysfs
  153. * or a scan for this device might have failed.
  154. */
  155. static void zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
  156. {
  157. struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
  158. if (unit) {
  159. zfcp_erp_wait(unit->port->adapter);
  160. wait_event(unit->scsi_scan_wq,
  161. atomic_test_mask(ZFCP_STATUS_UNIT_SCSI_WORK_PENDING,
  162. &unit->status) == 0);
  163. atomic_clear_mask(ZFCP_STATUS_UNIT_REGISTERED, &unit->status);
  164. sdpnt->hostdata = NULL;
  165. unit->device = NULL;
  166. zfcp_erp_unit_failed(unit);
  167. zfcp_unit_put(unit);
  168. } else {
  169. ZFCP_LOG_NORMAL("bug: no unit associated with SCSI device at "
  170. "address %p\n", sdpnt);
  171. }
  172. }
  173. /*
  174. * called from scsi midlayer to allow finetuning of a device.
  175. */
  176. static int
  177. zfcp_scsi_slave_configure(struct scsi_device *sdp)
  178. {
  179. if (sdp->tagged_supported)
  180. scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
  181. else
  182. scsi_adjust_queue_depth(sdp, 0, 1);
  183. return 0;
  184. }
  185. /**
  186. * zfcp_scsi_command_fail - set result in scsi_cmnd and call scsi_done function
  187. * @scpnt: pointer to struct scsi_cmnd where result is set
  188. * @result: result to be set in scpnt (e.g. DID_ERROR)
  189. */
  190. static void
  191. zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
  192. {
  193. set_host_byte(&scpnt->result, result);
  194. if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
  195. zfcp_scsi_dbf_event_result("fail", 4,
  196. (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
  197. scpnt, NULL);
  198. /* return directly */
  199. scpnt->scsi_done(scpnt);
  200. }
  201. /**
  202. * zfcp_scsi_command_async - worker for zfcp_scsi_queuecommand and
  203. * zfcp_scsi_command_sync
  204. * @adapter: adapter where scsi command is issued
  205. * @unit: unit to which scsi command is sent
  206. * @scpnt: scsi command to be sent
  207. * @timer: timer to be started if request is successfully initiated
  208. *
  209. * Note: In scsi_done function must be set in scpnt.
  210. */
  211. int
  212. zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
  213. struct scsi_cmnd *scpnt, int use_timer)
  214. {
  215. int tmp;
  216. int retval;
  217. retval = 0;
  218. BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
  219. BUG_ON(scpnt->scsi_done == NULL);
  220. if (unlikely(NULL == unit)) {
  221. zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
  222. goto out;
  223. }
  224. if (unlikely(
  225. atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status) ||
  226. !atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status))) {
  227. ZFCP_LOG_DEBUG("stopping SCSI I/O on unit 0x%016Lx on port "
  228. "0x%016Lx on adapter %s\n",
  229. unit->fcp_lun, unit->port->wwpn,
  230. zfcp_get_busid_by_adapter(adapter));
  231. zfcp_scsi_command_fail(scpnt, DID_ERROR);
  232. goto out;
  233. }
  234. if (unlikely(
  235. !atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))) {
  236. ZFCP_LOG_DEBUG("adapter %s not ready or unit 0x%016Lx "
  237. "on port 0x%016Lx in recovery\n",
  238. zfcp_get_busid_by_unit(unit),
  239. unit->fcp_lun, unit->port->wwpn);
  240. zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
  241. goto out;
  242. }
  243. tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, use_timer,
  244. ZFCP_REQ_AUTO_CLEANUP);
  245. if (unlikely(tmp < 0)) {
  246. ZFCP_LOG_DEBUG("error: initiation of Send FCP Cmnd failed\n");
  247. retval = SCSI_MLQUEUE_HOST_BUSY;
  248. }
  249. out:
  250. return retval;
  251. }
  252. static void
  253. zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
  254. {
  255. struct completion *wait = (struct completion *) scpnt->SCp.ptr;
  256. complete(wait);
  257. }
  258. /**
  259. * zfcp_scsi_command_sync - send a SCSI command and wait for completion
  260. * @unit: unit where command is sent to
  261. * @scpnt: scsi command to be sent
  262. * @use_timer: indicates whether timer should be setup or not
  263. * Return: 0
  264. *
  265. * Errors are indicated in scpnt->result
  266. */
  267. int
  268. zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
  269. int use_timer)
  270. {
  271. int ret;
  272. DECLARE_COMPLETION_ONSTACK(wait);
  273. scpnt->SCp.ptr = (void *) &wait; /* silent re-use */
  274. scpnt->scsi_done = zfcp_scsi_command_sync_handler;
  275. ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt,
  276. use_timer);
  277. if (ret == 0)
  278. wait_for_completion(&wait);
  279. scpnt->SCp.ptr = NULL;
  280. return 0;
  281. }
  282. /*
  283. * function: zfcp_scsi_queuecommand
  284. *
  285. * purpose: enqueues a SCSI command to the specified target device
  286. *
  287. * returns: 0 - success, SCSI command enqueued
  288. * !0 - failure
  289. */
  290. static int
  291. zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
  292. void (*done) (struct scsi_cmnd *))
  293. {
  294. struct zfcp_unit *unit;
  295. struct zfcp_adapter *adapter;
  296. /* reset the status for this request */
  297. scpnt->result = 0;
  298. scpnt->host_scribble = NULL;
  299. scpnt->scsi_done = done;
  300. /*
  301. * figure out adapter and target device
  302. * (stored there by zfcp_scsi_slave_alloc)
  303. */
  304. adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
  305. unit = (struct zfcp_unit *) scpnt->device->hostdata;
  306. return zfcp_scsi_command_async(adapter, unit, scpnt, 0);
  307. }
  308. static struct zfcp_unit *
  309. zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, unsigned int id,
  310. unsigned int lun)
  311. {
  312. struct zfcp_port *port;
  313. struct zfcp_unit *unit, *retval = NULL;
  314. list_for_each_entry(port, &adapter->port_list_head, list) {
  315. if (!port->rport || (id != port->rport->scsi_target_id))
  316. continue;
  317. list_for_each_entry(unit, &port->unit_list_head, list) {
  318. if (lun == unit->scsi_lun) {
  319. retval = unit;
  320. goto out;
  321. }
  322. }
  323. }
  324. out:
  325. return retval;
  326. }
  327. /**
  328. * zfcp_scsi_eh_abort_handler - abort the specified SCSI command
  329. * @scpnt: pointer to scsi_cmnd to be aborted
  330. * Return: SUCCESS - command has been aborted and cleaned up in internal
  331. * bookkeeping, SCSI stack won't be called for aborted command
  332. * FAILED - otherwise
  333. *
  334. * We do not need to care for a SCSI command which completes normally
  335. * but late during this abort routine runs. We are allowed to return
  336. * late commands to the SCSI stack. It tracks the state of commands and
  337. * will handle late commands. (Usually, the normal completion of late
  338. * commands is ignored with respect to the running abort operation.)
  339. */
  340. static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
  341. {
  342. struct Scsi_Host *scsi_host;
  343. struct zfcp_adapter *adapter;
  344. struct zfcp_unit *unit;
  345. struct zfcp_fsf_req *fsf_req;
  346. unsigned long flags;
  347. unsigned long old_req_id;
  348. int retval = SUCCESS;
  349. scsi_host = scpnt->device->host;
  350. adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
  351. unit = (struct zfcp_unit *) scpnt->device->hostdata;
  352. ZFCP_LOG_INFO("aborting scsi_cmnd=%p on adapter %s\n",
  353. scpnt, zfcp_get_busid_by_adapter(adapter));
  354. /* avoid race condition between late normal completion and abort */
  355. write_lock_irqsave(&adapter->abort_lock, flags);
  356. /* Check whether corresponding fsf_req is still pending */
  357. spin_lock(&adapter->req_list_lock);
  358. fsf_req = zfcp_reqlist_find(adapter,
  359. (unsigned long) scpnt->host_scribble);
  360. spin_unlock(&adapter->req_list_lock);
  361. if (!fsf_req) {
  362. write_unlock_irqrestore(&adapter->abort_lock, flags);
  363. zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, 0);
  364. retval = SUCCESS;
  365. goto out;
  366. }
  367. fsf_req->data = 0;
  368. fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
  369. old_req_id = fsf_req->req_id;
  370. /* don't access old fsf_req after releasing the abort_lock */
  371. write_unlock_irqrestore(&adapter->abort_lock, flags);
  372. fsf_req = zfcp_fsf_abort_fcp_command(old_req_id, adapter, unit, 0);
  373. if (!fsf_req) {
  374. ZFCP_LOG_INFO("error: initiation of Abort FCP Cmnd failed\n");
  375. zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
  376. old_req_id);
  377. retval = FAILED;
  378. goto out;
  379. }
  380. __wait_event(fsf_req->completion_wq,
  381. fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
  382. if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
  383. zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, fsf_req, 0);
  384. retval = SUCCESS;
  385. } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
  386. zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, fsf_req, 0);
  387. retval = SUCCESS;
  388. } else {
  389. zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, fsf_req, 0);
  390. retval = FAILED;
  391. }
  392. zfcp_fsf_req_free(fsf_req);
  393. out:
  394. return retval;
  395. }
  396. static int
  397. zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
  398. {
  399. int retval;
  400. struct zfcp_unit *unit = (struct zfcp_unit *) scpnt->device->hostdata;
  401. if (!unit) {
  402. ZFCP_LOG_NORMAL("bug: Tried reset for nonexistent unit\n");
  403. retval = SUCCESS;
  404. goto out;
  405. }
  406. ZFCP_LOG_NORMAL("resetting unit 0x%016Lx\n", unit->fcp_lun);
  407. /*
  408. * If we do not know whether the unit supports 'logical unit reset'
  409. * then try 'logical unit reset' and proceed with 'target reset'
  410. * if 'logical unit reset' fails.
  411. * If the unit is known not to support 'logical unit reset' then
  412. * skip 'logical unit reset' and try 'target reset' immediately.
  413. */
  414. if (!atomic_test_mask(ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
  415. &unit->status)) {
  416. retval = zfcp_task_management_function(unit,
  417. FCP_LOGICAL_UNIT_RESET,
  418. scpnt);
  419. if (retval) {
  420. ZFCP_LOG_DEBUG("unit reset failed (unit=%p)\n", unit);
  421. if (retval == -ENOTSUPP)
  422. atomic_set_mask
  423. (ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
  424. &unit->status);
  425. /* fall through and try 'target reset' next */
  426. } else {
  427. ZFCP_LOG_DEBUG("unit reset succeeded (unit=%p)\n",
  428. unit);
  429. /* avoid 'target reset' */
  430. retval = SUCCESS;
  431. goto out;
  432. }
  433. }
  434. retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
  435. if (retval) {
  436. ZFCP_LOG_DEBUG("target reset failed (unit=%p)\n", unit);
  437. retval = FAILED;
  438. } else {
  439. ZFCP_LOG_DEBUG("target reset succeeded (unit=%p)\n", unit);
  440. retval = SUCCESS;
  441. }
  442. out:
  443. return retval;
  444. }
  445. static int
  446. zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags,
  447. struct scsi_cmnd *scpnt)
  448. {
  449. struct zfcp_adapter *adapter = unit->port->adapter;
  450. struct zfcp_fsf_req *fsf_req;
  451. int retval = 0;
  452. /* issue task management function */
  453. fsf_req = zfcp_fsf_send_fcp_command_task_management
  454. (adapter, unit, tm_flags, 0);
  455. if (!fsf_req) {
  456. ZFCP_LOG_INFO("error: creation of task management request "
  457. "failed for unit 0x%016Lx on port 0x%016Lx on "
  458. "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
  459. zfcp_get_busid_by_adapter(adapter));
  460. zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
  461. retval = -ENOMEM;
  462. goto out;
  463. }
  464. __wait_event(fsf_req->completion_wq,
  465. fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
  466. /*
  467. * check completion status of task management function
  468. */
  469. if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
  470. zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
  471. retval = -EIO;
  472. } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
  473. zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
  474. retval = -ENOTSUPP;
  475. } else
  476. zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
  477. zfcp_fsf_req_free(fsf_req);
  478. out:
  479. return retval;
  480. }
  481. /**
  482. * zfcp_scsi_eh_host_reset_handler - handler for host and bus reset
  483. */
  484. static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
  485. {
  486. struct zfcp_unit *unit;
  487. struct zfcp_adapter *adapter;
  488. unit = (struct zfcp_unit*) scpnt->device->hostdata;
  489. adapter = unit->port->adapter;
  490. ZFCP_LOG_NORMAL("host/bus reset because of problems with "
  491. "unit 0x%016Lx\n", unit->fcp_lun);
  492. zfcp_erp_adapter_reopen(adapter, 0);
  493. zfcp_erp_wait(adapter);
  494. return SUCCESS;
  495. }
  496. int
  497. zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
  498. {
  499. int retval = 0;
  500. static unsigned int unique_id = 0;
  501. /* register adapter as SCSI host with mid layer of SCSI stack */
  502. adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
  503. sizeof (struct zfcp_adapter *));
  504. if (!adapter->scsi_host) {
  505. ZFCP_LOG_NORMAL("error: registration with SCSI stack failed "
  506. "for adapter %s ",
  507. zfcp_get_busid_by_adapter(adapter));
  508. retval = -EIO;
  509. goto out;
  510. }
  511. ZFCP_LOG_DEBUG("host registered, scsi_host=%p\n", adapter->scsi_host);
  512. /* tell the SCSI stack some characteristics of this adapter */
  513. adapter->scsi_host->max_id = 1;
  514. adapter->scsi_host->max_lun = 1;
  515. adapter->scsi_host->max_channel = 0;
  516. adapter->scsi_host->unique_id = unique_id++; /* FIXME */
  517. adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
  518. adapter->scsi_host->transportt = zfcp_data.scsi_transport_template;
  519. /*
  520. * save a pointer to our own adapter data structure within
  521. * hostdata field of SCSI host data structure
  522. */
  523. adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
  524. if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
  525. scsi_host_put(adapter->scsi_host);
  526. retval = -EIO;
  527. goto out;
  528. }
  529. atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
  530. out:
  531. return retval;
  532. }
  533. void
  534. zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
  535. {
  536. struct Scsi_Host *shost;
  537. struct zfcp_port *port;
  538. shost = adapter->scsi_host;
  539. if (!shost)
  540. return;
  541. read_lock_irq(&zfcp_data.config_lock);
  542. list_for_each_entry(port, &adapter->port_list_head, list)
  543. if (port->rport)
  544. port->rport = NULL;
  545. read_unlock_irq(&zfcp_data.config_lock);
  546. fc_remove_host(shost);
  547. scsi_remove_host(shost);
  548. scsi_host_put(shost);
  549. adapter->scsi_host = NULL;
  550. atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
  551. return;
  552. }
  553. /*
  554. * Support functions for FC transport class
  555. */
  556. static struct fc_host_statistics*
  557. zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
  558. {
  559. struct fc_host_statistics *fc_stats;
  560. if (!adapter->fc_stats) {
  561. fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
  562. if (!fc_stats)
  563. return NULL;
  564. adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
  565. }
  566. memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
  567. return adapter->fc_stats;
  568. }
  569. static void
  570. zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
  571. struct fsf_qtcb_bottom_port *data,
  572. struct fsf_qtcb_bottom_port *old)
  573. {
  574. fc_stats->seconds_since_last_reset = data->seconds_since_last_reset -
  575. old->seconds_since_last_reset;
  576. fc_stats->tx_frames = data->tx_frames - old->tx_frames;
  577. fc_stats->tx_words = data->tx_words - old->tx_words;
  578. fc_stats->rx_frames = data->rx_frames - old->rx_frames;
  579. fc_stats->rx_words = data->rx_words - old->rx_words;
  580. fc_stats->lip_count = data->lip - old->lip;
  581. fc_stats->nos_count = data->nos - old->nos;
  582. fc_stats->error_frames = data->error_frames - old->error_frames;
  583. fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
  584. fc_stats->link_failure_count = data->link_failure - old->link_failure;
  585. fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
  586. fc_stats->loss_of_signal_count = data->loss_of_signal -
  587. old->loss_of_signal;
  588. fc_stats->prim_seq_protocol_err_count = data->psp_error_counts -
  589. old->psp_error_counts;
  590. fc_stats->invalid_tx_word_count = data->invalid_tx_words -
  591. old->invalid_tx_words;
  592. fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
  593. fc_stats->fcp_input_requests = data->input_requests -
  594. old->input_requests;
  595. fc_stats->fcp_output_requests = data->output_requests -
  596. old->output_requests;
  597. fc_stats->fcp_control_requests = data->control_requests -
  598. old->control_requests;
  599. fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
  600. fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
  601. }
  602. static void
  603. zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
  604. struct fsf_qtcb_bottom_port *data)
  605. {
  606. fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
  607. fc_stats->tx_frames = data->tx_frames;
  608. fc_stats->tx_words = data->tx_words;
  609. fc_stats->rx_frames = data->rx_frames;
  610. fc_stats->rx_words = data->rx_words;
  611. fc_stats->lip_count = data->lip;
  612. fc_stats->nos_count = data->nos;
  613. fc_stats->error_frames = data->error_frames;
  614. fc_stats->dumped_frames = data->dumped_frames;
  615. fc_stats->link_failure_count = data->link_failure;
  616. fc_stats->loss_of_sync_count = data->loss_of_sync;
  617. fc_stats->loss_of_signal_count = data->loss_of_signal;
  618. fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
  619. fc_stats->invalid_tx_word_count = data->invalid_tx_words;
  620. fc_stats->invalid_crc_count = data->invalid_crcs;
  621. fc_stats->fcp_input_requests = data->input_requests;
  622. fc_stats->fcp_output_requests = data->output_requests;
  623. fc_stats->fcp_control_requests = data->control_requests;
  624. fc_stats->fcp_input_megabytes = data->input_mb;
  625. fc_stats->fcp_output_megabytes = data->output_mb;
  626. }
  627. /**
  628. * zfcp_get_fc_host_stats - provide fc_host_statistics for scsi_transport_fc
  629. *
  630. * assumption: scsi_transport_fc synchronizes calls of
  631. * get_fc_host_stats and reset_fc_host_stats
  632. * (XXX to be checked otherwise introduce locking)
  633. */
  634. static struct fc_host_statistics *
  635. zfcp_get_fc_host_stats(struct Scsi_Host *shost)
  636. {
  637. struct zfcp_adapter *adapter;
  638. struct fc_host_statistics *fc_stats;
  639. struct fsf_qtcb_bottom_port *data;
  640. int ret;
  641. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  642. fc_stats = zfcp_init_fc_host_stats(adapter);
  643. if (!fc_stats)
  644. return NULL;
  645. data = kzalloc(sizeof(*data), GFP_KERNEL);
  646. if (!data)
  647. return NULL;
  648. ret = zfcp_fsf_exchange_port_data(NULL, adapter, data);
  649. if (ret) {
  650. kfree(data);
  651. return NULL; /* XXX return zeroed fc_stats? */
  652. }
  653. if (adapter->stats_reset &&
  654. ((jiffies/HZ - adapter->stats_reset) <
  655. data->seconds_since_last_reset)) {
  656. zfcp_adjust_fc_host_stats(fc_stats, data,
  657. adapter->stats_reset_data);
  658. } else
  659. zfcp_set_fc_host_stats(fc_stats, data);
  660. kfree(data);
  661. return fc_stats;
  662. }
  663. static void
  664. zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
  665. {
  666. struct zfcp_adapter *adapter;
  667. struct fsf_qtcb_bottom_port *data, *old_data;
  668. int ret;
  669. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  670. data = kzalloc(sizeof(*data), GFP_KERNEL);
  671. if (!data)
  672. return;
  673. ret = zfcp_fsf_exchange_port_data(NULL, adapter, data);
  674. if (ret == 0) {
  675. adapter->stats_reset = jiffies/HZ;
  676. old_data = adapter->stats_reset_data;
  677. adapter->stats_reset_data = data; /* finally freed in
  678. adater_dequeue */
  679. kfree(old_data);
  680. }
  681. }
  682. static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
  683. {
  684. rport->dev_loss_tmo = timeout;
  685. }
  686. struct fc_function_template zfcp_transport_functions = {
  687. .show_starget_port_id = 1,
  688. .show_starget_port_name = 1,
  689. .show_starget_node_name = 1,
  690. .show_rport_supported_classes = 1,
  691. .show_rport_maxframe_size = 1,
  692. .show_rport_dev_loss_tmo = 1,
  693. .show_host_node_name = 1,
  694. .show_host_port_name = 1,
  695. .show_host_permanent_port_name = 1,
  696. .show_host_supported_classes = 1,
  697. .show_host_supported_speeds = 1,
  698. .show_host_maxframe_size = 1,
  699. .show_host_serial_number = 1,
  700. .get_fc_host_stats = zfcp_get_fc_host_stats,
  701. .reset_fc_host_stats = zfcp_reset_fc_host_stats,
  702. .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
  703. /* no functions registered for following dynamic attributes but
  704. directly set by LLDD */
  705. .show_host_port_type = 1,
  706. .show_host_speed = 1,
  707. .show_host_port_id = 1,
  708. };
  709. /**
  710. * ZFCP_DEFINE_SCSI_ATTR
  711. * @_name: name of show attribute
  712. * @_format: format string
  713. * @_value: value to print
  714. *
  715. * Generates attribute for a unit.
  716. */
  717. #define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value) \
  718. static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr, \
  719. char *buf) \
  720. { \
  721. struct scsi_device *sdev; \
  722. struct zfcp_unit *unit; \
  723. \
  724. sdev = to_scsi_device(dev); \
  725. unit = sdev->hostdata; \
  726. return sprintf(buf, _format, _value); \
  727. } \
  728. \
  729. static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
  730. ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
  731. ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
  732. ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
  733. static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
  734. &dev_attr_fcp_lun,
  735. &dev_attr_wwpn,
  736. &dev_attr_hba_id,
  737. NULL
  738. };
  739. #undef ZFCP_LOG_AREA